ubi.h 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2006, 2007
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём)
  20. */
  21. #ifndef __UBI_UBI_H__
  22. #define __UBI_UBI_H__
  23. #include <linux/init.h>
  24. #include <linux/types.h>
  25. #include <linux/list.h>
  26. #include <linux/rbtree.h>
  27. #include <linux/sched.h>
  28. #include <linux/wait.h>
  29. #include <linux/mutex.h>
  30. #include <linux/rwsem.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/fs.h>
  33. #include <linux/cdev.h>
  34. #include <linux/device.h>
  35. #include <linux/slab.h>
  36. #include <linux/string.h>
  37. #include <linux/vmalloc.h>
  38. #include <linux/notifier.h>
  39. #include <linux/mtd/mtd.h>
  40. #include <linux/mtd/ubi.h>
  41. #include <asm/pgtable.h>
  42. #include "ubi-media.h"
  43. /* Maximum number of supported UBI devices */
  44. #define UBI_MAX_DEVICES 32
  45. /* UBI name used for character devices, sysfs, etc */
  46. #define UBI_NAME_STR "ubi"
  47. /* Normal UBI messages */
  48. #define ubi_msg(fmt, ...) pr_notice("UBI: " fmt "\n", ##__VA_ARGS__)
  49. /* UBI warning messages */
  50. #define ubi_warn(fmt, ...) pr_warn("UBI warning: %s: " fmt "\n", \
  51. __func__, ##__VA_ARGS__)
  52. /* UBI error messages */
  53. #define ubi_err(fmt, ...) pr_err("UBI error: %s: " fmt "\n", \
  54. __func__, ##__VA_ARGS__)
  55. /* Background thread name pattern */
  56. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  57. /*
  58. * This marker in the EBA table means that the LEB is um-mapped.
  59. * NOTE! It has to have the same value as %UBI_ALL.
  60. */
  61. #define UBI_LEB_UNMAPPED -1
  62. /*
  63. * In case of errors, UBI tries to repeat the operation several times before
  64. * returning error. The below constant defines how many times UBI re-tries.
  65. */
  66. #define UBI_IO_RETRIES 3
  67. /*
  68. * Length of the protection queue. The length is effectively equivalent to the
  69. * number of (global) erase cycles PEBs are protected from the wear-leveling
  70. * worker.
  71. */
  72. #define UBI_PROT_QUEUE_LEN 10
  73. /* The volume ID/LEB number/erase counter is unknown */
  74. #define UBI_UNKNOWN -1
  75. /*
  76. * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
  77. * + 2 for the number plus 1 for the trailing zero byte.
  78. */
  79. #define UBI_DFS_DIR_NAME "ubi%d"
  80. #define UBI_DFS_DIR_LEN (3 + 2 + 1)
  81. /*
  82. * Error codes returned by the I/O sub-system.
  83. *
  84. * UBI_IO_FF: the read region of flash contains only 0xFFs
  85. * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
  86. * integrity error reported by the MTD driver
  87. * (uncorrectable ECC error in case of NAND)
  88. * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
  89. * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
  90. * data integrity error reported by the MTD driver
  91. * (uncorrectable ECC error in case of NAND)
  92. * UBI_IO_BITFLIPS: bit-flips were detected and corrected
  93. *
  94. * Note, it is probably better to have bit-flip and ebadmsg as flags which can
  95. * be or'ed with other error code. But this is a big change because there are
  96. * may callers, so it does not worth the risk of introducing a bug
  97. */
  98. enum {
  99. UBI_IO_FF = 1,
  100. UBI_IO_FF_BITFLIPS,
  101. UBI_IO_BAD_HDR,
  102. UBI_IO_BAD_HDR_EBADMSG,
  103. UBI_IO_BITFLIPS,
  104. };
  105. /*
  106. * Return codes of the 'ubi_eba_copy_leb()' function.
  107. *
  108. * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
  109. * PEB was put meanwhile, or there is I/O on the source PEB
  110. * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
  111. * PEB
  112. * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
  113. * PEB
  114. * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
  115. * PEB
  116. * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
  117. * target PEB
  118. * MOVE_RETRY: retry scrubbing the PEB
  119. */
  120. enum {
  121. MOVE_CANCEL_RACE = 1,
  122. MOVE_SOURCE_RD_ERR,
  123. MOVE_TARGET_RD_ERR,
  124. MOVE_TARGET_WR_ERR,
  125. MOVE_TARGET_BITFLIPS,
  126. MOVE_RETRY,
  127. };
  128. /*
  129. * Return codes of the fastmap sub-system
  130. *
  131. * UBI_NO_FASTMAP: No fastmap super block was found
  132. * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
  133. */
  134. enum {
  135. UBI_NO_FASTMAP = 1,
  136. UBI_BAD_FASTMAP,
  137. };
  138. /**
  139. * struct ubi_wl_entry - wear-leveling entry.
  140. * @u.rb: link in the corresponding (free/used) RB-tree
  141. * @u.list: link in the protection queue
  142. * @ec: erase counter
  143. * @pnum: physical eraseblock number
  144. *
  145. * This data structure is used in the WL sub-system. Each physical eraseblock
  146. * has a corresponding &struct wl_entry object which may be kept in different
  147. * RB-trees. See WL sub-system for details.
  148. */
  149. struct ubi_wl_entry {
  150. union {
  151. struct rb_node rb;
  152. struct list_head list;
  153. } u;
  154. int ec;
  155. int pnum;
  156. };
  157. /**
  158. * struct ubi_ltree_entry - an entry in the lock tree.
  159. * @rb: links RB-tree nodes
  160. * @vol_id: volume ID of the locked logical eraseblock
  161. * @lnum: locked logical eraseblock number
  162. * @users: how many tasks are using this logical eraseblock or wait for it
  163. * @mutex: read/write mutex to implement read/write access serialization to
  164. * the (@vol_id, @lnum) logical eraseblock
  165. *
  166. * This data structure is used in the EBA sub-system to implement per-LEB
  167. * locking. When a logical eraseblock is being locked - corresponding
  168. * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
  169. * See EBA sub-system for details.
  170. */
  171. struct ubi_ltree_entry {
  172. struct rb_node rb;
  173. int vol_id;
  174. int lnum;
  175. int users;
  176. struct rw_semaphore mutex;
  177. };
  178. /**
  179. * struct ubi_rename_entry - volume re-name description data structure.
  180. * @new_name_len: new volume name length
  181. * @new_name: new volume name
  182. * @remove: if not zero, this volume should be removed, not re-named
  183. * @desc: descriptor of the volume
  184. * @list: links re-name entries into a list
  185. *
  186. * This data structure is utilized in the multiple volume re-name code. Namely,
  187. * UBI first creates a list of &struct ubi_rename_entry objects from the
  188. * &struct ubi_rnvol_req request object, and then utilizes this list to do all
  189. * the job.
  190. */
  191. struct ubi_rename_entry {
  192. int new_name_len;
  193. char new_name[UBI_VOL_NAME_MAX + 1];
  194. int remove;
  195. struct ubi_volume_desc *desc;
  196. struct list_head list;
  197. };
  198. struct ubi_volume_desc;
  199. /**
  200. * struct ubi_fastmap_layout - in-memory fastmap data structure.
  201. * @e: PEBs used by the current fastmap
  202. * @to_be_tortured: if non-zero tortured this PEB
  203. * @used_blocks: number of used PEBs
  204. * @max_pool_size: maximal size of the user pool
  205. * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
  206. */
  207. struct ubi_fastmap_layout {
  208. struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
  209. int to_be_tortured[UBI_FM_MAX_BLOCKS];
  210. int used_blocks;
  211. int max_pool_size;
  212. int max_wl_pool_size;
  213. };
  214. /**
  215. * struct ubi_fm_pool - in-memory fastmap pool
  216. * @pebs: PEBs in this pool
  217. * @used: number of used PEBs
  218. * @size: total number of PEBs in this pool
  219. * @max_size: maximal size of the pool
  220. *
  221. * A pool gets filled with up to max_size.
  222. * If all PEBs within the pool are used a new fastmap will be written
  223. * to the flash and the pool gets refilled with empty PEBs.
  224. *
  225. */
  226. struct ubi_fm_pool {
  227. int pebs[UBI_FM_MAX_POOL_SIZE];
  228. int used;
  229. int size;
  230. int max_size;
  231. };
  232. /**
  233. * struct ubi_volume - UBI volume description data structure.
  234. * @dev: device object to make use of the the Linux device model
  235. * @cdev: character device object to create character device
  236. * @ubi: reference to the UBI device description object
  237. * @vol_id: volume ID
  238. * @ref_count: volume reference count
  239. * @readers: number of users holding this volume in read-only mode
  240. * @writers: number of users holding this volume in read-write mode
  241. * @exclusive: whether somebody holds this volume in exclusive mode
  242. *
  243. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  244. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  245. * @usable_leb_size: logical eraseblock size without padding
  246. * @used_ebs: how many logical eraseblocks in this volume contain data
  247. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  248. * @used_bytes: how many bytes of data this volume contains
  249. * @alignment: volume alignment
  250. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  251. * satisfy the requested alignment
  252. * @name_len: volume name length
  253. * @name: volume name
  254. *
  255. * @upd_ebs: how many eraseblocks are expected to be updated
  256. * @ch_lnum: LEB number which is being changing by the atomic LEB change
  257. * operation
  258. * @upd_bytes: how many bytes are expected to be received for volume update or
  259. * atomic LEB change
  260. * @upd_received: how many bytes were already received for volume update or
  261. * atomic LEB change
  262. * @upd_buf: update buffer which is used to collect update data or data for
  263. * atomic LEB change
  264. *
  265. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  266. * @checked: %1 if this static volume was checked
  267. * @corrupted: %1 if the volume is corrupted (static volumes only)
  268. * @upd_marker: %1 if the update marker is set for this volume
  269. * @updating: %1 if the volume is being updated
  270. * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
  271. * @direct_writes: %1 if direct writes are enabled for this volume
  272. *
  273. * The @corrupted field indicates that the volume's contents is corrupted.
  274. * Since UBI protects only static volumes, this field is not relevant to
  275. * dynamic volumes - it is user's responsibility to assure their data
  276. * integrity.
  277. *
  278. * The @upd_marker flag indicates that this volume is either being updated at
  279. * the moment or is damaged because of an unclean reboot.
  280. */
  281. struct ubi_volume {
  282. struct device dev;
  283. struct cdev cdev;
  284. struct ubi_device *ubi;
  285. int vol_id;
  286. int ref_count;
  287. int readers;
  288. int writers;
  289. int exclusive;
  290. int reserved_pebs;
  291. int vol_type;
  292. int usable_leb_size;
  293. int used_ebs;
  294. int last_eb_bytes;
  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. 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 @cancel argument is
  676. * not zero, the worker has to free the resources and exit immediately. The
  677. * worker has to return zero in case of success and a negative error code in
  678. * case of failure.
  679. */
  680. struct ubi_work {
  681. struct list_head list;
  682. int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
  683. /* The below fields are only relevant to erasure works */
  684. struct ubi_wl_entry *e;
  685. int vol_id;
  686. int lnum;
  687. int torture;
  688. int anchor;
  689. };
  690. #include "debug.h"
  691. extern struct kmem_cache *ubi_wl_entry_slab;
  692. extern const struct file_operations ubi_ctrl_cdev_operations;
  693. extern const struct file_operations ubi_cdev_operations;
  694. extern const struct file_operations ubi_vol_cdev_operations;
  695. extern struct class *ubi_class;
  696. extern struct mutex ubi_devices_mutex;
  697. extern struct blocking_notifier_head ubi_notifiers;
  698. /* attach.c */
  699. int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
  700. int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
  701. struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
  702. int vol_id);
  703. void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
  704. struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
  705. struct ubi_attach_info *ai);
  706. int ubi_attach(struct ubi_device *ubi, int force_scan);
  707. void ubi_destroy_ai(struct ubi_attach_info *ai);
  708. /* vtbl.c */
  709. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  710. struct ubi_vtbl_record *vtbl_rec);
  711. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  712. struct list_head *rename_list);
  713. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
  714. /* vmt.c */
  715. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  716. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
  717. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  718. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
  719. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  720. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  721. /* upd.c */
  722. int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
  723. long long bytes);
  724. int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
  725. const void __user *buf, int count);
  726. int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  727. const struct ubi_leb_change_req *req);
  728. int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
  729. const void __user *buf, int count);
  730. /* misc.c */
  731. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  732. int length);
  733. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  734. void ubi_update_reserved(struct ubi_device *ubi);
  735. void ubi_calculate_reserved(struct ubi_device *ubi);
  736. int ubi_check_pattern(const void *buf, uint8_t patt, int size);
  737. /* eba.c */
  738. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  739. int lnum);
  740. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  741. void *buf, int offset, int len, int check);
  742. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  743. const void *buf, int offset, int len);
  744. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  745. int lnum, const void *buf, int len, int used_ebs);
  746. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  747. int lnum, const void *buf, int len);
  748. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  749. struct ubi_vid_hdr *vid_hdr);
  750. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  751. unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
  752. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  753. struct ubi_attach_info *ai_scan);
  754. /* wl.c */
  755. int ubi_wl_get_peb(struct ubi_device *ubi);
  756. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  757. int pnum, int torture);
  758. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
  759. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  760. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  761. void ubi_wl_close(struct ubi_device *ubi);
  762. int ubi_thread(void *u);
  763. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
  764. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
  765. int lnum, int torture);
  766. int ubi_is_erase_work(struct ubi_work *wrk);
  767. void ubi_refill_pools(struct ubi_device *ubi);
  768. int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
  769. /* io.c */
  770. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  771. int len);
  772. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  773. int len);
  774. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
  775. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  776. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  777. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  778. struct ubi_ec_hdr *ec_hdr, int verbose);
  779. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  780. struct ubi_ec_hdr *ec_hdr);
  781. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  782. struct ubi_vid_hdr *vid_hdr, int verbose);
  783. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  784. struct ubi_vid_hdr *vid_hdr);
  785. /* build.c */
  786. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  787. int vid_hdr_offset, int max_beb_per1024);
  788. int ubi_detach_mtd_dev(int ubi_num, int anyway);
  789. struct ubi_device *ubi_get_device(int ubi_num);
  790. void ubi_put_device(struct ubi_device *ubi);
  791. struct ubi_device *ubi_get_by_major(int major);
  792. int ubi_major2num(int major);
  793. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
  794. int ntype);
  795. int ubi_notify_all(struct ubi_device *ubi, int ntype,
  796. struct notifier_block *nb);
  797. int ubi_enumerate_volumes(struct notifier_block *nb);
  798. void ubi_free_internal_volumes(struct ubi_device *ubi);
  799. /* kapi.c */
  800. void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
  801. void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
  802. struct ubi_volume_info *vi);
  803. /* scan.c */
  804. int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
  805. int pnum, const struct ubi_vid_hdr *vid_hdr);
  806. /* fastmap.c */
  807. size_t ubi_calc_fm_size(struct ubi_device *ubi);
  808. int ubi_update_fastmap(struct ubi_device *ubi);
  809. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  810. int fm_anchor);
  811. /*
  812. * ubi_rb_for_each_entry - walk an RB-tree.
  813. * @rb: a pointer to type 'struct rb_node' to use as a loop counter
  814. * @pos: a pointer to RB-tree entry type to use as a loop counter
  815. * @root: RB-tree's root
  816. * @member: the name of the 'struct rb_node' within the RB-tree entry
  817. */
  818. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  819. for (rb = rb_first(root), \
  820. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  821. rb; \
  822. rb = rb_next(rb), \
  823. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
  824. /*
  825. * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
  826. *
  827. * @av: volume attaching information
  828. * @aeb: attaching eraseblock information
  829. * @list: the list to move to
  830. */
  831. static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
  832. struct ubi_ainf_peb *aeb,
  833. struct list_head *list)
  834. {
  835. rb_erase(&aeb->u.rb, &av->root);
  836. list_add_tail(&aeb->u.list, list);
  837. }
  838. /**
  839. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  840. * @ubi: UBI device description object
  841. * @gfp_flags: GFP flags to allocate with
  842. *
  843. * This function returns a pointer to the newly allocated and zero-filled
  844. * volume identifier header object in case of success and %NULL in case of
  845. * failure.
  846. */
  847. static inline struct ubi_vid_hdr *
  848. ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
  849. {
  850. void *vid_hdr;
  851. vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
  852. if (!vid_hdr)
  853. return NULL;
  854. /*
  855. * VID headers may be stored at un-aligned flash offsets, so we shift
  856. * the pointer.
  857. */
  858. return vid_hdr + ubi->vid_hdr_shift;
  859. }
  860. /**
  861. * ubi_free_vid_hdr - free a volume identifier header object.
  862. * @ubi: UBI device description object
  863. * @vid_hdr: the object to free
  864. */
  865. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  866. struct ubi_vid_hdr *vid_hdr)
  867. {
  868. void *p = vid_hdr;
  869. if (!p)
  870. return;
  871. kfree(p - ubi->vid_hdr_shift);
  872. }
  873. /*
  874. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  875. * the beginning of the logical eraseblock, not to the beginning of the
  876. * physical eraseblock.
  877. */
  878. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  879. int pnum, int offset, int len)
  880. {
  881. ubi_assert(offset >= 0);
  882. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  883. }
  884. /*
  885. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  886. * the beginning of the logical eraseblock, not to the beginning of the
  887. * physical eraseblock.
  888. */
  889. static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
  890. int pnum, int offset, int len)
  891. {
  892. ubi_assert(offset >= 0);
  893. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  894. }
  895. /**
  896. * ubi_ro_mode - switch to read-only mode.
  897. * @ubi: UBI device description object
  898. */
  899. static inline void ubi_ro_mode(struct ubi_device *ubi)
  900. {
  901. if (!ubi->ro_mode) {
  902. ubi->ro_mode = 1;
  903. ubi_warn("switch to read-only mode");
  904. dump_stack();
  905. }
  906. }
  907. /**
  908. * vol_id2idx - get table index by volume ID.
  909. * @ubi: UBI device description object
  910. * @vol_id: volume ID
  911. */
  912. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  913. {
  914. if (vol_id >= UBI_INTERNAL_VOL_START)
  915. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  916. else
  917. return vol_id;
  918. }
  919. /**
  920. * idx2vol_id - get volume ID by table index.
  921. * @ubi: UBI device description object
  922. * @idx: table index
  923. */
  924. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  925. {
  926. if (idx >= ubi->vtbl_slots)
  927. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  928. else
  929. return idx;
  930. }
  931. #endif /* !__UBI_UBI_H__ */