]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/fuse/dev.c
fuse: implement unsolicited notification
[net-next-2.6.git] / fs / fuse / dev.c
CommitLineData
334f485d
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 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
e18b890b 22static struct kmem_cache *fuse_req_cachep;
334f485d 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{
e94b1766 44 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
334f485d
MS
45 if (req)
46 fuse_request_init(req);
47 return req;
48}
49
3be5a52b
MS
50struct 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
334f485d
MS
58void fuse_request_free(struct fuse_req *req)
59{
60 kmem_cache_free(fuse_req_cachep, req);
61}
62
8bfc016d 63static void block_sigs(sigset_t *oldset)
334f485d
MS
64{
65 sigset_t mask;
66
67 siginitsetinv(&mask, sigmask(SIGKILL));
68 sigprocmask(SIG_BLOCK, &mask, oldset);
69}
70
8bfc016d 71static void restore_sigs(sigset_t *oldset)
334f485d
MS
72{
73 sigprocmask(SIG_SETMASK, oldset, NULL);
74}
75
334f485d
MS
76static void __fuse_get_request(struct fuse_req *req)
77{
78 atomic_inc(&req->count);
79}
80
81/* Must be called with > 1 refcount */
82static void __fuse_put_request(struct fuse_req *req)
83{
84 BUG_ON(atomic_read(&req->count) < 2);
85 atomic_dec(&req->count);
86}
87
33649c91
MS
88static 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
ce1d5a49 95struct fuse_req *fuse_get_req(struct fuse_conn *fc)
334f485d 96{
08a53cdc
MS
97 struct fuse_req *req;
98 sigset_t oldset;
9bc5ddda 99 int intr;
08a53cdc
MS
100 int err;
101
9bc5ddda 102 atomic_inc(&fc->num_waiting);
08a53cdc 103 block_sigs(&oldset);
9bc5ddda 104 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
08a53cdc 105 restore_sigs(&oldset);
9bc5ddda
MS
106 err = -EINTR;
107 if (intr)
108 goto out;
08a53cdc 109
51eb01e7
MS
110 err = -ENOTCONN;
111 if (!fc->connected)
112 goto out;
113
08a53cdc 114 req = fuse_request_alloc();
9bc5ddda 115 err = -ENOMEM;
ce1d5a49 116 if (!req)
9bc5ddda 117 goto out;
334f485d 118
33649c91 119 fuse_req_init_context(req);
9bc5ddda 120 req->waiting = 1;
334f485d 121 return req;
9bc5ddda
MS
122
123 out:
124 atomic_dec(&fc->num_waiting);
125 return ERR_PTR(err);
334f485d
MS
126}
127
33649c91
MS
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 */
133static 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 {
de5e3dec 140 wait_event(fc->reserved_req_waitq, ff->reserved_req);
33649c91
MS
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 */
157static 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;
de5e3dec 166 wake_up_all(&fc->reserved_req_waitq);
33649c91
MS
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 */
184struct 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
334f485d 199void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
7128ec2a
MS
200{
201 if (atomic_dec_and_test(&req->count)) {
9bc5ddda
MS
202 if (req->waiting)
203 atomic_dec(&fc->num_waiting);
33649c91
MS
204
205 if (req->stolen_file)
206 put_reserved_req(fc, req);
207 else
208 fuse_request_free(req);
7128ec2a
MS
209 }
210}
211
d12def1b
MS
212static 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
223static 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
233static 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
248static 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
334f485d
MS
261/*
262 * This function is called when a request is finished. Either a reply
f9a2842e 263 * has arrived or it was aborted (and not yet sent) or some error
f43b155a 264 * occurred during communication with userspace, or the device file
51eb01e7
MS
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
7128ec2a 268 *
d7133114 269 * Called with fc->lock, unlocks it
334f485d
MS
270 */
271static void request_end(struct fuse_conn *fc, struct fuse_req *req)
105f4d7a 272 __releases(fc->lock)
334f485d 273{
51eb01e7
MS
274 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
275 req->end = NULL;
d77a1d5b 276 list_del(&req->list);
a4d27e75 277 list_del(&req->intr_entry);
83cfd493 278 req->state = FUSE_REQ_FINISHED;
51eb01e7
MS
279 if (req->background) {
280 if (fc->num_background == FUSE_MAX_BACKGROUND) {
281 fc->blocked = 0;
282 wake_up_all(&fc->blocked_waitq);
283 }
f92b99b9
MS
284 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
285 clear_bdi_congested(&fc->bdi, READ);
286 clear_bdi_congested(&fc->bdi, WRITE);
287 }
51eb01e7 288 fc->num_background--;
d12def1b
MS
289 fc->active_background--;
290 flush_bg_queue(fc);
334f485d 291 }
51eb01e7 292 spin_unlock(&fc->lock);
51eb01e7
MS
293 wake_up(&req->waitq);
294 if (end)
295 end(fc, req);
e9bb09dd 296 fuse_put_request(fc, req);
334f485d
MS
297}
298
a4d27e75
MS
299static void wait_answer_interruptible(struct fuse_conn *fc,
300 struct fuse_req *req)
4dbf930e 301 __releases(fc->lock) __acquires(fc->lock)
a4d27e75
MS
302{
303 if (signal_pending(current))
304 return;
305
306 spin_unlock(&fc->lock);
307 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
308 spin_lock(&fc->lock);
309}
310
311static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
312{
313 list_add_tail(&req->intr_entry, &fc->interrupts);
314 wake_up(&fc->waitq);
315 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
316}
317
7c352bdf 318static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
4dbf930e 319 __releases(fc->lock) __acquires(fc->lock)
334f485d 320{
a4d27e75
MS
321 if (!fc->no_interrupt) {
322 /* Any signal may interrupt this */
323 wait_answer_interruptible(fc, req);
334f485d 324
a4d27e75
MS
325 if (req->aborted)
326 goto aborted;
327 if (req->state == FUSE_REQ_FINISHED)
328 return;
329
330 req->interrupted = 1;
331 if (req->state == FUSE_REQ_SENT)
332 queue_interrupt(fc, req);
333 }
334
a131de0a 335 if (!req->force) {
a4d27e75
MS
336 sigset_t oldset;
337
338 /* Only fatal signals may interrupt this */
51eb01e7 339 block_sigs(&oldset);
a4d27e75 340 wait_answer_interruptible(fc, req);
51eb01e7 341 restore_sigs(&oldset);
a131de0a
MS
342
343 if (req->aborted)
344 goto aborted;
345 if (req->state == FUSE_REQ_FINISHED)
346 return;
347
348 /* Request is not yet in userspace, bail out */
349 if (req->state == FUSE_REQ_PENDING) {
350 list_del(&req->list);
351 __fuse_put_request(req);
352 req->out.h.error = -EINTR;
353 return;
354 }
51eb01e7 355 }
334f485d 356
a131de0a
MS
357 /*
358 * Either request is already in userspace, or it was forced.
359 * Wait it out.
360 */
361 spin_unlock(&fc->lock);
362 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
363 spin_lock(&fc->lock);
a4d27e75 364
a131de0a
MS
365 if (!req->aborted)
366 return;
a4d27e75
MS
367
368 aborted:
a131de0a 369 BUG_ON(req->state != FUSE_REQ_FINISHED);
334f485d
MS
370 if (req->locked) {
371 /* This is uninterruptible sleep, because data is
372 being copied to/from the buffers of req. During
373 locked state, there mustn't be any filesystem
374 operation (e.g. page fault), since that could lead
375 to deadlock */
d7133114 376 spin_unlock(&fc->lock);
334f485d 377 wait_event(req->waitq, !req->locked);
d7133114 378 spin_lock(&fc->lock);
334f485d 379 }
334f485d
MS
380}
381
7c352bdf 382void request_send(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
383{
384 req->isreply = 1;
d7133114 385 spin_lock(&fc->lock);
1e9a4ed9 386 if (!fc->connected)
334f485d
MS
387 req->out.h.error = -ENOTCONN;
388 else if (fc->conn_error)
389 req->out.h.error = -ECONNREFUSED;
390 else {
391 queue_request(fc, req);
392 /* acquire extra reference, since request is still needed
393 after request_end() */
394 __fuse_get_request(req);
395
7c352bdf 396 request_wait_answer(fc, req);
334f485d 397 }
d7133114 398 spin_unlock(&fc->lock);
334f485d
MS
399}
400
d12def1b
MS
401static void request_send_nowait_locked(struct fuse_conn *fc,
402 struct fuse_req *req)
403{
404 req->background = 1;
405 fc->num_background++;
406 if (fc->num_background == FUSE_MAX_BACKGROUND)
407 fc->blocked = 1;
408 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
409 set_bdi_congested(&fc->bdi, READ);
410 set_bdi_congested(&fc->bdi, WRITE);
411 }
412 list_add_tail(&req->list, &fc->bg_queue);
413 flush_bg_queue(fc);
414}
415
334f485d
MS
416static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
417{
d7133114 418 spin_lock(&fc->lock);
1e9a4ed9 419 if (fc->connected) {
d12def1b 420 request_send_nowait_locked(fc, req);
d7133114 421 spin_unlock(&fc->lock);
334f485d
MS
422 } else {
423 req->out.h.error = -ENOTCONN;
424 request_end(fc, req);
425 }
426}
427
428void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
429{
430 req->isreply = 0;
431 request_send_nowait(fc, req);
432}
433
434void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
435{
436 req->isreply = 1;
334f485d
MS
437 request_send_nowait(fc, req);
438}
439
3be5a52b
MS
440/*
441 * Called under fc->lock
442 *
443 * fc->connected must have been checked previously
444 */
445void request_send_background_locked(struct fuse_conn *fc, struct fuse_req *req)
446{
447 req->isreply = 1;
448 request_send_nowait_locked(fc, req);
449}
450
334f485d
MS
451/*
452 * Lock the request. Up to the next unlock_request() there mustn't be
453 * anything that could cause a page-fault. If the request was already
f9a2842e 454 * aborted bail out.
334f485d 455 */
d7133114 456static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
457{
458 int err = 0;
459 if (req) {
d7133114 460 spin_lock(&fc->lock);
f9a2842e 461 if (req->aborted)
334f485d
MS
462 err = -ENOENT;
463 else
464 req->locked = 1;
d7133114 465 spin_unlock(&fc->lock);
334f485d
MS
466 }
467 return err;
468}
469
470/*
f9a2842e 471 * Unlock request. If it was aborted during being locked, the
334f485d
MS
472 * requester thread is currently waiting for it to be unlocked, so
473 * wake it up.
474 */
d7133114 475static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
476{
477 if (req) {
d7133114 478 spin_lock(&fc->lock);
334f485d 479 req->locked = 0;
f9a2842e 480 if (req->aborted)
334f485d 481 wake_up(&req->waitq);
d7133114 482 spin_unlock(&fc->lock);
334f485d
MS
483 }
484}
485
486struct fuse_copy_state {
d7133114 487 struct fuse_conn *fc;
334f485d
MS
488 int write;
489 struct fuse_req *req;
490 const struct iovec *iov;
491 unsigned long nr_segs;
492 unsigned long seglen;
493 unsigned long addr;
494 struct page *pg;
495 void *mapaddr;
496 void *buf;
497 unsigned len;
498};
499
d7133114
MS
500static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
501 int write, struct fuse_req *req,
502 const struct iovec *iov, unsigned long nr_segs)
334f485d
MS
503{
504 memset(cs, 0, sizeof(*cs));
d7133114 505 cs->fc = fc;
334f485d
MS
506 cs->write = write;
507 cs->req = req;
508 cs->iov = iov;
509 cs->nr_segs = nr_segs;
510}
511
512/* Unmap and put previous page of userspace buffer */
8bfc016d 513static void fuse_copy_finish(struct fuse_copy_state *cs)
334f485d
MS
514{
515 if (cs->mapaddr) {
516 kunmap_atomic(cs->mapaddr, KM_USER0);
517 if (cs->write) {
518 flush_dcache_page(cs->pg);
519 set_page_dirty_lock(cs->pg);
520 }
521 put_page(cs->pg);
522 cs->mapaddr = NULL;
523 }
524}
525
526/*
527 * Get another pagefull of userspace buffer, and map it to kernel
528 * address space, and lock request
529 */
530static int fuse_copy_fill(struct fuse_copy_state *cs)
531{
532 unsigned long offset;
533 int err;
534
d7133114 535 unlock_request(cs->fc, cs->req);
334f485d
MS
536 fuse_copy_finish(cs);
537 if (!cs->seglen) {
538 BUG_ON(!cs->nr_segs);
539 cs->seglen = cs->iov[0].iov_len;
540 cs->addr = (unsigned long) cs->iov[0].iov_base;
1729a16c
MS
541 cs->iov++;
542 cs->nr_segs--;
334f485d
MS
543 }
544 down_read(&current->mm->mmap_sem);
545 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
546 &cs->pg, NULL);
547 up_read(&current->mm->mmap_sem);
548 if (err < 0)
549 return err;
550 BUG_ON(err != 1);
551 offset = cs->addr % PAGE_SIZE;
552 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
553 cs->buf = cs->mapaddr + offset;
554 cs->len = min(PAGE_SIZE - offset, cs->seglen);
555 cs->seglen -= cs->len;
556 cs->addr += cs->len;
557
d7133114 558 return lock_request(cs->fc, cs->req);
334f485d
MS
559}
560
561/* Do as much copy to/from userspace buffer as we can */
8bfc016d 562static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
334f485d
MS
563{
564 unsigned ncpy = min(*size, cs->len);
565 if (val) {
566 if (cs->write)
567 memcpy(cs->buf, *val, ncpy);
568 else
569 memcpy(*val, cs->buf, ncpy);
570 *val += ncpy;
571 }
572 *size -= ncpy;
573 cs->len -= ncpy;
574 cs->buf += ncpy;
575 return ncpy;
576}
577
578/*
579 * Copy a page in the request to/from the userspace buffer. Must be
580 * done atomically
581 */
8bfc016d
MS
582static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
583 unsigned offset, unsigned count, int zeroing)
334f485d
MS
584{
585 if (page && zeroing && count < PAGE_SIZE) {
586 void *mapaddr = kmap_atomic(page, KM_USER1);
587 memset(mapaddr, 0, PAGE_SIZE);
588 kunmap_atomic(mapaddr, KM_USER1);
589 }
590 while (count) {
1729a16c
MS
591 if (!cs->len) {
592 int err = fuse_copy_fill(cs);
593 if (err)
594 return err;
595 }
334f485d
MS
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 */
610static 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 */
632static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
633{
634 while (size) {
1729a16c
MS
635 if (!cs->len) {
636 int err = fuse_copy_fill(cs);
637 if (err)
638 return err;
639 }
334f485d
MS
640 fuse_copy_do(cs, &val, &size);
641 }
642 return 0;
643}
644
645/* Copy request arguments to/from userspace buffer */
646static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
647 unsigned argpages, struct fuse_arg *args,
648 int zeroing)
649{
650 int err = 0;
651 unsigned i;
652
653 for (i = 0; !err && i < numargs; i++) {
654 struct fuse_arg *arg = &args[i];
655 if (i == numargs - 1 && argpages)
656 err = fuse_copy_pages(cs, arg->size, zeroing);
657 else
658 err = fuse_copy_one(cs, arg->value, arg->size);
659 }
660 return err;
661}
662
a4d27e75
MS
663static int request_pending(struct fuse_conn *fc)
664{
665 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
666}
667
334f485d
MS
668/* Wait until a request is available on the pending list */
669static void request_wait(struct fuse_conn *fc)
670{
671 DECLARE_WAITQUEUE(wait, current);
672
673 add_wait_queue_exclusive(&fc->waitq, &wait);
a4d27e75 674 while (fc->connected && !request_pending(fc)) {
334f485d
MS
675 set_current_state(TASK_INTERRUPTIBLE);
676 if (signal_pending(current))
677 break;
678
d7133114 679 spin_unlock(&fc->lock);
334f485d 680 schedule();
d7133114 681 spin_lock(&fc->lock);
334f485d
MS
682 }
683 set_current_state(TASK_RUNNING);
684 remove_wait_queue(&fc->waitq, &wait);
685}
686
a4d27e75
MS
687/*
688 * Transfer an interrupt request to userspace
689 *
690 * Unlike other requests this is assembled on demand, without a need
691 * to allocate a separate fuse_req structure.
692 *
693 * Called with fc->lock held, releases it
694 */
695static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
696 const struct iovec *iov, unsigned long nr_segs)
105f4d7a 697 __releases(fc->lock)
a4d27e75
MS
698{
699 struct fuse_copy_state cs;
700 struct fuse_in_header ih;
701 struct fuse_interrupt_in arg;
702 unsigned reqsize = sizeof(ih) + sizeof(arg);
703 int err;
704
705 list_del_init(&req->intr_entry);
706 req->intr_unique = fuse_get_unique(fc);
707 memset(&ih, 0, sizeof(ih));
708 memset(&arg, 0, sizeof(arg));
709 ih.len = reqsize;
710 ih.opcode = FUSE_INTERRUPT;
711 ih.unique = req->intr_unique;
712 arg.unique = req->in.h.unique;
713
714 spin_unlock(&fc->lock);
715 if (iov_length(iov, nr_segs) < reqsize)
716 return -EINVAL;
717
718 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
719 err = fuse_copy_one(&cs, &ih, sizeof(ih));
720 if (!err)
721 err = fuse_copy_one(&cs, &arg, sizeof(arg));
722 fuse_copy_finish(&cs);
723
724 return err ? err : reqsize;
725}
726
334f485d
MS
727/*
728 * Read a single request into the userspace filesystem's buffer. This
729 * function waits until a request is available, then removes it from
730 * the pending list and copies request data to userspace buffer. If
f9a2842e
MS
731 * no reply is needed (FORGET) or request has been aborted or there
732 * was an error during the copying then it's finished by calling
334f485d
MS
733 * request_end(). Otherwise add it to the processing list, and set
734 * the 'sent' flag.
735 */
ee0b3e67
BP
736static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
737 unsigned long nr_segs, loff_t pos)
334f485d
MS
738{
739 int err;
334f485d
MS
740 struct fuse_req *req;
741 struct fuse_in *in;
742 struct fuse_copy_state cs;
743 unsigned reqsize;
ee0b3e67 744 struct file *file = iocb->ki_filp;
0720b315
MS
745 struct fuse_conn *fc = fuse_get_conn(file);
746 if (!fc)
747 return -EPERM;
334f485d 748
1d3d752b 749 restart:
d7133114 750 spin_lock(&fc->lock);
e5ac1d1e
JD
751 err = -EAGAIN;
752 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
a4d27e75 753 !request_pending(fc))
e5ac1d1e
JD
754 goto err_unlock;
755
334f485d
MS
756 request_wait(fc);
757 err = -ENODEV;
9ba7cbba 758 if (!fc->connected)
334f485d
MS
759 goto err_unlock;
760 err = -ERESTARTSYS;
a4d27e75 761 if (!request_pending(fc))
334f485d
MS
762 goto err_unlock;
763
a4d27e75
MS
764 if (!list_empty(&fc->interrupts)) {
765 req = list_entry(fc->interrupts.next, struct fuse_req,
766 intr_entry);
767 return fuse_read_interrupt(fc, req, iov, nr_segs);
768 }
769
334f485d 770 req = list_entry(fc->pending.next, struct fuse_req, list);
83cfd493 771 req->state = FUSE_REQ_READING;
d77a1d5b 772 list_move(&req->list, &fc->io);
334f485d
MS
773
774 in = &req->in;
1d3d752b
MS
775 reqsize = in->h.len;
776 /* If request is too large, reply with an error and restart the read */
777 if (iov_length(iov, nr_segs) < reqsize) {
778 req->out.h.error = -EIO;
779 /* SETXATTR is special, since it may contain too large data */
780 if (in->h.opcode == FUSE_SETXATTR)
781 req->out.h.error = -E2BIG;
782 request_end(fc, req);
783 goto restart;
334f485d 784 }
d7133114
MS
785 spin_unlock(&fc->lock);
786 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
1d3d752b
MS
787 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
788 if (!err)
789 err = fuse_copy_args(&cs, in->numargs, in->argpages,
790 (struct fuse_arg *) in->args, 0);
334f485d 791 fuse_copy_finish(&cs);
d7133114 792 spin_lock(&fc->lock);
334f485d 793 req->locked = 0;
c9c9d7df
MS
794 if (req->aborted) {
795 request_end(fc, req);
796 return -ENODEV;
797 }
334f485d 798 if (err) {
c9c9d7df 799 req->out.h.error = -EIO;
334f485d
MS
800 request_end(fc, req);
801 return err;
802 }
803 if (!req->isreply)
804 request_end(fc, req);
805 else {
83cfd493 806 req->state = FUSE_REQ_SENT;
d77a1d5b 807 list_move_tail(&req->list, &fc->processing);
a4d27e75
MS
808 if (req->interrupted)
809 queue_interrupt(fc, req);
d7133114 810 spin_unlock(&fc->lock);
334f485d
MS
811 }
812 return reqsize;
813
814 err_unlock:
d7133114 815 spin_unlock(&fc->lock);
334f485d
MS
816 return err;
817}
818
8599396b
TH
819static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
820 unsigned int size, struct fuse_copy_state *cs)
821{
822 switch (code) {
823 default:
824 return -EINVAL;
825 }
826}
827
334f485d
MS
828/* Look up request on processing list by unique ID */
829static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
830{
831 struct list_head *entry;
832
833 list_for_each(entry, &fc->processing) {
834 struct fuse_req *req;
835 req = list_entry(entry, struct fuse_req, list);
a4d27e75 836 if (req->in.h.unique == unique || req->intr_unique == unique)
334f485d
MS
837 return req;
838 }
839 return NULL;
840}
841
842static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
843 unsigned nbytes)
844{
845 unsigned reqsize = sizeof(struct fuse_out_header);
846
847 if (out->h.error)
848 return nbytes != reqsize ? -EINVAL : 0;
849
850 reqsize += len_args(out->numargs, out->args);
851
852 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
853 return -EINVAL;
854 else if (reqsize > nbytes) {
855 struct fuse_arg *lastarg = &out->args[out->numargs-1];
856 unsigned diffsize = reqsize - nbytes;
857 if (diffsize > lastarg->size)
858 return -EINVAL;
859 lastarg->size -= diffsize;
860 }
861 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
862 out->page_zeroing);
863}
864
865/*
866 * Write a single reply to a request. First the header is copied from
867 * the write buffer. The request is then searched on the processing
868 * list by the unique ID found in the header. If found, then remove
869 * it from the list and copy the rest of the buffer to the request.
870 * The request is finished by calling request_end()
871 */
ee0b3e67
BP
872static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
873 unsigned long nr_segs, loff_t pos)
334f485d
MS
874{
875 int err;
876 unsigned nbytes = iov_length(iov, nr_segs);
877 struct fuse_req *req;
878 struct fuse_out_header oh;
879 struct fuse_copy_state cs;
ee0b3e67 880 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
334f485d 881 if (!fc)
a87046d8 882 return -EPERM;
334f485d 883
d7133114 884 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
334f485d
MS
885 if (nbytes < sizeof(struct fuse_out_header))
886 return -EINVAL;
887
888 err = fuse_copy_one(&cs, &oh, sizeof(oh));
889 if (err)
890 goto err_finish;
8599396b
TH
891
892 err = -EINVAL;
893 if (oh.len != nbytes)
894 goto err_finish;
895
896 /*
897 * Zero oh.unique indicates unsolicited notification message
898 * and error contains notification code.
899 */
900 if (!oh.unique) {
901 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs);
902 fuse_copy_finish(&cs);
903 return err ? err : nbytes;
904 }
905
334f485d 906 err = -EINVAL;
8599396b 907 if (oh.error <= -1000 || oh.error > 0)
334f485d
MS
908 goto err_finish;
909
d7133114 910 spin_lock(&fc->lock);
69a53bf2
MS
911 err = -ENOENT;
912 if (!fc->connected)
913 goto err_unlock;
914
334f485d 915 req = request_find(fc, oh.unique);
334f485d
MS
916 if (!req)
917 goto err_unlock;
918
f9a2842e 919 if (req->aborted) {
d7133114 920 spin_unlock(&fc->lock);
334f485d 921 fuse_copy_finish(&cs);
d7133114 922 spin_lock(&fc->lock);
222f1d69 923 request_end(fc, req);
334f485d
MS
924 return -ENOENT;
925 }
a4d27e75
MS
926 /* Is it an interrupt reply? */
927 if (req->intr_unique == oh.unique) {
928 err = -EINVAL;
929 if (nbytes != sizeof(struct fuse_out_header))
930 goto err_unlock;
931
932 if (oh.error == -ENOSYS)
933 fc->no_interrupt = 1;
934 else if (oh.error == -EAGAIN)
935 queue_interrupt(fc, req);
936
937 spin_unlock(&fc->lock);
938 fuse_copy_finish(&cs);
939 return nbytes;
940 }
941
942 req->state = FUSE_REQ_WRITING;
d77a1d5b 943 list_move(&req->list, &fc->io);
334f485d
MS
944 req->out.h = oh;
945 req->locked = 1;
946 cs.req = req;
d7133114 947 spin_unlock(&fc->lock);
334f485d
MS
948
949 err = copy_out_args(&cs, &req->out, nbytes);
950 fuse_copy_finish(&cs);
951
d7133114 952 spin_lock(&fc->lock);
334f485d
MS
953 req->locked = 0;
954 if (!err) {
f9a2842e 955 if (req->aborted)
334f485d 956 err = -ENOENT;
f9a2842e 957 } else if (!req->aborted)
334f485d
MS
958 req->out.h.error = -EIO;
959 request_end(fc, req);
960
961 return err ? err : nbytes;
962
963 err_unlock:
d7133114 964 spin_unlock(&fc->lock);
334f485d
MS
965 err_finish:
966 fuse_copy_finish(&cs);
967 return err;
968}
969
334f485d
MS
970static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
971{
334f485d 972 unsigned mask = POLLOUT | POLLWRNORM;
7025d9ad 973 struct fuse_conn *fc = fuse_get_conn(file);
334f485d 974 if (!fc)
7025d9ad 975 return POLLERR;
334f485d
MS
976
977 poll_wait(file, &fc->waitq, wait);
978
d7133114 979 spin_lock(&fc->lock);
7025d9ad
MS
980 if (!fc->connected)
981 mask = POLLERR;
a4d27e75 982 else if (request_pending(fc))
7025d9ad 983 mask |= POLLIN | POLLRDNORM;
d7133114 984 spin_unlock(&fc->lock);
334f485d
MS
985
986 return mask;
987}
988
69a53bf2
MS
989/*
990 * Abort all requests on the given list (pending or processing)
991 *
d7133114 992 * This function releases and reacquires fc->lock
69a53bf2 993 */
334f485d
MS
994static void end_requests(struct fuse_conn *fc, struct list_head *head)
995{
996 while (!list_empty(head)) {
997 struct fuse_req *req;
998 req = list_entry(head->next, struct fuse_req, list);
334f485d
MS
999 req->out.h.error = -ECONNABORTED;
1000 request_end(fc, req);
d7133114 1001 spin_lock(&fc->lock);
334f485d
MS
1002 }
1003}
1004
69a53bf2
MS
1005/*
1006 * Abort requests under I/O
1007 *
f9a2842e 1008 * The requests are set to aborted and finished, and the request
69a53bf2
MS
1009 * waiter is woken up. This will make request_wait_answer() wait
1010 * until the request is unlocked and then return.
64c6d8ed
MS
1011 *
1012 * If the request is asynchronous, then the end function needs to be
1013 * called after waiting for the request to be unlocked (if it was
1014 * locked).
69a53bf2
MS
1015 */
1016static void end_io_requests(struct fuse_conn *fc)
4dbf930e 1017 __releases(fc->lock) __acquires(fc->lock)
69a53bf2
MS
1018{
1019 while (!list_empty(&fc->io)) {
64c6d8ed
MS
1020 struct fuse_req *req =
1021 list_entry(fc->io.next, struct fuse_req, list);
1022 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1023
f9a2842e 1024 req->aborted = 1;
69a53bf2
MS
1025 req->out.h.error = -ECONNABORTED;
1026 req->state = FUSE_REQ_FINISHED;
1027 list_del_init(&req->list);
1028 wake_up(&req->waitq);
64c6d8ed
MS
1029 if (end) {
1030 req->end = NULL;
64c6d8ed 1031 __fuse_get_request(req);
d7133114 1032 spin_unlock(&fc->lock);
64c6d8ed
MS
1033 wait_event(req->waitq, !req->locked);
1034 end(fc, req);
e9bb09dd 1035 fuse_put_request(fc, req);
d7133114 1036 spin_lock(&fc->lock);
64c6d8ed 1037 }
69a53bf2
MS
1038 }
1039}
1040
1041/*
1042 * Abort all requests.
1043 *
1044 * Emergency exit in case of a malicious or accidental deadlock, or
1045 * just a hung filesystem.
1046 *
1047 * The same effect is usually achievable through killing the
1048 * filesystem daemon and all users of the filesystem. The exception
1049 * is the combination of an asynchronous request and the tricky
1050 * deadlock (see Documentation/filesystems/fuse.txt).
1051 *
1052 * During the aborting, progression of requests from the pending and
1053 * processing lists onto the io list, and progression of new requests
1054 * onto the pending list is prevented by req->connected being false.
1055 *
1056 * Progression of requests under I/O to the processing list is
f9a2842e
MS
1057 * prevented by the req->aborted flag being true for these requests.
1058 * For this reason requests on the io list must be aborted first.
69a53bf2
MS
1059 */
1060void fuse_abort_conn(struct fuse_conn *fc)
1061{
d7133114 1062 spin_lock(&fc->lock);
69a53bf2
MS
1063 if (fc->connected) {
1064 fc->connected = 0;
51eb01e7 1065 fc->blocked = 0;
69a53bf2
MS
1066 end_io_requests(fc);
1067 end_requests(fc, &fc->pending);
1068 end_requests(fc, &fc->processing);
1069 wake_up_all(&fc->waitq);
51eb01e7 1070 wake_up_all(&fc->blocked_waitq);
385a17bf 1071 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
69a53bf2 1072 }
d7133114 1073 spin_unlock(&fc->lock);
69a53bf2
MS
1074}
1075
334f485d
MS
1076static int fuse_dev_release(struct inode *inode, struct file *file)
1077{
0720b315 1078 struct fuse_conn *fc = fuse_get_conn(file);
334f485d 1079 if (fc) {
d7133114 1080 spin_lock(&fc->lock);
1e9a4ed9 1081 fc->connected = 0;
334f485d
MS
1082 end_requests(fc, &fc->pending);
1083 end_requests(fc, &fc->processing);
d7133114 1084 spin_unlock(&fc->lock);
bafa9654 1085 fuse_conn_put(fc);
385a17bf 1086 }
f543f253 1087
334f485d
MS
1088 return 0;
1089}
1090
385a17bf
JD
1091static int fuse_dev_fasync(int fd, struct file *file, int on)
1092{
1093 struct fuse_conn *fc = fuse_get_conn(file);
1094 if (!fc)
a87046d8 1095 return -EPERM;
385a17bf
JD
1096
1097 /* No locking - fasync_helper does its own locking */
1098 return fasync_helper(fd, file, on, &fc->fasync);
1099}
1100
4b6f5d20 1101const struct file_operations fuse_dev_operations = {
334f485d
MS
1102 .owner = THIS_MODULE,
1103 .llseek = no_llseek,
ee0b3e67
BP
1104 .read = do_sync_read,
1105 .aio_read = fuse_dev_read,
1106 .write = do_sync_write,
1107 .aio_write = fuse_dev_write,
334f485d
MS
1108 .poll = fuse_dev_poll,
1109 .release = fuse_dev_release,
385a17bf 1110 .fasync = fuse_dev_fasync,
334f485d
MS
1111};
1112
1113static struct miscdevice fuse_miscdevice = {
1114 .minor = FUSE_MINOR,
1115 .name = "fuse",
1116 .fops = &fuse_dev_operations,
1117};
1118
1119int __init fuse_dev_init(void)
1120{
1121 int err = -ENOMEM;
1122 fuse_req_cachep = kmem_cache_create("fuse_request",
1123 sizeof(struct fuse_req),
20c2df83 1124 0, 0, NULL);
334f485d
MS
1125 if (!fuse_req_cachep)
1126 goto out;
1127
1128 err = misc_register(&fuse_miscdevice);
1129 if (err)
1130 goto out_cache_clean;
1131
1132 return 0;
1133
1134 out_cache_clean:
1135 kmem_cache_destroy(fuse_req_cachep);
1136 out:
1137 return err;
1138}
1139
1140void fuse_dev_cleanup(void)
1141{
1142 misc_deregister(&fuse_miscdevice);
1143 kmem_cache_destroy(fuse_req_cachep);
1144}