Lines Matching refs:ubi
14 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work); in update_fastmap_work_fn() local
16 ubi_update_fastmap(ubi); in update_fastmap_work_fn()
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()
42 static inline void return_unused_peb(struct ubi_device *ubi, in return_unused_peb() argument
45 wl_tree_add(e, &ubi->free); in return_unused_peb()
46 ubi->free_count++; in return_unused_peb()
54 static void return_unused_pool_pebs(struct ubi_device *ubi, in return_unused_pool_pebs() argument
61 e = ubi->lookuptbl[pool->pebs[i]]; in return_unused_pool_pebs()
62 return_unused_peb(ubi, e); in return_unused_pool_pebs()
75 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor) in ubi_wl_get_fm_peb() argument
79 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1)) 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()
112 static bool has_enough_free_count(struct ubi_device *ubi, bool is_wl_pool) in has_enough_free_count() argument
117 if (!ubi->free.rb_node) in has_enough_free_count()
120 beb_rsvd_pebs = is_wl_pool ? ubi->beb_rsvd_pebs : 0; in has_enough_free_count()
121 if (ubi->fm_wl_pool.size > 0 && !(ubi->ro_mode || ubi->fm_disabled)) in has_enough_free_count()
122 fm_used = ubi->fm_size / ubi->leb_size - 1; in has_enough_free_count()
124 return ubi->free_count - beb_rsvd_pebs > fm_used; in has_enough_free_count()
131 void ubi_refill_pools(struct ubi_device *ubi) in ubi_refill_pools() argument
133 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in ubi_refill_pools()
134 struct ubi_fm_pool *pool = &ubi->fm_pool; in ubi_refill_pools()
138 spin_lock(&ubi->wl_lock); in ubi_refill_pools()
140 return_unused_pool_pebs(ubi, wl_pool); in ubi_refill_pools()
141 return_unused_pool_pebs(ubi, pool); in ubi_refill_pools()
146 if (ubi->fm_anchor) { in ubi_refill_pools()
147 wl_tree_add(ubi->fm_anchor, &ubi->free); in ubi_refill_pools()
148 ubi->free_count++; in ubi_refill_pools()
149 ubi->fm_anchor = NULL; in ubi_refill_pools()
152 if (!ubi->fm_disabled) in ubi_refill_pools()
157 ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1); in ubi_refill_pools()
162 if (!has_enough_free_count(ubi, false)) in ubi_refill_pools()
165 e = wl_get_wle(ubi); in ubi_refill_pools()
175 if (!has_enough_free_count(ubi, true)) in ubi_refill_pools()
178 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); in ubi_refill_pools()
179 self_check_in_wl_tree(ubi, e, &ubi->free); in ubi_refill_pools()
180 rb_erase(&e->u.rb, &ubi->free); in ubi_refill_pools()
181 ubi->free_count--; in ubi_refill_pools()
195 spin_unlock(&ubi->wl_lock); in ubi_refill_pools()
207 static int produce_free_peb(struct ubi_device *ubi) in produce_free_peb() argument
211 while (!ubi->free.rb_node && ubi->works_count) { in produce_free_peb()
213 err = do_work(ubi); in produce_free_peb()
230 int ubi_wl_get_peb(struct ubi_device *ubi) in ubi_wl_get_peb() argument
233 struct ubi_fm_pool *pool = &ubi->fm_pool; in ubi_wl_get_peb()
234 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; in ubi_wl_get_peb()
237 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
238 spin_lock(&ubi->wl_lock); in ubi_wl_get_peb()
243 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
244 up_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
245 ret = ubi_update_fastmap(ubi); in ubi_wl_get_peb()
247 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret); in ubi_wl_get_peb()
248 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
251 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
252 spin_lock(&ubi->wl_lock); in ubi_wl_get_peb()
256 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
259 ubi_err(ubi, "Unable to get a free PEB from user WL pool"); in ubi_wl_get_peb()
263 up_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
264 ret = produce_free_peb(ubi); in ubi_wl_get_peb()
266 down_read(&ubi->fm_eba_sem); in ubi_wl_get_peb()
274 prot_queue_add(ubi, ubi->lookuptbl[ret]); in ubi_wl_get_peb()
275 spin_unlock(&ubi->wl_lock); in ubi_wl_get_peb()
286 static struct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi) in next_peb_for_wl() argument
288 struct ubi_fm_pool *pool = &ubi->fm_wl_pool; in next_peb_for_wl()
295 return ubi->lookuptbl[pnum]; in next_peb_for_wl()
306 static bool need_wear_leveling(struct ubi_device *ubi) in need_wear_leveling() argument
311 if (!ubi->used.rb_node) in need_wear_leveling()
314 e = next_peb_for_wl(ubi); in need_wear_leveling()
316 if (!ubi->free.rb_node) in need_wear_leveling()
318 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); in need_wear_leveling()
322 if (ubi->free.rb_node) { in need_wear_leveling()
323 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); in need_wear_leveling()
327 e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); in need_wear_leveling()
336 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) in get_peb_for_wl() argument
338 struct ubi_fm_pool *pool = &ubi->fm_wl_pool; in get_peb_for_wl()
341 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); in get_peb_for_wl()
347 if (!ubi->fm_work_scheduled) { in get_peb_for_wl()
348 ubi->fm_work_scheduled = 1; in get_peb_for_wl()
349 schedule_work(&ubi->fm_work); in get_peb_for_wl()
355 return ubi->lookuptbl[pnum]; in get_peb_for_wl()
362 int ubi_ensure_anchor_pebs(struct ubi_device *ubi) in ubi_ensure_anchor_pebs() argument
367 spin_lock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
370 if (ubi->fm_anchor) { in ubi_ensure_anchor_pebs()
371 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
376 anchor = ubi_wl_get_fm_peb(ubi, 1); in ubi_ensure_anchor_pebs()
378 ubi->fm_anchor = anchor; in ubi_ensure_anchor_pebs()
379 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
383 ubi->fm_do_produce_anchor = 1; in ubi_ensure_anchor_pebs()
385 if (ubi->wl_scheduled) { in ubi_ensure_anchor_pebs()
386 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
389 ubi->wl_scheduled = 1; in ubi_ensure_anchor_pebs()
390 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
394 spin_lock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
395 ubi->wl_scheduled = 0; in ubi_ensure_anchor_pebs()
396 spin_unlock(&ubi->wl_lock); in ubi_ensure_anchor_pebs()
401 __schedule_ubi_work(ubi, wrk); in ubi_ensure_anchor_pebs()
415 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e, in ubi_wl_put_fm_peb() argument
424 ubi_assert(pnum < ubi->peb_count); in ubi_wl_put_fm_peb()
426 spin_lock(&ubi->wl_lock); in ubi_wl_put_fm_peb()
427 e = ubi->lookuptbl[pnum]; in ubi_wl_put_fm_peb()
436 ubi->lookuptbl[pnum] = e; in ubi_wl_put_fm_peb()
439 spin_unlock(&ubi->wl_lock); in ubi_wl_put_fm_peb()
442 return schedule_erase(ubi, e, vol_id, lnum, torture, true); in ubi_wl_put_fm_peb()
454 static void ubi_fastmap_close(struct ubi_device *ubi) in ubi_fastmap_close() argument
458 return_unused_pool_pebs(ubi, &ubi->fm_pool); in ubi_fastmap_close()
459 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool); in ubi_fastmap_close()
461 if (ubi->fm_anchor) { in ubi_fastmap_close()
462 return_unused_peb(ubi, ubi->fm_anchor); in ubi_fastmap_close()
463 ubi->fm_anchor = NULL; in ubi_fastmap_close()
466 if (ubi->fm) { in ubi_fastmap_close()
467 for (i = 0; i < ubi->fm->used_blocks; i++) in ubi_fastmap_close()
468 kfree(ubi->fm->e[i]); in ubi_fastmap_close()
470 kfree(ubi->fm); in ubi_fastmap_close()
481 static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi, in may_reserve_for_fm() argument
484 if (e && !ubi->fm_disabled && !ubi->fm && in may_reserve_for_fm()