18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2012 Linutronix GmbH 48c2ecf20Sopenharmony_ci * Copyright (c) 2014 sigma star gmbh 58c2ecf20Sopenharmony_ci * Author: Richard Weinberger <richard@nod.at> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci/** 98c2ecf20Sopenharmony_ci * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue 108c2ecf20Sopenharmony_ci * @wrk: the work description object 118c2ecf20Sopenharmony_ci */ 128c2ecf20Sopenharmony_cistatic void update_fastmap_work_fn(struct work_struct *wrk) 138c2ecf20Sopenharmony_ci{ 148c2ecf20Sopenharmony_ci struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work); 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci ubi_update_fastmap(ubi); 178c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 188c2ecf20Sopenharmony_ci ubi->fm_work_scheduled = 0; 198c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 208c2ecf20Sopenharmony_ci} 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci/** 238c2ecf20Sopenharmony_ci * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB. 248c2ecf20Sopenharmony_ci * @root: the RB-tree where to look for 258c2ecf20Sopenharmony_ci */ 268c2ecf20Sopenharmony_cistatic struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root) 278c2ecf20Sopenharmony_ci{ 288c2ecf20Sopenharmony_ci struct rb_node *p; 298c2ecf20Sopenharmony_ci struct ubi_wl_entry *e, *victim = NULL; 308c2ecf20Sopenharmony_ci int max_ec = UBI_MAX_ERASECOUNTER; 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ci ubi_rb_for_each_entry(p, e, root, u.rb) { 338c2ecf20Sopenharmony_ci if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) { 348c2ecf20Sopenharmony_ci victim = e; 358c2ecf20Sopenharmony_ci max_ec = e->ec; 368c2ecf20Sopenharmony_ci } 378c2ecf20Sopenharmony_ci } 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ci return victim; 408c2ecf20Sopenharmony_ci} 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistatic inline void return_unused_peb(struct ubi_device *ubi, 438c2ecf20Sopenharmony_ci struct ubi_wl_entry *e) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci wl_tree_add(e, &ubi->free); 468c2ecf20Sopenharmony_ci ubi->free_count++; 478c2ecf20Sopenharmony_ci} 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ci/** 508c2ecf20Sopenharmony_ci * return_unused_pool_pebs - returns unused PEB to the free tree. 518c2ecf20Sopenharmony_ci * @ubi: UBI device description object 528c2ecf20Sopenharmony_ci * @pool: fastmap pool description object 538c2ecf20Sopenharmony_ci */ 548c2ecf20Sopenharmony_cistatic void return_unused_pool_pebs(struct ubi_device *ubi, 558c2ecf20Sopenharmony_ci struct ubi_fm_pool *pool) 568c2ecf20Sopenharmony_ci{ 578c2ecf20Sopenharmony_ci int i; 588c2ecf20Sopenharmony_ci struct ubi_wl_entry *e; 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci for (i = pool->used; i < pool->size; i++) { 618c2ecf20Sopenharmony_ci e = ubi->lookuptbl[pool->pebs[i]]; 628c2ecf20Sopenharmony_ci return_unused_peb(ubi, e); 638c2ecf20Sopenharmony_ci } 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_ci/** 678c2ecf20Sopenharmony_ci * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number. 688c2ecf20Sopenharmony_ci * @ubi: UBI device description object 698c2ecf20Sopenharmony_ci * @anchor: This PEB will be used as anchor PEB by fastmap 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * The function returns a physical erase block with a given maximal number 728c2ecf20Sopenharmony_ci * and removes it from the wl subsystem. 738c2ecf20Sopenharmony_ci * Must be called with wl_lock held! 748c2ecf20Sopenharmony_ci */ 758c2ecf20Sopenharmony_cistruct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor) 768c2ecf20Sopenharmony_ci{ 778c2ecf20Sopenharmony_ci struct ubi_wl_entry *e = NULL; 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1)) 808c2ecf20Sopenharmony_ci goto out; 818c2ecf20Sopenharmony_ci 828c2ecf20Sopenharmony_ci if (anchor) 838c2ecf20Sopenharmony_ci e = find_anchor_wl_entry(&ubi->free); 848c2ecf20Sopenharmony_ci else 858c2ecf20Sopenharmony_ci e = find_mean_wl_entry(ubi, &ubi->free); 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci if (!e) 888c2ecf20Sopenharmony_ci goto out; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->free); 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci /* remove it from the free list, 938c2ecf20Sopenharmony_ci * the wl subsystem does no longer know this erase block */ 948c2ecf20Sopenharmony_ci rb_erase(&e->u.rb, &ubi->free); 958c2ecf20Sopenharmony_ci ubi->free_count--; 968c2ecf20Sopenharmony_ciout: 978c2ecf20Sopenharmony_ci return e; 988c2ecf20Sopenharmony_ci} 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci/* 1018c2ecf20Sopenharmony_ci * has_enough_free_count - whether ubi has enough free pebs to fill fm pools 1028c2ecf20Sopenharmony_ci * @ubi: UBI device description object 1038c2ecf20Sopenharmony_ci * @is_wl_pool: whether UBI is filling wear leveling pool 1048c2ecf20Sopenharmony_ci * 1058c2ecf20Sopenharmony_ci * This helper function checks whether there are enough free pebs (deducted 1068c2ecf20Sopenharmony_ci * by fastmap pebs) to fill fm_pool and fm_wl_pool, above rule works after 1078c2ecf20Sopenharmony_ci * there is at least one of free pebs is filled into fm_wl_pool. 1088c2ecf20Sopenharmony_ci * For wear leveling pool, UBI should also reserve free pebs for bad pebs 1098c2ecf20Sopenharmony_ci * handling, because there maybe no enough free pebs for user volumes after 1108c2ecf20Sopenharmony_ci * producing new bad pebs. 1118c2ecf20Sopenharmony_ci */ 1128c2ecf20Sopenharmony_cistatic bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool) 1138c2ecf20Sopenharmony_ci{ 1148c2ecf20Sopenharmony_ci int fm_used = 0; // fastmap non anchor pebs. 1158c2ecf20Sopenharmony_ci int beb_rsvd_pebs; 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci if (!ubi->free.rb_node) 1188c2ecf20Sopenharmony_ci return false; 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci beb_rsvd_pebs = is_wl_pool ? ubi->beb_rsvd_pebs : 0; 1218c2ecf20Sopenharmony_ci if (ubi->fm_wl_pool.size > 0 && !(ubi->ro_mode || ubi->fm_disabled)) 1228c2ecf20Sopenharmony_ci fm_used = ubi->fm_size / ubi->leb_size - 1; 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci return ubi->free_count - beb_rsvd_pebs > fm_used; 1258c2ecf20Sopenharmony_ci} 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci/** 1288c2ecf20Sopenharmony_ci * ubi_refill_pools - refills all fastmap PEB pools. 1298c2ecf20Sopenharmony_ci * @ubi: UBI device description object 1308c2ecf20Sopenharmony_ci */ 1318c2ecf20Sopenharmony_civoid ubi_refill_pools(struct ubi_device *ubi) 1328c2ecf20Sopenharmony_ci{ 1338c2ecf20Sopenharmony_ci struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; 1348c2ecf20Sopenharmony_ci struct ubi_fm_pool *pool = &ubi->fm_pool; 1358c2ecf20Sopenharmony_ci struct ubi_wl_entry *e; 1368c2ecf20Sopenharmony_ci int enough; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci return_unused_pool_pebs(ubi, wl_pool); 1418c2ecf20Sopenharmony_ci return_unused_pool_pebs(ubi, pool); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci wl_pool->size = 0; 1448c2ecf20Sopenharmony_ci pool->size = 0; 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci if (ubi->fm_anchor) { 1478c2ecf20Sopenharmony_ci wl_tree_add(ubi->fm_anchor, &ubi->free); 1488c2ecf20Sopenharmony_ci ubi->free_count++; 1498c2ecf20Sopenharmony_ci ubi->fm_anchor = NULL; 1508c2ecf20Sopenharmony_ci } 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci if (!ubi->fm_disabled) 1538c2ecf20Sopenharmony_ci /* 1548c2ecf20Sopenharmony_ci * All available PEBs are in ubi->free, now is the time to get 1558c2ecf20Sopenharmony_ci * the best anchor PEBs. 1568c2ecf20Sopenharmony_ci */ 1578c2ecf20Sopenharmony_ci ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1); 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci for (;;) { 1608c2ecf20Sopenharmony_ci enough = 0; 1618c2ecf20Sopenharmony_ci if (pool->size < pool->max_size) { 1628c2ecf20Sopenharmony_ci if (!has_enough_free_count(ubi, false)) 1638c2ecf20Sopenharmony_ci break; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci e = wl_get_wle(ubi); 1668c2ecf20Sopenharmony_ci if (!e) 1678c2ecf20Sopenharmony_ci break; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci pool->pebs[pool->size] = e->pnum; 1708c2ecf20Sopenharmony_ci pool->size++; 1718c2ecf20Sopenharmony_ci } else 1728c2ecf20Sopenharmony_ci enough++; 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci if (wl_pool->size < wl_pool->max_size) { 1758c2ecf20Sopenharmony_ci if (!has_enough_free_count(ubi, true)) 1768c2ecf20Sopenharmony_ci break; 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); 1798c2ecf20Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->free); 1808c2ecf20Sopenharmony_ci rb_erase(&e->u.rb, &ubi->free); 1818c2ecf20Sopenharmony_ci ubi->free_count--; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci wl_pool->pebs[wl_pool->size] = e->pnum; 1848c2ecf20Sopenharmony_ci wl_pool->size++; 1858c2ecf20Sopenharmony_ci } else 1868c2ecf20Sopenharmony_ci enough++; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci if (enough == 2) 1898c2ecf20Sopenharmony_ci break; 1908c2ecf20Sopenharmony_ci } 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci wl_pool->used = 0; 1938c2ecf20Sopenharmony_ci pool->used = 0; 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 1968c2ecf20Sopenharmony_ci} 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci/** 1998c2ecf20Sopenharmony_ci * produce_free_peb - produce a free physical eraseblock. 2008c2ecf20Sopenharmony_ci * @ubi: UBI device description object 2018c2ecf20Sopenharmony_ci * 2028c2ecf20Sopenharmony_ci * This function tries to make a free PEB by means of synchronous execution of 2038c2ecf20Sopenharmony_ci * pending works. This may be needed if, for example the background thread is 2048c2ecf20Sopenharmony_ci * disabled. Returns zero in case of success and a negative error code in case 2058c2ecf20Sopenharmony_ci * of failure. 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_cistatic int produce_free_peb(struct ubi_device *ubi) 2088c2ecf20Sopenharmony_ci{ 2098c2ecf20Sopenharmony_ci int err; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci while (!ubi->free.rb_node && ubi->works_count) { 2128c2ecf20Sopenharmony_ci dbg_wl("do one work synchronously"); 2138c2ecf20Sopenharmony_ci err = do_work(ubi); 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci if (err) 2168c2ecf20Sopenharmony_ci return err; 2178c2ecf20Sopenharmony_ci } 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci return 0; 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_ci/** 2238c2ecf20Sopenharmony_ci * ubi_wl_get_peb - get a physical eraseblock. 2248c2ecf20Sopenharmony_ci * @ubi: UBI device description object 2258c2ecf20Sopenharmony_ci * 2268c2ecf20Sopenharmony_ci * This function returns a physical eraseblock in case of success and a 2278c2ecf20Sopenharmony_ci * negative error code in case of failure. 2288c2ecf20Sopenharmony_ci * Returns with ubi->fm_eba_sem held in read mode! 2298c2ecf20Sopenharmony_ci */ 2308c2ecf20Sopenharmony_ciint ubi_wl_get_peb(struct ubi_device *ubi) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci int ret, attempts = 0; 2338c2ecf20Sopenharmony_ci struct ubi_fm_pool *pool = &ubi->fm_pool; 2348c2ecf20Sopenharmony_ci struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; 2358c2ecf20Sopenharmony_ci 2368c2ecf20Sopenharmony_ciagain: 2378c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 2388c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci /* We check here also for the WL pool because at this point we can 2418c2ecf20Sopenharmony_ci * refill the WL pool synchronous. */ 2428c2ecf20Sopenharmony_ci if (pool->used == pool->size || wl_pool->used == wl_pool->size) { 2438c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 2448c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 2458c2ecf20Sopenharmony_ci ret = ubi_update_fastmap(ubi); 2468c2ecf20Sopenharmony_ci if (ret) { 2478c2ecf20Sopenharmony_ci ubi_msg(ubi, "Unable to write a new fastmap: %i", ret); 2488c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 2498c2ecf20Sopenharmony_ci return -ENOSPC; 2508c2ecf20Sopenharmony_ci } 2518c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 2528c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 2538c2ecf20Sopenharmony_ci } 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ci if (pool->used == pool->size) { 2568c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 2578c2ecf20Sopenharmony_ci attempts++; 2588c2ecf20Sopenharmony_ci if (attempts == 10) { 2598c2ecf20Sopenharmony_ci ubi_err(ubi, "Unable to get a free PEB from user WL pool"); 2608c2ecf20Sopenharmony_ci ret = -ENOSPC; 2618c2ecf20Sopenharmony_ci goto out; 2628c2ecf20Sopenharmony_ci } 2638c2ecf20Sopenharmony_ci up_read(&ubi->fm_eba_sem); 2648c2ecf20Sopenharmony_ci ret = produce_free_peb(ubi); 2658c2ecf20Sopenharmony_ci if (ret < 0) { 2668c2ecf20Sopenharmony_ci down_read(&ubi->fm_eba_sem); 2678c2ecf20Sopenharmony_ci goto out; 2688c2ecf20Sopenharmony_ci } 2698c2ecf20Sopenharmony_ci goto again; 2708c2ecf20Sopenharmony_ci } 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci ubi_assert(pool->used < pool->size); 2738c2ecf20Sopenharmony_ci ret = pool->pebs[pool->used++]; 2748c2ecf20Sopenharmony_ci prot_queue_add(ubi, ubi->lookuptbl[ret]); 2758c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 2768c2ecf20Sopenharmony_ciout: 2778c2ecf20Sopenharmony_ci return ret; 2788c2ecf20Sopenharmony_ci} 2798c2ecf20Sopenharmony_ci 2808c2ecf20Sopenharmony_ci/* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system. 2818c2ecf20Sopenharmony_ci * 2828c2ecf20Sopenharmony_ci * @ubi: UBI device description object 2838c2ecf20Sopenharmony_ci */ 2848c2ecf20Sopenharmony_cistatic struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) 2858c2ecf20Sopenharmony_ci{ 2868c2ecf20Sopenharmony_ci struct ubi_fm_pool *pool = &ubi->fm_wl_pool; 2878c2ecf20Sopenharmony_ci int pnum; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ci ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci if (pool->used == pool->size) { 2928c2ecf20Sopenharmony_ci /* We cannot update the fastmap here because this 2938c2ecf20Sopenharmony_ci * function is called in atomic context. 2948c2ecf20Sopenharmony_ci * Let's fail here and refill/update it as soon as possible. */ 2958c2ecf20Sopenharmony_ci if (!ubi->fm_work_scheduled) { 2968c2ecf20Sopenharmony_ci ubi->fm_work_scheduled = 1; 2978c2ecf20Sopenharmony_ci schedule_work(&ubi->fm_work); 2988c2ecf20Sopenharmony_ci } 2998c2ecf20Sopenharmony_ci return NULL; 3008c2ecf20Sopenharmony_ci } 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci pnum = pool->pebs[pool->used++]; 3038c2ecf20Sopenharmony_ci return ubi->lookuptbl[pnum]; 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_ci/** 3078c2ecf20Sopenharmony_ci * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB. 3088c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3098c2ecf20Sopenharmony_ci */ 3108c2ecf20Sopenharmony_ciint ubi_ensure_anchor_pebs(struct ubi_device *ubi) 3118c2ecf20Sopenharmony_ci{ 3128c2ecf20Sopenharmony_ci struct ubi_work *wrk; 3138c2ecf20Sopenharmony_ci struct ubi_wl_entry *anchor; 3148c2ecf20Sopenharmony_ci 3158c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 3168c2ecf20Sopenharmony_ci 3178c2ecf20Sopenharmony_ci /* Do we already have an anchor? */ 3188c2ecf20Sopenharmony_ci if (ubi->fm_anchor) { 3198c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3208c2ecf20Sopenharmony_ci return 0; 3218c2ecf20Sopenharmony_ci } 3228c2ecf20Sopenharmony_ci 3238c2ecf20Sopenharmony_ci /* See if we can find an anchor PEB on the list of free PEBs */ 3248c2ecf20Sopenharmony_ci anchor = ubi_wl_get_fm_peb(ubi, 1); 3258c2ecf20Sopenharmony_ci if (anchor) { 3268c2ecf20Sopenharmony_ci ubi->fm_anchor = anchor; 3278c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3288c2ecf20Sopenharmony_ci return 0; 3298c2ecf20Sopenharmony_ci } 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci ubi->fm_do_produce_anchor = 1; 3328c2ecf20Sopenharmony_ci /* No luck, trigger wear leveling to produce a new anchor PEB. */ 3338c2ecf20Sopenharmony_ci if (ubi->wl_scheduled) { 3348c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3358c2ecf20Sopenharmony_ci return 0; 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci ubi->wl_scheduled = 1; 3388c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); 3418c2ecf20Sopenharmony_ci if (!wrk) { 3428c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 3438c2ecf20Sopenharmony_ci ubi->wl_scheduled = 0; 3448c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3458c2ecf20Sopenharmony_ci return -ENOMEM; 3468c2ecf20Sopenharmony_ci } 3478c2ecf20Sopenharmony_ci 3488c2ecf20Sopenharmony_ci wrk->func = &wear_leveling_worker; 3498c2ecf20Sopenharmony_ci __schedule_ubi_work(ubi, wrk); 3508c2ecf20Sopenharmony_ci return 0; 3518c2ecf20Sopenharmony_ci} 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ci/** 3548c2ecf20Sopenharmony_ci * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling 3558c2ecf20Sopenharmony_ci * sub-system. 3568c2ecf20Sopenharmony_ci * see: ubi_wl_put_peb() 3578c2ecf20Sopenharmony_ci * 3588c2ecf20Sopenharmony_ci * @ubi: UBI device description object 3598c2ecf20Sopenharmony_ci * @fm_e: physical eraseblock to return 3608c2ecf20Sopenharmony_ci * @lnum: the last used logical eraseblock number for the PEB 3618c2ecf20Sopenharmony_ci * @torture: if this physical eraseblock has to be tortured 3628c2ecf20Sopenharmony_ci */ 3638c2ecf20Sopenharmony_ciint ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e, 3648c2ecf20Sopenharmony_ci int lnum, int torture) 3658c2ecf20Sopenharmony_ci{ 3668c2ecf20Sopenharmony_ci struct ubi_wl_entry *e; 3678c2ecf20Sopenharmony_ci int vol_id, pnum = fm_e->pnum; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci dbg_wl("PEB %d", pnum); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci ubi_assert(pnum >= 0); 3728c2ecf20Sopenharmony_ci ubi_assert(pnum < ubi->peb_count); 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci spin_lock(&ubi->wl_lock); 3758c2ecf20Sopenharmony_ci e = ubi->lookuptbl[pnum]; 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_ci /* This can happen if we recovered from a fastmap the very 3788c2ecf20Sopenharmony_ci * first time and writing now a new one. In this case the wl system 3798c2ecf20Sopenharmony_ci * has never seen any PEB used by the original fastmap. 3808c2ecf20Sopenharmony_ci */ 3818c2ecf20Sopenharmony_ci if (!e) { 3828c2ecf20Sopenharmony_ci e = fm_e; 3838c2ecf20Sopenharmony_ci ubi_assert(e->ec >= 0); 3848c2ecf20Sopenharmony_ci ubi->lookuptbl[pnum] = e; 3858c2ecf20Sopenharmony_ci } 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci spin_unlock(&ubi->wl_lock); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID; 3908c2ecf20Sopenharmony_ci return schedule_erase(ubi, e, vol_id, lnum, torture, true); 3918c2ecf20Sopenharmony_ci} 3928c2ecf20Sopenharmony_ci 3938c2ecf20Sopenharmony_ci/** 3948c2ecf20Sopenharmony_ci * ubi_is_erase_work - checks whether a work is erase work. 3958c2ecf20Sopenharmony_ci * @wrk: The work object to be checked 3968c2ecf20Sopenharmony_ci */ 3978c2ecf20Sopenharmony_ciint ubi_is_erase_work(struct ubi_work *wrk) 3988c2ecf20Sopenharmony_ci{ 3998c2ecf20Sopenharmony_ci return wrk->func == erase_worker; 4008c2ecf20Sopenharmony_ci} 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_cistatic void ubi_fastmap_close(struct ubi_device *ubi) 4038c2ecf20Sopenharmony_ci{ 4048c2ecf20Sopenharmony_ci int i; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci return_unused_pool_pebs(ubi, &ubi->fm_pool); 4078c2ecf20Sopenharmony_ci return_unused_pool_pebs(ubi, &ubi->fm_wl_pool); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci if (ubi->fm_anchor) { 4108c2ecf20Sopenharmony_ci return_unused_peb(ubi, ubi->fm_anchor); 4118c2ecf20Sopenharmony_ci ubi->fm_anchor = NULL; 4128c2ecf20Sopenharmony_ci } 4138c2ecf20Sopenharmony_ci 4148c2ecf20Sopenharmony_ci if (ubi->fm) { 4158c2ecf20Sopenharmony_ci for (i = 0; i < ubi->fm->used_blocks; i++) 4168c2ecf20Sopenharmony_ci kfree(ubi->fm->e[i]); 4178c2ecf20Sopenharmony_ci } 4188c2ecf20Sopenharmony_ci kfree(ubi->fm); 4198c2ecf20Sopenharmony_ci} 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci/** 4228c2ecf20Sopenharmony_ci * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap. 4238c2ecf20Sopenharmony_ci * See find_mean_wl_entry() 4248c2ecf20Sopenharmony_ci * 4258c2ecf20Sopenharmony_ci * @ubi: UBI device description object 4268c2ecf20Sopenharmony_ci * @e: physical eraseblock to return 4278c2ecf20Sopenharmony_ci * @root: RB tree to test against. 4288c2ecf20Sopenharmony_ci */ 4298c2ecf20Sopenharmony_cistatic struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi, 4308c2ecf20Sopenharmony_ci struct ubi_wl_entry *e, 4318c2ecf20Sopenharmony_ci struct rb_root *root) { 4328c2ecf20Sopenharmony_ci if (e && !ubi->fm_disabled && !ubi->fm && 4338c2ecf20Sopenharmony_ci e->pnum < UBI_FM_MAX_START) 4348c2ecf20Sopenharmony_ci e = rb_entry(rb_next(root->rb_node), 4358c2ecf20Sopenharmony_ci struct ubi_wl_entry, u.rb); 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci return e; 4388c2ecf20Sopenharmony_ci} 439