File: /usr/src/linux/fs/ramfs/inode.c

1     /*
2      * Resizable simple ram filesystem for Linux.
3      *
4      * Copyright (C) 2000 Linus Torvalds.
5      *               2000 Transmeta Corp.
6      *
7      * This file is released under the GPL.
8      */
9     
10     /*
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.
14      *
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.
18      *
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.
23      */
24     
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>
31     
32     #include <asm/uaccess.h>
33     
34     /* some random number */
35     #define RAMFS_MAGIC	0x858458f6
36     
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;
42     
43     static int ramfs_statfs(struct super_block *sb, struct statfs *buf)
44     {
45     	buf->f_type = RAMFS_MAGIC;
46     	buf->f_bsize = PAGE_CACHE_SIZE;
47     	buf->f_namelen = 255;
48     	return 0;
49     }
50     
51     /*
52      * Lookup the data. This is trivial - if the dentry didn't already
53      * exist, we know it is negative.
54      */
55     static struct dentry * ramfs_lookup(struct inode *dir, struct dentry *dentry)
56     {
57     	d_add(dentry, NULL);
58     	return NULL;
59     }
60     
61     /*
62      * Read a page. Again trivial. If it didn't already exist
63      * in the page cache, it is zero-filled.
64      */
65     static int ramfs_readpage(struct file *file, struct page * page)
66     {
67     	if (!Page_Uptodate(page)) {
68     		memset(kmap(page), 0, PAGE_CACHE_SIZE);
69     		kunmap(page);
70     		flush_dcache_page(page);
71     		SetPageUptodate(page);
72     	}
73     	UnlockPage(page);
74     	return 0;
75     }
76     
77     /*
78      * Writing: just make sure the page gets marked dirty, so that
79      * the page stealer won't grab it.
80      */
81     static int ramfs_writepage(struct page *page)
82     {
83     	SetPageDirty(page);
84     	UnlockPage(page);
85     	return 0;
86     }
87     
88     static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
89     {
90     	void *addr = kmap(page);
91     	if (!Page_Uptodate(page)) {
92     		memset(addr, 0, PAGE_CACHE_SIZE);
93     		flush_dcache_page(page);
94     		SetPageUptodate(page);
95     	}
96     	SetPageDirty(page);
97     	return 0;
98     }
99     
100     static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
101     {
102     	struct inode *inode = page->mapping->host;
103     	loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
104     
105     	kunmap(page);
106     	if (pos > inode->i_size)
107     		inode->i_size = pos;
108     	return 0;
109     }
110     
111     struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
112     {
113     	struct inode * inode = new_inode(sb);
114     
115     	if (inode) {
116     		inode->i_mode = mode;
117     		inode->i_uid = current->fsuid;
118     		inode->i_gid = current->fsgid;
119     		inode->i_blksize = PAGE_CACHE_SIZE;
120     		inode->i_blocks = 0;
121     		inode->i_rdev = NODEV;
122     		inode->i_mapping->a_ops = &ramfs_aops;
123     		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
124     		switch (mode & S_IFMT) {
125     		default:
126     			init_special_inode(inode, mode, dev);
127     			break;
128     		case S_IFREG:
129     			inode->i_fop = &ramfs_file_operations;
130     			break;
131     		case S_IFDIR:
132     			inode->i_op = &ramfs_dir_inode_operations;
133     			inode->i_fop = &ramfs_dir_operations;
134     			break;
135     		case S_IFLNK:
136     			inode->i_op = &page_symlink_inode_operations;
137     			break;
138     		}
139     	}
140     	return inode;
141     }
142     
143     /*
144      * File creation. Allocate an inode, and we're done..
145      */
146     static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
147     {
148     	struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
149     	int error = -ENOSPC;
150     
151     	if (inode) {
152     		d_instantiate(dentry, inode);
153     		dget(dentry);		/* Extra count - pin the dentry in core */
154     		error = 0;
155     	}
156     	return error;
157     }
158     
159     static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
160     {
161     	return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
162     }
163     
164     static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
165     {
166     	return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
167     }
168     
169     /*
170      * Link a file..
171      */
172     static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
173     {
174     	struct inode *inode = old_dentry->d_inode;
175     
176     	if (S_ISDIR(inode->i_mode))
177     		return -EPERM;
178     
179     	inode->i_nlink++;
180     	atomic_inc(&inode->i_count);	/* New dentry reference */
181     	dget(dentry);		/* Extra pinning count for the created dentry */
182     	d_instantiate(dentry, inode);
183     	return 0;
184     }
185     
186     static inline int ramfs_positive(struct dentry *dentry)
187     {
188     	return dentry->d_inode && !d_unhashed(dentry);
189     }
190     
191     /*
192      * Check that a directory is empty (this works
193      * for regular files too, they'll just always be
194      * considered empty..).
195      *
196      * Note that an empty directory can still have
197      * children, they just all have to be negative..
198      */
199     static int ramfs_empty(struct dentry *dentry)
200     {
201     	struct list_head *list;
202     
203     	spin_lock(&dcache_lock);
204     	list = dentry->d_subdirs.next;
205     
206     	while (list != &dentry->d_subdirs) {
207     		struct dentry *de = list_entry(list, struct dentry, d_child);
208     
209     		if (ramfs_positive(de)) {
210     			spin_unlock(&dcache_lock);
211     			return 0;
212     		}
213     		list = list->next;
214     	}
215     	spin_unlock(&dcache_lock);
216     	return 1;
217     }
218     
219     /*
220      * This works for both directories and regular files.
221      * (non-directories will always have empty subdirs)
222      */
223     static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
224     {
225     	int retval = -ENOTEMPTY;
226     
227     	if (ramfs_empty(dentry)) {
228     		struct inode *inode = dentry->d_inode;
229     
230     		inode->i_nlink--;
231     		dput(dentry);			/* Undo the count from "create" - this does all the work */
232     		retval = 0;
233     	}
234     	return retval;
235     }
236     
237     #define ramfs_rmdir ramfs_unlink
238     
239     /*
240      * The VFS layer already does all the dentry stuff for rename,
241      * we just have to decrement the usage count for the target if
242      * it exists so that the VFS layer correctly free's it when it
243      * gets overwritten.
244      */
245     static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
246     {
247     	int error = -ENOTEMPTY;
248     
249     	if (ramfs_empty(new_dentry)) {
250     		struct inode *inode = new_dentry->d_inode;
251     		if (inode) {
252     			inode->i_nlink--;
253     			dput(new_dentry);
254     		}
255     		error = 0;
256     	}
257     	return error;
258     }
259     
260     static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
261     {
262     	int error;
263     
264     	error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
265     	if (!error) {
266     		int l = strlen(symname)+1;
267     		struct inode *inode = dentry->d_inode;
268     		error = block_symlink(inode, symname, l);
269     	}
270     	return error;
271     }
272     
273     static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
274     {
275     	return 0;
276     }
277     
278     static struct address_space_operations ramfs_aops = {
279     	readpage:	ramfs_readpage,
280     	writepage:	ramfs_writepage,
281     	prepare_write:	ramfs_prepare_write,
282     	commit_write:	ramfs_commit_write
283     };
284     
285     static struct file_operations ramfs_file_operations = {
286     	read:		generic_file_read,
287     	write:		generic_file_write,
288     	mmap:		generic_file_mmap,
289     	fsync:		ramfs_sync_file,
290     };
291     
292     static struct file_operations ramfs_dir_operations = {
293     	read:		generic_read_dir,
294     	readdir:	dcache_readdir,
295     	fsync:		ramfs_sync_file,
296     };
297     
298     static struct inode_operations ramfs_dir_inode_operations = {
299     	create:		ramfs_create,
300     	lookup:		ramfs_lookup,
301     	link:		ramfs_link,
302     	unlink:		ramfs_unlink,
303     	symlink:	ramfs_symlink,
304     	mkdir:		ramfs_mkdir,
305     	rmdir:		ramfs_rmdir,
306     	mknod:		ramfs_mknod,
307     	rename:		ramfs_rename,
308     };
309     
310     static struct super_operations ramfs_ops = {
311     	statfs:		ramfs_statfs,
312     	put_inode:	force_delete,
313     };
314     
315     static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
316     {
317     	struct inode * inode;
318     	struct dentry * root;
319     
320     	sb->s_blocksize = PAGE_CACHE_SIZE;
321     	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
322     	sb->s_magic = RAMFS_MAGIC;
323     	sb->s_op = &ramfs_ops;
324     	inode = ramfs_get_inode(sb, S_IFDIR | 0755, 0);
325     	if (!inode)
326     		return NULL;
327     
328     	root = d_alloc_root(inode);
329     	if (!root) {
330     		iput(inode);
331     		return NULL;
332     	}
333     	sb->s_root = root;
334     	return sb;
335     }
336     
337     static DECLARE_FSTYPE(ramfs_fs_type, "ramfs", ramfs_read_super, FS_LITTER);
338     
339     static int __init init_ramfs_fs(void)
340     {
341     	return register_filesystem(&ramfs_fs_type);
342     }
343     
344     static void __exit exit_ramfs_fs(void)
345     {
346     	unregister_filesystem(&ramfs_fs_type);
347     }
348     
349     module_init(init_ramfs_fs)
350     module_exit(exit_ramfs_fs)
351