fuse: fsync() did not return IO errors
[deliverable/linux.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
b111c8c0 420 req = fuse_get_req_nofail_nopages(fc, file);
b6aeaded
MS
421 memset(&inarg, 0, sizeof(inarg));
422 inarg.fh = ff->fh;
9c8ef561 423 inarg.lock_owner = fuse_lock_owner_id(fc, id);
b6aeaded
MS
424 req->in.h.opcode = FUSE_FLUSH;
425 req->in.h.nodeid = get_node_id(inode);
b6aeaded
MS
426 req->in.numargs = 1;
427 req->in.args[0].size = sizeof(inarg);
428 req->in.args[0].value = &inarg;
825d6d33 429 __set_bit(FR_FORCE, &req->flags);
b93f858a 430 fuse_request_send(fc, req);
b6aeaded
MS
431 err = req->out.h.error;
432 fuse_put_request(fc, req);
433 if (err == -ENOSYS) {
434 fc->no_flush = 1;
435 err = 0;
436 }
437 return err;
438}
439
02c24a82
JB
440int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
441 int datasync, int isdir)
b6aeaded 442{
7ea80859 443 struct inode *inode = file->f_mapping->host;
b6aeaded
MS
444 struct fuse_conn *fc = get_fuse_conn(inode);
445 struct fuse_file *ff = file->private_data;
7078187a 446 FUSE_ARGS(args);
b6aeaded
MS
447 struct fuse_fsync_in inarg;
448 int err;
449
248d86e8
MS
450 if (is_bad_inode(inode))
451 return -EIO;
452
5955102c 453 inode_lock(inode);
02c24a82 454
3be5a52b
MS
455 /*
456 * Start writeback against all dirty pages of the inode, then
457 * wait for all outstanding writes, before sending the FSYNC
458 * request.
459 */
22401e7b 460 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3be5a52b 461 if (err)
02c24a82 462 goto out;
3be5a52b
MS
463
464 fuse_sync_writes(inode);
ac7f052b
AK
465
466 /*
467 * Due to implementation of fuse writeback
468 * filemap_write_and_wait_range() does not catch errors.
469 * We have to do this directly after fuse_sync_writes()
470 */
471 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
472 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
473 err = -ENOSPC;
474 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
475 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
476 err = -EIO;
477 if (err)
478 goto out;
479
1e18bda8
MS
480 err = sync_inode_metadata(inode, 1);
481 if (err)
482 goto out;
3be5a52b 483
22401e7b
MS
484 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
485 goto out;
b0aa7606 486
b6aeaded
MS
487 memset(&inarg, 0, sizeof(inarg));
488 inarg.fh = ff->fh;
489 inarg.fsync_flags = datasync ? 1 : 0;
7078187a
MS
490 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
491 args.in.h.nodeid = get_node_id(inode);
492 args.in.numargs = 1;
493 args.in.args[0].size = sizeof(inarg);
494 args.in.args[0].value = &inarg;
495 err = fuse_simple_request(fc, &args);
b6aeaded 496 if (err == -ENOSYS) {
82547981
MS
497 if (isdir)
498 fc->no_fsyncdir = 1;
499 else
500 fc->no_fsync = 1;
b6aeaded
MS
501 err = 0;
502 }
02c24a82 503out:
5955102c 504 inode_unlock(inode);
b6aeaded
MS
505 return err;
506}
507
02c24a82
JB
508static int fuse_fsync(struct file *file, loff_t start, loff_t end,
509 int datasync)
82547981 510{
02c24a82 511 return fuse_fsync_common(file, start, end, datasync, 0);
82547981
MS
512}
513
2106cb18
MS
514void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
515 size_t count, int opcode)
b6aeaded 516{
5c5c5e51 517 struct fuse_read_in *inarg = &req->misc.read.in;
a6643094 518 struct fuse_file *ff = file->private_data;
b6aeaded 519
361b1eb5
MS
520 inarg->fh = ff->fh;
521 inarg->offset = pos;
522 inarg->size = count;
a6643094 523 inarg->flags = file->f_flags;
361b1eb5 524 req->in.h.opcode = opcode;
2106cb18 525 req->in.h.nodeid = ff->nodeid;
b6aeaded
MS
526 req->in.numargs = 1;
527 req->in.args[0].size = sizeof(struct fuse_read_in);
c1aa96a5 528 req->in.args[0].value = inarg;
b6aeaded
MS
529 req->out.argvar = 1;
530 req->out.numargs = 1;
531 req->out.args[0].size = count;
b6aeaded
MS
532}
533
187c5c36
MP
534static void fuse_release_user_pages(struct fuse_req *req, int write)
535{
536 unsigned i;
537
538 for (i = 0; i < req->num_pages; i++) {
539 struct page *page = req->pages[i];
540 if (write)
541 set_page_dirty_lock(page);
542 put_page(page);
543 }
544}
545
744742d6
SF
546static void fuse_io_release(struct kref *kref)
547{
548 kfree(container_of(kref, struct fuse_io_priv, refcnt));
549}
550
9d5722b7
CH
551static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
552{
553 if (io->err)
554 return io->err;
555
556 if (io->bytes >= 0 && io->write)
557 return -EIO;
558
559 return io->bytes < 0 ? io->size : io->bytes;
560}
561
01e9d11a
MP
562/**
563 * In case of short read, the caller sets 'pos' to the position of
564 * actual end of fuse request in IO request. Otherwise, if bytes_requested
565 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
566 *
567 * An example:
568 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
569 * both submitted asynchronously. The first of them was ACKed by userspace as
570 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
571 * second request was ACKed as short, e.g. only 1K was read, resulting in
572 * pos == 33K.
573 *
574 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
575 * will be equal to the length of the longest contiguous fragment of
576 * transferred data starting from the beginning of IO request.
577 */
578static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
579{
580 int left;
581
582 spin_lock(&io->lock);
583 if (err)
584 io->err = io->err ? : err;
585 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
586 io->bytes = pos;
587
588 left = --io->reqs;
7879c4e5 589 if (!left && io->blocking)
9d5722b7 590 complete(io->done);
01e9d11a
MP
591 spin_unlock(&io->lock);
592
7879c4e5 593 if (!left && !io->blocking) {
9d5722b7 594 ssize_t res = fuse_get_res_by_io(io);
01e9d11a 595
9d5722b7
CH
596 if (res >= 0) {
597 struct inode *inode = file_inode(io->iocb->ki_filp);
598 struct fuse_conn *fc = get_fuse_conn(inode);
599 struct fuse_inode *fi = get_fuse_inode(inode);
01e9d11a 600
9d5722b7
CH
601 spin_lock(&fc->lock);
602 fi->attr_version = ++fc->attr_version;
603 spin_unlock(&fc->lock);
01e9d11a
MP
604 }
605
04b2fa9f 606 io->iocb->ki_complete(io->iocb, res, 0);
01e9d11a 607 }
744742d6
SF
608
609 kref_put(&io->refcnt, fuse_io_release);
01e9d11a
MP
610}
611
612static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
613{
614 struct fuse_io_priv *io = req->io;
615 ssize_t pos = -1;
616
617 fuse_release_user_pages(req, !io->write);
618
619 if (io->write) {
620 if (req->misc.write.in.size != req->misc.write.out.size)
621 pos = req->misc.write.in.offset - io->offset +
622 req->misc.write.out.size;
623 } else {
624 if (req->misc.read.in.size != req->out.args[0].size)
625 pos = req->misc.read.in.offset - io->offset +
626 req->out.args[0].size;
627 }
628
629 fuse_aio_complete(io, req->out.h.error, pos);
630}
631
632static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
633 size_t num_bytes, struct fuse_io_priv *io)
634{
635 spin_lock(&io->lock);
744742d6 636 kref_get(&io->refcnt);
01e9d11a
MP
637 io->size += num_bytes;
638 io->reqs++;
639 spin_unlock(&io->lock);
640
641 req->io = io;
642 req->end = fuse_aio_complete_req;
643
36cf66ed 644 __fuse_get_request(req);
01e9d11a
MP
645 fuse_request_send_background(fc, req);
646
647 return num_bytes;
648}
649
36cf66ed 650static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
2106cb18 651 loff_t pos, size_t count, fl_owner_t owner)
04730fef 652{
36cf66ed 653 struct file *file = io->file;
2106cb18
MS
654 struct fuse_file *ff = file->private_data;
655 struct fuse_conn *fc = ff->fc;
f3332114 656
2106cb18 657 fuse_read_fill(req, file, pos, count, FUSE_READ);
f3332114 658 if (owner != NULL) {
5c5c5e51 659 struct fuse_read_in *inarg = &req->misc.read.in;
f3332114
MS
660
661 inarg->read_flags |= FUSE_READ_LOCKOWNER;
662 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
663 }
36cf66ed
MP
664
665 if (io->async)
666 return fuse_async_req_send(fc, req, count, io);
667
b93f858a 668 fuse_request_send(fc, req);
361b1eb5 669 return req->out.args[0].size;
04730fef
MS
670}
671
5c5c5e51
MS
672static void fuse_read_update_size(struct inode *inode, loff_t size,
673 u64 attr_ver)
674{
675 struct fuse_conn *fc = get_fuse_conn(inode);
676 struct fuse_inode *fi = get_fuse_inode(inode);
677
678 spin_lock(&fc->lock);
06a7c3c2
MP
679 if (attr_ver == fi->attr_version && size < inode->i_size &&
680 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
5c5c5e51
MS
681 fi->attr_version = ++fc->attr_version;
682 i_size_write(inode, size);
683 }
684 spin_unlock(&fc->lock);
685}
686
a92adc82
PE
687static void fuse_short_read(struct fuse_req *req, struct inode *inode,
688 u64 attr_ver)
689{
690 size_t num_read = req->out.args[0].size;
8373200b
PE
691 struct fuse_conn *fc = get_fuse_conn(inode);
692
693 if (fc->writeback_cache) {
694 /*
695 * A hole in a file. Some data after the hole are in page cache,
696 * but have not reached the client fs yet. So, the hole is not
697 * present there.
698 */
699 int i;
09cbfeaf
KS
700 int start_idx = num_read >> PAGE_SHIFT;
701 size_t off = num_read & (PAGE_SIZE - 1);
a92adc82 702
8373200b 703 for (i = start_idx; i < req->num_pages; i++) {
09cbfeaf 704 zero_user_segment(req->pages[i], off, PAGE_SIZE);
8373200b
PE
705 off = 0;
706 }
707 } else {
708 loff_t pos = page_offset(req->pages[0]) + num_read;
709 fuse_read_update_size(inode, pos, attr_ver);
710 }
a92adc82
PE
711}
712
482fce55 713static int fuse_do_readpage(struct file *file, struct page *page)
b6aeaded 714{
744742d6 715 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
b6aeaded
MS
716 struct inode *inode = page->mapping->host;
717 struct fuse_conn *fc = get_fuse_conn(inode);
248d86e8 718 struct fuse_req *req;
5c5c5e51
MS
719 size_t num_read;
720 loff_t pos = page_offset(page);
09cbfeaf 721 size_t count = PAGE_SIZE;
5c5c5e51 722 u64 attr_ver;
248d86e8
MS
723 int err;
724
3be5a52b 725 /*
25985edc 726 * Page writeback can extend beyond the lifetime of the
3be5a52b
MS
727 * page-cache page, so make sure we read a properly synced
728 * page.
729 */
730 fuse_wait_on_page_writeback(inode, page->index);
731
b111c8c0 732 req = fuse_get_req(fc, 1);
ce1d5a49 733 if (IS_ERR(req))
482fce55 734 return PTR_ERR(req);
b6aeaded 735
5c5c5e51
MS
736 attr_ver = fuse_get_attr_version(fc);
737
b6aeaded 738 req->out.page_zeroing = 1;
f4975c67 739 req->out.argpages = 1;
b6aeaded
MS
740 req->num_pages = 1;
741 req->pages[0] = page;
85f40aec 742 req->page_descs[0].length = count;
36cf66ed 743 num_read = fuse_send_read(req, &io, pos, count, NULL);
b6aeaded 744 err = req->out.h.error;
5c5c5e51
MS
745
746 if (!err) {
747 /*
748 * Short read means EOF. If file size is larger, truncate it
749 */
750 if (num_read < count)
a92adc82 751 fuse_short_read(req, inode, attr_ver);
5c5c5e51 752
b6aeaded 753 SetPageUptodate(page);
5c5c5e51
MS
754 }
755
a92adc82 756 fuse_put_request(fc, req);
482fce55
MP
757
758 return err;
759}
760
761static int fuse_readpage(struct file *file, struct page *page)
762{
763 struct inode *inode = page->mapping->host;
764 int err;
765
766 err = -EIO;
767 if (is_bad_inode(inode))
768 goto out;
769
770 err = fuse_do_readpage(file, page);
451418fc 771 fuse_invalidate_atime(inode);
b6aeaded
MS
772 out:
773 unlock_page(page);
774 return err;
775}
776
c1aa96a5 777static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
db50b96c 778{
c1aa96a5 779 int i;
5c5c5e51
MS
780 size_t count = req->misc.read.in.size;
781 size_t num_read = req->out.args[0].size;
ce534fb0 782 struct address_space *mapping = NULL;
c1aa96a5 783
ce534fb0
MS
784 for (i = 0; mapping == NULL && i < req->num_pages; i++)
785 mapping = req->pages[i]->mapping;
5c5c5e51 786
ce534fb0
MS
787 if (mapping) {
788 struct inode *inode = mapping->host;
789
790 /*
791 * Short read means EOF. If file size is larger, truncate it
792 */
a92adc82
PE
793 if (!req->out.h.error && num_read < count)
794 fuse_short_read(req, inode, req->misc.read.attr_ver);
ce534fb0 795
451418fc 796 fuse_invalidate_atime(inode);
ce534fb0 797 }
c1aa96a5 798
db50b96c
MS
799 for (i = 0; i < req->num_pages; i++) {
800 struct page *page = req->pages[i];
801 if (!req->out.h.error)
802 SetPageUptodate(page);
c1aa96a5
MS
803 else
804 SetPageError(page);
db50b96c 805 unlock_page(page);
09cbfeaf 806 put_page(page);
db50b96c 807 }
c756e0a4 808 if (req->ff)
5a18ec17 809 fuse_file_put(req->ff, false);
c1aa96a5
MS
810}
811
2106cb18 812static void fuse_send_readpages(struct fuse_req *req, struct file *file)
c1aa96a5 813{
2106cb18
MS
814 struct fuse_file *ff = file->private_data;
815 struct fuse_conn *fc = ff->fc;
c1aa96a5 816 loff_t pos = page_offset(req->pages[0]);
09cbfeaf 817 size_t count = req->num_pages << PAGE_SHIFT;
f4975c67
MS
818
819 req->out.argpages = 1;
c1aa96a5 820 req->out.page_zeroing = 1;
ce534fb0 821 req->out.page_replace = 1;
2106cb18 822 fuse_read_fill(req, file, pos, count, FUSE_READ);
5c5c5e51 823 req->misc.read.attr_ver = fuse_get_attr_version(fc);
9cd68455 824 if (fc->async_read) {
c756e0a4 825 req->ff = fuse_file_get(ff);
9cd68455 826 req->end = fuse_readpages_end;
b93f858a 827 fuse_request_send_background(fc, req);
9cd68455 828 } else {
b93f858a 829 fuse_request_send(fc, req);
9cd68455 830 fuse_readpages_end(fc, req);
e9bb09dd 831 fuse_put_request(fc, req);
9cd68455 832 }
db50b96c
MS
833}
834
c756e0a4 835struct fuse_fill_data {
db50b96c 836 struct fuse_req *req;
a6643094 837 struct file *file;
db50b96c 838 struct inode *inode;
f8dbdf81 839 unsigned nr_pages;
db50b96c
MS
840};
841
842static int fuse_readpages_fill(void *_data, struct page *page)
843{
c756e0a4 844 struct fuse_fill_data *data = _data;
db50b96c
MS
845 struct fuse_req *req = data->req;
846 struct inode *inode = data->inode;
847 struct fuse_conn *fc = get_fuse_conn(inode);
848
3be5a52b
MS
849 fuse_wait_on_page_writeback(inode, page->index);
850
db50b96c
MS
851 if (req->num_pages &&
852 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
09cbfeaf 853 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
db50b96c 854 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
f8dbdf81
MP
855 int nr_alloc = min_t(unsigned, data->nr_pages,
856 FUSE_MAX_PAGES_PER_REQ);
2106cb18 857 fuse_send_readpages(req, data->file);
8b41e671
MP
858 if (fc->async_read)
859 req = fuse_get_req_for_background(fc, nr_alloc);
860 else
861 req = fuse_get_req(fc, nr_alloc);
862
863 data->req = req;
ce1d5a49 864 if (IS_ERR(req)) {
db50b96c 865 unlock_page(page);
ce1d5a49 866 return PTR_ERR(req);
db50b96c 867 }
db50b96c 868 }
f8dbdf81
MP
869
870 if (WARN_ON(req->num_pages >= req->max_pages)) {
871 fuse_put_request(fc, req);
872 return -EIO;
873 }
874
09cbfeaf 875 get_page(page);
db50b96c 876 req->pages[req->num_pages] = page;
85f40aec 877 req->page_descs[req->num_pages].length = PAGE_SIZE;
1729a16c 878 req->num_pages++;
f8dbdf81 879 data->nr_pages--;
db50b96c
MS
880 return 0;
881}
882
883static int fuse_readpages(struct file *file, struct address_space *mapping,
884 struct list_head *pages, unsigned nr_pages)
885{
886 struct inode *inode = mapping->host;
887 struct fuse_conn *fc = get_fuse_conn(inode);
c756e0a4 888 struct fuse_fill_data data;
db50b96c 889 int err;
f8dbdf81 890 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
248d86e8 891
1d7ea732 892 err = -EIO;
248d86e8 893 if (is_bad_inode(inode))
2e990021 894 goto out;
248d86e8 895
a6643094 896 data.file = file;
db50b96c 897 data.inode = inode;
8b41e671
MP
898 if (fc->async_read)
899 data.req = fuse_get_req_for_background(fc, nr_alloc);
900 else
901 data.req = fuse_get_req(fc, nr_alloc);
f8dbdf81 902 data.nr_pages = nr_pages;
1d7ea732 903 err = PTR_ERR(data.req);
ce1d5a49 904 if (IS_ERR(data.req))
2e990021 905 goto out;
db50b96c
MS
906
907 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
d3406ffa
MS
908 if (!err) {
909 if (data.req->num_pages)
2106cb18 910 fuse_send_readpages(data.req, file);
d3406ffa
MS
911 else
912 fuse_put_request(fc, data.req);
913 }
2e990021 914out:
1d7ea732 915 return err;
db50b96c
MS
916}
917
37c20f16 918static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
bcb4be80
MS
919{
920 struct inode *inode = iocb->ki_filp->f_mapping->host;
a8894274 921 struct fuse_conn *fc = get_fuse_conn(inode);
bcb4be80 922
a8894274
BF
923 /*
924 * In auto invalidate mode, always update attributes on read.
925 * Otherwise, only update if we attempt to read past EOF (to ensure
926 * i_size is up to date).
927 */
928 if (fc->auto_inval_data ||
37c20f16 929 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
bcb4be80 930 int err;
bcb4be80
MS
931 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
932 if (err)
933 return err;
934 }
935
37c20f16 936 return generic_file_read_iter(iocb, to);
bcb4be80
MS
937}
938
2d698b07 939static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
2106cb18 940 loff_t pos, size_t count)
b6aeaded 941{
b25e82e5
MS
942 struct fuse_write_in *inarg = &req->misc.write.in;
943 struct fuse_write_out *outarg = &req->misc.write.out;
b6aeaded 944
b25e82e5
MS
945 inarg->fh = ff->fh;
946 inarg->offset = pos;
947 inarg->size = count;
b6aeaded 948 req->in.h.opcode = FUSE_WRITE;
2106cb18 949 req->in.h.nodeid = ff->nodeid;
b6aeaded 950 req->in.numargs = 2;
2106cb18 951 if (ff->fc->minor < 9)
f3332114
MS
952 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
953 else
954 req->in.args[0].size = sizeof(struct fuse_write_in);
b25e82e5 955 req->in.args[0].value = inarg;
b6aeaded
MS
956 req->in.args[1].size = count;
957 req->out.numargs = 1;
958 req->out.args[0].size = sizeof(struct fuse_write_out);
b25e82e5
MS
959 req->out.args[0].value = outarg;
960}
961
36cf66ed 962static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
2106cb18 963 loff_t pos, size_t count, fl_owner_t owner)
b25e82e5 964{
36cf66ed 965 struct file *file = io->file;
2106cb18
MS
966 struct fuse_file *ff = file->private_data;
967 struct fuse_conn *fc = ff->fc;
2d698b07
MS
968 struct fuse_write_in *inarg = &req->misc.write.in;
969
2106cb18 970 fuse_write_fill(req, ff, pos, count);
2d698b07 971 inarg->flags = file->f_flags;
f3332114 972 if (owner != NULL) {
f3332114
MS
973 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
974 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
975 }
36cf66ed
MP
976
977 if (io->async)
978 return fuse_async_req_send(fc, req, count, io);
979
b93f858a 980 fuse_request_send(fc, req);
b25e82e5 981 return req->misc.write.out.size;
b6aeaded
MS
982}
983
b0aa7606 984bool fuse_write_update_size(struct inode *inode, loff_t pos)
854512ec
MS
985{
986 struct fuse_conn *fc = get_fuse_conn(inode);
987 struct fuse_inode *fi = get_fuse_inode(inode);
b0aa7606 988 bool ret = false;
854512ec
MS
989
990 spin_lock(&fc->lock);
991 fi->attr_version = ++fc->attr_version;
b0aa7606 992 if (pos > inode->i_size) {
854512ec 993 i_size_write(inode, pos);
b0aa7606
MP
994 ret = true;
995 }
854512ec 996 spin_unlock(&fc->lock);
b0aa7606
MP
997
998 return ret;
854512ec
MS
999}
1000
ea9b9907
NP
1001static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1002 struct inode *inode, loff_t pos,
1003 size_t count)
1004{
1005 size_t res;
1006 unsigned offset;
1007 unsigned i;
744742d6 1008 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
ea9b9907
NP
1009
1010 for (i = 0; i < req->num_pages; i++)
1011 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1012
36cf66ed 1013 res = fuse_send_write(req, &io, pos, count, NULL);
ea9b9907 1014
b2430d75 1015 offset = req->page_descs[0].offset;
ea9b9907
NP
1016 count = res;
1017 for (i = 0; i < req->num_pages; i++) {
1018 struct page *page = req->pages[i];
1019
09cbfeaf 1020 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
ea9b9907
NP
1021 SetPageUptodate(page);
1022
09cbfeaf
KS
1023 if (count > PAGE_SIZE - offset)
1024 count -= PAGE_SIZE - offset;
ea9b9907
NP
1025 else
1026 count = 0;
1027 offset = 0;
1028
1029 unlock_page(page);
09cbfeaf 1030 put_page(page);
ea9b9907
NP
1031 }
1032
1033 return res;
1034}
1035
1036static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1037 struct address_space *mapping,
1038 struct iov_iter *ii, loff_t pos)
1039{
1040 struct fuse_conn *fc = get_fuse_conn(mapping->host);
09cbfeaf 1041 unsigned offset = pos & (PAGE_SIZE - 1);
ea9b9907
NP
1042 size_t count = 0;
1043 int err;
1044
f4975c67 1045 req->in.argpages = 1;
b2430d75 1046 req->page_descs[0].offset = offset;
ea9b9907
NP
1047
1048 do {
1049 size_t tmp;
1050 struct page *page;
09cbfeaf
KS
1051 pgoff_t index = pos >> PAGE_SHIFT;
1052 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
ea9b9907
NP
1053 iov_iter_count(ii));
1054
1055 bytes = min_t(size_t, bytes, fc->max_write - count);
1056
1057 again:
1058 err = -EFAULT;
1059 if (iov_iter_fault_in_readable(ii, bytes))
1060 break;
1061
1062 err = -ENOMEM;
54566b2c 1063 page = grab_cache_page_write_begin(mapping, index, 0);
ea9b9907
NP
1064 if (!page)
1065 break;
1066
931e80e4 1067 if (mapping_writably_mapped(mapping))
1068 flush_dcache_page(page);
1069
ea9b9907 1070 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
ea9b9907
NP
1071 flush_dcache_page(page);
1072
3ca8138f 1073 iov_iter_advance(ii, tmp);
ea9b9907
NP
1074 if (!tmp) {
1075 unlock_page(page);
09cbfeaf 1076 put_page(page);
ea9b9907
NP
1077 bytes = min(bytes, iov_iter_single_seg_count(ii));
1078 goto again;
1079 }
1080
1081 err = 0;
1082 req->pages[req->num_pages] = page;
85f40aec 1083 req->page_descs[req->num_pages].length = tmp;
ea9b9907
NP
1084 req->num_pages++;
1085
ea9b9907
NP
1086 count += tmp;
1087 pos += tmp;
1088 offset += tmp;
09cbfeaf 1089 if (offset == PAGE_SIZE)
ea9b9907
NP
1090 offset = 0;
1091
78bb6cb9
MS
1092 if (!fc->big_writes)
1093 break;
ea9b9907 1094 } while (iov_iter_count(ii) && count < fc->max_write &&
d07f09f5 1095 req->num_pages < req->max_pages && offset == 0);
ea9b9907
NP
1096
1097 return count > 0 ? count : err;
1098}
1099
d07f09f5
MP
1100static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1101{
1102 return min_t(unsigned,
09cbfeaf
KS
1103 ((pos + len - 1) >> PAGE_SHIFT) -
1104 (pos >> PAGE_SHIFT) + 1,
d07f09f5
MP
1105 FUSE_MAX_PAGES_PER_REQ);
1106}
1107
ea9b9907
NP
1108static ssize_t fuse_perform_write(struct file *file,
1109 struct address_space *mapping,
1110 struct iov_iter *ii, loff_t pos)
1111{
1112 struct inode *inode = mapping->host;
1113 struct fuse_conn *fc = get_fuse_conn(inode);
06a7c3c2 1114 struct fuse_inode *fi = get_fuse_inode(inode);
ea9b9907
NP
1115 int err = 0;
1116 ssize_t res = 0;
1117
1118 if (is_bad_inode(inode))
1119 return -EIO;
1120
06a7c3c2
MP
1121 if (inode->i_size < pos + iov_iter_count(ii))
1122 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1123
ea9b9907
NP
1124 do {
1125 struct fuse_req *req;
1126 ssize_t count;
d07f09f5 1127 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
ea9b9907 1128
d07f09f5 1129 req = fuse_get_req(fc, nr_pages);
ea9b9907
NP
1130 if (IS_ERR(req)) {
1131 err = PTR_ERR(req);
1132 break;
1133 }
1134
1135 count = fuse_fill_write_pages(req, mapping, ii, pos);
1136 if (count <= 0) {
1137 err = count;
1138 } else {
1139 size_t num_written;
1140
1141 num_written = fuse_send_write_pages(req, file, inode,
1142 pos, count);
1143 err = req->out.h.error;
1144 if (!err) {
1145 res += num_written;
1146 pos += num_written;
1147
1148 /* break out of the loop on short write */
1149 if (num_written != count)
1150 err = -EIO;
1151 }
1152 }
1153 fuse_put_request(fc, req);
1154 } while (!err && iov_iter_count(ii));
1155
1156 if (res > 0)
1157 fuse_write_update_size(inode, pos);
1158
06a7c3c2 1159 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
ea9b9907
NP
1160 fuse_invalidate_attr(inode);
1161
1162 return res > 0 ? res : err;
1163}
1164
84c3d55c 1165static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
ea9b9907
NP
1166{
1167 struct file *file = iocb->ki_filp;
1168 struct address_space *mapping = file->f_mapping;
ea9b9907 1169 ssize_t written = 0;
4273b793 1170 ssize_t written_buffered = 0;
ea9b9907
NP
1171 struct inode *inode = mapping->host;
1172 ssize_t err;
4273b793 1173 loff_t endbyte = 0;
ea9b9907 1174
4d99ff8f
PE
1175 if (get_fuse_conn(inode)->writeback_cache) {
1176 /* Update size (EOF optimization) and mode (SUID clearing) */
1177 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1178 if (err)
1179 return err;
1180
84c3d55c 1181 return generic_file_write_iter(iocb, from);
4d99ff8f
PE
1182 }
1183
5955102c 1184 inode_lock(inode);
ea9b9907
NP
1185
1186 /* We can write back this queue in page reclaim */
de1414a6 1187 current->backing_dev_info = inode_to_bdi(inode);
ea9b9907 1188
3309dd04
AV
1189 err = generic_write_checks(iocb, from);
1190 if (err <= 0)
ea9b9907
NP
1191 goto out;
1192
5fa8e0a1 1193 err = file_remove_privs(file);
ea9b9907
NP
1194 if (err)
1195 goto out;
1196
c3b2da31
JB
1197 err = file_update_time(file);
1198 if (err)
1199 goto out;
ea9b9907 1200
2ba48ce5 1201 if (iocb->ki_flags & IOCB_DIRECT) {
3309dd04 1202 loff_t pos = iocb->ki_pos;
1af5bb49 1203 written = generic_file_direct_write(iocb, from);
84c3d55c 1204 if (written < 0 || !iov_iter_count(from))
4273b793
AA
1205 goto out;
1206
1207 pos += written;
ea9b9907 1208
84c3d55c 1209 written_buffered = fuse_perform_write(file, mapping, from, pos);
4273b793
AA
1210 if (written_buffered < 0) {
1211 err = written_buffered;
1212 goto out;
1213 }
1214 endbyte = pos + written_buffered - 1;
1215
1216 err = filemap_write_and_wait_range(file->f_mapping, pos,
1217 endbyte);
1218 if (err)
1219 goto out;
1220
1221 invalidate_mapping_pages(file->f_mapping,
09cbfeaf
KS
1222 pos >> PAGE_SHIFT,
1223 endbyte >> PAGE_SHIFT);
4273b793
AA
1224
1225 written += written_buffered;
1226 iocb->ki_pos = pos + written_buffered;
1227 } else {
3309dd04 1228 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
4273b793 1229 if (written >= 0)
3309dd04 1230 iocb->ki_pos += written;
4273b793 1231 }
ea9b9907
NP
1232out:
1233 current->backing_dev_info = NULL;
5955102c 1234 inode_unlock(inode);
ea9b9907
NP
1235
1236 return written ? written : err;
1237}
1238
7c190c8b
MP
1239static inline void fuse_page_descs_length_init(struct fuse_req *req,
1240 unsigned index, unsigned nr_pages)
85f40aec
MP
1241{
1242 int i;
1243
7c190c8b 1244 for (i = index; i < index + nr_pages; i++)
85f40aec
MP
1245 req->page_descs[i].length = PAGE_SIZE -
1246 req->page_descs[i].offset;
1247}
1248
7c190c8b
MP
1249static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1250{
1251 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1252}
1253
1254static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1255 size_t max_size)
1256{
1257 return min(iov_iter_single_seg_count(ii), max_size);
1258}
1259
b98d023a 1260static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
ce60a2f1 1261 size_t *nbytesp, int write)
413ef8cb 1262{
7c190c8b 1263 size_t nbytes = 0; /* # bytes already packed in req */
742f9927 1264 ssize_t ret = 0;
b98d023a 1265
f4975c67 1266 /* Special case for kernel I/O: can copy directly into the buffer */
62a8067a 1267 if (ii->type & ITER_KVEC) {
7c190c8b
MP
1268 unsigned long user_addr = fuse_get_user_addr(ii);
1269 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1270
f4975c67
MS
1271 if (write)
1272 req->in.args[1].value = (void *) user_addr;
1273 else
1274 req->out.args[0].value = (void *) user_addr;
1275
b98d023a
MP
1276 iov_iter_advance(ii, frag_size);
1277 *nbytesp = frag_size;
f4975c67
MS
1278 return 0;
1279 }
413ef8cb 1280
5565a9d8 1281 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
7c190c8b 1282 unsigned npages;
f67da30c 1283 size_t start;
742f9927 1284 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
2c80929c 1285 *nbytesp - nbytes,
c7f3888a
AV
1286 req->max_pages - req->num_pages,
1287 &start);
7c190c8b 1288 if (ret < 0)
742f9927 1289 break;
7c190c8b 1290
c9c37e2e
AV
1291 iov_iter_advance(ii, ret);
1292 nbytes += ret;
7c190c8b 1293
c9c37e2e
AV
1294 ret += start;
1295 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
7c190c8b 1296
c9c37e2e 1297 req->page_descs[req->num_pages].offset = start;
7c190c8b
MP
1298 fuse_page_descs_length_init(req, req->num_pages, npages);
1299
1300 req->num_pages += npages;
1301 req->page_descs[req->num_pages - 1].length -=
c9c37e2e 1302 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
7c190c8b 1303 }
f4975c67
MS
1304
1305 if (write)
1306 req->in.argpages = 1;
1307 else
1308 req->out.argpages = 1;
1309
7c190c8b 1310 *nbytesp = nbytes;
f4975c67 1311
2c932d4c 1312 return ret < 0 ? ret : 0;
413ef8cb
MS
1313}
1314
5565a9d8
MP
1315static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1316{
f67da30c 1317 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
5565a9d8
MP
1318}
1319
d22a943f
AV
1320ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1321 loff_t *ppos, int flags)
413ef8cb 1322{
ea8cd333
PE
1323 int write = flags & FUSE_DIO_WRITE;
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
MP
1366 if (!io->async)
1367 fuse_release_user_pages(req, !write);
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);
385b1268 1469 dec_zone_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);
3be5a52b 1659 inc_zone_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);
8b284dc4 1773 dec_zone_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);
26d614df 1872 inc_zone_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
1988 if (!PageUptodate(page)) {
1989 /* Zero any unwritten bytes at the end of the page */
09cbfeaf 1990 size_t endoff = (pos + copied) & ~PAGE_MASK;
6b12c1b3 1991 if (endoff)
09cbfeaf 1992 zero_user_segment(page, endoff, PAGE_SIZE);
6b12c1b3
PE
1993 SetPageUptodate(page);
1994 }
1995
1996 fuse_write_update_size(inode, pos + copied);
1997 set_page_dirty(page);
1998 unlock_page(page);
09cbfeaf 1999 put_page(page);
6b12c1b3
PE
2000
2001 return copied;
2002}
2003
3be5a52b
MS
2004static int fuse_launder_page(struct page *page)
2005{
2006 int err = 0;
2007 if (clear_page_dirty_for_io(page)) {
2008 struct inode *inode = page->mapping->host;
2009 err = fuse_writepage_locked(page);
2010 if (!err)
2011 fuse_wait_on_page_writeback(inode, page->index);
2012 }
2013 return err;
2014}
2015
2016/*
2017 * Write back dirty pages now, because there may not be any suitable
2018 * open files later
2019 */
2020static void fuse_vma_close(struct vm_area_struct *vma)
2021{
2022 filemap_write_and_wait(vma->vm_file->f_mapping);
2023}
2024
2025/*
2026 * Wait for writeback against this page to complete before allowing it
2027 * to be marked dirty again, and hence written back again, possibly
2028 * before the previous writepage completed.
2029 *
2030 * Block here, instead of in ->writepage(), so that the userspace fs
2031 * can only block processes actually operating on the filesystem.
2032 *
2033 * Otherwise unprivileged userspace fs would be able to block
2034 * unrelated:
2035 *
2036 * - page migration
2037 * - sync(2)
2038 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2039 */
c2ec175c 2040static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
3be5a52b 2041{
c2ec175c 2042 struct page *page = vmf->page;
cca24370
MS
2043 struct inode *inode = file_inode(vma->vm_file);
2044
2045 file_update_time(vma->vm_file);
2046 lock_page(page);
2047 if (page->mapping != inode->i_mapping) {
2048 unlock_page(page);
2049 return VM_FAULT_NOPAGE;
2050 }
3be5a52b
MS
2051
2052 fuse_wait_on_page_writeback(inode, page->index);
cca24370 2053 return VM_FAULT_LOCKED;
3be5a52b
MS
2054}
2055
f0f37e2f 2056static const struct vm_operations_struct fuse_file_vm_ops = {
3be5a52b
MS
2057 .close = fuse_vma_close,
2058 .fault = filemap_fault,
f1820361 2059 .map_pages = filemap_map_pages,
3be5a52b
MS
2060 .page_mkwrite = fuse_page_mkwrite,
2061};
2062
2063static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2064{
650b22b9
PE
2065 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2066 fuse_link_write_file(file);
2067
3be5a52b
MS
2068 file_accessed(file);
2069 vma->vm_ops = &fuse_file_vm_ops;
b6aeaded
MS
2070 return 0;
2071}
2072
fc280c96
MS
2073static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2074{
2075 /* Can't provide the coherency needed for MAP_SHARED */
2076 if (vma->vm_flags & VM_MAYSHARE)
2077 return -ENODEV;
2078
3121bfe7
MS
2079 invalidate_inode_pages2(file->f_mapping);
2080
fc280c96
MS
2081 return generic_file_mmap(file, vma);
2082}
2083
71421259
MS
2084static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2085 struct file_lock *fl)
2086{
2087 switch (ffl->type) {
2088 case F_UNLCK:
2089 break;
2090
2091 case F_RDLCK:
2092 case F_WRLCK:
2093 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2094 ffl->end < ffl->start)
2095 return -EIO;
2096
2097 fl->fl_start = ffl->start;
2098 fl->fl_end = ffl->end;
2099 fl->fl_pid = ffl->pid;
2100 break;
2101
2102 default:
2103 return -EIO;
2104 }
2105 fl->fl_type = ffl->type;
2106 return 0;
2107}
2108
7078187a 2109static void fuse_lk_fill(struct fuse_args *args, struct file *file,
a9ff4f87 2110 const struct file_lock *fl, int opcode, pid_t pid,
7078187a 2111 int flock, struct fuse_lk_in *inarg)
71421259 2112{
6131ffaa 2113 struct inode *inode = file_inode(file);
9c8ef561 2114 struct fuse_conn *fc = get_fuse_conn(inode);
71421259 2115 struct fuse_file *ff = file->private_data;
7078187a
MS
2116
2117 memset(inarg, 0, sizeof(*inarg));
2118 inarg->fh = ff->fh;
2119 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2120 inarg->lk.start = fl->fl_start;
2121 inarg->lk.end = fl->fl_end;
2122 inarg->lk.type = fl->fl_type;
2123 inarg->lk.pid = pid;
a9ff4f87 2124 if (flock)
7078187a
MS
2125 inarg->lk_flags |= FUSE_LK_FLOCK;
2126 args->in.h.opcode = opcode;
2127 args->in.h.nodeid = get_node_id(inode);
2128 args->in.numargs = 1;
2129 args->in.args[0].size = sizeof(*inarg);
2130 args->in.args[0].value = inarg;
71421259
MS
2131}
2132
2133static int fuse_getlk(struct file *file, struct file_lock *fl)
2134{
6131ffaa 2135 struct inode *inode = file_inode(file);
71421259 2136 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a
MS
2137 FUSE_ARGS(args);
2138 struct fuse_lk_in inarg;
71421259
MS
2139 struct fuse_lk_out outarg;
2140 int err;
2141
7078187a
MS
2142 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2143 args.out.numargs = 1;
2144 args.out.args[0].size = sizeof(outarg);
2145 args.out.args[0].value = &outarg;
2146 err = fuse_simple_request(fc, &args);
71421259
MS
2147 if (!err)
2148 err = convert_fuse_file_lock(&outarg.lk, fl);
2149
2150 return err;
2151}
2152
a9ff4f87 2153static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
71421259 2154{
6131ffaa 2155 struct inode *inode = file_inode(file);
71421259 2156 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a
MS
2157 FUSE_ARGS(args);
2158 struct fuse_lk_in inarg;
71421259
MS
2159 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2160 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2161 int err;
2162
8fb47a4f 2163 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
48e90761
MS
2164 /* NLM needs asynchronous locks, which we don't support yet */
2165 return -ENOLCK;
2166 }
2167
71421259
MS
2168 /* Unlock on close is handled by the flush method */
2169 if (fl->fl_flags & FL_CLOSE)
2170 return 0;
2171
7078187a
MS
2172 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2173 err = fuse_simple_request(fc, &args);
71421259 2174
a4d27e75
MS
2175 /* locking is restartable */
2176 if (err == -EINTR)
2177 err = -ERESTARTSYS;
7078187a 2178
71421259
MS
2179 return err;
2180}
2181
2182static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2183{
6131ffaa 2184 struct inode *inode = file_inode(file);
71421259
MS
2185 struct fuse_conn *fc = get_fuse_conn(inode);
2186 int err;
2187
48e90761
MS
2188 if (cmd == F_CANCELLK) {
2189 err = 0;
2190 } else if (cmd == F_GETLK) {
71421259 2191 if (fc->no_lock) {
9d6a8c5c 2192 posix_test_lock(file, fl);
71421259
MS
2193 err = 0;
2194 } else
2195 err = fuse_getlk(file, fl);
2196 } else {
2197 if (fc->no_lock)
48e90761 2198 err = posix_lock_file(file, fl, NULL);
71421259 2199 else
a9ff4f87 2200 err = fuse_setlk(file, fl, 0);
71421259
MS
2201 }
2202 return err;
2203}
2204
a9ff4f87
MS
2205static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2206{
6131ffaa 2207 struct inode *inode = file_inode(file);
a9ff4f87
MS
2208 struct fuse_conn *fc = get_fuse_conn(inode);
2209 int err;
2210
37fb3a30 2211 if (fc->no_flock) {
4f656367 2212 err = locks_lock_file_wait(file, fl);
a9ff4f87 2213 } else {
37fb3a30
MS
2214 struct fuse_file *ff = file->private_data;
2215
a9ff4f87 2216 /* emulate flock with POSIX locks */
37fb3a30 2217 ff->flock = true;
a9ff4f87
MS
2218 err = fuse_setlk(file, fl, 1);
2219 }
2220
2221 return err;
2222}
2223
b2d2272f
MS
2224static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2225{
2226 struct inode *inode = mapping->host;
2227 struct fuse_conn *fc = get_fuse_conn(inode);
7078187a 2228 FUSE_ARGS(args);
b2d2272f
MS
2229 struct fuse_bmap_in inarg;
2230 struct fuse_bmap_out outarg;
2231 int err;
2232
2233 if (!inode->i_sb->s_bdev || fc->no_bmap)
2234 return 0;
2235
b2d2272f
MS
2236 memset(&inarg, 0, sizeof(inarg));
2237 inarg.block = block;
2238 inarg.blocksize = inode->i_sb->s_blocksize;
7078187a
MS
2239 args.in.h.opcode = FUSE_BMAP;
2240 args.in.h.nodeid = get_node_id(inode);
2241 args.in.numargs = 1;
2242 args.in.args[0].size = sizeof(inarg);
2243 args.in.args[0].value = &inarg;
2244 args.out.numargs = 1;
2245 args.out.args[0].size = sizeof(outarg);
2246 args.out.args[0].value = &outarg;
2247 err = fuse_simple_request(fc, &args);
b2d2272f
MS
2248 if (err == -ENOSYS)
2249 fc->no_bmap = 1;
2250
2251 return err ? 0 : outarg.block;
2252}
2253
0b5da8db
R
2254static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2255{
2256 struct inode *inode = file->f_mapping->host;
2257 struct fuse_conn *fc = get_fuse_conn(inode);
2258 struct fuse_file *ff = file->private_data;
2259 FUSE_ARGS(args);
2260 struct fuse_lseek_in inarg = {
2261 .fh = ff->fh,
2262 .offset = offset,
2263 .whence = whence
2264 };
2265 struct fuse_lseek_out outarg;
2266 int err;
2267
2268 if (fc->no_lseek)
2269 goto fallback;
2270
2271 args.in.h.opcode = FUSE_LSEEK;
2272 args.in.h.nodeid = ff->nodeid;
2273 args.in.numargs = 1;
2274 args.in.args[0].size = sizeof(inarg);
2275 args.in.args[0].value = &inarg;
2276 args.out.numargs = 1;
2277 args.out.args[0].size = sizeof(outarg);
2278 args.out.args[0].value = &outarg;
2279 err = fuse_simple_request(fc, &args);
2280 if (err) {
2281 if (err == -ENOSYS) {
2282 fc->no_lseek = 1;
2283 goto fallback;
2284 }
2285 return err;
2286 }
2287
2288 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2289
2290fallback:
2291 err = fuse_update_attributes(inode, NULL, file, NULL);
2292 if (!err)
2293 return generic_file_llseek(file, offset, whence);
2294 else
2295 return err;
2296}
2297
965c8e59 2298static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
5559b8f4
MS
2299{
2300 loff_t retval;
6131ffaa 2301 struct inode *inode = file_inode(file);
5559b8f4 2302
0b5da8db
R
2303 switch (whence) {
2304 case SEEK_SET:
2305 case SEEK_CUR:
2306 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
965c8e59 2307 retval = generic_file_llseek(file, offset, whence);
0b5da8db
R
2308 break;
2309 case SEEK_END:
5955102c 2310 inode_lock(inode);
0b5da8db
R
2311 retval = fuse_update_attributes(inode, NULL, file, NULL);
2312 if (!retval)
2313 retval = generic_file_llseek(file, offset, whence);
5955102c 2314 inode_unlock(inode);
0b5da8db
R
2315 break;
2316 case SEEK_HOLE:
2317 case SEEK_DATA:
5955102c 2318 inode_lock(inode);
0b5da8db 2319 retval = fuse_lseek(file, offset, whence);
5955102c 2320 inode_unlock(inode);
0b5da8db
R
2321 break;
2322 default:
2323 retval = -EINVAL;
2324 }
c07c3d19 2325
5559b8f4
MS
2326 return retval;
2327}
2328
59efec7b
TH
2329static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2330 unsigned int nr_segs, size_t bytes, bool to_user)
2331{
2332 struct iov_iter ii;
2333 int page_idx = 0;
2334
2335 if (!bytes)
2336 return 0;
2337
71d8e532 2338 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
59efec7b
TH
2339
2340 while (iov_iter_count(&ii)) {
2341 struct page *page = pages[page_idx++];
2342 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
4aa0edd2 2343 void *kaddr;
59efec7b 2344
4aa0edd2 2345 kaddr = kmap(page);
59efec7b
TH
2346
2347 while (todo) {
2348 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2349 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2350 size_t copy = min(todo, iov_len);
2351 size_t left;
2352
2353 if (!to_user)
2354 left = copy_from_user(kaddr, uaddr, copy);
2355 else
2356 left = copy_to_user(uaddr, kaddr, copy);
2357
2358 if (unlikely(left))
2359 return -EFAULT;
2360
2361 iov_iter_advance(&ii, copy);
2362 todo -= copy;
2363 kaddr += copy;
2364 }
2365
0bd87182 2366 kunmap(page);
59efec7b
TH
2367 }
2368
2369 return 0;
2370}
2371
d9d318d3
MS
2372/*
2373 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2374 * ABI was defined to be 'struct iovec' which is different on 32bit
2375 * and 64bit. Fortunately we can determine which structure the server
2376 * used from the size of the reply.
2377 */
1baa26b2
MS
2378static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2379 size_t transferred, unsigned count,
2380 bool is_compat)
d9d318d3
MS
2381{
2382#ifdef CONFIG_COMPAT
2383 if (count * sizeof(struct compat_iovec) == transferred) {
2384 struct compat_iovec *ciov = src;
2385 unsigned i;
2386
2387 /*
2388 * With this interface a 32bit server cannot support
2389 * non-compat (i.e. ones coming from 64bit apps) ioctl
2390 * requests
2391 */
2392 if (!is_compat)
2393 return -EINVAL;
2394
2395 for (i = 0; i < count; i++) {
2396 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2397 dst[i].iov_len = ciov[i].iov_len;
2398 }
2399 return 0;
2400 }
2401#endif
2402
2403 if (count * sizeof(struct iovec) != transferred)
2404 return -EIO;
2405
2406 memcpy(dst, src, transferred);
2407 return 0;
2408}
2409
7572777e
MS
2410/* Make sure iov_length() won't overflow */
2411static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2412{
2413 size_t n;
2414 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2415
fb6ccff6 2416 for (n = 0; n < count; n++, iov++) {
7572777e
MS
2417 if (iov->iov_len > (size_t) max)
2418 return -ENOMEM;
2419 max -= iov->iov_len;
2420 }
2421 return 0;
2422}
2423
1baa26b2
MS
2424static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2425 void *src, size_t transferred, unsigned count,
2426 bool is_compat)
2427{
2428 unsigned i;
2429 struct fuse_ioctl_iovec *fiov = src;
2430
2431 if (fc->minor < 16) {
2432 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2433 count, is_compat);
2434 }
2435
2436 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2437 return -EIO;
2438
2439 for (i = 0; i < count; i++) {
2440 /* Did the server supply an inappropriate value? */
2441 if (fiov[i].base != (unsigned long) fiov[i].base ||
2442 fiov[i].len != (unsigned long) fiov[i].len)
2443 return -EIO;
2444
2445 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2446 dst[i].iov_len = (size_t) fiov[i].len;
2447
2448#ifdef CONFIG_COMPAT
2449 if (is_compat &&
2450 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2451 (compat_size_t) dst[i].iov_len != fiov[i].len))
2452 return -EIO;
2453#endif
2454 }
2455
2456 return 0;
2457}
2458
2459
59efec7b
TH
2460/*
2461 * For ioctls, there is no generic way to determine how much memory
2462 * needs to be read and/or written. Furthermore, ioctls are allowed
2463 * to dereference the passed pointer, so the parameter requires deep
2464 * copying but FUSE has no idea whatsoever about what to copy in or
2465 * out.
2466 *
2467 * This is solved by allowing FUSE server to retry ioctl with
2468 * necessary in/out iovecs. Let's assume the ioctl implementation
2469 * needs to read in the following structure.
2470 *
2471 * struct a {
2472 * char *buf;
2473 * size_t buflen;
2474 * }
2475 *
2476 * On the first callout to FUSE server, inarg->in_size and
2477 * inarg->out_size will be NULL; then, the server completes the ioctl
2478 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2479 * the actual iov array to
2480 *
2481 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2482 *
2483 * which tells FUSE to copy in the requested area and retry the ioctl.
2484 * On the second round, the server has access to the structure and
2485 * from that it can tell what to look for next, so on the invocation,
2486 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2487 *
2488 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2489 * { .iov_base = a.buf, .iov_len = a.buflen } }
2490 *
2491 * FUSE will copy both struct a and the pointed buffer from the
2492 * process doing the ioctl and retry ioctl with both struct a and the
2493 * buffer.
2494 *
2495 * This time, FUSE server has everything it needs and completes ioctl
2496 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2497 *
2498 * Copying data out works the same way.
2499 *
2500 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2501 * automatically initializes in and out iovs by decoding @cmd with
2502 * _IOC_* macros and the server is not allowed to request RETRY. This
2503 * limits ioctl data transfers to well-formed ioctls and is the forced
2504 * behavior for all FUSE servers.
2505 */
08cbf542
TH
2506long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2507 unsigned int flags)
59efec7b 2508{
59efec7b 2509 struct fuse_file *ff = file->private_data;
d36f2487 2510 struct fuse_conn *fc = ff->fc;
59efec7b
TH
2511 struct fuse_ioctl_in inarg = {
2512 .fh = ff->fh,
2513 .cmd = cmd,
2514 .arg = arg,
2515 .flags = flags
2516 };
2517 struct fuse_ioctl_out outarg;
2518 struct fuse_req *req = NULL;
2519 struct page **pages = NULL;
8ac83505 2520 struct iovec *iov_page = NULL;
59efec7b
TH
2521 struct iovec *in_iov = NULL, *out_iov = NULL;
2522 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2523 size_t in_size, out_size, transferred;
2524 int err;
2525
1baa26b2
MS
2526#if BITS_PER_LONG == 32
2527 inarg.flags |= FUSE_IOCTL_32BIT;
2528#else
2529 if (flags & FUSE_IOCTL_COMPAT)
2530 inarg.flags |= FUSE_IOCTL_32BIT;
2531#endif
2532
59efec7b 2533 /* assume all the iovs returned by client always fits in a page */
1baa26b2 2534 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
59efec7b 2535
59efec7b 2536 err = -ENOMEM;
c411cc88 2537 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
8ac83505 2538 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
59efec7b
TH
2539 if (!pages || !iov_page)
2540 goto out;
2541
2542 /*
2543 * If restricted, initialize IO parameters as encoded in @cmd.
2544 * RETRY from server is not allowed.
2545 */
2546 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
8ac83505 2547 struct iovec *iov = iov_page;
59efec7b 2548
c9f0523d 2549 iov->iov_base = (void __user *)arg;
59efec7b
TH
2550 iov->iov_len = _IOC_SIZE(cmd);
2551
2552 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2553 in_iov = iov;
2554 in_iovs = 1;
2555 }
2556
2557 if (_IOC_DIR(cmd) & _IOC_READ) {
2558 out_iov = iov;
2559 out_iovs = 1;
2560 }
2561 }
2562
2563 retry:
2564 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2565 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2566
2567 /*
2568 * Out data can be used either for actual out data or iovs,
2569 * make sure there always is at least one page.
2570 */
2571 out_size = max_t(size_t, out_size, PAGE_SIZE);
2572 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2573
2574 /* make sure there are enough buffer pages and init request with them */
2575 err = -ENOMEM;
2576 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2577 goto out;
2578 while (num_pages < max_pages) {
2579 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2580 if (!pages[num_pages])
2581 goto out;
2582 num_pages++;
2583 }
2584
54b96670 2585 req = fuse_get_req(fc, num_pages);
59efec7b
TH
2586 if (IS_ERR(req)) {
2587 err = PTR_ERR(req);
2588 req = NULL;
2589 goto out;
2590 }
2591 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2592 req->num_pages = num_pages;
7c190c8b 2593 fuse_page_descs_length_init(req, 0, req->num_pages);
59efec7b
TH
2594
2595 /* okay, let's send it to the client */
2596 req->in.h.opcode = FUSE_IOCTL;
d36f2487 2597 req->in.h.nodeid = ff->nodeid;
59efec7b
TH
2598 req->in.numargs = 1;
2599 req->in.args[0].size = sizeof(inarg);
2600 req->in.args[0].value = &inarg;
2601 if (in_size) {
2602 req->in.numargs++;
2603 req->in.args[1].size = in_size;
2604 req->in.argpages = 1;
2605
2606 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2607 false);
2608 if (err)
2609 goto out;
2610 }
2611
2612 req->out.numargs = 2;
2613 req->out.args[0].size = sizeof(outarg);
2614 req->out.args[0].value = &outarg;
2615 req->out.args[1].size = out_size;
2616 req->out.argpages = 1;
2617 req->out.argvar = 1;
2618
b93f858a 2619 fuse_request_send(fc, req);
59efec7b
TH
2620 err = req->out.h.error;
2621 transferred = req->out.args[1].size;
2622 fuse_put_request(fc, req);
2623 req = NULL;
2624 if (err)
2625 goto out;
2626
2627 /* did it ask for retry? */
2628 if (outarg.flags & FUSE_IOCTL_RETRY) {
8ac83505 2629 void *vaddr;
59efec7b
TH
2630
2631 /* no retry if in restricted mode */
2632 err = -EIO;
2633 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2634 goto out;
2635
2636 in_iovs = outarg.in_iovs;
2637 out_iovs = outarg.out_iovs;
2638
2639 /*
2640 * Make sure things are in boundary, separate checks
2641 * are to protect against overflow.
2642 */
2643 err = -ENOMEM;
2644 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2645 out_iovs > FUSE_IOCTL_MAX_IOV ||
2646 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2647 goto out;
2648
2408f6ef 2649 vaddr = kmap_atomic(pages[0]);
1baa26b2 2650 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
d9d318d3
MS
2651 transferred, in_iovs + out_iovs,
2652 (flags & FUSE_IOCTL_COMPAT) != 0);
2408f6ef 2653 kunmap_atomic(vaddr);
d9d318d3
MS
2654 if (err)
2655 goto out;
59efec7b 2656
8ac83505 2657 in_iov = iov_page;
59efec7b
TH
2658 out_iov = in_iov + in_iovs;
2659
7572777e
MS
2660 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2661 if (err)
2662 goto out;
2663
2664 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2665 if (err)
2666 goto out;
2667
59efec7b
TH
2668 goto retry;
2669 }
2670
2671 err = -EIO;
2672 if (transferred > inarg.out_size)
2673 goto out;
2674
2675 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2676 out:
2677 if (req)
2678 fuse_put_request(fc, req);
8ac83505 2679 free_page((unsigned long) iov_page);
59efec7b
TH
2680 while (num_pages)
2681 __free_page(pages[--num_pages]);
2682 kfree(pages);
2683
2684 return err ? err : outarg.result;
2685}
08cbf542 2686EXPORT_SYMBOL_GPL(fuse_do_ioctl);
59efec7b 2687
b18da0c5
MS
2688long fuse_ioctl_common(struct file *file, unsigned int cmd,
2689 unsigned long arg, unsigned int flags)
d36f2487 2690{
6131ffaa 2691 struct inode *inode = file_inode(file);
d36f2487
MS
2692 struct fuse_conn *fc = get_fuse_conn(inode);
2693
c2132c1b 2694 if (!fuse_allow_current_process(fc))
d36f2487
MS
2695 return -EACCES;
2696
2697 if (is_bad_inode(inode))
2698 return -EIO;
2699
2700 return fuse_do_ioctl(file, cmd, arg, flags);
2701}
2702
59efec7b
TH
2703static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2704 unsigned long arg)
2705{
b18da0c5 2706 return fuse_ioctl_common(file, cmd, arg, 0);
59efec7b
TH
2707}
2708
2709static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2710 unsigned long arg)
2711{
b18da0c5 2712 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
59efec7b
TH
2713}
2714
95668a69
TH
2715/*
2716 * All files which have been polled are linked to RB tree
2717 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2718 * find the matching one.
2719 */
2720static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2721 struct rb_node **parent_out)
2722{
2723 struct rb_node **link = &fc->polled_files.rb_node;
2724 struct rb_node *last = NULL;
2725
2726 while (*link) {
2727 struct fuse_file *ff;
2728
2729 last = *link;
2730 ff = rb_entry(last, struct fuse_file, polled_node);
2731
2732 if (kh < ff->kh)
2733 link = &last->rb_left;
2734 else if (kh > ff->kh)
2735 link = &last->rb_right;
2736 else
2737 return link;
2738 }
2739
2740 if (parent_out)
2741 *parent_out = last;
2742 return link;
2743}
2744
2745/*
2746 * The file is about to be polled. Make sure it's on the polled_files
2747 * RB tree. Note that files once added to the polled_files tree are
2748 * not removed before the file is released. This is because a file
2749 * polled once is likely to be polled again.
2750 */
2751static void fuse_register_polled_file(struct fuse_conn *fc,
2752 struct fuse_file *ff)
2753{
2754 spin_lock(&fc->lock);
2755 if (RB_EMPTY_NODE(&ff->polled_node)) {
f3846266 2756 struct rb_node **link, *uninitialized_var(parent);
95668a69
TH
2757
2758 link = fuse_find_polled_node(fc, ff->kh, &parent);
2759 BUG_ON(*link);
2760 rb_link_node(&ff->polled_node, parent, link);
2761 rb_insert_color(&ff->polled_node, &fc->polled_files);
2762 }
2763 spin_unlock(&fc->lock);
2764}
2765
08cbf542 2766unsigned fuse_file_poll(struct file *file, poll_table *wait)
95668a69 2767{
95668a69 2768 struct fuse_file *ff = file->private_data;
797759aa 2769 struct fuse_conn *fc = ff->fc;
95668a69
TH
2770 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2771 struct fuse_poll_out outarg;
7078187a 2772 FUSE_ARGS(args);
95668a69
TH
2773 int err;
2774
2775 if (fc->no_poll)
2776 return DEFAULT_POLLMASK;
2777
2778 poll_wait(file, &ff->poll_wait, wait);
0415d291 2779 inarg.events = (__u32)poll_requested_events(wait);
95668a69
TH
2780
2781 /*
2782 * Ask for notification iff there's someone waiting for it.
2783 * The client may ignore the flag and always notify.
2784 */
2785 if (waitqueue_active(&ff->poll_wait)) {
2786 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2787 fuse_register_polled_file(fc, ff);
2788 }
2789
7078187a
MS
2790 args.in.h.opcode = FUSE_POLL;
2791 args.in.h.nodeid = ff->nodeid;
2792 args.in.numargs = 1;
2793 args.in.args[0].size = sizeof(inarg);
2794 args.in.args[0].value = &inarg;
2795 args.out.numargs = 1;
2796 args.out.args[0].size = sizeof(outarg);
2797 args.out.args[0].value = &outarg;
2798 err = fuse_simple_request(fc, &args);
95668a69
TH
2799
2800 if (!err)
2801 return outarg.revents;
2802 if (err == -ENOSYS) {
2803 fc->no_poll = 1;
2804 return DEFAULT_POLLMASK;
2805 }
2806 return POLLERR;
2807}
08cbf542 2808EXPORT_SYMBOL_GPL(fuse_file_poll);
95668a69
TH
2809
2810/*
2811 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2812 * wakes up the poll waiters.
2813 */
2814int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2815 struct fuse_notify_poll_wakeup_out *outarg)
2816{
2817 u64 kh = outarg->kh;
2818 struct rb_node **link;
2819
2820 spin_lock(&fc->lock);
2821
2822 link = fuse_find_polled_node(fc, kh, NULL);
2823 if (*link) {
2824 struct fuse_file *ff;
2825
2826 ff = rb_entry(*link, struct fuse_file, polled_node);
2827 wake_up_interruptible_sync(&ff->poll_wait);
2828 }
2829
2830 spin_unlock(&fc->lock);
2831 return 0;
2832}
2833
efb9fa9e
MP
2834static void fuse_do_truncate(struct file *file)
2835{
2836 struct inode *inode = file->f_mapping->host;
2837 struct iattr attr;
2838
2839 attr.ia_valid = ATTR_SIZE;
2840 attr.ia_size = i_size_read(inode);
2841
2842 attr.ia_file = file;
2843 attr.ia_valid |= ATTR_FILE;
2844
2845 fuse_do_setattr(inode, &attr, file);
2846}
2847
e5c5f05d
MP
2848static inline loff_t fuse_round_up(loff_t off)
2849{
2850 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2851}
2852
4273b793 2853static ssize_t
c8b8e32d 2854fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
4273b793 2855{
9d5722b7 2856 DECLARE_COMPLETION_ONSTACK(wait);
4273b793 2857 ssize_t ret = 0;
60b9df7a
MS
2858 struct file *file = iocb->ki_filp;
2859 struct fuse_file *ff = file->private_data;
e5c5f05d 2860 bool async_dio = ff->fc->async_dio;
4273b793 2861 loff_t pos = 0;
bcba24cc
MP
2862 struct inode *inode;
2863 loff_t i_size;
a6cbcd4a 2864 size_t count = iov_iter_count(iter);
c8b8e32d 2865 loff_t offset = iocb->ki_pos;
36cf66ed 2866 struct fuse_io_priv *io;
4273b793 2867
4273b793 2868 pos = offset;
bcba24cc
MP
2869 inode = file->f_mapping->host;
2870 i_size = i_size_read(inode);
4273b793 2871
6f673763 2872 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
9fe55eea
SW
2873 return 0;
2874
439ee5f0 2875 /* optimization for short read */
6f673763 2876 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
439ee5f0
MP
2877 if (offset >= i_size)
2878 return 0;
6b775b18
AV
2879 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2880 count = iov_iter_count(iter);
439ee5f0
MP
2881 }
2882
bcba24cc 2883 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
36cf66ed
MP
2884 if (!io)
2885 return -ENOMEM;
bcba24cc 2886 spin_lock_init(&io->lock);
744742d6 2887 kref_init(&io->refcnt);
bcba24cc
MP
2888 io->reqs = 1;
2889 io->bytes = -1;
2890 io->size = 0;
2891 io->offset = offset;
6f673763 2892 io->write = (iov_iter_rw(iter) == WRITE);
bcba24cc 2893 io->err = 0;
36cf66ed 2894 io->file = file;
bcba24cc
MP
2895 /*
2896 * By default, we want to optimize all I/Os with async request
60b9df7a 2897 * submission to the client filesystem if supported.
bcba24cc 2898 */
e5c5f05d 2899 io->async = async_dio;
bcba24cc 2900 io->iocb = iocb;
7879c4e5 2901 io->blocking = is_sync_kiocb(iocb);
bcba24cc
MP
2902
2903 /*
7879c4e5
AS
2904 * We cannot asynchronously extend the size of a file.
2905 * In such case the aio will behave exactly like sync io.
bcba24cc 2906 */
7879c4e5
AS
2907 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2908 io->blocking = true;
4273b793 2909
7879c4e5 2910 if (io->async && io->blocking) {
744742d6
SF
2911 /*
2912 * Additional reference to keep io around after
2913 * calling fuse_aio_complete()
2914 */
2915 kref_get(&io->refcnt);
9d5722b7 2916 io->done = &wait;
744742d6 2917 }
9d5722b7 2918
6f673763 2919 if (iov_iter_rw(iter) == WRITE) {
6b775b18 2920 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
812408fb
AV
2921 fuse_invalidate_attr(inode);
2922 } else {
d22a943f 2923 ret = __fuse_direct_read(io, iter, &pos);
812408fb 2924 }
36cf66ed 2925
bcba24cc
MP
2926 if (io->async) {
2927 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2928
2929 /* we have a non-extending, async request, so return */
7879c4e5 2930 if (!io->blocking)
bcba24cc
MP
2931 return -EIOCBQUEUED;
2932
9d5722b7
CH
2933 wait_for_completion(&wait);
2934 ret = fuse_get_res_by_io(io);
bcba24cc
MP
2935 }
2936
744742d6 2937 kref_put(&io->refcnt, fuse_io_release);
9d5722b7 2938
6f673763 2939 if (iov_iter_rw(iter) == WRITE) {
efb9fa9e
MP
2940 if (ret > 0)
2941 fuse_write_update_size(inode, pos);
2942 else if (ret < 0 && offset + count > i_size)
2943 fuse_do_truncate(file);
2944 }
4273b793
AA
2945
2946 return ret;
2947}
2948
cdadb11c
MS
2949static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2950 loff_t length)
05ba1f08
AP
2951{
2952 struct fuse_file *ff = file->private_data;
1c68271c 2953 struct inode *inode = file_inode(file);
0ab08f57 2954 struct fuse_inode *fi = get_fuse_inode(inode);
05ba1f08 2955 struct fuse_conn *fc = ff->fc;
7078187a 2956 FUSE_ARGS(args);
05ba1f08
AP
2957 struct fuse_fallocate_in inarg = {
2958 .fh = ff->fh,
2959 .offset = offset,
2960 .length = length,
2961 .mode = mode
2962 };
2963 int err;
14c14414
MP
2964 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2965 (mode & FALLOC_FL_PUNCH_HOLE);
05ba1f08 2966
4adb8302
MS
2967 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2968 return -EOPNOTSUPP;
2969
519c6040
MS
2970 if (fc->no_fallocate)
2971 return -EOPNOTSUPP;
2972
14c14414 2973 if (lock_inode) {
5955102c 2974 inode_lock(inode);
bde52788
MP
2975 if (mode & FALLOC_FL_PUNCH_HOLE) {
2976 loff_t endbyte = offset + length - 1;
2977 err = filemap_write_and_wait_range(inode->i_mapping,
2978 offset, endbyte);
2979 if (err)
2980 goto out;
2981
2982 fuse_sync_writes(inode);
2983 }
3634a632
BF
2984 }
2985
0ab08f57
MP
2986 if (!(mode & FALLOC_FL_KEEP_SIZE))
2987 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2988
7078187a
MS
2989 args.in.h.opcode = FUSE_FALLOCATE;
2990 args.in.h.nodeid = ff->nodeid;
2991 args.in.numargs = 1;
2992 args.in.args[0].size = sizeof(inarg);
2993 args.in.args[0].value = &inarg;
2994 err = fuse_simple_request(fc, &args);
519c6040
MS
2995 if (err == -ENOSYS) {
2996 fc->no_fallocate = 1;
2997 err = -EOPNOTSUPP;
2998 }
bee6c307
BF
2999 if (err)
3000 goto out;
3001
3002 /* we could have extended the file */
b0aa7606
MP
3003 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3004 bool changed = fuse_write_update_size(inode, offset + length);
3005
93d2269d
MS
3006 if (changed && fc->writeback_cache)
3007 file_update_time(file);
b0aa7606 3008 }
bee6c307
BF
3009
3010 if (mode & FALLOC_FL_PUNCH_HOLE)
3011 truncate_pagecache_range(inode, offset, offset + length - 1);
3012
3013 fuse_invalidate_attr(inode);
3014
3634a632 3015out:
0ab08f57
MP
3016 if (!(mode & FALLOC_FL_KEEP_SIZE))
3017 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3018
bde52788 3019 if (lock_inode)
5955102c 3020 inode_unlock(inode);
3634a632 3021
05ba1f08
AP
3022 return err;
3023}
05ba1f08 3024
4b6f5d20 3025static const struct file_operations fuse_file_operations = {
5559b8f4 3026 .llseek = fuse_file_llseek,
37c20f16 3027 .read_iter = fuse_file_read_iter,
84c3d55c 3028 .write_iter = fuse_file_write_iter,
b6aeaded
MS
3029 .mmap = fuse_file_mmap,
3030 .open = fuse_open,
3031 .flush = fuse_flush,
3032 .release = fuse_release,
3033 .fsync = fuse_fsync,
71421259 3034 .lock = fuse_file_lock,
a9ff4f87 3035 .flock = fuse_file_flock,
5ffc4ef4 3036 .splice_read = generic_file_splice_read,
59efec7b
TH
3037 .unlocked_ioctl = fuse_file_ioctl,
3038 .compat_ioctl = fuse_file_compat_ioctl,
95668a69 3039 .poll = fuse_file_poll,
05ba1f08 3040 .fallocate = fuse_file_fallocate,
b6aeaded
MS
3041};
3042
4b6f5d20 3043static const struct file_operations fuse_direct_io_file_operations = {
5559b8f4 3044 .llseek = fuse_file_llseek,
15316263 3045 .read_iter = fuse_direct_read_iter,
15316263 3046 .write_iter = fuse_direct_write_iter,
fc280c96 3047 .mmap = fuse_direct_mmap,
413ef8cb
MS
3048 .open = fuse_open,
3049 .flush = fuse_flush,
3050 .release = fuse_release,
3051 .fsync = fuse_fsync,
71421259 3052 .lock = fuse_file_lock,
a9ff4f87 3053 .flock = fuse_file_flock,
59efec7b
TH
3054 .unlocked_ioctl = fuse_file_ioctl,
3055 .compat_ioctl = fuse_file_compat_ioctl,
95668a69 3056 .poll = fuse_file_poll,
05ba1f08 3057 .fallocate = fuse_file_fallocate,
fc280c96 3058 /* no splice_read */
413ef8cb
MS
3059};
3060
f5e54d6e 3061static const struct address_space_operations fuse_file_aops = {
b6aeaded 3062 .readpage = fuse_readpage,
3be5a52b 3063 .writepage = fuse_writepage,
26d614df 3064 .writepages = fuse_writepages,
3be5a52b 3065 .launder_page = fuse_launder_page,
db50b96c 3066 .readpages = fuse_readpages,
3be5a52b 3067 .set_page_dirty = __set_page_dirty_nobuffers,
b2d2272f 3068 .bmap = fuse_bmap,
4273b793 3069 .direct_IO = fuse_direct_IO,
6b12c1b3
PE
3070 .write_begin = fuse_write_begin,
3071 .write_end = fuse_write_end,
b6aeaded
MS
3072};
3073
3074void fuse_init_file_inode(struct inode *inode)
3075{
45323fb7
MS
3076 inode->i_fop = &fuse_file_operations;
3077 inode->i_data.a_ops = &fuse_file_aops;
b6aeaded 3078}
This page took 1.051222 seconds and 5 git commands to generate.