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