ubi.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 <mtd/ubi-header.h>
  39. #include <linux/mtd/ubi.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. __FUNCTION__, ##__VA_ARGS__)
  51. /* UBI error messages */
  52. #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
  53. __FUNCTION__, ##__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 unit.
  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. extern int ubi_devices_cnt;
  85. extern struct ubi_device *ubi_devices[];
  86. /**
  87. * struct ubi_wl_entry - wear-leveling entry.
  88. * @rb: link in the corresponding RB-tree
  89. * @ec: erase counter
  90. * @pnum: physical eraseblock number
  91. *
  92. * This data structure is used in the WL unit. Each physical eraseblock has a
  93. * corresponding &struct wl_entry object which may be kept in different
  94. * RB-trees. See WL unit for details.
  95. */
  96. struct ubi_wl_entry {
  97. struct rb_node rb;
  98. int ec;
  99. int pnum;
  100. };
  101. /**
  102. * struct ubi_ltree_entry - an entry in the lock tree.
  103. * @rb: links RB-tree nodes
  104. * @vol_id: volume ID of the locked logical eraseblock
  105. * @lnum: locked logical eraseblock number
  106. * @users: how many tasks are using this logical eraseblock or wait for it
  107. * @mutex: read/write mutex to implement read/write access serialization to
  108. * the (@vol_id, @lnum) logical eraseblock
  109. *
  110. * This data structure is used in the EBA unit to implement per-LEB locking.
  111. * When a logical eraseblock is being locked - corresponding
  112. * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
  113. * See EBA unit for details.
  114. */
  115. struct ubi_ltree_entry {
  116. struct rb_node rb;
  117. int vol_id;
  118. int lnum;
  119. int users;
  120. struct rw_semaphore mutex;
  121. };
  122. struct ubi_volume_desc;
  123. /**
  124. * struct ubi_volume - UBI volume description data structure.
  125. * @dev: device object to make use of the the Linux device model
  126. * @cdev: character device object to create character device
  127. * @ubi: reference to the UBI device description object
  128. * @vol_id: volume ID
  129. * @readers: number of users holding this volume in read-only mode
  130. * @writers: number of users holding this volume in read-write mode
  131. * @exclusive: whether somebody holds this volume in exclusive mode
  132. * @removed: if the volume was removed
  133. * @checked: if this static volume was checked
  134. *
  135. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  136. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  137. * @usable_leb_size: logical eraseblock size without padding
  138. * @used_ebs: how many logical eraseblocks in this volume contain data
  139. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  140. * @used_bytes: how many bytes of data this volume contains
  141. * @upd_marker: non-zero if the update marker is set for this volume
  142. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  143. * @alignment: volume alignment
  144. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  145. * satisfy the requested alignment
  146. * @name_len: volume name length
  147. * @name: volume name
  148. *
  149. * @updating: whether the volume is being updated
  150. * @upd_ebs: how many eraseblocks are expected to be updated
  151. * @upd_bytes: how many bytes are expected to be received
  152. * @upd_received: how many update bytes were already received
  153. * @upd_buf: update buffer which is used to collect update data
  154. *
  155. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  156. *
  157. * @gluebi_desc: gluebi UBI volume descriptor
  158. * @gluebi_refcount: reference count of the gluebi MTD device
  159. * @gluebi_mtd: MTD device description object of the gluebi MTD device
  160. *
  161. * The @corrupted field indicates that the volume's contents is corrupted.
  162. * Since UBI protects only static volumes, this field is not relevant to
  163. * dynamic volumes - it is user's responsibility to assure their data
  164. * integrity.
  165. *
  166. * The @upd_marker flag indicates that this volume is either being updated at
  167. * the moment or is damaged because of an unclean reboot.
  168. */
  169. struct ubi_volume {
  170. struct device dev;
  171. struct cdev cdev;
  172. struct ubi_device *ubi;
  173. int vol_id;
  174. int readers;
  175. int writers;
  176. int exclusive;
  177. int removed;
  178. int checked;
  179. int reserved_pebs;
  180. int vol_type;
  181. int usable_leb_size;
  182. int used_ebs;
  183. int last_eb_bytes;
  184. long long used_bytes;
  185. int upd_marker;
  186. int corrupted;
  187. int alignment;
  188. int data_pad;
  189. int name_len;
  190. char name[UBI_VOL_NAME_MAX+1];
  191. int updating;
  192. int upd_ebs;
  193. long long upd_bytes;
  194. long long upd_received;
  195. void *upd_buf;
  196. int *eba_tbl;
  197. #ifdef CONFIG_MTD_UBI_GLUEBI
  198. /* Gluebi-related stuff may be compiled out */
  199. struct ubi_volume_desc *gluebi_desc;
  200. int gluebi_refcount;
  201. struct mtd_info gluebi_mtd;
  202. #endif
  203. };
  204. /**
  205. * struct ubi_volume_desc - descriptor of the UBI volume returned when it is
  206. * opened.
  207. * @vol: reference to the corresponding volume description object
  208. * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
  209. */
  210. struct ubi_volume_desc {
  211. struct ubi_volume *vol;
  212. int mode;
  213. };
  214. struct ubi_wl_entry;
  215. /**
  216. * struct ubi_device - UBI device description structure
  217. * @dev: class device object to use the the Linux device model
  218. * @cdev: character device object to create character device
  219. * @ubi_num: UBI device number
  220. * @ubi_name: UBI device name
  221. * @vol_count: number of volumes in this UBI device
  222. * @volumes: volumes of this UBI device
  223. * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
  224. * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count, @vol->readers,
  225. * @vol->writers, @vol->exclusive, @vol->removed, @vol->mapping and
  226. * @vol->eba_tbl.
  227. *
  228. * @rsvd_pebs: count of reserved physical eraseblocks
  229. * @avail_pebs: count of available physical eraseblocks
  230. * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
  231. * handling
  232. * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
  233. *
  234. * @vtbl_slots: how many slots are available in the volume table
  235. * @vtbl_size: size of the volume table in bytes
  236. * @vtbl: in-RAM volume table copy
  237. * @vtbl_mutex: protects on-flash volume table
  238. *
  239. * @max_ec: current highest erase counter value
  240. * @mean_ec: current mean erase counter value
  241. *
  242. * @global_sqnum: global sequence number
  243. * @ltree_lock: protects the lock tree and @global_sqnum
  244. * @ltree: the lock tree
  245. * @alc_mutex: serializes "atomic LEB change" operations
  246. *
  247. * @used: RB-tree of used physical eraseblocks
  248. * @free: RB-tree of free physical eraseblocks
  249. * @scrub: RB-tree of physical eraseblocks which need scrubbing
  250. * @prot: protection trees
  251. * @prot.pnum: protection tree indexed by physical eraseblock numbers
  252. * @prot.aec: protection tree indexed by absolute erase counter value
  253. * @wl_lock: protects the @used, @free, @prot, @lookuptbl, @abs_ec, @move_from,
  254. * @move_to, @move_to_put @erase_pending, @wl_scheduled, and @works
  255. * fields
  256. * @wl_scheduled: non-zero if the wear-leveling was scheduled
  257. * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
  258. * physical eraseblock
  259. * @abs_ec: absolute erase counter
  260. * @move_from: physical eraseblock from where the data is being moved
  261. * @move_to: physical eraseblock where the data is being moved to
  262. * @move_from_put: if the "from" PEB was put
  263. * @move_to_put: if the "to" PEB was put
  264. * @works: list of pending works
  265. * @works_count: count of pending works
  266. * @bgt_thread: background thread description object
  267. * @thread_enabled: if the background thread is enabled
  268. * @bgt_name: background thread name
  269. *
  270. * @flash_size: underlying MTD device size (in bytes)
  271. * @peb_count: count of physical eraseblocks on the MTD device
  272. * @peb_size: physical eraseblock size
  273. * @bad_peb_count: count of bad physical eraseblocks
  274. * @good_peb_count: count of good physical eraseblocks
  275. * @min_io_size: minimal input/output unit size of the underlying MTD device
  276. * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
  277. * @ro_mode: if the UBI device is in read-only mode
  278. * @leb_size: logical eraseblock size
  279. * @leb_start: starting offset of logical eraseblocks within physical
  280. * eraseblocks
  281. * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
  282. * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
  283. * @vid_hdr_offset: starting offset of the volume identifier header (might be
  284. * unaligned)
  285. * @vid_hdr_aloffset: starting offset of the VID header aligned to
  286. * @hdrs_min_io_size
  287. * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
  288. * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
  289. * not
  290. * @mtd: MTD device descriptor
  291. *
  292. * @peb_buf1: a buffer of PEB size used for different purposes
  293. * @peb_buf2: another buffer of PEB size used for different purposes
  294. * @buf_mutex: proptects @peb_buf1 and @peb_buf2
  295. * @dbg_peb_buf: buffer of PEB size used for debugging
  296. * @dbg_buf_mutex: proptects @dbg_peb_buf
  297. */
  298. struct ubi_device {
  299. struct cdev cdev;
  300. struct device dev;
  301. int ubi_num;
  302. char ubi_name[sizeof(UBI_NAME_STR)+5];
  303. int vol_count;
  304. struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
  305. spinlock_t volumes_lock;
  306. int rsvd_pebs;
  307. int avail_pebs;
  308. int beb_rsvd_pebs;
  309. int beb_rsvd_level;
  310. int vtbl_slots;
  311. int vtbl_size;
  312. struct ubi_vtbl_record *vtbl;
  313. struct mutex vtbl_mutex;
  314. int max_ec;
  315. int mean_ec;
  316. /* EBA unit's stuff */
  317. unsigned long long global_sqnum;
  318. spinlock_t ltree_lock;
  319. struct rb_root ltree;
  320. struct mutex alc_mutex;
  321. /* Wear-leveling unit's stuff */
  322. struct rb_root used;
  323. struct rb_root free;
  324. struct rb_root scrub;
  325. struct {
  326. struct rb_root pnum;
  327. struct rb_root aec;
  328. } prot;
  329. spinlock_t wl_lock;
  330. int wl_scheduled;
  331. struct ubi_wl_entry **lookuptbl;
  332. unsigned long long abs_ec;
  333. struct ubi_wl_entry *move_from;
  334. struct ubi_wl_entry *move_to;
  335. int move_from_put;
  336. int move_to_put;
  337. struct list_head works;
  338. int works_count;
  339. struct task_struct *bgt_thread;
  340. int thread_enabled;
  341. char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
  342. /* I/O unit's stuff */
  343. long long flash_size;
  344. int peb_count;
  345. int peb_size;
  346. int bad_peb_count;
  347. int good_peb_count;
  348. int min_io_size;
  349. int hdrs_min_io_size;
  350. int ro_mode;
  351. int leb_size;
  352. int leb_start;
  353. int ec_hdr_alsize;
  354. int vid_hdr_alsize;
  355. int vid_hdr_offset;
  356. int vid_hdr_aloffset;
  357. int vid_hdr_shift;
  358. int bad_allowed;
  359. struct mtd_info *mtd;
  360. void *peb_buf1;
  361. void *peb_buf2;
  362. struct mutex buf_mutex;
  363. #ifdef CONFIG_MTD_UBI_DEBUG
  364. void *dbg_peb_buf;
  365. struct mutex dbg_buf_mutex;
  366. #endif
  367. };
  368. extern struct kmem_cache *ubi_ltree_slab;
  369. extern struct kmem_cache *ubi_wl_entry_slab;
  370. extern struct file_operations ubi_cdev_operations;
  371. extern struct file_operations ubi_vol_cdev_operations;
  372. extern struct class *ubi_class;
  373. /* vtbl.c */
  374. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  375. struct ubi_vtbl_record *vtbl_rec);
  376. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
  377. /* vmt.c */
  378. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  379. int ubi_remove_volume(struct ubi_volume_desc *desc);
  380. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  381. int ubi_add_volume(struct ubi_device *ubi, int vol_id);
  382. void ubi_free_volume(struct ubi_device *ubi, int vol_id);
  383. /* upd.c */
  384. int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes);
  385. int ubi_more_update_data(struct ubi_device *ubi, int vol_id,
  386. const void __user *buf, int count);
  387. /* misc.c */
  388. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf, int length);
  389. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  390. void ubi_calculate_reserved(struct ubi_device *ubi);
  391. /* gluebi.c */
  392. #ifdef CONFIG_MTD_UBI_GLUEBI
  393. int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
  394. int ubi_destroy_gluebi(struct ubi_volume *vol);
  395. void ubi_gluebi_updated(struct ubi_volume *vol);
  396. #else
  397. #define ubi_create_gluebi(ubi, vol) 0
  398. #define ubi_destroy_gluebi(vol) 0
  399. #define ubi_gluebi_updated(vol)
  400. #endif
  401. /* eba.c */
  402. int ubi_eba_unmap_leb(struct ubi_device *ubi, int vol_id, int lnum);
  403. int ubi_eba_read_leb(struct ubi_device *ubi, int vol_id, int lnum, void *buf,
  404. int offset, int len, int check);
  405. int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum,
  406. const void *buf, int offset, int len, int dtype);
  407. int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum,
  408. const void *buf, int len, int dtype,
  409. int used_ebs);
  410. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
  411. const void *buf, int len, int dtype);
  412. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  413. struct ubi_vid_hdr *vid_hdr);
  414. int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  415. void ubi_eba_close(const struct ubi_device *ubi);
  416. /* wl.c */
  417. int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
  418. int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
  419. int ubi_wl_flush(struct ubi_device *ubi);
  420. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  421. int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
  422. void ubi_wl_close(struct ubi_device *ubi);
  423. /* io.c */
  424. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  425. int len);
  426. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  427. int len);
  428. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
  429. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  430. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  431. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  432. struct ubi_ec_hdr *ec_hdr, int verbose);
  433. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  434. struct ubi_ec_hdr *ec_hdr);
  435. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  436. struct ubi_vid_hdr *vid_hdr, int verbose);
  437. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  438. struct ubi_vid_hdr *vid_hdr);
  439. /*
  440. * ubi_rb_for_each_entry - walk an RB-tree.
  441. * @rb: a pointer to type 'struct rb_node' to to use as a loop counter
  442. * @pos: a pointer to RB-tree entry type to use as a loop counter
  443. * @root: RB-tree's root
  444. * @member: the name of the 'struct rb_node' within the RB-tree entry
  445. */
  446. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  447. for (rb = rb_first(root), \
  448. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  449. rb; \
  450. rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
  451. /**
  452. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  453. * @ubi: UBI device description object
  454. * @gfp_flags: GFP flags to allocate with
  455. *
  456. * This function returns a pointer to the newly allocated and zero-filled
  457. * volume identifier header object in case of success and %NULL in case of
  458. * failure.
  459. */
  460. static inline struct ubi_vid_hdr *
  461. ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
  462. {
  463. void *vid_hdr;
  464. vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
  465. if (!vid_hdr)
  466. return NULL;
  467. /*
  468. * VID headers may be stored at un-aligned flash offsets, so we shift
  469. * the pointer.
  470. */
  471. return vid_hdr + ubi->vid_hdr_shift;
  472. }
  473. /**
  474. * ubi_free_vid_hdr - free a volume identifier header object.
  475. * @ubi: UBI device description object
  476. * @vid_hdr: the object to free
  477. */
  478. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  479. struct ubi_vid_hdr *vid_hdr)
  480. {
  481. void *p = vid_hdr;
  482. if (!p)
  483. return;
  484. kfree(p - ubi->vid_hdr_shift);
  485. }
  486. /*
  487. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  488. * the beginning of the logical eraseblock, not to the beginning of the
  489. * physical eraseblock.
  490. */
  491. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  492. int pnum, int offset, int len)
  493. {
  494. ubi_assert(offset >= 0);
  495. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  496. }
  497. /*
  498. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  499. * the beginning of the logical eraseblock, not to the beginning of the
  500. * physical eraseblock.
  501. */
  502. static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
  503. int pnum, int offset, int len)
  504. {
  505. ubi_assert(offset >= 0);
  506. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  507. }
  508. /**
  509. * ubi_ro_mode - switch to read-only mode.
  510. * @ubi: UBI device description object
  511. */
  512. static inline void ubi_ro_mode(struct ubi_device *ubi)
  513. {
  514. ubi->ro_mode = 1;
  515. ubi_warn("switch to read-only mode");
  516. }
  517. /**
  518. * vol_id2idx - get table index by volume ID.
  519. * @ubi: UBI device description object
  520. * @vol_id: volume ID
  521. */
  522. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  523. {
  524. if (vol_id >= UBI_INTERNAL_VOL_START)
  525. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  526. else
  527. return vol_id;
  528. }
  529. /**
  530. * idx2vol_id - get volume ID by table index.
  531. * @ubi: UBI device description object
  532. * @idx: table index
  533. */
  534. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  535. {
  536. if (idx >= ubi->vtbl_slots)
  537. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  538. else
  539. return idx;
  540. }
  541. #endif /* !__UBI_UBI_H__ */