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