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 file_update_time(file);
214 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
216 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
217 fuse_link_write_file(file);
220 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
222 struct fuse_mount *fm = get_fuse_mount(inode);
223 struct fuse_conn *fc = fm->fc;
225 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
226 fc->atomic_o_trunc &&
228 bool dax_truncate = (file->f_flags & O_TRUNC) &&
229 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
231 if (fuse_is_bad(inode))
234 err = generic_file_open(inode, file);
238 if (is_wb_truncate || dax_truncate)
242 filemap_invalidate_lock(inode->i_mapping);
243 err = fuse_dax_break_layouts(inode, 0, 0);
245 goto out_inode_unlock;
248 if (is_wb_truncate || dax_truncate)
249 fuse_set_nowrite(inode);
251 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
253 fuse_finish_open(inode, file);
255 if (is_wb_truncate || dax_truncate)
256 fuse_release_nowrite(inode);
258 struct fuse_file *ff = file->private_data;
260 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
261 truncate_pagecache(inode, 0);
262 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
263 invalidate_inode_pages2(inode->i_mapping);
266 filemap_invalidate_unlock(inode->i_mapping);
268 if (is_wb_truncate || dax_truncate)
274 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
275 unsigned int flags, int opcode)
277 struct fuse_conn *fc = ff->fm->fc;
278 struct fuse_release_args *ra = ff->release_args;
280 /* Inode is NULL on error path of fuse_create_open() */
282 spin_lock(&fi->lock);
283 list_del(&ff->write_entry);
284 spin_unlock(&fi->lock);
286 spin_lock(&fc->lock);
287 if (!RB_EMPTY_NODE(&ff->polled_node))
288 rb_erase(&ff->polled_node, &fc->polled_files);
289 spin_unlock(&fc->lock);
291 wake_up_interruptible_all(&ff->poll_wait);
293 ra->inarg.fh = ff->fh;
294 ra->inarg.flags = flags;
295 ra->args.in_numargs = 1;
296 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
297 ra->args.in_args[0].value = &ra->inarg;
298 ra->args.opcode = opcode;
299 ra->args.nodeid = ff->nodeid;
300 ra->args.force = true;
301 ra->args.nocreds = true;
304 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
305 unsigned int open_flags, fl_owner_t id, bool isdir)
307 struct fuse_inode *fi = get_fuse_inode(inode);
308 struct fuse_release_args *ra = ff->release_args;
309 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
311 fuse_prepare_release(fi, ff, open_flags, opcode);
314 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
315 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
317 /* Hold inode until release is finished */
318 ra->inode = igrab(inode);
321 * Normally this will send the RELEASE request, however if
322 * some asynchronous READ or WRITE requests are outstanding,
323 * the sending will be delayed.
325 * Make the release synchronous if this is a fuseblk mount,
326 * synchronous RELEASE is allowed (and desirable) in this case
327 * because the server can be trusted not to screw up.
329 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
332 void fuse_release_common(struct file *file, bool isdir)
334 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
335 (fl_owner_t) file, isdir);
338 static int fuse_open(struct inode *inode, struct file *file)
340 return fuse_open_common(inode, file, false);
343 static int fuse_release(struct inode *inode, struct file *file)
345 struct fuse_conn *fc = get_fuse_conn(inode);
348 * Dirty pages might remain despite write_inode_now() call from
349 * fuse_flush() due to writes racing with the close.
351 if (fc->writeback_cache)
352 write_inode_now(inode, 1);
354 fuse_release_common(file, false);
356 /* return value is ignored by VFS */
360 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
363 WARN_ON(refcount_read(&ff->count) > 1);
364 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
366 * iput(NULL) is a no-op and since the refcount is 1 and everything's
367 * synchronous, we are fine with not doing igrab() here"
369 fuse_file_put(ff, true, false);
371 EXPORT_SYMBOL_GPL(fuse_sync_release);
374 * Scramble the ID space with XTEA, so that the value of the files_struct
375 * pointer is not exposed to userspace.
377 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
379 u32 *k = fc->scramble_key;
380 u64 v = (unsigned long) id;
386 for (i = 0; i < 32; i++) {
387 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
389 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
392 return (u64) v0 + ((u64) v1 << 32);
395 struct fuse_writepage_args {
396 struct fuse_io_args ia;
397 struct rb_node writepages_entry;
398 struct list_head queue_entry;
399 struct fuse_writepage_args *next;
401 struct fuse_sync_bucket *bucket;
404 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
405 pgoff_t idx_from, pgoff_t idx_to)
409 n = fi->writepages.rb_node;
412 struct fuse_writepage_args *wpa;
415 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
416 WARN_ON(get_fuse_inode(wpa->inode) != fi);
417 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
418 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
420 else if (idx_to < curr_index)
429 * Check if any page in a range is under writeback
431 * This is currently done by walking the list of writepage requests
432 * for the inode, which can be pretty inefficient.
434 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
437 struct fuse_inode *fi = get_fuse_inode(inode);
440 spin_lock(&fi->lock);
441 found = fuse_find_writeback(fi, idx_from, idx_to);
442 spin_unlock(&fi->lock);
447 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
449 return fuse_range_is_writeback(inode, index, index);
453 * Wait for page writeback to be completed.
455 * Since fuse doesn't rely on the VM writeback tracking, this has to
456 * use some other means.
458 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
460 struct fuse_inode *fi = get_fuse_inode(inode);
462 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
466 * Wait for all pending writepages on the inode to finish.
468 * This is currently done by blocking further writes with FUSE_NOWRITE
469 * and waiting for all sent writes to complete.
471 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
472 * could conflict with truncation.
474 static void fuse_sync_writes(struct inode *inode)
476 fuse_set_nowrite(inode);
477 fuse_release_nowrite(inode);
480 static int fuse_flush(struct file *file, fl_owner_t id)
482 struct inode *inode = file_inode(file);
483 struct fuse_mount *fm = get_fuse_mount(inode);
484 struct fuse_file *ff = file->private_data;
485 struct fuse_flush_in inarg;
489 if (fuse_is_bad(inode))
492 if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
495 err = write_inode_now(inode, 1);
500 fuse_sync_writes(inode);
503 err = filemap_check_errors(file->f_mapping);
508 if (fm->fc->no_flush)
511 memset(&inarg, 0, sizeof(inarg));
513 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
514 args.opcode = FUSE_FLUSH;
515 args.nodeid = get_node_id(inode);
517 args.in_args[0].size = sizeof(inarg);
518 args.in_args[0].value = &inarg;
521 err = fuse_simple_request(fm, &args);
522 if (err == -ENOSYS) {
523 fm->fc->no_flush = 1;
529 * In memory i_blocks is not maintained by fuse, if writeback cache is
530 * enabled, i_blocks from cached attr may not be accurate.
532 if (!err && fm->fc->writeback_cache)
533 fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
537 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
538 int datasync, int opcode)
540 struct inode *inode = file->f_mapping->host;
541 struct fuse_mount *fm = get_fuse_mount(inode);
542 struct fuse_file *ff = file->private_data;
544 struct fuse_fsync_in inarg;
546 memset(&inarg, 0, sizeof(inarg));
548 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
549 args.opcode = opcode;
550 args.nodeid = get_node_id(inode);
552 args.in_args[0].size = sizeof(inarg);
553 args.in_args[0].value = &inarg;
554 return fuse_simple_request(fm, &args);
557 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
560 struct inode *inode = file->f_mapping->host;
561 struct fuse_conn *fc = get_fuse_conn(inode);
564 if (fuse_is_bad(inode))
570 * Start writeback against all dirty pages of the inode, then
571 * wait for all outstanding writes, before sending the FSYNC
574 err = file_write_and_wait_range(file, start, end);
578 fuse_sync_writes(inode);
581 * Due to implementation of fuse writeback
582 * file_write_and_wait_range() does not catch errors.
583 * We have to do this directly after fuse_sync_writes()
585 err = file_check_and_advance_wb_err(file);
589 err = sync_inode_metadata(inode, 1);
596 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
597 if (err == -ENOSYS) {
607 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
608 size_t count, int opcode)
610 struct fuse_file *ff = file->private_data;
611 struct fuse_args *args = &ia->ap.args;
613 ia->read.in.fh = ff->fh;
614 ia->read.in.offset = pos;
615 ia->read.in.size = count;
616 ia->read.in.flags = file->f_flags;
617 args->opcode = opcode;
618 args->nodeid = ff->nodeid;
619 args->in_numargs = 1;
620 args->in_args[0].size = sizeof(ia->read.in);
621 args->in_args[0].value = &ia->read.in;
622 args->out_argvar = true;
623 args->out_numargs = 1;
624 args->out_args[0].size = count;
627 static void fuse_release_user_pages(struct fuse_args_pages *ap,
632 for (i = 0; i < ap->num_pages; i++) {
634 set_page_dirty_lock(ap->pages[i]);
635 put_page(ap->pages[i]);
639 static void fuse_io_release(struct kref *kref)
641 kfree(container_of(kref, struct fuse_io_priv, refcnt));
644 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
649 if (io->bytes >= 0 && io->write)
652 return io->bytes < 0 ? io->size : io->bytes;
656 * In case of short read, the caller sets 'pos' to the position of
657 * actual end of fuse request in IO request. Otherwise, if bytes_requested
658 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
661 * User requested DIO read of 64K. It was split into two 32K fuse requests,
662 * both submitted asynchronously. The first of them was ACKed by userspace as
663 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
664 * second request was ACKed as short, e.g. only 1K was read, resulting in
667 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
668 * will be equal to the length of the longest contiguous fragment of
669 * transferred data starting from the beginning of IO request.
671 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
675 spin_lock(&io->lock);
677 io->err = io->err ? : err;
678 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
682 if (!left && io->blocking)
684 spin_unlock(&io->lock);
686 if (!left && !io->blocking) {
687 ssize_t res = fuse_get_res_by_io(io);
690 struct inode *inode = file_inode(io->iocb->ki_filp);
691 struct fuse_conn *fc = get_fuse_conn(inode);
692 struct fuse_inode *fi = get_fuse_inode(inode);
694 spin_lock(&fi->lock);
695 fi->attr_version = atomic64_inc_return(&fc->attr_version);
696 spin_unlock(&fi->lock);
699 io->iocb->ki_complete(io->iocb, res);
702 kref_put(&io->refcnt, fuse_io_release);
705 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
708 struct fuse_io_args *ia;
710 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
713 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
723 static void fuse_io_free(struct fuse_io_args *ia)
729 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
732 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
733 struct fuse_io_priv *io = ia->io;
736 fuse_release_user_pages(&ia->ap, io->should_dirty);
740 } else if (io->write) {
741 if (ia->write.out.size > ia->write.in.size) {
743 } else if (ia->write.in.size != ia->write.out.size) {
744 pos = ia->write.in.offset - io->offset +
748 u32 outsize = args->out_args[0].size;
750 if (ia->read.in.size != outsize)
751 pos = ia->read.in.offset - io->offset + outsize;
754 fuse_aio_complete(io, err, pos);
758 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
759 struct fuse_io_args *ia, size_t num_bytes)
762 struct fuse_io_priv *io = ia->io;
764 spin_lock(&io->lock);
765 kref_get(&io->refcnt);
766 io->size += num_bytes;
768 spin_unlock(&io->lock);
770 ia->ap.args.end = fuse_aio_complete_req;
771 ia->ap.args.may_block = io->should_dirty;
772 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
774 fuse_aio_complete_req(fm, &ia->ap.args, err);
779 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
782 struct file *file = ia->io->iocb->ki_filp;
783 struct fuse_file *ff = file->private_data;
784 struct fuse_mount *fm = ff->fm;
786 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
788 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
789 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
793 return fuse_async_req_send(fm, ia, count);
795 return fuse_simple_request(fm, &ia->ap.args);
798 static void fuse_read_update_size(struct inode *inode, loff_t size,
801 struct fuse_conn *fc = get_fuse_conn(inode);
802 struct fuse_inode *fi = get_fuse_inode(inode);
804 spin_lock(&fi->lock);
805 if (attr_ver >= fi->attr_version && size < inode->i_size &&
806 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
807 fi->attr_version = atomic64_inc_return(&fc->attr_version);
808 i_size_write(inode, size);
810 spin_unlock(&fi->lock);
813 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
814 struct fuse_args_pages *ap)
816 struct fuse_conn *fc = get_fuse_conn(inode);
819 * If writeback_cache is enabled, a short read means there's a hole in
820 * the file. Some data after the hole is in page cache, but has not
821 * reached the client fs yet. So the hole is not present there.
823 if (!fc->writeback_cache) {
824 loff_t pos = page_offset(ap->pages[0]) + num_read;
825 fuse_read_update_size(inode, pos, attr_ver);
829 static int fuse_do_readpage(struct file *file, struct page *page)
831 struct inode *inode = page->mapping->host;
832 struct fuse_mount *fm = get_fuse_mount(inode);
833 loff_t pos = page_offset(page);
834 struct fuse_page_desc desc = { .length = PAGE_SIZE };
835 struct fuse_io_args ia = {
836 .ap.args.page_zeroing = true,
837 .ap.args.out_pages = true,
846 * Page writeback can extend beyond the lifetime of the
847 * page-cache page, so make sure we read a properly synced
850 fuse_wait_on_page_writeback(inode, page->index);
852 attr_ver = fuse_get_attr_version(fm->fc);
854 /* Don't overflow end offset */
855 if (pos + (desc.length - 1) == LLONG_MAX)
858 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
859 res = fuse_simple_request(fm, &ia.ap.args);
863 * Short read means EOF. If file size is larger, truncate it
865 if (res < desc.length)
866 fuse_short_read(inode, attr_ver, res, &ia.ap);
868 SetPageUptodate(page);
873 static int fuse_read_folio(struct file *file, struct folio *folio)
875 struct page *page = &folio->page;
876 struct inode *inode = page->mapping->host;
880 if (fuse_is_bad(inode))
883 err = fuse_do_readpage(file, page);
884 fuse_invalidate_atime(inode);
890 static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
894 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
895 struct fuse_args_pages *ap = &ia->ap;
896 size_t count = ia->read.in.size;
897 size_t num_read = args->out_args[0].size;
898 struct address_space *mapping = NULL;
900 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
901 mapping = ap->pages[i]->mapping;
904 struct inode *inode = mapping->host;
907 * Short read means EOF. If file size is larger, truncate it
909 if (!err && num_read < count)
910 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
912 fuse_invalidate_atime(inode);
915 for (i = 0; i < ap->num_pages; i++) {
916 struct page *page = ap->pages[i];
919 SetPageUptodate(page);
926 fuse_file_put(ia->ff, false, false);
931 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
933 struct fuse_file *ff = file->private_data;
934 struct fuse_mount *fm = ff->fm;
935 struct fuse_args_pages *ap = &ia->ap;
936 loff_t pos = page_offset(ap->pages[0]);
937 size_t count = ap->num_pages << PAGE_SHIFT;
941 ap->args.out_pages = true;
942 ap->args.page_zeroing = true;
943 ap->args.page_replace = true;
945 /* Don't overflow end offset */
946 if (pos + (count - 1) == LLONG_MAX) {
948 ap->descs[ap->num_pages - 1].length--;
950 WARN_ON((loff_t) (pos + count) < 0);
952 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
953 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
954 if (fm->fc->async_read) {
955 ia->ff = fuse_file_get(ff);
956 ap->args.end = fuse_readpages_end;
957 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
961 res = fuse_simple_request(fm, &ap->args);
962 err = res < 0 ? res : 0;
964 fuse_readpages_end(fm, &ap->args, err);
967 static void fuse_readahead(struct readahead_control *rac)
969 struct inode *inode = rac->mapping->host;
970 struct fuse_conn *fc = get_fuse_conn(inode);
971 unsigned int i, max_pages, nr_pages = 0;
973 if (fuse_is_bad(inode))
976 max_pages = min_t(unsigned int, fc->max_pages,
977 fc->max_read / PAGE_SIZE);
980 struct fuse_io_args *ia;
981 struct fuse_args_pages *ap;
983 if (fc->num_background >= fc->congestion_threshold &&
984 rac->ra->async_size >= readahead_count(rac))
986 * Congested and only async pages left, so skip the
991 nr_pages = readahead_count(rac) - nr_pages;
992 if (nr_pages > max_pages)
993 nr_pages = max_pages;
996 ia = fuse_io_alloc(NULL, nr_pages);
1000 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
1001 for (i = 0; i < nr_pages; i++) {
1002 fuse_wait_on_page_writeback(inode,
1003 readahead_index(rac) + i);
1004 ap->descs[i].length = PAGE_SIZE;
1006 ap->num_pages = nr_pages;
1007 fuse_send_readpages(ia, rac->file);
1011 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
1013 struct inode *inode = iocb->ki_filp->f_mapping->host;
1014 struct fuse_conn *fc = get_fuse_conn(inode);
1017 * In auto invalidate mode, always update attributes on read.
1018 * Otherwise, only update if we attempt to read past EOF (to ensure
1019 * i_size is up to date).
1021 if (fc->auto_inval_data ||
1022 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1024 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE);
1029 return generic_file_read_iter(iocb, to);
1032 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1033 loff_t pos, size_t count)
1035 struct fuse_args *args = &ia->ap.args;
1037 ia->write.in.fh = ff->fh;
1038 ia->write.in.offset = pos;
1039 ia->write.in.size = count;
1040 args->opcode = FUSE_WRITE;
1041 args->nodeid = ff->nodeid;
1042 args->in_numargs = 2;
1043 if (ff->fm->fc->minor < 9)
1044 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1046 args->in_args[0].size = sizeof(ia->write.in);
1047 args->in_args[0].value = &ia->write.in;
1048 args->in_args[1].size = count;
1049 args->out_numargs = 1;
1050 args->out_args[0].size = sizeof(ia->write.out);
1051 args->out_args[0].value = &ia->write.out;
1054 static unsigned int fuse_write_flags(struct kiocb *iocb)
1056 unsigned int flags = iocb->ki_filp->f_flags;
1058 if (iocb_is_dsync(iocb))
1060 if (iocb->ki_flags & IOCB_SYNC)
1066 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1067 size_t count, fl_owner_t owner)
1069 struct kiocb *iocb = ia->io->iocb;
1070 struct file *file = iocb->ki_filp;
1071 struct fuse_file *ff = file->private_data;
1072 struct fuse_mount *fm = ff->fm;
1073 struct fuse_write_in *inarg = &ia->write.in;
1076 fuse_write_args_fill(ia, ff, pos, count);
1077 inarg->flags = fuse_write_flags(iocb);
1078 if (owner != NULL) {
1079 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1080 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
1084 return fuse_async_req_send(fm, ia, count);
1086 err = fuse_simple_request(fm, &ia->ap.args);
1087 if (!err && ia->write.out.size > count)
1090 return err ?: ia->write.out.size;
1093 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)
1095 struct fuse_conn *fc = get_fuse_conn(inode);
1096 struct fuse_inode *fi = get_fuse_inode(inode);
1099 spin_lock(&fi->lock);
1100 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1101 if (written > 0 && pos > inode->i_size) {
1102 i_size_write(inode, pos);
1105 spin_unlock(&fi->lock);
1107 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
1112 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1113 struct kiocb *iocb, struct inode *inode,
1114 loff_t pos, size_t count)
1116 struct fuse_args_pages *ap = &ia->ap;
1117 struct file *file = iocb->ki_filp;
1118 struct fuse_file *ff = file->private_data;
1119 struct fuse_mount *fm = ff->fm;
1120 unsigned int offset, i;
1124 for (i = 0; i < ap->num_pages; i++)
1125 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1127 fuse_write_args_fill(ia, ff, pos, count);
1128 ia->write.in.flags = fuse_write_flags(iocb);
1129 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1130 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1132 err = fuse_simple_request(fm, &ap->args);
1133 if (!err && ia->write.out.size > count)
1136 short_write = ia->write.out.size < count;
1137 offset = ap->descs[0].offset;
1138 count = ia->write.out.size;
1139 for (i = 0; i < ap->num_pages; i++) {
1140 struct page *page = ap->pages[i];
1143 ClearPageUptodate(page);
1145 if (count >= PAGE_SIZE - offset)
1146 count -= PAGE_SIZE - offset;
1149 ClearPageUptodate(page);
1154 if (ia->write.page_locked && (i == ap->num_pages - 1))
1162 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
1163 struct address_space *mapping,
1164 struct iov_iter *ii, loff_t pos,
1165 unsigned int max_pages)
1167 struct fuse_args_pages *ap = &ia->ap;
1168 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1169 unsigned offset = pos & (PAGE_SIZE - 1);
1173 ap->args.in_pages = true;
1174 ap->descs[0].offset = offset;
1179 pgoff_t index = pos >> PAGE_SHIFT;
1180 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1181 iov_iter_count(ii));
1183 bytes = min_t(size_t, bytes, fc->max_write - count);
1187 if (fault_in_iov_iter_readable(ii, bytes))
1191 page = grab_cache_page_write_begin(mapping, index);
1195 if (mapping_writably_mapped(mapping))
1196 flush_dcache_page(page);
1198 tmp = copy_page_from_iter_atomic(page, offset, bytes, ii);
1199 flush_dcache_page(page);
1208 ap->pages[ap->num_pages] = page;
1209 ap->descs[ap->num_pages].length = tmp;
1215 if (offset == PAGE_SIZE)
1218 /* If we copied full page, mark it uptodate */
1219 if (tmp == PAGE_SIZE)
1220 SetPageUptodate(page);
1222 if (PageUptodate(page)) {
1225 ia->write.page_locked = true;
1228 if (!fc->big_writes)
1230 } while (iov_iter_count(ii) && count < fc->max_write &&
1231 ap->num_pages < max_pages && offset == 0);
1233 return count > 0 ? count : err;
1236 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1237 unsigned int max_pages)
1239 return min_t(unsigned int,
1240 ((pos + len - 1) >> PAGE_SHIFT) -
1241 (pos >> PAGE_SHIFT) + 1,
1245 static ssize_t fuse_perform_write(struct kiocb *iocb,
1246 struct address_space *mapping,
1247 struct iov_iter *ii, loff_t pos)
1249 struct inode *inode = mapping->host;
1250 struct fuse_conn *fc = get_fuse_conn(inode);
1251 struct fuse_inode *fi = get_fuse_inode(inode);
1255 if (inode->i_size < pos + iov_iter_count(ii))
1256 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1260 struct fuse_io_args ia = {};
1261 struct fuse_args_pages *ap = &ia.ap;
1262 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1265 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1271 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
1275 err = fuse_send_write_pages(&ia, iocb, inode,
1278 size_t num_written = ia.write.out.size;
1283 /* break out of the loop on short write */
1284 if (num_written != count)
1289 } while (!err && iov_iter_count(ii));
1291 fuse_write_update_attr(inode, pos, res);
1292 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1294 return res > 0 ? res : err;
1297 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1299 struct file *file = iocb->ki_filp;
1300 struct address_space *mapping = file->f_mapping;
1301 ssize_t written = 0;
1302 ssize_t written_buffered = 0;
1303 struct inode *inode = mapping->host;
1305 struct fuse_conn *fc = get_fuse_conn(inode);
1308 if (fc->writeback_cache) {
1309 /* Update size (EOF optimization) and mode (SUID clearing) */
1310 err = fuse_update_attributes(mapping->host, file,
1311 STATX_SIZE | STATX_MODE);
1315 if (fc->handle_killpriv_v2 &&
1316 should_remove_suid(file_dentry(file))) {
1320 return generic_file_write_iter(iocb, from);
1326 /* We can write back this queue in page reclaim */
1327 current->backing_dev_info = inode_to_bdi(inode);
1329 err = generic_write_checks(iocb, from);
1333 err = file_remove_privs(file);
1337 err = file_update_time(file);
1341 if (iocb->ki_flags & IOCB_DIRECT) {
1342 loff_t pos = iocb->ki_pos;
1343 written = generic_file_direct_write(iocb, from);
1344 if (written < 0 || !iov_iter_count(from))
1349 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1350 if (written_buffered < 0) {
1351 err = written_buffered;
1354 endbyte = pos + written_buffered - 1;
1356 err = filemap_write_and_wait_range(file->f_mapping, pos,
1361 invalidate_mapping_pages(file->f_mapping,
1363 endbyte >> PAGE_SHIFT);
1365 written += written_buffered;
1366 iocb->ki_pos = pos + written_buffered;
1368 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1370 iocb->ki_pos += written;
1373 current->backing_dev_info = NULL;
1374 inode_unlock(inode);
1376 written = generic_write_sync(iocb, written);
1378 return written ? written : err;
1381 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1383 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1386 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1389 return min(iov_iter_single_seg_count(ii), max_size);
1392 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1393 size_t *nbytesp, int write,
1394 unsigned int max_pages)
1396 size_t nbytes = 0; /* # bytes already packed in req */
1399 /* Special case for kernel I/O: can copy directly into the buffer */
1400 if (iov_iter_is_kvec(ii)) {
1401 unsigned long user_addr = fuse_get_user_addr(ii);
1402 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1405 ap->args.in_args[1].value = (void *) user_addr;
1407 ap->args.out_args[0].value = (void *) user_addr;
1409 iov_iter_advance(ii, frag_size);
1410 *nbytesp = frag_size;
1414 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1417 ret = iov_iter_get_pages2(ii, &ap->pages[ap->num_pages],
1419 max_pages - ap->num_pages,
1427 npages = DIV_ROUND_UP(ret, PAGE_SIZE);
1429 ap->descs[ap->num_pages].offset = start;
1430 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1432 ap->num_pages += npages;
1433 ap->descs[ap->num_pages - 1].length -=
1434 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1437 ap->args.user_pages = true;
1439 ap->args.in_pages = true;
1441 ap->args.out_pages = true;
1445 return ret < 0 ? ret : 0;
1448 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1449 loff_t *ppos, int flags)
1451 int write = flags & FUSE_DIO_WRITE;
1452 int cuse = flags & FUSE_DIO_CUSE;
1453 struct file *file = io->iocb->ki_filp;
1454 struct inode *inode = file->f_mapping->host;
1455 struct fuse_file *ff = file->private_data;
1456 struct fuse_conn *fc = ff->fm->fc;
1457 size_t nmax = write ? fc->max_write : fc->max_read;
1459 size_t count = iov_iter_count(iter);
1460 pgoff_t idx_from = pos >> PAGE_SHIFT;
1461 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1464 struct fuse_io_args *ia;
1465 unsigned int max_pages;
1467 max_pages = iov_iter_npages(iter, fc->max_pages);
1468 ia = fuse_io_alloc(io, max_pages);
1472 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1475 fuse_sync_writes(inode);
1477 inode_unlock(inode);
1480 io->should_dirty = !write && user_backed_iter(iter);
1483 fl_owner_t owner = current->files;
1484 size_t nbytes = min(count, nmax);
1486 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1492 if (!capable(CAP_FSETID))
1493 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1495 nres = fuse_send_write(ia, pos, nbytes, owner);
1497 nres = fuse_send_read(ia, pos, nbytes, owner);
1500 if (!io->async || nres < 0) {
1501 fuse_release_user_pages(&ia->ap, io->should_dirty);
1506 iov_iter_revert(iter, nbytes);
1510 WARN_ON(nres > nbytes);
1515 if (nres != nbytes) {
1516 iov_iter_revert(iter, nbytes - nres);
1520 max_pages = iov_iter_npages(iter, fc->max_pages);
1521 ia = fuse_io_alloc(io, max_pages);
1531 return res > 0 ? res : err;
1533 EXPORT_SYMBOL_GPL(fuse_direct_io);
1535 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1536 struct iov_iter *iter,
1540 struct inode *inode = file_inode(io->iocb->ki_filp);
1542 res = fuse_direct_io(io, iter, ppos, 0);
1544 fuse_invalidate_atime(inode);
1549 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1551 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1555 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1556 res = fuse_direct_IO(iocb, to);
1558 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1560 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1566 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1568 struct inode *inode = file_inode(iocb->ki_filp);
1569 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1572 /* Don't allow parallel writes to the same file */
1574 res = generic_write_checks(iocb, from);
1576 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1577 res = fuse_direct_IO(iocb, from);
1579 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1581 fuse_write_update_attr(inode, iocb->ki_pos, res);
1584 inode_unlock(inode);
1589 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1591 struct file *file = iocb->ki_filp;
1592 struct fuse_file *ff = file->private_data;
1593 struct inode *inode = file_inode(file);
1595 if (fuse_is_bad(inode))
1598 if (FUSE_IS_DAX(inode))
1599 return fuse_dax_read_iter(iocb, to);
1601 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1602 return fuse_cache_read_iter(iocb, to);
1604 return fuse_direct_read_iter(iocb, to);
1607 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1609 struct file *file = iocb->ki_filp;
1610 struct fuse_file *ff = file->private_data;
1611 struct inode *inode = file_inode(file);
1613 if (fuse_is_bad(inode))
1616 if (FUSE_IS_DAX(inode))
1617 return fuse_dax_write_iter(iocb, from);
1619 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1620 return fuse_cache_write_iter(iocb, from);
1622 return fuse_direct_write_iter(iocb, from);
1625 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1627 struct fuse_args_pages *ap = &wpa->ia.ap;
1631 fuse_sync_bucket_dec(wpa->bucket);
1633 for (i = 0; i < ap->num_pages; i++)
1634 __free_page(ap->pages[i]);
1637 fuse_file_put(wpa->ia.ff, false, false);
1643 static void fuse_writepage_finish(struct fuse_mount *fm,
1644 struct fuse_writepage_args *wpa)
1646 struct fuse_args_pages *ap = &wpa->ia.ap;
1647 struct inode *inode = wpa->inode;
1648 struct fuse_inode *fi = get_fuse_inode(inode);
1649 struct backing_dev_info *bdi = inode_to_bdi(inode);
1652 for (i = 0; i < ap->num_pages; i++) {
1653 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1654 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1655 wb_writeout_inc(&bdi->wb);
1657 wake_up(&fi->page_waitq);
1660 /* Called under fi->lock, may release and reacquire it */
1661 static void fuse_send_writepage(struct fuse_mount *fm,
1662 struct fuse_writepage_args *wpa, loff_t size)
1663 __releases(fi->lock)
1664 __acquires(fi->lock)
1666 struct fuse_writepage_args *aux, *next;
1667 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1668 struct fuse_write_in *inarg = &wpa->ia.write.in;
1669 struct fuse_args *args = &wpa->ia.ap.args;
1670 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1674 if (inarg->offset + data_size <= size) {
1675 inarg->size = data_size;
1676 } else if (inarg->offset < size) {
1677 inarg->size = size - inarg->offset;
1679 /* Got truncated off completely */
1683 args->in_args[1].size = inarg->size;
1685 args->nocreds = true;
1687 err = fuse_simple_background(fm, args, GFP_ATOMIC);
1688 if (err == -ENOMEM) {
1689 spin_unlock(&fi->lock);
1690 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
1691 spin_lock(&fi->lock);
1694 /* Fails on broken connection only */
1702 rb_erase(&wpa->writepages_entry, &fi->writepages);
1703 fuse_writepage_finish(fm, wpa);
1704 spin_unlock(&fi->lock);
1706 /* After fuse_writepage_finish() aux request list is private */
1707 for (aux = wpa->next; aux; aux = next) {
1710 fuse_writepage_free(aux);
1713 fuse_writepage_free(wpa);
1714 spin_lock(&fi->lock);
1718 * If fi->writectr is positive (no truncate or fsync going on) send
1719 * all queued writepage requests.
1721 * Called with fi->lock
1723 void fuse_flush_writepages(struct inode *inode)
1724 __releases(fi->lock)
1725 __acquires(fi->lock)
1727 struct fuse_mount *fm = get_fuse_mount(inode);
1728 struct fuse_inode *fi = get_fuse_inode(inode);
1729 loff_t crop = i_size_read(inode);
1730 struct fuse_writepage_args *wpa;
1732 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1733 wpa = list_entry(fi->queued_writes.next,
1734 struct fuse_writepage_args, queue_entry);
1735 list_del_init(&wpa->queue_entry);
1736 fuse_send_writepage(fm, wpa, crop);
1740 static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1741 struct fuse_writepage_args *wpa)
1743 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1744 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1745 struct rb_node **p = &root->rb_node;
1746 struct rb_node *parent = NULL;
1748 WARN_ON(!wpa->ia.ap.num_pages);
1750 struct fuse_writepage_args *curr;
1754 curr = rb_entry(parent, struct fuse_writepage_args,
1756 WARN_ON(curr->inode != wpa->inode);
1757 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1759 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1760 p = &(*p)->rb_right;
1761 else if (idx_to < curr_index)
1767 rb_link_node(&wpa->writepages_entry, parent, p);
1768 rb_insert_color(&wpa->writepages_entry, root);
1772 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1774 WARN_ON(fuse_insert_writeback(root, wpa));
1777 static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
1780 struct fuse_writepage_args *wpa =
1781 container_of(args, typeof(*wpa), ia.ap.args);
1782 struct inode *inode = wpa->inode;
1783 struct fuse_inode *fi = get_fuse_inode(inode);
1784 struct fuse_conn *fc = get_fuse_conn(inode);
1786 mapping_set_error(inode->i_mapping, error);
1788 * A writeback finished and this might have updated mtime/ctime on
1789 * server making local mtime/ctime stale. Hence invalidate attrs.
1790 * Do this only if writeback_cache is not enabled. If writeback_cache
1791 * is enabled, we trust local ctime/mtime.
1793 if (!fc->writeback_cache)
1794 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
1795 spin_lock(&fi->lock);
1796 rb_erase(&wpa->writepages_entry, &fi->writepages);
1798 struct fuse_mount *fm = get_fuse_mount(inode);
1799 struct fuse_write_in *inarg = &wpa->ia.write.in;
1800 struct fuse_writepage_args *next = wpa->next;
1802 wpa->next = next->next;
1804 next->ia.ff = fuse_file_get(wpa->ia.ff);
1805 tree_insert(&fi->writepages, next);
1808 * Skip fuse_flush_writepages() to make it easy to crop requests
1809 * based on primary request size.
1811 * 1st case (trivial): there are no concurrent activities using
1812 * fuse_set/release_nowrite. Then we're on safe side because
1813 * fuse_flush_writepages() would call fuse_send_writepage()
1816 * 2nd case: someone called fuse_set_nowrite and it is waiting
1817 * now for completion of all in-flight requests. This happens
1818 * rarely and no more than once per page, so this should be
1821 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1822 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1823 * that fuse_set_nowrite returned implies that all in-flight
1824 * requests were completed along with all of their secondary
1825 * requests. Further primary requests are blocked by negative
1826 * writectr. Hence there cannot be any in-flight requests and
1827 * no invocations of fuse_writepage_end() while we're in
1828 * fuse_set_nowrite..fuse_release_nowrite section.
1830 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
1833 fuse_writepage_finish(fm, wpa);
1834 spin_unlock(&fi->lock);
1835 fuse_writepage_free(wpa);
1838 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
1840 struct fuse_file *ff;
1842 spin_lock(&fi->lock);
1843 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file,
1847 spin_unlock(&fi->lock);
1852 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
1854 struct fuse_file *ff = __fuse_write_file_get(fi);
1859 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1861 struct fuse_inode *fi = get_fuse_inode(inode);
1862 struct fuse_file *ff;
1866 * Inode is always written before the last reference is dropped and
1867 * hence this should not be reached from reclaim.
1869 * Writing back the inode from reclaim can deadlock if the request
1870 * processing itself needs an allocation. Allocations triggering
1871 * reclaim while serving a request can't be prevented, because it can
1872 * involve any number of unrelated userspace processes.
1874 WARN_ON(wbc->for_reclaim);
1876 ff = __fuse_write_file_get(fi);
1877 err = fuse_flush_times(inode, ff);
1879 fuse_file_put(ff, false, false);
1884 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1886 struct fuse_writepage_args *wpa;
1887 struct fuse_args_pages *ap;
1889 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1893 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1903 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
1904 struct fuse_writepage_args *wpa)
1910 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
1912 wpa->bucket = rcu_dereference(fc->curr_bucket);
1913 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
1917 static int fuse_writepage_locked(struct page *page)
1919 struct address_space *mapping = page->mapping;
1920 struct inode *inode = mapping->host;
1921 struct fuse_conn *fc = get_fuse_conn(inode);
1922 struct fuse_inode *fi = get_fuse_inode(inode);
1923 struct fuse_writepage_args *wpa;
1924 struct fuse_args_pages *ap;
1925 struct page *tmp_page;
1926 int error = -ENOMEM;
1928 set_page_writeback(page);
1930 wpa = fuse_writepage_args_alloc();
1935 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1940 wpa->ia.ff = fuse_write_file_get(fi);
1944 fuse_writepage_add_to_bucket(fc, wpa);
1945 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1947 copy_highpage(tmp_page, page);
1948 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1950 ap->args.in_pages = true;
1952 ap->pages[0] = tmp_page;
1953 ap->descs[0].offset = 0;
1954 ap->descs[0].length = PAGE_SIZE;
1955 ap->args.end = fuse_writepage_end;
1958 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1959 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1961 spin_lock(&fi->lock);
1962 tree_insert(&fi->writepages, wpa);
1963 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1964 fuse_flush_writepages(inode);
1965 spin_unlock(&fi->lock);
1967 end_page_writeback(page);
1972 __free_page(tmp_page);
1976 mapping_set_error(page->mapping, error);
1977 end_page_writeback(page);
1981 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1983 struct fuse_conn *fc = get_fuse_conn(page->mapping->host);
1986 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1988 * ->writepages() should be called for sync() and friends. We
1989 * should only get here on direct reclaim and then we are
1990 * allowed to skip a page which is already in flight
1992 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1994 redirty_page_for_writepage(wbc, page);
1999 if (wbc->sync_mode == WB_SYNC_NONE &&
2000 fc->num_background >= fc->congestion_threshold)
2001 return AOP_WRITEPAGE_ACTIVATE;
2003 err = fuse_writepage_locked(page);
2009 struct fuse_fill_wb_data {
2010 struct fuse_writepage_args *wpa;
2011 struct fuse_file *ff;
2012 struct inode *inode;
2013 struct page **orig_pages;
2014 unsigned int max_pages;
2017 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
2019 struct fuse_args_pages *ap = &data->wpa->ia.ap;
2020 struct fuse_conn *fc = get_fuse_conn(data->inode);
2021 struct page **pages;
2022 struct fuse_page_desc *descs;
2023 unsigned int npages = min_t(unsigned int,
2024 max_t(unsigned int, data->max_pages * 2,
2025 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
2027 WARN_ON(npages <= data->max_pages);
2029 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
2033 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
2034 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
2038 data->max_pages = npages;
2043 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2045 struct fuse_writepage_args *wpa = data->wpa;
2046 struct inode *inode = data->inode;
2047 struct fuse_inode *fi = get_fuse_inode(inode);
2048 int num_pages = wpa->ia.ap.num_pages;
2051 wpa->ia.ff = fuse_file_get(data->ff);
2052 spin_lock(&fi->lock);
2053 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2054 fuse_flush_writepages(inode);
2055 spin_unlock(&fi->lock);
2057 for (i = 0; i < num_pages; i++)
2058 end_page_writeback(data->orig_pages[i]);
2062 * Check under fi->lock if the page is under writeback, and insert it onto the
2063 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2064 * one already added for a page at this offset. If there's none, then insert
2065 * this new request onto the auxiliary list, otherwise reuse the existing one by
2066 * swapping the new temp page with the old one.
2068 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2071 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2072 struct fuse_writepage_args *tmp;
2073 struct fuse_writepage_args *old_wpa;
2074 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
2076 WARN_ON(new_ap->num_pages != 0);
2077 new_ap->num_pages = 1;
2079 spin_lock(&fi->lock);
2080 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
2082 spin_unlock(&fi->lock);
2086 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2089 WARN_ON(tmp->inode != new_wpa->inode);
2090 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2091 if (curr_index == page->index) {
2092 WARN_ON(tmp->ia.ap.num_pages != 1);
2093 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2099 new_wpa->next = old_wpa->next;
2100 old_wpa->next = new_wpa;
2103 spin_unlock(&fi->lock);
2106 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2108 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2109 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2110 wb_writeout_inc(&bdi->wb);
2111 fuse_writepage_free(new_wpa);
2117 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2118 struct fuse_args_pages *ap,
2119 struct fuse_fill_wb_data *data)
2121 WARN_ON(!ap->num_pages);
2124 * Being under writeback is unlikely but possible. For example direct
2125 * read to an mmaped fuse file will set the page dirty twice; once when
2126 * the pages are faulted with get_user_pages(), and then after the read
2129 if (fuse_page_is_writeback(data->inode, page->index))
2132 /* Reached max pages */
2133 if (ap->num_pages == fc->max_pages)
2136 /* Reached max write bytes */
2137 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2141 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2144 /* Need to grow the pages array? If so, did the expansion fail? */
2145 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2151 static int fuse_writepages_fill(struct page *page,
2152 struct writeback_control *wbc, void *_data)
2154 struct fuse_fill_wb_data *data = _data;
2155 struct fuse_writepage_args *wpa = data->wpa;
2156 struct fuse_args_pages *ap = &wpa->ia.ap;
2157 struct inode *inode = data->inode;
2158 struct fuse_inode *fi = get_fuse_inode(inode);
2159 struct fuse_conn *fc = get_fuse_conn(inode);
2160 struct page *tmp_page;
2165 data->ff = fuse_write_file_get(fi);
2170 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
2171 fuse_writepages_send(data);
2176 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2181 * The page must not be redirtied until the writeout is completed
2182 * (i.e. userspace has sent a reply to the write request). Otherwise
2183 * there could be more than one temporary page instance for each real
2186 * This is ensured by holding the page lock in page_mkwrite() while
2187 * checking fuse_page_is_writeback(). We already hold the page lock
2188 * since clear_page_dirty_for_io() and keep it held until we add the
2189 * request to the fi->writepages list and increment ap->num_pages.
2190 * After this fuse_page_is_writeback() will indicate that the page is
2191 * under writeback, so we can release the page lock.
2193 if (data->wpa == NULL) {
2195 wpa = fuse_writepage_args_alloc();
2197 __free_page(tmp_page);
2200 fuse_writepage_add_to_bucket(fc, wpa);
2202 data->max_pages = 1;
2205 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2206 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2208 ap->args.in_pages = true;
2209 ap->args.end = fuse_writepage_end;
2213 set_page_writeback(page);
2215 copy_highpage(tmp_page, page);
2216 ap->pages[ap->num_pages] = tmp_page;
2217 ap->descs[ap->num_pages].offset = 0;
2218 ap->descs[ap->num_pages].length = PAGE_SIZE;
2219 data->orig_pages[ap->num_pages] = page;
2221 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2222 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2227 * Protected by fi->lock against concurrent access by
2228 * fuse_page_is_writeback().
2230 spin_lock(&fi->lock);
2232 spin_unlock(&fi->lock);
2233 } else if (fuse_writepage_add(wpa, page)) {
2236 end_page_writeback(page);
2244 static int fuse_writepages(struct address_space *mapping,
2245 struct writeback_control *wbc)
2247 struct inode *inode = mapping->host;
2248 struct fuse_conn *fc = get_fuse_conn(inode);
2249 struct fuse_fill_wb_data data;
2253 if (fuse_is_bad(inode))
2256 if (wbc->sync_mode == WB_SYNC_NONE &&
2257 fc->num_background >= fc->congestion_threshold)
2265 data.orig_pages = kcalloc(fc->max_pages,
2266 sizeof(struct page *),
2268 if (!data.orig_pages)
2271 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2273 WARN_ON(!data.wpa->ia.ap.num_pages);
2274 fuse_writepages_send(&data);
2277 fuse_file_put(data.ff, false, false);
2279 kfree(data.orig_pages);
2285 * It's worthy to make sure that space is reserved on disk for the write,
2286 * but how to implement it without killing performance need more thinking.
2288 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2289 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
2291 pgoff_t index = pos >> PAGE_SHIFT;
2292 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2297 WARN_ON(!fc->writeback_cache);
2299 page = grab_cache_page_write_begin(mapping, index);
2303 fuse_wait_on_page_writeback(mapping->host, page->index);
2305 if (PageUptodate(page) || len == PAGE_SIZE)
2308 * Check if the start this page comes after the end of file, in which
2309 * case the readpage can be optimized away.
2311 fsize = i_size_read(mapping->host);
2312 if (fsize <= (pos & PAGE_MASK)) {
2313 size_t off = pos & ~PAGE_MASK;
2315 zero_user_segment(page, 0, off);
2318 err = fuse_do_readpage(file, page);
2332 static int fuse_write_end(struct file *file, struct address_space *mapping,
2333 loff_t pos, unsigned len, unsigned copied,
2334 struct page *page, void *fsdata)
2336 struct inode *inode = page->mapping->host;
2338 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2343 if (!PageUptodate(page)) {
2344 /* Zero any unwritten bytes at the end of the page */
2345 size_t endoff = pos & ~PAGE_MASK;
2347 zero_user_segment(page, endoff, PAGE_SIZE);
2348 SetPageUptodate(page);
2351 if (pos > inode->i_size)
2352 i_size_write(inode, pos);
2354 set_page_dirty(page);
2363 static int fuse_launder_folio(struct folio *folio)
2366 if (folio_clear_dirty_for_io(folio)) {
2367 struct inode *inode = folio->mapping->host;
2369 /* Serialize with pending writeback for the same page */
2370 fuse_wait_on_page_writeback(inode, folio->index);
2371 err = fuse_writepage_locked(&folio->page);
2373 fuse_wait_on_page_writeback(inode, folio->index);
2379 * Write back dirty data/metadata now (there may not be any suitable
2380 * open files later for data)
2382 static void fuse_vma_close(struct vm_area_struct *vma)
2386 err = write_inode_now(vma->vm_file->f_mapping->host, 1);
2387 mapping_set_error(vma->vm_file->f_mapping, err);
2391 * Wait for writeback against this page to complete before allowing it
2392 * to be marked dirty again, and hence written back again, possibly
2393 * before the previous writepage completed.
2395 * Block here, instead of in ->writepage(), so that the userspace fs
2396 * can only block processes actually operating on the filesystem.
2398 * Otherwise unprivileged userspace fs would be able to block
2403 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2405 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2407 struct page *page = vmf->page;
2408 struct inode *inode = file_inode(vmf->vma->vm_file);
2410 file_update_time(vmf->vma->vm_file);
2412 if (page->mapping != inode->i_mapping) {
2414 return VM_FAULT_NOPAGE;
2417 fuse_wait_on_page_writeback(inode, page->index);
2418 return VM_FAULT_LOCKED;
2421 static const struct vm_operations_struct fuse_file_vm_ops = {
2422 .close = fuse_vma_close,
2423 .fault = filemap_fault,
2424 .map_pages = filemap_map_pages,
2425 .page_mkwrite = fuse_page_mkwrite,
2428 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2430 struct fuse_file *ff = file->private_data;
2432 /* DAX mmap is superior to direct_io mmap */
2433 if (FUSE_IS_DAX(file_inode(file)))
2434 return fuse_dax_mmap(file, vma);
2436 if (ff->open_flags & FOPEN_DIRECT_IO) {
2437 /* Can't provide the coherency needed for MAP_SHARED */
2438 if (vma->vm_flags & VM_MAYSHARE)
2441 invalidate_inode_pages2(file->f_mapping);
2443 return generic_file_mmap(file, vma);
2446 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2447 fuse_link_write_file(file);
2449 file_accessed(file);
2450 vma->vm_ops = &fuse_file_vm_ops;
2454 static int convert_fuse_file_lock(struct fuse_conn *fc,
2455 const struct fuse_file_lock *ffl,
2456 struct file_lock *fl)
2458 switch (ffl->type) {
2464 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2465 ffl->end < ffl->start)
2468 fl->fl_start = ffl->start;
2469 fl->fl_end = ffl->end;
2472 * Convert pid into init's pid namespace. The locks API will
2473 * translate it into the caller's pid namespace.
2476 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2483 fl->fl_type = ffl->type;
2487 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2488 const struct file_lock *fl, int opcode, pid_t pid,
2489 int flock, struct fuse_lk_in *inarg)
2491 struct inode *inode = file_inode(file);
2492 struct fuse_conn *fc = get_fuse_conn(inode);
2493 struct fuse_file *ff = file->private_data;
2495 memset(inarg, 0, sizeof(*inarg));
2497 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2498 inarg->lk.start = fl->fl_start;
2499 inarg->lk.end = fl->fl_end;
2500 inarg->lk.type = fl->fl_type;
2501 inarg->lk.pid = pid;
2503 inarg->lk_flags |= FUSE_LK_FLOCK;
2504 args->opcode = opcode;
2505 args->nodeid = get_node_id(inode);
2506 args->in_numargs = 1;
2507 args->in_args[0].size = sizeof(*inarg);
2508 args->in_args[0].value = inarg;
2511 static int fuse_getlk(struct file *file, struct file_lock *fl)
2513 struct inode *inode = file_inode(file);
2514 struct fuse_mount *fm = get_fuse_mount(inode);
2516 struct fuse_lk_in inarg;
2517 struct fuse_lk_out outarg;
2520 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2521 args.out_numargs = 1;
2522 args.out_args[0].size = sizeof(outarg);
2523 args.out_args[0].value = &outarg;
2524 err = fuse_simple_request(fm, &args);
2526 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2531 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2533 struct inode *inode = file_inode(file);
2534 struct fuse_mount *fm = get_fuse_mount(inode);
2536 struct fuse_lk_in inarg;
2537 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2538 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2539 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2542 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2543 /* NLM needs asynchronous locks, which we don't support yet */
2547 /* Unlock on close is handled by the flush method */
2548 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2551 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2552 err = fuse_simple_request(fm, &args);
2554 /* locking is restartable */
2561 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2563 struct inode *inode = file_inode(file);
2564 struct fuse_conn *fc = get_fuse_conn(inode);
2567 if (cmd == F_CANCELLK) {
2569 } else if (cmd == F_GETLK) {
2571 posix_test_lock(file, fl);
2574 err = fuse_getlk(file, fl);
2577 err = posix_lock_file(file, fl, NULL);
2579 err = fuse_setlk(file, fl, 0);
2584 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2586 struct inode *inode = file_inode(file);
2587 struct fuse_conn *fc = get_fuse_conn(inode);
2591 err = locks_lock_file_wait(file, fl);
2593 struct fuse_file *ff = file->private_data;
2595 /* emulate flock with POSIX locks */
2597 err = fuse_setlk(file, fl, 1);
2603 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2605 struct inode *inode = mapping->host;
2606 struct fuse_mount *fm = get_fuse_mount(inode);
2608 struct fuse_bmap_in inarg;
2609 struct fuse_bmap_out outarg;
2612 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2615 memset(&inarg, 0, sizeof(inarg));
2616 inarg.block = block;
2617 inarg.blocksize = inode->i_sb->s_blocksize;
2618 args.opcode = FUSE_BMAP;
2619 args.nodeid = get_node_id(inode);
2620 args.in_numargs = 1;
2621 args.in_args[0].size = sizeof(inarg);
2622 args.in_args[0].value = &inarg;
2623 args.out_numargs = 1;
2624 args.out_args[0].size = sizeof(outarg);
2625 args.out_args[0].value = &outarg;
2626 err = fuse_simple_request(fm, &args);
2628 fm->fc->no_bmap = 1;
2630 return err ? 0 : outarg.block;
2633 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2635 struct inode *inode = file->f_mapping->host;
2636 struct fuse_mount *fm = get_fuse_mount(inode);
2637 struct fuse_file *ff = file->private_data;
2639 struct fuse_lseek_in inarg = {
2644 struct fuse_lseek_out outarg;
2647 if (fm->fc->no_lseek)
2650 args.opcode = FUSE_LSEEK;
2651 args.nodeid = ff->nodeid;
2652 args.in_numargs = 1;
2653 args.in_args[0].size = sizeof(inarg);
2654 args.in_args[0].value = &inarg;
2655 args.out_numargs = 1;
2656 args.out_args[0].size = sizeof(outarg);
2657 args.out_args[0].value = &outarg;
2658 err = fuse_simple_request(fm, &args);
2660 if (err == -ENOSYS) {
2661 fm->fc->no_lseek = 1;
2667 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2670 err = fuse_update_attributes(inode, file, STATX_SIZE);
2672 return generic_file_llseek(file, offset, whence);
2677 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2680 struct inode *inode = file_inode(file);
2685 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2686 retval = generic_file_llseek(file, offset, whence);
2690 retval = fuse_update_attributes(inode, file, STATX_SIZE);
2692 retval = generic_file_llseek(file, offset, whence);
2693 inode_unlock(inode);
2698 retval = fuse_lseek(file, offset, whence);
2699 inode_unlock(inode);
2709 * All files which have been polled are linked to RB tree
2710 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2711 * find the matching one.
2713 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2714 struct rb_node **parent_out)
2716 struct rb_node **link = &fc->polled_files.rb_node;
2717 struct rb_node *last = NULL;
2720 struct fuse_file *ff;
2723 ff = rb_entry(last, struct fuse_file, polled_node);
2726 link = &last->rb_left;
2727 else if (kh > ff->kh)
2728 link = &last->rb_right;
2739 * The file is about to be polled. Make sure it's on the polled_files
2740 * RB tree. Note that files once added to the polled_files tree are
2741 * not removed before the file is released. This is because a file
2742 * polled once is likely to be polled again.
2744 static void fuse_register_polled_file(struct fuse_conn *fc,
2745 struct fuse_file *ff)
2747 spin_lock(&fc->lock);
2748 if (RB_EMPTY_NODE(&ff->polled_node)) {
2749 struct rb_node **link, *parent;
2751 link = fuse_find_polled_node(fc, ff->kh, &parent);
2753 rb_link_node(&ff->polled_node, parent, link);
2754 rb_insert_color(&ff->polled_node, &fc->polled_files);
2756 spin_unlock(&fc->lock);
2759 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2761 struct fuse_file *ff = file->private_data;
2762 struct fuse_mount *fm = ff->fm;
2763 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2764 struct fuse_poll_out outarg;
2768 if (fm->fc->no_poll)
2769 return DEFAULT_POLLMASK;
2771 poll_wait(file, &ff->poll_wait, wait);
2772 inarg.events = mangle_poll(poll_requested_events(wait));
2775 * Ask for notification iff there's someone waiting for it.
2776 * The client may ignore the flag and always notify.
2778 if (waitqueue_active(&ff->poll_wait)) {
2779 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2780 fuse_register_polled_file(fm->fc, ff);
2783 args.opcode = FUSE_POLL;
2784 args.nodeid = ff->nodeid;
2785 args.in_numargs = 1;
2786 args.in_args[0].size = sizeof(inarg);
2787 args.in_args[0].value = &inarg;
2788 args.out_numargs = 1;
2789 args.out_args[0].size = sizeof(outarg);
2790 args.out_args[0].value = &outarg;
2791 err = fuse_simple_request(fm, &args);
2794 return demangle_poll(outarg.revents);
2795 if (err == -ENOSYS) {
2796 fm->fc->no_poll = 1;
2797 return DEFAULT_POLLMASK;
2801 EXPORT_SYMBOL_GPL(fuse_file_poll);
2804 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2805 * wakes up the poll waiters.
2807 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2808 struct fuse_notify_poll_wakeup_out *outarg)
2810 u64 kh = outarg->kh;
2811 struct rb_node **link;
2813 spin_lock(&fc->lock);
2815 link = fuse_find_polled_node(fc, kh, NULL);
2817 struct fuse_file *ff;
2819 ff = rb_entry(*link, struct fuse_file, polled_node);
2820 wake_up_interruptible_sync(&ff->poll_wait);
2823 spin_unlock(&fc->lock);
2827 static void fuse_do_truncate(struct file *file)
2829 struct inode *inode = file->f_mapping->host;
2832 attr.ia_valid = ATTR_SIZE;
2833 attr.ia_size = i_size_read(inode);
2835 attr.ia_file = file;
2836 attr.ia_valid |= ATTR_FILE;
2838 fuse_do_setattr(file_dentry(file), &attr, file);
2841 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2843 return round_up(off, fc->max_pages << PAGE_SHIFT);
2847 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2849 DECLARE_COMPLETION_ONSTACK(wait);
2851 struct file *file = iocb->ki_filp;
2852 struct fuse_file *ff = file->private_data;
2854 struct inode *inode;
2856 size_t count = iov_iter_count(iter), shortened = 0;
2857 loff_t offset = iocb->ki_pos;
2858 struct fuse_io_priv *io;
2861 inode = file->f_mapping->host;
2862 i_size = i_size_read(inode);
2864 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
2867 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2870 spin_lock_init(&io->lock);
2871 kref_init(&io->refcnt);
2875 io->offset = offset;
2876 io->write = (iov_iter_rw(iter) == WRITE);
2879 * By default, we want to optimize all I/Os with async request
2880 * submission to the client filesystem if supported.
2882 io->async = ff->fm->fc->async_dio;
2884 io->blocking = is_sync_kiocb(iocb);
2886 /* optimization for short read */
2887 if (io->async && !io->write && offset + count > i_size) {
2888 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
2889 shortened = count - iov_iter_count(iter);
2894 * We cannot asynchronously extend the size of a file.
2895 * In such case the aio will behave exactly like sync io.
2897 if ((offset + count > i_size) && io->write)
2898 io->blocking = true;
2900 if (io->async && io->blocking) {
2902 * Additional reference to keep io around after
2903 * calling fuse_aio_complete()
2905 kref_get(&io->refcnt);
2909 if (iov_iter_rw(iter) == WRITE) {
2910 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2911 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
2913 ret = __fuse_direct_read(io, iter, &pos);
2915 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
2918 bool blocking = io->blocking;
2920 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2922 /* we have a non-extending, async request, so return */
2924 return -EIOCBQUEUED;
2926 wait_for_completion(&wait);
2927 ret = fuse_get_res_by_io(io);
2930 kref_put(&io->refcnt, fuse_io_release);
2932 if (iov_iter_rw(iter) == WRITE) {
2933 fuse_write_update_attr(inode, pos, ret);
2934 if (ret < 0 && offset + count > i_size)
2935 fuse_do_truncate(file);
2941 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
2943 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
2946 fuse_sync_writes(inode);
2951 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2954 struct fuse_file *ff = file->private_data;
2955 struct inode *inode = file_inode(file);
2956 struct fuse_inode *fi = get_fuse_inode(inode);
2957 struct fuse_mount *fm = ff->fm;
2959 struct fuse_fallocate_in inarg = {
2966 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2967 (mode & (FALLOC_FL_PUNCH_HOLE |
2968 FALLOC_FL_ZERO_RANGE));
2970 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
2972 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2973 FALLOC_FL_ZERO_RANGE))
2976 if (fm->fc->no_fallocate)
2982 filemap_invalidate_lock(inode->i_mapping);
2983 err = fuse_dax_break_layouts(inode, 0, 0);
2988 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
2989 loff_t endbyte = offset + length - 1;
2991 err = fuse_writeback_range(inode, offset, endbyte);
2997 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2998 offset + length > i_size_read(inode)) {
2999 err = inode_newsize_ok(inode, offset + length);
3004 err = file_modified(file);
3008 if (!(mode & FALLOC_FL_KEEP_SIZE))
3009 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3011 args.opcode = FUSE_FALLOCATE;
3012 args.nodeid = ff->nodeid;
3013 args.in_numargs = 1;
3014 args.in_args[0].size = sizeof(inarg);
3015 args.in_args[0].value = &inarg;
3016 err = fuse_simple_request(fm, &args);
3017 if (err == -ENOSYS) {
3018 fm->fc->no_fallocate = 1;
3024 /* we could have extended the file */
3025 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3026 if (fuse_write_update_attr(inode, offset + length, length))
3027 file_update_time(file);
3030 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
3031 truncate_pagecache_range(inode, offset, offset + length - 1);
3033 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
3036 if (!(mode & FALLOC_FL_KEEP_SIZE))
3037 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3040 filemap_invalidate_unlock(inode->i_mapping);
3043 inode_unlock(inode);
3045 fuse_flush_time_update(inode);
3050 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3051 struct file *file_out, loff_t pos_out,
3052 size_t len, unsigned int flags)
3054 struct fuse_file *ff_in = file_in->private_data;
3055 struct fuse_file *ff_out = file_out->private_data;
3056 struct inode *inode_in = file_inode(file_in);
3057 struct inode *inode_out = file_inode(file_out);
3058 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3059 struct fuse_mount *fm = ff_in->fm;
3060 struct fuse_conn *fc = fm->fc;
3062 struct fuse_copy_file_range_in inarg = {
3065 .nodeid_out = ff_out->nodeid,
3066 .fh_out = ff_out->fh,
3071 struct fuse_write_out outarg;
3073 /* mark unstable when write-back is not used, and file_out gets
3075 bool is_unstable = (!fc->writeback_cache) &&
3076 ((pos_out + len) > inode_out->i_size);
3078 if (fc->no_copy_file_range)
3081 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3084 inode_lock(inode_in);
3085 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3086 inode_unlock(inode_in);
3090 inode_lock(inode_out);
3092 err = file_modified(file_out);
3097 * Write out dirty pages in the destination file before sending the COPY
3098 * request to userspace. After the request is completed, truncate off
3099 * pages (including partial ones) from the cache that have been copied,
3100 * since these contain stale data at that point.
3102 * This should be mostly correct, but if the COPY writes to partial
3103 * pages (at the start or end) and the parts not covered by the COPY are
3104 * written through a memory map after calling fuse_writeback_range(),
3105 * then these partial page modifications will be lost on truncation.
3107 * It is unlikely that someone would rely on such mixed style
3108 * modifications. Yet this does give less guarantees than if the
3109 * copying was performed with write(2).
3111 * To fix this a mapping->invalidate_lock could be used to prevent new
3112 * faults while the copy is ongoing.
3114 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3119 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3121 args.opcode = FUSE_COPY_FILE_RANGE;
3122 args.nodeid = ff_in->nodeid;
3123 args.in_numargs = 1;
3124 args.in_args[0].size = sizeof(inarg);
3125 args.in_args[0].value = &inarg;
3126 args.out_numargs = 1;
3127 args.out_args[0].size = sizeof(outarg);
3128 args.out_args[0].value = &outarg;
3129 err = fuse_simple_request(fm, &args);
3130 if (err == -ENOSYS) {
3131 fc->no_copy_file_range = 1;
3137 truncate_inode_pages_range(inode_out->i_mapping,
3138 ALIGN_DOWN(pos_out, PAGE_SIZE),
3139 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3141 file_update_time(file_out);
3142 fuse_write_update_attr(inode_out, pos_out + outarg.size, outarg.size);
3147 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3149 inode_unlock(inode_out);
3150 file_accessed(file_in);
3152 fuse_flush_time_update(inode_out);
3157 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3158 struct file *dst_file, loff_t dst_off,
3159 size_t len, unsigned int flags)
3163 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3166 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3167 ret = generic_copy_file_range(src_file, src_off, dst_file,
3168 dst_off, len, flags);
3172 static const struct file_operations fuse_file_operations = {
3173 .llseek = fuse_file_llseek,
3174 .read_iter = fuse_file_read_iter,
3175 .write_iter = fuse_file_write_iter,
3176 .mmap = fuse_file_mmap,
3178 .flush = fuse_flush,
3179 .release = fuse_release,
3180 .fsync = fuse_fsync,
3181 .lock = fuse_file_lock,
3182 .get_unmapped_area = thp_get_unmapped_area,
3183 .flock = fuse_file_flock,
3184 .splice_read = generic_file_splice_read,
3185 .splice_write = iter_file_splice_write,
3186 .unlocked_ioctl = fuse_file_ioctl,
3187 .compat_ioctl = fuse_file_compat_ioctl,
3188 .poll = fuse_file_poll,
3189 .fallocate = fuse_file_fallocate,
3190 .copy_file_range = fuse_copy_file_range,
3193 static const struct address_space_operations fuse_file_aops = {
3194 .read_folio = fuse_read_folio,
3195 .readahead = fuse_readahead,
3196 .writepage = fuse_writepage,
3197 .writepages = fuse_writepages,
3198 .launder_folio = fuse_launder_folio,
3199 .dirty_folio = filemap_dirty_folio,
3201 .direct_IO = fuse_direct_IO,
3202 .write_begin = fuse_write_begin,
3203 .write_end = fuse_write_end,
3206 void fuse_init_file_inode(struct inode *inode, unsigned int flags)
3208 struct fuse_inode *fi = get_fuse_inode(inode);
3210 inode->i_fop = &fuse_file_operations;
3211 inode->i_data.a_ops = &fuse_file_aops;
3213 INIT_LIST_HEAD(&fi->write_files);
3214 INIT_LIST_HEAD(&fi->queued_writes);
3216 init_waitqueue_head(&fi->page_waitq);
3217 fi->writepages = RB_ROOT;
3219 if (IS_ENABLED(CONFIG_FUSE_DAX))
3220 fuse_dax_inode_init(inode, flags);