aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-10-22 19:34:23 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-10-22 19:34:23 -0400
commit4f3a29dadaf999a273f1e7fe2476595d0283eef3 (patch)
tree3776499f6edc95a36533c7efe32a7b6038c88b61
parent06d362931a530e0d48c1a9554a752da4ed240f0b (diff)
parent45aafd32996e27bfc4862654ff31231bdddbe200 (diff)
Merge branch 'linux-next' of git://git.infradead.org/ubi-2.6
* 'linux-next' of git://git.infradead.org/ubi-2.6: UBI: tighten the corrupted PEB criteria UBI: fix check_data_ff return code UBI: remember copy_flag while scanning UBI: preserve corrupted PEBs UBI: add truly corrupted PEBs to corrupted list UBI: introduce debugging helper function UBI: make check_pattern function non-static UBI: do not put eraseblocks to the corrupted list unnecessarily UBI: separate out corrupted list UBI: change cascade of ifs to switch statements UBI: rename a local variable UBI: handle bit-flips when no header found UBI: remove duplicate IO error codes UBI: rename IO error code UBI: fix small 80 characters limit style issue UBI: cleanup and simplify Kconfig
-rw-r--r--drivers/mtd/ubi/Kconfig17
-rw-r--r--drivers/mtd/ubi/Kconfig.debug29
-rw-r--r--drivers/mtd/ubi/build.c6
-rw-r--r--drivers/mtd/ubi/debug.h4
-rw-r--r--drivers/mtd/ubi/eba.c10
-rw-r--r--drivers/mtd/ubi/io.c138
-rw-r--r--drivers/mtd/ubi/misc.c19
-rw-r--r--drivers/mtd/ubi/scan.c387
-rw-r--r--drivers/mtd/ubi/scan.h19
-rw-r--r--drivers/mtd/ubi/ubi.h29
-rw-r--r--drivers/mtd/ubi/vmt.c6
-rw-r--r--drivers/mtd/ubi/vtbl.c10
-rw-r--r--drivers/mtd/ubi/wl.c31
13 files changed, 419 insertions, 286 deletions
diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig
index f702a163d8df..3cf193fb5e00 100644
--- a/drivers/mtd/ubi/Kconfig
+++ b/drivers/mtd/ubi/Kconfig
@@ -1,9 +1,5 @@
1menu "UBI - Unsorted block images" 1menuconfig MTD_UBI
2 depends on MTD 2 tristate "Enable UBI - Unsorted block images"
3
4config MTD_UBI
5 tristate "Enable UBI"
6 depends on MTD
7 select CRC32 3 select CRC32
8 help 4 help
9 UBI is a software layer above MTD layer which admits of LVM-like 5 UBI is a software layer above MTD layer which admits of LVM-like
@@ -12,11 +8,12 @@ config MTD_UBI
12 capabilities. Please, consult the MTD web site for more details 8 capabilities. Please, consult the MTD web site for more details
13 (www.linux-mtd.infradead.org). 9 (www.linux-mtd.infradead.org).
14 10
11if MTD_UBI
12
15config MTD_UBI_WL_THRESHOLD 13config MTD_UBI_WL_THRESHOLD
16 int "UBI wear-leveling threshold" 14 int "UBI wear-leveling threshold"
17 default 4096 15 default 4096
18 range 2 65536 16 range 2 65536
19 depends on MTD_UBI
20 help 17 help
21 This parameter defines the maximum difference between the highest 18 This parameter defines the maximum difference between the highest
22 erase counter value and the lowest erase counter value of eraseblocks 19 erase counter value and the lowest erase counter value of eraseblocks
@@ -34,7 +31,6 @@ config MTD_UBI_BEB_RESERVE
34 int "Percentage of reserved eraseblocks for bad eraseblocks handling" 31 int "Percentage of reserved eraseblocks for bad eraseblocks handling"
35 default 1 32 default 1
36 range 0 25 33 range 0 25
37 depends on MTD_UBI
38 help 34 help
39 If the MTD device admits of bad eraseblocks (e.g. NAND flash), UBI 35 If the MTD device admits of bad eraseblocks (e.g. NAND flash), UBI
40 reserves some amount of physical eraseblocks to handle new bad 36 reserves some amount of physical eraseblocks to handle new bad
@@ -48,8 +44,6 @@ config MTD_UBI_BEB_RESERVE
48 44
49config MTD_UBI_GLUEBI 45config MTD_UBI_GLUEBI
50 tristate "MTD devices emulation driver (gluebi)" 46 tristate "MTD devices emulation driver (gluebi)"
51 default n
52 depends on MTD_UBI
53 help 47 help
54 This option enables gluebi - an additional driver which emulates MTD 48 This option enables gluebi - an additional driver which emulates MTD
55 devices on top of UBI volumes: for each UBI volumes an MTD device is 49 devices on top of UBI volumes: for each UBI volumes an MTD device is
@@ -59,4 +53,5 @@ config MTD_UBI_GLUEBI
59 software. 53 software.
60 54
61source "drivers/mtd/ubi/Kconfig.debug" 55source "drivers/mtd/ubi/Kconfig.debug"
62endmenu 56
57endif # MTD_UBI
diff --git a/drivers/mtd/ubi/Kconfig.debug b/drivers/mtd/ubi/Kconfig.debug
index 61f6e5e40458..fad4adc0fe2c 100644
--- a/drivers/mtd/ubi/Kconfig.debug
+++ b/drivers/mtd/ubi/Kconfig.debug
@@ -1,94 +1,73 @@
1comment "UBI debugging options" 1comment "UBI debugging options"
2 depends on MTD_UBI
3 2
4config MTD_UBI_DEBUG 3config MTD_UBI_DEBUG
5 bool "UBI debugging" 4 bool "UBI debugging"
6 depends on SYSFS 5 depends on SYSFS
7 depends on MTD_UBI
8 select DEBUG_FS 6 select DEBUG_FS
9 select KALLSYMS_ALL if KALLSYMS && DEBUG_KERNEL 7 select KALLSYMS_ALL if KALLSYMS && DEBUG_KERNEL
10 help 8 help
11 This option enables UBI debugging. 9 This option enables UBI debugging.
12 10
11if MTD_UBI_DEBUG
12
13config MTD_UBI_DEBUG_MSG 13config MTD_UBI_DEBUG_MSG
14 bool "UBI debugging messages" 14 bool "UBI debugging messages"
15 depends on MTD_UBI_DEBUG
16 default n
17 help 15 help
18 This option enables UBI debugging messages. 16 This option enables UBI debugging messages.
19 17
20config MTD_UBI_DEBUG_PARANOID 18config MTD_UBI_DEBUG_PARANOID
21 bool "Extra self-checks" 19 bool "Extra self-checks"
22 default n
23 depends on MTD_UBI_DEBUG
24 help 20 help
25 This option enables extra checks in UBI code. Note this slows UBI down 21 This option enables extra checks in UBI code. Note this slows UBI down
26 significantly. 22 significantly.
27 23
28config MTD_UBI_DEBUG_DISABLE_BGT 24config MTD_UBI_DEBUG_DISABLE_BGT
29 bool "Do not enable the UBI background thread" 25 bool "Do not enable the UBI background thread"
30 depends on MTD_UBI_DEBUG
31 default n
32 help 26 help
33 This option switches the background thread off by default. The thread 27 This option switches the background thread off by default. The thread
34 may be also be enabled/disabled via UBI sysfs. 28 may be also be enabled/disabled via UBI sysfs.
35 29
36config MTD_UBI_DEBUG_EMULATE_BITFLIPS 30config MTD_UBI_DEBUG_EMULATE_BITFLIPS
37 bool "Emulate flash bit-flips" 31 bool "Emulate flash bit-flips"
38 depends on MTD_UBI_DEBUG
39 default n
40 help 32 help
41 This option emulates bit-flips with probability 1/50, which in turn 33 This option emulates bit-flips with probability 1/50, which in turn
42 causes scrubbing. Useful for debugging and stressing UBI. 34 causes scrubbing. Useful for debugging and stressing UBI.
43 35
44config MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES 36config MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES
45 bool "Emulate flash write failures" 37 bool "Emulate flash write failures"
46 depends on MTD_UBI_DEBUG
47 default n
48 help 38 help
49 This option emulates write failures with probability 1/100. Useful for 39 This option emulates write failures with probability 1/100. Useful for
50 debugging and testing how UBI handlines errors. 40 debugging and testing how UBI handlines errors.
51 41
52config MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES 42config MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES
53 bool "Emulate flash erase failures" 43 bool "Emulate flash erase failures"
54 depends on MTD_UBI_DEBUG
55 default n
56 help 44 help
57 This option emulates erase failures with probability 1/100. Useful for 45 This option emulates erase failures with probability 1/100. Useful for
58 debugging and testing how UBI handlines errors. 46 debugging and testing how UBI handlines errors.
59 47
60menu "Additional UBI debugging messages" 48comment "Additional UBI debugging messages"
61 depends on MTD_UBI_DEBUG
62 49
63config MTD_UBI_DEBUG_MSG_BLD 50config MTD_UBI_DEBUG_MSG_BLD
64 bool "Additional UBI initialization and build messages" 51 bool "Additional UBI initialization and build messages"
65 default n
66 depends on MTD_UBI_DEBUG
67 help 52 help
68 This option enables detailed UBI initialization and device build 53 This option enables detailed UBI initialization and device build
69 debugging messages. 54 debugging messages.
70 55
71config MTD_UBI_DEBUG_MSG_EBA 56config MTD_UBI_DEBUG_MSG_EBA
72 bool "Eraseblock association unit messages" 57 bool "Eraseblock association unit messages"
73 default n
74 depends on MTD_UBI_DEBUG
75 help 58 help
76 This option enables debugging messages from the UBI eraseblock 59 This option enables debugging messages from the UBI eraseblock
77 association unit. 60 association unit.
78 61
79config MTD_UBI_DEBUG_MSG_WL 62config MTD_UBI_DEBUG_MSG_WL
80 bool "Wear-leveling unit messages" 63 bool "Wear-leveling unit messages"
81 default n
82 depends on MTD_UBI_DEBUG
83 help 64 help
84 This option enables debugging messages from the UBI wear-leveling 65 This option enables debugging messages from the UBI wear-leveling
85 unit. 66 unit.
86 67
87config MTD_UBI_DEBUG_MSG_IO 68config MTD_UBI_DEBUG_MSG_IO
88 bool "Input/output unit messages" 69 bool "Input/output unit messages"
89 default n
90 depends on MTD_UBI_DEBUG
91 help 70 help
92 This option enables debugging messages from the UBI input/output unit. 71 This option enables debugging messages from the UBI input/output unit.
93 72
94endmenu # UBI debugging messages 73endif # MTD_UBI_DEBUG
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 78ae89488a4f..5ebe280225d6 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -95,8 +95,8 @@ DEFINE_MUTEX(ubi_devices_mutex);
95static DEFINE_SPINLOCK(ubi_devices_lock); 95static DEFINE_SPINLOCK(ubi_devices_lock);
96 96
97/* "Show" method for files in '/<sysfs>/class/ubi/' */ 97/* "Show" method for files in '/<sysfs>/class/ubi/' */
98static ssize_t ubi_version_show(struct class *class, struct class_attribute *attr, 98static ssize_t ubi_version_show(struct class *class,
99 char *buf) 99 struct class_attribute *attr, char *buf)
100{ 100{
101 return sprintf(buf, "%d\n", UBI_VERSION); 101 return sprintf(buf, "%d\n", UBI_VERSION);
102} 102}
@@ -591,6 +591,7 @@ static int attach_by_scanning(struct ubi_device *ubi)
591 591
592 ubi->bad_peb_count = si->bad_peb_count; 592 ubi->bad_peb_count = si->bad_peb_count;
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->corr_peb_count = si->corr_peb_count;
594 ubi->max_ec = si->max_ec; 595 ubi->max_ec = si->max_ec;
595 ubi->mean_ec = si->mean_ec; 596 ubi->mean_ec = si->mean_ec;
596 ubi_msg("max. sequence number: %llu", si->max_sqnum); 597 ubi_msg("max. sequence number: %llu", si->max_sqnum);
@@ -972,6 +973,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
972 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); 973 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
973 ubi_msg("number of good PEBs: %d", ubi->good_peb_count); 974 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
974 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); 975 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
976 ubi_msg("number of corrupted PEBs: %d", ubi->corr_peb_count);
975 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); 977 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
976 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); 978 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
977 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); 979 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index 17a107129726..9eca95074bc2 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -57,6 +57,9 @@ void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb, int type);
57void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req); 57void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req);
58void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len); 58void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
59 59
60#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
61 print_hex_dump(l, ps, pt, r, g, b, len, a)
62
60#ifdef CONFIG_MTD_UBI_DEBUG_MSG 63#ifdef CONFIG_MTD_UBI_DEBUG_MSG
61/* General debugging messages */ 64/* General debugging messages */
62#define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 65#define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
@@ -172,6 +175,7 @@ static inline int ubi_dbg_is_erase_failure(void)
172#define ubi_dbg_dump_seb(seb, type) ({}) 175#define ubi_dbg_dump_seb(seb, type) ({})
173#define ubi_dbg_dump_mkvol_req(req) ({}) 176#define ubi_dbg_dump_mkvol_req(req) ({})
174#define ubi_dbg_dump_flash(ubi, pnum, offset, len) ({}) 177#define ubi_dbg_dump_flash(ubi, pnum, offset, len) ({})
178#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) ({})
175 179
176#define UBI_IO_DEBUG 0 180#define UBI_IO_DEBUG 0
177#define DBG_DISABLE_BGT 0 181#define DBG_DISABLE_BGT 0
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c
index fe74749e0dae..4be671815014 100644
--- a/drivers/mtd/ubi/eba.c
+++ b/drivers/mtd/ubi/eba.c
@@ -418,7 +418,7 @@ 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_HDR_READ || 421 if (err == UBI_IO_BAD_HDR_EBADMSG ||
422 err == UBI_IO_BAD_HDR) { 422 err == UBI_IO_BAD_HDR) {
423 ubi_warn("corrupted VID header at PEB " 423 ubi_warn("corrupted VID header at PEB "
424 "%d, LEB %d:%d", pnum, vol_id, 424 "%d, LEB %d:%d", pnum, vol_id,
@@ -963,7 +963,7 @@ write_error:
963static int is_error_sane(int err) 963static int is_error_sane(int err)
964{ 964{
965 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR || 965 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
966 err == UBI_IO_BAD_HDR_READ || err == -ETIMEDOUT) 966 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT)
967 return 0; 967 return 0;
968 return 1; 968 return 1;
969} 969}
@@ -1201,6 +1201,9 @@ static void print_rsvd_warning(struct ubi_device *ubi,
1201 1201
1202 ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d," 1202 ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d,"
1203 " need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); 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/**
@@ -1263,6 +1266,9 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1263 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { 1266 if (ubi->avail_pebs < EBA_RESERVED_PEBS) {
1264 ubi_err("no enough physical eraseblocks (%d, need %d)", 1267 ubi_err("no enough physical eraseblocks (%d, need %d)",
1265 ubi->avail_pebs, EBA_RESERVED_PEBS); 1268 ubi->avail_pebs, EBA_RESERVED_PEBS);
1269 if (ubi->corr_peb_count)
1270 ubi_err("%d PEBs are corrupted and not used",
1271 ubi->corr_peb_count);
1266 err = -ENOSPC; 1272 err = -ENOSPC;
1267 goto out_free; 1273 goto out_free;
1268 } 1274 }
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 332f992f13d9..c2960ac9f39c 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -376,25 +376,6 @@ retry:
376 return 0; 376 return 0;
377} 377}
378 378
379/**
380 * check_pattern - check if buffer contains only a certain byte pattern.
381 * @buf: buffer to check
382 * @patt: the pattern to check
383 * @size: buffer size in bytes
384 *
385 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
386 * something else was also found.
387 */
388static int check_pattern(const void *buf, uint8_t patt, int size)
389{
390 int i;
391
392 for (i = 0; i < size; i++)
393 if (((const uint8_t *)buf)[i] != patt)
394 return 0;
395 return 1;
396}
397
398/* Patterns to write to a physical eraseblock when torturing it */ 379/* Patterns to write to a physical eraseblock when torturing it */
399static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 380static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
400 381
@@ -426,7 +407,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
426 if (err) 407 if (err)
427 goto out; 408 goto out;
428 409
429 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 410 err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
430 if (err == 0) { 411 if (err == 0) {
431 ubi_err("erased PEB %d, but a non-0xFF byte found", 412 ubi_err("erased PEB %d, but a non-0xFF byte found",
432 pnum); 413 pnum);
@@ -445,7 +426,8 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
445 if (err) 426 if (err)
446 goto out; 427 goto out;
447 428
448 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); 429 err = ubi_check_pattern(ubi->peb_buf1, patterns[i],
430 ubi->peb_size);
449 if (err == 0) { 431 if (err == 0) {
450 ubi_err("pattern %x checking failed for PEB %d", 432 ubi_err("pattern %x checking failed for PEB %d",
451 patterns[i], pnum); 433 patterns[i], pnum);
@@ -517,7 +499,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
517 * In this case we probably anyway have garbage in this PEB. 499 * In this case we probably anyway have garbage in this PEB.
518 */ 500 */
519 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 501 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
520 if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR) 502 if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR)
521 /* 503 /*
522 * The VID header is corrupted, so we can safely erase this 504 * The VID header is corrupted, so we can safely erase this
523 * PEB and not afraid that it will be treated as a valid PEB in 505 * PEB and not afraid that it will be treated as a valid PEB in
@@ -712,47 +694,47 @@ bad:
712 * and corrected by the flash driver; this is harmless but may indicate that 694 * and corrected by the flash driver; this is harmless but may indicate that
713 * this eraseblock may become bad soon (but may be not); 695 * this eraseblock may become bad soon (but may be not);
714 * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error); 696 * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
715 * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; 697 * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
698 * a data integrity error (uncorrectable ECC error in case of NAND);
699 * o %UBI_IO_FF if only 0xFF bytes were read (the PEB is supposedly empty)
716 * o a negative error code in case of failure. 700 * o a negative error code in case of failure.
717 */ 701 */
718int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 702int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
719 struct ubi_ec_hdr *ec_hdr, int verbose) 703 struct ubi_ec_hdr *ec_hdr, int verbose)
720{ 704{
721 int err, read_err = 0; 705 int err, read_err;
722 uint32_t crc, magic, hdr_crc; 706 uint32_t crc, magic, hdr_crc;
723 707
724 dbg_io("read EC header from PEB %d", pnum); 708 dbg_io("read EC header from PEB %d", pnum);
725 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 709 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
726 710
727 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 711 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
728 if (err) { 712 if (read_err) {
729 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 713 if (read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
730 return err; 714 return read_err;
731 715
732 /* 716 /*
733 * We read all the data, but either a correctable bit-flip 717 * We read all the data, but either a correctable bit-flip
734 * occurred, or MTD reported about some data integrity error, 718 * occurred, or MTD reported a data integrity error
735 * like an ECC error in case of NAND. The former is harmless, 719 * (uncorrectable ECC error in case of NAND). The former is
736 * the later may mean that the read data is corrupted. But we 720 * harmless, the later may mean that the read data is
737 * have a CRC check-sum and we will detect this. If the EC 721 * corrupted. But we have a CRC check-sum and we will detect
738 * header is still OK, we just report this as there was a 722 * this. If the EC header is still OK, we just report this as
739 * bit-flip. 723 * there was a bit-flip, to force scrubbing.
740 */ 724 */
741 if (err == -EBADMSG)
742 read_err = UBI_IO_BAD_HDR_READ;
743 } 725 }
744 726
745 magic = be32_to_cpu(ec_hdr->magic); 727 magic = be32_to_cpu(ec_hdr->magic);
746 if (magic != UBI_EC_HDR_MAGIC) { 728 if (magic != UBI_EC_HDR_MAGIC) {
747 if (read_err) 729 if (read_err == -EBADMSG)
748 return read_err; 730 return UBI_IO_BAD_HDR_EBADMSG;
749 731
750 /* 732 /*
751 * The magic field is wrong. Let's check if we have read all 733 * The magic field is wrong. Let's check if we have read all
752 * 0xFF. If yes, this physical eraseblock is assumed to be 734 * 0xFF. If yes, this physical eraseblock is assumed to be
753 * empty. 735 * empty.
754 */ 736 */
755 if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 737 if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
756 /* The physical eraseblock is supposedly empty */ 738 /* The physical eraseblock is supposedly empty */
757 if (verbose) 739 if (verbose)
758 ubi_warn("no EC header found at PEB %d, " 740 ubi_warn("no EC header found at PEB %d, "
@@ -760,7 +742,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
760 else if (UBI_IO_DEBUG) 742 else if (UBI_IO_DEBUG)
761 dbg_msg("no EC header found at PEB %d, " 743 dbg_msg("no EC header found at PEB %d, "
762 "only 0xFF bytes", pnum); 744 "only 0xFF bytes", pnum);
763 return UBI_IO_PEB_EMPTY; 745 if (!read_err)
746 return UBI_IO_FF;
747 else
748 return UBI_IO_FF_BITFLIPS;
764 } 749 }
765 750
766 /* 751 /*
@@ -788,7 +773,11 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
788 } else if (UBI_IO_DEBUG) 773 } else if (UBI_IO_DEBUG)
789 dbg_msg("bad EC header CRC at PEB %d, calculated " 774 dbg_msg("bad EC header CRC at PEB %d, calculated "
790 "%#08x, read %#08x", pnum, crc, hdr_crc); 775 "%#08x, read %#08x", pnum, crc, hdr_crc);
791 return read_err ?: UBI_IO_BAD_HDR; 776
777 if (!read_err)
778 return UBI_IO_BAD_HDR;
779 else
780 return UBI_IO_BAD_HDR_EBADMSG;
792 } 781 }
793 782
794 /* And of course validate what has just been read from the media */ 783 /* And of course validate what has just been read from the media */
@@ -975,22 +964,16 @@ bad:
975 * 964 *
976 * This function reads the volume identifier header from physical eraseblock 965 * This function reads the volume identifier header from physical eraseblock
977 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 966 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read
978 * volume identifier header. The following codes may be returned: 967 * volume identifier header. The error codes are the same as in
968 * 'ubi_io_read_ec_hdr()'.
979 * 969 *
980 * o %0 if the CRC checksum is correct and the header was successfully read; 970 * Note, the implementation of this function is also very similar to
981 * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected 971 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
982 * and corrected by the flash driver; this is harmless but may indicate that
983 * this eraseblock may become bad soon;
984 * o %UBI_IO_BAD_HDR if the volume identifier header is corrupted (a CRC
985 * error detected);
986 * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
987 * header there);
988 * o a negative error code in case of failure.
989 */ 972 */
990int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 973int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
991 struct ubi_vid_hdr *vid_hdr, int verbose) 974 struct ubi_vid_hdr *vid_hdr, int verbose)
992{ 975{
993 int err, read_err = 0; 976 int err, read_err;
994 uint32_t crc, magic, hdr_crc; 977 uint32_t crc, magic, hdr_crc;
995 void *p; 978 void *p;
996 979
@@ -998,48 +981,29 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
998 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 981 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
999 982
1000 p = (char *)vid_hdr - ubi->vid_hdr_shift; 983 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1001 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 984 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1002 ubi->vid_hdr_alsize); 985 ubi->vid_hdr_alsize);
1003 if (err) { 986 if (read_err && read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
1004 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 987 return read_err;
1005 return err;
1006
1007 /*
1008 * We read all the data, but either a correctable bit-flip
1009 * occurred, or MTD reported about some data integrity error,
1010 * like an ECC error in case of NAND. The former is harmless,
1011 * the later may mean the read data is corrupted. But we have a
1012 * CRC check-sum and we will identify this. If the VID header is
1013 * still OK, we just report this as there was a bit-flip.
1014 */
1015 if (err == -EBADMSG)
1016 read_err = UBI_IO_BAD_HDR_READ;
1017 }
1018 988
1019 magic = be32_to_cpu(vid_hdr->magic); 989 magic = be32_to_cpu(vid_hdr->magic);
1020 if (magic != UBI_VID_HDR_MAGIC) { 990 if (magic != UBI_VID_HDR_MAGIC) {
1021 if (read_err) 991 if (read_err == -EBADMSG)
1022 return read_err; 992 return UBI_IO_BAD_HDR_EBADMSG;
1023 993
1024 /* 994 if (ubi_check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1025 * If we have read all 0xFF bytes, the VID header probably does
1026 * not exist and the physical eraseblock is assumed to be free.
1027 */
1028 if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
1029 /* The physical eraseblock is supposedly free */
1030 if (verbose) 995 if (verbose)
1031 ubi_warn("no VID header found at PEB %d, " 996 ubi_warn("no VID header found at PEB %d, "
1032 "only 0xFF bytes", pnum); 997 "only 0xFF bytes", pnum);
1033 else if (UBI_IO_DEBUG) 998 else if (UBI_IO_DEBUG)
1034 dbg_msg("no VID header found at PEB %d, " 999 dbg_msg("no VID header found at PEB %d, "
1035 "only 0xFF bytes", pnum); 1000 "only 0xFF bytes", pnum);
1036 return UBI_IO_PEB_FREE; 1001 if (!read_err)
1002 return UBI_IO_FF;
1003 else
1004 return UBI_IO_FF_BITFLIPS;
1037 } 1005 }
1038 1006
1039 /*
1040 * This is not a valid VID header, and these are not 0xFF
1041 * bytes. Report that the header is corrupted.
1042 */
1043 if (verbose) { 1007 if (verbose) {
1044 ubi_warn("bad magic number at PEB %d: %08x instead of " 1008 ubi_warn("bad magic number at PEB %d: %08x instead of "
1045 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1009 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
@@ -1061,20 +1025,18 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1061 } else if (UBI_IO_DEBUG) 1025 } else if (UBI_IO_DEBUG)
1062 dbg_msg("bad CRC at PEB %d, calculated %#08x, " 1026 dbg_msg("bad CRC at PEB %d, calculated %#08x, "
1063 "read %#08x", pnum, crc, hdr_crc); 1027 "read %#08x", pnum, crc, hdr_crc);
1064 return read_err ?: UBI_IO_BAD_HDR; 1028 if (!read_err)
1029 return UBI_IO_BAD_HDR;
1030 else
1031 return UBI_IO_BAD_HDR_EBADMSG;
1065 } 1032 }
1066 1033
1067 /* Validate the VID header that we have just read */
1068 err = validate_vid_hdr(ubi, vid_hdr); 1034 err = validate_vid_hdr(ubi, vid_hdr);
1069 if (err) { 1035 if (err) {
1070 ubi_err("validation failed for PEB %d", pnum); 1036 ubi_err("validation failed for PEB %d", pnum);
1071 return -EINVAL; 1037 return -EINVAL;
1072 } 1038 }
1073 1039
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 */
1078 return read_err ? UBI_IO_BITFLIPS : 0; 1040 return read_err ? UBI_IO_BITFLIPS : 0;
1079} 1041}
1080 1042
@@ -1383,7 +1345,7 @@ int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1383 goto error; 1345 goto error;
1384 } 1346 }
1385 1347
1386 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 1348 err = ubi_check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1387 if (err == 0) { 1349 if (err == 0) {
1388 ubi_err("flash region at PEB %d:%d, length %d does not " 1350 ubi_err("flash region at PEB %d:%d, length %d does not "
1389 "contain all 0xFF bytes", pnum, offset, len); 1351 "contain all 0xFF bytes", pnum, offset, len);
diff --git a/drivers/mtd/ubi/misc.c b/drivers/mtd/ubi/misc.c
index 22ad31402945..ff2a65c37f69 100644
--- a/drivers/mtd/ubi/misc.c
+++ b/drivers/mtd/ubi/misc.c
@@ -103,3 +103,22 @@ void ubi_calculate_reserved(struct ubi_device *ubi)
103 if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS) 103 if (ubi->beb_rsvd_level < MIN_RESEVED_PEBS)
104 ubi->beb_rsvd_level = MIN_RESEVED_PEBS; 104 ubi->beb_rsvd_level = MIN_RESEVED_PEBS;
105} 105}
106
107/**
108 * ubi_check_pattern - check if buffer contains only a certain byte pattern.
109 * @buf: buffer to check
110 * @patt: the pattern to check
111 * @size: buffer size in bytes
112 *
113 * This function returns %1 in there are only @patt bytes in @buf, and %0 if
114 * something else was also found.
115 */
116int ubi_check_pattern(const void *buf, uint8_t patt, int size)
117{
118 int i;
119
120 for (i = 0; i < size; i++)
121 if (((const uint8_t *)buf)[i] != patt)
122 return 0;
123 return 1;
124}
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 69b52e9c9489..3c631863bf40 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,33 @@
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 * UBI tries to distinguish between 2 types of corruptions.
43 * 1. Corruptions caused by power cuts. These are harmless and expected
44 * corruptions and UBI tries to handle them gracefully, without printing too
45 * many warnings and error messages. The idea is that we do not lose
46 * important data in these case - we may lose only the data which was being
47 * written to the media just before the power cut happened, and the upper
48 * layers (e.g., UBIFS) are supposed to handle these situations. UBI puts
49 * these PEBs to the head of the @erase list and they are scheduled for
50 * erasure.
51 *
52 * 2. Unexpected corruptions which are not caused by power cuts. During
53 * scanning, such PEBs are put to the @corr list and UBI preserves them.
54 * Obviously, this lessens the amount of available PEBs, and if at some
55 * point UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly
56 * informs about such PEBs every time the MTD device is attached.
57 *
58 * However, it is difficult to reliably distinguish between these types of
59 * corruptions and UBI's strategy is as follows. UBI assumes (2.) if the VID
60 * header is corrupted and the data area does not contain all 0xFFs, and there
61 * were not bit-flips or integrity errors while reading the data area. Otherwise
62 * UBI assumes (1.). The assumptions are:
63 * o if the data area contains only 0xFFs, there is no data, and it is safe
64 * to just erase this PEB.
65 * o if the data area has bit-flips and data integrity errors (ECC errors on
66 * NAND), it is probably a PEB which was being erased when power cut
67 * happened.
41 */ 68 */
42 69
43#include <linux/err.h> 70#include <linux/err.h>
@@ -62,26 +89,26 @@ static struct ubi_vid_hdr *vidh;
62 * @si: scanning information 89 * @si: scanning information
63 * @pnum: physical eraseblock number to add 90 * @pnum: physical eraseblock number to add
64 * @ec: erase counter of the physical eraseblock 91 * @ec: erase counter of the physical eraseblock
92 * @to_head: if not zero, add to the head of the list
65 * @list: the list to add to 93 * @list: the list to add to
66 * 94 *
67 * This function adds physical eraseblock @pnum to free, erase, corrupted or 95 * 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 96 * If @to_head is not zero, PEB will be added to the head of the list, which
69 * case of failure. 97 * basically means it will be processed first later. E.g., we add corrupted
98 * PEBs (corrupted due to power cuts) to the head of the erase list to make
99 * sure we erase them first and get rid of corruptions ASAP. This function
100 * returns zero in case of success and a negative error code in case of
101 * failure.
70 */ 102 */
71static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, 103static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
72 struct list_head *list) 104 struct list_head *list)
73{ 105{
74 struct ubi_scan_leb *seb; 106 struct ubi_scan_leb *seb;
75 107
76 if (list == &si->free) { 108 if (list == &si->free) {
77 dbg_bld("add to free: PEB %d, EC %d", pnum, ec); 109 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
78 si->free_peb_count += 1;
79 } else if (list == &si->erase) { 110 } else if (list == &si->erase) {
80 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); 111 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) { 112 } else if (list == &si->alien) {
86 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); 113 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
87 si->alien_peb_count += 1; 114 si->alien_peb_count += 1;
@@ -94,7 +121,37 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
94 121
95 seb->pnum = pnum; 122 seb->pnum = pnum;
96 seb->ec = ec; 123 seb->ec = ec;
97 list_add_tail(&seb->u.list, list); 124 if (to_head)
125 list_add(&seb->u.list, list);
126 else
127 list_add_tail(&seb->u.list, list);
128 return 0;
129}
130
131/**
132 * add_corrupted - add a corrupted physical eraseblock.
133 * @si: scanning information
134 * @pnum: physical eraseblock number to add
135 * @ec: erase counter of the physical eraseblock
136 *
137 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
138 * The corruption was presumably not caused by a power cut. Returns zero in
139 * case of success and a negative error code in case of failure.
140 */
141static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
142{
143 struct ubi_scan_leb *seb;
144
145 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
146
147 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
148 if (!seb)
149 return -ENOMEM;
150
151 si->corr_peb_count += 1;
152 seb->pnum = pnum;
153 seb->ec = ec;
154 list_add(&seb->u.list, &si->corr);
98 return 0; 155 return 0;
99} 156}
100 157
@@ -258,8 +315,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 315 * created before sequence numbers support has been added. At
259 * that times we used 32-bit LEB versions stored in logical 316 * that times we used 32-bit LEB versions stored in logical
260 * eraseblocks. That was before UBI got into mainline. We do not 317 * eraseblocks. That was before UBI got into mainline. We do not
261 * support these images anymore. Well, those images will work 318 * support these images anymore. Well, those images still work,
262 * still work, but only if no unclean reboots happened. 319 * but only if no unclean reboots happened.
263 */ 320 */
264 ubi_err("unsupported on-flash UBI format\n"); 321 ubi_err("unsupported on-flash UBI format\n");
265 return -EINVAL; 322 return -EINVAL;
@@ -285,19 +342,25 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
285 return 1; 342 return 1;
286 } 343 }
287 } else { 344 } else {
288 pnum = seb->pnum; 345 if (!seb->copy_flag) {
346 /* It is not a copy, so it is newer */
347 dbg_bld("first PEB %d is newer, copy_flag is unset",
348 pnum);
349 return bitflips << 1;
350 }
289 351
290 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 352 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
291 if (!vh) 353 if (!vh)
292 return -ENOMEM; 354 return -ENOMEM;
293 355
356 pnum = seb->pnum;
294 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 357 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
295 if (err) { 358 if (err) {
296 if (err == UBI_IO_BITFLIPS) 359 if (err == UBI_IO_BITFLIPS)
297 bitflips = 1; 360 bitflips = 1;
298 else { 361 else {
299 dbg_err("VID of PEB %d header is bad, but it " 362 dbg_err("VID of PEB %d header is bad, but it "
300 "was OK earlier", pnum); 363 "was OK earlier, err %d", pnum, err);
301 if (err > 0) 364 if (err > 0)
302 err = -EIO; 365 err = -EIO;
303 366
@@ -305,14 +368,6 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
305 } 368 }
306 } 369 }
307 370
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; 371 vid_hdr = vh;
317 } 372 }
318 373
@@ -463,18 +518,15 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
463 if (err) 518 if (err)
464 return err; 519 return err;
465 520
466 if (cmp_res & 4) 521 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
467 err = add_to_list(si, seb->pnum, seb->ec, 522 &si->erase);
468 &si->corr);
469 else
470 err = add_to_list(si, seb->pnum, seb->ec,
471 &si->erase);
472 if (err) 523 if (err)
473 return err; 524 return err;
474 525
475 seb->ec = ec; 526 seb->ec = ec;
476 seb->pnum = pnum; 527 seb->pnum = pnum;
477 seb->scrub = ((cmp_res & 2) || bitflips); 528 seb->scrub = ((cmp_res & 2) || bitflips);
529 seb->copy_flag = vid_hdr->copy_flag;
478 seb->sqnum = sqnum; 530 seb->sqnum = sqnum;
479 531
480 if (sv->highest_lnum == lnum) 532 if (sv->highest_lnum == lnum)
@@ -487,10 +539,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 539 * This logical eraseblock is older than the one found
488 * previously. 540 * previously.
489 */ 541 */
490 if (cmp_res & 4) 542 return add_to_list(si, pnum, ec, cmp_res & 4,
491 return add_to_list(si, pnum, ec, &si->corr); 543 &si->erase);
492 else
493 return add_to_list(si, pnum, ec, &si->erase);
494 } 544 }
495 } 545 }
496 546
@@ -510,8 +560,9 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
510 seb->ec = ec; 560 seb->ec = ec;
511 seb->pnum = pnum; 561 seb->pnum = pnum;
512 seb->lnum = lnum; 562 seb->lnum = lnum;
513 seb->sqnum = sqnum;
514 seb->scrub = bitflips; 563 seb->scrub = bitflips;
564 seb->copy_flag = vid_hdr->copy_flag;
565 seb->sqnum = sqnum;
515 566
516 if (sv->highest_lnum <= lnum) { 567 if (sv->highest_lnum <= lnum) {
517 sv->highest_lnum = lnum; 568 sv->highest_lnum = lnum;
@@ -521,7 +572,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
521 sv->leb_count += 1; 572 sv->leb_count += 1;
522 rb_link_node(&seb->u.rb, parent, p); 573 rb_link_node(&seb->u.rb, parent, p);
523 rb_insert_color(&seb->u.rb, &sv->root); 574 rb_insert_color(&seb->u.rb, &sv->root);
524 si->used_peb_count += 1;
525 return 0; 575 return 0;
526} 576}
527 577
@@ -668,8 +718,8 @@ out_free:
668struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, 718struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
669 struct ubi_scan_info *si) 719 struct ubi_scan_info *si)
670{ 720{
671 int err = 0, i; 721 int err = 0;
672 struct ubi_scan_leb *seb; 722 struct ubi_scan_leb *seb, *tmp_seb;
673 723
674 if (!list_empty(&si->free)) { 724 if (!list_empty(&si->free)) {
675 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); 725 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
@@ -678,38 +728,86 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
678 return seb; 728 return seb;
679 } 729 }
680 730
681 for (i = 0; i < 2; i++) { 731 /*
682 struct list_head *head; 732 * We try to erase the first physical eraseblock from the erase list
683 struct ubi_scan_leb *tmp_seb; 733 * and pick it if we succeed, or try to erase the next one if not. And
734 * so forth. We don't want to take care about bad eraseblocks here -
735 * they'll be handled later.
736 */
737 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) {
738 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
739 seb->ec = si->mean_ec;
684 740
685 if (i == 0) 741 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
686 head = &si->erase; 742 if (err)
687 else 743 continue;
688 head = &si->corr;
689 744
745 seb->ec += 1;
746 list_del(&seb->u.list);
747 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
748 return seb;
749 }
750
751 ubi_err("no free eraseblocks");
752 return ERR_PTR(-ENOSPC);
753}
754
755/**
756 * check_corruption - check the data area of PEB.
757 * @ubi: UBI device description object
758 * @vid_hrd: the (corrupted) VID header of this PEB
759 * @pnum: the physical eraseblock number to check
760 *
761 * This is a helper function which is used to distinguish between VID header
762 * corruptions caused by power cuts and other reasons. If the PEB contains only
763 * 0xFF bytes in the data area, the VID header is most probably corrupted
764 * because of a power cut (%0 is returned in this case). Otherwise, it was
765 * probably corrupted for some other reasons (%1 is returned in this case). A
766 * negative error code is returned if a read error occurred.
767 *
768 * If the corruption reason was a power cut, UBI can safely erase this PEB.
769 * Otherwise, it should preserve it to avoid possibly destroying important
770 * information.
771 */
772static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
773 int pnum)
774{
775 int err;
776
777 mutex_lock(&ubi->buf_mutex);
778 memset(ubi->peb_buf1, 0x00, ubi->leb_size);
779
780 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, ubi->leb_start,
781 ubi->leb_size);
782 if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
690 /* 783 /*
691 * We try to erase the first physical eraseblock from the @head 784 * Bit-flips or integrity errors while reading the data area.
692 * list and pick it if we succeed, or try to erase the 785 * 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 786 * this, but presumably a power cut happened while this PEB was
694 * about bad eraseblocks here - they'll be handled later. 787 * erased, so it became unstable and corrupted, and should be
788 * erased.
695 */ 789 */
696 list_for_each_entry_safe(seb, tmp_seb, head, u.list) { 790 return 0;
697 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 791 }
698 seb->ec = si->mean_ec;
699 792
700 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 793 if (err)
701 if (err) 794 return err;
702 continue;
703 795
704 seb->ec += 1; 796 if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size)) {
705 list_del(&seb->u.list); 797 mutex_unlock(&ubi->buf_mutex);
706 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec); 798 return 0;
707 return seb;
708 }
709 } 799 }
710 800
711 ubi_err("no eraseblocks found"); 801 ubi_err("PEB %d contains corrupted VID header, and the data does not "
712 return ERR_PTR(-ENOSPC); 802 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
803 "header corruption which requires manual inspection", pnum);
804 ubi_dbg_dump_vid_hdr(vid_hdr);
805 dbg_msg("hexdump of PEB %d offset %d, length %d",
806 pnum, ubi->leb_start, ubi->leb_size);
807 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
808 ubi->peb_buf1, ubi->leb_size, 1);
809 mutex_unlock(&ubi->buf_mutex);
810 return 1;
713} 811}
714 812
715/** 813/**
@@ -725,7 +823,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
725 int pnum) 823 int pnum)
726{ 824{
727 long long uninitialized_var(ec); 825 long long uninitialized_var(ec);
728 int err, bitflips = 0, vol_id, ec_corr = 0; 826 int err, bitflips = 0, vol_id, ec_err = 0;
729 827
730 dbg_bld("scan PEB %d", pnum); 828 dbg_bld("scan PEB %d", pnum);
731 829
@@ -746,22 +844,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); 844 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
747 if (err < 0) 845 if (err < 0)
748 return err; 846 return err;
749 else if (err == UBI_IO_BITFLIPS) 847 switch (err) {
848 case 0:
849 break;
850 case UBI_IO_BITFLIPS:
750 bitflips = 1; 851 bitflips = 1;
751 else if (err == UBI_IO_PEB_EMPTY) 852 break;
752 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); 853 case UBI_IO_FF:
753 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) { 854 si->empty_peb_count += 1;
855 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
856 &si->erase);
857 case UBI_IO_FF_BITFLIPS:
858 si->empty_peb_count += 1;
859 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
860 &si->erase);
861 case UBI_IO_BAD_HDR_EBADMSG:
862 case UBI_IO_BAD_HDR:
754 /* 863 /*
755 * We have to also look at the VID header, possibly it is not 864 * 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 865 * corrupted. Set %bitflips flag in order to make this PEB be
757 * moved and EC be re-created. 866 * moved and EC be re-created.
758 */ 867 */
759 ec_corr = err; 868 ec_err = err;
760 ec = UBI_SCAN_UNKNOWN_EC; 869 ec = UBI_SCAN_UNKNOWN_EC;
761 bitflips = 1; 870 bitflips = 1;
871 break;
872 default:
873 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
874 return -EINVAL;
762 } 875 }
763 876
764 if (!ec_corr) { 877 if (!ec_err) {
765 int image_seq; 878 int image_seq;
766 879
767 /* Make sure UBI version is OK */ 880 /* Make sure UBI version is OK */
@@ -814,24 +927,67 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
814 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); 927 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
815 if (err < 0) 928 if (err < 0)
816 return err; 929 return err;
817 else if (err == UBI_IO_BITFLIPS) 930 switch (err) {
931 case 0:
932 break;
933 case UBI_IO_BITFLIPS:
818 bitflips = 1; 934 bitflips = 1;
819 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR || 935 break;
820 (err == UBI_IO_PEB_FREE && ec_corr)) { 936 case UBI_IO_BAD_HDR_EBADMSG:
821 /* VID header is corrupted */ 937 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
822 if (err == UBI_IO_BAD_HDR_READ || 938 /*
823 ec_corr == UBI_IO_BAD_HDR_READ) 939 * Both EC and VID headers are corrupted and were read
824 si->read_err_count += 1; 940 * with data integrity error, probably this is a bad
825 err = add_to_list(si, pnum, ec, &si->corr); 941 * PEB, bit it is not marked as bad yet. This may also
942 * be a result of power cut during erasure.
943 */
944 si->maybe_bad_peb_count += 1;
945 case UBI_IO_BAD_HDR:
946 if (ec_err)
947 /*
948 * Both headers are corrupted. There is a possibility
949 * that this a valid UBI PEB which has corresponding
950 * LEB, but the headers are corrupted. However, it is
951 * impossible to distinguish it from a PEB which just
952 * contains garbage because of a power cut during erase
953 * operation. So we just schedule this PEB for erasure.
954 */
955 err = 0;
956 else
957 /*
958 * The EC was OK, but the VID header is corrupted. We
959 * have to check what is in the data area.
960 */
961 err = check_corruption(ubi, vidh, pnum);
962
963 if (err < 0)
964 return err;
965 else if (!err)
966 /* This corruption is caused by a power cut */
967 err = add_to_list(si, pnum, ec, 1, &si->erase);
968 else
969 /* This is an unexpected corruption */
970 err = add_corrupted(si, pnum, ec);
826 if (err) 971 if (err)
827 return err; 972 return err;
828 goto adjust_mean_ec; 973 goto adjust_mean_ec;
829 } else if (err == UBI_IO_PEB_FREE) { 974 case UBI_IO_FF_BITFLIPS:
830 /* No VID header - the physical eraseblock is free */ 975 err = add_to_list(si, pnum, ec, 1, &si->erase);
831 err = add_to_list(si, pnum, ec, &si->free);
832 if (err) 976 if (err)
833 return err; 977 return err;
834 goto adjust_mean_ec; 978 goto adjust_mean_ec;
979 case UBI_IO_FF:
980 if (ec_err)
981 err = add_to_list(si, pnum, ec, 1, &si->erase);
982 else
983 err = add_to_list(si, pnum, ec, 0, &si->free);
984 if (err)
985 return err;
986 goto adjust_mean_ec;
987 default:
988 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
989 err);
990 return -EINVAL;
835 } 991 }
836 992
837 vol_id = be32_to_cpu(vidh->vol_id); 993 vol_id = be32_to_cpu(vidh->vol_id);
@@ -843,7 +999,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
843 case UBI_COMPAT_DELETE: 999 case UBI_COMPAT_DELETE:
844 ubi_msg("\"delete\" compatible internal volume %d:%d" 1000 ubi_msg("\"delete\" compatible internal volume %d:%d"
845 " found, will remove it", vol_id, lnum); 1001 " found, will remove it", vol_id, lnum);
846 err = add_to_list(si, pnum, ec, &si->erase); 1002 err = add_to_list(si, pnum, ec, 1, &si->erase);
847 if (err) 1003 if (err)
848 return err; 1004 return err;
849 return 0; 1005 return 0;
@@ -858,7 +1014,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
858 case UBI_COMPAT_PRESERVE: 1014 case UBI_COMPAT_PRESERVE:
859 ubi_msg("\"preserve\" compatible internal volume %d:%d" 1015 ubi_msg("\"preserve\" compatible internal volume %d:%d"
860 " found", vol_id, lnum); 1016 " found", vol_id, lnum);
861 err = add_to_list(si, pnum, ec, &si->alien); 1017 err = add_to_list(si, pnum, ec, 0, &si->alien);
862 if (err) 1018 if (err)
863 return err; 1019 return err;
864 return 0; 1020 return 0;
@@ -870,7 +1026,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
870 } 1026 }
871 } 1027 }
872 1028
873 if (ec_corr) 1029 if (ec_err)
874 ubi_warn("valid VID header but corrupted EC header at PEB %d", 1030 ubi_warn("valid VID header but corrupted EC header at PEB %d",
875 pnum); 1031 pnum);
876 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); 1032 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
@@ -878,7 +1034,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
878 return err; 1034 return err;
879 1035
880adjust_mean_ec: 1036adjust_mean_ec:
881 if (!ec_corr) { 1037 if (!ec_err) {
882 si->ec_sum += ec; 1038 si->ec_sum += ec;
883 si->ec_count += 1; 1039 si->ec_count += 1;
884 if (ec > si->max_ec) 1040 if (ec > si->max_ec)
@@ -904,19 +1060,20 @@ adjust_mean_ec:
904static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si) 1060static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
905{ 1061{
906 struct ubi_scan_leb *seb; 1062 struct ubi_scan_leb *seb;
907 int max_corr; 1063 int max_corr, peb_count;
908 1064
909 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count; 1065 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
910 max_corr = max_corr / 20 ?: 8; 1066 max_corr = peb_count / 20 ?: 8;
911 1067
912 /* 1068 /*
913 * Few corrupted PEBs are not a problem and may be just a result of 1069 * 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 1070 * unclean reboots. However, many of them may indicate some problems
915 * with the flash HW or driver. 1071 * with the flash HW or driver.
916 */ 1072 */
917 if (si->corr_peb_count >= 8) { 1073 if (si->corr_peb_count) {
918 ubi_warn("%d PEBs are corrupted", si->corr_peb_count); 1074 ubi_err("%d PEBs are corrupted and preserved",
919 printk(KERN_WARNING "corrupted PEBs are:"); 1075 si->corr_peb_count);
1076 printk(KERN_ERR "Corrupted PEBs are:");
920 list_for_each_entry(seb, &si->corr, u.list) 1077 list_for_each_entry(seb, &si->corr, u.list)
921 printk(KERN_CONT " %d", seb->pnum); 1078 printk(KERN_CONT " %d", seb->pnum);
922 printk(KERN_CONT "\n"); 1079 printk(KERN_CONT "\n");
@@ -931,41 +1088,35 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
931 } 1088 }
932 } 1089 }
933 1090
934 if (si->free_peb_count + si->used_peb_count + 1091 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
935 si->alien_peb_count == 0) { 1092 /*
936 /* No UBI-formatted eraseblocks were found */ 1093 * All PEBs are empty, or almost all - a couple PEBs look like
937 if (si->corr_peb_count == si->read_err_count && 1094 * they may be bad PEBs which were not marked as bad yet.
938 si->corr_peb_count < 8) { 1095 *
939 /* No or just few corrupted PEBs, and all of them had a 1096 * This piece of code basically tries to distinguish between
940 * read error. We assume that those are bad PEBs, which 1097 * the following situations:
941 * were just not marked as bad so far. 1098 *
942 * 1099 * 1. Flash is empty, but there are few bad PEBs, which are not
943 * This piece of code basically tries to distinguish 1100 * marked as bad so far, and which were read with error. We
944 * between the following 2 situations: 1101 * want to go ahead and format this flash. While formatting,
945 * 1102 * the faulty PEBs will probably be marked as bad.
946 * 1. Flash is empty, but there are few bad PEBs, which 1103 *
947 * are not marked as bad so far, and which were read 1104 * 2. Flash contains non-UBI data and we do not want to format
948 * with error. We want to go ahead and format this 1105 * it and destroy possibly important information.
949 * flash. While formating, the faulty PEBs will 1106 */
950 * probably be marked as bad. 1107 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; 1108 si->is_empty = 1;
958 ubi_msg("empty MTD device detected"); 1109 ubi_msg("empty MTD device detected");
959 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq)); 1110 get_random_bytes(&ubi->image_seq,
1111 sizeof(ubi->image_seq));
960 } else { 1112 } else {
961 ubi_err("MTD device possibly contains non-UBI data, " 1113 ubi_err("MTD device is not UBI-formatted and possibly "
962 "refusing it"); 1114 "contains non-UBI data - refusing it");
963 return -EINVAL; 1115 return -EINVAL;
964 } 1116 }
1117
965 } 1118 }
966 1119
967 if (si->corr_peb_count > 0)
968 ubi_msg("corrupted PEBs will be formatted");
969 return 0; 1120 return 0;
970} 1121}
971 1122
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
index 2576a8d1532b..a3264f0bef2b 100644
--- a/drivers/mtd/ubi/scan.h
+++ b/drivers/mtd/ubi/scan.h
@@ -30,6 +30,7 @@
30 * @pnum: physical eraseblock number 30 * @pnum: physical eraseblock number
31 * @lnum: logical eraseblock number 31 * @lnum: logical eraseblock number
32 * @scrub: if this physical eraseblock needs scrubbing 32 * @scrub: if this physical eraseblock needs scrubbing
33 * @copy_flag: this LEB is a copy (@copy_flag is set in VID header of this LEB)
33 * @sqnum: sequence number 34 * @sqnum: sequence number
34 * @u: unions RB-tree or @list links 35 * @u: unions RB-tree or @list links
35 * @u.rb: link in the per-volume RB-tree of &struct ubi_scan_leb objects 36 * @u.rb: link in the per-volume RB-tree of &struct ubi_scan_leb objects
@@ -42,7 +43,8 @@ struct ubi_scan_leb {
42 int ec; 43 int ec;
43 int pnum; 44 int pnum;
44 int lnum; 45 int lnum;
45 int scrub; 46 unsigned int scrub:1;
47 unsigned int copy_flag:1;
46 unsigned long long sqnum; 48 unsigned long long sqnum;
47 union { 49 union {
48 struct rb_node rb; 50 struct rb_node rb;
@@ -91,14 +93,13 @@ struct ubi_scan_volume {
91 * @erase: list of physical eraseblocks which have to be erased 93 * @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., 94 * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
93 * those belonging to "preserve"-compatible internal volumes) 95 * 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 * @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 * @empty_peb_count: count of PEBs which are presumably empty (contain only
97 * returned) 98 * 0xFF bytes)
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 99 * @alien_peb_count: count of PEBs in the @alien list
101 * @bad_peb_count: count of bad physical eraseblocks 100 * @bad_peb_count: count of bad physical eraseblocks
101 * @maybe_bad_peb_count: count of bad physical eraseblocks which are not marked
102 * as bad yet, but which look like bad
102 * @vols_found: number of volumes found during scanning 103 * @vols_found: number of volumes found during scanning
103 * @highest_vol_id: highest volume ID 104 * @highest_vol_id: highest volume ID
104 * @is_empty: flag indicating whether the MTD device is empty or not 105 * @is_empty: flag indicating whether the MTD device is empty or not
@@ -119,13 +120,11 @@ struct ubi_scan_info {
119 struct list_head free; 120 struct list_head free;
120 struct list_head erase; 121 struct list_head erase;
121 struct list_head alien; 122 struct list_head alien;
122 int used_peb_count;
123 int corr_peb_count; 123 int corr_peb_count;
124 int read_err_count; 124 int empty_peb_count;
125 int free_peb_count;
126 int erase_peb_count;
127 int alien_peb_count; 125 int alien_peb_count;
128 int bad_peb_count; 126 int bad_peb_count;
127 int maybe_bad_peb_count;
129 int vols_found; 128 int vols_found;
130 int highest_vol_id; 129 int highest_vol_id;
131 int is_empty; 130 int is_empty;
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index 0359e0cce482..0b0149c41fe3 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -85,21 +85,26 @@
85/* 85/*
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_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only 88 * UBI_IO_FF: the read region of flash contains only 0xFFs
89 * %0xFF bytes 89 * UBI_IO_FF_BITFLIPS: the same as %UBI_IO_FF, but also also there was a data
90 * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a 90 * integrity error reported by the MTD driver
91 * valid erase counter header, and the rest are %0xFF bytes 91 * (uncorrectable ECC error in case of NAND)
92 * 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)
93 * UBI_IO_BAD_HDR_READ: the same as %UBI_IO_BAD_HDR, but also there was a read 93 * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
94 * error reported by the flash driver 94 * data integrity error reported by the MTD driver
95 * (uncorrectable ECC error in case of NAND)
95 * 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
96 */ 101 */
97enum { 102enum {
98 UBI_IO_PEB_EMPTY = 1, 103 UBI_IO_FF = 1,
99 UBI_IO_PEB_FREE, 104 UBI_IO_FF_BITFLIPS,
100 UBI_IO_BAD_HDR, 105 UBI_IO_BAD_HDR,
101 UBI_IO_BAD_HDR_READ, 106 UBI_IO_BAD_HDR_EBADMSG,
102 UBI_IO_BITFLIPS 107 UBI_IO_BITFLIPS,
103}; 108};
104 109
105/* 110/*
@@ -356,6 +361,8 @@ struct ubi_wl_entry;
356 * @peb_size: physical eraseblock size 361 * @peb_size: physical eraseblock size
357 * @bad_peb_count: count of bad physical eraseblocks 362 * @bad_peb_count: count of bad physical eraseblocks
358 * @good_peb_count: count of good physical eraseblocks 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)
359 * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous 366 * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
360 * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks 367 * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
361 * @min_io_size: minimal input/output unit size of the underlying MTD device 368 * @min_io_size: minimal input/output unit size of the underlying MTD device
@@ -442,6 +449,7 @@ struct ubi_device {
442 int peb_size; 449 int peb_size;
443 int bad_peb_count; 450 int bad_peb_count;
444 int good_peb_count; 451 int good_peb_count;
452 int corr_peb_count;
445 int erroneous_peb_count; 453 int erroneous_peb_count;
446 int max_erroneous; 454 int max_erroneous;
447 int min_io_size; 455 int min_io_size;
@@ -506,6 +514,7 @@ int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
506 int length); 514 int length);
507int ubi_check_volume(struct ubi_device *ubi, int vol_id); 515int ubi_check_volume(struct ubi_device *ubi, int vol_id);
508void ubi_calculate_reserved(struct ubi_device *ubi); 516void ubi_calculate_reserved(struct ubi_device *ubi);
517int ubi_check_pattern(const void *buf, uint8_t patt, int size);
509 518
510/* eba.c */ 519/* eba.c */
511int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, 520int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol,
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index e42afab9a9fe..c47620dfc722 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -261,6 +261,9 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
261 /* Reserve physical eraseblocks */ 261 /* Reserve physical eraseblocks */
262 if (vol->reserved_pebs > ubi->avail_pebs) { 262 if (vol->reserved_pebs > ubi->avail_pebs) {
263 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); 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 err = -ENOSPC; 267 err = -ENOSPC;
265 goto out_unlock; 268 goto out_unlock;
266 } 269 }
@@ -527,6 +530,9 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
527 if (pebs > ubi->avail_pebs) { 530 if (pebs > ubi->avail_pebs) {
528 dbg_err("not enough PEBs: requested %d, available %d", 531 dbg_err("not enough PEBs: requested %d, available %d",
529 pebs, ubi->avail_pebs); 532 pebs, ubi->avail_pebs);
533 if (ubi->corr_peb_count)
534 dbg_err("%d PEBs are corrupted and not used",
535 ubi->corr_peb_count);
530 spin_unlock(&ubi->volumes_lock); 536 spin_unlock(&ubi->volumes_lock);
531 err = -ENOSPC; 537 err = -ENOSPC;
532 goto out_free; 538 goto out_free;
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
index 14c10bed94ee..fcdb7f65fe0b 100644
--- a/drivers/mtd/ubi/vtbl.c
+++ b/drivers/mtd/ubi/vtbl.c
@@ -366,7 +366,7 @@ write_error:
366 * Probably this physical eraseblock went bad, try to pick 366 * Probably this physical eraseblock went bad, try to pick
367 * another one. 367 * another one.
368 */ 368 */
369 list_add_tail(&new_seb->u.list, &si->corr); 369 list_add(&new_seb->u.list, &si->erase);
370 goto retry; 370 goto retry;
371 } 371 }
372 kfree(new_seb); 372 kfree(new_seb);
@@ -662,9 +662,13 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
662 ubi->vol_count += 1; 662 ubi->vol_count += 1;
663 vol->ubi = ubi; 663 vol->ubi = ubi;
664 664
665 if (reserved_pebs > ubi->avail_pebs) 665 if (reserved_pebs > ubi->avail_pebs) {
666 ubi_err("not enough PEBs, required %d, available %d", 666 ubi_err("not enough PEBs, required %d, available %d",
667 reserved_pebs, ubi->avail_pebs); 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 ubi->rsvd_pebs += reserved_pebs; 672 ubi->rsvd_pebs += reserved_pebs;
669 ubi->avail_pebs -= reserved_pebs; 673 ubi->avail_pebs -= reserved_pebs;
670 674
@@ -837,7 +841,7 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
837 return PTR_ERR(ubi->vtbl); 841 return PTR_ERR(ubi->vtbl);
838 } 842 }
839 843
840 ubi->avail_pebs = ubi->good_peb_count; 844 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
841 845
842 /* 846 /*
843 * The layout volume is OK, initialize the corresponding in-RAM data 847 * The layout volume is OK, initialize the corresponding in-RAM data
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 97a435672eaf..655bbbe415d9 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -745,7 +745,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
745 745
746 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0); 746 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
747 if (err && err != UBI_IO_BITFLIPS) { 747 if (err && err != UBI_IO_BITFLIPS) {
748 if (err == UBI_IO_PEB_FREE) { 748 if (err == UBI_IO_FF) {
749 /* 749 /*
750 * We are trying to move PEB without a VID header. UBI 750 * We are trying to move PEB without a VID header. UBI
751 * always write VID headers shortly after the PEB was 751 * always write VID headers shortly after the PEB was
@@ -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",
@@ -1468,22 +1478,6 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1468 ubi->lookuptbl[e->pnum] = e; 1478 ubi->lookuptbl[e->pnum] = e;
1469 } 1479 }
1470 1480
1471 list_for_each_entry(seb, &si->corr, u.list) {
1472 cond_resched();
1473
1474 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1475 if (!e)
1476 goto out_free;
1477
1478 e->pnum = seb->pnum;
1479 e->ec = seb->ec;
1480 ubi->lookuptbl[e->pnum] = e;
1481 if (schedule_erase(ubi, e, 0)) {
1482 kmem_cache_free(ubi_wl_entry_slab, e);
1483 goto out_free;
1484 }
1485 }
1486
1487 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1481 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1488 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) { 1482 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1489 cond_resched(); 1483 cond_resched();
@@ -1510,6 +1504,9 @@ int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1510 if (ubi->avail_pebs < WL_RESERVED_PEBS) { 1504 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
1511 ubi_err("no enough physical eraseblocks (%d, need %d)", 1505 ubi_err("no enough physical eraseblocks (%d, need %d)",
1512 ubi->avail_pebs, WL_RESERVED_PEBS); 1506 ubi->avail_pebs, WL_RESERVED_PEBS);
1507 if (ubi->corr_peb_count)
1508 ubi_err("%d PEBs are corrupted and not used",
1509 ubi->corr_peb_count);
1513 goto out_free; 1510 goto out_free;
1514 } 1511 }
1515 ubi->avail_pebs -= WL_RESERVED_PEBS; 1512 ubi->avail_pebs -= WL_RESERVED_PEBS;