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);
262 if (list_empty(&ubi->works)) {
263 spin_unlock(&ubi->wl_lock);
264 up_read(&ubi->work_sem);
268 wrk = list_entry(ubi->works.next, struct ubi_work, list);
269 list_del(&wrk->list);
270 ubi->works_count -= 1;
271 ubi_assert(ubi->works_count >= 0);
272 spin_unlock(&ubi->wl_lock);
275 * Call the worker function. Do not touch the work structure
276 * after this call as it will have been freed or reused by that
277 * time by the worker function.
279 err = wrk->func(ubi, wrk, 0);
281 ubi_err("work failed with error code %d", err);
282 up_read(&ubi->work_sem);
288 * produce_free_peb - produce a free physical eraseblock.
289 * @ubi: UBI device description object
291 * This function tries to make a free PEB by means of synchronous execution of
292 * pending works. This may be needed if, for example the background thread is
293 * disabled. Returns zero in case of success and a negative error code in case
296 static int produce_free_peb(struct ubi_device *ubi)
300 spin_lock(&ubi->wl_lock);
301 while (!ubi->free.rb_node) {
302 spin_unlock(&ubi->wl_lock);
304 dbg_wl("do one work synchronously");
309 spin_lock(&ubi->wl_lock);
311 spin_unlock(&ubi->wl_lock);
317 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
318 * @e: the wear-leveling entry to check
319 * @root: the root of the tree
321 * This function returns non-zero if @e is in the @root RB-tree and zero if it
324 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
330 struct ubi_wl_entry *e1;
332 e1 = rb_entry(p, struct ubi_wl_entry, rb);
334 if (e->pnum == e1->pnum) {
341 else if (e->ec > e1->ec)
344 ubi_assert(e->pnum != e1->pnum);
345 if (e->pnum < e1->pnum)
356 * prot_tree_add - add physical eraseblock to protection trees.
357 * @ubi: UBI device description object
358 * @e: the physical eraseblock to add
359 * @pe: protection entry object to use
360 * @abs_ec: absolute erase counter value when this physical eraseblock has
361 * to be removed from the protection trees.
363 * @wl->lock has to be locked.
365 static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
366 struct ubi_wl_prot_entry *pe, int abs_ec)
368 struct rb_node **p, *parent = NULL;
369 struct ubi_wl_prot_entry *pe1;
372 pe->abs_ec = ubi->abs_ec + abs_ec;
374 p = &ubi->prot.pnum.rb_node;
377 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
379 if (e->pnum < pe1->e->pnum)
384 rb_link_node(&pe->rb_pnum, parent, p);
385 rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
387 p = &ubi->prot.aec.rb_node;
391 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
393 if (pe->abs_ec < pe1->abs_ec)
398 rb_link_node(&pe->rb_aec, parent, p);
399 rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
403 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
404 * @root: the RB-tree where to look for
405 * @max: highest possible erase counter
407 * This function looks for a wear leveling entry with erase counter closest to
408 * @max and less then @max.
410 static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
413 struct ubi_wl_entry *e;
415 e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
420 struct ubi_wl_entry *e1;
422 e1 = rb_entry(p, struct ubi_wl_entry, rb);
435 * ubi_wl_get_peb - get a physical eraseblock.
436 * @ubi: UBI device description object
437 * @dtype: type of data which will be stored in this physical eraseblock
439 * This function returns a physical eraseblock in case of success and a
440 * negative error code in case of failure. Might sleep.
442 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
444 int err, protect, medium_ec;
445 struct ubi_wl_entry *e, *first, *last;
446 struct ubi_wl_prot_entry *pe;
448 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
449 dtype == UBI_UNKNOWN);
451 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
456 spin_lock(&ubi->wl_lock);
457 if (!ubi->free.rb_node) {
458 if (ubi->works_count == 0) {
459 ubi_assert(list_empty(&ubi->works));
460 ubi_err("no free eraseblocks");
461 spin_unlock(&ubi->wl_lock);
465 spin_unlock(&ubi->wl_lock);
467 err = produce_free_peb(ubi);
478 * For long term data we pick a physical eraseblock
479 * with high erase counter. But the highest erase
480 * counter we can pick is bounded by the the lowest
481 * erase counter plus %WL_FREE_MAX_DIFF.
483 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
484 protect = LT_PROTECTION;
488 * For unknown data we pick a physical eraseblock with
489 * medium erase counter. But we by no means can pick a
490 * physical eraseblock with erase counter greater or
491 * equivalent than the lowest erase counter plus
494 first = rb_entry(rb_first(&ubi->free),
495 struct ubi_wl_entry, rb);
496 last = rb_entry(rb_last(&ubi->free),
497 struct ubi_wl_entry, rb);
499 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
500 e = rb_entry(ubi->free.rb_node,
501 struct ubi_wl_entry, rb);
503 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
504 e = find_wl_entry(&ubi->free, medium_ec);
506 protect = U_PROTECTION;
510 * For short term data we pick a physical eraseblock
511 * with the lowest erase counter as we expect it will
514 e = rb_entry(rb_first(&ubi->free),
515 struct ubi_wl_entry, rb);
516 protect = ST_PROTECTION;
525 * Move the physical eraseblock to the protection trees where it will
526 * be protected from being moved for some time.
528 paranoid_check_in_wl_tree(e, &ubi->free);
529 rb_erase(&e->rb, &ubi->free);
530 prot_tree_add(ubi, e, pe, protect);
532 dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
533 spin_unlock(&ubi->wl_lock);
539 * prot_tree_del - remove a physical eraseblock from the protection trees
540 * @ubi: UBI device description object
541 * @pnum: the physical eraseblock to remove
543 * This function returns PEB @pnum from the protection trees and returns zero
544 * in case of success and %-ENODEV if the PEB was not found in the protection
547 static int prot_tree_del(struct ubi_device *ubi, int pnum)
550 struct ubi_wl_prot_entry *pe = NULL;
552 p = ubi->prot.pnum.rb_node;
555 pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
557 if (pnum == pe->e->pnum)
560 if (pnum < pe->e->pnum)
569 ubi_assert(pe->e->pnum == pnum);
570 rb_erase(&pe->rb_aec, &ubi->prot.aec);
571 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
577 * sync_erase - synchronously erase a physical eraseblock.
578 * @ubi: UBI device description object
579 * @e: the the physical eraseblock to erase
580 * @torture: if the physical eraseblock has to be tortured
582 * This function returns zero in case of success and a negative error code in
585 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture)
588 struct ubi_ec_hdr *ec_hdr;
589 unsigned long long ec = e->ec;
591 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
593 err = paranoid_check_ec(ubi, e->pnum, e->ec);
597 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
601 err = ubi_io_sync_erase(ubi, e->pnum, torture);
606 if (ec > UBI_MAX_ERASECOUNTER) {
608 * Erase counter overflow. Upgrade UBI and use 64-bit
609 * erase counters internally.
611 ubi_err("erase counter overflow at PEB %d, EC %llu",
617 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
619 ec_hdr->ec = cpu_to_be64(ec);
621 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
626 spin_lock(&ubi->wl_lock);
627 if (e->ec > ubi->max_ec)
629 spin_unlock(&ubi->wl_lock);
637 * check_protection_over - check if it is time to stop protecting some
638 * physical eraseblocks.
639 * @ubi: UBI device description object
641 * This function is called after each erase operation, when the absolute erase
642 * counter is incremented, to check if some physical eraseblock have not to be
643 * protected any longer. These physical eraseblocks are moved from the
644 * protection trees to the used tree.
646 static void check_protection_over(struct ubi_device *ubi)
648 struct ubi_wl_prot_entry *pe;
651 * There may be several protected physical eraseblock to remove,
655 spin_lock(&ubi->wl_lock);
656 if (!ubi->prot.aec.rb_node) {
657 spin_unlock(&ubi->wl_lock);
661 pe = rb_entry(rb_first(&ubi->prot.aec),
662 struct ubi_wl_prot_entry, rb_aec);
664 if (pe->abs_ec > ubi->abs_ec) {
665 spin_unlock(&ubi->wl_lock);
669 dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
670 pe->e->pnum, ubi->abs_ec, pe->abs_ec);
671 rb_erase(&pe->rb_aec, &ubi->prot.aec);
672 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
673 wl_tree_add(pe->e, &ubi->used);
674 spin_unlock(&ubi->wl_lock);
682 * schedule_ubi_work - schedule a work.
683 * @ubi: UBI device description object
684 * @wrk: the work to schedule
686 * This function enqueues a work defined by @wrk to the tail of the pending
689 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
691 spin_lock(&ubi->wl_lock);
692 list_add_tail(&wrk->list, &ubi->works);
693 ubi_assert(ubi->works_count >= 0);
694 ubi->works_count += 1;
695 if (ubi->thread_enabled)
696 wake_up_process(ubi->bgt_thread);
697 spin_unlock(&ubi->wl_lock);
700 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
704 * schedule_erase - schedule an erase work.
705 * @ubi: UBI device description object
706 * @e: the WL entry of the physical eraseblock to erase
707 * @torture: if the physical eraseblock has to be tortured
709 * This function returns zero in case of success and a %-ENOMEM in case of
712 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
715 struct ubi_work *wl_wrk;
717 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
718 e->pnum, e->ec, torture);
720 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
724 wl_wrk->func = &erase_worker;
726 wl_wrk->torture = torture;
728 schedule_ubi_work(ubi, wl_wrk);
733 * wear_leveling_worker - wear-leveling worker function.
734 * @ubi: UBI device description object
735 * @wrk: the work object
736 * @cancel: non-zero if the worker has to free memory and exit
738 * This function copies a more worn out physical eraseblock to a less worn out
739 * one. Returns zero in case of success and a negative error code in case of
742 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
745 int err, put = 0, scrubbing = 0, protect = 0;
746 struct ubi_wl_prot_entry *uninitialized_var(pe);
747 struct ubi_wl_entry *e1, *e2;
748 struct ubi_vid_hdr *vid_hdr;
755 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
759 mutex_lock(&ubi->move_mutex);
760 spin_lock(&ubi->wl_lock);
761 ubi_assert(!ubi->move_from && !ubi->move_to);
762 ubi_assert(!ubi->move_to_put);
764 if (!ubi->free.rb_node ||
765 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
767 * No free physical eraseblocks? Well, they must be waiting in
768 * the queue to be erased. Cancel movement - it will be
769 * triggered again when a free physical eraseblock appears.
771 * No used physical eraseblocks? They must be temporarily
772 * protected from being moved. They will be moved to the
773 * @ubi->used tree later and the wear-leveling will be
776 dbg_wl("cancel WL, a list is empty: free %d, used %d",
777 !ubi->free.rb_node, !ubi->used.rb_node);
781 if (!ubi->scrub.rb_node) {
783 * Now pick the least worn-out used physical eraseblock and a
784 * highly worn-out free physical eraseblock. If the erase
785 * counters differ much enough, start wear-leveling.
787 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
788 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
790 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
791 dbg_wl("no WL needed: min used EC %d, max free EC %d",
795 paranoid_check_in_wl_tree(e1, &ubi->used);
796 rb_erase(&e1->rb, &ubi->used);
797 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
798 e1->pnum, e1->ec, e2->pnum, e2->ec);
800 /* Perform scrubbing */
802 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
803 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
804 paranoid_check_in_wl_tree(e1, &ubi->scrub);
805 rb_erase(&e1->rb, &ubi->scrub);
806 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
809 paranoid_check_in_wl_tree(e2, &ubi->free);
810 rb_erase(&e2->rb, &ubi->free);
813 spin_unlock(&ubi->wl_lock);
816 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
817 * We so far do not know which logical eraseblock our physical
818 * eraseblock (@e1) belongs to. We have to read the volume identifier
821 * Note, we are protected from this PEB being unmapped and erased. The
822 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
823 * which is being moved was unmapped.
826 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
827 if (err && err != UBI_IO_BITFLIPS) {
828 if (err == UBI_IO_PEB_FREE) {
830 * We are trying to move PEB without a VID header. UBI
831 * always write VID headers shortly after the PEB was
832 * given, so we have a situation when it did not have
833 * chance to write it down because it was preempted.
834 * Just re-schedule the work, so that next time it will
835 * likely have the VID header in place.
837 dbg_wl("PEB %d has no VID header", e1->pnum);
841 ubi_err("error %d while reading VID header from PEB %d",
848 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
857 * For some reason the LEB was not moved - it might be because
858 * the volume is being deleted. We should prevent this PEB from
859 * being selected for wear-levelling movement for some "time",
860 * so put it to the protection tree.
863 dbg_wl("cancelled moving PEB %d", e1->pnum);
864 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
873 ubi_free_vid_hdr(ubi, vid_hdr);
874 spin_lock(&ubi->wl_lock);
876 prot_tree_add(ubi, e1, pe, protect);
877 if (!ubi->move_to_put)
878 wl_tree_add(e2, &ubi->used);
881 ubi->move_from = ubi->move_to = NULL;
882 ubi->move_to_put = ubi->wl_scheduled = 0;
883 spin_unlock(&ubi->wl_lock);
887 * Well, the target PEB was put meanwhile, schedule it for
890 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
891 err = schedule_erase(ubi, e2, 0);
897 err = schedule_erase(ubi, e1, 0);
904 mutex_unlock(&ubi->move_mutex);
908 * For some reasons the LEB was not moved, might be an error, might be
909 * something else. @e1 was not changed, so return it back. @e2 might
910 * be changed, schedule it for erasure.
913 ubi_free_vid_hdr(ubi, vid_hdr);
914 spin_lock(&ubi->wl_lock);
916 wl_tree_add(e1, &ubi->scrub);
918 wl_tree_add(e1, &ubi->used);
919 ubi->move_from = ubi->move_to = NULL;
920 ubi->move_to_put = ubi->wl_scheduled = 0;
921 spin_unlock(&ubi->wl_lock);
923 err = schedule_erase(ubi, e2, 0);
927 mutex_unlock(&ubi->move_mutex);
931 ubi_err("error %d while moving PEB %d to PEB %d",
932 err, e1->pnum, e2->pnum);
934 ubi_free_vid_hdr(ubi, vid_hdr);
935 spin_lock(&ubi->wl_lock);
936 ubi->move_from = ubi->move_to = NULL;
937 ubi->move_to_put = ubi->wl_scheduled = 0;
938 spin_unlock(&ubi->wl_lock);
940 kmem_cache_free(ubi_wl_entry_slab, e1);
941 kmem_cache_free(ubi_wl_entry_slab, e2);
944 mutex_unlock(&ubi->move_mutex);
948 ubi->wl_scheduled = 0;
949 spin_unlock(&ubi->wl_lock);
950 mutex_unlock(&ubi->move_mutex);
951 ubi_free_vid_hdr(ubi, vid_hdr);
956 * ensure_wear_leveling - schedule wear-leveling if it is needed.
957 * @ubi: UBI device description object
959 * This function checks if it is time to start wear-leveling and schedules it
960 * if yes. This function returns zero in case of success and a negative error
961 * code in case of failure.
963 static int ensure_wear_leveling(struct ubi_device *ubi)
966 struct ubi_wl_entry *e1;
967 struct ubi_wl_entry *e2;
968 struct ubi_work *wrk;
970 spin_lock(&ubi->wl_lock);
971 if (ubi->wl_scheduled)
972 /* Wear-leveling is already in the work queue */
976 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
977 * the WL worker has to be scheduled anyway.
979 if (!ubi->scrub.rb_node) {
980 if (!ubi->used.rb_node || !ubi->free.rb_node)
981 /* No physical eraseblocks - no deal */
985 * We schedule wear-leveling only if the difference between the
986 * lowest erase counter of used physical eraseblocks and a high
987 * erase counter of free physical eraseblocks is greater then
990 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
991 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
993 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
995 dbg_wl("schedule wear-leveling");
997 dbg_wl("schedule scrubbing");
999 ubi->wl_scheduled = 1;
1000 spin_unlock(&ubi->wl_lock);
1002 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1008 wrk->func = &wear_leveling_worker;
1009 schedule_ubi_work(ubi, wrk);
1013 spin_lock(&ubi->wl_lock);
1014 ubi->wl_scheduled = 0;
1016 spin_unlock(&ubi->wl_lock);
1021 * erase_worker - physical eraseblock erase worker function.
1022 * @ubi: UBI device description object
1023 * @wl_wrk: the work object
1024 * @cancel: non-zero if the worker has to free memory and exit
1026 * This function erases a physical eraseblock and perform torture testing if
1027 * needed. It also takes care about marking the physical eraseblock bad if
1028 * needed. Returns zero in case of success and a negative error code in case of
1031 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1034 struct ubi_wl_entry *e = wl_wrk->e;
1035 int pnum = e->pnum, err, need;
1038 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1040 kmem_cache_free(ubi_wl_entry_slab, e);
1044 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
1046 err = sync_erase(ubi, e, wl_wrk->torture);
1048 /* Fine, we've erased it successfully */
1051 spin_lock(&ubi->wl_lock);
1053 wl_tree_add(e, &ubi->free);
1054 spin_unlock(&ubi->wl_lock);
1057 * One more erase operation has happened, take care about protected
1058 * physical eraseblocks.
1060 check_protection_over(ubi);
1062 /* And take care about wear-leveling */
1063 err = ensure_wear_leveling(ubi);
1067 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1069 kmem_cache_free(ubi_wl_entry_slab, e);
1071 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1075 /* Re-schedule the LEB for erasure */
1076 err1 = schedule_erase(ubi, e, 0);
1082 } else if (err != -EIO) {
1084 * If this is not %-EIO, we have no idea what to do. Scheduling
1085 * this physical eraseblock for erasure again would cause
1086 * errors again and again. Well, lets switch to RO mode.
1091 /* It is %-EIO, the PEB went bad */
1093 if (!ubi->bad_allowed) {
1094 ubi_err("bad physical eraseblock %d detected", pnum);
1098 spin_lock(&ubi->volumes_lock);
1099 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1101 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1102 ubi->avail_pebs -= need;
1103 ubi->rsvd_pebs += need;
1104 ubi->beb_rsvd_pebs += need;
1106 ubi_msg("reserve more %d PEBs", need);
1109 if (ubi->beb_rsvd_pebs == 0) {
1110 spin_unlock(&ubi->volumes_lock);
1111 ubi_err("no reserved physical eraseblocks");
1115 spin_unlock(&ubi->volumes_lock);
1116 ubi_msg("mark PEB %d as bad", pnum);
1118 err = ubi_io_mark_bad(ubi, pnum);
1122 spin_lock(&ubi->volumes_lock);
1123 ubi->beb_rsvd_pebs -= 1;
1124 ubi->bad_peb_count += 1;
1125 ubi->good_peb_count -= 1;
1126 ubi_calculate_reserved(ubi);
1127 if (ubi->beb_rsvd_pebs == 0)
1128 ubi_warn("last PEB from the reserved pool was used");
1129 spin_unlock(&ubi->volumes_lock);
1139 * ubi_wl_put_peb - return a physical eraseblock to the wear-leveling unit.
1140 * @ubi: UBI device description object
1141 * @pnum: physical eraseblock to return
1142 * @torture: if this physical eraseblock has to be tortured
1144 * This function is called to return physical eraseblock @pnum to the pool of
1145 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1146 * occurred to this @pnum and it has to be tested. This function returns zero
1147 * in case of success, and a negative error code in case of failure.
1149 int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1152 struct ubi_wl_entry *e;
1154 dbg_wl("PEB %d", pnum);
1155 ubi_assert(pnum >= 0);
1156 ubi_assert(pnum < ubi->peb_count);
1159 spin_lock(&ubi->wl_lock);
1160 e = ubi->lookuptbl[pnum];
1161 if (e == ubi->move_from) {
1163 * User is putting the physical eraseblock which was selected to
1164 * be moved. It will be scheduled for erasure in the
1165 * wear-leveling worker.
1167 dbg_wl("PEB %d is being moved, wait", pnum);
1168 spin_unlock(&ubi->wl_lock);
1170 /* Wait for the WL worker by taking the @ubi->move_mutex */
1171 mutex_lock(&ubi->move_mutex);
1172 mutex_unlock(&ubi->move_mutex);
1174 } else if (e == ubi->move_to) {
1176 * User is putting the physical eraseblock which was selected
1177 * as the target the data is moved to. It may happen if the EBA
1178 * unit already re-mapped the LEB in 'ubi_eba_copy_leb()' but
1179 * the WL unit has not put the PEB to the "used" tree yet, but
1180 * it is about to do this. So we just set a flag which will
1181 * tell the WL worker that the PEB is not needed anymore and
1182 * should be scheduled for erasure.
1184 dbg_wl("PEB %d is the target of data moving", pnum);
1185 ubi_assert(!ubi->move_to_put);
1186 ubi->move_to_put = 1;
1187 spin_unlock(&ubi->wl_lock);
1190 if (in_wl_tree(e, &ubi->used)) {
1191 paranoid_check_in_wl_tree(e, &ubi->used);
1192 rb_erase(&e->rb, &ubi->used);
1193 } else if (in_wl_tree(e, &ubi->scrub)) {
1194 paranoid_check_in_wl_tree(e, &ubi->scrub);
1195 rb_erase(&e->rb, &ubi->scrub);
1197 err = prot_tree_del(ubi, e->pnum);
1199 ubi_err("PEB %d not found", pnum);
1201 spin_unlock(&ubi->wl_lock);
1206 spin_unlock(&ubi->wl_lock);
1208 err = schedule_erase(ubi, e, torture);
1210 spin_lock(&ubi->wl_lock);
1211 wl_tree_add(e, &ubi->used);
1212 spin_unlock(&ubi->wl_lock);
1219 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1220 * @ubi: UBI device description object
1221 * @pnum: the physical eraseblock to schedule
1223 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1224 * needs scrubbing. This function schedules a physical eraseblock for
1225 * scrubbing which is done in background. This function returns zero in case of
1226 * success and a negative error code in case of failure.
1228 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1230 struct ubi_wl_entry *e;
1232 ubi_msg("schedule PEB %d for scrubbing", pnum);
1235 spin_lock(&ubi->wl_lock);
1236 e = ubi->lookuptbl[pnum];
1237 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1238 spin_unlock(&ubi->wl_lock);
1242 if (e == ubi->move_to) {
1244 * This physical eraseblock was used to move data to. The data
1245 * was moved but the PEB was not yet inserted to the proper
1246 * tree. We should just wait a little and let the WL worker
1249 spin_unlock(&ubi->wl_lock);
1250 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1255 if (in_wl_tree(e, &ubi->used)) {
1256 paranoid_check_in_wl_tree(e, &ubi->used);
1257 rb_erase(&e->rb, &ubi->used);
1261 err = prot_tree_del(ubi, e->pnum);
1263 ubi_err("PEB %d not found", pnum);
1265 spin_unlock(&ubi->wl_lock);
1270 wl_tree_add(e, &ubi->scrub);
1271 spin_unlock(&ubi->wl_lock);
1274 * Technically scrubbing is the same as wear-leveling, so it is done
1277 return ensure_wear_leveling(ubi);
1281 * ubi_wl_flush - flush all pending works.
1282 * @ubi: UBI device description object
1284 * This function returns zero in case of success and a negative error code in
1287 int ubi_wl_flush(struct ubi_device *ubi)
1292 * Erase while the pending works queue is not empty, but not more then
1293 * the number of currently pending works.
1295 dbg_wl("flush (%d pending works)", ubi->works_count);
1296 while (ubi->works_count) {
1303 * Make sure all the works which have been done in parallel are
1306 down_write(&ubi->work_sem);
1307 up_write(&ubi->work_sem);
1310 * And in case last was the WL worker and it cancelled the LEB
1311 * movement, flush again.
1313 while (ubi->works_count) {
1314 dbg_wl("flush more (%d pending works)", ubi->works_count);
1324 * tree_destroy - destroy an RB-tree.
1325 * @root: the root of the tree to destroy
1327 static void tree_destroy(struct rb_root *root)
1330 struct ubi_wl_entry *e;
1336 else if (rb->rb_right)
1339 e = rb_entry(rb, struct ubi_wl_entry, rb);
1343 if (rb->rb_left == &e->rb)
1346 rb->rb_right = NULL;
1349 kmem_cache_free(ubi_wl_entry_slab, e);
1355 * ubi_thread - UBI background thread.
1356 * @u: the UBI device description object pointer
1358 int ubi_thread(void *u)
1361 struct ubi_device *ubi = u;
1363 ubi_msg("background thread \"%s\" started, PID %d",
1364 ubi->bgt_name, task_pid_nr(current));
1370 if (kthread_should_stop())
1373 if (try_to_freeze())
1376 spin_lock(&ubi->wl_lock);
1377 if (list_empty(&ubi->works) || ubi->ro_mode ||
1378 !ubi->thread_enabled) {
1379 set_current_state(TASK_INTERRUPTIBLE);
1380 spin_unlock(&ubi->wl_lock);
1384 spin_unlock(&ubi->wl_lock);
1388 ubi_err("%s: work failed with error code %d",
1389 ubi->bgt_name, err);
1390 if (failures++ > WL_MAX_FAILURES) {
1392 * Too many failures, disable the thread and
1393 * switch to read-only mode.
1395 ubi_msg("%s: %d consecutive failures",
1396 ubi->bgt_name, WL_MAX_FAILURES);
1407 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1412 * cancel_pending - cancel all pending works.
1413 * @ubi: UBI device description object
1415 static void cancel_pending(struct ubi_device *ubi)
1417 while (!list_empty(&ubi->works)) {
1418 struct ubi_work *wrk;
1420 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1421 list_del(&wrk->list);
1422 wrk->func(ubi, wrk, 1);
1423 ubi->works_count -= 1;
1424 ubi_assert(ubi->works_count >= 0);
1429 * ubi_wl_init_scan - initialize the wear-leveling unit using scanning
1431 * @ubi: UBI device description object
1432 * @si: scanning information
1434 * This function returns zero in case of success, and a negative error code in
1437 int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1440 struct rb_node *rb1, *rb2;
1441 struct ubi_scan_volume *sv;
1442 struct ubi_scan_leb *seb, *tmp;
1443 struct ubi_wl_entry *e;
1446 ubi->used = ubi->free = ubi->scrub = RB_ROOT;
1447 ubi->prot.pnum = ubi->prot.aec = RB_ROOT;
1448 spin_lock_init(&ubi->wl_lock);
1449 mutex_init(&ubi->move_mutex);
1450 init_rwsem(&ubi->work_sem);
1451 ubi->max_ec = si->max_ec;
1452 INIT_LIST_HEAD(&ubi->works);
1454 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1457 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1458 if (!ubi->lookuptbl)
1461 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1464 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1468 e->pnum = seb->pnum;
1470 ubi->lookuptbl[e->pnum] = e;
1471 if (schedule_erase(ubi, e, 0)) {
1472 kmem_cache_free(ubi_wl_entry_slab, e);
1477 list_for_each_entry(seb, &si->free, u.list) {
1480 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1484 e->pnum = seb->pnum;
1486 ubi_assert(e->ec >= 0);
1487 wl_tree_add(e, &ubi->free);
1488 ubi->lookuptbl[e->pnum] = e;
1491 list_for_each_entry(seb, &si->corr, u.list) {
1494 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1498 e->pnum = seb->pnum;
1500 ubi->lookuptbl[e->pnum] = e;
1501 if (schedule_erase(ubi, e, 0)) {
1502 kmem_cache_free(ubi_wl_entry_slab, e);
1507 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1508 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1511 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1515 e->pnum = seb->pnum;
1517 ubi->lookuptbl[e->pnum] = e;
1519 dbg_wl("add PEB %d EC %d to the used tree",
1521 wl_tree_add(e, &ubi->used);
1523 dbg_wl("add PEB %d EC %d to the scrub tree",
1525 wl_tree_add(e, &ubi->scrub);
1530 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
1531 ubi_err("no enough physical eraseblocks (%d, need %d)",
1532 ubi->avail_pebs, WL_RESERVED_PEBS);
1535 ubi->avail_pebs -= WL_RESERVED_PEBS;
1536 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1538 /* Schedule wear-leveling if needed */
1539 err = ensure_wear_leveling(ubi);
1546 cancel_pending(ubi);
1547 tree_destroy(&ubi->used);
1548 tree_destroy(&ubi->free);
1549 tree_destroy(&ubi->scrub);
1550 kfree(ubi->lookuptbl);
1555 * protection_trees_destroy - destroy the protection RB-trees.
1556 * @ubi: UBI device description object
1558 static void protection_trees_destroy(struct ubi_device *ubi)
1561 struct ubi_wl_prot_entry *pe;
1563 rb = ubi->prot.aec.rb_node;
1567 else if (rb->rb_right)
1570 pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
1574 if (rb->rb_left == &pe->rb_aec)
1577 rb->rb_right = NULL;
1580 kmem_cache_free(ubi_wl_entry_slab, pe->e);
1587 * ubi_wl_close - close the wear-leveling unit.
1588 * @ubi: UBI device description object
1590 void ubi_wl_close(struct ubi_device *ubi)
1592 dbg_wl("close the UBI wear-leveling unit");
1594 cancel_pending(ubi);
1595 protection_trees_destroy(ubi);
1596 tree_destroy(&ubi->used);
1597 tree_destroy(&ubi->free);
1598 tree_destroy(&ubi->scrub);
1599 kfree(ubi->lookuptbl);
1602 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1605 * paranoid_check_ec - make sure that the erase counter of a physical eraseblock
1607 * @ubi: UBI device description object
1608 * @pnum: the physical eraseblock number to check
1609 * @ec: the erase counter to check
1611 * This function returns zero if the erase counter of physical eraseblock @pnum
1612 * is equivalent to @ec, %1 if not, and a negative error code if an error
1615 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1619 struct ubi_ec_hdr *ec_hdr;
1621 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1625 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1626 if (err && err != UBI_IO_BITFLIPS) {
1627 /* The header does not have to exist */
1632 read_ec = be64_to_cpu(ec_hdr->ec);
1633 if (ec != read_ec) {
1634 ubi_err("paranoid check failed for PEB %d", pnum);
1635 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1636 ubi_dbg_dump_stack();
1647 * paranoid_check_in_wl_tree - make sure that a wear-leveling entry is present
1649 * @e: the wear-leveling entry to check
1650 * @root: the root of the tree
1652 * This function returns zero if @e is in the @root RB-tree and %1 if it
1655 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1656 struct rb_root *root)
1658 if (in_wl_tree(e, root))
1661 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1662 e->pnum, e->ec, root);
1663 ubi_dbg_dump_stack();
1667 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */