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 return 0;
85 }
86
87 static int ramfs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
88 {
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);
94 }
95 SetPageDirty(page);
96 return 0;
97 }
98
99 static int ramfs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
100 {
101 struct inode *inode = page->mapping->host;
102 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
103
104 kunmap(page);
105 if (pos > inode->i_size)
106 inode->i_size = pos;
107 return 0;
108 }
109
110 struct inode *ramfs_get_inode(struct super_block *sb, int mode, int dev)
111 {
112 struct inode * inode = new_inode(sb);
113
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;
137 }
138 }
139 return inode;
140 }
141
142 /*
143 * File creation. Allocate an inode, and we're done..
144 */
145 static int ramfs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev)
146 {
147 struct inode * inode = ramfs_get_inode(dir->i_sb, mode, dev);
148 int error = -ENOSPC;
149
150 if (inode) {
151 d_instantiate(dentry, inode);
152 dget(dentry); /* Extra count - pin the dentry in core */
153 error = 0;
154 }
155 return error;
156 }
157
158 static int ramfs_mkdir(struct inode * dir, struct dentry * dentry, int mode)
159 {
160 return ramfs_mknod(dir, dentry, mode | S_IFDIR, 0);
161 }
162
163 static int ramfs_create(struct inode *dir, struct dentry *dentry, int mode)
164 {
165 return ramfs_mknod(dir, dentry, mode | S_IFREG, 0);
166 }
167
168 /*
169 * Link a file..
170 */
171 static int ramfs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry)
172 {
173 struct inode *inode = old_dentry->d_inode;
174
175 if (S_ISDIR(inode->i_mode))
176 return -EPERM;
177
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;
183 }
184
185 static inline int ramfs_positive(struct dentry *dentry)
186 {
187 return dentry->d_inode && !d_unhashed(dentry);
188 }
189
190 /*
191 * Check that a directory is empty (this works
192 * for regular files too, they'll just always be
193 * considered empty..).
194 *
195 * Note that an empty directory can still have
196 * children, they just all have to be negative..
197 */
198 static int ramfs_empty(struct dentry *dentry)
199 {
200 struct list_head *list;
201
202 spin_lock(&dcache_lock);
203 list = dentry->d_subdirs.next;
204
205 while (list != &dentry->d_subdirs) {
206 struct dentry *de = list_entry(list, struct dentry, d_child);
207
208 if (ramfs_positive(de)) {
209 spin_unlock(&dcache_lock);
210 return 0;
211 }
212 list = list->next;
213 }
214 spin_unlock(&dcache_lock);
215 return 1;
216 }
217
218 /*
219 * This works for both directories and regular files.
220 * (non-directories will always have empty subdirs)
221 */
222 static int ramfs_unlink(struct inode * dir, struct dentry *dentry)
223 {
224 int retval = -ENOTEMPTY;
225
226 if (ramfs_empty(dentry)) {
227 struct inode *inode = dentry->d_inode;
228
229 inode->i_nlink--;
230 dput(dentry); /* Undo the count from "create" - this does all the work */
231 retval = 0;
232 }
233 return retval;
234 }
235
236 #define ramfs_rmdir ramfs_unlink
237
238 /*
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.
243 */
244 static int ramfs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry)
245 {
246 int error = -ENOTEMPTY;
247
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);
253 }
254 error = 0;
255 }
256 return error;
257 }
258
259 static int ramfs_symlink(struct inode * dir, struct dentry *dentry, const char * symname)
260 {
261 int error;
262
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);
268 }
269 return error;
270 }
271
272 static int ramfs_sync_file(struct file * file, struct dentry *dentry, int datasync)
273 {
274 return 0;
275 }
276
277 static struct address_space_operations ramfs_aops = {
278 readpage: ramfs_readpage,
279 writepage: ramfs_writepage,
280 prepare_write: ramfs_prepare_write,
281 commit_write: ramfs_commit_write
282 };
283
284 static struct file_operations ramfs_file_operations = {
285 read: generic_file_read,
286 write: generic_file_write,
287 mmap: generic_file_mmap,
288 fsync: ramfs_sync_file,
289 };
290
291 static struct file_operations ramfs_dir_operations = {
292 read: generic_read_dir,
293 readdir: dcache_readdir,
294 fsync: ramfs_sync_file,
295 };
296
297 static struct inode_operations ramfs_dir_inode_operations = {
298 create: ramfs_create,
299 lookup: ramfs_lookup,
300 link: ramfs_link,
301 unlink: ramfs_unlink,
302 symlink: ramfs_symlink,
303 mkdir: ramfs_mkdir,
304 rmdir: ramfs_rmdir,
305 mknod: ramfs_mknod,
306 rename: ramfs_rename,
307 };
308
309 static struct super_operations ramfs_ops = {
310 statfs: ramfs_statfs,
311 put_inode: force_delete,
312 };
313
314 static struct super_block *ramfs_read_super(struct super_block * sb, void * data, int silent)
315 {
316 struct inode * inode;
317 struct dentry * root;
318
319 sb->s_blocksize = PAGE_CACHE_SIZE;
320 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
321 sb->s_magic = RAMFS_MAGIC;
322 sb->s_op = &ramfs_ops;
323 inode = ramfs_get_inode(sb, S_IFDIR | 0755, 0);
324 if (!inode)
325 return NULL;
326
327 root = d_alloc_root(inode);
328 if (!root) {
329 iput(inode);
330 return NULL;
331 }
332 sb->s_root = root;
333 return sb;
334 }
335
336 static DECLARE_FSTYPE(ramfs_fs_type, "ramfs", ramfs_read_super, FS_LITTER);
337
338 static int __init init_ramfs_fs(void)
339 {
340 return register_filesystem(&ramfs_fs_type);
341 }
342
343 static void __exit exit_ramfs_fs(void)
344 {
345 unregister_filesystem(&ramfs_fs_type);
346 }
347
348 module_init(init_ramfs_fs)
349 module_exit(exit_ramfs_fs)
350
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.