ubi.h 21 KB

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