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/namei.h>
17 #include <linux/pagemap.h>
18 #include <linux/file.h>
19 #include <linux/slab.h>
20 #include <linux/pipe_fs_i.h>
21 #include <linux/swap.h>
22 #include <linux/splice.h>
23 #include <linux/freezer.h>
25 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
26 MODULE_ALIAS("devname:fuse");
28 static struct kmem_cache *fuse_req_cachep;
30 static struct fuse_dev *fuse_get_dev(struct file *file)
33 * Lockless access is OK, because file->private data is set
34 * once during mount and is valid until the file is released.
36 return ACCESS_ONCE(file->private_data);
39 static void fuse_request_init(struct fuse_req *req, struct page **pages,
40 struct fuse_page_desc *page_descs,
43 memset(req, 0, sizeof(*req));
44 memset(pages, 0, sizeof(*pages) * npages);
45 memset(page_descs, 0, sizeof(*page_descs) * npages);
46 INIT_LIST_HEAD(&req->list);
47 INIT_LIST_HEAD(&req->intr_entry);
48 init_waitqueue_head(&req->waitq);
49 atomic_set(&req->count, 1);
51 req->page_descs = page_descs;
52 req->max_pages = npages;
53 __set_bit(FR_PENDING, &req->flags);
56 static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
58 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
61 struct fuse_page_desc *page_descs;
63 if (npages <= FUSE_REQ_INLINE_PAGES) {
64 pages = req->inline_pages;
65 page_descs = req->inline_page_descs;
67 pages = kmalloc(sizeof(struct page *) * npages, flags);
68 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
72 if (!pages || !page_descs) {
75 kmem_cache_free(fuse_req_cachep, req);
79 fuse_request_init(req, pages, page_descs, npages);
84 struct fuse_req *fuse_request_alloc(unsigned npages)
86 return __fuse_request_alloc(npages, GFP_KERNEL);
88 EXPORT_SYMBOL_GPL(fuse_request_alloc);
90 struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
92 return __fuse_request_alloc(npages, GFP_NOFS);
95 void fuse_request_free(struct fuse_req *req)
97 if (req->pages != req->inline_pages) {
99 kfree(req->page_descs);
101 kmem_cache_free(fuse_req_cachep, req);
104 static void block_sigs(sigset_t *oldset)
108 siginitsetinv(&mask, sigmask(SIGKILL));
109 sigprocmask(SIG_BLOCK, &mask, oldset);
112 static void restore_sigs(sigset_t *oldset)
114 sigprocmask(SIG_SETMASK, oldset, NULL);
117 void __fuse_get_request(struct fuse_req *req)
119 atomic_inc(&req->count);
122 /* Must be called with > 1 refcount */
123 static void __fuse_put_request(struct fuse_req *req)
125 BUG_ON(atomic_read(&req->count) < 2);
126 atomic_dec(&req->count);
129 static void fuse_req_init_context(struct fuse_req *req)
131 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
132 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
133 req->in.h.pid = current->pid;
136 void fuse_set_initialized(struct fuse_conn *fc)
138 /* Make sure stores before this are seen on another CPU */
143 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
145 return !fc->initialized || (for_background && fc->blocked);
148 static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
151 struct fuse_req *req;
153 atomic_inc(&fc->num_waiting);
155 if (fuse_block_alloc(fc, for_background)) {
160 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
161 !fuse_block_alloc(fc, for_background));
162 restore_sigs(&oldset);
167 /* Matches smp_wmb() in fuse_set_initialized() */
178 req = fuse_request_alloc(npages);
182 wake_up(&fc->blocked_waitq);
186 fuse_req_init_context(req);
187 __set_bit(FR_WAITING, &req->flags);
189 __set_bit(FR_BACKGROUND, &req->flags);
194 atomic_dec(&fc->num_waiting);
198 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
200 return __fuse_get_req(fc, npages, false);
202 EXPORT_SYMBOL_GPL(fuse_get_req);
204 struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
207 return __fuse_get_req(fc, npages, true);
209 EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
212 * Return request in fuse_file->reserved_req. However that may
213 * currently be in use. If that is the case, wait for it to become
216 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
219 struct fuse_req *req = NULL;
220 struct fuse_file *ff = file->private_data;
223 wait_event(fc->reserved_req_waitq, ff->reserved_req);
224 spin_lock(&fc->lock);
225 if (ff->reserved_req) {
226 req = ff->reserved_req;
227 ff->reserved_req = NULL;
228 req->stolen_file = get_file(file);
230 spin_unlock(&fc->lock);
237 * Put stolen request back into fuse_file->reserved_req
239 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
241 struct file *file = req->stolen_file;
242 struct fuse_file *ff = file->private_data;
244 spin_lock(&fc->lock);
245 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
246 BUG_ON(ff->reserved_req);
247 ff->reserved_req = req;
248 wake_up_all(&fc->reserved_req_waitq);
249 spin_unlock(&fc->lock);
254 * Gets a requests for a file operation, always succeeds
256 * This is used for sending the FLUSH request, which must get to
257 * userspace, due to POSIX locks which may need to be unlocked.
259 * If allocation fails due to OOM, use the reserved request in
262 * This is very unlikely to deadlock accidentally, since the
263 * filesystem should not have it's own file open. If deadlock is
264 * intentional, it can still be broken by "aborting" the filesystem.
266 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
269 struct fuse_req *req;
271 atomic_inc(&fc->num_waiting);
272 wait_event(fc->blocked_waitq, fc->initialized);
273 /* Matches smp_wmb() in fuse_set_initialized() */
275 req = fuse_request_alloc(0);
277 req = get_reserved_req(fc, file);
279 fuse_req_init_context(req);
280 __set_bit(FR_WAITING, &req->flags);
281 __clear_bit(FR_BACKGROUND, &req->flags);
285 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
287 if (atomic_dec_and_test(&req->count)) {
288 if (test_bit(FR_BACKGROUND, &req->flags)) {
290 * We get here in the unlikely case that a background
291 * request was allocated but not sent
293 spin_lock(&fc->lock);
295 wake_up(&fc->blocked_waitq);
296 spin_unlock(&fc->lock);
299 if (test_bit(FR_WAITING, &req->flags)) {
300 __clear_bit(FR_WAITING, &req->flags);
301 atomic_dec(&fc->num_waiting);
304 if (req->stolen_file)
305 put_reserved_req(fc, req);
307 fuse_request_free(req);
310 EXPORT_SYMBOL_GPL(fuse_put_request);
312 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
317 for (i = 0; i < numargs; i++)
318 nbytes += args[i].size;
323 static u64 fuse_get_unique(struct fuse_iqueue *fiq)
325 return ++fiq->reqctr;
328 static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
330 req->in.h.len = sizeof(struct fuse_in_header) +
331 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
332 list_add_tail(&req->list, &fiq->pending);
333 wake_up_locked(&fiq->waitq);
334 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
337 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
338 u64 nodeid, u64 nlookup)
340 struct fuse_iqueue *fiq = &fc->iq;
342 forget->forget_one.nodeid = nodeid;
343 forget->forget_one.nlookup = nlookup;
345 spin_lock(&fiq->waitq.lock);
346 if (fiq->connected) {
347 fiq->forget_list_tail->next = forget;
348 fiq->forget_list_tail = forget;
349 wake_up_locked(&fiq->waitq);
350 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
354 spin_unlock(&fiq->waitq.lock);
357 static void flush_bg_queue(struct fuse_conn *fc)
359 while (fc->active_background < fc->max_background &&
360 !list_empty(&fc->bg_queue)) {
361 struct fuse_req *req;
362 struct fuse_iqueue *fiq = &fc->iq;
364 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
365 list_del(&req->list);
366 fc->active_background++;
367 spin_lock(&fiq->waitq.lock);
368 req->in.h.unique = fuse_get_unique(fiq);
369 queue_request(fiq, req);
370 spin_unlock(&fiq->waitq.lock);
375 * This function is called when a request is finished. Either a reply
376 * has arrived or it was aborted (and not yet sent) or some error
377 * occurred during communication with userspace, or the device file
378 * was closed. The requester thread is woken up (if still waiting),
379 * the 'end' callback is called if given, else the reference to the
380 * request is released
382 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
384 struct fuse_iqueue *fiq = &fc->iq;
386 if (test_and_set_bit(FR_FINISHED, &req->flags))
389 spin_lock(&fiq->waitq.lock);
390 list_del_init(&req->intr_entry);
391 spin_unlock(&fiq->waitq.lock);
392 WARN_ON(test_bit(FR_PENDING, &req->flags));
393 WARN_ON(test_bit(FR_SENT, &req->flags));
394 if (test_bit(FR_BACKGROUND, &req->flags)) {
395 spin_lock(&fc->lock);
396 clear_bit(FR_BACKGROUND, &req->flags);
397 if (fc->num_background == fc->max_background)
400 /* Wake up next waiter, if any */
401 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
402 wake_up(&fc->blocked_waitq);
404 if (fc->num_background == fc->congestion_threshold &&
405 fc->connected && fc->bdi_initialized) {
406 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
407 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
409 fc->num_background--;
410 fc->active_background--;
412 spin_unlock(&fc->lock);
414 wake_up(&req->waitq);
417 fuse_put_request(fc, req);
420 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
422 spin_lock(&fiq->waitq.lock);
423 if (test_bit(FR_FINISHED, &req->flags)) {
424 spin_unlock(&fiq->waitq.lock);
427 if (list_empty(&req->intr_entry)) {
428 list_add_tail(&req->intr_entry, &fiq->interrupts);
429 wake_up_locked(&fiq->waitq);
431 spin_unlock(&fiq->waitq.lock);
432 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
435 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
437 struct fuse_iqueue *fiq = &fc->iq;
440 if (!fc->no_interrupt) {
441 /* Any signal may interrupt this */
442 err = wait_event_interruptible(req->waitq,
443 test_bit(FR_FINISHED, &req->flags));
447 set_bit(FR_INTERRUPTED, &req->flags);
448 /* matches barrier in fuse_dev_do_read() */
449 smp_mb__after_atomic();
450 if (test_bit(FR_SENT, &req->flags))
451 queue_interrupt(fiq, req);
454 if (!test_bit(FR_FORCE, &req->flags)) {
457 /* Only fatal signals may interrupt this */
459 err = wait_event_interruptible(req->waitq,
460 test_bit(FR_FINISHED, &req->flags));
461 restore_sigs(&oldset);
466 spin_lock(&fiq->waitq.lock);
467 /* Request is not yet in userspace, bail out */
468 if (test_bit(FR_PENDING, &req->flags)) {
469 list_del(&req->list);
470 spin_unlock(&fiq->waitq.lock);
471 __fuse_put_request(req);
472 req->out.h.error = -EINTR;
475 spin_unlock(&fiq->waitq.lock);
479 * Either request is already in userspace, or it was forced.
482 while (!test_bit(FR_FINISHED, &req->flags))
483 wait_event_freezable(req->waitq,
484 test_bit(FR_FINISHED, &req->flags));
487 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
489 struct fuse_iqueue *fiq = &fc->iq;
491 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
492 spin_lock(&fiq->waitq.lock);
493 if (!fiq->connected) {
494 spin_unlock(&fiq->waitq.lock);
495 req->out.h.error = -ENOTCONN;
497 req->in.h.unique = fuse_get_unique(fiq);
498 queue_request(fiq, req);
499 /* acquire extra reference, since request is still needed
500 after request_end() */
501 __fuse_get_request(req);
502 spin_unlock(&fiq->waitq.lock);
504 request_wait_answer(fc, req);
505 /* Pairs with smp_wmb() in request_end() */
510 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
512 __set_bit(FR_ISREPLY, &req->flags);
513 if (!test_bit(FR_WAITING, &req->flags)) {
514 __set_bit(FR_WAITING, &req->flags);
515 atomic_inc(&fc->num_waiting);
517 __fuse_request_send(fc, req);
519 EXPORT_SYMBOL_GPL(fuse_request_send);
521 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
523 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
524 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
527 switch (args->in.h.opcode) {
534 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
538 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
542 if (fc->minor < 12) {
543 switch (args->in.h.opcode) {
545 args->in.args[0].size = sizeof(struct fuse_open_in);
548 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
554 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
556 struct fuse_req *req;
559 req = fuse_get_req(fc, 0);
563 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
564 fuse_adjust_compat(fc, args);
566 req->in.h.opcode = args->in.h.opcode;
567 req->in.h.nodeid = args->in.h.nodeid;
568 req->in.numargs = args->in.numargs;
569 memcpy(req->in.args, args->in.args,
570 args->in.numargs * sizeof(struct fuse_in_arg));
571 req->out.argvar = args->out.argvar;
572 req->out.numargs = args->out.numargs;
573 memcpy(req->out.args, args->out.args,
574 args->out.numargs * sizeof(struct fuse_arg));
575 fuse_request_send(fc, req);
576 ret = req->out.h.error;
577 if (!ret && args->out.argvar) {
578 BUG_ON(args->out.numargs != 1);
579 ret = req->out.args[0].size;
581 fuse_put_request(fc, req);
587 * Called under fc->lock
589 * fc->connected must have been checked previously
591 void fuse_request_send_background_locked(struct fuse_conn *fc,
592 struct fuse_req *req)
594 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
595 if (!test_bit(FR_WAITING, &req->flags)) {
596 __set_bit(FR_WAITING, &req->flags);
597 atomic_inc(&fc->num_waiting);
599 __set_bit(FR_ISREPLY, &req->flags);
600 fc->num_background++;
601 if (fc->num_background == fc->max_background)
603 if (fc->num_background == fc->congestion_threshold &&
604 fc->bdi_initialized) {
605 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
606 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
608 list_add_tail(&req->list, &fc->bg_queue);
612 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
615 spin_lock(&fc->lock);
617 fuse_request_send_background_locked(fc, req);
618 spin_unlock(&fc->lock);
620 spin_unlock(&fc->lock);
621 req->out.h.error = -ENOTCONN;
623 fuse_put_request(fc, req);
626 EXPORT_SYMBOL_GPL(fuse_request_send_background);
628 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
629 struct fuse_req *req, u64 unique)
632 struct fuse_iqueue *fiq = &fc->iq;
634 __clear_bit(FR_ISREPLY, &req->flags);
635 req->in.h.unique = unique;
636 spin_lock(&fiq->waitq.lock);
637 if (fiq->connected) {
638 queue_request(fiq, req);
641 spin_unlock(&fiq->waitq.lock);
646 void fuse_force_forget(struct file *file, u64 nodeid)
648 struct inode *inode = file_inode(file);
649 struct fuse_conn *fc = get_fuse_conn(inode);
650 struct fuse_req *req;
651 struct fuse_forget_in inarg;
653 memset(&inarg, 0, sizeof(inarg));
655 req = fuse_get_req_nofail_nopages(fc, file);
656 req->in.h.opcode = FUSE_FORGET;
657 req->in.h.nodeid = nodeid;
659 req->in.args[0].size = sizeof(inarg);
660 req->in.args[0].value = &inarg;
661 __clear_bit(FR_ISREPLY, &req->flags);
662 __fuse_request_send(fc, req);
664 fuse_put_request(fc, req);
668 * Lock the request. Up to the next unlock_request() there mustn't be
669 * anything that could cause a page-fault. If the request was already
672 static int lock_request(struct fuse_req *req)
676 spin_lock(&req->waitq.lock);
677 if (test_bit(FR_ABORTED, &req->flags))
680 set_bit(FR_LOCKED, &req->flags);
681 spin_unlock(&req->waitq.lock);
687 * Unlock request. If it was aborted while locked, caller is responsible
688 * for unlocking and ending the request.
690 static int unlock_request(struct fuse_req *req)
694 spin_lock(&req->waitq.lock);
695 if (test_bit(FR_ABORTED, &req->flags))
698 clear_bit(FR_LOCKED, &req->flags);
699 spin_unlock(&req->waitq.lock);
704 struct fuse_copy_state {
706 struct fuse_req *req;
707 struct iov_iter *iter;
708 struct pipe_buffer *pipebufs;
709 struct pipe_buffer *currbuf;
710 struct pipe_inode_info *pipe;
711 unsigned long nr_segs;
715 unsigned move_pages:1;
718 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
719 struct iov_iter *iter)
721 memset(cs, 0, sizeof(*cs));
726 /* Unmap and put previous page of userspace buffer */
727 static void fuse_copy_finish(struct fuse_copy_state *cs)
730 struct pipe_buffer *buf = cs->currbuf;
733 buf->len = PAGE_SIZE - cs->len;
737 flush_dcache_page(cs->pg);
738 set_page_dirty_lock(cs->pg);
746 * Get another pagefull of userspace buffer, and map it to kernel
747 * address space, and lock request
749 static int fuse_copy_fill(struct fuse_copy_state *cs)
754 err = unlock_request(cs->req);
758 fuse_copy_finish(cs);
760 struct pipe_buffer *buf = cs->pipebufs;
763 err = buf->ops->confirm(cs->pipe, buf);
767 BUG_ON(!cs->nr_segs);
770 cs->offset = buf->offset;
775 if (cs->nr_segs == cs->pipe->buffers)
778 page = alloc_page(GFP_HIGHUSER);
795 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
803 iov_iter_advance(cs->iter, err);
806 return lock_request(cs->req);
809 /* Do as much copy to/from userspace buffer as we can */
810 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
812 unsigned ncpy = min(*size, cs->len);
814 void *pgaddr = kmap_atomic(cs->pg);
815 void *buf = pgaddr + cs->offset;
818 memcpy(buf, *val, ncpy);
820 memcpy(*val, buf, ncpy);
822 kunmap_atomic(pgaddr);
831 static int fuse_check_page(struct page *page)
833 if (page_mapcount(page) ||
834 page->mapping != NULL ||
835 page_count(page) != 1 ||
836 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
843 printk(KERN_WARNING "fuse: trying to steal weird page\n");
844 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);
850 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
853 struct page *oldpage = *pagep;
854 struct page *newpage;
855 struct pipe_buffer *buf = cs->pipebufs;
857 err = unlock_request(cs->req);
861 fuse_copy_finish(cs);
863 err = buf->ops->confirm(cs->pipe, buf);
867 BUG_ON(!cs->nr_segs);
873 if (cs->len != PAGE_SIZE)
876 if (buf->ops->steal(cs->pipe, buf) != 0)
881 if (!PageUptodate(newpage))
882 SetPageUptodate(newpage);
884 ClearPageMappedToDisk(newpage);
886 if (fuse_check_page(newpage) != 0)
887 goto out_fallback_unlock;
890 * This is a new and locked page, it shouldn't be mapped or
891 * have any special flags on it
893 if (WARN_ON(page_mapped(oldpage)))
894 goto out_fallback_unlock;
895 if (WARN_ON(page_has_private(oldpage)))
896 goto out_fallback_unlock;
897 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
898 goto out_fallback_unlock;
899 if (WARN_ON(PageMlocked(oldpage)))
900 goto out_fallback_unlock;
902 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
904 unlock_page(newpage);
908 page_cache_get(newpage);
910 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
911 lru_cache_add_file(newpage);
914 spin_lock(&cs->req->waitq.lock);
915 if (test_bit(FR_ABORTED, &cs->req->flags))
919 spin_unlock(&cs->req->waitq.lock);
922 unlock_page(newpage);
923 page_cache_release(newpage);
927 unlock_page(oldpage);
928 page_cache_release(oldpage);
934 unlock_page(newpage);
937 cs->offset = buf->offset;
939 err = lock_request(cs->req);
946 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
947 unsigned offset, unsigned count)
949 struct pipe_buffer *buf;
952 if (cs->nr_segs == cs->pipe->buffers)
955 err = unlock_request(cs->req);
959 fuse_copy_finish(cs);
962 page_cache_get(page);
964 buf->offset = offset;
975 * Copy a page in the request to/from the userspace buffer. Must be
978 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
979 unsigned offset, unsigned count, int zeroing)
982 struct page *page = *pagep;
984 if (page && zeroing && count < PAGE_SIZE)
985 clear_highpage(page);
988 if (cs->write && cs->pipebufs && page) {
989 return fuse_ref_page(cs, page, offset, count);
990 } else if (!cs->len) {
991 if (cs->move_pages && page &&
992 offset == 0 && count == PAGE_SIZE) {
993 err = fuse_try_move_page(cs, pagep);
997 err = fuse_copy_fill(cs);
1003 void *mapaddr = kmap_atomic(page);
1004 void *buf = mapaddr + offset;
1005 offset += fuse_copy_do(cs, &buf, &count);
1006 kunmap_atomic(mapaddr);
1008 offset += fuse_copy_do(cs, NULL, &count);
1010 if (page && !cs->write)
1011 flush_dcache_page(page);
1015 /* Copy pages in the request to/from userspace buffer */
1016 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1020 struct fuse_req *req = cs->req;
1022 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1024 unsigned offset = req->page_descs[i].offset;
1025 unsigned count = min(nbytes, req->page_descs[i].length);
1027 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1037 /* Copy a single argument in the request to/from userspace buffer */
1038 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1042 int err = fuse_copy_fill(cs);
1046 fuse_copy_do(cs, &val, &size);
1051 /* Copy request arguments to/from userspace buffer */
1052 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1053 unsigned argpages, struct fuse_arg *args,
1059 for (i = 0; !err && i < numargs; i++) {
1060 struct fuse_arg *arg = &args[i];
1061 if (i == numargs - 1 && argpages)
1062 err = fuse_copy_pages(cs, arg->size, zeroing);
1064 err = fuse_copy_one(cs, arg->value, arg->size);
1069 static int forget_pending(struct fuse_iqueue *fiq)
1071 return fiq->forget_list_head.next != NULL;
1074 static int request_pending(struct fuse_iqueue *fiq)
1076 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1077 forget_pending(fiq);
1081 * Transfer an interrupt request to userspace
1083 * Unlike other requests this is assembled on demand, without a need
1084 * to allocate a separate fuse_req structure.
1086 * Called with fiq->waitq.lock held, releases it
1088 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1089 struct fuse_copy_state *cs,
1090 size_t nbytes, struct fuse_req *req)
1091 __releases(fiq->waitq.lock)
1093 struct fuse_in_header ih;
1094 struct fuse_interrupt_in arg;
1095 unsigned reqsize = sizeof(ih) + sizeof(arg);
1098 list_del_init(&req->intr_entry);
1099 req->intr_unique = fuse_get_unique(fiq);
1100 memset(&ih, 0, sizeof(ih));
1101 memset(&arg, 0, sizeof(arg));
1103 ih.opcode = FUSE_INTERRUPT;
1104 ih.unique = req->intr_unique;
1105 arg.unique = req->in.h.unique;
1107 spin_unlock(&fiq->waitq.lock);
1108 if (nbytes < reqsize)
1111 err = fuse_copy_one(cs, &ih, sizeof(ih));
1113 err = fuse_copy_one(cs, &arg, sizeof(arg));
1114 fuse_copy_finish(cs);
1116 return err ? err : reqsize;
1119 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1123 struct fuse_forget_link *head = fiq->forget_list_head.next;
1124 struct fuse_forget_link **newhead = &head;
1127 for (count = 0; *newhead != NULL && count < max; count++)
1128 newhead = &(*newhead)->next;
1130 fiq->forget_list_head.next = *newhead;
1132 if (fiq->forget_list_head.next == NULL)
1133 fiq->forget_list_tail = &fiq->forget_list_head;
1141 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1142 struct fuse_copy_state *cs,
1144 __releases(fiq->waitq.lock)
1147 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1148 struct fuse_forget_in arg = {
1149 .nlookup = forget->forget_one.nlookup,
1151 struct fuse_in_header ih = {
1152 .opcode = FUSE_FORGET,
1153 .nodeid = forget->forget_one.nodeid,
1154 .unique = fuse_get_unique(fiq),
1155 .len = sizeof(ih) + sizeof(arg),
1158 spin_unlock(&fiq->waitq.lock);
1160 if (nbytes < ih.len)
1163 err = fuse_copy_one(cs, &ih, sizeof(ih));
1165 err = fuse_copy_one(cs, &arg, sizeof(arg));
1166 fuse_copy_finish(cs);
1174 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1175 struct fuse_copy_state *cs, size_t nbytes)
1176 __releases(fiq->waitq.lock)
1179 unsigned max_forgets;
1181 struct fuse_forget_link *head;
1182 struct fuse_batch_forget_in arg = { .count = 0 };
1183 struct fuse_in_header ih = {
1184 .opcode = FUSE_BATCH_FORGET,
1185 .unique = fuse_get_unique(fiq),
1186 .len = sizeof(ih) + sizeof(arg),
1189 if (nbytes < ih.len) {
1190 spin_unlock(&fiq->waitq.lock);
1194 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1195 head = dequeue_forget(fiq, max_forgets, &count);
1196 spin_unlock(&fiq->waitq.lock);
1199 ih.len += count * sizeof(struct fuse_forget_one);
1200 err = fuse_copy_one(cs, &ih, sizeof(ih));
1202 err = fuse_copy_one(cs, &arg, sizeof(arg));
1205 struct fuse_forget_link *forget = head;
1208 err = fuse_copy_one(cs, &forget->forget_one,
1209 sizeof(forget->forget_one));
1211 head = forget->next;
1215 fuse_copy_finish(cs);
1223 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1224 struct fuse_copy_state *cs,
1226 __releases(fiq->waitq.lock)
1228 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1229 return fuse_read_single_forget(fiq, cs, nbytes);
1231 return fuse_read_batch_forget(fiq, cs, nbytes);
1235 * Read a single request into the userspace filesystem's buffer. This
1236 * function waits until a request is available, then removes it from
1237 * the pending list and copies request data to userspace buffer. If
1238 * no reply is needed (FORGET) or request has been aborted or there
1239 * was an error during the copying then it's finished by calling
1240 * request_end(). Otherwise add it to the processing list, and set
1243 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1244 struct fuse_copy_state *cs, size_t nbytes)
1247 struct fuse_conn *fc = fud->fc;
1248 struct fuse_iqueue *fiq = &fc->iq;
1249 struct fuse_pqueue *fpq = &fud->pq;
1250 struct fuse_req *req;
1255 spin_lock(&fiq->waitq.lock);
1257 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1258 !request_pending(fiq))
1261 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1262 !fiq->connected || request_pending(fiq));
1267 if (!fiq->connected)
1270 if (!list_empty(&fiq->interrupts)) {
1271 req = list_entry(fiq->interrupts.next, struct fuse_req,
1273 return fuse_read_interrupt(fiq, cs, nbytes, req);
1276 if (forget_pending(fiq)) {
1277 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1278 return fuse_read_forget(fc, fiq, cs, nbytes);
1280 if (fiq->forget_batch <= -8)
1281 fiq->forget_batch = 16;
1284 req = list_entry(fiq->pending.next, struct fuse_req, list);
1285 clear_bit(FR_PENDING, &req->flags);
1286 list_del_init(&req->list);
1287 spin_unlock(&fiq->waitq.lock);
1290 reqsize = in->h.len;
1291 /* If request is too large, reply with an error and restart the read */
1292 if (nbytes < reqsize) {
1293 req->out.h.error = -EIO;
1294 /* SETXATTR is special, since it may contain too large data */
1295 if (in->h.opcode == FUSE_SETXATTR)
1296 req->out.h.error = -E2BIG;
1297 request_end(fc, req);
1300 spin_lock(&fpq->lock);
1301 list_add(&req->list, &fpq->io);
1302 spin_unlock(&fpq->lock);
1304 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1306 err = fuse_copy_args(cs, in->numargs, in->argpages,
1307 (struct fuse_arg *) in->args, 0);
1308 fuse_copy_finish(cs);
1309 spin_lock(&fpq->lock);
1310 clear_bit(FR_LOCKED, &req->flags);
1311 if (!fpq->connected) {
1316 req->out.h.error = -EIO;
1319 if (!test_bit(FR_ISREPLY, &req->flags)) {
1323 list_move_tail(&req->list, &fpq->processing);
1324 spin_unlock(&fpq->lock);
1325 set_bit(FR_SENT, &req->flags);
1326 /* matches barrier in request_wait_answer() */
1327 smp_mb__after_atomic();
1328 if (test_bit(FR_INTERRUPTED, &req->flags))
1329 queue_interrupt(fiq, req);
1334 if (!test_bit(FR_PRIVATE, &req->flags))
1335 list_del_init(&req->list);
1336 spin_unlock(&fpq->lock);
1337 request_end(fc, req);
1341 spin_unlock(&fiq->waitq.lock);
1345 static int fuse_dev_open(struct inode *inode, struct file *file)
1348 * The fuse device's file's private_data is used to hold
1349 * the fuse_conn(ection) when it is mounted, and is used to
1350 * keep track of whether the file has been mounted already.
1352 file->private_data = NULL;
1356 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1358 struct fuse_copy_state cs;
1359 struct file *file = iocb->ki_filp;
1360 struct fuse_dev *fud = fuse_get_dev(file);
1365 if (!iter_is_iovec(to))
1368 fuse_copy_init(&cs, 1, to);
1370 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1373 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1374 struct pipe_inode_info *pipe,
1375 size_t len, unsigned int flags)
1380 struct pipe_buffer *bufs;
1381 struct fuse_copy_state cs;
1382 struct fuse_dev *fud = fuse_get_dev(in);
1387 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1391 fuse_copy_init(&cs, 1, NULL);
1394 ret = fuse_dev_do_read(fud, in, &cs, len);
1401 if (!pipe->readers) {
1402 send_sig(SIGPIPE, current, 0);
1408 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1413 while (page_nr < cs.nr_segs) {
1414 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1415 struct pipe_buffer *buf = pipe->bufs + newbuf;
1417 buf->page = bufs[page_nr].page;
1418 buf->offset = bufs[page_nr].offset;
1419 buf->len = bufs[page_nr].len;
1421 * Need to be careful about this. Having buf->ops in module
1422 * code can Oops if the buffer persists after module unload.
1424 buf->ops = &nosteal_pipe_buf_ops;
1439 if (waitqueue_active(&pipe->wait))
1440 wake_up_interruptible(&pipe->wait);
1441 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1445 for (; page_nr < cs.nr_segs; page_nr++)
1446 page_cache_release(bufs[page_nr].page);
1452 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1453 struct fuse_copy_state *cs)
1455 struct fuse_notify_poll_wakeup_out outarg;
1458 if (size != sizeof(outarg))
1461 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1465 fuse_copy_finish(cs);
1466 return fuse_notify_poll_wakeup(fc, &outarg);
1469 fuse_copy_finish(cs);
1473 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1474 struct fuse_copy_state *cs)
1476 struct fuse_notify_inval_inode_out outarg;
1479 if (size != sizeof(outarg))
1482 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1485 fuse_copy_finish(cs);
1487 down_read(&fc->killsb);
1490 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1491 outarg.off, outarg.len);
1493 up_read(&fc->killsb);
1497 fuse_copy_finish(cs);
1501 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1502 struct fuse_copy_state *cs)
1504 struct fuse_notify_inval_entry_out outarg;
1509 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1514 if (size < sizeof(outarg))
1517 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1521 err = -ENAMETOOLONG;
1522 if (outarg.namelen > FUSE_NAME_MAX)
1526 if (size != sizeof(outarg) + outarg.namelen + 1)
1530 name.len = outarg.namelen;
1531 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1534 fuse_copy_finish(cs);
1535 buf[outarg.namelen] = 0;
1536 name.hash = full_name_hash(name.name, name.len);
1538 down_read(&fc->killsb);
1541 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1542 up_read(&fc->killsb);
1548 fuse_copy_finish(cs);
1552 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1553 struct fuse_copy_state *cs)
1555 struct fuse_notify_delete_out outarg;
1560 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1565 if (size < sizeof(outarg))
1568 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1572 err = -ENAMETOOLONG;
1573 if (outarg.namelen > FUSE_NAME_MAX)
1577 if (size != sizeof(outarg) + outarg.namelen + 1)
1581 name.len = outarg.namelen;
1582 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1585 fuse_copy_finish(cs);
1586 buf[outarg.namelen] = 0;
1587 name.hash = full_name_hash(name.name, name.len);
1589 down_read(&fc->killsb);
1592 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1593 outarg.child, &name);
1594 up_read(&fc->killsb);
1600 fuse_copy_finish(cs);
1604 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1605 struct fuse_copy_state *cs)
1607 struct fuse_notify_store_out outarg;
1608 struct inode *inode;
1609 struct address_space *mapping;
1613 unsigned int offset;
1619 if (size < sizeof(outarg))
1622 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1627 if (size - sizeof(outarg) != outarg.size)
1630 nodeid = outarg.nodeid;
1632 down_read(&fc->killsb);
1638 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1642 mapping = inode->i_mapping;
1643 index = outarg.offset >> PAGE_CACHE_SHIFT;
1644 offset = outarg.offset & ~PAGE_CACHE_MASK;
1645 file_size = i_size_read(inode);
1646 end = outarg.offset + outarg.size;
1647 if (end > file_size) {
1649 fuse_write_update_size(inode, file_size);
1655 unsigned int this_num;
1658 page = find_or_create_page(mapping, index,
1659 mapping_gfp_mask(mapping));
1663 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1664 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1665 if (!err && offset == 0 &&
1666 (this_num == PAGE_CACHE_SIZE || file_size == end))
1667 SetPageUptodate(page);
1669 page_cache_release(page);
1684 up_read(&fc->killsb);
1686 fuse_copy_finish(cs);
1690 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1692 release_pages(req->pages, req->num_pages, false);
1695 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1696 struct fuse_notify_retrieve_out *outarg)
1699 struct address_space *mapping = inode->i_mapping;
1700 struct fuse_req *req;
1704 unsigned int offset;
1705 size_t total_len = 0;
1708 offset = outarg->offset & ~PAGE_CACHE_MASK;
1709 file_size = i_size_read(inode);
1712 if (outarg->offset > file_size)
1714 else if (outarg->offset + num > file_size)
1715 num = file_size - outarg->offset;
1717 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1718 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1720 req = fuse_get_req(fc, num_pages);
1722 return PTR_ERR(req);
1724 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1725 req->in.h.nodeid = outarg->nodeid;
1726 req->in.numargs = 2;
1727 req->in.argpages = 1;
1728 req->page_descs[0].offset = offset;
1729 req->end = fuse_retrieve_end;
1731 index = outarg->offset >> PAGE_CACHE_SHIFT;
1733 while (num && req->num_pages < num_pages) {
1735 unsigned int this_num;
1737 page = find_get_page(mapping, index);
1741 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1742 req->pages[req->num_pages] = page;
1743 req->page_descs[req->num_pages].length = this_num;
1748 total_len += this_num;
1751 req->misc.retrieve_in.offset = outarg->offset;
1752 req->misc.retrieve_in.size = total_len;
1753 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1754 req->in.args[0].value = &req->misc.retrieve_in;
1755 req->in.args[1].size = total_len;
1757 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1759 fuse_retrieve_end(fc, req);
1764 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1765 struct fuse_copy_state *cs)
1767 struct fuse_notify_retrieve_out outarg;
1768 struct inode *inode;
1772 if (size != sizeof(outarg))
1775 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1779 fuse_copy_finish(cs);
1781 down_read(&fc->killsb);
1784 u64 nodeid = outarg.nodeid;
1786 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1788 err = fuse_retrieve(fc, inode, &outarg);
1792 up_read(&fc->killsb);
1797 fuse_copy_finish(cs);
1801 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1802 unsigned int size, struct fuse_copy_state *cs)
1804 /* Don't try to move pages (yet) */
1808 case FUSE_NOTIFY_POLL:
1809 return fuse_notify_poll(fc, size, cs);
1811 case FUSE_NOTIFY_INVAL_INODE:
1812 return fuse_notify_inval_inode(fc, size, cs);
1814 case FUSE_NOTIFY_INVAL_ENTRY:
1815 return fuse_notify_inval_entry(fc, size, cs);
1817 case FUSE_NOTIFY_STORE:
1818 return fuse_notify_store(fc, size, cs);
1820 case FUSE_NOTIFY_RETRIEVE:
1821 return fuse_notify_retrieve(fc, size, cs);
1823 case FUSE_NOTIFY_DELETE:
1824 return fuse_notify_delete(fc, size, cs);
1827 fuse_copy_finish(cs);
1832 /* Look up request on processing list by unique ID */
1833 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1835 struct fuse_req *req;
1837 list_for_each_entry(req, &fpq->processing, list) {
1838 if (req->in.h.unique == unique || req->intr_unique == unique)
1844 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1847 unsigned reqsize = sizeof(struct fuse_out_header);
1850 return nbytes != reqsize ? -EINVAL : 0;
1852 reqsize += len_args(out->numargs, out->args);
1854 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1856 else if (reqsize > nbytes) {
1857 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1858 unsigned diffsize = reqsize - nbytes;
1859 if (diffsize > lastarg->size)
1861 lastarg->size -= diffsize;
1863 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1868 * Write a single reply to a request. First the header is copied from
1869 * the write buffer. The request is then searched on the processing
1870 * list by the unique ID found in the header. If found, then remove
1871 * it from the list and copy the rest of the buffer to the request.
1872 * The request is finished by calling request_end()
1874 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1875 struct fuse_copy_state *cs, size_t nbytes)
1878 struct fuse_conn *fc = fud->fc;
1879 struct fuse_pqueue *fpq = &fud->pq;
1880 struct fuse_req *req;
1881 struct fuse_out_header oh;
1883 if (nbytes < sizeof(struct fuse_out_header))
1886 err = fuse_copy_one(cs, &oh, sizeof(oh));
1891 if (oh.len != nbytes)
1895 * Zero oh.unique indicates unsolicited notification message
1896 * and error contains notification code.
1899 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1900 return err ? err : nbytes;
1904 if (oh.error <= -1000 || oh.error > 0)
1907 spin_lock(&fpq->lock);
1909 if (!fpq->connected)
1912 req = request_find(fpq, oh.unique);
1916 /* Is it an interrupt reply? */
1917 if (req->intr_unique == oh.unique) {
1918 spin_unlock(&fpq->lock);
1921 if (nbytes != sizeof(struct fuse_out_header))
1924 if (oh.error == -ENOSYS)
1925 fc->no_interrupt = 1;
1926 else if (oh.error == -EAGAIN)
1927 queue_interrupt(&fc->iq, req);
1929 fuse_copy_finish(cs);
1933 clear_bit(FR_SENT, &req->flags);
1934 list_move(&req->list, &fpq->io);
1936 set_bit(FR_LOCKED, &req->flags);
1937 spin_unlock(&fpq->lock);
1939 if (!req->out.page_replace)
1942 err = copy_out_args(cs, &req->out, nbytes);
1943 if (req->in.h.opcode == FUSE_CANONICAL_PATH) {
1944 req->out.h.error = kern_path((char *)req->out.args[0].value, 0,
1945 req->canonical_path);
1947 fuse_copy_finish(cs);
1949 spin_lock(&fpq->lock);
1950 clear_bit(FR_LOCKED, &req->flags);
1951 if (!fpq->connected)
1954 req->out.h.error = -EIO;
1955 if (!test_bit(FR_PRIVATE, &req->flags))
1956 list_del_init(&req->list);
1957 spin_unlock(&fpq->lock);
1959 request_end(fc, req);
1961 return err ? err : nbytes;
1964 spin_unlock(&fpq->lock);
1966 fuse_copy_finish(cs);
1970 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1972 struct fuse_copy_state cs;
1973 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1978 if (!iter_is_iovec(from))
1981 fuse_copy_init(&cs, 0, from);
1983 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
1986 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1987 struct file *out, loff_t *ppos,
1988 size_t len, unsigned int flags)
1992 struct pipe_buffer *bufs;
1993 struct fuse_copy_state cs;
1994 struct fuse_dev *fud;
1998 fud = fuse_get_dev(out);
2002 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2009 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2010 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2020 struct pipe_buffer *ibuf;
2021 struct pipe_buffer *obuf;
2023 BUG_ON(nbuf >= pipe->buffers);
2024 BUG_ON(!pipe->nrbufs);
2025 ibuf = &pipe->bufs[pipe->curbuf];
2028 if (rem >= ibuf->len) {
2031 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2034 ibuf->ops->get(pipe, ibuf);
2036 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2038 ibuf->offset += obuf->len;
2039 ibuf->len -= obuf->len;
2046 fuse_copy_init(&cs, 0, NULL);
2051 if (flags & SPLICE_F_MOVE)
2054 ret = fuse_dev_do_write(fud, &cs, len);
2056 for (idx = 0; idx < nbuf; idx++) {
2057 struct pipe_buffer *buf = &bufs[idx];
2058 buf->ops->release(pipe, buf);
2065 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2067 unsigned mask = POLLOUT | POLLWRNORM;
2068 struct fuse_iqueue *fiq;
2069 struct fuse_dev *fud = fuse_get_dev(file);
2075 poll_wait(file, &fiq->waitq, wait);
2077 spin_lock(&fiq->waitq.lock);
2078 if (!fiq->connected)
2080 else if (request_pending(fiq))
2081 mask |= POLLIN | POLLRDNORM;
2082 spin_unlock(&fiq->waitq.lock);
2088 * Abort all requests on the given list (pending or processing)
2090 * This function releases and reacquires fc->lock
2092 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2094 while (!list_empty(head)) {
2095 struct fuse_req *req;
2096 req = list_entry(head->next, struct fuse_req, list);
2097 req->out.h.error = -ECONNABORTED;
2098 clear_bit(FR_SENT, &req->flags);
2099 list_del_init(&req->list);
2100 request_end(fc, req);
2104 static void end_polls(struct fuse_conn *fc)
2108 p = rb_first(&fc->polled_files);
2111 struct fuse_file *ff;
2112 ff = rb_entry(p, struct fuse_file, polled_node);
2113 wake_up_interruptible_all(&ff->poll_wait);
2120 * Abort all requests.
2122 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2125 * The same effect is usually achievable through killing the filesystem daemon
2126 * and all users of the filesystem. The exception is the combination of an
2127 * asynchronous request and the tricky deadlock (see
2128 * Documentation/filesystems/fuse.txt).
2130 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2131 * requests, they should be finished off immediately. Locked requests will be
2132 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2133 * requests. It is possible that some request will finish before we can. This
2134 * is OK, the request will in that case be removed from the list before we touch
2137 void fuse_abort_conn(struct fuse_conn *fc)
2139 struct fuse_iqueue *fiq = &fc->iq;
2141 spin_lock(&fc->lock);
2142 if (fc->connected) {
2143 struct fuse_dev *fud;
2144 struct fuse_req *req, *next;
2150 fuse_set_initialized(fc);
2151 list_for_each_entry(fud, &fc->devices, entry) {
2152 struct fuse_pqueue *fpq = &fud->pq;
2154 spin_lock(&fpq->lock);
2156 list_for_each_entry_safe(req, next, &fpq->io, list) {
2157 req->out.h.error = -ECONNABORTED;
2158 spin_lock(&req->waitq.lock);
2159 set_bit(FR_ABORTED, &req->flags);
2160 if (!test_bit(FR_LOCKED, &req->flags)) {
2161 set_bit(FR_PRIVATE, &req->flags);
2162 list_move(&req->list, &to_end1);
2164 spin_unlock(&req->waitq.lock);
2166 list_splice_init(&fpq->processing, &to_end2);
2167 spin_unlock(&fpq->lock);
2169 fc->max_background = UINT_MAX;
2172 spin_lock(&fiq->waitq.lock);
2174 list_splice_init(&fiq->pending, &to_end2);
2175 list_for_each_entry(req, &to_end2, list)
2176 clear_bit(FR_PENDING, &req->flags);
2177 while (forget_pending(fiq))
2178 kfree(dequeue_forget(fiq, 1, NULL));
2179 wake_up_all_locked(&fiq->waitq);
2180 spin_unlock(&fiq->waitq.lock);
2181 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2183 wake_up_all(&fc->blocked_waitq);
2184 spin_unlock(&fc->lock);
2186 while (!list_empty(&to_end1)) {
2187 req = list_first_entry(&to_end1, struct fuse_req, list);
2188 __fuse_get_request(req);
2189 list_del_init(&req->list);
2190 request_end(fc, req);
2192 end_requests(fc, &to_end2);
2194 spin_unlock(&fc->lock);
2197 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2199 int fuse_dev_release(struct inode *inode, struct file *file)
2201 struct fuse_dev *fud = fuse_get_dev(file);
2204 struct fuse_conn *fc = fud->fc;
2205 struct fuse_pqueue *fpq = &fud->pq;
2207 WARN_ON(!list_empty(&fpq->io));
2208 end_requests(fc, &fpq->processing);
2209 /* Are we the last open device? */
2210 if (atomic_dec_and_test(&fc->dev_count)) {
2211 WARN_ON(fc->iq.fasync != NULL);
2212 fuse_abort_conn(fc);
2218 EXPORT_SYMBOL_GPL(fuse_dev_release);
2220 static int fuse_dev_fasync(int fd, struct file *file, int on)
2222 struct fuse_dev *fud = fuse_get_dev(file);
2227 /* No locking - fasync_helper does its own locking */
2228 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2231 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2233 struct fuse_dev *fud;
2235 if (new->private_data)
2238 fud = fuse_dev_alloc(fc);
2242 new->private_data = fud;
2243 atomic_inc(&fc->dev_count);
2248 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2253 if (cmd == FUSE_DEV_IOC_CLONE) {
2257 if (!get_user(oldfd, (__u32 __user *) arg)) {
2258 struct file *old = fget(oldfd);
2262 struct fuse_dev *fud = NULL;
2265 * Check against file->f_op because CUSE
2266 * uses the same ioctl handler.
2268 if (old->f_op == file->f_op &&
2269 old->f_cred->user_ns == file->f_cred->user_ns)
2270 fud = fuse_get_dev(old);
2273 mutex_lock(&fuse_mutex);
2274 err = fuse_device_clone(fud->fc, file);
2275 mutex_unlock(&fuse_mutex);
2284 const struct file_operations fuse_dev_operations = {
2285 .owner = THIS_MODULE,
2286 .open = fuse_dev_open,
2287 .llseek = no_llseek,
2288 .read_iter = fuse_dev_read,
2289 .splice_read = fuse_dev_splice_read,
2290 .write_iter = fuse_dev_write,
2291 .splice_write = fuse_dev_splice_write,
2292 .poll = fuse_dev_poll,
2293 .release = fuse_dev_release,
2294 .fasync = fuse_dev_fasync,
2295 .unlocked_ioctl = fuse_dev_ioctl,
2296 .compat_ioctl = fuse_dev_ioctl,
2298 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2300 static struct miscdevice fuse_miscdevice = {
2301 .minor = FUSE_MINOR,
2303 .fops = &fuse_dev_operations,
2306 int __init fuse_dev_init(void)
2309 fuse_req_cachep = kmem_cache_create("fuse_request",
2310 sizeof(struct fuse_req),
2312 if (!fuse_req_cachep)
2315 err = misc_register(&fuse_miscdevice);
2317 goto out_cache_clean;
2322 kmem_cache_destroy(fuse_req_cachep);
2327 void fuse_dev_cleanup(void)
2329 misc_deregister(&fuse_miscdevice);
2330 kmem_cache_destroy(fuse_req_cachep);