28 sizeof(struct ubi_fm_scan_pool) + \
29 sizeof(struct ubi_fm_scan_pool) + \
30 (ubi->peb_count *
sizeof(
struct ubi_fm_ec)) + \
31 (sizeof(struct ubi_fm_eba) + \
32 (ubi->peb_count *
sizeof(
__be32))) + \
34 return roundup(size, ubi->leb_size);
77 int pnum,
int ec,
int scrub)
130 else if (vol_id > av->
vol_id)
147 dbg_bld(
"found volume (ID %i)", vol_id);
149 rb_link_node(&av->
rb, parent, p);
170 p = &av->
root.rb_node;
189 rb_link_node(&aeb->
u.
rb, parent, p);
246 victim->
ec = aeb->
ec;
254 dbg_bld(
"vol %i: AEB %i's PEB %i is the newer",
257 aeb->
ec = new_aeb->
ec;
265 dbg_bld(
"vol %i: AEB %i's PEB %i is old, dropping it",
284 rb_link_node(&new_aeb->
u.
rb, parent, p);
332 ubi_err(
"orphaned volume in fastmap pool!");
338 return update_vol(ubi, ai, av, new_vh, new_aeb);
361 if (aeb->
pnum == pnum) {
384 int *pebs,
int pool_size,
unsigned long long *max_sqnum,
402 dbg_bld(
"scanning fastmap pool: size = %i", pool_size);
408 for (i = 0; i < pool_size; i++) {
414 ubi_err(
"bad PEB in fastmap pool!");
421 ubi_err(
"unable to read EC header! PEB:%i err:%i",
429 ubi_err(
"bad image seq: 0x%x, expected: 0x%x",
439 dbg_bld(
"Adding PEB to free: %i", pnum);
441 add_aeb(ai, free, pnum, ec, 1);
443 add_aeb(ai, free, pnum, ec, 0);
446 dbg_bld(
"Found non empty PEB:%i in pool", pnum);
453 if (tmp_aeb->
pnum == pnum) {
477 if (*max_sqnum < new_aeb->
sqnum)
478 *max_sqnum = new_aeb->
sqnum;
480 err = process_pool_aeb(ubi, ai, vh, new_aeb);
487 ubi_err(
"fastmap pool PEBs contains damaged PEBs!");
495 ubi_free_vid_hdr(ubi, vh);
547 int ret,
i,
j, pool_size, wl_pool_size;
548 size_t fm_pos = 0, fm_size = ubi->fm_size;
549 unsigned long long max_sqnum = 0;
550 void *fm_raw = ubi->fm_buf;
552 INIT_LIST_HEAD(&
used);
553 INIT_LIST_HEAD(&free);
554 INIT_LIST_HEAD(&eba_orphans);
555 INIT_LIST_HEAD(&ai->corr);
556 INIT_LIST_HEAD(&ai->free);
557 INIT_LIST_HEAD(&ai->erase);
558 INIT_LIST_HEAD(&ai->alien);
565 if (!ai->aeb_slab_cache) {
571 ai->max_sqnum = fmsb->
sqnum;
573 if (fm_pos >= fm_size)
576 fmhdr = (
struct ubi_fm_hdr *)(fm_raw + fm_pos);
577 fm_pos +=
sizeof(*fmhdr);
578 if (fm_pos >= fm_size)
582 ubi_err(
"bad fastmap header magic: 0x%x, expected: 0x%x",
588 fm_pos +=
sizeof(*fmpl1);
589 if (fm_pos >= fm_size)
592 ubi_err(
"bad fastmap pool magic: 0x%x, expected: 0x%x",
598 fm_pos +=
sizeof(*fmpl2);
599 if (fm_pos >= fm_size)
602 ubi_err(
"bad fastmap pool magic: 0x%x, expected: 0x%x",
613 ubi_err(
"bad pool size: %i", pool_size);
618 ubi_err(
"bad WL pool size: %i", wl_pool_size);
624 fm->max_pool_size < 0) {
625 ubi_err(
"bad maximal pool size: %i", fm->max_pool_size);
630 fm->max_wl_pool_size < 0) {
631 ubi_err(
"bad maximal WL pool size: %i", fm->max_wl_pool_size);
637 fmec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
638 fm_pos +=
sizeof(*fmec);
639 if (fm_pos >= fm_size)
648 fmec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
649 fm_pos +=
sizeof(*fmec);
650 if (fm_pos >= fm_size)
659 fmec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
660 fm_pos +=
sizeof(*fmec);
661 if (fm_pos >= fm_size)
670 fmec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
671 fm_pos +=
sizeof(*fmec);
672 if (fm_pos >= fm_size)
679 ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count);
685 fm_pos +=
sizeof(*fmvhdr);
686 if (fm_pos >= fm_size)
690 ubi_err(
"bad fastmap vol header magic: 0x%x, " \
709 fm_eba = (
struct ubi_fm_eba *)(fm_raw + fm_pos);
710 fm_pos +=
sizeof(*fm_eba);
712 if (fm_pos >= fm_size)
716 ubi_err(
"bad fastmap EBA header magic: 0x%x, " \
730 if (tmp_aeb->
pnum == pnum)
763 assign_aeb_to_av(ai, aeb, av);
765 dbg_bld(
"inserting PEB:%i (LEB %i) to vol %i",
769 ech = kzalloc(ubi->ec_hdr_alsize,
GFP_KERNEL);
780 ubi_err(
"bad PEB in fastmap EBA orphan list");
788 ubi_err(
"unable to read EC header! PEB:%i " \
789 "err:%i", tmp_aeb->
pnum, err);
798 assign_aeb_to_av(ai, tmp_aeb, av);
804 ret = scan_pool(ubi, ai, fmpl1->
pebs, pool_size, &max_sqnum,
805 &eba_orphans, &free);
809 ret = scan_pool(ubi, ai, fmpl2->
pebs, wl_pool_size, &max_sqnum,
810 &eba_orphans, &free);
814 if (max_sqnum > ai->max_sqnum)
815 ai->max_sqnum = max_sqnum;
828 if (
WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count -
829 ai->bad_peb_count - fm->used_blocks))
860 unsigned long long sqnum = 0;
885 ubi_err(
"bad super block magic: 0x%x, expected: 0x%x",
892 ubi_err(
"bad fastmap version: %i, expected: %i",
900 ubi_err(
"number of fastmap blocks is invalid: %i", used_blocks);
907 ubi_err(
"bad fastmap size: %zi, expected: %zi", fm_size,
935 ubi_err(
"unable to read fastmap block# %i EC (PEB: %i)",
953 ubi_err(
"unable to read fastmap block# %i (PEB: %i)",
960 ubi_err(
"bad fastmap anchor vol_id: 0x%x," \
969 ubi_err(
"bad fastmap data vol_id: 0x%x," \
984 ubi_err(
"unable to read fastmap block# %i (PEB: %i, " \
985 "err: %i)", i, pnum, ret);
997 if (crc != tmp_crc) {
998 ubi_err(
"fastmap data CRC is invalid");
999 ubi_err(
"CRC should be: 0x%x, calc: 0x%x", tmp_crc, crc);
1008 ret = ubi_attach_fastmap(ubi, ai, fm);
1033 ubi->
fm_pool.max_size = ubi->
fm->max_pool_size;
1035 ubi_msg(
"attached by fastmap");
1040 ubi_free_vid_hdr(ubi, vh);
1045 ubi_err(
"Attach by fastmap failed, doing a full scan!");
1049 ubi_free_vid_hdr(ubi, vh);
1064 static int ubi_write_fastmap(
struct ubi_device *ubi,
1080 int ret,
i,
j, free_peb_count, used_peb_count, vol_count;
1081 int scrub_peb_count, erase_peb_count;
1102 fm_pos +=
sizeof(*fmsb);
1105 fmh = (
struct ubi_fm_hdr *)(fm_raw + fm_pos);
1106 fm_pos +=
sizeof(*fmh);
1118 scrub_peb_count = 0;
1119 erase_peb_count = 0;
1123 fm_pos +=
sizeof(*fmpl1);
1128 for (i = 0; i < ubi->
fm_pool.size; i++)
1132 fm_pos +=
sizeof(*fmpl2);
1142 fec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
1148 fm_pos +=
sizeof(*fec);
1155 fec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
1161 fm_pos +=
sizeof(*fec);
1168 fec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
1174 fm_pos +=
sizeof(*fec);
1185 fec = (
struct ubi_fm_ec *)(fm_raw + fm_pos);
1191 fm_pos +=
sizeof(*fec);
1206 fm_pos +=
sizeof(*fvh);
1219 feba = (
struct ubi_fm_eba *)(fm_raw + fm_pos);
1238 dbg_bld(
"writing fastmap SB to PEB %i", new_fm->
e[0]->pnum);
1241 ubi_err(
"unable to write vid_hdr to fastmap SB!");
1257 dbg_bld(
"writing fastmap data to PEB %i sqnum %llu",
1261 ubi_err(
"unable to write vid_hdr to PEB %i!",
1262 new_fm->
e[i]->pnum);
1271 ubi_err(
"unable to write fastmap to PEB %i!",
1272 new_fm->
e[i]->pnum);
1283 ubi_free_vid_hdr(ubi, avhdr);
1284 ubi_free_vid_hdr(ubi, dvhdr);
1296 static int erase_block(
struct ubi_device *ubi,
int pnum)
1343 static int invalidate_fastmap(
struct ubi_device *ubi,
1349 ret = erase_block(ubi, fm->
e[0]->pnum);
1397 new_fm = kzalloc(
sizeof(*new_fm),
GFP_KERNEL);
1407 if (!new_fm->
e[i]) {
1431 if (!tmp_e && !old_fm) {
1433 ubi_err(
"could not get any free erase block");
1435 for (j = 1; j <
i; j++)
1440 }
else if (!tmp_e && old_fm) {
1441 ret = erase_block(ubi, old_fm->
e[i]->pnum);
1445 for (j = 1; j <
i; j++)
1449 ubi_err(
"could not erase old fastmap PEB");
1453 new_fm->
e[
i]->pnum = old_fm->
e[
i]->pnum;
1454 new_fm->
e[
i]->ec = old_fm->
e[
i]->ec;
1456 new_fm->
e[
i]->pnum = tmp_e->
pnum;
1457 new_fm->
e[
i]->ec = tmp_e->
ec;
1472 ret = erase_block(ubi, old_fm->
e[0]->pnum);
1475 ubi_err(
"could not erase old anchor PEB");
1483 new_fm->
e[0]->pnum = old_fm->
e[0]->pnum;
1484 new_fm->
e[0]->ec =
ret;
1490 new_fm->
e[0]->pnum = tmp_e->
pnum;
1491 new_fm->
e[0]->ec = tmp_e->
ec;
1496 ubi_err(
"could not find any anchor PEB");
1505 new_fm->
e[0]->pnum = tmp_e->
pnum;
1506 new_fm->
e[0]->ec = tmp_e->
ec;
1511 ret = ubi_write_fastmap(ubi, new_fm);
1526 ubi_warn(
"Unable to write new fastmap, err=%i", ret);
1530 ret = invalidate_fastmap(ubi, old_fm);
1532 ubi_err(
"Unable to invalidiate current fastmap!");