Lines Matching +full:wear +full:- +full:leveling
1 // SPDX-License-Identifier: GPL-2.0-only
9 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
17 spin_lock(&ubi->wl_lock); in update_fastmap_work_fn()
18 ubi->fm_work_scheduled = 0; in update_fastmap_work_fn()
19 spin_unlock(&ubi->wl_lock); in update_fastmap_work_fn()
23 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
24 * @root: the RB-tree where to look for
33 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) { in find_anchor_wl_entry()
35 max_ec = e->ec; in find_anchor_wl_entry()
45 wl_tree_add(e, &ubi->free); in return_unused_peb()
46 ubi->free_count++; in return_unused_peb()
50 * return_unused_pool_pebs - returns unused PEB to the free tree.
60 for (i = pool->used; i < pool->size; i++) { in return_unused_pool_pebs()
61 e = ubi->lookuptbl[pool->pebs[i]]; in return_unused_pool_pebs()
67 * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
79 if (!ubi->free.rb_node) in ubi_wl_get_fm_peb()
83 e = find_anchor_wl_entry(&ubi->free); in ubi_wl_get_fm_peb()
85 e = find_mean_wl_entry(ubi, &ubi->free); in ubi_wl_get_fm_peb()
90 self_check_in_wl_tree(ubi, e, &ubi->free); in ubi_wl_get_fm_peb()
94 rb_erase(&e->u.rb, &ubi->free); in ubi_wl_get_fm_peb()
95 ubi->free_count--; in ubi_wl_get_fm_peb()
101 * wait_free_pebs_for_pool - wait until there enough free pebs
110 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in wait_free_pebs_for_pool()
111 struct ubi_fm_pool *pool = &ubi->fm_pool; in wait_free_pebs_for_pool()
117 * 3. fm pebs - 1: Twice fastmap size deducted by fastmap and fm_anchor in wait_free_pebs_for_pool()
118 * 4. beb_rsvd_pebs: This value should be get under lock ubi->wl_lock in wait_free_pebs_for_pool()
121 ubi->fm_size / ubi->leb_size - 1 + ubi->fm_pool_rsv_cnt; in wait_free_pebs_for_pool()
124 spin_lock(&ubi->wl_lock); in wait_free_pebs_for_pool()
125 free = ubi->free_count; in wait_free_pebs_for_pool()
126 free += pool->size - pool->used + wl_pool->size - wl_pool->used; in wait_free_pebs_for_pool()
127 expect_free = reserved + ubi->beb_rsvd_pebs; in wait_free_pebs_for_pool()
128 spin_unlock(&ubi->wl_lock); in wait_free_pebs_for_pool()
133 * -EBUSY is returned from mtd layer caused by system shutdown. in wait_free_pebs_for_pool()
141 * left_free_count - returns the number of free pebs to fill fm pools
151 if (!ubi->free.rb_node) in left_free_count()
154 if (!ubi->ro_mode && !ubi->fm_disabled) in left_free_count()
155 fm_used = ubi->fm_size / ubi->leb_size - 1; in left_free_count()
157 return ubi->free_count - fm_used; in left_free_count()
161 * can_fill_pools - whether free PEBs will be left after filling pools
170 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in can_fill_pools()
171 struct ubi_fm_pool *pool = &ubi->fm_pool; in can_fill_pools()
172 int pool_need = pool->max_size - pool->size + in can_fill_pools()
173 wl_pool->max_size - wl_pool->size; in can_fill_pools()
175 if (free - pool_need < 1) in can_fill_pools()
182 * ubi_refill_pools_and_lock - refills all fastmap PEB pools and takes fm locks.
187 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in ubi_refill_pools_and_lock()
188 struct ubi_fm_pool *pool = &ubi->fm_pool; in ubi_refill_pools_and_lock()
192 if (!ubi->ro_mode && !ubi->fm_disabled) in ubi_refill_pools_and_lock()
195 down_write(&ubi->fm_protect); in ubi_refill_pools_and_lock()
196 down_write(&ubi->work_sem); in ubi_refill_pools_and_lock()
197 down_write(&ubi->fm_eba_sem); in ubi_refill_pools_and_lock()
199 spin_lock(&ubi->wl_lock); in ubi_refill_pools_and_lock()
204 wl_pool->size = 0; in ubi_refill_pools_and_lock()
205 pool->size = 0; in ubi_refill_pools_and_lock()
207 if (ubi->fm_anchor) { in ubi_refill_pools_and_lock()
208 wl_tree_add(ubi->fm_anchor, &ubi->free); in ubi_refill_pools_and_lock()
209 ubi->free_count++; in ubi_refill_pools_and_lock()
210 ubi->fm_anchor = NULL; in ubi_refill_pools_and_lock()
213 if (!ubi->fm_disabled) in ubi_refill_pools_and_lock()
215 * All available PEBs are in ubi->free, now is the time to get in ubi_refill_pools_and_lock()
218 ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1); in ubi_refill_pools_and_lock()
222 if (pool->size < pool->max_size) { in ubi_refill_pools_and_lock()
230 pool->pebs[pool->size] = e->pnum; in ubi_refill_pools_and_lock()
231 pool->size++; in ubi_refill_pools_and_lock()
235 if (wl_pool->size < wl_pool->max_size) { in ubi_refill_pools_and_lock()
241 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, in ubi_refill_pools_and_lock()
243 self_check_in_wl_tree(ubi, e, &ubi->free); in ubi_refill_pools_and_lock()
244 rb_erase(&e->u.rb, &ubi->free); in ubi_refill_pools_and_lock()
245 ubi->free_count--; in ubi_refill_pools_and_lock()
247 wl_pool->pebs[wl_pool->size] = e->pnum; in ubi_refill_pools_and_lock()
248 wl_pool->size++; in ubi_refill_pools_and_lock()
256 wl_pool->used = 0; in ubi_refill_pools_and_lock()
257 pool->used = 0; in ubi_refill_pools_and_lock()
259 spin_unlock(&ubi->wl_lock); in ubi_refill_pools_and_lock()
263 * produce_free_peb - produce a free physical eraseblock.
275 while (!ubi->free.rb_node && ubi->works_count) { in produce_free_peb()
287 * ubi_wl_get_peb - get a physical eraseblock.
292 * Returns with ubi->fm_eba_sem held in read mode!
297 struct ubi_fm_pool *pool = &ubi->fm_pool; in ubi_wl_get_peb()
298 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in ubi_wl_get_peb()
301 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
302 spin_lock(&ubi->wl_lock); in ubi_wl_get_peb()
306 if (pool->used == pool->size || wl_pool->used == wl_pool->size) { in ubi_wl_get_peb()
307 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
308 up_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
312 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
313 return -ENOSPC; in ubi_wl_get_peb()
315 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
316 spin_lock(&ubi->wl_lock); in ubi_wl_get_peb()
319 if (pool->used == pool->size) { in ubi_wl_get_peb()
320 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
324 ret = -ENOSPC; in ubi_wl_get_peb()
327 up_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
330 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
336 ubi_assert(pool->used < pool->size); in ubi_wl_get_peb()
337 ret = pool->pebs[pool->used++]; in ubi_wl_get_peb()
338 prot_queue_add(ubi, ubi->lookuptbl[ret]); in ubi_wl_get_peb()
339 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
345 * next_peb_for_wl - returns next PEB to be used internally by the
346 * WL sub-system.
352 struct ubi_fm_pool *pool = &ubi->fm_wl_pool; in next_peb_for_wl()
355 if (pool->used == pool->size) in next_peb_for_wl()
358 pnum = pool->pebs[pool->used]; in next_peb_for_wl()
359 return ubi->lookuptbl[pnum]; in next_peb_for_wl()
363 * need_wear_leveling - checks whether to trigger a wear leveling work.
365 * and 'ubi->free', because free PEB in 'ubi->free' tree maybe moved into
375 if (!ubi->used.rb_node) in need_wear_leveling()
380 if (!ubi->free.rb_node) in need_wear_leveling()
382 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0); in need_wear_leveling()
383 ec = e->ec; in need_wear_leveling()
385 ec = e->ec; in need_wear_leveling()
386 if (ubi->free.rb_node) { in need_wear_leveling()
387 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0); in need_wear_leveling()
388 ec = max(ec, e->ec); in need_wear_leveling()
391 e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); in need_wear_leveling()
393 return ec - e->ec >= UBI_WL_THRESHOLD; in need_wear_leveling()
396 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
402 struct ubi_fm_pool *pool = &ubi->fm_wl_pool; in get_peb_for_wl()
405 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); in get_peb_for_wl()
407 if (pool->used == pool->size) { in get_peb_for_wl()
411 if (!ubi->fm_work_scheduled) { in get_peb_for_wl()
412 ubi->fm_work_scheduled = 1; in get_peb_for_wl()
413 schedule_work(&ubi->fm_work); in get_peb_for_wl()
418 pnum = pool->pebs[pool->used++]; in get_peb_for_wl()
419 return ubi->lookuptbl[pnum]; in get_peb_for_wl()
423 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
431 spin_lock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
434 if (ubi->fm_anchor) { in ubi_ensure_anchor_pebs()
435 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
442 ubi->fm_anchor = anchor; in ubi_ensure_anchor_pebs()
443 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
447 ubi->fm_do_produce_anchor = 1; in ubi_ensure_anchor_pebs()
448 /* No luck, trigger wear leveling to produce a new anchor PEB. */ in ubi_ensure_anchor_pebs()
449 if (ubi->wl_scheduled) { in ubi_ensure_anchor_pebs()
450 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
453 ubi->wl_scheduled = 1; in ubi_ensure_anchor_pebs()
454 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
458 spin_lock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
459 ubi->wl_scheduled = 0; in ubi_ensure_anchor_pebs()
460 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
461 return -ENOMEM; in ubi_ensure_anchor_pebs()
464 wrk->func = &wear_leveling_worker; in ubi_ensure_anchor_pebs()
470 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
471 * sub-system.
483 int vol_id, pnum = fm_e->pnum; in ubi_wl_put_fm_peb()
488 ubi_assert(pnum < ubi->peb_count); in ubi_wl_put_fm_peb()
490 spin_lock(&ubi->wl_lock); in ubi_wl_put_fm_peb()
491 e = ubi->lookuptbl[pnum]; in ubi_wl_put_fm_peb()
499 ubi_assert(e->ec >= 0); in ubi_wl_put_fm_peb()
500 ubi->lookuptbl[pnum] = e; in ubi_wl_put_fm_peb()
503 spin_unlock(&ubi->wl_lock); in ubi_wl_put_fm_peb()
510 * ubi_is_erase_work - checks whether a work is erase work.
515 return wrk->func == erase_worker; in ubi_is_erase_work()
522 return_unused_pool_pebs(ubi, &ubi->fm_pool); in ubi_fastmap_close()
523 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool); in ubi_fastmap_close()
525 if (ubi->fm_anchor) { in ubi_fastmap_close()
526 return_unused_peb(ubi, ubi->fm_anchor); in ubi_fastmap_close()
527 ubi->fm_anchor = NULL; in ubi_fastmap_close()
530 if (ubi->fm) { in ubi_fastmap_close()
531 for (i = 0; i < ubi->fm->used_blocks; i++) in ubi_fastmap_close()
532 kfree(ubi->fm->e[i]); in ubi_fastmap_close()
534 kfree(ubi->fm); in ubi_fastmap_close()
538 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
548 if (e && !ubi->fm_disabled && !ubi->fm && !ubi->fm_anchor && in may_reserve_for_fm()
549 e->pnum < UBI_FM_MAX_START) in may_reserve_for_fm()
550 e = rb_entry(rb_next(root->rb_node), in may_reserve_for_fm()