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