2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
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/swap.h>
18 #include <linux/falloc.h>
19 #include <linux/uio.h>
22 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
23 unsigned int open_flags, int opcode,
24 struct fuse_open_out *outargp)
26 struct fuse_open_in inarg;
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fm->fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
34 if (fm->fc->handle_killpriv_v2 &&
35 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
36 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
42 args.in_args[0].size = sizeof(inarg);
43 args.in_args[0].value = &inarg;
45 args.out_args[0].size = sizeof(*outargp);
46 args.out_args[0].value = outargp;
48 return fuse_simple_request(fm, &args);
51 struct fuse_release_args {
52 struct fuse_args args;
53 struct fuse_release_in inarg;
57 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
61 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
66 ff->release_args = kzalloc(sizeof(*ff->release_args),
68 if (!ff->release_args) {
73 INIT_LIST_HEAD(&ff->write_entry);
74 mutex_init(&ff->readdir.lock);
75 refcount_set(&ff->count, 1);
76 RB_CLEAR_NODE(&ff->polled_node);
77 init_waitqueue_head(&ff->poll_wait);
79 ff->kh = atomic64_inc_return(&fm->fc->khctr);
84 void fuse_file_free(struct fuse_file *ff)
86 kfree(ff->release_args);
87 mutex_destroy(&ff->readdir.lock);
91 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
93 refcount_inc(&ff->count);
97 static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
100 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
106 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
108 if (refcount_dec_and_test(&ff->count)) {
109 struct fuse_args *args = &ff->release_args->args;
111 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
112 /* Do nothing when client does not implement 'open' */
113 fuse_release_end(ff->fm, args, 0);
115 fuse_simple_request(ff->fm, args);
116 fuse_release_end(ff->fm, args, 0);
118 args->end = fuse_release_end;
119 if (fuse_simple_background(ff->fm, args,
120 GFP_KERNEL | __GFP_NOFAIL))
121 fuse_release_end(ff->fm, args, -ENOTCONN);
127 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
128 unsigned int open_flags, bool isdir)
130 struct fuse_conn *fc = fm->fc;
131 struct fuse_file *ff;
132 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
134 ff = fuse_file_alloc(fm);
136 return ERR_PTR(-ENOMEM);
139 /* Default for no-open */
140 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
141 if (isdir ? !fc->no_opendir : !fc->no_open) {
142 struct fuse_open_out outarg;
145 err = fuse_send_open(fm, nodeid, open_flags, opcode, &outarg);
148 ff->open_flags = outarg.open_flags;
150 } else if (err != -ENOSYS) {
162 ff->open_flags &= ~FOPEN_DIRECT_IO;
169 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
172 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
175 file->private_data = ff;
177 return PTR_ERR_OR_ZERO(ff);
179 EXPORT_SYMBOL_GPL(fuse_do_open);
181 static void fuse_link_write_file(struct file *file)
183 struct inode *inode = file_inode(file);
184 struct fuse_inode *fi = get_fuse_inode(inode);
185 struct fuse_file *ff = file->private_data;
187 * file may be written through mmap, so chain it onto the
188 * inodes's write_file list
190 spin_lock(&fi->lock);
191 if (list_empty(&ff->write_entry))
192 list_add(&ff->write_entry, &fi->write_files);
193 spin_unlock(&fi->lock);
196 void fuse_finish_open(struct inode *inode, struct file *file)
198 struct fuse_file *ff = file->private_data;
199 struct fuse_conn *fc = get_fuse_conn(inode);
201 if (ff->open_flags & FOPEN_STREAM)
202 stream_open(inode, file);
203 else if (ff->open_flags & FOPEN_NONSEEKABLE)
204 nonseekable_open(inode, file);
206 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
207 struct fuse_inode *fi = get_fuse_inode(inode);
209 spin_lock(&fi->lock);
210 fi->attr_version = atomic64_inc_return(&fc->attr_version);
211 i_size_write(inode, 0);
212 spin_unlock(&fi->lock);
213 truncate_pagecache(inode, 0);
214 fuse_invalidate_attr(inode);
215 if (fc->writeback_cache)
216 file_update_time(file);
217 } else if (!(ff->open_flags & FOPEN_KEEP_CACHE)) {
218 invalidate_inode_pages2(inode->i_mapping);
221 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
222 fuse_link_write_file(file);
225 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
227 struct fuse_mount *fm = get_fuse_mount(inode);
228 struct fuse_conn *fc = fm->fc;
230 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
231 fc->atomic_o_trunc &&
233 bool dax_truncate = (file->f_flags & O_TRUNC) &&
234 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
236 if (fuse_is_bad(inode))
239 err = generic_file_open(inode, file);
243 if (is_wb_truncate || dax_truncate) {
245 fuse_set_nowrite(inode);
249 down_write(&get_fuse_inode(inode)->i_mmap_sem);
250 err = fuse_dax_break_layouts(inode, 0, 0);
255 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
257 fuse_finish_open(inode, file);
261 up_write(&get_fuse_inode(inode)->i_mmap_sem);
263 if (is_wb_truncate | dax_truncate) {
264 fuse_release_nowrite(inode);
271 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
272 unsigned int flags, int opcode)
274 struct fuse_conn *fc = ff->fm->fc;
275 struct fuse_release_args *ra = ff->release_args;
277 /* Inode is NULL on error path of fuse_create_open() */
279 spin_lock(&fi->lock);
280 list_del(&ff->write_entry);
281 spin_unlock(&fi->lock);
283 spin_lock(&fc->lock);
284 if (!RB_EMPTY_NODE(&ff->polled_node))
285 rb_erase(&ff->polled_node, &fc->polled_files);
286 spin_unlock(&fc->lock);
288 wake_up_interruptible_all(&ff->poll_wait);
290 ra->inarg.fh = ff->fh;
291 ra->inarg.flags = flags;
292 ra->args.in_numargs = 1;
293 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
294 ra->args.in_args[0].value = &ra->inarg;
295 ra->args.opcode = opcode;
296 ra->args.nodeid = ff->nodeid;
297 ra->args.force = true;
298 ra->args.nocreds = true;
301 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
302 unsigned int open_flags, fl_owner_t id, bool isdir)
304 struct fuse_inode *fi = get_fuse_inode(inode);
305 struct fuse_release_args *ra = ff->release_args;
306 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
308 fuse_prepare_release(fi, ff, open_flags, opcode);
311 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
312 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
314 /* Hold inode until release is finished */
315 ra->inode = igrab(inode);
318 * Normally this will send the RELEASE request, however if
319 * some asynchronous READ or WRITE requests are outstanding,
320 * the sending will be delayed.
322 * Make the release synchronous if this is a fuseblk mount,
323 * synchronous RELEASE is allowed (and desirable) in this case
324 * because the server can be trusted not to screw up.
326 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
329 void fuse_release_common(struct file *file, bool isdir)
331 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
332 (fl_owner_t) file, isdir);
335 static int fuse_open(struct inode *inode, struct file *file)
337 return fuse_open_common(inode, file, false);
340 static int fuse_release(struct inode *inode, struct file *file)
342 struct fuse_conn *fc = get_fuse_conn(inode);
344 /* see fuse_vma_close() for !writeback_cache case */
345 if (fc->writeback_cache)
346 write_inode_now(inode, 1);
348 fuse_release_common(file, false);
350 /* return value is ignored by VFS */
354 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
357 WARN_ON(refcount_read(&ff->count) > 1);
358 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
360 * iput(NULL) is a no-op and since the refcount is 1 and everything's
361 * synchronous, we are fine with not doing igrab() here"
363 fuse_file_put(ff, true, false);
365 EXPORT_SYMBOL_GPL(fuse_sync_release);
368 * Scramble the ID space with XTEA, so that the value of the files_struct
369 * pointer is not exposed to userspace.
371 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
373 u32 *k = fc->scramble_key;
374 u64 v = (unsigned long) id;
380 for (i = 0; i < 32; i++) {
381 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
383 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
386 return (u64) v0 + ((u64) v1 << 32);
389 struct fuse_writepage_args {
390 struct fuse_io_args ia;
391 struct rb_node writepages_entry;
392 struct list_head queue_entry;
393 struct fuse_writepage_args *next;
397 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
398 pgoff_t idx_from, pgoff_t idx_to)
402 n = fi->writepages.rb_node;
405 struct fuse_writepage_args *wpa;
408 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
409 WARN_ON(get_fuse_inode(wpa->inode) != fi);
410 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
411 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
413 else if (idx_to < curr_index)
422 * Check if any page in a range is under writeback
424 * This is currently done by walking the list of writepage requests
425 * for the inode, which can be pretty inefficient.
427 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
430 struct fuse_inode *fi = get_fuse_inode(inode);
433 spin_lock(&fi->lock);
434 found = fuse_find_writeback(fi, idx_from, idx_to);
435 spin_unlock(&fi->lock);
440 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
442 return fuse_range_is_writeback(inode, index, index);
446 * Wait for page writeback to be completed.
448 * Since fuse doesn't rely on the VM writeback tracking, this has to
449 * use some other means.
451 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
453 struct fuse_inode *fi = get_fuse_inode(inode);
455 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
459 * Wait for all pending writepages on the inode to finish.
461 * This is currently done by blocking further writes with FUSE_NOWRITE
462 * and waiting for all sent writes to complete.
464 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
465 * could conflict with truncation.
467 static void fuse_sync_writes(struct inode *inode)
469 fuse_set_nowrite(inode);
470 fuse_release_nowrite(inode);
473 static int fuse_flush(struct file *file, fl_owner_t id)
475 struct inode *inode = file_inode(file);
476 struct fuse_mount *fm = get_fuse_mount(inode);
477 struct fuse_file *ff = file->private_data;
478 struct fuse_flush_in inarg;
482 if (fuse_is_bad(inode))
485 err = write_inode_now(inode, 1);
490 fuse_sync_writes(inode);
493 err = filemap_check_errors(file->f_mapping);
498 if (fm->fc->no_flush)
501 memset(&inarg, 0, sizeof(inarg));
503 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
504 args.opcode = FUSE_FLUSH;
505 args.nodeid = get_node_id(inode);
507 args.in_args[0].size = sizeof(inarg);
508 args.in_args[0].value = &inarg;
511 err = fuse_simple_request(fm, &args);
512 if (err == -ENOSYS) {
513 fm->fc->no_flush = 1;
519 * In memory i_blocks is not maintained by fuse, if writeback cache is
520 * enabled, i_blocks from cached attr may not be accurate.
522 if (!err && fm->fc->writeback_cache)
523 fuse_invalidate_attr(inode);
527 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
528 int datasync, int opcode)
530 struct inode *inode = file->f_mapping->host;
531 struct fuse_mount *fm = get_fuse_mount(inode);
532 struct fuse_file *ff = file->private_data;
534 struct fuse_fsync_in inarg;
536 memset(&inarg, 0, sizeof(inarg));
538 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
539 args.opcode = opcode;
540 args.nodeid = get_node_id(inode);
542 args.in_args[0].size = sizeof(inarg);
543 args.in_args[0].value = &inarg;
544 return fuse_simple_request(fm, &args);
547 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
550 struct inode *inode = file->f_mapping->host;
551 struct fuse_conn *fc = get_fuse_conn(inode);
554 if (fuse_is_bad(inode))
560 * Start writeback against all dirty pages of the inode, then
561 * wait for all outstanding writes, before sending the FSYNC
564 err = file_write_and_wait_range(file, start, end);
568 fuse_sync_writes(inode);
571 * Due to implementation of fuse writeback
572 * file_write_and_wait_range() does not catch errors.
573 * We have to do this directly after fuse_sync_writes()
575 err = file_check_and_advance_wb_err(file);
579 err = sync_inode_metadata(inode, 1);
586 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
587 if (err == -ENOSYS) {
597 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
598 size_t count, int opcode)
600 struct fuse_file *ff = file->private_data;
601 struct fuse_args *args = &ia->ap.args;
603 ia->read.in.fh = ff->fh;
604 ia->read.in.offset = pos;
605 ia->read.in.size = count;
606 ia->read.in.flags = file->f_flags;
607 args->opcode = opcode;
608 args->nodeid = ff->nodeid;
609 args->in_numargs = 1;
610 args->in_args[0].size = sizeof(ia->read.in);
611 args->in_args[0].value = &ia->read.in;
612 args->out_argvar = true;
613 args->out_numargs = 1;
614 args->out_args[0].size = count;
617 static void fuse_release_user_pages(struct fuse_args_pages *ap,
622 for (i = 0; i < ap->num_pages; i++) {
624 set_page_dirty_lock(ap->pages[i]);
625 put_page(ap->pages[i]);
629 static void fuse_io_release(struct kref *kref)
631 kfree(container_of(kref, struct fuse_io_priv, refcnt));
634 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
639 if (io->bytes >= 0 && io->write)
642 return io->bytes < 0 ? io->size : io->bytes;
646 * In case of short read, the caller sets 'pos' to the position of
647 * actual end of fuse request in IO request. Otherwise, if bytes_requested
648 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
651 * User requested DIO read of 64K. It was split into two 32K fuse requests,
652 * both submitted asynchronously. The first of them was ACKed by userspace as
653 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
654 * second request was ACKed as short, e.g. only 1K was read, resulting in
657 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
658 * will be equal to the length of the longest contiguous fragment of
659 * transferred data starting from the beginning of IO request.
661 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
665 spin_lock(&io->lock);
667 io->err = io->err ? : err;
668 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
672 if (!left && io->blocking)
674 spin_unlock(&io->lock);
676 if (!left && !io->blocking) {
677 ssize_t res = fuse_get_res_by_io(io);
680 struct inode *inode = file_inode(io->iocb->ki_filp);
681 struct fuse_conn *fc = get_fuse_conn(inode);
682 struct fuse_inode *fi = get_fuse_inode(inode);
684 spin_lock(&fi->lock);
685 fi->attr_version = atomic64_inc_return(&fc->attr_version);
686 spin_unlock(&fi->lock);
689 io->iocb->ki_complete(io->iocb, res, 0);
692 kref_put(&io->refcnt, fuse_io_release);
695 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
698 struct fuse_io_args *ia;
700 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
703 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
713 static void fuse_io_free(struct fuse_io_args *ia)
719 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
722 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
723 struct fuse_io_priv *io = ia->io;
726 fuse_release_user_pages(&ia->ap, io->should_dirty);
730 } else if (io->write) {
731 if (ia->write.out.size > ia->write.in.size) {
733 } else if (ia->write.in.size != ia->write.out.size) {
734 pos = ia->write.in.offset - io->offset +
738 u32 outsize = args->out_args[0].size;
740 if (ia->read.in.size != outsize)
741 pos = ia->read.in.offset - io->offset + outsize;
744 fuse_aio_complete(io, err, pos);
748 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
749 struct fuse_io_args *ia, size_t num_bytes)
752 struct fuse_io_priv *io = ia->io;
754 spin_lock(&io->lock);
755 kref_get(&io->refcnt);
756 io->size += num_bytes;
758 spin_unlock(&io->lock);
760 ia->ap.args.end = fuse_aio_complete_req;
761 ia->ap.args.may_block = io->should_dirty;
762 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
764 fuse_aio_complete_req(fm, &ia->ap.args, err);
769 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
772 struct file *file = ia->io->iocb->ki_filp;
773 struct fuse_file *ff = file->private_data;
774 struct fuse_mount *fm = ff->fm;
776 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
778 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
779 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
783 return fuse_async_req_send(fm, ia, count);
785 return fuse_simple_request(fm, &ia->ap.args);
788 static void fuse_read_update_size(struct inode *inode, loff_t size,
791 struct fuse_conn *fc = get_fuse_conn(inode);
792 struct fuse_inode *fi = get_fuse_inode(inode);
794 spin_lock(&fi->lock);
795 if (attr_ver == fi->attr_version && size < inode->i_size &&
796 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
797 fi->attr_version = atomic64_inc_return(&fc->attr_version);
798 i_size_write(inode, size);
800 spin_unlock(&fi->lock);
803 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
804 struct fuse_args_pages *ap)
806 struct fuse_conn *fc = get_fuse_conn(inode);
809 * If writeback_cache is enabled, a short read means there's a hole in
810 * the file. Some data after the hole is in page cache, but has not
811 * reached the client fs yet. So the hole is not present there.
813 if (!fc->writeback_cache) {
814 loff_t pos = page_offset(ap->pages[0]) + num_read;
815 fuse_read_update_size(inode, pos, attr_ver);
819 static int fuse_do_readpage(struct file *file, struct page *page)
821 struct inode *inode = page->mapping->host;
822 struct fuse_mount *fm = get_fuse_mount(inode);
823 loff_t pos = page_offset(page);
824 struct fuse_page_desc desc = { .length = PAGE_SIZE };
825 struct fuse_io_args ia = {
826 .ap.args.page_zeroing = true,
827 .ap.args.out_pages = true,
836 * Page writeback can extend beyond the lifetime of the
837 * page-cache page, so make sure we read a properly synced
840 fuse_wait_on_page_writeback(inode, page->index);
842 attr_ver = fuse_get_attr_version(fm->fc);
844 /* Don't overflow end offset */
845 if (pos + (desc.length - 1) == LLONG_MAX)
848 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
849 res = fuse_simple_request(fm, &ia.ap.args);
853 * Short read means EOF. If file size is larger, truncate it
855 if (res < desc.length)
856 fuse_short_read(inode, attr_ver, res, &ia.ap);
858 SetPageUptodate(page);
863 static int fuse_readpage(struct file *file, struct page *page)
865 struct inode *inode = page->mapping->host;
869 if (fuse_is_bad(inode))
872 err = fuse_do_readpage(file, page);
873 fuse_invalidate_atime(inode);
879 static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
883 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
884 struct fuse_args_pages *ap = &ia->ap;
885 size_t count = ia->read.in.size;
886 size_t num_read = args->out_args[0].size;
887 struct address_space *mapping = NULL;
889 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
890 mapping = ap->pages[i]->mapping;
893 struct inode *inode = mapping->host;
896 * Short read means EOF. If file size is larger, truncate it
898 if (!err && num_read < count)
899 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
901 fuse_invalidate_atime(inode);
904 for (i = 0; i < ap->num_pages; i++) {
905 struct page *page = ap->pages[i];
908 SetPageUptodate(page);
915 fuse_file_put(ia->ff, false, false);
920 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
922 struct fuse_file *ff = file->private_data;
923 struct fuse_mount *fm = ff->fm;
924 struct fuse_args_pages *ap = &ia->ap;
925 loff_t pos = page_offset(ap->pages[0]);
926 size_t count = ap->num_pages << PAGE_SHIFT;
930 ap->args.out_pages = true;
931 ap->args.page_zeroing = true;
932 ap->args.page_replace = true;
934 /* Don't overflow end offset */
935 if (pos + (count - 1) == LLONG_MAX) {
937 ap->descs[ap->num_pages - 1].length--;
939 WARN_ON((loff_t) (pos + count) < 0);
941 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
942 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
943 if (fm->fc->async_read) {
944 ia->ff = fuse_file_get(ff);
945 ap->args.end = fuse_readpages_end;
946 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
950 res = fuse_simple_request(fm, &ap->args);
951 err = res < 0 ? res : 0;
953 fuse_readpages_end(fm, &ap->args, err);
956 static void fuse_readahead(struct readahead_control *rac)
958 struct inode *inode = rac->mapping->host;
959 struct fuse_conn *fc = get_fuse_conn(inode);
960 unsigned int i, max_pages, nr_pages = 0;
962 if (fuse_is_bad(inode))
965 max_pages = min_t(unsigned int, fc->max_pages,
966 fc->max_read / PAGE_SIZE);
969 struct fuse_io_args *ia;
970 struct fuse_args_pages *ap;
972 nr_pages = readahead_count(rac) - nr_pages;
973 if (nr_pages > max_pages)
974 nr_pages = max_pages;
977 ia = fuse_io_alloc(NULL, nr_pages);
981 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
982 for (i = 0; i < nr_pages; i++) {
983 fuse_wait_on_page_writeback(inode,
984 readahead_index(rac) + i);
985 ap->descs[i].length = PAGE_SIZE;
987 ap->num_pages = nr_pages;
988 fuse_send_readpages(ia, rac->file);
992 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
994 struct inode *inode = iocb->ki_filp->f_mapping->host;
995 struct fuse_conn *fc = get_fuse_conn(inode);
998 * In auto invalidate mode, always update attributes on read.
999 * Otherwise, only update if we attempt to read past EOF (to ensure
1000 * i_size is up to date).
1002 if (fc->auto_inval_data ||
1003 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1005 err = fuse_update_attributes(inode, iocb->ki_filp);
1010 return generic_file_read_iter(iocb, to);
1013 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1014 loff_t pos, size_t count)
1016 struct fuse_args *args = &ia->ap.args;
1018 ia->write.in.fh = ff->fh;
1019 ia->write.in.offset = pos;
1020 ia->write.in.size = count;
1021 args->opcode = FUSE_WRITE;
1022 args->nodeid = ff->nodeid;
1023 args->in_numargs = 2;
1024 if (ff->fm->fc->minor < 9)
1025 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1027 args->in_args[0].size = sizeof(ia->write.in);
1028 args->in_args[0].value = &ia->write.in;
1029 args->in_args[1].size = count;
1030 args->out_numargs = 1;
1031 args->out_args[0].size = sizeof(ia->write.out);
1032 args->out_args[0].value = &ia->write.out;
1035 static unsigned int fuse_write_flags(struct kiocb *iocb)
1037 unsigned int flags = iocb->ki_filp->f_flags;
1039 if (iocb->ki_flags & IOCB_DSYNC)
1041 if (iocb->ki_flags & IOCB_SYNC)
1047 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1048 size_t count, fl_owner_t owner)
1050 struct kiocb *iocb = ia->io->iocb;
1051 struct file *file = iocb->ki_filp;
1052 struct fuse_file *ff = file->private_data;
1053 struct fuse_mount *fm = ff->fm;
1054 struct fuse_write_in *inarg = &ia->write.in;
1057 fuse_write_args_fill(ia, ff, pos, count);
1058 inarg->flags = fuse_write_flags(iocb);
1059 if (owner != NULL) {
1060 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1061 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
1065 return fuse_async_req_send(fm, ia, count);
1067 err = fuse_simple_request(fm, &ia->ap.args);
1068 if (!err && ia->write.out.size > count)
1071 return err ?: ia->write.out.size;
1074 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1076 struct fuse_conn *fc = get_fuse_conn(inode);
1077 struct fuse_inode *fi = get_fuse_inode(inode);
1080 spin_lock(&fi->lock);
1081 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1082 if (pos > inode->i_size) {
1083 i_size_write(inode, pos);
1086 spin_unlock(&fi->lock);
1091 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1092 struct kiocb *iocb, struct inode *inode,
1093 loff_t pos, size_t count)
1095 struct fuse_args_pages *ap = &ia->ap;
1096 struct file *file = iocb->ki_filp;
1097 struct fuse_file *ff = file->private_data;
1098 struct fuse_mount *fm = ff->fm;
1099 unsigned int offset, i;
1103 for (i = 0; i < ap->num_pages; i++)
1104 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1106 fuse_write_args_fill(ia, ff, pos, count);
1107 ia->write.in.flags = fuse_write_flags(iocb);
1108 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1109 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1111 err = fuse_simple_request(fm, &ap->args);
1112 if (!err && ia->write.out.size > count)
1115 short_write = ia->write.out.size < count;
1116 offset = ap->descs[0].offset;
1117 count = ia->write.out.size;
1118 for (i = 0; i < ap->num_pages; i++) {
1119 struct page *page = ap->pages[i];
1122 ClearPageUptodate(page);
1124 if (count >= PAGE_SIZE - offset)
1125 count -= PAGE_SIZE - offset;
1128 ClearPageUptodate(page);
1133 if (ia->write.page_locked && (i == ap->num_pages - 1))
1141 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
1142 struct address_space *mapping,
1143 struct iov_iter *ii, loff_t pos,
1144 unsigned int max_pages)
1146 struct fuse_args_pages *ap = &ia->ap;
1147 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1148 unsigned offset = pos & (PAGE_SIZE - 1);
1152 ap->args.in_pages = true;
1153 ap->descs[0].offset = offset;
1158 pgoff_t index = pos >> PAGE_SHIFT;
1159 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1160 iov_iter_count(ii));
1162 bytes = min_t(size_t, bytes, fc->max_write - count);
1166 if (iov_iter_fault_in_readable(ii, bytes))
1170 page = grab_cache_page_write_begin(mapping, index, 0);
1174 if (mapping_writably_mapped(mapping))
1175 flush_dcache_page(page);
1177 tmp = copy_page_from_iter_atomic(page, offset, bytes, ii);
1178 flush_dcache_page(page);
1187 ap->pages[ap->num_pages] = page;
1188 ap->descs[ap->num_pages].length = tmp;
1194 if (offset == PAGE_SIZE)
1197 /* If we copied full page, mark it uptodate */
1198 if (tmp == PAGE_SIZE)
1199 SetPageUptodate(page);
1201 if (PageUptodate(page)) {
1204 ia->write.page_locked = true;
1207 if (!fc->big_writes)
1209 } while (iov_iter_count(ii) && count < fc->max_write &&
1210 ap->num_pages < max_pages && offset == 0);
1212 return count > 0 ? count : err;
1215 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1216 unsigned int max_pages)
1218 return min_t(unsigned int,
1219 ((pos + len - 1) >> PAGE_SHIFT) -
1220 (pos >> PAGE_SHIFT) + 1,
1224 static ssize_t fuse_perform_write(struct kiocb *iocb,
1225 struct address_space *mapping,
1226 struct iov_iter *ii, loff_t pos)
1228 struct inode *inode = mapping->host;
1229 struct fuse_conn *fc = get_fuse_conn(inode);
1230 struct fuse_inode *fi = get_fuse_inode(inode);
1234 if (inode->i_size < pos + iov_iter_count(ii))
1235 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1239 struct fuse_io_args ia = {};
1240 struct fuse_args_pages *ap = &ia.ap;
1241 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1244 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1250 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
1254 err = fuse_send_write_pages(&ia, iocb, inode,
1257 size_t num_written = ia.write.out.size;
1262 /* break out of the loop on short write */
1263 if (num_written != count)
1268 } while (!err && iov_iter_count(ii));
1271 fuse_write_update_size(inode, pos);
1273 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1274 fuse_invalidate_attr(inode);
1276 return res > 0 ? res : err;
1279 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1281 struct file *file = iocb->ki_filp;
1282 struct address_space *mapping = file->f_mapping;
1283 ssize_t written = 0;
1284 ssize_t written_buffered = 0;
1285 struct inode *inode = mapping->host;
1287 struct fuse_conn *fc = get_fuse_conn(inode);
1290 if (fc->writeback_cache) {
1291 /* Update size (EOF optimization) and mode (SUID clearing) */
1292 err = fuse_update_attributes(mapping->host, file);
1296 if (fc->handle_killpriv_v2 &&
1297 should_remove_suid(file_dentry(file))) {
1301 return generic_file_write_iter(iocb, from);
1307 /* We can write back this queue in page reclaim */
1308 current->backing_dev_info = inode_to_bdi(inode);
1310 err = generic_write_checks(iocb, from);
1314 err = file_remove_privs(file);
1318 err = file_update_time(file);
1322 if (iocb->ki_flags & IOCB_DIRECT) {
1323 loff_t pos = iocb->ki_pos;
1324 written = generic_file_direct_write(iocb, from);
1325 if (written < 0 || !iov_iter_count(from))
1330 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1331 if (written_buffered < 0) {
1332 err = written_buffered;
1335 endbyte = pos + written_buffered - 1;
1337 err = filemap_write_and_wait_range(file->f_mapping, pos,
1342 invalidate_mapping_pages(file->f_mapping,
1344 endbyte >> PAGE_SHIFT);
1346 written += written_buffered;
1347 iocb->ki_pos = pos + written_buffered;
1349 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1351 iocb->ki_pos += written;
1354 current->backing_dev_info = NULL;
1355 inode_unlock(inode);
1357 written = generic_write_sync(iocb, written);
1359 return written ? written : err;
1362 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1364 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1367 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1370 return min(iov_iter_single_seg_count(ii), max_size);
1373 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1374 size_t *nbytesp, int write,
1375 unsigned int max_pages)
1377 size_t nbytes = 0; /* # bytes already packed in req */
1380 /* Special case for kernel I/O: can copy directly into the buffer */
1381 if (iov_iter_is_kvec(ii)) {
1382 unsigned long user_addr = fuse_get_user_addr(ii);
1383 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1386 ap->args.in_args[1].value = (void *) user_addr;
1388 ap->args.out_args[0].value = (void *) user_addr;
1390 iov_iter_advance(ii, frag_size);
1391 *nbytesp = frag_size;
1395 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1398 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1400 max_pages - ap->num_pages,
1405 iov_iter_advance(ii, ret);
1409 npages = DIV_ROUND_UP(ret, PAGE_SIZE);
1411 ap->descs[ap->num_pages].offset = start;
1412 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1414 ap->num_pages += npages;
1415 ap->descs[ap->num_pages - 1].length -=
1416 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1420 ap->args.in_pages = true;
1422 ap->args.out_pages = true;
1426 return ret < 0 ? ret : 0;
1429 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1430 loff_t *ppos, int flags)
1432 int write = flags & FUSE_DIO_WRITE;
1433 int cuse = flags & FUSE_DIO_CUSE;
1434 struct file *file = io->iocb->ki_filp;
1435 struct inode *inode = file->f_mapping->host;
1436 struct fuse_file *ff = file->private_data;
1437 struct fuse_conn *fc = ff->fm->fc;
1438 size_t nmax = write ? fc->max_write : fc->max_read;
1440 size_t count = iov_iter_count(iter);
1441 pgoff_t idx_from = pos >> PAGE_SHIFT;
1442 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1445 struct fuse_io_args *ia;
1446 unsigned int max_pages;
1448 max_pages = iov_iter_npages(iter, fc->max_pages);
1449 ia = fuse_io_alloc(io, max_pages);
1454 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1457 fuse_sync_writes(inode);
1459 inode_unlock(inode);
1462 io->should_dirty = !write && iter_is_iovec(iter);
1465 fl_owner_t owner = current->files;
1466 size_t nbytes = min(count, nmax);
1468 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1474 if (!capable(CAP_FSETID))
1475 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1477 nres = fuse_send_write(ia, pos, nbytes, owner);
1479 nres = fuse_send_read(ia, pos, nbytes, owner);
1482 if (!io->async || nres < 0) {
1483 fuse_release_user_pages(&ia->ap, io->should_dirty);
1488 iov_iter_revert(iter, nbytes);
1492 WARN_ON(nres > nbytes);
1497 if (nres != nbytes) {
1498 iov_iter_revert(iter, nbytes - nres);
1502 max_pages = iov_iter_npages(iter, fc->max_pages);
1503 ia = fuse_io_alloc(io, max_pages);
1513 return res > 0 ? res : err;
1515 EXPORT_SYMBOL_GPL(fuse_direct_io);
1517 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1518 struct iov_iter *iter,
1522 struct inode *inode = file_inode(io->iocb->ki_filp);
1524 res = fuse_direct_io(io, iter, ppos, 0);
1526 fuse_invalidate_atime(inode);
1531 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1533 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1537 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1538 res = fuse_direct_IO(iocb, to);
1540 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1542 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1548 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1550 struct inode *inode = file_inode(iocb->ki_filp);
1551 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1554 /* Don't allow parallel writes to the same file */
1556 res = generic_write_checks(iocb, from);
1558 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1559 res = fuse_direct_IO(iocb, from);
1561 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1565 fuse_invalidate_attr(inode);
1567 fuse_write_update_size(inode, iocb->ki_pos);
1568 inode_unlock(inode);
1573 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1575 struct file *file = iocb->ki_filp;
1576 struct fuse_file *ff = file->private_data;
1577 struct inode *inode = file_inode(file);
1579 if (fuse_is_bad(inode))
1582 if (FUSE_IS_DAX(inode))
1583 return fuse_dax_read_iter(iocb, to);
1585 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1586 return fuse_cache_read_iter(iocb, to);
1588 return fuse_direct_read_iter(iocb, to);
1591 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1593 struct file *file = iocb->ki_filp;
1594 struct fuse_file *ff = file->private_data;
1595 struct inode *inode = file_inode(file);
1597 if (fuse_is_bad(inode))
1600 if (FUSE_IS_DAX(inode))
1601 return fuse_dax_write_iter(iocb, from);
1603 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1604 return fuse_cache_write_iter(iocb, from);
1606 return fuse_direct_write_iter(iocb, from);
1609 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1611 struct fuse_args_pages *ap = &wpa->ia.ap;
1614 for (i = 0; i < ap->num_pages; i++)
1615 __free_page(ap->pages[i]);
1618 fuse_file_put(wpa->ia.ff, false, false);
1624 static void fuse_writepage_finish(struct fuse_mount *fm,
1625 struct fuse_writepage_args *wpa)
1627 struct fuse_args_pages *ap = &wpa->ia.ap;
1628 struct inode *inode = wpa->inode;
1629 struct fuse_inode *fi = get_fuse_inode(inode);
1630 struct backing_dev_info *bdi = inode_to_bdi(inode);
1633 for (i = 0; i < ap->num_pages; i++) {
1634 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1635 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1636 wb_writeout_inc(&bdi->wb);
1638 wake_up(&fi->page_waitq);
1641 /* Called under fi->lock, may release and reacquire it */
1642 static void fuse_send_writepage(struct fuse_mount *fm,
1643 struct fuse_writepage_args *wpa, loff_t size)
1644 __releases(fi->lock)
1645 __acquires(fi->lock)
1647 struct fuse_writepage_args *aux, *next;
1648 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1649 struct fuse_write_in *inarg = &wpa->ia.write.in;
1650 struct fuse_args *args = &wpa->ia.ap.args;
1651 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1655 if (inarg->offset + data_size <= size) {
1656 inarg->size = data_size;
1657 } else if (inarg->offset < size) {
1658 inarg->size = size - inarg->offset;
1660 /* Got truncated off completely */
1664 args->in_args[1].size = inarg->size;
1666 args->nocreds = true;
1668 err = fuse_simple_background(fm, args, GFP_ATOMIC);
1669 if (err == -ENOMEM) {
1670 spin_unlock(&fi->lock);
1671 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
1672 spin_lock(&fi->lock);
1675 /* Fails on broken connection only */
1683 rb_erase(&wpa->writepages_entry, &fi->writepages);
1684 fuse_writepage_finish(fm, wpa);
1685 spin_unlock(&fi->lock);
1687 /* After fuse_writepage_finish() aux request list is private */
1688 for (aux = wpa->next; aux; aux = next) {
1691 fuse_writepage_free(aux);
1694 fuse_writepage_free(wpa);
1695 spin_lock(&fi->lock);
1699 * If fi->writectr is positive (no truncate or fsync going on) send
1700 * all queued writepage requests.
1702 * Called with fi->lock
1704 void fuse_flush_writepages(struct inode *inode)
1705 __releases(fi->lock)
1706 __acquires(fi->lock)
1708 struct fuse_mount *fm = get_fuse_mount(inode);
1709 struct fuse_inode *fi = get_fuse_inode(inode);
1710 loff_t crop = i_size_read(inode);
1711 struct fuse_writepage_args *wpa;
1713 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1714 wpa = list_entry(fi->queued_writes.next,
1715 struct fuse_writepage_args, queue_entry);
1716 list_del_init(&wpa->queue_entry);
1717 fuse_send_writepage(fm, wpa, crop);
1721 static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1722 struct fuse_writepage_args *wpa)
1724 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1725 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1726 struct rb_node **p = &root->rb_node;
1727 struct rb_node *parent = NULL;
1729 WARN_ON(!wpa->ia.ap.num_pages);
1731 struct fuse_writepage_args *curr;
1735 curr = rb_entry(parent, struct fuse_writepage_args,
1737 WARN_ON(curr->inode != wpa->inode);
1738 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1740 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1741 p = &(*p)->rb_right;
1742 else if (idx_to < curr_index)
1748 rb_link_node(&wpa->writepages_entry, parent, p);
1749 rb_insert_color(&wpa->writepages_entry, root);
1753 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1755 WARN_ON(fuse_insert_writeback(root, wpa));
1758 static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
1761 struct fuse_writepage_args *wpa =
1762 container_of(args, typeof(*wpa), ia.ap.args);
1763 struct inode *inode = wpa->inode;
1764 struct fuse_inode *fi = get_fuse_inode(inode);
1765 struct fuse_conn *fc = get_fuse_conn(inode);
1767 mapping_set_error(inode->i_mapping, error);
1769 * A writeback finished and this might have updated mtime/ctime on
1770 * server making local mtime/ctime stale. Hence invalidate attrs.
1771 * Do this only if writeback_cache is not enabled. If writeback_cache
1772 * is enabled, we trust local ctime/mtime.
1774 if (!fc->writeback_cache)
1775 fuse_invalidate_attr(inode);
1776 spin_lock(&fi->lock);
1777 rb_erase(&wpa->writepages_entry, &fi->writepages);
1779 struct fuse_mount *fm = get_fuse_mount(inode);
1780 struct fuse_write_in *inarg = &wpa->ia.write.in;
1781 struct fuse_writepage_args *next = wpa->next;
1783 wpa->next = next->next;
1785 next->ia.ff = fuse_file_get(wpa->ia.ff);
1786 tree_insert(&fi->writepages, next);
1789 * Skip fuse_flush_writepages() to make it easy to crop requests
1790 * based on primary request size.
1792 * 1st case (trivial): there are no concurrent activities using
1793 * fuse_set/release_nowrite. Then we're on safe side because
1794 * fuse_flush_writepages() would call fuse_send_writepage()
1797 * 2nd case: someone called fuse_set_nowrite and it is waiting
1798 * now for completion of all in-flight requests. This happens
1799 * rarely and no more than once per page, so this should be
1802 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1803 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1804 * that fuse_set_nowrite returned implies that all in-flight
1805 * requests were completed along with all of their secondary
1806 * requests. Further primary requests are blocked by negative
1807 * writectr. Hence there cannot be any in-flight requests and
1808 * no invocations of fuse_writepage_end() while we're in
1809 * fuse_set_nowrite..fuse_release_nowrite section.
1811 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
1814 fuse_writepage_finish(fm, wpa);
1815 spin_unlock(&fi->lock);
1816 fuse_writepage_free(wpa);
1819 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1820 struct fuse_inode *fi)
1822 struct fuse_file *ff = NULL;
1824 spin_lock(&fi->lock);
1825 if (!list_empty(&fi->write_files)) {
1826 ff = list_entry(fi->write_files.next, struct fuse_file,
1830 spin_unlock(&fi->lock);
1835 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1836 struct fuse_inode *fi)
1838 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1843 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1845 struct fuse_conn *fc = get_fuse_conn(inode);
1846 struct fuse_inode *fi = get_fuse_inode(inode);
1847 struct fuse_file *ff;
1850 ff = __fuse_write_file_get(fc, fi);
1851 err = fuse_flush_times(inode, ff);
1853 fuse_file_put(ff, false, false);
1858 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1860 struct fuse_writepage_args *wpa;
1861 struct fuse_args_pages *ap;
1863 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1867 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1877 static int fuse_writepage_locked(struct page *page)
1879 struct address_space *mapping = page->mapping;
1880 struct inode *inode = mapping->host;
1881 struct fuse_conn *fc = get_fuse_conn(inode);
1882 struct fuse_inode *fi = get_fuse_inode(inode);
1883 struct fuse_writepage_args *wpa;
1884 struct fuse_args_pages *ap;
1885 struct page *tmp_page;
1886 int error = -ENOMEM;
1888 set_page_writeback(page);
1890 wpa = fuse_writepage_args_alloc();
1895 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1900 wpa->ia.ff = fuse_write_file_get(fc, fi);
1904 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1906 copy_highpage(tmp_page, page);
1907 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1909 ap->args.in_pages = true;
1911 ap->pages[0] = tmp_page;
1912 ap->descs[0].offset = 0;
1913 ap->descs[0].length = PAGE_SIZE;
1914 ap->args.end = fuse_writepage_end;
1917 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1918 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1920 spin_lock(&fi->lock);
1921 tree_insert(&fi->writepages, wpa);
1922 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1923 fuse_flush_writepages(inode);
1924 spin_unlock(&fi->lock);
1926 end_page_writeback(page);
1931 __free_page(tmp_page);
1935 mapping_set_error(page->mapping, error);
1936 end_page_writeback(page);
1940 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1944 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1946 * ->writepages() should be called for sync() and friends. We
1947 * should only get here on direct reclaim and then we are
1948 * allowed to skip a page which is already in flight
1950 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1952 redirty_page_for_writepage(wbc, page);
1957 err = fuse_writepage_locked(page);
1963 struct fuse_fill_wb_data {
1964 struct fuse_writepage_args *wpa;
1965 struct fuse_file *ff;
1966 struct inode *inode;
1967 struct page **orig_pages;
1968 unsigned int max_pages;
1971 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1973 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1974 struct fuse_conn *fc = get_fuse_conn(data->inode);
1975 struct page **pages;
1976 struct fuse_page_desc *descs;
1977 unsigned int npages = min_t(unsigned int,
1978 max_t(unsigned int, data->max_pages * 2,
1979 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1981 WARN_ON(npages <= data->max_pages);
1983 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1987 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1988 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1992 data->max_pages = npages;
1997 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1999 struct fuse_writepage_args *wpa = data->wpa;
2000 struct inode *inode = data->inode;
2001 struct fuse_inode *fi = get_fuse_inode(inode);
2002 int num_pages = wpa->ia.ap.num_pages;
2005 wpa->ia.ff = fuse_file_get(data->ff);
2006 spin_lock(&fi->lock);
2007 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2008 fuse_flush_writepages(inode);
2009 spin_unlock(&fi->lock);
2011 for (i = 0; i < num_pages; i++)
2012 end_page_writeback(data->orig_pages[i]);
2016 * Check under fi->lock if the page is under writeback, and insert it onto the
2017 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2018 * one already added for a page at this offset. If there's none, then insert
2019 * this new request onto the auxiliary list, otherwise reuse the existing one by
2020 * swapping the new temp page with the old one.
2022 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2025 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2026 struct fuse_writepage_args *tmp;
2027 struct fuse_writepage_args *old_wpa;
2028 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
2030 WARN_ON(new_ap->num_pages != 0);
2031 new_ap->num_pages = 1;
2033 spin_lock(&fi->lock);
2034 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
2036 spin_unlock(&fi->lock);
2040 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2043 WARN_ON(tmp->inode != new_wpa->inode);
2044 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2045 if (curr_index == page->index) {
2046 WARN_ON(tmp->ia.ap.num_pages != 1);
2047 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2053 new_wpa->next = old_wpa->next;
2054 old_wpa->next = new_wpa;
2057 spin_unlock(&fi->lock);
2060 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2062 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2063 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2064 wb_writeout_inc(&bdi->wb);
2065 fuse_writepage_free(new_wpa);
2071 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2072 struct fuse_args_pages *ap,
2073 struct fuse_fill_wb_data *data)
2075 WARN_ON(!ap->num_pages);
2078 * Being under writeback is unlikely but possible. For example direct
2079 * read to an mmaped fuse file will set the page dirty twice; once when
2080 * the pages are faulted with get_user_pages(), and then after the read
2083 if (fuse_page_is_writeback(data->inode, page->index))
2086 /* Reached max pages */
2087 if (ap->num_pages == fc->max_pages)
2090 /* Reached max write bytes */
2091 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2095 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2098 /* Need to grow the pages array? If so, did the expansion fail? */
2099 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2105 static int fuse_writepages_fill(struct page *page,
2106 struct writeback_control *wbc, void *_data)
2108 struct fuse_fill_wb_data *data = _data;
2109 struct fuse_writepage_args *wpa = data->wpa;
2110 struct fuse_args_pages *ap = &wpa->ia.ap;
2111 struct inode *inode = data->inode;
2112 struct fuse_inode *fi = get_fuse_inode(inode);
2113 struct fuse_conn *fc = get_fuse_conn(inode);
2114 struct page *tmp_page;
2119 data->ff = fuse_write_file_get(fc, fi);
2124 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
2125 fuse_writepages_send(data);
2130 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2135 * The page must not be redirtied until the writeout is completed
2136 * (i.e. userspace has sent a reply to the write request). Otherwise
2137 * there could be more than one temporary page instance for each real
2140 * This is ensured by holding the page lock in page_mkwrite() while
2141 * checking fuse_page_is_writeback(). We already hold the page lock
2142 * since clear_page_dirty_for_io() and keep it held until we add the
2143 * request to the fi->writepages list and increment ap->num_pages.
2144 * After this fuse_page_is_writeback() will indicate that the page is
2145 * under writeback, so we can release the page lock.
2147 if (data->wpa == NULL) {
2149 wpa = fuse_writepage_args_alloc();
2151 __free_page(tmp_page);
2154 data->max_pages = 1;
2157 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2158 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2160 ap->args.in_pages = true;
2161 ap->args.end = fuse_writepage_end;
2165 set_page_writeback(page);
2167 copy_highpage(tmp_page, page);
2168 ap->pages[ap->num_pages] = tmp_page;
2169 ap->descs[ap->num_pages].offset = 0;
2170 ap->descs[ap->num_pages].length = PAGE_SIZE;
2171 data->orig_pages[ap->num_pages] = page;
2173 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2174 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2179 * Protected by fi->lock against concurrent access by
2180 * fuse_page_is_writeback().
2182 spin_lock(&fi->lock);
2184 spin_unlock(&fi->lock);
2185 } else if (fuse_writepage_add(wpa, page)) {
2188 end_page_writeback(page);
2196 static int fuse_writepages(struct address_space *mapping,
2197 struct writeback_control *wbc)
2199 struct inode *inode = mapping->host;
2200 struct fuse_conn *fc = get_fuse_conn(inode);
2201 struct fuse_fill_wb_data data;
2205 if (fuse_is_bad(inode))
2213 data.orig_pages = kcalloc(fc->max_pages,
2214 sizeof(struct page *),
2216 if (!data.orig_pages)
2219 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2221 WARN_ON(!data.wpa->ia.ap.num_pages);
2222 fuse_writepages_send(&data);
2225 fuse_file_put(data.ff, false, false);
2227 kfree(data.orig_pages);
2233 * It's worthy to make sure that space is reserved on disk for the write,
2234 * but how to implement it without killing performance need more thinking.
2236 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2237 loff_t pos, unsigned len, unsigned flags,
2238 struct page **pagep, void **fsdata)
2240 pgoff_t index = pos >> PAGE_SHIFT;
2241 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2246 WARN_ON(!fc->writeback_cache);
2248 page = grab_cache_page_write_begin(mapping, index, flags);
2252 fuse_wait_on_page_writeback(mapping->host, page->index);
2254 if (PageUptodate(page) || len == PAGE_SIZE)
2257 * Check if the start this page comes after the end of file, in which
2258 * case the readpage can be optimized away.
2260 fsize = i_size_read(mapping->host);
2261 if (fsize <= (pos & PAGE_MASK)) {
2262 size_t off = pos & ~PAGE_MASK;
2264 zero_user_segment(page, 0, off);
2267 err = fuse_do_readpage(file, page);
2281 static int fuse_write_end(struct file *file, struct address_space *mapping,
2282 loff_t pos, unsigned len, unsigned copied,
2283 struct page *page, void *fsdata)
2285 struct inode *inode = page->mapping->host;
2287 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2291 if (!PageUptodate(page)) {
2292 /* Zero any unwritten bytes at the end of the page */
2293 size_t endoff = (pos + copied) & ~PAGE_MASK;
2295 zero_user_segment(page, endoff, PAGE_SIZE);
2296 SetPageUptodate(page);
2299 fuse_write_update_size(inode, pos + copied);
2300 set_page_dirty(page);
2309 static int fuse_launder_page(struct page *page)
2312 if (clear_page_dirty_for_io(page)) {
2313 struct inode *inode = page->mapping->host;
2315 /* Serialize with pending writeback for the same page */
2316 fuse_wait_on_page_writeback(inode, page->index);
2317 err = fuse_writepage_locked(page);
2319 fuse_wait_on_page_writeback(inode, page->index);
2325 * Write back dirty pages now, because there may not be any suitable
2328 static void fuse_vma_close(struct vm_area_struct *vma)
2330 filemap_write_and_wait(vma->vm_file->f_mapping);
2334 * Wait for writeback against this page to complete before allowing it
2335 * to be marked dirty again, and hence written back again, possibly
2336 * before the previous writepage completed.
2338 * Block here, instead of in ->writepage(), so that the userspace fs
2339 * can only block processes actually operating on the filesystem.
2341 * Otherwise unprivileged userspace fs would be able to block
2346 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2348 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2350 struct page *page = vmf->page;
2351 struct inode *inode = file_inode(vmf->vma->vm_file);
2353 file_update_time(vmf->vma->vm_file);
2355 if (page->mapping != inode->i_mapping) {
2357 return VM_FAULT_NOPAGE;
2360 fuse_wait_on_page_writeback(inode, page->index);
2361 return VM_FAULT_LOCKED;
2364 static const struct vm_operations_struct fuse_file_vm_ops = {
2365 .close = fuse_vma_close,
2366 .fault = filemap_fault,
2367 .map_pages = filemap_map_pages,
2368 .page_mkwrite = fuse_page_mkwrite,
2371 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2373 struct fuse_file *ff = file->private_data;
2375 /* DAX mmap is superior to direct_io mmap */
2376 if (FUSE_IS_DAX(file_inode(file)))
2377 return fuse_dax_mmap(file, vma);
2379 if (ff->open_flags & FOPEN_DIRECT_IO) {
2380 /* Can't provide the coherency needed for MAP_SHARED */
2381 if (vma->vm_flags & VM_MAYSHARE)
2384 invalidate_inode_pages2(file->f_mapping);
2386 return generic_file_mmap(file, vma);
2389 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2390 fuse_link_write_file(file);
2392 file_accessed(file);
2393 vma->vm_ops = &fuse_file_vm_ops;
2397 static int convert_fuse_file_lock(struct fuse_conn *fc,
2398 const struct fuse_file_lock *ffl,
2399 struct file_lock *fl)
2401 switch (ffl->type) {
2407 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2408 ffl->end < ffl->start)
2411 fl->fl_start = ffl->start;
2412 fl->fl_end = ffl->end;
2415 * Convert pid into init's pid namespace. The locks API will
2416 * translate it into the caller's pid namespace.
2419 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2426 fl->fl_type = ffl->type;
2430 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2431 const struct file_lock *fl, int opcode, pid_t pid,
2432 int flock, struct fuse_lk_in *inarg)
2434 struct inode *inode = file_inode(file);
2435 struct fuse_conn *fc = get_fuse_conn(inode);
2436 struct fuse_file *ff = file->private_data;
2438 memset(inarg, 0, sizeof(*inarg));
2440 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2441 inarg->lk.start = fl->fl_start;
2442 inarg->lk.end = fl->fl_end;
2443 inarg->lk.type = fl->fl_type;
2444 inarg->lk.pid = pid;
2446 inarg->lk_flags |= FUSE_LK_FLOCK;
2447 args->opcode = opcode;
2448 args->nodeid = get_node_id(inode);
2449 args->in_numargs = 1;
2450 args->in_args[0].size = sizeof(*inarg);
2451 args->in_args[0].value = inarg;
2454 static int fuse_getlk(struct file *file, struct file_lock *fl)
2456 struct inode *inode = file_inode(file);
2457 struct fuse_mount *fm = get_fuse_mount(inode);
2459 struct fuse_lk_in inarg;
2460 struct fuse_lk_out outarg;
2463 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2464 args.out_numargs = 1;
2465 args.out_args[0].size = sizeof(outarg);
2466 args.out_args[0].value = &outarg;
2467 err = fuse_simple_request(fm, &args);
2469 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2474 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2476 struct inode *inode = file_inode(file);
2477 struct fuse_mount *fm = get_fuse_mount(inode);
2479 struct fuse_lk_in inarg;
2480 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2481 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2482 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2485 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2486 /* NLM needs asynchronous locks, which we don't support yet */
2490 /* Unlock on close is handled by the flush method */
2491 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2494 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2495 err = fuse_simple_request(fm, &args);
2497 /* locking is restartable */
2504 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2506 struct inode *inode = file_inode(file);
2507 struct fuse_conn *fc = get_fuse_conn(inode);
2510 if (cmd == F_CANCELLK) {
2512 } else if (cmd == F_GETLK) {
2514 posix_test_lock(file, fl);
2517 err = fuse_getlk(file, fl);
2520 err = posix_lock_file(file, fl, NULL);
2522 err = fuse_setlk(file, fl, 0);
2527 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2529 struct inode *inode = file_inode(file);
2530 struct fuse_conn *fc = get_fuse_conn(inode);
2534 err = locks_lock_file_wait(file, fl);
2536 struct fuse_file *ff = file->private_data;
2538 /* emulate flock with POSIX locks */
2540 err = fuse_setlk(file, fl, 1);
2546 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2548 struct inode *inode = mapping->host;
2549 struct fuse_mount *fm = get_fuse_mount(inode);
2551 struct fuse_bmap_in inarg;
2552 struct fuse_bmap_out outarg;
2555 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2558 memset(&inarg, 0, sizeof(inarg));
2559 inarg.block = block;
2560 inarg.blocksize = inode->i_sb->s_blocksize;
2561 args.opcode = FUSE_BMAP;
2562 args.nodeid = get_node_id(inode);
2563 args.in_numargs = 1;
2564 args.in_args[0].size = sizeof(inarg);
2565 args.in_args[0].value = &inarg;
2566 args.out_numargs = 1;
2567 args.out_args[0].size = sizeof(outarg);
2568 args.out_args[0].value = &outarg;
2569 err = fuse_simple_request(fm, &args);
2571 fm->fc->no_bmap = 1;
2573 return err ? 0 : outarg.block;
2576 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2578 struct inode *inode = file->f_mapping->host;
2579 struct fuse_mount *fm = get_fuse_mount(inode);
2580 struct fuse_file *ff = file->private_data;
2582 struct fuse_lseek_in inarg = {
2587 struct fuse_lseek_out outarg;
2590 if (fm->fc->no_lseek)
2593 args.opcode = FUSE_LSEEK;
2594 args.nodeid = ff->nodeid;
2595 args.in_numargs = 1;
2596 args.in_args[0].size = sizeof(inarg);
2597 args.in_args[0].value = &inarg;
2598 args.out_numargs = 1;
2599 args.out_args[0].size = sizeof(outarg);
2600 args.out_args[0].value = &outarg;
2601 err = fuse_simple_request(fm, &args);
2603 if (err == -ENOSYS) {
2604 fm->fc->no_lseek = 1;
2610 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2613 err = fuse_update_attributes(inode, file);
2615 return generic_file_llseek(file, offset, whence);
2620 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2623 struct inode *inode = file_inode(file);
2628 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2629 retval = generic_file_llseek(file, offset, whence);
2633 retval = fuse_update_attributes(inode, file);
2635 retval = generic_file_llseek(file, offset, whence);
2636 inode_unlock(inode);
2641 retval = fuse_lseek(file, offset, whence);
2642 inode_unlock(inode);
2652 * All files which have been polled are linked to RB tree
2653 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2654 * find the matching one.
2656 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2657 struct rb_node **parent_out)
2659 struct rb_node **link = &fc->polled_files.rb_node;
2660 struct rb_node *last = NULL;
2663 struct fuse_file *ff;
2666 ff = rb_entry(last, struct fuse_file, polled_node);
2669 link = &last->rb_left;
2670 else if (kh > ff->kh)
2671 link = &last->rb_right;
2682 * The file is about to be polled. Make sure it's on the polled_files
2683 * RB tree. Note that files once added to the polled_files tree are
2684 * not removed before the file is released. This is because a file
2685 * polled once is likely to be polled again.
2687 static void fuse_register_polled_file(struct fuse_conn *fc,
2688 struct fuse_file *ff)
2690 spin_lock(&fc->lock);
2691 if (RB_EMPTY_NODE(&ff->polled_node)) {
2692 struct rb_node **link, *parent;
2694 link = fuse_find_polled_node(fc, ff->kh, &parent);
2696 rb_link_node(&ff->polled_node, parent, link);
2697 rb_insert_color(&ff->polled_node, &fc->polled_files);
2699 spin_unlock(&fc->lock);
2702 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2704 struct fuse_file *ff = file->private_data;
2705 struct fuse_mount *fm = ff->fm;
2706 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2707 struct fuse_poll_out outarg;
2711 if (fm->fc->no_poll)
2712 return DEFAULT_POLLMASK;
2714 poll_wait(file, &ff->poll_wait, wait);
2715 inarg.events = mangle_poll(poll_requested_events(wait));
2718 * Ask for notification iff there's someone waiting for it.
2719 * The client may ignore the flag and always notify.
2721 if (waitqueue_active(&ff->poll_wait)) {
2722 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2723 fuse_register_polled_file(fm->fc, ff);
2726 args.opcode = FUSE_POLL;
2727 args.nodeid = ff->nodeid;
2728 args.in_numargs = 1;
2729 args.in_args[0].size = sizeof(inarg);
2730 args.in_args[0].value = &inarg;
2731 args.out_numargs = 1;
2732 args.out_args[0].size = sizeof(outarg);
2733 args.out_args[0].value = &outarg;
2734 err = fuse_simple_request(fm, &args);
2737 return demangle_poll(outarg.revents);
2738 if (err == -ENOSYS) {
2739 fm->fc->no_poll = 1;
2740 return DEFAULT_POLLMASK;
2744 EXPORT_SYMBOL_GPL(fuse_file_poll);
2747 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2748 * wakes up the poll waiters.
2750 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2751 struct fuse_notify_poll_wakeup_out *outarg)
2753 u64 kh = outarg->kh;
2754 struct rb_node **link;
2756 spin_lock(&fc->lock);
2758 link = fuse_find_polled_node(fc, kh, NULL);
2760 struct fuse_file *ff;
2762 ff = rb_entry(*link, struct fuse_file, polled_node);
2763 wake_up_interruptible_sync(&ff->poll_wait);
2766 spin_unlock(&fc->lock);
2770 static void fuse_do_truncate(struct file *file)
2772 struct inode *inode = file->f_mapping->host;
2775 attr.ia_valid = ATTR_SIZE;
2776 attr.ia_size = i_size_read(inode);
2778 attr.ia_file = file;
2779 attr.ia_valid |= ATTR_FILE;
2781 fuse_do_setattr(file_dentry(file), &attr, file);
2784 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2786 return round_up(off, fc->max_pages << PAGE_SHIFT);
2790 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2792 DECLARE_COMPLETION_ONSTACK(wait);
2794 struct file *file = iocb->ki_filp;
2795 struct fuse_file *ff = file->private_data;
2797 struct inode *inode;
2799 size_t count = iov_iter_count(iter), shortened = 0;
2800 loff_t offset = iocb->ki_pos;
2801 struct fuse_io_priv *io;
2804 inode = file->f_mapping->host;
2805 i_size = i_size_read(inode);
2807 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
2810 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2813 spin_lock_init(&io->lock);
2814 kref_init(&io->refcnt);
2818 io->offset = offset;
2819 io->write = (iov_iter_rw(iter) == WRITE);
2822 * By default, we want to optimize all I/Os with async request
2823 * submission to the client filesystem if supported.
2825 io->async = ff->fm->fc->async_dio;
2827 io->blocking = is_sync_kiocb(iocb);
2829 /* optimization for short read */
2830 if (io->async && !io->write && offset + count > i_size) {
2831 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
2832 shortened = count - iov_iter_count(iter);
2837 * We cannot asynchronously extend the size of a file.
2838 * In such case the aio will behave exactly like sync io.
2840 if ((offset + count > i_size) && io->write)
2841 io->blocking = true;
2843 if (io->async && io->blocking) {
2845 * Additional reference to keep io around after
2846 * calling fuse_aio_complete()
2848 kref_get(&io->refcnt);
2852 if (iov_iter_rw(iter) == WRITE) {
2853 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2854 fuse_invalidate_attr(inode);
2856 ret = __fuse_direct_read(io, iter, &pos);
2858 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
2861 bool blocking = io->blocking;
2863 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2865 /* we have a non-extending, async request, so return */
2867 return -EIOCBQUEUED;
2869 wait_for_completion(&wait);
2870 ret = fuse_get_res_by_io(io);
2873 kref_put(&io->refcnt, fuse_io_release);
2875 if (iov_iter_rw(iter) == WRITE) {
2877 fuse_write_update_size(inode, pos);
2878 else if (ret < 0 && offset + count > i_size)
2879 fuse_do_truncate(file);
2885 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
2887 int err = filemap_write_and_wait_range(inode->i_mapping, start, -1);
2890 fuse_sync_writes(inode);
2895 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2898 struct fuse_file *ff = file->private_data;
2899 struct inode *inode = file_inode(file);
2900 struct fuse_inode *fi = get_fuse_inode(inode);
2901 struct fuse_mount *fm = ff->fm;
2903 struct fuse_fallocate_in inarg = {
2910 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2911 (mode & (FALLOC_FL_PUNCH_HOLE |
2912 FALLOC_FL_ZERO_RANGE));
2914 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
2916 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2917 FALLOC_FL_ZERO_RANGE))
2920 if (fm->fc->no_fallocate)
2926 down_write(&fi->i_mmap_sem);
2927 err = fuse_dax_break_layouts(inode, 0, 0);
2932 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
2933 loff_t endbyte = offset + length - 1;
2935 err = fuse_writeback_range(inode, offset, endbyte);
2941 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2942 offset + length > i_size_read(inode)) {
2943 err = inode_newsize_ok(inode, offset + length);
2948 if (!(mode & FALLOC_FL_KEEP_SIZE))
2949 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2951 args.opcode = FUSE_FALLOCATE;
2952 args.nodeid = ff->nodeid;
2953 args.in_numargs = 1;
2954 args.in_args[0].size = sizeof(inarg);
2955 args.in_args[0].value = &inarg;
2956 err = fuse_simple_request(fm, &args);
2957 if (err == -ENOSYS) {
2958 fm->fc->no_fallocate = 1;
2964 /* we could have extended the file */
2965 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2966 bool changed = fuse_write_update_size(inode, offset + length);
2968 if (changed && fm->fc->writeback_cache)
2969 file_update_time(file);
2972 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
2973 truncate_pagecache_range(inode, offset, offset + length - 1);
2975 fuse_invalidate_attr(inode);
2978 if (!(mode & FALLOC_FL_KEEP_SIZE))
2979 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2982 up_write(&fi->i_mmap_sem);
2985 inode_unlock(inode);
2990 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
2991 struct file *file_out, loff_t pos_out,
2992 size_t len, unsigned int flags)
2994 struct fuse_file *ff_in = file_in->private_data;
2995 struct fuse_file *ff_out = file_out->private_data;
2996 struct inode *inode_in = file_inode(file_in);
2997 struct inode *inode_out = file_inode(file_out);
2998 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
2999 struct fuse_mount *fm = ff_in->fm;
3000 struct fuse_conn *fc = fm->fc;
3002 struct fuse_copy_file_range_in inarg = {
3005 .nodeid_out = ff_out->nodeid,
3006 .fh_out = ff_out->fh,
3011 struct fuse_write_out outarg;
3013 /* mark unstable when write-back is not used, and file_out gets
3015 bool is_unstable = (!fc->writeback_cache) &&
3016 ((pos_out + len) > inode_out->i_size);
3018 if (fc->no_copy_file_range)
3021 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3024 inode_lock(inode_in);
3025 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3026 inode_unlock(inode_in);
3030 inode_lock(inode_out);
3032 err = file_modified(file_out);
3037 * Write out dirty pages in the destination file before sending the COPY
3038 * request to userspace. After the request is completed, truncate off
3039 * pages (including partial ones) from the cache that have been copied,
3040 * since these contain stale data at that point.
3042 * This should be mostly correct, but if the COPY writes to partial
3043 * pages (at the start or end) and the parts not covered by the COPY are
3044 * written through a memory map after calling fuse_writeback_range(),
3045 * then these partial page modifications will be lost on truncation.
3047 * It is unlikely that someone would rely on such mixed style
3048 * modifications. Yet this does give less guarantees than if the
3049 * copying was performed with write(2).
3051 * To fix this a i_mmap_sem style lock could be used to prevent new
3052 * faults while the copy is ongoing.
3054 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3059 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3061 args.opcode = FUSE_COPY_FILE_RANGE;
3062 args.nodeid = ff_in->nodeid;
3063 args.in_numargs = 1;
3064 args.in_args[0].size = sizeof(inarg);
3065 args.in_args[0].value = &inarg;
3066 args.out_numargs = 1;
3067 args.out_args[0].size = sizeof(outarg);
3068 args.out_args[0].value = &outarg;
3069 err = fuse_simple_request(fm, &args);
3070 if (err == -ENOSYS) {
3071 fc->no_copy_file_range = 1;
3077 truncate_inode_pages_range(inode_out->i_mapping,
3078 ALIGN_DOWN(pos_out, PAGE_SIZE),
3079 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3081 if (fc->writeback_cache) {
3082 fuse_write_update_size(inode_out, pos_out + outarg.size);
3083 file_update_time(file_out);
3086 fuse_invalidate_attr(inode_out);
3091 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3093 inode_unlock(inode_out);
3094 file_accessed(file_in);
3099 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3100 struct file *dst_file, loff_t dst_off,
3101 size_t len, unsigned int flags)
3105 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3108 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3109 ret = generic_copy_file_range(src_file, src_off, dst_file,
3110 dst_off, len, flags);
3114 static const struct file_operations fuse_file_operations = {
3115 .llseek = fuse_file_llseek,
3116 .read_iter = fuse_file_read_iter,
3117 .write_iter = fuse_file_write_iter,
3118 .mmap = fuse_file_mmap,
3120 .flush = fuse_flush,
3121 .release = fuse_release,
3122 .fsync = fuse_fsync,
3123 .lock = fuse_file_lock,
3124 .get_unmapped_area = thp_get_unmapped_area,
3125 .flock = fuse_file_flock,
3126 .splice_read = generic_file_splice_read,
3127 .splice_write = iter_file_splice_write,
3128 .unlocked_ioctl = fuse_file_ioctl,
3129 .compat_ioctl = fuse_file_compat_ioctl,
3130 .poll = fuse_file_poll,
3131 .fallocate = fuse_file_fallocate,
3132 .copy_file_range = fuse_copy_file_range,
3135 static const struct address_space_operations fuse_file_aops = {
3136 .readpage = fuse_readpage,
3137 .readahead = fuse_readahead,
3138 .writepage = fuse_writepage,
3139 .writepages = fuse_writepages,
3140 .launder_page = fuse_launder_page,
3141 .set_page_dirty = __set_page_dirty_nobuffers,
3143 .direct_IO = fuse_direct_IO,
3144 .write_begin = fuse_write_begin,
3145 .write_end = fuse_write_end,
3148 void fuse_init_file_inode(struct inode *inode)
3150 struct fuse_inode *fi = get_fuse_inode(inode);
3152 inode->i_fop = &fuse_file_operations;
3153 inode->i_data.a_ops = &fuse_file_aops;
3155 INIT_LIST_HEAD(&fi->write_files);
3156 INIT_LIST_HEAD(&fi->queued_writes);
3158 init_waitqueue_head(&fi->page_waitq);
3159 fi->writepages = RB_ROOT;
3161 if (IS_ENABLED(CONFIG_FUSE_DAX))
3162 fuse_dax_inode_init(inode);