blob: f0bc10743bc0ff2bdaf13ed2d2f2168dca2c8265 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03002 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04003 * Copyright (c) International Business Machines Corp., 2006
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
20 */
21
22/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030023 * UBI wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040024 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030025 * This sub-system is responsible for wear-leveling. It works in terms of
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080026 * physical eraseblocks and erase counters and knows nothing about logical
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030027 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
28 * eraseblocks are of two types - used and free. Used physical eraseblocks are
29 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
30 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040031 *
32 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030033 * header. The rest of the physical eraseblock contains only %0xFF bytes.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040034 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030035 * When physical eraseblocks are returned to the WL sub-system by means of the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040036 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
37 * done asynchronously in context of the per-UBI device background thread,
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030038 * which is also managed by the WL sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040039 *
40 * The wear-leveling is ensured by means of moving the contents of used
41 * physical eraseblocks with low erase counter to free physical eraseblocks
42 * with high erase counter.
43 *
44 * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
45 * an "optimal" physical eraseblock. For example, when it is known that the
46 * physical eraseblock will be "put" soon because it contains short-term data,
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030047 * the WL sub-system may pick a free physical eraseblock with low erase
48 * counter, and so forth.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040049 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030050 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
51 * bad.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040052 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030053 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
54 * in a physical eraseblock, it has to be moved. Technically this is the same
55 * as moving it for wear-leveling reasons.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040056 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030057 * As it was said, for the UBI sub-system all physical eraseblocks are either
58 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030059 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
60 * RB-trees, as well as (temporarily) in the @wl->pq queue.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080061 *
62 * When the WL sub-system returns a physical eraseblock, the physical
63 * eraseblock is protected from being moved for some "time". For this reason,
64 * the physical eraseblock is not directly moved from the @wl->free tree to the
65 * @wl->used tree. There is a protection queue in between where this
66 * physical eraseblock is temporarily stored (@wl->pq).
67 *
68 * All this protection stuff is needed because:
69 * o we don't want to move physical eraseblocks just after we have given them
70 * to the user; instead, we first want to let users fill them up with data;
71 *
72 * o there is a chance that the user will put the physical eraseblock very
73 * soon, so it makes sense not to move it for some time, but wait; this is
74 * especially important in case of "short term" physical eraseblocks.
75 *
76 * Physical eraseblocks stay protected only for limited time. But the "time" is
77 * measured in erase cycles in this case. This is implemented with help of the
78 * protection queue. Eraseblocks are put to the tail of this queue when they
79 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
80 * head of the queue on each erase operation (for any eraseblock). So the
81 * length of the queue defines how may (global) erase cycles PEBs are protected.
82 *
83 * To put it differently, each physical eraseblock has 2 main states: free and
84 * used. The former state corresponds to the @wl->free tree. The latter state
85 * is split up on several sub-states:
86 * o the WL movement is allowed (@wl->used tree);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +030087 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030088 * erroneous - e.g., there was a read error;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080089 * o the WL movement is temporarily prohibited (@wl->pq queue);
90 * o scrubbing is needed (@wl->scrub tree).
91 *
92 * Depending on the sub-state, wear-leveling entries of the used physical
93 * eraseblocks may be kept in one of those structures.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040094 *
95 * Note, in this implementation, we keep a small in-RAM object for each physical
96 * eraseblock. This is surely not a scalable solution. But it appears to be good
97 * enough for moderately large flashes and it is simple. In future, one may
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030098 * re-work this sub-system and make it more scalable.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040099 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300100 * At the moment this sub-system does not utilize the sequence number, which
101 * was introduced relatively recently. But it would be wise to do this because
102 * the sequence number of a logical eraseblock characterizes how old is it. For
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400103 * example, when we move a PEB with low erase counter, and we need to pick the
104 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
105 * pick target PEB with an average EC if our PEB is not very "old". This is a
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300106 * room for future re-works of the WL sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400107 */
108
109#include <linux/slab.h>
110#include <linux/crc32.h>
111#include <linux/freezer.h>
112#include <linux/kthread.h>
113#include "ubi.h"
114
115/* Number of physical eraseblocks reserved for wear-leveling purposes */
116#define WL_RESERVED_PEBS 1
117
118/*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119 * Maximum difference between two erase counters. If this threshold is
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300120 * exceeded, the WL sub-system starts moving data from used physical
121 * eraseblocks with low erase counter to free physical eraseblocks with high
122 * erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400123 */
124#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
125
126/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300127 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400128 * physical eraseblock to move to. The simplest way would be just to pick the
129 * one with the highest erase counter. But in certain workloads this could lead
130 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
131 * situation when the picked physical eraseblock is constantly erased after the
132 * data is written to it. So, we have a constant which limits the highest erase
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300133 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200134 * does not pick eraseblocks with erase counter greater than the lowest erase
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400135 * counter plus %WL_FREE_MAX_DIFF.
136 */
137#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
138
139/*
140 * Maximum number of consecutive background thread failures which is enough to
141 * switch to read-only mode.
142 */
143#define WL_MAX_FAILURES 32
144
145/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400146 * struct ubi_work - UBI work description data structure.
147 * @list: a link in the list of pending works
148 * @func: worker function
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400149 * @e: physical eraseblock to erase
150 * @torture: if the physical eraseblock has to be tortured
151 *
152 * The @func pointer points to the worker function. If the @cancel argument is
153 * not zero, the worker has to free the resources and exit immediately. The
154 * worker has to return zero in case of success and a negative error code in
155 * case of failure.
156 */
157struct ubi_work {
158 struct list_head list;
159 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
160 /* The below fields are only relevant to erasure works */
161 struct ubi_wl_entry *e;
162 int torture;
163};
164
Artem Bityutskiy92d124f2011-03-14 18:17:40 +0200165#ifdef CONFIG_MTD_UBI_DEBUG
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300166static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300167static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
168 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169 struct rb_root *root);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300170static int paranoid_check_in_pq(const struct ubi_device *ubi,
171 struct ubi_wl_entry *e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400172#else
173#define paranoid_check_ec(ubi, pnum, ec) 0
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300174#define paranoid_check_in_wl_tree(ubi, e, root)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800175#define paranoid_check_in_pq(ubi, e) 0
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400176#endif
177
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400178/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400179 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
180 * @e: the wear-leveling entry to add
181 * @root: the root of the tree
182 *
183 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
184 * the @ubi->used and @ubi->free RB-trees.
185 */
186static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
187{
188 struct rb_node **p, *parent = NULL;
189
190 p = &root->rb_node;
191 while (*p) {
192 struct ubi_wl_entry *e1;
193
194 parent = *p;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800195 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400196
197 if (e->ec < e1->ec)
198 p = &(*p)->rb_left;
199 else if (e->ec > e1->ec)
200 p = &(*p)->rb_right;
201 else {
202 ubi_assert(e->pnum != e1->pnum);
203 if (e->pnum < e1->pnum)
204 p = &(*p)->rb_left;
205 else
206 p = &(*p)->rb_right;
207 }
208 }
209
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800210 rb_link_node(&e->u.rb, parent, p);
211 rb_insert_color(&e->u.rb, root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400212}
213
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400214/**
215 * do_work - do one pending work.
216 * @ubi: UBI device description object
217 *
218 * This function returns zero in case of success and a negative error code in
219 * case of failure.
220 */
221static int do_work(struct ubi_device *ubi)
222{
223 int err;
224 struct ubi_work *wrk;
225
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200226 cond_resched();
227
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200228 /*
229 * @ubi->work_sem is used to synchronize with the workers. Workers take
230 * it in read mode, so many of them may be doing works at a time. But
231 * the queue flush code has to be sure the whole queue of works is
232 * done, and it takes the mutex in write mode.
233 */
234 down_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400235 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400236 if (list_empty(&ubi->works)) {
237 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200238 up_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239 return 0;
240 }
241
242 wrk = list_entry(ubi->works.next, struct ubi_work, list);
243 list_del(&wrk->list);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200244 ubi->works_count -= 1;
245 ubi_assert(ubi->works_count >= 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400246 spin_unlock(&ubi->wl_lock);
247
248 /*
249 * Call the worker function. Do not touch the work structure
250 * after this call as it will have been freed or reused by that
251 * time by the worker function.
252 */
253 err = wrk->func(ubi, wrk, 0);
254 if (err)
255 ubi_err("work failed with error code %d", err);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200256 up_read(&ubi->work_sem);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200257
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400258 return err;
259}
260
261/**
262 * produce_free_peb - produce a free physical eraseblock.
263 * @ubi: UBI device description object
264 *
265 * This function tries to make a free PEB by means of synchronous execution of
266 * pending works. This may be needed if, for example the background thread is
267 * disabled. Returns zero in case of success and a negative error code in case
268 * of failure.
269 */
270static int produce_free_peb(struct ubi_device *ubi)
271{
272 int err;
273
274 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300275 while (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400276 spin_unlock(&ubi->wl_lock);
277
278 dbg_wl("do one work synchronously");
279 err = do_work(ubi);
280 if (err)
281 return err;
282
283 spin_lock(&ubi->wl_lock);
284 }
285 spin_unlock(&ubi->wl_lock);
286
287 return 0;
288}
289
290/**
291 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
292 * @e: the wear-leveling entry to check
293 * @root: the root of the tree
294 *
295 * This function returns non-zero if @e is in the @root RB-tree and zero if it
296 * is not.
297 */
298static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
299{
300 struct rb_node *p;
301
302 p = root->rb_node;
303 while (p) {
304 struct ubi_wl_entry *e1;
305
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800306 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400307
308 if (e->pnum == e1->pnum) {
309 ubi_assert(e == e1);
310 return 1;
311 }
312
313 if (e->ec < e1->ec)
314 p = p->rb_left;
315 else if (e->ec > e1->ec)
316 p = p->rb_right;
317 else {
318 ubi_assert(e->pnum != e1->pnum);
319 if (e->pnum < e1->pnum)
320 p = p->rb_left;
321 else
322 p = p->rb_right;
323 }
324 }
325
326 return 0;
327}
328
329/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800330 * prot_queue_add - add physical eraseblock to the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400331 * @ubi: UBI device description object
332 * @e: the physical eraseblock to add
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400333 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800334 * This function adds @e to the tail of the protection queue @ubi->pq, where
335 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
336 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
337 * be locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400338 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800339static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400340{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800341 int pq_tail = ubi->pq_head - 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400342
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800343 if (pq_tail < 0)
344 pq_tail = UBI_PROT_QUEUE_LEN - 1;
345 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
346 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
347 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400348}
349
350/**
351 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
352 * @root: the RB-tree where to look for
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200353 * @diff: maximum possible difference from the smallest erase counter
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400354 *
355 * This function looks for a wear leveling entry with erase counter closest to
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200356 * min + @diff, where min is the smallest erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400357 */
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200358static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359{
360 struct rb_node *p;
361 struct ubi_wl_entry *e;
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200362 int max;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400363
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800364 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200365 max = e->ec + diff;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400366
367 p = root->rb_node;
368 while (p) {
369 struct ubi_wl_entry *e1;
370
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800371 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400372 if (e1->ec >= max)
373 p = p->rb_left;
374 else {
375 p = p->rb_right;
376 e = e1;
377 }
378 }
379
380 return e;
381}
382
383/**
384 * ubi_wl_get_peb - get a physical eraseblock.
385 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386 *
387 * This function returns a physical eraseblock in case of success and a
388 * negative error code in case of failure. Might sleep.
389 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200390int ubi_wl_get_peb(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400391{
Artem Bityutskiy7eb3aa652012-03-07 19:08:36 +0200392 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400393 struct ubi_wl_entry *e, *first, *last;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400394
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395retry:
396 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300397 if (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400398 if (ubi->works_count == 0) {
399 ubi_assert(list_empty(&ubi->works));
400 ubi_err("no free eraseblocks");
401 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400402 return -ENOSPC;
403 }
404 spin_unlock(&ubi->wl_lock);
405
406 err = produce_free_peb(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800407 if (err < 0)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400408 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409 goto retry;
410 }
411
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200412 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
413 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400414
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200415 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
416 e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb);
417 else
418 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400419
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300420 paranoid_check_in_wl_tree(ubi, e, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800421
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800423 * Move the physical eraseblock to the protection queue where it will
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400424 * be protected from being moved for some time.
425 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800426 rb_erase(&e->u.rb, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800427 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
428 prot_queue_add(ubi, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300430
431 err = ubi_dbg_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
432 ubi->peb_size - ubi->vid_hdr_aloffset);
433 if (err) {
Artem Bityutskiy13987882009-06-29 15:58:36 +0300434 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200435 return err;
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300436 }
437
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400438 return e->pnum;
439}
440
441/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800442 * prot_queue_del - remove a physical eraseblock from the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443 * @ubi: UBI device description object
444 * @pnum: the physical eraseblock to remove
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200445 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800446 * This function deletes PEB @pnum from the protection queue and returns zero
447 * in case of success and %-ENODEV if the PEB was not found.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400448 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800449static int prot_queue_del(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400450{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800451 struct ubi_wl_entry *e;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400452
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800453 e = ubi->lookuptbl[pnum];
454 if (!e)
455 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400456
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800457 if (paranoid_check_in_pq(ubi, e))
458 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400459
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800460 list_del(&e->u.list);
461 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200462 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400463}
464
465/**
466 * sync_erase - synchronously erase a physical eraseblock.
467 * @ubi: UBI device description object
468 * @e: the the physical eraseblock to erase
469 * @torture: if the physical eraseblock has to be tortured
470 *
471 * This function returns zero in case of success and a negative error code in
472 * case of failure.
473 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300474static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
475 int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400476{
477 int err;
478 struct ubi_ec_hdr *ec_hdr;
479 unsigned long long ec = e->ec;
480
481 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
482
483 err = paranoid_check_ec(ubi, e->pnum, e->ec);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200484 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400485 return -EINVAL;
486
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300487 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400488 if (!ec_hdr)
489 return -ENOMEM;
490
491 err = ubi_io_sync_erase(ubi, e->pnum, torture);
492 if (err < 0)
493 goto out_free;
494
495 ec += err;
496 if (ec > UBI_MAX_ERASECOUNTER) {
497 /*
498 * Erase counter overflow. Upgrade UBI and use 64-bit
499 * erase counters internally.
500 */
501 ubi_err("erase counter overflow at PEB %d, EC %llu",
502 e->pnum, ec);
503 err = -EINVAL;
504 goto out_free;
505 }
506
507 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
508
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300509 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400510
511 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
512 if (err)
513 goto out_free;
514
515 e->ec = ec;
516 spin_lock(&ubi->wl_lock);
517 if (e->ec > ubi->max_ec)
518 ubi->max_ec = e->ec;
519 spin_unlock(&ubi->wl_lock);
520
521out_free:
522 kfree(ec_hdr);
523 return err;
524}
525
526/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800527 * serve_prot_queue - check if it is time to stop protecting PEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400528 * @ubi: UBI device description object
529 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800530 * This function is called after each erase operation and removes PEBs from the
531 * tail of the protection queue. These PEBs have been protected for long enough
532 * and should be moved to the used tree.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400533 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800534static void serve_prot_queue(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400535{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800536 struct ubi_wl_entry *e, *tmp;
537 int count;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400538
539 /*
540 * There may be several protected physical eraseblock to remove,
541 * process them all.
542 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800543repeat:
544 count = 0;
545 spin_lock(&ubi->wl_lock);
546 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
547 dbg_wl("PEB %d EC %d protection over, move to used tree",
548 e->pnum, e->ec);
549
550 list_del(&e->u.list);
551 wl_tree_add(e, &ubi->used);
552 if (count++ > 32) {
553 /*
554 * Let's be nice and avoid holding the spinlock for
555 * too long.
556 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400557 spin_unlock(&ubi->wl_lock);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800558 cond_resched();
559 goto repeat;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400560 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400561 }
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800562
563 ubi->pq_head += 1;
564 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
565 ubi->pq_head = 0;
566 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
567 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400568}
569
570/**
571 * schedule_ubi_work - schedule a work.
572 * @ubi: UBI device description object
573 * @wrk: the work to schedule
574 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800575 * This function adds a work defined by @wrk to the tail of the pending works
576 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400577 */
578static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
579{
580 spin_lock(&ubi->wl_lock);
581 list_add_tail(&wrk->list, &ubi->works);
582 ubi_assert(ubi->works_count >= 0);
583 ubi->works_count += 1;
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +0300584 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400585 wake_up_process(ubi->bgt_thread);
586 spin_unlock(&ubi->wl_lock);
587}
588
589static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
590 int cancel);
591
592/**
593 * schedule_erase - schedule an erase work.
594 * @ubi: UBI device description object
595 * @e: the WL entry of the physical eraseblock to erase
596 * @torture: if the physical eraseblock has to be tortured
597 *
598 * This function returns zero in case of success and a %-ENOMEM in case of
599 * failure.
600 */
601static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
602 int torture)
603{
604 struct ubi_work *wl_wrk;
605
606 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
607 e->pnum, e->ec, torture);
608
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300609 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610 if (!wl_wrk)
611 return -ENOMEM;
612
613 wl_wrk->func = &erase_worker;
614 wl_wrk->e = e;
615 wl_wrk->torture = torture;
616
617 schedule_ubi_work(ubi, wl_wrk);
618 return 0;
619}
620
621/**
622 * wear_leveling_worker - wear-leveling worker function.
623 * @ubi: UBI device description object
624 * @wrk: the work object
625 * @cancel: non-zero if the worker has to free memory and exit
626 *
627 * This function copies a more worn out physical eraseblock to a less worn out
628 * one. Returns zero in case of success and a negative error code in case of
629 * failure.
630 */
631static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
632 int cancel)
633{
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300634 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300635 int vol_id = -1, uninitialized_var(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400636 struct ubi_wl_entry *e1, *e2;
637 struct ubi_vid_hdr *vid_hdr;
638
639 kfree(wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400640 if (cancel)
641 return 0;
642
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300643 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400644 if (!vid_hdr)
645 return -ENOMEM;
646
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200647 mutex_lock(&ubi->move_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400648 spin_lock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200649 ubi_assert(!ubi->move_from && !ubi->move_to);
650 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400651
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200652 if (!ubi->free.rb_node ||
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300653 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200655 * No free physical eraseblocks? Well, they must be waiting in
656 * the queue to be erased. Cancel movement - it will be
657 * triggered again when a free physical eraseblock appears.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400658 *
659 * No used physical eraseblocks? They must be temporarily
660 * protected from being moved. They will be moved to the
661 * @ubi->used tree later and the wear-leveling will be
662 * triggered again.
663 */
664 dbg_wl("cancel WL, a list is empty: free %d, used %d",
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300665 !ubi->free.rb_node, !ubi->used.rb_node);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200666 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400667 }
668
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300669 if (!ubi->scrub.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400670 /*
671 * Now pick the least worn-out used physical eraseblock and a
672 * highly worn-out free physical eraseblock. If the erase
673 * counters differ much enough, start wear-leveling.
674 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800675 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400676 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
677
678 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
679 dbg_wl("no WL needed: min used EC %d, max free EC %d",
680 e1->ec, e2->ec);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200681 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400682 }
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300683 paranoid_check_in_wl_tree(ubi, e1, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800684 rb_erase(&e1->u.rb, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400685 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
686 e1->pnum, e1->ec, e2->pnum, e2->ec);
687 } else {
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200688 /* Perform scrubbing */
689 scrubbing = 1;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800690 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400691 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300692 paranoid_check_in_wl_tree(ubi, e1, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800693 rb_erase(&e1->u.rb, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400694 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
695 }
696
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300697 paranoid_check_in_wl_tree(ubi, e2, &ubi->free);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800698 rb_erase(&e2->u.rb, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400699 ubi->move_from = e1;
700 ubi->move_to = e2;
701 spin_unlock(&ubi->wl_lock);
702
703 /*
704 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
705 * We so far do not know which logical eraseblock our physical
706 * eraseblock (@e1) belongs to. We have to read the volume identifier
707 * header first.
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200708 *
709 * Note, we are protected from this PEB being unmapped and erased. The
710 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
711 * which is being moved was unmapped.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400712 */
713
714 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
715 if (err && err != UBI_IO_BITFLIPS) {
Artem Bityutskiy74d82d22010-09-03 02:11:20 +0300716 if (err == UBI_IO_FF) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400717 /*
718 * We are trying to move PEB without a VID header. UBI
719 * always write VID headers shortly after the PEB was
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300720 * given, so we have a situation when it has not yet
721 * had a chance to write it, because it was preempted.
722 * So add this PEB to the protection queue so far,
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300723 * because presumably more data will be written there
724 * (including the missing VID header), and then we'll
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300725 * move it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400726 */
727 dbg_wl("PEB %d has no VID header", e1->pnum);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300728 protect = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200729 goto out_not_moved;
Artem Bityutskiy92e1a7d2010-09-03 14:22:17 +0300730 } else if (err == UBI_IO_FF_BITFLIPS) {
731 /*
732 * The same situation as %UBI_IO_FF, but bit-flips were
733 * detected. It is better to schedule this PEB for
734 * scrubbing.
735 */
736 dbg_wl("PEB %d has no VID header but has bit-flips",
737 e1->pnum);
738 scrubbing = 1;
739 goto out_not_moved;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400740 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200741
742 ubi_err("error %d while reading VID header from PEB %d",
743 err, e1->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200744 goto out_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400745 }
746
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300747 vol_id = be32_to_cpu(vid_hdr->vol_id);
748 lnum = be32_to_cpu(vid_hdr->lnum);
749
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400750 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
751 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300752 if (err == MOVE_CANCEL_RACE) {
753 /*
754 * The LEB has not been moved because the volume is
755 * being deleted or the PEB has been put meanwhile. We
756 * should prevent this PEB from being selected for
757 * wear-leveling movement again, so put it to the
758 * protection queue.
759 */
760 protect = 1;
761 goto out_not_moved;
762 }
Bhavesh Parekhe801e122011-11-30 17:43:42 +0530763 if (err == MOVE_RETRY) {
764 scrubbing = 1;
765 goto out_not_moved;
766 }
Artem Bityutskiycc831462012-03-09 10:31:18 +0200767 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300768 err == MOVE_TARGET_RD_ERR) {
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300769 /*
770 * Target PEB had bit-flips or write error - torture it.
771 */
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200772 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200773 goto out_not_moved;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200774 }
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300775
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300776 if (err == MOVE_SOURCE_RD_ERR) {
777 /*
778 * An error happened while reading the source PEB. Do
779 * not switch to R/O mode in this case, and give the
780 * upper layers a possibility to recover from this,
781 * e.g. by unmapping corresponding LEB. Instead, just
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300782 * put this PEB to the @ubi->erroneous list to prevent
783 * UBI from trying to move it over and over again.
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300784 */
785 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
786 ubi_err("too many erroneous eraseblocks (%d)",
787 ubi->erroneous_peb_count);
788 goto out_error;
789 }
790 erroneous = 1;
791 goto out_not_moved;
792 }
793
Artem Bityutskiy90bf0262009-05-23 16:04:17 +0300794 if (err < 0)
795 goto out_error;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200796
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300797 ubi_assert(0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400798 }
799
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200800 /* The PEB has been successfully moved */
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200801 if (scrubbing)
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300802 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
803 e1->pnum, vol_id, lnum, e2->pnum);
804 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300805
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400806 spin_lock(&ubi->wl_lock);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200807 if (!ubi->move_to_put) {
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300808 wl_tree_add(e2, &ubi->used);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200809 e2 = NULL;
810 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400811 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200812 ubi->move_to_put = ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400813 spin_unlock(&ubi->wl_lock);
814
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200815 err = schedule_erase(ubi, e1, 0);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200816 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300817 kmem_cache_free(ubi_wl_entry_slab, e1);
Artem Bityutskiy21d08bb2009-06-08 19:28:18 +0300818 if (e2)
819 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300820 goto out_ro;
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200821 }
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200822
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200823 if (e2) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400824 /*
825 * Well, the target PEB was put meanwhile, schedule it for
826 * erasure.
827 */
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300828 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
829 e2->pnum, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400830 err = schedule_erase(ubi, e2, 0);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300831 if (err) {
832 kmem_cache_free(ubi_wl_entry_slab, e2);
833 goto out_ro;
834 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400835 }
836
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400837 dbg_wl("done");
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200838 mutex_unlock(&ubi->move_mutex);
839 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400840
841 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200842 * For some reasons the LEB was not moved, might be an error, might be
843 * something else. @e1 was not changed, so return it back. @e2 might
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200844 * have been changed, schedule it for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400845 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200846out_not_moved:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300847 if (vol_id != -1)
848 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
849 e1->pnum, vol_id, lnum, e2->pnum, err);
850 else
851 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
852 e1->pnum, e2->pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400853 spin_lock(&ubi->wl_lock);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300854 if (protect)
855 prot_queue_add(ubi, e1);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300856 else if (erroneous) {
857 wl_tree_add(e1, &ubi->erroneous);
858 ubi->erroneous_peb_count += 1;
859 } else if (scrubbing)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200860 wl_tree_add(e1, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400861 else
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300862 wl_tree_add(e1, &ubi->used);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200863 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400864 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200865 ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400866 spin_unlock(&ubi->wl_lock);
867
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300868 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200869 err = schedule_erase(ubi, e2, torture);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300870 if (err) {
871 kmem_cache_free(ubi_wl_entry_slab, e2);
872 goto out_ro;
873 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200874 mutex_unlock(&ubi->move_mutex);
875 return 0;
876
877out_error:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300878 if (vol_id != -1)
879 ubi_err("error %d while moving PEB %d to PEB %d",
880 err, e1->pnum, e2->pnum);
881 else
882 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
883 err, e1->pnum, vol_id, lnum, e2->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200884 spin_lock(&ubi->wl_lock);
885 ubi->move_from = ubi->move_to = NULL;
886 ubi->move_to_put = ubi->wl_scheduled = 0;
887 spin_unlock(&ubi->wl_lock);
888
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300889 ubi_free_vid_hdr(ubi, vid_hdr);
890 kmem_cache_free(ubi_wl_entry_slab, e1);
891 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200892
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300893out_ro:
894 ubi_ro_mode(ubi);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200895 mutex_unlock(&ubi->move_mutex);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300896 ubi_assert(err != 0);
897 return err < 0 ? err : -EIO;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200898
899out_cancel:
900 ubi->wl_scheduled = 0;
901 spin_unlock(&ubi->wl_lock);
902 mutex_unlock(&ubi->move_mutex);
903 ubi_free_vid_hdr(ubi, vid_hdr);
904 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400905}
906
907/**
908 * ensure_wear_leveling - schedule wear-leveling if it is needed.
909 * @ubi: UBI device description object
910 *
911 * This function checks if it is time to start wear-leveling and schedules it
912 * if yes. This function returns zero in case of success and a negative error
913 * code in case of failure.
914 */
915static int ensure_wear_leveling(struct ubi_device *ubi)
916{
917 int err = 0;
918 struct ubi_wl_entry *e1;
919 struct ubi_wl_entry *e2;
920 struct ubi_work *wrk;
921
922 spin_lock(&ubi->wl_lock);
923 if (ubi->wl_scheduled)
924 /* Wear-leveling is already in the work queue */
925 goto out_unlock;
926
927 /*
928 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
929 * the WL worker has to be scheduled anyway.
930 */
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300931 if (!ubi->scrub.rb_node) {
932 if (!ubi->used.rb_node || !ubi->free.rb_node)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400933 /* No physical eraseblocks - no deal */
934 goto out_unlock;
935
936 /*
937 * We schedule wear-leveling only if the difference between the
938 * lowest erase counter of used physical eraseblocks and a high
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200939 * erase counter of free physical eraseblocks is greater than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400940 * %UBI_WL_THRESHOLD.
941 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800942 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400943 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
944
945 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
946 goto out_unlock;
947 dbg_wl("schedule wear-leveling");
948 } else
949 dbg_wl("schedule scrubbing");
950
951 ubi->wl_scheduled = 1;
952 spin_unlock(&ubi->wl_lock);
953
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300954 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400955 if (!wrk) {
956 err = -ENOMEM;
957 goto out_cancel;
958 }
959
960 wrk->func = &wear_leveling_worker;
961 schedule_ubi_work(ubi, wrk);
962 return err;
963
964out_cancel:
965 spin_lock(&ubi->wl_lock);
966 ubi->wl_scheduled = 0;
967out_unlock:
968 spin_unlock(&ubi->wl_lock);
969 return err;
970}
971
972/**
973 * erase_worker - physical eraseblock erase worker function.
974 * @ubi: UBI device description object
975 * @wl_wrk: the work object
976 * @cancel: non-zero if the worker has to free memory and exit
977 *
978 * This function erases a physical eraseblock and perform torture testing if
979 * needed. It also takes care about marking the physical eraseblock bad if
980 * needed. Returns zero in case of success and a negative error code in case of
981 * failure.
982 */
983static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
984 int cancel)
985{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986 struct ubi_wl_entry *e = wl_wrk->e;
Artem Bityutskiy784c1452007-07-18 13:42:10 +0300987 int pnum = e->pnum, err, need;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400988
989 if (cancel) {
990 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
991 kfree(wl_wrk);
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +0200992 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400993 return 0;
994 }
995
996 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
997
998 err = sync_erase(ubi, e, wl_wrk->torture);
999 if (!err) {
1000 /* Fine, we've erased it successfully */
1001 kfree(wl_wrk);
1002
1003 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001004 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001005 spin_unlock(&ubi->wl_lock);
1006
1007 /*
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001008 * One more erase operation has happened, take care about
1009 * protected physical eraseblocks.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001010 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001011 serve_prot_queue(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001012
1013 /* And take care about wear-leveling */
1014 err = ensure_wear_leveling(ubi);
1015 return err;
1016 }
1017
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +03001018 ubi_err("failed to erase PEB %d, error %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001019 kfree(wl_wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001020
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001021 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1022 err == -EBUSY) {
1023 int err1;
1024
1025 /* Re-schedule the LEB for erasure */
1026 err1 = schedule_erase(ubi, e, 0);
1027 if (err1) {
1028 err = err1;
1029 goto out_ro;
1030 }
1031 return err;
Artem Bityutskiye57e0d82012-01-05 10:47:18 +02001032 }
1033
1034 kmem_cache_free(ubi_wl_entry_slab, e);
1035 if (err != -EIO)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001036 /*
1037 * If this is not %-EIO, we have no idea what to do. Scheduling
1038 * this physical eraseblock for erasure again would cause
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001039 * errors again and again. Well, lets switch to R/O mode.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001040 */
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001041 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001042
1043 /* It is %-EIO, the PEB went bad */
1044
1045 if (!ubi->bad_allowed) {
1046 ubi_err("bad physical eraseblock %d detected", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001047 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001048 }
1049
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001050 spin_lock(&ubi->volumes_lock);
1051 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1052 if (need > 0) {
1053 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1054 ubi->avail_pebs -= need;
1055 ubi->rsvd_pebs += need;
1056 ubi->beb_rsvd_pebs += need;
1057 if (need > 0)
1058 ubi_msg("reserve more %d PEBs", need);
1059 }
1060
1061 if (ubi->beb_rsvd_pebs == 0) {
1062 spin_unlock(&ubi->volumes_lock);
1063 ubi_err("no reserved physical eraseblocks");
1064 goto out_ro;
1065 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001066 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001067
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001068 ubi_msg("mark PEB %d as bad", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001069 err = ubi_io_mark_bad(ubi, pnum);
1070 if (err)
1071 goto out_ro;
1072
1073 spin_lock(&ubi->volumes_lock);
1074 ubi->beb_rsvd_pebs -= 1;
1075 ubi->bad_peb_count += 1;
1076 ubi->good_peb_count -= 1;
1077 ubi_calculate_reserved(ubi);
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001078 if (ubi->beb_rsvd_pebs)
1079 ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1080 else
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001081 ubi_warn("last PEB from the reserved pool was used");
1082 spin_unlock(&ubi->volumes_lock);
1083
1084 return err;
1085
1086out_ro:
1087 ubi_ro_mode(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001088 return err;
1089}
1090
1091/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001092 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001093 * @ubi: UBI device description object
1094 * @pnum: physical eraseblock to return
1095 * @torture: if this physical eraseblock has to be tortured
1096 *
1097 * This function is called to return physical eraseblock @pnum to the pool of
1098 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1099 * occurred to this @pnum and it has to be tested. This function returns zero
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001100 * in case of success, and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001101 */
1102int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1103{
1104 int err;
1105 struct ubi_wl_entry *e;
1106
1107 dbg_wl("PEB %d", pnum);
1108 ubi_assert(pnum >= 0);
1109 ubi_assert(pnum < ubi->peb_count);
1110
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001111retry:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001112 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001113 e = ubi->lookuptbl[pnum];
1114 if (e == ubi->move_from) {
1115 /*
1116 * User is putting the physical eraseblock which was selected to
1117 * be moved. It will be scheduled for erasure in the
1118 * wear-leveling worker.
1119 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001120 dbg_wl("PEB %d is being moved, wait", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001121 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001122
1123 /* Wait for the WL worker by taking the @ubi->move_mutex */
1124 mutex_lock(&ubi->move_mutex);
1125 mutex_unlock(&ubi->move_mutex);
1126 goto retry;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001127 } else if (e == ubi->move_to) {
1128 /*
1129 * User is putting the physical eraseblock which was selected
1130 * as the target the data is moved to. It may happen if the EBA
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001131 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1132 * but the WL sub-system has not put the PEB to the "used" tree
1133 * yet, but it is about to do this. So we just set a flag which
1134 * will tell the WL worker that the PEB is not needed anymore
1135 * and should be scheduled for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001136 */
1137 dbg_wl("PEB %d is the target of data moving", pnum);
1138 ubi_assert(!ubi->move_to_put);
1139 ubi->move_to_put = 1;
1140 spin_unlock(&ubi->wl_lock);
1141 return 0;
1142 } else {
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001143 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001144 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001145 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001146 } else if (in_wl_tree(e, &ubi->scrub)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001147 paranoid_check_in_wl_tree(ubi, e, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001148 rb_erase(&e->u.rb, &ubi->scrub);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001149 } else if (in_wl_tree(e, &ubi->erroneous)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001150 paranoid_check_in_wl_tree(ubi, e, &ubi->erroneous);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001151 rb_erase(&e->u.rb, &ubi->erroneous);
1152 ubi->erroneous_peb_count -= 1;
1153 ubi_assert(ubi->erroneous_peb_count >= 0);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001154 /* Erroneous PEBs should be tortured */
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001155 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001156 } else {
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001157 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001158 if (err) {
1159 ubi_err("PEB %d not found", pnum);
1160 ubi_ro_mode(ubi);
1161 spin_unlock(&ubi->wl_lock);
1162 return err;
1163 }
1164 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001165 }
1166 spin_unlock(&ubi->wl_lock);
1167
1168 err = schedule_erase(ubi, e, torture);
1169 if (err) {
1170 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001171 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001172 spin_unlock(&ubi->wl_lock);
1173 }
1174
1175 return err;
1176}
1177
1178/**
1179 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1180 * @ubi: UBI device description object
1181 * @pnum: the physical eraseblock to schedule
1182 *
1183 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1184 * needs scrubbing. This function schedules a physical eraseblock for
1185 * scrubbing which is done in background. This function returns zero in case of
1186 * success and a negative error code in case of failure.
1187 */
1188int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1189{
1190 struct ubi_wl_entry *e;
1191
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +03001192 dbg_msg("schedule PEB %d for scrubbing", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001193
1194retry:
1195 spin_lock(&ubi->wl_lock);
1196 e = ubi->lookuptbl[pnum];
Artem Bityutskiyd3f6e6c2010-08-29 23:34:44 +03001197 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1198 in_wl_tree(e, &ubi->erroneous)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001199 spin_unlock(&ubi->wl_lock);
1200 return 0;
1201 }
1202
1203 if (e == ubi->move_to) {
1204 /*
1205 * This physical eraseblock was used to move data to. The data
1206 * was moved but the PEB was not yet inserted to the proper
1207 * tree. We should just wait a little and let the WL worker
1208 * proceed.
1209 */
1210 spin_unlock(&ubi->wl_lock);
1211 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1212 yield();
1213 goto retry;
1214 }
1215
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001216 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001217 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001218 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001219 } else {
1220 int err;
1221
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001222 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001223 if (err) {
1224 ubi_err("PEB %d not found", pnum);
1225 ubi_ro_mode(ubi);
1226 spin_unlock(&ubi->wl_lock);
1227 return err;
1228 }
1229 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001230
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001231 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001232 spin_unlock(&ubi->wl_lock);
1233
1234 /*
1235 * Technically scrubbing is the same as wear-leveling, so it is done
1236 * by the WL worker.
1237 */
1238 return ensure_wear_leveling(ubi);
1239}
1240
1241/**
1242 * ubi_wl_flush - flush all pending works.
1243 * @ubi: UBI device description object
1244 *
1245 * This function returns zero in case of success and a negative error code in
1246 * case of failure.
1247 */
1248int ubi_wl_flush(struct ubi_device *ubi)
1249{
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001250 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001251
1252 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001253 * Erase while the pending works queue is not empty, but not more than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001254 * the number of currently pending works.
1255 */
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001256 dbg_wl("flush (%d pending works)", ubi->works_count);
1257 while (ubi->works_count) {
1258 err = do_work(ubi);
1259 if (err)
1260 return err;
1261 }
1262
1263 /*
1264 * Make sure all the works which have been done in parallel are
1265 * finished.
1266 */
1267 down_write(&ubi->work_sem);
1268 up_write(&ubi->work_sem);
1269
1270 /*
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +02001271 * And in case last was the WL worker and it canceled the LEB
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001272 * movement, flush again.
1273 */
1274 while (ubi->works_count) {
1275 dbg_wl("flush more (%d pending works)", ubi->works_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001276 err = do_work(ubi);
1277 if (err)
1278 return err;
1279 }
1280
1281 return 0;
1282}
1283
1284/**
1285 * tree_destroy - destroy an RB-tree.
1286 * @root: the root of the tree to destroy
1287 */
1288static void tree_destroy(struct rb_root *root)
1289{
1290 struct rb_node *rb;
1291 struct ubi_wl_entry *e;
1292
1293 rb = root->rb_node;
1294 while (rb) {
1295 if (rb->rb_left)
1296 rb = rb->rb_left;
1297 else if (rb->rb_right)
1298 rb = rb->rb_right;
1299 else {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001300 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001301
1302 rb = rb_parent(rb);
1303 if (rb) {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001304 if (rb->rb_left == &e->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001305 rb->rb_left = NULL;
1306 else
1307 rb->rb_right = NULL;
1308 }
1309
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001310 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001311 }
1312 }
1313}
1314
1315/**
1316 * ubi_thread - UBI background thread.
1317 * @u: the UBI device description object pointer
1318 */
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001319int ubi_thread(void *u)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001320{
1321 int failures = 0;
1322 struct ubi_device *ubi = u;
1323
1324 ubi_msg("background thread \"%s\" started, PID %d",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001325 ubi->bgt_name, task_pid_nr(current));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001326
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001327 set_freezable();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001328 for (;;) {
1329 int err;
1330
1331 if (kthread_should_stop())
Kyungmin Parkcadb40c2008-05-22 10:32:18 +09001332 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001333
1334 if (try_to_freeze())
1335 continue;
1336
1337 spin_lock(&ubi->wl_lock);
1338 if (list_empty(&ubi->works) || ubi->ro_mode ||
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +03001339 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001340 set_current_state(TASK_INTERRUPTIBLE);
1341 spin_unlock(&ubi->wl_lock);
1342 schedule();
1343 continue;
1344 }
1345 spin_unlock(&ubi->wl_lock);
1346
1347 err = do_work(ubi);
1348 if (err) {
1349 ubi_err("%s: work failed with error code %d",
1350 ubi->bgt_name, err);
1351 if (failures++ > WL_MAX_FAILURES) {
1352 /*
1353 * Too many failures, disable the thread and
1354 * switch to read-only mode.
1355 */
1356 ubi_msg("%s: %d consecutive failures",
1357 ubi->bgt_name, WL_MAX_FAILURES);
1358 ubi_ro_mode(ubi);
Vitaliy Gusev2ad49882008-11-05 18:27:18 +03001359 ubi->thread_enabled = 0;
1360 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001361 }
1362 } else
1363 failures = 0;
1364
1365 cond_resched();
1366 }
1367
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001368 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1369 return 0;
1370}
1371
1372/**
1373 * cancel_pending - cancel all pending works.
1374 * @ubi: UBI device description object
1375 */
1376static void cancel_pending(struct ubi_device *ubi)
1377{
1378 while (!list_empty(&ubi->works)) {
1379 struct ubi_work *wrk;
1380
1381 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1382 list_del(&wrk->list);
1383 wrk->func(ubi, wrk, 1);
1384 ubi->works_count -= 1;
1385 ubi_assert(ubi->works_count >= 0);
1386 }
1387}
1388
1389/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001390 * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001391 * @ubi: UBI device description object
1392 * @si: scanning information
1393 *
1394 * This function returns zero in case of success, and a negative error code in
1395 * case of failure.
1396 */
1397int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1398{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001399 int err, i;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001400 struct rb_node *rb1, *rb2;
1401 struct ubi_scan_volume *sv;
1402 struct ubi_scan_leb *seb, *tmp;
1403 struct ubi_wl_entry *e;
1404
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001405 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001406 spin_lock_init(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001407 mutex_init(&ubi->move_mutex);
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001408 init_rwsem(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001409 ubi->max_ec = si->max_ec;
1410 INIT_LIST_HEAD(&ubi->works);
1411
1412 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1413
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001414 err = -ENOMEM;
1415 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1416 if (!ubi->lookuptbl)
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001417 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001418
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001419 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1420 INIT_LIST_HEAD(&ubi->pq[i]);
1421 ubi->pq_head = 0;
1422
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001423 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1424 cond_resched();
1425
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001426 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001427 if (!e)
1428 goto out_free;
1429
1430 e->pnum = seb->pnum;
1431 e->ec = seb->ec;
1432 ubi->lookuptbl[e->pnum] = e;
1433 if (schedule_erase(ubi, e, 0)) {
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001434 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001435 goto out_free;
1436 }
1437 }
1438
1439 list_for_each_entry(seb, &si->free, u.list) {
1440 cond_resched();
1441
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001442 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001443 if (!e)
1444 goto out_free;
1445
1446 e->pnum = seb->pnum;
1447 e->ec = seb->ec;
1448 ubi_assert(e->ec >= 0);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001449 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001450 ubi->lookuptbl[e->pnum] = e;
1451 }
1452
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001453 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1454 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1455 cond_resched();
1456
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001457 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001458 if (!e)
1459 goto out_free;
1460
1461 e->pnum = seb->pnum;
1462 e->ec = seb->ec;
1463 ubi->lookuptbl[e->pnum] = e;
1464 if (!seb->scrub) {
1465 dbg_wl("add PEB %d EC %d to the used tree",
1466 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001467 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001468 } else {
1469 dbg_wl("add PEB %d EC %d to the scrub tree",
1470 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001471 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001472 }
1473 }
1474 }
1475
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001476 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001477 ubi_err("no enough physical eraseblocks (%d, need %d)",
1478 ubi->avail_pebs, WL_RESERVED_PEBS);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +03001479 if (ubi->corr_peb_count)
1480 ubi_err("%d PEBs are corrupted and not used",
1481 ubi->corr_peb_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001482 goto out_free;
1483 }
1484 ubi->avail_pebs -= WL_RESERVED_PEBS;
1485 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1486
1487 /* Schedule wear-leveling if needed */
1488 err = ensure_wear_leveling(ubi);
1489 if (err)
1490 goto out_free;
1491
1492 return 0;
1493
1494out_free:
1495 cancel_pending(ubi);
1496 tree_destroy(&ubi->used);
1497 tree_destroy(&ubi->free);
1498 tree_destroy(&ubi->scrub);
1499 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001500 return err;
1501}
1502
1503/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001504 * protection_queue_destroy - destroy the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001505 * @ubi: UBI device description object
1506 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001507static void protection_queue_destroy(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001508{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001509 int i;
1510 struct ubi_wl_entry *e, *tmp;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001511
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001512 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1513 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1514 list_del(&e->u.list);
1515 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001516 }
1517 }
1518}
1519
1520/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001521 * ubi_wl_close - close the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001522 * @ubi: UBI device description object
1523 */
1524void ubi_wl_close(struct ubi_device *ubi)
1525{
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001526 dbg_wl("close the WL sub-system");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001527 cancel_pending(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001528 protection_queue_destroy(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001529 tree_destroy(&ubi->used);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001530 tree_destroy(&ubi->erroneous);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001531 tree_destroy(&ubi->free);
1532 tree_destroy(&ubi->scrub);
1533 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001534}
1535
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001536#ifdef CONFIG_MTD_UBI_DEBUG
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001537
1538/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001539 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001540 * @ubi: UBI device description object
1541 * @pnum: the physical eraseblock number to check
1542 * @ec: the erase counter to check
1543 *
1544 * This function returns zero if the erase counter of physical eraseblock @pnum
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001545 * is equivalent to @ec, and a negative error code if not or if an error
1546 * occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001547 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001548static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001549{
1550 int err;
1551 long long read_ec;
1552 struct ubi_ec_hdr *ec_hdr;
1553
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001554 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001555 return 0;
1556
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001557 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001558 if (!ec_hdr)
1559 return -ENOMEM;
1560
1561 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1562 if (err && err != UBI_IO_BITFLIPS) {
1563 /* The header does not have to exist */
1564 err = 0;
1565 goto out_free;
1566 }
1567
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001568 read_ec = be64_to_cpu(ec_hdr->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001569 if (ec != read_ec) {
1570 ubi_err("paranoid check failed for PEB %d", pnum);
1571 ubi_err("read EC is %lld, should be %d", read_ec, ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001572 dump_stack();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001573 err = 1;
1574 } else
1575 err = 0;
1576
1577out_free:
1578 kfree(ec_hdr);
1579 return err;
1580}
1581
1582/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001583 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001584 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001585 * @e: the wear-leveling entry to check
1586 * @root: the root of the tree
1587 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001588 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1589 * is not.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001590 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001591static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
1592 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001593 struct rb_root *root)
1594{
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001595 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001596 return 0;
1597
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001598 if (in_wl_tree(e, root))
1599 return 0;
1600
1601 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1602 e->pnum, e->ec, root);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001603 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001604 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001605}
1606
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001607/**
1608 * paranoid_check_in_pq - check if wear-leveling entry is in the protection
1609 * queue.
1610 * @ubi: UBI device description object
1611 * @e: the wear-leveling entry to check
1612 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001613 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001614 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001615static int paranoid_check_in_pq(const struct ubi_device *ubi,
1616 struct ubi_wl_entry *e)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001617{
1618 struct ubi_wl_entry *p;
1619 int i;
1620
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001621 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001622 return 0;
1623
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001624 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1625 list_for_each_entry(p, &ubi->pq[i], u.list)
1626 if (p == e)
1627 return 0;
1628
1629 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue",
1630 e->pnum, e->ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001631 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001632 return -EINVAL;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001633}
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001634
1635#endif /* CONFIG_MTD_UBI_DEBUG */