aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--MAINTAINERS2
-rw-r--r--drivers/mtd/ubi/build.c25
-rw-r--r--drivers/mtd/ubi/cdev.c49
-rw-r--r--drivers/mtd/ubi/debug.c44
-rw-r--r--drivers/mtd/ubi/debug.h2
-rw-r--r--drivers/mtd/ubi/eba.c98
-rw-r--r--drivers/mtd/ubi/gluebi.c27
-rw-r--r--drivers/mtd/ubi/io.c65
-rw-r--r--drivers/mtd/ubi/kapi.c19
-rw-r--r--drivers/mtd/ubi/misc.c4
-rw-r--r--drivers/mtd/ubi/scan.c127
-rw-r--r--drivers/mtd/ubi/scan.h2
-rw-r--r--drivers/mtd/ubi/ubi.h3
-rw-r--r--drivers/mtd/ubi/upd.c4
-rw-r--r--drivers/mtd/ubi/vmt.c53
-rw-r--r--drivers/mtd/ubi/vtbl.c85
-rw-r--r--drivers/mtd/ubi/wl.c93
-rw-r--r--include/mtd/ubi-header.h101
18 files changed, 403 insertions, 400 deletions
diff --git a/MAINTAINERS b/MAINTAINERS
index a9615a567da6..1698dbbc7375 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -2393,7 +2393,7 @@ P: Artem Bityutskiy
2393M: dedekind@infradead.org 2393M: dedekind@infradead.org
2394W: http://www.linux-mtd.infradead.org/ 2394W: http://www.linux-mtd.infradead.org/
2395L: linux-mtd@lists.infradead.org 2395L: linux-mtd@lists.infradead.org
2396T: git git://git.infradead.org/ubi-2.6.git 2396T: git git://git.infradead.org/~dedekind/ubi-2.6.git
2397S: Maintained 2397S: Maintained
2398 2398
2399MICROTEK X6 SCANNER 2399MICROTEK X6 SCANNER
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 555d594d1811..1cb22bfae750 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -33,6 +33,7 @@
33#include <linux/moduleparam.h> 33#include <linux/moduleparam.h>
34#include <linux/stringify.h> 34#include <linux/stringify.h>
35#include <linux/stat.h> 35#include <linux/stat.h>
36#include <linux/log2.h>
36#include "ubi.h" 37#include "ubi.h"
37 38
38/* Maximum length of the 'mtd=' parameter */ 39/* Maximum length of the 'mtd=' parameter */
@@ -369,7 +370,7 @@ static int attach_by_scanning(struct ubi_device *ubi)
369out_wl: 370out_wl:
370 ubi_wl_close(ubi); 371 ubi_wl_close(ubi);
371out_vtbl: 372out_vtbl:
372 kfree(ubi->vtbl); 373 vfree(ubi->vtbl);
373out_si: 374out_si:
374 ubi_scan_destroy_si(si); 375 ubi_scan_destroy_si(si);
375 return err; 376 return err;
@@ -422,8 +423,7 @@ static int io_init(struct ubi_device *ubi)
422 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft; 423 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
423 424
424 /* Make sure minimal I/O unit is power of 2 */ 425 /* Make sure minimal I/O unit is power of 2 */
425 if (ubi->min_io_size == 0 || 426 if (!is_power_of_2(ubi->min_io_size)) {
426 (ubi->min_io_size & (ubi->min_io_size - 1))) {
427 ubi_err("bad min. I/O unit"); 427 ubi_err("bad min. I/O unit");
428 return -EINVAL; 428 return -EINVAL;
429 } 429 }
@@ -593,8 +593,6 @@ static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
593 if (err) 593 if (err)
594 goto out_detach; 594 goto out_detach;
595 595
596 ubi_devices_cnt += 1;
597
598 ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt); 596 ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt);
599 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name); 597 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
600 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); 598 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
@@ -624,12 +622,13 @@ static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
624 wake_up_process(ubi->bgt_thread); 622 wake_up_process(ubi->bgt_thread);
625 } 623 }
626 624
625 ubi_devices_cnt += 1;
627 return 0; 626 return 0;
628 627
629out_detach: 628out_detach:
630 ubi_eba_close(ubi); 629 ubi_eba_close(ubi);
631 ubi_wl_close(ubi); 630 ubi_wl_close(ubi);
632 kfree(ubi->vtbl); 631 vfree(ubi->vtbl);
633out_free: 632out_free:
634 kfree(ubi); 633 kfree(ubi);
635out_mtd: 634out_mtd:
@@ -650,7 +649,7 @@ static void detach_mtd_dev(struct ubi_device *ubi)
650 uif_close(ubi); 649 uif_close(ubi);
651 ubi_eba_close(ubi); 650 ubi_eba_close(ubi);
652 ubi_wl_close(ubi); 651 ubi_wl_close(ubi);
653 kfree(ubi->vtbl); 652 vfree(ubi->vtbl);
654 put_mtd_device(ubi->mtd); 653 put_mtd_device(ubi->mtd);
655 kfree(ubi_devices[ubi_num]); 654 kfree(ubi_devices[ubi_num]);
656 ubi_devices[ubi_num] = NULL; 655 ubi_devices[ubi_num] = NULL;
@@ -686,13 +685,6 @@ static int __init ubi_init(void)
686 struct mtd_dev_param *p = &mtd_dev_param[i]; 685 struct mtd_dev_param *p = &mtd_dev_param[i];
687 686
688 cond_resched(); 687 cond_resched();
689
690 if (!p->name) {
691 dbg_err("empty name");
692 err = -EINVAL;
693 goto out_detach;
694 }
695
696 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs); 688 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
697 if (err) 689 if (err)
698 goto out_detach; 690 goto out_detach;
@@ -799,7 +791,7 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
799 791
800 /* Get rid of the final newline */ 792 /* Get rid of the final newline */
801 if (buf[len - 1] == '\n') 793 if (buf[len - 1] == '\n')
802 buf[len - 1] = 0; 794 buf[len - 1] = '\0';
803 795
804 for (i = 0; i < 3; i++) 796 for (i = 0; i < 3; i++)
805 tokens[i] = strsep(&pbuf, ","); 797 tokens[i] = strsep(&pbuf, ",");
@@ -809,9 +801,6 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
809 return -EINVAL; 801 return -EINVAL;
810 } 802 }
811 803
812 if (tokens[0] == '\0')
813 return -EINVAL;
814
815 p = &mtd_dev_param[mtd_devs]; 804 p = &mtd_dev_param[mtd_devs];
816 strcpy(&p->name[0], tokens[0]); 805 strcpy(&p->name[0], tokens[0]);
817 806
diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/cdev.c
index 6612eb79bf17..fe4da1e96c52 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/cdev.c
@@ -64,6 +64,7 @@ static struct ubi_device *major_to_device(int major)
64 if (ubi_devices[i] && ubi_devices[i]->major == major) 64 if (ubi_devices[i] && ubi_devices[i]->major == major)
65 return ubi_devices[i]; 65 return ubi_devices[i];
66 BUG(); 66 BUG();
67 return NULL;
67} 68}
68 69
69/** 70/**
@@ -153,7 +154,7 @@ static int vol_cdev_release(struct inode *inode, struct file *file)
153 ubi_warn("update of volume %d not finished, volume is damaged", 154 ubi_warn("update of volume %d not finished, volume is damaged",
154 vol->vol_id); 155 vol->vol_id);
155 vol->updating = 0; 156 vol->updating = 0;
156 kfree(vol->upd_buf); 157 vfree(vol->upd_buf);
157 } 158 }
158 159
159 ubi_close_volume(desc); 160 ubi_close_volume(desc);
@@ -232,7 +233,7 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
232 tbuf_size = vol->usable_leb_size; 233 tbuf_size = vol->usable_leb_size;
233 if (count < tbuf_size) 234 if (count < tbuf_size)
234 tbuf_size = ALIGN(count, ubi->min_io_size); 235 tbuf_size = ALIGN(count, ubi->min_io_size);
235 tbuf = kmalloc(tbuf_size, GFP_KERNEL); 236 tbuf = vmalloc(tbuf_size);
236 if (!tbuf) 237 if (!tbuf)
237 return -ENOMEM; 238 return -ENOMEM;
238 239
@@ -271,7 +272,7 @@ static ssize_t vol_cdev_read(struct file *file, __user char *buf, size_t count,
271 len = count > tbuf_size ? tbuf_size : count; 272 len = count > tbuf_size ? tbuf_size : count;
272 } while (count); 273 } while (count);
273 274
274 kfree(tbuf); 275 vfree(tbuf);
275 return err ? err : count_save - count; 276 return err ? err : count_save - count;
276} 277}
277 278
@@ -320,7 +321,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
320 tbuf_size = vol->usable_leb_size; 321 tbuf_size = vol->usable_leb_size;
321 if (count < tbuf_size) 322 if (count < tbuf_size)
322 tbuf_size = ALIGN(count, ubi->min_io_size); 323 tbuf_size = ALIGN(count, ubi->min_io_size);
323 tbuf = kmalloc(tbuf_size, GFP_KERNEL); 324 tbuf = vmalloc(tbuf_size);
324 if (!tbuf) 325 if (!tbuf)
325 return -ENOMEM; 326 return -ENOMEM;
326 327
@@ -355,7 +356,7 @@ static ssize_t vol_cdev_direct_write(struct file *file, const char __user *buf,
355 len = count > tbuf_size ? tbuf_size : count; 356 len = count > tbuf_size ? tbuf_size : count;
356 } 357 }
357 358
358 kfree(tbuf); 359 vfree(tbuf);
359 return err ? err : count_save - count; 360 return err ? err : count_save - count;
360} 361}
361 362
@@ -397,6 +398,7 @@ static ssize_t vol_cdev_write(struct file *file, const char __user *buf,
397 vol->corrupted = 1; 398 vol->corrupted = 1;
398 } 399 }
399 vol->checked = 1; 400 vol->checked = 1;
401 ubi_gluebi_updated(vol);
400 revoke_exclusive(desc, UBI_READWRITE); 402 revoke_exclusive(desc, UBI_READWRITE);
401 } 403 }
402 404
@@ -413,19 +415,7 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
413 struct ubi_device *ubi = vol->ubi; 415 struct ubi_device *ubi = vol->ubi;
414 void __user *argp = (void __user *)arg; 416 void __user *argp = (void __user *)arg;
415 417
416 if (_IOC_NR(cmd) > VOL_CDEV_IOC_MAX_SEQ ||
417 _IOC_TYPE(cmd) != UBI_VOL_IOC_MAGIC)
418 return -ENOTTY;
419
420 if (_IOC_DIR(cmd) && _IOC_READ)
421 err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
422 else if (_IOC_DIR(cmd) && _IOC_WRITE)
423 err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
424 if (err)
425 return -EFAULT;
426
427 switch (cmd) { 418 switch (cmd) {
428
429 /* Volume update command */ 419 /* Volume update command */
430 case UBI_IOCVOLUP: 420 case UBI_IOCVOLUP:
431 { 421 {
@@ -471,7 +461,7 @@ static int vol_cdev_ioctl(struct inode *inode, struct file *file,
471 { 461 {
472 int32_t lnum; 462 int32_t lnum;
473 463
474 err = __get_user(lnum, (__user int32_t *)argp); 464 err = get_user(lnum, (__user int32_t *)argp);
475 if (err) { 465 if (err) {
476 err = -EFAULT; 466 err = -EFAULT;
477 break; 467 break;
@@ -587,17 +577,6 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
587 struct ubi_volume_desc *desc; 577 struct ubi_volume_desc *desc;
588 void __user *argp = (void __user *)arg; 578 void __user *argp = (void __user *)arg;
589 579
590 if (_IOC_NR(cmd) > UBI_CDEV_IOC_MAX_SEQ ||
591 _IOC_TYPE(cmd) != UBI_IOC_MAGIC)
592 return -ENOTTY;
593
594 if (_IOC_DIR(cmd) && _IOC_READ)
595 err = !access_ok(VERIFY_WRITE, argp, _IOC_SIZE(cmd));
596 else if (_IOC_DIR(cmd) && _IOC_WRITE)
597 err = !access_ok(VERIFY_READ, argp, _IOC_SIZE(cmd));
598 if (err)
599 return -EFAULT;
600
601 if (!capable(CAP_SYS_RESOURCE)) 580 if (!capable(CAP_SYS_RESOURCE))
602 return -EPERM; 581 return -EPERM;
603 582
@@ -612,7 +591,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
612 struct ubi_mkvol_req req; 591 struct ubi_mkvol_req req;
613 592
614 dbg_msg("create volume"); 593 dbg_msg("create volume");
615 err = __copy_from_user(&req, argp, 594 err = copy_from_user(&req, argp,
616 sizeof(struct ubi_mkvol_req)); 595 sizeof(struct ubi_mkvol_req));
617 if (err) { 596 if (err) {
618 err = -EFAULT; 597 err = -EFAULT;
@@ -629,7 +608,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
629 if (err) 608 if (err)
630 break; 609 break;
631 610
632 err = __put_user(req.vol_id, (__user int32_t *)argp); 611 err = put_user(req.vol_id, (__user int32_t *)argp);
633 if (err) 612 if (err)
634 err = -EFAULT; 613 err = -EFAULT;
635 614
@@ -642,7 +621,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
642 int vol_id; 621 int vol_id;
643 622
644 dbg_msg("remove volume"); 623 dbg_msg("remove volume");
645 err = __get_user(vol_id, (__user int32_t *)argp); 624 err = get_user(vol_id, (__user int32_t *)argp);
646 if (err) { 625 if (err) {
647 err = -EFAULT; 626 err = -EFAULT;
648 break; 627 break;
@@ -669,7 +648,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
669 struct ubi_rsvol_req req; 648 struct ubi_rsvol_req req;
670 649
671 dbg_msg("re-size volume"); 650 dbg_msg("re-size volume");
672 err = __copy_from_user(&req, argp, 651 err = copy_from_user(&req, argp,
673 sizeof(struct ubi_rsvol_req)); 652 sizeof(struct ubi_rsvol_req));
674 if (err) { 653 if (err) {
675 err = -EFAULT; 654 err = -EFAULT;
@@ -707,7 +686,7 @@ static int ubi_cdev_ioctl(struct inode *inode, struct file *file,
707struct file_operations ubi_cdev_operations = { 686struct file_operations ubi_cdev_operations = {
708 .owner = THIS_MODULE, 687 .owner = THIS_MODULE,
709 .ioctl = ubi_cdev_ioctl, 688 .ioctl = ubi_cdev_ioctl,
710 .llseek = no_llseek 689 .llseek = no_llseek,
711}; 690};
712 691
713/* UBI volume character device operations */ 692/* UBI volume character device operations */
@@ -718,5 +697,5 @@ struct file_operations ubi_vol_cdev_operations = {
718 .llseek = vol_cdev_llseek, 697 .llseek = vol_cdev_llseek,
719 .read = vol_cdev_read, 698 .read = vol_cdev_read,
720 .write = vol_cdev_write, 699 .write = vol_cdev_write,
721 .ioctl = vol_cdev_ioctl 700 .ioctl = vol_cdev_ioctl,
722}; 701};
diff --git a/drivers/mtd/ubi/debug.c b/drivers/mtd/ubi/debug.c
index 86364221fafe..310341e5cd43 100644
--- a/drivers/mtd/ubi/debug.c
+++ b/drivers/mtd/ubi/debug.c
@@ -35,12 +35,12 @@
35void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr) 35void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
36{ 36{
37 dbg_msg("erase counter header dump:"); 37 dbg_msg("erase counter header dump:");
38 dbg_msg("magic %#08x", ubi32_to_cpu(ec_hdr->magic)); 38 dbg_msg("magic %#08x", be32_to_cpu(ec_hdr->magic));
39 dbg_msg("version %d", (int)ec_hdr->version); 39 dbg_msg("version %d", (int)ec_hdr->version);
40 dbg_msg("ec %llu", (long long)ubi64_to_cpu(ec_hdr->ec)); 40 dbg_msg("ec %llu", (long long)be64_to_cpu(ec_hdr->ec));
41 dbg_msg("vid_hdr_offset %d", ubi32_to_cpu(ec_hdr->vid_hdr_offset)); 41 dbg_msg("vid_hdr_offset %d", be32_to_cpu(ec_hdr->vid_hdr_offset));
42 dbg_msg("data_offset %d", ubi32_to_cpu(ec_hdr->data_offset)); 42 dbg_msg("data_offset %d", be32_to_cpu(ec_hdr->data_offset));
43 dbg_msg("hdr_crc %#08x", ubi32_to_cpu(ec_hdr->hdr_crc)); 43 dbg_msg("hdr_crc %#08x", be32_to_cpu(ec_hdr->hdr_crc));
44 dbg_msg("erase counter header hexdump:"); 44 dbg_msg("erase counter header hexdump:");
45 ubi_dbg_hexdump(ec_hdr, UBI_EC_HDR_SIZE); 45 ubi_dbg_hexdump(ec_hdr, UBI_EC_HDR_SIZE);
46} 46}
@@ -52,20 +52,20 @@ void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr)
52void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr) 52void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr)
53{ 53{
54 dbg_msg("volume identifier header dump:"); 54 dbg_msg("volume identifier header dump:");
55 dbg_msg("magic %08x", ubi32_to_cpu(vid_hdr->magic)); 55 dbg_msg("magic %08x", be32_to_cpu(vid_hdr->magic));
56 dbg_msg("version %d", (int)vid_hdr->version); 56 dbg_msg("version %d", (int)vid_hdr->version);
57 dbg_msg("vol_type %d", (int)vid_hdr->vol_type); 57 dbg_msg("vol_type %d", (int)vid_hdr->vol_type);
58 dbg_msg("copy_flag %d", (int)vid_hdr->copy_flag); 58 dbg_msg("copy_flag %d", (int)vid_hdr->copy_flag);
59 dbg_msg("compat %d", (int)vid_hdr->compat); 59 dbg_msg("compat %d", (int)vid_hdr->compat);
60 dbg_msg("vol_id %d", ubi32_to_cpu(vid_hdr->vol_id)); 60 dbg_msg("vol_id %d", be32_to_cpu(vid_hdr->vol_id));
61 dbg_msg("lnum %d", ubi32_to_cpu(vid_hdr->lnum)); 61 dbg_msg("lnum %d", be32_to_cpu(vid_hdr->lnum));
62 dbg_msg("leb_ver %u", ubi32_to_cpu(vid_hdr->leb_ver)); 62 dbg_msg("leb_ver %u", be32_to_cpu(vid_hdr->leb_ver));
63 dbg_msg("data_size %d", ubi32_to_cpu(vid_hdr->data_size)); 63 dbg_msg("data_size %d", be32_to_cpu(vid_hdr->data_size));
64 dbg_msg("used_ebs %d", ubi32_to_cpu(vid_hdr->used_ebs)); 64 dbg_msg("used_ebs %d", be32_to_cpu(vid_hdr->used_ebs));
65 dbg_msg("data_pad %d", ubi32_to_cpu(vid_hdr->data_pad)); 65 dbg_msg("data_pad %d", be32_to_cpu(vid_hdr->data_pad));
66 dbg_msg("sqnum %llu", 66 dbg_msg("sqnum %llu",
67 (unsigned long long)ubi64_to_cpu(vid_hdr->sqnum)); 67 (unsigned long long)be64_to_cpu(vid_hdr->sqnum));
68 dbg_msg("hdr_crc %08x", ubi32_to_cpu(vid_hdr->hdr_crc)); 68 dbg_msg("hdr_crc %08x", be32_to_cpu(vid_hdr->hdr_crc));
69 dbg_msg("volume identifier header hexdump:"); 69 dbg_msg("volume identifier header hexdump:");
70} 70}
71 71
@@ -91,7 +91,7 @@ void ubi_dbg_dump_vol_info(const struct ubi_volume *vol)
91 91
92 if (vol->name_len <= UBI_VOL_NAME_MAX && 92 if (vol->name_len <= UBI_VOL_NAME_MAX &&
93 strnlen(vol->name, vol->name_len + 1) == vol->name_len) { 93 strnlen(vol->name, vol->name_len + 1) == vol->name_len) {
94 dbg_msg("name %s", vol->name); 94 dbg_msg("name %s", vol->name);
95 } else { 95 } else {
96 dbg_msg("the 1st 5 characters of the name: %c%c%c%c%c", 96 dbg_msg("the 1st 5 characters of the name: %c%c%c%c%c",
97 vol->name[0], vol->name[1], vol->name[2], 97 vol->name[0], vol->name[1], vol->name[2],
@@ -106,30 +106,30 @@ void ubi_dbg_dump_vol_info(const struct ubi_volume *vol)
106 */ 106 */
107void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx) 107void ubi_dbg_dump_vtbl_record(const struct ubi_vtbl_record *r, int idx)
108{ 108{
109 int name_len = ubi16_to_cpu(r->name_len); 109 int name_len = be16_to_cpu(r->name_len);
110 110
111 dbg_msg("volume table record %d dump:", idx); 111 dbg_msg("volume table record %d dump:", idx);
112 dbg_msg("reserved_pebs %d", ubi32_to_cpu(r->reserved_pebs)); 112 dbg_msg("reserved_pebs %d", be32_to_cpu(r->reserved_pebs));
113 dbg_msg("alignment %d", ubi32_to_cpu(r->alignment)); 113 dbg_msg("alignment %d", be32_to_cpu(r->alignment));
114 dbg_msg("data_pad %d", ubi32_to_cpu(r->data_pad)); 114 dbg_msg("data_pad %d", be32_to_cpu(r->data_pad));
115 dbg_msg("vol_type %d", (int)r->vol_type); 115 dbg_msg("vol_type %d", (int)r->vol_type);
116 dbg_msg("upd_marker %d", (int)r->upd_marker); 116 dbg_msg("upd_marker %d", (int)r->upd_marker);
117 dbg_msg("name_len %d", name_len); 117 dbg_msg("name_len %d", name_len);
118 118
119 if (r->name[0] == '\0') { 119 if (r->name[0] == '\0') {
120 dbg_msg("name NULL"); 120 dbg_msg("name NULL");
121 return; 121 return;
122 } 122 }
123 123
124 if (name_len <= UBI_VOL_NAME_MAX && 124 if (name_len <= UBI_VOL_NAME_MAX &&
125 strnlen(&r->name[0], name_len + 1) == name_len) { 125 strnlen(&r->name[0], name_len + 1) == name_len) {
126 dbg_msg("name %s", &r->name[0]); 126 dbg_msg("name %s", &r->name[0]);
127 } else { 127 } else {
128 dbg_msg("1st 5 characters of the name: %c%c%c%c%c", 128 dbg_msg("1st 5 characters of the name: %c%c%c%c%c",
129 r->name[0], r->name[1], r->name[2], r->name[3], 129 r->name[0], r->name[1], r->name[2], r->name[3],
130 r->name[4]); 130 r->name[4]);
131 } 131 }
132 dbg_msg("crc %#08x", ubi32_to_cpu(r->crc)); 132 dbg_msg("crc %#08x", be32_to_cpu(r->crc));
133} 133}
134 134
135/** 135/**
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index f816ad9a36c0..ff8f39548cd8 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -52,7 +52,6 @@ struct ubi_scan_volume;
52struct ubi_scan_leb; 52struct ubi_scan_leb;
53struct ubi_mkvol_req; 53struct ubi_mkvol_req;
54 54
55void ubi_dbg_print(int type, const char *func, const char *fmt, ...);
56void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr); 55void ubi_dbg_dump_ec_hdr(const struct ubi_ec_hdr *ec_hdr);
57void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr); 56void ubi_dbg_dump_vid_hdr(const struct ubi_vid_hdr *vid_hdr);
58void ubi_dbg_dump_vol_info(const struct ubi_volume *vol); 57void ubi_dbg_dump_vol_info(const struct ubi_volume *vol);
@@ -66,7 +65,6 @@ void ubi_dbg_hexdump(const void *buf, int size);
66 65
67#define dbg_msg(fmt, ...) ({}) 66#define dbg_msg(fmt, ...) ({})
68#define ubi_dbg_dump_stack() ({}) 67#define ubi_dbg_dump_stack() ({})
69#define ubi_dbg_print(func, fmt, ...) ({})
70#define ubi_dbg_dump_ec_hdr(ec_hdr) ({}) 68#define ubi_dbg_dump_ec_hdr(ec_hdr) ({})
71#define ubi_dbg_dump_vid_hdr(vid_hdr) ({}) 69#define ubi_dbg_dump_vid_hdr(vid_hdr) ({})
72#define ubi_dbg_dump_vol_info(vol) ({}) 70#define ubi_dbg_dump_vol_info(vol) ({})
diff --git a/drivers/mtd/ubi/eba.c b/drivers/mtd/ubi/eba.c
index 7c6b223b3f8a..8aff9385613f 100644
--- a/drivers/mtd/ubi/eba.c
+++ b/drivers/mtd/ubi/eba.c
@@ -425,10 +425,10 @@ retry:
425 } else if (err == UBI_IO_BITFLIPS) 425 } else if (err == UBI_IO_BITFLIPS)
426 scrub = 1; 426 scrub = 1;
427 427
428 ubi_assert(lnum < ubi32_to_cpu(vid_hdr->used_ebs)); 428 ubi_assert(lnum < be32_to_cpu(vid_hdr->used_ebs));
429 ubi_assert(len == ubi32_to_cpu(vid_hdr->data_size)); 429 ubi_assert(len == be32_to_cpu(vid_hdr->data_size));
430 430
431 crc = ubi32_to_cpu(vid_hdr->data_crc); 431 crc = be32_to_cpu(vid_hdr->data_crc);
432 ubi_free_vid_hdr(ubi, vid_hdr); 432 ubi_free_vid_hdr(ubi, vid_hdr);
433 } 433 }
434 434
@@ -518,13 +518,13 @@ retry:
518 goto out_put; 518 goto out_put;
519 } 519 }
520 520
521 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 521 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
522 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr); 522 err = ubi_io_write_vid_hdr(ubi, new_pnum, vid_hdr);
523 if (err) 523 if (err)
524 goto write_error; 524 goto write_error;
525 525
526 data_size = offset + len; 526 data_size = offset + len;
527 new_buf = kmalloc(data_size, GFP_KERNEL); 527 new_buf = vmalloc(data_size);
528 if (!new_buf) { 528 if (!new_buf) {
529 err = -ENOMEM; 529 err = -ENOMEM;
530 goto out_put; 530 goto out_put;
@@ -535,7 +535,7 @@ retry:
535 if (offset > 0) { 535 if (offset > 0) {
536 err = ubi_io_read_data(ubi, new_buf, pnum, 0, offset); 536 err = ubi_io_read_data(ubi, new_buf, pnum, 0, offset);
537 if (err && err != UBI_IO_BITFLIPS) { 537 if (err && err != UBI_IO_BITFLIPS) {
538 kfree(new_buf); 538 vfree(new_buf);
539 goto out_put; 539 goto out_put;
540 } 540 }
541 } 541 }
@@ -544,11 +544,11 @@ retry:
544 544
545 err = ubi_io_write_data(ubi, new_buf, new_pnum, 0, data_size); 545 err = ubi_io_write_data(ubi, new_buf, new_pnum, 0, data_size);
546 if (err) { 546 if (err) {
547 kfree(new_buf); 547 vfree(new_buf);
548 goto write_error; 548 goto write_error;
549 } 549 }
550 550
551 kfree(new_buf); 551 vfree(new_buf);
552 ubi_free_vid_hdr(ubi, vid_hdr); 552 ubi_free_vid_hdr(ubi, vid_hdr);
553 553
554 vol->eba_tbl[lnum] = new_pnum; 554 vol->eba_tbl[lnum] = new_pnum;
@@ -634,11 +634,11 @@ int ubi_eba_write_leb(struct ubi_device *ubi, int vol_id, int lnum,
634 } 634 }
635 635
636 vid_hdr->vol_type = UBI_VID_DYNAMIC; 636 vid_hdr->vol_type = UBI_VID_DYNAMIC;
637 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 637 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
638 vid_hdr->vol_id = cpu_to_ubi32(vol_id); 638 vid_hdr->vol_id = cpu_to_be32(vol_id);
639 vid_hdr->lnum = cpu_to_ubi32(lnum); 639 vid_hdr->lnum = cpu_to_be32(lnum);
640 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 640 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
641 vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); 641 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
642 642
643retry: 643retry:
644 pnum = ubi_wl_get_peb(ubi, dtype); 644 pnum = ubi_wl_get_peb(ubi, dtype);
@@ -692,7 +692,7 @@ write_error:
692 return err; 692 return err;
693 } 693 }
694 694
695 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 695 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
696 ubi_msg("try another PEB"); 696 ubi_msg("try another PEB");
697 goto retry; 697 goto retry;
698} 698}
@@ -748,17 +748,17 @@ int ubi_eba_write_leb_st(struct ubi_device *ubi, int vol_id, int lnum,
748 return err; 748 return err;
749 } 749 }
750 750
751 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 751 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
752 vid_hdr->vol_id = cpu_to_ubi32(vol_id); 752 vid_hdr->vol_id = cpu_to_be32(vol_id);
753 vid_hdr->lnum = cpu_to_ubi32(lnum); 753 vid_hdr->lnum = cpu_to_be32(lnum);
754 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 754 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
755 vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); 755 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
756 756
757 crc = crc32(UBI_CRC32_INIT, buf, data_size); 757 crc = crc32(UBI_CRC32_INIT, buf, data_size);
758 vid_hdr->vol_type = UBI_VID_STATIC; 758 vid_hdr->vol_type = UBI_VID_STATIC;
759 vid_hdr->data_size = cpu_to_ubi32(data_size); 759 vid_hdr->data_size = cpu_to_be32(data_size);
760 vid_hdr->used_ebs = cpu_to_ubi32(used_ebs); 760 vid_hdr->used_ebs = cpu_to_be32(used_ebs);
761 vid_hdr->data_crc = cpu_to_ubi32(crc); 761 vid_hdr->data_crc = cpu_to_be32(crc);
762 762
763retry: 763retry:
764 pnum = ubi_wl_get_peb(ubi, dtype); 764 pnum = ubi_wl_get_peb(ubi, dtype);
@@ -813,7 +813,7 @@ write_error:
813 return err; 813 return err;
814 } 814 }
815 815
816 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 816 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
817 ubi_msg("try another PEB"); 817 ubi_msg("try another PEB");
818 goto retry; 818 goto retry;
819} 819}
@@ -854,17 +854,17 @@ int ubi_eba_atomic_leb_change(struct ubi_device *ubi, int vol_id, int lnum,
854 return err; 854 return err;
855 } 855 }
856 856
857 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 857 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
858 vid_hdr->vol_id = cpu_to_ubi32(vol_id); 858 vid_hdr->vol_id = cpu_to_be32(vol_id);
859 vid_hdr->lnum = cpu_to_ubi32(lnum); 859 vid_hdr->lnum = cpu_to_be32(lnum);
860 vid_hdr->compat = ubi_get_compat(ubi, vol_id); 860 vid_hdr->compat = ubi_get_compat(ubi, vol_id);
861 vid_hdr->data_pad = cpu_to_ubi32(vol->data_pad); 861 vid_hdr->data_pad = cpu_to_be32(vol->data_pad);
862 862
863 crc = crc32(UBI_CRC32_INIT, buf, len); 863 crc = crc32(UBI_CRC32_INIT, buf, len);
864 vid_hdr->vol_type = UBI_VID_STATIC; 864 vid_hdr->vol_type = UBI_VID_DYNAMIC;
865 vid_hdr->data_size = cpu_to_ubi32(len); 865 vid_hdr->data_size = cpu_to_be32(len);
866 vid_hdr->copy_flag = 1; 866 vid_hdr->copy_flag = 1;
867 vid_hdr->data_crc = cpu_to_ubi32(crc); 867 vid_hdr->data_crc = cpu_to_be32(crc);
868 868
869retry: 869retry:
870 pnum = ubi_wl_get_peb(ubi, dtype); 870 pnum = ubi_wl_get_peb(ubi, dtype);
@@ -891,11 +891,13 @@ retry:
891 goto write_error; 891 goto write_error;
892 } 892 }
893 893
894 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1); 894 if (vol->eba_tbl[lnum] >= 0) {
895 if (err) { 895 err = ubi_wl_put_peb(ubi, vol->eba_tbl[lnum], 1);
896 ubi_free_vid_hdr(ubi, vid_hdr); 896 if (err) {
897 leb_write_unlock(ubi, vol_id, lnum); 897 ubi_free_vid_hdr(ubi, vid_hdr);
898 return err; 898 leb_write_unlock(ubi, vol_id, lnum);
899 return err;
900 }
899 } 901 }
900 902
901 vol->eba_tbl[lnum] = pnum; 903 vol->eba_tbl[lnum] = pnum;
@@ -924,7 +926,7 @@ write_error:
924 return err; 926 return err;
925 } 927 }
926 928
927 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 929 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
928 ubi_msg("try another PEB"); 930 ubi_msg("try another PEB");
929 goto retry; 931 goto retry;
930} 932}
@@ -965,19 +967,19 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
965 uint32_t crc; 967 uint32_t crc;
966 void *buf, *buf1 = NULL; 968 void *buf, *buf1 = NULL;
967 969
968 vol_id = ubi32_to_cpu(vid_hdr->vol_id); 970 vol_id = be32_to_cpu(vid_hdr->vol_id);
969 lnum = ubi32_to_cpu(vid_hdr->lnum); 971 lnum = be32_to_cpu(vid_hdr->lnum);
970 972
971 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to); 973 dbg_eba("copy LEB %d:%d, PEB %d to PEB %d", vol_id, lnum, from, to);
972 974
973 if (vid_hdr->vol_type == UBI_VID_STATIC) { 975 if (vid_hdr->vol_type == UBI_VID_STATIC) {
974 data_size = ubi32_to_cpu(vid_hdr->data_size); 976 data_size = be32_to_cpu(vid_hdr->data_size);
975 aldata_size = ALIGN(data_size, ubi->min_io_size); 977 aldata_size = ALIGN(data_size, ubi->min_io_size);
976 } else 978 } else
977 data_size = aldata_size = 979 data_size = aldata_size =
978 ubi->leb_size - ubi32_to_cpu(vid_hdr->data_pad); 980 ubi->leb_size - be32_to_cpu(vid_hdr->data_pad);
979 981
980 buf = kmalloc(aldata_size, GFP_KERNEL); 982 buf = vmalloc(aldata_size);
981 if (!buf) 983 if (!buf)
982 return -ENOMEM; 984 return -ENOMEM;
983 985
@@ -987,7 +989,7 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
987 */ 989 */
988 err = leb_write_lock(ubi, vol_id, lnum); 990 err = leb_write_lock(ubi, vol_id, lnum);
989 if (err) { 991 if (err) {
990 kfree(buf); 992 vfree(buf);
991 return err; 993 return err;
992 } 994 }
993 995
@@ -1054,10 +1056,10 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1054 */ 1056 */
1055 if (data_size > 0) { 1057 if (data_size > 0) {
1056 vid_hdr->copy_flag = 1; 1058 vid_hdr->copy_flag = 1;
1057 vid_hdr->data_size = cpu_to_ubi32(data_size); 1059 vid_hdr->data_size = cpu_to_be32(data_size);
1058 vid_hdr->data_crc = cpu_to_ubi32(crc); 1060 vid_hdr->data_crc = cpu_to_be32(crc);
1059 } 1061 }
1060 vid_hdr->sqnum = cpu_to_ubi64(next_sqnum(ubi)); 1062 vid_hdr->sqnum = cpu_to_be64(next_sqnum(ubi));
1061 1063
1062 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr); 1064 err = ubi_io_write_vid_hdr(ubi, to, vid_hdr);
1063 if (err) 1065 if (err)
@@ -1082,7 +1084,7 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1082 * We've written the data and are going to read it back to make 1084 * We've written the data and are going to read it back to make
1083 * sure it was written correctly. 1085 * sure it was written correctly.
1084 */ 1086 */
1085 buf1 = kmalloc(aldata_size, GFP_KERNEL); 1087 buf1 = vmalloc(aldata_size);
1086 if (!buf1) { 1088 if (!buf1) {
1087 err = -ENOMEM; 1089 err = -ENOMEM;
1088 goto out_unlock; 1090 goto out_unlock;
@@ -1111,15 +1113,15 @@ int ubi_eba_copy_leb(struct ubi_device *ubi, int from, int to,
1111 vol->eba_tbl[lnum] = to; 1113 vol->eba_tbl[lnum] = to;
1112 1114
1113 leb_write_unlock(ubi, vol_id, lnum); 1115 leb_write_unlock(ubi, vol_id, lnum);
1114 kfree(buf); 1116 vfree(buf);
1115 kfree(buf1); 1117 vfree(buf1);
1116 1118
1117 return 0; 1119 return 0;
1118 1120
1119out_unlock: 1121out_unlock:
1120 leb_write_unlock(ubi, vol_id, lnum); 1122 leb_write_unlock(ubi, vol_id, lnum);
1121 kfree(buf); 1123 vfree(buf);
1122 kfree(buf1); 1124 vfree(buf1);
1123 return err; 1125 return err;
1124} 1126}
1125 1127
diff --git a/drivers/mtd/ubi/gluebi.c b/drivers/mtd/ubi/gluebi.c
index fc9478d605ff..41ff74c60e14 100644
--- a/drivers/mtd/ubi/gluebi.c
+++ b/drivers/mtd/ubi/gluebi.c
@@ -282,7 +282,6 @@ int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
282 mtd->flags = MTD_WRITEABLE; 282 mtd->flags = MTD_WRITEABLE;
283 mtd->writesize = ubi->min_io_size; 283 mtd->writesize = ubi->min_io_size;
284 mtd->owner = THIS_MODULE; 284 mtd->owner = THIS_MODULE;
285 mtd->size = vol->usable_leb_size * vol->reserved_pebs;
286 mtd->erasesize = vol->usable_leb_size; 285 mtd->erasesize = vol->usable_leb_size;
287 mtd->read = gluebi_read; 286 mtd->read = gluebi_read;
288 mtd->write = gluebi_write; 287 mtd->write = gluebi_write;
@@ -290,6 +289,15 @@ int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
290 mtd->get_device = gluebi_get_device; 289 mtd->get_device = gluebi_get_device;
291 mtd->put_device = gluebi_put_device; 290 mtd->put_device = gluebi_put_device;
292 291
292 /*
293 * In case of dynamic volume, MTD device size is just volume size. In
294 * case of a static volume the size is equivalent to the amount of data
295 * bytes, which is zero at this moment and will be changed after volume
296 * update.
297 */
298 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
299 mtd->size = vol->usable_leb_size * vol->reserved_pebs;
300
293 if (add_mtd_device(mtd)) { 301 if (add_mtd_device(mtd)) {
294 ubi_err("cannot not add MTD device\n"); 302 ubi_err("cannot not add MTD device\n");
295 kfree(mtd->name); 303 kfree(mtd->name);
@@ -321,3 +329,20 @@ int ubi_destroy_gluebi(struct ubi_volume *vol)
321 kfree(mtd->name); 329 kfree(mtd->name);
322 return 0; 330 return 0;
323} 331}
332
333/**
334 * ubi_gluebi_updated - UBI volume was updated notifier.
335 * @vol: volume description object
336 *
337 * This function is called every time an UBI volume is updated. This function
338 * does nothing if volume @vol is dynamic, and changes MTD device size if the
339 * volume is static. This is needed because static volumes cannot be read past
340 * data they contain.
341 */
342void ubi_gluebi_updated(struct ubi_volume *vol)
343{
344 struct mtd_info *mtd = &vol->gluebi_mtd;
345
346 if (vol->vol_type == UBI_STATIC_VOLUME)
347 mtd->size = vol->used_bytes;
348}
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 438914d05151..b0d8f4cede97 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -125,9 +125,9 @@ static int paranoid_check_all_ff(const struct ubi_device *ubi, int pnum,
125 * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but 125 * o %UBI_IO_BITFLIPS if all the requested data were successfully read, but
126 * correctable bit-flips were detected; this is harmless but may indicate 126 * correctable bit-flips were detected; this is harmless but may indicate
127 * that this eraseblock may become bad soon (but do not have to); 127 * that this eraseblock may become bad soon (but do not have to);
128 * o %-EBADMSG if the MTD subsystem reported about data data integrity 128 * o %-EBADMSG if the MTD subsystem reported about data integrity problems, for
129 * problems, for example it can me an ECC error in case of NAND; this most 129 * example it can be an ECC error in case of NAND; this most probably means
130 * probably means that the data is corrupted; 130 * that the data is corrupted;
131 * o %-EIO if some I/O error occurred; 131 * o %-EIO if some I/O error occurred;
132 * o other negative error codes in case of other errors. 132 * o other negative error codes in case of other errors.
133 */ 133 */
@@ -298,7 +298,7 @@ retry:
298 memset(&ei, 0, sizeof(struct erase_info)); 298 memset(&ei, 0, sizeof(struct erase_info));
299 299
300 ei.mtd = ubi->mtd; 300 ei.mtd = ubi->mtd;
301 ei.addr = pnum * ubi->peb_size; 301 ei.addr = (loff_t)pnum * ubi->peb_size;
302 ei.len = ubi->peb_size; 302 ei.len = ubi->peb_size;
303 ei.callback = erase_callback; 303 ei.callback = erase_callback;
304 ei.priv = (unsigned long)&wq; 304 ei.priv = (unsigned long)&wq;
@@ -382,7 +382,7 @@ static int torture_peb(const struct ubi_device *ubi, int pnum)
382 void *buf; 382 void *buf;
383 int err, i, patt_count; 383 int err, i, patt_count;
384 384
385 buf = kmalloc(ubi->peb_size, GFP_KERNEL); 385 buf = vmalloc(ubi->peb_size);
386 if (!buf) 386 if (!buf)
387 return -ENOMEM; 387 return -ENOMEM;
388 388
@@ -437,7 +437,7 @@ out:
437 * physical eraseblock which means something is wrong with it. 437 * physical eraseblock which means something is wrong with it.
438 */ 438 */
439 err = -EIO; 439 err = -EIO;
440 kfree(buf); 440 vfree(buf);
441 return err; 441 return err;
442} 442}
443 443
@@ -557,9 +557,9 @@ static int validate_ec_hdr(const struct ubi_device *ubi,
557 long long ec; 557 long long ec;
558 int vid_hdr_offset, leb_start; 558 int vid_hdr_offset, leb_start;
559 559
560 ec = ubi64_to_cpu(ec_hdr->ec); 560 ec = be64_to_cpu(ec_hdr->ec);
561 vid_hdr_offset = ubi32_to_cpu(ec_hdr->vid_hdr_offset); 561 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset);
562 leb_start = ubi32_to_cpu(ec_hdr->data_offset); 562 leb_start = be32_to_cpu(ec_hdr->data_offset);
563 563
564 if (ec_hdr->version != UBI_VERSION) { 564 if (ec_hdr->version != UBI_VERSION) {
565 ubi_err("node with incompatible UBI version found: " 565 ubi_err("node with incompatible UBI version found: "
@@ -640,7 +640,7 @@ int ubi_io_read_ec_hdr(const struct ubi_device *ubi, int pnum,
640 read_err = err; 640 read_err = err;
641 } 641 }
642 642
643 magic = ubi32_to_cpu(ec_hdr->magic); 643 magic = be32_to_cpu(ec_hdr->magic);
644 if (magic != UBI_EC_HDR_MAGIC) { 644 if (magic != UBI_EC_HDR_MAGIC) {
645 /* 645 /*
646 * The magic field is wrong. Let's check if we have read all 646 * The magic field is wrong. Let's check if we have read all
@@ -684,7 +684,7 @@ int ubi_io_read_ec_hdr(const struct ubi_device *ubi, int pnum,
684 } 684 }
685 685
686 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 686 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
687 hdr_crc = ubi32_to_cpu(ec_hdr->hdr_crc); 687 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
688 688
689 if (hdr_crc != crc) { 689 if (hdr_crc != crc) {
690 if (verbose) { 690 if (verbose) {
@@ -729,12 +729,12 @@ int ubi_io_write_ec_hdr(const struct ubi_device *ubi, int pnum,
729 dbg_io("write EC header to PEB %d", pnum); 729 dbg_io("write EC header to PEB %d", pnum);
730 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 730 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
731 731
732 ec_hdr->magic = cpu_to_ubi32(UBI_EC_HDR_MAGIC); 732 ec_hdr->magic = cpu_to_be32(UBI_EC_HDR_MAGIC);
733 ec_hdr->version = UBI_VERSION; 733 ec_hdr->version = UBI_VERSION;
734 ec_hdr->vid_hdr_offset = cpu_to_ubi32(ubi->vid_hdr_offset); 734 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset);
735 ec_hdr->data_offset = cpu_to_ubi32(ubi->leb_start); 735 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start);
736 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 736 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
737 ec_hdr->hdr_crc = cpu_to_ubi32(crc); 737 ec_hdr->hdr_crc = cpu_to_be32(crc);
738 738
739 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 739 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
740 if (err) 740 if (err)
@@ -757,13 +757,13 @@ static int validate_vid_hdr(const struct ubi_device *ubi,
757{ 757{
758 int vol_type = vid_hdr->vol_type; 758 int vol_type = vid_hdr->vol_type;
759 int copy_flag = vid_hdr->copy_flag; 759 int copy_flag = vid_hdr->copy_flag;
760 int vol_id = ubi32_to_cpu(vid_hdr->vol_id); 760 int vol_id = be32_to_cpu(vid_hdr->vol_id);
761 int lnum = ubi32_to_cpu(vid_hdr->lnum); 761 int lnum = be32_to_cpu(vid_hdr->lnum);
762 int compat = vid_hdr->compat; 762 int compat = vid_hdr->compat;
763 int data_size = ubi32_to_cpu(vid_hdr->data_size); 763 int data_size = be32_to_cpu(vid_hdr->data_size);
764 int used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); 764 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
765 int data_pad = ubi32_to_cpu(vid_hdr->data_pad); 765 int data_pad = be32_to_cpu(vid_hdr->data_pad);
766 int data_crc = ubi32_to_cpu(vid_hdr->data_crc); 766 int data_crc = be32_to_cpu(vid_hdr->data_crc);
767 int usable_leb_size = ubi->leb_size - data_pad; 767 int usable_leb_size = ubi->leb_size - data_pad;
768 768
769 if (copy_flag != 0 && copy_flag != 1) { 769 if (copy_flag != 0 && copy_flag != 1) {
@@ -914,7 +914,7 @@ int ubi_io_read_vid_hdr(const struct ubi_device *ubi, int pnum,
914 read_err = err; 914 read_err = err;
915 } 915 }
916 916
917 magic = ubi32_to_cpu(vid_hdr->magic); 917 magic = be32_to_cpu(vid_hdr->magic);
918 if (magic != UBI_VID_HDR_MAGIC) { 918 if (magic != UBI_VID_HDR_MAGIC) {
919 /* 919 /*
920 * If we have read all 0xFF bytes, the VID header probably does 920 * If we have read all 0xFF bytes, the VID header probably does
@@ -957,7 +957,7 @@ int ubi_io_read_vid_hdr(const struct ubi_device *ubi, int pnum,
957 } 957 }
958 958
959 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 959 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
960 hdr_crc = ubi32_to_cpu(vid_hdr->hdr_crc); 960 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
961 961
962 if (hdr_crc != crc) { 962 if (hdr_crc != crc) {
963 if (verbose) { 963 if (verbose) {
@@ -1007,10 +1007,10 @@ int ubi_io_write_vid_hdr(const struct ubi_device *ubi, int pnum,
1007 if (err) 1007 if (err)
1008 return err > 0 ? -EINVAL: err; 1008 return err > 0 ? -EINVAL: err;
1009 1009
1010 vid_hdr->magic = cpu_to_ubi32(UBI_VID_HDR_MAGIC); 1010 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1011 vid_hdr->version = UBI_VERSION; 1011 vid_hdr->version = UBI_VERSION;
1012 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC); 1012 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_VID_HDR_SIZE_CRC);
1013 vid_hdr->hdr_crc = cpu_to_ubi32(crc); 1013 vid_hdr->hdr_crc = cpu_to_be32(crc);
1014 1014
1015 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1015 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1016 if (err) 1016 if (err)
@@ -1060,7 +1060,7 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1060 int err; 1060 int err;
1061 uint32_t magic; 1061 uint32_t magic;
1062 1062
1063 magic = ubi32_to_cpu(ec_hdr->magic); 1063 magic = be32_to_cpu(ec_hdr->magic);
1064 if (magic != UBI_EC_HDR_MAGIC) { 1064 if (magic != UBI_EC_HDR_MAGIC) {
1065 ubi_err("bad magic %#08x, must be %#08x", 1065 ubi_err("bad magic %#08x, must be %#08x",
1066 magic, UBI_EC_HDR_MAGIC); 1066 magic, UBI_EC_HDR_MAGIC);
@@ -1105,7 +1105,7 @@ static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1105 goto exit; 1105 goto exit;
1106 1106
1107 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 1107 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC);
1108 hdr_crc = ubi32_to_cpu(ec_hdr->hdr_crc); 1108 hdr_crc = be32_to_cpu(ec_hdr->hdr_crc);
1109 if (hdr_crc != crc) { 1109 if (hdr_crc != crc) {
1110 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc); 1110 ubi_err("bad CRC, calculated %#08x, read %#08x", crc, hdr_crc);
1111 ubi_err("paranoid check failed for PEB %d", pnum); 1111 ubi_err("paranoid check failed for PEB %d", pnum);
@@ -1137,7 +1137,7 @@ static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1137 int err; 1137 int err;
1138 uint32_t magic; 1138 uint32_t magic;
1139 1139
1140 magic = ubi32_to_cpu(vid_hdr->magic); 1140 magic = be32_to_cpu(vid_hdr->magic);
1141 if (magic != UBI_VID_HDR_MAGIC) { 1141 if (magic != UBI_VID_HDR_MAGIC) {
1142 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x", 1142 ubi_err("bad VID header magic %#08x at PEB %d, must be %#08x",
1143 magic, pnum, UBI_VID_HDR_MAGIC); 1143 magic, pnum, UBI_VID_HDR_MAGIC);
@@ -1187,7 +1187,7 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1187 goto exit; 1187 goto exit;
1188 1188
1189 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC); 1189 crc = crc32(UBI_CRC32_INIT, vid_hdr, UBI_EC_HDR_SIZE_CRC);
1190 hdr_crc = ubi32_to_cpu(vid_hdr->hdr_crc); 1190 hdr_crc = be32_to_cpu(vid_hdr->hdr_crc);
1191 if (hdr_crc != crc) { 1191 if (hdr_crc != crc) {
1192 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, " 1192 ubi_err("bad VID header CRC at PEB %d, calculated %#08x, "
1193 "read %#08x", pnum, crc, hdr_crc); 1193 "read %#08x", pnum, crc, hdr_crc);
@@ -1224,9 +1224,10 @@ static int paranoid_check_all_ff(const struct ubi_device *ubi, int pnum,
1224 void *buf; 1224 void *buf;
1225 loff_t addr = (loff_t)pnum * ubi->peb_size + offset; 1225 loff_t addr = (loff_t)pnum * ubi->peb_size + offset;
1226 1226
1227 buf = kzalloc(len, GFP_KERNEL); 1227 buf = vmalloc(len);
1228 if (!buf) 1228 if (!buf)
1229 return -ENOMEM; 1229 return -ENOMEM;
1230 memset(buf, 0, len);
1230 1231
1231 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf); 1232 err = ubi->mtd->read(ubi->mtd, addr, len, &read, buf);
1232 if (err && err != -EUCLEAN) { 1233 if (err && err != -EUCLEAN) {
@@ -1242,7 +1243,7 @@ static int paranoid_check_all_ff(const struct ubi_device *ubi, int pnum,
1242 goto fail; 1243 goto fail;
1243 } 1244 }
1244 1245
1245 kfree(buf); 1246 vfree(buf);
1246 return 0; 1247 return 0;
1247 1248
1248fail: 1249fail:
@@ -1252,7 +1253,7 @@ fail:
1252 err = 1; 1253 err = 1;
1253error: 1254error:
1254 ubi_dbg_dump_stack(); 1255 ubi_dbg_dump_stack();
1255 kfree(buf); 1256 vfree(buf);
1256 return err; 1257 return err;
1257} 1258}
1258 1259
diff --git a/drivers/mtd/ubi/kapi.c b/drivers/mtd/ubi/kapi.c
index d352c4575c3d..4a458e83e4e9 100644
--- a/drivers/mtd/ubi/kapi.c
+++ b/drivers/mtd/ubi/kapi.c
@@ -37,14 +37,9 @@ int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
37{ 37{
38 const struct ubi_device *ubi; 38 const struct ubi_device *ubi;
39 39
40 if (!try_module_get(THIS_MODULE))
41 return -ENODEV;
42
43 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES || 40 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES ||
44 !ubi_devices[ubi_num]) { 41 !ubi_devices[ubi_num])
45 module_put(THIS_MODULE);
46 return -ENODEV; 42 return -ENODEV;
47 }
48 43
49 ubi = ubi_devices[ubi_num]; 44 ubi = ubi_devices[ubi_num];
50 di->ubi_num = ubi->ubi_num; 45 di->ubi_num = ubi->ubi_num;
@@ -52,7 +47,6 @@ int ubi_get_device_info(int ubi_num, struct ubi_device_info *di)
52 di->min_io_size = ubi->min_io_size; 47 di->min_io_size = ubi->min_io_size;
53 di->ro_mode = ubi->ro_mode; 48 di->ro_mode = ubi->ro_mode;
54 di->cdev = MKDEV(ubi->major, 0); 49 di->cdev = MKDEV(ubi->major, 0);
55 module_put(THIS_MODULE);
56 return 0; 50 return 0;
57} 51}
58EXPORT_SYMBOL_GPL(ubi_get_device_info); 52EXPORT_SYMBOL_GPL(ubi_get_device_info);
@@ -319,9 +313,14 @@ int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
319 offset + len > vol->usable_leb_size) 313 offset + len > vol->usable_leb_size)
320 return -EINVAL; 314 return -EINVAL;
321 315
322 if (vol->vol_type == UBI_STATIC_VOLUME && lnum == vol->used_ebs - 1 && 316 if (vol->vol_type == UBI_STATIC_VOLUME) {
323 offset + len > vol->last_eb_bytes) 317 if (vol->used_ebs == 0)
324 return -EINVAL; 318 /* Empty static UBI volume */
319 return 0;
320 if (lnum == vol->used_ebs - 1 &&
321 offset + len > vol->last_eb_bytes)
322 return -EINVAL;
323 }
325 324
326 if (vol->upd_marker) 325 if (vol->upd_marker)
327 return -EBADF; 326 return -EBADF;
diff --git a/drivers/mtd/ubi/misc.c b/drivers/mtd/ubi/misc.c
index 38d4e6757dc7..9e2338c8e2cf 100644
--- a/drivers/mtd/ubi/misc.c
+++ b/drivers/mtd/ubi/misc.c
@@ -67,7 +67,7 @@ int ubi_check_volume(struct ubi_device *ubi, int vol_id)
67 if (vol->vol_type != UBI_STATIC_VOLUME) 67 if (vol->vol_type != UBI_STATIC_VOLUME)
68 return 0; 68 return 0;
69 69
70 buf = kmalloc(vol->usable_leb_size, GFP_KERNEL); 70 buf = vmalloc(vol->usable_leb_size);
71 if (!buf) 71 if (!buf)
72 return -ENOMEM; 72 return -ENOMEM;
73 73
@@ -87,7 +87,7 @@ int ubi_check_volume(struct ubi_device *ubi, int vol_id)
87 } 87 }
88 } 88 }
89 89
90 kfree(buf); 90 vfree(buf);
91 return err; 91 return err;
92} 92}
93 93
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 473f3200b868..94ee54934411 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -24,7 +24,7 @@
24 * This unit is responsible for scanning the flash media, checking UBI 24 * This unit is responsible for scanning the flash media, checking UBI
25 * headers and providing complete information about the UBI flash image. 25 * headers and providing complete information about the UBI flash image.
26 * 26 *
27 * The scanning information is reoresented by a &struct ubi_scan_info' object. 27 * The scanning information is represented by a &struct ubi_scan_info' object.
28 * Information about found volumes is represented by &struct ubi_scan_volume 28 * Information about found volumes is represented by &struct ubi_scan_volume
29 * objects which are kept in volume RB-tree with root at the @volumes field. 29 * objects which are kept in volume RB-tree with root at the @volumes field.
30 * The RB-tree is indexed by the volume ID. 30 * The RB-tree is indexed by the volume ID.
@@ -55,8 +55,19 @@ static int paranoid_check_si(const struct ubi_device *ubi,
55static struct ubi_ec_hdr *ech; 55static struct ubi_ec_hdr *ech;
56static struct ubi_vid_hdr *vidh; 56static struct ubi_vid_hdr *vidh;
57 57
58int ubi_scan_add_to_list(struct ubi_scan_info *si, int pnum, int ec, 58/**
59 struct list_head *list) 59 * add_to_list - add physical eraseblock to a list.
60 * @si: scanning information
61 * @pnum: physical eraseblock number to add
62 * @ec: erase counter of the physical eraseblock
63 * @list: the list to add to
64 *
65 * This function adds physical eraseblock @pnum to free, erase, corrupted or
66 * alien lists. Returns zero in case of success and a negative error code in
67 * case of failure.
68 */
69static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
70 struct list_head *list)
60{ 71{
61 struct ubi_scan_leb *seb; 72 struct ubi_scan_leb *seb;
62 73
@@ -121,9 +132,9 @@ static int validate_vid_hdr(const struct ubi_vid_hdr *vid_hdr,
121 const struct ubi_scan_volume *sv, int pnum) 132 const struct ubi_scan_volume *sv, int pnum)
122{ 133{
123 int vol_type = vid_hdr->vol_type; 134 int vol_type = vid_hdr->vol_type;
124 int vol_id = ubi32_to_cpu(vid_hdr->vol_id); 135 int vol_id = be32_to_cpu(vid_hdr->vol_id);
125 int used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); 136 int used_ebs = be32_to_cpu(vid_hdr->used_ebs);
126 int data_pad = ubi32_to_cpu(vid_hdr->data_pad); 137 int data_pad = be32_to_cpu(vid_hdr->data_pad);
127 138
128 if (sv->leb_count != 0) { 139 if (sv->leb_count != 0) {
129 int sv_vol_type; 140 int sv_vol_type;
@@ -189,7 +200,7 @@ static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
189 struct ubi_scan_volume *sv; 200 struct ubi_scan_volume *sv;
190 struct rb_node **p = &si->volumes.rb_node, *parent = NULL; 201 struct rb_node **p = &si->volumes.rb_node, *parent = NULL;
191 202
192 ubi_assert(vol_id == ubi32_to_cpu(vid_hdr->vol_id)); 203 ubi_assert(vol_id == be32_to_cpu(vid_hdr->vol_id));
193 204
194 /* Walk the volume RB-tree to look if this volume is already present */ 205 /* Walk the volume RB-tree to look if this volume is already present */
195 while (*p) { 206 while (*p) {
@@ -211,11 +222,10 @@ static struct ubi_scan_volume *add_volume(struct ubi_scan_info *si, int vol_id,
211 return ERR_PTR(-ENOMEM); 222 return ERR_PTR(-ENOMEM);
212 223
213 sv->highest_lnum = sv->leb_count = 0; 224 sv->highest_lnum = sv->leb_count = 0;
214 si->max_sqnum = 0;
215 sv->vol_id = vol_id; 225 sv->vol_id = vol_id;
216 sv->root = RB_ROOT; 226 sv->root = RB_ROOT;
217 sv->used_ebs = ubi32_to_cpu(vid_hdr->used_ebs); 227 sv->used_ebs = be32_to_cpu(vid_hdr->used_ebs);
218 sv->data_pad = ubi32_to_cpu(vid_hdr->data_pad); 228 sv->data_pad = be32_to_cpu(vid_hdr->data_pad);
219 sv->compat = vid_hdr->compat; 229 sv->compat = vid_hdr->compat;
220 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME 230 sv->vol_type = vid_hdr->vol_type == UBI_VID_DYNAMIC ? UBI_DYNAMIC_VOLUME
221 : UBI_STATIC_VOLUME; 231 : UBI_STATIC_VOLUME;
@@ -257,10 +267,10 @@ static int compare_lebs(const struct ubi_device *ubi,
257 int len, err, second_is_newer, bitflips = 0, corrupted = 0; 267 int len, err, second_is_newer, bitflips = 0, corrupted = 0;
258 uint32_t data_crc, crc; 268 uint32_t data_crc, crc;
259 struct ubi_vid_hdr *vidh = NULL; 269 struct ubi_vid_hdr *vidh = NULL;
260 unsigned long long sqnum2 = ubi64_to_cpu(vid_hdr->sqnum); 270 unsigned long long sqnum2 = be64_to_cpu(vid_hdr->sqnum);
261 271
262 if (seb->sqnum == 0 && sqnum2 == 0) { 272 if (seb->sqnum == 0 && sqnum2 == 0) {
263 long long abs, v1 = seb->leb_ver, v2 = ubi32_to_cpu(vid_hdr->leb_ver); 273 long long abs, v1 = seb->leb_ver, v2 = be32_to_cpu(vid_hdr->leb_ver);
264 274
265 /* 275 /*
266 * UBI constantly increases the logical eraseblock version 276 * UBI constantly increases the logical eraseblock version
@@ -344,8 +354,8 @@ static int compare_lebs(const struct ubi_device *ubi,
344 354
345 /* Read the data of the copy and check the CRC */ 355 /* Read the data of the copy and check the CRC */
346 356
347 len = ubi32_to_cpu(vid_hdr->data_size); 357 len = be32_to_cpu(vid_hdr->data_size);
348 buf = kmalloc(len, GFP_KERNEL); 358 buf = vmalloc(len);
349 if (!buf) { 359 if (!buf) {
350 err = -ENOMEM; 360 err = -ENOMEM;
351 goto out_free_vidh; 361 goto out_free_vidh;
@@ -355,7 +365,7 @@ static int compare_lebs(const struct ubi_device *ubi,
355 if (err && err != UBI_IO_BITFLIPS) 365 if (err && err != UBI_IO_BITFLIPS)
356 goto out_free_buf; 366 goto out_free_buf;
357 367
358 data_crc = ubi32_to_cpu(vid_hdr->data_crc); 368 data_crc = be32_to_cpu(vid_hdr->data_crc);
359 crc = crc32(UBI_CRC32_INIT, buf, len); 369 crc = crc32(UBI_CRC32_INIT, buf, len);
360 if (crc != data_crc) { 370 if (crc != data_crc) {
361 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x", 371 dbg_bld("PEB %d CRC error: calculated %#08x, must be %#08x",
@@ -368,7 +378,7 @@ static int compare_lebs(const struct ubi_device *ubi,
368 bitflips = !!err; 378 bitflips = !!err;
369 } 379 }
370 380
371 kfree(buf); 381 vfree(buf);
372 ubi_free_vid_hdr(ubi, vidh); 382 ubi_free_vid_hdr(ubi, vidh);
373 383
374 if (second_is_newer) 384 if (second_is_newer)
@@ -379,7 +389,7 @@ static int compare_lebs(const struct ubi_device *ubi,
379 return second_is_newer | (bitflips << 1) | (corrupted << 2); 389 return second_is_newer | (bitflips << 1) | (corrupted << 2);
380 390
381out_free_buf: 391out_free_buf:
382 kfree(buf); 392 vfree(buf);
383out_free_vidh: 393out_free_vidh:
384 ubi_free_vid_hdr(ubi, vidh); 394 ubi_free_vid_hdr(ubi, vidh);
385 ubi_assert(err < 0); 395 ubi_assert(err < 0);
@@ -396,8 +406,12 @@ out_free_vidh:
396 * @vid_hdr: the volume identifier header 406 * @vid_hdr: the volume identifier header
397 * @bitflips: if bit-flips were detected when this physical eraseblock was read 407 * @bitflips: if bit-flips were detected when this physical eraseblock was read
398 * 408 *
399 * This function returns zero in case of success and a negative error code in 409 * This function adds information about a used physical eraseblock to the
400 * case of failure. 410 * 'used' tree of the corresponding volume. The function is rather complex
411 * because it has to handle cases when this is not the first physical
412 * eraseblock belonging to the same logical eraseblock, and the newer one has
413 * to be picked, while the older one has to be dropped. This function returns
414 * zero in case of success and a negative error code in case of failure.
401 */ 415 */
402int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si, 416int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
403 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, 417 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
@@ -410,10 +424,10 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
410 struct ubi_scan_leb *seb; 424 struct ubi_scan_leb *seb;
411 struct rb_node **p, *parent = NULL; 425 struct rb_node **p, *parent = NULL;
412 426
413 vol_id = ubi32_to_cpu(vid_hdr->vol_id); 427 vol_id = be32_to_cpu(vid_hdr->vol_id);
414 lnum = ubi32_to_cpu(vid_hdr->lnum); 428 lnum = be32_to_cpu(vid_hdr->lnum);
415 sqnum = ubi64_to_cpu(vid_hdr->sqnum); 429 sqnum = be64_to_cpu(vid_hdr->sqnum);
416 leb_ver = ubi32_to_cpu(vid_hdr->leb_ver); 430 leb_ver = be32_to_cpu(vid_hdr->leb_ver);
417 431
418 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d", 432 dbg_bld("PEB %d, LEB %d:%d, EC %d, sqnum %llu, ver %u, bitflips %d",
419 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips); 433 pnum, vol_id, lnum, ec, sqnum, leb_ver, bitflips);
@@ -422,6 +436,9 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
422 if (IS_ERR(sv) < 0) 436 if (IS_ERR(sv) < 0)
423 return PTR_ERR(sv); 437 return PTR_ERR(sv);
424 438
439 if (si->max_sqnum < sqnum)
440 si->max_sqnum = sqnum;
441
425 /* 442 /*
426 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look 443 * Walk the RB-tree of logical eraseblocks of volume @vol_id to look
427 * if this is the first instance of this logical eraseblock or not. 444 * if this is the first instance of this logical eraseblock or not.
@@ -492,11 +509,11 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
492 return err; 509 return err;
493 510
494 if (cmp_res & 4) 511 if (cmp_res & 4)
495 err = ubi_scan_add_to_list(si, seb->pnum, 512 err = add_to_list(si, seb->pnum, seb->ec,
496 seb->ec, &si->corr); 513 &si->corr);
497 else 514 else
498 err = ubi_scan_add_to_list(si, seb->pnum, 515 err = add_to_list(si, seb->pnum, seb->ec,
499 seb->ec, &si->erase); 516 &si->erase);
500 if (err) 517 if (err)
501 return err; 518 return err;
502 519
@@ -508,7 +525,7 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
508 525
509 if (sv->highest_lnum == lnum) 526 if (sv->highest_lnum == lnum)
510 sv->last_data_size = 527 sv->last_data_size =
511 ubi32_to_cpu(vid_hdr->data_size); 528 be32_to_cpu(vid_hdr->data_size);
512 529
513 return 0; 530 return 0;
514 } else { 531 } else {
@@ -517,11 +534,9 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
517 * previously. 534 * previously.
518 */ 535 */
519 if (cmp_res & 4) 536 if (cmp_res & 4)
520 return ubi_scan_add_to_list(si, pnum, ec, 537 return add_to_list(si, pnum, ec, &si->corr);
521 &si->corr);
522 else 538 else
523 return ubi_scan_add_to_list(si, pnum, ec, 539 return add_to_list(si, pnum, ec, &si->erase);
524 &si->erase);
525 } 540 }
526 } 541 }
527 542
@@ -547,12 +562,9 @@ int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
547 562
548 if (sv->highest_lnum <= lnum) { 563 if (sv->highest_lnum <= lnum) {
549 sv->highest_lnum = lnum; 564 sv->highest_lnum = lnum;
550 sv->last_data_size = ubi32_to_cpu(vid_hdr->data_size); 565 sv->last_data_size = be32_to_cpu(vid_hdr->data_size);
551 } 566 }
552 567
553 if (si->max_sqnum < sqnum)
554 si->max_sqnum = sqnum;
555
556 sv->leb_count += 1; 568 sv->leb_count += 1;
557 rb_link_node(&seb->u.rb, parent, p); 569 rb_link_node(&seb->u.rb, parent, p);
558 rb_insert_color(&seb->u.rb, &sv->root); 570 rb_insert_color(&seb->u.rb, &sv->root);
@@ -674,7 +686,7 @@ int ubi_scan_erase_peb(const struct ubi_device *ubi,
674 return -EINVAL; 686 return -EINVAL;
675 } 687 }
676 688
677 ec_hdr->ec = cpu_to_ubi64(ec); 689 ec_hdr->ec = cpu_to_be64(ec);
678 690
679 err = ubi_io_sync_erase(ubi, pnum, 0); 691 err = ubi_io_sync_erase(ubi, pnum, 0);
680 if (err < 0) 692 if (err < 0)
@@ -754,7 +766,7 @@ struct ubi_scan_leb *ubi_scan_get_free_peb(const struct ubi_device *ubi,
754 * @si: scanning information 766 * @si: scanning information
755 * @pnum: the physical eraseblock number 767 * @pnum: the physical eraseblock number
756 * 768 *
757 * This function returns a zero if the physical eraseblock was succesfully 769 * This function returns a zero if the physical eraseblock was successfully
758 * handled and a negative error code in case of failure. 770 * handled and a negative error code in case of failure.
759 */ 771 */
760static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum) 772static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum)
@@ -783,8 +795,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
783 else if (err == UBI_IO_BITFLIPS) 795 else if (err == UBI_IO_BITFLIPS)
784 bitflips = 1; 796 bitflips = 1;
785 else if (err == UBI_IO_PEB_EMPTY) 797 else if (err == UBI_IO_PEB_EMPTY)
786 return ubi_scan_add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, 798 return add_to_list(si, pnum, UBI_SCAN_UNKNOWN_EC, &si->erase);
787 &si->erase);
788 else if (err == UBI_IO_BAD_EC_HDR) { 799 else if (err == UBI_IO_BAD_EC_HDR) {
789 /* 800 /*
790 * We have to also look at the VID header, possibly it is not 801 * We have to also look at the VID header, possibly it is not
@@ -806,7 +817,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
806 return -EINVAL; 817 return -EINVAL;
807 } 818 }
808 819
809 ec = ubi64_to_cpu(ech->ec); 820 ec = be64_to_cpu(ech->ec);
810 if (ec > UBI_MAX_ERASECOUNTER) { 821 if (ec > UBI_MAX_ERASECOUNTER) {
811 /* 822 /*
812 * Erase counter overflow. The EC headers have 64 bits 823 * Erase counter overflow. The EC headers have 64 bits
@@ -832,28 +843,28 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
832 else if (err == UBI_IO_BAD_VID_HDR || 843 else if (err == UBI_IO_BAD_VID_HDR ||
833 (err == UBI_IO_PEB_FREE && ec_corr)) { 844 (err == UBI_IO_PEB_FREE && ec_corr)) {
834 /* VID header is corrupted */ 845 /* VID header is corrupted */
835 err = ubi_scan_add_to_list(si, pnum, ec, &si->corr); 846 err = add_to_list(si, pnum, ec, &si->corr);
836 if (err) 847 if (err)
837 return err; 848 return err;
838 goto adjust_mean_ec; 849 goto adjust_mean_ec;
839 } else if (err == UBI_IO_PEB_FREE) { 850 } else if (err == UBI_IO_PEB_FREE) {
840 /* No VID header - the physical eraseblock is free */ 851 /* No VID header - the physical eraseblock is free */
841 err = ubi_scan_add_to_list(si, pnum, ec, &si->free); 852 err = add_to_list(si, pnum, ec, &si->free);
842 if (err) 853 if (err)
843 return err; 854 return err;
844 goto adjust_mean_ec; 855 goto adjust_mean_ec;
845 } 856 }
846 857
847 vol_id = ubi32_to_cpu(vidh->vol_id); 858 vol_id = be32_to_cpu(vidh->vol_id);
848 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) { 859 if (vol_id > UBI_MAX_VOLUMES && vol_id != UBI_LAYOUT_VOL_ID) {
849 int lnum = ubi32_to_cpu(vidh->lnum); 860 int lnum = be32_to_cpu(vidh->lnum);
850 861
851 /* Unsupported internal volume */ 862 /* Unsupported internal volume */
852 switch (vidh->compat) { 863 switch (vidh->compat) {
853 case UBI_COMPAT_DELETE: 864 case UBI_COMPAT_DELETE:
854 ubi_msg("\"delete\" compatible internal volume %d:%d" 865 ubi_msg("\"delete\" compatible internal volume %d:%d"
855 " found, remove it", vol_id, lnum); 866 " found, remove it", vol_id, lnum);
856 err = ubi_scan_add_to_list(si, pnum, ec, &si->corr); 867 err = add_to_list(si, pnum, ec, &si->corr);
857 if (err) 868 if (err)
858 return err; 869 return err;
859 break; 870 break;
@@ -868,7 +879,7 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
868 case UBI_COMPAT_PRESERVE: 879 case UBI_COMPAT_PRESERVE:
869 ubi_msg("\"preserve\" compatible internal volume %d:%d" 880 ubi_msg("\"preserve\" compatible internal volume %d:%d"
870 " found", vol_id, lnum); 881 " found", vol_id, lnum);
871 err = ubi_scan_add_to_list(si, pnum, ec, &si->alien); 882 err = add_to_list(si, pnum, ec, &si->alien);
872 if (err) 883 if (err)
873 return err; 884 return err;
874 si->alien_peb_count += 1; 885 si->alien_peb_count += 1;
@@ -1109,7 +1120,7 @@ static int paranoid_check_si(const struct ubi_device *ubi,
1109 uint8_t *buf; 1120 uint8_t *buf;
1110 1121
1111 /* 1122 /*
1112 * At first, check that scanning information is ok. 1123 * At first, check that scanning information is OK.
1113 */ 1124 */
1114 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) { 1125 ubi_rb_for_each_entry(rb1, sv, &si->volumes, rb) {
1115 int leb_count = 0; 1126 int leb_count = 0;
@@ -1249,12 +1260,12 @@ static int paranoid_check_si(const struct ubi_device *ubi,
1249 goto bad_vid_hdr; 1260 goto bad_vid_hdr;
1250 } 1261 }
1251 1262
1252 if (seb->sqnum != ubi64_to_cpu(vidh->sqnum)) { 1263 if (seb->sqnum != be64_to_cpu(vidh->sqnum)) {
1253 ubi_err("bad sqnum %llu", seb->sqnum); 1264 ubi_err("bad sqnum %llu", seb->sqnum);
1254 goto bad_vid_hdr; 1265 goto bad_vid_hdr;
1255 } 1266 }
1256 1267
1257 if (sv->vol_id != ubi32_to_cpu(vidh->vol_id)) { 1268 if (sv->vol_id != be32_to_cpu(vidh->vol_id)) {
1258 ubi_err("bad vol_id %d", sv->vol_id); 1269 ubi_err("bad vol_id %d", sv->vol_id);
1259 goto bad_vid_hdr; 1270 goto bad_vid_hdr;
1260 } 1271 }
@@ -1264,22 +1275,22 @@ static int paranoid_check_si(const struct ubi_device *ubi,
1264 goto bad_vid_hdr; 1275 goto bad_vid_hdr;
1265 } 1276 }
1266 1277
1267 if (seb->lnum != ubi32_to_cpu(vidh->lnum)) { 1278 if (seb->lnum != be32_to_cpu(vidh->lnum)) {
1268 ubi_err("bad lnum %d", seb->lnum); 1279 ubi_err("bad lnum %d", seb->lnum);
1269 goto bad_vid_hdr; 1280 goto bad_vid_hdr;
1270 } 1281 }
1271 1282
1272 if (sv->used_ebs != ubi32_to_cpu(vidh->used_ebs)) { 1283 if (sv->used_ebs != be32_to_cpu(vidh->used_ebs)) {
1273 ubi_err("bad used_ebs %d", sv->used_ebs); 1284 ubi_err("bad used_ebs %d", sv->used_ebs);
1274 goto bad_vid_hdr; 1285 goto bad_vid_hdr;
1275 } 1286 }
1276 1287
1277 if (sv->data_pad != ubi32_to_cpu(vidh->data_pad)) { 1288 if (sv->data_pad != be32_to_cpu(vidh->data_pad)) {
1278 ubi_err("bad data_pad %d", sv->data_pad); 1289 ubi_err("bad data_pad %d", sv->data_pad);
1279 goto bad_vid_hdr; 1290 goto bad_vid_hdr;
1280 } 1291 }
1281 1292
1282 if (seb->leb_ver != ubi32_to_cpu(vidh->leb_ver)) { 1293 if (seb->leb_ver != be32_to_cpu(vidh->leb_ver)) {
1283 ubi_err("bad leb_ver %u", seb->leb_ver); 1294 ubi_err("bad leb_ver %u", seb->leb_ver);
1284 goto bad_vid_hdr; 1295 goto bad_vid_hdr;
1285 } 1296 }
@@ -1288,12 +1299,12 @@ static int paranoid_check_si(const struct ubi_device *ubi,
1288 if (!last_seb) 1299 if (!last_seb)
1289 continue; 1300 continue;
1290 1301
1291 if (sv->highest_lnum != ubi32_to_cpu(vidh->lnum)) { 1302 if (sv->highest_lnum != be32_to_cpu(vidh->lnum)) {
1292 ubi_err("bad highest_lnum %d", sv->highest_lnum); 1303 ubi_err("bad highest_lnum %d", sv->highest_lnum);
1293 goto bad_vid_hdr; 1304 goto bad_vid_hdr;
1294 } 1305 }
1295 1306
1296 if (sv->last_data_size != ubi32_to_cpu(vidh->data_size)) { 1307 if (sv->last_data_size != be32_to_cpu(vidh->data_size)) {
1297 ubi_err("bad last_data_size %d", sv->last_data_size); 1308 ubi_err("bad last_data_size %d", sv->last_data_size);
1298 goto bad_vid_hdr; 1309 goto bad_vid_hdr;
1299 } 1310 }
@@ -1310,8 +1321,10 @@ static int paranoid_check_si(const struct ubi_device *ubi,
1310 memset(buf, 1, ubi->peb_count); 1321 memset(buf, 1, ubi->peb_count);
1311 for (pnum = 0; pnum < ubi->peb_count; pnum++) { 1322 for (pnum = 0; pnum < ubi->peb_count; pnum++) {
1312 err = ubi_io_is_bad(ubi, pnum); 1323 err = ubi_io_is_bad(ubi, pnum);
1313 if (err < 0) 1324 if (err < 0) {
1325 kfree(buf);
1314 return err; 1326 return err;
1327 }
1315 else if (err) 1328 else if (err)
1316 buf[pnum] = 0; 1329 buf[pnum] = 0;
1317 } 1330 }
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
index 3949f6192c76..140e82e26534 100644
--- a/drivers/mtd/ubi/scan.h
+++ b/drivers/mtd/ubi/scan.h
@@ -147,8 +147,6 @@ static inline void ubi_scan_move_to_list(struct ubi_scan_volume *sv,
147 list_add_tail(&seb->u.list, list); 147 list_add_tail(&seb->u.list, list);
148} 148}
149 149
150int ubi_scan_add_to_list(struct ubi_scan_info *si, int pnum, int ec,
151 struct list_head *list);
152int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si, 150int ubi_scan_add_used(const struct ubi_device *ubi, struct ubi_scan_info *si,
153 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr, 151 int pnum, int ec, const struct ubi_vid_hdr *vid_hdr,
154 int bitflips); 152 int bitflips);
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index feb647f108f0..5959f91be240 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -35,6 +35,7 @@
35#include <linux/cdev.h> 35#include <linux/cdev.h>
36#include <linux/device.h> 36#include <linux/device.h>
37#include <linux/string.h> 37#include <linux/string.h>
38#include <linux/vmalloc.h>
38#include <linux/mtd/mtd.h> 39#include <linux/mtd/mtd.h>
39 40
40#include <mtd/ubi-header.h> 41#include <mtd/ubi-header.h>
@@ -374,9 +375,11 @@ void ubi_calculate_reserved(struct ubi_device *ubi);
374#ifdef CONFIG_MTD_UBI_GLUEBI 375#ifdef CONFIG_MTD_UBI_GLUEBI
375int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol); 376int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol);
376int ubi_destroy_gluebi(struct ubi_volume *vol); 377int ubi_destroy_gluebi(struct ubi_volume *vol);
378void ubi_gluebi_updated(struct ubi_volume *vol);
377#else 379#else
378#define ubi_create_gluebi(ubi, vol) 0 380#define ubi_create_gluebi(ubi, vol) 0
379#define ubi_destroy_gluebi(vol) 0 381#define ubi_destroy_gluebi(vol) 0
382#define ubi_gluebi_updated(vol)
380#endif 383#endif
381 384
382/* eba.c */ 385/* eba.c */
diff --git a/drivers/mtd/ubi/upd.c b/drivers/mtd/ubi/upd.c
index 8925b977e3dc..0efc586a8328 100644
--- a/drivers/mtd/ubi/upd.c
+++ b/drivers/mtd/ubi/upd.c
@@ -150,7 +150,7 @@ int ubi_start_update(struct ubi_device *ubi, int vol_id, long long bytes)
150 vol->updating = 0; 150 vol->updating = 0;
151 } 151 }
152 152
153 vol->upd_buf = kmalloc(ubi->leb_size, GFP_KERNEL); 153 vol->upd_buf = vmalloc(ubi->leb_size);
154 if (!vol->upd_buf) 154 if (!vol->upd_buf)
155 return -ENOMEM; 155 return -ENOMEM;
156 156
@@ -339,7 +339,7 @@ int ubi_more_update_data(struct ubi_device *ubi, int vol_id,
339 err = ubi_wl_flush(ubi); 339 err = ubi_wl_flush(ubi);
340 if (err == 0) { 340 if (err == 0) {
341 err = to_write; 341 err = to_write;
342 kfree(vol->upd_buf); 342 vfree(vol->upd_buf);
343 vol->updating = 0; 343 vol->updating = 0;
344 } 344 }
345 } 345 }
diff --git a/drivers/mtd/ubi/vmt.c b/drivers/mtd/ubi/vmt.c
index 622d0d18952c..ea0d5c825ab4 100644
--- a/drivers/mtd/ubi/vmt.c
+++ b/drivers/mtd/ubi/vmt.c
@@ -228,7 +228,7 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
228 for (i = 0; i < ubi->vtbl_slots; i++) 228 for (i = 0; i < ubi->vtbl_slots; i++)
229 if (ubi->volumes[i] && 229 if (ubi->volumes[i] &&
230 ubi->volumes[i]->name_len == req->name_len && 230 ubi->volumes[i]->name_len == req->name_len &&
231 strcmp(ubi->volumes[i]->name, req->name) == 0) { 231 !strcmp(ubi->volumes[i]->name, req->name)) {
232 dbg_err("volume \"%s\" exists (ID %d)", req->name, i); 232 dbg_err("volume \"%s\" exists (ID %d)", req->name, i);
233 goto out_unlock; 233 goto out_unlock;
234 } 234 }
@@ -243,7 +243,6 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
243 /* Reserve physical eraseblocks */ 243 /* Reserve physical eraseblocks */
244 if (vol->reserved_pebs > ubi->avail_pebs) { 244 if (vol->reserved_pebs > ubi->avail_pebs) {
245 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs); 245 dbg_err("not enough PEBs, only %d available", ubi->avail_pebs);
246 spin_unlock(&ubi->volumes_lock);
247 err = -ENOSPC; 246 err = -ENOSPC;
248 goto out_unlock; 247 goto out_unlock;
249 } 248 }
@@ -281,7 +280,8 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
281 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { 280 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
282 vol->used_ebs = vol->reserved_pebs; 281 vol->used_ebs = vol->reserved_pebs;
283 vol->last_eb_bytes = vol->usable_leb_size; 282 vol->last_eb_bytes = vol->usable_leb_size;
284 vol->used_bytes = vol->used_ebs * vol->usable_leb_size; 283 vol->used_bytes =
284 (long long)vol->used_ebs * vol->usable_leb_size;
285 } else { 285 } else {
286 bytes = vol->used_bytes; 286 bytes = vol->used_bytes;
287 vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size); 287 vol->last_eb_bytes = do_div(bytes, vol->usable_leb_size);
@@ -320,10 +320,10 @@ int ubi_create_volume(struct ubi_device *ubi, struct ubi_mkvol_req *req)
320 320
321 /* Fill volume table record */ 321 /* Fill volume table record */
322 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record)); 322 memset(&vtbl_rec, 0, sizeof(struct ubi_vtbl_record));
323 vtbl_rec.reserved_pebs = cpu_to_ubi32(vol->reserved_pebs); 323 vtbl_rec.reserved_pebs = cpu_to_be32(vol->reserved_pebs);
324 vtbl_rec.alignment = cpu_to_ubi32(vol->alignment); 324 vtbl_rec.alignment = cpu_to_be32(vol->alignment);
325 vtbl_rec.data_pad = cpu_to_ubi32(vol->data_pad); 325 vtbl_rec.data_pad = cpu_to_be32(vol->data_pad);
326 vtbl_rec.name_len = cpu_to_ubi16(vol->name_len); 326 vtbl_rec.name_len = cpu_to_be16(vol->name_len);
327 if (vol->vol_type == UBI_DYNAMIC_VOLUME) 327 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
328 vtbl_rec.vol_type = UBI_VID_DYNAMIC; 328 vtbl_rec.vol_type = UBI_VID_DYNAMIC;
329 else 329 else
@@ -352,6 +352,7 @@ out_acc:
352 spin_lock(&ubi->volumes_lock); 352 spin_lock(&ubi->volumes_lock);
353 ubi->rsvd_pebs -= vol->reserved_pebs; 353 ubi->rsvd_pebs -= vol->reserved_pebs;
354 ubi->avail_pebs += vol->reserved_pebs; 354 ubi->avail_pebs += vol->reserved_pebs;
355 ubi->volumes[vol_id] = NULL;
355out_unlock: 356out_unlock:
356 spin_unlock(&ubi->volumes_lock); 357 spin_unlock(&ubi->volumes_lock);
357 kfree(vol); 358 kfree(vol);
@@ -368,6 +369,7 @@ out_sysfs:
368 spin_lock(&ubi->volumes_lock); 369 spin_lock(&ubi->volumes_lock);
369 ubi->rsvd_pebs -= vol->reserved_pebs; 370 ubi->rsvd_pebs -= vol->reserved_pebs;
370 ubi->avail_pebs += vol->reserved_pebs; 371 ubi->avail_pebs += vol->reserved_pebs;
372 ubi->volumes[vol_id] = NULL;
371 spin_unlock(&ubi->volumes_lock); 373 spin_unlock(&ubi->volumes_lock);
372 volume_sysfs_close(vol); 374 volume_sysfs_close(vol);
373 return err; 375 return err;
@@ -503,7 +505,7 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
503 505
504 /* Change volume table record */ 506 /* Change volume table record */
505 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record)); 507 memcpy(&vtbl_rec, &ubi->vtbl[vol_id], sizeof(struct ubi_vtbl_record));
506 vtbl_rec.reserved_pebs = cpu_to_ubi32(reserved_pebs); 508 vtbl_rec.reserved_pebs = cpu_to_be32(reserved_pebs);
507 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec); 509 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
508 if (err) 510 if (err)
509 goto out_acc; 511 goto out_acc;
@@ -537,7 +539,8 @@ int ubi_resize_volume(struct ubi_volume_desc *desc, int reserved_pebs)
537 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { 539 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
538 vol->used_ebs = reserved_pebs; 540 vol->used_ebs = reserved_pebs;
539 vol->last_eb_bytes = vol->usable_leb_size; 541 vol->last_eb_bytes = vol->usable_leb_size;
540 vol->used_bytes = vol->used_ebs * vol->usable_leb_size; 542 vol->used_bytes =
543 (long long)vol->used_ebs * vol->usable_leb_size;
541 } 544 }
542 545
543 paranoid_check_volumes(ubi); 546 paranoid_check_volumes(ubi);
@@ -643,21 +646,33 @@ void ubi_free_volume(struct ubi_device *ubi, int vol_id)
643 * @ubi: UBI device description object 646 * @ubi: UBI device description object
644 * @vol_id: volume ID 647 * @vol_id: volume ID
645 */ 648 */
646static void paranoid_check_volume(const struct ubi_device *ubi, int vol_id) 649static void paranoid_check_volume(struct ubi_device *ubi, int vol_id)
647{ 650{
648 int idx = vol_id2idx(ubi, vol_id); 651 int idx = vol_id2idx(ubi, vol_id);
649 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker; 652 int reserved_pebs, alignment, data_pad, vol_type, name_len, upd_marker;
650 const struct ubi_volume *vol = ubi->volumes[idx]; 653 const struct ubi_volume *vol;
651 long long n; 654 long long n;
652 const char *name; 655 const char *name;
653 656
654 reserved_pebs = ubi32_to_cpu(ubi->vtbl[vol_id].reserved_pebs); 657 spin_lock(&ubi->volumes_lock);
658 reserved_pebs = be32_to_cpu(ubi->vtbl[vol_id].reserved_pebs);
659 vol = ubi->volumes[idx];
655 660
656 if (!vol) { 661 if (!vol) {
657 if (reserved_pebs) { 662 if (reserved_pebs) {
658 ubi_err("no volume info, but volume exists"); 663 ubi_err("no volume info, but volume exists");
659 goto fail; 664 goto fail;
660 } 665 }
666 spin_unlock(&ubi->volumes_lock);
667 return;
668 }
669
670 if (vol->exclusive) {
671 /*
672 * The volume may be being created at the moment, do not check
673 * it (e.g., it may be in the middle of ubi_create_volume().
674 */
675 spin_unlock(&ubi->volumes_lock);
661 return; 676 return;
662 } 677 }
663 678
@@ -726,7 +741,7 @@ static void paranoid_check_volume(const struct ubi_device *ubi, int vol_id)
726 goto fail; 741 goto fail;
727 } 742 }
728 743
729 n = vol->used_ebs * vol->usable_leb_size; 744 n = (long long)vol->used_ebs * vol->usable_leb_size;
730 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { 745 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
731 if (vol->corrupted != 0) { 746 if (vol->corrupted != 0) {
732 ubi_err("corrupted dynamic volume"); 747 ubi_err("corrupted dynamic volume");
@@ -765,9 +780,9 @@ static void paranoid_check_volume(const struct ubi_device *ubi, int vol_id)
765 } 780 }
766 } 781 }
767 782
768 alignment = ubi32_to_cpu(ubi->vtbl[vol_id].alignment); 783 alignment = be32_to_cpu(ubi->vtbl[vol_id].alignment);
769 data_pad = ubi32_to_cpu(ubi->vtbl[vol_id].data_pad); 784 data_pad = be32_to_cpu(ubi->vtbl[vol_id].data_pad);
770 name_len = ubi16_to_cpu(ubi->vtbl[vol_id].name_len); 785 name_len = be16_to_cpu(ubi->vtbl[vol_id].name_len);
771 upd_marker = ubi->vtbl[vol_id].upd_marker; 786 upd_marker = ubi->vtbl[vol_id].upd_marker;
772 name = &ubi->vtbl[vol_id].name[0]; 787 name = &ubi->vtbl[vol_id].name[0];
773 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC) 788 if (ubi->vtbl[vol_id].vol_type == UBI_VID_DYNAMIC)
@@ -782,12 +797,14 @@ static void paranoid_check_volume(const struct ubi_device *ubi, int vol_id)
782 goto fail; 797 goto fail;
783 } 798 }
784 799
800 spin_unlock(&ubi->volumes_lock);
785 return; 801 return;
786 802
787fail: 803fail:
788 ubi_err("paranoid check failed"); 804 ubi_err("paranoid check failed for volume %d", vol_id);
789 ubi_dbg_dump_vol_info(vol); 805 ubi_dbg_dump_vol_info(vol);
790 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id); 806 ubi_dbg_dump_vtbl_record(&ubi->vtbl[vol_id], vol_id);
807 spin_unlock(&ubi->volumes_lock);
791 BUG(); 808 BUG();
792} 809}
793 810
@@ -800,10 +817,8 @@ static void paranoid_check_volumes(struct ubi_device *ubi)
800 int i; 817 int i;
801 818
802 mutex_lock(&ubi->vtbl_mutex); 819 mutex_lock(&ubi->vtbl_mutex);
803 spin_lock(&ubi->volumes_lock);
804 for (i = 0; i < ubi->vtbl_slots; i++) 820 for (i = 0; i < ubi->vtbl_slots; i++)
805 paranoid_check_volume(ubi, i); 821 paranoid_check_volume(ubi, i);
806 spin_unlock(&ubi->volumes_lock);
807 mutex_unlock(&ubi->vtbl_mutex); 822 mutex_unlock(&ubi->vtbl_mutex);
808} 823}
809#endif 824#endif
diff --git a/drivers/mtd/ubi/vtbl.c b/drivers/mtd/ubi/vtbl.c
index b6fd6bbd941e..bc5df50813d6 100644
--- a/drivers/mtd/ubi/vtbl.c
+++ b/drivers/mtd/ubi/vtbl.c
@@ -93,12 +93,9 @@ int ubi_change_vtbl_record(struct ubi_device *ubi, int idx,
93 vtbl_rec = &empty_vtbl_record; 93 vtbl_rec = &empty_vtbl_record;
94 else { 94 else {
95 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC); 95 crc = crc32(UBI_CRC32_INIT, vtbl_rec, UBI_VTBL_RECORD_SIZE_CRC);
96 vtbl_rec->crc = cpu_to_ubi32(crc); 96 vtbl_rec->crc = cpu_to_be32(crc);
97 } 97 }
98 98
99 dbg_msg("change record %d", idx);
100 ubi_dbg_dump_vtbl_record(vtbl_rec, idx);
101
102 mutex_lock(&ubi->vtbl_mutex); 99 mutex_lock(&ubi->vtbl_mutex);
103 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record)); 100 memcpy(&ubi->vtbl[idx], vtbl_rec, sizeof(struct ubi_vtbl_record));
104 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) { 101 for (i = 0; i < UBI_LAYOUT_VOLUME_EBS; i++) {
@@ -141,18 +138,18 @@ static int vtbl_check(const struct ubi_device *ubi,
141 for (i = 0; i < ubi->vtbl_slots; i++) { 138 for (i = 0; i < ubi->vtbl_slots; i++) {
142 cond_resched(); 139 cond_resched();
143 140
144 reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs); 141 reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
145 alignment = ubi32_to_cpu(vtbl[i].alignment); 142 alignment = be32_to_cpu(vtbl[i].alignment);
146 data_pad = ubi32_to_cpu(vtbl[i].data_pad); 143 data_pad = be32_to_cpu(vtbl[i].data_pad);
147 upd_marker = vtbl[i].upd_marker; 144 upd_marker = vtbl[i].upd_marker;
148 vol_type = vtbl[i].vol_type; 145 vol_type = vtbl[i].vol_type;
149 name_len = ubi16_to_cpu(vtbl[i].name_len); 146 name_len = be16_to_cpu(vtbl[i].name_len);
150 name = &vtbl[i].name[0]; 147 name = &vtbl[i].name[0];
151 148
152 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC); 149 crc = crc32(UBI_CRC32_INIT, &vtbl[i], UBI_VTBL_RECORD_SIZE_CRC);
153 if (ubi32_to_cpu(vtbl[i].crc) != crc) { 150 if (be32_to_cpu(vtbl[i].crc) != crc) {
154 ubi_err("bad CRC at record %u: %#08x, not %#08x", 151 ubi_err("bad CRC at record %u: %#08x, not %#08x",
155 i, crc, ubi32_to_cpu(vtbl[i].crc)); 152 i, crc, be32_to_cpu(vtbl[i].crc));
156 ubi_dbg_dump_vtbl_record(&vtbl[i], i); 153 ubi_dbg_dump_vtbl_record(&vtbl[i], i);
157 return 1; 154 return 1;
158 } 155 }
@@ -225,8 +222,8 @@ static int vtbl_check(const struct ubi_device *ubi,
225 /* Checks that all names are unique */ 222 /* Checks that all names are unique */
226 for (i = 0; i < ubi->vtbl_slots - 1; i++) { 223 for (i = 0; i < ubi->vtbl_slots - 1; i++) {
227 for (n = i + 1; n < ubi->vtbl_slots; n++) { 224 for (n = i + 1; n < ubi->vtbl_slots; n++) {
228 int len1 = ubi16_to_cpu(vtbl[i].name_len); 225 int len1 = be16_to_cpu(vtbl[i].name_len);
229 int len2 = ubi16_to_cpu(vtbl[n].name_len); 226 int len2 = be16_to_cpu(vtbl[n].name_len);
230 227
231 if (len1 > 0 && len1 == len2 && 228 if (len1 > 0 && len1 == len2 &&
232 !strncmp(vtbl[i].name, vtbl[n].name, len1)) { 229 !strncmp(vtbl[i].name, vtbl[n].name, len1)) {
@@ -288,13 +285,13 @@ retry:
288 } 285 }
289 286
290 vid_hdr->vol_type = UBI_VID_DYNAMIC; 287 vid_hdr->vol_type = UBI_VID_DYNAMIC;
291 vid_hdr->vol_id = cpu_to_ubi32(UBI_LAYOUT_VOL_ID); 288 vid_hdr->vol_id = cpu_to_be32(UBI_LAYOUT_VOL_ID);
292 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT; 289 vid_hdr->compat = UBI_LAYOUT_VOLUME_COMPAT;
293 vid_hdr->data_size = vid_hdr->used_ebs = 290 vid_hdr->data_size = vid_hdr->used_ebs =
294 vid_hdr->data_pad = cpu_to_ubi32(0); 291 vid_hdr->data_pad = cpu_to_be32(0);
295 vid_hdr->lnum = cpu_to_ubi32(copy); 292 vid_hdr->lnum = cpu_to_be32(copy);
296 vid_hdr->sqnum = cpu_to_ubi64(++si->max_sqnum); 293 vid_hdr->sqnum = cpu_to_be64(++si->max_sqnum);
297 vid_hdr->leb_ver = cpu_to_ubi32(old_seb ? old_seb->leb_ver + 1: 0); 294 vid_hdr->leb_ver = cpu_to_be32(old_seb ? old_seb->leb_ver + 1: 0);
298 295
299 /* The EC header is already there, write the VID header */ 296 /* The EC header is already there, write the VID header */
300 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr); 297 err = ubi_io_write_vid_hdr(ubi, new_seb->pnum, vid_hdr);
@@ -317,14 +314,15 @@ retry:
317 return err; 314 return err;
318 315
319write_error: 316write_error:
320 kfree(new_seb); 317 if (err == -EIO && ++tries <= 5) {
321 /* May be this physical eraseblock went bad, try to pick another one */ 318 /*
322 if (++tries <= 5) { 319 * Probably this physical eraseblock went bad, try to pick
323 err = ubi_scan_add_to_list(si, new_seb->pnum, new_seb->ec, 320 * another one.
324 &si->corr); 321 */
325 if (!err) 322 list_add_tail(&new_seb->u.list, &si->corr);
326 goto retry; 323 goto retry;
327 } 324 }
325 kfree(new_seb);
328out_free: 326out_free:
329 ubi_free_vid_hdr(ubi, vid_hdr); 327 ubi_free_vid_hdr(ubi, vid_hdr);
330 return err; 328 return err;
@@ -380,11 +378,12 @@ static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
380 378
381 /* Read both LEB 0 and LEB 1 into memory */ 379 /* Read both LEB 0 and LEB 1 into memory */
382 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) { 380 ubi_rb_for_each_entry(rb, seb, &sv->root, u.rb) {
383 leb[seb->lnum] = kzalloc(ubi->vtbl_size, GFP_KERNEL); 381 leb[seb->lnum] = vmalloc(ubi->vtbl_size);
384 if (!leb[seb->lnum]) { 382 if (!leb[seb->lnum]) {
385 err = -ENOMEM; 383 err = -ENOMEM;
386 goto out_free; 384 goto out_free;
387 } 385 }
386 memset(leb[seb->lnum], 0, ubi->vtbl_size);
388 387
389 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0, 388 err = ubi_io_read_data(ubi, leb[seb->lnum], seb->pnum, 0,
390 ubi->vtbl_size); 389 ubi->vtbl_size);
@@ -415,7 +414,7 @@ static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
415 } 414 }
416 415
417 /* Both LEB 1 and LEB 2 are OK and consistent */ 416 /* Both LEB 1 and LEB 2 are OK and consistent */
418 kfree(leb[1]); 417 vfree(leb[1]);
419 return leb[0]; 418 return leb[0];
420 } else { 419 } else {
421 /* LEB 0 is corrupted or does not exist */ 420 /* LEB 0 is corrupted or does not exist */
@@ -436,13 +435,13 @@ static struct ubi_vtbl_record *process_lvol(const struct ubi_device *ubi,
436 goto out_free; 435 goto out_free;
437 ubi_msg("volume table was restored"); 436 ubi_msg("volume table was restored");
438 437
439 kfree(leb[0]); 438 vfree(leb[0]);
440 return leb[1]; 439 return leb[1];
441 } 440 }
442 441
443out_free: 442out_free:
444 kfree(leb[0]); 443 vfree(leb[0]);
445 kfree(leb[1]); 444 vfree(leb[1]);
446 return ERR_PTR(err); 445 return ERR_PTR(err);
447} 446}
448 447
@@ -460,9 +459,10 @@ static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi,
460 int i; 459 int i;
461 struct ubi_vtbl_record *vtbl; 460 struct ubi_vtbl_record *vtbl;
462 461
463 vtbl = kzalloc(ubi->vtbl_size, GFP_KERNEL); 462 vtbl = vmalloc(ubi->vtbl_size);
464 if (!vtbl) 463 if (!vtbl)
465 return ERR_PTR(-ENOMEM); 464 return ERR_PTR(-ENOMEM);
465 memset(vtbl, 0, ubi->vtbl_size);
466 466
467 for (i = 0; i < ubi->vtbl_slots; i++) 467 for (i = 0; i < ubi->vtbl_slots; i++)
468 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE); 468 memcpy(&vtbl[i], &empty_vtbl_record, UBI_VTBL_RECORD_SIZE);
@@ -472,7 +472,7 @@ static struct ubi_vtbl_record *create_empty_lvol(const struct ubi_device *ubi,
472 472
473 err = create_vtbl(ubi, si, i, vtbl); 473 err = create_vtbl(ubi, si, i, vtbl);
474 if (err) { 474 if (err) {
475 kfree(vtbl); 475 vfree(vtbl);
476 return ERR_PTR(err); 476 return ERR_PTR(err);
477 } 477 }
478 } 478 }
@@ -500,19 +500,19 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
500 for (i = 0; i < ubi->vtbl_slots; i++) { 500 for (i = 0; i < ubi->vtbl_slots; i++) {
501 cond_resched(); 501 cond_resched();
502 502
503 if (ubi32_to_cpu(vtbl[i].reserved_pebs) == 0) 503 if (be32_to_cpu(vtbl[i].reserved_pebs) == 0)
504 continue; /* Empty record */ 504 continue; /* Empty record */
505 505
506 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL); 506 vol = kzalloc(sizeof(struct ubi_volume), GFP_KERNEL);
507 if (!vol) 507 if (!vol)
508 return -ENOMEM; 508 return -ENOMEM;
509 509
510 vol->reserved_pebs = ubi32_to_cpu(vtbl[i].reserved_pebs); 510 vol->reserved_pebs = be32_to_cpu(vtbl[i].reserved_pebs);
511 vol->alignment = ubi32_to_cpu(vtbl[i].alignment); 511 vol->alignment = be32_to_cpu(vtbl[i].alignment);
512 vol->data_pad = ubi32_to_cpu(vtbl[i].data_pad); 512 vol->data_pad = be32_to_cpu(vtbl[i].data_pad);
513 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ? 513 vol->vol_type = vtbl[i].vol_type == UBI_VID_DYNAMIC ?
514 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME; 514 UBI_DYNAMIC_VOLUME : UBI_STATIC_VOLUME;
515 vol->name_len = ubi16_to_cpu(vtbl[i].name_len); 515 vol->name_len = be16_to_cpu(vtbl[i].name_len);
516 vol->usable_leb_size = ubi->leb_size - vol->data_pad; 516 vol->usable_leb_size = ubi->leb_size - vol->data_pad;
517 memcpy(vol->name, vtbl[i].name, vol->name_len); 517 memcpy(vol->name, vtbl[i].name, vol->name_len);
518 vol->name[vol->name_len] = '\0'; 518 vol->name[vol->name_len] = '\0';
@@ -531,7 +531,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
531 if (vol->vol_type == UBI_DYNAMIC_VOLUME) { 531 if (vol->vol_type == UBI_DYNAMIC_VOLUME) {
532 vol->used_ebs = vol->reserved_pebs; 532 vol->used_ebs = vol->reserved_pebs;
533 vol->last_eb_bytes = vol->usable_leb_size; 533 vol->last_eb_bytes = vol->usable_leb_size;
534 vol->used_bytes = vol->used_ebs * vol->usable_leb_size; 534 vol->used_bytes =
535 (long long)vol->used_ebs * vol->usable_leb_size;
535 continue; 536 continue;
536 } 537 }
537 538
@@ -561,7 +562,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
561 } 562 }
562 563
563 vol->used_ebs = sv->used_ebs; 564 vol->used_ebs = sv->used_ebs;
564 vol->used_bytes = (vol->used_ebs - 1) * vol->usable_leb_size; 565 vol->used_bytes =
566 (long long)(vol->used_ebs - 1) * vol->usable_leb_size;
565 vol->used_bytes += sv->last_data_size; 567 vol->used_bytes += sv->last_data_size;
566 vol->last_eb_bytes = sv->last_data_size; 568 vol->last_eb_bytes = sv->last_data_size;
567 } 569 }
@@ -578,7 +580,8 @@ static int init_volumes(struct ubi_device *ubi, const struct ubi_scan_info *si,
578 vol->usable_leb_size = ubi->leb_size; 580 vol->usable_leb_size = ubi->leb_size;
579 vol->used_ebs = vol->reserved_pebs; 581 vol->used_ebs = vol->reserved_pebs;
580 vol->last_eb_bytes = vol->reserved_pebs; 582 vol->last_eb_bytes = vol->reserved_pebs;
581 vol->used_bytes = vol->used_ebs * (ubi->leb_size - vol->data_pad); 583 vol->used_bytes =
584 (long long)vol->used_ebs * (ubi->leb_size - vol->data_pad);
582 vol->vol_id = UBI_LAYOUT_VOL_ID; 585 vol->vol_id = UBI_LAYOUT_VOL_ID;
583 586
584 ubi_assert(!ubi->volumes[i]); 587 ubi_assert(!ubi->volumes[i]);
@@ -718,7 +721,7 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
718 int i, err; 721 int i, err;
719 struct ubi_scan_volume *sv; 722 struct ubi_scan_volume *sv;
720 723
721 empty_vtbl_record.crc = cpu_to_ubi32(0xf116c36b); 724 empty_vtbl_record.crc = cpu_to_be32(0xf116c36b);
722 725
723 /* 726 /*
724 * The number of supported volumes is limited by the eraseblock size 727 * The number of supported volumes is limited by the eraseblock size
@@ -783,7 +786,7 @@ int ubi_read_volume_table(struct ubi_device *ubi, struct ubi_scan_info *si)
783 return 0; 786 return 0;
784 787
785out_free: 788out_free:
786 kfree(ubi->vtbl); 789 vfree(ubi->vtbl);
787 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) 790 for (i = 0; i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++)
788 if (ubi->volumes[i]) { 791 if (ubi->volumes[i]) {
789 kfree(ubi->volumes[i]); 792 kfree(ubi->volumes[i]);
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index ab2174a56bc2..9de953762097 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -667,7 +667,7 @@ static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e, int tortur
667 667
668 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec); 668 dbg_wl("erased PEB %d, new EC %llu", e->pnum, ec);
669 669
670 ec_hdr->ec = cpu_to_ubi64(ec); 670 ec_hdr->ec = cpu_to_be64(ec);
671 671
672 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr); 672 err = ubi_io_write_ec_hdr(ubi, e->pnum, ec_hdr);
673 if (err) 673 if (err)
@@ -1060,9 +1060,8 @@ out_unlock:
1060static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk, 1060static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1061 int cancel) 1061 int cancel)
1062{ 1062{
1063 int err;
1064 struct ubi_wl_entry *e = wl_wrk->e; 1063 struct ubi_wl_entry *e = wl_wrk->e;
1065 int pnum = e->pnum; 1064 int pnum = e->pnum, err, need;
1066 1065
1067 if (cancel) { 1066 if (cancel) {
1068 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec); 1067 dbg_wl("cancel erasure of PEB %d EC %d", pnum, e->ec);
@@ -1097,62 +1096,70 @@ static int erase_worker(struct ubi_device *ubi, struct ubi_work *wl_wrk,
1097 kfree(wl_wrk); 1096 kfree(wl_wrk);
1098 kmem_cache_free(wl_entries_slab, e); 1097 kmem_cache_free(wl_entries_slab, e);
1099 1098
1100 if (err != -EIO) { 1099 if (err == -EINTR || err == -ENOMEM || err == -EAGAIN ||
1100 err == -EBUSY) {
1101 int err1;
1102
1103 /* Re-schedule the LEB for erasure */
1104 err1 = schedule_erase(ubi, e, 0);
1105 if (err1) {
1106 err = err1;
1107 goto out_ro;
1108 }
1109 return err;
1110 } else if (err != -EIO) {
1101 /* 1111 /*
1102 * If this is not %-EIO, we have no idea what to do. Scheduling 1112 * If this is not %-EIO, we have no idea what to do. Scheduling
1103 * this physical eraseblock for erasure again would cause 1113 * this physical eraseblock for erasure again would cause
1104 * errors again and again. Well, lets switch to RO mode. 1114 * errors again and again. Well, lets switch to RO mode.
1105 */ 1115 */
1106 ubi_ro_mode(ubi); 1116 goto out_ro;
1107 return err;
1108 } 1117 }
1109 1118
1110 /* It is %-EIO, the PEB went bad */ 1119 /* It is %-EIO, the PEB went bad */
1111 1120
1112 if (!ubi->bad_allowed) { 1121 if (!ubi->bad_allowed) {
1113 ubi_err("bad physical eraseblock %d detected", pnum); 1122 ubi_err("bad physical eraseblock %d detected", pnum);
1114 ubi_ro_mode(ubi); 1123 goto out_ro;
1115 err = -EIO; 1124 }
1116 } else {
1117 int need;
1118
1119 spin_lock(&ubi->volumes_lock);
1120 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1121 if (need > 0) {
1122 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1123 ubi->avail_pebs -= need;
1124 ubi->rsvd_pebs += need;
1125 ubi->beb_rsvd_pebs += need;
1126 if (need > 0)
1127 ubi_msg("reserve more %d PEBs", need);
1128 }
1129 1125
1130 if (ubi->beb_rsvd_pebs == 0) { 1126 spin_lock(&ubi->volumes_lock);
1131 spin_unlock(&ubi->volumes_lock); 1127 need = ubi->beb_rsvd_level - ubi->beb_rsvd_pebs + 1;
1132 ubi_err("no reserved physical eraseblocks"); 1128 if (need > 0) {
1133 ubi_ro_mode(ubi); 1129 need = ubi->avail_pebs >= need ? need : ubi->avail_pebs;
1134 return -EIO; 1130 ubi->avail_pebs -= need;
1135 } 1131 ubi->rsvd_pebs += need;
1132 ubi->beb_rsvd_pebs += need;
1133 if (need > 0)
1134 ubi_msg("reserve more %d PEBs", need);
1135 }
1136 1136
1137 if (ubi->beb_rsvd_pebs == 0) {
1137 spin_unlock(&ubi->volumes_lock); 1138 spin_unlock(&ubi->volumes_lock);
1138 ubi_msg("mark PEB %d as bad", pnum); 1139 ubi_err("no reserved physical eraseblocks");
1140 goto out_ro;
1141 }
1139 1142
1140 err = ubi_io_mark_bad(ubi, pnum); 1143 spin_unlock(&ubi->volumes_lock);
1141 if (err) { 1144 ubi_msg("mark PEB %d as bad", pnum);
1142 ubi_ro_mode(ubi);
1143 return err;
1144 }
1145 1145
1146 spin_lock(&ubi->volumes_lock); 1146 err = ubi_io_mark_bad(ubi, pnum);
1147 ubi->beb_rsvd_pebs -= 1; 1147 if (err)
1148 ubi->bad_peb_count += 1; 1148 goto out_ro;
1149 ubi->good_peb_count -= 1; 1149
1150 ubi_calculate_reserved(ubi); 1150 spin_lock(&ubi->volumes_lock);
1151 if (ubi->beb_rsvd_pebs == 0) 1151 ubi->beb_rsvd_pebs -= 1;
1152 ubi_warn("last PEB from the reserved pool was used"); 1152 ubi->bad_peb_count += 1;
1153 spin_unlock(&ubi->volumes_lock); 1153 ubi->good_peb_count -= 1;
1154 } 1154 ubi_calculate_reserved(ubi);
1155 if (ubi->beb_rsvd_pebs == 0)
1156 ubi_warn("last PEB from the reserved pool was used");
1157 spin_unlock(&ubi->volumes_lock);
1158
1159 return err;
1155 1160
1161out_ro:
1162 ubi_ro_mode(ubi);
1156 return err; 1163 return err;
1157} 1164}
1158 1165
@@ -1634,7 +1641,7 @@ static int paranoid_check_ec(const struct ubi_device *ubi, int pnum, int ec)
1634 goto out_free; 1641 goto out_free;
1635 } 1642 }
1636 1643
1637 read_ec = ubi64_to_cpu(ec_hdr->ec); 1644 read_ec = be64_to_cpu(ec_hdr->ec);
1638 if (ec != read_ec) { 1645 if (ec != read_ec) {
1639 ubi_err("paranoid check failed for PEB %d", pnum); 1646 ubi_err("paranoid check failed for PEB %d", pnum);
1640 ubi_err("read EC is %lld, should be %d", read_ec, ec); 1647 ubi_err("read EC is %lld, should be %d", read_ec, ec);
diff --git a/include/mtd/ubi-header.h b/include/mtd/ubi-header.h
index fa479c71aa34..74efa7763479 100644
--- a/include/mtd/ubi-header.h
+++ b/include/mtd/ubi-header.h
@@ -74,42 +74,13 @@ enum {
74 UBI_COMPAT_REJECT = 5 74 UBI_COMPAT_REJECT = 5
75}; 75};
76 76
77/*
78 * ubi16_t/ubi32_t/ubi64_t - 16, 32, and 64-bit integers used in UBI on-flash
79 * data structures.
80 */
81typedef struct {
82 uint16_t int16;
83} __attribute__ ((packed)) ubi16_t;
84
85typedef struct {
86 uint32_t int32;
87} __attribute__ ((packed)) ubi32_t;
88
89typedef struct {
90 uint64_t int64;
91} __attribute__ ((packed)) ubi64_t;
92
93/*
94 * In this implementation of UBI uses the big-endian format for on-flash
95 * integers. The below are the corresponding conversion macros.
96 */
97#define cpu_to_ubi16(x) ((ubi16_t){__cpu_to_be16(x)})
98#define ubi16_to_cpu(x) ((uint16_t)__be16_to_cpu((x).int16))
99
100#define cpu_to_ubi32(x) ((ubi32_t){__cpu_to_be32(x)})
101#define ubi32_to_cpu(x) ((uint32_t)__be32_to_cpu((x).int32))
102
103#define cpu_to_ubi64(x) ((ubi64_t){__cpu_to_be64(x)})
104#define ubi64_to_cpu(x) ((uint64_t)__be64_to_cpu((x).int64))
105
106/* Sizes of UBI headers */ 77/* Sizes of UBI headers */
107#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr) 78#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
108#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr) 79#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
109 80
110/* Sizes of UBI headers without the ending CRC */ 81/* Sizes of UBI headers without the ending CRC */
111#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(ubi32_t)) 82#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
112#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(ubi32_t)) 83#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
113 84
114/** 85/**
115 * struct ubi_ec_hdr - UBI erase counter header. 86 * struct ubi_ec_hdr - UBI erase counter header.
@@ -137,14 +108,14 @@ typedef struct {
137 * eraseblocks. 108 * eraseblocks.
138 */ 109 */
139struct ubi_ec_hdr { 110struct ubi_ec_hdr {
140 ubi32_t magic; 111 __be32 magic;
141 uint8_t version; 112 __u8 version;
142 uint8_t padding1[3]; 113 __u8 padding1[3];
143 ubi64_t ec; /* Warning: the current limit is 31-bit anyway! */ 114 __be64 ec; /* Warning: the current limit is 31-bit anyway! */
144 ubi32_t vid_hdr_offset; 115 __be32 vid_hdr_offset;
145 ubi32_t data_offset; 116 __be32 data_offset;
146 uint8_t padding2[36]; 117 __u8 padding2[36];
147 ubi32_t hdr_crc; 118 __be32 hdr_crc;
148} __attribute__ ((packed)); 119} __attribute__ ((packed));
149 120
150/** 121/**
@@ -262,22 +233,22 @@ struct ubi_ec_hdr {
262 * software (say, cramfs) on top of the UBI volume. 233 * software (say, cramfs) on top of the UBI volume.
263 */ 234 */
264struct ubi_vid_hdr { 235struct ubi_vid_hdr {
265 ubi32_t magic; 236 __be32 magic;
266 uint8_t version; 237 __u8 version;
267 uint8_t vol_type; 238 __u8 vol_type;
268 uint8_t copy_flag; 239 __u8 copy_flag;
269 uint8_t compat; 240 __u8 compat;
270 ubi32_t vol_id; 241 __be32 vol_id;
271 ubi32_t lnum; 242 __be32 lnum;
272 ubi32_t leb_ver; /* obsolete, to be removed, don't use */ 243 __be32 leb_ver; /* obsolete, to be removed, don't use */
273 ubi32_t data_size; 244 __be32 data_size;
274 ubi32_t used_ebs; 245 __be32 used_ebs;
275 ubi32_t data_pad; 246 __be32 data_pad;
276 ubi32_t data_crc; 247 __be32 data_crc;
277 uint8_t padding1[4]; 248 __u8 padding1[4];
278 ubi64_t sqnum; 249 __be64 sqnum;
279 uint8_t padding2[12]; 250 __u8 padding2[12];
280 ubi32_t hdr_crc; 251 __be32 hdr_crc;
281} __attribute__ ((packed)); 252} __attribute__ ((packed));
282 253
283/* Internal UBI volumes count */ 254/* Internal UBI volumes count */
@@ -306,7 +277,7 @@ struct ubi_vid_hdr {
306#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record) 277#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
307 278
308/* Size of the volume table record without the ending CRC */ 279/* Size of the volume table record without the ending CRC */
309#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(ubi32_t)) 280#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
310 281
311/** 282/**
312 * struct ubi_vtbl_record - a record in the volume table. 283 * struct ubi_vtbl_record - a record in the volume table.
@@ -346,15 +317,15 @@ struct ubi_vid_hdr {
346 * Empty records contain all zeroes and the CRC checksum of those zeroes. 317 * Empty records contain all zeroes and the CRC checksum of those zeroes.
347 */ 318 */
348struct ubi_vtbl_record { 319struct ubi_vtbl_record {
349 ubi32_t reserved_pebs; 320 __be32 reserved_pebs;
350 ubi32_t alignment; 321 __be32 alignment;
351 ubi32_t data_pad; 322 __be32 data_pad;
352 uint8_t vol_type; 323 __u8 vol_type;
353 uint8_t upd_marker; 324 __u8 upd_marker;
354 ubi16_t name_len; 325 __be16 name_len;
355 uint8_t name[UBI_VOL_NAME_MAX+1]; 326 __u8 name[UBI_VOL_NAME_MAX+1];
356 uint8_t padding2[24]; 327 __u8 padding2[24];
357 ubi32_t crc; 328 __be32 crc;
358} __attribute__ ((packed)); 329} __attribute__ ((packed));
359 330
360#endif /* !__UBI_HEADER_H__ */ 331#endif /* !__UBI_HEADER_H__ */