blob: 89ca90feb342678562b2c286ebd5b4de6dca0d08 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkc91a7192008-11-19 16:28:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 *
Kyungmin Parkc91a7192008-11-19 16:28:06 +01005 * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner
6 */
7
8/*
Heiko Schocherff94bc42014-06-24 10:10:04 +02009 * UBI wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010010 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020011 * This sub-system is responsible for wear-leveling. It works in terms of
12 * physical eraseblocks and erase counters and knows nothing about logical
13 * eraseblocks, volumes, etc. From this sub-system's perspective all physical
14 * eraseblocks are of two types - used and free. Used physical eraseblocks are
15 * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical
16 * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010017 *
18 * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter
Heiko Schocherff94bc42014-06-24 10:10:04 +020019 * header. The rest of the physical eraseblock contains only %0xFF bytes.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010020 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020021 * When physical eraseblocks are returned to the WL sub-system by means of the
Kyungmin Parkc91a7192008-11-19 16:28:06 +010022 * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is
23 * done asynchronously in context of the per-UBI device background thread,
Heiko Schocherff94bc42014-06-24 10:10:04 +020024 * which is also managed by the WL sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010025 *
26 * The wear-leveling is ensured by means of moving the contents of used
27 * physical eraseblocks with low erase counter to free physical eraseblocks
28 * with high erase counter.
29 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020030 * If the WL sub-system fails to erase a physical eraseblock, it marks it as
31 * bad.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010032 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020033 * This sub-system is also responsible for scrubbing. If a bit-flip is detected
34 * in a physical eraseblock, it has to be moved. Technically this is the same
35 * as moving it for wear-leveling reasons.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010036 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020037 * As it was said, for the UBI sub-system all physical eraseblocks are either
38 * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while
39 * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub
40 * RB-trees, as well as (temporarily) in the @wl->pq queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010041 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020042 * When the WL sub-system returns a physical eraseblock, the physical
43 * eraseblock is protected from being moved for some "time". For this reason,
44 * the physical eraseblock is not directly moved from the @wl->free tree to the
45 * @wl->used tree. There is a protection queue in between where this
46 * physical eraseblock is temporarily stored (@wl->pq).
47 *
48 * All this protection stuff is needed because:
49 * o we don't want to move physical eraseblocks just after we have given them
50 * to the user; instead, we first want to let users fill them up with data;
51 *
52 * o there is a chance that the user will put the physical eraseblock very
53 * soon, so it makes sense not to move it for some time, but wait.
54 *
55 * Physical eraseblocks stay protected only for limited time. But the "time" is
56 * measured in erase cycles in this case. This is implemented with help of the
57 * protection queue. Eraseblocks are put to the tail of this queue when they
58 * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the
59 * head of the queue on each erase operation (for any eraseblock). So the
60 * length of the queue defines how may (global) erase cycles PEBs are protected.
61 *
62 * To put it differently, each physical eraseblock has 2 main states: free and
63 * used. The former state corresponds to the @wl->free tree. The latter state
64 * is split up on several sub-states:
65 * o the WL movement is allowed (@wl->used tree);
66 * o the WL movement is disallowed (@wl->erroneous) because the PEB is
67 * erroneous - e.g., there was a read error;
68 * o the WL movement is temporarily prohibited (@wl->pq queue);
69 * o scrubbing is needed (@wl->scrub tree).
70 *
71 * Depending on the sub-state, wear-leveling entries of the used physical
72 * eraseblocks may be kept in one of those structures.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010073 *
74 * Note, in this implementation, we keep a small in-RAM object for each physical
75 * eraseblock. This is surely not a scalable solution. But it appears to be good
76 * enough for moderately large flashes and it is simple. In future, one may
Heiko Schocherff94bc42014-06-24 10:10:04 +020077 * re-work this sub-system and make it more scalable.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010078 *
Heiko Schocherff94bc42014-06-24 10:10:04 +020079 * At the moment this sub-system does not utilize the sequence number, which
80 * was introduced relatively recently. But it would be wise to do this because
81 * the sequence number of a logical eraseblock characterizes how old is it. For
Kyungmin Parkc91a7192008-11-19 16:28:06 +010082 * example, when we move a PEB with low erase counter, and we need to pick the
83 * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we
84 * pick target PEB with an average EC if our PEB is not very "old". This is a
Heiko Schocherff94bc42014-06-24 10:10:04 +020085 * room for future re-works of the WL sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +010086 */
87
Heiko Schocherff94bc42014-06-24 10:10:04 +020088#ifndef __UBOOT__
Kyungmin Parkc91a7192008-11-19 16:28:06 +010089#include <linux/slab.h>
90#include <linux/crc32.h>
91#include <linux/freezer.h>
92#include <linux/kthread.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020093#else
94#include <ubi_uboot.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010095#endif
96
Kyungmin Parkc91a7192008-11-19 16:28:06 +010097#include "ubi.h"
Heiko Schocher0195a7b2015-10-22 06:19:21 +020098#include "wl.h"
Kyungmin Parkc91a7192008-11-19 16:28:06 +010099
100/* Number of physical eraseblocks reserved for wear-leveling purposes */
101#define WL_RESERVED_PEBS 1
102
103/*
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100104 * Maximum difference between two erase counters. If this threshold is
Heiko Schocherff94bc42014-06-24 10:10:04 +0200105 * exceeded, the WL sub-system starts moving data from used physical
106 * eraseblocks with low erase counter to free physical eraseblocks with high
107 * erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100108 */
109#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
110
111/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200112 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100113 * physical eraseblock to move to. The simplest way would be just to pick the
114 * one with the highest erase counter. But in certain workloads this could lead
115 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
116 * situation when the picked physical eraseblock is constantly erased after the
117 * data is written to it. So, we have a constant which limits the highest erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200118 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
119 * does not pick eraseblocks with erase counter greater than the lowest erase
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100120 * counter plus %WL_FREE_MAX_DIFF.
121 */
122#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
123
124/*
125 * Maximum number of consecutive background thread failures which is enough to
126 * switch to read-only mode.
127 */
128#define WL_MAX_FAILURES 32
129
Heiko Schocherff94bc42014-06-24 10:10:04 +0200130static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
131static int self_check_in_wl_tree(const struct ubi_device *ubi,
132 struct ubi_wl_entry *e, struct rb_root *root);
133static int self_check_in_pq(const struct ubi_device *ubi,
134 struct ubi_wl_entry *e);
135
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100136/**
137 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
138 * @e: the wear-leveling entry to add
139 * @root: the root of the tree
140 *
141 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
142 * the @ubi->used and @ubi->free RB-trees.
143 */
144static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
145{
146 struct rb_node **p, *parent = NULL;
147
148 p = &root->rb_node;
149 while (*p) {
150 struct ubi_wl_entry *e1;
151
152 parent = *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200153 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100154
155 if (e->ec < e1->ec)
156 p = &(*p)->rb_left;
157 else if (e->ec > e1->ec)
158 p = &(*p)->rb_right;
159 else {
160 ubi_assert(e->pnum != e1->pnum);
161 if (e->pnum < e1->pnum)
162 p = &(*p)->rb_left;
163 else
164 p = &(*p)->rb_right;
165 }
166 }
167
Heiko Schocherff94bc42014-06-24 10:10:04 +0200168 rb_link_node(&e->u.rb, parent, p);
169 rb_insert_color(&e->u.rb, root);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100170}
171
172/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200173 * wl_tree_destroy - destroy a wear-leveling entry.
174 * @ubi: UBI device description object
175 * @e: the wear-leveling entry to add
176 *
177 * This function destroys a wear leveling entry and removes
178 * the reference from the lookup table.
179 */
180static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
181{
182 ubi->lookuptbl[e->pnum] = NULL;
183 kmem_cache_free(ubi_wl_entry_slab, e);
184}
185
186/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100187 * do_work - do one pending work.
188 * @ubi: UBI device description object
189 *
190 * This function returns zero in case of success and a negative error code in
191 * case of failure.
192 */
193static int do_work(struct ubi_device *ubi)
194{
195 int err;
196 struct ubi_work *wrk;
197
198 cond_resched();
199
200 /*
201 * @ubi->work_sem is used to synchronize with the workers. Workers take
202 * it in read mode, so many of them may be doing works at a time. But
203 * the queue flush code has to be sure the whole queue of works is
204 * done, and it takes the mutex in write mode.
205 */
206 down_read(&ubi->work_sem);
207 spin_lock(&ubi->wl_lock);
208 if (list_empty(&ubi->works)) {
209 spin_unlock(&ubi->wl_lock);
210 up_read(&ubi->work_sem);
211 return 0;
212 }
213
214 wrk = list_entry(ubi->works.next, struct ubi_work, list);
215 list_del(&wrk->list);
216 ubi->works_count -= 1;
217 ubi_assert(ubi->works_count >= 0);
218 spin_unlock(&ubi->wl_lock);
219
220 /*
221 * Call the worker function. Do not touch the work structure
222 * after this call as it will have been freed or reused by that
223 * time by the worker function.
224 */
225 err = wrk->func(ubi, wrk, 0);
226 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200227 ubi_err(ubi, "work failed with error code %d", err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100228 up_read(&ubi->work_sem);
229
230 return err;
231}
232
233/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100234 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
235 * @e: the wear-leveling entry to check
236 * @root: the root of the tree
237 *
238 * This function returns non-zero if @e is in the @root RB-tree and zero if it
239 * is not.
240 */
241static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
242{
243 struct rb_node *p;
244
245 p = root->rb_node;
246 while (p) {
247 struct ubi_wl_entry *e1;
248
Heiko Schocherff94bc42014-06-24 10:10:04 +0200249 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100250
251 if (e->pnum == e1->pnum) {
252 ubi_assert(e == e1);
253 return 1;
254 }
255
256 if (e->ec < e1->ec)
257 p = p->rb_left;
258 else if (e->ec > e1->ec)
259 p = p->rb_right;
260 else {
261 ubi_assert(e->pnum != e1->pnum);
262 if (e->pnum < e1->pnum)
263 p = p->rb_left;
264 else
265 p = p->rb_right;
266 }
267 }
268
269 return 0;
270}
271
272/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200273 * prot_queue_add - add physical eraseblock to the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100274 * @ubi: UBI device description object
275 * @e: the physical eraseblock to add
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100276 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200277 * This function adds @e to the tail of the protection queue @ubi->pq, where
278 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
279 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
280 * be locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100281 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200282static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100283{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200284 int pq_tail = ubi->pq_head - 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100285
Heiko Schocherff94bc42014-06-24 10:10:04 +0200286 if (pq_tail < 0)
287 pq_tail = UBI_PROT_QUEUE_LEN - 1;
288 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
289 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
290 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100291}
292
293/**
294 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200295 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100296 * @root: the RB-tree where to look for
Heiko Schocherff94bc42014-06-24 10:10:04 +0200297 * @diff: maximum possible difference from the smallest erase counter
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100298 *
299 * This function looks for a wear leveling entry with erase counter closest to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200300 * min + @diff, where min is the smallest erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100301 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200302static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
303 struct rb_root *root, int diff)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100304{
305 struct rb_node *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200306 struct ubi_wl_entry *e, *prev_e = NULL;
307 int max;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100308
Heiko Schocherff94bc42014-06-24 10:10:04 +0200309 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
310 max = e->ec + diff;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100311
312 p = root->rb_node;
313 while (p) {
314 struct ubi_wl_entry *e1;
315
Heiko Schocherff94bc42014-06-24 10:10:04 +0200316 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100317 if (e1->ec >= max)
318 p = p->rb_left;
319 else {
320 p = p->rb_right;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200321 prev_e = e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100322 e = e1;
323 }
324 }
325
Heiko Schocherff94bc42014-06-24 10:10:04 +0200326 /* If no fastmap has been written and this WL entry can be used
327 * as anchor PEB, hold it back and return the second best WL entry
328 * such that fastmap can use the anchor PEB later. */
329 if (prev_e && !ubi->fm_disabled &&
330 !ubi->fm && e->pnum < UBI_FM_MAX_START)
331 return prev_e;
332
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100333 return e;
334}
335
336/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337 * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100338 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200339 * @root: the RB-tree where to look for
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100340 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200341 * This function looks for a wear leveling entry with medium erase counter,
342 * but not greater or equivalent than the lowest erase counter plus
343 * %WL_FREE_MAX_DIFF/2.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100344 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200345static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
346 struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100347{
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100348 struct ubi_wl_entry *e, *first, *last;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100349
Heiko Schocherff94bc42014-06-24 10:10:04 +0200350 first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
351 last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100352
Heiko Schocherff94bc42014-06-24 10:10:04 +0200353 if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
354 e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100355
Heiko Schocherff94bc42014-06-24 10:10:04 +0200356 /* If no fastmap has been written and this WL entry can be used
357 * as anchor PEB, hold it back and return the second best
358 * WL entry such that fastmap can use the anchor PEB later. */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200359 e = may_reserve_for_fm(ubi, e, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200360 } else
361 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
362
363 return e;
364}
365
Heiko Schocherff94bc42014-06-24 10:10:04 +0200366/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200367 * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or
368 * refill_wl_user_pool().
Heiko Schocherff94bc42014-06-24 10:10:04 +0200369 * @ubi: UBI device description object
370 *
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200371 * This function returns a a wear leveling entry in case of success and
372 * NULL in case of failure.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200373 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200374static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200375{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200376 struct ubi_wl_entry *e;
377
Heiko Schocherff94bc42014-06-24 10:10:04 +0200378 e = find_mean_wl_entry(ubi, &ubi->free);
379 if (!e) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200380 ubi_err(ubi, "no free eraseblocks");
381 return NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200382 }
383
384 self_check_in_wl_tree(ubi, e, &ubi->free);
385
386 /*
387 * Move the physical eraseblock to the protection queue where it will
388 * be protected from being moved for some time.
389 */
390 rb_erase(&e->u.rb, &ubi->free);
391 ubi->free_count--;
392 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200393
394 return e;
395}
396
Heiko Schocherff94bc42014-06-24 10:10:04 +0200397/**
398 * prot_queue_del - remove a physical eraseblock from the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100399 * @ubi: UBI device description object
400 * @pnum: the physical eraseblock to remove
401 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200402 * This function deletes PEB @pnum from the protection queue and returns zero
403 * in case of success and %-ENODEV if the PEB was not found.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100404 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200405static int prot_queue_del(struct ubi_device *ubi, int pnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100406{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200407 struct ubi_wl_entry *e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100408
Heiko Schocherff94bc42014-06-24 10:10:04 +0200409 e = ubi->lookuptbl[pnum];
410 if (!e)
411 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100412
Heiko Schocherff94bc42014-06-24 10:10:04 +0200413 if (self_check_in_pq(ubi, e))
414 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100415
Heiko Schocherff94bc42014-06-24 10:10:04 +0200416 list_del(&e->u.list);
417 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100418 return 0;
419}
420
421/**
422 * sync_erase - synchronously erase a physical eraseblock.
423 * @ubi: UBI device description object
424 * @e: the the physical eraseblock to erase
425 * @torture: if the physical eraseblock has to be tortured
426 *
427 * This function returns zero in case of success and a negative error code in
428 * case of failure.
429 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200430static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
431 int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100432{
433 int err;
434 struct ubi_ec_hdr *ec_hdr;
435 unsigned long long ec = e->ec;
436
437 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
438
Heiko Schocherff94bc42014-06-24 10:10:04 +0200439 err = self_check_ec(ubi, e->pnum, e->ec);
440 if (err)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100441 return -EINVAL;
442
443 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
444 if (!ec_hdr)
445 return -ENOMEM;
446
447 err = ubi_io_sync_erase(ubi, e->pnum, torture);
448 if (err < 0)
449 goto out_free;
450
451 ec += err;
452 if (ec > UBI_MAX_ERASECOUNTER) {
453 /*
454 * Erase counter overflow. Upgrade UBI and use 64-bit
455 * erase counters internally.
456 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200457 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100458 e->pnum, ec);
459 err = -EINVAL;
460 goto out_free;
461 }
462
463 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
464
465 ec_hdr->ec = cpu_to_be64(ec);
466
467 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
468 if (err)
469 goto out_free;
470
471 e->ec = ec;
472 spin_lock(&ubi->wl_lock);
473 if (e->ec > ubi->max_ec)
474 ubi->max_ec = e->ec;
475 spin_unlock(&ubi->wl_lock);
476
477out_free:
478 kfree(ec_hdr);
479 return err;
480}
481
482/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200483 * serve_prot_queue - check if it is time to stop protecting PEBs.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100484 * @ubi: UBI device description object
485 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200486 * This function is called after each erase operation and removes PEBs from the
487 * tail of the protection queue. These PEBs have been protected for long enough
488 * and should be moved to the used tree.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100489 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200490static void serve_prot_queue(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100491{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200492 struct ubi_wl_entry *e, *tmp;
493 int count;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100494
495 /*
496 * There may be several protected physical eraseblock to remove,
497 * process them all.
498 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200499repeat:
500 count = 0;
501 spin_lock(&ubi->wl_lock);
502 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
503 dbg_wl("PEB %d EC %d protection over, move to used tree",
504 e->pnum, e->ec);
505
506 list_del(&e->u.list);
507 wl_tree_add(e, &ubi->used);
508 if (count++ > 32) {
509 /*
510 * Let's be nice and avoid holding the spinlock for
511 * too long.
512 */
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100513 spin_unlock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200514 cond_resched();
515 goto repeat;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100516 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100517 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200518
519 ubi->pq_head += 1;
520 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
521 ubi->pq_head = 0;
522 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
523 spin_unlock(&ubi->wl_lock);
524}
525
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100526#ifdef __UBOOT__
527void ubi_do_worker(struct ubi_device *ubi)
528{
529 int err;
530
531 if (list_empty(&ubi->works) || ubi->ro_mode ||
532 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi))
533 return;
534
535 spin_lock(&ubi->wl_lock);
536 while (!list_empty(&ubi->works)) {
537 /*
538 * call do_work, which executes exactly one work form the queue,
539 * including removeing it from the work queue.
540 */
541 spin_unlock(&ubi->wl_lock);
542 err = do_work(ubi);
543 spin_lock(&ubi->wl_lock);
544 if (err) {
545 ubi_err(ubi, "%s: work failed with error code %d",
546 ubi->bgt_name, err);
547 }
548 }
549 spin_unlock(&ubi->wl_lock);
550}
551#endif
552
Heiko Schocherff94bc42014-06-24 10:10:04 +0200553/**
554 * __schedule_ubi_work - schedule a work.
555 * @ubi: UBI device description object
556 * @wrk: the work to schedule
557 *
558 * This function adds a work defined by @wrk to the tail of the pending works
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200559 * list. Can only be used if ubi->work_sem is already held in read mode!
Heiko Schocherff94bc42014-06-24 10:10:04 +0200560 */
561static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
562{
563 spin_lock(&ubi->wl_lock);
564 list_add_tail(&wrk->list, &ubi->works);
565 ubi_assert(ubi->works_count >= 0);
566 ubi->works_count += 1;
567#ifndef __UBOOT__
568 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
569 wake_up_process(ubi->bgt_thread);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200570#endif
571 spin_unlock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100572}
573
574/**
575 * schedule_ubi_work - schedule a work.
576 * @ubi: UBI device description object
577 * @wrk: the work to schedule
578 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200579 * This function adds a work defined by @wrk to the tail of the pending works
580 * list.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100581 */
582static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
583{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200584 down_read(&ubi->work_sem);
585 __schedule_ubi_work(ubi, wrk);
586 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100587}
588
589static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200590 int shutdown);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200591
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100592/**
593 * schedule_erase - schedule an erase work.
594 * @ubi: UBI device description object
595 * @e: the WL entry of the physical eraseblock to erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200596 * @vol_id: the volume ID that last used this PEB
597 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100598 * @torture: if the physical eraseblock has to be tortured
599 *
600 * This function returns zero in case of success and a %-ENOMEM in case of
601 * failure.
602 */
603static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200604 int vol_id, int lnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100605{
606 struct ubi_work *wl_wrk;
607
Heiko Schocherff94bc42014-06-24 10:10:04 +0200608 ubi_assert(e);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200609
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100610 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
611 e->pnum, e->ec, torture);
612
613 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
614 if (!wl_wrk)
615 return -ENOMEM;
616
617 wl_wrk->func = &erase_worker;
618 wl_wrk->e = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200619 wl_wrk->vol_id = vol_id;
620 wl_wrk->lnum = lnum;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100621 wl_wrk->torture = torture;
622
623 schedule_ubi_work(ubi, wl_wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100624
625#ifdef __UBOOT__
626 ubi_do_worker(ubi);
627#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100628 return 0;
629}
630
631/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200632 * do_sync_erase - run the erase worker synchronously.
633 * @ubi: UBI device description object
634 * @e: the WL entry of the physical eraseblock to erase
635 * @vol_id: the volume ID that last used this PEB
636 * @lnum: the last used logical eraseblock number for the PEB
637 * @torture: if the physical eraseblock has to be tortured
638 *
639 */
640static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
641 int vol_id, int lnum, int torture)
642{
643 struct ubi_work *wl_wrk;
644
645 dbg_wl("sync erase of PEB %i", e->pnum);
646
647 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
648 if (!wl_wrk)
649 return -ENOMEM;
650
651 wl_wrk->e = e;
652 wl_wrk->vol_id = vol_id;
653 wl_wrk->lnum = lnum;
654 wl_wrk->torture = torture;
655
656 return erase_worker(ubi, wl_wrk, 0);
657}
658
Heiko Schocherff94bc42014-06-24 10:10:04 +0200659/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100660 * wear_leveling_worker - wear-leveling worker function.
661 * @ubi: UBI device description object
662 * @wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200663 * @shutdown: non-zero if the worker has to free memory and exit
664 * because the WL-subsystem is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100665 *
666 * This function copies a more worn out physical eraseblock to a less worn out
667 * one. Returns zero in case of success and a negative error code in case of
668 * failure.
669 */
670static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200671 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100672{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200673 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200674 int vol_id = -1, lnum = -1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200675#ifdef CONFIG_MTD_UBI_FASTMAP
676 int anchor = wrk->anchor;
677#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100678 struct ubi_wl_entry *e1, *e2;
679 struct ubi_vid_hdr *vid_hdr;
680
681 kfree(wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200682 if (shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100683 return 0;
684
685 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
686 if (!vid_hdr)
687 return -ENOMEM;
688
689 mutex_lock(&ubi->move_mutex);
690 spin_lock(&ubi->wl_lock);
691 ubi_assert(!ubi->move_from && !ubi->move_to);
692 ubi_assert(!ubi->move_to_put);
693
694 if (!ubi->free.rb_node ||
695 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
696 /*
697 * No free physical eraseblocks? Well, they must be waiting in
698 * the queue to be erased. Cancel movement - it will be
699 * triggered again when a free physical eraseblock appears.
700 *
701 * No used physical eraseblocks? They must be temporarily
702 * protected from being moved. They will be moved to the
703 * @ubi->used tree later and the wear-leveling will be
704 * triggered again.
705 */
706 dbg_wl("cancel WL, a list is empty: free %d, used %d",
707 !ubi->free.rb_node, !ubi->used.rb_node);
708 goto out_cancel;
709 }
710
Heiko Schocherff94bc42014-06-24 10:10:04 +0200711#ifdef CONFIG_MTD_UBI_FASTMAP
712 /* Check whether we need to produce an anchor PEB */
713 if (!anchor)
714 anchor = !anchor_pebs_avalible(&ubi->free);
715
716 if (anchor) {
717 e1 = find_anchor_wl_entry(&ubi->used);
718 if (!e1)
719 goto out_cancel;
720 e2 = get_peb_for_wl(ubi);
721 if (!e2)
722 goto out_cancel;
723
724 self_check_in_wl_tree(ubi, e1, &ubi->used);
725 rb_erase(&e1->u.rb, &ubi->used);
726 dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
727 } else if (!ubi->scrub.rb_node) {
728#else
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100729 if (!ubi->scrub.rb_node) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200730#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100731 /*
732 * Now pick the least worn-out used physical eraseblock and a
733 * highly worn-out free physical eraseblock. If the erase
734 * counters differ much enough, start wear-leveling.
735 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200736 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
737 e2 = get_peb_for_wl(ubi);
738 if (!e2)
739 goto out_cancel;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100740
741 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
742 dbg_wl("no WL needed: min used EC %d, max free EC %d",
743 e1->ec, e2->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200744
745 /* Give the unused PEB back */
746 wl_tree_add(e2, &ubi->free);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200747 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100748 goto out_cancel;
749 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200750 self_check_in_wl_tree(ubi, e1, &ubi->used);
751 rb_erase(&e1->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100752 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
753 e1->pnum, e1->ec, e2->pnum, e2->ec);
754 } else {
755 /* Perform scrubbing */
756 scrubbing = 1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200757 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
758 e2 = get_peb_for_wl(ubi);
759 if (!e2)
760 goto out_cancel;
761
762 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
763 rb_erase(&e1->u.rb, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100764 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
765 }
766
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100767 ubi->move_from = e1;
768 ubi->move_to = e2;
769 spin_unlock(&ubi->wl_lock);
770
771 /*
772 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
773 * We so far do not know which logical eraseblock our physical
774 * eraseblock (@e1) belongs to. We have to read the volume identifier
775 * header first.
776 *
777 * Note, we are protected from this PEB being unmapped and erased. The
778 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
779 * which is being moved was unmapped.
780 */
781
782 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
783 if (err && err != UBI_IO_BITFLIPS) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200784 if (err == UBI_IO_FF) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100785 /*
786 * We are trying to move PEB without a VID header. UBI
787 * always write VID headers shortly after the PEB was
Heiko Schocherff94bc42014-06-24 10:10:04 +0200788 * given, so we have a situation when it has not yet
789 * had a chance to write it, because it was preempted.
790 * So add this PEB to the protection queue so far,
791 * because presumably more data will be written there
792 * (including the missing VID header), and then we'll
793 * move it.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100794 */
795 dbg_wl("PEB %d has no VID header", e1->pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200796 protect = 1;
797 goto out_not_moved;
798 } else if (err == UBI_IO_FF_BITFLIPS) {
799 /*
800 * The same situation as %UBI_IO_FF, but bit-flips were
801 * detected. It is better to schedule this PEB for
802 * scrubbing.
803 */
804 dbg_wl("PEB %d has no VID header but has bit-flips",
805 e1->pnum);
806 scrubbing = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100807 goto out_not_moved;
808 }
809
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200810 ubi_err(ubi, "error %d while reading VID header from PEB %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100811 err, e1->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100812 goto out_error;
813 }
814
Heiko Schocherff94bc42014-06-24 10:10:04 +0200815 vol_id = be32_to_cpu(vid_hdr->vol_id);
816 lnum = be32_to_cpu(vid_hdr->lnum);
817
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100818 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
819 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200820 if (err == MOVE_CANCEL_RACE) {
821 /*
822 * The LEB has not been moved because the volume is
823 * being deleted or the PEB has been put meanwhile. We
824 * should prevent this PEB from being selected for
825 * wear-leveling movement again, so put it to the
826 * protection queue.
827 */
828 protect = 1;
829 goto out_not_moved;
830 }
831 if (err == MOVE_RETRY) {
832 scrubbing = 1;
833 goto out_not_moved;
834 }
835 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
836 err == MOVE_TARGET_RD_ERR) {
837 /*
838 * Target PEB had bit-flips or write error - torture it.
839 */
840 torture = 1;
841 goto out_not_moved;
842 }
843
844 if (err == MOVE_SOURCE_RD_ERR) {
845 /*
846 * An error happened while reading the source PEB. Do
847 * not switch to R/O mode in this case, and give the
848 * upper layers a possibility to recover from this,
849 * e.g. by unmapping corresponding LEB. Instead, just
850 * put this PEB to the @ubi->erroneous list to prevent
851 * UBI from trying to move it over and over again.
852 */
853 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200854 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200855 ubi->erroneous_peb_count);
856 goto out_error;
857 }
858 erroneous = 1;
859 goto out_not_moved;
860 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100861
862 if (err < 0)
863 goto out_error;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100864
Heiko Schocherff94bc42014-06-24 10:10:04 +0200865 ubi_assert(0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100866 }
867
Heiko Schocherff94bc42014-06-24 10:10:04 +0200868 /* The PEB has been successfully moved */
869 if (scrubbing)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200870 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200871 e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100872 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200873
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100874 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200875 if (!ubi->move_to_put) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100876 wl_tree_add(e2, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200877 e2 = NULL;
878 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100879 ubi->move_from = ubi->move_to = NULL;
880 ubi->move_to_put = ubi->wl_scheduled = 0;
881 spin_unlock(&ubi->wl_lock);
882
Heiko Schocherff94bc42014-06-24 10:10:04 +0200883 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
884 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200885 if (e2)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200886 wl_entry_destroy(ubi, e2);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200887 goto out_ro;
888 }
889
890 if (e2) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100891 /*
892 * Well, the target PEB was put meanwhile, schedule it for
893 * erasure.
894 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200895 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
896 e2->pnum, vol_id, lnum);
897 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200898 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200899 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100900 }
901
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100902 dbg_wl("done");
903 mutex_unlock(&ubi->move_mutex);
904 return 0;
905
906 /*
907 * For some reasons the LEB was not moved, might be an error, might be
908 * something else. @e1 was not changed, so return it back. @e2 might
Heiko Schocherff94bc42014-06-24 10:10:04 +0200909 * have been changed, schedule it for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100910 */
911out_not_moved:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200912 if (vol_id != -1)
913 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
914 e1->pnum, vol_id, lnum, e2->pnum, err);
915 else
916 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
917 e1->pnum, e2->pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100918 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200919 if (protect)
920 prot_queue_add(ubi, e1);
921 else if (erroneous) {
922 wl_tree_add(e1, &ubi->erroneous);
923 ubi->erroneous_peb_count += 1;
924 } else if (scrubbing)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100925 wl_tree_add(e1, &ubi->scrub);
926 else
927 wl_tree_add(e1, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200928 ubi_assert(!ubi->move_to_put);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100929 ubi->move_from = ubi->move_to = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200930 ubi->wl_scheduled = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100931 spin_unlock(&ubi->wl_lock);
932
Heiko Schocherff94bc42014-06-24 10:10:04 +0200933 ubi_free_vid_hdr(ubi, vid_hdr);
934 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200935 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200936 goto out_ro;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200937
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100938 mutex_unlock(&ubi->move_mutex);
939 return 0;
940
941out_error:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200942 if (vol_id != -1)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200943 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200944 err, e1->pnum, e2->pnum);
945 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200946 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200947 err, e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100948 spin_lock(&ubi->wl_lock);
949 ubi->move_from = ubi->move_to = NULL;
950 ubi->move_to_put = ubi->wl_scheduled = 0;
951 spin_unlock(&ubi->wl_lock);
952
Heiko Schocherff94bc42014-06-24 10:10:04 +0200953 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200954 wl_entry_destroy(ubi, e1);
955 wl_entry_destroy(ubi, e2);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100956
Heiko Schocherff94bc42014-06-24 10:10:04 +0200957out_ro:
958 ubi_ro_mode(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100959 mutex_unlock(&ubi->move_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200960 ubi_assert(err != 0);
961 return err < 0 ? err : -EIO;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100962
963out_cancel:
964 ubi->wl_scheduled = 0;
965 spin_unlock(&ubi->wl_lock);
966 mutex_unlock(&ubi->move_mutex);
967 ubi_free_vid_hdr(ubi, vid_hdr);
968 return 0;
969}
970
971/**
972 * ensure_wear_leveling - schedule wear-leveling if it is needed.
973 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200974 * @nested: set to non-zero if this function is called from UBI worker
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100975 *
976 * This function checks if it is time to start wear-leveling and schedules it
977 * if yes. This function returns zero in case of success and a negative error
978 * code in case of failure.
979 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200980static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100981{
982 int err = 0;
983 struct ubi_wl_entry *e1;
984 struct ubi_wl_entry *e2;
985 struct ubi_work *wrk;
986
987 spin_lock(&ubi->wl_lock);
988 if (ubi->wl_scheduled)
989 /* Wear-leveling is already in the work queue */
990 goto out_unlock;
991
992 /*
993 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
994 * the WL worker has to be scheduled anyway.
995 */
996 if (!ubi->scrub.rb_node) {
997 if (!ubi->used.rb_node || !ubi->free.rb_node)
998 /* No physical eraseblocks - no deal */
999 goto out_unlock;
1000
1001 /*
1002 * We schedule wear-leveling only if the difference between the
1003 * lowest erase counter of used physical eraseblocks and a high
Heiko Schocherff94bc42014-06-24 10:10:04 +02001004 * erase counter of free physical eraseblocks is greater than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001005 * %UBI_WL_THRESHOLD.
1006 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001007 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1008 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001009
1010 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
1011 goto out_unlock;
1012 dbg_wl("schedule wear-leveling");
1013 } else
1014 dbg_wl("schedule scrubbing");
1015
1016 ubi->wl_scheduled = 1;
1017 spin_unlock(&ubi->wl_lock);
1018
1019 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1020 if (!wrk) {
1021 err = -ENOMEM;
1022 goto out_cancel;
1023 }
1024
Heiko Schocherff94bc42014-06-24 10:10:04 +02001025 wrk->anchor = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001026 wrk->func = &wear_leveling_worker;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001027 if (nested)
1028 __schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001029#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +02001030 else
1031 schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001032#else
1033 else {
1034 schedule_ubi_work(ubi, wrk);
1035 ubi_do_worker(ubi);
1036 }
1037#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001038 return err;
1039
1040out_cancel:
1041 spin_lock(&ubi->wl_lock);
1042 ubi->wl_scheduled = 0;
1043out_unlock:
1044 spin_unlock(&ubi->wl_lock);
1045 return err;
1046}
1047
1048/**
1049 * erase_worker - physical eraseblock erase worker function.
1050 * @ubi: UBI device description object
1051 * @wl_wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001052 * @shutdown: non-zero if the worker has to free memory and exit
1053 * because the WL sub-system is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001054 *
1055 * This function erases a physical eraseblock and perform torture testing if
1056 * needed. It also takes care about marking the physical eraseblock bad if
1057 * needed. Returns zero in case of success and a negative error code in case of
1058 * failure.
1059 */
1060static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001061 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001062{
1063 struct ubi_wl_entry *e = wl_wrk->e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001064 int pnum = e->pnum;
1065 int vol_id = wl_wrk->vol_id;
1066 int lnum = wl_wrk->lnum;
1067 int err, available_consumed = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001068
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001069 if (shutdown) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001070 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1071 kfree(wl_wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001072 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001073 return 0;
1074 }
1075
Heiko Schocherff94bc42014-06-24 10:10:04 +02001076 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1077 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1078
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001079 err = sync_erase(ubi, e, wl_wrk->torture);
1080 if (!err) {
1081 /* Fine, we've erased it successfully */
1082 kfree(wl_wrk);
1083
1084 spin_lock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001085 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001086 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001087 spin_unlock(&ubi->wl_lock);
1088
1089 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001090 * One more erase operation has happened, take care about
1091 * protected physical eraseblocks.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001092 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001093 serve_prot_queue(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001094
1095 /* And take care about wear-leveling */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001096 err = ensure_wear_leveling(ubi, 1);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001097 return err;
1098 }
1099
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001100 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001101 kfree(wl_wrk);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001102
1103 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1104 err == -EBUSY) {
1105 int err1;
1106
1107 /* Re-schedule the LEB for erasure */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001108 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001109 if (err1) {
1110 err = err1;
1111 goto out_ro;
1112 }
1113 return err;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001114 }
1115
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001116 wl_entry_destroy(ubi, e);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001117 if (err != -EIO)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001118 /*
1119 * If this is not %-EIO, we have no idea what to do. Scheduling
1120 * this physical eraseblock for erasure again would cause
Heiko Schocherff94bc42014-06-24 10:10:04 +02001121 * errors again and again. Well, lets switch to R/O mode.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001122 */
1123 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001124
1125 /* It is %-EIO, the PEB went bad */
1126
1127 if (!ubi->bad_allowed) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001128 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001129 goto out_ro;
1130 }
1131
1132 spin_lock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001133 if (ubi->beb_rsvd_pebs == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001134 if (ubi->avail_pebs == 0) {
1135 spin_unlock(&ubi->volumes_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001136 ubi_err(ubi, "no reserved/available physical eraseblocks");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001137 goto out_ro;
1138 }
1139 ubi->avail_pebs -= 1;
1140 available_consumed = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001141 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001142 spin_unlock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001143
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001144 ubi_msg(ubi, "mark PEB %d as bad", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001145 err = ubi_io_mark_bad(ubi, pnum);
1146 if (err)
1147 goto out_ro;
1148
1149 spin_lock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001150 if (ubi->beb_rsvd_pebs > 0) {
1151 if (available_consumed) {
1152 /*
1153 * The amount of reserved PEBs increased since we last
1154 * checked.
1155 */
1156 ubi->avail_pebs += 1;
1157 available_consumed = 0;
1158 }
1159 ubi->beb_rsvd_pebs -= 1;
1160 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001161 ubi->bad_peb_count += 1;
1162 ubi->good_peb_count -= 1;
1163 ubi_calculate_reserved(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001164 if (available_consumed)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001165 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001166 else if (ubi->beb_rsvd_pebs)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001167 ubi_msg(ubi, "%d PEBs left in the reserve",
1168 ubi->beb_rsvd_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001169 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001170 ubi_warn(ubi, "last PEB from the reserve was used");
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001171 spin_unlock(&ubi->volumes_lock);
1172
1173 return err;
1174
1175out_ro:
Heiko Schocherff94bc42014-06-24 10:10:04 +02001176 if (available_consumed) {
1177 spin_lock(&ubi->volumes_lock);
1178 ubi->avail_pebs += 1;
1179 spin_unlock(&ubi->volumes_lock);
1180 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001181 ubi_ro_mode(ubi);
1182 return err;
1183}
1184
1185/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001186 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001187 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001188 * @vol_id: the volume ID that last used this PEB
1189 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001190 * @pnum: physical eraseblock to return
1191 * @torture: if this physical eraseblock has to be tortured
1192 *
1193 * This function is called to return physical eraseblock @pnum to the pool of
1194 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1195 * occurred to this @pnum and it has to be tested. This function returns zero
1196 * in case of success, and a negative error code in case of failure.
1197 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001198int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1199 int pnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001200{
1201 int err;
1202 struct ubi_wl_entry *e;
1203
1204 dbg_wl("PEB %d", pnum);
1205 ubi_assert(pnum >= 0);
1206 ubi_assert(pnum < ubi->peb_count);
1207
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001208 down_read(&ubi->fm_protect);
1209
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001210retry:
1211 spin_lock(&ubi->wl_lock);
1212 e = ubi->lookuptbl[pnum];
1213 if (e == ubi->move_from) {
1214 /*
1215 * User is putting the physical eraseblock which was selected to
1216 * be moved. It will be scheduled for erasure in the
1217 * wear-leveling worker.
1218 */
1219 dbg_wl("PEB %d is being moved, wait", pnum);
1220 spin_unlock(&ubi->wl_lock);
1221
1222 /* Wait for the WL worker by taking the @ubi->move_mutex */
1223 mutex_lock(&ubi->move_mutex);
1224 mutex_unlock(&ubi->move_mutex);
1225 goto retry;
1226 } else if (e == ubi->move_to) {
1227 /*
1228 * User is putting the physical eraseblock which was selected
1229 * as the target the data is moved to. It may happen if the EBA
Heiko Schocherff94bc42014-06-24 10:10:04 +02001230 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1231 * but the WL sub-system has not put the PEB to the "used" tree
1232 * yet, but it is about to do this. So we just set a flag which
1233 * will tell the WL worker that the PEB is not needed anymore
1234 * and should be scheduled for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001235 */
1236 dbg_wl("PEB %d is the target of data moving", pnum);
1237 ubi_assert(!ubi->move_to_put);
1238 ubi->move_to_put = 1;
1239 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001240 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001241 return 0;
1242 } else {
1243 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001244 self_check_in_wl_tree(ubi, e, &ubi->used);
1245 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001246 } else if (in_wl_tree(e, &ubi->scrub)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001247 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1248 rb_erase(&e->u.rb, &ubi->scrub);
1249 } else if (in_wl_tree(e, &ubi->erroneous)) {
1250 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1251 rb_erase(&e->u.rb, &ubi->erroneous);
1252 ubi->erroneous_peb_count -= 1;
1253 ubi_assert(ubi->erroneous_peb_count >= 0);
1254 /* Erroneous PEBs should be tortured */
1255 torture = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001256 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001257 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001258 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001259 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001260 ubi_ro_mode(ubi);
1261 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001262 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001263 return err;
1264 }
1265 }
1266 }
1267 spin_unlock(&ubi->wl_lock);
1268
Heiko Schocherff94bc42014-06-24 10:10:04 +02001269 err = schedule_erase(ubi, e, vol_id, lnum, torture);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001270 if (err) {
1271 spin_lock(&ubi->wl_lock);
1272 wl_tree_add(e, &ubi->used);
1273 spin_unlock(&ubi->wl_lock);
1274 }
1275
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001276 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001277 return err;
1278}
1279
1280/**
1281 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1282 * @ubi: UBI device description object
1283 * @pnum: the physical eraseblock to schedule
1284 *
1285 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1286 * needs scrubbing. This function schedules a physical eraseblock for
1287 * scrubbing which is done in background. This function returns zero in case of
1288 * success and a negative error code in case of failure.
1289 */
1290int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1291{
1292 struct ubi_wl_entry *e;
1293
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001294 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001295
1296retry:
1297 spin_lock(&ubi->wl_lock);
1298 e = ubi->lookuptbl[pnum];
Heiko Schocherff94bc42014-06-24 10:10:04 +02001299 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1300 in_wl_tree(e, &ubi->erroneous)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001301 spin_unlock(&ubi->wl_lock);
1302 return 0;
1303 }
1304
1305 if (e == ubi->move_to) {
1306 /*
1307 * This physical eraseblock was used to move data to. The data
1308 * was moved but the PEB was not yet inserted to the proper
1309 * tree. We should just wait a little and let the WL worker
1310 * proceed.
1311 */
1312 spin_unlock(&ubi->wl_lock);
1313 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1314 yield();
1315 goto retry;
1316 }
1317
1318 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001319 self_check_in_wl_tree(ubi, e, &ubi->used);
1320 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001321 } else {
1322 int err;
1323
Heiko Schocherff94bc42014-06-24 10:10:04 +02001324 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001325 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001326 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001327 ubi_ro_mode(ubi);
1328 spin_unlock(&ubi->wl_lock);
1329 return err;
1330 }
1331 }
1332
1333 wl_tree_add(e, &ubi->scrub);
1334 spin_unlock(&ubi->wl_lock);
1335
1336 /*
1337 * Technically scrubbing is the same as wear-leveling, so it is done
1338 * by the WL worker.
1339 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001340 return ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001341}
1342
1343/**
1344 * ubi_wl_flush - flush all pending works.
1345 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001346 * @vol_id: the volume id to flush for
1347 * @lnum: the logical eraseblock number to flush for
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001348 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001349 * This function executes all pending works for a particular volume id /
1350 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1351 * acts as a wildcard for all of the corresponding volume numbers or logical
1352 * eraseblock numbers. It returns zero in case of success and a negative error
1353 * code in case of failure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001354 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001355int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001356{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001357 int err = 0;
1358 int found = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001359
1360 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001361 * Erase while the pending works queue is not empty, but not more than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001362 * the number of currently pending works.
1363 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001364 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1365 vol_id, lnum, ubi->works_count);
1366
1367 while (found) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001368 struct ubi_work *wrk, *tmp;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001369 found = 0;
1370
1371 down_read(&ubi->work_sem);
1372 spin_lock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001373 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001374 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1375 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1376 list_del(&wrk->list);
1377 ubi->works_count -= 1;
1378 ubi_assert(ubi->works_count >= 0);
1379 spin_unlock(&ubi->wl_lock);
1380
1381 err = wrk->func(ubi, wrk, 0);
1382 if (err) {
1383 up_read(&ubi->work_sem);
1384 return err;
1385 }
1386
1387 spin_lock(&ubi->wl_lock);
1388 found = 1;
1389 break;
1390 }
1391 }
1392 spin_unlock(&ubi->wl_lock);
1393 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001394 }
1395
1396 /*
1397 * Make sure all the works which have been done in parallel are
1398 * finished.
1399 */
1400 down_write(&ubi->work_sem);
1401 up_write(&ubi->work_sem);
1402
Heiko Schocherff94bc42014-06-24 10:10:04 +02001403 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001404}
1405
1406/**
1407 * tree_destroy - destroy an RB-tree.
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001408 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001409 * @root: the root of the tree to destroy
1410 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001411static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001412{
1413 struct rb_node *rb;
1414 struct ubi_wl_entry *e;
1415
1416 rb = root->rb_node;
1417 while (rb) {
1418 if (rb->rb_left)
1419 rb = rb->rb_left;
1420 else if (rb->rb_right)
1421 rb = rb->rb_right;
1422 else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001423 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001424
1425 rb = rb_parent(rb);
1426 if (rb) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001427 if (rb->rb_left == &e->u.rb)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001428 rb->rb_left = NULL;
1429 else
1430 rb->rb_right = NULL;
1431 }
1432
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001433 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001434 }
1435 }
1436}
1437
1438/**
1439 * ubi_thread - UBI background thread.
1440 * @u: the UBI device description object pointer
1441 */
1442int ubi_thread(void *u)
1443{
1444 int failures = 0;
1445 struct ubi_device *ubi = u;
1446
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001447 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001448 ubi->bgt_name, task_pid_nr(current));
1449
1450 set_freezable();
1451 for (;;) {
1452 int err;
1453
1454 if (kthread_should_stop())
1455 break;
1456
1457 if (try_to_freeze())
1458 continue;
1459
1460 spin_lock(&ubi->wl_lock);
1461 if (list_empty(&ubi->works) || ubi->ro_mode ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02001462 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001463 set_current_state(TASK_INTERRUPTIBLE);
1464 spin_unlock(&ubi->wl_lock);
1465 schedule();
1466 continue;
1467 }
1468 spin_unlock(&ubi->wl_lock);
1469
1470 err = do_work(ubi);
1471 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001472 ubi_err(ubi, "%s: work failed with error code %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001473 ubi->bgt_name, err);
1474 if (failures++ > WL_MAX_FAILURES) {
1475 /*
1476 * Too many failures, disable the thread and
1477 * switch to read-only mode.
1478 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001479 ubi_msg(ubi, "%s: %d consecutive failures",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001480 ubi->bgt_name, WL_MAX_FAILURES);
1481 ubi_ro_mode(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001482 ubi->thread_enabled = 0;
1483 continue;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001484 }
1485 } else
1486 failures = 0;
1487
1488 cond_resched();
1489 }
1490
1491 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1492 return 0;
1493}
1494
1495/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001496 * shutdown_work - shutdown all pending works.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001497 * @ubi: UBI device description object
1498 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001499static void shutdown_work(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001500{
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001501#ifdef CONFIG_MTD_UBI_FASTMAP
1502#ifndef __UBOOT__
1503 flush_work(&ubi->fm_work);
1504#else
1505 /* in U-Boot, we have all work done */
1506#endif
1507#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001508 while (!list_empty(&ubi->works)) {
1509 struct ubi_work *wrk;
1510
1511 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1512 list_del(&wrk->list);
1513 wrk->func(ubi, wrk, 1);
1514 ubi->works_count -= 1;
1515 ubi_assert(ubi->works_count >= 0);
1516 }
1517}
1518
1519/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001520 * ubi_wl_init - initialize the WL sub-system using attaching information.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001521 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001522 * @ai: attaching information
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001523 *
1524 * This function returns zero in case of success, and a negative error code in
1525 * case of failure.
1526 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001527int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001528{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001529 int err, i, reserved_pebs, found_pebs = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001530 struct rb_node *rb1, *rb2;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001531 struct ubi_ainf_volume *av;
1532 struct ubi_ainf_peb *aeb, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001533 struct ubi_wl_entry *e;
1534
Heiko Schocherff94bc42014-06-24 10:10:04 +02001535 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001536 spin_lock_init(&ubi->wl_lock);
1537 mutex_init(&ubi->move_mutex);
1538 init_rwsem(&ubi->work_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001539 ubi->max_ec = ai->max_ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001540 INIT_LIST_HEAD(&ubi->works);
1541
1542 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1543
1544 err = -ENOMEM;
1545 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1546 if (!ubi->lookuptbl)
1547 return err;
1548
Heiko Schocherff94bc42014-06-24 10:10:04 +02001549 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1550 INIT_LIST_HEAD(&ubi->pq[i]);
1551 ubi->pq_head = 0;
1552
Heiko Schocher68fc4492016-02-02 11:54:35 +01001553 ubi->free_count = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001554 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001555 cond_resched();
1556
1557 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1558 if (!e)
1559 goto out_free;
1560
Heiko Schocherff94bc42014-06-24 10:10:04 +02001561 e->pnum = aeb->pnum;
1562 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001563 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001564 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001565 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001566 goto out_free;
1567 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001568
1569 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001570 }
1571
Heiko Schocherff94bc42014-06-24 10:10:04 +02001572 list_for_each_entry(aeb, &ai->free, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001573 cond_resched();
1574
1575 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1576 if (!e)
1577 goto out_free;
1578
Heiko Schocherff94bc42014-06-24 10:10:04 +02001579 e->pnum = aeb->pnum;
1580 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001581 ubi_assert(e->ec >= 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001582
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001583 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001584 ubi->free_count++;
1585
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001586 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001587
1588 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001589 }
1590
Heiko Schocherff94bc42014-06-24 10:10:04 +02001591 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1592 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001593 cond_resched();
1594
1595 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1596 if (!e)
1597 goto out_free;
1598
Heiko Schocherff94bc42014-06-24 10:10:04 +02001599 e->pnum = aeb->pnum;
1600 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001601 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001602
1603 if (!aeb->scrub) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001604 dbg_wl("add PEB %d EC %d to the used tree",
1605 e->pnum, e->ec);
1606 wl_tree_add(e, &ubi->used);
1607 } else {
1608 dbg_wl("add PEB %d EC %d to the scrub tree",
1609 e->pnum, e->ec);
1610 wl_tree_add(e, &ubi->scrub);
1611 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001612
1613 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001614 }
1615 }
1616
Heiko Schocherff94bc42014-06-24 10:10:04 +02001617 dbg_wl("found %i PEBs", found_pebs);
1618
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001619 if (ubi->fm) {
1620 ubi_assert(ubi->good_peb_count ==
Heiko Schocherff94bc42014-06-24 10:10:04 +02001621 found_pebs + ubi->fm->used_blocks);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001622
1623 for (i = 0; i < ubi->fm->used_blocks; i++) {
1624 e = ubi->fm->e[i];
1625 ubi->lookuptbl[e->pnum] = e;
1626 }
1627 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001628 else
1629 ubi_assert(ubi->good_peb_count == found_pebs);
1630
1631 reserved_pebs = WL_RESERVED_PEBS;
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001632 ubi_fastmap_init(ubi, &reserved_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001633
1634 if (ubi->avail_pebs < reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001635 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001636 ubi->avail_pebs, reserved_pebs);
1637 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001638 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001639 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001640 goto out_free;
1641 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001642 ubi->avail_pebs -= reserved_pebs;
1643 ubi->rsvd_pebs += reserved_pebs;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001644
1645 /* Schedule wear-leveling if needed */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001646 err = ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001647 if (err)
1648 goto out_free;
1649
1650 return 0;
1651
1652out_free:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001653 shutdown_work(ubi);
1654 tree_destroy(ubi, &ubi->used);
1655 tree_destroy(ubi, &ubi->free);
1656 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001657 kfree(ubi->lookuptbl);
1658 return err;
1659}
1660
1661/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001662 * protection_queue_destroy - destroy the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001663 * @ubi: UBI device description object
1664 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001665static void protection_queue_destroy(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001666{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001667 int i;
1668 struct ubi_wl_entry *e, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001669
Heiko Schocherff94bc42014-06-24 10:10:04 +02001670 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1671 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1672 list_del(&e->u.list);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001673 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001674 }
1675 }
1676}
1677
1678/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001679 * ubi_wl_close - close the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001680 * @ubi: UBI device description object
1681 */
1682void ubi_wl_close(struct ubi_device *ubi)
1683{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001684 dbg_wl("close the WL sub-system");
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001685 ubi_fastmap_close(ubi);
1686 shutdown_work(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001687 protection_queue_destroy(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001688 tree_destroy(ubi, &ubi->used);
1689 tree_destroy(ubi, &ubi->erroneous);
1690 tree_destroy(ubi, &ubi->free);
1691 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001692 kfree(ubi->lookuptbl);
1693}
1694
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001695/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001696 * self_check_ec - make sure that the erase counter of a PEB is correct.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001697 * @ubi: UBI device description object
1698 * @pnum: the physical eraseblock number to check
1699 * @ec: the erase counter to check
1700 *
1701 * This function returns zero if the erase counter of physical eraseblock @pnum
Heiko Schocherff94bc42014-06-24 10:10:04 +02001702 * is equivalent to @ec, and a negative error code if not or if an error
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001703 * occurred.
1704 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001705static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001706{
1707 int err;
1708 long long read_ec;
1709 struct ubi_ec_hdr *ec_hdr;
1710
Heiko Schocherff94bc42014-06-24 10:10:04 +02001711 if (!ubi_dbg_chk_gen(ubi))
1712 return 0;
1713
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001714 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1715 if (!ec_hdr)
1716 return -ENOMEM;
1717
1718 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1719 if (err && err != UBI_IO_BITFLIPS) {
1720 /* The header does not have to exist */
1721 err = 0;
1722 goto out_free;
1723 }
1724
1725 read_ec = be64_to_cpu(ec_hdr->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001726 if (ec != read_ec && read_ec - ec > 1) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001727 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1728 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001729 dump_stack();
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001730 err = 1;
1731 } else
1732 err = 0;
1733
1734out_free:
1735 kfree(ec_hdr);
1736 return err;
1737}
1738
1739/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001740 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
1741 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001742 * @e: the wear-leveling entry to check
1743 * @root: the root of the tree
1744 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001745 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001746 * is not.
1747 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001748static int self_check_in_wl_tree(const struct ubi_device *ubi,
1749 struct ubi_wl_entry *e, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001750{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001751 if (!ubi_dbg_chk_gen(ubi))
1752 return 0;
1753
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001754 if (in_wl_tree(e, root))
1755 return 0;
1756
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001757 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001758 e->pnum, e->ec, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001759 dump_stack();
1760 return -EINVAL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001761}
1762
Heiko Schocherff94bc42014-06-24 10:10:04 +02001763/**
1764 * self_check_in_pq - check if wear-leveling entry is in the protection
1765 * queue.
1766 * @ubi: UBI device description object
1767 * @e: the wear-leveling entry to check
1768 *
1769 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
1770 */
1771static int self_check_in_pq(const struct ubi_device *ubi,
1772 struct ubi_wl_entry *e)
1773{
1774 struct ubi_wl_entry *p;
1775 int i;
1776
1777 if (!ubi_dbg_chk_gen(ubi))
1778 return 0;
1779
1780 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1781 list_for_each_entry(p, &ubi->pq[i], u.list)
1782 if (p == e)
1783 return 0;
1784
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001785 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001786 e->pnum, e->ec);
1787 dump_stack();
1788 return -EINVAL;
1789}
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001790#ifndef CONFIG_MTD_UBI_FASTMAP
1791static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
1792{
1793 struct ubi_wl_entry *e;
1794
1795 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1796 self_check_in_wl_tree(ubi, e, &ubi->free);
1797 ubi->free_count--;
1798 ubi_assert(ubi->free_count >= 0);
1799 rb_erase(&e->u.rb, &ubi->free);
1800
1801 return e;
1802}
1803
1804/**
1805 * produce_free_peb - produce a free physical eraseblock.
1806 * @ubi: UBI device description object
1807 *
1808 * This function tries to make a free PEB by means of synchronous execution of
1809 * pending works. This may be needed if, for example the background thread is
1810 * disabled. Returns zero in case of success and a negative error code in case
1811 * of failure.
1812 */
1813static int produce_free_peb(struct ubi_device *ubi)
1814{
1815 int err;
1816
1817 while (!ubi->free.rb_node && ubi->works_count) {
1818 spin_unlock(&ubi->wl_lock);
1819
1820 dbg_wl("do one work synchronously");
1821 err = do_work(ubi);
1822
1823 spin_lock(&ubi->wl_lock);
1824 if (err)
1825 return err;
1826 }
1827
1828 return 0;
1829}
1830
1831/**
1832 * ubi_wl_get_peb - get a physical eraseblock.
1833 * @ubi: UBI device description object
1834 *
1835 * This function returns a physical eraseblock in case of success and a
1836 * negative error code in case of failure.
1837 * Returns with ubi->fm_eba_sem held in read mode!
1838 */
1839int ubi_wl_get_peb(struct ubi_device *ubi)
1840{
1841 int err;
1842 struct ubi_wl_entry *e;
1843
1844retry:
1845 down_read(&ubi->fm_eba_sem);
1846 spin_lock(&ubi->wl_lock);
1847 if (!ubi->free.rb_node) {
1848 if (ubi->works_count == 0) {
1849 ubi_err(ubi, "no free eraseblocks");
1850 ubi_assert(list_empty(&ubi->works));
1851 spin_unlock(&ubi->wl_lock);
1852 return -ENOSPC;
1853 }
1854
1855 err = produce_free_peb(ubi);
1856 if (err < 0) {
1857 spin_unlock(&ubi->wl_lock);
1858 return err;
1859 }
1860 spin_unlock(&ubi->wl_lock);
1861 up_read(&ubi->fm_eba_sem);
1862 goto retry;
1863
1864 }
1865 e = wl_get_wle(ubi);
1866 prot_queue_add(ubi, e);
1867 spin_unlock(&ubi->wl_lock);
1868
1869 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
1870 ubi->peb_size - ubi->vid_hdr_aloffset);
1871 if (err) {
1872 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
1873 return err;
1874 }
1875
1876 return e->pnum;
1877}
1878#else
1879#include "fastmap-wl.c"
1880#endif