blob: f4d06dbdb2a9820b5c2350d2a4cbae415a1f3b18 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03002 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04003 * Copyright (c) International Business Machines Corp., 2006
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
20 */
21
22/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030023 * UBI wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040024 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030025 * This sub-system is responsible for wear-leveling. It works in terms of
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080026 * physical eraseblocks and erase counters and knows nothing about logical
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030027 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
28 * eraseblocks are of two types - used and free. Used physical eraseblocks are
29 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
30 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040031 *
32 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030033 * header. The rest of the physical eraseblock contains only %0xFF bytes.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040034 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030035 * When physical eraseblocks are returned to the WL sub-system by means of the
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040036 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
37 * done asynchronously in context of the per-UBI device background thread,
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030038 * which is also managed by the WL sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040039 *
40 * The wear-leveling is ensured by means of moving the contents of used
41 * physical eraseblocks with low erase counter to free physical eraseblocks
42 * with high erase counter.
43 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030044 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
45 * bad.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040046 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030047 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
48 * in a physical eraseblock, it has to be moved. Technically this is the same
49 * as moving it for wear-leveling reasons.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040050 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030051 * As it was said, for the UBI sub-system all physical eraseblocks are either
52 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030053 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
54 * RB-trees, as well as (temporarily) in the @wl->pq queue.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080055 *
56 * When the WL sub-system returns a physical eraseblock, the physical
57 * eraseblock is protected from being moved for some "time". For this reason,
58 * the physical eraseblock is not directly moved from the @wl->free tree to the
59 * @wl->used tree. There is a protection queue in between where this
60 * physical eraseblock is temporarily stored (@wl->pq).
61 *
62 * All this protection stuff is needed because:
63 * o we don't want to move physical eraseblocks just after we have given them
64 * to the user; instead, we first want to let users fill them up with data;
65 *
66 * o there is a chance that the user will put the physical eraseblock very
Artem Bityutskiy44156262012-05-14 19:49:35 +030067 * soon, so it makes sense not to move it for some time, but wait.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080068 *
69 * Physical eraseblocks stay protected only for limited time. But the "time" is
70 * measured in erase cycles in this case. This is implemented with help of the
71 * protection queue. Eraseblocks are put to the tail of this queue when they
72 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
73 * head of the queue on each erase operation (for any eraseblock). So the
74 * length of the queue defines how may (global) erase cycles PEBs are protected.
75 *
76 * To put it differently, each physical eraseblock has 2 main states: free and
77 * used. The former state corresponds to the @wl->free tree. The latter state
78 * is split up on several sub-states:
79 * o the WL movement is allowed (@wl->used tree);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +030080 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +030081 * erroneous - e.g., there was a read error;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +080082 * o the WL movement is temporarily prohibited (@wl->pq queue);
83 * o scrubbing is needed (@wl->scrub tree).
84 *
85 * Depending on the sub-state, wear-leveling entries of the used physical
86 * eraseblocks may be kept in one of those structures.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040087 *
88 * Note, in this implementation, we keep a small in-RAM object for each physical
89 * eraseblock. This is surely not a scalable solution. But it appears to be good
90 * enough for moderately large flashes and it is simple. In future, one may
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030091 * re-work this sub-system and make it more scalable.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040092 *
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030093 * At the moment this sub-system does not utilize the sequence number, which
94 * was introduced relatively recently. But it would be wise to do this because
95 * the sequence number of a logical eraseblock characterizes how old is it. For
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040096 * example, when we move a PEB with low erase counter, and we need to pick the
97 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
98 * pick target PEB with an average EC if our PEB is not very "old". This is a
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +030099 * room for future re-works of the WL sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400100 */
101
102#include <linux/slab.h>
103#include <linux/crc32.h>
104#include <linux/freezer.h>
105#include <linux/kthread.h>
106#include "ubi.h"
107
108/* Number of physical eraseblocks reserved for wear-leveling purposes */
109#define WL_RESERVED_PEBS 1
110
111/*
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400112 * Maximum difference between two erase counters. If this threshold is
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300113 * exceeded, the WL sub-system starts moving data from used physical
114 * eraseblocks with low erase counter to free physical eraseblocks with high
115 * erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400116 */
117#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
118
119/*
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300120 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400121 * physical eraseblock to move to. The simplest way would be just to pick the
122 * one with the highest erase counter. But in certain workloads this could lead
123 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
124 * situation when the picked physical eraseblock is constantly erased after the
125 * data is written to it. So, we have a constant which limits the highest erase
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +0300126 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200127 * does not pick eraseblocks with erase counter greater than the lowest erase
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400128 * counter plus %WL_FREE_MAX_DIFF.
129 */
130#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
131
132/*
133 * Maximum number of consecutive background thread failures which is enough to
134 * switch to read-only mode.
135 */
136#define WL_MAX_FAILURES 32
137
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300138static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
139static int self_check_in_wl_tree(const struct ubi_device *ubi,
140 struct ubi_wl_entry *e, struct rb_root *root);
141static int self_check_in_pq(const struct ubi_device *ubi,
142 struct ubi_wl_entry *e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400143
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400144/**
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400145 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
146 * @e: the wear-leveling entry to add
147 * @root: the root of the tree
148 *
149 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
150 * the @ubi->used and @ubi->free RB-trees.
151 */
152static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
153{
154 struct rb_node **p, *parent = NULL;
155
156 p = &root->rb_node;
157 while (*p) {
158 struct ubi_wl_entry *e1;
159
160 parent = *p;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800161 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400162
163 if (e->ec < e1->ec)
164 p = &(*p)->rb_left;
165 else if (e->ec > e1->ec)
166 p = &(*p)->rb_right;
167 else {
168 ubi_assert(e->pnum != e1->pnum);
169 if (e->pnum < e1->pnum)
170 p = &(*p)->rb_left;
171 else
172 p = &(*p)->rb_right;
173 }
174 }
175
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800176 rb_link_node(&e->u.rb, parent, p);
177 rb_insert_color(&e->u.rb, root);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400178}
179
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400180/**
181 * do_work - do one pending work.
182 * @ubi: UBI device description object
183 *
184 * This function returns zero in case of success and a negative error code in
185 * case of failure.
186 */
187static int do_work(struct ubi_device *ubi)
188{
189 int err;
190 struct ubi_work *wrk;
191
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200192 cond_resched();
193
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200194 /*
195 * @ubi->work_sem is used to synchronize with the workers. Workers take
196 * it in read mode, so many of them may be doing works at a time. But
197 * the queue flush code has to be sure the whole queue of works is
198 * done, and it takes the mutex in write mode.
199 */
200 down_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400201 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400202 if (list_empty(&ubi->works)) {
203 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200204 up_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400205 return 0;
206 }
207
208 wrk = list_entry(ubi->works.next, struct ubi_work, list);
209 list_del(&wrk->list);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200210 ubi->works_count -= 1;
211 ubi_assert(ubi->works_count >= 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400212 spin_unlock(&ubi->wl_lock);
213
214 /*
215 * Call the worker function. Do not touch the work structure
216 * after this call as it will have been freed or reused by that
217 * time by the worker function.
218 */
219 err = wrk->func(ubi, wrk, 0);
220 if (err)
221 ubi_err("work failed with error code %d", err);
Artem Bityutskiy593dd332007-12-18 15:54:35 +0200222 up_read(&ubi->work_sem);
Artem Bityutskiy16f557e2007-12-19 16:03:17 +0200223
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400224 return err;
225}
226
227/**
228 * produce_free_peb - produce a free physical eraseblock.
229 * @ubi: UBI device description object
230 *
231 * This function tries to make a free PEB by means of synchronous execution of
232 * pending works. This may be needed if, for example the background thread is
233 * disabled. Returns zero in case of success and a negative error code in case
234 * of failure.
235 */
236static int produce_free_peb(struct ubi_device *ubi)
237{
238 int err;
239
240 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300241 while (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400242 spin_unlock(&ubi->wl_lock);
243
244 dbg_wl("do one work synchronously");
245 err = do_work(ubi);
246 if (err)
247 return err;
248
249 spin_lock(&ubi->wl_lock);
250 }
251 spin_unlock(&ubi->wl_lock);
252
253 return 0;
254}
255
256/**
257 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
258 * @e: the wear-leveling entry to check
259 * @root: the root of the tree
260 *
261 * This function returns non-zero if @e is in the @root RB-tree and zero if it
262 * is not.
263 */
264static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
265{
266 struct rb_node *p;
267
268 p = root->rb_node;
269 while (p) {
270 struct ubi_wl_entry *e1;
271
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800272 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400273
274 if (e->pnum == e1->pnum) {
275 ubi_assert(e == e1);
276 return 1;
277 }
278
279 if (e->ec < e1->ec)
280 p = p->rb_left;
281 else if (e->ec > e1->ec)
282 p = p->rb_right;
283 else {
284 ubi_assert(e->pnum != e1->pnum);
285 if (e->pnum < e1->pnum)
286 p = p->rb_left;
287 else
288 p = p->rb_right;
289 }
290 }
291
292 return 0;
293}
294
295/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800296 * prot_queue_add - add physical eraseblock to the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400297 * @ubi: UBI device description object
298 * @e: the physical eraseblock to add
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400299 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800300 * This function adds @e to the tail of the protection queue @ubi->pq, where
301 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
302 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
303 * be locked.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400304 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800305static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400306{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800307 int pq_tail = ubi->pq_head - 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400308
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800309 if (pq_tail < 0)
310 pq_tail = UBI_PROT_QUEUE_LEN - 1;
311 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
312 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
313 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400314}
315
316/**
317 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
318 * @root: the RB-tree where to look for
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200319 * @diff: maximum possible difference from the smallest erase counter
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400320 *
321 * This function looks for a wear leveling entry with erase counter closest to
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200322 * min + @diff, where min is the smallest erase counter.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400323 */
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200324static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400325{
326 struct rb_node *p;
327 struct ubi_wl_entry *e;
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200328 int max;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800330 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
Artem Bityutskiyadd82872012-03-07 18:56:29 +0200331 max = e->ec + diff;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400332
333 p = root->rb_node;
334 while (p) {
335 struct ubi_wl_entry *e1;
336
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800337 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400338 if (e1->ec >= max)
339 p = p->rb_left;
340 else {
341 p = p->rb_right;
342 e = e1;
343 }
344 }
345
346 return e;
347}
348
349/**
350 * ubi_wl_get_peb - get a physical eraseblock.
351 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400352 *
353 * This function returns a physical eraseblock in case of success and a
354 * negative error code in case of failure. Might sleep.
355 */
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200356int ubi_wl_get_peb(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400357{
Artem Bityutskiy7eb3aa652012-03-07 19:08:36 +0200358 int err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400359 struct ubi_wl_entry *e, *first, *last;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400360
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400361retry:
362 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300363 if (!ubi->free.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400364 if (ubi->works_count == 0) {
365 ubi_assert(list_empty(&ubi->works));
366 ubi_err("no free eraseblocks");
367 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400368 return -ENOSPC;
369 }
370 spin_unlock(&ubi->wl_lock);
371
372 err = produce_free_peb(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800373 if (err < 0)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400374 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400375 goto retry;
376 }
377
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200378 first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb);
379 last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400380
Richard Weinbergerb36a2612012-05-14 17:55:51 +0200381 if (last->ec - first->ec < WL_FREE_MAX_DIFF)
382 e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb);
383 else
384 e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400385
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300386 self_check_in_wl_tree(ubi, e, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800387
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400388 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800389 * Move the physical eraseblock to the protection queue where it will
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400390 * be protected from being moved for some time.
391 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800392 rb_erase(&e->u.rb, &ubi->free);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800393 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
394 prot_queue_add(ubi, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400395 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300396
Artem Bityutskiy97d61042012-05-16 19:29:04 +0300397 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
398 ubi->peb_size - ubi->vid_hdr_aloffset);
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300399 if (err) {
Artem Bityutskiy13987882009-06-29 15:58:36 +0300400 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200401 return err;
Artem Bityutskiy40a71a82009-06-28 19:16:55 +0300402 }
403
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400404 return e->pnum;
405}
406
407/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800408 * prot_queue_del - remove a physical eraseblock from the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400409 * @ubi: UBI device description object
410 * @pnum: the physical eraseblock to remove
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200411 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800412 * This function deletes PEB @pnum from the protection queue and returns zero
413 * in case of success and %-ENODEV if the PEB was not found.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400414 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800415static int prot_queue_del(struct ubi_device *ubi, int pnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400416{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800417 struct ubi_wl_entry *e;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400418
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800419 e = ubi->lookuptbl[pnum];
420 if (!e)
421 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400422
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300423 if (self_check_in_pq(ubi, e))
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800424 return -ENODEV;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400425
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800426 list_del(&e->u.list);
427 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200428 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400429}
430
431/**
432 * sync_erase - synchronously erase a physical eraseblock.
433 * @ubi: UBI device description object
434 * @e: the the physical eraseblock to erase
435 * @torture: if the physical eraseblock has to be tortured
436 *
437 * This function returns zero in case of success and a negative error code in
438 * case of failure.
439 */
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300440static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
441 int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400442{
443 int err;
444 struct ubi_ec_hdr *ec_hdr;
445 unsigned long long ec = e->ec;
446
447 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
448
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300449 err = self_check_ec(ubi, e->pnum, e->ec);
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +0200450 if (err)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400451 return -EINVAL;
452
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300453 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400454 if (!ec_hdr)
455 return -ENOMEM;
456
457 err = ubi_io_sync_erase(ubi, e->pnum, torture);
458 if (err < 0)
459 goto out_free;
460
461 ec += err;
462 if (ec > UBI_MAX_ERASECOUNTER) {
463 /*
464 * Erase counter overflow. Upgrade UBI and use 64-bit
465 * erase counters internally.
466 */
467 ubi_err("erase counter overflow at PEB %d, EC %llu",
468 e->pnum, ec);
469 err = -EINVAL;
470 goto out_free;
471 }
472
473 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
474
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300475 ec_hdr->ec = cpu_to_be64(ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400476
477 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
478 if (err)
479 goto out_free;
480
481 e->ec = ec;
482 spin_lock(&ubi->wl_lock);
483 if (e->ec > ubi->max_ec)
484 ubi->max_ec = e->ec;
485 spin_unlock(&ubi->wl_lock);
486
487out_free:
488 kfree(ec_hdr);
489 return err;
490}
491
492/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800493 * serve_prot_queue - check if it is time to stop protecting PEBs.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400494 * @ubi: UBI device description object
495 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800496 * This function is called after each erase operation and removes PEBs from the
497 * tail of the protection queue. These PEBs have been protected for long enough
498 * and should be moved to the used tree.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400499 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800500static void serve_prot_queue(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400501{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800502 struct ubi_wl_entry *e, *tmp;
503 int count;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400504
505 /*
506 * There may be several protected physical eraseblock to remove,
507 * process them all.
508 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800509repeat:
510 count = 0;
511 spin_lock(&ubi->wl_lock);
512 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
513 dbg_wl("PEB %d EC %d protection over, move to used tree",
514 e->pnum, e->ec);
515
516 list_del(&e->u.list);
517 wl_tree_add(e, &ubi->used);
518 if (count++ > 32) {
519 /*
520 * Let's be nice and avoid holding the spinlock for
521 * too long.
522 */
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400523 spin_unlock(&ubi->wl_lock);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800524 cond_resched();
525 goto repeat;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400526 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400527 }
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800528
529 ubi->pq_head += 1;
530 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
531 ubi->pq_head = 0;
532 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
533 spin_unlock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400534}
535
536/**
537 * schedule_ubi_work - schedule a work.
538 * @ubi: UBI device description object
539 * @wrk: the work to schedule
540 *
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800541 * This function adds a work defined by @wrk to the tail of the pending works
542 * list.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400543 */
544static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
545{
546 spin_lock(&ubi->wl_lock);
547 list_add_tail(&wrk->list, &ubi->works);
548 ubi_assert(ubi->works_count >= 0);
549 ubi->works_count += 1;
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +0300550 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400551 wake_up_process(ubi->bgt_thread);
552 spin_unlock(&ubi->wl_lock);
553}
554
555static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
556 int cancel);
557
558/**
559 * schedule_erase - schedule an erase work.
560 * @ubi: UBI device description object
561 * @e: the WL entry of the physical eraseblock to erase
Joel Reardond36e59e2012-05-18 15:40:24 +0200562 * @vol_id: the volume ID that last used this PEB
563 * @lnum: the last used logical eraseblock number for the PEB
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400564 * @torture: if the physical eraseblock has to be tortured
565 *
566 * This function returns zero in case of success and a %-ENOMEM in case of
567 * failure.
568 */
569static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
Joel Reardond36e59e2012-05-18 15:40:24 +0200570 int vol_id, int lnum, int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400571{
572 struct ubi_work *wl_wrk;
573
574 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
575 e->pnum, e->ec, torture);
576
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300577 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400578 if (!wl_wrk)
579 return -ENOMEM;
580
581 wl_wrk->func = &erase_worker;
582 wl_wrk->e = e;
Joel Reardond36e59e2012-05-18 15:40:24 +0200583 wl_wrk->vol_id = vol_id;
584 wl_wrk->lnum = lnum;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400585 wl_wrk->torture = torture;
586
587 schedule_ubi_work(ubi, wl_wrk);
588 return 0;
589}
590
591/**
592 * wear_leveling_worker - wear-leveling worker function.
593 * @ubi: UBI device description object
594 * @wrk: the work object
595 * @cancel: non-zero if the worker has to free memory and exit
596 *
597 * This function copies a more worn out physical eraseblock to a less worn out
598 * one. Returns zero in case of success and a negative error code in case of
599 * failure.
600 */
601static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
602 int cancel)
603{
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300604 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300605 int vol_id = -1, uninitialized_var(lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400606 struct ubi_wl_entry *e1, *e2;
607 struct ubi_vid_hdr *vid_hdr;
608
609 kfree(wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610 if (cancel)
611 return 0;
612
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300613 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400614 if (!vid_hdr)
615 return -ENOMEM;
616
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200617 mutex_lock(&ubi->move_mutex);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400618 spin_lock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200619 ubi_assert(!ubi->move_from && !ubi->move_to);
620 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400621
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200622 if (!ubi->free.rb_node ||
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300623 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400624 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200625 * No free physical eraseblocks? Well, they must be waiting in
626 * the queue to be erased. Cancel movement - it will be
627 * triggered again when a free physical eraseblock appears.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400628 *
629 * No used physical eraseblocks? They must be temporarily
630 * protected from being moved. They will be moved to the
631 * @ubi->used tree later and the wear-leveling will be
632 * triggered again.
633 */
634 dbg_wl("cancel WL, a list is empty: free %d, used %d",
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300635 !ubi->free.rb_node, !ubi->used.rb_node);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200636 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400637 }
638
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300639 if (!ubi->scrub.rb_node) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400640 /*
641 * Now pick the least worn-out used physical eraseblock and a
642 * highly worn-out free physical eraseblock. If the erase
643 * counters differ much enough, start wear-leveling.
644 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800645 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400646 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
647
648 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
649 dbg_wl("no WL needed: min used EC %d, max free EC %d",
650 e1->ec, e2->ec);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200651 goto out_cancel;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400652 }
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300653 self_check_in_wl_tree(ubi, e1, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800654 rb_erase(&e1->u.rb, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400655 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
656 e1->pnum, e1->ec, e2->pnum, e2->ec);
657 } else {
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200658 /* Perform scrubbing */
659 scrubbing = 1;
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800660 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400661 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300662 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800663 rb_erase(&e1->u.rb, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400664 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
665 }
666
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +0300667 self_check_in_wl_tree(ubi, e2, &ubi->free);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800668 rb_erase(&e2->u.rb, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400669 ubi->move_from = e1;
670 ubi->move_to = e2;
671 spin_unlock(&ubi->wl_lock);
672
673 /*
674 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
675 * We so far do not know which logical eraseblock our physical
676 * eraseblock (@e1) belongs to. We have to read the volume identifier
677 * header first.
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200678 *
679 * Note, we are protected from this PEB being unmapped and erased. The
680 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
681 * which is being moved was unmapped.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400682 */
683
684 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
685 if (err && err != UBI_IO_BITFLIPS) {
Artem Bityutskiy74d82d22010-09-03 02:11:20 +0300686 if (err == UBI_IO_FF) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400687 /*
688 * We are trying to move PEB without a VID header. UBI
689 * always write VID headers shortly after the PEB was
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300690 * given, so we have a situation when it has not yet
691 * had a chance to write it, because it was preempted.
692 * So add this PEB to the protection queue so far,
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300693 * because presumably more data will be written there
694 * (including the missing VID header), and then we'll
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300695 * move it.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400696 */
697 dbg_wl("PEB %d has no VID header", e1->pnum);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300698 protect = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200699 goto out_not_moved;
Artem Bityutskiy92e1a7d2010-09-03 14:22:17 +0300700 } else if (err == UBI_IO_FF_BITFLIPS) {
701 /*
702 * The same situation as %UBI_IO_FF, but bit-flips were
703 * detected. It is better to schedule this PEB for
704 * scrubbing.
705 */
706 dbg_wl("PEB %d has no VID header but has bit-flips",
707 e1->pnum);
708 scrubbing = 1;
709 goto out_not_moved;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400710 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200711
712 ubi_err("error %d while reading VID header from PEB %d",
713 err, e1->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200714 goto out_error;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400715 }
716
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300717 vol_id = be32_to_cpu(vid_hdr->vol_id);
718 lnum = be32_to_cpu(vid_hdr->lnum);
719
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400720 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
721 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300722 if (err == MOVE_CANCEL_RACE) {
723 /*
724 * The LEB has not been moved because the volume is
725 * being deleted or the PEB has been put meanwhile. We
726 * should prevent this PEB from being selected for
727 * wear-leveling movement again, so put it to the
728 * protection queue.
729 */
730 protect = 1;
731 goto out_not_moved;
732 }
Bhavesh Parekhe801e122011-11-30 17:43:42 +0530733 if (err == MOVE_RETRY) {
734 scrubbing = 1;
735 goto out_not_moved;
736 }
Artem Bityutskiycc831462012-03-09 10:31:18 +0200737 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300738 err == MOVE_TARGET_RD_ERR) {
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300739 /*
740 * Target PEB had bit-flips or write error - torture it.
741 */
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200742 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200743 goto out_not_moved;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200744 }
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300745
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300746 if (err == MOVE_SOURCE_RD_ERR) {
747 /*
748 * An error happened while reading the source PEB. Do
749 * not switch to R/O mode in this case, and give the
750 * upper layers a possibility to recover from this,
751 * e.g. by unmapping corresponding LEB. Instead, just
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +0300752 * put this PEB to the @ubi->erroneous list to prevent
753 * UBI from trying to move it over and over again.
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300754 */
755 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
756 ubi_err("too many erroneous eraseblocks (%d)",
757 ubi->erroneous_peb_count);
758 goto out_error;
759 }
760 erroneous = 1;
761 goto out_not_moved;
762 }
763
Artem Bityutskiy90bf0262009-05-23 16:04:17 +0300764 if (err < 0)
765 goto out_error;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200766
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300767 ubi_assert(0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400768 }
769
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200770 /* The PEB has been successfully moved */
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200771 if (scrubbing)
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300772 ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
773 e1->pnum, vol_id, lnum, e2->pnum);
774 ubi_free_vid_hdr(ubi, vid_hdr);
Artem Bityutskiy8c1e6ee2008-07-18 12:20:23 +0300775
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400776 spin_lock(&ubi->wl_lock);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200777 if (!ubi->move_to_put) {
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300778 wl_tree_add(e2, &ubi->used);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200779 e2 = NULL;
780 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400781 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200782 ubi->move_to_put = ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400783 spin_unlock(&ubi->wl_lock);
784
Joel Reardond36e59e2012-05-18 15:40:24 +0200785 err = schedule_erase(ubi, e1, vol_id, lnum, 0);
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200786 if (err) {
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300787 kmem_cache_free(ubi_wl_entry_slab, e1);
Artem Bityutskiy21d08bb2009-06-08 19:28:18 +0300788 if (e2)
789 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300790 goto out_ro;
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200791 }
Artem Bityutskiy6a8f4832008-12-05 12:23:48 +0200792
Artem Bityutskiy3c98b0a2008-12-05 12:42:45 +0200793 if (e2) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400794 /*
795 * Well, the target PEB was put meanwhile, schedule it for
796 * erasure.
797 */
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300798 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
799 e2->pnum, vol_id, lnum);
Joel Reardond36e59e2012-05-18 15:40:24 +0200800 err = schedule_erase(ubi, e2, vol_id, lnum, 0);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300801 if (err) {
802 kmem_cache_free(ubi_wl_entry_slab, e2);
803 goto out_ro;
804 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400805 }
806
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400807 dbg_wl("done");
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200808 mutex_unlock(&ubi->move_mutex);
809 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400810
811 /*
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200812 * For some reasons the LEB was not moved, might be an error, might be
813 * something else. @e1 was not changed, so return it back. @e2 might
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200814 * have been changed, schedule it for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400815 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200816out_not_moved:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300817 if (vol_id != -1)
818 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
819 e1->pnum, vol_id, lnum, e2->pnum, err);
820 else
821 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
822 e1->pnum, e2->pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400823 spin_lock(&ubi->wl_lock);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300824 if (protect)
825 prot_queue_add(ubi, e1);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +0300826 else if (erroneous) {
827 wl_tree_add(e1, &ubi->erroneous);
828 ubi->erroneous_peb_count += 1;
829 } else if (scrubbing)
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200830 wl_tree_add(e1, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400831 else
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300832 wl_tree_add(e1, &ubi->used);
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200833 ubi_assert(!ubi->move_to_put);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400834 ubi->move_from = ubi->move_to = NULL;
Artem Bityutskiy6fa6f5b2008-12-05 13:37:02 +0200835 ubi->wl_scheduled = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400836 spin_unlock(&ubi->wl_lock);
837
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300838 ubi_free_vid_hdr(ubi, vid_hdr);
Joel Reardond36e59e2012-05-18 15:40:24 +0200839 err = schedule_erase(ubi, e2, vol_id, lnum, torture);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300840 if (err) {
841 kmem_cache_free(ubi_wl_entry_slab, e2);
842 goto out_ro;
843 }
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200844 mutex_unlock(&ubi->move_mutex);
845 return 0;
846
847out_error:
Artem Bityutskiy9c259a52009-06-08 12:49:08 +0300848 if (vol_id != -1)
849 ubi_err("error %d while moving PEB %d to PEB %d",
850 err, e1->pnum, e2->pnum);
851 else
852 ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d",
853 err, e1->pnum, vol_id, lnum, e2->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200854 spin_lock(&ubi->wl_lock);
855 ubi->move_from = ubi->move_to = NULL;
856 ubi->move_to_put = ubi->wl_scheduled = 0;
857 spin_unlock(&ubi->wl_lock);
858
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300859 ubi_free_vid_hdr(ubi, vid_hdr);
860 kmem_cache_free(ubi_wl_entry_slab, e1);
861 kmem_cache_free(ubi_wl_entry_slab, e2);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200862
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300863out_ro:
864 ubi_ro_mode(ubi);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200865 mutex_unlock(&ubi->move_mutex);
Artem Bityutskiy87960c02009-05-24 11:58:58 +0300866 ubi_assert(err != 0);
867 return err < 0 ? err : -EIO;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +0200868
869out_cancel:
870 ubi->wl_scheduled = 0;
871 spin_unlock(&ubi->wl_lock);
872 mutex_unlock(&ubi->move_mutex);
873 ubi_free_vid_hdr(ubi, vid_hdr);
874 return 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400875}
876
877/**
878 * ensure_wear_leveling - schedule wear-leveling if it is needed.
879 * @ubi: UBI device description object
880 *
881 * This function checks if it is time to start wear-leveling and schedules it
882 * if yes. This function returns zero in case of success and a negative error
883 * code in case of failure.
884 */
885static int ensure_wear_leveling(struct ubi_device *ubi)
886{
887 int err = 0;
888 struct ubi_wl_entry *e1;
889 struct ubi_wl_entry *e2;
890 struct ubi_work *wrk;
891
892 spin_lock(&ubi->wl_lock);
893 if (ubi->wl_scheduled)
894 /* Wear-leveling is already in the work queue */
895 goto out_unlock;
896
897 /*
898 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
899 * the WL worker has to be scheduled anyway.
900 */
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300901 if (!ubi->scrub.rb_node) {
902 if (!ubi->used.rb_node || !ubi->free.rb_node)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400903 /* No physical eraseblocks - no deal */
904 goto out_unlock;
905
906 /*
907 * We schedule wear-leveling only if the difference between the
908 * lowest erase counter of used physical eraseblocks and a high
Frederik Schwarzer025dfda2008-10-16 19:02:37 +0200909 * erase counter of free physical eraseblocks is greater than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400910 * %UBI_WL_THRESHOLD.
911 */
Xiaochuan-Xu23553b22008-12-09 19:44:12 +0800912 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400913 e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF);
914
915 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
916 goto out_unlock;
917 dbg_wl("schedule wear-leveling");
918 } else
919 dbg_wl("schedule scrubbing");
920
921 ubi->wl_scheduled = 1;
922 spin_unlock(&ubi->wl_lock);
923
Artem Bityutskiy33818bb2007-08-28 21:29:32 +0300924 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400925 if (!wrk) {
926 err = -ENOMEM;
927 goto out_cancel;
928 }
929
930 wrk->func = &wear_leveling_worker;
931 schedule_ubi_work(ubi, wrk);
932 return err;
933
934out_cancel:
935 spin_lock(&ubi->wl_lock);
936 ubi->wl_scheduled = 0;
937out_unlock:
938 spin_unlock(&ubi->wl_lock);
939 return err;
940}
941
942/**
943 * erase_worker - physical eraseblock erase worker function.
944 * @ubi: UBI device description object
945 * @wl_wrk: the work object
946 * @cancel: non-zero if the worker has to free memory and exit
947 *
948 * This function erases a physical eraseblock and perform torture testing if
949 * needed. It also takes care about marking the physical eraseblock bad if
950 * needed. Returns zero in case of success and a negative error code in case of
951 * failure.
952 */
953static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
954 int cancel)
955{
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400956 struct ubi_wl_entry *e = wl_wrk->e;
Shmulik Ladkani37f758a2012-07-04 11:06:01 +0300957 int pnum = e->pnum;
Joel Reardond36e59e2012-05-18 15:40:24 +0200958 int vol_id = wl_wrk->vol_id;
959 int lnum = wl_wrk->lnum;
Shmulik Ladkani37f758a2012-07-04 11:06:01 +0300960 int err, available_consumed = 0;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400961
962 if (cancel) {
963 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
964 kfree(wl_wrk);
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +0200965 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400966 return 0;
967 }
968
Joel Reardond36e59e2012-05-18 15:40:24 +0200969 dbg_wl("erase PEB %d EC %d LEB %d:%d",
970 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400971
972 err = sync_erase(ubi, e, wl_wrk->torture);
973 if (!err) {
974 /* Fine, we've erased it successfully */
975 kfree(wl_wrk);
976
977 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +0300978 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400979 spin_unlock(&ubi->wl_lock);
980
981 /*
Artem Bityutskiy9c9ec142008-07-18 13:19:52 +0300982 * One more erase operation has happened, take care about
983 * protected physical eraseblocks.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400984 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +0800985 serve_prot_queue(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400986
987 /* And take care about wear-leveling */
988 err = ensure_wear_leveling(ubi);
989 return err;
990 }
991
Artem Bityutskiy8d2d4012007-07-22 22:32:51 +0300992 ubi_err("failed to erase PEB %d, error %d", pnum, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400993 kfree(wl_wrk);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400994
Artem Bityutskiy784c1452007-07-18 13:42:10 +0300995 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
996 err == -EBUSY) {
997 int err1;
998
999 /* Re-schedule the LEB for erasure */
Joel Reardond36e59e2012-05-18 15:40:24 +02001000 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001001 if (err1) {
1002 err = err1;
1003 goto out_ro;
1004 }
1005 return err;
Artem Bityutskiye57e0d82012-01-05 10:47:18 +02001006 }
1007
1008 kmem_cache_free(ubi_wl_entry_slab, e);
1009 if (err != -EIO)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001010 /*
1011 * If this is not %-EIO, we have no idea what to do. Scheduling
1012 * this physical eraseblock for erasure again would cause
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001013 * errors again and again. Well, lets switch to R/O mode.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001014 */
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001015 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001016
1017 /* It is %-EIO, the PEB went bad */
1018
1019 if (!ubi->bad_allowed) {
1020 ubi_err("bad physical eraseblock %d detected", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001021 goto out_ro;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001022 }
1023
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001024 spin_lock(&ubi->volumes_lock);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001025 if (ubi->beb_rsvd_pebs == 0) {
Shmulik Ladkani37f758a2012-07-04 11:06:01 +03001026 if (ubi->avail_pebs == 0) {
1027 spin_unlock(&ubi->volumes_lock);
1028 ubi_err("no reserved/available physical eraseblocks");
1029 goto out_ro;
1030 }
1031 ubi->avail_pebs -= 1;
1032 available_consumed = 1;
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001033 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001034 spin_unlock(&ubi->volumes_lock);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001035
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001036 ubi_msg("mark PEB %d as bad", pnum);
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001037 err = ubi_io_mark_bad(ubi, pnum);
1038 if (err)
1039 goto out_ro;
1040
1041 spin_lock(&ubi->volumes_lock);
Shmulik Ladkani37f758a2012-07-04 11:06:01 +03001042 if (ubi->beb_rsvd_pebs > 0) {
1043 if (available_consumed) {
1044 /*
1045 * The amount of reserved PEBs increased since we last
1046 * checked.
1047 */
1048 ubi->avail_pebs += 1;
1049 available_consumed = 0;
1050 }
1051 ubi->beb_rsvd_pebs -= 1;
1052 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001053 ubi->bad_peb_count += 1;
1054 ubi->good_peb_count -= 1;
1055 ubi_calculate_reserved(ubi);
Shmulik Ladkani37f758a2012-07-04 11:06:01 +03001056 if (available_consumed)
1057 ubi_warn("no PEBs in the reserved pool, used an available PEB");
1058 else if (ubi->beb_rsvd_pebs)
Artem Bityutskiy52b605d2009-06-08 16:52:48 +03001059 ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs);
1060 else
Shmulik Ladkani37f758a2012-07-04 11:06:01 +03001061 ubi_warn("last PEB from the reserve was used");
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001062 spin_unlock(&ubi->volumes_lock);
1063
1064 return err;
1065
1066out_ro:
Shmulik Ladkani37f758a2012-07-04 11:06:01 +03001067 if (available_consumed) {
1068 spin_lock(&ubi->volumes_lock);
1069 ubi->avail_pebs += 1;
1070 spin_unlock(&ubi->volumes_lock);
1071 }
Artem Bityutskiy784c1452007-07-18 13:42:10 +03001072 ubi_ro_mode(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001073 return err;
1074}
1075
1076/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001077 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001078 * @ubi: UBI device description object
Joel Reardond36e59e2012-05-18 15:40:24 +02001079 * @vol_id: the volume ID that last used this PEB
1080 * @lnum: the last used logical eraseblock number for the PEB
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001081 * @pnum: physical eraseblock to return
1082 * @torture: if this physical eraseblock has to be tortured
1083 *
1084 * This function is called to return physical eraseblock @pnum to the pool of
1085 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1086 * occurred to this @pnum and it has to be tested. This function returns zero
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001087 * in case of success, and a negative error code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001088 */
Joel Reardond36e59e2012-05-18 15:40:24 +02001089int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1090 int pnum, int torture)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001091{
1092 int err;
1093 struct ubi_wl_entry *e;
1094
1095 dbg_wl("PEB %d", pnum);
1096 ubi_assert(pnum >= 0);
1097 ubi_assert(pnum < ubi->peb_count);
1098
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001099retry:
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001100 spin_lock(&ubi->wl_lock);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001101 e = ubi->lookuptbl[pnum];
1102 if (e == ubi->move_from) {
1103 /*
1104 * User is putting the physical eraseblock which was selected to
1105 * be moved. It will be scheduled for erasure in the
1106 * wear-leveling worker.
1107 */
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001108 dbg_wl("PEB %d is being moved, wait", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001109 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001110
1111 /* Wait for the WL worker by taking the @ubi->move_mutex */
1112 mutex_lock(&ubi->move_mutex);
1113 mutex_unlock(&ubi->move_mutex);
1114 goto retry;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001115 } else if (e == ubi->move_to) {
1116 /*
1117 * User is putting the physical eraseblock which was selected
1118 * as the target the data is moved to. It may happen if the EBA
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001119 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1120 * but the WL sub-system has not put the PEB to the "used" tree
1121 * yet, but it is about to do this. So we just set a flag which
1122 * will tell the WL worker that the PEB is not needed anymore
1123 * and should be scheduled for erasure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001124 */
1125 dbg_wl("PEB %d is the target of data moving", pnum);
1126 ubi_assert(!ubi->move_to_put);
1127 ubi->move_to_put = 1;
1128 spin_unlock(&ubi->wl_lock);
1129 return 0;
1130 } else {
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001131 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001132 self_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001133 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001134 } else if (in_wl_tree(e, &ubi->scrub)) {
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001135 self_check_in_wl_tree(ubi, e, &ubi->scrub);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001136 rb_erase(&e->u.rb, &ubi->scrub);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001137 } else if (in_wl_tree(e, &ubi->erroneous)) {
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001138 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001139 rb_erase(&e->u.rb, &ubi->erroneous);
1140 ubi->erroneous_peb_count -= 1;
1141 ubi_assert(ubi->erroneous_peb_count >= 0);
Artem Bityutskiy815bc5f8f2009-06-08 19:28:18 +03001142 /* Erroneous PEBs should be tortured */
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001143 torture = 1;
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001144 } else {
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001145 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001146 if (err) {
1147 ubi_err("PEB %d not found", pnum);
1148 ubi_ro_mode(ubi);
1149 spin_unlock(&ubi->wl_lock);
1150 return err;
1151 }
1152 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001153 }
1154 spin_unlock(&ubi->wl_lock);
1155
Joel Reardond36e59e2012-05-18 15:40:24 +02001156 err = schedule_erase(ubi, e, vol_id, lnum, torture);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001157 if (err) {
1158 spin_lock(&ubi->wl_lock);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001159 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001160 spin_unlock(&ubi->wl_lock);
1161 }
1162
1163 return err;
1164}
1165
1166/**
1167 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1168 * @ubi: UBI device description object
1169 * @pnum: the physical eraseblock to schedule
1170 *
1171 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1172 * needs scrubbing. This function schedules a physical eraseblock for
1173 * scrubbing which is done in background. This function returns zero in case of
1174 * success and a negative error code in case of failure.
1175 */
1176int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1177{
1178 struct ubi_wl_entry *e;
1179
Artem Bityutskiy719bb842012-08-27 17:14:58 +03001180 ubi_msg("schedule PEB %d for scrubbing", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001181
1182retry:
1183 spin_lock(&ubi->wl_lock);
1184 e = ubi->lookuptbl[pnum];
Artem Bityutskiyd3f6e6c2010-08-29 23:34:44 +03001185 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1186 in_wl_tree(e, &ubi->erroneous)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001187 spin_unlock(&ubi->wl_lock);
1188 return 0;
1189 }
1190
1191 if (e == ubi->move_to) {
1192 /*
1193 * This physical eraseblock was used to move data to. The data
1194 * was moved but the PEB was not yet inserted to the proper
1195 * tree. We should just wait a little and let the WL worker
1196 * proceed.
1197 */
1198 spin_unlock(&ubi->wl_lock);
1199 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1200 yield();
1201 goto retry;
1202 }
1203
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001204 if (in_wl_tree(e, &ubi->used)) {
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001205 self_check_in_wl_tree(ubi, e, &ubi->used);
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001206 rb_erase(&e->u.rb, &ubi->used);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001207 } else {
1208 int err;
1209
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001210 err = prot_queue_del(ubi, e->pnum);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001211 if (err) {
1212 ubi_err("PEB %d not found", pnum);
1213 ubi_ro_mode(ubi);
1214 spin_unlock(&ubi->wl_lock);
1215 return err;
1216 }
1217 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001218
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001219 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001220 spin_unlock(&ubi->wl_lock);
1221
1222 /*
1223 * Technically scrubbing is the same as wear-leveling, so it is done
1224 * by the WL worker.
1225 */
1226 return ensure_wear_leveling(ubi);
1227}
1228
1229/**
1230 * ubi_wl_flush - flush all pending works.
1231 * @ubi: UBI device description object
Joel Reardon62f384552012-05-20 21:27:11 +02001232 * @vol_id: the volume id to flush for
1233 * @lnum: the logical eraseblock number to flush for
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001234 *
Joel Reardon62f384552012-05-20 21:27:11 +02001235 * This function executes all pending works for a particular volume id /
1236 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1237 * acts as a wildcard for all of the corresponding volume numbers or logical
1238 * eraseblock numbers. It returns zero in case of success and a negative error
1239 * code in case of failure.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001240 */
Joel Reardon62f384552012-05-20 21:27:11 +02001241int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001242{
Joel Reardon62f384552012-05-20 21:27:11 +02001243 int err = 0;
1244 int found = 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001245
1246 /*
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001247 * Erase while the pending works queue is not empty, but not more than
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001248 * the number of currently pending works.
1249 */
Joel Reardon62f384552012-05-20 21:27:11 +02001250 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1251 vol_id, lnum, ubi->works_count);
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001252
Joel Reardon62f384552012-05-20 21:27:11 +02001253 while (found) {
1254 struct ubi_work *wrk;
1255 found = 0;
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001256
Artem Bityutskiy12027f12012-06-07 15:15:30 +03001257 down_read(&ubi->work_sem);
Joel Reardon62f384552012-05-20 21:27:11 +02001258 spin_lock(&ubi->wl_lock);
1259 list_for_each_entry(wrk, &ubi->works, list) {
1260 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1261 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1262 list_del(&wrk->list);
1263 ubi->works_count -= 1;
1264 ubi_assert(ubi->works_count >= 0);
1265 spin_unlock(&ubi->wl_lock);
1266
1267 err = wrk->func(ubi, wrk, 0);
Artem Bityutskiy12027f12012-06-07 15:15:30 +03001268 if (err) {
1269 up_read(&ubi->work_sem);
1270 return err;
1271 }
1272
Joel Reardon62f384552012-05-20 21:27:11 +02001273 spin_lock(&ubi->wl_lock);
1274 found = 1;
1275 break;
1276 }
1277 }
1278 spin_unlock(&ubi->wl_lock);
Artem Bityutskiy12027f12012-06-07 15:15:30 +03001279 up_read(&ubi->work_sem);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001280 }
1281
Artem Bityutskiy12027f12012-06-07 15:15:30 +03001282 /*
1283 * Make sure all the works which have been done in parallel are
1284 * finished.
1285 */
1286 down_write(&ubi->work_sem);
Joel Reardon62f384552012-05-20 21:27:11 +02001287 up_write(&ubi->work_sem);
Artem Bityutskiy12027f12012-06-07 15:15:30 +03001288
Joel Reardon62f384552012-05-20 21:27:11 +02001289 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001290}
1291
1292/**
1293 * tree_destroy - destroy an RB-tree.
1294 * @root: the root of the tree to destroy
1295 */
1296static void tree_destroy(struct rb_root *root)
1297{
1298 struct rb_node *rb;
1299 struct ubi_wl_entry *e;
1300
1301 rb = root->rb_node;
1302 while (rb) {
1303 if (rb->rb_left)
1304 rb = rb->rb_left;
1305 else if (rb->rb_right)
1306 rb = rb->rb_right;
1307 else {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001308 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001309
1310 rb = rb_parent(rb);
1311 if (rb) {
Xiaochuan-Xu23553b22008-12-09 19:44:12 +08001312 if (rb->rb_left == &e->u.rb)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001313 rb->rb_left = NULL;
1314 else
1315 rb->rb_right = NULL;
1316 }
1317
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001318 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001319 }
1320 }
1321}
1322
1323/**
1324 * ubi_thread - UBI background thread.
1325 * @u: the UBI device description object pointer
1326 */
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001327int ubi_thread(void *u)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001328{
1329 int failures = 0;
1330 struct ubi_device *ubi = u;
1331
1332 ubi_msg("background thread \"%s\" started, PID %d",
Pavel Emelyanovba25f9d2007-10-18 23:40:40 -07001333 ubi->bgt_name, task_pid_nr(current));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001334
Rafael J. Wysocki83144182007-07-17 04:03:35 -07001335 set_freezable();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001336 for (;;) {
1337 int err;
1338
1339 if (kthread_should_stop())
Kyungmin Parkcadb40c2008-05-22 10:32:18 +09001340 break;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001341
1342 if (try_to_freeze())
1343 continue;
1344
1345 spin_lock(&ubi->wl_lock);
1346 if (list_empty(&ubi->works) || ubi->ro_mode ||
Artem Bityutskiy27a0f2a2011-05-18 16:03:23 +03001347 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001348 set_current_state(TASK_INTERRUPTIBLE);
1349 spin_unlock(&ubi->wl_lock);
1350 schedule();
1351 continue;
1352 }
1353 spin_unlock(&ubi->wl_lock);
1354
1355 err = do_work(ubi);
1356 if (err) {
1357 ubi_err("%s: work failed with error code %d",
1358 ubi->bgt_name, err);
1359 if (failures++ > WL_MAX_FAILURES) {
1360 /*
1361 * Too many failures, disable the thread and
1362 * switch to read-only mode.
1363 */
1364 ubi_msg("%s: %d consecutive failures",
1365 ubi->bgt_name, WL_MAX_FAILURES);
1366 ubi_ro_mode(ubi);
Vitaliy Gusev2ad49882008-11-05 18:27:18 +03001367 ubi->thread_enabled = 0;
1368 continue;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001369 }
1370 } else
1371 failures = 0;
1372
1373 cond_resched();
1374 }
1375
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001376 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1377 return 0;
1378}
1379
1380/**
1381 * cancel_pending - cancel all pending works.
1382 * @ubi: UBI device description object
1383 */
1384static void cancel_pending(struct ubi_device *ubi)
1385{
1386 while (!list_empty(&ubi->works)) {
1387 struct ubi_work *wrk;
1388
1389 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1390 list_del(&wrk->list);
1391 wrk->func(ubi, wrk, 1);
1392 ubi->works_count -= 1;
1393 ubi_assert(ubi->works_count >= 0);
1394 }
1395}
1396
1397/**
Artem Bityutskiy41e0cd92012-05-17 21:05:33 +03001398 * ubi_wl_init - initialize the WL sub-system using attaching information.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001399 * @ubi: UBI device description object
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001400 * @ai: attaching information
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001401 *
1402 * This function returns zero in case of success, and a negative error code in
1403 * case of failure.
1404 */
Artem Bityutskiy41e0cd92012-05-17 21:05:33 +03001405int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001406{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001407 int err, i;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001408 struct rb_node *rb1, *rb2;
Artem Bityutskiy517af482012-05-17 14:38:34 +03001409 struct ubi_ainf_volume *av;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001410 struct ubi_ainf_peb *aeb, *tmp;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001411 struct ubi_wl_entry *e;
1412
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001413 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001414 spin_lock_init(&ubi->wl_lock);
Artem Bityutskiy43f9b252007-12-18 15:06:55 +02001415 mutex_init(&ubi->move_mutex);
Artem Bityutskiy593dd332007-12-18 15:54:35 +02001416 init_rwsem(&ubi->work_sem);
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001417 ubi->max_ec = ai->max_ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001418 INIT_LIST_HEAD(&ubi->works);
1419
1420 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1421
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001422 err = -ENOMEM;
1423 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1424 if (!ubi->lookuptbl)
Artem Bityutskiycdfa7882007-12-17 20:33:20 +02001425 return err;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001426
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001427 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1428 INIT_LIST_HEAD(&ubi->pq[i]);
1429 ubi->pq_head = 0;
1430
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001431 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001432 cond_resched();
1433
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001434 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001435 if (!e)
1436 goto out_free;
1437
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001438 e->pnum = aeb->pnum;
1439 e->ec = aeb->ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001440 ubi->lookuptbl[e->pnum] = e;
Joel Reardond36e59e2012-05-18 15:40:24 +02001441 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001442 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001443 goto out_free;
1444 }
1445 }
1446
Artem Bityutskiya4e60422012-05-17 13:09:08 +03001447 list_for_each_entry(aeb, &ai->free, u.list) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001448 cond_resched();
1449
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001450 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001451 if (!e)
1452 goto out_free;
1453
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001454 e->pnum = aeb->pnum;
1455 e->ec = aeb->ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001456 ubi_assert(e->ec >= 0);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001457 wl_tree_add(e, &ubi->free);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001458 ubi->lookuptbl[e->pnum] = e;
1459 }
1460
Artem Bityutskiy517af482012-05-17 14:38:34 +03001461 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1462 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001463 cond_resched();
1464
Artem Bityutskiy06b68ba2007-12-16 12:49:01 +02001465 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001466 if (!e)
1467 goto out_free;
1468
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001469 e->pnum = aeb->pnum;
1470 e->ec = aeb->ec;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001471 ubi->lookuptbl[e->pnum] = e;
Artem Bityutskiy2c5ec5c2012-05-17 08:26:24 +03001472 if (!aeb->scrub) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001473 dbg_wl("add PEB %d EC %d to the used tree",
1474 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001475 wl_tree_add(e, &ubi->used);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001476 } else {
1477 dbg_wl("add PEB %d EC %d to the scrub tree",
1478 e->pnum, e->ec);
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001479 wl_tree_add(e, &ubi->scrub);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001480 }
1481 }
1482 }
1483
Artem Bityutskiy5abde382007-09-13 14:48:20 +03001484 if (ubi->avail_pebs < WL_RESERVED_PEBS) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001485 ubi_err("no enough physical eraseblocks (%d, need %d)",
1486 ubi->avail_pebs, WL_RESERVED_PEBS);
Artem Bityutskiy5fc01ab2010-09-03 23:08:15 +03001487 if (ubi->corr_peb_count)
1488 ubi_err("%d PEBs are corrupted and not used",
1489 ubi->corr_peb_count);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001490 goto out_free;
1491 }
1492 ubi->avail_pebs -= WL_RESERVED_PEBS;
1493 ubi->rsvd_pebs += WL_RESERVED_PEBS;
1494
1495 /* Schedule wear-leveling if needed */
1496 err = ensure_wear_leveling(ubi);
1497 if (err)
1498 goto out_free;
1499
1500 return 0;
1501
1502out_free:
1503 cancel_pending(ubi);
1504 tree_destroy(&ubi->used);
1505 tree_destroy(&ubi->free);
1506 tree_destroy(&ubi->scrub);
1507 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001508 return err;
1509}
1510
1511/**
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001512 * protection_queue_destroy - destroy the protection queue.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001513 * @ubi: UBI device description object
1514 */
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001515static void protection_queue_destroy(struct ubi_device *ubi)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001516{
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001517 int i;
1518 struct ubi_wl_entry *e, *tmp;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001519
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001520 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1521 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1522 list_del(&e->u.list);
1523 kmem_cache_free(ubi_wl_entry_slab, e);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001524 }
1525 }
1526}
1527
1528/**
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001529 * ubi_wl_close - close the wear-leveling sub-system.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001530 * @ubi: UBI device description object
1531 */
1532void ubi_wl_close(struct ubi_device *ubi)
1533{
Artem Bityutskiy85c6e6e2008-07-16 10:25:56 +03001534 dbg_wl("close the WL sub-system");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001535 cancel_pending(ubi);
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001536 protection_queue_destroy(ubi);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001537 tree_destroy(&ubi->used);
Artem Bityutskiyb86a2c52009-05-24 14:13:34 +03001538 tree_destroy(&ubi->erroneous);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001539 tree_destroy(&ubi->free);
1540 tree_destroy(&ubi->scrub);
1541 kfree(ubi->lookuptbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001542}
1543
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001544/**
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001545 * self_check_ec - make sure that the erase counter of a PEB is correct.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001546 * @ubi: UBI device description object
1547 * @pnum: the physical eraseblock number to check
1548 * @ec: the erase counter to check
1549 *
1550 * This function returns zero if the erase counter of physical eraseblock @pnum
Artem Bityutskiyfeddbb32011-03-28 10:12:25 +03001551 * is equivalent to @ec, and a negative error code if not or if an error
1552 * occurred.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001553 */
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001554static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001555{
1556 int err;
1557 long long read_ec;
1558 struct ubi_ec_hdr *ec_hdr;
1559
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001560 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001561 return 0;
1562
Artem Bityutskiy33818bb2007-08-28 21:29:32 +03001563 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001564 if (!ec_hdr)
1565 return -ENOMEM;
1566
1567 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1568 if (err && err != UBI_IO_BITFLIPS) {
1569 /* The header does not have to exist */
1570 err = 0;
1571 goto out_free;
1572 }
1573
Christoph Hellwig3261ebd2007-05-21 17:41:46 +03001574 read_ec = be64_to_cpu(ec_hdr->ec);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001575 if (ec != read_ec) {
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001576 ubi_err("self-check failed for PEB %d", pnum);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001577 ubi_err("read EC is %lld, should be %d", read_ec, ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001578 dump_stack();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001579 err = 1;
1580 } else
1581 err = 0;
1582
1583out_free:
1584 kfree(ec_hdr);
1585 return err;
1586}
1587
1588/**
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001589 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
Artem Bityutskiyd99383b2011-05-18 14:47:34 +03001590 * @ubi: UBI device description object
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001591 * @e: the wear-leveling entry to check
1592 * @root: the root of the tree
1593 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001594 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1595 * is not.
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001596 */
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001597static int self_check_in_wl_tree(const struct ubi_device *ubi,
1598 struct ubi_wl_entry *e, struct rb_root *root)
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001599{
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001600 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001601 return 0;
1602
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001603 if (in_wl_tree(e, root))
1604 return 0;
1605
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001606 ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ",
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001607 e->pnum, e->ec, root);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001608 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001609 return -EINVAL;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001610}
1611
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001612/**
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001613 * self_check_in_pq - check if wear-leveling entry is in the protection
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001614 * queue.
1615 * @ubi: UBI device description object
1616 * @e: the wear-leveling entry to check
1617 *
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001618 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001619 */
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001620static int self_check_in_pq(const struct ubi_device *ubi,
1621 struct ubi_wl_entry *e)
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001622{
1623 struct ubi_wl_entry *p;
1624 int i;
1625
Artem Bityutskiy2a734bb2011-05-18 14:53:05 +03001626 if (!ubi->dbg->chk_gen)
Artem Bityutskiy92d124f2011-03-14 18:17:40 +02001627 return 0;
1628
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001629 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1630 list_for_each_entry(p, &ubi->pq[i], u.list)
1631 if (p == e)
1632 return 0;
1633
Artem Bityutskiy7bf523a2012-05-16 18:29:54 +03001634 ubi_err("self-check failed for PEB %d, EC %d, Protect queue",
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001635 e->pnum, e->ec);
Artem Bityutskiy25886a32012-04-24 06:59:49 +03001636 dump_stack();
Artem Bityutskiyadbf05e2010-01-20 10:28:58 +02001637 return -EINVAL;
Xiaochuan-Xu7b6c32d2008-12-15 21:07:41 +08001638}