ubi.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/string.h>
  36. #include <linux/mtd/mtd.h>
  37. #include <mtd/ubi-header.h>
  38. #include <linux/mtd/ubi.h>
  39. #include "scan.h"
  40. #include "debug.h"
  41. /* Maximum number of supported UBI devices */
  42. #define UBI_MAX_DEVICES 32
  43. /* UBI name used for character devices, sysfs, etc */
  44. #define UBI_NAME_STR "ubi"
  45. /* Normal UBI messages */
  46. #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
  47. /* UBI warning messages */
  48. #define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \
  49. __FUNCTION__, ##__VA_ARGS__)
  50. /* UBI error messages */
  51. #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
  52. __FUNCTION__, ##__VA_ARGS__)
  53. /* Lowest number PEBs reserved for bad PEB handling */
  54. #define MIN_RESEVED_PEBS 2
  55. /* Background thread name pattern */
  56. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  57. /* This marker in the EBA table means that the LEB is um-mapped */
  58. #define UBI_LEB_UNMAPPED -1
  59. /*
  60. * In case of errors, UBI tries to repeat the operation several times before
  61. * returning error. The below constant defines how many times UBI re-tries.
  62. */
  63. #define UBI_IO_RETRIES 3
  64. /*
  65. * Error codes returned by the I/O unit.
  66. *
  67. * UBI_IO_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only
  68. * 0xFF bytes
  69. * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a
  70. * valid erase counter header, and the rest are %0xFF bytes
  71. * UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC)
  72. * UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or
  73. * CRC)
  74. * UBI_IO_BITFLIPS: bit-flips were detected and corrected
  75. */
  76. enum {
  77. UBI_IO_PEB_EMPTY = 1,
  78. UBI_IO_PEB_FREE,
  79. UBI_IO_BAD_EC_HDR,
  80. UBI_IO_BAD_VID_HDR,
  81. UBI_IO_BITFLIPS
  82. };
  83. extern int ubi_devices_cnt;
  84. extern struct ubi_device *ubi_devices[];
  85. struct ubi_volume_desc;
  86. /**
  87. * struct ubi_volume - UBI volume description data structure.
  88. * @dev: device object to make use of the the Linux device model
  89. * @cdev: character device object to create character device
  90. * @ubi: reference to the UBI device description object
  91. * @vol_id: volume ID
  92. * @readers: number of users holding this volume in read-only mode
  93. * @writers: number of users holding this volume in read-write mode
  94. * @exclusive: whether somebody holds this volume in exclusive mode
  95. * @removed: if the volume was removed
  96. * @checked: if this static volume was checked
  97. *
  98. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  99. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  100. * @usable_leb_size: logical eraseblock size without padding
  101. * @used_ebs: how many logical eraseblocks in this volume contain data
  102. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  103. * @used_bytes: how many bytes of data this volume contains
  104. * @upd_marker: non-zero if the update marker is set for this volume
  105. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  106. * @alignment: volume alignment
  107. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  108. * satisfy the requested alignment
  109. * @name_len: volume name length
  110. * @name: volume name
  111. *
  112. * @updating: whether the volume is being updated
  113. * @upd_ebs: how many eraseblocks are expected to be updated
  114. * @upd_bytes: how many bytes are expected to be received
  115. * @upd_received: how many update bytes were already received
  116. * @upd_buf: update buffer which is used to collect update data
  117. *
  118. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  119. *
  120. * @gluebi_desc: gluebi UBI volume descriptor
  121. * @gluebi_refcount: reference count of the gluebi MTD device
  122. * @gluebi_mtd: MTD device description object of the gluebi MTD device
  123. *
  124. * The @corrupted field indicates that the volume's contents is corrupted.
  125. * Since UBI protects only static volumes, this field is not relevant to
  126. * dynamic volumes - it is user's responsibility to assure their data
  127. * integrity.
  128. *
  129. * The @upd_marker flag indicates that this volume is either being updated at
  130. * the moment or is damaged because of an unclean reboot.
  131. */
  132. struct ubi_volume {
  133. struct device dev;
  134. struct cdev cdev;
  135. struct ubi_device *ubi;
  136. int vol_id;
  137. int readers;
  138. int writers;
  139. int exclusive;
  140. int removed;
  141. int checked;
  142. int reserved_pebs;
  143. int vol_type;
  144. int usable_leb_size;
  145. int used_ebs;
  146. int last_eb_bytes;
  147. long long used_bytes;
  148. int upd_marker;
  149. int corrupted;
  150. int alignment;
  151. int data_pad;
  152. int name_len;
  153. char name[UBI_VOL_NAME_MAX+1];
  154. int updating;
  155. int upd_ebs;
  156. long long upd_bytes;
  157. long long upd_received;
  158. void *upd_buf;
  159. int *eba_tbl;
  160. #ifdef CONFIG_MTD_UBI_GLUEBI
  161. /* Gluebi-related stuff may be compiled out */
  162. struct ubi_volume_desc *gluebi_desc;
  163. int gluebi_refcount;
  164. struct mtd_info gluebi_mtd;
  165. #endif
  166. };
  167. /**
  168. * struct ubi_volume_desc - descriptor of the UBI volume returned when it is
  169. * opened.
  170. * @vol: reference to the corresponding volume description object
  171. * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
  172. */
  173. struct ubi_volume_desc {
  174. struct ubi_volume *vol;
  175. int mode;
  176. };
  177. struct ubi_wl_entry;
  178. /**
  179. * struct ubi_device - UBI device description structure
  180. * @dev: class device object to use the the Linux device model
  181. * @cdev: character device object to create character device
  182. * @ubi_num: UBI device number
  183. * @ubi_name: UBI device name
  184. * @major: character device major number
  185. * @vol_count: number of volumes in this UBI device
  186. * @volumes: volumes of this UBI device
  187. * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
  188. * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count, @vol->readers,
  189. * @vol->writers, @vol->exclusive, @vol->removed, @vol->mapping and
  190. * @vol->eba_tbl.
  191. *
  192. * @rsvd_pebs: count of reserved physical eraseblocks
  193. * @avail_pebs: count of available physical eraseblocks
  194. * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
  195. * handling
  196. * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
  197. *
  198. * @vtbl_slots: how many slots are available in the volume table
  199. * @vtbl_size: size of the volume table in bytes
  200. * @vtbl: in-RAM volume table copy
  201. *
  202. * @max_ec: current highest erase counter value
  203. * @mean_ec: current mean erase counter value
  204. *
  205. * global_sqnum: global sequence number
  206. * @ltree_lock: protects the lock tree and @global_sqnum
  207. * @ltree: the lock tree
  208. * @vtbl_mutex: protects on-flash volume table
  209. *
  210. * @used: RB-tree of used physical eraseblocks
  211. * @free: RB-tree of free physical eraseblocks
  212. * @scrub: RB-tree of physical eraseblocks which need scrubbing
  213. * @prot: protection trees
  214. * @prot.pnum: protection tree indexed by physical eraseblock numbers
  215. * @prot.aec: protection tree indexed by absolute erase counter value
  216. * @wl_lock: protects the @used, @free, @prot, @lookuptbl, @abs_ec, @move_from,
  217. * @move_to, @move_to_put @erase_pending, @wl_scheduled, and @works
  218. * fields
  219. * @wl_scheduled: non-zero if the wear-leveling was scheduled
  220. * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
  221. * physical eraseblock
  222. * @abs_ec: absolute erase counter
  223. * @move_from: physical eraseblock from where the data is being moved
  224. * @move_to: physical eraseblock where the data is being moved to
  225. * @move_from_put: if the "from" PEB was put
  226. * @move_to_put: if the "to" PEB was put
  227. * @works: list of pending works
  228. * @works_count: count of pending works
  229. * @bgt_thread: background thread description object
  230. * @thread_enabled: if the background thread is enabled
  231. * @bgt_name: background thread name
  232. *
  233. * @flash_size: underlying MTD device size (in bytes)
  234. * @peb_count: count of physical eraseblocks on the MTD device
  235. * @peb_size: physical eraseblock size
  236. * @bad_peb_count: count of bad physical eraseblocks
  237. * @good_peb_count: count of good physical eraseblocks
  238. * @min_io_size: minimal input/output unit size of the underlying MTD device
  239. * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
  240. * @ro_mode: if the UBI device is in read-only mode
  241. * @leb_size: logical eraseblock size
  242. * @leb_start: starting offset of logical eraseblocks within physical
  243. * eraseblocks
  244. * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
  245. * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
  246. * @vid_hdr_offset: starting offset of the volume identifier header (might be
  247. * unaligned)
  248. * @vid_hdr_aloffset: starting offset of the VID header aligned to
  249. * @hdrs_min_io_size
  250. * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
  251. * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
  252. * not
  253. * @mtd: MTD device descriptor
  254. */
  255. struct ubi_device {
  256. struct cdev cdev;
  257. struct device dev;
  258. int ubi_num;
  259. char ubi_name[sizeof(UBI_NAME_STR)+5];
  260. int major;
  261. int vol_count;
  262. struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
  263. spinlock_t volumes_lock;
  264. int rsvd_pebs;
  265. int avail_pebs;
  266. int beb_rsvd_pebs;
  267. int beb_rsvd_level;
  268. int vtbl_slots;
  269. int vtbl_size;
  270. struct ubi_vtbl_record *vtbl;
  271. struct mutex vtbl_mutex;
  272. int max_ec;
  273. int mean_ec;
  274. /* EBA unit's stuff */
  275. unsigned long long global_sqnum;
  276. spinlock_t ltree_lock;
  277. struct rb_root ltree;
  278. /* Wear-leveling unit's stuff */
  279. struct rb_root used;
  280. struct rb_root free;
  281. struct rb_root scrub;
  282. struct {
  283. struct rb_root pnum;
  284. struct rb_root aec;
  285. } prot;
  286. spinlock_t wl_lock;
  287. int wl_scheduled;
  288. struct ubi_wl_entry **lookuptbl;
  289. unsigned long long abs_ec;
  290. struct ubi_wl_entry *move_from;
  291. struct ubi_wl_entry *move_to;
  292. int move_from_put;
  293. int move_to_put;
  294. struct list_head works;
  295. int works_count;
  296. struct task_struct *bgt_thread;
  297. int thread_enabled;
  298. char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
  299. /* I/O unit's stuff */
  300. long long flash_size;
  301. int peb_count;
  302. int peb_size;
  303. int bad_peb_count;
  304. int good_peb_count;
  305. int min_io_size;
  306. int hdrs_min_io_size;
  307. int ro_mode;
  308. int leb_size;
  309. int leb_start;
  310. int ec_hdr_alsize;
  311. int vid_hdr_alsize;
  312. int vid_hdr_offset;
  313. int vid_hdr_aloffset;
  314. int vid_hdr_shift;
  315. int bad_allowed;
  316. struct mtd_info *mtd;
  317. };
  318. extern struct file_operations ubi_cdev_operations;
  319. extern struct file_operations ubi_vol_cdev_operations;
  320. extern struct class *ubi_class;
  321. /* vtbl.c */
  322. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  323. struct ubi_vtbl_record *vtbl_rec);
  324. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
  325. /* vmt.c */
  326. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  327. int ubi_remove_volume(struct ubi_volume_desc *desc);
  328. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  329. int ubi_add_volume(struct ubi_device *ubi, int vol_id);
  330. void ubi_free_volume(struct ubi_device *ubi, int vol_id);
  331. /* upd.c */
  332. int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes);
  333. int ubi_more_update_data(struct ubi_device *ubi, int vol_id,
  334. const void __user *buf, int count);
  335. /* misc.c */
  336. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length);
  337. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  338. void ubi_calculate_reserved(struct ubi_device *ubi);
  339. /* gluebi.c */
  340. #ifdef CONFIG_MTD_UBI_GLUEBI
  341. int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
  342. int ubi_destroy_gluebi(struct ubi_volume *vol);
  343. #else
  344. #define ubi_create_gluebi(ubi, vol) 0
  345. #define ubi_destroy_gluebi(vol) 0
  346. #endif
  347. /* eba.c */
  348. int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum);
  349. int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,
  350. int offset, int len, int check);
  351. int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum,
  352. const void *buf, int offset, int len, int dtype);
  353. int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum,
  354. const void *buf, int len, int dtype,
  355. int used_ebs);
  356. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
  357. const void *buf, int len, int dtype);
  358. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  359. struct ubi_vid_hdr *vid_hdr);
  360. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  361. void ubi_eba_close(const struct ubi_device *ubi);
  362. /* wl.c */
  363. int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
  364. int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
  365. int ubi_wl_flush(struct ubi_device *ubi);
  366. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  367. int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  368. void ubi_wl_close(struct ubi_device *ubi);
  369. /* io.c */
  370. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  371. int len);
  372. int ubi_io_write(const struct ubi_device *ubi, const void *buf, int pnum,
  373. int offset, int len);
  374. int ubi_io_sync_erase(const struct ubi_device *ubi, int pnum, int torture);
  375. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  376. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  377. int ubi_io_read_ec_hdr(const struct ubi_device *ubi, int pnum,
  378. struct ubi_ec_hdr *ec_hdr, int verbose);
  379. int ubi_io_write_ec_hdr(const struct ubi_device *ubi, int pnum,
  380. struct ubi_ec_hdr *ec_hdr);
  381. int ubi_io_read_vid_hdr(const struct ubi_device *ubi, int pnum,
  382. struct ubi_vid_hdr *vid_hdr, int verbose);
  383. int ubi_io_write_vid_hdr(const struct ubi_device *ubi, int pnum,
  384. struct ubi_vid_hdr *vid_hdr);
  385. /*
  386. * ubi_rb_for_each_entry - walk an RB-tree.
  387. * @rb: a pointer to type 'struct rb_node' to to use as a loop counter
  388. * @pos: a pointer to RB-tree entry type to use as a loop counter
  389. * @root: RB-tree's root
  390. * @member: the name of the 'struct rb_node' within the RB-tree entry
  391. */
  392. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  393. for (rb = rb_first(root), \
  394. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  395. rb; \
  396. rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
  397. /**
  398. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  399. * @ubi: UBI device description object
  400. *
  401. * This function returns a pointer to the newly allocated and zero-filled
  402. * volume identifier header object in case of success and %NULL in case of
  403. * failure.
  404. */
  405. static inline struct ubi_vid_hdr *ubi_zalloc_vid_hdr(const struct ubi_device *ubi)
  406. {
  407. void *vid_hdr;
  408. vid_hdr = kzalloc(ubi->vid_hdr_alsize, GFP_KERNEL);
  409. if (!vid_hdr)
  410. return NULL;
  411. /*
  412. * VID headers may be stored at un-aligned flash offsets, so we shift
  413. * the pointer.
  414. */
  415. return vid_hdr + ubi->vid_hdr_shift;
  416. }
  417. /**
  418. * ubi_free_vid_hdr - free a volume identifier header object.
  419. * @ubi: UBI device description object
  420. * @vid_hdr: the object to free
  421. */
  422. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  423. struct ubi_vid_hdr *vid_hdr)
  424. {
  425. void *p = vid_hdr;
  426. if (!p)
  427. return;
  428. kfree(p - ubi->vid_hdr_shift);
  429. }
  430. /*
  431. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  432. * the beginning of the logical eraseblock, not to the beginning of the
  433. * physical eraseblock.
  434. */
  435. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  436. int pnum, int offset, int len)
  437. {
  438. ubi_assert(offset >= 0);
  439. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  440. }
  441. /*
  442. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  443. * the beginning of the logical eraseblock, not to the beginning of the
  444. * physical eraseblock.
  445. */
  446. static inline int ubi_io_write_data(const struct ubi_device *ubi, const void *buf,
  447. int pnum, int offset, int len)
  448. {
  449. ubi_assert(offset >= 0);
  450. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  451. }
  452. /**
  453. * ubi_ro_mode - switch to read-only mode.
  454. * @ubi: UBI device description object
  455. */
  456. static inline void ubi_ro_mode(struct ubi_device *ubi)
  457. {
  458. ubi->ro_mode = 1;
  459. ubi_warn("switch to read-only mode");
  460. }
  461. /**
  462. * vol_id2idx - get table index by volume ID.
  463. * @ubi: UBI device description object
  464. * @vol_id: volume ID
  465. */
  466. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  467. {
  468. if (vol_id >= UBI_INTERNAL_VOL_START)
  469. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  470. else
  471. return vol_id;
  472. }
  473. /**
  474. * idx2vol_id - get volume ID by table index.
  475. * @ubi: UBI device description object
  476. * @idx: table index
  477. */
  478. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  479. {
  480. if (idx >= ubi->vtbl_slots)
  481. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  482. else
  483. return idx;
  484. }
  485. #endif /* !__UBI_UBI_H__ */