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