summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--commands/ubi.c30
-rw-r--r--commands/ubiformat.c24
-rw-r--r--commands/umount.c2
-rw-r--r--drivers/mtd/ubi/Makefile2
-rw-r--r--drivers/mtd/ubi/barebox.c (renamed from drivers/mtd/ubi/cdev.c)57
-rw-r--r--drivers/mtd/ubi/ubi.h1
-rw-r--r--fs/fs.c48
-rw-r--r--include/fs.h2
-rw-r--r--include/mtd/ubi-user.h3
9 files changed, 157 insertions, 12 deletions
diff --git a/commands/ubi.c b/commands/ubi.c
index 9463127151..844d75dcb8 100644
--- a/commands/ubi.c
+++ b/commands/ubi.c
@@ -122,14 +122,36 @@ BAREBOX_CMD_END
static int do_ubidetach(int argc, char *argv[])
{
- int ubi_num, ret;
+ int fd, ret;
+ struct mtd_info_user user;
if (argc != 2)
return COMMAND_ERROR_USAGE;
- ubi_num = simple_strtoul(argv[1], NULL, 0);
- ret = ubi_detach_mtd_dev(ubi_num, 1);
+ fd = open(argv[optind], O_RDWR);
+ if (fd < 0) {
+ int ubi_num = simple_strtoul(argv[1], NULL, 0);
+ ret = ubi_detach(ubi_num);
+ goto out;
+ }
+
+ ret = ioctl(fd, MEMGETINFO, &user);
+ if (!ret) {
+ int ubi_num = ubi_num_get_by_mtd(user.mtd);
+ if (ubi_num < 0) {
+ ret = ubi_num;
+ goto out;
+ }
+
+ ret = ubi_detach(ubi_num);
+ if (!ret)
+ goto out_close;
+ }
+
+out_close:
+ close(fd);
+out:
if (ret)
printf("failed to detach: %s\n", strerror(-ret));
@@ -139,7 +161,7 @@ static int do_ubidetach(int argc, char *argv[])
BAREBOX_CMD_START(ubidetach)
.cmd = do_ubidetach,
BAREBOX_CMD_DESC("detach an UBI device")
- BAREBOX_CMD_OPTS("UBINUM")
+ BAREBOX_CMD_OPTS("mtd device/UBINUM")
BAREBOX_CMD_GROUP(CMD_GRP_PART)
BAREBOX_CMD_END
diff --git a/commands/ubiformat.c b/commands/ubiformat.c
index 0172654231..2ffdd0c1b5 100644
--- a/commands/ubiformat.c
+++ b/commands/ubiformat.c
@@ -42,6 +42,7 @@
#include <libbb.h>
#include <libfile.h>
#include <linux/mtd/mtd.h>
+#include <linux/mtd/ubi.h>
#include <linux/kernel.h>
#include <linux/stat.h>
#include <linux/log2.h>
@@ -562,6 +563,7 @@ int do_ubiformat(int argc, char *argv[])
struct ubigen_info ui;
struct ubi_scan_info *si;
struct mtd_info_user mtd_user;
+ int ubi_num;
err = parse_opt(argc, argv);
if (err)
@@ -622,8 +624,15 @@ int do_ubiformat(int argc, char *argv[])
goto out_close;
}
- /* Make sure this MTD device is not attached to UBI */
- /* FIXME! Find a proper way to do this in barebox! */
+ ubi_num = ubi_num_get_by_mtd(mtd_user.mtd);
+ if (ubi_num >= 0) {
+ err = ubi_detach(ubi_num);
+ if (err) {
+ sys_errmsg("Cannot detach %d\n", err);
+ goto out_close;
+ }
+ }
+
eb_cnt = mtd_div_by_eb(mtd->size, mtd);
@@ -759,6 +768,17 @@ int do_ubiformat(int argc, char *argv[])
}
libscan_ubi_scan_free(si);
+
+ /* Reattach the ubi device in case it was attached in the beginning */
+ if (ubi_num >= 0) {
+ err = ubi_attach_mtd_dev(mtd_user.mtd, ubi_num, 0, 20);
+ if (err) {
+ pr_err("Failed to reattach ubi device to ubi number %d, %d\n",
+ ubi_num, err);
+ return err;
+ }
+ }
+
return 0;
out_free:
diff --git a/commands/umount.c b/commands/umount.c
index 84c84e42c6..fdf4da95a0 100644
--- a/commands/umount.c
+++ b/commands/umount.c
@@ -37,7 +37,7 @@ BAREBOX_CMD_HELP_END
BAREBOX_CMD_START(umount)
.cmd = do_umount,
BAREBOX_CMD_DESC("umount a filesystem")
- BAREBOX_CMD_OPTS("MOUNTPOINT")
+ BAREBOX_CMD_OPTS("MOUNTPOINT/DEVICEPATH")
BAREBOX_CMD_GROUP(CMD_GRP_PART)
BAREBOX_CMD_HELP(cmd_umount_help)
BAREBOX_CMD_END
diff --git a/drivers/mtd/ubi/Makefile b/drivers/mtd/ubi/Makefile
index 795b116ecc..33ac39026c 100644
--- a/drivers/mtd/ubi/Makefile
+++ b/drivers/mtd/ubi/Makefile
@@ -1,5 +1,5 @@
obj-$(CONFIG_MTD_UBI) += ubi.o
-ubi-y += vtbl.o vmt.o upd.o build.o cdev.o kapi.o eba.o io.o wl.o attach.o
+ubi-y += vtbl.o vmt.o upd.o build.o barebox.o kapi.o eba.o io.o wl.o attach.o
ubi-y += misc.o debug.o
ubi-$(CONFIG_MTD_UBI_FASTMAP) += fastmap.o
diff --git a/drivers/mtd/ubi/cdev.c b/drivers/mtd/ubi/barebox.c
index fe71a8d609..c26a2455b2 100644
--- a/drivers/mtd/ubi/cdev.c
+++ b/drivers/mtd/ubi/barebox.c
@@ -260,3 +260,60 @@ void ubi_cdev_remove(struct ubi_device *ubi)
devfs_remove(cdev);
kfree(cdev->name);
}
+
+static void ubi_umount_volumes(struct ubi_device *ubi)
+{
+ int i;
+
+ for (i = 0; i < ubi->vtbl_slots; i++) {
+ struct ubi_volume *vol = ubi->volumes[i];
+ if (!vol)
+ continue;
+ umount_by_cdev(&vol->cdev);
+ }
+}
+
+/**
+ * ubi_detach - detach an UBI device
+ * @ubi_num: The UBI device number
+ *
+ * UBI volumes used by UBIFS will be unmounted before detaching the
+ * UBI device.
+ *
+ * @return: 0 for success, negative error code otherwise
+ */
+int ubi_detach(int ubi_num)
+{
+ struct ubi_device *ubi;
+
+ if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
+ return -EINVAL;
+
+ ubi = ubi_devices[ubi_num];
+ if (!ubi)
+ return -ENOENT;
+
+ ubi_umount_volumes(ubi);
+
+ return ubi_detach_mtd_dev(ubi_num, 1);
+}
+
+/**
+ * ubi_num_get_by_mtd - find the ubi number to the given mtd
+ * @mtd: the mtd device
+ *
+ * @return: positive or zero for a UBI number, negative error code otherwise
+ */
+int ubi_num_get_by_mtd(struct mtd_info *mtd)
+{
+ int i;
+ struct ubi_device *ubi;
+
+ for (i = 0; i < UBI_MAX_DEVICES; i++) {
+ ubi = ubi_devices[i];
+ if (ubi && mtd == ubi->mtd)
+ return ubi->ubi_num;
+ }
+
+ return -ENOENT;
+}
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index 03a36d2e76..a8ed0d7710 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -809,6 +809,7 @@ int ubi_io_write_vid_hdr(struct ubi_device *ubi, int pnum,
struct ubi_vid_hdr *vid_hdr);
/* build.c */
+int ubi_detach_mtd_dev(int ubi_num, int anyway);
struct ubi_device *ubi_get_device(int ubi_num);
void ubi_put_device(struct ubi_device *ubi);
struct ubi_device *ubi_get_by_major(int major);
diff --git a/fs/fs.c b/fs/fs.c
index c4b3583433..440adae14c 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -1317,6 +1317,40 @@ err_free_path:
}
EXPORT_SYMBOL(mount);
+static int fsdev_umount(struct fs_device_d *fsdev)
+{
+ return unregister_device(&fsdev->dev);
+}
+
+/**
+ * umount_by_cdev Use a cdev struct to umount all mounted filesystems
+ * @param cdev cdev to the according device
+ * @return 0 on success or if cdev was not mounted, -errno otherwise
+ */
+int umount_by_cdev(struct cdev *cdev)
+{
+ struct fs_device_d *fs;
+ struct fs_device_d *fs_tmp;
+ int first_error = 0;
+
+ for_each_fs_device_safe(fs_tmp, fs) {
+ int ret;
+
+ if (fs->cdev == cdev) {
+ ret = fsdev_umount(fs);
+ if (ret) {
+ pr_err("Failed umounting %s, %d, continuing anyway\n",
+ fs->path, ret);
+ if (!first_error)
+ first_error = ret;
+ }
+ }
+ }
+
+ return first_error;
+}
+EXPORT_SYMBOL(umount_by_cdev);
+
int umount(const char *pathname)
{
struct fs_device_d *fsdev = NULL, *f;
@@ -1329,6 +1363,16 @@ int umount(const char *pathname)
}
}
+ if (!fsdev) {
+ struct cdev *cdev = cdev_open(p, O_RDWR);
+
+ if (cdev) {
+ free(p);
+ cdev_close(cdev);
+ return umount_by_cdev(cdev);
+ }
+ }
+
free(p);
if (f == fs_dev_root && !list_is_singular(&fs_device_list)) {
@@ -1341,9 +1385,7 @@ int umount(const char *pathname)
return -EFAULT;
}
- unregister_device(&fsdev->dev);
-
- return 0;
+ return fsdev_umount(fsdev);
}
EXPORT_SYMBOL(umount);
diff --git a/include/fs.h b/include/fs.h
index 9f4164ed77..b9d1e6e09a 100644
--- a/include/fs.h
+++ b/include/fs.h
@@ -88,6 +88,7 @@ struct fs_driver_d {
extern struct list_head fs_device_list;
#define for_each_fs_device(f) list_for_each_entry(f, &fs_device_list, list)
+#define for_each_fs_device_safe(tmp, f) list_for_each_entry_safe(f, tmp, &fs_device_list, list)
extern struct bus_type fs_bus;
struct fs_device_d {
@@ -143,6 +144,7 @@ int readlink(const char *path, char *buf, size_t bufsiz);
int mount (const char *device, const char *fsname, const char *path,
const char *fsoptions);
int umount(const char *pathname);
+int umount_by_cdev(struct cdev *cdev);
/* not-so-standard functions */
int erase(int fd, loff_t count, loff_t offset);
diff --git a/include/mtd/ubi-user.h b/include/mtd/ubi-user.h
index 2000ef2fd0..8c02f96e4c 100644
--- a/include/mtd/ubi-user.h
+++ b/include/mtd/ubi-user.h
@@ -406,6 +406,7 @@ struct ubi_set_vol_prop_req {
int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
int vid_hdr_offset, int max_beb_per1024);
-int ubi_detach_mtd_dev(int ubi_num, int anyway);
+int ubi_detach(int ubi_num);
+int ubi_num_get_by_mtd(struct mtd_info *mtd);
#endif /* __UBI_USER_H__ */