- Linus: drop support for old-style Makefiles entirely. Big.
[davej-history.git] / fs / ramfs / inode.c
blob36fa933a499bf2aae534dc2158bea848a74bb4ac
1 /*
2 * Resizable simple ram filesystem for Linux.
4 * Copyright (C) 2000 Linus Torvalds.
5 * 2000 Transmeta Corp.
7 * This file is released under the GPL.
8 */
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>
26 #include <linux/fs.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;
47 buf->f_namelen = 255;
48 return 0;
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 d_add(dentry, NULL);
58 return NULL;
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);
69 kunmap(page);
70 flush_dcache_page(page);
71 SetPageUptodate(page);
73 UnlockPage(page);
74 return 0;
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)
83 SetPageDirty(page);
84 return 0;
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);
95 SetPageDirty(page);
96 return 0;
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;
104 kunmap(page);
105 if (pos > inode->i_size)
106 inode->i_size = pos;
107 return 0;
110 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
112 struct inode * inode = new_inode(sb);
114 if (inode) {
115 inode->i_mode = mode;
116 inode->i_uid = current->fsuid;
117 inode->i_gid = current->fsgid;
118 inode->i_blksize = PAGE_CACHE_SIZE;
119 inode->i_blocks = 0;
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) {
124 default:
125 init_special_inode(inode, mode, dev);
126 break;
127 case S_IFREG:
128 inode->i_fop = &ramfs_file_operations;
129 break;
130 case S_IFDIR:
131 inode->i_op = &ramfs_dir_inode_operations;
132 inode->i_fop = &ramfs_dir_operations;
133 break;
134 case S_IFLNK:
135 inode->i_op = &page_symlink_inode_operations;
136 break;
139 return inode;
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);
148 int error = -ENOSPC;
150 if (inode) {
151 d_instantiate(dentry, inode);
152 dget(dentry); /* Extra count - pin the dentry in core */
153 error = 0;
155 return error;
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);
169 * Link a file..
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))
176 return -EPERM;
178 inode->i_nlink++;
179 atomic_inc(&inode->i_count); /* New dentry reference */
180 dget(dentry); /* Extra pinning count for the created dentry */
181 d_instantiate(dentry, inode);
182 return 0;
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);
210 return 0;
212 list = list->next;
214 spin_unlock(&dcache_lock);
215 return 1;
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;
229 inode->i_nlink--;
230 dput(dentry); /* Undo the count from "create" - this does all the work */
231 retval = 0;
233 return retval;
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
242 * gets overwritten.
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;
250 if (inode) {
251 inode->i_nlink--;
252 dput(new_dentry);
254 error = 0;
256 return error;
259 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
261 int error;
263 error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
264 if (!error) {
265 int l = strlen(symname)+1;
266 struct inode *inode = dentry->d_inode;
267 error = block_symlink(inode, symname, l);
269 return error;
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,
293 link: ramfs_link,
294 unlink: ramfs_unlink,
295 symlink: ramfs_symlink,
296 mkdir: ramfs_mkdir,
297 rmdir: ramfs_rmdir,
298 mknod: ramfs_mknod,
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);
317 if (!inode)
318 return NULL;
320 root = d_alloc_root(inode);
321 if (!root) {
322 iput(inode);
323 return NULL;
325 sb->s_root = root;
326 return sb;
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)