aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/scan.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/mtd/ubi/scan.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/mtd/ubi/scan.c')
-rw-r--r--drivers/mtd/ubi/scan.c450
1 files changed, 317 insertions, 133 deletions
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 69b52e9c9489..2135a53732ff 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -29,7 +29,7 @@
29 * objects which are kept in volume RB-tree with root at the @volumes field. 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. 30 * The RB-tree is indexed by the volume ID.
31 * 31 *
32 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects. 32 * Scanned 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 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 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 35 * an RB-tree of per-volume objects and each of these objects is the root of
@@ -38,6 +38,47 @@
38 * Corrupted physical eraseblocks are put to the @corr list, free physical 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 39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list. 40 * erased are put to the @erase list.
41 *
42 * About corruptions
43 * ~~~~~~~~~~~~~~~~~
44 *
45 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
46 * whether the headers are corrupted or not. Sometimes UBI also protects the
47 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
48 * when it moves the contents of a PEB for wear-leveling purposes.
49 *
50 * UBI tries to distinguish between 2 types of corruptions.
51 *
52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
53 * tries to handle them gracefully, without printing too many warnings and
54 * error messages. The idea is that we do not lose important data in these case
55 * - we may lose only the data which was being written to the media just before
56 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
57 * handle such data losses (e.g., by using the FS journal).
58 *
59 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
60 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
61 * PEBs in the @erase list are scheduled for erasure later.
62 *
63 * 2. Unexpected corruptions which are not caused by power cuts. During
64 * scanning, such PEBs are put to the @corr list and UBI preserves them.
65 * Obviously, this lessens the amount of available PEBs, and if at some point
66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
67 * about such PEBs every time the MTD device is attached.
68 *
69 * However, it is difficult to reliably distinguish between these types of
70 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
71 * if the VID header is corrupted and the data area does not contain all 0xFFs,
72 * and there were no bit-flips or integrity errors while reading the data area.
73 * Otherwise UBI assumes corruption type 1. So the decision criteria are as
74 * follows.
75 * o If the data area contains only 0xFFs, there is no data, and it is safe
76 * to just erase this PEB - this is corruption type 1.
77 * o If the data area has bit-flips or data integrity errors (ECC errors on
78 * NAND), it is probably a PEB which was being erased when power cut
79 * happened, so this is corruption type 1. However, this is just a guess,
80 * which might be wrong.
81 * o Otherwise this it corruption type 2.
41 */ 82 */
42 83
43#include <linux/err.h> 84#include <linux/err.h>
@@ -47,7 +88,7 @@
47#include <linux/random.h> 88#include <linux/random.h>
48#include "ubi.h" 89#include "ubi.h"
49 90
50#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 91#ifdef CONFIG_MTD_UBI_DEBUG
51static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si); 92static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
52#else 93#else
53#define paranoid_check_si(ubi, si) 0 94#define paranoid_check_si(ubi, si) 0
@@ -62,39 +103,69 @@ static struct ubi_vid_hdr *vidh;
62 * @si: scanning information 103 * @si: scanning information
63 * @pnum: physical eraseblock number to add 104 * @pnum: physical eraseblock number to add
64 * @ec: erase counter of the physical eraseblock 105 * @ec: erase counter of the physical eraseblock
106 * @to_head: if not zero, add to the head of the list
65 * @list: the list to add to 107 * @list: the list to add to
66 * 108 *
67 * This function adds physical eraseblock @pnum to free, erase, corrupted or 109 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
68 * alien lists. Returns zero in case of success and a negative error code in 110 * If @to_head is not zero, PEB will be added to the head of the list, which
69 * case of failure. 111 * basically means it will be processed first later. E.g., we add corrupted
112 * PEBs (corrupted due to power cuts) to the head of the erase list to make
113 * sure we erase them first and get rid of corruptions ASAP. This function
114 * returns zero in case of success and a negative error code in case of
115 * failure.
70 */ 116 */
71static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, 117static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
72 struct list_head *list) 118 struct list_head *list)
73{ 119{
74 struct ubi_scan_leb *seb; 120 struct ubi_scan_leb *seb;
75 121
76 if (list == &si->free) { 122 if (list == &si->free) {
77 dbg_bld("add to free: PEB %d, EC %d", pnum, ec); 123 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
78 si->free_peb_count += 1;
79 } else if (list == &si->erase) { 124 } else if (list == &si->erase) {
80 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); 125 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
81 si->erase_peb_count += 1;
82 } else if (list == &si->corr) {
83 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
84 si->corr_peb_count += 1;
85 } else if (list == &si->alien) { 126 } else if (list == &si->alien) {
86 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); 127 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
87 si->alien_peb_count += 1; 128 si->alien_peb_count += 1;
88 } else 129 } else
89 BUG(); 130 BUG();
90 131
91 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); 132 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
133 if (!seb)
134 return -ENOMEM;
135
136 seb->pnum = pnum;
137 seb->ec = ec;
138 if (to_head)
139 list_add(&seb->u.list, list);
140 else
141 list_add_tail(&seb->u.list, list);
142 return 0;
143}
144
145/**
146 * add_corrupted - add a corrupted physical eraseblock.
147 * @si: scanning information
148 * @pnum: physical eraseblock number to add
149 * @ec: erase counter of the physical eraseblock
150 *
151 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
152 * The corruption was presumably not caused by a power cut. Returns zero in
153 * case of success and a negative error code in case of failure.
154 */
155static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
156{
157 struct ubi_scan_leb *seb;
158
159 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
160
161 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
92 if (!seb) 162 if (!seb)
93 return -ENOMEM; 163 return -ENOMEM;
94 164
165 si->corr_peb_count += 1;
95 seb->pnum = pnum; 166 seb->pnum = pnum;
96 seb->ec = ec; 167 seb->ec = ec;
97 list_add_tail(&seb->u.list, list); 168 list_add(&seb->u.list, &si->corr);
98 return 0; 169 return 0;
99} 170}
100 171
@@ -258,8 +329,8 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
258 * created before sequence numbers support has been added. At 329 * created before sequence numbers support has been added. At
259 * that times we used 32-bit LEB versions stored in logical 330 * that times we used 32-bit LEB versions stored in logical
260 * eraseblocks. That was before UBI got into mainline. We do not 331 * eraseblocks. That was before UBI got into mainline. We do not
261 * support these images anymore. Well, those images will work 332 * support these images anymore. Well, those images still work,
262 * still work, but only if no unclean reboots happened. 333 * but only if no unclean reboots happened.
263 */ 334 */
264 ubi_err("unsupported on-flash UBI format\n"); 335 ubi_err("unsupported on-flash UBI format\n");
265 return -EINVAL; 336 return -EINVAL;
@@ -285,19 +356,25 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
285 return 1; 356 return 1;
286 } 357 }
287 } else { 358 } else {
288 pnum = seb->pnum; 359 if (!seb->copy_flag) {
360 /* It is not a copy, so it is newer */
361 dbg_bld("first PEB %d is newer, copy_flag is unset",
362 pnum);
363 return bitflips << 1;
364 }
289 365
290 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 366 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
291 if (!vh) 367 if (!vh)
292 return -ENOMEM; 368 return -ENOMEM;
293 369
370 pnum = seb->pnum;
294 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 371 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
295 if (err) { 372 if (err) {
296 if (err == UBI_IO_BITFLIPS) 373 if (err == UBI_IO_BITFLIPS)
297 bitflips = 1; 374 bitflips = 1;
298 else { 375 else {
299 dbg_err("VID of PEB %d header is bad, but it " 376 dbg_err("VID of PEB %d header is bad, but it "
300 "was OK earlier", pnum); 377 "was OK earlier, err %d", pnum, err);
301 if (err > 0) 378 if (err > 0)
302 err = -EIO; 379 err = -EIO;
303 380
@@ -305,14 +382,6 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
305 } 382 }
306 } 383 }
307 384
308 if (!vh->copy_flag) {
309 /* It is not a copy, so it is newer */
310 dbg_bld("first PEB %d is newer, copy_flag is unset",
311 pnum);
312 err = bitflips << 1;
313 goto out_free_vidh;
314 }
315
316 vid_hdr = vh; 385 vid_hdr = vh;
317 } 386 }
318 387
@@ -463,18 +532,15 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
463 if (err) 532 if (err)
464 return err; 533 return err;
465 534
466 if (cmp_res & 4) 535 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
467 err = add_to_list(si, seb->pnum, seb->ec, 536 &si->erase);
468 &si->corr);
469 else
470 err = add_to_list(si, seb->pnum, seb->ec,
471 &si->erase);
472 if (err) 537 if (err)
473 return err; 538 return err;
474 539
475 seb->ec = ec; 540 seb->ec = ec;
476 seb->pnum = pnum; 541 seb->pnum = pnum;
477 seb->scrub = ((cmp_res & 2) || bitflips); 542 seb->scrub = ((cmp_res & 2) || bitflips);
543 seb->copy_flag = vid_hdr->copy_flag;
478 seb->sqnum = sqnum; 544 seb->sqnum = sqnum;
479 545
480 if (sv->highest_lnum == lnum) 546 if (sv->highest_lnum == lnum)
@@ -487,10 +553,8 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
487 * This logical eraseblock is older than the one found 553 * This logical eraseblock is older than the one found
488 * previously. 554 * previously.
489 */ 555 */
490 if (cmp_res & 4) 556 return add_to_list(si, pnum, ec, cmp_res & 4,
491 return add_to_list(si, pnum, ec, &si->corr); 557 &si->erase);
492 else
493 return add_to_list(si, pnum, ec, &si->erase);
494 } 558 }
495 } 559 }
496 560
@@ -503,15 +567,16 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
503 if (err) 567 if (err)
504 return err; 568 return err;
505 569
506 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); 570 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
507 if (!seb) 571 if (!seb)
508 return -ENOMEM; 572 return -ENOMEM;
509 573
510 seb->ec = ec; 574 seb->ec = ec;
511 seb->pnum = pnum; 575 seb->pnum = pnum;
512 seb->lnum = lnum; 576 seb->lnum = lnum;
513 seb->sqnum = sqnum;
514 seb->scrub = bitflips; 577 seb->scrub = bitflips;
578 seb->copy_flag = vid_hdr->copy_flag;
579 seb->sqnum = sqnum;
515 580
516 if (sv->highest_lnum <= lnum) { 581 if (sv->highest_lnum <= lnum) {
517 sv->highest_lnum = lnum; 582 sv->highest_lnum = lnum;
@@ -521,7 +586,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
521 sv->leb_count += 1; 586 sv->leb_count += 1;
522 rb_link_node(&seb->u.rb, parent, p); 587 rb_link_node(&seb->u.rb, parent, p);
523 rb_insert_color(&seb->u.rb, &sv->root); 588 rb_insert_color(&seb->u.rb, &sv->root);
524 si->used_peb_count += 1;
525 return 0; 589 return 0;
526} 590}
527 591
@@ -668,8 +732,8 @@ out_free:
668struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, 732struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
669 struct ubi_scan_info *si) 733 struct ubi_scan_info *si)
670{ 734{
671 int err = 0, i; 735 int err = 0;
672 struct ubi_scan_leb *seb; 736 struct ubi_scan_leb *seb, *tmp_seb;
673 737
674 if (!list_empty(&si->free)) { 738 if (!list_empty(&si->free)) {
675 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); 739 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
@@ -678,38 +742,88 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
678 return seb; 742 return seb;
679 } 743 }
680 744
681 for (i = 0; i < 2; i++) { 745 /*
682 struct list_head *head; 746 * We try to erase the first physical eraseblock from the erase list
683 struct ubi_scan_leb *tmp_seb; 747 * and pick it if we succeed, or try to erase the next one if not. And
748 * so forth. We don't want to take care about bad eraseblocks here -
749 * they'll be handled later.
750 */
751 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) {
752 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
753 seb->ec = si->mean_ec;
684 754
685 if (i == 0) 755 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
686 head = &si->erase; 756 if (err)
687 else 757 continue;
688 head = &si->corr;
689 758
759 seb->ec += 1;
760 list_del(&seb->u.list);
761 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
762 return seb;
763 }
764
765 ubi_err("no free eraseblocks");
766 return ERR_PTR(-ENOSPC);
767}
768
769/**
770 * check_corruption - check the data area of PEB.
771 * @ubi: UBI device description object
772 * @vid_hrd: the (corrupted) VID header of this PEB
773 * @pnum: the physical eraseblock number to check
774 *
775 * This is a helper function which is used to distinguish between VID header
776 * corruptions caused by power cuts and other reasons. If the PEB contains only
777 * 0xFF bytes in the data area, the VID header is most probably corrupted
778 * because of a power cut (%0 is returned in this case). Otherwise, it was
779 * probably corrupted for some other reasons (%1 is returned in this case). A
780 * negative error code is returned if a read error occurred.
781 *
782 * If the corruption reason was a power cut, UBI can safely erase this PEB.
783 * Otherwise, it should preserve it to avoid possibly destroying important
784 * information.
785 */
786static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
787 int pnum)
788{
789 int err;
790
791 mutex_lock(&ubi->buf_mutex);
792 memset(ubi->peb_buf1, 0x00, ubi->leb_size);
793
794 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, ubi->leb_start,
795 ubi->leb_size);
796 if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
690 /* 797 /*
691 * We try to erase the first physical eraseblock from the @head 798 * Bit-flips or integrity errors while reading the data area.
692 * list and pick it if we succeed, or try to erase the 799 * It is difficult to say for sure what type of corruption is
693 * next one if not. And so forth. We don't want to take care 800 * this, but presumably a power cut happened while this PEB was
694 * about bad eraseblocks here - they'll be handled later. 801 * erased, so it became unstable and corrupted, and should be
802 * erased.
695 */ 803 */
696 list_for_each_entry_safe(seb, tmp_seb, head, u.list) { 804 err = 0;
697 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 805 goto out_unlock;
698 seb->ec = si->mean_ec; 806 }
699 807
700 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 808 if (err)
701 if (err) 809 goto out_unlock;
702 continue;
703 810
704 seb->ec += 1; 811 if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size))
705 list_del(&seb->u.list); 812 goto out_unlock;
706 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
707 return seb;
708 }
709 }
710 813
711 ubi_err("no eraseblocks found"); 814 ubi_err("PEB %d contains corrupted VID header, and the data does not "
712 return ERR_PTR(-ENOSPC); 815 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
816 "header corruption which requires manual inspection", pnum);
817 ubi_dbg_dump_vid_hdr(vid_hdr);
818 dbg_msg("hexdump of PEB %d offset %d, length %d",
819 pnum, ubi->leb_start, ubi->leb_size);
820 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
821 ubi->peb_buf1, ubi->leb_size, 1);
822 err = 1;
823
824out_unlock:
825 mutex_unlock(&ubi->buf_mutex);
826 return err;
713} 827}
714 828
715/** 829/**
@@ -725,7 +839,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
725 int pnum) 839 int pnum)
726{ 840{
727 long long uninitialized_var(ec); 841 long long uninitialized_var(ec);
728 int err, bitflips = 0, vol_id, ec_corr = 0; 842 int err, bitflips = 0, vol_id, ec_err = 0;
729 843
730 dbg_bld("scan PEB %d", pnum); 844 dbg_bld("scan PEB %d", pnum);
731 845
@@ -746,22 +860,37 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
746 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); 860 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
747 if (err < 0) 861 if (err < 0)
748 return err; 862 return err;
749 else if (err == UBI_IO_BITFLIPS) 863 switch (err) {
864 case 0:
865 break;
866 case UBI_IO_BITFLIPS:
750 bitflips = 1; 867 bitflips = 1;
751 else if (err == UBI_IO_PEB_EMPTY) 868 break;
752 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); 869 case UBI_IO_FF:
753 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) { 870 si->empty_peb_count += 1;
871 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
872 &si->erase);
873 case UBI_IO_FF_BITFLIPS:
874 si->empty_peb_count += 1;
875 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
876 &si->erase);
877 case UBI_IO_BAD_HDR_EBADMSG:
878 case UBI_IO_BAD_HDR:
754 /* 879 /*
755 * We have to also look at the VID header, possibly it is not 880 * We have to also look at the VID header, possibly it is not
756 * corrupted. Set %bitflips flag in order to make this PEB be 881 * corrupted. Set %bitflips flag in order to make this PEB be
757 * moved and EC be re-created. 882 * moved and EC be re-created.
758 */ 883 */
759 ec_corr = err; 884 ec_err = err;
760 ec = UBI_SCAN_UNKNOWN_EC; 885 ec = UBI_SCAN_UNKNOWN_EC;
761 bitflips = 1; 886 bitflips = 1;
887 break;
888 default:
889 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
890 return -EINVAL;
762 } 891 }
763 892
764 if (!ec_corr) { 893 if (!ec_err) {
765 int image_seq; 894 int image_seq;
766 895
767 /* Make sure UBI version is OK */ 896 /* Make sure UBI version is OK */
@@ -814,24 +943,71 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
814 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); 943 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
815 if (err < 0) 944 if (err < 0)
816 return err; 945 return err;
817 else if (err == UBI_IO_BITFLIPS) 946 switch (err) {
947 case 0:
948 break;
949 case UBI_IO_BITFLIPS:
818 bitflips = 1; 950 bitflips = 1;
819 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR || 951 break;
820 (err == UBI_IO_PEB_FREE && ec_corr)) { 952 case UBI_IO_BAD_HDR_EBADMSG:
821 /* VID header is corrupted */ 953 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
822 if (err == UBI_IO_BAD_HDR_READ || 954 /*
823 ec_corr == UBI_IO_BAD_HDR_READ) 955 * Both EC and VID headers are corrupted and were read
824 si->read_err_count += 1; 956 * with data integrity error, probably this is a bad
825 err = add_to_list(si, pnum, ec, &si->corr); 957 * PEB, bit it is not marked as bad yet. This may also
958 * be a result of power cut during erasure.
959 */
960 si->maybe_bad_peb_count += 1;
961 case UBI_IO_BAD_HDR:
962 if (ec_err)
963 /*
964 * Both headers are corrupted. There is a possibility
965 * that this a valid UBI PEB which has corresponding
966 * LEB, but the headers are corrupted. However, it is
967 * impossible to distinguish it from a PEB which just
968 * contains garbage because of a power cut during erase
969 * operation. So we just schedule this PEB for erasure.
970 *
971 * Besides, in case of NOR flash, we deliberately
972 * corrupt both headers because NOR flash erasure is
973 * slow and can start from the end.
974 */
975 err = 0;
976 else
977 /*
978 * The EC was OK, but the VID header is corrupted. We
979 * have to check what is in the data area.
980 */
981 err = check_corruption(ubi, vidh, pnum);
982
983 if (err < 0)
984 return err;
985 else if (!err)
986 /* This corruption is caused by a power cut */
987 err = add_to_list(si, pnum, ec, 1, &si->erase);
988 else
989 /* This is an unexpected corruption */
990 err = add_corrupted(si, pnum, ec);
991 if (err)
992 return err;
993 goto adjust_mean_ec;
994 case UBI_IO_FF_BITFLIPS:
995 err = add_to_list(si, pnum, ec, 1, &si->erase);
826 if (err) 996 if (err)
827 return err; 997 return err;
828 goto adjust_mean_ec; 998 goto adjust_mean_ec;
829 } else if (err == UBI_IO_PEB_FREE) { 999 case UBI_IO_FF:
830 /* No VID header - the physical eraseblock is free */ 1000 if (ec_err)
831 err = add_to_list(si, pnum, ec, &si->free); 1001 err = add_to_list(si, pnum, ec, 1, &si->erase);
1002 else
1003 err = add_to_list(si, pnum, ec, 0, &si->free);
832 if (err) 1004 if (err)
833 return err; 1005 return err;
834 goto adjust_mean_ec; 1006 goto adjust_mean_ec;
1007 default:
1008 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
1009 err);
1010 return -EINVAL;
835 } 1011 }
836 1012
837 vol_id = be32_to_cpu(vidh->vol_id); 1013 vol_id = be32_to_cpu(vidh->vol_id);
@@ -843,7 +1019,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
843 case UBI_COMPAT_DELETE: 1019 case UBI_COMPAT_DELETE:
844 ubi_msg("\"delete\" compatible internal volume %d:%d" 1020 ubi_msg("\"delete\" compatible internal volume %d:%d"
845 " found, will remove it", vol_id, lnum); 1021 " found, will remove it", vol_id, lnum);
846 err = add_to_list(si, pnum, ec, &si->erase); 1022 err = add_to_list(si, pnum, ec, 1, &si->erase);
847 if (err) 1023 if (err)
848 return err; 1024 return err;
849 return 0; 1025 return 0;
@@ -858,7 +1034,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
858 case UBI_COMPAT_PRESERVE: 1034 case UBI_COMPAT_PRESERVE:
859 ubi_msg("\"preserve\" compatible internal volume %d:%d" 1035 ubi_msg("\"preserve\" compatible internal volume %d:%d"
860 " found", vol_id, lnum); 1036 " found", vol_id, lnum);
861 err = add_to_list(si, pnum, ec, &si->alien); 1037 err = add_to_list(si, pnum, ec, 0, &si->alien);
862 if (err) 1038 if (err)
863 return err; 1039 return err;
864 return 0; 1040 return 0;
@@ -870,7 +1046,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
870 } 1046 }
871 } 1047 }
872 1048
873 if (ec_corr) 1049 if (ec_err)
874 ubi_warn("valid VID header but corrupted EC header at PEB %d", 1050 ubi_warn("valid VID header but corrupted EC header at PEB %d",
875 pnum); 1051 pnum);
876 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); 1052 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
@@ -878,7 +1054,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
878 return err; 1054 return err;
879 1055
880adjust_mean_ec: 1056adjust_mean_ec:
881 if (!ec_corr) { 1057 if (!ec_err) {
882 si->ec_sum += ec; 1058 si->ec_sum += ec;
883 si->ec_count += 1; 1059 si->ec_count += 1;
884 if (ec > si->max_ec) 1060 if (ec > si->max_ec)
@@ -904,19 +1080,20 @@ adjust_mean_ec:
904static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si) 1080static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
905{ 1081{
906 struct ubi_scan_leb *seb; 1082 struct ubi_scan_leb *seb;
907 int max_corr; 1083 int max_corr, peb_count;
908 1084
909 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count; 1085 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
910 max_corr = max_corr / 20 ?: 8; 1086 max_corr = peb_count / 20 ?: 8;
911 1087
912 /* 1088 /*
913 * Few corrupted PEBs are not a problem and may be just a result of 1089 * Few corrupted PEBs is not a problem and may be just a result of
914 * unclean reboots. However, many of them may indicate some problems 1090 * unclean reboots. However, many of them may indicate some problems
915 * with the flash HW or driver. 1091 * with the flash HW or driver.
916 */ 1092 */
917 if (si->corr_peb_count >= 8) { 1093 if (si->corr_peb_count) {
918 ubi_warn("%d PEBs are corrupted", si->corr_peb_count); 1094 ubi_err("%d PEBs are corrupted and preserved",
919 printk(KERN_WARNING "corrupted PEBs are:"); 1095 si->corr_peb_count);
1096 printk(KERN_ERR "Corrupted PEBs are:");
920 list_for_each_entry(seb, &si->corr, u.list) 1097 list_for_each_entry(seb, &si->corr, u.list)
921 printk(KERN_CONT " %d", seb->pnum); 1098 printk(KERN_CONT " %d", seb->pnum);
922 printk(KERN_CONT "\n"); 1099 printk(KERN_CONT "\n");
@@ -926,46 +1103,40 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
926 * otherwise, only print a warning. 1103 * otherwise, only print a warning.
927 */ 1104 */
928 if (si->corr_peb_count >= max_corr) { 1105 if (si->corr_peb_count >= max_corr) {
929 ubi_err("too many corrupted PEBs, refusing this device"); 1106 ubi_err("too many corrupted PEBs, refusing");
930 return -EINVAL; 1107 return -EINVAL;
931 } 1108 }
932 } 1109 }
933 1110
934 if (si->free_peb_count + si->used_peb_count + 1111 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
935 si->alien_peb_count == 0) { 1112 /*
936 /* No UBI-formatted eraseblocks were found */ 1113 * All PEBs are empty, or almost all - a couple PEBs look like
937 if (si->corr_peb_count == si->read_err_count && 1114 * they may be bad PEBs which were not marked as bad yet.
938 si->corr_peb_count < 8) { 1115 *
939 /* No or just few corrupted PEBs, and all of them had a 1116 * This piece of code basically tries to distinguish between
940 * read error. We assume that those are bad PEBs, which 1117 * the following situations:
941 * were just not marked as bad so far. 1118 *
942 * 1119 * 1. Flash is empty, but there are few bad PEBs, which are not
943 * This piece of code basically tries to distinguish 1120 * marked as bad so far, and which were read with error. We
944 * between the following 2 situations: 1121 * want to go ahead and format this flash. While formatting,
945 * 1122 * the faulty PEBs will probably be marked as bad.
946 * 1. Flash is empty, but there are few bad PEBs, which 1123 *
947 * are not marked as bad so far, and which were read 1124 * 2. Flash contains non-UBI data and we do not want to format
948 * with error. We want to go ahead and format this 1125 * it and destroy possibly important information.
949 * flash. While formating, the faulty PEBs will 1126 */
950 * probably be marked as bad. 1127 if (si->maybe_bad_peb_count <= 2) {
951 *
952 * 2. Flash probably contains non-UBI data and we do
953 * not want to format it and destroy possibly needed
954 * data (e.g., consider the case when the bootloader
955 * MTD partition was accidentally fed to UBI).
956 */
957 si->is_empty = 1; 1128 si->is_empty = 1;
958 ubi_msg("empty MTD device detected"); 1129 ubi_msg("empty MTD device detected");
959 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq)); 1130 get_random_bytes(&ubi->image_seq,
1131 sizeof(ubi->image_seq));
960 } else { 1132 } else {
961 ubi_err("MTD device possibly contains non-UBI data, " 1133 ubi_err("MTD device is not UBI-formatted and possibly "
962 "refusing it"); 1134 "contains non-UBI data - refusing it");
963 return -EINVAL; 1135 return -EINVAL;
964 } 1136 }
1137
965 } 1138 }
966 1139
967 if (si->corr_peb_count > 0)
968 ubi_msg("corrupted PEBs will be formatted");
969 return 0; 1140 return 0;
970} 1141}
971 1142
@@ -995,9 +1166,15 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
995 si->volumes = RB_ROOT; 1166 si->volumes = RB_ROOT;
996 1167
997 err = -ENOMEM; 1168 err = -ENOMEM;
1169 si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
1170 sizeof(struct ubi_scan_leb),
1171 0, 0, NULL);
1172 if (!si->scan_leb_slab)
1173 goto out_si;
1174
998 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1175 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
999 if (!ech) 1176 if (!ech)
1000 goto out_si; 1177 goto out_slab;
1001 1178
1002 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1179 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1003 if (!vidh) 1180 if (!vidh)
@@ -1058,6 +1235,8 @@ out_vidh:
1058 ubi_free_vid_hdr(ubi, vidh); 1235 ubi_free_vid_hdr(ubi, vidh);
1059out_ech: 1236out_ech:
1060 kfree(ech); 1237 kfree(ech);
1238out_slab:
1239 kmem_cache_destroy(si->scan_leb_slab);
1061out_si: 1240out_si:
1062 ubi_scan_destroy_si(si); 1241 ubi_scan_destroy_si(si);
1063 return ERR_PTR(err); 1242 return ERR_PTR(err);
@@ -1066,11 +1245,12 @@ out_si:
1066/** 1245/**
1067 * destroy_sv - free the scanning volume information 1246 * destroy_sv - free the scanning volume information
1068 * @sv: scanning volume information 1247 * @sv: scanning volume information
1248 * @si: scanning information
1069 * 1249 *
1070 * This function destroys the volume RB-tree (@sv->root) and the scanning 1250 * This function destroys the volume RB-tree (@sv->root) and the scanning
1071 * volume information. 1251 * volume information.
1072 */ 1252 */
1073static void destroy_sv(struct ubi_scan_volume *sv) 1253static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
1074{ 1254{
1075 struct ubi_scan_leb *seb; 1255 struct ubi_scan_leb *seb;
1076 struct rb_node *this = sv->root.rb_node; 1256 struct rb_node *this = sv->root.rb_node;
@@ -1090,7 +1270,7 @@ static void destroy_sv(struct ubi_scan_volume *sv)
1090 this->rb_right = NULL; 1270 this->rb_right = NULL;
1091 } 1271 }
1092 1272
1093 kfree(seb); 1273 kmem_cache_free(si->scan_leb_slab, seb);
1094 } 1274 }
1095 } 1275 }
1096 kfree(sv); 1276 kfree(sv);
@@ -1108,19 +1288,19 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1108 1288
1109 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { 1289 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1110 list_del(&seb->u.list); 1290 list_del(&seb->u.list);
1111 kfree(seb); 1291 kmem_cache_free(si->scan_leb_slab, seb);
1112 } 1292 }
1113 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { 1293 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1114 list_del(&seb->u.list); 1294 list_del(&seb->u.list);
1115 kfree(seb); 1295 kmem_cache_free(si->scan_leb_slab, seb);
1116 } 1296 }
1117 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { 1297 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1118 list_del(&seb->u.list); 1298 list_del(&seb->u.list);
1119 kfree(seb); 1299 kmem_cache_free(si->scan_leb_slab, seb);
1120 } 1300 }
1121 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { 1301 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1122 list_del(&seb->u.list); 1302 list_del(&seb->u.list);
1123 kfree(seb); 1303 kmem_cache_free(si->scan_leb_slab, seb);
1124 } 1304 }
1125 1305
1126 /* Destroy the volume RB-tree */ 1306 /* Destroy the volume RB-tree */
@@ -1141,14 +1321,15 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1141 rb->rb_right = NULL; 1321 rb->rb_right = NULL;
1142 } 1322 }
1143 1323
1144 destroy_sv(sv); 1324 destroy_sv(si, sv);
1145 } 1325 }
1146 } 1326 }
1147 1327
1328 kmem_cache_destroy(si->scan_leb_slab);
1148 kfree(si); 1329 kfree(si);
1149} 1330}
1150 1331
1151#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1332#ifdef CONFIG_MTD_UBI_DEBUG
1152 1333
1153/** 1334/**
1154 * paranoid_check_si - check the scanning information. 1335 * paranoid_check_si - check the scanning information.
@@ -1166,6 +1347,9 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1166 struct ubi_scan_leb *seb, *last_seb; 1347 struct ubi_scan_leb *seb, *last_seb;
1167 uint8_t *buf; 1348 uint8_t *buf;
1168 1349
1350 if (!(ubi_chk_flags & UBI_CHK_GEN))
1351 return 0;
1352
1169 /* 1353 /*
1170 * At first, check that scanning information is OK. 1354 * At first, check that scanning information is OK.
1171 */ 1355 */
@@ -1418,4 +1602,4 @@ out:
1418 return -EINVAL; 1602 return -EINVAL;
1419} 1603}
1420 1604
1421#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1605#endif /* CONFIG_MTD_UBI_DEBUG */