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