ubi.h 33 KB

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