cciss: fix problem that deleting multiple logical drives could cause a panic
[linux-2.6/libata-dev.git] / drivers / mtd / ubi / wl.c
blobdcb6dac1dc5446b4966d85ef8e6d8e1db5ed9dcb
1 /*
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 sub-system.
24 * This sub-system is responsible for wear-leveling. It works in terms of
25 * physical* eraseblocks and erase counters and knows nothing about logical
26 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
27 * eraseblocks are of two types - used and free. Used physical eraseblocks are
28 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
29 * eraseblocks are 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 sub-system 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 sub-system.
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 sub-system may pick a free physical eraseblock with low erase
47 * counter, and so forth.
49 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
50 * bad.
52 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
53 * in a physical eraseblock, it has to be moved. Technically this is the same
54 * as moving it for wear-leveling reasons.
56 * As it was said, for the UBI sub-system all physical eraseblocks are either
57 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
58 * used eraseblocks are kept in a set of different RB-trees: @wl->used,
59 * @wl->prot.pnum, @wl->prot.aec, and @wl->scrub.
61 * Note, in this implementation, we keep a small in-RAM object for each physical
62 * eraseblock. This is surely not a scalable solution. But it appears to be good
63 * enough for moderately large flashes and it is simple. In future, one may
64 * re-work this sub-system and make it more scalable.
66 * At the moment this sub-system does not utilize the sequence number, which
67 * was introduced relatively recently. But it would be wise to do this because
68 * the sequence number of a logical eraseblock characterizes how old is it. For
69 * example, when we move a PEB with low erase counter, and we need to pick the
70 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
71 * pick target PEB with an average EC if our PEB is not very "old". This is a
72 * room for future re-works of the WL sub-system.
74 * Note: the stuff with protection trees looks too complex and is difficult to
75 * understand. Should be fixed.
78 #include <linux/slab.h>
79 #include <linux/crc32.h>
80 #include <linux/freezer.h>
81 #include <linux/kthread.h>
82 #include "ubi.h"
84 /* Number of physical eraseblocks reserved for wear-leveling purposes */
85 #define WL_RESERVED_PEBS 1
88 * How many erase cycles are short term, unknown, and long term physical
89 * eraseblocks protected.
91 #define ST_PROTECTION 16
92 #define U_PROTECTION 10
93 #define LT_PROTECTION 4
96 * Maximum difference between two erase counters. If this threshold is
97 * exceeded, the WL sub-system starts moving data from used physical
98 * eraseblocks with low erase counter to free physical eraseblocks with high
99 * erase counter.
101 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
104 * When a physical eraseblock is moved, the WL sub-system has to pick the target
105 * physical eraseblock to move to. The simplest way would be just to pick the
106 * one with the highest erase counter. But in certain workloads this could lead
107 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
108 * situation when the picked physical eraseblock is constantly erased after the
109 * data is written to it. So, we have a constant which limits the highest erase
110 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
111 * does not pick eraseblocks with erase counter greater then the lowest erase
112 * counter plus %WL_FREE_MAX_DIFF.
114 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
117 * Maximum number of consecutive background thread failures which is enough to
118 * switch to read-only mode.
120 #define WL_MAX_FAILURES 32
123 * struct ubi_wl_prot_entry - PEB protection entry.
124 * @rb_pnum: link in the @wl->prot.pnum RB-tree
125 * @rb_aec: link in the @wl->prot.aec RB-tree
126 * @abs_ec: the absolute erase counter value when the protection ends
127 * @e: the wear-leveling entry of the physical eraseblock under protection
129 * When the WL sub-system returns a physical eraseblock, the physical
130 * eraseblock is protected from being moved for some "time". For this reason,
131 * the physical eraseblock is not directly moved from the @wl->free tree to the
132 * @wl->used tree. There is one more tree in between where this physical
133 * eraseblock is temporarily stored (@wl->prot).
135 * All this protection stuff is needed because:
136 * o we don't want to move physical eraseblocks just after we have given them
137 * to the user; instead, we first want to let users fill them up with data;
139 * o there is a chance that the user will put the physical eraseblock very
140 * soon, so it makes sense not to move it for some time, but wait; this is
141 * especially important in case of "short term" physical eraseblocks.
143 * Physical eraseblocks stay protected only for limited time. But the "time" is
144 * measured in erase cycles in this case. This is implemented with help of the
145 * absolute erase counter (@wl->abs_ec). When it reaches certain value, the
146 * physical eraseblocks are moved from the protection trees (@wl->prot.*) to
147 * the @wl->used tree.
149 * Protected physical eraseblocks are searched by physical eraseblock number
150 * (when they are put) and by the absolute erase counter (to check if it is
151 * time to move them to the @wl->used tree). So there are actually 2 RB-trees
152 * storing the protected physical eraseblocks: @wl->prot.pnum and
153 * @wl->prot.aec. They are referred to as the "protection" trees. The
154 * first one is indexed by the physical eraseblock number. The second one is
155 * indexed by the absolute erase counter. Both trees store
156 * &struct ubi_wl_prot_entry objects.
158 * Each physical eraseblock has 2 main states: free and used. The former state
159 * corresponds to the @wl->free tree. The latter state is split up on several
160 * sub-states:
161 * o the WL movement is allowed (@wl->used tree);
162 * o the WL movement is temporarily prohibited (@wl->prot.pnum and
163 * @wl->prot.aec trees);
164 * o scrubbing is needed (@wl->scrub tree).
166 * Depending on the sub-state, wear-leveling entries of the used physical
167 * eraseblocks may be kept in one of those trees.
169 struct ubi_wl_prot_entry {
170 struct rb_node rb_pnum;
171 struct rb_node rb_aec;
172 unsigned long long abs_ec;
173 struct ubi_wl_entry *e;
177 * struct ubi_work - UBI work description data structure.
178 * @list: a link in the list of pending works
179 * @func: worker function
180 * @priv: private data of the worker function
181 * @e: physical eraseblock to erase
182 * @torture: if the physical eraseblock has to be tortured
184 * The @func pointer points to the worker function. If the @cancel argument is
185 * not zero, the worker has to free the resources and exit immediately. The
186 * worker has to return zero in case of success and a negative error code in
187 * case of failure.
189 struct ubi_work {
190 struct list_head list;
191 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
192 /* The below fields are only relevant to erasure works */
193 struct ubi_wl_entry *e;
194 int torture;
197 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
198 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
199 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
200 struct rb_root *root);
201 #else
202 #define paranoid_check_ec(ubi, pnum, ec) 0
203 #define paranoid_check_in_wl_tree(e, root)
204 #endif
207 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
208 * @e: the wear-leveling entry to add
209 * @root: the root of the tree
211 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
212 * the @ubi->used and @ubi->free RB-trees.
214 static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
216 struct rb_node **p, *parent = NULL;
218 p = &root->rb_node;
219 while (*p) {
220 struct ubi_wl_entry *e1;
222 parent = *p;
223 e1 = rb_entry(parent, struct ubi_wl_entry, rb);
225 if (e->ec < e1->ec)
226 p = &(*p)->rb_left;
227 else if (e->ec > e1->ec)
228 p = &(*p)->rb_right;
229 else {
230 ubi_assert(e->pnum != e1->pnum);
231 if (e->pnum < e1->pnum)
232 p = &(*p)->rb_left;
233 else
234 p = &(*p)->rb_right;
238 rb_link_node(&e->rb, parent, p);
239 rb_insert_color(&e->rb, root);
243 * do_work - do one pending work.
244 * @ubi: UBI device description object
246 * This function returns zero in case of success and a negative error code in
247 * case of failure.
249 static int do_work(struct ubi_device *ubi)
251 int err;
252 struct ubi_work *wrk;
254 cond_resched();
257 * @ubi->work_sem is used to synchronize with the workers. Workers take
258 * it in read mode, so many of them may be doing works at a time. But
259 * the queue flush code has to be sure the whole queue of works is
260 * done, and it takes the mutex in write mode.
262 down_read(&ubi->work_sem);
263 spin_lock(&ubi->wl_lock);
264 if (list_empty(&ubi->works)) {
265 spin_unlock(&ubi->wl_lock);
266 up_read(&ubi->work_sem);
267 return 0;
270 wrk = list_entry(ubi->works.next, struct ubi_work, list);
271 list_del(&wrk->list);
272 ubi->works_count -= 1;
273 ubi_assert(ubi->works_count >= 0);
274 spin_unlock(&ubi->wl_lock);
277 * Call the worker function. Do not touch the work structure
278 * after this call as it will have been freed or reused by that
279 * time by the worker function.
281 err = wrk->func(ubi, wrk, 0);
282 if (err)
283 ubi_err("work failed with error code %d", err);
284 up_read(&ubi->work_sem);
286 return err;
290 * produce_free_peb - produce a free physical eraseblock.
291 * @ubi: UBI device description object
293 * This function tries to make a free PEB by means of synchronous execution of
294 * pending works. This may be needed if, for example the background thread is
295 * disabled. Returns zero in case of success and a negative error code in case
296 * of failure.
298 static int produce_free_peb(struct ubi_device *ubi)
300 int err;
302 spin_lock(&ubi->wl_lock);
303 while (!ubi->free.rb_node) {
304 spin_unlock(&ubi->wl_lock);
306 dbg_wl("do one work synchronously");
307 err = do_work(ubi);
308 if (err)
309 return err;
311 spin_lock(&ubi->wl_lock);
313 spin_unlock(&ubi->wl_lock);
315 return 0;
319 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
320 * @e: the wear-leveling entry to check
321 * @root: the root of the tree
323 * This function returns non-zero if @e is in the @root RB-tree and zero if it
324 * is not.
326 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
328 struct rb_node *p;
330 p = root->rb_node;
331 while (p) {
332 struct ubi_wl_entry *e1;
334 e1 = rb_entry(p, struct ubi_wl_entry, rb);
336 if (e->pnum == e1->pnum) {
337 ubi_assert(e == e1);
338 return 1;
341 if (e->ec < e1->ec)
342 p = p->rb_left;
343 else if (e->ec > e1->ec)
344 p = p->rb_right;
345 else {
346 ubi_assert(e->pnum != e1->pnum);
347 if (e->pnum < e1->pnum)
348 p = p->rb_left;
349 else
350 p = p->rb_right;
354 return 0;
358 * prot_tree_add - add physical eraseblock to protection trees.
359 * @ubi: UBI device description object
360 * @e: the physical eraseblock to add
361 * @pe: protection entry object to use
362 * @abs_ec: absolute erase counter value when this physical eraseblock has
363 * to be removed from the protection trees.
365 * @wl->lock has to be locked.
367 static void prot_tree_add(struct ubi_device *ubi, struct ubi_wl_entry *e,
368 struct ubi_wl_prot_entry *pe, int abs_ec)
370 struct rb_node **p, *parent = NULL;
371 struct ubi_wl_prot_entry *pe1;
373 pe->e = e;
374 pe->abs_ec = ubi->abs_ec + abs_ec;
376 p = &ubi->prot.pnum.rb_node;
377 while (*p) {
378 parent = *p;
379 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_pnum);
381 if (e->pnum < pe1->e->pnum)
382 p = &(*p)->rb_left;
383 else
384 p = &(*p)->rb_right;
386 rb_link_node(&pe->rb_pnum, parent, p);
387 rb_insert_color(&pe->rb_pnum, &ubi->prot.pnum);
389 p = &ubi->prot.aec.rb_node;
390 parent = NULL;
391 while (*p) {
392 parent = *p;
393 pe1 = rb_entry(parent, struct ubi_wl_prot_entry, rb_aec);
395 if (pe->abs_ec < pe1->abs_ec)
396 p = &(*p)->rb_left;
397 else
398 p = &(*p)->rb_right;
400 rb_link_node(&pe->rb_aec, parent, p);
401 rb_insert_color(&pe->rb_aec, &ubi->prot.aec);
405 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
406 * @root: the RB-tree where to look for
407 * @max: highest possible erase counter
409 * This function looks for a wear leveling entry with erase counter closest to
410 * @max and less then @max.
412 static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
414 struct rb_node *p;
415 struct ubi_wl_entry *e;
417 e = rb_entry(rb_first(root), struct ubi_wl_entry, rb);
418 max += e->ec;
420 p = root->rb_node;
421 while (p) {
422 struct ubi_wl_entry *e1;
424 e1 = rb_entry(p, struct ubi_wl_entry, rb);
425 if (e1->ec >= max)
426 p = p->rb_left;
427 else {
428 p = p->rb_right;
429 e = e1;
433 return e;
437 * ubi_wl_get_peb - get a physical eraseblock.
438 * @ubi: UBI device description object
439 * @dtype: type of data which will be stored in this physical eraseblock
441 * This function returns a physical eraseblock in case of success and a
442 * negative error code in case of failure. Might sleep.
444 int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
446 int err, protect, medium_ec;
447 struct ubi_wl_entry *e, *first, *last;
448 struct ubi_wl_prot_entry *pe;
450 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
451 dtype == UBI_UNKNOWN);
453 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
454 if (!pe)
455 return -ENOMEM;
457 retry:
458 spin_lock(&ubi->wl_lock);
459 if (!ubi->free.rb_node) {
460 if (ubi->works_count == 0) {
461 ubi_assert(list_empty(&ubi->works));
462 ubi_err("no free eraseblocks");
463 spin_unlock(&ubi->wl_lock);
464 kfree(pe);
465 return -ENOSPC;
467 spin_unlock(&ubi->wl_lock);
469 err = produce_free_peb(ubi);
470 if (err < 0) {
471 kfree(pe);
472 return err;
474 goto retry;
477 switch (dtype) {
478 case UBI_LONGTERM:
480 * For long term data we pick a physical eraseblock with high
481 * erase counter. But the highest erase counter we can pick is
482 * bounded by the the lowest erase counter plus
483 * %WL_FREE_MAX_DIFF.
485 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
486 protect = LT_PROTECTION;
487 break;
488 case UBI_UNKNOWN:
490 * For unknown data we pick a physical eraseblock with medium
491 * erase counter. But we by no means can pick a physical
492 * eraseblock with erase counter greater or equivalent than the
493 * lowest erase counter plus %WL_FREE_MAX_DIFF.
495 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, rb);
496 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, rb);
498 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
499 e = rb_entry(ubi->free.rb_node,
500 struct ubi_wl_entry, rb);
501 else {
502 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
503 e = find_wl_entry(&ubi->free, medium_ec);
505 protect = U_PROTECTION;
506 break;
507 case UBI_SHORTTERM:
509 * For short term data we pick a physical eraseblock with the
510 * lowest erase counter as we expect it will be erased soon.
512 e = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, rb);
513 protect = ST_PROTECTION;
514 break;
515 default:
516 protect = 0;
517 e = NULL;
518 BUG();
522 * Move the physical eraseblock to the protection trees where it will
523 * be protected from being moved for some time.
525 paranoid_check_in_wl_tree(e, &ubi->free);
526 rb_erase(&e->rb, &ubi->free);
527 prot_tree_add(ubi, e, pe, protect);
529 dbg_wl("PEB %d EC %d, protection %d", e->pnum, e->ec, protect);
530 spin_unlock(&ubi->wl_lock);
532 return e->pnum;
536 * prot_tree_del - remove a physical eraseblock from the protection trees
537 * @ubi: UBI device description object
538 * @pnum: the physical eraseblock to remove
540 * This function returns PEB @pnum from the protection trees and returns zero
541 * in case of success and %-ENODEV if the PEB was not found in the protection
542 * trees.
544 static int prot_tree_del(struct ubi_device *ubi, int pnum)
546 struct rb_node *p;
547 struct ubi_wl_prot_entry *pe = NULL;
549 p = ubi->prot.pnum.rb_node;
550 while (p) {
552 pe = rb_entry(p, struct ubi_wl_prot_entry, rb_pnum);
554 if (pnum == pe->e->pnum)
555 goto found;
557 if (pnum < pe->e->pnum)
558 p = p->rb_left;
559 else
560 p = p->rb_right;
563 return -ENODEV;
565 found:
566 ubi_assert(pe->e->pnum == pnum);
567 rb_erase(&pe->rb_aec, &ubi->prot.aec);
568 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
569 kfree(pe);
570 return 0;
574 * sync_erase - synchronously erase a physical eraseblock.
575 * @ubi: UBI device description object
576 * @e: the the physical eraseblock to erase
577 * @torture: if the physical eraseblock has to be tortured
579 * This function returns zero in case of success and a negative error code in
580 * case of failure.
582 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
583 int torture)
585 int err;
586 struct ubi_ec_hdr *ec_hdr;
587 unsigned long long ec = e->ec;
589 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
591 err = paranoid_check_ec(ubi, e->pnum, e->ec);
592 if (err > 0)
593 return -EINVAL;
595 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
596 if (!ec_hdr)
597 return -ENOMEM;
599 err = ubi_io_sync_erase(ubi, e->pnum, torture);
600 if (err < 0)
601 goto out_free;
603 ec += err;
604 if (ec > UBI_MAX_ERASECOUNTER) {
606 * Erase counter overflow. Upgrade UBI and use 64-bit
607 * erase counters internally.
609 ubi_err("erase counter overflow at PEB %d, EC %llu",
610 e->pnum, ec);
611 err = -EINVAL;
612 goto out_free;
615 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
617 ec_hdr->ec = cpu_to_be64(ec);
619 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
620 if (err)
621 goto out_free;
623 e->ec = ec;
624 spin_lock(&ubi->wl_lock);
625 if (e->ec > ubi->max_ec)
626 ubi->max_ec = e->ec;
627 spin_unlock(&ubi->wl_lock);
629 out_free:
630 kfree(ec_hdr);
631 return err;
635 * check_protection_over - check if it is time to stop protecting some PEBs.
636 * @ubi: UBI device description object
638 * This function is called after each erase operation, when the absolute erase
639 * counter is incremented, to check if some physical eraseblock have not to be
640 * protected any longer. These physical eraseblocks are moved from the
641 * protection trees to the used tree.
643 static void check_protection_over(struct ubi_device *ubi)
645 struct ubi_wl_prot_entry *pe;
648 * There may be several protected physical eraseblock to remove,
649 * process them all.
651 while (1) {
652 spin_lock(&ubi->wl_lock);
653 if (!ubi->prot.aec.rb_node) {
654 spin_unlock(&ubi->wl_lock);
655 break;
658 pe = rb_entry(rb_first(&ubi->prot.aec),
659 struct ubi_wl_prot_entry, rb_aec);
661 if (pe->abs_ec > ubi->abs_ec) {
662 spin_unlock(&ubi->wl_lock);
663 break;
666 dbg_wl("PEB %d protection over, abs_ec %llu, PEB abs_ec %llu",
667 pe->e->pnum, ubi->abs_ec, pe->abs_ec);
668 rb_erase(&pe->rb_aec, &ubi->prot.aec);
669 rb_erase(&pe->rb_pnum, &ubi->prot.pnum);
670 wl_tree_add(pe->e, &ubi->used);
671 spin_unlock(&ubi->wl_lock);
673 kfree(pe);
674 cond_resched();
679 * schedule_ubi_work - schedule a work.
680 * @ubi: UBI device description object
681 * @wrk: the work to schedule
683 * This function enqueues a work defined by @wrk to the tail of the pending
684 * works list.
686 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
688 spin_lock(&ubi->wl_lock);
689 list_add_tail(&wrk->list, &ubi->works);
690 ubi_assert(ubi->works_count >= 0);
691 ubi->works_count += 1;
692 if (ubi->thread_enabled)
693 wake_up_process(ubi->bgt_thread);
694 spin_unlock(&ubi->wl_lock);
697 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
698 int cancel);
701 * schedule_erase - schedule an erase work.
702 * @ubi: UBI device description object
703 * @e: the WL entry of the physical eraseblock to erase
704 * @torture: if the physical eraseblock has to be tortured
706 * This function returns zero in case of success and a %-ENOMEM in case of
707 * failure.
709 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
710 int torture)
712 struct ubi_work *wl_wrk;
714 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
715 e->pnum, e->ec, torture);
717 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
718 if (!wl_wrk)
719 return -ENOMEM;
721 wl_wrk->func = &erase_worker;
722 wl_wrk->e = e;
723 wl_wrk->torture = torture;
725 schedule_ubi_work(ubi, wl_wrk);
726 return 0;
730 * wear_leveling_worker - wear-leveling worker function.
731 * @ubi: UBI device description object
732 * @wrk: the work object
733 * @cancel: non-zero if the worker has to free memory and exit
735 * This function copies a more worn out physical eraseblock to a less worn out
736 * one. Returns zero in case of success and a negative error code in case of
737 * failure.
739 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
740 int cancel)
742 int err, put = 0, scrubbing = 0, protect = 0;
743 struct ubi_wl_prot_entry *uninitialized_var(pe);
744 struct ubi_wl_entry *e1, *e2;
745 struct ubi_vid_hdr *vid_hdr;
747 kfree(wrk);
749 if (cancel)
750 return 0;
752 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
753 if (!vid_hdr)
754 return -ENOMEM;
756 mutex_lock(&ubi->move_mutex);
757 spin_lock(&ubi->wl_lock);
758 ubi_assert(!ubi->move_from && !ubi->move_to);
759 ubi_assert(!ubi->move_to_put);
761 if (!ubi->free.rb_node ||
762 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
764 * No free physical eraseblocks? Well, they must be waiting in
765 * the queue to be erased. Cancel movement - it will be
766 * triggered again when a free physical eraseblock appears.
768 * No used physical eraseblocks? They must be temporarily
769 * protected from being moved. They will be moved to the
770 * @ubi->used tree later and the wear-leveling will be
771 * triggered again.
773 dbg_wl("cancel WL, a list is empty: free %d, used %d",
774 !ubi->free.rb_node, !ubi->used.rb_node);
775 goto out_cancel;
778 if (!ubi->scrub.rb_node) {
780 * Now pick the least worn-out used physical eraseblock and a
781 * highly worn-out free physical eraseblock. If the erase
782 * counters differ much enough, start wear-leveling.
784 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
785 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
787 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
788 dbg_wl("no WL needed: min used EC %d, max free EC %d",
789 e1->ec, e2->ec);
790 goto out_cancel;
792 paranoid_check_in_wl_tree(e1, &ubi->used);
793 rb_erase(&e1->rb, &ubi->used);
794 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
795 e1->pnum, e1->ec, e2->pnum, e2->ec);
796 } else {
797 /* Perform scrubbing */
798 scrubbing = 1;
799 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, rb);
800 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
801 paranoid_check_in_wl_tree(e1, &ubi->scrub);
802 rb_erase(&e1->rb, &ubi->scrub);
803 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
806 paranoid_check_in_wl_tree(e2, &ubi->free);
807 rb_erase(&e2->rb, &ubi->free);
808 ubi->move_from = e1;
809 ubi->move_to = e2;
810 spin_unlock(&ubi->wl_lock);
813 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
814 * We so far do not know which logical eraseblock our physical
815 * eraseblock (@e1) belongs to. We have to read the volume identifier
816 * header first.
818 * Note, we are protected from this PEB being unmapped and erased. The
819 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
820 * which is being moved was unmapped.
823 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
824 if (err && err != UBI_IO_BITFLIPS) {
825 if (err == UBI_IO_PEB_FREE) {
827 * We are trying to move PEB without a VID header. UBI
828 * always write VID headers shortly after the PEB was
829 * given, so we have a situation when it did not have
830 * chance to write it down because it was preempted.
831 * Just re-schedule the work, so that next time it will
832 * likely have the VID header in place.
834 dbg_wl("PEB %d has no VID header", e1->pnum);
835 goto out_not_moved;
838 ubi_err("error %d while reading VID header from PEB %d",
839 err, e1->pnum);
840 if (err > 0)
841 err = -EIO;
842 goto out_error;
845 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
846 if (err) {
848 if (err < 0)
849 goto out_error;
850 if (err == 1)
851 goto out_not_moved;
854 * For some reason the LEB was not moved - it might be because
855 * the volume is being deleted. We should prevent this PEB from
856 * being selected for wear-levelling movement for some "time",
857 * so put it to the protection tree.
860 dbg_wl("cancelled moving PEB %d", e1->pnum);
861 pe = kmalloc(sizeof(struct ubi_wl_prot_entry), GFP_NOFS);
862 if (!pe) {
863 err = -ENOMEM;
864 goto out_error;
867 protect = 1;
870 ubi_free_vid_hdr(ubi, vid_hdr);
871 if (scrubbing && !protect)
872 ubi_msg("scrubbed PEB %d, data moved to PEB %d",
873 e1->pnum, e2->pnum);
875 spin_lock(&ubi->wl_lock);
876 if (protect)
877 prot_tree_add(ubi, e1, pe, protect);
878 if (!ubi->move_to_put)
879 wl_tree_add(e2, &ubi->used);
880 else
881 put = 1;
882 ubi->move_from = ubi->move_to = NULL;
883 ubi->move_to_put = ubi->wl_scheduled = 0;
884 spin_unlock(&ubi->wl_lock);
886 if (put) {
888 * Well, the target PEB was put meanwhile, schedule it for
889 * erasure.
891 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
892 err = schedule_erase(ubi, e2, 0);
893 if (err)
894 goto out_error;
897 if (!protect) {
898 err = schedule_erase(ubi, e1, 0);
899 if (err)
900 goto out_error;
904 dbg_wl("done");
905 mutex_unlock(&ubi->move_mutex);
906 return 0;
909 * For some reasons the LEB was not moved, might be an error, might be
910 * something else. @e1 was not changed, so return it back. @e2 might
911 * be changed, schedule it for erasure.
913 out_not_moved:
914 ubi_free_vid_hdr(ubi, vid_hdr);
915 spin_lock(&ubi->wl_lock);
916 if (scrubbing)
917 wl_tree_add(e1, &ubi->scrub);
918 else
919 wl_tree_add(e1, &ubi->used);
920 ubi->move_from = ubi->move_to = NULL;
921 ubi->move_to_put = ubi->wl_scheduled = 0;
922 spin_unlock(&ubi->wl_lock);
924 err = schedule_erase(ubi, e2, 0);
925 if (err)
926 goto out_error;
928 mutex_unlock(&ubi->move_mutex);
929 return 0;
931 out_error:
932 ubi_err("error %d while moving PEB %d to PEB %d",
933 err, e1->pnum, e2->pnum);
935 ubi_free_vid_hdr(ubi, vid_hdr);
936 spin_lock(&ubi->wl_lock);
937 ubi->move_from = ubi->move_to = NULL;
938 ubi->move_to_put = ubi->wl_scheduled = 0;
939 spin_unlock(&ubi->wl_lock);
941 kmem_cache_free(ubi_wl_entry_slab, e1);
942 kmem_cache_free(ubi_wl_entry_slab, e2);
943 ubi_ro_mode(ubi);
945 mutex_unlock(&ubi->move_mutex);
946 return err;
948 out_cancel:
949 ubi->wl_scheduled = 0;
950 spin_unlock(&ubi->wl_lock);
951 mutex_unlock(&ubi->move_mutex);
952 ubi_free_vid_hdr(ubi, vid_hdr);
953 return 0;
957 * ensure_wear_leveling - schedule wear-leveling if it is needed.
958 * @ubi: UBI device description object
960 * This function checks if it is time to start wear-leveling and schedules it
961 * if yes. This function returns zero in case of success and a negative error
962 * code in case of failure.
964 static int ensure_wear_leveling(struct ubi_device *ubi)
966 int err = 0;
967 struct ubi_wl_entry *e1;
968 struct ubi_wl_entry *e2;
969 struct ubi_work *wrk;
971 spin_lock(&ubi->wl_lock);
972 if (ubi->wl_scheduled)
973 /* Wear-leveling is already in the work queue */
974 goto out_unlock;
977 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
978 * the WL worker has to be scheduled anyway.
980 if (!ubi->scrub.rb_node) {
981 if (!ubi->used.rb_node || !ubi->free.rb_node)
982 /* No physical eraseblocks - no deal */
983 goto out_unlock;
986 * We schedule wear-leveling only if the difference between the
987 * lowest erase counter of used physical eraseblocks and a high
988 * erase counter of free physical eraseblocks is greater then
989 * %UBI_WL_THRESHOLD.
991 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, rb);
992 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
994 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
995 goto out_unlock;
996 dbg_wl("schedule wear-leveling");
997 } else
998 dbg_wl("schedule scrubbing");
1000 ubi->wl_scheduled = 1;
1001 spin_unlock(&ubi->wl_lock);
1003 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1004 if (!wrk) {
1005 err = -ENOMEM;
1006 goto out_cancel;
1009 wrk->func = &wear_leveling_worker;
1010 schedule_ubi_work(ubi, wrk);
1011 return err;
1013 out_cancel:
1014 spin_lock(&ubi->wl_lock);
1015 ubi->wl_scheduled = 0;
1016 out_unlock:
1017 spin_unlock(&ubi->wl_lock);
1018 return err;
1022 * erase_worker - physical eraseblock erase worker function.
1023 * @ubi: UBI device description object
1024 * @wl_wrk: the work object
1025 * @cancel: non-zero if the worker has to free memory and exit
1027 * This function erases a physical eraseblock and perform torture testing if
1028 * needed. It also takes care about marking the physical eraseblock bad if
1029 * needed. Returns zero in case of success and a negative error code in case of
1030 * failure.
1032 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1033 int cancel)
1035 struct ubi_wl_entry *e = wl_wrk->e;
1036 int pnum = e->pnum, err, need;
1038 if (cancel) {
1039 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1040 kfree(wl_wrk);
1041 kmem_cache_free(ubi_wl_entry_slab, e);
1042 return 0;
1045 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
1047 err = sync_erase(ubi, e, wl_wrk->torture);
1048 if (!err) {
1049 /* Fine, we've erased it successfully */
1050 kfree(wl_wrk);
1052 spin_lock(&ubi->wl_lock);
1053 ubi->abs_ec += 1;
1054 wl_tree_add(e, &ubi->free);
1055 spin_unlock(&ubi->wl_lock);
1058 * One more erase operation has happened, take care about
1059 * protected physical eraseblocks.
1061 check_protection_over(ubi);
1063 /* And take care about wear-leveling */
1064 err = ensure_wear_leveling(ubi);
1065 return err;
1068 ubi_err("failed to erase PEB %d, error %d", pnum, err);
1069 kfree(wl_wrk);
1070 kmem_cache_free(ubi_wl_entry_slab, e);
1072 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1073 err == -EBUSY) {
1074 int err1;
1076 /* Re-schedule the LEB for erasure */
1077 err1 = schedule_erase(ubi, e, 0);
1078 if (err1) {
1079 err = err1;
1080 goto out_ro;
1082 return err;
1083 } else if (err != -EIO) {
1085 * If this is not %-EIO, we have no idea what to do. Scheduling
1086 * this physical eraseblock for erasure again would cause
1087 * errors again and again. Well, lets switch to RO mode.
1089 goto out_ro;
1092 /* It is %-EIO, the PEB went bad */
1094 if (!ubi->bad_allowed) {
1095 ubi_err("bad physical eraseblock %d detected", pnum);
1096 goto out_ro;
1099 spin_lock(&ubi->volumes_lock);
1100 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1101 if (need > 0) {
1102 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1103 ubi->avail_pebs -= need;
1104 ubi->rsvd_pebs += need;
1105 ubi->beb_rsvd_pebs += need;
1106 if (need > 0)
1107 ubi_msg("reserve more %d PEBs", need);
1110 if (ubi->beb_rsvd_pebs == 0) {
1111 spin_unlock(&ubi->volumes_lock);
1112 ubi_err("no reserved physical eraseblocks");
1113 goto out_ro;
1116 spin_unlock(&ubi->volumes_lock);
1117 ubi_msg("mark PEB %d as bad", pnum);
1119 err = ubi_io_mark_bad(ubi, pnum);
1120 if (err)
1121 goto out_ro;
1123 spin_lock(&ubi->volumes_lock);
1124 ubi->beb_rsvd_pebs -= 1;
1125 ubi->bad_peb_count += 1;
1126 ubi->good_peb_count -= 1;
1127 ubi_calculate_reserved(ubi);
1128 if (ubi->beb_rsvd_pebs == 0)
1129 ubi_warn("last PEB from the reserved pool was used");
1130 spin_unlock(&ubi->volumes_lock);
1132 return err;
1134 out_ro:
1135 ubi_ro_mode(ubi);
1136 return err;
1140 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1141 * @ubi: UBI device description object
1142 * @pnum: physical eraseblock to return
1143 * @torture: if this physical eraseblock has to be tortured
1145 * This function is called to return physical eraseblock @pnum to the pool of
1146 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1147 * occurred to this @pnum and it has to be tested. This function returns zero
1148 * in case of success, and a negative error code in case of failure.
1150 int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1152 int err;
1153 struct ubi_wl_entry *e;
1155 dbg_wl("PEB %d", pnum);
1156 ubi_assert(pnum >= 0);
1157 ubi_assert(pnum < ubi->peb_count);
1159 retry:
1160 spin_lock(&ubi->wl_lock);
1161 e = ubi->lookuptbl[pnum];
1162 if (e == ubi->move_from) {
1164 * User is putting the physical eraseblock which was selected to
1165 * be moved. It will be scheduled for erasure in the
1166 * wear-leveling worker.
1168 dbg_wl("PEB %d is being moved, wait", pnum);
1169 spin_unlock(&ubi->wl_lock);
1171 /* Wait for the WL worker by taking the @ubi->move_mutex */
1172 mutex_lock(&ubi->move_mutex);
1173 mutex_unlock(&ubi->move_mutex);
1174 goto retry;
1175 } else if (e == ubi->move_to) {
1177 * User is putting the physical eraseblock which was selected
1178 * as the target the data is moved to. It may happen if the EBA
1179 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1180 * but the WL sub-system has not put the PEB to the "used" tree
1181 * yet, but it is about to do this. So we just set a flag which
1182 * will tell the WL worker that the PEB is not needed anymore
1183 * and should be scheduled for erasure.
1185 dbg_wl("PEB %d is the target of data moving", pnum);
1186 ubi_assert(!ubi->move_to_put);
1187 ubi->move_to_put = 1;
1188 spin_unlock(&ubi->wl_lock);
1189 return 0;
1190 } else {
1191 if (in_wl_tree(e, &ubi->used)) {
1192 paranoid_check_in_wl_tree(e, &ubi->used);
1193 rb_erase(&e->rb, &ubi->used);
1194 } else if (in_wl_tree(e, &ubi->scrub)) {
1195 paranoid_check_in_wl_tree(e, &ubi->scrub);
1196 rb_erase(&e->rb, &ubi->scrub);
1197 } else {
1198 err = prot_tree_del(ubi, e->pnum);
1199 if (err) {
1200 ubi_err("PEB %d not found", pnum);
1201 ubi_ro_mode(ubi);
1202 spin_unlock(&ubi->wl_lock);
1203 return err;
1207 spin_unlock(&ubi->wl_lock);
1209 err = schedule_erase(ubi, e, torture);
1210 if (err) {
1211 spin_lock(&ubi->wl_lock);
1212 wl_tree_add(e, &ubi->used);
1213 spin_unlock(&ubi->wl_lock);
1216 return err;
1220 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1221 * @ubi: UBI device description object
1222 * @pnum: the physical eraseblock to schedule
1224 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1225 * needs scrubbing. This function schedules a physical eraseblock for
1226 * scrubbing which is done in background. This function returns zero in case of
1227 * success and a negative error code in case of failure.
1229 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1231 struct ubi_wl_entry *e;
1233 dbg_msg("schedule PEB %d for scrubbing", pnum);
1235 retry:
1236 spin_lock(&ubi->wl_lock);
1237 e = ubi->lookuptbl[pnum];
1238 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1239 spin_unlock(&ubi->wl_lock);
1240 return 0;
1243 if (e == ubi->move_to) {
1245 * This physical eraseblock was used to move data to. The data
1246 * was moved but the PEB was not yet inserted to the proper
1247 * tree. We should just wait a little and let the WL worker
1248 * proceed.
1250 spin_unlock(&ubi->wl_lock);
1251 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1252 yield();
1253 goto retry;
1256 if (in_wl_tree(e, &ubi->used)) {
1257 paranoid_check_in_wl_tree(e, &ubi->used);
1258 rb_erase(&e->rb, &ubi->used);
1259 } else {
1260 int err;
1262 err = prot_tree_del(ubi, e->pnum);
1263 if (err) {
1264 ubi_err("PEB %d not found", pnum);
1265 ubi_ro_mode(ubi);
1266 spin_unlock(&ubi->wl_lock);
1267 return err;
1271 wl_tree_add(e, &ubi->scrub);
1272 spin_unlock(&ubi->wl_lock);
1275 * Technically scrubbing is the same as wear-leveling, so it is done
1276 * by the WL worker.
1278 return ensure_wear_leveling(ubi);
1282 * ubi_wl_flush - flush all pending works.
1283 * @ubi: UBI device description object
1285 * This function returns zero in case of success and a negative error code in
1286 * case of failure.
1288 int ubi_wl_flush(struct ubi_device *ubi)
1290 int err;
1293 * Erase while the pending works queue is not empty, but not more then
1294 * the number of currently pending works.
1296 dbg_wl("flush (%d pending works)", ubi->works_count);
1297 while (ubi->works_count) {
1298 err = do_work(ubi);
1299 if (err)
1300 return err;
1304 * Make sure all the works which have been done in parallel are
1305 * finished.
1307 down_write(&ubi->work_sem);
1308 up_write(&ubi->work_sem);
1311 * And in case last was the WL worker and it cancelled the LEB
1312 * movement, flush again.
1314 while (ubi->works_count) {
1315 dbg_wl("flush more (%d pending works)", ubi->works_count);
1316 err = do_work(ubi);
1317 if (err)
1318 return err;
1321 return 0;
1325 * tree_destroy - destroy an RB-tree.
1326 * @root: the root of the tree to destroy
1328 static void tree_destroy(struct rb_root *root)
1330 struct rb_node *rb;
1331 struct ubi_wl_entry *e;
1333 rb = root->rb_node;
1334 while (rb) {
1335 if (rb->rb_left)
1336 rb = rb->rb_left;
1337 else if (rb->rb_right)
1338 rb = rb->rb_right;
1339 else {
1340 e = rb_entry(rb, struct ubi_wl_entry, rb);
1342 rb = rb_parent(rb);
1343 if (rb) {
1344 if (rb->rb_left == &e->rb)
1345 rb->rb_left = NULL;
1346 else
1347 rb->rb_right = NULL;
1350 kmem_cache_free(ubi_wl_entry_slab, e);
1356 * ubi_thread - UBI background thread.
1357 * @u: the UBI device description object pointer
1359 int ubi_thread(void *u)
1361 int failures = 0;
1362 struct ubi_device *ubi = u;
1364 ubi_msg("background thread \"%s\" started, PID %d",
1365 ubi->bgt_name, task_pid_nr(current));
1367 set_freezable();
1368 for (;;) {
1369 int err;
1371 if (kthread_should_stop())
1372 break;
1374 if (try_to_freeze())
1375 continue;
1377 spin_lock(&ubi->wl_lock);
1378 if (list_empty(&ubi->works) || ubi->ro_mode ||
1379 !ubi->thread_enabled) {
1380 set_current_state(TASK_INTERRUPTIBLE);
1381 spin_unlock(&ubi->wl_lock);
1382 schedule();
1383 continue;
1385 spin_unlock(&ubi->wl_lock);
1387 err = do_work(ubi);
1388 if (err) {
1389 ubi_err("%s: work failed with error code %d",
1390 ubi->bgt_name, err);
1391 if (failures++ > WL_MAX_FAILURES) {
1393 * Too many failures, disable the thread and
1394 * switch to read-only mode.
1396 ubi_msg("%s: %d consecutive failures",
1397 ubi->bgt_name, WL_MAX_FAILURES);
1398 ubi_ro_mode(ubi);
1399 ubi->thread_enabled = 0;
1400 continue;
1402 } else
1403 failures = 0;
1405 cond_resched();
1408 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1409 return 0;
1413 * cancel_pending - cancel all pending works.
1414 * @ubi: UBI device description object
1416 static void cancel_pending(struct ubi_device *ubi)
1418 while (!list_empty(&ubi->works)) {
1419 struct ubi_work *wrk;
1421 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1422 list_del(&wrk->list);
1423 wrk->func(ubi, wrk, 1);
1424 ubi->works_count -= 1;
1425 ubi_assert(ubi->works_count >= 0);
1430 * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
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
1435 * case of failure.
1437 int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1439 int err;
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);
1456 err = -ENOMEM;
1457 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1458 if (!ubi->lookuptbl)
1459 return err;
1461 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1462 cond_resched();
1464 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1465 if (!e)
1466 goto out_free;
1468 e->pnum = seb->pnum;
1469 e->ec = seb->ec;
1470 ubi->lookuptbl[e->pnum] = e;
1471 if (schedule_erase(ubi, e, 0)) {
1472 kmem_cache_free(ubi_wl_entry_slab, e);
1473 goto out_free;
1477 list_for_each_entry(seb, &si->free, u.list) {
1478 cond_resched();
1480 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1481 if (!e)
1482 goto out_free;
1484 e->pnum = seb->pnum;
1485 e->ec = seb->ec;
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) {
1492 cond_resched();
1494 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1495 if (!e)
1496 goto out_free;
1498 e->pnum = seb->pnum;
1499 e->ec = seb->ec;
1500 ubi->lookuptbl[e->pnum] = e;
1501 if (schedule_erase(ubi, e, 0)) {
1502 kmem_cache_free(ubi_wl_entry_slab, e);
1503 goto out_free;
1507 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1508 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1509 cond_resched();
1511 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1512 if (!e)
1513 goto out_free;
1515 e->pnum = seb->pnum;
1516 e->ec = seb->ec;
1517 ubi->lookuptbl[e->pnum] = e;
1518 if (!seb->scrub) {
1519 dbg_wl("add PEB %d EC %d to the used tree",
1520 e->pnum, e->ec);
1521 wl_tree_add(e, &ubi->used);
1522 } else {
1523 dbg_wl("add PEB %d EC %d to the scrub tree",
1524 e->pnum, e->ec);
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);
1533 goto out_free;
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);
1540 if (err)
1541 goto out_free;
1543 return 0;
1545 out_free:
1546 cancel_pending(ubi);
1547 tree_destroy(&ubi->used);
1548 tree_destroy(&ubi->free);
1549 tree_destroy(&ubi->scrub);
1550 kfree(ubi->lookuptbl);
1551 return err;
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)
1560 struct rb_node *rb;
1561 struct ubi_wl_prot_entry *pe;
1563 rb = ubi->prot.aec.rb_node;
1564 while (rb) {
1565 if (rb->rb_left)
1566 rb = rb->rb_left;
1567 else if (rb->rb_right)
1568 rb = rb->rb_right;
1569 else {
1570 pe = rb_entry(rb, struct ubi_wl_prot_entry, rb_aec);
1572 rb = rb_parent(rb);
1573 if (rb) {
1574 if (rb->rb_left == &pe->rb_aec)
1575 rb->rb_left = NULL;
1576 else
1577 rb->rb_right = NULL;
1580 kmem_cache_free(ubi_wl_entry_slab, pe->e);
1581 kfree(pe);
1587 * ubi_wl_close - close the wear-leveling sub-system.
1588 * @ubi: UBI device description object
1590 void ubi_wl_close(struct ubi_device *ubi)
1592 dbg_wl("close the WL sub-system");
1593 cancel_pending(ubi);
1594 protection_trees_destroy(ubi);
1595 tree_destroy(&ubi->used);
1596 tree_destroy(&ubi->free);
1597 tree_destroy(&ubi->scrub);
1598 kfree(ubi->lookuptbl);
1601 #ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1604 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
1605 * @ubi: UBI device description object
1606 * @pnum: the physical eraseblock number to check
1607 * @ec: the erase counter to check
1609 * This function returns zero if the erase counter of physical eraseblock @pnum
1610 * is equivalent to @ec, %1 if not, and a negative error code if an error
1611 * occurred.
1613 static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1615 int err;
1616 long long read_ec;
1617 struct ubi_ec_hdr *ec_hdr;
1619 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1620 if (!ec_hdr)
1621 return -ENOMEM;
1623 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1624 if (err && err != UBI_IO_BITFLIPS) {
1625 /* The header does not have to exist */
1626 err = 0;
1627 goto out_free;
1630 read_ec = be64_to_cpu(ec_hdr->ec);
1631 if (ec != read_ec) {
1632 ubi_err("paranoid check failed for PEB %d", pnum);
1633 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1634 ubi_dbg_dump_stack();
1635 err = 1;
1636 } else
1637 err = 0;
1639 out_free:
1640 kfree(ec_hdr);
1641 return err;
1645 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
1646 * @e: the wear-leveling entry to check
1647 * @root: the root of the tree
1649 * This function returns zero if @e is in the @root RB-tree and %1 if it is
1650 * not.
1652 static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1653 struct rb_root *root)
1655 if (in_wl_tree(e, root))
1656 return 0;
1658 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1659 e->pnum, e->ec, root);
1660 ubi_dbg_dump_stack();
1661 return 1;
1664 #endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */