ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / libfs.c
1 /*
2  *      fs/libfs.c
3  *      Library for filesystems writers.
4  */
5
6 #include <linux/module.h>
7 #include <linux/pagemap.h>
8 #include <linux/mount.h>
9 #include <linux/vfs.h>
10
11 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
12                    struct kstat *stat)
13 {
14         struct inode *inode = dentry->d_inode;
15         generic_fillattr(inode, stat);
16         stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
17         return 0;
18 }
19
20 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
21 {
22         buf->f_type = sb->s_magic;
23         buf->f_bsize = PAGE_CACHE_SIZE;
24         buf->f_namelen = NAME_MAX;
25         return 0;
26 }
27
28 /*
29  * Lookup the data. This is trivial - if the dentry didn't already
30  * exist, we know it is negative.
31  */
32
33 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
34 {
35         if (dentry->d_name.len > NAME_MAX)
36                 return ERR_PTR(-ENAMETOOLONG);
37         d_add(dentry, NULL);
38         return NULL;
39 }
40
41 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
42 {
43         return 0;
44 }
45  
46 int dcache_dir_open(struct inode *inode, struct file *file)
47 {
48         static struct qstr cursor_name = {.len = 1, .name = "."};
49
50         file->private_data = d_alloc(file->f_dentry, &cursor_name);
51
52         return file->private_data ? 0 : -ENOMEM;
53 }
54
55 int dcache_dir_close(struct inode *inode, struct file *file)
56 {
57         dput(file->private_data);
58         return 0;
59 }
60
61 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
62 {
63         down(&file->f_dentry->d_inode->i_sem);
64         switch (origin) {
65                 case 1:
66                         offset += file->f_pos;
67                 case 0:
68                         if (offset >= 0)
69                                 break;
70                 default:
71                         up(&file->f_dentry->d_inode->i_sem);
72                         return -EINVAL;
73         }
74         if (offset != file->f_pos) {
75                 file->f_pos = offset;
76                 if (file->f_pos >= 2) {
77                         struct list_head *p;
78                         struct dentry *cursor = file->private_data;
79                         loff_t n = file->f_pos - 2;
80
81                         spin_lock(&dcache_lock);
82                         list_del(&cursor->d_child);
83                         p = file->f_dentry->d_subdirs.next;
84                         while (n && p != &file->f_dentry->d_subdirs) {
85                                 struct dentry *next;
86                                 next = list_entry(p, struct dentry, d_child);
87                                 if (!d_unhashed(next) && next->d_inode)
88                                         n--;
89                                 p = p->next;
90                         }
91                         list_add_tail(&cursor->d_child, p);
92                         spin_unlock(&dcache_lock);
93                 }
94         }
95         up(&file->f_dentry->d_inode->i_sem);
96         return offset;
97 }
98
99 /* Relationship between i_mode and the DT_xxx types */
100 static inline unsigned char dt_type(struct inode *inode)
101 {
102         return (inode->i_mode >> 12) & 15;
103 }
104
105 /*
106  * Directory is locked and all positive dentries in it are safe, since
107  * for ramfs-type trees they can't go away without unlink() or rmdir(),
108  * both impossible due to the lock on directory.
109  */
110
111 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
112 {
113         struct dentry *dentry = filp->f_dentry;
114         struct dentry *cursor = filp->private_data;
115         struct list_head *p, *q = &cursor->d_child;
116         ino_t ino;
117         int i = filp->f_pos;
118
119         switch (i) {
120                 case 0:
121                         ino = dentry->d_inode->i_ino;
122                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
123                                 break;
124                         filp->f_pos++;
125                         i++;
126                         /* fallthrough */
127                 case 1:
128                         ino = parent_ino(dentry);
129                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
130                                 break;
131                         filp->f_pos++;
132                         i++;
133                         /* fallthrough */
134                 default:
135                         spin_lock(&dcache_lock);
136                         if (filp->f_pos == 2) {
137                                 list_del(q);
138                                 list_add(q, &dentry->d_subdirs);
139                         }
140                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
141                                 struct dentry *next;
142                                 next = list_entry(p, struct dentry, d_child);
143                                 if (d_unhashed(next) || !next->d_inode)
144                                         continue;
145
146                                 spin_unlock(&dcache_lock);
147                                 if (filldir(dirent, next->d_name.name, next->d_name.len, filp->f_pos, next->d_inode->i_ino, dt_type(next->d_inode)) < 0)
148                                         return 0;
149                                 spin_lock(&dcache_lock);
150                                 /* next is still alive */
151                                 list_del(q);
152                                 list_add(q, p);
153                                 p = q;
154                                 filp->f_pos++;
155                         }
156                         spin_unlock(&dcache_lock);
157         }
158         return 0;
159 }
160
161 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
162 {
163         return -EISDIR;
164 }
165
166 struct file_operations simple_dir_operations = {
167         .open           = dcache_dir_open,
168         .release        = dcache_dir_close,
169         .llseek         = dcache_dir_lseek,
170         .read           = generic_read_dir,
171         .readdir        = dcache_readdir,
172 };
173
174 struct inode_operations simple_dir_inode_operations = {
175         .lookup         = simple_lookup,
176 };
177
178 /*
179  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
180  * will never be mountable)
181  */
182 struct super_block *
183 get_sb_pseudo(struct file_system_type *fs_type, char *name,
184         struct super_operations *ops, unsigned long magic)
185 {
186         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
187         static struct super_operations default_ops = {.statfs = simple_statfs};
188         struct dentry *dentry;
189         struct inode *root;
190         struct qstr d_name = {.name = name, .len = strlen(name)};
191
192         if (IS_ERR(s))
193                 return s;
194
195         s->s_flags = MS_NOUSER;
196         s->s_maxbytes = ~0ULL;
197         s->s_blocksize = 1024;
198         s->s_blocksize_bits = 10;
199         s->s_magic = magic;
200         s->s_op = ops ? ops : &default_ops;
201         root = new_inode(s);
202         if (!root)
203                 goto Enomem;
204         root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
205         root->i_uid = root->i_gid = 0;
206         root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
207         dentry = d_alloc(NULL, &d_name);
208         if (!dentry) {
209                 iput(root);
210                 goto Enomem;
211         }
212         dentry->d_sb = s;
213         dentry->d_parent = dentry;
214         d_instantiate(dentry, root);
215         s->s_root = dentry;
216         s->s_flags |= MS_ACTIVE;
217         return s;
218
219 Enomem:
220         up_write(&s->s_umount);
221         deactivate_super(s);
222         return ERR_PTR(-ENOMEM);
223 }
224
225 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
226 {
227         struct inode *inode = old_dentry->d_inode;
228
229         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
230         inode->i_nlink++;
231         atomic_inc(&inode->i_count);
232         dget(dentry);
233         d_instantiate(dentry, inode);
234         return 0;
235 }
236
237 static inline int simple_positive(struct dentry *dentry)
238 {
239         return dentry->d_inode && !d_unhashed(dentry);
240 }
241
242 int simple_empty(struct dentry *dentry)
243 {
244         struct dentry *child;
245         int ret = 0;
246
247         spin_lock(&dcache_lock);
248         list_for_each_entry(child, &dentry->d_subdirs, d_child)
249                 if (simple_positive(child))
250                         goto out;
251         ret = 1;
252 out:
253         spin_unlock(&dcache_lock);
254         return ret;
255 }
256
257 int simple_unlink(struct inode *dir, struct dentry *dentry)
258 {
259         struct inode *inode = dentry->d_inode;
260
261         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
262         inode->i_nlink--;
263         dput(dentry);
264         return 0;
265 }
266
267 int simple_rmdir(struct inode *dir, struct dentry *dentry)
268 {
269         if (!simple_empty(dentry))
270                 return -ENOTEMPTY;
271
272         dentry->d_inode->i_nlink--;
273         simple_unlink(dir, dentry);
274         dir->i_nlink--;
275         return 0;
276 }
277
278 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
279                 struct inode *new_dir, struct dentry *new_dentry)
280 {
281         struct inode *inode = old_dentry->d_inode;
282         int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
283
284         if (!simple_empty(new_dentry))
285                 return -ENOTEMPTY;
286
287         if (new_dentry->d_inode) {
288                 simple_unlink(new_dir, new_dentry);
289                 if (they_are_dirs)
290                         old_dir->i_nlink--;
291         } else if (they_are_dirs) {
292                 old_dir->i_nlink--;
293                 new_dir->i_nlink++;
294         }
295
296         old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
297                 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
298
299         return 0;
300 }
301
302 int simple_readpage(struct file *file, struct page *page)
303 {
304         void *kaddr;
305
306         if (PageUptodate(page))
307                 goto out;
308
309         kaddr = kmap_atomic(page, KM_USER0);
310         memset(kaddr, 0, PAGE_CACHE_SIZE);
311         kunmap_atomic(kaddr, KM_USER0);
312         flush_dcache_page(page);
313         SetPageUptodate(page);
314 out:
315         unlock_page(page);
316         return 0;
317 }
318
319 int simple_prepare_write(struct file *file, struct page *page,
320                         unsigned from, unsigned to)
321 {
322         if (!PageUptodate(page)) {
323                 if (to - from != PAGE_CACHE_SIZE) {
324                         void *kaddr = kmap_atomic(page, KM_USER0);
325                         memset(kaddr, 0, from);
326                         memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
327                         flush_dcache_page(page);
328                         kunmap_atomic(kaddr, KM_USER0);
329                 }
330                 SetPageUptodate(page);
331         }
332         return 0;
333 }
334
335 int simple_commit_write(struct file *file, struct page *page,
336                         unsigned offset, unsigned to)
337 {
338         struct inode *inode = page->mapping->host;
339         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
340
341         /*
342          * No need to use i_size_read() here, the i_size
343          * cannot change under us because we hold the i_sem.
344          */
345         if (pos > inode->i_size)
346                 i_size_write(inode, pos);
347         set_page_dirty(page);
348         return 0;
349 }
350
351 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
352 {
353         static struct super_operations s_ops = {.statfs = simple_statfs};
354         struct inode *inode;
355         struct dentry *root;
356         struct dentry *dentry;
357         int i;
358
359         s->s_blocksize = PAGE_CACHE_SIZE;
360         s->s_blocksize_bits = PAGE_CACHE_SHIFT;
361         s->s_magic = magic;
362         s->s_op = &s_ops;
363
364         inode = new_inode(s);
365         if (!inode)
366                 return -ENOMEM;
367         inode->i_mode = S_IFDIR | 0755;
368         inode->i_uid = inode->i_gid = 0;
369         inode->i_blksize = PAGE_CACHE_SIZE;
370         inode->i_blocks = 0;
371         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
372         inode->i_op = &simple_dir_inode_operations;
373         inode->i_fop = &simple_dir_operations;
374         root = d_alloc_root(inode);
375         if (!root) {
376                 iput(inode);
377                 return -ENOMEM;
378         }
379         for (i = 0; !files->name || files->name[0]; i++, files++) {
380                 struct qstr name;
381                 if (!files->name)
382                         continue;
383                 name.name = files->name;
384                 name.len = strlen(name.name);
385                 name.hash = full_name_hash(name.name, name.len);
386                 dentry = d_alloc(root, &name);
387                 if (!dentry)
388                         goto out;
389                 inode = new_inode(s);
390                 if (!inode)
391                         goto out;
392                 inode->i_mode = S_IFREG | files->mode;
393                 inode->i_uid = inode->i_gid = 0;
394                 inode->i_blksize = PAGE_CACHE_SIZE;
395                 inode->i_blocks = 0;
396                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
397                 inode->i_fop = files->ops;
398                 inode->i_ino = i;
399                 d_add(dentry, inode);
400         }
401         s->s_root = root;
402         return 0;
403 out:
404         d_genocide(root);
405         dput(root);
406         return -ENOMEM;
407 }
408
409 static spinlock_t pin_fs_lock = SPIN_LOCK_UNLOCKED;
410
411 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
412 {
413         struct vfsmount *mnt = NULL;
414         spin_lock(&pin_fs_lock);
415         if (unlikely(!*mount)) {
416                 spin_unlock(&pin_fs_lock);
417                 mnt = do_kern_mount(name, 0, name, NULL);
418                 if (IS_ERR(mnt))
419                         return PTR_ERR(mnt);
420                 spin_lock(&pin_fs_lock);
421                 if (!*mount)
422                         *mount = mnt;
423         }
424         mntget(*mount);
425         ++*count;
426         spin_unlock(&pin_fs_lock);
427         mntput(mnt);
428         return 0;
429 }
430
431 void simple_release_fs(struct vfsmount **mount, int *count)
432 {
433         struct vfsmount *mnt;
434         spin_lock(&pin_fs_lock);
435         mnt = *mount;
436         if (!--*count)
437                 *mount = NULL;
438         spin_unlock(&pin_fs_lock);
439         mntput(mnt);
440 }
441
442 EXPORT_SYMBOL(dcache_dir_close);
443 EXPORT_SYMBOL(dcache_dir_lseek);
444 EXPORT_SYMBOL(dcache_dir_open);
445 EXPORT_SYMBOL(dcache_readdir);
446 EXPORT_SYMBOL(generic_read_dir);
447 EXPORT_SYMBOL(simple_commit_write);
448 EXPORT_SYMBOL(simple_dir_inode_operations);
449 EXPORT_SYMBOL(simple_dir_operations);
450 EXPORT_SYMBOL(simple_empty);
451 EXPORT_SYMBOL(simple_fill_super);
452 EXPORT_SYMBOL(simple_getattr);
453 EXPORT_SYMBOL(simple_link);
454 EXPORT_SYMBOL(simple_lookup);
455 EXPORT_SYMBOL(simple_pin_fs);
456 EXPORT_SYMBOL(simple_prepare_write);
457 EXPORT_SYMBOL(simple_readpage);
458 EXPORT_SYMBOL(simple_release_fs);
459 EXPORT_SYMBOL(simple_rename);
460 EXPORT_SYMBOL(simple_rmdir);
461 EXPORT_SYMBOL(simple_statfs);
462 EXPORT_SYMBOL(simple_sync_file);
463 EXPORT_SYMBOL(simple_unlink);