import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / 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  * Usage limits added by David Gibson, Linuxcare Australia.
8  * This file is released under the GPL.
9  */
10
11 /*
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.
15  *
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.
19  *
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.
24  */
25
26 #include <linux/module.h>
27 #include <linux/fs.h>
28 #include <linux/pagemap.h>
29 #include <linux/init.h>
30 #include <linux/string.h>
31 #include <linux/locks.h>
32
33 #include <asm/uaccess.h>
34
35 /* some random number */
36 #define RAMFS_MAGIC     0x858458f6
37
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;
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 = NAME_MAX;
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         if (dentry->d_name.len > NAME_MAX)
58                 return ERR_PTR(-ENAMETOOLONG);
59         d_add(dentry, NULL);
60         return NULL;
61 }
62
63 /*
64  * Read a page. Again trivial. If it didn't already exist
65  * in the page cache, it is zero-filled.
66  */
67 static int ramfs_readpage(struct file *file, struct page * page)
68 {
69         if (!Page_Uptodate(page)) {
70                 memset(kmap(page), 0, PAGE_CACHE_SIZE);
71                 kunmap(page);
72                 flush_dcache_page(page);
73                 SetPageUptodate(page);
74         }
75         UnlockPage(page);
76         return 0;
77 }
78
79 static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
80 {
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);
86         }
87         SetPageDirty(page);
88         return 0;
89 }
90
91 static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
92 {
93         struct inode *inode = page->mapping->host;
94         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
95
96         kunmap(page);
97         if (pos > inode->i_size)
98                 inode->i_size = pos;
99         return 0;
100 }
101
102 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
103 {
104         struct inode * inode = new_inode(sb);
105
106         if (inode) {
107                 inode->i_mode = mode;
108                 inode->i_uid = current->fsuid;
109                 inode->i_gid = current->fsgid;
110                 inode->i_blksize = PAGE_CACHE_SIZE;
111                 inode->i_blocks = 0;
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) {
116                 default:
117                         init_special_inode(inode, mode, dev);
118                         break;
119                 case S_IFREG:
120                         inode->i_fop = &ramfs_file_operations;
121                         break;
122                 case S_IFDIR:
123                         inode->i_op = &ramfs_dir_inode_operations;
124                         inode->i_fop = &dcache_dir_ops;
125                         break;
126                 case S_IFLNK:
127                         inode->i_op = &page_symlink_inode_operations;
128                         break;
129                 }
130         }
131         return inode;
132 }
133
134 /*
135  * File creation. Allocate an inode, and we're done..
136  */
137 static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
138 {
139         struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
140         int error = -ENOSPC;
141
142         if (inode) {
143                 if (dir->i_mode & S_ISGID) {
144                         inode->i_gid = dir->i_gid;
145                         if (S_ISDIR(mode))
146                                 inode->i_mode |= S_ISGID;
147                 }
148                 d_instantiate(dentry, inode);
149                 dget(dentry);           /* Extra count - pin the dentry in core */
150                 error = 0;
151         }
152         return error;
153 }
154
155 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
156 {
157         return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
158 }
159
160 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
161 {
162         return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
163 }
164
165 /*
166  * Link a file..
167  */
168 static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
169 {
170         struct inode *inode = old_dentry->d_inode;
171
172         if (S_ISDIR(inode->i_mode))
173                 return -EPERM;
174
175         inode->i_nlink++;
176         atomic_inc(&inode->i_count);    /* New dentry reference */
177         dget(dentry);           /* Extra pinning count for the created dentry */
178         d_instantiate(dentry, inode);
179         return 0;
180 }
181
182 static inline int ramfs_positive(struct dentry *dentry)
183 {
184         return dentry->d_inode && !d_unhashed(dentry);
185 }
186
187 /*
188  * Check that a directory is empty (this works
189  * for regular files too, they'll just always be
190  * considered empty..).
191  *
192  * Note that an empty directory can still have
193  * children, they just all have to be negative..
194  */
195 static int ramfs_empty(struct dentry *dentry)
196 {
197         struct list_head *list;
198
199         spin_lock(&dcache_lock);
200         list = dentry->d_subdirs.next;
201
202         while (list != &dentry->d_subdirs) {
203                 struct dentry *de = list_entry(list, struct dentry, d_child);
204
205                 if (ramfs_positive(de)) {
206                         spin_unlock(&dcache_lock);
207                         return 0;
208                 }
209                 list = list->next;
210         }
211         spin_unlock(&dcache_lock);
212         return 1;
213 }
214
215 /*
216  * This works for both directories and regular files.
217  * (non-directories will always have empty subdirs)
218  */
219 static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
220 {
221         int retval = -ENOTEMPTY;
222
223         if (ramfs_empty(dentry)) {
224                 struct inode *inode = dentry->d_inode;
225
226                 inode->i_nlink--;
227                 dput(dentry);                   /* Undo the count from "create" - this does all the work */
228                 retval = 0;
229         }
230         return retval;
231 }
232
233 #define ramfs_rmdir ramfs_unlink
234
235 /*
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
239  * gets overwritten.
240  */
241 static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
242 {
243         int error = -ENOTEMPTY;
244
245         if (ramfs_empty(new_dentry)) {
246                 struct inode *inode = new_dentry->d_inode;
247                 if (inode) {
248                         inode->i_nlink--;
249                         dput(new_dentry);
250                 }
251                 error = 0;
252         }
253         return error;
254 }
255
256 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
257 {
258         int error;
259
260         error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
261         if (!error) {
262                 int l = strlen(symname)+1;
263                 struct inode *inode = dentry->d_inode;
264                 error = block_symlink(inode, symname, l);
265         }
266         return error;
267 }
268
269 static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
270 {
271         return 0;
272 }
273
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
279 };
280
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,
286 };
287
288 static struct inode_operations ramfs_dir_inode_operations = {
289         create:         ramfs_create,
290         lookup:         ramfs_lookup,
291         link:           ramfs_link,
292         unlink:         ramfs_unlink,
293         symlink:        ramfs_symlink,
294         mkdir:          ramfs_mkdir,
295         rmdir:          ramfs_rmdir,
296         mknod:          ramfs_mknod,
297         rename:         ramfs_rename,
298 };
299
300 static struct super_operations ramfs_ops = {
301         statfs:         ramfs_statfs,
302         put_inode:      force_delete,
303 };
304
305 static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
306 {
307         struct inode * inode;
308         struct dentry * root;
309
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);
315         if (!inode)
316                 return NULL;
317
318         root = d_alloc_root(inode);
319         if (!root) {
320                 iput(inode);
321                 return NULL;
322         }
323         sb->s_root = root;
324         return sb;
325 }
326
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);
329
330 static int __init init_ramfs_fs(void)
331 {
332         return register_filesystem(&ramfs_fs_type);
333 }
334
335 static void __exit exit_ramfs_fs(void)
336 {
337         unregister_filesystem(&ramfs_fs_type);
338 }
339
340 module_init(init_ramfs_fs)
341 module_exit(exit_ramfs_fs)
342
343 int __init init_rootfs(void)
344 {
345         return register_filesystem(&rootfs_fs_type);
346 }
347
348 MODULE_LICENSE("GPL");
349