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