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