Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2012 Linutronix GmbH |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 4 | * Copyright (c) 2014 sigma star gmbh |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5 | * Author: Richard Weinberger <richard@nod.at> |
| 6 | * |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 9 | #ifndef __UBOOT__ |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 10 | #include <dm/devres.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 11 | #include <linux/crc32.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 12 | #include <linux/err.h> |
Simon Glass | 48b6c6b | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 13 | #include <u-boot/crc.h> |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 14 | #else |
| 15 | #include <div64.h> |
| 16 | #include <malloc.h> |
| 17 | #include <ubi_uboot.h> |
| 18 | #endif |
| 19 | |
| 20 | #include <linux/compat.h> |
| 21 | #include <linux/math64.h> |
| 22 | #include "ubi.h" |
| 23 | |
| 24 | /** |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 25 | * init_seen - allocate memory for used for debugging. |
| 26 | * @ubi: UBI device description object |
| 27 | */ |
| 28 | static inline int *init_seen(struct ubi_device *ubi) |
| 29 | { |
| 30 | int *ret; |
| 31 | |
| 32 | if (!ubi_dbg_chk_fastmap(ubi)) |
| 33 | return NULL; |
| 34 | |
| 35 | ret = kcalloc(ubi->peb_count, sizeof(int), GFP_KERNEL); |
| 36 | if (!ret) |
| 37 | return ERR_PTR(-ENOMEM); |
| 38 | |
| 39 | return ret; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * free_seen - free the seen logic integer array. |
| 44 | * @seen: integer array of @ubi->peb_count size |
| 45 | */ |
| 46 | static inline void free_seen(int *seen) |
| 47 | { |
| 48 | kfree(seen); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * set_seen - mark a PEB as seen. |
| 53 | * @ubi: UBI device description object |
| 54 | * @pnum: The PEB to be makred as seen |
| 55 | * @seen: integer array of @ubi->peb_count size |
| 56 | */ |
| 57 | static inline void set_seen(struct ubi_device *ubi, int pnum, int *seen) |
| 58 | { |
| 59 | if (!ubi_dbg_chk_fastmap(ubi) || !seen) |
| 60 | return; |
| 61 | |
| 62 | seen[pnum] = 1; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * self_check_seen - check whether all PEB have been seen by fastmap. |
| 67 | * @ubi: UBI device description object |
| 68 | * @seen: integer array of @ubi->peb_count size |
| 69 | */ |
| 70 | static int self_check_seen(struct ubi_device *ubi, int *seen) |
| 71 | { |
| 72 | int pnum, ret = 0; |
| 73 | |
| 74 | if (!ubi_dbg_chk_fastmap(ubi) || !seen) |
| 75 | return 0; |
| 76 | |
| 77 | for (pnum = 0; pnum < ubi->peb_count; pnum++) { |
| 78 | if (!seen[pnum] && ubi->lookuptbl[pnum]) { |
| 79 | ubi_err(ubi, "self-check failed for PEB %d, fastmap didn't see it", pnum); |
| 80 | ret = -EINVAL; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return ret; |
| 85 | } |
| 86 | |
| 87 | /** |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 88 | * ubi_calc_fm_size - calculates the fastmap size in bytes for an UBI device. |
| 89 | * @ubi: UBI device description object |
| 90 | */ |
| 91 | size_t ubi_calc_fm_size(struct ubi_device *ubi) |
| 92 | { |
| 93 | size_t size; |
| 94 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 95 | size = sizeof(struct ubi_fm_sb) + |
| 96 | sizeof(struct ubi_fm_hdr) + |
| 97 | sizeof(struct ubi_fm_scan_pool) + |
| 98 | sizeof(struct ubi_fm_scan_pool) + |
| 99 | (ubi->peb_count * sizeof(struct ubi_fm_ec)) + |
| 100 | (sizeof(struct ubi_fm_eba) + |
| 101 | (ubi->peb_count * sizeof(__be32))) + |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 102 | sizeof(struct ubi_fm_volhdr) * UBI_MAX_VOLUMES; |
| 103 | return roundup(size, ubi->leb_size); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * new_fm_vhdr - allocate a new volume header for fastmap usage. |
| 109 | * @ubi: UBI device description object |
| 110 | * @vol_id: the VID of the new header |
| 111 | * |
| 112 | * Returns a new struct ubi_vid_hdr on success. |
| 113 | * NULL indicates out of memory. |
| 114 | */ |
| 115 | static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id) |
| 116 | { |
| 117 | struct ubi_vid_hdr *new; |
| 118 | |
| 119 | new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
| 120 | if (!new) |
| 121 | goto out; |
| 122 | |
| 123 | new->vol_type = UBI_VID_DYNAMIC; |
| 124 | new->vol_id = cpu_to_be32(vol_id); |
| 125 | |
| 126 | /* UBI implementations without fastmap support have to delete the |
| 127 | * fastmap. |
| 128 | */ |
| 129 | new->compat = UBI_COMPAT_DELETE; |
| 130 | |
| 131 | out: |
| 132 | return new; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * add_aeb - create and add a attach erase block to a given list. |
| 137 | * @ai: UBI attach info object |
| 138 | * @list: the target list |
| 139 | * @pnum: PEB number of the new attach erase block |
| 140 | * @ec: erease counter of the new LEB |
| 141 | * @scrub: scrub this PEB after attaching |
| 142 | * |
| 143 | * Returns 0 on success, < 0 indicates an internal error. |
| 144 | */ |
| 145 | static int add_aeb(struct ubi_attach_info *ai, struct list_head *list, |
| 146 | int pnum, int ec, int scrub) |
| 147 | { |
| 148 | struct ubi_ainf_peb *aeb; |
| 149 | |
| 150 | aeb = kmem_cache_alloc(ai->aeb_slab_cache, GFP_KERNEL); |
| 151 | if (!aeb) |
| 152 | return -ENOMEM; |
| 153 | |
| 154 | aeb->pnum = pnum; |
| 155 | aeb->ec = ec; |
| 156 | aeb->lnum = -1; |
| 157 | aeb->scrub = scrub; |
| 158 | aeb->copy_flag = aeb->sqnum = 0; |
| 159 | |
| 160 | ai->ec_sum += aeb->ec; |
| 161 | ai->ec_count++; |
| 162 | |
| 163 | if (ai->max_ec < aeb->ec) |
| 164 | ai->max_ec = aeb->ec; |
| 165 | |
| 166 | if (ai->min_ec > aeb->ec) |
| 167 | ai->min_ec = aeb->ec; |
| 168 | |
| 169 | list_add_tail(&aeb->u.list, list); |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * add_vol - create and add a new volume to ubi_attach_info. |
| 176 | * @ai: ubi_attach_info object |
| 177 | * @vol_id: VID of the new volume |
| 178 | * @used_ebs: number of used EBS |
| 179 | * @data_pad: data padding value of the new volume |
| 180 | * @vol_type: volume type |
| 181 | * @last_eb_bytes: number of bytes in the last LEB |
| 182 | * |
| 183 | * Returns the new struct ubi_ainf_volume on success. |
| 184 | * NULL indicates an error. |
| 185 | */ |
| 186 | static struct ubi_ainf_volume *add_vol(struct ubi_attach_info *ai, int vol_id, |
| 187 | int used_ebs, int data_pad, u8 vol_type, |
| 188 | int last_eb_bytes) |
| 189 | { |
| 190 | struct ubi_ainf_volume *av; |
| 191 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
| 192 | |
| 193 | while (*p) { |
| 194 | parent = *p; |
| 195 | av = rb_entry(parent, struct ubi_ainf_volume, rb); |
| 196 | |
| 197 | if (vol_id > av->vol_id) |
| 198 | p = &(*p)->rb_left; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 199 | else if (vol_id < av->vol_id) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 200 | p = &(*p)->rb_right; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 201 | else |
| 202 | return ERR_PTR(-EINVAL); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | av = kmalloc(sizeof(struct ubi_ainf_volume), GFP_KERNEL); |
| 206 | if (!av) |
| 207 | goto out; |
| 208 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 209 | av->highest_lnum = av->leb_count = av->used_ebs = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 210 | av->vol_id = vol_id; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 211 | av->data_pad = data_pad; |
| 212 | av->last_data_size = last_eb_bytes; |
| 213 | av->compat = 0; |
| 214 | av->vol_type = vol_type; |
| 215 | av->root = RB_ROOT; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 216 | if (av->vol_type == UBI_STATIC_VOLUME) |
| 217 | av->used_ebs = used_ebs; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 218 | |
| 219 | dbg_bld("found volume (ID %i)", vol_id); |
| 220 | |
| 221 | rb_link_node(&av->rb, parent, p); |
| 222 | rb_insert_color(&av->rb, &ai->volumes); |
| 223 | |
| 224 | out: |
| 225 | return av; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * assign_aeb_to_av - assigns a SEB to a given ainf_volume and removes it |
| 230 | * from it's original list. |
| 231 | * @ai: ubi_attach_info object |
| 232 | * @aeb: the to be assigned SEB |
| 233 | * @av: target scan volume |
| 234 | */ |
| 235 | static void assign_aeb_to_av(struct ubi_attach_info *ai, |
| 236 | struct ubi_ainf_peb *aeb, |
| 237 | struct ubi_ainf_volume *av) |
| 238 | { |
| 239 | struct ubi_ainf_peb *tmp_aeb; |
| 240 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
| 241 | |
| 242 | p = &av->root.rb_node; |
| 243 | while (*p) { |
| 244 | parent = *p; |
| 245 | |
| 246 | tmp_aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); |
| 247 | if (aeb->lnum != tmp_aeb->lnum) { |
| 248 | if (aeb->lnum < tmp_aeb->lnum) |
| 249 | p = &(*p)->rb_left; |
| 250 | else |
| 251 | p = &(*p)->rb_right; |
| 252 | |
| 253 | continue; |
| 254 | } else |
| 255 | break; |
| 256 | } |
| 257 | |
| 258 | list_del(&aeb->u.list); |
| 259 | av->leb_count++; |
| 260 | |
| 261 | rb_link_node(&aeb->u.rb, parent, p); |
| 262 | rb_insert_color(&aeb->u.rb, &av->root); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * update_vol - inserts or updates a LEB which was found a pool. |
| 267 | * @ubi: the UBI device object |
| 268 | * @ai: attach info object |
| 269 | * @av: the volume this LEB belongs to |
| 270 | * @new_vh: the volume header derived from new_aeb |
| 271 | * @new_aeb: the AEB to be examined |
| 272 | * |
| 273 | * Returns 0 on success, < 0 indicates an internal error. |
| 274 | */ |
| 275 | static int update_vol(struct ubi_device *ubi, struct ubi_attach_info *ai, |
| 276 | struct ubi_ainf_volume *av, struct ubi_vid_hdr *new_vh, |
| 277 | struct ubi_ainf_peb *new_aeb) |
| 278 | { |
| 279 | struct rb_node **p = &av->root.rb_node, *parent = NULL; |
| 280 | struct ubi_ainf_peb *aeb, *victim; |
| 281 | int cmp_res; |
| 282 | |
| 283 | while (*p) { |
| 284 | parent = *p; |
| 285 | aeb = rb_entry(parent, struct ubi_ainf_peb, u.rb); |
| 286 | |
| 287 | if (be32_to_cpu(new_vh->lnum) != aeb->lnum) { |
| 288 | if (be32_to_cpu(new_vh->lnum) < aeb->lnum) |
| 289 | p = &(*p)->rb_left; |
| 290 | else |
| 291 | p = &(*p)->rb_right; |
| 292 | |
| 293 | continue; |
| 294 | } |
| 295 | |
| 296 | /* This case can happen if the fastmap gets written |
| 297 | * because of a volume change (creation, deletion, ..). |
| 298 | * Then a PEB can be within the persistent EBA and the pool. |
| 299 | */ |
| 300 | if (aeb->pnum == new_aeb->pnum) { |
| 301 | ubi_assert(aeb->lnum == new_aeb->lnum); |
| 302 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | cmp_res = ubi_compare_lebs(ubi, aeb, new_aeb->pnum, new_vh); |
| 308 | if (cmp_res < 0) |
| 309 | return cmp_res; |
| 310 | |
| 311 | /* new_aeb is newer */ |
| 312 | if (cmp_res & 1) { |
| 313 | victim = kmem_cache_alloc(ai->aeb_slab_cache, |
| 314 | GFP_KERNEL); |
| 315 | if (!victim) |
| 316 | return -ENOMEM; |
| 317 | |
| 318 | victim->ec = aeb->ec; |
| 319 | victim->pnum = aeb->pnum; |
| 320 | list_add_tail(&victim->u.list, &ai->erase); |
| 321 | |
| 322 | if (av->highest_lnum == be32_to_cpu(new_vh->lnum)) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 323 | av->last_data_size = |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 324 | be32_to_cpu(new_vh->data_size); |
| 325 | |
| 326 | dbg_bld("vol %i: AEB %i's PEB %i is the newer", |
| 327 | av->vol_id, aeb->lnum, new_aeb->pnum); |
| 328 | |
| 329 | aeb->ec = new_aeb->ec; |
| 330 | aeb->pnum = new_aeb->pnum; |
| 331 | aeb->copy_flag = new_vh->copy_flag; |
| 332 | aeb->scrub = new_aeb->scrub; |
| 333 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); |
| 334 | |
| 335 | /* new_aeb is older */ |
| 336 | } else { |
| 337 | dbg_bld("vol %i: AEB %i's PEB %i is old, dropping it", |
| 338 | av->vol_id, aeb->lnum, new_aeb->pnum); |
| 339 | list_add_tail(&new_aeb->u.list, &ai->erase); |
| 340 | } |
| 341 | |
| 342 | return 0; |
| 343 | } |
| 344 | /* This LEB is new, let's add it to the volume */ |
| 345 | |
| 346 | if (av->highest_lnum <= be32_to_cpu(new_vh->lnum)) { |
| 347 | av->highest_lnum = be32_to_cpu(new_vh->lnum); |
| 348 | av->last_data_size = be32_to_cpu(new_vh->data_size); |
| 349 | } |
| 350 | |
| 351 | if (av->vol_type == UBI_STATIC_VOLUME) |
| 352 | av->used_ebs = be32_to_cpu(new_vh->used_ebs); |
| 353 | |
| 354 | av->leb_count++; |
| 355 | |
| 356 | rb_link_node(&new_aeb->u.rb, parent, p); |
| 357 | rb_insert_color(&new_aeb->u.rb, &av->root); |
| 358 | |
| 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * process_pool_aeb - we found a non-empty PEB in a pool. |
| 364 | * @ubi: UBI device object |
| 365 | * @ai: attach info object |
| 366 | * @new_vh: the volume header derived from new_aeb |
| 367 | * @new_aeb: the AEB to be examined |
| 368 | * |
| 369 | * Returns 0 on success, < 0 indicates an internal error. |
| 370 | */ |
| 371 | static int process_pool_aeb(struct ubi_device *ubi, struct ubi_attach_info *ai, |
| 372 | struct ubi_vid_hdr *new_vh, |
| 373 | struct ubi_ainf_peb *new_aeb) |
| 374 | { |
| 375 | struct ubi_ainf_volume *av, *tmp_av = NULL; |
| 376 | struct rb_node **p = &ai->volumes.rb_node, *parent = NULL; |
| 377 | int found = 0; |
| 378 | |
| 379 | if (be32_to_cpu(new_vh->vol_id) == UBI_FM_SB_VOLUME_ID || |
| 380 | be32_to_cpu(new_vh->vol_id) == UBI_FM_DATA_VOLUME_ID) { |
| 381 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); |
| 382 | |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | /* Find the volume this SEB belongs to */ |
| 387 | while (*p) { |
| 388 | parent = *p; |
| 389 | tmp_av = rb_entry(parent, struct ubi_ainf_volume, rb); |
| 390 | |
| 391 | if (be32_to_cpu(new_vh->vol_id) > tmp_av->vol_id) |
| 392 | p = &(*p)->rb_left; |
| 393 | else if (be32_to_cpu(new_vh->vol_id) < tmp_av->vol_id) |
| 394 | p = &(*p)->rb_right; |
| 395 | else { |
| 396 | found = 1; |
| 397 | break; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | if (found) |
| 402 | av = tmp_av; |
| 403 | else { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 404 | ubi_err(ubi, "orphaned volume in fastmap pool!"); |
| 405 | kmem_cache_free(ai->aeb_slab_cache, new_aeb); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 406 | return UBI_BAD_FASTMAP; |
| 407 | } |
| 408 | |
| 409 | ubi_assert(be32_to_cpu(new_vh->vol_id) == av->vol_id); |
| 410 | |
| 411 | return update_vol(ubi, ai, av, new_vh, new_aeb); |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * unmap_peb - unmap a PEB. |
| 416 | * If fastmap detects a free PEB in the pool it has to check whether |
| 417 | * this PEB has been unmapped after writing the fastmap. |
| 418 | * |
| 419 | * @ai: UBI attach info object |
| 420 | * @pnum: The PEB to be unmapped |
| 421 | */ |
| 422 | static void unmap_peb(struct ubi_attach_info *ai, int pnum) |
| 423 | { |
| 424 | struct ubi_ainf_volume *av; |
| 425 | struct rb_node *node, *node2; |
| 426 | struct ubi_ainf_peb *aeb; |
| 427 | |
| 428 | for (node = rb_first(&ai->volumes); node; node = rb_next(node)) { |
| 429 | av = rb_entry(node, struct ubi_ainf_volume, rb); |
| 430 | |
| 431 | for (node2 = rb_first(&av->root); node2; |
| 432 | node2 = rb_next(node2)) { |
| 433 | aeb = rb_entry(node2, struct ubi_ainf_peb, u.rb); |
| 434 | if (aeb->pnum == pnum) { |
| 435 | rb_erase(&aeb->u.rb, &av->root); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 436 | av->leb_count--; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 437 | kmem_cache_free(ai->aeb_slab_cache, aeb); |
| 438 | return; |
| 439 | } |
| 440 | } |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * scan_pool - scans a pool for changed (no longer empty PEBs). |
| 446 | * @ubi: UBI device object |
| 447 | * @ai: attach info object |
| 448 | * @pebs: an array of all PEB numbers in the to be scanned pool |
| 449 | * @pool_size: size of the pool (number of entries in @pebs) |
| 450 | * @max_sqnum: pointer to the maximal sequence number |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 451 | * @free: list of PEBs which are most likely free (and go into @ai->free) |
| 452 | * |
| 453 | * Returns 0 on success, if the pool is unusable UBI_BAD_FASTMAP is returned. |
| 454 | * < 0 indicates an internal error. |
| 455 | */ |
| 456 | #ifndef __UBOOT__ |
| 457 | static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, |
Heiko Schocher | 7f2a481 | 2015-10-22 06:19:22 +0200 | [diff] [blame] | 458 | __be32 *pebs, int pool_size, unsigned long long *max_sqnum, |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 459 | struct list_head *free) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 460 | #else |
| 461 | static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai, |
Heiko Schocher | 7f2a481 | 2015-10-22 06:19:22 +0200 | [diff] [blame] | 462 | __be32 *pebs, int pool_size, unsigned long long *max_sqnum, |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 463 | struct list_head *free) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 464 | #endif |
| 465 | { |
| 466 | struct ubi_vid_hdr *vh; |
| 467 | struct ubi_ec_hdr *ech; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 468 | struct ubi_ainf_peb *new_aeb; |
| 469 | int i, pnum, err, ret = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 470 | |
| 471 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 472 | if (!ech) |
| 473 | return -ENOMEM; |
| 474 | |
| 475 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
| 476 | if (!vh) { |
| 477 | kfree(ech); |
| 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
| 481 | dbg_bld("scanning fastmap pool: size = %i", pool_size); |
| 482 | |
| 483 | /* |
| 484 | * Now scan all PEBs in the pool to find changes which have been made |
| 485 | * after the creation of the fastmap |
| 486 | */ |
| 487 | for (i = 0; i < pool_size; i++) { |
| 488 | int scrub = 0; |
| 489 | int image_seq; |
| 490 | |
| 491 | pnum = be32_to_cpu(pebs[i]); |
| 492 | |
| 493 | if (ubi_io_is_bad(ubi, pnum)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 494 | ubi_err(ubi, "bad PEB in fastmap pool!"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 495 | ret = UBI_BAD_FASTMAP; |
| 496 | goto out; |
| 497 | } |
| 498 | |
| 499 | err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); |
| 500 | if (err && err != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 501 | ubi_err(ubi, "unable to read EC header! PEB:%i err:%i", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 502 | pnum, err); |
| 503 | ret = err > 0 ? UBI_BAD_FASTMAP : err; |
| 504 | goto out; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 505 | } else if (err == UBI_IO_BITFLIPS) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 506 | scrub = 1; |
| 507 | |
| 508 | /* |
| 509 | * Older UBI implementations have image_seq set to zero, so |
| 510 | * we shouldn't fail if image_seq == 0. |
| 511 | */ |
| 512 | image_seq = be32_to_cpu(ech->image_seq); |
| 513 | |
| 514 | if (image_seq && (image_seq != ubi->image_seq)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 515 | ubi_err(ubi, "bad image seq: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 516 | be32_to_cpu(ech->image_seq), ubi->image_seq); |
| 517 | ret = UBI_BAD_FASTMAP; |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); |
| 522 | if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) { |
| 523 | unsigned long long ec = be64_to_cpu(ech->ec); |
| 524 | unmap_peb(ai, pnum); |
| 525 | dbg_bld("Adding PEB to free: %i", pnum); |
| 526 | if (err == UBI_IO_FF_BITFLIPS) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 527 | add_aeb(ai, free, pnum, ec, 1); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 528 | else |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 529 | add_aeb(ai, free, pnum, ec, 0); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 530 | continue; |
| 531 | } else if (err == 0 || err == UBI_IO_BITFLIPS) { |
| 532 | dbg_bld("Found non empty PEB:%i in pool", pnum); |
| 533 | |
| 534 | if (err == UBI_IO_BITFLIPS) |
| 535 | scrub = 1; |
| 536 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 537 | new_aeb = kmem_cache_alloc(ai->aeb_slab_cache, |
| 538 | GFP_KERNEL); |
| 539 | if (!new_aeb) { |
| 540 | ret = -ENOMEM; |
| 541 | goto out; |
| 542 | } |
| 543 | |
| 544 | new_aeb->ec = be64_to_cpu(ech->ec); |
| 545 | new_aeb->pnum = pnum; |
| 546 | new_aeb->lnum = be32_to_cpu(vh->lnum); |
| 547 | new_aeb->sqnum = be64_to_cpu(vh->sqnum); |
| 548 | new_aeb->copy_flag = vh->copy_flag; |
| 549 | new_aeb->scrub = scrub; |
| 550 | |
| 551 | if (*max_sqnum < new_aeb->sqnum) |
| 552 | *max_sqnum = new_aeb->sqnum; |
| 553 | |
| 554 | err = process_pool_aeb(ubi, ai, vh, new_aeb); |
| 555 | if (err) { |
| 556 | ret = err > 0 ? UBI_BAD_FASTMAP : err; |
| 557 | goto out; |
| 558 | } |
| 559 | } else { |
| 560 | /* We are paranoid and fall back to scanning mode */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 561 | ubi_err(ubi, "fastmap pool PEBs contains damaged PEBs!"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 562 | ret = err > 0 ? UBI_BAD_FASTMAP : err; |
| 563 | goto out; |
| 564 | } |
| 565 | |
| 566 | } |
| 567 | |
| 568 | out: |
| 569 | ubi_free_vid_hdr(ubi, vh); |
| 570 | kfree(ech); |
| 571 | return ret; |
| 572 | } |
| 573 | |
| 574 | /** |
| 575 | * count_fastmap_pebs - Counts the PEBs found by fastmap. |
| 576 | * @ai: The UBI attach info object |
| 577 | */ |
| 578 | static int count_fastmap_pebs(struct ubi_attach_info *ai) |
| 579 | { |
| 580 | struct ubi_ainf_peb *aeb; |
| 581 | struct ubi_ainf_volume *av; |
| 582 | struct rb_node *rb1, *rb2; |
| 583 | int n = 0; |
| 584 | |
| 585 | list_for_each_entry(aeb, &ai->erase, u.list) |
| 586 | n++; |
| 587 | |
| 588 | list_for_each_entry(aeb, &ai->free, u.list) |
| 589 | n++; |
| 590 | |
| 591 | ubi_rb_for_each_entry(rb1, av, &ai->volumes, rb) |
| 592 | ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) |
| 593 | n++; |
| 594 | |
| 595 | return n; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * ubi_attach_fastmap - creates ubi_attach_info from a fastmap. |
| 600 | * @ubi: UBI device object |
| 601 | * @ai: UBI attach info object |
| 602 | * @fm: the fastmap to be attached |
| 603 | * |
| 604 | * Returns 0 on success, UBI_BAD_FASTMAP if the found fastmap was unusable. |
| 605 | * < 0 indicates an internal error. |
| 606 | */ |
| 607 | static int ubi_attach_fastmap(struct ubi_device *ubi, |
| 608 | struct ubi_attach_info *ai, |
| 609 | struct ubi_fastmap_layout *fm) |
| 610 | { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 611 | struct list_head used, free; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 612 | struct ubi_ainf_volume *av; |
| 613 | struct ubi_ainf_peb *aeb, *tmp_aeb, *_tmp_aeb; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 614 | struct ubi_fm_sb *fmsb; |
| 615 | struct ubi_fm_hdr *fmhdr; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 616 | struct ubi_fm_scan_pool *fmpl, *fmpl_wl; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 617 | struct ubi_fm_ec *fmec; |
| 618 | struct ubi_fm_volhdr *fmvhdr; |
| 619 | struct ubi_fm_eba *fm_eba; |
| 620 | int ret, i, j, pool_size, wl_pool_size; |
| 621 | size_t fm_pos = 0, fm_size = ubi->fm_size; |
| 622 | unsigned long long max_sqnum = 0; |
| 623 | void *fm_raw = ubi->fm_buf; |
| 624 | |
| 625 | INIT_LIST_HEAD(&used); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 626 | INIT_LIST_HEAD(&free); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 627 | ai->min_ec = UBI_MAX_ERASECOUNTER; |
| 628 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 629 | fmsb = (struct ubi_fm_sb *)(fm_raw); |
| 630 | ai->max_sqnum = fmsb->sqnum; |
| 631 | fm_pos += sizeof(struct ubi_fm_sb); |
| 632 | if (fm_pos >= fm_size) |
| 633 | goto fail_bad; |
| 634 | |
| 635 | fmhdr = (struct ubi_fm_hdr *)(fm_raw + fm_pos); |
| 636 | fm_pos += sizeof(*fmhdr); |
| 637 | if (fm_pos >= fm_size) |
| 638 | goto fail_bad; |
| 639 | |
| 640 | if (be32_to_cpu(fmhdr->magic) != UBI_FM_HDR_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 641 | ubi_err(ubi, "bad fastmap header magic: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 642 | be32_to_cpu(fmhdr->magic), UBI_FM_HDR_MAGIC); |
| 643 | goto fail_bad; |
| 644 | } |
| 645 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 646 | fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); |
| 647 | fm_pos += sizeof(*fmpl); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 648 | if (fm_pos >= fm_size) |
| 649 | goto fail_bad; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 650 | if (be32_to_cpu(fmpl->magic) != UBI_FM_POOL_MAGIC) { |
| 651 | ubi_err(ubi, "bad fastmap pool magic: 0x%x, expected: 0x%x", |
| 652 | be32_to_cpu(fmpl->magic), UBI_FM_POOL_MAGIC); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 653 | goto fail_bad; |
| 654 | } |
| 655 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 656 | fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); |
| 657 | fm_pos += sizeof(*fmpl_wl); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 658 | if (fm_pos >= fm_size) |
| 659 | goto fail_bad; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 660 | if (be32_to_cpu(fmpl_wl->magic) != UBI_FM_POOL_MAGIC) { |
| 661 | ubi_err(ubi, "bad fastmap WL pool magic: 0x%x, expected: 0x%x", |
| 662 | be32_to_cpu(fmpl_wl->magic), UBI_FM_POOL_MAGIC); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 663 | goto fail_bad; |
| 664 | } |
| 665 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 666 | pool_size = be16_to_cpu(fmpl->size); |
| 667 | wl_pool_size = be16_to_cpu(fmpl_wl->size); |
| 668 | fm->max_pool_size = be16_to_cpu(fmpl->max_size); |
| 669 | fm->max_wl_pool_size = be16_to_cpu(fmpl_wl->max_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 670 | |
| 671 | if (pool_size > UBI_FM_MAX_POOL_SIZE || pool_size < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 672 | ubi_err(ubi, "bad pool size: %i", pool_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 673 | goto fail_bad; |
| 674 | } |
| 675 | |
| 676 | if (wl_pool_size > UBI_FM_MAX_POOL_SIZE || wl_pool_size < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 677 | ubi_err(ubi, "bad WL pool size: %i", wl_pool_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 678 | goto fail_bad; |
| 679 | } |
| 680 | |
| 681 | |
| 682 | if (fm->max_pool_size > UBI_FM_MAX_POOL_SIZE || |
| 683 | fm->max_pool_size < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 684 | ubi_err(ubi, "bad maximal pool size: %i", fm->max_pool_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 685 | goto fail_bad; |
| 686 | } |
| 687 | |
| 688 | if (fm->max_wl_pool_size > UBI_FM_MAX_POOL_SIZE || |
| 689 | fm->max_wl_pool_size < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 690 | ubi_err(ubi, "bad maximal WL pool size: %i", |
| 691 | fm->max_wl_pool_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 692 | goto fail_bad; |
| 693 | } |
| 694 | |
| 695 | /* read EC values from free list */ |
| 696 | for (i = 0; i < be32_to_cpu(fmhdr->free_peb_count); i++) { |
| 697 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 698 | fm_pos += sizeof(*fmec); |
| 699 | if (fm_pos >= fm_size) |
| 700 | goto fail_bad; |
| 701 | |
| 702 | add_aeb(ai, &ai->free, be32_to_cpu(fmec->pnum), |
| 703 | be32_to_cpu(fmec->ec), 0); |
| 704 | } |
| 705 | |
| 706 | /* read EC values from used list */ |
| 707 | for (i = 0; i < be32_to_cpu(fmhdr->used_peb_count); i++) { |
| 708 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 709 | fm_pos += sizeof(*fmec); |
| 710 | if (fm_pos >= fm_size) |
| 711 | goto fail_bad; |
| 712 | |
| 713 | add_aeb(ai, &used, be32_to_cpu(fmec->pnum), |
| 714 | be32_to_cpu(fmec->ec), 0); |
| 715 | } |
| 716 | |
| 717 | /* read EC values from scrub list */ |
| 718 | for (i = 0; i < be32_to_cpu(fmhdr->scrub_peb_count); i++) { |
| 719 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 720 | fm_pos += sizeof(*fmec); |
| 721 | if (fm_pos >= fm_size) |
| 722 | goto fail_bad; |
| 723 | |
| 724 | add_aeb(ai, &used, be32_to_cpu(fmec->pnum), |
| 725 | be32_to_cpu(fmec->ec), 1); |
| 726 | } |
| 727 | |
| 728 | /* read EC values from erase list */ |
| 729 | for (i = 0; i < be32_to_cpu(fmhdr->erase_peb_count); i++) { |
| 730 | fmec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 731 | fm_pos += sizeof(*fmec); |
| 732 | if (fm_pos >= fm_size) |
| 733 | goto fail_bad; |
| 734 | |
| 735 | add_aeb(ai, &ai->erase, be32_to_cpu(fmec->pnum), |
| 736 | be32_to_cpu(fmec->ec), 1); |
| 737 | } |
| 738 | |
| 739 | ai->mean_ec = div_u64(ai->ec_sum, ai->ec_count); |
| 740 | ai->bad_peb_count = be32_to_cpu(fmhdr->bad_peb_count); |
| 741 | |
| 742 | /* Iterate over all volumes and read their EBA table */ |
| 743 | for (i = 0; i < be32_to_cpu(fmhdr->vol_count); i++) { |
| 744 | fmvhdr = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); |
| 745 | fm_pos += sizeof(*fmvhdr); |
| 746 | if (fm_pos >= fm_size) |
| 747 | goto fail_bad; |
| 748 | |
| 749 | if (be32_to_cpu(fmvhdr->magic) != UBI_FM_VHDR_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 750 | ubi_err(ubi, "bad fastmap vol header magic: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 751 | be32_to_cpu(fmvhdr->magic), UBI_FM_VHDR_MAGIC); |
| 752 | goto fail_bad; |
| 753 | } |
| 754 | |
| 755 | av = add_vol(ai, be32_to_cpu(fmvhdr->vol_id), |
| 756 | be32_to_cpu(fmvhdr->used_ebs), |
| 757 | be32_to_cpu(fmvhdr->data_pad), |
| 758 | fmvhdr->vol_type, |
| 759 | be32_to_cpu(fmvhdr->last_eb_bytes)); |
| 760 | |
| 761 | if (!av) |
| 762 | goto fail_bad; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 763 | if (PTR_ERR(av) == -EINVAL) { |
| 764 | ubi_err(ubi, "volume (ID %i) already exists", |
| 765 | fmvhdr->vol_id); |
| 766 | goto fail_bad; |
| 767 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 768 | |
| 769 | ai->vols_found++; |
| 770 | if (ai->highest_vol_id < be32_to_cpu(fmvhdr->vol_id)) |
| 771 | ai->highest_vol_id = be32_to_cpu(fmvhdr->vol_id); |
| 772 | |
| 773 | fm_eba = (struct ubi_fm_eba *)(fm_raw + fm_pos); |
| 774 | fm_pos += sizeof(*fm_eba); |
| 775 | fm_pos += (sizeof(__be32) * be32_to_cpu(fm_eba->reserved_pebs)); |
| 776 | if (fm_pos >= fm_size) |
| 777 | goto fail_bad; |
| 778 | |
| 779 | if (be32_to_cpu(fm_eba->magic) != UBI_FM_EBA_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 780 | ubi_err(ubi, "bad fastmap EBA header magic: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 781 | be32_to_cpu(fm_eba->magic), UBI_FM_EBA_MAGIC); |
| 782 | goto fail_bad; |
| 783 | } |
| 784 | |
| 785 | for (j = 0; j < be32_to_cpu(fm_eba->reserved_pebs); j++) { |
| 786 | int pnum = be32_to_cpu(fm_eba->pnum[j]); |
| 787 | |
| 788 | if ((int)be32_to_cpu(fm_eba->pnum[j]) < 0) |
| 789 | continue; |
| 790 | |
| 791 | aeb = NULL; |
| 792 | list_for_each_entry(tmp_aeb, &used, u.list) { |
| 793 | if (tmp_aeb->pnum == pnum) { |
| 794 | aeb = tmp_aeb; |
| 795 | break; |
| 796 | } |
| 797 | } |
| 798 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 799 | if (!aeb) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 800 | ubi_err(ubi, "PEB %i is in EBA but not in used list", pnum); |
| 801 | goto fail_bad; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | aeb->lnum = j; |
| 805 | |
| 806 | if (av->highest_lnum <= aeb->lnum) |
| 807 | av->highest_lnum = aeb->lnum; |
| 808 | |
| 809 | assign_aeb_to_av(ai, aeb, av); |
| 810 | |
| 811 | dbg_bld("inserting PEB:%i (LEB %i) to vol %i", |
| 812 | aeb->pnum, aeb->lnum, av->vol_id); |
| 813 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 816 | ret = scan_pool(ubi, ai, fmpl->pebs, pool_size, &max_sqnum, &free); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 817 | if (ret) |
| 818 | goto fail; |
| 819 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 820 | ret = scan_pool(ubi, ai, fmpl_wl->pebs, wl_pool_size, &max_sqnum, &free); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 821 | if (ret) |
| 822 | goto fail; |
| 823 | |
| 824 | if (max_sqnum > ai->max_sqnum) |
| 825 | ai->max_sqnum = max_sqnum; |
| 826 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 827 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 828 | list_move_tail(&tmp_aeb->u.list, &ai->free); |
| 829 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 830 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) |
| 831 | list_move_tail(&tmp_aeb->u.list, &ai->erase); |
| 832 | |
| 833 | ubi_assert(list_empty(&free)); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 834 | |
| 835 | /* |
| 836 | * If fastmap is leaking PEBs (must not happen), raise a |
| 837 | * fat warning and fall back to scanning mode. |
| 838 | * We do this here because in ubi_wl_init() it's too late |
| 839 | * and we cannot fall back to scanning. |
| 840 | */ |
| 841 | #ifndef __UBOOT__ |
| 842 | if (WARN_ON(count_fastmap_pebs(ai) != ubi->peb_count - |
| 843 | ai->bad_peb_count - fm->used_blocks)) |
| 844 | goto fail_bad; |
| 845 | #else |
| 846 | if (count_fastmap_pebs(ai) != ubi->peb_count - |
| 847 | ai->bad_peb_count - fm->used_blocks) { |
| 848 | WARN_ON(1); |
| 849 | goto fail_bad; |
| 850 | } |
| 851 | #endif |
| 852 | |
| 853 | return 0; |
| 854 | |
| 855 | fail_bad: |
| 856 | ret = UBI_BAD_FASTMAP; |
| 857 | fail: |
| 858 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &used, u.list) { |
| 859 | list_del(&tmp_aeb->u.list); |
| 860 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
| 861 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 862 | list_for_each_entry_safe(tmp_aeb, _tmp_aeb, &free, u.list) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 863 | list_del(&tmp_aeb->u.list); |
| 864 | kmem_cache_free(ai->aeb_slab_cache, tmp_aeb); |
| 865 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * ubi_scan_fastmap - scan the fastmap. |
| 872 | * @ubi: UBI device object |
| 873 | * @ai: UBI attach info to be filled |
| 874 | * @fm_anchor: The fastmap starts at this PEB |
| 875 | * |
| 876 | * Returns 0 on success, UBI_NO_FASTMAP if no fastmap was found, |
| 877 | * UBI_BAD_FASTMAP if one was found but is not usable. |
| 878 | * < 0 indicates an internal error. |
| 879 | */ |
| 880 | int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai, |
| 881 | int fm_anchor) |
| 882 | { |
| 883 | struct ubi_fm_sb *fmsb, *fmsb2; |
| 884 | struct ubi_vid_hdr *vh; |
| 885 | struct ubi_ec_hdr *ech; |
| 886 | struct ubi_fastmap_layout *fm; |
| 887 | int i, used_blocks, pnum, ret = 0; |
| 888 | size_t fm_size; |
| 889 | __be32 crc, tmp_crc; |
| 890 | unsigned long long sqnum = 0; |
| 891 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 892 | down_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 893 | memset(ubi->fm_buf, 0, ubi->fm_size); |
| 894 | |
| 895 | fmsb = kmalloc(sizeof(*fmsb), GFP_KERNEL); |
| 896 | if (!fmsb) { |
| 897 | ret = -ENOMEM; |
| 898 | goto out; |
| 899 | } |
| 900 | |
| 901 | fm = kzalloc(sizeof(*fm), GFP_KERNEL); |
| 902 | if (!fm) { |
| 903 | ret = -ENOMEM; |
| 904 | kfree(fmsb); |
| 905 | goto out; |
| 906 | } |
| 907 | |
| 908 | ret = ubi_io_read(ubi, fmsb, fm_anchor, ubi->leb_start, sizeof(*fmsb)); |
| 909 | if (ret && ret != UBI_IO_BITFLIPS) |
| 910 | goto free_fm_sb; |
| 911 | else if (ret == UBI_IO_BITFLIPS) |
| 912 | fm->to_be_tortured[0] = 1; |
| 913 | |
| 914 | if (be32_to_cpu(fmsb->magic) != UBI_FM_SB_MAGIC) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 915 | ubi_err(ubi, "bad super block magic: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 916 | be32_to_cpu(fmsb->magic), UBI_FM_SB_MAGIC); |
| 917 | ret = UBI_BAD_FASTMAP; |
| 918 | goto free_fm_sb; |
| 919 | } |
| 920 | |
| 921 | if (fmsb->version != UBI_FM_FMT_VERSION) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 922 | ubi_err(ubi, "bad fastmap version: %i, expected: %i", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 923 | fmsb->version, UBI_FM_FMT_VERSION); |
| 924 | ret = UBI_BAD_FASTMAP; |
| 925 | goto free_fm_sb; |
| 926 | } |
| 927 | |
| 928 | used_blocks = be32_to_cpu(fmsb->used_blocks); |
| 929 | if (used_blocks > UBI_FM_MAX_BLOCKS || used_blocks < 1) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 930 | ubi_err(ubi, "number of fastmap blocks is invalid: %i", |
| 931 | used_blocks); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 932 | ret = UBI_BAD_FASTMAP; |
| 933 | goto free_fm_sb; |
| 934 | } |
| 935 | |
| 936 | fm_size = ubi->leb_size * used_blocks; |
| 937 | if (fm_size != ubi->fm_size) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 938 | ubi_err(ubi, "bad fastmap size: %zi, expected: %zi", |
| 939 | fm_size, ubi->fm_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 940 | ret = UBI_BAD_FASTMAP; |
| 941 | goto free_fm_sb; |
| 942 | } |
| 943 | |
| 944 | ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 945 | if (!ech) { |
| 946 | ret = -ENOMEM; |
| 947 | goto free_fm_sb; |
| 948 | } |
| 949 | |
| 950 | vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); |
| 951 | if (!vh) { |
| 952 | ret = -ENOMEM; |
| 953 | goto free_hdr; |
| 954 | } |
| 955 | |
| 956 | for (i = 0; i < used_blocks; i++) { |
| 957 | int image_seq; |
| 958 | |
| 959 | pnum = be32_to_cpu(fmsb->block_loc[i]); |
| 960 | |
| 961 | if (ubi_io_is_bad(ubi, pnum)) { |
| 962 | ret = UBI_BAD_FASTMAP; |
| 963 | goto free_hdr; |
| 964 | } |
| 965 | |
| 966 | ret = ubi_io_read_ec_hdr(ubi, pnum, ech, 0); |
| 967 | if (ret && ret != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 968 | ubi_err(ubi, "unable to read fastmap block# %i EC (PEB: %i)", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 969 | i, pnum); |
| 970 | if (ret > 0) |
| 971 | ret = UBI_BAD_FASTMAP; |
| 972 | goto free_hdr; |
| 973 | } else if (ret == UBI_IO_BITFLIPS) |
| 974 | fm->to_be_tortured[i] = 1; |
| 975 | |
| 976 | image_seq = be32_to_cpu(ech->image_seq); |
| 977 | if (!ubi->image_seq) |
| 978 | ubi->image_seq = image_seq; |
| 979 | |
| 980 | /* |
| 981 | * Older UBI implementations have image_seq set to zero, so |
| 982 | * we shouldn't fail if image_seq == 0. |
| 983 | */ |
| 984 | if (image_seq && (image_seq != ubi->image_seq)) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 985 | ubi_err(ubi, "wrong image seq:%d instead of %d", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 986 | be32_to_cpu(ech->image_seq), ubi->image_seq); |
| 987 | ret = UBI_BAD_FASTMAP; |
| 988 | goto free_hdr; |
| 989 | } |
| 990 | |
| 991 | ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); |
| 992 | if (ret && ret != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 993 | ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 994 | i, pnum); |
| 995 | goto free_hdr; |
| 996 | } |
| 997 | |
| 998 | if (i == 0) { |
| 999 | if (be32_to_cpu(vh->vol_id) != UBI_FM_SB_VOLUME_ID) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1000 | ubi_err(ubi, "bad fastmap anchor vol_id: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1001 | be32_to_cpu(vh->vol_id), |
| 1002 | UBI_FM_SB_VOLUME_ID); |
| 1003 | ret = UBI_BAD_FASTMAP; |
| 1004 | goto free_hdr; |
| 1005 | } |
| 1006 | } else { |
| 1007 | if (be32_to_cpu(vh->vol_id) != UBI_FM_DATA_VOLUME_ID) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1008 | ubi_err(ubi, "bad fastmap data vol_id: 0x%x, expected: 0x%x", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1009 | be32_to_cpu(vh->vol_id), |
| 1010 | UBI_FM_DATA_VOLUME_ID); |
| 1011 | ret = UBI_BAD_FASTMAP; |
| 1012 | goto free_hdr; |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | if (sqnum < be64_to_cpu(vh->sqnum)) |
| 1017 | sqnum = be64_to_cpu(vh->sqnum); |
| 1018 | |
| 1019 | ret = ubi_io_read(ubi, ubi->fm_buf + (ubi->leb_size * i), pnum, |
| 1020 | ubi->leb_start, ubi->leb_size); |
| 1021 | if (ret && ret != UBI_IO_BITFLIPS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1022 | ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i, " |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1023 | "err: %i)", i, pnum, ret); |
| 1024 | goto free_hdr; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | kfree(fmsb); |
| 1029 | fmsb = NULL; |
| 1030 | |
| 1031 | fmsb2 = (struct ubi_fm_sb *)(ubi->fm_buf); |
| 1032 | tmp_crc = be32_to_cpu(fmsb2->data_crc); |
| 1033 | fmsb2->data_crc = 0; |
| 1034 | crc = crc32(UBI_CRC32_INIT, ubi->fm_buf, fm_size); |
| 1035 | if (crc != tmp_crc) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1036 | ubi_err(ubi, "fastmap data CRC is invalid"); |
| 1037 | ubi_err(ubi, "CRC should be: 0x%x, calc: 0x%x", |
| 1038 | tmp_crc, crc); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1039 | ret = UBI_BAD_FASTMAP; |
| 1040 | goto free_hdr; |
| 1041 | } |
| 1042 | |
| 1043 | fmsb2->sqnum = sqnum; |
| 1044 | |
| 1045 | fm->used_blocks = used_blocks; |
| 1046 | |
| 1047 | ret = ubi_attach_fastmap(ubi, ai, fm); |
| 1048 | if (ret) { |
| 1049 | if (ret > 0) |
| 1050 | ret = UBI_BAD_FASTMAP; |
| 1051 | goto free_hdr; |
| 1052 | } |
| 1053 | |
| 1054 | for (i = 0; i < used_blocks; i++) { |
| 1055 | struct ubi_wl_entry *e; |
| 1056 | |
| 1057 | e = kmem_cache_alloc(ubi_wl_entry_slab, GFP_KERNEL); |
| 1058 | if (!e) { |
| 1059 | while (i--) |
| 1060 | kfree(fm->e[i]); |
| 1061 | |
| 1062 | ret = -ENOMEM; |
| 1063 | goto free_hdr; |
| 1064 | } |
| 1065 | |
| 1066 | e->pnum = be32_to_cpu(fmsb2->block_loc[i]); |
| 1067 | e->ec = be32_to_cpu(fmsb2->block_ec[i]); |
| 1068 | fm->e[i] = e; |
| 1069 | } |
| 1070 | |
| 1071 | ubi->fm = fm; |
| 1072 | ubi->fm_pool.max_size = ubi->fm->max_pool_size; |
| 1073 | ubi->fm_wl_pool.max_size = ubi->fm->max_wl_pool_size; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1074 | ubi_msg(ubi, "attached by fastmap"); |
| 1075 | ubi_msg(ubi, "fastmap pool size: %d", ubi->fm_pool.max_size); |
| 1076 | ubi_msg(ubi, "fastmap WL pool size: %d", |
| 1077 | ubi->fm_wl_pool.max_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1078 | ubi->fm_disabled = 0; |
| 1079 | |
| 1080 | ubi_free_vid_hdr(ubi, vh); |
| 1081 | kfree(ech); |
| 1082 | out: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1083 | up_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1084 | if (ret == UBI_BAD_FASTMAP) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1085 | ubi_err(ubi, "Attach by fastmap failed, doing a full scan!"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1086 | return ret; |
| 1087 | |
| 1088 | free_hdr: |
| 1089 | ubi_free_vid_hdr(ubi, vh); |
| 1090 | kfree(ech); |
| 1091 | free_fm_sb: |
| 1092 | kfree(fmsb); |
| 1093 | kfree(fm); |
| 1094 | goto out; |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * ubi_write_fastmap - writes a fastmap. |
| 1099 | * @ubi: UBI device object |
| 1100 | * @new_fm: the to be written fastmap |
| 1101 | * |
| 1102 | * Returns 0 on success, < 0 indicates an internal error. |
| 1103 | */ |
| 1104 | static int ubi_write_fastmap(struct ubi_device *ubi, |
| 1105 | struct ubi_fastmap_layout *new_fm) |
| 1106 | { |
| 1107 | size_t fm_pos = 0; |
| 1108 | void *fm_raw; |
| 1109 | struct ubi_fm_sb *fmsb; |
| 1110 | struct ubi_fm_hdr *fmh; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1111 | struct ubi_fm_scan_pool *fmpl, *fmpl_wl; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1112 | struct ubi_fm_ec *fec; |
| 1113 | struct ubi_fm_volhdr *fvh; |
| 1114 | struct ubi_fm_eba *feba; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1115 | struct ubi_wl_entry *wl_e; |
| 1116 | struct ubi_volume *vol; |
| 1117 | struct ubi_vid_hdr *avhdr, *dvhdr; |
| 1118 | struct ubi_work *ubi_wrk; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1119 | struct rb_node *tmp_rb; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1120 | int ret, i, j, free_peb_count, used_peb_count, vol_count; |
| 1121 | int scrub_peb_count, erase_peb_count; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1122 | int *seen_pebs = NULL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1123 | |
| 1124 | fm_raw = ubi->fm_buf; |
| 1125 | memset(ubi->fm_buf, 0, ubi->fm_size); |
| 1126 | |
| 1127 | avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); |
| 1128 | if (!avhdr) { |
| 1129 | ret = -ENOMEM; |
| 1130 | goto out; |
| 1131 | } |
| 1132 | |
| 1133 | dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID); |
| 1134 | if (!dvhdr) { |
| 1135 | ret = -ENOMEM; |
| 1136 | goto out_kfree; |
| 1137 | } |
| 1138 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1139 | seen_pebs = init_seen(ubi); |
| 1140 | if (IS_ERR(seen_pebs)) { |
| 1141 | ret = PTR_ERR(seen_pebs); |
| 1142 | goto out_kfree; |
| 1143 | } |
| 1144 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1145 | spin_lock(&ubi->volumes_lock); |
| 1146 | spin_lock(&ubi->wl_lock); |
| 1147 | |
| 1148 | fmsb = (struct ubi_fm_sb *)fm_raw; |
| 1149 | fm_pos += sizeof(*fmsb); |
| 1150 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1151 | |
| 1152 | fmh = (struct ubi_fm_hdr *)(fm_raw + fm_pos); |
| 1153 | fm_pos += sizeof(*fmh); |
| 1154 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1155 | |
| 1156 | fmsb->magic = cpu_to_be32(UBI_FM_SB_MAGIC); |
| 1157 | fmsb->version = UBI_FM_FMT_VERSION; |
| 1158 | fmsb->used_blocks = cpu_to_be32(new_fm->used_blocks); |
| 1159 | /* the max sqnum will be filled in while *reading* the fastmap */ |
| 1160 | fmsb->sqnum = 0; |
| 1161 | |
| 1162 | fmh->magic = cpu_to_be32(UBI_FM_HDR_MAGIC); |
| 1163 | free_peb_count = 0; |
| 1164 | used_peb_count = 0; |
| 1165 | scrub_peb_count = 0; |
| 1166 | erase_peb_count = 0; |
| 1167 | vol_count = 0; |
| 1168 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1169 | fmpl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); |
| 1170 | fm_pos += sizeof(*fmpl); |
| 1171 | fmpl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); |
| 1172 | fmpl->size = cpu_to_be16(ubi->fm_pool.size); |
| 1173 | fmpl->max_size = cpu_to_be16(ubi->fm_pool.max_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1174 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1175 | for (i = 0; i < ubi->fm_pool.size; i++) { |
| 1176 | fmpl->pebs[i] = cpu_to_be32(ubi->fm_pool.pebs[i]); |
| 1177 | set_seen(ubi, ubi->fm_pool.pebs[i], seen_pebs); |
| 1178 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1179 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1180 | fmpl_wl = (struct ubi_fm_scan_pool *)(fm_raw + fm_pos); |
| 1181 | fm_pos += sizeof(*fmpl_wl); |
| 1182 | fmpl_wl->magic = cpu_to_be32(UBI_FM_POOL_MAGIC); |
| 1183 | fmpl_wl->size = cpu_to_be16(ubi->fm_wl_pool.size); |
| 1184 | fmpl_wl->max_size = cpu_to_be16(ubi->fm_wl_pool.max_size); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1185 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1186 | for (i = 0; i < ubi->fm_wl_pool.size; i++) { |
| 1187 | fmpl_wl->pebs[i] = cpu_to_be32(ubi->fm_wl_pool.pebs[i]); |
| 1188 | set_seen(ubi, ubi->fm_wl_pool.pebs[i], seen_pebs); |
| 1189 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1190 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1191 | ubi_for_each_free_peb(ubi, wl_e, tmp_rb) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1192 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 1193 | |
| 1194 | fec->pnum = cpu_to_be32(wl_e->pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1195 | set_seen(ubi, wl_e->pnum, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1196 | fec->ec = cpu_to_be32(wl_e->ec); |
| 1197 | |
| 1198 | free_peb_count++; |
| 1199 | fm_pos += sizeof(*fec); |
| 1200 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1201 | } |
| 1202 | fmh->free_peb_count = cpu_to_be32(free_peb_count); |
| 1203 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1204 | ubi_for_each_used_peb(ubi, wl_e, tmp_rb) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1205 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 1206 | |
| 1207 | fec->pnum = cpu_to_be32(wl_e->pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1208 | set_seen(ubi, wl_e->pnum, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1209 | fec->ec = cpu_to_be32(wl_e->ec); |
| 1210 | |
| 1211 | used_peb_count++; |
| 1212 | fm_pos += sizeof(*fec); |
| 1213 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1214 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1215 | |
| 1216 | ubi_for_each_protected_peb(ubi, i, wl_e) { |
| 1217 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 1218 | |
| 1219 | fec->pnum = cpu_to_be32(wl_e->pnum); |
| 1220 | set_seen(ubi, wl_e->pnum, seen_pebs); |
| 1221 | fec->ec = cpu_to_be32(wl_e->ec); |
| 1222 | |
| 1223 | used_peb_count++; |
| 1224 | fm_pos += sizeof(*fec); |
| 1225 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1226 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1227 | fmh->used_peb_count = cpu_to_be32(used_peb_count); |
| 1228 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1229 | ubi_for_each_scrub_peb(ubi, wl_e, tmp_rb) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1230 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 1231 | |
| 1232 | fec->pnum = cpu_to_be32(wl_e->pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1233 | set_seen(ubi, wl_e->pnum, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1234 | fec->ec = cpu_to_be32(wl_e->ec); |
| 1235 | |
| 1236 | scrub_peb_count++; |
| 1237 | fm_pos += sizeof(*fec); |
| 1238 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1239 | } |
| 1240 | fmh->scrub_peb_count = cpu_to_be32(scrub_peb_count); |
| 1241 | |
| 1242 | |
| 1243 | list_for_each_entry(ubi_wrk, &ubi->works, list) { |
| 1244 | if (ubi_is_erase_work(ubi_wrk)) { |
| 1245 | wl_e = ubi_wrk->e; |
| 1246 | ubi_assert(wl_e); |
| 1247 | |
| 1248 | fec = (struct ubi_fm_ec *)(fm_raw + fm_pos); |
| 1249 | |
| 1250 | fec->pnum = cpu_to_be32(wl_e->pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1251 | set_seen(ubi, wl_e->pnum, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1252 | fec->ec = cpu_to_be32(wl_e->ec); |
| 1253 | |
| 1254 | erase_peb_count++; |
| 1255 | fm_pos += sizeof(*fec); |
| 1256 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1257 | } |
| 1258 | } |
| 1259 | fmh->erase_peb_count = cpu_to_be32(erase_peb_count); |
| 1260 | |
| 1261 | for (i = 0; i < UBI_MAX_VOLUMES + UBI_INT_VOL_COUNT; i++) { |
| 1262 | vol = ubi->volumes[i]; |
| 1263 | |
| 1264 | if (!vol) |
| 1265 | continue; |
| 1266 | |
| 1267 | vol_count++; |
| 1268 | |
| 1269 | fvh = (struct ubi_fm_volhdr *)(fm_raw + fm_pos); |
| 1270 | fm_pos += sizeof(*fvh); |
| 1271 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1272 | |
| 1273 | fvh->magic = cpu_to_be32(UBI_FM_VHDR_MAGIC); |
| 1274 | fvh->vol_id = cpu_to_be32(vol->vol_id); |
| 1275 | fvh->vol_type = vol->vol_type; |
| 1276 | fvh->used_ebs = cpu_to_be32(vol->used_ebs); |
| 1277 | fvh->data_pad = cpu_to_be32(vol->data_pad); |
| 1278 | fvh->last_eb_bytes = cpu_to_be32(vol->last_eb_bytes); |
| 1279 | |
| 1280 | ubi_assert(vol->vol_type == UBI_DYNAMIC_VOLUME || |
| 1281 | vol->vol_type == UBI_STATIC_VOLUME); |
| 1282 | |
| 1283 | feba = (struct ubi_fm_eba *)(fm_raw + fm_pos); |
| 1284 | fm_pos += sizeof(*feba) + (sizeof(__be32) * vol->reserved_pebs); |
| 1285 | ubi_assert(fm_pos <= ubi->fm_size); |
| 1286 | |
| 1287 | for (j = 0; j < vol->reserved_pebs; j++) |
| 1288 | feba->pnum[j] = cpu_to_be32(vol->eba_tbl[j]); |
| 1289 | |
| 1290 | feba->reserved_pebs = cpu_to_be32(j); |
| 1291 | feba->magic = cpu_to_be32(UBI_FM_EBA_MAGIC); |
| 1292 | } |
| 1293 | fmh->vol_count = cpu_to_be32(vol_count); |
| 1294 | fmh->bad_peb_count = cpu_to_be32(ubi->bad_peb_count); |
| 1295 | |
| 1296 | avhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
| 1297 | avhdr->lnum = 0; |
| 1298 | |
| 1299 | spin_unlock(&ubi->wl_lock); |
| 1300 | spin_unlock(&ubi->volumes_lock); |
| 1301 | |
| 1302 | dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); |
| 1303 | ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); |
| 1304 | if (ret) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1305 | ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1306 | goto out_kfree; |
| 1307 | } |
| 1308 | |
| 1309 | for (i = 0; i < new_fm->used_blocks; i++) { |
| 1310 | fmsb->block_loc[i] = cpu_to_be32(new_fm->e[i]->pnum); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1311 | set_seen(ubi, new_fm->e[i]->pnum, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1312 | fmsb->block_ec[i] = cpu_to_be32(new_fm->e[i]->ec); |
| 1313 | } |
| 1314 | |
| 1315 | fmsb->data_crc = 0; |
| 1316 | fmsb->data_crc = cpu_to_be32(crc32(UBI_CRC32_INIT, fm_raw, |
| 1317 | ubi->fm_size)); |
| 1318 | |
| 1319 | for (i = 1; i < new_fm->used_blocks; i++) { |
| 1320 | dvhdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
| 1321 | dvhdr->lnum = cpu_to_be32(i); |
| 1322 | dbg_bld("writing fastmap data to PEB %i sqnum %llu", |
| 1323 | new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); |
| 1324 | ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); |
| 1325 | if (ret) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1326 | ubi_err(ubi, "unable to write vid_hdr to PEB %i!", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1327 | new_fm->e[i]->pnum); |
| 1328 | goto out_kfree; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | for (i = 0; i < new_fm->used_blocks; i++) { |
| 1333 | ret = ubi_io_write(ubi, fm_raw + (i * ubi->leb_size), |
| 1334 | new_fm->e[i]->pnum, ubi->leb_start, ubi->leb_size); |
| 1335 | if (ret) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1336 | ubi_err(ubi, "unable to write fastmap to PEB %i!", |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1337 | new_fm->e[i]->pnum); |
| 1338 | goto out_kfree; |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | ubi_assert(new_fm); |
| 1343 | ubi->fm = new_fm; |
| 1344 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1345 | ret = self_check_seen(ubi, seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1346 | dbg_bld("fastmap written!"); |
| 1347 | |
| 1348 | out_kfree: |
| 1349 | ubi_free_vid_hdr(ubi, avhdr); |
| 1350 | ubi_free_vid_hdr(ubi, dvhdr); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1351 | free_seen(seen_pebs); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1352 | out: |
| 1353 | return ret; |
| 1354 | } |
| 1355 | |
| 1356 | /** |
| 1357 | * erase_block - Manually erase a PEB. |
| 1358 | * @ubi: UBI device object |
| 1359 | * @pnum: PEB to be erased |
| 1360 | * |
| 1361 | * Returns the new EC value on success, < 0 indicates an internal error. |
| 1362 | */ |
| 1363 | static int erase_block(struct ubi_device *ubi, int pnum) |
| 1364 | { |
| 1365 | int ret; |
| 1366 | struct ubi_ec_hdr *ec_hdr; |
| 1367 | long long ec; |
| 1368 | |
| 1369 | ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); |
| 1370 | if (!ec_hdr) |
| 1371 | return -ENOMEM; |
| 1372 | |
| 1373 | ret = ubi_io_read_ec_hdr(ubi, pnum, ec_hdr, 0); |
| 1374 | if (ret < 0) |
| 1375 | goto out; |
| 1376 | else if (ret && ret != UBI_IO_BITFLIPS) { |
| 1377 | ret = -EINVAL; |
| 1378 | goto out; |
| 1379 | } |
| 1380 | |
| 1381 | ret = ubi_io_sync_erase(ubi, pnum, 0); |
| 1382 | if (ret < 0) |
| 1383 | goto out; |
| 1384 | |
| 1385 | ec = be64_to_cpu(ec_hdr->ec); |
| 1386 | ec += ret; |
| 1387 | if (ec > UBI_MAX_ERASECOUNTER) { |
| 1388 | ret = -EINVAL; |
| 1389 | goto out; |
| 1390 | } |
| 1391 | |
| 1392 | ec_hdr->ec = cpu_to_be64(ec); |
| 1393 | ret = ubi_io_write_ec_hdr(ubi, pnum, ec_hdr); |
| 1394 | if (ret < 0) |
| 1395 | goto out; |
| 1396 | |
| 1397 | ret = ec; |
| 1398 | out: |
| 1399 | kfree(ec_hdr); |
| 1400 | return ret; |
| 1401 | } |
| 1402 | |
| 1403 | /** |
| 1404 | * invalidate_fastmap - destroys a fastmap. |
| 1405 | * @ubi: UBI device object |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1406 | * |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1407 | * This function ensures that upon next UBI attach a full scan |
| 1408 | * is issued. We need this if UBI is about to write a new fastmap |
| 1409 | * but is unable to do so. In this case we have two options: |
| 1410 | * a) Make sure that the current fastmap will not be usued upon |
| 1411 | * attach time and contine or b) fall back to RO mode to have the |
| 1412 | * current fastmap in a valid state. |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1413 | * Returns 0 on success, < 0 indicates an internal error. |
| 1414 | */ |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1415 | static int invalidate_fastmap(struct ubi_device *ubi) |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1416 | { |
| 1417 | int ret; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1418 | struct ubi_fastmap_layout *fm; |
| 1419 | struct ubi_wl_entry *e; |
| 1420 | struct ubi_vid_hdr *vh = NULL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1421 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1422 | if (!ubi->fm) |
| 1423 | return 0; |
| 1424 | |
| 1425 | ubi->fm = NULL; |
| 1426 | |
| 1427 | ret = -ENOMEM; |
| 1428 | fm = kzalloc(sizeof(*fm), GFP_KERNEL); |
| 1429 | if (!fm) |
| 1430 | goto out; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1431 | |
| 1432 | vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); |
| 1433 | if (!vh) |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1434 | goto out_free_fm; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1435 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1436 | ret = -ENOSPC; |
| 1437 | e = ubi_wl_get_fm_peb(ubi, 1); |
| 1438 | if (!e) |
| 1439 | goto out_free_fm; |
| 1440 | |
| 1441 | /* |
| 1442 | * Create fake fastmap such that UBI will fall back |
| 1443 | * to scanning mode. |
| 1444 | */ |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1445 | vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1446 | ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh); |
| 1447 | if (ret < 0) { |
| 1448 | ubi_wl_put_fm_peb(ubi, e, 0, 0); |
| 1449 | goto out_free_fm; |
| 1450 | } |
| 1451 | |
| 1452 | fm->used_blocks = 1; |
| 1453 | fm->e[0] = e; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1454 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1455 | ubi->fm = fm; |
| 1456 | |
| 1457 | out: |
| 1458 | ubi_free_vid_hdr(ubi, vh); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1459 | return ret; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1460 | |
| 1461 | out_free_fm: |
| 1462 | kfree(fm); |
| 1463 | goto out; |
| 1464 | } |
| 1465 | |
| 1466 | /** |
| 1467 | * return_fm_pebs - returns all PEBs used by a fastmap back to the |
| 1468 | * WL sub-system. |
| 1469 | * @ubi: UBI device object |
| 1470 | * @fm: fastmap layout object |
| 1471 | */ |
| 1472 | static void return_fm_pebs(struct ubi_device *ubi, |
| 1473 | struct ubi_fastmap_layout *fm) |
| 1474 | { |
| 1475 | int i; |
| 1476 | |
| 1477 | if (!fm) |
| 1478 | return; |
| 1479 | |
| 1480 | for (i = 0; i < fm->used_blocks; i++) { |
| 1481 | if (fm->e[i]) { |
| 1482 | ubi_wl_put_fm_peb(ubi, fm->e[i], i, |
| 1483 | fm->to_be_tortured[i]); |
| 1484 | fm->e[i] = NULL; |
| 1485 | } |
| 1486 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
| 1489 | /** |
| 1490 | * ubi_update_fastmap - will be called by UBI if a volume changes or |
| 1491 | * a fastmap pool becomes full. |
| 1492 | * @ubi: UBI device object |
| 1493 | * |
| 1494 | * Returns 0 on success, < 0 indicates an internal error. |
| 1495 | */ |
| 1496 | int ubi_update_fastmap(struct ubi_device *ubi) |
| 1497 | { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1498 | int ret, i, j; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1499 | struct ubi_fastmap_layout *new_fm, *old_fm; |
| 1500 | struct ubi_wl_entry *tmp_e; |
| 1501 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1502 | down_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1503 | |
| 1504 | ubi_refill_pools(ubi); |
| 1505 | |
| 1506 | if (ubi->ro_mode || ubi->fm_disabled) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1507 | up_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1508 | return 0; |
| 1509 | } |
| 1510 | |
| 1511 | ret = ubi_ensure_anchor_pebs(ubi); |
| 1512 | if (ret) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1513 | up_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1514 | return ret; |
| 1515 | } |
| 1516 | |
| 1517 | new_fm = kzalloc(sizeof(*new_fm), GFP_KERNEL); |
| 1518 | if (!new_fm) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1519 | up_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1520 | return -ENOMEM; |
| 1521 | } |
| 1522 | |
| 1523 | new_fm->used_blocks = ubi->fm_size / ubi->leb_size; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1524 | old_fm = ubi->fm; |
| 1525 | ubi->fm = NULL; |
| 1526 | |
| 1527 | if (new_fm->used_blocks > UBI_FM_MAX_BLOCKS) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1528 | ubi_err(ubi, "fastmap too large"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1529 | ret = -ENOSPC; |
| 1530 | goto err; |
| 1531 | } |
| 1532 | |
| 1533 | for (i = 1; i < new_fm->used_blocks; i++) { |
| 1534 | spin_lock(&ubi->wl_lock); |
| 1535 | tmp_e = ubi_wl_get_fm_peb(ubi, 0); |
| 1536 | spin_unlock(&ubi->wl_lock); |
| 1537 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1538 | if (!tmp_e) { |
| 1539 | if (old_fm && old_fm->e[i]) { |
| 1540 | ret = erase_block(ubi, old_fm->e[i]->pnum); |
| 1541 | if (ret < 0) { |
| 1542 | ubi_err(ubi, "could not erase old fastmap PEB"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1543 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1544 | for (j = 1; j < i; j++) { |
| 1545 | ubi_wl_put_fm_peb(ubi, new_fm->e[j], |
| 1546 | j, 0); |
| 1547 | new_fm->e[j] = NULL; |
| 1548 | } |
| 1549 | goto err; |
| 1550 | } |
| 1551 | new_fm->e[i] = old_fm->e[i]; |
| 1552 | old_fm->e[i] = NULL; |
| 1553 | } else { |
| 1554 | ubi_err(ubi, "could not get any free erase block"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1555 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1556 | for (j = 1; j < i; j++) { |
| 1557 | ubi_wl_put_fm_peb(ubi, new_fm->e[j], j, 0); |
| 1558 | new_fm->e[j] = NULL; |
| 1559 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1560 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1561 | ret = -ENOSPC; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1562 | goto err; |
| 1563 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1564 | } else { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1565 | new_fm->e[i] = tmp_e; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1566 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1567 | if (old_fm && old_fm->e[i]) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1568 | ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, |
| 1569 | old_fm->to_be_tortured[i]); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1570 | old_fm->e[i] = NULL; |
| 1571 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1572 | } |
| 1573 | } |
| 1574 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1575 | /* Old fastmap is larger than the new one */ |
| 1576 | if (old_fm && new_fm->used_blocks < old_fm->used_blocks) { |
| 1577 | for (i = new_fm->used_blocks; i < old_fm->used_blocks; i++) { |
| 1578 | ubi_wl_put_fm_peb(ubi, old_fm->e[i], i, |
| 1579 | old_fm->to_be_tortured[i]); |
| 1580 | old_fm->e[i] = NULL; |
| 1581 | } |
| 1582 | } |
| 1583 | |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1584 | spin_lock(&ubi->wl_lock); |
| 1585 | tmp_e = ubi_wl_get_fm_peb(ubi, 1); |
| 1586 | spin_unlock(&ubi->wl_lock); |
| 1587 | |
| 1588 | if (old_fm) { |
| 1589 | /* no fresh anchor PEB was found, reuse the old one */ |
| 1590 | if (!tmp_e) { |
| 1591 | ret = erase_block(ubi, old_fm->e[0]->pnum); |
| 1592 | if (ret < 0) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1593 | ubi_err(ubi, "could not erase old anchor PEB"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1594 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1595 | for (i = 1; i < new_fm->used_blocks; i++) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1596 | ubi_wl_put_fm_peb(ubi, new_fm->e[i], |
| 1597 | i, 0); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1598 | new_fm->e[i] = NULL; |
| 1599 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1600 | goto err; |
| 1601 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1602 | new_fm->e[0] = old_fm->e[0]; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1603 | new_fm->e[0]->ec = ret; |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1604 | old_fm->e[0] = NULL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1605 | } else { |
| 1606 | /* we've got a new anchor PEB, return the old one */ |
| 1607 | ubi_wl_put_fm_peb(ubi, old_fm->e[0], 0, |
| 1608 | old_fm->to_be_tortured[0]); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1609 | new_fm->e[0] = tmp_e; |
| 1610 | old_fm->e[0] = NULL; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1611 | } |
| 1612 | } else { |
| 1613 | if (!tmp_e) { |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1614 | ubi_err(ubi, "could not find any anchor PEB"); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1615 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1616 | for (i = 1; i < new_fm->used_blocks; i++) { |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1617 | ubi_wl_put_fm_peb(ubi, new_fm->e[i], i, 0); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1618 | new_fm->e[i] = NULL; |
| 1619 | } |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1620 | |
| 1621 | ret = -ENOSPC; |
| 1622 | goto err; |
| 1623 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1624 | new_fm->e[0] = tmp_e; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | down_write(&ubi->work_sem); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1628 | down_write(&ubi->fm_eba_sem); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1629 | ret = ubi_write_fastmap(ubi, new_fm); |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1630 | up_write(&ubi->fm_eba_sem); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1631 | up_write(&ubi->work_sem); |
| 1632 | |
| 1633 | if (ret) |
| 1634 | goto err; |
| 1635 | |
| 1636 | out_unlock: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1637 | up_write(&ubi->fm_protect); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1638 | kfree(old_fm); |
| 1639 | return ret; |
| 1640 | |
| 1641 | err: |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1642 | ubi_warn(ubi, "Unable to write new fastmap, err=%i", ret); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1643 | |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1644 | ret = invalidate_fastmap(ubi); |
| 1645 | if (ret < 0) { |
| 1646 | ubi_err(ubi, "Unable to invalidiate current fastmap!"); |
| 1647 | ubi_ro_mode(ubi); |
| 1648 | } else { |
| 1649 | return_fm_pebs(ubi, old_fm); |
| 1650 | return_fm_pebs(ubi, new_fm); |
| 1651 | ret = 0; |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1652 | } |
Heiko Schocher | 94b66de | 2015-10-22 06:19:21 +0200 | [diff] [blame] | 1653 | |
| 1654 | kfree(new_fm); |
Heiko Schocher | f5895d1 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1655 | goto out_unlock; |
| 1656 | } |