2 * Copyright (c) 2012 Linutronix GmbH
3 * Copyright (c) 2014 sigma star gmbh
4 * Author: Richard Weinberger <richard@nod.at>
6 * SPDX-License-Identifier: GPL-2.0+
11 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
12 * @wrk: the work description object
15 static void update_fastmap_work_fn(struct work_struct *wrk)
17 void update_fastmap_work_fn(struct ubi_device *ubi)
21 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
24 ubi_update_fastmap(ubi);
25 spin_lock(&ubi->wl_lock);
26 ubi->fm_work_scheduled = 0;
27 spin_unlock(&ubi->wl_lock);
31 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
32 * @root: the RB-tree where to look for
34 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
37 struct ubi_wl_entry *e, *victim = NULL;
38 int max_ec = UBI_MAX_ERASECOUNTER;
40 ubi_rb_for_each_entry(p, e, root, u.rb) {
41 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
51 * return_unused_pool_pebs - returns unused PEB to the free tree.
52 * @ubi: UBI device description object
53 * @pool: fastmap pool description object
55 static void return_unused_pool_pebs(struct ubi_device *ubi,
56 struct ubi_fm_pool *pool)
59 struct ubi_wl_entry *e;
61 for (i = pool->used; i < pool->size; i++) {
62 e = ubi->lookuptbl[pool->pebs[i]];
63 wl_tree_add(e, &ubi->free);
68 static int anchor_pebs_avalible(struct rb_root *root)
71 struct ubi_wl_entry *e;
73 ubi_rb_for_each_entry(p, e, root, u.rb)
74 if (e->pnum < UBI_FM_MAX_START)
81 * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
82 * @ubi: UBI device description object
83 * @anchor: This PEB will be used as anchor PEB by fastmap
85 * The function returns a physical erase block with a given maximal number
86 * and removes it from the wl subsystem.
87 * Must be called with wl_lock held!
89 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
91 struct ubi_wl_entry *e = NULL;
93 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
97 e = find_anchor_wl_entry(&ubi->free);
99 e = find_mean_wl_entry(ubi, &ubi->free);
104 self_check_in_wl_tree(ubi, e, &ubi->free);
106 /* remove it from the free list,
107 * the wl subsystem does no longer know this erase block */
108 rb_erase(&e->u.rb, &ubi->free);
115 * ubi_refill_pools - refills all fastmap PEB pools.
116 * @ubi: UBI device description object
118 void ubi_refill_pools(struct ubi_device *ubi)
120 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
121 struct ubi_fm_pool *pool = &ubi->fm_pool;
122 struct ubi_wl_entry *e;
125 spin_lock(&ubi->wl_lock);
127 return_unused_pool_pebs(ubi, wl_pool);
128 return_unused_pool_pebs(ubi, pool);
135 if (pool->size < pool->max_size) {
136 if (!ubi->free.rb_node)
143 pool->pebs[pool->size] = e->pnum;
148 if (wl_pool->size < wl_pool->max_size) {
149 if (!ubi->free.rb_node ||
150 (ubi->free_count - ubi->beb_rsvd_pebs < 5))
153 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
154 self_check_in_wl_tree(ubi, e, &ubi->free);
155 rb_erase(&e->u.rb, &ubi->free);
158 wl_pool->pebs[wl_pool->size] = e->pnum;
170 spin_unlock(&ubi->wl_lock);
174 * ubi_wl_get_peb - get a physical eraseblock.
175 * @ubi: UBI device description object
177 * This function returns a physical eraseblock in case of success and a
178 * negative error code in case of failure.
179 * Returns with ubi->fm_eba_sem held in read mode!
181 int ubi_wl_get_peb(struct ubi_device *ubi)
183 int ret, retried = 0;
184 struct ubi_fm_pool *pool = &ubi->fm_pool;
185 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
188 down_read(&ubi->fm_eba_sem);
189 spin_lock(&ubi->wl_lock);
191 /* We check here also for the WL pool because at this point we can
192 * refill the WL pool synchronous. */
193 if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
194 spin_unlock(&ubi->wl_lock);
195 up_read(&ubi->fm_eba_sem);
196 ret = ubi_update_fastmap(ubi);
198 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
199 down_read(&ubi->fm_eba_sem);
202 down_read(&ubi->fm_eba_sem);
203 spin_lock(&ubi->wl_lock);
206 if (pool->used == pool->size) {
207 spin_unlock(&ubi->wl_lock);
209 ubi_err(ubi, "Unable to get a free PEB from user WL pool");
214 up_read(&ubi->fm_eba_sem);
218 ubi_assert(pool->used < pool->size);
219 ret = pool->pebs[pool->used++];
220 prot_queue_add(ubi, ubi->lookuptbl[ret]);
221 spin_unlock(&ubi->wl_lock);
226 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
228 * @ubi: UBI device description object
230 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
232 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
235 if (pool->used == pool->size) {
237 /* We cannot update the fastmap here because this
238 * function is called in atomic context.
239 * Let's fail here and refill/update it as soon as possible. */
240 if (!ubi->fm_work_scheduled) {
241 ubi->fm_work_scheduled = 1;
242 schedule_work(&ubi->fm_work);
247 * No work queues in U-Boot, we must do this immediately
249 update_fastmap_work_fn(ubi);
253 pnum = pool->pebs[pool->used++];
254 return ubi->lookuptbl[pnum];
258 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
259 * @ubi: UBI device description object
261 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
263 struct ubi_work *wrk;
265 spin_lock(&ubi->wl_lock);
266 if (ubi->wl_scheduled) {
267 spin_unlock(&ubi->wl_lock);
270 ubi->wl_scheduled = 1;
271 spin_unlock(&ubi->wl_lock);
273 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
275 spin_lock(&ubi->wl_lock);
276 ubi->wl_scheduled = 0;
277 spin_unlock(&ubi->wl_lock);
282 wrk->func = &wear_leveling_worker;
283 schedule_ubi_work(ubi, wrk);
288 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
290 * see: ubi_wl_put_peb()
292 * @ubi: UBI device description object
293 * @fm_e: physical eraseblock to return
294 * @lnum: the last used logical eraseblock number for the PEB
295 * @torture: if this physical eraseblock has to be tortured
297 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
298 int lnum, int torture)
300 struct ubi_wl_entry *e;
301 int vol_id, pnum = fm_e->pnum;
303 dbg_wl("PEB %d", pnum);
305 ubi_assert(pnum >= 0);
306 ubi_assert(pnum < ubi->peb_count);
308 spin_lock(&ubi->wl_lock);
309 e = ubi->lookuptbl[pnum];
311 /* This can happen if we recovered from a fastmap the very
312 * first time and writing now a new one. In this case the wl system
313 * has never seen any PEB used by the original fastmap.
317 ubi_assert(e->ec >= 0);
318 ubi->lookuptbl[pnum] = e;
321 spin_unlock(&ubi->wl_lock);
323 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
324 return schedule_erase(ubi, e, vol_id, lnum, torture);
328 * ubi_is_erase_work - checks whether a work is erase work.
329 * @wrk: The work object to be checked
331 int ubi_is_erase_work(struct ubi_work *wrk)
333 return wrk->func == erase_worker;
336 static void ubi_fastmap_close(struct ubi_device *ubi)
341 flush_work(&ubi->fm_work);
343 update_fastmap_work_fn(ubi);
345 return_unused_pool_pebs(ubi, &ubi->fm_pool);
346 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
349 for (i = 0; i < ubi->fm->used_blocks; i++)
350 kfree(ubi->fm->e[i]);
356 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
357 * See find_mean_wl_entry()
359 * @ubi: UBI device description object
360 * @e: physical eraseblock to return
361 * @root: RB tree to test against.
363 static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
364 struct ubi_wl_entry *e,
365 struct rb_root *root) {
366 if (e && !ubi->fm_disabled && !ubi->fm &&
367 e->pnum < UBI_FM_MAX_START)
368 e = rb_entry(rb_next(root->rb_node),
369 struct ubi_wl_entry, u.rb);