2 * Copyright (c) 2012 Linutronix GmbH
3 * Copyright (c) 2014 sigma star gmbh
4 * Author: Richard Weinberger <richard@nod.at>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
18 * update_fastmap_work_fn - calls ubi_update_fastmap from a work queue
19 * @wrk: the work description object
21 static void update_fastmap_work_fn(struct work_struct *wrk)
23 struct ubi_device *ubi = container_of(wrk, struct ubi_device, fm_work);
25 ubi_update_fastmap(ubi);
26 spin_lock(&ubi->wl_lock);
27 ubi->fm_work_scheduled = 0;
28 spin_unlock(&ubi->wl_lock);
32 * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB.
33 * @root: the RB-tree where to look for
35 static struct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
38 struct ubi_wl_entry *e, *victim = NULL;
39 int max_ec = UBI_MAX_ERASECOUNTER;
41 ubi_rb_for_each_entry(p, e, root, u.rb) {
42 if (e->pnum < UBI_FM_MAX_START && e->ec < max_ec) {
52 * return_unused_pool_pebs - returns unused PEB to the free tree.
53 * @ubi: UBI device description object
54 * @pool: fastmap pool description object
56 static void return_unused_pool_pebs(struct ubi_device *ubi,
57 struct ubi_fm_pool *pool)
60 struct ubi_wl_entry *e;
62 for (i = pool->used; i < pool->size; i++) {
63 e = ubi->lookuptbl[pool->pebs[i]];
64 wl_tree_add(e, &ubi->free);
69 static int anchor_pebs_avalible(struct rb_root *root)
72 struct ubi_wl_entry *e;
74 ubi_rb_for_each_entry(p, e, root, u.rb)
75 if (e->pnum < UBI_FM_MAX_START)
82 * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number.
83 * @ubi: UBI device description object
84 * @anchor: This PEB will be used as anchor PEB by fastmap
86 * The function returns a physical erase block with a given maximal number
87 * and removes it from the wl subsystem.
88 * Must be called with wl_lock held!
90 struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
92 struct ubi_wl_entry *e = NULL;
94 if (!ubi->free.rb_node || (ubi->free_count - ubi->beb_rsvd_pebs < 1))
98 e = find_anchor_wl_entry(&ubi->free);
100 e = find_mean_wl_entry(ubi, &ubi->free);
105 self_check_in_wl_tree(ubi, e, &ubi->free);
107 /* remove it from the free list,
108 * the wl subsystem does no longer know this erase block */
109 rb_erase(&e->u.rb, &ubi->free);
116 * ubi_refill_pools - refills all fastmap PEB pools.
117 * @ubi: UBI device description object
119 void ubi_refill_pools(struct ubi_device *ubi)
121 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
122 struct ubi_fm_pool *pool = &ubi->fm_pool;
123 struct ubi_wl_entry *e;
126 spin_lock(&ubi->wl_lock);
128 return_unused_pool_pebs(ubi, wl_pool);
129 return_unused_pool_pebs(ubi, pool);
136 if (pool->size < pool->max_size) {
137 if (!ubi->free.rb_node)
144 pool->pebs[pool->size] = e->pnum;
149 if (wl_pool->size < wl_pool->max_size) {
150 if (!ubi->free.rb_node ||
151 (ubi->free_count - ubi->beb_rsvd_pebs < 5))
154 e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF);
155 self_check_in_wl_tree(ubi, e, &ubi->free);
156 rb_erase(&e->u.rb, &ubi->free);
159 wl_pool->pebs[wl_pool->size] = e->pnum;
171 spin_unlock(&ubi->wl_lock);
175 * ubi_wl_get_peb - get a physical eraseblock.
176 * @ubi: UBI device description object
178 * This function returns a physical eraseblock in case of success and a
179 * negative error code in case of failure.
180 * Returns with ubi->fm_eba_sem held in read mode!
182 int ubi_wl_get_peb(struct ubi_device *ubi)
184 int ret, retried = 0;
185 struct ubi_fm_pool *pool = &ubi->fm_pool;
186 struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
189 down_read(&ubi->fm_eba_sem);
190 spin_lock(&ubi->wl_lock);
192 /* We check here also for the WL pool because at this point we can
193 * refill the WL pool synchronous. */
194 if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
195 spin_unlock(&ubi->wl_lock);
196 up_read(&ubi->fm_eba_sem);
197 ret = ubi_update_fastmap(ubi);
199 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
200 down_read(&ubi->fm_eba_sem);
203 down_read(&ubi->fm_eba_sem);
204 spin_lock(&ubi->wl_lock);
207 if (pool->used == pool->size) {
208 spin_unlock(&ubi->wl_lock);
210 ubi_err(ubi, "Unable to get a free PEB from user WL pool");
215 up_read(&ubi->fm_eba_sem);
219 ubi_assert(pool->used < pool->size);
220 ret = pool->pebs[pool->used++];
221 prot_queue_add(ubi, ubi->lookuptbl[ret]);
222 spin_unlock(&ubi->wl_lock);
227 /* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system.
229 * @ubi: UBI device description object
231 static struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
233 struct ubi_fm_pool *pool = &ubi->fm_wl_pool;
236 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 pnum = pool->pebs[pool->used++];
248 return ubi->lookuptbl[pnum];
252 * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB.
253 * @ubi: UBI device description object
255 int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
257 struct ubi_work *wrk;
259 spin_lock(&ubi->wl_lock);
260 if (ubi->wl_scheduled) {
261 spin_unlock(&ubi->wl_lock);
264 ubi->wl_scheduled = 1;
265 spin_unlock(&ubi->wl_lock);
267 wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS);
269 spin_lock(&ubi->wl_lock);
270 ubi->wl_scheduled = 0;
271 spin_unlock(&ubi->wl_lock);
276 wrk->func = &wear_leveling_worker;
277 schedule_ubi_work(ubi, wrk);
282 * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling
284 * see: ubi_wl_put_peb()
286 * @ubi: UBI device description object
287 * @fm_e: physical eraseblock to return
288 * @lnum: the last used logical eraseblock number for the PEB
289 * @torture: if this physical eraseblock has to be tortured
291 int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e,
292 int lnum, int torture)
294 struct ubi_wl_entry *e;
295 int vol_id, pnum = fm_e->pnum;
297 dbg_wl("PEB %d", pnum);
299 ubi_assert(pnum >= 0);
300 ubi_assert(pnum < ubi->peb_count);
302 spin_lock(&ubi->wl_lock);
303 e = ubi->lookuptbl[pnum];
305 /* This can happen if we recovered from a fastmap the very
306 * first time and writing now a new one. In this case the wl system
307 * has never seen any PEB used by the original fastmap.
311 ubi_assert(e->ec >= 0);
312 ubi->lookuptbl[pnum] = e;
315 spin_unlock(&ubi->wl_lock);
317 vol_id = lnum ? UBI_FM_DATA_VOLUME_ID : UBI_FM_SB_VOLUME_ID;
318 return schedule_erase(ubi, e, vol_id, lnum, torture);
322 * ubi_is_erase_work - checks whether a work is erase work.
323 * @wrk: The work object to be checked
325 int ubi_is_erase_work(struct ubi_work *wrk)
327 return wrk->func == erase_worker;
330 static void ubi_fastmap_close(struct ubi_device *ubi)
334 flush_work(&ubi->fm_work);
335 return_unused_pool_pebs(ubi, &ubi->fm_pool);
336 return_unused_pool_pebs(ubi, &ubi->fm_wl_pool);
339 for (i = 0; i < ubi->fm->used_blocks; i++)
340 kfree(ubi->fm->e[i]);
346 * may_reserve_for_fm - tests whether a PEB shall be reserved for fastmap.
347 * See find_mean_wl_entry()
349 * @ubi: UBI device description object
350 * @e: physical eraseblock to return
351 * @root: RB tree to test against.
353 static struct ubi_wl_entry *may_reserve_for_fm(struct ubi_device *ubi,
354 struct ubi_wl_entry *e,
355 struct rb_root *root) {
356 if (e && !ubi->fm_disabled && !ubi->fm &&
357 e->pnum < UBI_FM_MAX_START)
358 e = rb_entry(rb_next(root->rb_node),
359 struct ubi_wl_entry, u.rb);