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