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