2 * Copyright (c) International Business Machines Corp., 2006
4 * SPDX-License-Identifier: GPL-2.0+
6 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
10 * UBI wear-leveling sub-system.
12 * This sub-system is responsible for wear-leveling. It works in terms of
13 * physical eraseblocks and erase counters and knows nothing about logical
14 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
15 * eraseblocks are of two types - used and free. Used physical eraseblocks are
16 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
17 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
19 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
20 * header. The rest of the physical eraseblock contains only %0xFF bytes.
22 * When physical eraseblocks are returned to the WL sub-system by means of the
23 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
24 * done asynchronously in context of the per-UBI device background thread,
25 * which is also managed by the WL sub-system.
27 * The wear-leveling is ensured by means of moving the contents of used
28 * physical eraseblocks with low erase counter to free physical eraseblocks
29 * with high erase counter.
31 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
34 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
35 * in a physical eraseblock, it has to be moved. Technically this is the same
36 * as moving it for wear-leveling reasons.
38 * As it was said, for the UBI sub-system all physical eraseblocks are either
39 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
40 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
41 * RB-trees, as well as (temporarily) in the @wl->pq queue.
43 * When the WL sub-system returns a physical eraseblock, the physical
44 * eraseblock is protected from being moved for some "time". For this reason,
45 * the physical eraseblock is not directly moved from the @wl->free tree to the
46 * @wl->used tree. There is a protection queue in between where this
47 * physical eraseblock is temporarily stored (@wl->pq).
49 * All this protection stuff is needed because:
50 * o we don't want to move physical eraseblocks just after we have given them
51 * to the user; instead, we first want to let users fill them up with data;
53 * o there is a chance that the user will put the physical eraseblock very
54 * soon, so it makes sense not to move it for some time, but wait.
56 * Physical eraseblocks stay protected only for limited time. But the "time" is
57 * measured in erase cycles in this case. This is implemented with help of the
58 * protection queue. Eraseblocks are put to the tail of this queue when they
59 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
60 * head of the queue on each erase operation (for any eraseblock). So the
61 * length of the queue defines how may (global) erase cycles PEBs are protected.
63 * To put it differently, each physical eraseblock has 2 main states: free and
64 * used. The former state corresponds to the @wl->free tree. The latter state
65 * is split up on several sub-states:
66 * o the WL movement is allowed (@wl->used tree);
67 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
68 * erroneous - e.g., there was a read error;
69 * o the WL movement is temporarily prohibited (@wl->pq queue);
70 * o scrubbing is needed (@wl->scrub tree).
72 * Depending on the sub-state, wear-leveling entries of the used physical
73 * eraseblocks may be kept in one of those structures.
75 * Note, in this implementation, we keep a small in-RAM object for each physical
76 * eraseblock. This is surely not a scalable solution. But it appears to be good
77 * enough for moderately large flashes and it is simple. In future, one may
78 * re-work this sub-system and make it more scalable.
80 * At the moment this sub-system does not utilize the sequence number, which
81 * was introduced relatively recently. But it would be wise to do this because
82 * the sequence number of a logical eraseblock characterizes how old is it. For
83 * example, when we move a PEB with low erase counter, and we need to pick the
84 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
85 * pick target PEB with an average EC if our PEB is not very "old". This is a
86 * room for future re-works of the WL sub-system.
91 #include <linux/slab.h>
92 #include <linux/crc32.h>
93 #include <linux/freezer.h>
94 #include <linux/kthread.h>
96 #include <ubi_uboot.h>
101 /* Number of physical eraseblocks reserved for wear-leveling purposes */
102 #define WL_RESERVED_PEBS 1
105 * Maximum difference between two erase counters. If this threshold is
106 * exceeded, the WL sub-system starts moving data from used physical
107 * eraseblocks with low erase counter to free physical eraseblocks with high
110 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
113 * When a physical eraseblock is moved, the WL sub-system has to pick the target
114 * physical eraseblock to move to. The simplest way would be just to pick the
115 * one with the highest erase counter. But in certain workloads this could lead
116 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
117 * situation when the picked physical eraseblock is constantly erased after the
118 * data is written to it. So, we have a constant which limits the highest erase
119 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
120 * does not pick eraseblocks with erase counter greater than the lowest erase
121 * counter plus %WL_FREE_MAX_DIFF.
123 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
126 * Maximum number of consecutive background thread failures which is enough to
127 * switch to read-only mode.
129 #define WL_MAX_FAILURES 32
131 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
132 static int self_check_in_wl_tree(const struct ubi_device *ubi,
133 struct ubi_wl_entry *e, struct rb_root *root);
134 static int self_check_in_pq(const struct ubi_device *ubi,
135 struct ubi_wl_entry *e);
137 #ifdef CONFIG_MTD_UBI_FASTMAP
140 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
141 * @wrk: the work description object
143 static void update_fastmap_work_fn(struct work_struct *wrk)
145 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
146 ubi_update_fastmap(ubi);
151 * ubi_ubi_is_fm_block - returns 1 if a PEB is currently used in a fastmap.
152 * @ubi: UBI device description object
153 * @pnum: the to be checked PEB
155 static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
162 for (i = 0; i < ubi->fm->used_blocks; i++)
163 if (ubi->fm->e[i]->pnum == pnum)
169 static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
176 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
177 * @e: the wear-leveling entry to add
178 * @root: the root of the tree
180 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
181 * the @ubi->used and @ubi->free RB-trees.
183 static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
185 struct rb_node **p, *parent = NULL;
189 struct ubi_wl_entry *e1;
192 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
196 else if (e->ec > e1->ec)
199 ubi_assert(e->pnum != e1->pnum);
200 if (e->pnum < e1->pnum)
207 rb_link_node(&e->u.rb, parent, p);
208 rb_insert_color(&e->u.rb, root);
212 * do_work - do one pending work.
213 * @ubi: UBI device description object
215 * This function returns zero in case of success and a negative error code in
218 static int do_work(struct ubi_device *ubi)
221 struct ubi_work *wrk;
226 * @ubi->work_sem is used to synchronize with the workers. Workers take
227 * it in read mode, so many of them may be doing works at a time. But
228 * the queue flush code has to be sure the whole queue of works is
229 * done, and it takes the mutex in write mode.
231 down_read(&ubi->work_sem);
232 spin_lock(&ubi->wl_lock);
233 if (list_empty(&ubi->works)) {
234 spin_unlock(&ubi->wl_lock);
235 up_read(&ubi->work_sem);
239 wrk = list_entry(ubi->works.next, struct ubi_work, list);
240 list_del(&wrk->list);
241 ubi->works_count -= 1;
242 ubi_assert(ubi->works_count >= 0);
243 spin_unlock(&ubi->wl_lock);
246 * Call the worker function. Do not touch the work structure
247 * after this call as it will have been freed or reused by that
248 * time by the worker function.
250 err = wrk->func(ubi, wrk, 0);
252 ubi_err("work failed with error code %d", err);
253 up_read(&ubi->work_sem);
259 * produce_free_peb - produce a free physical eraseblock.
260 * @ubi: UBI device description object
262 * This function tries to make a free PEB by means of synchronous execution of
263 * pending works. This may be needed if, for example the background thread is
264 * disabled. Returns zero in case of success and a negative error code in case
267 static int produce_free_peb(struct ubi_device *ubi)
271 while (!ubi->free.rb_node) {
272 spin_unlock(&ubi->wl_lock);
274 dbg_wl("do one work synchronously");
277 spin_lock(&ubi->wl_lock);
286 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
287 * @e: the wear-leveling entry to check
288 * @root: the root of the tree
290 * This function returns non-zero if @e is in the @root RB-tree and zero if it
293 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
299 struct ubi_wl_entry *e1;
301 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
303 if (e->pnum == e1->pnum) {
310 else if (e->ec > e1->ec)
313 ubi_assert(e->pnum != e1->pnum);
314 if (e->pnum < e1->pnum)
325 * prot_queue_add - add physical eraseblock to the protection queue.
326 * @ubi: UBI device description object
327 * @e: the physical eraseblock to add
329 * This function adds @e to the tail of the protection queue @ubi->pq, where
330 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
331 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
334 static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
336 int pq_tail = ubi->pq_head - 1;
339 pq_tail = UBI_PROT_QUEUE_LEN - 1;
340 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
341 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
342 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
346 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
347 * @ubi: UBI device description object
348 * @root: the RB-tree where to look for
349 * @diff: maximum possible difference from the smallest erase counter
351 * This function looks for a wear leveling entry with erase counter closest to
352 * min + @diff, where min is the smallest erase counter.
354 static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
355 struct rb_root *root, int diff)
358 struct ubi_wl_entry *e, *prev_e = NULL;
361 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
366 struct ubi_wl_entry *e1;
368 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
378 /* If no fastmap has been written and this WL entry can be used
379 * as anchor PEB, hold it back and return the second best WL entry
380 * such that fastmap can use the anchor PEB later. */
381 if (prev_e && !ubi->fm_disabled &&
382 !ubi->fm && e->pnum < UBI_FM_MAX_START)
389 * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
390 * @ubi: UBI device description object
391 * @root: the RB-tree where to look for
393 * This function looks for a wear leveling entry with medium erase counter,
394 * but not greater or equivalent than the lowest erase counter plus
395 * %WL_FREE_MAX_DIFF/2.
397 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
398 struct rb_root *root)
400 struct ubi_wl_entry *e, *first, *last;
402 first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
403 last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
405 if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
406 e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
408 #ifdef CONFIG_MTD_UBI_FASTMAP
409 /* If no fastmap has been written and this WL entry can be used
410 * as anchor PEB, hold it back and return the second best
411 * WL entry such that fastmap can use the anchor PEB later. */
412 if (e && !ubi->fm_disabled && !ubi->fm &&
413 e->pnum < UBI_FM_MAX_START)
414 e = rb_entry(rb_next(root->rb_node),
415 struct ubi_wl_entry, u.rb);
418 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
423 #ifdef CONFIG_MTD_UBI_FASTMAP
425 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
426 * @root: the RB-tree where to look for
428 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
431 struct ubi_wl_entry *e, *victim = NULL;
432 int max_ec = UBI_MAX_ERASECOUNTER;
434 ubi_rb_for_each_entry(p, e, root, u.rb) {
435 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
444 static int anchor_pebs_avalible(struct rb_root *root)
447 struct ubi_wl_entry *e;
449 ubi_rb_for_each_entry(p, e, root, u.rb)
450 if (e->pnum < UBI_FM_MAX_START)
457 * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
458 * @ubi: UBI device description object
459 * @anchor: This PEB will be used as anchor PEB by fastmap
461 * The function returns a physical erase block with a given maximal number
462 * and removes it from the wl subsystem.
463 * Must be called with wl_lock held!
465 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
467 struct ubi_wl_entry *e = NULL;
469 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
473 e = find_anchor_wl_entry(&ubi->free);
475 e = find_mean_wl_entry(ubi, &ubi->free);
480 self_check_in_wl_tree(ubi, e, &ubi->free);
482 /* remove it from the free list,
483 * the wl subsystem does no longer know this erase block */
484 rb_erase(&e->u.rb, &ubi->free);
492 * __wl_get_peb - get a physical eraseblock.
493 * @ubi: UBI device description object
495 * This function returns a physical eraseblock in case of success and a
496 * negative error code in case of failure.
498 static int __wl_get_peb(struct ubi_device *ubi)
501 struct ubi_wl_entry *e;
504 if (!ubi->free.rb_node) {
505 if (ubi->works_count == 0) {
506 ubi_err("no free eraseblocks");
507 ubi_assert(list_empty(&ubi->works));
511 err = produce_free_peb(ubi);
517 e = find_mean_wl_entry(ubi, &ubi->free);
519 ubi_err("no free eraseblocks");
523 self_check_in_wl_tree(ubi, e, &ubi->free);
526 * Move the physical eraseblock to the protection queue where it will
527 * be protected from being moved for some time.
529 rb_erase(&e->u.rb, &ubi->free);
531 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
532 #ifndef CONFIG_MTD_UBI_FASTMAP
533 /* We have to enqueue e only if fastmap is disabled,
534 * is fastmap enabled prot_queue_add() will be called by
535 * ubi_wl_get_peb() after removing e from the pool. */
536 prot_queue_add(ubi, e);
541 #ifdef CONFIG_MTD_UBI_FASTMAP
543 * return_unused_pool_pebs - returns unused PEB to the free tree.
544 * @ubi: UBI device description object
545 * @pool: fastmap pool description object
547 static void return_unused_pool_pebs(struct ubi_device *ubi,
548 struct ubi_fm_pool *pool)
551 struct ubi_wl_entry *e;
553 for (i = pool->used; i < pool->size; i++) {
554 e = ubi->lookuptbl[pool->pebs[i]];
555 wl_tree_add(e, &ubi->free);
561 * refill_wl_pool - refills all the fastmap pool used by the
563 * @ubi: UBI device description object
565 static void refill_wl_pool(struct ubi_device *ubi)
567 struct ubi_wl_entry *e;
568 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
570 return_unused_pool_pebs(ubi, pool);
572 for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
573 if (!ubi->free.rb_node ||
574 (ubi->free_count - ubi->beb_rsvd_pebs < 5))
577 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
578 self_check_in_wl_tree(ubi, e, &ubi->free);
579 rb_erase(&e->u.rb, &ubi->free);
582 pool->pebs[pool->size] = e->pnum;
588 * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
589 * @ubi: UBI device description object
591 static void refill_wl_user_pool(struct ubi_device *ubi)
593 struct ubi_fm_pool *pool = &ubi->fm_pool;
595 return_unused_pool_pebs(ubi, pool);
597 for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
598 pool->pebs[pool->size] = __wl_get_peb(ubi);
599 if (pool->pebs[pool->size] < 0)
606 * ubi_refill_pools - refills all fastmap PEB pools.
607 * @ubi: UBI device description object
609 void ubi_refill_pools(struct ubi_device *ubi)
611 spin_lock(&ubi->wl_lock);
613 refill_wl_user_pool(ubi);
614 spin_unlock(&ubi->wl_lock);
617 /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
620 int ubi_wl_get_peb(struct ubi_device *ubi)
623 struct ubi_fm_pool *pool = &ubi->fm_pool;
624 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
626 if (!pool->size || !wl_pool->size || pool->used == pool->size ||
627 wl_pool->used == wl_pool->size)
628 ubi_update_fastmap(ubi);
630 /* we got not a single free PEB */
634 spin_lock(&ubi->wl_lock);
635 ret = pool->pebs[pool->used++];
636 prot_queue_add(ubi, ubi->lookuptbl[ret]);
637 spin_unlock(&ubi->wl_lock);
643 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
645 * @ubi: UBI device description object
647 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
649 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
652 if (pool->used == pool->size || !pool->size) {
653 /* We cannot update the fastmap here because this
654 * function is called in atomic context.
655 * Let's fail here and refill/update it as soon as possible. */
657 schedule_work(&ubi->fm_work);
659 /* In U-Boot we must call this directly */
660 ubi_update_fastmap(ubi);
664 pnum = pool->pebs[pool->used++];
665 return ubi->lookuptbl[pnum];
669 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
671 struct ubi_wl_entry *e;
673 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
674 self_check_in_wl_tree(ubi, e, &ubi->free);
675 rb_erase(&e->u.rb, &ubi->free);
680 int ubi_wl_get_peb(struct ubi_device *ubi)
684 spin_lock(&ubi->wl_lock);
685 peb = __wl_get_peb(ubi);
686 spin_unlock(&ubi->wl_lock);
688 err = ubi_self_check_all_ff(ubi, peb, ubi->vid_hdr_aloffset,
689 ubi->peb_size - ubi->vid_hdr_aloffset);
691 ubi_err("new PEB %d does not contain all 0xFF bytes", peb);
700 * prot_queue_del - remove a physical eraseblock from the protection queue.
701 * @ubi: UBI device description object
702 * @pnum: the physical eraseblock to remove
704 * This function deletes PEB @pnum from the protection queue and returns zero
705 * in case of success and %-ENODEV if the PEB was not found.
707 static int prot_queue_del(struct ubi_device *ubi, int pnum)
709 struct ubi_wl_entry *e;
711 e = ubi->lookuptbl[pnum];
715 if (self_check_in_pq(ubi, e))
718 list_del(&e->u.list);
719 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
724 * sync_erase - synchronously erase a physical eraseblock.
725 * @ubi: UBI device description object
726 * @e: the the physical eraseblock to erase
727 * @torture: if the physical eraseblock has to be tortured
729 * This function returns zero in case of success and a negative error code in
732 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
736 struct ubi_ec_hdr *ec_hdr;
737 unsigned long long ec = e->ec;
739 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
741 err = self_check_ec(ubi, e->pnum, e->ec);
745 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
749 err = ubi_io_sync_erase(ubi, e->pnum, torture);
754 if (ec > UBI_MAX_ERASECOUNTER) {
756 * Erase counter overflow. Upgrade UBI and use 64-bit
757 * erase counters internally.
759 ubi_err("erase counter overflow at PEB %d, EC %llu",
765 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
767 ec_hdr->ec = cpu_to_be64(ec);
769 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
774 spin_lock(&ubi->wl_lock);
775 if (e->ec > ubi->max_ec)
777 spin_unlock(&ubi->wl_lock);
785 * serve_prot_queue - check if it is time to stop protecting PEBs.
786 * @ubi: UBI device description object
788 * This function is called after each erase operation and removes PEBs from the
789 * tail of the protection queue. These PEBs have been protected for long enough
790 * and should be moved to the used tree.
792 static void serve_prot_queue(struct ubi_device *ubi)
794 struct ubi_wl_entry *e, *tmp;
798 * There may be several protected physical eraseblock to remove,
803 spin_lock(&ubi->wl_lock);
804 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
805 dbg_wl("PEB %d EC %d protection over, move to used tree",
808 list_del(&e->u.list);
809 wl_tree_add(e, &ubi->used);
812 * Let's be nice and avoid holding the spinlock for
815 spin_unlock(&ubi->wl_lock);
822 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
824 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
825 spin_unlock(&ubi->wl_lock);
829 * __schedule_ubi_work - schedule a work.
830 * @ubi: UBI device description object
831 * @wrk: the work to schedule
833 * This function adds a work defined by @wrk to the tail of the pending works
834 * list. Can only be used of ubi->work_sem is already held in read mode!
836 static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
838 spin_lock(&ubi->wl_lock);
839 list_add_tail(&wrk->list, &ubi->works);
840 ubi_assert(ubi->works_count >= 0);
841 ubi->works_count += 1;
843 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
844 wake_up_process(ubi->bgt_thread);
847 * U-Boot special: We have no bgt_thread in U-Boot!
848 * So just call do_work() here directly.
852 spin_unlock(&ubi->wl_lock);
856 * schedule_ubi_work - schedule a work.
857 * @ubi: UBI device description object
858 * @wrk: the work to schedule
860 * This function adds a work defined by @wrk to the tail of the pending works
863 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
865 down_read(&ubi->work_sem);
866 __schedule_ubi_work(ubi, wrk);
867 up_read(&ubi->work_sem);
870 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
873 #ifdef CONFIG_MTD_UBI_FASTMAP
875 * ubi_is_erase_work - checks whether a work is erase work.
876 * @wrk: The work object to be checked
878 int ubi_is_erase_work(struct ubi_work *wrk)
880 return wrk->func == erase_worker;
885 * schedule_erase - schedule an erase work.
886 * @ubi: UBI device description object
887 * @e: the WL entry of the physical eraseblock to erase
888 * @vol_id: the volume ID that last used this PEB
889 * @lnum: the last used logical eraseblock number for the PEB
890 * @torture: if the physical eraseblock has to be tortured
892 * This function returns zero in case of success and a %-ENOMEM in case of
895 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
896 int vol_id, int lnum, int torture)
898 struct ubi_work *wl_wrk;
901 ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
903 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
904 e->pnum, e->ec, torture);
906 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
910 wl_wrk->func = &erase_worker;
912 wl_wrk->vol_id = vol_id;
914 wl_wrk->torture = torture;
916 schedule_ubi_work(ubi, wl_wrk);
921 * do_sync_erase - run the erase worker synchronously.
922 * @ubi: UBI device description object
923 * @e: the WL entry of the physical eraseblock to erase
924 * @vol_id: the volume ID that last used this PEB
925 * @lnum: the last used logical eraseblock number for the PEB
926 * @torture: if the physical eraseblock has to be tortured
929 static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
930 int vol_id, int lnum, int torture)
932 struct ubi_work *wl_wrk;
934 dbg_wl("sync erase of PEB %i", e->pnum);
936 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
941 wl_wrk->vol_id = vol_id;
943 wl_wrk->torture = torture;
945 return erase_worker(ubi, wl_wrk, 0);
948 #ifdef CONFIG_MTD_UBI_FASTMAP
950 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
952 * see: ubi_wl_put_peb()
954 * @ubi: UBI device description object
955 * @fm_e: physical eraseblock to return
956 * @lnum: the last used logical eraseblock number for the PEB
957 * @torture: if this physical eraseblock has to be tortured
959 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
960 int lnum, int torture)
962 struct ubi_wl_entry *e;
963 int vol_id, pnum = fm_e->pnum;
965 dbg_wl("PEB %d", pnum);
967 ubi_assert(pnum >= 0);
968 ubi_assert(pnum < ubi->peb_count);
970 spin_lock(&ubi->wl_lock);
971 e = ubi->lookuptbl[pnum];
973 /* This can happen if we recovered from a fastmap the very
974 * first time and writing now a new one. In this case the wl system
975 * has never seen any PEB used by the original fastmap.
979 ubi_assert(e->ec >= 0);
980 ubi->lookuptbl[pnum] = e;
986 spin_unlock(&ubi->wl_lock);
988 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
989 return schedule_erase(ubi, e, vol_id, lnum, torture);
994 * wear_leveling_worker - wear-leveling worker function.
995 * @ubi: UBI device description object
996 * @wrk: the work object
997 * @cancel: non-zero if the worker has to free memory and exit
999 * This function copies a more worn out physical eraseblock to a less worn out
1000 * one. Returns zero in case of success and a negative error code in case of
1003 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
1006 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
1007 int vol_id = -1, uninitialized_var(lnum);
1008 #ifdef CONFIG_MTD_UBI_FASTMAP
1009 int anchor = wrk->anchor;
1011 struct ubi_wl_entry *e1, *e2;
1012 struct ubi_vid_hdr *vid_hdr;
1018 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1022 mutex_lock(&ubi->move_mutex);
1023 spin_lock(&ubi->wl_lock);
1024 ubi_assert(!ubi->move_from && !ubi->move_to);
1025 ubi_assert(!ubi->move_to_put);
1027 if (!ubi->free.rb_node ||
1028 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
1030 * No free physical eraseblocks? Well, they must be waiting in
1031 * the queue to be erased. Cancel movement - it will be
1032 * triggered again when a free physical eraseblock appears.
1034 * No used physical eraseblocks? They must be temporarily
1035 * protected from being moved. They will be moved to the
1036 * @ubi->used tree later and the wear-leveling will be
1039 dbg_wl("cancel WL, a list is empty: free %d, used %d",
1040 !ubi->free.rb_node, !ubi->used.rb_node);
1044 #ifdef CONFIG_MTD_UBI_FASTMAP
1045 /* Check whether we need to produce an anchor PEB */
1047 anchor = !anchor_pebs_avalible(&ubi->free);
1050 e1 = find_anchor_wl_entry(&ubi->used);
1053 e2 = get_peb_for_wl(ubi);
1057 self_check_in_wl_tree(ubi, e1, &ubi->used);
1058 rb_erase(&e1->u.rb, &ubi->used);
1059 dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
1060 } else if (!ubi->scrub.rb_node) {
1062 if (!ubi->scrub.rb_node) {
1065 * Now pick the least worn-out used physical eraseblock and a
1066 * highly worn-out free physical eraseblock. If the erase
1067 * counters differ much enough, start wear-leveling.
1069 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1070 e2 = get_peb_for_wl(ubi);
1074 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
1075 dbg_wl("no WL needed: min used EC %d, max free EC %d",
1078 /* Give the unused PEB back */
1079 wl_tree_add(e2, &ubi->free);
1082 self_check_in_wl_tree(ubi, e1, &ubi->used);
1083 rb_erase(&e1->u.rb, &ubi->used);
1084 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
1085 e1->pnum, e1->ec, e2->pnum, e2->ec);
1087 /* Perform scrubbing */
1089 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
1090 e2 = get_peb_for_wl(ubi);
1094 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
1095 rb_erase(&e1->u.rb, &ubi->scrub);
1096 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
1099 ubi->move_from = e1;
1101 spin_unlock(&ubi->wl_lock);
1104 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
1105 * We so far do not know which logical eraseblock our physical
1106 * eraseblock (@e1) belongs to. We have to read the volume identifier
1109 * Note, we are protected from this PEB being unmapped and erased. The
1110 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
1111 * which is being moved was unmapped.
1114 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
1115 if (err && err != UBI_IO_BITFLIPS) {
1116 if (err == UBI_IO_FF) {
1118 * We are trying to move PEB without a VID header. UBI
1119 * always write VID headers shortly after the PEB was
1120 * given, so we have a situation when it has not yet
1121 * had a chance to write it, because it was preempted.
1122 * So add this PEB to the protection queue so far,
1123 * because presumably more data will be written there
1124 * (including the missing VID header), and then we'll
1127 dbg_wl("PEB %d has no VID header", e1->pnum);
1130 } else if (err == UBI_IO_FF_BITFLIPS) {
1132 * The same situation as %UBI_IO_FF, but bit-flips were
1133 * detected. It is better to schedule this PEB for
1136 dbg_wl("PEB %d has no VID header but has bit-flips",
1142 ubi_err("error %d while reading VID header from PEB %d",
1147 vol_id = be32_to_cpu(vid_hdr->vol_id);
1148 lnum = be32_to_cpu(vid_hdr->lnum);
1150 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
1152 if (err == MOVE_CANCEL_RACE) {
1154 * The LEB has not been moved because the volume is
1155 * being deleted or the PEB has been put meanwhile. We
1156 * should prevent this PEB from being selected for
1157 * wear-leveling movement again, so put it to the
1163 if (err == MOVE_RETRY) {
1167 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
1168 err == MOVE_TARGET_RD_ERR) {
1170 * Target PEB had bit-flips or write error - torture it.
1176 if (err == MOVE_SOURCE_RD_ERR) {
1178 * An error happened while reading the source PEB. Do
1179 * not switch to R/O mode in this case, and give the
1180 * upper layers a possibility to recover from this,
1181 * e.g. by unmapping corresponding LEB. Instead, just
1182 * put this PEB to the @ubi->erroneous list to prevent
1183 * UBI from trying to move it over and over again.
1185 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
1186 ubi_err("too many erroneous eraseblocks (%d)",
1187 ubi->erroneous_peb_count);
1200 /* The PEB has been successfully moved */
1202 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
1203 e1->pnum, vol_id, lnum, e2->pnum);
1204 ubi_free_vid_hdr(ubi, vid_hdr);
1206 spin_lock(&ubi->wl_lock);
1207 if (!ubi->move_to_put) {
1208 wl_tree_add(e2, &ubi->used);
1211 ubi->move_from = ubi->move_to = NULL;
1212 ubi->move_to_put = ubi->wl_scheduled = 0;
1213 spin_unlock(&ubi->wl_lock);
1215 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
1217 kmem_cache_free(ubi_wl_entry_slab, e1);
1219 kmem_cache_free(ubi_wl_entry_slab, e2);
1225 * Well, the target PEB was put meanwhile, schedule it for
1228 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
1229 e2->pnum, vol_id, lnum);
1230 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
1232 kmem_cache_free(ubi_wl_entry_slab, e2);
1238 mutex_unlock(&ubi->move_mutex);
1242 * For some reasons the LEB was not moved, might be an error, might be
1243 * something else. @e1 was not changed, so return it back. @e2 might
1244 * have been changed, schedule it for erasure.
1248 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
1249 e1->pnum, vol_id, lnum, e2->pnum, err);
1251 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
1252 e1->pnum, e2->pnum, err);
1253 spin_lock(&ubi->wl_lock);
1255 prot_queue_add(ubi, e1);
1256 else if (erroneous) {
1257 wl_tree_add(e1, &ubi->erroneous);
1258 ubi->erroneous_peb_count += 1;
1259 } else if (scrubbing)
1260 wl_tree_add(e1, &ubi->scrub);
1262 wl_tree_add(e1, &ubi->used);
1263 ubi_assert(!ubi->move_to_put);
1264 ubi->move_from = ubi->move_to = NULL;
1265 ubi->wl_scheduled = 0;
1266 spin_unlock(&ubi->wl_lock);
1268 ubi_free_vid_hdr(ubi, vid_hdr);
1269 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
1271 kmem_cache_free(ubi_wl_entry_slab, e2);
1274 mutex_unlock(&ubi->move_mutex);
1279 ubi_err("error %d while moving PEB %d to PEB %d",
1280 err, e1->pnum, e2->pnum);
1282 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
1283 err, e1->pnum, vol_id, lnum, e2->pnum);
1284 spin_lock(&ubi->wl_lock);
1285 ubi->move_from = ubi->move_to = NULL;
1286 ubi->move_to_put = ubi->wl_scheduled = 0;
1287 spin_unlock(&ubi->wl_lock);
1289 ubi_free_vid_hdr(ubi, vid_hdr);
1290 kmem_cache_free(ubi_wl_entry_slab, e1);
1291 kmem_cache_free(ubi_wl_entry_slab, e2);
1295 mutex_unlock(&ubi->move_mutex);
1296 ubi_assert(err != 0);
1297 return err < 0 ? err : -EIO;
1300 ubi->wl_scheduled = 0;
1301 spin_unlock(&ubi->wl_lock);
1302 mutex_unlock(&ubi->move_mutex);
1303 ubi_free_vid_hdr(ubi, vid_hdr);
1308 * ensure_wear_leveling - schedule wear-leveling if it is needed.
1309 * @ubi: UBI device description object
1310 * @nested: set to non-zero if this function is called from UBI worker
1312 * This function checks if it is time to start wear-leveling and schedules it
1313 * if yes. This function returns zero in case of success and a negative error
1314 * code in case of failure.
1316 static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
1319 struct ubi_wl_entry *e1;
1320 struct ubi_wl_entry *e2;
1321 struct ubi_work *wrk;
1323 spin_lock(&ubi->wl_lock);
1324 if (ubi->wl_scheduled)
1325 /* Wear-leveling is already in the work queue */
1329 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1330 * the WL worker has to be scheduled anyway.
1332 if (!ubi->scrub.rb_node) {
1333 if (!ubi->used.rb_node || !ubi->free.rb_node)
1334 /* No physical eraseblocks - no deal */
1338 * We schedule wear-leveling only if the difference between the
1339 * lowest erase counter of used physical eraseblocks and a high
1340 * erase counter of free physical eraseblocks is greater than
1341 * %UBI_WL_THRESHOLD.
1343 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1344 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1346 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
1348 dbg_wl("schedule wear-leveling");
1350 dbg_wl("schedule scrubbing");
1352 ubi->wl_scheduled = 1;
1353 spin_unlock(&ubi->wl_lock);
1355 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1362 wrk->func = &wear_leveling_worker;
1364 __schedule_ubi_work(ubi, wrk);
1366 schedule_ubi_work(ubi, wrk);
1370 spin_lock(&ubi->wl_lock);
1371 ubi->wl_scheduled = 0;
1373 spin_unlock(&ubi->wl_lock);
1377 #ifdef CONFIG_MTD_UBI_FASTMAP
1379 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
1380 * @ubi: UBI device description object
1382 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
1384 struct ubi_work *wrk;
1386 spin_lock(&ubi->wl_lock);
1387 if (ubi->wl_scheduled) {
1388 spin_unlock(&ubi->wl_lock);
1391 ubi->wl_scheduled = 1;
1392 spin_unlock(&ubi->wl_lock);
1394 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1396 spin_lock(&ubi->wl_lock);
1397 ubi->wl_scheduled = 0;
1398 spin_unlock(&ubi->wl_lock);
1403 wrk->func = &wear_leveling_worker;
1404 schedule_ubi_work(ubi, wrk);
1410 * erase_worker - physical eraseblock erase worker function.
1411 * @ubi: UBI device description object
1412 * @wl_wrk: the work object
1413 * @cancel: non-zero if the worker has to free memory and exit
1415 * This function erases a physical eraseblock and perform torture testing if
1416 * needed. It also takes care about marking the physical eraseblock bad if
1417 * needed. Returns zero in case of success and a negative error code in case of
1420 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1423 struct ubi_wl_entry *e = wl_wrk->e;
1425 int vol_id = wl_wrk->vol_id;
1426 int lnum = wl_wrk->lnum;
1427 int err, available_consumed = 0;
1430 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1432 kmem_cache_free(ubi_wl_entry_slab, e);
1436 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1437 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1439 ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1441 err = sync_erase(ubi, e, wl_wrk->torture);
1443 /* Fine, we've erased it successfully */
1446 spin_lock(&ubi->wl_lock);
1447 wl_tree_add(e, &ubi->free);
1449 spin_unlock(&ubi->wl_lock);
1452 * One more erase operation has happened, take care about
1453 * protected physical eraseblocks.
1455 serve_prot_queue(ubi);
1457 /* And take care about wear-leveling */
1458 err = ensure_wear_leveling(ubi, 1);
1462 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1465 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1469 /* Re-schedule the LEB for erasure */
1470 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
1478 kmem_cache_free(ubi_wl_entry_slab, e);
1481 * If this is not %-EIO, we have no idea what to do. Scheduling
1482 * this physical eraseblock for erasure again would cause
1483 * errors again and again. Well, lets switch to R/O mode.
1487 /* It is %-EIO, the PEB went bad */
1489 if (!ubi->bad_allowed) {
1490 ubi_err("bad physical eraseblock %d detected", pnum);
1494 spin_lock(&ubi->volumes_lock);
1495 if (ubi->beb_rsvd_pebs == 0) {
1496 if (ubi->avail_pebs == 0) {
1497 spin_unlock(&ubi->volumes_lock);
1498 ubi_err("no reserved/available physical eraseblocks");
1501 ubi->avail_pebs -= 1;
1502 available_consumed = 1;
1504 spin_unlock(&ubi->volumes_lock);
1506 ubi_msg("mark PEB %d as bad", pnum);
1507 err = ubi_io_mark_bad(ubi, pnum);
1511 spin_lock(&ubi->volumes_lock);
1512 if (ubi->beb_rsvd_pebs > 0) {
1513 if (available_consumed) {
1515 * The amount of reserved PEBs increased since we last
1518 ubi->avail_pebs += 1;
1519 available_consumed = 0;
1521 ubi->beb_rsvd_pebs -= 1;
1523 ubi->bad_peb_count += 1;
1524 ubi->good_peb_count -= 1;
1525 ubi_calculate_reserved(ubi);
1526 if (available_consumed)
1527 ubi_warn("no PEBs in the reserved pool, used an available PEB");
1528 else if (ubi->beb_rsvd_pebs)
1529 ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1531 ubi_warn("last PEB from the reserve was used");
1532 spin_unlock(&ubi->volumes_lock);
1537 if (available_consumed) {
1538 spin_lock(&ubi->volumes_lock);
1539 ubi->avail_pebs += 1;
1540 spin_unlock(&ubi->volumes_lock);
1547 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1548 * @ubi: UBI device description object
1549 * @vol_id: the volume ID that last used this PEB
1550 * @lnum: the last used logical eraseblock number for the PEB
1551 * @pnum: physical eraseblock to return
1552 * @torture: if this physical eraseblock has to be tortured
1554 * This function is called to return physical eraseblock @pnum to the pool of
1555 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1556 * occurred to this @pnum and it has to be tested. This function returns zero
1557 * in case of success, and a negative error code in case of failure.
1559 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1560 int pnum, int torture)
1563 struct ubi_wl_entry *e;
1565 dbg_wl("PEB %d", pnum);
1566 ubi_assert(pnum >= 0);
1567 ubi_assert(pnum < ubi->peb_count);
1570 spin_lock(&ubi->wl_lock);
1571 e = ubi->lookuptbl[pnum];
1572 if (e == ubi->move_from) {
1574 * User is putting the physical eraseblock which was selected to
1575 * be moved. It will be scheduled for erasure in the
1576 * wear-leveling worker.
1578 dbg_wl("PEB %d is being moved, wait", pnum);
1579 spin_unlock(&ubi->wl_lock);
1581 /* Wait for the WL worker by taking the @ubi->move_mutex */
1582 mutex_lock(&ubi->move_mutex);
1583 mutex_unlock(&ubi->move_mutex);
1585 } else if (e == ubi->move_to) {
1587 * User is putting the physical eraseblock which was selected
1588 * as the target the data is moved to. It may happen if the EBA
1589 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1590 * but the WL sub-system has not put the PEB to the "used" tree
1591 * yet, but it is about to do this. So we just set a flag which
1592 * will tell the WL worker that the PEB is not needed anymore
1593 * and should be scheduled for erasure.
1595 dbg_wl("PEB %d is the target of data moving", pnum);
1596 ubi_assert(!ubi->move_to_put);
1597 ubi->move_to_put = 1;
1598 spin_unlock(&ubi->wl_lock);
1601 if (in_wl_tree(e, &ubi->used)) {
1602 self_check_in_wl_tree(ubi, e, &ubi->used);
1603 rb_erase(&e->u.rb, &ubi->used);
1604 } else if (in_wl_tree(e, &ubi->scrub)) {
1605 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1606 rb_erase(&e->u.rb, &ubi->scrub);
1607 } else if (in_wl_tree(e, &ubi->erroneous)) {
1608 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1609 rb_erase(&e->u.rb, &ubi->erroneous);
1610 ubi->erroneous_peb_count -= 1;
1611 ubi_assert(ubi->erroneous_peb_count >= 0);
1612 /* Erroneous PEBs should be tortured */
1615 err = prot_queue_del(ubi, e->pnum);
1617 ubi_err("PEB %d not found", pnum);
1619 spin_unlock(&ubi->wl_lock);
1624 spin_unlock(&ubi->wl_lock);
1626 err = schedule_erase(ubi, e, vol_id, lnum, torture);
1628 spin_lock(&ubi->wl_lock);
1629 wl_tree_add(e, &ubi->used);
1630 spin_unlock(&ubi->wl_lock);
1637 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1638 * @ubi: UBI device description object
1639 * @pnum: the physical eraseblock to schedule
1641 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1642 * needs scrubbing. This function schedules a physical eraseblock for
1643 * scrubbing which is done in background. This function returns zero in case of
1644 * success and a negative error code in case of failure.
1646 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1648 struct ubi_wl_entry *e;
1650 ubi_msg("schedule PEB %d for scrubbing", pnum);
1653 spin_lock(&ubi->wl_lock);
1654 e = ubi->lookuptbl[pnum];
1655 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1656 in_wl_tree(e, &ubi->erroneous)) {
1657 spin_unlock(&ubi->wl_lock);
1661 if (e == ubi->move_to) {
1663 * This physical eraseblock was used to move data to. The data
1664 * was moved but the PEB was not yet inserted to the proper
1665 * tree. We should just wait a little and let the WL worker
1668 spin_unlock(&ubi->wl_lock);
1669 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1674 if (in_wl_tree(e, &ubi->used)) {
1675 self_check_in_wl_tree(ubi, e, &ubi->used);
1676 rb_erase(&e->u.rb, &ubi->used);
1680 err = prot_queue_del(ubi, e->pnum);
1682 ubi_err("PEB %d not found", pnum);
1684 spin_unlock(&ubi->wl_lock);
1689 wl_tree_add(e, &ubi->scrub);
1690 spin_unlock(&ubi->wl_lock);
1693 * Technically scrubbing is the same as wear-leveling, so it is done
1696 return ensure_wear_leveling(ubi, 0);
1700 * ubi_wl_flush - flush all pending works.
1701 * @ubi: UBI device description object
1702 * @vol_id: the volume id to flush for
1703 * @lnum: the logical eraseblock number to flush for
1705 * This function executes all pending works for a particular volume id /
1706 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1707 * acts as a wildcard for all of the corresponding volume numbers or logical
1708 * eraseblock numbers. It returns zero in case of success and a negative error
1709 * code in case of failure.
1711 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
1717 * Erase while the pending works queue is not empty, but not more than
1718 * the number of currently pending works.
1720 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1721 vol_id, lnum, ubi->works_count);
1724 struct ubi_work *wrk;
1727 down_read(&ubi->work_sem);
1728 spin_lock(&ubi->wl_lock);
1729 list_for_each_entry(wrk, &ubi->works, list) {
1730 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1731 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1732 list_del(&wrk->list);
1733 ubi->works_count -= 1;
1734 ubi_assert(ubi->works_count >= 0);
1735 spin_unlock(&ubi->wl_lock);
1737 err = wrk->func(ubi, wrk, 0);
1739 up_read(&ubi->work_sem);
1743 spin_lock(&ubi->wl_lock);
1748 spin_unlock(&ubi->wl_lock);
1749 up_read(&ubi->work_sem);
1753 * Make sure all the works which have been done in parallel are
1756 down_write(&ubi->work_sem);
1757 up_write(&ubi->work_sem);
1763 * tree_destroy - destroy an RB-tree.
1764 * @root: the root of the tree to destroy
1766 static void tree_destroy(struct rb_root *root)
1769 struct ubi_wl_entry *e;
1775 else if (rb->rb_right)
1778 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
1782 if (rb->rb_left == &e->u.rb)
1785 rb->rb_right = NULL;
1788 kmem_cache_free(ubi_wl_entry_slab, e);
1794 * ubi_thread - UBI background thread.
1795 * @u: the UBI device description object pointer
1797 int ubi_thread(void *u)
1800 struct ubi_device *ubi = u;
1802 ubi_msg("background thread \"%s\" started, PID %d",
1803 ubi->bgt_name, task_pid_nr(current));
1809 if (kthread_should_stop())
1812 if (try_to_freeze())
1815 spin_lock(&ubi->wl_lock);
1816 if (list_empty(&ubi->works) || ubi->ro_mode ||
1817 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1818 set_current_state(TASK_INTERRUPTIBLE);
1819 spin_unlock(&ubi->wl_lock);
1823 spin_unlock(&ubi->wl_lock);
1827 ubi_err("%s: work failed with error code %d",
1828 ubi->bgt_name, err);
1829 if (failures++ > WL_MAX_FAILURES) {
1831 * Too many failures, disable the thread and
1832 * switch to read-only mode.
1834 ubi_msg("%s: %d consecutive failures",
1835 ubi->bgt_name, WL_MAX_FAILURES);
1837 ubi->thread_enabled = 0;
1846 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1851 * cancel_pending - cancel all pending works.
1852 * @ubi: UBI device description object
1854 static void cancel_pending(struct ubi_device *ubi)
1856 while (!list_empty(&ubi->works)) {
1857 struct ubi_work *wrk;
1859 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1860 list_del(&wrk->list);
1861 wrk->func(ubi, wrk, 1);
1862 ubi->works_count -= 1;
1863 ubi_assert(ubi->works_count >= 0);
1868 * ubi_wl_init - initialize the WL sub-system using attaching information.
1869 * @ubi: UBI device description object
1870 * @ai: attaching information
1872 * This function returns zero in case of success, and a negative error code in
1875 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1877 int err, i, reserved_pebs, found_pebs = 0;
1878 struct rb_node *rb1, *rb2;
1879 struct ubi_ainf_volume *av;
1880 struct ubi_ainf_peb *aeb, *tmp;
1881 struct ubi_wl_entry *e;
1883 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1884 spin_lock_init(&ubi->wl_lock);
1885 mutex_init(&ubi->move_mutex);
1886 init_rwsem(&ubi->work_sem);
1887 ubi->max_ec = ai->max_ec;
1888 INIT_LIST_HEAD(&ubi->works);
1890 #ifdef CONFIG_MTD_UBI_FASTMAP
1891 INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
1895 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1898 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1899 if (!ubi->lookuptbl)
1902 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1903 INIT_LIST_HEAD(&ubi->pq[i]);
1906 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
1909 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1913 e->pnum = aeb->pnum;
1915 ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1916 ubi->lookuptbl[e->pnum] = e;
1917 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
1918 kmem_cache_free(ubi_wl_entry_slab, e);
1925 ubi->free_count = 0;
1926 list_for_each_entry(aeb, &ai->free, u.list) {
1929 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1933 e->pnum = aeb->pnum;
1935 ubi_assert(e->ec >= 0);
1936 ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1938 wl_tree_add(e, &ubi->free);
1941 ubi->lookuptbl[e->pnum] = e;
1946 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1947 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1950 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1954 e->pnum = aeb->pnum;
1956 ubi->lookuptbl[e->pnum] = e;
1959 dbg_wl("add PEB %d EC %d to the used tree",
1961 wl_tree_add(e, &ubi->used);
1963 dbg_wl("add PEB %d EC %d to the scrub tree",
1965 wl_tree_add(e, &ubi->scrub);
1972 dbg_wl("found %i PEBs", found_pebs);
1975 ubi_assert(ubi->good_peb_count == \
1976 found_pebs + ubi->fm->used_blocks);
1978 ubi_assert(ubi->good_peb_count == found_pebs);
1980 reserved_pebs = WL_RESERVED_PEBS;
1981 #ifdef CONFIG_MTD_UBI_FASTMAP
1982 /* Reserve enough LEBs to store two fastmaps. */
1983 reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
1986 if (ubi->avail_pebs < reserved_pebs) {
1987 ubi_err("no enough physical eraseblocks (%d, need %d)",
1988 ubi->avail_pebs, reserved_pebs);
1989 if (ubi->corr_peb_count)
1990 ubi_err("%d PEBs are corrupted and not used",
1991 ubi->corr_peb_count);
1994 ubi->avail_pebs -= reserved_pebs;
1995 ubi->rsvd_pebs += reserved_pebs;
1997 /* Schedule wear-leveling if needed */
1998 err = ensure_wear_leveling(ubi, 0);
2005 cancel_pending(ubi);
2006 tree_destroy(&ubi->used);
2007 tree_destroy(&ubi->free);
2008 tree_destroy(&ubi->scrub);
2009 kfree(ubi->lookuptbl);
2014 * protection_queue_destroy - destroy the protection queue.
2015 * @ubi: UBI device description object
2017 static void protection_queue_destroy(struct ubi_device *ubi)
2020 struct ubi_wl_entry *e, *tmp;
2022 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
2023 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
2024 list_del(&e->u.list);
2025 kmem_cache_free(ubi_wl_entry_slab, e);
2031 * ubi_wl_close - close the wear-leveling sub-system.
2032 * @ubi: UBI device description object
2034 void ubi_wl_close(struct ubi_device *ubi)
2036 dbg_wl("close the WL sub-system");
2037 cancel_pending(ubi);
2038 protection_queue_destroy(ubi);
2039 tree_destroy(&ubi->used);
2040 tree_destroy(&ubi->erroneous);
2041 tree_destroy(&ubi->free);
2042 tree_destroy(&ubi->scrub);
2043 kfree(ubi->lookuptbl);
2047 * self_check_ec - make sure that the erase counter of a PEB is correct.
2048 * @ubi: UBI device description object
2049 * @pnum: the physical eraseblock number to check
2050 * @ec: the erase counter to check
2052 * This function returns zero if the erase counter of physical eraseblock @pnum
2053 * is equivalent to @ec, and a negative error code if not or if an error
2056 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
2060 struct ubi_ec_hdr *ec_hdr;
2062 if (!ubi_dbg_chk_gen(ubi))
2065 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
2069 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
2070 if (err && err != UBI_IO_BITFLIPS) {
2071 /* The header does not have to exist */
2076 read_ec = be64_to_cpu(ec_hdr->ec);
2077 if (ec != read_ec && read_ec - ec > 1) {
2078 ubi_err("self-check failed for PEB %d", pnum);
2079 ubi_err("read EC is %lld, should be %d", read_ec, ec);
2091 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
2092 * @ubi: UBI device description object
2093 * @e: the wear-leveling entry to check
2094 * @root: the root of the tree
2096 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
2099 static int self_check_in_wl_tree(const struct ubi_device *ubi,
2100 struct ubi_wl_entry *e, struct rb_root *root)
2102 if (!ubi_dbg_chk_gen(ubi))
2105 if (in_wl_tree(e, root))
2108 ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
2109 e->pnum, e->ec, root);
2115 * self_check_in_pq - check if wear-leveling entry is in the protection
2117 * @ubi: UBI device description object
2118 * @e: the wear-leveling entry to check
2120 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2122 static int self_check_in_pq(const struct ubi_device *ubi,
2123 struct ubi_wl_entry *e)
2125 struct ubi_wl_entry *p;
2128 if (!ubi_dbg_chk_gen(ubi))
2131 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
2132 list_for_each_entry(p, &ubi->pq[i], u.list)
2136 ubi_err("self-check failed for PEB %d, EC %d, Protect queue",