]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/fuse/dev.c
fuse: add reference counting to fuse_file
[linux-2.6-omap-h63xx.git] / fs / fuse / dev.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/init.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/uio.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/slab.h>
19
20 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
21
22 static struct kmem_cache *fuse_req_cachep;
23
24 static struct fuse_conn *fuse_get_conn(struct file *file)
25 {
26         /*
27          * Lockless access is OK, because file->private data is set
28          * once during mount and is valid until the file is released.
29          */
30         return file->private_data;
31 }
32
33 static void fuse_request_init(struct fuse_req *req)
34 {
35         memset(req, 0, sizeof(*req));
36         INIT_LIST_HEAD(&req->list);
37         INIT_LIST_HEAD(&req->intr_entry);
38         init_waitqueue_head(&req->waitq);
39         atomic_set(&req->count, 1);
40 }
41
42 struct fuse_req *fuse_request_alloc(void)
43 {
44         struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
45         if (req)
46                 fuse_request_init(req);
47         return req;
48 }
49
50 void fuse_request_free(struct fuse_req *req)
51 {
52         kmem_cache_free(fuse_req_cachep, req);
53 }
54
55 static void block_sigs(sigset_t *oldset)
56 {
57         sigset_t mask;
58
59         siginitsetinv(&mask, sigmask(SIGKILL));
60         sigprocmask(SIG_BLOCK, &mask, oldset);
61 }
62
63 static void restore_sigs(sigset_t *oldset)
64 {
65         sigprocmask(SIG_SETMASK, oldset, NULL);
66 }
67
68 static void __fuse_get_request(struct fuse_req *req)
69 {
70         atomic_inc(&req->count);
71 }
72
73 /* Must be called with > 1 refcount */
74 static void __fuse_put_request(struct fuse_req *req)
75 {
76         BUG_ON(atomic_read(&req->count) < 2);
77         atomic_dec(&req->count);
78 }
79
80 static void fuse_req_init_context(struct fuse_req *req)
81 {
82         req->in.h.uid = current->fsuid;
83         req->in.h.gid = current->fsgid;
84         req->in.h.pid = current->pid;
85 }
86
87 struct fuse_req *fuse_get_req(struct fuse_conn *fc)
88 {
89         struct fuse_req *req;
90         sigset_t oldset;
91         int intr;
92         int err;
93
94         atomic_inc(&fc->num_waiting);
95         block_sigs(&oldset);
96         intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
97         restore_sigs(&oldset);
98         err = -EINTR;
99         if (intr)
100                 goto out;
101
102         err = -ENOTCONN;
103         if (!fc->connected)
104                 goto out;
105
106         req = fuse_request_alloc();
107         err = -ENOMEM;
108         if (!req)
109                 goto out;
110
111         fuse_req_init_context(req);
112         req->waiting = 1;
113         return req;
114
115  out:
116         atomic_dec(&fc->num_waiting);
117         return ERR_PTR(err);
118 }
119
120 /*
121  * Return request in fuse_file->reserved_req.  However that may
122  * currently be in use.  If that is the case, wait for it to become
123  * available.
124  */
125 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
126                                          struct file *file)
127 {
128         struct fuse_req *req = NULL;
129         struct fuse_file *ff = file->private_data;
130
131         do {
132                 wait_event(fc->reserved_req_waitq, ff->reserved_req);
133                 spin_lock(&fc->lock);
134                 if (ff->reserved_req) {
135                         req = ff->reserved_req;
136                         ff->reserved_req = NULL;
137                         get_file(file);
138                         req->stolen_file = file;
139                 }
140                 spin_unlock(&fc->lock);
141         } while (!req);
142
143         return req;
144 }
145
146 /*
147  * Put stolen request back into fuse_file->reserved_req
148  */
149 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
150 {
151         struct file *file = req->stolen_file;
152         struct fuse_file *ff = file->private_data;
153
154         spin_lock(&fc->lock);
155         fuse_request_init(req);
156         BUG_ON(ff->reserved_req);
157         ff->reserved_req = req;
158         wake_up_all(&fc->reserved_req_waitq);
159         spin_unlock(&fc->lock);
160         fput(file);
161 }
162
163 /*
164  * Gets a requests for a file operation, always succeeds
165  *
166  * This is used for sending the FLUSH request, which must get to
167  * userspace, due to POSIX locks which may need to be unlocked.
168  *
169  * If allocation fails due to OOM, use the reserved request in
170  * fuse_file.
171  *
172  * This is very unlikely to deadlock accidentally, since the
173  * filesystem should not have it's own file open.  If deadlock is
174  * intentional, it can still be broken by "aborting" the filesystem.
175  */
176 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
177 {
178         struct fuse_req *req;
179
180         atomic_inc(&fc->num_waiting);
181         wait_event(fc->blocked_waitq, !fc->blocked);
182         req = fuse_request_alloc();
183         if (!req)
184                 req = get_reserved_req(fc, file);
185
186         fuse_req_init_context(req);
187         req->waiting = 1;
188         return req;
189 }
190
191 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
192 {
193         if (atomic_dec_and_test(&req->count)) {
194                 if (req->waiting)
195                         atomic_dec(&fc->num_waiting);
196
197                 if (req->stolen_file)
198                         put_reserved_req(fc, req);
199                 else
200                         fuse_request_free(req);
201         }
202 }
203
204 /*
205  * This function is called when a request is finished.  Either a reply
206  * has arrived or it was aborted (and not yet sent) or some error
207  * occurred during communication with userspace, or the device file
208  * was closed.  The requester thread is woken up (if still waiting),
209  * the 'end' callback is called if given, else the reference to the
210  * request is released
211  *
212  * Called with fc->lock, unlocks it
213  */
214 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
215         __releases(fc->lock)
216 {
217         void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
218         req->end = NULL;
219         list_del(&req->list);
220         list_del(&req->intr_entry);
221         req->state = FUSE_REQ_FINISHED;
222         if (req->background) {
223                 if (fc->num_background == FUSE_MAX_BACKGROUND) {
224                         fc->blocked = 0;
225                         wake_up_all(&fc->blocked_waitq);
226                 }
227                 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
228                         clear_bdi_congested(&fc->bdi, READ);
229                         clear_bdi_congested(&fc->bdi, WRITE);
230                 }
231                 fc->num_background--;
232         }
233         spin_unlock(&fc->lock);
234         dput(req->dentry);
235         mntput(req->vfsmount);
236         wake_up(&req->waitq);
237         if (end)
238                 end(fc, req);
239         else
240                 fuse_put_request(fc, req);
241 }
242
243 static void wait_answer_interruptible(struct fuse_conn *fc,
244                                       struct fuse_req *req)
245 {
246         if (signal_pending(current))
247                 return;
248
249         spin_unlock(&fc->lock);
250         wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
251         spin_lock(&fc->lock);
252 }
253
254 static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
255 {
256         list_add_tail(&req->intr_entry, &fc->interrupts);
257         wake_up(&fc->waitq);
258         kill_fasync(&fc->fasync, SIGIO, POLL_IN);
259 }
260
261 /* Called with fc->lock held.  Releases, and then reacquires it. */
262 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
263 {
264         if (!fc->no_interrupt) {
265                 /* Any signal may interrupt this */
266                 wait_answer_interruptible(fc, req);
267
268                 if (req->aborted)
269                         goto aborted;
270                 if (req->state == FUSE_REQ_FINISHED)
271                         return;
272
273                 req->interrupted = 1;
274                 if (req->state == FUSE_REQ_SENT)
275                         queue_interrupt(fc, req);
276         }
277
278         if (req->force) {
279                 spin_unlock(&fc->lock);
280                 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
281                 spin_lock(&fc->lock);
282         } else {
283                 sigset_t oldset;
284
285                 /* Only fatal signals may interrupt this */
286                 block_sigs(&oldset);
287                 wait_answer_interruptible(fc, req);
288                 restore_sigs(&oldset);
289         }
290
291         if (req->aborted)
292                 goto aborted;
293         if (req->state == FUSE_REQ_FINISHED)
294                 return;
295
296         req->out.h.error = -EINTR;
297         req->aborted = 1;
298
299  aborted:
300         if (req->locked) {
301                 /* This is uninterruptible sleep, because data is
302                    being copied to/from the buffers of req.  During
303                    locked state, there mustn't be any filesystem
304                    operation (e.g. page fault), since that could lead
305                    to deadlock */
306                 spin_unlock(&fc->lock);
307                 wait_event(req->waitq, !req->locked);
308                 spin_lock(&fc->lock);
309         }
310         if (req->state == FUSE_REQ_PENDING) {
311                 list_del(&req->list);
312                 __fuse_put_request(req);
313         } else if (req->state == FUSE_REQ_SENT) {
314                 spin_unlock(&fc->lock);
315                 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
316                 spin_lock(&fc->lock);
317         }
318 }
319
320 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
321 {
322         unsigned nbytes = 0;
323         unsigned i;
324
325         for (i = 0; i < numargs; i++)
326                 nbytes += args[i].size;
327
328         return nbytes;
329 }
330
331 static u64 fuse_get_unique(struct fuse_conn *fc)
332  {
333         fc->reqctr++;
334         /* zero is special */
335         if (fc->reqctr == 0)
336                 fc->reqctr = 1;
337
338         return fc->reqctr;
339 }
340
341 static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
342 {
343         req->in.h.unique = fuse_get_unique(fc);
344         req->in.h.len = sizeof(struct fuse_in_header) +
345                 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
346         list_add_tail(&req->list, &fc->pending);
347         req->state = FUSE_REQ_PENDING;
348         if (!req->waiting) {
349                 req->waiting = 1;
350                 atomic_inc(&fc->num_waiting);
351         }
352         wake_up(&fc->waitq);
353         kill_fasync(&fc->fasync, SIGIO, POLL_IN);
354 }
355
356 void request_send(struct fuse_conn *fc, struct fuse_req *req)
357 {
358         req->isreply = 1;
359         spin_lock(&fc->lock);
360         if (!fc->connected)
361                 req->out.h.error = -ENOTCONN;
362         else if (fc->conn_error)
363                 req->out.h.error = -ECONNREFUSED;
364         else {
365                 queue_request(fc, req);
366                 /* acquire extra reference, since request is still needed
367                    after request_end() */
368                 __fuse_get_request(req);
369
370                 request_wait_answer(fc, req);
371         }
372         spin_unlock(&fc->lock);
373 }
374
375 static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
376 {
377         spin_lock(&fc->lock);
378         if (fc->connected) {
379                 req->background = 1;
380                 fc->num_background++;
381                 if (fc->num_background == FUSE_MAX_BACKGROUND)
382                         fc->blocked = 1;
383                 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
384                         set_bdi_congested(&fc->bdi, READ);
385                         set_bdi_congested(&fc->bdi, WRITE);
386                 }
387
388                 queue_request(fc, req);
389                 spin_unlock(&fc->lock);
390         } else {
391                 req->out.h.error = -ENOTCONN;
392                 request_end(fc, req);
393         }
394 }
395
396 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
397 {
398         req->isreply = 0;
399         request_send_nowait(fc, req);
400 }
401
402 void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
403 {
404         req->isreply = 1;
405         request_send_nowait(fc, req);
406 }
407
408 /*
409  * Lock the request.  Up to the next unlock_request() there mustn't be
410  * anything that could cause a page-fault.  If the request was already
411  * aborted bail out.
412  */
413 static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
414 {
415         int err = 0;
416         if (req) {
417                 spin_lock(&fc->lock);
418                 if (req->aborted)
419                         err = -ENOENT;
420                 else
421                         req->locked = 1;
422                 spin_unlock(&fc->lock);
423         }
424         return err;
425 }
426
427 /*
428  * Unlock request.  If it was aborted during being locked, the
429  * requester thread is currently waiting for it to be unlocked, so
430  * wake it up.
431  */
432 static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
433 {
434         if (req) {
435                 spin_lock(&fc->lock);
436                 req->locked = 0;
437                 if (req->aborted)
438                         wake_up(&req->waitq);
439                 spin_unlock(&fc->lock);
440         }
441 }
442
443 struct fuse_copy_state {
444         struct fuse_conn *fc;
445         int write;
446         struct fuse_req *req;
447         const struct iovec *iov;
448         unsigned long nr_segs;
449         unsigned long seglen;
450         unsigned long addr;
451         struct page *pg;
452         void *mapaddr;
453         void *buf;
454         unsigned len;
455 };
456
457 static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
458                            int write, struct fuse_req *req,
459                            const struct iovec *iov, unsigned long nr_segs)
460 {
461         memset(cs, 0, sizeof(*cs));
462         cs->fc = fc;
463         cs->write = write;
464         cs->req = req;
465         cs->iov = iov;
466         cs->nr_segs = nr_segs;
467 }
468
469 /* Unmap and put previous page of userspace buffer */
470 static void fuse_copy_finish(struct fuse_copy_state *cs)
471 {
472         if (cs->mapaddr) {
473                 kunmap_atomic(cs->mapaddr, KM_USER0);
474                 if (cs->write) {
475                         flush_dcache_page(cs->pg);
476                         set_page_dirty_lock(cs->pg);
477                 }
478                 put_page(cs->pg);
479                 cs->mapaddr = NULL;
480         }
481 }
482
483 /*
484  * Get another pagefull of userspace buffer, and map it to kernel
485  * address space, and lock request
486  */
487 static int fuse_copy_fill(struct fuse_copy_state *cs)
488 {
489         unsigned long offset;
490         int err;
491
492         unlock_request(cs->fc, cs->req);
493         fuse_copy_finish(cs);
494         if (!cs->seglen) {
495                 BUG_ON(!cs->nr_segs);
496                 cs->seglen = cs->iov[0].iov_len;
497                 cs->addr = (unsigned long) cs->iov[0].iov_base;
498                 cs->iov ++;
499                 cs->nr_segs --;
500         }
501         down_read(&current->mm->mmap_sem);
502         err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
503                              &cs->pg, NULL);
504         up_read(&current->mm->mmap_sem);
505         if (err < 0)
506                 return err;
507         BUG_ON(err != 1);
508         offset = cs->addr % PAGE_SIZE;
509         cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
510         cs->buf = cs->mapaddr + offset;
511         cs->len = min(PAGE_SIZE - offset, cs->seglen);
512         cs->seglen -= cs->len;
513         cs->addr += cs->len;
514
515         return lock_request(cs->fc, cs->req);
516 }
517
518 /* Do as much copy to/from userspace buffer as we can */
519 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
520 {
521         unsigned ncpy = min(*size, cs->len);
522         if (val) {
523                 if (cs->write)
524                         memcpy(cs->buf, *val, ncpy);
525                 else
526                         memcpy(*val, cs->buf, ncpy);
527                 *val += ncpy;
528         }
529         *size -= ncpy;
530         cs->len -= ncpy;
531         cs->buf += ncpy;
532         return ncpy;
533 }
534
535 /*
536  * Copy a page in the request to/from the userspace buffer.  Must be
537  * done atomically
538  */
539 static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
540                           unsigned offset, unsigned count, int zeroing)
541 {
542         if (page && zeroing && count < PAGE_SIZE) {
543                 void *mapaddr = kmap_atomic(page, KM_USER1);
544                 memset(mapaddr, 0, PAGE_SIZE);
545                 kunmap_atomic(mapaddr, KM_USER1);
546         }
547         while (count) {
548                 int err;
549                 if (!cs->len && (err = fuse_copy_fill(cs)))
550                         return err;
551                 if (page) {
552                         void *mapaddr = kmap_atomic(page, KM_USER1);
553                         void *buf = mapaddr + offset;
554                         offset += fuse_copy_do(cs, &buf, &count);
555                         kunmap_atomic(mapaddr, KM_USER1);
556                 } else
557                         offset += fuse_copy_do(cs, NULL, &count);
558         }
559         if (page && !cs->write)
560                 flush_dcache_page(page);
561         return 0;
562 }
563
564 /* Copy pages in the request to/from userspace buffer */
565 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
566                            int zeroing)
567 {
568         unsigned i;
569         struct fuse_req *req = cs->req;
570         unsigned offset = req->page_offset;
571         unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
572
573         for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
574                 struct page *page = req->pages[i];
575                 int err = fuse_copy_page(cs, page, offset, count, zeroing);
576                 if (err)
577                         return err;
578
579                 nbytes -= count;
580                 count = min(nbytes, (unsigned) PAGE_SIZE);
581                 offset = 0;
582         }
583         return 0;
584 }
585
586 /* Copy a single argument in the request to/from userspace buffer */
587 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
588 {
589         while (size) {
590                 int err;
591                 if (!cs->len && (err = fuse_copy_fill(cs)))
592                         return err;
593                 fuse_copy_do(cs, &val, &size);
594         }
595         return 0;
596 }
597
598 /* Copy request arguments to/from userspace buffer */
599 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
600                           unsigned argpages, struct fuse_arg *args,
601                           int zeroing)
602 {
603         int err = 0;
604         unsigned i;
605
606         for (i = 0; !err && i < numargs; i++)  {
607                 struct fuse_arg *arg = &args[i];
608                 if (i == numargs - 1 && argpages)
609                         err = fuse_copy_pages(cs, arg->size, zeroing);
610                 else
611                         err = fuse_copy_one(cs, arg->value, arg->size);
612         }
613         return err;
614 }
615
616 static int request_pending(struct fuse_conn *fc)
617 {
618         return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
619 }
620
621 /* Wait until a request is available on the pending list */
622 static void request_wait(struct fuse_conn *fc)
623 {
624         DECLARE_WAITQUEUE(wait, current);
625
626         add_wait_queue_exclusive(&fc->waitq, &wait);
627         while (fc->connected && !request_pending(fc)) {
628                 set_current_state(TASK_INTERRUPTIBLE);
629                 if (signal_pending(current))
630                         break;
631
632                 spin_unlock(&fc->lock);
633                 schedule();
634                 spin_lock(&fc->lock);
635         }
636         set_current_state(TASK_RUNNING);
637         remove_wait_queue(&fc->waitq, &wait);
638 }
639
640 /*
641  * Transfer an interrupt request to userspace
642  *
643  * Unlike other requests this is assembled on demand, without a need
644  * to allocate a separate fuse_req structure.
645  *
646  * Called with fc->lock held, releases it
647  */
648 static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
649                                const struct iovec *iov, unsigned long nr_segs)
650         __releases(fc->lock)
651 {
652         struct fuse_copy_state cs;
653         struct fuse_in_header ih;
654         struct fuse_interrupt_in arg;
655         unsigned reqsize = sizeof(ih) + sizeof(arg);
656         int err;
657
658         list_del_init(&req->intr_entry);
659         req->intr_unique = fuse_get_unique(fc);
660         memset(&ih, 0, sizeof(ih));
661         memset(&arg, 0, sizeof(arg));
662         ih.len = reqsize;
663         ih.opcode = FUSE_INTERRUPT;
664         ih.unique = req->intr_unique;
665         arg.unique = req->in.h.unique;
666
667         spin_unlock(&fc->lock);
668         if (iov_length(iov, nr_segs) < reqsize)
669                 return -EINVAL;
670
671         fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
672         err = fuse_copy_one(&cs, &ih, sizeof(ih));
673         if (!err)
674                 err = fuse_copy_one(&cs, &arg, sizeof(arg));
675         fuse_copy_finish(&cs);
676
677         return err ? err : reqsize;
678 }
679
680 /*
681  * Read a single request into the userspace filesystem's buffer.  This
682  * function waits until a request is available, then removes it from
683  * the pending list and copies request data to userspace buffer.  If
684  * no reply is needed (FORGET) or request has been aborted or there
685  * was an error during the copying then it's finished by calling
686  * request_end().  Otherwise add it to the processing list, and set
687  * the 'sent' flag.
688  */
689 static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
690                               unsigned long nr_segs, loff_t pos)
691 {
692         int err;
693         struct fuse_req *req;
694         struct fuse_in *in;
695         struct fuse_copy_state cs;
696         unsigned reqsize;
697         struct file *file = iocb->ki_filp;
698         struct fuse_conn *fc = fuse_get_conn(file);
699         if (!fc)
700                 return -EPERM;
701
702  restart:
703         spin_lock(&fc->lock);
704         err = -EAGAIN;
705         if ((file->f_flags & O_NONBLOCK) && fc->connected &&
706             !request_pending(fc))
707                 goto err_unlock;
708
709         request_wait(fc);
710         err = -ENODEV;
711         if (!fc->connected)
712                 goto err_unlock;
713         err = -ERESTARTSYS;
714         if (!request_pending(fc))
715                 goto err_unlock;
716
717         if (!list_empty(&fc->interrupts)) {
718                 req = list_entry(fc->interrupts.next, struct fuse_req,
719                                  intr_entry);
720                 return fuse_read_interrupt(fc, req, iov, nr_segs);
721         }
722
723         req = list_entry(fc->pending.next, struct fuse_req, list);
724         req->state = FUSE_REQ_READING;
725         list_move(&req->list, &fc->io);
726
727         in = &req->in;
728         reqsize = in->h.len;
729         /* If request is too large, reply with an error and restart the read */
730         if (iov_length(iov, nr_segs) < reqsize) {
731                 req->out.h.error = -EIO;
732                 /* SETXATTR is special, since it may contain too large data */
733                 if (in->h.opcode == FUSE_SETXATTR)
734                         req->out.h.error = -E2BIG;
735                 request_end(fc, req);
736                 goto restart;
737         }
738         spin_unlock(&fc->lock);
739         fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
740         err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
741         if (!err)
742                 err = fuse_copy_args(&cs, in->numargs, in->argpages,
743                                      (struct fuse_arg *) in->args, 0);
744         fuse_copy_finish(&cs);
745         spin_lock(&fc->lock);
746         req->locked = 0;
747         if (!err && req->aborted)
748                 err = -ENOENT;
749         if (err) {
750                 if (!req->aborted)
751                         req->out.h.error = -EIO;
752                 request_end(fc, req);
753                 return err;
754         }
755         if (!req->isreply)
756                 request_end(fc, req);
757         else {
758                 req->state = FUSE_REQ_SENT;
759                 list_move_tail(&req->list, &fc->processing);
760                 if (req->interrupted)
761                         queue_interrupt(fc, req);
762                 spin_unlock(&fc->lock);
763         }
764         return reqsize;
765
766  err_unlock:
767         spin_unlock(&fc->lock);
768         return err;
769 }
770
771 /* Look up request on processing list by unique ID */
772 static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
773 {
774         struct list_head *entry;
775
776         list_for_each(entry, &fc->processing) {
777                 struct fuse_req *req;
778                 req = list_entry(entry, struct fuse_req, list);
779                 if (req->in.h.unique == unique || req->intr_unique == unique)
780                         return req;
781         }
782         return NULL;
783 }
784
785 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
786                          unsigned nbytes)
787 {
788         unsigned reqsize = sizeof(struct fuse_out_header);
789
790         if (out->h.error)
791                 return nbytes != reqsize ? -EINVAL : 0;
792
793         reqsize += len_args(out->numargs, out->args);
794
795         if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
796                 return -EINVAL;
797         else if (reqsize > nbytes) {
798                 struct fuse_arg *lastarg = &out->args[out->numargs-1];
799                 unsigned diffsize = reqsize - nbytes;
800                 if (diffsize > lastarg->size)
801                         return -EINVAL;
802                 lastarg->size -= diffsize;
803         }
804         return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
805                               out->page_zeroing);
806 }
807
808 /*
809  * Write a single reply to a request.  First the header is copied from
810  * the write buffer.  The request is then searched on the processing
811  * list by the unique ID found in the header.  If found, then remove
812  * it from the list and copy the rest of the buffer to the request.
813  * The request is finished by calling request_end()
814  */
815 static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
816                                unsigned long nr_segs, loff_t pos)
817 {
818         int err;
819         unsigned nbytes = iov_length(iov, nr_segs);
820         struct fuse_req *req;
821         struct fuse_out_header oh;
822         struct fuse_copy_state cs;
823         struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
824         if (!fc)
825                 return -EPERM;
826
827         fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
828         if (nbytes < sizeof(struct fuse_out_header))
829                 return -EINVAL;
830
831         err = fuse_copy_one(&cs, &oh, sizeof(oh));
832         if (err)
833                 goto err_finish;
834         err = -EINVAL;
835         if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
836             oh.len != nbytes)
837                 goto err_finish;
838
839         spin_lock(&fc->lock);
840         err = -ENOENT;
841         if (!fc->connected)
842                 goto err_unlock;
843
844         req = request_find(fc, oh.unique);
845         if (!req)
846                 goto err_unlock;
847
848         if (req->aborted) {
849                 spin_unlock(&fc->lock);
850                 fuse_copy_finish(&cs);
851                 spin_lock(&fc->lock);
852                 request_end(fc, req);
853                 return -ENOENT;
854         }
855         /* Is it an interrupt reply? */
856         if (req->intr_unique == oh.unique) {
857                 err = -EINVAL;
858                 if (nbytes != sizeof(struct fuse_out_header))
859                         goto err_unlock;
860
861                 if (oh.error == -ENOSYS)
862                         fc->no_interrupt = 1;
863                 else if (oh.error == -EAGAIN)
864                         queue_interrupt(fc, req);
865
866                 spin_unlock(&fc->lock);
867                 fuse_copy_finish(&cs);
868                 return nbytes;
869         }
870
871         req->state = FUSE_REQ_WRITING;
872         list_move(&req->list, &fc->io);
873         req->out.h = oh;
874         req->locked = 1;
875         cs.req = req;
876         spin_unlock(&fc->lock);
877
878         err = copy_out_args(&cs, &req->out, nbytes);
879         fuse_copy_finish(&cs);
880
881         spin_lock(&fc->lock);
882         req->locked = 0;
883         if (!err) {
884                 if (req->aborted)
885                         err = -ENOENT;
886         } else if (!req->aborted)
887                 req->out.h.error = -EIO;
888         request_end(fc, req);
889
890         return err ? err : nbytes;
891
892  err_unlock:
893         spin_unlock(&fc->lock);
894  err_finish:
895         fuse_copy_finish(&cs);
896         return err;
897 }
898
899 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
900 {
901         unsigned mask = POLLOUT | POLLWRNORM;
902         struct fuse_conn *fc = fuse_get_conn(file);
903         if (!fc)
904                 return POLLERR;
905
906         poll_wait(file, &fc->waitq, wait);
907
908         spin_lock(&fc->lock);
909         if (!fc->connected)
910                 mask = POLLERR;
911         else if (request_pending(fc))
912                 mask |= POLLIN | POLLRDNORM;
913         spin_unlock(&fc->lock);
914
915         return mask;
916 }
917
918 /*
919  * Abort all requests on the given list (pending or processing)
920  *
921  * This function releases and reacquires fc->lock
922  */
923 static void end_requests(struct fuse_conn *fc, struct list_head *head)
924 {
925         while (!list_empty(head)) {
926                 struct fuse_req *req;
927                 req = list_entry(head->next, struct fuse_req, list);
928                 req->out.h.error = -ECONNABORTED;
929                 request_end(fc, req);
930                 spin_lock(&fc->lock);
931         }
932 }
933
934 /*
935  * Abort requests under I/O
936  *
937  * The requests are set to aborted and finished, and the request
938  * waiter is woken up.  This will make request_wait_answer() wait
939  * until the request is unlocked and then return.
940  *
941  * If the request is asynchronous, then the end function needs to be
942  * called after waiting for the request to be unlocked (if it was
943  * locked).
944  */
945 static void end_io_requests(struct fuse_conn *fc)
946 {
947         while (!list_empty(&fc->io)) {
948                 struct fuse_req *req =
949                         list_entry(fc->io.next, struct fuse_req, list);
950                 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
951
952                 req->aborted = 1;
953                 req->out.h.error = -ECONNABORTED;
954                 req->state = FUSE_REQ_FINISHED;
955                 list_del_init(&req->list);
956                 wake_up(&req->waitq);
957                 if (end) {
958                         req->end = NULL;
959                         /* The end function will consume this reference */
960                         __fuse_get_request(req);
961                         spin_unlock(&fc->lock);
962                         wait_event(req->waitq, !req->locked);
963                         end(fc, req);
964                         spin_lock(&fc->lock);
965                 }
966         }
967 }
968
969 /*
970  * Abort all requests.
971  *
972  * Emergency exit in case of a malicious or accidental deadlock, or
973  * just a hung filesystem.
974  *
975  * The same effect is usually achievable through killing the
976  * filesystem daemon and all users of the filesystem.  The exception
977  * is the combination of an asynchronous request and the tricky
978  * deadlock (see Documentation/filesystems/fuse.txt).
979  *
980  * During the aborting, progression of requests from the pending and
981  * processing lists onto the io list, and progression of new requests
982  * onto the pending list is prevented by req->connected being false.
983  *
984  * Progression of requests under I/O to the processing list is
985  * prevented by the req->aborted flag being true for these requests.
986  * For this reason requests on the io list must be aborted first.
987  */
988 void fuse_abort_conn(struct fuse_conn *fc)
989 {
990         spin_lock(&fc->lock);
991         if (fc->connected) {
992                 fc->connected = 0;
993                 fc->blocked = 0;
994                 end_io_requests(fc);
995                 end_requests(fc, &fc->pending);
996                 end_requests(fc, &fc->processing);
997                 wake_up_all(&fc->waitq);
998                 wake_up_all(&fc->blocked_waitq);
999                 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
1000         }
1001         spin_unlock(&fc->lock);
1002 }
1003
1004 static int fuse_dev_release(struct inode *inode, struct file *file)
1005 {
1006         struct fuse_conn *fc = fuse_get_conn(file);
1007         if (fc) {
1008                 spin_lock(&fc->lock);
1009                 fc->connected = 0;
1010                 end_requests(fc, &fc->pending);
1011                 end_requests(fc, &fc->processing);
1012                 spin_unlock(&fc->lock);
1013                 fasync_helper(-1, file, 0, &fc->fasync);
1014                 fuse_conn_put(fc);
1015         }
1016
1017         return 0;
1018 }
1019
1020 static int fuse_dev_fasync(int fd, struct file *file, int on)
1021 {
1022         struct fuse_conn *fc = fuse_get_conn(file);
1023         if (!fc)
1024                 return -EPERM;
1025
1026         /* No locking - fasync_helper does its own locking */
1027         return fasync_helper(fd, file, on, &fc->fasync);
1028 }
1029
1030 const struct file_operations fuse_dev_operations = {
1031         .owner          = THIS_MODULE,
1032         .llseek         = no_llseek,
1033         .read           = do_sync_read,
1034         .aio_read       = fuse_dev_read,
1035         .write          = do_sync_write,
1036         .aio_write      = fuse_dev_write,
1037         .poll           = fuse_dev_poll,
1038         .release        = fuse_dev_release,
1039         .fasync         = fuse_dev_fasync,
1040 };
1041
1042 static struct miscdevice fuse_miscdevice = {
1043         .minor = FUSE_MINOR,
1044         .name  = "fuse",
1045         .fops = &fuse_dev_operations,
1046 };
1047
1048 int __init fuse_dev_init(void)
1049 {
1050         int err = -ENOMEM;
1051         fuse_req_cachep = kmem_cache_create("fuse_request",
1052                                             sizeof(struct fuse_req),
1053                                             0, 0, NULL);
1054         if (!fuse_req_cachep)
1055                 goto out;
1056
1057         err = misc_register(&fuse_miscdevice);
1058         if (err)
1059                 goto out_cache_clean;
1060
1061         return 0;
1062
1063  out_cache_clean:
1064         kmem_cache_destroy(fuse_req_cachep);
1065  out:
1066         return err;
1067 }
1068
1069 void fuse_dev_cleanup(void)
1070 {
1071         misc_deregister(&fuse_miscdevice);
1072         kmem_cache_destroy(fuse_req_cachep);
1073 }