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