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