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