Skip to contentSkip to conversation

initmpfs: use tmpfs instead of ramfs for rootfs

PATCH In review Rob Landley 2 participants 9 messages Jun 29, 2013 – Jun 30, 2013

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

cover initmpfs: use tmpfs instead of ramfs for rootfs

Use tmpfs for rootfs when CONFIG_TMPFS=y and there's no root=. Specify rootfstype=ramfs to get the old initramfs behavior.

The previous initramfs code provided a fairly crappy root filesystem:
didn't let you --bind mount directories out of it, reported zero size/usage so it didn't show up in "df" and couldn't run things like rpm that query available space before proceeding, would fill up all available memory and panic the system if you wrote too much to it...

Using tmpfs instead provides a much better root filesystem.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#1 lore raw

PATCH 1/5 initmpfs: replace MS_NOUSER in initramfs

From: Rob Landley <[email protected]>

Mounting MS_NOUSER prevents --bind mounts from rootfs. Prevent new rootfs mounts with a different mechanism that doesn't affect bind mounts.

---

 fs/ramfs/inode.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/ramfs/inode.c +8 -1
index c24f1e1..14b9c35 100644
@@ -247,7 +247,14 @@ struct dentry *ramfs_mount(struct file_system_type *fs_type,
 static struct dentry *rootfs_mount(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data)
 {
-	return mount_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
+	static int once;
+
+	if (once)
+		return ERR_PTR(-ENODEV);
+	else
+		once++;
+
+	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
 }
 
 static void ramfs_kill_sb(struct super_block *sb)
#2 lore raw

Re: [PATCH 1/5] initmpfs: replace MS_NOUSER in initramfs

Quoted 4 lines · Rob Landley

From: Rob Landley <[email protected]>

Mounting MS_NOUSER prevents --bind mounts from rootfs. Prevent new rootfs mounts with a different mechanism that doesn't affect bind mounts.

I don't see patches 4 and 5 so I don't know if you have covered this elsewhere but a very important part of the reason for MS_NOUSER is to prevent unmounting of rootfs.

The entire vfs breaks if you are allowed to unmount rootfs, and it appears this patch is allowing that.

Eric

Quoted 26 lines

---

 fs/ramfs/inode.c |    9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
fs/ramfs/inode.c +8 -1
index c24f1e1..14b9c35 100644
@@ -247,7 +247,14 @@ struct dentry *ramfs_mount(struct file_system_type *fs_type,
 static struct dentry *rootfs_mount(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data)
 {
-	return mount_nodev(fs_type, flags|MS_NOUSER, data, ramfs_fill_super);
+	static int once;
+
+	if (once)
+		return ERR_PTR(-ENODEV);
+	else
+		once++;
+
+	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
 }
 
 static void ramfs_kill_sb(struct super_block *sb)
#3 lore raw

Re: [PATCH 1/5] initmpfs: replace MS_NOUSER in initramfs

Quoted 11 lines · Eric W. Biederman
Quoted 3 lines · Rob Landley

From: Rob Landley <[email protected]>

Mounting MS_NOUSER prevents --bind mounts from rootfs. Prevent new

rootfs

Quoted 1 line

mounts with a different mechanism that doesn't affect bind mounts.

I don't see patches 4 and 5 so I don't know if you have covered this elsewhere but a very important part of the reason for MS_NOUSER is to prevent unmounting of rootfs.

Actually rootfs has separate protections against umounting. I tried several varieties of "umount -f /" and "cd /; umount -l .; umount -f .." and so on; they're all ignored.

Quoted 2 lines

The entire vfs breaks if you are allowed to unmount rootfs, and it appears this patch is allowing that.

Yes, I hit that many moons ago. (Doing either mount --move or pivot_root on initramfs, and then umounting it once it was in a subdirectory: system locked hard; I can try to dig up a link if you like, it was something like 2005? Maybe 2.6.11? It got fixed. This was before I implemented switch_root in busybox, because the experience is what taught me the difference between pivot_root and switch_root.)

I tested "mount --bind / home" on initmpfs and got "invalid argument". (Haven't tried pivot_root again because that binary's not on my test system, but when you're _not_ on rootfs that'll kill the system if you do it in the global namespace without knowing what you're doing. I can specifically test that if you like...)

Rob

#4 lore raw

Re: [PATCH 1/5] initmpfs: replace MS_NOUSER in initramfs

In reply to Eric W. Biederman
Quoted 6 lines · Eric W. Biederman
Quoted 3 lines · Rob Landley

From: Rob Landley <[email protected]>

Mounting MS_NOUSER prevents --bind mounts from rootfs. Prevent new

rootfs

Quoted 8 lines
Quoted 1 line

mounts with a different mechanism that doesn't affect bind mounts.

I don't see patches 4 and 5 so I don't know if you have covered this elsewhere but a very important part of the reason for MS_NOUSER is to prevent unmounting of rootfs.

The entire vfs breaks if you are allowed to unmount rootfs, and it appears this patch is allowing that.

I mean I tried "mount --move / home". (--bind worked.)

Long day...

Rob

#5 lore raw

PATCH 2/5 initmpfs: Move bdi setup from init_rootfs to init_ramfs

In reply to Rob Landley

From: Rob Landley <[email protected]>

Even though ramfs hasn't got a backing device, commit e0bf68ddec4f added one anyway, and put the initialization in init_rootfs() since that's the first user, leaving it out of init_ramfs() to avoid duplication.

But initmpfs uses init_tmpfs() instead, so move the init into the filesystem's init function, add a "once" guard to prevent duplicate initialization, and call the filesystem init from rootfs init.

This goes part of the way to allowing ramfs to be built as a module.

---

 fs/ramfs/inode.c |   25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
fs/ramfs/inode.c +20 -5
@@ -277,21 +277,36 @@
 
 static int __init init_ramfs_fs(void)
 {
-	return register_filesystem(&ramfs_fs_type);
+	static int once;
+	int err;
+
+	if (once)
+		return 0;
+	else
+		once++;
+
+	err = bdi_init(&ramfs_backing_dev_info);
+	if (err)
+		return err;
+
+	err = register_filesystem(&ramfs_fs_type);
+	if (err)
+		bdi_destroy(&ramfs_backing_dev_info);
+
+	return err;
 }
 module_init(init_ramfs_fs)
 
 int __init init_rootfs(void)
 {
-	int err;
+	int err = register_filesystem(&rootfs_fs_type);
 
-	err = bdi_init(&ramfs_backing_dev_info);
 	if (err)
 		return err;
 
-	err = register_filesystem(&rootfs_fs_type);
+	err = init_ramfs_fs();
 	if (err)
-		bdi_destroy(&ramfs_backing_dev_info);
+		unregister_filesystem(&rootfs_fs_type);
 
 	return err;
 }
#6 lore raw

PATCH 3/5 initmpfs: Move rootfs code from fs/ramfs/ to init/

In reply to Rob Landley

From: Rob Landley <[email protected]>

When the rootfs code was a wrapper around ramfs, having them in the same file made sense. Now that it can wrap another filesystem type, move it in with the init code instead.

This also allows a subsequent patch to access rootfstype= command line arg.

---

 fs/namespace.c        |    2 +-
 fs/ramfs/inode.c      |   34 +---------------------------------
 include/linux/init.h  |    1 +
 include/linux/ramfs.h |    2 +-
 init/do_mounts.c      |   34 ++++++++++++++++++++++++++++++++++
 5 files changed, 38 insertions(+), 35 deletions(-)
fs/namespace.c +1 -1
@@ -17,7 +17,7 @@
 #include <linux/security.h>
 #include <linux/idr.h>
 #include <linux/acct.h>		/* acct_auto_close_mnt */
-#include <linux/ramfs.h>	/* init_rootfs */
+#include <linux/init.h>		/* init_rootfs */
 #include <linux/fs_struct.h>	/* get_fs_root et.al. */
 #include <linux/fsnotify.h>	/* fsnotify_vfsmount_delete */
 #include <linux/uaccess.h>
fs/ramfs/inode.c +1 -33
@@ -244,19 +244,6 @@
 	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
 }
 
-static struct dentry *rootfs_mount(struct file_system_type *fs_type,
-	int flags, const char *dev_name, void *data)
-{
-	static int once;
-
-	if (once)
-		return ERR_PTR(-ENODEV);
-	else
-		once++;
-
-	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
-}
-
 static void ramfs_kill_sb(struct super_block *sb)
 {
 	kfree(sb->s_fs_info);
@@ -269,13 +256,8 @@
 	.kill_sb	= ramfs_kill_sb,
 	.fs_flags	= FS_USERNS_MOUNT,
 };
-static struct file_system_type rootfs_fs_type = {
-	.name		= "rootfs",
-	.mount		= rootfs_mount,
-	.kill_sb	= kill_litter_super,
-};
 
-static int __init init_ramfs_fs(void)
+int __init init_ramfs_fs(void)
 {
 	static int once;
 	int err;
@@ -296,17 +276,3 @@
 	return err;
 }
 module_init(init_ramfs_fs)
-
-int __init init_rootfs(void)
-{
-	int err = register_filesystem(&rootfs_fs_type);
-
-	if (err)
-		return err;
-
-	err = init_ramfs_fs();
-	if (err)
-		unregister_filesystem(&rootfs_fs_type);
-
-	return err;
-}
include/linux/init.h +1 -0
@@ -154,6 +154,7 @@
 void setup_arch(char **);
 void prepare_namespace(void);
 void __init load_default_modules(void);
+int __init init_rootfs(void);
 
 extern void (*late_time_init)(void);
 
include/linux/ramfs.h +1 -1
@@ -25,7 +25,7 @@
 
 extern const struct file_operations ramfs_file_operations;
 extern const struct vm_operations_struct generic_file_vm_ops;
-extern int __init init_rootfs(void);
+extern int __init init_ramfs_fs(void);
 
 int ramfs_fill_super(struct super_block *sb, void *data, int silent);
 
init/do_mounts.c +34 -0
@@ -26,6 +26,7 @@
 #include <linux/async.h>
 #include <linux/fs_struct.h>
 #include <linux/slab.h>
+#include <linux/ramfs.h>
 
 #include <linux/nfs_fs.h>
 #include <linux/nfs_fs_sb.h>
@@ -588,3 +589,36 @@
 	sys_mount(".", "/", NULL, MS_MOVE, NULL);
 	sys_chroot(".");
 }
+
+static struct dentry *rootfs_mount(struct file_system_type *fs_type,
+	int flags, const char *dev_name, void *data)
+{
+	static int once;
+
+	if (once)
+		return ERR_PTR(-ENODEV);
+	else
+		once++;
+
+	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
+}
+
+static struct file_system_type rootfs_fs_type = {
+	.name		= "rootfs",
+	.mount		= rootfs_mount,
+	.kill_sb	= kill_litter_super,
+};
+
+int __init init_rootfs(void)
+{
+	int err = register_filesystem(&rootfs_fs_type);
+
+	if (err)
+		return err;
+
+	err = init_ramfs_fs();
+	if (err)
+		unregister_filesystem(&rootfs_fs_type);
+
+	return err;
+}
fs/ramfs/inode.c +0 -0
index c24f1e1..3b9f114 100644
#7 lore raw

PATCH 4/5 initmpfs: Make rootfs use tmpfs when CONFIG_TMPFS enabled.

In reply to Rob Landley

From: Rob Landley <[email protected]>

Conditionally call the appropriate fs_init function and fill_super functions. Add a use once guard to shmem_init() to simply succeed on a second call.

(Note that IS_ENABLED() is a compile time constant so dead code elimination removes unused function calls when CONFIG_TMPFS is disabled.)

---

 init/do_mounts.c |   10 ++++++++--
 mm/shmem.c       |    4 ++++
 2 files changed, 12 insertions(+), 2 deletions(-)
init/do_mounts.c +8 -2
@@ -27,6 +27,7 @@
 #include <linux/fs_struct.h>
 #include <linux/slab.h>
 #include <linux/ramfs.h>
+#include <linux/shmem_fs.h>
 
 #include <linux/nfs_fs.h>
 #include <linux/nfs_fs_sb.h>
@@ -600,7 +599,8 @@
 	else
 		once++;
 
-	return mount_nodev(fs_type, flags, data, ramfs_fill_super);
+	return mount_nodev(fs_type, flags, data,
+		IS_ENABLED(CONFIG_TMPFS) ? shmem_fill_super : ramfs_fill_super);
 }
 
 static struct file_system_type rootfs_fs_type = {
@@ -616,7 +616,11 @@
 	if (err)
 		return err;
 
-	err = init_ramfs_fs();
+	if (IS_ENABLED(CONFIG_TMPFS))
+		err = shmem_init();
+	else
+		err = init_ramfs_fs();
+
 	if (err)
 		unregister_filesystem(&rootfs_fs_type);
 
mm/shmem.c +4 -0
@@ -2787,6 +2787,10 @@
 {
 	int error;
 
+	/* If rootfs called this, don't re-init */
+	if (shmem_inode_cachep)
+		return 0;
+
 	error = bdi_init(&shmem_backing_dev_info);
 	if (error)
 		goto out4;

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

PATCH 5/5 initmpfs: Use initramfs if rootfstype= or root= specified.

In reply to Rob Landley

From: Rob Landley <[email protected]>

Command line option rootfstype=ramfs to obtain old initramfs behavior, and use ramfs instead of tmpfs for stub when root= defined (for cosmetic reasons).

---

 init/do_mounts.c |   15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
init/do_mounts.c +11 -4
@@ -591,18 +591,22 @@
 	sys_chroot(".");
 }
 
+static bool is_tmpfs;
 static struct dentry *rootfs_mount(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data)
 {
 	static int once;
+	void *fill = ramfs_fill_super;
 
 	if (once)
 		return ERR_PTR(-ENODEV);
 	else
 		once++;
 
-	return mount_nodev(fs_type, flags, data,
-		IS_ENABLED(CONFIG_TMPFS) ? shmem_fill_super : ramfs_fill_super);
+	if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
+		fill = shmem_fill_super;
+
+	return mount_nodev(fs_type, flags, data, fill);
 }
 
 static struct file_system_type rootfs_fs_type = {
@@ -618,9 +622,12 @@
 	if (err)
 		return err;
 
-	if (IS_ENABLED(CONFIG_TMPFS))
+	if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
+		(!root_fs_names || strstr(root_fs_names, "tmpfs")))
+	{
 		err = shmem_init();
-	else
+		is_tmpfs = true;
+	} else
 		err = init_ramfs_fs();
 
 	if (err)