Commit 7046e668 authored by Linus Torvalds's avatar Linus Torvalds

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
parents c939f9f9 64d4b4c9
...@@ -593,6 +593,7 @@ static int attach_by_scanning(struct ubi_device *ubi) ...@@ -593,6 +593,7 @@ static int attach_by_scanning(struct ubi_device *ubi)
ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count; ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
ubi->max_ec = si->max_ec; ubi->max_ec = si->max_ec;
ubi->mean_ec = si->mean_ec; ubi->mean_ec = si->mean_ec;
ubi_msg("max. sequence number: %llu", si->max_sqnum);
err = ubi_read_volume_table(ubi, si); err = ubi_read_volume_table(ubi, si);
if (err) if (err)
......
...@@ -418,7 +418,8 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, ...@@ -418,7 +418,8 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
* may try to recover data. FIXME: but this is * may try to recover data. FIXME: but this is
* not implemented. * not implemented.
*/ */
if (err == UBI_IO_BAD_VID_HDR) { if (err == UBI_IO_BAD_HDR_READ ||
err == UBI_IO_BAD_HDR) {
ubi_warn("corrupted VID header at PEB " ubi_warn("corrupted VID header at PEB "
"%d, LEB %d:%d", pnum, vol_id, "%d, LEB %d:%d", pnum, vol_id,
lnum); lnum);
...@@ -961,8 +962,8 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, ...@@ -961,8 +962,8 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
*/ */
static int is_error_sane(int err) static int is_error_sane(int err)
{ {
if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_VID_HDR || if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR ||
err == -ETIMEDOUT) err == UBI_IO_BAD_HDR_READ || err == -ETIMEDOUT)
return 0; return 0;
return 1; return 1;
} }
...@@ -1164,6 +1165,44 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, ...@@ -1164,6 +1165,44 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
return err; return err;
} }
/**
* print_rsvd_warning - warn about not having enough reserved PEBs.
* @ubi: UBI device description object
*
* This is a helper function for 'ubi_eba_init_scan()' which is called when UBI
* cannot reserve enough PEBs for bad block handling. This function makes a
* decision whether we have to print a warning or not. The algorithm is as
* follows:
* o if this is a new UBI image, then just print the warning
* o if this is an UBI image which has already been used for some time, print
* a warning only if we can reserve less than 10% of the expected amount of
* the reserved PEB.
*
* The idea is that when UBI is used, PEBs become bad, and the reserved pool
* of PEBs becomes smaller, which is normal and we do not want to scare users
* with a warning every time they attach the MTD device. This was an issue
* reported by real users.
*/
static void print_rsvd_warning(struct ubi_device *ubi,
struct ubi_scan_info *si)
{
/*
* The 1 << 18 (256KiB) number is picked randomly, just a reasonably
* large number to distinguish between newly flashed and used images.
*/
if (si->max_sqnum > (1 << 18)) {
int min = ubi->beb_rsvd_level / 10;
if (!min)
min = 1;
if (ubi->beb_rsvd_pebs > min)
return;
}
ubi_warn("cannot reserve enough PEBs for bad PEB handling, reserved %d,"
" need %d", ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
}
/** /**
* ubi_eba_init_scan - initialize the EBA sub-system using scanning information. * ubi_eba_init_scan - initialize the EBA sub-system using scanning information.
* @ubi: UBI device description object * @ubi: UBI device description object
...@@ -1236,9 +1275,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si) ...@@ -1236,9 +1275,7 @@ int ubi_eba_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
if (ubi->avail_pebs < ubi->beb_rsvd_level) { if (ubi->avail_pebs < ubi->beb_rsvd_level) {
/* No enough free physical eraseblocks */ /* No enough free physical eraseblocks */
ubi->beb_rsvd_pebs = ubi->avail_pebs; ubi->beb_rsvd_pebs = ubi->avail_pebs;
ubi_warn("cannot reserve enough PEBs for bad PEB " print_rsvd_warning(ubi, si);
"handling, reserved %d, need %d",
ubi->beb_rsvd_pebs, ubi->beb_rsvd_level);
} else } else
ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; ubi->beb_rsvd_pebs = ubi->beb_rsvd_level;
......
...@@ -150,6 +150,8 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, ...@@ -150,6 +150,8 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
retry: retry:
err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
if (err) { if (err) {
const char *errstr = (err == -EBADMSG) ? " (ECC error)" : "";
if (err == -EUCLEAN) { if (err == -EUCLEAN) {
/* /*
* -EUCLEAN is reported if there was a bit-flip which * -EUCLEAN is reported if there was a bit-flip which
...@@ -165,15 +167,15 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset, ...@@ -165,15 +167,15 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
} }
if (read != len && retries++ < UBI_IO_RETRIES) { if (read != len && retries++ < UBI_IO_RETRIES) {
dbg_io("error %d while reading %d bytes from PEB %d:%d," dbg_io("error %d%s while reading %d bytes from PEB %d:%d,"
" read only %zd bytes, retry", " read only %zd bytes, retry",
err, len, pnum, offset, read); err, errstr, len, pnum, offset, read);
yield(); yield();
goto retry; goto retry;
} }
ubi_err("error %d while reading %d bytes from PEB %d:%d, " ubi_err("error %d%s while reading %d bytes from PEB %d:%d, "
"read %zd bytes", err, len, pnum, offset, read); "read %zd bytes", err, errstr, len, pnum, offset, read);
ubi_dbg_dump_stack(); ubi_dbg_dump_stack();
/* /*
...@@ -515,7 +517,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum) ...@@ -515,7 +517,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
* In this case we probably anyway have garbage in this PEB. * In this case we probably anyway have garbage in this PEB.
*/ */
err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); err1 = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0);
if (err1 == UBI_IO_BAD_VID_HDR) if (err1 == UBI_IO_BAD_HDR_READ || err1 == UBI_IO_BAD_HDR)
/* /*
* The VID header is corrupted, so we can safely erase this * The VID header is corrupted, so we can safely erase this
* PEB and not afraid that it will be treated as a valid PEB in * PEB and not afraid that it will be treated as a valid PEB in
...@@ -709,7 +711,7 @@ static int validate_ec_hdr(const struct ubi_device *ubi, ...@@ -709,7 +711,7 @@ static int validate_ec_hdr(const struct ubi_device *ubi,
* o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
* and corrected by the flash driver; this is harmless but may indicate that * and corrected by the flash driver; this is harmless but may indicate that
* this eraseblock may become bad soon (but may be not); * this eraseblock may become bad soon (but may be not);
* o %UBI_IO_BAD_EC_HDR if the erase counter header is corrupted (a CRC error); * o %UBI_IO_BAD_HDR if the erase counter header is corrupted (a CRC error);
* o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty; * o %UBI_IO_PEB_EMPTY if the physical eraseblock is empty;
* o a negative error code in case of failure. * o a negative error code in case of failure.
*/ */
...@@ -736,23 +738,21 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, ...@@ -736,23 +738,21 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
* header is still OK, we just report this as there was a * header is still OK, we just report this as there was a
* bit-flip. * bit-flip.
*/ */
read_err = err; if (err == -EBADMSG)
read_err = UBI_IO_BAD_HDR_READ;
} }
magic = be32_to_cpu(ec_hdr->magic); magic = be32_to_cpu(ec_hdr->magic);
if (magic != UBI_EC_HDR_MAGIC) { if (magic != UBI_EC_HDR_MAGIC) {
if (read_err)
return read_err;
/* /*
* The magic field is wrong. Let's check if we have read all * The magic field is wrong. Let's check if we have read all
* 0xFF. If yes, this physical eraseblock is assumed to be * 0xFF. If yes, this physical eraseblock is assumed to be
* empty. * empty.
*
* But if there was a read error, we do not test it for all
* 0xFFs. Even if it does contain all 0xFFs, this error
* indicates that something is still wrong with this physical
* eraseblock and we anyway cannot treat it as empty.
*/ */
if (read_err != -EBADMSG && if (check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
/* The physical eraseblock is supposedly empty */ /* The physical eraseblock is supposedly empty */
if (verbose) if (verbose)
ubi_warn("no EC header found at PEB %d, " ubi_warn("no EC header found at PEB %d, "
...@@ -774,7 +774,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, ...@@ -774,7 +774,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
} else if (UBI_IO_DEBUG) } else if (UBI_IO_DEBUG)
dbg_msg("bad magic number at PEB %d: %08x instead of " dbg_msg("bad magic number at PEB %d: %08x instead of "
"%08x", pnum, magic, UBI_EC_HDR_MAGIC); "%08x", pnum, magic, UBI_EC_HDR_MAGIC);
return UBI_IO_BAD_EC_HDR; return UBI_IO_BAD_HDR;
} }
crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
...@@ -788,7 +788,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, ...@@ -788,7 +788,7 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
} else if (UBI_IO_DEBUG) } else if (UBI_IO_DEBUG)
dbg_msg("bad EC header CRC at PEB %d, calculated " dbg_msg("bad EC header CRC at PEB %d, calculated "
"%#08x, read %#08x", pnum, crc, hdr_crc); "%#08x, read %#08x", pnum, crc, hdr_crc);
return UBI_IO_BAD_EC_HDR; return read_err ?: UBI_IO_BAD_HDR;
} }
/* And of course validate what has just been read from the media */ /* And of course validate what has just been read from the media */
...@@ -798,6 +798,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum, ...@@ -798,6 +798,10 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
return -EINVAL; return -EINVAL;
} }
/*
* If there was %-EBADMSG, but the header CRC is still OK, report about
* a bit-flip to force scrubbing on this PEB.
*/
return read_err ? UBI_IO_BITFLIPS : 0; return read_err ? UBI_IO_BITFLIPS : 0;
} }
...@@ -977,7 +981,7 @@ static int validate_vid_hdr(const struct ubi_device *ubi, ...@@ -977,7 +981,7 @@ static int validate_vid_hdr(const struct ubi_device *ubi,
* o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected * o %UBI_IO_BITFLIPS if the CRC is correct, but bit-flips were detected
* and corrected by the flash driver; this is harmless but may indicate that * and corrected by the flash driver; this is harmless but may indicate that
* this eraseblock may become bad soon; * this eraseblock may become bad soon;
* o %UBI_IO_BAD_VID_HDR if the volume identifier header is corrupted (a CRC * o %UBI_IO_BAD_HDR if the volume identifier header is corrupted (a CRC
* error detected); * error detected);
* o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID * o %UBI_IO_PEB_FREE if the physical eraseblock is free (i.e., there is no VID
* header there); * header there);
...@@ -1008,22 +1012,20 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, ...@@ -1008,22 +1012,20 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
* CRC check-sum and we will identify this. If the VID header is * CRC check-sum and we will identify this. If the VID header is
* still OK, we just report this as there was a bit-flip. * still OK, we just report this as there was a bit-flip.
*/ */
read_err = err; if (err == -EBADMSG)
read_err = UBI_IO_BAD_HDR_READ;
} }
magic = be32_to_cpu(vid_hdr->magic); magic = be32_to_cpu(vid_hdr->magic);
if (magic != UBI_VID_HDR_MAGIC) { if (magic != UBI_VID_HDR_MAGIC) {
if (read_err)
return read_err;
/* /*
* If we have read all 0xFF bytes, the VID header probably does * If we have read all 0xFF bytes, the VID header probably does
* not exist and the physical eraseblock is assumed to be free. * not exist and the physical eraseblock is assumed to be free.
*
* But if there was a read error, we do not test the data for
* 0xFFs. Even if it does contain all 0xFFs, this error
* indicates that something is still wrong with this physical
* eraseblock and it cannot be regarded as free.
*/ */
if (read_err != -EBADMSG && if (check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
/* The physical eraseblock is supposedly free */ /* The physical eraseblock is supposedly free */
if (verbose) if (verbose)
ubi_warn("no VID header found at PEB %d, " ubi_warn("no VID header found at PEB %d, "
...@@ -1045,7 +1047,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, ...@@ -1045,7 +1047,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
} else if (UBI_IO_DEBUG) } else if (UBI_IO_DEBUG)
dbg_msg("bad magic number at PEB %d: %08x instead of " dbg_msg("bad magic number at PEB %d: %08x instead of "
"%08x", pnum, magic, UBI_VID_HDR_MAGIC); "%08x", pnum, magic, UBI_VID_HDR_MAGIC);
return UBI_IO_BAD_VID_HDR; return UBI_IO_BAD_HDR;
} }
crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
...@@ -1059,7 +1061,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, ...@@ -1059,7 +1061,7 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
} else if (UBI_IO_DEBUG) } else if (UBI_IO_DEBUG)
dbg_msg("bad CRC at PEB %d, calculated %#08x, " dbg_msg("bad CRC at PEB %d, calculated %#08x, "
"read %#08x", pnum, crc, hdr_crc); "read %#08x", pnum, crc, hdr_crc);
return UBI_IO_BAD_VID_HDR; return read_err ?: UBI_IO_BAD_HDR;
} }
/* Validate the VID header that we have just read */ /* Validate the VID header that we have just read */
...@@ -1069,6 +1071,10 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, ...@@ -1069,6 +1071,10 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
return -EINVAL; return -EINVAL;
} }
/*
* If there was a read error (%-EBADMSG), but the header CRC is still
* OK, report about a bit-flip to force scrubbing on this PEB.
*/
return read_err ? UBI_IO_BITFLIPS : 0; return read_err ? UBI_IO_BITFLIPS : 0;
} }
......
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/crc32.h> #include <linux/crc32.h>
#include <linux/math64.h> #include <linux/math64.h>
#include <linux/random.h>
#include "ubi.h" #include "ubi.h"
#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
...@@ -72,16 +73,19 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec, ...@@ -72,16 +73,19 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
{ {
struct ubi_scan_leb *seb; struct ubi_scan_leb *seb;
if (list == &si->free) if (list == &si->free) {
dbg_bld("add to free: PEB %d, EC %d", pnum, ec); dbg_bld("add to free: PEB %d, EC %d", pnum, ec);
else if (list == &si->erase) si->free_peb_count += 1;
} else if (list == &si->erase) {
dbg_bld("add to erase: PEB %d, EC %d", pnum, ec); dbg_bld("add to erase: PEB %d, EC %d", pnum, ec);
else if (list == &si->corr) { si->erase_peb_count += 1;
} else if (list == &si->corr) {
dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec); dbg_bld("add to corrupted: PEB %d, EC %d", pnum, ec);
si->corr_count += 1; si->corr_peb_count += 1;
} else if (list == &si->alien) } else if (list == &si->alien) {
dbg_bld("add to alien: PEB %d, EC %d", pnum, ec); dbg_bld("add to alien: PEB %d, EC %d", pnum, ec);
else si->alien_peb_count += 1;
} else
BUG(); BUG();
seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL); seb = kmalloc(sizeof(struct ubi_scan_leb), GFP_KERNEL);
...@@ -517,6 +521,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -517,6 +521,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
sv->leb_count += 1; sv->leb_count += 1;
rb_link_node(&seb->u.rb, parent, p); rb_link_node(&seb->u.rb, parent, p);
rb_insert_color(&seb->u.rb, &sv->root); rb_insert_color(&seb->u.rb, &sv->root);
si->used_peb_count += 1;
return 0; return 0;
} }
...@@ -745,19 +750,17 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -745,19 +750,17 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
bitflips = 1; bitflips = 1;
else if (err == UBI_IO_PEB_EMPTY) else if (err == UBI_IO_PEB_EMPTY)
return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase); return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
else if (err == UBI_IO_BAD_EC_HDR) { else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR) {
/* /*
* We have to also look at the VID header, possibly it is not * We have to also look at the VID header, possibly it is not
* corrupted. Set %bitflips flag in order to make this PEB be * corrupted. Set %bitflips flag in order to make this PEB be
* moved and EC be re-created. * moved and EC be re-created.
*/ */
ec_corr = 1; ec_corr = err;
ec = UBI_SCAN_UNKNOWN_EC; ec = UBI_SCAN_UNKNOWN_EC;
bitflips = 1; bitflips = 1;
} }
si->is_empty = 0;
if (!ec_corr) { if (!ec_corr) {
int image_seq; int image_seq;
...@@ -813,9 +816,12 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -813,9 +816,12 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
return err; return err;
else if (err == UBI_IO_BITFLIPS) else if (err == UBI_IO_BITFLIPS)
bitflips = 1; bitflips = 1;
else if (err == UBI_IO_BAD_VID_HDR || else if (err == UBI_IO_BAD_HDR_READ || err == UBI_IO_BAD_HDR ||
(err == UBI_IO_PEB_FREE && ec_corr)) { (err == UBI_IO_PEB_FREE && ec_corr)) {
/* VID header is corrupted */ /* VID header is corrupted */
if (err == UBI_IO_BAD_HDR_READ ||
ec_corr == UBI_IO_BAD_HDR_READ)
si->read_err_count += 1;
err = add_to_list(si, pnum, ec, &si->corr); err = add_to_list(si, pnum, ec, &si->corr);
if (err) if (err)
return err; return err;
...@@ -836,11 +842,11 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -836,11 +842,11 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
switch (vidh->compat) { switch (vidh->compat) {
case UBI_COMPAT_DELETE: case UBI_COMPAT_DELETE:
ubi_msg("\"delete\" compatible internal volume %d:%d" ubi_msg("\"delete\" compatible internal volume %d:%d"
" found, remove it", vol_id, lnum); " found, will remove it", vol_id, lnum);
err = add_to_list(si, pnum, ec, &si->corr); err = add_to_list(si, pnum, ec, &si->corr);
if (err) if (err)
return err; return err;
break; return 0;
case UBI_COMPAT_RO: case UBI_COMPAT_RO:
ubi_msg("read-only compatible internal volume %d:%d" ubi_msg("read-only compatible internal volume %d:%d"
...@@ -855,7 +861,6 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -855,7 +861,6 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
err = add_to_list(si, pnum, ec, &si->alien); err = add_to_list(si, pnum, ec, &si->alien);
if (err) if (err)
return err; return err;
si->alien_peb_count += 1;
return 0; return 0;
case UBI_COMPAT_REJECT: case UBI_COMPAT_REJECT:
...@@ -885,6 +890,85 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, ...@@ -885,6 +890,85 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
return 0; return 0;
} }
/**
* check_what_we_have - check what PEB were found by scanning.
* @ubi: UBI device description object
* @si: scanning information
*
* This is a helper function which takes a look what PEBs were found by
* scanning, and decides whether the flash is empty and should be formatted and
* whether there are too many corrupted PEBs and we should not attach this
* MTD device. Returns zero if we should proceed with attaching the MTD device,
* and %-EINVAL if we should not.
*/
static int check_what_we_have(struct ubi_device *ubi, struct ubi_scan_info *si)
{
struct ubi_scan_leb *seb;
int max_corr;
max_corr = ubi->peb_count - si->bad_peb_count - si->alien_peb_count;
max_corr = max_corr / 20 ?: 8;
/*
* 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.
*/
if (si->corr_peb_count >= 8) {
ubi_warn("%d PEBs are corrupted", si->corr_peb_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");
/*
* If too many PEBs are corrupted, we refuse attaching,
* otherwise, only print a warning.
*/
if (si->corr_peb_count >= max_corr) {
ubi_err("too many corrupted PEBs, refusing this device");
return -EINVAL;
}
}
if (si->free_peb_count + si->used_peb_count +
si->alien_peb_count == 0) {
/* No UBI-formatted eraseblocks were found */
if (si->corr_peb_count == si->read_err_count &&
si->corr_peb_count < 8) {
/* No or just few corrupted PEBs, and all of them had a
* read error. We assume that those are bad PEBs, which
* were just not marked as bad so far.
*
* This piece of code basically tries to distinguish
* between the following 2 situations:
*
* 1. Flash is empty, but there are few bad PEBs, which
* are not marked as bad so far, and which were read
* with error. We want to go ahead and format this
* flash. While formating, the faulty PEBs will
* probably be marked as bad.
*
* 2. Flash probably contains non-UBI data and we do
* not want to format it and destroy possibly needed
* data (e.g., consider the case when the bootloader
* MTD partition was accidentally fed to UBI).
*/
si->is_empty = 1;
ubi_msg("empty MTD device detected");
get_random_bytes(&ubi->image_seq, sizeof(ubi->image_seq));
} else {
ubi_err("MTD device possibly contains non-UBI data, "
"refusing it");
return -EINVAL;
}
}
if (si->corr_peb_count > 0)
ubi_msg("corrupted PEBs will be formatted");
return 0;
}
/** /**
* ubi_scan - scan an MTD device. * ubi_scan - scan an MTD device.
* @ubi: UBI device description object * @ubi: UBI device description object
...@@ -909,7 +993,6 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) ...@@ -909,7 +993,6 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
INIT_LIST_HEAD(&si->erase); INIT_LIST_HEAD(&si->erase);
INIT_LIST_HEAD(&si->alien); INIT_LIST_HEAD(&si->alien);
si->volumes = RB_ROOT; si->volumes = RB_ROOT;
si->is_empty = 1;
err = -ENOMEM; err = -ENOMEM;
ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL);
...@@ -935,21 +1018,9 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi) ...@@ -935,21 +1018,9 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
if (si->ec_count) if (si->ec_count)
si->mean_ec = div_u64(si->ec_sum, si->ec_count); si->mean_ec = div_u64(si->ec_sum, si->ec_count);
if (si->is_empty) err = check_what_we_have(ubi, si);
ubi_msg("empty MTD device detected"); if (err)
goto out_vidh;
/*
* 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 * In case of unknown erase counter we use the mean erase counter
......
...@@ -91,10 +91,16 @@ struct ubi_scan_volume { ...@@ -91,10 +91,16 @@ struct ubi_scan_volume {
* @erase: list of physical eraseblocks which have to be erased * @erase: list of physical eraseblocks which have to be erased
* @alien: list of physical eraseblocks which should not be used by UBI (e.g., * @alien: list of physical eraseblocks which should not be used by UBI (e.g.,
* those belonging to "preserve"-compatible internal volumes) * those belonging to "preserve"-compatible internal volumes)
* @used_peb_count: count of used PEBs
* @corr_peb_count: count of PEBs in the @corr list
* @read_err_count: count of PEBs read with error (%UBI_IO_BAD_HDR_READ was
* returned)
* @free_peb_count: count of PEBs in the @free list
* @erase_peb_count: count of PEBs in the @erase list
* @alien_peb_count: count of PEBs in the @alien list
* @bad_peb_count: count of bad physical eraseblocks * @bad_peb_count: count of bad physical eraseblocks
* @vols_found: number of volumes found during scanning * @vols_found: number of volumes found during scanning
* @highest_vol_id: highest volume ID * @highest_vol_id: highest volume ID
* @alien_peb_count: count of physical eraseblocks in the @alien list
* @is_empty: flag indicating whether the MTD device is empty or not * @is_empty: flag indicating whether the MTD device is empty or not
* @min_ec: lowest erase counter value * @min_ec: lowest erase counter value
* @max_ec: highest erase counter value * @max_ec: highest erase counter value
...@@ -102,7 +108,6 @@ struct ubi_scan_volume { ...@@ -102,7 +108,6 @@ struct ubi_scan_volume {
* @mean_ec: mean erase counter value * @mean_ec: mean erase counter value
* @ec_sum: a temporary variable used when calculating @mean_ec * @ec_sum: a temporary variable used when calculating @mean_ec
* @ec_count: a temporary variable used when calculating @mean_ec * @ec_count: a temporary variable used when calculating @mean_ec
* @corr_count: count of corrupted PEBs
* *
* This data structure contains the result of scanning and may be used by other * This data structure contains the result of scanning and may be used by other
* UBI sub-systems to build final UBI data structures, further error-recovery * UBI sub-systems to build final UBI data structures, further error-recovery
...@@ -114,10 +119,15 @@ struct ubi_scan_info { ...@@ -114,10 +119,15 @@ struct ubi_scan_info {
struct list_head free; struct list_head free;
struct list_head erase; struct list_head erase;
struct list_head alien; struct list_head alien;
int used_peb_count;
int corr_peb_count;
int read_err_count;
int free_peb_count;
int erase_peb_count;
int alien_peb_count;
int bad_peb_count; int bad_peb_count;
int vols_found; int vols_found;
int highest_vol_id; int highest_vol_id;
int alien_peb_count;
int is_empty; int is_empty;
int min_ec; int min_ec;
int max_ec; int max_ec;
...@@ -125,7 +135,6 @@ struct ubi_scan_info { ...@@ -125,7 +135,6 @@ struct ubi_scan_info {
int mean_ec; int mean_ec;
uint64_t ec_sum; uint64_t ec_sum;
int ec_count; int ec_count;
int corr_count;
}; };
struct ubi_device; struct ubi_device;
...@@ -135,7 +144,7 @@ struct ubi_vid_hdr; ...@@ -135,7 +144,7 @@ struct ubi_vid_hdr;
* ubi_scan_move_to_list - move a PEB from the volume tree to a list. * ubi_scan_move_to_list - move a PEB from the volume tree to a list.
* *
* @sv: volume scanning information * @sv: volume scanning information
* @seb: scanning eraseblock infprmation * @seb: scanning eraseblock information
* @list: the list to move to * @list: the list to move to
*/ */
static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv, static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv,
......
...@@ -89,16 +89,16 @@ ...@@ -89,16 +89,16 @@
* %0xFF bytes * %0xFF bytes
* UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a * UBI_IO_PEB_FREE: the physical eraseblock is free, i.e. it contains only a
* valid erase counter header, and the rest are %0xFF bytes * valid erase counter header, and the rest are %0xFF bytes
* UBI_IO_BAD_EC_HDR: the erase counter header is corrupted (bad magic or CRC) * UBI_IO_BAD_HDR: the EC or VID header is corrupted (bad magic or CRC)
* UBI_IO_BAD_VID_HDR: the volume identifier header is corrupted (bad magic or * UBI_IO_BAD_HDR_READ: the same as %UBI_IO_BAD_HDR, but also there was a read
* CRC) * error reported by the flash driver
* UBI_IO_BITFLIPS: bit-flips were detected and corrected * UBI_IO_BITFLIPS: bit-flips were detected and corrected
*/ */
enum { enum {
UBI_IO_PEB_EMPTY = 1, UBI_IO_PEB_EMPTY = 1,
UBI_IO_PEB_FREE, UBI_IO_PEB_FREE,
UBI_IO_BAD_EC_HDR, UBI_IO_BAD_HDR,
UBI_IO_BAD_VID_HDR, UBI_IO_BAD_HDR_READ,
UBI_IO_BITFLIPS UBI_IO_BITFLIPS
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment