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