ubi.h 23 KB

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