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