From fd0eba1d63339805c2f3ec4e2ab493c1d5aef2de Mon Sep 17 00:00:00 2001
From: Ryan Hope <rmh3093@gmail.com>
Date: Fri, 17 Apr 2009 09:43:47 -0400
Subject: [PATCH 1/1] tmpfs-root

---
 fs/Kconfig       |   21 +++++++++++++++++++++
 fs/Makefile      |    1 +
 fs/ramfs/inode.c |   39 +++++++++++++++++++++------------------
 mm/shmem.c       |   26 ++++++++++++++++++++++++++
 4 files changed, 69 insertions(+), 18 deletions(-)

diff --git a/fs/Kconfig b/fs/Kconfig
index 64d44ef..770ed72 100644
--- a/fs/Kconfig
+++ b/fs/Kconfig
@@ -133,6 +133,27 @@ config TMPFS_POSIX_ACL
 
 	  If you don't know what Access Control Lists are, say N.
 
+config TMPFS_ROOT
+	bool "Use tmpfs instrad of ramfs for initramfs"
+	depends on TMPFS && SHMEM
+	default n
+	help
+	  This replaces the ramfs used for unpacking the cpio images
+	  with tmpfs.
+
+	  If unsure, say N
+
+config RAMFS
+	bool "Ramfs file system support" if TMPFS_ROOT
+	default y
+	---help---
+	  Ramfs is a file system which keeps all files in RAM. Unlike tmpfs,
+	  it cannot be swapped to disk, and it has the risk of deadlocking
+	  the system by using all memory.
+
+	  Ramfs is used for booting the system and unpacking the cpio if
+	  TMPFS_ROOT is not set.
+
 config HUGETLBFS
 	bool "HugeTLB file system support"
 	depends on X86 || IA64 || SPARC64 || (S390 && 64BIT) || \
diff --git a/fs/Makefile b/fs/Makefile
index af6d047..5b0a5d0 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -64,6 +64,7 @@ obj-$(CONFIG_DLM)		+= dlm/
  
 # Do not add any filesystems before this line
 obj-$(CONFIG_FSCACHE)		+= fscache/
+obj-$(CONFIG_RAMFS)             += ramfs/
 obj-$(CONFIG_REISERFS_FS)	+= reiserfs/
 obj-$(CONFIG_EXT3_FS)		+= ext3/ # Before ext2 so root fs can be ext3
 obj-$(CONFIG_EXT2_FS)		+= ext2/
diff --git a/fs/ramfs/inode.c b/fs/ramfs/inode.c
index a6090aa..93d0874 100644
--- a/fs/ramfs/inode.c
+++ b/fs/ramfs/inode.c
@@ -267,6 +267,13 @@ int ramfs_get_sb(struct file_system_type *fs_type,
 	return get_sb_nodev(fs_type, flags, data, ramfs_fill_super, mnt);
 }
 
+static struct file_system_type ramfs_fs_type = {
+	.name		= "ramfs",
+	.get_sb		= ramfs_get_sb,
+	.kill_sb	= kill_litter_super,
+};
+
+#ifndef CONFIG_TMPFS_ROOT
 static int rootfs_get_sb(struct file_system_type *fs_type,
 	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
 {
@@ -280,30 +287,12 @@ static void ramfs_kill_sb(struct super_block *sb)
 	kill_litter_super(sb);
 }
 
-static struct file_system_type ramfs_fs_type = {
-	.name		= "ramfs",
-	.get_sb		= ramfs_get_sb,
-	.kill_sb	= ramfs_kill_sb,
-};
 static struct file_system_type rootfs_fs_type = {
 	.name		= "rootfs",
 	.get_sb		= rootfs_get_sb,
 	.kill_sb	= kill_litter_super,
 };
 
-static int __init init_ramfs_fs(void)
-{
-	return register_filesystem(&ramfs_fs_type);
-}
-
-static void __exit exit_ramfs_fs(void)
-{
-	unregister_filesystem(&ramfs_fs_type);
-}
-
-module_init(init_ramfs_fs)
-module_exit(exit_ramfs_fs)
-
 int __init init_rootfs(void)
 {
 	int err;
@@ -318,5 +307,19 @@ int __init init_rootfs(void)
 
 	return err;
 }
+#endif
+
+static int __init init_ramfs_fs(void)
+{
+	return register_filesystem(&ramfs_fs_type);
+}
+
+static void __exit exit_ramfs_fs(void)
+{
+	unregister_filesystem(&ramfs_fs_type);
+}
+
+module_init(init_ramfs_fs)
+module_exit(exit_ramfs_fs)
 
 MODULE_LICENSE("GPL");
diff --git a/mm/shmem.c b/mm/shmem.c
index eef4ebe..3fab1cd 100644
--- a/mm/shmem.c
+++ b/mm/shmem.c
@@ -2413,6 +2413,10 @@ static void init_once(void *foo)
 
 static int init_inodecache(void)
 {
+#ifdef CONFIG_TMPFS_ROOT
+	if (shmem_inode_cachep)
+		return 0;
+#endif
 	shmem_inode_cachep = kmem_cache_create("shmem_inode_cache",
 				sizeof(struct shmem_inode_info),
 				0, SLAB_PANIC, init_once);
@@ -2684,6 +2688,28 @@ put_memory:
 }
 EXPORT_SYMBOL_GPL(shmem_file_setup);
 
+#ifdef CONFIG_TMPFS_ROOT
+static int rootfs_get_sb(struct file_system_type *fs_type,
+	int flags, const char *dev_name, void *data, struct vfsmount *mnt)
+{
+	return get_sb_nodev(fs_type, flags, data, shmem_fill_super, mnt);
+}
+
+
+static struct file_system_type rootfs_fs_type = {
+	.name		= "rootfs",
+	.get_sb		= rootfs_get_sb,
+	.kill_sb	= kill_litter_super,
+};
+
+int __init init_rootfs(void)
+{
+	if (init_inodecache())
+		panic("Can't initialize shm inode cache");
+	return register_filesystem(&rootfs_fs_type);
+}
+#endif
+
 /**
  * shmem_zero_setup - setup a shared anonymous mapping
  * @vma: the vma to be mmapped is prepared by do_mmap_pgoff
-- 
