Lines Matching full:ubi
9 * UBI wear-leveling sub-system.
23 * done asynchronously in context of the per-UBI device background thread,
37 * As it was said, for the UBI sub-system all physical eraseblocks are either
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) in wl_entry_destroy() argument
177 ubi->lookuptbl[e->pnum] = NULL; in wl_entry_destroy()
183 * @ubi: UBI device description object
190 static int do_work(struct ubi_device *ubi, int *executed) in do_work() argument
198 * @ubi->work_sem is used to synchronize with the workers. Workers take in do_work()
203 down_read(&ubi->work_sem); in do_work()
204 spin_lock(&ubi->wl_lock); in do_work()
205 if (list_empty(&ubi->works)) { in do_work()
206 spin_unlock(&ubi->wl_lock); in do_work()
207 up_read(&ubi->work_sem); in do_work()
215 wrk = list_entry(ubi->works.next, struct ubi_work, list); in do_work()
217 ubi->works_count -= 1; in do_work()
218 ubi_assert(ubi->works_count >= 0); in do_work()
219 spin_unlock(&ubi->wl_lock); in do_work()
226 err = wrk->func(ubi, wrk, 0); in do_work()
228 ubi_err(ubi, "work failed with error code %d", err); in do_work()
229 up_read(&ubi->work_sem); in do_work()
275 * @ubi: UBI device description object
281 static inline int in_pq(const struct ubi_device *ubi, struct ubi_wl_entry *e) in in_pq() argument
287 list_for_each_entry(p, &ubi->pq[i], u.list) in in_pq()
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) in prot_queue_add() argument
306 int pq_tail = ubi->pq_head - 1; in prot_queue_add()
311 list_add_tail(&e->u.list, &ubi->pq[pq_tail]); in prot_queue_add()
317 * @ubi: UBI device description object
325 static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi, in find_wl_entry() argument
356 * @ubi: UBI device description object
363 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi, in find_mean_wl_entry() argument
380 e = may_reserve_for_fm(ubi, e, root); in find_mean_wl_entry()
382 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2, 0); in find_mean_wl_entry()
390 * @ubi: UBI device description object
395 static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi) in wl_get_wle() argument
399 e = find_mean_wl_entry(ubi, &ubi->free); in wl_get_wle()
401 ubi_err(ubi, "no free eraseblocks"); in wl_get_wle()
405 self_check_in_wl_tree(ubi, e, &ubi->free); in wl_get_wle()
411 rb_erase(&e->u.rb, &ubi->free); in wl_get_wle()
412 ubi->free_count--; in wl_get_wle()
420 * @ubi: UBI device description object
426 static int prot_queue_del(struct ubi_device *ubi, int pnum) in prot_queue_del() argument
430 e = ubi->lookuptbl[pnum]; in prot_queue_del()
434 if (self_check_in_pq(ubi, e)) in prot_queue_del()
444 * @ubi: UBI device description object
451 int ubi_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture) in ubi_sync_erase() argument
459 err = self_check_ec(ubi, e->pnum, e->ec); in ubi_sync_erase()
463 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); in ubi_sync_erase()
467 err = ubi_io_sync_erase(ubi, e->pnum, torture); in ubi_sync_erase()
474 * Erase counter overflow. Upgrade UBI and use 64-bit in ubi_sync_erase()
477 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu", in ubi_sync_erase()
487 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); in ubi_sync_erase()
492 spin_lock(&ubi->wl_lock); in ubi_sync_erase()
493 if (e->ec > ubi->max_ec) in ubi_sync_erase()
494 ubi->max_ec = e->ec; in ubi_sync_erase()
495 spin_unlock(&ubi->wl_lock); in ubi_sync_erase()
504 * @ubi: UBI device description object
510 static void serve_prot_queue(struct ubi_device *ubi) in serve_prot_queue() argument
521 spin_lock(&ubi->wl_lock); in serve_prot_queue()
522 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) { in serve_prot_queue()
527 wl_tree_add(e, &ubi->used); in serve_prot_queue()
533 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
539 ubi->pq_head += 1; in serve_prot_queue()
540 if (ubi->pq_head == UBI_PROT_QUEUE_LEN) in serve_prot_queue()
541 ubi->pq_head = 0; in serve_prot_queue()
542 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN); in serve_prot_queue()
543 spin_unlock(&ubi->wl_lock); in serve_prot_queue()
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) in __schedule_ubi_work() argument
556 spin_lock(&ubi->wl_lock); in __schedule_ubi_work()
557 list_add_tail(&wrk->list, &ubi->works); in __schedule_ubi_work()
558 ubi_assert(ubi->works_count >= 0); in __schedule_ubi_work()
559 ubi->works_count += 1; in __schedule_ubi_work()
560 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi)) in __schedule_ubi_work()
561 wake_up_process(ubi->bgt_thread); in __schedule_ubi_work()
562 spin_unlock(&ubi->wl_lock); in __schedule_ubi_work()
567 * @ubi: UBI device description object
573 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) in schedule_ubi_work() argument
575 down_read(&ubi->work_sem); in schedule_ubi_work()
576 __schedule_ubi_work(ubi, wrk); in schedule_ubi_work()
577 up_read(&ubi->work_sem); in schedule_ubi_work()
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, in schedule_erase() argument
616 __schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
618 schedule_ubi_work(ubi, wl_wrk); in schedule_erase()
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, in do_sync_erase() argument
644 return __erase_worker(ubi, &wl_wrk); in do_sync_erase()
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, argument
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) ||
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);
792 * We are trying to move PEB without a VID header. UBI
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);
869 * put this PEB to the @ubi->erroneous list to prevent
870 * UBI from trying to move it over and over again.
872 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
873 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
874 ubi->erroneous_peb_count);
890 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
894 spin_lock(&ubi->wl_lock);
895 if (!ubi->move_to_put) {
896 wl_tree_add(e2, &ubi->used);
899 ubi->move_from = ubi->move_to = NULL;
900 ubi->move_to_put = ubi->wl_scheduled = 0;
901 spin_unlock(&ubi->wl_lock);
903 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
906 spin_lock(&ubi->wl_lock);
907 wl_entry_destroy(ubi, e2);
908 spin_unlock(&ubi->wl_lock);
920 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
926 mutex_unlock(&ubi->move_mutex);
927 up_read(&ubi->fm_eba_sem);
942 spin_lock(&ubi->wl_lock);
944 prot_queue_add(ubi, e1);
946 wl_tree_add(e1, &ubi->erroneous);
947 ubi->erroneous_peb_count += 1;
949 wl_tree_add(e1, &ubi->scrub);
951 wl_tree_add(e1, &ubi->used);
953 wl_tree_add(e2, &ubi->free);
954 ubi->free_count++;
957 ubi_assert(!ubi->move_to_put);
958 ubi->move_from = ubi->move_to = NULL;
959 ubi->wl_scheduled = 0;
960 spin_unlock(&ubi->wl_lock);
964 ensure_wear_leveling(ubi, 1);
966 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
972 err = do_sync_erase(ubi, e1, vol_id, lnum, 1);
977 mutex_unlock(&ubi->move_mutex);
978 up_read(&ubi->fm_eba_sem);
983 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
986 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
988 spin_lock(&ubi->wl_lock);
989 ubi->move_from = ubi->move_to = NULL;
990 ubi->move_to_put = ubi->wl_scheduled = 0;
991 wl_entry_destroy(ubi, e1);
992 wl_entry_destroy(ubi, e2);
993 spin_unlock(&ubi->wl_lock);
998 ubi_ro_mode(ubi);
999 mutex_unlock(&ubi->move_mutex);
1000 up_read(&ubi->fm_eba_sem);
1005 ubi->wl_scheduled = 0;
1006 spin_unlock(&ubi->wl_lock);
1007 mutex_unlock(&ubi->move_mutex);
1008 up_read(&ubi->fm_eba_sem);
1015 * @ubi: UBI device description object
1016 * @nested: set to non-zero if this function is called from UBI worker
1022 static int ensure_wear_leveling(struct ubi_device *ubi, int nested) argument
1027 spin_lock(&ubi->wl_lock);
1028 if (ubi->wl_scheduled)
1033 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1036 if (!ubi->scrub.rb_node) {
1038 if (!need_wear_leveling(ubi))
1044 if (!ubi->used.rb_node || !ubi->free.rb_node)
1054 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1055 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
1064 ubi->wl_scheduled = 1;
1065 spin_unlock(&ubi->wl_lock);
1075 __schedule_ubi_work(ubi, wrk);
1077 schedule_ubi_work(ubi, wrk);
1081 spin_lock(&ubi->wl_lock);
1082 ubi->wl_scheduled = 0;
1084 spin_unlock(&ubi->wl_lock);
1090 * @ubi: UBI device description object
1098 static int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) argument
1109 err = ubi_sync_erase(ubi, e, wl_wrk->torture);
1111 spin_lock(&ubi->wl_lock);
1113 if (!ubi->fm_disabled && !ubi->fm_anchor &&
1119 ubi->fm_anchor = e;
1120 ubi->fm_do_produce_anchor = 0;
1122 wl_tree_add(e, &ubi->free);
1123 ubi->free_count++;
1126 spin_unlock(&ubi->wl_lock);
1132 serve_prot_queue(ubi);
1135 err = ensure_wear_leveling(ubi, 1);
1139 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
1146 err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true);
1148 spin_lock(&ubi->wl_lock);
1149 wl_entry_destroy(ubi, e);
1150 spin_unlock(&ubi->wl_lock);
1157 spin_lock(&ubi->wl_lock);
1158 wl_entry_destroy(ubi, e);
1159 spin_unlock(&ubi->wl_lock);
1170 if (!ubi->bad_allowed) {
1171 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
1175 spin_lock(&ubi->volumes_lock);
1176 if (ubi->beb_rsvd_pebs == 0) {
1177 if (ubi->avail_pebs == 0) {
1178 spin_unlock(&ubi->volumes_lock);
1179 ubi_err(ubi, "no reserved/available physical eraseblocks");
1182 ubi->avail_pebs -= 1;
1185 spin_unlock(&ubi->volumes_lock);
1187 ubi_msg(ubi, "mark PEB %d as bad", pnum);
1188 err = ubi_io_mark_bad(ubi, pnum);
1192 spin_lock(&ubi->volumes_lock);
1193 if (ubi->beb_rsvd_pebs > 0) {
1199 ubi->avail_pebs += 1;
1202 ubi->beb_rsvd_pebs -= 1;
1204 ubi->bad_peb_count += 1;
1205 ubi->good_peb_count -= 1;
1206 ubi_calculate_reserved(ubi);
1208 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
1209 else if (ubi->beb_rsvd_pebs)
1210 ubi_msg(ubi, "%d PEBs left in the reserve",
1211 ubi->beb_rsvd_pebs);
1213 ubi_warn(ubi, "last PEB from the reserve was used");
1214 spin_unlock(&ubi->volumes_lock);
1220 spin_lock(&ubi->volumes_lock);
1221 ubi->avail_pebs += 1;
1222 spin_unlock(&ubi->volumes_lock);
1224 ubi_ro_mode(ubi);
1228 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, argument
1238 wl_entry_destroy(ubi, e);
1242 ret = __erase_worker(ubi, wl_wrk);
1249 * @ubi: UBI device description object
1260 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, argument
1268 ubi_assert(pnum < ubi->peb_count);
1270 down_read(&ubi->fm_protect);
1273 spin_lock(&ubi->wl_lock);
1274 e = ubi->lookuptbl[pnum];
1280 * ubi_wl_put_peb) will set ubi ro_mode at the same time,
1283 spin_unlock(&ubi->wl_lock);
1284 up_read(&ubi->fm_protect);
1287 if (e == ubi->move_from) {
1294 spin_unlock(&ubi->wl_lock);
1296 /* Wait for the WL worker by taking the @ubi->move_mutex */
1297 mutex_lock(&ubi->move_mutex);
1298 mutex_unlock(&ubi->move_mutex);
1300 } else if (e == ubi->move_to) {
1311 ubi_assert(!ubi->move_to_put);
1312 ubi->move_to_put = 1;
1313 spin_unlock(&ubi->wl_lock);
1314 up_read(&ubi->fm_protect);
1317 if (in_wl_tree(e, &ubi->used)) {
1318 self_check_in_wl_tree(ubi, e, &ubi->used);
1319 rb_erase(&e->u.rb, &ubi->used);
1320 } else if (in_wl_tree(e, &ubi->scrub)) {
1321 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1322 rb_erase(&e->u.rb, &ubi->scrub);
1323 } else if (in_wl_tree(e, &ubi->erroneous)) {
1324 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1325 rb_erase(&e->u.rb, &ubi->erroneous);
1326 ubi->erroneous_peb_count -= 1;
1327 ubi_assert(ubi->erroneous_peb_count >= 0);
1331 err = prot_queue_del(ubi, e->pnum);
1333 ubi_err(ubi, "PEB %d not found", pnum);
1334 ubi_ro_mode(ubi);
1335 spin_unlock(&ubi->wl_lock);
1336 up_read(&ubi->fm_protect);
1341 spin_unlock(&ubi->wl_lock);
1343 err = schedule_erase(ubi, e, vol_id, lnum, torture, false);
1345 spin_lock(&ubi->wl_lock);
1346 wl_tree_add(e, &ubi->used);
1347 spin_unlock(&ubi->wl_lock);
1350 up_read(&ubi->fm_protect);
1356 * @ubi: UBI device description object
1364 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) argument
1368 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
1371 spin_lock(&ubi->wl_lock);
1372 e = ubi->lookuptbl[pnum];
1373 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1374 in_wl_tree(e, &ubi->erroneous)) {
1375 spin_unlock(&ubi->wl_lock);
1379 if (e == ubi->move_to) {
1386 spin_unlock(&ubi->wl_lock);
1392 if (in_wl_tree(e, &ubi->used)) {
1393 self_check_in_wl_tree(ubi, e, &ubi->used);
1394 rb_erase(&e->u.rb, &ubi->used);
1398 err = prot_queue_del(ubi, e->pnum);
1400 ubi_err(ubi, "PEB %d not found", pnum);
1401 ubi_ro_mode(ubi);
1402 spin_unlock(&ubi->wl_lock);
1407 wl_tree_add(e, &ubi->scrub);
1408 spin_unlock(&ubi->wl_lock);
1414 return ensure_wear_leveling(ubi, 0);
1419 * @ubi: UBI device description object
1429 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) argument
1439 vol_id, lnum, ubi->works_count);
1445 down_read(&ubi->work_sem);
1446 spin_lock(&ubi->wl_lock);
1447 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
1451 ubi->works_count -= 1;
1452 ubi_assert(ubi->works_count >= 0);
1453 spin_unlock(&ubi->wl_lock);
1455 err = wrk->func(ubi, wrk, 0);
1457 up_read(&ubi->work_sem);
1461 spin_lock(&ubi->wl_lock);
1466 spin_unlock(&ubi->wl_lock);
1467 up_read(&ubi->work_sem);
1474 down_write(&ubi->work_sem);
1475 up_write(&ubi->work_sem);
1480 static bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e) argument
1482 if (in_wl_tree(e, &ubi->scrub))
1484 else if (in_wl_tree(e, &ubi->erroneous))
1486 else if (ubi->move_from == e)
1488 else if (ubi->move_to == e)
1496 * @ubi: UBI device description object
1507 * %ENOENT, PEB is no longer used by UBI
1513 int ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force) argument
1518 if (pnum < 0 || pnum >= ubi->peb_count) {
1527 down_write(&ubi->work_sem);
1533 spin_lock(&ubi->wl_lock);
1534 e = ubi->lookuptbl[pnum];
1536 spin_unlock(&ubi->wl_lock);
1544 if (!scrub_possible(ubi, e)) {
1545 spin_unlock(&ubi->wl_lock);
1549 spin_unlock(&ubi->wl_lock);
1552 mutex_lock(&ubi->buf_mutex);
1553 err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size);
1554 mutex_unlock(&ubi->buf_mutex);
1561 spin_lock(&ubi->wl_lock);
1564 * Recheck. We released wl_lock, UBI might have killed the
1567 e = ubi->lookuptbl[pnum];
1569 spin_unlock(&ubi->wl_lock);
1577 if (!scrub_possible(ubi, e)) {
1578 spin_unlock(&ubi->wl_lock);
1583 if (in_pq(ubi, e)) {
1584 prot_queue_del(ubi, e->pnum);
1585 wl_tree_add(e, &ubi->scrub);
1586 spin_unlock(&ubi->wl_lock);
1588 err = ensure_wear_leveling(ubi, 1);
1589 } else if (in_wl_tree(e, &ubi->used)) {
1590 rb_erase(&e->u.rb, &ubi->used);
1591 wl_tree_add(e, &ubi->scrub);
1592 spin_unlock(&ubi->wl_lock);
1594 err = ensure_wear_leveling(ubi, 1);
1595 } else if (in_wl_tree(e, &ubi->free)) {
1596 rb_erase(&e->u.rb, &ubi->free);
1597 ubi->free_count--;
1598 spin_unlock(&ubi->wl_lock);
1604 err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN,
1607 spin_unlock(&ubi->wl_lock);
1618 up_write(&ubi->work_sem);
1626 * @ubi: UBI device description object
1629 static void tree_destroy(struct ubi_device *ubi, struct rb_root *root) argument
1651 wl_entry_destroy(ubi, e);
1657 * ubi_thread - UBI background thread.
1658 * @u: the UBI device description object pointer
1663 struct ubi_device *ubi = u; local
1665 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
1666 ubi->bgt_name, task_pid_nr(current));
1678 spin_lock(&ubi->wl_lock);
1679 if (list_empty(&ubi->works) || ubi->ro_mode ||
1680 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1682 spin_unlock(&ubi->wl_lock);
1699 spin_unlock(&ubi->wl_lock);
1701 err = do_work(ubi, NULL);
1703 ubi_err(ubi, "%s: work failed with error code %d",
1704 ubi->bgt_name, err);
1710 ubi_msg(ubi, "%s: %d consecutive failures",
1711 ubi->bgt_name, WL_MAX_FAILURES);
1712 ubi_ro_mode(ubi);
1713 ubi->thread_enabled = 0;
1722 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1723 ubi->thread_enabled = 0;
1729 * @ubi: UBI device description object
1731 static void shutdown_work(struct ubi_device *ubi) argument
1733 while (!list_empty(&ubi->works)) {
1736 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1738 wrk->func(ubi, wrk, 1);
1739 ubi->works_count -= 1;
1740 ubi_assert(ubi->works_count >= 0);
1745 * erase_aeb - erase a PEB given in UBI attach info PEB
1746 * @ubi: UBI device description object
1747 * @aeb: UBI attach info PEB
1750 static int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) argument
1761 ubi->lookuptbl[e->pnum] = e;
1764 err = ubi_sync_erase(ubi, e, false);
1768 wl_tree_add(e, &ubi->free);
1769 ubi->free_count++;
1771 err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false);
1779 wl_entry_destroy(ubi, e);
1786 * @ubi: UBI device description object
1792 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) argument
1800 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1801 spin_lock_init(&ubi->wl_lock);
1802 mutex_init(&ubi->move_mutex);
1803 init_rwsem(&ubi->work_sem);
1804 ubi->max_ec = ai->max_ec;
1805 INIT_LIST_HEAD(&ubi->works);
1807 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1810 ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL);
1811 if (!ubi->lookuptbl)
1815 INIT_LIST_HEAD(&ubi->pq[i]);
1816 ubi->pq_head = 0;
1818 ubi->free_count = 0;
1822 err = erase_aeb(ubi, aeb, false);
1842 wl_tree_add(e, &ubi->free);
1843 ubi->free_count++;
1845 ubi->lookuptbl[e->pnum] = e;
1862 ubi->lookuptbl[e->pnum] = e;
1867 wl_tree_add(e, &ubi->used);
1871 wl_tree_add(e, &ubi->scrub);
1881 e = ubi_find_fm_block(ubi, aeb->pnum);
1884 ubi_assert(!ubi->lookuptbl[e->pnum]);
1885 ubi->lookuptbl[e->pnum] = e;
1895 if (ubi->lookuptbl[aeb->pnum])
1910 err = erase_aeb(ubi, aeb, sync);
1920 ubi_assert(ubi->good_peb_count == found_pebs);
1923 ubi_fastmap_init(ubi, &reserved_pebs);
1925 if (ubi->avail_pebs < reserved_pebs) {
1926 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
1927 ubi->avail_pebs, reserved_pebs);
1928 if (ubi->corr_peb_count)
1929 ubi_err(ubi, "%d PEBs are corrupted and not used",
1930 ubi->corr_peb_count);
1934 ubi->avail_pebs -= reserved_pebs;
1935 ubi->rsvd_pebs += reserved_pebs;
1938 err = ensure_wear_leveling(ubi, 0);
1943 if (!ubi->ro_mode && !ubi->fm_disabled)
1944 ubi_ensure_anchor_pebs(ubi);
1949 shutdown_work(ubi);
1950 tree_destroy(ubi, &ubi->used);
1951 tree_destroy(ubi, &ubi->free);
1952 tree_destroy(ubi, &ubi->scrub);
1953 kfree(ubi->lookuptbl);
1959 * @ubi: UBI device description object
1961 static void protection_queue_destroy(struct ubi_device *ubi) argument
1967 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1969 wl_entry_destroy(ubi, e);
1976 * @ubi: UBI device description object
1978 void ubi_wl_close(struct ubi_device *ubi) argument
1981 ubi_fastmap_close(ubi);
1982 shutdown_work(ubi);
1983 protection_queue_destroy(ubi);
1984 tree_destroy(ubi, &ubi->used);
1985 tree_destroy(ubi, &ubi->erroneous);
1986 tree_destroy(ubi, &ubi->free);
1987 tree_destroy(ubi, &ubi->scrub);
1988 kfree(ubi->lookuptbl);
1993 * @ubi: UBI device description object
2001 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec) argument
2007 if (!ubi_dbg_chk_gen(ubi))
2010 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
2014 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
2023 ubi_err(ubi, "self-check failed for PEB %d", pnum);
2024 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
2037 * @ubi: UBI device description object
2044 static int self_check_in_wl_tree(const struct ubi_device *ubi, argument
2047 if (!ubi_dbg_chk_gen(ubi))
2053 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
2062 * @ubi: UBI device description object
2065 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2067 static int self_check_in_pq(const struct ubi_device *ubi, argument
2070 if (!ubi_dbg_chk_gen(ubi))
2073 if (in_pq(ubi, e))
2076 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
2082 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) argument
2086 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
2087 self_check_in_wl_tree(ubi, e, &ubi->free);
2088 ubi->free_count--;
2089 ubi_assert(ubi->free_count >= 0);
2090 rb_erase(&e->u.rb, &ubi->free);
2097 * @ubi: UBI device description object
2104 static int produce_free_peb(struct ubi_device *ubi) argument
2108 while (!ubi->free.rb_node && ubi->works_count) {
2109 spin_unlock(&ubi->wl_lock);
2112 err = do_work(ubi, NULL);
2114 spin_lock(&ubi->wl_lock);
2124 * @ubi: UBI device description object
2128 * Returns with ubi->fm_eba_sem held in read mode!
2130 int ubi_wl_get_peb(struct ubi_device *ubi) argument
2136 down_read(&ubi->fm_eba_sem);
2137 spin_lock(&ubi->wl_lock);
2138 if (!ubi->free.rb_node) {
2139 if (ubi->works_count == 0) {
2140 ubi_err(ubi, "no free eraseblocks");
2141 ubi_assert(list_empty(&ubi->works));
2142 spin_unlock(&ubi->wl_lock);
2146 err = produce_free_peb(ubi);
2148 spin_unlock(&ubi->wl_lock);
2151 spin_unlock(&ubi->wl_lock);
2152 up_read(&ubi->fm_eba_sem);
2156 e = wl_get_wle(ubi);
2157 prot_queue_add(ubi, e);
2158 spin_unlock(&ubi->wl_lock);
2160 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
2161 ubi->peb_size - ubi->vid_hdr_aloffset);
2163 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);