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