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