2 * Copyright (c) International Business Machines Corp., 2006
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
22 * UBI wear-leveling sub-system.
24 * This sub-system is responsible for wear-leveling. It works in terms of
25 * physical eraseblocks and erase counters and knows nothing about logical
26 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
27 * eraseblocks are of two types - used and free. Used physical eraseblocks are
28 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
29 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
31 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
32 * header. The rest of the physical eraseblock contains only %0xFF bytes.
34 * When physical eraseblocks are returned to the WL sub-system by means of the
35 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
36 * done asynchronously in context of the per-UBI device background thread,
37 * which is also managed by the WL sub-system.
39 * The wear-leveling is ensured by means of moving the contents of used
40 * physical eraseblocks with low erase counter to free physical eraseblocks
41 * with high erase counter.
43 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
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.
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.
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).
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;
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.
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.
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).
84 * Depending on the sub-state, wear-leveling entries of the used physical
85 * eraseblocks may be kept in one of those structures.
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.
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.
101 #include <linux/slab.h>
102 #include <linux/crc32.h>
103 #include <linux/freezer.h>
104 #include <linux/kthread.h>
107 /* Number of physical eraseblocks reserved for wear-leveling purposes */
108 #define WL_RESERVED_PEBS 1
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
116 #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
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.
129 #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
132 * Maximum number of consecutive background thread failures which is enough to
133 * switch to read-only mode.
135 #define WL_MAX_FAILURES 32
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
);
143 #ifdef CONFIG_MTD_UBI_FASTMAP
145 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
146 * @wrk: the work description object
148 static void update_fastmap_work_fn(struct work_struct
*wrk
)
150 struct ubi_device
*ubi
= container_of(wrk
, struct ubi_device
, fm_work
);
151 ubi_update_fastmap(ubi
);
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
159 static int ubi_is_fm_block(struct ubi_device
*ubi
, int pnum
)
166 for (i
= 0; i
< ubi
->fm
->used_blocks
; i
++)
167 if (ubi
->fm
->e
[i
]->pnum
== pnum
)
173 static int ubi_is_fm_block(struct ubi_device
*ubi
, int pnum
)
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
184 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
185 * the @ubi->used and @ubi->free RB-trees.
187 static void wl_tree_add(struct ubi_wl_entry
*e
, struct rb_root
*root
)
189 struct rb_node
**p
, *parent
= NULL
;
193 struct ubi_wl_entry
*e1
;
196 e1
= rb_entry(parent
, struct ubi_wl_entry
, u
.rb
);
200 else if (e
->ec
> e1
->ec
)
203 ubi_assert(e
->pnum
!= e1
->pnum
);
204 if (e
->pnum
< e1
->pnum
)
211 rb_link_node(&e
->u
.rb
, parent
, p
);
212 rb_insert_color(&e
->u
.rb
, root
);
216 * do_work - do one pending work.
217 * @ubi: UBI device description object
219 * This function returns zero in case of success and a negative error code in
222 static int do_work(struct ubi_device
*ubi
)
225 struct ubi_work
*wrk
;
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.
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
);
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
);
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.
254 err
= wrk
->func(ubi
, wrk
, 0);
256 ubi_err("work failed with error code %d", err
);
257 up_read(&ubi
->work_sem
);
263 * produce_free_peb - produce a free physical eraseblock.
264 * @ubi: UBI device description object
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
271 static int produce_free_peb(struct ubi_device
*ubi
)
275 while (!ubi
->free
.rb_node
) {
276 if (ubi
->works_count
== 0) {
277 ubi_err("no free eraseblocks");
280 spin_unlock(&ubi
->wl_lock
);
282 dbg_wl("do one work synchronously");
285 spin_lock(&ubi
->wl_lock
);
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
298 * This function returns non-zero if @e is in the @root RB-tree and zero if it
301 static int in_wl_tree(struct ubi_wl_entry
*e
, struct rb_root
*root
)
307 struct ubi_wl_entry
*e1
;
309 e1
= rb_entry(p
, struct ubi_wl_entry
, u
.rb
);
311 if (e
->pnum
== e1
->pnum
) {
318 else if (e
->ec
> e1
->ec
)
321 ubi_assert(e
->pnum
!= e1
->pnum
);
322 if (e
->pnum
< e1
->pnum
)
333 * prot_queue_add - add physical eraseblock to the protection queue.
334 * @ubi: UBI device description object
335 * @e: the physical eraseblock to add
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
342 static void prot_queue_add(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
)
344 int pq_tail
= ubi
->pq_head
- 1;
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
);
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
359 * This function looks for a wear leveling entry with erase counter closest to
360 * min + @diff, where min is the smallest erase counter.
362 static struct ubi_wl_entry
*find_wl_entry(struct ubi_device
*ubi
,
363 struct rb_root
*root
, int diff
)
366 struct ubi_wl_entry
*e
, *prev_e
= NULL
;
369 e
= rb_entry(rb_first(root
), struct ubi_wl_entry
, u
.rb
);
374 struct ubi_wl_entry
*e1
;
376 e1
= rb_entry(p
, struct ubi_wl_entry
, u
.rb
);
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
)
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
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.
405 static struct ubi_wl_entry
*find_mean_wl_entry(struct ubi_device
*ubi
,
406 struct rb_root
*root
)
408 struct ubi_wl_entry
*e
, *first
, *last
;
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
);
413 if (last
->ec
- first
->ec
< ubi
->wl_th
*2) {
414 e
= rb_entry(root
->rb_node
, struct ubi_wl_entry
, u
.rb
);
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
);
426 e
= find_wl_entry(ubi
, root
, ubi
->wl_th
);
431 #ifdef CONFIG_MTD_UBI_FASTMAP
433 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
434 * @root: the RB-tree where to look for
436 static struct ubi_wl_entry
*find_anchor_wl_entry(struct rb_root
*root
)
439 struct ubi_wl_entry
*e
, *victim
= NULL
;
440 int max_ec
= UBI_MAX_ERASECOUNTER
;
442 ubi_rb_for_each_entry(p
, e
, root
, u
.rb
) {
443 if (e
->pnum
< UBI_FM_MAX_START
&& e
->ec
< max_ec
) {
452 static int anchor_pebs_avalible(struct rb_root
*root
)
455 struct ubi_wl_entry
*e
;
457 ubi_rb_for_each_entry(p
, e
, root
, u
.rb
)
458 if (e
->pnum
< UBI_FM_MAX_START
)
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
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!
473 struct ubi_wl_entry
*ubi_wl_get_fm_peb(struct ubi_device
*ubi
, int anchor
)
475 struct ubi_wl_entry
*e
= NULL
;
477 if (!ubi
->free
.rb_node
|| (ubi
->free_count
- ubi
->beb_rsvd_pebs
< 1))
481 e
= find_anchor_wl_entry(&ubi
->free
);
483 e
= find_mean_wl_entry(ubi
, &ubi
->free
);
488 self_check_in_wl_tree(ubi
, e
, &ubi
->free
);
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
);
500 * __wl_get_peb - get a physical eraseblock.
501 * @ubi: UBI device description object
503 * This function returns a physical eraseblock in case of success and a
504 * negative error code in case of failure.
506 static int __wl_get_peb(struct ubi_device
*ubi
)
509 struct ubi_wl_entry
*e
;
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
));
519 err
= produce_free_peb(ubi
);
525 e
= find_mean_wl_entry(ubi
, &ubi
->free
);
527 ubi_err("no free eraseblocks");
531 self_check_in_wl_tree(ubi
, e
, &ubi
->free
);
534 * Move the physical eraseblock to the protection queue where it will
535 * be protected from being moved for some time.
537 rb_erase(&e
->u
.rb
, &ubi
->free
);
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
);
549 #ifdef CONFIG_MTD_UBI_FASTMAP
551 * return_unused_pool_pebs - returns unused PEB to the free tree.
552 * @ubi: UBI device description object
553 * @pool: fastmap pool description object
555 static void return_unused_pool_pebs(struct ubi_device
*ubi
,
556 struct ubi_fm_pool
*pool
)
559 struct ubi_wl_entry
*e
;
561 for (i
= pool
->used
; i
< pool
->size
; i
++) {
562 e
= ubi
->lookuptbl
[pool
->pebs
[i
]];
563 wl_tree_add(e
, &ubi
->free
);
569 * refill_wl_pool - refills all the fastmap pool used by the
571 * @ubi: UBI device description object
573 static void refill_wl_pool(struct ubi_device
*ubi
)
575 struct ubi_wl_entry
*e
;
576 struct ubi_fm_pool
*pool
= &ubi
->fm_wl_pool
;
578 return_unused_pool_pebs(ubi
, pool
);
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))
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
);
591 pool
->pebs
[pool
->size
] = e
->pnum
;
596 * refill_wl_user_pool - refills all the fastmap pool used by ubi_wl_get_peb.
597 * @ubi: UBI device description object
599 static void refill_wl_user_pool(struct ubi_device
*ubi
)
601 struct ubi_fm_pool
*pool
= &ubi
->fm_pool
;
603 return_unused_pool_pebs(ubi
, pool
);
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)
611 #ifdef MTK_TMP_DEBUG_LOG
612 if(pool
->size
== 0) {
613 if (!ubi
->free
.rb_node
) {
614 ubi_err("Free list is empty");
616 ubi_err("Free count %d", ubi
->free_count
);
618 ubi_err("work count %d", ubi
->works_count
);
624 * ubi_refill_pools - refills all fastmap PEB pools.
625 * @ubi: UBI device description object
627 void ubi_refill_pools(struct ubi_device
*ubi
)
629 spin_lock(&ubi
->wl_lock
);
631 refill_wl_user_pool(ubi
);
632 spin_unlock(&ubi
->wl_lock
);
635 /* ubi_wl_get_peb - works exaclty like __wl_get_peb but keeps track of
638 int ubi_wl_get_peb(struct ubi_device
*ubi
)
641 struct ubi_fm_pool
*pool
= &ubi
->fm_pool
;
642 struct ubi_fm_pool
*wl_pool
= &ubi
->fm_wl_pool
;
644 if (!pool
->size
|| !wl_pool
->size
|| pool
->used
== pool
->size
||
645 wl_pool
->used
== wl_pool
->size
)
646 ubi_update_fastmap(ubi
);
648 /* we got not a single free PEB */
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
);
661 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
663 * @ubi: UBI device description object
665 static struct ubi_wl_entry
*get_peb_for_wl(struct ubi_device
*ubi
)
667 struct ubi_fm_pool
*pool
= &ubi
->fm_wl_pool
;
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
);
677 pnum
= pool
->pebs
[pool
->used
++];
678 return ubi
->lookuptbl
[pnum
];
682 static struct ubi_wl_entry
*get_peb_for_wl(struct ubi_device
*ubi
)
684 struct ubi_wl_entry
*e
;
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
);
693 int ubi_wl_get_peb(struct ubi_device
*ubi
)
697 spin_lock(&ubi
->wl_lock
);
698 peb
= __wl_get_peb(ubi
);
699 spin_unlock(&ubi
->wl_lock
);
701 err
= ubi_self_check_all_ff(ubi
, peb
, ubi
->vid_hdr_aloffset
,
702 ubi
->peb_size
- ubi
->vid_hdr_aloffset
);
704 ubi_err("new PEB %d does not contain all 0xFF bytes", peb
);
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
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.
720 static int prot_queue_del(struct ubi_device
*ubi
, int pnum
)
722 struct ubi_wl_entry
*e
;
724 e
= ubi
->lookuptbl
[pnum
];
728 if (self_check_in_pq(ubi
, e
))
731 list_del(&e
->u
.list
);
732 dbg_wl("deleted PEB %d from the protection queue", e
->pnum
);
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
742 * This function returns zero in case of success and a negative error code in
745 int sync_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
749 struct ubi_ec_hdr
*ec_hdr
;
750 unsigned long long old_ec
= e
->ec
, ec
= e
->ec
; //MTK: old_ec
752 dbg_wl("erase PEB %d, old EC %llu", e
->pnum
, ec
);
754 err
= self_check_ec(ubi
, e
->pnum
, e
->ec
);
758 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_NOFS
);
762 err
= ubi_io_sync_erase(ubi
, e
->pnum
, torture
);
767 if (ec
> UBI_MAX_ERASECOUNTER
) {
769 * Erase counter overflow. Upgrade UBI and use 64-bit
770 * erase counters internally.
772 ubi_err("erase counter overflow at PEB %d, EC %llu",
778 dbg_wl("erased PEB %d, new EC %llu", e
->pnum
, ec
);
780 ec_hdr
->ec
= cpu_to_be64(ec
);
782 err
= ubi_io_write_ec_hdr(ubi
, e
->pnum
, ec_hdr
);
787 spin_lock(&ubi
->wl_lock
);
788 if (e
->ec
> ubi
->max_ec
)
790 //MTK start: the incresing of ec > 1 is doing by torture
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
);
796 spin_unlock(&ubi
->wl_lock
);
804 * serve_prot_queue - check if it is time to stop protecting PEBs.
805 * @ubi: UBI device description object
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.
811 static void serve_prot_queue(struct ubi_device
*ubi
)
813 struct ubi_wl_entry
*e
, *tmp
;
817 * There may be several protected physical eraseblock to remove,
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",
827 list_del(&e
->u
.list
);
828 wl_tree_add(e
, &ubi
->used
);
831 * Let's be nice and avoid holding the spinlock for
834 spin_unlock(&ubi
->wl_lock
);
841 if (ubi
->pq_head
== UBI_PROT_QUEUE_LEN
)
843 ubi_assert(ubi
->pq_head
>= 0 && ubi
->pq_head
< UBI_PROT_QUEUE_LEN
);
844 spin_unlock(&ubi
->wl_lock
);
848 * __schedule_ubi_work - schedule a work.
849 * @ubi: UBI device description object
850 * @wrk: the work to schedule
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!
855 static void __schedule_ubi_work(struct ubi_device
*ubi
, struct ubi_work
*wrk
)
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
);
867 * schedule_ubi_work - schedule a work.
868 * @ubi: UBI device description object
869 * @wrk: the work to schedule
871 * This function adds a work defined by @wrk to the tail of the pending works
874 static void schedule_ubi_work(struct ubi_device
*ubi
, struct ubi_work
*wrk
)
876 down_read(&ubi
->work_sem
);
877 __schedule_ubi_work(ubi
, wrk
);
878 up_read(&ubi
->work_sem
);
881 static int erase_worker(struct ubi_device
*ubi
, struct ubi_work
*wl_wrk
,
884 #ifdef CONFIG_MTD_UBI_FASTMAP
886 * ubi_is_erase_work - checks whether a work is erase work.
887 * @wrk: The work object to be checked
889 int ubi_is_erase_work(struct ubi_work
*wrk
)
891 return wrk
->func
== erase_worker
;
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
903 * This function returns zero in case of success and a %-ENOMEM in case of
906 static int schedule_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
907 int vol_id
, int lnum
, int torture
)
909 struct ubi_work
*wl_wrk
;
912 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
914 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
915 e
->pnum
, e
->ec
, torture
);
917 wl_wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
921 wl_wrk
->func
= &erase_worker
;
923 wl_wrk
->vol_id
= vol_id
;
925 wl_wrk
->torture
= torture
;
927 schedule_ubi_work(ubi
, wl_wrk
);
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
940 static int do_sync_erase(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
941 int vol_id
, int lnum
, int torture
)
943 struct ubi_work
*wl_wrk
;
945 dbg_wl("sync erase of PEB %i", e
->pnum
);
947 wl_wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
952 wl_wrk
->vol_id
= vol_id
;
954 wl_wrk
->torture
= torture
;
956 return erase_worker(ubi
, wl_wrk
, 0);
959 #ifdef CONFIG_MTD_UBI_FASTMAP
961 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
963 * see: ubi_wl_put_peb()
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
970 int ubi_wl_put_fm_peb(struct ubi_device
*ubi
, struct ubi_wl_entry
*fm_e
,
971 int lnum
, int torture
)
973 struct ubi_wl_entry
*e
;
974 int vol_id
, pnum
= fm_e
->pnum
;
976 dbg_wl("PEB %d", pnum
);
978 ubi_assert(pnum
>= 0);
979 ubi_assert(pnum
< ubi
->peb_count
);
981 spin_lock(&ubi
->wl_lock
);
982 e
= ubi
->lookuptbl
[pnum
];
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.
990 ubi_assert(e
->ec
>= 0);
991 ubi
->lookuptbl
[pnum
] = e
;
997 spin_unlock(&ubi
->wl_lock
);
999 vol_id
= lnum
? UBI_FM_DATA_VOLUME_ID
: UBI_FM_SB_VOLUME_ID
;
1000 return schedule_erase(ubi
, e
, vol_id
, lnum
, torture
);
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
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
1014 static int wear_leveling_worker(struct ubi_device
*ubi
, struct ubi_work
*wrk
,
1017 int erase_e2
=1, err
, scrubbing
= 0, torture
= 0, protect
= 0, erroneous
= 0;
1018 int vol_id
= -1, uninitialized_var(lnum
);
1019 #ifdef CONFIG_MTD_UBI_FASTMAP
1020 int anchor
= wrk
->anchor
;
1022 struct ubi_wl_entry
*e1
, *e2
;
1023 struct ubi_vid_hdr
*vid_hdr
;
1024 int do_wl
= 0; //wl or not, 1 for wl, 2 for scrubbing
1030 vid_hdr
= ubi_zalloc_vid_hdr(ubi
, GFP_NOFS
);
1034 mutex_lock(&ubi
->move_mutex
);
1035 spin_lock(&ubi
->wl_lock
);
1036 ubi_assert(!ubi
->move_from
&& !ubi
->move_to
);
1037 ubi_assert(!ubi
->move_to_put
);
1039 if (!ubi
->free
.rb_node
||
1040 (!ubi
->used
.rb_node
&& !ubi
->scrub
.rb_node
)) {
1042 * No free physical eraseblocks? Well, they must be waiting in
1043 * the queue to be erased. Cancel movement - it will be
1044 * triggered again when a free physical eraseblock appears.
1046 * No used physical eraseblocks? They must be temporarily
1047 * protected from being moved. They will be moved to the
1048 * @ubi->used tree later and the wear-leveling will be
1051 dbg_wl("cancel WL, a list is empty: free %d, used %d",
1052 !ubi
->free
.rb_node
, !ubi
->used
.rb_node
);
1056 #ifdef CONFIG_MTD_UBI_FASTMAP
1057 /* Check whether we need to produce an anchor PEB */
1059 anchor
= !anchor_pebs_avalible(&ubi
->free
);
1062 e1
= find_anchor_wl_entry(&ubi
->used
);
1065 e2
= get_peb_for_wl(ubi
);
1069 self_check_in_wl_tree(ubi
, e1
, &ubi
->used
);
1070 rb_erase(&e1
->u
.rb
, &ubi
->used
);
1071 dbg_wl("anchor-move PEB %d to PEB %d", e1
->pnum
, e2
->pnum
);
1072 } else if (!ubi
->scrub
.rb_node
) {
1074 if (!ubi
->scrub
.rb_node
) {
1077 * Now pick the least worn-out used physical eraseblock and a
1078 * highly worn-out free physical eraseblock. If the erase
1079 * counters differ much enough, start wear-leveling.
1081 e1
= rb_entry(rb_first(&ubi
->used
), struct ubi_wl_entry
, u
.rb
);
1082 e2
= get_peb_for_wl(ubi
);
1086 if (!(e2
->ec
- e1
->ec
>= ubi
->wl_th
)) {
1087 dbg_wl("no WL needed: min used EC %d, max free EC %d",
1090 /* Give the unused PEB back */
1091 wl_tree_add(e2
, &ubi
->free
);
1094 self_check_in_wl_tree(ubi
, e1
, &ubi
->used
);
1095 rb_erase(&e1
->u
.rb
, &ubi
->used
);
1096 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
1097 e1
->pnum
, e1
->ec
, e2
->pnum
, e2
->ec
);
1100 /* Perform scrubbing */
1102 e1
= rb_entry(rb_first(&ubi
->scrub
), struct ubi_wl_entry
, u
.rb
);
1103 e2
= get_peb_for_wl(ubi
);
1107 self_check_in_wl_tree(ubi
, e1
, &ubi
->scrub
);
1108 rb_erase(&e1
->u
.rb
, &ubi
->scrub
);
1109 dbg_wl("scrub PEB %d to PEB %d", e1
->pnum
, e2
->pnum
);
1113 ubi
->move_from
= e1
;
1115 spin_unlock(&ubi
->wl_lock
);
1118 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
1119 * We so far do not know which logical eraseblock our physical
1120 * eraseblock (@e1) belongs to. We have to read the volume identifier
1123 * Note, we are protected from this PEB being unmapped and erased. The
1124 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
1125 * which is being moved was unmapped.
1128 err
= ubi_io_read_vid_hdr(ubi
, e1
->pnum
, vid_hdr
, 0);
1129 if (err
&& err
!= UBI_IO_BITFLIPS
) {
1130 if (err
== UBI_IO_FF
) {
1132 * We are trying to move PEB without a VID header. UBI
1133 * always write VID headers shortly after the PEB was
1134 * given, so we have a situation when it has not yet
1135 * had a chance to write it, because it was preempted.
1136 * So add this PEB to the protection queue so far,
1137 * because presumably more data will be written there
1138 * (including the missing VID header), and then we'll
1141 dbg_wl("PEB %d has no VID header", e1
->pnum
);
1145 } else if (err
== UBI_IO_FF_BITFLIPS
) {
1147 * The same situation as %UBI_IO_FF, but bit-flips were
1148 * detected. It is better to schedule this PEB for
1151 dbg_wl("PEB %d has no VID header but has bit-flips",
1158 ubi_err("error %d while reading VID header from PEB %d",
1163 vol_id
= be32_to_cpu(vid_hdr
->vol_id
);
1164 lnum
= be32_to_cpu(vid_hdr
->lnum
);
1166 err
= ubi_eba_copy_leb(ubi
, e1
->pnum
, e2
->pnum
, vid_hdr
, do_wl
); //MTK: pass do_wl
1168 if (err
== MOVE_CANCEL_RACE
) {
1170 * The LEB has not been moved because the volume is
1171 * being deleted or the PEB has been put meanwhile. We
1172 * should prevent this PEB from being selected for
1173 * wear-leveling movement again, so put it to the
1180 if (err
== MOVE_RETRY
) {
1182 atomic_inc(&ubi
->move_retry
); //MTK
1186 if (err
== MOVE_TARGET_BITFLIPS
|| err
== MOVE_TARGET_WR_ERR
||
1187 err
== MOVE_TARGET_RD_ERR
) {
1189 * Target PEB had bit-flips or write error - torture it.
1195 if (err
== MOVE_SOURCE_RD_ERR
) {
1197 * An error happened while reading the source PEB. Do
1198 * not switch to R/O mode in this case, and give the
1199 * upper layers a possibility to recover from this,
1200 * e.g. by unmapping corresponding LEB. Instead, just
1201 * put this PEB to the @ubi->erroneous list to prevent
1202 * UBI from trying to move it over and over again.
1204 if (ubi
->erroneous_peb_count
> ubi
->max_erroneous
) {
1205 ubi_err("too many erroneous eraseblocks (%d)",
1206 ubi
->erroneous_peb_count
);
1219 /* The PEB has been successfully moved */
1221 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
1222 e1
->pnum
, vol_id
, lnum
, e2
->pnum
);
1223 ubi_free_vid_hdr(ubi
, vid_hdr
);
1225 spin_lock(&ubi
->wl_lock
);
1226 if (!ubi
->move_to_put
) {
1227 wl_tree_add(e2
, &ubi
->used
);
1230 ubi
->move_from
= ubi
->move_to
= NULL
;
1231 ubi
->move_to_put
= ubi
->wl_scheduled
= 0;
1232 spin_unlock(&ubi
->wl_lock
);
1234 err
= do_sync_erase(ubi
, e1
, vol_id
, lnum
, 0);
1236 kmem_cache_free(ubi_wl_entry_slab
, e1
);
1238 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1244 * Well, the target PEB was put meanwhile, schedule it for
1247 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
1248 e2
->pnum
, vol_id
, lnum
);
1249 err
= do_sync_erase(ubi
, e2
, vol_id
, lnum
, 0);
1251 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1257 mutex_unlock(&ubi
->move_mutex
);
1261 * For some reasons the LEB was not moved, might be an error, might be
1262 * something else. @e1 was not changed, so return it back. @e2 might
1263 * have been changed, schedule it for erasure.
1267 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
1268 e1
->pnum
, vol_id
, lnum
, e2
->pnum
, err
);
1270 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
1271 e1
->pnum
, e2
->pnum
, err
);
1272 spin_lock(&ubi
->wl_lock
);
1274 prot_queue_add(ubi
, e1
);
1275 else if (erroneous
) {
1276 wl_tree_add(e1
, &ubi
->erroneous
);
1277 ubi
->erroneous_peb_count
+= 1;
1278 } else if (scrubbing
)
1279 wl_tree_add(e1
, &ubi
->scrub
);
1281 wl_tree_add(e1
, &ubi
->used
);
1282 ubi_assert(!ubi
->move_to_put
);
1283 ubi
->move_from
= ubi
->move_to
= NULL
;
1284 ubi
->wl_scheduled
= 0;
1285 spin_unlock(&ubi
->wl_lock
);
1287 ubi_free_vid_hdr(ubi
, vid_hdr
);
1290 err
= do_sync_erase(ubi
, e2
, vol_id
, lnum
, torture
);
1292 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1296 spin_lock(&ubi
->wl_lock
);
1297 wl_tree_add(e2
, &ubi
->free
);
1298 spin_unlock(&ubi
->wl_lock
);
1301 mutex_unlock(&ubi
->move_mutex
);
1306 ubi_err("error %d while moving PEB %d to PEB %d",
1307 err
, e1
->pnum
, e2
->pnum
);
1309 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
1310 err
, e1
->pnum
, vol_id
, lnum
, e2
->pnum
);
1311 spin_lock(&ubi
->wl_lock
);
1312 ubi
->move_from
= ubi
->move_to
= NULL
;
1313 ubi
->move_to_put
= ubi
->wl_scheduled
= 0;
1314 spin_unlock(&ubi
->wl_lock
);
1316 ubi_free_vid_hdr(ubi
, vid_hdr
);
1317 kmem_cache_free(ubi_wl_entry_slab
, e1
);
1318 kmem_cache_free(ubi_wl_entry_slab
, e2
);
1322 mutex_unlock(&ubi
->move_mutex
);
1323 ubi_assert(err
!= 0);
1324 return err
< 0 ? err
: -EIO
;
1327 ubi
->wl_scheduled
= 0;
1328 spin_unlock(&ubi
->wl_lock
);
1329 mutex_unlock(&ubi
->move_mutex
);
1330 ubi_free_vid_hdr(ubi
, vid_hdr
);
1335 * ensure_wear_leveling - schedule wear-leveling if it is needed.
1336 * @ubi: UBI device description object
1337 * @nested: set to non-zero if this function is called from UBI worker
1339 * This function checks if it is time to start wear-leveling and schedules it
1340 * if yes. This function returns zero in case of success and a negative error
1341 * code in case of failure.
1343 static int ensure_wear_leveling(struct ubi_device
*ubi
, int nested
)
1346 struct ubi_wl_entry
*e1
;
1347 struct ubi_wl_entry
*e2
;
1348 struct ubi_work
*wrk
;
1350 spin_lock(&ubi
->wl_lock
);
1351 if (ubi
->wl_scheduled
)
1352 /* Wear-leveling is already in the work queue */
1356 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
1357 * the WL worker has to be scheduled anyway.
1359 if (!ubi
->scrub
.rb_node
) {
1360 if (!ubi
->used
.rb_node
|| !ubi
->free
.rb_node
)
1361 /* No physical eraseblocks - no deal */
1365 * We schedule wear-leveling only if the difference between the
1366 * lowest erase counter of used physical eraseblocks and a high
1367 * erase counter of free physical eraseblocks is greater than
1368 * %UBI_WL_THRESHOLD.
1370 e1
= rb_entry(rb_first(&ubi
->used
), struct ubi_wl_entry
, u
.rb
);
1371 e2
= find_wl_entry(ubi
, &ubi
->free
, ubi
->wl_th
*2);
1373 if (!(e2
->ec
- e1
->ec
>= ubi
->wl_th
))
1375 dbg_wl("schedule wear-leveling");
1377 dbg_wl("schedule scrubbing");
1379 ubi
->wl_scheduled
= 1;
1380 spin_unlock(&ubi
->wl_lock
);
1382 wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
1389 wrk
->func
= &wear_leveling_worker
;
1391 __schedule_ubi_work(ubi
, wrk
);
1393 schedule_ubi_work(ubi
, wrk
);
1397 spin_lock(&ubi
->wl_lock
);
1398 ubi
->wl_scheduled
= 0;
1400 spin_unlock(&ubi
->wl_lock
);
1404 #ifdef CONFIG_MTD_UBI_FASTMAP
1406 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
1407 * @ubi: UBI device description object
1409 int ubi_ensure_anchor_pebs(struct ubi_device
*ubi
)
1411 struct ubi_work
*wrk
;
1413 spin_lock(&ubi
->wl_lock
);
1414 if (ubi
->wl_scheduled
) {
1415 spin_unlock(&ubi
->wl_lock
);
1418 ubi
->wl_scheduled
= 1;
1419 spin_unlock(&ubi
->wl_lock
);
1421 wrk
= kmalloc(sizeof(struct ubi_work
), GFP_NOFS
);
1423 spin_lock(&ubi
->wl_lock
);
1424 ubi
->wl_scheduled
= 0;
1425 spin_unlock(&ubi
->wl_lock
);
1430 wrk
->func
= &wear_leveling_worker
;
1431 schedule_ubi_work(ubi
, wrk
);
1437 * erase_worker - physical eraseblock erase worker function.
1438 * @ubi: UBI device description object
1439 * @wl_wrk: the work object
1440 * @cancel: non-zero if the worker has to free memory and exit
1442 * This function erases a physical eraseblock and perform torture testing if
1443 * needed. It also takes care about marking the physical eraseblock bad if
1444 * needed. Returns zero in case of success and a negative error code in case of
1447 static int erase_worker(struct ubi_device
*ubi
, struct ubi_work
*wl_wrk
,
1450 struct ubi_wl_entry
*e
= wl_wrk
->e
;
1452 int vol_id
= wl_wrk
->vol_id
;
1453 int lnum
= wl_wrk
->lnum
;
1454 int err
, available_consumed
= 0;
1457 dbg_wl("cancel erasure of PEB %d EC %d", pnum
, e
->ec
);
1459 kmem_cache_free(ubi_wl_entry_slab
, e
);
1463 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1464 pnum
, e
->ec
, wl_wrk
->vol_id
, wl_wrk
->lnum
);
1466 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
1468 err
= sync_erase(ubi
, e
, wl_wrk
->torture
);
1470 /* Fine, we've erased it successfully */
1473 spin_lock(&ubi
->wl_lock
);
1474 wl_tree_add(e
, &ubi
->free
);
1476 spin_unlock(&ubi
->wl_lock
);
1479 * One more erase operation has happened, take care about
1480 * protected physical eraseblocks.
1482 serve_prot_queue(ubi
);
1484 /* And take care about wear-leveling */
1485 err
= ensure_wear_leveling(ubi
, 1);
1489 ubi_err("failed to erase PEB %d, error %d", pnum
, err
);
1492 if (err
== -EINTR
|| err
== -ENOMEM
|| err
== -EAGAIN
||
1496 /* Re-schedule the LEB for erasure */
1497 err1
= schedule_erase(ubi
, e
, vol_id
, lnum
, 0);
1505 kmem_cache_free(ubi_wl_entry_slab
, e
);
1508 * If this is not %-EIO, we have no idea what to do. Scheduling
1509 * this physical eraseblock for erasure again would cause
1510 * errors again and again. Well, lets switch to R/O mode.
1514 /* It is %-EIO, the PEB went bad */
1516 if (!ubi
->bad_allowed
) {
1517 ubi_err("bad physical eraseblock %d detected", pnum
);
1521 spin_lock(&ubi
->volumes_lock
);
1522 if (ubi
->beb_rsvd_pebs
== 0) {
1523 if (ubi
->avail_pebs
== 0) {
1524 spin_unlock(&ubi
->volumes_lock
);
1525 ubi_err("no reserved/available physical eraseblocks");
1528 ubi
->avail_pebs
-= 1;
1529 available_consumed
= 1;
1531 spin_unlock(&ubi
->volumes_lock
);
1533 ubi_msg("mark PEB %d as bad", pnum
);
1534 err
= ubi_io_mark_bad(ubi
, pnum
);
1538 spin_lock(&ubi
->volumes_lock
);
1539 if (ubi
->beb_rsvd_pebs
> 0) {
1540 if (available_consumed
) {
1542 * The amount of reserved PEBs increased since we last
1545 ubi
->avail_pebs
+= 1;
1546 available_consumed
= 0;
1548 ubi
->beb_rsvd_pebs
-= 1;
1550 ubi
->bad_peb_count
+= 1;
1551 ubi
->good_peb_count
-= 1;
1552 ubi_calculate_reserved(ubi
);
1553 if (available_consumed
)
1554 ubi_warn("no PEBs in the reserved pool, used an available PEB");
1555 else if (ubi
->beb_rsvd_pebs
)
1556 ubi_msg("%d PEBs left in the reserve", ubi
->beb_rsvd_pebs
);
1558 ubi_warn("last PEB from the reserve was used");
1559 spin_unlock(&ubi
->volumes_lock
);
1564 if (available_consumed
) {
1565 spin_lock(&ubi
->volumes_lock
);
1566 ubi
->avail_pebs
+= 1;
1567 spin_unlock(&ubi
->volumes_lock
);
1574 * ubifs_erase_peb - erase physical eraseblock for mtk.
1575 * @ubi: UBI device description object
1576 * @wl_wrk: the work object
1577 * @cancel: non-zero if the worker has to free memory and exit
1579 * This function erases a physical eraseblock and perform torture testing if
1580 * needed. It also takes care about marking the physical eraseblock bad if
1581 * needed. Returns zero in case of success and a negative error code in case of
1584 static int ubi_erase_peb(struct ubi_device
*ubi
, struct ubi_wl_entry
*e
,
1587 int pnum
= e
->pnum
, err
, need
;
1593 err
= sync_erase(ubi
, e
, torture
);
1595 /* Fine, we've erased it successfully */
1596 spin_lock(&ubi
->wl_lock
);
1597 wl_tree_add(e
, &ubi
->free
);
1599 spin_unlock(&ubi
->wl_lock
);
1602 * One more erase operation has happened, take care about
1603 * protected physical eraseblocks.
1605 serve_prot_queue(ubi
);
1607 /* And take care about wear-leveling */
1608 err
= ensure_wear_leveling(ubi
, 1);
1612 ubi_err("failed to erase PEB %d, error %d", pnum
, err
);
1614 if (err
== -EINTR
|| err
== -ENOMEM
|| err
== -EAGAIN
||
1623 kmem_cache_free(ubi_wl_entry_slab
, e
);
1626 * If this is not %-EIO, we have no idea what to do. Scheduling
1627 * this physical eraseblock for erasure again would cause
1628 * errors again and again. Well, lets switch to R/O mode.
1632 /* It is %-EIO, the PEB went bad */
1634 if (!ubi
->bad_allowed
) {
1635 ubi_err("bad physical eraseblock %d detected", pnum
);
1639 spin_lock(&ubi
->volumes_lock
);
1640 need
= ubi
->beb_rsvd_level
- ubi
->beb_rsvd_pebs
+ 1;
1642 need
= ubi
->avail_pebs
>= need
? need
: ubi
->avail_pebs
;
1643 ubi
->avail_pebs
-= need
;
1644 ubi
->rsvd_pebs
+= need
;
1645 ubi
->beb_rsvd_pebs
+= need
;
1647 ubi_msg("reserve more %d PEBs", need
);
1650 if (ubi
->beb_rsvd_pebs
== 0) {
1651 spin_unlock(&ubi
->volumes_lock
);
1652 ubi_err("no reserved physical eraseblocks");
1655 spin_unlock(&ubi
->volumes_lock
);
1657 ubi_msg("mark PEB %d as bad", pnum
);
1658 err
= ubi_io_mark_bad(ubi
, pnum
);
1662 spin_lock(&ubi
->volumes_lock
);
1663 ubi
->beb_rsvd_pebs
-= 1;
1664 ubi
->bad_peb_count
+= 1;
1665 ubi
->good_peb_count
-= 1;
1666 ubi_calculate_reserved(ubi
);
1667 if (ubi
->beb_rsvd_pebs
)
1668 ubi_msg("%d PEBs left in the reserve", ubi
->beb_rsvd_pebs
);
1670 ubi_warn("last PEB from the reserved pool was used");
1671 spin_unlock(&ubi
->volumes_lock
);
1681 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
1682 * @ubi: UBI device description object
1683 * @vol_id: the volume ID that last used this PEB
1684 * @lnum: the last used logical eraseblock number for the PEB
1685 * @pnum: physical eraseblock to return
1686 * @torture: if this physical eraseblock has to be tortured
1688 * This function is called to return physical eraseblock @pnum to the pool of
1689 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1690 * occurred to this @pnum and it has to be tested. This function returns zero
1691 * in case of success, and a negative error code in case of failure.
1693 int ubi_wl_put_peb(struct ubi_device
*ubi
, int vol_id
, int lnum
,
1694 int pnum
, int torture
)
1697 struct ubi_wl_entry
*e
;
1699 dbg_wl("PEB %d", pnum
);
1700 ubi_assert(pnum
>= 0);
1701 ubi_assert(pnum
< ubi
->peb_count
);
1704 spin_lock(&ubi
->wl_lock
);
1705 e
= ubi
->lookuptbl
[pnum
];
1706 if (e
== ubi
->move_from
) {
1708 * User is putting the physical eraseblock which was selected to
1709 * be moved. It will be scheduled for erasure in the
1710 * wear-leveling worker.
1712 dbg_wl("PEB %d is being moved, wait", pnum
);
1713 spin_unlock(&ubi
->wl_lock
);
1715 /* Wait for the WL worker by taking the @ubi->move_mutex */
1716 mutex_lock(&ubi
->move_mutex
);
1717 mutex_unlock(&ubi
->move_mutex
);
1719 } else if (e
== ubi
->move_to
) {
1721 * User is putting the physical eraseblock which was selected
1722 * as the target the data is moved to. It may happen if the EBA
1723 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1724 * but the WL sub-system has not put the PEB to the "used" tree
1725 * yet, but it is about to do this. So we just set a flag which
1726 * will tell the WL worker that the PEB is not needed anymore
1727 * and should be scheduled for erasure.
1729 dbg_wl("PEB %d is the target of data moving", pnum
);
1730 ubi_assert(!ubi
->move_to_put
);
1731 ubi
->move_to_put
= 1;
1732 spin_unlock(&ubi
->wl_lock
);
1735 if (in_wl_tree(e
, &ubi
->used
)) {
1736 self_check_in_wl_tree(ubi
, e
, &ubi
->used
);
1737 rb_erase(&e
->u
.rb
, &ubi
->used
);
1738 } else if (in_wl_tree(e
, &ubi
->scrub
)) {
1739 self_check_in_wl_tree(ubi
, e
, &ubi
->scrub
);
1740 rb_erase(&e
->u
.rb
, &ubi
->scrub
);
1741 } else if (in_wl_tree(e
, &ubi
->erroneous
)) {
1742 self_check_in_wl_tree(ubi
, e
, &ubi
->erroneous
);
1743 rb_erase(&e
->u
.rb
, &ubi
->erroneous
);
1744 ubi
->erroneous_peb_count
-= 1;
1745 ubi_assert(ubi
->erroneous_peb_count
>= 0);
1746 /* Erroneous PEBs should be tortured */
1749 err
= prot_queue_del(ubi
, e
->pnum
);
1751 ubi_err("PEB %d not found", pnum
);
1753 spin_unlock(&ubi
->wl_lock
);
1758 spin_unlock(&ubi
->wl_lock
);
1760 err
= schedule_erase(ubi
, e
, vol_id
, lnum
, torture
);
1762 spin_lock(&ubi
->wl_lock
);
1763 wl_tree_add(e
, &ubi
->used
);
1764 spin_unlock(&ubi
->wl_lock
);
1771 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1772 * @ubi: UBI device description object
1773 * @pnum: the physical eraseblock to schedule
1775 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1776 * needs scrubbing. This function schedules a physical eraseblock for
1777 * scrubbing which is done in background. This function returns zero in case of
1778 * success and a negative error code in case of failure.
1780 int ubi_wl_scrub_peb(struct ubi_device
*ubi
, int pnum
)
1782 struct ubi_wl_entry
*e
;
1784 ubi_msg("schedule PEB %d for scrubbing", pnum
);
1787 spin_lock(&ubi
->wl_lock
);
1788 e
= ubi
->lookuptbl
[pnum
];
1789 if (e
== ubi
->move_from
|| in_wl_tree(e
, &ubi
->scrub
) ||
1790 in_wl_tree(e
, &ubi
->erroneous
)) {
1791 spin_unlock(&ubi
->wl_lock
);
1795 if (e
== ubi
->move_to
) {
1797 * This physical eraseblock was used to move data to. The data
1798 * was moved but the PEB was not yet inserted to the proper
1799 * tree. We should just wait a little and let the WL worker
1802 spin_unlock(&ubi
->wl_lock
);
1803 dbg_wl("the PEB %d is not in proper tree, retry", pnum
);
1808 if (in_wl_tree(e
, &ubi
->used
)) {
1809 self_check_in_wl_tree(ubi
, e
, &ubi
->used
);
1810 rb_erase(&e
->u
.rb
, &ubi
->used
);
1814 err
= prot_queue_del(ubi
, e
->pnum
);
1816 ubi_err("PEB %d not found", pnum
);
1818 spin_unlock(&ubi
->wl_lock
);
1823 wl_tree_add(e
, &ubi
->scrub
);
1824 spin_unlock(&ubi
->wl_lock
);
1827 * Technically scrubbing is the same as wear-leveling, so it is done
1830 return ensure_wear_leveling(ubi
, 0);
1834 * ubi_wl_flush - flush all pending works.
1835 * @ubi: UBI device description object
1836 * @vol_id: the volume id to flush for
1837 * @lnum: the logical eraseblock number to flush for
1839 * This function executes all pending works for a particular volume id /
1840 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1841 * acts as a wildcard for all of the corresponding volume numbers or logical
1842 * eraseblock numbers. It returns zero in case of success and a negative error
1843 * code in case of failure.
1845 int ubi_wl_flush(struct ubi_device
*ubi
, int vol_id
, int lnum
)
1851 * Erase while the pending works queue is not empty, but not more than
1852 * the number of currently pending works.
1854 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1855 vol_id
, lnum
, ubi
->works_count
);
1858 struct ubi_work
*wrk
;
1861 down_read(&ubi
->work_sem
);
1862 spin_lock(&ubi
->wl_lock
);
1863 list_for_each_entry(wrk
, &ubi
->works
, list
) {
1864 if ((vol_id
== UBI_ALL
|| wrk
->vol_id
== vol_id
) &&
1865 (lnum
== UBI_ALL
|| wrk
->lnum
== lnum
)) {
1866 list_del(&wrk
->list
);
1867 ubi
->works_count
-= 1;
1868 ubi_assert(ubi
->works_count
>= 0);
1869 spin_unlock(&ubi
->wl_lock
);
1871 err
= wrk
->func(ubi
, wrk
, 0);
1873 up_read(&ubi
->work_sem
);
1877 spin_lock(&ubi
->wl_lock
);
1882 spin_unlock(&ubi
->wl_lock
);
1883 up_read(&ubi
->work_sem
);
1887 * Make sure all the works which have been done in parallel are
1890 down_write(&ubi
->work_sem
);
1891 up_write(&ubi
->work_sem
);
1897 * tree_destroy - destroy an RB-tree.
1898 * @root: the root of the tree to destroy
1900 static void tree_destroy(struct rb_root
*root
)
1903 struct ubi_wl_entry
*e
;
1909 else if (rb
->rb_right
)
1912 e
= rb_entry(rb
, struct ubi_wl_entry
, u
.rb
);
1916 if (rb
->rb_left
== &e
->u
.rb
)
1919 rb
->rb_right
= NULL
;
1922 kmem_cache_free(ubi_wl_entry_slab
, e
);
1928 * ubi_thread - UBI background thread.
1929 * @u: the UBI device description object pointer
1931 int ubi_thread(void *u
)
1934 struct ubi_device
*ubi
= u
;
1936 ubi_msg("background thread \"%s\" started, PID %d",
1937 ubi
->bgt_name
, task_pid_nr(current
));
1943 if (kthread_should_stop())
1946 if (try_to_freeze())
1949 spin_lock(&ubi
->wl_lock
);
1950 if (list_empty(&ubi
->works
) || ubi
->ro_mode
||
1951 !ubi
->thread_enabled
|| ubi_dbg_is_bgt_disabled(ubi
)) {
1952 set_current_state(TASK_INTERRUPTIBLE
);
1953 spin_unlock(&ubi
->wl_lock
);
1957 spin_unlock(&ubi
->wl_lock
);
1961 ubi_err("%s: work failed with error code %d",
1962 ubi
->bgt_name
, err
);
1963 if (failures
++ > WL_MAX_FAILURES
) {
1965 * Too many failures, disable the thread and
1966 * switch to read-only mode.
1968 ubi_msg("%s: %d consecutive failures",
1969 ubi
->bgt_name
, WL_MAX_FAILURES
);
1971 ubi
->thread_enabled
= 0;
1980 dbg_wl("background thread \"%s\" is killed", ubi
->bgt_name
);
1985 * cancel_pending - cancel all pending works.
1986 * @ubi: UBI device description object
1988 static void cancel_pending(struct ubi_device
*ubi
)
1990 while (!list_empty(&ubi
->works
)) {
1991 struct ubi_work
*wrk
;
1993 wrk
= list_entry(ubi
->works
.next
, struct ubi_work
, list
);
1994 list_del(&wrk
->list
);
1995 wrk
->func(ubi
, wrk
, 1);
1996 ubi
->works_count
-= 1;
1997 ubi_assert(ubi
->works_count
>= 0);
2002 * ubi_wl_init - initialize the WL sub-system using attaching information.
2003 * @ubi: UBI device description object
2004 * @ai: attaching information
2006 * This function returns zero in case of success, and a negative error code in
2009 int ubi_wl_init(struct ubi_device
*ubi
, struct ubi_attach_info
*ai
)
2011 int err
, i
, reserved_pebs
, found_pebs
= 0;
2012 struct rb_node
*rb1
, *rb2
;
2013 struct ubi_ainf_volume
*av
;
2014 struct ubi_ainf_peb
*aeb
, *tmp
;
2015 struct ubi_wl_entry
*e
;
2017 ubi
->used
= ubi
->erroneous
= ubi
->free
= ubi
->scrub
= RB_ROOT
;
2018 spin_lock_init(&ubi
->wl_lock
);
2019 mutex_init(&ubi
->move_mutex
);
2020 init_rwsem(&ubi
->work_sem
);
2021 ubi
->max_ec
= ai
->max_ec
;
2022 INIT_LIST_HEAD(&ubi
->works
);
2023 #ifdef CONFIG_MTD_UBI_FASTMAP
2024 INIT_WORK(&ubi
->fm_work
, update_fastmap_work_fn
);
2027 sprintf(ubi
->bgt_name
, UBI_BGT_NAME_PATTERN
, ubi
->ubi_num
);
2030 ubi
->lookuptbl
= kzalloc(ubi
->peb_count
* sizeof(void *), GFP_KERNEL
);
2031 if (!ubi
->lookuptbl
)
2034 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; i
++)
2035 INIT_LIST_HEAD(&ubi
->pq
[i
]);
2038 list_for_each_entry_safe(aeb
, tmp
, &ai
->erase
, u
.list
) {
2041 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
2045 e
->pnum
= aeb
->pnum
;
2047 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
2048 ubi
->lookuptbl
[e
->pnum
] = e
;
2051 if(ubi_erase_peb(ubi
,e
,0)){
2052 kmem_cache_free(ubi_wl_entry_slab
, e
);
2057 if(schedule_erase(ubi
, e
, aeb
->vol_id
, aeb
->lnum
, 0)) {
2058 kmem_cache_free(ubi_wl_entry_slab
, e
);
2065 ubi
->free_count
= 0;
2066 list_for_each_entry(aeb
, &ai
->free
, u
.list
) {
2069 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
2073 e
->pnum
= aeb
->pnum
;
2075 ubi_assert(e
->ec
>= 0);
2076 ubi_assert(!ubi_is_fm_block(ubi
, e
->pnum
));
2078 wl_tree_add(e
, &ubi
->free
);
2081 ubi
->lookuptbl
[e
->pnum
] = e
;
2086 ubi_rb_for_each_entry(rb1
, av
, &ai
->volumes
, rb
) {
2087 ubi_rb_for_each_entry(rb2
, aeb
, &av
->root
, u
.rb
) {
2090 e
= kmem_cache_alloc(ubi_wl_entry_slab
, GFP_KERNEL
);
2094 e
->pnum
= aeb
->pnum
;
2096 ubi
->lookuptbl
[e
->pnum
] = e
;
2099 dbg_wl("add PEB %d EC %d to the used tree",
2101 wl_tree_add(e
, &ubi
->used
);
2103 dbg_wl("add PEB %d EC %d to the scrub tree",
2105 wl_tree_add(e
, &ubi
->scrub
);
2112 dbg_wl("found %i PEBs", found_pebs
);
2115 ubi_assert(ubi
->good_peb_count
== \
2116 found_pebs
+ ubi
->fm
->used_blocks
);
2118 ubi_assert(ubi
->good_peb_count
== found_pebs
);
2120 reserved_pebs
= WL_RESERVED_PEBS
;
2121 #ifdef CONFIG_MTD_UBI_FASTMAP
2122 /* Reserve enough LEBs to store two fastmaps. */
2123 reserved_pebs
+= (ubi
->fm_size
/ ubi
->leb_size
) * 2;
2126 if (ubi
->avail_pebs
< reserved_pebs
) {
2127 ubi_err("no enough physical eraseblocks (%d, need %d)",
2128 ubi
->avail_pebs
, reserved_pebs
);
2129 if (ubi
->corr_peb_count
)
2130 ubi_err("%d PEBs are corrupted and not used",
2131 ubi
->corr_peb_count
);
2134 ubi
->avail_pebs
-= reserved_pebs
;
2135 ubi
->rsvd_pebs
+= reserved_pebs
;
2137 /* Schedule wear-leveling if needed */
2138 err
= ensure_wear_leveling(ubi
, 0);
2145 cancel_pending(ubi
);
2146 tree_destroy(&ubi
->used
);
2147 tree_destroy(&ubi
->free
);
2148 tree_destroy(&ubi
->scrub
);
2149 kfree(ubi
->lookuptbl
);
2154 * protection_queue_destroy - destroy the protection queue.
2155 * @ubi: UBI device description object
2157 static void protection_queue_destroy(struct ubi_device
*ubi
)
2160 struct ubi_wl_entry
*e
, *tmp
;
2162 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; ++i
) {
2163 list_for_each_entry_safe(e
, tmp
, &ubi
->pq
[i
], u
.list
) {
2164 list_del(&e
->u
.list
);
2165 kmem_cache_free(ubi_wl_entry_slab
, e
);
2171 * ubi_wl_close - close the wear-leveling sub-system.
2172 * @ubi: UBI device description object
2174 void ubi_wl_close(struct ubi_device
*ubi
)
2176 dbg_wl("close the WL sub-system");
2177 cancel_pending(ubi
);
2178 protection_queue_destroy(ubi
);
2179 tree_destroy(&ubi
->used
);
2180 tree_destroy(&ubi
->erroneous
);
2181 tree_destroy(&ubi
->free
);
2182 tree_destroy(&ubi
->scrub
);
2183 kfree(ubi
->lookuptbl
);
2186 #ifdef MTK_IPOH_SUPPORT
2187 void ubi_wl_move_pg_to_used(struct ubi_device
*ubi
, int pnum
) {
2188 struct ubi_wl_entry
*e
;
2189 e
= ubi
->lookuptbl
[pnum
];
2190 if(in_wl_tree(e
, &ubi
->used
) == 0) {
2191 prot_queue_del(ubi
, e
->pnum
);
2192 wl_tree_add(e
, &ubi
->used
);
2198 * self_check_ec - make sure that the erase counter of a PEB is correct.
2199 * @ubi: UBI device description object
2200 * @pnum: the physical eraseblock number to check
2201 * @ec: the erase counter to check
2203 * This function returns zero if the erase counter of physical eraseblock @pnum
2204 * is equivalent to @ec, and a negative error code if not or if an error
2207 static int self_check_ec(struct ubi_device
*ubi
, int pnum
, int ec
)
2211 struct ubi_ec_hdr
*ec_hdr
;
2213 if (!ubi_dbg_chk_gen(ubi
))
2216 ec_hdr
= kzalloc(ubi
->ec_hdr_alsize
, GFP_NOFS
);
2220 err
= ubi_io_read_ec_hdr(ubi
, pnum
, ec_hdr
, 0);
2221 if (err
&& err
!= UBI_IO_BITFLIPS
) {
2222 /* The header does not have to exist */
2227 read_ec
= be64_to_cpu(ec_hdr
->ec
);
2228 if (ec
!= read_ec
&& read_ec
- ec
> 1) {
2229 ubi_err("self-check failed for PEB %d", pnum
);
2230 ubi_err("read EC is %lld, should be %d", read_ec
, ec
);
2242 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
2243 * @ubi: UBI device description object
2244 * @e: the wear-leveling entry to check
2245 * @root: the root of the tree
2247 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
2250 static int self_check_in_wl_tree(const struct ubi_device
*ubi
,
2251 struct ubi_wl_entry
*e
, struct rb_root
*root
)
2253 if (!ubi_dbg_chk_gen(ubi
))
2256 if (in_wl_tree(e
, root
))
2259 ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
2260 e
->pnum
, e
->ec
, root
);
2266 * self_check_in_pq - check if wear-leveling entry is in the protection
2268 * @ubi: UBI device description object
2269 * @e: the wear-leveling entry to check
2271 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
2273 static int self_check_in_pq(const struct ubi_device
*ubi
,
2274 struct ubi_wl_entry
*e
)
2276 struct ubi_wl_entry
*p
;
2279 if (!ubi_dbg_chk_gen(ubi
))
2282 for (i
= 0; i
< UBI_PROT_QUEUE_LEN
; ++i
)
2283 list_for_each_entry(p
, &ubi
->pq
[i
], u
.list
)
2287 ubi_err("self-check failed for PEB %d, EC %d, Protect queue",