ubi.h 22 KB

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