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