• Home
  • Raw
  • Download

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 spin_lock(&ubi->wl_lock);
895 wl_entry_destroy(ubi, e2);
896 spin_unlock(&ubi->wl_lock);
908 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
914 mutex_unlock(&ubi->move_mutex);
915 up_read(&ubi->fm_eba_sem);
930 spin_lock(&ubi->wl_lock);
932 prot_queue_add(ubi, e1);
934 wl_tree_add(e1, &ubi->erroneous);
935 ubi->erroneous_peb_count += 1;
937 wl_tree_add(e1, &ubi->scrub);
939 wl_tree_add(e1, &ubi->used);
941 wl_tree_add(e2, &ubi->free);
942 ubi->free_count++;
945 ubi_assert(!ubi->move_to_put);
946 ubi->move_from = ubi->move_to = NULL;
947 ubi->wl_scheduled = 0;
948 spin_unlock(&ubi->wl_lock);
952 ensure_wear_leveling(ubi, 1);
954 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
960 err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
965 mutex_unlock(&ubi->move_mutex);
966 up_read(&ubi->fm_eba_sem);
971 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
974 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
976 spin_lock(&ubi->wl_lock);
977 ubi->move_from = ubi->move_to = NULL;
978 ubi->move_to_put = ubi->wl_scheduled = 0;
979 wl_entry_destroy(ubi, e1);
980 wl_entry_destroy(ubi, e2);
981 spin_unlock(&ubi->wl_lock);
986 ubi_ro_mode(ubi);
987 mutex_unlock(&ubi->move_mutex);
988 up_read(&ubi->fm_eba_sem);
993 ubi->wl_scheduled = 0;
994 spin_unlock(&ubi->wl_lock);
995 mutex_unlock(&ubi->move_mutex);
996 up_read(&ubi->fm_eba_sem);
1010 static int ensure_wear_leveling(struct ubi_device *ubi, int nested) argument
1015 spin_lock(&ubi->wl_lock);
1016 if (ubi->wl_scheduled)
1024 if (!ubi->scrub.rb_node) {
1026 if (!need_wear_leveling(ubi))
1032 if (!ubi->used.rb_node || !ubi->free.rb_node)
1042 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1043 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1052 ubi->wl_scheduled = 1;
1053 spin_unlock(&ubi->wl_lock);
1063 __schedule_ubi_work(ubi, wrk);
1065 schedule_ubi_work(ubi, wrk);
1069 spin_lock(&ubi->wl_lock);
1070 ubi->wl_scheduled = 0;
1072 spin_unlock(&ubi->wl_lock);
1086 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) argument
1097 err = sync_erase(ubi, e, wl_wrk->torture);
1099 spin_lock(&ubi->wl_lock);
1101 if (!ubi->fm_disabled && !ubi->fm_anchor &&
1107 ubi->fm_anchor = e;
1108 ubi->fm_do_produce_anchor = 0;
1110 wl_tree_add(e, &ubi->free);
1111 ubi->free_count++;
1114 spin_unlock(&ubi->wl_lock);
1120 serve_prot_queue(ubi);
1123 err = ensure_wear_leveling(ubi, 1);
1127 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
1134 err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
1136 spin_lock(&ubi->wl_lock);
1137 wl_entry_destroy(ubi, e);
1138 spin_unlock(&ubi->wl_lock);
1145 spin_lock(&ubi->wl_lock);
1146 wl_entry_destroy(ubi, e);
1147 spin_unlock(&ubi->wl_lock);
1158 if (!ubi->bad_allowed) {
1159 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
1163 spin_lock(&ubi->volumes_lock);
1164 if (ubi->beb_rsvd_pebs == 0) {
1165 if (ubi->avail_pebs == 0) {
1166 spin_unlock(&ubi->volumes_lock);
1167 ubi_err(ubi, "no reserved/available physical eraseblocks");
1170 ubi->avail_pebs -= 1;
1173 spin_unlock(&ubi->volumes_lock);
1175 ubi_msg(ubi, "mark PEB %d as bad", pnum);
1176 err = ubi_io_mark_bad(ubi, pnum);
1180 spin_lock(&ubi->volumes_lock);
1181 if (ubi->beb_rsvd_pebs > 0) {
1187 ubi->avail_pebs += 1;
1190 ubi->beb_rsvd_pebs -= 1;
1192 ubi->bad_peb_count += 1;
1193 ubi->good_peb_count -= 1;
1194 ubi_calculate_reserved(ubi);
1196 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
1197 else if (ubi->beb_rsvd_pebs)
1198 ubi_msg(ubi, "%d PEBs left in the reserve",
1199 ubi->beb_rsvd_pebs);
1201 ubi_warn(ubi, "last PEB from the reserve was used");
1202 spin_unlock(&ubi->volumes_lock);
1208 spin_lock(&ubi->volumes_lock);
1209 ubi->avail_pebs += 1;
1210 spin_unlock(&ubi->volumes_lock);
1212 ubi_ro_mode(ubi);
1216 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, argument
1226 wl_entry_destroy(ubi, e);
1230 ret = __erase_worker(ubi, wl_wrk);
1248 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, argument
1256 ubi_assert(pnum < ubi->peb_count);
1258 down_read(&ubi->fm_protect);
1261 spin_lock(&ubi->wl_lock);
1262 e = ubi->lookuptbl[pnum];
1271 spin_unlock(&ubi->wl_lock);
1272 up_read(&ubi->fm_protect);
1275 if (e == ubi->move_from) {
1282 spin_unlock(&ubi->wl_lock);
1285 mutex_lock(&ubi->move_mutex);
1286 mutex_unlock(&ubi->move_mutex);
1288 } else if (e == ubi->move_to) {
1299 ubi_assert(!ubi->move_to_put);
1300 ubi->move_to_put = 1;
1301 spin_unlock(&ubi->wl_lock);
1302 up_read(&ubi->fm_protect);
1305 if (in_wl_tree(e, &ubi->used)) {
1306 self_check_in_wl_tree(ubi, e, &ubi->used);
1307 rb_erase(&e->u.rb, &ubi->used);
1308 } else if (in_wl_tree(e, &ubi->scrub)) {
1309 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1310 rb_erase(&e->u.rb, &ubi->scrub);
1311 } else if (in_wl_tree(e, &ubi->erroneous)) {
1312 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1313 rb_erase(&e->u.rb, &ubi->erroneous);
1314 ubi->erroneous_peb_count -= 1;
1315 ubi_assert(ubi->erroneous_peb_count >= 0);
1319 err = prot_queue_del(ubi, e->pnum);
1321 ubi_err(ubi, "PEB %d not found", pnum);
1322 ubi_ro_mode(ubi);
1323 spin_unlock(&ubi->wl_lock);
1324 up_read(&ubi->fm_protect);
1329 spin_unlock(&ubi->wl_lock);
1331 err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
1333 spin_lock(&ubi->wl_lock);
1334 wl_tree_add(e, &ubi->used);
1335 spin_unlock(&ubi->wl_lock);
1338 up_read(&ubi->fm_protect);
1352 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) argument
1356 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
1359 spin_lock(&ubi->wl_lock);
1360 e = ubi->lookuptbl[pnum];
1361 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1362 in_wl_tree(e, &ubi->erroneous)) {
1363 spin_unlock(&ubi->wl_lock);
1367 if (e == ubi->move_to) {
1374 spin_unlock(&ubi->wl_lock);
1380 if (in_wl_tree(e, &ubi->used)) {
1381 self_check_in_wl_tree(ubi, e, &ubi->used);
1382 rb_erase(&e->u.rb, &ubi->used);
1386 err = prot_queue_del(ubi, e->pnum);
1388 ubi_err(ubi, "PEB %d not found", pnum);
1389 ubi_ro_mode(ubi);
1390 spin_unlock(&ubi->wl_lock);
1395 wl_tree_add(e, &ubi->scrub);
1396 spin_unlock(&ubi->wl_lock);
1402 return ensure_wear_leveling(ubi, 0);
1417 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) argument
1427 vol_id, lnum, ubi->works_count);
1433 down_read(&ubi->work_sem);
1434 spin_lock(&ubi->wl_lock);
1435 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
1439 ubi->works_count -= 1;
1440 ubi_assert(ubi->works_count >= 0);
1441 spin_unlock(&ubi->wl_lock);
1443 err = wrk->func(ubi, wrk, 0);
1445 up_read(&ubi->work_sem);
1449 spin_lock(&ubi->wl_lock);
1454 spin_unlock(&ubi->wl_lock);
1455 up_read(&ubi->work_sem);
1462 down_write(&ubi->work_sem);
1463 up_write(&ubi->work_sem);
1468 static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e) argument
1470 if (in_wl_tree(e, &ubi->scrub))
1472 else if (in_wl_tree(e, &ubi->erroneous))
1474 else if (ubi->move_from == e)
1476 else if (ubi->move_to == e)
1501 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force) argument
1506 if (pnum < 0 || pnum >= ubi->peb_count) {
1515 down_write(&ubi->work_sem);
1521 spin_lock(&ubi->wl_lock);
1522 e = ubi->lookuptbl[pnum];
1524 spin_unlock(&ubi->wl_lock);
1532 if (!scrub_possible(ubi, e)) {
1533 spin_unlock(&ubi->wl_lock);
1537 spin_unlock(&ubi->wl_lock);
1540 mutex_lock(&ubi->buf_mutex);
1541 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
1542 mutex_unlock(&ubi->buf_mutex);
1549 spin_lock(&ubi->wl_lock);
1555 e = ubi->lookuptbl[pnum];
1557 spin_unlock(&ubi->wl_lock);
1565 if (!scrub_possible(ubi, e)) {
1566 spin_unlock(&ubi->wl_lock);
1571 if (in_pq(ubi, e)) {
1572 prot_queue_del(ubi, e->pnum);
1573 wl_tree_add(e, &ubi->scrub);
1574 spin_unlock(&ubi->wl_lock);
1576 err = ensure_wear_leveling(ubi, 1);
1577 } else if (in_wl_tree(e, &ubi->used)) {
1578 rb_erase(&e->u.rb, &ubi->used);
1579 wl_tree_add(e, &ubi->scrub);
1580 spin_unlock(&ubi->wl_lock);
1582 err = ensure_wear_leveling(ubi, 1);
1583 } else if (in_wl_tree(e, &ubi->free)) {
1584 rb_erase(&e->u.rb, &ubi->free);
1585 ubi->free_count--;
1586 spin_unlock(&ubi->wl_lock);
1592 err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
1595 spin_unlock(&ubi->wl_lock);
1606 up_write(&ubi->work_sem);
1617 static void tree_destroy(struct ubi_device *ubi, struct rb_root *root) argument
1639 wl_entry_destroy(ubi, e);
1651 struct ubi_device *ubi = u; local
1653 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
1654 ubi->bgt_name, task_pid_nr(current));
1666 spin_lock(&ubi->wl_lock);
1667 if (list_empty(&ubi->works) || ubi->ro_mode ||
1668 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1670 spin_unlock(&ubi->wl_lock);
1687 spin_unlock(&ubi->wl_lock);
1689 err = do_work(ubi);
1691 ubi_err(ubi, "%s: work failed with error code %d",
1692 ubi->bgt_name, err);
1698 ubi_msg(ubi, "%s: %d consecutive failures",
1699 ubi->bgt_name, WL_MAX_FAILURES);
1700 ubi_ro_mode(ubi);
1701 ubi->thread_enabled = 0;
1710 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1711 ubi->thread_enabled = 0;
1719 static void shutdown_work(struct ubi_device *ubi) argument
1721 while (!list_empty(&ubi->works)) {
1724 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1726 wrk->func(ubi, wrk, 1);
1727 ubi->works_count -= 1;
1728 ubi_assert(ubi->works_count >= 0);
1738 static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) argument
1749 ubi->lookuptbl[e->pnum] = e;
1752 err = sync_erase(ubi, e, false);
1756 wl_tree_add(e, &ubi->free);
1757 ubi->free_count++;
1759 err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
1767 wl_entry_destroy(ubi, e);
1780 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) argument
1788 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1789 spin_lock_init(&ubi->wl_lock);
1790 mutex_init(&ubi->move_mutex);
1791 init_rwsem(&ubi->work_sem);
1792 ubi->max_ec = ai->max_ec;
1793 INIT_LIST_HEAD(&ubi->works);
1795 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1798 ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
1799 if (!ubi->lookuptbl)
1803 INIT_LIST_HEAD(&ubi->pq[i]);
1804 ubi->pq_head = 0;
1806 ubi->free_count = 0;
1810 err = erase_aeb(ubi, aeb, false);
1830 wl_tree_add(e, &ubi->free);
1831 ubi->free_count++;
1833 ubi->lookuptbl[e->pnum] = e;
1850 ubi->lookuptbl[e->pnum] = e;
1855 wl_tree_add(e, &ubi->used);
1859 wl_tree_add(e, &ubi->scrub);
1869 e = ubi_find_fm_block(ubi, aeb->pnum);
1872 ubi_assert(!ubi->lookuptbl[e->pnum]);
1873 ubi->lookuptbl[e->pnum] = e;
1883 if (ubi->lookuptbl[aeb->pnum])
1898 err = erase_aeb(ubi, aeb, sync);
1908 ubi_assert(ubi->good_peb_count == found_pebs);
1911 ubi_fastmap_init(ubi, &reserved_pebs);
1913 if (ubi->avail_pebs < reserved_pebs) {
1914 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1915 ubi->avail_pebs, reserved_pebs);
1916 if (ubi->corr_peb_count)
1917 ubi_err(ubi, "%d PEBs are corrupted and not used",
1918 ubi->corr_peb_count);
1922 ubi->avail_pebs -= reserved_pebs;
1923 ubi->rsvd_pebs += reserved_pebs;
1926 err = ensure_wear_leveling(ubi, 0);
1931 if (!ubi->ro_mode && !ubi->fm_disabled)
1932 ubi_ensure_anchor_pebs(ubi);
1937 shutdown_work(ubi);
1938 tree_destroy(ubi, &ubi->used);
1939 tree_destroy(ubi, &ubi->free);
1940 tree_destroy(ubi, &ubi->scrub);
1941 kfree(ubi->lookuptbl);
1949 static void protection_queue_destroy(struct ubi_device *ubi) argument
1955 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1957 wl_entry_destroy(ubi, e);
1966 void ubi_wl_close(struct ubi_device *ubi) argument
1969 ubi_fastmap_close(ubi);
1970 shutdown_work(ubi);
1971 protection_queue_destroy(ubi);
1972 tree_destroy(ubi, &ubi->used);
1973 tree_destroy(ubi, &ubi->erroneous);
1974 tree_destroy(ubi, &ubi->free);
1975 tree_destroy(ubi, &ubi->scrub);
1976 kfree(ubi->lookuptbl);
1989 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec) argument
1995 if (!ubi_dbg_chk_gen(ubi))
1998 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
2002 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
2011 ubi_err(ubi, "self-check failed for PEB %d", pnum);
2012 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
2032 static int self_check_in_wl_tree(const struct ubi_device *ubi, argument
2035 if (!ubi_dbg_chk_gen(ubi))
2041 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
2055 static int self_check_in_pq(const struct ubi_device *ubi, argument
2058 if (!ubi_dbg_chk_gen(ubi))
2061 if (in_pq(ubi, e))
2064 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
2070 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) argument
2074 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
2075 self_check_in_wl_tree(ubi, e, &ubi->free);
2076 ubi->free_count--;
2077 ubi_assert(ubi->free_count >= 0);
2078 rb_erase(&e->u.rb, &ubi->free);
2092 static int produce_free_peb(struct ubi_device *ubi) argument
2096 while (!ubi->free.rb_node && ubi->works_count) {
2097 spin_unlock(&ubi->wl_lock);
2100 err = do_work(ubi);
2102 spin_lock(&ubi->wl_lock);
2118 int ubi_wl_get_peb(struct ubi_device *ubi) argument
2124 down_read(&ubi->fm_eba_sem);
2125 spin_lock(&ubi->wl_lock);
2126 if (!ubi->free.rb_node) {
2127 if (ubi->works_count == 0) {
2128 ubi_err(ubi, "no free eraseblocks");
2129 ubi_assert(list_empty(&ubi->works));
2130 spin_unlock(&ubi->wl_lock);
2134 err = produce_free_peb(ubi);
2136 spin_unlock(&ubi->wl_lock);
2139 spin_unlock(&ubi->wl_lock);
2140 up_read(&ubi->fm_eba_sem);
2144 e = wl_get_wle(ubi);
2145 prot_queue_add(ubi, e);
2146 spin_unlock(&ubi->wl_lock);
2148 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
2149 ubi->peb_size - ubi->vid_hdr_aloffset);
2151 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);