xref: /linux/drivers/mtd/ubi/wl.c (revision f49f4ab95c301dbccad0efe85296d908b8ae7ad4)
1 /*
2  * @ubi: UBI device description object
3  * Copyright (c) International Business Machines Corp., 2006
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
20  */
21 
22 /*
23  * UBI wear-leveling sub-system.
24  *
25  * This sub-system is responsible for wear-leveling. It works in terms of
26  * physical eraseblocks and erase counters and knows nothing about logical
27  * eraseblocks, volumes, etc. From this sub-system's perspective all physical
28  * eraseblocks are of two types - used and free. Used physical eraseblocks are
29  * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
30  * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
31  *
32  * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
33  * header. The rest of the physical eraseblock contains only %0xFF bytes.
34  *
35  * When physical eraseblocks are returned to the WL sub-system by means of the
36  * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
37  * done asynchronously in context of the per-UBI device background thread,
38  * which is also managed by the WL sub-system.
39  *
40  * The wear-leveling is ensured by means of moving the contents of used
41  * physical eraseblocks with low erase counter to free physical eraseblocks
42  * with high erase counter.
43  *
44  * If the WL sub-system fails to erase a physical eraseblock, it marks it as
45  * bad.
46  *
47  * This sub-system is also responsible for scrubbing. If a bit-flip is detected
48  * in a physical eraseblock, it has to be moved. Technically this is the same
49  * as moving it for wear-leveling reasons.
50  *
51  * As it was said, for the UBI sub-system all physical eraseblocks are either
52  * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
53  * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
54  * RB-trees, as well as (temporarily) in the @wl->pq queue.
55  *
56  * When the WL sub-system returns a physical eraseblock, the physical
57  * eraseblock is protected from being moved for some "time". For this reason,
58  * the physical eraseblock is not directly moved from the @wl->free tree to the
59  * @wl->used tree. There is a protection queue in between where this
60  * physical eraseblock is temporarily stored (@wl->pq).
61  *
62  * All this protection stuff is needed because:
63  *  o we don't want to move physical eraseblocks just after we have given them
64  *    to the user; instead, we first want to let users fill them up with data;
65  *
66  *  o there is a chance that the user will put the physical eraseblock very
67  *    soon, so it makes sense not to move it for some time, but wait.
68  *
69  * Physical eraseblocks stay protected only for limited time. But the "time" is
70  * measured in erase cycles in this case. This is implemented with help of the
71  * protection queue. Eraseblocks are put to the tail of this queue when they
72  * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
73  * head of the queue on each erase operation (for any eraseblock). So the
74  * length of the queue defines how may (global) erase cycles PEBs are protected.
75  *
76  * To put it differently, each physical eraseblock has 2 main states: free and
77  * used. The former state corresponds to the @wl->free tree. The latter state
78  * is split up on several sub-states:
79  * o the WL movement is allowed (@wl->used tree);
80  * o the WL movement is disallowed (@wl->erroneous) because the PEB is
81  *   erroneous - e.g., there was a read error;
82  * o the WL movement is temporarily prohibited (@wl->pq queue);
83  * o scrubbing is needed (@wl->scrub tree).
84  *
85  * Depending on the sub-state, wear-leveling entries of the used physical
86  * eraseblocks may be kept in one of those structures.
87  *
88  * Note, in this implementation, we keep a small in-RAM object for each physical
89  * eraseblock. This is surely not a scalable solution. But it appears to be good
90  * enough for moderately large flashes and it is simple. In future, one may
91  * re-work this sub-system and make it more scalable.
92  *
93  * At the moment this sub-system does not utilize the sequence number, which
94  * was introduced relatively recently. But it would be wise to do this because
95  * the sequence number of a logical eraseblock characterizes how old is it. For
96  * example, when we move a PEB with low erase counter, and we need to pick the
97  * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
98  * pick target PEB with an average EC if our PEB is not very "old". This is a
99  * room for future re-works of the WL sub-system.
100  */
101 
102 #include <linux/slab.h>
103 #include <linux/crc32.h>
104 #include <linux/freezer.h>
105 #include <linux/kthread.h>
106 #include "ubi.h"
107 
108 /* Number of physical eraseblocks reserved for wear-leveling purposes */
109 #define WL_RESERVED_PEBS 1
110 
111 /*
112  * Maximum difference between two erase counters. If this threshold is
113  * exceeded, the WL sub-system starts moving data from used physical
114  * eraseblocks with low erase counter to free physical eraseblocks with high
115  * erase counter.
116  */
117 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
118 
119 /*
120  * When a physical eraseblock is moved, the WL sub-system has to pick the target
121  * physical eraseblock to move to. The simplest way would be just to pick the
122  * one with the highest erase counter. But in certain workloads this could lead
123  * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
124  * situation when the picked physical eraseblock is constantly erased after the
125  * data is written to it. So, we have a constant which limits the highest erase
126  * counter of the free physical eraseblock to pick. Namely, the WL sub-system
127  * does not pick eraseblocks with erase counter greater than the lowest erase
128  * counter plus %WL_FREE_MAX_DIFF.
129  */
130 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
131 
132 /*
133  * Maximum number of consecutive background thread failures which is enough to
134  * switch to read-only mode.
135  */
136 #define WL_MAX_FAILURES 32
137 
138 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
139 static int self_check_in_wl_tree(const struct ubi_device *ubi,
140 				 struct ubi_wl_entry *e, struct rb_root *root);
141 static int self_check_in_pq(const struct ubi_device *ubi,
142 			    struct ubi_wl_entry *e);
143 
144 #ifdef CONFIG_MTD_UBI_FASTMAP
145 /**
146  * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
147  * @wrk: the work description object
148  */
149 static void update_fastmap_work_fn(struct work_struct *wrk)
150 {
151 	struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
152 	ubi_update_fastmap(ubi);
153 }
154 
155 /**
156  *  ubi_ubi_is_fm_block - returns 1 if a PEB is currently used in a fastmap.
157  *  @ubi: UBI device description object
158  *  @pnum: the to be checked PEB
159  */
160 static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
161 {
162 	int i;
163 
164 	if (!ubi->fm)
165 		return 0;
166 
167 	for (i = 0; i < ubi->fm->used_blocks; i++)
168 		if (ubi->fm->e[i]->pnum == pnum)
169 			return 1;
170 
171 	return 0;
172 }
173 #else
174 static int ubi_is_fm_block(struct ubi_device *ubi, int pnum)
175 {
176 	return 0;
177 }
178 #endif
179 
180 /**
181  * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
182  * @e: the wear-leveling entry to add
183  * @root: the root of the tree
184  *
185  * Note, we use (erase counter, physical eraseblock number) pairs as keys in
186  * the @ubi->used and @ubi->free RB-trees.
187  */
188 static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
189 {
190 	struct rb_node **p, *parent = NULL;
191 
192 	p = &root->rb_node;
193 	while (*p) {
194 		struct ubi_wl_entry *e1;
195 
196 		parent = *p;
197 		e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
198 
199 		if (e->ec < e1->ec)
200 			p = &(*p)->rb_left;
201 		else if (e->ec > e1->ec)
202 			p = &(*p)->rb_right;
203 		else {
204 			ubi_assert(e->pnum != e1->pnum);
205 			if (e->pnum < e1->pnum)
206 				p = &(*p)->rb_left;
207 			else
208 				p = &(*p)->rb_right;
209 		}
210 	}
211 
212 	rb_link_node(&e->u.rb, parent, p);
213 	rb_insert_color(&e->u.rb, root);
214 }
215 
216 /**
217  * do_work - do one pending work.
218  * @ubi: UBI device description object
219  *
220  * This function returns zero in case of success and a negative error code in
221  * case of failure.
222  */
223 static int do_work(struct ubi_device *ubi)
224 {
225 	int err;
226 	struct ubi_work *wrk;
227 
228 	cond_resched();
229 
230 	/*
231 	 * @ubi->work_sem is used to synchronize with the workers. Workers take
232 	 * it in read mode, so many of them may be doing works at a time. But
233 	 * the queue flush code has to be sure the whole queue of works is
234 	 * done, and it takes the mutex in write mode.
235 	 */
236 	down_read(&ubi->work_sem);
237 	spin_lock(&ubi->wl_lock);
238 	if (list_empty(&ubi->works)) {
239 		spin_unlock(&ubi->wl_lock);
240 		up_read(&ubi->work_sem);
241 		return 0;
242 	}
243 
244 	wrk = list_entry(ubi->works.next, struct ubi_work, list);
245 	list_del(&wrk->list);
246 	ubi->works_count -= 1;
247 	ubi_assert(ubi->works_count >= 0);
248 	spin_unlock(&ubi->wl_lock);
249 
250 	/*
251 	 * Call the worker function. Do not touch the work structure
252 	 * after this call as it will have been freed or reused by that
253 	 * time by the worker function.
254 	 */
255 	err = wrk->func(ubi, wrk, 0);
256 	if (err)
257 		ubi_err("work failed with error code %d", err);
258 	up_read(&ubi->work_sem);
259 
260 	return err;
261 }
262 
263 /**
264  * produce_free_peb - produce a free physical eraseblock.
265  * @ubi: UBI device description object
266  *
267  * This function tries to make a free PEB by means of synchronous execution of
268  * pending works. This may be needed if, for example the background thread is
269  * disabled. Returns zero in case of success and a negative error code in case
270  * of failure.
271  */
272 static int produce_free_peb(struct ubi_device *ubi)
273 {
274 	int err;
275 
276 	while (!ubi->free.rb_node) {
277 		spin_unlock(&ubi->wl_lock);
278 
279 		dbg_wl("do one work synchronously");
280 		err = do_work(ubi);
281 
282 		spin_lock(&ubi->wl_lock);
283 		if (err)
284 			return err;
285 	}
286 
287 	return 0;
288 }
289 
290 /**
291  * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
292  * @e: the wear-leveling entry to check
293  * @root: the root of the tree
294  *
295  * This function returns non-zero if @e is in the @root RB-tree and zero if it
296  * is not.
297  */
298 static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
299 {
300 	struct rb_node *p;
301 
302 	p = root->rb_node;
303 	while (p) {
304 		struct ubi_wl_entry *e1;
305 
306 		e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
307 
308 		if (e->pnum == e1->pnum) {
309 			ubi_assert(e == e1);
310 			return 1;
311 		}
312 
313 		if (e->ec < e1->ec)
314 			p = p->rb_left;
315 		else if (e->ec > e1->ec)
316 			p = p->rb_right;
317 		else {
318 			ubi_assert(e->pnum != e1->pnum);
319 			if (e->pnum < e1->pnum)
320 				p = p->rb_left;
321 			else
322 				p = p->rb_right;
323 		}
324 	}
325 
326 	return 0;
327 }
328 
329 /**
330  * prot_queue_add - add physical eraseblock to the protection queue.
331  * @ubi: UBI device description object
332  * @e: the physical eraseblock to add
333  *
334  * This function adds @e to the tail of the protection queue @ubi->pq, where
335  * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
336  * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
337  * be locked.
338  */
339 static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
340 {
341 	int pq_tail = ubi->pq_head - 1;
342 
343 	if (pq_tail < 0)
344 		pq_tail = UBI_PROT_QUEUE_LEN - 1;
345 	ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
346 	list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
347 	dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
348 }
349 
350 /**
351  * find_wl_entry - find wear-leveling entry closest to certain erase counter.
352  * @ubi: UBI device description object
353  * @root: the RB-tree where to look for
354  * @diff: maximum possible difference from the smallest erase counter
355  *
356  * This function looks for a wear leveling entry with erase counter closest to
357  * min + @diff, where min is the smallest erase counter.
358  */
359 static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
360 					  struct rb_root *root, int diff)
361 {
362 	struct rb_node *p;
363 	struct ubi_wl_entry *e, *prev_e = NULL;
364 	int max;
365 
366 	e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
367 	max = e->ec + diff;
368 
369 	p = root->rb_node;
370 	while (p) {
371 		struct ubi_wl_entry *e1;
372 
373 		e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
374 		if (e1->ec >= max)
375 			p = p->rb_left;
376 		else {
377 			p = p->rb_right;
378 			prev_e = e;
379 			e = e1;
380 		}
381 	}
382 
383 	/* If no fastmap has been written and this WL entry can be used
384 	 * as anchor PEB, hold it back and return the second best WL entry
385 	 * such that fastmap can use the anchor PEB later. */
386 	if (prev_e && !ubi->fm_disabled &&
387 	    !ubi->fm && e->pnum < UBI_FM_MAX_START)
388 		return prev_e;
389 
390 	return e;
391 }
392 
393 /**
394  * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
395  * @ubi: UBI device description object
396  * @root: the RB-tree where to look for
397  *
398  * This function looks for a wear leveling entry with medium erase counter,
399  * but not greater or equivalent than the lowest erase counter plus
400  * %WL_FREE_MAX_DIFF/2.
401  */
402 static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
403 					       struct rb_root *root)
404 {
405 	struct ubi_wl_entry *e, *first, *last;
406 
407 	first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
408 	last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
409 
410 	if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
411 		e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
412 
413 #ifdef CONFIG_MTD_UBI_FASTMAP
414 		/* If no fastmap has been written and this WL entry can be used
415 		 * as anchor PEB, hold it back and return the second best
416 		 * WL entry such that fastmap can use the anchor PEB later. */
417 		if (e && !ubi->fm_disabled && !ubi->fm &&
418 		    e->pnum < UBI_FM_MAX_START)
419 			e = rb_entry(rb_next(root->rb_node),
420 				     struct ubi_wl_entry, u.rb);
421 #endif
422 	} else
423 		e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
424 
425 	return e;
426 }
427 
428 #ifdef CONFIG_MTD_UBI_FASTMAP
429 /**
430  * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
431  * @root: the RB-tree where to look for
432  */
433 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
434 {
435 	struct rb_node *p;
436 	struct ubi_wl_entry *e, *victim = NULL;
437 	int max_ec = UBI_MAX_ERASECOUNTER;
438 
439 	ubi_rb_for_each_entry(p, e, root, u.rb) {
440 		if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
441 			victim = e;
442 			max_ec = e->ec;
443 		}
444 	}
445 
446 	return victim;
447 }
448 
449 static int anchor_pebs_avalible(struct rb_root *root)
450 {
451 	struct rb_node *p;
452 	struct ubi_wl_entry *e;
453 
454 	ubi_rb_for_each_entry(p, e, root, u.rb)
455 		if (e->pnum < UBI_FM_MAX_START)
456 			return 1;
457 
458 	return 0;
459 }
460 
461 /**
462  * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
463  * @ubi: UBI device description object
464  * @anchor: This PEB will be used as anchor PEB by fastmap
465  *
466  * The function returns a physical erase block with a given maximal number
467  * and removes it from the wl subsystem.
468  * Must be called with wl_lock held!
469  */
470 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
471 {
472 	struct ubi_wl_entry *e = NULL;
473 
474 	if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
475 		goto out;
476 
477 	if (anchor)
478 		e = find_anchor_wl_entry(&ubi->free);
479 	else
480 		e = find_mean_wl_entry(ubi, &ubi->free);
481 
482 	if (!e)
483 		goto out;
484 
485 	self_check_in_wl_tree(ubi, e, &ubi->free);
486 
487 	/* remove it from the free list,
488 	 * the wl subsystem does no longer know this erase block */
489 	rb_erase(&e->u.rb, &ubi->free);
490 	ubi->free_count--;
491 out:
492 	return e;
493 }
494 #endif
495 
496 /**
497  * __wl_get_peb - get a physical eraseblock.
498  * @ubi: UBI device description object
499  *
500  * This function returns a physical eraseblock in case of success and a
501  * negative error code in case of failure. Might sleep.
502  */
503 static int __wl_get_peb(struct ubi_device *ubi)
504 {
505 	int err;
506 	struct ubi_wl_entry *e;
507 
508 retry:
509 	if (!ubi->free.rb_node) {
510 		if (ubi->works_count == 0) {
511 			ubi_err("no free eraseblocks");
512 			ubi_assert(list_empty(&ubi->works));
513 			return -ENOSPC;
514 		}
515 
516 		err = produce_free_peb(ubi);
517 		if (err < 0)
518 			return err;
519 		goto retry;
520 	}
521 
522 	e = find_mean_wl_entry(ubi, &ubi->free);
523 	if (!e) {
524 		ubi_err("no free eraseblocks");
525 		return -ENOSPC;
526 	}
527 
528 	self_check_in_wl_tree(ubi, e, &ubi->free);
529 
530 	/*
531 	 * Move the physical eraseblock to the protection queue where it will
532 	 * be protected from being moved for some time.
533 	 */
534 	rb_erase(&e->u.rb, &ubi->free);
535 	ubi->free_count--;
536 	dbg_wl("PEB %d EC %d", e->pnum, e->ec);
537 #ifndef CONFIG_MTD_UBI_FASTMAP
538 	/* We have to enqueue e only if fastmap is disabled,
539 	 * is fastmap enabled prot_queue_add() will be called by
540 	 * ubi_wl_get_peb() after removing e from the pool. */
541 	prot_queue_add(ubi, e);
542 #endif
543 	err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
544 				    ubi->peb_size - ubi->vid_hdr_aloffset);
545 	if (err) {
546 		ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
547 		return err;
548 	}
549 
550 	return e->pnum;
551 }
552 
553 #ifdef CONFIG_MTD_UBI_FASTMAP
554 /**
555  * return_unused_pool_pebs - returns unused PEB to the free tree.
556  * @ubi: UBI device description object
557  * @pool: fastmap pool description object
558  */
559 static void return_unused_pool_pebs(struct ubi_device *ubi,
560 				    struct ubi_fm_pool *pool)
561 {
562 	int i;
563 	struct ubi_wl_entry *e;
564 
565 	for (i = pool->used; i < pool->size; i++) {
566 		e = ubi->lookuptbl[pool->pebs[i]];
567 		wl_tree_add(e, &ubi->free);
568 		ubi->free_count++;
569 	}
570 }
571 
572 /**
573  * refill_wl_pool - refills all the fastmap pool used by the
574  * WL sub-system.
575  * @ubi: UBI device description object
576  */
577 static void refill_wl_pool(struct ubi_device *ubi)
578 {
579 	struct ubi_wl_entry *e;
580 	struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
581 
582 	return_unused_pool_pebs(ubi, pool);
583 
584 	for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
585 		if (!ubi->free.rb_node ||
586 		   (ubi->free_count - ubi->beb_rsvd_pebs < 5))
587 			break;
588 
589 		e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
590 		self_check_in_wl_tree(ubi, e, &ubi->free);
591 		rb_erase(&e->u.rb, &ubi->free);
592 		ubi->free_count--;
593 
594 		pool->pebs[pool->size] = e->pnum;
595 	}
596 	pool->used = 0;
597 }
598 
599 /**
600  * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
601  * @ubi: UBI device description object
602  */
603 static void refill_wl_user_pool(struct ubi_device *ubi)
604 {
605 	struct ubi_fm_pool *pool = &ubi->fm_pool;
606 
607 	return_unused_pool_pebs(ubi, pool);
608 
609 	for (pool->size = 0; pool->size < pool->max_size; pool->size++) {
610 		if (!ubi->free.rb_node ||
611 		   (ubi->free_count - ubi->beb_rsvd_pebs < 1))
612 			break;
613 
614 		pool->pebs[pool->size] = __wl_get_peb(ubi);
615 		if (pool->pebs[pool->size] < 0)
616 			break;
617 	}
618 	pool->used = 0;
619 }
620 
621 /**
622  * ubi_refill_pools - refills all fastmap PEB pools.
623  * @ubi: UBI device description object
624  */
625 void ubi_refill_pools(struct ubi_device *ubi)
626 {
627 	spin_lock(&ubi->wl_lock);
628 	refill_wl_pool(ubi);
629 	refill_wl_user_pool(ubi);
630 	spin_unlock(&ubi->wl_lock);
631 }
632 
633 /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
634  * the fastmap pool.
635  */
636 int ubi_wl_get_peb(struct ubi_device *ubi)
637 {
638 	int ret;
639 	struct ubi_fm_pool *pool = &ubi->fm_pool;
640 	struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
641 
642 	if (!pool->size || !wl_pool->size || pool->used == pool->size ||
643 	    wl_pool->used == wl_pool->size)
644 		ubi_update_fastmap(ubi);
645 
646 	/* we got not a single free PEB */
647 	if (!pool->size)
648 		ret = -ENOSPC;
649 	else {
650 		spin_lock(&ubi->wl_lock);
651 		ret = pool->pebs[pool->used++];
652 		prot_queue_add(ubi, ubi->lookuptbl[ret]);
653 		spin_unlock(&ubi->wl_lock);
654 	}
655 
656 	return ret;
657 }
658 
659 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
660  *
661  * @ubi: UBI device description object
662  */
663 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
664 {
665 	struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
666 	int pnum;
667 
668 	if (pool->used == pool->size || !pool->size) {
669 		/* We cannot update the fastmap here because this
670 		 * function is called in atomic context.
671 		 * Let's fail here and refill/update it as soon as possible. */
672 		schedule_work(&ubi->fm_work);
673 		return NULL;
674 	} else {
675 		pnum = pool->pebs[pool->used++];
676 		return ubi->lookuptbl[pnum];
677 	}
678 }
679 #else
680 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
681 {
682 	return find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
683 }
684 
685 int ubi_wl_get_peb(struct ubi_device *ubi)
686 {
687 	int peb;
688 
689 	spin_lock(&ubi->wl_lock);
690 	peb = __wl_get_peb(ubi);
691 	spin_unlock(&ubi->wl_lock);
692 
693 	return peb;
694 }
695 #endif
696 
697 /**
698  * prot_queue_del - remove a physical eraseblock from the protection queue.
699  * @ubi: UBI device description object
700  * @pnum: the physical eraseblock to remove
701  *
702  * This function deletes PEB @pnum from the protection queue and returns zero
703  * in case of success and %-ENODEV if the PEB was not found.
704  */
705 static int prot_queue_del(struct ubi_device *ubi, int pnum)
706 {
707 	struct ubi_wl_entry *e;
708 
709 	e = ubi->lookuptbl[pnum];
710 	if (!e)
711 		return -ENODEV;
712 
713 	if (self_check_in_pq(ubi, e))
714 		return -ENODEV;
715 
716 	list_del(&e->u.list);
717 	dbg_wl("deleted PEB %d from the protection queue", e->pnum);
718 	return 0;
719 }
720 
721 /**
722  * sync_erase - synchronously erase a physical eraseblock.
723  * @ubi: UBI device description object
724  * @e: the the physical eraseblock to erase
725  * @torture: if the physical eraseblock has to be tortured
726  *
727  * This function returns zero in case of success and a negative error code in
728  * case of failure.
729  */
730 static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
731 		      int torture)
732 {
733 	int err;
734 	struct ubi_ec_hdr *ec_hdr;
735 	unsigned long long ec = e->ec;
736 
737 	dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
738 
739 	err = self_check_ec(ubi, e->pnum, e->ec);
740 	if (err)
741 		return -EINVAL;
742 
743 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
744 	if (!ec_hdr)
745 		return -ENOMEM;
746 
747 	err = ubi_io_sync_erase(ubi, e->pnum, torture);
748 	if (err < 0)
749 		goto out_free;
750 
751 	ec += err;
752 	if (ec > UBI_MAX_ERASECOUNTER) {
753 		/*
754 		 * Erase counter overflow. Upgrade UBI and use 64-bit
755 		 * erase counters internally.
756 		 */
757 		ubi_err("erase counter overflow at PEB %d, EC %llu",
758 			e->pnum, ec);
759 		err = -EINVAL;
760 		goto out_free;
761 	}
762 
763 	dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
764 
765 	ec_hdr->ec = cpu_to_be64(ec);
766 
767 	err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
768 	if (err)
769 		goto out_free;
770 
771 	e->ec = ec;
772 	spin_lock(&ubi->wl_lock);
773 	if (e->ec > ubi->max_ec)
774 		ubi->max_ec = e->ec;
775 	spin_unlock(&ubi->wl_lock);
776 
777 out_free:
778 	kfree(ec_hdr);
779 	return err;
780 }
781 
782 /**
783  * serve_prot_queue - check if it is time to stop protecting PEBs.
784  * @ubi: UBI device description object
785  *
786  * This function is called after each erase operation and removes PEBs from the
787  * tail of the protection queue. These PEBs have been protected for long enough
788  * and should be moved to the used tree.
789  */
790 static void serve_prot_queue(struct ubi_device *ubi)
791 {
792 	struct ubi_wl_entry *e, *tmp;
793 	int count;
794 
795 	/*
796 	 * There may be several protected physical eraseblock to remove,
797 	 * process them all.
798 	 */
799 repeat:
800 	count = 0;
801 	spin_lock(&ubi->wl_lock);
802 	list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
803 		dbg_wl("PEB %d EC %d protection over, move to used tree",
804 			e->pnum, e->ec);
805 
806 		list_del(&e->u.list);
807 		wl_tree_add(e, &ubi->used);
808 		if (count++ > 32) {
809 			/*
810 			 * Let's be nice and avoid holding the spinlock for
811 			 * too long.
812 			 */
813 			spin_unlock(&ubi->wl_lock);
814 			cond_resched();
815 			goto repeat;
816 		}
817 	}
818 
819 	ubi->pq_head += 1;
820 	if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
821 		ubi->pq_head = 0;
822 	ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
823 	spin_unlock(&ubi->wl_lock);
824 }
825 
826 /**
827  * __schedule_ubi_work - schedule a work.
828  * @ubi: UBI device description object
829  * @wrk: the work to schedule
830  *
831  * This function adds a work defined by @wrk to the tail of the pending works
832  * list. Can only be used of ubi->work_sem is already held in read mode!
833  */
834 static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
835 {
836 	spin_lock(&ubi->wl_lock);
837 	list_add_tail(&wrk->list, &ubi->works);
838 	ubi_assert(ubi->works_count >= 0);
839 	ubi->works_count += 1;
840 	if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
841 		wake_up_process(ubi->bgt_thread);
842 	spin_unlock(&ubi->wl_lock);
843 }
844 
845 /**
846  * schedule_ubi_work - schedule a work.
847  * @ubi: UBI device description object
848  * @wrk: the work to schedule
849  *
850  * This function adds a work defined by @wrk to the tail of the pending works
851  * list.
852  */
853 static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
854 {
855 	down_read(&ubi->work_sem);
856 	__schedule_ubi_work(ubi, wrk);
857 	up_read(&ubi->work_sem);
858 }
859 
860 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
861 			int cancel);
862 
863 #ifdef CONFIG_MTD_UBI_FASTMAP
864 /**
865  * ubi_is_erase_work - checks whether a work is erase work.
866  * @wrk: The work object to be checked
867  */
868 int ubi_is_erase_work(struct ubi_work *wrk)
869 {
870 	return wrk->func == erase_worker;
871 }
872 #endif
873 
874 /**
875  * schedule_erase - schedule an erase work.
876  * @ubi: UBI device description object
877  * @e: the WL entry of the physical eraseblock to erase
878  * @vol_id: the volume ID that last used this PEB
879  * @lnum: the last used logical eraseblock number for the PEB
880  * @torture: if the physical eraseblock has to be tortured
881  *
882  * This function returns zero in case of success and a %-ENOMEM in case of
883  * failure.
884  */
885 static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
886 			  int vol_id, int lnum, int torture)
887 {
888 	struct ubi_work *wl_wrk;
889 
890 	ubi_assert(e);
891 	ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
892 
893 	dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
894 	       e->pnum, e->ec, torture);
895 
896 	wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
897 	if (!wl_wrk)
898 		return -ENOMEM;
899 
900 	wl_wrk->func = &erase_worker;
901 	wl_wrk->e = e;
902 	wl_wrk->vol_id = vol_id;
903 	wl_wrk->lnum = lnum;
904 	wl_wrk->torture = torture;
905 
906 	schedule_ubi_work(ubi, wl_wrk);
907 	return 0;
908 }
909 
910 /**
911  * do_sync_erase - run the erase worker synchronously.
912  * @ubi: UBI device description object
913  * @e: the WL entry of the physical eraseblock to erase
914  * @vol_id: the volume ID that last used this PEB
915  * @lnum: the last used logical eraseblock number for the PEB
916  * @torture: if the physical eraseblock has to be tortured
917  *
918  */
919 static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
920 			 int vol_id, int lnum, int torture)
921 {
922 	struct ubi_work *wl_wrk;
923 
924 	dbg_wl("sync erase of PEB %i", e->pnum);
925 
926 	wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
927 	if (!wl_wrk)
928 		return -ENOMEM;
929 
930 	wl_wrk->e = e;
931 	wl_wrk->vol_id = vol_id;
932 	wl_wrk->lnum = lnum;
933 	wl_wrk->torture = torture;
934 
935 	return erase_worker(ubi, wl_wrk, 0);
936 }
937 
938 #ifdef CONFIG_MTD_UBI_FASTMAP
939 /**
940  * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
941  * sub-system.
942  * see: ubi_wl_put_peb()
943  *
944  * @ubi: UBI device description object
945  * @fm_e: physical eraseblock to return
946  * @lnum: the last used logical eraseblock number for the PEB
947  * @torture: if this physical eraseblock has to be tortured
948  */
949 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
950 		      int lnum, int torture)
951 {
952 	struct ubi_wl_entry *e;
953 	int vol_id, pnum = fm_e->pnum;
954 
955 	dbg_wl("PEB %d", pnum);
956 
957 	ubi_assert(pnum >= 0);
958 	ubi_assert(pnum < ubi->peb_count);
959 
960 	spin_lock(&ubi->wl_lock);
961 	e = ubi->lookuptbl[pnum];
962 
963 	/* This can happen if we recovered from a fastmap the very
964 	 * first time and writing now a new one. In this case the wl system
965 	 * has never seen any PEB used by the original fastmap.
966 	 */
967 	if (!e) {
968 		e = fm_e;
969 		ubi_assert(e->ec >= 0);
970 		ubi->lookuptbl[pnum] = e;
971 	} else {
972 		e->ec = fm_e->ec;
973 		kfree(fm_e);
974 	}
975 
976 	spin_unlock(&ubi->wl_lock);
977 
978 	vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
979 	return schedule_erase(ubi, e, vol_id, lnum, torture);
980 }
981 #endif
982 
983 /**
984  * wear_leveling_worker - wear-leveling worker function.
985  * @ubi: UBI device description object
986  * @wrk: the work object
987  * @cancel: non-zero if the worker has to free memory and exit
988  *
989  * This function copies a more worn out physical eraseblock to a less worn out
990  * one. Returns zero in case of success and a negative error code in case of
991  * failure.
992  */
993 static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
994 				int cancel)
995 {
996 	int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
997 	int vol_id = -1, uninitialized_var(lnum);
998 #ifdef CONFIG_MTD_UBI_FASTMAP
999 	int anchor = wrk->anchor;
1000 #endif
1001 	struct ubi_wl_entry *e1, *e2;
1002 	struct ubi_vid_hdr *vid_hdr;
1003 
1004 	kfree(wrk);
1005 	if (cancel)
1006 		return 0;
1007 
1008 	vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
1009 	if (!vid_hdr)
1010 		return -ENOMEM;
1011 
1012 	mutex_lock(&ubi->move_mutex);
1013 	spin_lock(&ubi->wl_lock);
1014 	ubi_assert(!ubi->move_from && !ubi->move_to);
1015 	ubi_assert(!ubi->move_to_put);
1016 
1017 	if (!ubi->free.rb_node ||
1018 	    (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
1019 		/*
1020 		 * No free physical eraseblocks? Well, they must be waiting in
1021 		 * the queue to be erased. Cancel movement - it will be
1022 		 * triggered again when a free physical eraseblock appears.
1023 		 *
1024 		 * No used physical eraseblocks? They must be temporarily
1025 		 * protected from being moved. They will be moved to the
1026 		 * @ubi->used tree later and the wear-leveling will be
1027 		 * triggered again.
1028 		 */
1029 		dbg_wl("cancel WL, a list is empty: free %d, used %d",
1030 		       !ubi->free.rb_node, !ubi->used.rb_node);
1031 		goto out_cancel;
1032 	}
1033 
1034 #ifdef CONFIG_MTD_UBI_FASTMAP
1035 	/* Check whether we need to produce an anchor PEB */
1036 	if (!anchor)
1037 		anchor = !anchor_pebs_avalible(&ubi->free);
1038 
1039 	if (anchor) {
1040 		e1 = find_anchor_wl_entry(&ubi->used);
1041 		if (!e1)
1042 			goto out_cancel;
1043 		e2 = get_peb_for_wl(ubi);
1044 		if (!e2)
1045 			goto out_cancel;
1046 
1047 		self_check_in_wl_tree(ubi, e1, &ubi->used);
1048 		rb_erase(&e1->u.rb, &ubi->used);
1049 		dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
1050 	} else if (!ubi->scrub.rb_node) {
1051 #else
1052 	if (!ubi->scrub.rb_node) {
1053 #endif
1054 		/*
1055 		 * Now pick the least worn-out used physical eraseblock and a
1056 		 * highly worn-out free physical eraseblock. If the erase
1057 		 * counters differ much enough, start wear-leveling.
1058 		 */
1059 		e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1060 		e2 = get_peb_for_wl(ubi);
1061 		if (!e2)
1062 			goto out_cancel;
1063 
1064 		if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
1065 			dbg_wl("no WL needed: min used EC %d, max free EC %d",
1066 			       e1->ec, e2->ec);
1067 			goto out_cancel;
1068 		}
1069 		self_check_in_wl_tree(ubi, e1, &ubi->used);
1070 		rb_erase(&e1->u.rb, &ubi->used);
1071 		dbg_wl("move PEB %d EC %d to PEB %d EC %d",
1072 		       e1->pnum, e1->ec, e2->pnum, e2->ec);
1073 	} else {
1074 		/* Perform scrubbing */
1075 		scrubbing = 1;
1076 		e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
1077 		e2 = get_peb_for_wl(ubi);
1078 		if (!e2)
1079 			goto out_cancel;
1080 
1081 		self_check_in_wl_tree(ubi, e1, &ubi->scrub);
1082 		rb_erase(&e1->u.rb, &ubi->scrub);
1083 		dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
1084 	}
1085 
1086 	ubi->move_from = e1;
1087 	ubi->move_to = e2;
1088 	spin_unlock(&ubi->wl_lock);
1089 
1090 	/*
1091 	 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
1092 	 * We so far do not know which logical eraseblock our physical
1093 	 * eraseblock (@e1) belongs to. We have to read the volume identifier
1094 	 * header first.
1095 	 *
1096 	 * Note, we are protected from this PEB being unmapped and erased. The
1097 	 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
1098 	 * which is being moved was unmapped.
1099 	 */
1100 
1101 	err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
1102 	if (err && err != UBI_IO_BITFLIPS) {
1103 		if (err == UBI_IO_FF) {
1104 			/*
1105 			 * We are trying to move PEB without a VID header. UBI
1106 			 * always write VID headers shortly after the PEB was
1107 			 * given, so we have a situation when it has not yet
1108 			 * had a chance to write it, because it was preempted.
1109 			 * So add this PEB to the protection queue so far,
1110 			 * because presumably more data will be written there
1111 			 * (including the missing VID header), and then we'll
1112 			 * move it.
1113 			 */
1114 			dbg_wl("PEB %d has no VID header", e1->pnum);
1115 			protect = 1;
1116 			goto out_not_moved;
1117 		} else if (err == UBI_IO_FF_BITFLIPS) {
1118 			/*
1119 			 * The same situation as %UBI_IO_FF, but bit-flips were
1120 			 * detected. It is better to schedule this PEB for
1121 			 * scrubbing.
1122 			 */
1123 			dbg_wl("PEB %d has no VID header but has bit-flips",
1124 			       e1->pnum);
1125 			scrubbing = 1;
1126 			goto out_not_moved;
1127 		}
1128 
1129 		ubi_err("error %d while reading VID header from PEB %d",
1130 			err, e1->pnum);
1131 		goto out_error;
1132 	}
1133 
1134 	vol_id = be32_to_cpu(vid_hdr->vol_id);
1135 	lnum = be32_to_cpu(vid_hdr->lnum);
1136 
1137 	err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
1138 	if (err) {
1139 		if (err == MOVE_CANCEL_RACE) {
1140 			/*
1141 			 * The LEB has not been moved because the volume is
1142 			 * being deleted or the PEB has been put meanwhile. We
1143 			 * should prevent this PEB from being selected for
1144 			 * wear-leveling movement again, so put it to the
1145 			 * protection queue.
1146 			 */
1147 			protect = 1;
1148 			goto out_not_moved;
1149 		}
1150 		if (err == MOVE_RETRY) {
1151 			scrubbing = 1;
1152 			goto out_not_moved;
1153 		}
1154 		if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
1155 		    err == MOVE_TARGET_RD_ERR) {
1156 			/*
1157 			 * Target PEB had bit-flips or write error - torture it.
1158 			 */
1159 			torture = 1;
1160 			goto out_not_moved;
1161 		}
1162 
1163 		if (err == MOVE_SOURCE_RD_ERR) {
1164 			/*
1165 			 * An error happened while reading the source PEB. Do
1166 			 * not switch to R/O mode in this case, and give the
1167 			 * upper layers a possibility to recover from this,
1168 			 * e.g. by unmapping corresponding LEB. Instead, just
1169 			 * put this PEB to the @ubi->erroneous list to prevent
1170 			 * UBI from trying to move it over and over again.
1171 			 */
1172 			if (ubi->erroneous_peb_count > ubi->max_erroneous) {
1173 				ubi_err("too many erroneous eraseblocks (%d)",
1174 					ubi->erroneous_peb_count);
1175 				goto out_error;
1176 			}
1177 			erroneous = 1;
1178 			goto out_not_moved;
1179 		}
1180 
1181 		if (err < 0)
1182 			goto out_error;
1183 
1184 		ubi_assert(0);
1185 	}
1186 
1187 	/* The PEB has been successfully moved */
1188 	if (scrubbing)
1189 		ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
1190 			e1->pnum, vol_id, lnum, e2->pnum);
1191 	ubi_free_vid_hdr(ubi, vid_hdr);
1192 
1193 	spin_lock(&ubi->wl_lock);
1194 	if (!ubi->move_to_put) {
1195 		wl_tree_add(e2, &ubi->used);
1196 		e2 = NULL;
1197 	}
1198 	ubi->move_from = ubi->move_to = NULL;
1199 	ubi->move_to_put = ubi->wl_scheduled = 0;
1200 	spin_unlock(&ubi->wl_lock);
1201 
1202 	err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
1203 	if (err) {
1204 		kmem_cache_free(ubi_wl_entry_slab, e1);
1205 		if (e2)
1206 			kmem_cache_free(ubi_wl_entry_slab, e2);
1207 		goto out_ro;
1208 	}
1209 
1210 	if (e2) {
1211 		/*
1212 		 * Well, the target PEB was put meanwhile, schedule it for
1213 		 * erasure.
1214 		 */
1215 		dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
1216 		       e2->pnum, vol_id, lnum);
1217 		err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
1218 		if (err) {
1219 			kmem_cache_free(ubi_wl_entry_slab, e2);
1220 			goto out_ro;
1221 		}
1222 	}
1223 
1224 	dbg_wl("done");
1225 	mutex_unlock(&ubi->move_mutex);
1226 	return 0;
1227 
1228 	/*
1229 	 * For some reasons the LEB was not moved, might be an error, might be
1230 	 * something else. @e1 was not changed, so return it back. @e2 might
1231 	 * have been changed, schedule it for erasure.
1232 	 */
1233 out_not_moved:
1234 	if (vol_id != -1)
1235 		dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
1236 		       e1->pnum, vol_id, lnum, e2->pnum, err);
1237 	else
1238 		dbg_wl("cancel moving PEB %d to PEB %d (%d)",
1239 		       e1->pnum, e2->pnum, err);
1240 	spin_lock(&ubi->wl_lock);
1241 	if (protect)
1242 		prot_queue_add(ubi, e1);
1243 	else if (erroneous) {
1244 		wl_tree_add(e1, &ubi->erroneous);
1245 		ubi->erroneous_peb_count += 1;
1246 	} else if (scrubbing)
1247 		wl_tree_add(e1, &ubi->scrub);
1248 	else
1249 		wl_tree_add(e1, &ubi->used);
1250 	ubi_assert(!ubi->move_to_put);
1251 	ubi->move_from = ubi->move_to = NULL;
1252 	ubi->wl_scheduled = 0;
1253 	spin_unlock(&ubi->wl_lock);
1254 
1255 	ubi_free_vid_hdr(ubi, vid_hdr);
1256 	err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
1257 	if (err) {
1258 		kmem_cache_free(ubi_wl_entry_slab, e2);
1259 		goto out_ro;
1260 	}
1261 	mutex_unlock(&ubi->move_mutex);
1262 	return 0;
1263 
1264 out_error:
1265 	if (vol_id != -1)
1266 		ubi_err("error %d while moving PEB %d to PEB %d",
1267 			err, e1->pnum, e2->pnum);
1268 	else
1269 		ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
1270 			err, e1->pnum, vol_id, lnum, e2->pnum);
1271 	spin_lock(&ubi->wl_lock);
1272 	ubi->move_from = ubi->move_to = NULL;
1273 	ubi->move_to_put = ubi->wl_scheduled = 0;
1274 	spin_unlock(&ubi->wl_lock);
1275 
1276 	ubi_free_vid_hdr(ubi, vid_hdr);
1277 	kmem_cache_free(ubi_wl_entry_slab, e1);
1278 	kmem_cache_free(ubi_wl_entry_slab, e2);
1279 
1280 out_ro:
1281 	ubi_ro_mode(ubi);
1282 	mutex_unlock(&ubi->move_mutex);
1283 	ubi_assert(err != 0);
1284 	return err < 0 ? err : -EIO;
1285 
1286 out_cancel:
1287 	ubi->wl_scheduled = 0;
1288 	spin_unlock(&ubi->wl_lock);
1289 	mutex_unlock(&ubi->move_mutex);
1290 	ubi_free_vid_hdr(ubi, vid_hdr);
1291 	return 0;
1292 }
1293 
1294 /**
1295  * ensure_wear_leveling - schedule wear-leveling if it is needed.
1296  * @ubi: UBI device description object
1297  * @nested: set to non-zero if this function is called from UBI worker
1298  *
1299  * This function checks if it is time to start wear-leveling and schedules it
1300  * if yes. This function returns zero in case of success and a negative error
1301  * code in case of failure.
1302  */
1303 static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
1304 {
1305 	int err = 0;
1306 	struct ubi_wl_entry *e1;
1307 	struct ubi_wl_entry *e2;
1308 	struct ubi_work *wrk;
1309 
1310 	spin_lock(&ubi->wl_lock);
1311 	if (ubi->wl_scheduled)
1312 		/* Wear-leveling is already in the work queue */
1313 		goto out_unlock;
1314 
1315 	/*
1316 	 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1317 	 * the WL worker has to be scheduled anyway.
1318 	 */
1319 	if (!ubi->scrub.rb_node) {
1320 		if (!ubi->used.rb_node || !ubi->free.rb_node)
1321 			/* No physical eraseblocks - no deal */
1322 			goto out_unlock;
1323 
1324 		/*
1325 		 * We schedule wear-leveling only if the difference between the
1326 		 * lowest erase counter of used physical eraseblocks and a high
1327 		 * erase counter of free physical eraseblocks is greater than
1328 		 * %UBI_WL_THRESHOLD.
1329 		 */
1330 		e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1331 		e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1332 
1333 		if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
1334 			goto out_unlock;
1335 		dbg_wl("schedule wear-leveling");
1336 	} else
1337 		dbg_wl("schedule scrubbing");
1338 
1339 	ubi->wl_scheduled = 1;
1340 	spin_unlock(&ubi->wl_lock);
1341 
1342 	wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1343 	if (!wrk) {
1344 		err = -ENOMEM;
1345 		goto out_cancel;
1346 	}
1347 
1348 	wrk->anchor = 0;
1349 	wrk->func = &wear_leveling_worker;
1350 	if (nested)
1351 		__schedule_ubi_work(ubi, wrk);
1352 	else
1353 		schedule_ubi_work(ubi, wrk);
1354 	return err;
1355 
1356 out_cancel:
1357 	spin_lock(&ubi->wl_lock);
1358 	ubi->wl_scheduled = 0;
1359 out_unlock:
1360 	spin_unlock(&ubi->wl_lock);
1361 	return err;
1362 }
1363 
1364 #ifdef CONFIG_MTD_UBI_FASTMAP
1365 /**
1366  * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
1367  * @ubi: UBI device description object
1368  */
1369 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
1370 {
1371 	struct ubi_work *wrk;
1372 
1373 	spin_lock(&ubi->wl_lock);
1374 	if (ubi->wl_scheduled) {
1375 		spin_unlock(&ubi->wl_lock);
1376 		return 0;
1377 	}
1378 	ubi->wl_scheduled = 1;
1379 	spin_unlock(&ubi->wl_lock);
1380 
1381 	wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1382 	if (!wrk) {
1383 		spin_lock(&ubi->wl_lock);
1384 		ubi->wl_scheduled = 0;
1385 		spin_unlock(&ubi->wl_lock);
1386 		return -ENOMEM;
1387 	}
1388 
1389 	wrk->anchor = 1;
1390 	wrk->func = &wear_leveling_worker;
1391 	schedule_ubi_work(ubi, wrk);
1392 	return 0;
1393 }
1394 #endif
1395 
1396 /**
1397  * erase_worker - physical eraseblock erase worker function.
1398  * @ubi: UBI device description object
1399  * @wl_wrk: the work object
1400  * @cancel: non-zero if the worker has to free memory and exit
1401  *
1402  * This function erases a physical eraseblock and perform torture testing if
1403  * needed. It also takes care about marking the physical eraseblock bad if
1404  * needed. Returns zero in case of success and a negative error code in case of
1405  * failure.
1406  */
1407 static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1408 			int cancel)
1409 {
1410 	struct ubi_wl_entry *e = wl_wrk->e;
1411 	int pnum = e->pnum;
1412 	int vol_id = wl_wrk->vol_id;
1413 	int lnum = wl_wrk->lnum;
1414 	int err, available_consumed = 0;
1415 
1416 	if (cancel) {
1417 		dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1418 		kfree(wl_wrk);
1419 		kmem_cache_free(ubi_wl_entry_slab, e);
1420 		return 0;
1421 	}
1422 
1423 	dbg_wl("erase PEB %d EC %d LEB %d:%d",
1424 	       pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1425 
1426 	ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1427 
1428 	err = sync_erase(ubi, e, wl_wrk->torture);
1429 	if (!err) {
1430 		/* Fine, we've erased it successfully */
1431 		kfree(wl_wrk);
1432 
1433 		spin_lock(&ubi->wl_lock);
1434 		wl_tree_add(e, &ubi->free);
1435 		ubi->free_count++;
1436 		spin_unlock(&ubi->wl_lock);
1437 
1438 		/*
1439 		 * One more erase operation has happened, take care about
1440 		 * protected physical eraseblocks.
1441 		 */
1442 		serve_prot_queue(ubi);
1443 
1444 		/* And take care about wear-leveling */
1445 		err = ensure_wear_leveling(ubi, 1);
1446 		return err;
1447 	}
1448 
1449 	ubi_err("failed to erase PEB %d, error %d", pnum, err);
1450 	kfree(wl_wrk);
1451 
1452 	if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1453 	    err == -EBUSY) {
1454 		int err1;
1455 
1456 		/* Re-schedule the LEB for erasure */
1457 		err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
1458 		if (err1) {
1459 			err = err1;
1460 			goto out_ro;
1461 		}
1462 		return err;
1463 	}
1464 
1465 	kmem_cache_free(ubi_wl_entry_slab, e);
1466 	if (err != -EIO)
1467 		/*
1468 		 * If this is not %-EIO, we have no idea what to do. Scheduling
1469 		 * this physical eraseblock for erasure again would cause
1470 		 * errors again and again. Well, lets switch to R/O mode.
1471 		 */
1472 		goto out_ro;
1473 
1474 	/* It is %-EIO, the PEB went bad */
1475 
1476 	if (!ubi->bad_allowed) {
1477 		ubi_err("bad physical eraseblock %d detected", pnum);
1478 		goto out_ro;
1479 	}
1480 
1481 	spin_lock(&ubi->volumes_lock);
1482 	if (ubi->beb_rsvd_pebs == 0) {
1483 		if (ubi->avail_pebs == 0) {
1484 			spin_unlock(&ubi->volumes_lock);
1485 			ubi_err("no reserved/available physical eraseblocks");
1486 			goto out_ro;
1487 		}
1488 		ubi->avail_pebs -= 1;
1489 		available_consumed = 1;
1490 	}
1491 	spin_unlock(&ubi->volumes_lock);
1492 
1493 	ubi_msg("mark PEB %d as bad", pnum);
1494 	err = ubi_io_mark_bad(ubi, pnum);
1495 	if (err)
1496 		goto out_ro;
1497 
1498 	spin_lock(&ubi->volumes_lock);
1499 	if (ubi->beb_rsvd_pebs > 0) {
1500 		if (available_consumed) {
1501 			/*
1502 			 * The amount of reserved PEBs increased since we last
1503 			 * checked.
1504 			 */
1505 			ubi->avail_pebs += 1;
1506 			available_consumed = 0;
1507 		}
1508 		ubi->beb_rsvd_pebs -= 1;
1509 	}
1510 	ubi->bad_peb_count += 1;
1511 	ubi->good_peb_count -= 1;
1512 	ubi_calculate_reserved(ubi);
1513 	if (available_consumed)
1514 		ubi_warn("no PEBs in the reserved pool, used an available PEB");
1515 	else if (ubi->beb_rsvd_pebs)
1516 		ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1517 	else
1518 		ubi_warn("last PEB from the reserve was used");
1519 	spin_unlock(&ubi->volumes_lock);
1520 
1521 	return err;
1522 
1523 out_ro:
1524 	if (available_consumed) {
1525 		spin_lock(&ubi->volumes_lock);
1526 		ubi->avail_pebs += 1;
1527 		spin_unlock(&ubi->volumes_lock);
1528 	}
1529 	ubi_ro_mode(ubi);
1530 	return err;
1531 }
1532 
1533 /**
1534  * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1535  * @ubi: UBI device description object
1536  * @vol_id: the volume ID that last used this PEB
1537  * @lnum: the last used logical eraseblock number for the PEB
1538  * @pnum: physical eraseblock to return
1539  * @torture: if this physical eraseblock has to be tortured
1540  *
1541  * This function is called to return physical eraseblock @pnum to the pool of
1542  * free physical eraseblocks. The @torture flag has to be set if an I/O error
1543  * occurred to this @pnum and it has to be tested. This function returns zero
1544  * in case of success, and a negative error code in case of failure.
1545  */
1546 int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1547 		   int pnum, int torture)
1548 {
1549 	int err;
1550 	struct ubi_wl_entry *e;
1551 
1552 	dbg_wl("PEB %d", pnum);
1553 	ubi_assert(pnum >= 0);
1554 	ubi_assert(pnum < ubi->peb_count);
1555 
1556 retry:
1557 	spin_lock(&ubi->wl_lock);
1558 	e = ubi->lookuptbl[pnum];
1559 	if (e == ubi->move_from) {
1560 		/*
1561 		 * User is putting the physical eraseblock which was selected to
1562 		 * be moved. It will be scheduled for erasure in the
1563 		 * wear-leveling worker.
1564 		 */
1565 		dbg_wl("PEB %d is being moved, wait", pnum);
1566 		spin_unlock(&ubi->wl_lock);
1567 
1568 		/* Wait for the WL worker by taking the @ubi->move_mutex */
1569 		mutex_lock(&ubi->move_mutex);
1570 		mutex_unlock(&ubi->move_mutex);
1571 		goto retry;
1572 	} else if (e == ubi->move_to) {
1573 		/*
1574 		 * User is putting the physical eraseblock which was selected
1575 		 * as the target the data is moved to. It may happen if the EBA
1576 		 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1577 		 * but the WL sub-system has not put the PEB to the "used" tree
1578 		 * yet, but it is about to do this. So we just set a flag which
1579 		 * will tell the WL worker that the PEB is not needed anymore
1580 		 * and should be scheduled for erasure.
1581 		 */
1582 		dbg_wl("PEB %d is the target of data moving", pnum);
1583 		ubi_assert(!ubi->move_to_put);
1584 		ubi->move_to_put = 1;
1585 		spin_unlock(&ubi->wl_lock);
1586 		return 0;
1587 	} else {
1588 		if (in_wl_tree(e, &ubi->used)) {
1589 			self_check_in_wl_tree(ubi, e, &ubi->used);
1590 			rb_erase(&e->u.rb, &ubi->used);
1591 		} else if (in_wl_tree(e, &ubi->scrub)) {
1592 			self_check_in_wl_tree(ubi, e, &ubi->scrub);
1593 			rb_erase(&e->u.rb, &ubi->scrub);
1594 		} else if (in_wl_tree(e, &ubi->erroneous)) {
1595 			self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1596 			rb_erase(&e->u.rb, &ubi->erroneous);
1597 			ubi->erroneous_peb_count -= 1;
1598 			ubi_assert(ubi->erroneous_peb_count >= 0);
1599 			/* Erroneous PEBs should be tortured */
1600 			torture = 1;
1601 		} else {
1602 			err = prot_queue_del(ubi, e->pnum);
1603 			if (err) {
1604 				ubi_err("PEB %d not found", pnum);
1605 				ubi_ro_mode(ubi);
1606 				spin_unlock(&ubi->wl_lock);
1607 				return err;
1608 			}
1609 		}
1610 	}
1611 	spin_unlock(&ubi->wl_lock);
1612 
1613 	err = schedule_erase(ubi, e, vol_id, lnum, torture);
1614 	if (err) {
1615 		spin_lock(&ubi->wl_lock);
1616 		wl_tree_add(e, &ubi->used);
1617 		spin_unlock(&ubi->wl_lock);
1618 	}
1619 
1620 	return err;
1621 }
1622 
1623 /**
1624  * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1625  * @ubi: UBI device description object
1626  * @pnum: the physical eraseblock to schedule
1627  *
1628  * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1629  * needs scrubbing. This function schedules a physical eraseblock for
1630  * scrubbing which is done in background. This function returns zero in case of
1631  * success and a negative error code in case of failure.
1632  */
1633 int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1634 {
1635 	struct ubi_wl_entry *e;
1636 
1637 	ubi_msg("schedule PEB %d for scrubbing", pnum);
1638 
1639 retry:
1640 	spin_lock(&ubi->wl_lock);
1641 	e = ubi->lookuptbl[pnum];
1642 	if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1643 				   in_wl_tree(e, &ubi->erroneous)) {
1644 		spin_unlock(&ubi->wl_lock);
1645 		return 0;
1646 	}
1647 
1648 	if (e == ubi->move_to) {
1649 		/*
1650 		 * This physical eraseblock was used to move data to. The data
1651 		 * was moved but the PEB was not yet inserted to the proper
1652 		 * tree. We should just wait a little and let the WL worker
1653 		 * proceed.
1654 		 */
1655 		spin_unlock(&ubi->wl_lock);
1656 		dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1657 		yield();
1658 		goto retry;
1659 	}
1660 
1661 	if (in_wl_tree(e, &ubi->used)) {
1662 		self_check_in_wl_tree(ubi, e, &ubi->used);
1663 		rb_erase(&e->u.rb, &ubi->used);
1664 	} else {
1665 		int err;
1666 
1667 		err = prot_queue_del(ubi, e->pnum);
1668 		if (err) {
1669 			ubi_err("PEB %d not found", pnum);
1670 			ubi_ro_mode(ubi);
1671 			spin_unlock(&ubi->wl_lock);
1672 			return err;
1673 		}
1674 	}
1675 
1676 	wl_tree_add(e, &ubi->scrub);
1677 	spin_unlock(&ubi->wl_lock);
1678 
1679 	/*
1680 	 * Technically scrubbing is the same as wear-leveling, so it is done
1681 	 * by the WL worker.
1682 	 */
1683 	return ensure_wear_leveling(ubi, 0);
1684 }
1685 
1686 /**
1687  * ubi_wl_flush - flush all pending works.
1688  * @ubi: UBI device description object
1689  * @vol_id: the volume id to flush for
1690  * @lnum: the logical eraseblock number to flush for
1691  *
1692  * This function executes all pending works for a particular volume id /
1693  * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1694  * acts as a wildcard for all of the corresponding volume numbers or logical
1695  * eraseblock numbers. It returns zero in case of success and a negative error
1696  * code in case of failure.
1697  */
1698 int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
1699 {
1700 	int err = 0;
1701 	int found = 1;
1702 
1703 	/*
1704 	 * Erase while the pending works queue is not empty, but not more than
1705 	 * the number of currently pending works.
1706 	 */
1707 	dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1708 	       vol_id, lnum, ubi->works_count);
1709 
1710 	while (found) {
1711 		struct ubi_work *wrk;
1712 		found = 0;
1713 
1714 		down_read(&ubi->work_sem);
1715 		spin_lock(&ubi->wl_lock);
1716 		list_for_each_entry(wrk, &ubi->works, list) {
1717 			if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1718 			    (lnum == UBI_ALL || wrk->lnum == lnum)) {
1719 				list_del(&wrk->list);
1720 				ubi->works_count -= 1;
1721 				ubi_assert(ubi->works_count >= 0);
1722 				spin_unlock(&ubi->wl_lock);
1723 
1724 				err = wrk->func(ubi, wrk, 0);
1725 				if (err) {
1726 					up_read(&ubi->work_sem);
1727 					return err;
1728 				}
1729 
1730 				spin_lock(&ubi->wl_lock);
1731 				found = 1;
1732 				break;
1733 			}
1734 		}
1735 		spin_unlock(&ubi->wl_lock);
1736 		up_read(&ubi->work_sem);
1737 	}
1738 
1739 	/*
1740 	 * Make sure all the works which have been done in parallel are
1741 	 * finished.
1742 	 */
1743 	down_write(&ubi->work_sem);
1744 	up_write(&ubi->work_sem);
1745 
1746 	return err;
1747 }
1748 
1749 /**
1750  * tree_destroy - destroy an RB-tree.
1751  * @root: the root of the tree to destroy
1752  */
1753 static void tree_destroy(struct rb_root *root)
1754 {
1755 	struct rb_node *rb;
1756 	struct ubi_wl_entry *e;
1757 
1758 	rb = root->rb_node;
1759 	while (rb) {
1760 		if (rb->rb_left)
1761 			rb = rb->rb_left;
1762 		else if (rb->rb_right)
1763 			rb = rb->rb_right;
1764 		else {
1765 			e = rb_entry(rb, struct ubi_wl_entry, u.rb);
1766 
1767 			rb = rb_parent(rb);
1768 			if (rb) {
1769 				if (rb->rb_left == &e->u.rb)
1770 					rb->rb_left = NULL;
1771 				else
1772 					rb->rb_right = NULL;
1773 			}
1774 
1775 			kmem_cache_free(ubi_wl_entry_slab, e);
1776 		}
1777 	}
1778 }
1779 
1780 /**
1781  * ubi_thread - UBI background thread.
1782  * @u: the UBI device description object pointer
1783  */
1784 int ubi_thread(void *u)
1785 {
1786 	int failures = 0;
1787 	struct ubi_device *ubi = u;
1788 
1789 	ubi_msg("background thread \"%s\" started, PID %d",
1790 		ubi->bgt_name, task_pid_nr(current));
1791 
1792 	set_freezable();
1793 	for (;;) {
1794 		int err;
1795 
1796 		if (kthread_should_stop())
1797 			break;
1798 
1799 		if (try_to_freeze())
1800 			continue;
1801 
1802 		spin_lock(&ubi->wl_lock);
1803 		if (list_empty(&ubi->works) || ubi->ro_mode ||
1804 		    !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
1805 			set_current_state(TASK_INTERRUPTIBLE);
1806 			spin_unlock(&ubi->wl_lock);
1807 			schedule();
1808 			continue;
1809 		}
1810 		spin_unlock(&ubi->wl_lock);
1811 
1812 		err = do_work(ubi);
1813 		if (err) {
1814 			ubi_err("%s: work failed with error code %d",
1815 				ubi->bgt_name, err);
1816 			if (failures++ > WL_MAX_FAILURES) {
1817 				/*
1818 				 * Too many failures, disable the thread and
1819 				 * switch to read-only mode.
1820 				 */
1821 				ubi_msg("%s: %d consecutive failures",
1822 					ubi->bgt_name, WL_MAX_FAILURES);
1823 				ubi_ro_mode(ubi);
1824 				ubi->thread_enabled = 0;
1825 				continue;
1826 			}
1827 		} else
1828 			failures = 0;
1829 
1830 		cond_resched();
1831 	}
1832 
1833 	dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1834 	return 0;
1835 }
1836 
1837 /**
1838  * cancel_pending - cancel all pending works.
1839  * @ubi: UBI device description object
1840  */
1841 static void cancel_pending(struct ubi_device *ubi)
1842 {
1843 	while (!list_empty(&ubi->works)) {
1844 		struct ubi_work *wrk;
1845 
1846 		wrk = list_entry(ubi->works.next, struct ubi_work, list);
1847 		list_del(&wrk->list);
1848 		wrk->func(ubi, wrk, 1);
1849 		ubi->works_count -= 1;
1850 		ubi_assert(ubi->works_count >= 0);
1851 	}
1852 }
1853 
1854 /**
1855  * ubi_wl_init - initialize the WL sub-system using attaching information.
1856  * @ubi: UBI device description object
1857  * @ai: attaching information
1858  *
1859  * This function returns zero in case of success, and a negative error code in
1860  * case of failure.
1861  */
1862 int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
1863 {
1864 	int err, i, reserved_pebs, found_pebs = 0;
1865 	struct rb_node *rb1, *rb2;
1866 	struct ubi_ainf_volume *av;
1867 	struct ubi_ainf_peb *aeb, *tmp;
1868 	struct ubi_wl_entry *e;
1869 
1870 	ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
1871 	spin_lock_init(&ubi->wl_lock);
1872 	mutex_init(&ubi->move_mutex);
1873 	init_rwsem(&ubi->work_sem);
1874 	ubi->max_ec = ai->max_ec;
1875 	INIT_LIST_HEAD(&ubi->works);
1876 #ifdef CONFIG_MTD_UBI_FASTMAP
1877 	INIT_WORK(&ubi->fm_work, update_fastmap_work_fn);
1878 #endif
1879 
1880 	sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1881 
1882 	err = -ENOMEM;
1883 	ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1884 	if (!ubi->lookuptbl)
1885 		return err;
1886 
1887 	for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1888 		INIT_LIST_HEAD(&ubi->pq[i]);
1889 	ubi->pq_head = 0;
1890 
1891 	list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
1892 		cond_resched();
1893 
1894 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1895 		if (!e)
1896 			goto out_free;
1897 
1898 		e->pnum = aeb->pnum;
1899 		e->ec = aeb->ec;
1900 		ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1901 		ubi->lookuptbl[e->pnum] = e;
1902 		if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
1903 			kmem_cache_free(ubi_wl_entry_slab, e);
1904 			goto out_free;
1905 		}
1906 
1907 		found_pebs++;
1908 	}
1909 
1910 	ubi->free_count = 0;
1911 	list_for_each_entry(aeb, &ai->free, u.list) {
1912 		cond_resched();
1913 
1914 		e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1915 		if (!e)
1916 			goto out_free;
1917 
1918 		e->pnum = aeb->pnum;
1919 		e->ec = aeb->ec;
1920 		ubi_assert(e->ec >= 0);
1921 		ubi_assert(!ubi_is_fm_block(ubi, e->pnum));
1922 
1923 		wl_tree_add(e, &ubi->free);
1924 		ubi->free_count++;
1925 
1926 		ubi->lookuptbl[e->pnum] = e;
1927 
1928 		found_pebs++;
1929 	}
1930 
1931 	ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1932 		ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
1933 			cond_resched();
1934 
1935 			e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1936 			if (!e)
1937 				goto out_free;
1938 
1939 			e->pnum = aeb->pnum;
1940 			e->ec = aeb->ec;
1941 			ubi->lookuptbl[e->pnum] = e;
1942 
1943 			if (!aeb->scrub) {
1944 				dbg_wl("add PEB %d EC %d to the used tree",
1945 				       e->pnum, e->ec);
1946 				wl_tree_add(e, &ubi->used);
1947 			} else {
1948 				dbg_wl("add PEB %d EC %d to the scrub tree",
1949 				       e->pnum, e->ec);
1950 				wl_tree_add(e, &ubi->scrub);
1951 			}
1952 
1953 			found_pebs++;
1954 		}
1955 	}
1956 
1957 	dbg_wl("found %i PEBs", found_pebs);
1958 
1959 	if (ubi->fm)
1960 		ubi_assert(ubi->good_peb_count == \
1961 			   found_pebs + ubi->fm->used_blocks);
1962 	else
1963 		ubi_assert(ubi->good_peb_count == found_pebs);
1964 
1965 	reserved_pebs = WL_RESERVED_PEBS;
1966 #ifdef CONFIG_MTD_UBI_FASTMAP
1967 	/* Reserve enough LEBs to store two fastmaps. */
1968 	reserved_pebs += (ubi->fm_size / ubi->leb_size) * 2;
1969 #endif
1970 
1971 	if (ubi->avail_pebs < reserved_pebs) {
1972 		ubi_err("no enough physical eraseblocks (%d, need %d)",
1973 			ubi->avail_pebs, reserved_pebs);
1974 		if (ubi->corr_peb_count)
1975 			ubi_err("%d PEBs are corrupted and not used",
1976 				ubi->corr_peb_count);
1977 		goto out_free;
1978 	}
1979 	ubi->avail_pebs -= reserved_pebs;
1980 	ubi->rsvd_pebs += reserved_pebs;
1981 
1982 	/* Schedule wear-leveling if needed */
1983 	err = ensure_wear_leveling(ubi, 0);
1984 	if (err)
1985 		goto out_free;
1986 
1987 	return 0;
1988 
1989 out_free:
1990 	cancel_pending(ubi);
1991 	tree_destroy(&ubi->used);
1992 	tree_destroy(&ubi->free);
1993 	tree_destroy(&ubi->scrub);
1994 	kfree(ubi->lookuptbl);
1995 	return err;
1996 }
1997 
1998 /**
1999  * protection_queue_destroy - destroy the protection queue.
2000  * @ubi: UBI device description object
2001  */
2002 static void protection_queue_destroy(struct ubi_device *ubi)
2003 {
2004 	int i;
2005 	struct ubi_wl_entry *e, *tmp;
2006 
2007 	for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
2008 		list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
2009 			list_del(&e->u.list);
2010 			kmem_cache_free(ubi_wl_entry_slab, e);
2011 		}
2012 	}
2013 }
2014 
2015 /**
2016  * ubi_wl_close - close the wear-leveling sub-system.
2017  * @ubi: UBI device description object
2018  */
2019 void ubi_wl_close(struct ubi_device *ubi)
2020 {
2021 	dbg_wl("close the WL sub-system");
2022 	cancel_pending(ubi);
2023 	protection_queue_destroy(ubi);
2024 	tree_destroy(&ubi->used);
2025 	tree_destroy(&ubi->erroneous);
2026 	tree_destroy(&ubi->free);
2027 	tree_destroy(&ubi->scrub);
2028 	kfree(ubi->lookuptbl);
2029 }
2030 
2031 /**
2032  * self_check_ec - make sure that the erase counter of a PEB is correct.
2033  * @ubi: UBI device description object
2034  * @pnum: the physical eraseblock number to check
2035  * @ec: the erase counter to check
2036  *
2037  * This function returns zero if the erase counter of physical eraseblock @pnum
2038  * is equivalent to @ec, and a negative error code if not or if an error
2039  * occurred.
2040  */
2041 static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
2042 {
2043 	int err;
2044 	long long read_ec;
2045 	struct ubi_ec_hdr *ec_hdr;
2046 
2047 	if (!ubi->dbg->chk_gen)
2048 		return 0;
2049 
2050 	ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
2051 	if (!ec_hdr)
2052 		return -ENOMEM;
2053 
2054 	err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
2055 	if (err && err != UBI_IO_BITFLIPS) {
2056 		/* The header does not have to exist */
2057 		err = 0;
2058 		goto out_free;
2059 	}
2060 
2061 	read_ec = be64_to_cpu(ec_hdr->ec);
2062 	if (ec != read_ec && read_ec - ec > 1) {
2063 		ubi_err("self-check failed for PEB %d", pnum);
2064 		ubi_err("read EC is %lld, should be %d", read_ec, ec);
2065 		dump_stack();
2066 		err = 1;
2067 	} else
2068 		err = 0;
2069 
2070 out_free:
2071 	kfree(ec_hdr);
2072 	return err;
2073 }
2074 
2075 /**
2076  * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
2077  * @ubi: UBI device description object
2078  * @e: the wear-leveling entry to check
2079  * @root: the root of the tree
2080  *
2081  * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
2082  * is not.
2083  */
2084 static int self_check_in_wl_tree(const struct ubi_device *ubi,
2085 				 struct ubi_wl_entry *e, struct rb_root *root)
2086 {
2087 	if (!ubi->dbg->chk_gen)
2088 		return 0;
2089 
2090 	if (in_wl_tree(e, root))
2091 		return 0;
2092 
2093 	ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
2094 		e->pnum, e->ec, root);
2095 	dump_stack();
2096 	return -EINVAL;
2097 }
2098 
2099 /**
2100  * self_check_in_pq - check if wear-leveling entry is in the protection
2101  *                        queue.
2102  * @ubi: UBI device description object
2103  * @e: the wear-leveling entry to check
2104  *
2105  * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2106  */
2107 static int self_check_in_pq(const struct ubi_device *ubi,
2108 			    struct ubi_wl_entry *e)
2109 {
2110 	struct ubi_wl_entry *p;
2111 	int i;
2112 
2113 	if (!ubi->dbg->chk_gen)
2114 		return 0;
2115 
2116 	for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
2117 		list_for_each_entry(p, &ubi->pq[i], u.list)
2118 			if (p == e)
2119 				return 0;
2120 
2121 	ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
2122 		e->pnum, e->ec);
2123 	dump_stack();
2124 	return -EINVAL;
2125 }
2126