blob: bae402418fb1daabfa5aea93c15336767de030a2 [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__
Simon Glassf7ae49f2020-05-10 11:40:05 -060089#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070090#include <dm/devres.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010091#include <linux/slab.h>
92#include <linux/crc32.h>
93#include <linux/freezer.h>
94#include <linux/kthread.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020095#else
96#include <ubi_uboot.h>
Kyungmin Parkc91a7192008-11-19 16:28:06 +010097#endif
98
Kyungmin Parkc91a7192008-11-19 16:28:06 +010099#include "ubi.h"
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200100#include "wl.h"
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100101
102/* Number of physical eraseblocks reserved for wear-leveling purposes */
103#define WL_RESERVED_PEBS 1
104
105/*
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100106 * Maximum difference between two erase counters. If this threshold is
Heiko Schocherff94bc42014-06-24 10:10:04 +0200107 * exceeded, the WL sub-system starts moving data from used physical
108 * eraseblocks with low erase counter to free physical eraseblocks with high
109 * erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100110 */
111#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD
112
113/*
Heiko Schocherff94bc42014-06-24 10:10:04 +0200114 * When a physical eraseblock is moved, the WL sub-system has to pick the target
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100115 * physical eraseblock to move to. The simplest way would be just to pick the
116 * one with the highest erase counter. But in certain workloads this could lead
117 * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a
118 * situation when the picked physical eraseblock is constantly erased after the
119 * data is written to it. So, we have a constant which limits the highest erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200120 * counter of the free physical eraseblock to pick. Namely, the WL sub-system
121 * does not pick eraseblocks with erase counter greater than the lowest erase
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100122 * counter plus %WL_FREE_MAX_DIFF.
123 */
124#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD)
125
126/*
127 * Maximum number of consecutive background thread failures which is enough to
128 * switch to read-only mode.
129 */
130#define WL_MAX_FAILURES 32
131
Heiko Schocherff94bc42014-06-24 10:10:04 +0200132static int self_check_ec(struct ubi_device *ubi, int pnum, int ec);
133static int self_check_in_wl_tree(const struct ubi_device *ubi,
134 struct ubi_wl_entry *e, struct rb_root *root);
135static int self_check_in_pq(const struct ubi_device *ubi,
136 struct ubi_wl_entry *e);
137
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100138/**
139 * wl_tree_add - add a wear-leveling entry to a WL RB-tree.
140 * @e: the wear-leveling entry to add
141 * @root: the root of the tree
142 *
143 * Note, we use (erase counter, physical eraseblock number) pairs as keys in
144 * the @ubi->used and @ubi->free RB-trees.
145 */
146static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root)
147{
148 struct rb_node **p, *parent = NULL;
149
150 p = &root->rb_node;
151 while (*p) {
152 struct ubi_wl_entry *e1;
153
154 parent = *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200155 e1 = rb_entry(parent, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100156
157 if (e->ec < e1->ec)
158 p = &(*p)->rb_left;
159 else if (e->ec > e1->ec)
160 p = &(*p)->rb_right;
161 else {
162 ubi_assert(e->pnum != e1->pnum);
163 if (e->pnum < e1->pnum)
164 p = &(*p)->rb_left;
165 else
166 p = &(*p)->rb_right;
167 }
168 }
169
Heiko Schocherff94bc42014-06-24 10:10:04 +0200170 rb_link_node(&e->u.rb, parent, p);
171 rb_insert_color(&e->u.rb, root);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100172}
173
174/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200175 * wl_tree_destroy - destroy a wear-leveling entry.
176 * @ubi: UBI device description object
177 * @e: the wear-leveling entry to add
178 *
179 * This function destroys a wear leveling entry and removes
180 * the reference from the lookup table.
181 */
182static void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e)
183{
184 ubi->lookuptbl[e->pnum] = NULL;
185 kmem_cache_free(ubi_wl_entry_slab, e);
186}
187
188/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100189 * do_work - do one pending work.
190 * @ubi: UBI device description object
191 *
192 * This function returns zero in case of success and a negative error code in
193 * case of failure.
194 */
195static int do_work(struct ubi_device *ubi)
196{
197 int err;
198 struct ubi_work *wrk;
199
200 cond_resched();
201
202 /*
203 * @ubi->work_sem is used to synchronize with the workers. Workers take
204 * it in read mode, so many of them may be doing works at a time. But
205 * the queue flush code has to be sure the whole queue of works is
206 * done, and it takes the mutex in write mode.
207 */
208 down_read(&ubi->work_sem);
209 spin_lock(&ubi->wl_lock);
210 if (list_empty(&ubi->works)) {
211 spin_unlock(&ubi->wl_lock);
212 up_read(&ubi->work_sem);
213 return 0;
214 }
215
216 wrk = list_entry(ubi->works.next, struct ubi_work, list);
217 list_del(&wrk->list);
218 ubi->works_count -= 1;
219 ubi_assert(ubi->works_count >= 0);
220 spin_unlock(&ubi->wl_lock);
221
222 /*
223 * Call the worker function. Do not touch the work structure
224 * after this call as it will have been freed or reused by that
225 * time by the worker function.
226 */
227 err = wrk->func(ubi, wrk, 0);
228 if (err)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200229 ubi_err(ubi, "work failed with error code %d", err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100230 up_read(&ubi->work_sem);
231
232 return err;
233}
234
235/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100236 * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree.
237 * @e: the wear-leveling entry to check
238 * @root: the root of the tree
239 *
240 * This function returns non-zero if @e is in the @root RB-tree and zero if it
241 * is not.
242 */
243static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root)
244{
245 struct rb_node *p;
246
247 p = root->rb_node;
248 while (p) {
249 struct ubi_wl_entry *e1;
250
Heiko Schocherff94bc42014-06-24 10:10:04 +0200251 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100252
253 if (e->pnum == e1->pnum) {
254 ubi_assert(e == e1);
255 return 1;
256 }
257
258 if (e->ec < e1->ec)
259 p = p->rb_left;
260 else if (e->ec > e1->ec)
261 p = p->rb_right;
262 else {
263 ubi_assert(e->pnum != e1->pnum);
264 if (e->pnum < e1->pnum)
265 p = p->rb_left;
266 else
267 p = p->rb_right;
268 }
269 }
270
271 return 0;
272}
273
274/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200275 * prot_queue_add - add physical eraseblock to the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100276 * @ubi: UBI device description object
277 * @e: the physical eraseblock to add
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100278 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200279 * This function adds @e to the tail of the protection queue @ubi->pq, where
280 * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be
281 * temporarily protected from the wear-leveling worker. Note, @wl->lock has to
282 * be locked.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100283 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200284static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100285{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200286 int pq_tail = ubi->pq_head - 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100287
Heiko Schocherff94bc42014-06-24 10:10:04 +0200288 if (pq_tail < 0)
289 pq_tail = UBI_PROT_QUEUE_LEN - 1;
290 ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN);
291 list_add_tail(&e->u.list, &ubi->pq[pq_tail]);
292 dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100293}
294
295/**
296 * find_wl_entry - find wear-leveling entry closest to certain erase counter.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200297 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100298 * @root: the RB-tree where to look for
Heiko Schocherff94bc42014-06-24 10:10:04 +0200299 * @diff: maximum possible difference from the smallest erase counter
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100300 *
301 * This function looks for a wear leveling entry with erase counter closest to
Heiko Schocherff94bc42014-06-24 10:10:04 +0200302 * min + @diff, where min is the smallest erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100303 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200304static struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi,
305 struct rb_root *root, int diff)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100306{
307 struct rb_node *p;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200308 struct ubi_wl_entry *e, *prev_e = NULL;
309 int max;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100310
Heiko Schocherff94bc42014-06-24 10:10:04 +0200311 e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
312 max = e->ec + diff;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100313
314 p = root->rb_node;
315 while (p) {
316 struct ubi_wl_entry *e1;
317
Heiko Schocherff94bc42014-06-24 10:10:04 +0200318 e1 = rb_entry(p, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100319 if (e1->ec >= max)
320 p = p->rb_left;
321 else {
322 p = p->rb_right;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200323 prev_e = e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100324 e = e1;
325 }
326 }
327
Heiko Schocherff94bc42014-06-24 10:10:04 +0200328 /* If no fastmap has been written and this WL entry can be used
329 * as anchor PEB, hold it back and return the second best WL entry
330 * such that fastmap can use the anchor PEB later. */
331 if (prev_e && !ubi->fm_disabled &&
332 !ubi->fm && e->pnum < UBI_FM_MAX_START)
333 return prev_e;
334
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100335 return e;
336}
337
338/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200339 * find_mean_wl_entry - find wear-leveling entry with medium erase counter.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100340 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200341 * @root: the RB-tree where to look for
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100342 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200343 * This function looks for a wear leveling entry with medium erase counter,
344 * but not greater or equivalent than the lowest erase counter plus
345 * %WL_FREE_MAX_DIFF/2.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100346 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200347static struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi,
348 struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100349{
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100350 struct ubi_wl_entry *e, *first, *last;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100351
Heiko Schocherff94bc42014-06-24 10:10:04 +0200352 first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb);
353 last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100354
Heiko Schocherff94bc42014-06-24 10:10:04 +0200355 if (last->ec - first->ec < WL_FREE_MAX_DIFF) {
356 e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100357
Heiko Schocherff94bc42014-06-24 10:10:04 +0200358 /* If no fastmap has been written and this WL entry can be used
359 * as anchor PEB, hold it back and return the second best
360 * WL entry such that fastmap can use the anchor PEB later. */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200361 e = may_reserve_for_fm(ubi, e, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200362 } else
363 e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2);
364
365 return e;
366}
367
Heiko Schocherff94bc42014-06-24 10:10:04 +0200368/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200369 * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or
370 * refill_wl_user_pool().
Heiko Schocherff94bc42014-06-24 10:10:04 +0200371 * @ubi: UBI device description object
372 *
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200373 * This function returns a a wear leveling entry in case of success and
374 * NULL in case of failure.
Heiko Schocherff94bc42014-06-24 10:10:04 +0200375 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200376static struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200377{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200378 struct ubi_wl_entry *e;
379
Heiko Schocherff94bc42014-06-24 10:10:04 +0200380 e = find_mean_wl_entry(ubi, &ubi->free);
381 if (!e) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200382 ubi_err(ubi, "no free eraseblocks");
383 return NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200384 }
385
386 self_check_in_wl_tree(ubi, e, &ubi->free);
387
388 /*
389 * Move the physical eraseblock to the protection queue where it will
390 * be protected from being moved for some time.
391 */
392 rb_erase(&e->u.rb, &ubi->free);
393 ubi->free_count--;
394 dbg_wl("PEB %d EC %d", e->pnum, e->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200395
396 return e;
397}
398
Heiko Schocherff94bc42014-06-24 10:10:04 +0200399/**
400 * prot_queue_del - remove a physical eraseblock from the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100401 * @ubi: UBI device description object
402 * @pnum: the physical eraseblock to remove
403 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200404 * This function deletes PEB @pnum from the protection queue and returns zero
405 * in case of success and %-ENODEV if the PEB was not found.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100406 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200407static int prot_queue_del(struct ubi_device *ubi, int pnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100408{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200409 struct ubi_wl_entry *e;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100410
Heiko Schocherff94bc42014-06-24 10:10:04 +0200411 e = ubi->lookuptbl[pnum];
412 if (!e)
413 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100414
Heiko Schocherff94bc42014-06-24 10:10:04 +0200415 if (self_check_in_pq(ubi, e))
416 return -ENODEV;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100417
Heiko Schocherff94bc42014-06-24 10:10:04 +0200418 list_del(&e->u.list);
419 dbg_wl("deleted PEB %d from the protection queue", e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100420 return 0;
421}
422
423/**
424 * sync_erase - synchronously erase a physical eraseblock.
425 * @ubi: UBI device description object
426 * @e: the the physical eraseblock to erase
427 * @torture: if the physical eraseblock has to be tortured
428 *
429 * This function returns zero in case of success and a negative error code in
430 * case of failure.
431 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200432static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
433 int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100434{
435 int err;
436 struct ubi_ec_hdr *ec_hdr;
437 unsigned long long ec = e->ec;
438
439 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
440
Heiko Schocherff94bc42014-06-24 10:10:04 +0200441 err = self_check_ec(ubi, e->pnum, e->ec);
442 if (err)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100443 return -EINVAL;
444
445 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
446 if (!ec_hdr)
447 return -ENOMEM;
448
449 err = ubi_io_sync_erase(ubi, e->pnum, torture);
450 if (err < 0)
451 goto out_free;
452
453 ec += err;
454 if (ec > UBI_MAX_ERASECOUNTER) {
455 /*
456 * Erase counter overflow. Upgrade UBI and use 64-bit
457 * erase counters internally.
458 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200459 ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100460 e->pnum, ec);
461 err = -EINVAL;
462 goto out_free;
463 }
464
465 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
466
467 ec_hdr->ec = cpu_to_be64(ec);
468
469 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
470 if (err)
471 goto out_free;
472
473 e->ec = ec;
474 spin_lock(&ubi->wl_lock);
475 if (e->ec > ubi->max_ec)
476 ubi->max_ec = e->ec;
477 spin_unlock(&ubi->wl_lock);
478
479out_free:
480 kfree(ec_hdr);
481 return err;
482}
483
484/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200485 * serve_prot_queue - check if it is time to stop protecting PEBs.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100486 * @ubi: UBI device description object
487 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200488 * This function is called after each erase operation and removes PEBs from the
489 * tail of the protection queue. These PEBs have been protected for long enough
490 * and should be moved to the used tree.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100491 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200492static void serve_prot_queue(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100493{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200494 struct ubi_wl_entry *e, *tmp;
495 int count;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100496
497 /*
498 * There may be several protected physical eraseblock to remove,
499 * process them all.
500 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200501repeat:
502 count = 0;
503 spin_lock(&ubi->wl_lock);
504 list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) {
505 dbg_wl("PEB %d EC %d protection over, move to used tree",
506 e->pnum, e->ec);
507
508 list_del(&e->u.list);
509 wl_tree_add(e, &ubi->used);
510 if (count++ > 32) {
511 /*
512 * Let's be nice and avoid holding the spinlock for
513 * too long.
514 */
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100515 spin_unlock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200516 cond_resched();
517 goto repeat;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100518 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100519 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200520
521 ubi->pq_head += 1;
522 if (ubi->pq_head == UBI_PROT_QUEUE_LEN)
523 ubi->pq_head = 0;
524 ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN);
525 spin_unlock(&ubi->wl_lock);
526}
527
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100528#ifdef __UBOOT__
529void ubi_do_worker(struct ubi_device *ubi)
530{
531 int err;
532
533 if (list_empty(&ubi->works) || ubi->ro_mode ||
534 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi))
535 return;
536
537 spin_lock(&ubi->wl_lock);
538 while (!list_empty(&ubi->works)) {
539 /*
540 * call do_work, which executes exactly one work form the queue,
541 * including removeing it from the work queue.
542 */
543 spin_unlock(&ubi->wl_lock);
544 err = do_work(ubi);
545 spin_lock(&ubi->wl_lock);
546 if (err) {
547 ubi_err(ubi, "%s: work failed with error code %d",
548 ubi->bgt_name, err);
549 }
550 }
551 spin_unlock(&ubi->wl_lock);
552}
553#endif
554
Heiko Schocherff94bc42014-06-24 10:10:04 +0200555/**
556 * __schedule_ubi_work - schedule a work.
557 * @ubi: UBI device description object
558 * @wrk: the work to schedule
559 *
560 * This function adds a work defined by @wrk to the tail of the pending works
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200561 * list. Can only be used if ubi->work_sem is already held in read mode!
Heiko Schocherff94bc42014-06-24 10:10:04 +0200562 */
563static void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
564{
565 spin_lock(&ubi->wl_lock);
566 list_add_tail(&wrk->list, &ubi->works);
567 ubi_assert(ubi->works_count >= 0);
568 ubi->works_count += 1;
569#ifndef __UBOOT__
570 if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi))
571 wake_up_process(ubi->bgt_thread);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200572#endif
573 spin_unlock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100574}
575
576/**
577 * schedule_ubi_work - schedule a work.
578 * @ubi: UBI device description object
579 * @wrk: the work to schedule
580 *
Heiko Schocherff94bc42014-06-24 10:10:04 +0200581 * This function adds a work defined by @wrk to the tail of the pending works
582 * list.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100583 */
584static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk)
585{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200586 down_read(&ubi->work_sem);
587 __schedule_ubi_work(ubi, wrk);
588 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100589}
590
591static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200592 int shutdown);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200593
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100594/**
595 * schedule_erase - schedule an erase work.
596 * @ubi: UBI device description object
597 * @e: the WL entry of the physical eraseblock to erase
Heiko Schocherff94bc42014-06-24 10:10:04 +0200598 * @vol_id: the volume ID that last used this PEB
599 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100600 * @torture: if the physical eraseblock has to be tortured
601 *
602 * This function returns zero in case of success and a %-ENOMEM in case of
603 * failure.
604 */
605static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
Heiko Schocherff94bc42014-06-24 10:10:04 +0200606 int vol_id, int lnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100607{
608 struct ubi_work *wl_wrk;
609
Heiko Schocherff94bc42014-06-24 10:10:04 +0200610 ubi_assert(e);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200611
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100612 dbg_wl("schedule erasure of PEB %d, EC %d, torture %d",
613 e->pnum, e->ec, torture);
614
615 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
616 if (!wl_wrk)
617 return -ENOMEM;
618
619 wl_wrk->func = &erase_worker;
620 wl_wrk->e = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200621 wl_wrk->vol_id = vol_id;
622 wl_wrk->lnum = lnum;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100623 wl_wrk->torture = torture;
624
625 schedule_ubi_work(ubi, wl_wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +0100626
627#ifdef __UBOOT__
628 ubi_do_worker(ubi);
629#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100630 return 0;
631}
632
633/**
Heiko Schocherff94bc42014-06-24 10:10:04 +0200634 * do_sync_erase - run the erase worker synchronously.
635 * @ubi: UBI device description object
636 * @e: the WL entry of the physical eraseblock to erase
637 * @vol_id: the volume ID that last used this PEB
638 * @lnum: the last used logical eraseblock number for the PEB
639 * @torture: if the physical eraseblock has to be tortured
640 *
641 */
642static int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
643 int vol_id, int lnum, int torture)
644{
645 struct ubi_work *wl_wrk;
646
647 dbg_wl("sync erase of PEB %i", e->pnum);
648
649 wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
650 if (!wl_wrk)
651 return -ENOMEM;
652
653 wl_wrk->e = e;
654 wl_wrk->vol_id = vol_id;
655 wl_wrk->lnum = lnum;
656 wl_wrk->torture = torture;
657
658 return erase_worker(ubi, wl_wrk, 0);
659}
660
Heiko Schocherff94bc42014-06-24 10:10:04 +0200661/**
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100662 * wear_leveling_worker - wear-leveling worker function.
663 * @ubi: UBI device description object
664 * @wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200665 * @shutdown: non-zero if the worker has to free memory and exit
666 * because the WL-subsystem is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100667 *
668 * This function copies a more worn out physical eraseblock to a less worn out
669 * one. Returns zero in case of success and a negative error code in case of
670 * failure.
671 */
672static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200673 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100674{
Heiko Schocherff94bc42014-06-24 10:10:04 +0200675 int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200676 int vol_id = -1, lnum = -1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200677#ifdef CONFIG_MTD_UBI_FASTMAP
678 int anchor = wrk->anchor;
679#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100680 struct ubi_wl_entry *e1, *e2;
681 struct ubi_vid_hdr *vid_hdr;
682
683 kfree(wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200684 if (shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100685 return 0;
686
687 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS);
688 if (!vid_hdr)
689 return -ENOMEM;
690
691 mutex_lock(&ubi->move_mutex);
692 spin_lock(&ubi->wl_lock);
693 ubi_assert(!ubi->move_from && !ubi->move_to);
694 ubi_assert(!ubi->move_to_put);
695
696 if (!ubi->free.rb_node ||
697 (!ubi->used.rb_node && !ubi->scrub.rb_node)) {
698 /*
699 * No free physical eraseblocks? Well, they must be waiting in
700 * the queue to be erased. Cancel movement - it will be
701 * triggered again when a free physical eraseblock appears.
702 *
703 * No used physical eraseblocks? They must be temporarily
704 * protected from being moved. They will be moved to the
705 * @ubi->used tree later and the wear-leveling will be
706 * triggered again.
707 */
708 dbg_wl("cancel WL, a list is empty: free %d, used %d",
709 !ubi->free.rb_node, !ubi->used.rb_node);
710 goto out_cancel;
711 }
712
Heiko Schocherff94bc42014-06-24 10:10:04 +0200713#ifdef CONFIG_MTD_UBI_FASTMAP
714 /* Check whether we need to produce an anchor PEB */
715 if (!anchor)
716 anchor = !anchor_pebs_avalible(&ubi->free);
717
718 if (anchor) {
719 e1 = find_anchor_wl_entry(&ubi->used);
720 if (!e1)
721 goto out_cancel;
722 e2 = get_peb_for_wl(ubi);
723 if (!e2)
724 goto out_cancel;
725
726 self_check_in_wl_tree(ubi, e1, &ubi->used);
727 rb_erase(&e1->u.rb, &ubi->used);
728 dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum);
729 } else if (!ubi->scrub.rb_node) {
730#else
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100731 if (!ubi->scrub.rb_node) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200732#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100733 /*
734 * Now pick the least worn-out used physical eraseblock and a
735 * highly worn-out free physical eraseblock. If the erase
736 * counters differ much enough, start wear-leveling.
737 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200738 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
739 e2 = get_peb_for_wl(ubi);
740 if (!e2)
741 goto out_cancel;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100742
743 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) {
744 dbg_wl("no WL needed: min used EC %d, max free EC %d",
745 e1->ec, e2->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200746
747 /* Give the unused PEB back */
748 wl_tree_add(e2, &ubi->free);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200749 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100750 goto out_cancel;
751 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200752 self_check_in_wl_tree(ubi, e1, &ubi->used);
753 rb_erase(&e1->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100754 dbg_wl("move PEB %d EC %d to PEB %d EC %d",
755 e1->pnum, e1->ec, e2->pnum, e2->ec);
756 } else {
757 /* Perform scrubbing */
758 scrubbing = 1;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200759 e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb);
760 e2 = get_peb_for_wl(ubi);
761 if (!e2)
762 goto out_cancel;
763
764 self_check_in_wl_tree(ubi, e1, &ubi->scrub);
765 rb_erase(&e1->u.rb, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100766 dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum);
767 }
768
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100769 ubi->move_from = e1;
770 ubi->move_to = e2;
771 spin_unlock(&ubi->wl_lock);
772
773 /*
774 * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum.
775 * We so far do not know which logical eraseblock our physical
776 * eraseblock (@e1) belongs to. We have to read the volume identifier
777 * header first.
778 *
779 * Note, we are protected from this PEB being unmapped and erased. The
780 * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB
781 * which is being moved was unmapped.
782 */
783
784 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0);
785 if (err && err != UBI_IO_BITFLIPS) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200786 if (err == UBI_IO_FF) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100787 /*
788 * We are trying to move PEB without a VID header. UBI
789 * always write VID headers shortly after the PEB was
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790 * given, so we have a situation when it has not yet
791 * had a chance to write it, because it was preempted.
792 * So add this PEB to the protection queue so far,
793 * because presumably more data will be written there
794 * (including the missing VID header), and then we'll
795 * move it.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100796 */
797 dbg_wl("PEB %d has no VID header", e1->pnum);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200798 protect = 1;
799 goto out_not_moved;
800 } else if (err == UBI_IO_FF_BITFLIPS) {
801 /*
802 * The same situation as %UBI_IO_FF, but bit-flips were
803 * detected. It is better to schedule this PEB for
804 * scrubbing.
805 */
806 dbg_wl("PEB %d has no VID header but has bit-flips",
807 e1->pnum);
808 scrubbing = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100809 goto out_not_moved;
810 }
811
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200812 ubi_err(ubi, "error %d while reading VID header from PEB %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100813 err, e1->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100814 goto out_error;
815 }
816
Heiko Schocherff94bc42014-06-24 10:10:04 +0200817 vol_id = be32_to_cpu(vid_hdr->vol_id);
818 lnum = be32_to_cpu(vid_hdr->lnum);
819
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100820 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr);
821 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200822 if (err == MOVE_CANCEL_RACE) {
823 /*
824 * The LEB has not been moved because the volume is
825 * being deleted or the PEB has been put meanwhile. We
826 * should prevent this PEB from being selected for
827 * wear-leveling movement again, so put it to the
828 * protection queue.
829 */
830 protect = 1;
831 goto out_not_moved;
832 }
833 if (err == MOVE_RETRY) {
834 scrubbing = 1;
835 goto out_not_moved;
836 }
837 if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR ||
838 err == MOVE_TARGET_RD_ERR) {
839 /*
840 * Target PEB had bit-flips or write error - torture it.
841 */
842 torture = 1;
843 goto out_not_moved;
844 }
845
846 if (err == MOVE_SOURCE_RD_ERR) {
847 /*
848 * An error happened while reading the source PEB. Do
849 * not switch to R/O mode in this case, and give the
850 * upper layers a possibility to recover from this,
851 * e.g. by unmapping corresponding LEB. Instead, just
852 * put this PEB to the @ubi->erroneous list to prevent
853 * UBI from trying to move it over and over again.
854 */
855 if (ubi->erroneous_peb_count > ubi->max_erroneous) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200856 ubi_err(ubi, "too many erroneous eraseblocks (%d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200857 ubi->erroneous_peb_count);
858 goto out_error;
859 }
860 erroneous = 1;
861 goto out_not_moved;
862 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100863
864 if (err < 0)
865 goto out_error;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100866
Heiko Schocherff94bc42014-06-24 10:10:04 +0200867 ubi_assert(0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100868 }
869
Heiko Schocherff94bc42014-06-24 10:10:04 +0200870 /* The PEB has been successfully moved */
871 if (scrubbing)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200872 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200873 e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100874 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200875
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100876 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200877 if (!ubi->move_to_put) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100878 wl_tree_add(e2, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200879 e2 = NULL;
880 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100881 ubi->move_from = ubi->move_to = NULL;
882 ubi->move_to_put = ubi->wl_scheduled = 0;
883 spin_unlock(&ubi->wl_lock);
884
Heiko Schocherff94bc42014-06-24 10:10:04 +0200885 err = do_sync_erase(ubi, e1, vol_id, lnum, 0);
886 if (err) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200887 if (e2)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200888 wl_entry_destroy(ubi, e2);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200889 goto out_ro;
890 }
891
892 if (e2) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100893 /*
894 * Well, the target PEB was put meanwhile, schedule it for
895 * erasure.
896 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200897 dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase",
898 e2->pnum, vol_id, lnum);
899 err = do_sync_erase(ubi, e2, vol_id, lnum, 0);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200900 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200901 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100902 }
903
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100904 dbg_wl("done");
905 mutex_unlock(&ubi->move_mutex);
906 return 0;
907
908 /*
909 * For some reasons the LEB was not moved, might be an error, might be
910 * something else. @e1 was not changed, so return it back. @e2 might
Heiko Schocherff94bc42014-06-24 10:10:04 +0200911 * have been changed, schedule it for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100912 */
913out_not_moved:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200914 if (vol_id != -1)
915 dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)",
916 e1->pnum, vol_id, lnum, e2->pnum, err);
917 else
918 dbg_wl("cancel moving PEB %d to PEB %d (%d)",
919 e1->pnum, e2->pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100920 spin_lock(&ubi->wl_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200921 if (protect)
922 prot_queue_add(ubi, e1);
923 else if (erroneous) {
924 wl_tree_add(e1, &ubi->erroneous);
925 ubi->erroneous_peb_count += 1;
926 } else if (scrubbing)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100927 wl_tree_add(e1, &ubi->scrub);
928 else
929 wl_tree_add(e1, &ubi->used);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200930 ubi_assert(!ubi->move_to_put);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100931 ubi->move_from = ubi->move_to = NULL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200932 ubi->wl_scheduled = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100933 spin_unlock(&ubi->wl_lock);
934
Heiko Schocherff94bc42014-06-24 10:10:04 +0200935 ubi_free_vid_hdr(ubi, vid_hdr);
936 err = do_sync_erase(ubi, e2, vol_id, lnum, torture);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200937 if (err)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200938 goto out_ro;
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200939
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100940 mutex_unlock(&ubi->move_mutex);
941 return 0;
942
943out_error:
Heiko Schocherff94bc42014-06-24 10:10:04 +0200944 if (vol_id != -1)
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200945 ubi_err(ubi, "error %d while moving PEB %d to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200946 err, e1->pnum, e2->pnum);
947 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200948 ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200949 err, e1->pnum, vol_id, lnum, e2->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100950 spin_lock(&ubi->wl_lock);
951 ubi->move_from = ubi->move_to = NULL;
952 ubi->move_to_put = ubi->wl_scheduled = 0;
953 spin_unlock(&ubi->wl_lock);
954
Heiko Schocherff94bc42014-06-24 10:10:04 +0200955 ubi_free_vid_hdr(ubi, vid_hdr);
Heiko Schocher0195a7b2015-10-22 06:19:21 +0200956 wl_entry_destroy(ubi, e1);
957 wl_entry_destroy(ubi, e2);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100958
Heiko Schocherff94bc42014-06-24 10:10:04 +0200959out_ro:
960 ubi_ro_mode(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100961 mutex_unlock(&ubi->move_mutex);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200962 ubi_assert(err != 0);
963 return err < 0 ? err : -EIO;
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100964
965out_cancel:
966 ubi->wl_scheduled = 0;
967 spin_unlock(&ubi->wl_lock);
968 mutex_unlock(&ubi->move_mutex);
969 ubi_free_vid_hdr(ubi, vid_hdr);
970 return 0;
971}
972
973/**
974 * ensure_wear_leveling - schedule wear-leveling if it is needed.
975 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +0200976 * @nested: set to non-zero if this function is called from UBI worker
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100977 *
978 * This function checks if it is time to start wear-leveling and schedules it
979 * if yes. This function returns zero in case of success and a negative error
980 * code in case of failure.
981 */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200982static int ensure_wear_leveling(struct ubi_device *ubi, int nested)
Kyungmin Parkc91a7192008-11-19 16:28:06 +0100983{
984 int err = 0;
985 struct ubi_wl_entry *e1;
986 struct ubi_wl_entry *e2;
987 struct ubi_work *wrk;
988
989 spin_lock(&ubi->wl_lock);
990 if (ubi->wl_scheduled)
991 /* Wear-leveling is already in the work queue */
992 goto out_unlock;
993
994 /*
995 * If the ubi->scrub tree is not empty, scrubbing is needed, and the
996 * the WL worker has to be scheduled anyway.
997 */
998 if (!ubi->scrub.rb_node) {
999 if (!ubi->used.rb_node || !ubi->free.rb_node)
1000 /* No physical eraseblocks - no deal */
1001 goto out_unlock;
1002
1003 /*
1004 * We schedule wear-leveling only if the difference between the
1005 * lowest erase counter of used physical eraseblocks and a high
Heiko Schocherff94bc42014-06-24 10:10:04 +02001006 * erase counter of free physical eraseblocks is greater than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001007 * %UBI_WL_THRESHOLD.
1008 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001009 e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
1010 e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001011
1012 if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD))
1013 goto out_unlock;
1014 dbg_wl("schedule wear-leveling");
1015 } else
1016 dbg_wl("schedule scrubbing");
1017
1018 ubi->wl_scheduled = 1;
1019 spin_unlock(&ubi->wl_lock);
1020
1021 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
1022 if (!wrk) {
1023 err = -ENOMEM;
1024 goto out_cancel;
1025 }
1026
Heiko Schocherff94bc42014-06-24 10:10:04 +02001027 wrk->anchor = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001028 wrk->func = &wear_leveling_worker;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001029 if (nested)
1030 __schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001031#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +02001032 else
1033 schedule_ubi_work(ubi, wrk);
Richard Weinbergerf82290a2018-02-08 07:29:52 +01001034#else
1035 else {
1036 schedule_ubi_work(ubi, wrk);
1037 ubi_do_worker(ubi);
1038 }
1039#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001040 return err;
1041
1042out_cancel:
1043 spin_lock(&ubi->wl_lock);
1044 ubi->wl_scheduled = 0;
1045out_unlock:
1046 spin_unlock(&ubi->wl_lock);
1047 return err;
1048}
1049
1050/**
1051 * erase_worker - physical eraseblock erase worker function.
1052 * @ubi: UBI device description object
1053 * @wl_wrk: the work object
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001054 * @shutdown: non-zero if the worker has to free memory and exit
1055 * because the WL sub-system is shutting down
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001056 *
1057 * This function erases a physical eraseblock and perform torture testing if
1058 * needed. It also takes care about marking the physical eraseblock bad if
1059 * needed. Returns zero in case of success and a negative error code in case of
1060 * failure.
1061 */
1062static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001063 int shutdown)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001064{
1065 struct ubi_wl_entry *e = wl_wrk->e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001066 int pnum = e->pnum;
1067 int vol_id = wl_wrk->vol_id;
1068 int lnum = wl_wrk->lnum;
1069 int err, available_consumed = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001070
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001071 if (shutdown) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001072 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
1073 kfree(wl_wrk);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001074 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001075 return 0;
1076 }
1077
Heiko Schocherff94bc42014-06-24 10:10:04 +02001078 dbg_wl("erase PEB %d EC %d LEB %d:%d",
1079 pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum);
1080
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001081 err = sync_erase(ubi, e, wl_wrk->torture);
1082 if (!err) {
1083 /* Fine, we've erased it successfully */
1084 kfree(wl_wrk);
1085
1086 spin_lock(&ubi->wl_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001087 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001088 ubi->free_count++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001089 spin_unlock(&ubi->wl_lock);
1090
1091 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001092 * One more erase operation has happened, take care about
1093 * protected physical eraseblocks.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001094 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001095 serve_prot_queue(ubi);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001096
1097 /* And take care about wear-leveling */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001098 err = ensure_wear_leveling(ubi, 1);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001099 return err;
1100 }
1101
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001102 ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001103 kfree(wl_wrk);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001104
1105 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1106 err == -EBUSY) {
1107 int err1;
1108
1109 /* Re-schedule the LEB for erasure */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001110 err1 = schedule_erase(ubi, e, vol_id, lnum, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001111 if (err1) {
1112 err = err1;
1113 goto out_ro;
1114 }
1115 return err;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001116 }
1117
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001118 wl_entry_destroy(ubi, e);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001119 if (err != -EIO)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001120 /*
1121 * If this is not %-EIO, we have no idea what to do. Scheduling
1122 * this physical eraseblock for erasure again would cause
Heiko Schocherff94bc42014-06-24 10:10:04 +02001123 * errors again and again. Well, lets switch to R/O mode.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001124 */
1125 goto out_ro;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001126
1127 /* It is %-EIO, the PEB went bad */
1128
1129 if (!ubi->bad_allowed) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001130 ubi_err(ubi, "bad physical eraseblock %d detected", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001131 goto out_ro;
1132 }
1133
1134 spin_lock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001135 if (ubi->beb_rsvd_pebs == 0) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001136 if (ubi->avail_pebs == 0) {
1137 spin_unlock(&ubi->volumes_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001138 ubi_err(ubi, "no reserved/available physical eraseblocks");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001139 goto out_ro;
1140 }
1141 ubi->avail_pebs -= 1;
1142 available_consumed = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001143 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001144 spin_unlock(&ubi->volumes_lock);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001145
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001146 ubi_msg(ubi, "mark PEB %d as bad", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001147 err = ubi_io_mark_bad(ubi, pnum);
1148 if (err)
1149 goto out_ro;
1150
1151 spin_lock(&ubi->volumes_lock);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001152 if (ubi->beb_rsvd_pebs > 0) {
1153 if (available_consumed) {
1154 /*
1155 * The amount of reserved PEBs increased since we last
1156 * checked.
1157 */
1158 ubi->avail_pebs += 1;
1159 available_consumed = 0;
1160 }
1161 ubi->beb_rsvd_pebs -= 1;
1162 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001163 ubi->bad_peb_count += 1;
1164 ubi->good_peb_count -= 1;
1165 ubi_calculate_reserved(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001166 if (available_consumed)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001167 ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB");
Heiko Schocherff94bc42014-06-24 10:10:04 +02001168 else if (ubi->beb_rsvd_pebs)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001169 ubi_msg(ubi, "%d PEBs left in the reserve",
1170 ubi->beb_rsvd_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001171 else
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001172 ubi_warn(ubi, "last PEB from the reserve was used");
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001173 spin_unlock(&ubi->volumes_lock);
1174
1175 return err;
1176
1177out_ro:
Heiko Schocherff94bc42014-06-24 10:10:04 +02001178 if (available_consumed) {
1179 spin_lock(&ubi->volumes_lock);
1180 ubi->avail_pebs += 1;
1181 spin_unlock(&ubi->volumes_lock);
1182 }
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001183 ubi_ro_mode(ubi);
1184 return err;
1185}
1186
1187/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001188 * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001189 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001190 * @vol_id: the volume ID that last used this PEB
1191 * @lnum: the last used logical eraseblock number for the PEB
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001192 * @pnum: physical eraseblock to return
1193 * @torture: if this physical eraseblock has to be tortured
1194 *
1195 * This function is called to return physical eraseblock @pnum to the pool of
1196 * free physical eraseblocks. The @torture flag has to be set if an I/O error
1197 * occurred to this @pnum and it has to be tested. This function returns zero
1198 * in case of success, and a negative error code in case of failure.
1199 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001200int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum,
1201 int pnum, int torture)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001202{
1203 int err;
1204 struct ubi_wl_entry *e;
1205
1206 dbg_wl("PEB %d", pnum);
1207 ubi_assert(pnum >= 0);
1208 ubi_assert(pnum < ubi->peb_count);
1209
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001210 down_read(&ubi->fm_protect);
1211
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001212retry:
1213 spin_lock(&ubi->wl_lock);
1214 e = ubi->lookuptbl[pnum];
1215 if (e == ubi->move_from) {
1216 /*
1217 * User is putting the physical eraseblock which was selected to
1218 * be moved. It will be scheduled for erasure in the
1219 * wear-leveling worker.
1220 */
1221 dbg_wl("PEB %d is being moved, wait", pnum);
1222 spin_unlock(&ubi->wl_lock);
1223
1224 /* Wait for the WL worker by taking the @ubi->move_mutex */
1225 mutex_lock(&ubi->move_mutex);
1226 mutex_unlock(&ubi->move_mutex);
1227 goto retry;
1228 } else if (e == ubi->move_to) {
1229 /*
1230 * User is putting the physical eraseblock which was selected
1231 * as the target the data is moved to. It may happen if the EBA
Heiko Schocherff94bc42014-06-24 10:10:04 +02001232 * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()'
1233 * but the WL sub-system has not put the PEB to the "used" tree
1234 * yet, but it is about to do this. So we just set a flag which
1235 * will tell the WL worker that the PEB is not needed anymore
1236 * and should be scheduled for erasure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001237 */
1238 dbg_wl("PEB %d is the target of data moving", pnum);
1239 ubi_assert(!ubi->move_to_put);
1240 ubi->move_to_put = 1;
1241 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001242 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001243 return 0;
1244 } else {
1245 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001246 self_check_in_wl_tree(ubi, e, &ubi->used);
1247 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001248 } else if (in_wl_tree(e, &ubi->scrub)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001249 self_check_in_wl_tree(ubi, e, &ubi->scrub);
1250 rb_erase(&e->u.rb, &ubi->scrub);
1251 } else if (in_wl_tree(e, &ubi->erroneous)) {
1252 self_check_in_wl_tree(ubi, e, &ubi->erroneous);
1253 rb_erase(&e->u.rb, &ubi->erroneous);
1254 ubi->erroneous_peb_count -= 1;
1255 ubi_assert(ubi->erroneous_peb_count >= 0);
1256 /* Erroneous PEBs should be tortured */
1257 torture = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001258 } else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001259 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001260 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001261 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001262 ubi_ro_mode(ubi);
1263 spin_unlock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001264 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001265 return err;
1266 }
1267 }
1268 }
1269 spin_unlock(&ubi->wl_lock);
1270
Heiko Schocherff94bc42014-06-24 10:10:04 +02001271 err = schedule_erase(ubi, e, vol_id, lnum, torture);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001272 if (err) {
1273 spin_lock(&ubi->wl_lock);
1274 wl_tree_add(e, &ubi->used);
1275 spin_unlock(&ubi->wl_lock);
1276 }
1277
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001278 up_read(&ubi->fm_protect);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001279 return err;
1280}
1281
1282/**
1283 * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing.
1284 * @ubi: UBI device description object
1285 * @pnum: the physical eraseblock to schedule
1286 *
1287 * If a bit-flip in a physical eraseblock is detected, this physical eraseblock
1288 * needs scrubbing. This function schedules a physical eraseblock for
1289 * scrubbing which is done in background. This function returns zero in case of
1290 * success and a negative error code in case of failure.
1291 */
1292int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum)
1293{
1294 struct ubi_wl_entry *e;
1295
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001296 ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001297
1298retry:
1299 spin_lock(&ubi->wl_lock);
1300 e = ubi->lookuptbl[pnum];
Heiko Schocherff94bc42014-06-24 10:10:04 +02001301 if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) ||
1302 in_wl_tree(e, &ubi->erroneous)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001303 spin_unlock(&ubi->wl_lock);
1304 return 0;
1305 }
1306
1307 if (e == ubi->move_to) {
1308 /*
1309 * This physical eraseblock was used to move data to. The data
1310 * was moved but the PEB was not yet inserted to the proper
1311 * tree. We should just wait a little and let the WL worker
1312 * proceed.
1313 */
1314 spin_unlock(&ubi->wl_lock);
1315 dbg_wl("the PEB %d is not in proper tree, retry", pnum);
1316 yield();
1317 goto retry;
1318 }
1319
1320 if (in_wl_tree(e, &ubi->used)) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001321 self_check_in_wl_tree(ubi, e, &ubi->used);
1322 rb_erase(&e->u.rb, &ubi->used);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001323 } else {
1324 int err;
1325
Heiko Schocherff94bc42014-06-24 10:10:04 +02001326 err = prot_queue_del(ubi, e->pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001327 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001328 ubi_err(ubi, "PEB %d not found", pnum);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001329 ubi_ro_mode(ubi);
1330 spin_unlock(&ubi->wl_lock);
1331 return err;
1332 }
1333 }
1334
1335 wl_tree_add(e, &ubi->scrub);
1336 spin_unlock(&ubi->wl_lock);
1337
1338 /*
1339 * Technically scrubbing is the same as wear-leveling, so it is done
1340 * by the WL worker.
1341 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001342 return ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001343}
1344
1345/**
1346 * ubi_wl_flush - flush all pending works.
1347 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001348 * @vol_id: the volume id to flush for
1349 * @lnum: the logical eraseblock number to flush for
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001350 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001351 * This function executes all pending works for a particular volume id /
1352 * logical eraseblock number pair. If either value is set to %UBI_ALL, then it
1353 * acts as a wildcard for all of the corresponding volume numbers or logical
1354 * eraseblock numbers. It returns zero in case of success and a negative error
1355 * code in case of failure.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001356 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001357int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001358{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001359 int err = 0;
1360 int found = 1;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001361
1362 /*
Heiko Schocherff94bc42014-06-24 10:10:04 +02001363 * Erase while the pending works queue is not empty, but not more than
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001364 * the number of currently pending works.
1365 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001366 dbg_wl("flush pending work for LEB %d:%d (%d pending works)",
1367 vol_id, lnum, ubi->works_count);
1368
1369 while (found) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001370 struct ubi_work *wrk, *tmp;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001371 found = 0;
1372
1373 down_read(&ubi->work_sem);
1374 spin_lock(&ubi->wl_lock);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001375 list_for_each_entry_safe(wrk, tmp, &ubi->works, list) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001376 if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) &&
1377 (lnum == UBI_ALL || wrk->lnum == lnum)) {
1378 list_del(&wrk->list);
1379 ubi->works_count -= 1;
1380 ubi_assert(ubi->works_count >= 0);
1381 spin_unlock(&ubi->wl_lock);
1382
1383 err = wrk->func(ubi, wrk, 0);
1384 if (err) {
1385 up_read(&ubi->work_sem);
1386 return err;
1387 }
1388
1389 spin_lock(&ubi->wl_lock);
1390 found = 1;
1391 break;
1392 }
1393 }
1394 spin_unlock(&ubi->wl_lock);
1395 up_read(&ubi->work_sem);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001396 }
1397
1398 /*
1399 * Make sure all the works which have been done in parallel are
1400 * finished.
1401 */
1402 down_write(&ubi->work_sem);
1403 up_write(&ubi->work_sem);
1404
Heiko Schocherff94bc42014-06-24 10:10:04 +02001405 return err;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001406}
1407
1408/**
1409 * tree_destroy - destroy an RB-tree.
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001410 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001411 * @root: the root of the tree to destroy
1412 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001413static void tree_destroy(struct ubi_device *ubi, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001414{
1415 struct rb_node *rb;
1416 struct ubi_wl_entry *e;
1417
1418 rb = root->rb_node;
1419 while (rb) {
1420 if (rb->rb_left)
1421 rb = rb->rb_left;
1422 else if (rb->rb_right)
1423 rb = rb->rb_right;
1424 else {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001425 e = rb_entry(rb, struct ubi_wl_entry, u.rb);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001426
1427 rb = rb_parent(rb);
1428 if (rb) {
Heiko Schocherff94bc42014-06-24 10:10:04 +02001429 if (rb->rb_left == &e->u.rb)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001430 rb->rb_left = NULL;
1431 else
1432 rb->rb_right = NULL;
1433 }
1434
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001435 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001436 }
1437 }
1438}
1439
1440/**
1441 * ubi_thread - UBI background thread.
1442 * @u: the UBI device description object pointer
1443 */
1444int ubi_thread(void *u)
1445{
1446 int failures = 0;
1447 struct ubi_device *ubi = u;
1448
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001449 ubi_msg(ubi, "background thread \"%s\" started, PID %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001450 ubi->bgt_name, task_pid_nr(current));
1451
1452 set_freezable();
1453 for (;;) {
1454 int err;
1455
1456 if (kthread_should_stop())
1457 break;
1458
1459 if (try_to_freeze())
1460 continue;
1461
1462 spin_lock(&ubi->wl_lock);
1463 if (list_empty(&ubi->works) || ubi->ro_mode ||
Heiko Schocherff94bc42014-06-24 10:10:04 +02001464 !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001465 set_current_state(TASK_INTERRUPTIBLE);
1466 spin_unlock(&ubi->wl_lock);
1467 schedule();
1468 continue;
1469 }
1470 spin_unlock(&ubi->wl_lock);
1471
1472 err = do_work(ubi);
1473 if (err) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001474 ubi_err(ubi, "%s: work failed with error code %d",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001475 ubi->bgt_name, err);
1476 if (failures++ > WL_MAX_FAILURES) {
1477 /*
1478 * Too many failures, disable the thread and
1479 * switch to read-only mode.
1480 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001481 ubi_msg(ubi, "%s: %d consecutive failures",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001482 ubi->bgt_name, WL_MAX_FAILURES);
1483 ubi_ro_mode(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001484 ubi->thread_enabled = 0;
1485 continue;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001486 }
1487 } else
1488 failures = 0;
1489
1490 cond_resched();
1491 }
1492
1493 dbg_wl("background thread \"%s\" is killed", ubi->bgt_name);
1494 return 0;
1495}
1496
1497/**
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001498 * shutdown_work - shutdown all pending works.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001499 * @ubi: UBI device description object
1500 */
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001501static void shutdown_work(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001502{
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001503#ifdef CONFIG_MTD_UBI_FASTMAP
1504#ifndef __UBOOT__
1505 flush_work(&ubi->fm_work);
1506#else
1507 /* in U-Boot, we have all work done */
1508#endif
1509#endif
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001510 while (!list_empty(&ubi->works)) {
1511 struct ubi_work *wrk;
1512
1513 wrk = list_entry(ubi->works.next, struct ubi_work, list);
1514 list_del(&wrk->list);
1515 wrk->func(ubi, wrk, 1);
1516 ubi->works_count -= 1;
1517 ubi_assert(ubi->works_count >= 0);
1518 }
1519}
1520
1521/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001522 * ubi_wl_init - initialize the WL sub-system using attaching information.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001523 * @ubi: UBI device description object
Heiko Schocherff94bc42014-06-24 10:10:04 +02001524 * @ai: attaching information
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001525 *
1526 * This function returns zero in case of success, and a negative error code in
1527 * case of failure.
1528 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001529int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001530{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001531 int err, i, reserved_pebs, found_pebs = 0;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001532 struct rb_node *rb1, *rb2;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001533 struct ubi_ainf_volume *av;
1534 struct ubi_ainf_peb *aeb, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001535 struct ubi_wl_entry *e;
1536
Heiko Schocherff94bc42014-06-24 10:10:04 +02001537 ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001538 spin_lock_init(&ubi->wl_lock);
1539 mutex_init(&ubi->move_mutex);
1540 init_rwsem(&ubi->work_sem);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001541 ubi->max_ec = ai->max_ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001542 INIT_LIST_HEAD(&ubi->works);
1543
1544 sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num);
1545
1546 err = -ENOMEM;
1547 ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL);
1548 if (!ubi->lookuptbl)
1549 return err;
1550
Heiko Schocherff94bc42014-06-24 10:10:04 +02001551 for (i = 0; i < UBI_PROT_QUEUE_LEN; i++)
1552 INIT_LIST_HEAD(&ubi->pq[i]);
1553 ubi->pq_head = 0;
1554
Heiko Schocher68fc4492016-02-02 11:54:35 +01001555 ubi->free_count = 0;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001556 list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001557 cond_resched();
1558
1559 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1560 if (!e)
1561 goto out_free;
1562
Heiko Schocherff94bc42014-06-24 10:10:04 +02001563 e->pnum = aeb->pnum;
1564 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001565 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001566 if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001567 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001568 goto out_free;
1569 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001570
1571 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001572 }
1573
Heiko Schocherff94bc42014-06-24 10:10:04 +02001574 list_for_each_entry(aeb, &ai->free, u.list) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001575 cond_resched();
1576
1577 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1578 if (!e)
1579 goto out_free;
1580
Heiko Schocherff94bc42014-06-24 10:10:04 +02001581 e->pnum = aeb->pnum;
1582 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001583 ubi_assert(e->ec >= 0);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001584
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001585 wl_tree_add(e, &ubi->free);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001586 ubi->free_count++;
1587
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001588 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001589
1590 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001591 }
1592
Heiko Schocherff94bc42014-06-24 10:10:04 +02001593 ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) {
1594 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001595 cond_resched();
1596
1597 e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL);
1598 if (!e)
1599 goto out_free;
1600
Heiko Schocherff94bc42014-06-24 10:10:04 +02001601 e->pnum = aeb->pnum;
1602 e->ec = aeb->ec;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001603 ubi->lookuptbl[e->pnum] = e;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001604
1605 if (!aeb->scrub) {
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001606 dbg_wl("add PEB %d EC %d to the used tree",
1607 e->pnum, e->ec);
1608 wl_tree_add(e, &ubi->used);
1609 } else {
1610 dbg_wl("add PEB %d EC %d to the scrub tree",
1611 e->pnum, e->ec);
1612 wl_tree_add(e, &ubi->scrub);
1613 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001614
1615 found_pebs++;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001616 }
1617 }
1618
Heiko Schocherff94bc42014-06-24 10:10:04 +02001619 dbg_wl("found %i PEBs", found_pebs);
1620
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001621 if (ubi->fm) {
1622 ubi_assert(ubi->good_peb_count ==
Heiko Schocherff94bc42014-06-24 10:10:04 +02001623 found_pebs + ubi->fm->used_blocks);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001624
1625 for (i = 0; i < ubi->fm->used_blocks; i++) {
1626 e = ubi->fm->e[i];
1627 ubi->lookuptbl[e->pnum] = e;
1628 }
1629 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001630 else
1631 ubi_assert(ubi->good_peb_count == found_pebs);
1632
1633 reserved_pebs = WL_RESERVED_PEBS;
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001634 ubi_fastmap_init(ubi, &reserved_pebs);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001635
1636 if (ubi->avail_pebs < reserved_pebs) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001637 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001638 ubi->avail_pebs, reserved_pebs);
1639 if (ubi->corr_peb_count)
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001640 ubi_err(ubi, "%d PEBs are corrupted and not used",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001641 ubi->corr_peb_count);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001642 goto out_free;
1643 }
Heiko Schocherff94bc42014-06-24 10:10:04 +02001644 ubi->avail_pebs -= reserved_pebs;
1645 ubi->rsvd_pebs += reserved_pebs;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001646
1647 /* Schedule wear-leveling if needed */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001648 err = ensure_wear_leveling(ubi, 0);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001649 if (err)
1650 goto out_free;
1651
1652 return 0;
1653
1654out_free:
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001655 shutdown_work(ubi);
1656 tree_destroy(ubi, &ubi->used);
1657 tree_destroy(ubi, &ubi->free);
1658 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001659 kfree(ubi->lookuptbl);
1660 return err;
1661}
1662
1663/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001664 * protection_queue_destroy - destroy the protection queue.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001665 * @ubi: UBI device description object
1666 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001667static void protection_queue_destroy(struct ubi_device *ubi)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001668{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001669 int i;
1670 struct ubi_wl_entry *e, *tmp;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001671
Heiko Schocherff94bc42014-06-24 10:10:04 +02001672 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) {
1673 list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) {
1674 list_del(&e->u.list);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001675 wl_entry_destroy(ubi, e);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001676 }
1677 }
1678}
1679
1680/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001681 * ubi_wl_close - close the wear-leveling sub-system.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001682 * @ubi: UBI device description object
1683 */
1684void ubi_wl_close(struct ubi_device *ubi)
1685{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001686 dbg_wl("close the WL sub-system");
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001687 ubi_fastmap_close(ubi);
1688 shutdown_work(ubi);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001689 protection_queue_destroy(ubi);
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001690 tree_destroy(ubi, &ubi->used);
1691 tree_destroy(ubi, &ubi->erroneous);
1692 tree_destroy(ubi, &ubi->free);
1693 tree_destroy(ubi, &ubi->scrub);
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001694 kfree(ubi->lookuptbl);
1695}
1696
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001697/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001698 * self_check_ec - make sure that the erase counter of a PEB is correct.
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001699 * @ubi: UBI device description object
1700 * @pnum: the physical eraseblock number to check
1701 * @ec: the erase counter to check
1702 *
1703 * This function returns zero if the erase counter of physical eraseblock @pnum
Heiko Schocherff94bc42014-06-24 10:10:04 +02001704 * is equivalent to @ec, and a negative error code if not or if an error
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001705 * occurred.
1706 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001707static int self_check_ec(struct ubi_device *ubi, int pnum, int ec)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001708{
1709 int err;
1710 long long read_ec;
1711 struct ubi_ec_hdr *ec_hdr;
1712
Heiko Schocherff94bc42014-06-24 10:10:04 +02001713 if (!ubi_dbg_chk_gen(ubi))
1714 return 0;
1715
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001716 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
1717 if (!ec_hdr)
1718 return -ENOMEM;
1719
1720 err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0);
1721 if (err && err != UBI_IO_BITFLIPS) {
1722 /* The header does not have to exist */
1723 err = 0;
1724 goto out_free;
1725 }
1726
1727 read_ec = be64_to_cpu(ec_hdr->ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001728 if (ec != read_ec && read_ec - ec > 1) {
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001729 ubi_err(ubi, "self-check failed for PEB %d", pnum);
1730 ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001731 dump_stack();
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001732 err = 1;
1733 } else
1734 err = 0;
1735
1736out_free:
1737 kfree(ec_hdr);
1738 return err;
1739}
1740
1741/**
Heiko Schocherff94bc42014-06-24 10:10:04 +02001742 * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree.
1743 * @ubi: UBI device description object
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001744 * @e: the wear-leveling entry to check
1745 * @root: the root of the tree
1746 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02001747 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001748 * is not.
1749 */
Heiko Schocherff94bc42014-06-24 10:10:04 +02001750static int self_check_in_wl_tree(const struct ubi_device *ubi,
1751 struct ubi_wl_entry *e, struct rb_root *root)
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001752{
Heiko Schocherff94bc42014-06-24 10:10:04 +02001753 if (!ubi_dbg_chk_gen(ubi))
1754 return 0;
1755
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001756 if (in_wl_tree(e, root))
1757 return 0;
1758
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001759 ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ",
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001760 e->pnum, e->ec, root);
Heiko Schocherff94bc42014-06-24 10:10:04 +02001761 dump_stack();
1762 return -EINVAL;
Kyungmin Parkc91a7192008-11-19 16:28:06 +01001763}
1764
Heiko Schocherff94bc42014-06-24 10:10:04 +02001765/**
1766 * self_check_in_pq - check if wear-leveling entry is in the protection
1767 * queue.
1768 * @ubi: UBI device description object
1769 * @e: the wear-leveling entry to check
1770 *
1771 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
1772 */
1773static int self_check_in_pq(const struct ubi_device *ubi,
1774 struct ubi_wl_entry *e)
1775{
1776 struct ubi_wl_entry *p;
1777 int i;
1778
1779 if (!ubi_dbg_chk_gen(ubi))
1780 return 0;
1781
1782 for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i)
1783 list_for_each_entry(p, &ubi->pq[i], u.list)
1784 if (p == e)
1785 return 0;
1786
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001787 ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue",
Heiko Schocherff94bc42014-06-24 10:10:04 +02001788 e->pnum, e->ec);
1789 dump_stack();
1790 return -EINVAL;
1791}
Heiko Schocher0195a7b2015-10-22 06:19:21 +02001792#ifndef CONFIG_MTD_UBI_FASTMAP
1793static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
1794{
1795 struct ubi_wl_entry *e;
1796
1797 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
1798 self_check_in_wl_tree(ubi, e, &ubi->free);
1799 ubi->free_count--;
1800 ubi_assert(ubi->free_count >= 0);
1801 rb_erase(&e->u.rb, &ubi->free);
1802
1803 return e;
1804}
1805
1806/**
1807 * produce_free_peb - produce a free physical eraseblock.
1808 * @ubi: UBI device description object
1809 *
1810 * This function tries to make a free PEB by means of synchronous execution of
1811 * pending works. This may be needed if, for example the background thread is
1812 * disabled. Returns zero in case of success and a negative error code in case
1813 * of failure.
1814 */
1815static int produce_free_peb(struct ubi_device *ubi)
1816{
1817 int err;
1818
1819 while (!ubi->free.rb_node && ubi->works_count) {
1820 spin_unlock(&ubi->wl_lock);
1821
1822 dbg_wl("do one work synchronously");
1823 err = do_work(ubi);
1824
1825 spin_lock(&ubi->wl_lock);
1826 if (err)
1827 return err;
1828 }
1829
1830 return 0;
1831}
1832
1833/**
1834 * ubi_wl_get_peb - get a physical eraseblock.
1835 * @ubi: UBI device description object
1836 *
1837 * This function returns a physical eraseblock in case of success and a
1838 * negative error code in case of failure.
1839 * Returns with ubi->fm_eba_sem held in read mode!
1840 */
1841int ubi_wl_get_peb(struct ubi_device *ubi)
1842{
1843 int err;
1844 struct ubi_wl_entry *e;
1845
1846retry:
1847 down_read(&ubi->fm_eba_sem);
1848 spin_lock(&ubi->wl_lock);
1849 if (!ubi->free.rb_node) {
1850 if (ubi->works_count == 0) {
1851 ubi_err(ubi, "no free eraseblocks");
1852 ubi_assert(list_empty(&ubi->works));
1853 spin_unlock(&ubi->wl_lock);
1854 return -ENOSPC;
1855 }
1856
1857 err = produce_free_peb(ubi);
1858 if (err < 0) {
1859 spin_unlock(&ubi->wl_lock);
1860 return err;
1861 }
1862 spin_unlock(&ubi->wl_lock);
1863 up_read(&ubi->fm_eba_sem);
1864 goto retry;
1865
1866 }
1867 e = wl_get_wle(ubi);
1868 prot_queue_add(ubi, e);
1869 spin_unlock(&ubi->wl_lock);
1870
1871 err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset,
1872 ubi->peb_size - ubi->vid_hdr_aloffset);
1873 if (err) {
1874 ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum);
1875 return err;
1876 }
1877
1878 return e->pnum;
1879}
1880#else
1881#include "fastmap-wl.c"
1882#endif