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