projects/linux·blob master · refs

2460 lines · 56.5 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 FUSE: Filesystem in Userspace
4 Copyright (C) 2001-2008 Miklos Szeredi <[email protected]>
5*/
6
7#include "dev.h"
8#include "args.h"
9#include "dev_uring_i.h"
10
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>
24#include <linux/seq_file.h>
25
26#include "fuse_trace.h"
27
28MODULE_ALIAS_MISCDEV(FUSE_MINOR);
29MODULE_ALIAS("devname:fuse");
30
31static DECLARE_WAIT_QUEUE_HEAD(fuse_dev_waitq);
32
33static struct kmem_cache *fuse_req_cachep;
34
35static void fuse_request_init(struct fuse_chan *fch, struct fuse_req *req)
36{
37 INIT_LIST_HEAD(&req->list);
38 INIT_LIST_HEAD(&req->intr_entry);
39 init_waitqueue_head(&req->waitq);
40 refcount_set(&req->count, 1);
41 __set_bit(FR_PENDING, &req->flags);
42 req->chan = fch;
43 req->create_time = jiffies;
44}
45
46static struct fuse_req *fuse_request_alloc(struct fuse_chan *fch, gfp_t flags)
47{
48 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
49 if (req)
50 fuse_request_init(fch, req);
51
52 return req;
53}
54
55static void fuse_request_free(struct fuse_req *req)
56{
57 WARN_ON(!list_empty(&req->intr_entry));
58 kmem_cache_free(fuse_req_cachep, req);
59}
60
61static void __fuse_get_request(struct fuse_req *req)
62{
63 refcount_inc(&req->count);
64}
65
66/* Must be called with > 1 refcount */
67static void __fuse_put_request(struct fuse_req *req)
68{
69 refcount_dec(&req->count);
70}
71
72void fuse_chan_set_initialized(struct fuse_chan *fch, struct fuse_chan_param *param)
73{
74 if (param) {
75 fch->minor = param->minor;
76 fch->max_write = param->max_write;
77 fch->max_pages = param->max_pages;
78
79 if (param->io_uring_enabled)
80 fuse_uring_conn_init(fch);
81 }
82
83 /* Pairs with smp_load_acquire() readers of fch->initialized */
84 smp_store_release(&fch->initialized, 1);
85 wake_up_all(&fch->blocked_waitq);
86}
87
88static bool fuse_block_alloc(struct fuse_chan *fch, bool for_background)
89{
90 /* Pairs with smp_store_release() in fuse_chan_set_initialized() */
91 if (!smp_load_acquire(&fch->initialized))
92 return true;
93
94 return (for_background && fch->blocked) ||
95 (fch->io_uring && fch->connected && !fuse_uring_ready(fch));
96}
97
98static void fuse_drop_waiting(struct fuse_chan *fch)
99{
100 /*
101 * lockess check of fch->connected is okay, because atomic_dec_and_test()
102 * provides a memory barrier matched with the one in fuse_chan_wait_aborted()
103 * to ensure no wake-up is missed.
104 */
105 if (atomic_dec_and_test(&fch->num_waiting) &&
106 !READ_ONCE(fch->connected)) {
107 /* wake up aborters */
108 wake_up_all(&fch->blocked_waitq);
109 }
110}
111
112static void fuse_put_request(struct fuse_req *req);
113
114static struct fuse_req *fuse_get_req(struct fuse_chan *fch, bool for_background)
115{
116 struct fuse_req *req;
117 int err;
118
119 atomic_inc(&fch->num_waiting);
120
121 if (fuse_block_alloc(fch, for_background)) {
122 err = -EINTR;
123 if (wait_event_state_exclusive(fch->blocked_waitq,
124 !fuse_block_alloc(fch, for_background),
125 (TASK_KILLABLE | TASK_FREEZABLE)))
126 goto out;
127 }
128
129 err = -ENOTCONN;
130 if (!fch->connected)
131 goto out;
132
133 req = fuse_request_alloc(fch, GFP_KERNEL);
134 err = -ENOMEM;
135 if (!req) {
136 if (for_background)
137 wake_up(&fch->blocked_waitq);
138 goto out;
139 }
140
141 __set_bit(FR_WAITING, &req->flags);
142 if (for_background)
143 __set_bit(FR_BACKGROUND, &req->flags);
144
145 return req;
146
147 out:
148 fuse_drop_waiting(fch);
149 return ERR_PTR(err);
150}
151
152static void fuse_put_request(struct fuse_req *req)
153{
154 struct fuse_chan *fch = req->chan;
155
156 if (refcount_dec_and_test(&req->count)) {
157 if (test_bit(FR_BACKGROUND, &req->flags)) {
158 /*
159 * We get here in the unlikely case that a background
160 * request was allocated but not sent
161 */
162 spin_lock(&fch->bg_lock);
163 if (!fch->blocked)
164 wake_up(&fch->blocked_waitq);
165 spin_unlock(&fch->bg_lock);
166 }
167
168 if (test_bit(FR_WAITING, &req->flags)) {
169 __clear_bit(FR_WAITING, &req->flags);
170 fuse_drop_waiting(fch);
171 }
172
173 fuse_request_free(req);
174 }
175}
176
177unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
178{
179 unsigned nbytes = 0;
180 unsigned i;
181
182 for (i = 0; i < numargs; i++)
183 nbytes += args[i].size;
184
185 return nbytes;
186}
187EXPORT_SYMBOL_GPL(fuse_len_args);
188
189static u64 fuse_get_unique_locked(struct fuse_iqueue *fiq)
190{
191 fiq->reqctr += FUSE_REQ_ID_STEP;
192 return fiq->reqctr;
193}
194
195u64 fuse_get_unique(struct fuse_iqueue *fiq)
196{
197 u64 ret;
198
199 spin_lock(&fiq->lock);
200 ret = fuse_get_unique_locked(fiq);
201 spin_unlock(&fiq->lock);
202
203 return ret;
204}
205EXPORT_SYMBOL_GPL(fuse_get_unique);
206
207unsigned int fuse_req_hash(u64 unique)
208{
209 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
210}
211EXPORT_SYMBOL_GPL(fuse_req_hash);
212
213/*
214 * A new request is available, wake fiq->waitq
215 */
216static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq, bool sync)
217__releases(fiq->lock)
218{
219 if (sync)
220 wake_up_sync(&fiq->waitq);
221 else
222 wake_up(&fiq->waitq);
223 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
224 spin_unlock(&fiq->lock);
225}
226
227struct fuse_forget_link *fuse_alloc_forget(void)
228{
229 return kzalloc_obj(struct fuse_forget_link, GFP_KERNEL_ACCOUNT);
230}
231
232void fuse_dev_queue_forget(struct fuse_iqueue *fiq,
233 struct fuse_forget_link *forget)
234{
235 spin_lock(&fiq->lock);
236 if (fiq->connected) {
237 fiq->forget_list_tail->next = forget;
238 fiq->forget_list_tail = forget;
239 fuse_dev_wake_and_unlock(fiq, false);
240 } else {
241 kfree(forget);
242 spin_unlock(&fiq->lock);
243 }
244}
245
246void fuse_dev_queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
247{
248 spin_lock(&fiq->lock);
249 /* Repeat FR_SENT test after obtaining the lock to prevent race with fuse_resend() */
250 if (list_empty(&req->intr_entry) && test_bit(FR_SENT, &req->flags)) {
251 list_add_tail(&req->intr_entry, &fiq->interrupts);
252 /*
253 * Pairs with smp_mb() implied by test_and_set_bit()
254 * from fuse_request_end().
255 */
256 smp_mb();
257 if (test_bit(FR_FINISHED, &req->flags)) {
258 list_del_init(&req->intr_entry);
259 spin_unlock(&fiq->lock);
260 } else {
261 fuse_dev_wake_and_unlock(fiq, false);
262 }
263 } else {
264 spin_unlock(&fiq->lock);
265 }
266}
267
268static inline void fuse_request_assign_unique_locked(struct fuse_iqueue *fiq,
269 struct fuse_req *req)
270{
271 if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
272 req->in.h.unique = fuse_get_unique_locked(fiq);
273
274 /* tracepoint captures in.h.unique and in.h.len */
275 trace_fuse_request_send(req);
276}
277
278inline void fuse_request_assign_unique(struct fuse_iqueue *fiq,
279 struct fuse_req *req)
280{
281 if (req->in.h.opcode != FUSE_NOTIFY_REPLY)
282 req->in.h.unique = fuse_get_unique(fiq);
283
284 /* tracepoint captures in.h.unique and in.h.len */
285 trace_fuse_request_send(req);
286}
287EXPORT_SYMBOL_GPL(fuse_request_assign_unique);
288
289static void fuse_dev_queue_req(struct fuse_iqueue *fiq, struct fuse_req *req)
290{
291 bool sync = test_and_clear_bit(FR_SYNC_WAKEUP, &req->flags);
292
293 spin_lock(&fiq->lock);
294 if (fiq->connected) {
295 fuse_request_assign_unique_locked(fiq, req);
296 list_add_tail(&req->list, &fiq->pending);
297 fuse_dev_wake_and_unlock(fiq, sync);
298 } else {
299 spin_unlock(&fiq->lock);
300 req->out.h.error = -ENOTCONN;
301 clear_bit(FR_PENDING, &req->flags);
302 fuse_request_end(req);
303 }
304}
305
306static const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
307 .send_forget = fuse_dev_queue_forget,
308 .send_interrupt = fuse_dev_queue_interrupt,
309 .send_req = fuse_dev_queue_req,
310};
311
312void fuse_iqueue_init(struct fuse_iqueue *fiq, const struct fuse_iqueue_ops *ops, void *priv)
313{
314 spin_lock_init(&fiq->lock);
315 init_waitqueue_head(&fiq->waitq);
316 INIT_LIST_HEAD(&fiq->pending);
317 INIT_LIST_HEAD(&fiq->interrupts);
318 fiq->forget_list_tail = &fiq->forget_list_head;
319 fiq->connected = 1;
320 fiq->ops = ops;
321 fiq->priv = priv;
322}
323EXPORT_SYMBOL_GPL(fuse_iqueue_init);
324
325void fuse_chan_release(struct fuse_chan *fch)
326{
327 struct fuse_iqueue *fiq = &fch->iq;
328
329 if (fiq->ops->release)
330 fiq->ops->release(fiq);
331
332 if (fch->timeout.req_timeout)
333 cancel_delayed_work_sync(&fch->timeout.work);
334}
335
336void fuse_chan_free(struct fuse_chan *fch)
337{
338 WARN_ON(!list_empty(&fch->devices));
339 kfree(fch->pq_prealloc);
340 kfree(fch);
341}
342EXPORT_SYMBOL_GPL(fuse_chan_free);
343
344struct fuse_chan *fuse_chan_new(void)
345{
346 struct fuse_chan *fch = kzalloc_obj(struct fuse_chan);
347 if (!fch)
348 return NULL;
349
350 spin_lock_init(&fch->lock);
351 INIT_LIST_HEAD(&fch->devices);
352 spin_lock_init(&fch->bg_lock);
353 INIT_LIST_HEAD(&fch->bg_queue);
354 init_waitqueue_head(&fch->blocked_waitq);
355 atomic_set(&fch->num_waiting, 0);
356 fch->max_background = FUSE_DEFAULT_MAX_BACKGROUND;
357 fch->initialized = 0;
358 fch->blocked = 0;
359 fch->connected = 1;
360 fch->timeout.req_timeout = 0;
361
362 return fch;
363}
364EXPORT_SYMBOL_GPL(fuse_chan_new);
365
366struct list_head *fuse_pqueue_alloc(void)
367{
368 struct list_head *pq = kzalloc_objs(struct list_head, FUSE_PQ_HASH_SIZE);
369
370 if (pq) {
371 for (int i = 0; i < FUSE_PQ_HASH_SIZE; i++)
372 INIT_LIST_HEAD(&pq[i]);
373 }
374 return pq;
375}
376
377struct fuse_chan *fuse_dev_chan_new(void)
378{
379 struct fuse_chan *fch __free(kfree) = fuse_chan_new();
380 if (!fch)
381 return NULL;
382
383 fch->pq_prealloc = fuse_pqueue_alloc();
384 if (!fch->pq_prealloc)
385 return NULL;
386
387 fuse_iqueue_init(&fch->iq, &fuse_dev_fiq_ops, NULL);
388
389 return no_free_ptr(fch);
390}
391EXPORT_SYMBOL_GPL(fuse_dev_chan_new);
392
393unsigned int fuse_chan_num_background(struct fuse_chan *fch)
394{
395 return READ_ONCE(fch->num_background);
396}
397
398unsigned int fuse_chan_max_background(struct fuse_chan *fch)
399{
400 return READ_ONCE(fch->max_background);
401}
402
403void fuse_chan_max_background_set(struct fuse_chan *fch, unsigned int val)
404{
405 spin_lock(&fch->bg_lock);
406 fch->max_background = val;
407 fch->blocked = fch->num_background >= fch->max_background;
408 if (!fch->blocked)
409 wake_up_nr(&fch->blocked_waitq,
410 fch->max_background - fch->num_background);
411 spin_unlock(&fch->bg_lock);
412}
413
414unsigned int fuse_chan_num_waiting(struct fuse_chan *fch)
415{
416 return atomic_read(&fch->num_waiting);
417}
418
419void fuse_chan_set_fc(struct fuse_chan *fch, struct fuse_conn *fc)
420{
421 fch->conn = fc;
422}
423
424void fuse_pqueue_init(struct fuse_pqueue *fpq)
425{
426 spin_lock_init(&fpq->lock);
427 INIT_LIST_HEAD(&fpq->io);
428 fpq->connected = 1;
429 fpq->processing = NULL;
430}
431
432static struct fuse_dev *fuse_dev_alloc_no_pq(void)
433{
434 struct fuse_dev *fud;
435
436 fud = kzalloc_obj(struct fuse_dev);
437 if (!fud)
438 return NULL;
439
440 refcount_set(&fud->ref, 1);
441 fuse_pqueue_init(&fud->pq);
442
443 return fud;
444}
445
446struct fuse_dev *fuse_dev_alloc(void)
447{
448 struct fuse_dev *fud __free(kfree) = fuse_dev_alloc_no_pq();
449 if (!fud)
450 return NULL;
451
452 fud->pq.processing = fuse_pqueue_alloc();
453 if (!fud->pq.processing)
454 return NULL;
455
456 return no_free_ptr(fud);
457}
458EXPORT_SYMBOL_GPL(fuse_dev_alloc);
459
460/*
461 * Installs @fch into @fud, return true on success. "Consumes" @pq in either case.
462 */
463static bool fuse_dev_install_with_pq(struct fuse_dev *fud, struct fuse_chan *fch,
464 struct list_head *pq)
465{
466 struct fuse_chan *old_fch;
467
468 guard(spinlock)(&fch->lock);
469 /*
470 * Pairs with:
471 * - xchg() in fuse_dev_release()
472 * - smp_load_acquire() in fuse_dev_fc_get()
473 */
474 old_fch = cmpxchg(&fud->chan, NULL, fch);
475 if (old_fch) {
476 /*
477 * failed to set fud->chan because
478 * - it was already set to a different fc
479 * - it was set to disconneted
480 */
481 kfree(pq);
482 return false;
483 }
484 if (pq) {
485 WARN_ON(fud->pq.processing);
486 fud->pq.processing = pq;
487 }
488 list_add_tail(&fud->entry, &fch->devices);
489 fuse_conn_get(fch->conn);
490 wake_up_all(&fuse_dev_waitq);
491 return true;
492}
493
494void fuse_dev_install(struct fuse_dev *fud, struct fuse_chan *fch)
495{
496 struct list_head *pq = fch->pq_prealloc;
497
498 fch->pq_prealloc = NULL;
499 if (!fuse_dev_install_with_pq(fud, fch, pq)) {
500 /* Channel is not usable without a dev */
501 fuse_chan_abort(fch, false);
502 }
503}
504EXPORT_SYMBOL_GPL(fuse_dev_install);
505
506struct fuse_dev *fuse_dev_alloc_install(struct fuse_chan *fch)
507{
508 struct fuse_dev *fud;
509
510 fud = fuse_dev_alloc_no_pq();
511 if (!fud)
512 return NULL;
513
514 fuse_dev_install(fud, fch);
515 return fud;
516}
517EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
518
519void fuse_dev_put(struct fuse_dev *fud)
520{
521 struct fuse_chan *fch;
522
523 if (!refcount_dec_and_test(&fud->ref))
524 return;
525
526 fch = fuse_dev_chan_get(fud);
527 if (fch && fch != FUSE_DEV_CHAN_DISCONNECTED) {
528 /* This is the virtiofs case (fuse_dev_release() not called) */
529 spin_lock(&fch->lock);
530 list_del(&fud->entry);
531 spin_unlock(&fch->lock);
532
533 fuse_conn_put(fch->conn);
534 }
535 kfree(fud->pq.processing);
536 kfree(fud);
537}
538EXPORT_SYMBOL_GPL(fuse_dev_put);
539
540bool fuse_dev_is_installed(struct fuse_dev *fud)
541{
542 struct fuse_chan *fch = fuse_dev_chan_get(fud);
543
544 return fch != NULL && fch != FUSE_DEV_CHAN_DISCONNECTED;
545}
546
547/*
548 * Checks if @fc matches the one installed in @fud
549 */
550bool fuse_dev_verify(struct fuse_dev *fud, struct fuse_chan *fch)
551{
552 return fuse_dev_chan_get(fud) == fch;
553}
554
555bool fuse_dev_is_sync_init(struct fuse_dev *fud)
556{
557 return fud->sync_init;
558}
559
560struct fuse_dev *fuse_dev_grab(struct file *file)
561{
562 struct fuse_dev *fud = fuse_file_to_fud(file);
563
564 refcount_inc(&fud->ref);
565 return fud;
566}
567
568static void fuse_send_one(struct fuse_iqueue *fiq, struct fuse_req *req)
569{
570 req->in.h.len = sizeof(struct fuse_in_header) +
571 fuse_len_args(req->args->in_numargs,
572 (struct fuse_arg *) req->args->in_args);
573 fiq->ops->send_req(fiq, req);
574}
575
576void fuse_chan_queue_forget(struct fuse_chan *fch, struct fuse_forget_link *forget,
577 u64 nodeid, u64 nlookup)
578{
579 struct fuse_iqueue *fiq = &fch->iq;
580
581 forget->forget_one.nodeid = nodeid;
582 forget->forget_one.nlookup = nlookup;
583
584 fiq->ops->send_forget(fiq, forget);
585}
586
587static void flush_bg_queue(struct fuse_chan *fch)
588{
589 struct fuse_iqueue *fiq = &fch->iq;
590
591 while (fch->active_background < fch->max_background &&
592 !list_empty(&fch->bg_queue)) {
593 struct fuse_req *req;
594
595 req = list_first_entry(&fch->bg_queue, struct fuse_req, list);
596 list_del(&req->list);
597 fch->active_background++;
598 fuse_send_one(fiq, req);
599 }
600}
601
602void fuse_request_bg_finish(struct fuse_chan *fch, struct fuse_req *req)
603{
604 lockdep_assert_held(&fch->bg_lock);
605
606 clear_bit(FR_BACKGROUND, &req->flags);
607 if (fch->num_background == fch->max_background) {
608 fch->blocked = 0;
609 wake_up(&fch->blocked_waitq);
610 } else if (!fch->blocked) {
611 /*
612 * Wake up next waiter, if any. It's okay to use
613 * waitqueue_active(), as we've already synced up
614 * fch->blocked with waiters with the wake_up() call
615 * above.
616 */
617 if (waitqueue_active(&fch->blocked_waitq))
618 wake_up(&fch->blocked_waitq);
619 }
620
621 fch->num_background--;
622 fch->active_background--;
623}
624
625/*
626 * This function is called when a request is finished. Either a reply
627 * has arrived or it was aborted (and not yet sent) or some error
628 * occurred during communication with userspace, or the device file
629 * was closed. The requester thread is woken up (if still waiting),
630 * the 'end' callback is called if given, else the reference to the
631 * request is released
632 */
633void fuse_request_end(struct fuse_req *req)
634{
635 struct fuse_chan *fch = req->chan;
636 struct fuse_iqueue *fiq = &fch->iq;
637
638 if (test_and_set_bit(FR_FINISHED, &req->flags))
639 goto put_request;
640
641 trace_fuse_request_end(req);
642 /*
643 * test_and_set_bit() implies smp_mb() between bit
644 * changing and below FR_INTERRUPTED check. Pairs with
645 * smp_mb() from queue_interrupt().
646 */
647 if (test_bit(FR_INTERRUPTED, &req->flags)) {
648 spin_lock(&fiq->lock);
649 list_del_init(&req->intr_entry);
650 spin_unlock(&fiq->lock);
651 }
652 WARN_ON(test_bit(FR_PENDING, &req->flags));
653 WARN_ON(test_bit(FR_SENT, &req->flags));
654 if (test_bit(FR_BACKGROUND, &req->flags)) {
655 spin_lock(&fch->bg_lock);
656 fuse_request_bg_finish(fch, req);
657 flush_bg_queue(fch);
658 spin_unlock(&fch->bg_lock);
659 } else {
660 /* Wake up waiter sleeping in request_wait_answer() */
661 wake_up(&req->waitq);
662 }
663
664 if (test_bit(FR_ASYNC, &req->flags))
665 req->args->end(req->args, req->out.h.error);
666put_request:
667 fuse_put_request(req);
668}
669EXPORT_SYMBOL_GPL(fuse_request_end);
670
671static int queue_interrupt(struct fuse_req *req)
672{
673 struct fuse_iqueue *fiq = &req->chan->iq;
674
675 /* Check for we've sent request to interrupt this req */
676 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags)))
677 return -EINVAL;
678
679 fiq->ops->send_interrupt(fiq, req);
680
681 return 0;
682}
683
684bool fuse_remove_pending_req(struct fuse_req *req, spinlock_t *lock)
685{
686 spin_lock(lock);
687 if (test_bit(FR_PENDING, &req->flags)) {
688 /*
689 * FR_PENDING does not get cleared as the request will end
690 * up in destruction anyway.
691 */
692 list_del(&req->list);
693 spin_unlock(lock);
694 __fuse_put_request(req);
695 req->out.h.error = -EINTR;
696 return true;
697 }
698 spin_unlock(lock);
699 return false;
700}
701
702static void request_wait_answer(struct fuse_req *req)
703{
704 struct fuse_chan *fch = req->chan;
705 struct fuse_iqueue *fiq = &fch->iq;
706 int err;
707
708 if (!fch->no_interrupt) {
709 /* Any signal may interrupt this */
710 err = wait_event_interruptible(req->waitq,
711 test_bit(FR_FINISHED, &req->flags));
712 if (!err)
713 return;
714
715 set_bit(FR_INTERRUPTED, &req->flags);
716 /* matches barrier in fuse_dev_do_read() */
717 smp_mb__after_atomic();
718 if (test_bit(FR_SENT, &req->flags))
719 queue_interrupt(req);
720 }
721
722 if (!test_bit(FR_FORCE, &req->flags)) {
723 bool removed;
724
725 /* Only fatal signals may interrupt this */
726 err = wait_event_killable(req->waitq,
727 test_bit(FR_FINISHED, &req->flags));
728 if (!err)
729 return;
730
731 if (req->args->abort_on_kill) {
732 fuse_chan_abort(fch, false);
733 goto wait_for_finish;
734 }
735
736 if (test_bit(FR_URING, &req->flags))
737 removed = fuse_uring_remove_pending_req(req);
738 else
739 removed = fuse_remove_pending_req(req, &fiq->lock);
740 if (removed)
741 return;
742 }
743
744wait_for_finish:
745 /*
746 * Either request is already in userspace, or it was forced.
747 * Wait it out.
748 */
749 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
750}
751
752static void __fuse_request_send(struct fuse_req *req)
753{
754 struct fuse_iqueue *fiq = &req->chan->iq;
755
756 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
757
758 /* acquire extra reference, since request is still needed after
759 fuse_request_end() */
760 __fuse_get_request(req);
761 /*
762 * This is a synchronous request: the caller will block waiting for
763 * the answer. Hint the scheduler via wake_up_sync().
764 */
765 set_bit(FR_SYNC_WAKEUP, &req->flags);
766 fuse_send_one(fiq, req);
767
768 request_wait_answer(req);
769 /* Pairs with smp_wmb() in fuse_request_end() */
770 smp_rmb();
771}
772
773static void fuse_adjust_compat(struct fuse_chan *fch, struct fuse_args *args)
774{
775 if (fch->minor < 4 && args->opcode == FUSE_STATFS)
776 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
777
778 if (fch->minor < 9) {
779 switch (args->opcode) {
780 case FUSE_LOOKUP:
781 case FUSE_CREATE:
782 case FUSE_MKNOD:
783 case FUSE_MKDIR:
784 case FUSE_SYMLINK:
785 case FUSE_LINK:
786 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
787 break;
788 case FUSE_GETATTR:
789 case FUSE_SETATTR:
790 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
791 break;
792 }
793 }
794 if (fch->minor < 12) {
795 switch (args->opcode) {
796 case FUSE_CREATE:
797 args->in_args[0].size = sizeof(struct fuse_open_in);
798 break;
799 case FUSE_MKNOD:
800 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
801 break;
802 }
803 }
804}
805
806static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
807{
808 req->in.h.opcode = args->opcode;
809 req->in.h.nodeid = args->nodeid;
810 req->in.h.uid = args->uid;
811 req->in.h.gid = args->gid;
812 req->in.h.pid = args->pid;
813 req->args = args;
814 if (args->is_ext)
815 req->in.h.total_extlen = args->in_args[args->ext_idx].size / 8;
816 if (args->end)
817 __set_bit(FR_ASYNC, &req->flags);
818}
819
820ssize_t fuse_chan_send(struct fuse_chan *fch, struct fuse_args *args)
821{
822 struct fuse_req *req;
823 ssize_t ret;
824
825 if (args->force) {
826 atomic_inc(&fch->num_waiting);
827 req = fuse_request_alloc(fch, GFP_KERNEL | __GFP_NOFAIL);
828
829 __set_bit(FR_WAITING, &req->flags);
830 if (!args->abort_on_kill)
831 __set_bit(FR_FORCE, &req->flags);
832 } else {
833 req = fuse_get_req(fch, false);
834 if (IS_ERR(req))
835 return PTR_ERR(req);
836 }
837
838 /* Needs to be done after fuse_get_req() so that fch->minor is valid */
839 fuse_adjust_compat(fch, args);
840 fuse_args_to_req(req, args);
841
842 if (!args->noreply)
843 __set_bit(FR_ISREPLY, &req->flags);
844 __fuse_request_send(req);
845 ret = req->out.h.error;
846 if (!ret && args->out_argvar) {
847 BUG_ON(args->out_numargs == 0);
848 ret = args->out_args[args->out_numargs - 1].size;
849 }
850 fuse_put_request(req);
851
852 return ret;
853}
854
855#ifdef CONFIG_FUSE_IO_URING
856static bool fuse_request_queue_background_uring(struct fuse_req *req)
857{
858 struct fuse_iqueue *fiq = &req->chan->iq;
859
860 req->in.h.len = sizeof(struct fuse_in_header) +
861 fuse_len_args(req->args->in_numargs,
862 (struct fuse_arg *) req->args->in_args);
863 fuse_request_assign_unique(fiq, req);
864
865 return fuse_uring_queue_bq_req(req);
866}
867#endif
868
869/*
870 * @return true if queued
871 */
872static int fuse_request_queue_background(struct fuse_req *req)
873{
874 struct fuse_chan *fch = req->chan;
875 bool queued = false;
876
877 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
878 if (!test_bit(FR_WAITING, &req->flags)) {
879 __set_bit(FR_WAITING, &req->flags);
880 atomic_inc(&fch->num_waiting);
881 }
882 __set_bit(FR_ISREPLY, &req->flags);
883
884#ifdef CONFIG_FUSE_IO_URING
885 if (fuse_uring_ready(fch))
886 return fuse_request_queue_background_uring(req);
887#endif
888
889 spin_lock(&fch->bg_lock);
890 if (likely(fch->connected)) {
891 fch->num_background++;
892 if (fch->num_background == fch->max_background)
893 fch->blocked = 1;
894 list_add_tail(&req->list, &fch->bg_queue);
895 flush_bg_queue(fch);
896 queued = true;
897 }
898 spin_unlock(&fch->bg_lock);
899
900 return queued;
901}
902
903int fuse_chan_send_bg(struct fuse_chan *fch, struct fuse_args *args, gfp_t gfp_flags)
904{
905 struct fuse_req *req;
906
907 if (args->force) {
908 req = fuse_request_alloc(fch, gfp_flags);
909 if (!req)
910 return -ENOMEM;
911 __set_bit(FR_BACKGROUND, &req->flags);
912 } else {
913 req = fuse_get_req(fch, true);
914 if (IS_ERR(req))
915 return PTR_ERR(req);
916 }
917
918 fuse_args_to_req(req, args);
919
920 if (!fuse_request_queue_background(req)) {
921 fuse_put_request(req);
922 return -ENOTCONN;
923 }
924
925 return 0;
926}
927
928int fuse_chan_send_notify_reply(struct fuse_chan *fch, struct fuse_args *args, u64 unique)
929{
930 struct fuse_req *req;
931 struct fuse_iqueue *fiq = &fch->iq;
932
933 req = fuse_get_req(fch, false);
934 if (IS_ERR(req))
935 return PTR_ERR(req);
936
937 __clear_bit(FR_ISREPLY, &req->flags);
938 req->in.h.unique = unique;
939
940 fuse_args_to_req(req, args);
941
942 fuse_send_one(fiq, req);
943
944 return 0;
945}
946
947/*
948 * Lock the request. Up to the next unlock_request() there mustn't be
949 * anything that could cause a page-fault. If the request was already
950 * aborted bail out.
951 */
952static int lock_request(struct fuse_req *req)
953{
954 int err = 0;
955 if (req) {
956 spin_lock(&req->waitq.lock);
957 if (test_bit(FR_ABORTED, &req->flags))
958 err = -ENOENT;
959 else
960 set_bit(FR_LOCKED, &req->flags);
961 spin_unlock(&req->waitq.lock);
962 }
963 return err;
964}
965
966/*
967 * Unlock request. If it was aborted while locked, caller is responsible
968 * for unlocking and ending the request.
969 */
970static int unlock_request(struct fuse_req *req)
971{
972 int err = 0;
973 if (req) {
974 spin_lock(&req->waitq.lock);
975 if (test_bit(FR_ABORTED, &req->flags))
976 err = -ENOENT;
977 else
978 clear_bit(FR_LOCKED, &req->flags);
979 spin_unlock(&req->waitq.lock);
980 }
981 return err;
982}
983
984void fuse_copy_init(struct fuse_copy_state *cs, bool write,
985 struct iov_iter *iter)
986{
987 memset(cs, 0, sizeof(*cs));
988 cs->write = write;
989 cs->iter = iter;
990}
991
992/* Unmap and put previous page of userspace buffer */
993void fuse_copy_finish(struct fuse_copy_state *cs)
994{
995 if (cs->currbuf) {
996 struct pipe_buffer *buf = cs->currbuf;
997
998 if (cs->write)
999 buf->len = PAGE_SIZE - cs->len;
1000 cs->currbuf = NULL;
1001 } else if (cs->pg) {
1002 if (cs->write) {
1003 flush_dcache_page(cs->pg);
1004 set_page_dirty_lock(cs->pg);
1005 }
1006 put_page(cs->pg);
1007 }
1008 cs->pg = NULL;
1009}
1010
1011/*
1012 * Get another pagefull of userspace buffer, and map it to kernel
1013 * address space, and lock request
1014 */
1015static int fuse_copy_fill(struct fuse_copy_state *cs)
1016{
1017 struct page *page;
1018 int err;
1019
1020 err = unlock_request(cs->req);
1021 if (err)
1022 return err;
1023
1024 fuse_copy_finish(cs);
1025 if (cs->pipebufs) {
1026 struct pipe_buffer *buf = cs->pipebufs;
1027
1028 if (!cs->write) {
1029 err = pipe_buf_confirm(cs->pipe, buf);
1030 if (err)
1031 return err;
1032
1033 BUG_ON(!cs->nr_segs);
1034 cs->currbuf = buf;
1035 cs->pg = buf->page;
1036 cs->offset = buf->offset;
1037 cs->len = buf->len;
1038 cs->pipebufs++;
1039 cs->nr_segs--;
1040 } else {
1041 if (cs->nr_segs >= cs->pipe->max_usage)
1042 return -EIO;
1043
1044 page = alloc_page(GFP_HIGHUSER);
1045 if (!page)
1046 return -ENOMEM;
1047
1048 buf->page = page;
1049 buf->offset = 0;
1050 buf->len = 0;
1051
1052 cs->currbuf = buf;
1053 cs->pg = page;
1054 cs->offset = 0;
1055 cs->len = PAGE_SIZE;
1056 cs->pipebufs++;
1057 cs->nr_segs++;
1058 }
1059 } else {
1060 size_t off;
1061 err = iov_iter_get_pages2(cs->iter, &page, PAGE_SIZE, 1, &off);
1062 if (err < 0)
1063 return err;
1064 BUG_ON(!err);
1065 cs->len = err;
1066 cs->offset = off;
1067 cs->pg = page;
1068 }
1069
1070 return lock_request(cs->req);
1071}
1072
1073/* Do as much copy to/from userspace buffer as we can */
1074static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
1075{
1076 unsigned ncpy = min(*size, cs->len);
1077 if (val) {
1078 void *pgaddr = kmap_local_page(cs->pg);
1079 void *buf = pgaddr + cs->offset;
1080
1081 if (cs->write)
1082 memcpy(buf, *val, ncpy);
1083 else
1084 memcpy(*val, buf, ncpy);
1085
1086 kunmap_local(pgaddr);
1087 *val += ncpy;
1088 }
1089 *size -= ncpy;
1090 cs->len -= ncpy;
1091 cs->offset += ncpy;
1092 if (cs->is_uring)
1093 cs->ring.copied_sz += ncpy;
1094
1095 return ncpy;
1096}
1097
1098static int fuse_check_folio(struct folio *folio)
1099{
1100 if (folio_mapped(folio) ||
1101 folio->mapping != NULL ||
1102 (folio->flags.f & PAGE_FLAGS_CHECK_AT_PREP &
1103 ~(1 << PG_locked |
1104 1 << PG_referenced |
1105 1 << PG_lru |
1106 1 << PG_active |
1107 1 << PG_workingset |
1108 1 << PG_reclaim |
1109 1 << PG_waiters |
1110 LRU_GEN_MASK | LRU_REFS_MASK))) {
1111 dump_page(&folio->page, "fuse: trying to steal weird page");
1112 return 1;
1113 }
1114 return 0;
1115}
1116
1117/*
1118 * Attempt to steal a page from the splice() pipe and move it into the
1119 * pagecache. If successful, the pointer in @pagep will be updated. The
1120 * folio that was originally in @pagep will lose a reference and the new
1121 * folio returned in @pagep will carry a reference.
1122 */
1123static int fuse_try_move_folio(struct fuse_copy_state *cs, struct folio **foliop)
1124{
1125 int err;
1126 struct folio *oldfolio = *foliop;
1127 struct folio *newfolio;
1128 struct pipe_buffer *buf = cs->pipebufs;
1129
1130 folio_get(oldfolio);
1131 err = unlock_request(cs->req);
1132 if (err)
1133 goto out_put_old;
1134
1135 fuse_copy_finish(cs);
1136
1137 err = pipe_buf_confirm(cs->pipe, buf);
1138 if (err)
1139 goto out_put_old;
1140
1141 BUG_ON(!cs->nr_segs);
1142 cs->currbuf = buf;
1143 cs->len = buf->len;
1144 cs->pipebufs++;
1145 cs->nr_segs--;
1146
1147 if (cs->len != folio_size(oldfolio))
1148 goto out_fallback;
1149
1150 if (!pipe_buf_try_steal(cs->pipe, buf))
1151 goto out_fallback;
1152
1153 newfolio = page_folio(buf->page);
1154
1155 folio_clear_uptodate(newfolio);
1156 folio_clear_mappedtodisk(newfolio);
1157
1158 if (folio_test_large(newfolio))
1159 goto out_fallback_unlock;
1160
1161 if (fuse_check_folio(newfolio) != 0)
1162 goto out_fallback_unlock;
1163
1164 /*
1165 * This is a new and locked page, it shouldn't be mapped or
1166 * have any special flags on it
1167 */
1168 if (WARN_ON(folio_mapped(oldfolio)))
1169 goto out_fallback_unlock;
1170 if (WARN_ON(folio_has_private(oldfolio)))
1171 goto out_fallback_unlock;
1172 if (WARN_ON(folio_test_dirty(oldfolio) ||
1173 folio_test_writeback(oldfolio)))
1174 goto out_fallback_unlock;
1175 if (WARN_ON(folio_test_mlocked(oldfolio)))
1176 goto out_fallback_unlock;
1177
1178 err = lock_request(cs->req);
1179 if (err)
1180 goto out_fallback_unlock;
1181
1182 replace_page_cache_folio(oldfolio, newfolio);
1183
1184 folio_get(newfolio);
1185
1186 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
1187 folio_add_lru(newfolio);
1188
1189 /*
1190 * Release while we have extra ref on stolen page. Otherwise
1191 * anon_pipe_buf_release() might think the page can be reused.
1192 */
1193 pipe_buf_release(cs->pipe, buf);
1194
1195 *foliop = newfolio;
1196 folio_unlock(oldfolio);
1197 /* Drop ref for ap->pages[] array */
1198 folio_put(oldfolio);
1199 cs->len = 0;
1200
1201 err = 0;
1202out_put_old:
1203 /* Drop ref obtained in this function */
1204 folio_put(oldfolio);
1205 return err;
1206
1207out_fallback_unlock:
1208 folio_unlock(newfolio);
1209out_fallback:
1210 cs->pg = buf->page;
1211 cs->offset = buf->offset;
1212
1213 err = lock_request(cs->req);
1214 if (!err)
1215 err = 1;
1216
1217 goto out_put_old;
1218}
1219
1220static int fuse_ref_folio(struct fuse_copy_state *cs, struct folio *folio,
1221 unsigned offset, unsigned count)
1222{
1223 struct pipe_buffer *buf;
1224 int err;
1225
1226 if (cs->nr_segs >= cs->pipe->max_usage)
1227 return -EIO;
1228
1229 folio_get(folio);
1230 err = unlock_request(cs->req);
1231 if (err) {
1232 folio_put(folio);
1233 return err;
1234 }
1235
1236 fuse_copy_finish(cs);
1237
1238 buf = cs->pipebufs;
1239 buf->page = &folio->page;
1240 buf->offset = offset;
1241 buf->len = count;
1242
1243 cs->pipebufs++;
1244 cs->nr_segs++;
1245 cs->len = 0;
1246
1247 return lock_request(cs->req);
1248}
1249
1250/*
1251 * Copy a folio in the request to/from the userspace buffer. Must be
1252 * done atomically
1253 */
1254int fuse_copy_folio(struct fuse_copy_state *cs, struct folio **foliop,
1255 unsigned offset, unsigned count, int zeroing)
1256{
1257 int err;
1258 struct folio *folio = *foliop;
1259 size_t size;
1260
1261 if (folio) {
1262 size = folio_size(folio);
1263 if (zeroing && count < size) {
1264 /*
1265 * When the copy is skipped the folio already holds the
1266 * payload, so only the bytes outside [offset, offset +
1267 * count) may be zeroed.
1268 *
1269 * Otherwise, the whole folio is cleared first so that a
1270 * failed copy leaves zeros rather than stale folio
1271 * contents.
1272 */
1273 if (cs->skip_folio_copy)
1274 folio_zero_segments(folio, 0, offset,
1275 offset + count, size);
1276 else
1277 folio_zero_range(folio, 0, size);
1278 }
1279 }
1280
1281 while (!cs->skip_folio_copy && count) {
1282 if (cs->write && cs->pipebufs && folio) {
1283 /*
1284 * Can't control lifetime of pipe buffers, so always
1285 * copy user pages.
1286 */
1287 if (cs->req->args->user_pages) {
1288 err = fuse_copy_fill(cs);
1289 if (err)
1290 return err;
1291 } else {
1292 return fuse_ref_folio(cs, folio, offset, count);
1293 }
1294 } else if (!cs->len) {
1295 if (cs->move_folios && folio &&
1296 offset == 0 && count == size) {
1297 err = fuse_try_move_folio(cs, foliop);
1298 if (err <= 0)
1299 return err;
1300 } else {
1301 err = fuse_copy_fill(cs);
1302 if (err)
1303 return err;
1304 }
1305 }
1306 if (folio) {
1307 void *mapaddr = kmap_local_folio(folio, offset);
1308 void *buf = mapaddr;
1309 unsigned int copy = count;
1310 unsigned int bytes_copied;
1311
1312 if (folio_test_highmem(folio) && count > PAGE_SIZE - offset_in_page(offset))
1313 copy = PAGE_SIZE - offset_in_page(offset);
1314
1315 bytes_copied = fuse_copy_do(cs, &buf, &copy);
1316 kunmap_local(mapaddr);
1317 offset += bytes_copied;
1318 count -= bytes_copied;
1319 } else
1320 offset += fuse_copy_do(cs, NULL, &count);
1321 }
1322 if (folio && !cs->write)
1323 flush_dcache_folio(folio);
1324 return 0;
1325}
1326
1327/* Copy folios in the request to/from userspace buffer */
1328static int fuse_copy_folios(struct fuse_copy_state *cs, unsigned nbytes,
1329 int zeroing)
1330{
1331 unsigned i;
1332 struct fuse_req *req = cs->req;
1333 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
1334
1335 for (i = 0; i < ap->num_folios && (nbytes || zeroing); i++) {
1336 int err;
1337 unsigned int offset = ap->descs[i].offset;
1338 unsigned int count = min(nbytes, ap->descs[i].length);
1339
1340 err = fuse_copy_folio(cs, &ap->folios[i], offset, count, zeroing);
1341 if (err)
1342 return err;
1343
1344 nbytes -= count;
1345 }
1346 return 0;
1347}
1348
1349/* Copy a single argument in the request to/from userspace buffer */
1350int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1351{
1352 while (size) {
1353 if (!cs->len) {
1354 int err = fuse_copy_fill(cs);
1355 if (err)
1356 return err;
1357 }
1358 fuse_copy_do(cs, &val, &size);
1359 }
1360 return 0;
1361}
1362
1363/* Copy request arguments to/from userspace buffer */
1364int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1365 unsigned argpages, struct fuse_arg *args,
1366 int zeroing)
1367{
1368 int err = 0;
1369 unsigned i;
1370
1371 for (i = 0; !err && i < numargs; i++) {
1372 struct fuse_arg *arg = &args[i];
1373 if (i == numargs - 1 && argpages)
1374 /*
1375 * if cs->skip_folio_copy is set, this just does any
1376 * needed zeroing. No copying is involved.
1377 */
1378 err = fuse_copy_folios(cs, arg->size, zeroing);
1379 else
1380 err = fuse_copy_one(cs, arg->value, arg->size);
1381 }
1382 return err;
1383}
1384
1385static int forget_pending(struct fuse_iqueue *fiq)
1386{
1387 return fiq->forget_list_head.next != NULL;
1388}
1389
1390static int request_pending(struct fuse_iqueue *fiq)
1391{
1392 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1393 forget_pending(fiq);
1394}
1395
1396/*
1397 * Transfer an interrupt request to userspace
1398 *
1399 * Unlike other requests this is assembled on demand, without a need
1400 * to allocate a separate fuse_req structure.
1401 *
1402 * Called with fiq->lock held, releases it
1403 */
1404static int fuse_read_interrupt(struct fuse_iqueue *fiq, struct fuse_copy_state *cs)
1405__releases(fiq->lock)
1406{
1407 struct fuse_req *req = list_first_entry(&fiq->interrupts, struct fuse_req, intr_entry);
1408 struct fuse_interrupt_in arg = {
1409 .unique = req->in.h.unique,
1410 };
1411 struct fuse_in_header ih = {
1412 .opcode = FUSE_INTERRUPT,
1413 .unique = (req->in.h.unique | FUSE_INT_REQ_BIT),
1414 .len = sizeof(ih) + sizeof(arg),
1415 };
1416 int err;
1417
1418 list_del_init(&req->intr_entry);
1419 spin_unlock(&fiq->lock);
1420
1421 err = fuse_copy_one(cs, &ih, sizeof(ih));
1422 if (!err)
1423 err = fuse_copy_one(cs, &arg, sizeof(arg));
1424 fuse_copy_finish(cs);
1425
1426 return err ? err : ih.len;
1427}
1428
1429static struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1430 unsigned int max,
1431 unsigned int *countp)
1432{
1433 struct fuse_forget_link *head = fiq->forget_list_head.next;
1434 struct fuse_forget_link **newhead = &head;
1435 unsigned count;
1436
1437 for (count = 0; *newhead != NULL && count < max; count++)
1438 newhead = &(*newhead)->next;
1439
1440 fiq->forget_list_head.next = *newhead;
1441 *newhead = NULL;
1442 if (fiq->forget_list_head.next == NULL)
1443 fiq->forget_list_tail = &fiq->forget_list_head;
1444
1445 if (countp != NULL)
1446 *countp = count;
1447
1448 return head;
1449}
1450
1451static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1452 struct fuse_copy_state *cs)
1453__releases(fiq->lock)
1454{
1455 int err;
1456 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
1457 struct fuse_forget_in arg = {
1458 .nlookup = forget->forget_one.nlookup,
1459 };
1460 struct fuse_in_header ih = {
1461 .opcode = FUSE_FORGET,
1462 .nodeid = forget->forget_one.nodeid,
1463 .unique = fuse_get_unique_locked(fiq),
1464 .len = sizeof(ih) + sizeof(arg),
1465 };
1466
1467 spin_unlock(&fiq->lock);
1468 kfree(forget);
1469
1470 err = fuse_copy_one(cs, &ih, sizeof(ih));
1471 if (!err)
1472 err = fuse_copy_one(cs, &arg, sizeof(arg));
1473 fuse_copy_finish(cs);
1474
1475 if (err)
1476 return err;
1477
1478 return ih.len;
1479}
1480
1481static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1482 struct fuse_copy_state *cs, size_t nbytes)
1483__releases(fiq->lock)
1484{
1485 int err;
1486 unsigned max_forgets;
1487 unsigned count;
1488 struct fuse_forget_link *head;
1489 struct fuse_batch_forget_in arg = { .count = 0 };
1490 struct fuse_in_header ih = {
1491 .opcode = FUSE_BATCH_FORGET,
1492 .unique = fuse_get_unique_locked(fiq),
1493 .len = sizeof(ih) + sizeof(arg),
1494 };
1495
1496 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1497 head = fuse_dequeue_forget(fiq, max_forgets, &count);
1498 spin_unlock(&fiq->lock);
1499
1500 arg.count = count;
1501 ih.len += count * sizeof(struct fuse_forget_one);
1502 err = fuse_copy_one(cs, &ih, sizeof(ih));
1503 if (!err)
1504 err = fuse_copy_one(cs, &arg, sizeof(arg));
1505
1506 while (head) {
1507 struct fuse_forget_link *forget = head;
1508
1509 if (!err) {
1510 err = fuse_copy_one(cs, &forget->forget_one,
1511 sizeof(forget->forget_one));
1512 }
1513 head = forget->next;
1514 kfree(forget);
1515 }
1516
1517 fuse_copy_finish(cs);
1518
1519 if (err)
1520 return err;
1521
1522 return ih.len;
1523}
1524
1525static int fuse_read_forget(struct fuse_chan *fch, struct fuse_iqueue *fiq,
1526 struct fuse_copy_state *cs,
1527 size_t nbytes)
1528__releases(fiq->lock)
1529{
1530 if (fch->minor < 16 || fiq->forget_list_head.next->next == NULL)
1531 return fuse_read_single_forget(fiq, cs);
1532 else
1533 return fuse_read_batch_forget(fiq, cs, nbytes);
1534}
1535
1536/*
1537 * Read a single request into the userspace filesystem's buffer. This
1538 * function waits until a request is available, then removes it from
1539 * the pending list and copies request data to userspace buffer. If
1540 * no reply is needed (FORGET) or request has been aborted or there
1541 * was an error during the copying then it's finished by calling
1542 * fuse_request_end(). Otherwise add it to the processing list, and set
1543 * the 'sent' flag.
1544 */
1545static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1546 struct fuse_copy_state *cs, size_t nbytes)
1547{
1548 ssize_t err;
1549 struct fuse_chan *fch = fud->chan;
1550 struct fuse_iqueue *fiq = &fch->iq;
1551 struct fuse_pqueue *fpq = &fud->pq;
1552 struct fuse_req *req;
1553 struct fuse_args *args;
1554 unsigned reqsize;
1555 unsigned int hash;
1556
1557 /*
1558 * Require sane minimum read buffer - that has capacity for fixed part
1559 * of any request header + negotiated max_write room for data.
1560 *
1561 * Historically libfuse reserves 4K for fixed header room, but e.g.
1562 * GlusterFS reserves only 80 bytes
1563 *
1564 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1565 *
1566 * which is the absolute minimum any sane filesystem should be using
1567 * for header room.
1568 */
1569 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1570 sizeof(struct fuse_in_header) +
1571 sizeof(struct fuse_write_in) +
1572 fch->max_write))
1573 return -EINVAL;
1574
1575 restart:
1576 for (;;) {
1577 spin_lock(&fiq->lock);
1578 if (!fiq->connected || request_pending(fiq))
1579 break;
1580 spin_unlock(&fiq->lock);
1581
1582 if (file->f_flags & O_NONBLOCK)
1583 return -EAGAIN;
1584 err = wait_event_interruptible_exclusive(fiq->waitq,
1585 !fiq->connected || request_pending(fiq));
1586 if (err)
1587 return err;
1588 }
1589
1590 if (!fiq->connected) {
1591 err = fch->abort_with_err ? -ECONNABORTED : -ENODEV;
1592 goto err_unlock;
1593 }
1594
1595 if (!list_empty(&fiq->interrupts))
1596 return fuse_read_interrupt(fiq, cs);
1597
1598 if (forget_pending(fiq)) {
1599 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1600 return fuse_read_forget(fch, fiq, cs, nbytes);
1601
1602 if (fiq->forget_batch <= -8)
1603 fiq->forget_batch = 16;
1604 }
1605
1606 req = list_entry(fiq->pending.next, struct fuse_req, list);
1607 clear_bit(FR_PENDING, &req->flags);
1608 list_del_init(&req->list);
1609 spin_unlock(&fiq->lock);
1610
1611 args = req->args;
1612 reqsize = req->in.h.len;
1613
1614 /* If request is too large, reply with an error and restart the read */
1615 if (nbytes < reqsize) {
1616 req->out.h.error = -EIO;
1617 /* SETXATTR is special, since it may contain too large data */
1618 if (args->opcode == FUSE_SETXATTR)
1619 req->out.h.error = -E2BIG;
1620 fuse_request_end(req);
1621 goto restart;
1622 }
1623 spin_lock(&fpq->lock);
1624 /*
1625 * Must not put request on fpq->io queue after having been shut down by
1626 * fuse_chan_abort()
1627 */
1628 if (!fpq->connected) {
1629 req->out.h.error = err = -ECONNABORTED;
1630 goto out_end;
1631 }
1632 list_add(&req->list, &fpq->io);
1633 spin_unlock(&fpq->lock);
1634 cs->req = req;
1635 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
1636 if (!err)
1637 err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1638 (struct fuse_arg *) args->in_args, 0);
1639 fuse_copy_finish(cs);
1640 spin_lock(&fpq->lock);
1641 clear_bit(FR_LOCKED, &req->flags);
1642 if (!fpq->connected) {
1643 err = fch->abort_with_err ? -ECONNABORTED : -ENODEV;
1644 goto out_end;
1645 }
1646 if (err) {
1647 req->out.h.error = -EIO;
1648 goto out_end;
1649 }
1650 if (!test_bit(FR_ISREPLY, &req->flags)) {
1651 err = reqsize;
1652 goto out_end;
1653 }
1654 hash = fuse_req_hash(req->in.h.unique);
1655 list_move_tail(&req->list, &fpq->processing[hash]);
1656 __fuse_get_request(req);
1657 set_bit(FR_SENT, &req->flags);
1658 trace_fuse_request_sent(req);
1659 spin_unlock(&fpq->lock);
1660 /* matches barrier in request_wait_answer() */
1661 smp_mb__after_atomic();
1662 if (test_bit(FR_INTERRUPTED, &req->flags))
1663 queue_interrupt(req);
1664 fuse_put_request(req);
1665
1666 return reqsize;
1667
1668out_end:
1669 if (!test_bit(FR_PRIVATE, &req->flags))
1670 list_del_init(&req->list);
1671 spin_unlock(&fpq->lock);
1672 fuse_request_end(req);
1673 return err;
1674
1675 err_unlock:
1676 spin_unlock(&fiq->lock);
1677 return err;
1678}
1679
1680static int fuse_dev_open(struct inode *inode, struct file *file)
1681{
1682 struct fuse_dev *fud = fuse_dev_alloc_no_pq();
1683
1684 if (!fud)
1685 return -ENOMEM;
1686
1687 file->private_data = fud;
1688 return 0;
1689}
1690
1691struct fuse_dev *fuse_get_dev(struct file *file)
1692{
1693 struct fuse_dev *fud = fuse_file_to_fud(file);
1694 int err;
1695
1696 if (unlikely(!fuse_dev_chan_get(fud))) {
1697 /* only block waiting for mount if sync init was requested */
1698 if (!fud->sync_init)
1699 return ERR_PTR(-EPERM);
1700
1701 err = wait_event_interruptible(fuse_dev_waitq, fuse_dev_chan_get(fud) != NULL);
1702 if (err)
1703 return ERR_PTR(err);
1704 }
1705
1706 return fud;
1707}
1708
1709static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1710{
1711 struct fuse_copy_state cs;
1712 struct file *file = iocb->ki_filp;
1713 struct fuse_dev *fud = fuse_get_dev(file);
1714
1715 if (IS_ERR(fud))
1716 return PTR_ERR(fud);
1717
1718 if (!user_backed_iter(to))
1719 return -EINVAL;
1720
1721 fuse_copy_init(&cs, true, to);
1722
1723 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1724}
1725
1726static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1727 struct pipe_inode_info *pipe,
1728 size_t len, unsigned int flags)
1729{
1730 int total, ret;
1731 int page_nr = 0;
1732 struct pipe_buffer *bufs;
1733 struct fuse_copy_state cs;
1734 struct fuse_dev *fud = fuse_get_dev(in);
1735
1736 if (IS_ERR(fud))
1737 return PTR_ERR(fud);
1738
1739 bufs = kvmalloc_objs(struct pipe_buffer, pipe->max_usage);
1740 if (!bufs)
1741 return -ENOMEM;
1742
1743 fuse_copy_init(&cs, true, NULL);
1744 cs.pipebufs = bufs;
1745 cs.pipe = pipe;
1746 ret = fuse_dev_do_read(fud, in, &cs, len);
1747 if (ret < 0)
1748 goto out;
1749
1750 if (pipe_buf_usage(pipe) + cs.nr_segs > pipe->max_usage) {
1751 ret = -EIO;
1752 goto out;
1753 }
1754
1755 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
1756 /*
1757 * Need to be careful about this. Having buf->ops in module
1758 * code can Oops if the buffer persists after module unload.
1759 */
1760 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
1761 bufs[page_nr].flags = 0;
1762 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1763 if (unlikely(ret < 0))
1764 break;
1765 }
1766 if (total)
1767 ret = total;
1768out:
1769 for (; page_nr < cs.nr_segs; page_nr++)
1770 put_page(bufs[page_nr].page);
1771
1772 kvfree(bufs);
1773 return ret;
1774}
1775
1776/*
1777 * Resending all processing queue requests.
1778 *
1779 * During a FUSE daemon panics and failover, it is possible for some inflight
1780 * requests to be lost and never returned. As a result, applications awaiting
1781 * replies would become stuck forever. To address this, we can use notification
1782 * to trigger resending of these pending requests to the FUSE daemon, ensuring
1783 * they are properly processed again.
1784 *
1785 * Please note that this strategy is applicable only to idempotent requests or
1786 * if the FUSE daemon takes careful measures to avoid processing duplicated
1787 * non-idempotent requests.
1788 */
1789void fuse_chan_resend(struct fuse_chan *fch)
1790{
1791 struct fuse_dev *fud;
1792 struct fuse_req *req;
1793 struct fuse_iqueue *fiq = &fch->iq;
1794 LIST_HEAD(to_queue);
1795 unsigned int i;
1796
1797 spin_lock(&fch->lock);
1798 if (!fch->connected) {
1799 spin_unlock(&fch->lock);
1800 return;
1801 }
1802
1803 list_for_each_entry(fud, &fch->devices, entry) {
1804 struct fuse_pqueue *fpq = &fud->pq;
1805
1806 spin_lock(&fpq->lock);
1807 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) {
1808 struct list_head *this_queue = &fpq->processing[i];
1809
1810 list_for_each_entry(req, this_queue, list)
1811 clear_bit(FR_SENT, &req->flags);
1812 list_splice_tail_init(this_queue, &to_queue);
1813 }
1814 spin_unlock(&fpq->lock);
1815 }
1816 spin_unlock(&fch->lock);
1817
1818 spin_lock(&fiq->lock);
1819 if (!fiq->connected) {
1820 spin_unlock(&fiq->lock);
1821 fuse_dev_end_requests(&to_queue);
1822 return;
1823 }
1824 /*
1825 * Remove interrupt entries for resent requests to prevent stale
1826 * intr_entry on fiq->interrupts after the request is re-queued.
1827 */
1828 list_for_each_entry(req, &to_queue, list) {
1829 set_bit(FR_PENDING, &req->flags);
1830 /* mark the request as resend request */
1831 req->in.h.unique |= FUSE_UNIQUE_RESEND;
1832
1833 if (test_bit(FR_INTERRUPTED, &req->flags))
1834 list_del_init(&req->intr_entry);
1835 }
1836 /* iq and pq requests are both oldest to newest */
1837 list_splice(&to_queue, &fiq->pending);
1838 fuse_dev_wake_and_unlock(fiq, false);
1839}
1840
1841/* Look up request on processing list by unique ID */
1842struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique)
1843{
1844 unsigned int hash = fuse_req_hash(unique);
1845 struct fuse_req *req;
1846
1847 list_for_each_entry(req, &fpq->processing[hash], list) {
1848 if (req->in.h.unique == unique)
1849 return req;
1850 }
1851 return NULL;
1852}
1853
1854int fuse_copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
1855 unsigned nbytes)
1856{
1857
1858 unsigned int reqsize = 0;
1859
1860 /*
1861 * Uring has all headers separated from args - args is payload only
1862 */
1863 if (!cs->is_uring)
1864 reqsize = sizeof(struct fuse_out_header);
1865
1866 reqsize += fuse_len_args(args->out_numargs, args->out_args);
1867
1868 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
1869 return -EINVAL;
1870 else if (reqsize > nbytes) {
1871 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
1872 unsigned diffsize = reqsize - nbytes;
1873
1874 if (diffsize > lastarg->size)
1875 return -EINVAL;
1876 lastarg->size -= diffsize;
1877 }
1878 return fuse_copy_args(cs, args->out_numargs, args->out_pages,
1879 args->out_args, args->page_zeroing);
1880}
1881
1882/*
1883 * Write a single reply to a request. First the header is copied from
1884 * the write buffer. The request is then searched on the processing
1885 * list by the unique ID found in the header. If found, then remove
1886 * it from the list and copy the rest of the buffer to the request.
1887 * The request is finished by calling fuse_request_end().
1888 */
1889static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1890 struct fuse_copy_state *cs, size_t nbytes)
1891{
1892 int err;
1893 struct fuse_chan *fch = fud->chan;
1894 struct fuse_pqueue *fpq = &fud->pq;
1895 struct fuse_req *req;
1896 struct fuse_out_header oh;
1897
1898 err = -EINVAL;
1899 if (nbytes < sizeof(struct fuse_out_header))
1900 goto out;
1901
1902 err = fuse_copy_one(cs, &oh, sizeof(oh));
1903 if (err)
1904 goto copy_finish;
1905
1906 err = -EINVAL;
1907 if (oh.len != nbytes)
1908 goto copy_finish;
1909
1910 /*
1911 * Zero oh.unique indicates unsolicited notification message
1912 * and error contains notification code.
1913 */
1914 if (!oh.unique) {
1915 /*
1916 * Only allow notifications during while the connection is in an
1917 * initialized and connected state
1918 */
1919 err = -EINVAL;
1920 /* Pairs with smp_store_release() in fuse_chan_set_initialized() */
1921 if (!smp_load_acquire(&fch->initialized) || !fch->connected)
1922 goto copy_finish;
1923
1924 /* Don't try to move folios (yet) */
1925 cs->move_folios = false;
1926
1927 err = fuse_notify(fch->conn, oh.error, nbytes - sizeof(oh), cs);
1928 goto copy_finish;
1929 }
1930
1931 err = -EINVAL;
1932 if (oh.error <= -512 || oh.error > 0)
1933 goto copy_finish;
1934
1935 spin_lock(&fpq->lock);
1936 req = NULL;
1937 if (fpq->connected)
1938 req = fuse_request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
1939
1940 err = -ENOENT;
1941 if (!req) {
1942 spin_unlock(&fpq->lock);
1943 goto copy_finish;
1944 }
1945
1946 /* Is it an interrupt reply ID? */
1947 if (oh.unique & FUSE_INT_REQ_BIT) {
1948 __fuse_get_request(req);
1949 spin_unlock(&fpq->lock);
1950
1951 err = 0;
1952 if (nbytes != sizeof(struct fuse_out_header))
1953 err = -EINVAL;
1954 else if (oh.error == -ENOSYS)
1955 fch->no_interrupt = 1;
1956 else if (oh.error == -EAGAIN)
1957 err = queue_interrupt(req);
1958
1959 fuse_put_request(req);
1960
1961 goto copy_finish;
1962 }
1963
1964 clear_bit(FR_SENT, &req->flags);
1965 list_move(&req->list, &fpq->io);
1966 req->out.h = oh;
1967 set_bit(FR_LOCKED, &req->flags);
1968 spin_unlock(&fpq->lock);
1969 cs->req = req;
1970 if (!req->args->page_replace)
1971 cs->move_folios = false;
1972
1973 if (oh.error)
1974 err = nbytes != sizeof(oh) ? -EINVAL : 0;
1975 else
1976 err = fuse_copy_out_args(cs, req->args, nbytes);
1977 fuse_copy_finish(cs);
1978
1979 spin_lock(&fpq->lock);
1980 clear_bit(FR_LOCKED, &req->flags);
1981 if (!fpq->connected)
1982 err = -ENOENT;
1983 else if (err)
1984 req->out.h.error = -EIO;
1985 if (!test_bit(FR_PRIVATE, &req->flags))
1986 list_del_init(&req->list);
1987 spin_unlock(&fpq->lock);
1988
1989 fuse_request_end(req);
1990out:
1991 return err ? err : nbytes;
1992
1993copy_finish:
1994 fuse_copy_finish(cs);
1995 goto out;
1996}
1997
1998static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1999{
2000 struct fuse_copy_state cs;
2001 struct fuse_dev *fud = __fuse_get_dev(iocb->ki_filp);
2002
2003 if (!fud)
2004 return -EPERM;
2005
2006 if (!user_backed_iter(from))
2007 return -EINVAL;
2008
2009 fuse_copy_init(&cs, false, from);
2010
2011 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
2012}
2013
2014static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
2015 struct file *out, loff_t *ppos,
2016 size_t len, unsigned int flags)
2017{
2018 unsigned int head, tail, count;
2019 unsigned nbuf;
2020 unsigned idx;
2021 struct pipe_buffer *bufs;
2022 struct fuse_copy_state cs;
2023 struct fuse_dev *fud = __fuse_get_dev(out);
2024 size_t rem;
2025 ssize_t ret;
2026
2027 if (!fud)
2028 return -EPERM;
2029
2030 pipe_lock(pipe);
2031
2032 head = pipe->head;
2033 tail = pipe->tail;
2034 count = pipe_occupancy(head, tail);
2035
2036 bufs = kvmalloc_objs(struct pipe_buffer, count);
2037 if (!bufs) {
2038 pipe_unlock(pipe);
2039 return -ENOMEM;
2040 }
2041
2042 nbuf = 0;
2043 rem = 0;
2044 for (idx = tail; !pipe_empty(head, idx) && rem < len; idx++)
2045 rem += pipe_buf(pipe, idx)->len;
2046
2047 ret = -EINVAL;
2048 if (rem < len)
2049 goto out_free;
2050
2051 rem = len;
2052 while (rem) {
2053 struct pipe_buffer *ibuf;
2054 struct pipe_buffer *obuf;
2055
2056 if (WARN_ON(nbuf >= count || pipe_empty(head, tail)))
2057 goto out_free;
2058
2059 ibuf = pipe_buf(pipe, tail);
2060 obuf = &bufs[nbuf];
2061
2062 if (rem >= ibuf->len) {
2063 *obuf = *ibuf;
2064 ibuf->ops = NULL;
2065 tail++;
2066 pipe->tail = tail;
2067 } else {
2068 if (!pipe_buf_get(pipe, ibuf))
2069 goto out_free;
2070
2071 *obuf = *ibuf;
2072 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2073 obuf->len = rem;
2074 ibuf->offset += obuf->len;
2075 ibuf->len -= obuf->len;
2076 }
2077 nbuf++;
2078 rem -= obuf->len;
2079 }
2080 pipe_unlock(pipe);
2081
2082 fuse_copy_init(&cs, false, NULL);
2083 cs.pipebufs = bufs;
2084 cs.nr_segs = nbuf;
2085 cs.pipe = pipe;
2086
2087 if (flags & SPLICE_F_MOVE)
2088 cs.move_folios = true;
2089
2090 ret = fuse_dev_do_write(fud, &cs, len);
2091
2092 pipe_lock(pipe);
2093out_free:
2094 for (idx = 0; idx < nbuf; idx++) {
2095 struct pipe_buffer *buf = &bufs[idx];
2096
2097 if (buf->ops)
2098 pipe_buf_release(pipe, buf);
2099 }
2100 pipe_unlock(pipe);
2101
2102 kvfree(bufs);
2103 return ret;
2104}
2105
2106static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
2107{
2108 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
2109 struct fuse_iqueue *fiq;
2110 struct fuse_dev *fud = fuse_get_dev(file);
2111
2112 if (IS_ERR(fud))
2113 return EPOLLERR;
2114
2115 fiq = &fud->chan->iq;
2116 poll_wait(file, &fiq->waitq, wait);
2117
2118 spin_lock(&fiq->lock);
2119 if (!fiq->connected)
2120 mask = EPOLLERR;
2121 else if (request_pending(fiq))
2122 mask |= EPOLLIN | EPOLLRDNORM;
2123 spin_unlock(&fiq->lock);
2124
2125 return mask;
2126}
2127
2128/* Abort all requests on the given list (pending or processing) */
2129void fuse_dev_end_requests(struct list_head *head)
2130{
2131 while (!list_empty(head)) {
2132 struct fuse_req *req;
2133 req = list_entry(head->next, struct fuse_req, list);
2134 req->out.h.error = -ECONNABORTED;
2135 clear_bit(FR_SENT, &req->flags);
2136 list_del_init(&req->list);
2137 fuse_request_end(req);
2138 }
2139}
2140
2141/*
2142 * Abort all requests.
2143 *
2144 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2145 * filesystem.
2146 *
2147 * The same effect is usually achievable through killing the filesystem daemon
2148 * and all users of the filesystem. The exception is the combination of an
2149 * asynchronous request and the tricky deadlock (see
2150 * Documentation/filesystems/fuse/fuse.rst).
2151 *
2152 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2153 * requests, they should be finished off immediately. Locked requests will be
2154 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2155 * requests. It is possible that some request will finish before we can. This
2156 * is OK, the request will in that case be removed from the list before we touch
2157 * it.
2158 */
2159void fuse_chan_abort(struct fuse_chan *fch, bool abort_with_err)
2160{
2161 struct fuse_iqueue *fiq = &fch->iq;
2162
2163 fch->abort_with_err = abort_with_err;
2164
2165 spin_lock(&fch->lock);
2166 if (fch->connected) {
2167 struct fuse_dev *fud;
2168 struct fuse_req *req, *next;
2169 LIST_HEAD(to_end);
2170 unsigned int i;
2171
2172 if (fch->timeout.req_timeout)
2173 cancel_delayed_work(&fch->timeout.work);
2174
2175 /* Background queuing checks fch->connected under bg_lock */
2176 spin_lock(&fch->bg_lock);
2177 fch->connected = 0;
2178 spin_unlock(&fch->bg_lock);
2179
2180 fuse_chan_set_initialized(fch, NULL);
2181 list_for_each_entry(fud, &fch->devices, entry) {
2182 struct fuse_pqueue *fpq = &fud->pq;
2183
2184 spin_lock(&fpq->lock);
2185 fpq->connected = 0;
2186 list_for_each_entry_safe(req, next, &fpq->io, list) {
2187 req->out.h.error = -ECONNABORTED;
2188 spin_lock(&req->waitq.lock);
2189 set_bit(FR_ABORTED, &req->flags);
2190 if (!test_bit(FR_LOCKED, &req->flags)) {
2191 set_bit(FR_PRIVATE, &req->flags);
2192 __fuse_get_request(req);
2193 list_move(&req->list, &to_end);
2194 }
2195 spin_unlock(&req->waitq.lock);
2196 }
2197 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2198 list_splice_tail_init(&fpq->processing[i],
2199 &to_end);
2200 spin_unlock(&fpq->lock);
2201 }
2202 spin_lock(&fch->bg_lock);
2203 fch->blocked = 0;
2204 fch->max_background = UINT_MAX;
2205 flush_bg_queue(fch);
2206 spin_unlock(&fch->bg_lock);
2207
2208 spin_lock(&fiq->lock);
2209 fiq->connected = 0;
2210 list_for_each_entry(req, &fiq->pending, list)
2211 clear_bit(FR_PENDING, &req->flags);
2212 list_splice_tail_init(&fiq->pending, &to_end);
2213 while (forget_pending(fiq))
2214 kfree(fuse_dequeue_forget(fiq, 1, NULL));
2215 wake_up_all(&fiq->waitq);
2216 spin_unlock(&fiq->lock);
2217 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2218 fuse_end_polls(fch->conn);
2219 wake_up_all(&fch->blocked_waitq);
2220 spin_unlock(&fch->lock);
2221
2222 fuse_dev_end_requests(&to_end);
2223
2224 /*
2225 * fch->lock must not be taken to avoid conflicts with io-uring
2226 * locks
2227 */
2228 fuse_uring_abort(fch);
2229 } else {
2230 spin_unlock(&fch->lock);
2231 }
2232}
2233EXPORT_SYMBOL_GPL(fuse_chan_abort);
2234
2235void fuse_chan_wait_aborted(struct fuse_chan *fch)
2236{
2237 /* matches implicit memory barrier in fuse_drop_waiting() */
2238 smp_mb();
2239 wait_event(fch->blocked_waitq, fuse_chan_num_waiting(fch) == 0);
2240
2241 fuse_uring_wait_stopped_queues(fch);
2242}
2243
2244int fuse_dev_release(struct inode *inode, struct file *file)
2245{
2246 struct fuse_dev *fud = fuse_file_to_fud(file);
2247 /* Pairs with cmpxchg() in fuse_dev_install() */
2248 struct fuse_chan *fch = xchg(&fud->chan, FUSE_DEV_CHAN_DISCONNECTED);
2249
2250 if (fch) {
2251 struct fuse_pqueue *fpq = &fud->pq;
2252 LIST_HEAD(to_end);
2253 unsigned int i;
2254 bool last;
2255
2256 /* Make sure fuse_dev_install_with_pq() has finished */
2257 spin_lock(&fch->lock);
2258 spin_lock(&fpq->lock);
2259 WARN_ON(!list_empty(&fpq->io));
2260 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2261 list_splice_init(&fpq->processing[i], &to_end);
2262 spin_unlock(&fpq->lock);
2263
2264 list_del(&fud->entry);
2265 /* Are we the last open device? */
2266 last = list_empty(&fch->devices);
2267 spin_unlock(&fch->lock);
2268
2269 fuse_dev_end_requests(&to_end);
2270
2271 if (last) {
2272 WARN_ON(fch->iq.fasync != NULL);
2273 fuse_chan_abort(fch, false);
2274 }
2275 fuse_conn_put(fch->conn);
2276 }
2277 fuse_dev_put(fud);
2278 return 0;
2279}
2280EXPORT_SYMBOL_GPL(fuse_dev_release);
2281
2282static int fuse_dev_fasync(int fd, struct file *file, int on)
2283{
2284 struct fuse_dev *fud = fuse_get_dev(file);
2285
2286 if (IS_ERR(fud))
2287 return PTR_ERR(fud);
2288
2289 /* No locking - fasync_helper does its own locking */
2290 return fasync_helper(fd, file, on, &fud->chan->iq.fasync);
2291}
2292
2293static long fuse_dev_ioctl_clone(struct file *file, __u32 __user *argp)
2294{
2295 int oldfd;
2296 struct fuse_dev *fud, *new_fud;
2297 struct list_head *pq;
2298
2299 if (get_user(oldfd, argp))
2300 return -EFAULT;
2301
2302 CLASS(fd, f)(oldfd);
2303 if (fd_empty(f))
2304 return -EINVAL;
2305
2306 /*
2307 * Check against file->f_op because CUSE
2308 * uses the same ioctl handler.
2309 */
2310 if (fd_file(f)->f_op != file->f_op)
2311 return -EINVAL;
2312
2313 fud = fuse_get_dev(fd_file(f));
2314 if (IS_ERR(fud))
2315 return PTR_ERR(fud);
2316
2317 pq = fuse_pqueue_alloc();
2318 if (!pq)
2319 return -ENOMEM;
2320
2321 new_fud = fuse_file_to_fud(file);
2322 if (!fuse_dev_install_with_pq(new_fud, fud->chan, pq))
2323 return -EINVAL;
2324
2325 return 0;
2326}
2327
2328static long fuse_dev_ioctl_backing_open(struct file *file,
2329 struct fuse_backing_map __user *argp)
2330{
2331 struct fuse_dev *fud = fuse_get_dev(file);
2332 struct fuse_backing_map map;
2333
2334 if (IS_ERR(fud))
2335 return PTR_ERR(fud);
2336
2337 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
2338 return -EOPNOTSUPP;
2339
2340 if (copy_from_user(&map, argp, sizeof(map)))
2341 return -EFAULT;
2342
2343 return fuse_backing_open(fud->chan->conn, &map);
2344}
2345
2346static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
2347{
2348 struct fuse_dev *fud = fuse_get_dev(file);
2349 int backing_id;
2350
2351 if (IS_ERR(fud))
2352 return PTR_ERR(fud);
2353
2354 if (!IS_ENABLED(CONFIG_FUSE_PASSTHROUGH))
2355 return -EOPNOTSUPP;
2356
2357 if (get_user(backing_id, argp))
2358 return -EFAULT;
2359
2360 return fuse_backing_close(fud->chan->conn, backing_id);
2361}
2362
2363static long fuse_dev_ioctl_sync_init(struct file *file)
2364{
2365 struct fuse_dev *fud = fuse_file_to_fud(file);
2366
2367 if (fuse_dev_chan_get(fud))
2368 return -EINVAL;
2369
2370 fud->sync_init = true;
2371 return 0;
2372}
2373
2374static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2375 unsigned long arg)
2376{
2377 void __user *argp = (void __user *)arg;
2378
2379 switch (cmd) {
2380 case FUSE_DEV_IOC_CLONE:
2381 return fuse_dev_ioctl_clone(file, argp);
2382
2383 case FUSE_DEV_IOC_BACKING_OPEN:
2384 return fuse_dev_ioctl_backing_open(file, argp);
2385
2386 case FUSE_DEV_IOC_BACKING_CLOSE:
2387 return fuse_dev_ioctl_backing_close(file, argp);
2388
2389 case FUSE_DEV_IOC_SYNC_INIT:
2390 return fuse_dev_ioctl_sync_init(file);
2391
2392 default:
2393 return -ENOTTY;
2394 }
2395}
2396
2397#ifdef CONFIG_PROC_FS
2398static void fuse_dev_show_fdinfo(struct seq_file *seq, struct file *file)
2399{
2400 struct fuse_dev *fud = __fuse_get_dev(file);
2401 if (!fud)
2402 return;
2403
2404 seq_printf(seq, "fuse_connection:\t%u\n", fuse_conn_get_id(fud->chan->conn));
2405}
2406#endif
2407
2408const struct file_operations fuse_dev_operations = {
2409 .owner = THIS_MODULE,
2410 .open = fuse_dev_open,
2411 .read_iter = fuse_dev_read,
2412 .splice_read = fuse_dev_splice_read,
2413 .write_iter = fuse_dev_write,
2414 .splice_write = fuse_dev_splice_write,
2415 .poll = fuse_dev_poll,
2416 .release = fuse_dev_release,
2417 .fasync = fuse_dev_fasync,
2418 .unlocked_ioctl = fuse_dev_ioctl,
2419 .compat_ioctl = compat_ptr_ioctl,
2420#ifdef CONFIG_FUSE_IO_URING
2421 .uring_cmd = fuse_uring_cmd,
2422#endif
2423#ifdef CONFIG_PROC_FS
2424 .show_fdinfo = fuse_dev_show_fdinfo,
2425#endif
2426};
2427EXPORT_SYMBOL_GPL(fuse_dev_operations);
2428
2429static struct miscdevice fuse_miscdevice = {
2430 .minor = FUSE_MINOR,
2431 .name = "fuse",
2432 .fops = &fuse_dev_operations,
2433};
2434
2435int __init fuse_dev_init(void)
2436{
2437 int err = -ENOMEM;
2438 fuse_req_cachep = kmem_cache_create("fuse_request",
2439 sizeof(struct fuse_req),
2440 0, 0, NULL);
2441 if (!fuse_req_cachep)
2442 goto out;
2443
2444 err = misc_register(&fuse_miscdevice);
2445 if (err)
2446 goto out_cache_clean;
2447
2448 return 0;
2449
2450 out_cache_clean:
2451 kmem_cache_destroy(fuse_req_cachep);
2452 out:
2453 return err;
2454}
2455
2456void fuse_dev_cleanup(void)
2457{
2458 misc_deregister(&fuse_miscdevice);
2459 kmem_cache_destroy(fuse_req_cachep);
2460}