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