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