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