Linux-libre 2.6.27.45-gnu1
[librecmc/linux-libre.git] / fs / fuse / dev.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2006  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7 */
8
9 #include "fuse_i.h"
10
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/uio.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/slab.h>
19
20 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
21
22 static struct kmem_cache *fuse_req_cachep;
23
24 static struct fuse_conn *fuse_get_conn(struct file *file)
25 {
26         /*
27          * Lockless access is OK, because file->private data is set
28          * once during mount and is valid until the file is released.
29          */
30         return file->private_data;
31 }
32
33 static void fuse_request_init(struct fuse_req *req)
34 {
35         memset(req, 0, sizeof(*req));
36         INIT_LIST_HEAD(&req->list);
37         INIT_LIST_HEAD(&req->intr_entry);
38         init_waitqueue_head(&req->waitq);
39         atomic_set(&req->count, 1);
40 }
41
42 struct fuse_req *fuse_request_alloc(void)
43 {
44         struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
45         if (req)
46                 fuse_request_init(req);
47         return req;
48 }
49
50 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                     fc->connected) {
286                         clear_bdi_congested(&fc->bdi, READ);
287                         clear_bdi_congested(&fc->bdi, WRITE);
288                 }
289                 fc->num_background--;
290                 fc->active_background--;
291                 flush_bg_queue(fc);
292         }
293         spin_unlock(&fc->lock);
294         wake_up(&req->waitq);
295         if (end)
296                 end(fc, req);
297         else
298                 fuse_put_request(fc, req);
299 }
300
301 static void wait_answer_interruptible(struct fuse_conn *fc,
302                                       struct fuse_req *req)
303         __releases(fc->lock) __acquires(fc->lock)
304 {
305         if (signal_pending(current))
306                 return;
307
308         spin_unlock(&fc->lock);
309         wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
310         spin_lock(&fc->lock);
311 }
312
313 static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
314 {
315         list_add_tail(&req->intr_entry, &fc->interrupts);
316         wake_up(&fc->waitq);
317         kill_fasync(&fc->fasync, SIGIO, POLL_IN);
318 }
319
320 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
321         __releases(fc->lock) __acquires(fc->lock)
322 {
323         if (!fc->no_interrupt) {
324                 /* Any signal may interrupt this */
325                 wait_answer_interruptible(fc, req);
326
327                 if (req->aborted)
328                         goto aborted;
329                 if (req->state == FUSE_REQ_FINISHED)
330                         return;
331
332                 req->interrupted = 1;
333                 if (req->state == FUSE_REQ_SENT)
334                         queue_interrupt(fc, req);
335         }
336
337         if (!req->force) {
338                 sigset_t oldset;
339
340                 /* Only fatal signals may interrupt this */
341                 block_sigs(&oldset);
342                 wait_answer_interruptible(fc, req);
343                 restore_sigs(&oldset);
344
345                 if (req->aborted)
346                         goto aborted;
347                 if (req->state == FUSE_REQ_FINISHED)
348                         return;
349
350                 /* Request is not yet in userspace, bail out */
351                 if (req->state == FUSE_REQ_PENDING) {
352                         list_del(&req->list);
353                         __fuse_put_request(req);
354                         req->out.h.error = -EINTR;
355                         return;
356                 }
357         }
358
359         /*
360          * Either request is already in userspace, or it was forced.
361          * Wait it out.
362          */
363         spin_unlock(&fc->lock);
364         wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
365         spin_lock(&fc->lock);
366
367         if (!req->aborted)
368                 return;
369
370  aborted:
371         BUG_ON(req->state != FUSE_REQ_FINISHED);
372         if (req->locked) {
373                 /* This is uninterruptible sleep, because data is
374                    being copied to/from the buffers of req.  During
375                    locked state, there mustn't be any filesystem
376                    operation (e.g. page fault), since that could lead
377                    to deadlock */
378                 spin_unlock(&fc->lock);
379                 wait_event(req->waitq, !req->locked);
380                 spin_lock(&fc->lock);
381         }
382 }
383
384 void request_send(struct fuse_conn *fc, struct fuse_req *req)
385 {
386         req->isreply = 1;
387         spin_lock(&fc->lock);
388         if (!fc->connected)
389                 req->out.h.error = -ENOTCONN;
390         else if (fc->conn_error)
391                 req->out.h.error = -ECONNREFUSED;
392         else {
393                 queue_request(fc, req);
394                 /* acquire extra reference, since request is still needed
395                    after request_end() */
396                 __fuse_get_request(req);
397
398                 request_wait_answer(fc, req);
399         }
400         spin_unlock(&fc->lock);
401 }
402
403 static void request_send_nowait_locked(struct fuse_conn *fc,
404                                        struct fuse_req *req)
405 {
406         req->background = 1;
407         fc->num_background++;
408         if (fc->num_background == FUSE_MAX_BACKGROUND)
409                 fc->blocked = 1;
410         if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
411                 set_bdi_congested(&fc->bdi, READ);
412                 set_bdi_congested(&fc->bdi, WRITE);
413         }
414         list_add_tail(&req->list, &fc->bg_queue);
415         flush_bg_queue(fc);
416 }
417
418 static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
419 {
420         spin_lock(&fc->lock);
421         if (fc->connected) {
422                 request_send_nowait_locked(fc, req);
423                 spin_unlock(&fc->lock);
424         } else {
425                 req->out.h.error = -ENOTCONN;
426                 request_end(fc, req);
427         }
428 }
429
430 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
431 {
432         req->isreply = 0;
433         request_send_nowait(fc, req);
434 }
435
436 void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
437 {
438         req->isreply = 1;
439         request_send_nowait(fc, req);
440 }
441
442 /*
443  * Called under fc->lock
444  *
445  * fc->connected must have been checked previously
446  */
447 void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
448 {
449         req->isreply = 1;
450         request_send_nowait_locked(fc, req);
451 }
452
453 /*
454  * Lock the request.  Up to the next unlock_request() there mustn't be
455  * anything that could cause a page-fault.  If the request was already
456  * aborted bail out.
457  */
458 static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
459 {
460         int err = 0;
461         if (req) {
462                 spin_lock(&fc->lock);
463                 if (req->aborted)
464                         err = -ENOENT;
465                 else
466                         req->locked = 1;
467                 spin_unlock(&fc->lock);
468         }
469         return err;
470 }
471
472 /*
473  * Unlock request.  If it was aborted during being locked, the
474  * requester thread is currently waiting for it to be unlocked, so
475  * wake it up.
476  */
477 static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
478 {
479         if (req) {
480                 spin_lock(&fc->lock);
481                 req->locked = 0;
482                 if (req->aborted)
483                         wake_up(&req->waitq);
484                 spin_unlock(&fc->lock);
485         }
486 }
487
488 struct fuse_copy_state {
489         struct fuse_conn *fc;
490         int write;
491         struct fuse_req *req;
492         const struct iovec *iov;
493         unsigned long nr_segs;
494         unsigned long seglen;
495         unsigned long addr;
496         struct page *pg;
497         void *mapaddr;
498         void *buf;
499         unsigned len;
500 };
501
502 static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
503                            int write, struct fuse_req *req,
504                            const struct iovec *iov, unsigned long nr_segs)
505 {
506         memset(cs, 0, sizeof(*cs));
507         cs->fc = fc;
508         cs->write = write;
509         cs->req = req;
510         cs->iov = iov;
511         cs->nr_segs = nr_segs;
512 }
513
514 /* Unmap and put previous page of userspace buffer */
515 static void fuse_copy_finish(struct fuse_copy_state *cs)
516 {
517         if (cs->mapaddr) {
518                 kunmap_atomic(cs->mapaddr, KM_USER0);
519                 if (cs->write) {
520                         flush_dcache_page(cs->pg);
521                         set_page_dirty_lock(cs->pg);
522                 }
523                 put_page(cs->pg);
524                 cs->mapaddr = NULL;
525         }
526 }
527
528 /*
529  * Get another pagefull of userspace buffer, and map it to kernel
530  * address space, and lock request
531  */
532 static int fuse_copy_fill(struct fuse_copy_state *cs)
533 {
534         unsigned long offset;
535         int err;
536
537         unlock_request(cs->fc, cs->req);
538         fuse_copy_finish(cs);
539         if (!cs->seglen) {
540                 BUG_ON(!cs->nr_segs);
541                 cs->seglen = cs->iov[0].iov_len;
542                 cs->addr = (unsigned long) cs->iov[0].iov_base;
543                 cs->iov ++;
544                 cs->nr_segs --;
545         }
546         down_read(&current->mm->mmap_sem);
547         err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
548                              &cs->pg, NULL);
549         up_read(&current->mm->mmap_sem);
550         if (err < 0)
551                 return err;
552         BUG_ON(err != 1);
553         offset = cs->addr % PAGE_SIZE;
554         cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
555         cs->buf = cs->mapaddr + offset;
556         cs->len = min(PAGE_SIZE - offset, cs->seglen);
557         cs->seglen -= cs->len;
558         cs->addr += cs->len;
559
560         return lock_request(cs->fc, cs->req);
561 }
562
563 /* Do as much copy to/from userspace buffer as we can */
564 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
565 {
566         unsigned ncpy = min(*size, cs->len);
567         if (val) {
568                 if (cs->write)
569                         memcpy(cs->buf, *val, ncpy);
570                 else
571                         memcpy(*val, cs->buf, ncpy);
572                 *val += ncpy;
573         }
574         *size -= ncpy;
575         cs->len -= ncpy;
576         cs->buf += ncpy;
577         return ncpy;
578 }
579
580 /*
581  * Copy a page in the request to/from the userspace buffer.  Must be
582  * done atomically
583  */
584 static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
585                           unsigned offset, unsigned count, int zeroing)
586 {
587         if (page && zeroing && count < PAGE_SIZE) {
588                 void *mapaddr = kmap_atomic(page, KM_USER1);
589                 memset(mapaddr, 0, PAGE_SIZE);
590                 kunmap_atomic(mapaddr, KM_USER1);
591         }
592         while (count) {
593                 int err;
594                 if (!cs->len && (err = fuse_copy_fill(cs)))
595                         return err;
596                 if (page) {
597                         void *mapaddr = kmap_atomic(page, KM_USER1);
598                         void *buf = mapaddr + offset;
599                         offset += fuse_copy_do(cs, &buf, &count);
600                         kunmap_atomic(mapaddr, KM_USER1);
601                 } else
602                         offset += fuse_copy_do(cs, NULL, &count);
603         }
604         if (page && !cs->write)
605                 flush_dcache_page(page);
606         return 0;
607 }
608
609 /* Copy pages in the request to/from userspace buffer */
610 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
611                            int zeroing)
612 {
613         unsigned i;
614         struct fuse_req *req = cs->req;
615         unsigned offset = req->page_offset;
616         unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
617
618         for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
619                 struct page *page = req->pages[i];
620                 int err = fuse_copy_page(cs, page, offset, count, zeroing);
621                 if (err)
622                         return err;
623
624                 nbytes -= count;
625                 count = min(nbytes, (unsigned) PAGE_SIZE);
626                 offset = 0;
627         }
628         return 0;
629 }
630
631 /* Copy a single argument in the request to/from userspace buffer */
632 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
633 {
634         while (size) {
635                 int err;
636                 if (!cs->len && (err = fuse_copy_fill(cs)))
637                         return err;
638                 fuse_copy_do(cs, &val, &size);
639         }
640         return 0;
641 }
642
643 /* Copy request arguments to/from userspace buffer */
644 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
645                           unsigned argpages, struct fuse_arg *args,
646                           int zeroing)
647 {
648         int err = 0;
649         unsigned i;
650
651         for (i = 0; !err && i < numargs; i++)  {
652                 struct fuse_arg *arg = &args[i];
653                 if (i == numargs - 1 && argpages)
654                         err = fuse_copy_pages(cs, arg->size, zeroing);
655                 else
656                         err = fuse_copy_one(cs, arg->value, arg->size);
657         }
658         return err;
659 }
660
661 static int request_pending(struct fuse_conn *fc)
662 {
663         return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
664 }
665
666 /* Wait until a request is available on the pending list */
667 static void request_wait(struct fuse_conn *fc)
668 {
669         DECLARE_WAITQUEUE(wait, current);
670
671         add_wait_queue_exclusive(&fc->waitq, &wait);
672         while (fc->connected && !request_pending(fc)) {
673                 set_current_state(TASK_INTERRUPTIBLE);
674                 if (signal_pending(current))
675                         break;
676
677                 spin_unlock(&fc->lock);
678                 schedule();
679                 spin_lock(&fc->lock);
680         }
681         set_current_state(TASK_RUNNING);
682         remove_wait_queue(&fc->waitq, &wait);
683 }
684
685 /*
686  * Transfer an interrupt request to userspace
687  *
688  * Unlike other requests this is assembled on demand, without a need
689  * to allocate a separate fuse_req structure.
690  *
691  * Called with fc->lock held, releases it
692  */
693 static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
694                                const struct iovec *iov, unsigned long nr_segs)
695         __releases(fc->lock)
696 {
697         struct fuse_copy_state cs;
698         struct fuse_in_header ih;
699         struct fuse_interrupt_in arg;
700         unsigned reqsize = sizeof(ih) + sizeof(arg);
701         int err;
702
703         list_del_init(&req->intr_entry);
704         req->intr_unique = fuse_get_unique(fc);
705         memset(&ih, 0, sizeof(ih));
706         memset(&arg, 0, sizeof(arg));
707         ih.len = reqsize;
708         ih.opcode = FUSE_INTERRUPT;
709         ih.unique = req->intr_unique;
710         arg.unique = req->in.h.unique;
711
712         spin_unlock(&fc->lock);
713         if (iov_length(iov, nr_segs) < reqsize)
714                 return -EINVAL;
715
716         fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
717         err = fuse_copy_one(&cs, &ih, sizeof(ih));
718         if (!err)
719                 err = fuse_copy_one(&cs, &arg, sizeof(arg));
720         fuse_copy_finish(&cs);
721
722         return err ? err : reqsize;
723 }
724
725 /*
726  * Read a single request into the userspace filesystem's buffer.  This
727  * function waits until a request is available, then removes it from
728  * the pending list and copies request data to userspace buffer.  If
729  * no reply is needed (FORGET) or request has been aborted or there
730  * was an error during the copying then it's finished by calling
731  * request_end().  Otherwise add it to the processing list, and set
732  * the 'sent' flag.
733  */
734 static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
735                               unsigned long nr_segs, loff_t pos)
736 {
737         int err;
738         struct fuse_req *req;
739         struct fuse_in *in;
740         struct fuse_copy_state cs;
741         unsigned reqsize;
742         struct file *file = iocb->ki_filp;
743         struct fuse_conn *fc = fuse_get_conn(file);
744         if (!fc)
745                 return -EPERM;
746
747  restart:
748         spin_lock(&fc->lock);
749         err = -EAGAIN;
750         if ((file->f_flags & O_NONBLOCK) && fc->connected &&
751             !request_pending(fc))
752                 goto err_unlock;
753
754         request_wait(fc);
755         err = -ENODEV;
756         if (!fc->connected)
757                 goto err_unlock;
758         err = -ERESTARTSYS;
759         if (!request_pending(fc))
760                 goto err_unlock;
761
762         if (!list_empty(&fc->interrupts)) {
763                 req = list_entry(fc->interrupts.next, struct fuse_req,
764                                  intr_entry);
765                 return fuse_read_interrupt(fc, req, iov, nr_segs);
766         }
767
768         req = list_entry(fc->pending.next, struct fuse_req, list);
769         req->state = FUSE_REQ_READING;
770         list_move(&req->list, &fc->io);
771
772         in = &req->in;
773         reqsize = in->h.len;
774         /* If request is too large, reply with an error and restart the read */
775         if (iov_length(iov, nr_segs) < reqsize) {
776                 req->out.h.error = -EIO;
777                 /* SETXATTR is special, since it may contain too large data */
778                 if (in->h.opcode == FUSE_SETXATTR)
779                         req->out.h.error = -E2BIG;
780                 request_end(fc, req);
781                 goto restart;
782         }
783         spin_unlock(&fc->lock);
784         fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
785         err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
786         if (!err)
787                 err = fuse_copy_args(&cs, in->numargs, in->argpages,
788                                      (struct fuse_arg *) in->args, 0);
789         fuse_copy_finish(&cs);
790         spin_lock(&fc->lock);
791         req->locked = 0;
792         if (req->aborted) {
793                 request_end(fc, req);
794                 return -ENODEV;
795         }
796         if (err) {
797                 req->out.h.error = -EIO;
798                 request_end(fc, req);
799                 return err;
800         }
801         if (!req->isreply)
802                 request_end(fc, req);
803         else {
804                 req->state = FUSE_REQ_SENT;
805                 list_move_tail(&req->list, &fc->processing);
806                 if (req->interrupted)
807                         queue_interrupt(fc, req);
808                 spin_unlock(&fc->lock);
809         }
810         return reqsize;
811
812  err_unlock:
813         spin_unlock(&fc->lock);
814         return err;
815 }
816
817 /* Look up request on processing list by unique ID */
818 static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
819 {
820         struct list_head *entry;
821
822         list_for_each(entry, &fc->processing) {
823                 struct fuse_req *req;
824                 req = list_entry(entry, struct fuse_req, list);
825                 if (req->in.h.unique == unique || req->intr_unique == unique)
826                         return req;
827         }
828         return NULL;
829 }
830
831 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
832                          unsigned nbytes)
833 {
834         unsigned reqsize = sizeof(struct fuse_out_header);
835
836         if (out->h.error)
837                 return nbytes != reqsize ? -EINVAL : 0;
838
839         reqsize += len_args(out->numargs, out->args);
840
841         if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
842                 return -EINVAL;
843         else if (reqsize > nbytes) {
844                 struct fuse_arg *lastarg = &out->args[out->numargs-1];
845                 unsigned diffsize = reqsize - nbytes;
846                 if (diffsize > lastarg->size)
847                         return -EINVAL;
848                 lastarg->size -= diffsize;
849         }
850         return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
851                               out->page_zeroing);
852 }
853
854 /*
855  * Write a single reply to a request.  First the header is copied from
856  * the write buffer.  The request is then searched on the processing
857  * list by the unique ID found in the header.  If found, then remove
858  * it from the list and copy the rest of the buffer to the request.
859  * The request is finished by calling request_end()
860  */
861 static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
862                                unsigned long nr_segs, loff_t pos)
863 {
864         int err;
865         unsigned nbytes = iov_length(iov, nr_segs);
866         struct fuse_req *req;
867         struct fuse_out_header oh;
868         struct fuse_copy_state cs;
869         struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
870         if (!fc)
871                 return -EPERM;
872
873         fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
874         if (nbytes < sizeof(struct fuse_out_header))
875                 return -EINVAL;
876
877         err = fuse_copy_one(&cs, &oh, sizeof(oh));
878         if (err)
879                 goto err_finish;
880         err = -EINVAL;
881         if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
882             oh.len != nbytes)
883                 goto err_finish;
884
885         spin_lock(&fc->lock);
886         err = -ENOENT;
887         if (!fc->connected)
888                 goto err_unlock;
889
890         req = request_find(fc, oh.unique);
891         if (!req)
892                 goto err_unlock;
893
894         if (req->aborted) {
895                 spin_unlock(&fc->lock);
896                 fuse_copy_finish(&cs);
897                 spin_lock(&fc->lock);
898                 request_end(fc, req);
899                 return -ENOENT;
900         }
901         /* Is it an interrupt reply? */
902         if (req->intr_unique == oh.unique) {
903                 err = -EINVAL;
904                 if (nbytes != sizeof(struct fuse_out_header))
905                         goto err_unlock;
906
907                 if (oh.error == -ENOSYS)
908                         fc->no_interrupt = 1;
909                 else if (oh.error == -EAGAIN)
910                         queue_interrupt(fc, req);
911
912                 spin_unlock(&fc->lock);
913                 fuse_copy_finish(&cs);
914                 return nbytes;
915         }
916
917         req->state = FUSE_REQ_WRITING;
918         list_move(&req->list, &fc->io);
919         req->out.h = oh;
920         req->locked = 1;
921         cs.req = req;
922         spin_unlock(&fc->lock);
923
924         err = copy_out_args(&cs, &req->out, nbytes);
925         fuse_copy_finish(&cs);
926
927         spin_lock(&fc->lock);
928         req->locked = 0;
929         if (!err) {
930                 if (req->aborted)
931                         err = -ENOENT;
932         } else if (!req->aborted)
933                 req->out.h.error = -EIO;
934         request_end(fc, req);
935
936         return err ? err : nbytes;
937
938  err_unlock:
939         spin_unlock(&fc->lock);
940  err_finish:
941         fuse_copy_finish(&cs);
942         return err;
943 }
944
945 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
946 {
947         unsigned mask = POLLOUT | POLLWRNORM;
948         struct fuse_conn *fc = fuse_get_conn(file);
949         if (!fc)
950                 return POLLERR;
951
952         poll_wait(file, &fc->waitq, wait);
953
954         spin_lock(&fc->lock);
955         if (!fc->connected)
956                 mask = POLLERR;
957         else if (request_pending(fc))
958                 mask |= POLLIN | POLLRDNORM;
959         spin_unlock(&fc->lock);
960
961         return mask;
962 }
963
964 /*
965  * Abort all requests on the given list (pending or processing)
966  *
967  * This function releases and reacquires fc->lock
968  */
969 static void end_requests(struct fuse_conn *fc, struct list_head *head)
970 {
971         while (!list_empty(head)) {
972                 struct fuse_req *req;
973                 req = list_entry(head->next, struct fuse_req, list);
974                 req->out.h.error = -ECONNABORTED;
975                 request_end(fc, req);
976                 spin_lock(&fc->lock);
977         }
978 }
979
980 /*
981  * Abort requests under I/O
982  *
983  * The requests are set to aborted and finished, and the request
984  * waiter is woken up.  This will make request_wait_answer() wait
985  * until the request is unlocked and then return.
986  *
987  * If the request is asynchronous, then the end function needs to be
988  * called after waiting for the request to be unlocked (if it was
989  * locked).
990  */
991 static void end_io_requests(struct fuse_conn *fc)
992         __releases(fc->lock) __acquires(fc->lock)
993 {
994         while (!list_empty(&fc->io)) {
995                 struct fuse_req *req =
996                         list_entry(fc->io.next, struct fuse_req, list);
997                 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
998
999                 req->aborted = 1;
1000                 req->out.h.error = -ECONNABORTED;
1001                 req->state = FUSE_REQ_FINISHED;
1002                 list_del_init(&req->list);
1003                 wake_up(&req->waitq);
1004                 if (end) {
1005                         req->end = NULL;
1006                         /* The end function will consume this reference */
1007                         __fuse_get_request(req);
1008                         spin_unlock(&fc->lock);
1009                         wait_event(req->waitq, !req->locked);
1010                         end(fc, req);
1011                         spin_lock(&fc->lock);
1012                 }
1013         }
1014 }
1015
1016 /*
1017  * Abort all requests.
1018  *
1019  * Emergency exit in case of a malicious or accidental deadlock, or
1020  * just a hung filesystem.
1021  *
1022  * The same effect is usually achievable through killing the
1023  * filesystem daemon and all users of the filesystem.  The exception
1024  * is the combination of an asynchronous request and the tricky
1025  * deadlock (see Documentation/filesystems/fuse.txt).
1026  *
1027  * During the aborting, progression of requests from the pending and
1028  * processing lists onto the io list, and progression of new requests
1029  * onto the pending list is prevented by req->connected being false.
1030  *
1031  * Progression of requests under I/O to the processing list is
1032  * prevented by the req->aborted flag being true for these requests.
1033  * For this reason requests on the io list must be aborted first.
1034  */
1035 void fuse_abort_conn(struct fuse_conn *fc)
1036 {
1037         spin_lock(&fc->lock);
1038         if (fc->connected) {
1039                 fc->connected = 0;
1040                 fc->blocked = 0;
1041                 end_io_requests(fc);
1042                 end_requests(fc, &fc->pending);
1043                 end_requests(fc, &fc->processing);
1044                 wake_up_all(&fc->waitq);
1045                 wake_up_all(&fc->blocked_waitq);
1046                 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
1047         }
1048         spin_unlock(&fc->lock);
1049 }
1050
1051 static int fuse_dev_release(struct inode *inode, struct file *file)
1052 {
1053         struct fuse_conn *fc = fuse_get_conn(file);
1054         if (fc) {
1055                 spin_lock(&fc->lock);
1056                 fc->connected = 0;
1057                 end_requests(fc, &fc->pending);
1058                 end_requests(fc, &fc->processing);
1059                 spin_unlock(&fc->lock);
1060                 fasync_helper(-1, file, 0, &fc->fasync);
1061                 fuse_conn_put(fc);
1062         }
1063
1064         return 0;
1065 }
1066
1067 static int fuse_dev_fasync(int fd, struct file *file, int on)
1068 {
1069         struct fuse_conn *fc = fuse_get_conn(file);
1070         if (!fc)
1071                 return -EPERM;
1072
1073         /* No locking - fasync_helper does its own locking */
1074         return fasync_helper(fd, file, on, &fc->fasync);
1075 }
1076
1077 const struct file_operations fuse_dev_operations = {
1078         .owner          = THIS_MODULE,
1079         .llseek         = no_llseek,
1080         .read           = do_sync_read,
1081         .aio_read       = fuse_dev_read,
1082         .write          = do_sync_write,
1083         .aio_write      = fuse_dev_write,
1084         .poll           = fuse_dev_poll,
1085         .release        = fuse_dev_release,
1086         .fasync         = fuse_dev_fasync,
1087 };
1088
1089 static struct miscdevice fuse_miscdevice = {
1090         .minor = FUSE_MINOR,
1091         .name  = "fuse",
1092         .fops = &fuse_dev_operations,
1093 };
1094
1095 int __init fuse_dev_init(void)
1096 {
1097         int err = -ENOMEM;
1098         fuse_req_cachep = kmem_cache_create("fuse_request",
1099                                             sizeof(struct fuse_req),
1100                                             0, 0, NULL);
1101         if (!fuse_req_cachep)
1102                 goto out;
1103
1104         err = misc_register(&fuse_miscdevice);
1105         if (err)
1106                 goto out_cache_clean;
1107
1108         return 0;
1109
1110  out_cache_clean:
1111         kmem_cache_destroy(fuse_req_cachep);
1112  out:
1113         return err;
1114 }
1115
1116 void fuse_dev_cleanup(void)
1117 {
1118         misc_deregister(&fuse_miscdevice);
1119         kmem_cache_destroy(fuse_req_cachep);
1120 }