• 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 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
27 MODULE_ALIAS("devname:fuse");
28 
29 /* Ordinary requests have even IDs, while interrupts IDs are odd */
30 #define FUSE_INT_REQ_BIT (1ULL << 0)
31 #define FUSE_REQ_ID_STEP (1ULL << 1)
32 
33 static struct kmem_cache *fuse_req_cachep;
34 
fuse_get_dev(struct file * file)35 static struct fuse_dev *fuse_get_dev(struct file *file)
36 {
37 	/*
38 	 * Lockless access is OK, because file->private data is set
39 	 * once during mount and is valid until the file is released.
40 	 */
41 	return READ_ONCE(file->private_data);
42 }
43 
fuse_request_init(struct fuse_mount * fm,struct fuse_req * req)44 static void fuse_request_init(struct fuse_mount *fm, struct fuse_req *req)
45 {
46 	INIT_LIST_HEAD(&req->list);
47 	INIT_LIST_HEAD(&req->intr_entry);
48 	init_waitqueue_head(&req->waitq);
49 	refcount_set(&req->count, 1);
50 	__set_bit(FR_PENDING, &req->flags);
51 	req->fm = fm;
52 }
53 
fuse_request_alloc(struct fuse_mount * fm,gfp_t flags)54 static struct fuse_req *fuse_request_alloc(struct fuse_mount *fm, gfp_t flags)
55 {
56 	struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
57 	if (req)
58 		fuse_request_init(fm, req);
59 
60 	return req;
61 }
62 
fuse_request_free(struct fuse_req * req)63 static void fuse_request_free(struct fuse_req *req)
64 {
65 	kmem_cache_free(fuse_req_cachep, req);
66 }
67 
__fuse_get_request(struct fuse_req * req)68 static void __fuse_get_request(struct fuse_req *req)
69 {
70 	refcount_inc(&req->count);
71 }
72 
73 /* Must be called with > 1 refcount */
__fuse_put_request(struct fuse_req * req)74 static void __fuse_put_request(struct fuse_req *req)
75 {
76 	refcount_dec(&req->count);
77 }
78 
fuse_set_initialized(struct fuse_conn * fc)79 void fuse_set_initialized(struct fuse_conn *fc)
80 {
81 	/* Make sure stores before this are seen on another CPU */
82 	smp_wmb();
83 	fc->initialized = 1;
84 }
85 
fuse_block_alloc(struct fuse_conn * fc,bool for_background)86 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
87 {
88 	return !fc->initialized || (for_background && fc->blocked);
89 }
90 
fuse_drop_waiting(struct fuse_conn * fc)91 static void fuse_drop_waiting(struct fuse_conn *fc)
92 {
93 	/*
94 	 * lockess check of fc->connected is okay, because atomic_dec_and_test()
95 	 * provides a memory barrier matched with the one in fuse_wait_aborted()
96 	 * to ensure no wake-up is missed.
97 	 */
98 	if (atomic_dec_and_test(&fc->num_waiting) &&
99 	    !READ_ONCE(fc->connected)) {
100 		/* wake up aborters */
101 		wake_up_all(&fc->blocked_waitq);
102 	}
103 }
104 
105 static void fuse_put_request(struct fuse_req *req);
106 
fuse_get_req(struct fuse_mount * fm,bool for_background)107 static struct fuse_req *fuse_get_req(struct fuse_mount *fm, bool for_background)
108 {
109 	struct fuse_conn *fc = fm->fc;
110 	struct fuse_req *req;
111 	int err;
112 	atomic_inc(&fc->num_waiting);
113 
114 	if (fuse_block_alloc(fc, for_background)) {
115 		err = -EINTR;
116 		if (wait_event_killable_exclusive(fc->blocked_waitq,
117 				!fuse_block_alloc(fc, for_background)))
118 			goto out;
119 	}
120 	/* Matches smp_wmb() in fuse_set_initialized() */
121 	smp_rmb();
122 
123 	err = -ENOTCONN;
124 	if (!fc->connected)
125 		goto out;
126 
127 	err = -ECONNREFUSED;
128 	if (fc->conn_error)
129 		goto out;
130 
131 	req = fuse_request_alloc(fm, GFP_KERNEL);
132 	err = -ENOMEM;
133 	if (!req) {
134 		if (for_background)
135 			wake_up(&fc->blocked_waitq);
136 		goto out;
137 	}
138 
139 	req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
140 	req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
141 	req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
142 
143 	__set_bit(FR_WAITING, &req->flags);
144 	if (for_background)
145 		__set_bit(FR_BACKGROUND, &req->flags);
146 
147 	if (unlikely(req->in.h.uid == ((uid_t)-1) ||
148 		     req->in.h.gid == ((gid_t)-1))) {
149 		fuse_put_request(req);
150 		return ERR_PTR(-EOVERFLOW);
151 	}
152 	return req;
153 
154  out:
155 	fuse_drop_waiting(fc);
156 	return ERR_PTR(err);
157 }
158 
fuse_put_request(struct fuse_req * req)159 static void fuse_put_request(struct fuse_req *req)
160 {
161 	struct fuse_conn *fc = req->fm->fc;
162 
163 	if (refcount_dec_and_test(&req->count)) {
164 		if (test_bit(FR_BACKGROUND, &req->flags)) {
165 			/*
166 			 * We get here in the unlikely case that a background
167 			 * request was allocated but not sent
168 			 */
169 			spin_lock(&fc->bg_lock);
170 			if (!fc->blocked)
171 				wake_up(&fc->blocked_waitq);
172 			spin_unlock(&fc->bg_lock);
173 		}
174 
175 		if (test_bit(FR_WAITING, &req->flags)) {
176 			__clear_bit(FR_WAITING, &req->flags);
177 			fuse_drop_waiting(fc);
178 		}
179 
180 		fuse_request_free(req);
181 	}
182 }
183 
fuse_len_args(unsigned int numargs,struct fuse_arg * args)184 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
185 {
186 	unsigned nbytes = 0;
187 	unsigned i;
188 
189 	for (i = 0; i < numargs; i++)
190 		nbytes += args[i].size;
191 
192 	return nbytes;
193 }
194 EXPORT_SYMBOL_GPL(fuse_len_args);
195 
fuse_get_unique(struct fuse_iqueue * fiq)196 u64 fuse_get_unique(struct fuse_iqueue *fiq)
197 {
198 	fiq->reqctr += FUSE_REQ_ID_STEP;
199 	return fiq->reqctr;
200 }
201 EXPORT_SYMBOL_GPL(fuse_get_unique);
202 
fuse_req_hash(u64 unique)203 static unsigned int fuse_req_hash(u64 unique)
204 {
205 	return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
206 }
207 
208 /**
209  * A new request is available, wake fiq->waitq
210  */
fuse_dev_wake_and_unlock(struct fuse_iqueue * fiq,bool sync)211 static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq, bool sync)
212 __releases(fiq->lock)
213 {
214 	if (sync)
215 		wake_up_sync(&fiq->waitq);
216 	else
217 		wake_up(&fiq->waitq);
218 	kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
219 	spin_unlock(&fiq->lock);
220 }
221 
222 const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
223 	.wake_forget_and_unlock		= fuse_dev_wake_and_unlock,
224 	.wake_interrupt_and_unlock	= fuse_dev_wake_and_unlock,
225 	.wake_pending_and_unlock	= fuse_dev_wake_and_unlock,
226 };
227 EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
228 
queue_request_and_unlock(struct fuse_iqueue * fiq,struct fuse_req * req,bool sync)229 static void queue_request_and_unlock(struct fuse_iqueue *fiq,
230 				     struct fuse_req *req, bool sync)
231 __releases(fiq->lock)
232 {
233 	req->in.h.len = sizeof(struct fuse_in_header) +
234 		fuse_len_args(req->args->in_numargs,
235 			      (struct fuse_arg *) req->args->in_args);
236 	list_add_tail(&req->list, &fiq->pending);
237 	fiq->ops->wake_pending_and_unlock(fiq, sync);
238 }
239 
fuse_queue_forget(struct fuse_conn * fc,struct fuse_forget_link * forget,u64 nodeid,u64 nlookup)240 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
241 		       u64 nodeid, u64 nlookup)
242 {
243 	struct fuse_iqueue *fiq = &fc->iq;
244 
245 	if (nodeid == 0) {
246 		kfree(forget);
247 		return;
248 	}
249 
250 	forget->forget_one.nodeid = nodeid;
251 	forget->forget_one.nlookup = nlookup;
252 
253 	spin_lock(&fiq->lock);
254 	if (fiq->connected) {
255 		fiq->forget_list_tail->next = forget;
256 		fiq->forget_list_tail = forget;
257 		fiq->ops->wake_forget_and_unlock(fiq, false);
258 	} else {
259 		kfree(forget);
260 		spin_unlock(&fiq->lock);
261 	}
262 }
263 
flush_bg_queue(struct fuse_conn * fc)264 static void flush_bg_queue(struct fuse_conn *fc)
265 {
266 	struct fuse_iqueue *fiq = &fc->iq;
267 
268 	while (fc->active_background < fc->max_background &&
269 	       !list_empty(&fc->bg_queue)) {
270 		struct fuse_req *req;
271 
272 		req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
273 		list_del(&req->list);
274 		fc->active_background++;
275 		spin_lock(&fiq->lock);
276 		req->in.h.unique = fuse_get_unique(fiq);
277 		queue_request_and_unlock(fiq, req, false);
278 	}
279 }
280 
281 /*
282  * This function is called when a request is finished.  Either a reply
283  * has arrived or it was aborted (and not yet sent) or some error
284  * occurred during communication with userspace, or the device file
285  * was closed.  The requester thread is woken up (if still waiting),
286  * the 'end' callback is called if given, else the reference to the
287  * request is released
288  */
fuse_request_end(struct fuse_req * req)289 void fuse_request_end(struct fuse_req *req)
290 {
291 	struct fuse_mount *fm = req->fm;
292 	struct fuse_conn *fc = fm->fc;
293 	struct fuse_iqueue *fiq = &fc->iq;
294 
295 	if (test_and_set_bit(FR_FINISHED, &req->flags))
296 		goto put_request;
297 
298 	/*
299 	 * test_and_set_bit() implies smp_mb() between bit
300 	 * changing and below FR_INTERRUPTED check. Pairs with
301 	 * smp_mb() from queue_interrupt().
302 	 */
303 	if (test_bit(FR_INTERRUPTED, &req->flags)) {
304 		spin_lock(&fiq->lock);
305 		list_del_init(&req->intr_entry);
306 		spin_unlock(&fiq->lock);
307 	}
308 	WARN_ON(test_bit(FR_PENDING, &req->flags));
309 	WARN_ON(test_bit(FR_SENT, &req->flags));
310 	if (test_bit(FR_BACKGROUND, &req->flags)) {
311 		spin_lock(&fc->bg_lock);
312 		clear_bit(FR_BACKGROUND, &req->flags);
313 		if (fc->num_background == fc->max_background) {
314 			fc->blocked = 0;
315 			wake_up(&fc->blocked_waitq);
316 		} else if (!fc->blocked) {
317 			/*
318 			 * Wake up next waiter, if any.  It's okay to use
319 			 * waitqueue_active(), as we've already synced up
320 			 * fc->blocked with waiters with the wake_up() call
321 			 * above.
322 			 */
323 			if (waitqueue_active(&fc->blocked_waitq))
324 				wake_up(&fc->blocked_waitq);
325 		}
326 
327 		if (fc->num_background == fc->congestion_threshold && fm->sb) {
328 			clear_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
329 			clear_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
330 		}
331 		fc->num_background--;
332 		fc->active_background--;
333 		flush_bg_queue(fc);
334 		spin_unlock(&fc->bg_lock);
335 	} else {
336 		/* Wake up waiter sleeping in request_wait_answer() */
337 		wake_up(&req->waitq);
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.padding = args->error_in;
492 	req->args = args;
493 	if (args->end)
494 		__set_bit(FR_ASYNC, &req->flags);
495 }
496 
fuse_simple_request(struct fuse_mount * fm,struct fuse_args * args)497 ssize_t fuse_simple_request(struct fuse_mount *fm, struct fuse_args *args)
498 {
499 	struct fuse_conn *fc = fm->fc;
500 	struct fuse_req *req;
501 	ssize_t ret;
502 
503 	if (args->force) {
504 		atomic_inc(&fc->num_waiting);
505 		req = fuse_request_alloc(fm, GFP_KERNEL | __GFP_NOFAIL);
506 
507 		if (!args->nocreds)
508 			fuse_force_creds(req);
509 
510 		__set_bit(FR_WAITING, &req->flags);
511 		__set_bit(FR_FORCE, &req->flags);
512 	} else {
513 		WARN_ON(args->nocreds);
514 		req = fuse_get_req(fm, false);
515 		if (IS_ERR(req))
516 			return PTR_ERR(req);
517 	}
518 
519 	/* Needs to be done after fuse_get_req() so that fc->minor is valid */
520 	fuse_adjust_compat(fc, args);
521 	fuse_args_to_req(req, args);
522 
523 	if (!args->noreply)
524 		__set_bit(FR_ISREPLY, &req->flags);
525 	__fuse_request_send(req);
526 	ret = req->out.h.error;
527 	if (!ret && args->out_argvar) {
528 		BUG_ON(args->out_numargs == 0);
529 		ret = args->out_args[args->out_numargs - 1].size;
530 	}
531 	fuse_put_request(req);
532 
533 	return ret;
534 }
535 
fuse_request_queue_background(struct fuse_req * req)536 static bool fuse_request_queue_background(struct fuse_req *req)
537 {
538 	struct fuse_mount *fm = req->fm;
539 	struct fuse_conn *fc = fm->fc;
540 	bool queued = false;
541 
542 	WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
543 	if (!test_bit(FR_WAITING, &req->flags)) {
544 		__set_bit(FR_WAITING, &req->flags);
545 		atomic_inc(&fc->num_waiting);
546 	}
547 	__set_bit(FR_ISREPLY, &req->flags);
548 	spin_lock(&fc->bg_lock);
549 	if (likely(fc->connected)) {
550 		fc->num_background++;
551 		if (fc->num_background == fc->max_background)
552 			fc->blocked = 1;
553 		if (fc->num_background == fc->congestion_threshold && fm->sb) {
554 			set_bdi_congested(fm->sb->s_bdi, BLK_RW_SYNC);
555 			set_bdi_congested(fm->sb->s_bdi, BLK_RW_ASYNC);
556 		}
557 		list_add_tail(&req->list, &fc->bg_queue);
558 		flush_bg_queue(fc);
559 		queued = true;
560 	}
561 	spin_unlock(&fc->bg_lock);
562 
563 	return queued;
564 }
565 
fuse_simple_background(struct fuse_mount * fm,struct fuse_args * args,gfp_t gfp_flags)566 int fuse_simple_background(struct fuse_mount *fm, struct fuse_args *args,
567 			    gfp_t gfp_flags)
568 {
569 	struct fuse_req *req;
570 
571 	if (args->force) {
572 		WARN_ON(!args->nocreds);
573 		req = fuse_request_alloc(fm, gfp_flags);
574 		if (!req)
575 			return -ENOMEM;
576 		__set_bit(FR_BACKGROUND, &req->flags);
577 	} else {
578 		WARN_ON(args->nocreds);
579 		req = fuse_get_req(fm, true);
580 		if (IS_ERR(req))
581 			return PTR_ERR(req);
582 	}
583 
584 	fuse_args_to_req(req, args);
585 
586 	if (!fuse_request_queue_background(req)) {
587 		fuse_put_request(req);
588 		return -ENOTCONN;
589 	}
590 
591 	return 0;
592 }
593 EXPORT_SYMBOL_GPL(fuse_simple_background);
594 
fuse_simple_notify_reply(struct fuse_mount * fm,struct fuse_args * args,u64 unique)595 static int fuse_simple_notify_reply(struct fuse_mount *fm,
596 				    struct fuse_args *args, u64 unique)
597 {
598 	struct fuse_req *req;
599 	struct fuse_iqueue *fiq = &fm->fc->iq;
600 	int err = 0;
601 
602 	req = fuse_get_req(fm, false);
603 	if (IS_ERR(req))
604 		return PTR_ERR(req);
605 
606 	__clear_bit(FR_ISREPLY, &req->flags);
607 	req->in.h.unique = unique;
608 
609 	fuse_args_to_req(req, args);
610 
611 	spin_lock(&fiq->lock);
612 	if (fiq->connected) {
613 		queue_request_and_unlock(fiq, req, false);
614 	} else {
615 		err = -ENODEV;
616 		spin_unlock(&fiq->lock);
617 		fuse_put_request(req);
618 	}
619 
620 	return err;
621 }
622 
623 /*
624  * Lock the request.  Up to the next unlock_request() there mustn't be
625  * anything that could cause a page-fault.  If the request was already
626  * aborted bail out.
627  */
lock_request(struct fuse_req * req)628 static int lock_request(struct fuse_req *req)
629 {
630 	int err = 0;
631 	if (req) {
632 		spin_lock(&req->waitq.lock);
633 		if (test_bit(FR_ABORTED, &req->flags))
634 			err = -ENOENT;
635 		else
636 			set_bit(FR_LOCKED, &req->flags);
637 		spin_unlock(&req->waitq.lock);
638 	}
639 	return err;
640 }
641 
642 /*
643  * Unlock request.  If it was aborted while locked, caller is responsible
644  * for unlocking and ending the request.
645  */
unlock_request(struct fuse_req * req)646 static int unlock_request(struct fuse_req *req)
647 {
648 	int err = 0;
649 	if (req) {
650 		spin_lock(&req->waitq.lock);
651 		if (test_bit(FR_ABORTED, &req->flags))
652 			err = -ENOENT;
653 		else
654 			clear_bit(FR_LOCKED, &req->flags);
655 		spin_unlock(&req->waitq.lock);
656 	}
657 	return err;
658 }
659 
660 struct fuse_copy_state {
661 	int write;
662 	struct fuse_req *req;
663 	struct iov_iter *iter;
664 	struct pipe_buffer *pipebufs;
665 	struct pipe_buffer *currbuf;
666 	struct pipe_inode_info *pipe;
667 	unsigned long nr_segs;
668 	struct page *pg;
669 	unsigned len;
670 	unsigned offset;
671 	unsigned move_pages:1;
672 };
673 
fuse_copy_init(struct fuse_copy_state * cs,int write,struct iov_iter * iter)674 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
675 			   struct iov_iter *iter)
676 {
677 	memset(cs, 0, sizeof(*cs));
678 	cs->write = write;
679 	cs->iter = iter;
680 }
681 
682 /* Unmap and put previous page of userspace buffer */
fuse_copy_finish(struct fuse_copy_state * cs)683 static void fuse_copy_finish(struct fuse_copy_state *cs)
684 {
685 	if (cs->currbuf) {
686 		struct pipe_buffer *buf = cs->currbuf;
687 
688 		if (cs->write)
689 			buf->len = PAGE_SIZE - cs->len;
690 		cs->currbuf = NULL;
691 	} else if (cs->pg) {
692 		if (cs->write) {
693 			flush_dcache_page(cs->pg);
694 			set_page_dirty_lock(cs->pg);
695 		}
696 		put_page(cs->pg);
697 	}
698 	cs->pg = NULL;
699 }
700 
701 /*
702  * Get another pagefull of userspace buffer, and map it to kernel
703  * address space, and lock request
704  */
fuse_copy_fill(struct fuse_copy_state * cs)705 static int fuse_copy_fill(struct fuse_copy_state *cs)
706 {
707 	struct page *page;
708 	int err;
709 
710 	err = unlock_request(cs->req);
711 	if (err)
712 		return err;
713 
714 	fuse_copy_finish(cs);
715 	if (cs->pipebufs) {
716 		struct pipe_buffer *buf = cs->pipebufs;
717 
718 		if (!cs->write) {
719 			err = pipe_buf_confirm(cs->pipe, buf);
720 			if (err)
721 				return err;
722 
723 			BUG_ON(!cs->nr_segs);
724 			cs->currbuf = buf;
725 			cs->pg = buf->page;
726 			cs->offset = buf->offset;
727 			cs->len = buf->len;
728 			cs->pipebufs++;
729 			cs->nr_segs--;
730 		} else {
731 			if (cs->nr_segs >= cs->pipe->max_usage)
732 				return -EIO;
733 
734 			page = alloc_page(GFP_HIGHUSER);
735 			if (!page)
736 				return -ENOMEM;
737 
738 			buf->page = page;
739 			buf->offset = 0;
740 			buf->len = 0;
741 
742 			cs->currbuf = buf;
743 			cs->pg = page;
744 			cs->offset = 0;
745 			cs->len = PAGE_SIZE;
746 			cs->pipebufs++;
747 			cs->nr_segs++;
748 		}
749 	} else {
750 		size_t off;
751 		err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
752 		if (err < 0)
753 			return err;
754 		BUG_ON(!err);
755 		cs->len = err;
756 		cs->offset = off;
757 		cs->pg = page;
758 		iov_iter_advance(cs->iter, err);
759 	}
760 
761 	return lock_request(cs->req);
762 }
763 
764 /* Do as much copy to/from userspace buffer as we can */
fuse_copy_do(struct fuse_copy_state * cs,void ** val,unsigned * size)765 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
766 {
767 	unsigned ncpy = min(*size, cs->len);
768 	if (val) {
769 		void *pgaddr = kmap_atomic(cs->pg);
770 		void *buf = pgaddr + cs->offset;
771 
772 		if (cs->write)
773 			memcpy(buf, *val, ncpy);
774 		else
775 			memcpy(*val, buf, ncpy);
776 
777 		kunmap_atomic(pgaddr);
778 		*val += ncpy;
779 	}
780 	*size -= ncpy;
781 	cs->len -= ncpy;
782 	cs->offset += ncpy;
783 	return ncpy;
784 }
785 
fuse_check_page(struct page * page)786 static int fuse_check_page(struct page *page)
787 {
788 	if (page_mapcount(page) ||
789 	    page->mapping != NULL ||
790 	    (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
791 	     ~(1 << PG_locked |
792 	       1 << PG_referenced |
793 	       1 << PG_uptodate |
794 	       1 << PG_lru |
795 	       1 << PG_active |
796 	       1 << PG_workingset |
797 	       1 << PG_reclaim |
798 	       1 << PG_waiters |
799 	       LRU_GEN_MASK | LRU_REFS_MASK))) {
800 		dump_page(page, "fuse: trying to steal weird page");
801 		return 1;
802 	}
803 	return 0;
804 }
805 
fuse_try_move_page(struct fuse_copy_state * cs,struct page ** pagep)806 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
807 {
808 	int err;
809 	struct page *oldpage = *pagep;
810 	struct page *newpage;
811 	struct pipe_buffer *buf = cs->pipebufs;
812 
813 	get_page(oldpage);
814 	err = unlock_request(cs->req);
815 	if (err)
816 		goto out_put_old;
817 
818 	fuse_copy_finish(cs);
819 
820 	err = pipe_buf_confirm(cs->pipe, buf);
821 	if (err)
822 		goto out_put_old;
823 
824 	BUG_ON(!cs->nr_segs);
825 	cs->currbuf = buf;
826 	cs->len = buf->len;
827 	cs->pipebufs++;
828 	cs->nr_segs--;
829 
830 	if (cs->len != PAGE_SIZE)
831 		goto out_fallback;
832 
833 	if (!pipe_buf_try_steal(cs->pipe, buf))
834 		goto out_fallback;
835 
836 	newpage = buf->page;
837 
838 	if (!PageUptodate(newpage))
839 		SetPageUptodate(newpage);
840 
841 	ClearPageMappedToDisk(newpage);
842 
843 	if (fuse_check_page(newpage) != 0)
844 		goto out_fallback_unlock;
845 
846 	/*
847 	 * This is a new and locked page, it shouldn't be mapped or
848 	 * have any special flags on it
849 	 */
850 	if (WARN_ON(page_mapped(oldpage)))
851 		goto out_fallback_unlock;
852 	if (WARN_ON(page_has_private(oldpage)))
853 		goto out_fallback_unlock;
854 	if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
855 		goto out_fallback_unlock;
856 	if (WARN_ON(PageMlocked(oldpage)))
857 		goto out_fallback_unlock;
858 
859 	replace_page_cache_page(oldpage, newpage);
860 
861 	get_page(newpage);
862 
863 	if (!(buf->flags & PIPE_BUF_FLAG_LRU))
864 		lru_cache_add(newpage);
865 
866 	/*
867 	 * Release while we have extra ref on stolen page.  Otherwise
868 	 * anon_pipe_buf_release() might think the page can be reused.
869 	 */
870 	pipe_buf_release(cs->pipe, buf);
871 
872 	err = 0;
873 	spin_lock(&cs->req->waitq.lock);
874 	if (test_bit(FR_ABORTED, &cs->req->flags))
875 		err = -ENOENT;
876 	else
877 		*pagep = newpage;
878 	spin_unlock(&cs->req->waitq.lock);
879 
880 	if (err) {
881 		unlock_page(newpage);
882 		put_page(newpage);
883 		goto out_put_old;
884 	}
885 
886 	unlock_page(oldpage);
887 	/* Drop ref for ap->pages[] array */
888 	put_page(oldpage);
889 	cs->len = 0;
890 
891 	err = 0;
892 out_put_old:
893 	/* Drop ref obtained in this function */
894 	put_page(oldpage);
895 	return err;
896 
897 out_fallback_unlock:
898 	unlock_page(newpage);
899 out_fallback:
900 	cs->pg = buf->page;
901 	cs->offset = buf->offset;
902 
903 	err = lock_request(cs->req);
904 	if (!err)
905 		err = 1;
906 
907 	goto out_put_old;
908 }
909 
fuse_ref_page(struct fuse_copy_state * cs,struct page * page,unsigned offset,unsigned count)910 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
911 			 unsigned offset, unsigned count)
912 {
913 	struct pipe_buffer *buf;
914 	int err;
915 
916 	if (cs->nr_segs >= cs->pipe->max_usage)
917 		return -EIO;
918 
919 	get_page(page);
920 	err = unlock_request(cs->req);
921 	if (err) {
922 		put_page(page);
923 		return err;
924 	}
925 
926 	fuse_copy_finish(cs);
927 
928 	buf = cs->pipebufs;
929 	buf->page = page;
930 	buf->offset = offset;
931 	buf->len = count;
932 
933 	cs->pipebufs++;
934 	cs->nr_segs++;
935 	cs->len = 0;
936 
937 	return 0;
938 }
939 
940 /*
941  * Copy a page in the request to/from the userspace buffer.  Must be
942  * done atomically
943  */
fuse_copy_page(struct fuse_copy_state * cs,struct page ** pagep,unsigned offset,unsigned count,int zeroing)944 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
945 			  unsigned offset, unsigned count, int zeroing)
946 {
947 	int err;
948 	struct page *page = *pagep;
949 
950 	if (page && zeroing && count < PAGE_SIZE)
951 		clear_highpage(page);
952 
953 	while (count) {
954 		if (cs->write && cs->pipebufs && page) {
955 			/*
956 			 * Can't control lifetime of pipe buffers, so always
957 			 * copy user pages.
958 			 */
959 			if (cs->req->args->user_pages) {
960 				err = fuse_copy_fill(cs);
961 				if (err)
962 					return err;
963 			} else {
964 				return fuse_ref_page(cs, page, offset, count);
965 			}
966 		} else if (!cs->len) {
967 			if (cs->move_pages && page &&
968 			    offset == 0 && count == PAGE_SIZE) {
969 				err = fuse_try_move_page(cs, pagep);
970 				if (err <= 0)
971 					return err;
972 			} else {
973 				err = fuse_copy_fill(cs);
974 				if (err)
975 					return err;
976 			}
977 		}
978 		if (page) {
979 			void *mapaddr = kmap_atomic(page);
980 			void *buf = mapaddr + offset;
981 			offset += fuse_copy_do(cs, &buf, &count);
982 			kunmap_atomic(mapaddr);
983 		} else
984 			offset += fuse_copy_do(cs, NULL, &count);
985 	}
986 	if (page && !cs->write)
987 		flush_dcache_page(page);
988 	return 0;
989 }
990 
991 /* Copy pages in the request to/from userspace buffer */
fuse_copy_pages(struct fuse_copy_state * cs,unsigned nbytes,int zeroing)992 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
993 			   int zeroing)
994 {
995 	unsigned i;
996 	struct fuse_req *req = cs->req;
997 	struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
998 
999 
1000 	for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) {
1001 		int err;
1002 		unsigned int offset = ap->descs[i].offset;
1003 		unsigned int count = min(nbytes, ap->descs[i].length);
1004 
1005 		err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing);
1006 		if (err)
1007 			return err;
1008 
1009 		nbytes -= count;
1010 	}
1011 	return 0;
1012 }
1013 
1014 /* Copy a single argument in the request to/from userspace buffer */
fuse_copy_one(struct fuse_copy_state * cs,void * val,unsigned size)1015 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1016 {
1017 	while (size) {
1018 		if (!cs->len) {
1019 			int err = fuse_copy_fill(cs);
1020 			if (err)
1021 				return err;
1022 		}
1023 		fuse_copy_do(cs, &val, &size);
1024 	}
1025 	return 0;
1026 }
1027 
1028 /* 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)1029 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1030 			  unsigned argpages, struct fuse_arg *args,
1031 			  int zeroing)
1032 {
1033 	int err = 0;
1034 	unsigned i;
1035 
1036 	for (i = 0; !err && i < numargs; i++)  {
1037 		struct fuse_arg *arg = &args[i];
1038 		if (i == numargs - 1 && argpages)
1039 			err = fuse_copy_pages(cs, arg->size, zeroing);
1040 		else
1041 			err = fuse_copy_one(cs, arg->value, arg->size);
1042 	}
1043 	return err;
1044 }
1045 
forget_pending(struct fuse_iqueue * fiq)1046 static int forget_pending(struct fuse_iqueue *fiq)
1047 {
1048 	return fiq->forget_list_head.next != NULL;
1049 }
1050 
request_pending(struct fuse_iqueue * fiq)1051 static int request_pending(struct fuse_iqueue *fiq)
1052 {
1053 	return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1054 		forget_pending(fiq);
1055 }
1056 
1057 /*
1058  * Transfer an interrupt request to userspace
1059  *
1060  * Unlike other requests this is assembled on demand, without a need
1061  * to allocate a separate fuse_req structure.
1062  *
1063  * Called with fiq->lock held, releases it
1064  */
fuse_read_interrupt(struct fuse_iqueue * fiq,struct fuse_copy_state * cs,size_t nbytes,struct fuse_req * req)1065 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1066 			       struct fuse_copy_state *cs,
1067 			       size_t nbytes, struct fuse_req *req)
1068 __releases(fiq->lock)
1069 {
1070 	struct fuse_in_header ih;
1071 	struct fuse_interrupt_in arg;
1072 	unsigned reqsize = sizeof(ih) + sizeof(arg);
1073 	int err;
1074 
1075 	list_del_init(&req->intr_entry);
1076 	memset(&ih, 0, sizeof(ih));
1077 	memset(&arg, 0, sizeof(arg));
1078 	ih.len = reqsize;
1079 	ih.opcode = FUSE_INTERRUPT;
1080 	ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
1081 	arg.unique = req->in.h.unique;
1082 
1083 	spin_unlock(&fiq->lock);
1084 	if (nbytes < reqsize)
1085 		return -EINVAL;
1086 
1087 	err = fuse_copy_one(cs, &ih, sizeof(ih));
1088 	if (!err)
1089 		err = fuse_copy_one(cs, &arg, sizeof(arg));
1090 	fuse_copy_finish(cs);
1091 
1092 	return err ? err : reqsize;
1093 }
1094 
fuse_dequeue_forget(struct fuse_iqueue * fiq,unsigned int max,unsigned int * countp)1095 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1096 					     unsigned int max,
1097 					     unsigned int *countp)
1098 {
1099 	struct fuse_forget_link *head = fiq->forget_list_head.next;
1100 	struct fuse_forget_link **newhead = &head;
1101 	unsigned count;
1102 
1103 	for (count = 0; *newhead != NULL && count < max; count++)
1104 		newhead = &(*newhead)->next;
1105 
1106 	fiq->forget_list_head.next = *newhead;
1107 	*newhead = NULL;
1108 	if (fiq->forget_list_head.next == NULL)
1109 		fiq->forget_list_tail = &fiq->forget_list_head;
1110 
1111 	if (countp != NULL)
1112 		*countp = count;
1113 
1114 	return head;
1115 }
1116 EXPORT_SYMBOL(fuse_dequeue_forget);
1117 
fuse_read_single_forget(struct fuse_iqueue * fiq,struct fuse_copy_state * cs,size_t nbytes)1118 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1119 				   struct fuse_copy_state *cs,
1120 				   size_t nbytes)
1121 __releases(fiq->lock)
1122 {
1123 	int err;
1124 	struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
1125 	struct fuse_forget_in arg = {
1126 		.nlookup = forget->forget_one.nlookup,
1127 	};
1128 	struct fuse_in_header ih = {
1129 		.opcode = FUSE_FORGET,
1130 		.nodeid = forget->forget_one.nodeid,
1131 		.unique = fuse_get_unique(fiq),
1132 		.len = sizeof(ih) + sizeof(arg),
1133 	};
1134 
1135 	spin_unlock(&fiq->lock);
1136 	kfree(forget);
1137 	if (nbytes < ih.len)
1138 		return -EINVAL;
1139 
1140 	err = fuse_copy_one(cs, &ih, sizeof(ih));
1141 	if (!err)
1142 		err = fuse_copy_one(cs, &arg, sizeof(arg));
1143 	fuse_copy_finish(cs);
1144 
1145 	if (err)
1146 		return err;
1147 
1148 	return ih.len;
1149 }
1150 
fuse_read_batch_forget(struct fuse_iqueue * fiq,struct fuse_copy_state * cs,size_t nbytes)1151 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1152 				   struct fuse_copy_state *cs, size_t nbytes)
1153 __releases(fiq->lock)
1154 {
1155 	int err;
1156 	unsigned max_forgets;
1157 	unsigned count;
1158 	struct fuse_forget_link *head;
1159 	struct fuse_batch_forget_in arg = { .count = 0 };
1160 	struct fuse_in_header ih = {
1161 		.opcode = FUSE_BATCH_FORGET,
1162 		.unique = fuse_get_unique(fiq),
1163 		.len = sizeof(ih) + sizeof(arg),
1164 	};
1165 
1166 	if (nbytes < ih.len) {
1167 		spin_unlock(&fiq->lock);
1168 		return -EINVAL;
1169 	}
1170 
1171 	max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1172 	head = fuse_dequeue_forget(fiq, max_forgets, &count);
1173 	spin_unlock(&fiq->lock);
1174 
1175 	arg.count = count;
1176 	ih.len += count * sizeof(struct fuse_forget_one);
1177 	err = fuse_copy_one(cs, &ih, sizeof(ih));
1178 	if (!err)
1179 		err = fuse_copy_one(cs, &arg, sizeof(arg));
1180 
1181 	while (head) {
1182 		struct fuse_forget_link *forget = head;
1183 
1184 		if (!err) {
1185 			err = fuse_copy_one(cs, &forget->forget_one,
1186 					    sizeof(forget->forget_one));
1187 		}
1188 		head = forget->next;
1189 		kfree(forget);
1190 	}
1191 
1192 	fuse_copy_finish(cs);
1193 
1194 	if (err)
1195 		return err;
1196 
1197 	return ih.len;
1198 }
1199 
fuse_read_forget(struct fuse_conn * fc,struct fuse_iqueue * fiq,struct fuse_copy_state * cs,size_t nbytes)1200 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1201 			    struct fuse_copy_state *cs,
1202 			    size_t nbytes)
1203 __releases(fiq->lock)
1204 {
1205 	if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1206 		return fuse_read_single_forget(fiq, cs, nbytes);
1207 	else
1208 		return fuse_read_batch_forget(fiq, cs, nbytes);
1209 }
1210 
1211 /*
1212  * Read a single request into the userspace filesystem's buffer.  This
1213  * function waits until a request is available, then removes it from
1214  * the pending list and copies request data to userspace buffer.  If
1215  * no reply is needed (FORGET) or request has been aborted or there
1216  * was an error during the copying then it's finished by calling
1217  * fuse_request_end().  Otherwise add it to the processing list, and set
1218  * the 'sent' flag.
1219  */
fuse_dev_do_read(struct fuse_dev * fud,struct file * file,struct fuse_copy_state * cs,size_t nbytes)1220 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1221 				struct fuse_copy_state *cs, size_t nbytes)
1222 {
1223 	ssize_t err;
1224 	struct fuse_conn *fc = fud->fc;
1225 	struct fuse_iqueue *fiq = &fc->iq;
1226 	struct fuse_pqueue *fpq = &fud->pq;
1227 	struct fuse_req *req;
1228 	struct fuse_args *args;
1229 	unsigned reqsize;
1230 	unsigned int hash;
1231 
1232 	/*
1233 	 * Require sane minimum read buffer - that has capacity for fixed part
1234 	 * of any request header + negotiated max_write room for data.
1235 	 *
1236 	 * Historically libfuse reserves 4K for fixed header room, but e.g.
1237 	 * GlusterFS reserves only 80 bytes
1238 	 *
1239 	 *	= `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1240 	 *
1241 	 * which is the absolute minimum any sane filesystem should be using
1242 	 * for header room.
1243 	 */
1244 	if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1245 			   sizeof(struct fuse_in_header) +
1246 			   sizeof(struct fuse_write_in) +
1247 			   fc->max_write))
1248 		return -EINVAL;
1249 
1250  restart:
1251 	for (;;) {
1252 		spin_lock(&fiq->lock);
1253 		if (!fiq->connected || request_pending(fiq))
1254 			break;
1255 		spin_unlock(&fiq->lock);
1256 
1257 		if (file->f_flags & O_NONBLOCK)
1258 			return -EAGAIN;
1259 		err = wait_event_interruptible_exclusive(fiq->waitq,
1260 				!fiq->connected || request_pending(fiq));
1261 		if (err)
1262 			return err;
1263 	}
1264 
1265 	if (!fiq->connected) {
1266 		err = fc->aborted ? -ECONNABORTED : -ENODEV;
1267 		goto err_unlock;
1268 	}
1269 
1270 	if (!list_empty(&fiq->interrupts)) {
1271 		req = list_entry(fiq->interrupts.next, struct fuse_req,
1272 				 intr_entry);
1273 		return fuse_read_interrupt(fiq, cs, nbytes, req);
1274 	}
1275 
1276 	if (forget_pending(fiq)) {
1277 		if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1278 			return fuse_read_forget(fc, fiq, cs, nbytes);
1279 
1280 		if (fiq->forget_batch <= -8)
1281 			fiq->forget_batch = 16;
1282 	}
1283 
1284 	req = list_entry(fiq->pending.next, struct fuse_req, list);
1285 	clear_bit(FR_PENDING, &req->flags);
1286 	list_del_init(&req->list);
1287 	spin_unlock(&fiq->lock);
1288 
1289 	args = req->args;
1290 	reqsize = req->in.h.len;
1291 
1292 	/* If request is too large, reply with an error and restart the read */
1293 	if (nbytes < reqsize) {
1294 		req->out.h.error = -EIO;
1295 		/* SETXATTR is special, since it may contain too large data */
1296 		if (args->opcode == FUSE_SETXATTR)
1297 			req->out.h.error = -E2BIG;
1298 		fuse_request_end(req);
1299 		goto restart;
1300 	}
1301 	spin_lock(&fpq->lock);
1302 	/*
1303 	 *  Must not put request on fpq->io queue after having been shut down by
1304 	 *  fuse_abort_conn()
1305 	 */
1306 	if (!fpq->connected) {
1307 		req->out.h.error = err = -ECONNABORTED;
1308 		goto out_end;
1309 
1310 	}
1311 	list_add(&req->list, &fpq->io);
1312 	spin_unlock(&fpq->lock);
1313 	cs->req = req;
1314 	err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
1315 	if (!err)
1316 		err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1317 				     (struct fuse_arg *) args->in_args, 0);
1318 	fuse_copy_finish(cs);
1319 	spin_lock(&fpq->lock);
1320 	clear_bit(FR_LOCKED, &req->flags);
1321 	if (!fpq->connected) {
1322 		err = fc->aborted ? -ECONNABORTED : -ENODEV;
1323 		goto out_end;
1324 	}
1325 	if (err) {
1326 		req->out.h.error = -EIO;
1327 		goto out_end;
1328 	}
1329 	if (!test_bit(FR_ISREPLY, &req->flags)) {
1330 		err = reqsize;
1331 		goto out_end;
1332 	}
1333 	hash = fuse_req_hash(req->in.h.unique);
1334 	list_move_tail(&req->list, &fpq->processing[hash]);
1335 	__fuse_get_request(req);
1336 	set_bit(FR_SENT, &req->flags);
1337 	spin_unlock(&fpq->lock);
1338 	/* matches barrier in request_wait_answer() */
1339 	smp_mb__after_atomic();
1340 	if (test_bit(FR_INTERRUPTED, &req->flags))
1341 		queue_interrupt(req);
1342 	fuse_put_request(req);
1343 
1344 	return reqsize;
1345 
1346 out_end:
1347 	if (!test_bit(FR_PRIVATE, &req->flags))
1348 		list_del_init(&req->list);
1349 	spin_unlock(&fpq->lock);
1350 	fuse_request_end(req);
1351 	return err;
1352 
1353  err_unlock:
1354 	spin_unlock(&fiq->lock);
1355 	return err;
1356 }
1357 
fuse_dev_open(struct inode * inode,struct file * file)1358 static int fuse_dev_open(struct inode *inode, struct file *file)
1359 {
1360 	/*
1361 	 * The fuse device's file's private_data is used to hold
1362 	 * the fuse_conn(ection) when it is mounted, and is used to
1363 	 * keep track of whether the file has been mounted already.
1364 	 */
1365 	file->private_data = NULL;
1366 	return 0;
1367 }
1368 
fuse_dev_read(struct kiocb * iocb,struct iov_iter * to)1369 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1370 {
1371 	struct fuse_copy_state cs;
1372 	struct file *file = iocb->ki_filp;
1373 	struct fuse_dev *fud = fuse_get_dev(file);
1374 
1375 	if (!fud)
1376 		return -EPERM;
1377 
1378 	if (!iter_is_iovec(to))
1379 		return -EINVAL;
1380 
1381 	fuse_copy_init(&cs, 1, to);
1382 
1383 	return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1384 }
1385 
fuse_dev_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)1386 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1387 				    struct pipe_inode_info *pipe,
1388 				    size_t len, unsigned int flags)
1389 {
1390 	int total, ret;
1391 	int page_nr = 0;
1392 	struct pipe_buffer *bufs;
1393 	struct fuse_copy_state cs;
1394 	struct fuse_dev *fud = fuse_get_dev(in);
1395 
1396 	if (!fud)
1397 		return -EPERM;
1398 
1399 	bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer),
1400 			      GFP_KERNEL);
1401 	if (!bufs)
1402 		return -ENOMEM;
1403 
1404 	fuse_copy_init(&cs, 1, NULL);
1405 	cs.pipebufs = bufs;
1406 	cs.pipe = pipe;
1407 	ret = fuse_dev_do_read(fud, in, &cs, len);
1408 	if (ret < 0)
1409 		goto out;
1410 
1411 	if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) {
1412 		ret = -EIO;
1413 		goto out;
1414 	}
1415 
1416 	for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
1417 		/*
1418 		 * Need to be careful about this.  Having buf->ops in module
1419 		 * code can Oops if the buffer persists after module unload.
1420 		 */
1421 		bufs[page_nr].ops = &nosteal_pipe_buf_ops;
1422 		bufs[page_nr].flags = 0;
1423 		ret = add_to_pipe(pipe, &bufs[page_nr++]);
1424 		if (unlikely(ret < 0))
1425 			break;
1426 	}
1427 	if (total)
1428 		ret = total;
1429 out:
1430 	for (; page_nr < cs.nr_segs; page_nr++)
1431 		put_page(bufs[page_nr].page);
1432 
1433 	kvfree(bufs);
1434 	return ret;
1435 }
1436 
fuse_notify_poll(struct fuse_conn * fc,unsigned int size,struct fuse_copy_state * cs)1437 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1438 			    struct fuse_copy_state *cs)
1439 {
1440 	struct fuse_notify_poll_wakeup_out outarg;
1441 	int err = -EINVAL;
1442 
1443 	if (size != sizeof(outarg))
1444 		goto err;
1445 
1446 	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1447 	if (err)
1448 		goto err;
1449 
1450 	fuse_copy_finish(cs);
1451 	return fuse_notify_poll_wakeup(fc, &outarg);
1452 
1453 err:
1454 	fuse_copy_finish(cs);
1455 	return err;
1456 }
1457 
fuse_notify_inval_inode(struct fuse_conn * fc,unsigned int size,struct fuse_copy_state * cs)1458 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1459 				   struct fuse_copy_state *cs)
1460 {
1461 	struct fuse_notify_inval_inode_out outarg;
1462 	int err = -EINVAL;
1463 
1464 	if (size != sizeof(outarg))
1465 		goto err;
1466 
1467 	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1468 	if (err)
1469 		goto err;
1470 	fuse_copy_finish(cs);
1471 
1472 	down_read(&fc->killsb);
1473 	err = fuse_reverse_inval_inode(fc, outarg.ino,
1474 				       outarg.off, outarg.len);
1475 	up_read(&fc->killsb);
1476 	return err;
1477 
1478 err:
1479 	fuse_copy_finish(cs);
1480 	return err;
1481 }
1482 
fuse_notify_inval_entry(struct fuse_conn * fc,unsigned int size,struct fuse_copy_state * cs)1483 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1484 				   struct fuse_copy_state *cs)
1485 {
1486 	struct fuse_notify_inval_entry_out outarg;
1487 	int err = -ENOMEM;
1488 	char *buf;
1489 	struct qstr name;
1490 
1491 	buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1492 	if (!buf)
1493 		goto err;
1494 
1495 	err = -EINVAL;
1496 	if (size < sizeof(outarg))
1497 		goto err;
1498 
1499 	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1500 	if (err)
1501 		goto err;
1502 
1503 	err = -ENAMETOOLONG;
1504 	if (outarg.namelen > FUSE_NAME_MAX)
1505 		goto err;
1506 
1507 	err = -EINVAL;
1508 	if (size != sizeof(outarg) + outarg.namelen + 1)
1509 		goto err;
1510 
1511 	name.name = buf;
1512 	name.len = outarg.namelen;
1513 	err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1514 	if (err)
1515 		goto err;
1516 	fuse_copy_finish(cs);
1517 	buf[outarg.namelen] = 0;
1518 
1519 	down_read(&fc->killsb);
1520 	err = fuse_reverse_inval_entry(fc, outarg.parent, 0, &name);
1521 	up_read(&fc->killsb);
1522 	kfree(buf);
1523 	return err;
1524 
1525 err:
1526 	kfree(buf);
1527 	fuse_copy_finish(cs);
1528 	return err;
1529 }
1530 
fuse_notify_delete(struct fuse_conn * fc,unsigned int size,struct fuse_copy_state * cs)1531 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1532 			      struct fuse_copy_state *cs)
1533 {
1534 	struct fuse_notify_delete_out outarg;
1535 	int err = -ENOMEM;
1536 	char *buf;
1537 	struct qstr name;
1538 
1539 	buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1540 	if (!buf)
1541 		goto err;
1542 
1543 	err = -EINVAL;
1544 	if (size < sizeof(outarg))
1545 		goto err;
1546 
1547 	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1548 	if (err)
1549 		goto err;
1550 
1551 	err = -ENAMETOOLONG;
1552 	if (outarg.namelen > FUSE_NAME_MAX)
1553 		goto err;
1554 
1555 	err = -EINVAL;
1556 	if (size != sizeof(outarg) + outarg.namelen + 1)
1557 		goto err;
1558 
1559 	name.name = buf;
1560 	name.len = outarg.namelen;
1561 	err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1562 	if (err)
1563 		goto err;
1564 	fuse_copy_finish(cs);
1565 	buf[outarg.namelen] = 0;
1566 
1567 	down_read(&fc->killsb);
1568 	err = fuse_reverse_inval_entry(fc, outarg.parent, outarg.child, &name);
1569 	up_read(&fc->killsb);
1570 	kfree(buf);
1571 	return err;
1572 
1573 err:
1574 	kfree(buf);
1575 	fuse_copy_finish(cs);
1576 	return err;
1577 }
1578 
fuse_notify_store(struct fuse_conn * fc,unsigned int size,struct fuse_copy_state * cs)1579 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1580 			     struct fuse_copy_state *cs)
1581 {
1582 	struct fuse_notify_store_out outarg;
1583 	struct inode *inode;
1584 	struct address_space *mapping;
1585 	u64 nodeid;
1586 	int err;
1587 	pgoff_t index;
1588 	unsigned int offset;
1589 	unsigned int num;
1590 	loff_t file_size;
1591 	loff_t end;
1592 
1593 	err = -EINVAL;
1594 	if (size < sizeof(outarg))
1595 		goto out_finish;
1596 
1597 	err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1598 	if (err)
1599 		goto out_finish;
1600 
1601 	err = -EINVAL;
1602 	if (size - sizeof(outarg) != outarg.size)
1603 		goto out_finish;
1604 
1605 	nodeid = outarg.nodeid;
1606 
1607 	down_read(&fc->killsb);
1608 
1609 	err = -ENOENT;
1610 	inode = fuse_ilookup(fc, nodeid,  NULL);
1611 	if (!inode)
1612 		goto out_up_killsb;
1613 
1614 	mapping = inode->i_mapping;
1615 	index = outarg.offset >> PAGE_SHIFT;
1616 	offset = outarg.offset & ~PAGE_MASK;
1617 	file_size = i_size_read(inode);
1618 	end = outarg.offset + outarg.size;
1619 	if (end > file_size) {
1620 		file_size = end;
1621 		fuse_write_update_size(inode, file_size);
1622 	}
1623 
1624 	num = outarg.size;
1625 	while (num) {
1626 		struct page *page;
1627 		unsigned int this_num;
1628 
1629 		err = -ENOMEM;
1630 		page = find_or_create_page(mapping, index,
1631 					   mapping_gfp_mask(mapping));
1632 		if (!page)
1633 			goto out_iput;
1634 
1635 		this_num = min_t(unsigned, num, PAGE_SIZE - offset);
1636 		err = fuse_copy_page(cs, &page, offset, this_num, 0);
1637 		if (!err && offset == 0 &&
1638 		    (this_num == PAGE_SIZE || file_size == end))
1639 			SetPageUptodate(page);
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 (!iter_is_iovec(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 
2298 	switch (cmd) {
2299 	case FUSE_DEV_IOC_CLONE:
2300 		res = -EFAULT;
2301 		if (!get_user(oldfd, (__u32 __user *)arg)) {
2302 			struct file *old = fget(oldfd);
2303 
2304 			res = -EINVAL;
2305 			if (old) {
2306 				/*
2307 				 * Check against file->f_op because CUSE
2308 				 * uses the same ioctl handler.
2309 				 */
2310 				if (old->f_op == file->f_op &&
2311 				    old->f_cred->user_ns ==
2312 					    file->f_cred->user_ns)
2313 					fud = fuse_get_dev(old);
2314 
2315 				if (fud) {
2316 					mutex_lock(&fuse_mutex);
2317 					res = fuse_device_clone(fud->fc, file);
2318 					mutex_unlock(&fuse_mutex);
2319 				}
2320 				fput(old);
2321 			}
2322 		}
2323 		break;
2324 	case FUSE_DEV_IOC_PASSTHROUGH_OPEN:
2325 		res = -EFAULT;
2326 		if (!get_user(oldfd, (__u32 __user *)arg)) {
2327 			res = -EINVAL;
2328 			fud = fuse_get_dev(file);
2329 			if (fud)
2330 				res = fuse_passthrough_open(fud, oldfd);
2331 		}
2332 		break;
2333 	default:
2334 		res = -ENOTTY;
2335 		break;
2336 	}
2337 	return res;
2338 }
2339 
2340 const struct file_operations fuse_dev_operations = {
2341 	.owner		= THIS_MODULE,
2342 	.open		= fuse_dev_open,
2343 	.llseek		= no_llseek,
2344 	.read_iter	= fuse_dev_read,
2345 	.splice_read	= fuse_dev_splice_read,
2346 	.write_iter	= fuse_dev_write,
2347 	.splice_write	= fuse_dev_splice_write,
2348 	.poll		= fuse_dev_poll,
2349 	.release	= fuse_dev_release,
2350 	.fasync		= fuse_dev_fasync,
2351 	.unlocked_ioctl = fuse_dev_ioctl,
2352 	.compat_ioctl   = compat_ptr_ioctl,
2353 };
2354 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2355 
2356 static struct miscdevice fuse_miscdevice = {
2357 	.minor = FUSE_MINOR,
2358 	.name  = "fuse",
2359 	.fops = &fuse_dev_operations,
2360 };
2361 
fuse_dev_init(void)2362 int __init fuse_dev_init(void)
2363 {
2364 	int err = -ENOMEM;
2365 	fuse_req_cachep = kmem_cache_create("fuse_request",
2366 					    sizeof(struct fuse_req),
2367 					    0, 0, NULL);
2368 	if (!fuse_req_cachep)
2369 		goto out;
2370 
2371 	err = misc_register(&fuse_miscdevice);
2372 	if (err)
2373 		goto out_cache_clean;
2374 
2375 	return 0;
2376 
2377  out_cache_clean:
2378 	kmem_cache_destroy(fuse_req_cachep);
2379  out:
2380 	return err;
2381 }
2382 
fuse_dev_cleanup(void)2383 void fuse_dev_cleanup(void)
2384 {
2385 	misc_deregister(&fuse_miscdevice);
2386 	kmem_cache_destroy(fuse_req_cachep);
2387 }
2388