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