162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) International Business Machines Corp., 2006 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner 662306a36Sopenharmony_ci */ 762306a36Sopenharmony_ci 862306a36Sopenharmony_ci/* 962306a36Sopenharmony_ci * UBI wear-leveling sub-system. 1062306a36Sopenharmony_ci * 1162306a36Sopenharmony_ci * This sub-system is responsible for wear-leveling. It works in terms of 1262306a36Sopenharmony_ci * physical eraseblocks and erase counters and knows nothing about logical 1362306a36Sopenharmony_ci * eraseblocks, volumes, etc. From this sub-system's perspective all physical 1462306a36Sopenharmony_ci * eraseblocks are of two types - used and free. Used physical eraseblocks are 1562306a36Sopenharmony_ci * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical 1662306a36Sopenharmony_ci * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function. 1762306a36Sopenharmony_ci * 1862306a36Sopenharmony_ci * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter 1962306a36Sopenharmony_ci * header. The rest of the physical eraseblock contains only %0xFF bytes. 2062306a36Sopenharmony_ci * 2162306a36Sopenharmony_ci * When physical eraseblocks are returned to the WL sub-system by means of the 2262306a36Sopenharmony_ci * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is 2362306a36Sopenharmony_ci * done asynchronously in context of the per-UBI device background thread, 2462306a36Sopenharmony_ci * which is also managed by the WL sub-system. 2562306a36Sopenharmony_ci * 2662306a36Sopenharmony_ci * The wear-leveling is ensured by means of moving the contents of used 2762306a36Sopenharmony_ci * physical eraseblocks with low erase counter to free physical eraseblocks 2862306a36Sopenharmony_ci * with high erase counter. 2962306a36Sopenharmony_ci * 3062306a36Sopenharmony_ci * If the WL sub-system fails to erase a physical eraseblock, it marks it as 3162306a36Sopenharmony_ci * bad. 3262306a36Sopenharmony_ci * 3362306a36Sopenharmony_ci * This sub-system is also responsible for scrubbing. If a bit-flip is detected 3462306a36Sopenharmony_ci * in a physical eraseblock, it has to be moved. Technically this is the same 3562306a36Sopenharmony_ci * as moving it for wear-leveling reasons. 3662306a36Sopenharmony_ci * 3762306a36Sopenharmony_ci * As it was said, for the UBI sub-system all physical eraseblocks are either 3862306a36Sopenharmony_ci * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while 3962306a36Sopenharmony_ci * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub 4062306a36Sopenharmony_ci * RB-trees, as well as (temporarily) in the @wl->pq queue. 4162306a36Sopenharmony_ci * 4262306a36Sopenharmony_ci * When the WL sub-system returns a physical eraseblock, the physical 4362306a36Sopenharmony_ci * eraseblock is protected from being moved for some "time". For this reason, 4462306a36Sopenharmony_ci * the physical eraseblock is not directly moved from the @wl->free tree to the 4562306a36Sopenharmony_ci * @wl->used tree. There is a protection queue in between where this 4662306a36Sopenharmony_ci * physical eraseblock is temporarily stored (@wl->pq). 4762306a36Sopenharmony_ci * 4862306a36Sopenharmony_ci * All this protection stuff is needed because: 4962306a36Sopenharmony_ci * o we don't want to move physical eraseblocks just after we have given them 5062306a36Sopenharmony_ci * to the user; instead, we first want to let users fill them up with data; 5162306a36Sopenharmony_ci * 5262306a36Sopenharmony_ci * o there is a chance that the user will put the physical eraseblock very 5362306a36Sopenharmony_ci * soon, so it makes sense not to move it for some time, but wait. 5462306a36Sopenharmony_ci * 5562306a36Sopenharmony_ci * Physical eraseblocks stay protected only for limited time. But the "time" is 5662306a36Sopenharmony_ci * measured in erase cycles in this case. This is implemented with help of the 5762306a36Sopenharmony_ci * protection queue. Eraseblocks are put to the tail of this queue when they 5862306a36Sopenharmony_ci * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the 5962306a36Sopenharmony_ci * head of the queue on each erase operation (for any eraseblock). So the 6062306a36Sopenharmony_ci * length of the queue defines how may (global) erase cycles PEBs are protected. 6162306a36Sopenharmony_ci * 6262306a36Sopenharmony_ci * To put it differently, each physical eraseblock has 2 main states: free and 6362306a36Sopenharmony_ci * used. The former state corresponds to the @wl->free tree. The latter state 6462306a36Sopenharmony_ci * is split up on several sub-states: 6562306a36Sopenharmony_ci * o the WL movement is allowed (@wl->used tree); 6662306a36Sopenharmony_ci * o the WL movement is disallowed (@wl->erroneous) because the PEB is 6762306a36Sopenharmony_ci * erroneous - e.g., there was a read error; 6862306a36Sopenharmony_ci * o the WL movement is temporarily prohibited (@wl->pq queue); 6962306a36Sopenharmony_ci * o scrubbing is needed (@wl->scrub tree). 7062306a36Sopenharmony_ci * 7162306a36Sopenharmony_ci * Depending on the sub-state, wear-leveling entries of the used physical 7262306a36Sopenharmony_ci * eraseblocks may be kept in one of those structures. 7362306a36Sopenharmony_ci * 7462306a36Sopenharmony_ci * Note, in this implementation, we keep a small in-RAM object for each physical 7562306a36Sopenharmony_ci * eraseblock. This is surely not a scalable solution. But it appears to be good 7662306a36Sopenharmony_ci * enough for moderately large flashes and it is simple. In future, one may 7762306a36Sopenharmony_ci * re-work this sub-system and make it more scalable. 7862306a36Sopenharmony_ci * 7962306a36Sopenharmony_ci * At the moment this sub-system does not utilize the sequence number, which 8062306a36Sopenharmony_ci * was introduced relatively recently. But it would be wise to do this because 8162306a36Sopenharmony_ci * the sequence number of a logical eraseblock characterizes how old is it. For 8262306a36Sopenharmony_ci * example, when we move a PEB with low erase counter, and we need to pick the 8362306a36Sopenharmony_ci * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we 8462306a36Sopenharmony_ci * pick target PEB with an average EC if our PEB is not very "old". This is a 8562306a36Sopenharmony_ci * room for future re-works of the WL sub-system. 8662306a36Sopenharmony_ci */ 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ci#include <linux/slab.h> 8962306a36Sopenharmony_ci#include <linux/crc32.h> 9062306a36Sopenharmony_ci#include <linux/freezer.h> 9162306a36Sopenharmony_ci#include <linux/kthread.h> 9262306a36Sopenharmony_ci#include "ubi.h" 9362306a36Sopenharmony_ci#include "wl.h" 9462306a36Sopenharmony_ci 9562306a36Sopenharmony_ci/* Number of physical eraseblocks reserved for wear-leveling purposes */ 9662306a36Sopenharmony_ci#define WL_RESERVED_PEBS 1 9762306a36Sopenharmony_ci 9862306a36Sopenharmony_ci/* 9962306a36Sopenharmony_ci * Maximum difference between two erase counters. If this threshold is 10062306a36Sopenharmony_ci * exceeded, the WL sub-system starts moving data from used physical 10162306a36Sopenharmony_ci * eraseblocks with low erase counter to free physical eraseblocks with high 10262306a36Sopenharmony_ci * erase counter. 10362306a36Sopenharmony_ci */ 10462306a36Sopenharmony_ci#define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD 10562306a36Sopenharmony_ci 10662306a36Sopenharmony_ci/* 10762306a36Sopenharmony_ci * When a physical eraseblock is moved, the WL sub-system has to pick the target 10862306a36Sopenharmony_ci * physical eraseblock to move to. The simplest way would be just to pick the 10962306a36Sopenharmony_ci * one with the highest erase counter. But in certain workloads this could lead 11062306a36Sopenharmony_ci * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a 11162306a36Sopenharmony_ci * situation when the picked physical eraseblock is constantly erased after the 11262306a36Sopenharmony_ci * data is written to it. So, we have a constant which limits the highest erase 11362306a36Sopenharmony_ci * counter of the free physical eraseblock to pick. Namely, the WL sub-system 11462306a36Sopenharmony_ci * does not pick eraseblocks with erase counter greater than the lowest erase 11562306a36Sopenharmony_ci * counter plus %WL_FREE_MAX_DIFF. 11662306a36Sopenharmony_ci */ 11762306a36Sopenharmony_ci#define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD) 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci/* 12062306a36Sopenharmony_ci * Maximum number of consecutive background thread failures which is enough to 12162306a36Sopenharmony_ci * switch to read-only mode. 12262306a36Sopenharmony_ci */ 12362306a36Sopenharmony_ci#define WL_MAX_FAILURES 32 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_cistatic int self_check_ec(struct ubi_device *ubi, int pnum, int ec); 12662306a36Sopenharmony_cistatic int self_check_in_wl_tree(const struct ubi_device *ubi, 12762306a36Sopenharmony_ci struct ubi_wl_entry *e, struct rb_root *root); 12862306a36Sopenharmony_cistatic int self_check_in_pq(const struct ubi_device *ubi, 12962306a36Sopenharmony_ci struct ubi_wl_entry *e); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci/** 13262306a36Sopenharmony_ci * wl_tree_add - add a wear-leveling entry to a WL RB-tree. 13362306a36Sopenharmony_ci * @e: the wear-leveling entry to add 13462306a36Sopenharmony_ci * @root: the root of the tree 13562306a36Sopenharmony_ci * 13662306a36Sopenharmony_ci * Note, we use (erase counter, physical eraseblock number) pairs as keys in 13762306a36Sopenharmony_ci * the @ubi->used and @ubi->free RB-trees. 13862306a36Sopenharmony_ci */ 13962306a36Sopenharmony_cistatic void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root) 14062306a36Sopenharmony_ci{ 14162306a36Sopenharmony_ci struct rb_node **p, *parent = NULL; 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci p = &root->rb_node; 14462306a36Sopenharmony_ci while (*p) { 14562306a36Sopenharmony_ci struct ubi_wl_entry *e1; 14662306a36Sopenharmony_ci 14762306a36Sopenharmony_ci parent = *p; 14862306a36Sopenharmony_ci e1 = rb_entry(parent, struct ubi_wl_entry, u.rb); 14962306a36Sopenharmony_ci 15062306a36Sopenharmony_ci if (e->ec < e1->ec) 15162306a36Sopenharmony_ci p = &(*p)->rb_left; 15262306a36Sopenharmony_ci else if (e->ec > e1->ec) 15362306a36Sopenharmony_ci p = &(*p)->rb_right; 15462306a36Sopenharmony_ci else { 15562306a36Sopenharmony_ci ubi_assert(e->pnum != e1->pnum); 15662306a36Sopenharmony_ci if (e->pnum < e1->pnum) 15762306a36Sopenharmony_ci p = &(*p)->rb_left; 15862306a36Sopenharmony_ci else 15962306a36Sopenharmony_ci p = &(*p)->rb_right; 16062306a36Sopenharmony_ci } 16162306a36Sopenharmony_ci } 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci rb_link_node(&e->u.rb, parent, p); 16462306a36Sopenharmony_ci rb_insert_color(&e->u.rb, root); 16562306a36Sopenharmony_ci} 16662306a36Sopenharmony_ci 16762306a36Sopenharmony_ci/** 16862306a36Sopenharmony_ci * wl_entry_destroy - destroy a wear-leveling entry. 16962306a36Sopenharmony_ci * @ubi: UBI device description object 17062306a36Sopenharmony_ci * @e: the wear-leveling entry to add 17162306a36Sopenharmony_ci * 17262306a36Sopenharmony_ci * This function destroys a wear leveling entry and removes 17362306a36Sopenharmony_ci * the reference from the lookup table. 17462306a36Sopenharmony_ci */ 17562306a36Sopenharmony_cistatic void wl_entry_destroy(struct ubi_device *ubi, struct ubi_wl_entry *e) 17662306a36Sopenharmony_ci{ 17762306a36Sopenharmony_ci ubi->lookuptbl[e->pnum] = NULL; 17862306a36Sopenharmony_ci kmem_cache_free(ubi_wl_entry_slab, e); 17962306a36Sopenharmony_ci} 18062306a36Sopenharmony_ci 18162306a36Sopenharmony_ci/** 18262306a36Sopenharmony_ci * do_work - do one pending work. 18362306a36Sopenharmony_ci * @ubi: UBI device description object 18462306a36Sopenharmony_ci * 18562306a36Sopenharmony_ci * This function returns zero in case of success and a negative error code in 18662306a36Sopenharmony_ci * case of failure. 18762306a36Sopenharmony_ci */ 18862306a36Sopenharmony_cistatic int do_work(struct ubi_device *ubi) 18962306a36Sopenharmony_ci{ 19062306a36Sopenharmony_ci int err; 19162306a36Sopenharmony_ci struct ubi_work *wrk; 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci cond_resched(); 19462306a36Sopenharmony_ci 19562306a36Sopenharmony_ci /* 19662306a36Sopenharmony_ci * @ubi->work_sem is used to synchronize with the workers. Workers take 19762306a36Sopenharmony_ci * it in read mode, so many of them may be doing works at a time. But 19862306a36Sopenharmony_ci * the queue flush code has to be sure the whole queue of works is 19962306a36Sopenharmony_ci * done, and it takes the mutex in write mode. 20062306a36Sopenharmony_ci */ 20162306a36Sopenharmony_ci down_read(&ubi->work_sem); 20262306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 20362306a36Sopenharmony_ci if (list_empty(&ubi->works)) { 20462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 20562306a36Sopenharmony_ci up_read(&ubi->work_sem); 20662306a36Sopenharmony_ci return 0; 20762306a36Sopenharmony_ci } 20862306a36Sopenharmony_ci 20962306a36Sopenharmony_ci wrk = list_entry(ubi->works.next, struct ubi_work, list); 21062306a36Sopenharmony_ci list_del(&wrk->list); 21162306a36Sopenharmony_ci ubi->works_count -= 1; 21262306a36Sopenharmony_ci ubi_assert(ubi->works_count >= 0); 21362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 21462306a36Sopenharmony_ci 21562306a36Sopenharmony_ci /* 21662306a36Sopenharmony_ci * Call the worker function. Do not touch the work structure 21762306a36Sopenharmony_ci * after this call as it will have been freed or reused by that 21862306a36Sopenharmony_ci * time by the worker function. 21962306a36Sopenharmony_ci */ 22062306a36Sopenharmony_ci err = wrk->func(ubi, wrk, 0); 22162306a36Sopenharmony_ci if (err) 22262306a36Sopenharmony_ci ubi_err(ubi, "work failed with error code %d", err); 22362306a36Sopenharmony_ci up_read(&ubi->work_sem); 22462306a36Sopenharmony_ci 22562306a36Sopenharmony_ci return err; 22662306a36Sopenharmony_ci} 22762306a36Sopenharmony_ci 22862306a36Sopenharmony_ci/** 22962306a36Sopenharmony_ci * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree. 23062306a36Sopenharmony_ci * @e: the wear-leveling entry to check 23162306a36Sopenharmony_ci * @root: the root of the tree 23262306a36Sopenharmony_ci * 23362306a36Sopenharmony_ci * This function returns non-zero if @e is in the @root RB-tree and zero if it 23462306a36Sopenharmony_ci * is not. 23562306a36Sopenharmony_ci */ 23662306a36Sopenharmony_cistatic int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root) 23762306a36Sopenharmony_ci{ 23862306a36Sopenharmony_ci struct rb_node *p; 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_ci p = root->rb_node; 24162306a36Sopenharmony_ci while (p) { 24262306a36Sopenharmony_ci struct ubi_wl_entry *e1; 24362306a36Sopenharmony_ci 24462306a36Sopenharmony_ci e1 = rb_entry(p, struct ubi_wl_entry, u.rb); 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci if (e->pnum == e1->pnum) { 24762306a36Sopenharmony_ci ubi_assert(e == e1); 24862306a36Sopenharmony_ci return 1; 24962306a36Sopenharmony_ci } 25062306a36Sopenharmony_ci 25162306a36Sopenharmony_ci if (e->ec < e1->ec) 25262306a36Sopenharmony_ci p = p->rb_left; 25362306a36Sopenharmony_ci else if (e->ec > e1->ec) 25462306a36Sopenharmony_ci p = p->rb_right; 25562306a36Sopenharmony_ci else { 25662306a36Sopenharmony_ci ubi_assert(e->pnum != e1->pnum); 25762306a36Sopenharmony_ci if (e->pnum < e1->pnum) 25862306a36Sopenharmony_ci p = p->rb_left; 25962306a36Sopenharmony_ci else 26062306a36Sopenharmony_ci p = p->rb_right; 26162306a36Sopenharmony_ci } 26262306a36Sopenharmony_ci } 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci return 0; 26562306a36Sopenharmony_ci} 26662306a36Sopenharmony_ci 26762306a36Sopenharmony_ci/** 26862306a36Sopenharmony_ci * in_pq - check if a wear-leveling entry is present in the protection queue. 26962306a36Sopenharmony_ci * @ubi: UBI device description object 27062306a36Sopenharmony_ci * @e: the wear-leveling entry to check 27162306a36Sopenharmony_ci * 27262306a36Sopenharmony_ci * This function returns non-zero if @e is in the protection queue and zero 27362306a36Sopenharmony_ci * if it is not. 27462306a36Sopenharmony_ci */ 27562306a36Sopenharmony_cistatic inline int in_pq(const struct ubi_device *ubi, struct ubi_wl_entry *e) 27662306a36Sopenharmony_ci{ 27762306a36Sopenharmony_ci struct ubi_wl_entry *p; 27862306a36Sopenharmony_ci int i; 27962306a36Sopenharmony_ci 28062306a36Sopenharmony_ci for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) 28162306a36Sopenharmony_ci list_for_each_entry(p, &ubi->pq[i], u.list) 28262306a36Sopenharmony_ci if (p == e) 28362306a36Sopenharmony_ci return 1; 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci return 0; 28662306a36Sopenharmony_ci} 28762306a36Sopenharmony_ci 28862306a36Sopenharmony_ci/** 28962306a36Sopenharmony_ci * prot_queue_add - add physical eraseblock to the protection queue. 29062306a36Sopenharmony_ci * @ubi: UBI device description object 29162306a36Sopenharmony_ci * @e: the physical eraseblock to add 29262306a36Sopenharmony_ci * 29362306a36Sopenharmony_ci * This function adds @e to the tail of the protection queue @ubi->pq, where 29462306a36Sopenharmony_ci * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be 29562306a36Sopenharmony_ci * temporarily protected from the wear-leveling worker. Note, @wl->lock has to 29662306a36Sopenharmony_ci * be locked. 29762306a36Sopenharmony_ci */ 29862306a36Sopenharmony_cistatic void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e) 29962306a36Sopenharmony_ci{ 30062306a36Sopenharmony_ci int pq_tail = ubi->pq_head - 1; 30162306a36Sopenharmony_ci 30262306a36Sopenharmony_ci if (pq_tail < 0) 30362306a36Sopenharmony_ci pq_tail = UBI_PROT_QUEUE_LEN - 1; 30462306a36Sopenharmony_ci ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN); 30562306a36Sopenharmony_ci list_add_tail(&e->u.list, &ubi->pq[pq_tail]); 30662306a36Sopenharmony_ci dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec); 30762306a36Sopenharmony_ci} 30862306a36Sopenharmony_ci 30962306a36Sopenharmony_ci/** 31062306a36Sopenharmony_ci * find_wl_entry - find wear-leveling entry closest to certain erase counter. 31162306a36Sopenharmony_ci * @ubi: UBI device description object 31262306a36Sopenharmony_ci * @root: the RB-tree where to look for 31362306a36Sopenharmony_ci * @diff: maximum possible difference from the smallest erase counter 31462306a36Sopenharmony_ci * 31562306a36Sopenharmony_ci * This function looks for a wear leveling entry with erase counter closest to 31662306a36Sopenharmony_ci * min + @diff, where min is the smallest erase counter. 31762306a36Sopenharmony_ci */ 31862306a36Sopenharmony_cistatic struct ubi_wl_entry *find_wl_entry(struct ubi_device *ubi, 31962306a36Sopenharmony_ci struct rb_root *root, int diff) 32062306a36Sopenharmony_ci{ 32162306a36Sopenharmony_ci struct rb_node *p; 32262306a36Sopenharmony_ci struct ubi_wl_entry *e; 32362306a36Sopenharmony_ci int max; 32462306a36Sopenharmony_ci 32562306a36Sopenharmony_ci e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb); 32662306a36Sopenharmony_ci max = e->ec + diff; 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ci p = root->rb_node; 32962306a36Sopenharmony_ci while (p) { 33062306a36Sopenharmony_ci struct ubi_wl_entry *e1; 33162306a36Sopenharmony_ci 33262306a36Sopenharmony_ci e1 = rb_entry(p, struct ubi_wl_entry, u.rb); 33362306a36Sopenharmony_ci if (e1->ec >= max) 33462306a36Sopenharmony_ci p = p->rb_left; 33562306a36Sopenharmony_ci else { 33662306a36Sopenharmony_ci p = p->rb_right; 33762306a36Sopenharmony_ci e = e1; 33862306a36Sopenharmony_ci } 33962306a36Sopenharmony_ci } 34062306a36Sopenharmony_ci 34162306a36Sopenharmony_ci return e; 34262306a36Sopenharmony_ci} 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ci/** 34562306a36Sopenharmony_ci * find_mean_wl_entry - find wear-leveling entry with medium erase counter. 34662306a36Sopenharmony_ci * @ubi: UBI device description object 34762306a36Sopenharmony_ci * @root: the RB-tree where to look for 34862306a36Sopenharmony_ci * 34962306a36Sopenharmony_ci * This function looks for a wear leveling entry with medium erase counter, 35062306a36Sopenharmony_ci * but not greater or equivalent than the lowest erase counter plus 35162306a36Sopenharmony_ci * %WL_FREE_MAX_DIFF/2. 35262306a36Sopenharmony_ci */ 35362306a36Sopenharmony_cistatic struct ubi_wl_entry *find_mean_wl_entry(struct ubi_device *ubi, 35462306a36Sopenharmony_ci struct rb_root *root) 35562306a36Sopenharmony_ci{ 35662306a36Sopenharmony_ci struct ubi_wl_entry *e, *first, *last; 35762306a36Sopenharmony_ci 35862306a36Sopenharmony_ci first = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb); 35962306a36Sopenharmony_ci last = rb_entry(rb_last(root), struct ubi_wl_entry, u.rb); 36062306a36Sopenharmony_ci 36162306a36Sopenharmony_ci if (last->ec - first->ec < WL_FREE_MAX_DIFF) { 36262306a36Sopenharmony_ci e = rb_entry(root->rb_node, struct ubi_wl_entry, u.rb); 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ci /* If no fastmap has been written and this WL entry can be used 36562306a36Sopenharmony_ci * as anchor PEB, hold it back and return the second best 36662306a36Sopenharmony_ci * WL entry such that fastmap can use the anchor PEB later. */ 36762306a36Sopenharmony_ci e = may_reserve_for_fm(ubi, e, root); 36862306a36Sopenharmony_ci } else 36962306a36Sopenharmony_ci e = find_wl_entry(ubi, root, WL_FREE_MAX_DIFF/2); 37062306a36Sopenharmony_ci 37162306a36Sopenharmony_ci return e; 37262306a36Sopenharmony_ci} 37362306a36Sopenharmony_ci 37462306a36Sopenharmony_ci/** 37562306a36Sopenharmony_ci * wl_get_wle - get a mean wl entry to be used by ubi_wl_get_peb() or 37662306a36Sopenharmony_ci * refill_wl_user_pool(). 37762306a36Sopenharmony_ci * @ubi: UBI device description object 37862306a36Sopenharmony_ci * 37962306a36Sopenharmony_ci * This function returns a wear leveling entry in case of success and 38062306a36Sopenharmony_ci * NULL in case of failure. 38162306a36Sopenharmony_ci */ 38262306a36Sopenharmony_cistatic struct ubi_wl_entry *wl_get_wle(struct ubi_device *ubi) 38362306a36Sopenharmony_ci{ 38462306a36Sopenharmony_ci struct ubi_wl_entry *e; 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci e = find_mean_wl_entry(ubi, &ubi->free); 38762306a36Sopenharmony_ci if (!e) { 38862306a36Sopenharmony_ci ubi_err(ubi, "no free eraseblocks"); 38962306a36Sopenharmony_ci return NULL; 39062306a36Sopenharmony_ci } 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->free); 39362306a36Sopenharmony_ci 39462306a36Sopenharmony_ci /* 39562306a36Sopenharmony_ci * Move the physical eraseblock to the protection queue where it will 39662306a36Sopenharmony_ci * be protected from being moved for some time. 39762306a36Sopenharmony_ci */ 39862306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->free); 39962306a36Sopenharmony_ci ubi->free_count--; 40062306a36Sopenharmony_ci dbg_wl("PEB %d EC %d", e->pnum, e->ec); 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ci return e; 40362306a36Sopenharmony_ci} 40462306a36Sopenharmony_ci 40562306a36Sopenharmony_ci/** 40662306a36Sopenharmony_ci * prot_queue_del - remove a physical eraseblock from the protection queue. 40762306a36Sopenharmony_ci * @ubi: UBI device description object 40862306a36Sopenharmony_ci * @pnum: the physical eraseblock to remove 40962306a36Sopenharmony_ci * 41062306a36Sopenharmony_ci * This function deletes PEB @pnum from the protection queue and returns zero 41162306a36Sopenharmony_ci * in case of success and %-ENODEV if the PEB was not found. 41262306a36Sopenharmony_ci */ 41362306a36Sopenharmony_cistatic int prot_queue_del(struct ubi_device *ubi, int pnum) 41462306a36Sopenharmony_ci{ 41562306a36Sopenharmony_ci struct ubi_wl_entry *e; 41662306a36Sopenharmony_ci 41762306a36Sopenharmony_ci e = ubi->lookuptbl[pnum]; 41862306a36Sopenharmony_ci if (!e) 41962306a36Sopenharmony_ci return -ENODEV; 42062306a36Sopenharmony_ci 42162306a36Sopenharmony_ci if (self_check_in_pq(ubi, e)) 42262306a36Sopenharmony_ci return -ENODEV; 42362306a36Sopenharmony_ci 42462306a36Sopenharmony_ci list_del(&e->u.list); 42562306a36Sopenharmony_ci dbg_wl("deleted PEB %d from the protection queue", e->pnum); 42662306a36Sopenharmony_ci return 0; 42762306a36Sopenharmony_ci} 42862306a36Sopenharmony_ci 42962306a36Sopenharmony_ci/** 43062306a36Sopenharmony_ci * sync_erase - synchronously erase a physical eraseblock. 43162306a36Sopenharmony_ci * @ubi: UBI device description object 43262306a36Sopenharmony_ci * @e: the physical eraseblock to erase 43362306a36Sopenharmony_ci * @torture: if the physical eraseblock has to be tortured 43462306a36Sopenharmony_ci * 43562306a36Sopenharmony_ci * This function returns zero in case of success and a negative error code in 43662306a36Sopenharmony_ci * case of failure. 43762306a36Sopenharmony_ci */ 43862306a36Sopenharmony_cistatic int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, 43962306a36Sopenharmony_ci int torture) 44062306a36Sopenharmony_ci{ 44162306a36Sopenharmony_ci int err; 44262306a36Sopenharmony_ci struct ubi_ec_hdr *ec_hdr; 44362306a36Sopenharmony_ci unsigned long long ec = e->ec; 44462306a36Sopenharmony_ci 44562306a36Sopenharmony_ci dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec); 44662306a36Sopenharmony_ci 44762306a36Sopenharmony_ci err = self_check_ec(ubi, e->pnum, e->ec); 44862306a36Sopenharmony_ci if (err) 44962306a36Sopenharmony_ci return -EINVAL; 45062306a36Sopenharmony_ci 45162306a36Sopenharmony_ci ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 45262306a36Sopenharmony_ci if (!ec_hdr) 45362306a36Sopenharmony_ci return -ENOMEM; 45462306a36Sopenharmony_ci 45562306a36Sopenharmony_ci err = ubi_io_sync_erase(ubi, e->pnum, torture); 45662306a36Sopenharmony_ci if (err < 0) 45762306a36Sopenharmony_ci goto out_free; 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci ec += err; 46062306a36Sopenharmony_ci if (ec > UBI_MAX_ERASECOUNTER) { 46162306a36Sopenharmony_ci /* 46262306a36Sopenharmony_ci * Erase counter overflow. Upgrade UBI and use 64-bit 46362306a36Sopenharmony_ci * erase counters internally. 46462306a36Sopenharmony_ci */ 46562306a36Sopenharmony_ci ubi_err(ubi, "erase counter overflow at PEB %d, EC %llu", 46662306a36Sopenharmony_ci e->pnum, ec); 46762306a36Sopenharmony_ci err = -EINVAL; 46862306a36Sopenharmony_ci goto out_free; 46962306a36Sopenharmony_ci } 47062306a36Sopenharmony_ci 47162306a36Sopenharmony_ci dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec); 47262306a36Sopenharmony_ci 47362306a36Sopenharmony_ci ec_hdr->ec = cpu_to_be64(ec); 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ci err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); 47662306a36Sopenharmony_ci if (err) 47762306a36Sopenharmony_ci goto out_free; 47862306a36Sopenharmony_ci 47962306a36Sopenharmony_ci e->ec = ec; 48062306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 48162306a36Sopenharmony_ci if (e->ec > ubi->max_ec) 48262306a36Sopenharmony_ci ubi->max_ec = e->ec; 48362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 48462306a36Sopenharmony_ci 48562306a36Sopenharmony_ciout_free: 48662306a36Sopenharmony_ci kfree(ec_hdr); 48762306a36Sopenharmony_ci return err; 48862306a36Sopenharmony_ci} 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci/** 49162306a36Sopenharmony_ci * serve_prot_queue - check if it is time to stop protecting PEBs. 49262306a36Sopenharmony_ci * @ubi: UBI device description object 49362306a36Sopenharmony_ci * 49462306a36Sopenharmony_ci * This function is called after each erase operation and removes PEBs from the 49562306a36Sopenharmony_ci * tail of the protection queue. These PEBs have been protected for long enough 49662306a36Sopenharmony_ci * and should be moved to the used tree. 49762306a36Sopenharmony_ci */ 49862306a36Sopenharmony_cistatic void serve_prot_queue(struct ubi_device *ubi) 49962306a36Sopenharmony_ci{ 50062306a36Sopenharmony_ci struct ubi_wl_entry *e, *tmp; 50162306a36Sopenharmony_ci int count; 50262306a36Sopenharmony_ci 50362306a36Sopenharmony_ci /* 50462306a36Sopenharmony_ci * There may be several protected physical eraseblock to remove, 50562306a36Sopenharmony_ci * process them all. 50662306a36Sopenharmony_ci */ 50762306a36Sopenharmony_cirepeat: 50862306a36Sopenharmony_ci count = 0; 50962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 51062306a36Sopenharmony_ci list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) { 51162306a36Sopenharmony_ci dbg_wl("PEB %d EC %d protection over, move to used tree", 51262306a36Sopenharmony_ci e->pnum, e->ec); 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ci list_del(&e->u.list); 51562306a36Sopenharmony_ci wl_tree_add(e, &ubi->used); 51662306a36Sopenharmony_ci if (count++ > 32) { 51762306a36Sopenharmony_ci /* 51862306a36Sopenharmony_ci * Let's be nice and avoid holding the spinlock for 51962306a36Sopenharmony_ci * too long. 52062306a36Sopenharmony_ci */ 52162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 52262306a36Sopenharmony_ci cond_resched(); 52362306a36Sopenharmony_ci goto repeat; 52462306a36Sopenharmony_ci } 52562306a36Sopenharmony_ci } 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci ubi->pq_head += 1; 52862306a36Sopenharmony_ci if (ubi->pq_head == UBI_PROT_QUEUE_LEN) 52962306a36Sopenharmony_ci ubi->pq_head = 0; 53062306a36Sopenharmony_ci ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN); 53162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 53262306a36Sopenharmony_ci} 53362306a36Sopenharmony_ci 53462306a36Sopenharmony_ci/** 53562306a36Sopenharmony_ci * __schedule_ubi_work - schedule a work. 53662306a36Sopenharmony_ci * @ubi: UBI device description object 53762306a36Sopenharmony_ci * @wrk: the work to schedule 53862306a36Sopenharmony_ci * 53962306a36Sopenharmony_ci * This function adds a work defined by @wrk to the tail of the pending works 54062306a36Sopenharmony_ci * list. Can only be used if ubi->work_sem is already held in read mode! 54162306a36Sopenharmony_ci */ 54262306a36Sopenharmony_cistatic void __schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) 54362306a36Sopenharmony_ci{ 54462306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 54562306a36Sopenharmony_ci list_add_tail(&wrk->list, &ubi->works); 54662306a36Sopenharmony_ci ubi_assert(ubi->works_count >= 0); 54762306a36Sopenharmony_ci ubi->works_count += 1; 54862306a36Sopenharmony_ci if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi)) 54962306a36Sopenharmony_ci wake_up_process(ubi->bgt_thread); 55062306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 55162306a36Sopenharmony_ci} 55262306a36Sopenharmony_ci 55362306a36Sopenharmony_ci/** 55462306a36Sopenharmony_ci * schedule_ubi_work - schedule a work. 55562306a36Sopenharmony_ci * @ubi: UBI device description object 55662306a36Sopenharmony_ci * @wrk: the work to schedule 55762306a36Sopenharmony_ci * 55862306a36Sopenharmony_ci * This function adds a work defined by @wrk to the tail of the pending works 55962306a36Sopenharmony_ci * list. 56062306a36Sopenharmony_ci */ 56162306a36Sopenharmony_cistatic void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) 56262306a36Sopenharmony_ci{ 56362306a36Sopenharmony_ci down_read(&ubi->work_sem); 56462306a36Sopenharmony_ci __schedule_ubi_work(ubi, wrk); 56562306a36Sopenharmony_ci up_read(&ubi->work_sem); 56662306a36Sopenharmony_ci} 56762306a36Sopenharmony_ci 56862306a36Sopenharmony_cistatic int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, 56962306a36Sopenharmony_ci int shutdown); 57062306a36Sopenharmony_ci 57162306a36Sopenharmony_ci/** 57262306a36Sopenharmony_ci * schedule_erase - schedule an erase work. 57362306a36Sopenharmony_ci * @ubi: UBI device description object 57462306a36Sopenharmony_ci * @e: the WL entry of the physical eraseblock to erase 57562306a36Sopenharmony_ci * @vol_id: the volume ID that last used this PEB 57662306a36Sopenharmony_ci * @lnum: the last used logical eraseblock number for the PEB 57762306a36Sopenharmony_ci * @torture: if the physical eraseblock has to be tortured 57862306a36Sopenharmony_ci * @nested: denotes whether the work_sem is already held 57962306a36Sopenharmony_ci * 58062306a36Sopenharmony_ci * This function returns zero in case of success and a %-ENOMEM in case of 58162306a36Sopenharmony_ci * failure. 58262306a36Sopenharmony_ci */ 58362306a36Sopenharmony_cistatic int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, 58462306a36Sopenharmony_ci int vol_id, int lnum, int torture, bool nested) 58562306a36Sopenharmony_ci{ 58662306a36Sopenharmony_ci struct ubi_work *wl_wrk; 58762306a36Sopenharmony_ci 58862306a36Sopenharmony_ci ubi_assert(e); 58962306a36Sopenharmony_ci 59062306a36Sopenharmony_ci dbg_wl("schedule erasure of PEB %d, EC %d, torture %d", 59162306a36Sopenharmony_ci e->pnum, e->ec, torture); 59262306a36Sopenharmony_ci 59362306a36Sopenharmony_ci wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); 59462306a36Sopenharmony_ci if (!wl_wrk) 59562306a36Sopenharmony_ci return -ENOMEM; 59662306a36Sopenharmony_ci 59762306a36Sopenharmony_ci wl_wrk->func = &erase_worker; 59862306a36Sopenharmony_ci wl_wrk->e = e; 59962306a36Sopenharmony_ci wl_wrk->vol_id = vol_id; 60062306a36Sopenharmony_ci wl_wrk->lnum = lnum; 60162306a36Sopenharmony_ci wl_wrk->torture = torture; 60262306a36Sopenharmony_ci 60362306a36Sopenharmony_ci if (nested) 60462306a36Sopenharmony_ci __schedule_ubi_work(ubi, wl_wrk); 60562306a36Sopenharmony_ci else 60662306a36Sopenharmony_ci schedule_ubi_work(ubi, wl_wrk); 60762306a36Sopenharmony_ci return 0; 60862306a36Sopenharmony_ci} 60962306a36Sopenharmony_ci 61062306a36Sopenharmony_cistatic int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk); 61162306a36Sopenharmony_ci/** 61262306a36Sopenharmony_ci * do_sync_erase - run the erase worker synchronously. 61362306a36Sopenharmony_ci * @ubi: UBI device description object 61462306a36Sopenharmony_ci * @e: the WL entry of the physical eraseblock to erase 61562306a36Sopenharmony_ci * @vol_id: the volume ID that last used this PEB 61662306a36Sopenharmony_ci * @lnum: the last used logical eraseblock number for the PEB 61762306a36Sopenharmony_ci * @torture: if the physical eraseblock has to be tortured 61862306a36Sopenharmony_ci * 61962306a36Sopenharmony_ci */ 62062306a36Sopenharmony_cistatic int do_sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, 62162306a36Sopenharmony_ci int vol_id, int lnum, int torture) 62262306a36Sopenharmony_ci{ 62362306a36Sopenharmony_ci struct ubi_work wl_wrk; 62462306a36Sopenharmony_ci 62562306a36Sopenharmony_ci dbg_wl("sync erase of PEB %i", e->pnum); 62662306a36Sopenharmony_ci 62762306a36Sopenharmony_ci wl_wrk.e = e; 62862306a36Sopenharmony_ci wl_wrk.vol_id = vol_id; 62962306a36Sopenharmony_ci wl_wrk.lnum = lnum; 63062306a36Sopenharmony_ci wl_wrk.torture = torture; 63162306a36Sopenharmony_ci 63262306a36Sopenharmony_ci return __erase_worker(ubi, &wl_wrk); 63362306a36Sopenharmony_ci} 63462306a36Sopenharmony_ci 63562306a36Sopenharmony_cistatic int ensure_wear_leveling(struct ubi_device *ubi, int nested); 63662306a36Sopenharmony_ci/** 63762306a36Sopenharmony_ci * wear_leveling_worker - wear-leveling worker function. 63862306a36Sopenharmony_ci * @ubi: UBI device description object 63962306a36Sopenharmony_ci * @wrk: the work object 64062306a36Sopenharmony_ci * @shutdown: non-zero if the worker has to free memory and exit 64162306a36Sopenharmony_ci * because the WL-subsystem is shutting down 64262306a36Sopenharmony_ci * 64362306a36Sopenharmony_ci * This function copies a more worn out physical eraseblock to a less worn out 64462306a36Sopenharmony_ci * one. Returns zero in case of success and a negative error code in case of 64562306a36Sopenharmony_ci * failure. 64662306a36Sopenharmony_ci */ 64762306a36Sopenharmony_cistatic int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, 64862306a36Sopenharmony_ci int shutdown) 64962306a36Sopenharmony_ci{ 65062306a36Sopenharmony_ci int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0; 65162306a36Sopenharmony_ci int erase = 0, keep = 0, vol_id = -1, lnum = -1; 65262306a36Sopenharmony_ci struct ubi_wl_entry *e1, *e2; 65362306a36Sopenharmony_ci struct ubi_vid_io_buf *vidb; 65462306a36Sopenharmony_ci struct ubi_vid_hdr *vid_hdr; 65562306a36Sopenharmony_ci int dst_leb_clean = 0; 65662306a36Sopenharmony_ci 65762306a36Sopenharmony_ci kfree(wrk); 65862306a36Sopenharmony_ci if (shutdown) 65962306a36Sopenharmony_ci return 0; 66062306a36Sopenharmony_ci 66162306a36Sopenharmony_ci vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 66262306a36Sopenharmony_ci if (!vidb) 66362306a36Sopenharmony_ci return -ENOMEM; 66462306a36Sopenharmony_ci 66562306a36Sopenharmony_ci vid_hdr = ubi_get_vid_hdr(vidb); 66662306a36Sopenharmony_ci 66762306a36Sopenharmony_ci down_read(&ubi->fm_eba_sem); 66862306a36Sopenharmony_ci mutex_lock(&ubi->move_mutex); 66962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 67062306a36Sopenharmony_ci ubi_assert(!ubi->move_from && !ubi->move_to); 67162306a36Sopenharmony_ci ubi_assert(!ubi->move_to_put); 67262306a36Sopenharmony_ci 67362306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP 67462306a36Sopenharmony_ci if (!next_peb_for_wl(ubi) || 67562306a36Sopenharmony_ci#else 67662306a36Sopenharmony_ci if (!ubi->free.rb_node || 67762306a36Sopenharmony_ci#endif 67862306a36Sopenharmony_ci (!ubi->used.rb_node && !ubi->scrub.rb_node)) { 67962306a36Sopenharmony_ci /* 68062306a36Sopenharmony_ci * No free physical eraseblocks? Well, they must be waiting in 68162306a36Sopenharmony_ci * the queue to be erased. Cancel movement - it will be 68262306a36Sopenharmony_ci * triggered again when a free physical eraseblock appears. 68362306a36Sopenharmony_ci * 68462306a36Sopenharmony_ci * No used physical eraseblocks? They must be temporarily 68562306a36Sopenharmony_ci * protected from being moved. They will be moved to the 68662306a36Sopenharmony_ci * @ubi->used tree later and the wear-leveling will be 68762306a36Sopenharmony_ci * triggered again. 68862306a36Sopenharmony_ci */ 68962306a36Sopenharmony_ci dbg_wl("cancel WL, a list is empty: free %d, used %d", 69062306a36Sopenharmony_ci !ubi->free.rb_node, !ubi->used.rb_node); 69162306a36Sopenharmony_ci goto out_cancel; 69262306a36Sopenharmony_ci } 69362306a36Sopenharmony_ci 69462306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP 69562306a36Sopenharmony_ci e1 = find_anchor_wl_entry(&ubi->used); 69662306a36Sopenharmony_ci if (e1 && ubi->fm_anchor && 69762306a36Sopenharmony_ci (ubi->fm_anchor->ec - e1->ec >= UBI_WL_THRESHOLD)) { 69862306a36Sopenharmony_ci ubi->fm_do_produce_anchor = 1; 69962306a36Sopenharmony_ci /* 70062306a36Sopenharmony_ci * fm_anchor is no longer considered a good anchor. 70162306a36Sopenharmony_ci * NULL assignment also prevents multiple wear level checks 70262306a36Sopenharmony_ci * of this PEB. 70362306a36Sopenharmony_ci */ 70462306a36Sopenharmony_ci wl_tree_add(ubi->fm_anchor, &ubi->free); 70562306a36Sopenharmony_ci ubi->fm_anchor = NULL; 70662306a36Sopenharmony_ci ubi->free_count++; 70762306a36Sopenharmony_ci } 70862306a36Sopenharmony_ci 70962306a36Sopenharmony_ci if (ubi->fm_do_produce_anchor) { 71062306a36Sopenharmony_ci if (!e1) 71162306a36Sopenharmony_ci goto out_cancel; 71262306a36Sopenharmony_ci e2 = get_peb_for_wl(ubi); 71362306a36Sopenharmony_ci if (!e2) 71462306a36Sopenharmony_ci goto out_cancel; 71562306a36Sopenharmony_ci 71662306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e1, &ubi->used); 71762306a36Sopenharmony_ci rb_erase(&e1->u.rb, &ubi->used); 71862306a36Sopenharmony_ci dbg_wl("anchor-move PEB %d to PEB %d", e1->pnum, e2->pnum); 71962306a36Sopenharmony_ci ubi->fm_do_produce_anchor = 0; 72062306a36Sopenharmony_ci } else if (!ubi->scrub.rb_node) { 72162306a36Sopenharmony_ci#else 72262306a36Sopenharmony_ci if (!ubi->scrub.rb_node) { 72362306a36Sopenharmony_ci#endif 72462306a36Sopenharmony_ci /* 72562306a36Sopenharmony_ci * Now pick the least worn-out used physical eraseblock and a 72662306a36Sopenharmony_ci * highly worn-out free physical eraseblock. If the erase 72762306a36Sopenharmony_ci * counters differ much enough, start wear-leveling. 72862306a36Sopenharmony_ci */ 72962306a36Sopenharmony_ci e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); 73062306a36Sopenharmony_ci e2 = get_peb_for_wl(ubi); 73162306a36Sopenharmony_ci if (!e2) 73262306a36Sopenharmony_ci goto out_cancel; 73362306a36Sopenharmony_ci 73462306a36Sopenharmony_ci if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) { 73562306a36Sopenharmony_ci dbg_wl("no WL needed: min used EC %d, max free EC %d", 73662306a36Sopenharmony_ci e1->ec, e2->ec); 73762306a36Sopenharmony_ci 73862306a36Sopenharmony_ci /* Give the unused PEB back */ 73962306a36Sopenharmony_ci wl_tree_add(e2, &ubi->free); 74062306a36Sopenharmony_ci ubi->free_count++; 74162306a36Sopenharmony_ci goto out_cancel; 74262306a36Sopenharmony_ci } 74362306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e1, &ubi->used); 74462306a36Sopenharmony_ci rb_erase(&e1->u.rb, &ubi->used); 74562306a36Sopenharmony_ci dbg_wl("move PEB %d EC %d to PEB %d EC %d", 74662306a36Sopenharmony_ci e1->pnum, e1->ec, e2->pnum, e2->ec); 74762306a36Sopenharmony_ci } else { 74862306a36Sopenharmony_ci /* Perform scrubbing */ 74962306a36Sopenharmony_ci scrubbing = 1; 75062306a36Sopenharmony_ci e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb); 75162306a36Sopenharmony_ci e2 = get_peb_for_wl(ubi); 75262306a36Sopenharmony_ci if (!e2) 75362306a36Sopenharmony_ci goto out_cancel; 75462306a36Sopenharmony_ci 75562306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e1, &ubi->scrub); 75662306a36Sopenharmony_ci rb_erase(&e1->u.rb, &ubi->scrub); 75762306a36Sopenharmony_ci dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum); 75862306a36Sopenharmony_ci } 75962306a36Sopenharmony_ci 76062306a36Sopenharmony_ci ubi->move_from = e1; 76162306a36Sopenharmony_ci ubi->move_to = e2; 76262306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 76362306a36Sopenharmony_ci 76462306a36Sopenharmony_ci /* 76562306a36Sopenharmony_ci * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum. 76662306a36Sopenharmony_ci * We so far do not know which logical eraseblock our physical 76762306a36Sopenharmony_ci * eraseblock (@e1) belongs to. We have to read the volume identifier 76862306a36Sopenharmony_ci * header first. 76962306a36Sopenharmony_ci * 77062306a36Sopenharmony_ci * Note, we are protected from this PEB being unmapped and erased. The 77162306a36Sopenharmony_ci * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB 77262306a36Sopenharmony_ci * which is being moved was unmapped. 77362306a36Sopenharmony_ci */ 77462306a36Sopenharmony_ci 77562306a36Sopenharmony_ci err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0); 77662306a36Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 77762306a36Sopenharmony_ci dst_leb_clean = 1; 77862306a36Sopenharmony_ci if (err == UBI_IO_FF) { 77962306a36Sopenharmony_ci /* 78062306a36Sopenharmony_ci * We are trying to move PEB without a VID header. UBI 78162306a36Sopenharmony_ci * always write VID headers shortly after the PEB was 78262306a36Sopenharmony_ci * given, so we have a situation when it has not yet 78362306a36Sopenharmony_ci * had a chance to write it, because it was preempted. 78462306a36Sopenharmony_ci * So add this PEB to the protection queue so far, 78562306a36Sopenharmony_ci * because presumably more data will be written there 78662306a36Sopenharmony_ci * (including the missing VID header), and then we'll 78762306a36Sopenharmony_ci * move it. 78862306a36Sopenharmony_ci */ 78962306a36Sopenharmony_ci dbg_wl("PEB %d has no VID header", e1->pnum); 79062306a36Sopenharmony_ci protect = 1; 79162306a36Sopenharmony_ci goto out_not_moved; 79262306a36Sopenharmony_ci } else if (err == UBI_IO_FF_BITFLIPS) { 79362306a36Sopenharmony_ci /* 79462306a36Sopenharmony_ci * The same situation as %UBI_IO_FF, but bit-flips were 79562306a36Sopenharmony_ci * detected. It is better to schedule this PEB for 79662306a36Sopenharmony_ci * scrubbing. 79762306a36Sopenharmony_ci */ 79862306a36Sopenharmony_ci dbg_wl("PEB %d has no VID header but has bit-flips", 79962306a36Sopenharmony_ci e1->pnum); 80062306a36Sopenharmony_ci scrubbing = 1; 80162306a36Sopenharmony_ci goto out_not_moved; 80262306a36Sopenharmony_ci } else if (ubi->fast_attach && err == UBI_IO_BAD_HDR_EBADMSG) { 80362306a36Sopenharmony_ci /* 80462306a36Sopenharmony_ci * While a full scan would detect interrupted erasures 80562306a36Sopenharmony_ci * at attach time we can face them here when attached from 80662306a36Sopenharmony_ci * Fastmap. 80762306a36Sopenharmony_ci */ 80862306a36Sopenharmony_ci dbg_wl("PEB %d has ECC errors, maybe from an interrupted erasure", 80962306a36Sopenharmony_ci e1->pnum); 81062306a36Sopenharmony_ci erase = 1; 81162306a36Sopenharmony_ci goto out_not_moved; 81262306a36Sopenharmony_ci } 81362306a36Sopenharmony_ci 81462306a36Sopenharmony_ci ubi_err(ubi, "error %d while reading VID header from PEB %d", 81562306a36Sopenharmony_ci err, e1->pnum); 81662306a36Sopenharmony_ci goto out_error; 81762306a36Sopenharmony_ci } 81862306a36Sopenharmony_ci 81962306a36Sopenharmony_ci vol_id = be32_to_cpu(vid_hdr->vol_id); 82062306a36Sopenharmony_ci lnum = be32_to_cpu(vid_hdr->lnum); 82162306a36Sopenharmony_ci 82262306a36Sopenharmony_ci err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb); 82362306a36Sopenharmony_ci if (err) { 82462306a36Sopenharmony_ci if (err == MOVE_CANCEL_RACE) { 82562306a36Sopenharmony_ci /* 82662306a36Sopenharmony_ci * The LEB has not been moved because the volume is 82762306a36Sopenharmony_ci * being deleted or the PEB has been put meanwhile. We 82862306a36Sopenharmony_ci * should prevent this PEB from being selected for 82962306a36Sopenharmony_ci * wear-leveling movement again, so put it to the 83062306a36Sopenharmony_ci * protection queue. 83162306a36Sopenharmony_ci */ 83262306a36Sopenharmony_ci protect = 1; 83362306a36Sopenharmony_ci dst_leb_clean = 1; 83462306a36Sopenharmony_ci goto out_not_moved; 83562306a36Sopenharmony_ci } 83662306a36Sopenharmony_ci if (err == MOVE_RETRY) { 83762306a36Sopenharmony_ci scrubbing = 1; 83862306a36Sopenharmony_ci dst_leb_clean = 1; 83962306a36Sopenharmony_ci goto out_not_moved; 84062306a36Sopenharmony_ci } 84162306a36Sopenharmony_ci if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR || 84262306a36Sopenharmony_ci err == MOVE_TARGET_RD_ERR) { 84362306a36Sopenharmony_ci /* 84462306a36Sopenharmony_ci * Target PEB had bit-flips or write error - torture it. 84562306a36Sopenharmony_ci */ 84662306a36Sopenharmony_ci torture = 1; 84762306a36Sopenharmony_ci keep = 1; 84862306a36Sopenharmony_ci goto out_not_moved; 84962306a36Sopenharmony_ci } 85062306a36Sopenharmony_ci 85162306a36Sopenharmony_ci if (err == MOVE_SOURCE_RD_ERR) { 85262306a36Sopenharmony_ci /* 85362306a36Sopenharmony_ci * An error happened while reading the source PEB. Do 85462306a36Sopenharmony_ci * not switch to R/O mode in this case, and give the 85562306a36Sopenharmony_ci * upper layers a possibility to recover from this, 85662306a36Sopenharmony_ci * e.g. by unmapping corresponding LEB. Instead, just 85762306a36Sopenharmony_ci * put this PEB to the @ubi->erroneous list to prevent 85862306a36Sopenharmony_ci * UBI from trying to move it over and over again. 85962306a36Sopenharmony_ci */ 86062306a36Sopenharmony_ci if (ubi->erroneous_peb_count > ubi->max_erroneous) { 86162306a36Sopenharmony_ci ubi_err(ubi, "too many erroneous eraseblocks (%d)", 86262306a36Sopenharmony_ci ubi->erroneous_peb_count); 86362306a36Sopenharmony_ci goto out_error; 86462306a36Sopenharmony_ci } 86562306a36Sopenharmony_ci dst_leb_clean = 1; 86662306a36Sopenharmony_ci erroneous = 1; 86762306a36Sopenharmony_ci goto out_not_moved; 86862306a36Sopenharmony_ci } 86962306a36Sopenharmony_ci 87062306a36Sopenharmony_ci if (err < 0) 87162306a36Sopenharmony_ci goto out_error; 87262306a36Sopenharmony_ci 87362306a36Sopenharmony_ci ubi_assert(0); 87462306a36Sopenharmony_ci } 87562306a36Sopenharmony_ci 87662306a36Sopenharmony_ci /* The PEB has been successfully moved */ 87762306a36Sopenharmony_ci if (scrubbing) 87862306a36Sopenharmony_ci ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d", 87962306a36Sopenharmony_ci e1->pnum, vol_id, lnum, e2->pnum); 88062306a36Sopenharmony_ci ubi_free_vid_buf(vidb); 88162306a36Sopenharmony_ci 88262306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 88362306a36Sopenharmony_ci if (!ubi->move_to_put) { 88462306a36Sopenharmony_ci wl_tree_add(e2, &ubi->used); 88562306a36Sopenharmony_ci e2 = NULL; 88662306a36Sopenharmony_ci } 88762306a36Sopenharmony_ci ubi->move_from = ubi->move_to = NULL; 88862306a36Sopenharmony_ci ubi->move_to_put = ubi->wl_scheduled = 0; 88962306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 89062306a36Sopenharmony_ci 89162306a36Sopenharmony_ci err = do_sync_erase(ubi, e1, vol_id, lnum, 0); 89262306a36Sopenharmony_ci if (err) { 89362306a36Sopenharmony_ci if (e2) { 89462306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 89562306a36Sopenharmony_ci wl_entry_destroy(ubi, e2); 89662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 89762306a36Sopenharmony_ci } 89862306a36Sopenharmony_ci goto out_ro; 89962306a36Sopenharmony_ci } 90062306a36Sopenharmony_ci 90162306a36Sopenharmony_ci if (e2) { 90262306a36Sopenharmony_ci /* 90362306a36Sopenharmony_ci * Well, the target PEB was put meanwhile, schedule it for 90462306a36Sopenharmony_ci * erasure. 90562306a36Sopenharmony_ci */ 90662306a36Sopenharmony_ci dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase", 90762306a36Sopenharmony_ci e2->pnum, vol_id, lnum); 90862306a36Sopenharmony_ci err = do_sync_erase(ubi, e2, vol_id, lnum, 0); 90962306a36Sopenharmony_ci if (err) 91062306a36Sopenharmony_ci goto out_ro; 91162306a36Sopenharmony_ci } 91262306a36Sopenharmony_ci 91362306a36Sopenharmony_ci dbg_wl("done"); 91462306a36Sopenharmony_ci mutex_unlock(&ubi->move_mutex); 91562306a36Sopenharmony_ci up_read(&ubi->fm_eba_sem); 91662306a36Sopenharmony_ci return 0; 91762306a36Sopenharmony_ci 91862306a36Sopenharmony_ci /* 91962306a36Sopenharmony_ci * For some reasons the LEB was not moved, might be an error, might be 92062306a36Sopenharmony_ci * something else. @e1 was not changed, so return it back. @e2 might 92162306a36Sopenharmony_ci * have been changed, schedule it for erasure. 92262306a36Sopenharmony_ci */ 92362306a36Sopenharmony_ciout_not_moved: 92462306a36Sopenharmony_ci if (vol_id != -1) 92562306a36Sopenharmony_ci dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)", 92662306a36Sopenharmony_ci e1->pnum, vol_id, lnum, e2->pnum, err); 92762306a36Sopenharmony_ci else 92862306a36Sopenharmony_ci dbg_wl("cancel moving PEB %d to PEB %d (%d)", 92962306a36Sopenharmony_ci e1->pnum, e2->pnum, err); 93062306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 93162306a36Sopenharmony_ci if (protect) 93262306a36Sopenharmony_ci prot_queue_add(ubi, e1); 93362306a36Sopenharmony_ci else if (erroneous) { 93462306a36Sopenharmony_ci wl_tree_add(e1, &ubi->erroneous); 93562306a36Sopenharmony_ci ubi->erroneous_peb_count += 1; 93662306a36Sopenharmony_ci } else if (scrubbing) 93762306a36Sopenharmony_ci wl_tree_add(e1, &ubi->scrub); 93862306a36Sopenharmony_ci else if (keep) 93962306a36Sopenharmony_ci wl_tree_add(e1, &ubi->used); 94062306a36Sopenharmony_ci if (dst_leb_clean) { 94162306a36Sopenharmony_ci wl_tree_add(e2, &ubi->free); 94262306a36Sopenharmony_ci ubi->free_count++; 94362306a36Sopenharmony_ci } 94462306a36Sopenharmony_ci 94562306a36Sopenharmony_ci ubi_assert(!ubi->move_to_put); 94662306a36Sopenharmony_ci ubi->move_from = ubi->move_to = NULL; 94762306a36Sopenharmony_ci ubi->wl_scheduled = 0; 94862306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 94962306a36Sopenharmony_ci 95062306a36Sopenharmony_ci ubi_free_vid_buf(vidb); 95162306a36Sopenharmony_ci if (dst_leb_clean) { 95262306a36Sopenharmony_ci ensure_wear_leveling(ubi, 1); 95362306a36Sopenharmony_ci } else { 95462306a36Sopenharmony_ci err = do_sync_erase(ubi, e2, vol_id, lnum, torture); 95562306a36Sopenharmony_ci if (err) 95662306a36Sopenharmony_ci goto out_ro; 95762306a36Sopenharmony_ci } 95862306a36Sopenharmony_ci 95962306a36Sopenharmony_ci if (erase) { 96062306a36Sopenharmony_ci err = do_sync_erase(ubi, e1, vol_id, lnum, 1); 96162306a36Sopenharmony_ci if (err) 96262306a36Sopenharmony_ci goto out_ro; 96362306a36Sopenharmony_ci } 96462306a36Sopenharmony_ci 96562306a36Sopenharmony_ci mutex_unlock(&ubi->move_mutex); 96662306a36Sopenharmony_ci up_read(&ubi->fm_eba_sem); 96762306a36Sopenharmony_ci return 0; 96862306a36Sopenharmony_ci 96962306a36Sopenharmony_ciout_error: 97062306a36Sopenharmony_ci if (vol_id != -1) 97162306a36Sopenharmony_ci ubi_err(ubi, "error %d while moving PEB %d to PEB %d", 97262306a36Sopenharmony_ci err, e1->pnum, e2->pnum); 97362306a36Sopenharmony_ci else 97462306a36Sopenharmony_ci ubi_err(ubi, "error %d while moving PEB %d (LEB %d:%d) to PEB %d", 97562306a36Sopenharmony_ci err, e1->pnum, vol_id, lnum, e2->pnum); 97662306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 97762306a36Sopenharmony_ci ubi->move_from = ubi->move_to = NULL; 97862306a36Sopenharmony_ci ubi->move_to_put = ubi->wl_scheduled = 0; 97962306a36Sopenharmony_ci wl_entry_destroy(ubi, e1); 98062306a36Sopenharmony_ci wl_entry_destroy(ubi, e2); 98162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 98262306a36Sopenharmony_ci 98362306a36Sopenharmony_ci ubi_free_vid_buf(vidb); 98462306a36Sopenharmony_ci 98562306a36Sopenharmony_ciout_ro: 98662306a36Sopenharmony_ci ubi_ro_mode(ubi); 98762306a36Sopenharmony_ci mutex_unlock(&ubi->move_mutex); 98862306a36Sopenharmony_ci up_read(&ubi->fm_eba_sem); 98962306a36Sopenharmony_ci ubi_assert(err != 0); 99062306a36Sopenharmony_ci return err < 0 ? err : -EIO; 99162306a36Sopenharmony_ci 99262306a36Sopenharmony_ciout_cancel: 99362306a36Sopenharmony_ci ubi->wl_scheduled = 0; 99462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 99562306a36Sopenharmony_ci mutex_unlock(&ubi->move_mutex); 99662306a36Sopenharmony_ci up_read(&ubi->fm_eba_sem); 99762306a36Sopenharmony_ci ubi_free_vid_buf(vidb); 99862306a36Sopenharmony_ci return 0; 99962306a36Sopenharmony_ci} 100062306a36Sopenharmony_ci 100162306a36Sopenharmony_ci/** 100262306a36Sopenharmony_ci * ensure_wear_leveling - schedule wear-leveling if it is needed. 100362306a36Sopenharmony_ci * @ubi: UBI device description object 100462306a36Sopenharmony_ci * @nested: set to non-zero if this function is called from UBI worker 100562306a36Sopenharmony_ci * 100662306a36Sopenharmony_ci * This function checks if it is time to start wear-leveling and schedules it 100762306a36Sopenharmony_ci * if yes. This function returns zero in case of success and a negative error 100862306a36Sopenharmony_ci * code in case of failure. 100962306a36Sopenharmony_ci */ 101062306a36Sopenharmony_cistatic int ensure_wear_leveling(struct ubi_device *ubi, int nested) 101162306a36Sopenharmony_ci{ 101262306a36Sopenharmony_ci int err = 0; 101362306a36Sopenharmony_ci struct ubi_work *wrk; 101462306a36Sopenharmony_ci 101562306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 101662306a36Sopenharmony_ci if (ubi->wl_scheduled) 101762306a36Sopenharmony_ci /* Wear-leveling is already in the work queue */ 101862306a36Sopenharmony_ci goto out_unlock; 101962306a36Sopenharmony_ci 102062306a36Sopenharmony_ci /* 102162306a36Sopenharmony_ci * If the ubi->scrub tree is not empty, scrubbing is needed, and the 102262306a36Sopenharmony_ci * WL worker has to be scheduled anyway. 102362306a36Sopenharmony_ci */ 102462306a36Sopenharmony_ci if (!ubi->scrub.rb_node) { 102562306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP 102662306a36Sopenharmony_ci if (!need_wear_leveling(ubi)) 102762306a36Sopenharmony_ci goto out_unlock; 102862306a36Sopenharmony_ci#else 102962306a36Sopenharmony_ci struct ubi_wl_entry *e1; 103062306a36Sopenharmony_ci struct ubi_wl_entry *e2; 103162306a36Sopenharmony_ci 103262306a36Sopenharmony_ci if (!ubi->used.rb_node || !ubi->free.rb_node) 103362306a36Sopenharmony_ci /* No physical eraseblocks - no deal */ 103462306a36Sopenharmony_ci goto out_unlock; 103562306a36Sopenharmony_ci 103662306a36Sopenharmony_ci /* 103762306a36Sopenharmony_ci * We schedule wear-leveling only if the difference between the 103862306a36Sopenharmony_ci * lowest erase counter of used physical eraseblocks and a high 103962306a36Sopenharmony_ci * erase counter of free physical eraseblocks is greater than 104062306a36Sopenharmony_ci * %UBI_WL_THRESHOLD. 104162306a36Sopenharmony_ci */ 104262306a36Sopenharmony_ci e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); 104362306a36Sopenharmony_ci e2 = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); 104462306a36Sopenharmony_ci 104562306a36Sopenharmony_ci if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) 104662306a36Sopenharmony_ci goto out_unlock; 104762306a36Sopenharmony_ci#endif 104862306a36Sopenharmony_ci dbg_wl("schedule wear-leveling"); 104962306a36Sopenharmony_ci } else 105062306a36Sopenharmony_ci dbg_wl("schedule scrubbing"); 105162306a36Sopenharmony_ci 105262306a36Sopenharmony_ci ubi->wl_scheduled = 1; 105362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 105462306a36Sopenharmony_ci 105562306a36Sopenharmony_ci wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); 105662306a36Sopenharmony_ci if (!wrk) { 105762306a36Sopenharmony_ci err = -ENOMEM; 105862306a36Sopenharmony_ci goto out_cancel; 105962306a36Sopenharmony_ci } 106062306a36Sopenharmony_ci 106162306a36Sopenharmony_ci wrk->func = &wear_leveling_worker; 106262306a36Sopenharmony_ci if (nested) 106362306a36Sopenharmony_ci __schedule_ubi_work(ubi, wrk); 106462306a36Sopenharmony_ci else 106562306a36Sopenharmony_ci schedule_ubi_work(ubi, wrk); 106662306a36Sopenharmony_ci return err; 106762306a36Sopenharmony_ci 106862306a36Sopenharmony_ciout_cancel: 106962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 107062306a36Sopenharmony_ci ubi->wl_scheduled = 0; 107162306a36Sopenharmony_ciout_unlock: 107262306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 107362306a36Sopenharmony_ci return err; 107462306a36Sopenharmony_ci} 107562306a36Sopenharmony_ci 107662306a36Sopenharmony_ci/** 107762306a36Sopenharmony_ci * __erase_worker - physical eraseblock erase worker function. 107862306a36Sopenharmony_ci * @ubi: UBI device description object 107962306a36Sopenharmony_ci * @wl_wrk: the work object 108062306a36Sopenharmony_ci * 108162306a36Sopenharmony_ci * This function erases a physical eraseblock and perform torture testing if 108262306a36Sopenharmony_ci * needed. It also takes care about marking the physical eraseblock bad if 108362306a36Sopenharmony_ci * needed. Returns zero in case of success and a negative error code in case of 108462306a36Sopenharmony_ci * failure. 108562306a36Sopenharmony_ci */ 108662306a36Sopenharmony_cistatic int __erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk) 108762306a36Sopenharmony_ci{ 108862306a36Sopenharmony_ci struct ubi_wl_entry *e = wl_wrk->e; 108962306a36Sopenharmony_ci int pnum = e->pnum; 109062306a36Sopenharmony_ci int vol_id = wl_wrk->vol_id; 109162306a36Sopenharmony_ci int lnum = wl_wrk->lnum; 109262306a36Sopenharmony_ci int err, available_consumed = 0; 109362306a36Sopenharmony_ci 109462306a36Sopenharmony_ci dbg_wl("erase PEB %d EC %d LEB %d:%d", 109562306a36Sopenharmony_ci pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum); 109662306a36Sopenharmony_ci 109762306a36Sopenharmony_ci err = sync_erase(ubi, e, wl_wrk->torture); 109862306a36Sopenharmony_ci if (!err) { 109962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 110062306a36Sopenharmony_ci 110162306a36Sopenharmony_ci if (!ubi->fm_disabled && !ubi->fm_anchor && 110262306a36Sopenharmony_ci e->pnum < UBI_FM_MAX_START) { 110362306a36Sopenharmony_ci /* 110462306a36Sopenharmony_ci * Abort anchor production, if needed it will be 110562306a36Sopenharmony_ci * enabled again in the wear leveling started below. 110662306a36Sopenharmony_ci */ 110762306a36Sopenharmony_ci ubi->fm_anchor = e; 110862306a36Sopenharmony_ci ubi->fm_do_produce_anchor = 0; 110962306a36Sopenharmony_ci } else { 111062306a36Sopenharmony_ci wl_tree_add(e, &ubi->free); 111162306a36Sopenharmony_ci ubi->free_count++; 111262306a36Sopenharmony_ci } 111362306a36Sopenharmony_ci 111462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 111562306a36Sopenharmony_ci 111662306a36Sopenharmony_ci /* 111762306a36Sopenharmony_ci * One more erase operation has happened, take care about 111862306a36Sopenharmony_ci * protected physical eraseblocks. 111962306a36Sopenharmony_ci */ 112062306a36Sopenharmony_ci serve_prot_queue(ubi); 112162306a36Sopenharmony_ci 112262306a36Sopenharmony_ci /* And take care about wear-leveling */ 112362306a36Sopenharmony_ci err = ensure_wear_leveling(ubi, 1); 112462306a36Sopenharmony_ci return err; 112562306a36Sopenharmony_ci } 112662306a36Sopenharmony_ci 112762306a36Sopenharmony_ci ubi_err(ubi, "failed to erase PEB %d, error %d", pnum, err); 112862306a36Sopenharmony_ci 112962306a36Sopenharmony_ci if (err == -EINTR || err == -ENOMEM || err == -EAGAIN || 113062306a36Sopenharmony_ci err == -EBUSY) { 113162306a36Sopenharmony_ci int err1; 113262306a36Sopenharmony_ci 113362306a36Sopenharmony_ci /* Re-schedule the LEB for erasure */ 113462306a36Sopenharmony_ci err1 = schedule_erase(ubi, e, vol_id, lnum, 0, true); 113562306a36Sopenharmony_ci if (err1) { 113662306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 113762306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 113862306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 113962306a36Sopenharmony_ci err = err1; 114062306a36Sopenharmony_ci goto out_ro; 114162306a36Sopenharmony_ci } 114262306a36Sopenharmony_ci return err; 114362306a36Sopenharmony_ci } 114462306a36Sopenharmony_ci 114562306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 114662306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 114762306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 114862306a36Sopenharmony_ci if (err != -EIO) 114962306a36Sopenharmony_ci /* 115062306a36Sopenharmony_ci * If this is not %-EIO, we have no idea what to do. Scheduling 115162306a36Sopenharmony_ci * this physical eraseblock for erasure again would cause 115262306a36Sopenharmony_ci * errors again and again. Well, lets switch to R/O mode. 115362306a36Sopenharmony_ci */ 115462306a36Sopenharmony_ci goto out_ro; 115562306a36Sopenharmony_ci 115662306a36Sopenharmony_ci /* It is %-EIO, the PEB went bad */ 115762306a36Sopenharmony_ci 115862306a36Sopenharmony_ci if (!ubi->bad_allowed) { 115962306a36Sopenharmony_ci ubi_err(ubi, "bad physical eraseblock %d detected", pnum); 116062306a36Sopenharmony_ci goto out_ro; 116162306a36Sopenharmony_ci } 116262306a36Sopenharmony_ci 116362306a36Sopenharmony_ci spin_lock(&ubi->volumes_lock); 116462306a36Sopenharmony_ci if (ubi->beb_rsvd_pebs == 0) { 116562306a36Sopenharmony_ci if (ubi->avail_pebs == 0) { 116662306a36Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 116762306a36Sopenharmony_ci ubi_err(ubi, "no reserved/available physical eraseblocks"); 116862306a36Sopenharmony_ci goto out_ro; 116962306a36Sopenharmony_ci } 117062306a36Sopenharmony_ci ubi->avail_pebs -= 1; 117162306a36Sopenharmony_ci available_consumed = 1; 117262306a36Sopenharmony_ci } 117362306a36Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 117462306a36Sopenharmony_ci 117562306a36Sopenharmony_ci ubi_msg(ubi, "mark PEB %d as bad", pnum); 117662306a36Sopenharmony_ci err = ubi_io_mark_bad(ubi, pnum); 117762306a36Sopenharmony_ci if (err) 117862306a36Sopenharmony_ci goto out_ro; 117962306a36Sopenharmony_ci 118062306a36Sopenharmony_ci spin_lock(&ubi->volumes_lock); 118162306a36Sopenharmony_ci if (ubi->beb_rsvd_pebs > 0) { 118262306a36Sopenharmony_ci if (available_consumed) { 118362306a36Sopenharmony_ci /* 118462306a36Sopenharmony_ci * The amount of reserved PEBs increased since we last 118562306a36Sopenharmony_ci * checked. 118662306a36Sopenharmony_ci */ 118762306a36Sopenharmony_ci ubi->avail_pebs += 1; 118862306a36Sopenharmony_ci available_consumed = 0; 118962306a36Sopenharmony_ci } 119062306a36Sopenharmony_ci ubi->beb_rsvd_pebs -= 1; 119162306a36Sopenharmony_ci } 119262306a36Sopenharmony_ci ubi->bad_peb_count += 1; 119362306a36Sopenharmony_ci ubi->good_peb_count -= 1; 119462306a36Sopenharmony_ci ubi_calculate_reserved(ubi); 119562306a36Sopenharmony_ci if (available_consumed) 119662306a36Sopenharmony_ci ubi_warn(ubi, "no PEBs in the reserved pool, used an available PEB"); 119762306a36Sopenharmony_ci else if (ubi->beb_rsvd_pebs) 119862306a36Sopenharmony_ci ubi_msg(ubi, "%d PEBs left in the reserve", 119962306a36Sopenharmony_ci ubi->beb_rsvd_pebs); 120062306a36Sopenharmony_ci else 120162306a36Sopenharmony_ci ubi_warn(ubi, "last PEB from the reserve was used"); 120262306a36Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 120362306a36Sopenharmony_ci 120462306a36Sopenharmony_ci return err; 120562306a36Sopenharmony_ci 120662306a36Sopenharmony_ciout_ro: 120762306a36Sopenharmony_ci if (available_consumed) { 120862306a36Sopenharmony_ci spin_lock(&ubi->volumes_lock); 120962306a36Sopenharmony_ci ubi->avail_pebs += 1; 121062306a36Sopenharmony_ci spin_unlock(&ubi->volumes_lock); 121162306a36Sopenharmony_ci } 121262306a36Sopenharmony_ci ubi_ro_mode(ubi); 121362306a36Sopenharmony_ci return err; 121462306a36Sopenharmony_ci} 121562306a36Sopenharmony_ci 121662306a36Sopenharmony_cistatic int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, 121762306a36Sopenharmony_ci int shutdown) 121862306a36Sopenharmony_ci{ 121962306a36Sopenharmony_ci int ret; 122062306a36Sopenharmony_ci 122162306a36Sopenharmony_ci if (shutdown) { 122262306a36Sopenharmony_ci struct ubi_wl_entry *e = wl_wrk->e; 122362306a36Sopenharmony_ci 122462306a36Sopenharmony_ci dbg_wl("cancel erasure of PEB %d EC %d", e->pnum, e->ec); 122562306a36Sopenharmony_ci kfree(wl_wrk); 122662306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 122762306a36Sopenharmony_ci return 0; 122862306a36Sopenharmony_ci } 122962306a36Sopenharmony_ci 123062306a36Sopenharmony_ci ret = __erase_worker(ubi, wl_wrk); 123162306a36Sopenharmony_ci kfree(wl_wrk); 123262306a36Sopenharmony_ci return ret; 123362306a36Sopenharmony_ci} 123462306a36Sopenharmony_ci 123562306a36Sopenharmony_ci/** 123662306a36Sopenharmony_ci * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system. 123762306a36Sopenharmony_ci * @ubi: UBI device description object 123862306a36Sopenharmony_ci * @vol_id: the volume ID that last used this PEB 123962306a36Sopenharmony_ci * @lnum: the last used logical eraseblock number for the PEB 124062306a36Sopenharmony_ci * @pnum: physical eraseblock to return 124162306a36Sopenharmony_ci * @torture: if this physical eraseblock has to be tortured 124262306a36Sopenharmony_ci * 124362306a36Sopenharmony_ci * This function is called to return physical eraseblock @pnum to the pool of 124462306a36Sopenharmony_ci * free physical eraseblocks. The @torture flag has to be set if an I/O error 124562306a36Sopenharmony_ci * occurred to this @pnum and it has to be tested. This function returns zero 124662306a36Sopenharmony_ci * in case of success, and a negative error code in case of failure. 124762306a36Sopenharmony_ci */ 124862306a36Sopenharmony_ciint ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, 124962306a36Sopenharmony_ci int pnum, int torture) 125062306a36Sopenharmony_ci{ 125162306a36Sopenharmony_ci int err; 125262306a36Sopenharmony_ci struct ubi_wl_entry *e; 125362306a36Sopenharmony_ci 125462306a36Sopenharmony_ci dbg_wl("PEB %d", pnum); 125562306a36Sopenharmony_ci ubi_assert(pnum >= 0); 125662306a36Sopenharmony_ci ubi_assert(pnum < ubi->peb_count); 125762306a36Sopenharmony_ci 125862306a36Sopenharmony_ci down_read(&ubi->fm_protect); 125962306a36Sopenharmony_ci 126062306a36Sopenharmony_ciretry: 126162306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 126262306a36Sopenharmony_ci e = ubi->lookuptbl[pnum]; 126362306a36Sopenharmony_ci if (!e) { 126462306a36Sopenharmony_ci /* 126562306a36Sopenharmony_ci * This wl entry has been removed for some errors by other 126662306a36Sopenharmony_ci * process (eg. wear leveling worker), corresponding process 126762306a36Sopenharmony_ci * (except __erase_worker, which cannot concurrent with 126862306a36Sopenharmony_ci * ubi_wl_put_peb) will set ubi ro_mode at the same time, 126962306a36Sopenharmony_ci * just ignore this wl entry. 127062306a36Sopenharmony_ci */ 127162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 127262306a36Sopenharmony_ci up_read(&ubi->fm_protect); 127362306a36Sopenharmony_ci return 0; 127462306a36Sopenharmony_ci } 127562306a36Sopenharmony_ci if (e == ubi->move_from) { 127662306a36Sopenharmony_ci /* 127762306a36Sopenharmony_ci * User is putting the physical eraseblock which was selected to 127862306a36Sopenharmony_ci * be moved. It will be scheduled for erasure in the 127962306a36Sopenharmony_ci * wear-leveling worker. 128062306a36Sopenharmony_ci */ 128162306a36Sopenharmony_ci dbg_wl("PEB %d is being moved, wait", pnum); 128262306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 128362306a36Sopenharmony_ci 128462306a36Sopenharmony_ci /* Wait for the WL worker by taking the @ubi->move_mutex */ 128562306a36Sopenharmony_ci mutex_lock(&ubi->move_mutex); 128662306a36Sopenharmony_ci mutex_unlock(&ubi->move_mutex); 128762306a36Sopenharmony_ci goto retry; 128862306a36Sopenharmony_ci } else if (e == ubi->move_to) { 128962306a36Sopenharmony_ci /* 129062306a36Sopenharmony_ci * User is putting the physical eraseblock which was selected 129162306a36Sopenharmony_ci * as the target the data is moved to. It may happen if the EBA 129262306a36Sopenharmony_ci * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()' 129362306a36Sopenharmony_ci * but the WL sub-system has not put the PEB to the "used" tree 129462306a36Sopenharmony_ci * yet, but it is about to do this. So we just set a flag which 129562306a36Sopenharmony_ci * will tell the WL worker that the PEB is not needed anymore 129662306a36Sopenharmony_ci * and should be scheduled for erasure. 129762306a36Sopenharmony_ci */ 129862306a36Sopenharmony_ci dbg_wl("PEB %d is the target of data moving", pnum); 129962306a36Sopenharmony_ci ubi_assert(!ubi->move_to_put); 130062306a36Sopenharmony_ci ubi->move_to_put = 1; 130162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 130262306a36Sopenharmony_ci up_read(&ubi->fm_protect); 130362306a36Sopenharmony_ci return 0; 130462306a36Sopenharmony_ci } else { 130562306a36Sopenharmony_ci if (in_wl_tree(e, &ubi->used)) { 130662306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->used); 130762306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->used); 130862306a36Sopenharmony_ci } else if (in_wl_tree(e, &ubi->scrub)) { 130962306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->scrub); 131062306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->scrub); 131162306a36Sopenharmony_ci } else if (in_wl_tree(e, &ubi->erroneous)) { 131262306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->erroneous); 131362306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->erroneous); 131462306a36Sopenharmony_ci ubi->erroneous_peb_count -= 1; 131562306a36Sopenharmony_ci ubi_assert(ubi->erroneous_peb_count >= 0); 131662306a36Sopenharmony_ci /* Erroneous PEBs should be tortured */ 131762306a36Sopenharmony_ci torture = 1; 131862306a36Sopenharmony_ci } else { 131962306a36Sopenharmony_ci err = prot_queue_del(ubi, e->pnum); 132062306a36Sopenharmony_ci if (err) { 132162306a36Sopenharmony_ci ubi_err(ubi, "PEB %d not found", pnum); 132262306a36Sopenharmony_ci ubi_ro_mode(ubi); 132362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 132462306a36Sopenharmony_ci up_read(&ubi->fm_protect); 132562306a36Sopenharmony_ci return err; 132662306a36Sopenharmony_ci } 132762306a36Sopenharmony_ci } 132862306a36Sopenharmony_ci } 132962306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 133062306a36Sopenharmony_ci 133162306a36Sopenharmony_ci err = schedule_erase(ubi, e, vol_id, lnum, torture, false); 133262306a36Sopenharmony_ci if (err) { 133362306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 133462306a36Sopenharmony_ci wl_tree_add(e, &ubi->used); 133562306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 133662306a36Sopenharmony_ci } 133762306a36Sopenharmony_ci 133862306a36Sopenharmony_ci up_read(&ubi->fm_protect); 133962306a36Sopenharmony_ci return err; 134062306a36Sopenharmony_ci} 134162306a36Sopenharmony_ci 134262306a36Sopenharmony_ci/** 134362306a36Sopenharmony_ci * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing. 134462306a36Sopenharmony_ci * @ubi: UBI device description object 134562306a36Sopenharmony_ci * @pnum: the physical eraseblock to schedule 134662306a36Sopenharmony_ci * 134762306a36Sopenharmony_ci * If a bit-flip in a physical eraseblock is detected, this physical eraseblock 134862306a36Sopenharmony_ci * needs scrubbing. This function schedules a physical eraseblock for 134962306a36Sopenharmony_ci * scrubbing which is done in background. This function returns zero in case of 135062306a36Sopenharmony_ci * success and a negative error code in case of failure. 135162306a36Sopenharmony_ci */ 135262306a36Sopenharmony_ciint ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) 135362306a36Sopenharmony_ci{ 135462306a36Sopenharmony_ci struct ubi_wl_entry *e; 135562306a36Sopenharmony_ci 135662306a36Sopenharmony_ci ubi_msg(ubi, "schedule PEB %d for scrubbing", pnum); 135762306a36Sopenharmony_ci 135862306a36Sopenharmony_ciretry: 135962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 136062306a36Sopenharmony_ci e = ubi->lookuptbl[pnum]; 136162306a36Sopenharmony_ci if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) || 136262306a36Sopenharmony_ci in_wl_tree(e, &ubi->erroneous)) { 136362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 136462306a36Sopenharmony_ci return 0; 136562306a36Sopenharmony_ci } 136662306a36Sopenharmony_ci 136762306a36Sopenharmony_ci if (e == ubi->move_to) { 136862306a36Sopenharmony_ci /* 136962306a36Sopenharmony_ci * This physical eraseblock was used to move data to. The data 137062306a36Sopenharmony_ci * was moved but the PEB was not yet inserted to the proper 137162306a36Sopenharmony_ci * tree. We should just wait a little and let the WL worker 137262306a36Sopenharmony_ci * proceed. 137362306a36Sopenharmony_ci */ 137462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 137562306a36Sopenharmony_ci dbg_wl("the PEB %d is not in proper tree, retry", pnum); 137662306a36Sopenharmony_ci yield(); 137762306a36Sopenharmony_ci goto retry; 137862306a36Sopenharmony_ci } 137962306a36Sopenharmony_ci 138062306a36Sopenharmony_ci if (in_wl_tree(e, &ubi->used)) { 138162306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->used); 138262306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->used); 138362306a36Sopenharmony_ci } else { 138462306a36Sopenharmony_ci int err; 138562306a36Sopenharmony_ci 138662306a36Sopenharmony_ci err = prot_queue_del(ubi, e->pnum); 138762306a36Sopenharmony_ci if (err) { 138862306a36Sopenharmony_ci ubi_err(ubi, "PEB %d not found", pnum); 138962306a36Sopenharmony_ci ubi_ro_mode(ubi); 139062306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 139162306a36Sopenharmony_ci return err; 139262306a36Sopenharmony_ci } 139362306a36Sopenharmony_ci } 139462306a36Sopenharmony_ci 139562306a36Sopenharmony_ci wl_tree_add(e, &ubi->scrub); 139662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 139762306a36Sopenharmony_ci 139862306a36Sopenharmony_ci /* 139962306a36Sopenharmony_ci * Technically scrubbing is the same as wear-leveling, so it is done 140062306a36Sopenharmony_ci * by the WL worker. 140162306a36Sopenharmony_ci */ 140262306a36Sopenharmony_ci return ensure_wear_leveling(ubi, 0); 140362306a36Sopenharmony_ci} 140462306a36Sopenharmony_ci 140562306a36Sopenharmony_ci/** 140662306a36Sopenharmony_ci * ubi_wl_flush - flush all pending works. 140762306a36Sopenharmony_ci * @ubi: UBI device description object 140862306a36Sopenharmony_ci * @vol_id: the volume id to flush for 140962306a36Sopenharmony_ci * @lnum: the logical eraseblock number to flush for 141062306a36Sopenharmony_ci * 141162306a36Sopenharmony_ci * This function executes all pending works for a particular volume id / 141262306a36Sopenharmony_ci * logical eraseblock number pair. If either value is set to %UBI_ALL, then it 141362306a36Sopenharmony_ci * acts as a wildcard for all of the corresponding volume numbers or logical 141462306a36Sopenharmony_ci * eraseblock numbers. It returns zero in case of success and a negative error 141562306a36Sopenharmony_ci * code in case of failure. 141662306a36Sopenharmony_ci */ 141762306a36Sopenharmony_ciint ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) 141862306a36Sopenharmony_ci{ 141962306a36Sopenharmony_ci int err = 0; 142062306a36Sopenharmony_ci int found = 1; 142162306a36Sopenharmony_ci 142262306a36Sopenharmony_ci /* 142362306a36Sopenharmony_ci * Erase while the pending works queue is not empty, but not more than 142462306a36Sopenharmony_ci * the number of currently pending works. 142562306a36Sopenharmony_ci */ 142662306a36Sopenharmony_ci dbg_wl("flush pending work for LEB %d:%d (%d pending works)", 142762306a36Sopenharmony_ci vol_id, lnum, ubi->works_count); 142862306a36Sopenharmony_ci 142962306a36Sopenharmony_ci while (found) { 143062306a36Sopenharmony_ci struct ubi_work *wrk, *tmp; 143162306a36Sopenharmony_ci found = 0; 143262306a36Sopenharmony_ci 143362306a36Sopenharmony_ci down_read(&ubi->work_sem); 143462306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 143562306a36Sopenharmony_ci list_for_each_entry_safe(wrk, tmp, &ubi->works, list) { 143662306a36Sopenharmony_ci if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) && 143762306a36Sopenharmony_ci (lnum == UBI_ALL || wrk->lnum == lnum)) { 143862306a36Sopenharmony_ci list_del(&wrk->list); 143962306a36Sopenharmony_ci ubi->works_count -= 1; 144062306a36Sopenharmony_ci ubi_assert(ubi->works_count >= 0); 144162306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 144262306a36Sopenharmony_ci 144362306a36Sopenharmony_ci err = wrk->func(ubi, wrk, 0); 144462306a36Sopenharmony_ci if (err) { 144562306a36Sopenharmony_ci up_read(&ubi->work_sem); 144662306a36Sopenharmony_ci return err; 144762306a36Sopenharmony_ci } 144862306a36Sopenharmony_ci 144962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 145062306a36Sopenharmony_ci found = 1; 145162306a36Sopenharmony_ci break; 145262306a36Sopenharmony_ci } 145362306a36Sopenharmony_ci } 145462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 145562306a36Sopenharmony_ci up_read(&ubi->work_sem); 145662306a36Sopenharmony_ci } 145762306a36Sopenharmony_ci 145862306a36Sopenharmony_ci /* 145962306a36Sopenharmony_ci * Make sure all the works which have been done in parallel are 146062306a36Sopenharmony_ci * finished. 146162306a36Sopenharmony_ci */ 146262306a36Sopenharmony_ci down_write(&ubi->work_sem); 146362306a36Sopenharmony_ci up_write(&ubi->work_sem); 146462306a36Sopenharmony_ci 146562306a36Sopenharmony_ci return err; 146662306a36Sopenharmony_ci} 146762306a36Sopenharmony_ci 146862306a36Sopenharmony_cistatic bool scrub_possible(struct ubi_device *ubi, struct ubi_wl_entry *e) 146962306a36Sopenharmony_ci{ 147062306a36Sopenharmony_ci if (in_wl_tree(e, &ubi->scrub)) 147162306a36Sopenharmony_ci return false; 147262306a36Sopenharmony_ci else if (in_wl_tree(e, &ubi->erroneous)) 147362306a36Sopenharmony_ci return false; 147462306a36Sopenharmony_ci else if (ubi->move_from == e) 147562306a36Sopenharmony_ci return false; 147662306a36Sopenharmony_ci else if (ubi->move_to == e) 147762306a36Sopenharmony_ci return false; 147862306a36Sopenharmony_ci 147962306a36Sopenharmony_ci return true; 148062306a36Sopenharmony_ci} 148162306a36Sopenharmony_ci 148262306a36Sopenharmony_ci/** 148362306a36Sopenharmony_ci * ubi_bitflip_check - Check an eraseblock for bitflips and scrub it if needed. 148462306a36Sopenharmony_ci * @ubi: UBI device description object 148562306a36Sopenharmony_ci * @pnum: the physical eraseblock to schedule 148662306a36Sopenharmony_ci * @force: don't read the block, assume bitflips happened and take action. 148762306a36Sopenharmony_ci * 148862306a36Sopenharmony_ci * This function reads the given eraseblock and checks if bitflips occured. 148962306a36Sopenharmony_ci * In case of bitflips, the eraseblock is scheduled for scrubbing. 149062306a36Sopenharmony_ci * If scrubbing is forced with @force, the eraseblock is not read, 149162306a36Sopenharmony_ci * but scheduled for scrubbing right away. 149262306a36Sopenharmony_ci * 149362306a36Sopenharmony_ci * Returns: 149462306a36Sopenharmony_ci * %EINVAL, PEB is out of range 149562306a36Sopenharmony_ci * %ENOENT, PEB is no longer used by UBI 149662306a36Sopenharmony_ci * %EBUSY, PEB cannot be checked now or a check is currently running on it 149762306a36Sopenharmony_ci * %EAGAIN, bit flips happened but scrubbing is currently not possible 149862306a36Sopenharmony_ci * %EUCLEAN, bit flips happened and PEB is scheduled for scrubbing 149962306a36Sopenharmony_ci * %0, no bit flips detected 150062306a36Sopenharmony_ci */ 150162306a36Sopenharmony_ciint ubi_bitflip_check(struct ubi_device *ubi, int pnum, int force) 150262306a36Sopenharmony_ci{ 150362306a36Sopenharmony_ci int err = 0; 150462306a36Sopenharmony_ci struct ubi_wl_entry *e; 150562306a36Sopenharmony_ci 150662306a36Sopenharmony_ci if (pnum < 0 || pnum >= ubi->peb_count) { 150762306a36Sopenharmony_ci err = -EINVAL; 150862306a36Sopenharmony_ci goto out; 150962306a36Sopenharmony_ci } 151062306a36Sopenharmony_ci 151162306a36Sopenharmony_ci /* 151262306a36Sopenharmony_ci * Pause all parallel work, otherwise it can happen that the 151362306a36Sopenharmony_ci * erase worker frees a wl entry under us. 151462306a36Sopenharmony_ci */ 151562306a36Sopenharmony_ci down_write(&ubi->work_sem); 151662306a36Sopenharmony_ci 151762306a36Sopenharmony_ci /* 151862306a36Sopenharmony_ci * Make sure that the wl entry does not change state while 151962306a36Sopenharmony_ci * inspecting it. 152062306a36Sopenharmony_ci */ 152162306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 152262306a36Sopenharmony_ci e = ubi->lookuptbl[pnum]; 152362306a36Sopenharmony_ci if (!e) { 152462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 152562306a36Sopenharmony_ci err = -ENOENT; 152662306a36Sopenharmony_ci goto out_resume; 152762306a36Sopenharmony_ci } 152862306a36Sopenharmony_ci 152962306a36Sopenharmony_ci /* 153062306a36Sopenharmony_ci * Does it make sense to check this PEB? 153162306a36Sopenharmony_ci */ 153262306a36Sopenharmony_ci if (!scrub_possible(ubi, e)) { 153362306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 153462306a36Sopenharmony_ci err = -EBUSY; 153562306a36Sopenharmony_ci goto out_resume; 153662306a36Sopenharmony_ci } 153762306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 153862306a36Sopenharmony_ci 153962306a36Sopenharmony_ci if (!force) { 154062306a36Sopenharmony_ci mutex_lock(&ubi->buf_mutex); 154162306a36Sopenharmony_ci err = ubi_io_read(ubi, ubi->peb_buf, pnum, 0, ubi->peb_size); 154262306a36Sopenharmony_ci mutex_unlock(&ubi->buf_mutex); 154362306a36Sopenharmony_ci } 154462306a36Sopenharmony_ci 154562306a36Sopenharmony_ci if (force || err == UBI_IO_BITFLIPS) { 154662306a36Sopenharmony_ci /* 154762306a36Sopenharmony_ci * Okay, bit flip happened, let's figure out what we can do. 154862306a36Sopenharmony_ci */ 154962306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 155062306a36Sopenharmony_ci 155162306a36Sopenharmony_ci /* 155262306a36Sopenharmony_ci * Recheck. We released wl_lock, UBI might have killed the 155362306a36Sopenharmony_ci * wl entry under us. 155462306a36Sopenharmony_ci */ 155562306a36Sopenharmony_ci e = ubi->lookuptbl[pnum]; 155662306a36Sopenharmony_ci if (!e) { 155762306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 155862306a36Sopenharmony_ci err = -ENOENT; 155962306a36Sopenharmony_ci goto out_resume; 156062306a36Sopenharmony_ci } 156162306a36Sopenharmony_ci 156262306a36Sopenharmony_ci /* 156362306a36Sopenharmony_ci * Need to re-check state 156462306a36Sopenharmony_ci */ 156562306a36Sopenharmony_ci if (!scrub_possible(ubi, e)) { 156662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 156762306a36Sopenharmony_ci err = -EBUSY; 156862306a36Sopenharmony_ci goto out_resume; 156962306a36Sopenharmony_ci } 157062306a36Sopenharmony_ci 157162306a36Sopenharmony_ci if (in_pq(ubi, e)) { 157262306a36Sopenharmony_ci prot_queue_del(ubi, e->pnum); 157362306a36Sopenharmony_ci wl_tree_add(e, &ubi->scrub); 157462306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 157562306a36Sopenharmony_ci 157662306a36Sopenharmony_ci err = ensure_wear_leveling(ubi, 1); 157762306a36Sopenharmony_ci } else if (in_wl_tree(e, &ubi->used)) { 157862306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->used); 157962306a36Sopenharmony_ci wl_tree_add(e, &ubi->scrub); 158062306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 158162306a36Sopenharmony_ci 158262306a36Sopenharmony_ci err = ensure_wear_leveling(ubi, 1); 158362306a36Sopenharmony_ci } else if (in_wl_tree(e, &ubi->free)) { 158462306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->free); 158562306a36Sopenharmony_ci ubi->free_count--; 158662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 158762306a36Sopenharmony_ci 158862306a36Sopenharmony_ci /* 158962306a36Sopenharmony_ci * This PEB is empty we can schedule it for 159062306a36Sopenharmony_ci * erasure right away. No wear leveling needed. 159162306a36Sopenharmony_ci */ 159262306a36Sopenharmony_ci err = schedule_erase(ubi, e, UBI_UNKNOWN, UBI_UNKNOWN, 159362306a36Sopenharmony_ci force ? 0 : 1, true); 159462306a36Sopenharmony_ci } else { 159562306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 159662306a36Sopenharmony_ci err = -EAGAIN; 159762306a36Sopenharmony_ci } 159862306a36Sopenharmony_ci 159962306a36Sopenharmony_ci if (!err && !force) 160062306a36Sopenharmony_ci err = -EUCLEAN; 160162306a36Sopenharmony_ci } else { 160262306a36Sopenharmony_ci err = 0; 160362306a36Sopenharmony_ci } 160462306a36Sopenharmony_ci 160562306a36Sopenharmony_ciout_resume: 160662306a36Sopenharmony_ci up_write(&ubi->work_sem); 160762306a36Sopenharmony_ciout: 160862306a36Sopenharmony_ci 160962306a36Sopenharmony_ci return err; 161062306a36Sopenharmony_ci} 161162306a36Sopenharmony_ci 161262306a36Sopenharmony_ci/** 161362306a36Sopenharmony_ci * tree_destroy - destroy an RB-tree. 161462306a36Sopenharmony_ci * @ubi: UBI device description object 161562306a36Sopenharmony_ci * @root: the root of the tree to destroy 161662306a36Sopenharmony_ci */ 161762306a36Sopenharmony_cistatic void tree_destroy(struct ubi_device *ubi, struct rb_root *root) 161862306a36Sopenharmony_ci{ 161962306a36Sopenharmony_ci struct rb_node *rb; 162062306a36Sopenharmony_ci struct ubi_wl_entry *e; 162162306a36Sopenharmony_ci 162262306a36Sopenharmony_ci rb = root->rb_node; 162362306a36Sopenharmony_ci while (rb) { 162462306a36Sopenharmony_ci if (rb->rb_left) 162562306a36Sopenharmony_ci rb = rb->rb_left; 162662306a36Sopenharmony_ci else if (rb->rb_right) 162762306a36Sopenharmony_ci rb = rb->rb_right; 162862306a36Sopenharmony_ci else { 162962306a36Sopenharmony_ci e = rb_entry(rb, struct ubi_wl_entry, u.rb); 163062306a36Sopenharmony_ci 163162306a36Sopenharmony_ci rb = rb_parent(rb); 163262306a36Sopenharmony_ci if (rb) { 163362306a36Sopenharmony_ci if (rb->rb_left == &e->u.rb) 163462306a36Sopenharmony_ci rb->rb_left = NULL; 163562306a36Sopenharmony_ci else 163662306a36Sopenharmony_ci rb->rb_right = NULL; 163762306a36Sopenharmony_ci } 163862306a36Sopenharmony_ci 163962306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 164062306a36Sopenharmony_ci } 164162306a36Sopenharmony_ci } 164262306a36Sopenharmony_ci} 164362306a36Sopenharmony_ci 164462306a36Sopenharmony_ci/** 164562306a36Sopenharmony_ci * ubi_thread - UBI background thread. 164662306a36Sopenharmony_ci * @u: the UBI device description object pointer 164762306a36Sopenharmony_ci */ 164862306a36Sopenharmony_ciint ubi_thread(void *u) 164962306a36Sopenharmony_ci{ 165062306a36Sopenharmony_ci int failures = 0; 165162306a36Sopenharmony_ci struct ubi_device *ubi = u; 165262306a36Sopenharmony_ci 165362306a36Sopenharmony_ci ubi_msg(ubi, "background thread \"%s\" started, PID %d", 165462306a36Sopenharmony_ci ubi->bgt_name, task_pid_nr(current)); 165562306a36Sopenharmony_ci 165662306a36Sopenharmony_ci set_freezable(); 165762306a36Sopenharmony_ci for (;;) { 165862306a36Sopenharmony_ci int err; 165962306a36Sopenharmony_ci 166062306a36Sopenharmony_ci if (kthread_should_stop()) 166162306a36Sopenharmony_ci break; 166262306a36Sopenharmony_ci 166362306a36Sopenharmony_ci if (try_to_freeze()) 166462306a36Sopenharmony_ci continue; 166562306a36Sopenharmony_ci 166662306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 166762306a36Sopenharmony_ci if (list_empty(&ubi->works) || ubi->ro_mode || 166862306a36Sopenharmony_ci !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) { 166962306a36Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 167062306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 167162306a36Sopenharmony_ci 167262306a36Sopenharmony_ci /* 167362306a36Sopenharmony_ci * Check kthread_should_stop() after we set the task 167462306a36Sopenharmony_ci * state to guarantee that we either see the stop bit 167562306a36Sopenharmony_ci * and exit or the task state is reset to runnable such 167662306a36Sopenharmony_ci * that it's not scheduled out indefinitely and detects 167762306a36Sopenharmony_ci * the stop bit at kthread_should_stop(). 167862306a36Sopenharmony_ci */ 167962306a36Sopenharmony_ci if (kthread_should_stop()) { 168062306a36Sopenharmony_ci set_current_state(TASK_RUNNING); 168162306a36Sopenharmony_ci break; 168262306a36Sopenharmony_ci } 168362306a36Sopenharmony_ci 168462306a36Sopenharmony_ci schedule(); 168562306a36Sopenharmony_ci continue; 168662306a36Sopenharmony_ci } 168762306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 168862306a36Sopenharmony_ci 168962306a36Sopenharmony_ci err = do_work(ubi); 169062306a36Sopenharmony_ci if (err) { 169162306a36Sopenharmony_ci ubi_err(ubi, "%s: work failed with error code %d", 169262306a36Sopenharmony_ci ubi->bgt_name, err); 169362306a36Sopenharmony_ci if (failures++ > WL_MAX_FAILURES) { 169462306a36Sopenharmony_ci /* 169562306a36Sopenharmony_ci * Too many failures, disable the thread and 169662306a36Sopenharmony_ci * switch to read-only mode. 169762306a36Sopenharmony_ci */ 169862306a36Sopenharmony_ci ubi_msg(ubi, "%s: %d consecutive failures", 169962306a36Sopenharmony_ci ubi->bgt_name, WL_MAX_FAILURES); 170062306a36Sopenharmony_ci ubi_ro_mode(ubi); 170162306a36Sopenharmony_ci ubi->thread_enabled = 0; 170262306a36Sopenharmony_ci continue; 170362306a36Sopenharmony_ci } 170462306a36Sopenharmony_ci } else 170562306a36Sopenharmony_ci failures = 0; 170662306a36Sopenharmony_ci 170762306a36Sopenharmony_ci cond_resched(); 170862306a36Sopenharmony_ci } 170962306a36Sopenharmony_ci 171062306a36Sopenharmony_ci dbg_wl("background thread \"%s\" is killed", ubi->bgt_name); 171162306a36Sopenharmony_ci ubi->thread_enabled = 0; 171262306a36Sopenharmony_ci return 0; 171362306a36Sopenharmony_ci} 171462306a36Sopenharmony_ci 171562306a36Sopenharmony_ci/** 171662306a36Sopenharmony_ci * shutdown_work - shutdown all pending works. 171762306a36Sopenharmony_ci * @ubi: UBI device description object 171862306a36Sopenharmony_ci */ 171962306a36Sopenharmony_cistatic void shutdown_work(struct ubi_device *ubi) 172062306a36Sopenharmony_ci{ 172162306a36Sopenharmony_ci while (!list_empty(&ubi->works)) { 172262306a36Sopenharmony_ci struct ubi_work *wrk; 172362306a36Sopenharmony_ci 172462306a36Sopenharmony_ci wrk = list_entry(ubi->works.next, struct ubi_work, list); 172562306a36Sopenharmony_ci list_del(&wrk->list); 172662306a36Sopenharmony_ci wrk->func(ubi, wrk, 1); 172762306a36Sopenharmony_ci ubi->works_count -= 1; 172862306a36Sopenharmony_ci ubi_assert(ubi->works_count >= 0); 172962306a36Sopenharmony_ci } 173062306a36Sopenharmony_ci} 173162306a36Sopenharmony_ci 173262306a36Sopenharmony_ci/** 173362306a36Sopenharmony_ci * erase_aeb - erase a PEB given in UBI attach info PEB 173462306a36Sopenharmony_ci * @ubi: UBI device description object 173562306a36Sopenharmony_ci * @aeb: UBI attach info PEB 173662306a36Sopenharmony_ci * @sync: If true, erase synchronously. Otherwise schedule for erasure 173762306a36Sopenharmony_ci */ 173862306a36Sopenharmony_cistatic int erase_aeb(struct ubi_device *ubi, struct ubi_ainf_peb *aeb, bool sync) 173962306a36Sopenharmony_ci{ 174062306a36Sopenharmony_ci struct ubi_wl_entry *e; 174162306a36Sopenharmony_ci int err; 174262306a36Sopenharmony_ci 174362306a36Sopenharmony_ci e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 174462306a36Sopenharmony_ci if (!e) 174562306a36Sopenharmony_ci return -ENOMEM; 174662306a36Sopenharmony_ci 174762306a36Sopenharmony_ci e->pnum = aeb->pnum; 174862306a36Sopenharmony_ci e->ec = aeb->ec; 174962306a36Sopenharmony_ci ubi->lookuptbl[e->pnum] = e; 175062306a36Sopenharmony_ci 175162306a36Sopenharmony_ci if (sync) { 175262306a36Sopenharmony_ci err = sync_erase(ubi, e, false); 175362306a36Sopenharmony_ci if (err) 175462306a36Sopenharmony_ci goto out_free; 175562306a36Sopenharmony_ci 175662306a36Sopenharmony_ci wl_tree_add(e, &ubi->free); 175762306a36Sopenharmony_ci ubi->free_count++; 175862306a36Sopenharmony_ci } else { 175962306a36Sopenharmony_ci err = schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0, false); 176062306a36Sopenharmony_ci if (err) 176162306a36Sopenharmony_ci goto out_free; 176262306a36Sopenharmony_ci } 176362306a36Sopenharmony_ci 176462306a36Sopenharmony_ci return 0; 176562306a36Sopenharmony_ci 176662306a36Sopenharmony_ciout_free: 176762306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 176862306a36Sopenharmony_ci 176962306a36Sopenharmony_ci return err; 177062306a36Sopenharmony_ci} 177162306a36Sopenharmony_ci 177262306a36Sopenharmony_ci/** 177362306a36Sopenharmony_ci * ubi_wl_init - initialize the WL sub-system using attaching information. 177462306a36Sopenharmony_ci * @ubi: UBI device description object 177562306a36Sopenharmony_ci * @ai: attaching information 177662306a36Sopenharmony_ci * 177762306a36Sopenharmony_ci * This function returns zero in case of success, and a negative error code in 177862306a36Sopenharmony_ci * case of failure. 177962306a36Sopenharmony_ci */ 178062306a36Sopenharmony_ciint ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) 178162306a36Sopenharmony_ci{ 178262306a36Sopenharmony_ci int err, i, reserved_pebs, found_pebs = 0; 178362306a36Sopenharmony_ci struct rb_node *rb1, *rb2; 178462306a36Sopenharmony_ci struct ubi_ainf_volume *av; 178562306a36Sopenharmony_ci struct ubi_ainf_peb *aeb, *tmp; 178662306a36Sopenharmony_ci struct ubi_wl_entry *e; 178762306a36Sopenharmony_ci 178862306a36Sopenharmony_ci ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT; 178962306a36Sopenharmony_ci spin_lock_init(&ubi->wl_lock); 179062306a36Sopenharmony_ci mutex_init(&ubi->move_mutex); 179162306a36Sopenharmony_ci init_rwsem(&ubi->work_sem); 179262306a36Sopenharmony_ci ubi->max_ec = ai->max_ec; 179362306a36Sopenharmony_ci INIT_LIST_HEAD(&ubi->works); 179462306a36Sopenharmony_ci 179562306a36Sopenharmony_ci sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num); 179662306a36Sopenharmony_ci 179762306a36Sopenharmony_ci err = -ENOMEM; 179862306a36Sopenharmony_ci ubi->lookuptbl = kcalloc(ubi->peb_count, sizeof(void *), GFP_KERNEL); 179962306a36Sopenharmony_ci if (!ubi->lookuptbl) 180062306a36Sopenharmony_ci return err; 180162306a36Sopenharmony_ci 180262306a36Sopenharmony_ci for (i = 0; i < UBI_PROT_QUEUE_LEN; i++) 180362306a36Sopenharmony_ci INIT_LIST_HEAD(&ubi->pq[i]); 180462306a36Sopenharmony_ci ubi->pq_head = 0; 180562306a36Sopenharmony_ci 180662306a36Sopenharmony_ci ubi->free_count = 0; 180762306a36Sopenharmony_ci list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) { 180862306a36Sopenharmony_ci cond_resched(); 180962306a36Sopenharmony_ci 181062306a36Sopenharmony_ci err = erase_aeb(ubi, aeb, false); 181162306a36Sopenharmony_ci if (err) 181262306a36Sopenharmony_ci goto out_free; 181362306a36Sopenharmony_ci 181462306a36Sopenharmony_ci found_pebs++; 181562306a36Sopenharmony_ci } 181662306a36Sopenharmony_ci 181762306a36Sopenharmony_ci list_for_each_entry(aeb, &ai->free, u.list) { 181862306a36Sopenharmony_ci cond_resched(); 181962306a36Sopenharmony_ci 182062306a36Sopenharmony_ci e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 182162306a36Sopenharmony_ci if (!e) { 182262306a36Sopenharmony_ci err = -ENOMEM; 182362306a36Sopenharmony_ci goto out_free; 182462306a36Sopenharmony_ci } 182562306a36Sopenharmony_ci 182662306a36Sopenharmony_ci e->pnum = aeb->pnum; 182762306a36Sopenharmony_ci e->ec = aeb->ec; 182862306a36Sopenharmony_ci ubi_assert(e->ec >= 0); 182962306a36Sopenharmony_ci 183062306a36Sopenharmony_ci wl_tree_add(e, &ubi->free); 183162306a36Sopenharmony_ci ubi->free_count++; 183262306a36Sopenharmony_ci 183362306a36Sopenharmony_ci ubi->lookuptbl[e->pnum] = e; 183462306a36Sopenharmony_ci 183562306a36Sopenharmony_ci found_pebs++; 183662306a36Sopenharmony_ci } 183762306a36Sopenharmony_ci 183862306a36Sopenharmony_ci ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { 183962306a36Sopenharmony_ci ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { 184062306a36Sopenharmony_ci cond_resched(); 184162306a36Sopenharmony_ci 184262306a36Sopenharmony_ci e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); 184362306a36Sopenharmony_ci if (!e) { 184462306a36Sopenharmony_ci err = -ENOMEM; 184562306a36Sopenharmony_ci goto out_free; 184662306a36Sopenharmony_ci } 184762306a36Sopenharmony_ci 184862306a36Sopenharmony_ci e->pnum = aeb->pnum; 184962306a36Sopenharmony_ci e->ec = aeb->ec; 185062306a36Sopenharmony_ci ubi->lookuptbl[e->pnum] = e; 185162306a36Sopenharmony_ci 185262306a36Sopenharmony_ci if (!aeb->scrub) { 185362306a36Sopenharmony_ci dbg_wl("add PEB %d EC %d to the used tree", 185462306a36Sopenharmony_ci e->pnum, e->ec); 185562306a36Sopenharmony_ci wl_tree_add(e, &ubi->used); 185662306a36Sopenharmony_ci } else { 185762306a36Sopenharmony_ci dbg_wl("add PEB %d EC %d to the scrub tree", 185862306a36Sopenharmony_ci e->pnum, e->ec); 185962306a36Sopenharmony_ci wl_tree_add(e, &ubi->scrub); 186062306a36Sopenharmony_ci } 186162306a36Sopenharmony_ci 186262306a36Sopenharmony_ci found_pebs++; 186362306a36Sopenharmony_ci } 186462306a36Sopenharmony_ci } 186562306a36Sopenharmony_ci 186662306a36Sopenharmony_ci list_for_each_entry(aeb, &ai->fastmap, u.list) { 186762306a36Sopenharmony_ci cond_resched(); 186862306a36Sopenharmony_ci 186962306a36Sopenharmony_ci e = ubi_find_fm_block(ubi, aeb->pnum); 187062306a36Sopenharmony_ci 187162306a36Sopenharmony_ci if (e) { 187262306a36Sopenharmony_ci ubi_assert(!ubi->lookuptbl[e->pnum]); 187362306a36Sopenharmony_ci ubi->lookuptbl[e->pnum] = e; 187462306a36Sopenharmony_ci } else { 187562306a36Sopenharmony_ci bool sync = false; 187662306a36Sopenharmony_ci 187762306a36Sopenharmony_ci /* 187862306a36Sopenharmony_ci * Usually old Fastmap PEBs are scheduled for erasure 187962306a36Sopenharmony_ci * and we don't have to care about them but if we face 188062306a36Sopenharmony_ci * an power cut before scheduling them we need to 188162306a36Sopenharmony_ci * take care of them here. 188262306a36Sopenharmony_ci */ 188362306a36Sopenharmony_ci if (ubi->lookuptbl[aeb->pnum]) 188462306a36Sopenharmony_ci continue; 188562306a36Sopenharmony_ci 188662306a36Sopenharmony_ci /* 188762306a36Sopenharmony_ci * The fastmap update code might not find a free PEB for 188862306a36Sopenharmony_ci * writing the fastmap anchor to and then reuses the 188962306a36Sopenharmony_ci * current fastmap anchor PEB. When this PEB gets erased 189062306a36Sopenharmony_ci * and a power cut happens before it is written again we 189162306a36Sopenharmony_ci * must make sure that the fastmap attach code doesn't 189262306a36Sopenharmony_ci * find any outdated fastmap anchors, hence we erase the 189362306a36Sopenharmony_ci * outdated fastmap anchor PEBs synchronously here. 189462306a36Sopenharmony_ci */ 189562306a36Sopenharmony_ci if (aeb->vol_id == UBI_FM_SB_VOLUME_ID) 189662306a36Sopenharmony_ci sync = true; 189762306a36Sopenharmony_ci 189862306a36Sopenharmony_ci err = erase_aeb(ubi, aeb, sync); 189962306a36Sopenharmony_ci if (err) 190062306a36Sopenharmony_ci goto out_free; 190162306a36Sopenharmony_ci } 190262306a36Sopenharmony_ci 190362306a36Sopenharmony_ci found_pebs++; 190462306a36Sopenharmony_ci } 190562306a36Sopenharmony_ci 190662306a36Sopenharmony_ci dbg_wl("found %i PEBs", found_pebs); 190762306a36Sopenharmony_ci 190862306a36Sopenharmony_ci ubi_assert(ubi->good_peb_count == found_pebs); 190962306a36Sopenharmony_ci 191062306a36Sopenharmony_ci reserved_pebs = WL_RESERVED_PEBS; 191162306a36Sopenharmony_ci ubi_fastmap_init(ubi, &reserved_pebs); 191262306a36Sopenharmony_ci 191362306a36Sopenharmony_ci if (ubi->avail_pebs < reserved_pebs) { 191462306a36Sopenharmony_ci ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", 191562306a36Sopenharmony_ci ubi->avail_pebs, reserved_pebs); 191662306a36Sopenharmony_ci if (ubi->corr_peb_count) 191762306a36Sopenharmony_ci ubi_err(ubi, "%d PEBs are corrupted and not used", 191862306a36Sopenharmony_ci ubi->corr_peb_count); 191962306a36Sopenharmony_ci err = -ENOSPC; 192062306a36Sopenharmony_ci goto out_free; 192162306a36Sopenharmony_ci } 192262306a36Sopenharmony_ci ubi->avail_pebs -= reserved_pebs; 192362306a36Sopenharmony_ci ubi->rsvd_pebs += reserved_pebs; 192462306a36Sopenharmony_ci 192562306a36Sopenharmony_ci /* Schedule wear-leveling if needed */ 192662306a36Sopenharmony_ci err = ensure_wear_leveling(ubi, 0); 192762306a36Sopenharmony_ci if (err) 192862306a36Sopenharmony_ci goto out_free; 192962306a36Sopenharmony_ci 193062306a36Sopenharmony_ci#ifdef CONFIG_MTD_UBI_FASTMAP 193162306a36Sopenharmony_ci if (!ubi->ro_mode && !ubi->fm_disabled) 193262306a36Sopenharmony_ci ubi_ensure_anchor_pebs(ubi); 193362306a36Sopenharmony_ci#endif 193462306a36Sopenharmony_ci return 0; 193562306a36Sopenharmony_ci 193662306a36Sopenharmony_ciout_free: 193762306a36Sopenharmony_ci shutdown_work(ubi); 193862306a36Sopenharmony_ci tree_destroy(ubi, &ubi->used); 193962306a36Sopenharmony_ci tree_destroy(ubi, &ubi->free); 194062306a36Sopenharmony_ci tree_destroy(ubi, &ubi->scrub); 194162306a36Sopenharmony_ci kfree(ubi->lookuptbl); 194262306a36Sopenharmony_ci return err; 194362306a36Sopenharmony_ci} 194462306a36Sopenharmony_ci 194562306a36Sopenharmony_ci/** 194662306a36Sopenharmony_ci * protection_queue_destroy - destroy the protection queue. 194762306a36Sopenharmony_ci * @ubi: UBI device description object 194862306a36Sopenharmony_ci */ 194962306a36Sopenharmony_cistatic void protection_queue_destroy(struct ubi_device *ubi) 195062306a36Sopenharmony_ci{ 195162306a36Sopenharmony_ci int i; 195262306a36Sopenharmony_ci struct ubi_wl_entry *e, *tmp; 195362306a36Sopenharmony_ci 195462306a36Sopenharmony_ci for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) { 195562306a36Sopenharmony_ci list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) { 195662306a36Sopenharmony_ci list_del(&e->u.list); 195762306a36Sopenharmony_ci wl_entry_destroy(ubi, e); 195862306a36Sopenharmony_ci } 195962306a36Sopenharmony_ci } 196062306a36Sopenharmony_ci} 196162306a36Sopenharmony_ci 196262306a36Sopenharmony_ci/** 196362306a36Sopenharmony_ci * ubi_wl_close - close the wear-leveling sub-system. 196462306a36Sopenharmony_ci * @ubi: UBI device description object 196562306a36Sopenharmony_ci */ 196662306a36Sopenharmony_civoid ubi_wl_close(struct ubi_device *ubi) 196762306a36Sopenharmony_ci{ 196862306a36Sopenharmony_ci dbg_wl("close the WL sub-system"); 196962306a36Sopenharmony_ci ubi_fastmap_close(ubi); 197062306a36Sopenharmony_ci shutdown_work(ubi); 197162306a36Sopenharmony_ci protection_queue_destroy(ubi); 197262306a36Sopenharmony_ci tree_destroy(ubi, &ubi->used); 197362306a36Sopenharmony_ci tree_destroy(ubi, &ubi->erroneous); 197462306a36Sopenharmony_ci tree_destroy(ubi, &ubi->free); 197562306a36Sopenharmony_ci tree_destroy(ubi, &ubi->scrub); 197662306a36Sopenharmony_ci kfree(ubi->lookuptbl); 197762306a36Sopenharmony_ci} 197862306a36Sopenharmony_ci 197962306a36Sopenharmony_ci/** 198062306a36Sopenharmony_ci * self_check_ec - make sure that the erase counter of a PEB is correct. 198162306a36Sopenharmony_ci * @ubi: UBI device description object 198262306a36Sopenharmony_ci * @pnum: the physical eraseblock number to check 198362306a36Sopenharmony_ci * @ec: the erase counter to check 198462306a36Sopenharmony_ci * 198562306a36Sopenharmony_ci * This function returns zero if the erase counter of physical eraseblock @pnum 198662306a36Sopenharmony_ci * is equivalent to @ec, and a negative error code if not or if an error 198762306a36Sopenharmony_ci * occurred. 198862306a36Sopenharmony_ci */ 198962306a36Sopenharmony_cistatic int self_check_ec(struct ubi_device *ubi, int pnum, int ec) 199062306a36Sopenharmony_ci{ 199162306a36Sopenharmony_ci int err; 199262306a36Sopenharmony_ci long long read_ec; 199362306a36Sopenharmony_ci struct ubi_ec_hdr *ec_hdr; 199462306a36Sopenharmony_ci 199562306a36Sopenharmony_ci if (!ubi_dbg_chk_gen(ubi)) 199662306a36Sopenharmony_ci return 0; 199762306a36Sopenharmony_ci 199862306a36Sopenharmony_ci ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 199962306a36Sopenharmony_ci if (!ec_hdr) 200062306a36Sopenharmony_ci return -ENOMEM; 200162306a36Sopenharmony_ci 200262306a36Sopenharmony_ci err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); 200362306a36Sopenharmony_ci if (err && err != UBI_IO_BITFLIPS) { 200462306a36Sopenharmony_ci /* The header does not have to exist */ 200562306a36Sopenharmony_ci err = 0; 200662306a36Sopenharmony_ci goto out_free; 200762306a36Sopenharmony_ci } 200862306a36Sopenharmony_ci 200962306a36Sopenharmony_ci read_ec = be64_to_cpu(ec_hdr->ec); 201062306a36Sopenharmony_ci if (ec != read_ec && read_ec - ec > 1) { 201162306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d", pnum); 201262306a36Sopenharmony_ci ubi_err(ubi, "read EC is %lld, should be %d", read_ec, ec); 201362306a36Sopenharmony_ci dump_stack(); 201462306a36Sopenharmony_ci err = 1; 201562306a36Sopenharmony_ci } else 201662306a36Sopenharmony_ci err = 0; 201762306a36Sopenharmony_ci 201862306a36Sopenharmony_ciout_free: 201962306a36Sopenharmony_ci kfree(ec_hdr); 202062306a36Sopenharmony_ci return err; 202162306a36Sopenharmony_ci} 202262306a36Sopenharmony_ci 202362306a36Sopenharmony_ci/** 202462306a36Sopenharmony_ci * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree. 202562306a36Sopenharmony_ci * @ubi: UBI device description object 202662306a36Sopenharmony_ci * @e: the wear-leveling entry to check 202762306a36Sopenharmony_ci * @root: the root of the tree 202862306a36Sopenharmony_ci * 202962306a36Sopenharmony_ci * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it 203062306a36Sopenharmony_ci * is not. 203162306a36Sopenharmony_ci */ 203262306a36Sopenharmony_cistatic int self_check_in_wl_tree(const struct ubi_device *ubi, 203362306a36Sopenharmony_ci struct ubi_wl_entry *e, struct rb_root *root) 203462306a36Sopenharmony_ci{ 203562306a36Sopenharmony_ci if (!ubi_dbg_chk_gen(ubi)) 203662306a36Sopenharmony_ci return 0; 203762306a36Sopenharmony_ci 203862306a36Sopenharmony_ci if (in_wl_tree(e, root)) 203962306a36Sopenharmony_ci return 0; 204062306a36Sopenharmony_ci 204162306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d, EC %d, RB-tree %p ", 204262306a36Sopenharmony_ci e->pnum, e->ec, root); 204362306a36Sopenharmony_ci dump_stack(); 204462306a36Sopenharmony_ci return -EINVAL; 204562306a36Sopenharmony_ci} 204662306a36Sopenharmony_ci 204762306a36Sopenharmony_ci/** 204862306a36Sopenharmony_ci * self_check_in_pq - check if wear-leveling entry is in the protection 204962306a36Sopenharmony_ci * queue. 205062306a36Sopenharmony_ci * @ubi: UBI device description object 205162306a36Sopenharmony_ci * @e: the wear-leveling entry to check 205262306a36Sopenharmony_ci * 205362306a36Sopenharmony_ci * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not. 205462306a36Sopenharmony_ci */ 205562306a36Sopenharmony_cistatic int self_check_in_pq(const struct ubi_device *ubi, 205662306a36Sopenharmony_ci struct ubi_wl_entry *e) 205762306a36Sopenharmony_ci{ 205862306a36Sopenharmony_ci if (!ubi_dbg_chk_gen(ubi)) 205962306a36Sopenharmony_ci return 0; 206062306a36Sopenharmony_ci 206162306a36Sopenharmony_ci if (in_pq(ubi, e)) 206262306a36Sopenharmony_ci return 0; 206362306a36Sopenharmony_ci 206462306a36Sopenharmony_ci ubi_err(ubi, "self-check failed for PEB %d, EC %d, Protect queue", 206562306a36Sopenharmony_ci e->pnum, e->ec); 206662306a36Sopenharmony_ci dump_stack(); 206762306a36Sopenharmony_ci return -EINVAL; 206862306a36Sopenharmony_ci} 206962306a36Sopenharmony_ci#ifndef CONFIG_MTD_UBI_FASTMAP 207062306a36Sopenharmony_cistatic struct ubi_wl_entry *get_peb_for_wl(struct ubi_device *ubi) 207162306a36Sopenharmony_ci{ 207262306a36Sopenharmony_ci struct ubi_wl_entry *e; 207362306a36Sopenharmony_ci 207462306a36Sopenharmony_ci e = find_wl_entry(ubi, &ubi->free, WL_FREE_MAX_DIFF); 207562306a36Sopenharmony_ci self_check_in_wl_tree(ubi, e, &ubi->free); 207662306a36Sopenharmony_ci ubi->free_count--; 207762306a36Sopenharmony_ci ubi_assert(ubi->free_count >= 0); 207862306a36Sopenharmony_ci rb_erase(&e->u.rb, &ubi->free); 207962306a36Sopenharmony_ci 208062306a36Sopenharmony_ci return e; 208162306a36Sopenharmony_ci} 208262306a36Sopenharmony_ci 208362306a36Sopenharmony_ci/** 208462306a36Sopenharmony_ci * produce_free_peb - produce a free physical eraseblock. 208562306a36Sopenharmony_ci * @ubi: UBI device description object 208662306a36Sopenharmony_ci * 208762306a36Sopenharmony_ci * This function tries to make a free PEB by means of synchronous execution of 208862306a36Sopenharmony_ci * pending works. This may be needed if, for example the background thread is 208962306a36Sopenharmony_ci * disabled. Returns zero in case of success and a negative error code in case 209062306a36Sopenharmony_ci * of failure. 209162306a36Sopenharmony_ci */ 209262306a36Sopenharmony_cistatic int produce_free_peb(struct ubi_device *ubi) 209362306a36Sopenharmony_ci{ 209462306a36Sopenharmony_ci int err; 209562306a36Sopenharmony_ci 209662306a36Sopenharmony_ci while (!ubi->free.rb_node && ubi->works_count) { 209762306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 209862306a36Sopenharmony_ci 209962306a36Sopenharmony_ci dbg_wl("do one work synchronously"); 210062306a36Sopenharmony_ci err = do_work(ubi); 210162306a36Sopenharmony_ci 210262306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 210362306a36Sopenharmony_ci if (err) 210462306a36Sopenharmony_ci return err; 210562306a36Sopenharmony_ci } 210662306a36Sopenharmony_ci 210762306a36Sopenharmony_ci return 0; 210862306a36Sopenharmony_ci} 210962306a36Sopenharmony_ci 211062306a36Sopenharmony_ci/** 211162306a36Sopenharmony_ci * ubi_wl_get_peb - get a physical eraseblock. 211262306a36Sopenharmony_ci * @ubi: UBI device description object 211362306a36Sopenharmony_ci * 211462306a36Sopenharmony_ci * This function returns a physical eraseblock in case of success and a 211562306a36Sopenharmony_ci * negative error code in case of failure. 211662306a36Sopenharmony_ci * Returns with ubi->fm_eba_sem held in read mode! 211762306a36Sopenharmony_ci */ 211862306a36Sopenharmony_ciint ubi_wl_get_peb(struct ubi_device *ubi) 211962306a36Sopenharmony_ci{ 212062306a36Sopenharmony_ci int err; 212162306a36Sopenharmony_ci struct ubi_wl_entry *e; 212262306a36Sopenharmony_ci 212362306a36Sopenharmony_ciretry: 212462306a36Sopenharmony_ci down_read(&ubi->fm_eba_sem); 212562306a36Sopenharmony_ci spin_lock(&ubi->wl_lock); 212662306a36Sopenharmony_ci if (!ubi->free.rb_node) { 212762306a36Sopenharmony_ci if (ubi->works_count == 0) { 212862306a36Sopenharmony_ci ubi_err(ubi, "no free eraseblocks"); 212962306a36Sopenharmony_ci ubi_assert(list_empty(&ubi->works)); 213062306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 213162306a36Sopenharmony_ci return -ENOSPC; 213262306a36Sopenharmony_ci } 213362306a36Sopenharmony_ci 213462306a36Sopenharmony_ci err = produce_free_peb(ubi); 213562306a36Sopenharmony_ci if (err < 0) { 213662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 213762306a36Sopenharmony_ci return err; 213862306a36Sopenharmony_ci } 213962306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 214062306a36Sopenharmony_ci up_read(&ubi->fm_eba_sem); 214162306a36Sopenharmony_ci goto retry; 214262306a36Sopenharmony_ci 214362306a36Sopenharmony_ci } 214462306a36Sopenharmony_ci e = wl_get_wle(ubi); 214562306a36Sopenharmony_ci prot_queue_add(ubi, e); 214662306a36Sopenharmony_ci spin_unlock(&ubi->wl_lock); 214762306a36Sopenharmony_ci 214862306a36Sopenharmony_ci err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset, 214962306a36Sopenharmony_ci ubi->peb_size - ubi->vid_hdr_aloffset); 215062306a36Sopenharmony_ci if (err) { 215162306a36Sopenharmony_ci ubi_err(ubi, "new PEB %d does not contain all 0xFF bytes", e->pnum); 215262306a36Sopenharmony_ci return err; 215362306a36Sopenharmony_ci } 215462306a36Sopenharmony_ci 215562306a36Sopenharmony_ci return e->pnum; 215662306a36Sopenharmony_ci} 215762306a36Sopenharmony_ci#else 215862306a36Sopenharmony_ci#include "fastmap-wl.c" 215962306a36Sopenharmony_ci#endif 2160