ubi.h 23 KB

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