ubi.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006, 2007
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Author: Artem Bityutskiy (Битюцкий Артём)
  8. */
  9. #ifndef __UBI_UBI_H__
  10. #define __UBI_UBI_H__
  11. #ifndef __UBOOT__
  12. #include <linux/types.h>
  13. #include <linux/list.h>
  14. #include <linux/rbtree.h>
  15. #include <linux/sched.h>
  16. #include <linux/wait.h>
  17. #include <linux/mutex.h>
  18. #include <linux/rwsem.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/fs.h>
  21. #include <linux/cdev.h>
  22. #include <linux/device.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <linux/vmalloc.h>
  26. #include <linux/notifier.h>
  27. #include <asm/pgtable.h>
  28. #else
  29. #include <ubi_uboot.h>
  30. #endif
  31. #include <linux/mtd/mtd.h>
  32. #include <linux/mtd/ubi.h>
  33. #include "ubi-media.h"
  34. #include <mtd/ubi-user.h>
  35. /* Maximum number of supported UBI devices */
  36. #define UBI_MAX_DEVICES 32
  37. /* UBI name used for character devices, sysfs, etc */
  38. #define UBI_NAME_STR "ubi"
  39. /* Normal UBI messages */
  40. #ifdef CONFIG_UBI_SILENCE_MSG
  41. #define ubi_msg(fmt, ...)
  42. #else
  43. #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
  44. #endif
  45. /* UBI warning messages */
  46. #define ubi_warn(fmt, ...) pr_warn("UBI warning: %s: " fmt "\n", \
  47. __func__, ##__VA_ARGS__)
  48. /* UBI error messages */
  49. #define ubi_err(fmt, ...) pr_err("UBI error: %s: " fmt "\n", \
  50. __func__, ##__VA_ARGS__)
  51. /* Background thread name pattern */
  52. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  53. /*
  54. * This marker in the EBA table means that the LEB is um-mapped.
  55. * NOTE! It has to have the same value as %UBI_ALL.
  56. */
  57. #define UBI_LEB_UNMAPPED -1
  58. /*
  59. * In case of errors, UBI tries to repeat the operation several times before
  60. * returning error. The below constant defines how many times UBI re-tries.
  61. */
  62. #define UBI_IO_RETRIES 3
  63. /*
  64. * Length of the protection queue. The length is effectively equivalent to the
  65. * number of (global) erase cycles PEBs are protected from the wear-leveling
  66. * worker.
  67. */
  68. #define UBI_PROT_QUEUE_LEN 10
  69. /* The volume ID/LEB number/erase counter is unknown */
  70. #define UBI_UNKNOWN -1
  71. /*
  72. * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
  73. * + 2 for the number plus 1 for the trailing zero byte.
  74. */
  75. #define UBI_DFS_DIR_NAME "ubi%d"
  76. #define UBI_DFS_DIR_LEN (3 + 2 + 1)
  77. /*
  78. * Error codes returned by the I/O sub-system.
  79. *
  80. * UBI_IO_FF: the read region of flash contains only 0xFFs
  81. * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
  82. * integrity error reported by the MTD driver
  83. * (uncorrectable ECC error in case of NAND)
  84. * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
  85. * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
  86. * data integrity error reported by the MTD driver
  87. * (uncorrectable ECC error in case of NAND)
  88. * UBI_IO_BITFLIPS: bit-flips were detected and corrected
  89. *
  90. * Note, it is probably better to have bit-flip and ebadmsg as flags which can
  91. * be or'ed with other error code. But this is a big change because there are
  92. * may callers, so it does not worth the risk of introducing a bug
  93. */
  94. enum {
  95. UBI_IO_FF = 1,
  96. UBI_IO_FF_BITFLIPS,
  97. UBI_IO_BAD_HDR,
  98. UBI_IO_BAD_HDR_EBADMSG,
  99. UBI_IO_BITFLIPS,
  100. };
  101. /*
  102. * Return codes of the 'ubi_eba_copy_leb()' function.
  103. *
  104. * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
  105. * PEB was put meanwhile, or there is I/O on the source PEB
  106. * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
  107. * PEB
  108. * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
  109. * PEB
  110. * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
  111. * PEB
  112. * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
  113. * target PEB
  114. * MOVE_RETRY: retry scrubbing the PEB
  115. */
  116. enum {
  117. MOVE_CANCEL_RACE = 1,
  118. MOVE_SOURCE_RD_ERR,
  119. MOVE_TARGET_RD_ERR,
  120. MOVE_TARGET_WR_ERR,
  121. MOVE_TARGET_BITFLIPS,
  122. MOVE_RETRY,
  123. };
  124. /*
  125. * Return codes of the fastmap sub-system
  126. *
  127. * UBI_NO_FASTMAP: No fastmap super block was found
  128. * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
  129. */
  130. enum {
  131. UBI_NO_FASTMAP = 1,
  132. UBI_BAD_FASTMAP,
  133. };
  134. /**
  135. * struct ubi_wl_entry - wear-leveling entry.
  136. * @u.rb: link in the corresponding (free/used) RB-tree
  137. * @u.list: link in the protection queue
  138. * @ec: erase counter
  139. * @pnum: physical eraseblock number
  140. *
  141. * This data structure is used in the WL sub-system. Each physical eraseblock
  142. * has a corresponding &struct wl_entry object which may be kept in different
  143. * RB-trees. See WL sub-system for details.
  144. */
  145. struct ubi_wl_entry {
  146. union {
  147. struct rb_node rb;
  148. struct list_head list;
  149. } u;
  150. int ec;
  151. int pnum;
  152. };
  153. /**
  154. * struct ubi_ltree_entry - an entry in the lock tree.
  155. * @rb: links RB-tree nodes
  156. * @vol_id: volume ID of the locked logical eraseblock
  157. * @lnum: locked logical eraseblock number
  158. * @users: how many tasks are using this logical eraseblock or wait for it
  159. * @mutex: read/write mutex to implement read/write access serialization to
  160. * the (@vol_id, @lnum) logical eraseblock
  161. *
  162. * This data structure is used in the EBA sub-system to implement per-LEB
  163. * locking. When a logical eraseblock is being locked - corresponding
  164. * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
  165. * See EBA sub-system for details.
  166. */
  167. struct ubi_ltree_entry {
  168. struct rb_node rb;
  169. int vol_id;
  170. int lnum;
  171. int users;
  172. struct rw_semaphore mutex;
  173. };
  174. /**
  175. * struct ubi_rename_entry - volume re-name description data structure.
  176. * @new_name_len: new volume name length
  177. * @new_name: new volume name
  178. * @remove: if not zero, this volume should be removed, not re-named
  179. * @desc: descriptor of the volume
  180. * @list: links re-name entries into a list
  181. *
  182. * This data structure is utilized in the multiple volume re-name code. Namely,
  183. * UBI first creates a list of &struct ubi_rename_entry objects from the
  184. * &struct ubi_rnvol_req request object, and then utilizes this list to do all
  185. * the job.
  186. */
  187. struct ubi_rename_entry {
  188. int new_name_len;
  189. char new_name[UBI_VOL_NAME_MAX + 1];
  190. int remove;
  191. struct ubi_volume_desc *desc;
  192. struct list_head list;
  193. };
  194. struct ubi_volume_desc;
  195. /**
  196. * struct ubi_fastmap_layout - in-memory fastmap data structure.
  197. * @e: PEBs used by the current fastmap
  198. * @to_be_tortured: if non-zero tortured this PEB
  199. * @used_blocks: number of used PEBs
  200. * @max_pool_size: maximal size of the user pool
  201. * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
  202. */
  203. struct ubi_fastmap_layout {
  204. struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
  205. int to_be_tortured[UBI_FM_MAX_BLOCKS];
  206. int used_blocks;
  207. int max_pool_size;
  208. int max_wl_pool_size;
  209. };
  210. /**
  211. * struct ubi_fm_pool - in-memory fastmap pool
  212. * @pebs: PEBs in this pool
  213. * @used: number of used PEBs
  214. * @size: total number of PEBs in this pool
  215. * @max_size: maximal size of the pool
  216. *
  217. * A pool gets filled with up to max_size.
  218. * If all PEBs within the pool are used a new fastmap will be written
  219. * to the flash and the pool gets refilled with empty PEBs.
  220. *
  221. */
  222. struct ubi_fm_pool {
  223. int pebs[UBI_FM_MAX_POOL_SIZE];
  224. int used;
  225. int size;
  226. int max_size;
  227. };
  228. /**
  229. * struct ubi_volume - UBI volume description data structure.
  230. * @dev: device object to make use of the the Linux device model
  231. * @cdev: character device object to create character device
  232. * @ubi: reference to the UBI device description object
  233. * @vol_id: volume ID
  234. * @ref_count: volume reference count
  235. * @readers: number of users holding this volume in read-only mode
  236. * @writers: number of users holding this volume in read-write mode
  237. * @exclusive: whether somebody holds this volume in exclusive mode
  238. *
  239. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  240. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  241. * @usable_leb_size: logical eraseblock size without padding
  242. * @used_ebs: how many logical eraseblocks in this volume contain data
  243. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  244. * @used_bytes: how many bytes of data this volume contains
  245. * @alignment: volume alignment
  246. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  247. * satisfy the requested alignment
  248. * @name_len: volume name length
  249. * @name: volume name
  250. *
  251. * @upd_ebs: how many eraseblocks are expected to be updated
  252. * @ch_lnum: LEB number which is being changing by the atomic LEB change
  253. * operation
  254. * @upd_bytes: how many bytes are expected to be received for volume update or
  255. * atomic LEB change
  256. * @upd_received: how many bytes were already received for volume update or
  257. * atomic LEB change
  258. * @upd_buf: update buffer which is used to collect update data or data for
  259. * atomic LEB change
  260. *
  261. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  262. * @checked: %1 if this static volume was checked
  263. * @corrupted: %1 if the volume is corrupted (static volumes only)
  264. * @upd_marker: %1 if the update marker is set for this volume
  265. * @updating: %1 if the volume is being updated
  266. * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
  267. * @direct_writes: %1 if direct writes are enabled for this volume
  268. *
  269. * The @corrupted field indicates that the volume's contents is corrupted.
  270. * Since UBI protects only static volumes, this field is not relevant to
  271. * dynamic volumes - it is user's responsibility to assure their data
  272. * integrity.
  273. *
  274. * The @upd_marker flag indicates that this volume is either being updated at
  275. * the moment or is damaged because of an unclean reboot.
  276. */
  277. struct ubi_volume {
  278. struct device dev;
  279. struct cdev cdev;
  280. struct ubi_device *ubi;
  281. int vol_id;
  282. int ref_count;
  283. int readers;
  284. int writers;
  285. int exclusive;
  286. int reserved_pebs;
  287. int vol_type;
  288. int usable_leb_size;
  289. int used_ebs;
  290. #ifndef __UBOOT__
  291. int last_eb_bytes;
  292. #else
  293. u32 last_eb_bytes;
  294. #endif
  295. long long used_bytes;
  296. int alignment;
  297. int data_pad;
  298. int name_len;
  299. char name[UBI_VOL_NAME_MAX + 1];
  300. int upd_ebs;
  301. int ch_lnum;
  302. long long upd_bytes;
  303. long long upd_received;
  304. void *upd_buf;
  305. int *eba_tbl;
  306. unsigned int checked:1;
  307. unsigned int corrupted:1;
  308. unsigned int upd_marker:1;
  309. unsigned int updating:1;
  310. unsigned int changing_leb:1;
  311. unsigned int direct_writes:1;
  312. };
  313. /**
  314. * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
  315. * @vol: reference to the corresponding volume description object
  316. * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
  317. */
  318. struct ubi_volume_desc {
  319. struct ubi_volume *vol;
  320. int mode;
  321. };
  322. struct ubi_wl_entry;
  323. /**
  324. * struct ubi_debug_info - debugging information for an UBI device.
  325. *
  326. * @chk_gen: if UBI general extra checks are enabled
  327. * @chk_io: if UBI I/O extra checks are enabled
  328. * @disable_bgt: disable the background task for testing purposes
  329. * @emulate_bitflips: emulate bit-flips for testing purposes
  330. * @emulate_io_failures: emulate write/erase failures for testing purposes
  331. * @dfs_dir_name: name of debugfs directory containing files of this UBI device
  332. * @dfs_dir: direntry object of the UBI device debugfs directory
  333. * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
  334. * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
  335. * @dfs_disable_bgt: debugfs knob to disable the background task
  336. * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
  337. * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
  338. */
  339. struct ubi_debug_info {
  340. unsigned int chk_gen:1;
  341. unsigned int chk_io:1;
  342. unsigned int disable_bgt:1;
  343. unsigned int emulate_bitflips:1;
  344. unsigned int emulate_io_failures:1;
  345. char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
  346. struct dentry *dfs_dir;
  347. struct dentry *dfs_chk_gen;
  348. struct dentry *dfs_chk_io;
  349. struct dentry *dfs_disable_bgt;
  350. struct dentry *dfs_emulate_bitflips;
  351. struct dentry *dfs_emulate_io_failures;
  352. };
  353. /**
  354. * struct ubi_device - UBI device description structure
  355. * @dev: UBI device object to use the the Linux device model
  356. * @cdev: character device object to create character device
  357. * @ubi_num: UBI device number
  358. * @ubi_name: UBI device name
  359. * @vol_count: number of volumes in this UBI device
  360. * @volumes: volumes of this UBI device
  361. * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
  362. * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
  363. * @vol->readers, @vol->writers, @vol->exclusive,
  364. * @vol->ref_count, @vol->mapping and @vol->eba_tbl.
  365. * @ref_count: count of references on the UBI device
  366. * @image_seq: image sequence number recorded on EC headers
  367. *
  368. * @rsvd_pebs: count of reserved physical eraseblocks
  369. * @avail_pebs: count of available physical eraseblocks
  370. * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
  371. * handling
  372. * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
  373. *
  374. * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
  375. * of UBI initialization
  376. * @vtbl_slots: how many slots are available in the volume table
  377. * @vtbl_size: size of the volume table in bytes
  378. * @vtbl: in-RAM volume table copy
  379. * @device_mutex: protects on-flash volume table and serializes volume
  380. * creation, deletion, update, re-size, re-name and set
  381. * property
  382. *
  383. * @max_ec: current highest erase counter value
  384. * @mean_ec: current mean erase counter value
  385. *
  386. * @global_sqnum: global sequence number
  387. * @ltree_lock: protects the lock tree and @global_sqnum
  388. * @ltree: the lock tree
  389. * @alc_mutex: serializes "atomic LEB change" operations
  390. *
  391. * @fm_disabled: non-zero if fastmap is disabled (default)
  392. * @fm: in-memory data structure of the currently used fastmap
  393. * @fm_pool: in-memory data structure of the fastmap pool
  394. * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
  395. * sub-system
  396. * @fm_mutex: serializes ubi_update_fastmap() and protects @fm_buf
  397. * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
  398. * @fm_size: fastmap size in bytes
  399. * @fm_sem: allows ubi_update_fastmap() to block EBA table changes
  400. * @fm_work: fastmap work queue
  401. *
  402. * @used: RB-tree of used physical eraseblocks
  403. * @erroneous: RB-tree of erroneous used physical eraseblocks
  404. * @free: RB-tree of free physical eraseblocks
  405. * @free_count: Contains the number of elements in @free
  406. * @scrub: RB-tree of physical eraseblocks which need scrubbing
  407. * @pq: protection queue (contain physical eraseblocks which are temporarily
  408. * protected from the wear-leveling worker)
  409. * @pq_head: protection queue head
  410. * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
  411. * @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
  412. * @erroneous, and @erroneous_peb_count fields
  413. * @move_mutex: serializes eraseblock moves
  414. * @work_sem: synchronizes the WL worker with use tasks
  415. * @wl_scheduled: non-zero if the wear-leveling was scheduled
  416. * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
  417. * physical eraseblock
  418. * @move_from: physical eraseblock from where the data is being moved
  419. * @move_to: physical eraseblock where the data is being moved to
  420. * @move_to_put: if the "to" PEB was put
  421. * @works: list of pending works
  422. * @works_count: count of pending works
  423. * @bgt_thread: background thread description object
  424. * @thread_enabled: if the background thread is enabled
  425. * @bgt_name: background thread name
  426. *
  427. * @flash_size: underlying MTD device size (in bytes)
  428. * @peb_count: count of physical eraseblocks on the MTD device
  429. * @peb_size: physical eraseblock size
  430. * @bad_peb_limit: top limit of expected bad physical eraseblocks
  431. * @bad_peb_count: count of bad physical eraseblocks
  432. * @good_peb_count: count of good physical eraseblocks
  433. * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
  434. * used by UBI)
  435. * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
  436. * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
  437. * @min_io_size: minimal input/output unit size of the underlying MTD device
  438. * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
  439. * @ro_mode: if the UBI device is in read-only mode
  440. * @leb_size: logical eraseblock size
  441. * @leb_start: starting offset of logical eraseblocks within physical
  442. * eraseblocks
  443. * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
  444. * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
  445. * @vid_hdr_offset: starting offset of the volume identifier header (might be
  446. * unaligned)
  447. * @vid_hdr_aloffset: starting offset of the VID header aligned to
  448. * @hdrs_min_io_size
  449. * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
  450. * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
  451. * not
  452. * @nor_flash: non-zero if working on top of NOR flash
  453. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  454. * time (MTD write buffer size)
  455. * @mtd: MTD device descriptor
  456. *
  457. * @peb_buf: a buffer of PEB size used for different purposes
  458. * @buf_mutex: protects @peb_buf
  459. * @ckvol_mutex: serializes static volume checking when opening
  460. *
  461. * @dbg: debugging information for this UBI device
  462. */
  463. struct ubi_device {
  464. struct cdev cdev;
  465. struct device dev;
  466. int ubi_num;
  467. char ubi_name[sizeof(UBI_NAME_STR)+5];
  468. int vol_count;
  469. struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
  470. spinlock_t volumes_lock;
  471. int ref_count;
  472. int image_seq;
  473. int rsvd_pebs;
  474. int avail_pebs;
  475. int beb_rsvd_pebs;
  476. int beb_rsvd_level;
  477. int bad_peb_limit;
  478. int autoresize_vol_id;
  479. int vtbl_slots;
  480. int vtbl_size;
  481. struct ubi_vtbl_record *vtbl;
  482. struct mutex device_mutex;
  483. int max_ec;
  484. /* Note, mean_ec is not updated run-time - should be fixed */
  485. int mean_ec;
  486. /* EBA sub-system's stuff */
  487. unsigned long long global_sqnum;
  488. spinlock_t ltree_lock;
  489. struct rb_root ltree;
  490. struct mutex alc_mutex;
  491. /* Fastmap stuff */
  492. int fm_disabled;
  493. struct ubi_fastmap_layout *fm;
  494. struct ubi_fm_pool fm_pool;
  495. struct ubi_fm_pool fm_wl_pool;
  496. struct rw_semaphore fm_sem;
  497. struct mutex fm_mutex;
  498. void *fm_buf;
  499. size_t fm_size;
  500. #ifndef __UBOOT__
  501. struct work_struct fm_work;
  502. #endif
  503. /* Wear-leveling sub-system's stuff */
  504. struct rb_root used;
  505. struct rb_root erroneous;
  506. struct rb_root free;
  507. int free_count;
  508. struct rb_root scrub;
  509. struct list_head pq[UBI_PROT_QUEUE_LEN];
  510. int pq_head;
  511. spinlock_t wl_lock;
  512. struct mutex move_mutex;
  513. struct rw_semaphore work_sem;
  514. int wl_scheduled;
  515. struct ubi_wl_entry **lookuptbl;
  516. struct ubi_wl_entry *move_from;
  517. struct ubi_wl_entry *move_to;
  518. int move_to_put;
  519. struct list_head works;
  520. int works_count;
  521. struct task_struct *bgt_thread;
  522. int thread_enabled;
  523. char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
  524. /* I/O sub-system's stuff */
  525. long long flash_size;
  526. int peb_count;
  527. int peb_size;
  528. int bad_peb_count;
  529. int good_peb_count;
  530. int corr_peb_count;
  531. int erroneous_peb_count;
  532. int max_erroneous;
  533. int min_io_size;
  534. int hdrs_min_io_size;
  535. int ro_mode;
  536. int leb_size;
  537. int leb_start;
  538. int ec_hdr_alsize;
  539. int vid_hdr_alsize;
  540. int vid_hdr_offset;
  541. int vid_hdr_aloffset;
  542. int vid_hdr_shift;
  543. unsigned int bad_allowed:1;
  544. unsigned int nor_flash:1;
  545. int max_write_size;
  546. struct mtd_info *mtd;
  547. void *peb_buf;
  548. struct mutex buf_mutex;
  549. struct mutex ckvol_mutex;
  550. struct ubi_debug_info dbg;
  551. };
  552. /**
  553. * struct ubi_ainf_peb - attach information about a physical eraseblock.
  554. * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
  555. * @pnum: physical eraseblock number
  556. * @vol_id: ID of the volume this LEB belongs to
  557. * @lnum: logical eraseblock number
  558. * @scrub: if this physical eraseblock needs scrubbing
  559. * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
  560. * @sqnum: sequence number
  561. * @u: unions RB-tree or @list links
  562. * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
  563. * @u.list: link in one of the eraseblock lists
  564. *
  565. * One object of this type is allocated for each physical eraseblock when
  566. * attaching an MTD device. Note, if this PEB does not belong to any LEB /
  567. * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
  568. */
  569. struct ubi_ainf_peb {
  570. int ec;
  571. int pnum;
  572. int vol_id;
  573. int lnum;
  574. unsigned int scrub:1;
  575. unsigned int copy_flag:1;
  576. unsigned long long sqnum;
  577. union {
  578. struct rb_node rb;
  579. struct list_head list;
  580. } u;
  581. };
  582. /**
  583. * struct ubi_ainf_volume - attaching information about a volume.
  584. * @vol_id: volume ID
  585. * @highest_lnum: highest logical eraseblock number in this volume
  586. * @leb_count: number of logical eraseblocks in this volume
  587. * @vol_type: volume type
  588. * @used_ebs: number of used logical eraseblocks in this volume (only for
  589. * static volumes)
  590. * @last_data_size: amount of data in the last logical eraseblock of this
  591. * volume (always equivalent to the usable logical eraseblock
  592. * size in case of dynamic volumes)
  593. * @data_pad: how many bytes at the end of logical eraseblocks of this volume
  594. * are not used (due to volume alignment)
  595. * @compat: compatibility flags of this volume
  596. * @rb: link in the volume RB-tree
  597. * @root: root of the RB-tree containing all the eraseblock belonging to this
  598. * volume (&struct ubi_ainf_peb objects)
  599. *
  600. * One object of this type is allocated for each volume when attaching an MTD
  601. * device.
  602. */
  603. struct ubi_ainf_volume {
  604. int vol_id;
  605. int highest_lnum;
  606. int leb_count;
  607. int vol_type;
  608. int used_ebs;
  609. int last_data_size;
  610. int data_pad;
  611. int compat;
  612. struct rb_node rb;
  613. struct rb_root root;
  614. };
  615. /**
  616. * struct ubi_attach_info - MTD device attaching information.
  617. * @volumes: root of the volume RB-tree
  618. * @corr: list of corrupted physical eraseblocks
  619. * @free: list of free physical eraseblocks
  620. * @erase: list of physical eraseblocks which have to be erased
  621. * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
  622. * those belonging to "preserve"-compatible internal volumes)
  623. * @corr_peb_count: count of PEBs in the @corr list
  624. * @empty_peb_count: count of PEBs which are presumably empty (contain only
  625. * 0xFF bytes)
  626. * @alien_peb_count: count of PEBs in the @alien list
  627. * @bad_peb_count: count of bad physical eraseblocks
  628. * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
  629. * as bad yet, but which look like bad
  630. * @vols_found: number of volumes found
  631. * @highest_vol_id: highest volume ID
  632. * @is_empty: flag indicating whether the MTD device is empty or not
  633. * @min_ec: lowest erase counter value
  634. * @max_ec: highest erase counter value
  635. * @max_sqnum: highest sequence number value
  636. * @mean_ec: mean erase counter value
  637. * @ec_sum: a temporary variable used when calculating @mean_ec
  638. * @ec_count: a temporary variable used when calculating @mean_ec
  639. * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
  640. *
  641. * This data structure contains the result of attaching an MTD device and may
  642. * be used by other UBI sub-systems to build final UBI data structures, further
  643. * error-recovery and so on.
  644. */
  645. struct ubi_attach_info {
  646. struct rb_root volumes;
  647. struct list_head corr;
  648. struct list_head free;
  649. struct list_head erase;
  650. struct list_head alien;
  651. int corr_peb_count;
  652. int empty_peb_count;
  653. int alien_peb_count;
  654. int bad_peb_count;
  655. int maybe_bad_peb_count;
  656. int vols_found;
  657. int highest_vol_id;
  658. int is_empty;
  659. int min_ec;
  660. int max_ec;
  661. unsigned long long max_sqnum;
  662. int mean_ec;
  663. uint64_t ec_sum;
  664. int ec_count;
  665. struct kmem_cache *aeb_slab_cache;
  666. };
  667. /**
  668. * struct ubi_work - UBI work description data structure.
  669. * @list: a link in the list of pending works
  670. * @func: worker function
  671. * @e: physical eraseblock to erase
  672. * @vol_id: the volume ID on which this erasure is being performed
  673. * @lnum: the logical eraseblock number
  674. * @torture: if the physical eraseblock has to be tortured
  675. * @anchor: produce a anchor PEB to by used by fastmap
  676. *
  677. * The @func pointer points to the worker function. If the @cancel argument is
  678. * not zero, the worker has to free the resources and exit immediately. The
  679. * worker has to return zero in case of success and a negative error code in
  680. * case of failure.
  681. */
  682. struct ubi_work {
  683. struct list_head list;
  684. int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
  685. /* The below fields are only relevant to erasure works */
  686. struct ubi_wl_entry *e;
  687. int vol_id;
  688. int lnum;
  689. int torture;
  690. int anchor;
  691. };
  692. #include "debug.h"
  693. extern struct kmem_cache *ubi_wl_entry_slab;
  694. extern const struct file_operations ubi_ctrl_cdev_operations;
  695. extern const struct file_operations ubi_cdev_operations;
  696. extern const struct file_operations ubi_vol_cdev_operations;
  697. extern struct class *ubi_class;
  698. extern struct mutex ubi_devices_mutex;
  699. extern struct blocking_notifier_head ubi_notifiers;
  700. /* attach.c */
  701. int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
  702. int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
  703. struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
  704. int vol_id);
  705. void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
  706. struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
  707. struct ubi_attach_info *ai);
  708. int ubi_attach(struct ubi_device *ubi, int force_scan);
  709. void ubi_destroy_ai(struct ubi_attach_info *ai);
  710. /* vtbl.c */
  711. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  712. struct ubi_vtbl_record *vtbl_rec);
  713. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  714. struct list_head *rename_list);
  715. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
  716. /* vmt.c */
  717. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  718. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
  719. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  720. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
  721. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  722. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  723. /* upd.c */
  724. int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
  725. long long bytes);
  726. int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
  727. const void __user *buf, int count);
  728. int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  729. const struct ubi_leb_change_req *req);
  730. int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
  731. const void __user *buf, int count);
  732. /* misc.c */
  733. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  734. int length);
  735. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  736. void ubi_update_reserved(struct ubi_device *ubi);
  737. void ubi_calculate_reserved(struct ubi_device *ubi);
  738. int ubi_check_pattern(const void *buf, uint8_t patt, int size);
  739. /* gluebi.c */
  740. #ifdef CONFIG_MTD_UBI_GLUEBI
  741. int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
  742. int ubi_destroy_gluebi(struct ubi_volume *vol);
  743. void ubi_gluebi_updated(struct ubi_volume *vol);
  744. #else
  745. #define ubi_create_gluebi(ubi, vol) 0
  746. static inline int ubi_destroy_gluebi(struct ubi_volume *vol)
  747. {
  748. return 0;
  749. }
  750. #define ubi_gluebi_updated(vol)
  751. #endif
  752. /* eba.c */
  753. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  754. int lnum);
  755. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  756. void *buf, int offset, int len, int check);
  757. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  758. const void *buf, int offset, int len);
  759. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  760. int lnum, const void *buf, int len, int used_ebs);
  761. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  762. int lnum, const void *buf, int len);
  763. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  764. struct ubi_vid_hdr *vid_hdr);
  765. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  766. unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
  767. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  768. struct ubi_attach_info *ai_scan);
  769. /* wl.c */
  770. int ubi_wl_get_peb(struct ubi_device *ubi);
  771. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  772. int pnum, int torture);
  773. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
  774. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  775. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  776. void ubi_wl_close(struct ubi_device *ubi);
  777. int ubi_thread(void *u);
  778. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
  779. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
  780. int lnum, int torture);
  781. int ubi_is_erase_work(struct ubi_work *wrk);
  782. void ubi_refill_pools(struct ubi_device *ubi);
  783. int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
  784. /* io.c */
  785. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  786. int len);
  787. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  788. int len);
  789. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
  790. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  791. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  792. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  793. struct ubi_ec_hdr *ec_hdr, int verbose);
  794. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  795. struct ubi_ec_hdr *ec_hdr);
  796. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  797. struct ubi_vid_hdr *vid_hdr, int verbose);
  798. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  799. struct ubi_vid_hdr *vid_hdr);
  800. /* build.c */
  801. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  802. int vid_hdr_offset, int max_beb_per1024);
  803. int ubi_detach_mtd_dev(int ubi_num, int anyway);
  804. struct ubi_device *ubi_get_device(int ubi_num);
  805. void ubi_put_device(struct ubi_device *ubi);
  806. struct ubi_device *ubi_get_by_major(int major);
  807. int ubi_major2num(int major);
  808. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
  809. int ntype);
  810. int ubi_notify_all(struct ubi_device *ubi, int ntype,
  811. struct notifier_block *nb);
  812. int ubi_enumerate_volumes(struct notifier_block *nb);
  813. void ubi_free_internal_volumes(struct ubi_device *ubi);
  814. /* kapi.c */
  815. void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
  816. void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
  817. struct ubi_volume_info *vi);
  818. /* scan.c */
  819. int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
  820. int pnum, const struct ubi_vid_hdr *vid_hdr);
  821. /* fastmap.c */
  822. size_t ubi_calc_fm_size(struct ubi_device *ubi);
  823. int ubi_update_fastmap(struct ubi_device *ubi);
  824. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  825. int fm_anchor);
  826. /* block.c */
  827. #ifdef CONFIG_MTD_UBI_BLOCK
  828. int ubiblock_init(void);
  829. void ubiblock_exit(void);
  830. int ubiblock_create(struct ubi_volume_info *vi);
  831. int ubiblock_remove(struct ubi_volume_info *vi);
  832. #else
  833. static inline int ubiblock_init(void) { return 0; }
  834. static inline void ubiblock_exit(void) {}
  835. static inline int ubiblock_create(struct ubi_volume_info *vi)
  836. {
  837. return -ENOSYS;
  838. }
  839. static inline int ubiblock_remove(struct ubi_volume_info *vi)
  840. {
  841. return -ENOSYS;
  842. }
  843. #endif
  844. /*
  845. * ubi_rb_for_each_entry - walk an RB-tree.
  846. * @rb: a pointer to type 'struct rb_node' to use as a loop counter
  847. * @pos: a pointer to RB-tree entry type to use as a loop counter
  848. * @root: RB-tree's root
  849. * @member: the name of the 'struct rb_node' within the RB-tree entry
  850. */
  851. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  852. for (rb = rb_first(root), \
  853. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  854. rb; \
  855. rb = rb_next(rb), \
  856. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
  857. /*
  858. * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
  859. *
  860. * @av: volume attaching information
  861. * @aeb: attaching eraseblock information
  862. * @list: the list to move to
  863. */
  864. static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
  865. struct ubi_ainf_peb *aeb,
  866. struct list_head *list)
  867. {
  868. rb_erase(&aeb->u.rb, &av->root);
  869. list_add_tail(&aeb->u.list, list);
  870. }
  871. /**
  872. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  873. * @ubi: UBI device description object
  874. * @gfp_flags: GFP flags to allocate with
  875. *
  876. * This function returns a pointer to the newly allocated and zero-filled
  877. * volume identifier header object in case of success and %NULL in case of
  878. * failure.
  879. */
  880. static inline struct ubi_vid_hdr *
  881. ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
  882. {
  883. void *vid_hdr;
  884. vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
  885. if (!vid_hdr)
  886. return NULL;
  887. /*
  888. * VID headers may be stored at un-aligned flash offsets, so we shift
  889. * the pointer.
  890. */
  891. return vid_hdr + ubi->vid_hdr_shift;
  892. }
  893. /**
  894. * ubi_free_vid_hdr - free a volume identifier header object.
  895. * @ubi: UBI device description object
  896. * @vid_hdr: the object to free
  897. */
  898. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  899. struct ubi_vid_hdr *vid_hdr)
  900. {
  901. void *p = vid_hdr;
  902. if (!p)
  903. return;
  904. kfree(p - ubi->vid_hdr_shift);
  905. }
  906. /*
  907. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  908. * the beginning of the logical eraseblock, not to the beginning of the
  909. * physical eraseblock.
  910. */
  911. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  912. int pnum, int offset, int len)
  913. {
  914. ubi_assert(offset >= 0);
  915. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  916. }
  917. /*
  918. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  919. * the beginning of the logical eraseblock, not to the beginning of the
  920. * physical eraseblock.
  921. */
  922. static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
  923. int pnum, int offset, int len)
  924. {
  925. ubi_assert(offset >= 0);
  926. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  927. }
  928. /**
  929. * ubi_ro_mode - switch to read-only mode.
  930. * @ubi: UBI device description object
  931. */
  932. static inline void ubi_ro_mode(struct ubi_device *ubi)
  933. {
  934. if (!ubi->ro_mode) {
  935. ubi->ro_mode = 1;
  936. ubi_warn("switch to read-only mode");
  937. dump_stack();
  938. }
  939. }
  940. /**
  941. * vol_id2idx - get table index by volume ID.
  942. * @ubi: UBI device description object
  943. * @vol_id: volume ID
  944. */
  945. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  946. {
  947. if (vol_id >= UBI_INTERNAL_VOL_START)
  948. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  949. else
  950. return vol_id;
  951. }
  952. /**
  953. * idx2vol_id - get volume ID by table index.
  954. * @ubi: UBI device description object
  955. * @idx: table index
  956. */
  957. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  958. {
  959. if (idx >= ubi->vtbl_slots)
  960. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  961. else
  962. return idx;
  963. }
  964. #endif /* !__UBI_UBI_H__ */