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;
252 spin_lock(&ubi->wl_lock);
254 if (list_empty(&ubi->works)) {
255 spin_unlock(&ubi->wl_lock);
259 wrk = list_entry(ubi->works.next, struct ubi_work, list);
260 list_del(&wrk->list);
261 spin_unlock(&ubi->wl_lock);
264 * Call the worker function. Do not touch the work structure
265 * after this call as it will have been freed or reused by that
266 * time by the worker function.
268 err = wrk->func(ubi, wrk, 0);
270 ubi_err("work failed with error code %d", err);
272 spin_lock(&ubi->wl_lock);
273 ubi->works_count -= 1;
274 ubi_assert(ubi->works_count >= 0);
275 spin_unlock(&ubi->wl_lock);
280 * produce_free_peb - produce a free physical eraseblock.
281 * @ubi: UBI device description object
283 * This function tries to make a free PEB by means of synchronous execution of
284 * pending works. This may be needed if, for example the background thread is
285 * disabled. Returns zero in case of success and a negative error code in case
288 static int produce_free_peb(struct ubi_device *ubi)
292 spin_lock(&ubi->wl_lock);
293 while (!ubi->free.rb_node) {
294 spin_unlock(&ubi->wl_lock);
296 dbg_wl("do one work synchronously");
301 spin_lock(&ubi->wl_lock);
303 spin_unlock(&ubi->wl_lock);
309 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
310 * @e: the wear-leveling entry to check
311 * @root: the root of the tree
313 * This function returns non-zero if @e is in the @root RB-tree and zero if it
316 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
322 struct ubi_wl_entry *e1;
324 e1 = rb_entry(p, struct ubi_wl_entry, rb);
326 if (e->pnum == e1->pnum) {
333 else if (e->ec > e1->ec)
336 ubi_assert(e->pnum != e1->pnum);
337 if (e->pnum < e1->pnum)
348 * prot_tree_add - add physical eraseblock to protection trees.
349 * @ubi: UBI device description object
350 * @e: the physical eraseblock to add
351 * @pe: protection entry object to use
352 * @abs_ec: absolute erase counter value when this physical eraseblock has
353 * to be removed from the protection trees.
355 * @wl->lock has to be locked.
357 static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
358 struct ubi_wl_prot_entry *pe, int abs_ec)
360 struct rb_node **p, *parent = NULL;
361 struct ubi_wl_prot_entry *pe1;
364 pe->abs_ec = ubi->abs_ec + abs_ec;
366 p = &ubi->prot.pnum.rb_node;
369 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
371 if (e->pnum < pe1->e->pnum)
376 rb_link_node(&pe->rb_pnum, parent, p);
377 rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
379 p = &ubi->prot.aec.rb_node;
383 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
385 if (pe->abs_ec < pe1->abs_ec)
390 rb_link_node(&pe->rb_aec, parent, p);
391 rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
395 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
396 * @root: the RB-tree where to look for
397 * @max: highest possible erase counter
399 * This function looks for a wear leveling entry with erase counter closest to
400 * @max and less then @max.
402 static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
405 struct ubi_wl_entry *e;
407 e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
412 struct ubi_wl_entry *e1;
414 e1 = rb_entry(p, struct ubi_wl_entry, rb);
427 * ubi_wl_get_peb - get a physical eraseblock.
428 * @ubi: UBI device description object
429 * @dtype: type of data which will be stored in this physical eraseblock
431 * This function returns a physical eraseblock in case of success and a
432 * negative error code in case of failure. Might sleep.
434 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
436 int err, protect, medium_ec;
437 struct ubi_wl_entry *e, *first, *last;
438 struct ubi_wl_prot_entry *pe;
440 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
441 dtype == UBI_UNKNOWN);
443 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
448 spin_lock(&ubi->wl_lock);
449 if (!ubi->free.rb_node) {
450 if (ubi->works_count == 0) {
451 ubi_assert(list_empty(&ubi->works));
452 ubi_err("no free eraseblocks");
453 spin_unlock(&ubi->wl_lock);
457 spin_unlock(&ubi->wl_lock);
459 err = produce_free_peb(ubi);
470 * For long term data we pick a physical eraseblock
471 * with high erase counter. But the highest erase
472 * counter we can pick is bounded by the the lowest
473 * erase counter plus %WL_FREE_MAX_DIFF.
475 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
476 protect = LT_PROTECTION;
480 * For unknown data we pick a physical eraseblock with
481 * medium erase counter. But we by no means can pick a
482 * physical eraseblock with erase counter greater or
483 * equivalent than the lowest erase counter plus
486 first = rb_entry(rb_first(&ubi->free),
487 struct ubi_wl_entry, rb);
488 last = rb_entry(rb_last(&ubi->free),
489 struct ubi_wl_entry, rb);
491 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
492 e = rb_entry(ubi->free.rb_node,
493 struct ubi_wl_entry, rb);
495 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
496 e = find_wl_entry(&ubi->free, medium_ec);
498 protect = U_PROTECTION;
502 * For short term data we pick a physical eraseblock
503 * with the lowest erase counter as we expect it will
506 e = rb_entry(rb_first(&ubi->free),
507 struct ubi_wl_entry, rb);
508 protect = ST_PROTECTION;
517 * Move the physical eraseblock to the protection trees where it will
518 * be protected from being moved for some time.
520 paranoid_check_in_wl_tree(e, &ubi->free);
521 rb_erase(&e->rb, &ubi->free);
522 prot_tree_add(ubi, e, pe, protect);
524 dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
525 spin_unlock(&ubi->wl_lock);
531 * prot_tree_del - remove a physical eraseblock from the protection trees
532 * @ubi: UBI device description object
533 * @pnum: the physical eraseblock to remove
535 static void prot_tree_del(struct ubi_device *ubi, int pnum)
538 struct ubi_wl_prot_entry *pe = NULL;
540 p = ubi->prot.pnum.rb_node;
543 pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
545 if (pnum == pe->e->pnum)
548 if (pnum < pe->e->pnum)
554 ubi_assert(pe->e->pnum == pnum);
555 rb_erase(&pe->rb_aec, &ubi->prot.aec);
556 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
561 * sync_erase - synchronously erase a physical eraseblock.
562 * @ubi: UBI device description object
563 * @e: the the physical eraseblock to erase
564 * @torture: if the physical eraseblock has to be tortured
566 * This function returns zero in case of success and a negative error code in
569 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int torture)
572 struct ubi_ec_hdr *ec_hdr;
573 unsigned long long ec = e->ec;
575 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
577 err = paranoid_check_ec(ubi, e->pnum, e->ec);
581 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
585 err = ubi_io_sync_erase(ubi, e->pnum, torture);
590 if (ec > UBI_MAX_ERASECOUNTER) {
592 * Erase counter overflow. Upgrade UBI and use 64-bit
593 * erase counters internally.
595 ubi_err("erase counter overflow at PEB %d, EC %llu",
601 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
603 ec_hdr->ec = cpu_to_be64(ec);
605 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
610 spin_lock(&ubi->wl_lock);
611 if (e->ec > ubi->max_ec)
613 spin_unlock(&ubi->wl_lock);
621 * check_protection_over - check if it is time to stop protecting some
622 * physical eraseblocks.
623 * @ubi: UBI device description object
625 * This function is called after each erase operation, when the absolute erase
626 * counter is incremented, to check if some physical eraseblock have not to be
627 * protected any longer. These physical eraseblocks are moved from the
628 * protection trees to the used tree.
630 static void check_protection_over(struct ubi_device *ubi)
632 struct ubi_wl_prot_entry *pe;
635 * There may be several protected physical eraseblock to remove,
639 spin_lock(&ubi->wl_lock);
640 if (!ubi->prot.aec.rb_node) {
641 spin_unlock(&ubi->wl_lock);
645 pe = rb_entry(rb_first(&ubi->prot.aec),
646 struct ubi_wl_prot_entry, rb_aec);
648 if (pe->abs_ec > ubi->abs_ec) {
649 spin_unlock(&ubi->wl_lock);
653 dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
654 pe->e->pnum, ubi->abs_ec, pe->abs_ec);
655 rb_erase(&pe->rb_aec, &ubi->prot.aec);
656 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
657 wl_tree_add(pe->e, &ubi->used);
658 spin_unlock(&ubi->wl_lock);
666 * schedule_ubi_work - schedule a work.
667 * @ubi: UBI device description object
668 * @wrk: the work to schedule
670 * This function enqueues a work defined by @wrk to the tail of the pending
673 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
675 spin_lock(&ubi->wl_lock);
676 list_add_tail(&wrk->list, &ubi->works);
677 ubi_assert(ubi->works_count >= 0);
678 ubi->works_count += 1;
679 if (ubi->thread_enabled)
680 wake_up_process(ubi->bgt_thread);
681 spin_unlock(&ubi->wl_lock);
684 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
688 * schedule_erase - schedule an erase work.
689 * @ubi: UBI device description object
690 * @e: the WL entry of the physical eraseblock to erase
691 * @torture: if the physical eraseblock has to be tortured
693 * This function returns zero in case of success and a %-ENOMEM in case of
696 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
699 struct ubi_work *wl_wrk;
701 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
702 e->pnum, e->ec, torture);
704 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
708 wl_wrk->func = &erase_worker;
710 wl_wrk->torture = torture;
712 schedule_ubi_work(ubi, wl_wrk);
717 * wear_leveling_worker - wear-leveling worker function.
718 * @ubi: UBI device description object
719 * @wrk: the work object
720 * @cancel: non-zero if the worker has to free memory and exit
722 * This function copies a more worn out physical eraseblock to a less worn out
723 * one. Returns zero in case of success and a negative error code in case of
726 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
730 struct ubi_wl_entry *e1, *e2;
731 struct ubi_vid_hdr *vid_hdr;
738 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
742 spin_lock(&ubi->wl_lock);
745 * Only one WL worker at a time is supported at this implementation, so
746 * make sure a PEB is not being moved already.
748 if (ubi->move_to || !ubi->free.rb_node ||
749 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
751 * Only one WL worker at a time is supported at this
752 * implementation, so if a LEB is already being moved, cancel.
754 * No free physical eraseblocks? Well, we cancel wear-leveling
755 * then. It will be triggered again when a free physical
756 * eraseblock appears.
758 * No used physical eraseblocks? They must be temporarily
759 * protected from being moved. They will be moved to the
760 * @ubi->used tree later and the wear-leveling will be
763 dbg_wl("cancel WL, a list is empty: free %d, used %d",
764 !ubi->free.rb_node, !ubi->used.rb_node);
765 ubi->wl_scheduled = 0;
766 spin_unlock(&ubi->wl_lock);
767 ubi_free_vid_hdr(ubi, vid_hdr);
771 if (!ubi->scrub.rb_node) {
773 * Now pick the least worn-out used physical eraseblock and a
774 * highly worn-out free physical eraseblock. If the erase
775 * counters differ much enough, start wear-leveling.
777 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
778 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
780 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
781 dbg_wl("no WL needed: min used EC %d, max free EC %d",
783 ubi->wl_scheduled = 0;
784 spin_unlock(&ubi->wl_lock);
785 ubi_free_vid_hdr(ubi, vid_hdr);
788 paranoid_check_in_wl_tree(e1, &ubi->used);
789 rb_erase(&e1->rb, &ubi->used);
790 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
791 e1->pnum, e1->ec, e2->pnum, e2->ec);
793 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
794 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
795 paranoid_check_in_wl_tree(e1, &ubi->scrub);
796 rb_erase(&e1->rb, &ubi->scrub);
797 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
800 paranoid_check_in_wl_tree(e2, &ubi->free);
801 rb_erase(&e2->rb, &ubi->free);
802 ubi_assert(!ubi->move_from && !ubi->move_to);
803 ubi_assert(!ubi->move_to_put && !ubi->move_from_put);
806 spin_unlock(&ubi->wl_lock);
809 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
810 * We so far do not know which logical eraseblock our physical
811 * eraseblock (@e1) belongs to. We have to read the volume identifier
815 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
816 if (err && err != UBI_IO_BITFLIPS) {
817 if (err == UBI_IO_PEB_FREE) {
819 * We are trying to move PEB without a VID header. UBI
820 * always write VID headers shortly after the PEB was
821 * given, so we have a situation when it did not have
822 * chance to write it down because it was preempted.
823 * Just re-schedule the work, so that next time it will
824 * likely have the VID header in place.
826 dbg_wl("PEB %d has no VID header", e1->pnum);
829 ubi_err("error %d while reading VID header from PEB %d",
837 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
839 if (err == UBI_IO_BITFLIPS)
844 ubi_free_vid_hdr(ubi, vid_hdr);
845 spin_lock(&ubi->wl_lock);
846 if (!ubi->move_to_put)
847 wl_tree_add(e2, &ubi->used);
850 ubi->move_from = ubi->move_to = NULL;
851 ubi->move_from_put = ubi->move_to_put = 0;
852 ubi->wl_scheduled = 0;
853 spin_unlock(&ubi->wl_lock);
857 * Well, the target PEB was put meanwhile, schedule it for
860 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
861 err = schedule_erase(ubi, e2, 0);
863 kmem_cache_free(ubi_wl_entry_slab, e2);
868 err = schedule_erase(ubi, e1, 0);
870 kmem_cache_free(ubi_wl_entry_slab, e1);
878 * Some error occurred. @e1 was not changed, so return it back. @e2
879 * might be changed, schedule it for erasure.
883 dbg_wl("error %d occurred, cancel operation", err);
884 ubi_assert(err <= 0);
886 ubi_free_vid_hdr(ubi, vid_hdr);
887 spin_lock(&ubi->wl_lock);
888 ubi->wl_scheduled = 0;
889 if (ubi->move_from_put)
892 wl_tree_add(e1, &ubi->used);
893 ubi->move_from = ubi->move_to = NULL;
894 ubi->move_from_put = ubi->move_to_put = 0;
895 spin_unlock(&ubi->wl_lock);
899 * Well, the target PEB was put meanwhile, schedule it for
902 dbg_wl("PEB %d was put meanwhile, erase", e1->pnum);
903 err = schedule_erase(ubi, e1, 0);
905 kmem_cache_free(ubi_wl_entry_slab, e1);
910 err = schedule_erase(ubi, e2, 0);
912 kmem_cache_free(ubi_wl_entry_slab, e2);
921 * ensure_wear_leveling - schedule wear-leveling if it is needed.
922 * @ubi: UBI device description object
924 * This function checks if it is time to start wear-leveling and schedules it
925 * if yes. This function returns zero in case of success and a negative error
926 * code in case of failure.
928 static int ensure_wear_leveling(struct ubi_device *ubi)
931 struct ubi_wl_entry *e1;
932 struct ubi_wl_entry *e2;
933 struct ubi_work *wrk;
935 spin_lock(&ubi->wl_lock);
936 if (ubi->wl_scheduled)
937 /* Wear-leveling is already in the work queue */
941 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
942 * the WL worker has to be scheduled anyway.
944 if (!ubi->scrub.rb_node) {
945 if (!ubi->used.rb_node || !ubi->free.rb_node)
946 /* No physical eraseblocks - no deal */
950 * We schedule wear-leveling only if the difference between the
951 * lowest erase counter of used physical eraseblocks and a high
952 * erase counter of free physical eraseblocks is greater then
955 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
956 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
958 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
960 dbg_wl("schedule wear-leveling");
962 dbg_wl("schedule scrubbing");
964 ubi->wl_scheduled = 1;
965 spin_unlock(&ubi->wl_lock);
967 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
973 wrk->func = &wear_leveling_worker;
974 schedule_ubi_work(ubi, wrk);
978 spin_lock(&ubi->wl_lock);
979 ubi->wl_scheduled = 0;
981 spin_unlock(&ubi->wl_lock);
986 * erase_worker - physical eraseblock erase worker function.
987 * @ubi: UBI device description object
988 * @wl_wrk: the work object
989 * @cancel: non-zero if the worker has to free memory and exit
991 * This function erases a physical eraseblock and perform torture testing if
992 * needed. It also takes care about marking the physical eraseblock bad if
993 * needed. Returns zero in case of success and a negative error code in case of
996 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
999 struct ubi_wl_entry *e = wl_wrk->e;
1000 int pnum = e->pnum, err, need;
1003 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1005 kmem_cache_free(ubi_wl_entry_slab, e);
1009 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
1011 err = sync_erase(ubi, e, wl_wrk->torture);
1013 /* Fine, we've erased it successfully */
1016 spin_lock(&ubi->wl_lock);
1018 wl_tree_add(e, &ubi->free);
1019 spin_unlock(&ubi->wl_lock);
1022 * One more erase operation has happened, take care about protected
1023 * physical eraseblocks.
1025 check_protection_over(ubi);
1027 /* And take care about wear-leveling */
1028 err = ensure_wear_leveling(ubi);
1032 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1034 kmem_cache_free(ubi_wl_entry_slab, e);
1036 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1040 /* Re-schedule the LEB for erasure */
1041 err1 = schedule_erase(ubi, e, 0);
1047 } else if (err != -EIO) {
1049 * If this is not %-EIO, we have no idea what to do. Scheduling
1050 * this physical eraseblock for erasure again would cause
1051 * errors again and again. Well, lets switch to RO mode.
1056 /* It is %-EIO, the PEB went bad */
1058 if (!ubi->bad_allowed) {
1059 ubi_err("bad physical eraseblock %d detected", pnum);
1063 spin_lock(&ubi->volumes_lock);
1064 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1066 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1067 ubi->avail_pebs -= need;
1068 ubi->rsvd_pebs += need;
1069 ubi->beb_rsvd_pebs += need;
1071 ubi_msg("reserve more %d PEBs", need);
1074 if (ubi->beb_rsvd_pebs == 0) {
1075 spin_unlock(&ubi->volumes_lock);
1076 ubi_err("no reserved physical eraseblocks");
1080 spin_unlock(&ubi->volumes_lock);
1081 ubi_msg("mark PEB %d as bad", pnum);
1083 err = ubi_io_mark_bad(ubi, pnum);
1087 spin_lock(&ubi->volumes_lock);
1088 ubi->beb_rsvd_pebs -= 1;
1089 ubi->bad_peb_count += 1;
1090 ubi->good_peb_count -= 1;
1091 ubi_calculate_reserved(ubi);
1092 if (ubi->beb_rsvd_pebs == 0)
1093 ubi_warn("last PEB from the reserved pool was used");
1094 spin_unlock(&ubi->volumes_lock);
1104 * ubi_wl_put_peb - return a physical eraseblock to the wear-leveling
1106 * @ubi: UBI device description object
1107 * @pnum: physical eraseblock to return
1108 * @torture: if this physical eraseblock has to be tortured
1110 * This function is called to return physical eraseblock @pnum to the pool of
1111 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1112 * occurred to this @pnum and it has to be tested. This function returns zero
1113 * in case of success and a negative error code in case of failure.
1115 int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1118 struct ubi_wl_entry *e;
1120 dbg_wl("PEB %d", pnum);
1121 ubi_assert(pnum >= 0);
1122 ubi_assert(pnum < ubi->peb_count);
1124 spin_lock(&ubi->wl_lock);
1126 e = ubi->lookuptbl[pnum];
1127 if (e == ubi->move_from) {
1129 * User is putting the physical eraseblock which was selected to
1130 * be moved. It will be scheduled for erasure in the
1131 * wear-leveling worker.
1133 dbg_wl("PEB %d is being moved", pnum);
1134 ubi_assert(!ubi->move_from_put);
1135 ubi->move_from_put = 1;
1136 spin_unlock(&ubi->wl_lock);
1138 } else if (e == ubi->move_to) {
1140 * User is putting the physical eraseblock which was selected
1141 * as the target the data is moved to. It may happen if the EBA
1142 * unit already re-mapped the LEB but the WL unit did has not
1143 * put the PEB to the "used" tree.
1145 dbg_wl("PEB %d is the target of data moving", pnum);
1146 ubi_assert(!ubi->move_to_put);
1147 ubi->move_to_put = 1;
1148 spin_unlock(&ubi->wl_lock);
1151 if (in_wl_tree(e, &ubi->used)) {
1152 paranoid_check_in_wl_tree(e, &ubi->used);
1153 rb_erase(&e->rb, &ubi->used);
1154 } else if (in_wl_tree(e, &ubi->scrub)) {
1155 paranoid_check_in_wl_tree(e, &ubi->scrub);
1156 rb_erase(&e->rb, &ubi->scrub);
1158 prot_tree_del(ubi, e->pnum);
1160 spin_unlock(&ubi->wl_lock);
1162 err = schedule_erase(ubi, e, torture);
1164 spin_lock(&ubi->wl_lock);
1165 wl_tree_add(e, &ubi->used);
1166 spin_unlock(&ubi->wl_lock);
1173 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1174 * @ubi: UBI device description object
1175 * @pnum: the physical eraseblock to schedule
1177 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1178 * needs scrubbing. This function schedules a physical eraseblock for
1179 * scrubbing which is done in background. This function returns zero in case of
1180 * success and a negative error code in case of failure.
1182 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1184 struct ubi_wl_entry *e;
1186 ubi_msg("schedule PEB %d for scrubbing", pnum);
1189 spin_lock(&ubi->wl_lock);
1190 e = ubi->lookuptbl[pnum];
1191 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1192 spin_unlock(&ubi->wl_lock);
1196 if (e == ubi->move_to) {
1198 * This physical eraseblock was used to move data to. The data
1199 * was moved but the PEB was not yet inserted to the proper
1200 * tree. We should just wait a little and let the WL worker
1203 spin_unlock(&ubi->wl_lock);
1204 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1209 if (in_wl_tree(e, &ubi->used)) {
1210 paranoid_check_in_wl_tree(e, &ubi->used);
1211 rb_erase(&e->rb, &ubi->used);
1213 prot_tree_del(ubi, pnum);
1215 wl_tree_add(e, &ubi->scrub);
1216 spin_unlock(&ubi->wl_lock);
1219 * Technically scrubbing is the same as wear-leveling, so it is done
1222 return ensure_wear_leveling(ubi);
1226 * ubi_wl_flush - flush all pending works.
1227 * @ubi: UBI device description object
1229 * This function returns zero in case of success and a negative error code in
1232 int ubi_wl_flush(struct ubi_device *ubi)
1234 int err, pending_count;
1236 pending_count = ubi->works_count;
1238 dbg_wl("flush (%d pending works)", pending_count);
1241 * Erase while the pending works queue is not empty, but not more then
1242 * the number of currently pending works.
1244 while (pending_count-- > 0) {
1254 * tree_destroy - destroy an RB-tree.
1255 * @root: the root of the tree to destroy
1257 static void tree_destroy(struct rb_root *root)
1260 struct ubi_wl_entry *e;
1266 else if (rb->rb_right)
1269 e = rb_entry(rb, struct ubi_wl_entry, rb);
1273 if (rb->rb_left == &e->rb)
1276 rb->rb_right = NULL;
1279 kmem_cache_free(ubi_wl_entry_slab, e);
1285 * ubi_thread - UBI background thread.
1286 * @u: the UBI device description object pointer
1288 static int ubi_thread(void *u)
1291 struct ubi_device *ubi = u;
1293 ubi_msg("background thread \"%s\" started, PID %d",
1294 ubi->bgt_name, task_pid_nr(current));
1300 if (kthread_should_stop())
1303 if (try_to_freeze())
1306 spin_lock(&ubi->wl_lock);
1307 if (list_empty(&ubi->works) || ubi->ro_mode ||
1308 !ubi->thread_enabled) {
1309 set_current_state(TASK_INTERRUPTIBLE);
1310 spin_unlock(&ubi->wl_lock);
1314 spin_unlock(&ubi->wl_lock);
1318 ubi_err("%s: work failed with error code %d",
1319 ubi->bgt_name, err);
1320 if (failures++ > WL_MAX_FAILURES) {
1322 * Too many failures, disable the thread and
1323 * switch to read-only mode.
1325 ubi_msg("%s: %d consecutive failures",
1326 ubi->bgt_name, WL_MAX_FAILURES);
1337 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1342 * cancel_pending - cancel all pending works.
1343 * @ubi: UBI device description object
1345 static void cancel_pending(struct ubi_device *ubi)
1347 while (!list_empty(&ubi->works)) {
1348 struct ubi_work *wrk;
1350 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1351 list_del(&wrk->list);
1352 wrk->func(ubi, wrk, 1);
1353 ubi->works_count -= 1;
1354 ubi_assert(ubi->works_count >= 0);
1359 * ubi_wl_init_scan - initialize the wear-leveling unit using scanning
1361 * @ubi: UBI device description object
1362 * @si: scanning information
1364 * This function returns zero in case of success, and a negative error code in
1367 int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1370 struct rb_node *rb1, *rb2;
1371 struct ubi_scan_volume *sv;
1372 struct ubi_scan_leb *seb, *tmp;
1373 struct ubi_wl_entry *e;
1376 ubi->used = ubi->free = ubi->scrub = RB_ROOT;
1377 ubi->prot.pnum = ubi->prot.aec = RB_ROOT;
1378 spin_lock_init(&ubi->wl_lock);
1379 ubi->max_ec = si->max_ec;
1380 INIT_LIST_HEAD(&ubi->works);
1382 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1384 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
1385 if (IS_ERR(ubi->bgt_thread)) {
1386 err = PTR_ERR(ubi->bgt_thread);
1387 ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
1393 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1394 if (!ubi->lookuptbl)
1397 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1400 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1404 e->pnum = seb->pnum;
1406 ubi->lookuptbl[e->pnum] = e;
1407 if (schedule_erase(ubi, e, 0)) {
1408 kmem_cache_free(ubi_wl_entry_slab, e);
1413 list_for_each_entry(seb, &si->free, u.list) {
1416 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1420 e->pnum = seb->pnum;
1422 ubi_assert(e->ec >= 0);
1423 wl_tree_add(e, &ubi->free);
1424 ubi->lookuptbl[e->pnum] = e;
1427 list_for_each_entry(seb, &si->corr, u.list) {
1430 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1434 e->pnum = seb->pnum;
1436 ubi->lookuptbl[e->pnum] = e;
1437 if (schedule_erase(ubi, e, 0)) {
1438 kmem_cache_free(ubi_wl_entry_slab, e);
1443 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1444 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1447 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1451 e->pnum = seb->pnum;
1453 ubi->lookuptbl[e->pnum] = e;
1455 dbg_wl("add PEB %d EC %d to the used tree",
1457 wl_tree_add(e, &ubi->used);
1459 dbg_wl("add PEB %d EC %d to the scrub tree",
1461 wl_tree_add(e, &ubi->scrub);
1466 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
1467 ubi_err("no enough physical eraseblocks (%d, need %d)",
1468 ubi->avail_pebs, WL_RESERVED_PEBS);
1471 ubi->avail_pebs -= WL_RESERVED_PEBS;
1472 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1474 /* Schedule wear-leveling if needed */
1475 err = ensure_wear_leveling(ubi);
1482 cancel_pending(ubi);
1483 tree_destroy(&ubi->used);
1484 tree_destroy(&ubi->free);
1485 tree_destroy(&ubi->scrub);
1486 kfree(ubi->lookuptbl);
1491 * protection_trees_destroy - destroy the protection RB-trees.
1492 * @ubi: UBI device description object
1494 static void protection_trees_destroy(struct ubi_device *ubi)
1497 struct ubi_wl_prot_entry *pe;
1499 rb = ubi->prot.aec.rb_node;
1503 else if (rb->rb_right)
1506 pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
1510 if (rb->rb_left == &pe->rb_aec)
1513 rb->rb_right = NULL;
1516 kmem_cache_free(ubi_wl_entry_slab, pe->e);
1523 * ubi_wl_close - close the wear-leveling unit.
1524 * @ubi: UBI device description object
1526 void ubi_wl_close(struct ubi_device *ubi)
1528 dbg_wl("disable \"%s\"", ubi->bgt_name);
1529 if (ubi->bgt_thread)
1530 kthread_stop(ubi->bgt_thread);
1532 dbg_wl("close the UBI wear-leveling unit");
1534 cancel_pending(ubi);
1535 protection_trees_destroy(ubi);
1536 tree_destroy(&ubi->used);
1537 tree_destroy(&ubi->free);
1538 tree_destroy(&ubi->scrub);
1539 kfree(ubi->lookuptbl);
1542 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1545 * paranoid_check_ec - make sure that the erase counter of a physical eraseblock
1547 * @ubi: UBI device description object
1548 * @pnum: the physical eraseblock number to check
1549 * @ec: the erase counter to check
1551 * This function returns zero if the erase counter of physical eraseblock @pnum
1552 * is equivalent to @ec, %1 if not, and a negative error code if an error
1555 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1559 struct ubi_ec_hdr *ec_hdr;
1561 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1565 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1566 if (err && err != UBI_IO_BITFLIPS) {
1567 /* The header does not have to exist */
1572 read_ec = be64_to_cpu(ec_hdr->ec);
1573 if (ec != read_ec) {
1574 ubi_err("paranoid check failed for PEB %d", pnum);
1575 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1576 ubi_dbg_dump_stack();
1587 * paranoid_check_in_wl_tree - make sure that a wear-leveling entry is present
1589 * @e: the wear-leveling entry to check
1590 * @root: the root of the tree
1592 * This function returns zero if @e is in the @root RB-tree and %1 if it
1595 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1596 struct rb_root *root)
1598 if (in_wl_tree(e, root))
1601 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1602 e->pnum, e->ec, root);
1603 ubi_dbg_dump_stack();
1607 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */