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