2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2006, 2007
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.
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.
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
19 * Author: Artem Bityutskiy (Битюцкий Артём)
25 #include <linux/init.h>
26 #include <linux/types.h>
27 #include <linux/list.h>
28 #include <linux/rbtree.h>
29 #include <linux/sched.h>
30 #include <linux/wait.h>
31 #include <linux/mutex.h>
32 #include <linux/rwsem.h>
33 #include <linux/spinlock.h>
35 #include <linux/cdev.h>
36 #include <linux/device.h>
37 #include <linux/string.h>
38 #include <linux/mtd/mtd.h>
40 #include <mtd/ubi-header.h>
41 #include <linux/mtd/ubi.h>
46 /* Maximum number of supported UBI devices */
47 #define UBI_MAX_DEVICES 32
49 /* UBI name used for character devices, sysfs, etc */
50 #define UBI_NAME_STR "ubi"
52 /* Normal UBI messages */
53 #define ubi_msg(fmt, ...) printk(KERN_NOTICE "UBI: " fmt "\n", ##__VA_ARGS__)
54 /* UBI warning messages */
55 #define ubi_warn(fmt, ...) printk(KERN_WARNING "UBI warning: %s: " fmt "\n", \
56 __FUNCTION__, ##__VA_ARGS__)
57 /* UBI error messages */
58 #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
59 __FUNCTION__, ##__VA_ARGS__)
61 /* Lowest number PEBs reserved for bad PEB handling */
62 #define MIN_RESEVED_PEBS 2
64 /* Background thread name pattern */
65 #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
67 /* This marker in the EBA table means that the LEB is um-mapped */
68 #define UBI_LEB_UNMAPPED -1
71 * In case of errors, UBI tries to repeat the operation several times before
72 * returning error. The below constant defines how many times UBI re-tries.
74 #define UBI_IO_RETRIES 3
77 * Error codes returned by the I/O unit.
79 * UBI_IO_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only
81 * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a
82 * valid erase counter header, and the rest are %0xFF bytes
83 * UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC)
84 * UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or
86 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
96 extern int ubi_devices_cnt
;
97 extern struct ubi_device
*ubi_devices
[];
99 struct ubi_volume_desc
;
102 * struct ubi_volume - UBI volume description data structure.
103 * @dev: device object to make use of the the Linux device model
104 * @cdev: character device object to create character device
105 * @ubi: reference to the UBI device description object
107 * @readers: number of users holding this volume in read-only mode
108 * @writers: number of users holding this volume in read-write mode
109 * @exclusive: whether somebody holds this volume in exclusive mode
110 * @removed: if the volume was removed
111 * @checked: if this static volume was checked
113 * @reserved_pebs: how many physical eraseblocks are reserved for this volume
114 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
115 * @usable_leb_size: logical eraseblock size without padding
116 * @used_ebs: how many logical eraseblocks in this volume contain data
117 * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
118 * @used_bytes: how many bytes of data this volume contains
119 * @upd_marker: non-zero if the update marker is set for this volume
120 * @corrupted: non-zero if the volume is corrupted (static volumes only)
121 * @alignment: volume alignment
122 * @data_pad: how many bytes are not used at the end of physical eraseblocks to
123 * satisfy the requested alignment
124 * @name_len: volume name length
127 * @updating: whether the volume is being updated
128 * @upd_ebs: how many eraseblocks are expected to be updated
129 * @upd_bytes: how many bytes are expected to be received
130 * @upd_received: how many update bytes were already received
131 * @upd_buf: update buffer which is used to collect update data
133 * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
135 * @gluebi_desc: gluebi UBI volume descriptor
136 * @gluebi_refcount: reference count of the gluebi MTD device
137 * @gluebi_mtd: MTD device description object of the gluebi MTD device
139 * The @corrupted field indicates that the volume's contents is corrupted.
140 * Since UBI protects only static volumes, this field is not relevant to
141 * dynamic volumes - it is user's responsibility to assure their data
144 * The @upd_marker flag indicates that this volume is either being updated at
145 * the moment or is damaged because of an unclean reboot.
150 struct ubi_device
*ubi
;
163 long long used_bytes
;
169 char name
[UBI_VOL_NAME_MAX
+1];
174 long long upd_received
;
179 #ifdef CONFIG_MTD_UBI_GLUEBI
180 /* Gluebi-related stuff may be compiled out */
181 struct ubi_volume_desc
*gluebi_desc
;
183 struct mtd_info gluebi_mtd
;
188 * struct ubi_volume_desc - descriptor of the UBI volume returned when it is
190 * @vol: reference to the corresponding volume description object
191 * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
193 struct ubi_volume_desc
{
194 struct ubi_volume
*vol
;
201 * struct ubi_device - UBI device description structure
202 * @dev: class device object to use the the Linux device model
203 * @cdev: character device object to create character device
204 * @ubi_num: UBI device number
205 * @ubi_name: UBI device name
206 * @major: character device major number
207 * @vol_count: number of volumes in this UBI device
208 * @volumes: volumes of this UBI device
209 * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
210 * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count, @vol->readers,
211 * @vol->writers, @vol->exclusive, @vol->removed, @vol->mapping and
214 * @rsvd_pebs: count of reserved physical eraseblocks
215 * @avail_pebs: count of available physical eraseblocks
216 * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
218 * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
220 * @vtbl_slots: how many slots are available in the volume table
221 * @vtbl_size: size of the volume table in bytes
222 * @vtbl: in-RAM volume table copy
224 * @max_ec: current highest erase counter value
225 * @mean_ec: current mean erase counter value
227 * global_sqnum: global sequence number
228 * @ltree_lock: protects the lock tree and @global_sqnum
229 * @ltree: the lock tree
230 * @vtbl_mutex: protects on-flash volume table
232 * @used: RB-tree of used physical eraseblocks
233 * @free: RB-tree of free physical eraseblocks
234 * @scrub: RB-tree of physical eraseblocks which need scrubbing
235 * @prot: protection trees
236 * @prot.pnum: protection tree indexed by physical eraseblock numbers
237 * @prot.aec: protection tree indexed by absolute erase counter value
238 * @wl_lock: protects the @used, @free, @prot, @lookuptbl, @abs_ec, @move_from,
239 * @move_to, @move_to_put @erase_pending, @wl_scheduled, and @works
241 * @wl_scheduled: non-zero if the wear-leveling was scheduled
242 * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
243 * physical eraseblock
244 * @abs_ec: absolute erase counter
245 * @move_from: physical eraseblock from where the data is being moved
246 * @move_to: physical eraseblock where the data is being moved to
247 * @move_from_put: if the "from" PEB was put
248 * @move_to_put: if the "to" PEB was put
249 * @works: list of pending works
250 * @works_count: count of pending works
251 * @bgt_thread: background thread description object
252 * @thread_enabled: if the background thread is enabled
253 * @bgt_name: background thread name
255 * @flash_size: underlying MTD device size (in bytes)
256 * @peb_count: count of physical eraseblocks on the MTD device
257 * @peb_size: physical eraseblock size
258 * @bad_peb_count: count of bad physical eraseblocks
259 * @good_peb_count: count of good physical eraseblocks
260 * @min_io_size: minimal input/output unit size of the underlying MTD device
261 * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
262 * @ro_mode: if the UBI device is in read-only mode
263 * @leb_size: logical eraseblock size
264 * @leb_start: starting offset of logical eraseblocks within physical
266 * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
267 * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
268 * @vid_hdr_offset: starting offset of the volume identifier header (might be
270 * @vid_hdr_aloffset: starting offset of the VID header aligned to
272 * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
273 * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
275 * @mtd: MTD device descriptor
281 char ubi_name
[sizeof(UBI_NAME_STR
)+5];
284 struct ubi_volume
*volumes
[UBI_MAX_VOLUMES
+UBI_INT_VOL_COUNT
];
285 spinlock_t volumes_lock
;
294 struct ubi_vtbl_record
*vtbl
;
295 struct mutex vtbl_mutex
;
300 /* EBA unit's stuff */
301 unsigned long long global_sqnum
;
302 spinlock_t ltree_lock
;
303 struct rb_root ltree
;
305 /* Wear-leveling unit's stuff */
308 struct rb_root scrub
;
315 struct ubi_wl_entry
**lookuptbl
;
316 unsigned long long abs_ec
;
317 struct ubi_wl_entry
*move_from
;
318 struct ubi_wl_entry
*move_to
;
321 struct list_head works
;
323 struct task_struct
*bgt_thread
;
325 char bgt_name
[sizeof(UBI_BGT_NAME_PATTERN
)+2];
327 /* I/O unit's stuff */
328 long long flash_size
;
334 int hdrs_min_io_size
;
341 int vid_hdr_aloffset
;
344 struct mtd_info
*mtd
;
347 extern struct file_operations ubi_cdev_operations
;
348 extern struct file_operations ubi_vol_cdev_operations
;
349 extern struct class *ubi_class
;
352 int ubi_change_vtbl_record(struct ubi_device
*ubi
, int idx
,
353 struct ubi_vtbl_record
*vtbl_rec
);
354 int ubi_read_volume_table(struct ubi_device
*ubi
, struct ubi_scan_info
*si
);
357 int ubi_create_volume(struct ubi_device
*ubi
, struct ubi_mkvol_req
*req
);
358 int ubi_remove_volume(struct ubi_volume_desc
*desc
);
359 int ubi_resize_volume(struct ubi_volume_desc
*desc
, int reserved_pebs
);
360 int ubi_add_volume(struct ubi_device
*ubi
, int vol_id
);
361 void ubi_free_volume(struct ubi_device
*ubi
, int vol_id
);
364 int ubi_start_update(struct ubi_device
*ubi
, int vol_id
, long long bytes
);
365 int ubi_more_update_data(struct ubi_device
*ubi
, int vol_id
,
366 const void __user
*buf
, int count
);
369 int ubi_calc_data_len(const struct ubi_device
*ubi
, const void *buf
, int length
);
370 int ubi_check_volume(struct ubi_device
*ubi
, int vol_id
);
371 void ubi_calculate_reserved(struct ubi_device
*ubi
);
374 #ifdef CONFIG_MTD_UBI_GLUEBI
375 int ubi_create_gluebi(struct ubi_device
*ubi
, struct ubi_volume
*vol
);
376 int ubi_destroy_gluebi(struct ubi_volume
*vol
);
378 #define ubi_create_gluebi(ubi, vol) 0
379 #define ubi_destroy_gluebi(vol) 0
383 int ubi_eba_unmap_leb(struct ubi_device
*ubi
, int vol_id
, int lnum
);
384 int ubi_eba_read_leb(struct ubi_device
*ubi
, int vol_id
, int lnum
, void *buf
,
385 int offset
, int len
, int check
);
386 int ubi_eba_write_leb(struct ubi_device
*ubi
, int vol_id
, int lnum
,
387 const void *buf
, int offset
, int len
, int dtype
);
388 int ubi_eba_write_leb_st(struct ubi_device
*ubi
, int vol_id
, int lnum
,
389 const void *buf
, int len
, int dtype
,
391 int ubi_eba_atomic_leb_change(struct ubi_device
*ubi
, int vol_id
, int lnum
,
392 const void *buf
, int len
, int dtype
);
393 int ubi_eba_copy_leb(struct ubi_device
*ubi
, int from
, int to
,
394 struct ubi_vid_hdr
*vid_hdr
);
395 int ubi_eba_init_scan(struct ubi_device
*ubi
, struct ubi_scan_info
*si
);
396 void ubi_eba_close(const struct ubi_device
*ubi
);
399 int ubi_wl_get_peb(struct ubi_device
*ubi
, int dtype
);
400 int ubi_wl_put_peb(struct ubi_device
*ubi
, int pnum
, int torture
);
401 int ubi_wl_flush(struct ubi_device
*ubi
);
402 int ubi_wl_scrub_peb(struct ubi_device
*ubi
, int pnum
);
403 int ubi_wl_init_scan(struct ubi_device
*ubi
, struct ubi_scan_info
*si
);
404 void ubi_wl_close(struct ubi_device
*ubi
);
407 int ubi_io_read(const struct ubi_device
*ubi
, void *buf
, int pnum
, int offset
,
409 int ubi_io_write(const struct ubi_device
*ubi
, const void *buf
, int pnum
,
410 int offset
, int len
);
411 int ubi_io_sync_erase(const struct ubi_device
*ubi
, int pnum
, int torture
);
412 int ubi_io_is_bad(const struct ubi_device
*ubi
, int pnum
);
413 int ubi_io_mark_bad(const struct ubi_device
*ubi
, int pnum
);
414 int ubi_io_read_ec_hdr(const struct ubi_device
*ubi
, int pnum
,
415 struct ubi_ec_hdr
*ec_hdr
, int verbose
);
416 int ubi_io_write_ec_hdr(const struct ubi_device
*ubi
, int pnum
,
417 struct ubi_ec_hdr
*ec_hdr
);
418 int ubi_io_read_vid_hdr(const struct ubi_device
*ubi
, int pnum
,
419 struct ubi_vid_hdr
*vid_hdr
, int verbose
);
420 int ubi_io_write_vid_hdr(const struct ubi_device
*ubi
, int pnum
,
421 struct ubi_vid_hdr
*vid_hdr
);
424 * ubi_rb_for_each_entry - walk an RB-tree.
425 * @rb: a pointer to type 'struct rb_node' to to use as a loop counter
426 * @pos: a pointer to RB-tree entry type to use as a loop counter
427 * @root: RB-tree's root
428 * @member: the name of the 'struct rb_node' within the RB-tree entry
430 #define ubi_rb_for_each_entry(rb, pos, root, member) \
431 for (rb = rb_first(root), \
432 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
434 rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
437 * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
438 * @ubi: UBI device description object
440 * This function returns a pointer to the newly allocated and zero-filled
441 * volume identifier header object in case of success and %NULL in case of
444 static inline struct ubi_vid_hdr
*ubi_zalloc_vid_hdr(const struct ubi_device
*ubi
)
448 vid_hdr
= kzalloc(ubi
->vid_hdr_alsize
, GFP_KERNEL
);
453 * VID headers may be stored at un-aligned flash offsets, so we shift
456 return vid_hdr
+ ubi
->vid_hdr_shift
;
460 * ubi_free_vid_hdr - free a volume identifier header object.
461 * @ubi: UBI device description object
462 * @vid_hdr: the object to free
464 static inline void ubi_free_vid_hdr(const struct ubi_device
*ubi
,
465 struct ubi_vid_hdr
*vid_hdr
)
472 kfree(p
- ubi
->vid_hdr_shift
);
476 * This function is equivalent to 'ubi_io_read()', but @offset is relative to
477 * the beginning of the logical eraseblock, not to the beginning of the
478 * physical eraseblock.
480 static inline int ubi_io_read_data(const struct ubi_device
*ubi
, void *buf
,
481 int pnum
, int offset
, int len
)
483 ubi_assert(offset
>= 0);
484 return ubi_io_read(ubi
, buf
, pnum
, offset
+ ubi
->leb_start
, len
);
488 * This function is equivalent to 'ubi_io_write()', but @offset is relative to
489 * the beginning of the logical eraseblock, not to the beginning of the
490 * physical eraseblock.
492 static inline int ubi_io_write_data(const struct ubi_device
*ubi
, const void *buf
,
493 int pnum
, int offset
, int len
)
495 ubi_assert(offset
>= 0);
496 return ubi_io_write(ubi
, buf
, pnum
, offset
+ ubi
->leb_start
, len
);
500 * ubi_ro_mode - switch to read-only mode.
501 * @ubi: UBI device description object
503 static inline void ubi_ro_mode(struct ubi_device
*ubi
)
506 ubi_warn("switch to read-only mode");
510 * vol_id2idx - get table index by volume ID.
511 * @ubi: UBI device description object
514 static inline int vol_id2idx(const struct ubi_device
*ubi
, int vol_id
)
516 if (vol_id
>= UBI_INTERNAL_VOL_START
)
517 return vol_id
- UBI_INTERNAL_VOL_START
+ ubi
->vtbl_slots
;
523 * idx2vol_id - get volume ID by table index.
524 * @ubi: UBI device description object
527 static inline int idx2vol_id(const struct ubi_device
*ubi
, int idx
)
529 if (idx
>= ubi
->vtbl_slots
)
530 return idx
- ubi
->vtbl_slots
+ UBI_INTERNAL_VOL_START
;
535 #endif /* !__UBI_UBI_H__ */