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