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