Skip to contentSkip to conversation

support booting of arbitrary non-blockdevice file systems

PATCH In review Christoph Hellwig 3 participants 7 messages Jun 17, 2021 – Jun 18, 2021 VFS

lore
Share & tools
lore
#0 lore raw

Hi all,

this series adds support to boot off arbitrary non-blockdevice root file systems, based off an earlier patch from Vivek.

#1 lore raw

PATCH 1/2 init: split get_fs_names

Split get_fs_names into one function that splits up the command line argument, and one that gets the list of all registered file systems.

---
 init/do_mounts.c | 48 ++++++++++++++++++++++++++----------------------
 1 file changed, 26 insertions(+), 22 deletions(-)
init/do_mounts.c +26 -22
index 74aede860de7..ec32de3ad52b 100644
@@ -338,30 +338,31 @@ __setup("rootflags=", root_data_setup);
 __setup("rootfstype=", fs_names_setup);
 __setup("rootdelay=", root_delay_setup);
 
-static void __init get_fs_names(char *page)
+static void __init split_fs_names(char *page, char *names)
 {
-	char *s = page;
-
-	if (root_fs_names) {
-		strcpy(page, root_fs_names);
-		while (*s++) {
-			if (s[-1] == ',')
-				s[-1] = '\0';
-		}
-	} else {
-		int len = get_filesystem_list(page);
-		char *p, *next;
+	strcpy(page, root_fs_names);
+	while (*page++) {
+		if (page[-1] == ',')
+			page[-1] = '\0';
+	}
+	*page = '\0';
+}
 
-		page[len] = '\0';
-		for (p = page-1; p; p = next) {
-			next = strchr(++p, '\n');
-			if (*p++ != '\t')
-				continue;
-			while ((*s++ = *p++) != '\n')
-				;
-			s[-1] = '\0';
-		}
+static void __init get_all_fs_names(char *page)
+{
+	int len = get_filesystem_list(page);
+	char *s = page, *p, *next;
+
+	page[len] = '\0';
+	for (p = page - 1; p; p = next) {
+		next = strchr(++p, '\n');
+		if (*p++ != '\t')
+			continue;
+		while ((*s++ = *p++) != '\n')
+			;
+		s[-1] = '\0';
 	}
+
 	*s = '\0';
 }
 
@@ -411,7 +412,10 @@ void __init mount_block_root(char *name, int flags)
 
 	scnprintf(b, BDEVNAME_SIZE, "unknown-block(%u,%u)",
 		  MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
-	get_fs_names(fs_names);
+	if (root_fs_names)
+		split_fs_names(fs_names, root_fs_names);
+	else
+		get_all_fs_names(fs_names);
 retry:
 	for (p = fs_names; *p; p += strlen(p)+1) {
 		int err = do_mount_root(name, p, flags, root_mount_data);
Signature · 1 line
2.30.2
#2 lore raw

PATCH 2/2 init: allow mounting arbitrary non-blockdevice filesystems as root

In reply to Christoph Hellwig

Currently the only non-blockdevice filesystems that can be used as the initial root filesystem are NFS and CIFS, which use the magic "root=/dev/nfs" and "root=/dev/cifs" syntax that requires the root device file system details to come from filesystem specific kernel command line options.

Add a little bit of new code that allows to just pass arbitrary string mount options to any non-blockdevice filesystems so that it can be mounted as the root file system.

For example a virtiofs root file system can be mounted using the following syntax:

"root=myfs rootfstype=virtiofs rw"

Based on an earlier patch from Vivek Goyal <[email protected]>.

---
 init/do_mounts.c | 40 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 40 insertions(+)
init/do_mounts.c +40 -0
index ec32de3ad52b..64c60cb72ecb 100644
@@ -534,6 +534,44 @@ static int __init mount_cifs_root(void)
 }
 #endif
 
+static int __init try_mount_nodev(char *fstype)
+{
+	struct file_system_type *fs = get_fs_type(fstype);
+	int err = -EINVAL;
+
+	if (!fs)
+		return -EINVAL;
+	if (!(fs->fs_flags & (FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA)))
+		err = do_mount_root(root_device_name, fstype, root_mountflags,
+					root_mount_data);
+	put_filesystem(fs);
+
+	if (err != -EACCES && err != -EINVAL)
+		panic("VFS: Unable to mount root \"%s\" (%s), err=%d\n",
+			      root_device_name, fstype, err);
+	return err;
+}
+
+static int __init mount_nodev_root(void)
+{
+	char *fs_names, *p;
+	int err = -EINVAL;
+
+	fs_names = (void *)__get_free_page(GFP_KERNEL);
+	if (!fs_names)
+		return -EINVAL;
+	split_fs_names(fs_names, root_fs_names);
+
+	for (p = fs_names; *p; p += strlen(p) + 1) {
+		err = try_mount_nodev(p);
+		if (!err)
+			break;
+	}
+
+	free_page((unsigned long)fs_names);
+	return err;
+}
+
 void __init mount_root(void)
 {
 #ifdef CONFIG_ROOT_NFS
@@ -550,6 +588,8 @@ void __init mount_root(void)
 		return;
 	}
 #endif
+	if (ROOT_DEV == 0 && mount_nodev_root() == 0)
+		return;
 #ifdef CONFIG_BLOCK
 	{
 		int err = create_dev("/dev/root", ROOT_DEV);
Signature · 1 line
2.30.2
#3 lore raw

Re: [PATCH 2/2] init: allow mounting arbitrary non-blockdevice filesystems as root

On Thu, Jun 17, 2021 at 05:36:49PM +0200, Christoph Hellwig wrote:

[..]

init/do_mounts.c static int __init mount_cifs_root(void) · line 544

Quoted 8 lines
init/do_mounts.c +8 -0
+static int __init try_mount_nodev(char *fstype)
+{
+	struct file_system_type *fs = get_fs_type(fstype);
+	int err = -EINVAL;
+
+	if (!fs)
+		return -EINVAL;
+	if (!(fs->fs_flags & (FS_REQUIRES_DEV | FS_BINARY_MOUNTDATA)))

Not sure what FS_BINARY_MOUNTDATA is why fs should not have that set. nfs seems to set it too. So that means they can't use try_mount_nodev().

init/do_mounts.c static int __init mount_cifs_root(void) · line 549

Quoted 5 lines
init/do_mounts.c +5 -0
+		err = do_mount_root(root_device_name, fstype, root_mountflags,
+					root_mount_data);
+	put_filesystem(fs);
+
+	if (err != -EACCES && err != -EINVAL)

In case of success err == 0, but we still panic(). We will need to check for success as well.

	if (err && err != -EACCES && err != -EINVAL)
Quoted 4 lines
init/do_mounts.c +4 -0
+		panic("VFS: Unable to mount root \"%s\" (%s), err=%d\n",
+			      root_device_name, fstype, err);
+	return err;
+}

init/do_mounts.c static int __init mount_cifs_root(void) · line 563

Quoted 10 lines
init/do_mounts.c +10 -0
+
+static int __init mount_nodev_root(void)
+{
+	char *fs_names, *p;
+	int err = -EINVAL;
+
+	fs_names = (void *)__get_free_page(GFP_KERNEL);
+	if (!fs_names)
+		return -EINVAL;
+	split_fs_names(fs_names, root_fs_names);

root_fs_names can be NULL and it crashes with NULL pointer dereference.

Vivek

Quoted 26 lines
init/do_mounts.c +13 -0
+
+	for (p = fs_names; *p; p += strlen(p) + 1) {
+		err = try_mount_nodev(p);
+		if (!err)
+			break;
+	}
+
+	free_page((unsigned long)fs_names);
+	return err;
+}
+
 void __init mount_root(void)
 {
 #ifdef CONFIG_ROOT_NFS
@@ -550,6 +588,8 @@ void __init mount_root(void)
 		return;
 	}
 #endif
+	if (ROOT_DEV == 0 && mount_nodev_root() == 0)
+		return;
 #ifdef CONFIG_BLOCK
 	{
 		int err = create_dev("/dev/root", ROOT_DEV);
Signature · 1 line
2.30.2
#4 lore raw

Re: [PATCH 2/2] init: allow mounting arbitrary non-blockdevice filesystems as root

Quoted 2 lines · Vivek Goyal

Not sure what FS_BINARY_MOUNTDATA is why fs should not have that set. nfs seems to set it too. So that means they can't use try_mount_nodev().

We can't really pass actual binary mountdata using the string separation scheme used by the rootfstype= option. But given that NFS only uses binary mountdata for legacy reasons and people get what they ask for using the option I think we can drop the check.

Quoted 2 lines

In case of success err == 0, but we still panic(). We will need to check for success as well.

Indeed.

Quoted 1 line

root_fs_names can be NULL and it crashes with NULL pointer dereference.

True.

What do you think of this version?

---

Quoted 1 line

From 141caa79a619b5f5d100eeb8e94ecf8b3b1c9af7 Mon Sep 17 00:00:00 2001

From: Christoph Hellwig <[email protected]>
Date: Fri, 18 Jun 2021 15:10:39 +0200
Subject: init: allow mounting arbitrary non-blockdevice filesystems as root

Currently the only non-blockdevice filesystems that can be used as the initial root filesystem are NFS and CIFS, which use the magic "root=/dev/nfs" and "root=/dev/cifs" syntax that requires the root device file system details to come from filesystem specific kernel command line options.

Add a little bit of new code that allows to just pass arbitrary string mount options to any non-blockdevice filesystems so that it can be mounted as the root file system.

For example a virtiofs root file system can be mounted using the following syntax:

"root=myfs rootfstype=virtiofs rw"

Based on an earlier patch from Vivek Goyal <[email protected]>.

---
 init/do_mounts.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
init/do_mounts.c +43 -0
index ec32de3ad52b..66c47193e9ee 100644
@@ -534,6 +534,45 @@ static int __init mount_cifs_root(void)
 }
 #endif
 
+static bool __init fs_is_nodev(char *fstype)
+{
+	struct file_system_type *fs = get_fs_type(fstype);
+	bool ret = false;
+
+	if (fs) {
+		ret = !(fs->fs_flags & FS_REQUIRES_DEV);
+		put_filesystem(fs);
+	}
+
+	return ret;
+}
+
+static int __init mount_nodev_root(void)
+{
+	char *fs_names, *fstype;
+	int err = -EINVAL;
+
+	fs_names = (void *)__get_free_page(GFP_KERNEL);
+	if (!fs_names)
+		return -EINVAL;
+	split_fs_names(fs_names, root_fs_names);
+
+	for (fstype = fs_names; *fstype; fstype += strlen(fstype) + 1) {
+		if (!fs_is_nodev(fstype))
+			continue;
+		err = do_mount_root(root_device_name, fstype, root_mountflags,
+				    root_mount_data);
+		if (!err)
+			break;
+		if (err != -EACCES && err != -EINVAL)
+			panic("VFS: Unable to mount root \"%s\" (%s), err=%d\n",
+			      root_device_name, fstype, err);
+	}
+
+	free_page((unsigned long)fs_names);
+	return err;
+}
+
 void __init mount_root(void)
 {
 #ifdef CONFIG_ROOT_NFS
@@ -550,6 +589,10 @@ void __init mount_root(void)
 		return;
 	}
 #endif
+	if (ROOT_DEV == 0 && root_fs_names) {
+		if (mount_nodev_root() == 0)
+			return;
+	}
 #ifdef CONFIG_BLOCK
 	{
 		int err = create_dev("/dev/root", ROOT_DEV);
Signature · 1 line
2.30.2
#5 lore raw

Re: [PATCH 2/2] init: allow mounting arbitrary non-blockdevice filesystems as root

Quoted 12 lines · Christoph Hellwig
Quoted 2 lines · Vivek Goyal

Not sure what FS_BINARY_MOUNTDATA is why fs should not have that set. nfs seems to set it too. So that means they can't use try_mount_nodev().

We can't really pass actual binary mountdata using the string separation scheme used by the rootfstype= option. But given that NFS only uses binary mountdata for legacy reasons and people get what they ask for using the option I think we can drop the check.

Quoted 2 lines

In case of success err == 0, but we still panic(). We will need to check for success as well.

Quoted 7 lines

Indeed.

Quoted 1 line

root_fs_names can be NULL and it crashes with NULL pointer dereference.

True.

What do you think of this version?

[ Cc Dominique Martinet ]

Hi Christoph,

This one works well for me. Just one minor nit. root_device_name, can be NULL as well if user passes following.

"rootfstype=virtiofs rw".

And currently mount_nodev_root() is assuming root_device_name is not NULL and we crash with null pointer dereference.

May be something like this.

        if (ROOT_DEV == 0 && root_device_name && root_fs_names) {
                if (mount_nodev_root() == 0)
                        return;
        }

Strangely people are using "rootfstype=virtiofs rw" to mount virtiofs as rootfs. They give their filesystem a tag named "/dev/root". And currently it works and they can mount virtiofs as rootfs.

With above change, current hackish way will also continue to work and not break existing setups.

Thanks
Vivek

Quoted 93 lines

---
From 141caa79a619b5f5d100eeb8e94ecf8b3b1c9af7 Mon Sep 17 00:00:00 2001
From: Christoph Hellwig <[email protected]>
Date: Fri, 18 Jun 2021 15:10:39 +0200
Subject: init: allow mounting arbitrary non-blockdevice filesystems as root

Currently the only non-blockdevice filesystems that can be used as the initial root filesystem are NFS and CIFS, which use the magic "root=/dev/nfs" and "root=/dev/cifs" syntax that requires the root device file system details to come from filesystem specific kernel command line options.

Add a little bit of new code that allows to just pass arbitrary string mount options to any non-blockdevice filesystems so that it can be mounted as the root file system.

For example a virtiofs root file system can be mounted using the following syntax:

"root=myfs rootfstype=virtiofs rw"

Based on an earlier patch from Vivek Goyal <[email protected]>.

---
 init/do_mounts.c | 43 +++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)
init/do_mounts.c +43 -0
index ec32de3ad52b..66c47193e9ee 100644
@@ -534,6 +534,45 @@ static int __init mount_cifs_root(void)
 }
 #endif
 
+static bool __init fs_is_nodev(char *fstype)
+{
+	struct file_system_type *fs = get_fs_type(fstype);
+	bool ret = false;
+
+	if (fs) {
+		ret = !(fs->fs_flags & FS_REQUIRES_DEV);
+		put_filesystem(fs);
+	}
+
+	return ret;
+}
+
+static int __init mount_nodev_root(void)
+{
+	char *fs_names, *fstype;
+	int err = -EINVAL;
+
+	fs_names = (void *)__get_free_page(GFP_KERNEL);
+	if (!fs_names)
+		return -EINVAL;
+	split_fs_names(fs_names, root_fs_names);
+
+	for (fstype = fs_names; *fstype; fstype += strlen(fstype) + 1) {
+		if (!fs_is_nodev(fstype))
+			continue;
+		err = do_mount_root(root_device_name, fstype, root_mountflags,
+				    root_mount_data);
+		if (!err)
+			break;
+		if (err != -EACCES && err != -EINVAL)
+			panic("VFS: Unable to mount root \"%s\" (%s), err=%d\n",
+			      root_device_name, fstype, err);
+	}
+
+	free_page((unsigned long)fs_names);
+	return err;
+}
+
 void __init mount_root(void)
 {
 #ifdef CONFIG_ROOT_NFS
@@ -550,6 +589,10 @@ void __init mount_root(void)
 		return;
 	}
 #endif
+	if (ROOT_DEV == 0 && root_fs_names) {
+		if (mount_nodev_root() == 0)
+			return;
+	}
 #ifdef CONFIG_BLOCK
 	{
 		int err = create_dev("/dev/root", ROOT_DEV);
Signature · 1 line
2.30.2
#6 lore raw
In reply to Christoph Hellwig
Quoted 2 lines · Christoph Hellwig

this series adds support to boot off arbitrary non-blockdevice root file systems, based off an earlier patch from Vivek.

Cool, thanks for working on generic syntax for mounting non-blockdevice file systems. Looks good modulo the comments Vivek had.

Stefan