xref: /openbmc/linux/drivers/mtd/ubi/ubi.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
11a59d1b8SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-or-later */
2801c135cSArtem B. Bityutskiy /*
3801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
4801c135cSArtem B. Bityutskiy  * Copyright (c) Nokia Corporation, 2006, 2007
5801c135cSArtem B. Bityutskiy  *
6801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
7801c135cSArtem B. Bityutskiy  */
8801c135cSArtem B. Bityutskiy 
9801c135cSArtem B. Bityutskiy #ifndef __UBI_UBI_H__
10801c135cSArtem B. Bityutskiy #define __UBI_UBI_H__
11801c135cSArtem B. Bityutskiy 
12801c135cSArtem B. Bityutskiy #include <linux/types.h>
13801c135cSArtem B. Bityutskiy #include <linux/list.h>
14801c135cSArtem B. Bityutskiy #include <linux/rbtree.h>
15801c135cSArtem B. Bityutskiy #include <linux/sched.h>
16801c135cSArtem B. Bityutskiy #include <linux/wait.h>
17801c135cSArtem B. Bityutskiy #include <linux/mutex.h>
18801c135cSArtem B. Bityutskiy #include <linux/rwsem.h>
19801c135cSArtem B. Bityutskiy #include <linux/spinlock.h>
20801c135cSArtem B. Bityutskiy #include <linux/fs.h>
21801c135cSArtem B. Bityutskiy #include <linux/cdev.h>
22801c135cSArtem B. Bityutskiy #include <linux/device.h>
235a0e3ad6STejun Heo #include <linux/slab.h>
24801c135cSArtem B. Bityutskiy #include <linux/string.h>
2592ad8f37SArtem Bityutskiy #include <linux/vmalloc.h>
26d9dd0887SKevin Cernekee #include <linux/notifier.h>
27801c135cSArtem B. Bityutskiy #include <linux/mtd/mtd.h>
28801c135cSArtem B. Bityutskiy #include <linux/mtd/ubi.h>
29ca5999fdSMike Rapoport #include <linux/pgtable.h>
30801c135cSArtem B. Bityutskiy 
3192a74f1cSArtem Bityutskiy #include "ubi-media.h"
32801c135cSArtem B. Bityutskiy 
33801c135cSArtem B. Bityutskiy /* Maximum number of supported UBI devices */
34801c135cSArtem B. Bityutskiy #define UBI_MAX_DEVICES 32
35801c135cSArtem B. Bityutskiy 
36801c135cSArtem B. Bityutskiy /* UBI name used for character devices, sysfs, etc */
37801c135cSArtem B. Bityutskiy #define UBI_NAME_STR "ubi"
38801c135cSArtem B. Bityutskiy 
3958d303deSJoe Perches struct ubi_device;
4058d303deSJoe Perches 
41801c135cSArtem B. Bityutskiy /* Normal UBI messages */
4258d303deSJoe Perches __printf(2, 3)
4358d303deSJoe Perches void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
4458d303deSJoe Perches 
45801c135cSArtem B. Bityutskiy /* UBI warning messages */
4658d303deSJoe Perches __printf(2, 3)
4758d303deSJoe Perches void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
4858d303deSJoe Perches 
49801c135cSArtem B. Bityutskiy /* UBI error messages */
5058d303deSJoe Perches __printf(2, 3)
5158d303deSJoe Perches void ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
52801c135cSArtem B. Bityutskiy 
53801c135cSArtem B. Bityutskiy /* Background thread name pattern */
54801c135cSArtem B. Bityutskiy #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
55801c135cSArtem B. Bityutskiy 
5605a3cb7dSArtem Bityutskiy /*
5705a3cb7dSArtem Bityutskiy  * This marker in the EBA table means that the LEB is um-mapped.
5805a3cb7dSArtem Bityutskiy  * NOTE! It has to have the same value as %UBI_ALL.
5905a3cb7dSArtem Bityutskiy  */
60801c135cSArtem B. Bityutskiy #define UBI_LEB_UNMAPPED -1
61801c135cSArtem B. Bityutskiy 
62801c135cSArtem B. Bityutskiy /*
63801c135cSArtem B. Bityutskiy  * In case of errors, UBI tries to repeat the operation several times before
64801c135cSArtem B. Bityutskiy  * returning error. The below constant defines how many times UBI re-tries.
65801c135cSArtem B. Bityutskiy  */
66801c135cSArtem B. Bityutskiy #define UBI_IO_RETRIES 3
67801c135cSArtem B. Bityutskiy 
68801c135cSArtem B. Bityutskiy /*
697b6c32daSXiaochuan-Xu  * Length of the protection queue. The length is effectively equivalent to the
707b6c32daSXiaochuan-Xu  * number of (global) erase cycles PEBs are protected from the wear-leveling
717b6c32daSXiaochuan-Xu  * worker.
727b6c32daSXiaochuan-Xu  */
737b6c32daSXiaochuan-Xu #define UBI_PROT_QUEUE_LEN 10
747b6c32daSXiaochuan-Xu 
7505a3cb7dSArtem Bityutskiy /* The volume ID/LEB number/erase counter is unknown */
760479ab48SArtem Bityutskiy #define UBI_UNKNOWN -1
770479ab48SArtem Bityutskiy 
780479ab48SArtem Bityutskiy /*
79eab73772SEzequiel Garcia  * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
80eab73772SEzequiel Garcia  * + 2 for the number plus 1 for the trailing zero byte.
81eab73772SEzequiel Garcia  */
82eab73772SEzequiel Garcia #define UBI_DFS_DIR_NAME "ubi%d"
83eab73772SEzequiel Garcia #define UBI_DFS_DIR_LEN  (3 + 2 + 1)
84eab73772SEzequiel Garcia 
85eab73772SEzequiel Garcia /*
8685c6e6e2SArtem Bityutskiy  * Error codes returned by the I/O sub-system.
87801c135cSArtem B. Bityutskiy  *
8874d82d26SArtem Bityutskiy  * UBI_IO_FF: the read region of flash contains only 0xFFs
89ec1f97f5SJilin Yuan  * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also there was a data
9092e1a7d9SArtem Bityutskiy  *                     integrity error reported by the MTD driver
9192e1a7d9SArtem Bityutskiy  *                     (uncorrectable ECC error in case of NAND)
92786d7831SArtem Bityutskiy  * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
93756e1df1SArtem Bityutskiy  * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
94756e1df1SArtem Bityutskiy  *                         data integrity error reported by the MTD driver
95756e1df1SArtem Bityutskiy  *                         (uncorrectable ECC error in case of NAND)
96801c135cSArtem B. Bityutskiy  * UBI_IO_BITFLIPS: bit-flips were detected and corrected
9792e1a7d9SArtem Bityutskiy  *
9892e1a7d9SArtem Bityutskiy  * Note, it is probably better to have bit-flip and ebadmsg as flags which can
9992e1a7d9SArtem Bityutskiy  * be or'ed with other error code. But this is a big change because there are
10092e1a7d9SArtem Bityutskiy  * may callers, so it does not worth the risk of introducing a bug
101801c135cSArtem B. Bityutskiy  */
102801c135cSArtem B. Bityutskiy enum {
10374d82d26SArtem Bityutskiy 	UBI_IO_FF = 1,
10492e1a7d9SArtem Bityutskiy 	UBI_IO_FF_BITFLIPS,
105786d7831SArtem Bityutskiy 	UBI_IO_BAD_HDR,
106756e1df1SArtem Bityutskiy 	UBI_IO_BAD_HDR_EBADMSG,
10792e1a7d9SArtem Bityutskiy 	UBI_IO_BITFLIPS,
108801c135cSArtem B. Bityutskiy };
109801c135cSArtem B. Bityutskiy 
11090bf0265SArtem Bityutskiy /*
11190bf0265SArtem Bityutskiy  * Return codes of the 'ubi_eba_copy_leb()' function.
11290bf0265SArtem Bityutskiy  *
11390bf0265SArtem Bityutskiy  * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
11490bf0265SArtem Bityutskiy  *                   PEB was put meanwhile, or there is I/O on the source PEB
115b86a2c56SArtem Bityutskiy  * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
116b86a2c56SArtem Bityutskiy  *                     PEB
117b86a2c56SArtem Bityutskiy  * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
118b86a2c56SArtem Bityutskiy  *                     PEB
11990bf0265SArtem Bityutskiy  * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
12090bf0265SArtem Bityutskiy  *                     PEB
121cc831464SArtem Bityutskiy  * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
12290bf0265SArtem Bityutskiy  *                       target PEB
123e801e128SBhavesh Parekh  * MOVE_RETRY: retry scrubbing the PEB
12490bf0265SArtem Bityutskiy  */
12590bf0265SArtem Bityutskiy enum {
12690bf0265SArtem Bityutskiy 	MOVE_CANCEL_RACE = 1,
127b86a2c56SArtem Bityutskiy 	MOVE_SOURCE_RD_ERR,
128b86a2c56SArtem Bityutskiy 	MOVE_TARGET_RD_ERR,
12990bf0265SArtem Bityutskiy 	MOVE_TARGET_WR_ERR,
130cc831464SArtem Bityutskiy 	MOVE_TARGET_BITFLIPS,
131e801e128SBhavesh Parekh 	MOVE_RETRY,
13290bf0265SArtem Bityutskiy };
13390bf0265SArtem Bityutskiy 
1345638b33aSRichard Weinberger /*
1355638b33aSRichard Weinberger  * Return codes of the fastmap sub-system
1365638b33aSRichard Weinberger  *
1375638b33aSRichard Weinberger  * UBI_NO_FASTMAP: No fastmap super block was found
1385638b33aSRichard Weinberger  * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
1395638b33aSRichard Weinberger  */
1405638b33aSRichard Weinberger enum {
1415638b33aSRichard Weinberger 	UBI_NO_FASTMAP = 1,
1425638b33aSRichard Weinberger 	UBI_BAD_FASTMAP,
1435638b33aSRichard Weinberger };
1445638b33aSRichard Weinberger 
14550269067Sdavid.oberhollenzer@sigma-star.at /*
14650269067Sdavid.oberhollenzer@sigma-star.at  * Flags for emulate_power_cut in ubi_debug_info
14750269067Sdavid.oberhollenzer@sigma-star.at  *
14850269067Sdavid.oberhollenzer@sigma-star.at  * POWER_CUT_EC_WRITE: Emulate a power cut when writing an EC header
14950269067Sdavid.oberhollenzer@sigma-star.at  * POWER_CUT_VID_WRITE: Emulate a power cut when writing a VID header
15050269067Sdavid.oberhollenzer@sigma-star.at  */
15150269067Sdavid.oberhollenzer@sigma-star.at enum {
15250269067Sdavid.oberhollenzer@sigma-star.at 	POWER_CUT_EC_WRITE = 0x01,
15350269067Sdavid.oberhollenzer@sigma-star.at 	POWER_CUT_VID_WRITE = 0x02,
15450269067Sdavid.oberhollenzer@sigma-star.at };
15550269067Sdavid.oberhollenzer@sigma-star.at 
1563a8d4642SArtem Bityutskiy /**
1573291b52fSBoris Brezillon  * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
1583291b52fSBoris Brezillon  *			   flash.
1593291b52fSBoris Brezillon  * @hdr: a pointer to the VID header stored in buffer
1603291b52fSBoris Brezillon  * @buffer: underlying buffer
1613291b52fSBoris Brezillon  */
1623291b52fSBoris Brezillon struct ubi_vid_io_buf {
1633291b52fSBoris Brezillon 	struct ubi_vid_hdr *hdr;
1643291b52fSBoris Brezillon 	void *buffer;
1653291b52fSBoris Brezillon };
1663291b52fSBoris Brezillon 
1673291b52fSBoris Brezillon /**
16806b68ba1SArtem Bityutskiy  * struct ubi_wl_entry - wear-leveling entry.
16923553b2cSXiaochuan-Xu  * @u.rb: link in the corresponding (free/used) RB-tree
1707b6c32daSXiaochuan-Xu  * @u.list: link in the protection queue
17106b68ba1SArtem Bityutskiy  * @ec: erase counter
17206b68ba1SArtem Bityutskiy  * @pnum: physical eraseblock number
17306b68ba1SArtem Bityutskiy  *
17485c6e6e2SArtem Bityutskiy  * This data structure is used in the WL sub-system. Each physical eraseblock
17585c6e6e2SArtem Bityutskiy  * has a corresponding &struct wl_entry object which may be kept in different
17685c6e6e2SArtem Bityutskiy  * RB-trees. See WL sub-system for details.
17706b68ba1SArtem Bityutskiy  */
17806b68ba1SArtem Bityutskiy struct ubi_wl_entry {
17923553b2cSXiaochuan-Xu 	union {
18006b68ba1SArtem Bityutskiy 		struct rb_node rb;
1817b6c32daSXiaochuan-Xu 		struct list_head list;
18223553b2cSXiaochuan-Xu 	} u;
18306b68ba1SArtem Bityutskiy 	int ec;
18406b68ba1SArtem Bityutskiy 	int pnum;
18506b68ba1SArtem Bityutskiy };
18606b68ba1SArtem Bityutskiy 
18706b68ba1SArtem Bityutskiy /**
1883a8d4642SArtem Bityutskiy  * struct ubi_ltree_entry - an entry in the lock tree.
1893a8d4642SArtem Bityutskiy  * @rb: links RB-tree nodes
1903a8d4642SArtem Bityutskiy  * @vol_id: volume ID of the locked logical eraseblock
1913a8d4642SArtem Bityutskiy  * @lnum: locked logical eraseblock number
1923a8d4642SArtem Bityutskiy  * @users: how many tasks are using this logical eraseblock or wait for it
1933a8d4642SArtem Bityutskiy  * @mutex: read/write mutex to implement read/write access serialization to
1943a8d4642SArtem Bityutskiy  *         the (@vol_id, @lnum) logical eraseblock
1953a8d4642SArtem Bityutskiy  *
19685c6e6e2SArtem Bityutskiy  * This data structure is used in the EBA sub-system to implement per-LEB
19785c6e6e2SArtem Bityutskiy  * locking. When a logical eraseblock is being locked - corresponding
1983a8d4642SArtem Bityutskiy  * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
19985c6e6e2SArtem Bityutskiy  * See EBA sub-system for details.
2003a8d4642SArtem Bityutskiy  */
2013a8d4642SArtem Bityutskiy struct ubi_ltree_entry {
2023a8d4642SArtem Bityutskiy 	struct rb_node rb;
2033a8d4642SArtem Bityutskiy 	int vol_id;
2043a8d4642SArtem Bityutskiy 	int lnum;
2053a8d4642SArtem Bityutskiy 	int users;
2063a8d4642SArtem Bityutskiy 	struct rw_semaphore mutex;
2073a8d4642SArtem Bityutskiy };
2083a8d4642SArtem Bityutskiy 
209f40ac9cdSArtem Bityutskiy /**
210f40ac9cdSArtem Bityutskiy  * struct ubi_rename_entry - volume re-name description data structure.
211f40ac9cdSArtem Bityutskiy  * @new_name_len: new volume name length
212f40ac9cdSArtem Bityutskiy  * @new_name: new volume name
213f40ac9cdSArtem Bityutskiy  * @remove: if not zero, this volume should be removed, not re-named
214f40ac9cdSArtem Bityutskiy  * @desc: descriptor of the volume
215f40ac9cdSArtem Bityutskiy  * @list: links re-name entries into a list
216f40ac9cdSArtem Bityutskiy  *
217f40ac9cdSArtem Bityutskiy  * This data structure is utilized in the multiple volume re-name code. Namely,
218f40ac9cdSArtem Bityutskiy  * UBI first creates a list of &struct ubi_rename_entry objects from the
219f40ac9cdSArtem Bityutskiy  * &struct ubi_rnvol_req request object, and then utilizes this list to do all
220f40ac9cdSArtem Bityutskiy  * the job.
221f40ac9cdSArtem Bityutskiy  */
222f40ac9cdSArtem Bityutskiy struct ubi_rename_entry {
223f40ac9cdSArtem Bityutskiy 	int new_name_len;
224f40ac9cdSArtem Bityutskiy 	char new_name[UBI_VOL_NAME_MAX + 1];
225f40ac9cdSArtem Bityutskiy 	int remove;
226f40ac9cdSArtem Bityutskiy 	struct ubi_volume_desc *desc;
227f40ac9cdSArtem Bityutskiy 	struct list_head list;
228f40ac9cdSArtem Bityutskiy };
229f40ac9cdSArtem Bityutskiy 
230801c135cSArtem B. Bityutskiy struct ubi_volume_desc;
231801c135cSArtem B. Bityutskiy 
232801c135cSArtem B. Bityutskiy /**
2335638b33aSRichard Weinberger  * struct ubi_fastmap_layout - in-memory fastmap data structure.
2345638b33aSRichard Weinberger  * @e: PEBs used by the current fastmap
2355638b33aSRichard Weinberger  * @to_be_tortured: if non-zero tortured this PEB
2365638b33aSRichard Weinberger  * @used_blocks: number of used PEBs
2375638b33aSRichard Weinberger  * @max_pool_size: maximal size of the user pool
2385638b33aSRichard Weinberger  * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
2395638b33aSRichard Weinberger  */
2405638b33aSRichard Weinberger struct ubi_fastmap_layout {
2415638b33aSRichard Weinberger 	struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
2425638b33aSRichard Weinberger 	int to_be_tortured[UBI_FM_MAX_BLOCKS];
2435638b33aSRichard Weinberger 	int used_blocks;
2445638b33aSRichard Weinberger 	int max_pool_size;
2455638b33aSRichard Weinberger 	int max_wl_pool_size;
2465638b33aSRichard Weinberger };
2475638b33aSRichard Weinberger 
2485638b33aSRichard Weinberger /**
2495638b33aSRichard Weinberger  * struct ubi_fm_pool - in-memory fastmap pool
2505638b33aSRichard Weinberger  * @pebs: PEBs in this pool
2515638b33aSRichard Weinberger  * @used: number of used PEBs
2525638b33aSRichard Weinberger  * @size: total number of PEBs in this pool
2535638b33aSRichard Weinberger  * @max_size: maximal size of the pool
2545638b33aSRichard Weinberger  *
2555638b33aSRichard Weinberger  * A pool gets filled with up to max_size.
2565638b33aSRichard Weinberger  * If all PEBs within the pool are used a new fastmap will be written
2575638b33aSRichard Weinberger  * to the flash and the pool gets refilled with empty PEBs.
2585638b33aSRichard Weinberger  *
2595638b33aSRichard Weinberger  */
2605638b33aSRichard Weinberger struct ubi_fm_pool {
2615638b33aSRichard Weinberger 	int pebs[UBI_FM_MAX_POOL_SIZE];
2625638b33aSRichard Weinberger 	int used;
2635638b33aSRichard Weinberger 	int size;
2645638b33aSRichard Weinberger 	int max_size;
2655638b33aSRichard Weinberger };
2665638b33aSRichard Weinberger 
2675638b33aSRichard Weinberger /**
2681f81a5ccSBoris Brezillon  * struct ubi_eba_leb_desc - EBA logical eraseblock descriptor
2691f81a5ccSBoris Brezillon  * @lnum: the logical eraseblock number
2701f81a5ccSBoris Brezillon  * @pnum: the physical eraseblock where the LEB can be found
2711f81a5ccSBoris Brezillon  *
2721f81a5ccSBoris Brezillon  * This structure is here to hide EBA's internal from other part of the
2731f81a5ccSBoris Brezillon  * UBI implementation.
2741f81a5ccSBoris Brezillon  *
2751f81a5ccSBoris Brezillon  * One can query the position of a LEB by calling ubi_eba_get_ldesc().
2761f81a5ccSBoris Brezillon  */
2771f81a5ccSBoris Brezillon struct ubi_eba_leb_desc {
2781f81a5ccSBoris Brezillon 	int lnum;
2791f81a5ccSBoris Brezillon 	int pnum;
2801f81a5ccSBoris Brezillon };
2811f81a5ccSBoris Brezillon 
2821f81a5ccSBoris Brezillon /**
283801c135cSArtem B. Bityutskiy  * struct ubi_volume - UBI volume description data structure.
284ec1f97f5SJilin Yuan  * @dev: device object to make use of the Linux device model
285801c135cSArtem B. Bityutskiy  * @cdev: character device object to create character device
286801c135cSArtem B. Bityutskiy  * @ubi: reference to the UBI device description object
287801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
288d05c77a8SArtem Bityutskiy  * @ref_count: volume reference count
289801c135cSArtem B. Bityutskiy  * @readers: number of users holding this volume in read-only mode
290801c135cSArtem B. Bityutskiy  * @writers: number of users holding this volume in read-write mode
291801c135cSArtem B. Bityutskiy  * @exclusive: whether somebody holds this volume in exclusive mode
292fafdd2bfSRichard Weinberger  * @metaonly: whether somebody is altering only meta data of this volume
293801c135cSArtem B. Bityutskiy  *
294801c135cSArtem B. Bityutskiy  * @reserved_pebs: how many physical eraseblocks are reserved for this volume
295801c135cSArtem B. Bityutskiy  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
296801c135cSArtem B. Bityutskiy  * @usable_leb_size: logical eraseblock size without padding
297801c135cSArtem B. Bityutskiy  * @used_ebs: how many logical eraseblocks in this volume contain data
298801c135cSArtem B. Bityutskiy  * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
299801c135cSArtem B. Bityutskiy  * @used_bytes: how many bytes of data this volume contains
300801c135cSArtem B. Bityutskiy  * @alignment: volume alignment
301801c135cSArtem B. Bityutskiy  * @data_pad: how many bytes are not used at the end of physical eraseblocks to
302801c135cSArtem B. Bityutskiy  *            satisfy the requested alignment
303801c135cSArtem B. Bityutskiy  * @name_len: volume name length
304801c135cSArtem B. Bityutskiy  * @name: volume name
305801c135cSArtem B. Bityutskiy  *
306801c135cSArtem B. Bityutskiy  * @upd_ebs: how many eraseblocks are expected to be updated
307e653879cSArtem Bityutskiy  * @ch_lnum: LEB number which is being changing by the atomic LEB change
308e653879cSArtem Bityutskiy  *           operation
309e653879cSArtem Bityutskiy  * @upd_bytes: how many bytes are expected to be received for volume update or
310e653879cSArtem Bityutskiy  *             atomic LEB change
311e653879cSArtem Bityutskiy  * @upd_received: how many bytes were already received for volume update or
312e653879cSArtem Bityutskiy  *                atomic LEB change
313e653879cSArtem Bityutskiy  * @upd_buf: update buffer which is used to collect update data or data for
314e653879cSArtem Bityutskiy  *           atomic LEB change
315801c135cSArtem B. Bityutskiy  *
316801c135cSArtem B. Bityutskiy  * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
31762652517SQuentin Schulz  * @skip_check: %1 if CRC check of this static volume should be skipped.
31862652517SQuentin Schulz  *		Directly reflects the presence of the
31962652517SQuentin Schulz  *		%UBI_VTBL_SKIP_CRC_CHECK_FLG flag in the vtbl entry
320896c0c06SArtem Bityutskiy  * @checked: %1 if this static volume was checked
321896c0c06SArtem Bityutskiy  * @corrupted: %1 if the volume is corrupted (static volumes only)
322896c0c06SArtem Bityutskiy  * @upd_marker: %1 if the update marker is set for this volume
323896c0c06SArtem Bityutskiy  * @updating: %1 if the volume is being updated
324e653879cSArtem Bityutskiy  * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
325766fb95bSSidney Amani  * @direct_writes: %1 if direct writes are enabled for this volume
326801c135cSArtem B. Bityutskiy  *
32734653fd8SRichard Weinberger  * @checkmap: bitmap to remember which PEB->LEB mappings got checked,
32834653fd8SRichard Weinberger  *            protected by UBI LEB lock tree.
32934653fd8SRichard Weinberger  *
330801c135cSArtem B. Bityutskiy  * The @corrupted field indicates that the volume's contents is corrupted.
331801c135cSArtem B. Bityutskiy  * Since UBI protects only static volumes, this field is not relevant to
332801c135cSArtem B. Bityutskiy  * dynamic volumes - it is user's responsibility to assure their data
333801c135cSArtem B. Bityutskiy  * integrity.
334801c135cSArtem B. Bityutskiy  *
335801c135cSArtem B. Bityutskiy  * The @upd_marker flag indicates that this volume is either being updated at
336801c135cSArtem B. Bityutskiy  * the moment or is damaged because of an unclean reboot.
337801c135cSArtem B. Bityutskiy  */
338801c135cSArtem B. Bityutskiy struct ubi_volume {
339801c135cSArtem B. Bityutskiy 	struct device dev;
340801c135cSArtem B. Bityutskiy 	struct cdev cdev;
341801c135cSArtem B. Bityutskiy 	struct ubi_device *ubi;
342801c135cSArtem B. Bityutskiy 	int vol_id;
343d05c77a8SArtem Bityutskiy 	int ref_count;
344801c135cSArtem B. Bityutskiy 	int readers;
345801c135cSArtem B. Bityutskiy 	int writers;
346801c135cSArtem B. Bityutskiy 	int exclusive;
347fafdd2bfSRichard Weinberger 	int metaonly;
348801c135cSArtem B. Bityutskiy 
349801c135cSArtem B. Bityutskiy 	int reserved_pebs;
350801c135cSArtem B. Bityutskiy 	int vol_type;
351801c135cSArtem B. Bityutskiy 	int usable_leb_size;
352801c135cSArtem B. Bityutskiy 	int used_ebs;
353801c135cSArtem B. Bityutskiy 	int last_eb_bytes;
354801c135cSArtem B. Bityutskiy 	long long used_bytes;
355801c135cSArtem B. Bityutskiy 	int alignment;
356801c135cSArtem B. Bityutskiy 	int data_pad;
357801c135cSArtem B. Bityutskiy 	int name_len;
358801c135cSArtem B. Bityutskiy 	char name[UBI_VOL_NAME_MAX + 1];
359801c135cSArtem B. Bityutskiy 
360801c135cSArtem B. Bityutskiy 	int upd_ebs;
361e653879cSArtem Bityutskiy 	int ch_lnum;
362801c135cSArtem B. Bityutskiy 	long long upd_bytes;
363801c135cSArtem B. Bityutskiy 	long long upd_received;
364801c135cSArtem B. Bityutskiy 	void *upd_buf;
365801c135cSArtem B. Bityutskiy 
366799dca34SBoris Brezillon 	struct ubi_eba_table *eba_tbl;
36762652517SQuentin Schulz 	unsigned int skip_check:1;
3688eee9f10SHarvey Harrison 	unsigned int checked:1;
3698eee9f10SHarvey Harrison 	unsigned int corrupted:1;
3708eee9f10SHarvey Harrison 	unsigned int upd_marker:1;
3718eee9f10SHarvey Harrison 	unsigned int updating:1;
3728eee9f10SHarvey Harrison 	unsigned int changing_leb:1;
373766fb95bSSidney Amani 	unsigned int direct_writes:1;
37434653fd8SRichard Weinberger 
37534653fd8SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
37634653fd8SRichard Weinberger 	unsigned long *checkmap;
37734653fd8SRichard Weinberger #endif
378801c135cSArtem B. Bityutskiy };
379801c135cSArtem B. Bityutskiy 
380801c135cSArtem B. Bityutskiy /**
38185c6e6e2SArtem Bityutskiy  * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
382801c135cSArtem B. Bityutskiy  * @vol: reference to the corresponding volume description object
383fafdd2bfSRichard Weinberger  * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, %UBI_EXCLUSIVE
384fafdd2bfSRichard Weinberger  * or %UBI_METAONLY)
385801c135cSArtem B. Bityutskiy  */
386801c135cSArtem B. Bityutskiy struct ubi_volume_desc {
387801c135cSArtem B. Bityutskiy 	struct ubi_volume *vol;
388801c135cSArtem B. Bityutskiy 	int mode;
389801c135cSArtem B. Bityutskiy };
390801c135cSArtem B. Bityutskiy 
391801c135cSArtem B. Bityutskiy /**
392eab73772SEzequiel Garcia  * struct ubi_debug_info - debugging information for an UBI device.
393eab73772SEzequiel Garcia  *
394eab73772SEzequiel Garcia  * @chk_gen: if UBI general extra checks are enabled
395eab73772SEzequiel Garcia  * @chk_io: if UBI I/O extra checks are enabled
3965fa7fa5dSRichard Weinberger  * @chk_fastmap: if UBI fastmap extra checks are enabled
397eab73772SEzequiel Garcia  * @disable_bgt: disable the background task for testing purposes
398eab73772SEzequiel Garcia  * @emulate_bitflips: emulate bit-flips for testing purposes
399eab73772SEzequiel Garcia  * @emulate_io_failures: emulate write/erase failures for testing purposes
40050269067Sdavid.oberhollenzer@sigma-star.at  * @emulate_power_cut: emulate power cut for testing purposes
40150269067Sdavid.oberhollenzer@sigma-star.at  * @power_cut_counter: count down for writes left until emulated power cut
40250269067Sdavid.oberhollenzer@sigma-star.at  * @power_cut_min: minimum number of writes before emulating a power cut
40350269067Sdavid.oberhollenzer@sigma-star.at  * @power_cut_max: maximum number of writes until emulating a power cut
404eab73772SEzequiel Garcia  * @dfs_dir_name: name of debugfs directory containing files of this UBI device
405eab73772SEzequiel Garcia  * @dfs_dir: direntry object of the UBI device debugfs directory
406eab73772SEzequiel Garcia  * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
407eab73772SEzequiel Garcia  * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
4085fa7fa5dSRichard Weinberger  * @dfs_chk_fastmap: debugfs knob to enable UBI fastmap extra checks
409eab73772SEzequiel Garcia  * @dfs_disable_bgt: debugfs knob to disable the background task
410eab73772SEzequiel Garcia  * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
411eab73772SEzequiel Garcia  * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
41250269067Sdavid.oberhollenzer@sigma-star.at  * @dfs_emulate_power_cut: debugfs knob to emulate power cuts
41350269067Sdavid.oberhollenzer@sigma-star.at  * @dfs_power_cut_min: debugfs knob for minimum writes before power cut
41450269067Sdavid.oberhollenzer@sigma-star.at  * @dfs_power_cut_max: debugfs knob for maximum writes until power cut
415eab73772SEzequiel Garcia  */
416eab73772SEzequiel Garcia struct ubi_debug_info {
417eab73772SEzequiel Garcia 	unsigned int chk_gen:1;
418eab73772SEzequiel Garcia 	unsigned int chk_io:1;
4195fa7fa5dSRichard Weinberger 	unsigned int chk_fastmap:1;
420eab73772SEzequiel Garcia 	unsigned int disable_bgt:1;
421eab73772SEzequiel Garcia 	unsigned int emulate_bitflips:1;
422eab73772SEzequiel Garcia 	unsigned int emulate_io_failures:1;
42350269067Sdavid.oberhollenzer@sigma-star.at 	unsigned int emulate_power_cut:2;
42450269067Sdavid.oberhollenzer@sigma-star.at 	unsigned int power_cut_counter;
42550269067Sdavid.oberhollenzer@sigma-star.at 	unsigned int power_cut_min;
42650269067Sdavid.oberhollenzer@sigma-star.at 	unsigned int power_cut_max;
427eab73772SEzequiel Garcia 	char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
428eab73772SEzequiel Garcia 	struct dentry *dfs_dir;
429eab73772SEzequiel Garcia 	struct dentry *dfs_chk_gen;
430eab73772SEzequiel Garcia 	struct dentry *dfs_chk_io;
4315fa7fa5dSRichard Weinberger 	struct dentry *dfs_chk_fastmap;
432eab73772SEzequiel Garcia 	struct dentry *dfs_disable_bgt;
433eab73772SEzequiel Garcia 	struct dentry *dfs_emulate_bitflips;
434eab73772SEzequiel Garcia 	struct dentry *dfs_emulate_io_failures;
43550269067Sdavid.oberhollenzer@sigma-star.at 	struct dentry *dfs_emulate_power_cut;
43650269067Sdavid.oberhollenzer@sigma-star.at 	struct dentry *dfs_power_cut_min;
43750269067Sdavid.oberhollenzer@sigma-star.at 	struct dentry *dfs_power_cut_max;
438eab73772SEzequiel Garcia };
439eab73772SEzequiel Garcia 
440eab73772SEzequiel Garcia /**
441801c135cSArtem B. Bityutskiy  * struct ubi_device - UBI device description structure
442ec1f97f5SJilin Yuan  * @dev: UBI device object to use the Linux device model
443801c135cSArtem B. Bityutskiy  * @cdev: character device object to create character device
444801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number
445801c135cSArtem B. Bityutskiy  * @ubi_name: UBI device name
446801c135cSArtem B. Bityutskiy  * @vol_count: number of volumes in this UBI device
447801c135cSArtem B. Bityutskiy  * @volumes: volumes of this UBI device
448801c135cSArtem B. Bityutskiy  * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
449d05c77a8SArtem Bityutskiy  *                @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
450d05c77a8SArtem Bityutskiy  *                @vol->readers, @vol->writers, @vol->exclusive,
451fafdd2bfSRichard Weinberger  *                @vol->metaonly, @vol->ref_count, @vol->mapping and
452fafdd2bfSRichard Weinberger  *                @vol->eba_tbl.
453e73f4459SArtem Bityutskiy  * @ref_count: count of references on the UBI device
4540c6c7fa1SAdrian Hunter  * @image_seq: image sequence number recorded on EC headers
455801c135cSArtem B. Bityutskiy  *
456801c135cSArtem B. Bityutskiy  * @rsvd_pebs: count of reserved physical eraseblocks
457801c135cSArtem B. Bityutskiy  * @avail_pebs: count of available physical eraseblocks
458801c135cSArtem B. Bityutskiy  * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
459801c135cSArtem B. Bityutskiy  *                 handling
460801c135cSArtem B. Bityutskiy  * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
461801c135cSArtem B. Bityutskiy  *
4624ccf8cffSArtem Bityutskiy  * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
4637b6c32daSXiaochuan-Xu  *                     of UBI initialization
464801c135cSArtem B. Bityutskiy  * @vtbl_slots: how many slots are available in the volume table
465801c135cSArtem B. Bityutskiy  * @vtbl_size: size of the volume table in bytes
466801c135cSArtem B. Bityutskiy  * @vtbl: in-RAM volume table copy
467f089c0b2SArtem Bityutskiy  * @device_mutex: protects on-flash volume table and serializes volume
468f089c0b2SArtem Bityutskiy  *                creation, deletion, update, re-size, re-name and set
469f089c0b2SArtem Bityutskiy  *                property
470801c135cSArtem B. Bityutskiy  *
471801c135cSArtem B. Bityutskiy  * @max_ec: current highest erase counter value
472801c135cSArtem B. Bityutskiy  * @mean_ec: current mean erase counter value
473801c135cSArtem B. Bityutskiy  *
474e8823bd6SArtem Bityutskiy  * @global_sqnum: global sequence number
475801c135cSArtem B. Bityutskiy  * @ltree_lock: protects the lock tree and @global_sqnum
476801c135cSArtem B. Bityutskiy  * @ltree: the lock tree
477e8823bd6SArtem Bityutskiy  * @alc_mutex: serializes "atomic LEB change" operations
478801c135cSArtem B. Bityutskiy  *
4795638b33aSRichard Weinberger  * @fm_disabled: non-zero if fastmap is disabled (default)
4805638b33aSRichard Weinberger  * @fm: in-memory data structure of the currently used fastmap
4815638b33aSRichard Weinberger  * @fm_pool: in-memory data structure of the fastmap pool
4825638b33aSRichard Weinberger  * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
4835638b33aSRichard Weinberger  *		sub-system
484111ab0b2SRichard Weinberger  * @fm_protect: serializes ubi_update_fastmap(), protects @fm_buf and makes sure
485111ab0b2SRichard Weinberger  * that critical sections cannot be interrupted by ubi_update_fastmap()
4865638b33aSRichard Weinberger  * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
4875638b33aSRichard Weinberger  * @fm_size: fastmap size in bytes
488111ab0b2SRichard Weinberger  * @fm_eba_sem: allows ubi_update_fastmap() to block EBA table changes
4895638b33aSRichard Weinberger  * @fm_work: fastmap work queue
49019371d73SRichard Weinberger  * @fm_work_scheduled: non-zero if fastmap work was scheduled
4911900149cSRichard Weinberger  * @fast_attach: non-zero if UBI was attached by fastmap
492d09e9a2bSZhihao Cheng  * @fm_anchor: The next anchor PEB to use for fastmap
493f9c34bb5SSascha Hauer  * @fm_do_produce_anchor: If true produce an anchor PEB in wl
4945638b33aSRichard Weinberger  *
495801c135cSArtem B. Bityutskiy  * @used: RB-tree of used physical eraseblocks
496b86a2c56SArtem Bityutskiy  * @erroneous: RB-tree of erroneous used physical eraseblocks
497801c135cSArtem B. Bityutskiy  * @free: RB-tree of free physical eraseblocks
4985638b33aSRichard Weinberger  * @free_count: Contains the number of elements in @free
499801c135cSArtem B. Bityutskiy  * @scrub: RB-tree of physical eraseblocks which need scrubbing
5007b6c32daSXiaochuan-Xu  * @pq: protection queue (contain physical eraseblocks which are temporarily
5017b6c32daSXiaochuan-Xu  *      protected from the wear-leveling worker)
5027b6c32daSXiaochuan-Xu  * @pq_head: protection queue head
5037b6c32daSXiaochuan-Xu  * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
504815bc5f8SArtem Bityutskiy  *	     @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
505d59f21beSRichard Weinberger  *	     @erroneous, @erroneous_peb_count, @fm_work_scheduled, @fm_pool,
506d59f21beSRichard Weinberger  *	     and @fm_wl_pool fields
50743f9b25aSArtem Bityutskiy  * @move_mutex: serializes eraseblock moves
508adfe83beSRichard Weinberger  * @work_sem: used to wait for all the scheduled works to finish and prevent
509adfe83beSRichard Weinberger  * new works from being submitted
510801c135cSArtem B. Bityutskiy  * @wl_scheduled: non-zero if the wear-leveling was scheduled
511801c135cSArtem B. Bityutskiy  * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
512801c135cSArtem B. Bityutskiy  *             physical eraseblock
513801c135cSArtem B. Bityutskiy  * @move_from: physical eraseblock from where the data is being moved
514801c135cSArtem B. Bityutskiy  * @move_to: physical eraseblock where the data is being moved to
515801c135cSArtem B. Bityutskiy  * @move_to_put: if the "to" PEB was put
516801c135cSArtem B. Bityutskiy  * @works: list of pending works
517801c135cSArtem B. Bityutskiy  * @works_count: count of pending works
518801c135cSArtem B. Bityutskiy  * @bgt_thread: background thread description object
519801c135cSArtem B. Bityutskiy  * @thread_enabled: if the background thread is enabled
520801c135cSArtem B. Bityutskiy  * @bgt_name: background thread name
521801c135cSArtem B. Bityutskiy  *
522801c135cSArtem B. Bityutskiy  * @flash_size: underlying MTD device size (in bytes)
523801c135cSArtem B. Bityutskiy  * @peb_count: count of physical eraseblocks on the MTD device
524801c135cSArtem B. Bityutskiy  * @peb_size: physical eraseblock size
5258beeb3bbSShmulik Ladkani  * @bad_peb_limit: top limit of expected bad physical eraseblocks
526801c135cSArtem B. Bityutskiy  * @bad_peb_count: count of bad physical eraseblocks
527801c135cSArtem B. Bityutskiy  * @good_peb_count: count of good physical eraseblocks
5285fc01ab6SArtem Bityutskiy  * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
5295fc01ab6SArtem Bityutskiy  *                  used by UBI)
530b86a2c56SArtem Bityutskiy  * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
531b86a2c56SArtem Bityutskiy  * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
532801c135cSArtem B. Bityutskiy  * @min_io_size: minimal input/output unit size of the underlying MTD device
533801c135cSArtem B. Bityutskiy  * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
534801c135cSArtem B. Bityutskiy  * @ro_mode: if the UBI device is in read-only mode
535801c135cSArtem B. Bityutskiy  * @leb_size: logical eraseblock size
536801c135cSArtem B. Bityutskiy  * @leb_start: starting offset of logical eraseblocks within physical
537801c135cSArtem B. Bityutskiy  *             eraseblocks
538801c135cSArtem B. Bityutskiy  * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
539801c135cSArtem B. Bityutskiy  * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
540801c135cSArtem B. Bityutskiy  * @vid_hdr_offset: starting offset of the volume identifier header (might be
541801c135cSArtem B. Bityutskiy  *                  unaligned)
542801c135cSArtem B. Bityutskiy  * @vid_hdr_aloffset: starting offset of the VID header aligned to
543801c135cSArtem B. Bityutskiy  *                    @hdrs_min_io_size
544801c135cSArtem B. Bityutskiy  * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
5452fae1312SAndrew F. Davis  * @bad_allowed: whether the MTD device admits bad physical eraseblocks or not
546ebf53f42SArtem Bityutskiy  * @nor_flash: non-zero if working on top of NOR flash
54730b542efSArtem Bityutskiy  * @max_write_size: maximum amount of bytes the underlying flash can write at a
54830b542efSArtem Bityutskiy  *                  time (MTD write buffer size)
549801c135cSArtem B. Bityutskiy  * @mtd: MTD device descriptor
550e88d6e10SArtem Bityutskiy  *
5510ca39d74SArtem Bityutskiy  * @peb_buf: a buffer of PEB size used for different purposes
5520ca39d74SArtem Bityutskiy  * @buf_mutex: protects @peb_buf
553f40ac9cdSArtem Bityutskiy  * @ckvol_mutex: serializes static volume checking when opening
5542a734bb8SArtem Bityutskiy  *
5552a734bb8SArtem Bityutskiy  * @dbg: debugging information for this UBI device
556801c135cSArtem B. Bityutskiy  */
557801c135cSArtem B. Bityutskiy struct ubi_device {
558801c135cSArtem B. Bityutskiy 	struct cdev cdev;
559801c135cSArtem B. Bityutskiy 	struct device dev;
560801c135cSArtem B. Bityutskiy 	int ubi_num;
561801c135cSArtem B. Bityutskiy 	char ubi_name[sizeof(UBI_NAME_STR)+5];
562801c135cSArtem B. Bityutskiy 	int vol_count;
563801c135cSArtem B. Bityutskiy 	struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
564801c135cSArtem B. Bityutskiy 	spinlock_t volumes_lock;
565e73f4459SArtem Bityutskiy 	int ref_count;
5660c6c7fa1SAdrian Hunter 	int image_seq;
567801c135cSArtem B. Bityutskiy 
568801c135cSArtem B. Bityutskiy 	int rsvd_pebs;
569801c135cSArtem B. Bityutskiy 	int avail_pebs;
570801c135cSArtem B. Bityutskiy 	int beb_rsvd_pebs;
571801c135cSArtem B. Bityutskiy 	int beb_rsvd_level;
5728beeb3bbSShmulik Ladkani 	int bad_peb_limit;
573801c135cSArtem B. Bityutskiy 
5744ccf8cffSArtem Bityutskiy 	int autoresize_vol_id;
575801c135cSArtem B. Bityutskiy 	int vtbl_slots;
576801c135cSArtem B. Bityutskiy 	int vtbl_size;
577801c135cSArtem B. Bityutskiy 	struct ubi_vtbl_record *vtbl;
578f089c0b2SArtem Bityutskiy 	struct mutex device_mutex;
579801c135cSArtem B. Bityutskiy 
580801c135cSArtem B. Bityutskiy 	int max_ec;
58185c6e6e2SArtem Bityutskiy 	/* Note, mean_ec is not updated run-time - should be fixed */
582801c135cSArtem B. Bityutskiy 	int mean_ec;
583801c135cSArtem B. Bityutskiy 
58485c6e6e2SArtem Bityutskiy 	/* EBA sub-system's stuff */
585801c135cSArtem B. Bityutskiy 	unsigned long long global_sqnum;
586801c135cSArtem B. Bityutskiy 	spinlock_t ltree_lock;
587801c135cSArtem B. Bityutskiy 	struct rb_root ltree;
588e8823bd6SArtem Bityutskiy 	struct mutex alc_mutex;
589801c135cSArtem B. Bityutskiy 
5905638b33aSRichard Weinberger 	/* Fastmap stuff */
5915638b33aSRichard Weinberger 	int fm_disabled;
5925638b33aSRichard Weinberger 	struct ubi_fastmap_layout *fm;
5935638b33aSRichard Weinberger 	struct ubi_fm_pool fm_pool;
5945638b33aSRichard Weinberger 	struct ubi_fm_pool fm_wl_pool;
595111ab0b2SRichard Weinberger 	struct rw_semaphore fm_eba_sem;
596111ab0b2SRichard Weinberger 	struct rw_semaphore fm_protect;
5975638b33aSRichard Weinberger 	void *fm_buf;
5985638b33aSRichard Weinberger 	size_t fm_size;
5995638b33aSRichard Weinberger 	struct work_struct fm_work;
60019371d73SRichard Weinberger 	int fm_work_scheduled;
6011900149cSRichard Weinberger 	int fast_attach;
602f9c34bb5SSascha Hauer 	struct ubi_wl_entry *fm_anchor;
603f9c34bb5SSascha Hauer 	int fm_do_produce_anchor;
6045638b33aSRichard Weinberger 
60585c6e6e2SArtem Bityutskiy 	/* Wear-leveling sub-system's stuff */
606801c135cSArtem B. Bityutskiy 	struct rb_root used;
607b86a2c56SArtem Bityutskiy 	struct rb_root erroneous;
608801c135cSArtem B. Bityutskiy 	struct rb_root free;
6095638b33aSRichard Weinberger 	int free_count;
610801c135cSArtem B. Bityutskiy 	struct rb_root scrub;
6117b6c32daSXiaochuan-Xu 	struct list_head pq[UBI_PROT_QUEUE_LEN];
6127b6c32daSXiaochuan-Xu 	int pq_head;
613801c135cSArtem B. Bityutskiy 	spinlock_t wl_lock;
61443f9b25aSArtem Bityutskiy 	struct mutex move_mutex;
615593dd33cSArtem Bityutskiy 	struct rw_semaphore work_sem;
616801c135cSArtem B. Bityutskiy 	int wl_scheduled;
617801c135cSArtem B. Bityutskiy 	struct ubi_wl_entry **lookuptbl;
618801c135cSArtem B. Bityutskiy 	struct ubi_wl_entry *move_from;
619801c135cSArtem B. Bityutskiy 	struct ubi_wl_entry *move_to;
620801c135cSArtem B. Bityutskiy 	int move_to_put;
621801c135cSArtem B. Bityutskiy 	struct list_head works;
622801c135cSArtem B. Bityutskiy 	int works_count;
623801c135cSArtem B. Bityutskiy 	struct task_struct *bgt_thread;
624801c135cSArtem B. Bityutskiy 	int thread_enabled;
625801c135cSArtem B. Bityutskiy 	char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
626801c135cSArtem B. Bityutskiy 
62785c6e6e2SArtem Bityutskiy 	/* I/O sub-system's stuff */
628801c135cSArtem B. Bityutskiy 	long long flash_size;
629801c135cSArtem B. Bityutskiy 	int peb_count;
630801c135cSArtem B. Bityutskiy 	int peb_size;
631801c135cSArtem B. Bityutskiy 	int bad_peb_count;
632801c135cSArtem B. Bityutskiy 	int good_peb_count;
6335fc01ab6SArtem Bityutskiy 	int corr_peb_count;
634b86a2c56SArtem Bityutskiy 	int erroneous_peb_count;
635b86a2c56SArtem Bityutskiy 	int max_erroneous;
636801c135cSArtem B. Bityutskiy 	int min_io_size;
637801c135cSArtem B. Bityutskiy 	int hdrs_min_io_size;
638801c135cSArtem B. Bityutskiy 	int ro_mode;
639801c135cSArtem B. Bityutskiy 	int leb_size;
640801c135cSArtem B. Bityutskiy 	int leb_start;
641801c135cSArtem B. Bityutskiy 	int ec_hdr_alsize;
642801c135cSArtem B. Bityutskiy 	int vid_hdr_alsize;
643801c135cSArtem B. Bityutskiy 	int vid_hdr_offset;
644801c135cSArtem B. Bityutskiy 	int vid_hdr_aloffset;
645801c135cSArtem B. Bityutskiy 	int vid_hdr_shift;
646ebf53f42SArtem Bityutskiy 	unsigned int bad_allowed:1;
647ebf53f42SArtem Bityutskiy 	unsigned int nor_flash:1;
64830b542efSArtem Bityutskiy 	int max_write_size;
649801c135cSArtem B. Bityutskiy 	struct mtd_info *mtd;
650e88d6e10SArtem Bityutskiy 
6510ca39d74SArtem Bityutskiy 	void *peb_buf;
652e88d6e10SArtem Bityutskiy 	struct mutex buf_mutex;
653783b273aSArtem Bityutskiy 	struct mutex ckvol_mutex;
6542a734bb8SArtem Bityutskiy 
655eab73772SEzequiel Garcia 	struct ubi_debug_info dbg;
656801c135cSArtem B. Bityutskiy };
657801c135cSArtem B. Bityutskiy 
6580479ab48SArtem Bityutskiy /**
6590479ab48SArtem Bityutskiy  * struct ubi_ainf_peb - attach information about a physical eraseblock.
6600479ab48SArtem Bityutskiy  * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
6610479ab48SArtem Bityutskiy  * @pnum: physical eraseblock number
6626dd3bc7eSJoel Reardon  * @vol_id: ID of the volume this LEB belongs to
6630479ab48SArtem Bityutskiy  * @lnum: logical eraseblock number
6640479ab48SArtem Bityutskiy  * @scrub: if this physical eraseblock needs scrubbing
6650479ab48SArtem Bityutskiy  * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
6660479ab48SArtem Bityutskiy  * @sqnum: sequence number
6670479ab48SArtem Bityutskiy  * @u: unions RB-tree or @list links
6680479ab48SArtem Bityutskiy  * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
6690479ab48SArtem Bityutskiy  * @u.list: link in one of the eraseblock lists
6700479ab48SArtem Bityutskiy  *
6710479ab48SArtem Bityutskiy  * One object of this type is allocated for each physical eraseblock when
6726dd3bc7eSJoel Reardon  * attaching an MTD device. Note, if this PEB does not belong to any LEB /
6736dd3bc7eSJoel Reardon  * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
6740479ab48SArtem Bityutskiy  */
6750479ab48SArtem Bityutskiy struct ubi_ainf_peb {
6760479ab48SArtem Bityutskiy 	int ec;
6770479ab48SArtem Bityutskiy 	int pnum;
6786dd3bc7eSJoel Reardon 	int vol_id;
6790479ab48SArtem Bityutskiy 	int lnum;
6800479ab48SArtem Bityutskiy 	unsigned int scrub:1;
6810479ab48SArtem Bityutskiy 	unsigned int copy_flag:1;
6820479ab48SArtem Bityutskiy 	unsigned long long sqnum;
6830479ab48SArtem Bityutskiy 	union {
6840479ab48SArtem Bityutskiy 		struct rb_node rb;
6850479ab48SArtem Bityutskiy 		struct list_head list;
6860479ab48SArtem Bityutskiy 	} u;
6870479ab48SArtem Bityutskiy };
6880479ab48SArtem Bityutskiy 
6890479ab48SArtem Bityutskiy /**
6900479ab48SArtem Bityutskiy  * struct ubi_ainf_volume - attaching information about a volume.
6910479ab48SArtem Bityutskiy  * @vol_id: volume ID
6920479ab48SArtem Bityutskiy  * @highest_lnum: highest logical eraseblock number in this volume
6930479ab48SArtem Bityutskiy  * @leb_count: number of logical eraseblocks in this volume
6940479ab48SArtem Bityutskiy  * @vol_type: volume type
6950479ab48SArtem Bityutskiy  * @used_ebs: number of used logical eraseblocks in this volume (only for
6960479ab48SArtem Bityutskiy  *            static volumes)
6970479ab48SArtem Bityutskiy  * @last_data_size: amount of data in the last logical eraseblock of this
6980479ab48SArtem Bityutskiy  *                  volume (always equivalent to the usable logical eraseblock
6990479ab48SArtem Bityutskiy  *                  size in case of dynamic volumes)
7000479ab48SArtem Bityutskiy  * @data_pad: how many bytes at the end of logical eraseblocks of this volume
7010479ab48SArtem Bityutskiy  *            are not used (due to volume alignment)
7020479ab48SArtem Bityutskiy  * @compat: compatibility flags of this volume
7030479ab48SArtem Bityutskiy  * @rb: link in the volume RB-tree
7040479ab48SArtem Bityutskiy  * @root: root of the RB-tree containing all the eraseblock belonging to this
7050479ab48SArtem Bityutskiy  *        volume (&struct ubi_ainf_peb objects)
7060479ab48SArtem Bityutskiy  *
7070479ab48SArtem Bityutskiy  * One object of this type is allocated for each volume when attaching an MTD
7080479ab48SArtem Bityutskiy  * device.
7090479ab48SArtem Bityutskiy  */
7100479ab48SArtem Bityutskiy struct ubi_ainf_volume {
7110479ab48SArtem Bityutskiy 	int vol_id;
7120479ab48SArtem Bityutskiy 	int highest_lnum;
7130479ab48SArtem Bityutskiy 	int leb_count;
7140479ab48SArtem Bityutskiy 	int vol_type;
7150479ab48SArtem Bityutskiy 	int used_ebs;
7160479ab48SArtem Bityutskiy 	int last_data_size;
7170479ab48SArtem Bityutskiy 	int data_pad;
7180479ab48SArtem Bityutskiy 	int compat;
7190479ab48SArtem Bityutskiy 	struct rb_node rb;
7200479ab48SArtem Bityutskiy 	struct rb_root root;
7210479ab48SArtem Bityutskiy };
7220479ab48SArtem Bityutskiy 
7230479ab48SArtem Bityutskiy /**
7240479ab48SArtem Bityutskiy  * struct ubi_attach_info - MTD device attaching information.
7250479ab48SArtem Bityutskiy  * @volumes: root of the volume RB-tree
7260479ab48SArtem Bityutskiy  * @corr: list of corrupted physical eraseblocks
7270479ab48SArtem Bityutskiy  * @free: list of free physical eraseblocks
7280479ab48SArtem Bityutskiy  * @erase: list of physical eraseblocks which have to be erased
7290479ab48SArtem Bityutskiy  * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
7300479ab48SArtem Bityutskiy  *         those belonging to "preserve"-compatible internal volumes)
731fdf10ed7SRichard Weinberger  * @fastmap: list of physical eraseblocks which relate to fastmap (e.g.,
732fdf10ed7SRichard Weinberger  *           eraseblocks of the current and not yet erased old fastmap blocks)
7330479ab48SArtem Bityutskiy  * @corr_peb_count: count of PEBs in the @corr list
7340479ab48SArtem Bityutskiy  * @empty_peb_count: count of PEBs which are presumably empty (contain only
7350479ab48SArtem Bityutskiy  *                   0xFF bytes)
7360479ab48SArtem Bityutskiy  * @alien_peb_count: count of PEBs in the @alien list
7370479ab48SArtem Bityutskiy  * @bad_peb_count: count of bad physical eraseblocks
7380479ab48SArtem Bityutskiy  * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
7390479ab48SArtem Bityutskiy  *                       as bad yet, but which look like bad
7400479ab48SArtem Bityutskiy  * @vols_found: number of volumes found
7410479ab48SArtem Bityutskiy  * @highest_vol_id: highest volume ID
7420479ab48SArtem Bityutskiy  * @is_empty: flag indicating whether the MTD device is empty or not
74374f2c6e9SRichard Weinberger  * @force_full_scan: flag indicating whether we need to do a full scan and drop
74474f2c6e9SRichard Weinberger 		     all existing Fastmap data structures
7450479ab48SArtem Bityutskiy  * @min_ec: lowest erase counter value
7460479ab48SArtem Bityutskiy  * @max_ec: highest erase counter value
7470479ab48SArtem Bityutskiy  * @max_sqnum: highest sequence number value
7480479ab48SArtem Bityutskiy  * @mean_ec: mean erase counter value
7490479ab48SArtem Bityutskiy  * @ec_sum: a temporary variable used when calculating @mean_ec
7500479ab48SArtem Bityutskiy  * @ec_count: a temporary variable used when calculating @mean_ec
7510479ab48SArtem Bityutskiy  * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
7527b6b749bSBoris Brezillon  * @ech: temporary EC header. Only available during scan
7533291b52fSBoris Brezillon  * @vidh: temporary VID buffer. Only available during scan
7540479ab48SArtem Bityutskiy  *
7550479ab48SArtem Bityutskiy  * This data structure contains the result of attaching an MTD device and may
7560479ab48SArtem Bityutskiy  * be used by other UBI sub-systems to build final UBI data structures, further
7570479ab48SArtem Bityutskiy  * error-recovery and so on.
7580479ab48SArtem Bityutskiy  */
7590479ab48SArtem Bityutskiy struct ubi_attach_info {
7600479ab48SArtem Bityutskiy 	struct rb_root volumes;
7610479ab48SArtem Bityutskiy 	struct list_head corr;
7620479ab48SArtem Bityutskiy 	struct list_head free;
7630479ab48SArtem Bityutskiy 	struct list_head erase;
7640479ab48SArtem Bityutskiy 	struct list_head alien;
765fdf10ed7SRichard Weinberger 	struct list_head fastmap;
7660479ab48SArtem Bityutskiy 	int corr_peb_count;
7670479ab48SArtem Bityutskiy 	int empty_peb_count;
7680479ab48SArtem Bityutskiy 	int alien_peb_count;
7690479ab48SArtem Bityutskiy 	int bad_peb_count;
7700479ab48SArtem Bityutskiy 	int maybe_bad_peb_count;
7710479ab48SArtem Bityutskiy 	int vols_found;
7720479ab48SArtem Bityutskiy 	int highest_vol_id;
7730479ab48SArtem Bityutskiy 	int is_empty;
77474f2c6e9SRichard Weinberger 	int force_full_scan;
7750479ab48SArtem Bityutskiy 	int min_ec;
7760479ab48SArtem Bityutskiy 	int max_ec;
7770479ab48SArtem Bityutskiy 	unsigned long long max_sqnum;
7780479ab48SArtem Bityutskiy 	int mean_ec;
7790479ab48SArtem Bityutskiy 	uint64_t ec_sum;
7800479ab48SArtem Bityutskiy 	int ec_count;
7810479ab48SArtem Bityutskiy 	struct kmem_cache *aeb_slab_cache;
7827b6b749bSBoris Brezillon 	struct ubi_ec_hdr *ech;
7833291b52fSBoris Brezillon 	struct ubi_vid_io_buf *vidb;
7840479ab48SArtem Bityutskiy };
7850479ab48SArtem Bityutskiy 
7865638b33aSRichard Weinberger /**
7875638b33aSRichard Weinberger  * struct ubi_work - UBI work description data structure.
7885638b33aSRichard Weinberger  * @list: a link in the list of pending works
7895638b33aSRichard Weinberger  * @func: worker function
7905638b33aSRichard Weinberger  * @e: physical eraseblock to erase
7915638b33aSRichard Weinberger  * @vol_id: the volume ID on which this erasure is being performed
7925638b33aSRichard Weinberger  * @lnum: the logical eraseblock number
7935638b33aSRichard Weinberger  * @torture: if the physical eraseblock has to be tortured
7945638b33aSRichard Weinberger  *
795849271a4SRichard Weinberger  * The @func pointer points to the worker function. If the @shutdown argument is
796849271a4SRichard Weinberger  * not zero, the worker has to free the resources and exit immediately as the
797849271a4SRichard Weinberger  * WL sub-system is shutting down.
798849271a4SRichard Weinberger  * The worker has to return zero in case of success and a negative error code in
7995638b33aSRichard Weinberger  * case of failure.
8005638b33aSRichard Weinberger  */
8015638b33aSRichard Weinberger struct ubi_work {
8025638b33aSRichard Weinberger 	struct list_head list;
803849271a4SRichard Weinberger 	int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
8045638b33aSRichard Weinberger 	/* The below fields are only relevant to erasure works */
8055638b33aSRichard Weinberger 	struct ubi_wl_entry *e;
8065638b33aSRichard Weinberger 	int vol_id;
8075638b33aSRichard Weinberger 	int lnum;
8085638b33aSRichard Weinberger 	int torture;
8095638b33aSRichard Weinberger };
8105638b33aSRichard Weinberger 
81118073733SArtem Bityutskiy #include "debug.h"
81218073733SArtem Bityutskiy 
81306b68ba1SArtem Bityutskiy extern struct kmem_cache *ubi_wl_entry_slab;
8144d187a88SJan Engelhardt extern const struct file_operations ubi_ctrl_cdev_operations;
8154d187a88SJan Engelhardt extern const struct file_operations ubi_cdev_operations;
8164d187a88SJan Engelhardt extern const struct file_operations ubi_vol_cdev_operations;
81753cd255cSTakashi Iwai extern struct class ubi_class;
818cdfa788aSArtem Bityutskiy extern struct mutex ubi_devices_mutex;
8190e0ee1ccSDmitry Pervushin extern struct blocking_notifier_head ubi_notifiers;
820801c135cSArtem B. Bityutskiy 
8215638b33aSRichard Weinberger /* attach.c */
82291f4285fSBoris Brezillon struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
82391f4285fSBoris Brezillon 				   int ec);
82491f4285fSBoris Brezillon void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
8250479ab48SArtem Bityutskiy int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
8260479ab48SArtem Bityutskiy 		  int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
827de4c455bSBoris Brezillon struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);
8280479ab48SArtem Bityutskiy struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
8290479ab48SArtem Bityutskiy 				    int vol_id);
8300479ab48SArtem Bityutskiy void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
8310479ab48SArtem Bityutskiy struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
8320479ab48SArtem Bityutskiy 				       struct ubi_attach_info *ai);
833dac6e208SRichard Weinberger int ubi_attach(struct ubi_device *ubi, int force_scan);
8340479ab48SArtem Bityutskiy void ubi_destroy_ai(struct ubi_attach_info *ai);
8350479ab48SArtem Bityutskiy 
836801c135cSArtem B. Bityutskiy /* vtbl.c */
837801c135cSArtem B. Bityutskiy int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
838801c135cSArtem B. Bityutskiy 			   struct ubi_vtbl_record *vtbl_rec);
839f40ac9cdSArtem Bityutskiy int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
840f40ac9cdSArtem Bityutskiy 			    struct list_head *rename_list);
841a4e6042fSArtem Bityutskiy int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
842801c135cSArtem B. Bityutskiy 
843801c135cSArtem B. Bityutskiy /* vmt.c */
844801c135cSArtem B. Bityutskiy int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
845f40ac9cdSArtem Bityutskiy int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
846801c135cSArtem B. Bityutskiy int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
847f40ac9cdSArtem Bityutskiy int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
84889b96b69SArtem Bityutskiy int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
84989b96b69SArtem Bityutskiy void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
850801c135cSArtem B. Bityutskiy 
851801c135cSArtem B. Bityutskiy /* upd.c */
8521b68d0eeSArtem Bityutskiy int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
8531b68d0eeSArtem Bityutskiy 		     long long bytes);
8541b68d0eeSArtem Bityutskiy int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
855801c135cSArtem B. Bityutskiy 			 const void __user *buf, int count);
856e653879cSArtem Bityutskiy int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
857e653879cSArtem Bityutskiy 			 const struct ubi_leb_change_req *req);
858e653879cSArtem Bityutskiy int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
859e653879cSArtem Bityutskiy 			     const void __user *buf, int count);
860801c135cSArtem B. Bityutskiy 
861801c135cSArtem B. Bityutskiy /* misc.c */
8629c9ec147SArtem Bityutskiy int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
8639c9ec147SArtem Bityutskiy 		      int length);
864801c135cSArtem B. Bityutskiy int ubi_check_volume(struct ubi_device *ubi, int vol_id);
86587e773c9SShmulik Ladkani void ubi_update_reserved(struct ubi_device *ubi);
866801c135cSArtem B. Bityutskiy void ubi_calculate_reserved(struct ubi_device *ubi);
867bb00e180SArtem Bityutskiy int ubi_check_pattern(const void *buf, uint8_t patt, int size);
868801c135cSArtem B. Bityutskiy 
ubi_leb_valid(struct ubi_volume * vol,int lnum)8699a5f09acSBoris Brezillon static inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
8709a5f09acSBoris Brezillon {
8719a5f09acSBoris Brezillon 	return lnum >= 0 && lnum < vol->reserved_pebs;
8729a5f09acSBoris Brezillon }
8739a5f09acSBoris Brezillon 
874801c135cSArtem B. Bityutskiy /* eba.c */
875799dca34SBoris Brezillon struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
876799dca34SBoris Brezillon 					   int nentries);
877799dca34SBoris Brezillon void ubi_eba_destroy_table(struct ubi_eba_table *tbl);
878799dca34SBoris Brezillon void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
879799dca34SBoris Brezillon 			int nentries);
880799dca34SBoris Brezillon void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl);
8811f81a5ccSBoris Brezillon void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
8821f81a5ccSBoris Brezillon 		       struct ubi_eba_leb_desc *ldesc);
88375547696SBoris Brezillon bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum);
88489b96b69SArtem Bityutskiy int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
88589b96b69SArtem Bityutskiy 		      int lnum);
88689b96b69SArtem Bityutskiy int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
88789b96b69SArtem Bityutskiy 		     void *buf, int offset, int len, int check);
8889ff08979SRichard Weinberger int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
8899ff08979SRichard Weinberger 			struct ubi_sgl *sgl, int lnum, int offset, int len,
8909ff08979SRichard Weinberger 			int check);
89189b96b69SArtem Bityutskiy int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
892b36a261eSRichard Weinberger 		      const void *buf, int offset, int len);
89389b96b69SArtem Bityutskiy int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
894b36a261eSRichard Weinberger 			 int lnum, const void *buf, int len, int used_ebs);
89589b96b69SArtem Bityutskiy int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
896b36a261eSRichard Weinberger 			      int lnum, const void *buf, int len);
897801c135cSArtem B. Bityutskiy int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
8983291b52fSBoris Brezillon 		     struct ubi_vid_io_buf *vidb);
89941e0cd9dSArtem Bityutskiy int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
9005638b33aSRichard Weinberger unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
9015638b33aSRichard Weinberger int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
9025638b33aSRichard Weinberger 		   struct ubi_attach_info *ai_scan);
903801c135cSArtem B. Bityutskiy 
904801c135cSArtem B. Bityutskiy /* wl.c */
905b36a261eSRichard Weinberger int ubi_wl_get_peb(struct ubi_device *ubi);
906d36e59e6SJoel Reardon int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
907d36e59e6SJoel Reardon 		   int pnum, int torture);
90862f38455SJoel Reardon int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
909801c135cSArtem B. Bityutskiy int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
91041e0cd9dSArtem Bityutskiy int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
911801c135cSArtem B. Bityutskiy void ubi_wl_close(struct ubi_device *ubi);
912cdfa788aSArtem Bityutskiy int ubi_thread(void *u);
9135638b33aSRichard Weinberger struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
9145638b33aSRichard Weinberger int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
9155638b33aSRichard Weinberger 		      int lnum, int torture);
9165638b33aSRichard Weinberger int ubi_is_erase_work(struct ubi_work *wrk);
9175638b33aSRichard Weinberger void ubi_refill_pools(struct ubi_device *ubi);
9185638b33aSRichard Weinberger int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
919663586c0SRichard Weinberger int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
920801c135cSArtem B. Bityutskiy 
921801c135cSArtem B. Bityutskiy /* io.c */
922801c135cSArtem B. Bityutskiy int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
923801c135cSArtem B. Bityutskiy 		int len);
924e88d6e10SArtem Bityutskiy int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
925e88d6e10SArtem Bityutskiy 		 int len);
926e88d6e10SArtem Bityutskiy int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
927801c135cSArtem B. Bityutskiy int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
928801c135cSArtem B. Bityutskiy int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
929e88d6e10SArtem Bityutskiy int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
930801c135cSArtem B. Bityutskiy 		       struct ubi_ec_hdr *ec_hdr, int verbose);
931e88d6e10SArtem Bityutskiy int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
932801c135cSArtem B. Bityutskiy 			struct ubi_ec_hdr *ec_hdr);
933e88d6e10SArtem Bityutskiy int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
9343291b52fSBoris Brezillon 			struct ubi_vid_io_buf *vidb, int verbose);
935e88d6e10SArtem Bityutskiy int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
9363291b52fSBoris Brezillon 			 struct ubi_vid_io_buf *vidb);
937801c135cSArtem B. Bityutskiy 
938e73f4459SArtem Bityutskiy /* build.c */
939256334c3SRichard Genoud int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
940*669d2044SZhihao Cheng 		       int vid_hdr_offset, int max_beb_per1024,
941*669d2044SZhihao Cheng 		       bool disable_fm);
942cdfa788aSArtem Bityutskiy int ubi_detach_mtd_dev(int ubi_num, int anyway);
943e73f4459SArtem Bityutskiy struct ubi_device *ubi_get_device(int ubi_num);
944e73f4459SArtem Bityutskiy void ubi_put_device(struct ubi_device *ubi);
945e73f4459SArtem Bityutskiy struct ubi_device *ubi_get_by_major(int major);
946e73f4459SArtem Bityutskiy int ubi_major2num(int major);
9470e0ee1ccSDmitry Pervushin int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
9480e0ee1ccSDmitry Pervushin 		      int ntype);
9490e0ee1ccSDmitry Pervushin int ubi_notify_all(struct ubi_device *ubi, int ntype,
9500e0ee1ccSDmitry Pervushin 		   struct notifier_block *nb);
9510e0ee1ccSDmitry Pervushin int ubi_enumerate_volumes(struct notifier_block *nb);
952fc55dacfSHou Tao void ubi_free_all_volumes(struct ubi_device *ubi);
95347e1ec70SArtem Bityutskiy void ubi_free_internal_volumes(struct ubi_device *ubi);
9540e0ee1ccSDmitry Pervushin 
9550e0ee1ccSDmitry Pervushin /* kapi.c */
9560e0ee1ccSDmitry Pervushin void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
9570e0ee1ccSDmitry Pervushin void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
9580e0ee1ccSDmitry Pervushin 			    struct ubi_volume_info *vi);
9595638b33aSRichard Weinberger /* scan.c */
9605638b33aSRichard Weinberger int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
9615638b33aSRichard Weinberger 		      int pnum, const struct ubi_vid_hdr *vid_hdr);
9625638b33aSRichard Weinberger 
9635638b33aSRichard Weinberger /* fastmap.c */
9645e0246e3SRichard Weinberger #ifdef CONFIG_MTD_UBI_FASTMAP
9655638b33aSRichard Weinberger size_t ubi_calc_fm_size(struct ubi_device *ubi);
9665638b33aSRichard Weinberger int ubi_update_fastmap(struct ubi_device *ubi);
9675638b33aSRichard Weinberger int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
968fdf10ed7SRichard Weinberger 		     struct ubi_attach_info *scan_ai);
96934653fd8SRichard Weinberger int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count);
97034653fd8SRichard Weinberger void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol);
9715e0246e3SRichard Weinberger #else
ubi_update_fastmap(struct ubi_device * ubi)9725e0246e3SRichard Weinberger static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
ubi_fastmap_init_checkmap(struct ubi_volume * vol,int leb_count)97309971877SRishi Gupta static inline int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) { return 0; }
ubi_fastmap_destroy_checkmap(struct ubi_volume * vol)97434653fd8SRichard Weinberger static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}
9755e0246e3SRichard Weinberger #endif
976e73f4459SArtem Bityutskiy 
9779d54c8a3SEzequiel Garcia /* block.c */
9789d54c8a3SEzequiel Garcia #ifdef CONFIG_MTD_UBI_BLOCK
9799d54c8a3SEzequiel Garcia int ubiblock_init(void);
9809d54c8a3SEzequiel Garcia void ubiblock_exit(void);
9814d283ee2SArtem Bityutskiy int ubiblock_create(struct ubi_volume_info *vi);
9824d283ee2SArtem Bityutskiy int ubiblock_remove(struct ubi_volume_info *vi);
9839d54c8a3SEzequiel Garcia #else
ubiblock_init(void)9849d54c8a3SEzequiel Garcia static inline int ubiblock_init(void) { return 0; }
ubiblock_exit(void)9859d54c8a3SEzequiel Garcia static inline void ubiblock_exit(void) {}
ubiblock_create(struct ubi_volume_info * vi)9864d283ee2SArtem Bityutskiy static inline int ubiblock_create(struct ubi_volume_info *vi)
9874d283ee2SArtem Bityutskiy {
98880744cc9SEzequiel Garcia 	return -ENOSYS;
9894d283ee2SArtem Bityutskiy }
ubiblock_remove(struct ubi_volume_info * vi)9904d283ee2SArtem Bityutskiy static inline int ubiblock_remove(struct ubi_volume_info *vi)
9914d283ee2SArtem Bityutskiy {
99280744cc9SEzequiel Garcia 	return -ENOSYS;
9934d283ee2SArtem Bityutskiy }
9949d54c8a3SEzequiel Garcia #endif
9959d54c8a3SEzequiel Garcia 
99623c482efSRichard Weinberger /*
99723c482efSRichard Weinberger  * ubi_for_each_free_peb - walk the UBI free RB tree.
99823c482efSRichard Weinberger  * @ubi: UBI device description object
99923c482efSRichard Weinberger  * @e: a pointer to a ubi_wl_entry to use as cursor
100023c482efSRichard Weinberger  * @pos: a pointer to RB-tree entry type to use as a loop counter
100123c482efSRichard Weinberger  */
100223c482efSRichard Weinberger #define ubi_for_each_free_peb(ubi, e, tmp_rb)	\
100323c482efSRichard Weinberger 	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
100423c482efSRichard Weinberger 
100523c482efSRichard Weinberger /*
100623c482efSRichard Weinberger  * ubi_for_each_used_peb - walk the UBI used RB tree.
100723c482efSRichard Weinberger  * @ubi: UBI device description object
100823c482efSRichard Weinberger  * @e: a pointer to a ubi_wl_entry to use as cursor
100923c482efSRichard Weinberger  * @pos: a pointer to RB-tree entry type to use as a loop counter
101023c482efSRichard Weinberger  */
101123c482efSRichard Weinberger #define ubi_for_each_used_peb(ubi, e, tmp_rb)	\
101223c482efSRichard Weinberger 	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
101323c482efSRichard Weinberger 
101423c482efSRichard Weinberger /*
101523c482efSRichard Weinberger  * ubi_for_each_scub_peb - walk the UBI scub RB tree.
101623c482efSRichard Weinberger  * @ubi: UBI device description object
101723c482efSRichard Weinberger  * @e: a pointer to a ubi_wl_entry to use as cursor
101823c482efSRichard Weinberger  * @pos: a pointer to RB-tree entry type to use as a loop counter
101923c482efSRichard Weinberger  */
102023c482efSRichard Weinberger #define ubi_for_each_scrub_peb(ubi, e, tmp_rb)	\
102123c482efSRichard Weinberger 	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
102223c482efSRichard Weinberger 
102323c482efSRichard Weinberger /*
102423c482efSRichard Weinberger  * ubi_for_each_protected_peb - walk the UBI protection queue.
102523c482efSRichard Weinberger  * @ubi: UBI device description object
102623c482efSRichard Weinberger  * @i: a integer used as counter
102723c482efSRichard Weinberger  * @e: a pointer to a ubi_wl_entry to use as cursor
102823c482efSRichard Weinberger  */
102923c482efSRichard Weinberger #define ubi_for_each_protected_peb(ubi, i, e)	\
103023c482efSRichard Weinberger 	for ((i) = 0; (i) < UBI_PROT_QUEUE_LEN; (i)++)	\
103123c482efSRichard Weinberger 		list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
10329d54c8a3SEzequiel Garcia 
1033801c135cSArtem B. Bityutskiy /*
1034801c135cSArtem B. Bityutskiy  * ubi_rb_for_each_entry - walk an RB-tree.
1035fd589a8fSAnand Gadiyar  * @rb: a pointer to type 'struct rb_node' to use as a loop counter
1036801c135cSArtem B. Bityutskiy  * @pos: a pointer to RB-tree entry type to use as a loop counter
1037801c135cSArtem B. Bityutskiy  * @root: RB-tree's root
1038801c135cSArtem B. Bityutskiy  * @member: the name of the 'struct rb_node' within the RB-tree entry
1039801c135cSArtem B. Bityutskiy  */
1040801c135cSArtem B. Bityutskiy #define ubi_rb_for_each_entry(rb, pos, root, member)                         \
1041801c135cSArtem B. Bityutskiy 	for (rb = rb_first(root),                                            \
1042801c135cSArtem B. Bityutskiy 	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL);     \
1043801c135cSArtem B. Bityutskiy 	     rb;                                                             \
1044758d8e46SPhil Carmody 	     rb = rb_next(rb),                                               \
1045758d8e46SPhil Carmody 	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
1046801c135cSArtem B. Bityutskiy 
10470479ab48SArtem Bityutskiy /*
10480479ab48SArtem Bityutskiy  * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
10490479ab48SArtem Bityutskiy  *
10500479ab48SArtem Bityutskiy  * @av: volume attaching information
10510479ab48SArtem Bityutskiy  * @aeb: attaching eraseblock information
10520479ab48SArtem Bityutskiy  * @list: the list to move to
10530479ab48SArtem Bityutskiy  */
ubi_move_aeb_to_list(struct ubi_ainf_volume * av,struct ubi_ainf_peb * aeb,struct list_head * list)10540479ab48SArtem Bityutskiy static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
10550479ab48SArtem Bityutskiy 					 struct ubi_ainf_peb *aeb,
10560479ab48SArtem Bityutskiy 					 struct list_head *list)
10570479ab48SArtem Bityutskiy {
10580479ab48SArtem Bityutskiy 		rb_erase(&aeb->u.rb, &av->root);
10590479ab48SArtem Bityutskiy 		list_add_tail(&aeb->u.list, list);
10600479ab48SArtem Bityutskiy }
10610479ab48SArtem Bityutskiy 
1062801c135cSArtem B. Bityutskiy /**
10633291b52fSBoris Brezillon  * ubi_init_vid_buf - Initialize a VID buffer
10643291b52fSBoris Brezillon  * @ubi: the UBI device
10653291b52fSBoris Brezillon  * @vidb: the VID buffer to initialize
10663291b52fSBoris Brezillon  * @buf: the underlying buffer
1067801c135cSArtem B. Bityutskiy  */
ubi_init_vid_buf(const struct ubi_device * ubi,struct ubi_vid_io_buf * vidb,void * buf)10683291b52fSBoris Brezillon static inline void ubi_init_vid_buf(const struct ubi_device *ubi,
10693291b52fSBoris Brezillon 				    struct ubi_vid_io_buf *vidb,
10703291b52fSBoris Brezillon 				    void *buf)
1071801c135cSArtem B. Bityutskiy {
10723291b52fSBoris Brezillon 	if (buf)
10733291b52fSBoris Brezillon 		memset(buf, 0, ubi->vid_hdr_alsize);
1074801c135cSArtem B. Bityutskiy 
10753291b52fSBoris Brezillon 	vidb->buffer = buf;
10763291b52fSBoris Brezillon 	vidb->hdr = buf + ubi->vid_hdr_shift;
1077801c135cSArtem B. Bityutskiy }
1078801c135cSArtem B. Bityutskiy 
1079801c135cSArtem B. Bityutskiy /**
10803291b52fSBoris Brezillon  * ubi_init_vid_buf - Allocate a VID buffer
10813291b52fSBoris Brezillon  * @ubi: the UBI device
10823291b52fSBoris Brezillon  * @gfp_flags: GFP flags to use for the allocation
1083801c135cSArtem B. Bityutskiy  */
10843291b52fSBoris Brezillon static inline struct ubi_vid_io_buf *
ubi_alloc_vid_buf(const struct ubi_device * ubi,gfp_t gfp_flags)10853291b52fSBoris Brezillon ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
1086801c135cSArtem B. Bityutskiy {
10873291b52fSBoris Brezillon 	struct ubi_vid_io_buf *vidb;
10883291b52fSBoris Brezillon 	void *buf;
1089801c135cSArtem B. Bityutskiy 
10903291b52fSBoris Brezillon 	vidb = kzalloc(sizeof(*vidb), gfp_flags);
10913291b52fSBoris Brezillon 	if (!vidb)
10923291b52fSBoris Brezillon 		return NULL;
10933291b52fSBoris Brezillon 
10943291b52fSBoris Brezillon 	buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
10953291b52fSBoris Brezillon 	if (!buf) {
10963291b52fSBoris Brezillon 		kfree(vidb);
10973291b52fSBoris Brezillon 		return NULL;
10983291b52fSBoris Brezillon 	}
10993291b52fSBoris Brezillon 
11003291b52fSBoris Brezillon 	ubi_init_vid_buf(ubi, vidb, buf);
11013291b52fSBoris Brezillon 
11023291b52fSBoris Brezillon 	return vidb;
11033291b52fSBoris Brezillon }
11043291b52fSBoris Brezillon 
11053291b52fSBoris Brezillon /**
11063291b52fSBoris Brezillon  * ubi_free_vid_buf - Free a VID buffer
11073291b52fSBoris Brezillon  * @vidb: the VID buffer to free
11083291b52fSBoris Brezillon  */
ubi_free_vid_buf(struct ubi_vid_io_buf * vidb)11093291b52fSBoris Brezillon static inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
11103291b52fSBoris Brezillon {
11113291b52fSBoris Brezillon 	if (!vidb)
1112801c135cSArtem B. Bityutskiy 		return;
1113801c135cSArtem B. Bityutskiy 
11143291b52fSBoris Brezillon 	kfree(vidb->buffer);
11153291b52fSBoris Brezillon 	kfree(vidb);
11163291b52fSBoris Brezillon }
11173291b52fSBoris Brezillon 
11183291b52fSBoris Brezillon /**
11193291b52fSBoris Brezillon  * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
11203291b52fSBoris Brezillon  * @vidb: VID buffer
11213291b52fSBoris Brezillon  */
ubi_get_vid_hdr(struct ubi_vid_io_buf * vidb)11223291b52fSBoris Brezillon static inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
11233291b52fSBoris Brezillon {
11243291b52fSBoris Brezillon 	return vidb->hdr;
1125801c135cSArtem B. Bityutskiy }
1126801c135cSArtem B. Bityutskiy 
1127801c135cSArtem B. Bityutskiy /*
1128801c135cSArtem B. Bityutskiy  * This function is equivalent to 'ubi_io_read()', but @offset is relative to
1129801c135cSArtem B. Bityutskiy  * the beginning of the logical eraseblock, not to the beginning of the
1130801c135cSArtem B. Bityutskiy  * physical eraseblock.
1131801c135cSArtem B. Bityutskiy  */
ubi_io_read_data(const struct ubi_device * ubi,void * buf,int pnum,int offset,int len)1132801c135cSArtem B. Bityutskiy static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
1133801c135cSArtem B. Bityutskiy 				   int pnum, int offset, int len)
1134801c135cSArtem B. Bityutskiy {
1135801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0);
1136801c135cSArtem B. Bityutskiy 	return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
1137801c135cSArtem B. Bityutskiy }
1138801c135cSArtem B. Bityutskiy 
1139801c135cSArtem B. Bityutskiy /*
1140801c135cSArtem B. Bityutskiy  * This function is equivalent to 'ubi_io_write()', but @offset is relative to
1141801c135cSArtem B. Bityutskiy  * the beginning of the logical eraseblock, not to the beginning of the
1142801c135cSArtem B. Bityutskiy  * physical eraseblock.
1143801c135cSArtem B. Bityutskiy  */
ubi_io_write_data(struct ubi_device * ubi,const void * buf,int pnum,int offset,int len)1144e88d6e10SArtem Bityutskiy static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
1145801c135cSArtem B. Bityutskiy 				    int pnum, int offset, int len)
1146801c135cSArtem B. Bityutskiy {
1147801c135cSArtem B. Bityutskiy 	ubi_assert(offset >= 0);
1148801c135cSArtem B. Bityutskiy 	return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
1149801c135cSArtem B. Bityutskiy }
1150801c135cSArtem B. Bityutskiy 
1151801c135cSArtem B. Bityutskiy /**
1152801c135cSArtem B. Bityutskiy  * ubi_ro_mode - switch to read-only mode.
1153801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1154801c135cSArtem B. Bityutskiy  */
ubi_ro_mode(struct ubi_device * ubi)1155801c135cSArtem B. Bityutskiy static inline void ubi_ro_mode(struct ubi_device *ubi)
1156801c135cSArtem B. Bityutskiy {
115743f9b25aSArtem Bityutskiy 	if (!ubi->ro_mode) {
1158801c135cSArtem B. Bityutskiy 		ubi->ro_mode = 1;
115932608703STanya Brokhman 		ubi_warn(ubi, "switch to read-only mode");
116025886a36SArtem Bityutskiy 		dump_stack();
1161801c135cSArtem B. Bityutskiy 	}
116243f9b25aSArtem Bityutskiy }
1163801c135cSArtem B. Bityutskiy 
1164801c135cSArtem B. Bityutskiy /**
1165801c135cSArtem B. Bityutskiy  * vol_id2idx - get table index by volume ID.
1166801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1167801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
1168801c135cSArtem B. Bityutskiy  */
vol_id2idx(const struct ubi_device * ubi,int vol_id)1169801c135cSArtem B. Bityutskiy static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
1170801c135cSArtem B. Bityutskiy {
1171801c135cSArtem B. Bityutskiy 	if (vol_id >= UBI_INTERNAL_VOL_START)
1172801c135cSArtem B. Bityutskiy 		return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
1173801c135cSArtem B. Bityutskiy 	else
1174801c135cSArtem B. Bityutskiy 		return vol_id;
1175801c135cSArtem B. Bityutskiy }
1176801c135cSArtem B. Bityutskiy 
1177801c135cSArtem B. Bityutskiy /**
1178801c135cSArtem B. Bityutskiy  * idx2vol_id - get volume ID by table index.
1179801c135cSArtem B. Bityutskiy  * @ubi: UBI device description object
1180801c135cSArtem B. Bityutskiy  * @idx: table index
1181801c135cSArtem B. Bityutskiy  */
idx2vol_id(const struct ubi_device * ubi,int idx)1182801c135cSArtem B. Bityutskiy static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
1183801c135cSArtem B. Bityutskiy {
1184801c135cSArtem B. Bityutskiy 	if (idx >= ubi->vtbl_slots)
1185801c135cSArtem B. Bityutskiy 		return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
1186801c135cSArtem B. Bityutskiy 	else
1187801c135cSArtem B. Bityutskiy 		return idx;
1188801c135cSArtem B. Bityutskiy }
1189801c135cSArtem B. Bityutskiy 
1190243a4f81SRichard Weinberger /**
1191243a4f81SRichard Weinberger  * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
1192243a4f81SRichard Weinberger  * @vol_id: volume ID
1193243a4f81SRichard Weinberger  */
ubi_is_fm_vol(int vol_id)1194243a4f81SRichard Weinberger static inline bool ubi_is_fm_vol(int vol_id)
1195243a4f81SRichard Weinberger {
1196243a4f81SRichard Weinberger 	switch (vol_id) {
1197243a4f81SRichard Weinberger 		case UBI_FM_SB_VOLUME_ID:
1198243a4f81SRichard Weinberger 		case UBI_FM_DATA_VOLUME_ID:
1199243a4f81SRichard Weinberger 		return true;
1200243a4f81SRichard Weinberger 	}
1201243a4f81SRichard Weinberger 
1202243a4f81SRichard Weinberger 	return false;
1203243a4f81SRichard Weinberger }
1204243a4f81SRichard Weinberger 
1205fdf10ed7SRichard Weinberger /**
1206fdf10ed7SRichard Weinberger  * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
1207fdf10ed7SRichard Weinberger  * @ubi: UBI device description object
1208fdf10ed7SRichard Weinberger  * @pnum: physical eraseblock to look for
1209fdf10ed7SRichard Weinberger  *
1210fdf10ed7SRichard Weinberger  * This function returns a wear leveling object if @pnum relates to the current
1211fdf10ed7SRichard Weinberger  * fastmap, @NULL otherwise.
1212fdf10ed7SRichard Weinberger  */
ubi_find_fm_block(const struct ubi_device * ubi,int pnum)1213fdf10ed7SRichard Weinberger static inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi,
1214fdf10ed7SRichard Weinberger 						     int pnum)
1215fdf10ed7SRichard Weinberger {
1216fdf10ed7SRichard Weinberger 	int i;
1217fdf10ed7SRichard Weinberger 
1218fdf10ed7SRichard Weinberger 	if (ubi->fm) {
1219fdf10ed7SRichard Weinberger 		for (i = 0; i < ubi->fm->used_blocks; i++) {
1220fdf10ed7SRichard Weinberger 			if (ubi->fm->e[i]->pnum == pnum)
1221fdf10ed7SRichard Weinberger 				return ubi->fm->e[i];
1222fdf10ed7SRichard Weinberger 		}
1223fdf10ed7SRichard Weinberger 	}
1224fdf10ed7SRichard Weinberger 
1225fdf10ed7SRichard Weinberger 	return NULL;
1226fdf10ed7SRichard Weinberger }
1227fdf10ed7SRichard Weinberger 
1228801c135cSArtem B. Bityutskiy #endif /* !__UBI_UBI_H__ */
1229