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