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
345 if (prev_e && !ubi->fm_disabled && in find_wl_entry()
346 !ubi->fm && e->pnum < UBI_FM_MAX_START) in find_wl_entry()
361 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi, in find_mean_wl_entry() argument
375 e = may_reserve_for_fm(ubi, e, root); in find_mean_wl_entry()
377 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2); in find_mean_wl_entry()
390 static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi) in wl_get_wle() argument
394 e = find_mean_wl_entry(ubi, &ubi->free); in wl_get_wle()
396 ubi_err(ubi, "no free eraseblocks"); in wl_get_wle()
400 self_check_in_wl_tree(ubi, e, &ubi->free); in wl_get_wle()
406 rb_erase(&e->u.rb, &ubi->free); in wl_get_wle()
407 ubi->free_count--; in wl_get_wle()
421 static int prot_queue_del(struct ubi_device *ubi, int pnum) in prot_queue_del() argument
425 e = ubi->lookuptbl[pnum]; in prot_queue_del()
429 if (self_check_in_pq(ubi, e)) in prot_queue_del()
446 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in sync_erase() argument
455 err = self_check_ec(ubi, e->pnum, e->ec); in sync_erase()
459 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in sync_erase()
463 err = ubi_io_sync_erase(ubi, e->pnum, torture); in sync_erase()
473 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu", in sync_erase()
483 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); in sync_erase()
488 spin_lock(&ubi->wl_lock); in sync_erase()
489 if (e->ec > ubi->max_ec) in sync_erase()
490 ubi->max_ec = e->ec; in sync_erase()
491 spin_unlock(&ubi->wl_lock); in sync_erase()
506 static void serve_prot_queue(struct ubi_device *ubi) in serve_prot_queue() argument
517 spin_lock(&ubi->wl_lock); in serve_prot_queue()
518 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) { in serve_prot_queue()
523 wl_tree_add(e, &ubi->used); in serve_prot_queue()
529 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
535 ubi->pq_head += 1; in serve_prot_queue()
536 if (ubi->pq_head == UBI_PROT_QUEUE_LEN) in serve_prot_queue()
537 ubi->pq_head = 0; in serve_prot_queue()
538 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN); in serve_prot_queue()
539 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
550 static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) in __schedule_ubi_work() argument
552 spin_lock(&ubi->wl_lock); in __schedule_ubi_work()
553 list_add_tail(&wrk->list, &ubi->works); in __schedule_ubi_work()
554 ubi_assert(ubi->works_count >= 0); in __schedule_ubi_work()
555 ubi->works_count += 1; in __schedule_ubi_work()
556 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi)) in __schedule_ubi_work()
557 wake_up_process(ubi->bgt_thread); in __schedule_ubi_work()
558 spin_unlock(&ubi->wl_lock); in __schedule_ubi_work()
569 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) in schedule_ubi_work() argument
571 down_read(&ubi->work_sem); in schedule_ubi_work()
572 __schedule_ubi_work(ubi, wrk); in schedule_ubi_work()
573 up_read(&ubi->work_sem); in schedule_ubi_work()
576 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
590 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in schedule_erase() argument
611 __schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
613 schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
617 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
627 static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, in do_sync_erase() argument
639 return __erase_worker(ubi, &wl_wrk); in do_sync_erase()
642 static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
654 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, argument
671 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
677 down_read(&ubi->fm_eba_sem);
678 mutex_lock(&ubi->move_mutex);
679 spin_lock(&ubi->wl_lock);
680 ubi_assert(!ubi->move_from && !ubi->move_to);
681 ubi_assert(!ubi->move_to_put);
683 if (!ubi->free.rb_node ||
684 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
696 !ubi->free.rb_node, !ubi->used.rb_node);
703 anchor = !anchor_pebs_available(&ubi->free);
706 e1 = find_anchor_wl_entry(&ubi->used);
709 e2 = get_peb_for_wl(ubi);
719 self_check_in_wl_tree(ubi, e1, &ubi->used);
720 rb_erase(&e1->u.rb, &ubi->used);
722 } else if (!ubi->scrub.rb_node) {
724 if (!ubi->scrub.rb_node) {
731 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
732 e2 = get_peb_for_wl(ubi);
741 wl_tree_add(e2, &ubi->free);
742 ubi->free_count++;
745 self_check_in_wl_tree(ubi, e1, &ubi->used);
746 rb_erase(&e1->u.rb, &ubi->used);
752 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
753 e2 = get_peb_for_wl(ubi);
757 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
758 rb_erase(&e1->u.rb, &ubi->scrub);
762 ubi->move_from = e1;
763 ubi->move_to = e2;
764 spin_unlock(&ubi->wl_lock);
777 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0);
804 } else if (ubi->fast_attach && err == UBI_IO_BAD_HDR_EBADMSG) {
816 ubi_err(ubi, "error %d while reading VID header from PEB %d",
824 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb);
862 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
863 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
864 ubi->erroneous_peb_count);
880 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
884 spin_lock(&ubi->wl_lock);
885 if (!ubi->move_to_put) {
886 wl_tree_add(e2, &ubi->used);
889 ubi->move_from = ubi->move_to = NULL;
890 ubi->move_to_put = ubi->wl_scheduled = 0;
891 spin_unlock(&ubi->wl_lock);
893 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
896 wl_entry_destroy(ubi, e2);
907 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
913 mutex_unlock(&ubi->move_mutex);
914 up_read(&ubi->fm_eba_sem);
929 spin_lock(&ubi->wl_lock);
931 prot_queue_add(ubi, e1);
933 wl_tree_add(e1, &ubi->erroneous);
934 ubi->erroneous_peb_count += 1;
936 wl_tree_add(e1, &ubi->scrub);
938 wl_tree_add(e1, &ubi->used);
940 wl_tree_add(e2, &ubi->free);
941 ubi->free_count++;
944 ubi_assert(!ubi->move_to_put);
945 ubi->move_from = ubi->move_to = NULL;
946 ubi->wl_scheduled = 0;
947 spin_unlock(&ubi->wl_lock);
951 ensure_wear_leveling(ubi, 1);
953 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
959 err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
964 mutex_unlock(&ubi->move_mutex);
965 up_read(&ubi->fm_eba_sem);
970 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
973 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
975 spin_lock(&ubi->wl_lock);
976 ubi->move_from = ubi->move_to = NULL;
977 ubi->move_to_put = ubi->wl_scheduled = 0;
978 spin_unlock(&ubi->wl_lock);
981 wl_entry_destroy(ubi, e1);
982 wl_entry_destroy(ubi, e2);
985 ubi_ro_mode(ubi);
986 mutex_unlock(&ubi->move_mutex);
987 up_read(&ubi->fm_eba_sem);
992 ubi->wl_scheduled = 0;
993 spin_unlock(&ubi->wl_lock);
994 mutex_unlock(&ubi->move_mutex);
995 up_read(&ubi->fm_eba_sem);
1009 static int ensure_wear_leveling(struct ubi_device *ubi, int nested) argument
1016 spin_lock(&ubi->wl_lock);
1017 if (ubi->wl_scheduled)
1025 if (!ubi->scrub.rb_node) {
1026 if (!ubi->used.rb_node || !ubi->free.rb_node)
1036 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1037 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1045 ubi->wl_scheduled = 1;
1046 spin_unlock(&ubi->wl_lock);
1057 __schedule_ubi_work(ubi, wrk);
1059 schedule_ubi_work(ubi, wrk);
1063 spin_lock(&ubi->wl_lock);
1064 ubi->wl_scheduled = 0;
1066 spin_unlock(&ubi->wl_lock);
1082 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) argument
1093 err = sync_erase(ubi, e, wl_wrk->torture);
1095 spin_lock(&ubi->wl_lock);
1096 wl_tree_add(e, &ubi->free);
1097 ubi->free_count++;
1098 spin_unlock(&ubi->wl_lock);
1104 serve_prot_queue(ubi);
1107 err = ensure_wear_leveling(ubi, 1);
1111 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
1118 err1 = schedule_erase(ubi, e, vol_id, lnum, 0, false);
1120 wl_entry_destroy(ubi, e);
1127 wl_entry_destroy(ubi, e);
1138 if (!ubi->bad_allowed) {
1139 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
1143 spin_lock(&ubi->volumes_lock);
1144 if (ubi->beb_rsvd_pebs == 0) {
1145 if (ubi->avail_pebs == 0) {
1146 spin_unlock(&ubi->volumes_lock);
1147 ubi_err(ubi, "no reserved/available physical eraseblocks");
1150 ubi->avail_pebs -= 1;
1153 spin_unlock(&ubi->volumes_lock);
1155 ubi_msg(ubi, "mark PEB %d as bad", pnum);
1156 err = ubi_io_mark_bad(ubi, pnum);
1160 spin_lock(&ubi->volumes_lock);
1161 if (ubi->beb_rsvd_pebs > 0) {
1167 ubi->avail_pebs += 1;
1170 ubi->beb_rsvd_pebs -= 1;
1172 ubi->bad_peb_count += 1;
1173 ubi->good_peb_count -= 1;
1174 ubi_calculate_reserved(ubi);
1176 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
1177 else if (ubi->beb_rsvd_pebs)
1178 ubi_msg(ubi, "%d PEBs left in the reserve",
1179 ubi->beb_rsvd_pebs);
1181 ubi_warn(ubi, "last PEB from the reserve was used");
1182 spin_unlock(&ubi->volumes_lock);
1188 spin_lock(&ubi->volumes_lock);
1189 ubi->avail_pebs += 1;
1190 spin_unlock(&ubi->volumes_lock);
1192 ubi_ro_mode(ubi);
1196 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, argument
1206 wl_entry_destroy(ubi, e);
1210 ret = __erase_worker(ubi, wl_wrk);
1228 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, argument
1236 ubi_assert(pnum < ubi->peb_count);
1238 down_read(&ubi->fm_protect);
1241 spin_lock(&ubi->wl_lock);
1242 e = ubi->lookuptbl[pnum];
1243 if (e == ubi->move_from) {
1250 spin_unlock(&ubi->wl_lock);
1253 mutex_lock(&ubi->move_mutex);
1254 mutex_unlock(&ubi->move_mutex);
1256 } else if (e == ubi->move_to) {
1267 ubi_assert(!ubi->move_to_put);
1268 ubi->move_to_put = 1;
1269 spin_unlock(&ubi->wl_lock);
1270 up_read(&ubi->fm_protect);
1273 if (in_wl_tree(e, &ubi->used)) {
1274 self_check_in_wl_tree(ubi, e, &ubi->used);
1275 rb_erase(&e->u.rb, &ubi->used);
1276 } else if (in_wl_tree(e, &ubi->scrub)) {
1277 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1278 rb_erase(&e->u.rb, &ubi->scrub);
1279 } else if (in_wl_tree(e, &ubi->erroneous)) {
1280 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1281 rb_erase(&e->u.rb, &ubi->erroneous);
1282 ubi->erroneous_peb_count -= 1;
1283 ubi_assert(ubi->erroneous_peb_count >= 0);
1287 err = prot_queue_del(ubi, e->pnum);
1289 ubi_err(ubi, "PEB %d not found", pnum);
1290 ubi_ro_mode(ubi);
1291 spin_unlock(&ubi->wl_lock);
1292 up_read(&ubi->fm_protect);
1297 spin_unlock(&ubi->wl_lock);
1299 err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
1301 spin_lock(&ubi->wl_lock);
1302 wl_tree_add(e, &ubi->used);
1303 spin_unlock(&ubi->wl_lock);
1306 up_read(&ubi->fm_protect);
1320 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) argument
1324 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
1327 spin_lock(&ubi->wl_lock);
1328 e = ubi->lookuptbl[pnum];
1329 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1330 in_wl_tree(e, &ubi->erroneous)) {
1331 spin_unlock(&ubi->wl_lock);
1335 if (e == ubi->move_to) {
1342 spin_unlock(&ubi->wl_lock);
1348 if (in_wl_tree(e, &ubi->used)) {
1349 self_check_in_wl_tree(ubi, e, &ubi->used);
1350 rb_erase(&e->u.rb, &ubi->used);
1354 err = prot_queue_del(ubi, e->pnum);
1356 ubi_err(ubi, "PEB %d not found", pnum);
1357 ubi_ro_mode(ubi);
1358 spin_unlock(&ubi->wl_lock);
1363 wl_tree_add(e, &ubi->scrub);
1364 spin_unlock(&ubi->wl_lock);
1370 return ensure_wear_leveling(ubi, 0);
1385 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) argument
1395 vol_id, lnum, ubi->works_count);
1401 down_read(&ubi->work_sem);
1402 spin_lock(&ubi->wl_lock);
1403 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
1407 ubi->works_count -= 1;
1408 ubi_assert(ubi->works_count >= 0);
1409 spin_unlock(&ubi->wl_lock);
1411 err = wrk->func(ubi, wrk, 0);
1413 up_read(&ubi->work_sem);
1417 spin_lock(&ubi->wl_lock);
1422 spin_unlock(&ubi->wl_lock);
1423 up_read(&ubi->work_sem);
1430 down_write(&ubi->work_sem);
1431 up_write(&ubi->work_sem);
1436 static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e) argument
1438 if (in_wl_tree(e, &ubi->scrub))
1440 else if (in_wl_tree(e, &ubi->erroneous))
1442 else if (ubi->move_from == e)
1444 else if (ubi->move_to == e)
1469 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force) argument
1474 if (pnum < 0 || pnum >= ubi->peb_count) {
1483 down_write(&ubi->work_sem);
1489 spin_lock(&ubi->wl_lock);
1490 e = ubi->lookuptbl[pnum];
1492 spin_unlock(&ubi->wl_lock);
1500 if (!scrub_possible(ubi, e)) {
1501 spin_unlock(&ubi->wl_lock);
1505 spin_unlock(&ubi->wl_lock);
1508 mutex_lock(&ubi->buf_mutex);
1509 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
1510 mutex_unlock(&ubi->buf_mutex);
1517 spin_lock(&ubi->wl_lock);
1523 e = ubi->lookuptbl[pnum];
1525 spin_unlock(&ubi->wl_lock);
1533 if (!scrub_possible(ubi, e)) {
1534 spin_unlock(&ubi->wl_lock);
1539 if (in_pq(ubi, e)) {
1540 prot_queue_del(ubi, e->pnum);
1541 wl_tree_add(e, &ubi->scrub);
1542 spin_unlock(&ubi->wl_lock);
1544 err = ensure_wear_leveling(ubi, 1);
1545 } else if (in_wl_tree(e, &ubi->used)) {
1546 rb_erase(&e->u.rb, &ubi->used);
1547 wl_tree_add(e, &ubi->scrub);
1548 spin_unlock(&ubi->wl_lock);
1550 err = ensure_wear_leveling(ubi, 1);
1551 } else if (in_wl_tree(e, &ubi->free)) {
1552 rb_erase(&e->u.rb, &ubi->free);
1553 ubi->free_count--;
1554 spin_unlock(&ubi->wl_lock);
1560 err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
1563 spin_unlock(&ubi->wl_lock);
1574 up_write(&ubi->work_sem);
1585 static void tree_destroy(struct ubi_device *ubi, struct rb_root *root) argument
1607 wl_entry_destroy(ubi, e);
1619 struct ubi_device *ubi = u; local
1621 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
1622 ubi->bgt_name, task_pid_nr(current));
1634 spin_lock(&ubi->wl_lock);
1635 if (list_empty(&ubi->works) || ubi->ro_mode ||
1636 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1638 spin_unlock(&ubi->wl_lock);
1642 spin_unlock(&ubi->wl_lock);
1644 err = do_work(ubi);
1646 ubi_err(ubi, "%s: work failed with error code %d",
1647 ubi->bgt_name, err);
1653 ubi_msg(ubi, "%s: %d consecutive failures",
1654 ubi->bgt_name, WL_MAX_FAILURES);
1655 ubi_ro_mode(ubi);
1656 ubi->thread_enabled = 0;
1665 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1666 ubi->thread_enabled = 0;
1674 static void shutdown_work(struct ubi_device *ubi) argument
1676 while (!list_empty(&ubi->works)) {
1679 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1681 wrk->func(ubi, wrk, 1);
1682 ubi->works_count -= 1;
1683 ubi_assert(ubi->works_count >= 0);
1693 static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) argument
1704 ubi->lookuptbl[e->pnum] = e;
1707 err = sync_erase(ubi, e, false);
1711 wl_tree_add(e, &ubi->free);
1712 ubi->free_count++;
1714 err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
1722 wl_entry_destroy(ubi, e);
1735 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) argument
1743 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1744 spin_lock_init(&ubi->wl_lock);
1745 mutex_init(&ubi->move_mutex);
1746 init_rwsem(&ubi->work_sem);
1747 ubi->max_ec = ai->max_ec;
1748 INIT_LIST_HEAD(&ubi->works);
1750 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1753 ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
1754 if (!ubi->lookuptbl)
1758 INIT_LIST_HEAD(&ubi->pq[i]);
1759 ubi->pq_head = 0;
1761 ubi->free_count = 0;
1765 err = erase_aeb(ubi, aeb, false);
1785 wl_tree_add(e, &ubi->free);
1786 ubi->free_count++;
1788 ubi->lookuptbl[e->pnum] = e;
1805 ubi->lookuptbl[e->pnum] = e;
1810 wl_tree_add(e, &ubi->used);
1814 wl_tree_add(e, &ubi->scrub);
1824 e = ubi_find_fm_block(ubi, aeb->pnum);
1827 ubi_assert(!ubi->lookuptbl[e->pnum]);
1828 ubi->lookuptbl[e->pnum] = e;
1838 if (ubi->lookuptbl[aeb->pnum])
1853 err = erase_aeb(ubi, aeb, sync);
1863 ubi_assert(ubi->good_peb_count == found_pebs);
1866 ubi_fastmap_init(ubi, &reserved_pebs);
1868 if (ubi->avail_pebs < reserved_pebs) {
1869 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1870 ubi->avail_pebs, reserved_pebs);
1871 if (ubi->corr_peb_count)
1872 ubi_err(ubi, "%d PEBs are corrupted and not used",
1873 ubi->corr_peb_count);
1877 ubi->avail_pebs -= reserved_pebs;
1878 ubi->rsvd_pebs += reserved_pebs;
1881 err = ensure_wear_leveling(ubi, 0);
1888 shutdown_work(ubi);
1889 tree_destroy(ubi, &ubi->used);
1890 tree_destroy(ubi, &ubi->free);
1891 tree_destroy(ubi, &ubi->scrub);
1892 kfree(ubi->lookuptbl);
1900 static void protection_queue_destroy(struct ubi_device *ubi) argument
1906 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1908 wl_entry_destroy(ubi, e);
1917 void ubi_wl_close(struct ubi_device *ubi) argument
1920 ubi_fastmap_close(ubi);
1921 shutdown_work(ubi);
1922 protection_queue_destroy(ubi);
1923 tree_destroy(ubi, &ubi->used);
1924 tree_destroy(ubi, &ubi->erroneous);
1925 tree_destroy(ubi, &ubi->free);
1926 tree_destroy(ubi, &ubi->scrub);
1927 kfree(ubi->lookuptbl);
1940 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec) argument
1946 if (!ubi_dbg_chk_gen(ubi))
1949 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1953 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1962 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1963 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
1983 static int self_check_in_wl_tree(const struct ubi_device *ubi, argument
1986 if (!ubi_dbg_chk_gen(ubi))
1992 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
2006 static int self_check_in_pq(const struct ubi_device *ubi, argument
2009 if (!ubi_dbg_chk_gen(ubi))
2012 if (in_pq(ubi, e))
2015 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
2021 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) argument
2025 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
2026 self_check_in_wl_tree(ubi, e, &ubi->free);
2027 ubi->free_count--;
2028 ubi_assert(ubi->free_count >= 0);
2029 rb_erase(&e->u.rb, &ubi->free);
2043 static int produce_free_peb(struct ubi_device *ubi) argument
2047 while (!ubi->free.rb_node && ubi->works_count) {
2048 spin_unlock(&ubi->wl_lock);
2051 err = do_work(ubi);
2053 spin_lock(&ubi->wl_lock);
2069 int ubi_wl_get_peb(struct ubi_device *ubi) argument
2075 down_read(&ubi->fm_eba_sem);
2076 spin_lock(&ubi->wl_lock);
2077 if (!ubi->free.rb_node) {
2078 if (ubi->works_count == 0) {
2079 ubi_err(ubi, "no free eraseblocks");
2080 ubi_assert(list_empty(&ubi->works));
2081 spin_unlock(&ubi->wl_lock);
2085 err = produce_free_peb(ubi);
2087 spin_unlock(&ubi->wl_lock);
2090 spin_unlock(&ubi->wl_lock);
2091 up_read(&ubi->fm_eba_sem);
2095 e = wl_get_wle(ubi);
2096 prot_queue_add(ubi, e);
2097 spin_unlock(&ubi->wl_lock);
2099 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
2100 ubi->peb_size - ubi->vid_hdr_aloffset);
2102 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);