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/vmalloc.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/ubi.h>
42 #include "ubi-media.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 __func__, ##__VA_ARGS__)
57 /* UBI error messages */
58 #define ubi_err(fmt, ...) printk(KERN_ERR "UBI error: %s: " fmt "\n", \
59 __func__, ##__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 * Length of the protection queue. The length is effectively equivalent to the
78 * number of (global) erase cycles PEBs are protected from the wear-leveling
81 #define UBI_PROT_QUEUE_LEN 10
84 * Error codes returned by the I/O sub-system.
86 * UBI_IO_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only
88 * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a
89 * valid erase counter header, and the rest are %0xFF bytes
90 * UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC)
91 * UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or
93 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
104 * Return codes of the 'ubi_eba_copy_leb()' function.
106 * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
107 * PEB was put meanwhile, or there is I/O on the source PEB
108 * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
110 * MOVE_CANCEL_BITFLIPS: canceled because a bit-flip was detected in the
114 MOVE_CANCEL_RACE = 1,
116 MOVE_CANCEL_BITFLIPS,
120 * struct ubi_wl_entry - wear-leveling entry.
121 * @u.rb: link in the corresponding (free/used) RB-tree
122 * @u.list: link in the protection queue
124 * @pnum: physical eraseblock number
126 * This data structure is used in the WL sub-system. Each physical eraseblock
127 * has a corresponding &struct wl_entry object which may be kept in different
128 * RB-trees. See WL sub-system for details.
130 struct ubi_wl_entry {
133 struct list_head list;
140 * struct ubi_ltree_entry - an entry in the lock tree.
141 * @rb: links RB-tree nodes
142 * @vol_id: volume ID of the locked logical eraseblock
143 * @lnum: locked logical eraseblock number
144 * @users: how many tasks are using this logical eraseblock or wait for it
145 * @mutex: read/write mutex to implement read/write access serialization to
146 * the (@vol_id, @lnum) logical eraseblock
148 * This data structure is used in the EBA sub-system to implement per-LEB
149 * locking. When a logical eraseblock is being locked - corresponding
150 * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
151 * See EBA sub-system for details.
153 struct ubi_ltree_entry {
158 struct rw_semaphore mutex;
162 * struct ubi_rename_entry - volume re-name description data structure.
163 * @new_name_len: new volume name length
164 * @new_name: new volume name
165 * @remove: if not zero, this volume should be removed, not re-named
166 * @desc: descriptor of the volume
167 * @list: links re-name entries into a list
169 * This data structure is utilized in the multiple volume re-name code. Namely,
170 * UBI first creates a list of &struct ubi_rename_entry objects from the
171 * &struct ubi_rnvol_req request object, and then utilizes this list to do all
174 struct ubi_rename_entry {
176 char new_name[UBI_VOL_NAME_MAX + 1];
178 struct ubi_volume_desc *desc;
179 struct list_head list;
182 struct ubi_volume_desc;
185 * struct ubi_volume - UBI volume description data structure.
186 * @dev: device object to make use of the the Linux device model
187 * @cdev: character device object to create character device
188 * @ubi: reference to the UBI device description object
190 * @ref_count: volume reference count
191 * @readers: number of users holding this volume in read-only mode
192 * @writers: number of users holding this volume in read-write mode
193 * @exclusive: whether somebody holds this volume in exclusive mode
195 * @reserved_pebs: how many physical eraseblocks are reserved for this volume
196 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
197 * @usable_leb_size: logical eraseblock size without padding
198 * @used_ebs: how many logical eraseblocks in this volume contain data
199 * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
200 * @used_bytes: how many bytes of data this volume contains
201 * @alignment: volume alignment
202 * @data_pad: how many bytes are not used at the end of physical eraseblocks to
203 * satisfy the requested alignment
204 * @name_len: volume name length
207 * @upd_ebs: how many eraseblocks are expected to be updated
208 * @ch_lnum: LEB number which is being changing by the atomic LEB change
210 * @ch_dtype: data persistency type which is being changing by the atomic LEB
212 * @upd_bytes: how many bytes are expected to be received for volume update or
214 * @upd_received: how many bytes were already received for volume update or
216 * @upd_buf: update buffer which is used to collect update data or data for
219 * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
220 * @checked: %1 if this static volume was checked
221 * @corrupted: %1 if the volume is corrupted (static volumes only)
222 * @upd_marker: %1 if the update marker is set for this volume
223 * @updating: %1 if the volume is being updated
224 * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
225 * @direct_writes: %1 if direct writes are enabled for this volume
227 * @gluebi_desc: gluebi UBI volume descriptor
228 * @gluebi_refcount: reference count of the gluebi MTD device
229 * @gluebi_mtd: MTD device description object of the gluebi MTD device
231 * The @corrupted field indicates that the volume's contents is corrupted.
232 * Since UBI protects only static volumes, this field is not relevant to
233 * dynamic volumes - it is user's responsibility to assure their data
236 * The @upd_marker flag indicates that this volume is either being updated at
237 * the moment or is damaged because of an unclean reboot.
242 struct ubi_device *ubi;
254 long long used_bytes;
258 char name[UBI_VOL_NAME_MAX + 1];
264 long long upd_received;
268 unsigned int checked:1;
269 unsigned int corrupted:1;
270 unsigned int upd_marker:1;
271 unsigned int updating:1;
272 unsigned int changing_leb:1;
273 unsigned int direct_writes:1;
275 #ifdef CONFIG_MTD_UBI_GLUEBI
277 * Gluebi-related stuff may be compiled out.
278 * Note: this should not be built into UBI but should be a separate
279 * ubimtd driver which works on top of UBI and emulates MTD devices.
281 struct ubi_volume_desc *gluebi_desc;
283 struct mtd_info gluebi_mtd;
288 * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
289 * @vol: reference to the corresponding volume description object
290 * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, or %UBI_EXCLUSIVE)
292 struct ubi_volume_desc {
293 struct ubi_volume *vol;
300 * struct ubi_device - UBI device description structure
301 * @dev: UBI device object to use the the Linux device model
302 * @cdev: character device object to create character device
303 * @ubi_num: UBI device number
304 * @ubi_name: UBI device name
305 * @vol_count: number of volumes in this UBI device
306 * @volumes: volumes of this UBI device
307 * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
308 * @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
309 * @vol->readers, @vol->writers, @vol->exclusive,
310 * @vol->ref_count, @vol->mapping and @vol->eba_tbl.
311 * @ref_count: count of references on the UBI device
313 * @rsvd_pebs: count of reserved physical eraseblocks
314 * @avail_pebs: count of available physical eraseblocks
315 * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
317 * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
319 * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
320 * of UBI initialization
321 * @vtbl_slots: how many slots are available in the volume table
322 * @vtbl_size: size of the volume table in bytes
323 * @vtbl: in-RAM volume table copy
324 * @device_mutex: protects on-flash volume table and serializes volume
325 * creation, deletion, update, re-size, re-name and set
328 * @max_ec: current highest erase counter value
329 * @mean_ec: current mean erase counter value
331 * @global_sqnum: global sequence number
332 * @ltree_lock: protects the lock tree and @global_sqnum
333 * @ltree: the lock tree
334 * @alc_mutex: serializes "atomic LEB change" operations
336 * @used: RB-tree of used physical eraseblocks
337 * @free: RB-tree of free physical eraseblocks
338 * @scrub: RB-tree of physical eraseblocks which need scrubbing
339 * @pq: protection queue (contain physical eraseblocks which are temporarily
340 * protected from the wear-leveling worker)
341 * @pq_head: protection queue head
342 * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
343 * @move_to, @move_to_put @erase_pending, @wl_scheduled and @works
345 * @move_mutex: serializes eraseblock moves
346 * @work_sem: synchronizes the WL worker with use tasks
347 * @wl_scheduled: non-zero if the wear-leveling was scheduled
348 * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
349 * physical eraseblock
350 * @move_from: physical eraseblock from where the data is being moved
351 * @move_to: physical eraseblock where the data is being moved to
352 * @move_to_put: if the "to" PEB was put
353 * @works: list of pending works
354 * @works_count: count of pending works
355 * @bgt_thread: background thread description object
356 * @thread_enabled: if the background thread is enabled
357 * @bgt_name: background thread name
359 * @flash_size: underlying MTD device size (in bytes)
360 * @peb_count: count of physical eraseblocks on the MTD device
361 * @peb_size: physical eraseblock size
362 * @bad_peb_count: count of bad physical eraseblocks
363 * @good_peb_count: count of good physical eraseblocks
364 * @min_io_size: minimal input/output unit size of the underlying MTD device
365 * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
366 * @ro_mode: if the UBI device is in read-only mode
367 * @leb_size: logical eraseblock size
368 * @leb_start: starting offset of logical eraseblocks within physical
370 * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
371 * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
372 * @vid_hdr_offset: starting offset of the volume identifier header (might be
374 * @vid_hdr_aloffset: starting offset of the VID header aligned to
376 * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
377 * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
379 * @mtd: MTD device descriptor
381 * @peb_buf1: a buffer of PEB size used for different purposes
382 * @peb_buf2: another buffer of PEB size used for different purposes
383 * @buf_mutex: protects @peb_buf1 and @peb_buf2
384 * @ckvol_mutex: serializes static volume checking when opening
385 * @dbg_peb_buf: buffer of PEB size used for debugging
386 * @dbg_buf_mutex: protects @dbg_peb_buf
392 char ubi_name[sizeof(UBI_NAME_STR)+5];
394 struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
395 spinlock_t volumes_lock;
403 int autoresize_vol_id;
406 struct ubi_vtbl_record *vtbl;
407 struct mutex device_mutex;
410 /* Note, mean_ec is not updated run-time - should be fixed */
413 /* EBA sub-system's stuff */
414 unsigned long long global_sqnum;
415 spinlock_t ltree_lock;
416 struct rb_root ltree;
417 struct mutex alc_mutex;
419 /* Wear-leveling sub-system's stuff */
422 struct rb_root scrub;
423 struct list_head pq[UBI_PROT_QUEUE_LEN];
426 struct mutex move_mutex;
427 struct rw_semaphore work_sem;
429 struct ubi_wl_entry **lookuptbl;
430 struct ubi_wl_entry *move_from;
431 struct ubi_wl_entry *move_to;
433 struct list_head works;
435 struct task_struct *bgt_thread;
437 char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
439 /* I/O sub-system's stuff */
440 long long flash_size;
446 int hdrs_min_io_size;
453 int vid_hdr_aloffset;
456 struct mtd_info *mtd;
460 struct mutex buf_mutex;
461 struct mutex ckvol_mutex;
462 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
464 struct mutex dbg_buf_mutex;
468 extern struct kmem_cache *ubi_wl_entry_slab;
469 extern const struct file_operations ubi_ctrl_cdev_operations;
470 extern const struct file_operations ubi_cdev_operations;
471 extern const struct file_operations ubi_vol_cdev_operations;
472 extern struct class *ubi_class;
473 extern struct mutex ubi_devices_mutex;
476 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
477 struct ubi_vtbl_record *vtbl_rec);
478 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
479 struct list_head *rename_list);
480 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si);
483 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
484 int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
485 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
486 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
487 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
488 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
491 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
493 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
494 const void __user *buf, int count);
495 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
496 const struct ubi_leb_change_req *req);
497 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
498 const void __user *buf, int count);
501 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
503 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
504 void ubi_calculate_reserved(struct ubi_device *ubi);
507 #ifdef CONFIG_MTD_UBI_GLUEBI
508 int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
509 int ubi_destroy_gluebi(struct ubi_volume *vol);
510 void ubi_gluebi_updated(struct ubi_volume *vol);
512 #define ubi_create_gluebi(ubi, vol) 0
513 #define ubi_destroy_gluebi(vol) 0
514 #define ubi_gluebi_updated(vol)
518 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
520 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
521 void *buf, int offset, int len, int check);
522 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
523 const void *buf, int offset, int len, int dtype);
524 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
525 int lnum, const void *buf, int len, int dtype,
527 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
528 int lnum, const void *buf, int len, int dtype);
529 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
530 struct ubi_vid_hdr *vid_hdr);
531 int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
534 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype);
535 int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture);
536 int ubi_wl_flush(struct ubi_device *ubi);
537 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
538 int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si);
539 void ubi_wl_close(struct ubi_device *ubi);
540 int ubi_thread(void *u);
543 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
545 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
547 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
548 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
549 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
550 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
551 struct ubi_ec_hdr *ec_hdr, int verbose);
552 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
553 struct ubi_ec_hdr *ec_hdr);
554 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
555 struct ubi_vid_hdr *vid_hdr, int verbose);
556 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
557 struct ubi_vid_hdr *vid_hdr);
560 int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset);
561 int ubi_detach_mtd_dev(int ubi_num, int anyway);
562 struct ubi_device *ubi_get_device(int ubi_num);
563 void ubi_put_device(struct ubi_device *ubi);
564 struct ubi_device *ubi_get_by_major(int major);
565 int ubi_major2num(int major);
568 * ubi_rb_for_each_entry - walk an RB-tree.
569 * @rb: a pointer to type 'struct rb_node' to to use as a loop counter
570 * @pos: a pointer to RB-tree entry type to use as a loop counter
571 * @root: RB-tree's root
572 * @member: the name of the 'struct rb_node' within the RB-tree entry
574 #define ubi_rb_for_each_entry(rb, pos, root, member) \
575 for (rb = rb_first(root), \
576 pos = (rb ? container_of(rb, typeof(*pos), member) : NULL); \
578 rb = rb_next(rb), pos = container_of(rb, typeof(*pos), member))
581 * ubi_zalloc_vid_hdr - allocate a volume identifier header object.
582 * @ubi: UBI device description object
583 * @gfp_flags: GFP flags to allocate with
585 * This function returns a pointer to the newly allocated and zero-filled
586 * volume identifier header object in case of success and %NULL in case of
589 static inline struct ubi_vid_hdr *
590 ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags)
594 vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags);
599 * VID headers may be stored at un-aligned flash offsets, so we shift
602 return vid_hdr + ubi->vid_hdr_shift;
606 * ubi_free_vid_hdr - free a volume identifier header object.
607 * @ubi: UBI device description object
608 * @vid_hdr: the object to free
610 static inline void ubi_free_vid_hdr(const struct ubi_device *ubi,
611 struct ubi_vid_hdr *vid_hdr)
618 kfree(p - ubi->vid_hdr_shift);
622 * This function is equivalent to 'ubi_io_read()', but @offset is relative to
623 * the beginning of the logical eraseblock, not to the beginning of the
624 * physical eraseblock.
626 static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
627 int pnum, int offset, int len)
629 ubi_assert(offset >= 0);
630 return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
634 * This function is equivalent to 'ubi_io_write()', but @offset is relative to
635 * the beginning of the logical eraseblock, not to the beginning of the
636 * physical eraseblock.
638 static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
639 int pnum, int offset, int len)
641 ubi_assert(offset >= 0);
642 return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
646 * ubi_ro_mode - switch to read-only mode.
647 * @ubi: UBI device description object
649 static inline void ubi_ro_mode(struct ubi_device *ubi)
653 ubi_warn("switch to read-only mode");
658 * vol_id2idx - get table index by volume ID.
659 * @ubi: UBI device description object
662 static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
664 if (vol_id >= UBI_INTERNAL_VOL_START)
665 return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
671 * idx2vol_id - get volume ID by table index.
672 * @ubi: UBI device description object
675 static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
677 if (idx >= ubi->vtbl_slots)
678 return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
683 #endif /* !__UBI_UBI_H__ */