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