aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/io.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi/io.c')
-rw-r--r--drivers/mtd/ubi/io.c157
1 files changed, 61 insertions, 96 deletions
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index effaff28bab1..4cb69925d8d9 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -98,17 +98,12 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
98static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum); 98static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum);
99static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 99static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
100 const struct ubi_vid_hdr *vid_hdr); 100 const struct ubi_vid_hdr *vid_hdr);
101static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset,
102 int len);
103static int paranoid_check_empty(struct ubi_device *ubi, int pnum);
104#else 101#else
105#define paranoid_check_not_bad(ubi, pnum) 0 102#define paranoid_check_not_bad(ubi, pnum) 0
106#define paranoid_check_peb_ec_hdr(ubi, pnum) 0 103#define paranoid_check_peb_ec_hdr(ubi, pnum) 0
107#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0 104#define paranoid_check_ec_hdr(ubi, pnum, ec_hdr) 0
108#define paranoid_check_peb_vid_hdr(ubi, pnum) 0 105#define paranoid_check_peb_vid_hdr(ubi, pnum) 0
109#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0 106#define paranoid_check_vid_hdr(ubi, pnum, vid_hdr) 0
110#define paranoid_check_all_ff(ubi, pnum, offset, len) 0
111#define paranoid_check_empty(ubi, pnum) 0
112#endif 107#endif
113 108
114/** 109/**
@@ -244,7 +239,7 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
244 return err > 0 ? -EINVAL : err; 239 return err > 0 ? -EINVAL : err;
245 240
246 /* The area we are writing to has to contain all 0xFF bytes */ 241 /* The area we are writing to has to contain all 0xFF bytes */
247 err = paranoid_check_all_ff(ubi, pnum, offset, len); 242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
248 if (err) 243 if (err)
249 return err > 0 ? -EINVAL : err; 244 return err > 0 ? -EINVAL : err;
250 245
@@ -271,8 +266,8 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
271 addr = (loff_t)pnum * ubi->peb_size + offset; 266 addr = (loff_t)pnum * ubi->peb_size + offset;
272 err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf); 267 err = ubi->mtd->write(ubi->mtd, addr, len, &written, buf);
273 if (err) { 268 if (err) {
274 ubi_err("error %d while writing %d bytes to PEB %d:%d, written" 269 ubi_err("error %d while writing %d bytes to PEB %d:%d, written "
275 " %zd bytes", err, len, pnum, offset, written); 270 "%zd bytes", err, len, pnum, offset, written);
276 ubi_dbg_dump_stack(); 271 ubi_dbg_dump_stack();
277 } else 272 } else
278 ubi_assert(written == len); 273 ubi_assert(written == len);
@@ -350,7 +345,7 @@ retry:
350 return -EIO; 345 return -EIO;
351 } 346 }
352 347
353 err = paranoid_check_all_ff(ubi, pnum, 0, ubi->peb_size); 348 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
354 if (err) 349 if (err)
355 return err > 0 ? -EINVAL : err; 350 return err > 0 ? -EINVAL : err;
356 351
@@ -459,6 +454,54 @@ out:
459} 454}
460 455
461/** 456/**
457 * nor_erase_prepare - prepare a NOR flash PEB for erasure.
458 * @ubi: UBI device description object
459 * @pnum: physical eraseblock number to prepare
460 *
461 * NOR flash, or at least some of them, have peculiar embedded PEB erasure
462 * algorithm: the PEB is first filled with zeroes, then it is erased. And
463 * filling with zeroes starts from the end of the PEB. This was observed with
464 * Spansion S29GL512N NOR flash.
465 *
466 * This means that in case of a power cut we may end up with intact data at the
467 * beginning of the PEB, and all zeroes at the end of PEB. In other words, the
468 * EC and VID headers are OK, but a large chunk of data at the end of PEB is
469 * zeroed. This makes UBI mistakenly treat this PEB as used and associate it
470 * with an LEB, which leads to subsequent failures (e.g., UBIFS fails).
471 *
472 * This function is called before erasing NOR PEBs and it zeroes out EC and VID
473 * magic numbers in order to invalidate them and prevent the failures. Returns
474 * zero in case of success and a negative error code in case of failure.
475 */
476static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
477{
478 int err;
479 size_t written;
480 loff_t addr;
481 uint32_t data = 0;
482
483 addr = (loff_t)pnum * ubi->peb_size;
484 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
485 if (err) {
486 ubi_err("error %d while writing 4 bytes to PEB %d:%d, written "
487 "%zd bytes", err, pnum, 0, written);
488 ubi_dbg_dump_stack();
489 return err;
490 }
491
492 addr += ubi->vid_hdr_aloffset;
493 err = ubi->mtd->write(ubi->mtd, addr, 4, &written, (void *)&data);
494 if (err) {
495 ubi_err("error %d while writing 4 bytes to PEB %d:%d, written "
496 "%zd bytes", err, pnum, ubi->vid_hdr_aloffset, written);
497 ubi_dbg_dump_stack();
498 return err;
499 }
500
501 return 0;
502}
503
504/**
462 * ubi_io_sync_erase - synchronously erase a physical eraseblock. 505 * ubi_io_sync_erase - synchronously erase a physical eraseblock.
463 * @ubi: UBI device description object 506 * @ubi: UBI device description object
464 * @pnum: physical eraseblock number to erase 507 * @pnum: physical eraseblock number to erase
@@ -489,6 +532,12 @@ int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
489 return -EROFS; 532 return -EROFS;
490 } 533 }
491 534
535 if (ubi->nor_flash) {
536 err = nor_erase_prepare(ubi, pnum);
537 if (err)
538 return err;
539 }
540
492 if (torture) { 541 if (torture) {
493 ret = torture_peb(ubi, pnum); 542 ret = torture_peb(ubi, pnum);
494 if (ret < 0) 543 if (ret < 0)
@@ -672,11 +721,6 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
672 if (read_err != -EBADMSG && 721 if (read_err != -EBADMSG &&
673 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) { 722 check_pattern(ec_hdr, 0xFF, UBI_EC_HDR_SIZE)) {
674 /* The physical eraseblock is supposedly empty */ 723 /* The physical eraseblock is supposedly empty */
675 err = paranoid_check_all_ff(ubi, pnum, 0,
676 ubi->peb_size);
677 if (err)
678 return err > 0 ? UBI_IO_BAD_EC_HDR : err;
679
680 if (verbose) 724 if (verbose)
681 ubi_warn("no EC header found at PEB %d, " 725 ubi_warn("no EC header found at PEB %d, "
682 "only 0xFF bytes", pnum); 726 "only 0xFF bytes", pnum);
@@ -752,6 +796,7 @@ int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
752 ec_hdr->version = UBI_VERSION; 796 ec_hdr->version = UBI_VERSION;
753 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 797 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
754 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 798 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
799 ec_hdr->image_seq = cpu_to_be32(ubi->image_seq);
755 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 800 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
756 ec_hdr->hdr_crc = cpu_to_be32(crc); 801 ec_hdr->hdr_crc = cpu_to_be32(crc);
757 802
@@ -947,15 +992,6 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
947 if (read_err != -EBADMSG && 992 if (read_err != -EBADMSG &&
948 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) { 993 check_pattern(vid_hdr, 0xFF, UBI_VID_HDR_SIZE)) {
949 /* The physical eraseblock is supposedly free */ 994 /* The physical eraseblock is supposedly free */
950
951 /*
952 * The below is just a paranoid check, it has to be
953 * compiled out if paranoid checks are disabled.
954 */
955 err = paranoid_check_empty(ubi, pnum);
956 if (err)
957 return err > 0 ? UBI_IO_BAD_VID_HDR : err;
958
959 if (verbose) 995 if (verbose)
960 ubi_warn("no VID header found at PEB %d, " 996 ubi_warn("no VID header found at PEB %d, "
961 "only 0xFF bytes", pnum); 997 "only 0xFF bytes", pnum);
@@ -1229,7 +1265,7 @@ exit:
1229} 1265}
1230 1266
1231/** 1267/**
1232 * paranoid_check_all_ff - check that a region of flash is empty. 1268 * ubi_dbg_check_all_ff - check that a region of flash is empty.
1233 * @ubi: UBI device description object 1269 * @ubi: UBI device description object
1234 * @pnum: the physical eraseblock number to check 1270 * @pnum: the physical eraseblock number to check
1235 * @offset: the starting offset within the physical eraseblock to check 1271 * @offset: the starting offset within the physical eraseblock to check
@@ -1239,8 +1275,7 @@ exit:
1239 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error 1275 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error
1240 * code if an error occurred. 1276 * code if an error occurred.
1241 */ 1277 */
1242static int paranoid_check_all_ff(struct ubi_device *ubi, int pnum, int offset, 1278int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1243 int len)
1244{ 1279{
1245 size_t read; 1280 size_t read;
1246 int err; 1281 int err;
@@ -1276,74 +1311,4 @@ error:
1276 return err; 1311 return err;
1277} 1312}
1278 1313
1279/**
1280 * paranoid_check_empty - whether a PEB is empty.
1281 * @ubi: UBI device description object
1282 * @pnum: the physical eraseblock number to check
1283 *
1284 * This function makes sure PEB @pnum is empty, which means it contains only
1285 * %0xFF data bytes. Returns zero if the PEB is empty, %1 if not, and a
1286 * negative error code in case of failure.
1287 *
1288 * Empty PEBs have the EC header, and do not have the VID header. The caller of
1289 * this function should have already made sure the PEB does not have the VID
1290 * header. However, this function re-checks that, because it is possible that
1291 * the header and data has already been written to the PEB.
1292 *
1293 * Let's consider a possible scenario. Suppose there are 2 tasks - A and B.
1294 * Task A is in 'wear_leveling_worker()'. It is reading VID header of PEB X to
1295 * find which LEB it corresponds to. PEB X is currently unmapped, and has no
1296 * VID header. Task B is trying to write to PEB X.
1297 *
1298 * Task A: in 'ubi_io_read_vid_hdr()': reads the VID header from PEB X. The
1299 * read data contain all 0xFF bytes;
1300 * Task B: writes VID header and some data to PEB X;
1301 * Task A: assumes PEB X is empty, calls 'paranoid_check_empty()'. And if we
1302 * do not re-read the VID header, and do not cancel the checking if it
1303 * is there, we fail.
1304 */
1305static int paranoid_check_empty(struct ubi_device *ubi, int pnum)
1306{
1307 int err, offs = ubi->vid_hdr_aloffset, len = ubi->vid_hdr_alsize;
1308 size_t read;
1309 uint32_t magic;
1310 const struct ubi_vid_hdr *vid_hdr;
1311
1312 mutex_lock(&ubi->dbg_buf_mutex);
1313 err = ubi->mtd->read(ubi->mtd, offs, len, &read, ubi->dbg_peb_buf);
1314 if (err && err != -EUCLEAN) {
1315 ubi_err("error %d while reading %d bytes from PEB %d:%d, "
1316 "read %zd bytes", err, len, pnum, offs, read);
1317 goto error;
1318 }
1319
1320 vid_hdr = ubi->dbg_peb_buf;
1321 magic = be32_to_cpu(vid_hdr->magic);
1322 if (magic == UBI_VID_HDR_MAGIC)
1323 /* The PEB contains VID header, so it is not empty */
1324 goto out;
1325
1326 err = check_pattern(ubi->dbg_peb_buf, 0xFF, len);
1327 if (err == 0) {
1328 ubi_err("flash region at PEB %d:%d, length %d does not "
1329 "contain all 0xFF bytes", pnum, offs, len);
1330 goto fail;
1331 }
1332
1333out:
1334 mutex_unlock(&ubi->dbg_buf_mutex);
1335 return 0;
1336
1337fail:
1338 ubi_err("paranoid check failed for PEB %d", pnum);
1339 ubi_msg("hex dump of the %d-%d region", offs, offs + len);
1340 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1341 ubi->dbg_peb_buf, len, 1);
1342 err = 1;
1343error:
1344 ubi_dbg_dump_stack();
1345 mutex_unlock(&ubi->dbg_buf_mutex);
1346 return err;
1347}
1348
1349#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1314#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */