]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mtd/ubi/wl.c
UBI: do not switch to R/O mode on read errors
[net-next-2.6.git] / drivers / mtd / ubi / wl.c
CommitLineData
801c135c
AB
1/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
19 */
20
21/*
85c6e6e2 22 * UBI wear-leveling sub-system.
801c135c 23 *
85c6e6e2 24 * This sub-system is responsible for wear-leveling. It works in terms of
7b6c32da 25 * physical eraseblocks and erase counters and knows nothing about logical
85c6e6e2
AB
26 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
27 * eraseblocks are of two types - used and free. Used physical eraseblocks are
28 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
29 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
801c135c
AB
30 *
31 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
85c6e6e2 32 * header. The rest of the physical eraseblock contains only %0xFF bytes.
801c135c 33 *
85c6e6e2 34 * When physical eraseblocks are returned to the WL sub-system by means of the
801c135c
AB
35 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
36 * done asynchronously in context of the per-UBI device background thread,
85c6e6e2 37 * which is also managed by the WL sub-system.
801c135c
AB
38 *
39 * The wear-leveling is ensured by means of moving the contents of used
40 * physical eraseblocks with low erase counter to free physical eraseblocks
41 * with high erase counter.
42 *
43 * The 'ubi_wl_get_peb()' function accepts data type hints which help to pick
44 * an "optimal" physical eraseblock. For example, when it is known that the
45 * physical eraseblock will be "put" soon because it contains short-term data,
85c6e6e2
AB
46 * the WL sub-system may pick a free physical eraseblock with low erase
47 * counter, and so forth.
801c135c 48 *
85c6e6e2
AB
49 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
50 * bad.
801c135c 51 *
85c6e6e2
AB
52 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
53 * in a physical eraseblock, it has to be moved. Technically this is the same
54 * as moving it for wear-leveling reasons.
801c135c 55 *
85c6e6e2
AB
56 * As it was said, for the UBI sub-system all physical eraseblocks are either
57 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
b86a2c56
AB
58 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
59 * RB-trees, as well as (temporarily) in the @wl->pq queue.
7b6c32da
XX
60 *
61 * When the WL sub-system returns a physical eraseblock, the physical
62 * eraseblock is protected from being moved for some "time". For this reason,
63 * the physical eraseblock is not directly moved from the @wl->free tree to the
64 * @wl->used tree. There is a protection queue in between where this
65 * physical eraseblock is temporarily stored (@wl->pq).
66 *
67 * All this protection stuff is needed because:
68 * o we don't want to move physical eraseblocks just after we have given them
69 * to the user; instead, we first want to let users fill them up with data;
70 *
71 * o there is a chance that the user will put the physical eraseblock very
72 * soon, so it makes sense not to move it for some time, but wait; this is
73 * especially important in case of "short term" physical eraseblocks.
74 *
75 * Physical eraseblocks stay protected only for limited time. But the "time" is
76 * measured in erase cycles in this case. This is implemented with help of the
77 * protection queue. Eraseblocks are put to the tail of this queue when they
78 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
79 * head of the queue on each erase operation (for any eraseblock). So the
80 * length of the queue defines how may (global) erase cycles PEBs are protected.
81 *
82 * To put it differently, each physical eraseblock has 2 main states: free and
83 * used. The former state corresponds to the @wl->free tree. The latter state
84 * is split up on several sub-states:
85 * o the WL movement is allowed (@wl->used tree);
b86a2c56
AB
86 * o the WL movement is disallowed (@wl->erroneous) becouse the PEB is
87 * erroneous - e.g., there was a read error;
7b6c32da
XX
88 * o the WL movement is temporarily prohibited (@wl->pq queue);
89 * o scrubbing is needed (@wl->scrub tree).
90 *
91 * Depending on the sub-state, wear-leveling entries of the used physical
92 * eraseblocks may be kept in one of those structures.
801c135c
AB
93 *
94 * Note, in this implementation, we keep a small in-RAM object for each physical
95 * eraseblock. This is surely not a scalable solution. But it appears to be good
96 * enough for moderately large flashes and it is simple. In future, one may
85c6e6e2 97 * re-work this sub-system and make it more scalable.
801c135c 98 *
85c6e6e2
AB
99 * At the moment this sub-system does not utilize the sequence number, which
100 * was introduced relatively recently. But it would be wise to do this because
101 * the sequence number of a logical eraseblock characterizes how old is it. For
801c135c
AB
102 * example, when we move a PEB with low erase counter, and we need to pick the
103 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
104 * pick target PEB with an average EC if our PEB is not very "old". This is a
85c6e6e2 105 * room for future re-works of the WL sub-system.
801c135c
AB
106 */
107
108#include <linux/slab.h>
109#include <linux/crc32.h>
110#include <linux/freezer.h>
111#include <linux/kthread.h>
112#include "ubi.h"
113
114/* Number of physical eraseblocks reserved for wear-leveling purposes */
115#define WL_RESERVED_PEBS 1
116
801c135c
AB
117/*
118 * Maximum difference between two erase counters. If this threshold is
85c6e6e2
AB
119 * exceeded, the WL sub-system starts moving data from used physical
120 * eraseblocks with low erase counter to free physical eraseblocks with high
121 * erase counter.
801c135c
AB
122 */
123#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
124
125/*
85c6e6e2 126 * When a physical eraseblock is moved, the WL sub-system has to pick the target
801c135c
AB
127 * physical eraseblock to move to. The simplest way would be just to pick the
128 * one with the highest erase counter. But in certain workloads this could lead
129 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
130 * situation when the picked physical eraseblock is constantly erased after the
131 * data is written to it. So, we have a constant which limits the highest erase
85c6e6e2 132 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
025dfdaf 133 * does not pick eraseblocks with erase counter greater than the lowest erase
801c135c
AB
134 * counter plus %WL_FREE_MAX_DIFF.
135 */
136#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
137
138/*
139 * Maximum number of consecutive background thread failures which is enough to
140 * switch to read-only mode.
141 */
142#define WL_MAX_FAILURES 32
143
801c135c
AB
144/**
145 * struct ubi_work - UBI work description data structure.
146 * @list: a link in the list of pending works
147 * @func: worker function
801c135c
AB
148 * @e: physical eraseblock to erase
149 * @torture: if the physical eraseblock has to be tortured
150 *
151 * The @func pointer points to the worker function. If the @cancel argument is
152 * not zero, the worker has to free the resources and exit immediately. The
153 * worker has to return zero in case of success and a negative error code in
154 * case of failure.
155 */
156struct ubi_work {
157 struct list_head list;
158 int (*func)(struct ubi_device *ubi, struct ubi_work *wrk, int cancel);
159 /* The below fields are only relevant to erasure works */
160 struct ubi_wl_entry *e;
161 int torture;
162};
163
164#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
e88d6e10 165static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec);
801c135c
AB
166static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
167 struct rb_root *root);
7b6c32da 168static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e);
801c135c
AB
169#else
170#define paranoid_check_ec(ubi, pnum, ec) 0
171#define paranoid_check_in_wl_tree(e, root)
7b6c32da 172#define paranoid_check_in_pq(ubi, e) 0
801c135c
AB
173#endif
174
801c135c
AB
175/**
176 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
177 * @e: the wear-leveling entry to add
178 * @root: the root of the tree
179 *
180 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
181 * the @ubi->used and @ubi->free RB-trees.
182 */
183static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
184{
185 struct rb_node **p, *parent = NULL;
186
187 p = &root->rb_node;
188 while (*p) {
189 struct ubi_wl_entry *e1;
190
191 parent = *p;
23553b2c 192 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
801c135c
AB
193
194 if (e->ec < e1->ec)
195 p = &(*p)->rb_left;
196 else if (e->ec > e1->ec)
197 p = &(*p)->rb_right;
198 else {
199 ubi_assert(e->pnum != e1->pnum);
200 if (e->pnum < e1->pnum)
201 p = &(*p)->rb_left;
202 else
203 p = &(*p)->rb_right;
204 }
205 }
206
23553b2c
XX
207 rb_link_node(&e->u.rb, parent, p);
208 rb_insert_color(&e->u.rb, root);
801c135c
AB
209}
210
801c135c
AB
211/**
212 * do_work - do one pending work.
213 * @ubi: UBI device description object
214 *
215 * This function returns zero in case of success and a negative error code in
216 * case of failure.
217 */
218static int do_work(struct ubi_device *ubi)
219{
220 int err;
221 struct ubi_work *wrk;
222
43f9b25a
AB
223 cond_resched();
224
593dd33c
AB
225 /*
226 * @ubi->work_sem is used to synchronize with the workers. Workers take
227 * it in read mode, so many of them may be doing works at a time. But
228 * the queue flush code has to be sure the whole queue of works is
229 * done, and it takes the mutex in write mode.
230 */
231 down_read(&ubi->work_sem);
801c135c 232 spin_lock(&ubi->wl_lock);
801c135c
AB
233 if (list_empty(&ubi->works)) {
234 spin_unlock(&ubi->wl_lock);
593dd33c 235 up_read(&ubi->work_sem);
801c135c
AB
236 return 0;
237 }
238
239 wrk = list_entry(ubi->works.next, struct ubi_work, list);
240 list_del(&wrk->list);
16f557ec
AB
241 ubi->works_count -= 1;
242 ubi_assert(ubi->works_count >= 0);
801c135c
AB
243 spin_unlock(&ubi->wl_lock);
244
245 /*
246 * Call the worker function. Do not touch the work structure
247 * after this call as it will have been freed or reused by that
248 * time by the worker function.
249 */
250 err = wrk->func(ubi, wrk, 0);
251 if (err)
252 ubi_err("work failed with error code %d", err);
593dd33c 253 up_read(&ubi->work_sem);
16f557ec 254
801c135c
AB
255 return err;
256}
257
258/**
259 * produce_free_peb - produce a free physical eraseblock.
260 * @ubi: UBI device description object
261 *
262 * This function tries to make a free PEB by means of synchronous execution of
263 * pending works. This may be needed if, for example the background thread is
264 * disabled. Returns zero in case of success and a negative error code in case
265 * of failure.
266 */
267static int produce_free_peb(struct ubi_device *ubi)
268{
269 int err;
270
271 spin_lock(&ubi->wl_lock);
5abde384 272 while (!ubi->free.rb_node) {
801c135c
AB
273 spin_unlock(&ubi->wl_lock);
274
275 dbg_wl("do one work synchronously");
276 err = do_work(ubi);
277 if (err)
278 return err;
279
280 spin_lock(&ubi->wl_lock);
281 }
282 spin_unlock(&ubi->wl_lock);
283
284 return 0;
285}
286
287/**
288 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
289 * @e: the wear-leveling entry to check
290 * @root: the root of the tree
291 *
292 * This function returns non-zero if @e is in the @root RB-tree and zero if it
293 * is not.
294 */
295static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
296{
297 struct rb_node *p;
298
299 p = root->rb_node;
300 while (p) {
301 struct ubi_wl_entry *e1;
302
23553b2c 303 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
801c135c
AB
304
305 if (e->pnum == e1->pnum) {
306 ubi_assert(e == e1);
307 return 1;
308 }
309
310 if (e->ec < e1->ec)
311 p = p->rb_left;
312 else if (e->ec > e1->ec)
313 p = p->rb_right;
314 else {
315 ubi_assert(e->pnum != e1->pnum);
316 if (e->pnum < e1->pnum)
317 p = p->rb_left;
318 else
319 p = p->rb_right;
320 }
321 }
322
323 return 0;
324}
325
326/**
7b6c32da 327 * prot_queue_add - add physical eraseblock to the protection queue.
801c135c
AB
328 * @ubi: UBI device description object
329 * @e: the physical eraseblock to add
801c135c 330 *
7b6c32da
XX
331 * This function adds @e to the tail of the protection queue @ubi->pq, where
332 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
333 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
334 * be locked.
801c135c 335 */
7b6c32da 336static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
801c135c 337{
7b6c32da 338 int pq_tail = ubi->pq_head - 1;
801c135c 339
7b6c32da
XX
340 if (pq_tail < 0)
341 pq_tail = UBI_PROT_QUEUE_LEN - 1;
342 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
343 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
344 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
801c135c
AB
345}
346
347/**
348 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
349 * @root: the RB-tree where to look for
350 * @max: highest possible erase counter
351 *
352 * This function looks for a wear leveling entry with erase counter closest to
353 * @max and less then @max.
354 */
355static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int max)
356{
357 struct rb_node *p;
358 struct ubi_wl_entry *e;
359
23553b2c 360 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
801c135c
AB
361 max += e->ec;
362
363 p = root->rb_node;
364 while (p) {
365 struct ubi_wl_entry *e1;
366
23553b2c 367 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
801c135c
AB
368 if (e1->ec >= max)
369 p = p->rb_left;
370 else {
371 p = p->rb_right;
372 e = e1;
373 }
374 }
375
376 return e;
377}
378
379/**
380 * ubi_wl_get_peb - get a physical eraseblock.
381 * @ubi: UBI device description object
382 * @dtype: type of data which will be stored in this physical eraseblock
383 *
384 * This function returns a physical eraseblock in case of success and a
385 * negative error code in case of failure. Might sleep.
386 */
387int ubi_wl_get_peb(struct ubi_device *ubi, int dtype)
388{
7b6c32da 389 int err, medium_ec;
801c135c 390 struct ubi_wl_entry *e, *first, *last;
801c135c
AB
391
392 ubi_assert(dtype == UBI_LONGTERM || dtype == UBI_SHORTTERM ||
393 dtype == UBI_UNKNOWN);
394
801c135c
AB
395retry:
396 spin_lock(&ubi->wl_lock);
5abde384 397 if (!ubi->free.rb_node) {
801c135c
AB
398 if (ubi->works_count == 0) {
399 ubi_assert(list_empty(&ubi->works));
400 ubi_err("no free eraseblocks");
401 spin_unlock(&ubi->wl_lock);
801c135c
AB
402 return -ENOSPC;
403 }
404 spin_unlock(&ubi->wl_lock);
405
406 err = produce_free_peb(ubi);
7b6c32da 407 if (err < 0)
801c135c 408 return err;
801c135c
AB
409 goto retry;
410 }
411
412 switch (dtype) {
9c9ec147
AB
413 case UBI_LONGTERM:
414 /*
415 * For long term data we pick a physical eraseblock with high
416 * erase counter. But the highest erase counter we can pick is
417 * bounded by the the lowest erase counter plus
418 * %WL_FREE_MAX_DIFF.
419 */
420 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
9c9ec147
AB
421 break;
422 case UBI_UNKNOWN:
423 /*
424 * For unknown data we pick a physical eraseblock with medium
425 * erase counter. But we by no means can pick a physical
426 * eraseblock with erase counter greater or equivalent than the
427 * lowest erase counter plus %WL_FREE_MAX_DIFF.
428 */
23553b2c
XX
429 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry,
430 u.rb);
431 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
801c135c 432
9c9ec147
AB
433 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
434 e = rb_entry(ubi->free.rb_node,
23553b2c 435 struct ubi_wl_entry, u.rb);
9c9ec147
AB
436 else {
437 medium_ec = (first->ec + WL_FREE_MAX_DIFF)/2;
438 e = find_wl_entry(&ubi->free, medium_ec);
439 }
9c9ec147
AB
440 break;
441 case UBI_SHORTTERM:
442 /*
443 * For short term data we pick a physical eraseblock with the
444 * lowest erase counter as we expect it will be erased soon.
445 */
23553b2c 446 e = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
9c9ec147
AB
447 break;
448 default:
9c9ec147 449 BUG();
801c135c
AB
450 }
451
7b6c32da
XX
452 paranoid_check_in_wl_tree(e, &ubi->free);
453
801c135c 454 /*
7b6c32da 455 * Move the physical eraseblock to the protection queue where it will
801c135c
AB
456 * be protected from being moved for some time.
457 */
23553b2c 458 rb_erase(&e->u.rb, &ubi->free);
7b6c32da
XX
459 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
460 prot_queue_add(ubi, e);
801c135c 461 spin_unlock(&ubi->wl_lock);
801c135c
AB
462 return e->pnum;
463}
464
465/**
7b6c32da 466 * prot_queue_del - remove a physical eraseblock from the protection queue.
801c135c
AB
467 * @ubi: UBI device description object
468 * @pnum: the physical eraseblock to remove
43f9b25a 469 *
7b6c32da
XX
470 * This function deletes PEB @pnum from the protection queue and returns zero
471 * in case of success and %-ENODEV if the PEB was not found.
801c135c 472 */
7b6c32da 473static int prot_queue_del(struct ubi_device *ubi, int pnum)
801c135c 474{
7b6c32da 475 struct ubi_wl_entry *e;
801c135c 476
7b6c32da
XX
477 e = ubi->lookuptbl[pnum];
478 if (!e)
479 return -ENODEV;
801c135c 480
7b6c32da
XX
481 if (paranoid_check_in_pq(ubi, e))
482 return -ENODEV;
43f9b25a 483
7b6c32da
XX
484 list_del(&e->u.list);
485 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
43f9b25a 486 return 0;
801c135c
AB
487}
488
489/**
490 * sync_erase - synchronously erase a physical eraseblock.
491 * @ubi: UBI device description object
492 * @e: the the physical eraseblock to erase
493 * @torture: if the physical eraseblock has to be tortured
494 *
495 * This function returns zero in case of success and a negative error code in
496 * case of failure.
497 */
9c9ec147
AB
498static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
499 int torture)
801c135c
AB
500{
501 int err;
502 struct ubi_ec_hdr *ec_hdr;
503 unsigned long long ec = e->ec;
504
505 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
506
507 err = paranoid_check_ec(ubi, e->pnum, e->ec);
508 if (err > 0)
509 return -EINVAL;
510
33818bbb 511 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
801c135c
AB
512 if (!ec_hdr)
513 return -ENOMEM;
514
515 err = ubi_io_sync_erase(ubi, e->pnum, torture);
516 if (err < 0)
517 goto out_free;
518
519 ec += err;
520 if (ec > UBI_MAX_ERASECOUNTER) {
521 /*
522 * Erase counter overflow. Upgrade UBI and use 64-bit
523 * erase counters internally.
524 */
525 ubi_err("erase counter overflow at PEB %d, EC %llu",
526 e->pnum, ec);
527 err = -EINVAL;
528 goto out_free;
529 }
530
531 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
532
3261ebd7 533 ec_hdr->ec = cpu_to_be64(ec);
801c135c
AB
534
535 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
536 if (err)
537 goto out_free;
538
539 e->ec = ec;
540 spin_lock(&ubi->wl_lock);
541 if (e->ec > ubi->max_ec)
542 ubi->max_ec = e->ec;
543 spin_unlock(&ubi->wl_lock);
544
545out_free:
546 kfree(ec_hdr);
547 return err;
548}
549
550/**
7b6c32da 551 * serve_prot_queue - check if it is time to stop protecting PEBs.
801c135c
AB
552 * @ubi: UBI device description object
553 *
7b6c32da
XX
554 * This function is called after each erase operation and removes PEBs from the
555 * tail of the protection queue. These PEBs have been protected for long enough
556 * and should be moved to the used tree.
801c135c 557 */
7b6c32da 558static void serve_prot_queue(struct ubi_device *ubi)
801c135c 559{
7b6c32da
XX
560 struct ubi_wl_entry *e, *tmp;
561 int count;
801c135c
AB
562
563 /*
564 * There may be several protected physical eraseblock to remove,
565 * process them all.
566 */
7b6c32da
XX
567repeat:
568 count = 0;
569 spin_lock(&ubi->wl_lock);
570 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
571 dbg_wl("PEB %d EC %d protection over, move to used tree",
572 e->pnum, e->ec);
801c135c 573
7b6c32da
XX
574 list_del(&e->u.list);
575 wl_tree_add(e, &ubi->used);
576 if (count++ > 32) {
577 /*
578 * Let's be nice and avoid holding the spinlock for
579 * too long.
580 */
801c135c 581 spin_unlock(&ubi->wl_lock);
7b6c32da
XX
582 cond_resched();
583 goto repeat;
801c135c 584 }
801c135c 585 }
7b6c32da
XX
586
587 ubi->pq_head += 1;
588 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
589 ubi->pq_head = 0;
590 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
591 spin_unlock(&ubi->wl_lock);
801c135c
AB
592}
593
594/**
595 * schedule_ubi_work - schedule a work.
596 * @ubi: UBI device description object
597 * @wrk: the work to schedule
598 *
7b6c32da
XX
599 * This function adds a work defined by @wrk to the tail of the pending works
600 * list.
801c135c
AB
601 */
602static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
603{
604 spin_lock(&ubi->wl_lock);
605 list_add_tail(&wrk->list, &ubi->works);
606 ubi_assert(ubi->works_count >= 0);
607 ubi->works_count += 1;
608 if (ubi->thread_enabled)
609 wake_up_process(ubi->bgt_thread);
610 spin_unlock(&ubi->wl_lock);
611}
612
613static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
614 int cancel);
615
616/**
617 * schedule_erase - schedule an erase work.
618 * @ubi: UBI device description object
619 * @e: the WL entry of the physical eraseblock to erase
620 * @torture: if the physical eraseblock has to be tortured
621 *
622 * This function returns zero in case of success and a %-ENOMEM in case of
623 * failure.
624 */
625static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
626 int torture)
627{
628 struct ubi_work *wl_wrk;
629
630 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
631 e->pnum, e->ec, torture);
632
33818bbb 633 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
801c135c
AB
634 if (!wl_wrk)
635 return -ENOMEM;
636
637 wl_wrk->func = &erase_worker;
638 wl_wrk->e = e;
639 wl_wrk->torture = torture;
640
641 schedule_ubi_work(ubi, wl_wrk);
642 return 0;
643}
644
645/**
646 * wear_leveling_worker - wear-leveling worker function.
647 * @ubi: UBI device description object
648 * @wrk: the work object
649 * @cancel: non-zero if the worker has to free memory and exit
650 *
651 * This function copies a more worn out physical eraseblock to a less worn out
652 * one. Returns zero in case of success and a negative error code in case of
653 * failure.
654 */
655static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
656 int cancel)
657{
b86a2c56 658 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
801c135c
AB
659 struct ubi_wl_entry *e1, *e2;
660 struct ubi_vid_hdr *vid_hdr;
661
662 kfree(wrk);
801c135c
AB
663 if (cancel)
664 return 0;
665
33818bbb 666 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
801c135c
AB
667 if (!vid_hdr)
668 return -ENOMEM;
669
43f9b25a 670 mutex_lock(&ubi->move_mutex);
801c135c 671 spin_lock(&ubi->wl_lock);
43f9b25a
AB
672 ubi_assert(!ubi->move_from && !ubi->move_to);
673 ubi_assert(!ubi->move_to_put);
801c135c 674
43f9b25a 675 if (!ubi->free.rb_node ||
5abde384 676 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
801c135c 677 /*
43f9b25a
AB
678 * No free physical eraseblocks? Well, they must be waiting in
679 * the queue to be erased. Cancel movement - it will be
680 * triggered again when a free physical eraseblock appears.
801c135c
AB
681 *
682 * No used physical eraseblocks? They must be temporarily
683 * protected from being moved. They will be moved to the
684 * @ubi->used tree later and the wear-leveling will be
685 * triggered again.
686 */
687 dbg_wl("cancel WL, a list is empty: free %d, used %d",
5abde384 688 !ubi->free.rb_node, !ubi->used.rb_node);
43f9b25a 689 goto out_cancel;
801c135c
AB
690 }
691
5abde384 692 if (!ubi->scrub.rb_node) {
801c135c
AB
693 /*
694 * Now pick the least worn-out used physical eraseblock and a
695 * highly worn-out free physical eraseblock. If the erase
696 * counters differ much enough, start wear-leveling.
697 */
23553b2c 698 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
801c135c
AB
699 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
700
701 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
702 dbg_wl("no WL needed: min used EC %d, max free EC %d",
703 e1->ec, e2->ec);
43f9b25a 704 goto out_cancel;
801c135c 705 }
5abde384 706 paranoid_check_in_wl_tree(e1, &ubi->used);
23553b2c 707 rb_erase(&e1->u.rb, &ubi->used);
801c135c
AB
708 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
709 e1->pnum, e1->ec, e2->pnum, e2->ec);
710 } else {
43f9b25a
AB
711 /* Perform scrubbing */
712 scrubbing = 1;
23553b2c 713 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
801c135c 714 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
5abde384 715 paranoid_check_in_wl_tree(e1, &ubi->scrub);
23553b2c 716 rb_erase(&e1->u.rb, &ubi->scrub);
801c135c
AB
717 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
718 }
719
5abde384 720 paranoid_check_in_wl_tree(e2, &ubi->free);
23553b2c 721 rb_erase(&e2->u.rb, &ubi->free);
801c135c
AB
722 ubi->move_from = e1;
723 ubi->move_to = e2;
724 spin_unlock(&ubi->wl_lock);
725
726 /*
727 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
728 * We so far do not know which logical eraseblock our physical
729 * eraseblock (@e1) belongs to. We have to read the volume identifier
730 * header first.
43f9b25a
AB
731 *
732 * Note, we are protected from this PEB being unmapped and erased. The
733 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
734 * which is being moved was unmapped.
801c135c
AB
735 */
736
737 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
738 if (err && err != UBI_IO_BITFLIPS) {
739 if (err == UBI_IO_PEB_FREE) {
740 /*
741 * We are trying to move PEB without a VID header. UBI
742 * always write VID headers shortly after the PEB was
87960c0b
AB
743 * given, so we have a situation when it has not yet
744 * had a chance to write it, because it was preempted.
745 * So add this PEB to the protection queue so far,
746 * because presubably more data will be written there
747 * (including the missin VID header), and then we'll
748 * move it.
801c135c
AB
749 */
750 dbg_wl("PEB %d has no VID header", e1->pnum);
87960c0b 751 protect = 1;
43f9b25a 752 goto out_not_moved;
801c135c 753 }
43f9b25a
AB
754
755 ubi_err("error %d while reading VID header from PEB %d",
756 err, e1->pnum);
43f9b25a 757 goto out_error;
801c135c
AB
758 }
759
760 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
761 if (err) {
87960c0b
AB
762 if (err == MOVE_CANCEL_RACE) {
763 /*
764 * The LEB has not been moved because the volume is
765 * being deleted or the PEB has been put meanwhile. We
766 * should prevent this PEB from being selected for
767 * wear-leveling movement again, so put it to the
768 * protection queue.
769 */
770 protect = 1;
771 goto out_not_moved;
772 }
773
b86a2c56
AB
774 if (err == MOVE_CANCEL_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
775 err == MOVE_TARGET_RD_ERR) {
90bf0265 776 /* Target PEB bit-flips or write error, torture it */
6fa6f5bb 777 torture = 1;
43f9b25a 778 goto out_not_moved;
6fa6f5bb 779 }
87960c0b 780
b86a2c56
AB
781 if (err == MOVE_SOURCE_RD_ERR) {
782 /*
783 * An error happened while reading the source PEB. Do
784 * not switch to R/O mode in this case, and give the
785 * upper layers a possibility to recover from this,
786 * e.g. by unmapping corresponding LEB. Instead, just
787 * put thie PEB to the @ubi->erroneus list to prevent
788 * UBI from trying to move the over and over again.
789 */
790 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
791 ubi_err("too many erroneous eraseblocks (%d)",
792 ubi->erroneous_peb_count);
793 goto out_error;
794 }
795 erroneous = 1;
796 goto out_not_moved;
797 }
798
90bf0265
AB
799 if (err < 0)
800 goto out_error;
43f9b25a 801
87960c0b 802 ubi_assert(0);
801c135c
AB
803 }
804
6a8f483f 805 /* The PEB has been successfully moved */
801c135c 806 ubi_free_vid_hdr(ubi, vid_hdr);
6a8f483f 807 if (scrubbing)
8c1e6ee1
AB
808 ubi_msg("scrubbed PEB %d, data moved to PEB %d",
809 e1->pnum, e2->pnum);
810
801c135c 811 spin_lock(&ubi->wl_lock);
3c98b0a0 812 if (!ubi->move_to_put) {
5abde384 813 wl_tree_add(e2, &ubi->used);
3c98b0a0
AB
814 e2 = NULL;
815 }
801c135c 816 ubi->move_from = ubi->move_to = NULL;
43f9b25a 817 ubi->move_to_put = ubi->wl_scheduled = 0;
801c135c
AB
818 spin_unlock(&ubi->wl_lock);
819
6a8f483f 820 err = schedule_erase(ubi, e1, 0);
3c98b0a0 821 if (err) {
87960c0b
AB
822 kmem_cache_free(ubi_wl_entry_slab, e1);
823 kmem_cache_free(ubi_wl_entry_slab, e2);
824 goto out_ro;
3c98b0a0 825 }
6a8f483f 826
3c98b0a0 827 if (e2) {
801c135c
AB
828 /*
829 * Well, the target PEB was put meanwhile, schedule it for
830 * erasure.
831 */
832 dbg_wl("PEB %d was put meanwhile, erase", e2->pnum);
833 err = schedule_erase(ubi, e2, 0);
87960c0b
AB
834 if (err) {
835 kmem_cache_free(ubi_wl_entry_slab, e2);
836 goto out_ro;
837 }
801c135c
AB
838 }
839
801c135c 840 dbg_wl("done");
43f9b25a
AB
841 mutex_unlock(&ubi->move_mutex);
842 return 0;
801c135c
AB
843
844 /*
43f9b25a
AB
845 * For some reasons the LEB was not moved, might be an error, might be
846 * something else. @e1 was not changed, so return it back. @e2 might
6fa6f5bb 847 * have been changed, schedule it for erasure.
801c135c 848 */
43f9b25a 849out_not_moved:
87960c0b
AB
850 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
851 e1->pnum, e2->pnum, err);
801c135c 852 spin_lock(&ubi->wl_lock);
87960c0b
AB
853 if (protect)
854 prot_queue_add(ubi, e1);
b86a2c56
AB
855 else if (erroneous) {
856 wl_tree_add(e1, &ubi->erroneous);
857 ubi->erroneous_peb_count += 1;
858 } else if (scrubbing)
43f9b25a 859 wl_tree_add(e1, &ubi->scrub);
801c135c 860 else
5abde384 861 wl_tree_add(e1, &ubi->used);
6fa6f5bb 862 ubi_assert(!ubi->move_to_put);
801c135c 863 ubi->move_from = ubi->move_to = NULL;
6fa6f5bb 864 ubi->wl_scheduled = 0;
801c135c
AB
865 spin_unlock(&ubi->wl_lock);
866
87960c0b 867 ubi_free_vid_hdr(ubi, vid_hdr);
6fa6f5bb 868 err = schedule_erase(ubi, e2, torture);
87960c0b
AB
869 if (err) {
870 kmem_cache_free(ubi_wl_entry_slab, e2);
871 goto out_ro;
872 }
43f9b25a
AB
873 mutex_unlock(&ubi->move_mutex);
874 return 0;
875
876out_error:
877 ubi_err("error %d while moving PEB %d to PEB %d",
878 err, e1->pnum, e2->pnum);
43f9b25a
AB
879 spin_lock(&ubi->wl_lock);
880 ubi->move_from = ubi->move_to = NULL;
881 ubi->move_to_put = ubi->wl_scheduled = 0;
882 spin_unlock(&ubi->wl_lock);
883
87960c0b
AB
884 ubi_free_vid_hdr(ubi, vid_hdr);
885 kmem_cache_free(ubi_wl_entry_slab, e1);
886 kmem_cache_free(ubi_wl_entry_slab, e2);
43f9b25a 887
87960c0b
AB
888out_ro:
889 ubi_ro_mode(ubi);
43f9b25a 890 mutex_unlock(&ubi->move_mutex);
87960c0b
AB
891 ubi_assert(err != 0);
892 return err < 0 ? err : -EIO;
43f9b25a
AB
893
894out_cancel:
895 ubi->wl_scheduled = 0;
896 spin_unlock(&ubi->wl_lock);
897 mutex_unlock(&ubi->move_mutex);
898 ubi_free_vid_hdr(ubi, vid_hdr);
899 return 0;
801c135c
AB
900}
901
902/**
903 * ensure_wear_leveling - schedule wear-leveling if it is needed.
904 * @ubi: UBI device description object
905 *
906 * This function checks if it is time to start wear-leveling and schedules it
907 * if yes. This function returns zero in case of success and a negative error
908 * code in case of failure.
909 */
910static int ensure_wear_leveling(struct ubi_device *ubi)
911{
912 int err = 0;
913 struct ubi_wl_entry *e1;
914 struct ubi_wl_entry *e2;
915 struct ubi_work *wrk;
916
917 spin_lock(&ubi->wl_lock);
918 if (ubi->wl_scheduled)
919 /* Wear-leveling is already in the work queue */
920 goto out_unlock;
921
922 /*
923 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
924 * the WL worker has to be scheduled anyway.
925 */
5abde384
AB
926 if (!ubi->scrub.rb_node) {
927 if (!ubi->used.rb_node || !ubi->free.rb_node)
801c135c
AB
928 /* No physical eraseblocks - no deal */
929 goto out_unlock;
930
931 /*
932 * We schedule wear-leveling only if the difference between the
933 * lowest erase counter of used physical eraseblocks and a high
025dfdaf 934 * erase counter of free physical eraseblocks is greater than
801c135c
AB
935 * %UBI_WL_THRESHOLD.
936 */
23553b2c 937 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
801c135c
AB
938 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
939
940 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
941 goto out_unlock;
942 dbg_wl("schedule wear-leveling");
943 } else
944 dbg_wl("schedule scrubbing");
945
946 ubi->wl_scheduled = 1;
947 spin_unlock(&ubi->wl_lock);
948
33818bbb 949 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
801c135c
AB
950 if (!wrk) {
951 err = -ENOMEM;
952 goto out_cancel;
953 }
954
955 wrk->func = &wear_leveling_worker;
956 schedule_ubi_work(ubi, wrk);
957 return err;
958
959out_cancel:
960 spin_lock(&ubi->wl_lock);
961 ubi->wl_scheduled = 0;
962out_unlock:
963 spin_unlock(&ubi->wl_lock);
964 return err;
965}
966
967/**
968 * erase_worker - physical eraseblock erase worker function.
969 * @ubi: UBI device description object
970 * @wl_wrk: the work object
971 * @cancel: non-zero if the worker has to free memory and exit
972 *
973 * This function erases a physical eraseblock and perform torture testing if
974 * needed. It also takes care about marking the physical eraseblock bad if
975 * needed. Returns zero in case of success and a negative error code in case of
976 * failure.
977 */
978static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
979 int cancel)
980{
801c135c 981 struct ubi_wl_entry *e = wl_wrk->e;
784c1454 982 int pnum = e->pnum, err, need;
801c135c
AB
983
984 if (cancel) {
985 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
986 kfree(wl_wrk);
06b68ba1 987 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c
AB
988 return 0;
989 }
990
991 dbg_wl("erase PEB %d EC %d", pnum, e->ec);
992
993 err = sync_erase(ubi, e, wl_wrk->torture);
994 if (!err) {
995 /* Fine, we've erased it successfully */
996 kfree(wl_wrk);
997
998 spin_lock(&ubi->wl_lock);
5abde384 999 wl_tree_add(e, &ubi->free);
801c135c
AB
1000 spin_unlock(&ubi->wl_lock);
1001
1002 /*
9c9ec147
AB
1003 * One more erase operation has happened, take care about
1004 * protected physical eraseblocks.
801c135c 1005 */
7b6c32da 1006 serve_prot_queue(ubi);
801c135c
AB
1007
1008 /* And take care about wear-leveling */
1009 err = ensure_wear_leveling(ubi);
1010 return err;
1011 }
1012
8d2d4011 1013 ubi_err("failed to erase PEB %d, error %d", pnum, err);
801c135c 1014 kfree(wl_wrk);
06b68ba1 1015 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c 1016
784c1454
AB
1017 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1018 err == -EBUSY) {
1019 int err1;
1020
1021 /* Re-schedule the LEB for erasure */
1022 err1 = schedule_erase(ubi, e, 0);
1023 if (err1) {
1024 err = err1;
1025 goto out_ro;
1026 }
1027 return err;
1028 } else if (err != -EIO) {
801c135c
AB
1029 /*
1030 * If this is not %-EIO, we have no idea what to do. Scheduling
1031 * this physical eraseblock for erasure again would cause
1032 * errors again and again. Well, lets switch to RO mode.
1033 */
784c1454 1034 goto out_ro;
801c135c
AB
1035 }
1036
1037 /* It is %-EIO, the PEB went bad */
1038
1039 if (!ubi->bad_allowed) {
1040 ubi_err("bad physical eraseblock %d detected", pnum);
784c1454
AB
1041 goto out_ro;
1042 }
801c135c 1043
784c1454
AB
1044 spin_lock(&ubi->volumes_lock);
1045 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1046 if (need > 0) {
1047 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1048 ubi->avail_pebs -= need;
1049 ubi->rsvd_pebs += need;
1050 ubi->beb_rsvd_pebs += need;
1051 if (need > 0)
1052 ubi_msg("reserve more %d PEBs", need);
1053 }
801c135c 1054
784c1454 1055 if (ubi->beb_rsvd_pebs == 0) {
801c135c 1056 spin_unlock(&ubi->volumes_lock);
784c1454
AB
1057 ubi_err("no reserved physical eraseblocks");
1058 goto out_ro;
1059 }
801c135c 1060
784c1454
AB
1061 spin_unlock(&ubi->volumes_lock);
1062 ubi_msg("mark PEB %d as bad", pnum);
801c135c 1063
784c1454
AB
1064 err = ubi_io_mark_bad(ubi, pnum);
1065 if (err)
1066 goto out_ro;
1067
1068 spin_lock(&ubi->volumes_lock);
1069 ubi->beb_rsvd_pebs -= 1;
1070 ubi->bad_peb_count += 1;
1071 ubi->good_peb_count -= 1;
1072 ubi_calculate_reserved(ubi);
1073 if (ubi->beb_rsvd_pebs == 0)
1074 ubi_warn("last PEB from the reserved pool was used");
1075 spin_unlock(&ubi->volumes_lock);
1076
1077 return err;
801c135c 1078
784c1454
AB
1079out_ro:
1080 ubi_ro_mode(ubi);
801c135c
AB
1081 return err;
1082}
1083
1084/**
85c6e6e2 1085 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
801c135c
AB
1086 * @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
43f9b25a 1093 * in case of success, and a negative error code in case of failure.
801c135c
AB
1094 */
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
43f9b25a 1104retry:
801c135c 1105 spin_lock(&ubi->wl_lock);
801c135c
AB
1106 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 */
43f9b25a 1113 dbg_wl("PEB %d is being moved, wait", pnum);
801c135c 1114 spin_unlock(&ubi->wl_lock);
43f9b25a
AB
1115
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;
801c135c
AB
1120 } 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
85c6e6e2
AB
1124 * 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.
801c135c
AB
1129 */
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 {
5abde384
AB
1136 if (in_wl_tree(e, &ubi->used)) {
1137 paranoid_check_in_wl_tree(e, &ubi->used);
23553b2c 1138 rb_erase(&e->u.rb, &ubi->used);
5abde384
AB
1139 } else if (in_wl_tree(e, &ubi->scrub)) {
1140 paranoid_check_in_wl_tree(e, &ubi->scrub);
23553b2c 1141 rb_erase(&e->u.rb, &ubi->scrub);
b86a2c56
AB
1142 } else if (in_wl_tree(e, &ubi->erroneous)) {
1143 paranoid_check_in_wl_tree(e, &ubi->erroneous);
1144 rb_erase(&e->u.rb, &ubi->erroneous);
1145 ubi->erroneous_peb_count -= 1;
1146 ubi_assert(ubi->erroneous_peb_count >= 0);
1147 /* Erronious PEBs should be tortured */
1148 torture = 1;
43f9b25a 1149 } else {
7b6c32da 1150 err = prot_queue_del(ubi, e->pnum);
43f9b25a
AB
1151 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 }
801c135c
AB
1158 }
1159 spin_unlock(&ubi->wl_lock);
1160
1161 err = schedule_erase(ubi, e, torture);
1162 if (err) {
1163 spin_lock(&ubi->wl_lock);
5abde384 1164 wl_tree_add(e, &ubi->used);
801c135c
AB
1165 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
8c1e6ee1 1185 dbg_msg("schedule PEB %d for scrubbing", pnum);
801c135c
AB
1186
1187retry:
1188 spin_lock(&ubi->wl_lock);
1189 e = ubi->lookuptbl[pnum];
1190 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub)) {
1191 spin_unlock(&ubi->wl_lock);
1192 return 0;
1193 }
1194
1195 if (e == ubi->move_to) {
1196 /*
1197 * This physical eraseblock was used to move data to. The data
1198 * was moved but the PEB was not yet inserted to the proper
1199 * tree. We should just wait a little and let the WL worker
1200 * proceed.
1201 */
1202 spin_unlock(&ubi->wl_lock);
1203 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1204 yield();
1205 goto retry;
1206 }
1207
5abde384
AB
1208 if (in_wl_tree(e, &ubi->used)) {
1209 paranoid_check_in_wl_tree(e, &ubi->used);
23553b2c 1210 rb_erase(&e->u.rb, &ubi->used);
43f9b25a
AB
1211 } else {
1212 int err;
1213
7b6c32da 1214 err = prot_queue_del(ubi, e->pnum);
43f9b25a
AB
1215 if (err) {
1216 ubi_err("PEB %d not found", pnum);
1217 ubi_ro_mode(ubi);
1218 spin_unlock(&ubi->wl_lock);
1219 return err;
1220 }
1221 }
801c135c 1222
5abde384 1223 wl_tree_add(e, &ubi->scrub);
801c135c
AB
1224 spin_unlock(&ubi->wl_lock);
1225
1226 /*
1227 * Technically scrubbing is the same as wear-leveling, so it is done
1228 * by the WL worker.
1229 */
1230 return ensure_wear_leveling(ubi);
1231}
1232
1233/**
1234 * ubi_wl_flush - flush all pending works.
1235 * @ubi: UBI device description object
1236 *
1237 * This function returns zero in case of success and a negative error code in
1238 * case of failure.
1239 */
1240int ubi_wl_flush(struct ubi_device *ubi)
1241{
593dd33c 1242 int err;
801c135c
AB
1243
1244 /*
7b6c32da 1245 * Erase while the pending works queue is not empty, but not more than
801c135c
AB
1246 * the number of currently pending works.
1247 */
593dd33c
AB
1248 dbg_wl("flush (%d pending works)", ubi->works_count);
1249 while (ubi->works_count) {
1250 err = do_work(ubi);
1251 if (err)
1252 return err;
1253 }
1254
1255 /*
1256 * Make sure all the works which have been done in parallel are
1257 * finished.
1258 */
1259 down_write(&ubi->work_sem);
1260 up_write(&ubi->work_sem);
1261
1262 /*
6fa6f5bb 1263 * And in case last was the WL worker and it canceled the LEB
593dd33c
AB
1264 * movement, flush again.
1265 */
1266 while (ubi->works_count) {
1267 dbg_wl("flush more (%d pending works)", ubi->works_count);
801c135c
AB
1268 err = do_work(ubi);
1269 if (err)
1270 return err;
1271 }
1272
1273 return 0;
1274}
1275
1276/**
1277 * tree_destroy - destroy an RB-tree.
1278 * @root: the root of the tree to destroy
1279 */
1280static void tree_destroy(struct rb_root *root)
1281{
1282 struct rb_node *rb;
1283 struct ubi_wl_entry *e;
1284
1285 rb = root->rb_node;
1286 while (rb) {
1287 if (rb->rb_left)
1288 rb = rb->rb_left;
1289 else if (rb->rb_right)
1290 rb = rb->rb_right;
1291 else {
23553b2c 1292 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
801c135c
AB
1293
1294 rb = rb_parent(rb);
1295 if (rb) {
23553b2c 1296 if (rb->rb_left == &e->u.rb)
801c135c
AB
1297 rb->rb_left = NULL;
1298 else
1299 rb->rb_right = NULL;
1300 }
1301
06b68ba1 1302 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c
AB
1303 }
1304 }
1305}
1306
1307/**
1308 * ubi_thread - UBI background thread.
1309 * @u: the UBI device description object pointer
1310 */
cdfa788a 1311int ubi_thread(void *u)
801c135c
AB
1312{
1313 int failures = 0;
1314 struct ubi_device *ubi = u;
1315
1316 ubi_msg("background thread \"%s\" started, PID %d",
ba25f9dc 1317 ubi->bgt_name, task_pid_nr(current));
801c135c 1318
83144186 1319 set_freezable();
801c135c
AB
1320 for (;;) {
1321 int err;
1322
1323 if (kthread_should_stop())
cadb40cc 1324 break;
801c135c
AB
1325
1326 if (try_to_freeze())
1327 continue;
1328
1329 spin_lock(&ubi->wl_lock);
1330 if (list_empty(&ubi->works) || ubi->ro_mode ||
1331 !ubi->thread_enabled) {
1332 set_current_state(TASK_INTERRUPTIBLE);
1333 spin_unlock(&ubi->wl_lock);
1334 schedule();
1335 continue;
1336 }
1337 spin_unlock(&ubi->wl_lock);
1338
1339 err = do_work(ubi);
1340 if (err) {
1341 ubi_err("%s: work failed with error code %d",
1342 ubi->bgt_name, err);
1343 if (failures++ > WL_MAX_FAILURES) {
1344 /*
1345 * Too many failures, disable the thread and
1346 * switch to read-only mode.
1347 */
1348 ubi_msg("%s: %d consecutive failures",
1349 ubi->bgt_name, WL_MAX_FAILURES);
1350 ubi_ro_mode(ubi);
2ad49887
VG
1351 ubi->thread_enabled = 0;
1352 continue;
801c135c
AB
1353 }
1354 } else
1355 failures = 0;
1356
1357 cond_resched();
1358 }
1359
801c135c
AB
1360 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1361 return 0;
1362}
1363
1364/**
1365 * cancel_pending - cancel all pending works.
1366 * @ubi: UBI device description object
1367 */
1368static void cancel_pending(struct ubi_device *ubi)
1369{
1370 while (!list_empty(&ubi->works)) {
1371 struct ubi_work *wrk;
1372
1373 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1374 list_del(&wrk->list);
1375 wrk->func(ubi, wrk, 1);
1376 ubi->works_count -= 1;
1377 ubi_assert(ubi->works_count >= 0);
1378 }
1379}
1380
1381/**
85c6e6e2 1382 * ubi_wl_init_scan - initialize the WL sub-system using scanning information.
801c135c
AB
1383 * @ubi: UBI device description object
1384 * @si: scanning information
1385 *
1386 * This function returns zero in case of success, and a negative error code in
1387 * case of failure.
1388 */
1389int ubi_wl_init_scan(struct ubi_device *ubi, struct ubi_scan_info *si)
1390{
7b6c32da 1391 int err, i;
801c135c
AB
1392 struct rb_node *rb1, *rb2;
1393 struct ubi_scan_volume *sv;
1394 struct ubi_scan_leb *seb, *tmp;
1395 struct ubi_wl_entry *e;
1396
b86a2c56 1397 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
801c135c 1398 spin_lock_init(&ubi->wl_lock);
43f9b25a 1399 mutex_init(&ubi->move_mutex);
593dd33c 1400 init_rwsem(&ubi->work_sem);
801c135c
AB
1401 ubi->max_ec = si->max_ec;
1402 INIT_LIST_HEAD(&ubi->works);
1403
1404 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1405
801c135c
AB
1406 err = -ENOMEM;
1407 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1408 if (!ubi->lookuptbl)
cdfa788a 1409 return err;
801c135c 1410
7b6c32da
XX
1411 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1412 INIT_LIST_HEAD(&ubi->pq[i]);
1413 ubi->pq_head = 0;
1414
801c135c
AB
1415 list_for_each_entry_safe(seb, tmp, &si->erase, u.list) {
1416 cond_resched();
1417
06b68ba1 1418 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
801c135c
AB
1419 if (!e)
1420 goto out_free;
1421
1422 e->pnum = seb->pnum;
1423 e->ec = seb->ec;
1424 ubi->lookuptbl[e->pnum] = e;
1425 if (schedule_erase(ubi, e, 0)) {
06b68ba1 1426 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c
AB
1427 goto out_free;
1428 }
1429 }
1430
1431 list_for_each_entry(seb, &si->free, u.list) {
1432 cond_resched();
1433
06b68ba1 1434 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
801c135c
AB
1435 if (!e)
1436 goto out_free;
1437
1438 e->pnum = seb->pnum;
1439 e->ec = seb->ec;
1440 ubi_assert(e->ec >= 0);
5abde384 1441 wl_tree_add(e, &ubi->free);
801c135c
AB
1442 ubi->lookuptbl[e->pnum] = e;
1443 }
1444
1445 list_for_each_entry(seb, &si->corr, u.list) {
1446 cond_resched();
1447
06b68ba1 1448 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
801c135c
AB
1449 if (!e)
1450 goto out_free;
1451
1452 e->pnum = seb->pnum;
1453 e->ec = seb->ec;
1454 ubi->lookuptbl[e->pnum] = e;
1455 if (schedule_erase(ubi, e, 0)) {
06b68ba1 1456 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c
AB
1457 goto out_free;
1458 }
1459 }
1460
1461 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1462 ubi_rb_for_each_entry(rb2, seb, &sv->root, u.rb) {
1463 cond_resched();
1464
06b68ba1 1465 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
801c135c
AB
1466 if (!e)
1467 goto out_free;
1468
1469 e->pnum = seb->pnum;
1470 e->ec = seb->ec;
1471 ubi->lookuptbl[e->pnum] = e;
1472 if (!seb->scrub) {
1473 dbg_wl("add PEB %d EC %d to the used tree",
1474 e->pnum, e->ec);
5abde384 1475 wl_tree_add(e, &ubi->used);
801c135c
AB
1476 } else {
1477 dbg_wl("add PEB %d EC %d to the scrub tree",
1478 e->pnum, e->ec);
5abde384 1479 wl_tree_add(e, &ubi->scrub);
801c135c
AB
1480 }
1481 }
1482 }
1483
5abde384 1484 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
801c135c
AB
1485 ubi_err("no enough physical eraseblocks (%d, need %d)",
1486 ubi->avail_pebs, WL_RESERVED_PEBS);
1487 goto out_free;
1488 }
1489 ubi->avail_pebs -= WL_RESERVED_PEBS;
1490 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1491
1492 /* Schedule wear-leveling if needed */
1493 err = ensure_wear_leveling(ubi);
1494 if (err)
1495 goto out_free;
1496
1497 return 0;
1498
1499out_free:
1500 cancel_pending(ubi);
1501 tree_destroy(&ubi->used);
1502 tree_destroy(&ubi->free);
1503 tree_destroy(&ubi->scrub);
1504 kfree(ubi->lookuptbl);
801c135c
AB
1505 return err;
1506}
1507
1508/**
7b6c32da 1509 * protection_queue_destroy - destroy the protection queue.
801c135c
AB
1510 * @ubi: UBI device description object
1511 */
7b6c32da 1512static void protection_queue_destroy(struct ubi_device *ubi)
801c135c 1513{
7b6c32da
XX
1514 int i;
1515 struct ubi_wl_entry *e, *tmp;
801c135c 1516
7b6c32da
XX
1517 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1518 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1519 list_del(&e->u.list);
1520 kmem_cache_free(ubi_wl_entry_slab, e);
801c135c
AB
1521 }
1522 }
1523}
1524
1525/**
85c6e6e2 1526 * ubi_wl_close - close the wear-leveling sub-system.
801c135c
AB
1527 * @ubi: UBI device description object
1528 */
1529void ubi_wl_close(struct ubi_device *ubi)
1530{
85c6e6e2 1531 dbg_wl("close the WL sub-system");
801c135c 1532 cancel_pending(ubi);
7b6c32da 1533 protection_queue_destroy(ubi);
801c135c 1534 tree_destroy(&ubi->used);
b86a2c56 1535 tree_destroy(&ubi->erroneous);
801c135c
AB
1536 tree_destroy(&ubi->free);
1537 tree_destroy(&ubi->scrub);
1538 kfree(ubi->lookuptbl);
801c135c
AB
1539}
1540
1541#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1542
1543/**
ebaaf1af 1544 * paranoid_check_ec - make sure that the erase counter of a PEB is correct.
801c135c
AB
1545 * @ubi: UBI device description object
1546 * @pnum: the physical eraseblock number to check
1547 * @ec: the erase counter to check
1548 *
1549 * This function returns zero if the erase counter of physical eraseblock @pnum
1550 * is equivalent to @ec, %1 if not, and a negative error code if an error
1551 * occurred.
1552 */
e88d6e10 1553static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
801c135c
AB
1554{
1555 int err;
1556 long long read_ec;
1557 struct ubi_ec_hdr *ec_hdr;
1558
33818bbb 1559 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
801c135c
AB
1560 if (!ec_hdr)
1561 return -ENOMEM;
1562
1563 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1564 if (err && err != UBI_IO_BITFLIPS) {
1565 /* The header does not have to exist */
1566 err = 0;
1567 goto out_free;
1568 }
1569
3261ebd7 1570 read_ec = be64_to_cpu(ec_hdr->ec);
801c135c
AB
1571 if (ec != read_ec) {
1572 ubi_err("paranoid check failed for PEB %d", pnum);
1573 ubi_err("read EC is %lld, should be %d", read_ec, ec);
1574 ubi_dbg_dump_stack();
1575 err = 1;
1576 } else
1577 err = 0;
1578
1579out_free:
1580 kfree(ec_hdr);
1581 return err;
1582}
1583
1584/**
ebaaf1af 1585 * paranoid_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
801c135c
AB
1586 * @e: the wear-leveling entry to check
1587 * @root: the root of the tree
1588 *
ebaaf1af
AB
1589 * This function returns zero if @e is in the @root RB-tree and %1 if it is
1590 * not.
801c135c
AB
1591 */
1592static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1593 struct rb_root *root)
1594{
1595 if (in_wl_tree(e, root))
1596 return 0;
1597
1598 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1599 e->pnum, e->ec, root);
1600 ubi_dbg_dump_stack();
1601 return 1;
1602}
1603
7b6c32da
XX
1604/**
1605 * paranoid_check_in_pq - check if wear-leveling entry is in the protection
1606 * queue.
1607 * @ubi: UBI device description object
1608 * @e: the wear-leveling entry to check
1609 *
1610 * This function returns zero if @e is in @ubi->pq and %1 if it is not.
1611 */
1612static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
1613{
1614 struct ubi_wl_entry *p;
1615 int i;
1616
1617 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);
1624 ubi_dbg_dump_stack();
1625 return 1;
1626}
801c135c 1627#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */