2 * Resizable simple ram filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
7 * This file is released under the GPL.
11 * NOTE! This filesystem is probably most useful
12 * not as a real filesystem, but as an example of
13 * how virtual filesystems can be written.
15 * It doesn't get much simpler than this. Consider
16 * that this file implements the full semantics of
17 * a POSIX-compliant read-write filesystem.
19 * Note in particular how the filesystem does not
20 * need to implement any data structures of its own
21 * to keep track of the virtual data: using the VFS
22 * caches is sufficient.
25 #include <linux/module.h>
27 #include <linux/pagemap.h>
28 #include <linux/init.h>
29 #include <linux/string.h>
30 #include <linux/locks.h>
32 #include <asm/uaccess.h>
34 /* some random number */
35 #define RAMFS_MAGIC 0x858458f6
37 static struct super_operations ramfs_ops
;
38 static struct address_space_operations ramfs_aops
;
39 static struct file_operations ramfs_dir_operations
;
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
);
78 * Writing: just make sure the page gets marked dirty, so that
79 * the page stealer won't grab it.
81 static int ramfs_writepage(struct page
*page
)
87 static int ramfs_prepare_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
89 void *addr
= kmap(page
);
90 if (!Page_Uptodate(page
)) {
91 memset(addr
, 0, PAGE_CACHE_SIZE
);
92 flush_dcache_page(page
);
93 SetPageUptodate(page
);
99 static int ramfs_commit_write(struct file
*file
, struct page
*page
, unsigned offset
, unsigned to
)
101 struct inode
*inode
= (struct inode
*)page
->mapping
->host
;
102 loff_t pos
= ((loff_t
)page
->index
<< PAGE_CACHE_SHIFT
) + to
;
105 if (pos
> inode
->i_size
)
110 struct inode
*ramfs_get_inode(struct super_block
*sb
, int mode
, int dev
)
112 struct inode
* inode
= new_inode(sb
);
115 inode
->i_mode
= mode
;
116 inode
->i_uid
= current
->fsuid
;
117 inode
->i_gid
= current
->fsgid
;
118 inode
->i_blksize
= PAGE_CACHE_SIZE
;
120 inode
->i_rdev
= to_kdev_t(dev
);
121 inode
->i_mapping
->a_ops
= &ramfs_aops
;
122 inode
->i_atime
= inode
->i_mtime
= inode
->i_ctime
= CURRENT_TIME
;
123 switch (mode
& S_IFMT
) {
125 init_special_inode(inode
, mode
, dev
);
128 inode
->i_fop
= &ramfs_file_operations
;
131 inode
->i_op
= &ramfs_dir_inode_operations
;
132 inode
->i_fop
= &ramfs_dir_operations
;
135 inode
->i_op
= &page_symlink_inode_operations
;
143 * File creation. Allocate an inode, and we're done..
145 static int ramfs_mknod(struct inode
*dir
, struct dentry
*dentry
, int mode
, int dev
)
147 struct inode
* inode
= ramfs_get_inode(dir
->i_sb
, mode
, dev
);
151 d_instantiate(dentry
, inode
);
152 dget(dentry
); /* Extra count - pin the dentry in core */
158 static int ramfs_mkdir(struct inode
* dir
, struct dentry
* dentry
, int mode
)
160 return ramfs_mknod(dir
, dentry
, mode
| S_IFDIR
, 0);
163 static int ramfs_create(struct inode
*dir
, struct dentry
*dentry
, int mode
)
165 return ramfs_mknod(dir
, dentry
, mode
| S_IFREG
, 0);
171 static int ramfs_link(struct dentry
*old_dentry
, struct inode
* dir
, struct dentry
* dentry
)
173 struct inode
*inode
= old_dentry
->d_inode
;
175 if (S_ISDIR(inode
->i_mode
))
179 atomic_inc(&inode
->i_count
); /* New dentry reference */
180 dget(dentry
); /* Extra pinning count for the created dentry */
181 d_instantiate(dentry
, inode
);
185 static inline int ramfs_positive(struct dentry
*dentry
)
187 return dentry
->d_inode
&& !d_unhashed(dentry
);
191 * Check that a directory is empty (this works
192 * for regular files too, they'll just always be
193 * considered empty..).
195 * Note that an empty directory can still have
196 * children, they just all have to be negative..
198 static int ramfs_empty(struct dentry
*dentry
)
200 struct list_head
*list
;
202 spin_lock(&dcache_lock
);
203 list
= dentry
->d_subdirs
.next
;
205 while (list
!= &dentry
->d_subdirs
) {
206 struct dentry
*de
= list_entry(list
, struct dentry
, d_child
);
208 if (ramfs_positive(de
)) {
209 spin_unlock(&dcache_lock
);
214 spin_unlock(&dcache_lock
);
219 * This works for both directories and regular files.
220 * (non-directories will always have empty subdirs)
222 static int ramfs_unlink(struct inode
* dir
, struct dentry
*dentry
)
224 int retval
= -ENOTEMPTY
;
226 if (ramfs_empty(dentry
)) {
227 struct inode
*inode
= dentry
->d_inode
;
230 dput(dentry
); /* Undo the count from "create" - this does all the work */
236 #define ramfs_rmdir ramfs_unlink
239 * The VFS layer already does all the dentry stuff for rename,
240 * we just have to decrement the usage count for the target if
241 * it exists so that the VFS layer correctly free's it when it
244 static int ramfs_rename(struct inode
* old_dir
, struct dentry
*old_dentry
, struct inode
* new_dir
,struct dentry
*new_dentry
)
246 int error
= -ENOTEMPTY
;
248 if (ramfs_empty(new_dentry
)) {
249 struct inode
*inode
= new_dentry
->d_inode
;
259 static int ramfs_symlink(struct inode
* dir
, struct dentry
*dentry
, const char * symname
)
263 error
= ramfs_mknod(dir
, dentry
, S_IFLNK
| S_IRWXUGO
, 0);
265 int l
= strlen(symname
)+1;
266 struct inode
*inode
= dentry
->d_inode
;
267 error
= block_symlink(inode
, symname
, l
);
272 static struct address_space_operations ramfs_aops
= {
273 readpage
: ramfs_readpage
,
274 writepage
: ramfs_writepage
,
275 prepare_write
: ramfs_prepare_write
,
276 commit_write
: ramfs_commit_write
279 static struct file_operations ramfs_file_operations
= {
280 read
: generic_file_read
,
281 write
: generic_file_write
,
282 mmap
: generic_file_mmap
285 static struct file_operations ramfs_dir_operations
= {
286 read
: generic_read_dir
,
287 readdir
: dcache_readdir
,
290 static struct inode_operations ramfs_dir_inode_operations
= {
291 create
: ramfs_create
,
292 lookup
: ramfs_lookup
,
294 unlink
: ramfs_unlink
,
295 symlink
: ramfs_symlink
,
299 rename
: ramfs_rename
,
302 static struct super_operations ramfs_ops
= {
303 statfs
: ramfs_statfs
,
304 put_inode
: force_delete
,
307 static struct super_block
*ramfs_read_super(struct super_block
* sb
, void * data
, int silent
)
309 struct inode
* inode
;
310 struct dentry
* root
;
312 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
313 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
314 sb
->s_magic
= RAMFS_MAGIC
;
315 sb
->s_op
= &ramfs_ops
;
316 inode
= ramfs_get_inode(sb
, S_IFDIR
| 0755, 0);
320 root
= d_alloc_root(inode
);
329 static DECLARE_FSTYPE(ramfs_fs_type
, "ramfs", ramfs_read_super
, FS_LITTER
);
331 static int __init
init_ramfs_fs(void)
333 return register_filesystem(&ramfs_fs_type
);
336 static void __exit
exit_ramfs_fs(void)
338 unregister_filesystem(&ramfs_fs_type
);
341 module_init(init_ramfs_fs
)
342 module_exit(exit_ramfs_fs
)