ubi.h 22 KB

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