aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-03-06 14:30:18 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2010-03-06 14:30:18 -0500
commit4582a30c2fdca5d2b40f63a20ea082b93230ff2b (patch)
tree59b00fb987c806cdf8164ae07ff229f156a210d8 /drivers/mtd
parent9cc0cb3c7d54f320b9eede6f4a49072ecadd864d (diff)
parent6e9065d756df5dac6dc02b94b82b4f5dbbf38caf (diff)
Merge git://git.infradead.org/ubi-2.6
* git://git.infradead.org/ubi-2.6: UBI: add write checking UBI: simplify debugging return codes UBI: fix attaching error path UBI: support attaching by MTD character device name UBI: mark few variables as __initdata
Diffstat (limited to 'drivers/mtd')
-rw-r--r--drivers/mtd/ubi/build.c133
-rw-r--r--drivers/mtd/ubi/debug.h4
-rw-r--r--drivers/mtd/ubi/io.c120
-rw-r--r--drivers/mtd/ubi/scan.c11
-rw-r--r--drivers/mtd/ubi/wl.c17
5 files changed, 198 insertions, 87 deletions
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 14cec04c34f9..bc45ef9af17d 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -37,6 +37,7 @@
37#include <linux/module.h> 37#include <linux/module.h>
38#include <linux/moduleparam.h> 38#include <linux/moduleparam.h>
39#include <linux/stringify.h> 39#include <linux/stringify.h>
40#include <linux/namei.h>
40#include <linux/stat.h> 41#include <linux/stat.h>
41#include <linux/miscdevice.h> 42#include <linux/miscdevice.h>
42#include <linux/log2.h> 43#include <linux/log2.h>
@@ -50,7 +51,8 @@
50 51
51/** 52/**
52 * struct mtd_dev_param - MTD device parameter description data structure. 53 * struct mtd_dev_param - MTD device parameter description data structure.
53 * @name: MTD device name or number string 54 * @name: MTD character device node path, MTD device name, or MTD device number
55 * string
54 * @vid_hdr_offs: VID header offset 56 * @vid_hdr_offs: VID header offset
55 */ 57 */
56struct mtd_dev_param { 58struct mtd_dev_param {
@@ -59,10 +61,10 @@ struct mtd_dev_param {
59}; 61};
60 62
61/* Numbers of elements set in the @mtd_dev_param array */ 63/* Numbers of elements set in the @mtd_dev_param array */
62static int mtd_devs; 64static int __initdata mtd_devs;
63 65
64/* MTD devices specification parameters */ 66/* MTD devices specification parameters */
65static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES]; 67static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
66 68
67/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */ 69/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
68struct class *ubi_class; 70struct class *ubi_class;
@@ -363,11 +365,13 @@ static void dev_release(struct device *dev)
363/** 365/**
364 * ubi_sysfs_init - initialize sysfs for an UBI device. 366 * ubi_sysfs_init - initialize sysfs for an UBI device.
365 * @ubi: UBI device description object 367 * @ubi: UBI device description object
368 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
369 * taken
366 * 370 *
367 * This function returns zero in case of success and a negative error code in 371 * This function returns zero in case of success and a negative error code in
368 * case of failure. 372 * case of failure.
369 */ 373 */
370static int ubi_sysfs_init(struct ubi_device *ubi) 374static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
371{ 375{
372 int err; 376 int err;
373 377
@@ -379,6 +383,7 @@ static int ubi_sysfs_init(struct ubi_device *ubi)
379 if (err) 383 if (err)
380 return err; 384 return err;
381 385
386 *ref = 1;
382 err = device_create_file(&ubi->dev, &dev_eraseblock_size); 387 err = device_create_file(&ubi->dev, &dev_eraseblock_size);
383 if (err) 388 if (err)
384 return err; 389 return err;
@@ -434,7 +439,7 @@ static void ubi_sysfs_close(struct ubi_device *ubi)
434} 439}
435 440
436/** 441/**
437 * kill_volumes - destroy all volumes. 442 * kill_volumes - destroy all user volumes.
438 * @ubi: UBI device description object 443 * @ubi: UBI device description object
439 */ 444 */
440static void kill_volumes(struct ubi_device *ubi) 445static void kill_volumes(struct ubi_device *ubi)
@@ -447,36 +452,29 @@ static void kill_volumes(struct ubi_device *ubi)
447} 452}
448 453
449/** 454/**
450 * free_user_volumes - free all user volumes.
451 * @ubi: UBI device description object
452 *
453 * Normally the volumes are freed at the release function of the volume device
454 * objects. However, on error paths the volumes have to be freed before the
455 * device objects have been initialized.
456 */
457static void free_user_volumes(struct ubi_device *ubi)
458{
459 int i;
460
461 for (i = 0; i < ubi->vtbl_slots; i++)
462 if (ubi->volumes[i]) {
463 kfree(ubi->volumes[i]->eba_tbl);
464 kfree(ubi->volumes[i]);
465 }
466}
467
468/**
469 * uif_init - initialize user interfaces for an UBI device. 455 * uif_init - initialize user interfaces for an UBI device.
470 * @ubi: UBI device description object 456 * @ubi: UBI device description object
457 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
458 * taken, otherwise set to %0
459 *
460 * This function initializes various user interfaces for an UBI device. If the
461 * initialization fails at an early stage, this function frees all the
462 * resources it allocated, returns an error, and @ref is set to %0. However,
463 * if the initialization fails after the UBI device was registered in the
464 * driver core subsystem, this function takes a reference to @ubi->dev, because
465 * otherwise the release function ('dev_release()') would free whole @ubi
466 * object. The @ref argument is set to %1 in this case. The caller has to put
467 * this reference.
471 * 468 *
472 * This function returns zero in case of success and a negative error code in 469 * This function returns zero in case of success and a negative error code in
473 * case of failure. Note, this function destroys all volumes if it fails. 470 * case of failure.
474 */ 471 */
475static int uif_init(struct ubi_device *ubi) 472static int uif_init(struct ubi_device *ubi, int *ref)
476{ 473{
477 int i, err; 474 int i, err;
478 dev_t dev; 475 dev_t dev;
479 476
477 *ref = 0;
480 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num); 478 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
481 479
482 /* 480 /*
@@ -504,7 +502,7 @@ static int uif_init(struct ubi_device *ubi)
504 goto out_unreg; 502 goto out_unreg;
505 } 503 }
506 504
507 err = ubi_sysfs_init(ubi); 505 err = ubi_sysfs_init(ubi, ref);
508 if (err) 506 if (err)
509 goto out_sysfs; 507 goto out_sysfs;
510 508
@@ -522,6 +520,8 @@ static int uif_init(struct ubi_device *ubi)
522out_volumes: 520out_volumes:
523 kill_volumes(ubi); 521 kill_volumes(ubi);
524out_sysfs: 522out_sysfs:
523 if (*ref)
524 get_device(&ubi->dev);
525 ubi_sysfs_close(ubi); 525 ubi_sysfs_close(ubi);
526 cdev_del(&ubi->cdev); 526 cdev_del(&ubi->cdev);
527out_unreg: 527out_unreg:
@@ -875,7 +875,7 @@ static int ubi_reboot_notifier(struct notifier_block *n, unsigned long state,
875int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset) 875int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
876{ 876{
877 struct ubi_device *ubi; 877 struct ubi_device *ubi;
878 int i, err, do_free = 1; 878 int i, err, ref = 0;
879 879
880 /* 880 /*
881 * Check if we already have the same MTD device attached. 881 * Check if we already have the same MTD device attached.
@@ -975,9 +975,9 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
975 goto out_detach; 975 goto out_detach;
976 } 976 }
977 977
978 err = uif_init(ubi); 978 err = uif_init(ubi, &ref);
979 if (err) 979 if (err)
980 goto out_nofree; 980 goto out_detach;
981 981
982 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name); 982 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
983 if (IS_ERR(ubi->bgt_thread)) { 983 if (IS_ERR(ubi->bgt_thread)) {
@@ -1025,12 +1025,8 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
1025 1025
1026out_uif: 1026out_uif:
1027 uif_close(ubi); 1027 uif_close(ubi);
1028out_nofree:
1029 do_free = 0;
1030out_detach: 1028out_detach:
1031 ubi_wl_close(ubi); 1029 ubi_wl_close(ubi);
1032 if (do_free)
1033 free_user_volumes(ubi);
1034 free_internal_volumes(ubi); 1030 free_internal_volumes(ubi);
1035 vfree(ubi->vtbl); 1031 vfree(ubi->vtbl);
1036out_free: 1032out_free:
@@ -1039,7 +1035,10 @@ out_free:
1039#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1035#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1040 vfree(ubi->dbg_peb_buf); 1036 vfree(ubi->dbg_peb_buf);
1041#endif 1037#endif
1042 kfree(ubi); 1038 if (ref)
1039 put_device(&ubi->dev);
1040 else
1041 kfree(ubi);
1043 return err; 1042 return err;
1044} 1043}
1045 1044
@@ -1096,7 +1095,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1096 1095
1097 /* 1096 /*
1098 * Get a reference to the device in order to prevent 'dev_release()' 1097 * Get a reference to the device in order to prevent 'dev_release()'
1099 * from freeing @ubi object. 1098 * from freeing the @ubi object.
1100 */ 1099 */
1101 get_device(&ubi->dev); 1100 get_device(&ubi->dev);
1102 1101
@@ -1116,13 +1115,50 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1116} 1115}
1117 1116
1118/** 1117/**
1119 * find_mtd_device - open an MTD device by its name or number. 1118 * open_mtd_by_chdev - open an MTD device by its character device node path.
1120 * @mtd_dev: name or number of the device 1119 * @mtd_dev: MTD character device node path
1120 *
1121 * This helper function opens an MTD device by its character node device path.
1122 * Returns MTD device description object in case of success and a negative
1123 * error code in case of failure.
1124 */
1125static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1126{
1127 int err, major, minor, mode;
1128 struct path path;
1129
1130 /* Probably this is an MTD character device node path */
1131 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1132 if (err)
1133 return ERR_PTR(err);
1134
1135 /* MTD device number is defined by the major / minor numbers */
1136 major = imajor(path.dentry->d_inode);
1137 minor = iminor(path.dentry->d_inode);
1138 mode = path.dentry->d_inode->i_mode;
1139 path_put(&path);
1140 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1141 return ERR_PTR(-EINVAL);
1142
1143 if (minor & 1)
1144 /*
1145 * Just do not think the "/dev/mtdrX" devices support is need,
1146 * so do not support them to avoid doing extra work.
1147 */
1148 return ERR_PTR(-EINVAL);
1149
1150 return get_mtd_device(NULL, minor / 2);
1151}
1152
1153/**
1154 * open_mtd_device - open MTD device by name, character device path, or number.
1155 * @mtd_dev: name, character device node path, or MTD device device number
1121 * 1156 *
1122 * This function tries to open and MTD device described by @mtd_dev string, 1157 * This function tries to open and MTD device described by @mtd_dev string,
1123 * which is first treated as an ASCII number, and if it is not true, it is 1158 * which is first treated as ASCII MTD device number, and if it is not true, it
1124 * treated as MTD device name. Returns MTD device description object in case of 1159 * is treated as MTD device name, and if that is also not true, it is treated
1125 * success and a negative error code in case of failure. 1160 * as MTD character device node path. Returns MTD device description object in
1161 * case of success and a negative error code in case of failure.
1126 */ 1162 */
1127static struct mtd_info * __init open_mtd_device(const char *mtd_dev) 1163static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1128{ 1164{
@@ -1137,6 +1173,9 @@ static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1137 * MTD device name. 1173 * MTD device name.
1138 */ 1174 */
1139 mtd = get_mtd_device_nm(mtd_dev); 1175 mtd = get_mtd_device_nm(mtd_dev);
1176 if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1177 /* Probably this is an MTD character device node path */
1178 mtd = open_mtd_by_chdev(mtd_dev);
1140 } else 1179 } else
1141 mtd = get_mtd_device(NULL, mtd_num); 1180 mtd = get_mtd_device(NULL, mtd_num);
1142 1181
@@ -1352,13 +1391,15 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1352 1391
1353module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000); 1392module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
1354MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: " 1393MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
1355 "mtd=<name|num>[,<vid_hdr_offs>].\n" 1394 "mtd=<name|num|path>[,<vid_hdr_offs>].\n"
1356 "Multiple \"mtd\" parameters may be specified.\n" 1395 "Multiple \"mtd\" parameters may be specified.\n"
1357 "MTD devices may be specified by their number or name.\n" 1396 "MTD devices may be specified by their number, name, or "
1397 "path to the MTD character device node.\n"
1358 "Optional \"vid_hdr_offs\" parameter specifies UBI VID " 1398 "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
1359 "header position and data starting position to be used " 1399 "header position to be used by UBI.\n"
1360 "by UBI.\n" 1400 "Example 1: mtd=/dev/mtd0 - attach MTD device "
1361 "Example: mtd=content,1984 mtd=4 - attach MTD device" 1401 "/dev/mtd0.\n"
1402 "Example 2: mtd=content,1984 mtd=4 - attach MTD device "
1362 "with name \"content\" using VID header offset 1984, and " 1403 "with name \"content\" using VID header offset 1984, and "
1363 "MTD device number 4 with default VID header offset."); 1404 "MTD device number 4 with default VID header offset.");
1364 1405
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index f30bcb372c05..17a107129726 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -96,8 +96,11 @@ void ubi_dbg_dump_flash(struct ubi_device *ubi, int pnum, int offset, int len);
96 96
97#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 97#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
98int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len); 98int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len);
99int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
100 int offset, int len);
99#else 101#else
100#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0 102#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0
103#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0
101#endif 104#endif
102 105
103#ifdef CONFIG_MTD_UBI_DEBUG_DISABLE_BGT 106#ifdef CONFIG_MTD_UBI_DEBUG_DISABLE_BGT
@@ -176,6 +179,7 @@ static inline int ubi_dbg_is_erase_failure(void)
176#define ubi_dbg_is_write_failure() 0 179#define ubi_dbg_is_write_failure() 0
177#define ubi_dbg_is_erase_failure() 0 180#define ubi_dbg_is_erase_failure() 0
178#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0 181#define ubi_dbg_check_all_ff(ubi, pnum, offset, len) 0
182#define ubi_dbg_check_write(ubi, buf, pnum, offset, len) 0
179 183
180#endif /* !CONFIG_MTD_UBI_DEBUG */ 184#endif /* !CONFIG_MTD_UBI_DEBUG */
181#endif /* !__UBI_DEBUG_H__ */ 185#endif /* !__UBI_DEBUG_H__ */
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index 8aa51e7a6a7d..b4ecc84c7549 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -143,7 +143,7 @@ int ubi_io_read(const struct ubi_device *ubi, void *buf, int pnum, int offset,
143 143
144 err = paranoid_check_not_bad(ubi, pnum); 144 err = paranoid_check_not_bad(ubi, pnum);
145 if (err) 145 if (err)
146 return err > 0 ? -EINVAL : err; 146 return err;
147 147
148 addr = (loff_t)pnum * ubi->peb_size + offset; 148 addr = (loff_t)pnum * ubi->peb_size + offset;
149retry: 149retry:
@@ -236,12 +236,12 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
236 236
237 err = paranoid_check_not_bad(ubi, pnum); 237 err = paranoid_check_not_bad(ubi, pnum);
238 if (err) 238 if (err)
239 return err > 0 ? -EINVAL : err; 239 return err;
240 240
241 /* The area we are writing to has to contain all 0xFF bytes */ 241 /* The area we are writing to has to contain all 0xFF bytes */
242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len); 242 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
243 if (err) 243 if (err)
244 return err > 0 ? -EINVAL : err; 244 return err;
245 245
246 if (offset >= ubi->leb_start) { 246 if (offset >= ubi->leb_start) {
247 /* 247 /*
@@ -250,10 +250,10 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
250 */ 250 */
251 err = paranoid_check_peb_ec_hdr(ubi, pnum); 251 err = paranoid_check_peb_ec_hdr(ubi, pnum);
252 if (err) 252 if (err)
253 return err > 0 ? -EINVAL : err; 253 return err;
254 err = paranoid_check_peb_vid_hdr(ubi, pnum); 254 err = paranoid_check_peb_vid_hdr(ubi, pnum);
255 if (err) 255 if (err)
256 return err > 0 ? -EINVAL : err; 256 return err;
257 } 257 }
258 258
259 if (ubi_dbg_is_write_failure()) { 259 if (ubi_dbg_is_write_failure()) {
@@ -273,6 +273,21 @@ int ubi_io_write(struct ubi_device *ubi, const void *buf, int pnum, int offset,
273 } else 273 } else
274 ubi_assert(written == len); 274 ubi_assert(written == len);
275 275
276 if (!err) {
277 err = ubi_dbg_check_write(ubi, buf, pnum, offset, len);
278 if (err)
279 return err;
280
281 /*
282 * Since we always write sequentially, the rest of the PEB has
283 * to contain only 0xFF bytes.
284 */
285 offset += len;
286 len = ubi->peb_size - offset;
287 if (len)
288 err = ubi_dbg_check_all_ff(ubi, pnum, offset, len);
289 }
290
276 return err; 291 return err;
277} 292}
278 293
@@ -348,7 +363,7 @@ retry:
348 363
349 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size); 364 err = ubi_dbg_check_all_ff(ubi, pnum, 0, ubi->peb_size);
350 if (err) 365 if (err)
351 return err > 0 ? -EINVAL : err; 366 return err;
352 367
353 if (ubi_dbg_is_erase_failure() && !err) { 368 if (ubi_dbg_is_erase_failure() && !err) {
354 dbg_err("cannot erase PEB %d (emulated)", pnum); 369 dbg_err("cannot erase PEB %d (emulated)", pnum);
@@ -542,7 +557,7 @@ int ubi_io_sync_erase(struct ubi_device *ubi, int pnum, int torture)
542 557
543 err = paranoid_check_not_bad(ubi, pnum); 558 err = paranoid_check_not_bad(ubi, pnum);
544 if (err != 0) 559 if (err != 0)
545 return err > 0 ? -EINVAL : err; 560 return err;
546 561
547 if (ubi->ro_mode) { 562 if (ubi->ro_mode) {
548 ubi_err("read-only mode"); 563 ubi_err("read-only mode");
@@ -819,7 +834,7 @@ int ubi_io_write_ec_hdr(struct ubi_device *ubi, int pnum,
819 834
820 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr); 835 err = paranoid_check_ec_hdr(ubi, pnum, ec_hdr);
821 if (err) 836 if (err)
822 return -EINVAL; 837 return err;
823 838
824 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize); 839 err = ubi_io_write(ubi, ec_hdr, pnum, 0, ubi->ec_hdr_alsize);
825 return err; 840 return err;
@@ -1083,7 +1098,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1083 1098
1084 err = paranoid_check_peb_ec_hdr(ubi, pnum); 1099 err = paranoid_check_peb_ec_hdr(ubi, pnum);
1085 if (err) 1100 if (err)
1086 return err > 0 ? -EINVAL : err; 1101 return err;
1087 1102
1088 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC); 1103 vid_hdr->magic = cpu_to_be32(UBI_VID_HDR_MAGIC);
1089 vid_hdr->version = UBI_VERSION; 1104 vid_hdr->version = UBI_VERSION;
@@ -1092,7 +1107,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1092 1107
1093 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr); 1108 err = paranoid_check_vid_hdr(ubi, pnum, vid_hdr);
1094 if (err) 1109 if (err)
1095 return -EINVAL; 1110 return err;
1096 1111
1097 p = (char *)vid_hdr - ubi->vid_hdr_shift; 1112 p = (char *)vid_hdr - ubi->vid_hdr_shift;
1098 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset, 1113 err = ubi_io_write(ubi, p, pnum, ubi->vid_hdr_aloffset,
@@ -1107,8 +1122,8 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
1107 * @ubi: UBI device description object 1122 * @ubi: UBI device description object
1108 * @pnum: physical eraseblock number to check 1123 * @pnum: physical eraseblock number to check
1109 * 1124 *
1110 * This function returns zero if the physical eraseblock is good, a positive 1125 * This function returns zero if the physical eraseblock is good, %-EINVAL if
1111 * number if it is bad and a negative error code if an error occurred. 1126 * it is bad and a negative error code if an error occurred.
1112 */ 1127 */
1113static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum) 1128static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1114{ 1129{
@@ -1120,7 +1135,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1120 1135
1121 ubi_err("paranoid check failed for PEB %d", pnum); 1136 ubi_err("paranoid check failed for PEB %d", pnum);
1122 ubi_dbg_dump_stack(); 1137 ubi_dbg_dump_stack();
1123 return err; 1138 return err > 0 ? -EINVAL : err;
1124} 1139}
1125 1140
1126/** 1141/**
@@ -1130,7 +1145,7 @@ static int paranoid_check_not_bad(const struct ubi_device *ubi, int pnum)
1130 * @ec_hdr: the erase counter header to check 1145 * @ec_hdr: the erase counter header to check
1131 * 1146 *
1132 * This function returns zero if the erase counter header contains valid 1147 * This function returns zero if the erase counter header contains valid
1133 * values, and %1 if not. 1148 * values, and %-EINVAL if not.
1134 */ 1149 */
1135static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum, 1150static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1136 const struct ubi_ec_hdr *ec_hdr) 1151 const struct ubi_ec_hdr *ec_hdr)
@@ -1156,7 +1171,7 @@ static int paranoid_check_ec_hdr(const struct ubi_device *ubi, int pnum,
1156fail: 1171fail:
1157 ubi_dbg_dump_ec_hdr(ec_hdr); 1172 ubi_dbg_dump_ec_hdr(ec_hdr);
1158 ubi_dbg_dump_stack(); 1173 ubi_dbg_dump_stack();
1159 return 1; 1174 return -EINVAL;
1160} 1175}
1161 1176
1162/** 1177/**
@@ -1164,8 +1179,8 @@ fail:
1164 * @ubi: UBI device description object 1179 * @ubi: UBI device description object
1165 * @pnum: the physical eraseblock number to check 1180 * @pnum: the physical eraseblock number to check
1166 * 1181 *
1167 * This function returns zero if the erase counter header is all right, %1 if 1182 * This function returns zero if the erase counter header is all right and and
1168 * not, and a negative error code if an error occurred. 1183 * a negative error code if not or if an error occurred.
1169 */ 1184 */
1170static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum) 1185static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1171{ 1186{
@@ -1188,7 +1203,7 @@ static int paranoid_check_peb_ec_hdr(const struct ubi_device *ubi, int pnum)
1188 ubi_err("paranoid check failed for PEB %d", pnum); 1203 ubi_err("paranoid check failed for PEB %d", pnum);
1189 ubi_dbg_dump_ec_hdr(ec_hdr); 1204 ubi_dbg_dump_ec_hdr(ec_hdr);
1190 ubi_dbg_dump_stack(); 1205 ubi_dbg_dump_stack();
1191 err = 1; 1206 err = -EINVAL;
1192 goto exit; 1207 goto exit;
1193 } 1208 }
1194 1209
@@ -1206,7 +1221,7 @@ exit:
1206 * @vid_hdr: the volume identifier header to check 1221 * @vid_hdr: the volume identifier header to check
1207 * 1222 *
1208 * This function returns zero if the volume identifier header is all right, and 1223 * This function returns zero if the volume identifier header is all right, and
1209 * %1 if not. 1224 * %-EINVAL if not.
1210 */ 1225 */
1211static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum, 1226static int paranoid_check_vid_hdr(const struct ubi_device *ubi, int pnum,
1212 const struct ubi_vid_hdr *vid_hdr) 1227 const struct ubi_vid_hdr *vid_hdr)
@@ -1233,7 +1248,7 @@ fail:
1233 ubi_err("paranoid check failed for PEB %d", pnum); 1248 ubi_err("paranoid check failed for PEB %d", pnum);
1234 ubi_dbg_dump_vid_hdr(vid_hdr); 1249 ubi_dbg_dump_vid_hdr(vid_hdr);
1235 ubi_dbg_dump_stack(); 1250 ubi_dbg_dump_stack();
1236 return 1; 1251 return -EINVAL;
1237 1252
1238} 1253}
1239 1254
@@ -1243,7 +1258,7 @@ fail:
1243 * @pnum: the physical eraseblock number to check 1258 * @pnum: the physical eraseblock number to check
1244 * 1259 *
1245 * This function returns zero if the volume identifier header is all right, 1260 * This function returns zero if the volume identifier header is all right,
1246 * %1 if not, and a negative error code if an error occurred. 1261 * and a negative error code if not or if an error occurred.
1247 */ 1262 */
1248static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum) 1263static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1249{ 1264{
@@ -1270,7 +1285,7 @@ static int paranoid_check_peb_vid_hdr(const struct ubi_device *ubi, int pnum)
1270 ubi_err("paranoid check failed for PEB %d", pnum); 1285 ubi_err("paranoid check failed for PEB %d", pnum);
1271 ubi_dbg_dump_vid_hdr(vid_hdr); 1286 ubi_dbg_dump_vid_hdr(vid_hdr);
1272 ubi_dbg_dump_stack(); 1287 ubi_dbg_dump_stack();
1273 err = 1; 1288 err = -EINVAL;
1274 goto exit; 1289 goto exit;
1275 } 1290 }
1276 1291
@@ -1282,6 +1297,61 @@ exit:
1282} 1297}
1283 1298
1284/** 1299/**
1300 * ubi_dbg_check_write - make sure write succeeded.
1301 * @ubi: UBI device description object
1302 * @buf: buffer with data which were written
1303 * @pnum: physical eraseblock number the data were written to
1304 * @offset: offset within the physical eraseblock the data were written to
1305 * @len: how many bytes were written
1306 *
1307 * This functions reads data which were recently written and compares it with
1308 * the original data buffer - the data have to match. Returns zero if the data
1309 * match and a negative error code if not or in case of failure.
1310 */
1311int ubi_dbg_check_write(struct ubi_device *ubi, const void *buf, int pnum,
1312 int offset, int len)
1313{
1314 int err, i;
1315
1316 mutex_lock(&ubi->dbg_buf_mutex);
1317 err = ubi_io_read(ubi, ubi->dbg_peb_buf, pnum, offset, len);
1318 if (err)
1319 goto out_unlock;
1320
1321 for (i = 0; i < len; i++) {
1322 uint8_t c = ((uint8_t *)buf)[i];
1323 uint8_t c1 = ((uint8_t *)ubi->dbg_peb_buf)[i];
1324 int dump_len;
1325
1326 if (c == c1)
1327 continue;
1328
1329 ubi_err("paranoid check failed for PEB %d:%d, len %d",
1330 pnum, offset, len);
1331 ubi_msg("data differ at position %d", i);
1332 dump_len = max_t(int, 128, len - i);
1333 ubi_msg("hex dump of the original buffer from %d to %d",
1334 i, i + dump_len);
1335 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1336 buf + i, dump_len, 1);
1337 ubi_msg("hex dump of the read buffer from %d to %d",
1338 i, i + dump_len);
1339 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1340 ubi->dbg_peb_buf + i, dump_len, 1);
1341 ubi_dbg_dump_stack();
1342 err = -EINVAL;
1343 goto out_unlock;
1344 }
1345 mutex_unlock(&ubi->dbg_buf_mutex);
1346
1347 return 0;
1348
1349out_unlock:
1350 mutex_unlock(&ubi->dbg_buf_mutex);
1351 return err;
1352}
1353
1354/**
1285 * ubi_dbg_check_all_ff - check that a region of flash is empty. 1355 * ubi_dbg_check_all_ff - check that a region of flash is empty.
1286 * @ubi: UBI device description object 1356 * @ubi: UBI device description object
1287 * @pnum: the physical eraseblock number to check 1357 * @pnum: the physical eraseblock number to check
@@ -1289,8 +1359,8 @@ exit:
1289 * @len: the length of the region to check 1359 * @len: the length of the region to check
1290 * 1360 *
1291 * This function returns zero if only 0xFF bytes are present at offset 1361 * This function returns zero if only 0xFF bytes are present at offset
1292 * @offset of the physical eraseblock @pnum, %1 if not, and a negative error 1362 * @offset of the physical eraseblock @pnum, and a negative error code if not
1293 * code if an error occurred. 1363 * or if an error occurred.
1294 */ 1364 */
1295int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len) 1365int ubi_dbg_check_all_ff(struct ubi_device *ubi, int pnum, int offset, int len)
1296{ 1366{
@@ -1321,7 +1391,7 @@ fail:
1321 ubi_msg("hex dump of the %d-%d region", offset, offset + len); 1391 ubi_msg("hex dump of the %d-%d region", offset, offset + len);
1322 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1, 1392 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET, 32, 1,
1323 ubi->dbg_peb_buf, len, 1); 1393 ubi->dbg_peb_buf, len, 1);
1324 err = 1; 1394 err = -EINVAL;
1325error: 1395error:
1326 ubi_dbg_dump_stack(); 1396 ubi_dbg_dump_stack();
1327 mutex_unlock(&ubi->dbg_buf_mutex); 1397 mutex_unlock(&ubi->dbg_buf_mutex);
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 90af61a2c3e4..594184bbd56a 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -974,11 +974,8 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
974 seb->ec = si->mean_ec; 974 seb->ec = si->mean_ec;
975 975
976 err = paranoid_check_si(ubi, si); 976 err = paranoid_check_si(ubi, si);
977 if (err) { 977 if (err)
978 if (err > 0)
979 err = -EINVAL;
980 goto out_vidh; 978 goto out_vidh;
981 }
982 979
983 ubi_free_vid_hdr(ubi, vidh); 980 ubi_free_vid_hdr(ubi, vidh);
984 kfree(ech); 981 kfree(ech);
@@ -1086,8 +1083,8 @@ void ubi_scan_destroy_si(struct ubi_scan_info *si)
1086 * @ubi: UBI device description object 1083 * @ubi: UBI device description object
1087 * @si: scanning information 1084 * @si: scanning information
1088 * 1085 *
1089 * This function returns zero if the scanning information is all right, %1 if 1086 * This function returns zero if the scanning information is all right, and a
1090 * not and a negative error code if an error occurred. 1087 * negative error code if not or if an error occurred.
1091 */ 1088 */
1092static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si) 1089static int paranoid_check_si(struct ubi_device *ubi, struct ubi_scan_info *si)
1093{ 1090{
@@ -1346,7 +1343,7 @@ bad_vid_hdr:
1346 1343
1347out: 1344out:
1348 ubi_dbg_dump_stack(); 1345 ubi_dbg_dump_stack();
1349 return 1; 1346 return -EINVAL;
1350} 1347}
1351 1348
1352#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1349#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */
diff --git a/drivers/mtd/ubi/wl.c b/drivers/mtd/ubi/wl.c
index 600c7229d5cf..f64ddabd4ac8 100644
--- a/drivers/mtd/ubi/wl.c
+++ b/drivers/mtd/ubi/wl.c
@@ -464,7 +464,7 @@ retry:
464 ubi->peb_size - ubi->vid_hdr_aloffset); 464 ubi->peb_size - ubi->vid_hdr_aloffset);
465 if (err) { 465 if (err) {
466 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum); 466 ubi_err("new PEB %d does not contain all 0xFF bytes", e->pnum);
467 return err > 0 ? -EINVAL : err; 467 return err;
468 } 468 }
469 469
470 return e->pnum; 470 return e->pnum;
@@ -513,7 +513,7 @@ static int sync_erase(struct ubi_device *ubi, struct ubi_wl_entry *e,
513 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec); 513 dbg_wl("erase PEB %d, old EC %llu", e->pnum, ec);
514 514
515 err = paranoid_check_ec(ubi, e->pnum, e->ec); 515 err = paranoid_check_ec(ubi, e->pnum, e->ec);
516 if (err > 0) 516 if (err)
517 return -EINVAL; 517 return -EINVAL;
518 518
519 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS); 519 ec_hdr = kzalloc(ubi->ec_hdr_alsize, GFP_NOFS);
@@ -1572,8 +1572,7 @@ void ubi_wl_close(struct ubi_device *ubi)
1572 * @ec: the erase counter to check 1572 * @ec: the erase counter to check
1573 * 1573 *
1574 * This function returns zero if the erase counter of physical eraseblock @pnum 1574 * This function returns zero if the erase counter of physical eraseblock @pnum
1575 * is equivalent to @ec, %1 if not, and a negative error code if an error 1575 * is equivalent to @ec, and a negative error code if not or if an error occurred.
1576 * occurred.
1577 */ 1576 */
1578static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec) 1577static int paranoid_check_ec(struct ubi_device *ubi, int pnum, int ec)
1579{ 1578{
@@ -1611,8 +1610,8 @@ out_free:
1611 * @e: the wear-leveling entry to check 1610 * @e: the wear-leveling entry to check
1612 * @root: the root of the tree 1611 * @root: the root of the tree
1613 * 1612 *
1614 * This function returns zero if @e is in the @root RB-tree and %1 if it is 1613 * This function returns zero if @e is in the @root RB-tree and %-EINVAL if it
1615 * not. 1614 * is not.
1616 */ 1615 */
1617static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e, 1616static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1618 struct rb_root *root) 1617 struct rb_root *root)
@@ -1623,7 +1622,7 @@ static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1623 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ", 1622 ubi_err("paranoid check failed for PEB %d, EC %d, RB-tree %p ",
1624 e->pnum, e->ec, root); 1623 e->pnum, e->ec, root);
1625 ubi_dbg_dump_stack(); 1624 ubi_dbg_dump_stack();
1626 return 1; 1625 return -EINVAL;
1627} 1626}
1628 1627
1629/** 1628/**
@@ -1632,7 +1631,7 @@ static int paranoid_check_in_wl_tree(struct ubi_wl_entry *e,
1632 * @ubi: UBI device description object 1631 * @ubi: UBI device description object
1633 * @e: the wear-leveling entry to check 1632 * @e: the wear-leveling entry to check
1634 * 1633 *
1635 * This function returns zero if @e is in @ubi->pq and %1 if it is not. 1634 * This function returns zero if @e is in @ubi->pq and %-EINVAL if it is not.
1636 */ 1635 */
1637static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e) 1636static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
1638{ 1637{
@@ -1647,6 +1646,6 @@ static int paranoid_check_in_pq(struct ubi_device *ubi, struct ubi_wl_entry *e)
1647 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue", 1646 ubi_err("paranoid check failed for PEB %d, EC %d, Protect queue",
1648 e->pnum, e->ec); 1647 e->pnum, e->ec);
1649 ubi_dbg_dump_stack(); 1648 ubi_dbg_dump_stack();
1650 return 1; 1649 return -EINVAL;
1651} 1650}
1652#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */ 1651#endif /* CONFIG_MTD_UBI_DEBUG_PARANOID */