Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0-or-later */
0002 /*
0003  * Copyright (c) International Business Machines Corp., 2006
0004  * Copyright (c) Nokia Corporation, 2006, 2007
0005  *
0006  * Author: Artem Bityutskiy (Битюцкий Артём)
0007  */
0008 
0009 #ifndef __UBI_UBI_H__
0010 #define __UBI_UBI_H__
0011 
0012 #include <linux/types.h>
0013 #include <linux/list.h>
0014 #include <linux/rbtree.h>
0015 #include <linux/sched.h>
0016 #include <linux/wait.h>
0017 #include <linux/mutex.h>
0018 #include <linux/rwsem.h>
0019 #include <linux/spinlock.h>
0020 #include <linux/fs.h>
0021 #include <linux/cdev.h>
0022 #include <linux/device.h>
0023 #include <linux/slab.h>
0024 #include <linux/string.h>
0025 #include <linux/vmalloc.h>
0026 #include <linux/notifier.h>
0027 #include <linux/mtd/mtd.h>
0028 #include <linux/mtd/ubi.h>
0029 #include <linux/pgtable.h>
0030 
0031 #include "ubi-media.h"
0032 
0033 /* Maximum number of supported UBI devices */
0034 #define UBI_MAX_DEVICES 32
0035 
0036 /* UBI name used for character devices, sysfs, etc */
0037 #define UBI_NAME_STR "ubi"
0038 
0039 struct ubi_device;
0040 
0041 /* Normal UBI messages */
0042 __printf(2, 3)
0043 void ubi_msg(const struct ubi_device *ubi, const char *fmt, ...);
0044 
0045 /* UBI warning messages */
0046 __printf(2, 3)
0047 void ubi_warn(const struct ubi_device *ubi, const char *fmt, ...);
0048 
0049 /* UBI error messages */
0050 __printf(2, 3)
0051 void ubi_err(const struct ubi_device *ubi, const char *fmt, ...);
0052 
0053 /* Background thread name pattern */
0054 #define UBI_BGT_NAME_PATTERN "ubi_bgt%dd"
0055 
0056 /*
0057  * This marker in the EBA table means that the LEB is um-mapped.
0058  * NOTE! It has to have the same value as %UBI_ALL.
0059  */
0060 #define UBI_LEB_UNMAPPED -1
0061 
0062 /*
0063  * In case of errors, UBI tries to repeat the operation several times before
0064  * returning error. The below constant defines how many times UBI re-tries.
0065  */
0066 #define UBI_IO_RETRIES 3
0067 
0068 /*
0069  * Length of the protection queue. The length is effectively equivalent to the
0070  * number of (global) erase cycles PEBs are protected from the wear-leveling
0071  * worker.
0072  */
0073 #define UBI_PROT_QUEUE_LEN 10
0074 
0075 /* The volume ID/LEB number/erase counter is unknown */
0076 #define UBI_UNKNOWN -1
0077 
0078 /*
0079  * The UBI debugfs directory name pattern and maximum name length (3 for "ubi"
0080  * + 2 for the number plus 1 for the trailing zero byte.
0081  */
0082 #define UBI_DFS_DIR_NAME "ubi%d"
0083 #define UBI_DFS_DIR_LEN  (3 + 2 + 1)
0084 
0085 /*
0086  * Error codes returned by the I/O sub-system.
0087  *
0088  * UBI_IO_FF: the read region of flash contains only 0xFFs
0089  * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
0090  *                     integrity error reported by the MTD driver
0091  *                     (uncorrectable ECC error in case of NAND)
0092  * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
0093  * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
0094  *                         data integrity error reported by the MTD driver
0095  *                         (uncorrectable ECC error in case of NAND)
0096  * UBI_IO_BITFLIPS: bit-flips were detected and corrected
0097  *
0098  * Note, it is probably better to have bit-flip and ebadmsg as flags which can
0099  * be or'ed with other error code. But this is a big change because there are
0100  * may callers, so it does not worth the risk of introducing a bug
0101  */
0102 enum {
0103     UBI_IO_FF = 1,
0104     UBI_IO_FF_BITFLIPS,
0105     UBI_IO_BAD_HDR,
0106     UBI_IO_BAD_HDR_EBADMSG,
0107     UBI_IO_BITFLIPS,
0108 };
0109 
0110 /*
0111  * Return codes of the 'ubi_eba_copy_leb()' function.
0112  *
0113  * MOVE_CANCEL_RACE: canceled because the volume is being deleted, the source
0114  *                   PEB was put meanwhile, or there is I/O on the source PEB
0115  * MOVE_SOURCE_RD_ERR: canceled because there was a read error from the source
0116  *                     PEB
0117  * MOVE_TARGET_RD_ERR: canceled because there was a read error from the target
0118  *                     PEB
0119  * MOVE_TARGET_WR_ERR: canceled because there was a write error to the target
0120  *                     PEB
0121  * MOVE_TARGET_BITFLIPS: canceled because a bit-flip was detected in the
0122  *                       target PEB
0123  * MOVE_RETRY: retry scrubbing the PEB
0124  */
0125 enum {
0126     MOVE_CANCEL_RACE = 1,
0127     MOVE_SOURCE_RD_ERR,
0128     MOVE_TARGET_RD_ERR,
0129     MOVE_TARGET_WR_ERR,
0130     MOVE_TARGET_BITFLIPS,
0131     MOVE_RETRY,
0132 };
0133 
0134 /*
0135  * Return codes of the fastmap sub-system
0136  *
0137  * UBI_NO_FASTMAP: No fastmap super block was found
0138  * UBI_BAD_FASTMAP: A fastmap was found but it's unusable
0139  */
0140 enum {
0141     UBI_NO_FASTMAP = 1,
0142     UBI_BAD_FASTMAP,
0143 };
0144 
0145 /*
0146  * Flags for emulate_power_cut in ubi_debug_info
0147  *
0148  * POWER_CUT_EC_WRITE: Emulate a power cut when writing an EC header
0149  * POWER_CUT_VID_WRITE: Emulate a power cut when writing a VID header
0150  */
0151 enum {
0152     POWER_CUT_EC_WRITE = 0x01,
0153     POWER_CUT_VID_WRITE = 0x02,
0154 };
0155 
0156 /**
0157  * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
0158  *             flash.
0159  * @hdr: a pointer to the VID header stored in buffer
0160  * @buffer: underlying buffer
0161  */
0162 struct ubi_vid_io_buf {
0163     struct ubi_vid_hdr *hdr;
0164     void *buffer;
0165 };
0166 
0167 /**
0168  * struct ubi_wl_entry - wear-leveling entry.
0169  * @u.rb: link in the corresponding (free/used) RB-tree
0170  * @u.list: link in the protection queue
0171  * @ec: erase counter
0172  * @pnum: physical eraseblock number
0173  *
0174  * This data structure is used in the WL sub-system. Each physical eraseblock
0175  * has a corresponding &struct wl_entry object which may be kept in different
0176  * RB-trees. See WL sub-system for details.
0177  */
0178 struct ubi_wl_entry {
0179     union {
0180         struct rb_node rb;
0181         struct list_head list;
0182     } u;
0183     int ec;
0184     int pnum;
0185 };
0186 
0187 /**
0188  * struct ubi_ltree_entry - an entry in the lock tree.
0189  * @rb: links RB-tree nodes
0190  * @vol_id: volume ID of the locked logical eraseblock
0191  * @lnum: locked logical eraseblock number
0192  * @users: how many tasks are using this logical eraseblock or wait for it
0193  * @mutex: read/write mutex to implement read/write access serialization to
0194  *         the (@vol_id, @lnum) logical eraseblock
0195  *
0196  * This data structure is used in the EBA sub-system to implement per-LEB
0197  * locking. When a logical eraseblock is being locked - corresponding
0198  * &struct ubi_ltree_entry object is inserted to the lock tree (@ubi->ltree).
0199  * See EBA sub-system for details.
0200  */
0201 struct ubi_ltree_entry {
0202     struct rb_node rb;
0203     int vol_id;
0204     int lnum;
0205     int users;
0206     struct rw_semaphore mutex;
0207 };
0208 
0209 /**
0210  * struct ubi_rename_entry - volume re-name description data structure.
0211  * @new_name_len: new volume name length
0212  * @new_name: new volume name
0213  * @remove: if not zero, this volume should be removed, not re-named
0214  * @desc: descriptor of the volume
0215  * @list: links re-name entries into a list
0216  *
0217  * This data structure is utilized in the multiple volume re-name code. Namely,
0218  * UBI first creates a list of &struct ubi_rename_entry objects from the
0219  * &struct ubi_rnvol_req request object, and then utilizes this list to do all
0220  * the job.
0221  */
0222 struct ubi_rename_entry {
0223     int new_name_len;
0224     char new_name[UBI_VOL_NAME_MAX + 1];
0225     int remove;
0226     struct ubi_volume_desc *desc;
0227     struct list_head list;
0228 };
0229 
0230 struct ubi_volume_desc;
0231 
0232 /**
0233  * struct ubi_fastmap_layout - in-memory fastmap data structure.
0234  * @e: PEBs used by the current fastmap
0235  * @to_be_tortured: if non-zero tortured this PEB
0236  * @used_blocks: number of used PEBs
0237  * @max_pool_size: maximal size of the user pool
0238  * @max_wl_pool_size: maximal size of the pool used by the WL sub-system
0239  */
0240 struct ubi_fastmap_layout {
0241     struct ubi_wl_entry *e[UBI_FM_MAX_BLOCKS];
0242     int to_be_tortured[UBI_FM_MAX_BLOCKS];
0243     int used_blocks;
0244     int max_pool_size;
0245     int max_wl_pool_size;
0246 };
0247 
0248 /**
0249  * struct ubi_fm_pool - in-memory fastmap pool
0250  * @pebs: PEBs in this pool
0251  * @used: number of used PEBs
0252  * @size: total number of PEBs in this pool
0253  * @max_size: maximal size of the pool
0254  *
0255  * A pool gets filled with up to max_size.
0256  * If all PEBs within the pool are used a new fastmap will be written
0257  * to the flash and the pool gets refilled with empty PEBs.
0258  *
0259  */
0260 struct ubi_fm_pool {
0261     int pebs[UBI_FM_MAX_POOL_SIZE];
0262     int used;
0263     int size;
0264     int max_size;
0265 };
0266 
0267 /**
0268  * struct ubi_eba_leb_desc - EBA logical eraseblock descriptor
0269  * @lnum: the logical eraseblock number
0270  * @pnum: the physical eraseblock where the LEB can be found
0271  *
0272  * This structure is here to hide EBA's internal from other part of the
0273  * UBI implementation.
0274  *
0275  * One can query the position of a LEB by calling ubi_eba_get_ldesc().
0276  */
0277 struct ubi_eba_leb_desc {
0278     int lnum;
0279     int pnum;
0280 };
0281 
0282 /**
0283  * struct ubi_volume - UBI volume description data structure.
0284  * @dev: device object to make use of the the Linux device model
0285  * @cdev: character device object to create character device
0286  * @ubi: reference to the UBI device description object
0287  * @vol_id: volume ID
0288  * @ref_count: volume reference count
0289  * @readers: number of users holding this volume in read-only mode
0290  * @writers: number of users holding this volume in read-write mode
0291  * @exclusive: whether somebody holds this volume in exclusive mode
0292  * @metaonly: whether somebody is altering only meta data of this volume
0293  *
0294  * @reserved_pebs: how many physical eraseblocks are reserved for this volume
0295  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
0296  * @usable_leb_size: logical eraseblock size without padding
0297  * @used_ebs: how many logical eraseblocks in this volume contain data
0298  * @last_eb_bytes: how many bytes are stored in the last logical eraseblock
0299  * @used_bytes: how many bytes of data this volume contains
0300  * @alignment: volume alignment
0301  * @data_pad: how many bytes are not used at the end of physical eraseblocks to
0302  *            satisfy the requested alignment
0303  * @name_len: volume name length
0304  * @name: volume name
0305  *
0306  * @upd_ebs: how many eraseblocks are expected to be updated
0307  * @ch_lnum: LEB number which is being changing by the atomic LEB change
0308  *           operation
0309  * @upd_bytes: how many bytes are expected to be received for volume update or
0310  *             atomic LEB change
0311  * @upd_received: how many bytes were already received for volume update or
0312  *                atomic LEB change
0313  * @upd_buf: update buffer which is used to collect update data or data for
0314  *           atomic LEB change
0315  *
0316  * @eba_tbl: EBA table of this volume (LEB->PEB mapping)
0317  * @skip_check: %1 if CRC check of this static volume should be skipped.
0318  *      Directly reflects the presence of the
0319  *      %UBI_VTBL_SKIP_CRC_CHECK_FLG flag in the vtbl entry
0320  * @checked: %1 if this static volume was checked
0321  * @corrupted: %1 if the volume is corrupted (static volumes only)
0322  * @upd_marker: %1 if the update marker is set for this volume
0323  * @updating: %1 if the volume is being updated
0324  * @changing_leb: %1 if the atomic LEB change ioctl command is in progress
0325  * @direct_writes: %1 if direct writes are enabled for this volume
0326  *
0327  * @checkmap: bitmap to remember which PEB->LEB mappings got checked,
0328  *            protected by UBI LEB lock tree.
0329  *
0330  * The @corrupted field indicates that the volume's contents is corrupted.
0331  * Since UBI protects only static volumes, this field is not relevant to
0332  * dynamic volumes - it is user's responsibility to assure their data
0333  * integrity.
0334  *
0335  * The @upd_marker flag indicates that this volume is either being updated at
0336  * the moment or is damaged because of an unclean reboot.
0337  */
0338 struct ubi_volume {
0339     struct device dev;
0340     struct cdev cdev;
0341     struct ubi_device *ubi;
0342     int vol_id;
0343     int ref_count;
0344     int readers;
0345     int writers;
0346     int exclusive;
0347     int metaonly;
0348 
0349     int reserved_pebs;
0350     int vol_type;
0351     int usable_leb_size;
0352     int used_ebs;
0353     int last_eb_bytes;
0354     long long used_bytes;
0355     int alignment;
0356     int data_pad;
0357     int name_len;
0358     char name[UBI_VOL_NAME_MAX + 1];
0359 
0360     int upd_ebs;
0361     int ch_lnum;
0362     long long upd_bytes;
0363     long long upd_received;
0364     void *upd_buf;
0365 
0366     struct ubi_eba_table *eba_tbl;
0367     unsigned int skip_check:1;
0368     unsigned int checked:1;
0369     unsigned int corrupted:1;
0370     unsigned int upd_marker:1;
0371     unsigned int updating:1;
0372     unsigned int changing_leb:1;
0373     unsigned int direct_writes:1;
0374 
0375 #ifdef CONFIG_MTD_UBI_FASTMAP
0376     unsigned long *checkmap;
0377 #endif
0378 };
0379 
0380 /**
0381  * struct ubi_volume_desc - UBI volume descriptor returned when it is opened.
0382  * @vol: reference to the corresponding volume description object
0383  * @mode: open mode (%UBI_READONLY, %UBI_READWRITE, %UBI_EXCLUSIVE
0384  * or %UBI_METAONLY)
0385  */
0386 struct ubi_volume_desc {
0387     struct ubi_volume *vol;
0388     int mode;
0389 };
0390 
0391 /**
0392  * struct ubi_debug_info - debugging information for an UBI device.
0393  *
0394  * @chk_gen: if UBI general extra checks are enabled
0395  * @chk_io: if UBI I/O extra checks are enabled
0396  * @chk_fastmap: if UBI fastmap extra checks are enabled
0397  * @disable_bgt: disable the background task for testing purposes
0398  * @emulate_bitflips: emulate bit-flips for testing purposes
0399  * @emulate_io_failures: emulate write/erase failures for testing purposes
0400  * @emulate_power_cut: emulate power cut for testing purposes
0401  * @power_cut_counter: count down for writes left until emulated power cut
0402  * @power_cut_min: minimum number of writes before emulating a power cut
0403  * @power_cut_max: maximum number of writes until emulating a power cut
0404  * @dfs_dir_name: name of debugfs directory containing files of this UBI device
0405  * @dfs_dir: direntry object of the UBI device debugfs directory
0406  * @dfs_chk_gen: debugfs knob to enable UBI general extra checks
0407  * @dfs_chk_io: debugfs knob to enable UBI I/O extra checks
0408  * @dfs_chk_fastmap: debugfs knob to enable UBI fastmap extra checks
0409  * @dfs_disable_bgt: debugfs knob to disable the background task
0410  * @dfs_emulate_bitflips: debugfs knob to emulate bit-flips
0411  * @dfs_emulate_io_failures: debugfs knob to emulate write/erase failures
0412  * @dfs_emulate_power_cut: debugfs knob to emulate power cuts
0413  * @dfs_power_cut_min: debugfs knob for minimum writes before power cut
0414  * @dfs_power_cut_max: debugfs knob for maximum writes until power cut
0415  */
0416 struct ubi_debug_info {
0417     unsigned int chk_gen:1;
0418     unsigned int chk_io:1;
0419     unsigned int chk_fastmap:1;
0420     unsigned int disable_bgt:1;
0421     unsigned int emulate_bitflips:1;
0422     unsigned int emulate_io_failures:1;
0423     unsigned int emulate_power_cut:2;
0424     unsigned int power_cut_counter;
0425     unsigned int power_cut_min;
0426     unsigned int power_cut_max;
0427     char dfs_dir_name[UBI_DFS_DIR_LEN + 1];
0428     struct dentry *dfs_dir;
0429     struct dentry *dfs_chk_gen;
0430     struct dentry *dfs_chk_io;
0431     struct dentry *dfs_chk_fastmap;
0432     struct dentry *dfs_disable_bgt;
0433     struct dentry *dfs_emulate_bitflips;
0434     struct dentry *dfs_emulate_io_failures;
0435     struct dentry *dfs_emulate_power_cut;
0436     struct dentry *dfs_power_cut_min;
0437     struct dentry *dfs_power_cut_max;
0438 };
0439 
0440 /**
0441  * struct ubi_device - UBI device description structure
0442  * @dev: UBI device object to use the the Linux device model
0443  * @cdev: character device object to create character device
0444  * @ubi_num: UBI device number
0445  * @ubi_name: UBI device name
0446  * @vol_count: number of volumes in this UBI device
0447  * @volumes: volumes of this UBI device
0448  * @volumes_lock: protects @volumes, @rsvd_pebs, @avail_pebs, beb_rsvd_pebs,
0449  *                @beb_rsvd_level, @bad_peb_count, @good_peb_count, @vol_count,
0450  *                @vol->readers, @vol->writers, @vol->exclusive,
0451  *                @vol->metaonly, @vol->ref_count, @vol->mapping and
0452  *                @vol->eba_tbl.
0453  * @ref_count: count of references on the UBI device
0454  * @image_seq: image sequence number recorded on EC headers
0455  *
0456  * @rsvd_pebs: count of reserved physical eraseblocks
0457  * @avail_pebs: count of available physical eraseblocks
0458  * @beb_rsvd_pebs: how many physical eraseblocks are reserved for bad PEB
0459  *                 handling
0460  * @beb_rsvd_level: normal level of PEBs reserved for bad PEB handling
0461  *
0462  * @autoresize_vol_id: ID of the volume which has to be auto-resized at the end
0463  *                     of UBI initialization
0464  * @vtbl_slots: how many slots are available in the volume table
0465  * @vtbl_size: size of the volume table in bytes
0466  * @vtbl: in-RAM volume table copy
0467  * @device_mutex: protects on-flash volume table and serializes volume
0468  *                creation, deletion, update, re-size, re-name and set
0469  *                property
0470  *
0471  * @max_ec: current highest erase counter value
0472  * @mean_ec: current mean erase counter value
0473  *
0474  * @global_sqnum: global sequence number
0475  * @ltree_lock: protects the lock tree and @global_sqnum
0476  * @ltree: the lock tree
0477  * @alc_mutex: serializes "atomic LEB change" operations
0478  *
0479  * @fm_disabled: non-zero if fastmap is disabled (default)
0480  * @fm: in-memory data structure of the currently used fastmap
0481  * @fm_pool: in-memory data structure of the fastmap pool
0482  * @fm_wl_pool: in-memory data structure of the fastmap pool used by the WL
0483  *      sub-system
0484  * @fm_protect: serializes ubi_update_fastmap(), protects @fm_buf and makes sure
0485  * that critical sections cannot be interrupted by ubi_update_fastmap()
0486  * @fm_buf: vmalloc()'d buffer which holds the raw fastmap
0487  * @fm_size: fastmap size in bytes
0488  * @fm_eba_sem: allows ubi_update_fastmap() to block EBA table changes
0489  * @fm_work: fastmap work queue
0490  * @fm_work_scheduled: non-zero if fastmap work was scheduled
0491  * @fast_attach: non-zero if UBI was attached by fastmap
0492  * @fm_anchor: The next anchor PEB to use for fastmap
0493  * @fm_do_produce_anchor: If true produce an anchor PEB in wl
0494  *
0495  * @used: RB-tree of used physical eraseblocks
0496  * @erroneous: RB-tree of erroneous used physical eraseblocks
0497  * @free: RB-tree of free physical eraseblocks
0498  * @free_count: Contains the number of elements in @free
0499  * @scrub: RB-tree of physical eraseblocks which need scrubbing
0500  * @pq: protection queue (contain physical eraseblocks which are temporarily
0501  *      protected from the wear-leveling worker)
0502  * @pq_head: protection queue head
0503  * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
0504  *       @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
0505  *       @erroneous, @erroneous_peb_count, @fm_work_scheduled, @fm_pool,
0506  *       and @fm_wl_pool fields
0507  * @move_mutex: serializes eraseblock moves
0508  * @work_sem: used to wait for all the scheduled works to finish and prevent
0509  * new works from being submitted
0510  * @wl_scheduled: non-zero if the wear-leveling was scheduled
0511  * @lookuptbl: a table to quickly find a &struct ubi_wl_entry object for any
0512  *             physical eraseblock
0513  * @move_from: physical eraseblock from where the data is being moved
0514  * @move_to: physical eraseblock where the data is being moved to
0515  * @move_to_put: if the "to" PEB was put
0516  * @works: list of pending works
0517  * @works_count: count of pending works
0518  * @bgt_thread: background thread description object
0519  * @thread_enabled: if the background thread is enabled
0520  * @bgt_name: background thread name
0521  *
0522  * @flash_size: underlying MTD device size (in bytes)
0523  * @peb_count: count of physical eraseblocks on the MTD device
0524  * @peb_size: physical eraseblock size
0525  * @bad_peb_limit: top limit of expected bad physical eraseblocks
0526  * @bad_peb_count: count of bad physical eraseblocks
0527  * @good_peb_count: count of good physical eraseblocks
0528  * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
0529  *                  used by UBI)
0530  * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
0531  * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
0532  * @min_io_size: minimal input/output unit size of the underlying MTD device
0533  * @hdrs_min_io_size: minimal I/O unit size used for VID and EC headers
0534  * @ro_mode: if the UBI device is in read-only mode
0535  * @leb_size: logical eraseblock size
0536  * @leb_start: starting offset of logical eraseblocks within physical
0537  *             eraseblocks
0538  * @ec_hdr_alsize: size of the EC header aligned to @hdrs_min_io_size
0539  * @vid_hdr_alsize: size of the VID header aligned to @hdrs_min_io_size
0540  * @vid_hdr_offset: starting offset of the volume identifier header (might be
0541  *                  unaligned)
0542  * @vid_hdr_aloffset: starting offset of the VID header aligned to
0543  *                    @hdrs_min_io_size
0544  * @vid_hdr_shift: contains @vid_hdr_offset - @vid_hdr_aloffset
0545  * @bad_allowed: whether the MTD device admits bad physical eraseblocks or not
0546  * @nor_flash: non-zero if working on top of NOR flash
0547  * @max_write_size: maximum amount of bytes the underlying flash can write at a
0548  *                  time (MTD write buffer size)
0549  * @mtd: MTD device descriptor
0550  *
0551  * @peb_buf: a buffer of PEB size used for different purposes
0552  * @buf_mutex: protects @peb_buf
0553  * @ckvol_mutex: serializes static volume checking when opening
0554  *
0555  * @dbg: debugging information for this UBI device
0556  */
0557 struct ubi_device {
0558     struct cdev cdev;
0559     struct device dev;
0560     int ubi_num;
0561     char ubi_name[sizeof(UBI_NAME_STR)+5];
0562     int vol_count;
0563     struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT];
0564     spinlock_t volumes_lock;
0565     int ref_count;
0566     int image_seq;
0567 
0568     int rsvd_pebs;
0569     int avail_pebs;
0570     int beb_rsvd_pebs;
0571     int beb_rsvd_level;
0572     int bad_peb_limit;
0573 
0574     int autoresize_vol_id;
0575     int vtbl_slots;
0576     int vtbl_size;
0577     struct ubi_vtbl_record *vtbl;
0578     struct mutex device_mutex;
0579 
0580     int max_ec;
0581     /* Note, mean_ec is not updated run-time - should be fixed */
0582     int mean_ec;
0583 
0584     /* EBA sub-system's stuff */
0585     unsigned long long global_sqnum;
0586     spinlock_t ltree_lock;
0587     struct rb_root ltree;
0588     struct mutex alc_mutex;
0589 
0590     /* Fastmap stuff */
0591     int fm_disabled;
0592     struct ubi_fastmap_layout *fm;
0593     struct ubi_fm_pool fm_pool;
0594     struct ubi_fm_pool fm_wl_pool;
0595     struct rw_semaphore fm_eba_sem;
0596     struct rw_semaphore fm_protect;
0597     void *fm_buf;
0598     size_t fm_size;
0599     struct work_struct fm_work;
0600     int fm_work_scheduled;
0601     int fast_attach;
0602     struct ubi_wl_entry *fm_anchor;
0603     int fm_do_produce_anchor;
0604 
0605     /* Wear-leveling sub-system's stuff */
0606     struct rb_root used;
0607     struct rb_root erroneous;
0608     struct rb_root free;
0609     int free_count;
0610     struct rb_root scrub;
0611     struct list_head pq[UBI_PROT_QUEUE_LEN];
0612     int pq_head;
0613     spinlock_t wl_lock;
0614     struct mutex move_mutex;
0615     struct rw_semaphore work_sem;
0616     int wl_scheduled;
0617     struct ubi_wl_entry **lookuptbl;
0618     struct ubi_wl_entry *move_from;
0619     struct ubi_wl_entry *move_to;
0620     int move_to_put;
0621     struct list_head works;
0622     int works_count;
0623     struct task_struct *bgt_thread;
0624     int thread_enabled;
0625     char bgt_name[sizeof(UBI_BGT_NAME_PATTERN)+2];
0626 
0627     /* I/O sub-system's stuff */
0628     long long flash_size;
0629     int peb_count;
0630     int peb_size;
0631     int bad_peb_count;
0632     int good_peb_count;
0633     int corr_peb_count;
0634     int erroneous_peb_count;
0635     int max_erroneous;
0636     int min_io_size;
0637     int hdrs_min_io_size;
0638     int ro_mode;
0639     int leb_size;
0640     int leb_start;
0641     int ec_hdr_alsize;
0642     int vid_hdr_alsize;
0643     int vid_hdr_offset;
0644     int vid_hdr_aloffset;
0645     int vid_hdr_shift;
0646     unsigned int bad_allowed:1;
0647     unsigned int nor_flash:1;
0648     int max_write_size;
0649     struct mtd_info *mtd;
0650 
0651     void *peb_buf;
0652     struct mutex buf_mutex;
0653     struct mutex ckvol_mutex;
0654 
0655     struct ubi_debug_info dbg;
0656 };
0657 
0658 /**
0659  * struct ubi_ainf_peb - attach information about a physical eraseblock.
0660  * @ec: erase counter (%UBI_UNKNOWN if it is unknown)
0661  * @pnum: physical eraseblock number
0662  * @vol_id: ID of the volume this LEB belongs to
0663  * @lnum: logical eraseblock number
0664  * @scrub: if this physical eraseblock needs scrubbing
0665  * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
0666  * @sqnum: sequence number
0667  * @u: unions RB-tree or @list links
0668  * @u.rb: link in the per-volume RB-tree of &struct ubi_ainf_peb objects
0669  * @u.list: link in one of the eraseblock lists
0670  *
0671  * One object of this type is allocated for each physical eraseblock when
0672  * attaching an MTD device. Note, if this PEB does not belong to any LEB /
0673  * volume, the @vol_id and @lnum fields are initialized to %UBI_UNKNOWN.
0674  */
0675 struct ubi_ainf_peb {
0676     int ec;
0677     int pnum;
0678     int vol_id;
0679     int lnum;
0680     unsigned int scrub:1;
0681     unsigned int copy_flag:1;
0682     unsigned long long sqnum;
0683     union {
0684         struct rb_node rb;
0685         struct list_head list;
0686     } u;
0687 };
0688 
0689 /**
0690  * struct ubi_ainf_volume - attaching information about a volume.
0691  * @vol_id: volume ID
0692  * @highest_lnum: highest logical eraseblock number in this volume
0693  * @leb_count: number of logical eraseblocks in this volume
0694  * @vol_type: volume type
0695  * @used_ebs: number of used logical eraseblocks in this volume (only for
0696  *            static volumes)
0697  * @last_data_size: amount of data in the last logical eraseblock of this
0698  *                  volume (always equivalent to the usable logical eraseblock
0699  *                  size in case of dynamic volumes)
0700  * @data_pad: how many bytes at the end of logical eraseblocks of this volume
0701  *            are not used (due to volume alignment)
0702  * @compat: compatibility flags of this volume
0703  * @rb: link in the volume RB-tree
0704  * @root: root of the RB-tree containing all the eraseblock belonging to this
0705  *        volume (&struct ubi_ainf_peb objects)
0706  *
0707  * One object of this type is allocated for each volume when attaching an MTD
0708  * device.
0709  */
0710 struct ubi_ainf_volume {
0711     int vol_id;
0712     int highest_lnum;
0713     int leb_count;
0714     int vol_type;
0715     int used_ebs;
0716     int last_data_size;
0717     int data_pad;
0718     int compat;
0719     struct rb_node rb;
0720     struct rb_root root;
0721 };
0722 
0723 /**
0724  * struct ubi_attach_info - MTD device attaching information.
0725  * @volumes: root of the volume RB-tree
0726  * @corr: list of corrupted physical eraseblocks
0727  * @free: list of free physical eraseblocks
0728  * @erase: list of physical eraseblocks which have to be erased
0729  * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
0730  *         those belonging to "preserve"-compatible internal volumes)
0731  * @fastmap: list of physical eraseblocks which relate to fastmap (e.g.,
0732  *           eraseblocks of the current and not yet erased old fastmap blocks)
0733  * @corr_peb_count: count of PEBs in the @corr list
0734  * @empty_peb_count: count of PEBs which are presumably empty (contain only
0735  *                   0xFF bytes)
0736  * @alien_peb_count: count of PEBs in the @alien list
0737  * @bad_peb_count: count of bad physical eraseblocks
0738  * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
0739  *                       as bad yet, but which look like bad
0740  * @vols_found: number of volumes found
0741  * @highest_vol_id: highest volume ID
0742  * @is_empty: flag indicating whether the MTD device is empty or not
0743  * @force_full_scan: flag indicating whether we need to do a full scan and drop
0744              all existing Fastmap data structures
0745  * @min_ec: lowest erase counter value
0746  * @max_ec: highest erase counter value
0747  * @max_sqnum: highest sequence number value
0748  * @mean_ec: mean erase counter value
0749  * @ec_sum: a temporary variable used when calculating @mean_ec
0750  * @ec_count: a temporary variable used when calculating @mean_ec
0751  * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
0752  * @ech: temporary EC header. Only available during scan
0753  * @vidh: temporary VID buffer. Only available during scan
0754  *
0755  * This data structure contains the result of attaching an MTD device and may
0756  * be used by other UBI sub-systems to build final UBI data structures, further
0757  * error-recovery and so on.
0758  */
0759 struct ubi_attach_info {
0760     struct rb_root volumes;
0761     struct list_head corr;
0762     struct list_head free;
0763     struct list_head erase;
0764     struct list_head alien;
0765     struct list_head fastmap;
0766     int corr_peb_count;
0767     int empty_peb_count;
0768     int alien_peb_count;
0769     int bad_peb_count;
0770     int maybe_bad_peb_count;
0771     int vols_found;
0772     int highest_vol_id;
0773     int is_empty;
0774     int force_full_scan;
0775     int min_ec;
0776     int max_ec;
0777     unsigned long long max_sqnum;
0778     int mean_ec;
0779     uint64_t ec_sum;
0780     int ec_count;
0781     struct kmem_cache *aeb_slab_cache;
0782     struct ubi_ec_hdr *ech;
0783     struct ubi_vid_io_buf *vidb;
0784 };
0785 
0786 /**
0787  * struct ubi_work - UBI work description data structure.
0788  * @list: a link in the list of pending works
0789  * @func: worker function
0790  * @e: physical eraseblock to erase
0791  * @vol_id: the volume ID on which this erasure is being performed
0792  * @lnum: the logical eraseblock number
0793  * @torture: if the physical eraseblock has to be tortured
0794  *
0795  * The @func pointer points to the worker function. If the @shutdown argument is
0796  * not zero, the worker has to free the resources and exit immediately as the
0797  * WL sub-system is shutting down.
0798  * The worker has to return zero in case of success and a negative error code in
0799  * case of failure.
0800  */
0801 struct ubi_work {
0802     struct list_head list;
0803     int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int shutdown);
0804     /* The below fields are only relevant to erasure works */
0805     struct ubi_wl_entry *e;
0806     int vol_id;
0807     int lnum;
0808     int torture;
0809 };
0810 
0811 #include "debug.h"
0812 
0813 extern struct kmem_cache *ubi_wl_entry_slab;
0814 extern const struct file_operations ubi_ctrl_cdev_operations;
0815 extern const struct file_operations ubi_cdev_operations;
0816 extern const struct file_operations ubi_vol_cdev_operations;
0817 extern struct class ubi_class;
0818 extern struct mutex ubi_devices_mutex;
0819 extern struct blocking_notifier_head ubi_notifiers;
0820 
0821 /* attach.c */
0822 struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum,
0823                    int ec);
0824 void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb);
0825 int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum,
0826           int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips);
0827 struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id);
0828 struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai,
0829                     int vol_id);
0830 void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av);
0831 struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi,
0832                        struct ubi_attach_info *ai);
0833 int ubi_attach(struct ubi_device *ubi, int force_scan);
0834 void ubi_destroy_ai(struct ubi_attach_info *ai);
0835 
0836 /* vtbl.c */
0837 int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
0838                struct ubi_vtbl_record *vtbl_rec);
0839 int ubi_vtbl_rename_volumes(struct ubi_device *ubi,
0840                 struct list_head *rename_list);
0841 int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_attach_info *ai);
0842 
0843 /* vmt.c */
0844 int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req);
0845 int ubi_remove_volume(struct ubi_volume_desc *desc, int no_vtbl);
0846 int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs);
0847 int ubi_rename_volumes(struct ubi_device *ubi, struct list_head *rename_list);
0848 int ubi_add_volume(struct ubi_device *ubi, struct ubi_volume *vol);
0849 void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol);
0850 
0851 /* upd.c */
0852 int ubi_start_update(struct ubi_device *ubi, struct ubi_volume *vol,
0853              long long bytes);
0854 int ubi_more_update_data(struct ubi_device *ubi, struct ubi_volume *vol,
0855              const void __user *buf, int count);
0856 int ubi_start_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
0857              const struct ubi_leb_change_req *req);
0858 int ubi_more_leb_change_data(struct ubi_device *ubi, struct ubi_volume *vol,
0859                  const void __user *buf, int count);
0860 
0861 /* misc.c */
0862 int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
0863               int length);
0864 int ubi_check_volume(struct ubi_device *ubi, int vol_id);
0865 void ubi_update_reserved(struct ubi_device *ubi);
0866 void ubi_calculate_reserved(struct ubi_device *ubi);
0867 int ubi_check_pattern(const void *buf, uint8_t patt, int size);
0868 
0869 static inline bool ubi_leb_valid(struct ubi_volume *vol, int lnum)
0870 {
0871     return lnum >= 0 && lnum < vol->reserved_pebs;
0872 }
0873 
0874 /* eba.c */
0875 struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol,
0876                        int nentries);
0877 void ubi_eba_destroy_table(struct ubi_eba_table *tbl);
0878 void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst,
0879             int nentries);
0880 void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl);
0881 void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum,
0882                struct ubi_eba_leb_desc *ldesc);
0883 bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum);
0884 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
0885               int lnum);
0886 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
0887              void *buf, int offset, int len, int check);
0888 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
0889             struct ubi_sgl *sgl, int lnum, int offset, int len,
0890             int check);
0891 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
0892               const void *buf, int offset, int len);
0893 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
0894              int lnum, const void *buf, int len, int used_ebs);
0895 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
0896                   int lnum, const void *buf, int len);
0897 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
0898              struct ubi_vid_io_buf *vidb);
0899 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
0900 unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
0901 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
0902            struct ubi_attach_info *ai_scan);
0903 
0904 /* wl.c */
0905 int ubi_wl_get_peb(struct ubi_device *ubi);
0906 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
0907            int pnum, int torture);
0908 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum);
0909 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum);
0910 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
0911 void ubi_wl_close(struct ubi_device *ubi);
0912 int ubi_thread(void *u);
0913 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor);
0914 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *used_e,
0915               int lnum, int torture);
0916 int ubi_is_erase_work(struct ubi_work *wrk);
0917 void ubi_refill_pools(struct ubi_device *ubi);
0918 int ubi_ensure_anchor_pebs(struct ubi_device *ubi);
0919 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force_scrub);
0920 
0921 /* io.c */
0922 int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
0923         int len);
0924 int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
0925          int len);
0926 int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture);
0927 int ubi_io_is_bad(const struct ubi_device *ubi, int pnum);
0928 int ubi_io_mark_bad(const struct ubi_device *ubi, int pnum);
0929 int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
0930                struct ubi_ec_hdr *ec_hdr, int verbose);
0931 int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
0932             struct ubi_ec_hdr *ec_hdr);
0933 int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
0934             struct ubi_vid_io_buf *vidb, int verbose);
0935 int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
0936              struct ubi_vid_io_buf *vidb);
0937 
0938 /* build.c */
0939 int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
0940                int vid_hdr_offset, int max_beb_per1024);
0941 int ubi_detach_mtd_dev(int ubi_num, int anyway);
0942 struct ubi_device *ubi_get_device(int ubi_num);
0943 void ubi_put_device(struct ubi_device *ubi);
0944 struct ubi_device *ubi_get_by_major(int major);
0945 int ubi_major2num(int major);
0946 int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol,
0947               int ntype);
0948 int ubi_notify_all(struct ubi_device *ubi, int ntype,
0949            struct notifier_block *nb);
0950 int ubi_enumerate_volumes(struct notifier_block *nb);
0951 void ubi_free_all_volumes(struct ubi_device *ubi);
0952 void ubi_free_internal_volumes(struct ubi_device *ubi);
0953 
0954 /* kapi.c */
0955 void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di);
0956 void ubi_do_get_volume_info(struct ubi_device *ubi, struct ubi_volume *vol,
0957                 struct ubi_volume_info *vi);
0958 /* scan.c */
0959 int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
0960               int pnum, const struct ubi_vid_hdr *vid_hdr);
0961 
0962 /* fastmap.c */
0963 #ifdef CONFIG_MTD_UBI_FASTMAP
0964 size_t ubi_calc_fm_size(struct ubi_device *ubi);
0965 int ubi_update_fastmap(struct ubi_device *ubi);
0966 int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
0967              struct ubi_attach_info *scan_ai);
0968 int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count);
0969 void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol);
0970 #else
0971 static inline int ubi_update_fastmap(struct ubi_device *ubi) { return 0; }
0972 static inline int ubi_fastmap_init_checkmap(struct ubi_volume *vol, int leb_count) { return 0; }
0973 static inline void ubi_fastmap_destroy_checkmap(struct ubi_volume *vol) {}
0974 #endif
0975 
0976 /* block.c */
0977 #ifdef CONFIG_MTD_UBI_BLOCK
0978 int ubiblock_init(void);
0979 void ubiblock_exit(void);
0980 int ubiblock_create(struct ubi_volume_info *vi);
0981 int ubiblock_remove(struct ubi_volume_info *vi);
0982 #else
0983 static inline int ubiblock_init(void) { return 0; }
0984 static inline void ubiblock_exit(void) {}
0985 static inline int ubiblock_create(struct ubi_volume_info *vi)
0986 {
0987     return -ENOSYS;
0988 }
0989 static inline int ubiblock_remove(struct ubi_volume_info *vi)
0990 {
0991     return -ENOSYS;
0992 }
0993 #endif
0994 
0995 /*
0996  * ubi_for_each_free_peb - walk the UBI free RB tree.
0997  * @ubi: UBI device description object
0998  * @e: a pointer to a ubi_wl_entry to use as cursor
0999  * @pos: a pointer to RB-tree entry type to use as a loop counter
1000  */
1001 #define ubi_for_each_free_peb(ubi, e, tmp_rb)   \
1002     ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->free, u.rb)
1003 
1004 /*
1005  * ubi_for_each_used_peb - walk the UBI used RB tree.
1006  * @ubi: UBI device description object
1007  * @e: a pointer to a ubi_wl_entry to use as cursor
1008  * @pos: a pointer to RB-tree entry type to use as a loop counter
1009  */
1010 #define ubi_for_each_used_peb(ubi, e, tmp_rb)   \
1011     ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->used, u.rb)
1012 
1013 /*
1014  * ubi_for_each_scub_peb - walk the UBI scub RB tree.
1015  * @ubi: UBI device description object
1016  * @e: a pointer to a ubi_wl_entry to use as cursor
1017  * @pos: a pointer to RB-tree entry type to use as a loop counter
1018  */
1019 #define ubi_for_each_scrub_peb(ubi, e, tmp_rb)  \
1020     ubi_rb_for_each_entry((tmp_rb), (e), &(ubi)->scrub, u.rb)
1021 
1022 /*
1023  * ubi_for_each_protected_peb - walk the UBI protection queue.
1024  * @ubi: UBI device description object
1025  * @i: a integer used as counter
1026  * @e: a pointer to a ubi_wl_entry to use as cursor
1027  */
1028 #define ubi_for_each_protected_peb(ubi, i, e)   \
1029     for ((i) = 0; (i) < UBI_PROT_QUEUE_LEN; (i)++)  \
1030         list_for_each_entry((e), &(ubi->pq[(i)]), u.list)
1031 
1032 /*
1033  * ubi_rb_for_each_entry - walk an RB-tree.
1034  * @rb: a pointer to type 'struct rb_node' to use as a loop counter
1035  * @pos: a pointer to RB-tree entry type to use as a loop counter
1036  * @root: RB-tree's root
1037  * @member: the name of the 'struct rb_node' within the RB-tree entry
1038  */
1039 #define ubi_rb_for_each_entry(rb, pos, root, member)                         \
1040     for (rb = rb_first(root),                                            \
1041          pos = (rb ? container_of(rb, typeof(*pos), member) : NULL);     \
1042          rb;                                                             \
1043          rb = rb_next(rb),                                               \
1044          pos = (rb ? container_of(rb, typeof(*pos), member) : NULL))
1045 
1046 /*
1047  * ubi_move_aeb_to_list - move a PEB from the volume tree to a list.
1048  *
1049  * @av: volume attaching information
1050  * @aeb: attaching eraseblock information
1051  * @list: the list to move to
1052  */
1053 static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
1054                      struct ubi_ainf_peb *aeb,
1055                      struct list_head *list)
1056 {
1057         rb_erase(&aeb->u.rb, &av->root);
1058         list_add_tail(&aeb->u.list, list);
1059 }
1060 
1061 /**
1062  * ubi_init_vid_buf - Initialize a VID buffer
1063  * @ubi: the UBI device
1064  * @vidb: the VID buffer to initialize
1065  * @buf: the underlying buffer
1066  */
1067 static inline void ubi_init_vid_buf(const struct ubi_device *ubi,
1068                     struct ubi_vid_io_buf *vidb,
1069                     void *buf)
1070 {
1071     if (buf)
1072         memset(buf, 0, ubi->vid_hdr_alsize);
1073 
1074     vidb->buffer = buf;
1075     vidb->hdr = buf + ubi->vid_hdr_shift;
1076 }
1077 
1078 /**
1079  * ubi_init_vid_buf - Allocate a VID buffer
1080  * @ubi: the UBI device
1081  * @gfp_flags: GFP flags to use for the allocation
1082  */
1083 static inline struct ubi_vid_io_buf *
1084 ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
1085 {
1086     struct ubi_vid_io_buf *vidb;
1087     void *buf;
1088 
1089     vidb = kzalloc(sizeof(*vidb), gfp_flags);
1090     if (!vidb)
1091         return NULL;
1092 
1093     buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
1094     if (!buf) {
1095         kfree(vidb);
1096         return NULL;
1097     }
1098 
1099     ubi_init_vid_buf(ubi, vidb, buf);
1100 
1101     return vidb;
1102 }
1103 
1104 /**
1105  * ubi_free_vid_buf - Free a VID buffer
1106  * @vidb: the VID buffer to free
1107  */
1108 static inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
1109 {
1110     if (!vidb)
1111         return;
1112 
1113     kfree(vidb->buffer);
1114     kfree(vidb);
1115 }
1116 
1117 /**
1118  * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
1119  * @vidb: VID buffer
1120  */
1121 static inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
1122 {
1123     return vidb->hdr;
1124 }
1125 
1126 /*
1127  * This function is equivalent to 'ubi_io_read()', but @offset is relative to
1128  * the beginning of the logical eraseblock, not to the beginning of the
1129  * physical eraseblock.
1130  */
1131 static inline int ubi_io_read_data(const struct ubi_device *ubi, void *buf,
1132                    int pnum, int offset, int len)
1133 {
1134     ubi_assert(offset >= 0);
1135     return ubi_io_read(ubi, buf, pnum, offset + ubi->leb_start, len);
1136 }
1137 
1138 /*
1139  * This function is equivalent to 'ubi_io_write()', but @offset is relative to
1140  * the beginning of the logical eraseblock, not to the beginning of the
1141  * physical eraseblock.
1142  */
1143 static inline int ubi_io_write_data(struct ubi_device *ubi, const void *buf,
1144                     int pnum, int offset, int len)
1145 {
1146     ubi_assert(offset >= 0);
1147     return ubi_io_write(ubi, buf, pnum, offset + ubi->leb_start, len);
1148 }
1149 
1150 /**
1151  * ubi_ro_mode - switch to read-only mode.
1152  * @ubi: UBI device description object
1153  */
1154 static inline void ubi_ro_mode(struct ubi_device *ubi)
1155 {
1156     if (!ubi->ro_mode) {
1157         ubi->ro_mode = 1;
1158         ubi_warn(ubi, "switch to read-only mode");
1159         dump_stack();
1160     }
1161 }
1162 
1163 /**
1164  * vol_id2idx - get table index by volume ID.
1165  * @ubi: UBI device description object
1166  * @vol_id: volume ID
1167  */
1168 static inline int vol_id2idx(const struct ubi_device *ubi, int vol_id)
1169 {
1170     if (vol_id >= UBI_INTERNAL_VOL_START)
1171         return vol_id - UBI_INTERNAL_VOL_START + ubi->vtbl_slots;
1172     else
1173         return vol_id;
1174 }
1175 
1176 /**
1177  * idx2vol_id - get volume ID by table index.
1178  * @ubi: UBI device description object
1179  * @idx: table index
1180  */
1181 static inline int idx2vol_id(const struct ubi_device *ubi, int idx)
1182 {
1183     if (idx >= ubi->vtbl_slots)
1184         return idx - ubi->vtbl_slots + UBI_INTERNAL_VOL_START;
1185     else
1186         return idx;
1187 }
1188 
1189 /**
1190  * ubi_is_fm_vol - check whether a volume ID is a Fastmap volume.
1191  * @vol_id: volume ID
1192  */
1193 static inline bool ubi_is_fm_vol(int vol_id)
1194 {
1195     switch (vol_id) {
1196         case UBI_FM_SB_VOLUME_ID:
1197         case UBI_FM_DATA_VOLUME_ID:
1198         return true;
1199     }
1200 
1201     return false;
1202 }
1203 
1204 /**
1205  * ubi_find_fm_block - check whether a PEB is part of the current Fastmap.
1206  * @ubi: UBI device description object
1207  * @pnum: physical eraseblock to look for
1208  *
1209  * This function returns a wear leveling object if @pnum relates to the current
1210  * fastmap, @NULL otherwise.
1211  */
1212 static inline struct ubi_wl_entry *ubi_find_fm_block(const struct ubi_device *ubi,
1213                              int pnum)
1214 {
1215     int i;
1216 
1217     if (ubi->fm) {
1218         for (i = 0; i < ubi->fm->used_blocks; i++) {
1219             if (ubi->fm->e[i]->pnum == pnum)
1220                 return ubi->fm->e[i];
1221         }
1222     }
1223 
1224     return NULL;
1225 }
1226 
1227 #endif /* !__UBI_UBI_H__ */