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