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