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>
21 #include <linux/filelock.h>
22 #include <linux/splice.h>
23 #include <linux/task_io_accounting_ops.h>
25 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
26 unsigned int open_flags, int opcode,
27 struct fuse_open_out *outargp)
29 struct fuse_open_in inarg;
32 memset(&inarg, 0, sizeof(inarg));
33 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
34 if (!fm->fc->atomic_o_trunc)
35 inarg.flags &= ~O_TRUNC;
37 if (fm->fc->handle_killpriv_v2 &&
38 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
39 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
45 args.in_args[0].size = sizeof(inarg);
46 args.in_args[0].value = &inarg;
48 args.out_args[0].size = sizeof(*outargp);
49 args.out_args[0].value = outargp;
51 return fuse_simple_request(fm, &args);
54 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm, bool release)
58 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
64 ff->args = kzalloc(sizeof(*ff->args), GFP_KERNEL_ACCOUNT);
71 INIT_LIST_HEAD(&ff->write_entry);
72 refcount_set(&ff->count, 1);
73 RB_CLEAR_NODE(&ff->polled_node);
74 init_waitqueue_head(&ff->poll_wait);
76 ff->kh = atomic64_inc_return(&fm->fc->khctr);
81 void fuse_file_free(struct fuse_file *ff)
87 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
89 refcount_inc(&ff->count);
93 static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
96 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
102 static void fuse_file_put(struct fuse_file *ff, bool sync)
104 if (refcount_dec_and_test(&ff->count)) {
105 struct fuse_release_args *ra = &ff->args->release_args;
106 struct fuse_args *args = (ra ? &ra->args : NULL);
109 fuse_file_io_release(ff, ra->inode);
112 /* Do nothing when server does not implement 'open' */
114 fuse_simple_request(ff->fm, args);
115 fuse_release_end(ff->fm, args, 0);
117 args->end = fuse_release_end;
118 if (fuse_simple_background(ff->fm, args,
119 GFP_KERNEL | __GFP_NOFAIL))
120 fuse_release_end(ff->fm, args, -ENOTCONN);
126 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
127 unsigned int open_flags, bool isdir)
129 struct fuse_conn *fc = fm->fc;
130 struct fuse_file *ff;
131 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
132 bool open = isdir ? !fc->no_opendir : !fc->no_open;
134 ff = fuse_file_alloc(fm, open);
136 return ERR_PTR(-ENOMEM);
139 /* Default for no-open */
140 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
142 /* Store outarg for fuse_finish_open() */
143 struct fuse_open_out *outargp = &ff->args->open_outarg;
146 err = fuse_send_open(fm, nodeid, open_flags, opcode, outargp);
148 ff->fh = outargp->fh;
149 ff->open_flags = outargp->open_flags;
150 } else if (err != -ENOSYS) {
154 /* No release needed */
165 ff->open_flags &= ~FOPEN_DIRECT_IO;
172 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
175 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
178 file->private_data = ff;
180 return PTR_ERR_OR_ZERO(ff);
182 EXPORT_SYMBOL_GPL(fuse_do_open);
184 static void fuse_link_write_file(struct file *file)
186 struct inode *inode = file_inode(file);
187 struct fuse_inode *fi = get_fuse_inode(inode);
188 struct fuse_file *ff = file->private_data;
190 * file may be written through mmap, so chain it onto the
191 * inodes's write_file list
193 spin_lock(&fi->lock);
194 if (list_empty(&ff->write_entry))
195 list_add(&ff->write_entry, &fi->write_files);
196 spin_unlock(&fi->lock);
199 int fuse_finish_open(struct inode *inode, struct file *file)
201 struct fuse_file *ff = file->private_data;
202 struct fuse_conn *fc = get_fuse_conn(inode);
205 err = fuse_file_io_open(file, inode);
209 if (ff->open_flags & FOPEN_STREAM)
210 stream_open(inode, file);
211 else if (ff->open_flags & FOPEN_NONSEEKABLE)
212 nonseekable_open(inode, file);
214 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
215 fuse_link_write_file(file);
220 static void fuse_truncate_update_attr(struct inode *inode, struct file *file)
222 struct fuse_conn *fc = get_fuse_conn(inode);
223 struct fuse_inode *fi = get_fuse_inode(inode);
225 spin_lock(&fi->lock);
226 fi->attr_version = atomic64_inc_return(&fc->attr_version);
227 i_size_write(inode, 0);
228 spin_unlock(&fi->lock);
229 file_update_time(file);
230 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
233 static int fuse_open(struct inode *inode, struct file *file)
235 struct fuse_mount *fm = get_fuse_mount(inode);
236 struct fuse_inode *fi = get_fuse_inode(inode);
237 struct fuse_conn *fc = fm->fc;
238 struct fuse_file *ff;
240 bool is_truncate = (file->f_flags & O_TRUNC) && fc->atomic_o_trunc;
241 bool is_wb_truncate = is_truncate && fc->writeback_cache;
242 bool dax_truncate = is_truncate && FUSE_IS_DAX(inode);
244 if (fuse_is_bad(inode))
247 err = generic_file_open(inode, file);
251 if (is_wb_truncate || dax_truncate)
255 filemap_invalidate_lock(inode->i_mapping);
256 err = fuse_dax_break_layouts(inode, 0, -1);
258 goto out_inode_unlock;
261 if (is_wb_truncate || dax_truncate)
262 fuse_set_nowrite(inode);
264 err = fuse_do_open(fm, get_node_id(inode), file, false);
266 ff = file->private_data;
267 err = fuse_finish_open(inode, file);
269 fuse_sync_release(fi, ff, file->f_flags);
270 else if (is_truncate)
271 fuse_truncate_update_attr(inode, file);
274 if (is_wb_truncate || dax_truncate)
275 fuse_release_nowrite(inode);
278 truncate_pagecache(inode, 0);
279 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
280 invalidate_inode_pages2(inode->i_mapping);
283 filemap_invalidate_unlock(inode->i_mapping);
285 if (is_wb_truncate || dax_truncate)
291 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
292 unsigned int flags, int opcode, bool sync)
294 struct fuse_conn *fc = ff->fm->fc;
295 struct fuse_release_args *ra = &ff->args->release_args;
297 if (fuse_file_passthrough(ff))
298 fuse_passthrough_release(ff, fuse_inode_backing(fi));
300 /* Inode is NULL on error path of fuse_create_open() */
302 spin_lock(&fi->lock);
303 list_del(&ff->write_entry);
304 spin_unlock(&fi->lock);
306 spin_lock(&fc->lock);
307 if (!RB_EMPTY_NODE(&ff->polled_node))
308 rb_erase(&ff->polled_node, &fc->polled_files);
309 spin_unlock(&fc->lock);
311 wake_up_interruptible_all(&ff->poll_wait);
316 /* ff->args was used for open outarg */
317 memset(ff->args, 0, sizeof(*ff->args));
318 ra->inarg.fh = ff->fh;
319 ra->inarg.flags = flags;
320 ra->args.in_numargs = 1;
321 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
322 ra->args.in_args[0].value = &ra->inarg;
323 ra->args.opcode = opcode;
324 ra->args.nodeid = ff->nodeid;
325 ra->args.force = true;
326 ra->args.nocreds = true;
329 * Hold inode until release is finished.
330 * From fuse_sync_release() the refcount is 1 and everything's
331 * synchronous, so we are fine with not doing igrab() here.
333 ra->inode = sync ? NULL : igrab(&fi->inode);
336 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
337 unsigned int open_flags, fl_owner_t id, bool isdir)
339 struct fuse_inode *fi = get_fuse_inode(inode);
340 struct fuse_release_args *ra = &ff->args->release_args;
341 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
343 fuse_prepare_release(fi, ff, open_flags, opcode, false);
345 if (ra && ff->flock) {
346 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
347 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
351 * Normally this will send the RELEASE request, however if
352 * some asynchronous READ or WRITE requests are outstanding,
353 * the sending will be delayed.
355 * Make the release synchronous if this is a fuseblk mount,
356 * synchronous RELEASE is allowed (and desirable) in this case
357 * because the server can be trusted not to screw up.
359 fuse_file_put(ff, ff->fm->fc->destroy);
362 void fuse_release_common(struct file *file, bool isdir)
364 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
365 (fl_owner_t) file, isdir);
368 static int fuse_release(struct inode *inode, struct file *file)
370 struct fuse_conn *fc = get_fuse_conn(inode);
373 * Dirty pages might remain despite write_inode_now() call from
374 * fuse_flush() due to writes racing with the close.
376 if (fc->writeback_cache)
377 write_inode_now(inode, 1);
379 fuse_release_common(file, false);
381 /* return value is ignored by VFS */
385 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
388 WARN_ON(refcount_read(&ff->count) > 1);
389 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE, true);
390 fuse_file_put(ff, true);
392 EXPORT_SYMBOL_GPL(fuse_sync_release);
395 * Scramble the ID space with XTEA, so that the value of the files_struct
396 * pointer is not exposed to userspace.
398 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
400 u32 *k = fc->scramble_key;
401 u64 v = (unsigned long) id;
407 for (i = 0; i < 32; i++) {
408 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
410 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
413 return (u64) v0 + ((u64) v1 << 32);
416 struct fuse_writepage_args {
417 struct fuse_io_args ia;
418 struct list_head queue_entry;
420 struct fuse_sync_bucket *bucket;
424 * Wait for all pending writepages on the inode to finish.
426 * This is currently done by blocking further writes with FUSE_NOWRITE
427 * and waiting for all sent writes to complete.
429 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
430 * could conflict with truncation.
432 static void fuse_sync_writes(struct inode *inode)
434 fuse_set_nowrite(inode);
435 fuse_release_nowrite(inode);
438 static int fuse_flush(struct file *file, fl_owner_t id)
440 struct inode *inode = file_inode(file);
441 struct fuse_mount *fm = get_fuse_mount(inode);
442 struct fuse_file *ff = file->private_data;
443 struct fuse_flush_in inarg;
447 if (fuse_is_bad(inode))
450 if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
453 err = write_inode_now(inode, 1);
457 err = filemap_check_errors(file->f_mapping);
462 if (fm->fc->no_flush)
465 memset(&inarg, 0, sizeof(inarg));
467 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
468 args.opcode = FUSE_FLUSH;
469 args.nodeid = get_node_id(inode);
471 args.in_args[0].size = sizeof(inarg);
472 args.in_args[0].value = &inarg;
475 err = fuse_simple_request(fm, &args);
476 if (err == -ENOSYS) {
477 fm->fc->no_flush = 1;
483 * In memory i_blocks is not maintained by fuse, if writeback cache is
484 * enabled, i_blocks from cached attr may not be accurate.
486 if (!err && fm->fc->writeback_cache)
487 fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
491 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
492 int datasync, int opcode)
494 struct inode *inode = file->f_mapping->host;
495 struct fuse_mount *fm = get_fuse_mount(inode);
496 struct fuse_file *ff = file->private_data;
498 struct fuse_fsync_in inarg;
500 memset(&inarg, 0, sizeof(inarg));
502 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
503 args.opcode = opcode;
504 args.nodeid = get_node_id(inode);
506 args.in_args[0].size = sizeof(inarg);
507 args.in_args[0].value = &inarg;
508 return fuse_simple_request(fm, &args);
511 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
514 struct inode *inode = file->f_mapping->host;
515 struct fuse_conn *fc = get_fuse_conn(inode);
518 if (fuse_is_bad(inode))
524 * Start writeback against all dirty pages of the inode, then
525 * wait for all outstanding writes, before sending the FSYNC
528 err = file_write_and_wait_range(file, start, end);
532 fuse_sync_writes(inode);
535 * Due to implementation of fuse writeback
536 * file_write_and_wait_range() does not catch errors.
537 * We have to do this directly after fuse_sync_writes()
539 err = file_check_and_advance_wb_err(file);
543 err = sync_inode_metadata(inode, 1);
550 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
551 if (err == -ENOSYS) {
561 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
562 size_t count, int opcode)
564 struct fuse_file *ff = file->private_data;
565 struct fuse_args *args = &ia->ap.args;
567 ia->read.in.fh = ff->fh;
568 ia->read.in.offset = pos;
569 ia->read.in.size = count;
570 ia->read.in.flags = file->f_flags;
571 args->opcode = opcode;
572 args->nodeid = ff->nodeid;
573 args->in_numargs = 1;
574 args->in_args[0].size = sizeof(ia->read.in);
575 args->in_args[0].value = &ia->read.in;
576 args->out_argvar = true;
577 args->out_numargs = 1;
578 args->out_args[0].size = count;
581 static void fuse_release_user_pages(struct fuse_args_pages *ap, ssize_t nres,
586 for (i = 0; i < ap->num_folios; i++) {
588 folio_mark_dirty_lock(ap->folios[i]);
589 if (ap->args.is_pinned)
590 unpin_folio(ap->folios[i]);
593 if (nres > 0 && ap->args.invalidate_vmap)
594 invalidate_kernel_vmap_range(ap->args.vmap_base, nres);
597 static void fuse_io_release(struct kref *kref)
599 kfree(container_of(kref, struct fuse_io_priv, refcnt));
602 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
607 if (io->bytes >= 0 && io->write)
610 return io->bytes < 0 ? io->size : io->bytes;
614 * In case of short read, the caller sets 'pos' to the position of
615 * actual end of fuse request in IO request. Otherwise, if bytes_requested
616 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
619 * User requested DIO read of 64K. It was split into two 32K fuse requests,
620 * both submitted asynchronously. The first of them was ACKed by userspace as
621 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
622 * second request was ACKed as short, e.g. only 1K was read, resulting in
625 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
626 * will be equal to the length of the longest contiguous fragment of
627 * transferred data starting from the beginning of IO request.
629 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
633 spin_lock(&io->lock);
635 io->err = io->err ? : err;
636 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
640 if (!left && io->blocking)
642 spin_unlock(&io->lock);
644 if (!left && !io->blocking) {
645 ssize_t res = fuse_get_res_by_io(io);
648 struct inode *inode = file_inode(io->iocb->ki_filp);
649 struct fuse_conn *fc = get_fuse_conn(inode);
650 struct fuse_inode *fi = get_fuse_inode(inode);
652 spin_lock(&fi->lock);
653 fi->attr_version = atomic64_inc_return(&fc->attr_version);
654 spin_unlock(&fi->lock);
657 io->iocb->ki_complete(io->iocb, res);
660 kref_put(&io->refcnt, fuse_io_release);
663 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
664 unsigned int nfolios)
666 struct fuse_io_args *ia;
668 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
671 ia->ap.folios = fuse_folios_alloc(nfolios, GFP_KERNEL,
673 if (!ia->ap.folios) {
681 static void fuse_io_free(struct fuse_io_args *ia)
683 kfree(ia->ap.folios);
687 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
690 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
691 struct fuse_io_priv *io = ia->io;
697 } else if (io->write) {
698 if (ia->write.out.size > ia->write.in.size) {
701 nres = ia->write.out.size;
702 if (ia->write.in.size != ia->write.out.size)
703 pos = ia->write.in.offset - io->offset +
707 u32 outsize = args->out_args[0].size;
710 if (ia->read.in.size != outsize)
711 pos = ia->read.in.offset - io->offset + outsize;
714 fuse_release_user_pages(&ia->ap, err ?: nres, io->should_dirty);
716 fuse_aio_complete(io, err, pos);
720 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
721 struct fuse_io_args *ia, size_t num_bytes)
724 struct fuse_io_priv *io = ia->io;
726 spin_lock(&io->lock);
727 kref_get(&io->refcnt);
728 io->size += num_bytes;
730 spin_unlock(&io->lock);
732 ia->ap.args.end = fuse_aio_complete_req;
733 ia->ap.args.may_block = io->should_dirty;
734 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
736 fuse_aio_complete_req(fm, &ia->ap.args, err);
741 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
744 struct file *file = ia->io->iocb->ki_filp;
745 struct fuse_file *ff = file->private_data;
746 struct fuse_mount *fm = ff->fm;
748 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
750 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
751 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
755 return fuse_async_req_send(fm, ia, count);
757 return fuse_simple_request(fm, &ia->ap.args);
760 static void fuse_read_update_size(struct inode *inode, loff_t size,
763 struct fuse_conn *fc = get_fuse_conn(inode);
764 struct fuse_inode *fi = get_fuse_inode(inode);
766 spin_lock(&fi->lock);
767 if (attr_ver >= fi->attr_version && size < inode->i_size &&
768 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
769 fi->attr_version = atomic64_inc_return(&fc->attr_version);
770 i_size_write(inode, size);
772 spin_unlock(&fi->lock);
775 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
776 struct fuse_args_pages *ap)
778 struct fuse_conn *fc = get_fuse_conn(inode);
781 * If writeback_cache is enabled, a short read means there's a hole in
782 * the file. Some data after the hole is in page cache, but has not
783 * reached the client fs yet. So the hole is not present there.
785 if (!fc->writeback_cache) {
786 loff_t pos = folio_pos(ap->folios[0]) + num_read;
787 fuse_read_update_size(inode, pos, attr_ver);
791 static int fuse_do_readfolio(struct file *file, struct folio *folio)
793 struct inode *inode = folio->mapping->host;
794 struct fuse_mount *fm = get_fuse_mount(inode);
795 loff_t pos = folio_pos(folio);
796 struct fuse_folio_desc desc = { .length = folio_size(folio) };
797 struct fuse_io_args ia = {
798 .ap.args.page_zeroing = true,
799 .ap.args.out_pages = true,
807 attr_ver = fuse_get_attr_version(fm->fc);
809 /* Don't overflow end offset */
810 if (pos + (desc.length - 1) == LLONG_MAX)
813 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
814 res = fuse_simple_request(fm, &ia.ap.args);
818 * Short read means EOF. If file size is larger, truncate it
820 if (res < desc.length)
821 fuse_short_read(inode, attr_ver, res, &ia.ap);
823 folio_mark_uptodate(folio);
828 static int fuse_read_folio(struct file *file, struct folio *folio)
830 struct inode *inode = folio->mapping->host;
834 if (fuse_is_bad(inode))
837 err = fuse_do_readfolio(file, folio);
838 fuse_invalidate_atime(inode);
844 static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
848 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
849 struct fuse_args_pages *ap = &ia->ap;
850 size_t count = ia->read.in.size;
851 size_t num_read = args->out_args[0].size;
852 struct address_space *mapping = NULL;
854 for (i = 0; mapping == NULL && i < ap->num_folios; i++)
855 mapping = ap->folios[i]->mapping;
858 struct inode *inode = mapping->host;
861 * Short read means EOF. If file size is larger, truncate it
863 if (!err && num_read < count)
864 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
866 fuse_invalidate_atime(inode);
869 for (i = 0; i < ap->num_folios; i++) {
870 folio_end_read(ap->folios[i], !err);
871 folio_put(ap->folios[i]);
874 fuse_file_put(ia->ff, false);
879 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file,
882 struct fuse_file *ff = file->private_data;
883 struct fuse_mount *fm = ff->fm;
884 struct fuse_args_pages *ap = &ia->ap;
885 loff_t pos = folio_pos(ap->folios[0]);
889 ap->args.out_pages = true;
890 ap->args.page_zeroing = true;
891 ap->args.page_replace = true;
893 /* Don't overflow end offset */
894 if (pos + (count - 1) == LLONG_MAX) {
896 ap->descs[ap->num_folios - 1].length--;
898 WARN_ON((loff_t) (pos + count) < 0);
900 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
901 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
902 if (fm->fc->async_read) {
903 ia->ff = fuse_file_get(ff);
904 ap->args.end = fuse_readpages_end;
905 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
909 res = fuse_simple_request(fm, &ap->args);
910 err = res < 0 ? res : 0;
912 fuse_readpages_end(fm, &ap->args, err);
915 static void fuse_readahead(struct readahead_control *rac)
917 struct inode *inode = rac->mapping->host;
918 struct fuse_conn *fc = get_fuse_conn(inode);
919 unsigned int max_pages, nr_pages;
920 struct folio *folio = NULL;
922 if (fuse_is_bad(inode))
925 max_pages = min_t(unsigned int, fc->max_pages,
926 fc->max_read / PAGE_SIZE);
929 * This is only accurate the first time through, since readahead_folio()
930 * doesn't update readahead_count() from the previous folio until the
931 * next call. Grab nr_pages here so we know how many pages we're going
932 * to have to process. This means that we will exit here with
933 * readahead_count() == folio_nr_pages(last_folio), but we will have
934 * consumed all of the folios, and read_pages() will call
935 * readahead_folio() again which will clean up the rac.
937 nr_pages = readahead_count(rac);
940 struct fuse_io_args *ia;
941 struct fuse_args_pages *ap;
942 unsigned cur_pages = min(max_pages, nr_pages);
943 unsigned int pages = 0;
945 if (fc->num_background >= fc->congestion_threshold &&
946 rac->ra->async_size >= readahead_count(rac))
948 * Congested and only async pages left, so skip the
953 ia = fuse_io_alloc(NULL, cur_pages);
958 while (pages < cur_pages) {
959 unsigned int folio_pages;
962 * This returns a folio with a ref held on it.
963 * The ref needs to be held until the request is
964 * completed, since the splice case (see
965 * fuse_try_move_page()) drops the ref after it's
966 * replaced in the page cache.
969 folio = __readahead_folio(rac);
971 folio_pages = folio_nr_pages(folio);
972 if (folio_pages > cur_pages - pages) {
974 * Large folios belonging to fuse will never
975 * have more pages than max_pages.
981 ap->folios[ap->num_folios] = folio;
982 ap->descs[ap->num_folios].length = folio_size(folio);
984 pages += folio_pages;
987 fuse_send_readpages(ia, rac->file, pages << PAGE_SHIFT);
991 folio_end_read(folio, false);
996 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
998 struct inode *inode = iocb->ki_filp->f_mapping->host;
999 struct fuse_conn *fc = get_fuse_conn(inode);
1002 * In auto invalidate mode, always update attributes on read.
1003 * Otherwise, only update if we attempt to read past EOF (to ensure
1004 * i_size is up to date).
1006 if (fc->auto_inval_data ||
1007 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1009 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE);
1014 return generic_file_read_iter(iocb, to);
1017 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1018 loff_t pos, size_t count)
1020 struct fuse_args *args = &ia->ap.args;
1022 ia->write.in.fh = ff->fh;
1023 ia->write.in.offset = pos;
1024 ia->write.in.size = count;
1025 args->opcode = FUSE_WRITE;
1026 args->nodeid = ff->nodeid;
1027 args->in_numargs = 2;
1028 if (ff->fm->fc->minor < 9)
1029 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1031 args->in_args[0].size = sizeof(ia->write.in);
1032 args->in_args[0].value = &ia->write.in;
1033 args->in_args[1].size = count;
1034 args->out_numargs = 1;
1035 args->out_args[0].size = sizeof(ia->write.out);
1036 args->out_args[0].value = &ia->write.out;
1039 static unsigned int fuse_write_flags(struct kiocb *iocb)
1041 unsigned int flags = iocb->ki_filp->f_flags;
1043 if (iocb_is_dsync(iocb))
1045 if (iocb->ki_flags & IOCB_SYNC)
1051 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1052 size_t count, fl_owner_t owner)
1054 struct kiocb *iocb = ia->io->iocb;
1055 struct file *file = iocb->ki_filp;
1056 struct fuse_file *ff = file->private_data;
1057 struct fuse_mount *fm = ff->fm;
1058 struct fuse_write_in *inarg = &ia->write.in;
1061 fuse_write_args_fill(ia, ff, pos, count);
1062 inarg->flags = fuse_write_flags(iocb);
1063 if (owner != NULL) {
1064 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1065 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
1069 return fuse_async_req_send(fm, ia, count);
1071 err = fuse_simple_request(fm, &ia->ap.args);
1072 if (!err && ia->write.out.size > count)
1075 return err ?: ia->write.out.size;
1078 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)
1080 struct fuse_conn *fc = get_fuse_conn(inode);
1081 struct fuse_inode *fi = get_fuse_inode(inode);
1084 spin_lock(&fi->lock);
1085 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1086 if (written > 0 && pos > inode->i_size) {
1087 i_size_write(inode, pos);
1090 spin_unlock(&fi->lock);
1092 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
1097 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1098 struct kiocb *iocb, struct inode *inode,
1099 loff_t pos, size_t count)
1101 struct fuse_args_pages *ap = &ia->ap;
1102 struct file *file = iocb->ki_filp;
1103 struct fuse_file *ff = file->private_data;
1104 struct fuse_mount *fm = ff->fm;
1105 unsigned int offset, i;
1109 for (i = 0; i < ap->num_folios; i++)
1110 folio_wait_writeback(ap->folios[i]);
1112 fuse_write_args_fill(ia, ff, pos, count);
1113 ia->write.in.flags = fuse_write_flags(iocb);
1114 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1115 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1117 err = fuse_simple_request(fm, &ap->args);
1118 if (!err && ia->write.out.size > count)
1121 short_write = ia->write.out.size < count;
1122 offset = ap->descs[0].offset;
1123 count = ia->write.out.size;
1124 for (i = 0; i < ap->num_folios; i++) {
1125 struct folio *folio = ap->folios[i];
1128 folio_clear_uptodate(folio);
1130 if (count >= folio_size(folio) - offset)
1131 count -= folio_size(folio) - offset;
1134 folio_clear_uptodate(folio);
1139 if (ia->write.folio_locked && (i == ap->num_folios - 1))
1140 folio_unlock(folio);
1147 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
1148 struct address_space *mapping,
1149 struct iov_iter *ii, loff_t pos,
1150 unsigned int max_folios)
1152 struct fuse_args_pages *ap = &ia->ap;
1153 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1154 unsigned offset = pos & (PAGE_SIZE - 1);
1159 num = min(iov_iter_count(ii), fc->max_write);
1161 ap->args.in_pages = true;
1162 ap->descs[0].offset = offset;
1164 while (num && ap->num_folios < max_folios) {
1166 struct folio *folio;
1167 pgoff_t index = pos >> PAGE_SHIFT;
1169 unsigned int folio_offset;
1172 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
1173 mapping_gfp_mask(mapping));
1174 if (IS_ERR(folio)) {
1175 err = PTR_ERR(folio);
1179 if (mapping_writably_mapped(mapping))
1180 flush_dcache_folio(folio);
1182 folio_offset = ((index - folio->index) << PAGE_SHIFT) + offset;
1183 bytes = min(folio_size(folio) - folio_offset, num);
1185 tmp = copy_folio_from_iter_atomic(folio, folio_offset, bytes, ii);
1186 flush_dcache_folio(folio);
1189 folio_unlock(folio);
1193 * Ensure forward progress by faulting in
1194 * while not holding the folio lock:
1196 if (fault_in_iov_iter_readable(ii, bytes)) {
1204 ap->folios[ap->num_folios] = folio;
1205 ap->descs[ap->num_folios].offset = folio_offset;
1206 ap->descs[ap->num_folios].length = tmp;
1213 if (offset == folio_size(folio))
1216 /* If we copied full folio, mark it uptodate */
1217 if (tmp == folio_size(folio))
1218 folio_mark_uptodate(folio);
1220 if (folio_test_uptodate(folio)) {
1221 folio_unlock(folio);
1223 ia->write.folio_locked = true;
1226 if (!fc->big_writes || offset != 0)
1230 return count > 0 ? count : err;
1233 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1234 unsigned int max_pages)
1236 return min_t(unsigned int,
1237 ((pos + len - 1) >> PAGE_SHIFT) -
1238 (pos >> PAGE_SHIFT) + 1,
1242 static ssize_t fuse_perform_write(struct kiocb *iocb, struct iov_iter *ii)
1244 struct address_space *mapping = iocb->ki_filp->f_mapping;
1245 struct inode *inode = mapping->host;
1246 struct fuse_conn *fc = get_fuse_conn(inode);
1247 struct fuse_inode *fi = get_fuse_inode(inode);
1248 loff_t pos = iocb->ki_pos;
1252 if (inode->i_size < pos + iov_iter_count(ii))
1253 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1257 struct fuse_io_args ia = {};
1258 struct fuse_args_pages *ap = &ia.ap;
1259 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1262 ap->folios = fuse_folios_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1268 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
1272 err = fuse_send_write_pages(&ia, iocb, inode,
1275 size_t num_written = ia.write.out.size;
1280 /* break out of the loop on short write */
1281 if (num_written != count)
1286 } while (!err && iov_iter_count(ii));
1288 fuse_write_update_attr(inode, pos, res);
1289 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1293 iocb->ki_pos += res;
1297 static bool fuse_io_past_eof(struct kiocb *iocb, struct iov_iter *iter)
1299 struct inode *inode = file_inode(iocb->ki_filp);
1301 return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode);
1305 * @return true if an exclusive lock for direct IO writes is needed
1307 static bool fuse_dio_wr_exclusive_lock(struct kiocb *iocb, struct iov_iter *from)
1309 struct file *file = iocb->ki_filp;
1310 struct fuse_file *ff = file->private_data;
1311 struct inode *inode = file_inode(iocb->ki_filp);
1312 struct fuse_inode *fi = get_fuse_inode(inode);
1314 /* Server side has to advise that it supports parallel dio writes. */
1315 if (!(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES))
1319 * Append will need to know the eventual EOF - always needs an
1322 if (iocb->ki_flags & IOCB_APPEND)
1325 /* shared locks are not allowed with parallel page cache IO */
1326 if (test_bit(FUSE_I_CACHE_IO_MODE, &fi->state))
1329 /* Parallel dio beyond EOF is not supported, at least for now. */
1330 if (fuse_io_past_eof(iocb, from))
1336 static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from,
1339 struct inode *inode = file_inode(iocb->ki_filp);
1340 struct fuse_inode *fi = get_fuse_inode(inode);
1342 *exclusive = fuse_dio_wr_exclusive_lock(iocb, from);
1346 inode_lock_shared(inode);
1348 * New parallal dio allowed only if inode is not in caching
1349 * mode and denies new opens in caching mode. This check
1350 * should be performed only after taking shared inode lock.
1351 * Previous past eof check was without inode lock and might
1352 * have raced, so check it again.
1354 if (fuse_io_past_eof(iocb, from) ||
1355 fuse_inode_uncached_io_start(fi, NULL) != 0) {
1356 inode_unlock_shared(inode);
1363 static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive)
1365 struct inode *inode = file_inode(iocb->ki_filp);
1366 struct fuse_inode *fi = get_fuse_inode(inode);
1369 inode_unlock(inode);
1371 /* Allow opens in caching mode after last parallel dio end */
1372 fuse_inode_uncached_io_end(fi);
1373 inode_unlock_shared(inode);
1377 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1379 struct file *file = iocb->ki_filp;
1380 struct mnt_idmap *idmap = file_mnt_idmap(file);
1381 struct address_space *mapping = file->f_mapping;
1382 ssize_t written = 0;
1383 struct inode *inode = mapping->host;
1385 struct fuse_conn *fc = get_fuse_conn(inode);
1387 if (fc->writeback_cache) {
1388 /* Update size (EOF optimization) and mode (SUID clearing) */
1389 err = fuse_update_attributes(mapping->host, file,
1390 STATX_SIZE | STATX_MODE);
1394 if (fc->handle_killpriv_v2 &&
1395 setattr_should_drop_suidgid(idmap,
1396 file_inode(file))) {
1400 return generic_file_write_iter(iocb, from);
1406 err = count = generic_write_checks(iocb, from);
1410 task_io_account_write(count);
1412 err = kiocb_modified(iocb);
1416 if (iocb->ki_flags & IOCB_DIRECT) {
1417 written = generic_file_direct_write(iocb, from);
1418 if (written < 0 || !iov_iter_count(from))
1420 written = direct_write_fallback(iocb, from, written,
1421 fuse_perform_write(iocb, from));
1423 written = fuse_perform_write(iocb, from);
1426 inode_unlock(inode);
1428 written = generic_write_sync(iocb, written);
1430 return written ? written : err;
1433 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1435 return (unsigned long)iter_iov(ii)->iov_base + ii->iov_offset;
1438 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1441 return min(iov_iter_single_seg_count(ii), max_size);
1444 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1445 size_t *nbytesp, int write,
1446 unsigned int max_pages,
1447 bool use_pages_for_kvec_io)
1449 bool flush_or_invalidate = false;
1450 unsigned int nr_pages = 0;
1451 size_t nbytes = 0; /* # bytes already packed in req */
1454 /* Special case for kernel I/O: can copy directly into the buffer.
1455 * However if the implementation of fuse_conn requires pages instead of
1456 * pointer (e.g., virtio-fs), use iov_iter_extract_pages() instead.
1458 if (iov_iter_is_kvec(ii)) {
1459 void *user_addr = (void *)fuse_get_user_addr(ii);
1461 if (!use_pages_for_kvec_io) {
1462 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1465 ap->args.in_args[1].value = user_addr;
1467 ap->args.out_args[0].value = user_addr;
1469 iov_iter_advance(ii, frag_size);
1470 *nbytesp = frag_size;
1474 if (is_vmalloc_addr(user_addr)) {
1475 ap->args.vmap_base = user_addr;
1476 flush_or_invalidate = true;
1481 * Until there is support for iov_iter_extract_folios(), we have to
1482 * manually extract pages using iov_iter_extract_pages() and then
1483 * copy that to a folios array.
1485 struct page **pages = kzalloc(max_pages * sizeof(struct page *),
1492 while (nbytes < *nbytesp && nr_pages < max_pages) {
1493 unsigned nfolios, i;
1496 ret = iov_iter_extract_pages(ii, &pages,
1498 max_pages - nr_pages,
1505 nfolios = DIV_ROUND_UP(ret + start, PAGE_SIZE);
1507 for (i = 0; i < nfolios; i++) {
1508 struct folio *folio = page_folio(pages[i]);
1509 unsigned int offset = start +
1510 (folio_page_idx(folio, pages[i]) << PAGE_SHIFT);
1511 unsigned int len = min_t(unsigned int, ret, PAGE_SIZE - start);
1513 ap->descs[ap->num_folios].offset = offset;
1514 ap->descs[ap->num_folios].length = len;
1515 ap->folios[ap->num_folios] = folio;
1521 nr_pages += nfolios;
1525 if (write && flush_or_invalidate)
1526 flush_kernel_vmap_range(ap->args.vmap_base, nbytes);
1528 ap->args.invalidate_vmap = !write && flush_or_invalidate;
1529 ap->args.is_pinned = iov_iter_extract_will_pin(ii);
1530 ap->args.user_pages = true;
1532 ap->args.in_pages = true;
1534 ap->args.out_pages = true;
1539 return ret < 0 ? ret : 0;
1542 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1543 loff_t *ppos, int flags)
1545 int write = flags & FUSE_DIO_WRITE;
1546 int cuse = flags & FUSE_DIO_CUSE;
1547 struct file *file = io->iocb->ki_filp;
1548 struct address_space *mapping = file->f_mapping;
1549 struct inode *inode = mapping->host;
1550 struct fuse_file *ff = file->private_data;
1551 struct fuse_conn *fc = ff->fm->fc;
1552 size_t nmax = write ? fc->max_write : fc->max_read;
1554 size_t count = iov_iter_count(iter);
1555 pgoff_t idx_from = pos >> PAGE_SHIFT;
1556 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1559 struct fuse_io_args *ia;
1560 unsigned int max_pages;
1561 bool fopen_direct_io = ff->open_flags & FOPEN_DIRECT_IO;
1563 max_pages = iov_iter_npages(iter, fc->max_pages);
1564 ia = fuse_io_alloc(io, max_pages);
1568 if (fopen_direct_io && fc->direct_io_allow_mmap) {
1569 res = filemap_write_and_wait_range(mapping, pos, pos + count - 1);
1575 if (!cuse && filemap_range_has_writeback(mapping, pos, (pos + count - 1))) {
1578 fuse_sync_writes(inode);
1580 inode_unlock(inode);
1583 if (fopen_direct_io && write) {
1584 res = invalidate_inode_pages2_range(mapping, idx_from, idx_to);
1591 io->should_dirty = !write && user_backed_iter(iter);
1594 fl_owner_t owner = current->files;
1595 size_t nbytes = min(count, nmax);
1597 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1598 max_pages, fc->use_pages_for_kvec_io);
1603 if (!capable(CAP_FSETID))
1604 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1606 nres = fuse_send_write(ia, pos, nbytes, owner);
1608 nres = fuse_send_read(ia, pos, nbytes, owner);
1611 if (!io->async || nres < 0) {
1612 fuse_release_user_pages(&ia->ap, nres, io->should_dirty);
1617 iov_iter_revert(iter, nbytes);
1621 WARN_ON(nres > nbytes);
1626 if (nres != nbytes) {
1627 iov_iter_revert(iter, nbytes - nres);
1631 max_pages = iov_iter_npages(iter, fc->max_pages);
1632 ia = fuse_io_alloc(io, max_pages);
1642 return res > 0 ? res : err;
1644 EXPORT_SYMBOL_GPL(fuse_direct_io);
1646 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1647 struct iov_iter *iter,
1651 struct inode *inode = file_inode(io->iocb->ki_filp);
1653 res = fuse_direct_io(io, iter, ppos, 0);
1655 fuse_invalidate_atime(inode);
1660 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1662 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1666 if (!is_sync_kiocb(iocb)) {
1667 res = fuse_direct_IO(iocb, to);
1669 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1671 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1677 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1679 struct inode *inode = file_inode(iocb->ki_filp);
1683 fuse_dio_lock(iocb, from, &exclusive);
1684 res = generic_write_checks(iocb, from);
1686 task_io_account_write(res);
1687 if (!is_sync_kiocb(iocb)) {
1688 res = fuse_direct_IO(iocb, from);
1690 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1692 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1694 fuse_write_update_attr(inode, iocb->ki_pos, res);
1697 fuse_dio_unlock(iocb, exclusive);
1702 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1704 struct file *file = iocb->ki_filp;
1705 struct fuse_file *ff = file->private_data;
1706 struct inode *inode = file_inode(file);
1708 if (fuse_is_bad(inode))
1711 if (FUSE_IS_DAX(inode))
1712 return fuse_dax_read_iter(iocb, to);
1714 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
1715 if (ff->open_flags & FOPEN_DIRECT_IO)
1716 return fuse_direct_read_iter(iocb, to);
1717 else if (fuse_file_passthrough(ff))
1718 return fuse_passthrough_read_iter(iocb, to);
1720 return fuse_cache_read_iter(iocb, to);
1723 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1725 struct file *file = iocb->ki_filp;
1726 struct fuse_file *ff = file->private_data;
1727 struct inode *inode = file_inode(file);
1729 if (fuse_is_bad(inode))
1732 if (FUSE_IS_DAX(inode))
1733 return fuse_dax_write_iter(iocb, from);
1735 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
1736 if (ff->open_flags & FOPEN_DIRECT_IO)
1737 return fuse_direct_write_iter(iocb, from);
1738 else if (fuse_file_passthrough(ff))
1739 return fuse_passthrough_write_iter(iocb, from);
1741 return fuse_cache_write_iter(iocb, from);
1744 static ssize_t fuse_splice_read(struct file *in, loff_t *ppos,
1745 struct pipe_inode_info *pipe, size_t len,
1748 struct fuse_file *ff = in->private_data;
1750 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
1751 if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
1752 return fuse_passthrough_splice_read(in, ppos, pipe, len, flags);
1754 return filemap_splice_read(in, ppos, pipe, len, flags);
1757 static ssize_t fuse_splice_write(struct pipe_inode_info *pipe, struct file *out,
1758 loff_t *ppos, size_t len, unsigned int flags)
1760 struct fuse_file *ff = out->private_data;
1762 /* FOPEN_DIRECT_IO overrides FOPEN_PASSTHROUGH */
1763 if (fuse_file_passthrough(ff) && !(ff->open_flags & FOPEN_DIRECT_IO))
1764 return fuse_passthrough_splice_write(pipe, out, ppos, len, flags);
1766 return iter_file_splice_write(pipe, out, ppos, len, flags);
1769 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1771 struct fuse_args_pages *ap = &wpa->ia.ap;
1774 fuse_sync_bucket_dec(wpa->bucket);
1776 fuse_file_put(wpa->ia.ff, false);
1782 static void fuse_writepage_finish(struct fuse_writepage_args *wpa)
1784 struct fuse_args_pages *ap = &wpa->ia.ap;
1785 struct inode *inode = wpa->inode;
1786 struct fuse_inode *fi = get_fuse_inode(inode);
1787 struct backing_dev_info *bdi = inode_to_bdi(inode);
1790 for (i = 0; i < ap->num_folios; i++) {
1792 * Benchmarks showed that ending writeback within the
1793 * scope of the fi->lock alleviates xarray lock
1794 * contention and noticeably improves performance.
1796 folio_end_writeback(ap->folios[i]);
1797 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1798 wb_writeout_inc(&bdi->wb);
1801 wake_up(&fi->page_waitq);
1804 /* Called under fi->lock, may release and reacquire it */
1805 static void fuse_send_writepage(struct fuse_mount *fm,
1806 struct fuse_writepage_args *wpa, loff_t size)
1807 __releases(fi->lock)
1808 __acquires(fi->lock)
1810 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1811 struct fuse_args_pages *ap = &wpa->ia.ap;
1812 struct fuse_write_in *inarg = &wpa->ia.write.in;
1813 struct fuse_args *args = &ap->args;
1814 __u64 data_size = 0;
1817 for (i = 0; i < ap->num_folios; i++)
1818 data_size += ap->descs[i].length;
1821 if (inarg->offset + data_size <= size) {
1822 inarg->size = data_size;
1823 } else if (inarg->offset < size) {
1824 inarg->size = size - inarg->offset;
1826 /* Got truncated off completely */
1830 args->in_args[1].size = inarg->size;
1832 args->nocreds = true;
1834 err = fuse_simple_background(fm, args, GFP_ATOMIC);
1835 if (err == -ENOMEM) {
1836 spin_unlock(&fi->lock);
1837 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
1838 spin_lock(&fi->lock);
1841 /* Fails on broken connection only */
1849 fuse_writepage_finish(wpa);
1850 spin_unlock(&fi->lock);
1851 fuse_writepage_free(wpa);
1852 spin_lock(&fi->lock);
1856 * If fi->writectr is positive (no truncate or fsync going on) send
1857 * all queued writepage requests.
1859 * Called with fi->lock
1861 void fuse_flush_writepages(struct inode *inode)
1862 __releases(fi->lock)
1863 __acquires(fi->lock)
1865 struct fuse_mount *fm = get_fuse_mount(inode);
1866 struct fuse_inode *fi = get_fuse_inode(inode);
1867 loff_t crop = i_size_read(inode);
1868 struct fuse_writepage_args *wpa;
1870 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1871 wpa = list_entry(fi->queued_writes.next,
1872 struct fuse_writepage_args, queue_entry);
1873 list_del_init(&wpa->queue_entry);
1874 fuse_send_writepage(fm, wpa, crop);
1878 static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
1881 struct fuse_writepage_args *wpa =
1882 container_of(args, typeof(*wpa), ia.ap.args);
1883 struct inode *inode = wpa->inode;
1884 struct fuse_inode *fi = get_fuse_inode(inode);
1885 struct fuse_conn *fc = get_fuse_conn(inode);
1887 mapping_set_error(inode->i_mapping, error);
1889 * A writeback finished and this might have updated mtime/ctime on
1890 * server making local mtime/ctime stale. Hence invalidate attrs.
1891 * Do this only if writeback_cache is not enabled. If writeback_cache
1892 * is enabled, we trust local ctime/mtime.
1894 if (!fc->writeback_cache)
1895 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
1896 spin_lock(&fi->lock);
1898 fuse_writepage_finish(wpa);
1899 spin_unlock(&fi->lock);
1900 fuse_writepage_free(wpa);
1903 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
1905 struct fuse_file *ff;
1907 spin_lock(&fi->lock);
1908 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file,
1912 spin_unlock(&fi->lock);
1917 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
1919 struct fuse_file *ff = __fuse_write_file_get(fi);
1924 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1926 struct fuse_inode *fi = get_fuse_inode(inode);
1927 struct fuse_file *ff;
1931 * Inode is always written before the last reference is dropped and
1932 * hence this should not be reached from reclaim.
1934 * Writing back the inode from reclaim can deadlock if the request
1935 * processing itself needs an allocation. Allocations triggering
1936 * reclaim while serving a request can't be prevented, because it can
1937 * involve any number of unrelated userspace processes.
1939 WARN_ON(wbc->for_reclaim);
1941 ff = __fuse_write_file_get(fi);
1942 err = fuse_flush_times(inode, ff);
1944 fuse_file_put(ff, false);
1949 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1951 struct fuse_writepage_args *wpa;
1952 struct fuse_args_pages *ap;
1954 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1958 ap->folios = fuse_folios_alloc(1, GFP_NOFS, &ap->descs);
1968 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
1969 struct fuse_writepage_args *wpa)
1975 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
1977 wpa->bucket = rcu_dereference(fc->curr_bucket);
1978 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
1982 static void fuse_writepage_args_page_fill(struct fuse_writepage_args *wpa, struct folio *folio,
1983 uint32_t folio_index)
1985 struct inode *inode = folio->mapping->host;
1986 struct fuse_args_pages *ap = &wpa->ia.ap;
1988 ap->folios[folio_index] = folio;
1989 ap->descs[folio_index].offset = 0;
1990 ap->descs[folio_index].length = folio_size(folio);
1992 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1995 static struct fuse_writepage_args *fuse_writepage_args_setup(struct folio *folio,
1996 struct fuse_file *ff)
1998 struct inode *inode = folio->mapping->host;
1999 struct fuse_conn *fc = get_fuse_conn(inode);
2000 struct fuse_writepage_args *wpa;
2001 struct fuse_args_pages *ap;
2003 wpa = fuse_writepage_args_alloc();
2007 fuse_writepage_add_to_bucket(fc, wpa);
2008 fuse_write_args_fill(&wpa->ia, ff, folio_pos(folio), 0);
2009 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2014 ap->args.in_pages = true;
2015 ap->args.end = fuse_writepage_end;
2020 static int fuse_writepage_locked(struct folio *folio)
2022 struct address_space *mapping = folio->mapping;
2023 struct inode *inode = mapping->host;
2024 struct fuse_inode *fi = get_fuse_inode(inode);
2025 struct fuse_writepage_args *wpa;
2026 struct fuse_args_pages *ap;
2027 struct fuse_file *ff;
2030 ff = fuse_write_file_get(fi);
2034 wpa = fuse_writepage_args_setup(folio, ff);
2037 goto err_writepage_args;
2042 folio_start_writeback(folio);
2043 fuse_writepage_args_page_fill(wpa, folio, 0);
2045 spin_lock(&fi->lock);
2046 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2047 fuse_flush_writepages(inode);
2048 spin_unlock(&fi->lock);
2053 fuse_file_put(ff, false);
2055 mapping_set_error(folio->mapping, error);
2059 struct fuse_fill_wb_data {
2060 struct fuse_writepage_args *wpa;
2061 struct fuse_file *ff;
2062 struct inode *inode;
2063 unsigned int max_folios;
2064 unsigned int nr_pages;
2067 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
2069 struct fuse_args_pages *ap = &data->wpa->ia.ap;
2070 struct fuse_conn *fc = get_fuse_conn(data->inode);
2071 struct folio **folios;
2072 struct fuse_folio_desc *descs;
2073 unsigned int nfolios = min_t(unsigned int,
2074 max_t(unsigned int, data->max_folios * 2,
2075 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
2077 WARN_ON(nfolios <= data->max_folios);
2079 folios = fuse_folios_alloc(nfolios, GFP_NOFS, &descs);
2083 memcpy(folios, ap->folios, sizeof(struct folio *) * ap->num_folios);
2084 memcpy(descs, ap->descs, sizeof(struct fuse_folio_desc) * ap->num_folios);
2086 ap->folios = folios;
2088 data->max_folios = nfolios;
2093 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2095 struct fuse_writepage_args *wpa = data->wpa;
2096 struct inode *inode = data->inode;
2097 struct fuse_inode *fi = get_fuse_inode(inode);
2099 spin_lock(&fi->lock);
2100 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2101 fuse_flush_writepages(inode);
2102 spin_unlock(&fi->lock);
2105 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct folio *folio,
2106 struct fuse_args_pages *ap,
2107 struct fuse_fill_wb_data *data)
2109 WARN_ON(!ap->num_folios);
2111 /* Reached max pages */
2112 if (data->nr_pages + folio_nr_pages(folio) > fc->max_pages)
2115 /* Reached max write bytes */
2116 if ((data->nr_pages * PAGE_SIZE) + folio_size(folio) > fc->max_write)
2120 if (folio_next_index(ap->folios[ap->num_folios - 1]) != folio->index)
2123 /* Need to grow the pages array? If so, did the expansion fail? */
2124 if (ap->num_folios == data->max_folios && !fuse_pages_realloc(data))
2130 static int fuse_writepages_fill(struct folio *folio,
2131 struct writeback_control *wbc, void *_data)
2133 struct fuse_fill_wb_data *data = _data;
2134 struct fuse_writepage_args *wpa = data->wpa;
2135 struct fuse_args_pages *ap = &wpa->ia.ap;
2136 struct inode *inode = data->inode;
2137 struct fuse_inode *fi = get_fuse_inode(inode);
2138 struct fuse_conn *fc = get_fuse_conn(inode);
2143 data->ff = fuse_write_file_get(fi);
2148 if (wpa && fuse_writepage_need_send(fc, folio, ap, data)) {
2149 fuse_writepages_send(data);
2154 if (data->wpa == NULL) {
2156 wpa = fuse_writepage_args_setup(folio, data->ff);
2159 fuse_file_get(wpa->ia.ff);
2160 data->max_folios = 1;
2163 folio_start_writeback(folio);
2165 fuse_writepage_args_page_fill(wpa, folio, ap->num_folios);
2166 data->nr_pages += folio_nr_pages(folio);
2173 folio_unlock(folio);
2178 static int fuse_writepages(struct address_space *mapping,
2179 struct writeback_control *wbc)
2181 struct inode *inode = mapping->host;
2182 struct fuse_conn *fc = get_fuse_conn(inode);
2183 struct fuse_fill_wb_data data;
2187 if (fuse_is_bad(inode))
2190 if (wbc->sync_mode == WB_SYNC_NONE &&
2191 fc->num_background >= fc->congestion_threshold)
2199 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2201 WARN_ON(!data.wpa->ia.ap.num_folios);
2202 fuse_writepages_send(&data);
2205 fuse_file_put(data.ff, false);
2212 * It's worthy to make sure that space is reserved on disk for the write,
2213 * but how to implement it without killing performance need more thinking.
2215 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2216 loff_t pos, unsigned len, struct folio **foliop, void **fsdata)
2218 pgoff_t index = pos >> PAGE_SHIFT;
2219 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2220 struct folio *folio;
2224 WARN_ON(!fc->writeback_cache);
2226 folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
2227 mapping_gfp_mask(mapping));
2231 if (folio_test_uptodate(folio) || len >= folio_size(folio))
2234 * Check if the start of this folio comes after the end of file,
2235 * in which case the readpage can be optimized away.
2237 fsize = i_size_read(mapping->host);
2238 if (fsize <= folio_pos(folio)) {
2239 size_t off = offset_in_folio(folio, pos);
2241 folio_zero_segment(folio, 0, off);
2244 err = fuse_do_readfolio(file, folio);
2252 folio_unlock(folio);
2258 static int fuse_write_end(struct file *file, struct address_space *mapping,
2259 loff_t pos, unsigned len, unsigned copied,
2260 struct folio *folio, void *fsdata)
2262 struct inode *inode = folio->mapping->host;
2264 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2269 if (!folio_test_uptodate(folio)) {
2270 /* Zero any unwritten bytes at the end of the page */
2271 size_t endoff = pos & ~PAGE_MASK;
2273 folio_zero_segment(folio, endoff, PAGE_SIZE);
2274 folio_mark_uptodate(folio);
2277 if (pos > inode->i_size)
2278 i_size_write(inode, pos);
2280 folio_mark_dirty(folio);
2283 folio_unlock(folio);
2289 static int fuse_launder_folio(struct folio *folio)
2292 if (folio_clear_dirty_for_io(folio)) {
2293 err = fuse_writepage_locked(folio);
2295 folio_wait_writeback(folio);
2301 * Write back dirty data/metadata now (there may not be any suitable
2302 * open files later for data)
2304 static void fuse_vma_close(struct vm_area_struct *vma)
2308 err = write_inode_now(vma->vm_file->f_mapping->host, 1);
2309 mapping_set_error(vma->vm_file->f_mapping, err);
2313 * Wait for writeback against this page to complete before allowing it
2314 * to be marked dirty again, and hence written back again, possibly
2315 * before the previous writepage completed.
2317 * Block here, instead of in ->writepage(), so that the userspace fs
2318 * can only block processes actually operating on the filesystem.
2320 * Otherwise unprivileged userspace fs would be able to block
2325 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2327 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2329 struct folio *folio = page_folio(vmf->page);
2330 struct inode *inode = file_inode(vmf->vma->vm_file);
2332 file_update_time(vmf->vma->vm_file);
2334 if (folio->mapping != inode->i_mapping) {
2335 folio_unlock(folio);
2336 return VM_FAULT_NOPAGE;
2339 folio_wait_writeback(folio);
2340 return VM_FAULT_LOCKED;
2343 static const struct vm_operations_struct fuse_file_vm_ops = {
2344 .close = fuse_vma_close,
2345 .fault = filemap_fault,
2346 .map_pages = filemap_map_pages,
2347 .page_mkwrite = fuse_page_mkwrite,
2350 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2352 struct fuse_file *ff = file->private_data;
2353 struct fuse_conn *fc = ff->fm->fc;
2354 struct inode *inode = file_inode(file);
2357 /* DAX mmap is superior to direct_io mmap */
2358 if (FUSE_IS_DAX(inode))
2359 return fuse_dax_mmap(file, vma);
2362 * If inode is in passthrough io mode, because it has some file open
2363 * in passthrough mode, either mmap to backing file or fail mmap,
2364 * because mixing cached mmap and passthrough io mode is not allowed.
2366 if (fuse_file_passthrough(ff))
2367 return fuse_passthrough_mmap(file, vma);
2368 else if (fuse_inode_backing(get_fuse_inode(inode)))
2372 * FOPEN_DIRECT_IO handling is special compared to O_DIRECT,
2373 * as does not allow MAP_SHARED mmap without FUSE_DIRECT_IO_ALLOW_MMAP.
2375 if (ff->open_flags & FOPEN_DIRECT_IO) {
2377 * Can't provide the coherency needed for MAP_SHARED
2378 * if FUSE_DIRECT_IO_ALLOW_MMAP isn't set.
2380 if ((vma->vm_flags & VM_MAYSHARE) && !fc->direct_io_allow_mmap)
2383 invalidate_inode_pages2(file->f_mapping);
2385 if (!(vma->vm_flags & VM_MAYSHARE)) {
2387 return generic_file_mmap(file, vma);
2391 * First mmap of direct_io file enters caching inode io mode.
2392 * Also waits for parallel dio writers to go into serial mode
2393 * (exclusive instead of shared lock).
2394 * After first mmap, the inode stays in caching io mode until
2395 * the direct_io file release.
2397 rc = fuse_file_cached_io_open(inode, ff);
2402 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2403 fuse_link_write_file(file);
2405 file_accessed(file);
2406 vma->vm_ops = &fuse_file_vm_ops;
2410 static int convert_fuse_file_lock(struct fuse_conn *fc,
2411 const struct fuse_file_lock *ffl,
2412 struct file_lock *fl)
2414 switch (ffl->type) {
2420 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2421 ffl->end < ffl->start)
2424 fl->fl_start = ffl->start;
2425 fl->fl_end = ffl->end;
2428 * Convert pid into init's pid namespace. The locks API will
2429 * translate it into the caller's pid namespace.
2432 fl->c.flc_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2439 fl->c.flc_type = ffl->type;
2443 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2444 const struct file_lock *fl, int opcode, pid_t pid,
2445 int flock, struct fuse_lk_in *inarg)
2447 struct inode *inode = file_inode(file);
2448 struct fuse_conn *fc = get_fuse_conn(inode);
2449 struct fuse_file *ff = file->private_data;
2451 memset(inarg, 0, sizeof(*inarg));
2453 inarg->owner = fuse_lock_owner_id(fc, fl->c.flc_owner);
2454 inarg->lk.start = fl->fl_start;
2455 inarg->lk.end = fl->fl_end;
2456 inarg->lk.type = fl->c.flc_type;
2457 inarg->lk.pid = pid;
2459 inarg->lk_flags |= FUSE_LK_FLOCK;
2460 args->opcode = opcode;
2461 args->nodeid = get_node_id(inode);
2462 args->in_numargs = 1;
2463 args->in_args[0].size = sizeof(*inarg);
2464 args->in_args[0].value = inarg;
2467 static int fuse_getlk(struct file *file, struct file_lock *fl)
2469 struct inode *inode = file_inode(file);
2470 struct fuse_mount *fm = get_fuse_mount(inode);
2472 struct fuse_lk_in inarg;
2473 struct fuse_lk_out outarg;
2476 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2477 args.out_numargs = 1;
2478 args.out_args[0].size = sizeof(outarg);
2479 args.out_args[0].value = &outarg;
2480 err = fuse_simple_request(fm, &args);
2482 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2487 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2489 struct inode *inode = file_inode(file);
2490 struct fuse_mount *fm = get_fuse_mount(inode);
2492 struct fuse_lk_in inarg;
2493 int opcode = (fl->c.flc_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2494 struct pid *pid = fl->c.flc_type != F_UNLCK ? task_tgid(current) : NULL;
2495 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2498 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2499 /* NLM needs asynchronous locks, which we don't support yet */
2503 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2504 err = fuse_simple_request(fm, &args);
2506 /* locking is restartable */
2513 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2515 struct inode *inode = file_inode(file);
2516 struct fuse_conn *fc = get_fuse_conn(inode);
2519 if (cmd == F_CANCELLK) {
2521 } else if (cmd == F_GETLK) {
2523 posix_test_lock(file, fl);
2526 err = fuse_getlk(file, fl);
2529 err = posix_lock_file(file, fl, NULL);
2531 err = fuse_setlk(file, fl, 0);
2536 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2538 struct inode *inode = file_inode(file);
2539 struct fuse_conn *fc = get_fuse_conn(inode);
2543 err = locks_lock_file_wait(file, fl);
2545 struct fuse_file *ff = file->private_data;
2547 /* emulate flock with POSIX locks */
2549 err = fuse_setlk(file, fl, 1);
2555 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2557 struct inode *inode = mapping->host;
2558 struct fuse_mount *fm = get_fuse_mount(inode);
2560 struct fuse_bmap_in inarg;
2561 struct fuse_bmap_out outarg;
2564 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2567 memset(&inarg, 0, sizeof(inarg));
2568 inarg.block = block;
2569 inarg.blocksize = inode->i_sb->s_blocksize;
2570 args.opcode = FUSE_BMAP;
2571 args.nodeid = get_node_id(inode);
2572 args.in_numargs = 1;
2573 args.in_args[0].size = sizeof(inarg);
2574 args.in_args[0].value = &inarg;
2575 args.out_numargs = 1;
2576 args.out_args[0].size = sizeof(outarg);
2577 args.out_args[0].value = &outarg;
2578 err = fuse_simple_request(fm, &args);
2580 fm->fc->no_bmap = 1;
2582 return err ? 0 : outarg.block;
2585 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2587 struct inode *inode = file->f_mapping->host;
2588 struct fuse_mount *fm = get_fuse_mount(inode);
2589 struct fuse_file *ff = file->private_data;
2591 struct fuse_lseek_in inarg = {
2596 struct fuse_lseek_out outarg;
2599 if (fm->fc->no_lseek)
2602 args.opcode = FUSE_LSEEK;
2603 args.nodeid = ff->nodeid;
2604 args.in_numargs = 1;
2605 args.in_args[0].size = sizeof(inarg);
2606 args.in_args[0].value = &inarg;
2607 args.out_numargs = 1;
2608 args.out_args[0].size = sizeof(outarg);
2609 args.out_args[0].value = &outarg;
2610 err = fuse_simple_request(fm, &args);
2612 if (err == -ENOSYS) {
2613 fm->fc->no_lseek = 1;
2619 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2622 err = fuse_update_attributes(inode, file, STATX_SIZE);
2624 return generic_file_llseek(file, offset, whence);
2629 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2632 struct inode *inode = file_inode(file);
2637 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2638 retval = generic_file_llseek(file, offset, whence);
2642 retval = fuse_update_attributes(inode, file, STATX_SIZE);
2644 retval = generic_file_llseek(file, offset, whence);
2645 inode_unlock(inode);
2650 retval = fuse_lseek(file, offset, whence);
2651 inode_unlock(inode);
2661 * All files which have been polled are linked to RB tree
2662 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2663 * find the matching one.
2665 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2666 struct rb_node **parent_out)
2668 struct rb_node **link = &fc->polled_files.rb_node;
2669 struct rb_node *last = NULL;
2672 struct fuse_file *ff;
2675 ff = rb_entry(last, struct fuse_file, polled_node);
2678 link = &last->rb_left;
2679 else if (kh > ff->kh)
2680 link = &last->rb_right;
2691 * The file is about to be polled. Make sure it's on the polled_files
2692 * RB tree. Note that files once added to the polled_files tree are
2693 * not removed before the file is released. This is because a file
2694 * polled once is likely to be polled again.
2696 static void fuse_register_polled_file(struct fuse_conn *fc,
2697 struct fuse_file *ff)
2699 spin_lock(&fc->lock);
2700 if (RB_EMPTY_NODE(&ff->polled_node)) {
2701 struct rb_node **link, *parent;
2703 link = fuse_find_polled_node(fc, ff->kh, &parent);
2705 rb_link_node(&ff->polled_node, parent, link);
2706 rb_insert_color(&ff->polled_node, &fc->polled_files);
2708 spin_unlock(&fc->lock);
2711 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2713 struct fuse_file *ff = file->private_data;
2714 struct fuse_mount *fm = ff->fm;
2715 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2716 struct fuse_poll_out outarg;
2720 if (fm->fc->no_poll)
2721 return DEFAULT_POLLMASK;
2723 poll_wait(file, &ff->poll_wait, wait);
2724 inarg.events = mangle_poll(poll_requested_events(wait));
2727 * Ask for notification iff there's someone waiting for it.
2728 * The client may ignore the flag and always notify.
2730 if (waitqueue_active(&ff->poll_wait)) {
2731 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2732 fuse_register_polled_file(fm->fc, ff);
2735 args.opcode = FUSE_POLL;
2736 args.nodeid = ff->nodeid;
2737 args.in_numargs = 1;
2738 args.in_args[0].size = sizeof(inarg);
2739 args.in_args[0].value = &inarg;
2740 args.out_numargs = 1;
2741 args.out_args[0].size = sizeof(outarg);
2742 args.out_args[0].value = &outarg;
2743 err = fuse_simple_request(fm, &args);
2746 return demangle_poll(outarg.revents);
2747 if (err == -ENOSYS) {
2748 fm->fc->no_poll = 1;
2749 return DEFAULT_POLLMASK;
2753 EXPORT_SYMBOL_GPL(fuse_file_poll);
2756 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2757 * wakes up the poll waiters.
2759 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2760 struct fuse_notify_poll_wakeup_out *outarg)
2762 u64 kh = outarg->kh;
2763 struct rb_node **link;
2765 spin_lock(&fc->lock);
2767 link = fuse_find_polled_node(fc, kh, NULL);
2769 struct fuse_file *ff;
2771 ff = rb_entry(*link, struct fuse_file, polled_node);
2772 wake_up_interruptible_sync(&ff->poll_wait);
2775 spin_unlock(&fc->lock);
2779 static void fuse_do_truncate(struct file *file)
2781 struct inode *inode = file->f_mapping->host;
2784 attr.ia_valid = ATTR_SIZE;
2785 attr.ia_size = i_size_read(inode);
2787 attr.ia_file = file;
2788 attr.ia_valid |= ATTR_FILE;
2790 fuse_do_setattr(file_mnt_idmap(file), file_dentry(file), &attr, file);
2793 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2795 return round_up(off, fc->max_pages << PAGE_SHIFT);
2799 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2801 DECLARE_COMPLETION_ONSTACK(wait);
2803 struct file *file = iocb->ki_filp;
2804 struct fuse_file *ff = file->private_data;
2806 struct inode *inode;
2808 size_t count = iov_iter_count(iter), shortened = 0;
2809 loff_t offset = iocb->ki_pos;
2810 struct fuse_io_priv *io;
2813 inode = file->f_mapping->host;
2814 i_size = i_size_read(inode);
2816 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
2819 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2822 spin_lock_init(&io->lock);
2823 kref_init(&io->refcnt);
2827 io->offset = offset;
2828 io->write = (iov_iter_rw(iter) == WRITE);
2831 * By default, we want to optimize all I/Os with async request
2832 * submission to the client filesystem if supported.
2834 io->async = ff->fm->fc->async_dio;
2836 io->blocking = is_sync_kiocb(iocb);
2838 /* optimization for short read */
2839 if (io->async && !io->write && offset + count > i_size) {
2840 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
2841 shortened = count - iov_iter_count(iter);
2846 * We cannot asynchronously extend the size of a file.
2847 * In such case the aio will behave exactly like sync io.
2849 if ((offset + count > i_size) && io->write)
2850 io->blocking = true;
2852 if (io->async && io->blocking) {
2854 * Additional reference to keep io around after
2855 * calling fuse_aio_complete()
2857 kref_get(&io->refcnt);
2861 if (iov_iter_rw(iter) == WRITE) {
2862 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2863 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
2865 ret = __fuse_direct_read(io, iter, &pos);
2867 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
2870 bool blocking = io->blocking;
2872 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2874 /* we have a non-extending, async request, so return */
2876 return -EIOCBQUEUED;
2878 wait_for_completion(&wait);
2879 ret = fuse_get_res_by_io(io);
2882 kref_put(&io->refcnt, fuse_io_release);
2884 if (iov_iter_rw(iter) == WRITE) {
2885 fuse_write_update_attr(inode, pos, ret);
2886 /* For extending writes we already hold exclusive lock */
2887 if (ret < 0 && offset + count > i_size)
2888 fuse_do_truncate(file);
2894 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
2896 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
2899 fuse_sync_writes(inode);
2904 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2907 struct fuse_file *ff = file->private_data;
2908 struct inode *inode = file_inode(file);
2909 struct fuse_inode *fi = get_fuse_inode(inode);
2910 struct fuse_mount *fm = ff->fm;
2912 struct fuse_fallocate_in inarg = {
2919 bool block_faults = FUSE_IS_DAX(inode) &&
2920 (!(mode & FALLOC_FL_KEEP_SIZE) ||
2921 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)));
2923 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
2924 FALLOC_FL_ZERO_RANGE))
2927 if (fm->fc->no_fallocate)
2932 filemap_invalidate_lock(inode->i_mapping);
2933 err = fuse_dax_break_layouts(inode, 0, -1);
2938 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
2939 loff_t endbyte = offset + length - 1;
2941 err = fuse_writeback_range(inode, offset, endbyte);
2946 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2947 offset + length > i_size_read(inode)) {
2948 err = inode_newsize_ok(inode, offset + length);
2953 err = file_modified(file);
2957 if (!(mode & FALLOC_FL_KEEP_SIZE))
2958 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2960 args.opcode = FUSE_FALLOCATE;
2961 args.nodeid = ff->nodeid;
2962 args.in_numargs = 1;
2963 args.in_args[0].size = sizeof(inarg);
2964 args.in_args[0].value = &inarg;
2965 err = fuse_simple_request(fm, &args);
2966 if (err == -ENOSYS) {
2967 fm->fc->no_fallocate = 1;
2973 /* we could have extended the file */
2974 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2975 if (fuse_write_update_attr(inode, offset + length, length))
2976 file_update_time(file);
2979 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
2980 truncate_pagecache_range(inode, offset, offset + length - 1);
2982 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
2985 if (!(mode & FALLOC_FL_KEEP_SIZE))
2986 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2989 filemap_invalidate_unlock(inode->i_mapping);
2991 inode_unlock(inode);
2993 fuse_flush_time_update(inode);
2998 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
2999 struct file *file_out, loff_t pos_out,
3000 size_t len, unsigned int flags)
3002 struct fuse_file *ff_in = file_in->private_data;
3003 struct fuse_file *ff_out = file_out->private_data;
3004 struct inode *inode_in = file_inode(file_in);
3005 struct inode *inode_out = file_inode(file_out);
3006 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3007 struct fuse_mount *fm = ff_in->fm;
3008 struct fuse_conn *fc = fm->fc;
3010 struct fuse_copy_file_range_in inarg = {
3013 .nodeid_out = ff_out->nodeid,
3014 .fh_out = ff_out->fh,
3019 struct fuse_write_out outarg;
3021 /* mark unstable when write-back is not used, and file_out gets
3023 bool is_unstable = (!fc->writeback_cache) &&
3024 ((pos_out + len) > inode_out->i_size);
3026 if (fc->no_copy_file_range)
3029 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3032 inode_lock(inode_in);
3033 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3034 inode_unlock(inode_in);
3038 inode_lock(inode_out);
3040 err = file_modified(file_out);
3045 * Write out dirty pages in the destination file before sending the COPY
3046 * request to userspace. After the request is completed, truncate off
3047 * pages (including partial ones) from the cache that have been copied,
3048 * since these contain stale data at that point.
3050 * This should be mostly correct, but if the COPY writes to partial
3051 * pages (at the start or end) and the parts not covered by the COPY are
3052 * written through a memory map after calling fuse_writeback_range(),
3053 * then these partial page modifications will be lost on truncation.
3055 * It is unlikely that someone would rely on such mixed style
3056 * modifications. Yet this does give less guarantees than if the
3057 * copying was performed with write(2).
3059 * To fix this a mapping->invalidate_lock could be used to prevent new
3060 * faults while the copy is ongoing.
3062 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3067 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3069 args.opcode = FUSE_COPY_FILE_RANGE;
3070 args.nodeid = ff_in->nodeid;
3071 args.in_numargs = 1;
3072 args.in_args[0].size = sizeof(inarg);
3073 args.in_args[0].value = &inarg;
3074 args.out_numargs = 1;
3075 args.out_args[0].size = sizeof(outarg);
3076 args.out_args[0].value = &outarg;
3077 err = fuse_simple_request(fm, &args);
3078 if (err == -ENOSYS) {
3079 fc->no_copy_file_range = 1;
3085 truncate_inode_pages_range(inode_out->i_mapping,
3086 ALIGN_DOWN(pos_out, PAGE_SIZE),
3087 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3089 file_update_time(file_out);
3090 fuse_write_update_attr(inode_out, pos_out + outarg.size, outarg.size);
3095 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3097 inode_unlock(inode_out);
3098 file_accessed(file_in);
3100 fuse_flush_time_update(inode_out);
3105 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3106 struct file *dst_file, loff_t dst_off,
3107 size_t len, unsigned int flags)
3111 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3114 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3115 ret = splice_copy_file_range(src_file, src_off, dst_file,
3120 static const struct file_operations fuse_file_operations = {
3121 .llseek = fuse_file_llseek,
3122 .read_iter = fuse_file_read_iter,
3123 .write_iter = fuse_file_write_iter,
3124 .mmap = fuse_file_mmap,
3126 .flush = fuse_flush,
3127 .release = fuse_release,
3128 .fsync = fuse_fsync,
3129 .lock = fuse_file_lock,
3130 .get_unmapped_area = thp_get_unmapped_area,
3131 .flock = fuse_file_flock,
3132 .splice_read = fuse_splice_read,
3133 .splice_write = fuse_splice_write,
3134 .unlocked_ioctl = fuse_file_ioctl,
3135 .compat_ioctl = fuse_file_compat_ioctl,
3136 .poll = fuse_file_poll,
3137 .fallocate = fuse_file_fallocate,
3138 .copy_file_range = fuse_copy_file_range,
3141 static const struct address_space_operations fuse_file_aops = {
3142 .read_folio = fuse_read_folio,
3143 .readahead = fuse_readahead,
3144 .writepages = fuse_writepages,
3145 .launder_folio = fuse_launder_folio,
3146 .dirty_folio = filemap_dirty_folio,
3147 .migrate_folio = filemap_migrate_folio,
3149 .direct_IO = fuse_direct_IO,
3150 .write_begin = fuse_write_begin,
3151 .write_end = fuse_write_end,
3154 void fuse_init_file_inode(struct inode *inode, unsigned int flags)
3156 struct fuse_inode *fi = get_fuse_inode(inode);
3157 struct fuse_conn *fc = get_fuse_conn(inode);
3159 inode->i_fop = &fuse_file_operations;
3160 inode->i_data.a_ops = &fuse_file_aops;
3161 if (fc->writeback_cache)
3162 mapping_set_writeback_may_deadlock_on_reclaim(&inode->i_data);
3164 INIT_LIST_HEAD(&fi->write_files);
3165 INIT_LIST_HEAD(&fi->queued_writes);
3168 init_waitqueue_head(&fi->page_waitq);
3169 init_waitqueue_head(&fi->direct_io_waitq);
3171 if (IS_ENABLED(CONFIG_FUSE_DAX))
3172 fuse_dax_inode_init(inode, flags);