ubi.h 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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/types.h>
  24. #include <linux/list.h>
  25. #include <linux/rbtree.h>
  26. #include <linux/sched.h>
  27. #include <linux/wait.h>
  28. #include <linux/mutex.h>
  29. #include <linux/rwsem.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/fs.h>
  32. #include <linux/cdev.h>
  33. #include <linux/device.h>
  34. #include <linux/slab.h>
  35. #include <linux/string.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/notifier.h>
  38. #include <linux/mtd/mtd.h>
  39. #include <linux/mtd/ubi.h>
  40. #include <asm/pgtable.h>
  41. #include "ubi-media.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, ...) pr_notice("UBI: " fmt "\n", ##__VA_ARGS__)
  48. /* UBI warning messages */
  49. #define ubi_warn(fmt, ...) pr_warn("UBI warning: %s: " fmt "\n", \
  50. __func__, ##__VA_ARGS__)
  51. /* UBI error messages */
  52. #define ubi_err(fmt, ...) pr_err("UBI error: %s: " fmt "\n", \
  53. __func__, ##__VA_ARGS__)
  54. /* Background thread name pattern */
  55. #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
  56. /*
  57. * This marker in the EBA table means that the LEB is um-mapped.
  58. * NOTE! It has to have the same value as %UBI_ALL.
  59. */
  60. #define UBI_LEB_UNMAPPED -1
  61. /*
  62. * In case of errors, UBI tries to repeat the operation several times before
  63. * returning error. The below constant defines how many times UBI re-tries.
  64. */
  65. #define UBI_IO_RETRIES 3
  66. /*
  67. * Length of the protection queue. The length is effectively equivalent to the
  68. * number of (global) erase cycles PEBs are protected from the wear-leveling
  69. * worker.
  70. */
  71. #define UBI_PROT_QUEUE_LEN 10
  72. /* The volume ID/LEB number/erase counter is unknown */
  73. #define UBI_UNKNOWN -1
  74. /*
  75. * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
  76. * + 2 for the number plus 1 for the trailing zero byte.
  77. */
  78. #define UBI_DFS_DIR_NAME "ubi%d"
  79. #define UBI_DFS_DIR_LEN (3 + 2 + 1)
  80. /*
  81. * Error codes returned by the I/O sub-system.
  82. *
  83. * UBI_IO_FF: the read region of flash contains only 0xFFs
  84. * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
  85. * integrity error reported by the MTD driver
  86. * (uncorrectable ECC error in case of NAND)
  87. * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
  88. * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
  89. * data integrity error reported by the MTD driver
  90. * (uncorrectable ECC error in case of NAND)
  91. * UBI_IO_BITFLIPS: bit-flips were detected and corrected
  92. *
  93. * Note, it is probably better to have bit-flip and ebadmsg as flags which can
  94. * be or'ed with other error code. But this is a big change because there are
  95. * may callers, so it does not worth the risk of introducing a bug
  96. */
  97. enum {
  98. UBI_IO_FF = 1,
  99. UBI_IO_FF_BITFLIPS,
  100. UBI_IO_BAD_HDR,
  101. UBI_IO_BAD_HDR_EBADMSG,
  102. UBI_IO_BITFLIPS,
  103. };
  104. /*
  105. * Return codes of the 'ubi_eba_copy_leb()' function.
  106. *
  107. * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
  108. * PEB was put meanwhile, or there is I/O on the source PEB
  109. * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
  110. * PEB
  111. * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
  112. * PEB
  113. * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
  114. * PEB
  115. * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
  116. * target PEB
  117. * MOVE_RETRY: retry scrubbing the PEB
  118. */
  119. enum {
  120. MOVE_CANCEL_RACE = 1,
  121. MOVE_SOURCE_RD_ERR,
  122. MOVE_TARGET_RD_ERR,
  123. MOVE_TARGET_WR_ERR,
  124. MOVE_TARGET_BITFLIPS,
  125. MOVE_RETRY,
  126. };
  127. /*
  128. * Return codes of the fastmap sub-system
  129. *
  130. * UBI_NO_FASTMAP: No fastmap super block was found
  131. * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
  132. */
  133. enum {
  134. UBI_NO_FASTMAP = 1,
  135. UBI_BAD_FASTMAP,
  136. };
  137. /**
  138. * struct ubi_wl_entry - wear-leveling entry.
  139. * @u.rb: link in the corresponding (free/used) RB-tree
  140. * @u.list: link in the protection queue
  141. * @ec: erase counter
  142. * @pnum: physical eraseblock number
  143. *
  144. * This data structure is used in the WL sub-system. Each physical eraseblock
  145. * has a corresponding &struct wl_entry object which may be kept in different
  146. * RB-trees. See WL sub-system for details.
  147. */
  148. struct ubi_wl_entry {
  149. union {
  150. struct rb_node rb;
  151. struct list_head list;
  152. } u;
  153. int ec;
  154. int pnum;
  155. };
  156. /**
  157. * struct ubi_ltree_entry - an entry in the lock tree.
  158. * @rb: links RB-tree nodes
  159. * @vol_id: volume ID of the locked logical eraseblock
  160. * @lnum: locked logical eraseblock number
  161. * @users: how many tasks are using this logical eraseblock or wait for it
  162. * @mutex: read/write mutex to implement read/write access serialization to
  163. * the (@vol_id, @lnum) logical eraseblock
  164. *
  165. * This data structure is used in the EBA sub-system to implement per-LEB
  166. * locking. When a logical eraseblock is being locked - corresponding
  167. * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
  168. * See EBA sub-system for details.
  169. */
  170. struct ubi_ltree_entry {
  171. struct rb_node rb;
  172. int vol_id;
  173. int lnum;
  174. int users;
  175. struct rw_semaphore mutex;
  176. };
  177. /**
  178. * struct ubi_rename_entry - volume re-name description data structure.
  179. * @new_name_len: new volume name length
  180. * @new_name: new volume name
  181. * @remove: if not zero, this volume should be removed, not re-named
  182. * @desc: descriptor of the volume
  183. * @list: links re-name entries into a list
  184. *
  185. * This data structure is utilized in the multiple volume re-name code. Namely,
  186. * UBI first creates a list of &struct ubi_rename_entry objects from the
  187. * &struct ubi_rnvol_req request object, and then utilizes this list to do all
  188. * the job.
  189. */
  190. struct ubi_rename_entry {
  191. int new_name_len;
  192. char new_name[UBI_VOL_NAME_MAX + 1];
  193. int remove;
  194. struct ubi_volume_desc *desc;
  195. struct list_head list;
  196. };
  197. struct ubi_volume_desc;
  198. /**
  199. * struct ubi_fastmap_layout - in-memory fastmap data structure.
  200. * @e: PEBs used by the current fastmap
  201. * @to_be_tortured: if non-zero tortured this PEB
  202. * @used_blocks: number of used PEBs
  203. * @max_pool_size: maximal size of the user pool
  204. * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
  205. */
  206. struct ubi_fastmap_layout {
  207. struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
  208. int to_be_tortured[UBI_FM_MAX_BLOCKS];
  209. int used_blocks;
  210. int max_pool_size;
  211. int max_wl_pool_size;
  212. };
  213. /**
  214. * struct ubi_fm_pool - in-memory fastmap pool
  215. * @pebs: PEBs in this pool
  216. * @used: number of used PEBs
  217. * @size: total number of PEBs in this pool
  218. * @max_size: maximal size of the pool
  219. *
  220. * A pool gets filled with up to max_size.
  221. * If all PEBs within the pool are used a new fastmap will be written
  222. * to the flash and the pool gets refilled with empty PEBs.
  223. *
  224. */
  225. struct ubi_fm_pool {
  226. int pebs[UBI_FM_MAX_POOL_SIZE];
  227. int used;
  228. int size;
  229. int max_size;
  230. };
  231. /**
  232. * struct ubi_volume - UBI volume description data structure.
  233. * @dev: device object to make use of the the Linux device model
  234. * @cdev: character device object to create character device
  235. * @ubi: reference to the UBI device description object
  236. * @vol_id: volume ID
  237. * @ref_count: volume reference count
  238. * @readers: number of users holding this volume in read-only mode
  239. * @writers: number of users holding this volume in read-write mode
  240. * @exclusive: whether somebody holds this volume in exclusive mode
  241. *
  242. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  243. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  244. * @usable_leb_size: logical eraseblock size without padding
  245. * @used_ebs: how many logical eraseblocks in this volume contain data
  246. * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
  247. * @used_bytes: how many bytes of data this volume contains
  248. * @alignment: volume alignment
  249. * @data_pad: how many bytes are not used at the end of physical eraseblocks to
  250. * satisfy the requested alignment
  251. * @name_len: volume name length
  252. * @name: volume name
  253. *
  254. * @upd_ebs: how many eraseblocks are expected to be updated
  255. * @ch_lnum: LEB number which is being changing by the atomic LEB change
  256. * operation
  257. * @upd_bytes: how many bytes are expected to be received for volume update or
  258. * atomic LEB change
  259. * @upd_received: how many bytes were already received for volume update or
  260. * atomic LEB change
  261. * @upd_buf: update buffer which is used to collect update data or data for
  262. * atomic LEB change
  263. *
  264. * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
  265. * @checked: %1 if this static volume was checked
  266. * @corrupted: %1 if the volume is corrupted (static volumes only)
  267. * @upd_marker: %1 if the update marker is set for this volume
  268. * @updating: %1 if the volume is being updated
  269. * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
  270. * @direct_writes: %1 if direct writes are enabled for this volume
  271. *
  272. * The @corrupted field indicates that the volume's contents is corrupted.
  273. * Since UBI protects only static volumes, this field is not relevant to
  274. * dynamic volumes - it is user's responsibility to assure their data
  275. * integrity.
  276. *
  277. * The @upd_marker flag indicates that this volume is either being updated at
  278. * the moment or is damaged because of an unclean reboot.
  279. */
  280. struct ubi_volume {
  281. struct device dev;
  282. struct cdev cdev;
  283. struct ubi_device *ubi;
  284. int vol_id;
  285. int ref_count;
  286. int readers;
  287. int writers;
  288. int exclusive;
  289. int reserved_pebs;
  290. int vol_type;
  291. int usable_leb_size;
  292. int used_ebs;
  293. int last_eb_bytes;
  294. long long used_bytes;
  295. int alignment;
  296. int data_pad;
  297. int name_len;
  298. char name[UBI_VOL_NAME_MAX + 1];
  299. int upd_ebs;
  300. int ch_lnum;
  301. long long upd_bytes;
  302. long long upd_received;
  303. void *upd_buf;
  304. int *eba_tbl;
  305. unsigned int checked:1;
  306. unsigned int corrupted:1;
  307. unsigned int upd_marker:1;
  308. unsigned int updating:1;
  309. unsigned int changing_leb:1;
  310. unsigned int direct_writes:1;
  311. };
  312. /**
  313. * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
  314. * @vol: reference to the corresponding volume description object
  315. * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
  316. */
  317. struct ubi_volume_desc {
  318. struct ubi_volume *vol;
  319. int mode;
  320. };
  321. struct ubi_wl_entry;
  322. /**
  323. * struct ubi_debug_info - debugging information for an UBI device.
  324. *
  325. * @chk_gen: if UBI general extra checks are enabled
  326. * @chk_io: if UBI I/O extra checks are enabled
  327. * @disable_bgt: disable the background task for testing purposes
  328. * @emulate_bitflips: emulate bit-flips for testing purposes
  329. * @emulate_io_failures: emulate write/erase failures for testing purposes
  330. * @dfs_dir_name: name of debugfs directory containing files of this UBI device
  331. * @dfs_dir: direntry object of the UBI device debugfs directory
  332. * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
  333. * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
  334. * @dfs_disable_bgt: debugfs knob to disable the background task
  335. * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
  336. * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
  337. */
  338. struct ubi_debug_info {
  339. unsigned int chk_gen:1;
  340. unsigned int chk_io:1;
  341. unsigned int disable_bgt:1;
  342. unsigned int emulate_bitflips:1;
  343. unsigned int emulate_io_failures:1;
  344. char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
  345. struct dentry *dfs_dir;
  346. struct dentry *dfs_chk_gen;
  347. struct dentry *dfs_chk_io;
  348. struct dentry *dfs_disable_bgt;
  349. struct dentry *dfs_emulate_bitflips;
  350. struct dentry *dfs_emulate_io_failures;
  351. };
  352. /**
  353. * struct ubi_device - UBI device description structure
  354. * @dev: UBI device object to use the the Linux device model
  355. * @cdev: character device object to create character device
  356. * @ubi_num: UBI device number
  357. * @ubi_name: UBI device name
  358. * @vol_count: number of volumes in this UBI device
  359. * @volumes: volumes of this UBI device
  360. * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
  361. * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
  362. * @vol->readers, @vol->writers, @vol->exclusive,
  363. * @vol->ref_count, @vol->mapping and @vol->eba_tbl.
  364. * @ref_count: count of references on the UBI device
  365. * @image_seq: image sequence number recorded on EC headers
  366. *
  367. * @rsvd_pebs: count of reserved physical eraseblocks
  368. * @avail_pebs: count of available physical eraseblocks
  369. * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
  370. * handling
  371. * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
  372. *
  373. * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
  374. * of UBI initialization
  375. * @vtbl_slots: how many slots are available in the volume table
  376. * @vtbl_size: size of the volume table in bytes
  377. * @vtbl: in-RAM volume table copy
  378. * @device_mutex: protects on-flash volume table and serializes volume
  379. * creation, deletion, update, re-size, re-name and set
  380. * property
  381. *
  382. * @max_ec: current highest erase counter value
  383. * @mean_ec: current mean erase counter value
  384. *
  385. * @global_sqnum: global sequence number
  386. * @ltree_lock: protects the lock tree and @global_sqnum
  387. * @ltree: the lock tree
  388. * @alc_mutex: serializes "atomic LEB change" operations
  389. *
  390. * @fm_disabled: non-zero if fastmap is disabled (default)
  391. * @fm: in-memory data structure of the currently used fastmap
  392. * @fm_pool: in-memory data structure of the fastmap pool
  393. * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
  394. * sub-system
  395. * @fm_mutex: serializes ubi_update_fastmap() and protects @fm_buf
  396. * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
  397. * @fm_size: fastmap size in bytes
  398. * @fm_sem: allows ubi_update_fastmap() to block EBA table changes
  399. * @fm_work: fastmap work queue
  400. *
  401. * @used: RB-tree of used physical eraseblocks
  402. * @erroneous: RB-tree of erroneous used physical eraseblocks
  403. * @free: RB-tree of free physical eraseblocks
  404. * @free_count: Contains the number of elements in @free
  405. * @scrub: RB-tree of physical eraseblocks which need scrubbing
  406. * @pq: protection queue (contain physical eraseblocks which are temporarily
  407. * protected from the wear-leveling worker)
  408. * @pq_head: protection queue head
  409. * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
  410. * @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
  411. * @erroneous, and @erroneous_peb_count fields
  412. * @move_mutex: serializes eraseblock moves
  413. * @work_sem: synchronizes the WL worker with use tasks
  414. * @wl_scheduled: non-zero if the wear-leveling was scheduled
  415. * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
  416. * physical eraseblock
  417. * @move_from: physical eraseblock from where the data is being moved
  418. * @move_to: physical eraseblock where the data is being moved to
  419. * @move_to_put: if the "to" PEB was put
  420. * @works: list of pending works
  421. * @works_count: count of pending works
  422. * @bgt_thread: background thread description object
  423. * @thread_enabled: if the background thread is enabled
  424. * @bgt_name: background thread name
  425. *
  426. * @flash_size: underlying MTD device size (in bytes)
  427. * @peb_count: count of physical eraseblocks on the MTD device
  428. * @peb_size: physical eraseblock size
  429. * @bad_peb_limit: top limit of expected bad physical eraseblocks
  430. * @bad_peb_count: count of bad physical eraseblocks
  431. * @good_peb_count: count of good physical eraseblocks
  432. * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
  433. * used by UBI)
  434. * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
  435. * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
  436. * @min_io_size: minimal input/output unit size of the underlying MTD device
  437. * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
  438. * @ro_mode: if the UBI device is in read-only mode
  439. * @leb_size: logical eraseblock size
  440. * @leb_start: starting offset of logical eraseblocks within physical
  441. * eraseblocks
  442. * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
  443. * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
  444. * @vid_hdr_offset: starting offset of the volume identifier header (might be
  445. * unaligned)
  446. * @vid_hdr_aloffset: starting offset of the VID header aligned to
  447. * @hdrs_min_io_size
  448. * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
  449. * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
  450. * not
  451. * @nor_flash: non-zero if working on top of NOR flash
  452. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  453. * time (MTD write buffer size)
  454. * @mtd: MTD device descriptor
  455. *
  456. * @peb_buf: a buffer of PEB size used for different purposes
  457. * @buf_mutex: protects @peb_buf
  458. * @ckvol_mutex: serializes static volume checking when opening
  459. *
  460. * @dbg: debugging information for this UBI device
  461. */
  462. struct ubi_device {
  463. struct cdev cdev;
  464. struct device dev;
  465. int ubi_num;
  466. char ubi_name[sizeof(UBI_NAME_STR)+5];
  467. int vol_count;
  468. struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
  469. spinlock_t volumes_lock;
  470. int ref_count;
  471. int image_seq;
  472. int rsvd_pebs;
  473. int avail_pebs;
  474. int beb_rsvd_pebs;
  475. int beb_rsvd_level;
  476. int bad_peb_limit;
  477. int autoresize_vol_id;
  478. int vtbl_slots;
  479. int vtbl_size;
  480. struct ubi_vtbl_record *vtbl;
  481. struct mutex device_mutex;
  482. int max_ec;
  483. /* Note, mean_ec is not updated run-time - should be fixed */
  484. int mean_ec;
  485. /* EBA sub-system's stuff */
  486. unsigned long long global_sqnum;
  487. spinlock_t ltree_lock;
  488. struct rb_root ltree;
  489. struct mutex alc_mutex;
  490. /* Fastmap stuff */
  491. int fm_disabled;
  492. struct ubi_fastmap_layout *fm;
  493. struct ubi_fm_pool fm_pool;
  494. struct ubi_fm_pool fm_wl_pool;
  495. struct rw_semaphore fm_sem;
  496. struct mutex fm_mutex;
  497. void *fm_buf;
  498. size_t fm_size;
  499. struct work_struct fm_work;
  500. /* Wear-leveling sub-system's stuff */
  501. struct rb_root used;
  502. struct rb_root erroneous;
  503. struct rb_root free;
  504. int free_count;
  505. struct rb_root scrub;
  506. struct list_head pq[UBI_PROT_QUEUE_LEN];
  507. int pq_head;
  508. spinlock_t wl_lock;
  509. struct mutex move_mutex;
  510. struct rw_semaphore work_sem;
  511. int wl_scheduled;
  512. struct ubi_wl_entry **lookuptbl;
  513. struct ubi_wl_entry *move_from;
  514. struct ubi_wl_entry *move_to;
  515. int move_to_put;
  516. struct list_head works;
  517. int works_count;
  518. struct task_struct *bgt_thread;
  519. int thread_enabled;
  520. char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
  521. /* I/O sub-system's stuff */
  522. long long flash_size;
  523. int peb_count;
  524. int peb_size;
  525. int bad_peb_count;
  526. int good_peb_count;
  527. int corr_peb_count;
  528. int erroneous_peb_count;
  529. int max_erroneous;
  530. int min_io_size;
  531. int hdrs_min_io_size;
  532. int ro_mode;
  533. int leb_size;
  534. int leb_start;
  535. int ec_hdr_alsize;
  536. int vid_hdr_alsize;
  537. int vid_hdr_offset;
  538. int vid_hdr_aloffset;
  539. int vid_hdr_shift;
  540. unsigned int bad_allowed:1;
  541. unsigned int nor_flash:1;
  542. int max_write_size;
  543. struct mtd_info *mtd;
  544. void *peb_buf;
  545. struct mutex buf_mutex;
  546. struct mutex ckvol_mutex;
  547. struct ubi_debug_info dbg;
  548. };
  549. /**
  550. * struct ubi_ainf_peb - attach information about a physical eraseblock.
  551. * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
  552. * @pnum: physical eraseblock number
  553. * @vol_id: ID of the volume this LEB belongs to
  554. * @lnum: logical eraseblock number
  555. * @scrub: if this physical eraseblock needs scrubbing
  556. * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
  557. * @sqnum: sequence number
  558. * @u: unions RB-tree or @list links
  559. * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
  560. * @u.list: link in one of the eraseblock lists
  561. *
  562. * One object of this type is allocated for each physical eraseblock when
  563. * attaching an MTD device. Note, if this PEB does not belong to any LEB /
  564. * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
  565. */
  566. struct ubi_ainf_peb {
  567. int ec;
  568. int pnum;
  569. int vol_id;
  570. int lnum;
  571. unsigned int scrub:1;
  572. unsigned int copy_flag:1;
  573. unsigned long long sqnum;
  574. union {
  575. struct rb_node rb;
  576. struct list_head list;
  577. } u;
  578. };
  579. /**
  580. * struct ubi_ainf_volume - attaching information about a volume.
  581. * @vol_id: volume ID
  582. * @highest_lnum: highest logical eraseblock number in this volume
  583. * @leb_count: number of logical eraseblocks in this volume
  584. * @vol_type: volume type
  585. * @used_ebs: number of used logical eraseblocks in this volume (only for
  586. * static volumes)
  587. * @last_data_size: amount of data in the last logical eraseblock of this
  588. * volume (always equivalent to the usable logical eraseblock
  589. * size in case of dynamic volumes)
  590. * @data_pad: how many bytes at the end of logical eraseblocks of this volume
  591. * are not used (due to volume alignment)
  592. * @compat: compatibility flags of this volume
  593. * @rb: link in the volume RB-tree
  594. * @root: root of the RB-tree containing all the eraseblock belonging to this
  595. * volume (&struct ubi_ainf_peb objects)
  596. *
  597. * One object of this type is allocated for each volume when attaching an MTD
  598. * device.
  599. */
  600. struct ubi_ainf_volume {
  601. int vol_id;
  602. int highest_lnum;
  603. int leb_count;
  604. int vol_type;
  605. int used_ebs;
  606. int last_data_size;
  607. int data_pad;
  608. int compat;
  609. struct rb_node rb;
  610. struct rb_root root;
  611. };
  612. /**
  613. * struct ubi_attach_info - MTD device attaching information.
  614. * @volumes: root of the volume RB-tree
  615. * @corr: list of corrupted physical eraseblocks
  616. * @free: list of free physical eraseblocks
  617. * @erase: list of physical eraseblocks which have to be erased
  618. * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
  619. * those belonging to "preserve"-compatible internal volumes)
  620. * @corr_peb_count: count of PEBs in the @corr list
  621. * @empty_peb_count: count of PEBs which are presumably empty (contain only
  622. * 0xFF bytes)
  623. * @alien_peb_count: count of PEBs in the @alien list
  624. * @bad_peb_count: count of bad physical eraseblocks
  625. * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
  626. * as bad yet, but which look like bad
  627. * @vols_found: number of volumes found
  628. * @highest_vol_id: highest volume ID
  629. * @is_empty: flag indicating whether the MTD device is empty or not
  630. * @min_ec: lowest erase counter value
  631. * @max_ec: highest erase counter value
  632. * @max_sqnum: highest sequence number value
  633. * @mean_ec: mean erase counter value
  634. * @ec_sum: a temporary variable used when calculating @mean_ec
  635. * @ec_count: a temporary variable used when calculating @mean_ec
  636. * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
  637. *
  638. * This data structure contains the result of attaching an MTD device and may
  639. * be used by other UBI sub-systems to build final UBI data structures, further
  640. * error-recovery and so on.
  641. */
  642. struct ubi_attach_info {
  643. struct rb_root volumes;
  644. struct list_head corr;
  645. struct list_head free;
  646. struct list_head erase;
  647. struct list_head alien;
  648. int corr_peb_count;
  649. int empty_peb_count;
  650. int alien_peb_count;
  651. int bad_peb_count;
  652. int maybe_bad_peb_count;
  653. int vols_found;
  654. int highest_vol_id;
  655. int is_empty;
  656. int min_ec;
  657. int max_ec;
  658. unsigned long long max_sqnum;
  659. int mean_ec;
  660. uint64_t ec_sum;
  661. int ec_count;
  662. struct kmem_cache *aeb_slab_cache;
  663. };
  664. /**
  665. * struct ubi_work - UBI work description data structure.
  666. * @list: a link in the list of pending works
  667. * @func: worker function
  668. * @e: physical eraseblock to erase
  669. * @vol_id: the volume ID on which this erasure is being performed
  670. * @lnum: the logical eraseblock number
  671. * @torture: if the physical eraseblock has to be tortured
  672. * @anchor: produce a anchor PEB to by used by fastmap
  673. *
  674. * The @func pointer points to the worker function. If the @cancel argument is
  675. * not zero, the worker has to free the resources and exit immediately. The
  676. * worker has to return zero in case of success and a negative error code in
  677. * case of failure.
  678. */
  679. struct ubi_work {
  680. struct list_head list;
  681. int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
  682. /* The below fields are only relevant to erasure works */
  683. struct ubi_wl_entry *e;
  684. int vol_id;
  685. int lnum;
  686. int torture;
  687. int anchor;
  688. };
  689. #include "debug.h"
  690. extern struct kmem_cache *ubi_wl_entry_slab;
  691. extern const struct file_operations ubi_ctrl_cdev_operations;
  692. extern const struct file_operations ubi_cdev_operations;
  693. extern const struct file_operations ubi_vol_cdev_operations;
  694. extern struct class *ubi_class;
  695. extern struct mutex ubi_devices_mutex;
  696. extern struct blocking_notifier_head ubi_notifiers;
  697. /* attach.c */
  698. int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
  699. int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
  700. struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
  701. int vol_id);
  702. void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
  703. struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
  704. struct ubi_attach_info *ai);
  705. int ubi_attach(struct ubi_device *ubi, int force_scan);
  706. void ubi_destroy_ai(struct ubi_attach_info *ai);
  707. /* vtbl.c */
  708. int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
  709. struct ubi_vtbl_record *vtbl_rec);
  710. int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
  711. struct list_head *rename_list);
  712. int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
  713. /* vmt.c */
  714. int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
  715. int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
  716. int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
  717. int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
  718. int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  719. void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
  720. /* upd.c */
  721. int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
  722. long long bytes);
  723. int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
  724. const void __user *buf, int count);
  725. int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  726. const struct ubi_leb_change_req *req);
  727. int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
  728. const void __user *buf, int count);
  729. /* misc.c */
  730. int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
  731. int length);
  732. int ubi_check_volume(struct ubi_device *ubi, int vol_id);
  733. void ubi_update_reserved(struct ubi_device *ubi);
  734. void ubi_calculate_reserved(struct ubi_device *ubi);
  735. int ubi_check_pattern(const void *buf, uint8_t patt, int size);
  736. /* eba.c */
  737. int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
  738. int lnum);
  739. int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  740. void *buf, int offset, int len, int check);
  741. int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
  742. const void *buf, int offset, int len);
  743. int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
  744. int lnum, const void *buf, int len, int used_ebs);
  745. int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
  746. int lnum, const void *buf, int len);
  747. int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
  748. struct ubi_vid_hdr *vid_hdr);
  749. int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  750. unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
  751. int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
  752. struct ubi_attach_info *ai_scan);
  753. /* wl.c */
  754. int ubi_wl_get_peb(struct ubi_device *ubi);
  755. int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
  756. int pnum, int torture);
  757. int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
  758. int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
  759. int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
  760. void ubi_wl_close(struct ubi_device *ubi);
  761. int ubi_thread(void *u);
  762. struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
  763. int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
  764. int lnum, int torture);
  765. int ubi_is_erase_work(struct ubi_work *wrk);
  766. void ubi_refill_pools(struct ubi_device *ubi);
  767. int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
  768. /* io.c */
  769. int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
  770. int len);
  771. int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
  772. int len);
  773. int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
  774. int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
  775. int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
  776. int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
  777. struct ubi_ec_hdr *ec_hdr, int verbose);
  778. int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
  779. struct ubi_ec_hdr *ec_hdr);
  780. int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
  781. struct ubi_vid_hdr *vid_hdr, int verbose);
  782. int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
  783. struct ubi_vid_hdr *vid_hdr);
  784. /* build.c */
  785. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
  786. int vid_hdr_offset, int max_beb_per1024);
  787. int ubi_detach_mtd_dev(int ubi_num, int anyway);
  788. struct ubi_device *ubi_get_device(int ubi_num);
  789. void ubi_put_device(struct ubi_device *ubi);
  790. struct ubi_device *ubi_get_by_major(int major);
  791. int ubi_major2num(int major);
  792. int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
  793. int ntype);
  794. int ubi_notify_all(struct ubi_device *ubi, int ntype,
  795. struct notifier_block *nb);
  796. int ubi_enumerate_volumes(struct notifier_block *nb);
  797. void ubi_free_internal_volumes(struct ubi_device *ubi);
  798. /* kapi.c */
  799. void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
  800. void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
  801. struct ubi_volume_info *vi);
  802. /* scan.c */
  803. int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
  804. int pnum, const struct ubi_vid_hdr *vid_hdr);
  805. /* fastmap.c */
  806. size_t ubi_calc_fm_size(struct ubi_device *ubi);
  807. int ubi_update_fastmap(struct ubi_device *ubi);
  808. int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
  809. int fm_anchor);
  810. /* block.c */
  811. #ifdef CONFIG_MTD_UBI_BLOCK
  812. int ubiblock_init(void);
  813. void ubiblock_exit(void);
  814. int ubiblock_create(struct ubi_volume_info *vi);
  815. int ubiblock_remove(struct ubi_volume_info *vi);
  816. #else
  817. static inline int ubiblock_init(void) { return 0; }
  818. static inline void ubiblock_exit(void) {}
  819. static inline int ubiblock_create(struct ubi_volume_info *vi)
  820. {
  821. return -ENOSYS;
  822. }
  823. static inline int ubiblock_remove(struct ubi_volume_info *vi)
  824. {
  825. return -ENOSYS;
  826. }
  827. #endif
  828. /*
  829. * ubi_rb_for_each_entry - walk an RB-tree.
  830. * @rb: a pointer to type 'struct rb_node' to use as a loop counter
  831. * @pos: a pointer to RB-tree entry type to use as a loop counter
  832. * @root: RB-tree's root
  833. * @member: the name of the 'struct rb_node' within the RB-tree entry
  834. */
  835. #define ubi_rb_for_each_entry(rb, pos, root, member) \
  836. for (rb = rb_first(root), \
  837. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
  838. rb; \
  839. rb = rb_next(rb), \
  840. pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
  841. /*
  842. * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
  843. *
  844. * @av: volume attaching information
  845. * @aeb: attaching eraseblock information
  846. * @list: the list to move to
  847. */
  848. static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
  849. struct ubi_ainf_peb *aeb,
  850. struct list_head *list)
  851. {
  852. rb_erase(&aeb->u.rb, &av->root);
  853. list_add_tail(&aeb->u.list, list);
  854. }
  855. /**
  856. * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
  857. * @ubi: UBI device description object
  858. * @gfp_flags: GFP flags to allocate with
  859. *
  860. * This function returns a pointer to the newly allocated and zero-filled
  861. * volume identifier header object in case of success and %NULL in case of
  862. * failure.
  863. */
  864. static inline struct ubi_vid_hdr *
  865. ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
  866. {
  867. void *vid_hdr;
  868. vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
  869. if (!vid_hdr)
  870. return NULL;
  871. /*
  872. * VID headers may be stored at un-aligned flash offsets, so we shift
  873. * the pointer.
  874. */
  875. return vid_hdr + ubi->vid_hdr_shift;
  876. }
  877. /**
  878. * ubi_free_vid_hdr - free a volume identifier header object.
  879. * @ubi: UBI device description object
  880. * @vid_hdr: the object to free
  881. */
  882. static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
  883. struct ubi_vid_hdr *vid_hdr)
  884. {
  885. void *p = vid_hdr;
  886. if (!p)
  887. return;
  888. kfree(p - ubi->vid_hdr_shift);
  889. }
  890. /*
  891. * This function is equivalent to 'ubi_io_read()', but @offset is relative to
  892. * the beginning of the logical eraseblock, not to the beginning of the
  893. * physical eraseblock.
  894. */
  895. static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
  896. int pnum, int offset, int len)
  897. {
  898. ubi_assert(offset >= 0);
  899. return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
  900. }
  901. /*
  902. * This function is equivalent to 'ubi_io_write()', but @offset is relative to
  903. * the beginning of the logical eraseblock, not to the beginning of the
  904. * physical eraseblock.
  905. */
  906. static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
  907. int pnum, int offset, int len)
  908. {
  909. ubi_assert(offset >= 0);
  910. return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
  911. }
  912. /**
  913. * ubi_ro_mode - switch to read-only mode.
  914. * @ubi: UBI device description object
  915. */
  916. static inline void ubi_ro_mode(struct ubi_device *ubi)
  917. {
  918. if (!ubi->ro_mode) {
  919. ubi->ro_mode = 1;
  920. ubi_warn("switch to read-only mode");
  921. dump_stack();
  922. }
  923. }
  924. /**
  925. * vol_id2idx - get table index by volume ID.
  926. * @ubi: UBI device description object
  927. * @vol_id: volume ID
  928. */
  929. static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
  930. {
  931. if (vol_id >= UBI_INTERNAL_VOL_START)
  932. return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
  933. else
  934. return vol_id;
  935. }
  936. /**
  937. * idx2vol_id - get volume ID by table index.
  938. * @ubi: UBI device description object
  939. * @idx: table index
  940. */
  941. static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
  942. {
  943. if (idx >= ubi->vtbl_slots)
  944. return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
  945. else
  946. return idx;
  947. }
  948. #endif /* !__UBI_UBI_H__ */