2 * Resizable simple ram filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
7 * Usage limits added by David Gibson, Linuxcare Australia.
8 * This file is released under the GPL.
12 * NOTE! This filesystem is probably most useful
13 * not as a real filesystem, but as an example of
14 * how virtual filesystems can be written.
16 * It doesn't get much simpler than this. Consider
17 * that this file implements the full semantics of
18 * a POSIX-compliant read-write filesystem.
20 * Note in particular how the filesystem does not
21 * need to implement any data structures of its own
22 * to keep track of the virtual data: using the VFS
23 * caches is sufficient.
26 #include <linux/module.h>
28 #include <linux/pagemap.h>
29 #include <linux/init.h>
30 #include <linux/string.h>
31 #include <linux/locks.h>
33 #include <asm/uaccess.h>
35 /* some random number */
36 #define RAMFS_MAGIC 0x858458f6
38 static struct super_operations ramfs_ops;
39 static struct address_space_operations ramfs_aops;
40 static struct file_operations ramfs_file_operations;
41 static struct inode_operations ramfs_dir_inode_operations;
43 static int ramfs_statfs(struct super_block *sb, struct statfs *buf)
45 buf->f_type = RAMFS_MAGIC;
46 buf->f_bsize = PAGE_CACHE_SIZE;
47 buf->f_namelen = NAME_MAX;
52 * Lookup the data. This is trivial - if the dentry didn't already
53 * exist, we know it is negative.
55 static struct dentry * ramfs_lookup(struct inode *dir, struct dentry *dentry)
57 if (dentry->d_name.len > NAME_MAX)
58 return ERR_PTR(-ENAMETOOLONG);
64 * Read a page. Again trivial. If it didn't already exist
65 * in the page cache, it is zero-filled.
67 static int ramfs_readpage(struct file *file, struct page * page)
69 if (!Page_Uptodate(page)) {
70 memset(kmap(page), 0, PAGE_CACHE_SIZE);
72 flush_dcache_page(page);
73 SetPageUptodate(page);
79 static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
81 void *addr = kmap(page);
82 if (!Page_Uptodate(page)) {
83 memset(addr, 0, PAGE_CACHE_SIZE);
84 flush_dcache_page(page);
85 SetPageUptodate(page);
91 static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
93 struct inode *inode = page->mapping->host;
94 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
97 if (pos > inode->i_size)
102 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
104 struct inode * inode = new_inode(sb);
107 inode->i_mode = mode;
108 inode->i_uid = current->fsuid;
109 inode->i_gid = current->fsgid;
110 inode->i_blksize = PAGE_CACHE_SIZE;
112 inode->i_rdev = NODEV;
113 inode->i_mapping->a_ops = &ramfs_aops;
114 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
115 switch (mode & S_IFMT) {
117 init_special_inode(inode, mode, dev);
120 inode->i_fop = &ramfs_file_operations;
123 inode->i_op = &ramfs_dir_inode_operations;
124 inode->i_fop = &dcache_dir_ops;
127 inode->i_op = &page_symlink_inode_operations;
135 * File creation. Allocate an inode, and we're done..
137 static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
139 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
143 if (dir->i_mode & S_ISGID) {
144 inode->i_gid = dir->i_gid;
146 inode->i_mode |= S_ISGID;
148 d_instantiate(dentry, inode);
149 dget(dentry); /* Extra count - pin the dentry in core */
155 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
157 return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
160 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
162 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
168 static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
170 struct inode *inode = old_dentry->d_inode;
172 if (S_ISDIR(inode->i_mode))
176 atomic_inc(&inode->i_count); /* New dentry reference */
177 dget(dentry); /* Extra pinning count for the created dentry */
178 d_instantiate(dentry, inode);
182 static inline int ramfs_positive(struct dentry *dentry)
184 return dentry->d_inode && !d_unhashed(dentry);
188 * Check that a directory is empty (this works
189 * for regular files too, they'll just always be
190 * considered empty..).
192 * Note that an empty directory can still have
193 * children, they just all have to be negative..
195 static int ramfs_empty(struct dentry *dentry)
197 struct list_head *list;
199 spin_lock(&dcache_lock);
200 list = dentry->d_subdirs.next;
202 while (list != &dentry->d_subdirs) {
203 struct dentry *de = list_entry(list, struct dentry, d_child);
205 if (ramfs_positive(de)) {
206 spin_unlock(&dcache_lock);
211 spin_unlock(&dcache_lock);
216 * This works for both directories and regular files.
217 * (non-directories will always have empty subdirs)
219 static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
221 int retval = -ENOTEMPTY;
223 if (ramfs_empty(dentry)) {
224 struct inode *inode = dentry->d_inode;
227 dput(dentry); /* Undo the count from "create" - this does all the work */
233 #define ramfs_rmdir ramfs_unlink
236 * The VFS layer already does all the dentry stuff for rename,
237 * we just have to decrement the usage count for the target if
238 * it exists so that the VFS layer correctly free's it when it
241 static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
243 int error = -ENOTEMPTY;
245 if (ramfs_empty(new_dentry)) {
246 struct inode *inode = new_dentry->d_inode;
256 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
260 error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
262 int l = strlen(symname)+1;
263 struct inode *inode = dentry->d_inode;
264 error = block_symlink(inode, symname, l);
269 static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
274 static struct address_space_operations ramfs_aops = {
275 readpage: ramfs_readpage,
276 writepage: fail_writepage,
277 prepare_write: ramfs_prepare_write,
278 commit_write: ramfs_commit_write
281 static struct file_operations ramfs_file_operations = {
282 read: generic_file_read,
283 write: generic_file_write,
284 mmap: generic_file_mmap,
285 fsync: ramfs_sync_file,
288 static struct inode_operations ramfs_dir_inode_operations = {
289 create: ramfs_create,
290 lookup: ramfs_lookup,
292 unlink: ramfs_unlink,
293 symlink: ramfs_symlink,
297 rename: ramfs_rename,
300 static struct super_operations ramfs_ops = {
301 statfs: ramfs_statfs,
302 put_inode: force_delete,
305 static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
307 struct inode * inode;
308 struct dentry * root;
310 sb->s_blocksize = PAGE_CACHE_SIZE;
311 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
312 sb->s_magic = RAMFS_MAGIC;
313 sb->s_op = &ramfs_ops;
314 inode = ramfs_get_inode(sb, S_IFDIR | 0755, 0);
318 root = d_alloc_root(inode);
327 static DECLARE_FSTYPE(ramfs_fs_type, "ramfs", ramfs_read_super, FS_LITTER);
328 static DECLARE_FSTYPE(rootfs_fs_type, "rootfs", ramfs_read_super, FS_NOMOUNT|FS_LITTER);
330 static int __init init_ramfs_fs(void)
332 return register_filesystem(&ramfs_fs_type);
335 static void __exit exit_ramfs_fs(void)
337 unregister_filesystem(&ramfs_fs_type);
340 module_init(init_ramfs_fs)
341 module_exit(exit_ramfs_fs)
343 int __init init_rootfs(void)
345 return register_filesystem(&rootfs_fs_type);
348 MODULE_LICENSE("GPL");