1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (c) International Business Machines Corp., 2006 4 * 5 * Author: Artem Bityutskiy (Битюцкий Артём) 6 */ 7 8 /* 9 * The UBI Eraseblock Association (EBA) sub-system. 10 * 11 * This sub-system is responsible for I/O to/from logical eraseblock. 12 * 13 * Although in this implementation the EBA table is fully kept and managed in 14 * RAM, which assumes poor scalability, it might be (partially) maintained on 15 * flash in future implementations. 16 * 17 * The EBA sub-system implements per-logical eraseblock locking. Before 18 * accessing a logical eraseblock it is locked for reading or writing. The 19 * per-logical eraseblock locking is implemented by means of the lock tree. The 20 * lock tree is an RB-tree which refers all the currently locked logical 21 * eraseblocks. The lock tree elements are &struct ubi_ltree_entry objects. 22 * They are indexed by (@vol_id, @lnum) pairs. 23 * 24 * EBA also maintains the global sequence counter which is incremented each 25 * time a logical eraseblock is mapped to a physical eraseblock and it is 26 * stored in the volume identifier header. This means that each VID header has 27 * a unique sequence number. The sequence number is only increased an we assume 28 * 64 bits is enough to never overflow. 29 */ 30 31 #include <linux/slab.h> 32 #include <linux/crc32.h> 33 #include <linux/err.h> 34 #include "ubi.h" 35 36 /** 37 * struct ubi_eba_entry - structure encoding a single LEB -> PEB association 38 * @pnum: the physical eraseblock number attached to the LEB 39 * 40 * This structure is encoding a LEB -> PEB association. Note that the LEB 41 * number is not stored here, because it is the index used to access the 42 * entries table. 43 */ 44 struct ubi_eba_entry { 45 int pnum; 46 }; 47 48 /** 49 * struct ubi_eba_table - LEB -> PEB association information 50 * @entries: the LEB to PEB mapping (one entry per LEB). 51 * 52 * This structure is private to the EBA logic and should be kept here. 53 * It is encoding the LEB to PEB association table, and is subject to 54 * changes. 55 */ 56 struct ubi_eba_table { 57 struct ubi_eba_entry *entries; 58 }; 59 60 /** 61 * ubi_next_sqnum - get next sequence number. 62 * @ubi: UBI device description object 63 * 64 * This function returns next sequence number to use, which is just the current 65 * global sequence counter value. It also increases the global sequence 66 * counter. 67 */ 68 unsigned long long ubi_next_sqnum(struct ubi_device *ubi) 69 { 70 unsigned long long sqnum; 71 72 spin_lock(&ubi->ltree_lock); 73 sqnum = ubi->global_sqnum++; 74 spin_unlock(&ubi->ltree_lock); 75 76 return sqnum; 77 } 78 79 /** 80 * ubi_get_compat - get compatibility flags of a volume. 81 * @ubi: UBI device description object 82 * @vol_id: volume ID 83 * 84 * This function returns compatibility flags for an internal volume. User 85 * volumes have no compatibility flags, so %0 is returned. 86 */ 87 static int ubi_get_compat(const struct ubi_device *ubi, int vol_id) 88 { 89 if (vol_id == UBI_LAYOUT_VOLUME_ID) 90 return UBI_LAYOUT_VOLUME_COMPAT; 91 return 0; 92 } 93 94 /** 95 * ubi_eba_get_ldesc - get information about a LEB 96 * @vol: volume description object 97 * @lnum: logical eraseblock number 98 * @ldesc: the LEB descriptor to fill 99 * 100 * Used to query information about a specific LEB. 101 * It is currently only returning the physical position of the LEB, but will be 102 * extended to provide more information. 103 */ 104 void ubi_eba_get_ldesc(struct ubi_volume *vol, int lnum, 105 struct ubi_eba_leb_desc *ldesc) 106 { 107 ldesc->lnum = lnum; 108 ldesc->pnum = vol->eba_tbl->entries[lnum].pnum; 109 } 110 111 /** 112 * ubi_eba_create_table - allocate a new EBA table and initialize it with all 113 * LEBs unmapped 114 * @vol: volume containing the EBA table to copy 115 * @nentries: number of entries in the table 116 * 117 * Allocate a new EBA table and initialize it with all LEBs unmapped. 118 * Returns a valid pointer if it succeed, an ERR_PTR() otherwise. 119 */ 120 struct ubi_eba_table *ubi_eba_create_table(struct ubi_volume *vol, 121 int nentries) 122 { 123 struct ubi_eba_table *tbl; 124 int err = -ENOMEM; 125 int i; 126 127 tbl = kzalloc_obj(*tbl, GFP_KERNEL); 128 if (!tbl) 129 return ERR_PTR(-ENOMEM); 130 131 tbl->entries = kmalloc_objs(*tbl->entries, nentries, GFP_KERNEL); 132 if (!tbl->entries) 133 goto err; 134 135 for (i = 0; i < nentries; i++) 136 tbl->entries[i].pnum = UBI_LEB_UNMAPPED; 137 138 return tbl; 139 140 err: 141 kfree(tbl); 142 143 return ERR_PTR(err); 144 } 145 146 /** 147 * ubi_eba_destroy_table - destroy an EBA table 148 * @tbl: the table to destroy 149 * 150 * Destroy an EBA table. 151 */ 152 void ubi_eba_destroy_table(struct ubi_eba_table *tbl) 153 { 154 if (!tbl) 155 return; 156 157 kfree(tbl->entries); 158 kfree(tbl); 159 } 160 161 /** 162 * ubi_eba_copy_table - copy the EBA table attached to vol into another table 163 * @vol: volume containing the EBA table to copy 164 * @dst: destination 165 * @nentries: number of entries to copy 166 * 167 * Copy the EBA table stored in vol into the one pointed by dst. 168 */ 169 void ubi_eba_copy_table(struct ubi_volume *vol, struct ubi_eba_table *dst, 170 int nentries) 171 { 172 struct ubi_eba_table *src; 173 int i; 174 175 ubi_assert(dst && vol && vol->eba_tbl); 176 177 src = vol->eba_tbl; 178 179 for (i = 0; i < nentries; i++) 180 dst->entries[i].pnum = src->entries[i].pnum; 181 } 182 183 /** 184 * ubi_eba_replace_table - assign a new EBA table to a volume 185 * @vol: volume containing the EBA table to copy 186 * @tbl: new EBA table 187 * 188 * Assign a new EBA table to the volume and release the old one. 189 */ 190 void ubi_eba_replace_table(struct ubi_volume *vol, struct ubi_eba_table *tbl) 191 { 192 ubi_eba_destroy_table(vol->eba_tbl); 193 vol->eba_tbl = tbl; 194 } 195 196 /** 197 * ltree_lookup - look up the lock tree. 198 * @ubi: UBI device description object 199 * @vol_id: volume ID 200 * @lnum: logical eraseblock number 201 * 202 * This function returns a pointer to the corresponding &struct ubi_ltree_entry 203 * object if the logical eraseblock is locked and %NULL if it is not. 204 * @ubi->ltree_lock has to be locked. 205 */ 206 static struct ubi_ltree_entry *ltree_lookup(struct ubi_device *ubi, int vol_id, 207 int lnum) 208 { 209 struct rb_node *p; 210 211 p = ubi->ltree.rb_node; 212 while (p) { 213 struct ubi_ltree_entry *le; 214 215 le = rb_entry(p, struct ubi_ltree_entry, rb); 216 217 if (vol_id < le->vol_id) 218 p = p->rb_left; 219 else if (vol_id > le->vol_id) 220 p = p->rb_right; 221 else { 222 if (lnum < le->lnum) 223 p = p->rb_left; 224 else if (lnum > le->lnum) 225 p = p->rb_right; 226 else 227 return le; 228 } 229 } 230 231 return NULL; 232 } 233 234 /** 235 * ltree_add_entry - add new entry to the lock tree. 236 * @ubi: UBI device description object 237 * @vol_id: volume ID 238 * @lnum: logical eraseblock number 239 * 240 * This function adds new entry for logical eraseblock (@vol_id, @lnum) to the 241 * lock tree. If such entry is already there, its usage counter is increased. 242 * Returns pointer to the lock tree entry or %-ENOMEM if memory allocation 243 * failed. 244 */ 245 static struct ubi_ltree_entry *ltree_add_entry(struct ubi_device *ubi, 246 int vol_id, int lnum) 247 { 248 struct ubi_ltree_entry *le, *le1, *le_free; 249 250 le = kmalloc_obj(struct ubi_ltree_entry, GFP_NOFS); 251 if (!le) 252 return ERR_PTR(-ENOMEM); 253 254 le->users = 0; 255 init_rwsem(&le->mutex); 256 le->vol_id = vol_id; 257 le->lnum = lnum; 258 259 spin_lock(&ubi->ltree_lock); 260 le1 = ltree_lookup(ubi, vol_id, lnum); 261 262 if (le1) { 263 /* 264 * This logical eraseblock is already locked. The newly 265 * allocated lock entry is not needed. 266 */ 267 le_free = le; 268 le = le1; 269 } else { 270 struct rb_node **p, *parent = NULL; 271 272 /* 273 * No lock entry, add the newly allocated one to the 274 * @ubi->ltree RB-tree. 275 */ 276 le_free = NULL; 277 278 p = &ubi->ltree.rb_node; 279 while (*p) { 280 parent = *p; 281 le1 = rb_entry(parent, struct ubi_ltree_entry, rb); 282 283 if (vol_id < le1->vol_id) 284 p = &(*p)->rb_left; 285 else if (vol_id > le1->vol_id) 286 p = &(*p)->rb_right; 287 else { 288 ubi_assert(lnum != le1->lnum); 289 if (lnum < le1->lnum) 290 p = &(*p)->rb_left; 291 else 292 p = &(*p)->rb_right; 293 } 294 } 295 296 rb_link_node(&le->rb, parent, p); 297 rb_insert_color(&le->rb, &ubi->ltree); 298 } 299 le->users += 1; 300 spin_unlock(&ubi->ltree_lock); 301 302 kfree(le_free); 303 return le; 304 } 305 306 /** 307 * leb_read_lock - lock logical eraseblock for reading. 308 * @ubi: UBI device description object 309 * @vol_id: volume ID 310 * @lnum: logical eraseblock number 311 * 312 * This function locks a logical eraseblock for reading. Returns zero in case 313 * of success and a negative error code in case of failure. 314 */ 315 static int leb_read_lock(struct ubi_device *ubi, int vol_id, int lnum) 316 { 317 struct ubi_ltree_entry *le; 318 319 le = ltree_add_entry(ubi, vol_id, lnum); 320 if (IS_ERR(le)) 321 return PTR_ERR(le); 322 down_read(&le->mutex); 323 return 0; 324 } 325 326 /** 327 * leb_read_unlock - unlock logical eraseblock. 328 * @ubi: UBI device description object 329 * @vol_id: volume ID 330 * @lnum: logical eraseblock number 331 */ 332 static void leb_read_unlock(struct ubi_device *ubi, int vol_id, int lnum) 333 { 334 struct ubi_ltree_entry *le; 335 336 spin_lock(&ubi->ltree_lock); 337 le = ltree_lookup(ubi, vol_id, lnum); 338 le->users -= 1; 339 ubi_assert(le->users >= 0); 340 up_read(&le->mutex); 341 if (le->users == 0) { 342 rb_erase(&le->rb, &ubi->ltree); 343 kfree(le); 344 } 345 spin_unlock(&ubi->ltree_lock); 346 } 347 348 /** 349 * leb_write_lock - lock logical eraseblock for writing. 350 * @ubi: UBI device description object 351 * @vol_id: volume ID 352 * @lnum: logical eraseblock number 353 * 354 * This function locks a logical eraseblock for writing. Returns zero in case 355 * of success and a negative error code in case of failure. 356 */ 357 static int leb_write_lock(struct ubi_device *ubi, int vol_id, int lnum) 358 { 359 struct ubi_ltree_entry *le; 360 361 le = ltree_add_entry(ubi, vol_id, lnum); 362 if (IS_ERR(le)) 363 return PTR_ERR(le); 364 down_write(&le->mutex); 365 return 0; 366 } 367 368 /** 369 * leb_write_trylock - try to lock logical eraseblock for writing. 370 * @ubi: UBI device description object 371 * @vol_id: volume ID 372 * @lnum: logical eraseblock number 373 * 374 * This function locks a logical eraseblock for writing if there is no 375 * contention and does nothing if there is contention. Returns %0 in case of 376 * success, %1 in case of contention, and a negative error code in case of 377 * failure. 378 */ 379 static int leb_write_trylock(struct ubi_device *ubi, int vol_id, int lnum) 380 { 381 struct ubi_ltree_entry *le; 382 383 le = ltree_add_entry(ubi, vol_id, lnum); 384 if (IS_ERR(le)) 385 return PTR_ERR(le); 386 if (down_write_trylock(&le->mutex)) 387 return 0; 388 389 /* Contention, cancel */ 390 spin_lock(&ubi->ltree_lock); 391 le->users -= 1; 392 ubi_assert(le->users >= 0); 393 if (le->users == 0) { 394 rb_erase(&le->rb, &ubi->ltree); 395 kfree(le); 396 } 397 spin_unlock(&ubi->ltree_lock); 398 399 return 1; 400 } 401 402 /** 403 * leb_write_unlock - unlock logical eraseblock. 404 * @ubi: UBI device description object 405 * @vol_id: volume ID 406 * @lnum: logical eraseblock number 407 */ 408 static void leb_write_unlock(struct ubi_device *ubi, int vol_id, int lnum) 409 { 410 struct ubi_ltree_entry *le; 411 412 spin_lock(&ubi->ltree_lock); 413 le = ltree_lookup(ubi, vol_id, lnum); 414 le->users -= 1; 415 ubi_assert(le->users >= 0); 416 up_write(&le->mutex); 417 if (le->users == 0) { 418 rb_erase(&le->rb, &ubi->ltree); 419 kfree(le); 420 } 421 spin_unlock(&ubi->ltree_lock); 422 } 423 424 /** 425 * ubi_eba_is_mapped - check if a LEB is mapped. 426 * @vol: volume description object 427 * @lnum: logical eraseblock number 428 * 429 * This function returns true if the LEB is mapped, false otherwise. 430 */ 431 bool ubi_eba_is_mapped(struct ubi_volume *vol, int lnum) 432 { 433 return vol->eba_tbl->entries[lnum].pnum >= 0; 434 } 435 436 /** 437 * ubi_eba_unmap_leb - un-map logical eraseblock. 438 * @ubi: UBI device description object 439 * @vol: volume description object 440 * @lnum: logical eraseblock number 441 * 442 * This function un-maps logical eraseblock @lnum and schedules corresponding 443 * physical eraseblock for erasure. Returns zero in case of success and a 444 * negative error code in case of failure. 445 */ 446 int ubi_eba_unmap_leb(struct ubi_device *ubi, struct ubi_volume *vol, 447 int lnum) 448 { 449 int err, pnum, vol_id = vol->vol_id; 450 451 if (ubi->ro_mode) 452 return -EROFS; 453 454 err = leb_write_lock(ubi, vol_id, lnum); 455 if (err) 456 return err; 457 458 pnum = vol->eba_tbl->entries[lnum].pnum; 459 if (pnum < 0) 460 /* This logical eraseblock is already unmapped */ 461 goto out_unlock; 462 463 dbg_eba("erase LEB %d:%d, PEB %d", vol_id, lnum, pnum); 464 465 down_read(&ubi->fm_eba_sem); 466 vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED; 467 up_read(&ubi->fm_eba_sem); 468 err = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 0); 469 470 out_unlock: 471 leb_write_unlock(ubi, vol_id, lnum); 472 return err; 473 } 474 475 #ifdef CONFIG_MTD_UBI_FASTMAP 476 /** 477 * check_mapping - check and fixup a mapping 478 * @ubi: UBI device description object 479 * @vol: volume description object 480 * @lnum: logical eraseblock number 481 * @pnum: physical eraseblock number 482 * 483 * Checks whether a given mapping is valid. Fastmap cannot track LEB unmap 484 * operations, if such an operation is interrupted the mapping still looks 485 * good, but upon first read an ECC is reported to the upper layer. 486 * Normaly during the full-scan at attach time this is fixed, for Fastmap 487 * we have to deal with it while reading. 488 * If the PEB behind a LEB shows this symthom we change the mapping to 489 * %UBI_LEB_UNMAPPED and schedule the PEB for erasure. 490 * 491 * Returns 0 on success, negative error code in case of failure. 492 */ 493 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 494 int *pnum) 495 { 496 int err; 497 struct ubi_vid_io_buf *vidb; 498 struct ubi_vid_hdr *vid_hdr; 499 500 if (!ubi->fast_attach) 501 return 0; 502 503 if (!vol->checkmap || test_bit(lnum, vol->checkmap)) 504 return 0; 505 506 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 507 if (!vidb) 508 return -ENOMEM; 509 510 err = ubi_io_read_vid_hdr(ubi, *pnum, vidb, 0); 511 if (err > 0 && err != UBI_IO_BITFLIPS) { 512 int torture = 0; 513 514 switch (err) { 515 case UBI_IO_FF: 516 case UBI_IO_FF_BITFLIPS: 517 case UBI_IO_BAD_HDR: 518 case UBI_IO_BAD_HDR_EBADMSG: 519 break; 520 default: 521 ubi_assert(0); 522 } 523 524 if (err == UBI_IO_BAD_HDR_EBADMSG || err == UBI_IO_FF_BITFLIPS) 525 torture = 1; 526 527 down_read(&ubi->fm_eba_sem); 528 vol->eba_tbl->entries[lnum].pnum = UBI_LEB_UNMAPPED; 529 up_read(&ubi->fm_eba_sem); 530 ubi_wl_put_peb(ubi, vol->vol_id, lnum, *pnum, torture); 531 532 *pnum = UBI_LEB_UNMAPPED; 533 } else if (err < 0) { 534 ubi_err(ubi, "unable to read VID header back from PEB %i: %i", 535 *pnum, err); 536 537 goto out_free; 538 } else { 539 int found_vol_id, found_lnum; 540 541 ubi_assert(err == 0 || err == UBI_IO_BITFLIPS); 542 543 vid_hdr = ubi_get_vid_hdr(vidb); 544 found_vol_id = be32_to_cpu(vid_hdr->vol_id); 545 found_lnum = be32_to_cpu(vid_hdr->lnum); 546 547 if (found_lnum != lnum || found_vol_id != vol->vol_id) { 548 ubi_err(ubi, "EBA mismatch! PEB %i is LEB %i:%i instead of LEB %i:%i", 549 *pnum, found_vol_id, found_lnum, vol->vol_id, lnum); 550 ubi_ro_mode(ubi); 551 err = -EINVAL; 552 goto out_free; 553 } 554 } 555 556 set_bit(lnum, vol->checkmap); 557 err = 0; 558 559 out_free: 560 ubi_free_vid_buf(vidb); 561 562 return err; 563 } 564 #else 565 static int check_mapping(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 566 int *pnum) 567 { 568 return 0; 569 } 570 #endif 571 572 /** 573 * ubi_eba_read_leb - read data. 574 * @ubi: UBI device description object 575 * @vol: volume description object 576 * @lnum: logical eraseblock number 577 * @buf: buffer to store the read data 578 * @offset: offset from where to read 579 * @len: how many bytes to read 580 * @check: data CRC check flag 581 * 582 * If the logical eraseblock @lnum is unmapped, @buf is filled with 0xFF 583 * bytes. The @check flag only makes sense for static volumes and forces 584 * eraseblock data CRC checking. 585 * 586 * In case of success this function returns zero. In case of a static volume, 587 * if data CRC mismatches - %-EBADMSG is returned. %-EBADMSG may also be 588 * returned for any volume type if an ECC error was detected by the MTD device 589 * driver. Other negative error cored may be returned in case of other errors. 590 */ 591 int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 592 void *buf, int offset, int len, int check) 593 { 594 int err, pnum, scrub = 0, vol_id = vol->vol_id; 595 struct ubi_vid_io_buf *vidb; 596 struct ubi_vid_hdr *vid_hdr; 597 uint32_t crc; 598 599 err = leb_read_lock(ubi, vol_id, lnum); 600 if (err) 601 return err; 602 603 pnum = vol->eba_tbl->entries[lnum].pnum; 604 if (pnum >= 0) { 605 err = check_mapping(ubi, vol, lnum, &pnum); 606 if (err < 0) 607 goto out_unlock; 608 } 609 610 if (pnum == UBI_LEB_UNMAPPED) { 611 /* 612 * The logical eraseblock is not mapped, fill the whole buffer 613 * with 0xFF bytes. The exception is static volumes for which 614 * it is an error to read unmapped logical eraseblocks. 615 */ 616 dbg_eba("read %d bytes from offset %d of LEB %d:%d (unmapped)", 617 len, offset, vol_id, lnum); 618 leb_read_unlock(ubi, vol_id, lnum); 619 ubi_assert(vol->vol_type != UBI_STATIC_VOLUME); 620 memset(buf, 0xFF, len); 621 return 0; 622 } 623 624 dbg_eba("read %d bytes from offset %d of LEB %d:%d, PEB %d", 625 len, offset, vol_id, lnum, pnum); 626 627 if (vol->vol_type == UBI_DYNAMIC_VOLUME) 628 check = 0; 629 630 retry: 631 if (check) { 632 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 633 if (!vidb) { 634 err = -ENOMEM; 635 goto out_unlock; 636 } 637 638 vid_hdr = ubi_get_vid_hdr(vidb); 639 640 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); 641 if (err && err != UBI_IO_BITFLIPS) { 642 if (err > 0) { 643 /* 644 * The header is either absent or corrupted. 645 * The former case means there is a bug - 646 * switch to read-only mode just in case. 647 * The latter case means a real corruption - we 648 * may try to recover data. FIXME: but this is 649 * not implemented. 650 */ 651 if (err == UBI_IO_BAD_HDR_EBADMSG || 652 err == UBI_IO_BAD_HDR) { 653 ubi_warn(ubi, "corrupted VID header at PEB %d, LEB %d:%d", 654 pnum, vol_id, lnum); 655 err = -EBADMSG; 656 } else { 657 /* 658 * Ending up here in the non-Fastmap case 659 * is a clear bug as the VID header had to 660 * be present at scan time to have it referenced. 661 * With fastmap the story is more complicated. 662 * Fastmap has the mapping info without the need 663 * of a full scan. So the LEB could have been 664 * unmapped, Fastmap cannot know this and keeps 665 * the LEB referenced. 666 * This is valid and works as the layer above UBI 667 * has to do bookkeeping about used/referenced 668 * LEBs in any case. 669 */ 670 if (ubi->fast_attach) { 671 err = -EBADMSG; 672 } else { 673 err = -EINVAL; 674 ubi_ro_mode(ubi); 675 } 676 } 677 } 678 goto out_free; 679 } else if (err == UBI_IO_BITFLIPS) 680 scrub = 1; 681 682 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs)); 683 ubi_assert(len == be32_to_cpu(vid_hdr->data_size)); 684 685 crc = be32_to_cpu(vid_hdr->data_crc); 686 ubi_free_vid_buf(vidb); 687 } 688 689 err = ubi_io_read_data(ubi, buf, pnum, offset, len); 690 if (err) { 691 if (err == UBI_IO_BITFLIPS) 692 scrub = 1; 693 else if (mtd_is_eccerr(err)) { 694 if (vol->vol_type == UBI_DYNAMIC_VOLUME) 695 goto out_unlock; 696 scrub = 1; 697 if (!check) { 698 ubi_msg(ubi, "force data checking"); 699 check = 1; 700 goto retry; 701 } 702 } else 703 goto out_unlock; 704 } 705 706 if (check) { 707 uint32_t crc1 = crc32(UBI_CRC32_INIT, buf, len); 708 if (crc1 != crc) { 709 ubi_warn(ubi, "CRC error: calculated %#08x, must be %#08x", 710 crc1, crc); 711 err = -EBADMSG; 712 goto out_unlock; 713 } 714 } 715 716 if (scrub) 717 err = ubi_wl_scrub_peb(ubi, pnum); 718 719 leb_read_unlock(ubi, vol_id, lnum); 720 return err; 721 722 out_free: 723 ubi_free_vid_buf(vidb); 724 out_unlock: 725 leb_read_unlock(ubi, vol_id, lnum); 726 return err; 727 } 728 729 /** 730 * ubi_eba_read_leb_sg - read data into a scatter gather list. 731 * @ubi: UBI device description object 732 * @vol: volume description object 733 * @lnum: logical eraseblock number 734 * @sgl: UBI scatter gather list to store the read data 735 * @offset: offset from where to read 736 * @len: how many bytes to read 737 * @check: data CRC check flag 738 * 739 * This function works exactly like ubi_eba_read_leb(). But instead of 740 * storing the read data into a buffer it writes to an UBI scatter gather 741 * list. 742 */ 743 int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol, 744 struct ubi_sgl *sgl, int lnum, int offset, int len, 745 int check) 746 { 747 int to_read; 748 int ret; 749 struct scatterlist *sg; 750 751 for (;;) { 752 ubi_assert(sgl->list_pos < UBI_MAX_SG_COUNT); 753 sg = &sgl->sg[sgl->list_pos]; 754 if (len < sg->length - sgl->page_pos) 755 to_read = len; 756 else 757 to_read = sg->length - sgl->page_pos; 758 759 ret = ubi_eba_read_leb(ubi, vol, lnum, 760 sg_virt(sg) + sgl->page_pos, offset, 761 to_read, check); 762 if (ret < 0) 763 return ret; 764 765 offset += to_read; 766 len -= to_read; 767 if (!len) { 768 sgl->page_pos += to_read; 769 if (sgl->page_pos == sg->length) { 770 sgl->list_pos++; 771 sgl->page_pos = 0; 772 } 773 774 break; 775 } 776 777 sgl->list_pos++; 778 sgl->page_pos = 0; 779 } 780 781 return ret; 782 } 783 784 /** 785 * try_recover_peb - try to recover from write failure. 786 * @vol: volume description object 787 * @pnum: the physical eraseblock to recover 788 * @lnum: logical eraseblock number 789 * @buf: data which was not written because of the write failure 790 * @offset: offset of the failed write 791 * @len: how many bytes should have been written 792 * @vidb: VID buffer 793 * @retry: whether the caller should retry in case of failure 794 * 795 * This function is called in case of a write failure and moves all good data 796 * from the potentially bad physical eraseblock to a good physical eraseblock. 797 * This function also writes the data which was not written due to the failure. 798 * Returns 0 in case of success, and a negative error code in case of failure. 799 * In case of failure, the %retry parameter is set to false if this is a fatal 800 * error (retrying won't help), and true otherwise. 801 */ 802 static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum, 803 const void *buf, int offset, int len, 804 struct ubi_vid_io_buf *vidb, bool *retry) 805 { 806 struct ubi_device *ubi = vol->ubi; 807 struct ubi_vid_hdr *vid_hdr; 808 int new_pnum, err, vol_id = vol->vol_id, data_size; 809 uint32_t crc; 810 811 *retry = false; 812 813 new_pnum = ubi_wl_get_peb(ubi); 814 if (new_pnum < 0) { 815 err = new_pnum; 816 goto out_put; 817 } 818 819 ubi_msg(ubi, "recover PEB %d, move data to PEB %d", 820 pnum, new_pnum); 821 822 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1); 823 if (err && err != UBI_IO_BITFLIPS) { 824 if (err > 0) 825 err = -EIO; 826 goto out_put; 827 } 828 829 vid_hdr = ubi_get_vid_hdr(vidb); 830 ubi_assert(vid_hdr->vol_type == UBI_VID_DYNAMIC); 831 832 mutex_lock(&ubi->buf_mutex); 833 memset(ubi->peb_buf + offset, 0xFF, len); 834 835 /* Read everything before the area where the write failure happened */ 836 if (offset > 0) { 837 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, offset); 838 if (err && err != UBI_IO_BITFLIPS) 839 goto out_unlock; 840 } 841 842 *retry = true; 843 844 memcpy(ubi->peb_buf + offset, buf, len); 845 846 data_size = offset + len; 847 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); 848 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 849 vid_hdr->copy_flag = 1; 850 vid_hdr->data_size = cpu_to_be32(data_size); 851 vid_hdr->data_crc = cpu_to_be32(crc); 852 err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb); 853 if (err) 854 goto out_unlock; 855 856 err = ubi_io_write_data(ubi, ubi->peb_buf, new_pnum, 0, data_size); 857 858 out_unlock: 859 mutex_unlock(&ubi->buf_mutex); 860 861 if (!err) 862 vol->eba_tbl->entries[lnum].pnum = new_pnum; 863 864 out_put: 865 up_read(&ubi->fm_eba_sem); 866 867 if (!err) { 868 ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); 869 ubi_msg(ubi, "data was successfully recovered"); 870 } else if (new_pnum >= 0) { 871 /* 872 * Bad luck? This physical eraseblock is bad too? Crud. Let's 873 * try to get another one. 874 */ 875 ubi_wl_put_peb(ubi, vol_id, lnum, new_pnum, 1); 876 ubi_warn(ubi, "failed to write to PEB %d", new_pnum); 877 } 878 879 return err; 880 } 881 882 /** 883 * recover_peb - recover from write failure. 884 * @ubi: UBI device description object 885 * @pnum: the physical eraseblock to recover 886 * @vol_id: volume ID 887 * @lnum: logical eraseblock number 888 * @buf: data which was not written because of the write failure 889 * @offset: offset of the failed write 890 * @len: how many bytes should have been written 891 * 892 * This function is called in case of a write failure and moves all good data 893 * from the potentially bad physical eraseblock to a good physical eraseblock. 894 * This function also writes the data which was not written due to the failure. 895 * Returns 0 in case of success, and a negative error code in case of failure. 896 * This function tries %UBI_IO_RETRIES before giving up. 897 */ 898 static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum, 899 const void *buf, int offset, int len) 900 { 901 int err, idx = vol_id2idx(ubi, vol_id), tries; 902 struct ubi_volume *vol = ubi->volumes[idx]; 903 struct ubi_vid_io_buf *vidb; 904 905 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 906 if (!vidb) 907 return -ENOMEM; 908 909 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 910 bool retry; 911 912 err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb, 913 &retry); 914 if (!err || !retry) 915 break; 916 917 ubi_msg(ubi, "try again"); 918 } 919 920 ubi_free_vid_buf(vidb); 921 922 return err; 923 } 924 925 /** 926 * try_write_vid_and_data - try to write VID header and data to a new PEB. 927 * @vol: volume description object 928 * @lnum: logical eraseblock number 929 * @vidb: the VID buffer to write 930 * @buf: buffer containing the data 931 * @offset: where to start writing data 932 * @len: how many bytes should be written 933 * 934 * This function tries to write VID header and data belonging to logical 935 * eraseblock @lnum of volume @vol to a new physical eraseblock. Returns zero 936 * in case of success and a negative error code in case of failure. 937 * In case of error, it is possible that something was still written to the 938 * flash media, but may be some garbage. 939 */ 940 static int try_write_vid_and_data(struct ubi_volume *vol, int lnum, 941 struct ubi_vid_io_buf *vidb, const void *buf, 942 int offset, int len) 943 { 944 struct ubi_device *ubi = vol->ubi; 945 int pnum, opnum, err, err2, vol_id = vol->vol_id; 946 947 pnum = ubi_wl_get_peb(ubi); 948 if (pnum < 0) { 949 err = pnum; 950 goto out_put; 951 } 952 953 opnum = vol->eba_tbl->entries[lnum].pnum; 954 955 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d", 956 len, offset, vol_id, lnum, pnum); 957 958 err = ubi_io_write_vid_hdr(ubi, pnum, vidb); 959 if (err) { 960 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", 961 vol_id, lnum, pnum); 962 goto out_put; 963 } 964 965 if (len) { 966 err = ubi_io_write_data(ubi, buf, pnum, offset, len); 967 if (err) { 968 ubi_warn(ubi, 969 "failed to write %d bytes at offset %d of LEB %d:%d, PEB %d", 970 len, offset, vol_id, lnum, pnum); 971 goto out_put; 972 } 973 } 974 975 vol->eba_tbl->entries[lnum].pnum = pnum; 976 977 out_put: 978 up_read(&ubi->fm_eba_sem); 979 980 if (err && pnum >= 0) { 981 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, pnum, 1); 982 if (err2) { 983 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", 984 pnum, err2); 985 } 986 } else if (!err && opnum >= 0) { 987 err2 = ubi_wl_put_peb(ubi, vol_id, lnum, opnum, 0); 988 if (err2) { 989 ubi_warn(ubi, "failed to return physical eraseblock %d, error %d", 990 opnum, err2); 991 } 992 } 993 994 return err; 995 } 996 997 /** 998 * ubi_eba_write_leb - write data to dynamic volume. 999 * @ubi: UBI device description object 1000 * @vol: volume description object 1001 * @lnum: logical eraseblock number 1002 * @buf: the data to write 1003 * @offset: offset within the logical eraseblock where to write 1004 * @len: how many bytes to write 1005 * 1006 * This function writes data to logical eraseblock @lnum of a dynamic volume 1007 * @vol. Returns zero in case of success and a negative error code in case 1008 * of failure. In case of error, it is possible that something was still 1009 * written to the flash media, but may be some garbage. 1010 * This function retries %UBI_IO_RETRIES times before giving up. 1011 */ 1012 int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum, 1013 const void *buf, int offset, int len) 1014 { 1015 int err, pnum, tries, vol_id = vol->vol_id; 1016 struct ubi_vid_io_buf *vidb; 1017 struct ubi_vid_hdr *vid_hdr; 1018 1019 if (ubi->ro_mode) 1020 return -EROFS; 1021 1022 err = leb_write_lock(ubi, vol_id, lnum); 1023 if (err) 1024 return err; 1025 1026 pnum = vol->eba_tbl->entries[lnum].pnum; 1027 if (pnum >= 0) { 1028 err = check_mapping(ubi, vol, lnum, &pnum); 1029 if (err < 0) 1030 goto out; 1031 } 1032 1033 if (pnum >= 0) { 1034 dbg_eba("write %d bytes at offset %d of LEB %d:%d, PEB %d", 1035 len, offset, vol_id, lnum, pnum); 1036 1037 err = ubi_io_write_data(ubi, buf, pnum, offset, len); 1038 if (err) { 1039 ubi_warn(ubi, "failed to write data to PEB %d", pnum); 1040 if (err == -EIO && ubi->bad_allowed) 1041 err = recover_peb(ubi, pnum, vol_id, lnum, buf, 1042 offset, len); 1043 } 1044 1045 goto out; 1046 } 1047 1048 /* 1049 * The logical eraseblock is not mapped. We have to get a free physical 1050 * eraseblock and write the volume identifier header there first. 1051 */ 1052 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 1053 if (!vidb) { 1054 leb_write_unlock(ubi, vol_id, lnum); 1055 return -ENOMEM; 1056 } 1057 1058 vid_hdr = ubi_get_vid_hdr(vidb); 1059 1060 vid_hdr->vol_type = UBI_VID_DYNAMIC; 1061 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1062 vid_hdr->vol_id = cpu_to_be32(vol_id); 1063 vid_hdr->lnum = cpu_to_be32(lnum); 1064 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 1065 vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 1066 1067 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 1068 err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len); 1069 if (err != -EIO || !ubi->bad_allowed) 1070 break; 1071 1072 /* 1073 * Fortunately, this is the first write operation to this 1074 * physical eraseblock, so just put it and request a new one. 1075 * We assume that if this physical eraseblock went bad, the 1076 * erase code will handle that. 1077 */ 1078 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1079 ubi_msg(ubi, "try another PEB"); 1080 } 1081 1082 ubi_free_vid_buf(vidb); 1083 1084 out: 1085 if (err) 1086 ubi_ro_mode(ubi); 1087 1088 leb_write_unlock(ubi, vol_id, lnum); 1089 1090 return err; 1091 } 1092 1093 /** 1094 * ubi_eba_write_leb_st - write data to static volume. 1095 * @ubi: UBI device description object 1096 * @vol: volume description object 1097 * @lnum: logical eraseblock number 1098 * @buf: data to write 1099 * @len: how many bytes to write 1100 * @used_ebs: how many logical eraseblocks will this volume contain 1101 * 1102 * This function writes data to logical eraseblock @lnum of static volume 1103 * @vol. The @used_ebs argument should contain total number of logical 1104 * eraseblock in this static volume. 1105 * 1106 * When writing to the last logical eraseblock, the @len argument doesn't have 1107 * to be aligned to the minimal I/O unit size. Instead, it has to be equivalent 1108 * to the real data size, although the @buf buffer has to contain the 1109 * alignment. In all other cases, @len has to be aligned. 1110 * 1111 * It is prohibited to write more than once to logical eraseblocks of static 1112 * volumes. This function returns zero in case of success and a negative error 1113 * code in case of failure. 1114 */ 1115 int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol, 1116 int lnum, const void *buf, int len, int used_ebs) 1117 { 1118 int err, tries, data_size = len, vol_id = vol->vol_id; 1119 struct ubi_vid_io_buf *vidb; 1120 struct ubi_vid_hdr *vid_hdr; 1121 uint32_t crc; 1122 1123 if (ubi->ro_mode) 1124 return -EROFS; 1125 1126 if (lnum == used_ebs - 1) 1127 /* If this is the last LEB @len may be unaligned */ 1128 len = ALIGN(data_size, ubi->min_io_size); 1129 else 1130 ubi_assert(!(len & (ubi->min_io_size - 1))); 1131 1132 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 1133 if (!vidb) 1134 return -ENOMEM; 1135 1136 vid_hdr = ubi_get_vid_hdr(vidb); 1137 1138 err = leb_write_lock(ubi, vol_id, lnum); 1139 if (err) 1140 goto out; 1141 1142 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1143 vid_hdr->vol_id = cpu_to_be32(vol_id); 1144 vid_hdr->lnum = cpu_to_be32(lnum); 1145 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 1146 vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 1147 1148 crc = crc32(UBI_CRC32_INIT, buf, data_size); 1149 vid_hdr->vol_type = UBI_VID_STATIC; 1150 vid_hdr->data_size = cpu_to_be32(data_size); 1151 vid_hdr->used_ebs = cpu_to_be32(used_ebs); 1152 vid_hdr->data_crc = cpu_to_be32(crc); 1153 1154 ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0); 1155 1156 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 1157 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len); 1158 if (err != -EIO || !ubi->bad_allowed) 1159 break; 1160 1161 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1162 ubi_msg(ubi, "try another PEB"); 1163 } 1164 1165 if (err) 1166 ubi_ro_mode(ubi); 1167 1168 leb_write_unlock(ubi, vol_id, lnum); 1169 1170 out: 1171 ubi_free_vid_buf(vidb); 1172 1173 return err; 1174 } 1175 1176 /* 1177 * ubi_eba_atomic_leb_change - change logical eraseblock atomically. 1178 * @ubi: UBI device description object 1179 * @vol: volume description object 1180 * @lnum: logical eraseblock number 1181 * @buf: data to write 1182 * @len: how many bytes to write 1183 * 1184 * This function changes the contents of a logical eraseblock atomically. @buf 1185 * has to contain new logical eraseblock data, and @len - the length of the 1186 * data, which has to be aligned. This function guarantees that in case of an 1187 * unclean reboot the old contents is preserved. Returns zero in case of 1188 * success and a negative error code in case of failure. 1189 * 1190 * UBI reserves one LEB for the "atomic LEB change" operation, so only one 1191 * LEB change may be done at a time. This is ensured by @ubi->alc_mutex. 1192 */ 1193 int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, 1194 int lnum, const void *buf, int len) 1195 { 1196 int err, tries, vol_id = vol->vol_id; 1197 struct ubi_vid_io_buf *vidb; 1198 struct ubi_vid_hdr *vid_hdr; 1199 uint32_t crc; 1200 1201 if (ubi->ro_mode) 1202 return -EROFS; 1203 1204 if (len == 0) { 1205 /* 1206 * Special case when data length is zero. In this case the LEB 1207 * has to be unmapped and mapped somewhere else. 1208 */ 1209 err = ubi_eba_unmap_leb(ubi, vol, lnum); 1210 if (err) 1211 return err; 1212 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); 1213 } 1214 1215 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS); 1216 if (!vidb) 1217 return -ENOMEM; 1218 1219 vid_hdr = ubi_get_vid_hdr(vidb); 1220 1221 mutex_lock(&ubi->alc_mutex); 1222 err = leb_write_lock(ubi, vol_id, lnum); 1223 if (err) 1224 goto out_mutex; 1225 1226 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1227 vid_hdr->vol_id = cpu_to_be32(vol_id); 1228 vid_hdr->lnum = cpu_to_be32(lnum); 1229 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 1230 vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 1231 1232 crc = crc32(UBI_CRC32_INIT, buf, len); 1233 vid_hdr->vol_type = UBI_VID_DYNAMIC; 1234 vid_hdr->data_size = cpu_to_be32(len); 1235 vid_hdr->copy_flag = 1; 1236 vid_hdr->data_crc = cpu_to_be32(crc); 1237 1238 dbg_eba("change LEB %d:%d", vol_id, lnum); 1239 1240 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 1241 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len); 1242 if (err != -EIO || !ubi->bad_allowed) 1243 break; 1244 1245 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1246 ubi_msg(ubi, "try another PEB"); 1247 } 1248 1249 /* 1250 * This flash device does not admit of bad eraseblocks or 1251 * something nasty and unexpected happened. Switch to read-only 1252 * mode just in case. 1253 */ 1254 if (err) 1255 ubi_ro_mode(ubi); 1256 1257 leb_write_unlock(ubi, vol_id, lnum); 1258 1259 out_mutex: 1260 mutex_unlock(&ubi->alc_mutex); 1261 ubi_free_vid_buf(vidb); 1262 return err; 1263 } 1264 1265 /** 1266 * is_error_sane - check whether a read error is sane. 1267 * @err: code of the error happened during reading 1268 * 1269 * This is a helper function for 'ubi_eba_copy_leb()' which is called when we 1270 * cannot read data from the target PEB (an error @err happened). If the error 1271 * code is sane, then we treat this error as non-fatal. Otherwise the error is 1272 * fatal and UBI will be switched to R/O mode later. 1273 * 1274 * The idea is that we try not to switch to R/O mode if the read error is 1275 * something which suggests there was a real read problem. E.g., %-EIO. Or a 1276 * memory allocation failed (-%ENOMEM). Otherwise, it is safer to switch to R/O 1277 * mode, simply because we do not know what happened at the MTD level, and we 1278 * cannot handle this. E.g., the underlying driver may have become crazy, and 1279 * it is safer to switch to R/O mode to preserve the data. 1280 * 1281 * And bear in mind, this is about reading from the target PEB, i.e. the PEB 1282 * which we have just written. 1283 */ 1284 static int is_error_sane(int err) 1285 { 1286 if (err == -EIO || err == -ENOMEM || err == UBI_IO_BAD_HDR || 1287 err == UBI_IO_BAD_HDR_EBADMSG || err == -ETIMEDOUT) 1288 return 0; 1289 return 1; 1290 } 1291 1292 /** 1293 * ubi_eba_copy_leb - copy logical eraseblock. 1294 * @ubi: UBI device description object 1295 * @from: physical eraseblock number from where to copy 1296 * @to: physical eraseblock number where to copy 1297 * @vidb: data structure from where the VID header is derived 1298 * 1299 * This function copies logical eraseblock from physical eraseblock @from to 1300 * physical eraseblock @to. The @vid_hdr buffer may be changed by this 1301 * function. Returns: 1302 * o %0 in case of success; 1303 * o %MOVE_CANCEL_RACE, %MOVE_TARGET_WR_ERR, %MOVE_TARGET_BITFLIPS, etc; 1304 * o a negative error code in case of failure. 1305 */ 1306 int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, 1307 struct ubi_vid_io_buf *vidb) 1308 { 1309 int err, vol_id, lnum, data_size, aldata_size, idx; 1310 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb); 1311 struct ubi_volume *vol; 1312 uint32_t crc; 1313 1314 ubi_assert(rwsem_is_locked(&ubi->fm_eba_sem)); 1315 1316 vol_id = be32_to_cpu(vid_hdr->vol_id); 1317 lnum = be32_to_cpu(vid_hdr->lnum); 1318 1319 dbg_wl("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to); 1320 1321 if (vid_hdr->vol_type == UBI_VID_STATIC) { 1322 data_size = be32_to_cpu(vid_hdr->data_size); 1323 aldata_size = ALIGN(data_size, ubi->min_io_size); 1324 } else 1325 data_size = aldata_size = 1326 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad); 1327 1328 idx = vol_id2idx(ubi, vol_id); 1329 spin_lock(&ubi->volumes_lock); 1330 /* 1331 * Note, we may race with volume deletion, which means that the volume 1332 * this logical eraseblock belongs to might be being deleted. Since the 1333 * volume deletion un-maps all the volume's logical eraseblocks, it will 1334 * be locked in 'ubi_wl_put_peb()' and wait for the WL worker to finish. 1335 */ 1336 vol = ubi->volumes[idx]; 1337 spin_unlock(&ubi->volumes_lock); 1338 if (!vol) { 1339 /* No need to do further work, cancel */ 1340 dbg_wl("volume %d is being removed, cancel", vol_id); 1341 return MOVE_CANCEL_RACE; 1342 } 1343 1344 /* 1345 * We do not want anybody to write to this logical eraseblock while we 1346 * are moving it, so lock it. 1347 * 1348 * Note, we are using non-waiting locking here, because we cannot sleep 1349 * on the LEB, since it may cause deadlocks. Indeed, imagine a task is 1350 * unmapping the LEB which is mapped to the PEB we are going to move 1351 * (@from). This task locks the LEB and goes sleep in the 1352 * 'ubi_wl_put_peb()' function on the @ubi->move_mutex. In turn, we are 1353 * holding @ubi->move_mutex and go sleep on the LEB lock. So, if the 1354 * LEB is already locked, we just do not move it and return 1355 * %MOVE_RETRY. Note, we do not return %MOVE_CANCEL_RACE here because 1356 * we do not know the reasons of the contention - it may be just a 1357 * normal I/O on this LEB, so we want to re-try. 1358 */ 1359 err = leb_write_trylock(ubi, vol_id, lnum); 1360 if (err) { 1361 dbg_wl("contention on LEB %d:%d, cancel", vol_id, lnum); 1362 return MOVE_RETRY; 1363 } 1364 1365 /* 1366 * The LEB might have been put meanwhile, and the task which put it is 1367 * probably waiting on @ubi->move_mutex. No need to continue the work, 1368 * cancel it. 1369 */ 1370 if (vol->eba_tbl->entries[lnum].pnum != from) { 1371 dbg_wl("LEB %d:%d is no longer mapped to PEB %d, mapped to PEB %d, cancel", 1372 vol_id, lnum, from, vol->eba_tbl->entries[lnum].pnum); 1373 err = MOVE_CANCEL_RACE; 1374 goto out_unlock_leb; 1375 } 1376 1377 /* 1378 * OK, now the LEB is locked and we can safely start moving it. Since 1379 * this function utilizes the @ubi->peb_buf buffer which is shared 1380 * with some other functions - we lock the buffer by taking the 1381 * @ubi->buf_mutex. 1382 */ 1383 mutex_lock(&ubi->buf_mutex); 1384 dbg_wl("read %d bytes of data", aldata_size); 1385 err = ubi_io_read_data(ubi, ubi->peb_buf, from, 0, aldata_size); 1386 if (err && err != UBI_IO_BITFLIPS) { 1387 ubi_warn(ubi, "error %d while reading data from PEB %d", 1388 err, from); 1389 err = MOVE_SOURCE_RD_ERR; 1390 goto out_unlock_buf; 1391 } 1392 1393 /* 1394 * Now we have got to calculate how much data we have to copy. In 1395 * case of a static volume it is fairly easy - the VID header contains 1396 * the data size. In case of a dynamic volume it is more difficult - we 1397 * have to read the contents, cut 0xFF bytes from the end and copy only 1398 * the first part. We must do this to avoid writing 0xFF bytes as it 1399 * may have some side-effects. And not only this. It is important not 1400 * to include those 0xFFs to CRC because later the they may be filled 1401 * by data. 1402 */ 1403 if (vid_hdr->vol_type == UBI_VID_DYNAMIC) 1404 aldata_size = data_size = 1405 ubi_calc_data_len(ubi, ubi->peb_buf, data_size); 1406 1407 cond_resched(); 1408 crc = crc32(UBI_CRC32_INIT, ubi->peb_buf, data_size); 1409 cond_resched(); 1410 1411 /* 1412 * It may turn out to be that the whole @from physical eraseblock 1413 * contains only 0xFF bytes. Then we have to only write the VID header 1414 * and do not write any data. This also means we should not set 1415 * @vid_hdr->copy_flag, @vid_hdr->data_size, and @vid_hdr->data_crc. 1416 */ 1417 if (data_size > 0) { 1418 vid_hdr->copy_flag = 1; 1419 vid_hdr->data_size = cpu_to_be32(data_size); 1420 vid_hdr->data_crc = cpu_to_be32(crc); 1421 } 1422 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1423 1424 err = ubi_io_write_vid_hdr(ubi, to, vidb); 1425 if (err) { 1426 if (err == -EIO) 1427 err = MOVE_TARGET_WR_ERR; 1428 goto out_unlock_buf; 1429 } 1430 1431 cond_resched(); 1432 1433 /* Read the VID header back and check if it was written correctly */ 1434 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1); 1435 if (err) { 1436 if (err != UBI_IO_BITFLIPS) { 1437 ubi_warn(ubi, "error %d while reading VID header back from PEB %d", 1438 err, to); 1439 if (is_error_sane(err)) 1440 err = MOVE_TARGET_RD_ERR; 1441 } else 1442 err = MOVE_TARGET_BITFLIPS; 1443 goto out_unlock_buf; 1444 } 1445 1446 if (data_size > 0) { 1447 err = ubi_io_write_data(ubi, ubi->peb_buf, to, 0, aldata_size); 1448 if (err) { 1449 if (err == -EIO) 1450 err = MOVE_TARGET_WR_ERR; 1451 goto out_unlock_buf; 1452 } 1453 1454 cond_resched(); 1455 } 1456 1457 ubi_assert(vol->eba_tbl->entries[lnum].pnum == from); 1458 1459 /** 1460 * The volumes_lock lock is needed here to prevent the expired old eba_tbl 1461 * being updated when the eba_tbl is copied in the ubi_resize_volume() process. 1462 */ 1463 spin_lock(&ubi->volumes_lock); 1464 vol->eba_tbl->entries[lnum].pnum = to; 1465 spin_unlock(&ubi->volumes_lock); 1466 1467 out_unlock_buf: 1468 mutex_unlock(&ubi->buf_mutex); 1469 out_unlock_leb: 1470 leb_write_unlock(ubi, vol_id, lnum); 1471 return err; 1472 } 1473 1474 /** 1475 * print_rsvd_warning - warn about not having enough reserved PEBs. 1476 * @ubi: UBI device description object 1477 * @ai: UBI attach info object 1478 * 1479 * This is a helper function for 'ubi_eba_init()' which is called when UBI 1480 * cannot reserve enough PEBs for bad block handling. This function makes a 1481 * decision whether we have to print a warning or not. The algorithm is as 1482 * follows: 1483 * o if this is a new UBI image, then just print the warning 1484 * o if this is an UBI image which has already been used for some time, print 1485 * a warning only if we can reserve less than 10% of the expected amount of 1486 * the reserved PEB. 1487 * 1488 * The idea is that when UBI is used, PEBs become bad, and the reserved pool 1489 * of PEBs becomes smaller, which is normal and we do not want to scare users 1490 * with a warning every time they attach the MTD device. This was an issue 1491 * reported by real users. 1492 */ 1493 static void print_rsvd_warning(struct ubi_device *ubi, 1494 struct ubi_attach_info *ai) 1495 { 1496 /* 1497 * The 1 << 18 (256KiB) number is picked randomly, just a reasonably 1498 * large number to distinguish between newly flashed and used images. 1499 */ 1500 if (ai->max_sqnum > (1 << 18)) { 1501 int min = ubi->beb_rsvd_level / 10; 1502 1503 if (!min) 1504 min = 1; 1505 if (ubi->beb_rsvd_pebs > min) 1506 return; 1507 } 1508 1509 ubi_warn(ubi, "cannot reserve enough PEBs for bad PEB handling, reserved %d, need %d", 1510 ubi->beb_rsvd_pebs, ubi->beb_rsvd_level); 1511 if (ubi->corr_peb_count) 1512 ubi_warn(ubi, "%d PEBs are corrupted and not used", 1513 ubi->corr_peb_count); 1514 } 1515 1516 /** 1517 * self_check_eba - run a self check on the EBA table constructed by fastmap. 1518 * @ubi: UBI device description object 1519 * @ai_fastmap: UBI attach info object created by fastmap 1520 * @ai_scan: UBI attach info object created by scanning 1521 * 1522 * Returns < 0 in case of an internal error, 0 otherwise. 1523 * If a bad EBA table entry was found it will be printed out and 1524 * ubi_assert() triggers. 1525 */ 1526 int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, 1527 struct ubi_attach_info *ai_scan) 1528 { 1529 int i, j, num_volumes, ret = 0; 1530 int **scan_eba, **fm_eba; 1531 struct ubi_ainf_volume *av; 1532 struct ubi_volume *vol; 1533 struct ubi_ainf_peb *aeb; 1534 struct rb_node *rb; 1535 1536 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; 1537 1538 scan_eba = kmalloc_objs(*scan_eba, num_volumes, GFP_KERNEL); 1539 if (!scan_eba) 1540 return -ENOMEM; 1541 1542 fm_eba = kmalloc_objs(*fm_eba, num_volumes, GFP_KERNEL); 1543 if (!fm_eba) { 1544 kfree(scan_eba); 1545 return -ENOMEM; 1546 } 1547 1548 for (i = 0; i < num_volumes; i++) { 1549 vol = ubi->volumes[i]; 1550 if (!vol) 1551 continue; 1552 1553 scan_eba[i] = kmalloc_objs(**scan_eba, vol->reserved_pebs, 1554 GFP_KERNEL); 1555 if (!scan_eba[i]) { 1556 ret = -ENOMEM; 1557 goto out_free; 1558 } 1559 1560 fm_eba[i] = kmalloc_objs(**fm_eba, vol->reserved_pebs, 1561 GFP_KERNEL); 1562 if (!fm_eba[i]) { 1563 ret = -ENOMEM; 1564 kfree(scan_eba[i]); 1565 goto out_free; 1566 } 1567 1568 for (j = 0; j < vol->reserved_pebs; j++) 1569 scan_eba[i][j] = fm_eba[i][j] = UBI_LEB_UNMAPPED; 1570 1571 av = ubi_find_av(ai_scan, idx2vol_id(ubi, i)); 1572 if (!av) 1573 continue; 1574 1575 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) 1576 scan_eba[i][aeb->lnum] = aeb->pnum; 1577 1578 av = ubi_find_av(ai_fastmap, idx2vol_id(ubi, i)); 1579 if (!av) 1580 continue; 1581 1582 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) 1583 fm_eba[i][aeb->lnum] = aeb->pnum; 1584 1585 for (j = 0; j < vol->reserved_pebs; j++) { 1586 if (scan_eba[i][j] != fm_eba[i][j]) { 1587 if (scan_eba[i][j] == UBI_LEB_UNMAPPED || 1588 fm_eba[i][j] == UBI_LEB_UNMAPPED) 1589 continue; 1590 1591 ubi_err(ubi, "LEB:%i:%i is PEB:%i instead of %i!", 1592 vol->vol_id, j, fm_eba[i][j], 1593 scan_eba[i][j]); 1594 ubi_assert(0); 1595 } 1596 } 1597 } 1598 1599 out_free: 1600 while (--i >= 0) { 1601 if (!ubi->volumes[i]) 1602 continue; 1603 1604 kfree(scan_eba[i]); 1605 kfree(fm_eba[i]); 1606 } 1607 1608 kfree(scan_eba); 1609 kfree(fm_eba); 1610 return ret; 1611 } 1612 1613 /** 1614 * ubi_eba_init - initialize the EBA sub-system using attaching information. 1615 * @ubi: UBI device description object 1616 * @ai: attaching information 1617 * 1618 * This function returns zero in case of success and a negative error code in 1619 * case of failure. 1620 */ 1621 int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai) 1622 { 1623 int i, err, num_volumes; 1624 struct ubi_ainf_volume *av; 1625 struct ubi_volume *vol; 1626 struct ubi_ainf_peb *aeb; 1627 struct rb_node *rb; 1628 1629 dbg_eba("initialize EBA sub-system"); 1630 1631 spin_lock_init(&ubi->ltree_lock); 1632 mutex_init(&ubi->alc_mutex); 1633 ubi->ltree = RB_ROOT; 1634 1635 ubi->global_sqnum = ai->max_sqnum + 1; 1636 num_volumes = ubi->vtbl_slots + UBI_INT_VOL_COUNT; 1637 1638 for (i = 0; i < num_volumes; i++) { 1639 struct ubi_eba_table *tbl; 1640 1641 vol = ubi->volumes[i]; 1642 if (!vol) 1643 continue; 1644 1645 cond_resched(); 1646 1647 tbl = ubi_eba_create_table(vol, vol->reserved_pebs); 1648 if (IS_ERR(tbl)) { 1649 err = PTR_ERR(tbl); 1650 goto out_free; 1651 } 1652 1653 ubi_eba_replace_table(vol, tbl); 1654 1655 av = ubi_find_av(ai, idx2vol_id(ubi, i)); 1656 if (!av) 1657 continue; 1658 1659 ubi_rb_for_each_entry(rb, aeb, &av->root, u.rb) { 1660 if (aeb->lnum >= vol->reserved_pebs) { 1661 /* 1662 * This may happen in case of an unclean reboot 1663 * during re-size. 1664 */ 1665 ubi_move_aeb_to_list(av, aeb, &ai->erase); 1666 } else { 1667 struct ubi_eba_entry *entry; 1668 1669 entry = &vol->eba_tbl->entries[aeb->lnum]; 1670 entry->pnum = aeb->pnum; 1671 } 1672 } 1673 } 1674 1675 if (ubi->avail_pebs < EBA_RESERVED_PEBS) { 1676 ubi_err(ubi, "no enough physical eraseblocks (%d, need %d)", 1677 ubi->avail_pebs, EBA_RESERVED_PEBS); 1678 if (ubi->corr_peb_count) 1679 ubi_err(ubi, "%d PEBs are corrupted and not used", 1680 ubi->corr_peb_count); 1681 err = -ENOSPC; 1682 goto out_free; 1683 } 1684 ubi->avail_pebs -= EBA_RESERVED_PEBS; 1685 ubi->rsvd_pebs += EBA_RESERVED_PEBS; 1686 1687 if (ubi->bad_allowed) { 1688 ubi_calculate_reserved(ubi); 1689 1690 if (ubi->avail_pebs < ubi->beb_rsvd_level) { 1691 /* No enough free physical eraseblocks */ 1692 ubi->beb_rsvd_pebs = ubi->avail_pebs; 1693 print_rsvd_warning(ubi, ai); 1694 } else 1695 ubi->beb_rsvd_pebs = ubi->beb_rsvd_level; 1696 1697 ubi->avail_pebs -= ubi->beb_rsvd_pebs; 1698 ubi->rsvd_pebs += ubi->beb_rsvd_pebs; 1699 } 1700 1701 dbg_eba("EBA sub-system is initialized"); 1702 return 0; 1703 1704 out_free: 1705 for (i = 0; i < num_volumes; i++) { 1706 if (!ubi->volumes[i]) 1707 continue; 1708 ubi_eba_replace_table(ubi->volumes[i], NULL); 1709 } 1710 return err; 1711 } 1712