attach.c (7b6b749b125a93d673ba12977007dfbd65a61c32) | attach.c (3291b52f9ff0acc80a8ee3f92a960db937dccecb) |
---|---|
1/* 2 * Copyright (c) International Business Machines Corp., 2006 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * --- 439 unchanged lines hidden (view full) --- 448 * o bit 2 is cleared: the older LEB is not corrupted; 449 * o bit 2 is set: the older LEB is corrupted. 450 */ 451int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, 452 int pnum, const struct ubi_vid_hdr *vid_hdr) 453{ 454 int len, err, second_is_newer, bitflips = 0, corrupted = 0; 455 uint32_t data_crc, crc; | 1/* 2 * Copyright (c) International Business Machines Corp., 2006 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * --- 439 unchanged lines hidden (view full) --- 448 * o bit 2 is cleared: the older LEB is not corrupted; 449 * o bit 2 is set: the older LEB is corrupted. 450 */ 451int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb, 452 int pnum, const struct ubi_vid_hdr *vid_hdr) 453{ 454 int len, err, second_is_newer, bitflips = 0, corrupted = 0; 455 uint32_t data_crc, crc; |
456 struct ubi_vid_hdr *vh = NULL; | 456 struct ubi_vid_io_buf *vidb = NULL; |
457 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); 458 459 if (sqnum2 == aeb->sqnum) { 460 /* 461 * This must be a really ancient UBI image which has been 462 * created before sequence numbers support has been added. At 463 * that times we used 32-bit LEB versions stored in logical 464 * eraseblocks. That was before UBI got into mainline. We do not --- 26 unchanged lines hidden (view full) --- 491 } else { 492 if (!aeb->copy_flag) { 493 /* It is not a copy, so it is newer */ 494 dbg_bld("first PEB %d is newer, copy_flag is unset", 495 pnum); 496 return bitflips << 1; 497 } 498 | 457 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum); 458 459 if (sqnum2 == aeb->sqnum) { 460 /* 461 * This must be a really ancient UBI image which has been 462 * created before sequence numbers support has been added. At 463 * that times we used 32-bit LEB versions stored in logical 464 * eraseblocks. That was before UBI got into mainline. We do not --- 26 unchanged lines hidden (view full) --- 491 } else { 492 if (!aeb->copy_flag) { 493 /* It is not a copy, so it is newer */ 494 dbg_bld("first PEB %d is newer, copy_flag is unset", 495 pnum); 496 return bitflips << 1; 497 } 498 |
499 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 500 if (!vh) | 499 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 500 if (!vidb) |
501 return -ENOMEM; 502 503 pnum = aeb->pnum; | 501 return -ENOMEM; 502 503 pnum = aeb->pnum; |
504 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); | 504 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); |
505 if (err) { 506 if (err == UBI_IO_BITFLIPS) 507 bitflips = 1; 508 else { 509 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d", 510 pnum, err); 511 if (err > 0) 512 err = -EIO; 513 514 goto out_free_vidh; 515 } 516 } 517 | 505 if (err) { 506 if (err == UBI_IO_BITFLIPS) 507 bitflips = 1; 508 else { 509 ubi_err(ubi, "VID of PEB %d header is bad, but it was OK earlier, err %d", 510 pnum, err); 511 if (err > 0) 512 err = -EIO; 513 514 goto out_free_vidh; 515 } 516 } 517 |
518 vid_hdr = vh; | 518 vid_hdr = ubi_get_vid_hdr(vidb); |
519 } 520 521 /* Read the data of the copy and check the CRC */ 522 523 len = be32_to_cpu(vid_hdr->data_size); 524 525 mutex_lock(&ubi->buf_mutex); 526 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); --- 9 unchanged lines hidden (view full) --- 536 bitflips = 0; 537 second_is_newer = !second_is_newer; 538 } else { 539 dbg_bld("PEB %d CRC is OK", pnum); 540 bitflips |= !!err; 541 } 542 mutex_unlock(&ubi->buf_mutex); 543 | 519 } 520 521 /* Read the data of the copy and check the CRC */ 522 523 len = be32_to_cpu(vid_hdr->data_size); 524 525 mutex_lock(&ubi->buf_mutex); 526 err = ubi_io_read_data(ubi, ubi->peb_buf, pnum, 0, len); --- 9 unchanged lines hidden (view full) --- 536 bitflips = 0; 537 second_is_newer = !second_is_newer; 538 } else { 539 dbg_bld("PEB %d CRC is OK", pnum); 540 bitflips |= !!err; 541 } 542 mutex_unlock(&ubi->buf_mutex); 543 |
544 ubi_free_vid_hdr(ubi, vh); | 544 ubi_free_vid_buf(vidb); |
545 546 if (second_is_newer) 547 dbg_bld("second PEB %d is newer, copy_flag is set", pnum); 548 else 549 dbg_bld("first PEB %d is newer, copy_flag is set", pnum); 550 551 return second_is_newer | (bitflips << 1) | (corrupted << 2); 552 553out_unlock: 554 mutex_unlock(&ubi->buf_mutex); 555out_free_vidh: | 545 546 if (second_is_newer) 547 dbg_bld("second PEB %d is newer, copy_flag is set", pnum); 548 else 549 dbg_bld("first PEB %d is newer, copy_flag is set", pnum); 550 551 return second_is_newer | (bitflips << 1) | (corrupted << 2); 552 553out_unlock: 554 mutex_unlock(&ubi->buf_mutex); 555out_free_vidh: |
556 ubi_free_vid_hdr(ubi, vh); | 556 ubi_free_vid_buf(vidb); |
557 return err; 558} 559 560/** 561 * ubi_add_to_av - add used physical eraseblock to the attaching information. 562 * @ubi: UBI device description object 563 * @ai: attaching information 564 * @pnum: the physical eraseblock number --- 385 unchanged lines hidden (view full) --- 950 * information about this PEB to the corresponding list or RB-tree in the 951 * "attaching info" structure. Returns zero if the physical eraseblock was 952 * successfully handled and a negative error code in case of failure. 953 */ 954static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, 955 int pnum, bool fast) 956{ 957 struct ubi_ec_hdr *ech = ai->ech; | 557 return err; 558} 559 560/** 561 * ubi_add_to_av - add used physical eraseblock to the attaching information. 562 * @ubi: UBI device description object 563 * @ai: attaching information 564 * @pnum: the physical eraseblock number --- 385 unchanged lines hidden (view full) --- 950 * information about this PEB to the corresponding list or RB-tree in the 951 * "attaching info" structure. Returns zero if the physical eraseblock was 952 * successfully handled and a negative error code in case of failure. 953 */ 954static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai, 955 int pnum, bool fast) 956{ 957 struct ubi_ec_hdr *ech = ai->ech; |
958 struct ubi_vid_hdr *vidh = ai->vidh; | 958 struct ubi_vid_io_buf *vidb = ai->vidb; 959 struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb); |
959 long long ec; 960 int err, bitflips = 0, vol_id = -1, ec_err = 0; 961 962 dbg_bld("scan PEB %d", pnum); 963 964 /* Skip bad physical eraseblocks */ 965 err = ubi_io_is_bad(ubi, pnum); 966 if (err < 0) --- 81 unchanged lines hidden (view full) --- 1048 image_seq, pnum, ubi->image_seq); 1049 ubi_dump_ec_hdr(ech); 1050 return -EINVAL; 1051 } 1052 } 1053 1054 /* OK, we've done with the EC header, let's look at the VID header */ 1055 | 960 long long ec; 961 int err, bitflips = 0, vol_id = -1, ec_err = 0; 962 963 dbg_bld("scan PEB %d", pnum); 964 965 /* Skip bad physical eraseblocks */ 966 err = ubi_io_is_bad(ubi, pnum); 967 if (err < 0) --- 81 unchanged lines hidden (view full) --- 1049 image_seq, pnum, ubi->image_seq); 1050 ubi_dump_ec_hdr(ech); 1051 return -EINVAL; 1052 } 1053 } 1054 1055 /* OK, we've done with the EC header, let's look at the VID header */ 1056 |
1056 err = ubi_io_read_vid_hdr(ubi, pnum, vidh, 0); | 1057 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 0); |
1057 if (err < 0) 1058 return err; 1059 switch (err) { 1060 case 0: 1061 break; 1062 case UBI_IO_BITFLIPS: 1063 bitflips = 1; 1064 break; --- 326 unchanged lines hidden (view full) --- 1391 struct ubi_ainf_peb *aeb; 1392 1393 err = -ENOMEM; 1394 1395 ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1396 if (!ai->ech) 1397 return err; 1398 | 1058 if (err < 0) 1059 return err; 1060 switch (err) { 1061 case 0: 1062 break; 1063 case UBI_IO_BITFLIPS: 1064 bitflips = 1; 1065 break; --- 326 unchanged lines hidden (view full) --- 1392 struct ubi_ainf_peb *aeb; 1393 1394 err = -ENOMEM; 1395 1396 ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1397 if (!ai->ech) 1398 return err; 1399 |
1399 ai->vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1400 if (!ai->vidh) | 1400 ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 1401 if (!ai->vidb) |
1401 goto out_ech; 1402 1403 for (pnum = start; pnum < ubi->peb_count; pnum++) { 1404 cond_resched(); 1405 1406 dbg_gen("process PEB %d", pnum); 1407 err = scan_peb(ubi, ai, pnum, false); 1408 if (err < 0) --- 32 unchanged lines hidden (view full) --- 1441 list_for_each_entry(aeb, &ai->erase, u.list) 1442 if (aeb->ec == UBI_UNKNOWN) 1443 aeb->ec = ai->mean_ec; 1444 1445 err = self_check_ai(ubi, ai); 1446 if (err) 1447 goto out_vidh; 1448 | 1402 goto out_ech; 1403 1404 for (pnum = start; pnum < ubi->peb_count; pnum++) { 1405 cond_resched(); 1406 1407 dbg_gen("process PEB %d", pnum); 1408 err = scan_peb(ubi, ai, pnum, false); 1409 if (err < 0) --- 32 unchanged lines hidden (view full) --- 1442 list_for_each_entry(aeb, &ai->erase, u.list) 1443 if (aeb->ec == UBI_UNKNOWN) 1444 aeb->ec = ai->mean_ec; 1445 1446 err = self_check_ai(ubi, ai); 1447 if (err) 1448 goto out_vidh; 1449 |
1449 ubi_free_vid_hdr(ubi, ai->vidh); | 1450 ubi_free_vid_buf(ai->vidb); |
1450 kfree(ai->ech); 1451 1452 return 0; 1453 1454out_vidh: | 1451 kfree(ai->ech); 1452 1453 return 0; 1454 1455out_vidh: |
1455 ubi_free_vid_hdr(ubi, ai->vidh); | 1456 ubi_free_vid_buf(ai->vidb); |
1456out_ech: 1457 kfree(ai->ech); 1458 return err; 1459} 1460 1461static struct ubi_attach_info *alloc_ai(void) 1462{ 1463 struct ubi_attach_info *ai; --- 41 unchanged lines hidden (view full) --- 1505 scan_ai = alloc_ai(); 1506 if (!scan_ai) 1507 goto out; 1508 1509 scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1510 if (!scan_ai->ech) 1511 goto out_ai; 1512 | 1457out_ech: 1458 kfree(ai->ech); 1459 return err; 1460} 1461 1462static struct ubi_attach_info *alloc_ai(void) 1463{ 1464 struct ubi_attach_info *ai; --- 41 unchanged lines hidden (view full) --- 1506 scan_ai = alloc_ai(); 1507 if (!scan_ai) 1508 goto out; 1509 1510 scan_ai->ech = kzalloc(ubi->ec_hdr_alsize, GFP_KERNEL); 1511 if (!scan_ai->ech) 1512 goto out_ai; 1513 |
1513 scan_ai->vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1514 if (!scan_ai->vidh) | 1514 scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL); 1515 if (!scan_ai->vidb) |
1515 goto out_ech; 1516 1517 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) { 1518 cond_resched(); 1519 1520 dbg_gen("process PEB %d", pnum); 1521 err = scan_peb(ubi, scan_ai, pnum, true); 1522 if (err < 0) 1523 goto out_vidh; 1524 } 1525 | 1516 goto out_ech; 1517 1518 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) { 1519 cond_resched(); 1520 1521 dbg_gen("process PEB %d", pnum); 1522 err = scan_peb(ubi, scan_ai, pnum, true); 1523 if (err < 0) 1524 goto out_vidh; 1525 } 1526 |
1526 ubi_free_vid_hdr(ubi, scan_ai->vidh); | 1527 ubi_free_vid_buf(scan_ai->vidb); |
1527 kfree(scan_ai->ech); 1528 1529 if (scan_ai->force_full_scan) 1530 err = UBI_NO_FASTMAP; 1531 else 1532 err = ubi_scan_fastmap(ubi, *ai, scan_ai); 1533 1534 if (err) { --- 4 unchanged lines hidden (view full) --- 1539 destroy_ai(*ai); 1540 *ai = scan_ai; 1541 } else 1542 destroy_ai(scan_ai); 1543 1544 return err; 1545 1546out_vidh: | 1528 kfree(scan_ai->ech); 1529 1530 if (scan_ai->force_full_scan) 1531 err = UBI_NO_FASTMAP; 1532 else 1533 err = ubi_scan_fastmap(ubi, *ai, scan_ai); 1534 1535 if (err) { --- 4 unchanged lines hidden (view full) --- 1540 destroy_ai(*ai); 1541 *ai = scan_ai; 1542 } else 1543 destroy_ai(scan_ai); 1544 1545 return err; 1546 1547out_vidh: |
1547 ubi_free_vid_hdr(ubi, scan_ai->vidh); | 1548 ubi_free_vid_buf(scan_ai->vidb); |
1548out_ech: 1549 kfree(scan_ai->ech); 1550out_ai: 1551 destroy_ai(scan_ai); 1552out: 1553 return err; 1554} 1555 --- 107 unchanged lines hidden (view full) --- 1663 * @ubi: UBI device description object 1664 * @ai: attaching information 1665 * 1666 * This function returns zero if the attaching information is all right, and a 1667 * negative error code if not or if an error occurred. 1668 */ 1669static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) 1670{ | 1549out_ech: 1550 kfree(scan_ai->ech); 1551out_ai: 1552 destroy_ai(scan_ai); 1553out: 1554 return err; 1555} 1556 --- 107 unchanged lines hidden (view full) --- 1664 * @ubi: UBI device description object 1665 * @ai: attaching information 1666 * 1667 * This function returns zero if the attaching information is all right, and a 1668 * negative error code if not or if an error occurred. 1669 */ 1670static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) 1671{ |
1671 struct ubi_vid_hdr *vidh = ai->vidh; | 1672 struct ubi_vid_io_buf *vidb = ai->vidb; 1673 struct ubi_vid_hdr *vidh = ubi_get_vid_hdr(vidb); |
1672 int pnum, err, vols_found = 0; 1673 struct rb_node *rb1, *rb2; 1674 struct ubi_ainf_volume *av; 1675 struct ubi_ainf_peb *aeb, *last_aeb; 1676 uint8_t *buf; 1677 1678 if (!ubi_dbg_chk_gen(ubi)) 1679 return 0; --- 119 unchanged lines hidden (view full) --- 1799 last_aeb = NULL; 1800 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { 1801 int vol_type; 1802 1803 cond_resched(); 1804 1805 last_aeb = aeb; 1806 | 1674 int pnum, err, vols_found = 0; 1675 struct rb_node *rb1, *rb2; 1676 struct ubi_ainf_volume *av; 1677 struct ubi_ainf_peb *aeb, *last_aeb; 1678 uint8_t *buf; 1679 1680 if (!ubi_dbg_chk_gen(ubi)) 1681 return 0; --- 119 unchanged lines hidden (view full) --- 1801 last_aeb = NULL; 1802 ubi_rb_for_each_entry(rb2, aeb, &av->root, u.rb) { 1803 int vol_type; 1804 1805 cond_resched(); 1806 1807 last_aeb = aeb; 1808 |
1807 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidh, 1); | 1809 err = ubi_io_read_vid_hdr(ubi, aeb->pnum, vidb, 1); |
1808 if (err && err != UBI_IO_BITFLIPS) { 1809 ubi_err(ubi, "VID header is not OK (%d)", 1810 err); 1811 if (err > 0) 1812 err = -EIO; 1813 return err; 1814 } 1815 --- 118 unchanged lines hidden --- | 1810 if (err && err != UBI_IO_BITFLIPS) { 1811 ubi_err(ubi, "VID header is not OK (%d)", 1812 err); 1813 if (err > 0) 1814 err = -EIO; 1815 return err; 1816 } 1817 --- 118 unchanged lines hidden --- |