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 * Release while we have extra ref on stolen page. Otherwise
932 * anon_pipe_buf_release() might think the page can be reused.
934 buf->ops->release(cs->pipe, buf);
938 spin_lock(&cs->req->waitq.lock);
939 if (test_bit(FR_ABORTED, &cs->req->flags))
943 spin_unlock(&cs->req->waitq.lock);
946 unlock_page(newpage);
947 page_cache_release(newpage);
951 unlock_page(oldpage);
952 page_cache_release(oldpage);
958 unlock_page(newpage);
961 cs->offset = buf->offset;
963 err = lock_request(cs->req);
970 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
971 unsigned offset, unsigned count)
973 struct pipe_buffer *buf;
976 if (cs->nr_segs == cs->pipe->buffers)
979 err = unlock_request(cs->req);
983 fuse_copy_finish(cs);
986 page_cache_get(page);
988 buf->offset = offset;
999 * Copy a page in the request to/from the userspace buffer. Must be
1002 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
1003 unsigned offset, unsigned count, int zeroing)
1006 struct page *page = *pagep;
1008 if (page && zeroing && count < PAGE_SIZE)
1009 clear_highpage(page);
1012 if (cs->write && cs->pipebufs && page) {
1013 return fuse_ref_page(cs, page, offset, count);
1014 } else if (!cs->len) {
1015 if (cs->move_pages && page &&
1016 offset == 0 && count == PAGE_SIZE) {
1017 err = fuse_try_move_page(cs, pagep);
1021 err = fuse_copy_fill(cs);
1027 void *mapaddr = kmap_atomic(page);
1028 void *buf = mapaddr + offset;
1029 offset += fuse_copy_do(cs, &buf, &count);
1030 kunmap_atomic(mapaddr);
1032 offset += fuse_copy_do(cs, NULL, &count);
1034 if (page && !cs->write)
1035 flush_dcache_page(page);
1039 /* Copy pages in the request to/from userspace buffer */
1040 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1044 struct fuse_req *req = cs->req;
1046 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1048 unsigned offset = req->page_descs[i].offset;
1049 unsigned count = min(nbytes, req->page_descs[i].length);
1051 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1061 /* Copy a single argument in the request to/from userspace buffer */
1062 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1066 int err = fuse_copy_fill(cs);
1070 fuse_copy_do(cs, &val, &size);
1075 /* Copy request arguments to/from userspace buffer */
1076 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1077 unsigned argpages, struct fuse_arg *args,
1083 for (i = 0; !err && i < numargs; i++) {
1084 struct fuse_arg *arg = &args[i];
1085 if (i == numargs - 1 && argpages)
1086 err = fuse_copy_pages(cs, arg->size, zeroing);
1088 err = fuse_copy_one(cs, arg->value, arg->size);
1093 static int forget_pending(struct fuse_iqueue *fiq)
1095 return fiq->forget_list_head.next != NULL;
1098 static int request_pending(struct fuse_iqueue *fiq)
1100 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1101 forget_pending(fiq);
1105 * Transfer an interrupt request to userspace
1107 * Unlike other requests this is assembled on demand, without a need
1108 * to allocate a separate fuse_req structure.
1110 * Called with fiq->waitq.lock held, releases it
1112 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1113 struct fuse_copy_state *cs,
1114 size_t nbytes, struct fuse_req *req)
1115 __releases(fiq->waitq.lock)
1117 struct fuse_in_header ih;
1118 struct fuse_interrupt_in arg;
1119 unsigned reqsize = sizeof(ih) + sizeof(arg);
1122 list_del_init(&req->intr_entry);
1123 req->intr_unique = fuse_get_unique(fiq);
1124 memset(&ih, 0, sizeof(ih));
1125 memset(&arg, 0, sizeof(arg));
1127 ih.opcode = FUSE_INTERRUPT;
1128 ih.unique = req->intr_unique;
1129 arg.unique = req->in.h.unique;
1131 spin_unlock(&fiq->waitq.lock);
1132 if (nbytes < reqsize)
1135 err = fuse_copy_one(cs, &ih, sizeof(ih));
1137 err = fuse_copy_one(cs, &arg, sizeof(arg));
1138 fuse_copy_finish(cs);
1140 return err ? err : reqsize;
1143 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1147 struct fuse_forget_link *head = fiq->forget_list_head.next;
1148 struct fuse_forget_link **newhead = &head;
1151 for (count = 0; *newhead != NULL && count < max; count++)
1152 newhead = &(*newhead)->next;
1154 fiq->forget_list_head.next = *newhead;
1156 if (fiq->forget_list_head.next == NULL)
1157 fiq->forget_list_tail = &fiq->forget_list_head;
1165 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1166 struct fuse_copy_state *cs,
1168 __releases(fiq->waitq.lock)
1171 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1172 struct fuse_forget_in arg = {
1173 .nlookup = forget->forget_one.nlookup,
1175 struct fuse_in_header ih = {
1176 .opcode = FUSE_FORGET,
1177 .nodeid = forget->forget_one.nodeid,
1178 .unique = fuse_get_unique(fiq),
1179 .len = sizeof(ih) + sizeof(arg),
1182 spin_unlock(&fiq->waitq.lock);
1184 if (nbytes < ih.len)
1187 err = fuse_copy_one(cs, &ih, sizeof(ih));
1189 err = fuse_copy_one(cs, &arg, sizeof(arg));
1190 fuse_copy_finish(cs);
1198 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1199 struct fuse_copy_state *cs, size_t nbytes)
1200 __releases(fiq->waitq.lock)
1203 unsigned max_forgets;
1205 struct fuse_forget_link *head;
1206 struct fuse_batch_forget_in arg = { .count = 0 };
1207 struct fuse_in_header ih = {
1208 .opcode = FUSE_BATCH_FORGET,
1209 .unique = fuse_get_unique(fiq),
1210 .len = sizeof(ih) + sizeof(arg),
1213 if (nbytes < ih.len) {
1214 spin_unlock(&fiq->waitq.lock);
1218 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1219 head = dequeue_forget(fiq, max_forgets, &count);
1220 spin_unlock(&fiq->waitq.lock);
1223 ih.len += count * sizeof(struct fuse_forget_one);
1224 err = fuse_copy_one(cs, &ih, sizeof(ih));
1226 err = fuse_copy_one(cs, &arg, sizeof(arg));
1229 struct fuse_forget_link *forget = head;
1232 err = fuse_copy_one(cs, &forget->forget_one,
1233 sizeof(forget->forget_one));
1235 head = forget->next;
1239 fuse_copy_finish(cs);
1247 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1248 struct fuse_copy_state *cs,
1250 __releases(fiq->waitq.lock)
1252 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1253 return fuse_read_single_forget(fiq, cs, nbytes);
1255 return fuse_read_batch_forget(fiq, cs, nbytes);
1259 * Read a single request into the userspace filesystem's buffer. This
1260 * function waits until a request is available, then removes it from
1261 * the pending list and copies request data to userspace buffer. If
1262 * no reply is needed (FORGET) or request has been aborted or there
1263 * was an error during the copying then it's finished by calling
1264 * request_end(). Otherwise add it to the processing list, and set
1267 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1268 struct fuse_copy_state *cs, size_t nbytes)
1271 struct fuse_conn *fc = fud->fc;
1272 struct fuse_iqueue *fiq = &fc->iq;
1273 struct fuse_pqueue *fpq = &fud->pq;
1274 struct fuse_req *req;
1279 spin_lock(&fiq->waitq.lock);
1281 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1282 !request_pending(fiq))
1285 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1286 !fiq->connected || request_pending(fiq));
1291 if (!fiq->connected)
1294 if (!list_empty(&fiq->interrupts)) {
1295 req = list_entry(fiq->interrupts.next, struct fuse_req,
1297 return fuse_read_interrupt(fiq, cs, nbytes, req);
1300 if (forget_pending(fiq)) {
1301 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1302 return fuse_read_forget(fc, fiq, cs, nbytes);
1304 if (fiq->forget_batch <= -8)
1305 fiq->forget_batch = 16;
1308 req = list_entry(fiq->pending.next, struct fuse_req, list);
1309 clear_bit(FR_PENDING, &req->flags);
1310 list_del_init(&req->list);
1311 spin_unlock(&fiq->waitq.lock);
1314 reqsize = in->h.len;
1315 /* If request is too large, reply with an error and restart the read */
1316 if (nbytes < reqsize) {
1317 req->out.h.error = -EIO;
1318 /* SETXATTR is special, since it may contain too large data */
1319 if (in->h.opcode == FUSE_SETXATTR)
1320 req->out.h.error = -E2BIG;
1321 request_end(fc, req);
1324 spin_lock(&fpq->lock);
1326 * Must not put request on fpq->io queue after having been shut down by
1329 if (!fpq->connected) {
1330 req->out.h.error = err = -ECONNABORTED;
1334 list_add(&req->list, &fpq->io);
1335 spin_unlock(&fpq->lock);
1337 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1339 err = fuse_copy_args(cs, in->numargs, in->argpages,
1340 (struct fuse_arg *) in->args, 0);
1341 fuse_copy_finish(cs);
1342 spin_lock(&fpq->lock);
1343 clear_bit(FR_LOCKED, &req->flags);
1344 if (!fpq->connected) {
1349 req->out.h.error = -EIO;
1352 if (!test_bit(FR_ISREPLY, &req->flags)) {
1356 list_move_tail(&req->list, &fpq->processing);
1357 __fuse_get_request(req);
1358 set_bit(FR_SENT, &req->flags);
1359 spin_unlock(&fpq->lock);
1360 /* matches barrier in request_wait_answer() */
1361 smp_mb__after_atomic();
1362 if (test_bit(FR_INTERRUPTED, &req->flags))
1363 queue_interrupt(fiq, req);
1364 fuse_put_request(fc, req);
1369 if (!test_bit(FR_PRIVATE, &req->flags))
1370 list_del_init(&req->list);
1371 spin_unlock(&fpq->lock);
1372 request_end(fc, req);
1376 spin_unlock(&fiq->waitq.lock);
1380 static int fuse_dev_open(struct inode *inode, struct file *file)
1383 * The fuse device's file's private_data is used to hold
1384 * the fuse_conn(ection) when it is mounted, and is used to
1385 * keep track of whether the file has been mounted already.
1387 file->private_data = NULL;
1391 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1393 struct fuse_copy_state cs;
1394 struct file *file = iocb->ki_filp;
1395 struct fuse_dev *fud = fuse_get_dev(file);
1400 if (!iter_is_iovec(to))
1403 fuse_copy_init(&cs, 1, to);
1405 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1408 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1409 struct pipe_inode_info *pipe,
1410 size_t len, unsigned int flags)
1415 struct pipe_buffer *bufs;
1416 struct fuse_copy_state cs;
1417 struct fuse_dev *fud = fuse_get_dev(in);
1422 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1426 fuse_copy_init(&cs, 1, NULL);
1429 ret = fuse_dev_do_read(fud, in, &cs, len);
1436 if (!pipe->readers) {
1437 send_sig(SIGPIPE, current, 0);
1443 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1448 while (page_nr < cs.nr_segs) {
1449 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1450 struct pipe_buffer *buf = pipe->bufs + newbuf;
1452 buf->page = bufs[page_nr].page;
1453 buf->offset = bufs[page_nr].offset;
1454 buf->len = bufs[page_nr].len;
1456 * Need to be careful about this. Having buf->ops in module
1457 * code can Oops if the buffer persists after module unload.
1459 buf->ops = &nosteal_pipe_buf_ops;
1474 if (waitqueue_active(&pipe->wait))
1475 wake_up_interruptible(&pipe->wait);
1476 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1480 for (; page_nr < cs.nr_segs; page_nr++)
1481 page_cache_release(bufs[page_nr].page);
1487 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1488 struct fuse_copy_state *cs)
1490 struct fuse_notify_poll_wakeup_out outarg;
1493 if (size != sizeof(outarg))
1496 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1500 fuse_copy_finish(cs);
1501 return fuse_notify_poll_wakeup(fc, &outarg);
1504 fuse_copy_finish(cs);
1508 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1509 struct fuse_copy_state *cs)
1511 struct fuse_notify_inval_inode_out outarg;
1514 if (size != sizeof(outarg))
1517 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1520 fuse_copy_finish(cs);
1522 down_read(&fc->killsb);
1525 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1526 outarg.off, outarg.len);
1528 up_read(&fc->killsb);
1532 fuse_copy_finish(cs);
1536 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1537 struct fuse_copy_state *cs)
1539 struct fuse_notify_inval_entry_out outarg;
1544 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1549 if (size < sizeof(outarg))
1552 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1556 err = -ENAMETOOLONG;
1557 if (outarg.namelen > FUSE_NAME_MAX)
1561 if (size != sizeof(outarg) + outarg.namelen + 1)
1565 name.len = outarg.namelen;
1566 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1569 fuse_copy_finish(cs);
1570 buf[outarg.namelen] = 0;
1571 name.hash = full_name_hash(name.name, name.len);
1573 down_read(&fc->killsb);
1576 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1577 up_read(&fc->killsb);
1583 fuse_copy_finish(cs);
1587 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1588 struct fuse_copy_state *cs)
1590 struct fuse_notify_delete_out outarg;
1595 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1600 if (size < sizeof(outarg))
1603 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1607 err = -ENAMETOOLONG;
1608 if (outarg.namelen > FUSE_NAME_MAX)
1612 if (size != sizeof(outarg) + outarg.namelen + 1)
1616 name.len = outarg.namelen;
1617 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1620 fuse_copy_finish(cs);
1621 buf[outarg.namelen] = 0;
1622 name.hash = full_name_hash(name.name, name.len);
1624 down_read(&fc->killsb);
1627 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1628 outarg.child, &name);
1629 up_read(&fc->killsb);
1635 fuse_copy_finish(cs);
1639 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1640 struct fuse_copy_state *cs)
1642 struct fuse_notify_store_out outarg;
1643 struct inode *inode;
1644 struct address_space *mapping;
1648 unsigned int offset;
1654 if (size < sizeof(outarg))
1657 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1662 if (size - sizeof(outarg) != outarg.size)
1665 nodeid = outarg.nodeid;
1667 down_read(&fc->killsb);
1673 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1677 mapping = inode->i_mapping;
1678 index = outarg.offset >> PAGE_CACHE_SHIFT;
1679 offset = outarg.offset & ~PAGE_CACHE_MASK;
1680 file_size = i_size_read(inode);
1681 end = outarg.offset + outarg.size;
1682 if (end > file_size) {
1684 fuse_write_update_size(inode, file_size);
1690 unsigned int this_num;
1693 page = find_or_create_page(mapping, index,
1694 mapping_gfp_mask(mapping));
1698 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1699 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1700 if (!err && offset == 0 &&
1701 (this_num == PAGE_CACHE_SIZE || file_size == end))
1702 SetPageUptodate(page);
1704 page_cache_release(page);
1719 up_read(&fc->killsb);
1721 fuse_copy_finish(cs);
1725 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1727 release_pages(req->pages, req->num_pages, false);
1730 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1731 struct fuse_notify_retrieve_out *outarg)
1734 struct address_space *mapping = inode->i_mapping;
1735 struct fuse_req *req;
1739 unsigned int offset;
1740 size_t total_len = 0;
1743 offset = outarg->offset & ~PAGE_CACHE_MASK;
1744 file_size = i_size_read(inode);
1746 num = min(outarg->size, fc->max_write);
1747 if (outarg->offset > file_size)
1749 else if (outarg->offset + num > file_size)
1750 num = file_size - outarg->offset;
1752 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1753 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1755 req = fuse_get_req(fc, num_pages);
1757 return PTR_ERR(req);
1759 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1760 req->in.h.nodeid = outarg->nodeid;
1761 req->in.numargs = 2;
1762 req->in.argpages = 1;
1763 req->end = fuse_retrieve_end;
1765 index = outarg->offset >> PAGE_CACHE_SHIFT;
1767 while (num && req->num_pages < num_pages) {
1769 unsigned int this_num;
1771 page = find_get_page(mapping, index);
1775 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1776 req->pages[req->num_pages] = page;
1777 req->page_descs[req->num_pages].offset = offset;
1778 req->page_descs[req->num_pages].length = this_num;
1783 total_len += this_num;
1786 req->misc.retrieve_in.offset = outarg->offset;
1787 req->misc.retrieve_in.size = total_len;
1788 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1789 req->in.args[0].value = &req->misc.retrieve_in;
1790 req->in.args[1].size = total_len;
1792 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1794 fuse_retrieve_end(fc, req);
1795 fuse_put_request(fc, req);
1801 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1802 struct fuse_copy_state *cs)
1804 struct fuse_notify_retrieve_out outarg;
1805 struct inode *inode;
1809 if (size != sizeof(outarg))
1812 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1816 fuse_copy_finish(cs);
1818 down_read(&fc->killsb);
1821 u64 nodeid = outarg.nodeid;
1823 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1825 err = fuse_retrieve(fc, inode, &outarg);
1829 up_read(&fc->killsb);
1834 fuse_copy_finish(cs);
1838 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1839 unsigned int size, struct fuse_copy_state *cs)
1841 /* Don't try to move pages (yet) */
1845 case FUSE_NOTIFY_POLL:
1846 return fuse_notify_poll(fc, size, cs);
1848 case FUSE_NOTIFY_INVAL_INODE:
1849 return fuse_notify_inval_inode(fc, size, cs);
1851 case FUSE_NOTIFY_INVAL_ENTRY:
1852 return fuse_notify_inval_entry(fc, size, cs);
1854 case FUSE_NOTIFY_STORE:
1855 return fuse_notify_store(fc, size, cs);
1857 case FUSE_NOTIFY_RETRIEVE:
1858 return fuse_notify_retrieve(fc, size, cs);
1860 case FUSE_NOTIFY_DELETE:
1861 return fuse_notify_delete(fc, size, cs);
1864 fuse_copy_finish(cs);
1869 /* Look up request on processing list by unique ID */
1870 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1872 struct fuse_req *req;
1874 list_for_each_entry(req, &fpq->processing, list) {
1875 if (req->in.h.unique == unique || req->intr_unique == unique)
1881 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1884 unsigned reqsize = sizeof(struct fuse_out_header);
1887 return nbytes != reqsize ? -EINVAL : 0;
1889 reqsize += len_args(out->numargs, out->args);
1891 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1893 else if (reqsize > nbytes) {
1894 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1895 unsigned diffsize = reqsize - nbytes;
1896 if (diffsize > lastarg->size)
1898 lastarg->size -= diffsize;
1900 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1905 * Write a single reply to a request. First the header is copied from
1906 * the write buffer. The request is then searched on the processing
1907 * list by the unique ID found in the header. If found, then remove
1908 * it from the list and copy the rest of the buffer to the request.
1909 * The request is finished by calling request_end()
1911 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1912 struct fuse_copy_state *cs, size_t nbytes)
1915 struct fuse_conn *fc = fud->fc;
1916 struct fuse_pqueue *fpq = &fud->pq;
1917 struct fuse_req *req;
1918 struct fuse_out_header oh;
1920 if (nbytes < sizeof(struct fuse_out_header))
1923 err = fuse_copy_one(cs, &oh, sizeof(oh));
1928 if (oh.len != nbytes)
1932 * Zero oh.unique indicates unsolicited notification message
1933 * and error contains notification code.
1936 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1937 return err ? err : nbytes;
1941 if (oh.error <= -512 || oh.error > 0)
1944 spin_lock(&fpq->lock);
1946 if (!fpq->connected)
1949 req = request_find(fpq, oh.unique);
1953 /* Is it an interrupt reply? */
1954 if (req->intr_unique == oh.unique) {
1955 __fuse_get_request(req);
1956 spin_unlock(&fpq->lock);
1959 if (nbytes != sizeof(struct fuse_out_header)) {
1960 fuse_put_request(fc, req);
1964 if (oh.error == -ENOSYS)
1965 fc->no_interrupt = 1;
1966 else if (oh.error == -EAGAIN)
1967 queue_interrupt(&fc->iq, req);
1968 fuse_put_request(fc, req);
1970 fuse_copy_finish(cs);
1974 clear_bit(FR_SENT, &req->flags);
1975 list_move(&req->list, &fpq->io);
1977 set_bit(FR_LOCKED, &req->flags);
1978 spin_unlock(&fpq->lock);
1980 if (!req->out.page_replace)
1983 err = copy_out_args(cs, &req->out, nbytes);
1984 fuse_copy_finish(cs);
1986 spin_lock(&fpq->lock);
1987 clear_bit(FR_LOCKED, &req->flags);
1988 if (!fpq->connected)
1991 req->out.h.error = -EIO;
1992 if (!test_bit(FR_PRIVATE, &req->flags))
1993 list_del_init(&req->list);
1994 spin_unlock(&fpq->lock);
1996 request_end(fc, req);
1998 return err ? err : nbytes;
2001 spin_unlock(&fpq->lock);
2003 fuse_copy_finish(cs);
2007 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
2009 struct fuse_copy_state cs;
2010 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
2015 if (!iter_is_iovec(from))
2018 fuse_copy_init(&cs, 0, from);
2020 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
2023 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
2024 struct file *out, loff_t *ppos,
2025 size_t len, unsigned int flags)
2029 struct pipe_buffer *bufs;
2030 struct fuse_copy_state cs;
2031 struct fuse_dev *fud;
2035 fud = fuse_get_dev(out);
2041 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2049 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2050 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2058 struct pipe_buffer *ibuf;
2059 struct pipe_buffer *obuf;
2061 BUG_ON(nbuf >= pipe->buffers);
2062 BUG_ON(!pipe->nrbufs);
2063 ibuf = &pipe->bufs[pipe->curbuf];
2066 if (rem >= ibuf->len) {
2069 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2072 if (!pipe_buf_get(pipe, ibuf))
2076 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2078 ibuf->offset += obuf->len;
2079 ibuf->len -= obuf->len;
2086 fuse_copy_init(&cs, 0, NULL);
2091 if (flags & SPLICE_F_MOVE)
2094 ret = fuse_dev_do_write(fud, &cs, len);
2098 for (idx = 0; idx < nbuf; idx++) {
2099 struct pipe_buffer *buf = &bufs[idx];
2101 buf->ops->release(pipe, buf);
2109 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2111 unsigned mask = POLLOUT | POLLWRNORM;
2112 struct fuse_iqueue *fiq;
2113 struct fuse_dev *fud = fuse_get_dev(file);
2119 poll_wait(file, &fiq->waitq, wait);
2121 spin_lock(&fiq->waitq.lock);
2122 if (!fiq->connected)
2124 else if (request_pending(fiq))
2125 mask |= POLLIN | POLLRDNORM;
2126 spin_unlock(&fiq->waitq.lock);
2132 * Abort all requests on the given list (pending or processing)
2134 * This function releases and reacquires fc->lock
2136 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2138 while (!list_empty(head)) {
2139 struct fuse_req *req;
2140 req = list_entry(head->next, struct fuse_req, list);
2141 req->out.h.error = -ECONNABORTED;
2142 clear_bit(FR_SENT, &req->flags);
2143 list_del_init(&req->list);
2144 request_end(fc, req);
2148 static void end_polls(struct fuse_conn *fc)
2152 p = rb_first(&fc->polled_files);
2155 struct fuse_file *ff;
2156 ff = rb_entry(p, struct fuse_file, polled_node);
2157 wake_up_interruptible_all(&ff->poll_wait);
2164 * Abort all requests.
2166 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2169 * The same effect is usually achievable through killing the filesystem daemon
2170 * and all users of the filesystem. The exception is the combination of an
2171 * asynchronous request and the tricky deadlock (see
2172 * Documentation/filesystems/fuse.txt).
2174 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2175 * requests, they should be finished off immediately. Locked requests will be
2176 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2177 * requests. It is possible that some request will finish before we can. This
2178 * is OK, the request will in that case be removed from the list before we touch
2181 void fuse_abort_conn(struct fuse_conn *fc)
2183 struct fuse_iqueue *fiq = &fc->iq;
2185 spin_lock(&fc->lock);
2186 if (fc->connected) {
2187 struct fuse_dev *fud;
2188 struct fuse_req *req, *next;
2194 fuse_set_initialized(fc);
2195 list_for_each_entry(fud, &fc->devices, entry) {
2196 struct fuse_pqueue *fpq = &fud->pq;
2198 spin_lock(&fpq->lock);
2200 list_for_each_entry_safe(req, next, &fpq->io, list) {
2201 req->out.h.error = -ECONNABORTED;
2202 spin_lock(&req->waitq.lock);
2203 set_bit(FR_ABORTED, &req->flags);
2204 if (!test_bit(FR_LOCKED, &req->flags)) {
2205 set_bit(FR_PRIVATE, &req->flags);
2206 __fuse_get_request(req);
2207 list_move(&req->list, &to_end1);
2209 spin_unlock(&req->waitq.lock);
2211 list_splice_init(&fpq->processing, &to_end2);
2212 spin_unlock(&fpq->lock);
2214 fc->max_background = UINT_MAX;
2217 spin_lock(&fiq->waitq.lock);
2219 list_splice_init(&fiq->pending, &to_end2);
2220 list_for_each_entry(req, &to_end2, list)
2221 clear_bit(FR_PENDING, &req->flags);
2222 while (forget_pending(fiq))
2223 kfree(dequeue_forget(fiq, 1, NULL));
2224 wake_up_all_locked(&fiq->waitq);
2225 spin_unlock(&fiq->waitq.lock);
2226 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2228 wake_up_all(&fc->blocked_waitq);
2229 spin_unlock(&fc->lock);
2231 while (!list_empty(&to_end1)) {
2232 req = list_first_entry(&to_end1, struct fuse_req, list);
2233 list_del_init(&req->list);
2234 request_end(fc, req);
2236 end_requests(fc, &to_end2);
2238 spin_unlock(&fc->lock);
2241 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2243 void fuse_wait_aborted(struct fuse_conn *fc)
2245 /* matches implicit memory barrier in fuse_drop_waiting() */
2247 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2250 int fuse_dev_release(struct inode *inode, struct file *file)
2252 struct fuse_dev *fud = fuse_get_dev(file);
2255 struct fuse_conn *fc = fud->fc;
2256 struct fuse_pqueue *fpq = &fud->pq;
2259 spin_lock(&fpq->lock);
2260 WARN_ON(!list_empty(&fpq->io));
2261 list_splice_init(&fpq->processing, &to_end);
2262 spin_unlock(&fpq->lock);
2264 end_requests(fc, &to_end);
2266 /* Are we the last open device? */
2267 if (atomic_dec_and_test(&fc->dev_count)) {
2268 WARN_ON(fc->iq.fasync != NULL);
2269 fuse_abort_conn(fc);
2275 EXPORT_SYMBOL_GPL(fuse_dev_release);
2277 static int fuse_dev_fasync(int fd, struct file *file, int on)
2279 struct fuse_dev *fud = fuse_get_dev(file);
2284 /* No locking - fasync_helper does its own locking */
2285 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2288 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2290 struct fuse_dev *fud;
2292 if (new->private_data)
2295 fud = fuse_dev_alloc(fc);
2299 new->private_data = fud;
2300 atomic_inc(&fc->dev_count);
2305 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2310 if (cmd == FUSE_DEV_IOC_CLONE) {
2314 if (!get_user(oldfd, (__u32 __user *) arg)) {
2315 struct file *old = fget(oldfd);
2319 struct fuse_dev *fud = NULL;
2322 * Check against file->f_op because CUSE
2323 * uses the same ioctl handler.
2325 if (old->f_op == file->f_op &&
2326 old->f_cred->user_ns == file->f_cred->user_ns)
2327 fud = fuse_get_dev(old);
2330 mutex_lock(&fuse_mutex);
2331 err = fuse_device_clone(fud->fc, file);
2332 mutex_unlock(&fuse_mutex);
2341 const struct file_operations fuse_dev_operations = {
2342 .owner = THIS_MODULE,
2343 .open = fuse_dev_open,
2344 .llseek = no_llseek,
2345 .read_iter = fuse_dev_read,
2346 .splice_read = fuse_dev_splice_read,
2347 .write_iter = fuse_dev_write,
2348 .splice_write = fuse_dev_splice_write,
2349 .poll = fuse_dev_poll,
2350 .release = fuse_dev_release,
2351 .fasync = fuse_dev_fasync,
2352 .unlocked_ioctl = fuse_dev_ioctl,
2353 .compat_ioctl = fuse_dev_ioctl,
2355 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2357 static struct miscdevice fuse_miscdevice = {
2358 .minor = FUSE_MINOR,
2360 .fops = &fuse_dev_operations,
2363 int __init fuse_dev_init(void)
2366 fuse_req_cachep = kmem_cache_create("fuse_request",
2367 sizeof(struct fuse_req),
2369 if (!fuse_req_cachep)
2372 err = misc_register(&fuse_miscdevice);
2374 goto out_cache_clean;
2379 kmem_cache_destroy(fuse_req_cachep);
2384 void fuse_dev_cleanup(void)
2386 misc_deregister(&fuse_miscdevice);
2387 kmem_cache_destroy(fuse_req_cachep);