ubi.h 41 KB

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