/** * find_anchor_wl_entry - find wear-leveling entry to used as anchor PEB. * @root: the RB-tree where to look for
*/ staticstruct ubi_wl_entry *find_anchor_wl_entry(struct rb_root *root)
{ struct rb_node *p; struct ubi_wl_entry *e, *victim = NULL; int max_ec = UBI_MAX_ERASECOUNTER;
/** * return_unused_pool_pebs - returns unused PEB to the free tree. * @ubi: UBI device description object * @pool: fastmap pool description object
*/ staticvoid return_unused_pool_pebs(struct ubi_device *ubi, struct ubi_fm_pool *pool)
{ int i; struct ubi_wl_entry *e;
for (i = pool->used; i < pool->size; i++) {
e = ubi->lookuptbl[pool->pebs[i]];
return_unused_peb(ubi, e);
}
}
/** * ubi_wl_get_fm_peb - find a physical erase block with a given maximal number. * @ubi: UBI device description object * @anchor: This PEB will be used as anchor PEB by fastmap * * The function returns a physical erase block with a given maximal number * and removes it from the wl subsystem. * Must be called with wl_lock held!
*/ struct ubi_wl_entry *ubi_wl_get_fm_peb(struct ubi_device *ubi, int anchor)
{ struct ubi_wl_entry *e = NULL;
if (!ubi->free.rb_node) goto out;
if (anchor)
e = find_anchor_wl_entry(&ubi->free); else
e = find_mean_wl_entry(ubi, &ubi->free);
if (!e) goto out;
self_check_in_wl_tree(ubi, e, &ubi->free);
/* remove it from the free list,
* the wl subsystem does no longer know this erase block */
rb_erase(&e->u.rb, &ubi->free);
ubi->free_count--;
out: return e;
}
/* * wait_free_pebs_for_pool - wait until there enough free pebs * @ubi: UBI device description object * * Wait and execute do_work until there are enough free pebs, fill pool * as much as we can. This will reduce pool refilling times, which can * reduce the fastmap updating frequency.
*/ staticvoid wait_free_pebs_for_pool(struct ubi_device *ubi)
{ struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; struct ubi_fm_pool *pool = &ubi->fm_pool; int free, expect_free, executed; /* * There are at least following free pebs which reserved by UBI: * 1. WL_RESERVED_PEBS[1] * 2. EBA_RESERVED_PEBS[1] * 3. fm pebs - 1: Twice fastmap size deducted by fastmap and fm_anchor * 4. beb_rsvd_pebs: This value should be get under lock ubi->wl_lock
*/ int reserved = WL_RESERVED_PEBS + EBA_RESERVED_PEBS +
ubi->fm_size / ubi->leb_size - 1 + ubi->fm_pool_rsv_cnt;
/* * Break out if there are no works or work is executed failure, * given the fact that erase_worker will schedule itself when * -EBUSY is returned from mtd layer caused by system shutdown.
*/ if (do_work(ubi, &executed) || !executed) break;
} while (free < expect_free);
}
/* * left_free_count - returns the number of free pebs to fill fm pools * @ubi: UBI device description object * * This helper function returns the number of free pebs (deducted * by fastmap pebs) to fill fm_pool and fm_wl_pool.
*/ staticint left_free_count(struct ubi_device *ubi)
{ int fm_used = 0; // fastmap non anchor pebs.
/* * can_fill_pools - whether free PEBs will be left after filling pools * @ubi: UBI device description object * @free: current number of free PEBs * * Return %1 if there are still left free PEBs after filling pools, * otherwise %0 is returned.
*/ staticint can_fill_pools(struct ubi_device *ubi, int free)
{ struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; struct ubi_fm_pool *pool = &ubi->fm_pool; int pool_need = pool->max_size - pool->size +
wl_pool->max_size - wl_pool->size;
if (free - pool_need < 1) return 0;
return 1;
}
/** * ubi_refill_pools_and_lock - refills all fastmap PEB pools and takes fm locks. * @ubi: UBI device description object
*/ void ubi_refill_pools_and_lock(struct ubi_device *ubi)
{ struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool; struct ubi_fm_pool *pool = &ubi->fm_pool; struct ubi_wl_entry *e; int enough;
if (!ubi->ro_mode && !ubi->fm_disabled)
wait_free_pebs_for_pool(ubi);
if (ubi->fm_anchor) {
wl_tree_add(ubi->fm_anchor, &ubi->free);
ubi->free_count++;
ubi->fm_anchor = NULL;
}
if (!ubi->fm_disabled) /* * All available PEBs are in ubi->free, now is the time to get * the best anchor PEBs.
*/
ubi->fm_anchor = ubi_wl_get_fm_peb(ubi, 1);
for (;;) {
enough = 0; if (pool->size < pool->max_size) { if (left_free_count(ubi) <= 0) break;
/** * produce_free_peb - produce a free physical eraseblock. * @ubi: UBI device description object * * This function tries to make a free PEB by means of synchronous execution of * pending works. This may be needed if, for example the background thread is * disabled. Returns zero in case of success and a negative error code in case * of failure.
*/ staticint produce_free_peb(struct ubi_device *ubi)
{ int err;
while (!ubi->free.rb_node && ubi->works_count) {
dbg_wl("do one work synchronously");
err = do_work(ubi, NULL);
if (err) return err;
}
return 0;
}
/** * ubi_wl_get_peb - get a physical eraseblock. * @ubi: UBI device description object * * This function returns a physical eraseblock in case of success and a * negative error code in case of failure. * Returns with ubi->fm_eba_sem held in read mode!
*/ int ubi_wl_get_peb(struct ubi_device *ubi)
{ int ret, attempts = 0; struct ubi_fm_pool *pool = &ubi->fm_pool; struct ubi_fm_pool *wl_pool = &ubi->fm_wl_pool;
/* We check here also for the WL pool because at this point we can
* refill the WL pool synchronous. */ if (pool->used == pool->size || wl_pool->used == wl_pool->size) {
spin_unlock(&ubi->wl_lock);
up_read(&ubi->fm_eba_sem);
ret = ubi_update_fastmap(ubi); if (ret) {
ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
down_read(&ubi->fm_eba_sem); return -ENOSPC;
}
down_read(&ubi->fm_eba_sem);
spin_lock(&ubi->wl_lock);
}
if (pool->used == pool->size) {
spin_unlock(&ubi->wl_lock);
attempts++; if (attempts == 10) {
ubi_err(ubi, "Unable to get a free PEB from user WL pool");
ret = -ENOSPC; goto out;
}
up_read(&ubi->fm_eba_sem);
ret = produce_free_peb(ubi); if (ret < 0) {
down_read(&ubi->fm_eba_sem); goto out;
} goto again;
}
/** * next_peb_for_wl - returns next PEB to be used internally by the * WL sub-system. * * @ubi: UBI device description object * @need_fill: whether to fill wear-leveling pool when no PEBs are found
*/ staticstruct ubi_wl_entry *next_peb_for_wl(struct ubi_device *ubi, bool need_fill)
{ struct ubi_fm_pool *pool = &ubi->fm_wl_pool; int pnum;
if (pool->used == pool->size) { if (need_fill && !ubi->fm_work_scheduled) { /* * We cannot update the fastmap here because this * function is called in atomic context. * Let's fail here and refill/update it as soon as * possible.
*/
ubi->fm_work_scheduled = 1;
schedule_work(&ubi->fm_work);
} return NULL;
}
/** * need_wear_leveling - checks whether to trigger a wear leveling work. * UBI fetches free PEB from wl_pool, we check free PEBs from both 'wl_pool' * and 'ubi->free', because free PEB in 'ubi->free' tree maybe moved into * 'wl_pool' by ubi_refill_pools(). * * @ubi: UBI device description object
*/ staticbool need_wear_leveling(struct ubi_device *ubi)
{ int ec; struct ubi_wl_entry *e;
if (!ubi->used.rb_node) returnfalse;
e = next_peb_for_wl(ubi, false); if (!e) { if (!ubi->free.rb_node) returnfalse;
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
ec = e->ec;
} else {
ec = e->ec; if (ubi->free.rb_node) {
e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF, 0);
ec = max(ec, e->ec);
}
}
e = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb);
return ec - e->ec >= UBI_WL_THRESHOLD;
}
/* get_peb_for_wl - returns a PEB to be used internally by the WL sub-system. * * @ubi: UBI device description object
*/ staticstruct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi)
{ struct ubi_fm_pool *pool = &ubi->fm_wl_pool; int pnum;
ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem));
if (pool->used == pool->size) { /* We cannot update the fastmap here because this * function is called in atomic context.
* Let's fail here and refill/update it as soon as possible. */ if (!ubi->fm_work_scheduled) {
ubi->fm_work_scheduled = 1;
schedule_work(&ubi->fm_work);
} return NULL;
}
/** * ubi_ensure_anchor_pebs - schedule wear-leveling to produce an anchor PEB. * @ubi: UBI device description object
*/ int ubi_ensure_anchor_pebs(struct ubi_device *ubi)
{ struct ubi_work *wrk; struct ubi_wl_entry *anchor;
spin_lock(&ubi->wl_lock);
/* Do we already have an anchor? */ if (ubi->fm_anchor) {
spin_unlock(&ubi->wl_lock); return 0;
}
/* See if we can find an anchor PEB on the list of free PEBs */
anchor = ubi_wl_get_fm_peb(ubi, 1); if (anchor) {
ubi->fm_anchor = anchor;
spin_unlock(&ubi->wl_lock); return 0;
}
ubi->fm_do_produce_anchor = 1; /* No luck, trigger wear leveling to produce a new anchor PEB. */ if (ubi->wl_scheduled) {
spin_unlock(&ubi->wl_lock); return 0;
}
ubi->wl_scheduled = 1;
spin_unlock(&ubi->wl_lock);
/** * ubi_wl_put_fm_peb - returns a PEB used in a fastmap to the wear-leveling * sub-system. * see: ubi_wl_put_peb() * * @ubi: UBI device description object * @fm_e: physical eraseblock to return * @lnum: the last used logical eraseblock number for the PEB * @torture: if this physical eraseblock has to be tortured
*/ int ubi_wl_put_fm_peb(struct ubi_device *ubi, struct ubi_wl_entry *fm_e, int lnum, int torture)
{ struct ubi_wl_entry *e; int vol_id, pnum = fm_e->pnum;
spin_lock(&ubi->wl_lock);
e = ubi->lookuptbl[pnum];
/* This can happen if we recovered from a fastmap the very * first time and writing now a new one. In this case the wl system * has never seen any PEB used by the original fastmap.
*/ if (!e) {
e = fm_e;
ubi_assert(e->ec >= 0);
ubi->lookuptbl[pnum] = e;
}
/** * ubi_is_erase_work - checks whether a work is erase work. * @wrk: The work object to be checked
*/ int ubi_is_erase_work(struct ubi_work *wrk)
{ return wrk->func == erase_worker;
}
staticvoid ubi_fastmap_close(struct ubi_device *ubi)
{ int i;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.