1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2012 Linutronix GmbH
4 * Copyright (c) 2014 sigma star gmbh
5 * Author: Richard Weinberger <richard@nod.at>
9 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
10 * @wrk: the work description object
12 static void update_fastmap_work_fn(struct work_struct *wrk)
14 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
16 ubi_update_fastmap(ubi);
17 spin_lock(&ubi->wl_lock);
18 ubi->fm_work_scheduled = 0;
19 spin_unlock(&ubi->wl_lock);
23 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
24 * @root: the RB-tree where to look for
26 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
29 struct ubi_wl_entry *e, *victim = NULL;
30 int max_ec = UBI_MAX_ERASECOUNTER;
32 ubi_rb_for_each_entry(p, e, root, u.rb) {
33 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
42 static inline void return_unused_peb(struct ubi_device *ubi,
43 struct ubi_wl_entry *e)
45 wl_tree_add(e, &ubi->free);
50 * return_unused_pool_pebs - returns unused PEB to the free tree.
51 * @ubi: UBI device description object
52 * @pool: fastmap pool description object
54 static void return_unused_pool_pebs(struct ubi_device *ubi,
55 struct ubi_fm_pool *pool)
58 struct ubi_wl_entry *e;
60 for (i = pool->used; i < pool->size; i++) {
61 e = ubi->lookuptbl[pool->pebs[i]];
62 return_unused_peb(ubi, e);
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
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!
75 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
77 struct ubi_wl_entry *e = NULL;
79 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
83 e = find_anchor_wl_entry(&ubi->free);
85 e = find_mean_wl_entry(ubi, &ubi->free);
90 self_check_in_wl_tree(ubi, e, &ubi->free);
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);
101 * has_enough_free_count - whether ubi has enough free pebs to fill fm pools
102 * @ubi: UBI device description object
103 * @is_wl_pool: whether UBI is filling wear leveling pool
105 * This helper function checks whether there are enough free pebs (deducted
106 * by fastmap pebs) to fill fm_pool and fm_wl_pool, above rule works after
107 * there is at least one of free pebs is filled into fm_wl_pool.
108 * For wear leveling pool, UBI should also reserve free pebs for bad pebs
109 * handling, because there maybe no enough free pebs for user volumes after
110 * producing new bad pebs.
112 static bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool)
114 int fm_used = 0; // fastmap non anchor pebs.
117 if (!ubi->free.rb_node)
120 beb_rsvd_pebs = is_wl_pool ? ubi->beb_rsvd_pebs : 0;
121 if (ubi->fm_wl_pool.size > 0 && !(ubi->ro_mode || ubi->fm_disabled))
122 fm_used = ubi->fm_size / ubi->leb_size - 1;
124 return ubi->free_count - beb_rsvd_pebs > fm_used;
128 * ubi_refill_pools - refills all fastmap PEB pools.
129 * @ubi: UBI device description object
131 void ubi_refill_pools(struct ubi_device *ubi)
133 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
134 struct ubi_fm_pool *pool = &ubi->fm_pool;
135 struct ubi_wl_entry *e;
138 spin_lock(&ubi->wl_lock);
140 return_unused_pool_pebs(ubi, wl_pool);
141 return_unused_pool_pebs(ubi, pool);
146 if (ubi->fm_anchor) {
147 wl_tree_add(ubi->fm_anchor, &ubi->free);
149 ubi->fm_anchor = NULL;
152 if (!ubi->fm_disabled)
154 * All available PEBs are in ubi->free, now is the time to get
155 * the best anchor PEBs.
157 ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1);
161 if (pool->size < pool->max_size) {
162 if (!has_enough_free_count(ubi, false))
169 pool->pebs[pool->size] = e->pnum;
174 if (wl_pool->size < wl_pool->max_size) {
175 if (!has_enough_free_count(ubi, true))
178 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
179 self_check_in_wl_tree(ubi, e, &ubi->free);
180 rb_erase(&e->u.rb, &ubi->free);
183 wl_pool->pebs[wl_pool->size] = e->pnum;
195 spin_unlock(&ubi->wl_lock);
199 * produce_free_peb - produce a free physical eraseblock.
200 * @ubi: UBI device description object
202 * This function tries to make a free PEB by means of synchronous execution of
203 * pending works. This may be needed if, for example the background thread is
204 * disabled. Returns zero in case of success and a negative error code in case
207 static int produce_free_peb(struct ubi_device *ubi)
211 while (!ubi->free.rb_node && ubi->works_count) {
212 dbg_wl("do one work synchronously");
223 * ubi_wl_get_peb - get a physical eraseblock.
224 * @ubi: UBI device description object
226 * This function returns a physical eraseblock in case of success and a
227 * negative error code in case of failure.
228 * Returns with ubi->fm_eba_sem held in read mode!
230 int ubi_wl_get_peb(struct ubi_device *ubi)
232 int ret, attempts = 0;
233 struct ubi_fm_pool *pool = &ubi->fm_pool;
234 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
237 down_read(&ubi->fm_eba_sem);
238 spin_lock(&ubi->wl_lock);
240 /* We check here also for the WL pool because at this point we can
241 * refill the WL pool synchronous. */
242 if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
243 spin_unlock(&ubi->wl_lock);
244 up_read(&ubi->fm_eba_sem);
245 ret = ubi_update_fastmap(ubi);
247 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
248 down_read(&ubi->fm_eba_sem);
251 down_read(&ubi->fm_eba_sem);
252 spin_lock(&ubi->wl_lock);
255 if (pool->used == pool->size) {
256 spin_unlock(&ubi->wl_lock);
258 if (attempts == 10) {
259 ubi_err(ubi, "Unable to get a free PEB from user WL pool");
263 up_read(&ubi->fm_eba_sem);
264 ret = produce_free_peb(ubi);
266 down_read(&ubi->fm_eba_sem);
272 ubi_assert(pool->used < pool->size);
273 ret = pool->pebs[pool->used++];
274 prot_queue_add(ubi, ubi->lookuptbl[ret]);
275 spin_unlock(&ubi->wl_lock);
280 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
282 * @ubi: UBI device description object
284 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
286 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
289 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
291 if (pool->used == pool->size) {
292 /* We cannot update the fastmap here because this
293 * function is called in atomic context.
294 * Let's fail here and refill/update it as soon as possible. */
295 if (!ubi->fm_work_scheduled) {
296 ubi->fm_work_scheduled = 1;
297 schedule_work(&ubi->fm_work);
302 pnum = pool->pebs[pool->used++];
303 return ubi->lookuptbl[pnum];
307 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
308 * @ubi: UBI device description object
310 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
312 struct ubi_work *wrk;
313 struct ubi_wl_entry *anchor;
315 spin_lock(&ubi->wl_lock);
317 /* Do we already have an anchor? */
318 if (ubi->fm_anchor) {
319 spin_unlock(&ubi->wl_lock);
323 /* See if we can find an anchor PEB on the list of free PEBs */
324 anchor = ubi_wl_get_fm_peb(ubi, 1);
326 ubi->fm_anchor = anchor;
327 spin_unlock(&ubi->wl_lock);
331 ubi->fm_do_produce_anchor = 1;
332 /* No luck, trigger wear leveling to produce a new anchor PEB. */
333 if (ubi->wl_scheduled) {
334 spin_unlock(&ubi->wl_lock);
337 ubi->wl_scheduled = 1;
338 spin_unlock(&ubi->wl_lock);
340 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
342 spin_lock(&ubi->wl_lock);
343 ubi->wl_scheduled = 0;
344 spin_unlock(&ubi->wl_lock);
348 wrk->func = &wear_leveling_worker;
349 __schedule_ubi_work(ubi, wrk);
354 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
356 * see: ubi_wl_put_peb()
358 * @ubi: UBI device description object
359 * @fm_e: physical eraseblock to return
360 * @lnum: the last used logical eraseblock number for the PEB
361 * @torture: if this physical eraseblock has to be tortured
363 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
364 int lnum, int torture)
366 struct ubi_wl_entry *e;
367 int vol_id, pnum = fm_e->pnum;
369 dbg_wl("PEB %d", pnum);
371 ubi_assert(pnum >= 0);
372 ubi_assert(pnum < ubi->peb_count);
374 spin_lock(&ubi->wl_lock);
375 e = ubi->lookuptbl[pnum];
377 /* This can happen if we recovered from a fastmap the very
378 * first time and writing now a new one. In this case the wl system
379 * has never seen any PEB used by the original fastmap.
383 ubi_assert(e->ec >= 0);
384 ubi->lookuptbl[pnum] = e;
387 spin_unlock(&ubi->wl_lock);
389 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
390 return schedule_erase(ubi, e, vol_id, lnum, torture, true);
394 * ubi_is_erase_work - checks whether a work is erase work.
395 * @wrk: The work object to be checked
397 int ubi_is_erase_work(struct ubi_work *wrk)
399 return wrk->func == erase_worker;
402 static void ubi_fastmap_close(struct ubi_device *ubi)
406 return_unused_pool_pebs(ubi, &ubi->fm_pool);
407 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
409 if (ubi->fm_anchor) {
410 return_unused_peb(ubi, ubi->fm_anchor);
411 ubi->fm_anchor = NULL;
415 for (i = 0; i < ubi->fm->used_blocks; i++)
416 kfree(ubi->fm->e[i]);
422 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
423 * See find_mean_wl_entry()
425 * @ubi: UBI device description object
426 * @e: physical eraseblock to return
427 * @root: RB tree to test against.
429 static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
430 struct ubi_wl_entry *e,
431 struct rb_root *root) {
432 if (e && !ubi->fm_disabled && !ubi->fm &&
433 e->pnum < UBI_FM_MAX_START)
434 e = rb_entry(rb_next(root->rb_node),
435 struct ubi_wl_entry, u.rb);