mtd/maps: gpio-addr-flash: pull in linux/ headers rather than asm/
[safe/jmp/linux-2.6] / drivers / mtd / ubi / scan.c
index 0bb7488..e7161ad 100644 (file)
@@ -42,7 +42,7 @@
 
 #include <linux/err.h>
 #include <linux/crc32.h>
-#include <asm/div64.h>
+#include <linux/math64.h>
 #include "ubi.h"
 
 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
@@ -75,9 +75,10 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
                dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
        else if (list == &si->erase)
                dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
-       else if (list == &si->corr)
+       else if (list == &si->corr) {
                dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
-       else if (list == &si->alien)
+               si->corr_count += 1;
+       } else if (list == &si->alien)
                dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
        else
                BUG();
@@ -93,8 +94,7 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
 }
 
 /**
- * validate_vid_hdr - check that volume identifier header is correct and
- * consistent.
+ * validate_vid_hdr - check volume identifier header.
  * @vid_hdr: the volume identifier header to check
  * @sv: information about the volume this logical eraseblock belongs to
  * @pnum: physical eraseblock number the VID header came from
@@ -247,46 +247,21 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
        struct ubi_vid_hdr *vh = NULL;
        unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
 
-       if (seb->sqnum == 0 && sqnum2 == 0) {
-               long long abs;
-               long long v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
-
+       if (sqnum2 == seb->sqnum) {
                /*
-                * UBI constantly increases the logical eraseblock version
-                * number and it can overflow. Thus, we have to bear in mind
-                * that versions that are close to %0xFFFFFFFF are less then
-                * versions that are close to %0.
-                *
-                * The UBI WL sub-system guarantees that the number of pending
-                * tasks is not greater then %0x7FFFFFFF. So, if the difference
-                * between any two versions is greater or equivalent to
-                * %0x7FFFFFFF, there was an overflow and the logical
-                * eraseblock with lower version is actually newer then the one
-                * with higher version.
-                *
-                * FIXME: but this is anyway obsolete and will be removed at
-                * some point.
+                * This must be a really ancient UBI image which has been
+                * created before sequence numbers support has been added. At
+                * that times we used 32-bit LEB versions stored in logical
+                * eraseblocks. That was before UBI got into mainline. We do not
+                * support these images anymore. Well, those images will work
+                * still work, but only if no unclean reboots happened.
                 */
-               dbg_bld("using old crappy leb_ver stuff");
-
-               if (v1 == v2) {
-                       ubi_err("PEB %d and PEB %d have the same version %lld",
-                               seb->pnum, pnum, v1);
-                       return -EINVAL;
-               }
+               ubi_err("unsupported on-flash UBI format\n");
+               return -EINVAL;
+       }
 
-               abs = v1 - v2;
-               if (abs < 0)
-                       abs = -abs;
-
-               if (abs < 0x7FFFFFFF)
-                       /* Non-overflow situation */
-                       second_is_newer = (v2 > v1);
-               else
-                       second_is_newer = (v2 < v1);
-       } else
-               /* Obviously the LEB with lower sequence counter is older */
-               second_is_newer = sqnum2 > seb->sqnum;
+       /* Obviously the LEB with lower sequence counter is older */
+       second_is_newer = !!(sqnum2 > seb->sqnum);
 
        /*
         * Now we know which copy is newer. If the copy flag of the PEB with
@@ -294,7 +269,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
         * check data CRC. For the second PEB we already have the VID header,
         * for the first one - we'll need to re-read it from flash.
         *
-        * FIXME: this may be optimized so that we wouldn't read twice.
+        * Note: this may be optimized so that we wouldn't read twice.
         */
 
        if (second_is_newer) {
@@ -346,7 +321,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
        }
 
        err = ubi_io_read_data(ubi, buf, pnum, 0, len);
-       if (err && err != UBI_IO_BITFLIPS)
+       if (err && err != UBI_IO_BITFLIPS && err != -EBADMSG)
                goto out_free_buf;
 
        data_crc = be32_to_cpu(vid_hdr->data_crc);
@@ -380,8 +355,7 @@ out_free_vidh:
 }
 
 /**
- * ubi_scan_add_used - add information about a physical eraseblock to the
- * scanning information.
+ * ubi_scan_add_used - add physical eraseblock to the scanning information.
  * @ubi: UBI device description object
  * @si: scanning information
  * @pnum: the physical eraseblock number
@@ -401,7 +375,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
                      int bitflips)
 {
        int err, vol_id, lnum;
-       uint32_t leb_ver;
        unsigned long long sqnum;
        struct ubi_scan_volume *sv;
        struct ubi_scan_leb *seb;
@@ -410,13 +383,12 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
        vol_id = be32_to_cpu(vid_hdr->vol_id);
        lnum = be32_to_cpu(vid_hdr->lnum);
        sqnum = be64_to_cpu(vid_hdr->sqnum);
-       leb_ver = be32_to_cpu(vid_hdr->leb_ver);
 
-       dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
-               pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
+       dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
+               pnum, vol_id, lnum, ec, sqnum, bitflips);
 
        sv = add_volume(si, vol_id, pnum, vid_hdr);
-       if (IS_ERR(sv) < 0)
+       if (IS_ERR(sv))
                return PTR_ERR(sv);
 
        if (si->max_sqnum < sqnum)
@@ -446,25 +418,20 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
                 */
 
                dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
-                       "LEB ver %u, EC %d", seb->pnum, seb->sqnum,
-                       seb->leb_ver, seb->ec);
-
-               /*
-                * Make sure that the logical eraseblocks have different
-                * versions. Otherwise the image is bad.
-                */
-               if (seb->leb_ver == leb_ver && leb_ver != 0) {
-                       ubi_err("two LEBs with same version %u", leb_ver);
-                       ubi_dbg_dump_seb(seb, 0);
-                       ubi_dbg_dump_vid_hdr(vid_hdr);
-                       return -EINVAL;
-               }
+                       "EC %d", seb->pnum, seb->sqnum, seb->ec);
 
                /*
                 * Make sure that the logical eraseblocks have different
                 * sequence numbers. Otherwise the image is bad.
                 *
-                * FIXME: remove 'sqnum != 0' check when leb_ver is removed.
+                * However, if the sequence number is zero, we assume it must
+                * be an ancient UBI image from the era when UBI did not have
+                * sequence numbers. We still can attach these images, unless
+                * there is a need to distinguish between old and new
+                * eraseblocks, in which case we'll refuse the image in
+                * 'compare_lebs()'. In other words, we attach old clean
+                * images, but refuse attaching old images with duplicated
+                * logical eraseblocks because there was an unclean reboot.
                 */
                if (seb->sqnum == sqnum && sqnum != 0) {
                        ubi_err("two LEBs with same sequence number %llu",
@@ -504,7 +471,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
                        seb->pnum = pnum;
                        seb->scrub = ((cmp_res & 2) || bitflips);
                        seb->sqnum = sqnum;
-                       seb->leb_ver = leb_ver;
 
                        if (sv->highest_lnum == lnum)
                                sv->last_data_size =
@@ -513,7 +479,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
                        return 0;
                } else {
                        /*
-                        * This logical eraseblock is older then the one found
+                        * This logical eraseblock is older than the one found
                         * previously.
                         */
                        if (cmp_res & 4)
@@ -541,7 +507,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
        seb->lnum = lnum;
        seb->sqnum = sqnum;
        seb->scrub = bitflips;
-       seb->leb_ver = leb_ver;
 
        if (sv->highest_lnum <= lnum) {
                sv->highest_lnum = lnum;
@@ -555,8 +520,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
 }
 
 /**
- * ubi_scan_find_sv - find information about a particular volume in the
- * scanning information.
+ * ubi_scan_find_sv - find volume in the scanning information.
  * @si: scanning information
  * @vol_id: the requested volume ID
  *
@@ -585,8 +549,7 @@ struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
 }
 
 /**
- * ubi_scan_find_seb - find information about a particular logical
- * eraseblock in the volume scanning information.
+ * ubi_scan_find_seb - find LEB in the volume scanning information.
  * @sv: a pointer to the volume scanning information
  * @lnum: the requested logical eraseblock
  *
@@ -744,8 +707,7 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
 }
 
 /**
- * process_eb - read UBI headers, check them and add corresponding data
- * to the scanning information.
+ * process_eb - read, check UBI headers, and add them to scanning information.
  * @ubi: UBI device description object
  * @si: scanning information
  * @pnum: the physical eraseblock number
@@ -796,6 +758,8 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
        si->is_empty = 0;
 
        if (!ec_corr) {
+               int image_seq;
+
                /* Make sure UBI version is OK */
                if (ech->version != UBI_VERSION) {
                        ubi_err("this UBI version is %d, image version is %d",
@@ -817,6 +781,29 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
                        ubi_dbg_dump_ec_hdr(ech);
                        return -EINVAL;
                }
+
+               /*
+                * Make sure that all PEBs have the same image sequence number.
+                * This allows us to detect situations when users flash UBI
+                * images incorrectly, so that the flash has the new UBI image
+                * and leftovers from the old one. This feature was added
+                * relatively recently, and the sequence number was always
+                * zero, because old UBI implementations always set it to zero.
+                * For this reasons, we do not panic if some PEBs have zero
+                * sequence number, while other PEBs have non-zero sequence
+                * number.
+                */
+               image_seq = be32_to_cpu(ech->image_seq);
+               if (!si->image_seq_set) {
+                       ubi->image_seq = image_seq;
+                       si->image_seq_set = 1;
+               } else if (ubi->image_seq && ubi->image_seq != image_seq) {
+                       ubi_err("bad image sequence number %d in PEB %d, "
+                               "expected %d", image_seq, pnum, ubi->image_seq);
+                       ubi_dbg_dump_ec_hdr(ech);
+                       return -EINVAL;
+               }
+
        }
 
        /* OK, we've done with the EC header, let's look at the VID header */
@@ -878,7 +865,9 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
                }
        }
 
-       /* Both UBI headers seem to be fine */
+       if (ec_corr)
+               ubi_warn("valid VID header but corrupted EC header at PEB %d",
+                        pnum);
        err = ubi_scan_add_used(ubi, si, pnum, ec, vidh, bitflips);
        if (err)
                return err;
@@ -943,15 +932,26 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
        dbg_msg("scanning is finished");
 
        /* Calculate mean erase counter */
-       if (si->ec_count) {
-               do_div(si->ec_sum, si->ec_count);
-               si->mean_ec = si->ec_sum;
-       }
+       if (si->ec_count)
+               si->mean_ec = div_u64(si->ec_sum, si->ec_count);
 
        if (si->is_empty)
                ubi_msg("empty MTD device detected");
 
        /*
+        * Few corrupted PEBs are not a problem and may be just a result of
+        * unclean reboots. However, many of them may indicate some problems
+        * with the flash HW or driver. Print a warning in this case.
+        */
+       if (si->corr_count >= 8 || si->corr_count >= ubi->peb_count / 4) {
+               ubi_warn("%d PEBs are corrupted", si->corr_count);
+               printk(KERN_WARNING "corrupted PEBs are:");
+               list_for_each_entry(seb, &si->corr, u.list)
+                       printk(KERN_CONT " %d", seb->pnum);
+               printk(KERN_CONT "\n");
+       }
+
+       /*
         * In case of unknown erase counter we use the mean erase counter
         * value.
         */
@@ -1083,8 +1083,7 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
 
 /**
- * paranoid_check_si - check if the scanning information is correct and
- * consistent.
+ * paranoid_check_si - check the scanning information.
  * @ubi: UBI device description object
  * @si: scanning information
  *
@@ -1269,11 +1268,6 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
                                ubi_err("bad data_pad %d", sv->data_pad);
                                goto bad_vid_hdr;
                        }
-
-                       if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
-                               ubi_err("bad leb_ver %u", seb->leb_ver);
-                               goto bad_vid_hdr;
-                       }
                }
 
                if (!last_seb)