Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 19 | */ |
| 20 | |
| 21 | /* |
| 22 | * UBI scanning unit. |
| 23 | * |
| 24 | * This unit is responsible for scanning the flash media, checking UBI |
| 25 | * headers and providing complete information about the UBI flash image. |
| 26 | * |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 27 | * The scanning information is represented by a &struct ubi_scan_info' object. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 28 | * Information about found volumes is represented by &struct ubi_scan_volume |
| 29 | * objects which are kept in volume RB-tree with root at the @volumes field. |
| 30 | * The RB-tree is indexed by the volume ID. |
| 31 | * |
| 32 | * Found logical eraseblocks are represented by &struct ubi_scan_leb objects. |
| 33 | * These objects are kept in per-volume RB-trees with the root at the |
| 34 | * corresponding &struct ubi_scan_volume object. To put it differently, we keep |
| 35 | * an RB-tree of per-volume objects and each of these objects is the root of |
| 36 | * RB-tree of per-eraseblock objects. |
| 37 | * |
| 38 | * Corrupted physical eraseblocks are put to the @corr list, free physical |
| 39 | * eraseblocks are put to the @free list and the physical eraseblock to be |
| 40 | * erased are put to the @erase list. |
| 41 | */ |
| 42 | |
| 43 | #include <linux/err.h> |
| 44 | #include <linux/crc32.h> |
| 45 | #include "ubi.h" |
| 46 | |
| 47 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 48 | static int paranoid_check_si(const struct ubi_device *ubi, |
| 49 | struct ubi_scan_info *si); |
| 50 | #else |
| 51 | #define paranoid_check_si(ubi, si) 0 |
| 52 | #endif |
| 53 | |
| 54 | /* Temporary variables used during scanning */ |
| 55 | static struct ubi_ec_hdr *ech; |
| 56 | static struct ubi_vid_hdr *vidh; |
| 57 | |
Artem Bityutskiy | 941dfb0 | 2007-05-05 16:33:13 +0300 | [diff] [blame] | 58 | /** |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 59 | * add_to_list - add physical eraseblock to a list. |
| 60 | * @si: scanning information |
| 61 | * @pnum: physical eraseblock number to add |
| 62 | * @ec: erase counter of the physical eraseblock |
| 63 | * @list: the list to add to |
| 64 | * |
| 65 | * This function adds physical eraseblock @pnum to free, erase, corrupted or |
| 66 | * alien lists. Returns zero in case of success and a negative error code in |
| 67 | * case of failure. |
| 68 | */ |
| 69 | static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, |
| 70 | struct list_head *list) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 71 | { |
| 72 | struct ubi_scan_leb *seb; |
| 73 | |
| 74 | if (list == &si->free) |
| 75 | dbg_bld("add to free: PEB %d, EC %d", pnum, ec); |
| 76 | else if (list == &si->erase) |
| 77 | dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); |
| 78 | else if (list == &si->corr) |
| 79 | dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); |
| 80 | else if (list == &si->alien) |
| 81 | dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); |
| 82 | else |
| 83 | BUG(); |
| 84 | |
| 85 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); |
| 86 | if (!seb) |
| 87 | return -ENOMEM; |
| 88 | |
| 89 | seb->pnum = pnum; |
| 90 | seb->ec = ec; |
| 91 | list_add_tail(&seb->u.list, list); |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * commit_to_mean_value - commit intermediate results to the final mean erase |
| 97 | * counter value. |
| 98 | * @si: scanning information |
| 99 | * |
| 100 | * This is a helper function which calculates partial mean erase counter mean |
| 101 | * value and adds it to the resulting mean value. As we can work only in |
| 102 | * integer arithmetic and we want to calculate the mean value of erase counter |
| 103 | * accurately, we first sum erase counter values in @si->ec_sum variable and |
| 104 | * count these components in @si->ec_count. If this temporary @si->ec_sum is |
| 105 | * going to overflow, we calculate the partial mean value |
| 106 | * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec. |
| 107 | */ |
| 108 | static void commit_to_mean_value(struct ubi_scan_info *si) |
| 109 | { |
| 110 | si->ec_sum /= si->ec_count; |
| 111 | if (si->ec_sum % si->ec_count >= si->ec_count / 2) |
| 112 | si->mean_ec += 1; |
| 113 | si->mean_ec += si->ec_sum; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * validate_vid_hdr - check that volume identifier header is correct and |
| 118 | * consistent. |
| 119 | * @vid_hdr: the volume identifier header to check |
| 120 | * @sv: information about the volume this logical eraseblock belongs to |
| 121 | * @pnum: physical eraseblock number the VID header came from |
| 122 | * |
| 123 | * This function checks that data stored in @vid_hdr is consistent. Returns |
| 124 | * non-zero if an inconsistency was found and zero if not. |
| 125 | * |
| 126 | * Note, UBI does sanity check of everything it reads from the flash media. |
| 127 | * Most of the checks are done in the I/O unit. Here we check that the |
| 128 | * information in the VID header is consistent to the information in other VID |
| 129 | * headers of the same volume. |
| 130 | */ |
| 131 | static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr, |
| 132 | const struct ubi_scan_volume *sv, int pnum) |
| 133 | { |
| 134 | int vol_type = vid_hdr->vol_type; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 135 | int vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 136 | int used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 137 | int data_pad = be32_to_cpu(vid_hdr->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 138 | |
| 139 | if (sv->leb_count != 0) { |
| 140 | int sv_vol_type; |
| 141 | |
| 142 | /* |
| 143 | * This is not the first logical eraseblock belonging to this |
| 144 | * volume. Ensure that the data in its VID header is consistent |
| 145 | * to the data in previous logical eraseblock headers. |
| 146 | */ |
| 147 | |
| 148 | if (vol_id != sv->vol_id) { |
| 149 | dbg_err("inconsistent vol_id"); |
| 150 | goto bad; |
| 151 | } |
| 152 | |
| 153 | if (sv->vol_type == UBI_STATIC_VOLUME) |
| 154 | sv_vol_type = UBI_VID_STATIC; |
| 155 | else |
| 156 | sv_vol_type = UBI_VID_DYNAMIC; |
| 157 | |
| 158 | if (vol_type != sv_vol_type) { |
| 159 | dbg_err("inconsistent vol_type"); |
| 160 | goto bad; |
| 161 | } |
| 162 | |
| 163 | if (used_ebs != sv->used_ebs) { |
| 164 | dbg_err("inconsistent used_ebs"); |
| 165 | goto bad; |
| 166 | } |
| 167 | |
| 168 | if (data_pad != sv->data_pad) { |
| 169 | dbg_err("inconsistent data_pad"); |
| 170 | goto bad; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | return 0; |
| 175 | |
| 176 | bad: |
| 177 | ubi_err("inconsistent VID header at PEB %d", pnum); |
| 178 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 179 | ubi_dbg_dump_sv(sv); |
| 180 | return -EINVAL; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * add_volume - add volume to the scanning information. |
| 185 | * @si: scanning information |
| 186 | * @vol_id: ID of the volume to add |
| 187 | * @pnum: physical eraseblock number |
| 188 | * @vid_hdr: volume identifier header |
| 189 | * |
| 190 | * If the volume corresponding to the @vid_hdr logical eraseblock is already |
| 191 | * present in the scanning information, this function does nothing. Otherwise |
| 192 | * it adds corresponding volume to the scanning information. Returns a pointer |
| 193 | * to the scanning volume object in case of success and a negative error code |
| 194 | * in case of failure. |
| 195 | */ |
| 196 | static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id, |
| 197 | int pnum, |
| 198 | const struct ubi_vid_hdr *vid_hdr) |
| 199 | { |
| 200 | struct ubi_scan_volume *sv; |
| 201 | struct rb_node **p = &si->volumes.rb_node, *parent = NULL; |
| 202 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 203 | ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 204 | |
| 205 | /* Walk the volume RB-tree to look if this volume is already present */ |
| 206 | while (*p) { |
| 207 | parent = *p; |
| 208 | sv = rb_entry(parent, struct ubi_scan_volume, rb); |
| 209 | |
| 210 | if (vol_id == sv->vol_id) |
| 211 | return sv; |
| 212 | |
| 213 | if (vol_id > sv->vol_id) |
| 214 | p = &(*p)->rb_left; |
| 215 | else |
| 216 | p = &(*p)->rb_right; |
| 217 | } |
| 218 | |
| 219 | /* The volume is absent - add it */ |
| 220 | sv = kmalloc(sizeof(struct ubi_scan_volume), GFP_KERNEL); |
| 221 | if (!sv) |
| 222 | return ERR_PTR(-ENOMEM); |
| 223 | |
| 224 | sv->highest_lnum = sv->leb_count = 0; |
| 225 | si->max_sqnum = 0; |
| 226 | sv->vol_id = vol_id; |
| 227 | sv->root = RB_ROOT; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 228 | sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs); |
| 229 | sv->data_pad = be32_to_cpu(vid_hdr->data_pad); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 230 | sv->compat = vid_hdr->compat; |
| 231 | sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME |
| 232 | : UBI_STATIC_VOLUME; |
| 233 | if (vol_id > si->highest_vol_id) |
| 234 | si->highest_vol_id = vol_id; |
| 235 | |
| 236 | rb_link_node(&sv->rb, parent, p); |
| 237 | rb_insert_color(&sv->rb, &si->volumes); |
| 238 | si->vols_found += 1; |
| 239 | dbg_bld("added volume %d", vol_id); |
| 240 | return sv; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * compare_lebs - find out which logical eraseblock is newer. |
| 245 | * @ubi: UBI device description object |
| 246 | * @seb: first logical eraseblock to compare |
| 247 | * @pnum: physical eraseblock number of the second logical eraseblock to |
| 248 | * compare |
| 249 | * @vid_hdr: volume identifier header of the second logical eraseblock |
| 250 | * |
| 251 | * This function compares 2 copies of a LEB and informs which one is newer. In |
| 252 | * case of success this function returns a positive value, in case of failure, a |
| 253 | * negative error code is returned. The success return codes use the following |
| 254 | * bits: |
| 255 | * o bit 0 is cleared: the first PEB (described by @seb) is newer then the |
| 256 | * second PEB (described by @pnum and @vid_hdr); |
| 257 | * o bit 0 is set: the second PEB is newer; |
| 258 | * o bit 1 is cleared: no bit-flips were detected in the newer LEB; |
| 259 | * o bit 1 is set: bit-flips were detected in the newer LEB; |
| 260 | * o bit 2 is cleared: the older LEB is not corrupted; |
| 261 | * o bit 2 is set: the older LEB is corrupted. |
| 262 | */ |
| 263 | static int compare_lebs(const struct ubi_device *ubi, |
| 264 | const struct ubi_scan_leb *seb, int pnum, |
| 265 | const struct ubi_vid_hdr *vid_hdr) |
| 266 | { |
| 267 | void *buf; |
| 268 | int len, err, second_is_newer, bitflips = 0, corrupted = 0; |
| 269 | uint32_t data_crc, crc; |
| 270 | struct ubi_vid_hdr *vidh = NULL; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 271 | unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 272 | |
| 273 | if (seb->sqnum == 0 && sqnum2 == 0) { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 274 | long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 275 | |
| 276 | /* |
| 277 | * UBI constantly increases the logical eraseblock version |
| 278 | * number and it can overflow. Thus, we have to bear in mind |
| 279 | * that versions that are close to %0xFFFFFFFF are less then |
| 280 | * versions that are close to %0. |
| 281 | * |
| 282 | * The UBI WL unit guarantees that the number of pending tasks |
| 283 | * is not greater then %0x7FFFFFFF. So, if the difference |
| 284 | * between any two versions is greater or equivalent to |
| 285 | * %0x7FFFFFFF, there was an overflow and the logical |
| 286 | * eraseblock with lower version is actually newer then the one |
| 287 | * with higher version. |
| 288 | * |
| 289 | * FIXME: but this is anyway obsolete and will be removed at |
| 290 | * some point. |
| 291 | */ |
| 292 | |
| 293 | dbg_bld("using old crappy leb_ver stuff"); |
| 294 | |
| 295 | abs = v1 - v2; |
| 296 | if (abs < 0) |
| 297 | abs = -abs; |
| 298 | |
| 299 | if (abs < 0x7FFFFFFF) |
| 300 | /* Non-overflow situation */ |
| 301 | second_is_newer = (v2 > v1); |
| 302 | else |
| 303 | second_is_newer = (v2 < v1); |
| 304 | } else |
| 305 | /* Obviously the LEB with lower sequence counter is older */ |
| 306 | second_is_newer = sqnum2 > seb->sqnum; |
| 307 | |
| 308 | /* |
| 309 | * Now we know which copy is newer. If the copy flag of the PEB with |
| 310 | * newer version is not set, then we just return, otherwise we have to |
| 311 | * check data CRC. For the second PEB we already have the VID header, |
| 312 | * for the first one - we'll need to re-read it from flash. |
| 313 | * |
| 314 | * FIXME: this may be optimized so that we wouldn't read twice. |
| 315 | */ |
| 316 | |
| 317 | if (second_is_newer) { |
| 318 | if (!vid_hdr->copy_flag) { |
| 319 | /* It is not a copy, so it is newer */ |
| 320 | dbg_bld("second PEB %d is newer, copy_flag is unset", |
| 321 | pnum); |
| 322 | return 1; |
| 323 | } |
| 324 | } else { |
| 325 | pnum = seb->pnum; |
| 326 | |
| 327 | vidh = ubi_zalloc_vid_hdr(ubi); |
| 328 | if (!vidh) |
| 329 | return -ENOMEM; |
| 330 | |
| 331 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); |
| 332 | if (err) { |
| 333 | if (err == UBI_IO_BITFLIPS) |
| 334 | bitflips = 1; |
| 335 | else { |
| 336 | dbg_err("VID of PEB %d header is bad, but it " |
| 337 | "was OK earlier", pnum); |
| 338 | if (err > 0) |
| 339 | err = -EIO; |
| 340 | |
| 341 | goto out_free_vidh; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (!vidh->copy_flag) { |
| 346 | /* It is not a copy, so it is newer */ |
| 347 | dbg_bld("first PEB %d is newer, copy_flag is unset", |
| 348 | pnum); |
| 349 | err = bitflips << 1; |
| 350 | goto out_free_vidh; |
| 351 | } |
| 352 | |
| 353 | vid_hdr = vidh; |
| 354 | } |
| 355 | |
| 356 | /* Read the data of the copy and check the CRC */ |
| 357 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 358 | len = be32_to_cpu(vid_hdr->data_size); |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 359 | buf = vmalloc(len); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 360 | if (!buf) { |
| 361 | err = -ENOMEM; |
| 362 | goto out_free_vidh; |
| 363 | } |
| 364 | |
| 365 | err = ubi_io_read_data(ubi, buf, pnum, 0, len); |
| 366 | if (err && err != UBI_IO_BITFLIPS) |
| 367 | goto out_free_buf; |
| 368 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 369 | data_crc = be32_to_cpu(vid_hdr->data_crc); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 370 | crc = crc32(UBI_CRC32_INIT, buf, len); |
| 371 | if (crc != data_crc) { |
| 372 | dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", |
| 373 | pnum, crc, data_crc); |
| 374 | corrupted = 1; |
| 375 | bitflips = 0; |
| 376 | second_is_newer = !second_is_newer; |
| 377 | } else { |
| 378 | dbg_bld("PEB %d CRC is OK", pnum); |
| 379 | bitflips = !!err; |
| 380 | } |
| 381 | |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 382 | vfree(buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 383 | ubi_free_vid_hdr(ubi, vidh); |
| 384 | |
| 385 | if (second_is_newer) |
| 386 | dbg_bld("second PEB %d is newer, copy_flag is set", pnum); |
| 387 | else |
| 388 | dbg_bld("first PEB %d is newer, copy_flag is set", pnum); |
| 389 | |
| 390 | return second_is_newer | (bitflips << 1) | (corrupted << 2); |
| 391 | |
| 392 | out_free_buf: |
Artem Bityutskiy | 92ad8f3 | 2007-05-06 16:12:54 +0300 | [diff] [blame] | 393 | vfree(buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 394 | out_free_vidh: |
| 395 | ubi_free_vid_hdr(ubi, vidh); |
| 396 | ubi_assert(err < 0); |
| 397 | return err; |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * ubi_scan_add_used - add information about a physical eraseblock to the |
| 402 | * scanning information. |
| 403 | * @ubi: UBI device description object |
| 404 | * @si: scanning information |
| 405 | * @pnum: the physical eraseblock number |
| 406 | * @ec: erase counter |
| 407 | * @vid_hdr: the volume identifier header |
| 408 | * @bitflips: if bit-flips were detected when this physical eraseblock was read |
| 409 | * |
Artem Bityutskiy | 79b510c | 2007-05-05 17:36:17 +0300 | [diff] [blame] | 410 | * This function adds information about a used physical eraseblock to the |
| 411 | * 'used' tree of the corresponding volume. The function is rather complex |
| 412 | * because it has to handle cases when this is not the first physical |
| 413 | * eraseblock belonging to the same logical eraseblock, and the newer one has |
| 414 | * to be picked, while the older one has to be dropped. This function returns |
| 415 | * 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] | 416 | */ |
| 417 | int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si, |
| 418 | int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, |
| 419 | int bitflips) |
| 420 | { |
| 421 | int err, vol_id, lnum; |
| 422 | uint32_t leb_ver; |
| 423 | unsigned long long sqnum; |
| 424 | struct ubi_scan_volume *sv; |
| 425 | struct ubi_scan_leb *seb; |
| 426 | struct rb_node **p, *parent = NULL; |
| 427 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 428 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 429 | lnum = be32_to_cpu(vid_hdr->lnum); |
| 430 | sqnum = be64_to_cpu(vid_hdr->sqnum); |
| 431 | leb_ver = be32_to_cpu(vid_hdr->leb_ver); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 432 | |
| 433 | dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d", |
| 434 | pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips); |
| 435 | |
| 436 | sv = add_volume(si, vol_id, pnum, vid_hdr); |
| 437 | if (IS_ERR(sv) < 0) |
| 438 | return PTR_ERR(sv); |
| 439 | |
Brijesh Singh | 76eafe4 | 2007-07-06 14:35:43 +0300 | [diff] [blame^] | 440 | if (si->max_sqnum < sqnum) |
| 441 | si->max_sqnum = sqnum; |
| 442 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 443 | /* |
| 444 | * Walk the RB-tree of logical eraseblocks of volume @vol_id to look |
| 445 | * if this is the first instance of this logical eraseblock or not. |
| 446 | */ |
| 447 | p = &sv->root.rb_node; |
| 448 | while (*p) { |
| 449 | int cmp_res; |
| 450 | |
| 451 | parent = *p; |
| 452 | seb = rb_entry(parent, struct ubi_scan_leb, u.rb); |
| 453 | if (lnum != seb->lnum) { |
| 454 | if (lnum < seb->lnum) |
| 455 | p = &(*p)->rb_left; |
| 456 | else |
| 457 | p = &(*p)->rb_right; |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | /* |
| 462 | * There is already a physical eraseblock describing the same |
| 463 | * logical eraseblock present. |
| 464 | */ |
| 465 | |
| 466 | dbg_bld("this LEB already exists: PEB %d, sqnum %llu, " |
| 467 | "LEB ver %u, EC %d", seb->pnum, seb->sqnum, |
| 468 | seb->leb_ver, seb->ec); |
| 469 | |
| 470 | /* |
| 471 | * Make sure that the logical eraseblocks have different |
| 472 | * versions. Otherwise the image is bad. |
| 473 | */ |
| 474 | if (seb->leb_ver == leb_ver && leb_ver != 0) { |
| 475 | ubi_err("two LEBs with same version %u", leb_ver); |
| 476 | ubi_dbg_dump_seb(seb, 0); |
| 477 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 478 | return -EINVAL; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Make sure that the logical eraseblocks have different |
| 483 | * sequence numbers. Otherwise the image is bad. |
| 484 | * |
| 485 | * FIXME: remove 'sqnum != 0' check when leb_ver is removed. |
| 486 | */ |
| 487 | if (seb->sqnum == sqnum && sqnum != 0) { |
| 488 | ubi_err("two LEBs with same sequence number %llu", |
| 489 | sqnum); |
| 490 | ubi_dbg_dump_seb(seb, 0); |
| 491 | ubi_dbg_dump_vid_hdr(vid_hdr); |
| 492 | return -EINVAL; |
| 493 | } |
| 494 | |
| 495 | /* |
| 496 | * Now we have to drop the older one and preserve the newer |
| 497 | * one. |
| 498 | */ |
| 499 | cmp_res = compare_lebs(ubi, seb, pnum, vid_hdr); |
| 500 | if (cmp_res < 0) |
| 501 | return cmp_res; |
| 502 | |
| 503 | if (cmp_res & 1) { |
| 504 | /* |
| 505 | * This logical eraseblock is newer then the one |
| 506 | * found earlier. |
| 507 | */ |
| 508 | err = validate_vid_hdr(vid_hdr, sv, pnum); |
| 509 | if (err) |
| 510 | return err; |
| 511 | |
| 512 | if (cmp_res & 4) |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 513 | err = add_to_list(si, seb->pnum, seb->ec, |
| 514 | &si->corr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 515 | else |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 516 | err = add_to_list(si, seb->pnum, seb->ec, |
| 517 | &si->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 518 | if (err) |
| 519 | return err; |
| 520 | |
| 521 | seb->ec = ec; |
| 522 | seb->pnum = pnum; |
| 523 | seb->scrub = ((cmp_res & 2) || bitflips); |
| 524 | seb->sqnum = sqnum; |
| 525 | seb->leb_ver = leb_ver; |
| 526 | |
| 527 | if (sv->highest_lnum == lnum) |
| 528 | sv->last_data_size = |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 529 | be32_to_cpu(vid_hdr->data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 530 | |
| 531 | return 0; |
| 532 | } else { |
| 533 | /* |
| 534 | * This logical eraseblock is older then the one found |
| 535 | * previously. |
| 536 | */ |
| 537 | if (cmp_res & 4) |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 538 | return add_to_list(si, pnum, ec, &si->corr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 539 | else |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 540 | return add_to_list(si, pnum, ec, &si->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * We've met this logical eraseblock for the first time, add it to the |
| 546 | * scanning information. |
| 547 | */ |
| 548 | |
| 549 | err = validate_vid_hdr(vid_hdr, sv, pnum); |
| 550 | if (err) |
| 551 | return err; |
| 552 | |
| 553 | seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); |
| 554 | if (!seb) |
| 555 | return -ENOMEM; |
| 556 | |
| 557 | seb->ec = ec; |
| 558 | seb->pnum = pnum; |
| 559 | seb->lnum = lnum; |
| 560 | seb->sqnum = sqnum; |
| 561 | seb->scrub = bitflips; |
| 562 | seb->leb_ver = leb_ver; |
| 563 | |
| 564 | if (sv->highest_lnum <= lnum) { |
| 565 | sv->highest_lnum = lnum; |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 566 | sv->last_data_size = be32_to_cpu(vid_hdr->data_size); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 567 | } |
| 568 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 569 | sv->leb_count += 1; |
| 570 | rb_link_node(&seb->u.rb, parent, p); |
| 571 | rb_insert_color(&seb->u.rb, &sv->root); |
| 572 | return 0; |
| 573 | } |
| 574 | |
| 575 | /** |
| 576 | * ubi_scan_find_sv - find information about a particular volume in the |
| 577 | * scanning information. |
| 578 | * @si: scanning information |
| 579 | * @vol_id: the requested volume ID |
| 580 | * |
| 581 | * This function returns a pointer to the volume description or %NULL if there |
| 582 | * are no data about this volume in the scanning information. |
| 583 | */ |
| 584 | struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si, |
| 585 | int vol_id) |
| 586 | { |
| 587 | struct ubi_scan_volume *sv; |
| 588 | struct rb_node *p = si->volumes.rb_node; |
| 589 | |
| 590 | while (p) { |
| 591 | sv = rb_entry(p, struct ubi_scan_volume, rb); |
| 592 | |
| 593 | if (vol_id == sv->vol_id) |
| 594 | return sv; |
| 595 | |
| 596 | if (vol_id > sv->vol_id) |
| 597 | p = p->rb_left; |
| 598 | else |
| 599 | p = p->rb_right; |
| 600 | } |
| 601 | |
| 602 | return NULL; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * ubi_scan_find_seb - find information about a particular logical |
| 607 | * eraseblock in the volume scanning information. |
| 608 | * @sv: a pointer to the volume scanning information |
| 609 | * @lnum: the requested logical eraseblock |
| 610 | * |
| 611 | * This function returns a pointer to the scanning logical eraseblock or %NULL |
| 612 | * if there are no data about it in the scanning volume information. |
| 613 | */ |
| 614 | struct ubi_scan_leb *ubi_scan_find_seb(const struct ubi_scan_volume *sv, |
| 615 | int lnum) |
| 616 | { |
| 617 | struct ubi_scan_leb *seb; |
| 618 | struct rb_node *p = sv->root.rb_node; |
| 619 | |
| 620 | while (p) { |
| 621 | seb = rb_entry(p, struct ubi_scan_leb, u.rb); |
| 622 | |
| 623 | if (lnum == seb->lnum) |
| 624 | return seb; |
| 625 | |
| 626 | if (lnum > seb->lnum) |
| 627 | p = p->rb_left; |
| 628 | else |
| 629 | p = p->rb_right; |
| 630 | } |
| 631 | |
| 632 | return NULL; |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * ubi_scan_rm_volume - delete scanning information about a volume. |
| 637 | * @si: scanning information |
| 638 | * @sv: the volume scanning information to delete |
| 639 | */ |
| 640 | void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv) |
| 641 | { |
| 642 | struct rb_node *rb; |
| 643 | struct ubi_scan_leb *seb; |
| 644 | |
| 645 | dbg_bld("remove scanning information about volume %d", sv->vol_id); |
| 646 | |
| 647 | while ((rb = rb_first(&sv->root))) { |
| 648 | seb = rb_entry(rb, struct ubi_scan_leb, u.rb); |
| 649 | rb_erase(&seb->u.rb, &sv->root); |
| 650 | list_add_tail(&seb->u.list, &si->erase); |
| 651 | } |
| 652 | |
| 653 | rb_erase(&sv->rb, &si->volumes); |
| 654 | kfree(sv); |
| 655 | si->vols_found -= 1; |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * ubi_scan_erase_peb - erase a physical eraseblock. |
| 660 | * @ubi: UBI device description object |
| 661 | * @si: scanning information |
| 662 | * @pnum: physical eraseblock number to erase; |
| 663 | * @ec: erase counter value to write (%UBI_SCAN_UNKNOWN_EC if it is unknown) |
| 664 | * |
| 665 | * This function erases physical eraseblock 'pnum', and writes the erase |
| 666 | * counter header to it. This function should only be used on UBI device |
| 667 | * initialization stages, when the EBA unit had not been yet initialized. This |
| 668 | * function returns zero in case of success and a negative error code in case |
| 669 | * of failure. |
| 670 | */ |
| 671 | int ubi_scan_erase_peb(const struct ubi_device *ubi, |
| 672 | const struct ubi_scan_info *si, int pnum, int ec) |
| 673 | { |
| 674 | int err; |
| 675 | struct ubi_ec_hdr *ec_hdr; |
| 676 | |
| 677 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 678 | if (!ec_hdr) |
| 679 | return -ENOMEM; |
| 680 | |
| 681 | if ((long long)ec >= UBI_MAX_ERASECOUNTER) { |
| 682 | /* |
| 683 | * Erase counter overflow. Upgrade UBI and use 64-bit |
| 684 | * erase counters internally. |
| 685 | */ |
| 686 | ubi_err("erase counter overflow at PEB %d, EC %d", pnum, ec); |
| 687 | return -EINVAL; |
| 688 | } |
| 689 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 690 | ec_hdr->ec = cpu_to_be64(ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 691 | |
| 692 | err = ubi_io_sync_erase(ubi, pnum, 0); |
| 693 | if (err < 0) |
| 694 | goto out_free; |
| 695 | |
| 696 | err = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); |
| 697 | |
| 698 | out_free: |
| 699 | kfree(ec_hdr); |
| 700 | return err; |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * ubi_scan_get_free_peb - get a free physical eraseblock. |
| 705 | * @ubi: UBI device description object |
| 706 | * @si: scanning information |
| 707 | * |
| 708 | * This function returns a free physical eraseblock. It is supposed to be |
| 709 | * called on the UBI initialization stages when the wear-leveling unit is not |
| 710 | * initialized yet. This function picks a physical eraseblocks from one of the |
| 711 | * lists, writes the EC header if it is needed, and removes it from the list. |
| 712 | * |
| 713 | * This function returns scanning physical eraseblock information in case of |
| 714 | * success and an error code in case of failure. |
| 715 | */ |
| 716 | struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi, |
| 717 | struct ubi_scan_info *si) |
| 718 | { |
| 719 | int err = 0, i; |
| 720 | struct ubi_scan_leb *seb; |
| 721 | |
| 722 | if (!list_empty(&si->free)) { |
| 723 | seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); |
| 724 | list_del(&seb->u.list); |
| 725 | dbg_bld("return free PEB %d, EC %d", seb->pnum, seb->ec); |
| 726 | return seb; |
| 727 | } |
| 728 | |
| 729 | for (i = 0; i < 2; i++) { |
| 730 | struct list_head *head; |
| 731 | struct ubi_scan_leb *tmp_seb; |
| 732 | |
| 733 | if (i == 0) |
| 734 | head = &si->erase; |
| 735 | else |
| 736 | head = &si->corr; |
| 737 | |
| 738 | /* |
| 739 | * We try to erase the first physical eraseblock from the @head |
| 740 | * list and pick it if we succeed, or try to erase the |
| 741 | * next one if not. And so forth. We don't want to take care |
| 742 | * about bad eraseblocks here - they'll be handled later. |
| 743 | */ |
| 744 | list_for_each_entry_safe(seb, tmp_seb, head, u.list) { |
| 745 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) |
| 746 | seb->ec = si->mean_ec; |
| 747 | |
| 748 | err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); |
| 749 | if (err) |
| 750 | continue; |
| 751 | |
| 752 | seb->ec += 1; |
| 753 | list_del(&seb->u.list); |
| 754 | dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); |
| 755 | return seb; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | ubi_err("no eraseblocks found"); |
| 760 | return ERR_PTR(-ENOSPC); |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * process_eb - read UBI headers, check them and add corresponding data |
| 765 | * to the scanning information. |
| 766 | * @ubi: UBI device description object |
| 767 | * @si: scanning information |
| 768 | * @pnum: the physical eraseblock number |
| 769 | * |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 770 | * This function returns a zero if the physical eraseblock was successfully |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 771 | * handled and a negative error code in case of failure. |
| 772 | */ |
| 773 | static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum) |
| 774 | { |
| 775 | long long ec; |
| 776 | int err, bitflips = 0, vol_id, ec_corr = 0; |
| 777 | |
| 778 | dbg_bld("scan PEB %d", pnum); |
| 779 | |
| 780 | /* Skip bad physical eraseblocks */ |
| 781 | err = ubi_io_is_bad(ubi, pnum); |
| 782 | if (err < 0) |
| 783 | return err; |
| 784 | else if (err) { |
| 785 | /* |
| 786 | * FIXME: this is actually duty of the I/O unit to initialize |
| 787 | * this, but MTD does not provide enough information. |
| 788 | */ |
| 789 | si->bad_peb_count += 1; |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); |
| 794 | if (err < 0) |
| 795 | return err; |
| 796 | else if (err == UBI_IO_BITFLIPS) |
| 797 | bitflips = 1; |
| 798 | else if (err == UBI_IO_PEB_EMPTY) |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 799 | return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 800 | else if (err == UBI_IO_BAD_EC_HDR) { |
| 801 | /* |
| 802 | * We have to also look at the VID header, possibly it is not |
| 803 | * corrupted. Set %bitflips flag in order to make this PEB be |
| 804 | * moved and EC be re-created. |
| 805 | */ |
| 806 | ec_corr = 1; |
| 807 | ec = UBI_SCAN_UNKNOWN_EC; |
| 808 | bitflips = 1; |
| 809 | } |
| 810 | |
| 811 | si->is_empty = 0; |
| 812 | |
| 813 | if (!ec_corr) { |
| 814 | /* Make sure UBI version is OK */ |
| 815 | if (ech->version != UBI_VERSION) { |
| 816 | ubi_err("this UBI version is %d, image version is %d", |
| 817 | UBI_VERSION, (int)ech->version); |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 821 | ec = be64_to_cpu(ech->ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 822 | if (ec > UBI_MAX_ERASECOUNTER) { |
| 823 | /* |
| 824 | * Erase counter overflow. The EC headers have 64 bits |
| 825 | * reserved, but we anyway make use of only 31 bit |
| 826 | * values, as this seems to be enough for any existing |
| 827 | * flash. Upgrade UBI and use 64-bit erase counters |
| 828 | * internally. |
| 829 | */ |
| 830 | ubi_err("erase counter overflow, max is %d", |
| 831 | UBI_MAX_ERASECOUNTER); |
| 832 | ubi_dbg_dump_ec_hdr(ech); |
| 833 | return -EINVAL; |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* OK, we've done with the EC header, let's look at the VID header */ |
| 838 | |
| 839 | err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); |
| 840 | if (err < 0) |
| 841 | return err; |
| 842 | else if (err == UBI_IO_BITFLIPS) |
| 843 | bitflips = 1; |
| 844 | else if (err == UBI_IO_BAD_VID_HDR || |
| 845 | (err == UBI_IO_PEB_FREE && ec_corr)) { |
| 846 | /* VID header is corrupted */ |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 847 | err = add_to_list(si, pnum, ec, &si->corr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 848 | if (err) |
| 849 | return err; |
| 850 | goto adjust_mean_ec; |
| 851 | } else if (err == UBI_IO_PEB_FREE) { |
| 852 | /* No VID header - the physical eraseblock is free */ |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 853 | err = add_to_list(si, pnum, ec, &si->free); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 854 | if (err) |
| 855 | return err; |
| 856 | goto adjust_mean_ec; |
| 857 | } |
| 858 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 859 | vol_id = be32_to_cpu(vidh->vol_id); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 860 | if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) { |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 861 | int lnum = be32_to_cpu(vidh->lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 862 | |
| 863 | /* Unsupported internal volume */ |
| 864 | switch (vidh->compat) { |
| 865 | case UBI_COMPAT_DELETE: |
| 866 | ubi_msg("\"delete\" compatible internal volume %d:%d" |
| 867 | " found, remove it", vol_id, lnum); |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 868 | err = add_to_list(si, pnum, ec, &si->corr); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 869 | if (err) |
| 870 | return err; |
| 871 | break; |
| 872 | |
| 873 | case UBI_COMPAT_RO: |
| 874 | ubi_msg("read-only compatible internal volume %d:%d" |
| 875 | " found, switch to read-only mode", |
| 876 | vol_id, lnum); |
| 877 | ubi->ro_mode = 1; |
| 878 | break; |
| 879 | |
| 880 | case UBI_COMPAT_PRESERVE: |
| 881 | ubi_msg("\"preserve\" compatible internal volume %d:%d" |
| 882 | " found", vol_id, lnum); |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 883 | err = add_to_list(si, pnum, ec, &si->alien); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 884 | if (err) |
| 885 | return err; |
| 886 | si->alien_peb_count += 1; |
| 887 | return 0; |
| 888 | |
| 889 | case UBI_COMPAT_REJECT: |
| 890 | ubi_err("incompatible internal volume %d:%d found", |
| 891 | vol_id, lnum); |
| 892 | return -EINVAL; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | /* Both UBI headers seem to be fine */ |
| 897 | err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); |
| 898 | if (err) |
| 899 | return err; |
| 900 | |
| 901 | adjust_mean_ec: |
| 902 | if (!ec_corr) { |
| 903 | if (si->ec_sum + ec < ec) { |
| 904 | commit_to_mean_value(si); |
| 905 | si->ec_sum = 0; |
| 906 | si->ec_count = 0; |
| 907 | } else { |
| 908 | si->ec_sum += ec; |
| 909 | si->ec_count += 1; |
| 910 | } |
| 911 | |
| 912 | if (ec > si->max_ec) |
| 913 | si->max_ec = ec; |
| 914 | if (ec < si->min_ec) |
| 915 | si->min_ec = ec; |
| 916 | } |
| 917 | |
| 918 | return 0; |
| 919 | } |
| 920 | |
| 921 | /** |
| 922 | * ubi_scan - scan an MTD device. |
| 923 | * @ubi: UBI device description object |
| 924 | * |
| 925 | * This function does full scanning of an MTD device and returns complete |
| 926 | * information about it. In case of failure, an error code is returned. |
| 927 | */ |
| 928 | struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) |
| 929 | { |
| 930 | int err, pnum; |
| 931 | struct rb_node *rb1, *rb2; |
| 932 | struct ubi_scan_volume *sv; |
| 933 | struct ubi_scan_leb *seb; |
| 934 | struct ubi_scan_info *si; |
| 935 | |
| 936 | si = kzalloc(sizeof(struct ubi_scan_info), GFP_KERNEL); |
| 937 | if (!si) |
| 938 | return ERR_PTR(-ENOMEM); |
| 939 | |
| 940 | INIT_LIST_HEAD(&si->corr); |
| 941 | INIT_LIST_HEAD(&si->free); |
| 942 | INIT_LIST_HEAD(&si->erase); |
| 943 | INIT_LIST_HEAD(&si->alien); |
| 944 | si->volumes = RB_ROOT; |
| 945 | si->is_empty = 1; |
| 946 | |
| 947 | err = -ENOMEM; |
| 948 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 949 | if (!ech) |
| 950 | goto out_si; |
| 951 | |
| 952 | vidh = ubi_zalloc_vid_hdr(ubi); |
| 953 | if (!vidh) |
| 954 | goto out_ech; |
| 955 | |
| 956 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
| 957 | cond_resched(); |
| 958 | |
| 959 | dbg_msg("process PEB %d", pnum); |
| 960 | err = process_eb(ubi, si, pnum); |
| 961 | if (err < 0) |
| 962 | goto out_vidh; |
| 963 | } |
| 964 | |
| 965 | dbg_msg("scanning is finished"); |
| 966 | |
| 967 | /* Finish mean erase counter calculations */ |
| 968 | if (si->ec_count) |
| 969 | commit_to_mean_value(si); |
| 970 | |
| 971 | if (si->is_empty) |
| 972 | ubi_msg("empty MTD device detected"); |
| 973 | |
| 974 | /* |
| 975 | * In case of unknown erase counter we use the mean erase counter |
| 976 | * value. |
| 977 | */ |
| 978 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { |
| 979 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) |
| 980 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) |
| 981 | seb->ec = si->mean_ec; |
| 982 | } |
| 983 | |
| 984 | list_for_each_entry(seb, &si->free, u.list) { |
| 985 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) |
| 986 | seb->ec = si->mean_ec; |
| 987 | } |
| 988 | |
| 989 | list_for_each_entry(seb, &si->corr, u.list) |
| 990 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) |
| 991 | seb->ec = si->mean_ec; |
| 992 | |
| 993 | list_for_each_entry(seb, &si->erase, u.list) |
| 994 | if (seb->ec == UBI_SCAN_UNKNOWN_EC) |
| 995 | seb->ec = si->mean_ec; |
| 996 | |
| 997 | err = paranoid_check_si(ubi, si); |
| 998 | if (err) { |
| 999 | if (err > 0) |
| 1000 | err = -EINVAL; |
| 1001 | goto out_vidh; |
| 1002 | } |
| 1003 | |
| 1004 | ubi_free_vid_hdr(ubi, vidh); |
| 1005 | kfree(ech); |
| 1006 | |
| 1007 | return si; |
| 1008 | |
| 1009 | out_vidh: |
| 1010 | ubi_free_vid_hdr(ubi, vidh); |
| 1011 | out_ech: |
| 1012 | kfree(ech); |
| 1013 | out_si: |
| 1014 | ubi_scan_destroy_si(si); |
| 1015 | return ERR_PTR(err); |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * destroy_sv - free the scanning volume information |
| 1020 | * @sv: scanning volume information |
| 1021 | * |
| 1022 | * This function destroys the volume RB-tree (@sv->root) and the scanning |
| 1023 | * volume information. |
| 1024 | */ |
| 1025 | static void destroy_sv(struct ubi_scan_volume *sv) |
| 1026 | { |
| 1027 | struct ubi_scan_leb *seb; |
| 1028 | struct rb_node *this = sv->root.rb_node; |
| 1029 | |
| 1030 | while (this) { |
| 1031 | if (this->rb_left) |
| 1032 | this = this->rb_left; |
| 1033 | else if (this->rb_right) |
| 1034 | this = this->rb_right; |
| 1035 | else { |
| 1036 | seb = rb_entry(this, struct ubi_scan_leb, u.rb); |
| 1037 | this = rb_parent(this); |
| 1038 | if (this) { |
| 1039 | if (this->rb_left == &seb->u.rb) |
| 1040 | this->rb_left = NULL; |
| 1041 | else |
| 1042 | this->rb_right = NULL; |
| 1043 | } |
| 1044 | |
| 1045 | kfree(seb); |
| 1046 | } |
| 1047 | } |
| 1048 | kfree(sv); |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * ubi_scan_destroy_si - destroy scanning information. |
| 1053 | * @si: scanning information |
| 1054 | */ |
| 1055 | void ubi_scan_destroy_si(struct ubi_scan_info *si) |
| 1056 | { |
| 1057 | struct ubi_scan_leb *seb, *seb_tmp; |
| 1058 | struct ubi_scan_volume *sv; |
| 1059 | struct rb_node *rb; |
| 1060 | |
| 1061 | list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { |
| 1062 | list_del(&seb->u.list); |
| 1063 | kfree(seb); |
| 1064 | } |
| 1065 | list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { |
| 1066 | list_del(&seb->u.list); |
| 1067 | kfree(seb); |
| 1068 | } |
| 1069 | list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { |
| 1070 | list_del(&seb->u.list); |
| 1071 | kfree(seb); |
| 1072 | } |
| 1073 | list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { |
| 1074 | list_del(&seb->u.list); |
| 1075 | kfree(seb); |
| 1076 | } |
| 1077 | |
| 1078 | /* Destroy the volume RB-tree */ |
| 1079 | rb = si->volumes.rb_node; |
| 1080 | while (rb) { |
| 1081 | if (rb->rb_left) |
| 1082 | rb = rb->rb_left; |
| 1083 | else if (rb->rb_right) |
| 1084 | rb = rb->rb_right; |
| 1085 | else { |
| 1086 | sv = rb_entry(rb, struct ubi_scan_volume, rb); |
| 1087 | |
| 1088 | rb = rb_parent(rb); |
| 1089 | if (rb) { |
| 1090 | if (rb->rb_left == &sv->rb) |
| 1091 | rb->rb_left = NULL; |
| 1092 | else |
| 1093 | rb->rb_right = NULL; |
| 1094 | } |
| 1095 | |
| 1096 | destroy_sv(sv); |
| 1097 | } |
| 1098 | } |
| 1099 | |
| 1100 | kfree(si); |
| 1101 | } |
| 1102 | |
| 1103 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 1104 | |
| 1105 | /** |
| 1106 | * paranoid_check_si - check if the scanning information is correct and |
| 1107 | * consistent. |
| 1108 | * @ubi: UBI device description object |
| 1109 | * @si: scanning information |
| 1110 | * |
| 1111 | * This function returns zero if the scanning information is all right, %1 if |
| 1112 | * not and a negative error code if an error occurred. |
| 1113 | */ |
| 1114 | static int paranoid_check_si(const struct ubi_device *ubi, |
| 1115 | struct ubi_scan_info *si) |
| 1116 | { |
| 1117 | int pnum, err, vols_found = 0; |
| 1118 | struct rb_node *rb1, *rb2; |
| 1119 | struct ubi_scan_volume *sv; |
| 1120 | struct ubi_scan_leb *seb, *last_seb; |
| 1121 | uint8_t *buf; |
| 1122 | |
| 1123 | /* |
Artem Bityutskiy | 78d87c9 | 2007-05-05 11:24:02 +0300 | [diff] [blame] | 1124 | * At first, check that scanning information is OK. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1125 | */ |
| 1126 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { |
| 1127 | int leb_count = 0; |
| 1128 | |
| 1129 | cond_resched(); |
| 1130 | |
| 1131 | vols_found += 1; |
| 1132 | |
| 1133 | if (si->is_empty) { |
| 1134 | ubi_err("bad is_empty flag"); |
| 1135 | goto bad_sv; |
| 1136 | } |
| 1137 | |
| 1138 | if (sv->vol_id < 0 || sv->highest_lnum < 0 || |
| 1139 | sv->leb_count < 0 || sv->vol_type < 0 || sv->used_ebs < 0 || |
| 1140 | sv->data_pad < 0 || sv->last_data_size < 0) { |
| 1141 | ubi_err("negative values"); |
| 1142 | goto bad_sv; |
| 1143 | } |
| 1144 | |
| 1145 | if (sv->vol_id >= UBI_MAX_VOLUMES && |
| 1146 | sv->vol_id < UBI_INTERNAL_VOL_START) { |
| 1147 | ubi_err("bad vol_id"); |
| 1148 | goto bad_sv; |
| 1149 | } |
| 1150 | |
| 1151 | if (sv->vol_id > si->highest_vol_id) { |
| 1152 | ubi_err("highest_vol_id is %d, but vol_id %d is there", |
| 1153 | si->highest_vol_id, sv->vol_id); |
| 1154 | goto out; |
| 1155 | } |
| 1156 | |
| 1157 | if (sv->vol_type != UBI_DYNAMIC_VOLUME && |
| 1158 | sv->vol_type != UBI_STATIC_VOLUME) { |
| 1159 | ubi_err("bad vol_type"); |
| 1160 | goto bad_sv; |
| 1161 | } |
| 1162 | |
| 1163 | if (sv->data_pad > ubi->leb_size / 2) { |
| 1164 | ubi_err("bad data_pad"); |
| 1165 | goto bad_sv; |
| 1166 | } |
| 1167 | |
| 1168 | last_seb = NULL; |
| 1169 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { |
| 1170 | cond_resched(); |
| 1171 | |
| 1172 | last_seb = seb; |
| 1173 | leb_count += 1; |
| 1174 | |
| 1175 | if (seb->pnum < 0 || seb->ec < 0) { |
| 1176 | ubi_err("negative values"); |
| 1177 | goto bad_seb; |
| 1178 | } |
| 1179 | |
| 1180 | if (seb->ec < si->min_ec) { |
| 1181 | ubi_err("bad si->min_ec (%d), %d found", |
| 1182 | si->min_ec, seb->ec); |
| 1183 | goto bad_seb; |
| 1184 | } |
| 1185 | |
| 1186 | if (seb->ec > si->max_ec) { |
| 1187 | ubi_err("bad si->max_ec (%d), %d found", |
| 1188 | si->max_ec, seb->ec); |
| 1189 | goto bad_seb; |
| 1190 | } |
| 1191 | |
| 1192 | if (seb->pnum >= ubi->peb_count) { |
| 1193 | ubi_err("too high PEB number %d, total PEBs %d", |
| 1194 | seb->pnum, ubi->peb_count); |
| 1195 | goto bad_seb; |
| 1196 | } |
| 1197 | |
| 1198 | if (sv->vol_type == UBI_STATIC_VOLUME) { |
| 1199 | if (seb->lnum >= sv->used_ebs) { |
| 1200 | ubi_err("bad lnum or used_ebs"); |
| 1201 | goto bad_seb; |
| 1202 | } |
| 1203 | } else { |
| 1204 | if (sv->used_ebs != 0) { |
| 1205 | ubi_err("non-zero used_ebs"); |
| 1206 | goto bad_seb; |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | if (seb->lnum > sv->highest_lnum) { |
| 1211 | ubi_err("incorrect highest_lnum or lnum"); |
| 1212 | goto bad_seb; |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | if (sv->leb_count != leb_count) { |
| 1217 | ubi_err("bad leb_count, %d objects in the tree", |
| 1218 | leb_count); |
| 1219 | goto bad_sv; |
| 1220 | } |
| 1221 | |
| 1222 | if (!last_seb) |
| 1223 | continue; |
| 1224 | |
| 1225 | seb = last_seb; |
| 1226 | |
| 1227 | if (seb->lnum != sv->highest_lnum) { |
| 1228 | ubi_err("bad highest_lnum"); |
| 1229 | goto bad_seb; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | if (vols_found != si->vols_found) { |
| 1234 | ubi_err("bad si->vols_found %d, should be %d", |
| 1235 | si->vols_found, vols_found); |
| 1236 | goto out; |
| 1237 | } |
| 1238 | |
| 1239 | /* Check that scanning information is correct */ |
| 1240 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { |
| 1241 | last_seb = NULL; |
| 1242 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { |
| 1243 | int vol_type; |
| 1244 | |
| 1245 | cond_resched(); |
| 1246 | |
| 1247 | last_seb = seb; |
| 1248 | |
| 1249 | err = ubi_io_read_vid_hdr(ubi, seb->pnum, vidh, 1); |
| 1250 | if (err && err != UBI_IO_BITFLIPS) { |
| 1251 | ubi_err("VID header is not OK (%d)", err); |
| 1252 | if (err > 0) |
| 1253 | err = -EIO; |
| 1254 | return err; |
| 1255 | } |
| 1256 | |
| 1257 | vol_type = vidh->vol_type == UBI_VID_DYNAMIC ? |
| 1258 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; |
| 1259 | if (sv->vol_type != vol_type) { |
| 1260 | ubi_err("bad vol_type"); |
| 1261 | goto bad_vid_hdr; |
| 1262 | } |
| 1263 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1264 | if (seb->sqnum != be64_to_cpu(vidh->sqnum)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1265 | ubi_err("bad sqnum %llu", seb->sqnum); |
| 1266 | goto bad_vid_hdr; |
| 1267 | } |
| 1268 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1269 | if (sv->vol_id != be32_to_cpu(vidh->vol_id)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1270 | ubi_err("bad vol_id %d", sv->vol_id); |
| 1271 | goto bad_vid_hdr; |
| 1272 | } |
| 1273 | |
| 1274 | if (sv->compat != vidh->compat) { |
| 1275 | ubi_err("bad compat %d", vidh->compat); |
| 1276 | goto bad_vid_hdr; |
| 1277 | } |
| 1278 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1279 | if (seb->lnum != be32_to_cpu(vidh->lnum)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1280 | ubi_err("bad lnum %d", seb->lnum); |
| 1281 | goto bad_vid_hdr; |
| 1282 | } |
| 1283 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1284 | if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1285 | ubi_err("bad used_ebs %d", sv->used_ebs); |
| 1286 | goto bad_vid_hdr; |
| 1287 | } |
| 1288 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1289 | if (sv->data_pad != be32_to_cpu(vidh->data_pad)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1290 | ubi_err("bad data_pad %d", sv->data_pad); |
| 1291 | goto bad_vid_hdr; |
| 1292 | } |
| 1293 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1294 | if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1295 | ubi_err("bad leb_ver %u", seb->leb_ver); |
| 1296 | goto bad_vid_hdr; |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | if (!last_seb) |
| 1301 | continue; |
| 1302 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1303 | if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1304 | ubi_err("bad highest_lnum %d", sv->highest_lnum); |
| 1305 | goto bad_vid_hdr; |
| 1306 | } |
| 1307 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1308 | if (sv->last_data_size != be32_to_cpu(vidh->data_size)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1309 | ubi_err("bad last_data_size %d", sv->last_data_size); |
| 1310 | goto bad_vid_hdr; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | /* |
| 1315 | * Make sure that all the physical eraseblocks are in one of the lists |
| 1316 | * or trees. |
| 1317 | */ |
| 1318 | buf = kmalloc(ubi->peb_count, GFP_KERNEL); |
| 1319 | if (!buf) |
| 1320 | return -ENOMEM; |
| 1321 | |
| 1322 | memset(buf, 1, ubi->peb_count); |
| 1323 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
| 1324 | err = ubi_io_is_bad(ubi, pnum); |
Artem Bityutskiy | 341e1a0 | 2007-05-03 11:59:51 +0300 | [diff] [blame] | 1325 | if (err < 0) { |
| 1326 | kfree(buf); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1327 | return err; |
Artem Bityutskiy | 341e1a0 | 2007-05-03 11:59:51 +0300 | [diff] [blame] | 1328 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1329 | else if (err) |
| 1330 | buf[pnum] = 0; |
| 1331 | } |
| 1332 | |
| 1333 | ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) |
| 1334 | ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) |
| 1335 | buf[seb->pnum] = 0; |
| 1336 | |
| 1337 | list_for_each_entry(seb, &si->free, u.list) |
| 1338 | buf[seb->pnum] = 0; |
| 1339 | |
| 1340 | list_for_each_entry(seb, &si->corr, u.list) |
| 1341 | buf[seb->pnum] = 0; |
| 1342 | |
| 1343 | list_for_each_entry(seb, &si->erase, u.list) |
| 1344 | buf[seb->pnum] = 0; |
| 1345 | |
| 1346 | list_for_each_entry(seb, &si->alien, u.list) |
| 1347 | buf[seb->pnum] = 0; |
| 1348 | |
| 1349 | err = 0; |
| 1350 | for (pnum = 0; pnum < ubi->peb_count; pnum++) |
| 1351 | if (buf[pnum]) { |
| 1352 | ubi_err("PEB %d is not referred", pnum); |
| 1353 | err = 1; |
| 1354 | } |
| 1355 | |
| 1356 | kfree(buf); |
| 1357 | if (err) |
| 1358 | goto out; |
| 1359 | return 0; |
| 1360 | |
| 1361 | bad_seb: |
| 1362 | ubi_err("bad scanning information about LEB %d", seb->lnum); |
| 1363 | ubi_dbg_dump_seb(seb, 0); |
| 1364 | ubi_dbg_dump_sv(sv); |
| 1365 | goto out; |
| 1366 | |
| 1367 | bad_sv: |
| 1368 | ubi_err("bad scanning information about volume %d", sv->vol_id); |
| 1369 | ubi_dbg_dump_sv(sv); |
| 1370 | goto out; |
| 1371 | |
| 1372 | bad_vid_hdr: |
| 1373 | ubi_err("bad scanning information about volume %d", sv->vol_id); |
| 1374 | ubi_dbg_dump_sv(sv); |
| 1375 | ubi_dbg_dump_vid_hdr(vidh); |
| 1376 | |
| 1377 | out: |
| 1378 | ubi_dbg_dump_stack(); |
| 1379 | return 1; |
| 1380 | } |
| 1381 | |
| 1382 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |