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/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
21 #include <linux/fs.h>
22 #include <trace/hooks/tmpfile.h>
23
fuse_send_open(struct fuse_mount * fm,u64 nodeid,unsigned int open_flags,int opcode,struct fuse_open_out * outargp)24 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
25 unsigned int open_flags, int opcode,
26 struct fuse_open_out *outargp)
27 {
28 struct fuse_open_in inarg;
29 FUSE_ARGS(args);
30
31 memset(&inarg, 0, sizeof(inarg));
32 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
33 trace_android_vh_tmpfile_send_open(&inarg.flags);
34 if (!fm->fc->atomic_o_trunc)
35 inarg.flags &= ~O_TRUNC;
36
37 if (fm->fc->handle_killpriv_v2 &&
38 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
39 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
40 }
41
42 args.opcode = opcode;
43 args.nodeid = nodeid;
44 args.in_numargs = 1;
45 args.in_args[0].size = sizeof(inarg);
46 args.in_args[0].value = &inarg;
47 args.out_numargs = 1;
48 args.out_args[0].size = sizeof(*outargp);
49 args.out_args[0].value = outargp;
50
51 return fuse_simple_request(fm, &args);
52 }
53
54 struct fuse_release_args {
55 struct fuse_args args;
56 struct fuse_release_in inarg;
57 struct inode *inode;
58 };
59
fuse_file_alloc(struct fuse_mount * fm)60 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
61 {
62 struct fuse_file *ff;
63
64 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
65 if (unlikely(!ff))
66 return NULL;
67
68 ff->fm = fm;
69 ff->release_args = kzalloc(sizeof(*ff->release_args),
70 GFP_KERNEL_ACCOUNT);
71 if (!ff->release_args) {
72 kfree(ff);
73 return NULL;
74 }
75
76 INIT_LIST_HEAD(&ff->write_entry);
77 mutex_init(&ff->readdir.lock);
78 refcount_set(&ff->count, 1);
79 RB_CLEAR_NODE(&ff->polled_node);
80 init_waitqueue_head(&ff->poll_wait);
81
82 ff->kh = atomic64_inc_return(&fm->fc->khctr);
83
84 return ff;
85 }
86
fuse_file_free(struct fuse_file * ff)87 void fuse_file_free(struct fuse_file *ff)
88 {
89 kfree(ff->release_args);
90 mutex_destroy(&ff->readdir.lock);
91 kfree(ff);
92 }
93
fuse_file_get(struct fuse_file * ff)94 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
95 {
96 refcount_inc(&ff->count);
97 return ff;
98 }
99
fuse_release_end(struct fuse_mount * fm,struct fuse_args * args,int error)100 static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
101 int error)
102 {
103 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
104
105 iput(ra->inode);
106 kfree(ra);
107 }
108
fuse_file_put(struct inode * inode,struct fuse_file * ff,bool sync,bool isdir)109 static void fuse_file_put(struct inode *inode, struct fuse_file *ff,
110 bool sync, bool isdir)
111 {
112 struct fuse_args *args = &ff->release_args->args;
113 #ifdef CONFIG_FUSE_BPF
114 struct fuse_err_ret fer;
115 #endif
116
117 if (!refcount_dec_and_test(&ff->count))
118 return;
119
120 #ifdef CONFIG_FUSE_BPF
121 fer = fuse_bpf_backing(inode, struct fuse_release_in,
122 fuse_release_initialize, fuse_release_backing,
123 fuse_release_finalize,
124 inode, ff);
125 if (fer.ret) {
126 fuse_release_end(ff->fm, args, 0);
127 } else
128 #endif
129 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
130 /* Do nothing when client does not implement 'open' */
131 fuse_release_end(ff->fm, args, 0);
132 } else if (sync) {
133 fuse_simple_request(ff->fm, args);
134 fuse_release_end(ff->fm, args, 0);
135 } else {
136 args->end = fuse_release_end;
137 if (fuse_simple_background(ff->fm, args,
138 GFP_KERNEL | __GFP_NOFAIL))
139 fuse_release_end(ff->fm, args, -ENOTCONN);
140 }
141 kfree(ff);
142 }
143
fuse_file_open(struct fuse_mount * fm,u64 nodeid,unsigned int open_flags,bool isdir)144 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
145 unsigned int open_flags, bool isdir)
146 {
147 struct fuse_conn *fc = fm->fc;
148 struct fuse_file *ff;
149 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
150
151 ff = fuse_file_alloc(fm);
152 if (!ff)
153 return ERR_PTR(-ENOMEM);
154
155 ff->fh = 0;
156 /* Default for no-open */
157 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
158 if (isdir ? !fc->no_opendir : !fc->no_open) {
159 struct fuse_open_out outarg;
160 int err;
161
162 err = fuse_send_open(fm, nodeid, open_flags, opcode, &outarg);
163 if (!err) {
164 ff->fh = outarg.fh;
165 ff->open_flags = outarg.open_flags;
166 fuse_passthrough_setup(fc, ff, &outarg);
167 } else if (err != -ENOSYS) {
168 fuse_file_free(ff);
169 return ERR_PTR(err);
170 } else {
171 if (isdir)
172 fc->no_opendir = 1;
173 else
174 fc->no_open = 1;
175 }
176 }
177
178 if (isdir)
179 ff->open_flags &= ~FOPEN_DIRECT_IO;
180
181 ff->nodeid = nodeid;
182
183 return ff;
184 }
185
fuse_do_open(struct fuse_mount * fm,u64 nodeid,struct file * file,bool isdir)186 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
187 bool isdir)
188 {
189 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
190
191 if (!IS_ERR(ff))
192 file->private_data = ff;
193
194 return PTR_ERR_OR_ZERO(ff);
195 }
196 EXPORT_SYMBOL_GPL(fuse_do_open);
197
fuse_link_write_file(struct file * file)198 static void fuse_link_write_file(struct file *file)
199 {
200 struct inode *inode = file_inode(file);
201 struct fuse_inode *fi = get_fuse_inode(inode);
202 struct fuse_file *ff = file->private_data;
203 /*
204 * file may be written through mmap, so chain it onto the
205 * inodes's write_file list
206 */
207 spin_lock(&fi->lock);
208 if (list_empty(&ff->write_entry))
209 list_add(&ff->write_entry, &fi->write_files);
210 spin_unlock(&fi->lock);
211 }
212
fuse_finish_open(struct inode * inode,struct file * file)213 void fuse_finish_open(struct inode *inode, struct file *file)
214 {
215 struct fuse_file *ff = file->private_data;
216 struct fuse_conn *fc = get_fuse_conn(inode);
217
218 if (ff->open_flags & FOPEN_STREAM)
219 stream_open(inode, file);
220 else if (ff->open_flags & FOPEN_NONSEEKABLE)
221 nonseekable_open(inode, file);
222
223 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
224 struct fuse_inode *fi = get_fuse_inode(inode);
225
226 spin_lock(&fi->lock);
227 fi->attr_version = atomic64_inc_return(&fc->attr_version);
228 i_size_write(inode, 0);
229 spin_unlock(&fi->lock);
230 fuse_invalidate_attr(inode);
231 if (fc->writeback_cache)
232 file_update_time(file);
233 }
234
235 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
236 fuse_link_write_file(file);
237 }
238
fuse_open_common(struct inode * inode,struct file * file,bool isdir)239 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
240 {
241 struct fuse_mount *fm = get_fuse_mount(inode);
242 struct fuse_conn *fc = fm->fc;
243 int err;
244 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
245 fc->atomic_o_trunc &&
246 fc->writeback_cache;
247 bool dax_truncate = (file->f_flags & O_TRUNC) &&
248 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
249
250 if (fuse_is_bad(inode))
251 return -EIO;
252
253 err = generic_file_open(inode, file);
254 if (err)
255 return err;
256
257 #ifdef CONFIG_FUSE_BPF
258 {
259 struct fuse_err_ret fer;
260
261 fer = fuse_bpf_backing(inode, struct fuse_open_io,
262 fuse_open_initialize,
263 fuse_open_backing,
264 fuse_open_finalize,
265 inode, file, isdir);
266 if (fer.ret)
267 return PTR_ERR(fer.result);
268 }
269 #endif
270
271 if (is_wb_truncate || dax_truncate)
272 inode_lock(inode);
273
274 if (dax_truncate) {
275 filemap_invalidate_lock(inode->i_mapping);
276 err = fuse_dax_break_layouts(inode, 0, 0);
277 if (err)
278 goto out_inode_unlock;
279 }
280
281 if (is_wb_truncate || dax_truncate)
282 fuse_set_nowrite(inode);
283
284 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
285 if (!err)
286 fuse_finish_open(inode, file);
287
288 if (is_wb_truncate || dax_truncate)
289 fuse_release_nowrite(inode);
290 if (!err) {
291 struct fuse_file *ff = file->private_data;
292
293 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
294 truncate_pagecache(inode, 0);
295 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
296 invalidate_inode_pages2(inode->i_mapping);
297 }
298 if (dax_truncate)
299 filemap_invalidate_unlock(inode->i_mapping);
300 out_inode_unlock:
301 if (is_wb_truncate || dax_truncate)
302 inode_unlock(inode);
303
304 return err;
305 }
306
fuse_prepare_release(struct fuse_inode * fi,struct fuse_file * ff,unsigned int flags,int opcode)307 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
308 unsigned int flags, int opcode)
309 {
310 struct fuse_conn *fc = ff->fm->fc;
311 struct fuse_release_args *ra = ff->release_args;
312
313 /* Inode is NULL on error path of fuse_create_open() */
314 if (likely(fi)) {
315 spin_lock(&fi->lock);
316 list_del(&ff->write_entry);
317 spin_unlock(&fi->lock);
318 }
319 spin_lock(&fc->lock);
320 if (!RB_EMPTY_NODE(&ff->polled_node))
321 rb_erase(&ff->polled_node, &fc->polled_files);
322 spin_unlock(&fc->lock);
323
324 wake_up_interruptible_all(&ff->poll_wait);
325
326 ra->inarg.fh = ff->fh;
327 ra->inarg.flags = flags;
328 ra->args.in_numargs = 1;
329 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
330 ra->args.in_args[0].value = &ra->inarg;
331 ra->args.opcode = opcode;
332 ra->args.nodeid = ff->nodeid;
333 ra->args.force = true;
334 ra->args.nocreds = true;
335 }
336
fuse_file_release(struct inode * inode,struct fuse_file * ff,unsigned int open_flags,fl_owner_t id,bool isdir)337 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
338 unsigned int open_flags, fl_owner_t id, bool isdir)
339 {
340 struct fuse_inode *fi = get_fuse_inode(inode);
341 struct fuse_release_args *ra = ff->release_args;
342 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
343
344 fuse_passthrough_release(&ff->passthrough);
345
346 fuse_prepare_release(fi, ff, open_flags, opcode);
347
348 if (ff->flock) {
349 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
350 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
351 }
352 /* Hold inode until release is finished */
353 ra->inode = igrab(inode);
354
355 /*
356 * Normally this will send the RELEASE request, however if
357 * some asynchronous READ or WRITE requests are outstanding,
358 * the sending will be delayed.
359 *
360 * Make the release synchronous if this is a fuseblk mount,
361 * synchronous RELEASE is allowed (and desirable) in this case
362 * because the server can be trusted not to screw up.
363 */
364 fuse_file_put(ra->inode, ff, ff->fm->fc->destroy, isdir);
365 }
366
fuse_release_common(struct file * file,bool isdir)367 void fuse_release_common(struct file *file, bool isdir)
368 {
369 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
370 (fl_owner_t) file, isdir);
371 }
372
fuse_open(struct inode * inode,struct file * file)373 static int fuse_open(struct inode *inode, struct file *file)
374 {
375 return fuse_open_common(inode, file, false);
376 }
377
fuse_release(struct inode * inode,struct file * file)378 static int fuse_release(struct inode *inode, struct file *file)
379 {
380 struct fuse_conn *fc = get_fuse_conn(inode);
381
382 /* see fuse_vma_close() for !writeback_cache case */
383 if (fc->writeback_cache)
384 write_inode_now(inode, 1);
385
386 fuse_release_common(file, false);
387
388 /* return value is ignored by VFS */
389 return 0;
390 }
391
fuse_sync_release(struct fuse_inode * fi,struct fuse_file * ff,unsigned int flags)392 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
393 unsigned int flags)
394 {
395 WARN_ON(refcount_read(&ff->count) > 1);
396 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
397 /*
398 * iput(NULL) is a no-op and since the refcount is 1 and everything's
399 * synchronous, we are fine with not doing igrab() here"
400 */
401 fuse_file_put(&fi->inode, ff, true, false);
402 }
403 EXPORT_SYMBOL_GPL(fuse_sync_release);
404
405 /*
406 * Scramble the ID space with XTEA, so that the value of the files_struct
407 * pointer is not exposed to userspace.
408 */
fuse_lock_owner_id(struct fuse_conn * fc,fl_owner_t id)409 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
410 {
411 u32 *k = fc->scramble_key;
412 u64 v = (unsigned long) id;
413 u32 v0 = v;
414 u32 v1 = v >> 32;
415 u32 sum = 0;
416 int i;
417
418 for (i = 0; i < 32; i++) {
419 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
420 sum += 0x9E3779B9;
421 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
422 }
423
424 return (u64) v0 + ((u64) v1 << 32);
425 }
426
427 struct fuse_writepage_args {
428 struct fuse_io_args ia;
429 struct rb_node writepages_entry;
430 struct list_head queue_entry;
431 struct fuse_writepage_args *next;
432 struct inode *inode;
433 struct fuse_sync_bucket *bucket;
434 };
435
fuse_find_writeback(struct fuse_inode * fi,pgoff_t idx_from,pgoff_t idx_to)436 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
437 pgoff_t idx_from, pgoff_t idx_to)
438 {
439 struct rb_node *n;
440
441 n = fi->writepages.rb_node;
442
443 while (n) {
444 struct fuse_writepage_args *wpa;
445 pgoff_t curr_index;
446
447 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
448 WARN_ON(get_fuse_inode(wpa->inode) != fi);
449 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
450 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
451 n = n->rb_right;
452 else if (idx_to < curr_index)
453 n = n->rb_left;
454 else
455 return wpa;
456 }
457 return NULL;
458 }
459
460 /*
461 * Check if any page in a range is under writeback
462 *
463 * This is currently done by walking the list of writepage requests
464 * for the inode, which can be pretty inefficient.
465 */
fuse_range_is_writeback(struct inode * inode,pgoff_t idx_from,pgoff_t idx_to)466 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
467 pgoff_t idx_to)
468 {
469 struct fuse_inode *fi = get_fuse_inode(inode);
470 bool found;
471
472 spin_lock(&fi->lock);
473 found = fuse_find_writeback(fi, idx_from, idx_to);
474 spin_unlock(&fi->lock);
475
476 return found;
477 }
478
fuse_page_is_writeback(struct inode * inode,pgoff_t index)479 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
480 {
481 return fuse_range_is_writeback(inode, index, index);
482 }
483
484 /*
485 * Wait for page writeback to be completed.
486 *
487 * Since fuse doesn't rely on the VM writeback tracking, this has to
488 * use some other means.
489 */
fuse_wait_on_page_writeback(struct inode * inode,pgoff_t index)490 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
491 {
492 struct fuse_inode *fi = get_fuse_inode(inode);
493
494 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
495 }
496
497 /*
498 * Wait for all pending writepages on the inode to finish.
499 *
500 * This is currently done by blocking further writes with FUSE_NOWRITE
501 * and waiting for all sent writes to complete.
502 *
503 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
504 * could conflict with truncation.
505 */
fuse_sync_writes(struct inode * inode)506 static void fuse_sync_writes(struct inode *inode)
507 {
508 fuse_set_nowrite(inode);
509 fuse_release_nowrite(inode);
510 }
511
fuse_flush(struct file * file,fl_owner_t id)512 static int fuse_flush(struct file *file, fl_owner_t id)
513 {
514 struct inode *inode = file_inode(file);
515 struct fuse_mount *fm = get_fuse_mount(inode);
516 struct fuse_file *ff = file->private_data;
517 struct fuse_flush_in inarg;
518 FUSE_ARGS(args);
519 int err;
520
521 #ifdef CONFIG_FUSE_BPF
522 struct fuse_err_ret fer;
523
524 fer = fuse_bpf_backing(file->f_inode, struct fuse_flush_in,
525 fuse_flush_initialize, fuse_flush_backing,
526 fuse_flush_finalize,
527 file, id);
528 if (fer.ret)
529 return PTR_ERR(fer.result);
530 #endif
531
532 if (fuse_is_bad(inode))
533 return -EIO;
534
535 err = write_inode_now(inode, 1);
536 if (err)
537 return err;
538
539 inode_lock(inode);
540 fuse_sync_writes(inode);
541 inode_unlock(inode);
542
543 err = filemap_check_errors(file->f_mapping);
544 if (err)
545 return err;
546
547 err = 0;
548 if (fm->fc->no_flush)
549 goto inval_attr_out;
550
551 memset(&inarg, 0, sizeof(inarg));
552 inarg.fh = ff->fh;
553 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
554 args.opcode = FUSE_FLUSH;
555 args.nodeid = get_node_id(inode);
556 args.in_numargs = 1;
557 args.in_args[0].size = sizeof(inarg);
558 args.in_args[0].value = &inarg;
559 args.force = true;
560
561 err = fuse_simple_request(fm, &args);
562 if (err == -ENOSYS) {
563 fm->fc->no_flush = 1;
564 err = 0;
565 }
566
567 inval_attr_out:
568 /*
569 * In memory i_blocks is not maintained by fuse, if writeback cache is
570 * enabled, i_blocks from cached attr may not be accurate.
571 */
572 if (!err && fm->fc->writeback_cache)
573 fuse_invalidate_attr(inode);
574 return err;
575 }
576
fuse_fsync_common(struct file * file,loff_t start,loff_t end,int datasync,int opcode)577 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
578 int datasync, int opcode)
579 {
580 struct inode *inode = file->f_mapping->host;
581 struct fuse_mount *fm = get_fuse_mount(inode);
582 struct fuse_file *ff = file->private_data;
583 FUSE_ARGS(args);
584 struct fuse_fsync_in inarg;
585
586 memset(&inarg, 0, sizeof(inarg));
587 inarg.fh = ff->fh;
588 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
589 args.opcode = opcode;
590 args.nodeid = get_node_id(inode);
591 args.in_numargs = 1;
592 args.in_args[0].size = sizeof(inarg);
593 args.in_args[0].value = &inarg;
594 return fuse_simple_request(fm, &args);
595 }
596
fuse_fsync(struct file * file,loff_t start,loff_t end,int datasync)597 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
598 int datasync)
599 {
600 struct inode *inode = file->f_mapping->host;
601 struct fuse_conn *fc = get_fuse_conn(inode);
602 int err;
603
604 #ifdef CONFIG_FUSE_BPF
605 struct fuse_err_ret fer;
606
607 fer = fuse_bpf_backing(inode, struct fuse_fsync_in,
608 fuse_fsync_initialize, fuse_fsync_backing,
609 fuse_fsync_finalize,
610 file, start, end, datasync);
611 if (fer.ret)
612 return PTR_ERR(fer.result);
613 #endif
614
615 if (fuse_is_bad(inode))
616 return -EIO;
617
618 inode_lock(inode);
619
620 /*
621 * Start writeback against all dirty pages of the inode, then
622 * wait for all outstanding writes, before sending the FSYNC
623 * request.
624 */
625 err = file_write_and_wait_range(file, start, end);
626 if (err)
627 goto out;
628
629 fuse_sync_writes(inode);
630
631 /*
632 * Due to implementation of fuse writeback
633 * file_write_and_wait_range() does not catch errors.
634 * We have to do this directly after fuse_sync_writes()
635 */
636 err = file_check_and_advance_wb_err(file);
637 if (err)
638 goto out;
639
640 err = sync_inode_metadata(inode, 1);
641 if (err)
642 goto out;
643
644 if (fc->no_fsync)
645 goto out;
646
647 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
648 if (err == -ENOSYS) {
649 fc->no_fsync = 1;
650 err = 0;
651 }
652 out:
653 inode_unlock(inode);
654
655 return err;
656 }
657
fuse_read_args_fill(struct fuse_io_args * ia,struct file * file,loff_t pos,size_t count,int opcode)658 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
659 size_t count, int opcode)
660 {
661 struct fuse_file *ff = file->private_data;
662 struct fuse_args *args = &ia->ap.args;
663
664 ia->read.in.fh = ff->fh;
665 ia->read.in.offset = pos;
666 ia->read.in.size = count;
667 ia->read.in.flags = file->f_flags;
668 args->opcode = opcode;
669 args->nodeid = ff->nodeid;
670 args->in_numargs = 1;
671 args->in_args[0].size = sizeof(ia->read.in);
672 args->in_args[0].value = &ia->read.in;
673 args->out_argvar = true;
674 args->out_numargs = 1;
675 args->out_args[0].size = count;
676 }
677
fuse_release_user_pages(struct fuse_args_pages * ap,bool should_dirty)678 static void fuse_release_user_pages(struct fuse_args_pages *ap,
679 bool should_dirty)
680 {
681 unsigned int i;
682
683 for (i = 0; i < ap->num_pages; i++) {
684 if (should_dirty)
685 set_page_dirty_lock(ap->pages[i]);
686 put_page(ap->pages[i]);
687 }
688 }
689
fuse_io_release(struct kref * kref)690 static void fuse_io_release(struct kref *kref)
691 {
692 kfree(container_of(kref, struct fuse_io_priv, refcnt));
693 }
694
fuse_get_res_by_io(struct fuse_io_priv * io)695 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
696 {
697 if (io->err)
698 return io->err;
699
700 if (io->bytes >= 0 && io->write)
701 return -EIO;
702
703 return io->bytes < 0 ? io->size : io->bytes;
704 }
705
706 /**
707 * In case of short read, the caller sets 'pos' to the position of
708 * actual end of fuse request in IO request. Otherwise, if bytes_requested
709 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
710 *
711 * An example:
712 * User requested DIO read of 64K. It was split into two 32K fuse requests,
713 * both submitted asynchronously. The first of them was ACKed by userspace as
714 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
715 * second request was ACKed as short, e.g. only 1K was read, resulting in
716 * pos == 33K.
717 *
718 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
719 * will be equal to the length of the longest contiguous fragment of
720 * transferred data starting from the beginning of IO request.
721 */
fuse_aio_complete(struct fuse_io_priv * io,int err,ssize_t pos)722 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
723 {
724 int left;
725
726 spin_lock(&io->lock);
727 if (err)
728 io->err = io->err ? : err;
729 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
730 io->bytes = pos;
731
732 left = --io->reqs;
733 if (!left && io->blocking)
734 complete(io->done);
735 spin_unlock(&io->lock);
736
737 if (!left && !io->blocking) {
738 ssize_t res = fuse_get_res_by_io(io);
739
740 if (res >= 0) {
741 struct inode *inode = file_inode(io->iocb->ki_filp);
742 struct fuse_conn *fc = get_fuse_conn(inode);
743 struct fuse_inode *fi = get_fuse_inode(inode);
744
745 spin_lock(&fi->lock);
746 fi->attr_version = atomic64_inc_return(&fc->attr_version);
747 spin_unlock(&fi->lock);
748 }
749
750 io->iocb->ki_complete(io->iocb, res, 0);
751 }
752
753 kref_put(&io->refcnt, fuse_io_release);
754 }
755
fuse_io_alloc(struct fuse_io_priv * io,unsigned int npages)756 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
757 unsigned int npages)
758 {
759 struct fuse_io_args *ia;
760
761 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
762 if (ia) {
763 ia->io = io;
764 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
765 &ia->ap.descs);
766 if (!ia->ap.pages) {
767 kfree(ia);
768 ia = NULL;
769 }
770 }
771 return ia;
772 }
773
fuse_io_free(struct fuse_io_args * ia)774 static void fuse_io_free(struct fuse_io_args *ia)
775 {
776 kfree(ia->ap.pages);
777 kfree(ia);
778 }
779
fuse_aio_complete_req(struct fuse_mount * fm,struct fuse_args * args,int err)780 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
781 int err)
782 {
783 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
784 struct fuse_io_priv *io = ia->io;
785 ssize_t pos = -1;
786
787 fuse_release_user_pages(&ia->ap, io->should_dirty);
788
789 if (err) {
790 /* Nothing */
791 } else if (io->write) {
792 if (ia->write.out.size > ia->write.in.size) {
793 err = -EIO;
794 } else if (ia->write.in.size != ia->write.out.size) {
795 pos = ia->write.in.offset - io->offset +
796 ia->write.out.size;
797 }
798 } else {
799 u32 outsize = args->out_args[0].size;
800
801 if (ia->read.in.size != outsize)
802 pos = ia->read.in.offset - io->offset + outsize;
803 }
804
805 fuse_aio_complete(io, err, pos);
806 fuse_io_free(ia);
807 }
808
fuse_async_req_send(struct fuse_mount * fm,struct fuse_io_args * ia,size_t num_bytes)809 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
810 struct fuse_io_args *ia, size_t num_bytes)
811 {
812 ssize_t err;
813 struct fuse_io_priv *io = ia->io;
814
815 spin_lock(&io->lock);
816 kref_get(&io->refcnt);
817 io->size += num_bytes;
818 io->reqs++;
819 spin_unlock(&io->lock);
820
821 ia->ap.args.end = fuse_aio_complete_req;
822 ia->ap.args.may_block = io->should_dirty;
823 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
824 if (err)
825 fuse_aio_complete_req(fm, &ia->ap.args, err);
826
827 return num_bytes;
828 }
829
fuse_send_read(struct fuse_io_args * ia,loff_t pos,size_t count,fl_owner_t owner)830 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
831 fl_owner_t owner)
832 {
833 struct file *file = ia->io->iocb->ki_filp;
834 struct fuse_file *ff = file->private_data;
835 struct fuse_mount *fm = ff->fm;
836
837 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
838 if (owner != NULL) {
839 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
840 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
841 }
842
843 if (ia->io->async)
844 return fuse_async_req_send(fm, ia, count);
845
846 return fuse_simple_request(fm, &ia->ap.args);
847 }
848
fuse_read_update_size(struct inode * inode,loff_t size,u64 attr_ver)849 static void fuse_read_update_size(struct inode *inode, loff_t size,
850 u64 attr_ver)
851 {
852 struct fuse_conn *fc = get_fuse_conn(inode);
853 struct fuse_inode *fi = get_fuse_inode(inode);
854
855 spin_lock(&fi->lock);
856 if (attr_ver >= fi->attr_version && size < inode->i_size &&
857 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
858 fi->attr_version = atomic64_inc_return(&fc->attr_version);
859 i_size_write(inode, size);
860 }
861 spin_unlock(&fi->lock);
862 }
863
fuse_short_read(struct inode * inode,u64 attr_ver,size_t num_read,struct fuse_args_pages * ap)864 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
865 struct fuse_args_pages *ap)
866 {
867 struct fuse_conn *fc = get_fuse_conn(inode);
868
869 /*
870 * If writeback_cache is enabled, a short read means there's a hole in
871 * the file. Some data after the hole is in page cache, but has not
872 * reached the client fs yet. So the hole is not present there.
873 */
874 if (!fc->writeback_cache) {
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 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1183 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1184
1185 err = fuse_simple_request(fm, &ap->args);
1186 if (!err && ia->write.out.size > count)
1187 err = -EIO;
1188
1189 short_write = ia->write.out.size < count;
1190 offset = ap->descs[0].offset;
1191 count = ia->write.out.size;
1192 for (i = 0; i < ap->num_pages; i++) {
1193 struct page *page = ap->pages[i];
1194
1195 if (err) {
1196 ClearPageUptodate(page);
1197 } else {
1198 if (count >= PAGE_SIZE - offset)
1199 count -= PAGE_SIZE - offset;
1200 else {
1201 if (short_write)
1202 ClearPageUptodate(page);
1203 count = 0;
1204 }
1205 offset = 0;
1206 }
1207 if (ia->write.page_locked && (i == ap->num_pages - 1))
1208 unlock_page(page);
1209 put_page(page);
1210 }
1211
1212 return err;
1213 }
1214
fuse_fill_write_pages(struct fuse_io_args * ia,struct address_space * mapping,struct iov_iter * ii,loff_t pos,unsigned int max_pages)1215 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
1216 struct address_space *mapping,
1217 struct iov_iter *ii, loff_t pos,
1218 unsigned int max_pages)
1219 {
1220 struct fuse_args_pages *ap = &ia->ap;
1221 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1222 unsigned offset = pos & (PAGE_SIZE - 1);
1223 size_t count = 0;
1224 int err;
1225
1226 ap->args.in_pages = true;
1227 ap->descs[0].offset = offset;
1228
1229 do {
1230 size_t tmp;
1231 struct page *page;
1232 pgoff_t index = pos >> PAGE_SHIFT;
1233 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1234 iov_iter_count(ii));
1235
1236 bytes = min_t(size_t, bytes, fc->max_write - count);
1237
1238 again:
1239 err = -EFAULT;
1240 if (fault_in_iov_iter_readable(ii, bytes))
1241 break;
1242
1243 err = -ENOMEM;
1244 page = grab_cache_page_write_begin(mapping, index, 0);
1245 if (!page)
1246 break;
1247
1248 if (mapping_writably_mapped(mapping))
1249 flush_dcache_page(page);
1250
1251 tmp = copy_page_from_iter_atomic(page, offset, bytes, ii);
1252 flush_dcache_page(page);
1253
1254 if (!tmp) {
1255 unlock_page(page);
1256 put_page(page);
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 struct fuse_conn *fc = get_fuse_conn(inode);
1362 loff_t endbyte = 0;
1363
1364 if (fc->writeback_cache) {
1365 /* Update size (EOF optimization) and mode (SUID clearing) */
1366 err = fuse_update_attributes(mapping->host, file);
1367 if (err)
1368 return err;
1369
1370 if (fc->handle_killpriv_v2 &&
1371 setattr_should_drop_suidgid(&init_user_ns, file_inode(file))) {
1372 goto writethrough;
1373 }
1374
1375 return generic_file_write_iter(iocb, from);
1376 }
1377
1378 writethrough:
1379 inode_lock(inode);
1380
1381 /* We can write back this queue in page reclaim */
1382 current->backing_dev_info = inode_to_bdi(inode);
1383
1384 err = generic_write_checks(iocb, from);
1385 if (err <= 0)
1386 goto out;
1387
1388 err = file_remove_privs(file);
1389 if (err)
1390 goto out;
1391
1392 err = file_update_time(file);
1393 if (err)
1394 goto out;
1395
1396 if (iocb->ki_flags & IOCB_DIRECT) {
1397 loff_t pos = iocb->ki_pos;
1398 written = generic_file_direct_write(iocb, from);
1399 if (written < 0 || !iov_iter_count(from))
1400 goto out;
1401
1402 pos += written;
1403
1404 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1405 if (written_buffered < 0) {
1406 err = written_buffered;
1407 goto out;
1408 }
1409 endbyte = pos + written_buffered - 1;
1410
1411 err = filemap_write_and_wait_range(file->f_mapping, pos,
1412 endbyte);
1413 if (err)
1414 goto out;
1415
1416 invalidate_mapping_pages(file->f_mapping,
1417 pos >> PAGE_SHIFT,
1418 endbyte >> PAGE_SHIFT);
1419
1420 written += written_buffered;
1421 iocb->ki_pos = pos + written_buffered;
1422 } else {
1423 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1424 if (written >= 0)
1425 iocb->ki_pos += written;
1426 }
1427 out:
1428 current->backing_dev_info = NULL;
1429 inode_unlock(inode);
1430 if (written > 0)
1431 written = generic_write_sync(iocb, written);
1432
1433 return written ? written : err;
1434 }
1435
fuse_get_user_addr(const struct iov_iter * ii)1436 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1437 {
1438 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1439 }
1440
fuse_get_frag_size(const struct iov_iter * ii,size_t max_size)1441 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1442 size_t max_size)
1443 {
1444 return min(iov_iter_single_seg_count(ii), max_size);
1445 }
1446
fuse_get_user_pages(struct fuse_args_pages * ap,struct iov_iter * ii,size_t * nbytesp,int write,unsigned int max_pages)1447 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1448 size_t *nbytesp, int write,
1449 unsigned int max_pages)
1450 {
1451 size_t nbytes = 0; /* # bytes already packed in req */
1452 ssize_t ret = 0;
1453
1454 /* Special case for kernel I/O: can copy directly into the buffer */
1455 if (iov_iter_is_kvec(ii)) {
1456 unsigned long user_addr = fuse_get_user_addr(ii);
1457 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1458
1459 if (write)
1460 ap->args.in_args[1].value = (void *) user_addr;
1461 else
1462 ap->args.out_args[0].value = (void *) user_addr;
1463
1464 iov_iter_advance(ii, frag_size);
1465 *nbytesp = frag_size;
1466 return 0;
1467 }
1468
1469 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1470 unsigned npages;
1471 size_t start;
1472 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1473 *nbytesp - nbytes,
1474 max_pages - ap->num_pages,
1475 &start);
1476 if (ret < 0)
1477 break;
1478
1479 iov_iter_advance(ii, ret);
1480 nbytes += ret;
1481
1482 ret += start;
1483 npages = DIV_ROUND_UP(ret, PAGE_SIZE);
1484
1485 ap->descs[ap->num_pages].offset = start;
1486 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1487
1488 ap->num_pages += npages;
1489 ap->descs[ap->num_pages - 1].length -=
1490 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1491 }
1492
1493 ap->args.user_pages = true;
1494 if (write)
1495 ap->args.in_pages = true;
1496 else
1497 ap->args.out_pages = true;
1498
1499 *nbytesp = nbytes;
1500
1501 return ret < 0 ? ret : 0;
1502 }
1503
fuse_direct_io(struct fuse_io_priv * io,struct iov_iter * iter,loff_t * ppos,int flags)1504 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1505 loff_t *ppos, int flags)
1506 {
1507 int write = flags & FUSE_DIO_WRITE;
1508 int cuse = flags & FUSE_DIO_CUSE;
1509 struct file *file = io->iocb->ki_filp;
1510 struct inode *inode = file->f_mapping->host;
1511 struct fuse_file *ff = file->private_data;
1512 struct fuse_conn *fc = ff->fm->fc;
1513 size_t nmax = write ? fc->max_write : fc->max_read;
1514 loff_t pos = *ppos;
1515 size_t count = iov_iter_count(iter);
1516 pgoff_t idx_from = pos >> PAGE_SHIFT;
1517 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1518 ssize_t res = 0;
1519 int err = 0;
1520 struct fuse_io_args *ia;
1521 unsigned int max_pages;
1522
1523 max_pages = iov_iter_npages(iter, fc->max_pages);
1524 ia = fuse_io_alloc(io, max_pages);
1525 if (!ia)
1526 return -ENOMEM;
1527
1528 ia->io = io;
1529 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1530 if (!write)
1531 inode_lock(inode);
1532 fuse_sync_writes(inode);
1533 if (!write)
1534 inode_unlock(inode);
1535 }
1536
1537 io->should_dirty = !write && iter_is_iovec(iter);
1538 while (count) {
1539 ssize_t nres;
1540 fl_owner_t owner = current->files;
1541 size_t nbytes = min(count, nmax);
1542
1543 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1544 max_pages);
1545 if (err && !nbytes)
1546 break;
1547
1548 if (write) {
1549 if (!capable(CAP_FSETID))
1550 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1551
1552 nres = fuse_send_write(ia, pos, nbytes, owner);
1553 } else {
1554 nres = fuse_send_read(ia, pos, nbytes, owner);
1555 }
1556
1557 if (!io->async || nres < 0) {
1558 fuse_release_user_pages(&ia->ap, io->should_dirty);
1559 fuse_io_free(ia);
1560 }
1561 ia = NULL;
1562 if (nres < 0) {
1563 iov_iter_revert(iter, nbytes);
1564 err = nres;
1565 break;
1566 }
1567 WARN_ON(nres > nbytes);
1568
1569 count -= nres;
1570 res += nres;
1571 pos += nres;
1572 if (nres != nbytes) {
1573 iov_iter_revert(iter, nbytes - nres);
1574 break;
1575 }
1576 if (count) {
1577 max_pages = iov_iter_npages(iter, fc->max_pages);
1578 ia = fuse_io_alloc(io, max_pages);
1579 if (!ia)
1580 break;
1581 }
1582 }
1583 if (ia)
1584 fuse_io_free(ia);
1585 if (res > 0)
1586 *ppos = pos;
1587
1588 return res > 0 ? res : err;
1589 }
1590 EXPORT_SYMBOL_GPL(fuse_direct_io);
1591
__fuse_direct_read(struct fuse_io_priv * io,struct iov_iter * iter,loff_t * ppos)1592 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1593 struct iov_iter *iter,
1594 loff_t *ppos)
1595 {
1596 ssize_t res;
1597 struct inode *inode = file_inode(io->iocb->ki_filp);
1598
1599 res = fuse_direct_io(io, iter, ppos, 0);
1600
1601 fuse_invalidate_atime(inode);
1602
1603 return res;
1604 }
1605
1606 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1607
fuse_direct_read_iter(struct kiocb * iocb,struct iov_iter * to)1608 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1609 {
1610 ssize_t res;
1611
1612 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1613 res = fuse_direct_IO(iocb, to);
1614 } else {
1615 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1616
1617 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1618 }
1619
1620 return res;
1621 }
1622
fuse_direct_write_iter(struct kiocb * iocb,struct iov_iter * from)1623 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1624 {
1625 struct inode *inode = file_inode(iocb->ki_filp);
1626 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1627 ssize_t res;
1628
1629 /* Don't allow parallel writes to the same file */
1630 inode_lock(inode);
1631 res = generic_write_checks(iocb, from);
1632 if (res > 0) {
1633 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1634 res = fuse_direct_IO(iocb, from);
1635 } else {
1636 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1637 FUSE_DIO_WRITE);
1638 }
1639 }
1640 fuse_invalidate_attr(inode);
1641 if (res > 0)
1642 fuse_write_update_size(inode, iocb->ki_pos);
1643 inode_unlock(inode);
1644
1645 return res;
1646 }
1647
fuse_file_read_iter(struct kiocb * iocb,struct iov_iter * to)1648 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1649 {
1650 struct file *file = iocb->ki_filp;
1651 struct fuse_file *ff = file->private_data;
1652 struct inode *inode = file_inode(file);
1653
1654 if (fuse_is_bad(inode))
1655 return -EIO;
1656
1657 if (FUSE_IS_DAX(inode))
1658 return fuse_dax_read_iter(iocb, to);
1659
1660 #ifdef CONFIG_FUSE_BPF
1661 {
1662 struct fuse_err_ret fer;
1663
1664 fer = fuse_bpf_backing(inode, struct fuse_file_read_iter_io,
1665 fuse_file_read_iter_initialize,
1666 fuse_file_read_iter_backing,
1667 fuse_file_read_iter_finalize,
1668 iocb, to);
1669 if (fer.ret)
1670 return PTR_ERR(fer.result);
1671 }
1672 #endif
1673
1674 if (ff->passthrough.filp)
1675 return fuse_passthrough_read_iter(iocb, to);
1676 else if (!(ff->open_flags & FOPEN_DIRECT_IO))
1677 return fuse_cache_read_iter(iocb, to);
1678 else
1679 return fuse_direct_read_iter(iocb, to);
1680 }
1681
fuse_file_write_iter(struct kiocb * iocb,struct iov_iter * from)1682 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1683 {
1684 struct file *file = iocb->ki_filp;
1685 struct fuse_file *ff = file->private_data;
1686 struct inode *inode = file_inode(file);
1687
1688 if (fuse_is_bad(inode))
1689 return -EIO;
1690
1691 if (FUSE_IS_DAX(inode))
1692 return fuse_dax_write_iter(iocb, from);
1693
1694 #ifdef CONFIG_FUSE_BPF
1695 {
1696 struct fuse_err_ret fer;
1697
1698 fer = fuse_bpf_backing(inode, struct fuse_file_write_iter_io,
1699 fuse_file_write_iter_initialize,
1700 fuse_file_write_iter_backing,
1701 fuse_file_write_iter_finalize,
1702 iocb, from);
1703 if (fer.ret)
1704 return PTR_ERR(fer.result);
1705 }
1706 #endif
1707
1708 if (ff->passthrough.filp)
1709 return fuse_passthrough_write_iter(iocb, from);
1710 else if (!(ff->open_flags & FOPEN_DIRECT_IO))
1711 return fuse_cache_write_iter(iocb, from);
1712 else
1713 return fuse_direct_write_iter(iocb, from);
1714 }
1715
fuse_writepage_free(struct fuse_writepage_args * wpa)1716 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1717 {
1718 struct fuse_args_pages *ap = &wpa->ia.ap;
1719 int i;
1720
1721 if (wpa->bucket)
1722 fuse_sync_bucket_dec(wpa->bucket);
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_inode * fi)1929 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
1930 {
1931 struct fuse_file *ff = NULL;
1932
1933 spin_lock(&fi->lock);
1934 if (!list_empty(&fi->write_files)) {
1935 ff = list_entry(fi->write_files.next, struct fuse_file,
1936 write_entry);
1937 fuse_file_get(ff);
1938 }
1939 spin_unlock(&fi->lock);
1940
1941 return ff;
1942 }
1943
fuse_write_file_get(struct fuse_inode * fi)1944 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
1945 {
1946 struct fuse_file *ff = __fuse_write_file_get(fi);
1947 WARN_ON(!ff);
1948 return ff;
1949 }
1950
fuse_write_inode(struct inode * inode,struct writeback_control * wbc)1951 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1952 {
1953 struct fuse_inode *fi = get_fuse_inode(inode);
1954 struct fuse_file *ff;
1955 int err;
1956
1957 /**
1958 * TODO - fully understand why this is necessary
1959 *
1960 * With fuse-bpf, fsstress fails if rename is enabled without this
1961 *
1962 * We are getting writes here on directory inodes, which do not have an
1963 * initialized file list so crash.
1964 *
1965 * The question is why we are getting those writes
1966 */
1967 if (!S_ISREG(inode->i_mode))
1968 return 0;
1969
1970 /*
1971 * Inode is always written before the last reference is dropped and
1972 * hence this should not be reached from reclaim.
1973 *
1974 * Writing back the inode from reclaim can deadlock if the request
1975 * processing itself needs an allocation. Allocations triggering
1976 * reclaim while serving a request can't be prevented, because it can
1977 * involve any number of unrelated userspace processes.
1978 */
1979 WARN_ON(wbc->for_reclaim);
1980
1981 ff = __fuse_write_file_get(fi);
1982 err = fuse_flush_times(inode, ff);
1983 if (ff)
1984 fuse_file_put(inode, ff, false, false);
1985
1986 return err;
1987 }
1988
fuse_writepage_args_alloc(void)1989 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1990 {
1991 struct fuse_writepage_args *wpa;
1992 struct fuse_args_pages *ap;
1993
1994 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1995 if (wpa) {
1996 ap = &wpa->ia.ap;
1997 ap->num_pages = 0;
1998 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1999 if (!ap->pages) {
2000 kfree(wpa);
2001 wpa = NULL;
2002 }
2003 }
2004 return wpa;
2005
2006 }
2007
fuse_writepage_add_to_bucket(struct fuse_conn * fc,struct fuse_writepage_args * wpa)2008 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
2009 struct fuse_writepage_args *wpa)
2010 {
2011 if (!fc->sync_fs)
2012 return;
2013
2014 rcu_read_lock();
2015 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
2016 do {
2017 wpa->bucket = rcu_dereference(fc->curr_bucket);
2018 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
2019 rcu_read_unlock();
2020 }
2021
fuse_writepage_locked(struct page * page)2022 static int fuse_writepage_locked(struct page *page)
2023 {
2024 struct address_space *mapping = page->mapping;
2025 struct inode *inode = mapping->host;
2026 struct fuse_conn *fc = get_fuse_conn(inode);
2027 struct fuse_inode *fi = get_fuse_inode(inode);
2028 struct fuse_writepage_args *wpa;
2029 struct fuse_args_pages *ap;
2030 struct page *tmp_page;
2031 int error = -ENOMEM;
2032
2033 set_page_writeback(page);
2034
2035 wpa = fuse_writepage_args_alloc();
2036 if (!wpa)
2037 goto err;
2038 ap = &wpa->ia.ap;
2039
2040 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2041 if (!tmp_page)
2042 goto err_free;
2043
2044 error = -EIO;
2045 wpa->ia.ff = fuse_write_file_get(fi);
2046 if (!wpa->ia.ff)
2047 goto err_nofile;
2048
2049 fuse_writepage_add_to_bucket(fc, wpa);
2050 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
2051
2052 copy_highpage(tmp_page, page);
2053 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2054 wpa->next = NULL;
2055 ap->args.in_pages = true;
2056 ap->num_pages = 1;
2057 ap->pages[0] = tmp_page;
2058 ap->descs[0].offset = 0;
2059 ap->descs[0].length = PAGE_SIZE;
2060 ap->args.end = fuse_writepage_end;
2061 wpa->inode = inode;
2062
2063 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2064 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2065
2066 spin_lock(&fi->lock);
2067 tree_insert(&fi->writepages, wpa);
2068 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2069 fuse_flush_writepages(inode);
2070 spin_unlock(&fi->lock);
2071
2072 end_page_writeback(page);
2073
2074 return 0;
2075
2076 err_nofile:
2077 __free_page(tmp_page);
2078 err_free:
2079 kfree(wpa);
2080 err:
2081 mapping_set_error(page->mapping, error);
2082 end_page_writeback(page);
2083 return error;
2084 }
2085
fuse_writepage(struct page * page,struct writeback_control * wbc)2086 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
2087 {
2088 int err;
2089
2090 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
2091 /*
2092 * ->writepages() should be called for sync() and friends. We
2093 * should only get here on direct reclaim and then we are
2094 * allowed to skip a page which is already in flight
2095 */
2096 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
2097
2098 redirty_page_for_writepage(wbc, page);
2099 unlock_page(page);
2100 return 0;
2101 }
2102
2103 err = fuse_writepage_locked(page);
2104 unlock_page(page);
2105
2106 return err;
2107 }
2108
2109 struct fuse_fill_wb_data {
2110 struct fuse_writepage_args *wpa;
2111 struct fuse_file *ff;
2112 struct inode *inode;
2113 struct page **orig_pages;
2114 unsigned int max_pages;
2115 };
2116
fuse_pages_realloc(struct fuse_fill_wb_data * data)2117 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
2118 {
2119 struct fuse_args_pages *ap = &data->wpa->ia.ap;
2120 struct fuse_conn *fc = get_fuse_conn(data->inode);
2121 struct page **pages;
2122 struct fuse_page_desc *descs;
2123 unsigned int npages = min_t(unsigned int,
2124 max_t(unsigned int, data->max_pages * 2,
2125 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
2126 fc->max_pages);
2127 WARN_ON(npages <= data->max_pages);
2128
2129 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
2130 if (!pages)
2131 return false;
2132
2133 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
2134 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
2135 kfree(ap->pages);
2136 ap->pages = pages;
2137 ap->descs = descs;
2138 data->max_pages = npages;
2139
2140 return true;
2141 }
2142
fuse_writepages_send(struct fuse_fill_wb_data * data)2143 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2144 {
2145 struct fuse_writepage_args *wpa = data->wpa;
2146 struct inode *inode = data->inode;
2147 struct fuse_inode *fi = get_fuse_inode(inode);
2148 int num_pages = wpa->ia.ap.num_pages;
2149 int i;
2150
2151 wpa->ia.ff = fuse_file_get(data->ff);
2152 spin_lock(&fi->lock);
2153 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2154 fuse_flush_writepages(inode);
2155 spin_unlock(&fi->lock);
2156
2157 for (i = 0; i < num_pages; i++)
2158 end_page_writeback(data->orig_pages[i]);
2159 }
2160
2161 /*
2162 * Check under fi->lock if the page is under writeback, and insert it onto the
2163 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2164 * one already added for a page at this offset. If there's none, then insert
2165 * this new request onto the auxiliary list, otherwise reuse the existing one by
2166 * swapping the new temp page with the old one.
2167 */
fuse_writepage_add(struct fuse_writepage_args * new_wpa,struct page * page)2168 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2169 struct page *page)
2170 {
2171 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2172 struct fuse_writepage_args *tmp;
2173 struct fuse_writepage_args *old_wpa;
2174 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
2175
2176 WARN_ON(new_ap->num_pages != 0);
2177 new_ap->num_pages = 1;
2178
2179 spin_lock(&fi->lock);
2180 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
2181 if (!old_wpa) {
2182 spin_unlock(&fi->lock);
2183 return true;
2184 }
2185
2186 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2187 pgoff_t curr_index;
2188
2189 WARN_ON(tmp->inode != new_wpa->inode);
2190 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2191 if (curr_index == page->index) {
2192 WARN_ON(tmp->ia.ap.num_pages != 1);
2193 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2194 break;
2195 }
2196 }
2197
2198 if (!tmp) {
2199 new_wpa->next = old_wpa->next;
2200 old_wpa->next = new_wpa;
2201 }
2202
2203 spin_unlock(&fi->lock);
2204
2205 if (tmp) {
2206 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2207
2208 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2209 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2210 wb_writeout_inc(&bdi->wb);
2211 fuse_writepage_free(new_wpa);
2212 }
2213
2214 return false;
2215 }
2216
fuse_writepage_need_send(struct fuse_conn * fc,struct page * page,struct fuse_args_pages * ap,struct fuse_fill_wb_data * data)2217 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2218 struct fuse_args_pages *ap,
2219 struct fuse_fill_wb_data *data)
2220 {
2221 WARN_ON(!ap->num_pages);
2222
2223 /*
2224 * Being under writeback is unlikely but possible. For example direct
2225 * read to an mmaped fuse file will set the page dirty twice; once when
2226 * the pages are faulted with get_user_pages(), and then after the read
2227 * completed.
2228 */
2229 if (fuse_page_is_writeback(data->inode, page->index))
2230 return true;
2231
2232 /* Reached max pages */
2233 if (ap->num_pages == fc->max_pages)
2234 return true;
2235
2236 /* Reached max write bytes */
2237 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2238 return true;
2239
2240 /* Discontinuity */
2241 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2242 return true;
2243
2244 /* Need to grow the pages array? If so, did the expansion fail? */
2245 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2246 return true;
2247
2248 return false;
2249 }
2250
fuse_writepages_fill(struct page * page,struct writeback_control * wbc,void * _data)2251 static int fuse_writepages_fill(struct page *page,
2252 struct writeback_control *wbc, void *_data)
2253 {
2254 struct fuse_fill_wb_data *data = _data;
2255 struct fuse_writepage_args *wpa = data->wpa;
2256 struct fuse_args_pages *ap = &wpa->ia.ap;
2257 struct inode *inode = data->inode;
2258 struct fuse_inode *fi = get_fuse_inode(inode);
2259 struct fuse_conn *fc = get_fuse_conn(inode);
2260 struct page *tmp_page;
2261 int err;
2262
2263 if (!data->ff) {
2264 err = -EIO;
2265 data->ff = fuse_write_file_get(fi);
2266 if (!data->ff)
2267 goto out_unlock;
2268 }
2269
2270 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
2271 fuse_writepages_send(data);
2272 data->wpa = NULL;
2273 }
2274
2275 err = -ENOMEM;
2276 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2277 if (!tmp_page)
2278 goto out_unlock;
2279
2280 /*
2281 * The page must not be redirtied until the writeout is completed
2282 * (i.e. userspace has sent a reply to the write request). Otherwise
2283 * there could be more than one temporary page instance for each real
2284 * page.
2285 *
2286 * This is ensured by holding the page lock in page_mkwrite() while
2287 * checking fuse_page_is_writeback(). We already hold the page lock
2288 * since clear_page_dirty_for_io() and keep it held until we add the
2289 * request to the fi->writepages list and increment ap->num_pages.
2290 * After this fuse_page_is_writeback() will indicate that the page is
2291 * under writeback, so we can release the page lock.
2292 */
2293 if (data->wpa == NULL) {
2294 err = -ENOMEM;
2295 wpa = fuse_writepage_args_alloc();
2296 if (!wpa) {
2297 __free_page(tmp_page);
2298 goto out_unlock;
2299 }
2300 fuse_writepage_add_to_bucket(fc, wpa);
2301
2302 data->max_pages = 1;
2303
2304 ap = &wpa->ia.ap;
2305 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2306 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2307 wpa->next = NULL;
2308 ap->args.in_pages = true;
2309 ap->args.end = fuse_writepage_end;
2310 ap->num_pages = 0;
2311 wpa->inode = inode;
2312 }
2313 set_page_writeback(page);
2314
2315 copy_highpage(tmp_page, page);
2316 ap->pages[ap->num_pages] = tmp_page;
2317 ap->descs[ap->num_pages].offset = 0;
2318 ap->descs[ap->num_pages].length = PAGE_SIZE;
2319 data->orig_pages[ap->num_pages] = page;
2320
2321 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2322 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2323
2324 err = 0;
2325 if (data->wpa) {
2326 /*
2327 * Protected by fi->lock against concurrent access by
2328 * fuse_page_is_writeback().
2329 */
2330 spin_lock(&fi->lock);
2331 ap->num_pages++;
2332 spin_unlock(&fi->lock);
2333 } else if (fuse_writepage_add(wpa, page)) {
2334 data->wpa = wpa;
2335 } else {
2336 end_page_writeback(page);
2337 }
2338 out_unlock:
2339 unlock_page(page);
2340
2341 return err;
2342 }
2343
fuse_writepages(struct address_space * mapping,struct writeback_control * wbc)2344 static int fuse_writepages(struct address_space *mapping,
2345 struct writeback_control *wbc)
2346 {
2347 struct inode *inode = mapping->host;
2348 struct fuse_conn *fc = get_fuse_conn(inode);
2349 struct fuse_fill_wb_data data;
2350 int err;
2351
2352 err = -EIO;
2353 if (fuse_is_bad(inode))
2354 goto out;
2355
2356 data.inode = inode;
2357 data.wpa = NULL;
2358 data.ff = NULL;
2359
2360 err = -ENOMEM;
2361 data.orig_pages = kcalloc(fc->max_pages,
2362 sizeof(struct page *),
2363 GFP_NOFS);
2364 if (!data.orig_pages)
2365 goto out;
2366
2367 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2368 if (data.wpa) {
2369 WARN_ON(!data.wpa->ia.ap.num_pages);
2370 fuse_writepages_send(&data);
2371 }
2372 if (data.ff)
2373 fuse_file_put(inode, data.ff, false, false);
2374
2375 kfree(data.orig_pages);
2376 out:
2377 return err;
2378 }
2379
2380 /*
2381 * It's worthy to make sure that space is reserved on disk for the write,
2382 * but how to implement it without killing performance need more thinking.
2383 */
fuse_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)2384 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2385 loff_t pos, unsigned len, unsigned flags,
2386 struct page **pagep, void **fsdata)
2387 {
2388 pgoff_t index = pos >> PAGE_SHIFT;
2389 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2390 struct page *page;
2391 loff_t fsize;
2392 int err = -ENOMEM;
2393
2394 WARN_ON(!fc->writeback_cache);
2395
2396 page = grab_cache_page_write_begin(mapping, index, flags);
2397 if (!page)
2398 goto error;
2399
2400 fuse_wait_on_page_writeback(mapping->host, page->index);
2401
2402 if (PageUptodate(page) || len == PAGE_SIZE)
2403 goto success;
2404 /*
2405 * Check if the start this page comes after the end of file, in which
2406 * case the readpage can be optimized away.
2407 */
2408 fsize = i_size_read(mapping->host);
2409 if (fsize <= (pos & PAGE_MASK)) {
2410 size_t off = pos & ~PAGE_MASK;
2411 if (off)
2412 zero_user_segment(page, 0, off);
2413 goto success;
2414 }
2415 err = fuse_do_readpage(file, page);
2416 if (err)
2417 goto cleanup;
2418 success:
2419 *pagep = page;
2420 return 0;
2421
2422 cleanup:
2423 unlock_page(page);
2424 put_page(page);
2425 error:
2426 return err;
2427 }
2428
fuse_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)2429 static int fuse_write_end(struct file *file, struct address_space *mapping,
2430 loff_t pos, unsigned len, unsigned copied,
2431 struct page *page, void *fsdata)
2432 {
2433 struct inode *inode = page->mapping->host;
2434
2435 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2436 if (!copied)
2437 goto unlock;
2438
2439 if (!PageUptodate(page)) {
2440 /* Zero any unwritten bytes at the end of the page */
2441 size_t endoff = (pos + copied) & ~PAGE_MASK;
2442 if (endoff)
2443 zero_user_segment(page, endoff, PAGE_SIZE);
2444 SetPageUptodate(page);
2445 }
2446
2447 fuse_write_update_size(inode, pos + copied);
2448 set_page_dirty(page);
2449
2450 unlock:
2451 unlock_page(page);
2452 put_page(page);
2453
2454 return copied;
2455 }
2456
fuse_launder_page(struct page * page)2457 static int fuse_launder_page(struct page *page)
2458 {
2459 int err = 0;
2460 if (clear_page_dirty_for_io(page)) {
2461 struct inode *inode = page->mapping->host;
2462
2463 /* Serialize with pending writeback for the same page */
2464 fuse_wait_on_page_writeback(inode, page->index);
2465 err = fuse_writepage_locked(page);
2466 if (!err)
2467 fuse_wait_on_page_writeback(inode, page->index);
2468 }
2469 return err;
2470 }
2471
2472 /*
2473 * Write back dirty pages now, because there may not be any suitable
2474 * open files later
2475 */
fuse_vma_close(struct vm_area_struct * vma)2476 static void fuse_vma_close(struct vm_area_struct *vma)
2477 {
2478 filemap_write_and_wait(vma->vm_file->f_mapping);
2479 }
2480
2481 /*
2482 * Wait for writeback against this page to complete before allowing it
2483 * to be marked dirty again, and hence written back again, possibly
2484 * before the previous writepage completed.
2485 *
2486 * Block here, instead of in ->writepage(), so that the userspace fs
2487 * can only block processes actually operating on the filesystem.
2488 *
2489 * Otherwise unprivileged userspace fs would be able to block
2490 * unrelated:
2491 *
2492 * - page migration
2493 * - sync(2)
2494 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2495 */
fuse_page_mkwrite(struct vm_fault * vmf)2496 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2497 {
2498 struct page *page = vmf->page;
2499 struct inode *inode = file_inode(vmf->vma->vm_file);
2500
2501 file_update_time(vmf->vma->vm_file);
2502 lock_page(page);
2503 if (page->mapping != inode->i_mapping) {
2504 unlock_page(page);
2505 return VM_FAULT_NOPAGE;
2506 }
2507
2508 fuse_wait_on_page_writeback(inode, page->index);
2509 return VM_FAULT_LOCKED;
2510 }
2511
2512 static const struct vm_operations_struct fuse_file_vm_ops = {
2513 .close = fuse_vma_close,
2514 .fault = filemap_fault,
2515 .map_pages = filemap_map_pages,
2516 .page_mkwrite = fuse_page_mkwrite,
2517 .speculative = true,
2518 };
2519
fuse_file_mmap(struct file * file,struct vm_area_struct * vma)2520 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2521 {
2522 struct fuse_file *ff = file->private_data;
2523
2524 /* DAX mmap is superior to direct_io mmap */
2525 if (FUSE_IS_DAX(file_inode(file)))
2526 return fuse_dax_mmap(file, vma);
2527
2528 #ifdef CONFIG_FUSE_BPF
2529 /* TODO - this is simply passthrough, not a proper BPF filter */
2530 if (ff->backing_file)
2531 return fuse_backing_mmap(file, vma);
2532 #endif
2533
2534 if (ff->passthrough.filp)
2535 return fuse_passthrough_mmap(file, vma);
2536
2537 if (ff->open_flags & FOPEN_DIRECT_IO) {
2538 /* Can't provide the coherency needed for MAP_SHARED */
2539 if (vma->vm_flags & VM_MAYSHARE)
2540 return -ENODEV;
2541
2542 invalidate_inode_pages2(file->f_mapping);
2543
2544 return generic_file_mmap(file, vma);
2545 }
2546
2547 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2548 fuse_link_write_file(file);
2549
2550 file_accessed(file);
2551 vma->vm_ops = &fuse_file_vm_ops;
2552 return 0;
2553 }
2554
convert_fuse_file_lock(struct fuse_conn * fc,const struct fuse_file_lock * ffl,struct file_lock * fl)2555 static int convert_fuse_file_lock(struct fuse_conn *fc,
2556 const struct fuse_file_lock *ffl,
2557 struct file_lock *fl)
2558 {
2559 switch (ffl->type) {
2560 case F_UNLCK:
2561 break;
2562
2563 case F_RDLCK:
2564 case F_WRLCK:
2565 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2566 ffl->end < ffl->start)
2567 return -EIO;
2568
2569 fl->fl_start = ffl->start;
2570 fl->fl_end = ffl->end;
2571
2572 /*
2573 * Convert pid into init's pid namespace. The locks API will
2574 * translate it into the caller's pid namespace.
2575 */
2576 rcu_read_lock();
2577 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2578 rcu_read_unlock();
2579 break;
2580
2581 default:
2582 return -EIO;
2583 }
2584 fl->fl_type = ffl->type;
2585 return 0;
2586 }
2587
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)2588 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2589 const struct file_lock *fl, int opcode, pid_t pid,
2590 int flock, struct fuse_lk_in *inarg)
2591 {
2592 struct inode *inode = file_inode(file);
2593 struct fuse_conn *fc = get_fuse_conn(inode);
2594 struct fuse_file *ff = file->private_data;
2595
2596 memset(inarg, 0, sizeof(*inarg));
2597 inarg->fh = ff->fh;
2598 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2599 inarg->lk.start = fl->fl_start;
2600 inarg->lk.end = fl->fl_end;
2601 inarg->lk.type = fl->fl_type;
2602 inarg->lk.pid = pid;
2603 if (flock)
2604 inarg->lk_flags |= FUSE_LK_FLOCK;
2605 args->opcode = opcode;
2606 args->nodeid = get_node_id(inode);
2607 args->in_numargs = 1;
2608 args->in_args[0].size = sizeof(*inarg);
2609 args->in_args[0].value = inarg;
2610 }
2611
fuse_getlk(struct file * file,struct file_lock * fl)2612 static int fuse_getlk(struct file *file, struct file_lock *fl)
2613 {
2614 struct inode *inode = file_inode(file);
2615 struct fuse_mount *fm = get_fuse_mount(inode);
2616 FUSE_ARGS(args);
2617 struct fuse_lk_in inarg;
2618 struct fuse_lk_out outarg;
2619 int err;
2620
2621 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2622 args.out_numargs = 1;
2623 args.out_args[0].size = sizeof(outarg);
2624 args.out_args[0].value = &outarg;
2625 err = fuse_simple_request(fm, &args);
2626 if (!err)
2627 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2628
2629 return err;
2630 }
2631
fuse_setlk(struct file * file,struct file_lock * fl,int flock)2632 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2633 {
2634 struct inode *inode = file_inode(file);
2635 struct fuse_mount *fm = get_fuse_mount(inode);
2636 FUSE_ARGS(args);
2637 struct fuse_lk_in inarg;
2638 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2639 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2640 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2641 int err;
2642
2643 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2644 /* NLM needs asynchronous locks, which we don't support yet */
2645 return -ENOLCK;
2646 }
2647
2648 /* Unlock on close is handled by the flush method */
2649 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2650 return 0;
2651
2652 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2653 err = fuse_simple_request(fm, &args);
2654
2655 /* locking is restartable */
2656 if (err == -EINTR)
2657 err = -ERESTARTSYS;
2658
2659 return err;
2660 }
2661
fuse_file_lock(struct file * file,int cmd,struct file_lock * fl)2662 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2663 {
2664 struct inode *inode = file_inode(file);
2665 struct fuse_conn *fc = get_fuse_conn(inode);
2666 int err;
2667
2668 if (cmd == F_CANCELLK) {
2669 err = 0;
2670 } else if (cmd == F_GETLK) {
2671 if (fc->no_lock) {
2672 posix_test_lock(file, fl);
2673 err = 0;
2674 } else
2675 err = fuse_getlk(file, fl);
2676 } else {
2677 if (fc->no_lock)
2678 err = posix_lock_file(file, fl, NULL);
2679 else
2680 err = fuse_setlk(file, fl, 0);
2681 }
2682 return err;
2683 }
2684
fuse_file_flock(struct file * file,int cmd,struct file_lock * fl)2685 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2686 {
2687 struct inode *inode = file_inode(file);
2688 struct fuse_conn *fc = get_fuse_conn(inode);
2689 struct fuse_file *ff = file->private_data;
2690 int err;
2691
2692 #ifdef CONFIG_FUSE_BPF
2693 /* TODO - this is simply passthrough, not a proper BPF filter */
2694 if (ff->backing_file)
2695 return fuse_file_flock_backing(file, cmd, fl);
2696 #endif
2697
2698 if (fc->no_flock) {
2699 err = locks_lock_file_wait(file, fl);
2700 } else {
2701
2702 /* emulate flock with POSIX locks */
2703 ff->flock = true;
2704 err = fuse_setlk(file, fl, 1);
2705 }
2706
2707 return err;
2708 }
2709
fuse_bmap(struct address_space * mapping,sector_t block)2710 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2711 {
2712 struct inode *inode = mapping->host;
2713 struct fuse_mount *fm = get_fuse_mount(inode);
2714 FUSE_ARGS(args);
2715 struct fuse_bmap_in inarg;
2716 struct fuse_bmap_out outarg;
2717 int err;
2718
2719 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2720 return 0;
2721
2722 memset(&inarg, 0, sizeof(inarg));
2723 inarg.block = block;
2724 inarg.blocksize = inode->i_sb->s_blocksize;
2725 args.opcode = FUSE_BMAP;
2726 args.nodeid = get_node_id(inode);
2727 args.in_numargs = 1;
2728 args.in_args[0].size = sizeof(inarg);
2729 args.in_args[0].value = &inarg;
2730 args.out_numargs = 1;
2731 args.out_args[0].size = sizeof(outarg);
2732 args.out_args[0].value = &outarg;
2733 err = fuse_simple_request(fm, &args);
2734 if (err == -ENOSYS)
2735 fm->fc->no_bmap = 1;
2736
2737 return err ? 0 : outarg.block;
2738 }
2739
fuse_lseek(struct file * file,loff_t offset,int whence)2740 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2741 {
2742 struct inode *inode = file->f_mapping->host;
2743 struct fuse_mount *fm = get_fuse_mount(inode);
2744 struct fuse_file *ff = file->private_data;
2745 FUSE_ARGS(args);
2746 struct fuse_lseek_in inarg = {
2747 .fh = ff->fh,
2748 .offset = offset,
2749 .whence = whence
2750 };
2751 struct fuse_lseek_out outarg;
2752 int err;
2753
2754 if (fm->fc->no_lseek)
2755 goto fallback;
2756
2757 args.opcode = FUSE_LSEEK;
2758 args.nodeid = ff->nodeid;
2759 args.in_numargs = 1;
2760 args.in_args[0].size = sizeof(inarg);
2761 args.in_args[0].value = &inarg;
2762 args.out_numargs = 1;
2763 args.out_args[0].size = sizeof(outarg);
2764 args.out_args[0].value = &outarg;
2765 err = fuse_simple_request(fm, &args);
2766 if (err) {
2767 if (err == -ENOSYS) {
2768 fm->fc->no_lseek = 1;
2769 goto fallback;
2770 }
2771 return err;
2772 }
2773
2774 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2775
2776 fallback:
2777 err = fuse_update_attributes(inode, file);
2778 if (!err)
2779 return generic_file_llseek(file, offset, whence);
2780 else
2781 return err;
2782 }
2783
fuse_file_llseek(struct file * file,loff_t offset,int whence)2784 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2785 {
2786 loff_t retval;
2787 struct inode *inode = file_inode(file);
2788 #ifdef CONFIG_FUSE_BPF
2789 struct fuse_err_ret fer;
2790
2791 fer = fuse_bpf_backing(inode, struct fuse_lseek_io,
2792 fuse_lseek_initialize,
2793 fuse_lseek_backing,
2794 fuse_lseek_finalize,
2795 file, offset, whence);
2796 if (fer.ret)
2797 return PTR_ERR(fer.result);
2798 #endif
2799
2800 switch (whence) {
2801 case SEEK_SET:
2802 case SEEK_CUR:
2803 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2804 retval = generic_file_llseek(file, offset, whence);
2805 break;
2806 case SEEK_END:
2807 inode_lock(inode);
2808 retval = fuse_update_attributes(inode, file);
2809 if (!retval)
2810 retval = generic_file_llseek(file, offset, whence);
2811 inode_unlock(inode);
2812 break;
2813 case SEEK_HOLE:
2814 case SEEK_DATA:
2815 inode_lock(inode);
2816 retval = fuse_lseek(file, offset, whence);
2817 inode_unlock(inode);
2818 break;
2819 default:
2820 retval = -EINVAL;
2821 }
2822
2823 return retval;
2824 }
2825
2826 /*
2827 * All files which have been polled are linked to RB tree
2828 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2829 * find the matching one.
2830 */
fuse_find_polled_node(struct fuse_conn * fc,u64 kh,struct rb_node ** parent_out)2831 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2832 struct rb_node **parent_out)
2833 {
2834 struct rb_node **link = &fc->polled_files.rb_node;
2835 struct rb_node *last = NULL;
2836
2837 while (*link) {
2838 struct fuse_file *ff;
2839
2840 last = *link;
2841 ff = rb_entry(last, struct fuse_file, polled_node);
2842
2843 if (kh < ff->kh)
2844 link = &last->rb_left;
2845 else if (kh > ff->kh)
2846 link = &last->rb_right;
2847 else
2848 return link;
2849 }
2850
2851 if (parent_out)
2852 *parent_out = last;
2853 return link;
2854 }
2855
2856 /*
2857 * The file is about to be polled. Make sure it's on the polled_files
2858 * RB tree. Note that files once added to the polled_files tree are
2859 * not removed before the file is released. This is because a file
2860 * polled once is likely to be polled again.
2861 */
fuse_register_polled_file(struct fuse_conn * fc,struct fuse_file * ff)2862 static void fuse_register_polled_file(struct fuse_conn *fc,
2863 struct fuse_file *ff)
2864 {
2865 spin_lock(&fc->lock);
2866 if (RB_EMPTY_NODE(&ff->polled_node)) {
2867 struct rb_node **link, *parent;
2868
2869 link = fuse_find_polled_node(fc, ff->kh, &parent);
2870 BUG_ON(*link);
2871 rb_link_node(&ff->polled_node, parent, link);
2872 rb_insert_color(&ff->polled_node, &fc->polled_files);
2873 }
2874 spin_unlock(&fc->lock);
2875 }
2876
fuse_file_poll(struct file * file,poll_table * wait)2877 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2878 {
2879 struct fuse_file *ff = file->private_data;
2880 struct fuse_mount *fm = ff->fm;
2881 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2882 struct fuse_poll_out outarg;
2883 FUSE_ARGS(args);
2884 int err;
2885
2886 if (fm->fc->no_poll)
2887 return DEFAULT_POLLMASK;
2888
2889 poll_wait(file, &ff->poll_wait, wait);
2890 inarg.events = mangle_poll(poll_requested_events(wait));
2891
2892 /*
2893 * Ask for notification iff there's someone waiting for it.
2894 * The client may ignore the flag and always notify.
2895 */
2896 if (waitqueue_active(&ff->poll_wait)) {
2897 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2898 fuse_register_polled_file(fm->fc, ff);
2899 }
2900
2901 args.opcode = FUSE_POLL;
2902 args.nodeid = ff->nodeid;
2903 args.in_numargs = 1;
2904 args.in_args[0].size = sizeof(inarg);
2905 args.in_args[0].value = &inarg;
2906 args.out_numargs = 1;
2907 args.out_args[0].size = sizeof(outarg);
2908 args.out_args[0].value = &outarg;
2909 err = fuse_simple_request(fm, &args);
2910
2911 if (!err)
2912 return demangle_poll(outarg.revents);
2913 if (err == -ENOSYS) {
2914 fm->fc->no_poll = 1;
2915 return DEFAULT_POLLMASK;
2916 }
2917 return EPOLLERR;
2918 }
2919 EXPORT_SYMBOL_GPL(fuse_file_poll);
2920
2921 /*
2922 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2923 * wakes up the poll waiters.
2924 */
fuse_notify_poll_wakeup(struct fuse_conn * fc,struct fuse_notify_poll_wakeup_out * outarg)2925 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2926 struct fuse_notify_poll_wakeup_out *outarg)
2927 {
2928 u64 kh = outarg->kh;
2929 struct rb_node **link;
2930
2931 spin_lock(&fc->lock);
2932
2933 link = fuse_find_polled_node(fc, kh, NULL);
2934 if (*link) {
2935 struct fuse_file *ff;
2936
2937 ff = rb_entry(*link, struct fuse_file, polled_node);
2938 wake_up_interruptible_sync(&ff->poll_wait);
2939 }
2940
2941 spin_unlock(&fc->lock);
2942 return 0;
2943 }
2944
fuse_do_truncate(struct file * file)2945 static void fuse_do_truncate(struct file *file)
2946 {
2947 struct inode *inode = file->f_mapping->host;
2948 struct iattr attr;
2949
2950 attr.ia_valid = ATTR_SIZE;
2951 attr.ia_size = i_size_read(inode);
2952
2953 attr.ia_file = file;
2954 attr.ia_valid |= ATTR_FILE;
2955
2956 fuse_do_setattr(file_dentry(file), &attr, file);
2957 }
2958
fuse_round_up(struct fuse_conn * fc,loff_t off)2959 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2960 {
2961 return round_up(off, fc->max_pages << PAGE_SHIFT);
2962 }
2963
2964 static ssize_t
fuse_direct_IO(struct kiocb * iocb,struct iov_iter * iter)2965 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2966 {
2967 DECLARE_COMPLETION_ONSTACK(wait);
2968 ssize_t ret = 0;
2969 struct file *file = iocb->ki_filp;
2970 struct fuse_file *ff = file->private_data;
2971 loff_t pos = 0;
2972 struct inode *inode;
2973 loff_t i_size;
2974 size_t count = iov_iter_count(iter), shortened = 0;
2975 loff_t offset = iocb->ki_pos;
2976 struct fuse_io_priv *io;
2977
2978 pos = offset;
2979 inode = file->f_mapping->host;
2980 i_size = i_size_read(inode);
2981
2982 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
2983 return 0;
2984
2985 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2986 if (!io)
2987 return -ENOMEM;
2988 spin_lock_init(&io->lock);
2989 kref_init(&io->refcnt);
2990 io->reqs = 1;
2991 io->bytes = -1;
2992 io->size = 0;
2993 io->offset = offset;
2994 io->write = (iov_iter_rw(iter) == WRITE);
2995 io->err = 0;
2996 /*
2997 * By default, we want to optimize all I/Os with async request
2998 * submission to the client filesystem if supported.
2999 */
3000 io->async = ff->fm->fc->async_dio;
3001 io->iocb = iocb;
3002 io->blocking = is_sync_kiocb(iocb);
3003
3004 /* optimization for short read */
3005 if (io->async && !io->write && offset + count > i_size) {
3006 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
3007 shortened = count - iov_iter_count(iter);
3008 count -= shortened;
3009 }
3010
3011 /*
3012 * We cannot asynchronously extend the size of a file.
3013 * In such case the aio will behave exactly like sync io.
3014 */
3015 if ((offset + count > i_size) && io->write)
3016 io->blocking = true;
3017
3018 if (io->async && io->blocking) {
3019 /*
3020 * Additional reference to keep io around after
3021 * calling fuse_aio_complete()
3022 */
3023 kref_get(&io->refcnt);
3024 io->done = &wait;
3025 }
3026
3027 if (iov_iter_rw(iter) == WRITE) {
3028 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3029 fuse_invalidate_attr(inode);
3030 } else {
3031 ret = __fuse_direct_read(io, iter, &pos);
3032 }
3033 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
3034
3035 if (io->async) {
3036 bool blocking = io->blocking;
3037
3038 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3039
3040 /* we have a non-extending, async request, so return */
3041 if (!blocking)
3042 return -EIOCBQUEUED;
3043
3044 wait_for_completion(&wait);
3045 ret = fuse_get_res_by_io(io);
3046 }
3047
3048 kref_put(&io->refcnt, fuse_io_release);
3049
3050 if (iov_iter_rw(iter) == WRITE) {
3051 if (ret > 0)
3052 fuse_write_update_size(inode, pos);
3053 else if (ret < 0 && offset + count > i_size)
3054 fuse_do_truncate(file);
3055 }
3056
3057 return ret;
3058 }
3059
fuse_writeback_range(struct inode * inode,loff_t start,loff_t end)3060 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3061 {
3062 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
3063
3064 if (!err)
3065 fuse_sync_writes(inode);
3066
3067 return err;
3068 }
3069
fuse_file_fallocate(struct file * file,int mode,loff_t offset,loff_t length)3070 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3071 loff_t length)
3072 {
3073 struct fuse_file *ff = file->private_data;
3074 struct inode *inode = file_inode(file);
3075 struct fuse_inode *fi = get_fuse_inode(inode);
3076 struct fuse_mount *fm = ff->fm;
3077 FUSE_ARGS(args);
3078 struct fuse_fallocate_in inarg = {
3079 .fh = ff->fh,
3080 .offset = offset,
3081 .length = length,
3082 .mode = mode
3083 };
3084 int err;
3085 bool block_faults = FUSE_IS_DAX(inode) &&
3086 (!(mode & FALLOC_FL_KEEP_SIZE) ||
3087 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)));
3088
3089 #ifdef CONFIG_FUSE_BPF
3090 struct fuse_err_ret fer;
3091
3092 fer = fuse_bpf_backing(inode, struct fuse_fallocate_in,
3093 fuse_file_fallocate_initialize,
3094 fuse_file_fallocate_backing,
3095 fuse_file_fallocate_finalize,
3096 file, mode, offset, length);
3097 if (fer.ret)
3098 return PTR_ERR(fer.result);
3099 #endif
3100
3101 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3102 FALLOC_FL_ZERO_RANGE))
3103 return -EOPNOTSUPP;
3104
3105 if (fm->fc->no_fallocate)
3106 return -EOPNOTSUPP;
3107
3108 inode_lock(inode);
3109 if (block_faults) {
3110 filemap_invalidate_lock(inode->i_mapping);
3111 err = fuse_dax_break_layouts(inode, 0, 0);
3112 if (err)
3113 goto out;
3114 }
3115
3116 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
3117 loff_t endbyte = offset + length - 1;
3118
3119 err = fuse_writeback_range(inode, offset, endbyte);
3120 if (err)
3121 goto out;
3122 }
3123
3124 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3125 offset + length > i_size_read(inode)) {
3126 err = inode_newsize_ok(inode, offset + length);
3127 if (err)
3128 goto out;
3129 }
3130
3131 err = file_modified(file);
3132 if (err)
3133 goto out;
3134
3135 if (!(mode & FALLOC_FL_KEEP_SIZE))
3136 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3137
3138 args.opcode = FUSE_FALLOCATE;
3139 args.nodeid = ff->nodeid;
3140 args.in_numargs = 1;
3141 args.in_args[0].size = sizeof(inarg);
3142 args.in_args[0].value = &inarg;
3143 err = fuse_simple_request(fm, &args);
3144 if (err == -ENOSYS) {
3145 fm->fc->no_fallocate = 1;
3146 err = -EOPNOTSUPP;
3147 }
3148 if (err)
3149 goto out;
3150
3151 /* we could have extended the file */
3152 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3153 bool changed = fuse_write_update_size(inode, offset + length);
3154
3155 if (changed && fm->fc->writeback_cache)
3156 file_update_time(file);
3157 }
3158
3159 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
3160 truncate_pagecache_range(inode, offset, offset + length - 1);
3161
3162 fuse_invalidate_attr(inode);
3163
3164 out:
3165 if (!(mode & FALLOC_FL_KEEP_SIZE))
3166 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3167
3168 if (block_faults)
3169 filemap_invalidate_unlock(inode->i_mapping);
3170
3171 inode_unlock(inode);
3172
3173 fuse_flush_time_update(inode);
3174
3175 return err;
3176 }
3177
__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)3178 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3179 struct file *file_out, loff_t pos_out,
3180 size_t len, unsigned int flags)
3181 {
3182 struct fuse_file *ff_in = file_in->private_data;
3183 struct fuse_file *ff_out = file_out->private_data;
3184 struct inode *inode_in = file_inode(file_in);
3185 struct inode *inode_out = file_inode(file_out);
3186 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3187 struct fuse_mount *fm = ff_in->fm;
3188 struct fuse_conn *fc = fm->fc;
3189 FUSE_ARGS(args);
3190 struct fuse_copy_file_range_in inarg = {
3191 .fh_in = ff_in->fh,
3192 .off_in = pos_in,
3193 .nodeid_out = ff_out->nodeid,
3194 .fh_out = ff_out->fh,
3195 .off_out = pos_out,
3196 .len = len,
3197 .flags = flags
3198 };
3199 struct fuse_write_out outarg;
3200 ssize_t err;
3201 /* mark unstable when write-back is not used, and file_out gets
3202 * extended */
3203 bool is_unstable = (!fc->writeback_cache) &&
3204 ((pos_out + len) > inode_out->i_size);
3205
3206 #ifdef CONFIG_FUSE_BPF
3207 struct fuse_err_ret fer;
3208
3209 fer = fuse_bpf_backing(file_in->f_inode, struct fuse_copy_file_range_io,
3210 fuse_copy_file_range_initialize,
3211 fuse_copy_file_range_backing,
3212 fuse_copy_file_range_finalize,
3213 file_in, pos_in, file_out, pos_out, len, flags);
3214 if (fer.ret)
3215 return PTR_ERR(fer.result);
3216 #endif
3217
3218 if (fc->no_copy_file_range)
3219 return -EOPNOTSUPP;
3220
3221 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3222 return -EXDEV;
3223
3224 inode_lock(inode_in);
3225 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3226 inode_unlock(inode_in);
3227 if (err)
3228 return err;
3229
3230 inode_lock(inode_out);
3231
3232 err = file_modified(file_out);
3233 if (err)
3234 goto out;
3235
3236 /*
3237 * Write out dirty pages in the destination file before sending the COPY
3238 * request to userspace. After the request is completed, truncate off
3239 * pages (including partial ones) from the cache that have been copied,
3240 * since these contain stale data at that point.
3241 *
3242 * This should be mostly correct, but if the COPY writes to partial
3243 * pages (at the start or end) and the parts not covered by the COPY are
3244 * written through a memory map after calling fuse_writeback_range(),
3245 * then these partial page modifications will be lost on truncation.
3246 *
3247 * It is unlikely that someone would rely on such mixed style
3248 * modifications. Yet this does give less guarantees than if the
3249 * copying was performed with write(2).
3250 *
3251 * To fix this a mapping->invalidate_lock could be used to prevent new
3252 * faults while the copy is ongoing.
3253 */
3254 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3255 if (err)
3256 goto out;
3257
3258 if (is_unstable)
3259 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3260
3261 args.opcode = FUSE_COPY_FILE_RANGE;
3262 args.nodeid = ff_in->nodeid;
3263 args.in_numargs = 1;
3264 args.in_args[0].size = sizeof(inarg);
3265 args.in_args[0].value = &inarg;
3266 args.out_numargs = 1;
3267 args.out_args[0].size = sizeof(outarg);
3268 args.out_args[0].value = &outarg;
3269 err = fuse_simple_request(fm, &args);
3270 if (err == -ENOSYS) {
3271 fc->no_copy_file_range = 1;
3272 err = -EOPNOTSUPP;
3273 }
3274 if (err)
3275 goto out;
3276
3277 truncate_inode_pages_range(inode_out->i_mapping,
3278 ALIGN_DOWN(pos_out, PAGE_SIZE),
3279 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3280
3281 if (fc->writeback_cache) {
3282 fuse_write_update_size(inode_out, pos_out + outarg.size);
3283 file_update_time(file_out);
3284 }
3285
3286 fuse_invalidate_attr(inode_out);
3287
3288 err = outarg.size;
3289 out:
3290 if (is_unstable)
3291 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3292
3293 inode_unlock(inode_out);
3294 file_accessed(file_in);
3295
3296 fuse_flush_time_update(inode_out);
3297
3298 return err;
3299 }
3300
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)3301 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3302 struct file *dst_file, loff_t dst_off,
3303 size_t len, unsigned int flags)
3304 {
3305 ssize_t ret;
3306
3307 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3308 len, flags);
3309
3310 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3311 ret = generic_copy_file_range(src_file, src_off, dst_file,
3312 dst_off, len, flags);
3313 return ret;
3314 }
3315
3316 static const struct file_operations fuse_file_operations = {
3317 .llseek = fuse_file_llseek,
3318 .read_iter = fuse_file_read_iter,
3319 .write_iter = fuse_file_write_iter,
3320 .mmap = fuse_file_mmap,
3321 .open = fuse_open,
3322 .flush = fuse_flush,
3323 .release = fuse_release,
3324 .fsync = fuse_fsync,
3325 .lock = fuse_file_lock,
3326 .get_unmapped_area = thp_get_unmapped_area,
3327 .flock = fuse_file_flock,
3328 .splice_read = generic_file_splice_read,
3329 .splice_write = iter_file_splice_write,
3330 .unlocked_ioctl = fuse_file_ioctl,
3331 .compat_ioctl = fuse_file_compat_ioctl,
3332 .poll = fuse_file_poll,
3333 .fallocate = fuse_file_fallocate,
3334 .copy_file_range = fuse_copy_file_range,
3335 };
3336
3337 static const struct address_space_operations fuse_file_aops = {
3338 .readpage = fuse_readpage,
3339 .readahead = fuse_readahead,
3340 .writepage = fuse_writepage,
3341 .writepages = fuse_writepages,
3342 .launder_page = fuse_launder_page,
3343 .set_page_dirty = __set_page_dirty_nobuffers,
3344 .bmap = fuse_bmap,
3345 .direct_IO = fuse_direct_IO,
3346 .write_begin = fuse_write_begin,
3347 .write_end = fuse_write_end,
3348 };
3349
fuse_init_file_inode(struct inode * inode)3350 void fuse_init_file_inode(struct inode *inode)
3351 {
3352 struct fuse_inode *fi = get_fuse_inode(inode);
3353
3354 inode->i_fop = &fuse_file_operations;
3355 inode->i_data.a_ops = &fuse_file_aops;
3356
3357 INIT_LIST_HEAD(&fi->write_files);
3358 INIT_LIST_HEAD(&fi->queued_writes);
3359 fi->writectr = 0;
3360 init_waitqueue_head(&fi->page_waitq);
3361 fi->writepages = RB_ROOT;
3362
3363 if (IS_ENABLED(CONFIG_FUSE_DAX))
3364 fuse_dax_inode_init(inode);
3365 }
3366