Merge branch 'linux-next' of git://git.infradead.org/ubi-2.6

* 'linux-next' of git://git.infradead.org/ubi-2.6:
UBI: do not warn unnecessarily
UBI: do not print message about corruptes PEBs if we have none of them
UBI: improve delete-compatible volumes handling
UBI: fix error message and compilation warnings
UBI: generate random image_seq when formatting MTD devices
UBI: improve ECC error message
UBI: improve corrupted flash handling
UBI: introduce eraseblock counter variables
UBI: introduce a new IO return code
UBI: simplify IO error codes

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