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