aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi')
-rw-r--r--drivers/mtd/ubi/Kconfig25
-rw-r--r--drivers/mtd/ubi/Kconfig.debug94
-rw-r--r--drivers/mtd/ubi/build.c40
-rw-r--r--drivers/mtd/ubi/cdev.c13
-rw-r--r--drivers/mtd/ubi/debug.c29
-rw-r--r--drivers/mtd/ubi/debug.h224
-rw-r--r--drivers/mtd/ubi/eba.c10
-rw-r--r--drivers/mtd/ubi/gluebi.c6
-rw-r--r--drivers/mtd/ubi/io.c326
-rw-r--r--drivers/mtd/ubi/kapi.c2
-rw-r--r--drivers/mtd/ubi/misc.c19
-rw-r--r--drivers/mtd/ubi/scan.c450
-rw-r--r--drivers/mtd/ubi/scan.h21
-rw-r--r--drivers/mtd/ubi/ubi-media.h6
-rw-r--r--drivers/mtd/ubi/ubi.h43
-rw-r--r--drivers/mtd/ubi/vmt.c18
-rw-r--r--drivers/mtd/ubi/vtbl.c25
-rw-r--r--drivers/mtd/ubi/wl.c54
18 files changed, 820 insertions, 585 deletions
diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig
index f702a163d8df..4dcc752a0c0b 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
@@ -58,5 +52,12 @@ config MTD_UBI_GLUEBI
58 work on top of UBI. Do not enable this unless you use legacy 52 work on top of UBI. Do not enable this unless you use legacy
59 software. 53 software.
60 54
61source "drivers/mtd/ubi/Kconfig.debug" 55config MTD_UBI_DEBUG
62endmenu 56 bool "UBI debugging"
57 depends on SYSFS
58 select DEBUG_FS
59 select KALLSYMS
60 help
61 This option enables UBI debugging.
62
63endif # MTD_UBI
diff --git a/drivers/mtd/ubi/Kconfig.debug b/drivers/mtd/ubi/Kconfig.debug
deleted file mode 100644
index 61f6e5e40458..000000000000
--- a/drivers/mtd/ubi/Kconfig.debug
+++ /dev/null
@@ -1,94 +0,0 @@
1comment "UBI debugging options"
2 depends on MTD_UBI
3
4config MTD_UBI_DEBUG
5 bool "UBI debugging"
6 depends on SYSFS
7 depends on MTD_UBI
8 select DEBUG_FS
9 select KALLSYMS_ALL if KALLSYMS && DEBUG_KERNEL
10 help
11 This option enables UBI debugging.
12
13config MTD_UBI_DEBUG_MSG
14 bool "UBI debugging messages"
15 depends on MTD_UBI_DEBUG
16 default n
17 help
18 This option enables UBI debugging messages.
19
20config MTD_UBI_DEBUG_PARANOID
21 bool "Extra self-checks"
22 default n
23 depends on MTD_UBI_DEBUG
24 help
25 This option enables extra checks in UBI code. Note this slows UBI down
26 significantly.
27
28config MTD_UBI_DEBUG_DISABLE_BGT
29 bool "Do not enable the UBI background thread"
30 depends on MTD_UBI_DEBUG
31 default n
32 help
33 This option switches the background thread off by default. The thread
34 may be also be enabled/disabled via UBI sysfs.
35
36config MTD_UBI_DEBUG_EMULATE_BITFLIPS
37 bool "Emulate flash bit-flips"
38 depends on MTD_UBI_DEBUG
39 default n
40 help
41 This option emulates bit-flips with probability 1/50, which in turn
42 causes scrubbing. Useful for debugging and stressing UBI.
43
44config MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES
45 bool "Emulate flash write failures"
46 depends on MTD_UBI_DEBUG
47 default n
48 help
49 This option emulates write failures with probability 1/100. Useful for
50 debugging and testing how UBI handlines errors.
51
52config MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES
53 bool "Emulate flash erase failures"
54 depends on MTD_UBI_DEBUG
55 default n
56 help
57 This option emulates erase failures with probability 1/100. Useful for
58 debugging and testing how UBI handlines errors.
59
60menu "Additional UBI debugging messages"
61 depends on MTD_UBI_DEBUG
62
63config MTD_UBI_DEBUG_MSG_BLD
64 bool "Additional UBI initialization and build messages"
65 default n
66 depends on MTD_UBI_DEBUG
67 help
68 This option enables detailed UBI initialization and device build
69 debugging messages.
70
71config MTD_UBI_DEBUG_MSG_EBA
72 bool "Eraseblock association unit messages"
73 default n
74 depends on MTD_UBI_DEBUG
75 help
76 This option enables debugging messages from the UBI eraseblock
77 association unit.
78
79config MTD_UBI_DEBUG_MSG_WL
80 bool "Wear-leveling unit messages"
81 default n
82 depends on MTD_UBI_DEBUG
83 help
84 This option enables debugging messages from the UBI wear-leveling
85 unit.
86
87config MTD_UBI_DEBUG_MSG_IO
88 bool "Input/output unit messages"
89 default n
90 depends on MTD_UBI_DEBUG
91 help
92 This option enables debugging messages from the UBI input/output unit.
93
94endmenu # UBI debugging messages
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 78ae89488a4f..65626c1c446d 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);
@@ -689,11 +690,25 @@ static int io_init(struct ubi_device *ubi)
689 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size); 690 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
690 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0); 691 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
691 692
693 ubi->max_write_size = ubi->mtd->writebufsize;
694 /*
695 * Maximum write size has to be greater or equivalent to min. I/O
696 * size, and be multiple of min. I/O size.
697 */
698 if (ubi->max_write_size < ubi->min_io_size ||
699 ubi->max_write_size % ubi->min_io_size ||
700 !is_power_of_2(ubi->max_write_size)) {
701 ubi_err("bad write buffer size %d for %d min. I/O unit",
702 ubi->max_write_size, ubi->min_io_size);
703 return -EINVAL;
704 }
705
692 /* Calculate default aligned sizes of EC and VID headers */ 706 /* Calculate default aligned sizes of EC and VID headers */
693 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size); 707 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
694 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size); 708 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
695 709
696 dbg_msg("min_io_size %d", ubi->min_io_size); 710 dbg_msg("min_io_size %d", ubi->min_io_size);
711 dbg_msg("max_write_size %d", ubi->max_write_size);
697 dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size); 712 dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
698 dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize); 713 dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
699 dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize); 714 dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
@@ -710,7 +725,7 @@ static int io_init(struct ubi_device *ubi)
710 } 725 }
711 726
712 /* Similar for the data offset */ 727 /* Similar for the data offset */
713 ubi->leb_start = ubi->vid_hdr_offset + UBI_EC_HDR_SIZE; 728 ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
714 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size); 729 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
715 730
716 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset); 731 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
@@ -922,6 +937,8 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
922 spin_lock_init(&ubi->volumes_lock); 937 spin_lock_init(&ubi->volumes_lock);
923 938
924 ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num); 939 ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
940 dbg_msg("sizeof(struct ubi_scan_leb) %zu", sizeof(struct ubi_scan_leb));
941 dbg_msg("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
925 942
926 err = io_init(ubi); 943 err = io_init(ubi);
927 if (err) 944 if (err)
@@ -936,13 +953,6 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
936 if (!ubi->peb_buf2) 953 if (!ubi->peb_buf2)
937 goto out_free; 954 goto out_free;
938 955
939#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
940 mutex_init(&ubi->dbg_buf_mutex);
941 ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
942 if (!ubi->dbg_peb_buf)
943 goto out_free;
944#endif
945
946 err = attach_by_scanning(ubi); 956 err = attach_by_scanning(ubi);
947 if (err) { 957 if (err) {
948 dbg_err("failed to attach by scanning, error %d", err); 958 dbg_err("failed to attach by scanning, error %d", err);
@@ -972,6 +982,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); 982 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
973 ubi_msg("number of good PEBs: %d", ubi->good_peb_count); 983 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
974 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); 984 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
985 ubi_msg("number of corrupted PEBs: %d", ubi->corr_peb_count);
975 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); 986 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
976 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); 987 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
977 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); 988 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
@@ -989,8 +1000,7 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
989 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up. 1000 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
990 */ 1001 */
991 spin_lock(&ubi->wl_lock); 1002 spin_lock(&ubi->wl_lock);
992 if (!DBG_DISABLE_BGT) 1003 ubi->thread_enabled = 1;
993 ubi->thread_enabled = 1;
994 wake_up_process(ubi->bgt_thread); 1004 wake_up_process(ubi->bgt_thread);
995 spin_unlock(&ubi->wl_lock); 1005 spin_unlock(&ubi->wl_lock);
996 1006
@@ -1007,9 +1017,6 @@ out_detach:
1007out_free: 1017out_free:
1008 vfree(ubi->peb_buf1); 1018 vfree(ubi->peb_buf1);
1009 vfree(ubi->peb_buf2); 1019 vfree(ubi->peb_buf2);
1010#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1011 vfree(ubi->dbg_peb_buf);
1012#endif
1013 if (ref) 1020 if (ref)
1014 put_device(&ubi->dev); 1021 put_device(&ubi->dev);
1015 else 1022 else
@@ -1080,9 +1087,6 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1080 put_mtd_device(ubi->mtd); 1087 put_mtd_device(ubi->mtd);
1081 vfree(ubi->peb_buf1); 1088 vfree(ubi->peb_buf1);
1082 vfree(ubi->peb_buf2); 1089 vfree(ubi->peb_buf2);
1083#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1084 vfree(ubi->dbg_peb_buf);
1085#endif
1086 ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num); 1090 ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
1087 put_device(&ubi->dev); 1091 put_device(&ubi->dev);
1088 return 0; 1092 return 0;
diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index 3d2d1a69e9a0..191f3bb3c41a 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -115,7 +115,7 @@ static int vol_cdev_open(struct inode *inode, struct file *file)
115 mode = UBI_READONLY; 115 mode = UBI_READONLY;
116 116
117 dbg_gen("open device %d, volume %d, mode %d", 117 dbg_gen("open device %d, volume %d, mode %d",
118 ubi_num, vol_id, mode); 118 ubi_num, vol_id, mode);
119 119
120 desc = ubi_open_volume(ubi_num, vol_id, mode); 120 desc = ubi_open_volume(ubi_num, vol_id, mode);
121 if (IS_ERR(desc)) 121 if (IS_ERR(desc))
@@ -158,7 +158,7 @@ static loff_t vol_cdev_llseek(struct file *file, loff_t offset, int origin)
158 loff_t new_offset; 158 loff_t new_offset;
159 159
160 if (vol->updating) { 160 if (vol->updating) {
161 /* Update is in progress, seeking is prohibited */ 161 /* Update is in progress, seeking is prohibited */
162 dbg_err("updating"); 162 dbg_err("updating");
163 return -EBUSY; 163 return -EBUSY;
164 } 164 }
@@ -561,18 +561,18 @@ static long vol_cdev_ioctl(struct file *file, unsigned int cmd,
561 } 561 }
562 562
563 /* Set volume property command */ 563 /* Set volume property command */
564 case UBI_IOCSETPROP: 564 case UBI_IOCSETVOLPROP:
565 { 565 {
566 struct ubi_set_prop_req req; 566 struct ubi_set_vol_prop_req req;
567 567
568 err = copy_from_user(&req, argp, 568 err = copy_from_user(&req, argp,
569 sizeof(struct ubi_set_prop_req)); 569 sizeof(struct ubi_set_vol_prop_req));
570 if (err) { 570 if (err) {
571 err = -EFAULT; 571 err = -EFAULT;
572 break; 572 break;
573 } 573 }
574 switch (req.property) { 574 switch (req.property) {
575 case UBI_PROP_DIRECT_WRITE: 575 case UBI_VOL_PROP_DIRECT_WRITE:
576 mutex_lock(&ubi->device_mutex); 576 mutex_lock(&ubi->device_mutex);
577 desc->vol->direct_writes = !!req.value; 577 desc->vol->direct_writes = !!req.value;
578 mutex_unlock(&ubi->device_mutex); 578 mutex_unlock(&ubi->device_mutex);
@@ -1100,4 +1100,5 @@ const struct file_operations ubi_ctrl_cdev_operations = {
1100 .owner = THIS_MODULE, 1100 .owner = THIS_MODULE,
1101 .unlocked_ioctl = ctrl_cdev_ioctl, 1101 .unlocked_ioctl = ctrl_cdev_ioctl,
1102 .compat_ioctl = ctrl_cdev_compat_ioctl, 1102 .compat_ioctl = ctrl_cdev_compat_ioctl,
1103 .llseek = no_llseek,
1103}; 1104};
diff --git a/drivers/mtd/ubi/debug.c b/drivers/mtd/ubi/debug.c
index 4876977e52cb..2224cbe41ddf 100644
--- a/drivers/mtd/ubi/debug.c
+++ b/drivers/mtd/ubi/debug.c
@@ -27,6 +27,17 @@
27#ifdef CONFIG_MTD_UBI_DEBUG 27#ifdef CONFIG_MTD_UBI_DEBUG
28 28
29#include "ubi.h" 29#include "ubi.h"
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32
33unsigned int ubi_chk_flags;
34unsigned int ubi_tst_flags;
35
36module_param_named(debug_chks, ubi_chk_flags, uint, S_IRUGO | S_IWUSR);
37module_param_named(debug_tsts, ubi_chk_flags, uint, S_IRUGO | S_IWUSR);
38
39MODULE_PARM_DESC(debug_chks, "Debug check flags");
40MODULE_PARM_DESC(debug_tsts, "Debug special test flags");
30 41
31/** 42/**
32 * ubi_dbg_dump_ec_hdr - dump an erase counter header. 43 * ubi_dbg_dump_ec_hdr - dump an erase counter header.
@@ -61,15 +72,15 @@ void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
61{ 72{
62 printk(KERN_DEBUG "Volume identifier header dump:\n"); 73 printk(KERN_DEBUG "Volume identifier header dump:\n");
63 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic)); 74 printk(KERN_DEBUG "\tmagic %08x\n", be32_to_cpu(vid_hdr->magic));
64 printk(KERN_DEBUG "\tversion %d\n", (int)vid_hdr->version); 75 printk(KERN_DEBUG "\tversion %d\n", (int)vid_hdr->version);
65 printk(KERN_DEBUG "\tvol_type %d\n", (int)vid_hdr->vol_type); 76 printk(KERN_DEBUG "\tvol_type %d\n", (int)vid_hdr->vol_type);
66 printk(KERN_DEBUG "\tcopy_flag %d\n", (int)vid_hdr->copy_flag); 77 printk(KERN_DEBUG "\tcopy_flag %d\n", (int)vid_hdr->copy_flag);
67 printk(KERN_DEBUG "\tcompat %d\n", (int)vid_hdr->compat); 78 printk(KERN_DEBUG "\tcompat %d\n", (int)vid_hdr->compat);
68 printk(KERN_DEBUG "\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id)); 79 printk(KERN_DEBUG "\tvol_id %d\n", be32_to_cpu(vid_hdr->vol_id));
69 printk(KERN_DEBUG "\tlnum %d\n", be32_to_cpu(vid_hdr->lnum)); 80 printk(KERN_DEBUG "\tlnum %d\n", be32_to_cpu(vid_hdr->lnum));
70 printk(KERN_DEBUG "\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size)); 81 printk(KERN_DEBUG "\tdata_size %d\n", be32_to_cpu(vid_hdr->data_size));
71 printk(KERN_DEBUG "\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs)); 82 printk(KERN_DEBUG "\tused_ebs %d\n", be32_to_cpu(vid_hdr->used_ebs));
72 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad)); 83 printk(KERN_DEBUG "\tdata_pad %d\n", be32_to_cpu(vid_hdr->data_pad));
73 printk(KERN_DEBUG "\tsqnum %llu\n", 84 printk(KERN_DEBUG "\tsqnum %llu\n",
74 (unsigned long long)be64_to_cpu(vid_hdr->sqnum)); 85 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
75 printk(KERN_DEBUG "\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc)); 86 printk(KERN_DEBUG "\thdr_crc %08x\n", be32_to_cpu(vid_hdr->hdr_crc));
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index 17a107129726..3f1a09c5c438 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -21,11 +21,17 @@
21#ifndef __UBI_DEBUG_H__ 21#ifndef __UBI_DEBUG_H__
22#define __UBI_DEBUG_H__ 22#define __UBI_DEBUG_H__
23 23
24struct ubi_ec_hdr;
25struct ubi_vid_hdr;
26struct ubi_volume;
27struct ubi_vtbl_record;
28struct ubi_scan_volume;
29struct ubi_scan_leb;
30struct ubi_mkvol_req;
31
24#ifdef CONFIG_MTD_UBI_DEBUG 32#ifdef CONFIG_MTD_UBI_DEBUG
25#include <linux/random.h> 33#include <linux/random.h>
26 34
27#define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
28
29#define ubi_assert(expr) do { \ 35#define ubi_assert(expr) do { \
30 if (unlikely(!(expr))) { \ 36 if (unlikely(!(expr))) { \
31 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \ 37 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
@@ -34,19 +40,28 @@
34 } \ 40 } \
35} while (0) 41} while (0)
36 42
37#define dbg_msg(fmt, ...) \ 43#define dbg_err(fmt, ...) ubi_err(fmt, ##__VA_ARGS__)
38 printk(KERN_DEBUG "UBI DBG (pid %d): %s: " fmt "\n", \
39 current->pid, __func__, ##__VA_ARGS__)
40 44
41#define ubi_dbg_dump_stack() dump_stack() 45#define ubi_dbg_dump_stack() dump_stack()
42 46
43struct ubi_ec_hdr; 47#define ubi_dbg_print_hex_dump(l, ps, pt, r, g, b, len, a) \
44struct ubi_vid_hdr; 48 print_hex_dump(l, ps, pt, r, g, b, len, a)
45struct ubi_volume; 49
46struct ubi_vtbl_record; 50#define ubi_dbg_msg(type, fmt, ...) \
47struct ubi_scan_volume; 51 pr_debug("UBI DBG " type ": " fmt "\n", ##__VA_ARGS__)
48struct ubi_scan_leb; 52
49struct ubi_mkvol_req; 53/* Just a debugging messages not related to any specific UBI subsystem */
54#define dbg_msg(fmt, ...) ubi_dbg_msg("msg", fmt, ##__VA_ARGS__)
55/* General debugging messages */
56#define dbg_gen(fmt, ...) ubi_dbg_msg("gen", fmt, ##__VA_ARGS__)
57/* Messages from the eraseblock association sub-system */
58#define dbg_eba(fmt, ...) ubi_dbg_msg("eba", fmt, ##__VA_ARGS__)
59/* Messages from the wear-leveling sub-system */
60#define dbg_wl(fmt, ...) ubi_dbg_msg("wl", fmt, ##__VA_ARGS__)
61/* Messages from the input/output sub-system */
62#define dbg_io(fmt, ...) ubi_dbg_msg("io", fmt, ##__VA_ARGS__)
63/* Initialization and build messages */
64#define dbg_bld(fmt, ...) ubi_dbg_msg("bld", fmt, ##__VA_ARGS__)
50 65
51void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr); 66void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
52void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr); 67void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
@@ -57,59 +72,51 @@ 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); 72void 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); 73void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
59 74
60#ifdef CONFIG_MTD_UBI_DEBUG_MSG 75extern unsigned int ubi_chk_flags;
61/* General debugging messages */
62#define dbg_gen(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
63#else
64#define dbg_gen(fmt, ...) ({})
65#endif
66
67#ifdef CONFIG_MTD_UBI_DEBUG_MSG_EBA
68/* Messages from the eraseblock association sub-system */
69#define dbg_eba(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
70#else
71#define dbg_eba(fmt, ...) ({})
72#endif
73
74#ifdef CONFIG_MTD_UBI_DEBUG_MSG_WL
75/* Messages from the wear-leveling sub-system */
76#define dbg_wl(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
77#else
78#define dbg_wl(fmt, ...) ({})
79#endif
80
81#ifdef CONFIG_MTD_UBI_DEBUG_MSG_IO
82/* Messages from the input/output sub-system */
83#define dbg_io(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
84#else
85#define dbg_io(fmt, ...) ({})
86#endif
87 76
88#ifdef CONFIG_MTD_UBI_DEBUG_MSG_BLD 77/*
89/* Initialization and build messages */ 78 * Debugging check flags.
90#define dbg_bld(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 79 *
91#define UBI_IO_DEBUG 1 80 * UBI_CHK_GEN: general checks
92#else 81 * UBI_CHK_IO: check writes and erases
93#define dbg_bld(fmt, ...) ({}) 82 */
94#define UBI_IO_DEBUG 0 83enum {
95#endif 84 UBI_CHK_GEN = 0x1,
85 UBI_CHK_IO = 0x2,
86};
96 87
97#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
98int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len); 88int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
99int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum, 89int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
100 int offset, int len); 90 int offset, int len);
101#else
102#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0
103#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0
104#endif
105 91
106#ifdef CONFIG_MTD_UBI_DEBUG_DISABLE_BGT 92extern unsigned int ubi_tst_flags;
107#define DBG_DISABLE_BGT 1 93
108#else 94/*
109#define DBG_DISABLE_BGT 0 95 * Special testing flags.
110#endif 96 *
97 * UBIFS_TST_DISABLE_BGT: disable the background thread
98 * UBI_TST_EMULATE_BITFLIPS: emulate bit-flips
99 * UBI_TST_EMULATE_WRITE_FAILURES: emulate write failures
100 * UBI_TST_EMULATE_ERASE_FAILURES: emulate erase failures
101 */
102enum {
103 UBI_TST_DISABLE_BGT = 0x1,
104 UBI_TST_EMULATE_BITFLIPS = 0x2,
105 UBI_TST_EMULATE_WRITE_FAILURES = 0x4,
106 UBI_TST_EMULATE_ERASE_FAILURES = 0x8,
107};
108
109/**
110 * ubi_dbg_is_bgt_disabled - if the background thread is disabled.
111 *
112 * Returns non-zero if the UBI background thread is disabled for testing
113 * purposes.
114 */
115static inline int ubi_dbg_is_bgt_disabled(void)
116{
117 return ubi_tst_flags & UBI_TST_DISABLE_BGT;
118}
111 119
112#ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_BITFLIPS
113/** 120/**
114 * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip. 121 * ubi_dbg_is_bitflip - if it is time to emulate a bit-flip.
115 * 122 *
@@ -117,13 +124,11 @@ int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
117 */ 124 */
118static inline int ubi_dbg_is_bitflip(void) 125static inline int ubi_dbg_is_bitflip(void)
119{ 126{
120 return !(random32() % 200); 127 if (ubi_tst_flags & UBI_TST_EMULATE_BITFLIPS)
128 return !(random32() % 200);
129 return 0;
121} 130}
122#else
123#define ubi_dbg_is_bitflip() 0
124#endif
125 131
126#ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_WRITE_FAILURES
127/** 132/**
128 * ubi_dbg_is_write_failure - if it is time to emulate a write failure. 133 * ubi_dbg_is_write_failure - if it is time to emulate a write failure.
129 * 134 *
@@ -132,13 +137,11 @@ static inline int ubi_dbg_is_bitflip(void)
132 */ 137 */
133static inline int ubi_dbg_is_write_failure(void) 138static inline int ubi_dbg_is_write_failure(void)
134{ 139{
135 return !(random32() % 500); 140 if (ubi_tst_flags & UBI_TST_EMULATE_WRITE_FAILURES)
141 return !(random32() % 500);
142 return 0;
136} 143}
137#else
138#define ubi_dbg_is_write_failure() 0
139#endif
140 144
141#ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_ERASE_FAILURES
142/** 145/**
143 * ubi_dbg_is_erase_failure - if its time to emulate an erase failure. 146 * ubi_dbg_is_erase_failure - if its time to emulate an erase failure.
144 * 147 *
@@ -147,39 +150,68 @@ static inline int ubi_dbg_is_write_failure(void)
147 */ 150 */
148static inline int ubi_dbg_is_erase_failure(void) 151static inline int ubi_dbg_is_erase_failure(void)
149{ 152{
153 if (ubi_tst_flags & UBI_TST_EMULATE_ERASE_FAILURES)
150 return !(random32() % 400); 154 return !(random32() % 400);
155 return 0;
151} 156}
152#else
153#define ubi_dbg_is_erase_failure() 0
154#endif
155 157
156#else 158#else
157 159
158#define ubi_assert(expr) ({}) 160/* Use "if (0)" to make compiler check arguments even if debugging is off */
159#define dbg_err(fmt, ...) ({}) 161#define ubi_assert(expr) do { \
160#define dbg_msg(fmt, ...) ({}) 162 if (0) { \
161#define dbg_gen(fmt, ...) ({}) 163 printk(KERN_CRIT "UBI assert failed in %s at %u (pid %d)\n", \
162#define dbg_eba(fmt, ...) ({}) 164 __func__, __LINE__, current->pid); \
163#define dbg_wl(fmt, ...) ({}) 165 } \
164#define dbg_io(fmt, ...) ({}) 166} while (0)
165#define dbg_bld(fmt, ...) ({}) 167
166#define ubi_dbg_dump_stack() ({}) 168#define dbg_err(fmt, ...) do { \
167#define ubi_dbg_dump_ec_hdr(ec_hdr) ({}) 169 if (0) \
168#define ubi_dbg_dump_vid_hdr(vid_hdr) ({}) 170 ubi_err(fmt, ##__VA_ARGS__); \
169#define ubi_dbg_dump_vol_info(vol) ({}) 171} while (0)
170#define ubi_dbg_dump_vtbl_record(r, idx) ({}) 172
171#define ubi_dbg_dump_sv(sv) ({}) 173#define ubi_dbg_msg(fmt, ...) do { \
172#define ubi_dbg_dump_seb(seb, type) ({}) 174 if (0) \
173#define ubi_dbg_dump_mkvol_req(req) ({}) 175 pr_debug(fmt "\n", ##__VA_ARGS__); \
174#define ubi_dbg_dump_flash(ubi, pnum, offset, len) ({}) 176} while (0)
175 177
176#define UBI_IO_DEBUG 0 178#define dbg_msg(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
177#define DBG_DISABLE_BGT 0 179#define dbg_gen(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
178#define ubi_dbg_is_bitflip() 0 180#define dbg_eba(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
179#define ubi_dbg_is_write_failure() 0 181#define dbg_wl(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
180#define ubi_dbg_is_erase_failure() 0 182#define dbg_io(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
181#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0 183#define dbg_bld(fmt, ...) ubi_dbg_msg(fmt, ##__VA_ARGS__)
182#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0 184
185static inline void ubi_dbg_dump_stack(void) { return; }
186static inline void
187ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) { return; }
188static inline void
189ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) { return; }
190static inline void
191ubi_dbg_dump_vol_info(const struct ubi_volume *vol) { return; }
192static inline void
193ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) { return; }
194static inline void ubi_dbg_dump_sv(const struct ubi_scan_volume *sv) { return; }
195static inline void ubi_dbg_dump_seb(const struct ubi_scan_leb *seb,
196 int type) { return; }
197static inline void
198ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req) { return; }
199static inline void ubi_dbg_dump_flash(struct ubi_device *ubi,
200 int pnum, int offset, int len) { return; }
201static inline void
202ubi_dbg_print_hex_dump(const char *l, const char *ps, int pt, int r,
203 int g, const void *b, size_t len, bool a) { return; }
204
205static inline int ubi_dbg_is_bgt_disabled(void) { return 0; }
206static inline int ubi_dbg_is_bitflip(void) { return 0; }
207static inline int ubi_dbg_is_write_failure(void) { return 0; }
208static inline int ubi_dbg_is_erase_failure(void) { return 0; }
209static inline int ubi_dbg_check_all_ff(struct ubi_device *ubi,
210 int pnum, int offset,
211 int len) { return 0; }
212static inline int ubi_dbg_check_write(struct ubi_device *ubi,
213 const void *buf, int pnum,
214 int offset, int len) { return 0; }
183 215
184#endif /* !CONFIG_MTD_UBI_DEBUG */ 216#endif /* !CONFIG_MTD_UBI_DEBUG */
185#endif /* !__UBI_DEBUG_H__ */ 217#endif /* !__UBI_DEBUG_H__ */
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/gluebi.c b/drivers/mtd/ubi/gluebi.c
index 9aa81584c8a2..941bc3c05d6e 100644
--- a/drivers/mtd/ubi/gluebi.c
+++ b/drivers/mtd/ubi/gluebi.c
@@ -365,7 +365,7 @@ static int gluebi_create(struct ubi_device_info *di,
365 vi->vol_id); 365 vi->vol_id);
366 mutex_unlock(&devices_mutex); 366 mutex_unlock(&devices_mutex);
367 367
368 if (add_mtd_device(mtd)) { 368 if (mtd_device_register(mtd, NULL, 0)) {
369 err_msg("cannot add MTD device"); 369 err_msg("cannot add MTD device");
370 kfree(mtd->name); 370 kfree(mtd->name);
371 kfree(gluebi); 371 kfree(gluebi);
@@ -407,7 +407,7 @@ static int gluebi_remove(struct ubi_volume_info *vi)
407 return err; 407 return err;
408 408
409 mtd = &gluebi->mtd; 409 mtd = &gluebi->mtd;
410 err = del_mtd_device(mtd); 410 err = mtd_device_unregister(mtd);
411 if (err) { 411 if (err) {
412 err_msg("cannot remove fake MTD device %d, UBI device %d, " 412 err_msg("cannot remove fake MTD device %d, UBI device %d, "
413 "volume %d, error %d", mtd->index, gluebi->ubi_num, 413 "volume %d, error %d", mtd->index, gluebi->ubi_num,
@@ -524,7 +524,7 @@ static void __exit ubi_gluebi_exit(void)
524 int err; 524 int err;
525 struct mtd_info *mtd = &gluebi->mtd; 525 struct mtd_info *mtd = &gluebi->mtd;
526 526
527 err = del_mtd_device(mtd); 527 err = mtd_device_unregister(mtd);
528 if (err) 528 if (err)
529 err_msg("error %d while removing gluebi MTD device %d, " 529 err_msg("error %d while removing gluebi MTD device %d, "
530 "UBI device %d, volume %d - ignoring", err, 530 "UBI device %d, volume %d - ignoring", err,
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 */
diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c
index 69fa4ef03c53..d39716e5b204 100644
--- a/drivers/mtd/ubi/kapi.c
+++ b/drivers/mtd/ubi/kapi.c
@@ -40,7 +40,9 @@ void ubi_do_get_device_info(struct ubi_device *ubi, struct ubi_device_info *di)
40{ 40{
41 di->ubi_num = ubi->ubi_num; 41 di->ubi_num = ubi->ubi_num;
42 di->leb_size = ubi->leb_size; 42 di->leb_size = ubi->leb_size;
43 di->leb_start = ubi->leb_start;
43 di->min_io_size = ubi->min_io_size; 44 di->min_io_size = ubi->min_io_size;
45 di->max_write_size = ubi->max_write_size;
44 di->ro_mode = ubi->ro_mode; 46 di->ro_mode = ubi->ro_mode;
45 di->cdev = ubi->cdev.dev; 47 di->cdev = ubi->cdev.dev;
46} 48}
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..2135a53732ff 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -29,7 +29,7 @@
29 * objects which are kept in volume RB-tree with root at the @volumes field. 29 * objects which are kept in volume RB-tree with root at the @volumes field.
30 * The RB-tree is indexed by the volume ID. 30 * The RB-tree is indexed by the volume ID.
31 * 31 *
32 * Found logical eraseblocks are represented by &struct ubi_scan_leb objects. 32 * Scanned logical eraseblocks are represented by &struct ubi_scan_leb objects.
33 * These objects are kept in per-volume RB-trees with the root at the 33 * These objects are kept in per-volume RB-trees with the root at the
34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep 34 * corresponding &struct ubi_scan_volume object. To put it differently, we keep
35 * an RB-tree of per-volume objects and each of these objects is the root of 35 * an RB-tree of per-volume objects and each of these objects is the root of
@@ -38,6 +38,47 @@
38 * Corrupted physical eraseblocks are put to the @corr list, free physical 38 * Corrupted physical eraseblocks are put to the @corr list, free physical
39 * eraseblocks are put to the @free list and the physical eraseblock to be 39 * eraseblocks are put to the @free list and the physical eraseblock to be
40 * erased are put to the @erase list. 40 * erased are put to the @erase list.
41 *
42 * About corruptions
43 * ~~~~~~~~~~~~~~~~~
44 *
45 * UBI protects EC and VID headers with CRC-32 checksums, so it can detect
46 * whether the headers are corrupted or not. Sometimes UBI also protects the
47 * data with CRC-32, e.g., when it executes the atomic LEB change operation, or
48 * when it moves the contents of a PEB for wear-leveling purposes.
49 *
50 * UBI tries to distinguish between 2 types of corruptions.
51 *
52 * 1. Corruptions caused by power cuts. These are expected corruptions and UBI
53 * tries to handle them gracefully, without printing too many warnings and
54 * error messages. The idea is that we do not lose important data in these case
55 * - we may lose only the data which was being written to the media just before
56 * the power cut happened, and the upper layers (e.g., UBIFS) are supposed to
57 * handle such data losses (e.g., by using the FS journal).
58 *
59 * When UBI detects a corruption (CRC-32 mismatch) in a PEB, and it looks like
60 * the reason is a power cut, UBI puts this PEB to the @erase list, and all
61 * PEBs in the @erase list are scheduled for erasure later.
62 *
63 * 2. Unexpected corruptions which are not caused by power cuts. During
64 * scanning, such PEBs are put to the @corr list and UBI preserves them.
65 * Obviously, this lessens the amount of available PEBs, and if at some point
66 * UBI runs out of free PEBs, it switches to R/O mode. UBI also loudly informs
67 * about such PEBs every time the MTD device is attached.
68 *
69 * However, it is difficult to reliably distinguish between these types of
70 * corruptions and UBI's strategy is as follows. UBI assumes corruption type 2
71 * if the VID header is corrupted and the data area does not contain all 0xFFs,
72 * and there were no bit-flips or integrity errors while reading the data area.
73 * Otherwise UBI assumes corruption type 1. So the decision criteria are as
74 * follows.
75 * o If the data area contains only 0xFFs, there is no data, and it is safe
76 * to just erase this PEB - this is corruption type 1.
77 * o If the data area has bit-flips or data integrity errors (ECC errors on
78 * NAND), it is probably a PEB which was being erased when power cut
79 * happened, so this is corruption type 1. However, this is just a guess,
80 * which might be wrong.
81 * o Otherwise this it corruption type 2.
41 */ 82 */
42 83
43#include <linux/err.h> 84#include <linux/err.h>
@@ -47,7 +88,7 @@
47#include <linux/random.h> 88#include <linux/random.h>
48#include "ubi.h" 89#include "ubi.h"
49 90
50#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 91#ifdef CONFIG_MTD_UBI_DEBUG
51static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si); 92static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si);
52#else 93#else
53#define paranoid_check_si(ubi, si) 0 94#define paranoid_check_si(ubi, si) 0
@@ -62,39 +103,69 @@ static struct ubi_vid_hdr *vidh;
62 * @si: scanning information 103 * @si: scanning information
63 * @pnum: physical eraseblock number to add 104 * @pnum: physical eraseblock number to add
64 * @ec: erase counter of the physical eraseblock 105 * @ec: erase counter of the physical eraseblock
106 * @to_head: if not zero, add to the head of the list
65 * @list: the list to add to 107 * @list: the list to add to
66 * 108 *
67 * This function adds physical eraseblock @pnum to free, erase, corrupted or 109 * This function adds physical eraseblock @pnum to free, erase, or alien lists.
68 * alien lists. Returns zero in case of success and a negative error code in 110 * If @to_head is not zero, PEB will be added to the head of the list, which
69 * case of failure. 111 * basically means it will be processed first later. E.g., we add corrupted
112 * PEBs (corrupted due to power cuts) to the head of the erase list to make
113 * sure we erase them first and get rid of corruptions ASAP. This function
114 * returns zero in case of success and a negative error code in case of
115 * failure.
70 */ 116 */
71static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, 117static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, int to_head,
72 struct list_head *list) 118 struct list_head *list)
73{ 119{
74 struct ubi_scan_leb *seb; 120 struct ubi_scan_leb *seb;
75 121
76 if (list == &si->free) { 122 if (list == &si->free) {
77 dbg_bld("add to free: PEB %d, EC %d", pnum, ec); 123 dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
78 si->free_peb_count += 1;
79 } else if (list == &si->erase) { 124 } else if (list == &si->erase) {
80 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); 125 dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
81 si->erase_peb_count += 1;
82 } else if (list == &si->corr) {
83 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
84 si->corr_peb_count += 1;
85 } else if (list == &si->alien) { 126 } else if (list == &si->alien) {
86 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); 127 dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
87 si->alien_peb_count += 1; 128 si->alien_peb_count += 1;
88 } else 129 } else
89 BUG(); 130 BUG();
90 131
91 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); 132 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
133 if (!seb)
134 return -ENOMEM;
135
136 seb->pnum = pnum;
137 seb->ec = ec;
138 if (to_head)
139 list_add(&seb->u.list, list);
140 else
141 list_add_tail(&seb->u.list, list);
142 return 0;
143}
144
145/**
146 * add_corrupted - add a corrupted physical eraseblock.
147 * @si: scanning information
148 * @pnum: physical eraseblock number to add
149 * @ec: erase counter of the physical eraseblock
150 *
151 * This function adds corrupted physical eraseblock @pnum to the 'corr' list.
152 * The corruption was presumably not caused by a power cut. Returns zero in
153 * case of success and a negative error code in case of failure.
154 */
155static int add_corrupted(struct ubi_scan_info *si, int pnum, int ec)
156{
157 struct ubi_scan_leb *seb;
158
159 dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
160
161 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
92 if (!seb) 162 if (!seb)
93 return -ENOMEM; 163 return -ENOMEM;
94 164
165 si->corr_peb_count += 1;
95 seb->pnum = pnum; 166 seb->pnum = pnum;
96 seb->ec = ec; 167 seb->ec = ec;
97 list_add_tail(&seb->u.list, list); 168 list_add(&seb->u.list, &si->corr);
98 return 0; 169 return 0;
99} 170}
100 171
@@ -258,8 +329,8 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
258 * created before sequence numbers support has been added. At 329 * created before sequence numbers support has been added. At
259 * that times we used 32-bit LEB versions stored in logical 330 * that times we used 32-bit LEB versions stored in logical
260 * eraseblocks. That was before UBI got into mainline. We do not 331 * eraseblocks. That was before UBI got into mainline. We do not
261 * support these images anymore. Well, those images will work 332 * support these images anymore. Well, those images still work,
262 * still work, but only if no unclean reboots happened. 333 * but only if no unclean reboots happened.
263 */ 334 */
264 ubi_err("unsupported on-flash UBI format\n"); 335 ubi_err("unsupported on-flash UBI format\n");
265 return -EINVAL; 336 return -EINVAL;
@@ -285,19 +356,25 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
285 return 1; 356 return 1;
286 } 357 }
287 } else { 358 } else {
288 pnum = seb->pnum; 359 if (!seb->copy_flag) {
360 /* It is not a copy, so it is newer */
361 dbg_bld("first PEB %d is newer, copy_flag is unset",
362 pnum);
363 return bitflips << 1;
364 }
289 365
290 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 366 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
291 if (!vh) 367 if (!vh)
292 return -ENOMEM; 368 return -ENOMEM;
293 369
370 pnum = seb->pnum;
294 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 371 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0);
295 if (err) { 372 if (err) {
296 if (err == UBI_IO_BITFLIPS) 373 if (err == UBI_IO_BITFLIPS)
297 bitflips = 1; 374 bitflips = 1;
298 else { 375 else {
299 dbg_err("VID of PEB %d header is bad, but it " 376 dbg_err("VID of PEB %d header is bad, but it "
300 "was OK earlier", pnum); 377 "was OK earlier, err %d", pnum, err);
301 if (err > 0) 378 if (err > 0)
302 err = -EIO; 379 err = -EIO;
303 380
@@ -305,14 +382,6 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
305 } 382 }
306 } 383 }
307 384
308 if (!vh->copy_flag) {
309 /* It is not a copy, so it is newer */
310 dbg_bld("first PEB %d is newer, copy_flag is unset",
311 pnum);
312 err = bitflips << 1;
313 goto out_free_vidh;
314 }
315
316 vid_hdr = vh; 385 vid_hdr = vh;
317 } 386 }
318 387
@@ -463,18 +532,15 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
463 if (err) 532 if (err)
464 return err; 533 return err;
465 534
466 if (cmp_res & 4) 535 err = add_to_list(si, seb->pnum, seb->ec, cmp_res & 4,
467 err = add_to_list(si, seb->pnum, seb->ec, 536 &si->erase);
468 &si->corr);
469 else
470 err = add_to_list(si, seb->pnum, seb->ec,
471 &si->erase);
472 if (err) 537 if (err)
473 return err; 538 return err;
474 539
475 seb->ec = ec; 540 seb->ec = ec;
476 seb->pnum = pnum; 541 seb->pnum = pnum;
477 seb->scrub = ((cmp_res & 2) || bitflips); 542 seb->scrub = ((cmp_res & 2) || bitflips);
543 seb->copy_flag = vid_hdr->copy_flag;
478 seb->sqnum = sqnum; 544 seb->sqnum = sqnum;
479 545
480 if (sv->highest_lnum == lnum) 546 if (sv->highest_lnum == lnum)
@@ -487,10 +553,8 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
487 * This logical eraseblock is older than the one found 553 * This logical eraseblock is older than the one found
488 * previously. 554 * previously.
489 */ 555 */
490 if (cmp_res & 4) 556 return add_to_list(si, pnum, ec, cmp_res & 4,
491 return add_to_list(si, pnum, ec, &si->corr); 557 &si->erase);
492 else
493 return add_to_list(si, pnum, ec, &si->erase);
494 } 558 }
495 } 559 }
496 560
@@ -503,15 +567,16 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
503 if (err) 567 if (err)
504 return err; 568 return err;
505 569
506 seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); 570 seb = kmem_cache_alloc(si->scan_leb_slab, GFP_KERNEL);
507 if (!seb) 571 if (!seb)
508 return -ENOMEM; 572 return -ENOMEM;
509 573
510 seb->ec = ec; 574 seb->ec = ec;
511 seb->pnum = pnum; 575 seb->pnum = pnum;
512 seb->lnum = lnum; 576 seb->lnum = lnum;
513 seb->sqnum = sqnum;
514 seb->scrub = bitflips; 577 seb->scrub = bitflips;
578 seb->copy_flag = vid_hdr->copy_flag;
579 seb->sqnum = sqnum;
515 580
516 if (sv->highest_lnum <= lnum) { 581 if (sv->highest_lnum <= lnum) {
517 sv->highest_lnum = lnum; 582 sv->highest_lnum = lnum;
@@ -521,7 +586,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
521 sv->leb_count += 1; 586 sv->leb_count += 1;
522 rb_link_node(&seb->u.rb, parent, p); 587 rb_link_node(&seb->u.rb, parent, p);
523 rb_insert_color(&seb->u.rb, &sv->root); 588 rb_insert_color(&seb->u.rb, &sv->root);
524 si->used_peb_count += 1;
525 return 0; 589 return 0;
526} 590}
527 591
@@ -668,8 +732,8 @@ out_free:
668struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi, 732struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
669 struct ubi_scan_info *si) 733 struct ubi_scan_info *si)
670{ 734{
671 int err = 0, i; 735 int err = 0;
672 struct ubi_scan_leb *seb; 736 struct ubi_scan_leb *seb, *tmp_seb;
673 737
674 if (!list_empty(&si->free)) { 738 if (!list_empty(&si->free)) {
675 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list); 739 seb = list_entry(si->free.next, struct ubi_scan_leb, u.list);
@@ -678,38 +742,88 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
678 return seb; 742 return seb;
679 } 743 }
680 744
681 for (i = 0; i < 2; i++) { 745 /*
682 struct list_head *head; 746 * We try to erase the first physical eraseblock from the erase list
683 struct ubi_scan_leb *tmp_seb; 747 * and pick it if we succeed, or try to erase the next one if not. And
748 * so forth. We don't want to take care about bad eraseblocks here -
749 * they'll be handled later.
750 */
751 list_for_each_entry_safe(seb, tmp_seb, &si->erase, u.list) {
752 if (seb->ec == UBI_SCAN_UNKNOWN_EC)
753 seb->ec = si->mean_ec;
684 754
685 if (i == 0) 755 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1);
686 head = &si->erase; 756 if (err)
687 else 757 continue;
688 head = &si->corr;
689 758
759 seb->ec += 1;
760 list_del(&seb->u.list);
761 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
762 return seb;
763 }
764
765 ubi_err("no free eraseblocks");
766 return ERR_PTR(-ENOSPC);
767}
768
769/**
770 * check_corruption - check the data area of PEB.
771 * @ubi: UBI device description object
772 * @vid_hrd: the (corrupted) VID header of this PEB
773 * @pnum: the physical eraseblock number to check
774 *
775 * This is a helper function which is used to distinguish between VID header
776 * corruptions caused by power cuts and other reasons. If the PEB contains only
777 * 0xFF bytes in the data area, the VID header is most probably corrupted
778 * because of a power cut (%0 is returned in this case). Otherwise, it was
779 * probably corrupted for some other reasons (%1 is returned in this case). A
780 * negative error code is returned if a read error occurred.
781 *
782 * If the corruption reason was a power cut, UBI can safely erase this PEB.
783 * Otherwise, it should preserve it to avoid possibly destroying important
784 * information.
785 */
786static int check_corruption(struct ubi_device *ubi, struct ubi_vid_hdr *vid_hdr,
787 int pnum)
788{
789 int err;
790
791 mutex_lock(&ubi->buf_mutex);
792 memset(ubi->peb_buf1, 0x00, ubi->leb_size);
793
794 err = ubi_io_read(ubi, ubi->peb_buf1, pnum, ubi->leb_start,
795 ubi->leb_size);
796 if (err == UBI_IO_BITFLIPS || err == -EBADMSG) {
690 /* 797 /*
691 * We try to erase the first physical eraseblock from the @head 798 * Bit-flips or integrity errors while reading the data area.
692 * list and pick it if we succeed, or try to erase the 799 * It is difficult to say for sure what type of corruption is
693 * next one if not. And so forth. We don't want to take care 800 * this, but presumably a power cut happened while this PEB was
694 * about bad eraseblocks here - they'll be handled later. 801 * erased, so it became unstable and corrupted, and should be
802 * erased.
695 */ 803 */
696 list_for_each_entry_safe(seb, tmp_seb, head, u.list) { 804 err = 0;
697 if (seb->ec == UBI_SCAN_UNKNOWN_EC) 805 goto out_unlock;
698 seb->ec = si->mean_ec; 806 }
699 807
700 err = ubi_scan_erase_peb(ubi, si, seb->pnum, seb->ec+1); 808 if (err)
701 if (err) 809 goto out_unlock;
702 continue;
703 810
704 seb->ec += 1; 811 if (ubi_check_pattern(ubi->peb_buf1, 0xFF, ubi->leb_size))
705 list_del(&seb->u.list); 812 goto out_unlock;
706 dbg_bld("return PEB %d, EC %d", seb->pnum, seb->ec);
707 return seb;
708 }
709 }
710 813
711 ubi_err("no eraseblocks found"); 814 ubi_err("PEB %d contains corrupted VID header, and the data does not "
712 return ERR_PTR(-ENOSPC); 815 "contain all 0xFF, this may be a non-UBI PEB or a severe VID "
816 "header corruption which requires manual inspection", pnum);
817 ubi_dbg_dump_vid_hdr(vid_hdr);
818 dbg_msg("hexdump of PEB %d offset %d, length %d",
819 pnum, ubi->leb_start, ubi->leb_size);
820 ubi_dbg_print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
821 ubi->peb_buf1, ubi->leb_size, 1);
822 err = 1;
823
824out_unlock:
825 mutex_unlock(&ubi->buf_mutex);
826 return err;
713} 827}
714 828
715/** 829/**
@@ -725,7 +839,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
725 int pnum) 839 int pnum)
726{ 840{
727 long long uninitialized_var(ec); 841 long long uninitialized_var(ec);
728 int err, bitflips = 0, vol_id, ec_corr = 0; 842 int err, bitflips = 0, vol_id, ec_err = 0;
729 843
730 dbg_bld("scan PEB %d", pnum); 844 dbg_bld("scan PEB %d", pnum);
731 845
@@ -746,22 +860,37 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
746 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); 860 err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
747 if (err < 0) 861 if (err < 0)
748 return err; 862 return err;
749 else if (err == UBI_IO_BITFLIPS) 863 switch (err) {
864 case 0:
865 break;
866 case UBI_IO_BITFLIPS:
750 bitflips = 1; 867 bitflips = 1;
751 else if (err == UBI_IO_PEB_EMPTY) 868 break;
752 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); 869 case UBI_IO_FF:
753 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) { 870 si->empty_peb_count += 1;
871 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 0,
872 &si->erase);
873 case UBI_IO_FF_BITFLIPS:
874 si->empty_peb_count += 1;
875 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 1,
876 &si->erase);
877 case UBI_IO_BAD_HDR_EBADMSG:
878 case UBI_IO_BAD_HDR:
754 /* 879 /*
755 * We have to also look at the VID header, possibly it is not 880 * We have to also look at the VID header, possibly it is not
756 * corrupted. Set %bitflips flag in order to make this PEB be 881 * corrupted. Set %bitflips flag in order to make this PEB be
757 * moved and EC be re-created. 882 * moved and EC be re-created.
758 */ 883 */
759 ec_corr = err; 884 ec_err = err;
760 ec = UBI_SCAN_UNKNOWN_EC; 885 ec = UBI_SCAN_UNKNOWN_EC;
761 bitflips = 1; 886 bitflips = 1;
887 break;
888 default:
889 ubi_err("'ubi_io_read_ec_hdr()' returned unknown code %d", err);
890 return -EINVAL;
762 } 891 }
763 892
764 if (!ec_corr) { 893 if (!ec_err) {
765 int image_seq; 894 int image_seq;
766 895
767 /* Make sure UBI version is OK */ 896 /* Make sure UBI version is OK */
@@ -814,24 +943,71 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
814 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); 943 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0);
815 if (err < 0) 944 if (err < 0)
816 return err; 945 return err;
817 else if (err == UBI_IO_BITFLIPS) 946 switch (err) {
947 case 0:
948 break;
949 case UBI_IO_BITFLIPS:
818 bitflips = 1; 950 bitflips = 1;
819 else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR || 951 break;
820 (err == UBI_IO_PEB_FREE && ec_corr)) { 952 case UBI_IO_BAD_HDR_EBADMSG:
821 /* VID header is corrupted */ 953 if (ec_err == UBI_IO_BAD_HDR_EBADMSG)
822 if (err == UBI_IO_BAD_HDR_READ || 954 /*
823 ec_corr == UBI_IO_BAD_HDR_READ) 955 * Both EC and VID headers are corrupted and were read
824 si->read_err_count += 1; 956 * with data integrity error, probably this is a bad
825 err = add_to_list(si, pnum, ec, &si->corr); 957 * PEB, bit it is not marked as bad yet. This may also
958 * be a result of power cut during erasure.
959 */
960 si->maybe_bad_peb_count += 1;
961 case UBI_IO_BAD_HDR:
962 if (ec_err)
963 /*
964 * Both headers are corrupted. There is a possibility
965 * that this a valid UBI PEB which has corresponding
966 * LEB, but the headers are corrupted. However, it is
967 * impossible to distinguish it from a PEB which just
968 * contains garbage because of a power cut during erase
969 * operation. So we just schedule this PEB for erasure.
970 *
971 * Besides, in case of NOR flash, we deliberately
972 * corrupt both headers because NOR flash erasure is
973 * slow and can start from the end.
974 */
975 err = 0;
976 else
977 /*
978 * The EC was OK, but the VID header is corrupted. We
979 * have to check what is in the data area.
980 */
981 err = check_corruption(ubi, vidh, pnum);
982
983 if (err < 0)
984 return err;
985 else if (!err)
986 /* This corruption is caused by a power cut */
987 err = add_to_list(si, pnum, ec, 1, &si->erase);
988 else
989 /* This is an unexpected corruption */
990 err = add_corrupted(si, pnum, ec);
991 if (err)
992 return err;
993 goto adjust_mean_ec;
994 case UBI_IO_FF_BITFLIPS:
995 err = add_to_list(si, pnum, ec, 1, &si->erase);
826 if (err) 996 if (err)
827 return err; 997 return err;
828 goto adjust_mean_ec; 998 goto adjust_mean_ec;
829 } else if (err == UBI_IO_PEB_FREE) { 999 case UBI_IO_FF:
830 /* No VID header - the physical eraseblock is free */ 1000 if (ec_err)
831 err = add_to_list(si, pnum, ec, &si->free); 1001 err = add_to_list(si, pnum, ec, 1, &si->erase);
1002 else
1003 err = add_to_list(si, pnum, ec, 0, &si->free);
832 if (err) 1004 if (err)
833 return err; 1005 return err;
834 goto adjust_mean_ec; 1006 goto adjust_mean_ec;
1007 default:
1008 ubi_err("'ubi_io_read_vid_hdr()' returned unknown code %d",
1009 err);
1010 return -EINVAL;
835 } 1011 }
836 1012
837 vol_id = be32_to_cpu(vidh->vol_id); 1013 vol_id = be32_to_cpu(vidh->vol_id);
@@ -843,7 +1019,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
843 case UBI_COMPAT_DELETE: 1019 case UBI_COMPAT_DELETE:
844 ubi_msg("\"delete\" compatible internal volume %d:%d" 1020 ubi_msg("\"delete\" compatible internal volume %d:%d"
845 " found, will remove it", vol_id, lnum); 1021 " found, will remove it", vol_id, lnum);
846 err = add_to_list(si, pnum, ec, &si->erase); 1022 err = add_to_list(si, pnum, ec, 1, &si->erase);
847 if (err) 1023 if (err)
848 return err; 1024 return err;
849 return 0; 1025 return 0;
@@ -858,7 +1034,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
858 case UBI_COMPAT_PRESERVE: 1034 case UBI_COMPAT_PRESERVE:
859 ubi_msg("\"preserve\" compatible internal volume %d:%d" 1035 ubi_msg("\"preserve\" compatible internal volume %d:%d"
860 " found", vol_id, lnum); 1036 " found", vol_id, lnum);
861 err = add_to_list(si, pnum, ec, &si->alien); 1037 err = add_to_list(si, pnum, ec, 0, &si->alien);
862 if (err) 1038 if (err)
863 return err; 1039 return err;
864 return 0; 1040 return 0;
@@ -870,7 +1046,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
870 } 1046 }
871 } 1047 }
872 1048
873 if (ec_corr) 1049 if (ec_err)
874 ubi_warn("valid VID header but corrupted EC header at PEB %d", 1050 ubi_warn("valid VID header but corrupted EC header at PEB %d",
875 pnum); 1051 pnum);
876 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips); 1052 err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
@@ -878,7 +1054,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
878 return err; 1054 return err;
879 1055
880adjust_mean_ec: 1056adjust_mean_ec:
881 if (!ec_corr) { 1057 if (!ec_err) {
882 si->ec_sum += ec; 1058 si->ec_sum += ec;
883 si->ec_count += 1; 1059 si->ec_count += 1;
884 if (ec > si->max_ec) 1060 if (ec > si->max_ec)
@@ -904,19 +1080,20 @@ adjust_mean_ec:
904static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si) 1080static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
905{ 1081{
906 struct ubi_scan_leb *seb; 1082 struct ubi_scan_leb *seb;
907 int max_corr; 1083 int max_corr, peb_count;
908 1084
909 max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count; 1085 peb_count = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
910 max_corr = max_corr / 20 ?: 8; 1086 max_corr = peb_count / 20 ?: 8;
911 1087
912 /* 1088 /*
913 * Few corrupted PEBs are not a problem and may be just a result of 1089 * Few corrupted PEBs is not a problem and may be just a result of
914 * unclean reboots. However, many of them may indicate some problems 1090 * unclean reboots. However, many of them may indicate some problems
915 * with the flash HW or driver. 1091 * with the flash HW or driver.
916 */ 1092 */
917 if (si->corr_peb_count >= 8) { 1093 if (si->corr_peb_count) {
918 ubi_warn("%d PEBs are corrupted", si->corr_peb_count); 1094 ubi_err("%d PEBs are corrupted and preserved",
919 printk(KERN_WARNING "corrupted PEBs are:"); 1095 si->corr_peb_count);
1096 printk(KERN_ERR "Corrupted PEBs are:");
920 list_for_each_entry(seb, &si->corr, u.list) 1097 list_for_each_entry(seb, &si->corr, u.list)
921 printk(KERN_CONT " %d", seb->pnum); 1098 printk(KERN_CONT " %d", seb->pnum);
922 printk(KERN_CONT "\n"); 1099 printk(KERN_CONT "\n");
@@ -926,46 +1103,40 @@ static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
926 * otherwise, only print a warning. 1103 * otherwise, only print a warning.
927 */ 1104 */
928 if (si->corr_peb_count >= max_corr) { 1105 if (si->corr_peb_count >= max_corr) {
929 ubi_err("too many corrupted PEBs, refusing this device"); 1106 ubi_err("too many corrupted PEBs, refusing");
930 return -EINVAL; 1107 return -EINVAL;
931 } 1108 }
932 } 1109 }
933 1110
934 if (si->free_peb_count + si->used_peb_count + 1111 if (si->empty_peb_count + si->maybe_bad_peb_count == peb_count) {
935 si->alien_peb_count == 0) { 1112 /*
936 /* No UBI-formatted eraseblocks were found */ 1113 * All PEBs are empty, or almost all - a couple PEBs look like
937 if (si->corr_peb_count == si->read_err_count && 1114 * they may be bad PEBs which were not marked as bad yet.
938 si->corr_peb_count < 8) { 1115 *
939 /* No or just few corrupted PEBs, and all of them had a 1116 * This piece of code basically tries to distinguish between
940 * read error. We assume that those are bad PEBs, which 1117 * the following situations:
941 * were just not marked as bad so far. 1118 *
942 * 1119 * 1. Flash is empty, but there are few bad PEBs, which are not
943 * This piece of code basically tries to distinguish 1120 * marked as bad so far, and which were read with error. We
944 * between the following 2 situations: 1121 * want to go ahead and format this flash. While formatting,
945 * 1122 * the faulty PEBs will probably be marked as bad.
946 * 1. Flash is empty, but there are few bad PEBs, which 1123 *
947 * are not marked as bad so far, and which were read 1124 * 2. Flash contains non-UBI data and we do not want to format
948 * with error. We want to go ahead and format this 1125 * it and destroy possibly important information.
949 * flash. While formating, the faulty PEBs will 1126 */
950 * probably be marked as bad. 1127 if (si->maybe_bad_peb_count <= 2) {
951 *
952 * 2. Flash probably contains non-UBI data and we do
953 * not want to format it and destroy possibly needed
954 * data (e.g., consider the case when the bootloader
955 * MTD partition was accidentally fed to UBI).
956 */
957 si->is_empty = 1; 1128 si->is_empty = 1;
958 ubi_msg("empty MTD device detected"); 1129 ubi_msg("empty MTD device detected");
959 get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq)); 1130 get_random_bytes(&ubi->image_seq,
1131 sizeof(ubi->image_seq));
960 } else { 1132 } else {
961 ubi_err("MTD device possibly contains non-UBI data, " 1133 ubi_err("MTD device is not UBI-formatted and possibly "
962 "refusing it"); 1134 "contains non-UBI data - refusing it");
963 return -EINVAL; 1135 return -EINVAL;
964 } 1136 }
1137
965 } 1138 }
966 1139
967 if (si->corr_peb_count > 0)
968 ubi_msg("corrupted PEBs will be formatted");
969 return 0; 1140 return 0;
970} 1141}
971 1142
@@ -995,9 +1166,15 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
995 si->volumes = RB_ROOT; 1166 si->volumes = RB_ROOT;
996 1167
997 err = -ENOMEM; 1168 err = -ENOMEM;
1169 si->scan_leb_slab = kmem_cache_create("ubi_scan_leb_slab",
1170 sizeof(struct ubi_scan_leb),
1171 0, 0, NULL);
1172 if (!si->scan_leb_slab)
1173 goto out_si;
1174
998 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1175 ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
999 if (!ech) 1176 if (!ech)
1000 goto out_si; 1177 goto out_slab;
1001 1178
1002 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1179 vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL);
1003 if (!vidh) 1180 if (!vidh)
@@ -1058,6 +1235,8 @@ out_vidh:
1058 ubi_free_vid_hdr(ubi, vidh); 1235 ubi_free_vid_hdr(ubi, vidh);
1059out_ech: 1236out_ech:
1060 kfree(ech); 1237 kfree(ech);
1238out_slab:
1239 kmem_cache_destroy(si->scan_leb_slab);
1061out_si: 1240out_si:
1062 ubi_scan_destroy_si(si); 1241 ubi_scan_destroy_si(si);
1063 return ERR_PTR(err); 1242 return ERR_PTR(err);
@@ -1066,11 +1245,12 @@ out_si:
1066/** 1245/**
1067 * destroy_sv - free the scanning volume information 1246 * destroy_sv - free the scanning volume information
1068 * @sv: scanning volume information 1247 * @sv: scanning volume information
1248 * @si: scanning information
1069 * 1249 *
1070 * This function destroys the volume RB-tree (@sv->root) and the scanning 1250 * This function destroys the volume RB-tree (@sv->root) and the scanning
1071 * volume information. 1251 * volume information.
1072 */ 1252 */
1073static void destroy_sv(struct ubi_scan_volume *sv) 1253static void destroy_sv(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
1074{ 1254{
1075 struct ubi_scan_leb *seb; 1255 struct ubi_scan_leb *seb;
1076 struct rb_node *this = sv->root.rb_node; 1256 struct rb_node *this = sv->root.rb_node;
@@ -1090,7 +1270,7 @@ static void destroy_sv(struct ubi_scan_volume *sv)
1090 this->rb_right = NULL; 1270 this->rb_right = NULL;
1091 } 1271 }
1092 1272
1093 kfree(seb); 1273 kmem_cache_free(si->scan_leb_slab, seb);
1094 } 1274 }
1095 } 1275 }
1096 kfree(sv); 1276 kfree(sv);
@@ -1108,19 +1288,19 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1108 1288
1109 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) { 1289 list_for_each_entry_safe(seb, seb_tmp, &si->alien, u.list) {
1110 list_del(&seb->u.list); 1290 list_del(&seb->u.list);
1111 kfree(seb); 1291 kmem_cache_free(si->scan_leb_slab, seb);
1112 } 1292 }
1113 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) { 1293 list_for_each_entry_safe(seb, seb_tmp, &si->erase, u.list) {
1114 list_del(&seb->u.list); 1294 list_del(&seb->u.list);
1115 kfree(seb); 1295 kmem_cache_free(si->scan_leb_slab, seb);
1116 } 1296 }
1117 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) { 1297 list_for_each_entry_safe(seb, seb_tmp, &si->corr, u.list) {
1118 list_del(&seb->u.list); 1298 list_del(&seb->u.list);
1119 kfree(seb); 1299 kmem_cache_free(si->scan_leb_slab, seb);
1120 } 1300 }
1121 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) { 1301 list_for_each_entry_safe(seb, seb_tmp, &si->free, u.list) {
1122 list_del(&seb->u.list); 1302 list_del(&seb->u.list);
1123 kfree(seb); 1303 kmem_cache_free(si->scan_leb_slab, seb);
1124 } 1304 }
1125 1305
1126 /* Destroy the volume RB-tree */ 1306 /* Destroy the volume RB-tree */
@@ -1141,14 +1321,15 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1141 rb->rb_right = NULL; 1321 rb->rb_right = NULL;
1142 } 1322 }
1143 1323
1144 destroy_sv(sv); 1324 destroy_sv(si, sv);
1145 } 1325 }
1146 } 1326 }
1147 1327
1328 kmem_cache_destroy(si->scan_leb_slab);
1148 kfree(si); 1329 kfree(si);
1149} 1330}
1150 1331
1151#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1332#ifdef CONFIG_MTD_UBI_DEBUG
1152 1333
1153/** 1334/**
1154 * paranoid_check_si - check the scanning information. 1335 * paranoid_check_si - check the scanning information.
@@ -1166,6 +1347,9 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1166 struct ubi_scan_leb *seb, *last_seb; 1347 struct ubi_scan_leb *seb, *last_seb;
1167 uint8_t *buf; 1348 uint8_t *buf;
1168 1349
1350 if (!(ubi_chk_flags & UBI_CHK_GEN))
1351 return 0;
1352
1169 /* 1353 /*
1170 * At first, check that scanning information is OK. 1354 * At first, check that scanning information is OK.
1171 */ 1355 */
@@ -1418,4 +1602,4 @@ out:
1418 return -EINVAL; 1602 return -EINVAL;
1419} 1603}
1420 1604
1421#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1605#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
index 2576a8d1532b..d48aef15ab5d 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
@@ -108,6 +109,7 @@ struct ubi_scan_volume {
108 * @mean_ec: mean erase counter value 109 * @mean_ec: mean erase counter value
109 * @ec_sum: a temporary variable used when calculating @mean_ec 110 * @ec_sum: a temporary variable used when calculating @mean_ec
110 * @ec_count: a temporary variable used when calculating @mean_ec 111 * @ec_count: a temporary variable used when calculating @mean_ec
112 * @scan_leb_slab: slab cache for &struct ubi_scan_leb objects
111 * 113 *
112 * This data structure contains the result of scanning and may be used by other 114 * This data structure contains the result of scanning and may be used by other
113 * UBI sub-systems to build final UBI data structures, further error-recovery 115 * UBI sub-systems to build final UBI data structures, further error-recovery
@@ -119,13 +121,11 @@ struct ubi_scan_info {
119 struct list_head free; 121 struct list_head free;
120 struct list_head erase; 122 struct list_head erase;
121 struct list_head alien; 123 struct list_head alien;
122 int used_peb_count;
123 int corr_peb_count; 124 int corr_peb_count;
124 int read_err_count; 125 int empty_peb_count;
125 int free_peb_count;
126 int erase_peb_count;
127 int alien_peb_count; 126 int alien_peb_count;
128 int bad_peb_count; 127 int bad_peb_count;
128 int maybe_bad_peb_count;
129 int vols_found; 129 int vols_found;
130 int highest_vol_id; 130 int highest_vol_id;
131 int is_empty; 131 int is_empty;
@@ -135,6 +135,7 @@ struct ubi_scan_info {
135 int mean_ec; 135 int mean_ec;
136 uint64_t ec_sum; 136 uint64_t ec_sum;
137 int ec_count; 137 int ec_count;
138 struct kmem_cache *scan_leb_slab;
138}; 139};
139 140
140struct ubi_device; 141struct ubi_device;
diff --git a/drivers/mtd/ubi/ubi-media.h b/drivers/mtd/ubi/ubi-media.h
index 503ea9b27309..6fb8ec2174a5 100644
--- a/drivers/mtd/ubi/ubi-media.h
+++ b/drivers/mtd/ubi/ubi-media.h
@@ -164,7 +164,7 @@ struct ubi_ec_hdr {
164 __be32 image_seq; 164 __be32 image_seq;
165 __u8 padding2[32]; 165 __u8 padding2[32];
166 __be32 hdr_crc; 166 __be32 hdr_crc;
167} __attribute__ ((packed)); 167} __packed;
168 168
169/** 169/**
170 * struct ubi_vid_hdr - on-flash UBI volume identifier header. 170 * struct ubi_vid_hdr - on-flash UBI volume identifier header.
@@ -292,7 +292,7 @@ struct ubi_vid_hdr {
292 __be64 sqnum; 292 __be64 sqnum;
293 __u8 padding3[12]; 293 __u8 padding3[12];
294 __be32 hdr_crc; 294 __be32 hdr_crc;
295} __attribute__ ((packed)); 295} __packed;
296 296
297/* Internal UBI volumes count */ 297/* Internal UBI volumes count */
298#define UBI_INT_VOL_COUNT 1 298#define UBI_INT_VOL_COUNT 1
@@ -373,6 +373,6 @@ struct ubi_vtbl_record {
373 __u8 flags; 373 __u8 flags;
374 __u8 padding[23]; 374 __u8 padding[23];
375 __be32 crc; 375 __be32 crc;
376} __attribute__ ((packed)); 376} __packed;
377 377
378#endif /* !__UBI_MEDIA_H__ */ 378#endif /* !__UBI_MEDIA_H__ */
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index 0359e0cce482..c6c22295898e 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -40,6 +40,7 @@
40#include <linux/notifier.h> 40#include <linux/notifier.h>
41#include <linux/mtd/mtd.h> 41#include <linux/mtd/mtd.h>
42#include <linux/mtd/ubi.h> 42#include <linux/mtd/ubi.h>
43#include <asm/pgtable.h>
43 44
44#include "ubi-media.h" 45#include "ubi-media.h"
45#include "scan.h" 46#include "scan.h"
@@ -85,21 +86,26 @@
85/* 86/*
86 * Error codes returned by the I/O sub-system. 87 * Error codes returned by the I/O sub-system.
87 * 88 *
88 * UBI_IO_PEB_EMPTY: the physical eraseblock is empty, i.e. it contains only 89 * UBI_IO_FF: the read region of flash contains only 0xFFs
89 * %0xFF bytes 90 * 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 91 * integrity error reported by the MTD driver
91 * valid erase counter header, and the rest are %0xFF bytes 92 * (uncorrectable ECC error in case of NAND)
92 * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC) 93 * 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 94 * UBI_IO_BAD_HDR_EBADMSG: the same as %UBI_IO_BAD_HDR, but also there was a
94 * error reported by the flash driver 95 * data integrity error reported by the MTD driver
96 * (uncorrectable ECC error in case of NAND)
95 * UBI_IO_BITFLIPS: bit-flips were detected and corrected 97 * UBI_IO_BITFLIPS: bit-flips were detected and corrected
98 *
99 * Note, it is probably better to have bit-flip and ebadmsg as flags which can
100 * be or'ed with other error code. But this is a big change because there are
101 * may callers, so it does not worth the risk of introducing a bug
96 */ 102 */
97enum { 103enum {
98 UBI_IO_PEB_EMPTY = 1, 104 UBI_IO_FF = 1,
99 UBI_IO_PEB_FREE, 105 UBI_IO_FF_BITFLIPS,
100 UBI_IO_BAD_HDR, 106 UBI_IO_BAD_HDR,
101 UBI_IO_BAD_HDR_READ, 107 UBI_IO_BAD_HDR_EBADMSG,
102 UBI_IO_BITFLIPS 108 UBI_IO_BITFLIPS,
103}; 109};
104 110
105/* 111/*
@@ -335,8 +341,8 @@ struct ubi_wl_entry;
335 * protected from the wear-leveling worker) 341 * protected from the wear-leveling worker)
336 * @pq_head: protection queue head 342 * @pq_head: protection queue head
337 * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from, 343 * @wl_lock: protects the @used, @free, @pq, @pq_head, @lookuptbl, @move_from,
338 * @move_to, @move_to_put @erase_pending, @wl_scheduled, @works, 344 * @move_to, @move_to_put @erase_pending, @wl_scheduled, @works,
339 * @erroneous, and @erroneous_peb_count fields 345 * @erroneous, and @erroneous_peb_count fields
340 * @move_mutex: serializes eraseblock moves 346 * @move_mutex: serializes eraseblock moves
341 * @work_sem: synchronizes the WL worker with use tasks 347 * @work_sem: synchronizes the WL worker with use tasks
342 * @wl_scheduled: non-zero if the wear-leveling was scheduled 348 * @wl_scheduled: non-zero if the wear-leveling was scheduled
@@ -356,6 +362,8 @@ struct ubi_wl_entry;
356 * @peb_size: physical eraseblock size 362 * @peb_size: physical eraseblock size
357 * @bad_peb_count: count of bad physical eraseblocks 363 * @bad_peb_count: count of bad physical eraseblocks
358 * @good_peb_count: count of good physical eraseblocks 364 * @good_peb_count: count of good physical eraseblocks
365 * @corr_peb_count: count of corrupted physical eraseblocks (preserved and not
366 * used by UBI)
359 * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous 367 * @erroneous_peb_count: count of erroneous physical eraseblocks in @erroneous
360 * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks 368 * @max_erroneous: maximum allowed amount of erroneous physical eraseblocks
361 * @min_io_size: minimal input/output unit size of the underlying MTD device 369 * @min_io_size: minimal input/output unit size of the underlying MTD device
@@ -374,14 +382,14 @@ struct ubi_wl_entry;
374 * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or 382 * @bad_allowed: whether the MTD device admits of bad physical eraseblocks or
375 * not 383 * not
376 * @nor_flash: non-zero if working on top of NOR flash 384 * @nor_flash: non-zero if working on top of NOR flash
385 * @max_write_size: maximum amount of bytes the underlying flash can write at a
386 * time (MTD write buffer size)
377 * @mtd: MTD device descriptor 387 * @mtd: MTD device descriptor
378 * 388 *
379 * @peb_buf1: a buffer of PEB size used for different purposes 389 * @peb_buf1: a buffer of PEB size used for different purposes
380 * @peb_buf2: another buffer of PEB size used for different purposes 390 * @peb_buf2: another buffer of PEB size used for different purposes
381 * @buf_mutex: protects @peb_buf1 and @peb_buf2 391 * @buf_mutex: protects @peb_buf1 and @peb_buf2
382 * @ckvol_mutex: serializes static volume checking when opening 392 * @ckvol_mutex: serializes static volume checking when opening
383 * @dbg_peb_buf: buffer of PEB size used for debugging
384 * @dbg_buf_mutex: protects @dbg_peb_buf
385 */ 393 */
386struct ubi_device { 394struct ubi_device {
387 struct cdev cdev; 395 struct cdev cdev;
@@ -442,6 +450,7 @@ struct ubi_device {
442 int peb_size; 450 int peb_size;
443 int bad_peb_count; 451 int bad_peb_count;
444 int good_peb_count; 452 int good_peb_count;
453 int corr_peb_count;
445 int erroneous_peb_count; 454 int erroneous_peb_count;
446 int max_erroneous; 455 int max_erroneous;
447 int min_io_size; 456 int min_io_size;
@@ -456,16 +465,13 @@ struct ubi_device {
456 int vid_hdr_shift; 465 int vid_hdr_shift;
457 unsigned int bad_allowed:1; 466 unsigned int bad_allowed:1;
458 unsigned int nor_flash:1; 467 unsigned int nor_flash:1;
468 int max_write_size;
459 struct mtd_info *mtd; 469 struct mtd_info *mtd;
460 470
461 void *peb_buf1; 471 void *peb_buf1;
462 void *peb_buf2; 472 void *peb_buf2;
463 struct mutex buf_mutex; 473 struct mutex buf_mutex;
464 struct mutex ckvol_mutex; 474 struct mutex ckvol_mutex;
465#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
466 void *dbg_peb_buf;
467 struct mutex dbg_buf_mutex;
468#endif
469}; 475};
470 476
471extern struct kmem_cache *ubi_wl_entry_slab; 477extern struct kmem_cache *ubi_wl_entry_slab;
@@ -506,6 +512,7 @@ int ubi_calc_data_len(const struct ubi_device *ubi, const void *buf,
506 int length); 512 int length);
507int ubi_check_volume(struct ubi_device *ubi, int vol_id); 513int ubi_check_volume(struct ubi_device *ubi, int vol_id);
508void ubi_calculate_reserved(struct ubi_device *ubi); 514void ubi_calculate_reserved(struct ubi_device *ubi);
515int ubi_check_pattern(const void *buf, uint8_t patt, int size);
509 516
510/* eba.c */ 517/* eba.c */
511int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, 518int 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..366eb70219a6 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -28,7 +28,7 @@
28#include <linux/slab.h> 28#include <linux/slab.h>
29#include "ubi.h" 29#include "ubi.h"
30 30
31#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 31#ifdef CONFIG_MTD_UBI_DEBUG
32static int paranoid_check_volumes(struct ubi_device *ubi); 32static int paranoid_check_volumes(struct ubi_device *ubi);
33#else 33#else
34#define paranoid_check_volumes(ubi) 0 34#define paranoid_check_volumes(ubi) 0
@@ -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;
@@ -705,7 +711,7 @@ void ubi_free_volume(struct ubi_device *ubi, struct ubi_volume *vol)
705 volume_sysfs_close(vol); 711 volume_sysfs_close(vol);
706} 712}
707 713
708#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 714#ifdef CONFIG_MTD_UBI_DEBUG
709 715
710/** 716/**
711 * paranoid_check_volume - check volume information. 717 * paranoid_check_volume - check volume information.
@@ -784,11 +790,6 @@ static int paranoid_check_volume(struct ubi_device *ubi, int vol_id)
784 goto fail; 790 goto fail;
785 } 791 }
786 792
787 if (!vol->name) {
788 ubi_err("NULL volume name");
789 goto fail;
790 }
791
792 n = strnlen(vol->name, vol->name_len + 1); 793 n = strnlen(vol->name, vol->name_len + 1);
793 if (n != vol->name_len) { 794 if (n != vol->name_len) {
794 ubi_err("bad name_len %lld", n); 795 ubi_err("bad name_len %lld", n);
@@ -870,6 +871,9 @@ static int paranoid_check_volumes(struct ubi_device *ubi)
870{ 871{
871 int i, err = 0; 872 int i, err = 0;
872 873
874 if (!(ubi_chk_flags & UBI_CHK_GEN))
875 return 0;
876
873 for (i = 0; i < ubi->vtbl_slots; i++) { 877 for (i = 0; i < ubi->vtbl_slots; i++) {
874 err = paranoid_check_volume(ubi, i); 878 err = paranoid_check_volume(ubi, i);
875 if (err) 879 if (err)
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
index 14c10bed94ee..fd3bf770f518 100644
--- a/drivers/mtd/ubi/vtbl.c
+++ b/drivers/mtd/ubi/vtbl.c
@@ -62,7 +62,7 @@
62#include <asm/div64.h> 62#include <asm/div64.h>
63#include "ubi.h" 63#include "ubi.h"
64 64
65#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 65#ifdef CONFIG_MTD_UBI_DEBUG
66static void paranoid_vtbl_check(const struct ubi_device *ubi); 66static void paranoid_vtbl_check(const struct ubi_device *ubi);
67#else 67#else
68#define paranoid_vtbl_check(ubi) 68#define paranoid_vtbl_check(ubi)
@@ -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);
@@ -425,12 +425,11 @@ static struct ubi_vtbl_record *process_lvol(struct ubi_device *ubi,
425 425
426 /* Read both LEB 0 and LEB 1 into memory */ 426 /* Read both LEB 0 and LEB 1 into memory */
427 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { 427 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
428 leb[seb->lnum] = vmalloc(ubi->vtbl_size); 428 leb[seb->lnum] = vzalloc(ubi->vtbl_size);
429 if (!leb[seb->lnum]) { 429 if (!leb[seb->lnum]) {
430 err = -ENOMEM; 430 err = -ENOMEM;
431 goto out_free; 431 goto out_free;
432 } 432 }
433 memset(leb[seb->lnum], 0, ubi->vtbl_size);
434 433
435 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, 434 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
436 ubi->vtbl_size); 435 ubi->vtbl_size);
@@ -516,10 +515,9 @@ static struct ubi_vtbl_record *create_empty_lvol(struct ubi_device *ubi,
516 int i; 515 int i;
517 struct ubi_vtbl_record *vtbl; 516 struct ubi_vtbl_record *vtbl;
518 517
519 vtbl = vmalloc(ubi->vtbl_size); 518 vtbl = vzalloc(ubi->vtbl_size);
520 if (!vtbl) 519 if (!vtbl)
521 return ERR_PTR(-ENOMEM); 520 return ERR_PTR(-ENOMEM);
522 memset(vtbl, 0, ubi->vtbl_size);
523 521
524 for (i = 0; i < ubi->vtbl_slots; i++) 522 for (i = 0; i < ubi->vtbl_slots; i++)
525 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); 523 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
@@ -662,9 +660,13 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
662 ubi->vol_count += 1; 660 ubi->vol_count += 1;
663 vol->ubi = ubi; 661 vol->ubi = ubi;
664 662
665 if (reserved_pebs > ubi->avail_pebs) 663 if (reserved_pebs > ubi->avail_pebs) {
666 ubi_err("not enough PEBs, required %d, available %d", 664 ubi_err("not enough PEBs, required %d, available %d",
667 reserved_pebs, ubi->avail_pebs); 665 reserved_pebs, ubi->avail_pebs);
666 if (ubi->corr_peb_count)
667 ubi_err("%d PEBs are corrupted and not used",
668 ubi->corr_peb_count);
669 }
668 ubi->rsvd_pebs += reserved_pebs; 670 ubi->rsvd_pebs += reserved_pebs;
669 ubi->avail_pebs -= reserved_pebs; 671 ubi->avail_pebs -= reserved_pebs;
670 672
@@ -837,7 +839,7 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
837 return PTR_ERR(ubi->vtbl); 839 return PTR_ERR(ubi->vtbl);
838 } 840 }
839 841
840 ubi->avail_pebs = ubi->good_peb_count; 842 ubi->avail_pebs = ubi->good_peb_count - ubi->corr_peb_count;
841 843
842 /* 844 /*
843 * The layout volume is OK, initialize the corresponding in-RAM data 845 * The layout volume is OK, initialize the corresponding in-RAM data
@@ -866,7 +868,7 @@ out_free:
866 return err; 868 return err;
867} 869}
868 870
869#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 871#ifdef CONFIG_MTD_UBI_DEBUG
870 872
871/** 873/**
872 * paranoid_vtbl_check - check volume table. 874 * paranoid_vtbl_check - check volume table.
@@ -874,10 +876,13 @@ out_free:
874 */ 876 */
875static void paranoid_vtbl_check(const struct ubi_device *ubi) 877static void paranoid_vtbl_check(const struct ubi_device *ubi)
876{ 878{
879 if (!(ubi_chk_flags & UBI_CHK_GEN))
880 return;
881
877 if (vtbl_check(ubi, ubi->vtbl)) { 882 if (vtbl_check(ubi, ubi->vtbl)) {
878 ubi_err("paranoid check failed"); 883 ubi_err("paranoid check failed");
879 BUG(); 884 BUG();
880 } 885 }
881} 886}
882 887
883#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 888#endif /* CONFIG_MTD_UBI_DEBUG */
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 97a435672eaf..ff2c4956eeff 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -161,7 +161,7 @@ struct ubi_work {
161 int torture; 161 int torture;
162}; 162};
163 163
164#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 164#ifdef CONFIG_MTD_UBI_DEBUG
165static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec); 165static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
166static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e, 166static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
167 struct rb_root *root); 167 struct rb_root *root);
@@ -613,7 +613,7 @@ static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
613 list_add_tail(&wrk->list, &ubi->works); 613 list_add_tail(&wrk->list, &ubi->works);
614 ubi_assert(ubi->works_count >= 0); 614 ubi_assert(ubi->works_count >= 0);
615 ubi->works_count += 1; 615 ubi->works_count += 1;
616 if (ubi->thread_enabled) 616 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled())
617 wake_up_process(ubi->bgt_thread); 617 wake_up_process(ubi->bgt_thread);
618 spin_unlock(&ubi->wl_lock); 618 spin_unlock(&ubi->wl_lock);
619} 619}
@@ -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",
@@ -1354,7 +1364,7 @@ int ubi_thread(void *u)
1354 1364
1355 spin_lock(&ubi->wl_lock); 1365 spin_lock(&ubi->wl_lock);
1356 if (list_empty(&ubi->works) || ubi->ro_mode || 1366 if (list_empty(&ubi->works) || ubi->ro_mode ||
1357 !ubi->thread_enabled) { 1367 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled()) {
1358 set_current_state(TASK_INTERRUPTIBLE); 1368 set_current_state(TASK_INTERRUPTIBLE);
1359 spin_unlock(&ubi->wl_lock); 1369 spin_unlock(&ubi->wl_lock);
1360 schedule(); 1370 schedule();
@@ -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;
@@ -1564,7 +1561,7 @@ void ubi_wl_close(struct ubi_device *ubi)
1564 kfree(ubi->lookuptbl); 1561 kfree(ubi->lookuptbl);
1565} 1562}
1566 1563
1567#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1564#ifdef CONFIG_MTD_UBI_DEBUG
1568 1565
1569/** 1566/**
1570 * paranoid_check_ec - make sure that the erase counter of a PEB is correct. 1567 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
@@ -1573,7 +1570,8 @@ void ubi_wl_close(struct ubi_device *ubi)
1573 * @ec: the erase counter to check 1570 * @ec: the erase counter to check
1574 * 1571 *
1575 * This function returns zero if the erase counter of physical eraseblock @pnum 1572 * This function returns zero if the erase counter of physical eraseblock @pnum
1576 * is equivalent to @ec, and a negative error code if not or if an error occurred. 1573 * is equivalent to @ec, and a negative error code if not or if an error
1574 * occurred.
1577 */ 1575 */
1578static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec) 1576static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1579{ 1577{
@@ -1581,6 +1579,9 @@ static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1581 long long read_ec; 1579 long long read_ec;
1582 struct ubi_ec_hdr *ec_hdr; 1580 struct ubi_ec_hdr *ec_hdr;
1583 1581
1582 if (!(ubi_chk_flags & UBI_CHK_GEN))
1583 return 0;
1584
1584 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 1585 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1585 if (!ec_hdr) 1586 if (!ec_hdr)
1586 return -ENOMEM; 1587 return -ENOMEM;
@@ -1617,6 +1618,9 @@ out_free:
1617static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e, 1618static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1618 struct rb_root *root) 1619 struct rb_root *root)
1619{ 1620{
1621 if (!(ubi_chk_flags & UBI_CHK_GEN))
1622 return 0;
1623
1620 if (in_wl_tree(e, root)) 1624 if (in_wl_tree(e, root))
1621 return 0; 1625 return 0;
1622 1626
@@ -1639,6 +1643,9 @@ static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
1639 struct ubi_wl_entry *p; 1643 struct ubi_wl_entry *p;
1640 int i; 1644 int i;
1641 1645
1646 if (!(ubi_chk_flags & UBI_CHK_GEN))
1647 return 0;
1648
1642 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) 1649 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1643 list_for_each_entry(p, &ubi->pq[i], u.list) 1650 list_for_each_entry(p, &ubi->pq[i], u.list)
1644 if (p == e) 1651 if (p == e)
@@ -1649,4 +1656,5 @@ static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
1649 ubi_dbg_dump_stack(); 1656 ubi_dbg_dump_stack();
1650 return -EINVAL; 1657 return -EINVAL;
1651} 1658}
1652#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1659
1660#endif /* CONFIG_MTD_UBI_DEBUG */