blob: f99dbd08483859d02a3f862d5da39625c2cff864 [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 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030044 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
45 * bad.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040046 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030047 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
48 * in a physical eraseblock, it has to be moved. Technically this is the same
49 * as moving it for wear-leveling reasons.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040050 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030051 * As it was said, for the UBI sub-system all physical eraseblocks are either
52 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030053 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
54 * RB-trees, as well as (temporarily) in the @wl->pq queue.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080055 *
56 * When the WL sub-system returns a physical eraseblock, the physical
57 * eraseblock is protected from being moved for some "time". For this reason,
58 * the physical eraseblock is not directly moved from the @wl->free tree to the
59 * @wl->used tree. There is a protection queue in between where this
60 * physical eraseblock is temporarily stored (@wl->pq).
61 *
62 * All this protection stuff is needed because:
63 * o we don't want to move physical eraseblocks just after we have given them
64 * to the user; instead, we first want to let users fill them up with data;
65 *
66 * o there is a chance that the user will put the physical eraseblock very
Artem Bityutskiy44156262012-05-14 19:49:35 +030067 * soon, so it makes sense not to move it for some time, but wait.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080068 *
69 * Physical eraseblocks stay protected only for limited time. But the "time" is
70 * measured in erase cycles in this case. This is implemented with help of the
71 * protection queue. Eraseblocks are put to the tail of this queue when they
72 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
73 * head of the queue on each erase operation (for any eraseblock). So the
74 * length of the queue defines how may (global) erase cycles PEBs are protected.
75 *
76 * To put it differently, each physical eraseblock has 2 main states: free and
77 * used. The former state corresponds to the @wl->free tree. The latter state
78 * is split up on several sub-states:
79 * o the WL movement is allowed (@wl->used tree);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +030080 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030081 * erroneous - e.g., there was a read error;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080082 * o the WL movement is temporarily prohibited (@wl->pq queue);
83 * o scrubbing is needed (@wl->scrub tree).
84 *
85 * Depending on the sub-state, wear-leveling entries of the used physical
86 * eraseblocks may be kept in one of those structures.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040087 *
88 * Note, in this implementation, we keep a small in-RAM object for each physical
89 * eraseblock. This is surely not a scalable solution. But it appears to be good
90 * enough for moderately large flashes and it is simple. In future, one may
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030091 * re-work this sub-system and make it more scalable.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040092 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030093 * At the moment this sub-system does not utilize the sequence number, which
94 * was introduced relatively recently. But it would be wise to do this because
95 * the sequence number of a logical eraseblock characterizes how old is it. For
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040096 * example, when we move a PEB with low erase counter, and we need to pick the
97 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
98 * pick target PEB with an average EC if our PEB is not very "old". This is a
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030099 * room for future re-works of the WL sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400100 */
101
102#include <linux/slab.h>
103#include <linux/crc32.h>
104#include <linux/freezer.h>
105#include <linux/kthread.h>
106#include "ubi.h"
107
108/* Number of physical eraseblocks reserved for wear-leveling purposes */
109#define WL_RESERVED_PEBS 1
110
111/*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400112 * Maximum difference between two erase counters. If this threshold is
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300113 * exceeded, the WL sub-system starts moving data from used physical
114 * eraseblocks with low erase counter to free physical eraseblocks with high
115 * erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400116 */
117#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
118
119/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300120 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400121 * physical eraseblock to move to. The simplest way would be just to pick the
122 * one with the highest erase counter. But in certain workloads this could lead
123 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
124 * situation when the picked physical eraseblock is constantly erased after the
125 * data is written to it. So, we have a constant which limits the highest erase
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300126 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200127 * does not pick eraseblocks with erase counter greater than the lowest erase
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400128 * counter plus %WL_FREE_MAX_DIFF.
129 */
130#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
131
132/*
133 * Maximum number of consecutive background thread failures which is enough to
134 * switch to read-only mode.
135 */
136#define WL_MAX_FAILURES 32
137
138/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400139 * struct ubi_work - UBI work description data structure.
140 * @list: a link in the list of pending works
141 * @func: worker function
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400142 * @e: physical eraseblock to erase
143 * @torture: if the physical eraseblock has to be tortured
144 *
145 * The @func pointer points to the worker function. If the @cancel argument is
146 * not zero, the worker has to free the resources and exit immediately. The
147 * worker has to return zero in case of success and a negative error code in
148 * case of failure.
149 */
150struct ubi_work {
151 struct list_head list;
152 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
153 /* The below fields are only relevant to erasure works */
154 struct ubi_wl_entry *e;
155 int torture;
156};
157
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300158static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300159static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
160 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400161 struct rb_root *root);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300162static int paranoid_check_in_pq(const struct ubi_device *ubi,
163 struct ubi_wl_entry *e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400164
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400165/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400166 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
167 * @e: the wear-leveling entry to add
168 * @root: the root of the tree
169 *
170 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
171 * the @ubi->used and @ubi->free RB-trees.
172 */
173static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
174{
175 struct rb_node **p, *parent = NULL;
176
177 p = &root->rb_node;
178 while (*p) {
179 struct ubi_wl_entry *e1;
180
181 parent = *p;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800182 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400183
184 if (e->ec < e1->ec)
185 p = &(*p)->rb_left;
186 else if (e->ec > e1->ec)
187 p = &(*p)->rb_right;
188 else {
189 ubi_assert(e->pnum != e1->pnum);
190 if (e->pnum < e1->pnum)
191 p = &(*p)->rb_left;
192 else
193 p = &(*p)->rb_right;
194 }
195 }
196
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800197 rb_link_node(&e->u.rb, parent, p);
198 rb_insert_color(&e->u.rb, root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400199}
200
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400201/**
202 * do_work - do one pending work.
203 * @ubi: UBI device description object
204 *
205 * This function returns zero in case of success and a negative error code in
206 * case of failure.
207 */
208static int do_work(struct ubi_device *ubi)
209{
210 int err;
211 struct ubi_work *wrk;
212
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200213 cond_resched();
214
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200215 /*
216 * @ubi->work_sem is used to synchronize with the workers. Workers take
217 * it in read mode, so many of them may be doing works at a time. But
218 * the queue flush code has to be sure the whole queue of works is
219 * done, and it takes the mutex in write mode.
220 */
221 down_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400222 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400223 if (list_empty(&ubi->works)) {
224 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200225 up_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400226 return 0;
227 }
228
229 wrk = list_entry(ubi->works.next, struct ubi_work, list);
230 list_del(&wrk->list);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200231 ubi->works_count -= 1;
232 ubi_assert(ubi->works_count >= 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400233 spin_unlock(&ubi->wl_lock);
234
235 /*
236 * Call the worker function. Do not touch the work structure
237 * after this call as it will have been freed or reused by that
238 * time by the worker function.
239 */
240 err = wrk->func(ubi, wrk, 0);
241 if (err)
242 ubi_err("work failed with error code %d", err);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200243 up_read(&ubi->work_sem);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200244
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400245 return err;
246}
247
248/**
249 * produce_free_peb - produce a free physical eraseblock.
250 * @ubi: UBI device description object
251 *
252 * This function tries to make a free PEB by means of synchronous execution of
253 * pending works. This may be needed if, for example the background thread is
254 * disabled. Returns zero in case of success and a negative error code in case
255 * of failure.
256 */
257static int produce_free_peb(struct ubi_device *ubi)
258{
259 int err;
260
261 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300262 while (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400263 spin_unlock(&ubi->wl_lock);
264
265 dbg_wl("do one work synchronously");
266 err = do_work(ubi);
267 if (err)
268 return err;
269
270 spin_lock(&ubi->wl_lock);
271 }
272 spin_unlock(&ubi->wl_lock);
273
274 return 0;
275}
276
277/**
278 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
279 * @e: the wear-leveling entry to check
280 * @root: the root of the tree
281 *
282 * This function returns non-zero if @e is in the @root RB-tree and zero if it
283 * is not.
284 */
285static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
286{
287 struct rb_node *p;
288
289 p = root->rb_node;
290 while (p) {
291 struct ubi_wl_entry *e1;
292
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800293 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400294
295 if (e->pnum == e1->pnum) {
296 ubi_assert(e == e1);
297 return 1;
298 }
299
300 if (e->ec < e1->ec)
301 p = p->rb_left;
302 else if (e->ec > e1->ec)
303 p = p->rb_right;
304 else {
305 ubi_assert(e->pnum != e1->pnum);
306 if (e->pnum < e1->pnum)
307 p = p->rb_left;
308 else
309 p = p->rb_right;
310 }
311 }
312
313 return 0;
314}
315
316/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800317 * prot_queue_add - add physical eraseblock to the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400318 * @ubi: UBI device description object
319 * @e: the physical eraseblock to add
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400320 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800321 * This function adds @e to the tail of the protection queue @ubi->pq, where
322 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
323 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
324 * be locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400325 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800326static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400327{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800328 int pq_tail = ubi->pq_head - 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800330 if (pq_tail < 0)
331 pq_tail = UBI_PROT_QUEUE_LEN - 1;
332 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
333 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
334 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400335}
336
337/**
338 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
339 * @root: the RB-tree where to look for
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200340 * @diff: maximum possible difference from the smallest erase counter
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400341 *
342 * This function looks for a wear leveling entry with erase counter closest to
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200343 * min + @diff, where min is the smallest erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400344 */
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200345static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400346{
347 struct rb_node *p;
348 struct ubi_wl_entry *e;
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200349 int max;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400350
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800351 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200352 max = e->ec + diff;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400353
354 p = root->rb_node;
355 while (p) {
356 struct ubi_wl_entry *e1;
357
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800358 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359 if (e1->ec >= max)
360 p = p->rb_left;
361 else {
362 p = p->rb_right;
363 e = e1;
364 }
365 }
366
367 return e;
368}
369
370/**
371 * ubi_wl_get_peb - get a physical eraseblock.
372 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400373 *
374 * This function returns a physical eraseblock in case of success and a
375 * negative error code in case of failure. Might sleep.
376 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200377int ubi_wl_get_peb(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400378{
Artem Bityutskiy7eb3aa652012-03-07 19:08:36 +0200379 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380 struct ubi_wl_entry *e, *first, *last;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400381
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400382retry:
383 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300384 if (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400385 if (ubi->works_count == 0) {
386 ubi_assert(list_empty(&ubi->works));
387 ubi_err("no free eraseblocks");
388 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400389 return -ENOSPC;
390 }
391 spin_unlock(&ubi->wl_lock);
392
393 err = produce_free_peb(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800394 if (err < 0)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400396 goto retry;
397 }
398
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200399 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
400 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200402 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
403 e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb);
404 else
405 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400406
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300407 paranoid_check_in_wl_tree(ubi, e, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800408
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800410 * Move the physical eraseblock to the protection queue where it will
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400411 * be protected from being moved for some time.
412 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800413 rb_erase(&e->u.rb, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800414 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
415 prot_queue_add(ubi, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400416 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300417
418 err = ubi_dbg_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
419 ubi->peb_size - ubi->vid_hdr_aloffset);
420 if (err) {
Artem Bityutskiy13987882009-06-29 15:58:36 +0300421 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200422 return err;
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300423 }
424
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400425 return e->pnum;
426}
427
428/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800429 * prot_queue_del - remove a physical eraseblock from the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400430 * @ubi: UBI device description object
431 * @pnum: the physical eraseblock to remove
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200432 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800433 * This function deletes PEB @pnum from the protection queue and returns zero
434 * in case of success and %-ENODEV if the PEB was not found.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400435 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800436static int prot_queue_del(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400437{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800438 struct ubi_wl_entry *e;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400439
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800440 e = ubi->lookuptbl[pnum];
441 if (!e)
442 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800444 if (paranoid_check_in_pq(ubi, e))
445 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400446
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800447 list_del(&e->u.list);
448 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200449 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400450}
451
452/**
453 * sync_erase - synchronously erase a physical eraseblock.
454 * @ubi: UBI device description object
455 * @e: the the physical eraseblock to erase
456 * @torture: if the physical eraseblock has to be tortured
457 *
458 * This function returns zero in case of success and a negative error code in
459 * case of failure.
460 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300461static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
462 int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400463{
464 int err;
465 struct ubi_ec_hdr *ec_hdr;
466 unsigned long long ec = e->ec;
467
468 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
469
470 err = paranoid_check_ec(ubi, e->pnum, e->ec);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200471 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400472 return -EINVAL;
473
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300474 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400475 if (!ec_hdr)
476 return -ENOMEM;
477
478 err = ubi_io_sync_erase(ubi, e->pnum, torture);
479 if (err < 0)
480 goto out_free;
481
482 ec += err;
483 if (ec > UBI_MAX_ERASECOUNTER) {
484 /*
485 * Erase counter overflow. Upgrade UBI and use 64-bit
486 * erase counters internally.
487 */
488 ubi_err("erase counter overflow at PEB %d, EC %llu",
489 e->pnum, ec);
490 err = -EINVAL;
491 goto out_free;
492 }
493
494 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
495
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300496 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400497
498 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
499 if (err)
500 goto out_free;
501
502 e->ec = ec;
503 spin_lock(&ubi->wl_lock);
504 if (e->ec > ubi->max_ec)
505 ubi->max_ec = e->ec;
506 spin_unlock(&ubi->wl_lock);
507
508out_free:
509 kfree(ec_hdr);
510 return err;
511}
512
513/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800514 * serve_prot_queue - check if it is time to stop protecting PEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400515 * @ubi: UBI device description object
516 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800517 * This function is called after each erase operation and removes PEBs from the
518 * tail of the protection queue. These PEBs have been protected for long enough
519 * and should be moved to the used tree.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400520 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800521static void serve_prot_queue(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400522{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800523 struct ubi_wl_entry *e, *tmp;
524 int count;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400525
526 /*
527 * There may be several protected physical eraseblock to remove,
528 * process them all.
529 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800530repeat:
531 count = 0;
532 spin_lock(&ubi->wl_lock);
533 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
534 dbg_wl("PEB %d EC %d protection over, move to used tree",
535 e->pnum, e->ec);
536
537 list_del(&e->u.list);
538 wl_tree_add(e, &ubi->used);
539 if (count++ > 32) {
540 /*
541 * Let's be nice and avoid holding the spinlock for
542 * too long.
543 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400544 spin_unlock(&ubi->wl_lock);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800545 cond_resched();
546 goto repeat;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400547 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400548 }
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800549
550 ubi->pq_head += 1;
551 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
552 ubi->pq_head = 0;
553 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
554 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400555}
556
557/**
558 * schedule_ubi_work - schedule a work.
559 * @ubi: UBI device description object
560 * @wrk: the work to schedule
561 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800562 * This function adds a work defined by @wrk to the tail of the pending works
563 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400564 */
565static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
566{
567 spin_lock(&ubi->wl_lock);
568 list_add_tail(&wrk->list, &ubi->works);
569 ubi_assert(ubi->works_count >= 0);
570 ubi->works_count += 1;
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +0300571 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400572 wake_up_process(ubi->bgt_thread);
573 spin_unlock(&ubi->wl_lock);
574}
575
576static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
577 int cancel);
578
579/**
580 * schedule_erase - schedule an erase work.
581 * @ubi: UBI device description object
582 * @e: the WL entry of the physical eraseblock to erase
583 * @torture: if the physical eraseblock has to be tortured
584 *
585 * This function returns zero in case of success and a %-ENOMEM in case of
586 * failure.
587 */
588static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
589 int torture)
590{
591 struct ubi_work *wl_wrk;
592
593 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
594 e->pnum, e->ec, torture);
595
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300596 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400597 if (!wl_wrk)
598 return -ENOMEM;
599
600 wl_wrk->func = &erase_worker;
601 wl_wrk->e = e;
602 wl_wrk->torture = torture;
603
604 schedule_ubi_work(ubi, wl_wrk);
605 return 0;
606}
607
608/**
609 * wear_leveling_worker - wear-leveling worker function.
610 * @ubi: UBI device description object
611 * @wrk: the work object
612 * @cancel: non-zero if the worker has to free memory and exit
613 *
614 * This function copies a more worn out physical eraseblock to a less worn out
615 * one. Returns zero in case of success and a negative error code in case of
616 * failure.
617 */
618static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
619 int cancel)
620{
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300621 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300622 int vol_id = -1, uninitialized_var(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400623 struct ubi_wl_entry *e1, *e2;
624 struct ubi_vid_hdr *vid_hdr;
625
626 kfree(wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400627 if (cancel)
628 return 0;
629
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300630 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400631 if (!vid_hdr)
632 return -ENOMEM;
633
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200634 mutex_lock(&ubi->move_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400635 spin_lock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200636 ubi_assert(!ubi->move_from && !ubi->move_to);
637 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400638
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200639 if (!ubi->free.rb_node ||
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300640 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400641 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200642 * No free physical eraseblocks? Well, they must be waiting in
643 * the queue to be erased. Cancel movement - it will be
644 * triggered again when a free physical eraseblock appears.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400645 *
646 * No used physical eraseblocks? They must be temporarily
647 * protected from being moved. They will be moved to the
648 * @ubi->used tree later and the wear-leveling will be
649 * triggered again.
650 */
651 dbg_wl("cancel WL, a list is empty: free %d, used %d",
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300652 !ubi->free.rb_node, !ubi->used.rb_node);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200653 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400654 }
655
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300656 if (!ubi->scrub.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400657 /*
658 * Now pick the least worn-out used physical eraseblock and a
659 * highly worn-out free physical eraseblock. If the erase
660 * counters differ much enough, start wear-leveling.
661 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800662 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
664
665 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
666 dbg_wl("no WL needed: min used EC %d, max free EC %d",
667 e1->ec, e2->ec);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200668 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 }
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300670 paranoid_check_in_wl_tree(ubi, e1, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800671 rb_erase(&e1->u.rb, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400672 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
673 e1->pnum, e1->ec, e2->pnum, e2->ec);
674 } else {
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200675 /* Perform scrubbing */
676 scrubbing = 1;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800677 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400678 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300679 paranoid_check_in_wl_tree(ubi, e1, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800680 rb_erase(&e1->u.rb, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400681 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
682 }
683
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300684 paranoid_check_in_wl_tree(ubi, e2, &ubi->free);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800685 rb_erase(&e2->u.rb, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400686 ubi->move_from = e1;
687 ubi->move_to = e2;
688 spin_unlock(&ubi->wl_lock);
689
690 /*
691 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
692 * We so far do not know which logical eraseblock our physical
693 * eraseblock (@e1) belongs to. We have to read the volume identifier
694 * header first.
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200695 *
696 * Note, we are protected from this PEB being unmapped and erased. The
697 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
698 * which is being moved was unmapped.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400699 */
700
701 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
702 if (err && err != UBI_IO_BITFLIPS) {
Artem Bityutskiy74d82d22010-09-03 02:11:20 +0300703 if (err == UBI_IO_FF) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400704 /*
705 * We are trying to move PEB without a VID header. UBI
706 * always write VID headers shortly after the PEB was
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300707 * given, so we have a situation when it has not yet
708 * had a chance to write it, because it was preempted.
709 * So add this PEB to the protection queue so far,
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300710 * because presumably more data will be written there
711 * (including the missing VID header), and then we'll
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300712 * move it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400713 */
714 dbg_wl("PEB %d has no VID header", e1->pnum);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300715 protect = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200716 goto out_not_moved;
Artem Bityutskiy92e1a7d2010-09-03 14:22:17 +0300717 } else if (err == UBI_IO_FF_BITFLIPS) {
718 /*
719 * The same situation as %UBI_IO_FF, but bit-flips were
720 * detected. It is better to schedule this PEB for
721 * scrubbing.
722 */
723 dbg_wl("PEB %d has no VID header but has bit-flips",
724 e1->pnum);
725 scrubbing = 1;
726 goto out_not_moved;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400727 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200728
729 ubi_err("error %d while reading VID header from PEB %d",
730 err, e1->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200731 goto out_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400732 }
733
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300734 vol_id = be32_to_cpu(vid_hdr->vol_id);
735 lnum = be32_to_cpu(vid_hdr->lnum);
736
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400737 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
738 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300739 if (err == MOVE_CANCEL_RACE) {
740 /*
741 * The LEB has not been moved because the volume is
742 * being deleted or the PEB has been put meanwhile. We
743 * should prevent this PEB from being selected for
744 * wear-leveling movement again, so put it to the
745 * protection queue.
746 */
747 protect = 1;
748 goto out_not_moved;
749 }
Bhavesh Parekhe801e122011-11-30 17:43:42 +0530750 if (err == MOVE_RETRY) {
751 scrubbing = 1;
752 goto out_not_moved;
753 }
Artem Bityutskiycc831462012-03-09 10:31:18 +0200754 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300755 err == MOVE_TARGET_RD_ERR) {
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300756 /*
757 * Target PEB had bit-flips or write error - torture it.
758 */
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200759 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200760 goto out_not_moved;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200761 }
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300762
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300763 if (err == MOVE_SOURCE_RD_ERR) {
764 /*
765 * An error happened while reading the source PEB. Do
766 * not switch to R/O mode in this case, and give the
767 * upper layers a possibility to recover from this,
768 * e.g. by unmapping corresponding LEB. Instead, just
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300769 * put this PEB to the @ubi->erroneous list to prevent
770 * UBI from trying to move it over and over again.
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300771 */
772 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
773 ubi_err("too many erroneous eraseblocks (%d)",
774 ubi->erroneous_peb_count);
775 goto out_error;
776 }
777 erroneous = 1;
778 goto out_not_moved;
779 }
780
Artem Bityutskiy90bf0262009-05-23 16:04:17 +0300781 if (err < 0)
782 goto out_error;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200783
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300784 ubi_assert(0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400785 }
786
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200787 /* The PEB has been successfully moved */
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200788 if (scrubbing)
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300789 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
790 e1->pnum, vol_id, lnum, e2->pnum);
791 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300792
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400793 spin_lock(&ubi->wl_lock);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200794 if (!ubi->move_to_put) {
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300795 wl_tree_add(e2, &ubi->used);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200796 e2 = NULL;
797 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400798 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200799 ubi->move_to_put = ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400800 spin_unlock(&ubi->wl_lock);
801
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200802 err = schedule_erase(ubi, e1, 0);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200803 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300804 kmem_cache_free(ubi_wl_entry_slab, e1);
Artem Bityutskiy21d08bb2009-06-08 19:28:18 +0300805 if (e2)
806 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300807 goto out_ro;
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200808 }
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200809
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200810 if (e2) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400811 /*
812 * Well, the target PEB was put meanwhile, schedule it for
813 * erasure.
814 */
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300815 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
816 e2->pnum, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400817 err = schedule_erase(ubi, e2, 0);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300818 if (err) {
819 kmem_cache_free(ubi_wl_entry_slab, e2);
820 goto out_ro;
821 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400822 }
823
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400824 dbg_wl("done");
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200825 mutex_unlock(&ubi->move_mutex);
826 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400827
828 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200829 * For some reasons the LEB was not moved, might be an error, might be
830 * something else. @e1 was not changed, so return it back. @e2 might
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200831 * have been changed, schedule it for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400832 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200833out_not_moved:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300834 if (vol_id != -1)
835 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
836 e1->pnum, vol_id, lnum, e2->pnum, err);
837 else
838 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
839 e1->pnum, e2->pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400840 spin_lock(&ubi->wl_lock);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300841 if (protect)
842 prot_queue_add(ubi, e1);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300843 else if (erroneous) {
844 wl_tree_add(e1, &ubi->erroneous);
845 ubi->erroneous_peb_count += 1;
846 } else if (scrubbing)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200847 wl_tree_add(e1, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400848 else
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300849 wl_tree_add(e1, &ubi->used);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200850 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400851 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200852 ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400853 spin_unlock(&ubi->wl_lock);
854
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300855 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200856 err = schedule_erase(ubi, e2, torture);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300857 if (err) {
858 kmem_cache_free(ubi_wl_entry_slab, e2);
859 goto out_ro;
860 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200861 mutex_unlock(&ubi->move_mutex);
862 return 0;
863
864out_error:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300865 if (vol_id != -1)
866 ubi_err("error %d while moving PEB %d to PEB %d",
867 err, e1->pnum, e2->pnum);
868 else
869 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
870 err, e1->pnum, vol_id, lnum, e2->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200871 spin_lock(&ubi->wl_lock);
872 ubi->move_from = ubi->move_to = NULL;
873 ubi->move_to_put = ubi->wl_scheduled = 0;
874 spin_unlock(&ubi->wl_lock);
875
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300876 ubi_free_vid_hdr(ubi, vid_hdr);
877 kmem_cache_free(ubi_wl_entry_slab, e1);
878 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200879
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300880out_ro:
881 ubi_ro_mode(ubi);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200882 mutex_unlock(&ubi->move_mutex);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300883 ubi_assert(err != 0);
884 return err < 0 ? err : -EIO;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200885
886out_cancel:
887 ubi->wl_scheduled = 0;
888 spin_unlock(&ubi->wl_lock);
889 mutex_unlock(&ubi->move_mutex);
890 ubi_free_vid_hdr(ubi, vid_hdr);
891 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400892}
893
894/**
895 * ensure_wear_leveling - schedule wear-leveling if it is needed.
896 * @ubi: UBI device description object
897 *
898 * This function checks if it is time to start wear-leveling and schedules it
899 * if yes. This function returns zero in case of success and a negative error
900 * code in case of failure.
901 */
902static int ensure_wear_leveling(struct ubi_device *ubi)
903{
904 int err = 0;
905 struct ubi_wl_entry *e1;
906 struct ubi_wl_entry *e2;
907 struct ubi_work *wrk;
908
909 spin_lock(&ubi->wl_lock);
910 if (ubi->wl_scheduled)
911 /* Wear-leveling is already in the work queue */
912 goto out_unlock;
913
914 /*
915 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
916 * the WL worker has to be scheduled anyway.
917 */
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300918 if (!ubi->scrub.rb_node) {
919 if (!ubi->used.rb_node || !ubi->free.rb_node)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400920 /* No physical eraseblocks - no deal */
921 goto out_unlock;
922
923 /*
924 * We schedule wear-leveling only if the difference between the
925 * lowest erase counter of used physical eraseblocks and a high
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200926 * erase counter of free physical eraseblocks is greater than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400927 * %UBI_WL_THRESHOLD.
928 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800929 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400930 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
931
932 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
933 goto out_unlock;
934 dbg_wl("schedule wear-leveling");
935 } else
936 dbg_wl("schedule scrubbing");
937
938 ubi->wl_scheduled = 1;
939 spin_unlock(&ubi->wl_lock);
940
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300941 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400942 if (!wrk) {
943 err = -ENOMEM;
944 goto out_cancel;
945 }
946
947 wrk->func = &wear_leveling_worker;
948 schedule_ubi_work(ubi, wrk);
949 return err;
950
951out_cancel:
952 spin_lock(&ubi->wl_lock);
953 ubi->wl_scheduled = 0;
954out_unlock:
955 spin_unlock(&ubi->wl_lock);
956 return err;
957}
958
959/**
960 * erase_worker - physical eraseblock erase worker function.
961 * @ubi: UBI device description object
962 * @wl_wrk: the work object
963 * @cancel: non-zero if the worker has to free memory and exit
964 *
965 * This function erases a physical eraseblock and perform torture testing if
966 * needed. It also takes care about marking the physical eraseblock bad if
967 * needed. Returns zero in case of success and a negative error code in case of
968 * failure.
969 */
970static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
971 int cancel)
972{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400973 struct ubi_wl_entry *e = wl_wrk->e;
Artem Bityutskiy784c1452007-07-18 13:42:10 +0300974 int pnum = e->pnum, err, need;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400975
976 if (cancel) {
977 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
978 kfree(wl_wrk);
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +0200979 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400980 return 0;
981 }
982
983 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
984
985 err = sync_erase(ubi, e, wl_wrk->torture);
986 if (!err) {
987 /* Fine, we've erased it successfully */
988 kfree(wl_wrk);
989
990 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300991 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400992 spin_unlock(&ubi->wl_lock);
993
994 /*
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300995 * One more erase operation has happened, take care about
996 * protected physical eraseblocks.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400997 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800998 serve_prot_queue(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400999
1000 /* And take care about wear-leveling */
1001 err = ensure_wear_leveling(ubi);
1002 return err;
1003 }
1004
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +03001005 ubi_err("failed to erase PEB %d, error %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001006 kfree(wl_wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001007
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001008 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1009 err == -EBUSY) {
1010 int err1;
1011
1012 /* Re-schedule the LEB for erasure */
1013 err1 = schedule_erase(ubi, e, 0);
1014 if (err1) {
1015 err = err1;
1016 goto out_ro;
1017 }
1018 return err;
Artem Bityutskiye57e0d82012-01-05 10:47:18 +02001019 }
1020
1021 kmem_cache_free(ubi_wl_entry_slab, e);
1022 if (err != -EIO)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001023 /*
1024 * If this is not %-EIO, we have no idea what to do. Scheduling
1025 * this physical eraseblock for erasure again would cause
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001026 * errors again and again. Well, lets switch to R/O mode.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001027 */
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001028 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001029
1030 /* It is %-EIO, the PEB went bad */
1031
1032 if (!ubi->bad_allowed) {
1033 ubi_err("bad physical eraseblock %d detected", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001034 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001035 }
1036
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001037 spin_lock(&ubi->volumes_lock);
1038 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1039 if (need > 0) {
1040 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1041 ubi->avail_pebs -= need;
1042 ubi->rsvd_pebs += need;
1043 ubi->beb_rsvd_pebs += need;
1044 if (need > 0)
1045 ubi_msg("reserve more %d PEBs", need);
1046 }
1047
1048 if (ubi->beb_rsvd_pebs == 0) {
1049 spin_unlock(&ubi->volumes_lock);
1050 ubi_err("no reserved physical eraseblocks");
1051 goto out_ro;
1052 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001053 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001054
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001055 ubi_msg("mark PEB %d as bad", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001056 err = ubi_io_mark_bad(ubi, pnum);
1057 if (err)
1058 goto out_ro;
1059
1060 spin_lock(&ubi->volumes_lock);
1061 ubi->beb_rsvd_pebs -= 1;
1062 ubi->bad_peb_count += 1;
1063 ubi->good_peb_count -= 1;
1064 ubi_calculate_reserved(ubi);
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001065 if (ubi->beb_rsvd_pebs)
1066 ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1067 else
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001068 ubi_warn("last PEB from the reserved pool was used");
1069 spin_unlock(&ubi->volumes_lock);
1070
1071 return err;
1072
1073out_ro:
1074 ubi_ro_mode(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001075 return err;
1076}
1077
1078/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001079 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001080 * @ubi: UBI device description object
1081 * @pnum: physical eraseblock to return
1082 * @torture: if this physical eraseblock has to be tortured
1083 *
1084 * This function is called to return physical eraseblock @pnum to the pool of
1085 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1086 * occurred to this @pnum and it has to be tested. This function returns zero
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001087 * in case of success, and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001088 */
1089int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1090{
1091 int err;
1092 struct ubi_wl_entry *e;
1093
1094 dbg_wl("PEB %d", pnum);
1095 ubi_assert(pnum >= 0);
1096 ubi_assert(pnum < ubi->peb_count);
1097
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001098retry:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001099 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001100 e = ubi->lookuptbl[pnum];
1101 if (e == ubi->move_from) {
1102 /*
1103 * User is putting the physical eraseblock which was selected to
1104 * be moved. It will be scheduled for erasure in the
1105 * wear-leveling worker.
1106 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001107 dbg_wl("PEB %d is being moved, wait", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001108 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001109
1110 /* Wait for the WL worker by taking the @ubi->move_mutex */
1111 mutex_lock(&ubi->move_mutex);
1112 mutex_unlock(&ubi->move_mutex);
1113 goto retry;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001114 } else if (e == ubi->move_to) {
1115 /*
1116 * User is putting the physical eraseblock which was selected
1117 * as the target the data is moved to. It may happen if the EBA
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001118 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1119 * but the WL sub-system has not put the PEB to the "used" tree
1120 * yet, but it is about to do this. So we just set a flag which
1121 * will tell the WL worker that the PEB is not needed anymore
1122 * and should be scheduled for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001123 */
1124 dbg_wl("PEB %d is the target of data moving", pnum);
1125 ubi_assert(!ubi->move_to_put);
1126 ubi->move_to_put = 1;
1127 spin_unlock(&ubi->wl_lock);
1128 return 0;
1129 } else {
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001130 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001131 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001132 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001133 } else if (in_wl_tree(e, &ubi->scrub)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001134 paranoid_check_in_wl_tree(ubi, e, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001135 rb_erase(&e->u.rb, &ubi->scrub);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001136 } else if (in_wl_tree(e, &ubi->erroneous)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001137 paranoid_check_in_wl_tree(ubi, e, &ubi->erroneous);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001138 rb_erase(&e->u.rb, &ubi->erroneous);
1139 ubi->erroneous_peb_count -= 1;
1140 ubi_assert(ubi->erroneous_peb_count >= 0);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001141 /* Erroneous PEBs should be tortured */
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001142 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001143 } else {
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001144 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001145 if (err) {
1146 ubi_err("PEB %d not found", pnum);
1147 ubi_ro_mode(ubi);
1148 spin_unlock(&ubi->wl_lock);
1149 return err;
1150 }
1151 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001152 }
1153 spin_unlock(&ubi->wl_lock);
1154
1155 err = schedule_erase(ubi, e, torture);
1156 if (err) {
1157 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001158 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001159 spin_unlock(&ubi->wl_lock);
1160 }
1161
1162 return err;
1163}
1164
1165/**
1166 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1167 * @ubi: UBI device description object
1168 * @pnum: the physical eraseblock to schedule
1169 *
1170 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1171 * needs scrubbing. This function schedules a physical eraseblock for
1172 * scrubbing which is done in background. This function returns zero in case of
1173 * success and a negative error code in case of failure.
1174 */
1175int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1176{
1177 struct ubi_wl_entry *e;
1178
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +03001179 dbg_msg("schedule PEB %d for scrubbing", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001180
1181retry:
1182 spin_lock(&ubi->wl_lock);
1183 e = ubi->lookuptbl[pnum];
Artem Bityutskiyd3f6e6c2010-08-29 23:34:44 +03001184 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1185 in_wl_tree(e, &ubi->erroneous)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001186 spin_unlock(&ubi->wl_lock);
1187 return 0;
1188 }
1189
1190 if (e == ubi->move_to) {
1191 /*
1192 * This physical eraseblock was used to move data to. The data
1193 * was moved but the PEB was not yet inserted to the proper
1194 * tree. We should just wait a little and let the WL worker
1195 * proceed.
1196 */
1197 spin_unlock(&ubi->wl_lock);
1198 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1199 yield();
1200 goto retry;
1201 }
1202
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001203 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001204 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001205 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001206 } else {
1207 int err;
1208
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001209 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001210 if (err) {
1211 ubi_err("PEB %d not found", pnum);
1212 ubi_ro_mode(ubi);
1213 spin_unlock(&ubi->wl_lock);
1214 return err;
1215 }
1216 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001217
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001218 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001219 spin_unlock(&ubi->wl_lock);
1220
1221 /*
1222 * Technically scrubbing is the same as wear-leveling, so it is done
1223 * by the WL worker.
1224 */
1225 return ensure_wear_leveling(ubi);
1226}
1227
1228/**
1229 * ubi_wl_flush - flush all pending works.
1230 * @ubi: UBI device description object
1231 *
1232 * This function returns zero in case of success and a negative error code in
1233 * case of failure.
1234 */
1235int ubi_wl_flush(struct ubi_device *ubi)
1236{
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001237 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001238
1239 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001240 * Erase while the pending works queue is not empty, but not more than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001241 * the number of currently pending works.
1242 */
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001243 dbg_wl("flush (%d pending works)", ubi->works_count);
1244 while (ubi->works_count) {
1245 err = do_work(ubi);
1246 if (err)
1247 return err;
1248 }
1249
1250 /*
1251 * Make sure all the works which have been done in parallel are
1252 * finished.
1253 */
1254 down_write(&ubi->work_sem);
1255 up_write(&ubi->work_sem);
1256
1257 /*
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +02001258 * And in case last was the WL worker and it canceled the LEB
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001259 * movement, flush again.
1260 */
1261 while (ubi->works_count) {
1262 dbg_wl("flush more (%d pending works)", ubi->works_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001263 err = do_work(ubi);
1264 if (err)
1265 return err;
1266 }
1267
1268 return 0;
1269}
1270
1271/**
1272 * tree_destroy - destroy an RB-tree.
1273 * @root: the root of the tree to destroy
1274 */
1275static void tree_destroy(struct rb_root *root)
1276{
1277 struct rb_node *rb;
1278 struct ubi_wl_entry *e;
1279
1280 rb = root->rb_node;
1281 while (rb) {
1282 if (rb->rb_left)
1283 rb = rb->rb_left;
1284 else if (rb->rb_right)
1285 rb = rb->rb_right;
1286 else {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001287 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001288
1289 rb = rb_parent(rb);
1290 if (rb) {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001291 if (rb->rb_left == &e->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001292 rb->rb_left = NULL;
1293 else
1294 rb->rb_right = NULL;
1295 }
1296
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001297 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001298 }
1299 }
1300}
1301
1302/**
1303 * ubi_thread - UBI background thread.
1304 * @u: the UBI device description object pointer
1305 */
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001306int ubi_thread(void *u)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001307{
1308 int failures = 0;
1309 struct ubi_device *ubi = u;
1310
1311 ubi_msg("background thread \"%s\" started, PID %d",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001312 ubi->bgt_name, task_pid_nr(current));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001313
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001314 set_freezable();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001315 for (;;) {
1316 int err;
1317
1318 if (kthread_should_stop())
Kyungmin Parkcadb40c2008-05-22 10:32:18 +09001319 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001320
1321 if (try_to_freeze())
1322 continue;
1323
1324 spin_lock(&ubi->wl_lock);
1325 if (list_empty(&ubi->works) || ubi->ro_mode ||
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +03001326 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001327 set_current_state(TASK_INTERRUPTIBLE);
1328 spin_unlock(&ubi->wl_lock);
1329 schedule();
1330 continue;
1331 }
1332 spin_unlock(&ubi->wl_lock);
1333
1334 err = do_work(ubi);
1335 if (err) {
1336 ubi_err("%s: work failed with error code %d",
1337 ubi->bgt_name, err);
1338 if (failures++ > WL_MAX_FAILURES) {
1339 /*
1340 * Too many failures, disable the thread and
1341 * switch to read-only mode.
1342 */
1343 ubi_msg("%s: %d consecutive failures",
1344 ubi->bgt_name, WL_MAX_FAILURES);
1345 ubi_ro_mode(ubi);
Vitaliy Gusev2ad49882008-11-05 18:27:18 +03001346 ubi->thread_enabled = 0;
1347 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001348 }
1349 } else
1350 failures = 0;
1351
1352 cond_resched();
1353 }
1354
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001355 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1356 return 0;
1357}
1358
1359/**
1360 * cancel_pending - cancel all pending works.
1361 * @ubi: UBI device description object
1362 */
1363static void cancel_pending(struct ubi_device *ubi)
1364{
1365 while (!list_empty(&ubi->works)) {
1366 struct ubi_work *wrk;
1367
1368 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1369 list_del(&wrk->list);
1370 wrk->func(ubi, wrk, 1);
1371 ubi->works_count -= 1;
1372 ubi_assert(ubi->works_count >= 0);
1373 }
1374}
1375
1376/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001377 * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001378 * @ubi: UBI device description object
1379 * @si: scanning information
1380 *
1381 * This function returns zero in case of success, and a negative error code in
1382 * case of failure.
1383 */
1384int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1385{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001386 int err, i;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001387 struct rb_node *rb1, *rb2;
1388 struct ubi_scan_volume *sv;
1389 struct ubi_scan_leb *seb, *tmp;
1390 struct ubi_wl_entry *e;
1391
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001392 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001393 spin_lock_init(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001394 mutex_init(&ubi->move_mutex);
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001395 init_rwsem(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001396 ubi->max_ec = si->max_ec;
1397 INIT_LIST_HEAD(&ubi->works);
1398
1399 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1400
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001401 err = -ENOMEM;
1402 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1403 if (!ubi->lookuptbl)
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001404 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001405
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001406 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1407 INIT_LIST_HEAD(&ubi->pq[i]);
1408 ubi->pq_head = 0;
1409
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001410 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1411 cond_resched();
1412
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001413 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001414 if (!e)
1415 goto out_free;
1416
1417 e->pnum = seb->pnum;
1418 e->ec = seb->ec;
1419 ubi->lookuptbl[e->pnum] = e;
1420 if (schedule_erase(ubi, e, 0)) {
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001421 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001422 goto out_free;
1423 }
1424 }
1425
1426 list_for_each_entry(seb, &si->free, u.list) {
1427 cond_resched();
1428
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001429 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001430 if (!e)
1431 goto out_free;
1432
1433 e->pnum = seb->pnum;
1434 e->ec = seb->ec;
1435 ubi_assert(e->ec >= 0);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001436 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001437 ubi->lookuptbl[e->pnum] = e;
1438 }
1439
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001440 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1441 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1442 cond_resched();
1443
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001444 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001445 if (!e)
1446 goto out_free;
1447
1448 e->pnum = seb->pnum;
1449 e->ec = seb->ec;
1450 ubi->lookuptbl[e->pnum] = e;
1451 if (!seb->scrub) {
1452 dbg_wl("add PEB %d EC %d to the used tree",
1453 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001454 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001455 } else {
1456 dbg_wl("add PEB %d EC %d to the scrub tree",
1457 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001458 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001459 }
1460 }
1461 }
1462
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001463 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001464 ubi_err("no enough physical eraseblocks (%d, need %d)",
1465 ubi->avail_pebs, WL_RESERVED_PEBS);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +03001466 if (ubi->corr_peb_count)
1467 ubi_err("%d PEBs are corrupted and not used",
1468 ubi->corr_peb_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001469 goto out_free;
1470 }
1471 ubi->avail_pebs -= WL_RESERVED_PEBS;
1472 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1473
1474 /* Schedule wear-leveling if needed */
1475 err = ensure_wear_leveling(ubi);
1476 if (err)
1477 goto out_free;
1478
1479 return 0;
1480
1481out_free:
1482 cancel_pending(ubi);
1483 tree_destroy(&ubi->used);
1484 tree_destroy(&ubi->free);
1485 tree_destroy(&ubi->scrub);
1486 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001487 return err;
1488}
1489
1490/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001491 * protection_queue_destroy - destroy the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001492 * @ubi: UBI device description object
1493 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001494static void protection_queue_destroy(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001495{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001496 int i;
1497 struct ubi_wl_entry *e, *tmp;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001498
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001499 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1500 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1501 list_del(&e->u.list);
1502 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001503 }
1504 }
1505}
1506
1507/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001508 * ubi_wl_close - close the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001509 * @ubi: UBI device description object
1510 */
1511void ubi_wl_close(struct ubi_device *ubi)
1512{
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001513 dbg_wl("close the WL sub-system");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001514 cancel_pending(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001515 protection_queue_destroy(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001516 tree_destroy(&ubi->used);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001517 tree_destroy(&ubi->erroneous);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001518 tree_destroy(&ubi->free);
1519 tree_destroy(&ubi->scrub);
1520 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001521}
1522
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001523/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001524 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001525 * @ubi: UBI device description object
1526 * @pnum: the physical eraseblock number to check
1527 * @ec: the erase counter to check
1528 *
1529 * This function returns zero if the erase counter of physical eraseblock @pnum
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001530 * is equivalent to @ec, and a negative error code if not or if an error
1531 * occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001532 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001533static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001534{
1535 int err;
1536 long long read_ec;
1537 struct ubi_ec_hdr *ec_hdr;
1538
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001539 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001540 return 0;
1541
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001542 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001543 if (!ec_hdr)
1544 return -ENOMEM;
1545
1546 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1547 if (err && err != UBI_IO_BITFLIPS) {
1548 /* The header does not have to exist */
1549 err = 0;
1550 goto out_free;
1551 }
1552
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001553 read_ec = be64_to_cpu(ec_hdr->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001554 if (ec != read_ec) {
1555 ubi_err("paranoid check failed for PEB %d", pnum);
1556 ubi_err("read EC is %lld, should be %d", read_ec, ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001557 dump_stack();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001558 err = 1;
1559 } else
1560 err = 0;
1561
1562out_free:
1563 kfree(ec_hdr);
1564 return err;
1565}
1566
1567/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001568 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001569 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001570 * @e: the wear-leveling entry to check
1571 * @root: the root of the tree
1572 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001573 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1574 * is not.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001575 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001576static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
1577 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001578 struct rb_root *root)
1579{
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001580 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001581 return 0;
1582
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001583 if (in_wl_tree(e, root))
1584 return 0;
1585
1586 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1587 e->pnum, e->ec, root);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001588 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001589 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001590}
1591
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001592/**
1593 * paranoid_check_in_pq - check if wear-leveling entry is in the protection
1594 * queue.
1595 * @ubi: UBI device description object
1596 * @e: the wear-leveling entry to check
1597 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001598 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001599 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001600static int paranoid_check_in_pq(const struct ubi_device *ubi,
1601 struct ubi_wl_entry *e)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001602{
1603 struct ubi_wl_entry *p;
1604 int i;
1605
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001606 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001607 return 0;
1608
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001609 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1610 list_for_each_entry(p, &ubi->pq[i], u.list)
1611 if (p == e)
1612 return 0;
1613
1614 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue",
1615 e->pnum, e->ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001616 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001617 return -EINVAL;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001618}