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