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