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