162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006
462306a36Sopenharmony_ci * Copyright (c) Nokia Corporation, 2006, 2007
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * Author: Artem Bityutskiy (Битюцкий Артём)
762306a36Sopenharmony_ci */
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci#ifndef __UBI_UBI_H__
1062306a36Sopenharmony_ci#define __UBI_UBI_H__
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ci#include <linux/types.h>
1362306a36Sopenharmony_ci#include <linux/list.h>
1462306a36Sopenharmony_ci#include <linux/rbtree.h>
1562306a36Sopenharmony_ci#include <linux/sched.h>
1662306a36Sopenharmony_ci#include <linux/wait.h>
1762306a36Sopenharmony_ci#include <linux/mutex.h>
1862306a36Sopenharmony_ci#include <linux/rwsem.h>
1962306a36Sopenharmony_ci#include <linux/spinlock.h>
2062306a36Sopenharmony_ci#include <linux/fs.h>
2162306a36Sopenharmony_ci#include <linux/cdev.h>
2262306a36Sopenharmony_ci#include <linux/device.h>
2362306a36Sopenharmony_ci#include <linux/slab.h>
2462306a36Sopenharmony_ci#include <linux/string.h>
2562306a36Sopenharmony_ci#include <linux/vmalloc.h>
2662306a36Sopenharmony_ci#include <linux/notifier.h>
2762306a36Sopenharmony_ci#include <linux/mtd/mtd.h>
2862306a36Sopenharmony_ci#include <linux/mtd/ubi.h>
2962306a36Sopenharmony_ci#include <linux/pgtable.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include "ubi-media.h"
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/* Maximum number of supported UBI devices */
3462306a36Sopenharmony_ci#define UBI_MAX_DEVICES 32
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci/* UBI name used for character devices, sysfs, etc */
3762306a36Sopenharmony_ci#define UBI_NAME_STR "ubi"
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_cistruct ubi_device;
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci/* Normal UBI messages */
4262306a36Sopenharmony_ci__printf(2, 3)
4362306a36Sopenharmony_civoid ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* UBI warning messages */
4662306a36Sopenharmony_ci__printf(2, 3)
4762306a36Sopenharmony_civoid ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci/* UBI error messages */
5062306a36Sopenharmony_ci__printf(2, 3)
5162306a36Sopenharmony_civoid ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci/* Background thread name pattern */
5462306a36Sopenharmony_ci#define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci/*
5762306a36Sopenharmony_ci * This marker in the EBA table means that the LEB is um-mapped.
5862306a36Sopenharmony_ci * NOTE! It has to have the same value as %UBI_ALL.
5962306a36Sopenharmony_ci */
6062306a36Sopenharmony_ci#define UBI_LEB_UNMAPPED -1
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci/*
6362306a36Sopenharmony_ci * In case of errors, UBI tries to repeat the operation several times before
6462306a36Sopenharmony_ci * returning error. The below constant defines how many times UBI re-tries.
6562306a36Sopenharmony_ci */
6662306a36Sopenharmony_ci#define UBI_IO_RETRIES 3
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci/*
6962306a36Sopenharmony_ci * Length of the protection queue. The length is effectively equivalent to the
7062306a36Sopenharmony_ci * number of (global) erase cycles PEBs are protected from the wear-leveling
7162306a36Sopenharmony_ci * worker.
7262306a36Sopenharmony_ci */
7362306a36Sopenharmony_ci#define UBI_PROT_QUEUE_LEN 10
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci/* The volume ID/LEB number/erase counter is unknown */
7662306a36Sopenharmony_ci#define UBI_UNKNOWN -1
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci/*
7962306a36Sopenharmony_ci * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
8062306a36Sopenharmony_ci * + 2 for the number plus 1 for the trailing zero byte.
8162306a36Sopenharmony_ci */
8262306a36Sopenharmony_ci#define UBI_DFS_DIR_NAME "ubi%d"
8362306a36Sopenharmony_ci#define UBI_DFS_DIR_LEN  (3 + 2 + 1)
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci/*
8662306a36Sopenharmony_ci * Error codes returned by the I/O sub-system.
8762306a36Sopenharmony_ci *
8862306a36Sopenharmony_ci * UBI_IO_FF: the read region of flash contains only 0xFFs
8962306a36Sopenharmony_ci * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also there was a data
9062306a36Sopenharmony_ci *                     integrity error reported by the MTD driver
9162306a36Sopenharmony_ci *                     (uncorrectable ECC error in case of NAND)
9262306a36Sopenharmony_ci * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
9362306a36Sopenharmony_ci * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
9462306a36Sopenharmony_ci *                         data integrity error reported by the MTD driver
9562306a36Sopenharmony_ci *                         (uncorrectable ECC error in case of NAND)
9662306a36Sopenharmony_ci * UBI_IO_BITFLIPS: bit-flips were detected and corrected
9762306a36Sopenharmony_ci *
9862306a36Sopenharmony_ci * Note, it is probably better to have bit-flip and ebadmsg as flags which can
9962306a36Sopenharmony_ci * be or'ed with other error code. But this is a big change because there are
10062306a36Sopenharmony_ci * may callers, so it does not worth the risk of introducing a bug
10162306a36Sopenharmony_ci */
10262306a36Sopenharmony_cienum {
10362306a36Sopenharmony_ci	UBI_IO_FF = 1,
10462306a36Sopenharmony_ci	UBI_IO_FF_BITFLIPS,
10562306a36Sopenharmony_ci	UBI_IO_BAD_HDR,
10662306a36Sopenharmony_ci	UBI_IO_BAD_HDR_EBADMSG,
10762306a36Sopenharmony_ci	UBI_IO_BITFLIPS,
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/*
11162306a36Sopenharmony_ci * Return codes of the 'ubi_eba_copy_leb()' function.
11262306a36Sopenharmony_ci *
11362306a36Sopenharmony_ci * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
11462306a36Sopenharmony_ci *                   PEB was put meanwhile, or there is I/O on the source PEB
11562306a36Sopenharmony_ci * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
11662306a36Sopenharmony_ci *                     PEB
11762306a36Sopenharmony_ci * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
11862306a36Sopenharmony_ci *                     PEB
11962306a36Sopenharmony_ci * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
12062306a36Sopenharmony_ci *                     PEB
12162306a36Sopenharmony_ci * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
12262306a36Sopenharmony_ci *                       target PEB
12362306a36Sopenharmony_ci * MOVE_RETRY: retry scrubbing the PEB
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_cienum {
12662306a36Sopenharmony_ci	MOVE_CANCEL_RACE = 1,
12762306a36Sopenharmony_ci	MOVE_SOURCE_RD_ERR,
12862306a36Sopenharmony_ci	MOVE_TARGET_RD_ERR,
12962306a36Sopenharmony_ci	MOVE_TARGET_WR_ERR,
13062306a36Sopenharmony_ci	MOVE_TARGET_BITFLIPS,
13162306a36Sopenharmony_ci	MOVE_RETRY,
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci/*
13562306a36Sopenharmony_ci * Return codes of the fastmap sub-system
13662306a36Sopenharmony_ci *
13762306a36Sopenharmony_ci * UBI_NO_FASTMAP: No fastmap super block was found
13862306a36Sopenharmony_ci * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
13962306a36Sopenharmony_ci */
14062306a36Sopenharmony_cienum {
14162306a36Sopenharmony_ci	UBI_NO_FASTMAP = 1,
14262306a36Sopenharmony_ci	UBI_BAD_FASTMAP,
14362306a36Sopenharmony_ci};
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci/*
14662306a36Sopenharmony_ci * Flags for emulate_power_cut in ubi_debug_info
14762306a36Sopenharmony_ci *
14862306a36Sopenharmony_ci * POWER_CUT_EC_WRITE: Emulate a power cut when writing an EC header
14962306a36Sopenharmony_ci * POWER_CUT_VID_WRITE: Emulate a power cut when writing a VID header
15062306a36Sopenharmony_ci */
15162306a36Sopenharmony_cienum {
15262306a36Sopenharmony_ci	POWER_CUT_EC_WRITE = 0x01,
15362306a36Sopenharmony_ci	POWER_CUT_VID_WRITE = 0x02,
15462306a36Sopenharmony_ci};
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci/**
15762306a36Sopenharmony_ci * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
15862306a36Sopenharmony_ci *			   flash.
15962306a36Sopenharmony_ci * @hdr: a pointer to the VID header stored in buffer
16062306a36Sopenharmony_ci * @buffer: underlying buffer
16162306a36Sopenharmony_ci */
16262306a36Sopenharmony_cistruct ubi_vid_io_buf {
16362306a36Sopenharmony_ci	struct ubi_vid_hdr *hdr;
16462306a36Sopenharmony_ci	void *buffer;
16562306a36Sopenharmony_ci};
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci/**
16862306a36Sopenharmony_ci * struct ubi_wl_entry - wear-leveling entry.
16962306a36Sopenharmony_ci * @u.rb: link in the corresponding (free/used) RB-tree
17062306a36Sopenharmony_ci * @u.list: link in the protection queue
17162306a36Sopenharmony_ci * @ec: erase counter
17262306a36Sopenharmony_ci * @pnum: physical eraseblock number
17362306a36Sopenharmony_ci *
17462306a36Sopenharmony_ci * This data structure is used in the WL sub-system. Each physical eraseblock
17562306a36Sopenharmony_ci * has a corresponding &struct wl_entry object which may be kept in different
17662306a36Sopenharmony_ci * RB-trees. See WL sub-system for details.
17762306a36Sopenharmony_ci */
17862306a36Sopenharmony_cistruct ubi_wl_entry {
17962306a36Sopenharmony_ci	union {
18062306a36Sopenharmony_ci		struct rb_node rb;
18162306a36Sopenharmony_ci		struct list_head list;
18262306a36Sopenharmony_ci	} u;
18362306a36Sopenharmony_ci	int ec;
18462306a36Sopenharmony_ci	int pnum;
18562306a36Sopenharmony_ci};
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci/**
18862306a36Sopenharmony_ci * struct ubi_ltree_entry - an entry in the lock tree.
18962306a36Sopenharmony_ci * @rb: links RB-tree nodes
19062306a36Sopenharmony_ci * @vol_id: volume ID of the locked logical eraseblock
19162306a36Sopenharmony_ci * @lnum: locked logical eraseblock number
19262306a36Sopenharmony_ci * @users: how many tasks are using this logical eraseblock or wait for it
19362306a36Sopenharmony_ci * @mutex: read/write mutex to implement read/write access serialization to
19462306a36Sopenharmony_ci *         the (@vol_id, @lnum) logical eraseblock
19562306a36Sopenharmony_ci *
19662306a36Sopenharmony_ci * This data structure is used in the EBA sub-system to implement per-LEB
19762306a36Sopenharmony_ci * locking. When a logical eraseblock is being locked - corresponding
19862306a36Sopenharmony_ci * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
19962306a36Sopenharmony_ci * See EBA sub-system for details.
20062306a36Sopenharmony_ci */
20162306a36Sopenharmony_cistruct ubi_ltree_entry {
20262306a36Sopenharmony_ci	struct rb_node rb;
20362306a36Sopenharmony_ci	int vol_id;
20462306a36Sopenharmony_ci	int lnum;
20562306a36Sopenharmony_ci	int users;
20662306a36Sopenharmony_ci	struct rw_semaphore mutex;
20762306a36Sopenharmony_ci};
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci/**
21062306a36Sopenharmony_ci * struct ubi_rename_entry - volume re-name description data structure.
21162306a36Sopenharmony_ci * @new_name_len: new volume name length
21262306a36Sopenharmony_ci * @new_name: new volume name
21362306a36Sopenharmony_ci * @remove: if not zero, this volume should be removed, not re-named
21462306a36Sopenharmony_ci * @desc: descriptor of the volume
21562306a36Sopenharmony_ci * @list: links re-name entries into a list
21662306a36Sopenharmony_ci *
21762306a36Sopenharmony_ci * This data structure is utilized in the multiple volume re-name code. Namely,
21862306a36Sopenharmony_ci * UBI first creates a list of &struct ubi_rename_entry objects from the
21962306a36Sopenharmony_ci * &struct ubi_rnvol_req request object, and then utilizes this list to do all
22062306a36Sopenharmony_ci * the job.
22162306a36Sopenharmony_ci */
22262306a36Sopenharmony_cistruct ubi_rename_entry {
22362306a36Sopenharmony_ci	int new_name_len;
22462306a36Sopenharmony_ci	char new_name[UBI_VOL_NAME_MAX + 1];
22562306a36Sopenharmony_ci	int remove;
22662306a36Sopenharmony_ci	struct ubi_volume_desc *desc;
22762306a36Sopenharmony_ci	struct list_head list;
22862306a36Sopenharmony_ci};
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_cistruct ubi_volume_desc;
23162306a36Sopenharmony_ci
23262306a36Sopenharmony_ci/**
23362306a36Sopenharmony_ci * struct ubi_fastmap_layout - in-memory fastmap data structure.
23462306a36Sopenharmony_ci * @e: PEBs used by the current fastmap
23562306a36Sopenharmony_ci * @to_be_tortured: if non-zero tortured this PEB
23662306a36Sopenharmony_ci * @used_blocks: number of used PEBs
23762306a36Sopenharmony_ci * @max_pool_size: maximal size of the user pool
23862306a36Sopenharmony_ci * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
23962306a36Sopenharmony_ci */
24062306a36Sopenharmony_cistruct ubi_fastmap_layout {
24162306a36Sopenharmony_ci	struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
24262306a36Sopenharmony_ci	int to_be_tortured[UBI_FM_MAX_BLOCKS];
24362306a36Sopenharmony_ci	int used_blocks;
24462306a36Sopenharmony_ci	int max_pool_size;
24562306a36Sopenharmony_ci	int max_wl_pool_size;
24662306a36Sopenharmony_ci};
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci/**
24962306a36Sopenharmony_ci * struct ubi_fm_pool - in-memory fastmap pool
25062306a36Sopenharmony_ci * @pebs: PEBs in this pool
25162306a36Sopenharmony_ci * @used: number of used PEBs
25262306a36Sopenharmony_ci * @size: total number of PEBs in this pool
25362306a36Sopenharmony_ci * @max_size: maximal size of the pool
25462306a36Sopenharmony_ci *
25562306a36Sopenharmony_ci * A pool gets filled with up to max_size.
25662306a36Sopenharmony_ci * If all PEBs within the pool are used a new fastmap will be written
25762306a36Sopenharmony_ci * to the flash and the pool gets refilled with empty PEBs.
25862306a36Sopenharmony_ci *
25962306a36Sopenharmony_ci */
26062306a36Sopenharmony_cistruct ubi_fm_pool {
26162306a36Sopenharmony_ci	int pebs[UBI_FM_MAX_POOL_SIZE];
26262306a36Sopenharmony_ci	int used;
26362306a36Sopenharmony_ci	int size;
26462306a36Sopenharmony_ci	int max_size;
26562306a36Sopenharmony_ci};
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/**
26862306a36Sopenharmony_ci * struct ubi_eba_leb_desc - EBA logical eraseblock descriptor
26962306a36Sopenharmony_ci * @lnum: the logical eraseblock number
27062306a36Sopenharmony_ci * @pnum: the physical eraseblock where the LEB can be found
27162306a36Sopenharmony_ci *
27262306a36Sopenharmony_ci * This structure is here to hide EBA's internal from other part of the
27362306a36Sopenharmony_ci * UBI implementation.
27462306a36Sopenharmony_ci *
27562306a36Sopenharmony_ci * One can query the position of a LEB by calling ubi_eba_get_ldesc().
27662306a36Sopenharmony_ci */
27762306a36Sopenharmony_cistruct ubi_eba_leb_desc {
27862306a36Sopenharmony_ci	int lnum;
27962306a36Sopenharmony_ci	int pnum;
28062306a36Sopenharmony_ci};
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ci/**
28362306a36Sopenharmony_ci * struct ubi_volume - UBI volume description data structure.
28462306a36Sopenharmony_ci * @dev: device object to make use of the Linux device model
28562306a36Sopenharmony_ci * @cdev: character device object to create character device
28662306a36Sopenharmony_ci * @ubi: reference to the UBI device description object
28762306a36Sopenharmony_ci * @vol_id: volume ID
28862306a36Sopenharmony_ci * @ref_count: volume reference count
28962306a36Sopenharmony_ci * @readers: number of users holding this volume in read-only mode
29062306a36Sopenharmony_ci * @writers: number of users holding this volume in read-write mode
29162306a36Sopenharmony_ci * @exclusive: whether somebody holds this volume in exclusive mode
29262306a36Sopenharmony_ci * @metaonly: whether somebody is altering only meta data of this volume
29362306a36Sopenharmony_ci *
29462306a36Sopenharmony_ci * @reserved_pebs: how many physical eraseblocks are reserved for this volume
29562306a36Sopenharmony_ci * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
29662306a36Sopenharmony_ci * @usable_leb_size: logical eraseblock size without padding
29762306a36Sopenharmony_ci * @used_ebs: how many logical eraseblocks in this volume contain data
29862306a36Sopenharmony_ci * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
29962306a36Sopenharmony_ci * @used_bytes: how many bytes of data this volume contains
30062306a36Sopenharmony_ci * @alignment: volume alignment
30162306a36Sopenharmony_ci * @data_pad: how many bytes are not used at the end of physical eraseblocks to
30262306a36Sopenharmony_ci *            satisfy the requested alignment
30362306a36Sopenharmony_ci * @name_len: volume name length
30462306a36Sopenharmony_ci * @name: volume name
30562306a36Sopenharmony_ci *
30662306a36Sopenharmony_ci * @upd_ebs: how many eraseblocks are expected to be updated
30762306a36Sopenharmony_ci * @ch_lnum: LEB number which is being changing by the atomic LEB change
30862306a36Sopenharmony_ci *           operation
30962306a36Sopenharmony_ci * @upd_bytes: how many bytes are expected to be received for volume update or
31062306a36Sopenharmony_ci *             atomic LEB change
31162306a36Sopenharmony_ci * @upd_received: how many bytes were already received for volume update or
31262306a36Sopenharmony_ci *                atomic LEB change
31362306a36Sopenharmony_ci * @upd_buf: update buffer which is used to collect update data or data for
31462306a36Sopenharmony_ci *           atomic LEB change
31562306a36Sopenharmony_ci *
31662306a36Sopenharmony_ci * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
31762306a36Sopenharmony_ci * @skip_check: %1 if CRC check of this static volume should be skipped.
31862306a36Sopenharmony_ci *		Directly reflects the presence of the
31962306a36Sopenharmony_ci *		%UBI_VTBL_SKIP_CRC_CHECK_FLG flag in the vtbl entry
32062306a36Sopenharmony_ci * @checked: %1 if this static volume was checked
32162306a36Sopenharmony_ci * @corrupted: %1 if the volume is corrupted (static volumes only)
32262306a36Sopenharmony_ci * @upd_marker: %1 if the update marker is set for this volume
32362306a36Sopenharmony_ci * @updating: %1 if the volume is being updated
32462306a36Sopenharmony_ci * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
32562306a36Sopenharmony_ci * @direct_writes: %1 if direct writes are enabled for this volume
32662306a36Sopenharmony_ci *
32762306a36Sopenharmony_ci * @checkmap: bitmap to remember which PEB->LEB mappings got checked,
32862306a36Sopenharmony_ci *            protected by UBI LEB lock tree.
32962306a36Sopenharmony_ci *
33062306a36Sopenharmony_ci * The @corrupted field indicates that the volume's contents is corrupted.
33162306a36Sopenharmony_ci * Since UBI protects only static volumes, this field is not relevant to
33262306a36Sopenharmony_ci * dynamic volumes - it is user's responsibility to assure their data
33362306a36Sopenharmony_ci * integrity.
33462306a36Sopenharmony_ci *
33562306a36Sopenharmony_ci * The @upd_marker flag indicates that this volume is either being updated at
33662306a36Sopenharmony_ci * the moment or is damaged because of an unclean reboot.
33762306a36Sopenharmony_ci */
33862306a36Sopenharmony_cistruct ubi_volume {
33962306a36Sopenharmony_ci	struct device dev;
34062306a36Sopenharmony_ci	struct cdev cdev;
34162306a36Sopenharmony_ci	struct ubi_device *ubi;
34262306a36Sopenharmony_ci	int vol_id;
34362306a36Sopenharmony_ci	int ref_count;
34462306a36Sopenharmony_ci	int readers;
34562306a36Sopenharmony_ci	int writers;
34662306a36Sopenharmony_ci	int exclusive;
34762306a36Sopenharmony_ci	int metaonly;
34862306a36Sopenharmony_ci
34962306a36Sopenharmony_ci	int reserved_pebs;
35062306a36Sopenharmony_ci	int vol_type;
35162306a36Sopenharmony_ci	int usable_leb_size;
35262306a36Sopenharmony_ci	int used_ebs;
35362306a36Sopenharmony_ci	int last_eb_bytes;
35462306a36Sopenharmony_ci	long long used_bytes;
35562306a36Sopenharmony_ci	int alignment;
35662306a36Sopenharmony_ci	int data_pad;
35762306a36Sopenharmony_ci	int name_len;
35862306a36Sopenharmony_ci	char name[UBI_VOL_NAME_MAX + 1];
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	int upd_ebs;
36162306a36Sopenharmony_ci	int ch_lnum;
36262306a36Sopenharmony_ci	long long upd_bytes;
36362306a36Sopenharmony_ci	long long upd_received;
36462306a36Sopenharmony_ci	void *upd_buf;
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_ci	struct ubi_eba_table *eba_tbl;
36762306a36Sopenharmony_ci	unsigned int skip_check:1;
36862306a36Sopenharmony_ci	unsigned int checked:1;
36962306a36Sopenharmony_ci	unsigned int corrupted:1;
37062306a36Sopenharmony_ci	unsigned int upd_marker:1;
37162306a36Sopenharmony_ci	unsigned int updating:1;
37262306a36Sopenharmony_ci	unsigned int changing_leb:1;
37362306a36Sopenharmony_ci	unsigned int direct_writes:1;
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP
37662306a36Sopenharmony_ci	unsigned long *checkmap;
37762306a36Sopenharmony_ci#endif
37862306a36Sopenharmony_ci};
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci/**
38162306a36Sopenharmony_ci * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
38262306a36Sopenharmony_ci * @vol: reference to the corresponding volume description object
38362306a36Sopenharmony_ci * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, %UBI_EXCLUSIVE
38462306a36Sopenharmony_ci * or %UBI_METAONLY)
38562306a36Sopenharmony_ci */
38662306a36Sopenharmony_cistruct ubi_volume_desc {
38762306a36Sopenharmony_ci	struct ubi_volume *vol;
38862306a36Sopenharmony_ci	int mode;
38962306a36Sopenharmony_ci};
39062306a36Sopenharmony_ci
39162306a36Sopenharmony_ci/**
39262306a36Sopenharmony_ci * struct ubi_debug_info - debugging information for an UBI device.
39362306a36Sopenharmony_ci *
39462306a36Sopenharmony_ci * @chk_gen: if UBI general extra checks are enabled
39562306a36Sopenharmony_ci * @chk_io: if UBI I/O extra checks are enabled
39662306a36Sopenharmony_ci * @chk_fastmap: if UBI fastmap extra checks are enabled
39762306a36Sopenharmony_ci * @disable_bgt: disable the background task for testing purposes
39862306a36Sopenharmony_ci * @emulate_bitflips: emulate bit-flips for testing purposes
39962306a36Sopenharmony_ci * @emulate_io_failures: emulate write/erase failures for testing purposes
40062306a36Sopenharmony_ci * @emulate_power_cut: emulate power cut for testing purposes
40162306a36Sopenharmony_ci * @power_cut_counter: count down for writes left until emulated power cut
40262306a36Sopenharmony_ci * @power_cut_min: minimum number of writes before emulating a power cut
40362306a36Sopenharmony_ci * @power_cut_max: maximum number of writes until emulating a power cut
40462306a36Sopenharmony_ci * @dfs_dir_name: name of debugfs directory containing files of this UBI device
40562306a36Sopenharmony_ci * @dfs_dir: direntry object of the UBI device debugfs directory
40662306a36Sopenharmony_ci * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
40762306a36Sopenharmony_ci * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
40862306a36Sopenharmony_ci * @dfs_chk_fastmap: debugfs knob to enable UBI fastmap extra checks
40962306a36Sopenharmony_ci * @dfs_disable_bgt: debugfs knob to disable the background task
41062306a36Sopenharmony_ci * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
41162306a36Sopenharmony_ci * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
41262306a36Sopenharmony_ci * @dfs_emulate_power_cut: debugfs knob to emulate power cuts
41362306a36Sopenharmony_ci * @dfs_power_cut_min: debugfs knob for minimum writes before power cut
41462306a36Sopenharmony_ci * @dfs_power_cut_max: debugfs knob for maximum writes until power cut
41562306a36Sopenharmony_ci */
41662306a36Sopenharmony_cistruct ubi_debug_info {
41762306a36Sopenharmony_ci	unsigned int chk_gen:1;
41862306a36Sopenharmony_ci	unsigned int chk_io:1;
41962306a36Sopenharmony_ci	unsigned int chk_fastmap:1;
42062306a36Sopenharmony_ci	unsigned int disable_bgt:1;
42162306a36Sopenharmony_ci	unsigned int emulate_bitflips:1;
42262306a36Sopenharmony_ci	unsigned int emulate_io_failures:1;
42362306a36Sopenharmony_ci	unsigned int emulate_power_cut:2;
42462306a36Sopenharmony_ci	unsigned int power_cut_counter;
42562306a36Sopenharmony_ci	unsigned int power_cut_min;
42662306a36Sopenharmony_ci	unsigned int power_cut_max;
42762306a36Sopenharmony_ci	char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
42862306a36Sopenharmony_ci	struct dentry *dfs_dir;
42962306a36Sopenharmony_ci	struct dentry *dfs_chk_gen;
43062306a36Sopenharmony_ci	struct dentry *dfs_chk_io;
43162306a36Sopenharmony_ci	struct dentry *dfs_chk_fastmap;
43262306a36Sopenharmony_ci	struct dentry *dfs_disable_bgt;
43362306a36Sopenharmony_ci	struct dentry *dfs_emulate_bitflips;
43462306a36Sopenharmony_ci	struct dentry *dfs_emulate_io_failures;
43562306a36Sopenharmony_ci	struct dentry *dfs_emulate_power_cut;
43662306a36Sopenharmony_ci	struct dentry *dfs_power_cut_min;
43762306a36Sopenharmony_ci	struct dentry *dfs_power_cut_max;
43862306a36Sopenharmony_ci};
43962306a36Sopenharmony_ci
44062306a36Sopenharmony_ci/**
44162306a36Sopenharmony_ci * struct ubi_device - UBI device description structure
44262306a36Sopenharmony_ci * @dev: UBI device object to use the Linux device model
44362306a36Sopenharmony_ci * @cdev: character device object to create character device
44462306a36Sopenharmony_ci * @ubi_num: UBI device number
44562306a36Sopenharmony_ci * @ubi_name: UBI device name
44662306a36Sopenharmony_ci * @vol_count: number of volumes in this UBI device
44762306a36Sopenharmony_ci * @volumes: volumes of this UBI device
44862306a36Sopenharmony_ci * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
44962306a36Sopenharmony_ci *                @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
45062306a36Sopenharmony_ci *                @vol->readers, @vol->writers, @vol->exclusive,
45162306a36Sopenharmony_ci *                @vol->metaonly, @vol->ref_count, @vol->mapping and
45262306a36Sopenharmony_ci *                @vol->eba_tbl.
45362306a36Sopenharmony_ci * @ref_count: count of references on the UBI device
45462306a36Sopenharmony_ci * @image_seq: image sequence number recorded on EC headers
45562306a36Sopenharmony_ci *
45662306a36Sopenharmony_ci * @rsvd_pebs: count of reserved physical eraseblocks
45762306a36Sopenharmony_ci * @avail_pebs: count of available physical eraseblocks
45862306a36Sopenharmony_ci * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
45962306a36Sopenharmony_ci *                 handling
46062306a36Sopenharmony_ci * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
46162306a36Sopenharmony_ci *
46262306a36Sopenharmony_ci * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
46362306a36Sopenharmony_ci *                     of UBI initialization
46462306a36Sopenharmony_ci * @vtbl_slots: how many slots are available in the volume table
46562306a36Sopenharmony_ci * @vtbl_size: size of the volume table in bytes
46662306a36Sopenharmony_ci * @vtbl: in-RAM volume table copy
46762306a36Sopenharmony_ci * @device_mutex: protects on-flash volume table and serializes volume
46862306a36Sopenharmony_ci *                creation, deletion, update, re-size, re-name and set
46962306a36Sopenharmony_ci *                property
47062306a36Sopenharmony_ci *
47162306a36Sopenharmony_ci * @max_ec: current highest erase counter value
47262306a36Sopenharmony_ci * @mean_ec: current mean erase counter value
47362306a36Sopenharmony_ci *
47462306a36Sopenharmony_ci * @global_sqnum: global sequence number
47562306a36Sopenharmony_ci * @ltree_lock: protects the lock tree and @global_sqnum
47662306a36Sopenharmony_ci * @ltree: the lock tree
47762306a36Sopenharmony_ci * @alc_mutex: serializes "atomic LEB change" operations
47862306a36Sopenharmony_ci *
47962306a36Sopenharmony_ci * @fm_disabled: non-zero if fastmap is disabled (default)
48062306a36Sopenharmony_ci * @fm: in-memory data structure of the currently used fastmap
48162306a36Sopenharmony_ci * @fm_pool: in-memory data structure of the fastmap pool
48262306a36Sopenharmony_ci * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
48362306a36Sopenharmony_ci *		sub-system
48462306a36Sopenharmony_ci * @fm_protect: serializes ubi_update_fastmap(), protects @fm_buf and makes sure
48562306a36Sopenharmony_ci * that critical sections cannot be interrupted by ubi_update_fastmap()
48662306a36Sopenharmony_ci * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
48762306a36Sopenharmony_ci * @fm_size: fastmap size in bytes
48862306a36Sopenharmony_ci * @fm_eba_sem: allows ubi_update_fastmap() to block EBA table changes
48962306a36Sopenharmony_ci * @fm_work: fastmap work queue
49062306a36Sopenharmony_ci * @fm_work_scheduled: non-zero if fastmap work was scheduled
49162306a36Sopenharmony_ci * @fast_attach: non-zero if UBI was attached by fastmap
49262306a36Sopenharmony_ci * @fm_anchor: The next anchor PEB to use for fastmap
49362306a36Sopenharmony_ci * @fm_do_produce_anchor: If true produce an anchor PEB in wl
49462306a36Sopenharmony_ci *
49562306a36Sopenharmony_ci * @used: RB-tree of used physical eraseblocks
49662306a36Sopenharmony_ci * @erroneous: RB-tree of erroneous used physical eraseblocks
49762306a36Sopenharmony_ci * @free: RB-tree of free physical eraseblocks
49862306a36Sopenharmony_ci * @free_count: Contains the number of elements in @free
49962306a36Sopenharmony_ci * @scrub: RB-tree of physical eraseblocks which need scrubbing
50062306a36Sopenharmony_ci * @pq: protection queue (contain physical eraseblocks which are temporarily
50162306a36Sopenharmony_ci *      protected from the wear-leveling worker)
50262306a36Sopenharmony_ci * @pq_head: protection queue head
50362306a36Sopenharmony_ci * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
50462306a36Sopenharmony_ci *	     @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
50562306a36Sopenharmony_ci *	     @erroneous, @erroneous_peb_count, @fm_work_scheduled, @fm_pool,
50662306a36Sopenharmony_ci *	     and @fm_wl_pool fields
50762306a36Sopenharmony_ci * @move_mutex: serializes eraseblock moves
50862306a36Sopenharmony_ci * @work_sem: used to wait for all the scheduled works to finish and prevent
50962306a36Sopenharmony_ci * new works from being submitted
51062306a36Sopenharmony_ci * @wl_scheduled: non-zero if the wear-leveling was scheduled
51162306a36Sopenharmony_ci * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
51262306a36Sopenharmony_ci *             physical eraseblock
51362306a36Sopenharmony_ci * @move_from: physical eraseblock from where the data is being moved
51462306a36Sopenharmony_ci * @move_to: physical eraseblock where the data is being moved to
51562306a36Sopenharmony_ci * @move_to_put: if the "to" PEB was put
51662306a36Sopenharmony_ci * @works: list of pending works
51762306a36Sopenharmony_ci * @works_count: count of pending works
51862306a36Sopenharmony_ci * @bgt_thread: background thread description object
51962306a36Sopenharmony_ci * @thread_enabled: if the background thread is enabled
52062306a36Sopenharmony_ci * @bgt_name: background thread name
52162306a36Sopenharmony_ci *
52262306a36Sopenharmony_ci * @flash_size: underlying MTD device size (in bytes)
52362306a36Sopenharmony_ci * @peb_count: count of physical eraseblocks on the MTD device
52462306a36Sopenharmony_ci * @peb_size: physical eraseblock size
52562306a36Sopenharmony_ci * @bad_peb_limit: top limit of expected bad physical eraseblocks
52662306a36Sopenharmony_ci * @bad_peb_count: count of bad physical eraseblocks
52762306a36Sopenharmony_ci * @good_peb_count: count of good physical eraseblocks
52862306a36Sopenharmony_ci * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
52962306a36Sopenharmony_ci *                  used by UBI)
53062306a36Sopenharmony_ci * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
53162306a36Sopenharmony_ci * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
53262306a36Sopenharmony_ci * @min_io_size: minimal input/output unit size of the underlying MTD device
53362306a36Sopenharmony_ci * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
53462306a36Sopenharmony_ci * @ro_mode: if the UBI device is in read-only mode
53562306a36Sopenharmony_ci * @leb_size: logical eraseblock size
53662306a36Sopenharmony_ci * @leb_start: starting offset of logical eraseblocks within physical
53762306a36Sopenharmony_ci *             eraseblocks
53862306a36Sopenharmony_ci * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
53962306a36Sopenharmony_ci * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
54062306a36Sopenharmony_ci * @vid_hdr_offset: starting offset of the volume identifier header (might be
54162306a36Sopenharmony_ci *                  unaligned)
54262306a36Sopenharmony_ci * @vid_hdr_aloffset: starting offset of the VID header aligned to
54362306a36Sopenharmony_ci *                    @hdrs_min_io_size
54462306a36Sopenharmony_ci * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
54562306a36Sopenharmony_ci * @bad_allowed: whether the MTD device admits bad physical eraseblocks or not
54662306a36Sopenharmony_ci * @nor_flash: non-zero if working on top of NOR flash
54762306a36Sopenharmony_ci * @max_write_size: maximum amount of bytes the underlying flash can write at a
54862306a36Sopenharmony_ci *                  time (MTD write buffer size)
54962306a36Sopenharmony_ci * @mtd: MTD device descriptor
55062306a36Sopenharmony_ci *
55162306a36Sopenharmony_ci * @peb_buf: a buffer of PEB size used for different purposes
55262306a36Sopenharmony_ci * @buf_mutex: protects @peb_buf
55362306a36Sopenharmony_ci * @ckvol_mutex: serializes static volume checking when opening
55462306a36Sopenharmony_ci *
55562306a36Sopenharmony_ci * @dbg: debugging information for this UBI device
55662306a36Sopenharmony_ci */
55762306a36Sopenharmony_cistruct ubi_device {
55862306a36Sopenharmony_ci	struct cdev cdev;
55962306a36Sopenharmony_ci	struct device dev;
56062306a36Sopenharmony_ci	int ubi_num;
56162306a36Sopenharmony_ci	char ubi_name[sizeof(UBI_NAME_STR)+5];
56262306a36Sopenharmony_ci	int vol_count;
56362306a36Sopenharmony_ci	struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
56462306a36Sopenharmony_ci	spinlock_t volumes_lock;
56562306a36Sopenharmony_ci	int ref_count;
56662306a36Sopenharmony_ci	int image_seq;
56762306a36Sopenharmony_ci
56862306a36Sopenharmony_ci	int rsvd_pebs;
56962306a36Sopenharmony_ci	int avail_pebs;
57062306a36Sopenharmony_ci	int beb_rsvd_pebs;
57162306a36Sopenharmony_ci	int beb_rsvd_level;
57262306a36Sopenharmony_ci	int bad_peb_limit;
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci	int autoresize_vol_id;
57562306a36Sopenharmony_ci	int vtbl_slots;
57662306a36Sopenharmony_ci	int vtbl_size;
57762306a36Sopenharmony_ci	struct ubi_vtbl_record *vtbl;
57862306a36Sopenharmony_ci	struct mutex device_mutex;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	int max_ec;
58162306a36Sopenharmony_ci	/* Note, mean_ec is not updated run-time - should be fixed */
58262306a36Sopenharmony_ci	int mean_ec;
58362306a36Sopenharmony_ci
58462306a36Sopenharmony_ci	/* EBA sub-system's stuff */
58562306a36Sopenharmony_ci	unsigned long long global_sqnum;
58662306a36Sopenharmony_ci	spinlock_t ltree_lock;
58762306a36Sopenharmony_ci	struct rb_root ltree;
58862306a36Sopenharmony_ci	struct mutex alc_mutex;
58962306a36Sopenharmony_ci
59062306a36Sopenharmony_ci	/* Fastmap stuff */
59162306a36Sopenharmony_ci	int fm_disabled;
59262306a36Sopenharmony_ci	struct ubi_fastmap_layout *fm;
59362306a36Sopenharmony_ci	struct ubi_fm_pool fm_pool;
59462306a36Sopenharmony_ci	struct ubi_fm_pool fm_wl_pool;
59562306a36Sopenharmony_ci	struct rw_semaphore fm_eba_sem;
59662306a36Sopenharmony_ci	struct rw_semaphore fm_protect;
59762306a36Sopenharmony_ci	void *fm_buf;
59862306a36Sopenharmony_ci	size_t fm_size;
59962306a36Sopenharmony_ci	struct work_struct fm_work;
60062306a36Sopenharmony_ci	int fm_work_scheduled;
60162306a36Sopenharmony_ci	int fast_attach;
60262306a36Sopenharmony_ci	struct ubi_wl_entry *fm_anchor;
60362306a36Sopenharmony_ci	int fm_do_produce_anchor;
60462306a36Sopenharmony_ci
60562306a36Sopenharmony_ci	/* Wear-leveling sub-system's stuff */
60662306a36Sopenharmony_ci	struct rb_root used;
60762306a36Sopenharmony_ci	struct rb_root erroneous;
60862306a36Sopenharmony_ci	struct rb_root free;
60962306a36Sopenharmony_ci	int free_count;
61062306a36Sopenharmony_ci	struct rb_root scrub;
61162306a36Sopenharmony_ci	struct list_head pq[UBI_PROT_QUEUE_LEN];
61262306a36Sopenharmony_ci	int pq_head;
61362306a36Sopenharmony_ci	spinlock_t wl_lock;
61462306a36Sopenharmony_ci	struct mutex move_mutex;
61562306a36Sopenharmony_ci	struct rw_semaphore work_sem;
61662306a36Sopenharmony_ci	int wl_scheduled;
61762306a36Sopenharmony_ci	struct ubi_wl_entry **lookuptbl;
61862306a36Sopenharmony_ci	struct ubi_wl_entry *move_from;
61962306a36Sopenharmony_ci	struct ubi_wl_entry *move_to;
62062306a36Sopenharmony_ci	int move_to_put;
62162306a36Sopenharmony_ci	struct list_head works;
62262306a36Sopenharmony_ci	int works_count;
62362306a36Sopenharmony_ci	struct task_struct *bgt_thread;
62462306a36Sopenharmony_ci	int thread_enabled;
62562306a36Sopenharmony_ci	char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci	/* I/O sub-system's stuff */
62862306a36Sopenharmony_ci	long long flash_size;
62962306a36Sopenharmony_ci	int peb_count;
63062306a36Sopenharmony_ci	int peb_size;
63162306a36Sopenharmony_ci	int bad_peb_count;
63262306a36Sopenharmony_ci	int good_peb_count;
63362306a36Sopenharmony_ci	int corr_peb_count;
63462306a36Sopenharmony_ci	int erroneous_peb_count;
63562306a36Sopenharmony_ci	int max_erroneous;
63662306a36Sopenharmony_ci	int min_io_size;
63762306a36Sopenharmony_ci	int hdrs_min_io_size;
63862306a36Sopenharmony_ci	int ro_mode;
63962306a36Sopenharmony_ci	int leb_size;
64062306a36Sopenharmony_ci	int leb_start;
64162306a36Sopenharmony_ci	int ec_hdr_alsize;
64262306a36Sopenharmony_ci	int vid_hdr_alsize;
64362306a36Sopenharmony_ci	int vid_hdr_offset;
64462306a36Sopenharmony_ci	int vid_hdr_aloffset;
64562306a36Sopenharmony_ci	int vid_hdr_shift;
64662306a36Sopenharmony_ci	unsigned int bad_allowed:1;
64762306a36Sopenharmony_ci	unsigned int nor_flash:1;
64862306a36Sopenharmony_ci	int max_write_size;
64962306a36Sopenharmony_ci	struct mtd_info *mtd;
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	void *peb_buf;
65262306a36Sopenharmony_ci	struct mutex buf_mutex;
65362306a36Sopenharmony_ci	struct mutex ckvol_mutex;
65462306a36Sopenharmony_ci
65562306a36Sopenharmony_ci	struct ubi_debug_info dbg;
65662306a36Sopenharmony_ci};
65762306a36Sopenharmony_ci
65862306a36Sopenharmony_ci/**
65962306a36Sopenharmony_ci * struct ubi_ainf_peb - attach information about a physical eraseblock.
66062306a36Sopenharmony_ci * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
66162306a36Sopenharmony_ci * @pnum: physical eraseblock number
66262306a36Sopenharmony_ci * @vol_id: ID of the volume this LEB belongs to
66362306a36Sopenharmony_ci * @lnum: logical eraseblock number
66462306a36Sopenharmony_ci * @scrub: if this physical eraseblock needs scrubbing
66562306a36Sopenharmony_ci * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
66662306a36Sopenharmony_ci * @sqnum: sequence number
66762306a36Sopenharmony_ci * @u: unions RB-tree or @list links
66862306a36Sopenharmony_ci * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
66962306a36Sopenharmony_ci * @u.list: link in one of the eraseblock lists
67062306a36Sopenharmony_ci *
67162306a36Sopenharmony_ci * One object of this type is allocated for each physical eraseblock when
67262306a36Sopenharmony_ci * attaching an MTD device. Note, if this PEB does not belong to any LEB /
67362306a36Sopenharmony_ci * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
67462306a36Sopenharmony_ci */
67562306a36Sopenharmony_cistruct ubi_ainf_peb {
67662306a36Sopenharmony_ci	int ec;
67762306a36Sopenharmony_ci	int pnum;
67862306a36Sopenharmony_ci	int vol_id;
67962306a36Sopenharmony_ci	int lnum;
68062306a36Sopenharmony_ci	unsigned int scrub:1;
68162306a36Sopenharmony_ci	unsigned int copy_flag:1;
68262306a36Sopenharmony_ci	unsigned long long sqnum;
68362306a36Sopenharmony_ci	union {
68462306a36Sopenharmony_ci		struct rb_node rb;
68562306a36Sopenharmony_ci		struct list_head list;
68662306a36Sopenharmony_ci	} u;
68762306a36Sopenharmony_ci};
68862306a36Sopenharmony_ci
68962306a36Sopenharmony_ci/**
69062306a36Sopenharmony_ci * struct ubi_ainf_volume - attaching information about a volume.
69162306a36Sopenharmony_ci * @vol_id: volume ID
69262306a36Sopenharmony_ci * @highest_lnum: highest logical eraseblock number in this volume
69362306a36Sopenharmony_ci * @leb_count: number of logical eraseblocks in this volume
69462306a36Sopenharmony_ci * @vol_type: volume type
69562306a36Sopenharmony_ci * @used_ebs: number of used logical eraseblocks in this volume (only for
69662306a36Sopenharmony_ci *            static volumes)
69762306a36Sopenharmony_ci * @last_data_size: amount of data in the last logical eraseblock of this
69862306a36Sopenharmony_ci *                  volume (always equivalent to the usable logical eraseblock
69962306a36Sopenharmony_ci *                  size in case of dynamic volumes)
70062306a36Sopenharmony_ci * @data_pad: how many bytes at the end of logical eraseblocks of this volume
70162306a36Sopenharmony_ci *            are not used (due to volume alignment)
70262306a36Sopenharmony_ci * @compat: compatibility flags of this volume
70362306a36Sopenharmony_ci * @rb: link in the volume RB-tree
70462306a36Sopenharmony_ci * @root: root of the RB-tree containing all the eraseblock belonging to this
70562306a36Sopenharmony_ci *        volume (&struct ubi_ainf_peb objects)
70662306a36Sopenharmony_ci *
70762306a36Sopenharmony_ci * One object of this type is allocated for each volume when attaching an MTD
70862306a36Sopenharmony_ci * device.
70962306a36Sopenharmony_ci */
71062306a36Sopenharmony_cistruct ubi_ainf_volume {
71162306a36Sopenharmony_ci	int vol_id;
71262306a36Sopenharmony_ci	int highest_lnum;
71362306a36Sopenharmony_ci	int leb_count;
71462306a36Sopenharmony_ci	int vol_type;
71562306a36Sopenharmony_ci	int used_ebs;
71662306a36Sopenharmony_ci	int last_data_size;
71762306a36Sopenharmony_ci	int data_pad;
71862306a36Sopenharmony_ci	int compat;
71962306a36Sopenharmony_ci	struct rb_node rb;
72062306a36Sopenharmony_ci	struct rb_root root;
72162306a36Sopenharmony_ci};
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci/**
72462306a36Sopenharmony_ci * struct ubi_attach_info - MTD device attaching information.
72562306a36Sopenharmony_ci * @volumes: root of the volume RB-tree
72662306a36Sopenharmony_ci * @corr: list of corrupted physical eraseblocks
72762306a36Sopenharmony_ci * @free: list of free physical eraseblocks
72862306a36Sopenharmony_ci * @erase: list of physical eraseblocks which have to be erased
72962306a36Sopenharmony_ci * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
73062306a36Sopenharmony_ci *         those belonging to "preserve"-compatible internal volumes)
73162306a36Sopenharmony_ci * @fastmap: list of physical eraseblocks which relate to fastmap (e.g.,
73262306a36Sopenharmony_ci *           eraseblocks of the current and not yet erased old fastmap blocks)
73362306a36Sopenharmony_ci * @corr_peb_count: count of PEBs in the @corr list
73462306a36Sopenharmony_ci * @empty_peb_count: count of PEBs which are presumably empty (contain only
73562306a36Sopenharmony_ci *                   0xFF bytes)
73662306a36Sopenharmony_ci * @alien_peb_count: count of PEBs in the @alien list
73762306a36Sopenharmony_ci * @bad_peb_count: count of bad physical eraseblocks
73862306a36Sopenharmony_ci * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
73962306a36Sopenharmony_ci *                       as bad yet, but which look like bad
74062306a36Sopenharmony_ci * @vols_found: number of volumes found
74162306a36Sopenharmony_ci * @highest_vol_id: highest volume ID
74262306a36Sopenharmony_ci * @is_empty: flag indicating whether the MTD device is empty or not
74362306a36Sopenharmony_ci * @force_full_scan: flag indicating whether we need to do a full scan and drop
74462306a36Sopenharmony_ci		     all existing Fastmap data structures
74562306a36Sopenharmony_ci * @min_ec: lowest erase counter value
74662306a36Sopenharmony_ci * @max_ec: highest erase counter value
74762306a36Sopenharmony_ci * @max_sqnum: highest sequence number value
74862306a36Sopenharmony_ci * @mean_ec: mean erase counter value
74962306a36Sopenharmony_ci * @ec_sum: a temporary variable used when calculating @mean_ec
75062306a36Sopenharmony_ci * @ec_count: a temporary variable used when calculating @mean_ec
75162306a36Sopenharmony_ci * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
75262306a36Sopenharmony_ci * @ech: temporary EC header. Only available during scan
75362306a36Sopenharmony_ci * @vidh: temporary VID buffer. Only available during scan
75462306a36Sopenharmony_ci *
75562306a36Sopenharmony_ci * This data structure contains the result of attaching an MTD device and may
75662306a36Sopenharmony_ci * be used by other UBI sub-systems to build final UBI data structures, further
75762306a36Sopenharmony_ci * error-recovery and so on.
75862306a36Sopenharmony_ci */
75962306a36Sopenharmony_cistruct ubi_attach_info {
76062306a36Sopenharmony_ci	struct rb_root volumes;
76162306a36Sopenharmony_ci	struct list_head corr;
76262306a36Sopenharmony_ci	struct list_head free;
76362306a36Sopenharmony_ci	struct list_head erase;
76462306a36Sopenharmony_ci	struct list_head alien;
76562306a36Sopenharmony_ci	struct list_head fastmap;
76662306a36Sopenharmony_ci	int corr_peb_count;
76762306a36Sopenharmony_ci	int empty_peb_count;
76862306a36Sopenharmony_ci	int alien_peb_count;
76962306a36Sopenharmony_ci	int bad_peb_count;
77062306a36Sopenharmony_ci	int maybe_bad_peb_count;
77162306a36Sopenharmony_ci	int vols_found;
77262306a36Sopenharmony_ci	int highest_vol_id;
77362306a36Sopenharmony_ci	int is_empty;
77462306a36Sopenharmony_ci	int force_full_scan;
77562306a36Sopenharmony_ci	int min_ec;
77662306a36Sopenharmony_ci	int max_ec;
77762306a36Sopenharmony_ci	unsigned long long max_sqnum;
77862306a36Sopenharmony_ci	int mean_ec;
77962306a36Sopenharmony_ci	uint64_t ec_sum;
78062306a36Sopenharmony_ci	int ec_count;
78162306a36Sopenharmony_ci	struct kmem_cache *aeb_slab_cache;
78262306a36Sopenharmony_ci	struct ubi_ec_hdr *ech;
78362306a36Sopenharmony_ci	struct ubi_vid_io_buf *vidb;
78462306a36Sopenharmony_ci};
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci/**
78762306a36Sopenharmony_ci * struct ubi_work - UBI work description data structure.
78862306a36Sopenharmony_ci * @list: a link in the list of pending works
78962306a36Sopenharmony_ci * @func: worker function
79062306a36Sopenharmony_ci * @e: physical eraseblock to erase
79162306a36Sopenharmony_ci * @vol_id: the volume ID on which this erasure is being performed
79262306a36Sopenharmony_ci * @lnum: the logical eraseblock number
79362306a36Sopenharmony_ci * @torture: if the physical eraseblock has to be tortured
79462306a36Sopenharmony_ci *
79562306a36Sopenharmony_ci * The @func pointer points to the worker function. If the @shutdown argument is
79662306a36Sopenharmony_ci * not zero, the worker has to free the resources and exit immediately as the
79762306a36Sopenharmony_ci * WL sub-system is shutting down.
79862306a36Sopenharmony_ci * The worker has to return zero in case of success and a negative error code in
79962306a36Sopenharmony_ci * case of failure.
80062306a36Sopenharmony_ci */
80162306a36Sopenharmony_cistruct ubi_work {
80262306a36Sopenharmony_ci	struct list_head list;
80362306a36Sopenharmony_ci	int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
80462306a36Sopenharmony_ci	/* The below fields are only relevant to erasure works */
80562306a36Sopenharmony_ci	struct ubi_wl_entry *e;
80662306a36Sopenharmony_ci	int vol_id;
80762306a36Sopenharmony_ci	int lnum;
80862306a36Sopenharmony_ci	int torture;
80962306a36Sopenharmony_ci};
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci#include "debug.h"
81262306a36Sopenharmony_ci
81362306a36Sopenharmony_ciextern struct kmem_cache *ubi_wl_entry_slab;
81462306a36Sopenharmony_ciextern const struct file_operations ubi_ctrl_cdev_operations;
81562306a36Sopenharmony_ciextern const struct file_operations ubi_cdev_operations;
81662306a36Sopenharmony_ciextern const struct file_operations ubi_vol_cdev_operations;
81762306a36Sopenharmony_ciextern struct class ubi_class;
81862306a36Sopenharmony_ciextern struct mutex ubi_devices_mutex;
81962306a36Sopenharmony_ciextern struct blocking_notifier_head ubi_notifiers;
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci/* attach.c */
82262306a36Sopenharmony_cistruct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
82362306a36Sopenharmony_ci				   int ec);
82462306a36Sopenharmony_civoid ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
82562306a36Sopenharmony_ciint ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
82662306a36Sopenharmony_ci		  int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
82762306a36Sopenharmony_cistruct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);
82862306a36Sopenharmony_cistruct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
82962306a36Sopenharmony_ci				    int vol_id);
83062306a36Sopenharmony_civoid ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
83162306a36Sopenharmony_cistruct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
83262306a36Sopenharmony_ci				       struct ubi_attach_info *ai);
83362306a36Sopenharmony_ciint ubi_attach(struct ubi_device *ubi, int force_scan);
83462306a36Sopenharmony_civoid ubi_destroy_ai(struct ubi_attach_info *ai);
83562306a36Sopenharmony_ci
83662306a36Sopenharmony_ci/* vtbl.c */
83762306a36Sopenharmony_ciint ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
83862306a36Sopenharmony_ci			   struct ubi_vtbl_record *vtbl_rec);
83962306a36Sopenharmony_ciint ubi_vtbl_rename_volumes(struct ubi_device *ubi,
84062306a36Sopenharmony_ci			    struct list_head *rename_list);
84162306a36Sopenharmony_ciint ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
84262306a36Sopenharmony_ci
84362306a36Sopenharmony_ci/* vmt.c */
84462306a36Sopenharmony_ciint ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
84562306a36Sopenharmony_ciint ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
84662306a36Sopenharmony_ciint ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
84762306a36Sopenharmony_ciint ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
84862306a36Sopenharmony_ciint ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
84962306a36Sopenharmony_civoid ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
85062306a36Sopenharmony_ci
85162306a36Sopenharmony_ci/* upd.c */
85262306a36Sopenharmony_ciint ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
85362306a36Sopenharmony_ci		     long long bytes);
85462306a36Sopenharmony_ciint ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
85562306a36Sopenharmony_ci			 const void __user *buf, int count);
85662306a36Sopenharmony_ciint ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
85762306a36Sopenharmony_ci			 const struct ubi_leb_change_req *req);
85862306a36Sopenharmony_ciint ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
85962306a36Sopenharmony_ci			     const void __user *buf, int count);
86062306a36Sopenharmony_ci
86162306a36Sopenharmony_ci/* misc.c */
86262306a36Sopenharmony_ciint ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
86362306a36Sopenharmony_ci		      int length);
86462306a36Sopenharmony_ciint ubi_check_volume(struct ubi_device *ubi, int vol_id);
86562306a36Sopenharmony_civoid ubi_update_reserved(struct ubi_device *ubi);
86662306a36Sopenharmony_civoid ubi_calculate_reserved(struct ubi_device *ubi);
86762306a36Sopenharmony_ciint ubi_check_pattern(const void *buf, uint8_t patt, int size);
86862306a36Sopenharmony_ci
86962306a36Sopenharmony_cistatic inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
87062306a36Sopenharmony_ci{
87162306a36Sopenharmony_ci	return lnum >= 0 && lnum < vol->reserved_pebs;
87262306a36Sopenharmony_ci}
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci/* eba.c */
87562306a36Sopenharmony_cistruct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
87662306a36Sopenharmony_ci					   int nentries);
87762306a36Sopenharmony_civoid ubi_eba_destroy_table(struct ubi_eba_table *tbl);
87862306a36Sopenharmony_civoid ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
87962306a36Sopenharmony_ci			int nentries);
88062306a36Sopenharmony_civoid ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl);
88162306a36Sopenharmony_civoid ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
88262306a36Sopenharmony_ci		       struct ubi_eba_leb_desc *ldesc);
88362306a36Sopenharmony_cibool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum);
88462306a36Sopenharmony_ciint ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
88562306a36Sopenharmony_ci		      int lnum);
88662306a36Sopenharmony_ciint ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
88762306a36Sopenharmony_ci		     void *buf, int offset, int len, int check);
88862306a36Sopenharmony_ciint ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
88962306a36Sopenharmony_ci			struct ubi_sgl *sgl, int lnum, int offset, int len,
89062306a36Sopenharmony_ci			int check);
89162306a36Sopenharmony_ciint ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
89262306a36Sopenharmony_ci		      const void *buf, int offset, int len);
89362306a36Sopenharmony_ciint ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
89462306a36Sopenharmony_ci			 int lnum, const void *buf, int len, int used_ebs);
89562306a36Sopenharmony_ciint ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
89662306a36Sopenharmony_ci			      int lnum, const void *buf, int len);
89762306a36Sopenharmony_ciint ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
89862306a36Sopenharmony_ci		     struct ubi_vid_io_buf *vidb);
89962306a36Sopenharmony_ciint ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
90062306a36Sopenharmony_ciunsigned long long ubi_next_sqnum(struct ubi_device *ubi);
90162306a36Sopenharmony_ciint self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
90262306a36Sopenharmony_ci		   struct ubi_attach_info *ai_scan);
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ci/* wl.c */
90562306a36Sopenharmony_ciint ubi_wl_get_peb(struct ubi_device *ubi);
90662306a36Sopenharmony_ciint ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
90762306a36Sopenharmony_ci		   int pnum, int torture);
90862306a36Sopenharmony_ciint ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
90962306a36Sopenharmony_ciint ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
91062306a36Sopenharmony_ciint ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
91162306a36Sopenharmony_civoid ubi_wl_close(struct ubi_device *ubi);
91262306a36Sopenharmony_ciint ubi_thread(void *u);
91362306a36Sopenharmony_cistruct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
91462306a36Sopenharmony_ciint ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
91562306a36Sopenharmony_ci		      int lnum, int torture);
91662306a36Sopenharmony_ciint ubi_is_erase_work(struct ubi_work *wrk);
91762306a36Sopenharmony_civoid ubi_refill_pools(struct ubi_device *ubi);
91862306a36Sopenharmony_ciint ubi_ensure_anchor_pebs(struct ubi_device *ubi);
91962306a36Sopenharmony_ciint ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci/* io.c */
92262306a36Sopenharmony_ciint ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
92362306a36Sopenharmony_ci		int len);
92462306a36Sopenharmony_ciint ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
92562306a36Sopenharmony_ci		 int len);
92662306a36Sopenharmony_ciint ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
92762306a36Sopenharmony_ciint ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
92862306a36Sopenharmony_ciint ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
92962306a36Sopenharmony_ciint ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
93062306a36Sopenharmony_ci		       struct ubi_ec_hdr *ec_hdr, int verbose);
93162306a36Sopenharmony_ciint ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
93262306a36Sopenharmony_ci			struct ubi_ec_hdr *ec_hdr);
93362306a36Sopenharmony_ciint ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
93462306a36Sopenharmony_ci			struct ubi_vid_io_buf *vidb, int verbose);
93562306a36Sopenharmony_ciint ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
93662306a36Sopenharmony_ci			 struct ubi_vid_io_buf *vidb);
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci/* build.c */
93962306a36Sopenharmony_ciint ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
94062306a36Sopenharmony_ci		       int vid_hdr_offset, int max_beb_per1024,
94162306a36Sopenharmony_ci		       bool disable_fm);
94262306a36Sopenharmony_ciint ubi_detach_mtd_dev(int ubi_num, int anyway);
94362306a36Sopenharmony_cistruct ubi_device *ubi_get_device(int ubi_num);
94462306a36Sopenharmony_civoid ubi_put_device(struct ubi_device *ubi);
94562306a36Sopenharmony_cistruct ubi_device *ubi_get_by_major(int major);
94662306a36Sopenharmony_ciint ubi_major2num(int major);
94762306a36Sopenharmony_ciint ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
94862306a36Sopenharmony_ci		      int ntype);
94962306a36Sopenharmony_ciint ubi_notify_all(struct ubi_device *ubi, int ntype,
95062306a36Sopenharmony_ci		   struct notifier_block *nb);
95162306a36Sopenharmony_ciint ubi_enumerate_volumes(struct notifier_block *nb);
95262306a36Sopenharmony_civoid ubi_free_all_volumes(struct ubi_device *ubi);
95362306a36Sopenharmony_civoid ubi_free_internal_volumes(struct ubi_device *ubi);
95462306a36Sopenharmony_ci
95562306a36Sopenharmony_ci/* kapi.c */
95662306a36Sopenharmony_civoid ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
95762306a36Sopenharmony_civoid ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
95862306a36Sopenharmony_ci			    struct ubi_volume_info *vi);
95962306a36Sopenharmony_ci/* scan.c */
96062306a36Sopenharmony_ciint ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
96162306a36Sopenharmony_ci		      int pnum, const struct ubi_vid_hdr *vid_hdr);
96262306a36Sopenharmony_ci
96362306a36Sopenharmony_ci/* fastmap.c */
96462306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP
96562306a36Sopenharmony_cisize_t ubi_calc_fm_size(struct ubi_device *ubi);
96662306a36Sopenharmony_ciint ubi_update_fastmap(struct ubi_device *ubi);
96762306a36Sopenharmony_ciint ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
96862306a36Sopenharmony_ci		     struct ubi_attach_info *scan_ai);
96962306a36Sopenharmony_ciint ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count);
97062306a36Sopenharmony_civoid ubi_fastmap_destroy_checkmap(struct ubi_volume *vol);
97162306a36Sopenharmony_ci#else
97262306a36Sopenharmony_cistatic inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
97362306a36Sopenharmony_cistatic inline int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) { return 0; }
97462306a36Sopenharmony_cistatic inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}
97562306a36Sopenharmony_ci#endif
97662306a36Sopenharmony_ci
97762306a36Sopenharmony_ci/* block.c */
97862306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_BLOCK
97962306a36Sopenharmony_ciint ubiblock_init(void);
98062306a36Sopenharmony_civoid ubiblock_exit(void);
98162306a36Sopenharmony_ciint ubiblock_create(struct ubi_volume_info *vi);
98262306a36Sopenharmony_ciint ubiblock_remove(struct ubi_volume_info *vi);
98362306a36Sopenharmony_ci#else
98462306a36Sopenharmony_cistatic inline int ubiblock_init(void) { return 0; }
98562306a36Sopenharmony_cistatic inline void ubiblock_exit(void) {}
98662306a36Sopenharmony_cistatic inline int ubiblock_create(struct ubi_volume_info *vi)
98762306a36Sopenharmony_ci{
98862306a36Sopenharmony_ci	return -ENOSYS;
98962306a36Sopenharmony_ci}
99062306a36Sopenharmony_cistatic inline int ubiblock_remove(struct ubi_volume_info *vi)
99162306a36Sopenharmony_ci{
99262306a36Sopenharmony_ci	return -ENOSYS;
99362306a36Sopenharmony_ci}
99462306a36Sopenharmony_ci#endif
99562306a36Sopenharmony_ci
99662306a36Sopenharmony_ci/*
99762306a36Sopenharmony_ci * ubi_for_each_free_peb - walk the UBI free RB tree.
99862306a36Sopenharmony_ci * @ubi: UBI device description object
99962306a36Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
100062306a36Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
100162306a36Sopenharmony_ci */
100262306a36Sopenharmony_ci#define ubi_for_each_free_peb(ubi, e, tmp_rb)	\
100362306a36Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
100462306a36Sopenharmony_ci
100562306a36Sopenharmony_ci/*
100662306a36Sopenharmony_ci * ubi_for_each_used_peb - walk the UBI used RB tree.
100762306a36Sopenharmony_ci * @ubi: UBI device description object
100862306a36Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
100962306a36Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
101062306a36Sopenharmony_ci */
101162306a36Sopenharmony_ci#define ubi_for_each_used_peb(ubi, e, tmp_rb)	\
101262306a36Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
101362306a36Sopenharmony_ci
101462306a36Sopenharmony_ci/*
101562306a36Sopenharmony_ci * ubi_for_each_scub_peb - walk the UBI scub RB tree.
101662306a36Sopenharmony_ci * @ubi: UBI device description object
101762306a36Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
101862306a36Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
101962306a36Sopenharmony_ci */
102062306a36Sopenharmony_ci#define ubi_for_each_scrub_peb(ubi, e, tmp_rb)	\
102162306a36Sopenharmony_ci	ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci/*
102462306a36Sopenharmony_ci * ubi_for_each_protected_peb - walk the UBI protection queue.
102562306a36Sopenharmony_ci * @ubi: UBI device description object
102662306a36Sopenharmony_ci * @i: a integer used as counter
102762306a36Sopenharmony_ci * @e: a pointer to a ubi_wl_entry to use as cursor
102862306a36Sopenharmony_ci */
102962306a36Sopenharmony_ci#define ubi_for_each_protected_peb(ubi, i, e)	\
103062306a36Sopenharmony_ci	for ((i) = 0; (i) < UBI_PROT_QUEUE_LEN; (i)++)	\
103162306a36Sopenharmony_ci		list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
103262306a36Sopenharmony_ci
103362306a36Sopenharmony_ci/*
103462306a36Sopenharmony_ci * ubi_rb_for_each_entry - walk an RB-tree.
103562306a36Sopenharmony_ci * @rb: a pointer to type 'struct rb_node' to use as a loop counter
103662306a36Sopenharmony_ci * @pos: a pointer to RB-tree entry type to use as a loop counter
103762306a36Sopenharmony_ci * @root: RB-tree's root
103862306a36Sopenharmony_ci * @member: the name of the 'struct rb_node' within the RB-tree entry
103962306a36Sopenharmony_ci */
104062306a36Sopenharmony_ci#define ubi_rb_for_each_entry(rb, pos, root, member)                         \
104162306a36Sopenharmony_ci	for (rb = rb_first(root),                                            \
104262306a36Sopenharmony_ci	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL);     \
104362306a36Sopenharmony_ci	     rb;                                                             \
104462306a36Sopenharmony_ci	     rb = rb_next(rb),                                               \
104562306a36Sopenharmony_ci	     pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci/*
104862306a36Sopenharmony_ci * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
104962306a36Sopenharmony_ci *
105062306a36Sopenharmony_ci * @av: volume attaching information
105162306a36Sopenharmony_ci * @aeb: attaching eraseblock information
105262306a36Sopenharmony_ci * @list: the list to move to
105362306a36Sopenharmony_ci */
105462306a36Sopenharmony_cistatic inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
105562306a36Sopenharmony_ci					 struct ubi_ainf_peb *aeb,
105662306a36Sopenharmony_ci					 struct list_head *list)
105762306a36Sopenharmony_ci{
105862306a36Sopenharmony_ci		rb_erase(&aeb->u.rb, &av->root);
105962306a36Sopenharmony_ci		list_add_tail(&aeb->u.list, list);
106062306a36Sopenharmony_ci}
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci/**
106362306a36Sopenharmony_ci * ubi_init_vid_buf - Initialize a VID buffer
106462306a36Sopenharmony_ci * @ubi: the UBI device
106562306a36Sopenharmony_ci * @vidb: the VID buffer to initialize
106662306a36Sopenharmony_ci * @buf: the underlying buffer
106762306a36Sopenharmony_ci */
106862306a36Sopenharmony_cistatic inline void ubi_init_vid_buf(const struct ubi_device *ubi,
106962306a36Sopenharmony_ci				    struct ubi_vid_io_buf *vidb,
107062306a36Sopenharmony_ci				    void *buf)
107162306a36Sopenharmony_ci{
107262306a36Sopenharmony_ci	if (buf)
107362306a36Sopenharmony_ci		memset(buf, 0, ubi->vid_hdr_alsize);
107462306a36Sopenharmony_ci
107562306a36Sopenharmony_ci	vidb->buffer = buf;
107662306a36Sopenharmony_ci	vidb->hdr = buf + ubi->vid_hdr_shift;
107762306a36Sopenharmony_ci}
107862306a36Sopenharmony_ci
107962306a36Sopenharmony_ci/**
108062306a36Sopenharmony_ci * ubi_init_vid_buf - Allocate a VID buffer
108162306a36Sopenharmony_ci * @ubi: the UBI device
108262306a36Sopenharmony_ci * @gfp_flags: GFP flags to use for the allocation
108362306a36Sopenharmony_ci */
108462306a36Sopenharmony_cistatic inline struct ubi_vid_io_buf *
108562306a36Sopenharmony_ciubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
108662306a36Sopenharmony_ci{
108762306a36Sopenharmony_ci	struct ubi_vid_io_buf *vidb;
108862306a36Sopenharmony_ci	void *buf;
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	vidb = kzalloc(sizeof(*vidb), gfp_flags);
109162306a36Sopenharmony_ci	if (!vidb)
109262306a36Sopenharmony_ci		return NULL;
109362306a36Sopenharmony_ci
109462306a36Sopenharmony_ci	buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
109562306a36Sopenharmony_ci	if (!buf) {
109662306a36Sopenharmony_ci		kfree(vidb);
109762306a36Sopenharmony_ci		return NULL;
109862306a36Sopenharmony_ci	}
109962306a36Sopenharmony_ci
110062306a36Sopenharmony_ci	ubi_init_vid_buf(ubi, vidb, buf);
110162306a36Sopenharmony_ci
110262306a36Sopenharmony_ci	return vidb;
110362306a36Sopenharmony_ci}
110462306a36Sopenharmony_ci
110562306a36Sopenharmony_ci/**
110662306a36Sopenharmony_ci * ubi_free_vid_buf - Free a VID buffer
110762306a36Sopenharmony_ci * @vidb: the VID buffer to free
110862306a36Sopenharmony_ci */
110962306a36Sopenharmony_cistatic inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
111062306a36Sopenharmony_ci{
111162306a36Sopenharmony_ci	if (!vidb)
111262306a36Sopenharmony_ci		return;
111362306a36Sopenharmony_ci
111462306a36Sopenharmony_ci	kfree(vidb->buffer);
111562306a36Sopenharmony_ci	kfree(vidb);
111662306a36Sopenharmony_ci}
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci/**
111962306a36Sopenharmony_ci * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
112062306a36Sopenharmony_ci * @vidb: VID buffer
112162306a36Sopenharmony_ci */
112262306a36Sopenharmony_cistatic inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
112362306a36Sopenharmony_ci{
112462306a36Sopenharmony_ci	return vidb->hdr;
112562306a36Sopenharmony_ci}
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci/*
112862306a36Sopenharmony_ci * This function is equivalent to 'ubi_io_read()', but @offset is relative to
112962306a36Sopenharmony_ci * the beginning of the logical eraseblock, not to the beginning of the
113062306a36Sopenharmony_ci * physical eraseblock.
113162306a36Sopenharmony_ci */
113262306a36Sopenharmony_cistatic inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
113362306a36Sopenharmony_ci				   int pnum, int offset, int len)
113462306a36Sopenharmony_ci{
113562306a36Sopenharmony_ci	ubi_assert(offset >= 0);
113662306a36Sopenharmony_ci	return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
113762306a36Sopenharmony_ci}
113862306a36Sopenharmony_ci
113962306a36Sopenharmony_ci/*
114062306a36Sopenharmony_ci * This function is equivalent to 'ubi_io_write()', but @offset is relative to
114162306a36Sopenharmony_ci * the beginning of the logical eraseblock, not to the beginning of the
114262306a36Sopenharmony_ci * physical eraseblock.
114362306a36Sopenharmony_ci */
114462306a36Sopenharmony_cistatic inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
114562306a36Sopenharmony_ci				    int pnum, int offset, int len)
114662306a36Sopenharmony_ci{
114762306a36Sopenharmony_ci	ubi_assert(offset >= 0);
114862306a36Sopenharmony_ci	return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
114962306a36Sopenharmony_ci}
115062306a36Sopenharmony_ci
115162306a36Sopenharmony_ci/**
115262306a36Sopenharmony_ci * ubi_ro_mode - switch to read-only mode.
115362306a36Sopenharmony_ci * @ubi: UBI device description object
115462306a36Sopenharmony_ci */
115562306a36Sopenharmony_cistatic inline void ubi_ro_mode(struct ubi_device *ubi)
115662306a36Sopenharmony_ci{
115762306a36Sopenharmony_ci	if (!ubi->ro_mode) {
115862306a36Sopenharmony_ci		ubi->ro_mode = 1;
115962306a36Sopenharmony_ci		ubi_warn(ubi, "switch to read-only mode");
116062306a36Sopenharmony_ci		dump_stack();
116162306a36Sopenharmony_ci	}
116262306a36Sopenharmony_ci}
116362306a36Sopenharmony_ci
116462306a36Sopenharmony_ci/**
116562306a36Sopenharmony_ci * vol_id2idx - get table index by volume ID.
116662306a36Sopenharmony_ci * @ubi: UBI device description object
116762306a36Sopenharmony_ci * @vol_id: volume ID
116862306a36Sopenharmony_ci */
116962306a36Sopenharmony_cistatic inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
117062306a36Sopenharmony_ci{
117162306a36Sopenharmony_ci	if (vol_id >= UBI_INTERNAL_VOL_START)
117262306a36Sopenharmony_ci		return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
117362306a36Sopenharmony_ci	else
117462306a36Sopenharmony_ci		return vol_id;
117562306a36Sopenharmony_ci}
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci/**
117862306a36Sopenharmony_ci * idx2vol_id - get volume ID by table index.
117962306a36Sopenharmony_ci * @ubi: UBI device description object
118062306a36Sopenharmony_ci * @idx: table index
118162306a36Sopenharmony_ci */
118262306a36Sopenharmony_cistatic inline int idx2vol_id(const struct ubi_device *ubi, int idx)
118362306a36Sopenharmony_ci{
118462306a36Sopenharmony_ci	if (idx >= ubi->vtbl_slots)
118562306a36Sopenharmony_ci		return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
118662306a36Sopenharmony_ci	else
118762306a36Sopenharmony_ci		return idx;
118862306a36Sopenharmony_ci}
118962306a36Sopenharmony_ci
119062306a36Sopenharmony_ci/**
119162306a36Sopenharmony_ci * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
119262306a36Sopenharmony_ci * @vol_id: volume ID
119362306a36Sopenharmony_ci */
119462306a36Sopenharmony_cistatic inline bool ubi_is_fm_vol(int vol_id)
119562306a36Sopenharmony_ci{
119662306a36Sopenharmony_ci	switch (vol_id) {
119762306a36Sopenharmony_ci		case UBI_FM_SB_VOLUME_ID:
119862306a36Sopenharmony_ci		case UBI_FM_DATA_VOLUME_ID:
119962306a36Sopenharmony_ci		return true;
120062306a36Sopenharmony_ci	}
120162306a36Sopenharmony_ci
120262306a36Sopenharmony_ci	return false;
120362306a36Sopenharmony_ci}
120462306a36Sopenharmony_ci
120562306a36Sopenharmony_ci/**
120662306a36Sopenharmony_ci * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
120762306a36Sopenharmony_ci * @ubi: UBI device description object
120862306a36Sopenharmony_ci * @pnum: physical eraseblock to look for
120962306a36Sopenharmony_ci *
121062306a36Sopenharmony_ci * This function returns a wear leveling object if @pnum relates to the current
121162306a36Sopenharmony_ci * fastmap, @NULL otherwise.
121262306a36Sopenharmony_ci */
121362306a36Sopenharmony_cistatic inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi,
121462306a36Sopenharmony_ci						     int pnum)
121562306a36Sopenharmony_ci{
121662306a36Sopenharmony_ci	int i;
121762306a36Sopenharmony_ci
121862306a36Sopenharmony_ci	if (ubi->fm) {
121962306a36Sopenharmony_ci		for (i = 0; i < ubi->fm->used_blocks; i++) {
122062306a36Sopenharmony_ci			if (ubi->fm->e[i]->pnum == pnum)
122162306a36Sopenharmony_ci				return ubi->fm->e[i];
122262306a36Sopenharmony_ci		}
122362306a36Sopenharmony_ci	}
122462306a36Sopenharmony_ci
122562306a36Sopenharmony_ci	return NULL;
122662306a36Sopenharmony_ci}
122762306a36Sopenharmony_ci
122862306a36Sopenharmony_ci#endif /* !__UBI_UBI_H__ */
1229