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