Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1 | /* |
Artem Bityutskiy | d99383b | 2011-05-18 14:47:34 +0300 | [diff] [blame] | 2 | * @ubi: UBI device description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 3 | * Copyright (c) International Business Machines Corp., 2006 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * Authors: Artem Bityutskiy (Битюцкий Артём), Thomas Gleixner |
| 20 | */ |
| 21 | |
| 22 | /* |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 23 | * UBI wear-leveling sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 24 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 25 | * This sub-system is responsible for wear-leveling. It works in terms of |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 26 | * physical eraseblocks and erase counters and knows nothing about logical |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 27 | * eraseblocks, volumes, etc. From this sub-system's perspective all physical |
| 28 | * eraseblocks are of two types - used and free. Used physical eraseblocks are |
| 29 | * those that were "get" by the 'ubi_wl_get_peb()' function, and free physical |
| 30 | * eraseblocks are those that were put by the 'ubi_wl_put_peb()' function. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 31 | * |
| 32 | * Physical eraseblocks returned by 'ubi_wl_get_peb()' have only erase counter |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 33 | * header. The rest of the physical eraseblock contains only %0xFF bytes. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 34 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 35 | * When physical eraseblocks are returned to the WL sub-system by means of the |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 36 | * 'ubi_wl_put_peb()' function, they are scheduled for erasure. The erasure is |
| 37 | * done asynchronously in context of the per-UBI device background thread, |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 38 | * which is also managed by the WL sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 39 | * |
| 40 | * The wear-leveling is ensured by means of moving the contents of used |
| 41 | * physical eraseblocks with low erase counter to free physical eraseblocks |
| 42 | * with high erase counter. |
| 43 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 44 | * If the WL sub-system fails to erase a physical eraseblock, it marks it as |
| 45 | * bad. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 46 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 47 | * This sub-system is also responsible for scrubbing. If a bit-flip is detected |
| 48 | * in a physical eraseblock, it has to be moved. Technically this is the same |
| 49 | * as moving it for wear-leveling reasons. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 50 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 51 | * As it was said, for the UBI sub-system all physical eraseblocks are either |
| 52 | * "free" or "used". Free eraseblock are kept in the @wl->free RB-tree, while |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 53 | * used eraseblocks are kept in @wl->used, @wl->erroneous, or @wl->scrub |
| 54 | * RB-trees, as well as (temporarily) in the @wl->pq queue. |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 55 | * |
| 56 | * When the WL sub-system returns a physical eraseblock, the physical |
| 57 | * eraseblock is protected from being moved for some "time". For this reason, |
| 58 | * the physical eraseblock is not directly moved from the @wl->free tree to the |
| 59 | * @wl->used tree. There is a protection queue in between where this |
| 60 | * physical eraseblock is temporarily stored (@wl->pq). |
| 61 | * |
| 62 | * All this protection stuff is needed because: |
| 63 | * o we don't want to move physical eraseblocks just after we have given them |
| 64 | * to the user; instead, we first want to let users fill them up with data; |
| 65 | * |
| 66 | * o there is a chance that the user will put the physical eraseblock very |
Artem Bityutskiy | 4415626 | 2012-05-14 19:49:35 +0300 | [diff] [blame] | 67 | * soon, so it makes sense not to move it for some time, but wait. |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 68 | * |
| 69 | * Physical eraseblocks stay protected only for limited time. But the "time" is |
| 70 | * measured in erase cycles in this case. This is implemented with help of the |
| 71 | * protection queue. Eraseblocks are put to the tail of this queue when they |
| 72 | * are returned by the 'ubi_wl_get_peb()', and eraseblocks are removed from the |
| 73 | * head of the queue on each erase operation (for any eraseblock). So the |
| 74 | * length of the queue defines how may (global) erase cycles PEBs are protected. |
| 75 | * |
| 76 | * To put it differently, each physical eraseblock has 2 main states: free and |
| 77 | * used. The former state corresponds to the @wl->free tree. The latter state |
| 78 | * is split up on several sub-states: |
| 79 | * o the WL movement is allowed (@wl->used tree); |
Artem Bityutskiy | 815bc5f8f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 80 | * o the WL movement is disallowed (@wl->erroneous) because the PEB is |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 81 | * erroneous - e.g., there was a read error; |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 82 | * o the WL movement is temporarily prohibited (@wl->pq queue); |
| 83 | * o scrubbing is needed (@wl->scrub tree). |
| 84 | * |
| 85 | * Depending on the sub-state, wear-leveling entries of the used physical |
| 86 | * eraseblocks may be kept in one of those structures. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 87 | * |
| 88 | * Note, in this implementation, we keep a small in-RAM object for each physical |
| 89 | * eraseblock. This is surely not a scalable solution. But it appears to be good |
| 90 | * enough for moderately large flashes and it is simple. In future, one may |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 91 | * re-work this sub-system and make it more scalable. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 92 | * |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 93 | * At the moment this sub-system does not utilize the sequence number, which |
| 94 | * was introduced relatively recently. But it would be wise to do this because |
| 95 | * the sequence number of a logical eraseblock characterizes how old is it. For |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 96 | * example, when we move a PEB with low erase counter, and we need to pick the |
| 97 | * target PEB, we pick a PEB with the highest EC if our PEB is "old" and we |
| 98 | * pick target PEB with an average EC if our PEB is not very "old". This is a |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 99 | * room for future re-works of the WL sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 100 | */ |
| 101 | |
| 102 | #include <linux/slab.h> |
| 103 | #include <linux/crc32.h> |
| 104 | #include <linux/freezer.h> |
| 105 | #include <linux/kthread.h> |
| 106 | #include "ubi.h" |
| 107 | |
| 108 | /* Number of physical eraseblocks reserved for wear-leveling purposes */ |
| 109 | #define WL_RESERVED_PEBS 1 |
| 110 | |
| 111 | /* |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 112 | * Maximum difference between two erase counters. If this threshold is |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 113 | * exceeded, the WL sub-system starts moving data from used physical |
| 114 | * eraseblocks with low erase counter to free physical eraseblocks with high |
| 115 | * erase counter. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 116 | */ |
| 117 | #define UBI_WL_THRESHOLD CONFIG_MTD_UBI_WL_THRESHOLD |
| 118 | |
| 119 | /* |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 120 | * When a physical eraseblock is moved, the WL sub-system has to pick the target |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 121 | * physical eraseblock to move to. The simplest way would be just to pick the |
| 122 | * one with the highest erase counter. But in certain workloads this could lead |
| 123 | * to an unlimited wear of one or few physical eraseblock. Indeed, imagine a |
| 124 | * situation when the picked physical eraseblock is constantly erased after the |
| 125 | * data is written to it. So, we have a constant which limits the highest erase |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 126 | * counter of the free physical eraseblock to pick. Namely, the WL sub-system |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 127 | * does not pick eraseblocks with erase counter greater than the lowest erase |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 128 | * counter plus %WL_FREE_MAX_DIFF. |
| 129 | */ |
| 130 | #define WL_FREE_MAX_DIFF (2*UBI_WL_THRESHOLD) |
| 131 | |
| 132 | /* |
| 133 | * Maximum number of consecutive background thread failures which is enough to |
| 134 | * switch to read-only mode. |
| 135 | */ |
| 136 | #define WL_MAX_FAILURES 32 |
| 137 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 138 | static int self_check_ec(struct ubi_device *ubi, int pnum, int ec); |
| 139 | static int self_check_in_wl_tree(const struct ubi_device *ubi, |
| 140 | struct ubi_wl_entry *e, struct rb_root *root); |
| 141 | static int self_check_in_pq(const struct ubi_device *ubi, |
| 142 | struct ubi_wl_entry *e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 143 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 144 | /** |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 145 | * wl_tree_add - add a wear-leveling entry to a WL RB-tree. |
| 146 | * @e: the wear-leveling entry to add |
| 147 | * @root: the root of the tree |
| 148 | * |
| 149 | * Note, we use (erase counter, physical eraseblock number) pairs as keys in |
| 150 | * the @ubi->used and @ubi->free RB-trees. |
| 151 | */ |
| 152 | static void wl_tree_add(struct ubi_wl_entry *e, struct rb_root *root) |
| 153 | { |
| 154 | struct rb_node **p, *parent = NULL; |
| 155 | |
| 156 | p = &root->rb_node; |
| 157 | while (*p) { |
| 158 | struct ubi_wl_entry *e1; |
| 159 | |
| 160 | parent = *p; |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 161 | e1 = rb_entry(parent, struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 162 | |
| 163 | if (e->ec < e1->ec) |
| 164 | p = &(*p)->rb_left; |
| 165 | else if (e->ec > e1->ec) |
| 166 | p = &(*p)->rb_right; |
| 167 | else { |
| 168 | ubi_assert(e->pnum != e1->pnum); |
| 169 | if (e->pnum < e1->pnum) |
| 170 | p = &(*p)->rb_left; |
| 171 | else |
| 172 | p = &(*p)->rb_right; |
| 173 | } |
| 174 | } |
| 175 | |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 176 | rb_link_node(&e->u.rb, parent, p); |
| 177 | rb_insert_color(&e->u.rb, root); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 178 | } |
| 179 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 180 | /** |
| 181 | * do_work - do one pending work. |
| 182 | * @ubi: UBI device description object |
| 183 | * |
| 184 | * This function returns zero in case of success and a negative error code in |
| 185 | * case of failure. |
| 186 | */ |
| 187 | static int do_work(struct ubi_device *ubi) |
| 188 | { |
| 189 | int err; |
| 190 | struct ubi_work *wrk; |
| 191 | |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 192 | cond_resched(); |
| 193 | |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 194 | /* |
| 195 | * @ubi->work_sem is used to synchronize with the workers. Workers take |
| 196 | * it in read mode, so many of them may be doing works at a time. But |
| 197 | * the queue flush code has to be sure the whole queue of works is |
| 198 | * done, and it takes the mutex in write mode. |
| 199 | */ |
| 200 | down_read(&ubi->work_sem); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 201 | spin_lock(&ubi->wl_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 202 | if (list_empty(&ubi->works)) { |
| 203 | spin_unlock(&ubi->wl_lock); |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 204 | up_read(&ubi->work_sem); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | wrk = list_entry(ubi->works.next, struct ubi_work, list); |
| 209 | list_del(&wrk->list); |
Artem Bityutskiy | 16f557e | 2007-12-19 16:03:17 +0200 | [diff] [blame] | 210 | ubi->works_count -= 1; |
| 211 | ubi_assert(ubi->works_count >= 0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 212 | spin_unlock(&ubi->wl_lock); |
| 213 | |
| 214 | /* |
| 215 | * Call the worker function. Do not touch the work structure |
| 216 | * after this call as it will have been freed or reused by that |
| 217 | * time by the worker function. |
| 218 | */ |
| 219 | err = wrk->func(ubi, wrk, 0); |
| 220 | if (err) |
| 221 | ubi_err("work failed with error code %d", err); |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 222 | up_read(&ubi->work_sem); |
Artem Bityutskiy | 16f557e | 2007-12-19 16:03:17 +0200 | [diff] [blame] | 223 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 224 | return err; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * produce_free_peb - produce a free physical eraseblock. |
| 229 | * @ubi: UBI device description object |
| 230 | * |
| 231 | * This function tries to make a free PEB by means of synchronous execution of |
| 232 | * pending works. This may be needed if, for example the background thread is |
| 233 | * disabled. Returns zero in case of success and a negative error code in case |
| 234 | * of failure. |
| 235 | */ |
| 236 | static int produce_free_peb(struct ubi_device *ubi) |
| 237 | { |
| 238 | int err; |
| 239 | |
| 240 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 241 | while (!ubi->free.rb_node) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 242 | spin_unlock(&ubi->wl_lock); |
| 243 | |
| 244 | dbg_wl("do one work synchronously"); |
| 245 | err = do_work(ubi); |
| 246 | if (err) |
| 247 | return err; |
| 248 | |
| 249 | spin_lock(&ubi->wl_lock); |
| 250 | } |
| 251 | spin_unlock(&ubi->wl_lock); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * in_wl_tree - check if wear-leveling entry is present in a WL RB-tree. |
| 258 | * @e: the wear-leveling entry to check |
| 259 | * @root: the root of the tree |
| 260 | * |
| 261 | * This function returns non-zero if @e is in the @root RB-tree and zero if it |
| 262 | * is not. |
| 263 | */ |
| 264 | static int in_wl_tree(struct ubi_wl_entry *e, struct rb_root *root) |
| 265 | { |
| 266 | struct rb_node *p; |
| 267 | |
| 268 | p = root->rb_node; |
| 269 | while (p) { |
| 270 | struct ubi_wl_entry *e1; |
| 271 | |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 272 | e1 = rb_entry(p, struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 273 | |
| 274 | if (e->pnum == e1->pnum) { |
| 275 | ubi_assert(e == e1); |
| 276 | return 1; |
| 277 | } |
| 278 | |
| 279 | if (e->ec < e1->ec) |
| 280 | p = p->rb_left; |
| 281 | else if (e->ec > e1->ec) |
| 282 | p = p->rb_right; |
| 283 | else { |
| 284 | ubi_assert(e->pnum != e1->pnum); |
| 285 | if (e->pnum < e1->pnum) |
| 286 | p = p->rb_left; |
| 287 | else |
| 288 | p = p->rb_right; |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | /** |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 296 | * prot_queue_add - add physical eraseblock to the protection queue. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 297 | * @ubi: UBI device description object |
| 298 | * @e: the physical eraseblock to add |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 299 | * |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 300 | * This function adds @e to the tail of the protection queue @ubi->pq, where |
| 301 | * @e will stay for %UBI_PROT_QUEUE_LEN erase operations and will be |
| 302 | * temporarily protected from the wear-leveling worker. Note, @wl->lock has to |
| 303 | * be locked. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 304 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 305 | static void prot_queue_add(struct ubi_device *ubi, struct ubi_wl_entry *e) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 306 | { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 307 | int pq_tail = ubi->pq_head - 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 308 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 309 | if (pq_tail < 0) |
| 310 | pq_tail = UBI_PROT_QUEUE_LEN - 1; |
| 311 | ubi_assert(pq_tail >= 0 && pq_tail < UBI_PROT_QUEUE_LEN); |
| 312 | list_add_tail(&e->u.list, &ubi->pq[pq_tail]); |
| 313 | dbg_wl("added PEB %d EC %d to the protection queue", e->pnum, e->ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | /** |
| 317 | * find_wl_entry - find wear-leveling entry closest to certain erase counter. |
| 318 | * @root: the RB-tree where to look for |
Artem Bityutskiy | add8287 | 2012-03-07 18:56:29 +0200 | [diff] [blame] | 319 | * @diff: maximum possible difference from the smallest erase counter |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 320 | * |
| 321 | * This function looks for a wear leveling entry with erase counter closest to |
Artem Bityutskiy | add8287 | 2012-03-07 18:56:29 +0200 | [diff] [blame] | 322 | * min + @diff, where min is the smallest erase counter. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 323 | */ |
Artem Bityutskiy | add8287 | 2012-03-07 18:56:29 +0200 | [diff] [blame] | 324 | static struct ubi_wl_entry *find_wl_entry(struct rb_root *root, int diff) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 325 | { |
| 326 | struct rb_node *p; |
| 327 | struct ubi_wl_entry *e; |
Artem Bityutskiy | add8287 | 2012-03-07 18:56:29 +0200 | [diff] [blame] | 328 | int max; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 329 | |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 330 | e = rb_entry(rb_first(root), struct ubi_wl_entry, u.rb); |
Artem Bityutskiy | add8287 | 2012-03-07 18:56:29 +0200 | [diff] [blame] | 331 | max = e->ec + diff; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 332 | |
| 333 | p = root->rb_node; |
| 334 | while (p) { |
| 335 | struct ubi_wl_entry *e1; |
| 336 | |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 337 | e1 = rb_entry(p, struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 338 | if (e1->ec >= max) |
| 339 | p = p->rb_left; |
| 340 | else { |
| 341 | p = p->rb_right; |
| 342 | e = e1; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | return e; |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * ubi_wl_get_peb - get a physical eraseblock. |
| 351 | * @ubi: UBI device description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 352 | * |
| 353 | * This function returns a physical eraseblock in case of success and a |
| 354 | * negative error code in case of failure. Might sleep. |
| 355 | */ |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 356 | int ubi_wl_get_peb(struct ubi_device *ubi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 357 | { |
Artem Bityutskiy | 7eb3aa65 | 2012-03-07 19:08:36 +0200 | [diff] [blame] | 358 | int err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 359 | struct ubi_wl_entry *e, *first, *last; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 360 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 361 | retry: |
| 362 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 363 | if (!ubi->free.rb_node) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 364 | if (ubi->works_count == 0) { |
| 365 | ubi_assert(list_empty(&ubi->works)); |
| 366 | ubi_err("no free eraseblocks"); |
| 367 | spin_unlock(&ubi->wl_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 368 | return -ENOSPC; |
| 369 | } |
| 370 | spin_unlock(&ubi->wl_lock); |
| 371 | |
| 372 | err = produce_free_peb(ubi); |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 373 | if (err < 0) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 374 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 375 | goto retry; |
| 376 | } |
| 377 | |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 378 | first = rb_entry(rb_first(&ubi->free), struct ubi_wl_entry, u.rb); |
| 379 | last = rb_entry(rb_last(&ubi->free), struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 380 | |
Richard Weinberger | b36a261 | 2012-05-14 17:55:51 +0200 | [diff] [blame] | 381 | if (last->ec - first->ec < WL_FREE_MAX_DIFF) |
| 382 | e = rb_entry(ubi->free.rb_node, struct ubi_wl_entry, u.rb); |
| 383 | else |
| 384 | e = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF/2); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 385 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 386 | self_check_in_wl_tree(ubi, e, &ubi->free); |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 387 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 388 | /* |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 389 | * Move the physical eraseblock to the protection queue where it will |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 390 | * be protected from being moved for some time. |
| 391 | */ |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 392 | rb_erase(&e->u.rb, &ubi->free); |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 393 | dbg_wl("PEB %d EC %d", e->pnum, e->ec); |
| 394 | prot_queue_add(ubi, e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 395 | spin_unlock(&ubi->wl_lock); |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 396 | |
Artem Bityutskiy | 97d6104 | 2012-05-16 19:29:04 +0300 | [diff] [blame] | 397 | err = ubi_self_check_all_ff(ubi, e->pnum, ubi->vid_hdr_aloffset, |
| 398 | ubi->peb_size - ubi->vid_hdr_aloffset); |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 399 | if (err) { |
Artem Bityutskiy | 1398788 | 2009-06-29 15:58:36 +0300 | [diff] [blame] | 400 | ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 401 | return err; |
Artem Bityutskiy | 40a71a8 | 2009-06-28 19:16:55 +0300 | [diff] [blame] | 402 | } |
| 403 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 404 | return e->pnum; |
| 405 | } |
| 406 | |
| 407 | /** |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 408 | * prot_queue_del - remove a physical eraseblock from the protection queue. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 409 | * @ubi: UBI device description object |
| 410 | * @pnum: the physical eraseblock to remove |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 411 | * |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 412 | * This function deletes PEB @pnum from the protection queue and returns zero |
| 413 | * in case of success and %-ENODEV if the PEB was not found. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 414 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 415 | static int prot_queue_del(struct ubi_device *ubi, int pnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 416 | { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 417 | struct ubi_wl_entry *e; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 418 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 419 | e = ubi->lookuptbl[pnum]; |
| 420 | if (!e) |
| 421 | return -ENODEV; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 422 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 423 | if (self_check_in_pq(ubi, e)) |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 424 | return -ENODEV; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 425 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 426 | list_del(&e->u.list); |
| 427 | dbg_wl("deleted PEB %d from the protection queue", e->pnum); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 428 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /** |
| 432 | * sync_erase - synchronously erase a physical eraseblock. |
| 433 | * @ubi: UBI device description object |
| 434 | * @e: the the physical eraseblock to erase |
| 435 | * @torture: if the physical eraseblock has to be tortured |
| 436 | * |
| 437 | * This function returns zero in case of success and a negative error code in |
| 438 | * case of failure. |
| 439 | */ |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 440 | static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, |
| 441 | int torture) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 442 | { |
| 443 | int err; |
| 444 | struct ubi_ec_hdr *ec_hdr; |
| 445 | unsigned long long ec = e->ec; |
| 446 | |
| 447 | dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec); |
| 448 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 449 | err = self_check_ec(ubi, e->pnum, e->ec); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 450 | if (err) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 451 | return -EINVAL; |
| 452 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 453 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 454 | if (!ec_hdr) |
| 455 | return -ENOMEM; |
| 456 | |
| 457 | err = ubi_io_sync_erase(ubi, e->pnum, torture); |
| 458 | if (err < 0) |
| 459 | goto out_free; |
| 460 | |
| 461 | ec += err; |
| 462 | if (ec > UBI_MAX_ERASECOUNTER) { |
| 463 | /* |
| 464 | * Erase counter overflow. Upgrade UBI and use 64-bit |
| 465 | * erase counters internally. |
| 466 | */ |
| 467 | ubi_err("erase counter overflow at PEB %d, EC %llu", |
| 468 | e->pnum, ec); |
| 469 | err = -EINVAL; |
| 470 | goto out_free; |
| 471 | } |
| 472 | |
| 473 | dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec); |
| 474 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 475 | ec_hdr->ec = cpu_to_be64(ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 476 | |
| 477 | err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); |
| 478 | if (err) |
| 479 | goto out_free; |
| 480 | |
| 481 | e->ec = ec; |
| 482 | spin_lock(&ubi->wl_lock); |
| 483 | if (e->ec > ubi->max_ec) |
| 484 | ubi->max_ec = e->ec; |
| 485 | spin_unlock(&ubi->wl_lock); |
| 486 | |
| 487 | out_free: |
| 488 | kfree(ec_hdr); |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | /** |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 493 | * serve_prot_queue - check if it is time to stop protecting PEBs. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 494 | * @ubi: UBI device description object |
| 495 | * |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 496 | * This function is called after each erase operation and removes PEBs from the |
| 497 | * tail of the protection queue. These PEBs have been protected for long enough |
| 498 | * and should be moved to the used tree. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 499 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 500 | static void serve_prot_queue(struct ubi_device *ubi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 501 | { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 502 | struct ubi_wl_entry *e, *tmp; |
| 503 | int count; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 504 | |
| 505 | /* |
| 506 | * There may be several protected physical eraseblock to remove, |
| 507 | * process them all. |
| 508 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 509 | repeat: |
| 510 | count = 0; |
| 511 | spin_lock(&ubi->wl_lock); |
| 512 | list_for_each_entry_safe(e, tmp, &ubi->pq[ubi->pq_head], u.list) { |
| 513 | dbg_wl("PEB %d EC %d protection over, move to used tree", |
| 514 | e->pnum, e->ec); |
| 515 | |
| 516 | list_del(&e->u.list); |
| 517 | wl_tree_add(e, &ubi->used); |
| 518 | if (count++ > 32) { |
| 519 | /* |
| 520 | * Let's be nice and avoid holding the spinlock for |
| 521 | * too long. |
| 522 | */ |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 523 | spin_unlock(&ubi->wl_lock); |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 524 | cond_resched(); |
| 525 | goto repeat; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 526 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 527 | } |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 528 | |
| 529 | ubi->pq_head += 1; |
| 530 | if (ubi->pq_head == UBI_PROT_QUEUE_LEN) |
| 531 | ubi->pq_head = 0; |
| 532 | ubi_assert(ubi->pq_head >= 0 && ubi->pq_head < UBI_PROT_QUEUE_LEN); |
| 533 | spin_unlock(&ubi->wl_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | /** |
| 537 | * schedule_ubi_work - schedule a work. |
| 538 | * @ubi: UBI device description object |
| 539 | * @wrk: the work to schedule |
| 540 | * |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 541 | * This function adds a work defined by @wrk to the tail of the pending works |
| 542 | * list. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 543 | */ |
| 544 | static void schedule_ubi_work(struct ubi_device *ubi, struct ubi_work *wrk) |
| 545 | { |
| 546 | spin_lock(&ubi->wl_lock); |
| 547 | list_add_tail(&wrk->list, &ubi->works); |
| 548 | ubi_assert(ubi->works_count >= 0); |
| 549 | ubi->works_count += 1; |
Artem Bityutskiy | 27a0f2a | 2011-05-18 16:03:23 +0300 | [diff] [blame] | 550 | if (ubi->thread_enabled && !ubi_dbg_is_bgt_disabled(ubi)) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 551 | wake_up_process(ubi->bgt_thread); |
| 552 | spin_unlock(&ubi->wl_lock); |
| 553 | } |
| 554 | |
| 555 | static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, |
| 556 | int cancel); |
| 557 | |
| 558 | /** |
| 559 | * schedule_erase - schedule an erase work. |
| 560 | * @ubi: UBI device description object |
| 561 | * @e: the WL entry of the physical eraseblock to erase |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 562 | * @vol_id: the volume ID that last used this PEB |
| 563 | * @lnum: the last used logical eraseblock number for the PEB |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 564 | * @torture: if the physical eraseblock has to be tortured |
| 565 | * |
| 566 | * This function returns zero in case of success and a %-ENOMEM in case of |
| 567 | * failure. |
| 568 | */ |
| 569 | static int schedule_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 570 | int vol_id, int lnum, int torture) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 571 | { |
| 572 | struct ubi_work *wl_wrk; |
| 573 | |
| 574 | dbg_wl("schedule erasure of PEB %d, EC %d, torture %d", |
| 575 | e->pnum, e->ec, torture); |
| 576 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 577 | wl_wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 578 | if (!wl_wrk) |
| 579 | return -ENOMEM; |
| 580 | |
| 581 | wl_wrk->func = &erase_worker; |
| 582 | wl_wrk->e = e; |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 583 | wl_wrk->vol_id = vol_id; |
| 584 | wl_wrk->lnum = lnum; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 585 | wl_wrk->torture = torture; |
| 586 | |
| 587 | schedule_ubi_work(ubi, wl_wrk); |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * wear_leveling_worker - wear-leveling worker function. |
| 593 | * @ubi: UBI device description object |
| 594 | * @wrk: the work object |
| 595 | * @cancel: non-zero if the worker has to free memory and exit |
| 596 | * |
| 597 | * This function copies a more worn out physical eraseblock to a less worn out |
| 598 | * one. Returns zero in case of success and a negative error code in case of |
| 599 | * failure. |
| 600 | */ |
| 601 | static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk, |
| 602 | int cancel) |
| 603 | { |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 604 | int err, scrubbing = 0, torture = 0, protect = 0, erroneous = 0; |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 605 | int vol_id = -1, uninitialized_var(lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 606 | struct ubi_wl_entry *e1, *e2; |
| 607 | struct ubi_vid_hdr *vid_hdr; |
| 608 | |
| 609 | kfree(wrk); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 610 | if (cancel) |
| 611 | return 0; |
| 612 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 613 | vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 614 | if (!vid_hdr) |
| 615 | return -ENOMEM; |
| 616 | |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 617 | mutex_lock(&ubi->move_mutex); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 618 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 619 | ubi_assert(!ubi->move_from && !ubi->move_to); |
| 620 | ubi_assert(!ubi->move_to_put); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 621 | |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 622 | if (!ubi->free.rb_node || |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 623 | (!ubi->used.rb_node && !ubi->scrub.rb_node)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 624 | /* |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 625 | * No free physical eraseblocks? Well, they must be waiting in |
| 626 | * the queue to be erased. Cancel movement - it will be |
| 627 | * triggered again when a free physical eraseblock appears. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 628 | * |
| 629 | * No used physical eraseblocks? They must be temporarily |
| 630 | * protected from being moved. They will be moved to the |
| 631 | * @ubi->used tree later and the wear-leveling will be |
| 632 | * triggered again. |
| 633 | */ |
| 634 | dbg_wl("cancel WL, a list is empty: free %d, used %d", |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 635 | !ubi->free.rb_node, !ubi->used.rb_node); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 636 | goto out_cancel; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 637 | } |
| 638 | |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 639 | if (!ubi->scrub.rb_node) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 640 | /* |
| 641 | * Now pick the least worn-out used physical eraseblock and a |
| 642 | * highly worn-out free physical eraseblock. If the erase |
| 643 | * counters differ much enough, start wear-leveling. |
| 644 | */ |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 645 | e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 646 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); |
| 647 | |
| 648 | if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) { |
| 649 | dbg_wl("no WL needed: min used EC %d, max free EC %d", |
| 650 | e1->ec, e2->ec); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 651 | goto out_cancel; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 652 | } |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 653 | self_check_in_wl_tree(ubi, e1, &ubi->used); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 654 | rb_erase(&e1->u.rb, &ubi->used); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 655 | dbg_wl("move PEB %d EC %d to PEB %d EC %d", |
| 656 | e1->pnum, e1->ec, e2->pnum, e2->ec); |
| 657 | } else { |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 658 | /* Perform scrubbing */ |
| 659 | scrubbing = 1; |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 660 | e1 = rb_entry(rb_first(&ubi->scrub), struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 661 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 662 | self_check_in_wl_tree(ubi, e1, &ubi->scrub); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 663 | rb_erase(&e1->u.rb, &ubi->scrub); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 664 | dbg_wl("scrub PEB %d to PEB %d", e1->pnum, e2->pnum); |
| 665 | } |
| 666 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 667 | self_check_in_wl_tree(ubi, e2, &ubi->free); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 668 | rb_erase(&e2->u.rb, &ubi->free); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 669 | ubi->move_from = e1; |
| 670 | ubi->move_to = e2; |
| 671 | spin_unlock(&ubi->wl_lock); |
| 672 | |
| 673 | /* |
| 674 | * Now we are going to copy physical eraseblock @e1->pnum to @e2->pnum. |
| 675 | * We so far do not know which logical eraseblock our physical |
| 676 | * eraseblock (@e1) belongs to. We have to read the volume identifier |
| 677 | * header first. |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 678 | * |
| 679 | * Note, we are protected from this PEB being unmapped and erased. The |
| 680 | * 'ubi_wl_put_peb()' would wait for moving to be finished if the PEB |
| 681 | * which is being moved was unmapped. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 682 | */ |
| 683 | |
| 684 | err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0); |
| 685 | if (err && err != UBI_IO_BITFLIPS) { |
Artem Bityutskiy | 74d82d2 | 2010-09-03 02:11:20 +0300 | [diff] [blame] | 686 | if (err == UBI_IO_FF) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 687 | /* |
| 688 | * We are trying to move PEB without a VID header. UBI |
| 689 | * always write VID headers shortly after the PEB was |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 690 | * given, so we have a situation when it has not yet |
| 691 | * had a chance to write it, because it was preempted. |
| 692 | * So add this PEB to the protection queue so far, |
Artem Bityutskiy | 815bc5f8f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 693 | * because presumably more data will be written there |
| 694 | * (including the missing VID header), and then we'll |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 695 | * move it. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 696 | */ |
| 697 | dbg_wl("PEB %d has no VID header", e1->pnum); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 698 | protect = 1; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 699 | goto out_not_moved; |
Artem Bityutskiy | 92e1a7d | 2010-09-03 14:22:17 +0300 | [diff] [blame] | 700 | } else if (err == UBI_IO_FF_BITFLIPS) { |
| 701 | /* |
| 702 | * The same situation as %UBI_IO_FF, but bit-flips were |
| 703 | * detected. It is better to schedule this PEB for |
| 704 | * scrubbing. |
| 705 | */ |
| 706 | dbg_wl("PEB %d has no VID header but has bit-flips", |
| 707 | e1->pnum); |
| 708 | scrubbing = 1; |
| 709 | goto out_not_moved; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 710 | } |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 711 | |
| 712 | ubi_err("error %d while reading VID header from PEB %d", |
| 713 | err, e1->pnum); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 714 | goto out_error; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 715 | } |
| 716 | |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 717 | vol_id = be32_to_cpu(vid_hdr->vol_id); |
| 718 | lnum = be32_to_cpu(vid_hdr->lnum); |
| 719 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 720 | err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr); |
| 721 | if (err) { |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 722 | if (err == MOVE_CANCEL_RACE) { |
| 723 | /* |
| 724 | * The LEB has not been moved because the volume is |
| 725 | * being deleted or the PEB has been put meanwhile. We |
| 726 | * should prevent this PEB from being selected for |
| 727 | * wear-leveling movement again, so put it to the |
| 728 | * protection queue. |
| 729 | */ |
| 730 | protect = 1; |
| 731 | goto out_not_moved; |
| 732 | } |
Bhavesh Parekh | e801e12 | 2011-11-30 17:43:42 +0530 | [diff] [blame] | 733 | if (err == MOVE_RETRY) { |
| 734 | scrubbing = 1; |
| 735 | goto out_not_moved; |
| 736 | } |
Artem Bityutskiy | cc83146 | 2012-03-09 10:31:18 +0200 | [diff] [blame] | 737 | if (err == MOVE_TARGET_BITFLIPS || err == MOVE_TARGET_WR_ERR || |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 738 | err == MOVE_TARGET_RD_ERR) { |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 739 | /* |
| 740 | * Target PEB had bit-flips or write error - torture it. |
| 741 | */ |
Artem Bityutskiy | 6fa6f5b | 2008-12-05 13:37:02 +0200 | [diff] [blame] | 742 | torture = 1; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 743 | goto out_not_moved; |
Artem Bityutskiy | 6fa6f5b | 2008-12-05 13:37:02 +0200 | [diff] [blame] | 744 | } |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 745 | |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 746 | if (err == MOVE_SOURCE_RD_ERR) { |
| 747 | /* |
| 748 | * An error happened while reading the source PEB. Do |
| 749 | * not switch to R/O mode in this case, and give the |
| 750 | * upper layers a possibility to recover from this, |
| 751 | * e.g. by unmapping corresponding LEB. Instead, just |
Artem Bityutskiy | 815bc5f8f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 752 | * put this PEB to the @ubi->erroneous list to prevent |
| 753 | * UBI from trying to move it over and over again. |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 754 | */ |
| 755 | if (ubi->erroneous_peb_count > ubi->max_erroneous) { |
| 756 | ubi_err("too many erroneous eraseblocks (%d)", |
| 757 | ubi->erroneous_peb_count); |
| 758 | goto out_error; |
| 759 | } |
| 760 | erroneous = 1; |
| 761 | goto out_not_moved; |
| 762 | } |
| 763 | |
Artem Bityutskiy | 90bf026 | 2009-05-23 16:04:17 +0300 | [diff] [blame] | 764 | if (err < 0) |
| 765 | goto out_error; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 766 | |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 767 | ubi_assert(0); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 768 | } |
| 769 | |
Artem Bityutskiy | 6a8f483 | 2008-12-05 12:23:48 +0200 | [diff] [blame] | 770 | /* The PEB has been successfully moved */ |
Artem Bityutskiy | 6a8f483 | 2008-12-05 12:23:48 +0200 | [diff] [blame] | 771 | if (scrubbing) |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 772 | ubi_msg("scrubbed PEB %d (LEB %d:%d), data moved to PEB %d", |
| 773 | e1->pnum, vol_id, lnum, e2->pnum); |
| 774 | ubi_free_vid_hdr(ubi, vid_hdr); |
Artem Bityutskiy | 8c1e6ee | 2008-07-18 12:20:23 +0300 | [diff] [blame] | 775 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 776 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 3c98b0a | 2008-12-05 12:42:45 +0200 | [diff] [blame] | 777 | if (!ubi->move_to_put) { |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 778 | wl_tree_add(e2, &ubi->used); |
Artem Bityutskiy | 3c98b0a | 2008-12-05 12:42:45 +0200 | [diff] [blame] | 779 | e2 = NULL; |
| 780 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 781 | ubi->move_from = ubi->move_to = NULL; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 782 | ubi->move_to_put = ubi->wl_scheduled = 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 783 | spin_unlock(&ubi->wl_lock); |
| 784 | |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 785 | err = schedule_erase(ubi, e1, vol_id, lnum, 0); |
Artem Bityutskiy | 3c98b0a | 2008-12-05 12:42:45 +0200 | [diff] [blame] | 786 | if (err) { |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 787 | kmem_cache_free(ubi_wl_entry_slab, e1); |
Artem Bityutskiy | 21d08bb | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 788 | if (e2) |
| 789 | kmem_cache_free(ubi_wl_entry_slab, e2); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 790 | goto out_ro; |
Artem Bityutskiy | 3c98b0a | 2008-12-05 12:42:45 +0200 | [diff] [blame] | 791 | } |
Artem Bityutskiy | 6a8f483 | 2008-12-05 12:23:48 +0200 | [diff] [blame] | 792 | |
Artem Bityutskiy | 3c98b0a | 2008-12-05 12:42:45 +0200 | [diff] [blame] | 793 | if (e2) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 794 | /* |
| 795 | * Well, the target PEB was put meanwhile, schedule it for |
| 796 | * erasure. |
| 797 | */ |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 798 | dbg_wl("PEB %d (LEB %d:%d) was put meanwhile, erase", |
| 799 | e2->pnum, vol_id, lnum); |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 800 | err = schedule_erase(ubi, e2, vol_id, lnum, 0); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 801 | if (err) { |
| 802 | kmem_cache_free(ubi_wl_entry_slab, e2); |
| 803 | goto out_ro; |
| 804 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 805 | } |
| 806 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 807 | dbg_wl("done"); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 808 | mutex_unlock(&ubi->move_mutex); |
| 809 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 810 | |
| 811 | /* |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 812 | * For some reasons the LEB was not moved, might be an error, might be |
| 813 | * something else. @e1 was not changed, so return it back. @e2 might |
Artem Bityutskiy | 6fa6f5b | 2008-12-05 13:37:02 +0200 | [diff] [blame] | 814 | * have been changed, schedule it for erasure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 815 | */ |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 816 | out_not_moved: |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 817 | if (vol_id != -1) |
| 818 | dbg_wl("cancel moving PEB %d (LEB %d:%d) to PEB %d (%d)", |
| 819 | e1->pnum, vol_id, lnum, e2->pnum, err); |
| 820 | else |
| 821 | dbg_wl("cancel moving PEB %d to PEB %d (%d)", |
| 822 | e1->pnum, e2->pnum, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 823 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 824 | if (protect) |
| 825 | prot_queue_add(ubi, e1); |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 826 | else if (erroneous) { |
| 827 | wl_tree_add(e1, &ubi->erroneous); |
| 828 | ubi->erroneous_peb_count += 1; |
| 829 | } else if (scrubbing) |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 830 | wl_tree_add(e1, &ubi->scrub); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 831 | else |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 832 | wl_tree_add(e1, &ubi->used); |
Artem Bityutskiy | 6fa6f5b | 2008-12-05 13:37:02 +0200 | [diff] [blame] | 833 | ubi_assert(!ubi->move_to_put); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 834 | ubi->move_from = ubi->move_to = NULL; |
Artem Bityutskiy | 6fa6f5b | 2008-12-05 13:37:02 +0200 | [diff] [blame] | 835 | ubi->wl_scheduled = 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 836 | spin_unlock(&ubi->wl_lock); |
| 837 | |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 838 | ubi_free_vid_hdr(ubi, vid_hdr); |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 839 | err = schedule_erase(ubi, e2, vol_id, lnum, torture); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 840 | if (err) { |
| 841 | kmem_cache_free(ubi_wl_entry_slab, e2); |
| 842 | goto out_ro; |
| 843 | } |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 844 | mutex_unlock(&ubi->move_mutex); |
| 845 | return 0; |
| 846 | |
| 847 | out_error: |
Artem Bityutskiy | 9c259a5 | 2009-06-08 12:49:08 +0300 | [diff] [blame] | 848 | if (vol_id != -1) |
| 849 | ubi_err("error %d while moving PEB %d to PEB %d", |
| 850 | err, e1->pnum, e2->pnum); |
| 851 | else |
| 852 | ubi_err("error %d while moving PEB %d (LEB %d:%d) to PEB %d", |
| 853 | err, e1->pnum, vol_id, lnum, e2->pnum); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 854 | spin_lock(&ubi->wl_lock); |
| 855 | ubi->move_from = ubi->move_to = NULL; |
| 856 | ubi->move_to_put = ubi->wl_scheduled = 0; |
| 857 | spin_unlock(&ubi->wl_lock); |
| 858 | |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 859 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 860 | kmem_cache_free(ubi_wl_entry_slab, e1); |
| 861 | kmem_cache_free(ubi_wl_entry_slab, e2); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 862 | |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 863 | out_ro: |
| 864 | ubi_ro_mode(ubi); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 865 | mutex_unlock(&ubi->move_mutex); |
Artem Bityutskiy | 87960c0 | 2009-05-24 11:58:58 +0300 | [diff] [blame] | 866 | ubi_assert(err != 0); |
| 867 | return err < 0 ? err : -EIO; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 868 | |
| 869 | out_cancel: |
| 870 | ubi->wl_scheduled = 0; |
| 871 | spin_unlock(&ubi->wl_lock); |
| 872 | mutex_unlock(&ubi->move_mutex); |
| 873 | ubi_free_vid_hdr(ubi, vid_hdr); |
| 874 | return 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 875 | } |
| 876 | |
| 877 | /** |
| 878 | * ensure_wear_leveling - schedule wear-leveling if it is needed. |
| 879 | * @ubi: UBI device description object |
| 880 | * |
| 881 | * This function checks if it is time to start wear-leveling and schedules it |
| 882 | * if yes. This function returns zero in case of success and a negative error |
| 883 | * code in case of failure. |
| 884 | */ |
| 885 | static int ensure_wear_leveling(struct ubi_device *ubi) |
| 886 | { |
| 887 | int err = 0; |
| 888 | struct ubi_wl_entry *e1; |
| 889 | struct ubi_wl_entry *e2; |
| 890 | struct ubi_work *wrk; |
| 891 | |
| 892 | spin_lock(&ubi->wl_lock); |
| 893 | if (ubi->wl_scheduled) |
| 894 | /* Wear-leveling is already in the work queue */ |
| 895 | goto out_unlock; |
| 896 | |
| 897 | /* |
| 898 | * If the ubi->scrub tree is not empty, scrubbing is needed, and the |
| 899 | * the WL worker has to be scheduled anyway. |
| 900 | */ |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 901 | if (!ubi->scrub.rb_node) { |
| 902 | if (!ubi->used.rb_node || !ubi->free.rb_node) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 903 | /* No physical eraseblocks - no deal */ |
| 904 | goto out_unlock; |
| 905 | |
| 906 | /* |
| 907 | * We schedule wear-leveling only if the difference between the |
| 908 | * lowest erase counter of used physical eraseblocks and a high |
Frederik Schwarzer | 025dfda | 2008-10-16 19:02:37 +0200 | [diff] [blame] | 909 | * erase counter of free physical eraseblocks is greater than |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 910 | * %UBI_WL_THRESHOLD. |
| 911 | */ |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 912 | e1 = rb_entry(rb_first(&ubi->used), struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 913 | e2 = find_wl_entry(&ubi->free, WL_FREE_MAX_DIFF); |
| 914 | |
| 915 | if (!(e2->ec - e1->ec >= UBI_WL_THRESHOLD)) |
| 916 | goto out_unlock; |
| 917 | dbg_wl("schedule wear-leveling"); |
| 918 | } else |
| 919 | dbg_wl("schedule scrubbing"); |
| 920 | |
| 921 | ubi->wl_scheduled = 1; |
| 922 | spin_unlock(&ubi->wl_lock); |
| 923 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 924 | wrk = kmalloc(sizeof(struct ubi_work), GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 925 | if (!wrk) { |
| 926 | err = -ENOMEM; |
| 927 | goto out_cancel; |
| 928 | } |
| 929 | |
| 930 | wrk->func = &wear_leveling_worker; |
| 931 | schedule_ubi_work(ubi, wrk); |
| 932 | return err; |
| 933 | |
| 934 | out_cancel: |
| 935 | spin_lock(&ubi->wl_lock); |
| 936 | ubi->wl_scheduled = 0; |
| 937 | out_unlock: |
| 938 | spin_unlock(&ubi->wl_lock); |
| 939 | return err; |
| 940 | } |
| 941 | |
| 942 | /** |
| 943 | * erase_worker - physical eraseblock erase worker function. |
| 944 | * @ubi: UBI device description object |
| 945 | * @wl_wrk: the work object |
| 946 | * @cancel: non-zero if the worker has to free memory and exit |
| 947 | * |
| 948 | * This function erases a physical eraseblock and perform torture testing if |
| 949 | * needed. It also takes care about marking the physical eraseblock bad if |
| 950 | * needed. Returns zero in case of success and a negative error code in case of |
| 951 | * failure. |
| 952 | */ |
| 953 | static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, |
| 954 | int cancel) |
| 955 | { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 956 | struct ubi_wl_entry *e = wl_wrk->e; |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 957 | int pnum = e->pnum; |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 958 | int vol_id = wl_wrk->vol_id; |
| 959 | int lnum = wl_wrk->lnum; |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 960 | int err, available_consumed = 0; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 961 | |
| 962 | if (cancel) { |
| 963 | dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec); |
| 964 | kfree(wl_wrk); |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 965 | kmem_cache_free(ubi_wl_entry_slab, e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 966 | return 0; |
| 967 | } |
| 968 | |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 969 | dbg_wl("erase PEB %d EC %d LEB %d:%d", |
| 970 | pnum, e->ec, wl_wrk->vol_id, wl_wrk->lnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 971 | |
| 972 | err = sync_erase(ubi, e, wl_wrk->torture); |
| 973 | if (!err) { |
| 974 | /* Fine, we've erased it successfully */ |
| 975 | kfree(wl_wrk); |
| 976 | |
| 977 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 978 | wl_tree_add(e, &ubi->free); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 979 | spin_unlock(&ubi->wl_lock); |
| 980 | |
| 981 | /* |
Artem Bityutskiy | 9c9ec14 | 2008-07-18 13:19:52 +0300 | [diff] [blame] | 982 | * One more erase operation has happened, take care about |
| 983 | * protected physical eraseblocks. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 984 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 985 | serve_prot_queue(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 986 | |
| 987 | /* And take care about wear-leveling */ |
| 988 | err = ensure_wear_leveling(ubi); |
| 989 | return err; |
| 990 | } |
| 991 | |
Artem Bityutskiy | 8d2d401 | 2007-07-22 22:32:51 +0300 | [diff] [blame] | 992 | ubi_err("failed to erase PEB %d, error %d", pnum, err); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 993 | kfree(wl_wrk); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 994 | |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 995 | if (err == -EINTR || err == -ENOMEM || err == -EAGAIN || |
| 996 | err == -EBUSY) { |
| 997 | int err1; |
| 998 | |
| 999 | /* Re-schedule the LEB for erasure */ |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 1000 | err1 = schedule_erase(ubi, e, vol_id, lnum, 0); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1001 | if (err1) { |
| 1002 | err = err1; |
| 1003 | goto out_ro; |
| 1004 | } |
| 1005 | return err; |
Artem Bityutskiy | e57e0d8 | 2012-01-05 10:47:18 +0200 | [diff] [blame] | 1006 | } |
| 1007 | |
| 1008 | kmem_cache_free(ubi_wl_entry_slab, e); |
| 1009 | if (err != -EIO) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1010 | /* |
| 1011 | * If this is not %-EIO, we have no idea what to do. Scheduling |
| 1012 | * this physical eraseblock for erasure again would cause |
Artem Bityutskiy | 815bc5f8f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 1013 | * errors again and again. Well, lets switch to R/O mode. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1014 | */ |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1015 | goto out_ro; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1016 | |
| 1017 | /* It is %-EIO, the PEB went bad */ |
| 1018 | |
| 1019 | if (!ubi->bad_allowed) { |
| 1020 | ubi_err("bad physical eraseblock %d detected", pnum); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1021 | goto out_ro; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1022 | } |
| 1023 | |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1024 | spin_lock(&ubi->volumes_lock); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1025 | if (ubi->beb_rsvd_pebs == 0) { |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 1026 | if (ubi->avail_pebs == 0) { |
| 1027 | spin_unlock(&ubi->volumes_lock); |
| 1028 | ubi_err("no reserved/available physical eraseblocks"); |
| 1029 | goto out_ro; |
| 1030 | } |
| 1031 | ubi->avail_pebs -= 1; |
| 1032 | available_consumed = 1; |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1033 | } |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1034 | spin_unlock(&ubi->volumes_lock); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1035 | |
Artem Bityutskiy | 52b605d | 2009-06-08 16:52:48 +0300 | [diff] [blame] | 1036 | ubi_msg("mark PEB %d as bad", pnum); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1037 | err = ubi_io_mark_bad(ubi, pnum); |
| 1038 | if (err) |
| 1039 | goto out_ro; |
| 1040 | |
| 1041 | spin_lock(&ubi->volumes_lock); |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 1042 | if (ubi->beb_rsvd_pebs > 0) { |
| 1043 | if (available_consumed) { |
| 1044 | /* |
| 1045 | * The amount of reserved PEBs increased since we last |
| 1046 | * checked. |
| 1047 | */ |
| 1048 | ubi->avail_pebs += 1; |
| 1049 | available_consumed = 0; |
| 1050 | } |
| 1051 | ubi->beb_rsvd_pebs -= 1; |
| 1052 | } |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1053 | ubi->bad_peb_count += 1; |
| 1054 | ubi->good_peb_count -= 1; |
| 1055 | ubi_calculate_reserved(ubi); |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 1056 | if (available_consumed) |
| 1057 | ubi_warn("no PEBs in the reserved pool, used an available PEB"); |
| 1058 | else if (ubi->beb_rsvd_pebs) |
Artem Bityutskiy | 52b605d | 2009-06-08 16:52:48 +0300 | [diff] [blame] | 1059 | ubi_msg("%d PEBs left in the reserve", ubi->beb_rsvd_pebs); |
| 1060 | else |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 1061 | ubi_warn("last PEB from the reserve was used"); |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1062 | spin_unlock(&ubi->volumes_lock); |
| 1063 | |
| 1064 | return err; |
| 1065 | |
| 1066 | out_ro: |
Shmulik Ladkani | 37f758a | 2012-07-04 11:06:01 +0300 | [diff] [blame] | 1067 | if (available_consumed) { |
| 1068 | spin_lock(&ubi->volumes_lock); |
| 1069 | ubi->avail_pebs += 1; |
| 1070 | spin_unlock(&ubi->volumes_lock); |
| 1071 | } |
Artem Bityutskiy | 784c145 | 2007-07-18 13:42:10 +0300 | [diff] [blame] | 1072 | ubi_ro_mode(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1073 | return err; |
| 1074 | } |
| 1075 | |
| 1076 | /** |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 1077 | * ubi_wl_put_peb - return a PEB to the wear-leveling sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1078 | * @ubi: UBI device description object |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 1079 | * @vol_id: the volume ID that last used this PEB |
| 1080 | * @lnum: the last used logical eraseblock number for the PEB |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1081 | * @pnum: physical eraseblock to return |
| 1082 | * @torture: if this physical eraseblock has to be tortured |
| 1083 | * |
| 1084 | * This function is called to return physical eraseblock @pnum to the pool of |
| 1085 | * free physical eraseblocks. The @torture flag has to be set if an I/O error |
| 1086 | * occurred to this @pnum and it has to be tested. This function returns zero |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1087 | * in case of success, and a negative error code in case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1088 | */ |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 1089 | int ubi_wl_put_peb(struct ubi_device *ubi, int vol_id, int lnum, |
| 1090 | int pnum, int torture) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1091 | { |
| 1092 | int err; |
| 1093 | struct ubi_wl_entry *e; |
| 1094 | |
| 1095 | dbg_wl("PEB %d", pnum); |
| 1096 | ubi_assert(pnum >= 0); |
| 1097 | ubi_assert(pnum < ubi->peb_count); |
| 1098 | |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1099 | retry: |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1100 | spin_lock(&ubi->wl_lock); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1101 | e = ubi->lookuptbl[pnum]; |
| 1102 | if (e == ubi->move_from) { |
| 1103 | /* |
| 1104 | * User is putting the physical eraseblock which was selected to |
| 1105 | * be moved. It will be scheduled for erasure in the |
| 1106 | * wear-leveling worker. |
| 1107 | */ |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1108 | dbg_wl("PEB %d is being moved, wait", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1109 | spin_unlock(&ubi->wl_lock); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* Wait for the WL worker by taking the @ubi->move_mutex */ |
| 1112 | mutex_lock(&ubi->move_mutex); |
| 1113 | mutex_unlock(&ubi->move_mutex); |
| 1114 | goto retry; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1115 | } else if (e == ubi->move_to) { |
| 1116 | /* |
| 1117 | * User is putting the physical eraseblock which was selected |
| 1118 | * as the target the data is moved to. It may happen if the EBA |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 1119 | * sub-system already re-mapped the LEB in 'ubi_eba_copy_leb()' |
| 1120 | * but the WL sub-system has not put the PEB to the "used" tree |
| 1121 | * yet, but it is about to do this. So we just set a flag which |
| 1122 | * will tell the WL worker that the PEB is not needed anymore |
| 1123 | * and should be scheduled for erasure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1124 | */ |
| 1125 | dbg_wl("PEB %d is the target of data moving", pnum); |
| 1126 | ubi_assert(!ubi->move_to_put); |
| 1127 | ubi->move_to_put = 1; |
| 1128 | spin_unlock(&ubi->wl_lock); |
| 1129 | return 0; |
| 1130 | } else { |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1131 | if (in_wl_tree(e, &ubi->used)) { |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1132 | self_check_in_wl_tree(ubi, e, &ubi->used); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 1133 | rb_erase(&e->u.rb, &ubi->used); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1134 | } else if (in_wl_tree(e, &ubi->scrub)) { |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1135 | self_check_in_wl_tree(ubi, e, &ubi->scrub); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 1136 | rb_erase(&e->u.rb, &ubi->scrub); |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 1137 | } else if (in_wl_tree(e, &ubi->erroneous)) { |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1138 | self_check_in_wl_tree(ubi, e, &ubi->erroneous); |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 1139 | rb_erase(&e->u.rb, &ubi->erroneous); |
| 1140 | ubi->erroneous_peb_count -= 1; |
| 1141 | ubi_assert(ubi->erroneous_peb_count >= 0); |
Artem Bityutskiy | 815bc5f8f | 2009-06-08 19:28:18 +0300 | [diff] [blame] | 1142 | /* Erroneous PEBs should be tortured */ |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 1143 | torture = 1; |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1144 | } else { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1145 | err = prot_queue_del(ubi, e->pnum); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1146 | if (err) { |
| 1147 | ubi_err("PEB %d not found", pnum); |
| 1148 | ubi_ro_mode(ubi); |
| 1149 | spin_unlock(&ubi->wl_lock); |
| 1150 | return err; |
| 1151 | } |
| 1152 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1153 | } |
| 1154 | spin_unlock(&ubi->wl_lock); |
| 1155 | |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 1156 | err = schedule_erase(ubi, e, vol_id, lnum, torture); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1157 | if (err) { |
| 1158 | spin_lock(&ubi->wl_lock); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1159 | wl_tree_add(e, &ubi->used); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1160 | spin_unlock(&ubi->wl_lock); |
| 1161 | } |
| 1162 | |
| 1163 | return err; |
| 1164 | } |
| 1165 | |
| 1166 | /** |
| 1167 | * ubi_wl_scrub_peb - schedule a physical eraseblock for scrubbing. |
| 1168 | * @ubi: UBI device description object |
| 1169 | * @pnum: the physical eraseblock to schedule |
| 1170 | * |
| 1171 | * If a bit-flip in a physical eraseblock is detected, this physical eraseblock |
| 1172 | * needs scrubbing. This function schedules a physical eraseblock for |
| 1173 | * scrubbing which is done in background. This function returns zero in case of |
| 1174 | * success and a negative error code in case of failure. |
| 1175 | */ |
| 1176 | int ubi_wl_scrub_peb(struct ubi_device *ubi, int pnum) |
| 1177 | { |
| 1178 | struct ubi_wl_entry *e; |
| 1179 | |
Artem Bityutskiy | 719bb84 | 2012-08-27 17:14:58 +0300 | [diff] [blame] | 1180 | ubi_msg("schedule PEB %d for scrubbing", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1181 | |
| 1182 | retry: |
| 1183 | spin_lock(&ubi->wl_lock); |
| 1184 | e = ubi->lookuptbl[pnum]; |
Artem Bityutskiy | d3f6e6c | 2010-08-29 23:34:44 +0300 | [diff] [blame] | 1185 | if (e == ubi->move_from || in_wl_tree(e, &ubi->scrub) || |
| 1186 | in_wl_tree(e, &ubi->erroneous)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1187 | spin_unlock(&ubi->wl_lock); |
| 1188 | return 0; |
| 1189 | } |
| 1190 | |
| 1191 | if (e == ubi->move_to) { |
| 1192 | /* |
| 1193 | * This physical eraseblock was used to move data to. The data |
| 1194 | * was moved but the PEB was not yet inserted to the proper |
| 1195 | * tree. We should just wait a little and let the WL worker |
| 1196 | * proceed. |
| 1197 | */ |
| 1198 | spin_unlock(&ubi->wl_lock); |
| 1199 | dbg_wl("the PEB %d is not in proper tree, retry", pnum); |
| 1200 | yield(); |
| 1201 | goto retry; |
| 1202 | } |
| 1203 | |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1204 | if (in_wl_tree(e, &ubi->used)) { |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1205 | self_check_in_wl_tree(ubi, e, &ubi->used); |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 1206 | rb_erase(&e->u.rb, &ubi->used); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1207 | } else { |
| 1208 | int err; |
| 1209 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1210 | err = prot_queue_del(ubi, e->pnum); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1211 | if (err) { |
| 1212 | ubi_err("PEB %d not found", pnum); |
| 1213 | ubi_ro_mode(ubi); |
| 1214 | spin_unlock(&ubi->wl_lock); |
| 1215 | return err; |
| 1216 | } |
| 1217 | } |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1218 | |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1219 | wl_tree_add(e, &ubi->scrub); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1220 | spin_unlock(&ubi->wl_lock); |
| 1221 | |
| 1222 | /* |
| 1223 | * Technically scrubbing is the same as wear-leveling, so it is done |
| 1224 | * by the WL worker. |
| 1225 | */ |
| 1226 | return ensure_wear_leveling(ubi); |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * ubi_wl_flush - flush all pending works. |
| 1231 | * @ubi: UBI device description object |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1232 | * @vol_id: the volume id to flush for |
| 1233 | * @lnum: the logical eraseblock number to flush for |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1234 | * |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1235 | * This function executes all pending works for a particular volume id / |
| 1236 | * logical eraseblock number pair. If either value is set to %UBI_ALL, then it |
| 1237 | * acts as a wildcard for all of the corresponding volume numbers or logical |
| 1238 | * eraseblock numbers. It returns zero in case of success and a negative error |
| 1239 | * code in case of failure. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1240 | */ |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1241 | int ubi_wl_flush(struct ubi_device *ubi, int vol_id, int lnum) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1242 | { |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1243 | int err = 0; |
| 1244 | int found = 1; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1245 | |
| 1246 | /* |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1247 | * Erase while the pending works queue is not empty, but not more than |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1248 | * the number of currently pending works. |
| 1249 | */ |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1250 | dbg_wl("flush pending work for LEB %d:%d (%d pending works)", |
| 1251 | vol_id, lnum, ubi->works_count); |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 1252 | |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1253 | while (found) { |
| 1254 | struct ubi_work *wrk; |
| 1255 | found = 0; |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 1256 | |
Artem Bityutskiy | 12027f1 | 2012-06-07 15:15:30 +0300 | [diff] [blame] | 1257 | down_read(&ubi->work_sem); |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1258 | spin_lock(&ubi->wl_lock); |
| 1259 | list_for_each_entry(wrk, &ubi->works, list) { |
| 1260 | if ((vol_id == UBI_ALL || wrk->vol_id == vol_id) && |
| 1261 | (lnum == UBI_ALL || wrk->lnum == lnum)) { |
| 1262 | list_del(&wrk->list); |
| 1263 | ubi->works_count -= 1; |
| 1264 | ubi_assert(ubi->works_count >= 0); |
| 1265 | spin_unlock(&ubi->wl_lock); |
| 1266 | |
| 1267 | err = wrk->func(ubi, wrk, 0); |
Artem Bityutskiy | 12027f1 | 2012-06-07 15:15:30 +0300 | [diff] [blame] | 1268 | if (err) { |
| 1269 | up_read(&ubi->work_sem); |
| 1270 | return err; |
| 1271 | } |
| 1272 | |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1273 | spin_lock(&ubi->wl_lock); |
| 1274 | found = 1; |
| 1275 | break; |
| 1276 | } |
| 1277 | } |
| 1278 | spin_unlock(&ubi->wl_lock); |
Artem Bityutskiy | 12027f1 | 2012-06-07 15:15:30 +0300 | [diff] [blame] | 1279 | up_read(&ubi->work_sem); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1280 | } |
| 1281 | |
Artem Bityutskiy | 12027f1 | 2012-06-07 15:15:30 +0300 | [diff] [blame] | 1282 | /* |
| 1283 | * Make sure all the works which have been done in parallel are |
| 1284 | * finished. |
| 1285 | */ |
| 1286 | down_write(&ubi->work_sem); |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1287 | up_write(&ubi->work_sem); |
Artem Bityutskiy | 12027f1 | 2012-06-07 15:15:30 +0300 | [diff] [blame] | 1288 | |
Joel Reardon | 62f38455 | 2012-05-20 21:27:11 +0200 | [diff] [blame] | 1289 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | /** |
| 1293 | * tree_destroy - destroy an RB-tree. |
| 1294 | * @root: the root of the tree to destroy |
| 1295 | */ |
| 1296 | static void tree_destroy(struct rb_root *root) |
| 1297 | { |
| 1298 | struct rb_node *rb; |
| 1299 | struct ubi_wl_entry *e; |
| 1300 | |
| 1301 | rb = root->rb_node; |
| 1302 | while (rb) { |
| 1303 | if (rb->rb_left) |
| 1304 | rb = rb->rb_left; |
| 1305 | else if (rb->rb_right) |
| 1306 | rb = rb->rb_right; |
| 1307 | else { |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 1308 | e = rb_entry(rb, struct ubi_wl_entry, u.rb); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1309 | |
| 1310 | rb = rb_parent(rb); |
| 1311 | if (rb) { |
Xiaochuan-Xu | 23553b2 | 2008-12-09 19:44:12 +0800 | [diff] [blame] | 1312 | if (rb->rb_left == &e->u.rb) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1313 | rb->rb_left = NULL; |
| 1314 | else |
| 1315 | rb->rb_right = NULL; |
| 1316 | } |
| 1317 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 1318 | kmem_cache_free(ubi_wl_entry_slab, e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1319 | } |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | /** |
| 1324 | * ubi_thread - UBI background thread. |
| 1325 | * @u: the UBI device description object pointer |
| 1326 | */ |
Artem Bityutskiy | cdfa788 | 2007-12-17 20:33:20 +0200 | [diff] [blame] | 1327 | int ubi_thread(void *u) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1328 | { |
| 1329 | int failures = 0; |
| 1330 | struct ubi_device *ubi = u; |
| 1331 | |
| 1332 | ubi_msg("background thread \"%s\" started, PID %d", |
Pavel Emelyanov | ba25f9d | 2007-10-18 23:40:40 -0700 | [diff] [blame] | 1333 | ubi->bgt_name, task_pid_nr(current)); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1334 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 1335 | set_freezable(); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1336 | for (;;) { |
| 1337 | int err; |
| 1338 | |
| 1339 | if (kthread_should_stop()) |
Kyungmin Park | cadb40c | 2008-05-22 10:32:18 +0900 | [diff] [blame] | 1340 | break; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1341 | |
| 1342 | if (try_to_freeze()) |
| 1343 | continue; |
| 1344 | |
| 1345 | spin_lock(&ubi->wl_lock); |
| 1346 | if (list_empty(&ubi->works) || ubi->ro_mode || |
Artem Bityutskiy | 27a0f2a | 2011-05-18 16:03:23 +0300 | [diff] [blame] | 1347 | !ubi->thread_enabled || ubi_dbg_is_bgt_disabled(ubi)) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1348 | set_current_state(TASK_INTERRUPTIBLE); |
| 1349 | spin_unlock(&ubi->wl_lock); |
| 1350 | schedule(); |
| 1351 | continue; |
| 1352 | } |
| 1353 | spin_unlock(&ubi->wl_lock); |
| 1354 | |
| 1355 | err = do_work(ubi); |
| 1356 | if (err) { |
| 1357 | ubi_err("%s: work failed with error code %d", |
| 1358 | ubi->bgt_name, err); |
| 1359 | if (failures++ > WL_MAX_FAILURES) { |
| 1360 | /* |
| 1361 | * Too many failures, disable the thread and |
| 1362 | * switch to read-only mode. |
| 1363 | */ |
| 1364 | ubi_msg("%s: %d consecutive failures", |
| 1365 | ubi->bgt_name, WL_MAX_FAILURES); |
| 1366 | ubi_ro_mode(ubi); |
Vitaliy Gusev | 2ad4988 | 2008-11-05 18:27:18 +0300 | [diff] [blame] | 1367 | ubi->thread_enabled = 0; |
| 1368 | continue; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1369 | } |
| 1370 | } else |
| 1371 | failures = 0; |
| 1372 | |
| 1373 | cond_resched(); |
| 1374 | } |
| 1375 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1376 | dbg_wl("background thread \"%s\" is killed", ubi->bgt_name); |
| 1377 | return 0; |
| 1378 | } |
| 1379 | |
| 1380 | /** |
| 1381 | * cancel_pending - cancel all pending works. |
| 1382 | * @ubi: UBI device description object |
| 1383 | */ |
| 1384 | static void cancel_pending(struct ubi_device *ubi) |
| 1385 | { |
| 1386 | while (!list_empty(&ubi->works)) { |
| 1387 | struct ubi_work *wrk; |
| 1388 | |
| 1389 | wrk = list_entry(ubi->works.next, struct ubi_work, list); |
| 1390 | list_del(&wrk->list); |
| 1391 | wrk->func(ubi, wrk, 1); |
| 1392 | ubi->works_count -= 1; |
| 1393 | ubi_assert(ubi->works_count >= 0); |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | /** |
Artem Bityutskiy | 41e0cd9 | 2012-05-17 21:05:33 +0300 | [diff] [blame] | 1398 | * ubi_wl_init - initialize the WL sub-system using attaching information. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1399 | * @ubi: UBI device description object |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1400 | * @ai: attaching information |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1401 | * |
| 1402 | * This function returns zero in case of success, and a negative error code in |
| 1403 | * case of failure. |
| 1404 | */ |
Artem Bityutskiy | 41e0cd9 | 2012-05-17 21:05:33 +0300 | [diff] [blame] | 1405 | int ubi_wl_init(struct ubi_device *ubi, struct ubi_attach_info *ai) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1406 | { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1407 | int err, i; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1408 | struct rb_node *rb1, *rb2; |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1409 | struct ubi_ainf_volume *av; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1410 | struct ubi_ainf_peb *aeb, *tmp; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1411 | struct ubi_wl_entry *e; |
| 1412 | |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 1413 | ubi->used = ubi->erroneous = ubi->free = ubi->scrub = RB_ROOT; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1414 | spin_lock_init(&ubi->wl_lock); |
Artem Bityutskiy | 43f9b25 | 2007-12-18 15:06:55 +0200 | [diff] [blame] | 1415 | mutex_init(&ubi->move_mutex); |
Artem Bityutskiy | 593dd33 | 2007-12-18 15:54:35 +0200 | [diff] [blame] | 1416 | init_rwsem(&ubi->work_sem); |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1417 | ubi->max_ec = ai->max_ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1418 | INIT_LIST_HEAD(&ubi->works); |
| 1419 | |
| 1420 | sprintf(ubi->bgt_name, UBI_BGT_NAME_PATTERN, ubi->ubi_num); |
| 1421 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1422 | err = -ENOMEM; |
| 1423 | ubi->lookuptbl = kzalloc(ubi->peb_count * sizeof(void *), GFP_KERNEL); |
| 1424 | if (!ubi->lookuptbl) |
Artem Bityutskiy | cdfa788 | 2007-12-17 20:33:20 +0200 | [diff] [blame] | 1425 | return err; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1426 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1427 | for (i = 0; i < UBI_PROT_QUEUE_LEN; i++) |
| 1428 | INIT_LIST_HEAD(&ubi->pq[i]); |
| 1429 | ubi->pq_head = 0; |
| 1430 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1431 | list_for_each_entry_safe(aeb, tmp, &ai->erase, u.list) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1432 | cond_resched(); |
| 1433 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 1434 | e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1435 | if (!e) |
| 1436 | goto out_free; |
| 1437 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1438 | e->pnum = aeb->pnum; |
| 1439 | e->ec = aeb->ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1440 | ubi->lookuptbl[e->pnum] = e; |
Joel Reardon | d36e59e | 2012-05-18 15:40:24 +0200 | [diff] [blame] | 1441 | if (schedule_erase(ubi, e, aeb->vol_id, aeb->lnum, 0)) { |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 1442 | kmem_cache_free(ubi_wl_entry_slab, e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1443 | goto out_free; |
| 1444 | } |
| 1445 | } |
| 1446 | |
Artem Bityutskiy | a4e6042 | 2012-05-17 13:09:08 +0300 | [diff] [blame] | 1447 | list_for_each_entry(aeb, &ai->free, u.list) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1448 | cond_resched(); |
| 1449 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 1450 | e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1451 | if (!e) |
| 1452 | goto out_free; |
| 1453 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1454 | e->pnum = aeb->pnum; |
| 1455 | e->ec = aeb->ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1456 | ubi_assert(e->ec >= 0); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1457 | wl_tree_add(e, &ubi->free); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1458 | ubi->lookuptbl[e->pnum] = e; |
| 1459 | } |
| 1460 | |
Artem Bityutskiy | 517af48 | 2012-05-17 14:38:34 +0300 | [diff] [blame] | 1461 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) { |
| 1462 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1463 | cond_resched(); |
| 1464 | |
Artem Bityutskiy | 06b68ba | 2007-12-16 12:49:01 +0200 | [diff] [blame] | 1465 | e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1466 | if (!e) |
| 1467 | goto out_free; |
| 1468 | |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1469 | e->pnum = aeb->pnum; |
| 1470 | e->ec = aeb->ec; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1471 | ubi->lookuptbl[e->pnum] = e; |
Artem Bityutskiy | 2c5ec5c | 2012-05-17 08:26:24 +0300 | [diff] [blame] | 1472 | if (!aeb->scrub) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1473 | dbg_wl("add PEB %d EC %d to the used tree", |
| 1474 | e->pnum, e->ec); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1475 | wl_tree_add(e, &ubi->used); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1476 | } else { |
| 1477 | dbg_wl("add PEB %d EC %d to the scrub tree", |
| 1478 | e->pnum, e->ec); |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1479 | wl_tree_add(e, &ubi->scrub); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | } |
| 1483 | |
Artem Bityutskiy | 5abde38 | 2007-09-13 14:48:20 +0300 | [diff] [blame] | 1484 | if (ubi->avail_pebs < WL_RESERVED_PEBS) { |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1485 | ubi_err("no enough physical eraseblocks (%d, need %d)", |
| 1486 | ubi->avail_pebs, WL_RESERVED_PEBS); |
Artem Bityutskiy | 5fc01ab | 2010-09-03 23:08:15 +0300 | [diff] [blame] | 1487 | if (ubi->corr_peb_count) |
| 1488 | ubi_err("%d PEBs are corrupted and not used", |
| 1489 | ubi->corr_peb_count); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1490 | goto out_free; |
| 1491 | } |
| 1492 | ubi->avail_pebs -= WL_RESERVED_PEBS; |
| 1493 | ubi->rsvd_pebs += WL_RESERVED_PEBS; |
| 1494 | |
| 1495 | /* Schedule wear-leveling if needed */ |
| 1496 | err = ensure_wear_leveling(ubi); |
| 1497 | if (err) |
| 1498 | goto out_free; |
| 1499 | |
| 1500 | return 0; |
| 1501 | |
| 1502 | out_free: |
| 1503 | cancel_pending(ubi); |
| 1504 | tree_destroy(&ubi->used); |
| 1505 | tree_destroy(&ubi->free); |
| 1506 | tree_destroy(&ubi->scrub); |
| 1507 | kfree(ubi->lookuptbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1508 | return err; |
| 1509 | } |
| 1510 | |
| 1511 | /** |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1512 | * protection_queue_destroy - destroy the protection queue. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1513 | * @ubi: UBI device description object |
| 1514 | */ |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1515 | static void protection_queue_destroy(struct ubi_device *ubi) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1516 | { |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1517 | int i; |
| 1518 | struct ubi_wl_entry *e, *tmp; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1519 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1520 | for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) { |
| 1521 | list_for_each_entry_safe(e, tmp, &ubi->pq[i], u.list) { |
| 1522 | list_del(&e->u.list); |
| 1523 | kmem_cache_free(ubi_wl_entry_slab, e); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /** |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 1529 | * ubi_wl_close - close the wear-leveling sub-system. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1530 | * @ubi: UBI device description object |
| 1531 | */ |
| 1532 | void ubi_wl_close(struct ubi_device *ubi) |
| 1533 | { |
Artem Bityutskiy | 85c6e6e | 2008-07-16 10:25:56 +0300 | [diff] [blame] | 1534 | dbg_wl("close the WL sub-system"); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1535 | cancel_pending(ubi); |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1536 | protection_queue_destroy(ubi); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1537 | tree_destroy(&ubi->used); |
Artem Bityutskiy | b86a2c5 | 2009-05-24 14:13:34 +0300 | [diff] [blame] | 1538 | tree_destroy(&ubi->erroneous); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1539 | tree_destroy(&ubi->free); |
| 1540 | tree_destroy(&ubi->scrub); |
| 1541 | kfree(ubi->lookuptbl); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1542 | } |
| 1543 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1544 | /** |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1545 | * self_check_ec - make sure that the erase counter of a PEB is correct. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1546 | * @ubi: UBI device description object |
| 1547 | * @pnum: the physical eraseblock number to check |
| 1548 | * @ec: the erase counter to check |
| 1549 | * |
| 1550 | * This function returns zero if the erase counter of physical eraseblock @pnum |
Artem Bityutskiy | feddbb3 | 2011-03-28 10:12:25 +0300 | [diff] [blame] | 1551 | * is equivalent to @ec, and a negative error code if not or if an error |
| 1552 | * occurred. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1553 | */ |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1554 | static int self_check_ec(struct ubi_device *ubi, int pnum, int ec) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1555 | { |
| 1556 | int err; |
| 1557 | long long read_ec; |
| 1558 | struct ubi_ec_hdr *ec_hdr; |
| 1559 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 1560 | if (!ubi->dbg->chk_gen) |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 1561 | return 0; |
| 1562 | |
Artem Bityutskiy | 33818bb | 2007-08-28 21:29:32 +0300 | [diff] [blame] | 1563 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1564 | if (!ec_hdr) |
| 1565 | return -ENOMEM; |
| 1566 | |
| 1567 | err = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); |
| 1568 | if (err && err != UBI_IO_BITFLIPS) { |
| 1569 | /* The header does not have to exist */ |
| 1570 | err = 0; |
| 1571 | goto out_free; |
| 1572 | } |
| 1573 | |
Christoph Hellwig | 3261ebd | 2007-05-21 17:41:46 +0300 | [diff] [blame] | 1574 | read_ec = be64_to_cpu(ec_hdr->ec); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1575 | if (ec != read_ec) { |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1576 | ubi_err("self-check failed for PEB %d", pnum); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1577 | ubi_err("read EC is %lld, should be %d", read_ec, ec); |
Artem Bityutskiy | 25886a3 | 2012-04-24 06:59:49 +0300 | [diff] [blame] | 1578 | dump_stack(); |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1579 | err = 1; |
| 1580 | } else |
| 1581 | err = 0; |
| 1582 | |
| 1583 | out_free: |
| 1584 | kfree(ec_hdr); |
| 1585 | return err; |
| 1586 | } |
| 1587 | |
| 1588 | /** |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1589 | * self_check_in_wl_tree - check that wear-leveling entry is in WL RB-tree. |
Artem Bityutskiy | d99383b | 2011-05-18 14:47:34 +0300 | [diff] [blame] | 1590 | * @ubi: UBI device description object |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1591 | * @e: the wear-leveling entry to check |
| 1592 | * @root: the root of the tree |
| 1593 | * |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1594 | * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it |
| 1595 | * is not. |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1596 | */ |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1597 | static int self_check_in_wl_tree(const struct ubi_device *ubi, |
| 1598 | struct ubi_wl_entry *e, struct rb_root *root) |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1599 | { |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 1600 | if (!ubi->dbg->chk_gen) |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 1601 | return 0; |
| 1602 | |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1603 | if (in_wl_tree(e, root)) |
| 1604 | return 0; |
| 1605 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1606 | ubi_err("self-check failed for PEB %d, EC %d, RB-tree %p ", |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1607 | e->pnum, e->ec, root); |
Artem Bityutskiy | 25886a3 | 2012-04-24 06:59:49 +0300 | [diff] [blame] | 1608 | dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1609 | return -EINVAL; |
Artem B. Bityutskiy | 801c135 | 2006-06-27 12:22:22 +0400 | [diff] [blame] | 1610 | } |
| 1611 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1612 | /** |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1613 | * self_check_in_pq - check if wear-leveling entry is in the protection |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1614 | * queue. |
| 1615 | * @ubi: UBI device description object |
| 1616 | * @e: the wear-leveling entry to check |
| 1617 | * |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1618 | * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not. |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1619 | */ |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1620 | static int self_check_in_pq(const struct ubi_device *ubi, |
| 1621 | struct ubi_wl_entry *e) |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1622 | { |
| 1623 | struct ubi_wl_entry *p; |
| 1624 | int i; |
| 1625 | |
Artem Bityutskiy | 2a734bb | 2011-05-18 14:53:05 +0300 | [diff] [blame] | 1626 | if (!ubi->dbg->chk_gen) |
Artem Bityutskiy | 92d124f | 2011-03-14 18:17:40 +0200 | [diff] [blame] | 1627 | return 0; |
| 1628 | |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1629 | for (i = 0; i < UBI_PROT_QUEUE_LEN; ++i) |
| 1630 | list_for_each_entry(p, &ubi->pq[i], u.list) |
| 1631 | if (p == e) |
| 1632 | return 0; |
| 1633 | |
Artem Bityutskiy | 7bf523a | 2012-05-16 18:29:54 +0300 | [diff] [blame] | 1634 | ubi_err("self-check failed for PEB %d, EC %d, Protect queue", |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1635 | e->pnum, e->ec); |
Artem Bityutskiy | 25886a3 | 2012-04-24 06:59:49 +0300 | [diff] [blame] | 1636 | dump_stack(); |
Artem Bityutskiy | adbf05e | 2010-01-20 10:28:58 +0200 | [diff] [blame] | 1637 | return -EINVAL; |
Xiaochuan-Xu | 7b6c32d | 2008-12-15 21:07:41 +0800 | [diff] [blame] | 1638 | } |