aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/io.c
diff options
context:
space:
mode:
authorGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
committerGlenn Elliott <gelliott@cs.unc.edu>2012-03-04 19:47:13 -0500
commitc71c03bda1e86c9d5198c5d83f712e695c4f2a1e (patch)
treeecb166cb3e2b7e2adb3b5e292245fefd23381ac8 /drivers/mtd/ubi/io.c
parentea53c912f8a86a8567697115b6a0d8152beee5c8 (diff)
parent6a00f206debf8a5c8899055726ad127dbeeed098 (diff)
Merge branch 'mpi-master' into wip-k-fmlpwip-k-fmlp
Conflicts: litmus/sched_cedf.c
Diffstat (limited to 'drivers/mtd/ubi/io.c')
-rw-r--r--drivers/mtd/ubi/io.c326
1 files changed, 185 insertions, 141 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 332f992f13d9..8c1b1c7bc4a7 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -91,7 +91,7 @@
91#include <linux/slab.h> 91#include <linux/slab.h>
92#include "ubi.h" 92#include "ubi.h"
93 93
94#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 94#ifdef CONFIG_MTD_UBI_DEBUG
95static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum); 95static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum);
96static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum); 96static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum);
97static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 97static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
@@ -146,6 +146,28 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
146 if (err) 146 if (err)
147 return err; 147 return err;
148 148
149 /*
150 * Deliberately corrupt the buffer to improve robustness. Indeed, if we
151 * do not do this, the following may happen:
152 * 1. The buffer contains data from previous operation, e.g., read from
153 * another PEB previously. The data looks like expected, e.g., if we
154 * just do not read anything and return - the caller would not
155 * notice this. E.g., if we are reading a VID header, the buffer may
156 * contain a valid VID header from another PEB.
157 * 2. The driver is buggy and returns us success or -EBADMSG or
158 * -EUCLEAN, but it does not actually put any data to the buffer.
159 *
160 * This may confuse UBI or upper layers - they may think the buffer
161 * contains valid data while in fact it is just old data. This is
162 * especially possible because UBI (and UBIFS) relies on CRC, and
163 * treats data as correct even in case of ECC errors if the CRC is
164 * correct.
165 *
166 * Try to prevent this situation by changing the first byte of the
167 * buffer.
168 */
169 *((uint8_t *)buf) ^= 0xFF;
170
149 addr = (loff_t)pnum * ubi->peb_size + offset; 171 addr = (loff_t)pnum * ubi->peb_size + offset;
150retry: 172retry:
151 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 173 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
@@ -166,9 +188,9 @@ retry:
166 return UBI_IO_BITFLIPS; 188 return UBI_IO_BITFLIPS;
167 } 189 }
168 190
169 if (read != len && retries++ < UBI_IO_RETRIES) { 191 if (retries++ < UBI_IO_RETRIES) {
170 dbg_io("error %d%s while reading %d bytes from PEB %d:%d," 192 dbg_io("error %d%s while reading %d bytes from PEB "
171 " read only %zd bytes, retry", 193 "%d:%d, read only %zd bytes, retry",
172 err, errstr, len, pnum, offset, read); 194 err, errstr, len, pnum, offset, read);
173 yield(); 195 yield();
174 goto retry; 196 goto retry;
@@ -322,6 +344,12 @@ static int do_sync_erase(struct ubi_device *ubi, int pnum)
322 wait_queue_head_t wq; 344 wait_queue_head_t wq;
323 345
324 dbg_io("erase PEB %d", pnum); 346 dbg_io("erase PEB %d", pnum);
347 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
348
349 if (ubi->ro_mode) {
350 ubi_err("read-only mode");
351 return -EROFS;
352 }
325 353
326retry: 354retry:
327 init_waitqueue_head(&wq); 355 init_waitqueue_head(&wq);
@@ -368,7 +396,7 @@ retry:
368 if (err) 396 if (err)
369 return err; 397 return err;
370 398
371 if (ubi_dbg_is_erase_failure() && !err) { 399 if (ubi_dbg_is_erase_failure()) {
372 dbg_err("cannot erase PEB %d (emulated)", pnum); 400 dbg_err("cannot erase PEB %d (emulated)", pnum);
373 return -EIO; 401 return -EIO;
374 } 402 }
@@ -376,25 +404,6 @@ retry:
376 return 0; 404 return 0;
377} 405}
378 406
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 */ 407/* Patterns to write to a physical eraseblock when torturing it */
399static uint8_t patterns[] = {0xa5, 0x5a, 0x0}; 408static uint8_t patterns[] = {0xa5, 0x5a, 0x0};
400 409
@@ -426,7 +435,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
426 if (err) 435 if (err)
427 goto out; 436 goto out;
428 437
429 err = check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size); 438 err = ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->peb_size);
430 if (err == 0) { 439 if (err == 0) {
431 ubi_err("erased PEB %d, but a non-0xFF byte found", 440 ubi_err("erased PEB %d, but a non-0xFF byte found",
432 pnum); 441 pnum);
@@ -445,7 +454,8 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
445 if (err) 454 if (err)
446 goto out; 455 goto out;
447 456
448 err = check_pattern(ubi->peb_buf1, patterns[i], ubi->peb_size); 457 err = ubi_check_pattern(ubi->peb_buf1, patterns[i],
458 ubi->peb_size);
449 if (err == 0) { 459 if (err == 0) {
450 ubi_err("pattern %x checking failed for PEB %d", 460 ubi_err("pattern %x checking failed for PEB %d",
451 patterns[i], pnum); 461 patterns[i], pnum);
@@ -455,7 +465,7 @@ static int torture_peb(struct ubi_device *ubi, int pnum)
455 } 465 }
456 466
457 err = patt_count; 467 err = patt_count;
458 ubi_msg("PEB %d passed torture test, do not mark it a bad", pnum); 468 ubi_msg("PEB %d passed torture test, do not mark it as bad", pnum);
459 469
460out: 470out:
461 mutex_unlock(&ubi->buf_mutex); 471 mutex_unlock(&ubi->buf_mutex);
@@ -498,12 +508,26 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
498 size_t written; 508 size_t written;
499 loff_t addr; 509 loff_t addr;
500 uint32_t data = 0; 510 uint32_t data = 0;
511 /*
512 * Note, we cannot generally define VID header buffers on stack,
513 * because of the way we deal with these buffers (see the header
514 * comment in this file). But we know this is a NOR-specific piece of
515 * code, so we can do this. But yes, this is error-prone and we should
516 * (pre-)allocate VID header buffer instead.
517 */
501 struct ubi_vid_hdr vid_hdr; 518 struct ubi_vid_hdr vid_hdr;
502 519
503 addr = (loff_t)pnum * ubi->peb_size + ubi->vid_hdr_aloffset; 520 /*
521 * It is important to first invalidate the EC header, and then the VID
522 * header. Otherwise a power cut may lead to valid EC header and
523 * invalid VID header, in which case UBI will treat this PEB as
524 * corrupted and will try to preserve it, and print scary warnings (see
525 * the header comment in scan.c for more information).
526 */
527 addr = (loff_t)pnum * ubi->peb_size;
504 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data); 528 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
505 if (!err) { 529 if (!err) {
506 addr -= ubi->vid_hdr_aloffset; 530 addr += ubi->vid_hdr_aloffset;
507 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, 531 err = ubi->mtd->write(ubi->mtd, addr, 4, &written,
508 (void *)&data); 532 (void *)&data);
509 if (!err) 533 if (!err)
@@ -512,18 +536,26 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
512 536
513 /* 537 /*
514 * We failed to write to the media. This was observed with Spansion 538 * We failed to write to the media. This was observed with Spansion
515 * S29GL512N NOR flash. Most probably the eraseblock erasure was 539 * S29GL512N NOR flash. Most probably the previously eraseblock erasure
516 * interrupted at a very inappropriate moment, so it became unwritable. 540 * was interrupted at a very inappropriate moment, so it became
517 * In this case we probably anyway have garbage in this PEB. 541 * unwritable. In this case we probably anyway have garbage in this
542 * PEB.
518 */ 543 */
519 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 544 err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
520 if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR) 545 if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
521 /* 546 err1 == UBI_IO_FF) {
522 * The VID header is corrupted, so we can safely erase this 547 struct ubi_ec_hdr ec_hdr;
523 * PEB and not afraid that it will be treated as a valid PEB in 548
524 * case of an unclean reboot. 549 err1 = ubi_io_read_ec_hdr(ubi, pnum, &ec_hdr, 0);
525 */ 550 if (err1 == UBI_IO_BAD_HDR_EBADMSG || err1 == UBI_IO_BAD_HDR ||
526 return 0; 551 err1 == UBI_IO_FF)
552 /*
553 * Both VID and EC headers are corrupted, so we can
554 * safely erase this PEB and not afraid that it will be
555 * treated as a valid PEB in case of an unclean reboot.
556 */
557 return 0;
558 }
527 559
528 /* 560 /*
529 * The PEB contains a valid VID header, but we cannot invalidate it. 561 * The PEB contains a valid VID header, but we cannot invalidate it.
@@ -712,55 +744,57 @@ bad:
712 * and corrected by the flash driver; this is harmless but may indicate that 744 * and corrected by the flash driver; this is harmless but may indicate that
713 * this eraseblock may become bad soon (but may be not); 745 * 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); 746 * 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; 747 * o %UBI_IO_BAD_HDR_EBADMSG is the same as %UBI_IO_BAD_HDR, but there also was
748 * a data integrity error (uncorrectable ECC error in case of NAND);
749 * 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. 750 * o a negative error code in case of failure.
717 */ 751 */
718int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, 752int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
719 struct ubi_ec_hdr *ec_hdr, int verbose) 753 struct ubi_ec_hdr *ec_hdr, int verbose)
720{ 754{
721 int err, read_err = 0; 755 int err, read_err;
722 uint32_t crc, magic, hdr_crc; 756 uint32_t crc, magic, hdr_crc;
723 757
724 dbg_io("read EC header from PEB %d", pnum); 758 dbg_io("read EC header from PEB %d", pnum);
725 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 759 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
726 760
727 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 761 read_err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
728 if (err) { 762 if (read_err) {
729 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 763 if (read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
730 return err; 764 return read_err;
731 765
732 /* 766 /*
733 * We read all the data, but either a correctable bit-flip 767 * We read all the data, but either a correctable bit-flip
734 * occurred, or MTD reported about some data integrity error, 768 * occurred, or MTD reported a data integrity error
735 * like an ECC error in case of NAND. The former is harmless, 769 * (uncorrectable ECC error in case of NAND). The former is
736 * the later may mean that the read data is corrupted. But we 770 * harmless, the later may mean that the read data is
737 * have a CRC check-sum and we will detect this. If the EC 771 * 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 772 * this. If the EC header is still OK, we just report this as
739 * bit-flip. 773 * there was a bit-flip, to force scrubbing.
740 */ 774 */
741 if (err == -EBADMSG)
742 read_err = UBI_IO_BAD_HDR_READ;
743 } 775 }
744 776
745 magic = be32_to_cpu(ec_hdr->magic); 777 magic = be32_to_cpu(ec_hdr->magic);
746 if (magic != UBI_EC_HDR_MAGIC) { 778 if (magic != UBI_EC_HDR_MAGIC) {
747 if (read_err) 779 if (read_err == -EBADMSG)
748 return read_err; 780 return UBI_IO_BAD_HDR_EBADMSG;
749 781
750 /* 782 /*
751 * The magic field is wrong. Let's check if we have read all 783 * The magic field is wrong. Let's check if we have read all
752 * 0xFF. If yes, this physical eraseblock is assumed to be 784 * 0xFF. If yes, this physical eraseblock is assumed to be
753 * empty. 785 * empty.
754 */ 786 */
755 if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 787 if (ubi_check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
756 /* The physical eraseblock is supposedly empty */ 788 /* The physical eraseblock is supposedly empty */
757 if (verbose) 789 if (verbose)
758 ubi_warn("no EC header found at PEB %d, " 790 ubi_warn("no EC header found at PEB %d, "
759 "only 0xFF bytes", pnum); 791 "only 0xFF bytes", pnum);
760 else if (UBI_IO_DEBUG) 792 dbg_bld("no EC header found at PEB %d, "
761 dbg_msg("no EC header found at PEB %d, " 793 "only 0xFF bytes", pnum);
762 "only 0xFF bytes", pnum); 794 if (!read_err)
763 return UBI_IO_PEB_EMPTY; 795 return UBI_IO_FF;
796 else
797 return UBI_IO_FF_BITFLIPS;
764 } 798 }
765 799
766 /* 800 /*
@@ -771,9 +805,9 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
771 ubi_warn("bad magic number at PEB %d: %08x instead of " 805 ubi_warn("bad magic number at PEB %d: %08x instead of "
772 "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 806 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
773 ubi_dbg_dump_ec_hdr(ec_hdr); 807 ubi_dbg_dump_ec_hdr(ec_hdr);
774 } else if (UBI_IO_DEBUG) 808 }
775 dbg_msg("bad magic number at PEB %d: %08x instead of " 809 dbg_bld("bad magic number at PEB %d: %08x instead of "
776 "%08x", pnum, magic, UBI_EC_HDR_MAGIC); 810 "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
777 return UBI_IO_BAD_HDR; 811 return UBI_IO_BAD_HDR;
778 } 812 }
779 813
@@ -785,10 +819,14 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
785 ubi_warn("bad EC header CRC at PEB %d, calculated " 819 ubi_warn("bad EC header CRC at PEB %d, calculated "
786 "%#08x, read %#08x", pnum, crc, hdr_crc); 820 "%#08x, read %#08x", pnum, crc, hdr_crc);
787 ubi_dbg_dump_ec_hdr(ec_hdr); 821 ubi_dbg_dump_ec_hdr(ec_hdr);
788 } else if (UBI_IO_DEBUG) 822 }
789 dbg_msg("bad EC header CRC at PEB %d, calculated " 823 dbg_bld("bad EC header CRC at PEB %d, calculated "
790 "%#08x, read %#08x", pnum, crc, hdr_crc); 824 "%#08x, read %#08x", pnum, crc, hdr_crc);
791 return read_err ?: UBI_IO_BAD_HDR; 825
826 if (!read_err)
827 return UBI_IO_BAD_HDR;
828 else
829 return UBI_IO_BAD_HDR_EBADMSG;
792 } 830 }
793 831
794 /* And of course validate what has just been read from the media */ 832 /* And of course validate what has just been read from the media */
@@ -975,22 +1013,16 @@ bad:
975 * 1013 *
976 * This function reads the volume identifier header from physical eraseblock 1014 * 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 1015 * @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: 1016 * volume identifier header. The error codes are the same as in
1017 * 'ubi_io_read_ec_hdr()'.
979 * 1018 *
980 * o %0 if the CRC checksum is correct and the header was successfully read; 1019 * 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 1020 * '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 */ 1021 */
990int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 1022int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
991 struct ubi_vid_hdr *vid_hdr, int verbose) 1023 struct ubi_vid_hdr *vid_hdr, int verbose)
992{ 1024{
993 int err, read_err = 0; 1025 int err, read_err;
994 uint32_t crc, magic, hdr_crc; 1026 uint32_t crc, magic, hdr_crc;
995 void *p; 1027 void *p;
996 1028
@@ -998,55 +1030,35 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
998 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1030 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
999 1031
1000 p = (char *)vid_hdr - ubi->vid_hdr_shift; 1032 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1001 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1033 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1002 ubi->vid_hdr_alsize); 1034 ubi->vid_hdr_alsize);
1003 if (err) { 1035 if (read_err && read_err != UBI_IO_BITFLIPS && read_err != -EBADMSG)
1004 if (err != UBI_IO_BITFLIPS && err != -EBADMSG) 1036 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 1037
1019 magic = be32_to_cpu(vid_hdr->magic); 1038 magic = be32_to_cpu(vid_hdr->magic);
1020 if (magic != UBI_VID_HDR_MAGIC) { 1039 if (magic != UBI_VID_HDR_MAGIC) {
1021 if (read_err) 1040 if (read_err == -EBADMSG)
1022 return read_err; 1041 return UBI_IO_BAD_HDR_EBADMSG;
1023 1042
1024 /* 1043 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) 1044 if (verbose)
1031 ubi_warn("no VID header found at PEB %d, " 1045 ubi_warn("no VID header found at PEB %d, "
1032 "only 0xFF bytes", pnum); 1046 "only 0xFF bytes", pnum);
1033 else if (UBI_IO_DEBUG) 1047 dbg_bld("no VID header found at PEB %d, "
1034 dbg_msg("no VID header found at PEB %d, " 1048 "only 0xFF bytes", pnum);
1035 "only 0xFF bytes", pnum); 1049 if (!read_err)
1036 return UBI_IO_PEB_FREE; 1050 return UBI_IO_FF;
1051 else
1052 return UBI_IO_FF_BITFLIPS;
1037 } 1053 }
1038 1054
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) { 1055 if (verbose) {
1044 ubi_warn("bad magic number at PEB %d: %08x instead of " 1056 ubi_warn("bad magic number at PEB %d: %08x instead of "
1045 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1057 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1046 ubi_dbg_dump_vid_hdr(vid_hdr); 1058 ubi_dbg_dump_vid_hdr(vid_hdr);
1047 } else if (UBI_IO_DEBUG) 1059 }
1048 dbg_msg("bad magic number at PEB %d: %08x instead of " 1060 dbg_bld("bad magic number at PEB %d: %08x instead of "
1049 "%08x", pnum, magic, UBI_VID_HDR_MAGIC); 1061 "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
1050 return UBI_IO_BAD_HDR; 1062 return UBI_IO_BAD_HDR;
1051 } 1063 }
1052 1064
@@ -1058,23 +1070,21 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1058 ubi_warn("bad CRC at PEB %d, calculated %#08x, " 1070 ubi_warn("bad CRC at PEB %d, calculated %#08x, "
1059 "read %#08x", pnum, crc, hdr_crc); 1071 "read %#08x", pnum, crc, hdr_crc);
1060 ubi_dbg_dump_vid_hdr(vid_hdr); 1072 ubi_dbg_dump_vid_hdr(vid_hdr);
1061 } else if (UBI_IO_DEBUG) 1073 }
1062 dbg_msg("bad CRC at PEB %d, calculated %#08x, " 1074 dbg_bld("bad CRC at PEB %d, calculated %#08x, "
1063 "read %#08x", pnum, crc, hdr_crc); 1075 "read %#08x", pnum, crc, hdr_crc);
1064 return read_err ?: UBI_IO_BAD_HDR; 1076 if (!read_err)
1077 return UBI_IO_BAD_HDR;
1078 else
1079 return UBI_IO_BAD_HDR_EBADMSG;
1065 } 1080 }
1066 1081
1067 /* Validate the VID header that we have just read */
1068 err = validate_vid_hdr(ubi, vid_hdr); 1082 err = validate_vid_hdr(ubi, vid_hdr);
1069 if (err) { 1083 if (err) {
1070 ubi_err("validation failed for PEB %d", pnum); 1084 ubi_err("validation failed for PEB %d", pnum);
1071 return -EINVAL; 1085 return -EINVAL;
1072 } 1086 }
1073 1087
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; 1088 return read_err ? UBI_IO_BITFLIPS : 0;
1079} 1089}
1080 1090
@@ -1122,7 +1132,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1122 return err; 1132 return err;
1123} 1133}
1124 1134
1125#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1135#ifdef CONFIG_MTD_UBI_DEBUG
1126 1136
1127/** 1137/**
1128 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad. 1138 * paranoid_check_not_bad - ensure that a physical eraseblock is not bad.
@@ -1136,6 +1146,9 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1136{ 1146{
1137 int err; 1147 int err;
1138 1148
1149 if (!(ubi_chk_flags & UBI_CHK_IO))
1150 return 0;
1151
1139 err = ubi_io_is_bad(ubi, pnum); 1152 err = ubi_io_is_bad(ubi, pnum);
1140 if (!err) 1153 if (!err)
1141 return err; 1154 return err;
@@ -1160,6 +1173,9 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1160 int err; 1173 int err;
1161 uint32_t magic; 1174 uint32_t magic;
1162 1175
1176 if (!(ubi_chk_flags & UBI_CHK_IO))
1177 return 0;
1178
1163 magic = be32_to_cpu(ec_hdr->magic); 1179 magic = be32_to_cpu(ec_hdr->magic);
1164 if (magic != UBI_EC_HDR_MAGIC) { 1180 if (magic != UBI_EC_HDR_MAGIC) {
1165 ubi_err("bad magic %#08x, must be %#08x", 1181 ubi_err("bad magic %#08x, must be %#08x",
@@ -1195,6 +1211,9 @@ static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1195 uint32_t crc, hdr_crc; 1211 uint32_t crc, hdr_crc;
1196 struct ubi_ec_hdr *ec_hdr; 1212 struct ubi_ec_hdr *ec_hdr;
1197 1213
1214 if (!(ubi_chk_flags & UBI_CHK_IO))
1215 return 0;
1216
1198 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 1217 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1199 if (!ec_hdr) 1218 if (!ec_hdr)
1200 return -ENOMEM; 1219 return -ENOMEM;
@@ -1236,6 +1255,9 @@ static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1236 int err; 1255 int err;
1237 uint32_t magic; 1256 uint32_t magic;
1238 1257
1258 if (!(ubi_chk_flags & UBI_CHK_IO))
1259 return 0;
1260
1239 magic = be32_to_cpu(vid_hdr->magic); 1261 magic = be32_to_cpu(vid_hdr->magic);
1240 if (magic != UBI_VID_HDR_MAGIC) { 1262 if (magic != UBI_VID_HDR_MAGIC) {
1241 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 1263 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
@@ -1274,6 +1296,9 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1274 struct ubi_vid_hdr *vid_hdr; 1296 struct ubi_vid_hdr *vid_hdr;
1275 void *p; 1297 void *p;
1276 1298
1299 if (!(ubi_chk_flags & UBI_CHK_IO))
1300 return 0;
1301
1277 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1302 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1278 if (!vid_hdr) 1303 if (!vid_hdr)
1279 return -ENOMEM; 1304 return -ENOMEM;
@@ -1319,15 +1344,26 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1319 int offset, int len) 1344 int offset, int len)
1320{ 1345{
1321 int err, i; 1346 int err, i;
1347 size_t read;
1348 void *buf1;
1349 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1322 1350
1323 mutex_lock(&ubi->dbg_buf_mutex); 1351 if (!(ubi_chk_flags & UBI_CHK_IO))
1324 err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len); 1352 return 0;
1325 if (err) 1353
1326 goto out_unlock; 1354 buf1 = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1355 if (!buf1) {
1356 ubi_err("cannot allocate memory to check writes");
1357 return 0;
1358 }
1359
1360 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf1);
1361 if (err && err != -EUCLEAN)
1362 goto out_free;
1327 1363
1328 for (i = 0; i < len; i++) { 1364 for (i = 0; i < len; i++) {
1329 uint8_t c = ((uint8_t *)buf)[i]; 1365 uint8_t c = ((uint8_t *)buf)[i];
1330 uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i]; 1366 uint8_t c1 = ((uint8_t *)buf1)[i];
1331 int dump_len; 1367 int dump_len;
1332 1368
1333 if (c == c1) 1369 if (c == c1)
@@ -1344,17 +1380,17 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1344 ubi_msg("hex dump of the read buffer from %d to %d", 1380 ubi_msg("hex dump of the read buffer from %d to %d",
1345 i, i + dump_len); 1381 i, i + dump_len);
1346 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1382 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1347 ubi->dbg_peb_buf + i, dump_len, 1); 1383 buf1 + i, dump_len, 1);
1348 ubi_dbg_dump_stack(); 1384 ubi_dbg_dump_stack();
1349 err = -EINVAL; 1385 err = -EINVAL;
1350 goto out_unlock; 1386 goto out_free;
1351 } 1387 }
1352 mutex_unlock(&ubi->dbg_buf_mutex);
1353 1388
1389 vfree(buf1);
1354 return 0; 1390 return 0;
1355 1391
1356out_unlock: 1392out_free:
1357 mutex_unlock(&ubi->dbg_buf_mutex); 1393 vfree(buf1);
1358 return err; 1394 return err;
1359} 1395}
1360 1396
@@ -1373,36 +1409,44 @@ int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1373{ 1409{
1374 size_t read; 1410 size_t read;
1375 int err; 1411 int err;
1412 void *buf;
1376 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 1413 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1377 1414
1378 mutex_lock(&ubi->dbg_buf_mutex); 1415 if (!(ubi_chk_flags & UBI_CHK_IO))
1379 err = ubi->mtd->read(ubi->mtd, addr, len, &read, ubi->dbg_peb_buf); 1416 return 0;
1417
1418 buf = __vmalloc(len, GFP_NOFS, PAGE_KERNEL);
1419 if (!buf) {
1420 ubi_err("cannot allocate memory to check for 0xFFs");
1421 return 0;
1422 }
1423
1424 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
1380 if (err && err != -EUCLEAN) { 1425 if (err && err != -EUCLEAN) {
1381 ubi_err("error %d while reading %d bytes from PEB %d:%d, " 1426 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1382 "read %zd bytes", err, len, pnum, offset, read); 1427 "read %zd bytes", err, len, pnum, offset, read);
1383 goto error; 1428 goto error;
1384 } 1429 }
1385 1430
1386 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len); 1431 err = ubi_check_pattern(buf, 0xFF, len);
1387 if (err == 0) { 1432 if (err == 0) {
1388 ubi_err("flash region at PEB %d:%d, length %d does not " 1433 ubi_err("flash region at PEB %d:%d, length %d does not "
1389 "contain all 0xFF bytes", pnum, offset, len); 1434 "contain all 0xFF bytes", pnum, offset, len);
1390 goto fail; 1435 goto fail;
1391 } 1436 }
1392 mutex_unlock(&ubi->dbg_buf_mutex);
1393 1437
1438 vfree(buf);
1394 return 0; 1439 return 0;
1395 1440
1396fail: 1441fail:
1397 ubi_err("paranoid check failed for PEB %d", pnum); 1442 ubi_err("paranoid check failed for PEB %d", pnum);
1398 ubi_msg("hex dump of the %d-%d region", offset, offset + len); 1443 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1399 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1444 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, buf, len, 1);
1400 ubi->dbg_peb_buf, len, 1);
1401 err = -EINVAL; 1445 err = -EINVAL;
1402error: 1446error:
1403 ubi_dbg_dump_stack(); 1447 ubi_dbg_dump_stack();
1404 mutex_unlock(&ubi->dbg_buf_mutex); 1448 vfree(buf);
1405 return err; 1449 return err;
1406} 1450}
1407 1451
1408#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1452#endif /* CONFIG_MTD_UBI_DEBUG */