xref: /openbmc/linux/fs/fuse/file.c (revision 20e2fc42)
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/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
21 
22 static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 				      struct fuse_page_desc **desc)
24 {
25 	struct page **pages;
26 
27 	pages = kzalloc(npages * (sizeof(struct page *) +
28 				  sizeof(struct fuse_page_desc)), flags);
29 	*desc = (void *) (pages + npages);
30 
31 	return pages;
32 }
33 
34 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 			  int opcode, struct fuse_open_out *outargp)
36 {
37 	struct fuse_open_in inarg;
38 	FUSE_ARGS(args);
39 
40 	memset(&inarg, 0, sizeof(inarg));
41 	inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 	if (!fc->atomic_o_trunc)
43 		inarg.flags &= ~O_TRUNC;
44 	args.opcode = opcode;
45 	args.nodeid = nodeid;
46 	args.in_numargs = 1;
47 	args.in_args[0].size = sizeof(inarg);
48 	args.in_args[0].value = &inarg;
49 	args.out_numargs = 1;
50 	args.out_args[0].size = sizeof(*outargp);
51 	args.out_args[0].value = outargp;
52 
53 	return fuse_simple_request(fc, &args);
54 }
55 
56 struct fuse_release_args {
57 	struct fuse_args args;
58 	struct fuse_release_in inarg;
59 	struct inode *inode;
60 };
61 
62 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
63 {
64 	struct fuse_file *ff;
65 
66 	ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
67 	if (unlikely(!ff))
68 		return NULL;
69 
70 	ff->fc = fc;
71 	ff->release_args = kzalloc(sizeof(*ff->release_args),
72 				   GFP_KERNEL_ACCOUNT);
73 	if (!ff->release_args) {
74 		kfree(ff);
75 		return NULL;
76 	}
77 
78 	INIT_LIST_HEAD(&ff->write_entry);
79 	mutex_init(&ff->readdir.lock);
80 	refcount_set(&ff->count, 1);
81 	RB_CLEAR_NODE(&ff->polled_node);
82 	init_waitqueue_head(&ff->poll_wait);
83 
84 	ff->kh = atomic64_inc_return(&fc->khctr);
85 
86 	return ff;
87 }
88 
89 void fuse_file_free(struct fuse_file *ff)
90 {
91 	kfree(ff->release_args);
92 	mutex_destroy(&ff->readdir.lock);
93 	kfree(ff);
94 }
95 
96 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
97 {
98 	refcount_inc(&ff->count);
99 	return ff;
100 }
101 
102 static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
103 			     int error)
104 {
105 	struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
106 
107 	iput(ra->inode);
108 	kfree(ra);
109 }
110 
111 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
112 {
113 	if (refcount_dec_and_test(&ff->count)) {
114 		struct fuse_args *args = &ff->release_args->args;
115 
116 		if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
117 			/* Do nothing when client does not implement 'open' */
118 			fuse_release_end(ff->fc, args, 0);
119 		} else if (sync) {
120 			fuse_simple_request(ff->fc, args);
121 			fuse_release_end(ff->fc, args, 0);
122 		} else {
123 			args->end = fuse_release_end;
124 			if (fuse_simple_background(ff->fc, args,
125 						   GFP_KERNEL | __GFP_NOFAIL))
126 				fuse_release_end(ff->fc, args, -ENOTCONN);
127 		}
128 		kfree(ff);
129 	}
130 }
131 
132 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
133 		 bool isdir)
134 {
135 	struct fuse_file *ff;
136 	int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
137 
138 	ff = fuse_file_alloc(fc);
139 	if (!ff)
140 		return -ENOMEM;
141 
142 	ff->fh = 0;
143 	/* Default for no-open */
144 	ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
145 	if (isdir ? !fc->no_opendir : !fc->no_open) {
146 		struct fuse_open_out outarg;
147 		int err;
148 
149 		err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
150 		if (!err) {
151 			ff->fh = outarg.fh;
152 			ff->open_flags = outarg.open_flags;
153 
154 		} else if (err != -ENOSYS) {
155 			fuse_file_free(ff);
156 			return err;
157 		} else {
158 			if (isdir)
159 				fc->no_opendir = 1;
160 			else
161 				fc->no_open = 1;
162 		}
163 	}
164 
165 	if (isdir)
166 		ff->open_flags &= ~FOPEN_DIRECT_IO;
167 
168 	ff->nodeid = nodeid;
169 	file->private_data = ff;
170 
171 	return 0;
172 }
173 EXPORT_SYMBOL_GPL(fuse_do_open);
174 
175 static void fuse_link_write_file(struct file *file)
176 {
177 	struct inode *inode = file_inode(file);
178 	struct fuse_inode *fi = get_fuse_inode(inode);
179 	struct fuse_file *ff = file->private_data;
180 	/*
181 	 * file may be written through mmap, so chain it onto the
182 	 * inodes's write_file list
183 	 */
184 	spin_lock(&fi->lock);
185 	if (list_empty(&ff->write_entry))
186 		list_add(&ff->write_entry, &fi->write_files);
187 	spin_unlock(&fi->lock);
188 }
189 
190 void fuse_finish_open(struct inode *inode, struct file *file)
191 {
192 	struct fuse_file *ff = file->private_data;
193 	struct fuse_conn *fc = get_fuse_conn(inode);
194 
195 	if (!(ff->open_flags & FOPEN_KEEP_CACHE))
196 		invalidate_inode_pages2(inode->i_mapping);
197 	if (ff->open_flags & FOPEN_STREAM)
198 		stream_open(inode, file);
199 	else if (ff->open_flags & FOPEN_NONSEEKABLE)
200 		nonseekable_open(inode, file);
201 	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 		struct fuse_inode *fi = get_fuse_inode(inode);
203 
204 		spin_lock(&fi->lock);
205 		fi->attr_version = atomic64_inc_return(&fc->attr_version);
206 		i_size_write(inode, 0);
207 		spin_unlock(&fi->lock);
208 		fuse_invalidate_attr(inode);
209 		if (fc->writeback_cache)
210 			file_update_time(file);
211 	}
212 	if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
213 		fuse_link_write_file(file);
214 }
215 
216 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
217 {
218 	struct fuse_conn *fc = get_fuse_conn(inode);
219 	int err;
220 	bool lock_inode = (file->f_flags & O_TRUNC) &&
221 			  fc->atomic_o_trunc &&
222 			  fc->writeback_cache;
223 
224 	err = generic_file_open(inode, file);
225 	if (err)
226 		return err;
227 
228 	if (lock_inode)
229 		inode_lock(inode);
230 
231 	err = fuse_do_open(fc, get_node_id(inode), file, isdir);
232 
233 	if (!err)
234 		fuse_finish_open(inode, file);
235 
236 	if (lock_inode)
237 		inode_unlock(inode);
238 
239 	return err;
240 }
241 
242 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
243 				 int flags, int opcode)
244 {
245 	struct fuse_conn *fc = ff->fc;
246 	struct fuse_release_args *ra = ff->release_args;
247 
248 	/* Inode is NULL on error path of fuse_create_open() */
249 	if (likely(fi)) {
250 		spin_lock(&fi->lock);
251 		list_del(&ff->write_entry);
252 		spin_unlock(&fi->lock);
253 	}
254 	spin_lock(&fc->lock);
255 	if (!RB_EMPTY_NODE(&ff->polled_node))
256 		rb_erase(&ff->polled_node, &fc->polled_files);
257 	spin_unlock(&fc->lock);
258 
259 	wake_up_interruptible_all(&ff->poll_wait);
260 
261 	ra->inarg.fh = ff->fh;
262 	ra->inarg.flags = flags;
263 	ra->args.in_numargs = 1;
264 	ra->args.in_args[0].size = sizeof(struct fuse_release_in);
265 	ra->args.in_args[0].value = &ra->inarg;
266 	ra->args.opcode = opcode;
267 	ra->args.nodeid = ff->nodeid;
268 	ra->args.force = true;
269 	ra->args.nocreds = true;
270 }
271 
272 void fuse_release_common(struct file *file, bool isdir)
273 {
274 	struct fuse_inode *fi = get_fuse_inode(file_inode(file));
275 	struct fuse_file *ff = file->private_data;
276 	struct fuse_release_args *ra = ff->release_args;
277 	int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
278 
279 	fuse_prepare_release(fi, ff, file->f_flags, opcode);
280 
281 	if (ff->flock) {
282 		ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
283 		ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
284 							  (fl_owner_t) file);
285 	}
286 	/* Hold inode until release is finished */
287 	ra->inode = igrab(file_inode(file));
288 
289 	/*
290 	 * Normally this will send the RELEASE request, however if
291 	 * some asynchronous READ or WRITE requests are outstanding,
292 	 * the sending will be delayed.
293 	 *
294 	 * Make the release synchronous if this is a fuseblk mount,
295 	 * synchronous RELEASE is allowed (and desirable) in this case
296 	 * because the server can be trusted not to screw up.
297 	 */
298 	fuse_file_put(ff, ff->fc->destroy, isdir);
299 }
300 
301 static int fuse_open(struct inode *inode, struct file *file)
302 {
303 	return fuse_open_common(inode, file, false);
304 }
305 
306 static int fuse_release(struct inode *inode, struct file *file)
307 {
308 	struct fuse_conn *fc = get_fuse_conn(inode);
309 
310 	/* see fuse_vma_close() for !writeback_cache case */
311 	if (fc->writeback_cache)
312 		write_inode_now(inode, 1);
313 
314 	fuse_release_common(file, false);
315 
316 	/* return value is ignored by VFS */
317 	return 0;
318 }
319 
320 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
321 {
322 	WARN_ON(refcount_read(&ff->count) > 1);
323 	fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
324 	/*
325 	 * iput(NULL) is a no-op and since the refcount is 1 and everything's
326 	 * synchronous, we are fine with not doing igrab() here"
327 	 */
328 	fuse_file_put(ff, true, false);
329 }
330 EXPORT_SYMBOL_GPL(fuse_sync_release);
331 
332 /*
333  * Scramble the ID space with XTEA, so that the value of the files_struct
334  * pointer is not exposed to userspace.
335  */
336 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
337 {
338 	u32 *k = fc->scramble_key;
339 	u64 v = (unsigned long) id;
340 	u32 v0 = v;
341 	u32 v1 = v >> 32;
342 	u32 sum = 0;
343 	int i;
344 
345 	for (i = 0; i < 32; i++) {
346 		v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
347 		sum += 0x9E3779B9;
348 		v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
349 	}
350 
351 	return (u64) v0 + ((u64) v1 << 32);
352 }
353 
354 struct fuse_writepage_args {
355 	struct fuse_io_args ia;
356 	struct list_head writepages_entry;
357 	struct list_head queue_entry;
358 	struct fuse_writepage_args *next;
359 	struct inode *inode;
360 };
361 
362 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
363 					    pgoff_t idx_from, pgoff_t idx_to)
364 {
365 	struct fuse_writepage_args *wpa;
366 
367 	list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
368 		pgoff_t curr_index;
369 
370 		WARN_ON(get_fuse_inode(wpa->inode) != fi);
371 		curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
372 		if (idx_from < curr_index + wpa->ia.ap.num_pages &&
373 		    curr_index <= idx_to) {
374 			return wpa;
375 		}
376 	}
377 	return NULL;
378 }
379 
380 /*
381  * Check if any page in a range is under writeback
382  *
383  * This is currently done by walking the list of writepage requests
384  * for the inode, which can be pretty inefficient.
385  */
386 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
387 				   pgoff_t idx_to)
388 {
389 	struct fuse_inode *fi = get_fuse_inode(inode);
390 	bool found;
391 
392 	spin_lock(&fi->lock);
393 	found = fuse_find_writeback(fi, idx_from, idx_to);
394 	spin_unlock(&fi->lock);
395 
396 	return found;
397 }
398 
399 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
400 {
401 	return fuse_range_is_writeback(inode, index, index);
402 }
403 
404 /*
405  * Wait for page writeback to be completed.
406  *
407  * Since fuse doesn't rely on the VM writeback tracking, this has to
408  * use some other means.
409  */
410 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
411 {
412 	struct fuse_inode *fi = get_fuse_inode(inode);
413 
414 	wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
415 }
416 
417 /*
418  * Wait for all pending writepages on the inode to finish.
419  *
420  * This is currently done by blocking further writes with FUSE_NOWRITE
421  * and waiting for all sent writes to complete.
422  *
423  * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
424  * could conflict with truncation.
425  */
426 static void fuse_sync_writes(struct inode *inode)
427 {
428 	fuse_set_nowrite(inode);
429 	fuse_release_nowrite(inode);
430 }
431 
432 static int fuse_flush(struct file *file, fl_owner_t id)
433 {
434 	struct inode *inode = file_inode(file);
435 	struct fuse_conn *fc = get_fuse_conn(inode);
436 	struct fuse_file *ff = file->private_data;
437 	struct fuse_flush_in inarg;
438 	FUSE_ARGS(args);
439 	int err;
440 
441 	if (is_bad_inode(inode))
442 		return -EIO;
443 
444 	if (fc->no_flush)
445 		return 0;
446 
447 	err = write_inode_now(inode, 1);
448 	if (err)
449 		return err;
450 
451 	inode_lock(inode);
452 	fuse_sync_writes(inode);
453 	inode_unlock(inode);
454 
455 	err = filemap_check_errors(file->f_mapping);
456 	if (err)
457 		return err;
458 
459 	memset(&inarg, 0, sizeof(inarg));
460 	inarg.fh = ff->fh;
461 	inarg.lock_owner = fuse_lock_owner_id(fc, id);
462 	args.opcode = FUSE_FLUSH;
463 	args.nodeid = get_node_id(inode);
464 	args.in_numargs = 1;
465 	args.in_args[0].size = sizeof(inarg);
466 	args.in_args[0].value = &inarg;
467 	args.force = true;
468 
469 	err = fuse_simple_request(fc, &args);
470 	if (err == -ENOSYS) {
471 		fc->no_flush = 1;
472 		err = 0;
473 	}
474 	return err;
475 }
476 
477 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
478 		      int datasync, int opcode)
479 {
480 	struct inode *inode = file->f_mapping->host;
481 	struct fuse_conn *fc = get_fuse_conn(inode);
482 	struct fuse_file *ff = file->private_data;
483 	FUSE_ARGS(args);
484 	struct fuse_fsync_in inarg;
485 
486 	memset(&inarg, 0, sizeof(inarg));
487 	inarg.fh = ff->fh;
488 	inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
489 	args.opcode = opcode;
490 	args.nodeid = get_node_id(inode);
491 	args.in_numargs = 1;
492 	args.in_args[0].size = sizeof(inarg);
493 	args.in_args[0].value = &inarg;
494 	return fuse_simple_request(fc, &args);
495 }
496 
497 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
498 		      int datasync)
499 {
500 	struct inode *inode = file->f_mapping->host;
501 	struct fuse_conn *fc = get_fuse_conn(inode);
502 	int err;
503 
504 	if (is_bad_inode(inode))
505 		return -EIO;
506 
507 	inode_lock(inode);
508 
509 	/*
510 	 * Start writeback against all dirty pages of the inode, then
511 	 * wait for all outstanding writes, before sending the FSYNC
512 	 * request.
513 	 */
514 	err = file_write_and_wait_range(file, start, end);
515 	if (err)
516 		goto out;
517 
518 	fuse_sync_writes(inode);
519 
520 	/*
521 	 * Due to implementation of fuse writeback
522 	 * file_write_and_wait_range() does not catch errors.
523 	 * We have to do this directly after fuse_sync_writes()
524 	 */
525 	err = file_check_and_advance_wb_err(file);
526 	if (err)
527 		goto out;
528 
529 	err = sync_inode_metadata(inode, 1);
530 	if (err)
531 		goto out;
532 
533 	if (fc->no_fsync)
534 		goto out;
535 
536 	err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
537 	if (err == -ENOSYS) {
538 		fc->no_fsync = 1;
539 		err = 0;
540 	}
541 out:
542 	inode_unlock(inode);
543 
544 	return err;
545 }
546 
547 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
548 			 size_t count, int opcode)
549 {
550 	struct fuse_file *ff = file->private_data;
551 	struct fuse_args *args = &ia->ap.args;
552 
553 	ia->read.in.fh = ff->fh;
554 	ia->read.in.offset = pos;
555 	ia->read.in.size = count;
556 	ia->read.in.flags = file->f_flags;
557 	args->opcode = opcode;
558 	args->nodeid = ff->nodeid;
559 	args->in_numargs = 1;
560 	args->in_args[0].size = sizeof(ia->read.in);
561 	args->in_args[0].value = &ia->read.in;
562 	args->out_argvar = true;
563 	args->out_numargs = 1;
564 	args->out_args[0].size = count;
565 }
566 
567 static void fuse_release_user_pages(struct fuse_args_pages *ap,
568 				    bool should_dirty)
569 {
570 	unsigned int i;
571 
572 	for (i = 0; i < ap->num_pages; i++) {
573 		if (should_dirty)
574 			set_page_dirty_lock(ap->pages[i]);
575 		put_page(ap->pages[i]);
576 	}
577 }
578 
579 static void fuse_io_release(struct kref *kref)
580 {
581 	kfree(container_of(kref, struct fuse_io_priv, refcnt));
582 }
583 
584 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
585 {
586 	if (io->err)
587 		return io->err;
588 
589 	if (io->bytes >= 0 && io->write)
590 		return -EIO;
591 
592 	return io->bytes < 0 ? io->size : io->bytes;
593 }
594 
595 /**
596  * In case of short read, the caller sets 'pos' to the position of
597  * actual end of fuse request in IO request. Otherwise, if bytes_requested
598  * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
599  *
600  * An example:
601  * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
602  * both submitted asynchronously. The first of them was ACKed by userspace as
603  * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
604  * second request was ACKed as short, e.g. only 1K was read, resulting in
605  * pos == 33K.
606  *
607  * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
608  * will be equal to the length of the longest contiguous fragment of
609  * transferred data starting from the beginning of IO request.
610  */
611 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
612 {
613 	int left;
614 
615 	spin_lock(&io->lock);
616 	if (err)
617 		io->err = io->err ? : err;
618 	else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
619 		io->bytes = pos;
620 
621 	left = --io->reqs;
622 	if (!left && io->blocking)
623 		complete(io->done);
624 	spin_unlock(&io->lock);
625 
626 	if (!left && !io->blocking) {
627 		ssize_t res = fuse_get_res_by_io(io);
628 
629 		if (res >= 0) {
630 			struct inode *inode = file_inode(io->iocb->ki_filp);
631 			struct fuse_conn *fc = get_fuse_conn(inode);
632 			struct fuse_inode *fi = get_fuse_inode(inode);
633 
634 			spin_lock(&fi->lock);
635 			fi->attr_version = atomic64_inc_return(&fc->attr_version);
636 			spin_unlock(&fi->lock);
637 		}
638 
639 		io->iocb->ki_complete(io->iocb, res, 0);
640 	}
641 
642 	kref_put(&io->refcnt, fuse_io_release);
643 }
644 
645 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
646 					  unsigned int npages)
647 {
648 	struct fuse_io_args *ia;
649 
650 	ia = kzalloc(sizeof(*ia), GFP_KERNEL);
651 	if (ia) {
652 		ia->io = io;
653 		ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
654 						&ia->ap.descs);
655 		if (!ia->ap.pages) {
656 			kfree(ia);
657 			ia = NULL;
658 		}
659 	}
660 	return ia;
661 }
662 
663 static void fuse_io_free(struct fuse_io_args *ia)
664 {
665 	kfree(ia->ap.pages);
666 	kfree(ia);
667 }
668 
669 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
670 				  int err)
671 {
672 	struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
673 	struct fuse_io_priv *io = ia->io;
674 	ssize_t pos = -1;
675 
676 	fuse_release_user_pages(&ia->ap, io->should_dirty);
677 
678 	if (err) {
679 		/* Nothing */
680 	} else if (io->write) {
681 		if (ia->write.out.size > ia->write.in.size) {
682 			err = -EIO;
683 		} else if (ia->write.in.size != ia->write.out.size) {
684 			pos = ia->write.in.offset - io->offset +
685 				ia->write.out.size;
686 		}
687 	} else {
688 		u32 outsize = args->out_args[0].size;
689 
690 		if (ia->read.in.size != outsize)
691 			pos = ia->read.in.offset - io->offset + outsize;
692 	}
693 
694 	fuse_aio_complete(io, err, pos);
695 	fuse_io_free(ia);
696 }
697 
698 static ssize_t fuse_async_req_send(struct fuse_conn *fc,
699 				   struct fuse_io_args *ia, size_t num_bytes)
700 {
701 	ssize_t err;
702 	struct fuse_io_priv *io = ia->io;
703 
704 	spin_lock(&io->lock);
705 	kref_get(&io->refcnt);
706 	io->size += num_bytes;
707 	io->reqs++;
708 	spin_unlock(&io->lock);
709 
710 	ia->ap.args.end = fuse_aio_complete_req;
711 	err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
712 
713 	return err ?: num_bytes;
714 }
715 
716 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
717 			      fl_owner_t owner)
718 {
719 	struct file *file = ia->io->iocb->ki_filp;
720 	struct fuse_file *ff = file->private_data;
721 	struct fuse_conn *fc = ff->fc;
722 
723 	fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
724 	if (owner != NULL) {
725 		ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
726 		ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
727 	}
728 
729 	if (ia->io->async)
730 		return fuse_async_req_send(fc, ia, count);
731 
732 	return fuse_simple_request(fc, &ia->ap.args);
733 }
734 
735 static void fuse_read_update_size(struct inode *inode, loff_t size,
736 				  u64 attr_ver)
737 {
738 	struct fuse_conn *fc = get_fuse_conn(inode);
739 	struct fuse_inode *fi = get_fuse_inode(inode);
740 
741 	spin_lock(&fi->lock);
742 	if (attr_ver == fi->attr_version && size < inode->i_size &&
743 	    !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
744 		fi->attr_version = atomic64_inc_return(&fc->attr_version);
745 		i_size_write(inode, size);
746 	}
747 	spin_unlock(&fi->lock);
748 }
749 
750 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
751 			    struct fuse_args_pages *ap)
752 {
753 	struct fuse_conn *fc = get_fuse_conn(inode);
754 
755 	if (fc->writeback_cache) {
756 		/*
757 		 * A hole in a file. Some data after the hole are in page cache,
758 		 * but have not reached the client fs yet. So, the hole is not
759 		 * present there.
760 		 */
761 		int i;
762 		int start_idx = num_read >> PAGE_SHIFT;
763 		size_t off = num_read & (PAGE_SIZE - 1);
764 
765 		for (i = start_idx; i < ap->num_pages; i++) {
766 			zero_user_segment(ap->pages[i], off, PAGE_SIZE);
767 			off = 0;
768 		}
769 	} else {
770 		loff_t pos = page_offset(ap->pages[0]) + num_read;
771 		fuse_read_update_size(inode, pos, attr_ver);
772 	}
773 }
774 
775 static int fuse_do_readpage(struct file *file, struct page *page)
776 {
777 	struct inode *inode = page->mapping->host;
778 	struct fuse_conn *fc = get_fuse_conn(inode);
779 	loff_t pos = page_offset(page);
780 	struct fuse_page_desc desc = { .length = PAGE_SIZE };
781 	struct fuse_io_args ia = {
782 		.ap.args.page_zeroing = true,
783 		.ap.args.out_pages = true,
784 		.ap.num_pages = 1,
785 		.ap.pages = &page,
786 		.ap.descs = &desc,
787 	};
788 	ssize_t res;
789 	u64 attr_ver;
790 
791 	/*
792 	 * Page writeback can extend beyond the lifetime of the
793 	 * page-cache page, so make sure we read a properly synced
794 	 * page.
795 	 */
796 	fuse_wait_on_page_writeback(inode, page->index);
797 
798 	attr_ver = fuse_get_attr_version(fc);
799 
800 	fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
801 	res = fuse_simple_request(fc, &ia.ap.args);
802 	if (res < 0)
803 		return res;
804 	/*
805 	 * Short read means EOF.  If file size is larger, truncate it
806 	 */
807 	if (res < desc.length)
808 		fuse_short_read(inode, attr_ver, res, &ia.ap);
809 
810 	SetPageUptodate(page);
811 
812 	return 0;
813 }
814 
815 static int fuse_readpage(struct file *file, struct page *page)
816 {
817 	struct inode *inode = page->mapping->host;
818 	int err;
819 
820 	err = -EIO;
821 	if (is_bad_inode(inode))
822 		goto out;
823 
824 	err = fuse_do_readpage(file, page);
825 	fuse_invalidate_atime(inode);
826  out:
827 	unlock_page(page);
828 	return err;
829 }
830 
831 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
832 			       int err)
833 {
834 	int i;
835 	struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
836 	struct fuse_args_pages *ap = &ia->ap;
837 	size_t count = ia->read.in.size;
838 	size_t num_read = args->out_args[0].size;
839 	struct address_space *mapping = NULL;
840 
841 	for (i = 0; mapping == NULL && i < ap->num_pages; i++)
842 		mapping = ap->pages[i]->mapping;
843 
844 	if (mapping) {
845 		struct inode *inode = mapping->host;
846 
847 		/*
848 		 * Short read means EOF. If file size is larger, truncate it
849 		 */
850 		if (!err && num_read < count)
851 			fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
852 
853 		fuse_invalidate_atime(inode);
854 	}
855 
856 	for (i = 0; i < ap->num_pages; i++) {
857 		struct page *page = ap->pages[i];
858 
859 		if (!err)
860 			SetPageUptodate(page);
861 		else
862 			SetPageError(page);
863 		unlock_page(page);
864 		put_page(page);
865 	}
866 	if (ia->ff)
867 		fuse_file_put(ia->ff, false, false);
868 
869 	fuse_io_free(ia);
870 }
871 
872 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
873 {
874 	struct fuse_file *ff = file->private_data;
875 	struct fuse_conn *fc = ff->fc;
876 	struct fuse_args_pages *ap = &ia->ap;
877 	loff_t pos = page_offset(ap->pages[0]);
878 	size_t count = ap->num_pages << PAGE_SHIFT;
879 	int err;
880 
881 	ap->args.out_pages = true;
882 	ap->args.page_zeroing = true;
883 	ap->args.page_replace = true;
884 	fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
885 	ia->read.attr_ver = fuse_get_attr_version(fc);
886 	if (fc->async_read) {
887 		ia->ff = fuse_file_get(ff);
888 		ap->args.end = fuse_readpages_end;
889 		err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
890 		if (!err)
891 			return;
892 	} else {
893 		err = fuse_simple_request(fc, &ap->args);
894 	}
895 	fuse_readpages_end(fc, &ap->args, err);
896 }
897 
898 struct fuse_fill_data {
899 	struct fuse_io_args *ia;
900 	struct file *file;
901 	struct inode *inode;
902 	unsigned int nr_pages;
903 	unsigned int max_pages;
904 };
905 
906 static int fuse_readpages_fill(void *_data, struct page *page)
907 {
908 	struct fuse_fill_data *data = _data;
909 	struct fuse_io_args *ia = data->ia;
910 	struct fuse_args_pages *ap = &ia->ap;
911 	struct inode *inode = data->inode;
912 	struct fuse_conn *fc = get_fuse_conn(inode);
913 
914 	fuse_wait_on_page_writeback(inode, page->index);
915 
916 	if (ap->num_pages &&
917 	    (ap->num_pages == fc->max_pages ||
918 	     (ap->num_pages + 1) * PAGE_SIZE > fc->max_read ||
919 	     ap->pages[ap->num_pages - 1]->index + 1 != page->index)) {
920 		data->max_pages = min_t(unsigned int, data->nr_pages,
921 					fc->max_pages);
922 		fuse_send_readpages(ia, data->file);
923 		data->ia = ia = fuse_io_alloc(NULL, data->max_pages);
924 		if (!ia) {
925 			unlock_page(page);
926 			return -ENOMEM;
927 		}
928 		ap = &ia->ap;
929 	}
930 
931 	if (WARN_ON(ap->num_pages >= data->max_pages)) {
932 		unlock_page(page);
933 		fuse_io_free(ia);
934 		return -EIO;
935 	}
936 
937 	get_page(page);
938 	ap->pages[ap->num_pages] = page;
939 	ap->descs[ap->num_pages].length = PAGE_SIZE;
940 	ap->num_pages++;
941 	data->nr_pages--;
942 	return 0;
943 }
944 
945 static int fuse_readpages(struct file *file, struct address_space *mapping,
946 			  struct list_head *pages, unsigned nr_pages)
947 {
948 	struct inode *inode = mapping->host;
949 	struct fuse_conn *fc = get_fuse_conn(inode);
950 	struct fuse_fill_data data;
951 	int err;
952 
953 	err = -EIO;
954 	if (is_bad_inode(inode))
955 		goto out;
956 
957 	data.file = file;
958 	data.inode = inode;
959 	data.nr_pages = nr_pages;
960 	data.max_pages = min_t(unsigned int, nr_pages, fc->max_pages);
961 ;
962 	data.ia = fuse_io_alloc(NULL, data.max_pages);
963 	err = -ENOMEM;
964 	if (!data.ia)
965 		goto out;
966 
967 	err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
968 	if (!err) {
969 		if (data.ia->ap.num_pages)
970 			fuse_send_readpages(data.ia, file);
971 		else
972 			fuse_io_free(data.ia);
973 	}
974 out:
975 	return err;
976 }
977 
978 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
979 {
980 	struct inode *inode = iocb->ki_filp->f_mapping->host;
981 	struct fuse_conn *fc = get_fuse_conn(inode);
982 
983 	/*
984 	 * In auto invalidate mode, always update attributes on read.
985 	 * Otherwise, only update if we attempt to read past EOF (to ensure
986 	 * i_size is up to date).
987 	 */
988 	if (fc->auto_inval_data ||
989 	    (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
990 		int err;
991 		err = fuse_update_attributes(inode, iocb->ki_filp);
992 		if (err)
993 			return err;
994 	}
995 
996 	return generic_file_read_iter(iocb, to);
997 }
998 
999 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1000 				 loff_t pos, size_t count)
1001 {
1002 	struct fuse_args *args = &ia->ap.args;
1003 
1004 	ia->write.in.fh = ff->fh;
1005 	ia->write.in.offset = pos;
1006 	ia->write.in.size = count;
1007 	args->opcode = FUSE_WRITE;
1008 	args->nodeid = ff->nodeid;
1009 	args->in_numargs = 2;
1010 	if (ff->fc->minor < 9)
1011 		args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1012 	else
1013 		args->in_args[0].size = sizeof(ia->write.in);
1014 	args->in_args[0].value = &ia->write.in;
1015 	args->in_args[1].size = count;
1016 	args->out_numargs = 1;
1017 	args->out_args[0].size = sizeof(ia->write.out);
1018 	args->out_args[0].value = &ia->write.out;
1019 }
1020 
1021 static unsigned int fuse_write_flags(struct kiocb *iocb)
1022 {
1023 	unsigned int flags = iocb->ki_filp->f_flags;
1024 
1025 	if (iocb->ki_flags & IOCB_DSYNC)
1026 		flags |= O_DSYNC;
1027 	if (iocb->ki_flags & IOCB_SYNC)
1028 		flags |= O_SYNC;
1029 
1030 	return flags;
1031 }
1032 
1033 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1034 			       size_t count, fl_owner_t owner)
1035 {
1036 	struct kiocb *iocb = ia->io->iocb;
1037 	struct file *file = iocb->ki_filp;
1038 	struct fuse_file *ff = file->private_data;
1039 	struct fuse_conn *fc = ff->fc;
1040 	struct fuse_write_in *inarg = &ia->write.in;
1041 	ssize_t err;
1042 
1043 	fuse_write_args_fill(ia, ff, pos, count);
1044 	inarg->flags = fuse_write_flags(iocb);
1045 	if (owner != NULL) {
1046 		inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1047 		inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1048 	}
1049 
1050 	if (ia->io->async)
1051 		return fuse_async_req_send(fc, ia, count);
1052 
1053 	err = fuse_simple_request(fc, &ia->ap.args);
1054 	if (!err && ia->write.out.size > count)
1055 		err = -EIO;
1056 
1057 	return err ?: ia->write.out.size;
1058 }
1059 
1060 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1061 {
1062 	struct fuse_conn *fc = get_fuse_conn(inode);
1063 	struct fuse_inode *fi = get_fuse_inode(inode);
1064 	bool ret = false;
1065 
1066 	spin_lock(&fi->lock);
1067 	fi->attr_version = atomic64_inc_return(&fc->attr_version);
1068 	if (pos > inode->i_size) {
1069 		i_size_write(inode, pos);
1070 		ret = true;
1071 	}
1072 	spin_unlock(&fi->lock);
1073 
1074 	return ret;
1075 }
1076 
1077 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1078 				     struct kiocb *iocb, struct inode *inode,
1079 				     loff_t pos, size_t count)
1080 {
1081 	struct fuse_args_pages *ap = &ia->ap;
1082 	struct file *file = iocb->ki_filp;
1083 	struct fuse_file *ff = file->private_data;
1084 	struct fuse_conn *fc = ff->fc;
1085 	unsigned int offset, i;
1086 	int err;
1087 
1088 	for (i = 0; i < ap->num_pages; i++)
1089 		fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1090 
1091 	fuse_write_args_fill(ia, ff, pos, count);
1092 	ia->write.in.flags = fuse_write_flags(iocb);
1093 
1094 	err = fuse_simple_request(fc, &ap->args);
1095 
1096 	offset = ap->descs[0].offset;
1097 	count = ia->write.out.size;
1098 	for (i = 0; i < ap->num_pages; i++) {
1099 		struct page *page = ap->pages[i];
1100 
1101 		if (!err && !offset && count >= PAGE_SIZE)
1102 			SetPageUptodate(page);
1103 
1104 		if (count > PAGE_SIZE - offset)
1105 			count -= PAGE_SIZE - offset;
1106 		else
1107 			count = 0;
1108 		offset = 0;
1109 
1110 		unlock_page(page);
1111 		put_page(page);
1112 	}
1113 
1114 	return err;
1115 }
1116 
1117 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1118 				     struct address_space *mapping,
1119 				     struct iov_iter *ii, loff_t pos,
1120 				     unsigned int max_pages)
1121 {
1122 	struct fuse_conn *fc = get_fuse_conn(mapping->host);
1123 	unsigned offset = pos & (PAGE_SIZE - 1);
1124 	size_t count = 0;
1125 	int err;
1126 
1127 	ap->args.in_pages = true;
1128 	ap->descs[0].offset = offset;
1129 
1130 	do {
1131 		size_t tmp;
1132 		struct page *page;
1133 		pgoff_t index = pos >> PAGE_SHIFT;
1134 		size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1135 				     iov_iter_count(ii));
1136 
1137 		bytes = min_t(size_t, bytes, fc->max_write - count);
1138 
1139  again:
1140 		err = -EFAULT;
1141 		if (iov_iter_fault_in_readable(ii, bytes))
1142 			break;
1143 
1144 		err = -ENOMEM;
1145 		page = grab_cache_page_write_begin(mapping, index, 0);
1146 		if (!page)
1147 			break;
1148 
1149 		if (mapping_writably_mapped(mapping))
1150 			flush_dcache_page(page);
1151 
1152 		tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1153 		flush_dcache_page(page);
1154 
1155 		iov_iter_advance(ii, tmp);
1156 		if (!tmp) {
1157 			unlock_page(page);
1158 			put_page(page);
1159 			bytes = min(bytes, iov_iter_single_seg_count(ii));
1160 			goto again;
1161 		}
1162 
1163 		err = 0;
1164 		ap->pages[ap->num_pages] = page;
1165 		ap->descs[ap->num_pages].length = tmp;
1166 		ap->num_pages++;
1167 
1168 		count += tmp;
1169 		pos += tmp;
1170 		offset += tmp;
1171 		if (offset == PAGE_SIZE)
1172 			offset = 0;
1173 
1174 		if (!fc->big_writes)
1175 			break;
1176 	} while (iov_iter_count(ii) && count < fc->max_write &&
1177 		 ap->num_pages < max_pages && offset == 0);
1178 
1179 	return count > 0 ? count : err;
1180 }
1181 
1182 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1183 				     unsigned int max_pages)
1184 {
1185 	return min_t(unsigned int,
1186 		     ((pos + len - 1) >> PAGE_SHIFT) -
1187 		     (pos >> PAGE_SHIFT) + 1,
1188 		     max_pages);
1189 }
1190 
1191 static ssize_t fuse_perform_write(struct kiocb *iocb,
1192 				  struct address_space *mapping,
1193 				  struct iov_iter *ii, loff_t pos)
1194 {
1195 	struct inode *inode = mapping->host;
1196 	struct fuse_conn *fc = get_fuse_conn(inode);
1197 	struct fuse_inode *fi = get_fuse_inode(inode);
1198 	int err = 0;
1199 	ssize_t res = 0;
1200 
1201 	if (inode->i_size < pos + iov_iter_count(ii))
1202 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1203 
1204 	do {
1205 		ssize_t count;
1206 		struct fuse_io_args ia = {};
1207 		struct fuse_args_pages *ap = &ia.ap;
1208 		unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1209 						      fc->max_pages);
1210 
1211 		ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1212 		if (!ap->pages) {
1213 			err = -ENOMEM;
1214 			break;
1215 		}
1216 
1217 		count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1218 		if (count <= 0) {
1219 			err = count;
1220 		} else {
1221 			err = fuse_send_write_pages(&ia, iocb, inode,
1222 						    pos, count);
1223 			if (!err) {
1224 				size_t num_written = ia.write.out.size;
1225 
1226 				res += num_written;
1227 				pos += num_written;
1228 
1229 				/* break out of the loop on short write */
1230 				if (num_written != count)
1231 					err = -EIO;
1232 			}
1233 		}
1234 		kfree(ap->pages);
1235 	} while (!err && iov_iter_count(ii));
1236 
1237 	if (res > 0)
1238 		fuse_write_update_size(inode, pos);
1239 
1240 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1241 	fuse_invalidate_attr(inode);
1242 
1243 	return res > 0 ? res : err;
1244 }
1245 
1246 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1247 {
1248 	struct file *file = iocb->ki_filp;
1249 	struct address_space *mapping = file->f_mapping;
1250 	ssize_t written = 0;
1251 	ssize_t written_buffered = 0;
1252 	struct inode *inode = mapping->host;
1253 	ssize_t err;
1254 	loff_t endbyte = 0;
1255 
1256 	if (get_fuse_conn(inode)->writeback_cache) {
1257 		/* Update size (EOF optimization) and mode (SUID clearing) */
1258 		err = fuse_update_attributes(mapping->host, file);
1259 		if (err)
1260 			return err;
1261 
1262 		return generic_file_write_iter(iocb, from);
1263 	}
1264 
1265 	inode_lock(inode);
1266 
1267 	/* We can write back this queue in page reclaim */
1268 	current->backing_dev_info = inode_to_bdi(inode);
1269 
1270 	err = generic_write_checks(iocb, from);
1271 	if (err <= 0)
1272 		goto out;
1273 
1274 	err = file_remove_privs(file);
1275 	if (err)
1276 		goto out;
1277 
1278 	err = file_update_time(file);
1279 	if (err)
1280 		goto out;
1281 
1282 	if (iocb->ki_flags & IOCB_DIRECT) {
1283 		loff_t pos = iocb->ki_pos;
1284 		written = generic_file_direct_write(iocb, from);
1285 		if (written < 0 || !iov_iter_count(from))
1286 			goto out;
1287 
1288 		pos += written;
1289 
1290 		written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1291 		if (written_buffered < 0) {
1292 			err = written_buffered;
1293 			goto out;
1294 		}
1295 		endbyte = pos + written_buffered - 1;
1296 
1297 		err = filemap_write_and_wait_range(file->f_mapping, pos,
1298 						   endbyte);
1299 		if (err)
1300 			goto out;
1301 
1302 		invalidate_mapping_pages(file->f_mapping,
1303 					 pos >> PAGE_SHIFT,
1304 					 endbyte >> PAGE_SHIFT);
1305 
1306 		written += written_buffered;
1307 		iocb->ki_pos = pos + written_buffered;
1308 	} else {
1309 		written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1310 		if (written >= 0)
1311 			iocb->ki_pos += written;
1312 	}
1313 out:
1314 	current->backing_dev_info = NULL;
1315 	inode_unlock(inode);
1316 	if (written > 0)
1317 		written = generic_write_sync(iocb, written);
1318 
1319 	return written ? written : err;
1320 }
1321 
1322 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1323 					       unsigned int index,
1324 					       unsigned int nr_pages)
1325 {
1326 	int i;
1327 
1328 	for (i = index; i < index + nr_pages; i++)
1329 		descs[i].length = PAGE_SIZE - descs[i].offset;
1330 }
1331 
1332 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1333 {
1334 	return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1335 }
1336 
1337 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1338 					size_t max_size)
1339 {
1340 	return min(iov_iter_single_seg_count(ii), max_size);
1341 }
1342 
1343 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1344 			       size_t *nbytesp, int write,
1345 			       unsigned int max_pages)
1346 {
1347 	size_t nbytes = 0;  /* # bytes already packed in req */
1348 	ssize_t ret = 0;
1349 
1350 	/* Special case for kernel I/O: can copy directly into the buffer */
1351 	if (iov_iter_is_kvec(ii)) {
1352 		unsigned long user_addr = fuse_get_user_addr(ii);
1353 		size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1354 
1355 		if (write)
1356 			ap->args.in_args[1].value = (void *) user_addr;
1357 		else
1358 			ap->args.out_args[0].value = (void *) user_addr;
1359 
1360 		iov_iter_advance(ii, frag_size);
1361 		*nbytesp = frag_size;
1362 		return 0;
1363 	}
1364 
1365 	while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1366 		unsigned npages;
1367 		size_t start;
1368 		ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1369 					*nbytesp - nbytes,
1370 					max_pages - ap->num_pages,
1371 					&start);
1372 		if (ret < 0)
1373 			break;
1374 
1375 		iov_iter_advance(ii, ret);
1376 		nbytes += ret;
1377 
1378 		ret += start;
1379 		npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1380 
1381 		ap->descs[ap->num_pages].offset = start;
1382 		fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1383 
1384 		ap->num_pages += npages;
1385 		ap->descs[ap->num_pages - 1].length -=
1386 			(PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1387 	}
1388 
1389 	if (write)
1390 		ap->args.in_pages = 1;
1391 	else
1392 		ap->args.out_pages = 1;
1393 
1394 	*nbytesp = nbytes;
1395 
1396 	return ret < 0 ? ret : 0;
1397 }
1398 
1399 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1400 		       loff_t *ppos, int flags)
1401 {
1402 	int write = flags & FUSE_DIO_WRITE;
1403 	int cuse = flags & FUSE_DIO_CUSE;
1404 	struct file *file = io->iocb->ki_filp;
1405 	struct inode *inode = file->f_mapping->host;
1406 	struct fuse_file *ff = file->private_data;
1407 	struct fuse_conn *fc = ff->fc;
1408 	size_t nmax = write ? fc->max_write : fc->max_read;
1409 	loff_t pos = *ppos;
1410 	size_t count = iov_iter_count(iter);
1411 	pgoff_t idx_from = pos >> PAGE_SHIFT;
1412 	pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1413 	ssize_t res = 0;
1414 	int err = 0;
1415 	struct fuse_io_args *ia;
1416 	unsigned int max_pages;
1417 
1418 	max_pages = iov_iter_npages(iter, fc->max_pages);
1419 	ia = fuse_io_alloc(io, max_pages);
1420 	if (!ia)
1421 		return -ENOMEM;
1422 
1423 	ia->io = io;
1424 	if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1425 		if (!write)
1426 			inode_lock(inode);
1427 		fuse_sync_writes(inode);
1428 		if (!write)
1429 			inode_unlock(inode);
1430 	}
1431 
1432 	io->should_dirty = !write && iter_is_iovec(iter);
1433 	while (count) {
1434 		ssize_t nres;
1435 		fl_owner_t owner = current->files;
1436 		size_t nbytes = min(count, nmax);
1437 
1438 		err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1439 					  max_pages);
1440 		if (err && !nbytes)
1441 			break;
1442 
1443 		if (write) {
1444 			if (!capable(CAP_FSETID))
1445 				ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1446 
1447 			nres = fuse_send_write(ia, pos, nbytes, owner);
1448 		} else {
1449 			nres = fuse_send_read(ia, pos, nbytes, owner);
1450 		}
1451 
1452 		if (!io->async || nres < 0) {
1453 			fuse_release_user_pages(&ia->ap, io->should_dirty);
1454 			fuse_io_free(ia);
1455 		}
1456 		ia = NULL;
1457 		if (nres < 0) {
1458 			err = nres;
1459 			break;
1460 		}
1461 		WARN_ON(nres > nbytes);
1462 
1463 		count -= nres;
1464 		res += nres;
1465 		pos += nres;
1466 		if (nres != nbytes)
1467 			break;
1468 		if (count) {
1469 			max_pages = iov_iter_npages(iter, fc->max_pages);
1470 			ia = fuse_io_alloc(io, max_pages);
1471 			if (!ia)
1472 				break;
1473 		}
1474 	}
1475 	if (ia)
1476 		fuse_io_free(ia);
1477 	if (res > 0)
1478 		*ppos = pos;
1479 
1480 	return res > 0 ? res : err;
1481 }
1482 EXPORT_SYMBOL_GPL(fuse_direct_io);
1483 
1484 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1485 				  struct iov_iter *iter,
1486 				  loff_t *ppos)
1487 {
1488 	ssize_t res;
1489 	struct inode *inode = file_inode(io->iocb->ki_filp);
1490 
1491 	res = fuse_direct_io(io, iter, ppos, 0);
1492 
1493 	fuse_invalidate_atime(inode);
1494 
1495 	return res;
1496 }
1497 
1498 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1499 
1500 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1501 {
1502 	ssize_t res;
1503 
1504 	if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1505 		res = fuse_direct_IO(iocb, to);
1506 	} else {
1507 		struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1508 
1509 		res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1510 	}
1511 
1512 	return res;
1513 }
1514 
1515 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1516 {
1517 	struct inode *inode = file_inode(iocb->ki_filp);
1518 	struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1519 	ssize_t res;
1520 
1521 	/* Don't allow parallel writes to the same file */
1522 	inode_lock(inode);
1523 	res = generic_write_checks(iocb, from);
1524 	if (res > 0) {
1525 		if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1526 			res = fuse_direct_IO(iocb, from);
1527 		} else {
1528 			res = fuse_direct_io(&io, from, &iocb->ki_pos,
1529 					     FUSE_DIO_WRITE);
1530 		}
1531 	}
1532 	fuse_invalidate_attr(inode);
1533 	if (res > 0)
1534 		fuse_write_update_size(inode, iocb->ki_pos);
1535 	inode_unlock(inode);
1536 
1537 	return res;
1538 }
1539 
1540 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1541 {
1542 	struct file *file = iocb->ki_filp;
1543 	struct fuse_file *ff = file->private_data;
1544 
1545 	if (is_bad_inode(file_inode(file)))
1546 		return -EIO;
1547 
1548 	if (!(ff->open_flags & FOPEN_DIRECT_IO))
1549 		return fuse_cache_read_iter(iocb, to);
1550 	else
1551 		return fuse_direct_read_iter(iocb, to);
1552 }
1553 
1554 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1555 {
1556 	struct file *file = iocb->ki_filp;
1557 	struct fuse_file *ff = file->private_data;
1558 
1559 	if (is_bad_inode(file_inode(file)))
1560 		return -EIO;
1561 
1562 	if (!(ff->open_flags & FOPEN_DIRECT_IO))
1563 		return fuse_cache_write_iter(iocb, from);
1564 	else
1565 		return fuse_direct_write_iter(iocb, from);
1566 }
1567 
1568 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1569 {
1570 	struct fuse_args_pages *ap = &wpa->ia.ap;
1571 	int i;
1572 
1573 	for (i = 0; i < ap->num_pages; i++)
1574 		__free_page(ap->pages[i]);
1575 
1576 	if (wpa->ia.ff)
1577 		fuse_file_put(wpa->ia.ff, false, false);
1578 
1579 	kfree(ap->pages);
1580 	kfree(wpa);
1581 }
1582 
1583 static void fuse_writepage_finish(struct fuse_conn *fc,
1584 				  struct fuse_writepage_args *wpa)
1585 {
1586 	struct fuse_args_pages *ap = &wpa->ia.ap;
1587 	struct inode *inode = wpa->inode;
1588 	struct fuse_inode *fi = get_fuse_inode(inode);
1589 	struct backing_dev_info *bdi = inode_to_bdi(inode);
1590 	int i;
1591 
1592 	list_del(&wpa->writepages_entry);
1593 	for (i = 0; i < ap->num_pages; i++) {
1594 		dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1595 		dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1596 		wb_writeout_inc(&bdi->wb);
1597 	}
1598 	wake_up(&fi->page_waitq);
1599 }
1600 
1601 /* Called under fi->lock, may release and reacquire it */
1602 static void fuse_send_writepage(struct fuse_conn *fc,
1603 				struct fuse_writepage_args *wpa, loff_t size)
1604 __releases(fi->lock)
1605 __acquires(fi->lock)
1606 {
1607 	struct fuse_writepage_args *aux, *next;
1608 	struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1609 	struct fuse_write_in *inarg = &wpa->ia.write.in;
1610 	struct fuse_args *args = &wpa->ia.ap.args;
1611 	__u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1612 	int err;
1613 
1614 	fi->writectr++;
1615 	if (inarg->offset + data_size <= size) {
1616 		inarg->size = data_size;
1617 	} else if (inarg->offset < size) {
1618 		inarg->size = size - inarg->offset;
1619 	} else {
1620 		/* Got truncated off completely */
1621 		goto out_free;
1622 	}
1623 
1624 	args->in_args[1].size = inarg->size;
1625 	args->force = true;
1626 	args->nocreds = true;
1627 
1628 	err = fuse_simple_background(fc, args, GFP_ATOMIC);
1629 	if (err == -ENOMEM) {
1630 		spin_unlock(&fi->lock);
1631 		err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1632 		spin_lock(&fi->lock);
1633 	}
1634 
1635 	/* Fails on broken connection only */
1636 	if (unlikely(err))
1637 		goto out_free;
1638 
1639 	return;
1640 
1641  out_free:
1642 	fi->writectr--;
1643 	fuse_writepage_finish(fc, wpa);
1644 	spin_unlock(&fi->lock);
1645 
1646 	/* After fuse_writepage_finish() aux request list is private */
1647 	for (aux = wpa->next; aux; aux = next) {
1648 		next = aux->next;
1649 		aux->next = NULL;
1650 		fuse_writepage_free(aux);
1651 	}
1652 
1653 	fuse_writepage_free(wpa);
1654 	spin_lock(&fi->lock);
1655 }
1656 
1657 /*
1658  * If fi->writectr is positive (no truncate or fsync going on) send
1659  * all queued writepage requests.
1660  *
1661  * Called with fi->lock
1662  */
1663 void fuse_flush_writepages(struct inode *inode)
1664 __releases(fi->lock)
1665 __acquires(fi->lock)
1666 {
1667 	struct fuse_conn *fc = get_fuse_conn(inode);
1668 	struct fuse_inode *fi = get_fuse_inode(inode);
1669 	loff_t crop = i_size_read(inode);
1670 	struct fuse_writepage_args *wpa;
1671 
1672 	while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1673 		wpa = list_entry(fi->queued_writes.next,
1674 				 struct fuse_writepage_args, queue_entry);
1675 		list_del_init(&wpa->queue_entry);
1676 		fuse_send_writepage(fc, wpa, crop);
1677 	}
1678 }
1679 
1680 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1681 			       int error)
1682 {
1683 	struct fuse_writepage_args *wpa =
1684 		container_of(args, typeof(*wpa), ia.ap.args);
1685 	struct inode *inode = wpa->inode;
1686 	struct fuse_inode *fi = get_fuse_inode(inode);
1687 
1688 	mapping_set_error(inode->i_mapping, error);
1689 	spin_lock(&fi->lock);
1690 	while (wpa->next) {
1691 		struct fuse_conn *fc = get_fuse_conn(inode);
1692 		struct fuse_write_in *inarg = &wpa->ia.write.in;
1693 		struct fuse_writepage_args *next = wpa->next;
1694 
1695 		wpa->next = next->next;
1696 		next->next = NULL;
1697 		next->ia.ff = fuse_file_get(wpa->ia.ff);
1698 		list_add(&next->writepages_entry, &fi->writepages);
1699 
1700 		/*
1701 		 * Skip fuse_flush_writepages() to make it easy to crop requests
1702 		 * based on primary request size.
1703 		 *
1704 		 * 1st case (trivial): there are no concurrent activities using
1705 		 * fuse_set/release_nowrite.  Then we're on safe side because
1706 		 * fuse_flush_writepages() would call fuse_send_writepage()
1707 		 * anyway.
1708 		 *
1709 		 * 2nd case: someone called fuse_set_nowrite and it is waiting
1710 		 * now for completion of all in-flight requests.  This happens
1711 		 * rarely and no more than once per page, so this should be
1712 		 * okay.
1713 		 *
1714 		 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1715 		 * of fuse_set_nowrite..fuse_release_nowrite section.  The fact
1716 		 * that fuse_set_nowrite returned implies that all in-flight
1717 		 * requests were completed along with all of their secondary
1718 		 * requests.  Further primary requests are blocked by negative
1719 		 * writectr.  Hence there cannot be any in-flight requests and
1720 		 * no invocations of fuse_writepage_end() while we're in
1721 		 * fuse_set_nowrite..fuse_release_nowrite section.
1722 		 */
1723 		fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1724 	}
1725 	fi->writectr--;
1726 	fuse_writepage_finish(fc, wpa);
1727 	spin_unlock(&fi->lock);
1728 	fuse_writepage_free(wpa);
1729 }
1730 
1731 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1732 					       struct fuse_inode *fi)
1733 {
1734 	struct fuse_file *ff = NULL;
1735 
1736 	spin_lock(&fi->lock);
1737 	if (!list_empty(&fi->write_files)) {
1738 		ff = list_entry(fi->write_files.next, struct fuse_file,
1739 				write_entry);
1740 		fuse_file_get(ff);
1741 	}
1742 	spin_unlock(&fi->lock);
1743 
1744 	return ff;
1745 }
1746 
1747 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1748 					     struct fuse_inode *fi)
1749 {
1750 	struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1751 	WARN_ON(!ff);
1752 	return ff;
1753 }
1754 
1755 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1756 {
1757 	struct fuse_conn *fc = get_fuse_conn(inode);
1758 	struct fuse_inode *fi = get_fuse_inode(inode);
1759 	struct fuse_file *ff;
1760 	int err;
1761 
1762 	ff = __fuse_write_file_get(fc, fi);
1763 	err = fuse_flush_times(inode, ff);
1764 	if (ff)
1765 		fuse_file_put(ff, false, false);
1766 
1767 	return err;
1768 }
1769 
1770 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1771 {
1772 	struct fuse_writepage_args *wpa;
1773 	struct fuse_args_pages *ap;
1774 
1775 	wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1776 	if (wpa) {
1777 		ap = &wpa->ia.ap;
1778 		ap->num_pages = 0;
1779 		ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1780 		if (!ap->pages) {
1781 			kfree(wpa);
1782 			wpa = NULL;
1783 		}
1784 	}
1785 	return wpa;
1786 
1787 }
1788 
1789 static int fuse_writepage_locked(struct page *page)
1790 {
1791 	struct address_space *mapping = page->mapping;
1792 	struct inode *inode = mapping->host;
1793 	struct fuse_conn *fc = get_fuse_conn(inode);
1794 	struct fuse_inode *fi = get_fuse_inode(inode);
1795 	struct fuse_writepage_args *wpa;
1796 	struct fuse_args_pages *ap;
1797 	struct page *tmp_page;
1798 	int error = -ENOMEM;
1799 
1800 	set_page_writeback(page);
1801 
1802 	wpa = fuse_writepage_args_alloc();
1803 	if (!wpa)
1804 		goto err;
1805 	ap = &wpa->ia.ap;
1806 
1807 	tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1808 	if (!tmp_page)
1809 		goto err_free;
1810 
1811 	error = -EIO;
1812 	wpa->ia.ff = fuse_write_file_get(fc, fi);
1813 	if (!wpa->ia.ff)
1814 		goto err_nofile;
1815 
1816 	fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1817 
1818 	copy_highpage(tmp_page, page);
1819 	wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1820 	wpa->next = NULL;
1821 	ap->args.in_pages = true;
1822 	ap->num_pages = 1;
1823 	ap->pages[0] = tmp_page;
1824 	ap->descs[0].offset = 0;
1825 	ap->descs[0].length = PAGE_SIZE;
1826 	ap->args.end = fuse_writepage_end;
1827 	wpa->inode = inode;
1828 
1829 	inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1830 	inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1831 
1832 	spin_lock(&fi->lock);
1833 	list_add(&wpa->writepages_entry, &fi->writepages);
1834 	list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1835 	fuse_flush_writepages(inode);
1836 	spin_unlock(&fi->lock);
1837 
1838 	end_page_writeback(page);
1839 
1840 	return 0;
1841 
1842 err_nofile:
1843 	__free_page(tmp_page);
1844 err_free:
1845 	kfree(wpa);
1846 err:
1847 	mapping_set_error(page->mapping, error);
1848 	end_page_writeback(page);
1849 	return error;
1850 }
1851 
1852 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1853 {
1854 	int err;
1855 
1856 	if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1857 		/*
1858 		 * ->writepages() should be called for sync() and friends.  We
1859 		 * should only get here on direct reclaim and then we are
1860 		 * allowed to skip a page which is already in flight
1861 		 */
1862 		WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1863 
1864 		redirty_page_for_writepage(wbc, page);
1865 		unlock_page(page);
1866 		return 0;
1867 	}
1868 
1869 	err = fuse_writepage_locked(page);
1870 	unlock_page(page);
1871 
1872 	return err;
1873 }
1874 
1875 struct fuse_fill_wb_data {
1876 	struct fuse_writepage_args *wpa;
1877 	struct fuse_file *ff;
1878 	struct inode *inode;
1879 	struct page **orig_pages;
1880 	unsigned int max_pages;
1881 };
1882 
1883 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1884 {
1885 	struct fuse_args_pages *ap = &data->wpa->ia.ap;
1886 	struct fuse_conn *fc = get_fuse_conn(data->inode);
1887 	struct page **pages;
1888 	struct fuse_page_desc *descs;
1889 	unsigned int npages = min_t(unsigned int,
1890 				    max_t(unsigned int, data->max_pages * 2,
1891 					  FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1892 				    fc->max_pages);
1893 	WARN_ON(npages <= data->max_pages);
1894 
1895 	pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1896 	if (!pages)
1897 		return false;
1898 
1899 	memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1900 	memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1901 	kfree(ap->pages);
1902 	ap->pages = pages;
1903 	ap->descs = descs;
1904 	data->max_pages = npages;
1905 
1906 	return true;
1907 }
1908 
1909 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1910 {
1911 	struct fuse_writepage_args *wpa = data->wpa;
1912 	struct inode *inode = data->inode;
1913 	struct fuse_inode *fi = get_fuse_inode(inode);
1914 	int num_pages = wpa->ia.ap.num_pages;
1915 	int i;
1916 
1917 	wpa->ia.ff = fuse_file_get(data->ff);
1918 	spin_lock(&fi->lock);
1919 	list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1920 	fuse_flush_writepages(inode);
1921 	spin_unlock(&fi->lock);
1922 
1923 	for (i = 0; i < num_pages; i++)
1924 		end_page_writeback(data->orig_pages[i]);
1925 }
1926 
1927 /*
1928  * First recheck under fi->lock if the offending offset is still under
1929  * writeback.  If yes, then iterate auxiliary write requests, to see if there's
1930  * one already added for a page at this offset.  If there's none, then insert
1931  * this new request onto the auxiliary list, otherwise reuse the existing one by
1932  * copying the new page contents over to the old temporary page.
1933  */
1934 static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
1935 				     struct page *page)
1936 {
1937 	struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1938 	struct fuse_writepage_args *tmp;
1939 	struct fuse_writepage_args *old_wpa;
1940 	struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
1941 
1942 	WARN_ON(new_ap->num_pages != 0);
1943 
1944 	spin_lock(&fi->lock);
1945 	list_del(&new_wpa->writepages_entry);
1946 	old_wpa = fuse_find_writeback(fi, page->index, page->index);
1947 	if (!old_wpa) {
1948 		list_add(&new_wpa->writepages_entry, &fi->writepages);
1949 		spin_unlock(&fi->lock);
1950 		return false;
1951 	}
1952 
1953 	new_ap->num_pages = 1;
1954 	for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
1955 		pgoff_t curr_index;
1956 
1957 		WARN_ON(tmp->inode != new_wpa->inode);
1958 		curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
1959 		if (curr_index == page->index) {
1960 			WARN_ON(tmp->ia.ap.num_pages != 1);
1961 			swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
1962 			break;
1963 		}
1964 	}
1965 
1966 	if (!tmp) {
1967 		new_wpa->next = old_wpa->next;
1968 		old_wpa->next = new_wpa;
1969 	}
1970 
1971 	spin_unlock(&fi->lock);
1972 
1973 	if (tmp) {
1974 		struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
1975 
1976 		dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1977 		dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
1978 		wb_writeout_inc(&bdi->wb);
1979 		fuse_writepage_free(new_wpa);
1980 	}
1981 
1982 	return true;
1983 }
1984 
1985 static int fuse_writepages_fill(struct page *page,
1986 		struct writeback_control *wbc, void *_data)
1987 {
1988 	struct fuse_fill_wb_data *data = _data;
1989 	struct fuse_writepage_args *wpa = data->wpa;
1990 	struct fuse_args_pages *ap = &wpa->ia.ap;
1991 	struct inode *inode = data->inode;
1992 	struct fuse_inode *fi = get_fuse_inode(inode);
1993 	struct fuse_conn *fc = get_fuse_conn(inode);
1994 	struct page *tmp_page;
1995 	bool is_writeback;
1996 	int err;
1997 
1998 	if (!data->ff) {
1999 		err = -EIO;
2000 		data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
2001 		if (!data->ff)
2002 			goto out_unlock;
2003 	}
2004 
2005 	/*
2006 	 * Being under writeback is unlikely but possible.  For example direct
2007 	 * read to an mmaped fuse file will set the page dirty twice; once when
2008 	 * the pages are faulted with get_user_pages(), and then after the read
2009 	 * completed.
2010 	 */
2011 	is_writeback = fuse_page_is_writeback(inode, page->index);
2012 
2013 	if (wpa && ap->num_pages &&
2014 	    (is_writeback || ap->num_pages == fc->max_pages ||
2015 	     (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2016 	     data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
2017 		fuse_writepages_send(data);
2018 		data->wpa = NULL;
2019 	} else if (wpa && ap->num_pages == data->max_pages) {
2020 		if (!fuse_pages_realloc(data)) {
2021 			fuse_writepages_send(data);
2022 			data->wpa = NULL;
2023 		}
2024 	}
2025 
2026 	err = -ENOMEM;
2027 	tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2028 	if (!tmp_page)
2029 		goto out_unlock;
2030 
2031 	/*
2032 	 * The page must not be redirtied until the writeout is completed
2033 	 * (i.e. userspace has sent a reply to the write request).  Otherwise
2034 	 * there could be more than one temporary page instance for each real
2035 	 * page.
2036 	 *
2037 	 * This is ensured by holding the page lock in page_mkwrite() while
2038 	 * checking fuse_page_is_writeback().  We already hold the page lock
2039 	 * since clear_page_dirty_for_io() and keep it held until we add the
2040 	 * request to the fi->writepages list and increment ap->num_pages.
2041 	 * After this fuse_page_is_writeback() will indicate that the page is
2042 	 * under writeback, so we can release the page lock.
2043 	 */
2044 	if (data->wpa == NULL) {
2045 		struct fuse_inode *fi = get_fuse_inode(inode);
2046 
2047 		err = -ENOMEM;
2048 		wpa = fuse_writepage_args_alloc();
2049 		if (!wpa) {
2050 			__free_page(tmp_page);
2051 			goto out_unlock;
2052 		}
2053 		data->max_pages = 1;
2054 
2055 		ap = &wpa->ia.ap;
2056 		fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2057 		wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2058 		wpa->next = NULL;
2059 		ap->args.in_pages = true;
2060 		ap->args.end = fuse_writepage_end;
2061 		ap->num_pages = 0;
2062 		wpa->inode = inode;
2063 
2064 		spin_lock(&fi->lock);
2065 		list_add(&wpa->writepages_entry, &fi->writepages);
2066 		spin_unlock(&fi->lock);
2067 
2068 		data->wpa = wpa;
2069 	}
2070 	set_page_writeback(page);
2071 
2072 	copy_highpage(tmp_page, page);
2073 	ap->pages[ap->num_pages] = tmp_page;
2074 	ap->descs[ap->num_pages].offset = 0;
2075 	ap->descs[ap->num_pages].length = PAGE_SIZE;
2076 
2077 	inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2078 	inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2079 
2080 	err = 0;
2081 	if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
2082 		end_page_writeback(page);
2083 		data->wpa = NULL;
2084 		goto out_unlock;
2085 	}
2086 	data->orig_pages[ap->num_pages] = page;
2087 
2088 	/*
2089 	 * Protected by fi->lock against concurrent access by
2090 	 * fuse_page_is_writeback().
2091 	 */
2092 	spin_lock(&fi->lock);
2093 	ap->num_pages++;
2094 	spin_unlock(&fi->lock);
2095 
2096 out_unlock:
2097 	unlock_page(page);
2098 
2099 	return err;
2100 }
2101 
2102 static int fuse_writepages(struct address_space *mapping,
2103 			   struct writeback_control *wbc)
2104 {
2105 	struct inode *inode = mapping->host;
2106 	struct fuse_conn *fc = get_fuse_conn(inode);
2107 	struct fuse_fill_wb_data data;
2108 	int err;
2109 
2110 	err = -EIO;
2111 	if (is_bad_inode(inode))
2112 		goto out;
2113 
2114 	data.inode = inode;
2115 	data.wpa = NULL;
2116 	data.ff = NULL;
2117 
2118 	err = -ENOMEM;
2119 	data.orig_pages = kcalloc(fc->max_pages,
2120 				  sizeof(struct page *),
2121 				  GFP_NOFS);
2122 	if (!data.orig_pages)
2123 		goto out;
2124 
2125 	err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2126 	if (data.wpa) {
2127 		/* Ignore errors if we can write at least one page */
2128 		WARN_ON(!data.wpa->ia.ap.num_pages);
2129 		fuse_writepages_send(&data);
2130 		err = 0;
2131 	}
2132 	if (data.ff)
2133 		fuse_file_put(data.ff, false, false);
2134 
2135 	kfree(data.orig_pages);
2136 out:
2137 	return err;
2138 }
2139 
2140 /*
2141  * It's worthy to make sure that space is reserved on disk for the write,
2142  * but how to implement it without killing performance need more thinking.
2143  */
2144 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2145 		loff_t pos, unsigned len, unsigned flags,
2146 		struct page **pagep, void **fsdata)
2147 {
2148 	pgoff_t index = pos >> PAGE_SHIFT;
2149 	struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2150 	struct page *page;
2151 	loff_t fsize;
2152 	int err = -ENOMEM;
2153 
2154 	WARN_ON(!fc->writeback_cache);
2155 
2156 	page = grab_cache_page_write_begin(mapping, index, flags);
2157 	if (!page)
2158 		goto error;
2159 
2160 	fuse_wait_on_page_writeback(mapping->host, page->index);
2161 
2162 	if (PageUptodate(page) || len == PAGE_SIZE)
2163 		goto success;
2164 	/*
2165 	 * Check if the start this page comes after the end of file, in which
2166 	 * case the readpage can be optimized away.
2167 	 */
2168 	fsize = i_size_read(mapping->host);
2169 	if (fsize <= (pos & PAGE_MASK)) {
2170 		size_t off = pos & ~PAGE_MASK;
2171 		if (off)
2172 			zero_user_segment(page, 0, off);
2173 		goto success;
2174 	}
2175 	err = fuse_do_readpage(file, page);
2176 	if (err)
2177 		goto cleanup;
2178 success:
2179 	*pagep = page;
2180 	return 0;
2181 
2182 cleanup:
2183 	unlock_page(page);
2184 	put_page(page);
2185 error:
2186 	return err;
2187 }
2188 
2189 static int fuse_write_end(struct file *file, struct address_space *mapping,
2190 		loff_t pos, unsigned len, unsigned copied,
2191 		struct page *page, void *fsdata)
2192 {
2193 	struct inode *inode = page->mapping->host;
2194 
2195 	/* Haven't copied anything?  Skip zeroing, size extending, dirtying. */
2196 	if (!copied)
2197 		goto unlock;
2198 
2199 	if (!PageUptodate(page)) {
2200 		/* Zero any unwritten bytes at the end of the page */
2201 		size_t endoff = (pos + copied) & ~PAGE_MASK;
2202 		if (endoff)
2203 			zero_user_segment(page, endoff, PAGE_SIZE);
2204 		SetPageUptodate(page);
2205 	}
2206 
2207 	fuse_write_update_size(inode, pos + copied);
2208 	set_page_dirty(page);
2209 
2210 unlock:
2211 	unlock_page(page);
2212 	put_page(page);
2213 
2214 	return copied;
2215 }
2216 
2217 static int fuse_launder_page(struct page *page)
2218 {
2219 	int err = 0;
2220 	if (clear_page_dirty_for_io(page)) {
2221 		struct inode *inode = page->mapping->host;
2222 		err = fuse_writepage_locked(page);
2223 		if (!err)
2224 			fuse_wait_on_page_writeback(inode, page->index);
2225 	}
2226 	return err;
2227 }
2228 
2229 /*
2230  * Write back dirty pages now, because there may not be any suitable
2231  * open files later
2232  */
2233 static void fuse_vma_close(struct vm_area_struct *vma)
2234 {
2235 	filemap_write_and_wait(vma->vm_file->f_mapping);
2236 }
2237 
2238 /*
2239  * Wait for writeback against this page to complete before allowing it
2240  * to be marked dirty again, and hence written back again, possibly
2241  * before the previous writepage completed.
2242  *
2243  * Block here, instead of in ->writepage(), so that the userspace fs
2244  * can only block processes actually operating on the filesystem.
2245  *
2246  * Otherwise unprivileged userspace fs would be able to block
2247  * unrelated:
2248  *
2249  * - page migration
2250  * - sync(2)
2251  * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2252  */
2253 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2254 {
2255 	struct page *page = vmf->page;
2256 	struct inode *inode = file_inode(vmf->vma->vm_file);
2257 
2258 	file_update_time(vmf->vma->vm_file);
2259 	lock_page(page);
2260 	if (page->mapping != inode->i_mapping) {
2261 		unlock_page(page);
2262 		return VM_FAULT_NOPAGE;
2263 	}
2264 
2265 	fuse_wait_on_page_writeback(inode, page->index);
2266 	return VM_FAULT_LOCKED;
2267 }
2268 
2269 static const struct vm_operations_struct fuse_file_vm_ops = {
2270 	.close		= fuse_vma_close,
2271 	.fault		= filemap_fault,
2272 	.map_pages	= filemap_map_pages,
2273 	.page_mkwrite	= fuse_page_mkwrite,
2274 };
2275 
2276 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2277 {
2278 	struct fuse_file *ff = file->private_data;
2279 
2280 	if (ff->open_flags & FOPEN_DIRECT_IO) {
2281 		/* Can't provide the coherency needed for MAP_SHARED */
2282 		if (vma->vm_flags & VM_MAYSHARE)
2283 			return -ENODEV;
2284 
2285 		invalidate_inode_pages2(file->f_mapping);
2286 
2287 		return generic_file_mmap(file, vma);
2288 	}
2289 
2290 	if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2291 		fuse_link_write_file(file);
2292 
2293 	file_accessed(file);
2294 	vma->vm_ops = &fuse_file_vm_ops;
2295 	return 0;
2296 }
2297 
2298 static int convert_fuse_file_lock(struct fuse_conn *fc,
2299 				  const struct fuse_file_lock *ffl,
2300 				  struct file_lock *fl)
2301 {
2302 	switch (ffl->type) {
2303 	case F_UNLCK:
2304 		break;
2305 
2306 	case F_RDLCK:
2307 	case F_WRLCK:
2308 		if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2309 		    ffl->end < ffl->start)
2310 			return -EIO;
2311 
2312 		fl->fl_start = ffl->start;
2313 		fl->fl_end = ffl->end;
2314 
2315 		/*
2316 		 * Convert pid into init's pid namespace.  The locks API will
2317 		 * translate it into the caller's pid namespace.
2318 		 */
2319 		rcu_read_lock();
2320 		fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2321 		rcu_read_unlock();
2322 		break;
2323 
2324 	default:
2325 		return -EIO;
2326 	}
2327 	fl->fl_type = ffl->type;
2328 	return 0;
2329 }
2330 
2331 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2332 			 const struct file_lock *fl, int opcode, pid_t pid,
2333 			 int flock, struct fuse_lk_in *inarg)
2334 {
2335 	struct inode *inode = file_inode(file);
2336 	struct fuse_conn *fc = get_fuse_conn(inode);
2337 	struct fuse_file *ff = file->private_data;
2338 
2339 	memset(inarg, 0, sizeof(*inarg));
2340 	inarg->fh = ff->fh;
2341 	inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2342 	inarg->lk.start = fl->fl_start;
2343 	inarg->lk.end = fl->fl_end;
2344 	inarg->lk.type = fl->fl_type;
2345 	inarg->lk.pid = pid;
2346 	if (flock)
2347 		inarg->lk_flags |= FUSE_LK_FLOCK;
2348 	args->opcode = opcode;
2349 	args->nodeid = get_node_id(inode);
2350 	args->in_numargs = 1;
2351 	args->in_args[0].size = sizeof(*inarg);
2352 	args->in_args[0].value = inarg;
2353 }
2354 
2355 static int fuse_getlk(struct file *file, struct file_lock *fl)
2356 {
2357 	struct inode *inode = file_inode(file);
2358 	struct fuse_conn *fc = get_fuse_conn(inode);
2359 	FUSE_ARGS(args);
2360 	struct fuse_lk_in inarg;
2361 	struct fuse_lk_out outarg;
2362 	int err;
2363 
2364 	fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2365 	args.out_numargs = 1;
2366 	args.out_args[0].size = sizeof(outarg);
2367 	args.out_args[0].value = &outarg;
2368 	err = fuse_simple_request(fc, &args);
2369 	if (!err)
2370 		err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2371 
2372 	return err;
2373 }
2374 
2375 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2376 {
2377 	struct inode *inode = file_inode(file);
2378 	struct fuse_conn *fc = get_fuse_conn(inode);
2379 	FUSE_ARGS(args);
2380 	struct fuse_lk_in inarg;
2381 	int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2382 	struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2383 	pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2384 	int err;
2385 
2386 	if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2387 		/* NLM needs asynchronous locks, which we don't support yet */
2388 		return -ENOLCK;
2389 	}
2390 
2391 	/* Unlock on close is handled by the flush method */
2392 	if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2393 		return 0;
2394 
2395 	fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2396 	err = fuse_simple_request(fc, &args);
2397 
2398 	/* locking is restartable */
2399 	if (err == -EINTR)
2400 		err = -ERESTARTSYS;
2401 
2402 	return err;
2403 }
2404 
2405 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2406 {
2407 	struct inode *inode = file_inode(file);
2408 	struct fuse_conn *fc = get_fuse_conn(inode);
2409 	int err;
2410 
2411 	if (cmd == F_CANCELLK) {
2412 		err = 0;
2413 	} else if (cmd == F_GETLK) {
2414 		if (fc->no_lock) {
2415 			posix_test_lock(file, fl);
2416 			err = 0;
2417 		} else
2418 			err = fuse_getlk(file, fl);
2419 	} else {
2420 		if (fc->no_lock)
2421 			err = posix_lock_file(file, fl, NULL);
2422 		else
2423 			err = fuse_setlk(file, fl, 0);
2424 	}
2425 	return err;
2426 }
2427 
2428 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2429 {
2430 	struct inode *inode = file_inode(file);
2431 	struct fuse_conn *fc = get_fuse_conn(inode);
2432 	int err;
2433 
2434 	if (fc->no_flock) {
2435 		err = locks_lock_file_wait(file, fl);
2436 	} else {
2437 		struct fuse_file *ff = file->private_data;
2438 
2439 		/* emulate flock with POSIX locks */
2440 		ff->flock = true;
2441 		err = fuse_setlk(file, fl, 1);
2442 	}
2443 
2444 	return err;
2445 }
2446 
2447 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2448 {
2449 	struct inode *inode = mapping->host;
2450 	struct fuse_conn *fc = get_fuse_conn(inode);
2451 	FUSE_ARGS(args);
2452 	struct fuse_bmap_in inarg;
2453 	struct fuse_bmap_out outarg;
2454 	int err;
2455 
2456 	if (!inode->i_sb->s_bdev || fc->no_bmap)
2457 		return 0;
2458 
2459 	memset(&inarg, 0, sizeof(inarg));
2460 	inarg.block = block;
2461 	inarg.blocksize = inode->i_sb->s_blocksize;
2462 	args.opcode = FUSE_BMAP;
2463 	args.nodeid = get_node_id(inode);
2464 	args.in_numargs = 1;
2465 	args.in_args[0].size = sizeof(inarg);
2466 	args.in_args[0].value = &inarg;
2467 	args.out_numargs = 1;
2468 	args.out_args[0].size = sizeof(outarg);
2469 	args.out_args[0].value = &outarg;
2470 	err = fuse_simple_request(fc, &args);
2471 	if (err == -ENOSYS)
2472 		fc->no_bmap = 1;
2473 
2474 	return err ? 0 : outarg.block;
2475 }
2476 
2477 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2478 {
2479 	struct inode *inode = file->f_mapping->host;
2480 	struct fuse_conn *fc = get_fuse_conn(inode);
2481 	struct fuse_file *ff = file->private_data;
2482 	FUSE_ARGS(args);
2483 	struct fuse_lseek_in inarg = {
2484 		.fh = ff->fh,
2485 		.offset = offset,
2486 		.whence = whence
2487 	};
2488 	struct fuse_lseek_out outarg;
2489 	int err;
2490 
2491 	if (fc->no_lseek)
2492 		goto fallback;
2493 
2494 	args.opcode = FUSE_LSEEK;
2495 	args.nodeid = ff->nodeid;
2496 	args.in_numargs = 1;
2497 	args.in_args[0].size = sizeof(inarg);
2498 	args.in_args[0].value = &inarg;
2499 	args.out_numargs = 1;
2500 	args.out_args[0].size = sizeof(outarg);
2501 	args.out_args[0].value = &outarg;
2502 	err = fuse_simple_request(fc, &args);
2503 	if (err) {
2504 		if (err == -ENOSYS) {
2505 			fc->no_lseek = 1;
2506 			goto fallback;
2507 		}
2508 		return err;
2509 	}
2510 
2511 	return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2512 
2513 fallback:
2514 	err = fuse_update_attributes(inode, file);
2515 	if (!err)
2516 		return generic_file_llseek(file, offset, whence);
2517 	else
2518 		return err;
2519 }
2520 
2521 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2522 {
2523 	loff_t retval;
2524 	struct inode *inode = file_inode(file);
2525 
2526 	switch (whence) {
2527 	case SEEK_SET:
2528 	case SEEK_CUR:
2529 		 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2530 		retval = generic_file_llseek(file, offset, whence);
2531 		break;
2532 	case SEEK_END:
2533 		inode_lock(inode);
2534 		retval = fuse_update_attributes(inode, file);
2535 		if (!retval)
2536 			retval = generic_file_llseek(file, offset, whence);
2537 		inode_unlock(inode);
2538 		break;
2539 	case SEEK_HOLE:
2540 	case SEEK_DATA:
2541 		inode_lock(inode);
2542 		retval = fuse_lseek(file, offset, whence);
2543 		inode_unlock(inode);
2544 		break;
2545 	default:
2546 		retval = -EINVAL;
2547 	}
2548 
2549 	return retval;
2550 }
2551 
2552 /*
2553  * CUSE servers compiled on 32bit broke on 64bit kernels because the
2554  * ABI was defined to be 'struct iovec' which is different on 32bit
2555  * and 64bit.  Fortunately we can determine which structure the server
2556  * used from the size of the reply.
2557  */
2558 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2559 				     size_t transferred, unsigned count,
2560 				     bool is_compat)
2561 {
2562 #ifdef CONFIG_COMPAT
2563 	if (count * sizeof(struct compat_iovec) == transferred) {
2564 		struct compat_iovec *ciov = src;
2565 		unsigned i;
2566 
2567 		/*
2568 		 * With this interface a 32bit server cannot support
2569 		 * non-compat (i.e. ones coming from 64bit apps) ioctl
2570 		 * requests
2571 		 */
2572 		if (!is_compat)
2573 			return -EINVAL;
2574 
2575 		for (i = 0; i < count; i++) {
2576 			dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2577 			dst[i].iov_len = ciov[i].iov_len;
2578 		}
2579 		return 0;
2580 	}
2581 #endif
2582 
2583 	if (count * sizeof(struct iovec) != transferred)
2584 		return -EIO;
2585 
2586 	memcpy(dst, src, transferred);
2587 	return 0;
2588 }
2589 
2590 /* Make sure iov_length() won't overflow */
2591 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2592 				 size_t count)
2593 {
2594 	size_t n;
2595 	u32 max = fc->max_pages << PAGE_SHIFT;
2596 
2597 	for (n = 0; n < count; n++, iov++) {
2598 		if (iov->iov_len > (size_t) max)
2599 			return -ENOMEM;
2600 		max -= iov->iov_len;
2601 	}
2602 	return 0;
2603 }
2604 
2605 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2606 				 void *src, size_t transferred, unsigned count,
2607 				 bool is_compat)
2608 {
2609 	unsigned i;
2610 	struct fuse_ioctl_iovec *fiov = src;
2611 
2612 	if (fc->minor < 16) {
2613 		return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2614 						 count, is_compat);
2615 	}
2616 
2617 	if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2618 		return -EIO;
2619 
2620 	for (i = 0; i < count; i++) {
2621 		/* Did the server supply an inappropriate value? */
2622 		if (fiov[i].base != (unsigned long) fiov[i].base ||
2623 		    fiov[i].len != (unsigned long) fiov[i].len)
2624 			return -EIO;
2625 
2626 		dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2627 		dst[i].iov_len = (size_t) fiov[i].len;
2628 
2629 #ifdef CONFIG_COMPAT
2630 		if (is_compat &&
2631 		    (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2632 		     (compat_size_t) dst[i].iov_len != fiov[i].len))
2633 			return -EIO;
2634 #endif
2635 	}
2636 
2637 	return 0;
2638 }
2639 
2640 
2641 /*
2642  * For ioctls, there is no generic way to determine how much memory
2643  * needs to be read and/or written.  Furthermore, ioctls are allowed
2644  * to dereference the passed pointer, so the parameter requires deep
2645  * copying but FUSE has no idea whatsoever about what to copy in or
2646  * out.
2647  *
2648  * This is solved by allowing FUSE server to retry ioctl with
2649  * necessary in/out iovecs.  Let's assume the ioctl implementation
2650  * needs to read in the following structure.
2651  *
2652  * struct a {
2653  *	char	*buf;
2654  *	size_t	buflen;
2655  * }
2656  *
2657  * On the first callout to FUSE server, inarg->in_size and
2658  * inarg->out_size will be NULL; then, the server completes the ioctl
2659  * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2660  * the actual iov array to
2661  *
2662  * { { .iov_base = inarg.arg,	.iov_len = sizeof(struct a) } }
2663  *
2664  * which tells FUSE to copy in the requested area and retry the ioctl.
2665  * On the second round, the server has access to the structure and
2666  * from that it can tell what to look for next, so on the invocation,
2667  * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2668  *
2669  * { { .iov_base = inarg.arg,	.iov_len = sizeof(struct a)	},
2670  *   { .iov_base = a.buf,	.iov_len = a.buflen		} }
2671  *
2672  * FUSE will copy both struct a and the pointed buffer from the
2673  * process doing the ioctl and retry ioctl with both struct a and the
2674  * buffer.
2675  *
2676  * This time, FUSE server has everything it needs and completes ioctl
2677  * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2678  *
2679  * Copying data out works the same way.
2680  *
2681  * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2682  * automatically initializes in and out iovs by decoding @cmd with
2683  * _IOC_* macros and the server is not allowed to request RETRY.  This
2684  * limits ioctl data transfers to well-formed ioctls and is the forced
2685  * behavior for all FUSE servers.
2686  */
2687 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2688 		   unsigned int flags)
2689 {
2690 	struct fuse_file *ff = file->private_data;
2691 	struct fuse_conn *fc = ff->fc;
2692 	struct fuse_ioctl_in inarg = {
2693 		.fh = ff->fh,
2694 		.cmd = cmd,
2695 		.arg = arg,
2696 		.flags = flags
2697 	};
2698 	struct fuse_ioctl_out outarg;
2699 	struct iovec *iov_page = NULL;
2700 	struct iovec *in_iov = NULL, *out_iov = NULL;
2701 	unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2702 	size_t in_size, out_size, c;
2703 	ssize_t transferred;
2704 	int err, i;
2705 	struct iov_iter ii;
2706 	struct fuse_args_pages ap = {};
2707 
2708 #if BITS_PER_LONG == 32
2709 	inarg.flags |= FUSE_IOCTL_32BIT;
2710 #else
2711 	if (flags & FUSE_IOCTL_COMPAT) {
2712 		inarg.flags |= FUSE_IOCTL_32BIT;
2713 #ifdef CONFIG_X86_X32
2714 		if (in_x32_syscall())
2715 			inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2716 #endif
2717 	}
2718 #endif
2719 
2720 	/* assume all the iovs returned by client always fits in a page */
2721 	BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2722 
2723 	err = -ENOMEM;
2724 	ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2725 	iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2726 	if (!ap.pages || !iov_page)
2727 		goto out;
2728 
2729 	fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2730 
2731 	/*
2732 	 * If restricted, initialize IO parameters as encoded in @cmd.
2733 	 * RETRY from server is not allowed.
2734 	 */
2735 	if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2736 		struct iovec *iov = iov_page;
2737 
2738 		iov->iov_base = (void __user *)arg;
2739 		iov->iov_len = _IOC_SIZE(cmd);
2740 
2741 		if (_IOC_DIR(cmd) & _IOC_WRITE) {
2742 			in_iov = iov;
2743 			in_iovs = 1;
2744 		}
2745 
2746 		if (_IOC_DIR(cmd) & _IOC_READ) {
2747 			out_iov = iov;
2748 			out_iovs = 1;
2749 		}
2750 	}
2751 
2752  retry:
2753 	inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2754 	inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2755 
2756 	/*
2757 	 * Out data can be used either for actual out data or iovs,
2758 	 * make sure there always is at least one page.
2759 	 */
2760 	out_size = max_t(size_t, out_size, PAGE_SIZE);
2761 	max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2762 
2763 	/* make sure there are enough buffer pages and init request with them */
2764 	err = -ENOMEM;
2765 	if (max_pages > fc->max_pages)
2766 		goto out;
2767 	while (ap.num_pages < max_pages) {
2768 		ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2769 		if (!ap.pages[ap.num_pages])
2770 			goto out;
2771 		ap.num_pages++;
2772 	}
2773 
2774 
2775 	/* okay, let's send it to the client */
2776 	ap.args.opcode = FUSE_IOCTL;
2777 	ap.args.nodeid = ff->nodeid;
2778 	ap.args.in_numargs = 1;
2779 	ap.args.in_args[0].size = sizeof(inarg);
2780 	ap.args.in_args[0].value = &inarg;
2781 	if (in_size) {
2782 		ap.args.in_numargs++;
2783 		ap.args.in_args[1].size = in_size;
2784 		ap.args.in_pages = true;
2785 
2786 		err = -EFAULT;
2787 		iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2788 		for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2789 			c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2790 			if (c != PAGE_SIZE && iov_iter_count(&ii))
2791 				goto out;
2792 		}
2793 	}
2794 
2795 	ap.args.out_numargs = 2;
2796 	ap.args.out_args[0].size = sizeof(outarg);
2797 	ap.args.out_args[0].value = &outarg;
2798 	ap.args.out_args[1].size = out_size;
2799 	ap.args.out_pages = true;
2800 	ap.args.out_argvar = true;
2801 
2802 	transferred = fuse_simple_request(fc, &ap.args);
2803 	err = transferred;
2804 	if (transferred < 0)
2805 		goto out;
2806 
2807 	/* did it ask for retry? */
2808 	if (outarg.flags & FUSE_IOCTL_RETRY) {
2809 		void *vaddr;
2810 
2811 		/* no retry if in restricted mode */
2812 		err = -EIO;
2813 		if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2814 			goto out;
2815 
2816 		in_iovs = outarg.in_iovs;
2817 		out_iovs = outarg.out_iovs;
2818 
2819 		/*
2820 		 * Make sure things are in boundary, separate checks
2821 		 * are to protect against overflow.
2822 		 */
2823 		err = -ENOMEM;
2824 		if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2825 		    out_iovs > FUSE_IOCTL_MAX_IOV ||
2826 		    in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2827 			goto out;
2828 
2829 		vaddr = kmap_atomic(ap.pages[0]);
2830 		err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2831 					    transferred, in_iovs + out_iovs,
2832 					    (flags & FUSE_IOCTL_COMPAT) != 0);
2833 		kunmap_atomic(vaddr);
2834 		if (err)
2835 			goto out;
2836 
2837 		in_iov = iov_page;
2838 		out_iov = in_iov + in_iovs;
2839 
2840 		err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2841 		if (err)
2842 			goto out;
2843 
2844 		err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2845 		if (err)
2846 			goto out;
2847 
2848 		goto retry;
2849 	}
2850 
2851 	err = -EIO;
2852 	if (transferred > inarg.out_size)
2853 		goto out;
2854 
2855 	err = -EFAULT;
2856 	iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2857 	for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2858 		c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2859 		if (c != PAGE_SIZE && iov_iter_count(&ii))
2860 			goto out;
2861 	}
2862 	err = 0;
2863  out:
2864 	free_page((unsigned long) iov_page);
2865 	while (ap.num_pages)
2866 		__free_page(ap.pages[--ap.num_pages]);
2867 	kfree(ap.pages);
2868 
2869 	return err ? err : outarg.result;
2870 }
2871 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2872 
2873 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2874 		       unsigned long arg, unsigned int flags)
2875 {
2876 	struct inode *inode = file_inode(file);
2877 	struct fuse_conn *fc = get_fuse_conn(inode);
2878 
2879 	if (!fuse_allow_current_process(fc))
2880 		return -EACCES;
2881 
2882 	if (is_bad_inode(inode))
2883 		return -EIO;
2884 
2885 	return fuse_do_ioctl(file, cmd, arg, flags);
2886 }
2887 
2888 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2889 			    unsigned long arg)
2890 {
2891 	return fuse_ioctl_common(file, cmd, arg, 0);
2892 }
2893 
2894 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2895 				   unsigned long arg)
2896 {
2897 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2898 }
2899 
2900 /*
2901  * All files which have been polled are linked to RB tree
2902  * fuse_conn->polled_files which is indexed by kh.  Walk the tree and
2903  * find the matching one.
2904  */
2905 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2906 					      struct rb_node **parent_out)
2907 {
2908 	struct rb_node **link = &fc->polled_files.rb_node;
2909 	struct rb_node *last = NULL;
2910 
2911 	while (*link) {
2912 		struct fuse_file *ff;
2913 
2914 		last = *link;
2915 		ff = rb_entry(last, struct fuse_file, polled_node);
2916 
2917 		if (kh < ff->kh)
2918 			link = &last->rb_left;
2919 		else if (kh > ff->kh)
2920 			link = &last->rb_right;
2921 		else
2922 			return link;
2923 	}
2924 
2925 	if (parent_out)
2926 		*parent_out = last;
2927 	return link;
2928 }
2929 
2930 /*
2931  * The file is about to be polled.  Make sure it's on the polled_files
2932  * RB tree.  Note that files once added to the polled_files tree are
2933  * not removed before the file is released.  This is because a file
2934  * polled once is likely to be polled again.
2935  */
2936 static void fuse_register_polled_file(struct fuse_conn *fc,
2937 				      struct fuse_file *ff)
2938 {
2939 	spin_lock(&fc->lock);
2940 	if (RB_EMPTY_NODE(&ff->polled_node)) {
2941 		struct rb_node **link, *uninitialized_var(parent);
2942 
2943 		link = fuse_find_polled_node(fc, ff->kh, &parent);
2944 		BUG_ON(*link);
2945 		rb_link_node(&ff->polled_node, parent, link);
2946 		rb_insert_color(&ff->polled_node, &fc->polled_files);
2947 	}
2948 	spin_unlock(&fc->lock);
2949 }
2950 
2951 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2952 {
2953 	struct fuse_file *ff = file->private_data;
2954 	struct fuse_conn *fc = ff->fc;
2955 	struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2956 	struct fuse_poll_out outarg;
2957 	FUSE_ARGS(args);
2958 	int err;
2959 
2960 	if (fc->no_poll)
2961 		return DEFAULT_POLLMASK;
2962 
2963 	poll_wait(file, &ff->poll_wait, wait);
2964 	inarg.events = mangle_poll(poll_requested_events(wait));
2965 
2966 	/*
2967 	 * Ask for notification iff there's someone waiting for it.
2968 	 * The client may ignore the flag and always notify.
2969 	 */
2970 	if (waitqueue_active(&ff->poll_wait)) {
2971 		inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2972 		fuse_register_polled_file(fc, ff);
2973 	}
2974 
2975 	args.opcode = FUSE_POLL;
2976 	args.nodeid = ff->nodeid;
2977 	args.in_numargs = 1;
2978 	args.in_args[0].size = sizeof(inarg);
2979 	args.in_args[0].value = &inarg;
2980 	args.out_numargs = 1;
2981 	args.out_args[0].size = sizeof(outarg);
2982 	args.out_args[0].value = &outarg;
2983 	err = fuse_simple_request(fc, &args);
2984 
2985 	if (!err)
2986 		return demangle_poll(outarg.revents);
2987 	if (err == -ENOSYS) {
2988 		fc->no_poll = 1;
2989 		return DEFAULT_POLLMASK;
2990 	}
2991 	return EPOLLERR;
2992 }
2993 EXPORT_SYMBOL_GPL(fuse_file_poll);
2994 
2995 /*
2996  * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2997  * wakes up the poll waiters.
2998  */
2999 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3000 			    struct fuse_notify_poll_wakeup_out *outarg)
3001 {
3002 	u64 kh = outarg->kh;
3003 	struct rb_node **link;
3004 
3005 	spin_lock(&fc->lock);
3006 
3007 	link = fuse_find_polled_node(fc, kh, NULL);
3008 	if (*link) {
3009 		struct fuse_file *ff;
3010 
3011 		ff = rb_entry(*link, struct fuse_file, polled_node);
3012 		wake_up_interruptible_sync(&ff->poll_wait);
3013 	}
3014 
3015 	spin_unlock(&fc->lock);
3016 	return 0;
3017 }
3018 
3019 static void fuse_do_truncate(struct file *file)
3020 {
3021 	struct inode *inode = file->f_mapping->host;
3022 	struct iattr attr;
3023 
3024 	attr.ia_valid = ATTR_SIZE;
3025 	attr.ia_size = i_size_read(inode);
3026 
3027 	attr.ia_file = file;
3028 	attr.ia_valid |= ATTR_FILE;
3029 
3030 	fuse_do_setattr(file_dentry(file), &attr, file);
3031 }
3032 
3033 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3034 {
3035 	return round_up(off, fc->max_pages << PAGE_SHIFT);
3036 }
3037 
3038 static ssize_t
3039 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3040 {
3041 	DECLARE_COMPLETION_ONSTACK(wait);
3042 	ssize_t ret = 0;
3043 	struct file *file = iocb->ki_filp;
3044 	struct fuse_file *ff = file->private_data;
3045 	bool async_dio = ff->fc->async_dio;
3046 	loff_t pos = 0;
3047 	struct inode *inode;
3048 	loff_t i_size;
3049 	size_t count = iov_iter_count(iter);
3050 	loff_t offset = iocb->ki_pos;
3051 	struct fuse_io_priv *io;
3052 
3053 	pos = offset;
3054 	inode = file->f_mapping->host;
3055 	i_size = i_size_read(inode);
3056 
3057 	if ((iov_iter_rw(iter) == READ) && (offset > i_size))
3058 		return 0;
3059 
3060 	/* optimization for short read */
3061 	if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
3062 		if (offset >= i_size)
3063 			return 0;
3064 		iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3065 		count = iov_iter_count(iter);
3066 	}
3067 
3068 	io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3069 	if (!io)
3070 		return -ENOMEM;
3071 	spin_lock_init(&io->lock);
3072 	kref_init(&io->refcnt);
3073 	io->reqs = 1;
3074 	io->bytes = -1;
3075 	io->size = 0;
3076 	io->offset = offset;
3077 	io->write = (iov_iter_rw(iter) == WRITE);
3078 	io->err = 0;
3079 	/*
3080 	 * By default, we want to optimize all I/Os with async request
3081 	 * submission to the client filesystem if supported.
3082 	 */
3083 	io->async = async_dio;
3084 	io->iocb = iocb;
3085 	io->blocking = is_sync_kiocb(iocb);
3086 
3087 	/*
3088 	 * We cannot asynchronously extend the size of a file.
3089 	 * In such case the aio will behave exactly like sync io.
3090 	 */
3091 	if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3092 		io->blocking = true;
3093 
3094 	if (io->async && io->blocking) {
3095 		/*
3096 		 * Additional reference to keep io around after
3097 		 * calling fuse_aio_complete()
3098 		 */
3099 		kref_get(&io->refcnt);
3100 		io->done = &wait;
3101 	}
3102 
3103 	if (iov_iter_rw(iter) == WRITE) {
3104 		ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3105 		fuse_invalidate_attr(inode);
3106 	} else {
3107 		ret = __fuse_direct_read(io, iter, &pos);
3108 	}
3109 
3110 	if (io->async) {
3111 		bool blocking = io->blocking;
3112 
3113 		fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3114 
3115 		/* we have a non-extending, async request, so return */
3116 		if (!blocking)
3117 			return -EIOCBQUEUED;
3118 
3119 		wait_for_completion(&wait);
3120 		ret = fuse_get_res_by_io(io);
3121 	}
3122 
3123 	kref_put(&io->refcnt, fuse_io_release);
3124 
3125 	if (iov_iter_rw(iter) == WRITE) {
3126 		if (ret > 0)
3127 			fuse_write_update_size(inode, pos);
3128 		else if (ret < 0 && offset + count > i_size)
3129 			fuse_do_truncate(file);
3130 	}
3131 
3132 	return ret;
3133 }
3134 
3135 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3136 {
3137 	int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3138 
3139 	if (!err)
3140 		fuse_sync_writes(inode);
3141 
3142 	return err;
3143 }
3144 
3145 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3146 				loff_t length)
3147 {
3148 	struct fuse_file *ff = file->private_data;
3149 	struct inode *inode = file_inode(file);
3150 	struct fuse_inode *fi = get_fuse_inode(inode);
3151 	struct fuse_conn *fc = ff->fc;
3152 	FUSE_ARGS(args);
3153 	struct fuse_fallocate_in inarg = {
3154 		.fh = ff->fh,
3155 		.offset = offset,
3156 		.length = length,
3157 		.mode = mode
3158 	};
3159 	int err;
3160 	bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3161 			   (mode & FALLOC_FL_PUNCH_HOLE);
3162 
3163 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3164 		return -EOPNOTSUPP;
3165 
3166 	if (fc->no_fallocate)
3167 		return -EOPNOTSUPP;
3168 
3169 	if (lock_inode) {
3170 		inode_lock(inode);
3171 		if (mode & FALLOC_FL_PUNCH_HOLE) {
3172 			loff_t endbyte = offset + length - 1;
3173 
3174 			err = fuse_writeback_range(inode, offset, endbyte);
3175 			if (err)
3176 				goto out;
3177 		}
3178 	}
3179 
3180 	if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3181 	    offset + length > i_size_read(inode)) {
3182 		err = inode_newsize_ok(inode, offset + length);
3183 		if (err)
3184 			goto out;
3185 	}
3186 
3187 	if (!(mode & FALLOC_FL_KEEP_SIZE))
3188 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3189 
3190 	args.opcode = FUSE_FALLOCATE;
3191 	args.nodeid = ff->nodeid;
3192 	args.in_numargs = 1;
3193 	args.in_args[0].size = sizeof(inarg);
3194 	args.in_args[0].value = &inarg;
3195 	err = fuse_simple_request(fc, &args);
3196 	if (err == -ENOSYS) {
3197 		fc->no_fallocate = 1;
3198 		err = -EOPNOTSUPP;
3199 	}
3200 	if (err)
3201 		goto out;
3202 
3203 	/* we could have extended the file */
3204 	if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3205 		bool changed = fuse_write_update_size(inode, offset + length);
3206 
3207 		if (changed && fc->writeback_cache)
3208 			file_update_time(file);
3209 	}
3210 
3211 	if (mode & FALLOC_FL_PUNCH_HOLE)
3212 		truncate_pagecache_range(inode, offset, offset + length - 1);
3213 
3214 	fuse_invalidate_attr(inode);
3215 
3216 out:
3217 	if (!(mode & FALLOC_FL_KEEP_SIZE))
3218 		clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3219 
3220 	if (lock_inode)
3221 		inode_unlock(inode);
3222 
3223 	return err;
3224 }
3225 
3226 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3227 				      struct file *file_out, loff_t pos_out,
3228 				      size_t len, unsigned int flags)
3229 {
3230 	struct fuse_file *ff_in = file_in->private_data;
3231 	struct fuse_file *ff_out = file_out->private_data;
3232 	struct inode *inode_in = file_inode(file_in);
3233 	struct inode *inode_out = file_inode(file_out);
3234 	struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3235 	struct fuse_conn *fc = ff_in->fc;
3236 	FUSE_ARGS(args);
3237 	struct fuse_copy_file_range_in inarg = {
3238 		.fh_in = ff_in->fh,
3239 		.off_in = pos_in,
3240 		.nodeid_out = ff_out->nodeid,
3241 		.fh_out = ff_out->fh,
3242 		.off_out = pos_out,
3243 		.len = len,
3244 		.flags = flags
3245 	};
3246 	struct fuse_write_out outarg;
3247 	ssize_t err;
3248 	/* mark unstable when write-back is not used, and file_out gets
3249 	 * extended */
3250 	bool is_unstable = (!fc->writeback_cache) &&
3251 			   ((pos_out + len) > inode_out->i_size);
3252 
3253 	if (fc->no_copy_file_range)
3254 		return -EOPNOTSUPP;
3255 
3256 	if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3257 		return -EXDEV;
3258 
3259 	if (fc->writeback_cache) {
3260 		inode_lock(inode_in);
3261 		err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
3262 		inode_unlock(inode_in);
3263 		if (err)
3264 			return err;
3265 	}
3266 
3267 	inode_lock(inode_out);
3268 
3269 	err = file_modified(file_out);
3270 	if (err)
3271 		goto out;
3272 
3273 	if (fc->writeback_cache) {
3274 		err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
3275 		if (err)
3276 			goto out;
3277 	}
3278 
3279 	if (is_unstable)
3280 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3281 
3282 	args.opcode = FUSE_COPY_FILE_RANGE;
3283 	args.nodeid = ff_in->nodeid;
3284 	args.in_numargs = 1;
3285 	args.in_args[0].size = sizeof(inarg);
3286 	args.in_args[0].value = &inarg;
3287 	args.out_numargs = 1;
3288 	args.out_args[0].size = sizeof(outarg);
3289 	args.out_args[0].value = &outarg;
3290 	err = fuse_simple_request(fc, &args);
3291 	if (err == -ENOSYS) {
3292 		fc->no_copy_file_range = 1;
3293 		err = -EOPNOTSUPP;
3294 	}
3295 	if (err)
3296 		goto out;
3297 
3298 	if (fc->writeback_cache) {
3299 		fuse_write_update_size(inode_out, pos_out + outarg.size);
3300 		file_update_time(file_out);
3301 	}
3302 
3303 	fuse_invalidate_attr(inode_out);
3304 
3305 	err = outarg.size;
3306 out:
3307 	if (is_unstable)
3308 		clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3309 
3310 	inode_unlock(inode_out);
3311 	file_accessed(file_in);
3312 
3313 	return err;
3314 }
3315 
3316 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3317 				    struct file *dst_file, loff_t dst_off,
3318 				    size_t len, unsigned int flags)
3319 {
3320 	ssize_t ret;
3321 
3322 	ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3323 				     len, flags);
3324 
3325 	if (ret == -EOPNOTSUPP || ret == -EXDEV)
3326 		ret = generic_copy_file_range(src_file, src_off, dst_file,
3327 					      dst_off, len, flags);
3328 	return ret;
3329 }
3330 
3331 static const struct file_operations fuse_file_operations = {
3332 	.llseek		= fuse_file_llseek,
3333 	.read_iter	= fuse_file_read_iter,
3334 	.write_iter	= fuse_file_write_iter,
3335 	.mmap		= fuse_file_mmap,
3336 	.open		= fuse_open,
3337 	.flush		= fuse_flush,
3338 	.release	= fuse_release,
3339 	.fsync		= fuse_fsync,
3340 	.lock		= fuse_file_lock,
3341 	.flock		= fuse_file_flock,
3342 	.splice_read	= generic_file_splice_read,
3343 	.splice_write	= iter_file_splice_write,
3344 	.unlocked_ioctl	= fuse_file_ioctl,
3345 	.compat_ioctl	= fuse_file_compat_ioctl,
3346 	.poll		= fuse_file_poll,
3347 	.fallocate	= fuse_file_fallocate,
3348 	.copy_file_range = fuse_copy_file_range,
3349 };
3350 
3351 static const struct address_space_operations fuse_file_aops  = {
3352 	.readpage	= fuse_readpage,
3353 	.writepage	= fuse_writepage,
3354 	.writepages	= fuse_writepages,
3355 	.launder_page	= fuse_launder_page,
3356 	.readpages	= fuse_readpages,
3357 	.set_page_dirty	= __set_page_dirty_nobuffers,
3358 	.bmap		= fuse_bmap,
3359 	.direct_IO	= fuse_direct_IO,
3360 	.write_begin	= fuse_write_begin,
3361 	.write_end	= fuse_write_end,
3362 };
3363 
3364 void fuse_init_file_inode(struct inode *inode)
3365 {
3366 	struct fuse_inode *fi = get_fuse_inode(inode);
3367 
3368 	inode->i_fop = &fuse_file_operations;
3369 	inode->i_data.a_ops = &fuse_file_aops;
3370 
3371 	INIT_LIST_HEAD(&fi->write_files);
3372 	INIT_LIST_HEAD(&fi->queued_writes);
3373 	fi->writectr = 0;
3374 	init_waitqueue_head(&fi->page_waitq);
3375 	INIT_LIST_HEAD(&fi->writepages);
3376 }
3377