treewide: Remove uninitialized_var() usage
[linux-2.6-block.git] / fs / fuse / file.c
CommitLineData
b6aeaded
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
b6aeaded
MS
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/slab.h>
13#include <linux/kernel.h>
e8edc6e0 14#include <linux/sched.h>
7a36094d 15#include <linux/sched/signal.h>
08cbf542 16#include <linux/module.h>
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
76a0294e 934static void fuse_readahead(struct readahead_control *rac)
db50b96c 935{
76a0294e 936 struct inode *inode = rac->mapping->host;
db50b96c 937 struct fuse_conn *fc = get_fuse_conn(inode);
76a0294e 938 unsigned int i, max_pages, nr_pages = 0;
db50b96c 939
248d86e8 940 if (is_bad_inode(inode))
76a0294e 941 return;
248d86e8 942
76a0294e
MWO
943 max_pages = min_t(unsigned int, fc->max_pages,
944 fc->max_read / PAGE_SIZE);
db50b96c 945
76a0294e
MWO
946 for (;;) {
947 struct fuse_io_args *ia;
948 struct fuse_args_pages *ap;
949
950 nr_pages = readahead_count(rac) - nr_pages;
951 if (nr_pages > max_pages)
952 nr_pages = max_pages;
953 if (nr_pages == 0)
954 break;
955 ia = fuse_io_alloc(NULL, nr_pages);
956 if (!ia)
957 return;
958 ap = &ia->ap;
959 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
960 for (i = 0; i < nr_pages; i++) {
961 fuse_wait_on_page_writeback(inode,
962 readahead_index(rac) + i);
963 ap->descs[i].length = PAGE_SIZE;
964 }
965 ap->num_pages = nr_pages;
966 fuse_send_readpages(ia, rac->file);
d3406ffa 967 }
db50b96c
MS
968}
969
55752a3a 970static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
bcb4be80
MS
971{
972 struct inode *inode = iocb->ki_filp->f_mapping->host;
a8894274 973 struct fuse_conn *fc = get_fuse_conn(inode);
bcb4be80 974
a8894274
BF
975 /*
976 * In auto invalidate mode, always update attributes on read.
977 * Otherwise, only update if we attempt to read past EOF (to ensure
978 * i_size is up to date).
979 */
980 if (fc->auto_inval_data ||
37c20f16 981 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
bcb4be80 982 int err;
5b97eeac 983 err = fuse_update_attributes(inode, iocb->ki_filp);
bcb4be80
MS
984 if (err)
985 return err;
986 }
987
37c20f16 988 return generic_file_read_iter(iocb, to);
bcb4be80
MS
989}
990
338f2e3f
MS
991static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
992 loff_t pos, size_t count)
993{
994 struct fuse_args *args = &ia->ap.args;
995
996 ia->write.in.fh = ff->fh;
997 ia->write.in.offset = pos;
998 ia->write.in.size = count;
999 args->opcode = FUSE_WRITE;
1000 args->nodeid = ff->nodeid;
1001 args->in_numargs = 2;
1002 if (ff->fc->minor < 9)
1003 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1004 else
1005 args->in_args[0].size = sizeof(ia->write.in);
1006 args->in_args[0].value = &ia->write.in;
1007 args->in_args[1].size = count;
1008 args->out_numargs = 1;
1009 args->out_args[0].size = sizeof(ia->write.out);
1010 args->out_args[0].value = &ia->write.out;
1011}
1012
1013static unsigned int fuse_write_flags(struct kiocb *iocb)
1014{
1015 unsigned int flags = iocb->ki_filp->f_flags;
1016
1017 if (iocb->ki_flags & IOCB_DSYNC)
1018 flags |= O_DSYNC;
1019 if (iocb->ki_flags & IOCB_SYNC)
1020 flags |= O_SYNC;
1021
1022 return flags;
1023}
1024
45ac96ed
MS
1025static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1026 size_t count, fl_owner_t owner)
b25e82e5 1027{
45ac96ed 1028 struct kiocb *iocb = ia->io->iocb;
e1c0eecb 1029 struct file *file = iocb->ki_filp;
2106cb18
MS
1030 struct fuse_file *ff = file->private_data;
1031 struct fuse_conn *fc = ff->fc;
45ac96ed
MS
1032 struct fuse_write_in *inarg = &ia->write.in;
1033 ssize_t err;
2d698b07 1034
45ac96ed 1035 fuse_write_args_fill(ia, ff, pos, count);
338f2e3f 1036 inarg->flags = fuse_write_flags(iocb);
f3332114 1037 if (owner != NULL) {
f3332114
MS
1038 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1039 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1040 }
36cf66ed 1041
45ac96ed
MS
1042 if (ia->io->async)
1043 return fuse_async_req_send(fc, ia, count);
1044
1045 err = fuse_simple_request(fc, &ia->ap.args);
1046 if (!err && ia->write.out.size > count)
1047 err = -EIO;
36cf66ed 1048
45ac96ed 1049 return err ?: ia->write.out.size;
b6aeaded
MS
1050}
1051
b0aa7606 1052bool fuse_write_update_size(struct inode *inode, loff_t pos)
854512ec
MS
1053{
1054 struct fuse_conn *fc = get_fuse_conn(inode);
1055 struct fuse_inode *fi = get_fuse_inode(inode);
b0aa7606 1056 bool ret = false;
854512ec 1057
f15ecfef 1058 spin_lock(&fi->lock);
4510d86f 1059 fi->attr_version = atomic64_inc_return(&fc->attr_version);
b0aa7606 1060 if (pos > inode->i_size) {
854512ec 1061 i_size_write(inode, pos);
b0aa7606
MP
1062 ret = true;
1063 }
f15ecfef 1064 spin_unlock(&fi->lock);
b0aa7606
MP
1065
1066 return ret;
854512ec
MS
1067}
1068
338f2e3f
MS
1069static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1070 struct kiocb *iocb, struct inode *inode,
1071 loff_t pos, size_t count)
ea9b9907 1072{
338f2e3f
MS
1073 struct fuse_args_pages *ap = &ia->ap;
1074 struct file *file = iocb->ki_filp;
1075 struct fuse_file *ff = file->private_data;
1076 struct fuse_conn *fc = ff->fc;
1077 unsigned int offset, i;
1078 int err;
ea9b9907 1079
338f2e3f
MS
1080 for (i = 0; i < ap->num_pages; i++)
1081 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
ea9b9907 1082
338f2e3f
MS
1083 fuse_write_args_fill(ia, ff, pos, count);
1084 ia->write.in.flags = fuse_write_flags(iocb);
ea9b9907 1085
338f2e3f 1086 err = fuse_simple_request(fc, &ap->args);
8aab336b
MS
1087 if (!err && ia->write.out.size > count)
1088 err = -EIO;
338f2e3f
MS
1089
1090 offset = ap->descs[0].offset;
1091 count = ia->write.out.size;
1092 for (i = 0; i < ap->num_pages; i++) {
1093 struct page *page = ap->pages[i];
ea9b9907 1094
338f2e3f 1095 if (!err && !offset && count >= PAGE_SIZE)
ea9b9907
NP
1096 SetPageUptodate(page);
1097
09cbfeaf
KS
1098 if (count > PAGE_SIZE - offset)
1099 count -= PAGE_SIZE - offset;
ea9b9907
NP
1100 else
1101 count = 0;
1102 offset = 0;
1103
1104 unlock_page(page);
09cbfeaf 1105 put_page(page);
ea9b9907
NP
1106 }
1107
338f2e3f 1108 return err;
ea9b9907
NP
1109}
1110
338f2e3f
MS
1111static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1112 struct address_space *mapping,
1113 struct iov_iter *ii, loff_t pos,
1114 unsigned int max_pages)
ea9b9907
NP
1115{
1116 struct fuse_conn *fc = get_fuse_conn(mapping->host);
09cbfeaf 1117 unsigned offset = pos & (PAGE_SIZE - 1);
ea9b9907
NP
1118 size_t count = 0;
1119 int err;
1120
338f2e3f
MS
1121 ap->args.in_pages = true;
1122 ap->descs[0].offset = offset;
ea9b9907
NP
1123
1124 do {
1125 size_t tmp;
1126 struct page *page;
09cbfeaf
KS
1127 pgoff_t index = pos >> PAGE_SHIFT;
1128 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
ea9b9907
NP
1129 iov_iter_count(ii));
1130
1131 bytes = min_t(size_t, bytes, fc->max_write - count);
1132
1133 again:
1134 err = -EFAULT;
1135 if (iov_iter_fault_in_readable(ii, bytes))
1136 break;
1137
1138 err = -ENOMEM;
54566b2c 1139 page = grab_cache_page_write_begin(mapping, index, 0);
ea9b9907
NP
1140 if (!page)
1141 break;
1142
931e80e4 1143 if (mapping_writably_mapped(mapping))
1144 flush_dcache_page(page);
1145
ea9b9907 1146 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
ea9b9907
NP
1147 flush_dcache_page(page);
1148
3ca8138f 1149 iov_iter_advance(ii, tmp);
ea9b9907
NP
1150 if (!tmp) {
1151 unlock_page(page);
09cbfeaf 1152 put_page(page);
ea9b9907
NP
1153 bytes = min(bytes, iov_iter_single_seg_count(ii));
1154 goto again;
1155 }
1156
1157 err = 0;
338f2e3f
MS
1158 ap->pages[ap->num_pages] = page;
1159 ap->descs[ap->num_pages].length = tmp;
1160 ap->num_pages++;
ea9b9907 1161
ea9b9907
NP
1162 count += tmp;
1163 pos += tmp;
1164 offset += tmp;
09cbfeaf 1165 if (offset == PAGE_SIZE)
ea9b9907
NP
1166 offset = 0;
1167
78bb6cb9
MS
1168 if (!fc->big_writes)
1169 break;
ea9b9907 1170 } while (iov_iter_count(ii) && count < fc->max_write &&
338f2e3f 1171 ap->num_pages < max_pages && offset == 0);
ea9b9907
NP
1172
1173 return count > 0 ? count : err;
1174}
1175
5da784cc
CS
1176static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1177 unsigned int max_pages)
d07f09f5 1178{
5da784cc 1179 return min_t(unsigned int,
09cbfeaf
KS
1180 ((pos + len - 1) >> PAGE_SHIFT) -
1181 (pos >> PAGE_SHIFT) + 1,
5da784cc 1182 max_pages);
d07f09f5
MP
1183}
1184
e1c0eecb 1185static ssize_t fuse_perform_write(struct kiocb *iocb,
ea9b9907
NP
1186 struct address_space *mapping,
1187 struct iov_iter *ii, loff_t pos)
1188{
1189 struct inode *inode = mapping->host;
1190 struct fuse_conn *fc = get_fuse_conn(inode);
06a7c3c2 1191 struct fuse_inode *fi = get_fuse_inode(inode);
ea9b9907
NP
1192 int err = 0;
1193 ssize_t res = 0;
1194
06a7c3c2
MP
1195 if (inode->i_size < pos + iov_iter_count(ii))
1196 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1197
ea9b9907 1198 do {
ea9b9907 1199 ssize_t count;
338f2e3f
MS
1200 struct fuse_io_args ia = {};
1201 struct fuse_args_pages *ap = &ia.ap;
5da784cc
CS
1202 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1203 fc->max_pages);
ea9b9907 1204
338f2e3f
MS
1205 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1206 if (!ap->pages) {
1207 err = -ENOMEM;
ea9b9907
NP
1208 break;
1209 }
1210
338f2e3f 1211 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
ea9b9907
NP
1212 if (count <= 0) {
1213 err = count;
1214 } else {
338f2e3f
MS
1215 err = fuse_send_write_pages(&ia, iocb, inode,
1216 pos, count);
ea9b9907 1217 if (!err) {
338f2e3f
MS
1218 size_t num_written = ia.write.out.size;
1219
ea9b9907
NP
1220 res += num_written;
1221 pos += num_written;
1222
1223 /* break out of the loop on short write */
1224 if (num_written != count)
1225 err = -EIO;
1226 }
1227 }
338f2e3f 1228 kfree(ap->pages);
ea9b9907
NP
1229 } while (!err && iov_iter_count(ii));
1230
1231 if (res > 0)
1232 fuse_write_update_size(inode, pos);
1233
06a7c3c2 1234 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
ea9b9907
NP
1235 fuse_invalidate_attr(inode);
1236
1237 return res > 0 ? res : err;
1238}
1239
55752a3a 1240static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
ea9b9907
NP
1241{
1242 struct file *file = iocb->ki_filp;
1243 struct address_space *mapping = file->f_mapping;
ea9b9907 1244 ssize_t written = 0;
4273b793 1245 ssize_t written_buffered = 0;
ea9b9907
NP
1246 struct inode *inode = mapping->host;
1247 ssize_t err;
4273b793 1248 loff_t endbyte = 0;
ea9b9907 1249
4d99ff8f
PE
1250 if (get_fuse_conn(inode)->writeback_cache) {
1251 /* Update size (EOF optimization) and mode (SUID clearing) */
5b97eeac 1252 err = fuse_update_attributes(mapping->host, file);
4d99ff8f
PE
1253 if (err)
1254 return err;
1255
84c3d55c 1256 return generic_file_write_iter(iocb, from);
4d99ff8f
PE
1257 }
1258
5955102c 1259 inode_lock(inode);
ea9b9907
NP
1260
1261 /* We can write back this queue in page reclaim */
de1414a6 1262 current->backing_dev_info = inode_to_bdi(inode);
ea9b9907 1263
3309dd04
AV
1264 err = generic_write_checks(iocb, from);
1265 if (err <= 0)
ea9b9907
NP
1266 goto out;
1267
5fa8e0a1 1268 err = file_remove_privs(file);
ea9b9907
NP
1269 if (err)
1270 goto out;
1271
c3b2da31
JB
1272 err = file_update_time(file);
1273 if (err)
1274 goto out;
ea9b9907 1275
2ba48ce5 1276 if (iocb->ki_flags & IOCB_DIRECT) {
3309dd04 1277 loff_t pos = iocb->ki_pos;
1af5bb49 1278 written = generic_file_direct_write(iocb, from);
84c3d55c 1279 if (written < 0 || !iov_iter_count(from))
4273b793
AA
1280 goto out;
1281
1282 pos += written;
ea9b9907 1283
e1c0eecb 1284 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
4273b793
AA
1285 if (written_buffered < 0) {
1286 err = written_buffered;
1287 goto out;
1288 }
1289 endbyte = pos + written_buffered - 1;
1290
1291 err = filemap_write_and_wait_range(file->f_mapping, pos,
1292 endbyte);
1293 if (err)
1294 goto out;
1295
1296 invalidate_mapping_pages(file->f_mapping,
09cbfeaf
KS
1297 pos >> PAGE_SHIFT,
1298 endbyte >> PAGE_SHIFT);
4273b793
AA
1299
1300 written += written_buffered;
1301 iocb->ki_pos = pos + written_buffered;
1302 } else {
e1c0eecb 1303 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
4273b793 1304 if (written >= 0)
3309dd04 1305 iocb->ki_pos += written;
4273b793 1306 }
ea9b9907
NP
1307out:
1308 current->backing_dev_info = NULL;
5955102c 1309 inode_unlock(inode);
e1c0eecb
MS
1310 if (written > 0)
1311 written = generic_write_sync(iocb, written);
ea9b9907
NP
1312
1313 return written ? written : err;
1314}
1315
093f38a2
MS
1316static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1317 unsigned int index,
1318 unsigned int nr_pages)
85f40aec
MP
1319{
1320 int i;
1321
7c190c8b 1322 for (i = index; i < index + nr_pages; i++)
093f38a2 1323 descs[i].length = PAGE_SIZE - descs[i].offset;
85f40aec
MP
1324}
1325
7c190c8b
MP
1326static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1327{
1328 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1329}
1330
1331static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1332 size_t max_size)
1333{
1334 return min(iov_iter_single_seg_count(ii), max_size);
1335}
1336
45ac96ed
MS
1337static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1338 size_t *nbytesp, int write,
1339 unsigned int max_pages)
413ef8cb 1340{
7c190c8b 1341 size_t nbytes = 0; /* # bytes already packed in req */
742f9927 1342 ssize_t ret = 0;
b98d023a 1343
f4975c67 1344 /* Special case for kernel I/O: can copy directly into the buffer */
00e23707 1345 if (iov_iter_is_kvec(ii)) {
7c190c8b
MP
1346 unsigned long user_addr = fuse_get_user_addr(ii);
1347 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1348
f4975c67 1349 if (write)
45ac96ed 1350 ap->args.in_args[1].value = (void *) user_addr;
f4975c67 1351 else
45ac96ed 1352 ap->args.out_args[0].value = (void *) user_addr;
f4975c67 1353
b98d023a
MP
1354 iov_iter_advance(ii, frag_size);
1355 *nbytesp = frag_size;
f4975c67
MS
1356 return 0;
1357 }
413ef8cb 1358
45ac96ed 1359 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
7c190c8b 1360 unsigned npages;
f67da30c 1361 size_t start;
45ac96ed 1362 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
2c80929c 1363 *nbytesp - nbytes,
45ac96ed 1364 max_pages - ap->num_pages,
c7f3888a 1365 &start);
7c190c8b 1366 if (ret < 0)
742f9927 1367 break;
7c190c8b 1368
c9c37e2e
AV
1369 iov_iter_advance(ii, ret);
1370 nbytes += ret;
7c190c8b 1371
c9c37e2e
AV
1372 ret += start;
1373 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
7c190c8b 1374
45ac96ed
MS
1375 ap->descs[ap->num_pages].offset = start;
1376 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
7c190c8b 1377
45ac96ed
MS
1378 ap->num_pages += npages;
1379 ap->descs[ap->num_pages - 1].length -=
c9c37e2e 1380 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
7c190c8b 1381 }
f4975c67
MS
1382
1383 if (write)
cabdb4fa 1384 ap->args.in_pages = true;
f4975c67 1385 else
cabdb4fa 1386 ap->args.out_pages = true;
f4975c67 1387
7c190c8b 1388 *nbytesp = nbytes;
f4975c67 1389
2c932d4c 1390 return ret < 0 ? ret : 0;
413ef8cb
MS
1391}
1392
d22a943f
AV
1393ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1394 loff_t *ppos, int flags)
413ef8cb 1395{
ea8cd333
PE
1396 int write = flags & FUSE_DIO_WRITE;
1397 int cuse = flags & FUSE_DIO_CUSE;
e1c0eecb 1398 struct file *file = io->iocb->ki_filp;
ea8cd333 1399 struct inode *inode = file->f_mapping->host;
2106cb18
MS
1400 struct fuse_file *ff = file->private_data;
1401 struct fuse_conn *fc = ff->fc;
413ef8cb
MS
1402 size_t nmax = write ? fc->max_write : fc->max_read;
1403 loff_t pos = *ppos;
d22a943f 1404 size_t count = iov_iter_count(iter);
09cbfeaf
KS
1405 pgoff_t idx_from = pos >> PAGE_SHIFT;
1406 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
413ef8cb 1407 ssize_t res = 0;
742f9927 1408 int err = 0;
45ac96ed
MS
1409 struct fuse_io_args *ia;
1410 unsigned int max_pages;
248d86e8 1411
45ac96ed
MS
1412 max_pages = iov_iter_npages(iter, fc->max_pages);
1413 ia = fuse_io_alloc(io, max_pages);
1414 if (!ia)
1415 return -ENOMEM;
413ef8cb 1416
45ac96ed 1417 ia->io = io;
ea8cd333
PE
1418 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1419 if (!write)
5955102c 1420 inode_lock(inode);
ea8cd333
PE
1421 fuse_sync_writes(inode);
1422 if (!write)
5955102c 1423 inode_unlock(inode);
ea8cd333
PE
1424 }
1425
61c12b49 1426 io->should_dirty = !write && iter_is_iovec(iter);
413ef8cb 1427 while (count) {
45ac96ed 1428 ssize_t nres;
2106cb18 1429 fl_owner_t owner = current->files;
f4975c67 1430 size_t nbytes = min(count, nmax);
45ac96ed
MS
1431
1432 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1433 max_pages);
742f9927 1434 if (err && !nbytes)
413ef8cb 1435 break;
f4975c67 1436
4a2abf99 1437 if (write) {
45ac96ed
MS
1438 if (!capable(CAP_FSETID))
1439 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
4a2abf99 1440
45ac96ed 1441 nres = fuse_send_write(ia, pos, nbytes, owner);
4a2abf99 1442 } else {
45ac96ed 1443 nres = fuse_send_read(ia, pos, nbytes, owner);
4a2abf99 1444 }
2106cb18 1445
45ac96ed
MS
1446 if (!io->async || nres < 0) {
1447 fuse_release_user_pages(&ia->ap, io->should_dirty);
1448 fuse_io_free(ia);
1449 }
1450 ia = NULL;
1451 if (nres < 0) {
f658adee 1452 iov_iter_revert(iter, nbytes);
45ac96ed 1453 err = nres;
413ef8cb
MS
1454 break;
1455 }
45ac96ed
MS
1456 WARN_ON(nres > nbytes);
1457
413ef8cb
MS
1458 count -= nres;
1459 res += nres;
1460 pos += nres;
f658adee
MS
1461 if (nres != nbytes) {
1462 iov_iter_revert(iter, nbytes - nres);
413ef8cb 1463 break;
f658adee 1464 }
56cf34ff 1465 if (count) {
45ac96ed
MS
1466 max_pages = iov_iter_npages(iter, fc->max_pages);
1467 ia = fuse_io_alloc(io, max_pages);
1468 if (!ia)
56cf34ff
MS
1469 break;
1470 }
413ef8cb 1471 }
45ac96ed
MS
1472 if (ia)
1473 fuse_io_free(ia);
d09cb9d7 1474 if (res > 0)
413ef8cb 1475 *ppos = pos;
413ef8cb 1476
742f9927 1477 return res > 0 ? res : err;
413ef8cb 1478}
08cbf542 1479EXPORT_SYMBOL_GPL(fuse_direct_io);
413ef8cb 1480
36cf66ed 1481static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
d22a943f
AV
1482 struct iov_iter *iter,
1483 loff_t *ppos)
413ef8cb 1484{
d09cb9d7 1485 ssize_t res;
e1c0eecb 1486 struct inode *inode = file_inode(io->iocb->ki_filp);
d09cb9d7 1487
d22a943f 1488 res = fuse_direct_io(io, iter, ppos, 0);
d09cb9d7 1489
9a2eb24d 1490 fuse_invalidate_atime(inode);
d09cb9d7
MS
1491
1492 return res;
413ef8cb
MS
1493}
1494
23c94e1c
MR
1495static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1496
15316263 1497static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
b98d023a 1498{
23c94e1c
MR
1499 ssize_t res;
1500
1501 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
23c94e1c
MR
1502 res = fuse_direct_IO(iocb, to);
1503 } else {
1504 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1505
1506 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1507 }
1508
1509 return res;
b98d023a
MP
1510}
1511
15316263 1512static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
4273b793 1513{
e1c0eecb
MS
1514 struct inode *inode = file_inode(iocb->ki_filp);
1515 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
15316263 1516 ssize_t res;
4273b793 1517
4273b793 1518 /* Don't allow parallel writes to the same file */
5955102c 1519 inode_lock(inode);
3309dd04 1520 res = generic_write_checks(iocb, from);
23c94e1c
MR
1521 if (res > 0) {
1522 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1523 res = fuse_direct_IO(iocb, from);
1524 } else {
1525 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1526 FUSE_DIO_WRITE);
1527 }
1528 }
812408fb 1529 fuse_invalidate_attr(inode);
bcba24cc 1530 if (res > 0)
15316263 1531 fuse_write_update_size(inode, iocb->ki_pos);
5955102c 1532 inode_unlock(inode);
4273b793
AA
1533
1534 return res;
1535}
1536
55752a3a
MS
1537static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1538{
2f7b6f5b
MS
1539 struct file *file = iocb->ki_filp;
1540 struct fuse_file *ff = file->private_data;
1541
1542 if (is_bad_inode(file_inode(file)))
1543 return -EIO;
55752a3a
MS
1544
1545 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1546 return fuse_cache_read_iter(iocb, to);
1547 else
1548 return fuse_direct_read_iter(iocb, to);
1549}
1550
1551static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1552{
2f7b6f5b
MS
1553 struct file *file = iocb->ki_filp;
1554 struct fuse_file *ff = file->private_data;
1555
1556 if (is_bad_inode(file_inode(file)))
1557 return -EIO;
55752a3a
MS
1558
1559 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1560 return fuse_cache_write_iter(iocb, from);
1561 else
1562 return fuse_direct_write_iter(iocb, from);
1563}
1564
33826ebb 1565static void fuse_writepage_free(struct fuse_writepage_args *wpa)
b6aeaded 1566{
33826ebb 1567 struct fuse_args_pages *ap = &wpa->ia.ap;
385b1268
PE
1568 int i;
1569
33826ebb
MS
1570 for (i = 0; i < ap->num_pages; i++)
1571 __free_page(ap->pages[i]);
1572
1573 if (wpa->ia.ff)
1574 fuse_file_put(wpa->ia.ff, false, false);
8b284dc4 1575
33826ebb
MS
1576 kfree(ap->pages);
1577 kfree(wpa);
3be5a52b
MS
1578}
1579
33826ebb
MS
1580static void fuse_writepage_finish(struct fuse_conn *fc,
1581 struct fuse_writepage_args *wpa)
3be5a52b 1582{
33826ebb
MS
1583 struct fuse_args_pages *ap = &wpa->ia.ap;
1584 struct inode *inode = wpa->inode;
3be5a52b 1585 struct fuse_inode *fi = get_fuse_inode(inode);
de1414a6 1586 struct backing_dev_info *bdi = inode_to_bdi(inode);
385b1268 1587 int i;
3be5a52b 1588
6b2fb799 1589 rb_erase(&wpa->writepages_entry, &fi->writepages);
33826ebb 1590 for (i = 0; i < ap->num_pages; i++) {
93f78d88 1591 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
33826ebb 1592 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
93f78d88 1593 wb_writeout_inc(&bdi->wb);
385b1268 1594 }
3be5a52b
MS
1595 wake_up(&fi->page_waitq);
1596}
1597
f15ecfef 1598/* Called under fi->lock, may release and reacquire it */
33826ebb
MS
1599static void fuse_send_writepage(struct fuse_conn *fc,
1600 struct fuse_writepage_args *wpa, loff_t size)
f15ecfef
KT
1601__releases(fi->lock)
1602__acquires(fi->lock)
3be5a52b 1603{
33826ebb
MS
1604 struct fuse_writepage_args *aux, *next;
1605 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1606 struct fuse_write_in *inarg = &wpa->ia.write.in;
1607 struct fuse_args *args = &wpa->ia.ap.args;
1608 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1609 int err;
3be5a52b 1610
33826ebb 1611 fi->writectr++;
385b1268
PE
1612 if (inarg->offset + data_size <= size) {
1613 inarg->size = data_size;
3be5a52b 1614 } else if (inarg->offset < size) {
385b1268 1615 inarg->size = size - inarg->offset;
3be5a52b
MS
1616 } else {
1617 /* Got truncated off completely */
1618 goto out_free;
b6aeaded 1619 }
3be5a52b 1620
33826ebb
MS
1621 args->in_args[1].size = inarg->size;
1622 args->force = true;
1623 args->nocreds = true;
1624
1625 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1626 if (err == -ENOMEM) {
1627 spin_unlock(&fi->lock);
1628 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1629 spin_lock(&fi->lock);
1630 }
1631
f15ecfef 1632 /* Fails on broken connection only */
33826ebb 1633 if (unlikely(err))
f15ecfef
KT
1634 goto out_free;
1635
3be5a52b
MS
1636 return;
1637
1638 out_free:
33826ebb
MS
1639 fi->writectr--;
1640 fuse_writepage_finish(fc, wpa);
f15ecfef 1641 spin_unlock(&fi->lock);
e2653bd5
MS
1642
1643 /* After fuse_writepage_finish() aux request list is private */
33826ebb
MS
1644 for (aux = wpa->next; aux; aux = next) {
1645 next = aux->next;
1646 aux->next = NULL;
1647 fuse_writepage_free(aux);
e2653bd5
MS
1648 }
1649
33826ebb 1650 fuse_writepage_free(wpa);
f15ecfef 1651 spin_lock(&fi->lock);
b6aeaded
MS
1652}
1653
3be5a52b
MS
1654/*
1655 * If fi->writectr is positive (no truncate or fsync going on) send
1656 * all queued writepage requests.
1657 *
f15ecfef 1658 * Called with fi->lock
3be5a52b
MS
1659 */
1660void fuse_flush_writepages(struct inode *inode)
f15ecfef
KT
1661__releases(fi->lock)
1662__acquires(fi->lock)
b6aeaded 1663{
3be5a52b
MS
1664 struct fuse_conn *fc = get_fuse_conn(inode);
1665 struct fuse_inode *fi = get_fuse_inode(inode);
9de5be06 1666 loff_t crop = i_size_read(inode);
33826ebb 1667 struct fuse_writepage_args *wpa;
3be5a52b
MS
1668
1669 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
33826ebb
MS
1670 wpa = list_entry(fi->queued_writes.next,
1671 struct fuse_writepage_args, queue_entry);
1672 list_del_init(&wpa->queue_entry);
1673 fuse_send_writepage(fc, wpa, crop);
3be5a52b
MS
1674 }
1675}
1676
6b2fb799
MP
1677static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1678{
1679 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1680 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1681 struct rb_node **p = &root->rb_node;
1682 struct rb_node *parent = NULL;
1683
1684 WARN_ON(!wpa->ia.ap.num_pages);
1685 while (*p) {
1686 struct fuse_writepage_args *curr;
1687 pgoff_t curr_index;
1688
1689 parent = *p;
1690 curr = rb_entry(parent, struct fuse_writepage_args,
1691 writepages_entry);
1692 WARN_ON(curr->inode != wpa->inode);
1693 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1694
1695 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1696 p = &(*p)->rb_right;
1697 else if (idx_to < curr_index)
1698 p = &(*p)->rb_left;
1699 else
1700 return (void) WARN_ON(true);
1701 }
1702
1703 rb_link_node(&wpa->writepages_entry, parent, p);
1704 rb_insert_color(&wpa->writepages_entry, root);
1705}
1706
33826ebb
MS
1707static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1708 int error)
3be5a52b 1709{
33826ebb
MS
1710 struct fuse_writepage_args *wpa =
1711 container_of(args, typeof(*wpa), ia.ap.args);
1712 struct inode *inode = wpa->inode;
3be5a52b
MS
1713 struct fuse_inode *fi = get_fuse_inode(inode);
1714
33826ebb 1715 mapping_set_error(inode->i_mapping, error);
f15ecfef 1716 spin_lock(&fi->lock);
33826ebb 1717 while (wpa->next) {
6eaf4782 1718 struct fuse_conn *fc = get_fuse_conn(inode);
33826ebb
MS
1719 struct fuse_write_in *inarg = &wpa->ia.write.in;
1720 struct fuse_writepage_args *next = wpa->next;
1721
1722 wpa->next = next->next;
1723 next->next = NULL;
1724 next->ia.ff = fuse_file_get(wpa->ia.ff);
6b2fb799 1725 tree_insert(&fi->writepages, next);
6eaf4782
MP
1726
1727 /*
1728 * Skip fuse_flush_writepages() to make it easy to crop requests
1729 * based on primary request size.
1730 *
1731 * 1st case (trivial): there are no concurrent activities using
1732 * fuse_set/release_nowrite. Then we're on safe side because
1733 * fuse_flush_writepages() would call fuse_send_writepage()
1734 * anyway.
1735 *
1736 * 2nd case: someone called fuse_set_nowrite and it is waiting
1737 * now for completion of all in-flight requests. This happens
1738 * rarely and no more than once per page, so this should be
1739 * okay.
1740 *
1741 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1742 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1743 * that fuse_set_nowrite returned implies that all in-flight
1744 * requests were completed along with all of their secondary
1745 * requests. Further primary requests are blocked by negative
1746 * writectr. Hence there cannot be any in-flight requests and
1747 * no invocations of fuse_writepage_end() while we're in
1748 * fuse_set_nowrite..fuse_release_nowrite section.
1749 */
1750 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
8b284dc4 1751 }
3be5a52b 1752 fi->writectr--;
33826ebb 1753 fuse_writepage_finish(fc, wpa);
f15ecfef 1754 spin_unlock(&fi->lock);
33826ebb 1755 fuse_writepage_free(wpa);
3be5a52b
MS
1756}
1757
1e18bda8
MS
1758static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1759 struct fuse_inode *fi)
adcadfa8 1760{
72523425 1761 struct fuse_file *ff = NULL;
adcadfa8 1762
f15ecfef 1763 spin_lock(&fi->lock);
1e18bda8 1764 if (!list_empty(&fi->write_files)) {
72523425
MS
1765 ff = list_entry(fi->write_files.next, struct fuse_file,
1766 write_entry);
1767 fuse_file_get(ff);
1768 }
f15ecfef 1769 spin_unlock(&fi->lock);
adcadfa8
PE
1770
1771 return ff;
1772}
1773
1e18bda8
MS
1774static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1775 struct fuse_inode *fi)
1776{
1777 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1778 WARN_ON(!ff);
1779 return ff;
1780}
1781
1782int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1783{
1784 struct fuse_conn *fc = get_fuse_conn(inode);
1785 struct fuse_inode *fi = get_fuse_inode(inode);
1786 struct fuse_file *ff;
1787 int err;
1788
1789 ff = __fuse_write_file_get(fc, fi);
ab9e13f7 1790 err = fuse_flush_times(inode, ff);
1e18bda8 1791 if (ff)
2e64ff15 1792 fuse_file_put(ff, false, false);
1e18bda8
MS
1793
1794 return err;
1795}
1796
33826ebb
MS
1797static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1798{
1799 struct fuse_writepage_args *wpa;
1800 struct fuse_args_pages *ap;
1801
1802 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1803 if (wpa) {
1804 ap = &wpa->ia.ap;
1805 ap->num_pages = 0;
1806 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1807 if (!ap->pages) {
1808 kfree(wpa);
1809 wpa = NULL;
1810 }
1811 }
1812 return wpa;
1813
1814}
1815
3be5a52b
MS
1816static int fuse_writepage_locked(struct page *page)
1817{
1818 struct address_space *mapping = page->mapping;
1819 struct inode *inode = mapping->host;
1820 struct fuse_conn *fc = get_fuse_conn(inode);
1821 struct fuse_inode *fi = get_fuse_inode(inode);
33826ebb
MS
1822 struct fuse_writepage_args *wpa;
1823 struct fuse_args_pages *ap;
3be5a52b 1824 struct page *tmp_page;
72523425 1825 int error = -ENOMEM;
3be5a52b
MS
1826
1827 set_page_writeback(page);
1828
33826ebb
MS
1829 wpa = fuse_writepage_args_alloc();
1830 if (!wpa)
3be5a52b 1831 goto err;
33826ebb 1832 ap = &wpa->ia.ap;
3be5a52b
MS
1833
1834 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1835 if (!tmp_page)
1836 goto err_free;
1837
72523425 1838 error = -EIO;
33826ebb
MS
1839 wpa->ia.ff = fuse_write_file_get(fc, fi);
1840 if (!wpa->ia.ff)
27f1b363 1841 goto err_nofile;
72523425 1842
33826ebb 1843 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
3be5a52b
MS
1844
1845 copy_highpage(tmp_page, page);
33826ebb
MS
1846 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1847 wpa->next = NULL;
1848 ap->args.in_pages = true;
1849 ap->num_pages = 1;
1850 ap->pages[0] = tmp_page;
1851 ap->descs[0].offset = 0;
1852 ap->descs[0].length = PAGE_SIZE;
1853 ap->args.end = fuse_writepage_end;
1854 wpa->inode = inode;
3be5a52b 1855
93f78d88 1856 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
11fb9989 1857 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
3be5a52b 1858
f15ecfef 1859 spin_lock(&fi->lock);
6b2fb799 1860 tree_insert(&fi->writepages, wpa);
33826ebb 1861 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
3be5a52b 1862 fuse_flush_writepages(inode);
f15ecfef 1863 spin_unlock(&fi->lock);
3be5a52b 1864
4a4ac4eb
MP
1865 end_page_writeback(page);
1866
3be5a52b
MS
1867 return 0;
1868
27f1b363
MP
1869err_nofile:
1870 __free_page(tmp_page);
3be5a52b 1871err_free:
33826ebb 1872 kfree(wpa);
3be5a52b 1873err:
9183976e 1874 mapping_set_error(page->mapping, error);
3be5a52b 1875 end_page_writeback(page);
72523425 1876 return error;
3be5a52b
MS
1877}
1878
1879static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1880{
1881 int err;
1882
ff17be08
MS
1883 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1884 /*
1885 * ->writepages() should be called for sync() and friends. We
1886 * should only get here on direct reclaim and then we are
1887 * allowed to skip a page which is already in flight
1888 */
1889 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1890
1891 redirty_page_for_writepage(wbc, page);
d5880c7a 1892 unlock_page(page);
ff17be08
MS
1893 return 0;
1894 }
1895
3be5a52b
MS
1896 err = fuse_writepage_locked(page);
1897 unlock_page(page);
1898
1899 return err;
1900}
1901
26d614df 1902struct fuse_fill_wb_data {
33826ebb 1903 struct fuse_writepage_args *wpa;
26d614df
PE
1904 struct fuse_file *ff;
1905 struct inode *inode;
2d033eaa 1906 struct page **orig_pages;
33826ebb 1907 unsigned int max_pages;
26d614df
PE
1908};
1909
33826ebb
MS
1910static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1911{
1912 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1913 struct fuse_conn *fc = get_fuse_conn(data->inode);
1914 struct page **pages;
1915 struct fuse_page_desc *descs;
1916 unsigned int npages = min_t(unsigned int,
1917 max_t(unsigned int, data->max_pages * 2,
1918 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1919 fc->max_pages);
1920 WARN_ON(npages <= data->max_pages);
1921
1922 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1923 if (!pages)
1924 return false;
1925
1926 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1927 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1928 kfree(ap->pages);
1929 ap->pages = pages;
1930 ap->descs = descs;
1931 data->max_pages = npages;
1932
1933 return true;
1934}
1935
26d614df
PE
1936static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1937{
33826ebb 1938 struct fuse_writepage_args *wpa = data->wpa;
26d614df 1939 struct inode *inode = data->inode;
26d614df 1940 struct fuse_inode *fi = get_fuse_inode(inode);
33826ebb 1941 int num_pages = wpa->ia.ap.num_pages;
2d033eaa 1942 int i;
26d614df 1943
33826ebb 1944 wpa->ia.ff = fuse_file_get(data->ff);
f15ecfef 1945 spin_lock(&fi->lock);
33826ebb 1946 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
26d614df 1947 fuse_flush_writepages(inode);
f15ecfef 1948 spin_unlock(&fi->lock);
2d033eaa
MP
1949
1950 for (i = 0; i < num_pages; i++)
1951 end_page_writeback(data->orig_pages[i]);
26d614df
PE
1952}
1953
7f305ca1
MS
1954/*
1955 * First recheck under fi->lock if the offending offset is still under
419234d5
MS
1956 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1957 * one already added for a page at this offset. If there's none, then insert
1958 * this new request onto the auxiliary list, otherwise reuse the existing one by
7f305ca1
MS
1959 * copying the new page contents over to the old temporary page.
1960 */
33826ebb 1961static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
8b284dc4
MS
1962 struct page *page)
1963{
33826ebb
MS
1964 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1965 struct fuse_writepage_args *tmp;
1966 struct fuse_writepage_args *old_wpa;
1967 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
8b284dc4 1968
33826ebb 1969 WARN_ON(new_ap->num_pages != 0);
8b284dc4 1970
f15ecfef 1971 spin_lock(&fi->lock);
6b2fb799 1972 rb_erase(&new_wpa->writepages_entry, &fi->writepages);
33826ebb
MS
1973 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1974 if (!old_wpa) {
6b2fb799 1975 tree_insert(&fi->writepages, new_wpa);
f15ecfef 1976 spin_unlock(&fi->lock);
2fe93bd4 1977 return false;
f6011081 1978 }
8b284dc4 1979
33826ebb
MS
1980 new_ap->num_pages = 1;
1981 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
7f305ca1
MS
1982 pgoff_t curr_index;
1983
33826ebb
MS
1984 WARN_ON(tmp->inode != new_wpa->inode);
1985 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
419234d5 1986 if (curr_index == page->index) {
33826ebb
MS
1987 WARN_ON(tmp->ia.ap.num_pages != 1);
1988 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
7f305ca1 1989 break;
8b284dc4
MS
1990 }
1991 }
1992
7f305ca1 1993 if (!tmp) {
33826ebb
MS
1994 new_wpa->next = old_wpa->next;
1995 old_wpa->next = new_wpa;
7f305ca1 1996 }
41b6e41f 1997
f15ecfef 1998 spin_unlock(&fi->lock);
7f305ca1
MS
1999
2000 if (tmp) {
33826ebb 2001 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
8b284dc4 2002
93f78d88 2003 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
33826ebb 2004 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
93f78d88 2005 wb_writeout_inc(&bdi->wb);
33826ebb 2006 fuse_writepage_free(new_wpa);
8b284dc4 2007 }
7f305ca1 2008
2fe93bd4 2009 return true;
8b284dc4
MS
2010}
2011
26d614df
PE
2012static int fuse_writepages_fill(struct page *page,
2013 struct writeback_control *wbc, void *_data)
2014{
2015 struct fuse_fill_wb_data *data = _data;
33826ebb
MS
2016 struct fuse_writepage_args *wpa = data->wpa;
2017 struct fuse_args_pages *ap = &wpa->ia.ap;
26d614df 2018 struct inode *inode = data->inode;
f15ecfef 2019 struct fuse_inode *fi = get_fuse_inode(inode);
26d614df
PE
2020 struct fuse_conn *fc = get_fuse_conn(inode);
2021 struct page *tmp_page;
8b284dc4 2022 bool is_writeback;
26d614df
PE
2023 int err;
2024
2025 if (!data->ff) {
2026 err = -EIO;
091d1a72 2027 data->ff = fuse_write_file_get(fc, fi);
26d614df
PE
2028 if (!data->ff)
2029 goto out_unlock;
2030 }
2031
8b284dc4
MS
2032 /*
2033 * Being under writeback is unlikely but possible. For example direct
2034 * read to an mmaped fuse file will set the page dirty twice; once when
2035 * the pages are faulted with get_user_pages(), and then after the read
2036 * completed.
2037 */
2038 is_writeback = fuse_page_is_writeback(inode, page->index);
2039
33826ebb
MS
2040 if (wpa && ap->num_pages &&
2041 (is_writeback || ap->num_pages == fc->max_pages ||
2042 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2043 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
8b284dc4 2044 fuse_writepages_send(data);
33826ebb
MS
2045 data->wpa = NULL;
2046 } else if (wpa && ap->num_pages == data->max_pages) {
2047 if (!fuse_pages_realloc(data)) {
e52a8250 2048 fuse_writepages_send(data);
33826ebb 2049 data->wpa = NULL;
e52a8250 2050 }
26d614df 2051 }
e52a8250 2052
26d614df
PE
2053 err = -ENOMEM;
2054 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2055 if (!tmp_page)
2056 goto out_unlock;
2057
2058 /*
2059 * The page must not be redirtied until the writeout is completed
2060 * (i.e. userspace has sent a reply to the write request). Otherwise
2061 * there could be more than one temporary page instance for each real
2062 * page.
2063 *
2064 * This is ensured by holding the page lock in page_mkwrite() while
2065 * checking fuse_page_is_writeback(). We already hold the page lock
2066 * since clear_page_dirty_for_io() and keep it held until we add the
33826ebb 2067 * request to the fi->writepages list and increment ap->num_pages.
26d614df
PE
2068 * After this fuse_page_is_writeback() will indicate that the page is
2069 * under writeback, so we can release the page lock.
2070 */
33826ebb 2071 if (data->wpa == NULL) {
26d614df 2072 err = -ENOMEM;
33826ebb
MS
2073 wpa = fuse_writepage_args_alloc();
2074 if (!wpa) {
26d614df
PE
2075 __free_page(tmp_page);
2076 goto out_unlock;
2077 }
33826ebb 2078 data->max_pages = 1;
26d614df 2079
33826ebb
MS
2080 ap = &wpa->ia.ap;
2081 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2082 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2083 wpa->next = NULL;
2084 ap->args.in_pages = true;
2085 ap->args.end = fuse_writepage_end;
2086 ap->num_pages = 0;
2087 wpa->inode = inode;
26d614df 2088
f15ecfef 2089 spin_lock(&fi->lock);
6b2fb799 2090 tree_insert(&fi->writepages, wpa);
f15ecfef 2091 spin_unlock(&fi->lock);
26d614df 2092
33826ebb 2093 data->wpa = wpa;
26d614df
PE
2094 }
2095 set_page_writeback(page);
2096
2097 copy_highpage(tmp_page, page);
33826ebb
MS
2098 ap->pages[ap->num_pages] = tmp_page;
2099 ap->descs[ap->num_pages].offset = 0;
2100 ap->descs[ap->num_pages].length = PAGE_SIZE;
26d614df 2101
93f78d88 2102 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
11fb9989 2103 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
8b284dc4
MS
2104
2105 err = 0;
33826ebb 2106 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
8b284dc4 2107 end_page_writeback(page);
33826ebb 2108 data->wpa = NULL;
8b284dc4
MS
2109 goto out_unlock;
2110 }
33826ebb 2111 data->orig_pages[ap->num_pages] = page;
26d614df
PE
2112
2113 /*
f15ecfef 2114 * Protected by fi->lock against concurrent access by
26d614df
PE
2115 * fuse_page_is_writeback().
2116 */
f15ecfef 2117 spin_lock(&fi->lock);
33826ebb 2118 ap->num_pages++;
f15ecfef 2119 spin_unlock(&fi->lock);
26d614df 2120
26d614df
PE
2121out_unlock:
2122 unlock_page(page);
2123
2124 return err;
2125}
2126
2127static int fuse_writepages(struct address_space *mapping,
2128 struct writeback_control *wbc)
2129{
2130 struct inode *inode = mapping->host;
5da784cc 2131 struct fuse_conn *fc = get_fuse_conn(inode);
26d614df
PE
2132 struct fuse_fill_wb_data data;
2133 int err;
2134
2135 err = -EIO;
2136 if (is_bad_inode(inode))
2137 goto out;
2138
2139 data.inode = inode;
33826ebb 2140 data.wpa = NULL;
26d614df
PE
2141 data.ff = NULL;
2142
2d033eaa 2143 err = -ENOMEM;
5da784cc 2144 data.orig_pages = kcalloc(fc->max_pages,
f2b3455e 2145 sizeof(struct page *),
2d033eaa
MP
2146 GFP_NOFS);
2147 if (!data.orig_pages)
2148 goto out;
2149
26d614df 2150 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
33826ebb 2151 if (data.wpa) {
26d614df 2152 /* Ignore errors if we can write at least one page */
33826ebb 2153 WARN_ON(!data.wpa->ia.ap.num_pages);
26d614df
PE
2154 fuse_writepages_send(&data);
2155 err = 0;
2156 }
2157 if (data.ff)
2e64ff15 2158 fuse_file_put(data.ff, false, false);
2d033eaa
MP
2159
2160 kfree(data.orig_pages);
26d614df
PE
2161out:
2162 return err;
2163}
2164
6b12c1b3
PE
2165/*
2166 * It's worthy to make sure that space is reserved on disk for the write,
2167 * but how to implement it without killing performance need more thinking.
2168 */
2169static int fuse_write_begin(struct file *file, struct address_space *mapping,
2170 loff_t pos, unsigned len, unsigned flags,
2171 struct page **pagep, void **fsdata)
2172{
09cbfeaf 2173 pgoff_t index = pos >> PAGE_SHIFT;
a455589f 2174 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
6b12c1b3
PE
2175 struct page *page;
2176 loff_t fsize;
2177 int err = -ENOMEM;
2178
2179 WARN_ON(!fc->writeback_cache);
2180
2181 page = grab_cache_page_write_begin(mapping, index, flags);
2182 if (!page)
2183 goto error;
2184
2185 fuse_wait_on_page_writeback(mapping->host, page->index);
2186
09cbfeaf 2187 if (PageUptodate(page) || len == PAGE_SIZE)
6b12c1b3
PE
2188 goto success;
2189 /*
2190 * Check if the start this page comes after the end of file, in which
2191 * case the readpage can be optimized away.
2192 */
2193 fsize = i_size_read(mapping->host);
09cbfeaf
KS
2194 if (fsize <= (pos & PAGE_MASK)) {
2195 size_t off = pos & ~PAGE_MASK;
6b12c1b3
PE
2196 if (off)
2197 zero_user_segment(page, 0, off);
2198 goto success;
2199 }
2200 err = fuse_do_readpage(file, page);
2201 if (err)
2202 goto cleanup;
2203success:
2204 *pagep = page;
2205 return 0;
2206
2207cleanup:
2208 unlock_page(page);
09cbfeaf 2209 put_page(page);
6b12c1b3
PE
2210error:
2211 return err;
2212}
2213
2214static int fuse_write_end(struct file *file, struct address_space *mapping,
2215 loff_t pos, unsigned len, unsigned copied,
2216 struct page *page, void *fsdata)
2217{
2218 struct inode *inode = page->mapping->host;
2219
59c3b76c
MS
2220 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2221 if (!copied)
2222 goto unlock;
2223
6b12c1b3
PE
2224 if (!PageUptodate(page)) {
2225 /* Zero any unwritten bytes at the end of the page */
09cbfeaf 2226 size_t endoff = (pos + copied) & ~PAGE_MASK;
6b12c1b3 2227 if (endoff)
09cbfeaf 2228 zero_user_segment(page, endoff, PAGE_SIZE);
6b12c1b3
PE
2229 SetPageUptodate(page);
2230 }
2231
2232 fuse_write_update_size(inode, pos + copied);
2233 set_page_dirty(page);
59c3b76c
MS
2234
2235unlock:
6b12c1b3 2236 unlock_page(page);
09cbfeaf 2237 put_page(page);
6b12c1b3
PE
2238
2239 return copied;
2240}
2241
3be5a52b
MS
2242static int fuse_launder_page(struct page *page)
2243{
2244 int err = 0;
2245 if (clear_page_dirty_for_io(page)) {
2246 struct inode *inode = page->mapping->host;
2247 err = fuse_writepage_locked(page);
2248 if (!err)
2249 fuse_wait_on_page_writeback(inode, page->index);
2250 }
2251 return err;
2252}
2253
2254/*
2255 * Write back dirty pages now, because there may not be any suitable
2256 * open files later
2257 */
2258static void fuse_vma_close(struct vm_area_struct *vma)
2259{
2260 filemap_write_and_wait(vma->vm_file->f_mapping);
2261}
2262
2263/*
2264 * Wait for writeback against this page to complete before allowing it
2265 * to be marked dirty again, and hence written back again, possibly
2266 * before the previous writepage completed.
2267 *
2268 * Block here, instead of in ->writepage(), so that the userspace fs
2269 * can only block processes actually operating on the filesystem.
2270 *
2271 * Otherwise unprivileged userspace fs would be able to block
2272 * unrelated:
2273 *
2274 * - page migration
2275 * - sync(2)
2276 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2277 */
46fb504a 2278static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
3be5a52b 2279{
c2ec175c 2280 struct page *page = vmf->page;
11bac800 2281 struct inode *inode = file_inode(vmf->vma->vm_file);
cca24370 2282
11bac800 2283 file_update_time(vmf->vma->vm_file);
cca24370
MS
2284 lock_page(page);
2285 if (page->mapping != inode->i_mapping) {
2286 unlock_page(page);
2287 return VM_FAULT_NOPAGE;
2288 }
3be5a52b
MS
2289
2290 fuse_wait_on_page_writeback(inode, page->index);
cca24370 2291 return VM_FAULT_LOCKED;
3be5a52b
MS
2292}
2293
f0f37e2f 2294static const struct vm_operations_struct fuse_file_vm_ops = {
3be5a52b
MS
2295 .close = fuse_vma_close,
2296 .fault = filemap_fault,
f1820361 2297 .map_pages = filemap_map_pages,
3be5a52b
MS
2298 .page_mkwrite = fuse_page_mkwrite,
2299};
2300
2301static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2302{
55752a3a
MS
2303 struct fuse_file *ff = file->private_data;
2304
2305 if (ff->open_flags & FOPEN_DIRECT_IO) {
2306 /* Can't provide the coherency needed for MAP_SHARED */
2307 if (vma->vm_flags & VM_MAYSHARE)
2308 return -ENODEV;
2309
2310 invalidate_inode_pages2(file->f_mapping);
2311
2312 return generic_file_mmap(file, vma);
2313 }
2314
650b22b9
PE
2315 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2316 fuse_link_write_file(file);
2317
3be5a52b
MS
2318 file_accessed(file);
2319 vma->vm_ops = &fuse_file_vm_ops;
b6aeaded
MS
2320 return 0;
2321}
2322
0b6e9ea0
SF
2323static int convert_fuse_file_lock(struct fuse_conn *fc,
2324 const struct fuse_file_lock *ffl,
71421259
MS
2325 struct file_lock *fl)
2326{
2327 switch (ffl->type) {
2328 case F_UNLCK:
2329 break;
2330
2331 case F_RDLCK:
2332 case F_WRLCK:
2333 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2334 ffl->end < ffl->start)
2335 return -EIO;
2336
2337 fl->fl_start = ffl->start;
2338 fl->fl_end = ffl->end;
0b6e9ea0
SF
2339
2340 /*
9d5b86ac
BC
2341 * Convert pid into init's pid namespace. The locks API will
2342 * translate it into the caller's pid namespace.
0b6e9ea0
SF
2343 */
2344 rcu_read_lock();
9d5b86ac 2345 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
0b6e9ea0 2346 rcu_read_unlock();
71421259
MS
2347 break;
2348
2349 default:
2350 return -EIO;
2351 }
2352 fl->fl_type = ffl->type;
2353 return 0;
2354}
2355
7078187a 2356static void fuse_lk_fill(struct fuse_args *args, struct file *file,
a9ff4f87 2357 const struct file_lock *fl, int opcode, pid_t pid,
7078187a 2358 int flock, struct fuse_lk_in *inarg)
71421259 2359{
6131ffaa 2360 struct inode *inode = file_inode(file);
9c8ef561 2361 struct fuse_conn *fc = get_fuse_conn(inode);
71421259 2362 struct fuse_file *ff = file->private_data;
7078187a
MS
2363
2364 memset(inarg, 0, sizeof(*inarg));
2365 inarg->fh = ff->fh;
2366 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2367 inarg->lk.start = fl->fl_start;
2368 inarg->lk.end = fl->fl_end;
2369 inarg->lk.type = fl->fl_type;
2370 inarg->lk.pid = pid;
a9ff4f87 2371 if (flock)
7078187a 2372 inarg->lk_flags |= FUSE_LK_FLOCK;
d5b48543
MS
2373 args->opcode = opcode;
2374 args->nodeid = get_node_id(inode);
2375 args->in_numargs = 1;
2376 args->in_args[0].size = sizeof(*inarg);
2377 args->in_args[0].value = inarg;
71421259
MS
2378}
2379
2380static int fuse_getlk(struct file *file, struct file_lock *fl)
2381{
6131ffaa 2382 struct inode *inode = file_inode(file);
71421259 2383 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a
MS
2384 FUSE_ARGS(args);
2385 struct fuse_lk_in inarg;
71421259
MS
2386 struct fuse_lk_out outarg;
2387 int err;
2388
7078187a 2389 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
d5b48543
MS
2390 args.out_numargs = 1;
2391 args.out_args[0].size = sizeof(outarg);
2392 args.out_args[0].value = &outarg;
7078187a 2393 err = fuse_simple_request(fc, &args);
71421259 2394 if (!err)
0b6e9ea0 2395 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
71421259
MS
2396
2397 return err;
2398}
2399
a9ff4f87 2400static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
71421259 2401{
6131ffaa 2402 struct inode *inode = file_inode(file);
71421259 2403 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a
MS
2404 FUSE_ARGS(args);
2405 struct fuse_lk_in inarg;
71421259 2406 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
0b6e9ea0
SF
2407 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2408 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
71421259
MS
2409 int err;
2410
8fb47a4f 2411 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
48e90761
MS
2412 /* NLM needs asynchronous locks, which we don't support yet */
2413 return -ENOLCK;
2414 }
2415
71421259 2416 /* Unlock on close is handled by the flush method */
50f2112c 2417 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
71421259
MS
2418 return 0;
2419
0b6e9ea0 2420 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
7078187a 2421 err = fuse_simple_request(fc, &args);
71421259 2422
a4d27e75
MS
2423 /* locking is restartable */
2424 if (err == -EINTR)
2425 err = -ERESTARTSYS;
7078187a 2426
71421259
MS
2427 return err;
2428}
2429
2430static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2431{
6131ffaa 2432 struct inode *inode = file_inode(file);
71421259
MS
2433 struct fuse_conn *fc = get_fuse_conn(inode);
2434 int err;
2435
48e90761
MS
2436 if (cmd == F_CANCELLK) {
2437 err = 0;
2438 } else if (cmd == F_GETLK) {
71421259 2439 if (fc->no_lock) {
9d6a8c5c 2440 posix_test_lock(file, fl);
71421259
MS
2441 err = 0;
2442 } else
2443 err = fuse_getlk(file, fl);
2444 } else {
2445 if (fc->no_lock)
48e90761 2446 err = posix_lock_file(file, fl, NULL);
71421259 2447 else
a9ff4f87 2448 err = fuse_setlk(file, fl, 0);
71421259
MS
2449 }
2450 return err;
2451}
2452
a9ff4f87
MS
2453static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2454{
6131ffaa 2455 struct inode *inode = file_inode(file);
a9ff4f87
MS
2456 struct fuse_conn *fc = get_fuse_conn(inode);
2457 int err;
2458
37fb3a30 2459 if (fc->no_flock) {
4f656367 2460 err = locks_lock_file_wait(file, fl);
a9ff4f87 2461 } else {
37fb3a30
MS
2462 struct fuse_file *ff = file->private_data;
2463
a9ff4f87 2464 /* emulate flock with POSIX locks */
37fb3a30 2465 ff->flock = true;
a9ff4f87
MS
2466 err = fuse_setlk(file, fl, 1);
2467 }
2468
2469 return err;
2470}
2471
b2d2272f
MS
2472static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2473{
2474 struct inode *inode = mapping->host;
2475 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a 2476 FUSE_ARGS(args);
b2d2272f
MS
2477 struct fuse_bmap_in inarg;
2478 struct fuse_bmap_out outarg;
2479 int err;
2480
2481 if (!inode->i_sb->s_bdev || fc->no_bmap)
2482 return 0;
2483
b2d2272f
MS
2484 memset(&inarg, 0, sizeof(inarg));
2485 inarg.block = block;
2486 inarg.blocksize = inode->i_sb->s_blocksize;
d5b48543
MS
2487 args.opcode = FUSE_BMAP;
2488 args.nodeid = get_node_id(inode);
2489 args.in_numargs = 1;
2490 args.in_args[0].size = sizeof(inarg);
2491 args.in_args[0].value = &inarg;
2492 args.out_numargs = 1;
2493 args.out_args[0].size = sizeof(outarg);
2494 args.out_args[0].value = &outarg;
7078187a 2495 err = fuse_simple_request(fc, &args);
b2d2272f
MS
2496 if (err == -ENOSYS)
2497 fc->no_bmap = 1;
2498
2499 return err ? 0 : outarg.block;
2500}
2501
0b5da8db
R
2502static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2503{
2504 struct inode *inode = file->f_mapping->host;
2505 struct fuse_conn *fc = get_fuse_conn(inode);
2506 struct fuse_file *ff = file->private_data;
2507 FUSE_ARGS(args);
2508 struct fuse_lseek_in inarg = {
2509 .fh = ff->fh,
2510 .offset = offset,
2511 .whence = whence
2512 };
2513 struct fuse_lseek_out outarg;
2514 int err;
2515
2516 if (fc->no_lseek)
2517 goto fallback;
2518
d5b48543
MS
2519 args.opcode = FUSE_LSEEK;
2520 args.nodeid = ff->nodeid;
2521 args.in_numargs = 1;
2522 args.in_args[0].size = sizeof(inarg);
2523 args.in_args[0].value = &inarg;
2524 args.out_numargs = 1;
2525 args.out_args[0].size = sizeof(outarg);
2526 args.out_args[0].value = &outarg;
0b5da8db
R
2527 err = fuse_simple_request(fc, &args);
2528 if (err) {
2529 if (err == -ENOSYS) {
2530 fc->no_lseek = 1;
2531 goto fallback;
2532 }
2533 return err;
2534 }
2535
2536 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2537
2538fallback:
5b97eeac 2539 err = fuse_update_attributes(inode, file);
0b5da8db
R
2540 if (!err)
2541 return generic_file_llseek(file, offset, whence);
2542 else
2543 return err;
2544}
2545
965c8e59 2546static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
5559b8f4
MS
2547{
2548 loff_t retval;
6131ffaa 2549 struct inode *inode = file_inode(file);
5559b8f4 2550
0b5da8db
R
2551 switch (whence) {
2552 case SEEK_SET:
2553 case SEEK_CUR:
2554 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
965c8e59 2555 retval = generic_file_llseek(file, offset, whence);
0b5da8db
R
2556 break;
2557 case SEEK_END:
5955102c 2558 inode_lock(inode);
5b97eeac 2559 retval = fuse_update_attributes(inode, file);
0b5da8db
R
2560 if (!retval)
2561 retval = generic_file_llseek(file, offset, whence);
5955102c 2562 inode_unlock(inode);
0b5da8db
R
2563 break;
2564 case SEEK_HOLE:
2565 case SEEK_DATA:
5955102c 2566 inode_lock(inode);
0b5da8db 2567 retval = fuse_lseek(file, offset, whence);
5955102c 2568 inode_unlock(inode);
0b5da8db
R
2569 break;
2570 default:
2571 retval = -EINVAL;
2572 }
c07c3d19 2573
5559b8f4
MS
2574 return retval;
2575}
2576
d9d318d3
MS
2577/*
2578 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2579 * ABI was defined to be 'struct iovec' which is different on 32bit
2580 * and 64bit. Fortunately we can determine which structure the server
2581 * used from the size of the reply.
2582 */
1baa26b2
MS
2583static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2584 size_t transferred, unsigned count,
2585 bool is_compat)
d9d318d3
MS
2586{
2587#ifdef CONFIG_COMPAT
2588 if (count * sizeof(struct compat_iovec) == transferred) {
2589 struct compat_iovec *ciov = src;
2590 unsigned i;
2591
2592 /*
2593 * With this interface a 32bit server cannot support
2594 * non-compat (i.e. ones coming from 64bit apps) ioctl
2595 * requests
2596 */
2597 if (!is_compat)
2598 return -EINVAL;
2599
2600 for (i = 0; i < count; i++) {
2601 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2602 dst[i].iov_len = ciov[i].iov_len;
2603 }
2604 return 0;
2605 }
2606#endif
2607
2608 if (count * sizeof(struct iovec) != transferred)
2609 return -EIO;
2610
2611 memcpy(dst, src, transferred);
2612 return 0;
2613}
2614
7572777e 2615/* Make sure iov_length() won't overflow */
5da784cc
CS
2616static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2617 size_t count)
7572777e
MS
2618{
2619 size_t n;
5da784cc 2620 u32 max = fc->max_pages << PAGE_SHIFT;
7572777e 2621
fb6ccff6 2622 for (n = 0; n < count; n++, iov++) {
7572777e
MS
2623 if (iov->iov_len > (size_t) max)
2624 return -ENOMEM;
2625 max -= iov->iov_len;
2626 }
2627 return 0;
2628}
2629
1baa26b2
MS
2630static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2631 void *src, size_t transferred, unsigned count,
2632 bool is_compat)
2633{
2634 unsigned i;
2635 struct fuse_ioctl_iovec *fiov = src;
2636
2637 if (fc->minor < 16) {
2638 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2639 count, is_compat);
2640 }
2641
2642 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2643 return -EIO;
2644
2645 for (i = 0; i < count; i++) {
2646 /* Did the server supply an inappropriate value? */
2647 if (fiov[i].base != (unsigned long) fiov[i].base ||
2648 fiov[i].len != (unsigned long) fiov[i].len)
2649 return -EIO;
2650
2651 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2652 dst[i].iov_len = (size_t) fiov[i].len;
2653
2654#ifdef CONFIG_COMPAT
2655 if (is_compat &&
2656 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2657 (compat_size_t) dst[i].iov_len != fiov[i].len))
2658 return -EIO;
2659#endif
2660 }
2661
2662 return 0;
2663}
2664
2665
59efec7b
TH
2666/*
2667 * For ioctls, there is no generic way to determine how much memory
2668 * needs to be read and/or written. Furthermore, ioctls are allowed
2669 * to dereference the passed pointer, so the parameter requires deep
2670 * copying but FUSE has no idea whatsoever about what to copy in or
2671 * out.
2672 *
2673 * This is solved by allowing FUSE server to retry ioctl with
2674 * necessary in/out iovecs. Let's assume the ioctl implementation
2675 * needs to read in the following structure.
2676 *
2677 * struct a {
2678 * char *buf;
2679 * size_t buflen;
2680 * }
2681 *
2682 * On the first callout to FUSE server, inarg->in_size and
2683 * inarg->out_size will be NULL; then, the server completes the ioctl
2684 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2685 * the actual iov array to
2686 *
2687 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2688 *
2689 * which tells FUSE to copy in the requested area and retry the ioctl.
2690 * On the second round, the server has access to the structure and
2691 * from that it can tell what to look for next, so on the invocation,
2692 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2693 *
2694 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2695 * { .iov_base = a.buf, .iov_len = a.buflen } }
2696 *
2697 * FUSE will copy both struct a and the pointed buffer from the
2698 * process doing the ioctl and retry ioctl with both struct a and the
2699 * buffer.
2700 *
2701 * This time, FUSE server has everything it needs and completes ioctl
2702 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2703 *
2704 * Copying data out works the same way.
2705 *
2706 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2707 * automatically initializes in and out iovs by decoding @cmd with
2708 * _IOC_* macros and the server is not allowed to request RETRY. This
2709 * limits ioctl data transfers to well-formed ioctls and is the forced
2710 * behavior for all FUSE servers.
2711 */
08cbf542
TH
2712long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2713 unsigned int flags)
59efec7b 2714{
59efec7b 2715 struct fuse_file *ff = file->private_data;
d36f2487 2716 struct fuse_conn *fc = ff->fc;
59efec7b
TH
2717 struct fuse_ioctl_in inarg = {
2718 .fh = ff->fh,
2719 .cmd = cmd,
2720 .arg = arg,
2721 .flags = flags
2722 };
2723 struct fuse_ioctl_out outarg;
8ac83505 2724 struct iovec *iov_page = NULL;
59efec7b 2725 struct iovec *in_iov = NULL, *out_iov = NULL;
093f38a2
MS
2726 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2727 size_t in_size, out_size, c;
2728 ssize_t transferred;
acbe5fda
MS
2729 int err, i;
2730 struct iov_iter ii;
093f38a2 2731 struct fuse_args_pages ap = {};
59efec7b 2732
1baa26b2
MS
2733#if BITS_PER_LONG == 32
2734 inarg.flags |= FUSE_IOCTL_32BIT;
2735#else
6407f44a 2736 if (flags & FUSE_IOCTL_COMPAT) {
1baa26b2 2737 inarg.flags |= FUSE_IOCTL_32BIT;
6407f44a
IA
2738#ifdef CONFIG_X86_X32
2739 if (in_x32_syscall())
2740 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2741#endif
2742 }
1baa26b2
MS
2743#endif
2744
59efec7b 2745 /* assume all the iovs returned by client always fits in a page */
1baa26b2 2746 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
59efec7b 2747
59efec7b 2748 err = -ENOMEM;
093f38a2 2749 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
8ac83505 2750 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
093f38a2 2751 if (!ap.pages || !iov_page)
59efec7b
TH
2752 goto out;
2753
093f38a2
MS
2754 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2755
59efec7b
TH
2756 /*
2757 * If restricted, initialize IO parameters as encoded in @cmd.
2758 * RETRY from server is not allowed.
2759 */
2760 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
8ac83505 2761 struct iovec *iov = iov_page;
59efec7b 2762
c9f0523d 2763 iov->iov_base = (void __user *)arg;
59efec7b
TH
2764 iov->iov_len = _IOC_SIZE(cmd);
2765
2766 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2767 in_iov = iov;
2768 in_iovs = 1;
2769 }
2770
2771 if (_IOC_DIR(cmd) & _IOC_READ) {
2772 out_iov = iov;
2773 out_iovs = 1;
2774 }
2775 }
2776
2777 retry:
2778 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2779 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2780
2781 /*
2782 * Out data can be used either for actual out data or iovs,
2783 * make sure there always is at least one page.
2784 */
2785 out_size = max_t(size_t, out_size, PAGE_SIZE);
2786 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2787
2788 /* make sure there are enough buffer pages and init request with them */
2789 err = -ENOMEM;
5da784cc 2790 if (max_pages > fc->max_pages)
59efec7b 2791 goto out;
093f38a2
MS
2792 while (ap.num_pages < max_pages) {
2793 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2794 if (!ap.pages[ap.num_pages])
59efec7b 2795 goto out;
093f38a2 2796 ap.num_pages++;
59efec7b
TH
2797 }
2798
59efec7b
TH
2799
2800 /* okay, let's send it to the client */
093f38a2
MS
2801 ap.args.opcode = FUSE_IOCTL;
2802 ap.args.nodeid = ff->nodeid;
2803 ap.args.in_numargs = 1;
2804 ap.args.in_args[0].size = sizeof(inarg);
2805 ap.args.in_args[0].value = &inarg;
59efec7b 2806 if (in_size) {
093f38a2
MS
2807 ap.args.in_numargs++;
2808 ap.args.in_args[1].size = in_size;
2809 ap.args.in_pages = true;
59efec7b 2810
acbe5fda
MS
2811 err = -EFAULT;
2812 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
093f38a2
MS
2813 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2814 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
acbe5fda
MS
2815 if (c != PAGE_SIZE && iov_iter_count(&ii))
2816 goto out;
2817 }
59efec7b
TH
2818 }
2819
093f38a2
MS
2820 ap.args.out_numargs = 2;
2821 ap.args.out_args[0].size = sizeof(outarg);
2822 ap.args.out_args[0].value = &outarg;
2823 ap.args.out_args[1].size = out_size;
2824 ap.args.out_pages = true;
2825 ap.args.out_argvar = true;
59efec7b 2826
093f38a2
MS
2827 transferred = fuse_simple_request(fc, &ap.args);
2828 err = transferred;
2829 if (transferred < 0)
59efec7b
TH
2830 goto out;
2831
2832 /* did it ask for retry? */
2833 if (outarg.flags & FUSE_IOCTL_RETRY) {
8ac83505 2834 void *vaddr;
59efec7b
TH
2835
2836 /* no retry if in restricted mode */
2837 err = -EIO;
2838 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2839 goto out;
2840
2841 in_iovs = outarg.in_iovs;
2842 out_iovs = outarg.out_iovs;
2843
2844 /*
2845 * Make sure things are in boundary, separate checks
2846 * are to protect against overflow.
2847 */
2848 err = -ENOMEM;
2849 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2850 out_iovs > FUSE_IOCTL_MAX_IOV ||
2851 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2852 goto out;
2853
093f38a2 2854 vaddr = kmap_atomic(ap.pages[0]);
1baa26b2 2855 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
d9d318d3
MS
2856 transferred, in_iovs + out_iovs,
2857 (flags & FUSE_IOCTL_COMPAT) != 0);
2408f6ef 2858 kunmap_atomic(vaddr);
d9d318d3
MS
2859 if (err)
2860 goto out;
59efec7b 2861
8ac83505 2862 in_iov = iov_page;
59efec7b
TH
2863 out_iov = in_iov + in_iovs;
2864
5da784cc 2865 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
7572777e
MS
2866 if (err)
2867 goto out;
2868
5da784cc 2869 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
7572777e
MS
2870 if (err)
2871 goto out;
2872
59efec7b
TH
2873 goto retry;
2874 }
2875
2876 err = -EIO;
2877 if (transferred > inarg.out_size)
2878 goto out;
2879
acbe5fda
MS
2880 err = -EFAULT;
2881 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
093f38a2
MS
2882 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2883 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
acbe5fda
MS
2884 if (c != PAGE_SIZE && iov_iter_count(&ii))
2885 goto out;
2886 }
2887 err = 0;
59efec7b 2888 out:
8ac83505 2889 free_page((unsigned long) iov_page);
093f38a2
MS
2890 while (ap.num_pages)
2891 __free_page(ap.pages[--ap.num_pages]);
2892 kfree(ap.pages);
59efec7b
TH
2893
2894 return err ? err : outarg.result;
2895}
08cbf542 2896EXPORT_SYMBOL_GPL(fuse_do_ioctl);
59efec7b 2897
b18da0c5
MS
2898long fuse_ioctl_common(struct file *file, unsigned int cmd,
2899 unsigned long arg, unsigned int flags)
d36f2487 2900{
6131ffaa 2901 struct inode *inode = file_inode(file);
d36f2487
MS
2902 struct fuse_conn *fc = get_fuse_conn(inode);
2903
c2132c1b 2904 if (!fuse_allow_current_process(fc))
d36f2487
MS
2905 return -EACCES;
2906
2907 if (is_bad_inode(inode))
2908 return -EIO;
2909
2910 return fuse_do_ioctl(file, cmd, arg, flags);
2911}
2912
59efec7b
TH
2913static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2914 unsigned long arg)
2915{
b18da0c5 2916 return fuse_ioctl_common(file, cmd, arg, 0);
59efec7b
TH
2917}
2918
2919static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2920 unsigned long arg)
2921{
b18da0c5 2922 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
59efec7b
TH
2923}
2924
95668a69
TH
2925/*
2926 * All files which have been polled are linked to RB tree
2927 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2928 * find the matching one.
2929 */
2930static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2931 struct rb_node **parent_out)
2932{
2933 struct rb_node **link = &fc->polled_files.rb_node;
2934 struct rb_node *last = NULL;
2935
2936 while (*link) {
2937 struct fuse_file *ff;
2938
2939 last = *link;
2940 ff = rb_entry(last, struct fuse_file, polled_node);
2941
2942 if (kh < ff->kh)
2943 link = &last->rb_left;
2944 else if (kh > ff->kh)
2945 link = &last->rb_right;
2946 else
2947 return link;
2948 }
2949
2950 if (parent_out)
2951 *parent_out = last;
2952 return link;
2953}
2954
2955/*
2956 * The file is about to be polled. Make sure it's on the polled_files
2957 * RB tree. Note that files once added to the polled_files tree are
2958 * not removed before the file is released. This is because a file
2959 * polled once is likely to be polled again.
2960 */
2961static void fuse_register_polled_file(struct fuse_conn *fc,
2962 struct fuse_file *ff)
2963{
2964 spin_lock(&fc->lock);
2965 if (RB_EMPTY_NODE(&ff->polled_node)) {
3f649ab7 2966 struct rb_node **link, *parent;
95668a69
TH
2967
2968 link = fuse_find_polled_node(fc, ff->kh, &parent);
2969 BUG_ON(*link);
2970 rb_link_node(&ff->polled_node, parent, link);
2971 rb_insert_color(&ff->polled_node, &fc->polled_files);
2972 }
2973 spin_unlock(&fc->lock);
2974}
2975
076ccb76 2976__poll_t fuse_file_poll(struct file *file, poll_table *wait)
95668a69 2977{
95668a69 2978 struct fuse_file *ff = file->private_data;
797759aa 2979 struct fuse_conn *fc = ff->fc;
95668a69
TH
2980 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2981 struct fuse_poll_out outarg;
7078187a 2982 FUSE_ARGS(args);
95668a69
TH
2983 int err;
2984
2985 if (fc->no_poll)
2986 return DEFAULT_POLLMASK;
2987
2988 poll_wait(file, &ff->poll_wait, wait);
c71d227f 2989 inarg.events = mangle_poll(poll_requested_events(wait));
95668a69
TH
2990
2991 /*
2992 * Ask for notification iff there's someone waiting for it.
2993 * The client may ignore the flag and always notify.
2994 */
2995 if (waitqueue_active(&ff->poll_wait)) {
2996 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2997 fuse_register_polled_file(fc, ff);
2998 }
2999
d5b48543
MS
3000 args.opcode = FUSE_POLL;
3001 args.nodeid = ff->nodeid;
3002 args.in_numargs = 1;
3003 args.in_args[0].size = sizeof(inarg);
3004 args.in_args[0].value = &inarg;
3005 args.out_numargs = 1;
3006 args.out_args[0].size = sizeof(outarg);
3007 args.out_args[0].value = &outarg;
7078187a 3008 err = fuse_simple_request(fc, &args);
95668a69
TH
3009
3010 if (!err)
c71d227f 3011 return demangle_poll(outarg.revents);
95668a69
TH
3012 if (err == -ENOSYS) {
3013 fc->no_poll = 1;
3014 return DEFAULT_POLLMASK;
3015 }
a9a08845 3016 return EPOLLERR;
95668a69 3017}
08cbf542 3018EXPORT_SYMBOL_GPL(fuse_file_poll);
95668a69
TH
3019
3020/*
3021 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3022 * wakes up the poll waiters.
3023 */
3024int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3025 struct fuse_notify_poll_wakeup_out *outarg)
3026{
3027 u64 kh = outarg->kh;
3028 struct rb_node **link;
3029
3030 spin_lock(&fc->lock);
3031
3032 link = fuse_find_polled_node(fc, kh, NULL);
3033 if (*link) {
3034 struct fuse_file *ff;
3035
3036 ff = rb_entry(*link, struct fuse_file, polled_node);
3037 wake_up_interruptible_sync(&ff->poll_wait);
3038 }
3039
3040 spin_unlock(&fc->lock);
3041 return 0;
3042}
3043
efb9fa9e
MP
3044static void fuse_do_truncate(struct file *file)
3045{
3046 struct inode *inode = file->f_mapping->host;
3047 struct iattr attr;
3048
3049 attr.ia_valid = ATTR_SIZE;
3050 attr.ia_size = i_size_read(inode);
3051
3052 attr.ia_file = file;
3053 attr.ia_valid |= ATTR_FILE;
3054
62490330 3055 fuse_do_setattr(file_dentry(file), &attr, file);
efb9fa9e
MP
3056}
3057
5da784cc 3058static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
e5c5f05d 3059{
5da784cc 3060 return round_up(off, fc->max_pages << PAGE_SHIFT);
e5c5f05d
MP
3061}
3062
4273b793 3063static ssize_t
c8b8e32d 3064fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4273b793 3065{
9d5722b7 3066 DECLARE_COMPLETION_ONSTACK(wait);
4273b793 3067 ssize_t ret = 0;
60b9df7a
MS
3068 struct file *file = iocb->ki_filp;
3069 struct fuse_file *ff = file->private_data;
e5c5f05d 3070 bool async_dio = ff->fc->async_dio;
4273b793 3071 loff_t pos = 0;
bcba24cc
MP
3072 struct inode *inode;
3073 loff_t i_size;
a6cbcd4a 3074 size_t count = iov_iter_count(iter);
c8b8e32d 3075 loff_t offset = iocb->ki_pos;
36cf66ed 3076 struct fuse_io_priv *io;
4273b793 3077
4273b793 3078 pos = offset;
bcba24cc
MP
3079 inode = file->f_mapping->host;
3080 i_size = i_size_read(inode);
4273b793 3081
6f673763 3082 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
9fe55eea
SW
3083 return 0;
3084
439ee5f0 3085 /* optimization for short read */
6f673763 3086 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
439ee5f0
MP
3087 if (offset >= i_size)
3088 return 0;
5da784cc 3089 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
6b775b18 3090 count = iov_iter_count(iter);
439ee5f0
MP
3091 }
3092
bcba24cc 3093 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
36cf66ed
MP
3094 if (!io)
3095 return -ENOMEM;
bcba24cc 3096 spin_lock_init(&io->lock);
744742d6 3097 kref_init(&io->refcnt);
bcba24cc
MP
3098 io->reqs = 1;
3099 io->bytes = -1;
3100 io->size = 0;
3101 io->offset = offset;
6f673763 3102 io->write = (iov_iter_rw(iter) == WRITE);
bcba24cc 3103 io->err = 0;
bcba24cc
MP
3104 /*
3105 * By default, we want to optimize all I/Os with async request
60b9df7a 3106 * submission to the client filesystem if supported.
bcba24cc 3107 */
e5c5f05d 3108 io->async = async_dio;
bcba24cc 3109 io->iocb = iocb;
7879c4e5 3110 io->blocking = is_sync_kiocb(iocb);
bcba24cc
MP
3111
3112 /*
7879c4e5
AS
3113 * We cannot asynchronously extend the size of a file.
3114 * In such case the aio will behave exactly like sync io.
bcba24cc 3115 */
7879c4e5
AS
3116 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3117 io->blocking = true;
4273b793 3118
7879c4e5 3119 if (io->async && io->blocking) {
744742d6
SF
3120 /*
3121 * Additional reference to keep io around after
3122 * calling fuse_aio_complete()
3123 */
3124 kref_get(&io->refcnt);
9d5722b7 3125 io->done = &wait;
744742d6 3126 }
9d5722b7 3127
6f673763 3128 if (iov_iter_rw(iter) == WRITE) {
6b775b18 3129 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
812408fb
AV
3130 fuse_invalidate_attr(inode);
3131 } else {
d22a943f 3132 ret = __fuse_direct_read(io, iter, &pos);
812408fb 3133 }
36cf66ed 3134
bcba24cc 3135 if (io->async) {
ebacb812
LC
3136 bool blocking = io->blocking;
3137
bcba24cc
MP
3138 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3139
3140 /* we have a non-extending, async request, so return */
ebacb812 3141 if (!blocking)
bcba24cc
MP
3142 return -EIOCBQUEUED;
3143
9d5722b7
CH
3144 wait_for_completion(&wait);
3145 ret = fuse_get_res_by_io(io);
bcba24cc
MP
3146 }
3147
744742d6 3148 kref_put(&io->refcnt, fuse_io_release);
9d5722b7 3149
6f673763 3150 if (iov_iter_rw(iter) == WRITE) {
efb9fa9e
MP
3151 if (ret > 0)
3152 fuse_write_update_size(inode, pos);
3153 else if (ret < 0 && offset + count > i_size)
3154 fuse_do_truncate(file);
3155 }
4273b793
AA
3156
3157 return ret;
3158}
3159
26eb3bae
MS
3160static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3161{
3162 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3163
3164 if (!err)
3165 fuse_sync_writes(inode);
3166
3167 return err;
3168}
3169
cdadb11c
MS
3170static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3171 loff_t length)
05ba1f08
AP
3172{
3173 struct fuse_file *ff = file->private_data;
1c68271c 3174 struct inode *inode = file_inode(file);
0ab08f57 3175 struct fuse_inode *fi = get_fuse_inode(inode);
05ba1f08 3176 struct fuse_conn *fc = ff->fc;
7078187a 3177 FUSE_ARGS(args);
05ba1f08
AP
3178 struct fuse_fallocate_in inarg = {
3179 .fh = ff->fh,
3180 .offset = offset,
3181 .length = length,
3182 .mode = mode
3183 };
3184 int err;
14c14414
MP
3185 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3186 (mode & FALLOC_FL_PUNCH_HOLE);
05ba1f08 3187
4adb8302
MS
3188 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3189 return -EOPNOTSUPP;
3190
519c6040
MS
3191 if (fc->no_fallocate)
3192 return -EOPNOTSUPP;
3193
14c14414 3194 if (lock_inode) {
5955102c 3195 inode_lock(inode);
bde52788
MP
3196 if (mode & FALLOC_FL_PUNCH_HOLE) {
3197 loff_t endbyte = offset + length - 1;
26eb3bae
MS
3198
3199 err = fuse_writeback_range(inode, offset, endbyte);
bde52788
MP
3200 if (err)
3201 goto out;
bde52788 3202 }
3634a632
BF
3203 }
3204
0cbade02
LB
3205 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3206 offset + length > i_size_read(inode)) {
3207 err = inode_newsize_ok(inode, offset + length);
3208 if (err)
35d6fcbb 3209 goto out;
0cbade02
LB
3210 }
3211
0ab08f57
MP
3212 if (!(mode & FALLOC_FL_KEEP_SIZE))
3213 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3214
d5b48543
MS
3215 args.opcode = FUSE_FALLOCATE;
3216 args.nodeid = ff->nodeid;
3217 args.in_numargs = 1;
3218 args.in_args[0].size = sizeof(inarg);
3219 args.in_args[0].value = &inarg;
7078187a 3220 err = fuse_simple_request(fc, &args);
519c6040
MS
3221 if (err == -ENOSYS) {
3222 fc->no_fallocate = 1;
3223 err = -EOPNOTSUPP;
3224 }
bee6c307
BF
3225 if (err)
3226 goto out;
3227
3228 /* we could have extended the file */
b0aa7606
MP
3229 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3230 bool changed = fuse_write_update_size(inode, offset + length);
3231
93d2269d
MS
3232 if (changed && fc->writeback_cache)
3233 file_update_time(file);
b0aa7606 3234 }
bee6c307
BF
3235
3236 if (mode & FALLOC_FL_PUNCH_HOLE)
3237 truncate_pagecache_range(inode, offset, offset + length - 1);
3238
3239 fuse_invalidate_attr(inode);
3240
3634a632 3241out:
0ab08f57
MP
3242 if (!(mode & FALLOC_FL_KEEP_SIZE))
3243 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3244
bde52788 3245 if (lock_inode)
5955102c 3246 inode_unlock(inode);
3634a632 3247
05ba1f08
AP
3248 return err;
3249}
05ba1f08 3250
64bf5ff5
DC
3251static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3252 struct file *file_out, loff_t pos_out,
3253 size_t len, unsigned int flags)
88bc7d50
NV
3254{
3255 struct fuse_file *ff_in = file_in->private_data;
3256 struct fuse_file *ff_out = file_out->private_data;
a2bc9236 3257 struct inode *inode_in = file_inode(file_in);
88bc7d50
NV
3258 struct inode *inode_out = file_inode(file_out);
3259 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3260 struct fuse_conn *fc = ff_in->fc;
3261 FUSE_ARGS(args);
3262 struct fuse_copy_file_range_in inarg = {
3263 .fh_in = ff_in->fh,
3264 .off_in = pos_in,
3265 .nodeid_out = ff_out->nodeid,
3266 .fh_out = ff_out->fh,
3267 .off_out = pos_out,
3268 .len = len,
3269 .flags = flags
3270 };
3271 struct fuse_write_out outarg;
3272 ssize_t err;
3273 /* mark unstable when write-back is not used, and file_out gets
3274 * extended */
3275 bool is_unstable = (!fc->writeback_cache) &&
3276 ((pos_out + len) > inode_out->i_size);
3277
3278 if (fc->no_copy_file_range)
3279 return -EOPNOTSUPP;
3280
5dae222a
AG
3281 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3282 return -EXDEV;
3283
2c4656df
MS
3284 inode_lock(inode_in);
3285 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3286 inode_unlock(inode_in);
3287 if (err)
3288 return err;
a2bc9236 3289
88bc7d50
NV
3290 inode_lock(inode_out);
3291
fe0da9c0
AG
3292 err = file_modified(file_out);
3293 if (err)
3294 goto out;
3295
9b46418c
MS
3296 /*
3297 * Write out dirty pages in the destination file before sending the COPY
3298 * request to userspace. After the request is completed, truncate off
3299 * pages (including partial ones) from the cache that have been copied,
3300 * since these contain stale data at that point.
3301 *
3302 * This should be mostly correct, but if the COPY writes to partial
3303 * pages (at the start or end) and the parts not covered by the COPY are
3304 * written through a memory map after calling fuse_writeback_range(),
3305 * then these partial page modifications will be lost on truncation.
3306 *
3307 * It is unlikely that someone would rely on such mixed style
3308 * modifications. Yet this does give less guarantees than if the
3309 * copying was performed with write(2).
3310 *
3311 * To fix this a i_mmap_sem style lock could be used to prevent new
3312 * faults while the copy is ongoing.
3313 */
2c4656df
MS
3314 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3315 if (err)
3316 goto out;
88bc7d50
NV
3317
3318 if (is_unstable)
3319 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3320
d5b48543
MS
3321 args.opcode = FUSE_COPY_FILE_RANGE;
3322 args.nodeid = ff_in->nodeid;
3323 args.in_numargs = 1;
3324 args.in_args[0].size = sizeof(inarg);
3325 args.in_args[0].value = &inarg;
3326 args.out_numargs = 1;
3327 args.out_args[0].size = sizeof(outarg);
3328 args.out_args[0].value = &outarg;
88bc7d50
NV
3329 err = fuse_simple_request(fc, &args);
3330 if (err == -ENOSYS) {
3331 fc->no_copy_file_range = 1;
3332 err = -EOPNOTSUPP;
3333 }
3334 if (err)
3335 goto out;
3336
9b46418c
MS
3337 truncate_inode_pages_range(inode_out->i_mapping,
3338 ALIGN_DOWN(pos_out, PAGE_SIZE),
3339 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3340
88bc7d50
NV
3341 if (fc->writeback_cache) {
3342 fuse_write_update_size(inode_out, pos_out + outarg.size);
3343 file_update_time(file_out);
3344 }
3345
3346 fuse_invalidate_attr(inode_out);
3347
3348 err = outarg.size;
3349out:
3350 if (is_unstable)
3351 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3352
3353 inode_unlock(inode_out);
fe0da9c0 3354 file_accessed(file_in);
88bc7d50
NV
3355
3356 return err;
3357}
3358
64bf5ff5
DC
3359static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3360 struct file *dst_file, loff_t dst_off,
3361 size_t len, unsigned int flags)
3362{
3363 ssize_t ret;
3364
3365 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3366 len, flags);
3367
5dae222a 3368 if (ret == -EOPNOTSUPP || ret == -EXDEV)
64bf5ff5
DC
3369 ret = generic_copy_file_range(src_file, src_off, dst_file,
3370 dst_off, len, flags);
3371 return ret;
3372}
3373
4b6f5d20 3374static const struct file_operations fuse_file_operations = {
5559b8f4 3375 .llseek = fuse_file_llseek,
37c20f16 3376 .read_iter = fuse_file_read_iter,
84c3d55c 3377 .write_iter = fuse_file_write_iter,
b6aeaded
MS
3378 .mmap = fuse_file_mmap,
3379 .open = fuse_open,
3380 .flush = fuse_flush,
3381 .release = fuse_release,
3382 .fsync = fuse_fsync,
71421259 3383 .lock = fuse_file_lock,
a9ff4f87 3384 .flock = fuse_file_flock,
3c3db095
MS
3385 .splice_read = generic_file_splice_read,
3386 .splice_write = iter_file_splice_write,
59efec7b
TH
3387 .unlocked_ioctl = fuse_file_ioctl,
3388 .compat_ioctl = fuse_file_compat_ioctl,
95668a69 3389 .poll = fuse_file_poll,
05ba1f08 3390 .fallocate = fuse_file_fallocate,
d4136d60 3391 .copy_file_range = fuse_copy_file_range,
413ef8cb
MS
3392};
3393
f5e54d6e 3394static const struct address_space_operations fuse_file_aops = {
b6aeaded 3395 .readpage = fuse_readpage,
76a0294e 3396 .readahead = fuse_readahead,
3be5a52b 3397 .writepage = fuse_writepage,
26d614df 3398 .writepages = fuse_writepages,
3be5a52b 3399 .launder_page = fuse_launder_page,
3be5a52b 3400 .set_page_dirty = __set_page_dirty_nobuffers,
b2d2272f 3401 .bmap = fuse_bmap,
4273b793 3402 .direct_IO = fuse_direct_IO,
6b12c1b3
PE
3403 .write_begin = fuse_write_begin,
3404 .write_end = fuse_write_end,
b6aeaded
MS
3405};
3406
3407void fuse_init_file_inode(struct inode *inode)
3408{
ab2257e9
MS
3409 struct fuse_inode *fi = get_fuse_inode(inode);
3410
45323fb7
MS
3411 inode->i_fop = &fuse_file_operations;
3412 inode->i_data.a_ops = &fuse_file_aops;
ab2257e9
MS
3413
3414 INIT_LIST_HEAD(&fi->write_files);
3415 INIT_LIST_HEAD(&fi->queued_writes);
3416 fi->writectr = 0;
3417 init_waitqueue_head(&fi->page_waitq);
6b2fb799 3418 fi->writepages = RB_ROOT;
b6aeaded 3419}