Thomas Gleixner | 1a59d1b8 | 2019-05-27 08:55:05 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 5 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 6 | */ |
| 7 | |
| 8 | /* |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 9 | * UBI attaching sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 10 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 11 | * This sub-system is responsible for attaching MTD devices and it also |
| 12 | * implements flash media scanning. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 13 | * |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 14 | * The attaching information is represented by a &struct ubi_attach_info' |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 15 | * object. Information about volumes is represented by &struct ubi_ainf_volume |
| 16 | * objects which are kept in volume RB-tree with root at the @volumes field. |
| 17 | * The RB-tree is indexed by the volume ID. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 18 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 19 | * Logical eraseblocks are represented by &struct ubi_ainf_peb objects. These |
| 20 | * objects are kept in per-volume RB-trees with the root at the corresponding |
| 21 | * &struct ubi_ainf_volume object. To put it differently, we keep an RB-tree of |
| 22 | * per-volume objects and each of these objects is the root of RB-tree of |
| 23 | * per-LEB objects. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 24 | * |
| 25 | * Corrupted physical eraseblocks are put to the @corr list, free physical |
| 26 | * eraseblocks are put to the @free list and the physical eraseblock to be |
| 27 | * erased are put to the @erase list. |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 28 | * |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 29 | * About corruptions |
| 30 | * ~~~~~~~~~~~~~~~~~ |
| 31 | * |
| 32 | * UBI protects EC and VID headers with CRC-32 checksums, so it can detect |
| 33 | * whether the headers are corrupted or not. Sometimes UBI also protects the |
| 34 | * data with CRC-32, e.g., when it executes the atomic LEB change operation, or |
| 35 | * when it moves the contents of a PEB for wear-leveling purposes. |
| 36 | * |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 37 | * UBI tries to distinguish between 2 types of corruptions. |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 38 | * |
| 39 | * 1. Corruptions caused by power cuts. These are expected corruptions and UBI |
| 40 | * tries to handle them gracefully, without printing too many warnings and |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 41 | * error messages. The idea is that we do not lose important data in these |
| 42 | * cases - we may lose only the data which were being written to the media just |
| 43 | * before the power cut happened, and the upper layers (e.g., UBIFS) are |
| 44 | * supposed to handle such data losses (e.g., by using the FS journal). |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 45 | * |
| 46 | * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like |
| 47 | * the reason is a power cut, UBI puts this PEB to the @erase list, and all |
| 48 | * PEBs in the @erase list are scheduled for erasure later. |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 49 | * |
| 50 | * 2. Unexpected corruptions which are not caused by power cuts. During |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 51 | * attaching, such PEBs are put to the @corr list and UBI preserves them. |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 52 | * Obviously, this lessens the amount of available PEBs, and if at some point |
| 53 | * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs |
| 54 | * about such PEBs every time the MTD device is attached. |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 55 | * |
| 56 | * However, it is difficult to reliably distinguish between these types of |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 57 | * corruptions and UBI's strategy is as follows (in case of attaching by |
| 58 | * scanning). UBI assumes corruption type 2 if the VID header is corrupted and |
| 59 | * the data area does not contain all 0xFFs, and there were no bit-flips or |
| 60 | * integrity errors (e.g., ECC errors in case of NAND) while reading the data |
| 61 | * area. Otherwise UBI assumes corruption type 1. So the decision criteria |
| 62 | * are as follows. |
| 63 | * o If the data area contains only 0xFFs, there are no data, and it is safe |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 64 | * to just erase this PEB - this is corruption type 1. |
| 65 | * o If the data area has bit-flips or data integrity errors (ECC errors on |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 66 | * NAND), it is probably a PEB which was being erased when power cut |
Artem Bityutskiy | fef2deb | 2010-10-29 08:34:50 +0300 | [diff] [blame] | 67 | * happened, so this is corruption type 1. However, this is just a guess, |
| 68 | * which might be wrong. |
Brian Norris | 55393ba | 2012-08-31 14:43:41 -0700 | [diff] [blame] | 69 | * o Otherwise this is corruption type 2. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 70 | */ |
| 71 | |
| 72 | #include <linux/err.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 73 | #include <linux/slab.h> |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 74 | #include <linux/crc32.h> |
Artem Bityutskiy | 3013ee3 | 2009-01-16 19:08:43 +0200 | [diff] [blame] | 75 | #include <linux/math64.h> |
Matthieu CASTET | 095751a | 2010-06-03 16:14:27 +0200 | [diff] [blame] | 76 | #include <linux/random.h> |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 77 | #include "ubi.h" |
| 78 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 79 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 80 | |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 81 | #define AV_FIND BIT(0) |
| 82 | #define AV_ADD BIT(1) |
| 83 | #define AV_FIND_OR_ADD (AV_FIND | AV_ADD) |
| 84 | |
| 85 | /** |
| 86 | * find_or_add_av - internal function to find a volume, add a volume or do |
| 87 | * both (find and add if missing). |
| 88 | * @ai: attaching information |
| 89 | * @vol_id: the requested volume ID |
| 90 | * @flags: a combination of the %AV_FIND and %AV_ADD flags describing the |
| 91 | * expected operation. If only %AV_ADD is set, -EEXIST is returned |
| 92 | * if the volume already exists. If only %AV_FIND is set, NULL is |
| 93 | * returned if the volume does not exist. And if both flags are |
| 94 | * set, the helper first tries to find an existing volume, and if |
| 95 | * it does not exist it creates a new one. |
| 96 | * @created: in value used to inform the caller whether it"s a newly created |
| 97 | * volume or not. |
| 98 | * |
| 99 | * This function returns a pointer to a volume description or an ERR_PTR if |
| 100 | * the operation failed. It can also return NULL if only %AV_FIND is set and |
| 101 | * the volume does not exist. |
| 102 | */ |
| 103 | static struct ubi_ainf_volume *find_or_add_av(struct ubi_attach_info *ai, |
| 104 | int vol_id, unsigned int flags, |
| 105 | bool *created) |
| 106 | { |
| 107 | struct ubi_ainf_volume *av; |
| 108 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
| 109 | |
| 110 | /* Walk the volume RB-tree to look if this volume is already present */ |
| 111 | while (*p) { |
| 112 | parent = *p; |
| 113 | av = rb_entry(parent, struct ubi_ainf_volume, rb); |
| 114 | |
| 115 | if (vol_id == av->vol_id) { |
| 116 | *created = false; |
| 117 | |
| 118 | if (!(flags & AV_FIND)) |
| 119 | return ERR_PTR(-EEXIST); |
| 120 | |
| 121 | return av; |
| 122 | } |
| 123 | |
| 124 | if (vol_id > av->vol_id) |
| 125 | p = &(*p)->rb_left; |
| 126 | else |
| 127 | p = &(*p)->rb_right; |
| 128 | } |
| 129 | |
| 130 | if (!(flags & AV_ADD)) |
| 131 | return NULL; |
| 132 | |
| 133 | /* The volume is absent - add it */ |
| 134 | av = kzalloc(sizeof(*av), GFP_KERNEL); |
| 135 | if (!av) |
| 136 | return ERR_PTR(-ENOMEM); |
| 137 | |
| 138 | av->vol_id = vol_id; |
| 139 | |
| 140 | if (vol_id > ai->highest_vol_id) |
| 141 | ai->highest_vol_id = vol_id; |
| 142 | |
| 143 | rb_link_node(&av->rb, parent, p); |
| 144 | rb_insert_color(&av->rb, &ai->volumes); |
| 145 | ai->vols_found += 1; |
| 146 | *created = true; |
| 147 | dbg_bld("added volume %d", vol_id); |
| 148 | return av; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * ubi_find_or_add_av - search for a volume in the attaching information and |
| 153 | * add one if it does not exist. |
| 154 | * @ai: attaching information |
| 155 | * @vol_id: the requested volume ID |
| 156 | * @created: whether the volume has been created or not |
| 157 | * |
| 158 | * This function returns a pointer to the new volume description or an |
| 159 | * ERR_PTR if the operation failed. |
| 160 | */ |
| 161 | static struct ubi_ainf_volume *ubi_find_or_add_av(struct ubi_attach_info *ai, |
| 162 | int vol_id, bool *created) |
| 163 | { |
| 164 | return find_or_add_av(ai, vol_id, AV_FIND_OR_ADD, created); |
| 165 | } |
| 166 | |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 167 | /** |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 168 | * ubi_alloc_aeb - allocate an aeb element |
| 169 | * @ai: attaching information |
| 170 | * @pnum: physical eraseblock number |
| 171 | * @ec: erase counter of the physical eraseblock |
| 172 | * |
| 173 | * Allocate an aeb object and initialize the pnum and ec information. |
| 174 | * vol_id and lnum are set to UBI_UNKNOWN, and the other fields are |
| 175 | * initialized to zero. |
| 176 | * Note that the element is not added in any list or RB tree. |
| 177 | */ |
| 178 | struct ubi_ainf_peb *ubi_alloc_aeb(struct ubi_attach_info *ai, int pnum, |
| 179 | int ec) |
| 180 | { |
| 181 | struct ubi_ainf_peb *aeb; |
| 182 | |
| 183 | aeb = kmem_cache_zalloc(ai->aeb_slab_cache, GFP_KERNEL); |
| 184 | if (!aeb) |
| 185 | return NULL; |
| 186 | |
| 187 | aeb->pnum = pnum; |
| 188 | aeb->ec = ec; |
| 189 | aeb->vol_id = UBI_UNKNOWN; |
| 190 | aeb->lnum = UBI_UNKNOWN; |
| 191 | |
| 192 | return aeb; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * ubi_free_aeb - free an aeb element |
| 197 | * @ai: attaching information |
| 198 | * @aeb: the element to free |
| 199 | * |
| 200 | * Free an aeb object. The caller must have removed the element from any list |
| 201 | * or RB tree. |
| 202 | */ |
| 203 | void ubi_free_aeb(struct ubi_attach_info *ai, struct ubi_ainf_peb *aeb) |
| 204 | { |
| 205 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
| 206 | } |
| 207 | |
| 208 | /** |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 209 | * add_to_list - add physical eraseblock to a list. |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 210 | * @ai: attaching information |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 211 | * @pnum: physical eraseblock number to add |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 212 | * @vol_id: the last used volume id for the PEB |
| 213 | * @lnum: the last used LEB number for the PEB |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 214 | * @ec: erase counter of the physical eraseblock |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 215 | * @to_head: if not zero, add to the head of the list |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 216 | * @list: the list to add to |
| 217 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 218 | * This function allocates a 'struct ubi_ainf_peb' object for physical |
| 219 | * eraseblock @pnum and adds it to the "free", "erase", or "alien" lists. |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 220 | * It stores the @lnum and @vol_id alongside, which can both be |
| 221 | * %UBI_UNKNOWN if they are not available, not readable, or not assigned. |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 222 | * If @to_head is not zero, PEB will be added to the head of the list, which |
| 223 | * basically means it will be processed first later. E.g., we add corrupted |
| 224 | * PEBs (corrupted due to power cuts) to the head of the erase list to make |
| 225 | * sure we erase them first and get rid of corruptions ASAP. This function |
| 226 | * returns zero in case of success and a negative error code in case of |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 227 | * failure. |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 228 | */ |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 229 | static int add_to_list(struct ubi_attach_info *ai, int pnum, int vol_id, |
| 230 | int lnum, int ec, int to_head, struct list_head *list) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 231 | { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 232 | struct ubi_ainf_peb *aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 233 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 234 | if (list == &ai->free) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 235 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 236 | } else if (list == &ai->erase) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 237 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 238 | } else if (list == &ai->alien) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 239 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 240 | ai->alien_peb_count += 1; |
Artem Bityutskiy | 33789fb | 2010-04-30 12:31:26 +0300 | [diff] [blame] | 241 | } else |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 242 | BUG(); |
| 243 | |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 244 | aeb = ubi_alloc_aeb(ai, pnum, ec); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 245 | if (!aeb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 246 | return -ENOMEM; |
| 247 | |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 248 | aeb->vol_id = vol_id; |
| 249 | aeb->lnum = lnum; |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 250 | if (to_head) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 251 | list_add(&aeb->u.list, list); |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 252 | else |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 253 | list_add_tail(&aeb->u.list, list); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | /** |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 258 | * add_corrupted - add a corrupted physical eraseblock. |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 259 | * @ai: attaching information |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 260 | * @pnum: physical eraseblock number to add |
| 261 | * @ec: erase counter of the physical eraseblock |
| 262 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 263 | * This function allocates a 'struct ubi_ainf_peb' object for a corrupted |
| 264 | * physical eraseblock @pnum and adds it to the 'corr' list. The corruption |
| 265 | * was presumably not caused by a power cut. Returns zero in case of success |
| 266 | * and a negative error code in case of failure. |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 267 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 268 | static int add_corrupted(struct ubi_attach_info *ai, int pnum, int ec) |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 269 | { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 270 | struct ubi_ainf_peb *aeb; |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 271 | |
| 272 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); |
| 273 | |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 274 | aeb = ubi_alloc_aeb(ai, pnum, ec); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 275 | if (!aeb) |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 276 | return -ENOMEM; |
| 277 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 278 | ai->corr_peb_count += 1; |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 279 | list_add(&aeb->u.list, &ai->corr); |
Artem Bityutskiy | 3fb3412 | 2010-09-03 15:36:12 +0300 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | /** |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 284 | * add_fastmap - add a Fastmap related physical eraseblock. |
| 285 | * @ai: attaching information |
| 286 | * @pnum: physical eraseblock number the VID header came from |
| 287 | * @vid_hdr: the volume identifier header |
| 288 | * @ec: erase counter of the physical eraseblock |
| 289 | * |
| 290 | * This function allocates a 'struct ubi_ainf_peb' object for a Fastamp |
| 291 | * physical eraseblock @pnum and adds it to the 'fastmap' list. |
| 292 | * Such blocks can be Fastmap super and data blocks from both the most |
| 293 | * recent Fastmap we're attaching from or from old Fastmaps which will |
| 294 | * be erased. |
| 295 | */ |
| 296 | static int add_fastmap(struct ubi_attach_info *ai, int pnum, |
| 297 | struct ubi_vid_hdr *vid_hdr, int ec) |
| 298 | { |
| 299 | struct ubi_ainf_peb *aeb; |
| 300 | |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 301 | aeb = ubi_alloc_aeb(ai, pnum, ec); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 302 | if (!aeb) |
| 303 | return -ENOMEM; |
| 304 | |
Boris Brezillon | 3f84e45 | 2016-09-16 16:59:10 +0200 | [diff] [blame] | 305 | aeb->vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 306 | aeb->sqnum = be64_to_cpu(vid_hdr->sqnum); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 307 | list_add(&aeb->u.list, &ai->fastmap); |
| 308 | |
| 309 | dbg_bld("add to fastmap list: PEB %d, vol_id %d, sqnum: %llu", pnum, |
| 310 | aeb->vol_id, aeb->sqnum); |
| 311 | |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | /** |
Artem Bityutskiy | ebaaf1a | 2008-07-18 13:34:32 +0300 | [diff] [blame] | 316 | * validate_vid_hdr - check volume identifier header. |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 317 | * @ubi: UBI device description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 318 | * @vid_hdr: the volume identifier header to check |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 319 | * @av: information about the volume this logical eraseblock belongs to |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 320 | * @pnum: physical eraseblock number the VID header came from |
| 321 | * |
| 322 | * This function checks that data stored in @vid_hdr is consistent. Returns |
| 323 | * non-zero if an inconsistency was found and zero if not. |
| 324 | * |
| 325 | * Note, UBI does sanity check of everything it reads from the flash media. |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 326 | * Most of the checks are done in the I/O sub-system. Here we check that the |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 327 | * information in the VID header is consistent to the information in other VID |
| 328 | * headers of the same volume. |
| 329 | */ |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 330 | static int validate_vid_hdr(const struct ubi_device *ubi, |
| 331 | const struct ubi_vid_hdr *vid_hdr, |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 332 | const struct ubi_ainf_volume *av, int pnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 333 | { |
| 334 | int vol_type = vid_hdr->vol_type; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 335 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 336 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 337 | int data_pad = be32_to_cpu(vid_hdr->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 338 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 339 | if (av->leb_count != 0) { |
| 340 | int av_vol_type; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 341 | |
| 342 | /* |
| 343 | * This is not the first logical eraseblock belonging to this |
| 344 | * volume. Ensure that the data in its VID header is consistent |
| 345 | * to the data in previous logical eraseblock headers. |
| 346 | */ |
| 347 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 348 | if (vol_id != av->vol_id) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 349 | ubi_err(ubi, "inconsistent vol_id"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 350 | goto bad; |
| 351 | } |
| 352 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 353 | if (av->vol_type == UBI_STATIC_VOLUME) |
| 354 | av_vol_type = UBI_VID_STATIC; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 355 | else |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 356 | av_vol_type = UBI_VID_DYNAMIC; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 357 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 358 | if (vol_type != av_vol_type) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 359 | ubi_err(ubi, "inconsistent vol_type"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 360 | goto bad; |
| 361 | } |
| 362 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 363 | if (used_ebs != av->used_ebs) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 364 | ubi_err(ubi, "inconsistent used_ebs"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 365 | goto bad; |
| 366 | } |
| 367 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 368 | if (data_pad != av->data_pad) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 369 | ubi_err(ubi, "inconsistent data_pad"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 370 | goto bad; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | |
| 376 | bad: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 377 | ubi_err(ubi, "inconsistent VID header at PEB %d", pnum); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 378 | ubi_dump_vid_hdr(vid_hdr); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 379 | ubi_dump_av(av); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 380 | return -EINVAL; |
| 381 | } |
| 382 | |
| 383 | /** |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 384 | * add_volume - add volume to the attaching information. |
| 385 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 386 | * @vol_id: ID of the volume to add |
| 387 | * @pnum: physical eraseblock number |
| 388 | * @vid_hdr: volume identifier header |
| 389 | * |
| 390 | * If the volume corresponding to the @vid_hdr logical eraseblock is already |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 391 | * present in the attaching information, this function does nothing. Otherwise |
| 392 | * it adds corresponding volume to the attaching information. Returns a pointer |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 393 | * to the allocated "av" object in case of success and a negative error code in |
| 394 | * case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 395 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 396 | static struct ubi_ainf_volume *add_volume(struct ubi_attach_info *ai, |
Artem Bityutskiy | afc15a8 | 2012-05-17 07:46:17 +0300 | [diff] [blame] | 397 | int vol_id, int pnum, |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 398 | const struct ubi_vid_hdr *vid_hdr) |
| 399 | { |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 400 | struct ubi_ainf_volume *av; |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 401 | bool created; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 402 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 403 | ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 404 | |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 405 | av = ubi_find_or_add_av(ai, vol_id, &created); |
| 406 | if (IS_ERR(av) || !created) |
| 407 | return av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 408 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 409 | av->used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 410 | av->data_pad = be32_to_cpu(vid_hdr->data_pad); |
| 411 | av->compat = vid_hdr->compat; |
| 412 | av->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 413 | : UBI_STATIC_VOLUME; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 414 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 415 | return av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /** |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 419 | * ubi_compare_lebs - find out which logical eraseblock is newer. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 420 | * @ubi: UBI device description object |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 421 | * @aeb: first logical eraseblock to compare |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 422 | * @pnum: physical eraseblock number of the second logical eraseblock to |
| 423 | * compare |
| 424 | * @vid_hdr: volume identifier header of the second logical eraseblock |
| 425 | * |
| 426 | * This function compares 2 copies of a LEB and informs which one is newer. In |
| 427 | * case of success this function returns a positive value, in case of failure, a |
| 428 | * negative error code is returned. The success return codes use the following |
| 429 | * bits: |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 430 | * o bit 0 is cleared: the first PEB (described by @aeb) is newer than the |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 431 | * second PEB (described by @pnum and @vid_hdr); |
| 432 | * o bit 0 is set: the second PEB is newer; |
| 433 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; |
| 434 | * o bit 1 is set: bit-flips were detected in the newer LEB; |
| 435 | * o bit 2 is cleared: the older LEB is not corrupted; |
| 436 | * o bit 2 is set: the older LEB is corrupted. |
| 437 | */ |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 438 | int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, |
Artem Bityutskiy | e88d6e10 | 2007-08-29 14:51:52 +0300 | [diff] [blame] | 439 | int pnum, const struct ubi_vid_hdr *vid_hdr) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 440 | { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 441 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; |
| 442 | uint32_t data_crc, crc; |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 443 | struct ubi_vid_io_buf *vidb = NULL; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 444 | unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 445 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 446 | if (sqnum2 == aeb->sqnum) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 447 | /* |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 448 | * This must be a really ancient UBI image which has been |
| 449 | * created before sequence numbers support has been added. At |
| 450 | * that times we used 32-bit LEB versions stored in logical |
| 451 | * eraseblocks. That was before UBI got into mainline. We do not |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 452 | * support these images anymore. Well, those images still work, |
| 453 | * but only if no unclean reboots happened. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 454 | */ |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 455 | ubi_err(ubi, "unsupported on-flash UBI format"); |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 456 | return -EINVAL; |
| 457 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 458 | |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 459 | /* Obviously the LEB with lower sequence counter is older */ |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 460 | second_is_newer = (sqnum2 > aeb->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * Now we know which copy is newer. If the copy flag of the PEB with |
| 464 | * newer version is not set, then we just return, otherwise we have to |
| 465 | * check data CRC. For the second PEB we already have the VID header, |
| 466 | * for the first one - we'll need to re-read it from flash. |
| 467 | * |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 468 | * Note: this may be optimized so that we wouldn't read twice. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 469 | */ |
| 470 | |
| 471 | if (second_is_newer) { |
| 472 | if (!vid_hdr->copy_flag) { |
| 473 | /* It is not a copy, so it is newer */ |
| 474 | dbg_bld("second PEB %d is newer, copy_flag is unset", |
| 475 | pnum); |
| 476 | return 1; |
| 477 | } |
| 478 | } else { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 479 | if (!aeb->copy_flag) { |
Artem Bityutskiy | fb22b59 | 2010-10-19 22:00:11 +0300 | [diff] [blame] | 480 | /* It is not a copy, so it is newer */ |
| 481 | dbg_bld("first PEB %d is newer, copy_flag is unset", |
| 482 | pnum); |
| 483 | return bitflips << 1; |
| 484 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 485 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 486 | vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); |
| 487 | if (!vidb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 488 | return -ENOMEM; |
| 489 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 490 | pnum = aeb->pnum; |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 491 | err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 492 | if (err) { |
| 493 | if (err == UBI_IO_BITFLIPS) |
| 494 | bitflips = 1; |
| 495 | else { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 496 | ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d", |
Artem Bityutskiy | 049333c | 2012-08-27 14:43:54 +0300 | [diff] [blame] | 497 | pnum, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 498 | if (err > 0) |
| 499 | err = -EIO; |
| 500 | |
| 501 | goto out_free_vidh; |
| 502 | } |
| 503 | } |
| 504 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 505 | vid_hdr = ubi_get_vid_hdr(vidb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* Read the data of the copy and check the CRC */ |
| 509 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 510 | len = be32_to_cpu(vid_hdr->data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 511 | |
Artem Bityutskiy | d125a75 | 2012-10-26 16:11:26 +0300 | [diff] [blame] | 512 | mutex_lock(&ubi->buf_mutex); |
| 513 | err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 514 | if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) |
Artem Bityutskiy | d125a75 | 2012-10-26 16:11:26 +0300 | [diff] [blame] | 515 | goto out_unlock; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 516 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 517 | data_crc = be32_to_cpu(vid_hdr->data_crc); |
Artem Bityutskiy | d125a75 | 2012-10-26 16:11:26 +0300 | [diff] [blame] | 518 | crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 519 | if (crc != data_crc) { |
| 520 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", |
| 521 | pnum, crc, data_crc); |
| 522 | corrupted = 1; |
| 523 | bitflips = 0; |
| 524 | second_is_newer = !second_is_newer; |
| 525 | } else { |
| 526 | dbg_bld("PEB %d CRC is OK", pnum); |
Brian Norris | 8eef7d7 | 2015-02-28 02:23:25 -0800 | [diff] [blame] | 527 | bitflips |= !!err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 528 | } |
Artem Bityutskiy | d125a75 | 2012-10-26 16:11:26 +0300 | [diff] [blame] | 529 | mutex_unlock(&ubi->buf_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 530 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 531 | ubi_free_vid_buf(vidb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 532 | |
| 533 | if (second_is_newer) |
| 534 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); |
| 535 | else |
| 536 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); |
| 537 | |
| 538 | return second_is_newer | (bitflips << 1) | (corrupted << 2); |
| 539 | |
Artem Bityutskiy | d125a75 | 2012-10-26 16:11:26 +0300 | [diff] [blame] | 540 | out_unlock: |
| 541 | mutex_unlock(&ubi->buf_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 542 | out_free_vidh: |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 543 | ubi_free_vid_buf(vidb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 544 | return err; |
| 545 | } |
| 546 | |
| 547 | /** |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 548 | * ubi_add_to_av - add used physical eraseblock to the attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 549 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 550 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 551 | * @pnum: the physical eraseblock number |
| 552 | * @ec: erase counter |
| 553 | * @vid_hdr: the volume identifier header |
| 554 | * @bitflips: if bit-flips were detected when this physical eraseblock was read |
| 555 | * |
Artem Bityutskiy | 79b510c | 2007-05-05 17:36:17 +0300 | [diff] [blame] | 556 | * This function adds information about a used physical eraseblock to the |
| 557 | * 'used' tree of the corresponding volume. The function is rather complex |
| 558 | * because it has to handle cases when this is not the first physical |
| 559 | * eraseblock belonging to the same logical eraseblock, and the newer one has |
| 560 | * to be picked, while the older one has to be dropped. This function returns |
| 561 | * zero in case of success and a negative error code in case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 562 | */ |
Artem Bityutskiy | 3561188 | 2012-05-17 15:31:31 +0300 | [diff] [blame] | 563 | int ubi_add_to_av(struct ubi_device *ubi, struct ubi_attach_info *ai, int pnum, |
| 564 | int ec, const struct ubi_vid_hdr *vid_hdr, int bitflips) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 565 | { |
| 566 | int err, vol_id, lnum; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 567 | unsigned long long sqnum; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 568 | struct ubi_ainf_volume *av; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 569 | struct ubi_ainf_peb *aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 570 | struct rb_node **p, *parent = NULL; |
| 571 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 572 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 573 | lnum = be32_to_cpu(vid_hdr->lnum); |
| 574 | sqnum = be64_to_cpu(vid_hdr->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 575 | |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 576 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d", |
| 577 | pnum, vol_id, lnum, ec, sqnum, bitflips); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 578 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 579 | av = add_volume(ai, vol_id, pnum, vid_hdr); |
| 580 | if (IS_ERR(av)) |
| 581 | return PTR_ERR(av); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 582 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 583 | if (ai->max_sqnum < sqnum) |
| 584 | ai->max_sqnum = sqnum; |
Brijesh Singh | 76eafe4 | 2007-07-06 14:35:43 +0300 | [diff] [blame] | 585 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 586 | /* |
| 587 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look |
| 588 | * if this is the first instance of this logical eraseblock or not. |
| 589 | */ |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 590 | p = &av->root.rb_node; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 591 | while (*p) { |
| 592 | int cmp_res; |
| 593 | |
| 594 | parent = *p; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 595 | aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); |
| 596 | if (lnum != aeb->lnum) { |
| 597 | if (lnum < aeb->lnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 598 | p = &(*p)->rb_left; |
| 599 | else |
| 600 | p = &(*p)->rb_right; |
| 601 | continue; |
| 602 | } |
| 603 | |
| 604 | /* |
| 605 | * There is already a physical eraseblock describing the same |
| 606 | * logical eraseblock present. |
| 607 | */ |
| 608 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 609 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, EC %d", |
| 610 | aeb->pnum, aeb->sqnum, aeb->ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 611 | |
| 612 | /* |
| 613 | * Make sure that the logical eraseblocks have different |
| 614 | * sequence numbers. Otherwise the image is bad. |
| 615 | * |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 616 | * However, if the sequence number is zero, we assume it must |
| 617 | * be an ancient UBI image from the era when UBI did not have |
| 618 | * sequence numbers. We still can attach these images, unless |
| 619 | * there is a need to distinguish between old and new |
| 620 | * eraseblocks, in which case we'll refuse the image in |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 621 | * 'ubi_compare_lebs()'. In other words, we attach old clean |
Artem Bityutskiy | 9869cd8 | 2008-07-18 13:53:39 +0300 | [diff] [blame] | 622 | * images, but refuse attaching old images with duplicated |
| 623 | * logical eraseblocks because there was an unclean reboot. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 624 | */ |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 625 | if (aeb->sqnum == sqnum && sqnum != 0) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 626 | ubi_err(ubi, "two LEBs with same sequence number %llu", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 627 | sqnum); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 628 | ubi_dump_aeb(aeb, 0); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 629 | ubi_dump_vid_hdr(vid_hdr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 630 | return -EINVAL; |
| 631 | } |
| 632 | |
| 633 | /* |
| 634 | * Now we have to drop the older one and preserve the newer |
| 635 | * one. |
| 636 | */ |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 637 | cmp_res = ubi_compare_lebs(ubi, aeb, pnum, vid_hdr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 638 | if (cmp_res < 0) |
| 639 | return cmp_res; |
| 640 | |
| 641 | if (cmp_res & 1) { |
| 642 | /* |
Shinya Kuribayashi | 3f50262 | 2010-05-06 19:21:47 +0900 | [diff] [blame] | 643 | * This logical eraseblock is newer than the one |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 644 | * found earlier. |
| 645 | */ |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 646 | err = validate_vid_hdr(ubi, vid_hdr, av, pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 647 | if (err) |
| 648 | return err; |
| 649 | |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 650 | err = add_to_list(ai, aeb->pnum, aeb->vol_id, |
| 651 | aeb->lnum, aeb->ec, cmp_res & 4, |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 652 | &ai->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 653 | if (err) |
| 654 | return err; |
| 655 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 656 | aeb->ec = ec; |
| 657 | aeb->pnum = pnum; |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 658 | aeb->vol_id = vol_id; |
| 659 | aeb->lnum = lnum; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 660 | aeb->scrub = ((cmp_res & 2) || bitflips); |
| 661 | aeb->copy_flag = vid_hdr->copy_flag; |
| 662 | aeb->sqnum = sqnum; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 663 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 664 | if (av->highest_lnum == lnum) |
| 665 | av->last_data_size = |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 666 | be32_to_cpu(vid_hdr->data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 667 | |
| 668 | return 0; |
| 669 | } else { |
| 670 | /* |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 671 | * This logical eraseblock is older than the one found |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 672 | * previously. |
| 673 | */ |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 674 | return add_to_list(ai, pnum, vol_id, lnum, ec, |
| 675 | cmp_res & 4, &ai->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * We've met this logical eraseblock for the first time, add it to the |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 681 | * attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 682 | */ |
| 683 | |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 684 | err = validate_vid_hdr(ubi, vid_hdr, av, pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 685 | if (err) |
| 686 | return err; |
| 687 | |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 688 | aeb = ubi_alloc_aeb(ai, pnum, ec); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 689 | if (!aeb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 690 | return -ENOMEM; |
| 691 | |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 692 | aeb->vol_id = vol_id; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 693 | aeb->lnum = lnum; |
| 694 | aeb->scrub = bitflips; |
| 695 | aeb->copy_flag = vid_hdr->copy_flag; |
| 696 | aeb->sqnum = sqnum; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 697 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 698 | if (av->highest_lnum <= lnum) { |
| 699 | av->highest_lnum = lnum; |
| 700 | av->last_data_size = be32_to_cpu(vid_hdr->data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 701 | } |
| 702 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 703 | av->leb_count += 1; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 704 | rb_link_node(&aeb->u.rb, parent, p); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 705 | rb_insert_color(&aeb->u.rb, &av->root); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | /** |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 710 | * ubi_add_av - add volume to the attaching information. |
| 711 | * @ai: attaching information |
| 712 | * @vol_id: the requested volume ID |
| 713 | * |
| 714 | * This function returns a pointer to the new volume description or an |
| 715 | * ERR_PTR if the operation failed. |
| 716 | */ |
| 717 | struct ubi_ainf_volume *ubi_add_av(struct ubi_attach_info *ai, int vol_id) |
| 718 | { |
| 719 | bool created; |
| 720 | |
| 721 | return find_or_add_av(ai, vol_id, AV_ADD, &created); |
| 722 | } |
| 723 | |
| 724 | /** |
Artem Bityutskiy | dcd85fd | 2012-05-17 15:33:20 +0300 | [diff] [blame] | 725 | * ubi_find_av - find volume in the attaching information. |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 726 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 727 | * @vol_id: the requested volume ID |
| 728 | * |
| 729 | * This function returns a pointer to the volume description or %NULL if there |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 730 | * are no data about this volume in the attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 731 | */ |
Artem Bityutskiy | dcd85fd | 2012-05-17 15:33:20 +0300 | [diff] [blame] | 732 | struct ubi_ainf_volume *ubi_find_av(const struct ubi_attach_info *ai, |
| 733 | int vol_id) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 734 | { |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 735 | bool created; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 736 | |
Boris Brezillon | de4c455 | 2016-09-16 16:59:14 +0200 | [diff] [blame] | 737 | return find_or_add_av((struct ubi_attach_info *)ai, vol_id, AV_FIND, |
| 738 | &created); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 739 | } |
| 740 | |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 741 | static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av, |
| 742 | struct list_head *list); |
| 743 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 744 | /** |
Artem Bityutskiy | d717dc2 | 2012-05-17 15:36:39 +0300 | [diff] [blame] | 745 | * ubi_remove_av - delete attaching information about a volume. |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 746 | * @ai: attaching information |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 747 | * @av: the volume attaching information to delete |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 748 | */ |
Artem Bityutskiy | d717dc2 | 2012-05-17 15:36:39 +0300 | [diff] [blame] | 749 | void ubi_remove_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 750 | { |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 751 | dbg_bld("remove attaching information about volume %d", av->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 752 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 753 | rb_erase(&av->rb, &ai->volumes); |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 754 | destroy_av(ai, av, &ai->erase); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 755 | ai->vols_found -= 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /** |
Artem Bityutskiy | 13d33da | 2012-05-17 15:20:28 +0300 | [diff] [blame] | 759 | * early_erase_peb - erase a physical eraseblock. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 760 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 761 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 762 | * @pnum: physical eraseblock number to erase; |
Artem Bityutskiy | 9c47fb2 | 2012-05-18 12:54:58 +0300 | [diff] [blame] | 763 | * @ec: erase counter value to write (%UBI_UNKNOWN if it is unknown) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 764 | * |
| 765 | * This function erases physical eraseblock 'pnum', and writes the erase |
| 766 | * counter header to it. This function should only be used on UBI device |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 767 | * initialization stages, when the EBA sub-system had not been yet initialized. |
| 768 | * This function returns zero in case of success and a negative error code in |
| 769 | * case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 770 | */ |
Artem Bityutskiy | 13d33da | 2012-05-17 15:20:28 +0300 | [diff] [blame] | 771 | static int early_erase_peb(struct ubi_device *ubi, |
| 772 | const struct ubi_attach_info *ai, int pnum, int ec) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 773 | { |
| 774 | int err; |
| 775 | struct ubi_ec_hdr *ec_hdr; |
| 776 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 777 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { |
| 778 | /* |
| 779 | * Erase counter overflow. Upgrade UBI and use 64-bit |
| 780 | * erase counters internally. |
| 781 | */ |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 782 | ubi_err(ubi, "erase counter overflow at PEB %d, EC %d", |
| 783 | pnum, ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 784 | return -EINVAL; |
| 785 | } |
| 786 | |
Florin Malita | dcec4c3 | 2007-07-19 15:22:41 -0400 | [diff] [blame] | 787 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 788 | if (!ec_hdr) |
| 789 | return -ENOMEM; |
| 790 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 791 | ec_hdr->ec = cpu_to_be64(ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 792 | |
| 793 | err = ubi_io_sync_erase(ubi, pnum, 0); |
| 794 | if (err < 0) |
| 795 | goto out_free; |
| 796 | |
| 797 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); |
| 798 | |
| 799 | out_free: |
| 800 | kfree(ec_hdr); |
| 801 | return err; |
| 802 | } |
| 803 | |
| 804 | /** |
Artem Bityutskiy | c87fbd7 | 2012-05-17 15:38:56 +0300 | [diff] [blame] | 805 | * ubi_early_get_peb - get a free physical eraseblock. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 806 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 807 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 808 | * |
| 809 | * This function returns a free physical eraseblock. It is supposed to be |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 810 | * called on the UBI initialization stages when the wear-leveling sub-system is |
| 811 | * not initialized yet. This function picks a physical eraseblocks from one of |
| 812 | * the lists, writes the EC header if it is needed, and removes it from the |
| 813 | * list. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 814 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 815 | * This function returns a pointer to the "aeb" of the found free PEB in case |
| 816 | * of success and an error code in case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 817 | */ |
Artem Bityutskiy | c87fbd7 | 2012-05-17 15:38:56 +0300 | [diff] [blame] | 818 | struct ubi_ainf_peb *ubi_early_get_peb(struct ubi_device *ubi, |
| 819 | struct ubi_attach_info *ai) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 820 | { |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 821 | int err = 0; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 822 | struct ubi_ainf_peb *aeb, *tmp_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 823 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 824 | if (!list_empty(&ai->free)) { |
| 825 | aeb = list_entry(ai->free.next, struct ubi_ainf_peb, u.list); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 826 | list_del(&aeb->u.list); |
| 827 | dbg_bld("return free PEB %d, EC %d", aeb->pnum, aeb->ec); |
| 828 | return aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 829 | } |
| 830 | |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 831 | /* |
| 832 | * We try to erase the first physical eraseblock from the erase list |
| 833 | * and pick it if we succeed, or try to erase the next one if not. And |
| 834 | * so forth. We don't want to take care about bad eraseblocks here - |
| 835 | * they'll be handled later. |
| 836 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 837 | list_for_each_entry_safe(aeb, tmp_aeb, &ai->erase, u.list) { |
Artem Bityutskiy | 9c47fb2 | 2012-05-18 12:54:58 +0300 | [diff] [blame] | 838 | if (aeb->ec == UBI_UNKNOWN) |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 839 | aeb->ec = ai->mean_ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 840 | |
Artem Bityutskiy | 13d33da | 2012-05-17 15:20:28 +0300 | [diff] [blame] | 841 | err = early_erase_peb(ubi, ai, aeb->pnum, aeb->ec+1); |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 842 | if (err) |
| 843 | continue; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 844 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 845 | aeb->ec += 1; |
| 846 | list_del(&aeb->u.list); |
| 847 | dbg_bld("return PEB %d, EC %d", aeb->pnum, aeb->ec); |
| 848 | return aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 849 | } |
| 850 | |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 851 | ubi_err(ubi, "no free eraseblocks"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 852 | return ERR_PTR(-ENOSPC); |
| 853 | } |
| 854 | |
| 855 | /** |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 856 | * check_corruption - check the data area of PEB. |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 857 | * @ubi: UBI device description object |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 858 | * @vid_hdr: the (corrupted) VID header of this PEB |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 859 | * @pnum: the physical eraseblock number to check |
| 860 | * |
| 861 | * This is a helper function which is used to distinguish between VID header |
| 862 | * corruptions caused by power cuts and other reasons. If the PEB contains only |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 863 | * 0xFF bytes in the data area, the VID header is most probably corrupted |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 864 | * because of a power cut (%0 is returned in this case). Otherwise, it was |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 865 | * probably corrupted for some other reasons (%1 is returned in this case). A |
| 866 | * negative error code is returned if a read error occurred. |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 867 | * |
| 868 | * If the corruption reason was a power cut, UBI can safely erase this PEB. |
| 869 | * Otherwise, it should preserve it to avoid possibly destroying important |
| 870 | * information. |
| 871 | */ |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 872 | static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr, |
| 873 | int pnum) |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 874 | { |
| 875 | int err; |
| 876 | |
| 877 | mutex_lock(&ubi->buf_mutex); |
Artem Bityutskiy | 0ca39d7 | 2012-03-08 15:29:37 +0200 | [diff] [blame] | 878 | memset(ubi->peb_buf, 0x00, ubi->leb_size); |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 879 | |
Artem Bityutskiy | 0ca39d7 | 2012-03-08 15:29:37 +0200 | [diff] [blame] | 880 | err = ubi_io_read(ubi, ubi->peb_buf, pnum, ubi->leb_start, |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 881 | ubi->leb_size); |
Brian Norris | d57f4054 | 2011-09-20 18:34:25 -0700 | [diff] [blame] | 882 | if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) { |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 883 | /* |
| 884 | * Bit-flips or integrity errors while reading the data area. |
| 885 | * It is difficult to say for sure what type of corruption is |
| 886 | * this, but presumably a power cut happened while this PEB was |
| 887 | * erased, so it became unstable and corrupted, and should be |
| 888 | * erased. |
| 889 | */ |
Dan Carpenter | 1b1d76e | 2010-11-18 06:58:04 +0300 | [diff] [blame] | 890 | err = 0; |
| 891 | goto out_unlock; |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | if (err) |
Dan Carpenter | 1b1d76e | 2010-11-18 06:58:04 +0300 | [diff] [blame] | 895 | goto out_unlock; |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 896 | |
Artem Bityutskiy | 0ca39d7 | 2012-03-08 15:29:37 +0200 | [diff] [blame] | 897 | if (ubi_check_pattern(ubi->peb_buf, 0xFF, ubi->leb_size)) |
Dan Carpenter | 1b1d76e | 2010-11-18 06:58:04 +0300 | [diff] [blame] | 898 | goto out_unlock; |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 899 | |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 900 | ubi_err(ubi, "PEB %d contains corrupted VID header, and the data does not contain all 0xFF", |
Artem Bityutskiy | 049333c | 2012-08-27 14:43:54 +0300 | [diff] [blame] | 901 | pnum); |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 902 | ubi_err(ubi, "this may be a non-UBI PEB or a severe VID header corruption which requires manual inspection"); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 903 | ubi_dump_vid_hdr(vid_hdr); |
Artem Bityutskiy | 719bb84 | 2012-08-27 17:14:58 +0300 | [diff] [blame] | 904 | pr_err("hexdump of PEB %d offset %d, length %d", |
| 905 | pnum, ubi->leb_start, ubi->leb_size); |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 906 | ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, |
Artem Bityutskiy | 0ca39d7 | 2012-03-08 15:29:37 +0200 | [diff] [blame] | 907 | ubi->peb_buf, ubi->leb_size, 1); |
Dan Carpenter | 1b1d76e | 2010-11-18 06:58:04 +0300 | [diff] [blame] | 908 | err = 1; |
| 909 | |
| 910 | out_unlock: |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 911 | mutex_unlock(&ubi->buf_mutex); |
Dan Carpenter | 1b1d76e | 2010-11-18 06:58:04 +0300 | [diff] [blame] | 912 | return err; |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 913 | } |
| 914 | |
Richard Weinberger | 243a4f8 | 2016-06-14 10:12:13 +0200 | [diff] [blame] | 915 | static bool vol_ignored(int vol_id) |
| 916 | { |
| 917 | switch (vol_id) { |
| 918 | case UBI_LAYOUT_VOLUME_ID: |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | #ifdef CONFIG_MTD_UBI_FASTMAP |
| 923 | return ubi_is_fm_vol(vol_id); |
| 924 | #else |
| 925 | return false; |
| 926 | #endif |
| 927 | } |
| 928 | |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 929 | /** |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 930 | * scan_peb - scan and process UBI headers of a PEB. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 931 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 932 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 933 | * @pnum: the physical eraseblock number |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 934 | * @fast: true if we're scanning for a Fastmap |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 935 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 936 | * This function reads UBI headers of PEB @pnum, checks them, and adds |
| 937 | * information about this PEB to the corresponding list or RB-tree in the |
| 938 | * "attaching info" structure. Returns zero if the physical eraseblock was |
| 939 | * successfully handled and a negative error code in case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 940 | */ |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 941 | static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, |
| 942 | int pnum, bool fast) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 943 | { |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 944 | struct ubi_ec_hdr *ech = ai->ech; |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 945 | struct ubi_vid_io_buf *vidb = ai->vidb; |
| 946 | struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 947 | long long ec; |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 948 | int err, bitflips = 0, vol_id = -1, ec_err = 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 949 | |
| 950 | dbg_bld("scan PEB %d", pnum); |
| 951 | |
| 952 | /* Skip bad physical eraseblocks */ |
| 953 | err = ubi_io_is_bad(ubi, pnum); |
| 954 | if (err < 0) |
| 955 | return err; |
| 956 | else if (err) { |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 957 | ai->bad_peb_count += 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); |
| 962 | if (err < 0) |
| 963 | return err; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 964 | switch (err) { |
| 965 | case 0: |
| 966 | break; |
| 967 | case UBI_IO_BITFLIPS: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 968 | bitflips = 1; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 969 | break; |
| 970 | case UBI_IO_FF: |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 971 | ai->empty_peb_count += 1; |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 972 | return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN, |
| 973 | UBI_UNKNOWN, 0, &ai->erase); |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 974 | case UBI_IO_FF_BITFLIPS: |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 975 | ai->empty_peb_count += 1; |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 976 | return add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN, |
| 977 | UBI_UNKNOWN, 1, &ai->erase); |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 978 | case UBI_IO_BAD_HDR_EBADMSG: |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 979 | case UBI_IO_BAD_HDR: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 980 | /* |
| 981 | * We have to also look at the VID header, possibly it is not |
| 982 | * corrupted. Set %bitflips flag in order to make this PEB be |
| 983 | * moved and EC be re-created. |
| 984 | */ |
Artem Bityutskiy | e0e718c | 2010-09-03 14:53:23 +0300 | [diff] [blame] | 985 | ec_err = err; |
Artem Bityutskiy | 9c47fb2 | 2012-05-18 12:54:58 +0300 | [diff] [blame] | 986 | ec = UBI_UNKNOWN; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 987 | bitflips = 1; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 988 | break; |
| 989 | default: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 990 | ubi_err(ubi, "'ubi_io_read_ec_hdr()' returned unknown code %d", |
| 991 | err); |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 992 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 993 | } |
| 994 | |
Artem Bityutskiy | e0e718c | 2010-09-03 14:53:23 +0300 | [diff] [blame] | 995 | if (!ec_err) { |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 996 | int image_seq; |
| 997 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 998 | /* Make sure UBI version is OK */ |
| 999 | if (ech->version != UBI_VERSION) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1000 | ubi_err(ubi, "this UBI version is %d, image version is %d", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1001 | UBI_VERSION, (int)ech->version); |
| 1002 | return -EINVAL; |
| 1003 | } |
| 1004 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1005 | ec = be64_to_cpu(ech->ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1006 | if (ec > UBI_MAX_ERASECOUNTER) { |
| 1007 | /* |
| 1008 | * Erase counter overflow. The EC headers have 64 bits |
| 1009 | * reserved, but we anyway make use of only 31 bit |
| 1010 | * values, as this seems to be enough for any existing |
| 1011 | * flash. Upgrade UBI and use 64-bit erase counters |
| 1012 | * internally. |
| 1013 | */ |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1014 | ubi_err(ubi, "erase counter overflow, max is %d", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1015 | UBI_MAX_ERASECOUNTER); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 1016 | ubi_dump_ec_hdr(ech); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1017 | return -EINVAL; |
| 1018 | } |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 1019 | |
Adrian Hunter | 32bc482 | 2009-07-24 19:16:04 +0300 | [diff] [blame] | 1020 | /* |
| 1021 | * Make sure that all PEBs have the same image sequence number. |
| 1022 | * This allows us to detect situations when users flash UBI |
| 1023 | * images incorrectly, so that the flash has the new UBI image |
| 1024 | * and leftovers from the old one. This feature was added |
| 1025 | * relatively recently, and the sequence number was always |
| 1026 | * zero, because old UBI implementations always set it to zero. |
| 1027 | * For this reasons, we do not panic if some PEBs have zero |
| 1028 | * sequence number, while other PEBs have non-zero sequence |
| 1029 | * number. |
| 1030 | */ |
Holger Brunck | 3dc948d | 2009-07-13 16:47:57 +0200 | [diff] [blame] | 1031 | image_seq = be32_to_cpu(ech->image_seq); |
Richard Genoud | 55b80c4 | 2013-09-28 15:55:14 +0200 | [diff] [blame] | 1032 | if (!ubi->image_seq) |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 1033 | ubi->image_seq = image_seq; |
Richard Genoud | 55b80c4 | 2013-09-28 15:55:14 +0200 | [diff] [blame] | 1034 | if (image_seq && ubi->image_seq != image_seq) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1035 | ubi_err(ubi, "bad image sequence number %d in PEB %d, expected %d", |
Artem Bityutskiy | 049333c | 2012-08-27 14:43:54 +0300 | [diff] [blame] | 1036 | image_seq, pnum, ubi->image_seq); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 1037 | ubi_dump_ec_hdr(ech); |
Artem Bityutskiy | fe96efc | 2009-06-30 16:11:59 +0300 | [diff] [blame] | 1038 | return -EINVAL; |
| 1039 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | /* OK, we've done with the EC header, let's look at the VID header */ |
| 1043 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1044 | err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1045 | if (err < 0) |
| 1046 | return err; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1047 | switch (err) { |
| 1048 | case 0: |
| 1049 | break; |
| 1050 | case UBI_IO_BITFLIPS: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1051 | bitflips = 1; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1052 | break; |
| 1053 | case UBI_IO_BAD_HDR_EBADMSG: |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1054 | if (ec_err == UBI_IO_BAD_HDR_EBADMSG) |
| 1055 | /* |
| 1056 | * Both EC and VID headers are corrupted and were read |
| 1057 | * with data integrity error, probably this is a bad |
| 1058 | * PEB, bit it is not marked as bad yet. This may also |
| 1059 | * be a result of power cut during erasure. |
| 1060 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1061 | ai->maybe_bad_peb_count += 1; |
Gustavo A. R. Silva | 7e5583f | 2018-10-05 10:29:31 +0200 | [diff] [blame] | 1062 | /* fall through */ |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1063 | case UBI_IO_BAD_HDR: |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 1064 | /* |
| 1065 | * If we're facing a bad VID header we have to drop *all* |
| 1066 | * Fastmap data structures we find. The most recent Fastmap |
| 1067 | * could be bad and therefore there is a chance that we attach |
| 1068 | * from an old one. On a fine MTD stack a PEB must not render |
| 1069 | * bad all of a sudden, but the reality is different. |
| 1070 | * So, let's be paranoid and help finding the root cause by |
| 1071 | * falling back to scanning mode instead of attaching with a |
| 1072 | * bad EBA table and cause data corruption which is hard to |
| 1073 | * analyze. |
| 1074 | */ |
| 1075 | if (fast) |
| 1076 | ai->force_full_scan = 1; |
| 1077 | |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1078 | if (ec_err) |
| 1079 | /* |
| 1080 | * Both headers are corrupted. There is a possibility |
| 1081 | * that this a valid UBI PEB which has corresponding |
| 1082 | * LEB, but the headers are corrupted. However, it is |
| 1083 | * impossible to distinguish it from a PEB which just |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 1084 | * contains garbage because of a power cut during erase |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1085 | * operation. So we just schedule this PEB for erasure. |
Artem Bityutskiy | 7ac760c | 2010-12-02 06:34:01 +0200 | [diff] [blame] | 1086 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1087 | * Besides, in case of NOR flash, we deliberately |
Artem Bityutskiy | 7ac760c | 2010-12-02 06:34:01 +0200 | [diff] [blame] | 1088 | * corrupt both headers because NOR flash erasure is |
| 1089 | * slow and can start from the end. |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1090 | */ |
| 1091 | err = 0; |
| 1092 | else |
| 1093 | /* |
| 1094 | * The EC was OK, but the VID header is corrupted. We |
| 1095 | * have to check what is in the data area. |
| 1096 | */ |
Artem Bityutskiy | 45aafd3 | 2010-10-20 11:54:58 +0300 | [diff] [blame] | 1097 | err = check_corruption(ubi, vidh, pnum); |
Artem Bityutskiy | df3fca4 | 2010-10-20 11:51:21 +0300 | [diff] [blame] | 1098 | |
| 1099 | if (err < 0) |
| 1100 | return err; |
| 1101 | else if (!err) |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1102 | /* This corruption is caused by a power cut */ |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1103 | err = add_to_list(ai, pnum, UBI_UNKNOWN, |
| 1104 | UBI_UNKNOWN, ec, 1, &ai->erase); |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1105 | else |
| 1106 | /* This is an unexpected corruption */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1107 | err = add_corrupted(ai, pnum, ec); |
Artem Bityutskiy | feeba4b | 2010-09-03 22:50:53 +0300 | [diff] [blame] | 1108 | if (err) |
| 1109 | return err; |
| 1110 | goto adjust_mean_ec; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1111 | case UBI_IO_FF_BITFLIPS: |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1112 | err = add_to_list(ai, pnum, UBI_UNKNOWN, UBI_UNKNOWN, |
| 1113 | ec, 1, &ai->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1114 | if (err) |
| 1115 | return err; |
| 1116 | goto adjust_mean_ec; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1117 | case UBI_IO_FF: |
Matthieu CASTET | 193819c | 2012-08-22 16:03:46 +0200 | [diff] [blame] | 1118 | if (ec_err || bitflips) |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1119 | err = add_to_list(ai, pnum, UBI_UNKNOWN, |
| 1120 | UBI_UNKNOWN, ec, 1, &ai->erase); |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1121 | else |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1122 | err = add_to_list(ai, pnum, UBI_UNKNOWN, |
| 1123 | UBI_UNKNOWN, ec, 0, &ai->free); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1124 | if (err) |
| 1125 | return err; |
| 1126 | goto adjust_mean_ec; |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1127 | default: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1128 | ubi_err(ubi, "'ubi_io_read_vid_hdr()' returned unknown code %d", |
Artem Bityutskiy | b332150 | 2010-09-03 14:40:55 +0300 | [diff] [blame] | 1129 | err); |
| 1130 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1131 | } |
| 1132 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1133 | vol_id = be32_to_cpu(vidh->vol_id); |
Richard Weinberger | 243a4f8 | 2016-06-14 10:12:13 +0200 | [diff] [blame] | 1134 | if (vol_id > UBI_MAX_VOLUMES && !vol_ignored(vol_id)) { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1135 | int lnum = be32_to_cpu(vidh->lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1136 | |
| 1137 | /* Unsupported internal volume */ |
| 1138 | switch (vidh->compat) { |
| 1139 | case UBI_COMPAT_DELETE: |
Richard Weinberger | 243a4f8 | 2016-06-14 10:12:13 +0200 | [diff] [blame] | 1140 | ubi_msg(ubi, "\"delete\" compatible internal volume %d:%d found, will remove it", |
| 1141 | vol_id, lnum); |
| 1142 | |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1143 | err = add_to_list(ai, pnum, vol_id, lnum, |
| 1144 | ec, 1, &ai->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1145 | if (err) |
| 1146 | return err; |
Brijesh Singh | 158132c | 2010-06-16 09:28:26 +0300 | [diff] [blame] | 1147 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1148 | |
| 1149 | case UBI_COMPAT_RO: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1150 | ubi_msg(ubi, "read-only compatible internal volume %d:%d found, switch to read-only mode", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1151 | vol_id, lnum); |
| 1152 | ubi->ro_mode = 1; |
| 1153 | break; |
| 1154 | |
| 1155 | case UBI_COMPAT_PRESERVE: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1156 | ubi_msg(ubi, "\"preserve\" compatible internal volume %d:%d found", |
Artem Bityutskiy | 049333c | 2012-08-27 14:43:54 +0300 | [diff] [blame] | 1157 | vol_id, lnum); |
Joel Reardon | 6dd3bc7 | 2012-05-16 14:20:56 +0200 | [diff] [blame] | 1158 | err = add_to_list(ai, pnum, vol_id, lnum, |
| 1159 | ec, 0, &ai->alien); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1160 | if (err) |
| 1161 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1162 | return 0; |
| 1163 | |
| 1164 | case UBI_COMPAT_REJECT: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1165 | ubi_err(ubi, "incompatible internal volume %d:%d found", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1166 | vol_id, lnum); |
| 1167 | return -EINVAL; |
| 1168 | } |
| 1169 | } |
| 1170 | |
Artem Bityutskiy | e0e718c | 2010-09-03 14:53:23 +0300 | [diff] [blame] | 1171 | if (ec_err) |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1172 | ubi_warn(ubi, "valid VID header but corrupted EC header at PEB %d", |
Artem Bityutskiy | 29a88c9 | 2009-07-19 14:03:16 +0300 | [diff] [blame] | 1173 | pnum); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1174 | |
| 1175 | if (ubi_is_fm_vol(vol_id)) |
| 1176 | err = add_fastmap(ai, pnum, vidh, ec); |
| 1177 | else |
| 1178 | err = ubi_add_to_av(ubi, ai, pnum, ec, vidh, bitflips); |
| 1179 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1180 | if (err) |
| 1181 | return err; |
| 1182 | |
| 1183 | adjust_mean_ec: |
Artem Bityutskiy | e0e718c | 2010-09-03 14:53:23 +0300 | [diff] [blame] | 1184 | if (!ec_err) { |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1185 | ai->ec_sum += ec; |
| 1186 | ai->ec_count += 1; |
| 1187 | if (ec > ai->max_ec) |
| 1188 | ai->max_ec = ec; |
| 1189 | if (ec < ai->min_ec) |
| 1190 | ai->min_ec = ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | return 0; |
| 1194 | } |
| 1195 | |
| 1196 | /** |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 1197 | * late_analysis - analyze the overall situation with PEB. |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1198 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1199 | * @ai: attaching information |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1200 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 1201 | * This is a helper function which takes a look what PEBs we have after we |
| 1202 | * gather information about all of them ("ai" is compete). It decides whether |
| 1203 | * the flash is empty and should be formatted of whether there are too many |
| 1204 | * corrupted PEBs and we should not attach this MTD device. Returns zero if we |
| 1205 | * should proceed with attaching the MTD device, and %-EINVAL if we should not. |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1206 | */ |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 1207 | static int late_analysis(struct ubi_device *ubi, struct ubi_attach_info *ai) |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1208 | { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1209 | struct ubi_ainf_peb *aeb; |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1210 | int max_corr, peb_count; |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1211 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1212 | peb_count = ubi->peb_count - ai->bad_peb_count - ai->alien_peb_count; |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1213 | max_corr = peb_count / 20 ?: 8; |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1214 | |
| 1215 | /* |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1216 | * Few corrupted PEBs is not a problem and may be just a result of |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1217 | * unclean reboots. However, many of them may indicate some problems |
| 1218 | * with the flash HW or driver. |
| 1219 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1220 | if (ai->corr_peb_count) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1221 | ubi_err(ubi, "%d PEBs are corrupted and preserved", |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1222 | ai->corr_peb_count); |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 1223 | pr_err("Corrupted PEBs are:"); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1224 | list_for_each_entry(aeb, &ai->corr, u.list) |
Artem Bityutskiy | e28453b | 2012-08-27 15:13:05 +0300 | [diff] [blame] | 1225 | pr_cont(" %d", aeb->pnum); |
| 1226 | pr_cont("\n"); |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1227 | |
| 1228 | /* |
| 1229 | * If too many PEBs are corrupted, we refuse attaching, |
| 1230 | * otherwise, only print a warning. |
| 1231 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1232 | if (ai->corr_peb_count >= max_corr) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1233 | ubi_err(ubi, "too many corrupted PEBs, refusing"); |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1234 | return -EINVAL; |
| 1235 | } |
| 1236 | } |
| 1237 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1238 | if (ai->empty_peb_count + ai->maybe_bad_peb_count == peb_count) { |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1239 | /* |
| 1240 | * All PEBs are empty, or almost all - a couple PEBs look like |
| 1241 | * they may be bad PEBs which were not marked as bad yet. |
| 1242 | * |
| 1243 | * This piece of code basically tries to distinguish between |
| 1244 | * the following situations: |
| 1245 | * |
| 1246 | * 1. Flash is empty, but there are few bad PEBs, which are not |
| 1247 | * marked as bad so far, and which were read with error. We |
| 1248 | * want to go ahead and format this flash. While formatting, |
| 1249 | * the faulty PEBs will probably be marked as bad. |
| 1250 | * |
| 1251 | * 2. Flash contains non-UBI data and we do not want to format |
| 1252 | * it and destroy possibly important information. |
| 1253 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1254 | if (ai->maybe_bad_peb_count <= 2) { |
| 1255 | ai->is_empty = 1; |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1256 | ubi_msg(ubi, "empty MTD device detected"); |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1257 | get_random_bytes(&ubi->image_seq, |
| 1258 | sizeof(ubi->image_seq)); |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1259 | } else { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1260 | ubi_err(ubi, "MTD device is not UBI-formatted and possibly contains non-UBI data - refusing it"); |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1261 | return -EINVAL; |
| 1262 | } |
Artem Bityutskiy | 0525dac | 2010-09-03 17:11:37 +0300 | [diff] [blame] | 1263 | |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1264 | } |
| 1265 | |
Artem Bityutskiy | 0798cea | 2010-04-30 13:02:33 +0300 | [diff] [blame] | 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 1270 | * destroy_av - free volume attaching information. |
| 1271 | * @av: volume attaching information |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1272 | * @ai: attaching information |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 1273 | * @list: put the aeb elements in there if !NULL, otherwise free them |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1274 | * |
Artem Bityutskiy | fbd0107 | 2012-05-17 16:12:26 +0300 | [diff] [blame] | 1275 | * This function destroys the volume attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1276 | */ |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 1277 | static void destroy_av(struct ubi_attach_info *ai, struct ubi_ainf_volume *av, |
| 1278 | struct list_head *list) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1279 | { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1280 | struct ubi_ainf_peb *aeb; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1281 | struct rb_node *this = av->root.rb_node; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1282 | |
| 1283 | while (this) { |
| 1284 | if (this->rb_left) |
| 1285 | this = this->rb_left; |
| 1286 | else if (this->rb_right) |
| 1287 | this = this->rb_right; |
| 1288 | else { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1289 | aeb = rb_entry(this, struct ubi_ainf_peb, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1290 | this = rb_parent(this); |
| 1291 | if (this) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1292 | if (this->rb_left == &aeb->u.rb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1293 | this->rb_left = NULL; |
| 1294 | else |
| 1295 | this->rb_right = NULL; |
| 1296 | } |
| 1297 | |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 1298 | if (list) |
| 1299 | list_add_tail(&aeb->u.list, list); |
| 1300 | else |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1301 | ubi_free_aeb(ai, aeb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1302 | } |
| 1303 | } |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1304 | kfree(av); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | /** |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1308 | * destroy_ai - destroy attaching information. |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1309 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1310 | */ |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1311 | static void destroy_ai(struct ubi_attach_info *ai) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1312 | { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1313 | struct ubi_ainf_peb *aeb, *aeb_tmp; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1314 | struct ubi_ainf_volume *av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1315 | struct rb_node *rb; |
| 1316 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1317 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->alien, u.list) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1318 | list_del(&aeb->u.list); |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1319 | ubi_free_aeb(ai, aeb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1320 | } |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1321 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->erase, u.list) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1322 | list_del(&aeb->u.list); |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1323 | ubi_free_aeb(ai, aeb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1324 | } |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1325 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->corr, u.list) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1326 | list_del(&aeb->u.list); |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1327 | ubi_free_aeb(ai, aeb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1328 | } |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1329 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->free, u.list) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1330 | list_del(&aeb->u.list); |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1331 | ubi_free_aeb(ai, aeb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1332 | } |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1333 | list_for_each_entry_safe(aeb, aeb_tmp, &ai->fastmap, u.list) { |
| 1334 | list_del(&aeb->u.list); |
Boris Brezillon | 91f4285 | 2016-09-16 16:59:18 +0200 | [diff] [blame] | 1335 | ubi_free_aeb(ai, aeb); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1336 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1337 | |
| 1338 | /* Destroy the volume RB-tree */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1339 | rb = ai->volumes.rb_node; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1340 | while (rb) { |
| 1341 | if (rb->rb_left) |
| 1342 | rb = rb->rb_left; |
| 1343 | else if (rb->rb_right) |
| 1344 | rb = rb->rb_right; |
| 1345 | else { |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1346 | av = rb_entry(rb, struct ubi_ainf_volume, rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1347 | |
| 1348 | rb = rb_parent(rb); |
| 1349 | if (rb) { |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1350 | if (rb->rb_left == &av->rb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1351 | rb->rb_left = NULL; |
| 1352 | else |
| 1353 | rb->rb_right = NULL; |
| 1354 | } |
| 1355 | |
Boris Brezillon | f9efe8d | 2016-09-16 16:59:15 +0200 | [diff] [blame] | 1356 | destroy_av(ai, av, NULL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1357 | } |
| 1358 | } |
| 1359 | |
Julia Lawall | f9a113d | 2015-09-13 14:15:16 +0200 | [diff] [blame] | 1360 | kmem_cache_destroy(ai->aeb_slab_cache); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1361 | kfree(ai); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1362 | } |
| 1363 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1364 | /** |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1365 | * scan_all - scan entire MTD device. |
| 1366 | * @ubi: UBI device description object |
| 1367 | * @ai: attach info object |
| 1368 | * @start: start scanning at this PEB |
| 1369 | * |
| 1370 | * This function does full scanning of an MTD device and returns complete |
| 1371 | * information about it in form of a "struct ubi_attach_info" object. In case |
| 1372 | * of failure, an error code is returned. |
| 1373 | */ |
| 1374 | static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai, |
| 1375 | int start) |
| 1376 | { |
| 1377 | int err, pnum; |
| 1378 | struct rb_node *rb1, *rb2; |
| 1379 | struct ubi_ainf_volume *av; |
| 1380 | struct ubi_ainf_peb *aeb; |
| 1381 | |
| 1382 | err = -ENOMEM; |
| 1383 | |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1384 | ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 1385 | if (!ai->ech) |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1386 | return err; |
| 1387 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1388 | ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); |
| 1389 | if (!ai->vidb) |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1390 | goto out_ech; |
| 1391 | |
| 1392 | for (pnum = start; pnum < ubi->peb_count; pnum++) { |
| 1393 | cond_resched(); |
| 1394 | |
| 1395 | dbg_gen("process PEB %d", pnum); |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 1396 | err = scan_peb(ubi, ai, pnum, false); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1397 | if (err < 0) |
| 1398 | goto out_vidh; |
| 1399 | } |
| 1400 | |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1401 | ubi_msg(ubi, "scanning is finished"); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1402 | |
| 1403 | /* Calculate mean erase counter */ |
| 1404 | if (ai->ec_count) |
| 1405 | ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); |
| 1406 | |
| 1407 | err = late_analysis(ubi, ai); |
| 1408 | if (err) |
| 1409 | goto out_vidh; |
| 1410 | |
| 1411 | /* |
| 1412 | * In case of unknown erase counter we use the mean erase counter |
| 1413 | * value. |
| 1414 | */ |
| 1415 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
| 1416 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) |
| 1417 | if (aeb->ec == UBI_UNKNOWN) |
| 1418 | aeb->ec = ai->mean_ec; |
| 1419 | } |
| 1420 | |
| 1421 | list_for_each_entry(aeb, &ai->free, u.list) { |
| 1422 | if (aeb->ec == UBI_UNKNOWN) |
| 1423 | aeb->ec = ai->mean_ec; |
| 1424 | } |
| 1425 | |
| 1426 | list_for_each_entry(aeb, &ai->corr, u.list) |
| 1427 | if (aeb->ec == UBI_UNKNOWN) |
| 1428 | aeb->ec = ai->mean_ec; |
| 1429 | |
| 1430 | list_for_each_entry(aeb, &ai->erase, u.list) |
| 1431 | if (aeb->ec == UBI_UNKNOWN) |
| 1432 | aeb->ec = ai->mean_ec; |
| 1433 | |
| 1434 | err = self_check_ai(ubi, ai); |
| 1435 | if (err) |
| 1436 | goto out_vidh; |
| 1437 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1438 | ubi_free_vid_buf(ai->vidb); |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1439 | kfree(ai->ech); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1440 | |
| 1441 | return 0; |
| 1442 | |
| 1443 | out_vidh: |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1444 | ubi_free_vid_buf(ai->vidb); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1445 | out_ech: |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1446 | kfree(ai->ech); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1447 | return err; |
| 1448 | } |
| 1449 | |
Richard Weinberger | d2158f6 | 2014-10-06 15:58:07 +0200 | [diff] [blame] | 1450 | static struct ubi_attach_info *alloc_ai(void) |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1451 | { |
| 1452 | struct ubi_attach_info *ai; |
| 1453 | |
| 1454 | ai = kzalloc(sizeof(struct ubi_attach_info), GFP_KERNEL); |
| 1455 | if (!ai) |
| 1456 | return ai; |
| 1457 | |
| 1458 | INIT_LIST_HEAD(&ai->corr); |
| 1459 | INIT_LIST_HEAD(&ai->free); |
| 1460 | INIT_LIST_HEAD(&ai->erase); |
| 1461 | INIT_LIST_HEAD(&ai->alien); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1462 | INIT_LIST_HEAD(&ai->fastmap); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1463 | ai->volumes = RB_ROOT; |
Richard Weinberger | d2158f6 | 2014-10-06 15:58:07 +0200 | [diff] [blame] | 1464 | ai->aeb_slab_cache = kmem_cache_create("ubi_aeb_slab_cache", |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1465 | sizeof(struct ubi_ainf_peb), |
| 1466 | 0, 0, NULL); |
| 1467 | if (!ai->aeb_slab_cache) { |
| 1468 | kfree(ai); |
| 1469 | ai = NULL; |
| 1470 | } |
| 1471 | |
| 1472 | return ai; |
| 1473 | } |
| 1474 | |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1475 | #ifdef CONFIG_MTD_UBI_FASTMAP |
| 1476 | |
| 1477 | /** |
Richard Weinberger | d139d30 | 2016-06-14 10:12:12 +0200 | [diff] [blame] | 1478 | * scan_fast - try to find a fastmap and attach from it. |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1479 | * @ubi: UBI device description object |
| 1480 | * @ai: attach info object |
| 1481 | * |
| 1482 | * Returns 0 on success, negative return values indicate an internal |
| 1483 | * error. |
| 1484 | * UBI_NO_FASTMAP denotes that no fastmap was found. |
| 1485 | * UBI_BAD_FASTMAP denotes that the found fastmap was invalid. |
| 1486 | */ |
| 1487 | static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai) |
| 1488 | { |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1489 | int err, pnum; |
| 1490 | struct ubi_attach_info *scan_ai; |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1491 | |
| 1492 | err = -ENOMEM; |
| 1493 | |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1494 | scan_ai = alloc_ai(); |
| 1495 | if (!scan_ai) |
| 1496 | goto out; |
| 1497 | |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1498 | scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 1499 | if (!scan_ai->ech) |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1500 | goto out_ai; |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1501 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1502 | scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); |
| 1503 | if (!scan_ai->vidb) |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1504 | goto out_ech; |
| 1505 | |
| 1506 | for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) { |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1507 | cond_resched(); |
| 1508 | |
| 1509 | dbg_gen("process PEB %d", pnum); |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 1510 | err = scan_peb(ubi, scan_ai, pnum, true); |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1511 | if (err < 0) |
| 1512 | goto out_vidh; |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1513 | } |
| 1514 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1515 | ubi_free_vid_buf(scan_ai->vidb); |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1516 | kfree(scan_ai->ech); |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1517 | |
Richard Weinberger | 74f2c6e | 2016-06-14 10:12:17 +0200 | [diff] [blame] | 1518 | if (scan_ai->force_full_scan) |
| 1519 | err = UBI_NO_FASTMAP; |
| 1520 | else |
| 1521 | err = ubi_scan_fastmap(ubi, *ai, scan_ai); |
| 1522 | |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1523 | if (err) { |
| 1524 | /* |
| 1525 | * Didn't attach via fastmap, do a full scan but reuse what |
| 1526 | * we've aready scanned. |
| 1527 | */ |
| 1528 | destroy_ai(*ai); |
| 1529 | *ai = scan_ai; |
| 1530 | } else |
| 1531 | destroy_ai(scan_ai); |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1532 | |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1533 | return err; |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1534 | |
| 1535 | out_vidh: |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1536 | ubi_free_vid_buf(scan_ai->vidb); |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1537 | out_ech: |
Boris Brezillon | 7b6b749 | 2016-09-16 16:59:19 +0200 | [diff] [blame] | 1538 | kfree(scan_ai->ech); |
Richard Weinberger | fdf10ed | 2016-06-14 10:12:15 +0200 | [diff] [blame] | 1539 | out_ai: |
| 1540 | destroy_ai(scan_ai); |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1541 | out: |
| 1542 | return err; |
| 1543 | } |
| 1544 | |
| 1545 | #endif |
| 1546 | |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1547 | /** |
| 1548 | * ubi_attach - attach an MTD device. |
| 1549 | * @ubi: UBI device descriptor |
| 1550 | * @force_scan: if set to non-zero attach by scanning |
| 1551 | * |
| 1552 | * This function returns zero in case of success and a negative error code in |
| 1553 | * case of failure. |
| 1554 | */ |
| 1555 | int ubi_attach(struct ubi_device *ubi, int force_scan) |
| 1556 | { |
| 1557 | int err; |
| 1558 | struct ubi_attach_info *ai; |
| 1559 | |
Richard Weinberger | d2158f6 | 2014-10-06 15:58:07 +0200 | [diff] [blame] | 1560 | ai = alloc_ai(); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1561 | if (!ai) |
| 1562 | return -ENOMEM; |
| 1563 | |
| 1564 | #ifdef CONFIG_MTD_UBI_FASTMAP |
| 1565 | /* On small flash devices we disable fastmap in any case. */ |
| 1566 | if ((int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd) <= UBI_FM_MAX_START) { |
| 1567 | ubi->fm_disabled = 1; |
| 1568 | force_scan = 1; |
| 1569 | } |
| 1570 | |
| 1571 | if (force_scan) |
| 1572 | err = scan_all(ubi, ai, 0); |
| 1573 | else { |
Richard Weinberger | 98105d0 | 2014-10-06 15:39:01 +0200 | [diff] [blame] | 1574 | err = scan_fast(ubi, &ai); |
Richard Weinberger | 180a535 | 2015-03-09 10:04:09 +0100 | [diff] [blame] | 1575 | if (err > 0 || mtd_is_eccerr(err)) { |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1576 | if (err != UBI_NO_FASTMAP) { |
| 1577 | destroy_ai(ai); |
Richard Weinberger | d2158f6 | 2014-10-06 15:58:07 +0200 | [diff] [blame] | 1578 | ai = alloc_ai(); |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1579 | if (!ai) |
| 1580 | return -ENOMEM; |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1581 | |
Richard Weinberger | 4b3e0a2 | 2013-09-28 15:55:12 +0200 | [diff] [blame] | 1582 | err = scan_all(ubi, ai, 0); |
| 1583 | } else { |
| 1584 | err = scan_all(ubi, ai, UBI_FM_MAX_START); |
| 1585 | } |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1586 | } |
| 1587 | } |
| 1588 | #else |
| 1589 | err = scan_all(ubi, ai, 0); |
| 1590 | #endif |
| 1591 | if (err) |
| 1592 | goto out_ai; |
| 1593 | |
| 1594 | ubi->bad_peb_count = ai->bad_peb_count; |
| 1595 | ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; |
| 1596 | ubi->corr_peb_count = ai->corr_peb_count; |
| 1597 | ubi->max_ec = ai->max_ec; |
| 1598 | ubi->mean_ec = ai->mean_ec; |
| 1599 | dbg_gen("max. sequence number: %llu", ai->max_sqnum); |
| 1600 | |
| 1601 | err = ubi_read_volume_table(ubi, ai); |
| 1602 | if (err) |
| 1603 | goto out_ai; |
| 1604 | |
| 1605 | err = ubi_wl_init(ubi, ai); |
| 1606 | if (err) |
| 1607 | goto out_vtbl; |
| 1608 | |
| 1609 | err = ubi_eba_init(ubi, ai); |
| 1610 | if (err) |
| 1611 | goto out_wl; |
| 1612 | |
| 1613 | #ifdef CONFIG_MTD_UBI_FASTMAP |
Richard Weinberger | 560d86a | 2014-10-27 13:00:26 +0100 | [diff] [blame] | 1614 | if (ubi->fm && ubi_dbg_chk_fastmap(ubi)) { |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1615 | struct ubi_attach_info *scan_ai; |
| 1616 | |
Richard Weinberger | d2158f6 | 2014-10-06 15:58:07 +0200 | [diff] [blame] | 1617 | scan_ai = alloc_ai(); |
Julia Lawall | 4d52514 | 2013-12-29 23:47:19 +0100 | [diff] [blame] | 1618 | if (!scan_ai) { |
| 1619 | err = -ENOMEM; |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1620 | goto out_wl; |
Julia Lawall | 4d52514 | 2013-12-29 23:47:19 +0100 | [diff] [blame] | 1621 | } |
Richard Weinberger | dac6e20 | 2012-09-26 17:51:47 +0200 | [diff] [blame] | 1622 | |
| 1623 | err = scan_all(ubi, scan_ai, 0); |
| 1624 | if (err) { |
| 1625 | destroy_ai(scan_ai); |
| 1626 | goto out_wl; |
| 1627 | } |
| 1628 | |
| 1629 | err = self_check_eba(ubi, ai, scan_ai); |
| 1630 | destroy_ai(scan_ai); |
| 1631 | |
| 1632 | if (err) |
| 1633 | goto out_wl; |
| 1634 | } |
| 1635 | #endif |
| 1636 | |
| 1637 | destroy_ai(ai); |
| 1638 | return 0; |
| 1639 | |
| 1640 | out_wl: |
| 1641 | ubi_wl_close(ubi); |
| 1642 | out_vtbl: |
| 1643 | ubi_free_internal_volumes(ubi); |
| 1644 | vfree(ubi->vtbl); |
| 1645 | out_ai: |
| 1646 | destroy_ai(ai); |
| 1647 | return err; |
| 1648 | } |
| 1649 | |
| 1650 | /** |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1651 | * self_check_ai - check the attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1652 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1653 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1654 | * |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1655 | * This function returns zero if the attaching information is all right, and a |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1656 | * negative error code if not or if an error occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1657 | */ |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1658 | static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1659 | { |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1660 | struct ubi_vid_io_buf *vidb = ai->vidb; |
| 1661 | struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1662 | int pnum, err, vols_found = 0; |
| 1663 | struct rb_node *rb1, *rb2; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1664 | struct ubi_ainf_volume *av; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1665 | struct ubi_ainf_peb *aeb, *last_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1666 | uint8_t *buf; |
| 1667 | |
Ezequiel Garcia | 6457557 | 2012-11-28 09:18:29 -0300 | [diff] [blame] | 1668 | if (!ubi_dbg_chk_gen(ubi)) |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 1669 | return 0; |
| 1670 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1671 | /* |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1672 | * At first, check that attaching information is OK. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1673 | */ |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1674 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1675 | int leb_count = 0; |
| 1676 | |
| 1677 | cond_resched(); |
| 1678 | |
| 1679 | vols_found += 1; |
| 1680 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1681 | if (ai->is_empty) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1682 | ubi_err(ubi, "bad is_empty flag"); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1683 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1684 | } |
| 1685 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1686 | if (av->vol_id < 0 || av->highest_lnum < 0 || |
| 1687 | av->leb_count < 0 || av->vol_type < 0 || av->used_ebs < 0 || |
| 1688 | av->data_pad < 0 || av->last_data_size < 0) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1689 | ubi_err(ubi, "negative values"); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1690 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1691 | } |
| 1692 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1693 | if (av->vol_id >= UBI_MAX_VOLUMES && |
| 1694 | av->vol_id < UBI_INTERNAL_VOL_START) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1695 | ubi_err(ubi, "bad vol_id"); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1696 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1697 | } |
| 1698 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1699 | if (av->vol_id > ai->highest_vol_id) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1700 | ubi_err(ubi, "highest_vol_id is %d, but vol_id %d is there", |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1701 | ai->highest_vol_id, av->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1702 | goto out; |
| 1703 | } |
| 1704 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1705 | if (av->vol_type != UBI_DYNAMIC_VOLUME && |
| 1706 | av->vol_type != UBI_STATIC_VOLUME) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1707 | ubi_err(ubi, "bad vol_type"); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1708 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1709 | } |
| 1710 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1711 | if (av->data_pad > ubi->leb_size / 2) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1712 | ubi_err(ubi, "bad data_pad"); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1713 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1714 | } |
| 1715 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1716 | last_aeb = NULL; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1717 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1718 | cond_resched(); |
| 1719 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1720 | last_aeb = aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1721 | leb_count += 1; |
| 1722 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1723 | if (aeb->pnum < 0 || aeb->ec < 0) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1724 | ubi_err(ubi, "negative values"); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1725 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1726 | } |
| 1727 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1728 | if (aeb->ec < ai->min_ec) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1729 | ubi_err(ubi, "bad ai->min_ec (%d), %d found", |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1730 | ai->min_ec, aeb->ec); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1731 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1732 | } |
| 1733 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1734 | if (aeb->ec > ai->max_ec) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1735 | ubi_err(ubi, "bad ai->max_ec (%d), %d found", |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1736 | ai->max_ec, aeb->ec); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1737 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1738 | } |
| 1739 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1740 | if (aeb->pnum >= ubi->peb_count) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1741 | ubi_err(ubi, "too high PEB number %d, total PEBs %d", |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1742 | aeb->pnum, ubi->peb_count); |
| 1743 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1744 | } |
| 1745 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1746 | if (av->vol_type == UBI_STATIC_VOLUME) { |
| 1747 | if (aeb->lnum >= av->used_ebs) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1748 | ubi_err(ubi, "bad lnum or used_ebs"); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1749 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1750 | } |
| 1751 | } else { |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1752 | if (av->used_ebs != 0) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1753 | ubi_err(ubi, "non-zero used_ebs"); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1754 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1755 | } |
| 1756 | } |
| 1757 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1758 | if (aeb->lnum > av->highest_lnum) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1759 | ubi_err(ubi, "incorrect highest_lnum or lnum"); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1760 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1764 | if (av->leb_count != leb_count) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1765 | ubi_err(ubi, "bad leb_count, %d objects in the tree", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1766 | leb_count); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1767 | goto bad_av; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1768 | } |
| 1769 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1770 | if (!last_aeb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1771 | continue; |
| 1772 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1773 | aeb = last_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1774 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1775 | if (aeb->lnum != av->highest_lnum) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1776 | ubi_err(ubi, "bad highest_lnum"); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1777 | goto bad_aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1778 | } |
| 1779 | } |
| 1780 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1781 | if (vols_found != ai->vols_found) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1782 | ubi_err(ubi, "bad ai->vols_found %d, should be %d", |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1783 | ai->vols_found, vols_found); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1784 | goto out; |
| 1785 | } |
| 1786 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1787 | /* Check that attaching information is correct */ |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1788 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1789 | last_aeb = NULL; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1790 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1791 | int vol_type; |
| 1792 | |
| 1793 | cond_resched(); |
| 1794 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1795 | last_aeb = aeb; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1796 | |
Boris Brezillon | 3291b52 | 2016-09-16 16:59:26 +0200 | [diff] [blame] | 1797 | err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1798 | if (err && err != UBI_IO_BITFLIPS) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1799 | ubi_err(ubi, "VID header is not OK (%d)", |
| 1800 | err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1801 | if (err > 0) |
| 1802 | err = -EIO; |
| 1803 | return err; |
| 1804 | } |
| 1805 | |
| 1806 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? |
| 1807 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1808 | if (av->vol_type != vol_type) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1809 | ubi_err(ubi, "bad vol_type"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1810 | goto bad_vid_hdr; |
| 1811 | } |
| 1812 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1813 | if (aeb->sqnum != be64_to_cpu(vidh->sqnum)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1814 | ubi_err(ubi, "bad sqnum %llu", aeb->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1815 | goto bad_vid_hdr; |
| 1816 | } |
| 1817 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1818 | if (av->vol_id != be32_to_cpu(vidh->vol_id)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1819 | ubi_err(ubi, "bad vol_id %d", av->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1820 | goto bad_vid_hdr; |
| 1821 | } |
| 1822 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1823 | if (av->compat != vidh->compat) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1824 | ubi_err(ubi, "bad compat %d", vidh->compat); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1825 | goto bad_vid_hdr; |
| 1826 | } |
| 1827 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1828 | if (aeb->lnum != be32_to_cpu(vidh->lnum)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1829 | ubi_err(ubi, "bad lnum %d", aeb->lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1830 | goto bad_vid_hdr; |
| 1831 | } |
| 1832 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1833 | if (av->used_ebs != be32_to_cpu(vidh->used_ebs)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1834 | ubi_err(ubi, "bad used_ebs %d", av->used_ebs); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1835 | goto bad_vid_hdr; |
| 1836 | } |
| 1837 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1838 | if (av->data_pad != be32_to_cpu(vidh->data_pad)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1839 | ubi_err(ubi, "bad data_pad %d", av->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1840 | goto bad_vid_hdr; |
| 1841 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1842 | } |
| 1843 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1844 | if (!last_aeb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1845 | continue; |
| 1846 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1847 | if (av->highest_lnum != be32_to_cpu(vidh->lnum)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1848 | ubi_err(ubi, "bad highest_lnum %d", av->highest_lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1849 | goto bad_vid_hdr; |
| 1850 | } |
| 1851 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1852 | if (av->last_data_size != be32_to_cpu(vidh->data_size)) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1853 | ubi_err(ubi, "bad last_data_size %d", |
| 1854 | av->last_data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1855 | goto bad_vid_hdr; |
| 1856 | } |
| 1857 | } |
| 1858 | |
| 1859 | /* |
| 1860 | * Make sure that all the physical eraseblocks are in one of the lists |
| 1861 | * or trees. |
| 1862 | */ |
Mariusz Kozlowski | d9b0744 | 2007-08-01 00:02:10 +0200 | [diff] [blame] | 1863 | buf = kzalloc(ubi->peb_count, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1864 | if (!buf) |
| 1865 | return -ENOMEM; |
| 1866 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1867 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
| 1868 | err = ubi_io_is_bad(ubi, pnum); |
Artem Bityutskiy | 341e1a0 | 2007-05-03 11:59:51 +0300 | [diff] [blame] | 1869 | if (err < 0) { |
| 1870 | kfree(buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1871 | return err; |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 1872 | } else if (err) |
Mariusz Kozlowski | d9b0744 | 2007-08-01 00:02:10 +0200 | [diff] [blame] | 1873 | buf[pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1874 | } |
| 1875 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1876 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) |
| 1877 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1878 | buf[aeb->pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1879 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1880 | list_for_each_entry(aeb, &ai->free, u.list) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1881 | buf[aeb->pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1882 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1883 | list_for_each_entry(aeb, &ai->corr, u.list) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1884 | buf[aeb->pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1885 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1886 | list_for_each_entry(aeb, &ai->erase, u.list) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1887 | buf[aeb->pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1888 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1889 | list_for_each_entry(aeb, &ai->alien, u.list) |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1890 | buf[aeb->pnum] = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1891 | |
| 1892 | err = 0; |
| 1893 | for (pnum = 0; pnum < ubi->peb_count; pnum++) |
Mariusz Kozlowski | d9b0744 | 2007-08-01 00:02:10 +0200 | [diff] [blame] | 1894 | if (!buf[pnum]) { |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1895 | ubi_err(ubi, "PEB %d is not referred", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1896 | err = 1; |
| 1897 | } |
| 1898 | |
| 1899 | kfree(buf); |
| 1900 | if (err) |
| 1901 | goto out; |
| 1902 | return 0; |
| 1903 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1904 | bad_aeb: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1905 | ubi_err(ubi, "bad attaching information about LEB %d", aeb->lnum); |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1906 | ubi_dump_aeb(aeb, 0); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1907 | ubi_dump_av(av); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1908 | goto out; |
| 1909 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1910 | bad_av: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1911 | ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1912 | ubi_dump_av(av); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1913 | goto out; |
| 1914 | |
| 1915 | bad_vid_hdr: |
Tanya Brokhman | 32608703 | 2014-10-20 19:57:00 +0300 | [diff] [blame] | 1916 | ubi_err(ubi, "bad attaching information about volume %d", av->vol_id); |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1917 | ubi_dump_av(av); |
Artem Bityutskiy | a904e3f | 2012-04-25 09:02:44 +0300 | [diff] [blame] | 1918 | ubi_dump_vid_hdr(vidh); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1919 | |
| 1920 | out: |
Artem Bityutskiy | 25886a3 | 2012-04-24 06:59:49 +0300 | [diff] [blame] | 1921 | dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1922 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1923 | } |