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