blob: 0de2a4a5f8d8e6ade8fc2f286d52e3f7e601848e [file] [log] [blame]
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02004 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkc91a7192008-11-19 16:28:06 +01005 *
6 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
7 */
8
9/*
Heiko Schocherff94bc42014-06-24 10:10:04 +020010 * UBI wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010011 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020012 * This sub-system is responsible for wear-leveling. It works in terms of
13 * physical eraseblocks and erase counters and knows nothing about logical
14 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
15 * eraseblocks are of two types - used and free. Used physical eraseblocks are
16 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
17 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010018 *
19 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
Heiko Schocherff94bc42014-06-24 10:10:04 +020020 * header. The rest of the physical eraseblock contains only %0xFF bytes.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010021 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020022 * When physical eraseblocks are returned to the WL sub-system by means of the
Kyungmin Parkc91a7192008-11-19 16:28:06 +010023 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
24 * done asynchronously in context of the per-UBI device background thread,
Heiko Schocherff94bc42014-06-24 10:10:04 +020025 * which is also managed by the WL sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010026 *
27 * The wear-leveling is ensured by means of moving the contents of used
28 * physical eraseblocks with low erase counter to free physical eraseblocks
29 * with high erase counter.
30 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020031 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
32 * bad.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010033 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020034 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
35 * in a physical eraseblock, it has to be moved. Technically this is the same
36 * as moving it for wear-leveling reasons.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010037 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020038 * As it was said, for the UBI sub-system all physical eraseblocks are either
39 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
40 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
41 * RB-trees, as well as (temporarily) in the @wl->pq queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010042 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020043 * When the WL sub-system returns a physical eraseblock, the physical
44 * eraseblock is protected from being moved for some "time". For this reason,
45 * the physical eraseblock is not directly moved from the @wl->free tree to the
46 * @wl->used tree. There is a protection queue in between where this
47 * physical eraseblock is temporarily stored (@wl->pq).
48 *
49 * All this protection stuff is needed because:
50 * o we don't want to move physical eraseblocks just after we have given them
51 * to the user; instead, we first want to let users fill them up with data;
52 *
53 * o there is a chance that the user will put the physical eraseblock very
54 * soon, so it makes sense not to move it for some time, but wait.
55 *
56 * Physical eraseblocks stay protected only for limited time. But the "time" is
57 * measured in erase cycles in this case. This is implemented with help of the
58 * protection queue. Eraseblocks are put to the tail of this queue when they
59 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
60 * head of the queue on each erase operation (for any eraseblock). So the
61 * length of the queue defines how may (global) erase cycles PEBs are protected.
62 *
63 * To put it differently, each physical eraseblock has 2 main states: free and
64 * used. The former state corresponds to the @wl->free tree. The latter state
65 * is split up on several sub-states:
66 * o the WL movement is allowed (@wl->used tree);
67 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
68 * erroneous - e.g., there was a read error;
69 * o the WL movement is temporarily prohibited (@wl->pq queue);
70 * o scrubbing is needed (@wl->scrub tree).
71 *
72 * Depending on the sub-state, wear-leveling entries of the used physical
73 * eraseblocks may be kept in one of those structures.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010074 *
75 * Note, in this implementation, we keep a small in-RAM object for each physical
76 * eraseblock. This is surely not a scalable solution. But it appears to be good
77 * enough for moderately large flashes and it is simple. In future, one may
Heiko Schocherff94bc42014-06-24 10:10:04 +020078 * re-work this sub-system and make it more scalable.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010079 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020080 * At the moment this sub-system does not utilize the sequence number, which
81 * was introduced relatively recently. But it would be wise to do this because
82 * the sequence number of a logical eraseblock characterizes how old is it. For
Kyungmin Parkc91a7192008-11-19 16:28:06 +010083 * example, when we move a PEB with low erase counter, and we need to pick the
84 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
85 * pick target PEB with an average EC if our PEB is not very "old". This is a
Heiko Schocherff94bc42014-06-24 10:10:04 +020086 * room for future re-works of the WL sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010087 */
88
Heiko Schocherff94bc42014-06-24 10:10:04 +020089#ifndef __UBOOT__
Kyungmin Parkc91a7192008-11-19 16:28:06 +010090#include <linux/slab.h>
91#include <linux/crc32.h>
92#include <linux/freezer.h>
93#include <linux/kthread.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020094#else
95#include <ubi_uboot.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010096#endif
97
Kyungmin Parkc91a7192008-11-19 16:28:06 +010098#include "ubi.h"
Heiko Schocher0195a7b2015-10-22 06:19:21 +020099#include "wl.h"
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100100
101/* Number of physical eraseblocks reserved for wear-leveling purposes */
102#define WL_RESERVED_PEBS 1
103
104/*
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100105 * Maximum difference between two erase counters. If this threshold is
Heiko Schocherff94bc42014-06-24 10:10:04 +0200106 * exceeded, the WL sub-system starts moving data from used physical
107 * eraseblocks with low erase counter to free physical eraseblocks with high
108 * erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100109 */
110#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
111
112/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200113 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100114 * physical eraseblock to move to. The simplest way would be just to pick the
115 * one with the highest erase counter. But in certain workloads this could lead
116 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
117 * situation when the picked physical eraseblock is constantly erased after the
118 * data is written to it. So, we have a constant which limits the highest erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200119 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
120 * does not pick eraseblocks with erase counter greater than the lowest erase
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100121 * counter plus %WL_FREE_MAX_DIFF.
122 */
123#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
124
125/*
126 * Maximum number of consecutive background thread failures which is enough to
127 * switch to read-only mode.
128 */
129#define WL_MAX_FAILURES 32
130
Heiko Schocherff94bc42014-06-24 10:10:04 +0200131static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
132static int self_check_in_wl_tree(const struct ubi_device *ubi,
133 struct ubi_wl_entry *e, struct rb_root *root);
134static int self_check_in_pq(const struct ubi_device *ubi,
135 struct ubi_wl_entry *e);
136
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100137/**
138 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
139 * @e: the wear-leveling entry to add
140 * @root: the root of the tree
141 *
142 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
143 * the @ubi->used and @ubi->free RB-trees.
144 */
145static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
146{
147 struct rb_node **p, *parent = NULL;
148
149 p = &root->rb_node;
150 while (*p) {
151 struct ubi_wl_entry *e1;
152
153 parent = *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200154 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100155
156 if (e->ec < e1->ec)
157 p = &(*p)->rb_left;
158 else if (e->ec > e1->ec)
159 p = &(*p)->rb_right;
160 else {
161 ubi_assert(e->pnum != e1->pnum);
162 if (e->pnum < e1->pnum)
163 p = &(*p)->rb_left;
164 else
165 p = &(*p)->rb_right;
166 }
167 }
168
Heiko Schocherff94bc42014-06-24 10:10:04 +0200169 rb_link_node(&e->u.rb, parent, p);
170 rb_insert_color(&e->u.rb, root);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100171}
172
173/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200174 * wl_tree_destroy - destroy a wear-leveling entry.
175 * @ubi: UBI device description object
176 * @e: the wear-leveling entry to add
177 *
178 * This function destroys a wear leveling entry and removes
179 * the reference from the lookup table.
180 */
181static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
182{
183 ubi->lookuptbl[e->pnum] = NULL;
184 kmem_cache_free(ubi_wl_entry_slab, e);
185}
186
187/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100188 * do_work - do one pending work.
189 * @ubi: UBI device description object
190 *
191 * This function returns zero in case of success and a negative error code in
192 * case of failure.
193 */
194static int do_work(struct ubi_device *ubi)
195{
196 int err;
197 struct ubi_work *wrk;
198
199 cond_resched();
200
201 /*
202 * @ubi->work_sem is used to synchronize with the workers. Workers take
203 * it in read mode, so many of them may be doing works at a time. But
204 * the queue flush code has to be sure the whole queue of works is
205 * done, and it takes the mutex in write mode.
206 */
207 down_read(&ubi->work_sem);
208 spin_lock(&ubi->wl_lock);
209 if (list_empty(&ubi->works)) {
210 spin_unlock(&ubi->wl_lock);
211 up_read(&ubi->work_sem);
212 return 0;
213 }
214
215 wrk = list_entry(ubi->works.next, struct ubi_work, list);
216 list_del(&wrk->list);
217 ubi->works_count -= 1;
218 ubi_assert(ubi->works_count >= 0);
219 spin_unlock(&ubi->wl_lock);
220
221 /*
222 * Call the worker function. Do not touch the work structure
223 * after this call as it will have been freed or reused by that
224 * time by the worker function.
225 */
226 err = wrk->func(ubi, wrk, 0);
227 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200228 ubi_err(ubi, "work failed with error code %d", err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100229 up_read(&ubi->work_sem);
230
231 return err;
232}
233
234/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100235 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
236 * @e: the wear-leveling entry to check
237 * @root: the root of the tree
238 *
239 * This function returns non-zero if @e is in the @root RB-tree and zero if it
240 * is not.
241 */
242static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
243{
244 struct rb_node *p;
245
246 p = root->rb_node;
247 while (p) {
248 struct ubi_wl_entry *e1;
249
Heiko Schocherff94bc42014-06-24 10:10:04 +0200250 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100251
252 if (e->pnum == e1->pnum) {
253 ubi_assert(e == e1);
254 return 1;
255 }
256
257 if (e->ec < e1->ec)
258 p = p->rb_left;
259 else if (e->ec > e1->ec)
260 p = p->rb_right;
261 else {
262 ubi_assert(e->pnum != e1->pnum);
263 if (e->pnum < e1->pnum)
264 p = p->rb_left;
265 else
266 p = p->rb_right;
267 }
268 }
269
270 return 0;
271}
272
273/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200274 * prot_queue_add - add physical eraseblock to the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100275 * @ubi: UBI device description object
276 * @e: the physical eraseblock to add
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100277 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200278 * This function adds @e to the tail of the protection queue @ubi->pq, where
279 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
280 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
281 * be locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100282 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200283static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100284{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200285 int pq_tail = ubi->pq_head - 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100286
Heiko Schocherff94bc42014-06-24 10:10:04 +0200287 if (pq_tail < 0)
288 pq_tail = UBI_PROT_QUEUE_LEN - 1;
289 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
290 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
291 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100292}
293
294/**
295 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200296 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100297 * @root: the RB-tree where to look for
Heiko Schocherff94bc42014-06-24 10:10:04 +0200298 * @diff: maximum possible difference from the smallest erase counter
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100299 *
300 * This function looks for a wear leveling entry with erase counter closest to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200301 * min + @diff, where min is the smallest erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100302 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200303static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
304 struct rb_root *root, int diff)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100305{
306 struct rb_node *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200307 struct ubi_wl_entry *e, *prev_e = NULL;
308 int max;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100309
Heiko Schocherff94bc42014-06-24 10:10:04 +0200310 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
311 max = e->ec + diff;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100312
313 p = root->rb_node;
314 while (p) {
315 struct ubi_wl_entry *e1;
316
Heiko Schocherff94bc42014-06-24 10:10:04 +0200317 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100318 if (e1->ec >= max)
319 p = p->rb_left;
320 else {
321 p = p->rb_right;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200322 prev_e = e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100323 e = e1;
324 }
325 }
326
Heiko Schocherff94bc42014-06-24 10:10:04 +0200327 /* If no fastmap has been written and this WL entry can be used
328 * as anchor PEB, hold it back and return the second best WL entry
329 * such that fastmap can use the anchor PEB later. */
330 if (prev_e && !ubi->fm_disabled &&
331 !ubi->fm && e->pnum < UBI_FM_MAX_START)
332 return prev_e;
333
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100334 return e;
335}
336
337/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200338 * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100339 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200340 * @root: the RB-tree where to look for
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100341 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200342 * This function looks for a wear leveling entry with medium erase counter,
343 * but not greater or equivalent than the lowest erase counter plus
344 * %WL_FREE_MAX_DIFF/2.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100345 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200346static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
347 struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100348{
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100349 struct ubi_wl_entry *e, *first, *last;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100350
Heiko Schocherff94bc42014-06-24 10:10:04 +0200351 first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
352 last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100353
Heiko Schocherff94bc42014-06-24 10:10:04 +0200354 if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
355 e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100356
Heiko Schocherff94bc42014-06-24 10:10:04 +0200357 /* If no fastmap has been written and this WL entry can be used
358 * as anchor PEB, hold it back and return the second best
359 * WL entry such that fastmap can use the anchor PEB later. */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200360 e = may_reserve_for_fm(ubi, e, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200361 } else
362 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
363
364 return e;
365}
366
Heiko Schocherff94bc42014-06-24 10:10:04 +0200367/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200368 * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or
369 * refill_wl_user_pool().
Heiko Schocherff94bc42014-06-24 10:10:04 +0200370 * @ubi: UBI device description object
371 *
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200372 * This function returns a a wear leveling entry in case of success and
373 * NULL in case of failure.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200374 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200375static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200376{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200377 struct ubi_wl_entry *e;
378
Heiko Schocherff94bc42014-06-24 10:10:04 +0200379 e = find_mean_wl_entry(ubi, &ubi->free);
380 if (!e) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200381 ubi_err(ubi, "no free eraseblocks");
382 return NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200383 }
384
385 self_check_in_wl_tree(ubi, e, &ubi->free);
386
387 /*
388 * Move the physical eraseblock to the protection queue where it will
389 * be protected from being moved for some time.
390 */
391 rb_erase(&e->u.rb, &ubi->free);
392 ubi->free_count--;
393 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200394
395 return e;
396}
397
Heiko Schocherff94bc42014-06-24 10:10:04 +0200398/**
399 * prot_queue_del - remove a physical eraseblock from the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100400 * @ubi: UBI device description object
401 * @pnum: the physical eraseblock to remove
402 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200403 * This function deletes PEB @pnum from the protection queue and returns zero
404 * in case of success and %-ENODEV if the PEB was not found.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100405 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200406static int prot_queue_del(struct ubi_device *ubi, int pnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100407{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200408 struct ubi_wl_entry *e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100409
Heiko Schocherff94bc42014-06-24 10:10:04 +0200410 e = ubi->lookuptbl[pnum];
411 if (!e)
412 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100413
Heiko Schocherff94bc42014-06-24 10:10:04 +0200414 if (self_check_in_pq(ubi, e))
415 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100416
Heiko Schocherff94bc42014-06-24 10:10:04 +0200417 list_del(&e->u.list);
418 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100419 return 0;
420}
421
422/**
423 * sync_erase - synchronously erase a physical eraseblock.
424 * @ubi: UBI device description object
425 * @e: the the physical eraseblock to erase
426 * @torture: if the physical eraseblock has to be tortured
427 *
428 * This function returns zero in case of success and a negative error code in
429 * case of failure.
430 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200431static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
432 int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100433{
434 int err;
435 struct ubi_ec_hdr *ec_hdr;
436 unsigned long long ec = e->ec;
437
438 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
439
Heiko Schocherff94bc42014-06-24 10:10:04 +0200440 err = self_check_ec(ubi, e->pnum, e->ec);
441 if (err)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100442 return -EINVAL;
443
444 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
445 if (!ec_hdr)
446 return -ENOMEM;
447
448 err = ubi_io_sync_erase(ubi, e->pnum, torture);
449 if (err < 0)
450 goto out_free;
451
452 ec += err;
453 if (ec > UBI_MAX_ERASECOUNTER) {
454 /*
455 * Erase counter overflow. Upgrade UBI and use 64-bit
456 * erase counters internally.
457 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200458 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100459 e->pnum, ec);
460 err = -EINVAL;
461 goto out_free;
462 }
463
464 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
465
466 ec_hdr->ec = cpu_to_be64(ec);
467
468 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
469 if (err)
470 goto out_free;
471
472 e->ec = ec;
473 spin_lock(&ubi->wl_lock);
474 if (e->ec > ubi->max_ec)
475 ubi->max_ec = e->ec;
476 spin_unlock(&ubi->wl_lock);
477
478out_free:
479 kfree(ec_hdr);
480 return err;
481}
482
483/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200484 * serve_prot_queue - check if it is time to stop protecting PEBs.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100485 * @ubi: UBI device description object
486 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200487 * This function is called after each erase operation and removes PEBs from the
488 * tail of the protection queue. These PEBs have been protected for long enough
489 * and should be moved to the used tree.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100490 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200491static void serve_prot_queue(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100492{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200493 struct ubi_wl_entry *e, *tmp;
494 int count;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100495
496 /*
497 * There may be several protected physical eraseblock to remove,
498 * process them all.
499 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200500repeat:
501 count = 0;
502 spin_lock(&ubi->wl_lock);
503 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
504 dbg_wl("PEB %d EC %d protection over, move to used tree",
505 e->pnum, e->ec);
506
507 list_del(&e->u.list);
508 wl_tree_add(e, &ubi->used);
509 if (count++ > 32) {
510 /*
511 * Let's be nice and avoid holding the spinlock for
512 * too long.
513 */
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100514 spin_unlock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200515 cond_resched();
516 goto repeat;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100517 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100518 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200519
520 ubi->pq_head += 1;
521 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
522 ubi->pq_head = 0;
523 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
524 spin_unlock(&ubi->wl_lock);
525}
526
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100527#ifdef __UBOOT__
528void ubi_do_worker(struct ubi_device *ubi)
529{
530 int err;
531
532 if (list_empty(&ubi->works) || ubi->ro_mode ||
533 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi))
534 return;
535
536 spin_lock(&ubi->wl_lock);
537 while (!list_empty(&ubi->works)) {
538 /*
539 * call do_work, which executes exactly one work form the queue,
540 * including removeing it from the work queue.
541 */
542 spin_unlock(&ubi->wl_lock);
543 err = do_work(ubi);
544 spin_lock(&ubi->wl_lock);
545 if (err) {
546 ubi_err(ubi, "%s: work failed with error code %d",
547 ubi->bgt_name, err);
548 }
549 }
550 spin_unlock(&ubi->wl_lock);
551}
552#endif
553
Heiko Schocherff94bc42014-06-24 10:10:04 +0200554/**
555 * __schedule_ubi_work - schedule a work.
556 * @ubi: UBI device description object
557 * @wrk: the work to schedule
558 *
559 * This function adds a work defined by @wrk to the tail of the pending works
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200560 * list. Can only be used if ubi->work_sem is already held in read mode!
Heiko Schocherff94bc42014-06-24 10:10:04 +0200561 */
562static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
563{
564 spin_lock(&ubi->wl_lock);
565 list_add_tail(&wrk->list, &ubi->works);
566 ubi_assert(ubi->works_count >= 0);
567 ubi->works_count += 1;
568#ifndef __UBOOT__
569 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
570 wake_up_process(ubi->bgt_thread);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200571#endif
572 spin_unlock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100573}
574
575/**
576 * schedule_ubi_work - schedule a work.
577 * @ubi: UBI device description object
578 * @wrk: the work to schedule
579 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200580 * This function adds a work defined by @wrk to the tail of the pending works
581 * list.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100582 */
583static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
584{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200585 down_read(&ubi->work_sem);
586 __schedule_ubi_work(ubi, wrk);
587 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100588}
589
590static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200591 int shutdown);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200592
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100593/**
594 * schedule_erase - schedule an erase work.
595 * @ubi: UBI device description object
596 * @e: the WL entry of the physical eraseblock to erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200597 * @vol_id: the volume ID that last used this PEB
598 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100599 * @torture: if the physical eraseblock has to be tortured
600 *
601 * This function returns zero in case of success and a %-ENOMEM in case of
602 * failure.
603 */
604static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200605 int vol_id, int lnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100606{
607 struct ubi_work *wl_wrk;
608
Heiko Schocherff94bc42014-06-24 10:10:04 +0200609 ubi_assert(e);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200610
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100611 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
612 e->pnum, e->ec, torture);
613
614 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
615 if (!wl_wrk)
616 return -ENOMEM;
617
618 wl_wrk->func = &erase_worker;
619 wl_wrk->e = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200620 wl_wrk->vol_id = vol_id;
621 wl_wrk->lnum = lnum;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100622 wl_wrk->torture = torture;
623
624 schedule_ubi_work(ubi, wl_wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100625
626#ifdef __UBOOT__
627 ubi_do_worker(ubi);
628#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100629 return 0;
630}
631
632/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200633 * do_sync_erase - run the erase worker synchronously.
634 * @ubi: UBI device description object
635 * @e: the WL entry of the physical eraseblock to erase
636 * @vol_id: the volume ID that last used this PEB
637 * @lnum: the last used logical eraseblock number for the PEB
638 * @torture: if the physical eraseblock has to be tortured
639 *
640 */
641static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
642 int vol_id, int lnum, int torture)
643{
644 struct ubi_work *wl_wrk;
645
646 dbg_wl("sync erase of PEB %i", e->pnum);
647
648 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
649 if (!wl_wrk)
650 return -ENOMEM;
651
652 wl_wrk->e = e;
653 wl_wrk->vol_id = vol_id;
654 wl_wrk->lnum = lnum;
655 wl_wrk->torture = torture;
656
657 return erase_worker(ubi, wl_wrk, 0);
658}
659
Heiko Schocherff94bc42014-06-24 10:10:04 +0200660/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100661 * wear_leveling_worker - wear-leveling worker function.
662 * @ubi: UBI device description object
663 * @wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200664 * @shutdown: non-zero if the worker has to free memory and exit
665 * because the WL-subsystem is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100666 *
667 * This function copies a more worn out physical eraseblock to a less worn out
668 * one. Returns zero in case of success and a negative error code in case of
669 * failure.
670 */
671static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200672 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100673{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200674 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200675 int vol_id = -1, lnum = -1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200676#ifdef CONFIG_MTD_UBI_FASTMAP
677 int anchor = wrk->anchor;
678#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100679 struct ubi_wl_entry *e1, *e2;
680 struct ubi_vid_hdr *vid_hdr;
681
682 kfree(wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200683 if (shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100684 return 0;
685
686 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
687 if (!vid_hdr)
688 return -ENOMEM;
689
690 mutex_lock(&ubi->move_mutex);
691 spin_lock(&ubi->wl_lock);
692 ubi_assert(!ubi->move_from && !ubi->move_to);
693 ubi_assert(!ubi->move_to_put);
694
695 if (!ubi->free.rb_node ||
696 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
697 /*
698 * No free physical eraseblocks? Well, they must be waiting in
699 * the queue to be erased. Cancel movement - it will be
700 * triggered again when a free physical eraseblock appears.
701 *
702 * No used physical eraseblocks? They must be temporarily
703 * protected from being moved. They will be moved to the
704 * @ubi->used tree later and the wear-leveling will be
705 * triggered again.
706 */
707 dbg_wl("cancel WL, a list is empty: free %d, used %d",
708 !ubi->free.rb_node, !ubi->used.rb_node);
709 goto out_cancel;
710 }
711
Heiko Schocherff94bc42014-06-24 10:10:04 +0200712#ifdef CONFIG_MTD_UBI_FASTMAP
713 /* Check whether we need to produce an anchor PEB */
714 if (!anchor)
715 anchor = !anchor_pebs_avalible(&ubi->free);
716
717 if (anchor) {
718 e1 = find_anchor_wl_entry(&ubi->used);
719 if (!e1)
720 goto out_cancel;
721 e2 = get_peb_for_wl(ubi);
722 if (!e2)
723 goto out_cancel;
724
725 self_check_in_wl_tree(ubi, e1, &ubi->used);
726 rb_erase(&e1->u.rb, &ubi->used);
727 dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
728 } else if (!ubi->scrub.rb_node) {
729#else
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100730 if (!ubi->scrub.rb_node) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200731#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100732 /*
733 * Now pick the least worn-out used physical eraseblock and a
734 * highly worn-out free physical eraseblock. If the erase
735 * counters differ much enough, start wear-leveling.
736 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200737 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
738 e2 = get_peb_for_wl(ubi);
739 if (!e2)
740 goto out_cancel;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100741
742 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
743 dbg_wl("no WL needed: min used EC %d, max free EC %d",
744 e1->ec, e2->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200745
746 /* Give the unused PEB back */
747 wl_tree_add(e2, &ubi->free);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200748 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100749 goto out_cancel;
750 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200751 self_check_in_wl_tree(ubi, e1, &ubi->used);
752 rb_erase(&e1->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100753 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
754 e1->pnum, e1->ec, e2->pnum, e2->ec);
755 } else {
756 /* Perform scrubbing */
757 scrubbing = 1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200758 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
759 e2 = get_peb_for_wl(ubi);
760 if (!e2)
761 goto out_cancel;
762
763 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
764 rb_erase(&e1->u.rb, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100765 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
766 }
767
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100768 ubi->move_from = e1;
769 ubi->move_to = e2;
770 spin_unlock(&ubi->wl_lock);
771
772 /*
773 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
774 * We so far do not know which logical eraseblock our physical
775 * eraseblock (@e1) belongs to. We have to read the volume identifier
776 * header first.
777 *
778 * Note, we are protected from this PEB being unmapped and erased. The
779 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
780 * which is being moved was unmapped.
781 */
782
783 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
784 if (err && err != UBI_IO_BITFLIPS) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200785 if (err == UBI_IO_FF) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100786 /*
787 * We are trying to move PEB without a VID header. UBI
788 * always write VID headers shortly after the PEB was
Heiko Schocherff94bc42014-06-24 10:10:04 +0200789 * given, so we have a situation when it has not yet
790 * had a chance to write it, because it was preempted.
791 * So add this PEB to the protection queue so far,
792 * because presumably more data will be written there
793 * (including the missing VID header), and then we'll
794 * move it.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100795 */
796 dbg_wl("PEB %d has no VID header", e1->pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200797 protect = 1;
798 goto out_not_moved;
799 } else if (err == UBI_IO_FF_BITFLIPS) {
800 /*
801 * The same situation as %UBI_IO_FF, but bit-flips were
802 * detected. It is better to schedule this PEB for
803 * scrubbing.
804 */
805 dbg_wl("PEB %d has no VID header but has bit-flips",
806 e1->pnum);
807 scrubbing = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100808 goto out_not_moved;
809 }
810
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200811 ubi_err(ubi, "error %d while reading VID header from PEB %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100812 err, e1->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100813 goto out_error;
814 }
815
Heiko Schocherff94bc42014-06-24 10:10:04 +0200816 vol_id = be32_to_cpu(vid_hdr->vol_id);
817 lnum = be32_to_cpu(vid_hdr->lnum);
818
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100819 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
820 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200821 if (err == MOVE_CANCEL_RACE) {
822 /*
823 * The LEB has not been moved because the volume is
824 * being deleted or the PEB has been put meanwhile. We
825 * should prevent this PEB from being selected for
826 * wear-leveling movement again, so put it to the
827 * protection queue.
828 */
829 protect = 1;
830 goto out_not_moved;
831 }
832 if (err == MOVE_RETRY) {
833 scrubbing = 1;
834 goto out_not_moved;
835 }
836 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
837 err == MOVE_TARGET_RD_ERR) {
838 /*
839 * Target PEB had bit-flips or write error - torture it.
840 */
841 torture = 1;
842 goto out_not_moved;
843 }
844
845 if (err == MOVE_SOURCE_RD_ERR) {
846 /*
847 * An error happened while reading the source PEB. Do
848 * not switch to R/O mode in this case, and give the
849 * upper layers a possibility to recover from this,
850 * e.g. by unmapping corresponding LEB. Instead, just
851 * put this PEB to the @ubi->erroneous list to prevent
852 * UBI from trying to move it over and over again.
853 */
854 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200855 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200856 ubi->erroneous_peb_count);
857 goto out_error;
858 }
859 erroneous = 1;
860 goto out_not_moved;
861 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100862
863 if (err < 0)
864 goto out_error;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100865
Heiko Schocherff94bc42014-06-24 10:10:04 +0200866 ubi_assert(0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100867 }
868
Heiko Schocherff94bc42014-06-24 10:10:04 +0200869 /* The PEB has been successfully moved */
870 if (scrubbing)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200871 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200872 e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100873 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200874
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100875 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200876 if (!ubi->move_to_put) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100877 wl_tree_add(e2, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200878 e2 = NULL;
879 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100880 ubi->move_from = ubi->move_to = NULL;
881 ubi->move_to_put = ubi->wl_scheduled = 0;
882 spin_unlock(&ubi->wl_lock);
883
Heiko Schocherff94bc42014-06-24 10:10:04 +0200884 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
885 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200886 if (e2)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200887 wl_entry_destroy(ubi, e2);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200888 goto out_ro;
889 }
890
891 if (e2) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100892 /*
893 * Well, the target PEB was put meanwhile, schedule it for
894 * erasure.
895 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200896 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
897 e2->pnum, vol_id, lnum);
898 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200899 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200900 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100901 }
902
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100903 dbg_wl("done");
904 mutex_unlock(&ubi->move_mutex);
905 return 0;
906
907 /*
908 * For some reasons the LEB was not moved, might be an error, might be
909 * something else. @e1 was not changed, so return it back. @e2 might
Heiko Schocherff94bc42014-06-24 10:10:04 +0200910 * have been changed, schedule it for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100911 */
912out_not_moved:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200913 if (vol_id != -1)
914 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
915 e1->pnum, vol_id, lnum, e2->pnum, err);
916 else
917 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
918 e1->pnum, e2->pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100919 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200920 if (protect)
921 prot_queue_add(ubi, e1);
922 else if (erroneous) {
923 wl_tree_add(e1, &ubi->erroneous);
924 ubi->erroneous_peb_count += 1;
925 } else if (scrubbing)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100926 wl_tree_add(e1, &ubi->scrub);
927 else
928 wl_tree_add(e1, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200929 ubi_assert(!ubi->move_to_put);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100930 ubi->move_from = ubi->move_to = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200931 ubi->wl_scheduled = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100932 spin_unlock(&ubi->wl_lock);
933
Heiko Schocherff94bc42014-06-24 10:10:04 +0200934 ubi_free_vid_hdr(ubi, vid_hdr);
935 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200936 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200937 goto out_ro;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200938
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100939 mutex_unlock(&ubi->move_mutex);
940 return 0;
941
942out_error:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200943 if (vol_id != -1)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200944 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200945 err, e1->pnum, e2->pnum);
946 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200947 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200948 err, e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100949 spin_lock(&ubi->wl_lock);
950 ubi->move_from = ubi->move_to = NULL;
951 ubi->move_to_put = ubi->wl_scheduled = 0;
952 spin_unlock(&ubi->wl_lock);
953
Heiko Schocherff94bc42014-06-24 10:10:04 +0200954 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200955 wl_entry_destroy(ubi, e1);
956 wl_entry_destroy(ubi, e2);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100957
Heiko Schocherff94bc42014-06-24 10:10:04 +0200958out_ro:
959 ubi_ro_mode(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100960 mutex_unlock(&ubi->move_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200961 ubi_assert(err != 0);
962 return err < 0 ? err : -EIO;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100963
964out_cancel:
965 ubi->wl_scheduled = 0;
966 spin_unlock(&ubi->wl_lock);
967 mutex_unlock(&ubi->move_mutex);
968 ubi_free_vid_hdr(ubi, vid_hdr);
969 return 0;
970}
971
972/**
973 * ensure_wear_leveling - schedule wear-leveling if it is needed.
974 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200975 * @nested: set to non-zero if this function is called from UBI worker
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100976 *
977 * This function checks if it is time to start wear-leveling and schedules it
978 * if yes. This function returns zero in case of success and a negative error
979 * code in case of failure.
980 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200981static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100982{
983 int err = 0;
984 struct ubi_wl_entry *e1;
985 struct ubi_wl_entry *e2;
986 struct ubi_work *wrk;
987
988 spin_lock(&ubi->wl_lock);
989 if (ubi->wl_scheduled)
990 /* Wear-leveling is already in the work queue */
991 goto out_unlock;
992
993 /*
994 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
995 * the WL worker has to be scheduled anyway.
996 */
997 if (!ubi->scrub.rb_node) {
998 if (!ubi->used.rb_node || !ubi->free.rb_node)
999 /* No physical eraseblocks - no deal */
1000 goto out_unlock;
1001
1002 /*
1003 * We schedule wear-leveling only if the difference between the
1004 * lowest erase counter of used physical eraseblocks and a high
Heiko Schocherff94bc42014-06-24 10:10:04 +02001005 * erase counter of free physical eraseblocks is greater than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001006 * %UBI_WL_THRESHOLD.
1007 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001008 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1009 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001010
1011 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
1012 goto out_unlock;
1013 dbg_wl("schedule wear-leveling");
1014 } else
1015 dbg_wl("schedule scrubbing");
1016
1017 ubi->wl_scheduled = 1;
1018 spin_unlock(&ubi->wl_lock);
1019
1020 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1021 if (!wrk) {
1022 err = -ENOMEM;
1023 goto out_cancel;
1024 }
1025
Heiko Schocherff94bc42014-06-24 10:10:04 +02001026 wrk->anchor = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001027 wrk->func = &wear_leveling_worker;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001028 if (nested)
1029 __schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001030#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +02001031 else
1032 schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001033#else
1034 else {
1035 schedule_ubi_work(ubi, wrk);
1036 ubi_do_worker(ubi);
1037 }
1038#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001039 return err;
1040
1041out_cancel:
1042 spin_lock(&ubi->wl_lock);
1043 ubi->wl_scheduled = 0;
1044out_unlock:
1045 spin_unlock(&ubi->wl_lock);
1046 return err;
1047}
1048
1049/**
1050 * erase_worker - physical eraseblock erase worker function.
1051 * @ubi: UBI device description object
1052 * @wl_wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001053 * @shutdown: non-zero if the worker has to free memory and exit
1054 * because the WL sub-system is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001055 *
1056 * This function erases a physical eraseblock and perform torture testing if
1057 * needed. It also takes care about marking the physical eraseblock bad if
1058 * needed. Returns zero in case of success and a negative error code in case of
1059 * failure.
1060 */
1061static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001062 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001063{
1064 struct ubi_wl_entry *e = wl_wrk->e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001065 int pnum = e->pnum;
1066 int vol_id = wl_wrk->vol_id;
1067 int lnum = wl_wrk->lnum;
1068 int err, available_consumed = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001069
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001070 if (shutdown) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001071 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1072 kfree(wl_wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001073 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001074 return 0;
1075 }
1076
Heiko Schocherff94bc42014-06-24 10:10:04 +02001077 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1078 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1079
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001080 err = sync_erase(ubi, e, wl_wrk->torture);
1081 if (!err) {
1082 /* Fine, we've erased it successfully */
1083 kfree(wl_wrk);
1084
1085 spin_lock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001086 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001087 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001088 spin_unlock(&ubi->wl_lock);
1089
1090 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001091 * One more erase operation has happened, take care about
1092 * protected physical eraseblocks.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001093 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001094 serve_prot_queue(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001095
1096 /* And take care about wear-leveling */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001097 err = ensure_wear_leveling(ubi, 1);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001098 return err;
1099 }
1100
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001101 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001102 kfree(wl_wrk);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001103
1104 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1105 err == -EBUSY) {
1106 int err1;
1107
1108 /* Re-schedule the LEB for erasure */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001109 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001110 if (err1) {
1111 err = err1;
1112 goto out_ro;
1113 }
1114 return err;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001115 }
1116
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001117 wl_entry_destroy(ubi, e);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001118 if (err != -EIO)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001119 /*
1120 * If this is not %-EIO, we have no idea what to do. Scheduling
1121 * this physical eraseblock for erasure again would cause
Heiko Schocherff94bc42014-06-24 10:10:04 +02001122 * errors again and again. Well, lets switch to R/O mode.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001123 */
1124 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001125
1126 /* It is %-EIO, the PEB went bad */
1127
1128 if (!ubi->bad_allowed) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001129 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001130 goto out_ro;
1131 }
1132
1133 spin_lock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001134 if (ubi->beb_rsvd_pebs == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001135 if (ubi->avail_pebs == 0) {
1136 spin_unlock(&ubi->volumes_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001137 ubi_err(ubi, "no reserved/available physical eraseblocks");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001138 goto out_ro;
1139 }
1140 ubi->avail_pebs -= 1;
1141 available_consumed = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001142 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001143 spin_unlock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001144
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001145 ubi_msg(ubi, "mark PEB %d as bad", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001146 err = ubi_io_mark_bad(ubi, pnum);
1147 if (err)
1148 goto out_ro;
1149
1150 spin_lock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001151 if (ubi->beb_rsvd_pebs > 0) {
1152 if (available_consumed) {
1153 /*
1154 * The amount of reserved PEBs increased since we last
1155 * checked.
1156 */
1157 ubi->avail_pebs += 1;
1158 available_consumed = 0;
1159 }
1160 ubi->beb_rsvd_pebs -= 1;
1161 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001162 ubi->bad_peb_count += 1;
1163 ubi->good_peb_count -= 1;
1164 ubi_calculate_reserved(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001165 if (available_consumed)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001166 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001167 else if (ubi->beb_rsvd_pebs)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001168 ubi_msg(ubi, "%d PEBs left in the reserve",
1169 ubi->beb_rsvd_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001170 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001171 ubi_warn(ubi, "last PEB from the reserve was used");
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001172 spin_unlock(&ubi->volumes_lock);
1173
1174 return err;
1175
1176out_ro:
Heiko Schocherff94bc42014-06-24 10:10:04 +02001177 if (available_consumed) {
1178 spin_lock(&ubi->volumes_lock);
1179 ubi->avail_pebs += 1;
1180 spin_unlock(&ubi->volumes_lock);
1181 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001182 ubi_ro_mode(ubi);
1183 return err;
1184}
1185
1186/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001187 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001188 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001189 * @vol_id: the volume ID that last used this PEB
1190 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001191 * @pnum: physical eraseblock to return
1192 * @torture: if this physical eraseblock has to be tortured
1193 *
1194 * This function is called to return physical eraseblock @pnum to the pool of
1195 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1196 * occurred to this @pnum and it has to be tested. This function returns zero
1197 * in case of success, and a negative error code in case of failure.
1198 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001199int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1200 int pnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001201{
1202 int err;
1203 struct ubi_wl_entry *e;
1204
1205 dbg_wl("PEB %d", pnum);
1206 ubi_assert(pnum >= 0);
1207 ubi_assert(pnum < ubi->peb_count);
1208
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001209 down_read(&ubi->fm_protect);
1210
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001211retry:
1212 spin_lock(&ubi->wl_lock);
1213 e = ubi->lookuptbl[pnum];
1214 if (e == ubi->move_from) {
1215 /*
1216 * User is putting the physical eraseblock which was selected to
1217 * be moved. It will be scheduled for erasure in the
1218 * wear-leveling worker.
1219 */
1220 dbg_wl("PEB %d is being moved, wait", pnum);
1221 spin_unlock(&ubi->wl_lock);
1222
1223 /* Wait for the WL worker by taking the @ubi->move_mutex */
1224 mutex_lock(&ubi->move_mutex);
1225 mutex_unlock(&ubi->move_mutex);
1226 goto retry;
1227 } else if (e == ubi->move_to) {
1228 /*
1229 * User is putting the physical eraseblock which was selected
1230 * as the target the data is moved to. It may happen if the EBA
Heiko Schocherff94bc42014-06-24 10:10:04 +02001231 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1232 * but the WL sub-system has not put the PEB to the "used" tree
1233 * yet, but it is about to do this. So we just set a flag which
1234 * will tell the WL worker that the PEB is not needed anymore
1235 * and should be scheduled for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001236 */
1237 dbg_wl("PEB %d is the target of data moving", pnum);
1238 ubi_assert(!ubi->move_to_put);
1239 ubi->move_to_put = 1;
1240 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001241 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001242 return 0;
1243 } else {
1244 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001245 self_check_in_wl_tree(ubi, e, &ubi->used);
1246 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001247 } else if (in_wl_tree(e, &ubi->scrub)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001248 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1249 rb_erase(&e->u.rb, &ubi->scrub);
1250 } else if (in_wl_tree(e, &ubi->erroneous)) {
1251 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1252 rb_erase(&e->u.rb, &ubi->erroneous);
1253 ubi->erroneous_peb_count -= 1;
1254 ubi_assert(ubi->erroneous_peb_count >= 0);
1255 /* Erroneous PEBs should be tortured */
1256 torture = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001257 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001258 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001259 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001260 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001261 ubi_ro_mode(ubi);
1262 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001263 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001264 return err;
1265 }
1266 }
1267 }
1268 spin_unlock(&ubi->wl_lock);
1269
Heiko Schocherff94bc42014-06-24 10:10:04 +02001270 err = schedule_erase(ubi, e, vol_id, lnum, torture);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001271 if (err) {
1272 spin_lock(&ubi->wl_lock);
1273 wl_tree_add(e, &ubi->used);
1274 spin_unlock(&ubi->wl_lock);
1275 }
1276
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001277 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001278 return err;
1279}
1280
1281/**
1282 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1283 * @ubi: UBI device description object
1284 * @pnum: the physical eraseblock to schedule
1285 *
1286 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1287 * needs scrubbing. This function schedules a physical eraseblock for
1288 * scrubbing which is done in background. This function returns zero in case of
1289 * success and a negative error code in case of failure.
1290 */
1291int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1292{
1293 struct ubi_wl_entry *e;
1294
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001295 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001296
1297retry:
1298 spin_lock(&ubi->wl_lock);
1299 e = ubi->lookuptbl[pnum];
Heiko Schocherff94bc42014-06-24 10:10:04 +02001300 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1301 in_wl_tree(e, &ubi->erroneous)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001302 spin_unlock(&ubi->wl_lock);
1303 return 0;
1304 }
1305
1306 if (e == ubi->move_to) {
1307 /*
1308 * This physical eraseblock was used to move data to. The data
1309 * was moved but the PEB was not yet inserted to the proper
1310 * tree. We should just wait a little and let the WL worker
1311 * proceed.
1312 */
1313 spin_unlock(&ubi->wl_lock);
1314 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1315 yield();
1316 goto retry;
1317 }
1318
1319 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001320 self_check_in_wl_tree(ubi, e, &ubi->used);
1321 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001322 } else {
1323 int err;
1324
Heiko Schocherff94bc42014-06-24 10:10:04 +02001325 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001326 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001327 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001328 ubi_ro_mode(ubi);
1329 spin_unlock(&ubi->wl_lock);
1330 return err;
1331 }
1332 }
1333
1334 wl_tree_add(e, &ubi->scrub);
1335 spin_unlock(&ubi->wl_lock);
1336
1337 /*
1338 * Technically scrubbing is the same as wear-leveling, so it is done
1339 * by the WL worker.
1340 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001341 return ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001342}
1343
1344/**
1345 * ubi_wl_flush - flush all pending works.
1346 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001347 * @vol_id: the volume id to flush for
1348 * @lnum: the logical eraseblock number to flush for
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001349 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001350 * This function executes all pending works for a particular volume id /
1351 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1352 * acts as a wildcard for all of the corresponding volume numbers or logical
1353 * eraseblock numbers. It returns zero in case of success and a negative error
1354 * code in case of failure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001355 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001356int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001357{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001358 int err = 0;
1359 int found = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001360
1361 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001362 * Erase while the pending works queue is not empty, but not more than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001363 * the number of currently pending works.
1364 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001365 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1366 vol_id, lnum, ubi->works_count);
1367
1368 while (found) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001369 struct ubi_work *wrk, *tmp;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001370 found = 0;
1371
1372 down_read(&ubi->work_sem);
1373 spin_lock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001374 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001375 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1376 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1377 list_del(&wrk->list);
1378 ubi->works_count -= 1;
1379 ubi_assert(ubi->works_count >= 0);
1380 spin_unlock(&ubi->wl_lock);
1381
1382 err = wrk->func(ubi, wrk, 0);
1383 if (err) {
1384 up_read(&ubi->work_sem);
1385 return err;
1386 }
1387
1388 spin_lock(&ubi->wl_lock);
1389 found = 1;
1390 break;
1391 }
1392 }
1393 spin_unlock(&ubi->wl_lock);
1394 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001395 }
1396
1397 /*
1398 * Make sure all the works which have been done in parallel are
1399 * finished.
1400 */
1401 down_write(&ubi->work_sem);
1402 up_write(&ubi->work_sem);
1403
Heiko Schocherff94bc42014-06-24 10:10:04 +02001404 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001405}
1406
1407/**
1408 * tree_destroy - destroy an RB-tree.
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001409 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001410 * @root: the root of the tree to destroy
1411 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001412static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001413{
1414 struct rb_node *rb;
1415 struct ubi_wl_entry *e;
1416
1417 rb = root->rb_node;
1418 while (rb) {
1419 if (rb->rb_left)
1420 rb = rb->rb_left;
1421 else if (rb->rb_right)
1422 rb = rb->rb_right;
1423 else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001424 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001425
1426 rb = rb_parent(rb);
1427 if (rb) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001428 if (rb->rb_left == &e->u.rb)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001429 rb->rb_left = NULL;
1430 else
1431 rb->rb_right = NULL;
1432 }
1433
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001434 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001435 }
1436 }
1437}
1438
1439/**
1440 * ubi_thread - UBI background thread.
1441 * @u: the UBI device description object pointer
1442 */
1443int ubi_thread(void *u)
1444{
1445 int failures = 0;
1446 struct ubi_device *ubi = u;
1447
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001448 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001449 ubi->bgt_name, task_pid_nr(current));
1450
1451 set_freezable();
1452 for (;;) {
1453 int err;
1454
1455 if (kthread_should_stop())
1456 break;
1457
1458 if (try_to_freeze())
1459 continue;
1460
1461 spin_lock(&ubi->wl_lock);
1462 if (list_empty(&ubi->works) || ubi->ro_mode ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02001463 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001464 set_current_state(TASK_INTERRUPTIBLE);
1465 spin_unlock(&ubi->wl_lock);
1466 schedule();
1467 continue;
1468 }
1469 spin_unlock(&ubi->wl_lock);
1470
1471 err = do_work(ubi);
1472 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001473 ubi_err(ubi, "%s: work failed with error code %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001474 ubi->bgt_name, err);
1475 if (failures++ > WL_MAX_FAILURES) {
1476 /*
1477 * Too many failures, disable the thread and
1478 * switch to read-only mode.
1479 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001480 ubi_msg(ubi, "%s: %d consecutive failures",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001481 ubi->bgt_name, WL_MAX_FAILURES);
1482 ubi_ro_mode(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001483 ubi->thread_enabled = 0;
1484 continue;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001485 }
1486 } else
1487 failures = 0;
1488
1489 cond_resched();
1490 }
1491
1492 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1493 return 0;
1494}
1495
1496/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001497 * shutdown_work - shutdown all pending works.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001498 * @ubi: UBI device description object
1499 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001500static void shutdown_work(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001501{
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001502#ifdef CONFIG_MTD_UBI_FASTMAP
1503#ifndef __UBOOT__
1504 flush_work(&ubi->fm_work);
1505#else
1506 /* in U-Boot, we have all work done */
1507#endif
1508#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001509 while (!list_empty(&ubi->works)) {
1510 struct ubi_work *wrk;
1511
1512 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1513 list_del(&wrk->list);
1514 wrk->func(ubi, wrk, 1);
1515 ubi->works_count -= 1;
1516 ubi_assert(ubi->works_count >= 0);
1517 }
1518}
1519
1520/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001521 * ubi_wl_init - initialize the WL sub-system using attaching information.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001522 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001523 * @ai: attaching information
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001524 *
1525 * This function returns zero in case of success, and a negative error code in
1526 * case of failure.
1527 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001528int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001529{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001530 int err, i, reserved_pebs, found_pebs = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001531 struct rb_node *rb1, *rb2;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001532 struct ubi_ainf_volume *av;
1533 struct ubi_ainf_peb *aeb, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001534 struct ubi_wl_entry *e;
1535
Heiko Schocherff94bc42014-06-24 10:10:04 +02001536 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001537 spin_lock_init(&ubi->wl_lock);
1538 mutex_init(&ubi->move_mutex);
1539 init_rwsem(&ubi->work_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001540 ubi->max_ec = ai->max_ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001541 INIT_LIST_HEAD(&ubi->works);
1542
1543 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1544
1545 err = -ENOMEM;
1546 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1547 if (!ubi->lookuptbl)
1548 return err;
1549
Heiko Schocherff94bc42014-06-24 10:10:04 +02001550 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1551 INIT_LIST_HEAD(&ubi->pq[i]);
1552 ubi->pq_head = 0;
1553
Heiko Schocher68fc4492016-02-02 11:54:35 +01001554 ubi->free_count = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001555 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001556 cond_resched();
1557
1558 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1559 if (!e)
1560 goto out_free;
1561
Heiko Schocherff94bc42014-06-24 10:10:04 +02001562 e->pnum = aeb->pnum;
1563 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001564 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001565 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001566 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001567 goto out_free;
1568 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001569
1570 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001571 }
1572
Heiko Schocherff94bc42014-06-24 10:10:04 +02001573 list_for_each_entry(aeb, &ai->free, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001574 cond_resched();
1575
1576 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1577 if (!e)
1578 goto out_free;
1579
Heiko Schocherff94bc42014-06-24 10:10:04 +02001580 e->pnum = aeb->pnum;
1581 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001582 ubi_assert(e->ec >= 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001583
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001584 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001585 ubi->free_count++;
1586
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001587 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001588
1589 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001590 }
1591
Heiko Schocherff94bc42014-06-24 10:10:04 +02001592 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1593 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001594 cond_resched();
1595
1596 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1597 if (!e)
1598 goto out_free;
1599
Heiko Schocherff94bc42014-06-24 10:10:04 +02001600 e->pnum = aeb->pnum;
1601 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001602 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001603
1604 if (!aeb->scrub) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001605 dbg_wl("add PEB %d EC %d to the used tree",
1606 e->pnum, e->ec);
1607 wl_tree_add(e, &ubi->used);
1608 } else {
1609 dbg_wl("add PEB %d EC %d to the scrub tree",
1610 e->pnum, e->ec);
1611 wl_tree_add(e, &ubi->scrub);
1612 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001613
1614 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001615 }
1616 }
1617
Heiko Schocherff94bc42014-06-24 10:10:04 +02001618 dbg_wl("found %i PEBs", found_pebs);
1619
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001620 if (ubi->fm) {
1621 ubi_assert(ubi->good_peb_count ==
Heiko Schocherff94bc42014-06-24 10:10:04 +02001622 found_pebs + ubi->fm->used_blocks);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001623
1624 for (i = 0; i < ubi->fm->used_blocks; i++) {
1625 e = ubi->fm->e[i];
1626 ubi->lookuptbl[e->pnum] = e;
1627 }
1628 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001629 else
1630 ubi_assert(ubi->good_peb_count == found_pebs);
1631
1632 reserved_pebs = WL_RESERVED_PEBS;
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001633 ubi_fastmap_init(ubi, &reserved_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001634
1635 if (ubi->avail_pebs < reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001636 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001637 ubi->avail_pebs, reserved_pebs);
1638 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001639 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001640 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001641 goto out_free;
1642 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001643 ubi->avail_pebs -= reserved_pebs;
1644 ubi->rsvd_pebs += reserved_pebs;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001645
1646 /* Schedule wear-leveling if needed */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001647 err = ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001648 if (err)
1649 goto out_free;
1650
1651 return 0;
1652
1653out_free:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001654 shutdown_work(ubi);
1655 tree_destroy(ubi, &ubi->used);
1656 tree_destroy(ubi, &ubi->free);
1657 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001658 kfree(ubi->lookuptbl);
1659 return err;
1660}
1661
1662/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001663 * protection_queue_destroy - destroy the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001664 * @ubi: UBI device description object
1665 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001666static void protection_queue_destroy(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001667{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001668 int i;
1669 struct ubi_wl_entry *e, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001670
Heiko Schocherff94bc42014-06-24 10:10:04 +02001671 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1672 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1673 list_del(&e->u.list);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001674 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001675 }
1676 }
1677}
1678
1679/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001680 * ubi_wl_close - close the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001681 * @ubi: UBI device description object
1682 */
1683void ubi_wl_close(struct ubi_device *ubi)
1684{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001685 dbg_wl("close the WL sub-system");
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001686 ubi_fastmap_close(ubi);
1687 shutdown_work(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001688 protection_queue_destroy(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001689 tree_destroy(ubi, &ubi->used);
1690 tree_destroy(ubi, &ubi->erroneous);
1691 tree_destroy(ubi, &ubi->free);
1692 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001693 kfree(ubi->lookuptbl);
1694}
1695
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001696/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001697 * self_check_ec - make sure that the erase counter of a PEB is correct.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001698 * @ubi: UBI device description object
1699 * @pnum: the physical eraseblock number to check
1700 * @ec: the erase counter to check
1701 *
1702 * This function returns zero if the erase counter of physical eraseblock @pnum
Heiko Schocherff94bc42014-06-24 10:10:04 +02001703 * is equivalent to @ec, and a negative error code if not or if an error
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001704 * occurred.
1705 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001706static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001707{
1708 int err;
1709 long long read_ec;
1710 struct ubi_ec_hdr *ec_hdr;
1711
Heiko Schocherff94bc42014-06-24 10:10:04 +02001712 if (!ubi_dbg_chk_gen(ubi))
1713 return 0;
1714
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001715 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1716 if (!ec_hdr)
1717 return -ENOMEM;
1718
1719 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1720 if (err && err != UBI_IO_BITFLIPS) {
1721 /* The header does not have to exist */
1722 err = 0;
1723 goto out_free;
1724 }
1725
1726 read_ec = be64_to_cpu(ec_hdr->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001727 if (ec != read_ec && read_ec - ec > 1) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001728 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1729 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001730 dump_stack();
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001731 err = 1;
1732 } else
1733 err = 0;
1734
1735out_free:
1736 kfree(ec_hdr);
1737 return err;
1738}
1739
1740/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001741 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
1742 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001743 * @e: the wear-leveling entry to check
1744 * @root: the root of the tree
1745 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001746 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001747 * is not.
1748 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001749static int self_check_in_wl_tree(const struct ubi_device *ubi,
1750 struct ubi_wl_entry *e, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001751{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001752 if (!ubi_dbg_chk_gen(ubi))
1753 return 0;
1754
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001755 if (in_wl_tree(e, root))
1756 return 0;
1757
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001758 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001759 e->pnum, e->ec, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001760 dump_stack();
1761 return -EINVAL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001762}
1763
Heiko Schocherff94bc42014-06-24 10:10:04 +02001764/**
1765 * self_check_in_pq - check if wear-leveling entry is in the protection
1766 * queue.
1767 * @ubi: UBI device description object
1768 * @e: the wear-leveling entry to check
1769 *
1770 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
1771 */
1772static int self_check_in_pq(const struct ubi_device *ubi,
1773 struct ubi_wl_entry *e)
1774{
1775 struct ubi_wl_entry *p;
1776 int i;
1777
1778 if (!ubi_dbg_chk_gen(ubi))
1779 return 0;
1780
1781 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1782 list_for_each_entry(p, &ubi->pq[i], u.list)
1783 if (p == e)
1784 return 0;
1785
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001786 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001787 e->pnum, e->ec);
1788 dump_stack();
1789 return -EINVAL;
1790}
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001791#ifndef CONFIG_MTD_UBI_FASTMAP
1792static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
1793{
1794 struct ubi_wl_entry *e;
1795
1796 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1797 self_check_in_wl_tree(ubi, e, &ubi->free);
1798 ubi->free_count--;
1799 ubi_assert(ubi->free_count >= 0);
1800 rb_erase(&e->u.rb, &ubi->free);
1801
1802 return e;
1803}
1804
1805/**
1806 * produce_free_peb - produce a free physical eraseblock.
1807 * @ubi: UBI device description object
1808 *
1809 * This function tries to make a free PEB by means of synchronous execution of
1810 * pending works. This may be needed if, for example the background thread is
1811 * disabled. Returns zero in case of success and a negative error code in case
1812 * of failure.
1813 */
1814static int produce_free_peb(struct ubi_device *ubi)
1815{
1816 int err;
1817
1818 while (!ubi->free.rb_node && ubi->works_count) {
1819 spin_unlock(&ubi->wl_lock);
1820
1821 dbg_wl("do one work synchronously");
1822 err = do_work(ubi);
1823
1824 spin_lock(&ubi->wl_lock);
1825 if (err)
1826 return err;
1827 }
1828
1829 return 0;
1830}
1831
1832/**
1833 * ubi_wl_get_peb - get a physical eraseblock.
1834 * @ubi: UBI device description object
1835 *
1836 * This function returns a physical eraseblock in case of success and a
1837 * negative error code in case of failure.
1838 * Returns with ubi->fm_eba_sem held in read mode!
1839 */
1840int ubi_wl_get_peb(struct ubi_device *ubi)
1841{
1842 int err;
1843 struct ubi_wl_entry *e;
1844
1845retry:
1846 down_read(&ubi->fm_eba_sem);
1847 spin_lock(&ubi->wl_lock);
1848 if (!ubi->free.rb_node) {
1849 if (ubi->works_count == 0) {
1850 ubi_err(ubi, "no free eraseblocks");
1851 ubi_assert(list_empty(&ubi->works));
1852 spin_unlock(&ubi->wl_lock);
1853 return -ENOSPC;
1854 }
1855
1856 err = produce_free_peb(ubi);
1857 if (err < 0) {
1858 spin_unlock(&ubi->wl_lock);
1859 return err;
1860 }
1861 spin_unlock(&ubi->wl_lock);
1862 up_read(&ubi->fm_eba_sem);
1863 goto retry;
1864
1865 }
1866 e = wl_get_wle(ubi);
1867 prot_queue_add(ubi, e);
1868 spin_unlock(&ubi->wl_lock);
1869
1870 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
1871 ubi->peb_size - ubi->vid_hdr_aloffset);
1872 if (err) {
1873 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
1874 return err;
1875 }
1876
1877 return e->pnum;
1878}
1879#else
1880#include "fastmap-wl.c"
1881#endif