ubi.h 24 KB

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