ubi.h 35 KB

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