xref: /kernel/linux/linux-5.10/drivers/mtd/ubi/ubi.h (revision 8c2ecf20)
18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006
48c2ecf20Sopenharmony_ci * Copyright (c) Nokia Corporation, 2006, 2007
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Author: Artem Bityutskiy (Битюцкий Артём)
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#ifndef __UBI_UBI_H__
108c2ecf20Sopenharmony_ci#define __UBI_UBI_H__
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/types.h>
138c2ecf20Sopenharmony_ci#include <linux/list.h>
148c2ecf20Sopenharmony_ci#include <linux/rbtree.h>
158c2ecf20Sopenharmony_ci#include <linux/sched.h>
168c2ecf20Sopenharmony_ci#include <linux/wait.h>
178c2ecf20Sopenharmony_ci#include <linux/mutex.h>
188c2ecf20Sopenharmony_ci#include <linux/rwsem.h>
198c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
208c2ecf20Sopenharmony_ci#include <linux/fs.h>
218c2ecf20Sopenharmony_ci#include <linux/cdev.h>
228c2ecf20Sopenharmony_ci#include <linux/device.h>
238c2ecf20Sopenharmony_ci#include <linux/slab.h>
248c2ecf20Sopenharmony_ci#include <linux/string.h>
258c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
268c2ecf20Sopenharmony_ci#include <linux/notifier.h>
278c2ecf20Sopenharmony_ci#include <linux/mtd/mtd.h>
288c2ecf20Sopenharmony_ci#include <linux/mtd/ubi.h>
298c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "ubi-media.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/* Maximum number of supported UBI devices */
348c2ecf20Sopenharmony_ci#define UBI_MAX_DEVICES 32
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* UBI name used for character devices, sysfs, etc */
378c2ecf20Sopenharmony_ci#define UBI_NAME_STR "ubi"
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct ubi_device;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci/* Normal UBI messages */
428c2ecf20Sopenharmony_ci__printf(2, 3)
438c2ecf20Sopenharmony_civoid ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci/* UBI warning messages */
468c2ecf20Sopenharmony_ci__printf(2, 3)
478c2ecf20Sopenharmony_civoid ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci/* UBI error messages */
508c2ecf20Sopenharmony_ci__printf(2, 3)
518c2ecf20Sopenharmony_civoid ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/* Background thread name pattern */
548c2ecf20Sopenharmony_ci#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/*
578c2ecf20Sopenharmony_ci * This marker in the EBA table means that the LEB is um-mapped.
588c2ecf20Sopenharmony_ci * NOTE! It has to have the same value as %UBI_ALL.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci#define UBI_LEB_UNMAPPED -1
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * In case of errors, UBI tries to repeat the operation several times before
648c2ecf20Sopenharmony_ci * returning error. The below constant defines how many times UBI re-tries.
658c2ecf20Sopenharmony_ci */
668c2ecf20Sopenharmony_ci#define UBI_IO_RETRIES 3
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * Length of the protection queue. The length is effectively equivalent to the
708c2ecf20Sopenharmony_ci * number of (global) erase cycles PEBs are protected from the wear-leveling
718c2ecf20Sopenharmony_ci * worker.
728c2ecf20Sopenharmony_ci */
738c2ecf20Sopenharmony_ci#define UBI_PROT_QUEUE_LEN 10
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci/* The volume ID/LEB number/erase counter is unknown */
768c2ecf20Sopenharmony_ci#define UBI_UNKNOWN -1
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci/*
798c2ecf20Sopenharmony_ci * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
808c2ecf20Sopenharmony_ci * + 2 for the number plus 1 for the trailing zero byte.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ci#define UBI_DFS_DIR_NAME "ubi%d"
838c2ecf20Sopenharmony_ci#define UBI_DFS_DIR_LEN  (3 + 2 + 1)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci/*
868c2ecf20Sopenharmony_ci * Error codes returned by the I/O sub-system.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * UBI_IO_FF: the read region of flash contains only 0xFFs
898c2ecf20Sopenharmony_ci * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
908c2ecf20Sopenharmony_ci *                     integrity error reported by the MTD driver
918c2ecf20Sopenharmony_ci *                     (uncorrectable ECC error in case of NAND)
928c2ecf20Sopenharmony_ci * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
938c2ecf20Sopenharmony_ci * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
948c2ecf20Sopenharmony_ci *                         data integrity error reported by the MTD driver
958c2ecf20Sopenharmony_ci *                         (uncorrectable ECC error in case of NAND)
968c2ecf20Sopenharmony_ci * UBI_IO_BITFLIPS: bit-flips were detected and corrected
978c2ecf20Sopenharmony_ci *
988c2ecf20Sopenharmony_ci * Note, it is probably better to have bit-flip and ebadmsg as flags which can
998c2ecf20Sopenharmony_ci * be or'ed with other error code. But this is a big change because there are
1008c2ecf20Sopenharmony_ci * may callers, so it does not worth the risk of introducing a bug
1018c2ecf20Sopenharmony_ci */
1028c2ecf20Sopenharmony_cienum {
1038c2ecf20Sopenharmony_ci	UBI_IO_FF = 1,
1048c2ecf20Sopenharmony_ci	UBI_IO_FF_BITFLIPS,
1058c2ecf20Sopenharmony_ci	UBI_IO_BAD_HDR,
1068c2ecf20Sopenharmony_ci	UBI_IO_BAD_HDR_EBADMSG,
1078c2ecf20Sopenharmony_ci	UBI_IO_BITFLIPS,
1088c2ecf20Sopenharmony_ci};
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/*
1118c2ecf20Sopenharmony_ci * Return codes of the 'ubi_eba_copy_leb()' function.
1128c2ecf20Sopenharmony_ci *
1138c2ecf20Sopenharmony_ci * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
1148c2ecf20Sopenharmony_ci *                   PEB was put meanwhile, or there is I/O on the source PEB
1158c2ecf20Sopenharmony_ci * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
1168c2ecf20Sopenharmony_ci *                     PEB
1178c2ecf20Sopenharmony_ci * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
1188c2ecf20Sopenharmony_ci *                     PEB
1198c2ecf20Sopenharmony_ci * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
1208c2ecf20Sopenharmony_ci *                     PEB
1218c2ecf20Sopenharmony_ci * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
1228c2ecf20Sopenharmony_ci *                       target PEB
1238c2ecf20Sopenharmony_ci * MOVE_RETRY: retry scrubbing the PEB
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_cienum {
1268c2ecf20Sopenharmony_ci	MOVE_CANCEL_RACE = 1,
1278c2ecf20Sopenharmony_ci	MOVE_SOURCE_RD_ERR,
1288c2ecf20Sopenharmony_ci	MOVE_TARGET_RD_ERR,
1298c2ecf20Sopenharmony_ci	MOVE_TARGET_WR_ERR,
1308c2ecf20Sopenharmony_ci	MOVE_TARGET_BITFLIPS,
1318c2ecf20Sopenharmony_ci	MOVE_RETRY,
1328c2ecf20Sopenharmony_ci};
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci/*
1358c2ecf20Sopenharmony_ci * Return codes of the fastmap sub-system
1368c2ecf20Sopenharmony_ci *
1378c2ecf20Sopenharmony_ci * UBI_NO_FASTMAP: No fastmap super block was found
1388c2ecf20Sopenharmony_ci * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
1398c2ecf20Sopenharmony_ci */
1408c2ecf20Sopenharmony_cienum {
1418c2ecf20Sopenharmony_ci	UBI_NO_FASTMAP = 1,
1428c2ecf20Sopenharmony_ci	UBI_BAD_FASTMAP,
1438c2ecf20Sopenharmony_ci};
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci/*
1468c2ecf20Sopenharmony_ci * Flags for emulate_power_cut in ubi_debug_info
1478c2ecf20Sopenharmony_ci *
1488c2ecf20Sopenharmony_ci * POWER_CUT_EC_WRITE: Emulate a power cut when writing an EC header
1498c2ecf20Sopenharmony_ci * POWER_CUT_VID_WRITE: Emulate a power cut when writing a VID header
1508c2ecf20Sopenharmony_ci */
1518c2ecf20Sopenharmony_cienum {
1528c2ecf20Sopenharmony_ci	POWER_CUT_EC_WRITE = 0x01,
1538c2ecf20Sopenharmony_ci	POWER_CUT_VID_WRITE = 0x02,
1548c2ecf20Sopenharmony_ci};
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci/**
1578c2ecf20Sopenharmony_ci * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
1588c2ecf20Sopenharmony_ci *			   flash.
1598c2ecf20Sopenharmony_ci * @hdr: a pointer to the VID header stored in buffer
1608c2ecf20Sopenharmony_ci * @buffer: underlying buffer
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_cistruct ubi_vid_io_buf {
1638c2ecf20Sopenharmony_ci	struct ubi_vid_hdr *hdr;
1648c2ecf20Sopenharmony_ci	void *buffer;
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci/**
1688c2ecf20Sopenharmony_ci * struct ubi_wl_entry - wear-leveling entry.
1698c2ecf20Sopenharmony_ci * @u.rb: link in the corresponding (free/used) RB-tree
1708c2ecf20Sopenharmony_ci * @u.list: link in the protection queue
1718c2ecf20Sopenharmony_ci * @ec: erase counter
1728c2ecf20Sopenharmony_ci * @pnum: physical eraseblock number
1738c2ecf20Sopenharmony_ci *
1748c2ecf20Sopenharmony_ci * This data structure is used in the WL sub-system. Each physical eraseblock
1758c2ecf20Sopenharmony_ci * has a corresponding &struct wl_entry object which may be kept in different
1768c2ecf20Sopenharmony_ci * RB-trees. See WL sub-system for details.
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_cistruct ubi_wl_entry {
1798c2ecf20Sopenharmony_ci	union {
1808c2ecf20Sopenharmony_ci		struct rb_node rb;
1818c2ecf20Sopenharmony_ci		struct list_head list;
1828c2ecf20Sopenharmony_ci	} u;
1838c2ecf20Sopenharmony_ci	int ec;
1848c2ecf20Sopenharmony_ci	int pnum;
1858c2ecf20Sopenharmony_ci};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci/**
1888c2ecf20Sopenharmony_ci * struct ubi_ltree_entry - an entry in the lock tree.
1898c2ecf20Sopenharmony_ci * @rb: links RB-tree nodes
1908c2ecf20Sopenharmony_ci * @vol_id: volume ID of the locked logical eraseblock
1918c2ecf20Sopenharmony_ci * @lnum: locked logical eraseblock number
1928c2ecf20Sopenharmony_ci * @users: how many tasks are using this logical eraseblock or wait for it
1938c2ecf20Sopenharmony_ci * @mutex: read/write mutex to implement read/write access serialization to
1948c2ecf20Sopenharmony_ci *         the (@vol_id, @lnum) logical eraseblock
1958c2ecf20Sopenharmony_ci *
1968c2ecf20Sopenharmony_ci * This data structure is used in the EBA sub-system to implement per-LEB
1978c2ecf20Sopenharmony_ci * locking. When a logical eraseblock is being locked - corresponding
1988c2ecf20Sopenharmony_ci * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
1998c2ecf20Sopenharmony_ci * See EBA sub-system for details.
2008c2ecf20Sopenharmony_ci */
2018c2ecf20Sopenharmony_cistruct ubi_ltree_entry {
2028c2ecf20Sopenharmony_ci	struct rb_node rb;
2038c2ecf20Sopenharmony_ci	int vol_id;
2048c2ecf20Sopenharmony_ci	int lnum;
2058c2ecf20Sopenharmony_ci	int users;
2068c2ecf20Sopenharmony_ci	struct rw_semaphore mutex;
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci/**
2108c2ecf20Sopenharmony_ci * struct ubi_rename_entry - volume re-name description data structure.
2118c2ecf20Sopenharmony_ci * @new_name_len: new volume name length
2128c2ecf20Sopenharmony_ci * @new_name: new volume name
2138c2ecf20Sopenharmony_ci * @remove: if not zero, this volume should be removed, not re-named
2148c2ecf20Sopenharmony_ci * @desc: descriptor of the volume
2158c2ecf20Sopenharmony_ci * @list: links re-name entries into a list
2168c2ecf20Sopenharmony_ci *
2178c2ecf20Sopenharmony_ci * This data structure is utilized in the multiple volume re-name code. Namely,
2188c2ecf20Sopenharmony_ci * UBI first creates a list of &struct ubi_rename_entry objects from the
2198c2ecf20Sopenharmony_ci * &struct ubi_rnvol_req request object, and then utilizes this list to do all
2208c2ecf20Sopenharmony_ci * the job.
2218c2ecf20Sopenharmony_ci */
2228c2ecf20Sopenharmony_cistruct ubi_rename_entry {
2238c2ecf20Sopenharmony_ci	int new_name_len;
2248c2ecf20Sopenharmony_ci	char new_name[UBI_VOL_NAME_MAX + 1];
2258c2ecf20Sopenharmony_ci	int remove;
2268c2ecf20Sopenharmony_ci	struct ubi_volume_desc *desc;
2278c2ecf20Sopenharmony_ci	struct list_head list;
2288c2ecf20Sopenharmony_ci};
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_cistruct ubi_volume_desc;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci/**
2338c2ecf20Sopenharmony_ci * struct ubi_fastmap_layout - in-memory fastmap data structure.
2348c2ecf20Sopenharmony_ci * @e: PEBs used by the current fastmap
2358c2ecf20Sopenharmony_ci * @to_be_tortured: if non-zero tortured this PEB
2368c2ecf20Sopenharmony_ci * @used_blocks: number of used PEBs
2378c2ecf20Sopenharmony_ci * @max_pool_size: maximal size of the user pool
2388c2ecf20Sopenharmony_ci * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
2398c2ecf20Sopenharmony_ci */
2408c2ecf20Sopenharmony_cistruct ubi_fastmap_layout {
2418c2ecf20Sopenharmony_ci	struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
2428c2ecf20Sopenharmony_ci	int to_be_tortured[UBI_FM_MAX_BLOCKS];
2438c2ecf20Sopenharmony_ci	int used_blocks;
2448c2ecf20Sopenharmony_ci	int max_pool_size;
2458c2ecf20Sopenharmony_ci	int max_wl_pool_size;
2468c2ecf20Sopenharmony_ci};
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci/**
2498c2ecf20Sopenharmony_ci * struct ubi_fm_pool - in-memory fastmap pool
2508c2ecf20Sopenharmony_ci * @pebs: PEBs in this pool
2518c2ecf20Sopenharmony_ci * @used: number of used PEBs
2528c2ecf20Sopenharmony_ci * @size: total number of PEBs in this pool
2538c2ecf20Sopenharmony_ci * @max_size: maximal size of the pool
2548c2ecf20Sopenharmony_ci *
2558c2ecf20Sopenharmony_ci * A pool gets filled with up to max_size.
2568c2ecf20Sopenharmony_ci * If all PEBs within the pool are used a new fastmap will be written
2578c2ecf20Sopenharmony_ci * to the flash and the pool gets refilled with empty PEBs.
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci */
2608c2ecf20Sopenharmony_cistruct ubi_fm_pool {
2618c2ecf20Sopenharmony_ci	int pebs[UBI_FM_MAX_POOL_SIZE];
2628c2ecf20Sopenharmony_ci	int used;
2638c2ecf20Sopenharmony_ci	int size;
2648c2ecf20Sopenharmony_ci	int max_size;
2658c2ecf20Sopenharmony_ci};
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci/**
2688c2ecf20Sopenharmony_ci * struct ubi_eba_leb_desc - EBA logical eraseblock descriptor
2698c2ecf20Sopenharmony_ci * @lnum: the logical eraseblock number
2708c2ecf20Sopenharmony_ci * @pnum: the physical eraseblock where the LEB can be found
2718c2ecf20Sopenharmony_ci *
2728c2ecf20Sopenharmony_ci * This structure is here to hide EBA's internal from other part of the
2738c2ecf20Sopenharmony_ci * UBI implementation.
2748c2ecf20Sopenharmony_ci *
2758c2ecf20Sopenharmony_ci * One can query the position of a LEB by calling ubi_eba_get_ldesc().
2768c2ecf20Sopenharmony_ci */
2778c2ecf20Sopenharmony_cistruct ubi_eba_leb_desc {
2788c2ecf20Sopenharmony_ci	int lnum;
2798c2ecf20Sopenharmony_ci	int pnum;
2808c2ecf20Sopenharmony_ci};
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci/**
2838c2ecf20Sopenharmony_ci * struct ubi_volume - UBI volume description data structure.
2848c2ecf20Sopenharmony_ci * @dev: device object to make use of the the Linux device model
2858c2ecf20Sopenharmony_ci * @cdev: character device object to create character device
2868c2ecf20Sopenharmony_ci * @ubi: reference to the UBI device description object
2878c2ecf20Sopenharmony_ci * @vol_id: volume ID
2888c2ecf20Sopenharmony_ci * @ref_count: volume reference count
2898c2ecf20Sopenharmony_ci * @readers: number of users holding this volume in read-only mode
2908c2ecf20Sopenharmony_ci * @writers: number of users holding this volume in read-write mode
2918c2ecf20Sopenharmony_ci * @exclusive: whether somebody holds this volume in exclusive mode
2928c2ecf20Sopenharmony_ci * @metaonly: whether somebody is altering only meta data of this volume
2938c2ecf20Sopenharmony_ci *
2948c2ecf20Sopenharmony_ci * @reserved_pebs: how many physical eraseblocks are reserved for this volume
2958c2ecf20Sopenharmony_ci * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
2968c2ecf20Sopenharmony_ci * @usable_leb_size: logical eraseblock size without padding
2978c2ecf20Sopenharmony_ci * @used_ebs: how many logical eraseblocks in this volume contain data
2988c2ecf20Sopenharmony_ci * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
2998c2ecf20Sopenharmony_ci * @used_bytes: how many bytes of data this volume contains
3008c2ecf20Sopenharmony_ci * @alignment: volume alignment
3018c2ecf20Sopenharmony_ci * @data_pad: how many bytes are not used at the end of physical eraseblocks to
3028c2ecf20Sopenharmony_ci *            satisfy the requested alignment
3038c2ecf20Sopenharmony_ci * @name_len: volume name length
3048c2ecf20Sopenharmony_ci * @name: volume name
3058c2ecf20Sopenharmony_ci *
3068c2ecf20Sopenharmony_ci * @upd_ebs: how many eraseblocks are expected to be updated
3078c2ecf20Sopenharmony_ci * @ch_lnum: LEB number which is being changing by the atomic LEB change
3088c2ecf20Sopenharmony_ci *           operation
3098c2ecf20Sopenharmony_ci * @upd_bytes: how many bytes are expected to be received for volume update or
3108c2ecf20Sopenharmony_ci *             atomic LEB change
3118c2ecf20Sopenharmony_ci * @upd_received: how many bytes were already received for volume update or
3128c2ecf20Sopenharmony_ci *                atomic LEB change
3138c2ecf20Sopenharmony_ci * @upd_buf: update buffer which is used to collect update data or data for
3148c2ecf20Sopenharmony_ci *           atomic LEB change
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
3178c2ecf20Sopenharmony_ci * @skip_check: %1 if CRC check of this static volume should be skipped.
3188c2ecf20Sopenharmony_ci *		Directly reflects the presence of the
3198c2ecf20Sopenharmony_ci *		%UBI_VTBL_SKIP_CRC_CHECK_FLG flag in the vtbl entry
3208c2ecf20Sopenharmony_ci * @checked: %1 if this static volume was checked
3218c2ecf20Sopenharmony_ci * @corrupted: %1 if the volume is corrupted (static volumes only)
3228c2ecf20Sopenharmony_ci * @upd_marker: %1 if the update marker is set for this volume
3238c2ecf20Sopenharmony_ci * @updating: %1 if the volume is being updated
3248c2ecf20Sopenharmony_ci * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
3258c2ecf20Sopenharmony_ci * @direct_writes: %1 if direct writes are enabled for this volume
3268c2ecf20Sopenharmony_ci *
3278c2ecf20Sopenharmony_ci * @checkmap: bitmap to remember which PEB->LEB mappings got checked,
3288c2ecf20Sopenharmony_ci *            protected by UBI LEB lock tree.
3298c2ecf20Sopenharmony_ci *
3308c2ecf20Sopenharmony_ci * The @corrupted field indicates that the volume's contents is corrupted.
3318c2ecf20Sopenharmony_ci * Since UBI protects only static volumes, this field is not relevant to
3328c2ecf20Sopenharmony_ci * dynamic volumes - it is user's responsibility to assure their data
3338c2ecf20Sopenharmony_ci * integrity.
3348c2ecf20Sopenharmony_ci *
3358c2ecf20Sopenharmony_ci * The @upd_marker flag indicates that this volume is either being updated at
3368c2ecf20Sopenharmony_ci * the moment or is damaged because of an unclean reboot.
3378c2ecf20Sopenharmony_ci */
3388c2ecf20Sopenharmony_cistruct ubi_volume {
3398c2ecf20Sopenharmony_ci	struct device dev;
3408c2ecf20Sopenharmony_ci	struct cdev cdev;
3418c2ecf20Sopenharmony_ci	struct ubi_device *ubi;
3428c2ecf20Sopenharmony_ci	int vol_id;
3438c2ecf20Sopenharmony_ci	int ref_count;
3448c2ecf20Sopenharmony_ci	int readers;
3458c2ecf20Sopenharmony_ci	int writers;
3468c2ecf20Sopenharmony_ci	int exclusive;
3478c2ecf20Sopenharmony_ci	int metaonly;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	int reserved_pebs;
3508c2ecf20Sopenharmony_ci	int vol_type;
3518c2ecf20Sopenharmony_ci	int usable_leb_size;
3528c2ecf20Sopenharmony_ci	int used_ebs;
3538c2ecf20Sopenharmony_ci	int last_eb_bytes;
3548c2ecf20Sopenharmony_ci	long long used_bytes;
3558c2ecf20Sopenharmony_ci	int alignment;
3568c2ecf20Sopenharmony_ci	int data_pad;
3578c2ecf20Sopenharmony_ci	int name_len;
3588c2ecf20Sopenharmony_ci	char name[UBI_VOL_NAME_MAX + 1];
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	int upd_ebs;
3618c2ecf20Sopenharmony_ci	int ch_lnum;
3628c2ecf20Sopenharmony_ci	long long upd_bytes;
3638c2ecf20Sopenharmony_ci	long long upd_received;
3648c2ecf20Sopenharmony_ci	void *upd_buf;
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci	struct ubi_eba_table *eba_tbl;
3678c2ecf20Sopenharmony_ci	unsigned int skip_check:1;
3688c2ecf20Sopenharmony_ci	unsigned int checked:1;
3698c2ecf20Sopenharmony_ci	unsigned int corrupted:1;
3708c2ecf20Sopenharmony_ci	unsigned int upd_marker:1;
3718c2ecf20Sopenharmony_ci	unsigned int updating:1;
3728c2ecf20Sopenharmony_ci	unsigned int changing_leb:1;
3738c2ecf20Sopenharmony_ci	unsigned int direct_writes:1;
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP
3768c2ecf20Sopenharmony_ci	unsigned long *checkmap;
3778c2ecf20Sopenharmony_ci#endif
3788c2ecf20Sopenharmony_ci};
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci/**
3818c2ecf20Sopenharmony_ci * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
3828c2ecf20Sopenharmony_ci * @vol: reference to the corresponding volume description object
3838c2ecf20Sopenharmony_ci * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, %UBI_EXCLUSIVE
3848c2ecf20Sopenharmony_ci * or %UBI_METAONLY)
3858c2ecf20Sopenharmony_ci */
3868c2ecf20Sopenharmony_cistruct ubi_volume_desc {
3878c2ecf20Sopenharmony_ci	struct ubi_volume *vol;
3888c2ecf20Sopenharmony_ci	int mode;
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistruct ubi_wl_entry;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci/**
3948c2ecf20Sopenharmony_ci * struct ubi_debug_info - debugging information for an UBI device.
3958c2ecf20Sopenharmony_ci *
3968c2ecf20Sopenharmony_ci * @chk_gen: if UBI general extra checks are enabled
3978c2ecf20Sopenharmony_ci * @chk_io: if UBI I/O extra checks are enabled
3988c2ecf20Sopenharmony_ci * @chk_fastmap: if UBI fastmap extra checks are enabled
3998c2ecf20Sopenharmony_ci * @disable_bgt: disable the background task for testing purposes
4008c2ecf20Sopenharmony_ci * @emulate_bitflips: emulate bit-flips for testing purposes
4018c2ecf20Sopenharmony_ci * @emulate_io_failures: emulate write/erase failures for testing purposes
4028c2ecf20Sopenharmony_ci * @emulate_power_cut: emulate power cut for testing purposes
4038c2ecf20Sopenharmony_ci * @power_cut_counter: count down for writes left until emulated power cut
4048c2ecf20Sopenharmony_ci * @power_cut_min: minimum number of writes before emulating a power cut
4058c2ecf20Sopenharmony_ci * @power_cut_max: maximum number of writes until emulating a power cut
4068c2ecf20Sopenharmony_ci * @dfs_dir_name: name of debugfs directory containing files of this UBI device
4078c2ecf20Sopenharmony_ci * @dfs_dir: direntry object of the UBI device debugfs directory
4088c2ecf20Sopenharmony_ci * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
4098c2ecf20Sopenharmony_ci * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
4108c2ecf20Sopenharmony_ci * @dfs_chk_fastmap: debugfs knob to enable UBI fastmap extra checks
4118c2ecf20Sopenharmony_ci * @dfs_disable_bgt: debugfs knob to disable the background task
4128c2ecf20Sopenharmony_ci * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
4138c2ecf20Sopenharmony_ci * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
4148c2ecf20Sopenharmony_ci * @dfs_emulate_power_cut: debugfs knob to emulate power cuts
4158c2ecf20Sopenharmony_ci * @dfs_power_cut_min: debugfs knob for minimum writes before power cut
4168c2ecf20Sopenharmony_ci * @dfs_power_cut_max: debugfs knob for maximum writes until power cut
4178c2ecf20Sopenharmony_ci */
4188c2ecf20Sopenharmony_cistruct ubi_debug_info {
4198c2ecf20Sopenharmony_ci	unsigned int chk_gen:1;
4208c2ecf20Sopenharmony_ci	unsigned int chk_io:1;
4218c2ecf20Sopenharmony_ci	unsigned int chk_fastmap:1;
4228c2ecf20Sopenharmony_ci	unsigned int disable_bgt:1;
4238c2ecf20Sopenharmony_ci	unsigned int emulate_bitflips:1;
4248c2ecf20Sopenharmony_ci	unsigned int emulate_io_failures:1;
4258c2ecf20Sopenharmony_ci	unsigned int emulate_power_cut:2;
4268c2ecf20Sopenharmony_ci	unsigned int power_cut_counter;
4278c2ecf20Sopenharmony_ci	unsigned int power_cut_min;
4288c2ecf20Sopenharmony_ci	unsigned int power_cut_max;
4298c2ecf20Sopenharmony_ci	char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
4308c2ecf20Sopenharmony_ci	struct dentry *dfs_dir;
4318c2ecf20Sopenharmony_ci	struct dentry *dfs_chk_gen;
4328c2ecf20Sopenharmony_ci	struct dentry *dfs_chk_io;
4338c2ecf20Sopenharmony_ci	struct dentry *dfs_chk_fastmap;
4348c2ecf20Sopenharmony_ci	struct dentry *dfs_disable_bgt;
4358c2ecf20Sopenharmony_ci	struct dentry *dfs_emulate_bitflips;
4368c2ecf20Sopenharmony_ci	struct dentry *dfs_emulate_io_failures;
4378c2ecf20Sopenharmony_ci	struct dentry *dfs_emulate_power_cut;
4388c2ecf20Sopenharmony_ci	struct dentry *dfs_power_cut_min;
4398c2ecf20Sopenharmony_ci	struct dentry *dfs_power_cut_max;
4408c2ecf20Sopenharmony_ci};
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci/**
4438c2ecf20Sopenharmony_ci * struct ubi_device - UBI device description structure
4448c2ecf20Sopenharmony_ci * @dev: UBI device object to use the the Linux device model
4458c2ecf20Sopenharmony_ci * @cdev: character device object to create character device
4468c2ecf20Sopenharmony_ci * @ubi_num: UBI device number
4478c2ecf20Sopenharmony_ci * @ubi_name: UBI device name
4488c2ecf20Sopenharmony_ci * @vol_count: number of volumes in this UBI device
4498c2ecf20Sopenharmony_ci * @volumes: volumes of this UBI device
4508c2ecf20Sopenharmony_ci * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
4518c2ecf20Sopenharmony_ci *                @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
4528c2ecf20Sopenharmony_ci *                @vol->readers, @vol->writers, @vol->exclusive,
4538c2ecf20Sopenharmony_ci *                @vol->metaonly, @vol->ref_count, @vol->mapping and
4548c2ecf20Sopenharmony_ci *                @vol->eba_tbl.
4558c2ecf20Sopenharmony_ci * @ref_count: count of references on the UBI device
4568c2ecf20Sopenharmony_ci * @image_seq: image sequence number recorded on EC headers
4578c2ecf20Sopenharmony_ci *
4588c2ecf20Sopenharmony_ci * @rsvd_pebs: count of reserved physical eraseblocks
4598c2ecf20Sopenharmony_ci * @avail_pebs: count of available physical eraseblocks
4608c2ecf20Sopenharmony_ci * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
4618c2ecf20Sopenharmony_ci *                 handling
4628c2ecf20Sopenharmony_ci * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
4638c2ecf20Sopenharmony_ci *
4648c2ecf20Sopenharmony_ci * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
4658c2ecf20Sopenharmony_ci *                     of UBI initialization
4668c2ecf20Sopenharmony_ci * @vtbl_slots: how many slots are available in the volume table
4678c2ecf20Sopenharmony_ci * @vtbl_size: size of the volume table in bytes
4688c2ecf20Sopenharmony_ci * @vtbl: in-RAM volume table copy
4698c2ecf20Sopenharmony_ci * @device_mutex: protects on-flash volume table and serializes volume
4708c2ecf20Sopenharmony_ci *                creation, deletion, update, re-size, re-name and set
4718c2ecf20Sopenharmony_ci *                property
4728c2ecf20Sopenharmony_ci *
4738c2ecf20Sopenharmony_ci * @max_ec: current highest erase counter value
4748c2ecf20Sopenharmony_ci * @mean_ec: current mean erase counter value
4758c2ecf20Sopenharmony_ci *
4768c2ecf20Sopenharmony_ci * @global_sqnum: global sequence number
4778c2ecf20Sopenharmony_ci * @ltree_lock: protects the lock tree and @global_sqnum
4788c2ecf20Sopenharmony_ci * @ltree: the lock tree
4798c2ecf20Sopenharmony_ci * @alc_mutex: serializes "atomic LEB change" operations
4808c2ecf20Sopenharmony_ci *
4818c2ecf20Sopenharmony_ci * @fm_disabled: non-zero if fastmap is disabled (default)
4828c2ecf20Sopenharmony_ci * @fm: in-memory data structure of the currently used fastmap
4838c2ecf20Sopenharmony_ci * @fm_pool: in-memory data structure of the fastmap pool
4848c2ecf20Sopenharmony_ci * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
4858c2ecf20Sopenharmony_ci *		sub-system
4868c2ecf20Sopenharmony_ci * @fm_protect: serializes ubi_update_fastmap(), protects @fm_buf and makes sure
4878c2ecf20Sopenharmony_ci * that critical sections cannot be interrupted by ubi_update_fastmap()
4888c2ecf20Sopenharmony_ci * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
4898c2ecf20Sopenharmony_ci * @fm_size: fastmap size in bytes
4908c2ecf20Sopenharmony_ci * @fm_eba_sem: allows ubi_update_fastmap() to block EBA table changes
4918c2ecf20Sopenharmony_ci * @fm_work: fastmap work queue
4928c2ecf20Sopenharmony_ci * @fm_work_scheduled: non-zero if fastmap work was scheduled
4938c2ecf20Sopenharmony_ci * @fast_attach: non-zero if UBI was attached by fastmap
4948c2ecf20Sopenharmony_ci * @fm_anchor: The next anchor PEB to use for fastmap
4958c2ecf20Sopenharmony_ci * @fm_do_produce_anchor: If true produce an anchor PEB in wl
4968c2ecf20Sopenharmony_ci *
4978c2ecf20Sopenharmony_ci * @used: RB-tree of used physical eraseblocks
4988c2ecf20Sopenharmony_ci * @erroneous: RB-tree of erroneous used physical eraseblocks
4998c2ecf20Sopenharmony_ci * @free: RB-tree of free physical eraseblocks
5008c2ecf20Sopenharmony_ci * @free_count: Contains the number of elements in @free
5018c2ecf20Sopenharmony_ci * @scrub: RB-tree of physical eraseblocks which need scrubbing
5028c2ecf20Sopenharmony_ci * @pq: protection queue (contain physical eraseblocks which are temporarily
5038c2ecf20Sopenharmony_ci *      protected from the wear-leveling worker)
5048c2ecf20Sopenharmony_ci * @pq_head: protection queue head
5058c2ecf20Sopenharmony_ci * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
5068c2ecf20Sopenharmony_ci *	     @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
5078c2ecf20Sopenharmony_ci *	     @erroneous, @erroneous_peb_count, @fm_work_scheduled, @fm_pool,
5088c2ecf20Sopenharmony_ci *	     and @fm_wl_pool fields
5098c2ecf20Sopenharmony_ci * @move_mutex: serializes eraseblock moves
5108c2ecf20Sopenharmony_ci * @work_sem: used to wait for all the scheduled works to finish and prevent
5118c2ecf20Sopenharmony_ci * new works from being submitted
5128c2ecf20Sopenharmony_ci * @wl_scheduled: non-zero if the wear-leveling was scheduled
5138c2ecf20Sopenharmony_ci * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
5148c2ecf20Sopenharmony_ci *             physical eraseblock
5158c2ecf20Sopenharmony_ci * @move_from: physical eraseblock from where the data is being moved
5168c2ecf20Sopenharmony_ci * @move_to: physical eraseblock where the data is being moved to
5178c2ecf20Sopenharmony_ci * @move_to_put: if the "to" PEB was put
5188c2ecf20Sopenharmony_ci * @works: list of pending works
5198c2ecf20Sopenharmony_ci * @works_count: count of pending works
5208c2ecf20Sopenharmony_ci * @bgt_thread: background thread description object
5218c2ecf20Sopenharmony_ci * @thread_enabled: if the background thread is enabled
5228c2ecf20Sopenharmony_ci * @bgt_name: background thread name
5238c2ecf20Sopenharmony_ci *
5248c2ecf20Sopenharmony_ci * @flash_size: underlying MTD device size (in bytes)
5258c2ecf20Sopenharmony_ci * @peb_count: count of physical eraseblocks on the MTD device
5268c2ecf20Sopenharmony_ci * @peb_size: physical eraseblock size
5278c2ecf20Sopenharmony_ci * @bad_peb_limit: top limit of expected bad physical eraseblocks
5288c2ecf20Sopenharmony_ci * @bad_peb_count: count of bad physical eraseblocks
5298c2ecf20Sopenharmony_ci * @good_peb_count: count of good physical eraseblocks
5308c2ecf20Sopenharmony_ci * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
5318c2ecf20Sopenharmony_ci *                  used by UBI)
5328c2ecf20Sopenharmony_ci * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
5338c2ecf20Sopenharmony_ci * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
5348c2ecf20Sopenharmony_ci * @min_io_size: minimal input/output unit size of the underlying MTD device
5358c2ecf20Sopenharmony_ci * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
5368c2ecf20Sopenharmony_ci * @ro_mode: if the UBI device is in read-only mode
5378c2ecf20Sopenharmony_ci * @leb_size: logical eraseblock size
5388c2ecf20Sopenharmony_ci * @leb_start: starting offset of logical eraseblocks within physical
5398c2ecf20Sopenharmony_ci *             eraseblocks
5408c2ecf20Sopenharmony_ci * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
5418c2ecf20Sopenharmony_ci * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
5428c2ecf20Sopenharmony_ci * @vid_hdr_offset: starting offset of the volume identifier header (might be
5438c2ecf20Sopenharmony_ci *                  unaligned)
5448c2ecf20Sopenharmony_ci * @vid_hdr_aloffset: starting offset of the VID header aligned to
5458c2ecf20Sopenharmony_ci *                    @hdrs_min_io_size
5468c2ecf20Sopenharmony_ci * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
5478c2ecf20Sopenharmony_ci * @bad_allowed: whether the MTD device admits bad physical eraseblocks or not
5488c2ecf20Sopenharmony_ci * @nor_flash: non-zero if working on top of NOR flash
5498c2ecf20Sopenharmony_ci * @max_write_size: maximum amount of bytes the underlying flash can write at a
5508c2ecf20Sopenharmony_ci *                  time (MTD write buffer size)
5518c2ecf20Sopenharmony_ci * @mtd: MTD device descriptor
5528c2ecf20Sopenharmony_ci *
5538c2ecf20Sopenharmony_ci * @peb_buf: a buffer of PEB size used for different purposes
5548c2ecf20Sopenharmony_ci * @buf_mutex: protects @peb_buf
5558c2ecf20Sopenharmony_ci * @ckvol_mutex: serializes static volume checking when opening
5568c2ecf20Sopenharmony_ci *
5578c2ecf20Sopenharmony_ci * @dbg: debugging information for this UBI device
5588c2ecf20Sopenharmony_ci */
5598c2ecf20Sopenharmony_cistruct ubi_device {
5608c2ecf20Sopenharmony_ci	struct cdev cdev;
5618c2ecf20Sopenharmony_ci	struct device dev;
5628c2ecf20Sopenharmony_ci	int ubi_num;
5638c2ecf20Sopenharmony_ci	char ubi_name[sizeof(UBI_NAME_STR)+5];
5648c2ecf20Sopenharmony_ci	int vol_count;
5658c2ecf20Sopenharmony_ci	struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
5668c2ecf20Sopenharmony_ci	spinlock_t volumes_lock;
5678c2ecf20Sopenharmony_ci	int ref_count;
5688c2ecf20Sopenharmony_ci	int image_seq;
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_ci	int rsvd_pebs;
5718c2ecf20Sopenharmony_ci	int avail_pebs;
5728c2ecf20Sopenharmony_ci	int beb_rsvd_pebs;
5738c2ecf20Sopenharmony_ci	int beb_rsvd_level;
5748c2ecf20Sopenharmony_ci	int bad_peb_limit;
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	int autoresize_vol_id;
5778c2ecf20Sopenharmony_ci	int vtbl_slots;
5788c2ecf20Sopenharmony_ci	int vtbl_size;
5798c2ecf20Sopenharmony_ci	struct ubi_vtbl_record *vtbl;
5808c2ecf20Sopenharmony_ci	struct mutex device_mutex;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci	int max_ec;
5838c2ecf20Sopenharmony_ci	/* Note, mean_ec is not updated run-time - should be fixed */
5848c2ecf20Sopenharmony_ci	int mean_ec;
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	/* EBA sub-system's stuff */
5878c2ecf20Sopenharmony_ci	unsigned long long global_sqnum;
5888c2ecf20Sopenharmony_ci	spinlock_t ltree_lock;
5898c2ecf20Sopenharmony_ci	struct rb_root ltree;
5908c2ecf20Sopenharmony_ci	struct mutex alc_mutex;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/* Fastmap stuff */
5938c2ecf20Sopenharmony_ci	int fm_disabled;
5948c2ecf20Sopenharmony_ci	struct ubi_fastmap_layout *fm;
5958c2ecf20Sopenharmony_ci	struct ubi_fm_pool fm_pool;
5968c2ecf20Sopenharmony_ci	struct ubi_fm_pool fm_wl_pool;
5978c2ecf20Sopenharmony_ci	struct rw_semaphore fm_eba_sem;
5988c2ecf20Sopenharmony_ci	struct rw_semaphore fm_protect;
5998c2ecf20Sopenharmony_ci	void *fm_buf;
6008c2ecf20Sopenharmony_ci	size_t fm_size;
6018c2ecf20Sopenharmony_ci	struct work_struct fm_work;
6028c2ecf20Sopenharmony_ci	int fm_work_scheduled;
6038c2ecf20Sopenharmony_ci	int fast_attach;
6048c2ecf20Sopenharmony_ci	struct ubi_wl_entry *fm_anchor;
6058c2ecf20Sopenharmony_ci	int fm_do_produce_anchor;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Wear-leveling sub-system's stuff */
6088c2ecf20Sopenharmony_ci	struct rb_root used;
6098c2ecf20Sopenharmony_ci	struct rb_root erroneous;
6108c2ecf20Sopenharmony_ci	struct rb_root free;
6118c2ecf20Sopenharmony_ci	int free_count;
6128c2ecf20Sopenharmony_ci	struct rb_root scrub;
6138c2ecf20Sopenharmony_ci	struct list_head pq[UBI_PROT_QUEUE_LEN];
6148c2ecf20Sopenharmony_ci	int pq_head;
6158c2ecf20Sopenharmony_ci	spinlock_t wl_lock;
6168c2ecf20Sopenharmony_ci	struct mutex move_mutex;
6178c2ecf20Sopenharmony_ci	struct rw_semaphore work_sem;
6188c2ecf20Sopenharmony_ci	int wl_scheduled;
6198c2ecf20Sopenharmony_ci	struct ubi_wl_entry **lookuptbl;
6208c2ecf20Sopenharmony_ci	struct ubi_wl_entry *move_from;
6218c2ecf20Sopenharmony_ci	struct ubi_wl_entry *move_to;
6228c2ecf20Sopenharmony_ci	int move_to_put;
6238c2ecf20Sopenharmony_ci	struct list_head works;
6248c2ecf20Sopenharmony_ci	int works_count;
6258c2ecf20Sopenharmony_ci	struct task_struct *bgt_thread;
6268c2ecf20Sopenharmony_ci	int thread_enabled;
6278c2ecf20Sopenharmony_ci	char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	/* I/O sub-system's stuff */
6308c2ecf20Sopenharmony_ci	long long flash_size;
6318c2ecf20Sopenharmony_ci	int peb_count;
6328c2ecf20Sopenharmony_ci	int peb_size;
6338c2ecf20Sopenharmony_ci	int bad_peb_count;
6348c2ecf20Sopenharmony_ci	int good_peb_count;
6358c2ecf20Sopenharmony_ci	int corr_peb_count;
6368c2ecf20Sopenharmony_ci	int erroneous_peb_count;
6378c2ecf20Sopenharmony_ci	int max_erroneous;
6388c2ecf20Sopenharmony_ci	int min_io_size;
6398c2ecf20Sopenharmony_ci	int hdrs_min_io_size;
6408c2ecf20Sopenharmony_ci	int ro_mode;
6418c2ecf20Sopenharmony_ci	int leb_size;
6428c2ecf20Sopenharmony_ci	int leb_start;
6438c2ecf20Sopenharmony_ci	int ec_hdr_alsize;
6448c2ecf20Sopenharmony_ci	int vid_hdr_alsize;
6458c2ecf20Sopenharmony_ci	int vid_hdr_offset;
6468c2ecf20Sopenharmony_ci	int vid_hdr_aloffset;
6478c2ecf20Sopenharmony_ci	int vid_hdr_shift;
6488c2ecf20Sopenharmony_ci	unsigned int bad_allowed:1;
6498c2ecf20Sopenharmony_ci	unsigned int nor_flash:1;
6508c2ecf20Sopenharmony_ci	int max_write_size;
6518c2ecf20Sopenharmony_ci	struct mtd_info *mtd;
6528c2ecf20Sopenharmony_ci
6538c2ecf20Sopenharmony_ci	void *peb_buf;
6548c2ecf20Sopenharmony_ci	struct mutex buf_mutex;
6558c2ecf20Sopenharmony_ci	struct mutex ckvol_mutex;
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	struct ubi_debug_info dbg;
6588c2ecf20Sopenharmony_ci};
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci/**
6618c2ecf20Sopenharmony_ci * struct ubi_ainf_peb - attach information about a physical eraseblock.
6628c2ecf20Sopenharmony_ci * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
6638c2ecf20Sopenharmony_ci * @pnum: physical eraseblock number
6648c2ecf20Sopenharmony_ci * @vol_id: ID of the volume this LEB belongs to
6658c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number
6668c2ecf20Sopenharmony_ci * @scrub: if this physical eraseblock needs scrubbing
6678c2ecf20Sopenharmony_ci * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
6688c2ecf20Sopenharmony_ci * @sqnum: sequence number
6698c2ecf20Sopenharmony_ci * @u: unions RB-tree or @list links
6708c2ecf20Sopenharmony_ci * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
6718c2ecf20Sopenharmony_ci * @u.list: link in one of the eraseblock lists
6728c2ecf20Sopenharmony_ci *
6738c2ecf20Sopenharmony_ci * One object of this type is allocated for each physical eraseblock when
6748c2ecf20Sopenharmony_ci * attaching an MTD device. Note, if this PEB does not belong to any LEB /
6758c2ecf20Sopenharmony_ci * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
6768c2ecf20Sopenharmony_ci */
6778c2ecf20Sopenharmony_cistruct ubi_ainf_peb {
6788c2ecf20Sopenharmony_ci	int ec;
6798c2ecf20Sopenharmony_ci	int pnum;
6808c2ecf20Sopenharmony_ci	int vol_id;
6818c2ecf20Sopenharmony_ci	int lnum;
6828c2ecf20Sopenharmony_ci	unsigned int scrub:1;
6838c2ecf20Sopenharmony_ci	unsigned int copy_flag:1;
6848c2ecf20Sopenharmony_ci	unsigned long long sqnum;
6858c2ecf20Sopenharmony_ci	union {
6868c2ecf20Sopenharmony_ci		struct rb_node rb;
6878c2ecf20Sopenharmony_ci		struct list_head list;
6888c2ecf20Sopenharmony_ci	} u;
6898c2ecf20Sopenharmony_ci};
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci/**
6928c2ecf20Sopenharmony_ci * struct ubi_ainf_volume - attaching information about a volume.
6938c2ecf20Sopenharmony_ci * @vol_id: volume ID
6948c2ecf20Sopenharmony_ci * @highest_lnum: highest logical eraseblock number in this volume
6958c2ecf20Sopenharmony_ci * @leb_count: number of logical eraseblocks in this volume
6968c2ecf20Sopenharmony_ci * @vol_type: volume type
6978c2ecf20Sopenharmony_ci * @used_ebs: number of used logical eraseblocks in this volume (only for
6988c2ecf20Sopenharmony_ci *            static volumes)
6998c2ecf20Sopenharmony_ci * @last_data_size: amount of data in the last logical eraseblock of this
7008c2ecf20Sopenharmony_ci *                  volume (always equivalent to the usable logical eraseblock
7018c2ecf20Sopenharmony_ci *                  size in case of dynamic volumes)
7028c2ecf20Sopenharmony_ci * @data_pad: how many bytes at the end of logical eraseblocks of this volume
7038c2ecf20Sopenharmony_ci *            are not used (due to volume alignment)
7048c2ecf20Sopenharmony_ci * @compat: compatibility flags of this volume
7058c2ecf20Sopenharmony_ci * @rb: link in the volume RB-tree
7068c2ecf20Sopenharmony_ci * @root: root of the RB-tree containing all the eraseblock belonging to this
7078c2ecf20Sopenharmony_ci *        volume (&struct ubi_ainf_peb objects)
7088c2ecf20Sopenharmony_ci *
7098c2ecf20Sopenharmony_ci * One object of this type is allocated for each volume when attaching an MTD
7108c2ecf20Sopenharmony_ci * device.
7118c2ecf20Sopenharmony_ci */
7128c2ecf20Sopenharmony_cistruct ubi_ainf_volume {
7138c2ecf20Sopenharmony_ci	int vol_id;
7148c2ecf20Sopenharmony_ci	int highest_lnum;
7158c2ecf20Sopenharmony_ci	int leb_count;
7168c2ecf20Sopenharmony_ci	int vol_type;
7178c2ecf20Sopenharmony_ci	int used_ebs;
7188c2ecf20Sopenharmony_ci	int last_data_size;
7198c2ecf20Sopenharmony_ci	int data_pad;
7208c2ecf20Sopenharmony_ci	int compat;
7218c2ecf20Sopenharmony_ci	struct rb_node rb;
7228c2ecf20Sopenharmony_ci	struct rb_root root;
7238c2ecf20Sopenharmony_ci};
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci/**
7268c2ecf20Sopenharmony_ci * struct ubi_attach_info - MTD device attaching information.
7278c2ecf20Sopenharmony_ci * @volumes: root of the volume RB-tree
7288c2ecf20Sopenharmony_ci * @corr: list of corrupted physical eraseblocks
7298c2ecf20Sopenharmony_ci * @free: list of free physical eraseblocks
7308c2ecf20Sopenharmony_ci * @erase: list of physical eraseblocks which have to be erased
7318c2ecf20Sopenharmony_ci * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
7328c2ecf20Sopenharmony_ci *         those belonging to "preserve"-compatible internal volumes)
7338c2ecf20Sopenharmony_ci * @fastmap: list of physical eraseblocks which relate to fastmap (e.g.,
7348c2ecf20Sopenharmony_ci *           eraseblocks of the current and not yet erased old fastmap blocks)
7358c2ecf20Sopenharmony_ci * @corr_peb_count: count of PEBs in the @corr list
7368c2ecf20Sopenharmony_ci * @empty_peb_count: count of PEBs which are presumably empty (contain only
7378c2ecf20Sopenharmony_ci *                   0xFF bytes)
7388c2ecf20Sopenharmony_ci * @alien_peb_count: count of PEBs in the @alien list
7398c2ecf20Sopenharmony_ci * @bad_peb_count: count of bad physical eraseblocks
7408c2ecf20Sopenharmony_ci * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
7418c2ecf20Sopenharmony_ci *                       as bad yet, but which look like bad
7428c2ecf20Sopenharmony_ci * @vols_found: number of volumes found
7438c2ecf20Sopenharmony_ci * @highest_vol_id: highest volume ID
7448c2ecf20Sopenharmony_ci * @is_empty: flag indicating whether the MTD device is empty or not
7458c2ecf20Sopenharmony_ci * @force_full_scan: flag indicating whether we need to do a full scan and drop
7468c2ecf20Sopenharmony_ci		     all existing Fastmap data structures
7478c2ecf20Sopenharmony_ci * @min_ec: lowest erase counter value
7488c2ecf20Sopenharmony_ci * @max_ec: highest erase counter value
7498c2ecf20Sopenharmony_ci * @max_sqnum: highest sequence number value
7508c2ecf20Sopenharmony_ci * @mean_ec: mean erase counter value
7518c2ecf20Sopenharmony_ci * @ec_sum: a temporary variable used when calculating @mean_ec
7528c2ecf20Sopenharmony_ci * @ec_count: a temporary variable used when calculating @mean_ec
7538c2ecf20Sopenharmony_ci * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
7548c2ecf20Sopenharmony_ci * @ech: temporary EC header. Only available during scan
7558c2ecf20Sopenharmony_ci * @vidh: temporary VID buffer. Only available during scan
7568c2ecf20Sopenharmony_ci *
7578c2ecf20Sopenharmony_ci * This data structure contains the result of attaching an MTD device and may
7588c2ecf20Sopenharmony_ci * be used by other UBI sub-systems to build final UBI data structures, further
7598c2ecf20Sopenharmony_ci * error-recovery and so on.
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_cistruct ubi_attach_info {
7628c2ecf20Sopenharmony_ci	struct rb_root volumes;
7638c2ecf20Sopenharmony_ci	struct list_head corr;
7648c2ecf20Sopenharmony_ci	struct list_head free;
7658c2ecf20Sopenharmony_ci	struct list_head erase;
7668c2ecf20Sopenharmony_ci	struct list_head alien;
7678c2ecf20Sopenharmony_ci	struct list_head fastmap;
7688c2ecf20Sopenharmony_ci	int corr_peb_count;
7698c2ecf20Sopenharmony_ci	int empty_peb_count;
7708c2ecf20Sopenharmony_ci	int alien_peb_count;
7718c2ecf20Sopenharmony_ci	int bad_peb_count;
7728c2ecf20Sopenharmony_ci	int maybe_bad_peb_count;
7738c2ecf20Sopenharmony_ci	int vols_found;
7748c2ecf20Sopenharmony_ci	int highest_vol_id;
7758c2ecf20Sopenharmony_ci	int is_empty;
7768c2ecf20Sopenharmony_ci	int force_full_scan;
7778c2ecf20Sopenharmony_ci	int min_ec;
7788c2ecf20Sopenharmony_ci	int max_ec;
7798c2ecf20Sopenharmony_ci	unsigned long long max_sqnum;
7808c2ecf20Sopenharmony_ci	int mean_ec;
7818c2ecf20Sopenharmony_ci	uint64_t ec_sum;
7828c2ecf20Sopenharmony_ci	int ec_count;
7838c2ecf20Sopenharmony_ci	struct kmem_cache *aeb_slab_cache;
7848c2ecf20Sopenharmony_ci	struct ubi_ec_hdr *ech;
7858c2ecf20Sopenharmony_ci	struct ubi_vid_io_buf *vidb;
7868c2ecf20Sopenharmony_ci};
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci/**
7898c2ecf20Sopenharmony_ci * struct ubi_work - UBI work description data structure.
7908c2ecf20Sopenharmony_ci * @list: a link in the list of pending works
7918c2ecf20Sopenharmony_ci * @func: worker function
7928c2ecf20Sopenharmony_ci * @e: physical eraseblock to erase
7938c2ecf20Sopenharmony_ci * @vol_id: the volume ID on which this erasure is being performed
7948c2ecf20Sopenharmony_ci * @lnum: the logical eraseblock number
7958c2ecf20Sopenharmony_ci * @torture: if the physical eraseblock has to be tortured
7968c2ecf20Sopenharmony_ci *
7978c2ecf20Sopenharmony_ci * The @func pointer points to the worker function. If the @shutdown argument is
7988c2ecf20Sopenharmony_ci * not zero, the worker has to free the resources and exit immediately as the
7998c2ecf20Sopenharmony_ci * WL sub-system is shutting down.
8008c2ecf20Sopenharmony_ci * The worker has to return zero in case of success and a negative error code in
8018c2ecf20Sopenharmony_ci * case of failure.
8028c2ecf20Sopenharmony_ci */
8038c2ecf20Sopenharmony_cistruct ubi_work {
8048c2ecf20Sopenharmony_ci	struct list_head list;
8058c2ecf20Sopenharmony_ci	int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
8068c2ecf20Sopenharmony_ci	/* The below fields are only relevant to erasure works */
8078c2ecf20Sopenharmony_ci	struct ubi_wl_entry *e;
8088c2ecf20Sopenharmony_ci	int vol_id;
8098c2ecf20Sopenharmony_ci	int lnum;
8108c2ecf20Sopenharmony_ci	int torture;
8118c2ecf20Sopenharmony_ci};
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci#include "debug.h"
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ciextern struct kmem_cache *ubi_wl_entry_slab;
8168c2ecf20Sopenharmony_ciextern const struct file_operations ubi_ctrl_cdev_operations;
8178c2ecf20Sopenharmony_ciextern const struct file_operations ubi_cdev_operations;
8188c2ecf20Sopenharmony_ciextern const struct file_operations ubi_vol_cdev_operations;
8198c2ecf20Sopenharmony_ciextern struct class ubi_class;
8208c2ecf20Sopenharmony_ciextern struct mutex ubi_devices_mutex;
8218c2ecf20Sopenharmony_ciextern struct blocking_notifier_head ubi_notifiers;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci/* attach.c */
8248c2ecf20Sopenharmony_cistruct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
8258c2ecf20Sopenharmony_ci				   int ec);
8268c2ecf20Sopenharmony_civoid ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
8278c2ecf20Sopenharmony_ciint ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
8288c2ecf20Sopenharmony_ci		  int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
8298c2ecf20Sopenharmony_cistruct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);
8308c2ecf20Sopenharmony_cistruct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
8318c2ecf20Sopenharmony_ci				    int vol_id);
8328c2ecf20Sopenharmony_civoid ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
8338c2ecf20Sopenharmony_cistruct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
8348c2ecf20Sopenharmony_ci				       struct ubi_attach_info *ai);
8358c2ecf20Sopenharmony_ciint ubi_attach(struct ubi_device *ubi, int force_scan);
8368c2ecf20Sopenharmony_civoid ubi_destroy_ai(struct ubi_attach_info *ai);
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci/* vtbl.c */
8398c2ecf20Sopenharmony_ciint ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
8408c2ecf20Sopenharmony_ci			   struct ubi_vtbl_record *vtbl_rec);
8418c2ecf20Sopenharmony_ciint ubi_vtbl_rename_volumes(struct ubi_device *ubi,
8428c2ecf20Sopenharmony_ci			    struct list_head *rename_list);
8438c2ecf20Sopenharmony_ciint ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci/* vmt.c */
8468c2ecf20Sopenharmony_ciint ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
8478c2ecf20Sopenharmony_ciint ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
8488c2ecf20Sopenharmony_ciint ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
8498c2ecf20Sopenharmony_ciint ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
8508c2ecf20Sopenharmony_ciint ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
8518c2ecf20Sopenharmony_civoid ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci/* upd.c */
8548c2ecf20Sopenharmony_ciint ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
8558c2ecf20Sopenharmony_ci		     long long bytes);
8568c2ecf20Sopenharmony_ciint ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
8578c2ecf20Sopenharmony_ci			 const void __user *buf, int count);
8588c2ecf20Sopenharmony_ciint ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
8598c2ecf20Sopenharmony_ci			 const struct ubi_leb_change_req *req);
8608c2ecf20Sopenharmony_ciint ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
8618c2ecf20Sopenharmony_ci			     const void __user *buf, int count);
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci/* misc.c */
8648c2ecf20Sopenharmony_ciint ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
8658c2ecf20Sopenharmony_ci		      int length);
8668c2ecf20Sopenharmony_ciint ubi_check_volume(struct ubi_device *ubi, int vol_id);
8678c2ecf20Sopenharmony_civoid ubi_update_reserved(struct ubi_device *ubi);
8688c2ecf20Sopenharmony_civoid ubi_calculate_reserved(struct ubi_device *ubi);
8698c2ecf20Sopenharmony_ciint ubi_check_pattern(const void *buf, uint8_t patt, int size);
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_cistatic inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
8728c2ecf20Sopenharmony_ci{
8738c2ecf20Sopenharmony_ci	return lnum >= 0 && lnum < vol->reserved_pebs;
8748c2ecf20Sopenharmony_ci}
8758c2ecf20Sopenharmony_ci
8768c2ecf20Sopenharmony_ci/* eba.c */
8778c2ecf20Sopenharmony_cistruct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
8788c2ecf20Sopenharmony_ci					   int nentries);
8798c2ecf20Sopenharmony_civoid ubi_eba_destroy_table(struct ubi_eba_table *tbl);
8808c2ecf20Sopenharmony_civoid ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
8818c2ecf20Sopenharmony_ci			int nentries);
8828c2ecf20Sopenharmony_civoid ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl);
8838c2ecf20Sopenharmony_civoid ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
8848c2ecf20Sopenharmony_ci		       struct ubi_eba_leb_desc *ldesc);
8858c2ecf20Sopenharmony_cibool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum);
8868c2ecf20Sopenharmony_ciint ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
8878c2ecf20Sopenharmony_ci		      int lnum);
8888c2ecf20Sopenharmony_ciint ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
8898c2ecf20Sopenharmony_ci		     void *buf, int offset, int len, int check);
8908c2ecf20Sopenharmony_ciint ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
8918c2ecf20Sopenharmony_ci			struct ubi_sgl *sgl, int lnum, int offset, int len,
8928c2ecf20Sopenharmony_ci			int check);
8938c2ecf20Sopenharmony_ciint ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
8948c2ecf20Sopenharmony_ci		      const void *buf, int offset, int len);
8958c2ecf20Sopenharmony_ciint ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
8968c2ecf20Sopenharmony_ci			 int lnum, const void *buf, int len, int used_ebs);
8978c2ecf20Sopenharmony_ciint ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
8988c2ecf20Sopenharmony_ci			      int lnum, const void *buf, int len);
8998c2ecf20Sopenharmony_ciint ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
9008c2ecf20Sopenharmony_ci		     struct ubi_vid_io_buf *vidb);
9018c2ecf20Sopenharmony_ciint ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
9028c2ecf20Sopenharmony_ciunsigned long long ubi_next_sqnum(struct ubi_device *ubi);
9038c2ecf20Sopenharmony_ciint self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
9048c2ecf20Sopenharmony_ci		   struct ubi_attach_info *ai_scan);
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci/* wl.c */
9078c2ecf20Sopenharmony_ciint ubi_wl_get_peb(struct ubi_device *ubi);
9088c2ecf20Sopenharmony_ciint ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
9098c2ecf20Sopenharmony_ci		   int pnum, int torture);
9108c2ecf20Sopenharmony_ciint ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
9118c2ecf20Sopenharmony_ciint ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
9128c2ecf20Sopenharmony_ciint ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
9138c2ecf20Sopenharmony_civoid ubi_wl_close(struct ubi_device *ubi);
9148c2ecf20Sopenharmony_ciint ubi_thread(void *u);
9158c2ecf20Sopenharmony_cistruct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
9168c2ecf20Sopenharmony_ciint ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
9178c2ecf20Sopenharmony_ci		      int lnum, int torture);
9188c2ecf20Sopenharmony_ciint ubi_is_erase_work(struct ubi_work *wrk);
9198c2ecf20Sopenharmony_civoid ubi_refill_pools(struct ubi_device *ubi);
9208c2ecf20Sopenharmony_ciint ubi_ensure_anchor_pebs(struct ubi_device *ubi);
9218c2ecf20Sopenharmony_ciint ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
9228c2ecf20Sopenharmony_ci
9238c2ecf20Sopenharmony_ci/* io.c */
9248c2ecf20Sopenharmony_ciint ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
9258c2ecf20Sopenharmony_ci		int len);
9268c2ecf20Sopenharmony_ciint ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
9278c2ecf20Sopenharmony_ci		 int len);
9288c2ecf20Sopenharmony_ciint ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
9298c2ecf20Sopenharmony_ciint ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
9308c2ecf20Sopenharmony_ciint ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
9318c2ecf20Sopenharmony_ciint ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
9328c2ecf20Sopenharmony_ci		       struct ubi_ec_hdr *ec_hdr, int verbose);
9338c2ecf20Sopenharmony_ciint ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
9348c2ecf20Sopenharmony_ci			struct ubi_ec_hdr *ec_hdr);
9358c2ecf20Sopenharmony_ciint ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
9368c2ecf20Sopenharmony_ci			struct ubi_vid_io_buf *vidb, int verbose);
9378c2ecf20Sopenharmony_ciint ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
9388c2ecf20Sopenharmony_ci			 struct ubi_vid_io_buf *vidb);
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci/* build.c */
9418c2ecf20Sopenharmony_ciint ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
9428c2ecf20Sopenharmony_ci		       int vid_hdr_offset, int max_beb_per1024);
9438c2ecf20Sopenharmony_ciint ubi_detach_mtd_dev(int ubi_num, int anyway);
9448c2ecf20Sopenharmony_cistruct ubi_device *ubi_get_device(int ubi_num);
9458c2ecf20Sopenharmony_civoid ubi_put_device(struct ubi_device *ubi);
9468c2ecf20Sopenharmony_cistruct ubi_device *ubi_get_by_major(int major);
9478c2ecf20Sopenharmony_ciint ubi_major2num(int major);
9488c2ecf20Sopenharmony_ciint ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
9498c2ecf20Sopenharmony_ci		      int ntype);
9508c2ecf20Sopenharmony_ciint ubi_notify_all(struct ubi_device *ubi, int ntype,
9518c2ecf20Sopenharmony_ci		   struct notifier_block *nb);
9528c2ecf20Sopenharmony_ciint ubi_enumerate_volumes(struct notifier_block *nb);
9538c2ecf20Sopenharmony_civoid ubi_free_all_volumes(struct ubi_device *ubi);
9548c2ecf20Sopenharmony_civoid ubi_free_internal_volumes(struct ubi_device *ubi);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci/* kapi.c */
9578c2ecf20Sopenharmony_civoid ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
9588c2ecf20Sopenharmony_civoid ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
9598c2ecf20Sopenharmony_ci			    struct ubi_volume_info *vi);
9608c2ecf20Sopenharmony_ci/* scan.c */
9618c2ecf20Sopenharmony_ciint ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
9628c2ecf20Sopenharmony_ci		      int pnum, const struct ubi_vid_hdr *vid_hdr);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci/* fastmap.c */
9658c2ecf20Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP
9668c2ecf20Sopenharmony_cisize_t ubi_calc_fm_size(struct ubi_device *ubi);
9678c2ecf20Sopenharmony_ciint ubi_update_fastmap(struct ubi_device *ubi);
9688c2ecf20Sopenharmony_ciint ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
9698c2ecf20Sopenharmony_ci		     struct ubi_attach_info *scan_ai);
9708c2ecf20Sopenharmony_ciint ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count);
9718c2ecf20Sopenharmony_civoid ubi_fastmap_destroy_checkmap(struct ubi_volume *vol);
9728c2ecf20Sopenharmony_ci#else
9738c2ecf20Sopenharmony_cistatic inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
9748c2ecf20Sopenharmony_cistatic inline int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) { return 0; }
9758c2ecf20Sopenharmony_cistatic inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}
9768c2ecf20Sopenharmony_ci#endif
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci/* block.c */
9798c2ecf20Sopenharmony_ci#ifdef CONFIG_MTD_UBI_BLOCK
9808c2ecf20Sopenharmony_ciint ubiblock_init(void);
9818c2ecf20Sopenharmony_civoid ubiblock_exit(void);
9828c2ecf20Sopenharmony_ciint ubiblock_create(struct ubi_volume_info *vi);
9838c2ecf20Sopenharmony_ciint ubiblock_remove(struct ubi_volume_info *vi);
9848c2ecf20Sopenharmony_ci#else
9858c2ecf20Sopenharmony_cistatic inline int ubiblock_init(void) { return 0; }
9868c2ecf20Sopenharmony_cistatic inline void ubiblock_exit(void) {}
9878c2ecf20Sopenharmony_cistatic inline int ubiblock_create(struct ubi_volume_info *vi)
9888c2ecf20Sopenharmony_ci{
9898c2ecf20Sopenharmony_ci	return -ENOSYS;
9908c2ecf20Sopenharmony_ci}
9918c2ecf20Sopenharmony_cistatic inline int ubiblock_remove(struct ubi_volume_info *vi)
9928c2ecf20Sopenharmony_ci{
9938c2ecf20Sopenharmony_ci	return -ENOSYS;
9948c2ecf20Sopenharmony_ci}
9958c2ecf20Sopenharmony_ci#endif
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci/*
9988c2ecf20Sopenharmony_ci * ubi_for_each_free_peb - walk the UBI free RB tree.
9998c2ecf20Sopenharmony_ci * @ubi: UBI device description object
10008c2ecf20Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
10018c2ecf20Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
10028c2ecf20Sopenharmony_ci */
10038c2ecf20Sopenharmony_ci#define ubi_for_each_free_peb(ubi, e, tmp_rb)	\
10048c2ecf20Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
10058c2ecf20Sopenharmony_ci
10068c2ecf20Sopenharmony_ci/*
10078c2ecf20Sopenharmony_ci * ubi_for_each_used_peb - walk the UBI used RB tree.
10088c2ecf20Sopenharmony_ci * @ubi: UBI device description object
10098c2ecf20Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
10108c2ecf20Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
10118c2ecf20Sopenharmony_ci */
10128c2ecf20Sopenharmony_ci#define ubi_for_each_used_peb(ubi, e, tmp_rb)	\
10138c2ecf20Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci/*
10168c2ecf20Sopenharmony_ci * ubi_for_each_scub_peb - walk the UBI scub RB tree.
10178c2ecf20Sopenharmony_ci * @ubi: UBI device description object
10188c2ecf20Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
10198c2ecf20Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
10208c2ecf20Sopenharmony_ci */
10218c2ecf20Sopenharmony_ci#define ubi_for_each_scrub_peb(ubi, e, tmp_rb)	\
10228c2ecf20Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
10238c2ecf20Sopenharmony_ci
10248c2ecf20Sopenharmony_ci/*
10258c2ecf20Sopenharmony_ci * ubi_for_each_protected_peb - walk the UBI protection queue.
10268c2ecf20Sopenharmony_ci * @ubi: UBI device description object
10278c2ecf20Sopenharmony_ci * @i: a integer used as counter
10288c2ecf20Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
10298c2ecf20Sopenharmony_ci */
10308c2ecf20Sopenharmony_ci#define ubi_for_each_protected_peb(ubi, i, e)	\
10318c2ecf20Sopenharmony_ci	for ((i) = 0; (i) < UBI_PROT_QUEUE_LEN; (i)++)	\
10328c2ecf20Sopenharmony_ci		list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
10338c2ecf20Sopenharmony_ci
10348c2ecf20Sopenharmony_ci/*
10358c2ecf20Sopenharmony_ci * ubi_rb_for_each_entry - walk an RB-tree.
10368c2ecf20Sopenharmony_ci * @rb: a pointer to type 'struct rb_node' to use as a loop counter
10378c2ecf20Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
10388c2ecf20Sopenharmony_ci * @root: RB-tree's root
10398c2ecf20Sopenharmony_ci * @member: the name of the 'struct rb_node' within the RB-tree entry
10408c2ecf20Sopenharmony_ci */
10418c2ecf20Sopenharmony_ci#define ubi_rb_for_each_entry(rb, pos, root, member)                         \
10428c2ecf20Sopenharmony_ci	for (rb = rb_first(root),                                            \
10438c2ecf20Sopenharmony_ci	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL);     \
10448c2ecf20Sopenharmony_ci	     rb;                                                             \
10458c2ecf20Sopenharmony_ci	     rb = rb_next(rb),                                               \
10468c2ecf20Sopenharmony_ci	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
10478c2ecf20Sopenharmony_ci
10488c2ecf20Sopenharmony_ci/*
10498c2ecf20Sopenharmony_ci * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
10508c2ecf20Sopenharmony_ci *
10518c2ecf20Sopenharmony_ci * @av: volume attaching information
10528c2ecf20Sopenharmony_ci * @aeb: attaching eraseblock information
10538c2ecf20Sopenharmony_ci * @list: the list to move to
10548c2ecf20Sopenharmony_ci */
10558c2ecf20Sopenharmony_cistatic inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
10568c2ecf20Sopenharmony_ci					 struct ubi_ainf_peb *aeb,
10578c2ecf20Sopenharmony_ci					 struct list_head *list)
10588c2ecf20Sopenharmony_ci{
10598c2ecf20Sopenharmony_ci		rb_erase(&aeb->u.rb, &av->root);
10608c2ecf20Sopenharmony_ci		list_add_tail(&aeb->u.list, list);
10618c2ecf20Sopenharmony_ci}
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci/**
10648c2ecf20Sopenharmony_ci * ubi_init_vid_buf - Initialize a VID buffer
10658c2ecf20Sopenharmony_ci * @ubi: the UBI device
10668c2ecf20Sopenharmony_ci * @vidb: the VID buffer to initialize
10678c2ecf20Sopenharmony_ci * @buf: the underlying buffer
10688c2ecf20Sopenharmony_ci */
10698c2ecf20Sopenharmony_cistatic inline void ubi_init_vid_buf(const struct ubi_device *ubi,
10708c2ecf20Sopenharmony_ci				    struct ubi_vid_io_buf *vidb,
10718c2ecf20Sopenharmony_ci				    void *buf)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	if (buf)
10748c2ecf20Sopenharmony_ci		memset(buf, 0, ubi->vid_hdr_alsize);
10758c2ecf20Sopenharmony_ci
10768c2ecf20Sopenharmony_ci	vidb->buffer = buf;
10778c2ecf20Sopenharmony_ci	vidb->hdr = buf + ubi->vid_hdr_shift;
10788c2ecf20Sopenharmony_ci}
10798c2ecf20Sopenharmony_ci
10808c2ecf20Sopenharmony_ci/**
10818c2ecf20Sopenharmony_ci * ubi_init_vid_buf - Allocate a VID buffer
10828c2ecf20Sopenharmony_ci * @ubi: the UBI device
10838c2ecf20Sopenharmony_ci * @gfp_flags: GFP flags to use for the allocation
10848c2ecf20Sopenharmony_ci */
10858c2ecf20Sopenharmony_cistatic inline struct ubi_vid_io_buf *
10868c2ecf20Sopenharmony_ciubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
10878c2ecf20Sopenharmony_ci{
10888c2ecf20Sopenharmony_ci	struct ubi_vid_io_buf *vidb;
10898c2ecf20Sopenharmony_ci	void *buf;
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	vidb = kzalloc(sizeof(*vidb), gfp_flags);
10928c2ecf20Sopenharmony_ci	if (!vidb)
10938c2ecf20Sopenharmony_ci		return NULL;
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci	buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
10968c2ecf20Sopenharmony_ci	if (!buf) {
10978c2ecf20Sopenharmony_ci		kfree(vidb);
10988c2ecf20Sopenharmony_ci		return NULL;
10998c2ecf20Sopenharmony_ci	}
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	ubi_init_vid_buf(ubi, vidb, buf);
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	return vidb;
11048c2ecf20Sopenharmony_ci}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_ci/**
11078c2ecf20Sopenharmony_ci * ubi_free_vid_buf - Free a VID buffer
11088c2ecf20Sopenharmony_ci * @vidb: the VID buffer to free
11098c2ecf20Sopenharmony_ci */
11108c2ecf20Sopenharmony_cistatic inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
11118c2ecf20Sopenharmony_ci{
11128c2ecf20Sopenharmony_ci	if (!vidb)
11138c2ecf20Sopenharmony_ci		return;
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	kfree(vidb->buffer);
11168c2ecf20Sopenharmony_ci	kfree(vidb);
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci/**
11208c2ecf20Sopenharmony_ci * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
11218c2ecf20Sopenharmony_ci * @vidb: VID buffer
11228c2ecf20Sopenharmony_ci */
11238c2ecf20Sopenharmony_cistatic inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci	return vidb->hdr;
11268c2ecf20Sopenharmony_ci}
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci/*
11298c2ecf20Sopenharmony_ci * This function is equivalent to 'ubi_io_read()', but @offset is relative to
11308c2ecf20Sopenharmony_ci * the beginning of the logical eraseblock, not to the beginning of the
11318c2ecf20Sopenharmony_ci * physical eraseblock.
11328c2ecf20Sopenharmony_ci */
11338c2ecf20Sopenharmony_cistatic inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
11348c2ecf20Sopenharmony_ci				   int pnum, int offset, int len)
11358c2ecf20Sopenharmony_ci{
11368c2ecf20Sopenharmony_ci	ubi_assert(offset >= 0);
11378c2ecf20Sopenharmony_ci	return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
11388c2ecf20Sopenharmony_ci}
11398c2ecf20Sopenharmony_ci
11408c2ecf20Sopenharmony_ci/*
11418c2ecf20Sopenharmony_ci * This function is equivalent to 'ubi_io_write()', but @offset is relative to
11428c2ecf20Sopenharmony_ci * the beginning of the logical eraseblock, not to the beginning of the
11438c2ecf20Sopenharmony_ci * physical eraseblock.
11448c2ecf20Sopenharmony_ci */
11458c2ecf20Sopenharmony_cistatic inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
11468c2ecf20Sopenharmony_ci				    int pnum, int offset, int len)
11478c2ecf20Sopenharmony_ci{
11488c2ecf20Sopenharmony_ci	ubi_assert(offset >= 0);
11498c2ecf20Sopenharmony_ci	return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
11508c2ecf20Sopenharmony_ci}
11518c2ecf20Sopenharmony_ci
11528c2ecf20Sopenharmony_ci/**
11538c2ecf20Sopenharmony_ci * ubi_ro_mode - switch to read-only mode.
11548c2ecf20Sopenharmony_ci * @ubi: UBI device description object
11558c2ecf20Sopenharmony_ci */
11568c2ecf20Sopenharmony_cistatic inline void ubi_ro_mode(struct ubi_device *ubi)
11578c2ecf20Sopenharmony_ci{
11588c2ecf20Sopenharmony_ci	if (!ubi->ro_mode) {
11598c2ecf20Sopenharmony_ci		ubi->ro_mode = 1;
11608c2ecf20Sopenharmony_ci		ubi_warn(ubi, "switch to read-only mode");
11618c2ecf20Sopenharmony_ci		dump_stack();
11628c2ecf20Sopenharmony_ci	}
11638c2ecf20Sopenharmony_ci}
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_ci/**
11668c2ecf20Sopenharmony_ci * vol_id2idx - get table index by volume ID.
11678c2ecf20Sopenharmony_ci * @ubi: UBI device description object
11688c2ecf20Sopenharmony_ci * @vol_id: volume ID
11698c2ecf20Sopenharmony_ci */
11708c2ecf20Sopenharmony_cistatic inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
11718c2ecf20Sopenharmony_ci{
11728c2ecf20Sopenharmony_ci	if (vol_id >= UBI_INTERNAL_VOL_START)
11738c2ecf20Sopenharmony_ci		return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
11748c2ecf20Sopenharmony_ci	else
11758c2ecf20Sopenharmony_ci		return vol_id;
11768c2ecf20Sopenharmony_ci}
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci/**
11798c2ecf20Sopenharmony_ci * idx2vol_id - get volume ID by table index.
11808c2ecf20Sopenharmony_ci * @ubi: UBI device description object
11818c2ecf20Sopenharmony_ci * @idx: table index
11828c2ecf20Sopenharmony_ci */
11838c2ecf20Sopenharmony_cistatic inline int idx2vol_id(const struct ubi_device *ubi, int idx)
11848c2ecf20Sopenharmony_ci{
11858c2ecf20Sopenharmony_ci	if (idx >= ubi->vtbl_slots)
11868c2ecf20Sopenharmony_ci		return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
11878c2ecf20Sopenharmony_ci	else
11888c2ecf20Sopenharmony_ci		return idx;
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci/**
11928c2ecf20Sopenharmony_ci * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
11938c2ecf20Sopenharmony_ci * @vol_id: volume ID
11948c2ecf20Sopenharmony_ci */
11958c2ecf20Sopenharmony_cistatic inline bool ubi_is_fm_vol(int vol_id)
11968c2ecf20Sopenharmony_ci{
11978c2ecf20Sopenharmony_ci	switch (vol_id) {
11988c2ecf20Sopenharmony_ci		case UBI_FM_SB_VOLUME_ID:
11998c2ecf20Sopenharmony_ci		case UBI_FM_DATA_VOLUME_ID:
12008c2ecf20Sopenharmony_ci		return true;
12018c2ecf20Sopenharmony_ci	}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_ci	return false;
12048c2ecf20Sopenharmony_ci}
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci/**
12078c2ecf20Sopenharmony_ci * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
12088c2ecf20Sopenharmony_ci * @ubi: UBI device description object
12098c2ecf20Sopenharmony_ci * @pnum: physical eraseblock to look for
12108c2ecf20Sopenharmony_ci *
12118c2ecf20Sopenharmony_ci * This function returns a wear leveling object if @pnum relates to the current
12128c2ecf20Sopenharmony_ci * fastmap, @NULL otherwise.
12138c2ecf20Sopenharmony_ci */
12148c2ecf20Sopenharmony_cistatic inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi,
12158c2ecf20Sopenharmony_ci						     int pnum)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	int i;
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	if (ubi->fm) {
12208c2ecf20Sopenharmony_ci		for (i = 0; i < ubi->fm->used_blocks; i++) {
12218c2ecf20Sopenharmony_ci			if (ubi->fm->e[i]->pnum == pnum)
12228c2ecf20Sopenharmony_ci				return ubi->fm->e[i];
12238c2ecf20Sopenharmony_ci		}
12248c2ecf20Sopenharmony_ci	}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	return NULL;
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_ci#endif /* !__UBI_UBI_H__ */
1230