[PATCH] fuse: add request interruption
[safe/jmp/linux-2.6] / fs / fuse / inode.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7 */
8
9 #include "fuse_i.h"
10
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/file.h>
14 #include <linux/seq_file.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/parser.h>
18 #include <linux/statfs.h>
19
20 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21 MODULE_DESCRIPTION("Filesystem in Userspace");
22 MODULE_LICENSE("GPL");
23
24 static kmem_cache_t *fuse_inode_cachep;
25 struct list_head fuse_conn_list;
26 DEFINE_MUTEX(fuse_mutex);
27
28 #define FUSE_SUPER_MAGIC 0x65735546
29
30 struct fuse_mount_data {
31         int fd;
32         unsigned rootmode;
33         unsigned user_id;
34         unsigned group_id;
35         unsigned fd_present : 1;
36         unsigned rootmode_present : 1;
37         unsigned user_id_present : 1;
38         unsigned group_id_present : 1;
39         unsigned flags;
40         unsigned max_read;
41 };
42
43 static struct inode *fuse_alloc_inode(struct super_block *sb)
44 {
45         struct inode *inode;
46         struct fuse_inode *fi;
47
48         inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
49         if (!inode)
50                 return NULL;
51
52         fi = get_fuse_inode(inode);
53         fi->i_time = jiffies - 1;
54         fi->nodeid = 0;
55         fi->nlookup = 0;
56         fi->forget_req = fuse_request_alloc();
57         if (!fi->forget_req) {
58                 kmem_cache_free(fuse_inode_cachep, inode);
59                 return NULL;
60         }
61
62         return inode;
63 }
64
65 static void fuse_destroy_inode(struct inode *inode)
66 {
67         struct fuse_inode *fi = get_fuse_inode(inode);
68         if (fi->forget_req)
69                 fuse_request_free(fi->forget_req);
70         kmem_cache_free(fuse_inode_cachep, inode);
71 }
72
73 static void fuse_read_inode(struct inode *inode)
74 {
75         /* No op */
76 }
77
78 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
79                       unsigned long nodeid, u64 nlookup)
80 {
81         struct fuse_forget_in *inarg = &req->misc.forget_in;
82         inarg->nlookup = nlookup;
83         req->in.h.opcode = FUSE_FORGET;
84         req->in.h.nodeid = nodeid;
85         req->in.numargs = 1;
86         req->in.args[0].size = sizeof(struct fuse_forget_in);
87         req->in.args[0].value = inarg;
88         request_send_noreply(fc, req);
89 }
90
91 static void fuse_clear_inode(struct inode *inode)
92 {
93         if (inode->i_sb->s_flags & MS_ACTIVE) {
94                 struct fuse_conn *fc = get_fuse_conn(inode);
95                 struct fuse_inode *fi = get_fuse_inode(inode);
96                 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
97                 fi->forget_req = NULL;
98         }
99 }
100
101 static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
102 {
103         if (*flags & MS_MANDLOCK)
104                 return -EINVAL;
105
106         return 0;
107 }
108
109 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
110 {
111         if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
112                 invalidate_inode_pages(inode->i_mapping);
113
114         inode->i_ino     = attr->ino;
115         inode->i_mode    = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
116         inode->i_nlink   = attr->nlink;
117         inode->i_uid     = attr->uid;
118         inode->i_gid     = attr->gid;
119         i_size_write(inode, attr->size);
120         inode->i_blksize = PAGE_CACHE_SIZE;
121         inode->i_blocks  = attr->blocks;
122         inode->i_atime.tv_sec   = attr->atime;
123         inode->i_atime.tv_nsec  = attr->atimensec;
124         inode->i_mtime.tv_sec   = attr->mtime;
125         inode->i_mtime.tv_nsec  = attr->mtimensec;
126         inode->i_ctime.tv_sec   = attr->ctime;
127         inode->i_ctime.tv_nsec  = attr->ctimensec;
128 }
129
130 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
131 {
132         inode->i_mode = attr->mode & S_IFMT;
133         i_size_write(inode, attr->size);
134         if (S_ISREG(inode->i_mode)) {
135                 fuse_init_common(inode);
136                 fuse_init_file_inode(inode);
137         } else if (S_ISDIR(inode->i_mode))
138                 fuse_init_dir(inode);
139         else if (S_ISLNK(inode->i_mode))
140                 fuse_init_symlink(inode);
141         else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
142                  S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
143                 fuse_init_common(inode);
144                 init_special_inode(inode, inode->i_mode,
145                                    new_decode_dev(attr->rdev));
146         } else
147                 BUG();
148 }
149
150 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
151 {
152         unsigned long nodeid = *(unsigned long *) _nodeidp;
153         if (get_node_id(inode) == nodeid)
154                 return 1;
155         else
156                 return 0;
157 }
158
159 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
160 {
161         unsigned long nodeid = *(unsigned long *) _nodeidp;
162         get_fuse_inode(inode)->nodeid = nodeid;
163         return 0;
164 }
165
166 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
167                         int generation, struct fuse_attr *attr)
168 {
169         struct inode *inode;
170         struct fuse_inode *fi;
171         struct fuse_conn *fc = get_fuse_conn_super(sb);
172         int retried = 0;
173
174  retry:
175         inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
176         if (!inode)
177                 return NULL;
178
179         if ((inode->i_state & I_NEW)) {
180                 inode->i_flags |= S_NOATIME|S_NOCMTIME;
181                 inode->i_generation = generation;
182                 inode->i_data.backing_dev_info = &fc->bdi;
183                 fuse_init_inode(inode, attr);
184                 unlock_new_inode(inode);
185         } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
186                 BUG_ON(retried);
187                 /* Inode has changed type, any I/O on the old should fail */
188                 make_bad_inode(inode);
189                 iput(inode);
190                 retried = 1;
191                 goto retry;
192         }
193
194         fi = get_fuse_inode(inode);
195         fi->nlookup ++;
196         fuse_change_attributes(inode, attr);
197         return inode;
198 }
199
200 static void fuse_umount_begin(struct super_block *sb)
201 {
202         fuse_abort_conn(get_fuse_conn_super(sb));
203 }
204
205 static void fuse_put_super(struct super_block *sb)
206 {
207         struct fuse_conn *fc = get_fuse_conn_super(sb);
208
209         spin_lock(&fc->lock);
210         fc->connected = 0;
211         fc->blocked = 0;
212         spin_unlock(&fc->lock);
213         /* Flush all readers on this fs */
214         kill_fasync(&fc->fasync, SIGIO, POLL_IN);
215         wake_up_all(&fc->waitq);
216         wake_up_all(&fc->blocked_waitq);
217         mutex_lock(&fuse_mutex);
218         list_del(&fc->entry);
219         fuse_ctl_remove_conn(fc);
220         mutex_unlock(&fuse_mutex);
221         fuse_conn_put(fc);
222 }
223
224 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
225 {
226         stbuf->f_type    = FUSE_SUPER_MAGIC;
227         stbuf->f_bsize   = attr->bsize;
228         stbuf->f_frsize  = attr->frsize;
229         stbuf->f_blocks  = attr->blocks;
230         stbuf->f_bfree   = attr->bfree;
231         stbuf->f_bavail  = attr->bavail;
232         stbuf->f_files   = attr->files;
233         stbuf->f_ffree   = attr->ffree;
234         stbuf->f_namelen = attr->namelen;
235         /* fsid is left zero */
236 }
237
238 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
239 {
240         struct super_block *sb = dentry->d_sb;
241         struct fuse_conn *fc = get_fuse_conn_super(sb);
242         struct fuse_req *req;
243         struct fuse_statfs_out outarg;
244         int err;
245
246         req = fuse_get_req(fc);
247         if (IS_ERR(req))
248                 return PTR_ERR(req);
249
250         memset(&outarg, 0, sizeof(outarg));
251         req->in.numargs = 0;
252         req->in.h.opcode = FUSE_STATFS;
253         req->out.numargs = 1;
254         req->out.args[0].size =
255                 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
256         req->out.args[0].value = &outarg;
257         request_send(fc, req);
258         err = req->out.h.error;
259         if (!err)
260                 convert_fuse_statfs(buf, &outarg.st);
261         fuse_put_request(fc, req);
262         return err;
263 }
264
265 enum {
266         OPT_FD,
267         OPT_ROOTMODE,
268         OPT_USER_ID,
269         OPT_GROUP_ID,
270         OPT_DEFAULT_PERMISSIONS,
271         OPT_ALLOW_OTHER,
272         OPT_MAX_READ,
273         OPT_ERR
274 };
275
276 static match_table_t tokens = {
277         {OPT_FD,                        "fd=%u"},
278         {OPT_ROOTMODE,                  "rootmode=%o"},
279         {OPT_USER_ID,                   "user_id=%u"},
280         {OPT_GROUP_ID,                  "group_id=%u"},
281         {OPT_DEFAULT_PERMISSIONS,       "default_permissions"},
282         {OPT_ALLOW_OTHER,               "allow_other"},
283         {OPT_MAX_READ,                  "max_read=%u"},
284         {OPT_ERR,                       NULL}
285 };
286
287 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
288 {
289         char *p;
290         memset(d, 0, sizeof(struct fuse_mount_data));
291         d->max_read = ~0;
292
293         while ((p = strsep(&opt, ",")) != NULL) {
294                 int token;
295                 int value;
296                 substring_t args[MAX_OPT_ARGS];
297                 if (!*p)
298                         continue;
299
300                 token = match_token(p, tokens, args);
301                 switch (token) {
302                 case OPT_FD:
303                         if (match_int(&args[0], &value))
304                                 return 0;
305                         d->fd = value;
306                         d->fd_present = 1;
307                         break;
308
309                 case OPT_ROOTMODE:
310                         if (match_octal(&args[0], &value))
311                                 return 0;
312                         d->rootmode = value;
313                         d->rootmode_present = 1;
314                         break;
315
316                 case OPT_USER_ID:
317                         if (match_int(&args[0], &value))
318                                 return 0;
319                         d->user_id = value;
320                         d->user_id_present = 1;
321                         break;
322
323                 case OPT_GROUP_ID:
324                         if (match_int(&args[0], &value))
325                                 return 0;
326                         d->group_id = value;
327                         d->group_id_present = 1;
328                         break;
329
330                 case OPT_DEFAULT_PERMISSIONS:
331                         d->flags |= FUSE_DEFAULT_PERMISSIONS;
332                         break;
333
334                 case OPT_ALLOW_OTHER:
335                         d->flags |= FUSE_ALLOW_OTHER;
336                         break;
337
338                 case OPT_MAX_READ:
339                         if (match_int(&args[0], &value))
340                                 return 0;
341                         d->max_read = value;
342                         break;
343
344                 default:
345                         return 0;
346                 }
347         }
348
349         if (!d->fd_present || !d->rootmode_present ||
350             !d->user_id_present || !d->group_id_present)
351                 return 0;
352
353         return 1;
354 }
355
356 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
357 {
358         struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
359
360         seq_printf(m, ",user_id=%u", fc->user_id);
361         seq_printf(m, ",group_id=%u", fc->group_id);
362         if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
363                 seq_puts(m, ",default_permissions");
364         if (fc->flags & FUSE_ALLOW_OTHER)
365                 seq_puts(m, ",allow_other");
366         if (fc->max_read != ~0)
367                 seq_printf(m, ",max_read=%u", fc->max_read);
368         return 0;
369 }
370
371 static struct fuse_conn *new_conn(void)
372 {
373         struct fuse_conn *fc;
374
375         fc = kzalloc(sizeof(*fc), GFP_KERNEL);
376         if (fc) {
377                 spin_lock_init(&fc->lock);
378                 atomic_set(&fc->count, 1);
379                 init_waitqueue_head(&fc->waitq);
380                 init_waitqueue_head(&fc->blocked_waitq);
381                 INIT_LIST_HEAD(&fc->pending);
382                 INIT_LIST_HEAD(&fc->processing);
383                 INIT_LIST_HEAD(&fc->io);
384                 INIT_LIST_HEAD(&fc->interrupts);
385                 atomic_set(&fc->num_waiting, 0);
386                 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
387                 fc->bdi.unplug_io_fn = default_unplug_io_fn;
388                 fc->reqctr = 0;
389                 fc->blocked = 1;
390         }
391         return fc;
392 }
393
394 void fuse_conn_put(struct fuse_conn *fc)
395 {
396         if (atomic_dec_and_test(&fc->count))
397                 kfree(fc);
398 }
399
400 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
401 {
402         atomic_inc(&fc->count);
403         return fc;
404 }
405
406 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
407 {
408         struct fuse_attr attr;
409         memset(&attr, 0, sizeof(attr));
410
411         attr.mode = mode;
412         attr.ino = FUSE_ROOT_ID;
413         return fuse_iget(sb, 1, 0, &attr);
414 }
415
416 static struct super_operations fuse_super_operations = {
417         .alloc_inode    = fuse_alloc_inode,
418         .destroy_inode  = fuse_destroy_inode,
419         .read_inode     = fuse_read_inode,
420         .clear_inode    = fuse_clear_inode,
421         .remount_fs     = fuse_remount_fs,
422         .put_super      = fuse_put_super,
423         .umount_begin   = fuse_umount_begin,
424         .statfs         = fuse_statfs,
425         .show_options   = fuse_show_options,
426 };
427
428 static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
429 {
430         struct fuse_init_out *arg = &req->misc.init_out;
431
432         if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
433                 fc->conn_error = 1;
434         else {
435                 unsigned long ra_pages;
436
437                 if (arg->minor >= 6) {
438                         ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
439                         if (arg->flags & FUSE_ASYNC_READ)
440                                 fc->async_read = 1;
441                         if (!(arg->flags & FUSE_POSIX_LOCKS))
442                                 fc->no_lock = 1;
443                 } else {
444                         ra_pages = fc->max_read / PAGE_CACHE_SIZE;
445                         fc->no_lock = 1;
446                 }
447
448                 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
449                 fc->minor = arg->minor;
450                 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
451         }
452         fuse_put_request(fc, req);
453         fc->blocked = 0;
454         wake_up_all(&fc->blocked_waitq);
455 }
456
457 static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
458 {
459         struct fuse_init_in *arg = &req->misc.init_in;
460
461         arg->major = FUSE_KERNEL_VERSION;
462         arg->minor = FUSE_KERNEL_MINOR_VERSION;
463         arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
464         arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS;
465         req->in.h.opcode = FUSE_INIT;
466         req->in.numargs = 1;
467         req->in.args[0].size = sizeof(*arg);
468         req->in.args[0].value = arg;
469         req->out.numargs = 1;
470         /* Variable length arguement used for backward compatibility
471            with interface version < 7.5.  Rest of init_out is zeroed
472            by do_get_request(), so a short reply is not a problem */
473         req->out.argvar = 1;
474         req->out.args[0].size = sizeof(struct fuse_init_out);
475         req->out.args[0].value = &req->misc.init_out;
476         req->end = process_init_reply;
477         request_send_background(fc, req);
478 }
479
480 static u64 conn_id(void)
481 {
482         static u64 ctr = 1;
483         return ctr++;
484 }
485
486 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
487 {
488         struct fuse_conn *fc;
489         struct inode *root;
490         struct fuse_mount_data d;
491         struct file *file;
492         struct dentry *root_dentry;
493         struct fuse_req *init_req;
494         int err;
495
496         if (sb->s_flags & MS_MANDLOCK)
497                 return -EINVAL;
498
499         if (!parse_fuse_opt((char *) data, &d))
500                 return -EINVAL;
501
502         sb->s_blocksize = PAGE_CACHE_SIZE;
503         sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
504         sb->s_magic = FUSE_SUPER_MAGIC;
505         sb->s_op = &fuse_super_operations;
506         sb->s_maxbytes = MAX_LFS_FILESIZE;
507
508         file = fget(d.fd);
509         if (!file)
510                 return -EINVAL;
511
512         if (file->f_op != &fuse_dev_operations)
513                 return -EINVAL;
514
515         fc = new_conn();
516         if (!fc)
517                 return -ENOMEM;
518
519         fc->flags = d.flags;
520         fc->user_id = d.user_id;
521         fc->group_id = d.group_id;
522         fc->max_read = d.max_read;
523
524         /* Used by get_root_inode() */
525         sb->s_fs_info = fc;
526
527         err = -ENOMEM;
528         root = get_root_inode(sb, d.rootmode);
529         if (!root)
530                 goto err;
531
532         root_dentry = d_alloc_root(root);
533         if (!root_dentry) {
534                 iput(root);
535                 goto err;
536         }
537
538         init_req = fuse_request_alloc();
539         if (!init_req)
540                 goto err_put_root;
541
542         mutex_lock(&fuse_mutex);
543         err = -EINVAL;
544         if (file->private_data)
545                 goto err_unlock;
546
547         fc->id = conn_id();
548         err = fuse_ctl_add_conn(fc);
549         if (err)
550                 goto err_unlock;
551
552         list_add_tail(&fc->entry, &fuse_conn_list);
553         sb->s_root = root_dentry;
554         fc->connected = 1;
555         file->private_data = fuse_conn_get(fc);
556         mutex_unlock(&fuse_mutex);
557         /*
558          * atomic_dec_and_test() in fput() provides the necessary
559          * memory barrier for file->private_data to be visible on all
560          * CPUs after this
561          */
562         fput(file);
563
564         fuse_send_init(fc, init_req);
565
566         return 0;
567
568  err_unlock:
569         mutex_unlock(&fuse_mutex);
570         fuse_request_free(init_req);
571  err_put_root:
572         dput(root_dentry);
573  err:
574         fput(file);
575         fuse_conn_put(fc);
576         return err;
577 }
578
579 static int fuse_get_sb(struct file_system_type *fs_type,
580                        int flags, const char *dev_name,
581                        void *raw_data, struct vfsmount *mnt)
582 {
583         return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
584 }
585
586 static struct file_system_type fuse_fs_type = {
587         .owner          = THIS_MODULE,
588         .name           = "fuse",
589         .get_sb         = fuse_get_sb,
590         .kill_sb        = kill_anon_super,
591 };
592
593 static decl_subsys(fuse, NULL, NULL);
594 static decl_subsys(connections, NULL, NULL);
595
596 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
597                                  unsigned long flags)
598 {
599         struct inode * inode = foo;
600
601         if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
602             SLAB_CTOR_CONSTRUCTOR)
603                 inode_init_once(inode);
604 }
605
606 static int __init fuse_fs_init(void)
607 {
608         int err;
609
610         err = register_filesystem(&fuse_fs_type);
611         if (err)
612                 printk("fuse: failed to register filesystem\n");
613         else {
614                 fuse_inode_cachep = kmem_cache_create("fuse_inode",
615                                                       sizeof(struct fuse_inode),
616                                                       0, SLAB_HWCACHE_ALIGN,
617                                                       fuse_inode_init_once, NULL);
618                 if (!fuse_inode_cachep) {
619                         unregister_filesystem(&fuse_fs_type);
620                         err = -ENOMEM;
621                 }
622         }
623
624         return err;
625 }
626
627 static void fuse_fs_cleanup(void)
628 {
629         unregister_filesystem(&fuse_fs_type);
630         kmem_cache_destroy(fuse_inode_cachep);
631 }
632
633 static int fuse_sysfs_init(void)
634 {
635         int err;
636
637         kset_set_kset_s(&fuse_subsys, fs_subsys);
638         err = subsystem_register(&fuse_subsys);
639         if (err)
640                 goto out_err;
641
642         kset_set_kset_s(&connections_subsys, fuse_subsys);
643         err = subsystem_register(&connections_subsys);
644         if (err)
645                 goto out_fuse_unregister;
646
647         return 0;
648
649  out_fuse_unregister:
650         subsystem_unregister(&fuse_subsys);
651  out_err:
652         return err;
653 }
654
655 static void fuse_sysfs_cleanup(void)
656 {
657         subsystem_unregister(&connections_subsys);
658         subsystem_unregister(&fuse_subsys);
659 }
660
661 static int __init fuse_init(void)
662 {
663         int res;
664
665         printk("fuse init (API version %i.%i)\n",
666                FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
667
668         INIT_LIST_HEAD(&fuse_conn_list);
669         res = fuse_fs_init();
670         if (res)
671                 goto err;
672
673         res = fuse_dev_init();
674         if (res)
675                 goto err_fs_cleanup;
676
677         res = fuse_sysfs_init();
678         if (res)
679                 goto err_dev_cleanup;
680
681         res = fuse_ctl_init();
682         if (res)
683                 goto err_sysfs_cleanup;
684
685         return 0;
686
687  err_sysfs_cleanup:
688         fuse_sysfs_cleanup();
689  err_dev_cleanup:
690         fuse_dev_cleanup();
691  err_fs_cleanup:
692         fuse_fs_cleanup();
693  err:
694         return res;
695 }
696
697 static void __exit fuse_exit(void)
698 {
699         printk(KERN_DEBUG "fuse exit\n");
700
701         fuse_ctl_cleanup();
702         fuse_sysfs_cleanup();
703         fuse_fs_cleanup();
704         fuse_dev_cleanup();
705 }
706
707 module_init(fuse_init);
708 module_exit(fuse_exit);