Skip to contentSkip to conversation

init: auto-create ubiblock device for non-UBIFS rootfs on UBI

PATCH In review Daniel Golle 2 participants 3 messages Aug 27, 2016 – Aug 28, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 3/3 init: auto-create ubiblock device for non-UBIFS rootfs on UBI

---
 init/do_mounts.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 55 insertions(+), 7 deletions(-)
init/do_mounts.c +55 -7
index dea5de9..485df12 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/ramfs.h>
 #include <linux/shmem_fs.h>
+#include <linux/mtd/ubi.h>
 
 #include <linux/nfs_fs.h>
 #include <linux/nfs_fs_sb.h>
@@ -179,6 +180,47 @@ done:
 }
 #endif
 
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+#define UBIFS_NODE_MAGIC  0x06101831
+static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc)
+{
+	int ret;
+	uint32_t magic_of, magic;
+	ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4);
+	if (ret)
+		return 0;
+	magic = le32_to_cpu(magic_of);
+	return magic == UBIFS_NODE_MAGIC;
+}
+
+static void ubiblock_create_rootdev(char *name)
+{
+	int ret, is_ubifs;
+	struct ubi_volume_desc *desc;
+	struct ubi_volume_info vi;
+	dev_t bdev;
+
+	desc = ubi_open_volume_str(name, UBI_READONLY);
+	if (IS_ERR(desc))
+		return;
+
+	ubi_get_volume_info(desc, &vi);
+
+	is_ubifs = ubi_vol_is_ubifs(desc);
+	ubi_close_volume(desc);
+
+	if (is_ubifs)
+		return;
+
+	ret = ubiblock_create_dev(&vi, &bdev);
+	if (!ret) {
+		pr_notice("ubiblock%u_%u: '%s' set to be root filesystem\n",
+			  vi.ubi_num, vi.vol_id, vi.name);
+		ROOT_DEV = bdev;
+	}
+}
+#endif
+
 /*
  *	Convert a name into device number.  We accept the following variants:
  *
@@ -569,14 +611,20 @@ void __init prepare_namespace(void)
 
 	if (saved_root_name[0]) {
 		root_device_name = saved_root_name;
-		if (!strncmp(root_device_name, "mtd", 3) ||
-		    !strncmp(root_device_name, "ubi", 3)) {
-			mount_block_root(root_device_name, root_mountflags);
-			goto out;
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+		if (!strncmp(root_device_name, "ubi", 3))
+			ubiblock_create_rootdev(root_device_name);
+#endif
+		if (ROOT_DEV == 0) {
+			if (!strncmp(root_device_name, "mtd", 3) ||
+			    !strncmp(root_device_name, "ubi", 3)) {
+				mount_block_root(root_device_name, root_mountflags);
+				goto out;
+			}
+			ROOT_DEV = name_to_dev_t(root_device_name);
+			if (strncmp(root_device_name, "/dev/", 5) == 0)
+				root_device_name += 5;
 		}
-		ROOT_DEV = name_to_dev_t(root_device_name);
-		if (strncmp(root_device_name, "/dev/", 5) == 0)
-			root_device_name += 5;
 	}
 
 	if (initrd_load())
Signature · 1 line
2.9.3
#1 lore raw
Quoted 10 lines · On Sat, 27 Aug 2016 21:44:46 +0200 Daniel Golle <[email protected]> wrote:
---
 init/do_mounts.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 55 insertions(+), 7 deletions(-)
init/do_mounts.c +0 -0
index dea5de9..485df12 100644

init/do_mounts.c line 31

Quoted 4 lines
init/do_mounts.c +1 -0
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/ramfs.h>
 #include <linux/shmem_fs.h>
+#include <linux/mtd/ubi.h>

Really??? You include UBI stuff in generic kernel code? Come on. Linux is defining clear interfaces to be implemented by drivers/FS for a good
reason: the core code should be implementation agnostic, and you're just breaking this rule.

Quoted 9 lines
init/do_mounts.c +2 -0
 #include <linux/nfs_fs.h>
 #include <linux/nfs_fs_sb.h>
@@ -179,6 +180,47 @@ done:
 }
 #endif
 
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+#define UBIFS_NODE_MAGIC  0x06101831

init/do_mounts.c done: · line 194

Quoted 10 lines
init/do_mounts.c +10 -0
+static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc)
+{
+	int ret;
+	uint32_t magic_of, magic;
+	ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4);
+	if (ret)
+		return 0;
+	magic = le32_to_cpu(magic_of);
+	return magic == UBIFS_NODE_MAGIC;
+}

This is even worst. Now your parsing data within a specific volume to determine if the volume is likely to contain a UBIFS FS. And all that is done in core kernel code.

Quoted 17 lines
init/do_mounts.c +17 -0
+
+static void ubiblock_create_rootdev(char *name)
+{
+	int ret, is_ubifs;
+	struct ubi_volume_desc *desc;
+	struct ubi_volume_info vi;
+	dev_t bdev;
+
+	desc = ubi_open_volume_str(name, UBI_READONLY);
+	if (IS_ERR(desc))
+		return;
+
+	ubi_get_volume_info(desc, &vi);
+
+	is_ubifs = ubi_vol_is_ubifs(desc);
+	ubi_close_volume(desc);
+

init/do_mounts.c done: · line 221

Quoted 10 lines
init/do_mounts.c +10 -0
+	if (is_ubifs)
+		return;
+
+	ret = ubiblock_create_dev(&vi, &bdev);
+	if (!ret) {
+		pr_notice("ubiblock%u_%u: '%s' set to be root filesystem\n",
+			  vi.ubi_num, vi.vol_id, vi.name);
+		ROOT_DEV = bdev;
+	}
+}

And it continues here. Now you're automatically creating a ubiblock device based on the UBIFS detection, and again, this is in core kernel code.

Quoted 20 lines
init/do_mounts.c +9 -4
+#endif
+
 /*
  *	Convert a name into device number.  We accept the following variants:
  *
@@ -569,14 +611,20 @@ void __init prepare_namespace(void)
 
 	if (saved_root_name[0]) {
 		root_device_name = saved_root_name;
-		if (!strncmp(root_device_name, "mtd", 3) ||
-		    !strncmp(root_device_name, "ubi", 3)) {
-			mount_block_root(root_device_name, root_mountflags);
-			goto out;
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+		if (!strncmp(root_device_name, "ubi", 3))
+			ubiblock_create_rootdev(root_device_name);
+#endif
+		if (ROOT_DEV == 0) {
+			if (!strncmp(root_device_name, "mtd", 3) ||
+			    !strncmp(root_device_name, "ubi", 3)) {

init/do_mounts.c void __init prepare_namespace(void) · line 579

Quoted 10 lines
init/do_mounts.c +6 -3
+				mount_block_root(root_device_name, root_mountflags);
+				goto out;
+			}
+			ROOT_DEV = name_to_dev_t(root_device_name);
+			if (strncmp(root_device_name, "/dev/", 5) == 0)
+				root_device_name += 5;
 		}
-		ROOT_DEV = name_to_dev_t(root_device_name);
-		if (strncmp(root_device_name, "/dev/", 5) == 0)
-			root_device_name += 5;

And the last piece: you're making use of all the hacks you've introduced earlier to create your blockdevice and pass it to the 'mount blockdev FS' logic.

I hope you understand why this patch is not acceptable.

Quoted 3 lines
init/do_mounts.c +0 -0
 	}
 
 	if (initrd_load())
#2 lore raw

Hi Boris,

thanks for the review! This is more helpful and more of the type of feedback I was hoping for.

Quoted 8 lines · Boris Brezillon
Quoted 5 lines · On Sat, 27 Aug 2016 21:44:46 +0200 Daniel Golle <[email protected]> wrote:
---
 init/do_mounts.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 55 insertions(+), 7 deletions(-)
Quoted 14 lines

init/do_mounts.c line 31

Quoted 9 lines
init/do_mounts.c +1 -0
index dea5de9..485df12 100644
@@ -28,6 +28,7 @@
 #include <linux/slab.h>
 #include <linux/ramfs.h>
 #include <linux/shmem_fs.h>
+#include <linux/mtd/ubi.h>

Really??? You include UBI stuff in generic kernel code? Come on. Linux is defining clear interfaces to be implemented by drivers/FS for a good
reason: the core code should be implementation agnostic, and you're just breaking this rule.

The whole thing could be moved to a new file (similar as done for other things for specific subsystems, like do_mounts_md.c).

Quoted 8 lines
Quoted 8 lines
init/do_mounts.c +0 -0
 #include <linux/nfs_fs.h>
 #include <linux/nfs_fs_sb.h>
@@ -179,6 +180,47 @@ done:
 }
 #endif
 
Quoted 16 lines

init/do_mounts.c done: · line 194

Quoted 12 lines
init/do_mounts.c +12 -0
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+#define UBIFS_NODE_MAGIC  0x06101831
+static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc)
+{
+	int ret;
+	uint32_t magic_of, magic;
+	ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4);
+	if (ret)
+		return 0;
+	magic = le32_to_cpu(magic_of);
+	return magic == UBIFS_NODE_MAGIC;
+}

This is even worst. Now your parsing data within a specific volume to determine if the volume is likely to contain a UBIFS FS. And all that is done in core kernel code.

I was unsure, however, maybe ubiblock should generally refuse to create a ubiblock device if an UBIFS signature is found...?
In that case, this function and the logic using it below could be moved to driver/mtd/ubi/block.c

Quoted 24 lines
Quoted 24 lines
init/do_mounts.c +23 -0
+
+static void ubiblock_create_rootdev(char *name)
+{
+	int ret, is_ubifs;
+	struct ubi_volume_desc *desc;
+	struct ubi_volume_info vi;
+	dev_t bdev;
+
+	desc = ubi_open_volume_str(name, UBI_READONLY);
+	if (IS_ERR(desc))
+		return;
+
+	ubi_get_volume_info(desc, &vi);
+
+	is_ubifs = ubi_vol_is_ubifs(desc);
+	ubi_close_volume(desc);
+
+	if (is_ubifs)
+		return;
+
+	ret = ubiblock_create_dev(&vi, &bdev);
+	if (!ret) {
+		pr_notice("ubiblock%u_%u: '%s' set to be root filesystem\n",
Quoted 8 lines

init/do_mounts.c done: · line 221

Quoted 4 lines
init/do_mounts.c +4 -0
+			  vi.ubi_num, vi.vol_id, vi.name);
+		ROOT_DEV = bdev;
+	}
+}

And it continues here. Now you're automatically creating a ubiblock device based on the UBIFS detection, and again, this is in core kernel code.

This, again, could go into a file of it's own like init/do_mounts_ubiblock.c
which is just how it's done for ramdisk and mdraid stuff.

Quoted 27 lines
Quoted 27 lines
init/do_mounts.c +15 -4
+#endif
+
 /*
  *	Convert a name into device number.  We accept the following variants:
  *
@@ -569,14 +611,20 @@ void __init prepare_namespace(void)
 
 	if (saved_root_name[0]) {
 		root_device_name = saved_root_name;
-		if (!strncmp(root_device_name, "mtd", 3) ||
-		    !strncmp(root_device_name, "ubi", 3)) {
-			mount_block_root(root_device_name, root_mountflags);
-			goto out;
+#if defined(CONFIG_MTD_UBI_BLOCK) && !defined(CONFIG_MTD_UBI_MODULE)
+		if (!strncmp(root_device_name, "ubi", 3))
+			ubiblock_create_rootdev(root_device_name);
+#endif
+		if (ROOT_DEV == 0) {
+			if (!strncmp(root_device_name, "mtd", 3) ||
+			    !strncmp(root_device_name, "ubi", 3)) {
+				mount_block_root(root_device_name, root_mountflags);
+				goto out;
+			}
+			ROOT_DEV = name_to_dev_t(root_device_name);
+			if (strncmp(root_device_name, "/dev/", 5) == 0)
+				root_device_name += 5;
Quoted 8 lines

init/do_mounts.c void __init prepare_namespace(void) · line 579

Quoted 4 lines
init/do_mounts.c +0 -3
 		}
-		ROOT_DEV = name_to_dev_t(root_device_name);
-		if (strncmp(root_device_name, "/dev/", 5) == 0)
-			root_device_name += 5;

And the last piece: you're making use of all the hacks you've introduced earlier to create your blockdevice and pass it to the 'mount blockdev FS' logic.

This is what is done for all sorts of block devices in that function... What's wrong with that approach?

Quoted 2 lines

I hope you understand why this patch is not acceptable.

I surely do, it was sent in the intention to start a discussion and collect comments, not with the intention to have it merged at this stage. I thus really appreciate your detailed review, though I'm aware that you are opposed to the whole idea of automagically creating the ubiblock device and thereby allowing to unify the rootfs= cmdline syntax to be agnostic to the filesystem-type used.
Well, thank you anyway.

Cheers

Daniel

Quoted 5 lines
Quoted 4 lines
init/do_mounts.c +0 -0
 	}
 
 	if (initrd_load())