aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi/build.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mtd/ubi/build.c')
-rw-r--r--drivers/mtd/ubi/build.c137
1 files changed, 90 insertions, 47 deletions
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 14cec04c34f9..55c726dde942 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -37,12 +37,14 @@
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>
43#include <linux/kthread.h> 44#include <linux/kthread.h>
44#include <linux/reboot.h> 45#include <linux/reboot.h>
45#include <linux/kernel.h> 46#include <linux/kernel.h>
47#include <linux/slab.h>
46#include "ubi.h" 48#include "ubi.h"
47 49
48/* Maximum length of the 'mtd=' parameter */ 50/* Maximum length of the 'mtd=' parameter */
@@ -50,7 +52,8 @@
50 52
51/** 53/**
52 * struct mtd_dev_param - MTD device parameter description data structure. 54 * struct mtd_dev_param - MTD device parameter description data structure.
53 * @name: MTD device name or number string 55 * @name: MTD character device node path, MTD device name, or MTD device number
56 * string
54 * @vid_hdr_offs: VID header offset 57 * @vid_hdr_offs: VID header offset
55 */ 58 */
56struct mtd_dev_param { 59struct mtd_dev_param {
@@ -59,10 +62,10 @@ struct mtd_dev_param {
59}; 62};
60 63
61/* Numbers of elements set in the @mtd_dev_param array */ 64/* Numbers of elements set in the @mtd_dev_param array */
62static int mtd_devs; 65static int __initdata mtd_devs;
63 66
64/* MTD devices specification parameters */ 67/* MTD devices specification parameters */
65static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES]; 68static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
66 69
67/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */ 70/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
68struct class *ubi_class; 71struct class *ubi_class;
@@ -87,7 +90,8 @@ DEFINE_MUTEX(ubi_devices_mutex);
87static DEFINE_SPINLOCK(ubi_devices_lock); 90static DEFINE_SPINLOCK(ubi_devices_lock);
88 91
89/* "Show" method for files in '/<sysfs>/class/ubi/' */ 92/* "Show" method for files in '/<sysfs>/class/ubi/' */
90static ssize_t ubi_version_show(struct class *class, char *buf) 93static ssize_t ubi_version_show(struct class *class, struct class_attribute *attr,
94 char *buf)
91{ 95{
92 return sprintf(buf, "%d\n", UBI_VERSION); 96 return sprintf(buf, "%d\n", UBI_VERSION);
93} 97}
@@ -363,11 +367,13 @@ static void dev_release(struct device *dev)
363/** 367/**
364 * ubi_sysfs_init - initialize sysfs for an UBI device. 368 * ubi_sysfs_init - initialize sysfs for an UBI device.
365 * @ubi: UBI device description object 369 * @ubi: UBI device description object
370 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
371 * taken
366 * 372 *
367 * This function returns zero in case of success and a negative error code in 373 * This function returns zero in case of success and a negative error code in
368 * case of failure. 374 * case of failure.
369 */ 375 */
370static int ubi_sysfs_init(struct ubi_device *ubi) 376static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
371{ 377{
372 int err; 378 int err;
373 379
@@ -379,6 +385,7 @@ static int ubi_sysfs_init(struct ubi_device *ubi)
379 if (err) 385 if (err)
380 return err; 386 return err;
381 387
388 *ref = 1;
382 err = device_create_file(&ubi->dev, &dev_eraseblock_size); 389 err = device_create_file(&ubi->dev, &dev_eraseblock_size);
383 if (err) 390 if (err)
384 return err; 391 return err;
@@ -434,7 +441,7 @@ static void ubi_sysfs_close(struct ubi_device *ubi)
434} 441}
435 442
436/** 443/**
437 * kill_volumes - destroy all volumes. 444 * kill_volumes - destroy all user volumes.
438 * @ubi: UBI device description object 445 * @ubi: UBI device description object
439 */ 446 */
440static void kill_volumes(struct ubi_device *ubi) 447static void kill_volumes(struct ubi_device *ubi)
@@ -447,36 +454,29 @@ static void kill_volumes(struct ubi_device *ubi)
447} 454}
448 455
449/** 456/**
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. 457 * uif_init - initialize user interfaces for an UBI device.
470 * @ubi: UBI device description object 458 * @ubi: UBI device description object
459 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
460 * taken, otherwise set to %0
461 *
462 * This function initializes various user interfaces for an UBI device. If the
463 * initialization fails at an early stage, this function frees all the
464 * resources it allocated, returns an error, and @ref is set to %0. However,
465 * if the initialization fails after the UBI device was registered in the
466 * driver core subsystem, this function takes a reference to @ubi->dev, because
467 * otherwise the release function ('dev_release()') would free whole @ubi
468 * object. The @ref argument is set to %1 in this case. The caller has to put
469 * this reference.
471 * 470 *
472 * This function returns zero in case of success and a negative error code in 471 * 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. 472 * case of failure.
474 */ 473 */
475static int uif_init(struct ubi_device *ubi) 474static int uif_init(struct ubi_device *ubi, int *ref)
476{ 475{
477 int i, err; 476 int i, err;
478 dev_t dev; 477 dev_t dev;
479 478
479 *ref = 0;
480 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num); 480 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
481 481
482 /* 482 /*
@@ -504,7 +504,7 @@ static int uif_init(struct ubi_device *ubi)
504 goto out_unreg; 504 goto out_unreg;
505 } 505 }
506 506
507 err = ubi_sysfs_init(ubi); 507 err = ubi_sysfs_init(ubi, ref);
508 if (err) 508 if (err)
509 goto out_sysfs; 509 goto out_sysfs;
510 510
@@ -522,6 +522,8 @@ static int uif_init(struct ubi_device *ubi)
522out_volumes: 522out_volumes:
523 kill_volumes(ubi); 523 kill_volumes(ubi);
524out_sysfs: 524out_sysfs:
525 if (*ref)
526 get_device(&ubi->dev);
525 ubi_sysfs_close(ubi); 527 ubi_sysfs_close(ubi);
526 cdev_del(&ubi->cdev); 528 cdev_del(&ubi->cdev);
527out_unreg: 529out_unreg:
@@ -875,7 +877,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) 877int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
876{ 878{
877 struct ubi_device *ubi; 879 struct ubi_device *ubi;
878 int i, err, do_free = 1; 880 int i, err, ref = 0;
879 881
880 /* 882 /*
881 * Check if we already have the same MTD device attached. 883 * Check if we already have the same MTD device attached.
@@ -975,9 +977,9 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
975 goto out_detach; 977 goto out_detach;
976 } 978 }
977 979
978 err = uif_init(ubi); 980 err = uif_init(ubi, &ref);
979 if (err) 981 if (err)
980 goto out_nofree; 982 goto out_detach;
981 983
982 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name); 984 ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
983 if (IS_ERR(ubi->bgt_thread)) { 985 if (IS_ERR(ubi->bgt_thread)) {
@@ -1025,12 +1027,8 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
1025 1027
1026out_uif: 1028out_uif:
1027 uif_close(ubi); 1029 uif_close(ubi);
1028out_nofree:
1029 do_free = 0;
1030out_detach: 1030out_detach:
1031 ubi_wl_close(ubi); 1031 ubi_wl_close(ubi);
1032 if (do_free)
1033 free_user_volumes(ubi);
1034 free_internal_volumes(ubi); 1032 free_internal_volumes(ubi);
1035 vfree(ubi->vtbl); 1033 vfree(ubi->vtbl);
1036out_free: 1034out_free:
@@ -1039,7 +1037,10 @@ out_free:
1039#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 1037#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
1040 vfree(ubi->dbg_peb_buf); 1038 vfree(ubi->dbg_peb_buf);
1041#endif 1039#endif
1042 kfree(ubi); 1040 if (ref)
1041 put_device(&ubi->dev);
1042 else
1043 kfree(ubi);
1043 return err; 1044 return err;
1044} 1045}
1045 1046
@@ -1096,7 +1097,7 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1096 1097
1097 /* 1098 /*
1098 * Get a reference to the device in order to prevent 'dev_release()' 1099 * Get a reference to the device in order to prevent 'dev_release()'
1099 * from freeing @ubi object. 1100 * from freeing the @ubi object.
1100 */ 1101 */
1101 get_device(&ubi->dev); 1102 get_device(&ubi->dev);
1102 1103
@@ -1116,13 +1117,50 @@ int ubi_detach_mtd_dev(int ubi_num, int anyway)
1116} 1117}
1117 1118
1118/** 1119/**
1119 * find_mtd_device - open an MTD device by its name or number. 1120 * open_mtd_by_chdev - open an MTD device by its character device node path.
1120 * @mtd_dev: name or number of the device 1121 * @mtd_dev: MTD character device node path
1122 *
1123 * This helper function opens an MTD device by its character node device path.
1124 * Returns MTD device description object in case of success and a negative
1125 * error code in case of failure.
1126 */
1127static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1128{
1129 int err, major, minor, mode;
1130 struct path path;
1131
1132 /* Probably this is an MTD character device node path */
1133 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1134 if (err)
1135 return ERR_PTR(err);
1136
1137 /* MTD device number is defined by the major / minor numbers */
1138 major = imajor(path.dentry->d_inode);
1139 minor = iminor(path.dentry->d_inode);
1140 mode = path.dentry->d_inode->i_mode;
1141 path_put(&path);
1142 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1143 return ERR_PTR(-EINVAL);
1144
1145 if (minor & 1)
1146 /*
1147 * Just do not think the "/dev/mtdrX" devices support is need,
1148 * so do not support them to avoid doing extra work.
1149 */
1150 return ERR_PTR(-EINVAL);
1151
1152 return get_mtd_device(NULL, minor / 2);
1153}
1154
1155/**
1156 * open_mtd_device - open MTD device by name, character device path, or number.
1157 * @mtd_dev: name, character device node path, or MTD device device number
1121 * 1158 *
1122 * This function tries to open and MTD device described by @mtd_dev string, 1159 * 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 1160 * 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 1161 * 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. 1162 * as MTD character device node path. Returns MTD device description object in
1163 * case of success and a negative error code in case of failure.
1126 */ 1164 */
1127static struct mtd_info * __init open_mtd_device(const char *mtd_dev) 1165static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1128{ 1166{
@@ -1137,6 +1175,9 @@ static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1137 * MTD device name. 1175 * MTD device name.
1138 */ 1176 */
1139 mtd = get_mtd_device_nm(mtd_dev); 1177 mtd = get_mtd_device_nm(mtd_dev);
1178 if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1179 /* Probably this is an MTD character device node path */
1180 mtd = open_mtd_by_chdev(mtd_dev);
1140 } else 1181 } else
1141 mtd = get_mtd_device(NULL, mtd_num); 1182 mtd = get_mtd_device(NULL, mtd_num);
1142 1183
@@ -1352,13 +1393,15 @@ static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1352 1393
1353module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000); 1394module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
1354MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: " 1395MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
1355 "mtd=<name|num>[,<vid_hdr_offs>].\n" 1396 "mtd=<name|num|path>[,<vid_hdr_offs>].\n"
1356 "Multiple \"mtd\" parameters may be specified.\n" 1397 "Multiple \"mtd\" parameters may be specified.\n"
1357 "MTD devices may be specified by their number or name.\n" 1398 "MTD devices may be specified by their number, name, or "
1399 "path to the MTD character device node.\n"
1358 "Optional \"vid_hdr_offs\" parameter specifies UBI VID " 1400 "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
1359 "header position and data starting position to be used " 1401 "header position to be used by UBI.\n"
1360 "by UBI.\n" 1402 "Example 1: mtd=/dev/mtd0 - attach MTD device "
1361 "Example: mtd=content,1984 mtd=4 - attach MTD device" 1403 "/dev/mtd0.\n"
1404 "Example 2: mtd=content,1984 mtd=4 - attach MTD device "
1362 "with name \"content\" using VID header offset 1984, and " 1405 "with name \"content\" using VID header offset 1984, and "
1363 "MTD device number 4 with default VID header offset."); 1406 "MTD device number 4 with default VID header offset.");
1364 1407