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 atomic_dec(&fc->num_waiting);
150 } else if (atomic_dec_and_test(&fc->num_waiting)) {
151 /* wake up aborters */
152 wake_up_all(&fc->blocked_waitq);
156 static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
159 struct fuse_req *req;
161 atomic_inc(&fc->num_waiting);
163 if (fuse_block_alloc(fc, for_background)) {
168 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
169 !fuse_block_alloc(fc, for_background));
170 restore_sigs(&oldset);
175 /* Matches smp_wmb() in fuse_set_initialized() */
186 req = fuse_request_alloc(npages);
190 wake_up(&fc->blocked_waitq);
194 fuse_req_init_context(req);
195 __set_bit(FR_WAITING, &req->flags);
197 __set_bit(FR_BACKGROUND, &req->flags);
202 fuse_drop_waiting(fc);
206 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
208 return __fuse_get_req(fc, npages, false);
210 EXPORT_SYMBOL_GPL(fuse_get_req);
212 struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
215 return __fuse_get_req(fc, npages, true);
217 EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
220 * Return request in fuse_file->reserved_req. However that may
221 * currently be in use. If that is the case, wait for it to become
224 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
227 struct fuse_req *req = NULL;
228 struct fuse_file *ff = file->private_data;
231 wait_event(fc->reserved_req_waitq, ff->reserved_req);
232 spin_lock(&fc->lock);
233 if (ff->reserved_req) {
234 req = ff->reserved_req;
235 ff->reserved_req = NULL;
236 req->stolen_file = get_file(file);
238 spin_unlock(&fc->lock);
245 * Put stolen request back into fuse_file->reserved_req
247 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
249 struct file *file = req->stolen_file;
250 struct fuse_file *ff = file->private_data;
252 spin_lock(&fc->lock);
253 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
254 BUG_ON(ff->reserved_req);
255 ff->reserved_req = req;
256 wake_up_all(&fc->reserved_req_waitq);
257 spin_unlock(&fc->lock);
262 * Gets a requests for a file operation, always succeeds
264 * This is used for sending the FLUSH request, which must get to
265 * userspace, due to POSIX locks which may need to be unlocked.
267 * If allocation fails due to OOM, use the reserved request in
270 * This is very unlikely to deadlock accidentally, since the
271 * filesystem should not have it's own file open. If deadlock is
272 * intentional, it can still be broken by "aborting" the filesystem.
274 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
277 struct fuse_req *req;
279 atomic_inc(&fc->num_waiting);
280 wait_event(fc->blocked_waitq, fc->initialized);
281 /* Matches smp_wmb() in fuse_set_initialized() */
283 req = fuse_request_alloc(0);
285 req = get_reserved_req(fc, file);
287 fuse_req_init_context(req);
288 __set_bit(FR_WAITING, &req->flags);
289 __clear_bit(FR_BACKGROUND, &req->flags);
293 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
295 if (atomic_dec_and_test(&req->count)) {
296 if (test_bit(FR_BACKGROUND, &req->flags)) {
298 * We get here in the unlikely case that a background
299 * request was allocated but not sent
301 spin_lock(&fc->lock);
303 wake_up(&fc->blocked_waitq);
304 spin_unlock(&fc->lock);
307 if (test_bit(FR_WAITING, &req->flags)) {
308 __clear_bit(FR_WAITING, &req->flags);
309 fuse_drop_waiting(fc);
312 if (req->stolen_file)
313 put_reserved_req(fc, req);
315 fuse_request_free(req);
318 EXPORT_SYMBOL_GPL(fuse_put_request);
320 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
325 for (i = 0; i < numargs; i++)
326 nbytes += args[i].size;
331 static u64 fuse_get_unique(struct fuse_iqueue *fiq)
333 return ++fiq->reqctr;
336 static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
338 req->in.h.len = sizeof(struct fuse_in_header) +
339 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
340 list_add_tail(&req->list, &fiq->pending);
341 wake_up_locked(&fiq->waitq);
342 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
345 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
346 u64 nodeid, u64 nlookup)
348 struct fuse_iqueue *fiq = &fc->iq;
350 forget->forget_one.nodeid = nodeid;
351 forget->forget_one.nlookup = nlookup;
353 spin_lock(&fiq->waitq.lock);
354 if (fiq->connected) {
355 fiq->forget_list_tail->next = forget;
356 fiq->forget_list_tail = forget;
357 wake_up_locked(&fiq->waitq);
358 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
362 spin_unlock(&fiq->waitq.lock);
365 static void flush_bg_queue(struct fuse_conn *fc)
367 while (fc->active_background < fc->max_background &&
368 !list_empty(&fc->bg_queue)) {
369 struct fuse_req *req;
370 struct fuse_iqueue *fiq = &fc->iq;
372 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
373 list_del(&req->list);
374 fc->active_background++;
375 spin_lock(&fiq->waitq.lock);
376 req->in.h.unique = fuse_get_unique(fiq);
377 queue_request(fiq, req);
378 spin_unlock(&fiq->waitq.lock);
383 * This function is called when a request is finished. Either a reply
384 * has arrived or it was aborted (and not yet sent) or some error
385 * occurred during communication with userspace, or the device file
386 * was closed. The requester thread is woken up (if still waiting),
387 * the 'end' callback is called if given, else the reference to the
388 * request is released
390 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
392 struct fuse_iqueue *fiq = &fc->iq;
394 if (test_and_set_bit(FR_FINISHED, &req->flags))
397 spin_lock(&fiq->waitq.lock);
398 list_del_init(&req->intr_entry);
399 spin_unlock(&fiq->waitq.lock);
400 WARN_ON(test_bit(FR_PENDING, &req->flags));
401 WARN_ON(test_bit(FR_SENT, &req->flags));
402 if (test_bit(FR_BACKGROUND, &req->flags)) {
403 spin_lock(&fc->lock);
404 clear_bit(FR_BACKGROUND, &req->flags);
405 if (fc->num_background == fc->max_background) {
407 wake_up(&fc->blocked_waitq);
408 } else if (!fc->blocked) {
410 * Wake up next waiter, if any. It's okay to use
411 * waitqueue_active(), as we've already synced up
412 * fc->blocked with waiters with the wake_up() call
415 if (waitqueue_active(&fc->blocked_waitq))
416 wake_up(&fc->blocked_waitq);
419 if (fc->num_background == fc->congestion_threshold &&
420 fc->connected && fc->bdi_initialized) {
421 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
422 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
424 fc->num_background--;
425 fc->active_background--;
427 spin_unlock(&fc->lock);
429 wake_up(&req->waitq);
433 fuse_put_request(fc, req);
436 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
438 spin_lock(&fiq->waitq.lock);
439 if (test_bit(FR_FINISHED, &req->flags)) {
440 spin_unlock(&fiq->waitq.lock);
443 if (list_empty(&req->intr_entry)) {
444 list_add_tail(&req->intr_entry, &fiq->interrupts);
445 wake_up_locked(&fiq->waitq);
447 spin_unlock(&fiq->waitq.lock);
448 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
451 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
453 struct fuse_iqueue *fiq = &fc->iq;
456 if (!fc->no_interrupt) {
457 /* Any signal may interrupt this */
458 err = wait_event_interruptible(req->waitq,
459 test_bit(FR_FINISHED, &req->flags));
463 set_bit(FR_INTERRUPTED, &req->flags);
464 /* matches barrier in fuse_dev_do_read() */
465 smp_mb__after_atomic();
466 if (test_bit(FR_SENT, &req->flags))
467 queue_interrupt(fiq, req);
470 if (!test_bit(FR_FORCE, &req->flags)) {
473 /* Only fatal signals may interrupt this */
475 err = wait_event_interruptible(req->waitq,
476 test_bit(FR_FINISHED, &req->flags));
477 restore_sigs(&oldset);
482 spin_lock(&fiq->waitq.lock);
483 /* Request is not yet in userspace, bail out */
484 if (test_bit(FR_PENDING, &req->flags)) {
485 list_del(&req->list);
486 spin_unlock(&fiq->waitq.lock);
487 __fuse_put_request(req);
488 req->out.h.error = -EINTR;
491 spin_unlock(&fiq->waitq.lock);
495 * Either request is already in userspace, or it was forced.
498 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
501 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
503 struct fuse_iqueue *fiq = &fc->iq;
505 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
506 spin_lock(&fiq->waitq.lock);
507 if (!fiq->connected) {
508 spin_unlock(&fiq->waitq.lock);
509 req->out.h.error = -ENOTCONN;
511 req->in.h.unique = fuse_get_unique(fiq);
512 queue_request(fiq, req);
513 /* acquire extra reference, since request is still needed
514 after request_end() */
515 __fuse_get_request(req);
516 spin_unlock(&fiq->waitq.lock);
518 request_wait_answer(fc, req);
519 /* Pairs with smp_wmb() in request_end() */
524 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
526 __set_bit(FR_ISREPLY, &req->flags);
527 if (!test_bit(FR_WAITING, &req->flags)) {
528 __set_bit(FR_WAITING, &req->flags);
529 atomic_inc(&fc->num_waiting);
531 __fuse_request_send(fc, req);
533 EXPORT_SYMBOL_GPL(fuse_request_send);
535 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
537 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
538 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
541 switch (args->in.h.opcode) {
548 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
552 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
556 if (fc->minor < 12) {
557 switch (args->in.h.opcode) {
559 args->in.args[0].size = sizeof(struct fuse_open_in);
562 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
568 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
570 struct fuse_req *req;
573 req = fuse_get_req(fc, 0);
577 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
578 fuse_adjust_compat(fc, args);
580 req->in.h.opcode = args->in.h.opcode;
581 req->in.h.nodeid = args->in.h.nodeid;
582 req->in.numargs = args->in.numargs;
583 memcpy(req->in.args, args->in.args,
584 args->in.numargs * sizeof(struct fuse_in_arg));
585 req->out.argvar = args->out.argvar;
586 req->out.numargs = args->out.numargs;
587 memcpy(req->out.args, args->out.args,
588 args->out.numargs * sizeof(struct fuse_arg));
589 fuse_request_send(fc, req);
590 ret = req->out.h.error;
591 if (!ret && args->out.argvar) {
592 BUG_ON(args->out.numargs != 1);
593 ret = req->out.args[0].size;
595 fuse_put_request(fc, req);
601 * Called under fc->lock
603 * fc->connected must have been checked previously
605 void fuse_request_send_background_locked(struct fuse_conn *fc,
606 struct fuse_req *req)
608 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
609 if (!test_bit(FR_WAITING, &req->flags)) {
610 __set_bit(FR_WAITING, &req->flags);
611 atomic_inc(&fc->num_waiting);
613 __set_bit(FR_ISREPLY, &req->flags);
614 fc->num_background++;
615 if (fc->num_background == fc->max_background)
617 if (fc->num_background == fc->congestion_threshold &&
618 fc->bdi_initialized) {
619 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
620 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
622 list_add_tail(&req->list, &fc->bg_queue);
626 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
629 spin_lock(&fc->lock);
631 fuse_request_send_background_locked(fc, req);
632 spin_unlock(&fc->lock);
634 spin_unlock(&fc->lock);
635 req->out.h.error = -ENOTCONN;
637 fuse_put_request(fc, req);
640 EXPORT_SYMBOL_GPL(fuse_request_send_background);
642 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
643 struct fuse_req *req, u64 unique)
646 struct fuse_iqueue *fiq = &fc->iq;
648 __clear_bit(FR_ISREPLY, &req->flags);
649 req->in.h.unique = unique;
650 spin_lock(&fiq->waitq.lock);
651 if (fiq->connected) {
652 queue_request(fiq, req);
655 spin_unlock(&fiq->waitq.lock);
660 void fuse_force_forget(struct file *file, u64 nodeid)
662 struct inode *inode = file_inode(file);
663 struct fuse_conn *fc = get_fuse_conn(inode);
664 struct fuse_req *req;
665 struct fuse_forget_in inarg;
667 memset(&inarg, 0, sizeof(inarg));
669 req = fuse_get_req_nofail_nopages(fc, file);
670 req->in.h.opcode = FUSE_FORGET;
671 req->in.h.nodeid = nodeid;
673 req->in.args[0].size = sizeof(inarg);
674 req->in.args[0].value = &inarg;
675 __clear_bit(FR_ISREPLY, &req->flags);
676 __fuse_request_send(fc, req);
678 fuse_put_request(fc, req);
682 * Lock the request. Up to the next unlock_request() there mustn't be
683 * anything that could cause a page-fault. If the request was already
686 static int lock_request(struct fuse_req *req)
690 spin_lock(&req->waitq.lock);
691 if (test_bit(FR_ABORTED, &req->flags))
694 set_bit(FR_LOCKED, &req->flags);
695 spin_unlock(&req->waitq.lock);
701 * Unlock request. If it was aborted while locked, caller is responsible
702 * for unlocking and ending the request.
704 static int unlock_request(struct fuse_req *req)
708 spin_lock(&req->waitq.lock);
709 if (test_bit(FR_ABORTED, &req->flags))
712 clear_bit(FR_LOCKED, &req->flags);
713 spin_unlock(&req->waitq.lock);
718 struct fuse_copy_state {
720 struct fuse_req *req;
721 struct iov_iter *iter;
722 struct pipe_buffer *pipebufs;
723 struct pipe_buffer *currbuf;
724 struct pipe_inode_info *pipe;
725 unsigned long nr_segs;
729 unsigned move_pages:1;
732 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
733 struct iov_iter *iter)
735 memset(cs, 0, sizeof(*cs));
740 /* Unmap and put previous page of userspace buffer */
741 static void fuse_copy_finish(struct fuse_copy_state *cs)
744 struct pipe_buffer *buf = cs->currbuf;
747 buf->len = PAGE_SIZE - cs->len;
751 flush_dcache_page(cs->pg);
752 set_page_dirty_lock(cs->pg);
760 * Get another pagefull of userspace buffer, and map it to kernel
761 * address space, and lock request
763 static int fuse_copy_fill(struct fuse_copy_state *cs)
768 err = unlock_request(cs->req);
772 fuse_copy_finish(cs);
774 struct pipe_buffer *buf = cs->pipebufs;
777 err = buf->ops->confirm(cs->pipe, buf);
781 BUG_ON(!cs->nr_segs);
784 cs->offset = buf->offset;
789 if (cs->nr_segs == cs->pipe->buffers)
792 page = alloc_page(GFP_HIGHUSER);
809 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
817 iov_iter_advance(cs->iter, err);
820 return lock_request(cs->req);
823 /* Do as much copy to/from userspace buffer as we can */
824 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
826 unsigned ncpy = min(*size, cs->len);
828 void *pgaddr = kmap_atomic(cs->pg);
829 void *buf = pgaddr + cs->offset;
832 memcpy(buf, *val, ncpy);
834 memcpy(*val, buf, ncpy);
836 kunmap_atomic(pgaddr);
845 static int fuse_check_page(struct page *page)
847 if (page_mapcount(page) ||
848 page->mapping != NULL ||
849 page_count(page) != 1 ||
850 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
857 printk(KERN_WARNING "fuse: trying to steal weird page\n");
858 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);
864 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
867 struct page *oldpage = *pagep;
868 struct page *newpage;
869 struct pipe_buffer *buf = cs->pipebufs;
871 err = unlock_request(cs->req);
875 fuse_copy_finish(cs);
877 err = buf->ops->confirm(cs->pipe, buf);
881 BUG_ON(!cs->nr_segs);
887 if (cs->len != PAGE_SIZE)
890 if (buf->ops->steal(cs->pipe, buf) != 0)
895 if (!PageUptodate(newpage))
896 SetPageUptodate(newpage);
898 ClearPageMappedToDisk(newpage);
900 if (fuse_check_page(newpage) != 0)
901 goto out_fallback_unlock;
904 * This is a new and locked page, it shouldn't be mapped or
905 * have any special flags on it
907 if (WARN_ON(page_mapped(oldpage)))
908 goto out_fallback_unlock;
909 if (WARN_ON(page_has_private(oldpage)))
910 goto out_fallback_unlock;
911 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
912 goto out_fallback_unlock;
913 if (WARN_ON(PageMlocked(oldpage)))
914 goto out_fallback_unlock;
916 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
918 unlock_page(newpage);
922 page_cache_get(newpage);
924 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
925 lru_cache_add_file(newpage);
928 spin_lock(&cs->req->waitq.lock);
929 if (test_bit(FR_ABORTED, &cs->req->flags))
933 spin_unlock(&cs->req->waitq.lock);
936 unlock_page(newpage);
937 page_cache_release(newpage);
941 unlock_page(oldpage);
942 page_cache_release(oldpage);
948 unlock_page(newpage);
951 cs->offset = buf->offset;
953 err = lock_request(cs->req);
960 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
961 unsigned offset, unsigned count)
963 struct pipe_buffer *buf;
966 if (cs->nr_segs == cs->pipe->buffers)
969 err = unlock_request(cs->req);
973 fuse_copy_finish(cs);
976 page_cache_get(page);
978 buf->offset = offset;
989 * Copy a page in the request to/from the userspace buffer. Must be
992 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
993 unsigned offset, unsigned count, int zeroing)
996 struct page *page = *pagep;
998 if (page && zeroing && count < PAGE_SIZE)
999 clear_highpage(page);
1002 if (cs->write && cs->pipebufs && page) {
1003 return fuse_ref_page(cs, page, offset, count);
1004 } else if (!cs->len) {
1005 if (cs->move_pages && page &&
1006 offset == 0 && count == PAGE_SIZE) {
1007 err = fuse_try_move_page(cs, pagep);
1011 err = fuse_copy_fill(cs);
1017 void *mapaddr = kmap_atomic(page);
1018 void *buf = mapaddr + offset;
1019 offset += fuse_copy_do(cs, &buf, &count);
1020 kunmap_atomic(mapaddr);
1022 offset += fuse_copy_do(cs, NULL, &count);
1024 if (page && !cs->write)
1025 flush_dcache_page(page);
1029 /* Copy pages in the request to/from userspace buffer */
1030 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1034 struct fuse_req *req = cs->req;
1036 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1038 unsigned offset = req->page_descs[i].offset;
1039 unsigned count = min(nbytes, req->page_descs[i].length);
1041 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1051 /* Copy a single argument in the request to/from userspace buffer */
1052 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1056 int err = fuse_copy_fill(cs);
1060 fuse_copy_do(cs, &val, &size);
1065 /* Copy request arguments to/from userspace buffer */
1066 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1067 unsigned argpages, struct fuse_arg *args,
1073 for (i = 0; !err && i < numargs; i++) {
1074 struct fuse_arg *arg = &args[i];
1075 if (i == numargs - 1 && argpages)
1076 err = fuse_copy_pages(cs, arg->size, zeroing);
1078 err = fuse_copy_one(cs, arg->value, arg->size);
1083 static int forget_pending(struct fuse_iqueue *fiq)
1085 return fiq->forget_list_head.next != NULL;
1088 static int request_pending(struct fuse_iqueue *fiq)
1090 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1091 forget_pending(fiq);
1095 * Transfer an interrupt request to userspace
1097 * Unlike other requests this is assembled on demand, without a need
1098 * to allocate a separate fuse_req structure.
1100 * Called with fiq->waitq.lock held, releases it
1102 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1103 struct fuse_copy_state *cs,
1104 size_t nbytes, struct fuse_req *req)
1105 __releases(fiq->waitq.lock)
1107 struct fuse_in_header ih;
1108 struct fuse_interrupt_in arg;
1109 unsigned reqsize = sizeof(ih) + sizeof(arg);
1112 list_del_init(&req->intr_entry);
1113 req->intr_unique = fuse_get_unique(fiq);
1114 memset(&ih, 0, sizeof(ih));
1115 memset(&arg, 0, sizeof(arg));
1117 ih.opcode = FUSE_INTERRUPT;
1118 ih.unique = req->intr_unique;
1119 arg.unique = req->in.h.unique;
1121 spin_unlock(&fiq->waitq.lock);
1122 if (nbytes < reqsize)
1125 err = fuse_copy_one(cs, &ih, sizeof(ih));
1127 err = fuse_copy_one(cs, &arg, sizeof(arg));
1128 fuse_copy_finish(cs);
1130 return err ? err : reqsize;
1133 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1137 struct fuse_forget_link *head = fiq->forget_list_head.next;
1138 struct fuse_forget_link **newhead = &head;
1141 for (count = 0; *newhead != NULL && count < max; count++)
1142 newhead = &(*newhead)->next;
1144 fiq->forget_list_head.next = *newhead;
1146 if (fiq->forget_list_head.next == NULL)
1147 fiq->forget_list_tail = &fiq->forget_list_head;
1155 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1156 struct fuse_copy_state *cs,
1158 __releases(fiq->waitq.lock)
1161 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1162 struct fuse_forget_in arg = {
1163 .nlookup = forget->forget_one.nlookup,
1165 struct fuse_in_header ih = {
1166 .opcode = FUSE_FORGET,
1167 .nodeid = forget->forget_one.nodeid,
1168 .unique = fuse_get_unique(fiq),
1169 .len = sizeof(ih) + sizeof(arg),
1172 spin_unlock(&fiq->waitq.lock);
1174 if (nbytes < ih.len)
1177 err = fuse_copy_one(cs, &ih, sizeof(ih));
1179 err = fuse_copy_one(cs, &arg, sizeof(arg));
1180 fuse_copy_finish(cs);
1188 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1189 struct fuse_copy_state *cs, size_t nbytes)
1190 __releases(fiq->waitq.lock)
1193 unsigned max_forgets;
1195 struct fuse_forget_link *head;
1196 struct fuse_batch_forget_in arg = { .count = 0 };
1197 struct fuse_in_header ih = {
1198 .opcode = FUSE_BATCH_FORGET,
1199 .unique = fuse_get_unique(fiq),
1200 .len = sizeof(ih) + sizeof(arg),
1203 if (nbytes < ih.len) {
1204 spin_unlock(&fiq->waitq.lock);
1208 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1209 head = dequeue_forget(fiq, max_forgets, &count);
1210 spin_unlock(&fiq->waitq.lock);
1213 ih.len += count * sizeof(struct fuse_forget_one);
1214 err = fuse_copy_one(cs, &ih, sizeof(ih));
1216 err = fuse_copy_one(cs, &arg, sizeof(arg));
1219 struct fuse_forget_link *forget = head;
1222 err = fuse_copy_one(cs, &forget->forget_one,
1223 sizeof(forget->forget_one));
1225 head = forget->next;
1229 fuse_copy_finish(cs);
1237 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1238 struct fuse_copy_state *cs,
1240 __releases(fiq->waitq.lock)
1242 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1243 return fuse_read_single_forget(fiq, cs, nbytes);
1245 return fuse_read_batch_forget(fiq, cs, nbytes);
1249 * Read a single request into the userspace filesystem's buffer. This
1250 * function waits until a request is available, then removes it from
1251 * the pending list and copies request data to userspace buffer. If
1252 * no reply is needed (FORGET) or request has been aborted or there
1253 * was an error during the copying then it's finished by calling
1254 * request_end(). Otherwise add it to the processing list, and set
1257 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1258 struct fuse_copy_state *cs, size_t nbytes)
1261 struct fuse_conn *fc = fud->fc;
1262 struct fuse_iqueue *fiq = &fc->iq;
1263 struct fuse_pqueue *fpq = &fud->pq;
1264 struct fuse_req *req;
1269 spin_lock(&fiq->waitq.lock);
1271 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1272 !request_pending(fiq))
1275 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1276 !fiq->connected || request_pending(fiq));
1281 if (!fiq->connected)
1284 if (!list_empty(&fiq->interrupts)) {
1285 req = list_entry(fiq->interrupts.next, struct fuse_req,
1287 return fuse_read_interrupt(fiq, cs, nbytes, req);
1290 if (forget_pending(fiq)) {
1291 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1292 return fuse_read_forget(fc, fiq, cs, nbytes);
1294 if (fiq->forget_batch <= -8)
1295 fiq->forget_batch = 16;
1298 req = list_entry(fiq->pending.next, struct fuse_req, list);
1299 clear_bit(FR_PENDING, &req->flags);
1300 list_del_init(&req->list);
1301 spin_unlock(&fiq->waitq.lock);
1304 reqsize = in->h.len;
1305 /* If request is too large, reply with an error and restart the read */
1306 if (nbytes < reqsize) {
1307 req->out.h.error = -EIO;
1308 /* SETXATTR is special, since it may contain too large data */
1309 if (in->h.opcode == FUSE_SETXATTR)
1310 req->out.h.error = -E2BIG;
1311 request_end(fc, req);
1314 spin_lock(&fpq->lock);
1315 list_add(&req->list, &fpq->io);
1316 spin_unlock(&fpq->lock);
1318 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1320 err = fuse_copy_args(cs, in->numargs, in->argpages,
1321 (struct fuse_arg *) in->args, 0);
1322 fuse_copy_finish(cs);
1323 spin_lock(&fpq->lock);
1324 clear_bit(FR_LOCKED, &req->flags);
1325 if (!fpq->connected) {
1330 req->out.h.error = -EIO;
1333 if (!test_bit(FR_ISREPLY, &req->flags)) {
1337 list_move_tail(&req->list, &fpq->processing);
1338 __fuse_get_request(req);
1339 set_bit(FR_SENT, &req->flags);
1340 spin_unlock(&fpq->lock);
1341 /* matches barrier in request_wait_answer() */
1342 smp_mb__after_atomic();
1343 if (test_bit(FR_INTERRUPTED, &req->flags))
1344 queue_interrupt(fiq, req);
1345 fuse_put_request(fc, req);
1350 if (!test_bit(FR_PRIVATE, &req->flags))
1351 list_del_init(&req->list);
1352 spin_unlock(&fpq->lock);
1353 request_end(fc, req);
1357 spin_unlock(&fiq->waitq.lock);
1361 static int fuse_dev_open(struct inode *inode, struct file *file)
1364 * The fuse device's file's private_data is used to hold
1365 * the fuse_conn(ection) when it is mounted, and is used to
1366 * keep track of whether the file has been mounted already.
1368 file->private_data = NULL;
1372 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1374 struct fuse_copy_state cs;
1375 struct file *file = iocb->ki_filp;
1376 struct fuse_dev *fud = fuse_get_dev(file);
1381 if (!iter_is_iovec(to))
1384 fuse_copy_init(&cs, 1, to);
1386 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1389 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1390 struct pipe_inode_info *pipe,
1391 size_t len, unsigned int flags)
1396 struct pipe_buffer *bufs;
1397 struct fuse_copy_state cs;
1398 struct fuse_dev *fud = fuse_get_dev(in);
1403 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1407 fuse_copy_init(&cs, 1, NULL);
1410 ret = fuse_dev_do_read(fud, in, &cs, len);
1417 if (!pipe->readers) {
1418 send_sig(SIGPIPE, current, 0);
1424 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1429 while (page_nr < cs.nr_segs) {
1430 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1431 struct pipe_buffer *buf = pipe->bufs + newbuf;
1433 buf->page = bufs[page_nr].page;
1434 buf->offset = bufs[page_nr].offset;
1435 buf->len = bufs[page_nr].len;
1437 * Need to be careful about this. Having buf->ops in module
1438 * code can Oops if the buffer persists after module unload.
1440 buf->ops = &nosteal_pipe_buf_ops;
1455 if (waitqueue_active(&pipe->wait))
1456 wake_up_interruptible(&pipe->wait);
1457 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1461 for (; page_nr < cs.nr_segs; page_nr++)
1462 page_cache_release(bufs[page_nr].page);
1468 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1469 struct fuse_copy_state *cs)
1471 struct fuse_notify_poll_wakeup_out outarg;
1474 if (size != sizeof(outarg))
1477 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1481 fuse_copy_finish(cs);
1482 return fuse_notify_poll_wakeup(fc, &outarg);
1485 fuse_copy_finish(cs);
1489 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1490 struct fuse_copy_state *cs)
1492 struct fuse_notify_inval_inode_out outarg;
1495 if (size != sizeof(outarg))
1498 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1501 fuse_copy_finish(cs);
1503 down_read(&fc->killsb);
1506 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1507 outarg.off, outarg.len);
1509 up_read(&fc->killsb);
1513 fuse_copy_finish(cs);
1517 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1518 struct fuse_copy_state *cs)
1520 struct fuse_notify_inval_entry_out outarg;
1525 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1530 if (size < sizeof(outarg))
1533 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1537 err = -ENAMETOOLONG;
1538 if (outarg.namelen > FUSE_NAME_MAX)
1542 if (size != sizeof(outarg) + outarg.namelen + 1)
1546 name.len = outarg.namelen;
1547 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1550 fuse_copy_finish(cs);
1551 buf[outarg.namelen] = 0;
1552 name.hash = full_name_hash(name.name, name.len);
1554 down_read(&fc->killsb);
1557 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1558 up_read(&fc->killsb);
1564 fuse_copy_finish(cs);
1568 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1569 struct fuse_copy_state *cs)
1571 struct fuse_notify_delete_out outarg;
1576 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1581 if (size < sizeof(outarg))
1584 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1588 err = -ENAMETOOLONG;
1589 if (outarg.namelen > FUSE_NAME_MAX)
1593 if (size != sizeof(outarg) + outarg.namelen + 1)
1597 name.len = outarg.namelen;
1598 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1601 fuse_copy_finish(cs);
1602 buf[outarg.namelen] = 0;
1603 name.hash = full_name_hash(name.name, name.len);
1605 down_read(&fc->killsb);
1608 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1609 outarg.child, &name);
1610 up_read(&fc->killsb);
1616 fuse_copy_finish(cs);
1620 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1621 struct fuse_copy_state *cs)
1623 struct fuse_notify_store_out outarg;
1624 struct inode *inode;
1625 struct address_space *mapping;
1629 unsigned int offset;
1635 if (size < sizeof(outarg))
1638 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1643 if (size - sizeof(outarg) != outarg.size)
1646 nodeid = outarg.nodeid;
1648 down_read(&fc->killsb);
1654 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1658 mapping = inode->i_mapping;
1659 index = outarg.offset >> PAGE_CACHE_SHIFT;
1660 offset = outarg.offset & ~PAGE_CACHE_MASK;
1661 file_size = i_size_read(inode);
1662 end = outarg.offset + outarg.size;
1663 if (end > file_size) {
1665 fuse_write_update_size(inode, file_size);
1671 unsigned int this_num;
1674 page = find_or_create_page(mapping, index,
1675 mapping_gfp_mask(mapping));
1679 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1680 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1681 if (!err && offset == 0 &&
1682 (this_num == PAGE_CACHE_SIZE || file_size == end))
1683 SetPageUptodate(page);
1685 page_cache_release(page);
1700 up_read(&fc->killsb);
1702 fuse_copy_finish(cs);
1706 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1708 release_pages(req->pages, req->num_pages, false);
1711 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1712 struct fuse_notify_retrieve_out *outarg)
1715 struct address_space *mapping = inode->i_mapping;
1716 struct fuse_req *req;
1720 unsigned int offset;
1721 size_t total_len = 0;
1724 offset = outarg->offset & ~PAGE_CACHE_MASK;
1725 file_size = i_size_read(inode);
1727 num = min(outarg->size, fc->max_write);
1728 if (outarg->offset > file_size)
1730 else if (outarg->offset + num > file_size)
1731 num = file_size - outarg->offset;
1733 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1734 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1736 req = fuse_get_req(fc, num_pages);
1738 return PTR_ERR(req);
1740 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1741 req->in.h.nodeid = outarg->nodeid;
1742 req->in.numargs = 2;
1743 req->in.argpages = 1;
1744 req->end = fuse_retrieve_end;
1746 index = outarg->offset >> PAGE_CACHE_SHIFT;
1748 while (num && req->num_pages < num_pages) {
1750 unsigned int this_num;
1752 page = find_get_page(mapping, index);
1756 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1757 req->pages[req->num_pages] = page;
1758 req->page_descs[req->num_pages].offset = offset;
1759 req->page_descs[req->num_pages].length = this_num;
1764 total_len += this_num;
1767 req->misc.retrieve_in.offset = outarg->offset;
1768 req->misc.retrieve_in.size = total_len;
1769 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1770 req->in.args[0].value = &req->misc.retrieve_in;
1771 req->in.args[1].size = total_len;
1773 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1775 fuse_retrieve_end(fc, req);
1776 fuse_put_request(fc, req);
1782 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1783 struct fuse_copy_state *cs)
1785 struct fuse_notify_retrieve_out outarg;
1786 struct inode *inode;
1790 if (size != sizeof(outarg))
1793 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1797 fuse_copy_finish(cs);
1799 down_read(&fc->killsb);
1802 u64 nodeid = outarg.nodeid;
1804 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1806 err = fuse_retrieve(fc, inode, &outarg);
1810 up_read(&fc->killsb);
1815 fuse_copy_finish(cs);
1819 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1820 unsigned int size, struct fuse_copy_state *cs)
1822 /* Don't try to move pages (yet) */
1826 case FUSE_NOTIFY_POLL:
1827 return fuse_notify_poll(fc, size, cs);
1829 case FUSE_NOTIFY_INVAL_INODE:
1830 return fuse_notify_inval_inode(fc, size, cs);
1832 case FUSE_NOTIFY_INVAL_ENTRY:
1833 return fuse_notify_inval_entry(fc, size, cs);
1835 case FUSE_NOTIFY_STORE:
1836 return fuse_notify_store(fc, size, cs);
1838 case FUSE_NOTIFY_RETRIEVE:
1839 return fuse_notify_retrieve(fc, size, cs);
1841 case FUSE_NOTIFY_DELETE:
1842 return fuse_notify_delete(fc, size, cs);
1845 fuse_copy_finish(cs);
1850 /* Look up request on processing list by unique ID */
1851 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1853 struct fuse_req *req;
1855 list_for_each_entry(req, &fpq->processing, list) {
1856 if (req->in.h.unique == unique || req->intr_unique == unique)
1862 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1865 unsigned reqsize = sizeof(struct fuse_out_header);
1868 return nbytes != reqsize ? -EINVAL : 0;
1870 reqsize += len_args(out->numargs, out->args);
1872 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1874 else if (reqsize > nbytes) {
1875 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1876 unsigned diffsize = reqsize - nbytes;
1877 if (diffsize > lastarg->size)
1879 lastarg->size -= diffsize;
1881 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1886 * Write a single reply to a request. First the header is copied from
1887 * the write buffer. The request is then searched on the processing
1888 * list by the unique ID found in the header. If found, then remove
1889 * it from the list and copy the rest of the buffer to the request.
1890 * The request is finished by calling request_end()
1892 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1893 struct fuse_copy_state *cs, size_t nbytes)
1896 struct fuse_conn *fc = fud->fc;
1897 struct fuse_pqueue *fpq = &fud->pq;
1898 struct fuse_req *req;
1899 struct fuse_out_header oh;
1901 if (nbytes < sizeof(struct fuse_out_header))
1904 err = fuse_copy_one(cs, &oh, sizeof(oh));
1909 if (oh.len != nbytes)
1913 * Zero oh.unique indicates unsolicited notification message
1914 * and error contains notification code.
1917 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1918 return err ? err : nbytes;
1922 if (oh.error <= -1000 || oh.error > 0)
1925 spin_lock(&fpq->lock);
1927 if (!fpq->connected)
1930 req = request_find(fpq, oh.unique);
1934 /* Is it an interrupt reply? */
1935 if (req->intr_unique == oh.unique) {
1936 __fuse_get_request(req);
1937 spin_unlock(&fpq->lock);
1940 if (nbytes != sizeof(struct fuse_out_header)) {
1941 fuse_put_request(fc, req);
1945 if (oh.error == -ENOSYS)
1946 fc->no_interrupt = 1;
1947 else if (oh.error == -EAGAIN)
1948 queue_interrupt(&fc->iq, req);
1949 fuse_put_request(fc, req);
1951 fuse_copy_finish(cs);
1955 clear_bit(FR_SENT, &req->flags);
1956 list_move(&req->list, &fpq->io);
1958 set_bit(FR_LOCKED, &req->flags);
1959 spin_unlock(&fpq->lock);
1961 if (!req->out.page_replace)
1964 err = copy_out_args(cs, &req->out, nbytes);
1965 fuse_copy_finish(cs);
1967 spin_lock(&fpq->lock);
1968 clear_bit(FR_LOCKED, &req->flags);
1969 if (!fpq->connected)
1972 req->out.h.error = -EIO;
1973 if (!test_bit(FR_PRIVATE, &req->flags))
1974 list_del_init(&req->list);
1975 spin_unlock(&fpq->lock);
1977 request_end(fc, req);
1979 return err ? err : nbytes;
1982 spin_unlock(&fpq->lock);
1984 fuse_copy_finish(cs);
1988 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1990 struct fuse_copy_state cs;
1991 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1996 if (!iter_is_iovec(from))
1999 fuse_copy_init(&cs, 0, from);
2001 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
2004 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
2005 struct file *out, loff_t *ppos,
2006 size_t len, unsigned int flags)
2010 struct pipe_buffer *bufs;
2011 struct fuse_copy_state cs;
2012 struct fuse_dev *fud;
2016 fud = fuse_get_dev(out);
2022 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2030 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2031 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2041 struct pipe_buffer *ibuf;
2042 struct pipe_buffer *obuf;
2044 BUG_ON(nbuf >= pipe->buffers);
2045 BUG_ON(!pipe->nrbufs);
2046 ibuf = &pipe->bufs[pipe->curbuf];
2049 if (rem >= ibuf->len) {
2052 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2055 ibuf->ops->get(pipe, ibuf);
2057 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2059 ibuf->offset += obuf->len;
2060 ibuf->len -= obuf->len;
2067 fuse_copy_init(&cs, 0, NULL);
2072 if (flags & SPLICE_F_MOVE)
2075 ret = fuse_dev_do_write(fud, &cs, len);
2078 for (idx = 0; idx < nbuf; idx++) {
2079 struct pipe_buffer *buf = &bufs[idx];
2080 buf->ops->release(pipe, buf);
2089 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2091 unsigned mask = POLLOUT | POLLWRNORM;
2092 struct fuse_iqueue *fiq;
2093 struct fuse_dev *fud = fuse_get_dev(file);
2099 poll_wait(file, &fiq->waitq, wait);
2101 spin_lock(&fiq->waitq.lock);
2102 if (!fiq->connected)
2104 else if (request_pending(fiq))
2105 mask |= POLLIN | POLLRDNORM;
2106 spin_unlock(&fiq->waitq.lock);
2112 * Abort all requests on the given list (pending or processing)
2114 * This function releases and reacquires fc->lock
2116 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2118 while (!list_empty(head)) {
2119 struct fuse_req *req;
2120 req = list_entry(head->next, struct fuse_req, list);
2121 req->out.h.error = -ECONNABORTED;
2122 clear_bit(FR_SENT, &req->flags);
2123 list_del_init(&req->list);
2124 request_end(fc, req);
2128 static void end_polls(struct fuse_conn *fc)
2132 p = rb_first(&fc->polled_files);
2135 struct fuse_file *ff;
2136 ff = rb_entry(p, struct fuse_file, polled_node);
2137 wake_up_interruptible_all(&ff->poll_wait);
2144 * Abort all requests.
2146 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2149 * The same effect is usually achievable through killing the filesystem daemon
2150 * and all users of the filesystem. The exception is the combination of an
2151 * asynchronous request and the tricky deadlock (see
2152 * Documentation/filesystems/fuse.txt).
2154 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2155 * requests, they should be finished off immediately. Locked requests will be
2156 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2157 * requests. It is possible that some request will finish before we can. This
2158 * is OK, the request will in that case be removed from the list before we touch
2161 void fuse_abort_conn(struct fuse_conn *fc)
2163 struct fuse_iqueue *fiq = &fc->iq;
2165 spin_lock(&fc->lock);
2166 if (fc->connected) {
2167 struct fuse_dev *fud;
2168 struct fuse_req *req, *next;
2174 fuse_set_initialized(fc);
2175 list_for_each_entry(fud, &fc->devices, entry) {
2176 struct fuse_pqueue *fpq = &fud->pq;
2178 spin_lock(&fpq->lock);
2180 list_for_each_entry_safe(req, next, &fpq->io, list) {
2181 req->out.h.error = -ECONNABORTED;
2182 spin_lock(&req->waitq.lock);
2183 set_bit(FR_ABORTED, &req->flags);
2184 if (!test_bit(FR_LOCKED, &req->flags)) {
2185 set_bit(FR_PRIVATE, &req->flags);
2186 __fuse_get_request(req);
2187 list_move(&req->list, &to_end1);
2189 spin_unlock(&req->waitq.lock);
2191 list_splice_init(&fpq->processing, &to_end2);
2192 spin_unlock(&fpq->lock);
2194 fc->max_background = UINT_MAX;
2197 spin_lock(&fiq->waitq.lock);
2199 list_splice_init(&fiq->pending, &to_end2);
2200 list_for_each_entry(req, &to_end2, list)
2201 clear_bit(FR_PENDING, &req->flags);
2202 while (forget_pending(fiq))
2203 kfree(dequeue_forget(fiq, 1, NULL));
2204 wake_up_all_locked(&fiq->waitq);
2205 spin_unlock(&fiq->waitq.lock);
2206 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2208 wake_up_all(&fc->blocked_waitq);
2209 spin_unlock(&fc->lock);
2211 while (!list_empty(&to_end1)) {
2212 req = list_first_entry(&to_end1, struct fuse_req, list);
2213 list_del_init(&req->list);
2214 request_end(fc, req);
2216 end_requests(fc, &to_end2);
2218 spin_unlock(&fc->lock);
2221 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2223 void fuse_wait_aborted(struct fuse_conn *fc)
2225 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2228 int fuse_dev_release(struct inode *inode, struct file *file)
2230 struct fuse_dev *fud = fuse_get_dev(file);
2233 struct fuse_conn *fc = fud->fc;
2234 struct fuse_pqueue *fpq = &fud->pq;
2237 spin_lock(&fpq->lock);
2238 WARN_ON(!list_empty(&fpq->io));
2239 list_splice_init(&fpq->processing, &to_end);
2240 spin_unlock(&fpq->lock);
2242 end_requests(fc, &to_end);
2244 /* Are we the last open device? */
2245 if (atomic_dec_and_test(&fc->dev_count)) {
2246 WARN_ON(fc->iq.fasync != NULL);
2247 fuse_abort_conn(fc);
2253 EXPORT_SYMBOL_GPL(fuse_dev_release);
2255 static int fuse_dev_fasync(int fd, struct file *file, int on)
2257 struct fuse_dev *fud = fuse_get_dev(file);
2262 /* No locking - fasync_helper does its own locking */
2263 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2266 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2268 struct fuse_dev *fud;
2270 if (new->private_data)
2273 fud = fuse_dev_alloc(fc);
2277 new->private_data = fud;
2278 atomic_inc(&fc->dev_count);
2283 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2288 if (cmd == FUSE_DEV_IOC_CLONE) {
2292 if (!get_user(oldfd, (__u32 __user *) arg)) {
2293 struct file *old = fget(oldfd);
2297 struct fuse_dev *fud = NULL;
2300 * Check against file->f_op because CUSE
2301 * uses the same ioctl handler.
2303 if (old->f_op == file->f_op &&
2304 old->f_cred->user_ns == file->f_cred->user_ns)
2305 fud = fuse_get_dev(old);
2308 mutex_lock(&fuse_mutex);
2309 err = fuse_device_clone(fud->fc, file);
2310 mutex_unlock(&fuse_mutex);
2319 const struct file_operations fuse_dev_operations = {
2320 .owner = THIS_MODULE,
2321 .open = fuse_dev_open,
2322 .llseek = no_llseek,
2323 .read_iter = fuse_dev_read,
2324 .splice_read = fuse_dev_splice_read,
2325 .write_iter = fuse_dev_write,
2326 .splice_write = fuse_dev_splice_write,
2327 .poll = fuse_dev_poll,
2328 .release = fuse_dev_release,
2329 .fasync = fuse_dev_fasync,
2330 .unlocked_ioctl = fuse_dev_ioctl,
2331 .compat_ioctl = fuse_dev_ioctl,
2333 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2335 static struct miscdevice fuse_miscdevice = {
2336 .minor = FUSE_MINOR,
2338 .fops = &fuse_dev_operations,
2341 int __init fuse_dev_init(void)
2344 fuse_req_cachep = kmem_cache_create("fuse_request",
2345 sizeof(struct fuse_req),
2347 if (!fuse_req_cachep)
2350 err = misc_register(&fuse_miscdevice);
2352 goto out_cache_clean;
2357 kmem_cache_destroy(fuse_req_cachep);
2362 void fuse_dev_cleanup(void)
2364 misc_deregister(&fuse_miscdevice);
2365 kmem_cache_destroy(fuse_req_cachep);