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