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