Kyungmin Park | c91a719 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) International Business Machines Corp., 2006 |
| 3 | * Copyright (c) Nokia Corporation, 2006, 2007 |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | c91a719 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 6 | * |
| 7 | * Author: Artem Bityutskiy (Битюцкий Артём) |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * This file includes volume table manipulation code. The volume table is an |
| 12 | * on-flash table containing volume meta-data like name, number of reserved |
| 13 | * physical eraseblocks, type, etc. The volume table is stored in the so-called |
| 14 | * "layout volume". |
| 15 | * |
| 16 | * The layout volume is an internal volume which is organized as follows. It |
| 17 | * consists of two logical eraseblocks - LEB 0 and LEB 1. Each logical |
| 18 | * eraseblock stores one volume table copy, i.e. LEB 0 and LEB 1 duplicate each |
| 19 | * other. This redundancy guarantees robustness to unclean reboots. The volume |
| 20 | * table is basically an array of volume table records. Each record contains |
| 21 | * full information about the volume and protected by a CRC checksum. |
| 22 | * |
| 23 | * The volume table is changed, it is first changed in RAM. Then LEB 0 is |
| 24 | * erased, and the updated volume table is written back to LEB 0. Then same for |
| 25 | * LEB 1. This scheme guarantees recoverability from unclean reboots. |
| 26 | * |
| 27 | * In this UBI implementation the on-flash volume table does not contain any |
| 28 | * information about how many data static volumes contain. This information may |
| 29 | * be found from the scanning data. |
| 30 | * |
| 31 | * But it would still be beneficial to store this information in the volume |
| 32 | * table. For example, suppose we have a static volume X, and all its physical |
| 33 | * eraseblocks became bad for some reasons. Suppose we are attaching the |
| 34 | * corresponding MTD device, the scanning has found no logical eraseblocks |
| 35 | * corresponding to the volume X. According to the volume table volume X does |
| 36 | * exist. So we don't know whether it is just empty or all its physical |
| 37 | * eraseblocks went bad. So we cannot alarm the user about this corruption. |
| 38 | * |
| 39 | * The volume table also stores so-called "update marker", which is used for |
| 40 | * volume updates. Before updating the volume, the update marker is set, and |
| 41 | * after the update operation is finished, the update marker is cleared. So if |
| 42 | * the update operation was interrupted (e.g. by an unclean reboot) - the |
| 43 | * update marker is still there and we know that the volume's contents is |
| 44 | * damaged. |
| 45 | */ |
| 46 | |
| 47 | #ifdef UBI_LINUX |
| 48 | #include <linux/crc32.h> |
| 49 | #include <linux/err.h> |
| 50 | #include <asm/div64.h> |
| 51 | #endif |
| 52 | |
| 53 | #include <ubi_uboot.h> |
| 54 | #include "ubi.h" |
| 55 | |
| 56 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 57 | static void paranoid_vtbl_check(const struct ubi_device *ubi); |
| 58 | #else |
| 59 | #define paranoid_vtbl_check(ubi) |
| 60 | #endif |
| 61 | |
| 62 | /* Empty volume table record */ |
| 63 | static struct ubi_vtbl_record empty_vtbl_record; |
| 64 | |
| 65 | /** |
| 66 | * ubi_change_vtbl_record - change volume table record. |
| 67 | * @ubi: UBI device description object |
| 68 | * @idx: table index to change |
| 69 | * @vtbl_rec: new volume table record |
| 70 | * |
| 71 | * This function changes volume table record @idx. If @vtbl_rec is %NULL, empty |
| 72 | * volume table record is written. The caller does not have to calculate CRC of |
| 73 | * the record as it is done by this function. Returns zero in case of success |
| 74 | * and a negative error code in case of failure. |
| 75 | */ |
| 76 | int ubi_change_vtbl_record(struct ubi_device *ubi, int idx, |
| 77 | struct ubi_vtbl_record *vtbl_rec) |
| 78 | { |
| 79 | int i, err; |
| 80 | uint32_t crc; |
| 81 | struct ubi_volume *layout_vol; |
| 82 | |
| 83 | ubi_assert(idx >= 0 && idx < ubi->vtbl_slots); |
| 84 | layout_vol = ubi->volumes[vol_id2idx(ubi, UBI_LAYOUT_VOLUME_ID)]; |
| 85 | |
| 86 | if (!vtbl_rec) |
| 87 | vtbl_rec = &empty_vtbl_record; |
| 88 | else { |
| 89 | crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC); |
| 90 | vtbl_rec->crc = cpu_to_be32(crc); |
| 91 | } |
| 92 | |
| 93 | memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); |
| 94 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
| 95 | err = ubi_eba_unmap_leb(ubi, layout_vol, i); |
| 96 | if (err) |
| 97 | return err; |
| 98 | |
| 99 | err = ubi_eba_write_leb(ubi, layout_vol, i, ubi->vtbl, 0, |
| 100 | ubi->vtbl_size, UBI_LONGTERM); |
| 101 | if (err) |
| 102 | return err; |
| 103 | } |
| 104 | |
| 105 | paranoid_vtbl_check(ubi); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * vtbl_check - check if volume table is not corrupted and contains sensible |
| 111 | * data. |
| 112 | * @ubi: UBI device description object |
| 113 | * @vtbl: volume table |
| 114 | * |
| 115 | * This function returns zero if @vtbl is all right, %1 if CRC is incorrect, |
| 116 | * and %-EINVAL if it contains inconsistent data. |
| 117 | */ |
| 118 | static int vtbl_check(const struct ubi_device *ubi, |
| 119 | const struct ubi_vtbl_record *vtbl) |
| 120 | { |
| 121 | int i, n, reserved_pebs, alignment, data_pad, vol_type, name_len; |
| 122 | int upd_marker, err; |
| 123 | uint32_t crc; |
| 124 | const char *name; |
| 125 | |
| 126 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 127 | cond_resched(); |
| 128 | |
| 129 | reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 130 | alignment = be32_to_cpu(vtbl[i].alignment); |
| 131 | data_pad = be32_to_cpu(vtbl[i].data_pad); |
| 132 | upd_marker = vtbl[i].upd_marker; |
| 133 | vol_type = vtbl[i].vol_type; |
| 134 | name_len = be16_to_cpu(vtbl[i].name_len); |
| 135 | name = (const char *) &vtbl[i].name[0]; |
| 136 | |
| 137 | crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC); |
| 138 | if (be32_to_cpu(vtbl[i].crc) != crc) { |
| 139 | ubi_err("bad CRC at record %u: %#08x, not %#08x", |
| 140 | i, crc, be32_to_cpu(vtbl[i].crc)); |
| 141 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 142 | return 1; |
| 143 | } |
| 144 | |
| 145 | if (reserved_pebs == 0) { |
| 146 | if (memcmp(&vtbl[i], &empty_vtbl_record, |
| 147 | UBI_VTBL_RECORD_SIZE)) { |
| 148 | err = 2; |
| 149 | goto bad; |
| 150 | } |
| 151 | continue; |
| 152 | } |
| 153 | |
| 154 | if (reserved_pebs < 0 || alignment < 0 || data_pad < 0 || |
| 155 | name_len < 0) { |
| 156 | err = 3; |
| 157 | goto bad; |
| 158 | } |
| 159 | |
| 160 | if (alignment > ubi->leb_size || alignment == 0) { |
| 161 | err = 4; |
| 162 | goto bad; |
| 163 | } |
| 164 | |
| 165 | n = alignment & (ubi->min_io_size - 1); |
| 166 | if (alignment != 1 && n) { |
| 167 | err = 5; |
| 168 | goto bad; |
| 169 | } |
| 170 | |
| 171 | n = ubi->leb_size % alignment; |
| 172 | if (data_pad != n) { |
| 173 | dbg_err("bad data_pad, has to be %d", n); |
| 174 | err = 6; |
| 175 | goto bad; |
| 176 | } |
| 177 | |
| 178 | if (vol_type != UBI_VID_DYNAMIC && vol_type != UBI_VID_STATIC) { |
| 179 | err = 7; |
| 180 | goto bad; |
| 181 | } |
| 182 | |
| 183 | if (upd_marker != 0 && upd_marker != 1) { |
| 184 | err = 8; |
| 185 | goto bad; |
| 186 | } |
| 187 | |
| 188 | if (reserved_pebs > ubi->good_peb_count) { |
| 189 | dbg_err("too large reserved_pebs, good PEBs %d", |
| 190 | ubi->good_peb_count); |
| 191 | err = 9; |
| 192 | goto bad; |
| 193 | } |
| 194 | |
| 195 | if (name_len > UBI_VOL_NAME_MAX) { |
| 196 | err = 10; |
| 197 | goto bad; |
| 198 | } |
| 199 | |
| 200 | if (name[0] == '\0') { |
| 201 | err = 11; |
| 202 | goto bad; |
| 203 | } |
| 204 | |
| 205 | if (name_len != strnlen(name, name_len + 1)) { |
| 206 | err = 12; |
| 207 | goto bad; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /* Checks that all names are unique */ |
| 212 | for (i = 0; i < ubi->vtbl_slots - 1; i++) { |
| 213 | for (n = i + 1; n < ubi->vtbl_slots; n++) { |
| 214 | int len1 = be16_to_cpu(vtbl[i].name_len); |
| 215 | int len2 = be16_to_cpu(vtbl[n].name_len); |
| 216 | |
| 217 | if (len1 > 0 && len1 == len2 && |
| 218 | !strncmp((char *)vtbl[i].name, (char *)vtbl[n].name, len1)) { |
| 219 | ubi_err("volumes %d and %d have the same name" |
| 220 | " \"%s\"", i, n, vtbl[i].name); |
| 221 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 222 | ubi_dbg_dump_vtbl_record(&vtbl[n], n); |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | |
| 230 | bad: |
| 231 | ubi_err("volume table check failed: record %d, error %d", i, err); |
| 232 | ubi_dbg_dump_vtbl_record(&vtbl[i], i); |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * create_vtbl - create a copy of volume table. |
| 238 | * @ubi: UBI device description object |
| 239 | * @si: scanning information |
| 240 | * @copy: number of the volume table copy |
| 241 | * @vtbl: contents of the volume table |
| 242 | * |
| 243 | * This function returns zero in case of success and a negative error code in |
| 244 | * case of failure. |
| 245 | */ |
| 246 | static int create_vtbl(struct ubi_device *ubi, struct ubi_scan_info *si, |
| 247 | int copy, void *vtbl) |
| 248 | { |
| 249 | int err, tries = 0; |
| 250 | static struct ubi_vid_hdr *vid_hdr; |
| 251 | struct ubi_scan_volume *sv; |
| 252 | struct ubi_scan_leb *new_seb, *old_seb = NULL; |
| 253 | |
| 254 | ubi_msg("create volume table (copy #%d)", copy + 1); |
| 255 | |
| 256 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
| 257 | if (!vid_hdr) |
| 258 | return -ENOMEM; |
| 259 | |
| 260 | /* |
| 261 | * Check if there is a logical eraseblock which would have to contain |
| 262 | * this volume table copy was found during scanning. It has to be wiped |
| 263 | * out. |
| 264 | */ |
| 265 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID); |
| 266 | if (sv) |
| 267 | old_seb = ubi_scan_find_seb(sv, copy); |
| 268 | |
| 269 | retry: |
| 270 | new_seb = ubi_scan_get_free_peb(ubi, si); |
| 271 | if (IS_ERR(new_seb)) { |
| 272 | err = PTR_ERR(new_seb); |
| 273 | goto out_free; |
| 274 | } |
| 275 | |
| 276 | vid_hdr->vol_type = UBI_VID_DYNAMIC; |
| 277 | vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOLUME_ID); |
| 278 | vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; |
| 279 | vid_hdr->data_size = vid_hdr->used_ebs = |
| 280 | vid_hdr->data_pad = cpu_to_be32(0); |
| 281 | vid_hdr->lnum = cpu_to_be32(copy); |
| 282 | vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum); |
| 283 | vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0); |
| 284 | |
| 285 | /* The EC header is already there, write the VID header */ |
| 286 | err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); |
| 287 | if (err) |
| 288 | goto write_error; |
| 289 | |
| 290 | /* Write the layout volume contents */ |
| 291 | err = ubi_io_write_data(ubi, vtbl, new_seb->pnum, 0, ubi->vtbl_size); |
| 292 | if (err) |
| 293 | goto write_error; |
| 294 | |
| 295 | /* |
| 296 | * And add it to the scanning information. Don't delete the old |
| 297 | * @old_seb as it will be deleted and freed in 'ubi_scan_add_used()'. |
| 298 | */ |
| 299 | err = ubi_scan_add_used(ubi, si, new_seb->pnum, new_seb->ec, |
| 300 | vid_hdr, 0); |
| 301 | kfree(new_seb); |
| 302 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 303 | return err; |
| 304 | |
| 305 | write_error: |
| 306 | if (err == -EIO && ++tries <= 5) { |
| 307 | /* |
| 308 | * Probably this physical eraseblock went bad, try to pick |
| 309 | * another one. |
| 310 | */ |
| 311 | list_add_tail(&new_seb->u.list, &si->corr); |
| 312 | goto retry; |
| 313 | } |
| 314 | kfree(new_seb); |
| 315 | out_free: |
| 316 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 317 | return err; |
| 318 | |
| 319 | } |
| 320 | |
| 321 | /** |
| 322 | * process_lvol - process the layout volume. |
| 323 | * @ubi: UBI device description object |
| 324 | * @si: scanning information |
| 325 | * @sv: layout volume scanning information |
| 326 | * |
| 327 | * This function is responsible for reading the layout volume, ensuring it is |
| 328 | * not corrupted, and recovering from corruptions if needed. Returns volume |
| 329 | * table in case of success and a negative error code in case of failure. |
| 330 | */ |
| 331 | static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi, |
| 332 | struct ubi_scan_info *si, |
| 333 | struct ubi_scan_volume *sv) |
| 334 | { |
| 335 | int err; |
| 336 | struct rb_node *rb; |
| 337 | struct ubi_scan_leb *seb; |
| 338 | struct ubi_vtbl_record *leb[UBI_LAYOUT_VOLUME_EBS] = { NULL, NULL }; |
| 339 | int leb_corrupted[UBI_LAYOUT_VOLUME_EBS] = {1, 1}; |
| 340 | |
| 341 | /* |
| 342 | * UBI goes through the following steps when it changes the layout |
| 343 | * volume: |
| 344 | * a. erase LEB 0; |
| 345 | * b. write new data to LEB 0; |
| 346 | * c. erase LEB 1; |
| 347 | * d. write new data to LEB 1. |
| 348 | * |
| 349 | * Before the change, both LEBs contain the same data. |
| 350 | * |
| 351 | * Due to unclean reboots, the contents of LEB 0 may be lost, but there |
| 352 | * should LEB 1. So it is OK if LEB 0 is corrupted while LEB 1 is not. |
| 353 | * Similarly, LEB 1 may be lost, but there should be LEB 0. And |
| 354 | * finally, unclean reboots may result in a situation when neither LEB |
| 355 | * 0 nor LEB 1 are corrupted, but they are different. In this case, LEB |
| 356 | * 0 contains more recent information. |
| 357 | * |
| 358 | * So the plan is to first check LEB 0. Then |
| 359 | * a. if LEB 0 is OK, it must be containing the most resent data; then |
| 360 | * we compare it with LEB 1, and if they are different, we copy LEB |
| 361 | * 0 to LEB 1; |
| 362 | * b. if LEB 0 is corrupted, but LEB 1 has to be OK, and we copy LEB 1 |
| 363 | * to LEB 0. |
| 364 | */ |
| 365 | |
| 366 | dbg_msg("check layout volume"); |
| 367 | |
| 368 | /* Read both LEB 0 and LEB 1 into memory */ |
| 369 | ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { |
| 370 | leb[seb->lnum] = vmalloc(ubi->vtbl_size); |
| 371 | if (!leb[seb->lnum]) { |
| 372 | err = -ENOMEM; |
| 373 | goto out_free; |
| 374 | } |
| 375 | memset(leb[seb->lnum], 0, ubi->vtbl_size); |
| 376 | |
| 377 | err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, |
| 378 | ubi->vtbl_size); |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 379 | if (err == UBI_IO_BITFLIPS || mtd_is_eccerr(err)) |
Kyungmin Park | c91a719 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 380 | /* |
| 381 | * Scrub the PEB later. Note, -EBADMSG indicates an |
| 382 | * uncorrectable ECC error, but we have our own CRC and |
| 383 | * the data will be checked later. If the data is OK, |
| 384 | * the PEB will be scrubbed (because we set |
| 385 | * seb->scrub). If the data is not OK, the contents of |
| 386 | * the PEB will be recovered from the second copy, and |
| 387 | * seb->scrub will be cleared in |
| 388 | * 'ubi_scan_add_used()'. |
| 389 | */ |
| 390 | seb->scrub = 1; |
| 391 | else if (err) |
| 392 | goto out_free; |
| 393 | } |
| 394 | |
| 395 | err = -EINVAL; |
| 396 | if (leb[0]) { |
| 397 | leb_corrupted[0] = vtbl_check(ubi, leb[0]); |
| 398 | if (leb_corrupted[0] < 0) |
| 399 | goto out_free; |
| 400 | } |
| 401 | |
| 402 | if (!leb_corrupted[0]) { |
| 403 | /* LEB 0 is OK */ |
| 404 | if (leb[1]) |
| 405 | leb_corrupted[1] = memcmp(leb[0], leb[1], ubi->vtbl_size); |
| 406 | if (leb_corrupted[1]) { |
| 407 | ubi_warn("volume table copy #2 is corrupted"); |
| 408 | err = create_vtbl(ubi, si, 1, leb[0]); |
| 409 | if (err) |
| 410 | goto out_free; |
| 411 | ubi_msg("volume table was restored"); |
| 412 | } |
| 413 | |
| 414 | /* Both LEB 1 and LEB 2 are OK and consistent */ |
| 415 | vfree(leb[1]); |
| 416 | return leb[0]; |
| 417 | } else { |
| 418 | /* LEB 0 is corrupted or does not exist */ |
| 419 | if (leb[1]) { |
| 420 | leb_corrupted[1] = vtbl_check(ubi, leb[1]); |
| 421 | if (leb_corrupted[1] < 0) |
| 422 | goto out_free; |
| 423 | } |
| 424 | if (leb_corrupted[1]) { |
| 425 | /* Both LEB 0 and LEB 1 are corrupted */ |
| 426 | ubi_err("both volume tables are corrupted"); |
| 427 | goto out_free; |
| 428 | } |
| 429 | |
| 430 | ubi_warn("volume table copy #1 is corrupted"); |
| 431 | err = create_vtbl(ubi, si, 0, leb[1]); |
| 432 | if (err) |
| 433 | goto out_free; |
| 434 | ubi_msg("volume table was restored"); |
| 435 | |
| 436 | vfree(leb[0]); |
| 437 | return leb[1]; |
| 438 | } |
| 439 | |
| 440 | out_free: |
| 441 | vfree(leb[0]); |
| 442 | vfree(leb[1]); |
| 443 | return ERR_PTR(err); |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * create_empty_lvol - create empty layout volume. |
| 448 | * @ubi: UBI device description object |
| 449 | * @si: scanning information |
| 450 | * |
| 451 | * This function returns volume table contents in case of success and a |
| 452 | * negative error code in case of failure. |
| 453 | */ |
| 454 | static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi, |
| 455 | struct ubi_scan_info *si) |
| 456 | { |
| 457 | int i; |
| 458 | struct ubi_vtbl_record *vtbl; |
| 459 | |
| 460 | vtbl = vmalloc(ubi->vtbl_size); |
| 461 | if (!vtbl) |
| 462 | return ERR_PTR(-ENOMEM); |
| 463 | memset(vtbl, 0, ubi->vtbl_size); |
| 464 | |
| 465 | for (i = 0; i < ubi->vtbl_slots; i++) |
| 466 | memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); |
| 467 | |
| 468 | for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { |
| 469 | int err; |
| 470 | |
| 471 | err = create_vtbl(ubi, si, i, vtbl); |
| 472 | if (err) { |
| 473 | vfree(vtbl); |
| 474 | return ERR_PTR(err); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | return vtbl; |
| 479 | } |
| 480 | |
| 481 | /** |
| 482 | * init_volumes - initialize volume information for existing volumes. |
| 483 | * @ubi: UBI device description object |
| 484 | * @si: scanning information |
| 485 | * @vtbl: volume table |
| 486 | * |
| 487 | * This function allocates volume description objects for existing volumes. |
| 488 | * Returns zero in case of success and a negative error code in case of |
| 489 | * failure. |
| 490 | */ |
| 491 | static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si, |
| 492 | const struct ubi_vtbl_record *vtbl) |
| 493 | { |
| 494 | int i, reserved_pebs = 0; |
| 495 | struct ubi_scan_volume *sv; |
| 496 | struct ubi_volume *vol; |
| 497 | |
| 498 | for (i = 0; i < ubi->vtbl_slots; i++) { |
| 499 | cond_resched(); |
| 500 | |
| 501 | if (be32_to_cpu(vtbl[i].reserved_pebs) == 0) |
| 502 | continue; /* Empty record */ |
| 503 | |
| 504 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 505 | if (!vol) |
| 506 | return -ENOMEM; |
| 507 | |
| 508 | vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs); |
| 509 | vol->alignment = be32_to_cpu(vtbl[i].alignment); |
| 510 | vol->data_pad = be32_to_cpu(vtbl[i].data_pad); |
Peter Horton | ceeba00 | 2010-06-12 10:11:56 +0900 | [diff] [blame] | 511 | vol->upd_marker = vtbl[i].upd_marker; |
Kyungmin Park | c91a719 | 2008-11-19 16:28:06 +0100 | [diff] [blame] | 512 | vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? |
| 513 | UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; |
| 514 | vol->name_len = be16_to_cpu(vtbl[i].name_len); |
| 515 | vol->usable_leb_size = ubi->leb_size - vol->data_pad; |
| 516 | memcpy(vol->name, vtbl[i].name, vol->name_len); |
| 517 | vol->name[vol->name_len] = '\0'; |
| 518 | vol->vol_id = i; |
| 519 | |
| 520 | if (vtbl[i].flags & UBI_VTBL_AUTORESIZE_FLG) { |
| 521 | /* Auto re-size flag may be set only for one volume */ |
| 522 | if (ubi->autoresize_vol_id != -1) { |
| 523 | ubi_err("more then one auto-resize volume (%d " |
| 524 | "and %d)", ubi->autoresize_vol_id, i); |
| 525 | kfree(vol); |
| 526 | return -EINVAL; |
| 527 | } |
| 528 | |
| 529 | ubi->autoresize_vol_id = i; |
| 530 | } |
| 531 | |
| 532 | ubi_assert(!ubi->volumes[i]); |
| 533 | ubi->volumes[i] = vol; |
| 534 | ubi->vol_count += 1; |
| 535 | vol->ubi = ubi; |
| 536 | reserved_pebs += vol->reserved_pebs; |
| 537 | |
| 538 | /* |
| 539 | * In case of dynamic volume UBI knows nothing about how many |
| 540 | * data is stored there. So assume the whole volume is used. |
| 541 | */ |
| 542 | if (vol->vol_type == UBI_DYNAMIC_VOLUME) { |
| 543 | vol->used_ebs = vol->reserved_pebs; |
| 544 | vol->last_eb_bytes = vol->usable_leb_size; |
| 545 | vol->used_bytes = |
| 546 | (long long)vol->used_ebs * vol->usable_leb_size; |
| 547 | continue; |
| 548 | } |
| 549 | |
| 550 | /* Static volumes only */ |
| 551 | sv = ubi_scan_find_sv(si, i); |
| 552 | if (!sv) { |
| 553 | /* |
| 554 | * No eraseblocks belonging to this volume found. We |
| 555 | * don't actually know whether this static volume is |
| 556 | * completely corrupted or just contains no data. And |
| 557 | * we cannot know this as long as data size is not |
| 558 | * stored on flash. So we just assume the volume is |
| 559 | * empty. FIXME: this should be handled. |
| 560 | */ |
| 561 | continue; |
| 562 | } |
| 563 | |
| 564 | if (sv->leb_count != sv->used_ebs) { |
| 565 | /* |
| 566 | * We found a static volume which misses several |
| 567 | * eraseblocks. Treat it as corrupted. |
| 568 | */ |
| 569 | ubi_warn("static volume %d misses %d LEBs - corrupted", |
| 570 | sv->vol_id, sv->used_ebs - sv->leb_count); |
| 571 | vol->corrupted = 1; |
| 572 | continue; |
| 573 | } |
| 574 | |
| 575 | vol->used_ebs = sv->used_ebs; |
| 576 | vol->used_bytes = |
| 577 | (long long)(vol->used_ebs - 1) * vol->usable_leb_size; |
| 578 | vol->used_bytes += sv->last_data_size; |
| 579 | vol->last_eb_bytes = sv->last_data_size; |
| 580 | } |
| 581 | |
| 582 | /* And add the layout volume */ |
| 583 | vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); |
| 584 | if (!vol) |
| 585 | return -ENOMEM; |
| 586 | |
| 587 | vol->reserved_pebs = UBI_LAYOUT_VOLUME_EBS; |
| 588 | vol->alignment = 1; |
| 589 | vol->vol_type = UBI_DYNAMIC_VOLUME; |
| 590 | vol->name_len = sizeof(UBI_LAYOUT_VOLUME_NAME) - 1; |
| 591 | memcpy(vol->name, UBI_LAYOUT_VOLUME_NAME, vol->name_len + 1); |
| 592 | vol->usable_leb_size = ubi->leb_size; |
| 593 | vol->used_ebs = vol->reserved_pebs; |
| 594 | vol->last_eb_bytes = vol->reserved_pebs; |
| 595 | vol->used_bytes = |
| 596 | (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad); |
| 597 | vol->vol_id = UBI_LAYOUT_VOLUME_ID; |
| 598 | vol->ref_count = 1; |
| 599 | |
| 600 | ubi_assert(!ubi->volumes[i]); |
| 601 | ubi->volumes[vol_id2idx(ubi, vol->vol_id)] = vol; |
| 602 | reserved_pebs += vol->reserved_pebs; |
| 603 | ubi->vol_count += 1; |
| 604 | vol->ubi = ubi; |
| 605 | |
| 606 | if (reserved_pebs > ubi->avail_pebs) |
| 607 | ubi_err("not enough PEBs, required %d, available %d", |
| 608 | reserved_pebs, ubi->avail_pebs); |
| 609 | ubi->rsvd_pebs += reserved_pebs; |
| 610 | ubi->avail_pebs -= reserved_pebs; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * check_sv - check volume scanning information. |
| 617 | * @vol: UBI volume description object |
| 618 | * @sv: volume scanning information |
| 619 | * |
| 620 | * This function returns zero if the volume scanning information is consistent |
| 621 | * to the data read from the volume tabla, and %-EINVAL if not. |
| 622 | */ |
| 623 | static int check_sv(const struct ubi_volume *vol, |
| 624 | const struct ubi_scan_volume *sv) |
| 625 | { |
| 626 | int err; |
| 627 | |
| 628 | if (sv->highest_lnum >= vol->reserved_pebs) { |
| 629 | err = 1; |
| 630 | goto bad; |
| 631 | } |
| 632 | if (sv->leb_count > vol->reserved_pebs) { |
| 633 | err = 2; |
| 634 | goto bad; |
| 635 | } |
| 636 | if (sv->vol_type != vol->vol_type) { |
| 637 | err = 3; |
| 638 | goto bad; |
| 639 | } |
| 640 | if (sv->used_ebs > vol->reserved_pebs) { |
| 641 | err = 4; |
| 642 | goto bad; |
| 643 | } |
| 644 | if (sv->data_pad != vol->data_pad) { |
| 645 | err = 5; |
| 646 | goto bad; |
| 647 | } |
| 648 | return 0; |
| 649 | |
| 650 | bad: |
| 651 | ubi_err("bad scanning information, error %d", err); |
| 652 | ubi_dbg_dump_sv(sv); |
| 653 | ubi_dbg_dump_vol_info(vol); |
| 654 | return -EINVAL; |
| 655 | } |
| 656 | |
| 657 | /** |
| 658 | * check_scanning_info - check that scanning information. |
| 659 | * @ubi: UBI device description object |
| 660 | * @si: scanning information |
| 661 | * |
| 662 | * Even though we protect on-flash data by CRC checksums, we still don't trust |
| 663 | * the media. This function ensures that scanning information is consistent to |
| 664 | * the information read from the volume table. Returns zero if the scanning |
| 665 | * information is OK and %-EINVAL if it is not. |
| 666 | */ |
| 667 | static int check_scanning_info(const struct ubi_device *ubi, |
| 668 | struct ubi_scan_info *si) |
| 669 | { |
| 670 | int err, i; |
| 671 | struct ubi_scan_volume *sv; |
| 672 | struct ubi_volume *vol; |
| 673 | |
| 674 | if (si->vols_found > UBI_INT_VOL_COUNT + ubi->vtbl_slots) { |
| 675 | ubi_err("scanning found %d volumes, maximum is %d + %d", |
| 676 | si->vols_found, UBI_INT_VOL_COUNT, ubi->vtbl_slots); |
| 677 | return -EINVAL; |
| 678 | } |
| 679 | |
| 680 | if (si->highest_vol_id >= ubi->vtbl_slots + UBI_INT_VOL_COUNT && |
| 681 | si->highest_vol_id < UBI_INTERNAL_VOL_START) { |
| 682 | ubi_err("too large volume ID %d found by scanning", |
| 683 | si->highest_vol_id); |
| 684 | return -EINVAL; |
| 685 | } |
| 686 | |
| 687 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) { |
| 688 | cond_resched(); |
| 689 | |
| 690 | sv = ubi_scan_find_sv(si, i); |
| 691 | vol = ubi->volumes[i]; |
| 692 | if (!vol) { |
| 693 | if (sv) |
| 694 | ubi_scan_rm_volume(si, sv); |
| 695 | continue; |
| 696 | } |
| 697 | |
| 698 | if (vol->reserved_pebs == 0) { |
| 699 | ubi_assert(i < ubi->vtbl_slots); |
| 700 | |
| 701 | if (!sv) |
| 702 | continue; |
| 703 | |
| 704 | /* |
| 705 | * During scanning we found a volume which does not |
| 706 | * exist according to the information in the volume |
| 707 | * table. This must have happened due to an unclean |
| 708 | * reboot while the volume was being removed. Discard |
| 709 | * these eraseblocks. |
| 710 | */ |
| 711 | ubi_msg("finish volume %d removal", sv->vol_id); |
| 712 | ubi_scan_rm_volume(si, sv); |
| 713 | } else if (sv) { |
| 714 | err = check_sv(vol, sv); |
| 715 | if (err) |
| 716 | return err; |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | return 0; |
| 721 | } |
| 722 | |
| 723 | /** |
| 724 | * ubi_read_volume_table - read volume table. |
| 725 | * information. |
| 726 | * @ubi: UBI device description object |
| 727 | * @si: scanning information |
| 728 | * |
| 729 | * This function reads volume table, checks it, recover from errors if needed, |
| 730 | * or creates it if needed. Returns zero in case of success and a negative |
| 731 | * error code in case of failure. |
| 732 | */ |
| 733 | int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si) |
| 734 | { |
| 735 | int i, err; |
| 736 | struct ubi_scan_volume *sv; |
| 737 | |
| 738 | empty_vtbl_record.crc = cpu_to_be32(0xf116c36b); |
| 739 | |
| 740 | /* |
| 741 | * The number of supported volumes is limited by the eraseblock size |
| 742 | * and by the UBI_MAX_VOLUMES constant. |
| 743 | */ |
| 744 | ubi->vtbl_slots = ubi->leb_size / UBI_VTBL_RECORD_SIZE; |
| 745 | if (ubi->vtbl_slots > UBI_MAX_VOLUMES) |
| 746 | ubi->vtbl_slots = UBI_MAX_VOLUMES; |
| 747 | |
| 748 | ubi->vtbl_size = ubi->vtbl_slots * UBI_VTBL_RECORD_SIZE; |
| 749 | ubi->vtbl_size = ALIGN(ubi->vtbl_size, ubi->min_io_size); |
| 750 | |
| 751 | sv = ubi_scan_find_sv(si, UBI_LAYOUT_VOLUME_ID); |
| 752 | if (!sv) { |
| 753 | /* |
| 754 | * No logical eraseblocks belonging to the layout volume were |
| 755 | * found. This could mean that the flash is just empty. In |
| 756 | * this case we create empty layout volume. |
| 757 | * |
| 758 | * But if flash is not empty this must be a corruption or the |
| 759 | * MTD device just contains garbage. |
| 760 | */ |
| 761 | if (si->is_empty) { |
| 762 | ubi->vtbl = create_empty_lvol(ubi, si); |
| 763 | if (IS_ERR(ubi->vtbl)) |
| 764 | return PTR_ERR(ubi->vtbl); |
| 765 | } else { |
| 766 | ubi_err("the layout volume was not found"); |
| 767 | return -EINVAL; |
| 768 | } |
| 769 | } else { |
| 770 | if (sv->leb_count > UBI_LAYOUT_VOLUME_EBS) { |
| 771 | /* This must not happen with proper UBI images */ |
| 772 | dbg_err("too many LEBs (%d) in layout volume", |
| 773 | sv->leb_count); |
| 774 | return -EINVAL; |
| 775 | } |
| 776 | |
| 777 | ubi->vtbl = process_lvol(ubi, si, sv); |
| 778 | if (IS_ERR(ubi->vtbl)) |
| 779 | return PTR_ERR(ubi->vtbl); |
| 780 | } |
| 781 | |
| 782 | ubi->avail_pebs = ubi->good_peb_count; |
| 783 | |
| 784 | /* |
| 785 | * The layout volume is OK, initialize the corresponding in-RAM data |
| 786 | * structures. |
| 787 | */ |
| 788 | err = init_volumes(ubi, si, ubi->vtbl); |
| 789 | if (err) |
| 790 | goto out_free; |
| 791 | |
| 792 | /* |
| 793 | * Get sure that the scanning information is consistent to the |
| 794 | * information stored in the volume table. |
| 795 | */ |
| 796 | err = check_scanning_info(ubi, si); |
| 797 | if (err) |
| 798 | goto out_free; |
| 799 | |
| 800 | return 0; |
| 801 | |
| 802 | out_free: |
| 803 | vfree(ubi->vtbl); |
| 804 | for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) |
| 805 | if (ubi->volumes[i]) { |
| 806 | kfree(ubi->volumes[i]); |
| 807 | ubi->volumes[i] = NULL; |
| 808 | } |
| 809 | return err; |
| 810 | } |
| 811 | |
| 812 | #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID |
| 813 | |
| 814 | /** |
| 815 | * paranoid_vtbl_check - check volume table. |
| 816 | * @ubi: UBI device description object |
| 817 | */ |
| 818 | static void paranoid_vtbl_check(const struct ubi_device *ubi) |
| 819 | { |
| 820 | if (vtbl_check(ubi, ubi->vtbl)) { |
| 821 | ubi_err("paranoid check failed"); |
| 822 | BUG(); |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ |