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