aboutsummaryrefslogtreecommitdiffstats
path: root/drivers
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-08-03 17:37:26 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-08-03 17:37:26 -0400
commit7046e668d7973c470146fbe6635967a1b4a31bca (patch)
tree213de429f7081469632f27f57052d71999fee5e5 /drivers
parentc939f9f9d225972a60181c95df8a9aa50f8b1701 (diff)
parent64d4b4c90a876401e503c3a3260e9d0ed066f271 (diff)
Merge branch 'linux-next' of git://git.infradead.org/ubi-2.6
* 'linux-next' of git://git.infradead.org/ubi-2.6: UBI: do not warn unnecessarily UBI: do not print message about corruptes PEBs if we have none of them UBI: improve delete-compatible volumes handling UBI: fix error message and compilation warnings UBI: generate random image_seq when formatting MTD devices UBI: improve ECC error message UBI: improve corrupted flash handling UBI: introduce eraseblock counter variables UBI: introduce a new IO return code UBI: simplify IO error codes
Diffstat (limited to 'drivers')
-rw-r--r--drivers/mtd/ubi/build.c3
-rw-r--r--drivers/mtd/ubi/eba.c49
-rw-r--r--drivers/mtd/ubi/io.c60
-rw-r--r--drivers/mtd/ubi/scan.c131
-rw-r--r--drivers/mtd/ubi/scan.h19
-rw-r--r--drivers/mtd/ubi/ubi.h10
6 files changed, 198 insertions, 74 deletions
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 13b05cb33b08..78ae89488a4f 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -593,6 +593,7 @@ static int attach_by_scanning(struct ubi_device *ubi)
593 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; 593 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
594 ubi->max_ec = si->max_ec; 594 ubi->max_ec = si->max_ec;
595 ubi->mean_ec = si->mean_ec; 595 ubi->mean_ec = si->mean_ec;
596 ubi_msg("max. sequence number: %llu", si->max_sqnum);
596 597
597 err = ubi_read_volume_table(ubi, si); 598 err = ubi_read_volume_table(ubi, si);
598 if (err) 599 if (err)
@@ -981,7 +982,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
981 ubi_msg("number of PEBs reserved for bad PEB handling: %d", 982 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
982 ubi->beb_rsvd_pebs); 983 ubi->beb_rsvd_pebs);
983 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); 984 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
984 ubi_msg("image sequence number: %d", ubi->image_seq); 985 ubi_msg("image sequence number: %d", ubi->image_seq);
985 986
986 /* 987 /*
987 * The below lock makes sure we do not race with 'ubi_thread()' which 988 * The below lock makes sure we do not race with 'ubi_thread()' which
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c
index 9f87c99189a9..fe74749e0dae 100644
--- a/drivers/mtd/ubi/eba.c
+++ b/drivers/mtd/ubi/eba.c
@@ -418,7 +418,8 @@ retry:
418 * may try to recover data. FIXME: but this is 418 * may try to recover data. FIXME: but this is
419 * not implemented. 419 * not implemented.
420 */ 420 */
421 if (err == UBI_IO_BAD_VID_HDR) { 421 if (err == UBI_IO_BAD_HDR_READ ||
422 err == UBI_IO_BAD_HDR) {
422 ubi_warn("corrupted VID header at PEB " 423 ubi_warn("corrupted VID header at PEB "
423 "%d, LEB %d:%d", pnum, vol_id, 424 "%d, LEB %d:%d", pnum, vol_id,
424 lnum); 425 lnum);
@@ -961,8 +962,8 @@ write_error:
961 */ 962 */
962static int is_error_sane(int err) 963static int is_error_sane(int err)
963{ 964{
964 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_VID_HDR || 965 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
965 err == -ETIMEDOUT) 966 err == UBI_IO_BAD_HDR_READ || err == -ETIMEDOUT)
966 return 0; 967 return 0;
967 return 1; 968 return 1;
968} 969}
@@ -1165,6 +1166,44 @@ out_unlock_leb:
1165} 1166}
1166 1167
1167/** 1168/**
1169 * print_rsvd_warning - warn about not having enough reserved PEBs.
1170 * @ubi: UBI device description object
1171 *
1172 * This is a helper function for 'ubi_eba_init_scan()' which is called when UBI
1173 * cannot reserve enough PEBs for bad block handling. This function makes a
1174 * decision whether we have to print a warning or not. The algorithm is as
1175 * follows:
1176 * o if this is a new UBI image, then just print the warning
1177 * o if this is an UBI image which has already been used for some time, print
1178 * a warning only if we can reserve less than 10% of the expected amount of
1179 * the reserved PEB.
1180 *
1181 * The idea is that when UBI is used, PEBs become bad, and the reserved pool
1182 * of PEBs becomes smaller, which is normal and we do not want to scare users
1183 * with a warning every time they attach the MTD device. This was an issue
1184 * reported by real users.
1185 */
1186static void print_rsvd_warning(struct ubi_device *ubi,
1187 struct ubi_scan_info *si)
1188{
1189 /*
1190 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably
1191 * large number to distinguish between newly flashed and used images.
1192 */
1193 if (si->max_sqnum > (1 << 18)) {
1194 int min = ubi->beb_rsvd_level / 10;
1195
1196 if (!min)
1197 min = 1;
1198 if (ubi->beb_rsvd_pebs > min)
1199 return;
1200 }
1201
1202 ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d,"
1203 " need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1204}
1205
1206/**
1168 * ubi_eba_init_scan - initialize the EBA sub-system using scanning information. 1207 * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
1169 * @ubi: UBI device description object 1208 * @ubi: UBI device description object
1170 * @si: scanning information 1209 * @si: scanning information
@@ -1236,9 +1275,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1236 if (ubi->avail_pebs < ubi->beb_rsvd_level) { 1275 if (ubi->avail_pebs < ubi->beb_rsvd_level) {
1237 /* No enough free physical eraseblocks */ 1276 /* No enough free physical eraseblocks */
1238 ubi->beb_rsvd_pebs = ubi->avail_pebs; 1277 ubi->beb_rsvd_pebs = ubi->avail_pebs;
1239 ubi_warn("cannot reserve enough PEBs for bad PEB " 1278 print_rsvd_warning(ubi, si);
1240 "handling, reserved %d, need %d",
1241 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
1242 } else 1279 } else
1243 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; 1280 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
1244 1281
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 4b979e34b159..332f992f13d9 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -150,6 +150,8 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
150retry: 150retry:
151 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 151 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
152 if (err) { 152 if (err) {
153 const char *errstr = (err == -EBADMSG) ? " (ECC error)" : "";
154
153 if (err == -EUCLEAN) { 155 if (err == -EUCLEAN) {
154 /* 156 /*
155 * -EUCLEAN is reported if there was a bit-flip which 157 * -EUCLEAN is reported if there was a bit-flip which
@@ -165,15 +167,15 @@ retry:
165 } 167 }
166 168
167 if (read != len && retries++ < UBI_IO_RETRIES) { 169 if (read != len && retries++ < UBI_IO_RETRIES) {
168 dbg_io("error %d while reading %d bytes from PEB %d:%d," 170 dbg_io("error %d%s while reading %d bytes from PEB %d:%d,"
169 " read only %zd bytes, retry", 171 " read only %zd bytes, retry",
170 err, len, pnum, offset, read); 172 err, errstr, len, pnum, offset, read);
171 yield(); 173 yield();
172 goto retry; 174 goto retry;
173 } 175 }
174 176
175 ubi_err("error %d while reading %d bytes from PEB %d:%d, " 177 ubi_err("error %d%s while reading %d bytes from PEB %d:%d, "
176 "read %zd bytes", err, len, pnum, offset, read); 178 "read %zd bytes", err, errstr, len, pnum, offset, read);
177 ubi_dbg_dump_stack(); 179 ubi_dbg_dump_stack();
178 180
179 /* 181 /*
@@ -515,7 +517,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
515 * In this case we probably anyway have garbage in this PEB. 517 * In this case we probably anyway have garbage in this PEB.
516 */ 518 */
517 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 519 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
518 if (err1 == UBI_IO_BAD_VID_HDR) 520 if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR)
519 /* 521 /*
520 * The VID header is corrupted, so we can safely erase this 522 * The VID header is corrupted, so we can safely erase this
521 * PEB and not afraid that it will be treated as a valid PEB in 523 * PEB and not afraid that it will be treated as a valid PEB in
@@ -709,7 +711,7 @@ bad:
709 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 711 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
710 * and corrected by the flash driver; this is harmless but may indicate that 712 * and corrected by the flash driver; this is harmless but may indicate that
711 * this eraseblock may become bad soon (but may be not); 713 * this eraseblock may become bad soon (but may be not);
712 * o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); 714 * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
713 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; 715 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
714 * o a negative error code in case of failure. 716 * o a negative error code in case of failure.
715 */ 717 */
@@ -736,23 +738,21 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
736 * header is still OK, we just report this as there was a 738 * header is still OK, we just report this as there was a
737 * bit-flip. 739 * bit-flip.
738 */ 740 */
739 read_err = err; 741 if (err == -EBADMSG)
742 read_err = UBI_IO_BAD_HDR_READ;
740 } 743 }
741 744
742 magic = be32_to_cpu(ec_hdr->magic); 745 magic = be32_to_cpu(ec_hdr->magic);
743 if (magic != UBI_EC_HDR_MAGIC) { 746 if (magic != UBI_EC_HDR_MAGIC) {
747 if (read_err)
748 return read_err;
749
744 /* 750 /*
745 * 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
746 * 0xFF. If yes, this physical eraseblock is assumed to be 752 * 0xFF. If yes, this physical eraseblock is assumed to be
747 * empty. 753 * empty.
748 *
749 * But if there was a read error, we do not test it for all
750 * 0xFFs. Even if it does contain all 0xFFs, this error
751 * indicates that something is still wrong with this physical
752 * eraseblock and we anyway cannot treat it as empty.
753 */ 754 */
754 if (read_err != -EBADMSG && 755 if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
755 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
756 /* The physical eraseblock is supposedly empty */ 756 /* The physical eraseblock is supposedly empty */
757 if (verbose) 757 if (verbose)
758 ubi_warn("no EC header found at PEB %d, " 758 ubi_warn("no EC header found at PEB %d, "
@@ -774,7 +774,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
774 } else if (UBI_IO_DEBUG) 774 } else if (UBI_IO_DEBUG)
775 dbg_msg("bad magic number at PEB %d: %08x instead of " 775 dbg_msg("bad magic number at PEB %d: %08x instead of "
776 "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 776 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
777 return UBI_IO_BAD_EC_HDR; 777 return UBI_IO_BAD_HDR;
778 } 778 }
779 779
780 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 780 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
@@ -788,7 +788,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
788 } else if (UBI_IO_DEBUG) 788 } else if (UBI_IO_DEBUG)
789 dbg_msg("bad EC header CRC at PEB %d, calculated " 789 dbg_msg("bad EC header CRC at PEB %d, calculated "
790 "%#08x, read %#08x", pnum, crc, hdr_crc); 790 "%#08x, read %#08x", pnum, crc, hdr_crc);
791 return UBI_IO_BAD_EC_HDR; 791 return read_err ?: UBI_IO_BAD_HDR;
792 } 792 }
793 793
794 /* And of course validate what has just been read from the media */ 794 /* And of course validate what has just been read from the media */
@@ -798,6 +798,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
798 return -EINVAL; 798 return -EINVAL;
799 } 799 }
800 800
801 /*
802 * If there was %-EBADMSG, but the header CRC is still OK, report about
803 * a bit-flip to force scrubbing on this PEB.
804 */
801 return read_err ? UBI_IO_BITFLIPS : 0; 805 return read_err ? UBI_IO_BITFLIPS : 0;
802} 806}
803 807
@@ -977,7 +981,7 @@ bad:
977 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 981 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
978 * and corrected by the flash driver; this is harmless but may indicate that 982 * and corrected by the flash driver; this is harmless but may indicate that
979 * this eraseblock may become bad soon; 983 * this eraseblock may become bad soon;
980 * o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC 984 * o %UBI_IO_BAD_HDR if the volume identifier header is corrupted (a CRC
981 * error detected); 985 * error detected);
982 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID 986 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
983 * header there); 987 * header there);
@@ -1008,22 +1012,20 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1008 * CRC check-sum and we will identify this. If the VID header is 1012 * CRC check-sum and we will identify this. If the VID header is
1009 * still OK, we just report this as there was a bit-flip. 1013 * still OK, we just report this as there was a bit-flip.
1010 */ 1014 */
1011 read_err = err; 1015 if (err == -EBADMSG)
1016 read_err = UBI_IO_BAD_HDR_READ;
1012 } 1017 }
1013 1018
1014 magic = be32_to_cpu(vid_hdr->magic); 1019 magic = be32_to_cpu(vid_hdr->magic);
1015 if (magic != UBI_VID_HDR_MAGIC) { 1020 if (magic != UBI_VID_HDR_MAGIC) {
1021 if (read_err)
1022 return read_err;
1023
1016 /* 1024 /*
1017 * If we have read all 0xFF bytes, the VID header probably does 1025 * If we have read all 0xFF bytes, the VID header probably does
1018 * not exist and the physical eraseblock is assumed to be free. 1026 * not exist and the physical eraseblock is assumed to be free.
1019 *
1020 * But if there was a read error, we do not test the data for
1021 * 0xFFs. Even if it does contain all 0xFFs, this error
1022 * indicates that something is still wrong with this physical
1023 * eraseblock and it cannot be regarded as free.
1024 */ 1027 */
1025 if (read_err != -EBADMSG && 1028 if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1026 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1027 /* The physical eraseblock is supposedly free */ 1029 /* The physical eraseblock is supposedly free */
1028 if (verbose) 1030 if (verbose)
1029 ubi_warn("no VID header found at PEB %d, " 1031 ubi_warn("no VID header found at PEB %d, "
@@ -1045,7 +1047,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1045 } else if (UBI_IO_DEBUG) 1047 } else if (UBI_IO_DEBUG)
1046 dbg_msg("bad magic number at PEB %d: %08x instead of " 1048 dbg_msg("bad magic number at PEB %d: %08x instead of "
1047 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1049 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1048 return UBI_IO_BAD_VID_HDR; 1050 return UBI_IO_BAD_HDR;
1049 } 1051 }
1050 1052
1051 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 1053 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
@@ -1059,7 +1061,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1059 } else if (UBI_IO_DEBUG) 1061 } else if (UBI_IO_DEBUG)
1060 dbg_msg("bad CRC at PEB %d, calculated %#08x, " 1062 dbg_msg("bad CRC at PEB %d, calculated %#08x, "
1061 "read %#08x", pnum, crc, hdr_crc); 1063 "read %#08x", pnum, crc, hdr_crc);
1062 return UBI_IO_BAD_VID_HDR; 1064 return read_err ?: UBI_IO_BAD_HDR;
1063 } 1065 }
1064 1066
1065 /* Validate the VID header that we have just read */ 1067 /* Validate the VID header that we have just read */
@@ -1069,6 +1071,10 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1069 return -EINVAL; 1071 return -EINVAL;
1070 } 1072 }
1071 1073
1074 /*
1075 * If there was a read error (%-EBADMSG), but the header CRC is still
1076 * OK, report about a bit-flip to force scrubbing on this PEB.
1077 */
1072 return read_err ? UBI_IO_BITFLIPS : 0; 1078 return read_err ? UBI_IO_BITFLIPS : 0;
1073} 1079}
1074 1080
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index aed19f33b8f3..372a15ac9995 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -44,6 +44,7 @@
44#include <linux/slab.h> 44#include <linux/slab.h>
45#include <linux/crc32.h> 45#include <linux/crc32.h>
46#include <linux/math64.h> 46#include <linux/math64.h>
47#include <linux/random.h>
47#include "ubi.h" 48#include "ubi.h"
48 49
49#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 50#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
@@ -72,16 +73,19 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
72{ 73{
73 struct ubi_scan_leb *seb; 74 struct ubi_scan_leb *seb;
74 75
75 if (list == &si->free) 76 if (list == &si->free) {
76 dbg_bld("add to free: PEB %d, EC %d", pnum, ec); 77 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
77 else if (list == &si->erase) 78 si->free_peb_count += 1;
79 } else if (list == &si->erase) {
78 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); 80 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
79 else if (list == &si->corr) { 81 si->erase_peb_count += 1;
82 } else if (list == &si->corr) {
80 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); 83 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
81 si->corr_count += 1; 84 si->corr_peb_count += 1;
82 } else if (list == &si->alien) 85 } else if (list == &si->alien) {
83 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); 86 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
84 else 87 si->alien_peb_count += 1;
88 } else
85 BUG(); 89 BUG();
86 90
87 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); 91 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
@@ -517,6 +521,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
517 sv->leb_count += 1; 521 sv->leb_count += 1;
518 rb_link_node(&seb->u.rb, parent, p); 522 rb_link_node(&seb->u.rb, parent, p);
519 rb_insert_color(&seb->u.rb, &sv->root); 523 rb_insert_color(&seb->u.rb, &sv->root);
524 si->used_peb_count += 1;
520 return 0; 525 return 0;
521} 526}
522 527
@@ -745,19 +750,17 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
745 bitflips = 1; 750 bitflips = 1;
746 else if (err == UBI_IO_PEB_EMPTY) 751 else if (err == UBI_IO_PEB_EMPTY)
747 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);
748 else if (err == UBI_IO_BAD_EC_HDR) { 753 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) {
749 /* 754 /*
750 * We have to also look at the VID header, possibly it is not 755 * We have to also look at the VID header, possibly it is not
751 * corrupted. Set %bitflips flag in order to make this PEB be 756 * corrupted. Set %bitflips flag in order to make this PEB be
752 * moved and EC be re-created. 757 * moved and EC be re-created.
753 */ 758 */
754 ec_corr = 1; 759 ec_corr = err;
755 ec = UBI_SCAN_UNKNOWN_EC; 760 ec = UBI_SCAN_UNKNOWN_EC;
756 bitflips = 1; 761 bitflips = 1;
757 } 762 }
758 763
759 si->is_empty = 0;
760
761 if (!ec_corr) { 764 if (!ec_corr) {
762 int image_seq; 765 int image_seq;
763 766
@@ -813,9 +816,12 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
813 return err; 816 return err;
814 else if (err == UBI_IO_BITFLIPS) 817 else if (err == UBI_IO_BITFLIPS)
815 bitflips = 1; 818 bitflips = 1;
816 else if (err == UBI_IO_BAD_VID_HDR || 819 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR ||
817 (err == UBI_IO_PEB_FREE && ec_corr)) { 820 (err == UBI_IO_PEB_FREE && ec_corr)) {
818 /* VID header is corrupted */ 821 /* VID header is corrupted */
822 if (err == UBI_IO_BAD_HDR_READ ||
823 ec_corr == UBI_IO_BAD_HDR_READ)
824 si->read_err_count += 1;
819 err = add_to_list(si, pnum, ec, &si->corr); 825 err = add_to_list(si, pnum, ec, &si->corr);
820 if (err) 826 if (err)
821 return err; 827 return err;
@@ -836,11 +842,11 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
836 switch (vidh->compat) { 842 switch (vidh->compat) {
837 case UBI_COMPAT_DELETE: 843 case UBI_COMPAT_DELETE:
838 ubi_msg("\"delete\" compatible internal volume %d:%d" 844 ubi_msg("\"delete\" compatible internal volume %d:%d"
839 " found, remove it", vol_id, lnum); 845 " found, will remove it", vol_id, lnum);
840 err = add_to_list(si, pnum, ec, &si->corr); 846 err = add_to_list(si, pnum, ec, &si->corr);
841 if (err) 847 if (err)
842 return err; 848 return err;
843 break; 849 return 0;
844 850
845 case UBI_COMPAT_RO: 851 case UBI_COMPAT_RO:
846 ubi_msg("read-only compatible internal volume %d:%d" 852 ubi_msg("read-only compatible internal volume %d:%d"
@@ -855,7 +861,6 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
855 err = add_to_list(si, pnum, ec, &si->alien); 861 err = add_to_list(si, pnum, ec, &si->alien);
856 if (err) 862 if (err)
857 return err; 863 return err;
858 si->alien_peb_count += 1;
859 return 0; 864 return 0;
860 865
861 case UBI_COMPAT_REJECT: 866 case UBI_COMPAT_REJECT:
@@ -886,6 +891,85 @@ adjust_mean_ec:
886} 891}
887 892
888/** 893/**
894 * check_what_we_have - check what PEB were found by scanning.
895 * @ubi: UBI device description object
896 * @si: scanning information
897 *
898 * This is a helper function which takes a look what PEBs were found by
899 * scanning, and decides whether the flash is empty and should be formatted and
900 * whether there are too many corrupted PEBs and we should not attach this
901 * MTD device. Returns zero if we should proceed with attaching the MTD device,
902 * and %-EINVAL if we should not.
903 */
904static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
905{
906 struct ubi_scan_leb *seb;
907 int max_corr;
908
909 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
910 max_corr = max_corr / 20 ?: 8;
911
912 /*
913 * Few corrupted PEBs are not a problem and may be just a result of
914 * unclean reboots. However, many of them may indicate some problems
915 * with the flash HW or driver.
916 */
917 if (si->corr_peb_count >= 8) {
918 ubi_warn("%d PEBs are corrupted", si->corr_peb_count);
919 printk(KERN_WARNING "corrupted PEBs are:");
920 list_for_each_entry(seb, &si->corr, u.list)
921 printk(KERN_CONT " %d", seb->pnum);
922 printk(KERN_CONT "\n");
923
924 /*
925 * If too many PEBs are corrupted, we refuse attaching,
926 * otherwise, only print a warning.
927 */
928 if (si->corr_peb_count >= max_corr) {
929 ubi_err("too many corrupted PEBs, refusing this device");
930 return -EINVAL;
931 }
932 }
933
934 if (si->free_peb_count + si->used_peb_count +
935 si->alien_peb_count == 0) {
936 /* No UBI-formatted eraseblocks were found */
937 if (si->corr_peb_count == si->read_err_count &&
938 si->corr_peb_count < 8) {
939 /* No or just few corrupted PEBs, and all of them had a
940 * read error. We assume that those are bad PEBs, which
941 * were just not marked as bad so far.
942 *
943 * This piece of code basically tries to distinguish
944 * between the following 2 situations:
945 *
946 * 1. Flash is empty, but there are few bad PEBs, which
947 * are not marked as bad so far, and which were read
948 * with error. We want to go ahead and format this
949 * flash. While formating, the faulty PEBs will
950 * probably be marked as bad.
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;
958 ubi_msg("empty MTD device detected");
959 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq));
960 } else {
961 ubi_err("MTD device possibly contains non-UBI data, "
962 "refusing it");
963 return -EINVAL;
964 }
965 }
966
967 if (si->corr_peb_count > 0)
968 ubi_msg("corrupted PEBs will be formatted");
969 return 0;
970}
971
972/**
889 * ubi_scan - scan an MTD device. 973 * ubi_scan - scan an MTD device.
890 * @ubi: UBI device description object 974 * @ubi: UBI device description object
891 * 975 *
@@ -909,7 +993,6 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
909 INIT_LIST_HEAD(&si->erase); 993 INIT_LIST_HEAD(&si->erase);
910 INIT_LIST_HEAD(&si->alien); 994 INIT_LIST_HEAD(&si->alien);
911 si->volumes = RB_ROOT; 995 si->volumes = RB_ROOT;
912 si->is_empty = 1;
913 996
914 err = -ENOMEM; 997 err = -ENOMEM;
915 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 998 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
@@ -935,21 +1018,9 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
935 if (si->ec_count) 1018 if (si->ec_count)
936 si->mean_ec = div_u64(si->ec_sum, si->ec_count); 1019 si->mean_ec = div_u64(si->ec_sum, si->ec_count);
937 1020
938 if (si->is_empty) 1021 err = check_what_we_have(ubi, si);
939 ubi_msg("empty MTD device detected"); 1022 if (err)
940 1023 goto out_vidh;
941 /*
942 * Few corrupted PEBs are not a problem and may be just a result of
943 * unclean reboots. However, many of them may indicate some problems
944 * with the flash HW or driver. Print a warning in this case.
945 */
946 if (si->corr_count >= 8 || si->corr_count >= ubi->peb_count / 4) {
947 ubi_warn("%d PEBs are corrupted", si->corr_count);
948 printk(KERN_WARNING "corrupted PEBs are:");
949 list_for_each_entry(seb, &si->corr, u.list)
950 printk(KERN_CONT " %d", seb->pnum);
951 printk(KERN_CONT "\n");
952 }
953 1024
954 /* 1025 /*
955 * In case of unknown erase counter we use the mean erase counter 1026 * In case of unknown erase counter we use the mean erase counter
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
index ff179ad7ca55..2576a8d1532b 100644
--- a/drivers/mtd/ubi/scan.h
+++ b/drivers/mtd/ubi/scan.h
@@ -91,10 +91,16 @@ struct ubi_scan_volume {
91 * @erase: list of physical eraseblocks which have to be erased 91 * @erase: list of physical eraseblocks which have to be erased
92 * @alien: list of physical eraseblocks which should not be used by UBI (e.g., 92 * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
93 * those belonging to "preserve"-compatible internal volumes) 93 * those belonging to "preserve"-compatible internal volumes)
94 * @used_peb_count: count of used PEBs
95 * @corr_peb_count: count of PEBs in the @corr list
96 * @read_err_count: count of PEBs read with error (%UBI_IO_BAD_HDR_READ was
97 * returned)
98 * @free_peb_count: count of PEBs in the @free list
99 * @erase_peb_count: count of PEBs in the @erase list
100 * @alien_peb_count: count of PEBs in the @alien list
94 * @bad_peb_count: count of bad physical eraseblocks 101 * @bad_peb_count: count of bad physical eraseblocks
95 * @vols_found: number of volumes found during scanning 102 * @vols_found: number of volumes found during scanning
96 * @highest_vol_id: highest volume ID 103 * @highest_vol_id: highest volume ID
97 * @alien_peb_count: count of physical eraseblocks in the @alien list
98 * @is_empty: flag indicating whether the MTD device is empty or not 104 * @is_empty: flag indicating whether the MTD device is empty or not
99 * @min_ec: lowest erase counter value 105 * @min_ec: lowest erase counter value
100 * @max_ec: highest erase counter value 106 * @max_ec: highest erase counter value
@@ -102,7 +108,6 @@ struct ubi_scan_volume {
102 * @mean_ec: mean erase counter value 108 * @mean_ec: mean erase counter value
103 * @ec_sum: a temporary variable used when calculating @mean_ec 109 * @ec_sum: a temporary variable used when calculating @mean_ec
104 * @ec_count: a temporary variable used when calculating @mean_ec 110 * @ec_count: a temporary variable used when calculating @mean_ec
105 * @corr_count: count of corrupted PEBs
106 * 111 *
107 * This data structure contains the result of scanning and may be used by other 112 * This data structure contains the result of scanning and may be used by other
108 * UBI sub-systems to build final UBI data structures, further error-recovery 113 * UBI sub-systems to build final UBI data structures, further error-recovery
@@ -114,10 +119,15 @@ struct ubi_scan_info {
114 struct list_head free; 119 struct list_head free;
115 struct list_head erase; 120 struct list_head erase;
116 struct list_head alien; 121 struct list_head alien;
122 int used_peb_count;
123 int corr_peb_count;
124 int read_err_count;
125 int free_peb_count;
126 int erase_peb_count;
127 int alien_peb_count;
117 int bad_peb_count; 128 int bad_peb_count;
118 int vols_found; 129 int vols_found;
119 int highest_vol_id; 130 int highest_vol_id;
120 int alien_peb_count;
121 int is_empty; 131 int is_empty;
122 int min_ec; 132 int min_ec;
123 int max_ec; 133 int max_ec;
@@ -125,7 +135,6 @@ struct ubi_scan_info {
125 int mean_ec; 135 int mean_ec;
126 uint64_t ec_sum; 136 uint64_t ec_sum;
127 int ec_count; 137 int ec_count;
128 int corr_count;
129}; 138};
130 139
131struct ubi_device; 140struct ubi_device;
@@ -135,7 +144,7 @@ struct ubi_vid_hdr;
135 * ubi_scan_move_to_list - move a PEB from the volume tree to a list. 144 * ubi_scan_move_to_list - move a PEB from the volume tree to a list.
136 * 145 *
137 * @sv: volume scanning information 146 * @sv: volume scanning information
138 * @seb: scanning eraseblock infprmation 147 * @seb: scanning eraseblock information
139 * @list: the list to move to 148 * @list: the list to move to
140 */ 149 */
141static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv, 150static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv,
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index a637f0283add..0359e0cce482 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -89,16 +89,16 @@
89 * %0xFF bytes 89 * %0xFF bytes
90 * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a 90 * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a
91 * valid erase counter header, and the rest are %0xFF bytes 91 * valid erase counter header, and the rest are %0xFF bytes
92 * UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC) 92 * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
93 * UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or 93 * UBI_IO_BAD_HDR_READ: the same as %UBI_IO_BAD_HDR, but also there was a read
94 * CRC) 94 * error reported by the flash driver
95 * UBI_IO_BITFLIPS: bit-flips were detected and corrected 95 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
96 */ 96 */
97enum { 97enum {
98 UBI_IO_PEB_EMPTY = 1, 98 UBI_IO_PEB_EMPTY = 1,
99 UBI_IO_PEB_FREE, 99 UBI_IO_PEB_FREE,
100 UBI_IO_BAD_EC_HDR, 100 UBI_IO_BAD_HDR,
101 UBI_IO_BAD_VID_HDR, 101 UBI_IO_BAD_HDR_READ,
102 UBI_IO_BITFLIPS 102 UBI_IO_BITFLIPS
103}; 103};
104 104