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