]>
git.ipfire.org Git - people/ms/u-boot.git/blob - drivers/mtd/ubi/wl.c
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
);
676 ubi_assert(ubi
->free_count
>= 0);
677 rb_erase(&e
->u
.rb
, &ubi
->free
);
682 int ubi_wl_get_peb(struct ubi_device
*ubi
)
686 spin_lock(&ubi
->wl_lock
);
687 peb
= __wl_get_peb(ubi
);
688 spin_unlock(&ubi
->wl_lock
);
693 err
= ubi_self_check_all_ff(ubi
, peb
, ubi
->vid_hdr_aloffset
,
694 ubi
->peb_size
- ubi
->vid_hdr_aloffset
);
696 ubi_err("new PEB %d does not contain all 0xFF bytes", peb
);
705 * prot_queue_del - remove a physical eraseblock from the protection queue.
706 * @ubi: UBI device description object
707 * @pnum: the physical eraseblock to remove
709 * This function deletes PEB @pnum from the protection queue and returns zero
710 * in case of success and %-ENODEV if the PEB was not found.
712 static int prot_queue_del(struct ubi_device
*ubi
, int pnum
)
714 struct ubi_wl_entry
*e
;
716 e
= ubi
->lookuptbl
[pnum
];
720 if (self_check_in_pq(ubi
, e
))
723 list_del(&e
->u
.list
);
724 dbg_wl("deleted PEB %d from the protection queue", e
->pnum
);
729 * sync_erase - synchronously erase a physical eraseblock.
730 * @ubi: UBI device description object
731 * @e: the the physical eraseblock to erase
732 * @torture: if the physical eraseblock has to be tortured
734 * This function returns zero in case of success and a negative error code in
737 static int sync_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
741 struct ubi_ec_hdr
*ec_hdr
;
742 unsigned long long ec
= e
->ec
;
744 dbg_wl("erase PEB %d, old EC %llu", e
->pnum
, ec
);
746 err
= self_check_ec(ubi
, e
->pnum
, e
->ec
);
750 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_NOFS
);
754 err
= ubi_io_sync_erase(ubi
, e
->pnum
, torture
);
759 if (ec
> UBI_MAX_ERASECOUNTER
) {
761 * Erase counter overflow. Upgrade UBI and use 64-bit
762 * erase counters internally.
764 ubi_err("erase counter overflow at PEB %d, EC %llu",
770 dbg_wl("erased PEB %d, new EC %llu", e
->pnum
, ec
);
772 ec_hdr
->ec
= cpu_to_be64(ec
);
774 err
= ubi_io_write_ec_hdr(ubi
, e
->pnum
, ec_hdr
);
779 spin_lock(&ubi
->wl_lock
);
780 if (e
->ec
> ubi
->max_ec
)
782 spin_unlock(&ubi
->wl_lock
);
790 * serve_prot_queue - check if it is time to stop protecting PEBs.
791 * @ubi: UBI device description object
793 * This function is called after each erase operation and removes PEBs from the
794 * tail of the protection queue. These PEBs have been protected for long enough
795 * and should be moved to the used tree.
797 static void serve_prot_queue(struct ubi_device
*ubi
)
799 struct ubi_wl_entry
*e
, *tmp
;
803 * There may be several protected physical eraseblock to remove,
808 spin_lock(&ubi
->wl_lock
);
809 list_for_each_entry_safe(e
, tmp
, &ubi
->pq
[ubi
->pq_head
], u
.list
) {
810 dbg_wl("PEB %d EC %d protection over, move to used tree",
813 list_del(&e
->u
.list
);
814 wl_tree_add(e
, &ubi
->used
);
817 * Let's be nice and avoid holding the spinlock for
820 spin_unlock(&ubi
->wl_lock
);
827 if (ubi
->pq_head
== UBI_PROT_QUEUE_LEN
)
829 ubi_assert(ubi
->pq_head
>= 0 && ubi
->pq_head
< UBI_PROT_QUEUE_LEN
);
830 spin_unlock(&ubi
->wl_lock
);
834 * __schedule_ubi_work - schedule a work.
835 * @ubi: UBI device description object
836 * @wrk: the work to schedule
838 * This function adds a work defined by @wrk to the tail of the pending works
839 * list. Can only be used of ubi->work_sem is already held in read mode!
841 static void __schedule_ubi_work(struct ubi_device
*ubi
, struct ubi_work
*wrk
)
843 spin_lock(&ubi
->wl_lock
);
844 list_add_tail(&wrk
->list
, &ubi
->works
);
845 ubi_assert(ubi
->works_count
>= 0);
846 ubi
->works_count
+= 1;
848 if (ubi
->thread_enabled
&& !ubi_dbg_is_bgt_disabled(ubi
))
849 wake_up_process(ubi
->bgt_thread
);
852 * U-Boot special: We have no bgt_thread in U-Boot!
853 * So just call do_work() here directly.
857 spin_unlock(&ubi
->wl_lock
);
861 * schedule_ubi_work - schedule a work.
862 * @ubi: UBI device description object
863 * @wrk: the work to schedule
865 * This function adds a work defined by @wrk to the tail of the pending works
868 static void schedule_ubi_work(struct ubi_device
*ubi
, struct ubi_work
*wrk
)
870 down_read(&ubi
->work_sem
);
871 __schedule_ubi_work(ubi
, wrk
);
872 up_read(&ubi
->work_sem
);
875 static int erase_worker(struct ubi_device
*ubi
, struct ubi_work
*wl_wrk
,
878 #ifdef CONFIG_MTD_UBI_FASTMAP
880 * ubi_is_erase_work - checks whether a work is erase work.
881 * @wrk: The work object to be checked
883 int ubi_is_erase_work(struct ubi_work
*wrk
)
885 return wrk
->func
== erase_worker
;
890 * schedule_erase - schedule an erase work.
891 * @ubi: UBI device description object
892 * @e: the WL entry of the physical eraseblock to erase
893 * @vol_id: the volume ID that last used this PEB
894 * @lnum: the last used logical eraseblock number for the PEB
895 * @torture: if the physical eraseblock has to be tortured
897 * This function returns zero in case of success and a %-ENOMEM in case of
900 static int schedule_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
901 int vol_id
, int lnum
, int torture
)
903 struct ubi_work
*wl_wrk
;
906 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
908 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
909 e
->pnum
, e
->ec
, torture
);
911 wl_wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
915 wl_wrk
->func
= &erase_worker
;
917 wl_wrk
->vol_id
= vol_id
;
919 wl_wrk
->torture
= torture
;
921 schedule_ubi_work(ubi
, wl_wrk
);
926 * do_sync_erase - run the erase worker synchronously.
927 * @ubi: UBI device description object
928 * @e: the WL entry of the physical eraseblock to erase
929 * @vol_id: the volume ID that last used this PEB
930 * @lnum: the last used logical eraseblock number for the PEB
931 * @torture: if the physical eraseblock has to be tortured
934 static int do_sync_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
935 int vol_id
, int lnum
, int torture
)
937 struct ubi_work
*wl_wrk
;
939 dbg_wl("sync erase of PEB %i", e
->pnum
);
941 wl_wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
946 wl_wrk
->vol_id
= vol_id
;
948 wl_wrk
->torture
= torture
;
950 return erase_worker(ubi
, wl_wrk
, 0);
953 #ifdef CONFIG_MTD_UBI_FASTMAP
955 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
957 * see: ubi_wl_put_peb()
959 * @ubi: UBI device description object
960 * @fm_e: physical eraseblock to return
961 * @lnum: the last used logical eraseblock number for the PEB
962 * @torture: if this physical eraseblock has to be tortured
964 int ubi_wl_put_fm_peb(struct ubi_device
*ubi
, struct ubi_wl_entry
*fm_e
,
965 int lnum
, int torture
)
967 struct ubi_wl_entry
*e
;
968 int vol_id
, pnum
= fm_e
->pnum
;
970 dbg_wl("PEB %d", pnum
);
972 ubi_assert(pnum
>= 0);
973 ubi_assert(pnum
< ubi
->peb_count
);
975 spin_lock(&ubi
->wl_lock
);
976 e
= ubi
->lookuptbl
[pnum
];
978 /* This can happen if we recovered from a fastmap the very
979 * first time and writing now a new one. In this case the wl system
980 * has never seen any PEB used by the original fastmap.
984 ubi_assert(e
->ec
>= 0);
985 ubi
->lookuptbl
[pnum
] = e
;
991 spin_unlock(&ubi
->wl_lock
);
993 vol_id
= lnum
? UBI_FM_DATA_VOLUME_ID
: UBI_FM_SB_VOLUME_ID
;
994 return schedule_erase(ubi
, e
, vol_id
, lnum
, torture
);
999 * wear_leveling_worker - wear-leveling worker function.
1000 * @ubi: UBI device description object
1001 * @wrk: the work object
1002 * @cancel: non-zero if the worker has to free memory and exit
1004 * This function copies a more worn out physical eraseblock to a less worn out
1005 * one. Returns zero in case of success and a negative error code in case of
1008 static int wear_leveling_worker(struct ubi_device
*ubi
, struct ubi_work
*wrk
,
1011 int err
, scrubbing
= 0, torture
= 0, protect
= 0, erroneous
= 0;
1012 int vol_id
= -1, uninitialized_var(lnum
);
1013 #ifdef CONFIG_MTD_UBI_FASTMAP
1014 int anchor
= wrk
->anchor
;
1016 struct ubi_wl_entry
*e1
, *e2
;
1017 struct ubi_vid_hdr
*vid_hdr
;
1023 vid_hdr
= ubi_zalloc_vid_hdr(ubi
, GFP_NOFS
);
1027 mutex_lock(&ubi
->move_mutex
);
1028 spin_lock(&ubi
->wl_lock
);
1029 ubi_assert(!ubi
->move_from
&& !ubi
->move_to
);
1030 ubi_assert(!ubi
->move_to_put
);
1032 if (!ubi
->free
.rb_node
||
1033 (!ubi
->used
.rb_node
&& !ubi
->scrub
.rb_node
)) {
1035 * No free physical eraseblocks? Well, they must be waiting in
1036 * the queue to be erased. Cancel movement - it will be
1037 * triggered again when a free physical eraseblock appears.
1039 * No used physical eraseblocks? They must be temporarily
1040 * protected from being moved. They will be moved to the
1041 * @ubi->used tree later and the wear-leveling will be
1044 dbg_wl("cancel WL, a list is empty: free %d, used %d",
1045 !ubi
->free
.rb_node
, !ubi
->used
.rb_node
);
1049 #ifdef CONFIG_MTD_UBI_FASTMAP
1050 /* Check whether we need to produce an anchor PEB */
1052 anchor
= !anchor_pebs_avalible(&ubi
->free
);
1055 e1
= find_anchor_wl_entry(&ubi
->used
);
1058 e2
= get_peb_for_wl(ubi
);
1062 self_check_in_wl_tree(ubi
, e1
, &ubi
->used
);
1063 rb_erase(&e1
->u
.rb
, &ubi
->used
);
1064 dbg_wl("anchor-move PEB %d to PEB %d", e1
->pnum
, e2
->pnum
);
1065 } else if (!ubi
->scrub
.rb_node
) {
1067 if (!ubi
->scrub
.rb_node
) {
1070 * Now pick the least worn-out used physical eraseblock and a
1071 * highly worn-out free physical eraseblock. If the erase
1072 * counters differ much enough, start wear-leveling.
1074 e1
= rb_entry(rb_first(&ubi
->used
), struct ubi_wl_entry
, u
.rb
);
1075 e2
= get_peb_for_wl(ubi
);
1079 if (!(e2
->ec
- e1
->ec
>= UBI_WL_THRESHOLD
)) {
1080 dbg_wl("no WL needed: min used EC %d, max free EC %d",
1083 /* Give the unused PEB back */
1084 wl_tree_add(e2
, &ubi
->free
);
1088 self_check_in_wl_tree(ubi
, e1
, &ubi
->used
);
1089 rb_erase(&e1
->u
.rb
, &ubi
->used
);
1090 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
1091 e1
->pnum
, e1
->ec
, e2
->pnum
, e2
->ec
);
1093 /* Perform scrubbing */
1095 e1
= rb_entry(rb_first(&ubi
->scrub
), struct ubi_wl_entry
, u
.rb
);
1096 e2
= get_peb_for_wl(ubi
);
1100 self_check_in_wl_tree(ubi
, e1
, &ubi
->scrub
);
1101 rb_erase(&e1
->u
.rb
, &ubi
->scrub
);
1102 dbg_wl("scrub PEB %d to PEB %d", e1
->pnum
, e2
->pnum
);
1105 ubi
->move_from
= e1
;
1107 spin_unlock(&ubi
->wl_lock
);
1110 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
1111 * We so far do not know which logical eraseblock our physical
1112 * eraseblock (@e1) belongs to. We have to read the volume identifier
1115 * Note, we are protected from this PEB being unmapped and erased. The
1116 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
1117 * which is being moved was unmapped.
1120 err
= ubi_io_read_vid_hdr(ubi
, e1
->pnum
, vid_hdr
, 0);
1121 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1122 if (err
== UBI_IO_FF
) {
1124 * We are trying to move PEB without a VID header. UBI
1125 * always write VID headers shortly after the PEB was
1126 * given, so we have a situation when it has not yet
1127 * had a chance to write it, because it was preempted.
1128 * So add this PEB to the protection queue so far,
1129 * because presumably more data will be written there
1130 * (including the missing VID header), and then we'll
1133 dbg_wl("PEB %d has no VID header", e1
->pnum
);
1136 } else if (err
== UBI_IO_FF_BITFLIPS
) {
1138 * The same situation as %UBI_IO_FF, but bit-flips were
1139 * detected. It is better to schedule this PEB for
1142 dbg_wl("PEB %d has no VID header but has bit-flips",
1148 ubi_err("error %d while reading VID header from PEB %d",
1153 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
1154 lnum
= be32_to_cpu(vid_hdr
->lnum
);
1156 err
= ubi_eba_copy_leb(ubi
, e1
->pnum
, e2
->pnum
, vid_hdr
);
1158 if (err
== MOVE_CANCEL_RACE
) {
1160 * The LEB has not been moved because the volume is
1161 * being deleted or the PEB has been put meanwhile. We
1162 * should prevent this PEB from being selected for
1163 * wear-leveling movement again, so put it to the
1169 if (err
== MOVE_RETRY
) {
1173 if (err
== MOVE_TARGET_BITFLIPS
|| err
== MOVE_TARGET_WR_ERR
||
1174 err
== MOVE_TARGET_RD_ERR
) {
1176 * Target PEB had bit-flips or write error - torture it.
1182 if (err
== MOVE_SOURCE_RD_ERR
) {
1184 * An error happened while reading the source PEB. Do
1185 * not switch to R/O mode in this case, and give the
1186 * upper layers a possibility to recover from this,
1187 * e.g. by unmapping corresponding LEB. Instead, just
1188 * put this PEB to the @ubi->erroneous list to prevent
1189 * UBI from trying to move it over and over again.
1191 if (ubi
->erroneous_peb_count
> ubi
->max_erroneous
) {
1192 ubi_err("too many erroneous eraseblocks (%d)",
1193 ubi
->erroneous_peb_count
);
1206 /* The PEB has been successfully moved */
1208 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
1209 e1
->pnum
, vol_id
, lnum
, e2
->pnum
);
1210 ubi_free_vid_hdr(ubi
, vid_hdr
);
1212 spin_lock(&ubi
->wl_lock
);
1213 if (!ubi
->move_to_put
) {
1214 wl_tree_add(e2
, &ubi
->used
);
1217 ubi
->move_from
= ubi
->move_to
= NULL
;
1218 ubi
->move_to_put
= ubi
->wl_scheduled
= 0;
1219 spin_unlock(&ubi
->wl_lock
);
1221 err
= do_sync_erase(ubi
, e1
, vol_id
, lnum
, 0);
1223 kmem_cache_free(ubi_wl_entry_slab
, e1
);
1225 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1231 * Well, the target PEB was put meanwhile, schedule it for
1234 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
1235 e2
->pnum
, vol_id
, lnum
);
1236 err
= do_sync_erase(ubi
, e2
, vol_id
, lnum
, 0);
1238 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1244 mutex_unlock(&ubi
->move_mutex
);
1248 * For some reasons the LEB was not moved, might be an error, might be
1249 * something else. @e1 was not changed, so return it back. @e2 might
1250 * have been changed, schedule it for erasure.
1254 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
1255 e1
->pnum
, vol_id
, lnum
, e2
->pnum
, err
);
1257 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
1258 e1
->pnum
, e2
->pnum
, err
);
1259 spin_lock(&ubi
->wl_lock
);
1261 prot_queue_add(ubi
, e1
);
1262 else if (erroneous
) {
1263 wl_tree_add(e1
, &ubi
->erroneous
);
1264 ubi
->erroneous_peb_count
+= 1;
1265 } else if (scrubbing
)
1266 wl_tree_add(e1
, &ubi
->scrub
);
1268 wl_tree_add(e1
, &ubi
->used
);
1269 ubi_assert(!ubi
->move_to_put
);
1270 ubi
->move_from
= ubi
->move_to
= NULL
;
1271 ubi
->wl_scheduled
= 0;
1272 spin_unlock(&ubi
->wl_lock
);
1274 ubi_free_vid_hdr(ubi
, vid_hdr
);
1275 err
= do_sync_erase(ubi
, e2
, vol_id
, lnum
, torture
);
1277 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1280 mutex_unlock(&ubi
->move_mutex
);
1285 ubi_err("error %d while moving PEB %d to PEB %d",
1286 err
, e1
->pnum
, e2
->pnum
);
1288 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
1289 err
, e1
->pnum
, vol_id
, lnum
, e2
->pnum
);
1290 spin_lock(&ubi
->wl_lock
);
1291 ubi
->move_from
= ubi
->move_to
= NULL
;
1292 ubi
->move_to_put
= ubi
->wl_scheduled
= 0;
1293 spin_unlock(&ubi
->wl_lock
);
1295 ubi_free_vid_hdr(ubi
, vid_hdr
);
1296 kmem_cache_free(ubi_wl_entry_slab
, e1
);
1297 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1301 mutex_unlock(&ubi
->move_mutex
);
1302 ubi_assert(err
!= 0);
1303 return err
< 0 ? err
: -EIO
;
1306 ubi
->wl_scheduled
= 0;
1307 spin_unlock(&ubi
->wl_lock
);
1308 mutex_unlock(&ubi
->move_mutex
);
1309 ubi_free_vid_hdr(ubi
, vid_hdr
);
1314 * ensure_wear_leveling - schedule wear-leveling if it is needed.
1315 * @ubi: UBI device description object
1316 * @nested: set to non-zero if this function is called from UBI worker
1318 * This function checks if it is time to start wear-leveling and schedules it
1319 * if yes. This function returns zero in case of success and a negative error
1320 * code in case of failure.
1322 static int ensure_wear_leveling(struct ubi_device
*ubi
, int nested
)
1325 struct ubi_wl_entry
*e1
;
1326 struct ubi_wl_entry
*e2
;
1327 struct ubi_work
*wrk
;
1329 spin_lock(&ubi
->wl_lock
);
1330 if (ubi
->wl_scheduled
)
1331 /* Wear-leveling is already in the work queue */
1335 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1336 * the WL worker has to be scheduled anyway.
1338 if (!ubi
->scrub
.rb_node
) {
1339 if (!ubi
->used
.rb_node
|| !ubi
->free
.rb_node
)
1340 /* No physical eraseblocks - no deal */
1344 * We schedule wear-leveling only if the difference between the
1345 * lowest erase counter of used physical eraseblocks and a high
1346 * erase counter of free physical eraseblocks is greater than
1347 * %UBI_WL_THRESHOLD.
1349 e1
= rb_entry(rb_first(&ubi
->used
), struct ubi_wl_entry
, u
.rb
);
1350 e2
= find_wl_entry(ubi
, &ubi
->free
, WL_FREE_MAX_DIFF
);
1352 if (!(e2
->ec
- e1
->ec
>= UBI_WL_THRESHOLD
))
1354 dbg_wl("schedule wear-leveling");
1356 dbg_wl("schedule scrubbing");
1358 ubi
->wl_scheduled
= 1;
1359 spin_unlock(&ubi
->wl_lock
);
1361 wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
1368 wrk
->func
= &wear_leveling_worker
;
1370 __schedule_ubi_work(ubi
, wrk
);
1372 schedule_ubi_work(ubi
, wrk
);
1376 spin_lock(&ubi
->wl_lock
);
1377 ubi
->wl_scheduled
= 0;
1379 spin_unlock(&ubi
->wl_lock
);
1383 #ifdef CONFIG_MTD_UBI_FASTMAP
1385 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
1386 * @ubi: UBI device description object
1388 int ubi_ensure_anchor_pebs(struct ubi_device
*ubi
)
1390 struct ubi_work
*wrk
;
1392 spin_lock(&ubi
->wl_lock
);
1393 if (ubi
->wl_scheduled
) {
1394 spin_unlock(&ubi
->wl_lock
);
1397 ubi
->wl_scheduled
= 1;
1398 spin_unlock(&ubi
->wl_lock
);
1400 wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
1402 spin_lock(&ubi
->wl_lock
);
1403 ubi
->wl_scheduled
= 0;
1404 spin_unlock(&ubi
->wl_lock
);
1409 wrk
->func
= &wear_leveling_worker
;
1410 schedule_ubi_work(ubi
, wrk
);
1416 * erase_worker - physical eraseblock erase worker function.
1417 * @ubi: UBI device description object
1418 * @wl_wrk: the work object
1419 * @cancel: non-zero if the worker has to free memory and exit
1421 * This function erases a physical eraseblock and perform torture testing if
1422 * needed. It also takes care about marking the physical eraseblock bad if
1423 * needed. Returns zero in case of success and a negative error code in case of
1426 static int erase_worker(struct ubi_device
*ubi
, struct ubi_work
*wl_wrk
,
1429 struct ubi_wl_entry
*e
= wl_wrk
->e
;
1431 int vol_id
= wl_wrk
->vol_id
;
1432 int lnum
= wl_wrk
->lnum
;
1433 int err
, available_consumed
= 0;
1436 dbg_wl("cancel erasure of PEB %d EC %d", pnum
, e
->ec
);
1438 kmem_cache_free(ubi_wl_entry_slab
, e
);
1442 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1443 pnum
, e
->ec
, wl_wrk
->vol_id
, wl_wrk
->lnum
);
1445 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
1447 err
= sync_erase(ubi
, e
, wl_wrk
->torture
);
1449 /* Fine, we've erased it successfully */
1452 spin_lock(&ubi
->wl_lock
);
1453 wl_tree_add(e
, &ubi
->free
);
1455 spin_unlock(&ubi
->wl_lock
);
1458 * One more erase operation has happened, take care about
1459 * protected physical eraseblocks.
1461 serve_prot_queue(ubi
);
1463 /* And take care about wear-leveling */
1464 err
= ensure_wear_leveling(ubi
, 1);
1468 ubi_err("failed to erase PEB %d, error %d", pnum
, err
);
1471 if (err
== -EINTR
|| err
== -ENOMEM
|| err
== -EAGAIN
||
1475 /* Re-schedule the LEB for erasure */
1476 err1
= schedule_erase(ubi
, e
, vol_id
, lnum
, 0);
1484 kmem_cache_free(ubi_wl_entry_slab
, e
);
1487 * If this is not %-EIO, we have no idea what to do. Scheduling
1488 * this physical eraseblock for erasure again would cause
1489 * errors again and again. Well, lets switch to R/O mode.
1493 /* It is %-EIO, the PEB went bad */
1495 if (!ubi
->bad_allowed
) {
1496 ubi_err("bad physical eraseblock %d detected", pnum
);
1500 spin_lock(&ubi
->volumes_lock
);
1501 if (ubi
->beb_rsvd_pebs
== 0) {
1502 if (ubi
->avail_pebs
== 0) {
1503 spin_unlock(&ubi
->volumes_lock
);
1504 ubi_err("no reserved/available physical eraseblocks");
1507 ubi
->avail_pebs
-= 1;
1508 available_consumed
= 1;
1510 spin_unlock(&ubi
->volumes_lock
);
1512 ubi_msg("mark PEB %d as bad", pnum
);
1513 err
= ubi_io_mark_bad(ubi
, pnum
);
1517 spin_lock(&ubi
->volumes_lock
);
1518 if (ubi
->beb_rsvd_pebs
> 0) {
1519 if (available_consumed
) {
1521 * The amount of reserved PEBs increased since we last
1524 ubi
->avail_pebs
+= 1;
1525 available_consumed
= 0;
1527 ubi
->beb_rsvd_pebs
-= 1;
1529 ubi
->bad_peb_count
+= 1;
1530 ubi
->good_peb_count
-= 1;
1531 ubi_calculate_reserved(ubi
);
1532 if (available_consumed
)
1533 ubi_warn("no PEBs in the reserved pool, used an available PEB");
1534 else if (ubi
->beb_rsvd_pebs
)
1535 ubi_msg("%d PEBs left in the reserve", ubi
->beb_rsvd_pebs
);
1537 ubi_warn("last PEB from the reserve was used");
1538 spin_unlock(&ubi
->volumes_lock
);
1543 if (available_consumed
) {
1544 spin_lock(&ubi
->volumes_lock
);
1545 ubi
->avail_pebs
+= 1;
1546 spin_unlock(&ubi
->volumes_lock
);
1553 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1554 * @ubi: UBI device description object
1555 * @vol_id: the volume ID that last used this PEB
1556 * @lnum: the last used logical eraseblock number for the PEB
1557 * @pnum: physical eraseblock to return
1558 * @torture: if this physical eraseblock has to be tortured
1560 * This function is called to return physical eraseblock @pnum to the pool of
1561 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1562 * occurred to this @pnum and it has to be tested. This function returns zero
1563 * in case of success, and a negative error code in case of failure.
1565 int ubi_wl_put_peb(struct ubi_device
*ubi
, int vol_id
, int lnum
,
1566 int pnum
, int torture
)
1569 struct ubi_wl_entry
*e
;
1571 dbg_wl("PEB %d", pnum
);
1572 ubi_assert(pnum
>= 0);
1573 ubi_assert(pnum
< ubi
->peb_count
);
1576 spin_lock(&ubi
->wl_lock
);
1577 e
= ubi
->lookuptbl
[pnum
];
1578 if (e
== ubi
->move_from
) {
1580 * User is putting the physical eraseblock which was selected to
1581 * be moved. It will be scheduled for erasure in the
1582 * wear-leveling worker.
1584 dbg_wl("PEB %d is being moved, wait", pnum
);
1585 spin_unlock(&ubi
->wl_lock
);
1587 /* Wait for the WL worker by taking the @ubi->move_mutex */
1588 mutex_lock(&ubi
->move_mutex
);
1589 mutex_unlock(&ubi
->move_mutex
);
1591 } else if (e
== ubi
->move_to
) {
1593 * User is putting the physical eraseblock which was selected
1594 * as the target the data is moved to. It may happen if the EBA
1595 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1596 * but the WL sub-system has not put the PEB to the "used" tree
1597 * yet, but it is about to do this. So we just set a flag which
1598 * will tell the WL worker that the PEB is not needed anymore
1599 * and should be scheduled for erasure.
1601 dbg_wl("PEB %d is the target of data moving", pnum
);
1602 ubi_assert(!ubi
->move_to_put
);
1603 ubi
->move_to_put
= 1;
1604 spin_unlock(&ubi
->wl_lock
);
1607 if (in_wl_tree(e
, &ubi
->used
)) {
1608 self_check_in_wl_tree(ubi
, e
, &ubi
->used
);
1609 rb_erase(&e
->u
.rb
, &ubi
->used
);
1610 } else if (in_wl_tree(e
, &ubi
->scrub
)) {
1611 self_check_in_wl_tree(ubi
, e
, &ubi
->scrub
);
1612 rb_erase(&e
->u
.rb
, &ubi
->scrub
);
1613 } else if (in_wl_tree(e
, &ubi
->erroneous
)) {
1614 self_check_in_wl_tree(ubi
, e
, &ubi
->erroneous
);
1615 rb_erase(&e
->u
.rb
, &ubi
->erroneous
);
1616 ubi
->erroneous_peb_count
-= 1;
1617 ubi_assert(ubi
->erroneous_peb_count
>= 0);
1618 /* Erroneous PEBs should be tortured */
1621 err
= prot_queue_del(ubi
, e
->pnum
);
1623 ubi_err("PEB %d not found", pnum
);
1625 spin_unlock(&ubi
->wl_lock
);
1630 spin_unlock(&ubi
->wl_lock
);
1632 err
= schedule_erase(ubi
, e
, vol_id
, lnum
, torture
);
1634 spin_lock(&ubi
->wl_lock
);
1635 wl_tree_add(e
, &ubi
->used
);
1636 spin_unlock(&ubi
->wl_lock
);
1643 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1644 * @ubi: UBI device description object
1645 * @pnum: the physical eraseblock to schedule
1647 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1648 * needs scrubbing. This function schedules a physical eraseblock for
1649 * scrubbing which is done in background. This function returns zero in case of
1650 * success and a negative error code in case of failure.
1652 int ubi_wl_scrub_peb(struct ubi_device
*ubi
, int pnum
)
1654 struct ubi_wl_entry
*e
;
1656 ubi_msg("schedule PEB %d for scrubbing", pnum
);
1659 spin_lock(&ubi
->wl_lock
);
1660 e
= ubi
->lookuptbl
[pnum
];
1661 if (e
== ubi
->move_from
|| in_wl_tree(e
, &ubi
->scrub
) ||
1662 in_wl_tree(e
, &ubi
->erroneous
)) {
1663 spin_unlock(&ubi
->wl_lock
);
1667 if (e
== ubi
->move_to
) {
1669 * This physical eraseblock was used to move data to. The data
1670 * was moved but the PEB was not yet inserted to the proper
1671 * tree. We should just wait a little and let the WL worker
1674 spin_unlock(&ubi
->wl_lock
);
1675 dbg_wl("the PEB %d is not in proper tree, retry", pnum
);
1680 if (in_wl_tree(e
, &ubi
->used
)) {
1681 self_check_in_wl_tree(ubi
, e
, &ubi
->used
);
1682 rb_erase(&e
->u
.rb
, &ubi
->used
);
1686 err
= prot_queue_del(ubi
, e
->pnum
);
1688 ubi_err("PEB %d not found", pnum
);
1690 spin_unlock(&ubi
->wl_lock
);
1695 wl_tree_add(e
, &ubi
->scrub
);
1696 spin_unlock(&ubi
->wl_lock
);
1699 * Technically scrubbing is the same as wear-leveling, so it is done
1702 return ensure_wear_leveling(ubi
, 0);
1706 * ubi_wl_flush - flush all pending works.
1707 * @ubi: UBI device description object
1708 * @vol_id: the volume id to flush for
1709 * @lnum: the logical eraseblock number to flush for
1711 * This function executes all pending works for a particular volume id /
1712 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1713 * acts as a wildcard for all of the corresponding volume numbers or logical
1714 * eraseblock numbers. It returns zero in case of success and a negative error
1715 * code in case of failure.
1717 int ubi_wl_flush(struct ubi_device
*ubi
, int vol_id
, int lnum
)
1723 * Erase while the pending works queue is not empty, but not more than
1724 * the number of currently pending works.
1726 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1727 vol_id
, lnum
, ubi
->works_count
);
1730 struct ubi_work
*wrk
;
1733 down_read(&ubi
->work_sem
);
1734 spin_lock(&ubi
->wl_lock
);
1735 list_for_each_entry(wrk
, &ubi
->works
, list
) {
1736 if ((vol_id
== UBI_ALL
|| wrk
->vol_id
== vol_id
) &&
1737 (lnum
== UBI_ALL
|| wrk
->lnum
== lnum
)) {
1738 list_del(&wrk
->list
);
1739 ubi
->works_count
-= 1;
1740 ubi_assert(ubi
->works_count
>= 0);
1741 spin_unlock(&ubi
->wl_lock
);
1743 err
= wrk
->func(ubi
, wrk
, 0);
1745 up_read(&ubi
->work_sem
);
1749 spin_lock(&ubi
->wl_lock
);
1754 spin_unlock(&ubi
->wl_lock
);
1755 up_read(&ubi
->work_sem
);
1759 * Make sure all the works which have been done in parallel are
1762 down_write(&ubi
->work_sem
);
1763 up_write(&ubi
->work_sem
);
1769 * tree_destroy - destroy an RB-tree.
1770 * @root: the root of the tree to destroy
1772 static void tree_destroy(struct rb_root
*root
)
1775 struct ubi_wl_entry
*e
;
1781 else if (rb
->rb_right
)
1784 e
= rb_entry(rb
, struct ubi_wl_entry
, u
.rb
);
1788 if (rb
->rb_left
== &e
->u
.rb
)
1791 rb
->rb_right
= NULL
;
1794 kmem_cache_free(ubi_wl_entry_slab
, e
);
1800 * ubi_thread - UBI background thread.
1801 * @u: the UBI device description object pointer
1803 int ubi_thread(void *u
)
1806 struct ubi_device
*ubi
= u
;
1808 ubi_msg("background thread \"%s\" started, PID %d",
1809 ubi
->bgt_name
, task_pid_nr(current
));
1815 if (kthread_should_stop())
1818 if (try_to_freeze())
1821 spin_lock(&ubi
->wl_lock
);
1822 if (list_empty(&ubi
->works
) || ubi
->ro_mode
||
1823 !ubi
->thread_enabled
|| ubi_dbg_is_bgt_disabled(ubi
)) {
1824 set_current_state(TASK_INTERRUPTIBLE
);
1825 spin_unlock(&ubi
->wl_lock
);
1829 spin_unlock(&ubi
->wl_lock
);
1833 ubi_err("%s: work failed with error code %d",
1834 ubi
->bgt_name
, err
);
1835 if (failures
++ > WL_MAX_FAILURES
) {
1837 * Too many failures, disable the thread and
1838 * switch to read-only mode.
1840 ubi_msg("%s: %d consecutive failures",
1841 ubi
->bgt_name
, WL_MAX_FAILURES
);
1843 ubi
->thread_enabled
= 0;
1852 dbg_wl("background thread \"%s\" is killed", ubi
->bgt_name
);
1857 * cancel_pending - cancel all pending works.
1858 * @ubi: UBI device description object
1860 static void cancel_pending(struct ubi_device
*ubi
)
1862 while (!list_empty(&ubi
->works
)) {
1863 struct ubi_work
*wrk
;
1865 wrk
= list_entry(ubi
->works
.next
, struct ubi_work
, list
);
1866 list_del(&wrk
->list
);
1867 wrk
->func(ubi
, wrk
, 1);
1868 ubi
->works_count
-= 1;
1869 ubi_assert(ubi
->works_count
>= 0);
1874 * ubi_wl_init - initialize the WL sub-system using attaching information.
1875 * @ubi: UBI device description object
1876 * @ai: attaching information
1878 * This function returns zero in case of success, and a negative error code in
1881 int ubi_wl_init(struct ubi_device
*ubi
, struct ubi_attach_info
*ai
)
1883 int err
, i
, reserved_pebs
, found_pebs
= 0;
1884 struct rb_node
*rb1
, *rb2
;
1885 struct ubi_ainf_volume
*av
;
1886 struct ubi_ainf_peb
*aeb
, *tmp
;
1887 struct ubi_wl_entry
*e
;
1889 ubi
->used
= ubi
->erroneous
= ubi
->free
= ubi
->scrub
= RB_ROOT
;
1890 spin_lock_init(&ubi
->wl_lock
);
1891 mutex_init(&ubi
->move_mutex
);
1892 init_rwsem(&ubi
->work_sem
);
1893 ubi
->max_ec
= ai
->max_ec
;
1894 INIT_LIST_HEAD(&ubi
->works
);
1896 #ifdef CONFIG_MTD_UBI_FASTMAP
1897 INIT_WORK(&ubi
->fm_work
, update_fastmap_work_fn
);
1901 sprintf(ubi
->bgt_name
, UBI_BGT_NAME_PATTERN
, ubi
->ubi_num
);
1904 ubi
->lookuptbl
= kzalloc(ubi
->peb_count
* sizeof(void *), GFP_KERNEL
);
1905 if (!ubi
->lookuptbl
)
1908 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; i
++)
1909 INIT_LIST_HEAD(&ubi
->pq
[i
]);
1912 list_for_each_entry_safe(aeb
, tmp
, &ai
->erase
, u
.list
) {
1915 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
1919 e
->pnum
= aeb
->pnum
;
1921 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
1922 ubi
->lookuptbl
[e
->pnum
] = e
;
1923 if (schedule_erase(ubi
, e
, aeb
->vol_id
, aeb
->lnum
, 0)) {
1924 kmem_cache_free(ubi_wl_entry_slab
, e
);
1931 ubi
->free_count
= 0;
1932 list_for_each_entry(aeb
, &ai
->free
, u
.list
) {
1935 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
1939 e
->pnum
= aeb
->pnum
;
1941 ubi_assert(e
->ec
>= 0);
1942 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
1944 wl_tree_add(e
, &ubi
->free
);
1947 ubi
->lookuptbl
[e
->pnum
] = e
;
1952 ubi_rb_for_each_entry(rb1
, av
, &ai
->volumes
, rb
) {
1953 ubi_rb_for_each_entry(rb2
, aeb
, &av
->root
, u
.rb
) {
1956 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
1960 e
->pnum
= aeb
->pnum
;
1962 ubi
->lookuptbl
[e
->pnum
] = e
;
1965 dbg_wl("add PEB %d EC %d to the used tree",
1967 wl_tree_add(e
, &ubi
->used
);
1969 dbg_wl("add PEB %d EC %d to the scrub tree",
1971 wl_tree_add(e
, &ubi
->scrub
);
1978 dbg_wl("found %i PEBs", found_pebs
);
1981 ubi_assert(ubi
->good_peb_count
== \
1982 found_pebs
+ ubi
->fm
->used_blocks
);
1984 ubi_assert(ubi
->good_peb_count
== found_pebs
);
1986 reserved_pebs
= WL_RESERVED_PEBS
;
1987 #ifdef CONFIG_MTD_UBI_FASTMAP
1988 /* Reserve enough LEBs to store two fastmaps. */
1989 reserved_pebs
+= (ubi
->fm_size
/ ubi
->leb_size
) * 2;
1992 if (ubi
->avail_pebs
< reserved_pebs
) {
1993 ubi_err("no enough physical eraseblocks (%d, need %d)",
1994 ubi
->avail_pebs
, reserved_pebs
);
1995 if (ubi
->corr_peb_count
)
1996 ubi_err("%d PEBs are corrupted and not used",
1997 ubi
->corr_peb_count
);
2000 ubi
->avail_pebs
-= reserved_pebs
;
2001 ubi
->rsvd_pebs
+= reserved_pebs
;
2003 /* Schedule wear-leveling if needed */
2004 err
= ensure_wear_leveling(ubi
, 0);
2011 cancel_pending(ubi
);
2012 tree_destroy(&ubi
->used
);
2013 tree_destroy(&ubi
->free
);
2014 tree_destroy(&ubi
->scrub
);
2015 kfree(ubi
->lookuptbl
);
2020 * protection_queue_destroy - destroy the protection queue.
2021 * @ubi: UBI device description object
2023 static void protection_queue_destroy(struct ubi_device
*ubi
)
2026 struct ubi_wl_entry
*e
, *tmp
;
2028 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; ++i
) {
2029 list_for_each_entry_safe(e
, tmp
, &ubi
->pq
[i
], u
.list
) {
2030 list_del(&e
->u
.list
);
2031 kmem_cache_free(ubi_wl_entry_slab
, e
);
2037 * ubi_wl_close - close the wear-leveling sub-system.
2038 * @ubi: UBI device description object
2040 void ubi_wl_close(struct ubi_device
*ubi
)
2042 dbg_wl("close the WL sub-system");
2043 cancel_pending(ubi
);
2044 protection_queue_destroy(ubi
);
2045 tree_destroy(&ubi
->used
);
2046 tree_destroy(&ubi
->erroneous
);
2047 tree_destroy(&ubi
->free
);
2048 tree_destroy(&ubi
->scrub
);
2049 kfree(ubi
->lookuptbl
);
2053 * self_check_ec - make sure that the erase counter of a PEB is correct.
2054 * @ubi: UBI device description object
2055 * @pnum: the physical eraseblock number to check
2056 * @ec: the erase counter to check
2058 * This function returns zero if the erase counter of physical eraseblock @pnum
2059 * is equivalent to @ec, and a negative error code if not or if an error
2062 static int self_check_ec(struct ubi_device
*ubi
, int pnum
, int ec
)
2066 struct ubi_ec_hdr
*ec_hdr
;
2068 if (!ubi_dbg_chk_gen(ubi
))
2071 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_NOFS
);
2075 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ec_hdr
, 0);
2076 if (err
&& err
!= UBI_IO_BITFLIPS
) {
2077 /* The header does not have to exist */
2082 read_ec
= be64_to_cpu(ec_hdr
->ec
);
2083 if (ec
!= read_ec
&& read_ec
- ec
> 1) {
2084 ubi_err("self-check failed for PEB %d", pnum
);
2085 ubi_err("read EC is %lld, should be %d", read_ec
, ec
);
2097 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
2098 * @ubi: UBI device description object
2099 * @e: the wear-leveling entry to check
2100 * @root: the root of the tree
2102 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
2105 static int self_check_in_wl_tree(const struct ubi_device
*ubi
,
2106 struct ubi_wl_entry
*e
, struct rb_root
*root
)
2108 if (!ubi_dbg_chk_gen(ubi
))
2111 if (in_wl_tree(e
, root
))
2114 ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
2115 e
->pnum
, e
->ec
, root
);
2121 * self_check_in_pq - check if wear-leveling entry is in the protection
2123 * @ubi: UBI device description object
2124 * @e: the wear-leveling entry to check
2126 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2128 static int self_check_in_pq(const struct ubi_device
*ubi
,
2129 struct ubi_wl_entry
*e
)
2131 struct ubi_wl_entry
*p
;
2134 if (!ubi_dbg_chk_gen(ubi
))
2137 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; ++i
)
2138 list_for_each_entry(p
, &ubi
->pq
[i
], u
.list
)
2142 ubi_err("self-check failed for PEB %d, EC %d, Protect queue",