2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
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 #include <linux/pipe_fs_i.h>
20 #include <linux/swap.h>
21 #include <linux/splice.h>
23 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
24 MODULE_ALIAS("devname:fuse");
26 static struct kmem_cache *fuse_req_cachep;
28 static struct fuse_dev *fuse_get_dev(struct file *file)
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
34 return ACCESS_ONCE(file->private_data);
37 static void fuse_request_init(struct fuse_req *req, struct page **pages,
38 struct fuse_page_desc *page_descs,
41 memset(req, 0, sizeof(*req));
42 memset(pages, 0, sizeof(*pages) * npages);
43 memset(page_descs, 0, sizeof(*page_descs) * npages);
44 INIT_LIST_HEAD(&req->list);
45 INIT_LIST_HEAD(&req->intr_entry);
46 init_waitqueue_head(&req->waitq);
47 atomic_set(&req->count, 1);
49 req->page_descs = page_descs;
50 req->max_pages = npages;
51 __set_bit(FR_PENDING, &req->flags);
54 static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
56 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
59 struct fuse_page_desc *page_descs;
61 if (npages <= FUSE_REQ_INLINE_PAGES) {
62 pages = req->inline_pages;
63 page_descs = req->inline_page_descs;
65 pages = kmalloc(sizeof(struct page *) * npages, flags);
66 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
70 if (!pages || !page_descs) {
73 kmem_cache_free(fuse_req_cachep, req);
77 fuse_request_init(req, pages, page_descs, npages);
82 struct fuse_req *fuse_request_alloc(unsigned npages)
84 return __fuse_request_alloc(npages, GFP_KERNEL);
86 EXPORT_SYMBOL_GPL(fuse_request_alloc);
88 struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
90 return __fuse_request_alloc(npages, GFP_NOFS);
93 void fuse_request_free(struct fuse_req *req)
95 if (req->pages != req->inline_pages) {
97 kfree(req->page_descs);
99 kmem_cache_free(fuse_req_cachep, req);
102 static void block_sigs(sigset_t *oldset)
106 siginitsetinv(&mask, sigmask(SIGKILL));
107 sigprocmask(SIG_BLOCK, &mask, oldset);
110 static void restore_sigs(sigset_t *oldset)
112 sigprocmask(SIG_SETMASK, oldset, NULL);
115 void __fuse_get_request(struct fuse_req *req)
117 atomic_inc(&req->count);
120 /* Must be called with > 1 refcount */
121 static void __fuse_put_request(struct fuse_req *req)
123 BUG_ON(atomic_read(&req->count) < 2);
124 atomic_dec(&req->count);
127 static void fuse_req_init_context(struct fuse_req *req)
129 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
130 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
131 req->in.h.pid = current->pid;
134 void fuse_set_initialized(struct fuse_conn *fc)
136 /* Make sure stores before this are seen on another CPU */
141 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
143 return !fc->initialized || (for_background && fc->blocked);
146 static void fuse_drop_waiting(struct fuse_conn *fc)
149 * lockess check of fc->connected is okay, because atomic_dec_and_test()
150 * provides a memory barrier mached with the one in fuse_wait_aborted()
151 * to ensure no wake-up is missed.
153 if (atomic_dec_and_test(&fc->num_waiting) &&
154 !READ_ONCE(fc->connected)) {
155 /* wake up aborters */
156 wake_up_all(&fc->blocked_waitq);
160 static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
163 struct fuse_req *req;
165 atomic_inc(&fc->num_waiting);
167 if (fuse_block_alloc(fc, for_background)) {
172 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
173 !fuse_block_alloc(fc, for_background));
174 restore_sigs(&oldset);
179 /* Matches smp_wmb() in fuse_set_initialized() */
190 req = fuse_request_alloc(npages);
194 wake_up(&fc->blocked_waitq);
198 fuse_req_init_context(req);
199 __set_bit(FR_WAITING, &req->flags);
201 __set_bit(FR_BACKGROUND, &req->flags);
206 fuse_drop_waiting(fc);
210 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
212 return __fuse_get_req(fc, npages, false);
214 EXPORT_SYMBOL_GPL(fuse_get_req);
216 struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
219 return __fuse_get_req(fc, npages, true);
221 EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
224 * Return request in fuse_file->reserved_req. However that may
225 * currently be in use. If that is the case, wait for it to become
228 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
231 struct fuse_req *req = NULL;
232 struct fuse_file *ff = file->private_data;
235 wait_event(fc->reserved_req_waitq, ff->reserved_req);
236 spin_lock(&fc->lock);
237 if (ff->reserved_req) {
238 req = ff->reserved_req;
239 ff->reserved_req = NULL;
240 req->stolen_file = get_file(file);
242 spin_unlock(&fc->lock);
249 * Put stolen request back into fuse_file->reserved_req
251 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
253 struct file *file = req->stolen_file;
254 struct fuse_file *ff = file->private_data;
256 spin_lock(&fc->lock);
257 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
258 BUG_ON(ff->reserved_req);
259 ff->reserved_req = req;
260 wake_up_all(&fc->reserved_req_waitq);
261 spin_unlock(&fc->lock);
266 * Gets a requests for a file operation, always succeeds
268 * This is used for sending the FLUSH request, which must get to
269 * userspace, due to POSIX locks which may need to be unlocked.
271 * If allocation fails due to OOM, use the reserved request in
274 * This is very unlikely to deadlock accidentally, since the
275 * filesystem should not have it's own file open. If deadlock is
276 * intentional, it can still be broken by "aborting" the filesystem.
278 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
281 struct fuse_req *req;
283 atomic_inc(&fc->num_waiting);
284 wait_event(fc->blocked_waitq, fc->initialized);
285 /* Matches smp_wmb() in fuse_set_initialized() */
287 req = fuse_request_alloc(0);
289 req = get_reserved_req(fc, file);
291 fuse_req_init_context(req);
292 __set_bit(FR_WAITING, &req->flags);
293 __clear_bit(FR_BACKGROUND, &req->flags);
297 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
299 if (atomic_dec_and_test(&req->count)) {
300 if (test_bit(FR_BACKGROUND, &req->flags)) {
302 * We get here in the unlikely case that a background
303 * request was allocated but not sent
305 spin_lock(&fc->lock);
307 wake_up(&fc->blocked_waitq);
308 spin_unlock(&fc->lock);
311 if (test_bit(FR_WAITING, &req->flags)) {
312 __clear_bit(FR_WAITING, &req->flags);
313 fuse_drop_waiting(fc);
316 if (req->stolen_file)
317 put_reserved_req(fc, req);
319 fuse_request_free(req);
322 EXPORT_SYMBOL_GPL(fuse_put_request);
324 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
329 for (i = 0; i < numargs; i++)
330 nbytes += args[i].size;
335 static u64 fuse_get_unique(struct fuse_iqueue *fiq)
337 return ++fiq->reqctr;
340 static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
342 req->in.h.len = sizeof(struct fuse_in_header) +
343 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
344 list_add_tail(&req->list, &fiq->pending);
345 wake_up_locked(&fiq->waitq);
346 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
349 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
350 u64 nodeid, u64 nlookup)
352 struct fuse_iqueue *fiq = &fc->iq;
354 forget->forget_one.nodeid = nodeid;
355 forget->forget_one.nlookup = nlookup;
357 spin_lock(&fiq->waitq.lock);
358 if (fiq->connected) {
359 fiq->forget_list_tail->next = forget;
360 fiq->forget_list_tail = forget;
361 wake_up_locked(&fiq->waitq);
362 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
366 spin_unlock(&fiq->waitq.lock);
369 static void flush_bg_queue(struct fuse_conn *fc)
371 while (fc->active_background < fc->max_background &&
372 !list_empty(&fc->bg_queue)) {
373 struct fuse_req *req;
374 struct fuse_iqueue *fiq = &fc->iq;
376 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
377 list_del(&req->list);
378 fc->active_background++;
379 spin_lock(&fiq->waitq.lock);
380 req->in.h.unique = fuse_get_unique(fiq);
381 queue_request(fiq, req);
382 spin_unlock(&fiq->waitq.lock);
387 * This function is called when a request is finished. Either a reply
388 * has arrived or it was aborted (and not yet sent) or some error
389 * occurred during communication with userspace, or the device file
390 * was closed. The requester thread is woken up (if still waiting),
391 * the 'end' callback is called if given, else the reference to the
392 * request is released
394 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
396 struct fuse_iqueue *fiq = &fc->iq;
398 if (test_and_set_bit(FR_FINISHED, &req->flags))
401 spin_lock(&fiq->waitq.lock);
402 list_del_init(&req->intr_entry);
403 spin_unlock(&fiq->waitq.lock);
404 WARN_ON(test_bit(FR_PENDING, &req->flags));
405 WARN_ON(test_bit(FR_SENT, &req->flags));
406 if (test_bit(FR_BACKGROUND, &req->flags)) {
407 spin_lock(&fc->lock);
408 clear_bit(FR_BACKGROUND, &req->flags);
409 if (fc->num_background == fc->max_background) {
411 wake_up(&fc->blocked_waitq);
412 } else if (!fc->blocked) {
414 * Wake up next waiter, if any. It's okay to use
415 * waitqueue_active(), as we've already synced up
416 * fc->blocked with waiters with the wake_up() call
419 if (waitqueue_active(&fc->blocked_waitq))
420 wake_up(&fc->blocked_waitq);
423 if (fc->num_background == fc->congestion_threshold &&
424 fc->connected && fc->bdi_initialized) {
425 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
426 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
428 fc->num_background--;
429 fc->active_background--;
431 spin_unlock(&fc->lock);
433 wake_up(&req->waitq);
437 fuse_put_request(fc, req);
440 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
442 spin_lock(&fiq->waitq.lock);
443 if (test_bit(FR_FINISHED, &req->flags)) {
444 spin_unlock(&fiq->waitq.lock);
447 if (list_empty(&req->intr_entry)) {
448 list_add_tail(&req->intr_entry, &fiq->interrupts);
449 wake_up_locked(&fiq->waitq);
451 spin_unlock(&fiq->waitq.lock);
452 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
455 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
457 struct fuse_iqueue *fiq = &fc->iq;
460 if (!fc->no_interrupt) {
461 /* Any signal may interrupt this */
462 err = wait_event_interruptible(req->waitq,
463 test_bit(FR_FINISHED, &req->flags));
467 set_bit(FR_INTERRUPTED, &req->flags);
468 /* matches barrier in fuse_dev_do_read() */
469 smp_mb__after_atomic();
470 if (test_bit(FR_SENT, &req->flags))
471 queue_interrupt(fiq, req);
474 if (!test_bit(FR_FORCE, &req->flags)) {
477 /* Only fatal signals may interrupt this */
479 err = wait_event_interruptible(req->waitq,
480 test_bit(FR_FINISHED, &req->flags));
481 restore_sigs(&oldset);
486 spin_lock(&fiq->waitq.lock);
487 /* Request is not yet in userspace, bail out */
488 if (test_bit(FR_PENDING, &req->flags)) {
489 list_del(&req->list);
490 spin_unlock(&fiq->waitq.lock);
491 __fuse_put_request(req);
492 req->out.h.error = -EINTR;
495 spin_unlock(&fiq->waitq.lock);
499 * Either request is already in userspace, or it was forced.
502 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
505 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
507 struct fuse_iqueue *fiq = &fc->iq;
509 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
510 spin_lock(&fiq->waitq.lock);
511 if (!fiq->connected) {
512 spin_unlock(&fiq->waitq.lock);
513 req->out.h.error = -ENOTCONN;
515 req->in.h.unique = fuse_get_unique(fiq);
516 queue_request(fiq, req);
517 /* acquire extra reference, since request is still needed
518 after request_end() */
519 __fuse_get_request(req);
520 spin_unlock(&fiq->waitq.lock);
522 request_wait_answer(fc, req);
523 /* Pairs with smp_wmb() in request_end() */
528 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
530 __set_bit(FR_ISREPLY, &req->flags);
531 if (!test_bit(FR_WAITING, &req->flags)) {
532 __set_bit(FR_WAITING, &req->flags);
533 atomic_inc(&fc->num_waiting);
535 __fuse_request_send(fc, req);
537 EXPORT_SYMBOL_GPL(fuse_request_send);
539 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
541 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
542 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
545 switch (args->in.h.opcode) {
552 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
556 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
560 if (fc->minor < 12) {
561 switch (args->in.h.opcode) {
563 args->in.args[0].size = sizeof(struct fuse_open_in);
566 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
572 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
574 struct fuse_req *req;
577 req = fuse_get_req(fc, 0);
581 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
582 fuse_adjust_compat(fc, args);
584 req->in.h.opcode = args->in.h.opcode;
585 req->in.h.nodeid = args->in.h.nodeid;
586 req->in.numargs = args->in.numargs;
587 memcpy(req->in.args, args->in.args,
588 args->in.numargs * sizeof(struct fuse_in_arg));
589 req->out.argvar = args->out.argvar;
590 req->out.numargs = args->out.numargs;
591 memcpy(req->out.args, args->out.args,
592 args->out.numargs * sizeof(struct fuse_arg));
593 fuse_request_send(fc, req);
594 ret = req->out.h.error;
595 if (!ret && args->out.argvar) {
596 BUG_ON(args->out.numargs != 1);
597 ret = req->out.args[0].size;
599 fuse_put_request(fc, req);
605 * Called under fc->lock
607 * fc->connected must have been checked previously
609 void fuse_request_send_background_locked(struct fuse_conn *fc,
610 struct fuse_req *req)
612 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
613 if (!test_bit(FR_WAITING, &req->flags)) {
614 __set_bit(FR_WAITING, &req->flags);
615 atomic_inc(&fc->num_waiting);
617 __set_bit(FR_ISREPLY, &req->flags);
618 fc->num_background++;
619 if (fc->num_background == fc->max_background)
621 if (fc->num_background == fc->congestion_threshold &&
622 fc->bdi_initialized) {
623 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
624 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
626 list_add_tail(&req->list, &fc->bg_queue);
630 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
633 spin_lock(&fc->lock);
635 fuse_request_send_background_locked(fc, req);
636 spin_unlock(&fc->lock);
638 spin_unlock(&fc->lock);
639 req->out.h.error = -ENOTCONN;
641 fuse_put_request(fc, req);
644 EXPORT_SYMBOL_GPL(fuse_request_send_background);
646 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
647 struct fuse_req *req, u64 unique)
650 struct fuse_iqueue *fiq = &fc->iq;
652 __clear_bit(FR_ISREPLY, &req->flags);
653 req->in.h.unique = unique;
654 spin_lock(&fiq->waitq.lock);
655 if (fiq->connected) {
656 queue_request(fiq, req);
659 spin_unlock(&fiq->waitq.lock);
664 void fuse_force_forget(struct file *file, u64 nodeid)
666 struct inode *inode = file_inode(file);
667 struct fuse_conn *fc = get_fuse_conn(inode);
668 struct fuse_req *req;
669 struct fuse_forget_in inarg;
671 memset(&inarg, 0, sizeof(inarg));
673 req = fuse_get_req_nofail_nopages(fc, file);
674 req->in.h.opcode = FUSE_FORGET;
675 req->in.h.nodeid = nodeid;
677 req->in.args[0].size = sizeof(inarg);
678 req->in.args[0].value = &inarg;
679 __clear_bit(FR_ISREPLY, &req->flags);
680 __fuse_request_send(fc, req);
682 fuse_put_request(fc, req);
686 * Lock the request. Up to the next unlock_request() there mustn't be
687 * anything that could cause a page-fault. If the request was already
690 static int lock_request(struct fuse_req *req)
694 spin_lock(&req->waitq.lock);
695 if (test_bit(FR_ABORTED, &req->flags))
698 set_bit(FR_LOCKED, &req->flags);
699 spin_unlock(&req->waitq.lock);
705 * Unlock request. If it was aborted while locked, caller is responsible
706 * for unlocking and ending the request.
708 static int unlock_request(struct fuse_req *req)
712 spin_lock(&req->waitq.lock);
713 if (test_bit(FR_ABORTED, &req->flags))
716 clear_bit(FR_LOCKED, &req->flags);
717 spin_unlock(&req->waitq.lock);
722 struct fuse_copy_state {
724 struct fuse_req *req;
725 struct iov_iter *iter;
726 struct pipe_buffer *pipebufs;
727 struct pipe_buffer *currbuf;
728 struct pipe_inode_info *pipe;
729 unsigned long nr_segs;
733 unsigned move_pages:1;
736 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
737 struct iov_iter *iter)
739 memset(cs, 0, sizeof(*cs));
744 /* Unmap and put previous page of userspace buffer */
745 static void fuse_copy_finish(struct fuse_copy_state *cs)
748 struct pipe_buffer *buf = cs->currbuf;
751 buf->len = PAGE_SIZE - cs->len;
755 flush_dcache_page(cs->pg);
756 set_page_dirty_lock(cs->pg);
764 * Get another pagefull of userspace buffer, and map it to kernel
765 * address space, and lock request
767 static int fuse_copy_fill(struct fuse_copy_state *cs)
772 err = unlock_request(cs->req);
776 fuse_copy_finish(cs);
778 struct pipe_buffer *buf = cs->pipebufs;
781 err = buf->ops->confirm(cs->pipe, buf);
785 BUG_ON(!cs->nr_segs);
788 cs->offset = buf->offset;
793 if (cs->nr_segs == cs->pipe->buffers)
796 page = alloc_page(GFP_HIGHUSER);
813 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
821 iov_iter_advance(cs->iter, err);
824 return lock_request(cs->req);
827 /* Do as much copy to/from userspace buffer as we can */
828 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
830 unsigned ncpy = min(*size, cs->len);
832 void *pgaddr = kmap_atomic(cs->pg);
833 void *buf = pgaddr + cs->offset;
836 memcpy(buf, *val, ncpy);
838 memcpy(*val, buf, ncpy);
840 kunmap_atomic(pgaddr);
849 static int fuse_check_page(struct page *page)
851 if (page_mapcount(page) ||
852 page->mapping != NULL ||
853 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
860 printk(KERN_WARNING "fuse: trying to steal weird page\n");
861 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
867 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
870 struct page *oldpage = *pagep;
871 struct page *newpage;
872 struct pipe_buffer *buf = cs->pipebufs;
874 err = unlock_request(cs->req);
878 fuse_copy_finish(cs);
880 err = buf->ops->confirm(cs->pipe, buf);
884 BUG_ON(!cs->nr_segs);
890 if (cs->len != PAGE_SIZE)
893 if (buf->ops->steal(cs->pipe, buf) != 0)
898 if (!PageUptodate(newpage))
899 SetPageUptodate(newpage);
901 ClearPageMappedToDisk(newpage);
903 if (fuse_check_page(newpage) != 0)
904 goto out_fallback_unlock;
907 * This is a new and locked page, it shouldn't be mapped or
908 * have any special flags on it
910 if (WARN_ON(page_mapped(oldpage)))
911 goto out_fallback_unlock;
912 if (WARN_ON(page_has_private(oldpage)))
913 goto out_fallback_unlock;
914 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
915 goto out_fallback_unlock;
916 if (WARN_ON(PageMlocked(oldpage)))
917 goto out_fallback_unlock;
919 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
921 unlock_page(newpage);
925 page_cache_get(newpage);
927 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
928 lru_cache_add_file(newpage);
931 spin_lock(&cs->req->waitq.lock);
932 if (test_bit(FR_ABORTED, &cs->req->flags))
936 spin_unlock(&cs->req->waitq.lock);
939 unlock_page(newpage);
940 page_cache_release(newpage);
944 unlock_page(oldpage);
945 page_cache_release(oldpage);
951 unlock_page(newpage);
954 cs->offset = buf->offset;
956 err = lock_request(cs->req);
963 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
964 unsigned offset, unsigned count)
966 struct pipe_buffer *buf;
969 if (cs->nr_segs == cs->pipe->buffers)
972 err = unlock_request(cs->req);
976 fuse_copy_finish(cs);
979 page_cache_get(page);
981 buf->offset = offset;
992 * Copy a page in the request to/from the userspace buffer. Must be
995 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
996 unsigned offset, unsigned count, int zeroing)
999 struct page *page = *pagep;
1001 if (page && zeroing && count < PAGE_SIZE)
1002 clear_highpage(page);
1005 if (cs->write && cs->pipebufs && page) {
1006 return fuse_ref_page(cs, page, offset, count);
1007 } else if (!cs->len) {
1008 if (cs->move_pages && page &&
1009 offset == 0 && count == PAGE_SIZE) {
1010 err = fuse_try_move_page(cs, pagep);
1014 err = fuse_copy_fill(cs);
1020 void *mapaddr = kmap_atomic(page);
1021 void *buf = mapaddr + offset;
1022 offset += fuse_copy_do(cs, &buf, &count);
1023 kunmap_atomic(mapaddr);
1025 offset += fuse_copy_do(cs, NULL, &count);
1027 if (page && !cs->write)
1028 flush_dcache_page(page);
1032 /* Copy pages in the request to/from userspace buffer */
1033 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1037 struct fuse_req *req = cs->req;
1039 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1041 unsigned offset = req->page_descs[i].offset;
1042 unsigned count = min(nbytes, req->page_descs[i].length);
1044 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1054 /* Copy a single argument in the request to/from userspace buffer */
1055 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1059 int err = fuse_copy_fill(cs);
1063 fuse_copy_do(cs, &val, &size);
1068 /* Copy request arguments to/from userspace buffer */
1069 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1070 unsigned argpages, struct fuse_arg *args,
1076 for (i = 0; !err && i < numargs; i++) {
1077 struct fuse_arg *arg = &args[i];
1078 if (i == numargs - 1 && argpages)
1079 err = fuse_copy_pages(cs, arg->size, zeroing);
1081 err = fuse_copy_one(cs, arg->value, arg->size);
1086 static int forget_pending(struct fuse_iqueue *fiq)
1088 return fiq->forget_list_head.next != NULL;
1091 static int request_pending(struct fuse_iqueue *fiq)
1093 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1094 forget_pending(fiq);
1098 * Transfer an interrupt request to userspace
1100 * Unlike other requests this is assembled on demand, without a need
1101 * to allocate a separate fuse_req structure.
1103 * Called with fiq->waitq.lock held, releases it
1105 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1106 struct fuse_copy_state *cs,
1107 size_t nbytes, struct fuse_req *req)
1108 __releases(fiq->waitq.lock)
1110 struct fuse_in_header ih;
1111 struct fuse_interrupt_in arg;
1112 unsigned reqsize = sizeof(ih) + sizeof(arg);
1115 list_del_init(&req->intr_entry);
1116 req->intr_unique = fuse_get_unique(fiq);
1117 memset(&ih, 0, sizeof(ih));
1118 memset(&arg, 0, sizeof(arg));
1120 ih.opcode = FUSE_INTERRUPT;
1121 ih.unique = req->intr_unique;
1122 arg.unique = req->in.h.unique;
1124 spin_unlock(&fiq->waitq.lock);
1125 if (nbytes < reqsize)
1128 err = fuse_copy_one(cs, &ih, sizeof(ih));
1130 err = fuse_copy_one(cs, &arg, sizeof(arg));
1131 fuse_copy_finish(cs);
1133 return err ? err : reqsize;
1136 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1140 struct fuse_forget_link *head = fiq->forget_list_head.next;
1141 struct fuse_forget_link **newhead = &head;
1144 for (count = 0; *newhead != NULL && count < max; count++)
1145 newhead = &(*newhead)->next;
1147 fiq->forget_list_head.next = *newhead;
1149 if (fiq->forget_list_head.next == NULL)
1150 fiq->forget_list_tail = &fiq->forget_list_head;
1158 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1159 struct fuse_copy_state *cs,
1161 __releases(fiq->waitq.lock)
1164 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1165 struct fuse_forget_in arg = {
1166 .nlookup = forget->forget_one.nlookup,
1168 struct fuse_in_header ih = {
1169 .opcode = FUSE_FORGET,
1170 .nodeid = forget->forget_one.nodeid,
1171 .unique = fuse_get_unique(fiq),
1172 .len = sizeof(ih) + sizeof(arg),
1175 spin_unlock(&fiq->waitq.lock);
1177 if (nbytes < ih.len)
1180 err = fuse_copy_one(cs, &ih, sizeof(ih));
1182 err = fuse_copy_one(cs, &arg, sizeof(arg));
1183 fuse_copy_finish(cs);
1191 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1192 struct fuse_copy_state *cs, size_t nbytes)
1193 __releases(fiq->waitq.lock)
1196 unsigned max_forgets;
1198 struct fuse_forget_link *head;
1199 struct fuse_batch_forget_in arg = { .count = 0 };
1200 struct fuse_in_header ih = {
1201 .opcode = FUSE_BATCH_FORGET,
1202 .unique = fuse_get_unique(fiq),
1203 .len = sizeof(ih) + sizeof(arg),
1206 if (nbytes < ih.len) {
1207 spin_unlock(&fiq->waitq.lock);
1211 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1212 head = dequeue_forget(fiq, max_forgets, &count);
1213 spin_unlock(&fiq->waitq.lock);
1216 ih.len += count * sizeof(struct fuse_forget_one);
1217 err = fuse_copy_one(cs, &ih, sizeof(ih));
1219 err = fuse_copy_one(cs, &arg, sizeof(arg));
1222 struct fuse_forget_link *forget = head;
1225 err = fuse_copy_one(cs, &forget->forget_one,
1226 sizeof(forget->forget_one));
1228 head = forget->next;
1232 fuse_copy_finish(cs);
1240 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1241 struct fuse_copy_state *cs,
1243 __releases(fiq->waitq.lock)
1245 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1246 return fuse_read_single_forget(fiq, cs, nbytes);
1248 return fuse_read_batch_forget(fiq, cs, nbytes);
1252 * Read a single request into the userspace filesystem's buffer. This
1253 * function waits until a request is available, then removes it from
1254 * the pending list and copies request data to userspace buffer. If
1255 * no reply is needed (FORGET) or request has been aborted or there
1256 * was an error during the copying then it's finished by calling
1257 * request_end(). Otherwise add it to the processing list, and set
1260 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1261 struct fuse_copy_state *cs, size_t nbytes)
1264 struct fuse_conn *fc = fud->fc;
1265 struct fuse_iqueue *fiq = &fc->iq;
1266 struct fuse_pqueue *fpq = &fud->pq;
1267 struct fuse_req *req;
1272 spin_lock(&fiq->waitq.lock);
1274 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1275 !request_pending(fiq))
1278 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1279 !fiq->connected || request_pending(fiq));
1284 if (!fiq->connected)
1287 if (!list_empty(&fiq->interrupts)) {
1288 req = list_entry(fiq->interrupts.next, struct fuse_req,
1290 return fuse_read_interrupt(fiq, cs, nbytes, req);
1293 if (forget_pending(fiq)) {
1294 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1295 return fuse_read_forget(fc, fiq, cs, nbytes);
1297 if (fiq->forget_batch <= -8)
1298 fiq->forget_batch = 16;
1301 req = list_entry(fiq->pending.next, struct fuse_req, list);
1302 clear_bit(FR_PENDING, &req->flags);
1303 list_del_init(&req->list);
1304 spin_unlock(&fiq->waitq.lock);
1307 reqsize = in->h.len;
1308 /* If request is too large, reply with an error and restart the read */
1309 if (nbytes < reqsize) {
1310 req->out.h.error = -EIO;
1311 /* SETXATTR is special, since it may contain too large data */
1312 if (in->h.opcode == FUSE_SETXATTR)
1313 req->out.h.error = -E2BIG;
1314 request_end(fc, req);
1317 spin_lock(&fpq->lock);
1319 * Must not put request on fpq->io queue after having been shut down by
1322 if (!fpq->connected) {
1323 req->out.h.error = err = -ECONNABORTED;
1327 list_add(&req->list, &fpq->io);
1328 spin_unlock(&fpq->lock);
1330 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1332 err = fuse_copy_args(cs, in->numargs, in->argpages,
1333 (struct fuse_arg *) in->args, 0);
1334 fuse_copy_finish(cs);
1335 spin_lock(&fpq->lock);
1336 clear_bit(FR_LOCKED, &req->flags);
1337 if (!fpq->connected) {
1342 req->out.h.error = -EIO;
1345 if (!test_bit(FR_ISREPLY, &req->flags)) {
1349 list_move_tail(&req->list, &fpq->processing);
1350 __fuse_get_request(req);
1351 set_bit(FR_SENT, &req->flags);
1352 spin_unlock(&fpq->lock);
1353 /* matches barrier in request_wait_answer() */
1354 smp_mb__after_atomic();
1355 if (test_bit(FR_INTERRUPTED, &req->flags))
1356 queue_interrupt(fiq, req);
1357 fuse_put_request(fc, req);
1362 if (!test_bit(FR_PRIVATE, &req->flags))
1363 list_del_init(&req->list);
1364 spin_unlock(&fpq->lock);
1365 request_end(fc, req);
1369 spin_unlock(&fiq->waitq.lock);
1373 static int fuse_dev_open(struct inode *inode, struct file *file)
1376 * The fuse device's file's private_data is used to hold
1377 * the fuse_conn(ection) when it is mounted, and is used to
1378 * keep track of whether the file has been mounted already.
1380 file->private_data = NULL;
1384 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1386 struct fuse_copy_state cs;
1387 struct file *file = iocb->ki_filp;
1388 struct fuse_dev *fud = fuse_get_dev(file);
1393 if (!iter_is_iovec(to))
1396 fuse_copy_init(&cs, 1, to);
1398 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1401 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1402 struct pipe_inode_info *pipe,
1403 size_t len, unsigned int flags)
1408 struct pipe_buffer *bufs;
1409 struct fuse_copy_state cs;
1410 struct fuse_dev *fud = fuse_get_dev(in);
1415 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1419 fuse_copy_init(&cs, 1, NULL);
1422 ret = fuse_dev_do_read(fud, in, &cs, len);
1429 if (!pipe->readers) {
1430 send_sig(SIGPIPE, current, 0);
1436 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1441 while (page_nr < cs.nr_segs) {
1442 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1443 struct pipe_buffer *buf = pipe->bufs + newbuf;
1445 buf->page = bufs[page_nr].page;
1446 buf->offset = bufs[page_nr].offset;
1447 buf->len = bufs[page_nr].len;
1449 * Need to be careful about this. Having buf->ops in module
1450 * code can Oops if the buffer persists after module unload.
1452 buf->ops = &nosteal_pipe_buf_ops;
1467 if (waitqueue_active(&pipe->wait))
1468 wake_up_interruptible(&pipe->wait);
1469 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1473 for (; page_nr < cs.nr_segs; page_nr++)
1474 page_cache_release(bufs[page_nr].page);
1480 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1481 struct fuse_copy_state *cs)
1483 struct fuse_notify_poll_wakeup_out outarg;
1486 if (size != sizeof(outarg))
1489 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1493 fuse_copy_finish(cs);
1494 return fuse_notify_poll_wakeup(fc, &outarg);
1497 fuse_copy_finish(cs);
1501 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1502 struct fuse_copy_state *cs)
1504 struct fuse_notify_inval_inode_out outarg;
1507 if (size != sizeof(outarg))
1510 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1513 fuse_copy_finish(cs);
1515 down_read(&fc->killsb);
1518 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1519 outarg.off, outarg.len);
1521 up_read(&fc->killsb);
1525 fuse_copy_finish(cs);
1529 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1530 struct fuse_copy_state *cs)
1532 struct fuse_notify_inval_entry_out outarg;
1537 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1542 if (size < sizeof(outarg))
1545 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1549 err = -ENAMETOOLONG;
1550 if (outarg.namelen > FUSE_NAME_MAX)
1554 if (size != sizeof(outarg) + outarg.namelen + 1)
1558 name.len = outarg.namelen;
1559 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1562 fuse_copy_finish(cs);
1563 buf[outarg.namelen] = 0;
1564 name.hash = full_name_hash(name.name, name.len);
1566 down_read(&fc->killsb);
1569 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1570 up_read(&fc->killsb);
1576 fuse_copy_finish(cs);
1580 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1581 struct fuse_copy_state *cs)
1583 struct fuse_notify_delete_out outarg;
1588 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1593 if (size < sizeof(outarg))
1596 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1600 err = -ENAMETOOLONG;
1601 if (outarg.namelen > FUSE_NAME_MAX)
1605 if (size != sizeof(outarg) + outarg.namelen + 1)
1609 name.len = outarg.namelen;
1610 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1613 fuse_copy_finish(cs);
1614 buf[outarg.namelen] = 0;
1615 name.hash = full_name_hash(name.name, name.len);
1617 down_read(&fc->killsb);
1620 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1621 outarg.child, &name);
1622 up_read(&fc->killsb);
1628 fuse_copy_finish(cs);
1632 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1633 struct fuse_copy_state *cs)
1635 struct fuse_notify_store_out outarg;
1636 struct inode *inode;
1637 struct address_space *mapping;
1641 unsigned int offset;
1647 if (size < sizeof(outarg))
1650 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1655 if (size - sizeof(outarg) != outarg.size)
1658 nodeid = outarg.nodeid;
1660 down_read(&fc->killsb);
1666 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1670 mapping = inode->i_mapping;
1671 index = outarg.offset >> PAGE_CACHE_SHIFT;
1672 offset = outarg.offset & ~PAGE_CACHE_MASK;
1673 file_size = i_size_read(inode);
1674 end = outarg.offset + outarg.size;
1675 if (end > file_size) {
1677 fuse_write_update_size(inode, file_size);
1683 unsigned int this_num;
1686 page = find_or_create_page(mapping, index,
1687 mapping_gfp_mask(mapping));
1691 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1692 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1693 if (!err && offset == 0 &&
1694 (this_num == PAGE_CACHE_SIZE || file_size == end))
1695 SetPageUptodate(page);
1697 page_cache_release(page);
1712 up_read(&fc->killsb);
1714 fuse_copy_finish(cs);
1718 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1720 release_pages(req->pages, req->num_pages, false);
1723 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1724 struct fuse_notify_retrieve_out *outarg)
1727 struct address_space *mapping = inode->i_mapping;
1728 struct fuse_req *req;
1732 unsigned int offset;
1733 size_t total_len = 0;
1736 offset = outarg->offset & ~PAGE_CACHE_MASK;
1737 file_size = i_size_read(inode);
1739 num = min(outarg->size, fc->max_write);
1740 if (outarg->offset > file_size)
1742 else if (outarg->offset + num > file_size)
1743 num = file_size - outarg->offset;
1745 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1746 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1748 req = fuse_get_req(fc, num_pages);
1750 return PTR_ERR(req);
1752 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1753 req->in.h.nodeid = outarg->nodeid;
1754 req->in.numargs = 2;
1755 req->in.argpages = 1;
1756 req->end = fuse_retrieve_end;
1758 index = outarg->offset >> PAGE_CACHE_SHIFT;
1760 while (num && req->num_pages < num_pages) {
1762 unsigned int this_num;
1764 page = find_get_page(mapping, index);
1768 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1769 req->pages[req->num_pages] = page;
1770 req->page_descs[req->num_pages].offset = offset;
1771 req->page_descs[req->num_pages].length = this_num;
1776 total_len += this_num;
1779 req->misc.retrieve_in.offset = outarg->offset;
1780 req->misc.retrieve_in.size = total_len;
1781 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1782 req->in.args[0].value = &req->misc.retrieve_in;
1783 req->in.args[1].size = total_len;
1785 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1787 fuse_retrieve_end(fc, req);
1788 fuse_put_request(fc, req);
1794 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1795 struct fuse_copy_state *cs)
1797 struct fuse_notify_retrieve_out outarg;
1798 struct inode *inode;
1802 if (size != sizeof(outarg))
1805 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1809 fuse_copy_finish(cs);
1811 down_read(&fc->killsb);
1814 u64 nodeid = outarg.nodeid;
1816 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1818 err = fuse_retrieve(fc, inode, &outarg);
1822 up_read(&fc->killsb);
1827 fuse_copy_finish(cs);
1831 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1832 unsigned int size, struct fuse_copy_state *cs)
1834 /* Don't try to move pages (yet) */
1838 case FUSE_NOTIFY_POLL:
1839 return fuse_notify_poll(fc, size, cs);
1841 case FUSE_NOTIFY_INVAL_INODE:
1842 return fuse_notify_inval_inode(fc, size, cs);
1844 case FUSE_NOTIFY_INVAL_ENTRY:
1845 return fuse_notify_inval_entry(fc, size, cs);
1847 case FUSE_NOTIFY_STORE:
1848 return fuse_notify_store(fc, size, cs);
1850 case FUSE_NOTIFY_RETRIEVE:
1851 return fuse_notify_retrieve(fc, size, cs);
1853 case FUSE_NOTIFY_DELETE:
1854 return fuse_notify_delete(fc, size, cs);
1857 fuse_copy_finish(cs);
1862 /* Look up request on processing list by unique ID */
1863 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1865 struct fuse_req *req;
1867 list_for_each_entry(req, &fpq->processing, list) {
1868 if (req->in.h.unique == unique || req->intr_unique == unique)
1874 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1877 unsigned reqsize = sizeof(struct fuse_out_header);
1880 return nbytes != reqsize ? -EINVAL : 0;
1882 reqsize += len_args(out->numargs, out->args);
1884 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1886 else if (reqsize > nbytes) {
1887 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1888 unsigned diffsize = reqsize - nbytes;
1889 if (diffsize > lastarg->size)
1891 lastarg->size -= diffsize;
1893 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1898 * Write a single reply to a request. First the header is copied from
1899 * the write buffer. The request is then searched on the processing
1900 * list by the unique ID found in the header. If found, then remove
1901 * it from the list and copy the rest of the buffer to the request.
1902 * The request is finished by calling request_end()
1904 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1905 struct fuse_copy_state *cs, size_t nbytes)
1908 struct fuse_conn *fc = fud->fc;
1909 struct fuse_pqueue *fpq = &fud->pq;
1910 struct fuse_req *req;
1911 struct fuse_out_header oh;
1913 if (nbytes < sizeof(struct fuse_out_header))
1916 err = fuse_copy_one(cs, &oh, sizeof(oh));
1921 if (oh.len != nbytes)
1925 * Zero oh.unique indicates unsolicited notification message
1926 * and error contains notification code.
1929 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1930 return err ? err : nbytes;
1934 if (oh.error <= -512 || oh.error > 0)
1937 spin_lock(&fpq->lock);
1939 if (!fpq->connected)
1942 req = request_find(fpq, oh.unique);
1946 /* Is it an interrupt reply? */
1947 if (req->intr_unique == oh.unique) {
1948 __fuse_get_request(req);
1949 spin_unlock(&fpq->lock);
1952 if (nbytes != sizeof(struct fuse_out_header)) {
1953 fuse_put_request(fc, req);
1957 if (oh.error == -ENOSYS)
1958 fc->no_interrupt = 1;
1959 else if (oh.error == -EAGAIN)
1960 queue_interrupt(&fc->iq, req);
1961 fuse_put_request(fc, req);
1963 fuse_copy_finish(cs);
1967 clear_bit(FR_SENT, &req->flags);
1968 list_move(&req->list, &fpq->io);
1970 set_bit(FR_LOCKED, &req->flags);
1971 spin_unlock(&fpq->lock);
1973 if (!req->out.page_replace)
1976 err = copy_out_args(cs, &req->out, nbytes);
1977 fuse_copy_finish(cs);
1979 spin_lock(&fpq->lock);
1980 clear_bit(FR_LOCKED, &req->flags);
1981 if (!fpq->connected)
1984 req->out.h.error = -EIO;
1985 if (!test_bit(FR_PRIVATE, &req->flags))
1986 list_del_init(&req->list);
1987 spin_unlock(&fpq->lock);
1989 request_end(fc, req);
1991 return err ? err : nbytes;
1994 spin_unlock(&fpq->lock);
1996 fuse_copy_finish(cs);
2000 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
2002 struct fuse_copy_state cs;
2003 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
2008 if (!iter_is_iovec(from))
2011 fuse_copy_init(&cs, 0, from);
2013 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
2016 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
2017 struct file *out, loff_t *ppos,
2018 size_t len, unsigned int flags)
2022 struct pipe_buffer *bufs;
2023 struct fuse_copy_state cs;
2024 struct fuse_dev *fud;
2028 fud = fuse_get_dev(out);
2034 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2042 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2043 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2051 struct pipe_buffer *ibuf;
2052 struct pipe_buffer *obuf;
2054 BUG_ON(nbuf >= pipe->buffers);
2055 BUG_ON(!pipe->nrbufs);
2056 ibuf = &pipe->bufs[pipe->curbuf];
2059 if (rem >= ibuf->len) {
2062 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2065 if (!pipe_buf_get(pipe, ibuf))
2069 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2071 ibuf->offset += obuf->len;
2072 ibuf->len -= obuf->len;
2079 fuse_copy_init(&cs, 0, NULL);
2084 if (flags & SPLICE_F_MOVE)
2087 ret = fuse_dev_do_write(fud, &cs, len);
2091 for (idx = 0; idx < nbuf; idx++) {
2092 struct pipe_buffer *buf = &bufs[idx];
2093 buf->ops->release(pipe, buf);
2101 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2103 unsigned mask = POLLOUT | POLLWRNORM;
2104 struct fuse_iqueue *fiq;
2105 struct fuse_dev *fud = fuse_get_dev(file);
2111 poll_wait(file, &fiq->waitq, wait);
2113 spin_lock(&fiq->waitq.lock);
2114 if (!fiq->connected)
2116 else if (request_pending(fiq))
2117 mask |= POLLIN | POLLRDNORM;
2118 spin_unlock(&fiq->waitq.lock);
2124 * Abort all requests on the given list (pending or processing)
2126 * This function releases and reacquires fc->lock
2128 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2130 while (!list_empty(head)) {
2131 struct fuse_req *req;
2132 req = list_entry(head->next, struct fuse_req, list);
2133 req->out.h.error = -ECONNABORTED;
2134 clear_bit(FR_SENT, &req->flags);
2135 list_del_init(&req->list);
2136 request_end(fc, req);
2140 static void end_polls(struct fuse_conn *fc)
2144 p = rb_first(&fc->polled_files);
2147 struct fuse_file *ff;
2148 ff = rb_entry(p, struct fuse_file, polled_node);
2149 wake_up_interruptible_all(&ff->poll_wait);
2156 * Abort all requests.
2158 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2161 * The same effect is usually achievable through killing the filesystem daemon
2162 * and all users of the filesystem. The exception is the combination of an
2163 * asynchronous request and the tricky deadlock (see
2164 * Documentation/filesystems/fuse.txt).
2166 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2167 * requests, they should be finished off immediately. Locked requests will be
2168 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2169 * requests. It is possible that some request will finish before we can. This
2170 * is OK, the request will in that case be removed from the list before we touch
2173 void fuse_abort_conn(struct fuse_conn *fc)
2175 struct fuse_iqueue *fiq = &fc->iq;
2177 spin_lock(&fc->lock);
2178 if (fc->connected) {
2179 struct fuse_dev *fud;
2180 struct fuse_req *req, *next;
2186 fuse_set_initialized(fc);
2187 list_for_each_entry(fud, &fc->devices, entry) {
2188 struct fuse_pqueue *fpq = &fud->pq;
2190 spin_lock(&fpq->lock);
2192 list_for_each_entry_safe(req, next, &fpq->io, list) {
2193 req->out.h.error = -ECONNABORTED;
2194 spin_lock(&req->waitq.lock);
2195 set_bit(FR_ABORTED, &req->flags);
2196 if (!test_bit(FR_LOCKED, &req->flags)) {
2197 set_bit(FR_PRIVATE, &req->flags);
2198 __fuse_get_request(req);
2199 list_move(&req->list, &to_end1);
2201 spin_unlock(&req->waitq.lock);
2203 list_splice_init(&fpq->processing, &to_end2);
2204 spin_unlock(&fpq->lock);
2206 fc->max_background = UINT_MAX;
2209 spin_lock(&fiq->waitq.lock);
2211 list_splice_init(&fiq->pending, &to_end2);
2212 list_for_each_entry(req, &to_end2, list)
2213 clear_bit(FR_PENDING, &req->flags);
2214 while (forget_pending(fiq))
2215 kfree(dequeue_forget(fiq, 1, NULL));
2216 wake_up_all_locked(&fiq->waitq);
2217 spin_unlock(&fiq->waitq.lock);
2218 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2220 wake_up_all(&fc->blocked_waitq);
2221 spin_unlock(&fc->lock);
2223 while (!list_empty(&to_end1)) {
2224 req = list_first_entry(&to_end1, struct fuse_req, list);
2225 list_del_init(&req->list);
2226 request_end(fc, req);
2228 end_requests(fc, &to_end2);
2230 spin_unlock(&fc->lock);
2233 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2235 void fuse_wait_aborted(struct fuse_conn *fc)
2237 /* matches implicit memory barrier in fuse_drop_waiting() */
2239 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2242 int fuse_dev_release(struct inode *inode, struct file *file)
2244 struct fuse_dev *fud = fuse_get_dev(file);
2247 struct fuse_conn *fc = fud->fc;
2248 struct fuse_pqueue *fpq = &fud->pq;
2251 spin_lock(&fpq->lock);
2252 WARN_ON(!list_empty(&fpq->io));
2253 list_splice_init(&fpq->processing, &to_end);
2254 spin_unlock(&fpq->lock);
2256 end_requests(fc, &to_end);
2258 /* Are we the last open device? */
2259 if (atomic_dec_and_test(&fc->dev_count)) {
2260 WARN_ON(fc->iq.fasync != NULL);
2261 fuse_abort_conn(fc);
2267 EXPORT_SYMBOL_GPL(fuse_dev_release);
2269 static int fuse_dev_fasync(int fd, struct file *file, int on)
2271 struct fuse_dev *fud = fuse_get_dev(file);
2276 /* No locking - fasync_helper does its own locking */
2277 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2280 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2282 struct fuse_dev *fud;
2284 if (new->private_data)
2287 fud = fuse_dev_alloc(fc);
2291 new->private_data = fud;
2292 atomic_inc(&fc->dev_count);
2297 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2302 if (cmd == FUSE_DEV_IOC_CLONE) {
2306 if (!get_user(oldfd, (__u32 __user *) arg)) {
2307 struct file *old = fget(oldfd);
2311 struct fuse_dev *fud = NULL;
2314 * Check against file->f_op because CUSE
2315 * uses the same ioctl handler.
2317 if (old->f_op == file->f_op &&
2318 old->f_cred->user_ns == file->f_cred->user_ns)
2319 fud = fuse_get_dev(old);
2322 mutex_lock(&fuse_mutex);
2323 err = fuse_device_clone(fud->fc, file);
2324 mutex_unlock(&fuse_mutex);
2333 const struct file_operations fuse_dev_operations = {
2334 .owner = THIS_MODULE,
2335 .open = fuse_dev_open,
2336 .llseek = no_llseek,
2337 .read_iter = fuse_dev_read,
2338 .splice_read = fuse_dev_splice_read,
2339 .write_iter = fuse_dev_write,
2340 .splice_write = fuse_dev_splice_write,
2341 .poll = fuse_dev_poll,
2342 .release = fuse_dev_release,
2343 .fasync = fuse_dev_fasync,
2344 .unlocked_ioctl = fuse_dev_ioctl,
2345 .compat_ioctl = fuse_dev_ioctl,
2347 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2349 static struct miscdevice fuse_miscdevice = {
2350 .minor = FUSE_MINOR,
2352 .fops = &fuse_dev_operations,
2355 int __init fuse_dev_init(void)
2358 fuse_req_cachep = kmem_cache_create("fuse_request",
2359 sizeof(struct fuse_req),
2361 if (!fuse_req_cachep)
2364 err = misc_register(&fuse_miscdevice);
2366 goto out_cache_clean;
2371 kmem_cache_destroy(fuse_req_cachep);
2376 void fuse_dev_cleanup(void)
2378 misc_deregister(&fuse_miscdevice);
2379 kmem_cache_destroy(fuse_req_cachep);