FreeBSD/Linux Kernel Cross Reference
sys/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 = 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 static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
78 {
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);
84 }
85 SetPageDirty(page);
86 return 0;
87 }
88
89 static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
90 {
91 struct inode *inode = page->mapping->host;
92 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
93
94 kunmap(page);
95 if (pos > inode->i_size)
96 inode->i_size = pos;
97 return 0;
98 }
99
100 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
101 {
102 struct inode * inode = new_inode(sb);
103
104 if (inode) {
105 inode->i_mode = mode;
106 inode->i_uid = current->fsuid;
107 inode->i_gid = current->fsgid;
108 inode->i_blksize = PAGE_CACHE_SIZE;
109 inode->i_blocks = 0;
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) {
114 default:
115 init_special_inode(inode, mode, dev);
116 break;
117 case S_IFREG:
118 inode->i_fop = &ramfs_file_operations;
119 break;
120 case S_IFDIR:
121 inode->i_op = &ramfs_dir_inode_operations;
122 inode->i_fop = &dcache_dir_ops;
123 break;
124 case S_IFLNK:
125 inode->i_op = &page_symlink_inode_operations;
126 break;
127 }
128 }
129 return inode;
130 }
131
132 /*
133 * File creation. Allocate an inode, and we're done..
134 */
135 static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
136 {
137 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
138 int error = -ENOSPC;
139
140 if (inode) {
141 d_instantiate(dentry, inode);
142 dget(dentry); /* Extra count - pin the dentry in core */
143 error = 0;
144 }
145 return error;
146 }
147
148 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
149 {
150 return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
151 }
152
153 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
154 {
155 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
156 }
157
158 /*
159 * Link a file..
160 */
161 static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
162 {
163 struct inode *inode = old_dentry->d_inode;
164
165 if (S_ISDIR(inode->i_mode))
166 return -EPERM;
167
168 inode->i_nlink++;
169 atomic_inc(&inode->i_count); /* New dentry reference */
170 dget(dentry); /* Extra pinning count for the created dentry */
171 d_instantiate(dentry, inode);
172 return 0;
173 }
174
175 static inline int ramfs_positive(struct dentry *dentry)
176 {
177 return dentry->d_inode && !d_unhashed(dentry);
178 }
179
180 /*
181 * Check that a directory is empty (this works
182 * for regular files too, they'll just always be
183 * considered empty..).
184 *
185 * Note that an empty directory can still have
186 * children, they just all have to be negative..
187 */
188 static int ramfs_empty(struct dentry *dentry)
189 {
190 struct list_head *list;
191
192 spin_lock(&dcache_lock);
193 list = dentry->d_subdirs.next;
194
195 while (list != &dentry->d_subdirs) {
196 struct dentry *de = list_entry(list, struct dentry, d_child);
197
198 if (ramfs_positive(de)) {
199 spin_unlock(&dcache_lock);
200 return 0;
201 }
202 list = list->next;
203 }
204 spin_unlock(&dcache_lock);
205 return 1;
206 }
207
208 /*
209 * This works for both directories and regular files.
210 * (non-directories will always have empty subdirs)
211 */
212 static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
213 {
214 int retval = -ENOTEMPTY;
215
216 if (ramfs_empty(dentry)) {
217 struct inode *inode = dentry->d_inode;
218
219 inode->i_nlink--;
220 dput(dentry); /* Undo the count from "create" - this does all the work */
221 retval = 0;
222 }
223 return retval;
224 }
225
226 #define ramfs_rmdir ramfs_unlink
227
228 /*
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
232 * gets overwritten.
233 */
234 static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
235 {
236 int error = -ENOTEMPTY;
237
238 if (ramfs_empty(new_dentry)) {
239 struct inode *inode = new_dentry->d_inode;
240 if (inode) {
241 inode->i_nlink--;
242 dput(new_dentry);
243 }
244 error = 0;
245 }
246 return error;
247 }
248
249 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
250 {
251 int error;
252
253 error = ramfs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0);
254 if (!error) {
255 int l = strlen(symname)+1;
256 struct inode *inode = dentry->d_inode;
257 error = block_symlink(inode, symname, l);
258 }
259 return error;
260 }
261
262 static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
263 {
264 return 0;
265 }
266
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
272 };
273
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,
279 };
280
281 static struct inode_operations ramfs_dir_inode_operations = {
282 create: ramfs_create,
283 lookup: ramfs_lookup,
284 link: ramfs_link,
285 unlink: ramfs_unlink,
286 symlink: ramfs_symlink,
287 mkdir: ramfs_mkdir,
288 rmdir: ramfs_rmdir,
289 mknod: ramfs_mknod,
290 rename: ramfs_rename,
291 };
292
293 static struct super_operations ramfs_ops = {
294 statfs: ramfs_statfs,
295 put_inode: force_delete,
296 };
297
298 static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
299 {
300 struct inode * inode;
301 struct dentry * root;
302
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);
308 if (!inode)
309 return NULL;
310
311 root = d_alloc_root(inode);
312 if (!root) {
313 iput(inode);
314 return NULL;
315 }
316 sb->s_root = root;
317 return sb;
318 }
319
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);
322
323 static int __init init_ramfs_fs(void)
324 {
325 return register_filesystem(&ramfs_fs_type);
326 }
327
328 static void __exit exit_ramfs_fs(void)
329 {
330 unregister_filesystem(&ramfs_fs_type);
331 }
332
333 module_init(init_ramfs_fs)
334 module_exit(exit_ramfs_fs)
335
336 int __init init_rootfs(void)
337 {
338 return register_filesystem(&rootfs_fs_type);
339 }
340
341 MODULE_LICENSE("GPL");
342
Cache object: 938fd8c12d2cf5bb7419db171925f00d
|