ubi.h 39 KB

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