Lines Matching refs:ubi

92 #include "ubi.h"
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,
137 * the @ubi->used and @ubi->free RB-trees.
169 * @ubi: UBI device description object
175 static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
177 ubi->lookuptbl[e->pnum] = NULL;
183 * @ubi: UBI device description object
190 static int do_work(struct ubi_device *ubi, int *executed)
198 * @ubi->work_sem is used to synchronize with the workers. Workers take
203 down_read(&ubi->work_sem);
204 spin_lock(&ubi->wl_lock);
205 if (list_empty(&ubi->works)) {
206 spin_unlock(&ubi->wl_lock);
207 up_read(&ubi->work_sem);
215 wrk = list_entry(ubi->works.next, struct ubi_work, list);
217 ubi->works_count -= 1;
218 ubi_assert(ubi->works_count >= 0);
219 spin_unlock(&ubi->wl_lock);
226 err = wrk->func(ubi, wrk, 0);
228 ubi_err(ubi, "work failed with error code %d", err);
229 up_read(&ubi->work_sem);
275 * @ubi: UBI device description object
281 static inline int in_pq(const struct ubi_device *ubi, struct ubi_wl_entry *e)
287 list_for_each_entry(p, &ubi->pq[i], u.list)
296 * @ubi: UBI device description object
299 * This function adds @e to the tail of the protection queue @ubi->pq, where
304 static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
306 int pq_tail = ubi->pq_head - 1;
311 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
317 * @ubi: UBI device description object
325 static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
356 * @ubi: UBI device description object
363 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
380 e = may_reserve_for_fm(ubi, e, root);
382 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2, 0);
390 * @ubi: UBI device description object
395 static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
399 e = find_mean_wl_entry(ubi, &ubi->free);
401 ubi_err(ubi, "no free eraseblocks");
405 self_check_in_wl_tree(ubi, e, &ubi->free);
411 rb_erase(&e->u.rb, &ubi->free);
412 ubi->free_count--;
420 * @ubi: UBI device description object
426 static int prot_queue_del(struct ubi_device *ubi, int pnum)
430 e = ubi->lookuptbl[pnum];
434 if (self_check_in_pq(ubi, e))
444 * @ubi: UBI device description object
451 int ubi_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture)
459 err = self_check_ec(ubi, e->pnum, e->ec);
463 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
467 err = ubi_io_sync_erase(ubi, e->pnum, torture);
477 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
487 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
492 spin_lock(&ubi->wl_lock);
493 if (e->ec > ubi->max_ec)
494 ubi->max_ec = e->ec;
495 spin_unlock(&ubi->wl_lock);
504 * @ubi: UBI device description object
510 static void serve_prot_queue(struct ubi_device *ubi)
521 spin_lock(&ubi->wl_lock);
522 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
527 wl_tree_add(e, &ubi->used);
533 spin_unlock(&ubi->wl_lock);
539 ubi->pq_head += 1;
540 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
541 ubi->pq_head = 0;
542 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
543 spin_unlock(&ubi->wl_lock);
548 * @ubi: UBI device description object
552 * list. Can only be used if ubi->work_sem is already held in read mode!
554 static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
556 spin_lock(&ubi->wl_lock);
557 list_add_tail(&wrk->list, &ubi->works);
558 ubi_assert(ubi->works_count >= 0);
559 ubi->works_count += 1;
560 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
561 wake_up_process(ubi->bgt_thread);
562 spin_unlock(&ubi->wl_lock);
567 * @ubi: UBI device description object
573 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
575 down_read(&ubi->work_sem);
576 __schedule_ubi_work(ubi, wrk);
577 up_read(&ubi->work_sem);
580 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
585 * @ubi: UBI device description object
595 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
616 __schedule_ubi_work(ubi, wl_wrk);
618 schedule_ubi_work(ubi, wl_wrk);
622 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk);
625 * @ubi: UBI device description object
632 static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
644 return __erase_worker(ubi, &wl_wrk);
647 static int ensure_wear_leveling(struct ubi_device *ubi, int nested);
650 * @ubi: UBI device description object
659 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
673 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
679 down_read(&ubi->fm_eba_sem);
680 mutex_lock(&ubi->move_mutex);
681 spin_lock(&ubi->wl_lock);
682 ubi_assert(!ubi->move_from && !ubi->move_to);
683 ubi_assert(!ubi->move_to_put);
686 if (!next_peb_for_wl(ubi, true) ||
688 if (!ubi->free.rb_node ||
690 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
698 * @ubi->used tree later and the wear-leveling will be
702 !ubi->free.rb_node, !ubi->used.rb_node);
707 e1 = find_anchor_wl_entry(&ubi->used);
708 if (e1 && ubi->fm_anchor &&
709 (ubi->fm_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) {
710 ubi->fm_do_produce_anchor = 1;
716 wl_tree_add(ubi->fm_anchor, &ubi->free);
717 ubi->fm_anchor = NULL;
718 ubi->free_count++;
721 if (ubi->fm_do_produce_anchor) {
724 e2 = get_peb_for_wl(ubi);
728 self_check_in_wl_tree(ubi, e1, &ubi->used);
729 rb_erase(&e1->u.rb, &ubi->used);
731 ubi->fm_do_produce_anchor = 0;
732 } else if (!ubi->scrub.rb_node) {
734 if (!ubi->scrub.rb_node) {
741 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
742 e2 = get_peb_for_wl(ubi);
751 wl_tree_add(e2, &ubi->free);
752 ubi->free_count++;
755 self_check_in_wl_tree(ubi, e1, &ubi->used);
756 rb_erase(&e1->u.rb, &ubi->used);
762 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
763 e2 = get_peb_for_wl(ubi);
767 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
768 rb_erase(&e1->u.rb, &ubi->scrub);
772 ubi->move_from = e1;
773 ubi->move_to = e2;
774 spin_unlock(&ubi->wl_lock);
787 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0);
814 } else if (ubi->fast_attach && err == UBI_IO_BAD_HDR_EBADMSG) {
826 ubi_err(ubi, "error %d while reading VID header from PEB %d",
834 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb);
852 * be put back into ubi->scrub list.
853 * 2. Non-scrub type PEB will be put back into ubi->used
876 * put this PEB to the @ubi->erroneous list to prevent
879 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
880 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
881 ubi->erroneous_peb_count);
897 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
901 spin_lock(&ubi->wl_lock);
902 if (!ubi->move_to_put) {
903 wl_tree_add(e2, &ubi->used);
906 ubi->move_from = ubi->move_to = NULL;
907 ubi->move_to_put = ubi->wl_scheduled = 0;
908 spin_unlock(&ubi->wl_lock);
910 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
913 spin_lock(&ubi->wl_lock);
914 wl_entry_destroy(ubi, e2);
915 spin_unlock(&ubi->wl_lock);
927 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
933 mutex_unlock(&ubi->move_mutex);
934 up_read(&ubi->fm_eba_sem);
949 spin_lock(&ubi->wl_lock);
951 prot_queue_add(ubi, e1);
953 wl_tree_add(e1, &ubi->erroneous);
954 ubi->erroneous_peb_count += 1;
956 wl_tree_add(e1, &ubi->scrub);
958 wl_tree_add(e1, &ubi->used);
960 wl_tree_add(e2, &ubi->free);
961 ubi->free_count++;
964 ubi_assert(!ubi->move_to_put);
965 ubi->move_from = ubi->move_to = NULL;
966 ubi->wl_scheduled = 0;
967 spin_unlock(&ubi->wl_lock);
971 ensure_wear_leveling(ubi, 1);
973 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
979 err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
984 mutex_unlock(&ubi->move_mutex);
985 up_read(&ubi->fm_eba_sem);
990 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
993 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
995 spin_lock(&ubi->wl_lock);
996 ubi->move_from = ubi->move_to = NULL;
997 ubi->move_to_put = ubi->wl_scheduled = 0;
998 wl_entry_destroy(ubi, e1);
999 wl_entry_destroy(ubi, e2);
1000 spin_unlock(&ubi->wl_lock);
1005 ubi_ro_mode(ubi);
1006 mutex_unlock(&ubi->move_mutex);
1007 up_read(&ubi->fm_eba_sem);
1012 ubi->wl_scheduled = 0;
1013 spin_unlock(&ubi->wl_lock);
1014 mutex_unlock(&ubi->move_mutex);
1015 up_read(&ubi->fm_eba_sem);
1022 * @ubi: UBI device description object
1029 static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
1034 spin_lock(&ubi->wl_lock);
1035 if (ubi->wl_scheduled)
1040 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1043 if (!ubi->scrub.rb_node) {
1045 if (!need_wear_leveling(ubi))
1051 if (!ubi->used.rb_node || !ubi->free.rb_node)
1061 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1062 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
1071 ubi->wl_scheduled = 1;
1072 spin_unlock(&ubi->wl_lock);
1082 __schedule_ubi_work(ubi, wrk);
1084 schedule_ubi_work(ubi, wrk);
1088 spin_lock(&ubi->wl_lock);
1089 ubi->wl_scheduled = 0;
1091 spin_unlock(&ubi->wl_lock);
1097 * @ubi: UBI device description object
1105 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk)
1116 err = ubi_sync_erase(ubi, e, wl_wrk->torture);
1118 spin_lock(&ubi->wl_lock);
1120 if (!ubi->fm_disabled && !ubi->fm_anchor &&
1126 ubi->fm_anchor = e;
1127 ubi->fm_do_produce_anchor = 0;
1129 wl_tree_add(e, &ubi->free);
1130 ubi->free_count++;
1133 spin_unlock(&ubi->wl_lock);
1139 serve_prot_queue(ubi);
1142 err = ensure_wear_leveling(ubi, 1);
1146 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
1153 err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
1155 spin_lock(&ubi->wl_lock);
1156 wl_entry_destroy(ubi, e);
1157 spin_unlock(&ubi->wl_lock);
1164 spin_lock(&ubi->wl_lock);
1165 wl_entry_destroy(ubi, e);
1166 spin_unlock(&ubi->wl_lock);
1177 if (!ubi->bad_allowed) {
1178 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
1182 spin_lock(&ubi->volumes_lock);
1183 if (ubi->beb_rsvd_pebs == 0) {
1184 if (ubi->avail_pebs == 0) {
1185 spin_unlock(&ubi->volumes_lock);
1186 ubi_err(ubi, "no reserved/available physical eraseblocks");
1189 ubi->avail_pebs -= 1;
1192 spin_unlock(&ubi->volumes_lock);
1194 ubi_msg(ubi, "mark PEB %d as bad", pnum);
1195 err = ubi_io_mark_bad(ubi, pnum);
1199 spin_lock(&ubi->volumes_lock);
1200 if (ubi->beb_rsvd_pebs > 0) {
1206 ubi->avail_pebs += 1;
1209 ubi->beb_rsvd_pebs -= 1;
1211 ubi->bad_peb_count += 1;
1212 ubi->good_peb_count -= 1;
1213 ubi_calculate_reserved(ubi);
1215 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
1216 else if (ubi->beb_rsvd_pebs)
1217 ubi_msg(ubi, "%d PEBs left in the reserve",
1218 ubi->beb_rsvd_pebs);
1220 ubi_warn(ubi, "last PEB from the reserve was used");
1221 spin_unlock(&ubi->volumes_lock);
1227 spin_lock(&ubi->volumes_lock);
1228 ubi->avail_pebs += 1;
1229 spin_unlock(&ubi->volumes_lock);
1231 ubi_ro_mode(ubi);
1235 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1245 wl_entry_destroy(ubi, e);
1249 ret = __erase_worker(ubi, wl_wrk);
1256 * @ubi: UBI device description object
1267 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1275 ubi_assert(pnum < ubi->peb_count);
1277 down_read(&ubi->fm_protect);
1280 spin_lock(&ubi->wl_lock);
1281 e = ubi->lookuptbl[pnum];
1287 * ubi_wl_put_peb) will set ubi ro_mode at the same time,
1290 spin_unlock(&ubi->wl_lock);
1291 up_read(&ubi->fm_protect);
1294 if (e == ubi->move_from) {
1301 spin_unlock(&ubi->wl_lock);
1303 /* Wait for the WL worker by taking the @ubi->move_mutex */
1304 mutex_lock(&ubi->move_mutex);
1305 mutex_unlock(&ubi->move_mutex);
1307 } else if (e == ubi->move_to) {
1318 ubi_assert(!ubi->move_to_put);
1319 ubi->move_to_put = 1;
1320 spin_unlock(&ubi->wl_lock);
1321 up_read(&ubi->fm_protect);
1324 if (in_wl_tree(e, &ubi->used)) {
1325 self_check_in_wl_tree(ubi, e, &ubi->used);
1326 rb_erase(&e->u.rb, &ubi->used);
1327 } else if (in_wl_tree(e, &ubi->scrub)) {
1328 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1329 rb_erase(&e->u.rb, &ubi->scrub);
1330 } else if (in_wl_tree(e, &ubi->erroneous)) {
1331 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1332 rb_erase(&e->u.rb, &ubi->erroneous);
1333 ubi->erroneous_peb_count -= 1;
1334 ubi_assert(ubi->erroneous_peb_count >= 0);
1338 err = prot_queue_del(ubi, e->pnum);
1340 ubi_err(ubi, "PEB %d not found", pnum);
1341 ubi_ro_mode(ubi);
1342 spin_unlock(&ubi->wl_lock);
1343 up_read(&ubi->fm_protect);
1348 spin_unlock(&ubi->wl_lock);
1350 err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
1352 spin_lock(&ubi->wl_lock);
1353 wl_tree_add(e, &ubi->used);
1354 spin_unlock(&ubi->wl_lock);
1357 up_read(&ubi->fm_protect);
1363 * @ubi: UBI device description object
1371 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1375 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
1378 spin_lock(&ubi->wl_lock);
1379 e = ubi->lookuptbl[pnum];
1380 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1381 in_wl_tree(e, &ubi->erroneous)) {
1382 spin_unlock(&ubi->wl_lock);
1386 if (e == ubi->move_to) {
1393 spin_unlock(&ubi->wl_lock);
1399 if (in_wl_tree(e, &ubi->used)) {
1400 self_check_in_wl_tree(ubi, e, &ubi->used);
1401 rb_erase(&e->u.rb, &ubi->used);
1405 err = prot_queue_del(ubi, e->pnum);
1407 ubi_err(ubi, "PEB %d not found", pnum);
1408 ubi_ro_mode(ubi);
1409 spin_unlock(&ubi->wl_lock);
1414 wl_tree_add(e, &ubi->scrub);
1415 spin_unlock(&ubi->wl_lock);
1421 return ensure_wear_leveling(ubi, 0);
1426 * @ubi: UBI device description object
1436 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
1446 vol_id, lnum, ubi->works_count);
1452 down_read(&ubi->work_sem);
1453 spin_lock(&ubi->wl_lock);
1454 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
1458 ubi->works_count -= 1;
1459 ubi_assert(ubi->works_count >= 0);
1460 spin_unlock(&ubi->wl_lock);
1462 err = wrk->func(ubi, wrk, 0);
1464 up_read(&ubi->work_sem);
1468 spin_lock(&ubi->wl_lock);
1473 spin_unlock(&ubi->wl_lock);
1474 up_read(&ubi->work_sem);
1481 down_write(&ubi->work_sem);
1482 up_write(&ubi->work_sem);
1487 static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e)
1489 if (in_wl_tree(e, &ubi->scrub))
1491 else if (in_wl_tree(e, &ubi->erroneous))
1493 else if (ubi->move_from == e)
1495 else if (ubi->move_to == e)
1503 * @ubi: UBI device description object
1520 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force)
1525 if (pnum < 0 || pnum >= ubi->peb_count) {
1534 down_write(&ubi->work_sem);
1540 spin_lock(&ubi->wl_lock);
1541 e = ubi->lookuptbl[pnum];
1543 spin_unlock(&ubi->wl_lock);
1551 if (!scrub_possible(ubi, e)) {
1552 spin_unlock(&ubi->wl_lock);
1556 spin_unlock(&ubi->wl_lock);
1559 mutex_lock(&ubi->buf_mutex);
1560 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
1561 mutex_unlock(&ubi->buf_mutex);
1568 spin_lock(&ubi->wl_lock);
1574 e = ubi->lookuptbl[pnum];
1576 spin_unlock(&ubi->wl_lock);
1584 if (!scrub_possible(ubi, e)) {
1585 spin_unlock(&ubi->wl_lock);
1590 if (in_pq(ubi, e)) {
1591 prot_queue_del(ubi, e->pnum);
1592 wl_tree_add(e, &ubi->scrub);
1593 spin_unlock(&ubi->wl_lock);
1595 err = ensure_wear_leveling(ubi, 1);
1596 } else if (in_wl_tree(e, &ubi->used)) {
1597 rb_erase(&e->u.rb, &ubi->used);
1598 wl_tree_add(e, &ubi->scrub);
1599 spin_unlock(&ubi->wl_lock);
1601 err = ensure_wear_leveling(ubi, 1);
1602 } else if (in_wl_tree(e, &ubi->free)) {
1603 rb_erase(&e->u.rb, &ubi->free);
1604 ubi->free_count--;
1605 spin_unlock(&ubi->wl_lock);
1611 err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
1614 spin_unlock(&ubi->wl_lock);
1625 up_write(&ubi->work_sem);
1633 * @ubi: UBI device description object
1636 static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
1658 wl_entry_destroy(ubi, e);
1670 struct ubi_device *ubi = u;
1672 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
1673 ubi->bgt_name, task_pid_nr(current));
1685 spin_lock(&ubi->wl_lock);
1686 if (list_empty(&ubi->works) || ubi->ro_mode ||
1687 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1689 spin_unlock(&ubi->wl_lock);
1706 spin_unlock(&ubi->wl_lock);
1708 err = do_work(ubi, NULL);
1710 ubi_err(ubi, "%s: work failed with error code %d",
1711 ubi->bgt_name, err);
1717 ubi_msg(ubi, "%s: %d consecutive failures",
1718 ubi->bgt_name, WL_MAX_FAILURES);
1719 ubi_ro_mode(ubi);
1720 ubi->thread_enabled = 0;
1729 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1730 ubi->thread_enabled = 0;
1736 * @ubi: UBI device description object
1738 static void shutdown_work(struct ubi_device *ubi)
1740 while (!list_empty(&ubi->works)) {
1743 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1745 wrk->func(ubi, wrk, 1);
1746 ubi->works_count -= 1;
1747 ubi_assert(ubi->works_count >= 0);
1753 * @ubi: UBI device description object
1757 static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync)
1768 ubi->lookuptbl[e->pnum] = e;
1771 err = ubi_sync_erase(ubi, e, false);
1775 wl_tree_add(e, &ubi->free);
1776 ubi->free_count++;
1778 err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
1786 wl_entry_destroy(ubi, e);
1793 * @ubi: UBI device description object
1799 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1807 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1808 spin_lock_init(&ubi->wl_lock);
1809 mutex_init(&ubi->move_mutex);
1810 init_rwsem(&ubi->work_sem);
1811 ubi->max_ec = ai->max_ec;
1812 INIT_LIST_HEAD(&ubi->works);
1814 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1817 ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
1818 if (!ubi->lookuptbl)
1822 INIT_LIST_HEAD(&ubi->pq[i]);
1823 ubi->pq_head = 0;
1825 ubi->free_count = 0;
1829 err = erase_aeb(ubi, aeb, false);
1849 wl_tree_add(e, &ubi->free);
1850 ubi->free_count++;
1852 ubi->lookuptbl[e->pnum] = e;
1869 ubi->lookuptbl[e->pnum] = e;
1874 wl_tree_add(e, &ubi->used);
1878 wl_tree_add(e, &ubi->scrub);
1888 e = ubi_find_fm_block(ubi, aeb->pnum);
1891 ubi_assert(!ubi->lookuptbl[e->pnum]);
1892 ubi->lookuptbl[e->pnum] = e;
1902 if (ubi->lookuptbl[aeb->pnum])
1917 err = erase_aeb(ubi, aeb, sync);
1927 ubi_assert(ubi->good_peb_count == found_pebs);
1930 ubi_fastmap_init(ubi, &reserved_pebs);
1932 if (ubi->avail_pebs < reserved_pebs) {
1933 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1934 ubi->avail_pebs, reserved_pebs);
1935 if (ubi->corr_peb_count)
1936 ubi_err(ubi, "%d PEBs are corrupted and not used",
1937 ubi->corr_peb_count);
1941 ubi->avail_pebs -= reserved_pebs;
1942 ubi->rsvd_pebs += reserved_pebs;
1945 err = ensure_wear_leveling(ubi, 0);
1950 if (!ubi->ro_mode && !ubi->fm_disabled)
1951 ubi_ensure_anchor_pebs(ubi);
1956 shutdown_work(ubi);
1957 tree_destroy(ubi, &ubi->used);
1958 tree_destroy(ubi, &ubi->free);
1959 tree_destroy(ubi, &ubi->scrub);
1960 kfree(ubi->lookuptbl);
1966 * @ubi: UBI device description object
1968 static void protection_queue_destroy(struct ubi_device *ubi)
1974 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1976 wl_entry_destroy(ubi, e);
1983 * @ubi: UBI device description object
1985 void ubi_wl_close(struct ubi_device *ubi)
1988 ubi_fastmap_close(ubi);
1989 shutdown_work(ubi);
1990 protection_queue_destroy(ubi);
1991 tree_destroy(ubi, &ubi->used);
1992 tree_destroy(ubi, &ubi->erroneous);
1993 tree_destroy(ubi, &ubi->free);
1994 tree_destroy(ubi, &ubi->scrub);
1995 kfree(ubi->lookuptbl);
2000 * @ubi: UBI device description object
2008 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
2014 if (!ubi_dbg_chk_gen(ubi))
2017 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
2021 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
2030 ubi_err(ubi, "self-check failed for PEB %d", pnum);
2031 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
2044 * @ubi: UBI device description object
2051 static int self_check_in_wl_tree(const struct ubi_device *ubi,
2054 if (!ubi_dbg_chk_gen(ubi))
2060 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
2069 * @ubi: UBI device description object
2072 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2074 static int self_check_in_pq(const struct ubi_device *ubi,
2077 if (!ubi_dbg_chk_gen(ubi))
2080 if (in_pq(ubi, e))
2083 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
2089 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
2093 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
2094 self_check_in_wl_tree(ubi, e, &ubi->free);
2095 ubi->free_count--;
2096 ubi_assert(ubi->free_count >= 0);
2097 rb_erase(&e->u.rb, &ubi->free);
2104 * @ubi: UBI device description object
2111 static int produce_free_peb(struct ubi_device *ubi)
2115 while (!ubi->free.rb_node && ubi->works_count) {
2116 spin_unlock(&ubi->wl_lock);
2119 err = do_work(ubi, NULL);
2121 spin_lock(&ubi->wl_lock);
2131 * @ubi: UBI device description object
2135 * Returns with ubi->fm_eba_sem held in read mode!
2137 int ubi_wl_get_peb(struct ubi_device *ubi)
2143 down_read(&ubi->fm_eba_sem);
2144 spin_lock(&ubi->wl_lock);
2145 if (!ubi->free.rb_node) {
2146 if (ubi->works_count == 0) {
2147 ubi_err(ubi, "no free eraseblocks");
2148 ubi_assert(list_empty(&ubi->works));
2149 spin_unlock(&ubi->wl_lock);
2153 err = produce_free_peb(ubi);
2155 spin_unlock(&ubi->wl_lock);
2158 spin_unlock(&ubi->wl_lock);
2159 up_read(&ubi->fm_eba_sem);
2163 e = wl_get_wle(ubi);
2164 prot_queue_add(ubi, e);
2165 spin_unlock(&ubi->wl_lock);
2167 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
2168 ubi->peb_size - ubi->vid_hdr_aloffset);
2170 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);