102 #include <linux/slab.h>
109 #define WL_RESERVED_PEBS 1
117 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
130 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
136 #define WL_MAX_FAILURES 32
138 static int self_check_ec(
struct ubi_device *ubi,
int pnum,
int ec);
139 static int self_check_in_wl_tree(
const struct ubi_device *ubi,
141 static int self_check_in_pq(
const struct ubi_device *ubi,
144 #ifdef CONFIG_MTD_UBI_FASTMAP
149 static void update_fastmap_work_fn(
struct work_struct *wrk)
160 static int ubi_is_fm_block(
struct ubi_device *ubi,
int pnum)
167 for (i = 0; i < ubi->
fm->used_blocks; i++)
168 if (ubi->
fm->e[i]->pnum == pnum)
174 static int ubi_is_fm_block(
struct ubi_device *ubi,
int pnum)
201 else if (e->
ec > e1->
ec)
212 rb_link_node(&e->
u.
rb, parent, p);
238 if (list_empty(&ubi->
works)) {
255 err = wrk->
func(ubi, wrk, 0);
257 ubi_err(
"work failed with error code %d", err);
272 static int produce_free_peb(
struct ubi_device *ubi)
276 while (!ubi->
free.rb_node) {
279 dbg_wl(
"do one work synchronously");
315 else if (e->
ec > e1->
ec)
341 int pq_tail = ubi->
pq_head - 1;
347 dbg_wl(
"added PEB %d EC %d to the protection queue", e->
pnum, e->
ec);
360 struct rb_root *root,
int diff)
413 #ifdef CONFIG_MTD_UBI_FASTMAP
428 #ifdef CONFIG_MTD_UBI_FASTMAP
449 static int anchor_pebs_avalible(
struct rb_root *root)
474 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
478 e = find_anchor_wl_entry(&ubi->free);
480 e = find_mean_wl_entry(ubi, &ubi->free);
485 self_check_in_wl_tree(ubi, e, &ubi->free);
503 static int __wl_get_peb(
struct ubi_device *ubi)
509 if (!ubi->
free.rb_node) {
511 ubi_err(
"no free eraseblocks");
516 err = produce_free_peb(ubi);
522 e = find_mean_wl_entry(ubi, &ubi->
free);
524 ubi_err(
"no free eraseblocks");
528 self_check_in_wl_tree(ubi, e, &ubi->
free);
537 #ifndef CONFIG_MTD_UBI_FASTMAP
541 prot_queue_add(ubi, e);
546 #ifdef CONFIG_MTD_UBI_FASTMAP
552 static void return_unused_pool_pebs(
struct ubi_device *ubi,
558 for (i = pool->
used; i < pool->
size; i++) {
560 wl_tree_add(e, &ubi->
free);
570 static void refill_wl_pool(
struct ubi_device *ubi)
575 return_unused_pool_pebs(ubi, pool);
578 if (!ubi->
free.rb_node ||
583 self_check_in_wl_tree(ubi, e, &ubi->
free);
596 static void refill_wl_user_pool(
struct ubi_device *ubi)
600 return_unused_pool_pebs(ubi, pool);
603 if (!ubi->
free.rb_node ||
607 pool->
pebs[pool->
size] = __wl_get_peb(ubi);
622 refill_wl_user_pool(ubi);
645 prot_queue_add(ubi, ubi->
lookuptbl[ret]);
678 self_check_in_wl_tree(ubi, e, &ubi->
free);
689 peb = __wl_get_peb(ubi);
695 ubi_err(
"new PEB %d does not contain all 0xFF bytes", peb);
711 static int prot_queue_del(
struct ubi_device *ubi,
int pnum)
719 if (self_check_in_pq(ubi, e))
723 dbg_wl(
"deleted PEB %d from the protection queue", e->
pnum);
741 unsigned long long ec = e->
ec;
743 dbg_wl(
"erase PEB %d, old EC %llu", e->
pnum, ec);
745 err = self_check_ec(ubi, e->
pnum, e->
ec);
763 ubi_err(
"erase counter overflow at PEB %d, EC %llu",
769 dbg_wl(
"erased PEB %d, new EC %llu", e->
pnum, ec);
796 static void serve_prot_queue(
struct ubi_device *ubi)
809 dbg_wl(
"PEB %d EC %d protection over, move to used tree",
813 wl_tree_add(e, &ubi->
used);
862 __schedule_ubi_work(ubi, wrk);
869 #ifdef CONFIG_MTD_UBI_FASTMAP
876 return wrk->
func == erase_worker;
892 int vol_id,
int lnum,
int torture)
899 dbg_wl(
"schedule erasure of PEB %d, EC %d, torture %d",
906 wl_wrk->
func = &erase_worker;
912 schedule_ubi_work(ubi, wl_wrk);
926 int vol_id,
int lnum,
int torture)
941 return erase_worker(ubi, wl_wrk, 0);
944 #ifdef CONFIG_MTD_UBI_FASTMAP
956 int lnum,
int torture)
985 return schedule_erase(ubi, e, vol_id, lnum, torture);
1002 int err, scrubbing = 0, torture = 0,
protect = 0, erroneous = 0;
1004 #ifdef CONFIG_MTD_UBI_FASTMAP
1005 int anchor = wrk->
anchor;
1014 vid_hdr = ubi_zalloc_vid_hdr(ubi,
GFP_NOFS);
1023 if (!ubi->
free.rb_node ||
1024 (!ubi->
used.rb_node && !ubi->
scrub.rb_node)) {
1035 dbg_wl(
"cancel WL, a list is empty: free %d, used %d",
1036 !ubi->
free.rb_node, !ubi->
used.rb_node);
1040 #ifdef CONFIG_MTD_UBI_FASTMAP
1043 anchor = !anchor_pebs_avalible(&ubi->
free);
1046 e1 = find_anchor_wl_entry(&ubi->
used);
1049 e2 = get_peb_for_wl(ubi);
1053 self_check_in_wl_tree(ubi, e1, &ubi->
used);
1056 }
else if (!ubi->
scrub.rb_node) {
1058 if (!ubi->
scrub.rb_node) {
1066 e2 = get_peb_for_wl(ubi);
1071 dbg_wl(
"no WL needed: min used EC %d, max free EC %d",
1075 self_check_in_wl_tree(ubi, e1, &ubi->
used);
1077 dbg_wl(
"move PEB %d EC %d to PEB %d EC %d",
1083 e2 = get_peb_for_wl(ubi);
1087 self_check_in_wl_tree(ubi, e1, &ubi->
scrub);
1120 dbg_wl(
"PEB %d has no VID header", e1->
pnum);
1129 dbg_wl(
"PEB %d has no VID header but has bit-flips",
1135 ubi_err(
"error %d while reading VID header from PEB %d",
1179 ubi_err(
"too many erroneous eraseblocks (%d)",
1195 ubi_msg(
"scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
1197 ubi_free_vid_hdr(ubi, vid_hdr);
1201 wl_tree_add(e2, &ubi->
used);
1208 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
1221 dbg_wl(
"PEB %d (LEB %d:%d) was put meanwhile, erase",
1222 e2->
pnum, vol_id, lnum);
1223 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
1241 dbg_wl(
"cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
1242 e1->
pnum, vol_id, lnum, e2->
pnum, err);
1244 dbg_wl(
"cancel moving PEB %d to PEB %d (%d)",
1248 prot_queue_add(ubi, e1);
1249 else if (erroneous) {
1252 }
else if (scrubbing)
1253 wl_tree_add(e1, &ubi->
scrub);
1255 wl_tree_add(e1, &ubi->
used);
1261 ubi_free_vid_hdr(ubi, vid_hdr);
1262 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
1272 ubi_err(
"error %d while moving PEB %d to PEB %d",
1275 ubi_err(
"error %d while moving PEB %d (LEB %d:%d) to PEB %d",
1276 err, e1->
pnum, vol_id, lnum, e2->
pnum);
1282 ubi_free_vid_hdr(ubi, vid_hdr);
1290 return err < 0 ? err : -
EIO;
1296 ubi_free_vid_hdr(ubi, vid_hdr);
1309 static int ensure_wear_leveling(
struct ubi_device *ubi,
int nested)
1325 if (!ubi->
scrub.rb_node) {
1326 if (!ubi->
used.rb_node || !ubi->
free.rb_node)
1341 dbg_wl(
"schedule wear-leveling");
1343 dbg_wl(
"schedule scrubbing");
1355 wrk->
func = &wear_leveling_worker;
1357 __schedule_ubi_work(ubi, wrk);
1359 schedule_ubi_work(ubi, wrk);
1370 #ifdef CONFIG_MTD_UBI_FASTMAP
1396 wrk->
func = &wear_leveling_worker;
1397 schedule_ubi_work(ubi, wrk);
1418 int vol_id = wl_wrk->
vol_id;
1419 int lnum = wl_wrk->
lnum;
1420 int err, available_consumed = 0;
1423 dbg_wl(
"cancel erasure of PEB %d EC %d", pnum, e->
ec);
1429 dbg_wl(
"erase PEB %d EC %d LEB %d:%d",
1434 err = sync_erase(ubi, e, wl_wrk->
torture);
1440 wl_tree_add(e, &ubi->
free);
1448 serve_prot_queue(ubi);
1451 err = ensure_wear_leveling(ubi, 1);
1455 ubi_err(
"failed to erase PEB %d, error %d", pnum, err);
1463 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
1483 ubi_err(
"bad physical eraseblock %d detected", pnum);
1491 ubi_err(
"no reserved/available physical eraseblocks");
1495 available_consumed = 1;
1499 ubi_msg(
"mark PEB %d as bad", pnum);
1506 if (available_consumed) {
1512 available_consumed = 0;
1519 if (available_consumed)
1520 ubi_warn(
"no PEBs in the reserved pool, used an available PEB");
1524 ubi_warn(
"last PEB from the reserve was used");
1530 if (available_consumed) {
1553 int pnum,
int torture)
1571 dbg_wl(
"PEB %d is being moved, wait", pnum);
1578 }
else if (e == ubi->
move_to) {
1588 dbg_wl(
"PEB %d is the target of data moving", pnum);
1594 if (in_wl_tree(e, &ubi->
used)) {
1595 self_check_in_wl_tree(ubi, e, &ubi->
used);
1597 }
else if (in_wl_tree(e, &ubi->
scrub)) {
1598 self_check_in_wl_tree(ubi, e, &ubi->
scrub);
1600 }
else if (in_wl_tree(e, &ubi->
erroneous)) {
1601 self_check_in_wl_tree(ubi, e, &ubi->
erroneous);
1608 err = prot_queue_del(ubi, e->
pnum);
1610 ubi_err(
"PEB %d not found", pnum);
1619 err = schedule_erase(ubi, e, vol_id, lnum, torture);
1622 wl_tree_add(e, &ubi->
used);
1643 ubi_msg(
"schedule PEB %d for scrubbing", pnum);
1662 dbg_wl(
"the PEB %d is not in proper tree, retry", pnum);
1667 if (in_wl_tree(e, &ubi->
used)) {
1668 self_check_in_wl_tree(ubi, e, &ubi->
used);
1673 err = prot_queue_del(ubi, e->
pnum);
1675 ubi_err(
"PEB %d not found", pnum);
1682 wl_tree_add(e, &ubi->
scrub);
1689 return ensure_wear_leveling(ubi, 0);
1713 dbg_wl(
"flush pending work for LEB %d:%d (%d pending works)",
1730 err = wrk->
func(ubi, wrk, 0);
1759 static void tree_destroy(
struct rb_root *root)
1795 ubi_msg(
"background thread \"%s\" started, PID %d",
1805 if (try_to_freeze())
1820 ubi_err(
"%s: work failed with error code %d",
1827 ubi_msg(
"%s: %d consecutive failures",
1847 static void cancel_pending(
struct ubi_device *ubi)
1849 while (!list_empty(&ubi->
works)) {
1854 wrk->
func(ubi, wrk, 1);
1870 int err,
i, reserved_pebs, found_pebs = 0;
1881 INIT_LIST_HEAD(&ubi->
works);
1882 #ifdef CONFIG_MTD_UBI_FASTMAP
1894 INIT_LIST_HEAD(&ubi->
pq[i]);
1908 if (schedule_erase(ubi, e, aeb->
vol_id, aeb->
lnum, 0)) {
1929 wl_tree_add(e, &ubi->
free);
1950 dbg_wl(
"add PEB %d EC %d to the used tree",
1952 wl_tree_add(e, &ubi->
used);
1954 dbg_wl(
"add PEB %d EC %d to the scrub tree",
1956 wl_tree_add(e, &ubi->
scrub);
1963 dbg_wl(
"found %i PEBs", found_pebs);
1967 found_pebs + ubi->
fm->used_blocks);
1972 #ifdef CONFIG_MTD_UBI_FASTMAP
1978 ubi_err(
"no enough physical eraseblocks (%d, need %d)",
1981 ubi_err(
"%d PEBs are corrupted and not used",
1989 err = ensure_wear_leveling(ubi, 0);
1996 cancel_pending(ubi);
1997 tree_destroy(&ubi->
used);
1998 tree_destroy(&ubi->
free);
1999 tree_destroy(&ubi->
scrub);
2008 static void protection_queue_destroy(
struct ubi_device *ubi)
2027 dbg_wl(
"close the WL sub-system");
2028 cancel_pending(ubi);
2029 protection_queue_destroy(ubi);
2030 tree_destroy(&ubi->
used);
2032 tree_destroy(&ubi->
free);
2033 tree_destroy(&ubi->
scrub);
2047 static int self_check_ec(
struct ubi_device *ubi,
int pnum,
int ec)
2053 if (!ubi->
dbg->chk_gen)
2068 if (ec != read_ec && read_ec - ec > 1) {
2069 ubi_err(
"self-check failed for PEB %d", pnum);
2070 ubi_err(
"read EC is %lld, should be %d", read_ec, ec);
2090 static int self_check_in_wl_tree(
const struct ubi_device *ubi,
2093 if (!ubi->
dbg->chk_gen)
2096 if (in_wl_tree(e, root))
2099 ubi_err(
"self-check failed for PEB %d, EC %d, RB-tree %p ",
2113 static int self_check_in_pq(
const struct ubi_device *ubi,
2119 if (!ubi->
dbg->chk_gen)
2127 ubi_err(
"self-check failed for PEB %d, EC %d, Protect queue",