Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[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 #include <linux/mutex.h>
11
12 #include <asm/uaccess.h>
13
14 int simple_getattr(struct vfsmount *mnt, struct dentry *dentry,
15                    struct kstat *stat)
16 {
17         struct inode *inode = dentry->d_inode;
18         generic_fillattr(inode, stat);
19         stat->blocks = inode->i_mapping->nrpages << (PAGE_CACHE_SHIFT - 9);
20         return 0;
21 }
22
23 int simple_statfs(struct super_block *sb, struct kstatfs *buf)
24 {
25         buf->f_type = sb->s_magic;
26         buf->f_bsize = PAGE_CACHE_SIZE;
27         buf->f_namelen = NAME_MAX;
28         return 0;
29 }
30
31 /*
32  * Retaining negative dentries for an in-memory filesystem just wastes
33  * memory and lookup time: arrange for them to be deleted immediately.
34  */
35 static int simple_delete_dentry(struct dentry *dentry)
36 {
37         return 1;
38 }
39
40 /*
41  * Lookup the data. This is trivial - if the dentry didn't already
42  * exist, we know it is negative.  Set d_op to delete negative dentries.
43  */
44 struct dentry *simple_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
45 {
46         static struct dentry_operations simple_dentry_operations = {
47                 .d_delete = simple_delete_dentry,
48         };
49
50         if (dentry->d_name.len > NAME_MAX)
51                 return ERR_PTR(-ENAMETOOLONG);
52         dentry->d_op = &simple_dentry_operations;
53         d_add(dentry, NULL);
54         return NULL;
55 }
56
57 int simple_sync_file(struct file * file, struct dentry *dentry, int datasync)
58 {
59         return 0;
60 }
61  
62 int dcache_dir_open(struct inode *inode, struct file *file)
63 {
64         static struct qstr cursor_name = {.len = 1, .name = "."};
65
66         file->private_data = d_alloc(file->f_dentry, &cursor_name);
67
68         return file->private_data ? 0 : -ENOMEM;
69 }
70
71 int dcache_dir_close(struct inode *inode, struct file *file)
72 {
73         dput(file->private_data);
74         return 0;
75 }
76
77 loff_t dcache_dir_lseek(struct file *file, loff_t offset, int origin)
78 {
79         mutex_lock(&file->f_dentry->d_inode->i_mutex);
80         switch (origin) {
81                 case 1:
82                         offset += file->f_pos;
83                 case 0:
84                         if (offset >= 0)
85                                 break;
86                 default:
87                         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
88                         return -EINVAL;
89         }
90         if (offset != file->f_pos) {
91                 file->f_pos = offset;
92                 if (file->f_pos >= 2) {
93                         struct list_head *p;
94                         struct dentry *cursor = file->private_data;
95                         loff_t n = file->f_pos - 2;
96
97                         spin_lock(&dcache_lock);
98                         list_del(&cursor->d_u.d_child);
99                         p = file->f_dentry->d_subdirs.next;
100                         while (n && p != &file->f_dentry->d_subdirs) {
101                                 struct dentry *next;
102                                 next = list_entry(p, struct dentry, d_u.d_child);
103                                 if (!d_unhashed(next) && next->d_inode)
104                                         n--;
105                                 p = p->next;
106                         }
107                         list_add_tail(&cursor->d_u.d_child, p);
108                         spin_unlock(&dcache_lock);
109                 }
110         }
111         mutex_unlock(&file->f_dentry->d_inode->i_mutex);
112         return offset;
113 }
114
115 /* Relationship between i_mode and the DT_xxx types */
116 static inline unsigned char dt_type(struct inode *inode)
117 {
118         return (inode->i_mode >> 12) & 15;
119 }
120
121 /*
122  * Directory is locked and all positive dentries in it are safe, since
123  * for ramfs-type trees they can't go away without unlink() or rmdir(),
124  * both impossible due to the lock on directory.
125  */
126
127 static inline int do_dcache_readdir_filter(struct file * filp,
128         void * dirent, filldir_t filldir, int (*filter)(struct dentry *dentry))
129 {
130         struct dentry *dentry = filp->f_dentry;
131         struct dentry *cursor = filp->private_data;
132         struct list_head *p, *q = &cursor->d_u.d_child;
133         ino_t ino;
134         int i = filp->f_pos;
135
136         switch (i) {
137                 case 0:
138                         ino = dentry->d_inode->i_ino;
139                         if (filldir(dirent, ".", 1, i, ino, DT_DIR) < 0)
140                                 break;
141                         filp->f_pos++;
142                         i++;
143                         /* fallthrough */
144                 case 1:
145                         ino = parent_ino(dentry);
146                         if (filldir(dirent, "..", 2, i, ino, DT_DIR) < 0)
147                                 break;
148                         filp->f_pos++;
149                         i++;
150                         /* fallthrough */
151                 default:
152                         spin_lock(&dcache_lock);
153                         if (filp->f_pos == 2) {
154                                 list_del(q);
155                                 list_add(q, &dentry->d_subdirs);
156                         }
157                         for (p=q->next; p != &dentry->d_subdirs; p=p->next) {
158                                 struct dentry *next;
159                                 next = list_entry(p, struct dentry, d_u.d_child);
160                                 if (d_unhashed(next) || !next->d_inode)
161                                         continue;
162                                 if (filter && !filter(next))
163                                         continue;
164
165                                 spin_unlock(&dcache_lock);
166                                 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)
167                                         return 0;
168                                 spin_lock(&dcache_lock);
169                                 /* next is still alive */
170                                 list_del(q);
171                                 list_add(q, p);
172                                 p = q;
173                                 filp->f_pos++;
174                         }
175                         spin_unlock(&dcache_lock);
176         }
177         return 0;
178 }
179
180 int dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
181 {
182         return do_dcache_readdir_filter(filp, dirent, filldir, NULL);
183 }
184
185 int dcache_readdir_filter(struct file * filp, void * dirent, filldir_t filldir,
186         int (*filter)(struct dentry *))
187 {
188         return do_dcache_readdir_filter(filp, dirent, filldir, filter);
189 }
190
191
192 ssize_t generic_read_dir(struct file *filp, char __user *buf, size_t siz, loff_t *ppos)
193 {
194         return -EISDIR;
195 }
196
197 const struct file_operations simple_dir_operations = {
198         .open           = dcache_dir_open,
199         .release        = dcache_dir_close,
200         .llseek         = dcache_dir_lseek,
201         .read           = generic_read_dir,
202         .readdir        = dcache_readdir,
203         .fsync          = simple_sync_file,
204 };
205
206 struct inode_operations simple_dir_inode_operations = {
207         .lookup         = simple_lookup,
208 };
209
210 /*
211  * Common helper for pseudo-filesystems (sockfs, pipefs, bdev - stuff that
212  * will never be mountable)
213  */
214 struct super_block *
215 get_sb_pseudo(struct file_system_type *fs_type, char *name,
216         struct super_operations *ops, unsigned long magic)
217 {
218         struct super_block *s = sget(fs_type, NULL, set_anon_super, NULL);
219         static struct super_operations default_ops = {.statfs = simple_statfs};
220         struct dentry *dentry;
221         struct inode *root;
222         struct qstr d_name = {.name = name, .len = strlen(name)};
223
224         if (IS_ERR(s))
225                 return s;
226
227         s->s_flags = MS_NOUSER;
228         s->s_maxbytes = ~0ULL;
229         s->s_blocksize = 1024;
230         s->s_blocksize_bits = 10;
231         s->s_magic = magic;
232         s->s_op = ops ? ops : &default_ops;
233         s->s_time_gran = 1;
234         root = new_inode(s);
235         if (!root)
236                 goto Enomem;
237         root->i_mode = S_IFDIR | S_IRUSR | S_IWUSR;
238         root->i_uid = root->i_gid = 0;
239         root->i_atime = root->i_mtime = root->i_ctime = CURRENT_TIME;
240         dentry = d_alloc(NULL, &d_name);
241         if (!dentry) {
242                 iput(root);
243                 goto Enomem;
244         }
245         dentry->d_sb = s;
246         dentry->d_parent = dentry;
247         d_instantiate(dentry, root);
248         s->s_root = dentry;
249         s->s_flags |= MS_ACTIVE;
250         return s;
251
252 Enomem:
253         up_write(&s->s_umount);
254         deactivate_super(s);
255         return ERR_PTR(-ENOMEM);
256 }
257
258 int simple_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
259 {
260         struct inode *inode = old_dentry->d_inode;
261
262         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
263         inode->i_nlink++;
264         atomic_inc(&inode->i_count);
265         dget(dentry);
266         d_instantiate(dentry, inode);
267         return 0;
268 }
269
270 static inline int simple_positive(struct dentry *dentry)
271 {
272         return dentry->d_inode && !d_unhashed(dentry);
273 }
274
275 int simple_empty(struct dentry *dentry)
276 {
277         struct dentry *child;
278         int ret = 0;
279
280         spin_lock(&dcache_lock);
281         list_for_each_entry(child, &dentry->d_subdirs, d_u.d_child)
282                 if (simple_positive(child))
283                         goto out;
284         ret = 1;
285 out:
286         spin_unlock(&dcache_lock);
287         return ret;
288 }
289
290 int simple_unlink(struct inode *dir, struct dentry *dentry)
291 {
292         struct inode *inode = dentry->d_inode;
293
294         inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
295         inode->i_nlink--;
296         dput(dentry);
297         return 0;
298 }
299
300 int simple_rmdir(struct inode *dir, struct dentry *dentry)
301 {
302         if (!simple_empty(dentry))
303                 return -ENOTEMPTY;
304
305         dentry->d_inode->i_nlink--;
306         simple_unlink(dir, dentry);
307         dir->i_nlink--;
308         return 0;
309 }
310
311 int simple_rename(struct inode *old_dir, struct dentry *old_dentry,
312                 struct inode *new_dir, struct dentry *new_dentry)
313 {
314         struct inode *inode = old_dentry->d_inode;
315         int they_are_dirs = S_ISDIR(old_dentry->d_inode->i_mode);
316
317         if (!simple_empty(new_dentry))
318                 return -ENOTEMPTY;
319
320         if (new_dentry->d_inode) {
321                 simple_unlink(new_dir, new_dentry);
322                 if (they_are_dirs)
323                         old_dir->i_nlink--;
324         } else if (they_are_dirs) {
325                 old_dir->i_nlink--;
326                 new_dir->i_nlink++;
327         }
328
329         old_dir->i_ctime = old_dir->i_mtime = new_dir->i_ctime =
330                 new_dir->i_mtime = inode->i_ctime = CURRENT_TIME;
331
332         return 0;
333 }
334
335 int simple_readpage(struct file *file, struct page *page)
336 {
337         void *kaddr;
338
339         if (PageUptodate(page))
340                 goto out;
341
342         kaddr = kmap_atomic(page, KM_USER0);
343         memset(kaddr, 0, PAGE_CACHE_SIZE);
344         kunmap_atomic(kaddr, KM_USER0);
345         flush_dcache_page(page);
346         SetPageUptodate(page);
347 out:
348         unlock_page(page);
349         return 0;
350 }
351
352 int simple_prepare_write(struct file *file, struct page *page,
353                         unsigned from, unsigned to)
354 {
355         if (!PageUptodate(page)) {
356                 if (to - from != PAGE_CACHE_SIZE) {
357                         void *kaddr = kmap_atomic(page, KM_USER0);
358                         memset(kaddr, 0, from);
359                         memset(kaddr + to, 0, PAGE_CACHE_SIZE - to);
360                         flush_dcache_page(page);
361                         kunmap_atomic(kaddr, KM_USER0);
362                 }
363                 SetPageUptodate(page);
364         }
365         return 0;
366 }
367
368 int simple_commit_write(struct file *file, struct page *page,
369                         unsigned offset, unsigned to)
370 {
371         struct inode *inode = page->mapping->host;
372         loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
373
374         /*
375          * No need to use i_size_read() here, the i_size
376          * cannot change under us because we hold the i_mutex.
377          */
378         if (pos > inode->i_size)
379                 i_size_write(inode, pos);
380         set_page_dirty(page);
381         return 0;
382 }
383
384 int simple_fill_super(struct super_block *s, int magic, struct tree_descr *files)
385 {
386         static struct super_operations s_ops = {.statfs = simple_statfs};
387         struct inode *inode;
388         struct dentry *root;
389         struct dentry *dentry;
390         int i;
391
392         s->s_blocksize = PAGE_CACHE_SIZE;
393         s->s_blocksize_bits = PAGE_CACHE_SHIFT;
394         s->s_magic = magic;
395         s->s_op = &s_ops;
396         s->s_time_gran = 1;
397
398         inode = new_inode(s);
399         if (!inode)
400                 return -ENOMEM;
401         inode->i_mode = S_IFDIR | 0755;
402         inode->i_uid = inode->i_gid = 0;
403         inode->i_blksize = PAGE_CACHE_SIZE;
404         inode->i_blocks = 0;
405         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
406         inode->i_op = &simple_dir_inode_operations;
407         inode->i_fop = &simple_dir_operations;
408         inode->i_nlink = 2;
409         root = d_alloc_root(inode);
410         if (!root) {
411                 iput(inode);
412                 return -ENOMEM;
413         }
414         for (i = 0; !files->name || files->name[0]; i++, files++) {
415                 if (!files->name)
416                         continue;
417                 dentry = d_alloc_name(root, files->name);
418                 if (!dentry)
419                         goto out;
420                 inode = new_inode(s);
421                 if (!inode)
422                         goto out;
423                 inode->i_mode = S_IFREG | files->mode;
424                 inode->i_uid = inode->i_gid = 0;
425                 inode->i_blksize = PAGE_CACHE_SIZE;
426                 inode->i_blocks = 0;
427                 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
428                 inode->i_fop = files->ops;
429                 inode->i_ino = i;
430                 d_add(dentry, inode);
431         }
432         s->s_root = root;
433         return 0;
434 out:
435         d_genocide(root);
436         dput(root);
437         return -ENOMEM;
438 }
439
440 static DEFINE_SPINLOCK(pin_fs_lock);
441
442 int simple_pin_fs(char *name, struct vfsmount **mount, int *count)
443 {
444         struct vfsmount *mnt = NULL;
445         spin_lock(&pin_fs_lock);
446         if (unlikely(!*mount)) {
447                 spin_unlock(&pin_fs_lock);
448                 mnt = do_kern_mount(name, 0, name, NULL);
449                 if (IS_ERR(mnt))
450                         return PTR_ERR(mnt);
451                 spin_lock(&pin_fs_lock);
452                 if (!*mount)
453                         *mount = mnt;
454         }
455         mntget(*mount);
456         ++*count;
457         spin_unlock(&pin_fs_lock);
458         mntput(mnt);
459         return 0;
460 }
461
462 void simple_release_fs(struct vfsmount **mount, int *count)
463 {
464         struct vfsmount *mnt;
465         spin_lock(&pin_fs_lock);
466         mnt = *mount;
467         if (!--*count)
468                 *mount = NULL;
469         spin_unlock(&pin_fs_lock);
470         mntput(mnt);
471 }
472
473 ssize_t simple_read_from_buffer(void __user *to, size_t count, loff_t *ppos,
474                                 const void *from, size_t available)
475 {
476         loff_t pos = *ppos;
477         if (pos < 0)
478                 return -EINVAL;
479         if (pos >= available)
480                 return 0;
481         if (count > available - pos)
482                 count = available - pos;
483         if (copy_to_user(to, from + pos, count))
484                 return -EFAULT;
485         *ppos = pos + count;
486         return count;
487 }
488
489 /*
490  * Transaction based IO.
491  * The file expects a single write which triggers the transaction, and then
492  * possibly a read which collects the result - which is stored in a
493  * file-local buffer.
494  */
495 char *simple_transaction_get(struct file *file, const char __user *buf, size_t size)
496 {
497         struct simple_transaction_argresp *ar;
498         static DEFINE_SPINLOCK(simple_transaction_lock);
499
500         if (size > SIMPLE_TRANSACTION_LIMIT - 1)
501                 return ERR_PTR(-EFBIG);
502
503         ar = (struct simple_transaction_argresp *)get_zeroed_page(GFP_KERNEL);
504         if (!ar)
505                 return ERR_PTR(-ENOMEM);
506
507         spin_lock(&simple_transaction_lock);
508
509         /* only one write allowed per open */
510         if (file->private_data) {
511                 spin_unlock(&simple_transaction_lock);
512                 free_page((unsigned long)ar);
513                 return ERR_PTR(-EBUSY);
514         }
515
516         file->private_data = ar;
517
518         spin_unlock(&simple_transaction_lock);
519
520         if (copy_from_user(ar->data, buf, size))
521                 return ERR_PTR(-EFAULT);
522
523         return ar->data;
524 }
525
526 ssize_t simple_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
527 {
528         struct simple_transaction_argresp *ar = file->private_data;
529
530         if (!ar)
531                 return 0;
532         return simple_read_from_buffer(buf, size, pos, ar->data, ar->size);
533 }
534
535 int simple_transaction_release(struct inode *inode, struct file *file)
536 {
537         free_page((unsigned long)file->private_data);
538         return 0;
539 }
540
541 /* Simple attribute files */
542
543 struct simple_attr {
544         u64 (*get)(void *);
545         void (*set)(void *, u64);
546         char get_buf[24];       /* enough to store a u64 and "\n\0" */
547         char set_buf[24];
548         void *data;
549         const char *fmt;        /* format for read operation */
550         struct mutex mutex;     /* protects access to these buffers */
551 };
552
553 /* simple_attr_open is called by an actual attribute open file operation
554  * to set the attribute specific access operations. */
555 int simple_attr_open(struct inode *inode, struct file *file,
556                      u64 (*get)(void *), void (*set)(void *, u64),
557                      const char *fmt)
558 {
559         struct simple_attr *attr;
560
561         attr = kmalloc(sizeof(*attr), GFP_KERNEL);
562         if (!attr)
563                 return -ENOMEM;
564
565         attr->get = get;
566         attr->set = set;
567         attr->data = inode->u.generic_ip;
568         attr->fmt = fmt;
569         mutex_init(&attr->mutex);
570
571         file->private_data = attr;
572
573         return nonseekable_open(inode, file);
574 }
575
576 int simple_attr_close(struct inode *inode, struct file *file)
577 {
578         kfree(file->private_data);
579         return 0;
580 }
581
582 /* read from the buffer that is filled with the get function */
583 ssize_t simple_attr_read(struct file *file, char __user *buf,
584                          size_t len, loff_t *ppos)
585 {
586         struct simple_attr *attr;
587         size_t size;
588         ssize_t ret;
589
590         attr = file->private_data;
591
592         if (!attr->get)
593                 return -EACCES;
594
595         mutex_lock(&attr->mutex);
596         if (*ppos) /* continued read */
597                 size = strlen(attr->get_buf);
598         else      /* first read */
599                 size = scnprintf(attr->get_buf, sizeof(attr->get_buf),
600                                  attr->fmt,
601                                  (unsigned long long)attr->get(attr->data));
602
603         ret = simple_read_from_buffer(buf, len, ppos, attr->get_buf, size);
604         mutex_unlock(&attr->mutex);
605         return ret;
606 }
607
608 /* interpret the buffer as a number to call the set function with */
609 ssize_t simple_attr_write(struct file *file, const char __user *buf,
610                           size_t len, loff_t *ppos)
611 {
612         struct simple_attr *attr;
613         u64 val;
614         size_t size;
615         ssize_t ret;
616
617         attr = file->private_data;
618
619         if (!attr->set)
620                 return -EACCES;
621
622         mutex_lock(&attr->mutex);
623         ret = -EFAULT;
624         size = min(sizeof(attr->set_buf) - 1, len);
625         if (copy_from_user(attr->set_buf, buf, size))
626                 goto out;
627
628         ret = len; /* claim we got the whole input */
629         attr->set_buf[size] = '\0';
630         val = simple_strtol(attr->set_buf, NULL, 0);
631         attr->set(attr->data, val);
632 out:
633         mutex_unlock(&attr->mutex);
634         return ret;
635 }
636
637 EXPORT_SYMBOL(dcache_dir_close);
638 EXPORT_SYMBOL(dcache_dir_lseek);
639 EXPORT_SYMBOL(dcache_dir_open);
640 EXPORT_SYMBOL(dcache_readdir);
641 EXPORT_SYMBOL(dcache_readdir_filter);
642 EXPORT_SYMBOL(generic_read_dir);
643 EXPORT_SYMBOL(get_sb_pseudo);
644 EXPORT_SYMBOL(simple_commit_write);
645 EXPORT_SYMBOL(simple_dir_inode_operations);
646 EXPORT_SYMBOL(simple_dir_operations);
647 EXPORT_SYMBOL(simple_empty);
648 EXPORT_SYMBOL(d_alloc_name);
649 EXPORT_SYMBOL(simple_fill_super);
650 EXPORT_SYMBOL(simple_getattr);
651 EXPORT_SYMBOL(simple_link);
652 EXPORT_SYMBOL(simple_lookup);
653 EXPORT_SYMBOL(simple_pin_fs);
654 EXPORT_SYMBOL(simple_prepare_write);
655 EXPORT_SYMBOL(simple_readpage);
656 EXPORT_SYMBOL(simple_release_fs);
657 EXPORT_SYMBOL(simple_rename);
658 EXPORT_SYMBOL(simple_rmdir);
659 EXPORT_SYMBOL(simple_statfs);
660 EXPORT_SYMBOL(simple_sync_file);
661 EXPORT_SYMBOL(simple_unlink);
662 EXPORT_SYMBOL(simple_read_from_buffer);
663 EXPORT_SYMBOL(simple_transaction_get);
664 EXPORT_SYMBOL(simple_transaction_read);
665 EXPORT_SYMBOL(simple_transaction_release);
666 EXPORT_SYMBOL_GPL(simple_attr_open);
667 EXPORT_SYMBOL_GPL(simple_attr_close);
668 EXPORT_SYMBOL_GPL(simple_attr_read);
669 EXPORT_SYMBOL_GPL(simple_attr_write);