blob: 64ce9930dacb2aa3a030710005723e04301bb707 [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 Bityutskiy92d124f2011-03-14 18:17:40 +0200158#ifdef CONFIG_MTD_UBI_DEBUG
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300159static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300160static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
161 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400162 struct rb_root *root);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300163static int paranoid_check_in_pq(const struct ubi_device *ubi,
164 struct ubi_wl_entry *e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400165#else
166#define paranoid_check_ec(ubi, pnum, ec) 0
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300167#define paranoid_check_in_wl_tree(ubi, e, root)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800168#define paranoid_check_in_pq(ubi, e) 0
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400169#endif
170
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400171/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400172 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
173 * @e: the wear-leveling entry to add
174 * @root: the root of the tree
175 *
176 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
177 * the @ubi->used and @ubi->free RB-trees.
178 */
179static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
180{
181 struct rb_node **p, *parent = NULL;
182
183 p = &root->rb_node;
184 while (*p) {
185 struct ubi_wl_entry *e1;
186
187 parent = *p;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800188 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400189
190 if (e->ec < e1->ec)
191 p = &(*p)->rb_left;
192 else if (e->ec > e1->ec)
193 p = &(*p)->rb_right;
194 else {
195 ubi_assert(e->pnum != e1->pnum);
196 if (e->pnum < e1->pnum)
197 p = &(*p)->rb_left;
198 else
199 p = &(*p)->rb_right;
200 }
201 }
202
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800203 rb_link_node(&e->u.rb, parent, p);
204 rb_insert_color(&e->u.rb, root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400205}
206
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400207/**
208 * do_work - do one pending work.
209 * @ubi: UBI device description object
210 *
211 * This function returns zero in case of success and a negative error code in
212 * case of failure.
213 */
214static int do_work(struct ubi_device *ubi)
215{
216 int err;
217 struct ubi_work *wrk;
218
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200219 cond_resched();
220
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200221 /*
222 * @ubi->work_sem is used to synchronize with the workers. Workers take
223 * it in read mode, so many of them may be doing works at a time. But
224 * the queue flush code has to be sure the whole queue of works is
225 * done, and it takes the mutex in write mode.
226 */
227 down_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400228 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400229 if (list_empty(&ubi->works)) {
230 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200231 up_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400232 return 0;
233 }
234
235 wrk = list_entry(ubi->works.next, struct ubi_work, list);
236 list_del(&wrk->list);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200237 ubi->works_count -= 1;
238 ubi_assert(ubi->works_count >= 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400239 spin_unlock(&ubi->wl_lock);
240
241 /*
242 * Call the worker function. Do not touch the work structure
243 * after this call as it will have been freed or reused by that
244 * time by the worker function.
245 */
246 err = wrk->func(ubi, wrk, 0);
247 if (err)
248 ubi_err("work failed with error code %d", err);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200249 up_read(&ubi->work_sem);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200250
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400251 return err;
252}
253
254/**
255 * produce_free_peb - produce a free physical eraseblock.
256 * @ubi: UBI device description object
257 *
258 * This function tries to make a free PEB by means of synchronous execution of
259 * pending works. This may be needed if, for example the background thread is
260 * disabled. Returns zero in case of success and a negative error code in case
261 * of failure.
262 */
263static int produce_free_peb(struct ubi_device *ubi)
264{
265 int err;
266
267 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300268 while (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400269 spin_unlock(&ubi->wl_lock);
270
271 dbg_wl("do one work synchronously");
272 err = do_work(ubi);
273 if (err)
274 return err;
275
276 spin_lock(&ubi->wl_lock);
277 }
278 spin_unlock(&ubi->wl_lock);
279
280 return 0;
281}
282
283/**
284 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
285 * @e: the wear-leveling entry to check
286 * @root: the root of the tree
287 *
288 * This function returns non-zero if @e is in the @root RB-tree and zero if it
289 * is not.
290 */
291static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
292{
293 struct rb_node *p;
294
295 p = root->rb_node;
296 while (p) {
297 struct ubi_wl_entry *e1;
298
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800299 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400300
301 if (e->pnum == e1->pnum) {
302 ubi_assert(e == e1);
303 return 1;
304 }
305
306 if (e->ec < e1->ec)
307 p = p->rb_left;
308 else if (e->ec > e1->ec)
309 p = p->rb_right;
310 else {
311 ubi_assert(e->pnum != e1->pnum);
312 if (e->pnum < e1->pnum)
313 p = p->rb_left;
314 else
315 p = p->rb_right;
316 }
317 }
318
319 return 0;
320}
321
322/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800323 * prot_queue_add - add physical eraseblock to the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400324 * @ubi: UBI device description object
325 * @e: the physical eraseblock to add
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400326 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800327 * This function adds @e to the tail of the protection queue @ubi->pq, where
328 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
329 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
330 * be locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400331 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800332static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400333{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800334 int pq_tail = ubi->pq_head - 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400335
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800336 if (pq_tail < 0)
337 pq_tail = UBI_PROT_QUEUE_LEN - 1;
338 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
339 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
340 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400341}
342
343/**
344 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
345 * @root: the RB-tree where to look for
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200346 * @diff: maximum possible difference from the smallest erase counter
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400347 *
348 * This function looks for a wear leveling entry with erase counter closest to
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200349 * min + @diff, where min is the smallest erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400350 */
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200351static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400352{
353 struct rb_node *p;
354 struct ubi_wl_entry *e;
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200355 int max;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400356
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800357 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200358 max = e->ec + diff;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359
360 p = root->rb_node;
361 while (p) {
362 struct ubi_wl_entry *e1;
363
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800364 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400365 if (e1->ec >= max)
366 p = p->rb_left;
367 else {
368 p = p->rb_right;
369 e = e1;
370 }
371 }
372
373 return e;
374}
375
376/**
377 * ubi_wl_get_peb - get a physical eraseblock.
378 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400379 *
380 * This function returns a physical eraseblock in case of success and a
381 * negative error code in case of failure. Might sleep.
382 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200383int ubi_wl_get_peb(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400384{
Artem Bityutskiy7eb3aa652012-03-07 19:08:36 +0200385 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400386 struct ubi_wl_entry *e, *first, *last;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400387
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388retry:
389 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300390 if (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400391 if (ubi->works_count == 0) {
392 ubi_assert(list_empty(&ubi->works));
393 ubi_err("no free eraseblocks");
394 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 return -ENOSPC;
396 }
397 spin_unlock(&ubi->wl_lock);
398
399 err = produce_free_peb(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800400 if (err < 0)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400401 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400402 goto retry;
403 }
404
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200405 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
406 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400407
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200408 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
409 e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb);
410 else
411 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400412
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300413 paranoid_check_in_wl_tree(ubi, e, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800414
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400415 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800416 * Move the physical eraseblock to the protection queue where it will
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400417 * be protected from being moved for some time.
418 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800419 rb_erase(&e->u.rb, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800420 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
421 prot_queue_add(ubi, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300423
424 err = ubi_dbg_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
425 ubi->peb_size - ubi->vid_hdr_aloffset);
426 if (err) {
Artem Bityutskiy13987882009-06-29 15:58:36 +0300427 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200428 return err;
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300429 }
430
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400431 return e->pnum;
432}
433
434/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800435 * prot_queue_del - remove a physical eraseblock from the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400436 * @ubi: UBI device description object
437 * @pnum: the physical eraseblock to remove
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200438 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800439 * This function deletes PEB @pnum from the protection queue and returns zero
440 * in case of success and %-ENODEV if the PEB was not found.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400441 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800442static int prot_queue_del(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400443{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800444 struct ubi_wl_entry *e;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400445
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800446 e = ubi->lookuptbl[pnum];
447 if (!e)
448 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400449
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800450 if (paranoid_check_in_pq(ubi, e))
451 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400452
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800453 list_del(&e->u.list);
454 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200455 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400456}
457
458/**
459 * sync_erase - synchronously erase a physical eraseblock.
460 * @ubi: UBI device description object
461 * @e: the the physical eraseblock to erase
462 * @torture: if the physical eraseblock has to be tortured
463 *
464 * This function returns zero in case of success and a negative error code in
465 * case of failure.
466 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300467static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
468 int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400469{
470 int err;
471 struct ubi_ec_hdr *ec_hdr;
472 unsigned long long ec = e->ec;
473
474 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
475
476 err = paranoid_check_ec(ubi, e->pnum, e->ec);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200477 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400478 return -EINVAL;
479
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300480 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400481 if (!ec_hdr)
482 return -ENOMEM;
483
484 err = ubi_io_sync_erase(ubi, e->pnum, torture);
485 if (err < 0)
486 goto out_free;
487
488 ec += err;
489 if (ec > UBI_MAX_ERASECOUNTER) {
490 /*
491 * Erase counter overflow. Upgrade UBI and use 64-bit
492 * erase counters internally.
493 */
494 ubi_err("erase counter overflow at PEB %d, EC %llu",
495 e->pnum, ec);
496 err = -EINVAL;
497 goto out_free;
498 }
499
500 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
501
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300502 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400503
504 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
505 if (err)
506 goto out_free;
507
508 e->ec = ec;
509 spin_lock(&ubi->wl_lock);
510 if (e->ec > ubi->max_ec)
511 ubi->max_ec = e->ec;
512 spin_unlock(&ubi->wl_lock);
513
514out_free:
515 kfree(ec_hdr);
516 return err;
517}
518
519/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800520 * serve_prot_queue - check if it is time to stop protecting PEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400521 * @ubi: UBI device description object
522 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800523 * This function is called after each erase operation and removes PEBs from the
524 * tail of the protection queue. These PEBs have been protected for long enough
525 * and should be moved to the used tree.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400526 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800527static void serve_prot_queue(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400528{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800529 struct ubi_wl_entry *e, *tmp;
530 int count;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400531
532 /*
533 * There may be several protected physical eraseblock to remove,
534 * process them all.
535 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800536repeat:
537 count = 0;
538 spin_lock(&ubi->wl_lock);
539 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
540 dbg_wl("PEB %d EC %d protection over, move to used tree",
541 e->pnum, e->ec);
542
543 list_del(&e->u.list);
544 wl_tree_add(e, &ubi->used);
545 if (count++ > 32) {
546 /*
547 * Let's be nice and avoid holding the spinlock for
548 * too long.
549 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400550 spin_unlock(&ubi->wl_lock);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800551 cond_resched();
552 goto repeat;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400553 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400554 }
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800555
556 ubi->pq_head += 1;
557 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
558 ubi->pq_head = 0;
559 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
560 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400561}
562
563/**
564 * schedule_ubi_work - schedule a work.
565 * @ubi: UBI device description object
566 * @wrk: the work to schedule
567 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800568 * This function adds a work defined by @wrk to the tail of the pending works
569 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400570 */
571static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
572{
573 spin_lock(&ubi->wl_lock);
574 list_add_tail(&wrk->list, &ubi->works);
575 ubi_assert(ubi->works_count >= 0);
576 ubi->works_count += 1;
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +0300577 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400578 wake_up_process(ubi->bgt_thread);
579 spin_unlock(&ubi->wl_lock);
580}
581
582static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
583 int cancel);
584
585/**
586 * schedule_erase - schedule an erase work.
587 * @ubi: UBI device description object
588 * @e: the WL entry of the physical eraseblock to erase
589 * @torture: if the physical eraseblock has to be tortured
590 *
591 * This function returns zero in case of success and a %-ENOMEM in case of
592 * failure.
593 */
594static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
595 int torture)
596{
597 struct ubi_work *wl_wrk;
598
599 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
600 e->pnum, e->ec, torture);
601
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300602 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400603 if (!wl_wrk)
604 return -ENOMEM;
605
606 wl_wrk->func = &erase_worker;
607 wl_wrk->e = e;
608 wl_wrk->torture = torture;
609
610 schedule_ubi_work(ubi, wl_wrk);
611 return 0;
612}
613
614/**
615 * wear_leveling_worker - wear-leveling worker function.
616 * @ubi: UBI device description object
617 * @wrk: the work object
618 * @cancel: non-zero if the worker has to free memory and exit
619 *
620 * This function copies a more worn out physical eraseblock to a less worn out
621 * one. Returns zero in case of success and a negative error code in case of
622 * failure.
623 */
624static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
625 int cancel)
626{
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300627 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300628 int vol_id = -1, uninitialized_var(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400629 struct ubi_wl_entry *e1, *e2;
630 struct ubi_vid_hdr *vid_hdr;
631
632 kfree(wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400633 if (cancel)
634 return 0;
635
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300636 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400637 if (!vid_hdr)
638 return -ENOMEM;
639
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200640 mutex_lock(&ubi->move_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400641 spin_lock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200642 ubi_assert(!ubi->move_from && !ubi->move_to);
643 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400644
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200645 if (!ubi->free.rb_node ||
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300646 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400647 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200648 * No free physical eraseblocks? Well, they must be waiting in
649 * the queue to be erased. Cancel movement - it will be
650 * triggered again when a free physical eraseblock appears.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400651 *
652 * No used physical eraseblocks? They must be temporarily
653 * protected from being moved. They will be moved to the
654 * @ubi->used tree later and the wear-leveling will be
655 * triggered again.
656 */
657 dbg_wl("cancel WL, a list is empty: free %d, used %d",
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300658 !ubi->free.rb_node, !ubi->used.rb_node);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200659 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400660 }
661
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300662 if (!ubi->scrub.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400663 /*
664 * Now pick the least worn-out used physical eraseblock and a
665 * highly worn-out free physical eraseblock. If the erase
666 * counters differ much enough, start wear-leveling.
667 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800668 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
670
671 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
672 dbg_wl("no WL needed: min used EC %d, max free EC %d",
673 e1->ec, e2->ec);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200674 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400675 }
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300676 paranoid_check_in_wl_tree(ubi, e1, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800677 rb_erase(&e1->u.rb, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400678 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
679 e1->pnum, e1->ec, e2->pnum, e2->ec);
680 } else {
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200681 /* Perform scrubbing */
682 scrubbing = 1;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800683 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400684 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300685 paranoid_check_in_wl_tree(ubi, e1, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800686 rb_erase(&e1->u.rb, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400687 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
688 }
689
Artem Bityutskiyd99383b2011-05-18 14:47:34 +0300690 paranoid_check_in_wl_tree(ubi, e2, &ubi->free);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800691 rb_erase(&e2->u.rb, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400692 ubi->move_from = e1;
693 ubi->move_to = e2;
694 spin_unlock(&ubi->wl_lock);
695
696 /*
697 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
698 * We so far do not know which logical eraseblock our physical
699 * eraseblock (@e1) belongs to. We have to read the volume identifier
700 * header first.
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200701 *
702 * Note, we are protected from this PEB being unmapped and erased. The
703 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
704 * which is being moved was unmapped.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400705 */
706
707 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
708 if (err && err != UBI_IO_BITFLIPS) {
Artem Bityutskiy74d82d22010-09-03 02:11:20 +0300709 if (err == UBI_IO_FF) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400710 /*
711 * We are trying to move PEB without a VID header. UBI
712 * always write VID headers shortly after the PEB was
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300713 * given, so we have a situation when it has not yet
714 * had a chance to write it, because it was preempted.
715 * So add this PEB to the protection queue so far,
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300716 * because presumably more data will be written there
717 * (including the missing VID header), and then we'll
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300718 * move it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400719 */
720 dbg_wl("PEB %d has no VID header", e1->pnum);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300721 protect = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200722 goto out_not_moved;
Artem Bityutskiy92e1a7d2010-09-03 14:22:17 +0300723 } else if (err == UBI_IO_FF_BITFLIPS) {
724 /*
725 * The same situation as %UBI_IO_FF, but bit-flips were
726 * detected. It is better to schedule this PEB for
727 * scrubbing.
728 */
729 dbg_wl("PEB %d has no VID header but has bit-flips",
730 e1->pnum);
731 scrubbing = 1;
732 goto out_not_moved;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400733 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200734
735 ubi_err("error %d while reading VID header from PEB %d",
736 err, e1->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200737 goto out_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400738 }
739
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300740 vol_id = be32_to_cpu(vid_hdr->vol_id);
741 lnum = be32_to_cpu(vid_hdr->lnum);
742
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400743 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
744 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300745 if (err == MOVE_CANCEL_RACE) {
746 /*
747 * The LEB has not been moved because the volume is
748 * being deleted or the PEB has been put meanwhile. We
749 * should prevent this PEB from being selected for
750 * wear-leveling movement again, so put it to the
751 * protection queue.
752 */
753 protect = 1;
754 goto out_not_moved;
755 }
Bhavesh Parekhe801e122011-11-30 17:43:42 +0530756 if (err == MOVE_RETRY) {
757 scrubbing = 1;
758 goto out_not_moved;
759 }
Artem Bityutskiycc831462012-03-09 10:31:18 +0200760 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300761 err == MOVE_TARGET_RD_ERR) {
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300762 /*
763 * Target PEB had bit-flips or write error - torture it.
764 */
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200765 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200766 goto out_not_moved;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200767 }
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300768
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300769 if (err == MOVE_SOURCE_RD_ERR) {
770 /*
771 * An error happened while reading the source PEB. Do
772 * not switch to R/O mode in this case, and give the
773 * upper layers a possibility to recover from this,
774 * e.g. by unmapping corresponding LEB. Instead, just
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300775 * put this PEB to the @ubi->erroneous list to prevent
776 * UBI from trying to move it over and over again.
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300777 */
778 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
779 ubi_err("too many erroneous eraseblocks (%d)",
780 ubi->erroneous_peb_count);
781 goto out_error;
782 }
783 erroneous = 1;
784 goto out_not_moved;
785 }
786
Artem Bityutskiy90bf0262009-05-23 16:04:17 +0300787 if (err < 0)
788 goto out_error;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200789
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300790 ubi_assert(0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400791 }
792
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200793 /* The PEB has been successfully moved */
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200794 if (scrubbing)
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300795 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
796 e1->pnum, vol_id, lnum, e2->pnum);
797 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300798
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400799 spin_lock(&ubi->wl_lock);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200800 if (!ubi->move_to_put) {
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300801 wl_tree_add(e2, &ubi->used);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200802 e2 = NULL;
803 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400804 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200805 ubi->move_to_put = ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400806 spin_unlock(&ubi->wl_lock);
807
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200808 err = schedule_erase(ubi, e1, 0);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200809 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300810 kmem_cache_free(ubi_wl_entry_slab, e1);
Artem Bityutskiy21d08bb2009-06-08 19:28:18 +0300811 if (e2)
812 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300813 goto out_ro;
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200814 }
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200815
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200816 if (e2) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400817 /*
818 * Well, the target PEB was put meanwhile, schedule it for
819 * erasure.
820 */
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300821 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
822 e2->pnum, vol_id, lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400823 err = schedule_erase(ubi, e2, 0);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300824 if (err) {
825 kmem_cache_free(ubi_wl_entry_slab, e2);
826 goto out_ro;
827 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400828 }
829
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400830 dbg_wl("done");
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200831 mutex_unlock(&ubi->move_mutex);
832 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833
834 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200835 * For some reasons the LEB was not moved, might be an error, might be
836 * something else. @e1 was not changed, so return it back. @e2 might
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200837 * have been changed, schedule it for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400838 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200839out_not_moved:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300840 if (vol_id != -1)
841 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
842 e1->pnum, vol_id, lnum, e2->pnum, err);
843 else
844 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
845 e1->pnum, e2->pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400846 spin_lock(&ubi->wl_lock);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300847 if (protect)
848 prot_queue_add(ubi, e1);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300849 else if (erroneous) {
850 wl_tree_add(e1, &ubi->erroneous);
851 ubi->erroneous_peb_count += 1;
852 } else if (scrubbing)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200853 wl_tree_add(e1, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400854 else
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300855 wl_tree_add(e1, &ubi->used);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200856 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400857 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200858 ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400859 spin_unlock(&ubi->wl_lock);
860
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300861 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200862 err = schedule_erase(ubi, e2, torture);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300863 if (err) {
864 kmem_cache_free(ubi_wl_entry_slab, e2);
865 goto out_ro;
866 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200867 mutex_unlock(&ubi->move_mutex);
868 return 0;
869
870out_error:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300871 if (vol_id != -1)
872 ubi_err("error %d while moving PEB %d to PEB %d",
873 err, e1->pnum, e2->pnum);
874 else
875 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
876 err, e1->pnum, vol_id, lnum, e2->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200877 spin_lock(&ubi->wl_lock);
878 ubi->move_from = ubi->move_to = NULL;
879 ubi->move_to_put = ubi->wl_scheduled = 0;
880 spin_unlock(&ubi->wl_lock);
881
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300882 ubi_free_vid_hdr(ubi, vid_hdr);
883 kmem_cache_free(ubi_wl_entry_slab, e1);
884 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200885
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300886out_ro:
887 ubi_ro_mode(ubi);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200888 mutex_unlock(&ubi->move_mutex);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300889 ubi_assert(err != 0);
890 return err < 0 ? err : -EIO;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200891
892out_cancel:
893 ubi->wl_scheduled = 0;
894 spin_unlock(&ubi->wl_lock);
895 mutex_unlock(&ubi->move_mutex);
896 ubi_free_vid_hdr(ubi, vid_hdr);
897 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400898}
899
900/**
901 * ensure_wear_leveling - schedule wear-leveling if it is needed.
902 * @ubi: UBI device description object
903 *
904 * This function checks if it is time to start wear-leveling and schedules it
905 * if yes. This function returns zero in case of success and a negative error
906 * code in case of failure.
907 */
908static int ensure_wear_leveling(struct ubi_device *ubi)
909{
910 int err = 0;
911 struct ubi_wl_entry *e1;
912 struct ubi_wl_entry *e2;
913 struct ubi_work *wrk;
914
915 spin_lock(&ubi->wl_lock);
916 if (ubi->wl_scheduled)
917 /* Wear-leveling is already in the work queue */
918 goto out_unlock;
919
920 /*
921 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
922 * the WL worker has to be scheduled anyway.
923 */
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300924 if (!ubi->scrub.rb_node) {
925 if (!ubi->used.rb_node || !ubi->free.rb_node)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400926 /* No physical eraseblocks - no deal */
927 goto out_unlock;
928
929 /*
930 * We schedule wear-leveling only if the difference between the
931 * lowest erase counter of used physical eraseblocks and a high
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200932 * erase counter of free physical eraseblocks is greater than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400933 * %UBI_WL_THRESHOLD.
934 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800935 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400936 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
937
938 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
939 goto out_unlock;
940 dbg_wl("schedule wear-leveling");
941 } else
942 dbg_wl("schedule scrubbing");
943
944 ubi->wl_scheduled = 1;
945 spin_unlock(&ubi->wl_lock);
946
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300947 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400948 if (!wrk) {
949 err = -ENOMEM;
950 goto out_cancel;
951 }
952
953 wrk->func = &wear_leveling_worker;
954 schedule_ubi_work(ubi, wrk);
955 return err;
956
957out_cancel:
958 spin_lock(&ubi->wl_lock);
959 ubi->wl_scheduled = 0;
960out_unlock:
961 spin_unlock(&ubi->wl_lock);
962 return err;
963}
964
965/**
966 * erase_worker - physical eraseblock erase worker function.
967 * @ubi: UBI device description object
968 * @wl_wrk: the work object
969 * @cancel: non-zero if the worker has to free memory and exit
970 *
971 * This function erases a physical eraseblock and perform torture testing if
972 * needed. It also takes care about marking the physical eraseblock bad if
973 * needed. Returns zero in case of success and a negative error code in case of
974 * failure.
975 */
976static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
977 int cancel)
978{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400979 struct ubi_wl_entry *e = wl_wrk->e;
Artem Bityutskiy784c1452007-07-18 13:42:10 +0300980 int pnum = e->pnum, err, need;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400981
982 if (cancel) {
983 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
984 kfree(wl_wrk);
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +0200985 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986 return 0;
987 }
988
989 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
990
991 err = sync_erase(ubi, e, wl_wrk->torture);
992 if (!err) {
993 /* Fine, we've erased it successfully */
994 kfree(wl_wrk);
995
996 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300997 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400998 spin_unlock(&ubi->wl_lock);
999
1000 /*
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +03001001 * One more erase operation has happened, take care about
1002 * protected physical eraseblocks.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001003 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001004 serve_prot_queue(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001005
1006 /* And take care about wear-leveling */
1007 err = ensure_wear_leveling(ubi);
1008 return err;
1009 }
1010
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +03001011 ubi_err("failed to erase PEB %d, error %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001012 kfree(wl_wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001013
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001014 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1015 err == -EBUSY) {
1016 int err1;
1017
1018 /* Re-schedule the LEB for erasure */
1019 err1 = schedule_erase(ubi, e, 0);
1020 if (err1) {
1021 err = err1;
1022 goto out_ro;
1023 }
1024 return err;
Artem Bityutskiye57e0d82012-01-05 10:47:18 +02001025 }
1026
1027 kmem_cache_free(ubi_wl_entry_slab, e);
1028 if (err != -EIO)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001029 /*
1030 * If this is not %-EIO, we have no idea what to do. Scheduling
1031 * this physical eraseblock for erasure again would cause
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001032 * errors again and again. Well, lets switch to R/O mode.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001033 */
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001034 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001035
1036 /* It is %-EIO, the PEB went bad */
1037
1038 if (!ubi->bad_allowed) {
1039 ubi_err("bad physical eraseblock %d detected", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001040 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001041 }
1042
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001043 spin_lock(&ubi->volumes_lock);
1044 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1045 if (need > 0) {
1046 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1047 ubi->avail_pebs -= need;
1048 ubi->rsvd_pebs += need;
1049 ubi->beb_rsvd_pebs += need;
1050 if (need > 0)
1051 ubi_msg("reserve more %d PEBs", need);
1052 }
1053
1054 if (ubi->beb_rsvd_pebs == 0) {
1055 spin_unlock(&ubi->volumes_lock);
1056 ubi_err("no reserved physical eraseblocks");
1057 goto out_ro;
1058 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001059 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001060
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001061 ubi_msg("mark PEB %d as bad", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001062 err = ubi_io_mark_bad(ubi, pnum);
1063 if (err)
1064 goto out_ro;
1065
1066 spin_lock(&ubi->volumes_lock);
1067 ubi->beb_rsvd_pebs -= 1;
1068 ubi->bad_peb_count += 1;
1069 ubi->good_peb_count -= 1;
1070 ubi_calculate_reserved(ubi);
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001071 if (ubi->beb_rsvd_pebs)
1072 ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1073 else
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001074 ubi_warn("last PEB from the reserved pool was used");
1075 spin_unlock(&ubi->volumes_lock);
1076
1077 return err;
1078
1079out_ro:
1080 ubi_ro_mode(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001081 return err;
1082}
1083
1084/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001085 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001086 * @ubi: UBI device description object
1087 * @pnum: physical eraseblock to return
1088 * @torture: if this physical eraseblock has to be tortured
1089 *
1090 * This function is called to return physical eraseblock @pnum to the pool of
1091 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1092 * occurred to this @pnum and it has to be tested. This function returns zero
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001093 * in case of success, and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001094 */
1095int ubi_wl_put_peb(struct ubi_device *ubi, int pnum, int torture)
1096{
1097 int err;
1098 struct ubi_wl_entry *e;
1099
1100 dbg_wl("PEB %d", pnum);
1101 ubi_assert(pnum >= 0);
1102 ubi_assert(pnum < ubi->peb_count);
1103
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001104retry:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001105 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001106 e = ubi->lookuptbl[pnum];
1107 if (e == ubi->move_from) {
1108 /*
1109 * User is putting the physical eraseblock which was selected to
1110 * be moved. It will be scheduled for erasure in the
1111 * wear-leveling worker.
1112 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001113 dbg_wl("PEB %d is being moved, wait", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001114 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001115
1116 /* Wait for the WL worker by taking the @ubi->move_mutex */
1117 mutex_lock(&ubi->move_mutex);
1118 mutex_unlock(&ubi->move_mutex);
1119 goto retry;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001120 } else if (e == ubi->move_to) {
1121 /*
1122 * User is putting the physical eraseblock which was selected
1123 * as the target the data is moved to. It may happen if the EBA
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001124 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1125 * but the WL sub-system has not put the PEB to the "used" tree
1126 * yet, but it is about to do this. So we just set a flag which
1127 * will tell the WL worker that the PEB is not needed anymore
1128 * and should be scheduled for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001129 */
1130 dbg_wl("PEB %d is the target of data moving", pnum);
1131 ubi_assert(!ubi->move_to_put);
1132 ubi->move_to_put = 1;
1133 spin_unlock(&ubi->wl_lock);
1134 return 0;
1135 } else {
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001136 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001137 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001138 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001139 } else if (in_wl_tree(e, &ubi->scrub)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001140 paranoid_check_in_wl_tree(ubi, e, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001141 rb_erase(&e->u.rb, &ubi->scrub);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001142 } else if (in_wl_tree(e, &ubi->erroneous)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001143 paranoid_check_in_wl_tree(ubi, e, &ubi->erroneous);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001144 rb_erase(&e->u.rb, &ubi->erroneous);
1145 ubi->erroneous_peb_count -= 1;
1146 ubi_assert(ubi->erroneous_peb_count >= 0);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001147 /* Erroneous PEBs should be tortured */
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001148 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001149 } else {
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001150 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001151 if (err) {
1152 ubi_err("PEB %d not found", pnum);
1153 ubi_ro_mode(ubi);
1154 spin_unlock(&ubi->wl_lock);
1155 return err;
1156 }
1157 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001158 }
1159 spin_unlock(&ubi->wl_lock);
1160
1161 err = schedule_erase(ubi, e, torture);
1162 if (err) {
1163 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001164 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001165 spin_unlock(&ubi->wl_lock);
1166 }
1167
1168 return err;
1169}
1170
1171/**
1172 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1173 * @ubi: UBI device description object
1174 * @pnum: the physical eraseblock to schedule
1175 *
1176 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1177 * needs scrubbing. This function schedules a physical eraseblock for
1178 * scrubbing which is done in background. This function returns zero in case of
1179 * success and a negative error code in case of failure.
1180 */
1181int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1182{
1183 struct ubi_wl_entry *e;
1184
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +03001185 dbg_msg("schedule PEB %d for scrubbing", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001186
1187retry:
1188 spin_lock(&ubi->wl_lock);
1189 e = ubi->lookuptbl[pnum];
Artem Bityutskiyd3f6e6c2010-08-29 23:34:44 +03001190 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1191 in_wl_tree(e, &ubi->erroneous)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001192 spin_unlock(&ubi->wl_lock);
1193 return 0;
1194 }
1195
1196 if (e == ubi->move_to) {
1197 /*
1198 * This physical eraseblock was used to move data to. The data
1199 * was moved but the PEB was not yet inserted to the proper
1200 * tree. We should just wait a little and let the WL worker
1201 * proceed.
1202 */
1203 spin_unlock(&ubi->wl_lock);
1204 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1205 yield();
1206 goto retry;
1207 }
1208
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001209 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001210 paranoid_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001211 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001212 } else {
1213 int err;
1214
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001215 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001216 if (err) {
1217 ubi_err("PEB %d not found", pnum);
1218 ubi_ro_mode(ubi);
1219 spin_unlock(&ubi->wl_lock);
1220 return err;
1221 }
1222 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001223
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001224 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001225 spin_unlock(&ubi->wl_lock);
1226
1227 /*
1228 * Technically scrubbing is the same as wear-leveling, so it is done
1229 * by the WL worker.
1230 */
1231 return ensure_wear_leveling(ubi);
1232}
1233
1234/**
1235 * ubi_wl_flush - flush all pending works.
1236 * @ubi: UBI device description object
1237 *
1238 * This function returns zero in case of success and a negative error code in
1239 * case of failure.
1240 */
1241int ubi_wl_flush(struct ubi_device *ubi)
1242{
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001243 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001244
1245 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001246 * Erase while the pending works queue is not empty, but not more than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001247 * the number of currently pending works.
1248 */
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001249 dbg_wl("flush (%d pending works)", ubi->works_count);
1250 while (ubi->works_count) {
1251 err = do_work(ubi);
1252 if (err)
1253 return err;
1254 }
1255
1256 /*
1257 * Make sure all the works which have been done in parallel are
1258 * finished.
1259 */
1260 down_write(&ubi->work_sem);
1261 up_write(&ubi->work_sem);
1262
1263 /*
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +02001264 * And in case last was the WL worker and it canceled the LEB
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001265 * movement, flush again.
1266 */
1267 while (ubi->works_count) {
1268 dbg_wl("flush more (%d pending works)", ubi->works_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001269 err = do_work(ubi);
1270 if (err)
1271 return err;
1272 }
1273
1274 return 0;
1275}
1276
1277/**
1278 * tree_destroy - destroy an RB-tree.
1279 * @root: the root of the tree to destroy
1280 */
1281static void tree_destroy(struct rb_root *root)
1282{
1283 struct rb_node *rb;
1284 struct ubi_wl_entry *e;
1285
1286 rb = root->rb_node;
1287 while (rb) {
1288 if (rb->rb_left)
1289 rb = rb->rb_left;
1290 else if (rb->rb_right)
1291 rb = rb->rb_right;
1292 else {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001293 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001294
1295 rb = rb_parent(rb);
1296 if (rb) {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001297 if (rb->rb_left == &e->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001298 rb->rb_left = NULL;
1299 else
1300 rb->rb_right = NULL;
1301 }
1302
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001303 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001304 }
1305 }
1306}
1307
1308/**
1309 * ubi_thread - UBI background thread.
1310 * @u: the UBI device description object pointer
1311 */
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001312int ubi_thread(void *u)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001313{
1314 int failures = 0;
1315 struct ubi_device *ubi = u;
1316
1317 ubi_msg("background thread \"%s\" started, PID %d",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001318 ubi->bgt_name, task_pid_nr(current));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001319
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001320 set_freezable();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001321 for (;;) {
1322 int err;
1323
1324 if (kthread_should_stop())
Kyungmin Parkcadb40c2008-05-22 10:32:18 +09001325 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001326
1327 if (try_to_freeze())
1328 continue;
1329
1330 spin_lock(&ubi->wl_lock);
1331 if (list_empty(&ubi->works) || ubi->ro_mode ||
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +03001332 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001333 set_current_state(TASK_INTERRUPTIBLE);
1334 spin_unlock(&ubi->wl_lock);
1335 schedule();
1336 continue;
1337 }
1338 spin_unlock(&ubi->wl_lock);
1339
1340 err = do_work(ubi);
1341 if (err) {
1342 ubi_err("%s: work failed with error code %d",
1343 ubi->bgt_name, err);
1344 if (failures++ > WL_MAX_FAILURES) {
1345 /*
1346 * Too many failures, disable the thread and
1347 * switch to read-only mode.
1348 */
1349 ubi_msg("%s: %d consecutive failures",
1350 ubi->bgt_name, WL_MAX_FAILURES);
1351 ubi_ro_mode(ubi);
Vitaliy Gusev2ad49882008-11-05 18:27:18 +03001352 ubi->thread_enabled = 0;
1353 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001354 }
1355 } else
1356 failures = 0;
1357
1358 cond_resched();
1359 }
1360
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001361 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1362 return 0;
1363}
1364
1365/**
1366 * cancel_pending - cancel all pending works.
1367 * @ubi: UBI device description object
1368 */
1369static void cancel_pending(struct ubi_device *ubi)
1370{
1371 while (!list_empty(&ubi->works)) {
1372 struct ubi_work *wrk;
1373
1374 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1375 list_del(&wrk->list);
1376 wrk->func(ubi, wrk, 1);
1377 ubi->works_count -= 1;
1378 ubi_assert(ubi->works_count >= 0);
1379 }
1380}
1381
1382/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001383 * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001384 * @ubi: UBI device description object
1385 * @si: scanning information
1386 *
1387 * This function returns zero in case of success, and a negative error code in
1388 * case of failure.
1389 */
1390int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1391{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001392 int err, i;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001393 struct rb_node *rb1, *rb2;
1394 struct ubi_scan_volume *sv;
1395 struct ubi_scan_leb *seb, *tmp;
1396 struct ubi_wl_entry *e;
1397
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001398 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001399 spin_lock_init(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001400 mutex_init(&ubi->move_mutex);
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001401 init_rwsem(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001402 ubi->max_ec = si->max_ec;
1403 INIT_LIST_HEAD(&ubi->works);
1404
1405 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1406
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001407 err = -ENOMEM;
1408 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1409 if (!ubi->lookuptbl)
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001410 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001411
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001412 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1413 INIT_LIST_HEAD(&ubi->pq[i]);
1414 ubi->pq_head = 0;
1415
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001416 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1417 cond_resched();
1418
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001419 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001420 if (!e)
1421 goto out_free;
1422
1423 e->pnum = seb->pnum;
1424 e->ec = seb->ec;
1425 ubi->lookuptbl[e->pnum] = e;
1426 if (schedule_erase(ubi, e, 0)) {
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001427 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001428 goto out_free;
1429 }
1430 }
1431
1432 list_for_each_entry(seb, &si->free, u.list) {
1433 cond_resched();
1434
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001435 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001436 if (!e)
1437 goto out_free;
1438
1439 e->pnum = seb->pnum;
1440 e->ec = seb->ec;
1441 ubi_assert(e->ec >= 0);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001442 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001443 ubi->lookuptbl[e->pnum] = e;
1444 }
1445
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001446 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1447 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1448 cond_resched();
1449
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001450 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001451 if (!e)
1452 goto out_free;
1453
1454 e->pnum = seb->pnum;
1455 e->ec = seb->ec;
1456 ubi->lookuptbl[e->pnum] = e;
1457 if (!seb->scrub) {
1458 dbg_wl("add PEB %d EC %d to the used tree",
1459 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001460 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001461 } else {
1462 dbg_wl("add PEB %d EC %d to the scrub tree",
1463 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001464 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001465 }
1466 }
1467 }
1468
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001469 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001470 ubi_err("no enough physical eraseblocks (%d, need %d)",
1471 ubi->avail_pebs, WL_RESERVED_PEBS);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +03001472 if (ubi->corr_peb_count)
1473 ubi_err("%d PEBs are corrupted and not used",
1474 ubi->corr_peb_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001475 goto out_free;
1476 }
1477 ubi->avail_pebs -= WL_RESERVED_PEBS;
1478 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1479
1480 /* Schedule wear-leveling if needed */
1481 err = ensure_wear_leveling(ubi);
1482 if (err)
1483 goto out_free;
1484
1485 return 0;
1486
1487out_free:
1488 cancel_pending(ubi);
1489 tree_destroy(&ubi->used);
1490 tree_destroy(&ubi->free);
1491 tree_destroy(&ubi->scrub);
1492 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001493 return err;
1494}
1495
1496/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001497 * protection_queue_destroy - destroy the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001498 * @ubi: UBI device description object
1499 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001500static void protection_queue_destroy(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001501{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001502 int i;
1503 struct ubi_wl_entry *e, *tmp;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001504
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001505 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1506 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1507 list_del(&e->u.list);
1508 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001509 }
1510 }
1511}
1512
1513/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001514 * ubi_wl_close - close the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001515 * @ubi: UBI device description object
1516 */
1517void ubi_wl_close(struct ubi_device *ubi)
1518{
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001519 dbg_wl("close the WL sub-system");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001520 cancel_pending(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001521 protection_queue_destroy(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001522 tree_destroy(&ubi->used);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001523 tree_destroy(&ubi->erroneous);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001524 tree_destroy(&ubi->free);
1525 tree_destroy(&ubi->scrub);
1526 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001527}
1528
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001529#ifdef CONFIG_MTD_UBI_DEBUG
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001530
1531/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001532 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001533 * @ubi: UBI device description object
1534 * @pnum: the physical eraseblock number to check
1535 * @ec: the erase counter to check
1536 *
1537 * This function returns zero if the erase counter of physical eraseblock @pnum
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001538 * is equivalent to @ec, and a negative error code if not or if an error
1539 * occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001540 */
Artem Bityutskiye88d6e102007-08-29 14:51:52 +03001541static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001542{
1543 int err;
1544 long long read_ec;
1545 struct ubi_ec_hdr *ec_hdr;
1546
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001547 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001548 return 0;
1549
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001550 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001551 if (!ec_hdr)
1552 return -ENOMEM;
1553
1554 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1555 if (err && err != UBI_IO_BITFLIPS) {
1556 /* The header does not have to exist */
1557 err = 0;
1558 goto out_free;
1559 }
1560
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001561 read_ec = be64_to_cpu(ec_hdr->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001562 if (ec != read_ec) {
1563 ubi_err("paranoid check failed for PEB %d", pnum);
1564 ubi_err("read EC is %lld, should be %d", read_ec, ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001565 dump_stack();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001566 err = 1;
1567 } else
1568 err = 0;
1569
1570out_free:
1571 kfree(ec_hdr);
1572 return err;
1573}
1574
1575/**
Artem Bityutskiyebaaf1a2008-07-18 13:34:32 +03001576 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001577 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001578 * @e: the wear-leveling entry to check
1579 * @root: the root of the tree
1580 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001581 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1582 * is not.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001583 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001584static int paranoid_check_in_wl_tree(const struct ubi_device *ubi,
1585 struct ubi_wl_entry *e,
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001586 struct rb_root *root)
1587{
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001588 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001589 return 0;
1590
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001591 if (in_wl_tree(e, root))
1592 return 0;
1593
1594 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1595 e->pnum, e->ec, root);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001596 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001597 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001598}
1599
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001600/**
1601 * paranoid_check_in_pq - check if wear-leveling entry is in the protection
1602 * queue.
1603 * @ubi: UBI device description object
1604 * @e: the wear-leveling entry to check
1605 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001606 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001607 */
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001608static int paranoid_check_in_pq(const struct ubi_device *ubi,
1609 struct ubi_wl_entry *e)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001610{
1611 struct ubi_wl_entry *p;
1612 int i;
1613
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001614 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001615 return 0;
1616
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001617 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1618 list_for_each_entry(p, &ubi->pq[i], u.list)
1619 if (p == e)
1620 return 0;
1621
1622 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue",
1623 e->pnum, e->ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001624 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001625 return -EINVAL;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001626}
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001627
1628#endif /* CONFIG_MTD_UBI_DEBUG */