ubi.h 37 KB

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