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;
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)
62 * Read a page. Again trivial. If it didn't already exist
63 * in the page cache, it is zero-filled.
65 static int ramfs_readpage(struct file *file, struct page * page)
67 if (!Page_Uptodate(page)) {
68 memset(kmap(page), 0, PAGE_CACHE_SIZE);
70 flush_dcache_page(page);
71 SetPageUptodate(page);
77 static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
79 void *addr = kmap(page);
80 if (!Page_Uptodate(page)) {
81 memset(addr, 0, PAGE_CACHE_SIZE);
82 flush_dcache_page(page);
83 SetPageUptodate(page);
89 static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
91 struct inode *inode = page->mapping->host;
92 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
95 if (pos > inode->i_size)
100 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
102 struct inode * inode = new_inode(sb);
105 inode->i_mode = mode;
106 inode->i_uid = current->fsuid;
107 inode->i_gid = current->fsgid;
108 inode->i_blksize = PAGE_CACHE_SIZE;
110 inode->i_rdev = NODEV;
111 inode->i_mapping->a_ops = &ramfs_aops;
112 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
113 switch (mode & S_IFMT) {
115 init_special_inode(inode, mode, dev);
118 inode->i_fop = &ramfs_file_operations;
121 inode->i_op = &ramfs_dir_inode_operations;
122 inode->i_fop = &dcache_dir_ops;
125 inode->i_op = &page_symlink_inode_operations;
133 * File creation. Allocate an inode, and we're done..
135 static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
137 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
141 d_instantiate(dentry, inode);
142 dget(dentry); /* Extra count - pin the dentry in core */
148 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
150 return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
153 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
155 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
161 static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
163 struct inode *inode = old_dentry->d_inode;
165 if (S_ISDIR(inode->i_mode))
169 atomic_inc(&inode->i_count); /* New dentry reference */
170 dget(dentry); /* Extra pinning count for the created dentry */
171 d_instantiate(dentry, inode);
175 static inline int ramfs_positive(struct dentry *dentry)
177 return dentry->d_inode && !d_unhashed(dentry);
181 * Check that a directory is empty (this works
182 * for regular files too, they'll just always be
183 * considered empty..).
185 * Note that an empty directory can still have
186 * children, they just all have to be negative..
188 static int ramfs_empty(struct dentry *dentry)
190 struct list_head *list;
192 spin_lock(&dcache_lock);
193 list = dentry->d_subdirs.next;
195 while (list != &dentry->d_subdirs) {
196 struct dentry *de = list_entry(list, struct dentry, d_child);
198 if (ramfs_positive(de)) {
199 spin_unlock(&dcache_lock);
204 spin_unlock(&dcache_lock);
209 * This works for both directories and regular files.
210 * (non-directories will always have empty subdirs)
212 static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
214 int retval = -ENOTEMPTY;
216 if (ramfs_empty(dentry)) {
217 struct inode *inode = dentry->d_inode;
220 dput(dentry); /* Undo the count from "create" - this does all the work */
226 #define ramfs_rmdir ramfs_unlink
229 * The VFS layer already does all the dentry stuff for rename,
230 * we just have to decrement the usage count for the target if
231 * it exists so that the VFS layer correctly free's it when it
234 static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
236 int error = -ENOTEMPTY;
238 if (ramfs_empty(new_dentry)) {
239 struct inode *inode = new_dentry->d_inode;
249 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
253 error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
255 int l = strlen(symname)+1;
256 struct inode *inode = dentry->d_inode;
257 error = block_symlink(inode, symname, l);
262 static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
267 static struct address_space_operations ramfs_aops = {
268 readpage: ramfs_readpage,
269 writepage: fail_writepage,
270 prepare_write: ramfs_prepare_write,
271 commit_write: ramfs_commit_write
274 static struct file_operations ramfs_file_operations = {
275 read: generic_file_read,
276 write: generic_file_write,
277 mmap: generic_file_mmap,
278 fsync: ramfs_sync_file,
281 static struct inode_operations ramfs_dir_inode_operations = {
282 create: ramfs_create,
283 lookup: ramfs_lookup,
285 unlink: ramfs_unlink,
286 symlink: ramfs_symlink,
290 rename: ramfs_rename,
293 static struct super_operations ramfs_ops = {
294 statfs: ramfs_statfs,
295 put_inode: force_delete,
298 static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
300 struct inode * inode;
301 struct dentry * root;
303 sb->s_blocksize = PAGE_CACHE_SIZE;
304 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
305 sb->s_magic = RAMFS_MAGIC;
306 sb->s_op = &ramfs_ops;
307 inode = ramfs_get_inode(sb, S_IFDIR | 0755, 0);
311 root = d_alloc_root(inode);
320 static DECLARE_FSTYPE(ramfs_fs_type, "ramfs", ramfs_read_super, FS_LITTER);
321 static DECLARE_FSTYPE(rootfs_fs_type, "rootfs", ramfs_read_super, FS_NOMOUNT|FS_LITTER);
323 static int __init init_ramfs_fs(void)
325 return register_filesystem(&ramfs_fs_type);
328 static void __exit exit_ramfs_fs(void)
330 unregister_filesystem(&ramfs_fs_type);
333 module_init(init_ramfs_fs)
334 module_exit(exit_ramfs_fs)
336 int __init init_rootfs(void)
338 return register_filesystem(&rootfs_fs_type);
341 MODULE_LICENSE("GPL");