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