2 FUSE: Filesystem in Userspace
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/sched/signal.h>
15 #include <linux/uio.h>
16 #include <linux/miscdevice.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/sched.h>
25 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
26 MODULE_ALIAS("devname:fuse");
28 /* Ordinary requests have even IDs, while interrupts IDs are odd */
29 #define FUSE_INT_REQ_BIT (1ULL << 0)
30 #define FUSE_REQ_ID_STEP (1ULL << 1)
32 static struct kmem_cache *fuse_req_cachep;
34 static void end_requests(struct list_head *head);
36 static struct fuse_dev *fuse_get_dev(struct file *file)
39 * Lockless access is OK, because file->private data is set
40 * once during mount and is valid until the file is released.
42 return READ_ONCE(file->private_data);
45 static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
47 INIT_LIST_HEAD(&req->list);
48 INIT_LIST_HEAD(&req->intr_entry);
49 init_waitqueue_head(&req->waitq);
50 refcount_set(&req->count, 1);
51 __set_bit(FR_PENDING, &req->flags);
55 static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags)
57 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
59 fuse_request_init(fm, req);
64 static void fuse_request_free(struct fuse_req *req)
66 kmem_cache_free(fuse_req_cachep, req);
69 static void __fuse_get_request(struct fuse_req *req)
71 refcount_inc(&req->count);
74 /* Must be called with > 1 refcount */
75 static void __fuse_put_request(struct fuse_req *req)
77 refcount_dec(&req->count);
80 void fuse_set_initialized(struct fuse_conn *fc)
82 /* Make sure stores before this are seen on another CPU */
87 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
89 return !fc->initialized || (for_background && fc->blocked);
92 static void fuse_drop_waiting(struct fuse_conn *fc)
95 * lockess check of fc->connected is okay, because atomic_dec_and_test()
96 * provides a memory barrier matched with the one in fuse_wait_aborted()
97 * to ensure no wake-up is missed.
99 if (atomic_dec_and_test(&fc->num_waiting) &&
100 !READ_ONCE(fc->connected)) {
101 /* wake up aborters */
102 wake_up_all(&fc->blocked_waitq);
106 static void fuse_put_request(struct fuse_req *req);
108 static struct fuse_req *fuse_get_req(struct fuse_mount *fm, bool for_background)
110 struct fuse_conn *fc = fm->fc;
111 struct fuse_req *req;
113 atomic_inc(&fc->num_waiting);
115 if (fuse_block_alloc(fc, for_background)) {
117 if (wait_event_killable_exclusive(fc->blocked_waitq,
118 !fuse_block_alloc(fc, for_background)))
121 /* Matches smp_wmb() in fuse_set_initialized() */
132 req = fuse_request_alloc(fm, GFP_KERNEL);
136 wake_up(&fc->blocked_waitq);
140 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
141 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
142 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
144 __set_bit(FR_WAITING, &req->flags);
146 __set_bit(FR_BACKGROUND, &req->flags);
148 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
149 req->in.h.gid == ((gid_t)-1))) {
150 fuse_put_request(req);
151 return ERR_PTR(-EOVERFLOW);
156 fuse_drop_waiting(fc);
160 static void fuse_put_request(struct fuse_req *req)
162 struct fuse_conn *fc = req->fm->fc;
164 if (refcount_dec_and_test(&req->count)) {
165 if (test_bit(FR_BACKGROUND, &req->flags)) {
167 * We get here in the unlikely case that a background
168 * request was allocated but not sent
170 spin_lock(&fc->bg_lock);
172 wake_up(&fc->blocked_waitq);
173 spin_unlock(&fc->bg_lock);
176 if (test_bit(FR_WAITING, &req->flags)) {
177 __clear_bit(FR_WAITING, &req->flags);
178 fuse_drop_waiting(fc);
181 fuse_request_free(req);
185 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
190 for (i = 0; i < numargs; i++)
191 nbytes += args[i].size;
195 EXPORT_SYMBOL_GPL(fuse_len_args);
197 u64 fuse_get_unique(struct fuse_iqueue *fiq)
199 fiq->reqctr += FUSE_REQ_ID_STEP;
202 EXPORT_SYMBOL_GPL(fuse_get_unique);
204 static unsigned int fuse_req_hash(u64 unique)
206 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
210 * A new request is available, wake fiq->waitq
212 static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)
213 __releases(fiq->lock)
215 wake_up(&fiq->waitq);
216 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
217 spin_unlock(&fiq->lock);
220 const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
221 .wake_forget_and_unlock = fuse_dev_wake_and_unlock,
222 .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock,
223 .wake_pending_and_unlock = fuse_dev_wake_and_unlock,
225 EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
227 static void queue_request_and_unlock(struct fuse_iqueue *fiq,
228 struct fuse_req *req)
229 __releases(fiq->lock)
231 req->in.h.len = sizeof(struct fuse_in_header) +
232 fuse_len_args(req->args->in_numargs,
233 (struct fuse_arg *) req->args->in_args);
234 list_add_tail(&req->list, &fiq->pending);
235 fiq->ops->wake_pending_and_unlock(fiq);
238 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
239 u64 nodeid, u64 nlookup)
241 struct fuse_iqueue *fiq = &fc->iq;
243 forget->forget_one.nodeid = nodeid;
244 forget->forget_one.nlookup = nlookup;
246 spin_lock(&fiq->lock);
247 if (fiq->connected) {
248 fiq->forget_list_tail->next = forget;
249 fiq->forget_list_tail = forget;
250 fiq->ops->wake_forget_and_unlock(fiq);
253 spin_unlock(&fiq->lock);
257 static void flush_bg_queue(struct fuse_conn *fc)
259 struct fuse_iqueue *fiq = &fc->iq;
261 while (fc->active_background < fc->max_background &&
262 !list_empty(&fc->bg_queue)) {
263 struct fuse_req *req;
265 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
266 list_del(&req->list);
267 fc->active_background++;
268 spin_lock(&fiq->lock);
269 req->in.h.unique = fuse_get_unique(fiq);
270 queue_request_and_unlock(fiq, req);
275 * This function is called when a request is finished. Either a reply
276 * has arrived or it was aborted (and not yet sent) or some error
277 * occurred during communication with userspace, or the device file
278 * was closed. The requester thread is woken up (if still waiting),
279 * the 'end' callback is called if given, else the reference to the
280 * request is released
282 void fuse_request_end(struct fuse_req *req)
284 struct fuse_mount *fm = req->fm;
285 struct fuse_conn *fc = fm->fc;
286 struct fuse_iqueue *fiq = &fc->iq;
288 if (test_and_set_bit(FR_FINISHED, &req->flags))
292 * test_and_set_bit() implies smp_mb() between bit
293 * changing and below FR_INTERRUPTED check. Pairs with
294 * smp_mb() from queue_interrupt().
296 if (test_bit(FR_INTERRUPTED, &req->flags)) {
297 spin_lock(&fiq->lock);
298 list_del_init(&req->intr_entry);
299 spin_unlock(&fiq->lock);
301 WARN_ON(test_bit(FR_PENDING, &req->flags));
302 WARN_ON(test_bit(FR_SENT, &req->flags));
303 if (test_bit(FR_BACKGROUND, &req->flags)) {
304 spin_lock(&fc->bg_lock);
305 clear_bit(FR_BACKGROUND, &req->flags);
306 if (fc->num_background == fc->max_background) {
308 wake_up(&fc->blocked_waitq);
309 } else if (!fc->blocked) {
311 * Wake up next waiter, if any. It's okay to use
312 * waitqueue_active(), as we've already synced up
313 * fc->blocked with waiters with the wake_up() call
316 if (waitqueue_active(&fc->blocked_waitq))
317 wake_up(&fc->blocked_waitq);
320 fc->num_background--;
321 fc->active_background--;
323 spin_unlock(&fc->bg_lock);
325 /* Wake up waiter sleeping in request_wait_answer() */
326 wake_up(&req->waitq);
329 if (test_bit(FR_ASYNC, &req->flags))
330 req->args->end(fm, req->args, req->out.h.error);
332 fuse_put_request(req);
334 EXPORT_SYMBOL_GPL(fuse_request_end);
336 static int queue_interrupt(struct fuse_req *req)
338 struct fuse_iqueue *fiq = &req->fm->fc->iq;
340 spin_lock(&fiq->lock);
341 /* Check for we've sent request to interrupt this req */
342 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {
343 spin_unlock(&fiq->lock);
347 if (list_empty(&req->intr_entry)) {
348 list_add_tail(&req->intr_entry, &fiq->interrupts);
350 * Pairs with smp_mb() implied by test_and_set_bit()
351 * from fuse_request_end().
354 if (test_bit(FR_FINISHED, &req->flags)) {
355 list_del_init(&req->intr_entry);
356 spin_unlock(&fiq->lock);
359 fiq->ops->wake_interrupt_and_unlock(fiq);
361 spin_unlock(&fiq->lock);
366 static void request_wait_answer(struct fuse_req *req)
368 struct fuse_conn *fc = req->fm->fc;
369 struct fuse_iqueue *fiq = &fc->iq;
372 if (!fc->no_interrupt) {
373 /* Any signal may interrupt this */
374 err = wait_event_interruptible(req->waitq,
375 test_bit(FR_FINISHED, &req->flags));
379 set_bit(FR_INTERRUPTED, &req->flags);
380 /* matches barrier in fuse_dev_do_read() */
381 smp_mb__after_atomic();
382 if (test_bit(FR_SENT, &req->flags))
383 queue_interrupt(req);
386 if (!test_bit(FR_FORCE, &req->flags)) {
387 /* Only fatal signals may interrupt this */
388 err = wait_event_killable(req->waitq,
389 test_bit(FR_FINISHED, &req->flags));
393 spin_lock(&fiq->lock);
394 /* Request is not yet in userspace, bail out */
395 if (test_bit(FR_PENDING, &req->flags)) {
396 list_del(&req->list);
397 spin_unlock(&fiq->lock);
398 __fuse_put_request(req);
399 req->out.h.error = -EINTR;
402 spin_unlock(&fiq->lock);
406 * Either request is already in userspace, or it was forced.
409 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
412 static void __fuse_request_send(struct fuse_req *req)
414 struct fuse_iqueue *fiq = &req->fm->fc->iq;
416 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
417 spin_lock(&fiq->lock);
418 if (!fiq->connected) {
419 spin_unlock(&fiq->lock);
420 req->out.h.error = -ENOTCONN;
422 req->in.h.unique = fuse_get_unique(fiq);
423 /* acquire extra reference, since request is still needed
424 after fuse_request_end() */
425 __fuse_get_request(req);
426 queue_request_and_unlock(fiq, req);
428 request_wait_answer(req);
429 /* Pairs with smp_wmb() in fuse_request_end() */
434 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
436 if (fc->minor < 4 && args->opcode == FUSE_STATFS)
437 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
440 switch (args->opcode) {
447 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
451 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
455 if (fc->minor < 12) {
456 switch (args->opcode) {
458 args->in_args[0].size = sizeof(struct fuse_open_in);
461 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
467 static void fuse_force_creds(struct fuse_req *req)
469 struct fuse_conn *fc = req->fm->fc;
471 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
472 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
473 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
476 static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
478 req->in.h.opcode = args->opcode;
479 req->in.h.nodeid = args->nodeid;
482 req->in.h.total_extlen = args->in_args[args->ext_idx].size / 8;
484 __set_bit(FR_ASYNC, &req->flags);
487 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args)
489 struct fuse_conn *fc = fm->fc;
490 struct fuse_req *req;
494 atomic_inc(&fc->num_waiting);
495 req = fuse_request_alloc(fm, GFP_KERNEL | __GFP_NOFAIL);
498 fuse_force_creds(req);
500 __set_bit(FR_WAITING, &req->flags);
501 __set_bit(FR_FORCE, &req->flags);
503 WARN_ON(args->nocreds);
504 req = fuse_get_req(fm, false);
509 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
510 fuse_adjust_compat(fc, args);
511 fuse_args_to_req(req, args);
514 __set_bit(FR_ISREPLY, &req->flags);
515 __fuse_request_send(req);
516 ret = req->out.h.error;
517 if (!ret && args->out_argvar) {
518 BUG_ON(args->out_numargs == 0);
519 ret = args->out_args[args->out_numargs - 1].size;
521 fuse_put_request(req);
526 static bool fuse_request_queue_background(struct fuse_req *req)
528 struct fuse_mount *fm = req->fm;
529 struct fuse_conn *fc = fm->fc;
532 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
533 if (!test_bit(FR_WAITING, &req->flags)) {
534 __set_bit(FR_WAITING, &req->flags);
535 atomic_inc(&fc->num_waiting);
537 __set_bit(FR_ISREPLY, &req->flags);
538 spin_lock(&fc->bg_lock);
539 if (likely(fc->connected)) {
540 fc->num_background++;
541 if (fc->num_background == fc->max_background)
543 list_add_tail(&req->list, &fc->bg_queue);
547 spin_unlock(&fc->bg_lock);
552 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
555 struct fuse_req *req;
558 WARN_ON(!args->nocreds);
559 req = fuse_request_alloc(fm, gfp_flags);
562 __set_bit(FR_BACKGROUND, &req->flags);
564 WARN_ON(args->nocreds);
565 req = fuse_get_req(fm, true);
570 fuse_args_to_req(req, args);
572 if (!fuse_request_queue_background(req)) {
573 fuse_put_request(req);
579 EXPORT_SYMBOL_GPL(fuse_simple_background);
581 static int fuse_simple_notify_reply(struct fuse_mount *fm,
582 struct fuse_args *args, u64 unique)
584 struct fuse_req *req;
585 struct fuse_iqueue *fiq = &fm->fc->iq;
588 req = fuse_get_req(fm, false);
592 __clear_bit(FR_ISREPLY, &req->flags);
593 req->in.h.unique = unique;
595 fuse_args_to_req(req, args);
597 spin_lock(&fiq->lock);
598 if (fiq->connected) {
599 queue_request_and_unlock(fiq, req);
602 spin_unlock(&fiq->lock);
603 fuse_put_request(req);
610 * Lock the request. Up to the next unlock_request() there mustn't be
611 * anything that could cause a page-fault. If the request was already
614 static int lock_request(struct fuse_req *req)
618 spin_lock(&req->waitq.lock);
619 if (test_bit(FR_ABORTED, &req->flags))
622 set_bit(FR_LOCKED, &req->flags);
623 spin_unlock(&req->waitq.lock);
629 * Unlock request. If it was aborted while locked, caller is responsible
630 * for unlocking and ending the request.
632 static int unlock_request(struct fuse_req *req)
636 spin_lock(&req->waitq.lock);
637 if (test_bit(FR_ABORTED, &req->flags))
640 clear_bit(FR_LOCKED, &req->flags);
641 spin_unlock(&req->waitq.lock);
646 struct fuse_copy_state {
648 struct fuse_req *req;
649 struct iov_iter *iter;
650 struct pipe_buffer *pipebufs;
651 struct pipe_buffer *currbuf;
652 struct pipe_inode_info *pipe;
653 unsigned long nr_segs;
657 unsigned move_pages:1;
660 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
661 struct iov_iter *iter)
663 memset(cs, 0, sizeof(*cs));
668 /* Unmap and put previous page of userspace buffer */
669 static void fuse_copy_finish(struct fuse_copy_state *cs)
672 struct pipe_buffer *buf = cs->currbuf;
675 buf->len = PAGE_SIZE - cs->len;
679 flush_dcache_page(cs->pg);
680 set_page_dirty_lock(cs->pg);
688 * Get another pagefull of userspace buffer, and map it to kernel
689 * address space, and lock request
691 static int fuse_copy_fill(struct fuse_copy_state *cs)
696 err = unlock_request(cs->req);
700 fuse_copy_finish(cs);
702 struct pipe_buffer *buf = cs->pipebufs;
705 err = pipe_buf_confirm(cs->pipe, buf);
709 BUG_ON(!cs->nr_segs);
712 cs->offset = buf->offset;
717 if (cs->nr_segs >= cs->pipe->max_usage)
720 page = alloc_page(GFP_HIGHUSER);
737 err = iov_iter_get_pages2(cs->iter, &page, PAGE_SIZE, 1, &off);
746 return lock_request(cs->req);
749 /* Do as much copy to/from userspace buffer as we can */
750 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
752 unsigned ncpy = min(*size, cs->len);
754 void *pgaddr = kmap_local_page(cs->pg);
755 void *buf = pgaddr + cs->offset;
758 memcpy(buf, *val, ncpy);
760 memcpy(*val, buf, ncpy);
762 kunmap_local(pgaddr);
771 static int fuse_check_folio(struct folio *folio)
773 if (folio_mapped(folio) ||
774 folio->mapping != NULL ||
775 (folio->flags & PAGE_FLAGS_CHECK_AT_PREP &
783 LRU_GEN_MASK | LRU_REFS_MASK))) {
784 dump_page(&folio->page, "fuse: trying to steal weird page");
790 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
793 struct folio *oldfolio = page_folio(*pagep);
794 struct folio *newfolio;
795 struct pipe_buffer *buf = cs->pipebufs;
798 err = unlock_request(cs->req);
802 fuse_copy_finish(cs);
804 err = pipe_buf_confirm(cs->pipe, buf);
808 BUG_ON(!cs->nr_segs);
814 if (cs->len != PAGE_SIZE)
817 if (!pipe_buf_try_steal(cs->pipe, buf))
820 newfolio = page_folio(buf->page);
822 folio_clear_uptodate(newfolio);
823 folio_clear_mappedtodisk(newfolio);
825 if (fuse_check_folio(newfolio) != 0)
826 goto out_fallback_unlock;
829 * This is a new and locked page, it shouldn't be mapped or
830 * have any special flags on it
832 if (WARN_ON(folio_mapped(oldfolio)))
833 goto out_fallback_unlock;
834 if (WARN_ON(folio_has_private(oldfolio)))
835 goto out_fallback_unlock;
836 if (WARN_ON(folio_test_dirty(oldfolio) ||
837 folio_test_writeback(oldfolio)))
838 goto out_fallback_unlock;
839 if (WARN_ON(folio_test_mlocked(oldfolio)))
840 goto out_fallback_unlock;
842 replace_page_cache_folio(oldfolio, newfolio);
846 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
847 folio_add_lru(newfolio);
850 * Release while we have extra ref on stolen page. Otherwise
851 * anon_pipe_buf_release() might think the page can be reused.
853 pipe_buf_release(cs->pipe, buf);
856 spin_lock(&cs->req->waitq.lock);
857 if (test_bit(FR_ABORTED, &cs->req->flags))
860 *pagep = &newfolio->page;
861 spin_unlock(&cs->req->waitq.lock);
864 folio_unlock(newfolio);
869 folio_unlock(oldfolio);
870 /* Drop ref for ap->pages[] array */
876 /* Drop ref obtained in this function */
881 folio_unlock(newfolio);
884 cs->offset = buf->offset;
886 err = lock_request(cs->req);
893 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
894 unsigned offset, unsigned count)
896 struct pipe_buffer *buf;
899 if (cs->nr_segs >= cs->pipe->max_usage)
903 err = unlock_request(cs->req);
909 fuse_copy_finish(cs);
913 buf->offset = offset;
924 * Copy a page in the request to/from the userspace buffer. Must be
927 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
928 unsigned offset, unsigned count, int zeroing)
931 struct page *page = *pagep;
933 if (page && zeroing && count < PAGE_SIZE)
934 clear_highpage(page);
937 if (cs->write && cs->pipebufs && page) {
939 * Can't control lifetime of pipe buffers, so always
942 if (cs->req->args->user_pages) {
943 err = fuse_copy_fill(cs);
947 return fuse_ref_page(cs, page, offset, count);
949 } else if (!cs->len) {
950 if (cs->move_pages && page &&
951 offset == 0 && count == PAGE_SIZE) {
952 err = fuse_try_move_page(cs, pagep);
956 err = fuse_copy_fill(cs);
962 void *mapaddr = kmap_local_page(page);
963 void *buf = mapaddr + offset;
964 offset += fuse_copy_do(cs, &buf, &count);
965 kunmap_local(mapaddr);
967 offset += fuse_copy_do(cs, NULL, &count);
969 if (page && !cs->write)
970 flush_dcache_page(page);
974 /* Copy pages in the request to/from userspace buffer */
975 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
979 struct fuse_req *req = cs->req;
980 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
983 for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) {
985 unsigned int offset = ap->descs[i].offset;
986 unsigned int count = min(nbytes, ap->descs[i].length);
988 err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing);
997 /* Copy a single argument in the request to/from userspace buffer */
998 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1002 int err = fuse_copy_fill(cs);
1006 fuse_copy_do(cs, &val, &size);
1011 /* Copy request arguments to/from userspace buffer */
1012 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1013 unsigned argpages, struct fuse_arg *args,
1019 for (i = 0; !err && i < numargs; i++) {
1020 struct fuse_arg *arg = &args[i];
1021 if (i == numargs - 1 && argpages)
1022 err = fuse_copy_pages(cs, arg->size, zeroing);
1024 err = fuse_copy_one(cs, arg->value, arg->size);
1029 static int forget_pending(struct fuse_iqueue *fiq)
1031 return fiq->forget_list_head.next != NULL;
1034 static int request_pending(struct fuse_iqueue *fiq)
1036 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1037 forget_pending(fiq);
1041 * Transfer an interrupt request to userspace
1043 * Unlike other requests this is assembled on demand, without a need
1044 * to allocate a separate fuse_req structure.
1046 * Called with fiq->lock held, releases it
1048 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1049 struct fuse_copy_state *cs,
1050 size_t nbytes, struct fuse_req *req)
1051 __releases(fiq->lock)
1053 struct fuse_in_header ih;
1054 struct fuse_interrupt_in arg;
1055 unsigned reqsize = sizeof(ih) + sizeof(arg);
1058 list_del_init(&req->intr_entry);
1059 memset(&ih, 0, sizeof(ih));
1060 memset(&arg, 0, sizeof(arg));
1062 ih.opcode = FUSE_INTERRUPT;
1063 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
1064 arg.unique = req->in.h.unique;
1066 spin_unlock(&fiq->lock);
1067 if (nbytes < reqsize)
1070 err = fuse_copy_one(cs, &ih, sizeof(ih));
1072 err = fuse_copy_one(cs, &arg, sizeof(arg));
1073 fuse_copy_finish(cs);
1075 return err ? err : reqsize;
1078 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1080 unsigned int *countp)
1082 struct fuse_forget_link *head = fiq->forget_list_head.next;
1083 struct fuse_forget_link **newhead = &head;
1086 for (count = 0; *newhead != NULL && count < max; count++)
1087 newhead = &(*newhead)->next;
1089 fiq->forget_list_head.next = *newhead;
1091 if (fiq->forget_list_head.next == NULL)
1092 fiq->forget_list_tail = &fiq->forget_list_head;
1099 EXPORT_SYMBOL(fuse_dequeue_forget);
1101 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1102 struct fuse_copy_state *cs,
1104 __releases(fiq->lock)
1107 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
1108 struct fuse_forget_in arg = {
1109 .nlookup = forget->forget_one.nlookup,
1111 struct fuse_in_header ih = {
1112 .opcode = FUSE_FORGET,
1113 .nodeid = forget->forget_one.nodeid,
1114 .unique = fuse_get_unique(fiq),
1115 .len = sizeof(ih) + sizeof(arg),
1118 spin_unlock(&fiq->lock);
1120 if (nbytes < ih.len)
1123 err = fuse_copy_one(cs, &ih, sizeof(ih));
1125 err = fuse_copy_one(cs, &arg, sizeof(arg));
1126 fuse_copy_finish(cs);
1134 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1135 struct fuse_copy_state *cs, size_t nbytes)
1136 __releases(fiq->lock)
1139 unsigned max_forgets;
1141 struct fuse_forget_link *head;
1142 struct fuse_batch_forget_in arg = { .count = 0 };
1143 struct fuse_in_header ih = {
1144 .opcode = FUSE_BATCH_FORGET,
1145 .unique = fuse_get_unique(fiq),
1146 .len = sizeof(ih) + sizeof(arg),
1149 if (nbytes < ih.len) {
1150 spin_unlock(&fiq->lock);
1154 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1155 head = fuse_dequeue_forget(fiq, max_forgets, &count);
1156 spin_unlock(&fiq->lock);
1159 ih.len += count * sizeof(struct fuse_forget_one);
1160 err = fuse_copy_one(cs, &ih, sizeof(ih));
1162 err = fuse_copy_one(cs, &arg, sizeof(arg));
1165 struct fuse_forget_link *forget = head;
1168 err = fuse_copy_one(cs, &forget->forget_one,
1169 sizeof(forget->forget_one));
1171 head = forget->next;
1175 fuse_copy_finish(cs);
1183 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1184 struct fuse_copy_state *cs,
1186 __releases(fiq->lock)
1188 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1189 return fuse_read_single_forget(fiq, cs, nbytes);
1191 return fuse_read_batch_forget(fiq, cs, nbytes);
1195 * Read a single request into the userspace filesystem's buffer. This
1196 * function waits until a request is available, then removes it from
1197 * the pending list and copies request data to userspace buffer. If
1198 * no reply is needed (FORGET) or request has been aborted or there
1199 * was an error during the copying then it's finished by calling
1200 * fuse_request_end(). Otherwise add it to the processing list, and set
1203 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1204 struct fuse_copy_state *cs, size_t nbytes)
1207 struct fuse_conn *fc = fud->fc;
1208 struct fuse_iqueue *fiq = &fc->iq;
1209 struct fuse_pqueue *fpq = &fud->pq;
1210 struct fuse_req *req;
1211 struct fuse_args *args;
1216 * Require sane minimum read buffer - that has capacity for fixed part
1217 * of any request header + negotiated max_write room for data.
1219 * Historically libfuse reserves 4K for fixed header room, but e.g.
1220 * GlusterFS reserves only 80 bytes
1222 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1224 * which is the absolute minimum any sane filesystem should be using
1227 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1228 sizeof(struct fuse_in_header) +
1229 sizeof(struct fuse_write_in) +
1235 spin_lock(&fiq->lock);
1236 if (!fiq->connected || request_pending(fiq))
1238 spin_unlock(&fiq->lock);
1240 if (file->f_flags & O_NONBLOCK)
1242 err = wait_event_interruptible_exclusive(fiq->waitq,
1243 !fiq->connected || request_pending(fiq));
1248 if (!fiq->connected) {
1249 err = fc->aborted ? -ECONNABORTED : -ENODEV;
1253 if (!list_empty(&fiq->interrupts)) {
1254 req = list_entry(fiq->interrupts.next, struct fuse_req,
1256 return fuse_read_interrupt(fiq, cs, nbytes, req);
1259 if (forget_pending(fiq)) {
1260 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1261 return fuse_read_forget(fc, fiq, cs, nbytes);
1263 if (fiq->forget_batch <= -8)
1264 fiq->forget_batch = 16;
1267 req = list_entry(fiq->pending.next, struct fuse_req, list);
1268 clear_bit(FR_PENDING, &req->flags);
1269 list_del_init(&req->list);
1270 spin_unlock(&fiq->lock);
1273 reqsize = req->in.h.len;
1275 /* If request is too large, reply with an error and restart the read */
1276 if (nbytes < reqsize) {
1277 req->out.h.error = -EIO;
1278 /* SETXATTR is special, since it may contain too large data */
1279 if (args->opcode == FUSE_SETXATTR)
1280 req->out.h.error = -E2BIG;
1281 fuse_request_end(req);
1284 spin_lock(&fpq->lock);
1286 * Must not put request on fpq->io queue after having been shut down by
1289 if (!fpq->connected) {
1290 req->out.h.error = err = -ECONNABORTED;
1294 list_add(&req->list, &fpq->io);
1295 spin_unlock(&fpq->lock);
1297 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
1299 err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1300 (struct fuse_arg *) args->in_args, 0);
1301 fuse_copy_finish(cs);
1302 spin_lock(&fpq->lock);
1303 clear_bit(FR_LOCKED, &req->flags);
1304 if (!fpq->connected) {
1305 err = fc->aborted ? -ECONNABORTED : -ENODEV;
1309 req->out.h.error = -EIO;
1312 if (!test_bit(FR_ISREPLY, &req->flags)) {
1316 hash = fuse_req_hash(req->in.h.unique);
1317 list_move_tail(&req->list, &fpq->processing[hash]);
1318 __fuse_get_request(req);
1319 set_bit(FR_SENT, &req->flags);
1320 spin_unlock(&fpq->lock);
1321 /* matches barrier in request_wait_answer() */
1322 smp_mb__after_atomic();
1323 if (test_bit(FR_INTERRUPTED, &req->flags))
1324 queue_interrupt(req);
1325 fuse_put_request(req);
1330 if (!test_bit(FR_PRIVATE, &req->flags))
1331 list_del_init(&req->list);
1332 spin_unlock(&fpq->lock);
1333 fuse_request_end(req);
1337 spin_unlock(&fiq->lock);
1341 static int fuse_dev_open(struct inode *inode, struct file *file)
1344 * The fuse device's file's private_data is used to hold
1345 * the fuse_conn(ection) when it is mounted, and is used to
1346 * keep track of whether the file has been mounted already.
1348 file->private_data = NULL;
1352 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1354 struct fuse_copy_state cs;
1355 struct file *file = iocb->ki_filp;
1356 struct fuse_dev *fud = fuse_get_dev(file);
1361 if (!user_backed_iter(to))
1364 fuse_copy_init(&cs, 1, to);
1366 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1369 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1370 struct pipe_inode_info *pipe,
1371 size_t len, unsigned int flags)
1375 struct pipe_buffer *bufs;
1376 struct fuse_copy_state cs;
1377 struct fuse_dev *fud = fuse_get_dev(in);
1382 bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer),
1387 fuse_copy_init(&cs, 1, NULL);
1390 ret = fuse_dev_do_read(fud, in, &cs, len);
1394 if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) {
1399 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
1401 * Need to be careful about this. Having buf->ops in module
1402 * code can Oops if the buffer persists after module unload.
1404 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
1405 bufs[page_nr].flags = 0;
1406 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1407 if (unlikely(ret < 0))
1413 for (; page_nr < cs.nr_segs; page_nr++)
1414 put_page(bufs[page_nr].page);
1420 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1421 struct fuse_copy_state *cs)
1423 struct fuse_notify_poll_wakeup_out outarg;
1426 if (size != sizeof(outarg))
1429 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1433 fuse_copy_finish(cs);
1434 return fuse_notify_poll_wakeup(fc, &outarg);
1437 fuse_copy_finish(cs);
1441 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1442 struct fuse_copy_state *cs)
1444 struct fuse_notify_inval_inode_out outarg;
1447 if (size != sizeof(outarg))
1450 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1453 fuse_copy_finish(cs);
1455 down_read(&fc->killsb);
1456 err = fuse_reverse_inval_inode(fc, outarg.ino,
1457 outarg.off, outarg.len);
1458 up_read(&fc->killsb);
1462 fuse_copy_finish(cs);
1466 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1467 struct fuse_copy_state *cs)
1469 struct fuse_notify_inval_entry_out outarg;
1474 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1479 if (size < sizeof(outarg))
1482 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1486 err = -ENAMETOOLONG;
1487 if (outarg.namelen > FUSE_NAME_MAX)
1491 if (size != sizeof(outarg) + outarg.namelen + 1)
1495 name.len = outarg.namelen;
1496 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1499 fuse_copy_finish(cs);
1500 buf[outarg.namelen] = 0;
1502 down_read(&fc->killsb);
1503 err = fuse_reverse_inval_entry(fc, outarg.parent, 0, &name, outarg.flags);
1504 up_read(&fc->killsb);
1510 fuse_copy_finish(cs);
1514 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1515 struct fuse_copy_state *cs)
1517 struct fuse_notify_delete_out outarg;
1522 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1527 if (size < sizeof(outarg))
1530 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1534 err = -ENAMETOOLONG;
1535 if (outarg.namelen > FUSE_NAME_MAX)
1539 if (size != sizeof(outarg) + outarg.namelen + 1)
1543 name.len = outarg.namelen;
1544 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1547 fuse_copy_finish(cs);
1548 buf[outarg.namelen] = 0;
1550 down_read(&fc->killsb);
1551 err = fuse_reverse_inval_entry(fc, outarg.parent, outarg.child, &name, 0);
1552 up_read(&fc->killsb);
1558 fuse_copy_finish(cs);
1562 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1563 struct fuse_copy_state *cs)
1565 struct fuse_notify_store_out outarg;
1566 struct inode *inode;
1567 struct address_space *mapping;
1571 unsigned int offset;
1577 if (size < sizeof(outarg))
1580 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1585 if (size - sizeof(outarg) != outarg.size)
1588 nodeid = outarg.nodeid;
1590 down_read(&fc->killsb);
1593 inode = fuse_ilookup(fc, nodeid, NULL);
1597 mapping = inode->i_mapping;
1598 index = outarg.offset >> PAGE_SHIFT;
1599 offset = outarg.offset & ~PAGE_MASK;
1600 file_size = i_size_read(inode);
1601 end = outarg.offset + outarg.size;
1602 if (end > file_size) {
1604 fuse_write_update_attr(inode, file_size, outarg.size);
1610 unsigned int this_num;
1613 page = find_or_create_page(mapping, index,
1614 mapping_gfp_mask(mapping));
1618 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
1619 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1620 if (!PageUptodate(page) && !err && offset == 0 &&
1621 (this_num == PAGE_SIZE || file_size == end)) {
1622 zero_user_segment(page, this_num, PAGE_SIZE);
1623 SetPageUptodate(page);
1641 up_read(&fc->killsb);
1643 fuse_copy_finish(cs);
1647 struct fuse_retrieve_args {
1648 struct fuse_args_pages ap;
1649 struct fuse_notify_retrieve_in inarg;
1652 static void fuse_retrieve_end(struct fuse_mount *fm, struct fuse_args *args,
1655 struct fuse_retrieve_args *ra =
1656 container_of(args, typeof(*ra), ap.args);
1658 release_pages(ra->ap.pages, ra->ap.num_pages);
1662 static int fuse_retrieve(struct fuse_mount *fm, struct inode *inode,
1663 struct fuse_notify_retrieve_out *outarg)
1666 struct address_space *mapping = inode->i_mapping;
1670 unsigned int offset;
1671 size_t total_len = 0;
1672 unsigned int num_pages;
1673 struct fuse_conn *fc = fm->fc;
1674 struct fuse_retrieve_args *ra;
1675 size_t args_size = sizeof(*ra);
1676 struct fuse_args_pages *ap;
1677 struct fuse_args *args;
1679 offset = outarg->offset & ~PAGE_MASK;
1680 file_size = i_size_read(inode);
1682 num = min(outarg->size, fc->max_write);
1683 if (outarg->offset > file_size)
1685 else if (outarg->offset + num > file_size)
1686 num = file_size - outarg->offset;
1688 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1689 num_pages = min(num_pages, fc->max_pages);
1691 args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0]));
1693 ra = kzalloc(args_size, GFP_KERNEL);
1698 ap->pages = (void *) (ra + 1);
1699 ap->descs = (void *) (ap->pages + num_pages);
1702 args->nodeid = outarg->nodeid;
1703 args->opcode = FUSE_NOTIFY_REPLY;
1704 args->in_numargs = 2;
1705 args->in_pages = true;
1706 args->end = fuse_retrieve_end;
1708 index = outarg->offset >> PAGE_SHIFT;
1710 while (num && ap->num_pages < num_pages) {
1712 unsigned int this_num;
1714 page = find_get_page(mapping, index);
1718 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
1719 ap->pages[ap->num_pages] = page;
1720 ap->descs[ap->num_pages].offset = offset;
1721 ap->descs[ap->num_pages].length = this_num;
1726 total_len += this_num;
1729 ra->inarg.offset = outarg->offset;
1730 ra->inarg.size = total_len;
1731 args->in_args[0].size = sizeof(ra->inarg);
1732 args->in_args[0].value = &ra->inarg;
1733 args->in_args[1].size = total_len;
1735 err = fuse_simple_notify_reply(fm, args, outarg->notify_unique);
1737 fuse_retrieve_end(fm, args, err);
1742 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1743 struct fuse_copy_state *cs)
1745 struct fuse_notify_retrieve_out outarg;
1746 struct fuse_mount *fm;
1747 struct inode *inode;
1752 if (size != sizeof(outarg))
1755 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1759 fuse_copy_finish(cs);
1761 down_read(&fc->killsb);
1763 nodeid = outarg.nodeid;
1765 inode = fuse_ilookup(fc, nodeid, &fm);
1767 err = fuse_retrieve(fm, inode, &outarg);
1770 up_read(&fc->killsb);
1775 fuse_copy_finish(cs);
1780 * Resending all processing queue requests.
1782 * During a FUSE daemon panics and failover, it is possible for some inflight
1783 * requests to be lost and never returned. As a result, applications awaiting
1784 * replies would become stuck forever. To address this, we can use notification
1785 * to trigger resending of these pending requests to the FUSE daemon, ensuring
1786 * they are properly processed again.
1788 * Please note that this strategy is applicable only to idempotent requests or
1789 * if the FUSE daemon takes careful measures to avoid processing duplicated
1790 * non-idempotent requests.
1792 static void fuse_resend(struct fuse_conn *fc)
1794 struct fuse_dev *fud;
1795 struct fuse_req *req, *next;
1796 struct fuse_iqueue *fiq = &fc->iq;
1797 LIST_HEAD(to_queue);
1800 spin_lock(&fc->lock);
1801 if (!fc->connected) {
1802 spin_unlock(&fc->lock);
1806 list_for_each_entry(fud, &fc->devices, entry) {
1807 struct fuse_pqueue *fpq = &fud->pq;
1809 spin_lock(&fpq->lock);
1810 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
1811 list_splice_tail_init(&fpq->processing[i], &to_queue);
1812 spin_unlock(&fpq->lock);
1814 spin_unlock(&fc->lock);
1816 list_for_each_entry_safe(req, next, &to_queue, list) {
1817 set_bit(FR_PENDING, &req->flags);
1818 clear_bit(FR_SENT, &req->flags);
1819 /* mark the request as resend request */
1820 req->in.h.unique |= FUSE_UNIQUE_RESEND;
1823 spin_lock(&fiq->lock);
1824 if (!fiq->connected) {
1825 spin_unlock(&fiq->lock);
1826 list_for_each_entry(req, &to_queue, list)
1827 clear_bit(FR_PENDING, &req->flags);
1828 end_requests(&to_queue);
1831 /* iq and pq requests are both oldest to newest */
1832 list_splice(&to_queue, &fiq->pending);
1833 fiq->ops->wake_pending_and_unlock(fiq);
1836 static int fuse_notify_resend(struct fuse_conn *fc)
1842 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1843 unsigned int size, struct fuse_copy_state *cs)
1845 /* Don't try to move pages (yet) */
1849 case FUSE_NOTIFY_POLL:
1850 return fuse_notify_poll(fc, size, cs);
1852 case FUSE_NOTIFY_INVAL_INODE:
1853 return fuse_notify_inval_inode(fc, size, cs);
1855 case FUSE_NOTIFY_INVAL_ENTRY:
1856 return fuse_notify_inval_entry(fc, size, cs);
1858 case FUSE_NOTIFY_STORE:
1859 return fuse_notify_store(fc, size, cs);
1861 case FUSE_NOTIFY_RETRIEVE:
1862 return fuse_notify_retrieve(fc, size, cs);
1864 case FUSE_NOTIFY_DELETE:
1865 return fuse_notify_delete(fc, size, cs);
1867 case FUSE_NOTIFY_RESEND:
1868 return fuse_notify_resend(fc);
1871 fuse_copy_finish(cs);
1876 /* Look up request on processing list by unique ID */
1877 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1879 unsigned int hash = fuse_req_hash(unique);
1880 struct fuse_req *req;
1882 list_for_each_entry(req, &fpq->processing[hash], list) {
1883 if (req->in.h.unique == unique)
1889 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
1892 unsigned reqsize = sizeof(struct fuse_out_header);
1894 reqsize += fuse_len_args(args->out_numargs, args->out_args);
1896 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
1898 else if (reqsize > nbytes) {
1899 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
1900 unsigned diffsize = reqsize - nbytes;
1902 if (diffsize > lastarg->size)
1904 lastarg->size -= diffsize;
1906 return fuse_copy_args(cs, args->out_numargs, args->out_pages,
1907 args->out_args, args->page_zeroing);
1911 * Write a single reply to a request. First the header is copied from
1912 * the write buffer. The request is then searched on the processing
1913 * list by the unique ID found in the header. If found, then remove
1914 * it from the list and copy the rest of the buffer to the request.
1915 * The request is finished by calling fuse_request_end().
1917 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1918 struct fuse_copy_state *cs, size_t nbytes)
1921 struct fuse_conn *fc = fud->fc;
1922 struct fuse_pqueue *fpq = &fud->pq;
1923 struct fuse_req *req;
1924 struct fuse_out_header oh;
1927 if (nbytes < sizeof(struct fuse_out_header))
1930 err = fuse_copy_one(cs, &oh, sizeof(oh));
1935 if (oh.len != nbytes)
1939 * Zero oh.unique indicates unsolicited notification message
1940 * and error contains notification code.
1943 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1948 if (oh.error <= -512 || oh.error > 0)
1951 spin_lock(&fpq->lock);
1954 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
1958 spin_unlock(&fpq->lock);
1962 /* Is it an interrupt reply ID? */
1963 if (oh.unique & FUSE_INT_REQ_BIT) {
1964 __fuse_get_request(req);
1965 spin_unlock(&fpq->lock);
1968 if (nbytes != sizeof(struct fuse_out_header))
1970 else if (oh.error == -ENOSYS)
1971 fc->no_interrupt = 1;
1972 else if (oh.error == -EAGAIN)
1973 err = queue_interrupt(req);
1975 fuse_put_request(req);
1980 clear_bit(FR_SENT, &req->flags);
1981 list_move(&req->list, &fpq->io);
1983 set_bit(FR_LOCKED, &req->flags);
1984 spin_unlock(&fpq->lock);
1986 if (!req->args->page_replace)
1990 err = nbytes != sizeof(oh) ? -EINVAL : 0;
1992 err = copy_out_args(cs, req->args, nbytes);
1993 fuse_copy_finish(cs);
1995 spin_lock(&fpq->lock);
1996 clear_bit(FR_LOCKED, &req->flags);
1997 if (!fpq->connected)
2000 req->out.h.error = -EIO;
2001 if (!test_bit(FR_PRIVATE, &req->flags))
2002 list_del_init(&req->list);
2003 spin_unlock(&fpq->lock);
2005 fuse_request_end(req);
2007 return err ? err : nbytes;
2010 fuse_copy_finish(cs);
2014 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
2016 struct fuse_copy_state cs;
2017 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
2022 if (!user_backed_iter(from))
2025 fuse_copy_init(&cs, 0, from);
2027 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
2030 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
2031 struct file *out, loff_t *ppos,
2032 size_t len, unsigned int flags)
2034 unsigned int head, tail, mask, count;
2037 struct pipe_buffer *bufs;
2038 struct fuse_copy_state cs;
2039 struct fuse_dev *fud;
2043 fud = fuse_get_dev(out);
2051 mask = pipe->ring_size - 1;
2052 count = head - tail;
2054 bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL);
2062 for (idx = tail; idx != head && rem < len; idx++)
2063 rem += pipe->bufs[idx & mask].len;
2071 struct pipe_buffer *ibuf;
2072 struct pipe_buffer *obuf;
2074 if (WARN_ON(nbuf >= count || tail == head))
2077 ibuf = &pipe->bufs[tail & mask];
2080 if (rem >= ibuf->len) {
2086 if (!pipe_buf_get(pipe, ibuf))
2090 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2092 ibuf->offset += obuf->len;
2093 ibuf->len -= obuf->len;
2100 fuse_copy_init(&cs, 0, NULL);
2105 if (flags & SPLICE_F_MOVE)
2108 ret = fuse_dev_do_write(fud, &cs, len);
2112 for (idx = 0; idx < nbuf; idx++) {
2113 struct pipe_buffer *buf = &bufs[idx];
2116 pipe_buf_release(pipe, buf);
2124 static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
2126 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
2127 struct fuse_iqueue *fiq;
2128 struct fuse_dev *fud = fuse_get_dev(file);
2134 poll_wait(file, &fiq->waitq, wait);
2136 spin_lock(&fiq->lock);
2137 if (!fiq->connected)
2139 else if (request_pending(fiq))
2140 mask |= EPOLLIN | EPOLLRDNORM;
2141 spin_unlock(&fiq->lock);
2146 /* Abort all requests on the given list (pending or processing) */
2147 static void end_requests(struct list_head *head)
2149 while (!list_empty(head)) {
2150 struct fuse_req *req;
2151 req = list_entry(head->next, struct fuse_req, list);
2152 req->out.h.error = -ECONNABORTED;
2153 clear_bit(FR_SENT, &req->flags);
2154 list_del_init(&req->list);
2155 fuse_request_end(req);
2159 static void end_polls(struct fuse_conn *fc)
2163 p = rb_first(&fc->polled_files);
2166 struct fuse_file *ff;
2167 ff = rb_entry(p, struct fuse_file, polled_node);
2168 wake_up_interruptible_all(&ff->poll_wait);
2175 * Abort all requests.
2177 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2180 * The same effect is usually achievable through killing the filesystem daemon
2181 * and all users of the filesystem. The exception is the combination of an
2182 * asynchronous request and the tricky deadlock (see
2183 * Documentation/filesystems/fuse.rst).
2185 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2186 * requests, they should be finished off immediately. Locked requests will be
2187 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2188 * requests. It is possible that some request will finish before we can. This
2189 * is OK, the request will in that case be removed from the list before we touch
2192 void fuse_abort_conn(struct fuse_conn *fc)
2194 struct fuse_iqueue *fiq = &fc->iq;
2196 spin_lock(&fc->lock);
2197 if (fc->connected) {
2198 struct fuse_dev *fud;
2199 struct fuse_req *req, *next;
2203 /* Background queuing checks fc->connected under bg_lock */
2204 spin_lock(&fc->bg_lock);
2206 spin_unlock(&fc->bg_lock);
2208 fuse_set_initialized(fc);
2209 list_for_each_entry(fud, &fc->devices, entry) {
2210 struct fuse_pqueue *fpq = &fud->pq;
2212 spin_lock(&fpq->lock);
2214 list_for_each_entry_safe(req, next, &fpq->io, list) {
2215 req->out.h.error = -ECONNABORTED;
2216 spin_lock(&req->waitq.lock);
2217 set_bit(FR_ABORTED, &req->flags);
2218 if (!test_bit(FR_LOCKED, &req->flags)) {
2219 set_bit(FR_PRIVATE, &req->flags);
2220 __fuse_get_request(req);
2221 list_move(&req->list, &to_end);
2223 spin_unlock(&req->waitq.lock);
2225 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2226 list_splice_tail_init(&fpq->processing[i],
2228 spin_unlock(&fpq->lock);
2230 spin_lock(&fc->bg_lock);
2232 fc->max_background = UINT_MAX;
2234 spin_unlock(&fc->bg_lock);
2236 spin_lock(&fiq->lock);
2238 list_for_each_entry(req, &fiq->pending, list)
2239 clear_bit(FR_PENDING, &req->flags);
2240 list_splice_tail_init(&fiq->pending, &to_end);
2241 while (forget_pending(fiq))
2242 kfree(fuse_dequeue_forget(fiq, 1, NULL));
2243 wake_up_all(&fiq->waitq);
2244 spin_unlock(&fiq->lock);
2245 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2247 wake_up_all(&fc->blocked_waitq);
2248 spin_unlock(&fc->lock);
2250 end_requests(&to_end);
2252 spin_unlock(&fc->lock);
2255 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2257 void fuse_wait_aborted(struct fuse_conn *fc)
2259 /* matches implicit memory barrier in fuse_drop_waiting() */
2261 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2264 int fuse_dev_release(struct inode *inode, struct file *file)
2266 struct fuse_dev *fud = fuse_get_dev(file);
2269 struct fuse_conn *fc = fud->fc;
2270 struct fuse_pqueue *fpq = &fud->pq;
2274 spin_lock(&fpq->lock);
2275 WARN_ON(!list_empty(&fpq->io));
2276 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2277 list_splice_init(&fpq->processing[i], &to_end);
2278 spin_unlock(&fpq->lock);
2280 end_requests(&to_end);
2282 /* Are we the last open device? */
2283 if (atomic_dec_and_test(&fc->dev_count)) {
2284 WARN_ON(fc->iq.fasync != NULL);
2285 fuse_abort_conn(fc);
2291 EXPORT_SYMBOL_GPL(fuse_dev_release);
2293 static int fuse_dev_fasync(int fd, struct file *file, int on)
2295 struct fuse_dev *fud = fuse_get_dev(file);
2300 /* No locking - fasync_helper does its own locking */
2301 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2304 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2306 struct fuse_dev *fud;
2308 if (new->private_data)
2311 fud = fuse_dev_alloc_install(fc);
2315 new->private_data = fud;
2316 atomic_inc(&fc->dev_count);
2321 static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
2325 struct fuse_dev *fud = NULL;
2328 if (get_user(oldfd, argp))
2336 * Check against file->f_op because CUSE
2337 * uses the same ioctl handler.
2339 if (f.file->f_op == file->f_op)
2340 fud = fuse_get_dev(f.file);
2344 mutex_lock(&fuse_mutex);
2345 res = fuse_device_clone(fud->fc, file);
2346 mutex_unlock(&fuse_mutex);
2353 static long fuse_dev_ioctl_backing_open(struct file *file,
2354 struct fuse_backing_map __user *argp)
2356 struct fuse_dev *fud = fuse_get_dev(file);
2357 struct fuse_backing_map map;
2362 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
2365 if (copy_from_user(&map, argp, sizeof(map)))
2368 return fuse_backing_open(fud->fc, &map);
2371 static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
2373 struct fuse_dev *fud = fuse_get_dev(file);
2379 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
2382 if (get_user(backing_id, argp))
2385 return fuse_backing_close(fud->fc, backing_id);
2388 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2391 void __user *argp = (void __user *)arg;
2394 case FUSE_DEV_IOC_CLONE:
2395 return fuse_dev_ioctl_clone(file, argp);
2397 case FUSE_DEV_IOC_BACKING_OPEN:
2398 return fuse_dev_ioctl_backing_open(file, argp);
2400 case FUSE_DEV_IOC_BACKING_CLOSE:
2401 return fuse_dev_ioctl_backing_close(file, argp);
2408 const struct file_operations fuse_dev_operations = {
2409 .owner = THIS_MODULE,
2410 .open = fuse_dev_open,
2411 .llseek = no_llseek,
2412 .read_iter = fuse_dev_read,
2413 .splice_read = fuse_dev_splice_read,
2414 .write_iter = fuse_dev_write,
2415 .splice_write = fuse_dev_splice_write,
2416 .poll = fuse_dev_poll,
2417 .release = fuse_dev_release,
2418 .fasync = fuse_dev_fasync,
2419 .unlocked_ioctl = fuse_dev_ioctl,
2420 .compat_ioctl = compat_ptr_ioctl,
2422 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2424 static struct miscdevice fuse_miscdevice = {
2425 .minor = FUSE_MINOR,
2427 .fops = &fuse_dev_operations,
2430 int __init fuse_dev_init(void)
2433 fuse_req_cachep = kmem_cache_create("fuse_request",
2434 sizeof(struct fuse_req),
2436 if (!fuse_req_cachep)
2439 err = misc_register(&fuse_miscdevice);
2441 goto out_cache_clean;
2446 kmem_cache_destroy(fuse_req_cachep);
2451 void fuse_dev_cleanup(void)
2453 misc_deregister(&fuse_miscdevice);
2454 kmem_cache_destroy(fuse_req_cachep);