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