ubi.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006, 2007
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём)
  20. */
  21. #ifndef __UBI_UBI_H__
  22. #define __UBI_UBI_H__
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/list.h>
  26. #include <linux/rbtree.h>
  27. #include <linux/sched.h>
  28. #include <linux/wait.h>
  29. #include <linux/mutex.h>
  30. #include <linux/rwsem.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/fs.h>
  33. #include <linux/cdev.h>
  34. #include <linux/device.h>
  35. #include <linux/slab.h>
  36. #include <linux/string.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/notifier.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/ubi.h>
  41. #include <asm/pgtable.h>
  42. #include "ubi-media.h"
  43. #include "scan.h"
  44. /* Maximum number of supported UBI devices */
  45. #define UBI_MAX_DEVICES 32
  46. /* UBI name used for character devices, sysfs, etc */
  47. #define UBI_NAME_STR "ubi"
  48. /* Normal UBI messages */
  49. #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
  50. /* UBI warning messages */
  51. #define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \
  52. __func__, ##__VA_ARGS__)
  53. /* UBI error messages */
  54. #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
  55. __func__, ##__VA_ARGS__)
  56. /* Lowest number PEBs reserved for bad PEB handling */
  57. #define MIN_RESEVED_PEBS 2
  58. /* Background thread name pattern */
  59. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  60. /* This marker in the EBA table means that the LEB is um-mapped */
  61. #define UBI_LEB_UNMAPPED -1
  62. /*
  63. * In case of errors, UBI tries to repeat the operation several times before
  64. * returning error. The below constant defines how many times UBI re-tries.
  65. */
  66. #define UBI_IO_RETRIES 3
  67. /*
  68. * Length of the protection queue. The length is effectively equivalent to the
  69. * number of (global) erase cycles PEBs are protected from the wear-leveling
  70. * worker.
  71. */
  72. #define UBI_PROT_QUEUE_LEN 10
  73. /*
  74. * Error codes returned by the I/O sub-system.
  75. *
  76. * UBI_IO_FF: the read region of flash contains only 0xFFs
  77. * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
  78. * integrity error reported by the MTD driver
  79. * (uncorrectable ECC error in case of NAND)
  80. * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
  81. * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
  82. * data integrity error reported by the MTD driver
  83. * (uncorrectable ECC error in case of NAND)
  84. * UBI_IO_BITFLIPS: bit-flips were detected and corrected
  85. *
  86. * Note, it is probably better to have bit-flip and ebadmsg as flags which can
  87. * be or'ed with other error code. But this is a big change because there are
  88. * may callers, so it does not worth the risk of introducing a bug
  89. */
  90. enum {
  91. UBI_IO_FF = 1,
  92. UBI_IO_FF_BITFLIPS,
  93. UBI_IO_BAD_HDR,
  94. UBI_IO_BAD_HDR_EBADMSG,
  95. UBI_IO_BITFLIPS,
  96. };
  97. /*
  98. * Return codes of the 'ubi_eba_copy_leb()' function.
  99. *
  100. * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
  101. * PEB was put meanwhile, or there is I/O on the source PEB
  102. * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
  103. * PEB
  104. * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
  105. * PEB
  106. * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
  107. * PEB
  108. * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
  109. * target PEB
  110. * MOVE_RETRY: retry scrubbing the PEB
  111. */
  112. enum {
  113. MOVE_CANCEL_RACE = 1,
  114. MOVE_SOURCE_RD_ERR,
  115. MOVE_TARGET_RD_ERR,
  116. MOVE_TARGET_WR_ERR,
  117. MOVE_TARGET_BITFLIPS,
  118. MOVE_RETRY,
  119. };
  120. /**
  121. * struct ubi_wl_entry - wear-leveling entry.
  122. * @u.rb: link in the corresponding (free/used) RB-tree
  123. * @u.list: link in the protection queue
  124. * @ec: erase counter
  125. * @pnum: physical eraseblock number
  126. *
  127. * This data structure is used in the WL sub-system. Each physical eraseblock
  128. * has a corresponding &struct wl_entry object which may be kept in different
  129. * RB-trees. See WL sub-system for details.
  130. */
  131. struct ubi_wl_entry {
  132. union {
  133. struct rb_node rb;
  134. struct list_head list;
  135. } u;
  136. int ec;
  137. int pnum;
  138. };
  139. /**
  140. * struct ubi_ltree_entry - an entry in the lock tree.
  141. * @rb: links RB-tree nodes
  142. * @vol_id: volume ID of the locked logical eraseblock
  143. * @lnum: locked logical eraseblock number
  144. * @users: how many tasks are using this logical eraseblock or wait for it
  145. * @mutex: read/write mutex to implement read/write access serialization to
  146. * the (@vol_id, @lnum) logical eraseblock
  147. *
  148. * This data structure is used in the EBA sub-system to implement per-LEB
  149. * locking. When a logical eraseblock is being locked - corresponding
  150. * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
  151. * See EBA sub-system for details.
  152. */
  153. struct ubi_ltree_entry {
  154. struct rb_node rb;
  155. int vol_id;
  156. int lnum;
  157. int users;
  158. struct rw_semaphore mutex;
  159. };
  160. /**
  161. * struct ubi_rename_entry - volume re-name description data structure.
  162. * @new_name_len: new volume name length
  163. * @new_name: new volume name
  164. * @remove: if not zero, this volume should be removed, not re-named
  165. * @desc: descriptor of the volume
  166. * @list: links re-name entries into a list
  167. *
  168. * This data structure is utilized in the multiple volume re-name code. Namely,
  169. * UBI first creates a list of &struct ubi_rename_entry objects from the
  170. * &struct ubi_rnvol_req request object, and then utilizes this list to do all
  171. * the job.
  172. */
  173. struct ubi_rename_entry {
  174. int new_name_len;
  175. char new_name[UBI_VOL_NAME_MAX + 1];
  176. int remove;
  177. struct ubi_volume_desc *desc;
  178. struct list_head list;
  179. };
  180. struct ubi_volume_desc;
  181. /**
  182. * struct ubi_volume - UBI volume description data structure.
  183. * @dev: device object to make use of the the Linux device model
  184. * @cdev: character device object to create character device
  185. * @ubi: reference to the UBI device description object
  186. * @vol_id: volume ID
  187. * @ref_count: volume reference count
  188. * @readers: number of users holding this volume in read-only mode
  189. * @writers: number of users holding this volume in read-write mode
  190. * @exclusive: whether somebody holds this volume in exclusive mode
  191. *
  192. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  193. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  194. * @usable_leb_size: logical eraseblock size without padding
  195. * @used_ebs: how many logical eraseblocks in this volume contain data
  196. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  197. * @used_bytes: how many bytes of data this volume contains
  198. * @alignment: volume alignment
  199. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  200. * satisfy the requested alignment
  201. * @name_len: volume name length
  202. * @name: volume name
  203. *
  204. * @upd_ebs: how many eraseblocks are expected to be updated
  205. * @ch_lnum: LEB number which is being changing by the atomic LEB change
  206. * operation
  207. * @ch_dtype: data persistency type which is being changing by the atomic LEB
  208. * change operation
  209. * @upd_bytes: how many bytes are expected to be received for volume update or
  210. * atomic LEB change
  211. * @upd_received: how many bytes were already received for volume update or
  212. * atomic LEB change
  213. * @upd_buf: update buffer which is used to collect update data or data for
  214. * atomic LEB change
  215. *
  216. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  217. * @checked: %1 if this static volume was checked
  218. * @corrupted: %1 if the volume is corrupted (static volumes only)
  219. * @upd_marker: %1 if the update marker is set for this volume
  220. * @updating: %1 if the volume is being updated
  221. * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
  222. * @direct_writes: %1 if direct writes are enabled for this volume
  223. *
  224. * The @corrupted field indicates that the volume's contents is corrupted.
  225. * Since UBI protects only static volumes, this field is not relevant to
  226. * dynamic volumes - it is user's responsibility to assure their data
  227. * integrity.
  228. *
  229. * The @upd_marker flag indicates that this volume is either being updated at
  230. * the moment or is damaged because of an unclean reboot.
  231. */
  232. struct ubi_volume {
  233. struct device dev;
  234. struct cdev cdev;
  235. struct ubi_device *ubi;
  236. int vol_id;
  237. int ref_count;
  238. int readers;
  239. int writers;
  240. int exclusive;
  241. int reserved_pebs;
  242. int vol_type;
  243. int usable_leb_size;
  244. int used_ebs;
  245. int last_eb_bytes;
  246. long long used_bytes;
  247. int alignment;
  248. int data_pad;
  249. int name_len;
  250. char name[UBI_VOL_NAME_MAX + 1];
  251. int upd_ebs;
  252. int ch_lnum;
  253. int ch_dtype;
  254. long long upd_bytes;
  255. long long upd_received;
  256. void *upd_buf;
  257. int *eba_tbl;
  258. unsigned int checked:1;
  259. unsigned int corrupted:1;
  260. unsigned int upd_marker:1;
  261. unsigned int updating:1;
  262. unsigned int changing_leb:1;
  263. unsigned int direct_writes:1;
  264. };
  265. /**
  266. * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
  267. * @vol: reference to the corresponding volume description object
  268. * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
  269. */
  270. struct ubi_volume_desc {
  271. struct ubi_volume *vol;
  272. int mode;
  273. };
  274. struct ubi_wl_entry;
  275. /**
  276. * struct ubi_device - UBI device description structure
  277. * @dev: UBI device object to use the the Linux device model
  278. * @cdev: character device object to create character device
  279. * @ubi_num: UBI device number
  280. * @ubi_name: UBI device name
  281. * @vol_count: number of volumes in this UBI device
  282. * @volumes: volumes of this UBI device
  283. * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
  284. * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
  285. * @vol->readers, @vol->writers, @vol->exclusive,
  286. * @vol->ref_count, @vol->mapping and @vol->eba_tbl.
  287. * @ref_count: count of references on the UBI device
  288. * @image_seq: image sequence number recorded on EC headers
  289. *
  290. * @rsvd_pebs: count of reserved physical eraseblocks
  291. * @avail_pebs: count of available physical eraseblocks
  292. * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
  293. * handling
  294. * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
  295. *
  296. * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
  297. * of UBI initialization
  298. * @vtbl_slots: how many slots are available in the volume table
  299. * @vtbl_size: size of the volume table in bytes
  300. * @vtbl: in-RAM volume table copy
  301. * @device_mutex: protects on-flash volume table and serializes volume
  302. * creation, deletion, update, re-size, re-name and set
  303. * property
  304. *
  305. * @max_ec: current highest erase counter value
  306. * @mean_ec: current mean erase counter value
  307. *
  308. * @global_sqnum: global sequence number
  309. * @ltree_lock: protects the lock tree and @global_sqnum
  310. * @ltree: the lock tree
  311. * @alc_mutex: serializes "atomic LEB change" operations
  312. *
  313. * @used: RB-tree of used physical eraseblocks
  314. * @erroneous: RB-tree of erroneous used physical eraseblocks
  315. * @free: RB-tree of free physical eraseblocks
  316. * @scrub: RB-tree of physical eraseblocks which need scrubbing
  317. * @pq: protection queue (contain physical eraseblocks which are temporarily
  318. * protected from the wear-leveling worker)
  319. * @pq_head: protection queue head
  320. * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
  321. * @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
  322. * @erroneous, and @erroneous_peb_count fields
  323. * @move_mutex: serializes eraseblock moves
  324. * @work_sem: synchronizes the WL worker with use tasks
  325. * @wl_scheduled: non-zero if the wear-leveling was scheduled
  326. * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
  327. * physical eraseblock
  328. * @move_from: physical eraseblock from where the data is being moved
  329. * @move_to: physical eraseblock where the data is being moved to
  330. * @move_to_put: if the "to" PEB was put
  331. * @works: list of pending works
  332. * @works_count: count of pending works
  333. * @bgt_thread: background thread description object
  334. * @thread_enabled: if the background thread is enabled
  335. * @bgt_name: background thread name
  336. *
  337. * @flash_size: underlying MTD device size (in bytes)
  338. * @peb_count: count of physical eraseblocks on the MTD device
  339. * @peb_size: physical eraseblock size
  340. * @bad_peb_count: count of bad physical eraseblocks
  341. * @good_peb_count: count of good physical eraseblocks
  342. * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
  343. * used by UBI)
  344. * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
  345. * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
  346. * @min_io_size: minimal input/output unit size of the underlying MTD device
  347. * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
  348. * @ro_mode: if the UBI device is in read-only mode
  349. * @leb_size: logical eraseblock size
  350. * @leb_start: starting offset of logical eraseblocks within physical
  351. * eraseblocks
  352. * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
  353. * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
  354. * @vid_hdr_offset: starting offset of the volume identifier header (might be
  355. * unaligned)
  356. * @vid_hdr_aloffset: starting offset of the VID header aligned to
  357. * @hdrs_min_io_size
  358. * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
  359. * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
  360. * not
  361. * @nor_flash: non-zero if working on top of NOR flash
  362. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  363. * time (MTD write buffer size)
  364. * @mtd: MTD device descriptor
  365. *
  366. * @peb_buf: a buffer of PEB size used for different purposes
  367. * @buf_mutex: protects @peb_buf
  368. * @ckvol_mutex: serializes static volume checking when opening
  369. *
  370. * @dbg: debugging information for this UBI device
  371. */
  372. struct ubi_device {
  373. struct cdev cdev;
  374. struct device dev;
  375. int ubi_num;
  376. char ubi_name[sizeof(UBI_NAME_STR)+5];
  377. int vol_count;
  378. struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
  379. spinlock_t volumes_lock;
  380. int ref_count;
  381. int image_seq;
  382. int rsvd_pebs;
  383. int avail_pebs;
  384. int beb_rsvd_pebs;
  385. int beb_rsvd_level;
  386. int autoresize_vol_id;
  387. int vtbl_slots;
  388. int vtbl_size;
  389. struct ubi_vtbl_record *vtbl;
  390. struct mutex device_mutex;
  391. int max_ec;
  392. /* Note, mean_ec is not updated run-time - should be fixed */
  393. int mean_ec;
  394. /* EBA sub-system's stuff */
  395. unsigned long long global_sqnum;
  396. spinlock_t ltree_lock;
  397. struct rb_root ltree;
  398. struct mutex alc_mutex;
  399. /* Wear-leveling sub-system's stuff */
  400. struct rb_root used;
  401. struct rb_root erroneous;
  402. struct rb_root free;
  403. struct rb_root scrub;
  404. struct list_head pq[UBI_PROT_QUEUE_LEN];
  405. int pq_head;
  406. spinlock_t wl_lock;
  407. struct mutex move_mutex;
  408. struct rw_semaphore work_sem;
  409. int wl_scheduled;
  410. struct ubi_wl_entry **lookuptbl;
  411. struct ubi_wl_entry *move_from;
  412. struct ubi_wl_entry *move_to;
  413. int move_to_put;
  414. struct list_head works;
  415. int works_count;
  416. struct task_struct *bgt_thread;
  417. int thread_enabled;
  418. char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
  419. /* I/O sub-system's stuff */
  420. long long flash_size;
  421. int peb_count;
  422. int peb_size;
  423. int bad_peb_count;
  424. int good_peb_count;
  425. int corr_peb_count;
  426. int erroneous_peb_count;
  427. int max_erroneous;
  428. int min_io_size;
  429. int hdrs_min_io_size;
  430. int ro_mode;
  431. int leb_size;
  432. int leb_start;
  433. int ec_hdr_alsize;
  434. int vid_hdr_alsize;
  435. int vid_hdr_offset;
  436. int vid_hdr_aloffset;
  437. int vid_hdr_shift;
  438. unsigned int bad_allowed:1;
  439. unsigned int nor_flash:1;
  440. int max_write_size;
  441. struct mtd_info *mtd;
  442. void *peb_buf;
  443. struct mutex buf_mutex;
  444. struct mutex ckvol_mutex;
  445. struct ubi_debug_info *dbg;
  446. };
  447. #include "debug.h"
  448. extern struct kmem_cache *ubi_wl_entry_slab;
  449. extern const struct file_operations ubi_ctrl_cdev_operations;
  450. extern const struct file_operations ubi_cdev_operations;
  451. extern const struct file_operations ubi_vol_cdev_operations;
  452. extern struct class *ubi_class;
  453. extern struct mutex ubi_devices_mutex;
  454. extern struct blocking_notifier_head ubi_notifiers;
  455. /* vtbl.c */
  456. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  457. struct ubi_vtbl_record *vtbl_rec);
  458. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  459. struct list_head *rename_list);
  460. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
  461. /* vmt.c */
  462. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  463. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
  464. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  465. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
  466. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  467. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  468. /* upd.c */
  469. int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
  470. long long bytes);
  471. int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
  472. const void __user *buf, int count);
  473. int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  474. const struct ubi_leb_change_req *req);
  475. int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
  476. const void __user *buf, int count);
  477. /* misc.c */
  478. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  479. int length);
  480. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  481. void ubi_calculate_reserved(struct ubi_device *ubi);
  482. int ubi_check_pattern(const void *buf, uint8_t patt, int size);
  483. /* eba.c */
  484. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  485. int lnum);
  486. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  487. void *buf, int offset, int len, int check);
  488. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  489. const void *buf, int offset, int len, int dtype);
  490. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  491. int lnum, const void *buf, int len, int dtype,
  492. int used_ebs);
  493. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  494. int lnum, const void *buf, int len, int dtype);
  495. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  496. struct ubi_vid_hdr *vid_hdr);
  497. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  498. /* wl.c */
  499. int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
  500. int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
  501. int ubi_wl_flush(struct ubi_device *ubi);
  502. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  503. int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  504. void ubi_wl_close(struct ubi_device *ubi);
  505. int ubi_thread(void *u);
  506. /* io.c */
  507. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  508. int len);
  509. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  510. int len);
  511. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
  512. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  513. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  514. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  515. struct ubi_ec_hdr *ec_hdr, int verbose);
  516. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  517. struct ubi_ec_hdr *ec_hdr);
  518. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  519. struct ubi_vid_hdr *vid_hdr, int verbose);
  520. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  521. struct ubi_vid_hdr *vid_hdr);
  522. /* build.c */
  523. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
  524. int ubi_detach_mtd_dev(int ubi_num, int anyway);
  525. struct ubi_device *ubi_get_device(int ubi_num);
  526. void ubi_put_device(struct ubi_device *ubi);
  527. struct ubi_device *ubi_get_by_major(int major);
  528. int ubi_major2num(int major);
  529. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
  530. int ntype);
  531. int ubi_notify_all(struct ubi_device *ubi, int ntype,
  532. struct notifier_block *nb);
  533. int ubi_enumerate_volumes(struct notifier_block *nb);
  534. /* kapi.c */
  535. void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
  536. void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
  537. struct ubi_volume_info *vi);
  538. /*
  539. * ubi_rb_for_each_entry - walk an RB-tree.
  540. * @rb: a pointer to type 'struct rb_node' to use as a loop counter
  541. * @pos: a pointer to RB-tree entry type to use as a loop counter
  542. * @root: RB-tree's root
  543. * @member: the name of the 'struct rb_node' within the RB-tree entry
  544. */
  545. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  546. for (rb = rb_first(root), \
  547. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  548. rb; \
  549. rb = rb_next(rb), \
  550. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
  551. /**
  552. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  553. * @ubi: UBI device description object
  554. * @gfp_flags: GFP flags to allocate with
  555. *
  556. * This function returns a pointer to the newly allocated and zero-filled
  557. * volume identifier header object in case of success and %NULL in case of
  558. * failure.
  559. */
  560. static inline struct ubi_vid_hdr *
  561. ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
  562. {
  563. void *vid_hdr;
  564. vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
  565. if (!vid_hdr)
  566. return NULL;
  567. /*
  568. * VID headers may be stored at un-aligned flash offsets, so we shift
  569. * the pointer.
  570. */
  571. return vid_hdr + ubi->vid_hdr_shift;
  572. }
  573. /**
  574. * ubi_free_vid_hdr - free a volume identifier header object.
  575. * @ubi: UBI device description object
  576. * @vid_hdr: the object to free
  577. */
  578. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  579. struct ubi_vid_hdr *vid_hdr)
  580. {
  581. void *p = vid_hdr;
  582. if (!p)
  583. return;
  584. kfree(p - ubi->vid_hdr_shift);
  585. }
  586. /*
  587. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  588. * the beginning of the logical eraseblock, not to the beginning of the
  589. * physical eraseblock.
  590. */
  591. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  592. int pnum, int offset, int len)
  593. {
  594. ubi_assert(offset >= 0);
  595. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  596. }
  597. /*
  598. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  599. * the beginning of the logical eraseblock, not to the beginning of the
  600. * physical eraseblock.
  601. */
  602. static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
  603. int pnum, int offset, int len)
  604. {
  605. ubi_assert(offset >= 0);
  606. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  607. }
  608. /**
  609. * ubi_ro_mode - switch to read-only mode.
  610. * @ubi: UBI device description object
  611. */
  612. static inline void ubi_ro_mode(struct ubi_device *ubi)
  613. {
  614. if (!ubi->ro_mode) {
  615. ubi->ro_mode = 1;
  616. ubi_warn("switch to read-only mode");
  617. ubi_dbg_dump_stack();
  618. }
  619. }
  620. /**
  621. * vol_id2idx - get table index by volume ID.
  622. * @ubi: UBI device description object
  623. * @vol_id: volume ID
  624. */
  625. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  626. {
  627. if (vol_id >= UBI_INTERNAL_VOL_START)
  628. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  629. else
  630. return vol_id;
  631. }
  632. /**
  633. * idx2vol_id - get volume ID by table index.
  634. * @ubi: UBI device description object
  635. * @idx: table index
  636. */
  637. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  638. {
  639. if (idx >= ubi->vtbl_slots)
  640. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  641. else
  642. return idx;
  643. }
  644. #endif /* !__UBI_UBI_H__ */