Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

UBI: preserve corrupted PEBs

Currently UBI erases all corrupted eraseblocks, irrespectively of the nature
of corruption: corruption due to power cuts and non-power cut corruption.
The former case is OK, but the latter is not, because UBI may destroy
potentially important data.

With this patch, during scanning, when UBI hits a PEB with corrupted VID
header, it checks whether this PEB contains only 0xFF data. If yes, it is
safe to erase this PEB and it is put to the 'erase' list. If not, this may
be important data and it is better to avoid erasing this PEB. Instead,
UBI puts it to the corr list and moves out of the pool of available PEB.
IOW, UBI preserves this PEB.

Such corrupted PEB lessen the amount of available PEBs. So the more of them
we accumulate, the less PEBs are available. The maximum amount of non-power
cut corrupted PEBs is 8.

This patch is a response to UBIFS problem where reporter
(Matthew L. Creech <mlcreech@gmail.com>) observes that UBIFS index points
to an unmapped LEB. The theory is that corresponding PEB somehow got
corrupted and UBI wiped it. This patch (actually a series of patches)
tries to make sure such PEBs are preserved - this would make it is easier
to analyze the corruption.

Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

+45 -47
+2
drivers/mtd/ubi/build.c
··· 591 591 592 592 ubi->bad_peb_count = si->bad_peb_count; 593 593 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; 594 + ubi->corr_peb_count = si->corr_peb_count; 594 595 ubi->max_ec = si->max_ec; 595 596 ubi->mean_ec = si->mean_ec; 596 597 ubi_msg("max. sequence number: %llu", si->max_sqnum); ··· 973 972 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); 974 973 ubi_msg("number of good PEBs: %d", ubi->good_peb_count); 975 974 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); 975 + ubi_msg("number of corrupted PEBs: %d", ubi->corr_peb_count); 976 976 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); 977 977 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); 978 978 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
+6
drivers/mtd/ubi/eba.c
··· 1201 1201 1202 1202 ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d," 1203 1203 " need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); 1204 + if (ubi->corr_peb_count) 1205 + ubi_warn("%d PEBs are corrupted and not used", 1206 + ubi->corr_peb_count); 1204 1207 } 1205 1208 1206 1209 /** ··· 1266 1263 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { 1267 1264 ubi_err("no enough physical eraseblocks (%d, need %d)", 1268 1265 ubi->avail_pebs, EBA_RESERVED_PEBS); 1266 + if (ubi->corr_peb_count) 1267 + ubi_err("%d PEBs are corrupted and not used", 1268 + ubi->corr_peb_count); 1269 1269 err = -ENOSPC; 1270 1270 goto out_free; 1271 1271 }
+19 -29
drivers/mtd/ubi/scan.c
··· 706 706 struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, 707 707 struct ubi_scan_info *si) 708 708 { 709 - int err = 0, i; 710 - struct ubi_scan_leb *seb; 709 + int err = 0; 710 + struct ubi_scan_leb *seb, *tmp_seb; 711 711 712 712 if (!list_empty(&si->free)) { 713 713 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); ··· 716 716 return seb; 717 717 } 718 718 719 - for (i = 0; i < 2; i++) { 720 - struct list_head *head; 721 - struct ubi_scan_leb *tmp_seb; 719 + /* 720 + * We try to erase the first physical eraseblock from the erase list 721 + * and pick it if we succeed, or try to erase the next one if not. And 722 + * so forth. We don't want to take care about bad eraseblocks here - 723 + * they'll be handled later. 724 + */ 725 + list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) { 726 + if (seb->ec == UBI_SCAN_UNKNOWN_EC) 727 + seb->ec = si->mean_ec; 722 728 723 - if (i == 0) 724 - head = &si->erase; 725 - else 726 - head = &si->corr; 729 + err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 730 + if (err) 731 + continue; 727 732 728 - /* 729 - * We try to erase the first physical eraseblock from the @head 730 - * list and pick it if we succeed, or try to erase the 731 - * next one if not. And so forth. We don't want to take care 732 - * about bad eraseblocks here - they'll be handled later. 733 - */ 734 - list_for_each_entry_safe(seb, tmp_seb, head, u.list) { 735 - if (seb->ec == UBI_SCAN_UNKNOWN_EC) 736 - seb->ec = si->mean_ec; 737 - 738 - err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 739 - if (err) 740 - continue; 741 - 742 - seb->ec += 1; 743 - list_del(&seb->u.list); 744 - dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); 745 - return seb; 746 - } 733 + seb->ec += 1; 734 + list_del(&seb->u.list); 735 + dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); 736 + return seb; 747 737 } 748 738 749 - ubi_err("no eraseblocks found"); 739 + ubi_err("no free eraseblocks"); 750 740 return ERR_PTR(-ENOSPC); 751 741 } 752 742
+3
drivers/mtd/ubi/ubi.h
··· 361 361 * @peb_size: physical eraseblock size 362 362 * @bad_peb_count: count of bad physical eraseblocks 363 363 * @good_peb_count: count of good physical eraseblocks 364 + * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not 365 + * used by UBI) 364 366 * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous 365 367 * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks 366 368 * @min_io_size: minimal input/output unit size of the underlying MTD device ··· 449 447 int peb_size; 450 448 int bad_peb_count; 451 449 int good_peb_count; 450 + int corr_peb_count; 452 451 int erroneous_peb_count; 453 452 int max_erroneous; 454 453 int min_io_size;
+6
drivers/mtd/ubi/vmt.c
··· 261 261 /* Reserve physical eraseblocks */ 262 262 if (vol->reserved_pebs > ubi->avail_pebs) { 263 263 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); 264 + if (ubi->corr_peb_count) 265 + dbg_err("%d PEBs are corrupted and not used", 266 + ubi->corr_peb_count); 264 267 err = -ENOSPC; 265 268 goto out_unlock; 266 269 } ··· 530 527 if (pebs > ubi->avail_pebs) { 531 528 dbg_err("not enough PEBs: requested %d, available %d", 532 529 pebs, ubi->avail_pebs); 530 + if (ubi->corr_peb_count) 531 + dbg_err("%d PEBs are corrupted and not used", 532 + ubi->corr_peb_count); 533 533 spin_unlock(&ubi->volumes_lock); 534 534 err = -ENOSPC; 535 535 goto out_free;
+6 -2
drivers/mtd/ubi/vtbl.c
··· 662 662 ubi->vol_count += 1; 663 663 vol->ubi = ubi; 664 664 665 - if (reserved_pebs > ubi->avail_pebs) 665 + if (reserved_pebs > ubi->avail_pebs) { 666 666 ubi_err("not enough PEBs, required %d, available %d", 667 667 reserved_pebs, ubi->avail_pebs); 668 + if (ubi->corr_peb_count) 669 + ubi_err("%d PEBs are corrupted and not used", 670 + ubi->corr_peb_count); 671 + } 668 672 ubi->rsvd_pebs += reserved_pebs; 669 673 ubi->avail_pebs -= reserved_pebs; 670 674 ··· 841 837 return PTR_ERR(ubi->vtbl); 842 838 } 843 839 844 - ubi->avail_pebs = ubi->good_peb_count; 840 + ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count; 845 841 846 842 /* 847 843 * The layout volume is OK, initialize the corresponding in-RAM data
+3 -16
drivers/mtd/ubi/wl.c
··· 1478 1478 ubi->lookuptbl[e->pnum] = e; 1479 1479 } 1480 1480 1481 - list_for_each_entry(seb, &si->corr, u.list) { 1482 - cond_resched(); 1483 - 1484 - e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 1485 - if (!e) 1486 - goto out_free; 1487 - 1488 - e->pnum = seb->pnum; 1489 - e->ec = seb->ec; 1490 - ubi->lookuptbl[e->pnum] = e; 1491 - if (schedule_erase(ubi, e, 0)) { 1492 - kmem_cache_free(ubi_wl_entry_slab, e); 1493 - goto out_free; 1494 - } 1495 - } 1496 - 1497 1481 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1498 1482 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { 1499 1483 cond_resched(); ··· 1504 1520 if (ubi->avail_pebs < WL_RESERVED_PEBS) { 1505 1521 ubi_err("no enough physical eraseblocks (%d, need %d)", 1506 1522 ubi->avail_pebs, WL_RESERVED_PEBS); 1523 + if (ubi->corr_peb_count) 1524 + ubi_err("%d PEBs are corrupted and not used", 1525 + ubi->corr_peb_count); 1507 1526 goto out_free; 1508 1527 } 1509 1528 ubi->avail_pebs -= WL_RESERVED_PEBS;