aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd
diff options
context:
space:
mode:
authorBoris Brezillon <boris.brezillon@free-electrons.com>2016-09-16 10:59:26 -0400
committerRichard Weinberger <richard@nod.at>2016-10-02 16:48:14 -0400
commit3291b52f9ff0acc80a8ee3f92a960db937dccecb (patch)
treed781fc235723086fe4e07a3f30d2147d56a4aa0b /drivers/mtd
parent799dca34ac543485f581bd8464ec9b1c4f0f852a (diff)
UBI: introduce the VID buffer concept
Currently, all VID headers are allocated and freed using the ubi_zalloc_vid_hdr() and ubi_free_vid_hdr() function. These functions make sure to align allocation on ubi->vid_hdr_alsize and adjust the vid_hdr pointer to match the ubi->vid_hdr_shift requirements. This works fine, but is a bit convoluted. Moreover, the future introduction of LEB consolidation (needed to support MLC/TLC NANDs) will allows a VID buffer to contain more than one VID header. Hence the creation of a ubi_vid_io_buf struct to attach extra information to the VID header. We currently only store the actual pointer of the underlying buffer, but will soon add the number of VID headers contained in the buffer. Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com> Signed-off-by: Richard Weinberger <richard@nod.at>
Diffstat (limited to 'drivers/mtd')
-rw-r--r--drivers/mtd/ubi/attach.c40
-rw-r--r--drivers/mtd/ubi/eba.c81
-rw-r--r--drivers/mtd/ubi/fastmap.c71
-rw-r--r--drivers/mtd/ubi/io.c39
-rw-r--r--drivers/mtd/ubi/ubi.h97
-rw-r--r--drivers/mtd/ubi/vtbl.c13
-rw-r--r--drivers/mtd/ubi/wl.c19
7 files changed, 218 insertions, 142 deletions
diff --git a/drivers/mtd/ubi/attach.c b/drivers/mtd/ubi/attach.c
index 507f5d6b6114..93ceea4f27d5 100644
--- a/drivers/mtd/ubi/attach.c
+++ b/drivers/mtd/ubi/attach.c
@@ -453,7 +453,7 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
453{ 453{
454 int len, err, second_is_newer, bitflips = 0, corrupted = 0; 454 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
455 uint32_t data_crc, crc; 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); 457 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
458 458
459 if (sqnum2 == aeb->sqnum) { 459 if (sqnum2 == aeb->sqnum) {
@@ -496,12 +496,12 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
496 return bitflips << 1; 496 return bitflips << 1;
497 } 497 }
498 498
499 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 499 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
500 if (!vh) 500 if (!vidb)
501 return -ENOMEM; 501 return -ENOMEM;
502 502
503 pnum = aeb->pnum; 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) { 505 if (err) {
506 if (err == UBI_IO_BITFLIPS) 506 if (err == UBI_IO_BITFLIPS)
507 bitflips = 1; 507 bitflips = 1;
@@ -515,7 +515,7 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
515 } 515 }
516 } 516 }
517 517
518 vid_hdr = vh; 518 vid_hdr = ubi_get_vid_hdr(vidb);
519 } 519 }
520 520
521 /* Read the data of the copy and check the CRC */ 521 /* Read the data of the copy and check the CRC */
@@ -541,7 +541,7 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
541 } 541 }
542 mutex_unlock(&ubi->buf_mutex); 542 mutex_unlock(&ubi->buf_mutex);
543 543
544 ubi_free_vid_hdr(ubi, vh); 544 ubi_free_vid_buf(vidb);
545 545
546 if (second_is_newer) 546 if (second_is_newer)
547 dbg_bld("second PEB %d is newer, copy_flag is set", pnum); 547 dbg_bld("second PEB %d is newer, copy_flag is set", pnum);
@@ -553,7 +553,7 @@ int ubi_compare_lebs(struct ubi_device *ubi, const struct ubi_ainf_peb *aeb,
553out_unlock: 553out_unlock:
554 mutex_unlock(&ubi->buf_mutex); 554 mutex_unlock(&ubi->buf_mutex);
555out_free_vidh: 555out_free_vidh:
556 ubi_free_vid_hdr(ubi, vh); 556 ubi_free_vid_buf(vidb);
557 return err; 557 return err;
558} 558}
559 559
@@ -955,7 +955,8 @@ static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
955 int pnum, bool fast) 955 int pnum, bool fast)
956{ 956{
957 struct ubi_ec_hdr *ech = ai->ech; 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 long long ec;
960 int err, bitflips = 0, vol_id = -1, ec_err = 0; 961 int err, bitflips = 0, vol_id = -1, ec_err = 0;
961 962
@@ -1053,7 +1054,7 @@ static int scan_peb(struct ubi_device *ubi, struct ubi_attach_info *ai,
1053 1054
1054 /* OK, we've done with the EC header, let's look at the VID header */ 1055 /* OK, we've done with the EC header, let's look at the VID header */
1055 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 if (err < 0)
1058 return err; 1059 return err;
1059 switch (err) { 1060 switch (err) {
@@ -1396,8 +1397,8 @@ static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
1396 if (!ai->ech) 1397 if (!ai->ech)
1397 return err; 1398 return err;
1398 1399
1399 ai->vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1400 ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
1400 if (!ai->vidh) 1401 if (!ai->vidb)
1401 goto out_ech; 1402 goto out_ech;
1402 1403
1403 for (pnum = start; pnum < ubi->peb_count; pnum++) { 1404 for (pnum = start; pnum < ubi->peb_count; pnum++) {
@@ -1446,13 +1447,13 @@ static int scan_all(struct ubi_device *ubi, struct ubi_attach_info *ai,
1446 if (err) 1447 if (err)
1447 goto out_vidh; 1448 goto out_vidh;
1448 1449
1449 ubi_free_vid_hdr(ubi, ai->vidh); 1450 ubi_free_vid_buf(ai->vidb);
1450 kfree(ai->ech); 1451 kfree(ai->ech);
1451 1452
1452 return 0; 1453 return 0;
1453 1454
1454out_vidh: 1455out_vidh:
1455 ubi_free_vid_hdr(ubi, ai->vidh); 1456 ubi_free_vid_buf(ai->vidb);
1456out_ech: 1457out_ech:
1457 kfree(ai->ech); 1458 kfree(ai->ech);
1458 return err; 1459 return err;
@@ -1510,8 +1511,8 @@ static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
1510 if (!scan_ai->ech) 1511 if (!scan_ai->ech)
1511 goto out_ai; 1512 goto out_ai;
1512 1513
1513 scan_ai->vidh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 1514 scan_ai->vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
1514 if (!scan_ai->vidh) 1515 if (!scan_ai->vidb)
1515 goto out_ech; 1516 goto out_ech;
1516 1517
1517 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) { 1518 for (pnum = 0; pnum < UBI_FM_MAX_START; pnum++) {
@@ -1523,7 +1524,7 @@ static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
1523 goto out_vidh; 1524 goto out_vidh;
1524 } 1525 }
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 kfree(scan_ai->ech);
1528 1529
1529 if (scan_ai->force_full_scan) 1530 if (scan_ai->force_full_scan)
@@ -1544,7 +1545,7 @@ static int scan_fast(struct ubi_device *ubi, struct ubi_attach_info **ai)
1544 return err; 1545 return err;
1545 1546
1546out_vidh: 1547out_vidh:
1547 ubi_free_vid_hdr(ubi, scan_ai->vidh); 1548 ubi_free_vid_buf(scan_ai->vidb);
1548out_ech: 1549out_ech:
1549 kfree(scan_ai->ech); 1550 kfree(scan_ai->ech);
1550out_ai: 1551out_ai:
@@ -1668,7 +1669,8 @@ out_ai:
1668 */ 1669 */
1669static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai) 1670static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
1670{ 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; 1674 int pnum, err, vols_found = 0;
1673 struct rb_node *rb1, *rb2; 1675 struct rb_node *rb1, *rb2;
1674 struct ubi_ainf_volume *av; 1676 struct ubi_ainf_volume *av;
@@ -1804,7 +1806,7 @@ static int self_check_ai(struct ubi_device *ubi, struct ubi_attach_info *ai)
1804 1806
1805 last_aeb = aeb; 1807 last_aeb = aeb;
1806 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) { 1810 if (err && err != UBI_IO_BITFLIPS) {
1809 ubi_err(ubi, "VID header is not OK (%d)", 1811 ubi_err(ubi, "VID header is not OK (%d)",
1810 err); 1812 err);
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c
index 884c91ffe7ba..ddf4e63eed76 100644
--- a/drivers/mtd/ubi/eba.c
+++ b/drivers/mtd/ubi/eba.c
@@ -513,6 +513,7 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
513 void *buf, int offset, int len, int check) 513 void *buf, int offset, int len, int check)
514{ 514{
515 int err, pnum, scrub = 0, vol_id = vol->vol_id; 515 int err, pnum, scrub = 0, vol_id = vol->vol_id;
516 struct ubi_vid_io_buf *vidb;
516 struct ubi_vid_hdr *vid_hdr; 517 struct ubi_vid_hdr *vid_hdr;
517 uint32_t uninitialized_var(crc); 518 uint32_t uninitialized_var(crc);
518 519
@@ -543,13 +544,15 @@ int ubi_eba_read_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
543 544
544retry: 545retry:
545 if (check) { 546 if (check) {
546 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 547 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
547 if (!vid_hdr) { 548 if (!vidb) {
548 err = -ENOMEM; 549 err = -ENOMEM;
549 goto out_unlock; 550 goto out_unlock;
550 } 551 }
551 552
552 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); 553 vid_hdr = ubi_get_vid_hdr(vidb);
554
555 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
553 if (err && err != UBI_IO_BITFLIPS) { 556 if (err && err != UBI_IO_BITFLIPS) {
554 if (err > 0) { 557 if (err > 0) {
555 /* 558 /*
@@ -595,7 +598,7 @@ retry:
595 ubi_assert(len == be32_to_cpu(vid_hdr->data_size)); 598 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
596 599
597 crc = be32_to_cpu(vid_hdr->data_crc); 600 crc = be32_to_cpu(vid_hdr->data_crc);
598 ubi_free_vid_hdr(ubi, vid_hdr); 601 ubi_free_vid_buf(vidb);
599 } 602 }
600 603
601 err = ubi_io_read_data(ubi, buf, pnum, offset, len); 604 err = ubi_io_read_data(ubi, buf, pnum, offset, len);
@@ -632,7 +635,7 @@ retry:
632 return err; 635 return err;
633 636
634out_free: 637out_free:
635 ubi_free_vid_hdr(ubi, vid_hdr); 638 ubi_free_vid_buf(vidb);
636out_unlock: 639out_unlock:
637 leb_read_unlock(ubi, vol_id, lnum); 640 leb_read_unlock(ubi, vol_id, lnum);
638 return err; 641 return err;
@@ -701,7 +704,7 @@ int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
701 * @buf: data which was not written because of the write failure 704 * @buf: data which was not written because of the write failure
702 * @offset: offset of the failed write 705 * @offset: offset of the failed write
703 * @len: how many bytes should have been written 706 * @len: how many bytes should have been written
704 * @vid: VID header 707 * @vidb: VID buffer
705 * @retry: whether the caller should retry in case of failure 708 * @retry: whether the caller should retry in case of failure
706 * 709 *
707 * This function is called in case of a write failure and moves all good data 710 * This function is called in case of a write failure and moves all good data
@@ -713,9 +716,10 @@ int ubi_eba_read_leb_sg(struct ubi_device *ubi, struct ubi_volume *vol,
713 */ 716 */
714static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum, 717static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
715 const void *buf, int offset, int len, 718 const void *buf, int offset, int len,
716 struct ubi_vid_hdr *vid_hdr, bool *retry) 719 struct ubi_vid_io_buf *vidb, bool *retry)
717{ 720{
718 struct ubi_device *ubi = vol->ubi; 721 struct ubi_device *ubi = vol->ubi;
722 struct ubi_vid_hdr *vid_hdr;
719 int new_pnum, err, vol_id = vol->vol_id, data_size; 723 int new_pnum, err, vol_id = vol->vol_id, data_size;
720 uint32_t crc; 724 uint32_t crc;
721 725
@@ -730,7 +734,7 @@ static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
730 ubi_msg(ubi, "recover PEB %d, move data to PEB %d", 734 ubi_msg(ubi, "recover PEB %d, move data to PEB %d",
731 pnum, new_pnum); 735 pnum, new_pnum);
732 736
733 err = ubi_io_read_vid_hdr(ubi, pnum, vid_hdr, 1); 737 err = ubi_io_read_vid_hdr(ubi, pnum, vidb, 1);
734 if (err && err != UBI_IO_BITFLIPS) { 738 if (err && err != UBI_IO_BITFLIPS) {
735 if (err > 0) 739 if (err > 0)
736 err = -EIO; 740 err = -EIO;
@@ -759,7 +763,7 @@ static int try_recover_peb(struct ubi_volume *vol, int pnum, int lnum,
759 vid_hdr->copy_flag = 1; 763 vid_hdr->copy_flag = 1;
760 vid_hdr->data_size = cpu_to_be32(data_size); 764 vid_hdr->data_size = cpu_to_be32(data_size);
761 vid_hdr->data_crc = cpu_to_be32(crc); 765 vid_hdr->data_crc = cpu_to_be32(crc);
762 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr); 766 err = ubi_io_write_vid_hdr(ubi, new_pnum, vidb);
763 if (err) 767 if (err)
764 goto out_unlock; 768 goto out_unlock;
765 769
@@ -810,24 +814,24 @@ static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
810{ 814{
811 int err, idx = vol_id2idx(ubi, vol_id), tries; 815 int err, idx = vol_id2idx(ubi, vol_id), tries;
812 struct ubi_volume *vol = ubi->volumes[idx]; 816 struct ubi_volume *vol = ubi->volumes[idx];
813 struct ubi_vid_hdr *vid_hdr; 817 struct ubi_vid_io_buf *vidb;
814 818
815 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 819 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
816 if (!vid_hdr) 820 if (!vidb)
817 return -ENOMEM; 821 return -ENOMEM;
818 822
819 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 823 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
820 bool retry; 824 bool retry;
821 825
822 err = try_recover_peb(vol, pnum, lnum, buf, offset, len, 826 err = try_recover_peb(vol, pnum, lnum, buf, offset, len, vidb,
823 vid_hdr, &retry); 827 &retry);
824 if (!err || !retry) 828 if (!err || !retry)
825 break; 829 break;
826 830
827 ubi_msg(ubi, "try again"); 831 ubi_msg(ubi, "try again");
828 } 832 }
829 833
830 ubi_free_vid_hdr(ubi, vid_hdr); 834 ubi_free_vid_buf(vidb);
831 835
832 return err; 836 return err;
833} 837}
@@ -836,7 +840,7 @@ static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
836 * try_write_vid_and_data - try to write VID header and data to a new PEB. 840 * try_write_vid_and_data - try to write VID header and data to a new PEB.
837 * @vol: volume description object 841 * @vol: volume description object
838 * @lnum: logical eraseblock number 842 * @lnum: logical eraseblock number
839 * @vid_hdr: VID header to write 843 * @vidb: the VID buffer to write
840 * @buf: buffer containing the data 844 * @buf: buffer containing the data
841 * @offset: where to start writing data 845 * @offset: where to start writing data
842 * @len: how many bytes should be written 846 * @len: how many bytes should be written
@@ -848,7 +852,7 @@ static int recover_peb(struct ubi_device *ubi, int pnum, int vol_id, int lnum,
848 * flash media, but may be some garbage. 852 * flash media, but may be some garbage.
849 */ 853 */
850static int try_write_vid_and_data(struct ubi_volume *vol, int lnum, 854static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
851 struct ubi_vid_hdr *vid_hdr, const void *buf, 855 struct ubi_vid_io_buf *vidb, const void *buf,
852 int offset, int len) 856 int offset, int len)
853{ 857{
854 struct ubi_device *ubi = vol->ubi; 858 struct ubi_device *ubi = vol->ubi;
@@ -865,7 +869,7 @@ static int try_write_vid_and_data(struct ubi_volume *vol, int lnum,
865 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d", 869 dbg_eba("write VID hdr and %d bytes at offset %d of LEB %d:%d, PEB %d",
866 len, offset, vol_id, lnum, pnum); 870 len, offset, vol_id, lnum, pnum);
867 871
868 err = ubi_io_write_vid_hdr(ubi, pnum, vid_hdr); 872 err = ubi_io_write_vid_hdr(ubi, pnum, vidb);
869 if (err) { 873 if (err) {
870 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d", 874 ubi_warn(ubi, "failed to write VID header to LEB %d:%d, PEB %d",
871 vol_id, lnum, pnum); 875 vol_id, lnum, pnum);
@@ -914,6 +918,7 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
914 const void *buf, int offset, int len) 918 const void *buf, int offset, int len)
915{ 919{
916 int err, pnum, tries, vol_id = vol->vol_id; 920 int err, pnum, tries, vol_id = vol->vol_id;
921 struct ubi_vid_io_buf *vidb;
917 struct ubi_vid_hdr *vid_hdr; 922 struct ubi_vid_hdr *vid_hdr;
918 923
919 if (ubi->ro_mode) 924 if (ubi->ro_mode)
@@ -943,12 +948,14 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
943 * The logical eraseblock is not mapped. We have to get a free physical 948 * The logical eraseblock is not mapped. We have to get a free physical
944 * eraseblock and write the volume identifier header there first. 949 * eraseblock and write the volume identifier header there first.
945 */ 950 */
946 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 951 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
947 if (!vid_hdr) { 952 if (!vidb) {
948 leb_write_unlock(ubi, vol_id, lnum); 953 leb_write_unlock(ubi, vol_id, lnum);
949 return -ENOMEM; 954 return -ENOMEM;
950 } 955 }
951 956
957 vid_hdr = ubi_get_vid_hdr(vidb);
958
952 vid_hdr->vol_type = UBI_VID_DYNAMIC; 959 vid_hdr->vol_type = UBI_VID_DYNAMIC;
953 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 960 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
954 vid_hdr->vol_id = cpu_to_be32(vol_id); 961 vid_hdr->vol_id = cpu_to_be32(vol_id);
@@ -957,8 +964,7 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
957 vid_hdr->data_pad = cpu_to_be32(vol->data_pad); 964 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
958 965
959 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 966 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
960 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, offset, 967 err = try_write_vid_and_data(vol, lnum, vidb, buf, offset, len);
961 len);
962 if (err != -EIO || !ubi->bad_allowed) 968 if (err != -EIO || !ubi->bad_allowed)
963 break; 969 break;
964 970
@@ -972,7 +978,7 @@ int ubi_eba_write_leb(struct ubi_device *ubi, struct ubi_volume *vol, int lnum,
972 ubi_msg(ubi, "try another PEB"); 978 ubi_msg(ubi, "try another PEB");
973 } 979 }
974 980
975 ubi_free_vid_hdr(ubi, vid_hdr); 981 ubi_free_vid_buf(vidb);
976 982
977out: 983out:
978 if (err) 984 if (err)
@@ -1009,6 +1015,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1009 int lnum, const void *buf, int len, int used_ebs) 1015 int lnum, const void *buf, int len, int used_ebs)
1010{ 1016{
1011 int err, tries, data_size = len, vol_id = vol->vol_id; 1017 int err, tries, data_size = len, vol_id = vol->vol_id;
1018 struct ubi_vid_io_buf *vidb;
1012 struct ubi_vid_hdr *vid_hdr; 1019 struct ubi_vid_hdr *vid_hdr;
1013 uint32_t crc; 1020 uint32_t crc;
1014 1021
@@ -1021,10 +1028,12 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1021 else 1028 else
1022 ubi_assert(!(len & (ubi->min_io_size - 1))); 1029 ubi_assert(!(len & (ubi->min_io_size - 1)));
1023 1030
1024 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1031 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
1025 if (!vid_hdr) 1032 if (!vidb)
1026 return -ENOMEM; 1033 return -ENOMEM;
1027 1034
1035 vid_hdr = ubi_get_vid_hdr(vidb);
1036
1028 err = leb_write_lock(ubi, vol_id, lnum); 1037 err = leb_write_lock(ubi, vol_id, lnum);
1029 if (err) 1038 if (err)
1030 goto out; 1039 goto out;
@@ -1044,7 +1053,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1044 ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0); 1053 ubi_assert(vol->eba_tbl->entries[lnum].pnum < 0);
1045 1054
1046 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 1055 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1047 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, 0, len); 1056 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1048 if (err != -EIO || !ubi->bad_allowed) 1057 if (err != -EIO || !ubi->bad_allowed)
1049 break; 1058 break;
1050 1059
@@ -1058,7 +1067,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
1058 leb_write_unlock(ubi, vol_id, lnum); 1067 leb_write_unlock(ubi, vol_id, lnum);
1059 1068
1060out: 1069out:
1061 ubi_free_vid_hdr(ubi, vid_hdr); 1070 ubi_free_vid_buf(vidb);
1062 1071
1063 return err; 1072 return err;
1064} 1073}
@@ -1084,6 +1093,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1084 int lnum, const void *buf, int len) 1093 int lnum, const void *buf, int len)
1085{ 1094{
1086 int err, tries, vol_id = vol->vol_id; 1095 int err, tries, vol_id = vol->vol_id;
1096 struct ubi_vid_io_buf *vidb;
1087 struct ubi_vid_hdr *vid_hdr; 1097 struct ubi_vid_hdr *vid_hdr;
1088 uint32_t crc; 1098 uint32_t crc;
1089 1099
@@ -1101,10 +1111,12 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1101 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0); 1111 return ubi_eba_write_leb(ubi, vol, lnum, NULL, 0, 0);
1102 } 1112 }
1103 1113
1104 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1114 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
1105 if (!vid_hdr) 1115 if (!vidb)
1106 return -ENOMEM; 1116 return -ENOMEM;
1107 1117
1118 vid_hdr = ubi_get_vid_hdr(vidb);
1119
1108 mutex_lock(&ubi->alc_mutex); 1120 mutex_lock(&ubi->alc_mutex);
1109 err = leb_write_lock(ubi, vol_id, lnum); 1121 err = leb_write_lock(ubi, vol_id, lnum);
1110 if (err) 1122 if (err)
@@ -1125,7 +1137,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1125 dbg_eba("change LEB %d:%d", vol_id, lnum); 1137 dbg_eba("change LEB %d:%d", vol_id, lnum);
1126 1138
1127 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) { 1139 for (tries = 0; tries <= UBI_IO_RETRIES; tries++) {
1128 err = try_write_vid_and_data(vol, lnum, vid_hdr, buf, 0, len); 1140 err = try_write_vid_and_data(vol, lnum, vidb, buf, 0, len);
1129 if (err != -EIO || !ubi->bad_allowed) 1141 if (err != -EIO || !ubi->bad_allowed)
1130 break; 1142 break;
1131 1143
@@ -1145,7 +1157,7 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
1145 1157
1146out_mutex: 1158out_mutex:
1147 mutex_unlock(&ubi->alc_mutex); 1159 mutex_unlock(&ubi->alc_mutex);
1148 ubi_free_vid_hdr(ubi, vid_hdr); 1160 ubi_free_vid_buf(vidb);
1149 return err; 1161 return err;
1150} 1162}
1151 1163
@@ -1191,9 +1203,10 @@ static int is_error_sane(int err)
1191 * o a negative error code in case of failure. 1203 * o a negative error code in case of failure.
1192 */ 1204 */
1193int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, 1205int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1194 struct ubi_vid_hdr *vid_hdr) 1206 struct ubi_vid_io_buf *vidb)
1195{ 1207{
1196 int err, vol_id, lnum, data_size, aldata_size, idx; 1208 int err, vol_id, lnum, data_size, aldata_size, idx;
1209 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1197 struct ubi_volume *vol; 1210 struct ubi_volume *vol;
1198 uint32_t crc; 1211 uint32_t crc;
1199 1212
@@ -1305,7 +1318,7 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1305 } 1318 }
1306 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1319 vid_hdr->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1307 1320
1308 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr); 1321 err = ubi_io_write_vid_hdr(ubi, to, vidb);
1309 if (err) { 1322 if (err) {
1310 if (err == -EIO) 1323 if (err == -EIO)
1311 err = MOVE_TARGET_WR_ERR; 1324 err = MOVE_TARGET_WR_ERR;
@@ -1315,7 +1328,7 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1315 cond_resched(); 1328 cond_resched();
1316 1329
1317 /* Read the VID header back and check if it was written correctly */ 1330 /* Read the VID header back and check if it was written correctly */
1318 err = ubi_io_read_vid_hdr(ubi, to, vid_hdr, 1); 1331 err = ubi_io_read_vid_hdr(ubi, to, vidb, 1);
1319 if (err) { 1332 if (err) {
1320 if (err != UBI_IO_BITFLIPS) { 1333 if (err != UBI_IO_BITFLIPS) {
1321 ubi_warn(ubi, "error %d while reading VID header back from PEB %d", 1334 ubi_warn(ubi, "error %d while reading VID header back from PEB %d",
diff --git a/drivers/mtd/ubi/fastmap.c b/drivers/mtd/ubi/fastmap.c
index 27a94f28819b..4adffb893376 100644
--- a/drivers/mtd/ubi/fastmap.c
+++ b/drivers/mtd/ubi/fastmap.c
@@ -110,21 +110,23 @@ size_t ubi_calc_fm_size(struct ubi_device *ubi)
110 * Returns a new struct ubi_vid_hdr on success. 110 * Returns a new struct ubi_vid_hdr on success.
111 * NULL indicates out of memory. 111 * NULL indicates out of memory.
112 */ 112 */
113static struct ubi_vid_hdr *new_fm_vhdr(struct ubi_device *ubi, int vol_id) 113static struct ubi_vid_io_buf *new_fm_vbuf(struct ubi_device *ubi, int vol_id)
114{ 114{
115 struct ubi_vid_hdr *new; 115 struct ubi_vid_io_buf *new;
116 struct ubi_vid_hdr *vh;
116 117
117 new = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 118 new = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
118 if (!new) 119 if (!new)
119 goto out; 120 goto out;
120 121
121 new->vol_type = UBI_VID_DYNAMIC; 122 vh = ubi_get_vid_hdr(new);
122 new->vol_id = cpu_to_be32(vol_id); 123 vh->vol_type = UBI_VID_DYNAMIC;
124 vh->vol_id = cpu_to_be32(vol_id);
123 125
124 /* UBI implementations without fastmap support have to delete the 126 /* UBI implementations without fastmap support have to delete the
125 * fastmap. 127 * fastmap.
126 */ 128 */
127 new->compat = UBI_COMPAT_DELETE; 129 vh->compat = UBI_COMPAT_DELETE;
128 130
129out: 131out:
130 return new; 132 return new;
@@ -408,6 +410,7 @@ static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
408 __be32 *pebs, int pool_size, unsigned long long *max_sqnum, 410 __be32 *pebs, int pool_size, unsigned long long *max_sqnum,
409 struct list_head *free) 411 struct list_head *free)
410{ 412{
413 struct ubi_vid_io_buf *vb;
411 struct ubi_vid_hdr *vh; 414 struct ubi_vid_hdr *vh;
412 struct ubi_ec_hdr *ech; 415 struct ubi_ec_hdr *ech;
413 struct ubi_ainf_peb *new_aeb; 416 struct ubi_ainf_peb *new_aeb;
@@ -417,12 +420,14 @@ static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
417 if (!ech) 420 if (!ech)
418 return -ENOMEM; 421 return -ENOMEM;
419 422
420 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 423 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
421 if (!vh) { 424 if (!vb) {
422 kfree(ech); 425 kfree(ech);
423 return -ENOMEM; 426 return -ENOMEM;
424 } 427 }
425 428
429 vh = ubi_get_vid_hdr(vb);
430
426 dbg_bld("scanning fastmap pool: size = %i", pool_size); 431 dbg_bld("scanning fastmap pool: size = %i", pool_size);
427 432
428 /* 433 /*
@@ -463,7 +468,7 @@ static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
463 goto out; 468 goto out;
464 } 469 }
465 470
466 err = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 471 err = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
467 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) { 472 if (err == UBI_IO_FF || err == UBI_IO_FF_BITFLIPS) {
468 unsigned long long ec = be64_to_cpu(ech->ec); 473 unsigned long long ec = be64_to_cpu(ech->ec);
469 unmap_peb(ai, pnum); 474 unmap_peb(ai, pnum);
@@ -509,7 +514,7 @@ static int scan_pool(struct ubi_device *ubi, struct ubi_attach_info *ai,
509 } 514 }
510 515
511out: 516out:
512 ubi_free_vid_hdr(ubi, vh); 517 ubi_free_vid_buf(vb);
513 kfree(ech); 518 kfree(ech);
514 return ret; 519 return ret;
515} 520}
@@ -837,6 +842,7 @@ int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
837 struct ubi_attach_info *scan_ai) 842 struct ubi_attach_info *scan_ai)
838{ 843{
839 struct ubi_fm_sb *fmsb, *fmsb2; 844 struct ubi_fm_sb *fmsb, *fmsb2;
845 struct ubi_vid_io_buf *vb;
840 struct ubi_vid_hdr *vh; 846 struct ubi_vid_hdr *vh;
841 struct ubi_ec_hdr *ech; 847 struct ubi_ec_hdr *ech;
842 struct ubi_fastmap_layout *fm; 848 struct ubi_fastmap_layout *fm;
@@ -912,12 +918,14 @@ int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
912 goto free_fm_sb; 918 goto free_fm_sb;
913 } 919 }
914 920
915 vh = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 921 vb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
916 if (!vh) { 922 if (!vb) {
917 ret = -ENOMEM; 923 ret = -ENOMEM;
918 goto free_hdr; 924 goto free_hdr;
919 } 925 }
920 926
927 vh = ubi_get_vid_hdr(vb);
928
921 for (i = 0; i < used_blocks; i++) { 929 for (i = 0; i < used_blocks; i++) {
922 int image_seq; 930 int image_seq;
923 931
@@ -960,7 +968,7 @@ int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
960 goto free_hdr; 968 goto free_hdr;
961 } 969 }
962 970
963 ret = ubi_io_read_vid_hdr(ubi, pnum, vh, 0); 971 ret = ubi_io_read_vid_hdr(ubi, pnum, vb, 0);
964 if (ret && ret != UBI_IO_BITFLIPS) { 972 if (ret && ret != UBI_IO_BITFLIPS) {
965 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)", 973 ubi_err(ubi, "unable to read fastmap block# %i (PEB: %i)",
966 i, pnum); 974 i, pnum);
@@ -1050,7 +1058,7 @@ int ubi_scan_fastmap(struct ubi_device *ubi, struct ubi_attach_info *ai,
1050 ubi->fm_disabled = 0; 1058 ubi->fm_disabled = 0;
1051 ubi->fast_attach = 1; 1059 ubi->fast_attach = 1;
1052 1060
1053 ubi_free_vid_hdr(ubi, vh); 1061 ubi_free_vid_buf(vb);
1054 kfree(ech); 1062 kfree(ech);
1055out: 1063out:
1056 up_write(&ubi->fm_protect); 1064 up_write(&ubi->fm_protect);
@@ -1059,7 +1067,7 @@ out:
1059 return ret; 1067 return ret;
1060 1068
1061free_hdr: 1069free_hdr:
1062 ubi_free_vid_hdr(ubi, vh); 1070 ubi_free_vid_buf(vb);
1063 kfree(ech); 1071 kfree(ech);
1064free_fm_sb: 1072free_fm_sb:
1065 kfree(fmsb); 1073 kfree(fmsb);
@@ -1087,6 +1095,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
1087 struct ubi_fm_eba *feba; 1095 struct ubi_fm_eba *feba;
1088 struct ubi_wl_entry *wl_e; 1096 struct ubi_wl_entry *wl_e;
1089 struct ubi_volume *vol; 1097 struct ubi_volume *vol;
1098 struct ubi_vid_io_buf *avbuf, *dvbuf;
1090 struct ubi_vid_hdr *avhdr, *dvhdr; 1099 struct ubi_vid_hdr *avhdr, *dvhdr;
1091 struct ubi_work *ubi_wrk; 1100 struct ubi_work *ubi_wrk;
1092 struct rb_node *tmp_rb; 1101 struct rb_node *tmp_rb;
@@ -1097,18 +1106,21 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
1097 fm_raw = ubi->fm_buf; 1106 fm_raw = ubi->fm_buf;
1098 memset(ubi->fm_buf, 0, ubi->fm_size); 1107 memset(ubi->fm_buf, 0, ubi->fm_size);
1099 1108
1100 avhdr = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); 1109 avbuf = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
1101 if (!avhdr) { 1110 if (!avbuf) {
1102 ret = -ENOMEM; 1111 ret = -ENOMEM;
1103 goto out; 1112 goto out;
1104 } 1113 }
1105 1114
1106 dvhdr = new_fm_vhdr(ubi, UBI_FM_DATA_VOLUME_ID); 1115 dvbuf = new_fm_vbuf(ubi, UBI_FM_DATA_VOLUME_ID);
1107 if (!dvhdr) { 1116 if (!dvbuf) {
1108 ret = -ENOMEM; 1117 ret = -ENOMEM;
1109 goto out_kfree; 1118 goto out_kfree;
1110 } 1119 }
1111 1120
1121 avhdr = ubi_get_vid_hdr(avbuf);
1122 dvhdr = ubi_get_vid_hdr(dvbuf);
1123
1112 seen_pebs = init_seen(ubi); 1124 seen_pebs = init_seen(ubi);
1113 if (IS_ERR(seen_pebs)) { 1125 if (IS_ERR(seen_pebs)) {
1114 ret = PTR_ERR(seen_pebs); 1126 ret = PTR_ERR(seen_pebs);
@@ -1277,7 +1289,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
1277 spin_unlock(&ubi->volumes_lock); 1289 spin_unlock(&ubi->volumes_lock);
1278 1290
1279 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum); 1291 dbg_bld("writing fastmap SB to PEB %i", new_fm->e[0]->pnum);
1280 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avhdr); 1292 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[0]->pnum, avbuf);
1281 if (ret) { 1293 if (ret) {
1282 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!"); 1294 ubi_err(ubi, "unable to write vid_hdr to fastmap SB!");
1283 goto out_kfree; 1295 goto out_kfree;
@@ -1298,7 +1310,7 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
1298 dvhdr->lnum = cpu_to_be32(i); 1310 dvhdr->lnum = cpu_to_be32(i);
1299 dbg_bld("writing fastmap data to PEB %i sqnum %llu", 1311 dbg_bld("writing fastmap data to PEB %i sqnum %llu",
1300 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum)); 1312 new_fm->e[i]->pnum, be64_to_cpu(dvhdr->sqnum));
1301 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvhdr); 1313 ret = ubi_io_write_vid_hdr(ubi, new_fm->e[i]->pnum, dvbuf);
1302 if (ret) { 1314 if (ret) {
1303 ubi_err(ubi, "unable to write vid_hdr to PEB %i!", 1315 ubi_err(ubi, "unable to write vid_hdr to PEB %i!",
1304 new_fm->e[i]->pnum); 1316 new_fm->e[i]->pnum);
@@ -1323,8 +1335,8 @@ static int ubi_write_fastmap(struct ubi_device *ubi,
1323 dbg_bld("fastmap written!"); 1335 dbg_bld("fastmap written!");
1324 1336
1325out_kfree: 1337out_kfree:
1326 ubi_free_vid_hdr(ubi, avhdr); 1338 ubi_free_vid_buf(avbuf);
1327 ubi_free_vid_hdr(ubi, dvhdr); 1339 ubi_free_vid_buf(dvbuf);
1328 free_seen(seen_pebs); 1340 free_seen(seen_pebs);
1329out: 1341out:
1330 return ret; 1342 return ret;
@@ -1394,7 +1406,8 @@ static int invalidate_fastmap(struct ubi_device *ubi)
1394 int ret; 1406 int ret;
1395 struct ubi_fastmap_layout *fm; 1407 struct ubi_fastmap_layout *fm;
1396 struct ubi_wl_entry *e; 1408 struct ubi_wl_entry *e;
1397 struct ubi_vid_hdr *vh = NULL; 1409 struct ubi_vid_io_buf *vb = NULL;
1410 struct ubi_vid_hdr *vh;
1398 1411
1399 if (!ubi->fm) 1412 if (!ubi->fm)
1400 return 0; 1413 return 0;
@@ -1406,10 +1419,12 @@ static int invalidate_fastmap(struct ubi_device *ubi)
1406 if (!fm) 1419 if (!fm)
1407 goto out; 1420 goto out;
1408 1421
1409 vh = new_fm_vhdr(ubi, UBI_FM_SB_VOLUME_ID); 1422 vb = new_fm_vbuf(ubi, UBI_FM_SB_VOLUME_ID);
1410 if (!vh) 1423 if (!vb)
1411 goto out_free_fm; 1424 goto out_free_fm;
1412 1425
1426 vh = ubi_get_vid_hdr(vb);
1427
1413 ret = -ENOSPC; 1428 ret = -ENOSPC;
1414 e = ubi_wl_get_fm_peb(ubi, 1); 1429 e = ubi_wl_get_fm_peb(ubi, 1);
1415 if (!e) 1430 if (!e)
@@ -1420,7 +1435,7 @@ static int invalidate_fastmap(struct ubi_device *ubi)
1420 * to scanning mode. 1435 * to scanning mode.
1421 */ 1436 */
1422 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi)); 1437 vh->sqnum = cpu_to_be64(ubi_next_sqnum(ubi));
1423 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vh); 1438 ret = ubi_io_write_vid_hdr(ubi, e->pnum, vb);
1424 if (ret < 0) { 1439 if (ret < 0) {
1425 ubi_wl_put_fm_peb(ubi, e, 0, 0); 1440 ubi_wl_put_fm_peb(ubi, e, 0, 0);
1426 goto out_free_fm; 1441 goto out_free_fm;
@@ -1432,7 +1447,7 @@ static int invalidate_fastmap(struct ubi_device *ubi)
1432 ubi->fm = fm; 1447 ubi->fm = fm;
1433 1448
1434out: 1449out:
1435 ubi_free_vid_hdr(ubi, vh); 1450 ubi_free_vid_buf(vb);
1436 return ret; 1451 return ret;
1437 1452
1438out_free_fm: 1453out_free_fm:
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index ff8cafe1e5cd..b6fb8f945c21 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -502,6 +502,7 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
502 loff_t addr; 502 loff_t addr;
503 uint32_t data = 0; 503 uint32_t data = 0;
504 struct ubi_ec_hdr ec_hdr; 504 struct ubi_ec_hdr ec_hdr;
505 struct ubi_vid_io_buf vidb;
505 506
506 /* 507 /*
507 * Note, we cannot generally define VID header buffers on stack, 508 * Note, we cannot generally define VID header buffers on stack,
@@ -528,7 +529,10 @@ static int nor_erase_prepare(struct ubi_device *ubi, int pnum)
528 goto error; 529 goto error;
529 } 530 }
530 531
531 err = ubi_io_read_vid_hdr(ubi, pnum, &vid_hdr, 0); 532 ubi_init_vid_buf(ubi, &vidb, &vid_hdr);
533 ubi_assert(&vid_hdr == ubi_get_vid_hdr(&vidb));
534
535 err = ubi_io_read_vid_hdr(ubi, pnum, &vidb, 0);
532 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR && 536 if (err != UBI_IO_BAD_HDR_EBADMSG && err != UBI_IO_BAD_HDR &&
533 err != UBI_IO_FF){ 537 err != UBI_IO_FF){
534 addr += ubi->vid_hdr_aloffset; 538 addr += ubi->vid_hdr_aloffset;
@@ -995,12 +999,11 @@ bad:
995 * ubi_io_read_vid_hdr - read and check a volume identifier header. 999 * ubi_io_read_vid_hdr - read and check a volume identifier header.
996 * @ubi: UBI device description object 1000 * @ubi: UBI device description object
997 * @pnum: physical eraseblock number to read from 1001 * @pnum: physical eraseblock number to read from
998 * @vid_hdr: &struct ubi_vid_hdr object where to store the read volume 1002 * @vidb: the volume identifier buffer to store data in
999 * identifier header
1000 * @verbose: be verbose if the header is corrupted or wasn't found 1003 * @verbose: be verbose if the header is corrupted or wasn't found
1001 * 1004 *
1002 * This function reads the volume identifier header from physical eraseblock 1005 * This function reads the volume identifier header from physical eraseblock
1003 * @pnum and stores it in @vid_hdr. It also checks CRC checksum of the read 1006 * @pnum and stores it in @vidb. It also checks CRC checksum of the read
1004 * volume identifier header. The error codes are the same as in 1007 * volume identifier header. The error codes are the same as in
1005 * 'ubi_io_read_ec_hdr()'. 1008 * 'ubi_io_read_ec_hdr()'.
1006 * 1009 *
@@ -1008,16 +1011,16 @@ bad:
1008 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'. 1011 * 'ubi_io_read_ec_hdr()', so refer commentaries in 'ubi_io_read_ec_hdr()'.
1009 */ 1012 */
1010int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 1013int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1011 struct ubi_vid_hdr *vid_hdr, int verbose) 1014 struct ubi_vid_io_buf *vidb, int verbose)
1012{ 1015{
1013 int err, read_err; 1016 int err, read_err;
1014 uint32_t crc, magic, hdr_crc; 1017 uint32_t crc, magic, hdr_crc;
1015 void *p; 1018 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1019 void *p = vidb->buffer;
1016 1020
1017 dbg_io("read VID header from PEB %d", pnum); 1021 dbg_io("read VID header from PEB %d", pnum);
1018 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1022 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
1019 1023
1020 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1021 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1024 read_err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1022 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE); 1025 ubi->vid_hdr_shift + UBI_VID_HDR_SIZE);
1023 if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err)) 1026 if (read_err && read_err != UBI_IO_BITFLIPS && !mtd_is_eccerr(read_err))
@@ -1080,23 +1083,24 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
1080 * ubi_io_write_vid_hdr - write a volume identifier header. 1083 * ubi_io_write_vid_hdr - write a volume identifier header.
1081 * @ubi: UBI device description object 1084 * @ubi: UBI device description object
1082 * @pnum: the physical eraseblock number to write to 1085 * @pnum: the physical eraseblock number to write to
1083 * @vid_hdr: the volume identifier header to write 1086 * @vidb: the volume identifier buffer to write
1084 * 1087 *
1085 * This function writes the volume identifier header described by @vid_hdr to 1088 * This function writes the volume identifier header described by @vid_hdr to
1086 * physical eraseblock @pnum. This function automatically fills the 1089 * physical eraseblock @pnum. This function automatically fills the
1087 * @vid_hdr->magic and the @vid_hdr->version fields, as well as calculates 1090 * @vidb->hdr->magic and the @vidb->hdr->version fields, as well as calculates
1088 * header CRC checksum and stores it at vid_hdr->hdr_crc. 1091 * header CRC checksum and stores it at vidb->hdr->hdr_crc.
1089 * 1092 *
1090 * This function returns zero in case of success and a negative error code in 1093 * This function returns zero in case of success and a negative error code in
1091 * case of failure. If %-EIO is returned, the physical eraseblock probably went 1094 * case of failure. If %-EIO is returned, the physical eraseblock probably went
1092 * bad. 1095 * bad.
1093 */ 1096 */
1094int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 1097int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1095 struct ubi_vid_hdr *vid_hdr) 1098 struct ubi_vid_io_buf *vidb)
1096{ 1099{
1100 struct ubi_vid_hdr *vid_hdr = ubi_get_vid_hdr(vidb);
1097 int err; 1101 int err;
1098 uint32_t crc; 1102 uint32_t crc;
1099 void *p; 1103 void *p = vidb->buffer;
1100 1104
1101 dbg_io("write VID header to PEB %d", pnum); 1105 dbg_io("write VID header to PEB %d", pnum);
1102 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 1106 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
@@ -1117,7 +1121,6 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1117 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE)) 1121 if (ubi_dbg_power_cut(ubi, POWER_CUT_VID_WRITE))
1118 return -EROFS; 1122 return -EROFS;
1119 1123
1120 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1121 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1124 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
1122 ubi->vid_hdr_alsize); 1125 ubi->vid_hdr_alsize);
1123 return err; 1126 return err;
@@ -1283,17 +1286,19 @@ static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1283{ 1286{
1284 int err; 1287 int err;
1285 uint32_t crc, hdr_crc; 1288 uint32_t crc, hdr_crc;
1289 struct ubi_vid_io_buf *vidb;
1286 struct ubi_vid_hdr *vid_hdr; 1290 struct ubi_vid_hdr *vid_hdr;
1287 void *p; 1291 void *p;
1288 1292
1289 if (!ubi_dbg_chk_io(ubi)) 1293 if (!ubi_dbg_chk_io(ubi))
1290 return 0; 1294 return 0;
1291 1295
1292 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 1296 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
1293 if (!vid_hdr) 1297 if (!vidb)
1294 return -ENOMEM; 1298 return -ENOMEM;
1295 1299
1296 p = (char *)vid_hdr - ubi->vid_hdr_shift; 1300 vid_hdr = ubi_get_vid_hdr(vidb);
1301 p = vidb->buffer;
1297 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 1302 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
1298 ubi->vid_hdr_alsize); 1303 ubi->vid_hdr_alsize);
1299 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err)) 1304 if (err && err != UBI_IO_BITFLIPS && !mtd_is_eccerr(err))
@@ -1314,7 +1319,7 @@ static int self_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1314 err = self_check_vid_hdr(ubi, pnum, vid_hdr); 1319 err = self_check_vid_hdr(ubi, pnum, vid_hdr);
1315 1320
1316exit: 1321exit:
1317 ubi_free_vid_hdr(ubi, vid_hdr); 1322 ubi_free_vid_buf(vidb);
1318 return err; 1323 return err;
1319} 1324}
1320 1325
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index bc145229a53c..697dbcba7371 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -167,6 +167,17 @@ enum {
167}; 167};
168 168
169/** 169/**
170 * struct ubi_vid_io_buf - VID buffer used to read/write VID info to/from the
171 * flash.
172 * @hdr: a pointer to the VID header stored in buffer
173 * @buffer: underlying buffer
174 */
175struct ubi_vid_io_buf {
176 struct ubi_vid_hdr *hdr;
177 void *buffer;
178};
179
180/**
170 * struct ubi_wl_entry - wear-leveling entry. 181 * struct ubi_wl_entry - wear-leveling entry.
171 * @u.rb: link in the corresponding (free/used) RB-tree 182 * @u.rb: link in the corresponding (free/used) RB-tree
172 * @u.list: link in the protection queue 183 * @u.list: link in the protection queue
@@ -740,7 +751,7 @@ struct ubi_ainf_volume {
740 * @ec_count: a temporary variable used when calculating @mean_ec 751 * @ec_count: a temporary variable used when calculating @mean_ec
741 * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects 752 * @aeb_slab_cache: slab cache for &struct ubi_ainf_peb objects
742 * @ech: temporary EC header. Only available during scan 753 * @ech: temporary EC header. Only available during scan
743 * @vidh: temporary VID header. Only available during scan 754 * @vidh: temporary VID buffer. Only available during scan
744 * 755 *
745 * This data structure contains the result of attaching an MTD device and may 756 * This data structure contains the result of attaching an MTD device and may
746 * be used by other UBI sub-systems to build final UBI data structures, further 757 * be used by other UBI sub-systems to build final UBI data structures, further
@@ -770,7 +781,7 @@ struct ubi_attach_info {
770 int ec_count; 781 int ec_count;
771 struct kmem_cache *aeb_slab_cache; 782 struct kmem_cache *aeb_slab_cache;
772 struct ubi_ec_hdr *ech; 783 struct ubi_ec_hdr *ech;
773 struct ubi_vid_hdr *vidh; 784 struct ubi_vid_io_buf *vidb;
774}; 785};
775 786
776/** 787/**
@@ -887,7 +898,7 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, struct ubi_volume *vol,
887int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol, 898int ubi_eba_atomic_leb_change(struct ubi_device *ubi, struct ubi_volume *vol,
888 int lnum, const void *buf, int len); 899 int lnum, const void *buf, int len);
889int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to, 900int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
890 struct ubi_vid_hdr *vid_hdr); 901 struct ubi_vid_io_buf *vidb);
891int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai); 902int ubi_eba_init(struct ubi_device *ubi, struct ubi_attach_info *ai);
892unsigned long long ubi_next_sqnum(struct ubi_device *ubi); 903unsigned long long ubi_next_sqnum(struct ubi_device *ubi);
893int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap, 904int self_check_eba(struct ubi_device *ubi, struct ubi_attach_info *ai_fastmap,
@@ -922,9 +933,9 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
922int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum, 933int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
923 struct ubi_ec_hdr *ec_hdr); 934 struct ubi_ec_hdr *ec_hdr);
924int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum, 935int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
925 struct ubi_vid_hdr *vid_hdr, int verbose); 936 struct ubi_vid_io_buf *vidb, int verbose);
926int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum, 937int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
927 struct ubi_vid_hdr *vid_hdr); 938 struct ubi_vid_io_buf *vidb);
928 939
929/* build.c */ 940/* build.c */
930int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, 941int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
@@ -1045,44 +1056,68 @@ static inline void ubi_move_aeb_to_list(struct ubi_ainf_volume *av,
1045} 1056}
1046 1057
1047/** 1058/**
1048 * ubi_zalloc_vid_hdr - allocate a volume identifier header object. 1059 * ubi_init_vid_buf - Initialize a VID buffer
1049 * @ubi: UBI device description object 1060 * @ubi: the UBI device
1050 * @gfp_flags: GFP flags to allocate with 1061 * @vidb: the VID buffer to initialize
1051 * 1062 * @buf: the underlying buffer
1052 * This function returns a pointer to the newly allocated and zero-filled 1063 */
1053 * volume identifier header object in case of success and %NULL in case of 1064static inline void ubi_init_vid_buf(const struct ubi_device *ubi,
1054 * failure. 1065 struct ubi_vid_io_buf *vidb,
1066 void *buf)
1067{
1068 if (buf)
1069 memset(buf, 0, ubi->vid_hdr_alsize);
1070
1071 vidb->buffer = buf;
1072 vidb->hdr = buf + ubi->vid_hdr_shift;
1073}
1074
1075/**
1076 * ubi_init_vid_buf - Allocate a VID buffer
1077 * @ubi: the UBI device
1078 * @gfp_flags: GFP flags to use for the allocation
1055 */ 1079 */
1056static inline struct ubi_vid_hdr * 1080static inline struct ubi_vid_io_buf *
1057ubi_zalloc_vid_hdr(const struct ubi_device *ubi, gfp_t gfp_flags) 1081ubi_alloc_vid_buf(const struct ubi_device *ubi, gfp_t gfp_flags)
1058{ 1082{
1059 void *vid_hdr; 1083 struct ubi_vid_io_buf *vidb;
1084 void *buf;
1060 1085
1061 vid_hdr = kzalloc(ubi->vid_hdr_alsize, gfp_flags); 1086 vidb = kzalloc(sizeof(*vidb), gfp_flags);
1062 if (!vid_hdr) 1087 if (!vidb)
1063 return NULL; 1088 return NULL;
1064 1089
1065 /* 1090 buf = kmalloc(ubi->vid_hdr_alsize, gfp_flags);
1066 * VID headers may be stored at un-aligned flash offsets, so we shift 1091 if (!buf) {
1067 * the pointer. 1092 kfree(vidb);
1068 */ 1093 return NULL;
1069 return vid_hdr + ubi->vid_hdr_shift; 1094 }
1095
1096 ubi_init_vid_buf(ubi, vidb, buf);
1097
1098 return vidb;
1070} 1099}
1071 1100
1072/** 1101/**
1073 * ubi_free_vid_hdr - free a volume identifier header object. 1102 * ubi_free_vid_buf - Free a VID buffer
1074 * @ubi: UBI device description object 1103 * @vidb: the VID buffer to free
1075 * @vid_hdr: the object to free
1076 */ 1104 */
1077static inline void ubi_free_vid_hdr(const struct ubi_device *ubi, 1105static inline void ubi_free_vid_buf(struct ubi_vid_io_buf *vidb)
1078 struct ubi_vid_hdr *vid_hdr)
1079{ 1106{
1080 void *p = vid_hdr; 1107 if (!vidb)
1081
1082 if (!p)
1083 return; 1108 return;
1084 1109
1085 kfree(p - ubi->vid_hdr_shift); 1110 kfree(vidb->buffer);
1111 kfree(vidb);
1112}
1113
1114/**
1115 * ubi_get_vid_hdr - Get the VID header attached to a VID buffer
1116 * @vidb: VID buffer
1117 */
1118static inline struct ubi_vid_hdr *ubi_get_vid_hdr(struct ubi_vid_io_buf *vidb)
1119{
1120 return vidb->hdr;
1086} 1121}
1087 1122
1088/* 1123/*
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
index 9e1457708cbf..263743e7b741 100644
--- a/drivers/mtd/ubi/vtbl.c
+++ b/drivers/mtd/ubi/vtbl.c
@@ -299,15 +299,18 @@ static int create_vtbl(struct ubi_device *ubi, struct ubi_attach_info *ai,
299 int copy, void *vtbl) 299 int copy, void *vtbl)
300{ 300{
301 int err, tries = 0; 301 int err, tries = 0;
302 struct ubi_vid_io_buf *vidb;
302 struct ubi_vid_hdr *vid_hdr; 303 struct ubi_vid_hdr *vid_hdr;
303 struct ubi_ainf_peb *new_aeb; 304 struct ubi_ainf_peb *new_aeb;
304 305
305 dbg_gen("create volume table (copy #%d)", copy + 1); 306 dbg_gen("create volume table (copy #%d)", copy + 1);
306 307
307 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_KERNEL); 308 vidb = ubi_alloc_vid_buf(ubi, GFP_KERNEL);
308 if (!vid_hdr) 309 if (!vidb)
309 return -ENOMEM; 310 return -ENOMEM;
310 311
312 vid_hdr = ubi_get_vid_hdr(vidb);
313
311retry: 314retry:
312 new_aeb = ubi_early_get_peb(ubi, ai); 315 new_aeb = ubi_early_get_peb(ubi, ai);
313 if (IS_ERR(new_aeb)) { 316 if (IS_ERR(new_aeb)) {
@@ -324,7 +327,7 @@ retry:
324 vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum); 327 vid_hdr->sqnum = cpu_to_be64(++ai->max_sqnum);
325 328
326 /* The EC header is already there, write the VID header */ 329 /* The EC header is already there, write the VID header */
327 err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vid_hdr); 330 err = ubi_io_write_vid_hdr(ubi, new_aeb->pnum, vidb);
328 if (err) 331 if (err)
329 goto write_error; 332 goto write_error;
330 333
@@ -339,7 +342,7 @@ retry:
339 */ 342 */
340 err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0); 343 err = ubi_add_to_av(ubi, ai, new_aeb->pnum, new_aeb->ec, vid_hdr, 0);
341 ubi_free_aeb(ai, new_aeb); 344 ubi_free_aeb(ai, new_aeb);
342 ubi_free_vid_hdr(ubi, vid_hdr); 345 ubi_free_vid_buf(vidb);
343 return err; 346 return err;
344 347
345write_error: 348write_error:
@@ -353,7 +356,7 @@ write_error:
353 } 356 }
354 ubi_free_aeb(ai, new_aeb); 357 ubi_free_aeb(ai, new_aeb);
355out_free: 358out_free:
356 ubi_free_vid_hdr(ubi, vid_hdr); 359 ubi_free_vid_buf(vidb);
357 return err; 360 return err;
358 361
359} 362}
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index f4533266d7b2..6351e8ae91aa 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -649,6 +649,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
649 int anchor = wrk->anchor; 649 int anchor = wrk->anchor;
650#endif 650#endif
651 struct ubi_wl_entry *e1, *e2; 651 struct ubi_wl_entry *e1, *e2;
652 struct ubi_vid_io_buf *vidb;
652 struct ubi_vid_hdr *vid_hdr; 653 struct ubi_vid_hdr *vid_hdr;
653 int dst_leb_clean = 0; 654 int dst_leb_clean = 0;
654 655
@@ -656,10 +657,12 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
656 if (shutdown) 657 if (shutdown)
657 return 0; 658 return 0;
658 659
659 vid_hdr = ubi_zalloc_vid_hdr(ubi, GFP_NOFS); 660 vidb = ubi_alloc_vid_buf(ubi, GFP_NOFS);
660 if (!vid_hdr) 661 if (!vidb)
661 return -ENOMEM; 662 return -ENOMEM;
662 663
664 vid_hdr = ubi_get_vid_hdr(vidb);
665
663 mutex_lock(&ubi->move_mutex); 666 mutex_lock(&ubi->move_mutex);
664 spin_lock(&ubi->wl_lock); 667 spin_lock(&ubi->wl_lock);
665 ubi_assert(!ubi->move_from && !ubi->move_to); 668 ubi_assert(!ubi->move_from && !ubi->move_to);
@@ -753,7 +756,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
753 * which is being moved was unmapped. 756 * which is being moved was unmapped.
754 */ 757 */
755 758
756 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vid_hdr, 0); 759 err = ubi_io_read_vid_hdr(ubi, e1->pnum, vidb, 0);
757 if (err && err != UBI_IO_BITFLIPS) { 760 if (err && err != UBI_IO_BITFLIPS) {
758 dst_leb_clean = 1; 761 dst_leb_clean = 1;
759 if (err == UBI_IO_FF) { 762 if (err == UBI_IO_FF) {
@@ -790,7 +793,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
790 vol_id = be32_to_cpu(vid_hdr->vol_id); 793 vol_id = be32_to_cpu(vid_hdr->vol_id);
791 lnum = be32_to_cpu(vid_hdr->lnum); 794 lnum = be32_to_cpu(vid_hdr->lnum);
792 795
793 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vid_hdr); 796 err = ubi_eba_copy_leb(ubi, e1->pnum, e2->pnum, vidb);
794 if (err) { 797 if (err) {
795 if (err == MOVE_CANCEL_RACE) { 798 if (err == MOVE_CANCEL_RACE) {
796 /* 799 /*
@@ -847,7 +850,7 @@ static int wear_leveling_worker(struct ubi_device *ubi, struct ubi_work *wrk,
847 if (scrubbing) 850 if (scrubbing)
848 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d", 851 ubi_msg(ubi, "scrubbed PEB %d (LEB %d:%d), data moved to PEB %d",
849 e1->pnum, vol_id, lnum, e2->pnum); 852 e1->pnum, vol_id, lnum, e2->pnum);
850 ubi_free_vid_hdr(ubi, vid_hdr); 853 ubi_free_vid_buf(vidb);
851 854
852 spin_lock(&ubi->wl_lock); 855 spin_lock(&ubi->wl_lock);
853 if (!ubi->move_to_put) { 856 if (!ubi->move_to_put) {
@@ -913,7 +916,7 @@ out_not_moved:
913 ubi->wl_scheduled = 0; 916 ubi->wl_scheduled = 0;
914 spin_unlock(&ubi->wl_lock); 917 spin_unlock(&ubi->wl_lock);
915 918
916 ubi_free_vid_hdr(ubi, vid_hdr); 919 ubi_free_vid_buf(vidb);
917 if (dst_leb_clean) { 920 if (dst_leb_clean) {
918 ensure_wear_leveling(ubi, 1); 921 ensure_wear_leveling(ubi, 1);
919 } else { 922 } else {
@@ -937,7 +940,7 @@ out_error:
937 ubi->move_to_put = ubi->wl_scheduled = 0; 940 ubi->move_to_put = ubi->wl_scheduled = 0;
938 spin_unlock(&ubi->wl_lock); 941 spin_unlock(&ubi->wl_lock);
939 942
940 ubi_free_vid_hdr(ubi, vid_hdr); 943 ubi_free_vid_buf(vidb);
941 wl_entry_destroy(ubi, e1); 944 wl_entry_destroy(ubi, e1);
942 wl_entry_destroy(ubi, e2); 945 wl_entry_destroy(ubi, e2);
943 946
@@ -951,7 +954,7 @@ out_cancel:
951 ubi->wl_scheduled = 0; 954 ubi->wl_scheduled = 0;
952 spin_unlock(&ubi->wl_lock); 955 spin_unlock(&ubi->wl_lock);
953 mutex_unlock(&ubi->move_mutex); 956 mutex_unlock(&ubi->move_mutex);
954 ubi_free_vid_hdr(ubi, vid_hdr); 957 ubi_free_vid_buf(vidb);
955 return 0; 958 return 0;
956} 959}
957 960