ubi.h 29 KB

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