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