2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
22 * UBI wear-leveling unit.
24 * This unit is responsible for wear-leveling. It works in terms of physical
25 * eraseblocks and erase counters and knows nothing about logical eraseblocks,
26 * volumes, etc. From this unit's perspective all physical eraseblocks are of
27 * two types - used and free. Used physical eraseblocks are those that were
28 * "get" by the 'ubi_wl_get_peb()' function, and free physical eraseblocks are
29 * those that were put by the 'ubi_wl_put_peb()' function.
31 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
32 * header. The rest of the physical eraseblock contains only 0xFF bytes.
34 * When physical eraseblocks are returned to the WL unit by means of the
35 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
36 * done asynchronously in context of the per-UBI device background thread,
37 * which is also managed by the WL unit.
39 * The wear-leveling is ensured by means of moving the contents of used
40 * physical eraseblocks with low erase counter to free physical eraseblocks
41 * with high erase counter.
43 * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
44 * an "optimal" physical eraseblock. For example, when it is known that the
45 * physical eraseblock will be "put" soon because it contains short-term data,
46 * the WL unit may pick a free physical eraseblock with low erase counter, and
49 * If the WL unit fails to erase a physical eraseblock, it marks it as bad.
51 * This unit is also responsible for scrubbing. If a bit-flip is detected in a
52 * physical eraseblock, it has to be moved. Technically this is the same as
53 * moving it for wear-leveling reasons.
55 * As it was said, for the UBI unit all physical eraseblocks are either "free"
56 * or "used". Free eraseblock are kept in the @wl->free RB-tree, while used
57 * eraseblocks are kept in a set of different RB-trees: @wl->used,
58 * @wl->prot.pnum, @wl->prot.aec, and @wl->scrub.
60 * Note, in this implementation, we keep a small in-RAM object for each physical
61 * eraseblock. This is surely not a scalable solution. But it appears to be good
62 * enough for moderately large flashes and it is simple. In future, one may
63 * re-work this unit and make it more scalable.
65 * At the moment this unit does not utilize the sequence number, which was
66 * introduced relatively recently. But it would be wise to do this because the
67 * sequence number of a logical eraseblock characterizes how old is it. For
68 * example, when we move a PEB with low erase counter, and we need to pick the
69 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
70 * pick target PEB with an average EC if our PEB is not very "old". This is a
71 * room for future re-works of the WL unit.
73 * FIXME: looks too complex, should be simplified (later).
76 #include <linux/slab.h>
77 #include <linux/crc32.h>
78 #include <linux/freezer.h>
79 #include <linux/kthread.h>
82 /* Number of physical eraseblocks reserved for wear-leveling purposes */
83 #define WL_RESERVED_PEBS 1
86 * How many erase cycles are short term, unknown, and long term physical
87 * eraseblocks protected.
89 #define ST_PROTECTION 16
90 #define U_PROTECTION 10
91 #define LT_PROTECTION 4
94 * Maximum difference between two erase counters. If this threshold is
95 * exceeded, the WL unit starts moving data from used physical eraseblocks with
96 * low erase counter to free physical eraseblocks with high erase counter.
98 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
101 * When a physical eraseblock is moved, the WL unit has to pick the target
102 * physical eraseblock to move to. The simplest way would be just to pick the
103 * one with the highest erase counter. But in certain workloads this could lead
104 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
105 * situation when the picked physical eraseblock is constantly erased after the
106 * data is written to it. So, we have a constant which limits the highest erase
107 * counter of the free physical eraseblock to pick. Namely, the WL unit does
108 * not pick eraseblocks with erase counter greater then the lowest erase
109 * counter plus %WL_FREE_MAX_DIFF.
111 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
114 * Maximum number of consecutive background thread failures which is enough to
115 * switch to read-only mode.
117 #define WL_MAX_FAILURES 32
120 * struct ubi_wl_prot_entry - PEB protection entry.
121 * @rb_pnum: link in the @wl->prot.pnum RB-tree
122 * @rb_aec: link in the @wl->prot.aec RB-tree
123 * @abs_ec: the absolute erase counter value when the protection ends
124 * @e: the wear-leveling entry of the physical eraseblock under protection
126 * When the WL unit returns a physical eraseblock, the physical eraseblock is
127 * protected from being moved for some "time". For this reason, the physical
128 * eraseblock is not directly moved from the @wl->free tree to the @wl->used
129 * tree. There is one more tree in between where this physical eraseblock is
130 * temporarily stored (@wl->prot).
132 * All this protection stuff is needed because:
133 * o we don't want to move physical eraseblocks just after we have given them
134 * to the user; instead, we first want to let users fill them up with data;
136 * o there is a chance that the user will put the physical eraseblock very
137 * soon, so it makes sense not to move it for some time, but wait; this is
138 * especially important in case of "short term" physical eraseblocks.
140 * Physical eraseblocks stay protected only for limited time. But the "time" is
141 * measured in erase cycles in this case. This is implemented with help of the
142 * absolute erase counter (@wl->abs_ec). When it reaches certain value, the
143 * physical eraseblocks are moved from the protection trees (@wl->prot.*) to
144 * the @wl->used tree.
146 * Protected physical eraseblocks are searched by physical eraseblock number
147 * (when they are put) and by the absolute erase counter (to check if it is
148 * time to move them to the @wl->used tree). So there are actually 2 RB-trees
149 * storing the protected physical eraseblocks: @wl->prot.pnum and
150 * @wl->prot.aec. They are referred to as the "protection" trees. The
151 * first one is indexed by the physical eraseblock number. The second one is
152 * indexed by the absolute erase counter. Both trees store
153 * &struct ubi_wl_prot_entry objects.
155 * Each physical eraseblock has 2 main states: free and used. The former state
156 * corresponds to the @wl->free tree. The latter state is split up on several
158 * o the WL movement is allowed (@wl->used tree);
159 * o the WL movement is temporarily prohibited (@wl->prot.pnum and
160 * @wl->prot.aec trees);
161 * o scrubbing is needed (@wl->scrub tree).
163 * Depending on the sub-state, wear-leveling entries of the used physical
164 * eraseblocks may be kept in one of those trees.
166 struct ubi_wl_prot_entry {
167 struct rb_node rb_pnum;
168 struct rb_node rb_aec;
169 unsigned long long abs_ec;
170 struct ubi_wl_entry *e;
174 * struct ubi_work - UBI work description data structure.
175 * @list: a link in the list of pending works
176 * @func: worker function
177 * @priv: private data of the worker function
179 * @e: physical eraseblock to erase
180 * @torture: if the physical eraseblock has to be tortured
182 * The @func pointer points to the worker function. If the @cancel argument is
183 * not zero, the worker has to free the resources and exit immediately. The
184 * worker has to return zero in case of success and a negative error code in
188 struct list_head list;
189 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
190 /* The below fields are only relevant to erasure works */
191 struct ubi_wl_entry *e;
195 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
196 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
197 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
198 struct rb_root *root);
200 #define paranoid_check_ec(ubi, pnum, ec) 0
201 #define paranoid_check_in_wl_tree(e, root)
205 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
206 * @e: the wear-leveling entry to add
207 * @root: the root of the tree
209 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
210 * the @ubi->used and @ubi->free RB-trees.
212 static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
214 struct rb_node **p, *parent = NULL;
218 struct ubi_wl_entry *e1;
221 e1 = rb_entry(parent, struct ubi_wl_entry, rb);
225 else if (e->ec > e1->ec)
228 ubi_assert(e->pnum != e1->pnum);
229 if (e->pnum < e1->pnum)
236 rb_link_node(&e->rb, parent, p);
237 rb_insert_color(&e->rb, root);
241 * do_work - do one pending work.
242 * @ubi: UBI device description object
244 * This function returns zero in case of success and a negative error code in
247 static int do_work(struct ubi_device *ubi)
250 struct ubi_work *wrk;
255 * @ubi->work_sem is used to synchronize with the workers. Workers take
256 * it in read mode, so many of them may be doing works at a time. But
257 * the queue flush code has to be sure the whole queue of works is
258 * done, and it takes the mutex in write mode.
260 down_read(&ubi->work_sem);
261 spin_lock(&ubi->wl_lock);
263 if (list_empty(&ubi->works)) {
264 spin_unlock(&ubi->wl_lock);
265 up_read(&ubi->work_sem);
269 wrk = list_entry(ubi->works.next, struct ubi_work, list);
270 list_del(&wrk->list);
271 spin_unlock(&ubi->wl_lock);
274 * Call the worker function. Do not touch the work structure
275 * after this call as it will have been freed or reused by that
276 * time by the worker function.
278 err = wrk->func(ubi, wrk, 0);
280 ubi_err("work failed with error code %d", err);
282 spin_lock(&ubi->wl_lock);
283 ubi->works_count -= 1;
284 ubi_assert(ubi->works_count >= 0);
285 spin_unlock(&ubi->wl_lock);
286 up_read(&ubi->work_sem);
291 * produce_free_peb - produce a free physical eraseblock.
292 * @ubi: UBI device description object
294 * This function tries to make a free PEB by means of synchronous execution of
295 * pending works. This may be needed if, for example the background thread is
296 * disabled. Returns zero in case of success and a negative error code in case
299 static int produce_free_peb(struct ubi_device *ubi)
303 spin_lock(&ubi->wl_lock);
304 while (!ubi->free.rb_node) {
305 spin_unlock(&ubi->wl_lock);
307 dbg_wl("do one work synchronously");
312 spin_lock(&ubi->wl_lock);
314 spin_unlock(&ubi->wl_lock);
320 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
321 * @e: the wear-leveling entry to check
322 * @root: the root of the tree
324 * This function returns non-zero if @e is in the @root RB-tree and zero if it
327 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
333 struct ubi_wl_entry *e1;
335 e1 = rb_entry(p, struct ubi_wl_entry, rb);
337 if (e->pnum == e1->pnum) {
344 else if (e->ec > e1->ec)
347 ubi_assert(e->pnum != e1->pnum);
348 if (e->pnum < e1->pnum)
359 * prot_tree_add - add physical eraseblock to protection trees.
360 * @ubi: UBI device description object
361 * @e: the physical eraseblock to add
362 * @pe: protection entry object to use
363 * @abs_ec: absolute erase counter value when this physical eraseblock has
364 * to be removed from the protection trees.
366 * @wl->lock has to be locked.
368 static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
369 struct ubi_wl_prot_entry *pe, int abs_ec)
371 struct rb_node **p, *parent = NULL;
372 struct ubi_wl_prot_entry *pe1;
375 pe->abs_ec = ubi->abs_ec + abs_ec;
377 p = &ubi->prot.pnum.rb_node;
380 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
382 if (e->pnum < pe1->e->pnum)
387 rb_link_node(&pe->rb_pnum, parent, p);
388 rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
390 p = &ubi->prot.aec.rb_node;
394 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
396 if (pe->abs_ec < pe1->abs_ec)
401 rb_link_node(&pe->rb_aec, parent, p);
402 rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
406 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
407 * @root: the RB-tree where to look for
408 * @max: highest possible erase counter
410 * This function looks for a wear leveling entry with erase counter closest to
411 * @max and less then @max.
413 static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
416 struct ubi_wl_entry *e;
418 e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
423 struct ubi_wl_entry *e1;
425 e1 = rb_entry(p, struct ubi_wl_entry, rb);
438 * ubi_wl_get_peb - get a physical eraseblock.
439 * @ubi: UBI device description object
440 * @dtype: type of data which will be stored in this physical eraseblock
442 * This function returns a physical eraseblock in case of success and a
443 * negative error code in case of failure. Might sleep.
445 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
447 int err, protect, medium_ec;
448 struct ubi_wl_entry *e, *first, *last;
449 struct ubi_wl_prot_entry *pe;
451 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
452 dtype == UBI_UNKNOWN);
454 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
459 spin_lock(&ubi->wl_lock);
460 if (!ubi->free.rb_node) {
461 if (ubi->works_count == 0) {
462 ubi_assert(list_empty(&ubi->works));
463 ubi_err("no free eraseblocks");
464 spin_unlock(&ubi->wl_lock);
468 spin_unlock(&ubi->wl_lock);
470 err = produce_free_peb(ubi);
481 * For long term data we pick a physical eraseblock
482 * with high erase counter. But the highest erase
483 * counter we can pick is bounded by the the lowest
484 * erase counter plus %WL_FREE_MAX_DIFF.
486 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
487 protect = LT_PROTECTION;
491 * For unknown data we pick a physical eraseblock with
492 * medium erase counter. But we by no means can pick a
493 * physical eraseblock with erase counter greater or
494 * equivalent than the lowest erase counter plus
497 first = rb_entry(rb_first(&ubi->free),
498 struct ubi_wl_entry, rb);
499 last = rb_entry(rb_last(&ubi->free),
500 struct ubi_wl_entry, rb);
502 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
503 e = rb_entry(ubi->free.rb_node,
504 struct ubi_wl_entry, rb);
506 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
507 e = find_wl_entry(&ubi->free, medium_ec);
509 protect = U_PROTECTION;
513 * For short term data we pick a physical eraseblock
514 * with the lowest erase counter as we expect it will
517 e = rb_entry(rb_first(&ubi->free),
518 struct ubi_wl_entry, rb);
519 protect = ST_PROTECTION;
528 * Move the physical eraseblock to the protection trees where it will
529 * be protected from being moved for some time.
531 paranoid_check_in_wl_tree(e, &ubi->free);
532 rb_erase(&e->rb, &ubi->free);
533 prot_tree_add(ubi, e, pe, protect);
535 dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
536 spin_unlock(&ubi->wl_lock);
542 * prot_tree_del - remove a physical eraseblock from the protection trees
543 * @ubi: UBI device description object
544 * @pnum: the physical eraseblock to remove
546 * This function returns PEB @pnum from the protection trees and returns zero
547 * in case of success and %-ENODEV if the PEB was not found in the protection
550 static int prot_tree_del(struct ubi_device *ubi, int pnum)
553 struct ubi_wl_prot_entry *pe = NULL;
555 p = ubi->prot.pnum.rb_node;
558 pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
560 if (pnum == pe->e->pnum)
563 if (pnum < pe->e->pnum)
572 ubi_assert(pe->e->pnum == pnum);
573 rb_erase(&pe->rb_aec, &ubi->prot.aec);
574 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
580 * sync_erase - synchronously erase a physical eraseblock.
581 * @ubi: UBI device description object
582 * @e: the the physical eraseblock to erase
583 * @torture: if the physical eraseblock has to be tortured
585 * This function returns zero in case of success and a negative error code in
588 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture)
591 struct ubi_ec_hdr *ec_hdr;
592 unsigned long long ec = e->ec;
594 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
596 err = paranoid_check_ec(ubi, e->pnum, e->ec);
600 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
604 err = ubi_io_sync_erase(ubi, e->pnum, torture);
609 if (ec > UBI_MAX_ERASECOUNTER) {
611 * Erase counter overflow. Upgrade UBI and use 64-bit
612 * erase counters internally.
614 ubi_err("erase counter overflow at PEB %d, EC %llu",
620 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
622 ec_hdr->ec = cpu_to_be64(ec);
624 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
629 spin_lock(&ubi->wl_lock);
630 if (e->ec > ubi->max_ec)
632 spin_unlock(&ubi->wl_lock);
640 * check_protection_over - check if it is time to stop protecting some
641 * physical eraseblocks.
642 * @ubi: UBI device description object
644 * This function is called after each erase operation, when the absolute erase
645 * counter is incremented, to check if some physical eraseblock have not to be
646 * protected any longer. These physical eraseblocks are moved from the
647 * protection trees to the used tree.
649 static void check_protection_over(struct ubi_device *ubi)
651 struct ubi_wl_prot_entry *pe;
654 * There may be several protected physical eraseblock to remove,
658 spin_lock(&ubi->wl_lock);
659 if (!ubi->prot.aec.rb_node) {
660 spin_unlock(&ubi->wl_lock);
664 pe = rb_entry(rb_first(&ubi->prot.aec),
665 struct ubi_wl_prot_entry, rb_aec);
667 if (pe->abs_ec > ubi->abs_ec) {
668 spin_unlock(&ubi->wl_lock);
672 dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
673 pe->e->pnum, ubi->abs_ec, pe->abs_ec);
674 rb_erase(&pe->rb_aec, &ubi->prot.aec);
675 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
676 wl_tree_add(pe->e, &ubi->used);
677 spin_unlock(&ubi->wl_lock);
685 * schedule_ubi_work - schedule a work.
686 * @ubi: UBI device description object
687 * @wrk: the work to schedule
689 * This function enqueues a work defined by @wrk to the tail of the pending
692 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
694 spin_lock(&ubi->wl_lock);
695 list_add_tail(&wrk->list, &ubi->works);
696 ubi_assert(ubi->works_count >= 0);
697 ubi->works_count += 1;
698 if (ubi->thread_enabled)
699 wake_up_process(ubi->bgt_thread);
700 spin_unlock(&ubi->wl_lock);
703 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
707 * schedule_erase - schedule an erase work.
708 * @ubi: UBI device description object
709 * @e: the WL entry of the physical eraseblock to erase
710 * @torture: if the physical eraseblock has to be tortured
712 * This function returns zero in case of success and a %-ENOMEM in case of
715 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
718 struct ubi_work *wl_wrk;
720 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
721 e->pnum, e->ec, torture);
723 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
727 wl_wrk->func = &erase_worker;
729 wl_wrk->torture = torture;
731 schedule_ubi_work(ubi, wl_wrk);
736 * wear_leveling_worker - wear-leveling worker function.
737 * @ubi: UBI device description object
738 * @wrk: the work object
739 * @cancel: non-zero if the worker has to free memory and exit
741 * This function copies a more worn out physical eraseblock to a less worn out
742 * one. Returns zero in case of success and a negative error code in case of
745 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
748 int err, put = 0, scrubbing = 0, protect = 0;
749 struct ubi_wl_prot_entry *pe;
750 struct ubi_wl_entry *e1, *e2;
751 struct ubi_vid_hdr *vid_hdr;
758 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
762 mutex_lock(&ubi->move_mutex);
763 spin_lock(&ubi->wl_lock);
764 ubi_assert(!ubi->move_from && !ubi->move_to);
765 ubi_assert(!ubi->move_to_put);
767 if (!ubi->free.rb_node ||
768 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
770 * No free physical eraseblocks? Well, they must be waiting in
771 * the queue to be erased. Cancel movement - it will be
772 * triggered again when a free physical eraseblock appears.
774 * No used physical eraseblocks? They must be temporarily
775 * protected from being moved. They will be moved to the
776 * @ubi->used tree later and the wear-leveling will be
779 dbg_wl("cancel WL, a list is empty: free %d, used %d",
780 !ubi->free.rb_node, !ubi->used.rb_node);
784 if (!ubi->scrub.rb_node) {
786 * Now pick the least worn-out used physical eraseblock and a
787 * highly worn-out free physical eraseblock. If the erase
788 * counters differ much enough, start wear-leveling.
790 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
791 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
793 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
794 dbg_wl("no WL needed: min used EC %d, max free EC %d",
798 paranoid_check_in_wl_tree(e1, &ubi->used);
799 rb_erase(&e1->rb, &ubi->used);
800 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
801 e1->pnum, e1->ec, e2->pnum, e2->ec);
803 /* Perform scrubbing */
805 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
806 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
807 paranoid_check_in_wl_tree(e1, &ubi->scrub);
808 rb_erase(&e1->rb, &ubi->scrub);
809 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
812 paranoid_check_in_wl_tree(e2, &ubi->free);
813 rb_erase(&e2->rb, &ubi->free);
816 spin_unlock(&ubi->wl_lock);
819 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
820 * We so far do not know which logical eraseblock our physical
821 * eraseblock (@e1) belongs to. We have to read the volume identifier
824 * Note, we are protected from this PEB being unmapped and erased. The
825 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
826 * which is being moved was unmapped.
829 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
830 if (err && err != UBI_IO_BITFLIPS) {
831 if (err == UBI_IO_PEB_FREE) {
833 * We are trying to move PEB without a VID header. UBI
834 * always write VID headers shortly after the PEB was
835 * given, so we have a situation when it did not have
836 * chance to write it down because it was preempted.
837 * Just re-schedule the work, so that next time it will
838 * likely have the VID header in place.
840 dbg_wl("PEB %d has no VID header", e1->pnum);
844 ubi_err("error %d while reading VID header from PEB %d",
851 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
860 * For some reason the LEB was not moved - it might be because
861 * the volume is being deleted. We should prevent this PEB from
862 * being selected for wear-levelling movement for some "time",
863 * so put it to the protection tree.
866 dbg_wl("cancelled moving PEB %d", e1->pnum);
867 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
876 ubi_free_vid_hdr(ubi, vid_hdr);
877 spin_lock(&ubi->wl_lock);
879 prot_tree_add(ubi, e1, pe, protect);
880 if (!ubi->move_to_put)
881 wl_tree_add(e2, &ubi->used);
884 ubi->move_from = ubi->move_to = NULL;
885 ubi->move_to_put = ubi->wl_scheduled = 0;
886 spin_unlock(&ubi->wl_lock);
890 * Well, the target PEB was put meanwhile, schedule it for
893 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
894 err = schedule_erase(ubi, e2, 0);
900 err = schedule_erase(ubi, e1, 0);
907 mutex_unlock(&ubi->move_mutex);
911 * For some reasons the LEB was not moved, might be an error, might be
912 * something else. @e1 was not changed, so return it back. @e2 might
913 * be changed, schedule it for erasure.
916 ubi_free_vid_hdr(ubi, vid_hdr);
917 spin_lock(&ubi->wl_lock);
919 wl_tree_add(e1, &ubi->scrub);
921 wl_tree_add(e1, &ubi->used);
922 ubi->move_from = ubi->move_to = NULL;
923 ubi->move_to_put = ubi->wl_scheduled = 0;
924 spin_unlock(&ubi->wl_lock);
926 err = schedule_erase(ubi, e2, 0);
930 mutex_unlock(&ubi->move_mutex);
934 ubi_err("error %d while moving PEB %d to PEB %d",
935 err, e1->pnum, e2->pnum);
937 ubi_free_vid_hdr(ubi, vid_hdr);
938 spin_lock(&ubi->wl_lock);
939 ubi->move_from = ubi->move_to = NULL;
940 ubi->move_to_put = ubi->wl_scheduled = 0;
941 spin_unlock(&ubi->wl_lock);
943 kmem_cache_free(ubi_wl_entry_slab, e1);
944 kmem_cache_free(ubi_wl_entry_slab, e2);
947 mutex_unlock(&ubi->move_mutex);
951 ubi->wl_scheduled = 0;
952 spin_unlock(&ubi->wl_lock);
953 mutex_unlock(&ubi->move_mutex);
954 ubi_free_vid_hdr(ubi, vid_hdr);
959 * ensure_wear_leveling - schedule wear-leveling if it is needed.
960 * @ubi: UBI device description object
962 * This function checks if it is time to start wear-leveling and schedules it
963 * if yes. This function returns zero in case of success and a negative error
964 * code in case of failure.
966 static int ensure_wear_leveling(struct ubi_device *ubi)
969 struct ubi_wl_entry *e1;
970 struct ubi_wl_entry *e2;
971 struct ubi_work *wrk;
973 spin_lock(&ubi->wl_lock);
974 if (ubi->wl_scheduled)
975 /* Wear-leveling is already in the work queue */
979 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
980 * the WL worker has to be scheduled anyway.
982 if (!ubi->scrub.rb_node) {
983 if (!ubi->used.rb_node || !ubi->free.rb_node)
984 /* No physical eraseblocks - no deal */
988 * We schedule wear-leveling only if the difference between the
989 * lowest erase counter of used physical eraseblocks and a high
990 * erase counter of free physical eraseblocks is greater then
993 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
994 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
996 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
998 dbg_wl("schedule wear-leveling");
1000 dbg_wl("schedule scrubbing");
1002 ubi->wl_scheduled = 1;
1003 spin_unlock(&ubi->wl_lock);
1005 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1011 wrk->func = &wear_leveling_worker;
1012 schedule_ubi_work(ubi, wrk);
1016 spin_lock(&ubi->wl_lock);
1017 ubi->wl_scheduled = 0;
1019 spin_unlock(&ubi->wl_lock);
1024 * erase_worker - physical eraseblock erase worker function.
1025 * @ubi: UBI device description object
1026 * @wl_wrk: the work object
1027 * @cancel: non-zero if the worker has to free memory and exit
1029 * This function erases a physical eraseblock and perform torture testing if
1030 * needed. It also takes care about marking the physical eraseblock bad if
1031 * needed. Returns zero in case of success and a negative error code in case of
1034 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1037 struct ubi_wl_entry *e = wl_wrk->e;
1038 int pnum = e->pnum, err, need;
1041 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1043 kmem_cache_free(ubi_wl_entry_slab, e);
1047 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
1049 err = sync_erase(ubi, e, wl_wrk->torture);
1051 /* Fine, we've erased it successfully */
1054 spin_lock(&ubi->wl_lock);
1056 wl_tree_add(e, &ubi->free);
1057 spin_unlock(&ubi->wl_lock);
1060 * One more erase operation has happened, take care about protected
1061 * physical eraseblocks.
1063 check_protection_over(ubi);
1065 /* And take care about wear-leveling */
1066 err = ensure_wear_leveling(ubi);
1070 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1072 kmem_cache_free(ubi_wl_entry_slab, e);
1074 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1078 /* Re-schedule the LEB for erasure */
1079 err1 = schedule_erase(ubi, e, 0);
1085 } else if (err != -EIO) {
1087 * If this is not %-EIO, we have no idea what to do. Scheduling
1088 * this physical eraseblock for erasure again would cause
1089 * errors again and again. Well, lets switch to RO mode.
1094 /* It is %-EIO, the PEB went bad */
1096 if (!ubi->bad_allowed) {
1097 ubi_err("bad physical eraseblock %d detected", pnum);
1101 spin_lock(&ubi->volumes_lock);
1102 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1104 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1105 ubi->avail_pebs -= need;
1106 ubi->rsvd_pebs += need;
1107 ubi->beb_rsvd_pebs += need;
1109 ubi_msg("reserve more %d PEBs", need);
1112 if (ubi->beb_rsvd_pebs == 0) {
1113 spin_unlock(&ubi->volumes_lock);
1114 ubi_err("no reserved physical eraseblocks");
1118 spin_unlock(&ubi->volumes_lock);
1119 ubi_msg("mark PEB %d as bad", pnum);
1121 err = ubi_io_mark_bad(ubi, pnum);
1125 spin_lock(&ubi->volumes_lock);
1126 ubi->beb_rsvd_pebs -= 1;
1127 ubi->bad_peb_count += 1;
1128 ubi->good_peb_count -= 1;
1129 ubi_calculate_reserved(ubi);
1130 if (ubi->beb_rsvd_pebs == 0)
1131 ubi_warn("last PEB from the reserved pool was used");
1132 spin_unlock(&ubi->volumes_lock);
1142 * ubi_wl_put_peb - return a physical eraseblock to the wear-leveling unit.
1143 * @ubi: UBI device description object
1144 * @pnum: physical eraseblock to return
1145 * @torture: if this physical eraseblock has to be tortured
1147 * This function is called to return physical eraseblock @pnum to the pool of
1148 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1149 * occurred to this @pnum and it has to be tested. This function returns zero
1150 * in case of success, and a negative error code in case of failure.
1152 int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1155 struct ubi_wl_entry *e;
1157 dbg_wl("PEB %d", pnum);
1158 ubi_assert(pnum >= 0);
1159 ubi_assert(pnum < ubi->peb_count);
1162 spin_lock(&ubi->wl_lock);
1163 e = ubi->lookuptbl[pnum];
1164 if (e == ubi->move_from) {
1166 * User is putting the physical eraseblock which was selected to
1167 * be moved. It will be scheduled for erasure in the
1168 * wear-leveling worker.
1170 dbg_wl("PEB %d is being moved, wait", pnum);
1171 spin_unlock(&ubi->wl_lock);
1173 /* Wait for the WL worker by taking the @ubi->move_mutex */
1174 mutex_lock(&ubi->move_mutex);
1175 mutex_unlock(&ubi->move_mutex);
1177 } else if (e == ubi->move_to) {
1179 * User is putting the physical eraseblock which was selected
1180 * as the target the data is moved to. It may happen if the EBA
1181 * unit already re-mapped the LEB in 'ubi_eba_copy_leb()' but
1182 * the WL unit has not put the PEB to the "used" tree yet, but
1183 * it is about to do this. So we just set a flag which will
1184 * tell the WL worker that the PEB is not needed anymore and
1185 * should be scheduled for erasure.
1187 dbg_wl("PEB %d is the target of data moving", pnum);
1188 ubi_assert(!ubi->move_to_put);
1189 ubi->move_to_put = 1;
1190 spin_unlock(&ubi->wl_lock);
1193 if (in_wl_tree(e, &ubi->used)) {
1194 paranoid_check_in_wl_tree(e, &ubi->used);
1195 rb_erase(&e->rb, &ubi->used);
1196 } else if (in_wl_tree(e, &ubi->scrub)) {
1197 paranoid_check_in_wl_tree(e, &ubi->scrub);
1198 rb_erase(&e->rb, &ubi->scrub);
1200 err = prot_tree_del(ubi, e->pnum);
1202 ubi_err("PEB %d not found", pnum);
1204 spin_unlock(&ubi->wl_lock);
1209 spin_unlock(&ubi->wl_lock);
1211 err = schedule_erase(ubi, e, torture);
1213 spin_lock(&ubi->wl_lock);
1214 wl_tree_add(e, &ubi->used);
1215 spin_unlock(&ubi->wl_lock);
1222 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1223 * @ubi: UBI device description object
1224 * @pnum: the physical eraseblock to schedule
1226 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1227 * needs scrubbing. This function schedules a physical eraseblock for
1228 * scrubbing which is done in background. This function returns zero in case of
1229 * success and a negative error code in case of failure.
1231 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1233 struct ubi_wl_entry *e;
1235 ubi_msg("schedule PEB %d for scrubbing", pnum);
1238 spin_lock(&ubi->wl_lock);
1239 e = ubi->lookuptbl[pnum];
1240 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1241 spin_unlock(&ubi->wl_lock);
1245 if (e == ubi->move_to) {
1247 * This physical eraseblock was used to move data to. The data
1248 * was moved but the PEB was not yet inserted to the proper
1249 * tree. We should just wait a little and let the WL worker
1252 spin_unlock(&ubi->wl_lock);
1253 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1258 if (in_wl_tree(e, &ubi->used)) {
1259 paranoid_check_in_wl_tree(e, &ubi->used);
1260 rb_erase(&e->rb, &ubi->used);
1264 err = prot_tree_del(ubi, e->pnum);
1266 ubi_err("PEB %d not found", pnum);
1268 spin_unlock(&ubi->wl_lock);
1273 wl_tree_add(e, &ubi->scrub);
1274 spin_unlock(&ubi->wl_lock);
1277 * Technically scrubbing is the same as wear-leveling, so it is done
1280 return ensure_wear_leveling(ubi);
1284 * ubi_wl_flush - flush all pending works.
1285 * @ubi: UBI device description object
1287 * This function returns zero in case of success and a negative error code in
1290 int ubi_wl_flush(struct ubi_device *ubi)
1295 * Erase while the pending works queue is not empty, but not more then
1296 * the number of currently pending works.
1298 dbg_wl("flush (%d pending works)", ubi->works_count);
1299 while (ubi->works_count) {
1306 * Make sure all the works which have been done in parallel are
1309 down_write(&ubi->work_sem);
1310 up_write(&ubi->work_sem);
1313 * And in case last was the WL worker and it cancelled the LEB
1314 * movement, flush again.
1316 while (ubi->works_count) {
1317 dbg_wl("flush more (%d pending works)", ubi->works_count);
1327 * tree_destroy - destroy an RB-tree.
1328 * @root: the root of the tree to destroy
1330 static void tree_destroy(struct rb_root *root)
1333 struct ubi_wl_entry *e;
1339 else if (rb->rb_right)
1342 e = rb_entry(rb, struct ubi_wl_entry, rb);
1346 if (rb->rb_left == &e->rb)
1349 rb->rb_right = NULL;
1352 kmem_cache_free(ubi_wl_entry_slab, e);
1358 * ubi_thread - UBI background thread.
1359 * @u: the UBI device description object pointer
1361 static int ubi_thread(void *u)
1364 struct ubi_device *ubi = u;
1366 ubi_msg("background thread \"%s\" started, PID %d",
1367 ubi->bgt_name, task_pid_nr(current));
1373 if (kthread_should_stop())
1376 if (try_to_freeze())
1379 spin_lock(&ubi->wl_lock);
1380 if (list_empty(&ubi->works) || ubi->ro_mode ||
1381 !ubi->thread_enabled) {
1382 set_current_state(TASK_INTERRUPTIBLE);
1383 spin_unlock(&ubi->wl_lock);
1387 spin_unlock(&ubi->wl_lock);
1391 ubi_err("%s: work failed with error code %d",
1392 ubi->bgt_name, err);
1393 if (failures++ > WL_MAX_FAILURES) {
1395 * Too many failures, disable the thread and
1396 * switch to read-only mode.
1398 ubi_msg("%s: %d consecutive failures",
1399 ubi->bgt_name, WL_MAX_FAILURES);
1410 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1415 * cancel_pending - cancel all pending works.
1416 * @ubi: UBI device description object
1418 static void cancel_pending(struct ubi_device *ubi)
1420 while (!list_empty(&ubi->works)) {
1421 struct ubi_work *wrk;
1423 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1424 list_del(&wrk->list);
1425 wrk->func(ubi, wrk, 1);
1426 ubi->works_count -= 1;
1427 ubi_assert(ubi->works_count >= 0);
1432 * ubi_wl_init_scan - initialize the wear-leveling unit using scanning
1434 * @ubi: UBI device description object
1435 * @si: scanning information
1437 * This function returns zero in case of success, and a negative error code in
1440 int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1443 struct rb_node *rb1, *rb2;
1444 struct ubi_scan_volume *sv;
1445 struct ubi_scan_leb *seb, *tmp;
1446 struct ubi_wl_entry *e;
1449 ubi->used = ubi->free = ubi->scrub = RB_ROOT;
1450 ubi->prot.pnum = ubi->prot.aec = RB_ROOT;
1451 spin_lock_init(&ubi->wl_lock);
1452 mutex_init(&ubi->move_mutex);
1453 init_rwsem(&ubi->work_sem);
1454 ubi->max_ec = si->max_ec;
1455 INIT_LIST_HEAD(&ubi->works);
1457 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1459 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
1460 if (IS_ERR(ubi->bgt_thread)) {
1461 err = PTR_ERR(ubi->bgt_thread);
1462 ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
1468 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1469 if (!ubi->lookuptbl)
1472 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1475 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1479 e->pnum = seb->pnum;
1481 ubi->lookuptbl[e->pnum] = e;
1482 if (schedule_erase(ubi, e, 0)) {
1483 kmem_cache_free(ubi_wl_entry_slab, e);
1488 list_for_each_entry(seb, &si->free, u.list) {
1491 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1495 e->pnum = seb->pnum;
1497 ubi_assert(e->ec >= 0);
1498 wl_tree_add(e, &ubi->free);
1499 ubi->lookuptbl[e->pnum] = e;
1502 list_for_each_entry(seb, &si->corr, u.list) {
1505 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1509 e->pnum = seb->pnum;
1511 ubi->lookuptbl[e->pnum] = e;
1512 if (schedule_erase(ubi, e, 0)) {
1513 kmem_cache_free(ubi_wl_entry_slab, e);
1518 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1519 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1522 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1526 e->pnum = seb->pnum;
1528 ubi->lookuptbl[e->pnum] = e;
1530 dbg_wl("add PEB %d EC %d to the used tree",
1532 wl_tree_add(e, &ubi->used);
1534 dbg_wl("add PEB %d EC %d to the scrub tree",
1536 wl_tree_add(e, &ubi->scrub);
1541 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
1542 ubi_err("no enough physical eraseblocks (%d, need %d)",
1543 ubi->avail_pebs, WL_RESERVED_PEBS);
1546 ubi->avail_pebs -= WL_RESERVED_PEBS;
1547 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1549 /* Schedule wear-leveling if needed */
1550 err = ensure_wear_leveling(ubi);
1557 cancel_pending(ubi);
1558 tree_destroy(&ubi->used);
1559 tree_destroy(&ubi->free);
1560 tree_destroy(&ubi->scrub);
1561 kfree(ubi->lookuptbl);
1566 * protection_trees_destroy - destroy the protection RB-trees.
1567 * @ubi: UBI device description object
1569 static void protection_trees_destroy(struct ubi_device *ubi)
1572 struct ubi_wl_prot_entry *pe;
1574 rb = ubi->prot.aec.rb_node;
1578 else if (rb->rb_right)
1581 pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
1585 if (rb->rb_left == &pe->rb_aec)
1588 rb->rb_right = NULL;
1591 kmem_cache_free(ubi_wl_entry_slab, pe->e);
1598 * ubi_wl_close - close the wear-leveling unit.
1599 * @ubi: UBI device description object
1601 void ubi_wl_close(struct ubi_device *ubi)
1603 dbg_wl("disable \"%s\"", ubi->bgt_name);
1604 if (ubi->bgt_thread)
1605 kthread_stop(ubi->bgt_thread);
1607 dbg_wl("close the UBI wear-leveling unit");
1609 cancel_pending(ubi);
1610 protection_trees_destroy(ubi);
1611 tree_destroy(&ubi->used);
1612 tree_destroy(&ubi->free);
1613 tree_destroy(&ubi->scrub);
1614 kfree(ubi->lookuptbl);
1617 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1620 * paranoid_check_ec - make sure that the erase counter of a physical eraseblock
1622 * @ubi: UBI device description object
1623 * @pnum: the physical eraseblock number to check
1624 * @ec: the erase counter to check
1626 * This function returns zero if the erase counter of physical eraseblock @pnum
1627 * is equivalent to @ec, %1 if not, and a negative error code if an error
1630 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1634 struct ubi_ec_hdr *ec_hdr;
1636 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1640 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1641 if (err && err != UBI_IO_BITFLIPS) {
1642 /* The header does not have to exist */
1647 read_ec = be64_to_cpu(ec_hdr->ec);
1648 if (ec != read_ec) {
1649 ubi_err("paranoid check failed for PEB %d", pnum);
1650 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1651 ubi_dbg_dump_stack();
1662 * paranoid_check_in_wl_tree - make sure that a wear-leveling entry is present
1664 * @e: the wear-leveling entry to check
1665 * @root: the root of the tree
1667 * This function returns zero if @e is in the @root RB-tree and %1 if it
1670 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1671 struct rb_root *root)
1673 if (in_wl_tree(e, root))
1676 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1677 e->pnum, e->ec, root);
1678 ubi_dbg_dump_stack();
1682 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */