aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/scan.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi/scan.c')
-rw-r--r--drivers/mtd/ubi/scan.c136
1 files changed, 47 insertions, 89 deletions
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 96d410e106ab..967bb4406df9 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -19,9 +19,9 @@
19 */ 19 */
20 20
21/* 21/*
22 * UBI scanning unit. 22 * UBI scanning sub-system.
23 * 23 *
24 * This unit is responsible for scanning the flash media, checking UBI 24 * This sub-system is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image. 25 * headers and providing complete information about the UBI flash image.
26 * 26 *
27 * The scanning information is represented by a &struct ubi_scan_info' object. 27 * The scanning information is represented by a &struct ubi_scan_info' object.
@@ -93,8 +93,7 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
93} 93}
94 94
95/** 95/**
96 * validate_vid_hdr - check that volume identifier header is correct and 96 * validate_vid_hdr - check volume identifier header.
97 * consistent.
98 * @vid_hdr: the volume identifier header to check 97 * @vid_hdr: the volume identifier header to check
99 * @sv: information about the volume this logical eraseblock belongs to 98 * @sv: information about the volume this logical eraseblock belongs to
100 * @pnum: physical eraseblock number the VID header came from 99 * @pnum: physical eraseblock number the VID header came from
@@ -103,7 +102,7 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
103 * non-zero if an inconsistency was found and zero if not. 102 * non-zero if an inconsistency was found and zero if not.
104 * 103 *
105 * Note, UBI does sanity check of everything it reads from the flash media. 104 * Note, UBI does sanity check of everything it reads from the flash media.
106 * Most of the checks are done in the I/O unit. Here we check that the 105 * Most of the checks are done in the I/O sub-system. Here we check that the
107 * information in the VID header is consistent to the information in other VID 106 * information in the VID header is consistent to the information in other VID
108 * headers of the same volume. 107 * headers of the same volume.
109 */ 108 */
@@ -247,45 +246,21 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
247 struct ubi_vid_hdr *vh = NULL; 246 struct ubi_vid_hdr *vh = NULL;
248 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); 247 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
249 248
250 if (seb->sqnum == 0 && sqnum2 == 0) { 249 if (sqnum2 == seb->sqnum) {
251 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
252
253 /* 250 /*
254 * UBI constantly increases the logical eraseblock version 251 * This must be a really ancient UBI image which has been
255 * number and it can overflow. Thus, we have to bear in mind 252 * created before sequence numbers support has been added. At
256 * that versions that are close to %0xFFFFFFFF are less then 253 * that times we used 32-bit LEB versions stored in logical
257 * versions that are close to %0. 254 * eraseblocks. That was before UBI got into mainline. We do not
258 * 255 * support these images anymore. Well, those images will work
259 * The UBI WL unit guarantees that the number of pending tasks 256 * still work, but only if no unclean reboots happened.
260 * is not greater then %0x7FFFFFFF. So, if the difference
261 * between any two versions is greater or equivalent to
262 * %0x7FFFFFFF, there was an overflow and the logical
263 * eraseblock with lower version is actually newer then the one
264 * with higher version.
265 *
266 * FIXME: but this is anyway obsolete and will be removed at
267 * some point.
268 */ 257 */
269 dbg_bld("using old crappy leb_ver stuff"); 258 ubi_err("unsupported on-flash UBI format\n");
270 259 return -EINVAL;
271 if (v1 == v2) { 260 }
272 ubi_err("PEB %d and PEB %d have the same version %lld",
273 seb->pnum, pnum, v1);
274 return -EINVAL;
275 }
276
277 abs = v1 - v2;
278 if (abs < 0)
279 abs = -abs;
280 261
281 if (abs < 0x7FFFFFFF) 262 /* Obviously the LEB with lower sequence counter is older */
282 /* Non-overflow situation */ 263 second_is_newer = !!(sqnum2 > seb->sqnum);
283 second_is_newer = (v2 > v1);
284 else
285 second_is_newer = (v2 < v1);
286 } else
287 /* Obviously the LEB with lower sequence counter is older */
288 second_is_newer = sqnum2 > seb->sqnum;
289 264
290 /* 265 /*
291 * Now we know which copy is newer. If the copy flag of the PEB with 266 * Now we know which copy is newer. If the copy flag of the PEB with
@@ -293,7 +268,7 @@ static int compare_lebs(struct ubi_device *ubi, const struct ubi_scan_leb *seb,
293 * check data CRC. For the second PEB we already have the VID header, 268 * check data CRC. For the second PEB we already have the VID header,
294 * for the first one - we'll need to re-read it from flash. 269 * for the first one - we'll need to re-read it from flash.
295 * 270 *
296 * FIXME: this may be optimized so that we wouldn't read twice. 271 * Note: this may be optimized so that we wouldn't read twice.
297 */ 272 */
298 273
299 if (second_is_newer) { 274 if (second_is_newer) {
@@ -379,8 +354,7 @@ out_free_vidh:
379} 354}
380 355
381/** 356/**
382 * ubi_scan_add_used - add information about a physical eraseblock to the 357 * ubi_scan_add_used - add physical eraseblock to the scanning information.
383 * scanning information.
384 * @ubi: UBI device description object 358 * @ubi: UBI device description object
385 * @si: scanning information 359 * @si: scanning information
386 * @pnum: the physical eraseblock number 360 * @pnum: the physical eraseblock number
@@ -400,7 +374,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
400 int bitflips) 374 int bitflips)
401{ 375{
402 int err, vol_id, lnum; 376 int err, vol_id, lnum;
403 uint32_t leb_ver;
404 unsigned long long sqnum; 377 unsigned long long sqnum;
405 struct ubi_scan_volume *sv; 378 struct ubi_scan_volume *sv;
406 struct ubi_scan_leb *seb; 379 struct ubi_scan_leb *seb;
@@ -409,10 +382,9 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
409 vol_id = be32_to_cpu(vid_hdr->vol_id); 382 vol_id = be32_to_cpu(vid_hdr->vol_id);
410 lnum = be32_to_cpu(vid_hdr->lnum); 383 lnum = be32_to_cpu(vid_hdr->lnum);
411 sqnum = be64_to_cpu(vid_hdr->sqnum); 384 sqnum = be64_to_cpu(vid_hdr->sqnum);
412 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
413 385
414 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d", 386 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, bitflips %d",
415 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips); 387 pnum, vol_id, lnum, ec, sqnum, bitflips);
416 388
417 sv = add_volume(si, vol_id, pnum, vid_hdr); 389 sv = add_volume(si, vol_id, pnum, vid_hdr);
418 if (IS_ERR(sv) < 0) 390 if (IS_ERR(sv) < 0)
@@ -445,25 +417,20 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
445 */ 417 */
446 418
447 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, " 419 dbg_bld("this LEB already exists: PEB %d, sqnum %llu, "
448 "LEB ver %u, EC %d", seb->pnum, seb->sqnum, 420 "EC %d", seb->pnum, seb->sqnum, seb->ec);
449 seb->leb_ver, seb->ec);
450
451 /*
452 * Make sure that the logical eraseblocks have different
453 * versions. Otherwise the image is bad.
454 */
455 if (seb->leb_ver == leb_ver && leb_ver != 0) {
456 ubi_err("two LEBs with same version %u", leb_ver);
457 ubi_dbg_dump_seb(seb, 0);
458 ubi_dbg_dump_vid_hdr(vid_hdr);
459 return -EINVAL;
460 }
461 421
462 /* 422 /*
463 * Make sure that the logical eraseblocks have different 423 * Make sure that the logical eraseblocks have different
464 * sequence numbers. Otherwise the image is bad. 424 * sequence numbers. Otherwise the image is bad.
465 * 425 *
466 * FIXME: remove 'sqnum != 0' check when leb_ver is removed. 426 * However, if the sequence number is zero, we assume it must
427 * be an ancient UBI image from the era when UBI did not have
428 * sequence numbers. We still can attach these images, unless
429 * there is a need to distinguish between old and new
430 * eraseblocks, in which case we'll refuse the image in
431 * 'compare_lebs()'. In other words, we attach old clean
432 * images, but refuse attaching old images with duplicated
433 * logical eraseblocks because there was an unclean reboot.
467 */ 434 */
468 if (seb->sqnum == sqnum && sqnum != 0) { 435 if (seb->sqnum == sqnum && sqnum != 0) {
469 ubi_err("two LEBs with same sequence number %llu", 436 ubi_err("two LEBs with same sequence number %llu",
@@ -503,7 +470,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
503 seb->pnum = pnum; 470 seb->pnum = pnum;
504 seb->scrub = ((cmp_res & 2) || bitflips); 471 seb->scrub = ((cmp_res & 2) || bitflips);
505 seb->sqnum = sqnum; 472 seb->sqnum = sqnum;
506 seb->leb_ver = leb_ver;
507 473
508 if (sv->highest_lnum == lnum) 474 if (sv->highest_lnum == lnum)
509 sv->last_data_size = 475 sv->last_data_size =
@@ -540,7 +506,6 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
540 seb->lnum = lnum; 506 seb->lnum = lnum;
541 seb->sqnum = sqnum; 507 seb->sqnum = sqnum;
542 seb->scrub = bitflips; 508 seb->scrub = bitflips;
543 seb->leb_ver = leb_ver;
544 509
545 if (sv->highest_lnum <= lnum) { 510 if (sv->highest_lnum <= lnum) {
546 sv->highest_lnum = lnum; 511 sv->highest_lnum = lnum;
@@ -554,8 +519,7 @@ int ubi_scan_add_used(struct ubi_device *ubi, struct ubi_scan_info *si,
554} 519}
555 520
556/** 521/**
557 * ubi_scan_find_sv - find information about a particular volume in the 522 * ubi_scan_find_sv - find volume in the scanning information.
558 * scanning information.
559 * @si: scanning information 523 * @si: scanning information
560 * @vol_id: the requested volume ID 524 * @vol_id: the requested volume ID
561 * 525 *
@@ -584,8 +548,7 @@ struct ubi_scan_volume *ubi_scan_find_sv(const struct ubi_scan_info *si,
584} 548}
585 549
586/** 550/**
587 * ubi_scan_find_seb - find information about a particular logical 551 * ubi_scan_find_seb - find LEB in the volume scanning information.
588 * eraseblock in the volume scanning information.
589 * @sv: a pointer to the volume scanning information 552 * @sv: a pointer to the volume scanning information
590 * @lnum: the requested logical eraseblock 553 * @lnum: the requested logical eraseblock
591 * 554 *
@@ -645,9 +608,9 @@ void ubi_scan_rm_volume(struct ubi_scan_info *si, struct ubi_scan_volume *sv)
645 * 608 *
646 * This function erases physical eraseblock 'pnum', and writes the erase 609 * This function erases physical eraseblock 'pnum', and writes the erase
647 * counter header to it. This function should only be used on UBI device 610 * counter header to it. This function should only be used on UBI device
648 * initialization stages, when the EBA unit had not been yet initialized. This 611 * initialization stages, when the EBA sub-system had not been yet initialized.
649 * function returns zero in case of success and a negative error code in case 612 * This function returns zero in case of success and a negative error code in
650 * of failure. 613 * case of failure.
651 */ 614 */
652int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si, 615int ubi_scan_erase_peb(struct ubi_device *ubi, const struct ubi_scan_info *si,
653 int pnum, int ec) 616 int pnum, int ec)
@@ -687,9 +650,10 @@ out_free:
687 * @si: scanning information 650 * @si: scanning information
688 * 651 *
689 * This function returns a free physical eraseblock. It is supposed to be 652 * This function returns a free physical eraseblock. It is supposed to be
690 * called on the UBI initialization stages when the wear-leveling unit is not 653 * called on the UBI initialization stages when the wear-leveling sub-system is
691 * initialized yet. This function picks a physical eraseblocks from one of the 654 * not initialized yet. This function picks a physical eraseblocks from one of
692 * lists, writes the EC header if it is needed, and removes it from the list. 655 * the lists, writes the EC header if it is needed, and removes it from the
656 * list.
693 * 657 *
694 * This function returns scanning physical eraseblock information in case of 658 * This function returns scanning physical eraseblock information in case of
695 * success and an error code in case of failure. 659 * success and an error code in case of failure.
@@ -742,8 +706,7 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
742} 706}
743 707
744/** 708/**
745 * process_eb - read UBI headers, check them and add corresponding data 709 * process_eb - read, check UBI headers, and add them to scanning information.
746 * to the scanning information.
747 * @ubi: UBI device description object 710 * @ubi: UBI device description object
748 * @si: scanning information 711 * @si: scanning information
749 * @pnum: the physical eraseblock number 712 * @pnum: the physical eraseblock number
@@ -751,7 +714,8 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(struct ubi_device *ubi,
751 * This function returns a zero if the physical eraseblock was successfully 714 * This function returns a zero if the physical eraseblock was successfully
752 * handled and a negative error code in case of failure. 715 * handled and a negative error code in case of failure.
753 */ 716 */
754static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum) 717static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si,
718 int pnum)
755{ 719{
756 long long uninitialized_var(ec); 720 long long uninitialized_var(ec);
757 int err, bitflips = 0, vol_id, ec_corr = 0; 721 int err, bitflips = 0, vol_id, ec_corr = 0;
@@ -764,8 +728,9 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
764 return err; 728 return err;
765 else if (err) { 729 else if (err) {
766 /* 730 /*
767 * FIXME: this is actually duty of the I/O unit to initialize 731 * FIXME: this is actually duty of the I/O sub-system to
768 * this, but MTD does not provide enough information. 732 * initialize this, but MTD does not provide enough
733 * information.
769 */ 734 */
770 si->bad_peb_count += 1; 735 si->bad_peb_count += 1;
771 return 0; 736 return 0;
@@ -930,7 +895,7 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
930 for (pnum = 0; pnum < ubi->peb_count; pnum++) { 895 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
931 cond_resched(); 896 cond_resched();
932 897
933 dbg_msg("process PEB %d", pnum); 898 dbg_gen("process PEB %d", pnum);
934 err = process_eb(ubi, si, pnum); 899 err = process_eb(ubi, si, pnum);
935 if (err < 0) 900 if (err < 0)
936 goto out_vidh; 901 goto out_vidh;
@@ -1079,8 +1044,7 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1079#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1044#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1080 1045
1081/** 1046/**
1082 * paranoid_check_si - check if the scanning information is correct and 1047 * paranoid_check_si - check the scanning information.
1083 * consistent.
1084 * @ubi: UBI device description object 1048 * @ubi: UBI device description object
1085 * @si: scanning information 1049 * @si: scanning information
1086 * 1050 *
@@ -1265,11 +1229,6 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1265 ubi_err("bad data_pad %d", sv->data_pad); 1229 ubi_err("bad data_pad %d", sv->data_pad);
1266 goto bad_vid_hdr; 1230 goto bad_vid_hdr;
1267 } 1231 }
1268
1269 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1270 ubi_err("bad leb_ver %u", seb->leb_ver);
1271 goto bad_vid_hdr;
1272 }
1273 } 1232 }
1274 1233
1275 if (!last_seb) 1234 if (!last_seb)
@@ -1299,8 +1258,7 @@ static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1299 if (err < 0) { 1258 if (err < 0) {
1300 kfree(buf); 1259 kfree(buf);
1301 return err; 1260 return err;
1302 } 1261 } else if (err)
1303 else if (err)
1304 buf[pnum] = 1; 1262 buf[pnum] = 1;
1305 } 1263 }
1306 1264