Lines Matching refs:ubi
125 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
126 static int self_check_in_wl_tree(const struct ubi_device *ubi,
128 static int self_check_in_pq(const struct ubi_device *ubi,
175 static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e) in wl_entry_destroy() argument
177 ubi->lookuptbl[e->pnum] = NULL; in wl_entry_destroy()
188 static int do_work(struct ubi_device *ubi) in do_work() argument
201 down_read(&ubi->work_sem); in do_work()
202 spin_lock(&ubi->wl_lock); in do_work()
203 if (list_empty(&ubi->works)) { in do_work()
204 spin_unlock(&ubi->wl_lock); in do_work()
205 up_read(&ubi->work_sem); in do_work()
209 wrk = list_entry(ubi->works.next, struct ubi_work, list); in do_work()
211 ubi->works_count -= 1; in do_work()
212 ubi_assert(ubi->works_count >= 0); in do_work()
213 spin_unlock(&ubi->wl_lock); in do_work()
220 err = wrk->func(ubi, wrk, 0); in do_work()
222 ubi_err(ubi, "work failed with error code %d", err); in do_work()
223 up_read(&ubi->work_sem); in do_work()
275 static inline int in_pq(const struct ubi_device *ubi, struct ubi_wl_entry *e) in in_pq() argument
281 list_for_each_entry(p, &ubi->pq[i], u.list) in in_pq()
298 static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e) in prot_queue_add() argument
300 int pq_tail = ubi->pq_head - 1; in prot_queue_add()
305 list_add_tail(&e->u.list, &ubi->pq[pq_tail]); in prot_queue_add()
318 static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi, in find_wl_entry() argument
353 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi, in find_mean_wl_entry() argument
367 e = may_reserve_for_fm(ubi, e, root); in find_mean_wl_entry()
369 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2); in find_mean_wl_entry()
382 static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi) in wl_get_wle() argument
386 e = find_mean_wl_entry(ubi, &ubi->free); in wl_get_wle()
388 ubi_err(ubi, "no free eraseblocks"); in wl_get_wle()
392 self_check_in_wl_tree(ubi, e, &ubi->free); in wl_get_wle()
398 rb_erase(&e->u.rb, &ubi->free); in wl_get_wle()
399 ubi->free_count--; in wl_get_wle()
413 static int prot_queue_del(struct ubi_device *ubi, int pnum) in prot_queue_del() argument
417 e = ubi->lookuptbl[pnum]; in prot_queue_del()
421 if (self_check_in_pq(ubi, e)) in prot_queue_del()
438 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in sync_erase() argument
447 err = self_check_ec(ubi, e->pnum, e->ec); in sync_erase()
451 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in sync_erase()
455 err = ubi_io_sync_erase(ubi, e->pnum, torture); in sync_erase()
465 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu", in sync_erase()
475 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); in sync_erase()
480 spin_lock(&ubi->wl_lock); in sync_erase()
481 if (e->ec > ubi->max_ec) in sync_erase()
482 ubi->max_ec = e->ec; in sync_erase()
483 spin_unlock(&ubi->wl_lock); in sync_erase()
498 static void serve_prot_queue(struct ubi_device *ubi) in serve_prot_queue() argument
509 spin_lock(&ubi->wl_lock); in serve_prot_queue()
510 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) { in serve_prot_queue()
515 wl_tree_add(e, &ubi->used); in serve_prot_queue()
521 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
527 ubi->pq_head += 1; in serve_prot_queue()
528 if (ubi->pq_head == UBI_PROT_QUEUE_LEN) in serve_prot_queue()
529 ubi->pq_head = 0; in serve_prot_queue()
530 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN); in serve_prot_queue()
531 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
542 static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) in __schedule_ubi_work() argument
544 spin_lock(&ubi->wl_lock); in __schedule_ubi_work()
545 list_add_tail(&wrk->list, &ubi->works); in __schedule_ubi_work()
546 ubi_assert(ubi->works_count >= 0); in __schedule_ubi_work()
547 ubi->works_count += 1; in __schedule_ubi_work()
548 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi)) in __schedule_ubi_work()
549 wake_up_process(ubi->bgt_thread); in __schedule_ubi_work()
550 spin_unlock(&ubi->wl_lock); in __schedule_ubi_work()
561 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) in schedule_ubi_work() argument
563 down_read(&ubi->work_sem); in schedule_ubi_work()
564 __schedule_ubi_work(ubi, wrk); in schedule_ubi_work()
565 up_read(&ubi->work_sem); in schedule_ubi_work()
568 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
583 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in schedule_erase() argument
604 __schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
606 schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
610 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
620 static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in do_sync_erase() argument
632 return __erase_worker(ubi, &wl_wrk); in do_sync_erase()
635 static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
647 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, argument
661 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
667 down_read(&ubi->fm_eba_sem);
668 mutex_lock(&ubi->move_mutex);
669 spin_lock(&ubi->wl_lock);
670 ubi_assert(!ubi->move_from && !ubi->move_to);
671 ubi_assert(!ubi->move_to_put);
674 if (!next_peb_for_wl(ubi) ||
676 if (!ubi->free.rb_node ||
678 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
690 !ubi->free.rb_node, !ubi->used.rb_node);
695 e1 = find_anchor_wl_entry(&ubi->used);
696 if (e1 && ubi->fm_anchor &&
697 (ubi->fm_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) {
698 ubi->fm_do_produce_anchor = 1;
704 wl_tree_add(ubi->fm_anchor, &ubi->free);
705 ubi->fm_anchor = NULL;
706 ubi->free_count++;
709 if (ubi->fm_do_produce_anchor) {
712 e2 = get_peb_for_wl(ubi);
716 self_check_in_wl_tree(ubi, e1, &ubi->used);
717 rb_erase(&e1->u.rb, &ubi->used);
719 ubi->fm_do_produce_anchor = 0;
720 } else if (!ubi->scrub.rb_node) {
722 if (!ubi->scrub.rb_node) {
729 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
730 e2 = get_peb_for_wl(ubi);
739 wl_tree_add(e2, &ubi->free);
740 ubi->free_count++;
743 self_check_in_wl_tree(ubi, e1, &ubi->used);
744 rb_erase(&e1->u.rb, &ubi->used);
750 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
751 e2 = get_peb_for_wl(ubi);
755 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
756 rb_erase(&e1->u.rb, &ubi->scrub);
760 ubi->move_from = e1;
761 ubi->move_to = e2;
762 spin_unlock(&ubi->wl_lock);
775 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0);
802 } else if (ubi->fast_attach && err == UBI_IO_BAD_HDR_EBADMSG) {
814 ubi_err(ubi, "error %d while reading VID header from PEB %d",
822 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb);
860 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
861 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
862 ubi->erroneous_peb_count);
878 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
882 spin_lock(&ubi->wl_lock);
883 if (!ubi->move_to_put) {
884 wl_tree_add(e2, &ubi->used);
887 ubi->move_from = ubi->move_to = NULL;
888 ubi->move_to_put = ubi->wl_scheduled = 0;
889 spin_unlock(&ubi->wl_lock);
891 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
894 wl_entry_destroy(ubi, e2);
905 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
911 mutex_unlock(&ubi->move_mutex);
912 up_read(&ubi->fm_eba_sem);
927 spin_lock(&ubi->wl_lock);
929 prot_queue_add(ubi, e1);
931 wl_tree_add(e1, &ubi->erroneous);
932 ubi->erroneous_peb_count += 1;
934 wl_tree_add(e1, &ubi->scrub);
936 wl_tree_add(e1, &ubi->used);
938 wl_tree_add(e2, &ubi->free);
939 ubi->free_count++;
942 ubi_assert(!ubi->move_to_put);
943 ubi->move_from = ubi->move_to = NULL;
944 ubi->wl_scheduled = 0;
945 spin_unlock(&ubi->wl_lock);
949 ensure_wear_leveling(ubi, 1);
951 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
957 err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
962 mutex_unlock(&ubi->move_mutex);
963 up_read(&ubi->fm_eba_sem);
968 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
971 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
973 spin_lock(&ubi->wl_lock);
974 ubi->move_from = ubi->move_to = NULL;
975 ubi->move_to_put = ubi->wl_scheduled = 0;
976 spin_unlock(&ubi->wl_lock);
979 wl_entry_destroy(ubi, e1);
980 wl_entry_destroy(ubi, e2);
983 ubi_ro_mode(ubi);
984 mutex_unlock(&ubi->move_mutex);
985 up_read(&ubi->fm_eba_sem);
990 ubi->wl_scheduled = 0;
991 spin_unlock(&ubi->wl_lock);
992 mutex_unlock(&ubi->move_mutex);
993 up_read(&ubi->fm_eba_sem);
1007 static int ensure_wear_leveling(struct ubi_device *ubi, int nested) argument
1012 spin_lock(&ubi->wl_lock);
1013 if (ubi->wl_scheduled)
1021 if (!ubi->scrub.rb_node) {
1023 if (!need_wear_leveling(ubi))
1029 if (!ubi->used.rb_node || !ubi->free.rb_node)
1039 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1040 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1049 ubi->wl_scheduled = 1;
1050 spin_unlock(&ubi->wl_lock);
1060 __schedule_ubi_work(ubi, wrk);
1062 schedule_ubi_work(ubi, wrk);
1066 spin_lock(&ubi->wl_lock);
1067 ubi->wl_scheduled = 0;
1069 spin_unlock(&ubi->wl_lock);
1083 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) argument
1094 err = sync_erase(ubi, e, wl_wrk->torture);
1096 spin_lock(&ubi->wl_lock);
1098 if (!ubi->fm_disabled && !ubi->fm_anchor &&
1104 ubi->fm_anchor = e;
1105 ubi->fm_do_produce_anchor = 0;
1107 wl_tree_add(e, &ubi->free);
1108 ubi->free_count++;
1111 spin_unlock(&ubi->wl_lock);
1117 serve_prot_queue(ubi);
1120 err = ensure_wear_leveling(ubi, 1);
1124 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
1131 err1 = schedule_erase(ubi, e, vol_id, lnum, 0, false);
1133 wl_entry_destroy(ubi, e);
1140 wl_entry_destroy(ubi, e);
1151 if (!ubi->bad_allowed) {
1152 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
1156 spin_lock(&ubi->volumes_lock);
1157 if (ubi->beb_rsvd_pebs == 0) {
1158 if (ubi->avail_pebs == 0) {
1159 spin_unlock(&ubi->volumes_lock);
1160 ubi_err(ubi, "no reserved/available physical eraseblocks");
1163 ubi->avail_pebs -= 1;
1166 spin_unlock(&ubi->volumes_lock);
1168 ubi_msg(ubi, "mark PEB %d as bad", pnum);
1169 err = ubi_io_mark_bad(ubi, pnum);
1173 spin_lock(&ubi->volumes_lock);
1174 if (ubi->beb_rsvd_pebs > 0) {
1180 ubi->avail_pebs += 1;
1183 ubi->beb_rsvd_pebs -= 1;
1185 ubi->bad_peb_count += 1;
1186 ubi->good_peb_count -= 1;
1187 ubi_calculate_reserved(ubi);
1189 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
1190 else if (ubi->beb_rsvd_pebs)
1191 ubi_msg(ubi, "%d PEBs left in the reserve",
1192 ubi->beb_rsvd_pebs);
1194 ubi_warn(ubi, "last PEB from the reserve was used");
1195 spin_unlock(&ubi->volumes_lock);
1201 spin_lock(&ubi->volumes_lock);
1202 ubi->avail_pebs += 1;
1203 spin_unlock(&ubi->volumes_lock);
1205 ubi_ro_mode(ubi);
1209 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, argument
1219 wl_entry_destroy(ubi, e);
1223 ret = __erase_worker(ubi, wl_wrk);
1241 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, argument
1249 ubi_assert(pnum < ubi->peb_count);
1251 down_read(&ubi->fm_protect);
1254 spin_lock(&ubi->wl_lock);
1255 e = ubi->lookuptbl[pnum];
1256 if (e == ubi->move_from) {
1263 spin_unlock(&ubi->wl_lock);
1266 mutex_lock(&ubi->move_mutex);
1267 mutex_unlock(&ubi->move_mutex);
1269 } else if (e == ubi->move_to) {
1280 ubi_assert(!ubi->move_to_put);
1281 ubi->move_to_put = 1;
1282 spin_unlock(&ubi->wl_lock);
1283 up_read(&ubi->fm_protect);
1286 if (in_wl_tree(e, &ubi->used)) {
1287 self_check_in_wl_tree(ubi, e, &ubi->used);
1288 rb_erase(&e->u.rb, &ubi->used);
1289 } else if (in_wl_tree(e, &ubi->scrub)) {
1290 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1291 rb_erase(&e->u.rb, &ubi->scrub);
1292 } else if (in_wl_tree(e, &ubi->erroneous)) {
1293 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1294 rb_erase(&e->u.rb, &ubi->erroneous);
1295 ubi->erroneous_peb_count -= 1;
1296 ubi_assert(ubi->erroneous_peb_count >= 0);
1300 err = prot_queue_del(ubi, e->pnum);
1302 ubi_err(ubi, "PEB %d not found", pnum);
1303 ubi_ro_mode(ubi);
1304 spin_unlock(&ubi->wl_lock);
1305 up_read(&ubi->fm_protect);
1310 spin_unlock(&ubi->wl_lock);
1312 err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
1314 spin_lock(&ubi->wl_lock);
1315 wl_tree_add(e, &ubi->used);
1316 spin_unlock(&ubi->wl_lock);
1319 up_read(&ubi->fm_protect);
1333 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) argument
1337 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
1340 spin_lock(&ubi->wl_lock);
1341 e = ubi->lookuptbl[pnum];
1342 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1343 in_wl_tree(e, &ubi->erroneous)) {
1344 spin_unlock(&ubi->wl_lock);
1348 if (e == ubi->move_to) {
1355 spin_unlock(&ubi->wl_lock);
1361 if (in_wl_tree(e, &ubi->used)) {
1362 self_check_in_wl_tree(ubi, e, &ubi->used);
1363 rb_erase(&e->u.rb, &ubi->used);
1367 err = prot_queue_del(ubi, e->pnum);
1369 ubi_err(ubi, "PEB %d not found", pnum);
1370 ubi_ro_mode(ubi);
1371 spin_unlock(&ubi->wl_lock);
1376 wl_tree_add(e, &ubi->scrub);
1377 spin_unlock(&ubi->wl_lock);
1383 return ensure_wear_leveling(ubi, 0);
1398 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) argument
1408 vol_id, lnum, ubi->works_count);
1414 down_read(&ubi->work_sem);
1415 spin_lock(&ubi->wl_lock);
1416 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
1420 ubi->works_count -= 1;
1421 ubi_assert(ubi->works_count >= 0);
1422 spin_unlock(&ubi->wl_lock);
1424 err = wrk->func(ubi, wrk, 0);
1426 up_read(&ubi->work_sem);
1430 spin_lock(&ubi->wl_lock);
1435 spin_unlock(&ubi->wl_lock);
1436 up_read(&ubi->work_sem);
1443 down_write(&ubi->work_sem);
1444 up_write(&ubi->work_sem);
1449 static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e) argument
1451 if (in_wl_tree(e, &ubi->scrub))
1453 else if (in_wl_tree(e, &ubi->erroneous))
1455 else if (ubi->move_from == e)
1457 else if (ubi->move_to == e)
1482 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force) argument
1487 if (pnum < 0 || pnum >= ubi->peb_count) {
1496 down_write(&ubi->work_sem);
1502 spin_lock(&ubi->wl_lock);
1503 e = ubi->lookuptbl[pnum];
1505 spin_unlock(&ubi->wl_lock);
1513 if (!scrub_possible(ubi, e)) {
1514 spin_unlock(&ubi->wl_lock);
1518 spin_unlock(&ubi->wl_lock);
1521 mutex_lock(&ubi->buf_mutex);
1522 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
1523 mutex_unlock(&ubi->buf_mutex);
1530 spin_lock(&ubi->wl_lock);
1536 e = ubi->lookuptbl[pnum];
1538 spin_unlock(&ubi->wl_lock);
1546 if (!scrub_possible(ubi, e)) {
1547 spin_unlock(&ubi->wl_lock);
1552 if (in_pq(ubi, e)) {
1553 prot_queue_del(ubi, e->pnum);
1554 wl_tree_add(e, &ubi->scrub);
1555 spin_unlock(&ubi->wl_lock);
1557 err = ensure_wear_leveling(ubi, 1);
1558 } else if (in_wl_tree(e, &ubi->used)) {
1559 rb_erase(&e->u.rb, &ubi->used);
1560 wl_tree_add(e, &ubi->scrub);
1561 spin_unlock(&ubi->wl_lock);
1563 err = ensure_wear_leveling(ubi, 1);
1564 } else if (in_wl_tree(e, &ubi->free)) {
1565 rb_erase(&e->u.rb, &ubi->free);
1566 ubi->free_count--;
1567 spin_unlock(&ubi->wl_lock);
1573 err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
1576 spin_unlock(&ubi->wl_lock);
1587 up_write(&ubi->work_sem);
1598 static void tree_destroy(struct ubi_device *ubi, struct rb_root *root) argument
1620 wl_entry_destroy(ubi, e);
1632 struct ubi_device *ubi = u; local
1634 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
1635 ubi->bgt_name, task_pid_nr(current));
1647 spin_lock(&ubi->wl_lock);
1648 if (list_empty(&ubi->works) || ubi->ro_mode ||
1649 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1651 spin_unlock(&ubi->wl_lock);
1668 spin_unlock(&ubi->wl_lock);
1670 err = do_work(ubi);
1672 ubi_err(ubi, "%s: work failed with error code %d",
1673 ubi->bgt_name, err);
1679 ubi_msg(ubi, "%s: %d consecutive failures",
1680 ubi->bgt_name, WL_MAX_FAILURES);
1681 ubi_ro_mode(ubi);
1682 ubi->thread_enabled = 0;
1691 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1692 ubi->thread_enabled = 0;
1700 static void shutdown_work(struct ubi_device *ubi) argument
1702 while (!list_empty(&ubi->works)) {
1705 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1707 wrk->func(ubi, wrk, 1);
1708 ubi->works_count -= 1;
1709 ubi_assert(ubi->works_count >= 0);
1719 static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) argument
1730 ubi->lookuptbl[e->pnum] = e;
1733 err = sync_erase(ubi, e, false);
1737 wl_tree_add(e, &ubi->free);
1738 ubi->free_count++;
1740 err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
1748 wl_entry_destroy(ubi, e);
1761 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) argument
1769 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1770 spin_lock_init(&ubi->wl_lock);
1771 mutex_init(&ubi->move_mutex);
1772 init_rwsem(&ubi->work_sem);
1773 ubi->max_ec = ai->max_ec;
1774 INIT_LIST_HEAD(&ubi->works);
1776 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1779 ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
1780 if (!ubi->lookuptbl)
1784 INIT_LIST_HEAD(&ubi->pq[i]);
1785 ubi->pq_head = 0;
1787 ubi->free_count = 0;
1791 err = erase_aeb(ubi, aeb, false);
1811 wl_tree_add(e, &ubi->free);
1812 ubi->free_count++;
1814 ubi->lookuptbl[e->pnum] = e;
1831 ubi->lookuptbl[e->pnum] = e;
1836 wl_tree_add(e, &ubi->used);
1840 wl_tree_add(e, &ubi->scrub);
1850 e = ubi_find_fm_block(ubi, aeb->pnum);
1853 ubi_assert(!ubi->lookuptbl[e->pnum]);
1854 ubi->lookuptbl[e->pnum] = e;
1864 if (ubi->lookuptbl[aeb->pnum])
1879 err = erase_aeb(ubi, aeb, sync);
1889 ubi_assert(ubi->good_peb_count == found_pebs);
1892 ubi_fastmap_init(ubi, &reserved_pebs);
1894 if (ubi->avail_pebs < reserved_pebs) {
1895 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1896 ubi->avail_pebs, reserved_pebs);
1897 if (ubi->corr_peb_count)
1898 ubi_err(ubi, "%d PEBs are corrupted and not used",
1899 ubi->corr_peb_count);
1903 ubi->avail_pebs -= reserved_pebs;
1904 ubi->rsvd_pebs += reserved_pebs;
1907 err = ensure_wear_leveling(ubi, 0);
1912 if (!ubi->ro_mode && !ubi->fm_disabled)
1913 ubi_ensure_anchor_pebs(ubi);
1918 shutdown_work(ubi);
1919 tree_destroy(ubi, &ubi->used);
1920 tree_destroy(ubi, &ubi->free);
1921 tree_destroy(ubi, &ubi->scrub);
1922 kfree(ubi->lookuptbl);
1930 static void protection_queue_destroy(struct ubi_device *ubi) argument
1936 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1938 wl_entry_destroy(ubi, e);
1947 void ubi_wl_close(struct ubi_device *ubi) argument
1950 ubi_fastmap_close(ubi);
1951 shutdown_work(ubi);
1952 protection_queue_destroy(ubi);
1953 tree_destroy(ubi, &ubi->used);
1954 tree_destroy(ubi, &ubi->erroneous);
1955 tree_destroy(ubi, &ubi->free);
1956 tree_destroy(ubi, &ubi->scrub);
1957 kfree(ubi->lookuptbl);
1970 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec) argument
1976 if (!ubi_dbg_chk_gen(ubi))
1979 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1983 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1992 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1993 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
2013 static int self_check_in_wl_tree(const struct ubi_device *ubi, argument
2016 if (!ubi_dbg_chk_gen(ubi))
2022 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
2036 static int self_check_in_pq(const struct ubi_device *ubi, argument
2039 if (!ubi_dbg_chk_gen(ubi))
2042 if (in_pq(ubi, e))
2045 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
2051 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) argument
2055 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
2056 self_check_in_wl_tree(ubi, e, &ubi->free);
2057 ubi->free_count--;
2058 ubi_assert(ubi->free_count >= 0);
2059 rb_erase(&e->u.rb, &ubi->free);
2073 static int produce_free_peb(struct ubi_device *ubi) argument
2077 while (!ubi->free.rb_node && ubi->works_count) {
2078 spin_unlock(&ubi->wl_lock);
2081 err = do_work(ubi);
2083 spin_lock(&ubi->wl_lock);
2099 int ubi_wl_get_peb(struct ubi_device *ubi) argument
2105 down_read(&ubi->fm_eba_sem);
2106 spin_lock(&ubi->wl_lock);
2107 if (!ubi->free.rb_node) {
2108 if (ubi->works_count == 0) {
2109 ubi_err(ubi, "no free eraseblocks");
2110 ubi_assert(list_empty(&ubi->works));
2111 spin_unlock(&ubi->wl_lock);
2115 err = produce_free_peb(ubi);
2117 spin_unlock(&ubi->wl_lock);
2120 spin_unlock(&ubi->wl_lock);
2121 up_read(&ubi->fm_eba_sem);
2125 e = wl_get_wle(ubi);
2126 prot_queue_add(ubi, e);
2127 spin_unlock(&ubi->wl_lock);
2129 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
2130 ubi->peb_size - ubi->vid_hdr_aloffset);
2132 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);