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