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