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