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/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 #define CREATE_TRACE_POINTS
26 #include "fuse_trace.h"
28 MODULE_ALIAS_MISCDEV(FUSE_MINOR
);
29 MODULE_ALIAS("devname:fuse");
31 /* Ordinary requests have even IDs, while interrupts IDs are odd */
32 #define FUSE_INT_REQ_BIT (1ULL << 0)
33 #define FUSE_REQ_ID_STEP (1ULL << 1)
35 static struct kmem_cache
*fuse_req_cachep
;
37 static void end_requests(struct list_head
*head
);
39 static struct fuse_dev
*fuse_get_dev(struct file
*file
)
42 * Lockless access is OK, because file->private data is set
43 * once during mount and is valid until the file is released.
45 return READ_ONCE(file
->private_data
);
48 static void fuse_request_init(struct fuse_mount
*fm
, struct fuse_req
*req
)
50 INIT_LIST_HEAD(&req
->list
);
51 INIT_LIST_HEAD(&req
->intr_entry
);
52 init_waitqueue_head(&req
->waitq
);
53 refcount_set(&req
->count
, 1);
54 __set_bit(FR_PENDING
, &req
->flags
);
58 static struct fuse_req
*fuse_request_alloc(struct fuse_mount
*fm
, gfp_t flags
)
60 struct fuse_req
*req
= kmem_cache_zalloc(fuse_req_cachep
, flags
);
62 fuse_request_init(fm
, req
);
67 static void fuse_request_free(struct fuse_req
*req
)
69 kmem_cache_free(fuse_req_cachep
, req
);
72 static void __fuse_get_request(struct fuse_req
*req
)
74 refcount_inc(&req
->count
);
77 /* Must be called with > 1 refcount */
78 static void __fuse_put_request(struct fuse_req
*req
)
80 refcount_dec(&req
->count
);
83 void fuse_set_initialized(struct fuse_conn
*fc
)
85 /* Make sure stores before this are seen on another CPU */
90 static bool fuse_block_alloc(struct fuse_conn
*fc
, bool for_background
)
92 return !fc
->initialized
|| (for_background
&& fc
->blocked
);
95 static void fuse_drop_waiting(struct fuse_conn
*fc
)
98 * lockess check of fc->connected is okay, because atomic_dec_and_test()
99 * provides a memory barrier matched with the one in fuse_wait_aborted()
100 * to ensure no wake-up is missed.
102 if (atomic_dec_and_test(&fc
->num_waiting
) &&
103 !READ_ONCE(fc
->connected
)) {
104 /* wake up aborters */
105 wake_up_all(&fc
->blocked_waitq
);
109 static void fuse_put_request(struct fuse_req
*req
);
111 static struct fuse_req
*fuse_get_req(struct mnt_idmap
*idmap
,
112 struct fuse_mount
*fm
,
115 struct fuse_conn
*fc
= fm
->fc
;
116 struct fuse_req
*req
;
117 bool no_idmap
= !fm
->sb
|| (fm
->sb
->s_iflags
& SB_I_NOIDMAP
);
122 atomic_inc(&fc
->num_waiting
);
124 if (fuse_block_alloc(fc
, for_background
)) {
126 if (wait_event_killable_exclusive(fc
->blocked_waitq
,
127 !fuse_block_alloc(fc
, for_background
)))
130 /* Matches smp_wmb() in fuse_set_initialized() */
141 req
= fuse_request_alloc(fm
, GFP_KERNEL
);
145 wake_up(&fc
->blocked_waitq
);
149 req
->in
.h
.pid
= pid_nr_ns(task_pid(current
), fc
->pid_ns
);
151 __set_bit(FR_WAITING
, &req
->flags
);
153 __set_bit(FR_BACKGROUND
, &req
->flags
);
156 * Keep the old behavior when idmappings support was not
157 * declared by a FUSE server.
159 * For those FUSE servers who support idmapped mounts,
160 * we send UID/GID only along with "inode creation"
161 * fuse requests, otherwise idmap == &invalid_mnt_idmap and
162 * req->in.h.{u,g}id will be equal to FUSE_INVALID_UIDGID.
164 fsuid
= no_idmap
? current_fsuid() : mapped_fsuid(idmap
, fc
->user_ns
);
165 fsgid
= no_idmap
? current_fsgid() : mapped_fsgid(idmap
, fc
->user_ns
);
166 req
->in
.h
.uid
= from_kuid(fc
->user_ns
, fsuid
);
167 req
->in
.h
.gid
= from_kgid(fc
->user_ns
, fsgid
);
169 if (no_idmap
&& unlikely(req
->in
.h
.uid
== ((uid_t
)-1) ||
170 req
->in
.h
.gid
== ((gid_t
)-1))) {
171 fuse_put_request(req
);
172 return ERR_PTR(-EOVERFLOW
);
178 fuse_drop_waiting(fc
);
182 static void fuse_put_request(struct fuse_req
*req
)
184 struct fuse_conn
*fc
= req
->fm
->fc
;
186 if (refcount_dec_and_test(&req
->count
)) {
187 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
189 * We get here in the unlikely case that a background
190 * request was allocated but not sent
192 spin_lock(&fc
->bg_lock
);
194 wake_up(&fc
->blocked_waitq
);
195 spin_unlock(&fc
->bg_lock
);
198 if (test_bit(FR_WAITING
, &req
->flags
)) {
199 __clear_bit(FR_WAITING
, &req
->flags
);
200 fuse_drop_waiting(fc
);
203 fuse_request_free(req
);
207 unsigned int fuse_len_args(unsigned int numargs
, struct fuse_arg
*args
)
212 for (i
= 0; i
< numargs
; i
++)
213 nbytes
+= args
[i
].size
;
217 EXPORT_SYMBOL_GPL(fuse_len_args
);
219 static u64
fuse_get_unique_locked(struct fuse_iqueue
*fiq
)
221 fiq
->reqctr
+= FUSE_REQ_ID_STEP
;
225 u64
fuse_get_unique(struct fuse_iqueue
*fiq
)
229 spin_lock(&fiq
->lock
);
230 ret
= fuse_get_unique_locked(fiq
);
231 spin_unlock(&fiq
->lock
);
235 EXPORT_SYMBOL_GPL(fuse_get_unique
);
237 static unsigned int fuse_req_hash(u64 unique
)
239 return hash_long(unique
& ~FUSE_INT_REQ_BIT
, FUSE_PQ_HASH_BITS
);
243 * A new request is available, wake fiq->waitq
245 static void fuse_dev_wake_and_unlock(struct fuse_iqueue
*fiq
)
246 __releases(fiq
->lock
)
248 wake_up(&fiq
->waitq
);
249 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
250 spin_unlock(&fiq
->lock
);
253 static void fuse_dev_queue_forget(struct fuse_iqueue
*fiq
, struct fuse_forget_link
*forget
)
255 spin_lock(&fiq
->lock
);
256 if (fiq
->connected
) {
257 fiq
->forget_list_tail
->next
= forget
;
258 fiq
->forget_list_tail
= forget
;
259 fuse_dev_wake_and_unlock(fiq
);
262 spin_unlock(&fiq
->lock
);
266 static void fuse_dev_queue_interrupt(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
268 spin_lock(&fiq
->lock
);
269 if (list_empty(&req
->intr_entry
)) {
270 list_add_tail(&req
->intr_entry
, &fiq
->interrupts
);
272 * Pairs with smp_mb() implied by test_and_set_bit()
273 * from fuse_request_end().
276 if (test_bit(FR_FINISHED
, &req
->flags
)) {
277 list_del_init(&req
->intr_entry
);
278 spin_unlock(&fiq
->lock
);
280 fuse_dev_wake_and_unlock(fiq
);
283 spin_unlock(&fiq
->lock
);
287 static void fuse_dev_queue_req(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
289 spin_lock(&fiq
->lock
);
290 if (fiq
->connected
) {
291 if (req
->in
.h
.opcode
!= FUSE_NOTIFY_REPLY
)
292 req
->in
.h
.unique
= fuse_get_unique_locked(fiq
);
293 list_add_tail(&req
->list
, &fiq
->pending
);
294 fuse_dev_wake_and_unlock(fiq
);
296 spin_unlock(&fiq
->lock
);
297 req
->out
.h
.error
= -ENOTCONN
;
298 clear_bit(FR_PENDING
, &req
->flags
);
299 fuse_request_end(req
);
303 const struct fuse_iqueue_ops fuse_dev_fiq_ops
= {
304 .send_forget
= fuse_dev_queue_forget
,
305 .send_interrupt
= fuse_dev_queue_interrupt
,
306 .send_req
= fuse_dev_queue_req
,
308 EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops
);
310 static void fuse_send_one(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
312 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
313 fuse_len_args(req
->args
->in_numargs
,
314 (struct fuse_arg
*) req
->args
->in_args
);
315 trace_fuse_request_send(req
);
316 fiq
->ops
->send_req(fiq
, req
);
319 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
320 u64 nodeid
, u64 nlookup
)
322 struct fuse_iqueue
*fiq
= &fc
->iq
;
324 forget
->forget_one
.nodeid
= nodeid
;
325 forget
->forget_one
.nlookup
= nlookup
;
327 fiq
->ops
->send_forget(fiq
, forget
);
330 static void flush_bg_queue(struct fuse_conn
*fc
)
332 struct fuse_iqueue
*fiq
= &fc
->iq
;
334 while (fc
->active_background
< fc
->max_background
&&
335 !list_empty(&fc
->bg_queue
)) {
336 struct fuse_req
*req
;
338 req
= list_first_entry(&fc
->bg_queue
, struct fuse_req
, list
);
339 list_del(&req
->list
);
340 fc
->active_background
++;
341 fuse_send_one(fiq
, req
);
346 * This function is called when a request is finished. Either a reply
347 * has arrived or it was aborted (and not yet sent) or some error
348 * occurred during communication with userspace, or the device file
349 * was closed. The requester thread is woken up (if still waiting),
350 * the 'end' callback is called if given, else the reference to the
351 * request is released
353 void fuse_request_end(struct fuse_req
*req
)
355 struct fuse_mount
*fm
= req
->fm
;
356 struct fuse_conn
*fc
= fm
->fc
;
357 struct fuse_iqueue
*fiq
= &fc
->iq
;
359 if (test_and_set_bit(FR_FINISHED
, &req
->flags
))
362 trace_fuse_request_end(req
);
364 * test_and_set_bit() implies smp_mb() between bit
365 * changing and below FR_INTERRUPTED check. Pairs with
366 * smp_mb() from queue_interrupt().
368 if (test_bit(FR_INTERRUPTED
, &req
->flags
)) {
369 spin_lock(&fiq
->lock
);
370 list_del_init(&req
->intr_entry
);
371 spin_unlock(&fiq
->lock
);
373 WARN_ON(test_bit(FR_PENDING
, &req
->flags
));
374 WARN_ON(test_bit(FR_SENT
, &req
->flags
));
375 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
376 spin_lock(&fc
->bg_lock
);
377 clear_bit(FR_BACKGROUND
, &req
->flags
);
378 if (fc
->num_background
== fc
->max_background
) {
380 wake_up(&fc
->blocked_waitq
);
381 } else if (!fc
->blocked
) {
383 * Wake up next waiter, if any. It's okay to use
384 * waitqueue_active(), as we've already synced up
385 * fc->blocked with waiters with the wake_up() call
388 if (waitqueue_active(&fc
->blocked_waitq
))
389 wake_up(&fc
->blocked_waitq
);
392 fc
->num_background
--;
393 fc
->active_background
--;
395 spin_unlock(&fc
->bg_lock
);
397 /* Wake up waiter sleeping in request_wait_answer() */
398 wake_up(&req
->waitq
);
401 if (test_bit(FR_ASYNC
, &req
->flags
))
402 req
->args
->end(fm
, req
->args
, req
->out
.h
.error
);
404 fuse_put_request(req
);
406 EXPORT_SYMBOL_GPL(fuse_request_end
);
408 static int queue_interrupt(struct fuse_req
*req
)
410 struct fuse_iqueue
*fiq
= &req
->fm
->fc
->iq
;
412 /* Check for we've sent request to interrupt this req */
413 if (unlikely(!test_bit(FR_INTERRUPTED
, &req
->flags
)))
416 fiq
->ops
->send_interrupt(fiq
, req
);
421 static void request_wait_answer(struct fuse_req
*req
)
423 struct fuse_conn
*fc
= req
->fm
->fc
;
424 struct fuse_iqueue
*fiq
= &fc
->iq
;
427 if (!fc
->no_interrupt
) {
428 /* Any signal may interrupt this */
429 err
= wait_event_interruptible(req
->waitq
,
430 test_bit(FR_FINISHED
, &req
->flags
));
434 set_bit(FR_INTERRUPTED
, &req
->flags
);
435 /* matches barrier in fuse_dev_do_read() */
436 smp_mb__after_atomic();
437 if (test_bit(FR_SENT
, &req
->flags
))
438 queue_interrupt(req
);
441 if (!test_bit(FR_FORCE
, &req
->flags
)) {
442 /* Only fatal signals may interrupt this */
443 err
= wait_event_killable(req
->waitq
,
444 test_bit(FR_FINISHED
, &req
->flags
));
448 spin_lock(&fiq
->lock
);
449 /* Request is not yet in userspace, bail out */
450 if (test_bit(FR_PENDING
, &req
->flags
)) {
451 list_del(&req
->list
);
452 spin_unlock(&fiq
->lock
);
453 __fuse_put_request(req
);
454 req
->out
.h
.error
= -EINTR
;
457 spin_unlock(&fiq
->lock
);
461 * Either request is already in userspace, or it was forced.
464 wait_event(req
->waitq
, test_bit(FR_FINISHED
, &req
->flags
));
467 static void __fuse_request_send(struct fuse_req
*req
)
469 struct fuse_iqueue
*fiq
= &req
->fm
->fc
->iq
;
471 BUG_ON(test_bit(FR_BACKGROUND
, &req
->flags
));
473 /* acquire extra reference, since request is still needed after
474 fuse_request_end() */
475 __fuse_get_request(req
);
476 fuse_send_one(fiq
, req
);
478 request_wait_answer(req
);
479 /* Pairs with smp_wmb() in fuse_request_end() */
483 static void fuse_adjust_compat(struct fuse_conn
*fc
, struct fuse_args
*args
)
485 if (fc
->minor
< 4 && args
->opcode
== FUSE_STATFS
)
486 args
->out_args
[0].size
= FUSE_COMPAT_STATFS_SIZE
;
489 switch (args
->opcode
) {
496 args
->out_args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
500 args
->out_args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
504 if (fc
->minor
< 12) {
505 switch (args
->opcode
) {
507 args
->in_args
[0].size
= sizeof(struct fuse_open_in
);
510 args
->in_args
[0].size
= FUSE_COMPAT_MKNOD_IN_SIZE
;
516 static void fuse_force_creds(struct fuse_req
*req
)
518 struct fuse_conn
*fc
= req
->fm
->fc
;
520 if (!req
->fm
->sb
|| req
->fm
->sb
->s_iflags
& SB_I_NOIDMAP
) {
521 req
->in
.h
.uid
= from_kuid_munged(fc
->user_ns
, current_fsuid());
522 req
->in
.h
.gid
= from_kgid_munged(fc
->user_ns
, current_fsgid());
524 req
->in
.h
.uid
= FUSE_INVALID_UIDGID
;
525 req
->in
.h
.gid
= FUSE_INVALID_UIDGID
;
528 req
->in
.h
.pid
= pid_nr_ns(task_pid(current
), fc
->pid_ns
);
531 static void fuse_args_to_req(struct fuse_req
*req
, struct fuse_args
*args
)
533 req
->in
.h
.opcode
= args
->opcode
;
534 req
->in
.h
.nodeid
= args
->nodeid
;
537 req
->in
.h
.total_extlen
= args
->in_args
[args
->ext_idx
].size
/ 8;
539 __set_bit(FR_ASYNC
, &req
->flags
);
542 ssize_t
__fuse_simple_request(struct mnt_idmap
*idmap
,
543 struct fuse_mount
*fm
,
544 struct fuse_args
*args
)
546 struct fuse_conn
*fc
= fm
->fc
;
547 struct fuse_req
*req
;
551 atomic_inc(&fc
->num_waiting
);
552 req
= fuse_request_alloc(fm
, GFP_KERNEL
| __GFP_NOFAIL
);
555 fuse_force_creds(req
);
557 __set_bit(FR_WAITING
, &req
->flags
);
558 __set_bit(FR_FORCE
, &req
->flags
);
560 WARN_ON(args
->nocreds
);
561 req
= fuse_get_req(idmap
, fm
, false);
566 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
567 fuse_adjust_compat(fc
, args
);
568 fuse_args_to_req(req
, args
);
571 __set_bit(FR_ISREPLY
, &req
->flags
);
572 __fuse_request_send(req
);
573 ret
= req
->out
.h
.error
;
574 if (!ret
&& args
->out_argvar
) {
575 BUG_ON(args
->out_numargs
== 0);
576 ret
= args
->out_args
[args
->out_numargs
- 1].size
;
578 fuse_put_request(req
);
583 static bool fuse_request_queue_background(struct fuse_req
*req
)
585 struct fuse_mount
*fm
= req
->fm
;
586 struct fuse_conn
*fc
= fm
->fc
;
589 WARN_ON(!test_bit(FR_BACKGROUND
, &req
->flags
));
590 if (!test_bit(FR_WAITING
, &req
->flags
)) {
591 __set_bit(FR_WAITING
, &req
->flags
);
592 atomic_inc(&fc
->num_waiting
);
594 __set_bit(FR_ISREPLY
, &req
->flags
);
595 spin_lock(&fc
->bg_lock
);
596 if (likely(fc
->connected
)) {
597 fc
->num_background
++;
598 if (fc
->num_background
== fc
->max_background
)
600 list_add_tail(&req
->list
, &fc
->bg_queue
);
604 spin_unlock(&fc
->bg_lock
);
609 int fuse_simple_background(struct fuse_mount
*fm
, struct fuse_args
*args
,
612 struct fuse_req
*req
;
615 WARN_ON(!args
->nocreds
);
616 req
= fuse_request_alloc(fm
, gfp_flags
);
619 __set_bit(FR_BACKGROUND
, &req
->flags
);
621 WARN_ON(args
->nocreds
);
622 req
= fuse_get_req(&invalid_mnt_idmap
, fm
, true);
627 fuse_args_to_req(req
, args
);
629 if (!fuse_request_queue_background(req
)) {
630 fuse_put_request(req
);
636 EXPORT_SYMBOL_GPL(fuse_simple_background
);
638 static int fuse_simple_notify_reply(struct fuse_mount
*fm
,
639 struct fuse_args
*args
, u64 unique
)
641 struct fuse_req
*req
;
642 struct fuse_iqueue
*fiq
= &fm
->fc
->iq
;
644 req
= fuse_get_req(&invalid_mnt_idmap
, fm
, false);
648 __clear_bit(FR_ISREPLY
, &req
->flags
);
649 req
->in
.h
.unique
= unique
;
651 fuse_args_to_req(req
, args
);
653 fuse_send_one(fiq
, req
);
659 * Lock the request. Up to the next unlock_request() there mustn't be
660 * anything that could cause a page-fault. If the request was already
663 static int lock_request(struct fuse_req
*req
)
667 spin_lock(&req
->waitq
.lock
);
668 if (test_bit(FR_ABORTED
, &req
->flags
))
671 set_bit(FR_LOCKED
, &req
->flags
);
672 spin_unlock(&req
->waitq
.lock
);
678 * Unlock request. If it was aborted while locked, caller is responsible
679 * for unlocking and ending the request.
681 static int unlock_request(struct fuse_req
*req
)
685 spin_lock(&req
->waitq
.lock
);
686 if (test_bit(FR_ABORTED
, &req
->flags
))
689 clear_bit(FR_LOCKED
, &req
->flags
);
690 spin_unlock(&req
->waitq
.lock
);
695 struct fuse_copy_state
{
697 struct fuse_req
*req
;
698 struct iov_iter
*iter
;
699 struct pipe_buffer
*pipebufs
;
700 struct pipe_buffer
*currbuf
;
701 struct pipe_inode_info
*pipe
;
702 unsigned long nr_segs
;
706 unsigned move_pages
:1;
709 static void fuse_copy_init(struct fuse_copy_state
*cs
, int write
,
710 struct iov_iter
*iter
)
712 memset(cs
, 0, sizeof(*cs
));
717 /* Unmap and put previous page of userspace buffer */
718 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
721 struct pipe_buffer
*buf
= cs
->currbuf
;
724 buf
->len
= PAGE_SIZE
- cs
->len
;
728 flush_dcache_page(cs
->pg
);
729 set_page_dirty_lock(cs
->pg
);
737 * Get another pagefull of userspace buffer, and map it to kernel
738 * address space, and lock request
740 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
745 err
= unlock_request(cs
->req
);
749 fuse_copy_finish(cs
);
751 struct pipe_buffer
*buf
= cs
->pipebufs
;
754 err
= pipe_buf_confirm(cs
->pipe
, buf
);
758 BUG_ON(!cs
->nr_segs
);
761 cs
->offset
= buf
->offset
;
766 if (cs
->nr_segs
>= cs
->pipe
->max_usage
)
769 page
= alloc_page(GFP_HIGHUSER
);
786 err
= iov_iter_get_pages2(cs
->iter
, &page
, PAGE_SIZE
, 1, &off
);
795 return lock_request(cs
->req
);
798 /* Do as much copy to/from userspace buffer as we can */
799 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
801 unsigned ncpy
= min(*size
, cs
->len
);
803 void *pgaddr
= kmap_local_page(cs
->pg
);
804 void *buf
= pgaddr
+ cs
->offset
;
807 memcpy(buf
, *val
, ncpy
);
809 memcpy(*val
, buf
, ncpy
);
811 kunmap_local(pgaddr
);
820 static int fuse_check_folio(struct folio
*folio
)
822 if (folio_mapped(folio
) ||
823 folio
->mapping
!= NULL
||
824 (folio
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
832 LRU_GEN_MASK
| LRU_REFS_MASK
))) {
833 dump_page(&folio
->page
, "fuse: trying to steal weird page");
839 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
842 struct folio
*oldfolio
= page_folio(*pagep
);
843 struct folio
*newfolio
;
844 struct pipe_buffer
*buf
= cs
->pipebufs
;
847 err
= unlock_request(cs
->req
);
851 fuse_copy_finish(cs
);
853 err
= pipe_buf_confirm(cs
->pipe
, buf
);
857 BUG_ON(!cs
->nr_segs
);
863 if (cs
->len
!= PAGE_SIZE
)
866 if (!pipe_buf_try_steal(cs
->pipe
, buf
))
869 newfolio
= page_folio(buf
->page
);
871 folio_clear_uptodate(newfolio
);
872 folio_clear_mappedtodisk(newfolio
);
874 if (fuse_check_folio(newfolio
) != 0)
875 goto out_fallback_unlock
;
878 * This is a new and locked page, it shouldn't be mapped or
879 * have any special flags on it
881 if (WARN_ON(folio_mapped(oldfolio
)))
882 goto out_fallback_unlock
;
883 if (WARN_ON(folio_has_private(oldfolio
)))
884 goto out_fallback_unlock
;
885 if (WARN_ON(folio_test_dirty(oldfolio
) ||
886 folio_test_writeback(oldfolio
)))
887 goto out_fallback_unlock
;
888 if (WARN_ON(folio_test_mlocked(oldfolio
)))
889 goto out_fallback_unlock
;
891 replace_page_cache_folio(oldfolio
, newfolio
);
895 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
896 folio_add_lru(newfolio
);
899 * Release while we have extra ref on stolen page. Otherwise
900 * anon_pipe_buf_release() might think the page can be reused.
902 pipe_buf_release(cs
->pipe
, buf
);
905 spin_lock(&cs
->req
->waitq
.lock
);
906 if (test_bit(FR_ABORTED
, &cs
->req
->flags
))
909 *pagep
= &newfolio
->page
;
910 spin_unlock(&cs
->req
->waitq
.lock
);
913 folio_unlock(newfolio
);
918 folio_unlock(oldfolio
);
919 /* Drop ref for ap->pages[] array */
925 /* Drop ref obtained in this function */
930 folio_unlock(newfolio
);
933 cs
->offset
= buf
->offset
;
935 err
= lock_request(cs
->req
);
942 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
943 unsigned offset
, unsigned count
)
945 struct pipe_buffer
*buf
;
948 if (cs
->nr_segs
>= cs
->pipe
->max_usage
)
952 err
= unlock_request(cs
->req
);
958 fuse_copy_finish(cs
);
962 buf
->offset
= offset
;
973 * Copy a page in the request to/from the userspace buffer. Must be
976 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
977 unsigned offset
, unsigned count
, int zeroing
)
980 struct page
*page
= *pagep
;
982 if (page
&& zeroing
&& count
< PAGE_SIZE
)
983 clear_highpage(page
);
986 if (cs
->write
&& cs
->pipebufs
&& page
) {
988 * Can't control lifetime of pipe buffers, so always
991 if (cs
->req
->args
->user_pages
) {
992 err
= fuse_copy_fill(cs
);
996 return fuse_ref_page(cs
, page
, offset
, count
);
998 } else if (!cs
->len
) {
999 if (cs
->move_pages
&& page
&&
1000 offset
== 0 && count
== PAGE_SIZE
) {
1001 err
= fuse_try_move_page(cs
, pagep
);
1005 err
= fuse_copy_fill(cs
);
1011 void *mapaddr
= kmap_local_page(page
);
1012 void *buf
= mapaddr
+ offset
;
1013 offset
+= fuse_copy_do(cs
, &buf
, &count
);
1014 kunmap_local(mapaddr
);
1016 offset
+= fuse_copy_do(cs
, NULL
, &count
);
1018 if (page
&& !cs
->write
)
1019 flush_dcache_page(page
);
1023 /* Copy pages in the request to/from userspace buffer */
1024 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
1028 struct fuse_req
*req
= cs
->req
;
1029 struct fuse_args_pages
*ap
= container_of(req
->args
, typeof(*ap
), args
);
1032 for (i
= 0; i
< ap
->num_pages
&& (nbytes
|| zeroing
); i
++) {
1034 unsigned int offset
= ap
->descs
[i
].offset
;
1035 unsigned int count
= min(nbytes
, ap
->descs
[i
].length
);
1037 err
= fuse_copy_page(cs
, &ap
->pages
[i
], offset
, count
, zeroing
);
1046 /* Copy a single argument in the request to/from userspace buffer */
1047 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
1051 int err
= fuse_copy_fill(cs
);
1055 fuse_copy_do(cs
, &val
, &size
);
1060 /* Copy request arguments to/from userspace buffer */
1061 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
1062 unsigned argpages
, struct fuse_arg
*args
,
1068 for (i
= 0; !err
&& i
< numargs
; i
++) {
1069 struct fuse_arg
*arg
= &args
[i
];
1070 if (i
== numargs
- 1 && argpages
)
1071 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1073 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1078 static int forget_pending(struct fuse_iqueue
*fiq
)
1080 return fiq
->forget_list_head
.next
!= NULL
;
1083 static int request_pending(struct fuse_iqueue
*fiq
)
1085 return !list_empty(&fiq
->pending
) || !list_empty(&fiq
->interrupts
) ||
1086 forget_pending(fiq
);
1090 * Transfer an interrupt request to userspace
1092 * Unlike other requests this is assembled on demand, without a need
1093 * to allocate a separate fuse_req structure.
1095 * Called with fiq->lock held, releases it
1097 static int fuse_read_interrupt(struct fuse_iqueue
*fiq
,
1098 struct fuse_copy_state
*cs
,
1099 size_t nbytes
, struct fuse_req
*req
)
1100 __releases(fiq
->lock
)
1102 struct fuse_in_header ih
;
1103 struct fuse_interrupt_in arg
;
1104 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1107 list_del_init(&req
->intr_entry
);
1108 memset(&ih
, 0, sizeof(ih
));
1109 memset(&arg
, 0, sizeof(arg
));
1111 ih
.opcode
= FUSE_INTERRUPT
;
1112 ih
.unique
= (req
->in
.h
.unique
| FUSE_INT_REQ_BIT
);
1113 arg
.unique
= req
->in
.h
.unique
;
1115 spin_unlock(&fiq
->lock
);
1116 if (nbytes
< reqsize
)
1119 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1121 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1122 fuse_copy_finish(cs
);
1124 return err
? err
: reqsize
;
1127 static struct fuse_forget_link
*fuse_dequeue_forget(struct fuse_iqueue
*fiq
,
1129 unsigned int *countp
)
1131 struct fuse_forget_link
*head
= fiq
->forget_list_head
.next
;
1132 struct fuse_forget_link
**newhead
= &head
;
1135 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1136 newhead
= &(*newhead
)->next
;
1138 fiq
->forget_list_head
.next
= *newhead
;
1140 if (fiq
->forget_list_head
.next
== NULL
)
1141 fiq
->forget_list_tail
= &fiq
->forget_list_head
;
1149 static int fuse_read_single_forget(struct fuse_iqueue
*fiq
,
1150 struct fuse_copy_state
*cs
,
1152 __releases(fiq
->lock
)
1155 struct fuse_forget_link
*forget
= fuse_dequeue_forget(fiq
, 1, NULL
);
1156 struct fuse_forget_in arg
= {
1157 .nlookup
= forget
->forget_one
.nlookup
,
1159 struct fuse_in_header ih
= {
1160 .opcode
= FUSE_FORGET
,
1161 .nodeid
= forget
->forget_one
.nodeid
,
1162 .unique
= fuse_get_unique_locked(fiq
),
1163 .len
= sizeof(ih
) + sizeof(arg
),
1166 spin_unlock(&fiq
->lock
);
1168 if (nbytes
< ih
.len
)
1171 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1173 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1174 fuse_copy_finish(cs
);
1182 static int fuse_read_batch_forget(struct fuse_iqueue
*fiq
,
1183 struct fuse_copy_state
*cs
, size_t nbytes
)
1184 __releases(fiq
->lock
)
1187 unsigned max_forgets
;
1189 struct fuse_forget_link
*head
;
1190 struct fuse_batch_forget_in arg
= { .count
= 0 };
1191 struct fuse_in_header ih
= {
1192 .opcode
= FUSE_BATCH_FORGET
,
1193 .unique
= fuse_get_unique_locked(fiq
),
1194 .len
= sizeof(ih
) + sizeof(arg
),
1197 if (nbytes
< ih
.len
) {
1198 spin_unlock(&fiq
->lock
);
1202 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1203 head
= fuse_dequeue_forget(fiq
, max_forgets
, &count
);
1204 spin_unlock(&fiq
->lock
);
1207 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1208 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1210 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1213 struct fuse_forget_link
*forget
= head
;
1216 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1217 sizeof(forget
->forget_one
));
1219 head
= forget
->next
;
1223 fuse_copy_finish(cs
);
1231 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_iqueue
*fiq
,
1232 struct fuse_copy_state
*cs
,
1234 __releases(fiq
->lock
)
1236 if (fc
->minor
< 16 || fiq
->forget_list_head
.next
->next
== NULL
)
1237 return fuse_read_single_forget(fiq
, cs
, nbytes
);
1239 return fuse_read_batch_forget(fiq
, cs
, nbytes
);
1243 * Read a single request into the userspace filesystem's buffer. This
1244 * function waits until a request is available, then removes it from
1245 * the pending list and copies request data to userspace buffer. If
1246 * no reply is needed (FORGET) or request has been aborted or there
1247 * was an error during the copying then it's finished by calling
1248 * fuse_request_end(). Otherwise add it to the processing list, and set
1251 static ssize_t
fuse_dev_do_read(struct fuse_dev
*fud
, struct file
*file
,
1252 struct fuse_copy_state
*cs
, size_t nbytes
)
1255 struct fuse_conn
*fc
= fud
->fc
;
1256 struct fuse_iqueue
*fiq
= &fc
->iq
;
1257 struct fuse_pqueue
*fpq
= &fud
->pq
;
1258 struct fuse_req
*req
;
1259 struct fuse_args
*args
;
1264 * Require sane minimum read buffer - that has capacity for fixed part
1265 * of any request header + negotiated max_write room for data.
1267 * Historically libfuse reserves 4K for fixed header room, but e.g.
1268 * GlusterFS reserves only 80 bytes
1270 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1272 * which is the absolute minimum any sane filesystem should be using
1275 if (nbytes
< max_t(size_t, FUSE_MIN_READ_BUFFER
,
1276 sizeof(struct fuse_in_header
) +
1277 sizeof(struct fuse_write_in
) +
1283 spin_lock(&fiq
->lock
);
1284 if (!fiq
->connected
|| request_pending(fiq
))
1286 spin_unlock(&fiq
->lock
);
1288 if (file
->f_flags
& O_NONBLOCK
)
1290 err
= wait_event_interruptible_exclusive(fiq
->waitq
,
1291 !fiq
->connected
|| request_pending(fiq
));
1296 if (!fiq
->connected
) {
1297 err
= fc
->aborted
? -ECONNABORTED
: -ENODEV
;
1301 if (!list_empty(&fiq
->interrupts
)) {
1302 req
= list_entry(fiq
->interrupts
.next
, struct fuse_req
,
1304 return fuse_read_interrupt(fiq
, cs
, nbytes
, req
);
1307 if (forget_pending(fiq
)) {
1308 if (list_empty(&fiq
->pending
) || fiq
->forget_batch
-- > 0)
1309 return fuse_read_forget(fc
, fiq
, cs
, nbytes
);
1311 if (fiq
->forget_batch
<= -8)
1312 fiq
->forget_batch
= 16;
1315 req
= list_entry(fiq
->pending
.next
, struct fuse_req
, list
);
1316 clear_bit(FR_PENDING
, &req
->flags
);
1317 list_del_init(&req
->list
);
1318 spin_unlock(&fiq
->lock
);
1321 reqsize
= req
->in
.h
.len
;
1323 /* If request is too large, reply with an error and restart the read */
1324 if (nbytes
< reqsize
) {
1325 req
->out
.h
.error
= -EIO
;
1326 /* SETXATTR is special, since it may contain too large data */
1327 if (args
->opcode
== FUSE_SETXATTR
)
1328 req
->out
.h
.error
= -E2BIG
;
1329 fuse_request_end(req
);
1332 spin_lock(&fpq
->lock
);
1334 * Must not put request on fpq->io queue after having been shut down by
1337 if (!fpq
->connected
) {
1338 req
->out
.h
.error
= err
= -ECONNABORTED
;
1342 list_add(&req
->list
, &fpq
->io
);
1343 spin_unlock(&fpq
->lock
);
1345 err
= fuse_copy_one(cs
, &req
->in
.h
, sizeof(req
->in
.h
));
1347 err
= fuse_copy_args(cs
, args
->in_numargs
, args
->in_pages
,
1348 (struct fuse_arg
*) args
->in_args
, 0);
1349 fuse_copy_finish(cs
);
1350 spin_lock(&fpq
->lock
);
1351 clear_bit(FR_LOCKED
, &req
->flags
);
1352 if (!fpq
->connected
) {
1353 err
= fc
->aborted
? -ECONNABORTED
: -ENODEV
;
1357 req
->out
.h
.error
= -EIO
;
1360 if (!test_bit(FR_ISREPLY
, &req
->flags
)) {
1364 hash
= fuse_req_hash(req
->in
.h
.unique
);
1365 list_move_tail(&req
->list
, &fpq
->processing
[hash
]);
1366 __fuse_get_request(req
);
1367 set_bit(FR_SENT
, &req
->flags
);
1368 spin_unlock(&fpq
->lock
);
1369 /* matches barrier in request_wait_answer() */
1370 smp_mb__after_atomic();
1371 if (test_bit(FR_INTERRUPTED
, &req
->flags
))
1372 queue_interrupt(req
);
1373 fuse_put_request(req
);
1378 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1379 list_del_init(&req
->list
);
1380 spin_unlock(&fpq
->lock
);
1381 fuse_request_end(req
);
1385 spin_unlock(&fiq
->lock
);
1389 static int fuse_dev_open(struct inode
*inode
, struct file
*file
)
1392 * The fuse device's file's private_data is used to hold
1393 * the fuse_conn(ection) when it is mounted, and is used to
1394 * keep track of whether the file has been mounted already.
1396 file
->private_data
= NULL
;
1400 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, struct iov_iter
*to
)
1402 struct fuse_copy_state cs
;
1403 struct file
*file
= iocb
->ki_filp
;
1404 struct fuse_dev
*fud
= fuse_get_dev(file
);
1409 if (!user_backed_iter(to
))
1412 fuse_copy_init(&cs
, 1, to
);
1414 return fuse_dev_do_read(fud
, file
, &cs
, iov_iter_count(to
));
1417 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1418 struct pipe_inode_info
*pipe
,
1419 size_t len
, unsigned int flags
)
1423 struct pipe_buffer
*bufs
;
1424 struct fuse_copy_state cs
;
1425 struct fuse_dev
*fud
= fuse_get_dev(in
);
1430 bufs
= kvmalloc_array(pipe
->max_usage
, sizeof(struct pipe_buffer
),
1435 fuse_copy_init(&cs
, 1, NULL
);
1438 ret
= fuse_dev_do_read(fud
, in
, &cs
, len
);
1442 if (pipe_occupancy(pipe
->head
, pipe
->tail
) + cs
.nr_segs
> pipe
->max_usage
) {
1447 for (ret
= total
= 0; page_nr
< cs
.nr_segs
; total
+= ret
) {
1449 * Need to be careful about this. Having buf->ops in module
1450 * code can Oops if the buffer persists after module unload.
1452 bufs
[page_nr
].ops
= &nosteal_pipe_buf_ops
;
1453 bufs
[page_nr
].flags
= 0;
1454 ret
= add_to_pipe(pipe
, &bufs
[page_nr
++]);
1455 if (unlikely(ret
< 0))
1461 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1462 put_page(bufs
[page_nr
].page
);
1468 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1469 struct fuse_copy_state
*cs
)
1471 struct fuse_notify_poll_wakeup_out outarg
;
1474 if (size
!= sizeof(outarg
))
1477 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1481 fuse_copy_finish(cs
);
1482 return fuse_notify_poll_wakeup(fc
, &outarg
);
1485 fuse_copy_finish(cs
);
1489 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1490 struct fuse_copy_state
*cs
)
1492 struct fuse_notify_inval_inode_out outarg
;
1495 if (size
!= sizeof(outarg
))
1498 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1501 fuse_copy_finish(cs
);
1503 down_read(&fc
->killsb
);
1504 err
= fuse_reverse_inval_inode(fc
, outarg
.ino
,
1505 outarg
.off
, outarg
.len
);
1506 up_read(&fc
->killsb
);
1510 fuse_copy_finish(cs
);
1514 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1515 struct fuse_copy_state
*cs
)
1517 struct fuse_notify_inval_entry_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
, 0, &name
, outarg
.flags
);
1552 up_read(&fc
->killsb
);
1558 fuse_copy_finish(cs
);
1562 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1563 struct fuse_copy_state
*cs
)
1565 struct fuse_notify_delete_out outarg
;
1570 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1575 if (size
< sizeof(outarg
))
1578 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1582 err
= -ENAMETOOLONG
;
1583 if (outarg
.namelen
> FUSE_NAME_MAX
)
1587 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1591 name
.len
= outarg
.namelen
;
1592 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1595 fuse_copy_finish(cs
);
1596 buf
[outarg
.namelen
] = 0;
1598 down_read(&fc
->killsb
);
1599 err
= fuse_reverse_inval_entry(fc
, outarg
.parent
, outarg
.child
, &name
, 0);
1600 up_read(&fc
->killsb
);
1606 fuse_copy_finish(cs
);
1610 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1611 struct fuse_copy_state
*cs
)
1613 struct fuse_notify_store_out outarg
;
1614 struct inode
*inode
;
1615 struct address_space
*mapping
;
1619 unsigned int offset
;
1625 if (size
< sizeof(outarg
))
1628 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1633 if (size
- sizeof(outarg
) != outarg
.size
)
1636 nodeid
= outarg
.nodeid
;
1638 down_read(&fc
->killsb
);
1641 inode
= fuse_ilookup(fc
, nodeid
, NULL
);
1645 mapping
= inode
->i_mapping
;
1646 index
= outarg
.offset
>> PAGE_SHIFT
;
1647 offset
= outarg
.offset
& ~PAGE_MASK
;
1648 file_size
= i_size_read(inode
);
1649 end
= outarg
.offset
+ outarg
.size
;
1650 if (end
> file_size
) {
1652 fuse_write_update_attr(inode
, file_size
, outarg
.size
);
1658 unsigned int this_num
;
1661 page
= find_or_create_page(mapping
, index
,
1662 mapping_gfp_mask(mapping
));
1666 this_num
= min_t(unsigned, num
, PAGE_SIZE
- offset
);
1667 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1668 if (!PageUptodate(page
) && !err
&& offset
== 0 &&
1669 (this_num
== PAGE_SIZE
|| file_size
== end
)) {
1670 zero_user_segment(page
, this_num
, PAGE_SIZE
);
1671 SetPageUptodate(page
);
1689 up_read(&fc
->killsb
);
1691 fuse_copy_finish(cs
);
1695 struct fuse_retrieve_args
{
1696 struct fuse_args_pages ap
;
1697 struct fuse_notify_retrieve_in inarg
;
1700 static void fuse_retrieve_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
1703 struct fuse_retrieve_args
*ra
=
1704 container_of(args
, typeof(*ra
), ap
.args
);
1706 release_pages(ra
->ap
.pages
, ra
->ap
.num_pages
);
1710 static int fuse_retrieve(struct fuse_mount
*fm
, struct inode
*inode
,
1711 struct fuse_notify_retrieve_out
*outarg
)
1714 struct address_space
*mapping
= inode
->i_mapping
;
1718 unsigned int offset
;
1719 size_t total_len
= 0;
1720 unsigned int num_pages
;
1721 struct fuse_conn
*fc
= fm
->fc
;
1722 struct fuse_retrieve_args
*ra
;
1723 size_t args_size
= sizeof(*ra
);
1724 struct fuse_args_pages
*ap
;
1725 struct fuse_args
*args
;
1727 offset
= outarg
->offset
& ~PAGE_MASK
;
1728 file_size
= i_size_read(inode
);
1730 num
= min(outarg
->size
, fc
->max_write
);
1731 if (outarg
->offset
> file_size
)
1733 else if (outarg
->offset
+ num
> file_size
)
1734 num
= file_size
- outarg
->offset
;
1736 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1737 num_pages
= min(num_pages
, fc
->max_pages
);
1739 args_size
+= num_pages
* (sizeof(ap
->pages
[0]) + sizeof(ap
->descs
[0]));
1741 ra
= kzalloc(args_size
, GFP_KERNEL
);
1746 ap
->pages
= (void *) (ra
+ 1);
1747 ap
->descs
= (void *) (ap
->pages
+ num_pages
);
1750 args
->nodeid
= outarg
->nodeid
;
1751 args
->opcode
= FUSE_NOTIFY_REPLY
;
1752 args
->in_numargs
= 2;
1753 args
->in_pages
= true;
1754 args
->end
= fuse_retrieve_end
;
1756 index
= outarg
->offset
>> PAGE_SHIFT
;
1758 while (num
&& ap
->num_pages
< num_pages
) {
1760 unsigned int this_num
;
1762 page
= find_get_page(mapping
, index
);
1766 this_num
= min_t(unsigned, num
, PAGE_SIZE
- offset
);
1767 ap
->pages
[ap
->num_pages
] = page
;
1768 ap
->descs
[ap
->num_pages
].offset
= offset
;
1769 ap
->descs
[ap
->num_pages
].length
= this_num
;
1774 total_len
+= this_num
;
1777 ra
->inarg
.offset
= outarg
->offset
;
1778 ra
->inarg
.size
= total_len
;
1779 args
->in_args
[0].size
= sizeof(ra
->inarg
);
1780 args
->in_args
[0].value
= &ra
->inarg
;
1781 args
->in_args
[1].size
= total_len
;
1783 err
= fuse_simple_notify_reply(fm
, args
, outarg
->notify_unique
);
1785 fuse_retrieve_end(fm
, args
, err
);
1790 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1791 struct fuse_copy_state
*cs
)
1793 struct fuse_notify_retrieve_out outarg
;
1794 struct fuse_mount
*fm
;
1795 struct inode
*inode
;
1800 if (size
!= sizeof(outarg
))
1803 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1807 fuse_copy_finish(cs
);
1809 down_read(&fc
->killsb
);
1811 nodeid
= outarg
.nodeid
;
1813 inode
= fuse_ilookup(fc
, nodeid
, &fm
);
1815 err
= fuse_retrieve(fm
, inode
, &outarg
);
1818 up_read(&fc
->killsb
);
1823 fuse_copy_finish(cs
);
1828 * Resending all processing queue requests.
1830 * During a FUSE daemon panics and failover, it is possible for some inflight
1831 * requests to be lost and never returned. As a result, applications awaiting
1832 * replies would become stuck forever. To address this, we can use notification
1833 * to trigger resending of these pending requests to the FUSE daemon, ensuring
1834 * they are properly processed again.
1836 * Please note that this strategy is applicable only to idempotent requests or
1837 * if the FUSE daemon takes careful measures to avoid processing duplicated
1838 * non-idempotent requests.
1840 static void fuse_resend(struct fuse_conn
*fc
)
1842 struct fuse_dev
*fud
;
1843 struct fuse_req
*req
, *next
;
1844 struct fuse_iqueue
*fiq
= &fc
->iq
;
1845 LIST_HEAD(to_queue
);
1848 spin_lock(&fc
->lock
);
1849 if (!fc
->connected
) {
1850 spin_unlock(&fc
->lock
);
1854 list_for_each_entry(fud
, &fc
->devices
, entry
) {
1855 struct fuse_pqueue
*fpq
= &fud
->pq
;
1857 spin_lock(&fpq
->lock
);
1858 for (i
= 0; i
< FUSE_PQ_HASH_SIZE
; i
++)
1859 list_splice_tail_init(&fpq
->processing
[i
], &to_queue
);
1860 spin_unlock(&fpq
->lock
);
1862 spin_unlock(&fc
->lock
);
1864 list_for_each_entry_safe(req
, next
, &to_queue
, list
) {
1865 set_bit(FR_PENDING
, &req
->flags
);
1866 clear_bit(FR_SENT
, &req
->flags
);
1867 /* mark the request as resend request */
1868 req
->in
.h
.unique
|= FUSE_UNIQUE_RESEND
;
1871 spin_lock(&fiq
->lock
);
1872 if (!fiq
->connected
) {
1873 spin_unlock(&fiq
->lock
);
1874 list_for_each_entry(req
, &to_queue
, list
)
1875 clear_bit(FR_PENDING
, &req
->flags
);
1876 end_requests(&to_queue
);
1879 /* iq and pq requests are both oldest to newest */
1880 list_splice(&to_queue
, &fiq
->pending
);
1881 fuse_dev_wake_and_unlock(fiq
);
1884 static int fuse_notify_resend(struct fuse_conn
*fc
)
1890 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1891 unsigned int size
, struct fuse_copy_state
*cs
)
1893 /* Don't try to move pages (yet) */
1897 case FUSE_NOTIFY_POLL
:
1898 return fuse_notify_poll(fc
, size
, cs
);
1900 case FUSE_NOTIFY_INVAL_INODE
:
1901 return fuse_notify_inval_inode(fc
, size
, cs
);
1903 case FUSE_NOTIFY_INVAL_ENTRY
:
1904 return fuse_notify_inval_entry(fc
, size
, cs
);
1906 case FUSE_NOTIFY_STORE
:
1907 return fuse_notify_store(fc
, size
, cs
);
1909 case FUSE_NOTIFY_RETRIEVE
:
1910 return fuse_notify_retrieve(fc
, size
, cs
);
1912 case FUSE_NOTIFY_DELETE
:
1913 return fuse_notify_delete(fc
, size
, cs
);
1915 case FUSE_NOTIFY_RESEND
:
1916 return fuse_notify_resend(fc
);
1919 fuse_copy_finish(cs
);
1924 /* Look up request on processing list by unique ID */
1925 static struct fuse_req
*request_find(struct fuse_pqueue
*fpq
, u64 unique
)
1927 unsigned int hash
= fuse_req_hash(unique
);
1928 struct fuse_req
*req
;
1930 list_for_each_entry(req
, &fpq
->processing
[hash
], list
) {
1931 if (req
->in
.h
.unique
== unique
)
1937 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_args
*args
,
1940 unsigned reqsize
= sizeof(struct fuse_out_header
);
1942 reqsize
+= fuse_len_args(args
->out_numargs
, args
->out_args
);
1944 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !args
->out_argvar
))
1946 else if (reqsize
> nbytes
) {
1947 struct fuse_arg
*lastarg
= &args
->out_args
[args
->out_numargs
-1];
1948 unsigned diffsize
= reqsize
- nbytes
;
1950 if (diffsize
> lastarg
->size
)
1952 lastarg
->size
-= diffsize
;
1954 return fuse_copy_args(cs
, args
->out_numargs
, args
->out_pages
,
1955 args
->out_args
, args
->page_zeroing
);
1959 * Write a single reply to a request. First the header is copied from
1960 * the write buffer. The request is then searched on the processing
1961 * list by the unique ID found in the header. If found, then remove
1962 * it from the list and copy the rest of the buffer to the request.
1963 * The request is finished by calling fuse_request_end().
1965 static ssize_t
fuse_dev_do_write(struct fuse_dev
*fud
,
1966 struct fuse_copy_state
*cs
, size_t nbytes
)
1969 struct fuse_conn
*fc
= fud
->fc
;
1970 struct fuse_pqueue
*fpq
= &fud
->pq
;
1971 struct fuse_req
*req
;
1972 struct fuse_out_header oh
;
1975 if (nbytes
< sizeof(struct fuse_out_header
))
1978 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1983 if (oh
.len
!= nbytes
)
1987 * Zero oh.unique indicates unsolicited notification message
1988 * and error contains notification code.
1991 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1996 if (oh
.error
<= -512 || oh
.error
> 0)
1999 spin_lock(&fpq
->lock
);
2002 req
= request_find(fpq
, oh
.unique
& ~FUSE_INT_REQ_BIT
);
2006 spin_unlock(&fpq
->lock
);
2010 /* Is it an interrupt reply ID? */
2011 if (oh
.unique
& FUSE_INT_REQ_BIT
) {
2012 __fuse_get_request(req
);
2013 spin_unlock(&fpq
->lock
);
2016 if (nbytes
!= sizeof(struct fuse_out_header
))
2018 else if (oh
.error
== -ENOSYS
)
2019 fc
->no_interrupt
= 1;
2020 else if (oh
.error
== -EAGAIN
)
2021 err
= queue_interrupt(req
);
2023 fuse_put_request(req
);
2028 clear_bit(FR_SENT
, &req
->flags
);
2029 list_move(&req
->list
, &fpq
->io
);
2031 set_bit(FR_LOCKED
, &req
->flags
);
2032 spin_unlock(&fpq
->lock
);
2034 if (!req
->args
->page_replace
)
2038 err
= nbytes
!= sizeof(oh
) ? -EINVAL
: 0;
2040 err
= copy_out_args(cs
, req
->args
, nbytes
);
2041 fuse_copy_finish(cs
);
2043 spin_lock(&fpq
->lock
);
2044 clear_bit(FR_LOCKED
, &req
->flags
);
2045 if (!fpq
->connected
)
2048 req
->out
.h
.error
= -EIO
;
2049 if (!test_bit(FR_PRIVATE
, &req
->flags
))
2050 list_del_init(&req
->list
);
2051 spin_unlock(&fpq
->lock
);
2053 fuse_request_end(req
);
2055 return err
? err
: nbytes
;
2058 fuse_copy_finish(cs
);
2062 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, struct iov_iter
*from
)
2064 struct fuse_copy_state cs
;
2065 struct fuse_dev
*fud
= fuse_get_dev(iocb
->ki_filp
);
2070 if (!user_backed_iter(from
))
2073 fuse_copy_init(&cs
, 0, from
);
2075 return fuse_dev_do_write(fud
, &cs
, iov_iter_count(from
));
2078 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
2079 struct file
*out
, loff_t
*ppos
,
2080 size_t len
, unsigned int flags
)
2082 unsigned int head
, tail
, mask
, count
;
2085 struct pipe_buffer
*bufs
;
2086 struct fuse_copy_state cs
;
2087 struct fuse_dev
*fud
;
2091 fud
= fuse_get_dev(out
);
2099 mask
= pipe
->ring_size
- 1;
2100 count
= head
- tail
;
2102 bufs
= kvmalloc_array(count
, sizeof(struct pipe_buffer
), GFP_KERNEL
);
2110 for (idx
= tail
; idx
!= head
&& rem
< len
; idx
++)
2111 rem
+= pipe
->bufs
[idx
& mask
].len
;
2119 struct pipe_buffer
*ibuf
;
2120 struct pipe_buffer
*obuf
;
2122 if (WARN_ON(nbuf
>= count
|| tail
== head
))
2125 ibuf
= &pipe
->bufs
[tail
& mask
];
2128 if (rem
>= ibuf
->len
) {
2134 if (!pipe_buf_get(pipe
, ibuf
))
2138 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
2140 ibuf
->offset
+= obuf
->len
;
2141 ibuf
->len
-= obuf
->len
;
2148 fuse_copy_init(&cs
, 0, NULL
);
2153 if (flags
& SPLICE_F_MOVE
)
2156 ret
= fuse_dev_do_write(fud
, &cs
, len
);
2160 for (idx
= 0; idx
< nbuf
; idx
++) {
2161 struct pipe_buffer
*buf
= &bufs
[idx
];
2164 pipe_buf_release(pipe
, buf
);
2172 static __poll_t
fuse_dev_poll(struct file
*file
, poll_table
*wait
)
2174 __poll_t mask
= EPOLLOUT
| EPOLLWRNORM
;
2175 struct fuse_iqueue
*fiq
;
2176 struct fuse_dev
*fud
= fuse_get_dev(file
);
2182 poll_wait(file
, &fiq
->waitq
, wait
);
2184 spin_lock(&fiq
->lock
);
2185 if (!fiq
->connected
)
2187 else if (request_pending(fiq
))
2188 mask
|= EPOLLIN
| EPOLLRDNORM
;
2189 spin_unlock(&fiq
->lock
);
2194 /* Abort all requests on the given list (pending or processing) */
2195 static void end_requests(struct list_head
*head
)
2197 while (!list_empty(head
)) {
2198 struct fuse_req
*req
;
2199 req
= list_entry(head
->next
, struct fuse_req
, list
);
2200 req
->out
.h
.error
= -ECONNABORTED
;
2201 clear_bit(FR_SENT
, &req
->flags
);
2202 list_del_init(&req
->list
);
2203 fuse_request_end(req
);
2207 static void end_polls(struct fuse_conn
*fc
)
2211 p
= rb_first(&fc
->polled_files
);
2214 struct fuse_file
*ff
;
2215 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2216 wake_up_interruptible_all(&ff
->poll_wait
);
2223 * Abort all requests.
2225 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2228 * The same effect is usually achievable through killing the filesystem daemon
2229 * and all users of the filesystem. The exception is the combination of an
2230 * asynchronous request and the tricky deadlock (see
2231 * Documentation/filesystems/fuse.rst).
2233 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2234 * requests, they should be finished off immediately. Locked requests will be
2235 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2236 * requests. It is possible that some request will finish before we can. This
2237 * is OK, the request will in that case be removed from the list before we touch
2240 void fuse_abort_conn(struct fuse_conn
*fc
)
2242 struct fuse_iqueue
*fiq
= &fc
->iq
;
2244 spin_lock(&fc
->lock
);
2245 if (fc
->connected
) {
2246 struct fuse_dev
*fud
;
2247 struct fuse_req
*req
, *next
;
2251 /* Background queuing checks fc->connected under bg_lock */
2252 spin_lock(&fc
->bg_lock
);
2254 spin_unlock(&fc
->bg_lock
);
2256 fuse_set_initialized(fc
);
2257 list_for_each_entry(fud
, &fc
->devices
, entry
) {
2258 struct fuse_pqueue
*fpq
= &fud
->pq
;
2260 spin_lock(&fpq
->lock
);
2262 list_for_each_entry_safe(req
, next
, &fpq
->io
, list
) {
2263 req
->out
.h
.error
= -ECONNABORTED
;
2264 spin_lock(&req
->waitq
.lock
);
2265 set_bit(FR_ABORTED
, &req
->flags
);
2266 if (!test_bit(FR_LOCKED
, &req
->flags
)) {
2267 set_bit(FR_PRIVATE
, &req
->flags
);
2268 __fuse_get_request(req
);
2269 list_move(&req
->list
, &to_end
);
2271 spin_unlock(&req
->waitq
.lock
);
2273 for (i
= 0; i
< FUSE_PQ_HASH_SIZE
; i
++)
2274 list_splice_tail_init(&fpq
->processing
[i
],
2276 spin_unlock(&fpq
->lock
);
2278 spin_lock(&fc
->bg_lock
);
2280 fc
->max_background
= UINT_MAX
;
2282 spin_unlock(&fc
->bg_lock
);
2284 spin_lock(&fiq
->lock
);
2286 list_for_each_entry(req
, &fiq
->pending
, list
)
2287 clear_bit(FR_PENDING
, &req
->flags
);
2288 list_splice_tail_init(&fiq
->pending
, &to_end
);
2289 while (forget_pending(fiq
))
2290 kfree(fuse_dequeue_forget(fiq
, 1, NULL
));
2291 wake_up_all(&fiq
->waitq
);
2292 spin_unlock(&fiq
->lock
);
2293 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
2295 wake_up_all(&fc
->blocked_waitq
);
2296 spin_unlock(&fc
->lock
);
2298 end_requests(&to_end
);
2300 spin_unlock(&fc
->lock
);
2303 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2305 void fuse_wait_aborted(struct fuse_conn
*fc
)
2307 /* matches implicit memory barrier in fuse_drop_waiting() */
2309 wait_event(fc
->blocked_waitq
, atomic_read(&fc
->num_waiting
) == 0);
2312 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2314 struct fuse_dev
*fud
= fuse_get_dev(file
);
2317 struct fuse_conn
*fc
= fud
->fc
;
2318 struct fuse_pqueue
*fpq
= &fud
->pq
;
2322 spin_lock(&fpq
->lock
);
2323 WARN_ON(!list_empty(&fpq
->io
));
2324 for (i
= 0; i
< FUSE_PQ_HASH_SIZE
; i
++)
2325 list_splice_init(&fpq
->processing
[i
], &to_end
);
2326 spin_unlock(&fpq
->lock
);
2328 end_requests(&to_end
);
2330 /* Are we the last open device? */
2331 if (atomic_dec_and_test(&fc
->dev_count
)) {
2332 WARN_ON(fc
->iq
.fasync
!= NULL
);
2333 fuse_abort_conn(fc
);
2339 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2341 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2343 struct fuse_dev
*fud
= fuse_get_dev(file
);
2348 /* No locking - fasync_helper does its own locking */
2349 return fasync_helper(fd
, file
, on
, &fud
->fc
->iq
.fasync
);
2352 static int fuse_device_clone(struct fuse_conn
*fc
, struct file
*new)
2354 struct fuse_dev
*fud
;
2356 if (new->private_data
)
2359 fud
= fuse_dev_alloc_install(fc
);
2363 new->private_data
= fud
;
2364 atomic_inc(&fc
->dev_count
);
2369 static long fuse_dev_ioctl_clone(struct file
*file
, __u32 __user
*argp
)
2373 struct fuse_dev
*fud
= NULL
;
2376 if (get_user(oldfd
, argp
))
2384 * Check against file->f_op because CUSE
2385 * uses the same ioctl handler.
2387 if (fd_file(f
)->f_op
== file
->f_op
)
2388 fud
= fuse_get_dev(fd_file(f
));
2392 mutex_lock(&fuse_mutex
);
2393 res
= fuse_device_clone(fud
->fc
, file
);
2394 mutex_unlock(&fuse_mutex
);
2401 static long fuse_dev_ioctl_backing_open(struct file
*file
,
2402 struct fuse_backing_map __user
*argp
)
2404 struct fuse_dev
*fud
= fuse_get_dev(file
);
2405 struct fuse_backing_map map
;
2410 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH
))
2413 if (copy_from_user(&map
, argp
, sizeof(map
)))
2416 return fuse_backing_open(fud
->fc
, &map
);
2419 static long fuse_dev_ioctl_backing_close(struct file
*file
, __u32 __user
*argp
)
2421 struct fuse_dev
*fud
= fuse_get_dev(file
);
2427 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH
))
2430 if (get_user(backing_id
, argp
))
2433 return fuse_backing_close(fud
->fc
, backing_id
);
2436 static long fuse_dev_ioctl(struct file
*file
, unsigned int cmd
,
2439 void __user
*argp
= (void __user
*)arg
;
2442 case FUSE_DEV_IOC_CLONE
:
2443 return fuse_dev_ioctl_clone(file
, argp
);
2445 case FUSE_DEV_IOC_BACKING_OPEN
:
2446 return fuse_dev_ioctl_backing_open(file
, argp
);
2448 case FUSE_DEV_IOC_BACKING_CLOSE
:
2449 return fuse_dev_ioctl_backing_close(file
, argp
);
2456 const struct file_operations fuse_dev_operations
= {
2457 .owner
= THIS_MODULE
,
2458 .open
= fuse_dev_open
,
2459 .read_iter
= fuse_dev_read
,
2460 .splice_read
= fuse_dev_splice_read
,
2461 .write_iter
= fuse_dev_write
,
2462 .splice_write
= fuse_dev_splice_write
,
2463 .poll
= fuse_dev_poll
,
2464 .release
= fuse_dev_release
,
2465 .fasync
= fuse_dev_fasync
,
2466 .unlocked_ioctl
= fuse_dev_ioctl
,
2467 .compat_ioctl
= compat_ptr_ioctl
,
2469 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2471 static struct miscdevice fuse_miscdevice
= {
2472 .minor
= FUSE_MINOR
,
2474 .fops
= &fuse_dev_operations
,
2477 int __init
fuse_dev_init(void)
2480 fuse_req_cachep
= kmem_cache_create("fuse_request",
2481 sizeof(struct fuse_req
),
2483 if (!fuse_req_cachep
)
2486 err
= misc_register(&fuse_miscdevice
);
2488 goto out_cache_clean
;
2493 kmem_cache_destroy(fuse_req_cachep
);
2498 void fuse_dev_cleanup(void)
2500 misc_deregister(&fuse_miscdevice
);
2501 kmem_cache_destroy(fuse_req_cachep
);