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