blob: b486250923c5a37af907e5001989518dab6f07bf [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-only
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Copyright (c) 2012 Linutronix GmbH
4 * Copyright (c) 2014 sigma star gmbh
5 * Author: Richard Weinberger <richard@nod.at>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8/**
9 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
10 * @wrk: the work description object
11 */
12static void update_fastmap_work_fn(struct work_struct *wrk)
13{
14 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
15
16 ubi_update_fastmap(ubi);
17 spin_lock(&ubi->wl_lock);
18 ubi->fm_work_scheduled = 0;
19 spin_unlock(&ubi->wl_lock);
20}
21
22/**
23 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
24 * @root: the RB-tree where to look for
25 */
26static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
27{
28 struct rb_node *p;
29 struct ubi_wl_entry *e, *victim = NULL;
30 int max_ec = UBI_MAX_ERASECOUNTER;
31
32 ubi_rb_for_each_entry(p, e, root, u.rb) {
33 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
34 victim = e;
35 max_ec = e->ec;
36 }
37 }
38
39 return victim;
40}
41
Olivier Deprez0e641232021-09-23 10:07:05 +020042static inline void return_unused_peb(struct ubi_device *ubi,
43 struct ubi_wl_entry *e)
44{
45 wl_tree_add(e, &ubi->free);
46 ubi->free_count++;
47}
48
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000049/**
50 * return_unused_pool_pebs - returns unused PEB to the free tree.
51 * @ubi: UBI device description object
52 * @pool: fastmap pool description object
53 */
54static void return_unused_pool_pebs(struct ubi_device *ubi,
55 struct ubi_fm_pool *pool)
56{
57 int i;
58 struct ubi_wl_entry *e;
59
60 for (i = pool->used; i < pool->size; i++) {
61 e = ubi->lookuptbl[pool->pebs[i]];
Olivier Deprez0e641232021-09-23 10:07:05 +020062 return_unused_peb(ubi, e);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063 }
64}
65
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000066/**
67 * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
68 * @ubi: UBI device description object
69 * @anchor: This PEB will be used as anchor PEB by fastmap
70 *
71 * The function returns a physical erase block with a given maximal number
72 * and removes it from the wl subsystem.
73 * Must be called with wl_lock held!
74 */
75struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
76{
77 struct ubi_wl_entry *e = NULL;
78
79 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
80 goto out;
81
82 if (anchor)
83 e = find_anchor_wl_entry(&ubi->free);
84 else
85 e = find_mean_wl_entry(ubi, &ubi->free);
86
87 if (!e)
88 goto out;
89
90 self_check_in_wl_tree(ubi, e, &ubi->free);
91
92 /* remove it from the free list,
93 * the wl subsystem does no longer know this erase block */
94 rb_erase(&e->u.rb, &ubi->free);
95 ubi->free_count--;
96out:
97 return e;
98}
99
100/**
101 * ubi_refill_pools - refills all fastmap PEB pools.
102 * @ubi: UBI device description object
103 */
104void ubi_refill_pools(struct ubi_device *ubi)
105{
106 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
107 struct ubi_fm_pool *pool = &ubi->fm_pool;
108 struct ubi_wl_entry *e;
109 int enough;
110
111 spin_lock(&ubi->wl_lock);
112
113 return_unused_pool_pebs(ubi, wl_pool);
114 return_unused_pool_pebs(ubi, pool);
115
116 wl_pool->size = 0;
117 pool->size = 0;
118
119 for (;;) {
120 enough = 0;
121 if (pool->size < pool->max_size) {
122 if (!ubi->free.rb_node)
123 break;
124
125 e = wl_get_wle(ubi);
126 if (!e)
127 break;
128
129 pool->pebs[pool->size] = e->pnum;
130 pool->size++;
131 } else
132 enough++;
133
134 if (wl_pool->size < wl_pool->max_size) {
135 if (!ubi->free.rb_node ||
136 (ubi->free_count - ubi->beb_rsvd_pebs < 5))
137 break;
138
139 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
140 self_check_in_wl_tree(ubi, e, &ubi->free);
141 rb_erase(&e->u.rb, &ubi->free);
142 ubi->free_count--;
143
144 wl_pool->pebs[wl_pool->size] = e->pnum;
145 wl_pool->size++;
146 } else
147 enough++;
148
149 if (enough == 2)
150 break;
151 }
152
153 wl_pool->used = 0;
154 pool->used = 0;
155
156 spin_unlock(&ubi->wl_lock);
157}
158
159/**
160 * produce_free_peb - produce a free physical eraseblock.
161 * @ubi: UBI device description object
162 *
163 * This function tries to make a free PEB by means of synchronous execution of
164 * pending works. This may be needed if, for example the background thread is
165 * disabled. Returns zero in case of success and a negative error code in case
166 * of failure.
167 */
168static int produce_free_peb(struct ubi_device *ubi)
169{
170 int err;
171
172 while (!ubi->free.rb_node && ubi->works_count) {
173 dbg_wl("do one work synchronously");
174 err = do_work(ubi);
175
176 if (err)
177 return err;
178 }
179
180 return 0;
181}
182
183/**
184 * ubi_wl_get_peb - get a physical eraseblock.
185 * @ubi: UBI device description object
186 *
187 * This function returns a physical eraseblock in case of success and a
188 * negative error code in case of failure.
189 * Returns with ubi->fm_eba_sem held in read mode!
190 */
191int ubi_wl_get_peb(struct ubi_device *ubi)
192{
David Brazdil0f672f62019-12-10 10:32:29 +0000193 int ret, attempts = 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000194 struct ubi_fm_pool *pool = &ubi->fm_pool;
195 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
196
197again:
198 down_read(&ubi->fm_eba_sem);
199 spin_lock(&ubi->wl_lock);
200
201 /* We check here also for the WL pool because at this point we can
202 * refill the WL pool synchronous. */
203 if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
204 spin_unlock(&ubi->wl_lock);
205 up_read(&ubi->fm_eba_sem);
206 ret = ubi_update_fastmap(ubi);
207 if (ret) {
208 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
209 down_read(&ubi->fm_eba_sem);
210 return -ENOSPC;
211 }
212 down_read(&ubi->fm_eba_sem);
213 spin_lock(&ubi->wl_lock);
214 }
215
216 if (pool->used == pool->size) {
217 spin_unlock(&ubi->wl_lock);
David Brazdil0f672f62019-12-10 10:32:29 +0000218 attempts++;
219 if (attempts == 10) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220 ubi_err(ubi, "Unable to get a free PEB from user WL pool");
221 ret = -ENOSPC;
222 goto out;
223 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000224 up_read(&ubi->fm_eba_sem);
225 ret = produce_free_peb(ubi);
226 if (ret < 0) {
227 down_read(&ubi->fm_eba_sem);
228 goto out;
229 }
230 goto again;
231 }
232
233 ubi_assert(pool->used < pool->size);
234 ret = pool->pebs[pool->used++];
235 prot_queue_add(ubi, ubi->lookuptbl[ret]);
236 spin_unlock(&ubi->wl_lock);
237out:
238 return ret;
239}
240
241/* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
242 *
243 * @ubi: UBI device description object
244 */
245static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
246{
247 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
248 int pnum;
249
250 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
251
252 if (pool->used == pool->size) {
253 /* We cannot update the fastmap here because this
254 * function is called in atomic context.
255 * Let's fail here and refill/update it as soon as possible. */
256 if (!ubi->fm_work_scheduled) {
257 ubi->fm_work_scheduled = 1;
258 schedule_work(&ubi->fm_work);
259 }
260 return NULL;
261 }
262
263 pnum = pool->pebs[pool->used++];
264 return ubi->lookuptbl[pnum];
265}
266
267/**
268 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
269 * @ubi: UBI device description object
270 */
271int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
272{
273 struct ubi_work *wrk;
Olivier Deprez0e641232021-09-23 10:07:05 +0200274 struct ubi_wl_entry *anchor;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000275
276 spin_lock(&ubi->wl_lock);
Olivier Deprez0e641232021-09-23 10:07:05 +0200277
278 /* Do we already have an anchor? */
279 if (ubi->fm_anchor) {
280 spin_unlock(&ubi->wl_lock);
281 return 0;
282 }
283
284 /* See if we can find an anchor PEB on the list of free PEBs */
285 anchor = ubi_wl_get_fm_peb(ubi, 1);
286 if (anchor) {
287 ubi->fm_anchor = anchor;
288 spin_unlock(&ubi->wl_lock);
289 return 0;
290 }
291
292 /* No luck, trigger wear leveling to produce a new anchor PEB */
293 ubi->fm_do_produce_anchor = 1;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000294 if (ubi->wl_scheduled) {
295 spin_unlock(&ubi->wl_lock);
296 return 0;
297 }
298 ubi->wl_scheduled = 1;
299 spin_unlock(&ubi->wl_lock);
300
301 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
302 if (!wrk) {
303 spin_lock(&ubi->wl_lock);
304 ubi->wl_scheduled = 0;
305 spin_unlock(&ubi->wl_lock);
306 return -ENOMEM;
307 }
308
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000309 wrk->func = &wear_leveling_worker;
310 __schedule_ubi_work(ubi, wrk);
311 return 0;
312}
313
314/**
315 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
316 * sub-system.
317 * see: ubi_wl_put_peb()
318 *
319 * @ubi: UBI device description object
320 * @fm_e: physical eraseblock to return
321 * @lnum: the last used logical eraseblock number for the PEB
322 * @torture: if this physical eraseblock has to be tortured
323 */
324int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
325 int lnum, int torture)
326{
327 struct ubi_wl_entry *e;
328 int vol_id, pnum = fm_e->pnum;
329
330 dbg_wl("PEB %d", pnum);
331
332 ubi_assert(pnum >= 0);
333 ubi_assert(pnum < ubi->peb_count);
334
335 spin_lock(&ubi->wl_lock);
336 e = ubi->lookuptbl[pnum];
337
338 /* This can happen if we recovered from a fastmap the very
339 * first time and writing now a new one. In this case the wl system
340 * has never seen any PEB used by the original fastmap.
341 */
342 if (!e) {
343 e = fm_e;
344 ubi_assert(e->ec >= 0);
345 ubi->lookuptbl[pnum] = e;
346 }
347
348 spin_unlock(&ubi->wl_lock);
349
350 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
351 return schedule_erase(ubi, e, vol_id, lnum, torture, true);
352}
353
354/**
355 * ubi_is_erase_work - checks whether a work is erase work.
356 * @wrk: The work object to be checked
357 */
358int ubi_is_erase_work(struct ubi_work *wrk)
359{
360 return wrk->func == erase_worker;
361}
362
363static void ubi_fastmap_close(struct ubi_device *ubi)
364{
365 int i;
366
367 return_unused_pool_pebs(ubi, &ubi->fm_pool);
368 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
369
Olivier Deprez0e641232021-09-23 10:07:05 +0200370 if (ubi->fm_anchor) {
371 return_unused_peb(ubi, ubi->fm_anchor);
372 ubi->fm_anchor = NULL;
373 }
374
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000375 if (ubi->fm) {
376 for (i = 0; i < ubi->fm->used_blocks; i++)
377 kfree(ubi->fm->e[i]);
378 }
379 kfree(ubi->fm);
380}
381
382/**
383 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
384 * See find_mean_wl_entry()
385 *
386 * @ubi: UBI device description object
387 * @e: physical eraseblock to return
388 * @root: RB tree to test against.
389 */
390static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
391 struct ubi_wl_entry *e,
392 struct rb_root *root) {
393 if (e && !ubi->fm_disabled && !ubi->fm &&
394 e->pnum < UBI_FM_MAX_START)
395 e = rb_entry(rb_next(root->rb_node),
396 struct ubi_wl_entry, u.rb);
397
398 return e;
399}