aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi
diff options
context:
space:
mode:
authorArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2010-09-03 07:22:17 -0400
committerArtem Bityutskiy <Artem.Bityutskiy@nokia.com>2010-10-19 10:19:56 -0400
commit92e1a7d9e7e07fb1cf0cbbcdf202938d0819b54d (patch)
tree48930121fa2f1cf9ebd36f85118fe433d1ecd01f /drivers/mtd/ubi
parent74d82d2660058e32644f0c673656b2a1d01d3688 (diff)
UBI: handle bit-flips when no header found
Currently UBI has one small flaw - when we read EC or VID header, but find only 0xFF bytes, we return UBI_IO_FF and do not report whether we had bit-flips or not. In case of the VID header, the scanning code adds this PEB to the free list, even though there were bit-flips. Imagine the following situation: we start writing VID header to a PEB and have a power cut, so the PEB becomes unstable. When we scan and read the PEB, we get a bit-flip. Currently, UBI would just ignore this and treat the PEB as free. This patch changes UBI behavior and now UBI will schedule this PEB for erasure. Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>
Diffstat (limited to 'drivers/mtd/ubi')
-rw-r--r--drivers/mtd/ubi/io.c54
-rw-r--r--drivers/mtd/ubi/scan.c4
-rw-r--r--drivers/mtd/ubi/ubi.h10
-rw-r--r--drivers/mtd/ubi/wl.c10
4 files changed, 51 insertions, 27 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 1677a215af64..b76252465c87 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -720,16 +720,16 @@ bad:
720int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 720int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
721 struct ubi_ec_hdr *ec_hdr, int verbose) 721 struct ubi_ec_hdr *ec_hdr, int verbose)
722{ 722{
723 int err, read_err = 0; 723 int err, read_err;
724 uint32_t crc, magic, hdr_crc; 724 uint32_t crc, magic, hdr_crc;
725 725
726 dbg_io("read EC header from PEB %d", pnum); 726 dbg_io("read EC header from PEB %d", pnum);
727 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 727 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
728 728
729 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 729 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
730 if (err) { 730 if (read_err) {
731 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 731 if (read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
732 return err; 732 return read_err;
733 733
734 /* 734 /*
735 * We read all the data, but either a correctable bit-flip 735 * We read all the data, but either a correctable bit-flip
@@ -740,14 +740,12 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
740 * this. If the EC header is still OK, we just report this as 740 * this. If the EC header is still OK, we just report this as
741 * there was a bit-flip, to force scrubbing. 741 * there was a bit-flip, to force scrubbing.
742 */ 742 */
743 if (err == -EBADMSG)
744 read_err = UBI_IO_BAD_HDR_EBADMSG;
745 } 743 }
746 744
747 magic = be32_to_cpu(ec_hdr->magic); 745 magic = be32_to_cpu(ec_hdr->magic);
748 if (magic != UBI_EC_HDR_MAGIC) { 746 if (magic != UBI_EC_HDR_MAGIC) {
749 if (read_err) 747 if (read_err == -EBADMSG)
750 return read_err; 748 return UBI_IO_BAD_HDR_EBADMSG;
751 749
752 /* 750 /*
753 * The magic field is wrong. Let's check if we have read all 751 * The magic field is wrong. Let's check if we have read all
@@ -762,7 +760,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
762 else if (UBI_IO_DEBUG) 760 else if (UBI_IO_DEBUG)
763 dbg_msg("no EC header found at PEB %d, " 761 dbg_msg("no EC header found at PEB %d, "
764 "only 0xFF bytes", pnum); 762 "only 0xFF bytes", pnum);
765 return UBI_IO_FF; 763 if (!read_err)
764 return UBI_IO_FF;
765 else
766 return UBI_IO_FF_BITFLIPS;
766 } 767 }
767 768
768 /* 769 /*
@@ -790,7 +791,11 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
790 } else if (UBI_IO_DEBUG) 791 } else if (UBI_IO_DEBUG)
791 dbg_msg("bad EC header CRC at PEB %d, calculated " 792 dbg_msg("bad EC header CRC at PEB %d, calculated "
792 "%#08x, read %#08x", pnum, crc, hdr_crc); 793 "%#08x, read %#08x", pnum, crc, hdr_crc);
793 return read_err ?: UBI_IO_BAD_HDR; 794
795 if (!read_err)
796 return UBI_IO_BAD_HDR;
797 else
798 return UBI_IO_BAD_HDR_EBADMSG;
794 } 799 }
795 800
796 /* And of course validate what has just been read from the media */ 801 /* And of course validate what has just been read from the media */
@@ -986,7 +991,7 @@ bad:
986int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 991int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
987 struct ubi_vid_hdr *vid_hdr, int verbose) 992 struct ubi_vid_hdr *vid_hdr, int verbose)
988{ 993{
989 int err, read_err = 0; 994 int err, read_err;
990 uint32_t crc, magic, hdr_crc; 995 uint32_t crc, magic, hdr_crc;
991 void *p; 996 void *p;
992 997
@@ -994,20 +999,15 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
994 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 999 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
995 1000
996 p = (char *)vid_hdr - ubi->vid_hdr_shift; 1001 p = (char *)vid_hdr - ubi->vid_hdr_shift;
997 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1002 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
998 ubi->vid_hdr_alsize); 1003 ubi->vid_hdr_alsize);
999 if (err) { 1004 if (read_err && read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
1000 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 1005 return read_err;
1001 return err;
1002
1003 if (err == -EBADMSG)
1004 read_err = UBI_IO_BAD_HDR_EBADMSG;
1005 }
1006 1006
1007 magic = be32_to_cpu(vid_hdr->magic); 1007 magic = be32_to_cpu(vid_hdr->magic);
1008 if (magic != UBI_VID_HDR_MAGIC) { 1008 if (magic != UBI_VID_HDR_MAGIC) {
1009 if (read_err) 1009 if (read_err == -EBADMSG)
1010 return read_err; 1010 return UBI_IO_BAD_HDR_EBADMSG;
1011 1011
1012 if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 1012 if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1013 if (verbose) 1013 if (verbose)
@@ -1016,7 +1016,10 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1016 else if (UBI_IO_DEBUG) 1016 else if (UBI_IO_DEBUG)
1017 dbg_msg("no VID header found at PEB %d, " 1017 dbg_msg("no VID header found at PEB %d, "
1018 "only 0xFF bytes", pnum); 1018 "only 0xFF bytes", pnum);
1019 return UBI_IO_FF; 1019 if (!read_err)
1020 return UBI_IO_FF;
1021 else
1022 return UBI_IO_FF_BITFLIPS;
1020 } 1023 }
1021 1024
1022 if (verbose) { 1025 if (verbose) {
@@ -1040,7 +1043,10 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1040 } else if (UBI_IO_DEBUG) 1043 } else if (UBI_IO_DEBUG)
1041 dbg_msg("bad CRC at PEB %d, calculated %#08x, " 1044 dbg_msg("bad CRC at PEB %d, calculated %#08x, "
1042 "read %#08x", pnum, crc, hdr_crc); 1045 "read %#08x", pnum, crc, hdr_crc);
1043 return read_err ?: UBI_IO_BAD_HDR; 1046 if (!read_err)
1047 return UBI_IO_BAD_HDR;
1048 else
1049 return UBI_IO_BAD_HDR_EBADMSG;
1044 } 1050 }
1045 1051
1046 err = validate_vid_hdr(ubi, vid_hdr); 1052 err = validate_vid_hdr(ubi, vid_hdr);
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 37cb18ff10c6..6f9080767e3f 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -748,7 +748,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
748 return err; 748 return err;
749 else if (err == UBI_IO_BITFLIPS) 749 else if (err == UBI_IO_BITFLIPS)
750 bitflips = 1; 750 bitflips = 1;
751 else if (err == UBI_IO_FF) 751 else if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS)
752 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); 752 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
753 else if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_BAD_HDR) { 753 else if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_BAD_HDR) {
754 /* 754 /*
@@ -817,7 +817,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
817 else if (err == UBI_IO_BITFLIPS) 817 else if (err == UBI_IO_BITFLIPS)
818 bitflips = 1; 818 bitflips = 1;
819 else if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_BAD_HDR || 819 else if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_BAD_HDR ||
820 (err == UBI_IO_FF && ec_corr)) { 820 (err == UBI_IO_FF && ec_corr) || err == UBI_IO_FF_BITFLIPS) {
821 /* VID header is corrupted */ 821 /* VID header is corrupted */
822 if (err == UBI_IO_BAD_HDR_EBADMSG || 822 if (err == UBI_IO_BAD_HDR_EBADMSG ||
823 ec_corr == UBI_IO_BAD_HDR_EBADMSG) 823 ec_corr == UBI_IO_BAD_HDR_EBADMSG)
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index 774bdcad6a07..10990770bc9e 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -86,17 +86,25 @@
86 * Error codes returned by the I/O sub-system. 86 * Error codes returned by the I/O sub-system.
87 * 87 *
88 * UBI_IO_FF: the read region of flash contains only 0xFFs 88 * UBI_IO_FF: the read region of flash contains only 0xFFs
89 * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
90 * integrity error reported by the MTD driver
91 * (uncorrectable ECC error in case of NAND)
89 * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC) 92 * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
90 * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a 93 * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
91 * data integrity error reported by the MTD driver 94 * data integrity error reported by the MTD driver
92 * (uncorrectable ECC error in case of NAND) 95 * (uncorrectable ECC error in case of NAND)
93 * UBI_IO_BITFLIPS: bit-flips were detected and corrected 96 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
97 *
98 * Note, it is probably better to have bit-flip and ebadmsg as flags which can
99 * be or'ed with other error code. But this is a big change because there are
100 * may callers, so it does not worth the risk of introducing a bug
94 */ 101 */
95enum { 102enum {
96 UBI_IO_FF = 1, 103 UBI_IO_FF = 1,
104 UBI_IO_FF_BITFLIPS,
97 UBI_IO_BAD_HDR, 105 UBI_IO_BAD_HDR,
98 UBI_IO_BAD_HDR_EBADMSG, 106 UBI_IO_BAD_HDR_EBADMSG,
99 UBI_IO_BITFLIPS 107 UBI_IO_BITFLIPS,
100}; 108};
101 109
102/* 110/*
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index a9e7c9eed703..605ecb1e22bb 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -759,6 +759,16 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
759 dbg_wl("PEB %d has no VID header", e1->pnum); 759 dbg_wl("PEB %d has no VID header", e1->pnum);
760 protect = 1; 760 protect = 1;
761 goto out_not_moved; 761 goto out_not_moved;
762 } else if (err == UBI_IO_FF_BITFLIPS) {
763 /*
764 * The same situation as %UBI_IO_FF, but bit-flips were
765 * detected. It is better to schedule this PEB for
766 * scrubbing.
767 */
768 dbg_wl("PEB %d has no VID header but has bit-flips",
769 e1->pnum);
770 scrubbing = 1;
771 goto out_not_moved;
762 } 772 }
763 773
764 ubi_err("error %d while reading VID header from PEB %d", 774 ubi_err("error %d while reading VID header from PEB %d",