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>
23 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid,
24 unsigned int open_flags, int opcode,
25 struct fuse_open_out *outargp)
27 struct fuse_open_in inarg;
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = open_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fm->fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
35 if (fm->fc->handle_killpriv_v2 &&
36 (inarg.flags & O_TRUNC) && !capable(CAP_FSETID)) {
37 inarg.open_flags |= FUSE_OPEN_KILL_SUIDGID;
43 args.in_args[0].size = sizeof(inarg);
44 args.in_args[0].value = &inarg;
46 args.out_args[0].size = sizeof(*outargp);
47 args.out_args[0].value = outargp;
49 return fuse_simple_request(fm, &args);
52 struct fuse_release_args {
53 struct fuse_args args;
54 struct fuse_release_in inarg;
58 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
62 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
67 ff->release_args = kzalloc(sizeof(*ff->release_args),
69 if (!ff->release_args) {
74 INIT_LIST_HEAD(&ff->write_entry);
75 mutex_init(&ff->readdir.lock);
76 refcount_set(&ff->count, 1);
77 RB_CLEAR_NODE(&ff->polled_node);
78 init_waitqueue_head(&ff->poll_wait);
80 ff->kh = atomic64_inc_return(&fm->fc->khctr);
85 void fuse_file_free(struct fuse_file *ff)
87 kfree(ff->release_args);
88 mutex_destroy(&ff->readdir.lock);
92 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
94 refcount_inc(&ff->count);
98 static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
101 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
107 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
109 if (refcount_dec_and_test(&ff->count)) {
110 struct fuse_args *args = &ff->release_args->args;
112 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
113 /* Do nothing when client does not implement 'open' */
114 fuse_release_end(ff->fm, args, 0);
116 fuse_simple_request(ff->fm, args);
117 fuse_release_end(ff->fm, args, 0);
119 args->end = fuse_release_end;
120 if (fuse_simple_background(ff->fm, args,
121 GFP_KERNEL | __GFP_NOFAIL))
122 fuse_release_end(ff->fm, args, -ENOTCONN);
128 struct fuse_file *fuse_file_open(struct fuse_mount *fm, u64 nodeid,
129 unsigned int open_flags, bool isdir)
131 struct fuse_conn *fc = fm->fc;
132 struct fuse_file *ff;
133 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
135 ff = fuse_file_alloc(fm);
137 return ERR_PTR(-ENOMEM);
140 /* Default for no-open */
141 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
142 if (isdir ? !fc->no_opendir : !fc->no_open) {
143 struct fuse_open_out outarg;
146 err = fuse_send_open(fm, nodeid, open_flags, opcode, &outarg);
149 ff->open_flags = outarg.open_flags;
151 } else if (err != -ENOSYS) {
163 ff->open_flags &= ~FOPEN_DIRECT_IO;
170 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
173 struct fuse_file *ff = fuse_file_open(fm, nodeid, file->f_flags, isdir);
176 file->private_data = ff;
178 return PTR_ERR_OR_ZERO(ff);
180 EXPORT_SYMBOL_GPL(fuse_do_open);
182 static void fuse_link_write_file(struct file *file)
184 struct inode *inode = file_inode(file);
185 struct fuse_inode *fi = get_fuse_inode(inode);
186 struct fuse_file *ff = file->private_data;
188 * file may be written through mmap, so chain it onto the
189 * inodes's write_file list
191 spin_lock(&fi->lock);
192 if (list_empty(&ff->write_entry))
193 list_add(&ff->write_entry, &fi->write_files);
194 spin_unlock(&fi->lock);
197 void fuse_finish_open(struct inode *inode, struct file *file)
199 struct fuse_file *ff = file->private_data;
200 struct fuse_conn *fc = get_fuse_conn(inode);
202 if (ff->open_flags & FOPEN_STREAM)
203 stream_open(inode, file);
204 else if (ff->open_flags & FOPEN_NONSEEKABLE)
205 nonseekable_open(inode, file);
207 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
208 struct fuse_inode *fi = get_fuse_inode(inode);
210 spin_lock(&fi->lock);
211 fi->attr_version = atomic64_inc_return(&fc->attr_version);
212 i_size_write(inode, 0);
213 spin_unlock(&fi->lock);
214 file_update_time(file);
215 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
217 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
218 fuse_link_write_file(file);
221 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
223 struct fuse_mount *fm = get_fuse_mount(inode);
224 struct fuse_conn *fc = fm->fc;
226 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
227 fc->atomic_o_trunc &&
229 bool dax_truncate = (file->f_flags & O_TRUNC) &&
230 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
232 if (fuse_is_bad(inode))
235 err = generic_file_open(inode, file);
239 if (is_wb_truncate || dax_truncate)
243 filemap_invalidate_lock(inode->i_mapping);
244 err = fuse_dax_break_layouts(inode, 0, 0);
246 goto out_inode_unlock;
249 if (is_wb_truncate || dax_truncate)
250 fuse_set_nowrite(inode);
252 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
254 fuse_finish_open(inode, file);
256 if (is_wb_truncate || dax_truncate)
257 fuse_release_nowrite(inode);
259 struct fuse_file *ff = file->private_data;
261 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
262 truncate_pagecache(inode, 0);
263 else if (!(ff->open_flags & FOPEN_KEEP_CACHE))
264 invalidate_inode_pages2(inode->i_mapping);
267 filemap_invalidate_unlock(inode->i_mapping);
269 if (is_wb_truncate || dax_truncate)
275 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
276 unsigned int flags, int opcode)
278 struct fuse_conn *fc = ff->fm->fc;
279 struct fuse_release_args *ra = ff->release_args;
281 /* Inode is NULL on error path of fuse_create_open() */
283 spin_lock(&fi->lock);
284 list_del(&ff->write_entry);
285 spin_unlock(&fi->lock);
287 spin_lock(&fc->lock);
288 if (!RB_EMPTY_NODE(&ff->polled_node))
289 rb_erase(&ff->polled_node, &fc->polled_files);
290 spin_unlock(&fc->lock);
292 wake_up_interruptible_all(&ff->poll_wait);
294 ra->inarg.fh = ff->fh;
295 ra->inarg.flags = flags;
296 ra->args.in_numargs = 1;
297 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
298 ra->args.in_args[0].value = &ra->inarg;
299 ra->args.opcode = opcode;
300 ra->args.nodeid = ff->nodeid;
301 ra->args.force = true;
302 ra->args.nocreds = true;
305 void fuse_file_release(struct inode *inode, struct fuse_file *ff,
306 unsigned int open_flags, fl_owner_t id, bool isdir)
308 struct fuse_inode *fi = get_fuse_inode(inode);
309 struct fuse_release_args *ra = ff->release_args;
310 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
312 fuse_prepare_release(fi, ff, open_flags, opcode);
315 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
316 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc, id);
318 /* Hold inode until release is finished */
319 ra->inode = igrab(inode);
322 * Normally this will send the RELEASE request, however if
323 * some asynchronous READ or WRITE requests are outstanding,
324 * the sending will be delayed.
326 * Make the release synchronous if this is a fuseblk mount,
327 * synchronous RELEASE is allowed (and desirable) in this case
328 * because the server can be trusted not to screw up.
330 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
333 void fuse_release_common(struct file *file, bool isdir)
335 fuse_file_release(file_inode(file), file->private_data, file->f_flags,
336 (fl_owner_t) file, isdir);
339 static int fuse_open(struct inode *inode, struct file *file)
341 return fuse_open_common(inode, file, false);
344 static int fuse_release(struct inode *inode, struct file *file)
346 struct fuse_conn *fc = get_fuse_conn(inode);
349 * Dirty pages might remain despite write_inode_now() call from
350 * fuse_flush() due to writes racing with the close.
352 if (fc->writeback_cache)
353 write_inode_now(inode, 1);
355 fuse_release_common(file, false);
357 /* return value is ignored by VFS */
361 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff,
364 WARN_ON(refcount_read(&ff->count) > 1);
365 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
367 * iput(NULL) is a no-op and since the refcount is 1 and everything's
368 * synchronous, we are fine with not doing igrab() here"
370 fuse_file_put(ff, true, false);
372 EXPORT_SYMBOL_GPL(fuse_sync_release);
375 * Scramble the ID space with XTEA, so that the value of the files_struct
376 * pointer is not exposed to userspace.
378 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
380 u32 *k = fc->scramble_key;
381 u64 v = (unsigned long) id;
387 for (i = 0; i < 32; i++) {
388 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
390 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
393 return (u64) v0 + ((u64) v1 << 32);
396 struct fuse_writepage_args {
397 struct fuse_io_args ia;
398 struct rb_node writepages_entry;
399 struct list_head queue_entry;
400 struct fuse_writepage_args *next;
402 struct fuse_sync_bucket *bucket;
405 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
406 pgoff_t idx_from, pgoff_t idx_to)
410 n = fi->writepages.rb_node;
413 struct fuse_writepage_args *wpa;
416 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
417 WARN_ON(get_fuse_inode(wpa->inode) != fi);
418 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
419 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
421 else if (idx_to < curr_index)
430 * Check if any page in a range is under writeback
432 * This is currently done by walking the list of writepage requests
433 * for the inode, which can be pretty inefficient.
435 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
438 struct fuse_inode *fi = get_fuse_inode(inode);
441 spin_lock(&fi->lock);
442 found = fuse_find_writeback(fi, idx_from, idx_to);
443 spin_unlock(&fi->lock);
448 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
450 return fuse_range_is_writeback(inode, index, index);
454 * Wait for page writeback to be completed.
456 * Since fuse doesn't rely on the VM writeback tracking, this has to
457 * use some other means.
459 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
461 struct fuse_inode *fi = get_fuse_inode(inode);
463 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
467 * Wait for all pending writepages on the inode to finish.
469 * This is currently done by blocking further writes with FUSE_NOWRITE
470 * and waiting for all sent writes to complete.
472 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
473 * could conflict with truncation.
475 static void fuse_sync_writes(struct inode *inode)
477 fuse_set_nowrite(inode);
478 fuse_release_nowrite(inode);
481 static int fuse_flush(struct file *file, fl_owner_t id)
483 struct inode *inode = file_inode(file);
484 struct fuse_mount *fm = get_fuse_mount(inode);
485 struct fuse_file *ff = file->private_data;
486 struct fuse_flush_in inarg;
490 if (fuse_is_bad(inode))
493 if (ff->open_flags & FOPEN_NOFLUSH && !fm->fc->writeback_cache)
496 err = write_inode_now(inode, 1);
501 fuse_sync_writes(inode);
504 err = filemap_check_errors(file->f_mapping);
509 if (fm->fc->no_flush)
512 memset(&inarg, 0, sizeof(inarg));
514 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
515 args.opcode = FUSE_FLUSH;
516 args.nodeid = get_node_id(inode);
518 args.in_args[0].size = sizeof(inarg);
519 args.in_args[0].value = &inarg;
522 err = fuse_simple_request(fm, &args);
523 if (err == -ENOSYS) {
524 fm->fc->no_flush = 1;
530 * In memory i_blocks is not maintained by fuse, if writeback cache is
531 * enabled, i_blocks from cached attr may not be accurate.
533 if (!err && fm->fc->writeback_cache)
534 fuse_invalidate_attr_mask(inode, STATX_BLOCKS);
538 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
539 int datasync, int opcode)
541 struct inode *inode = file->f_mapping->host;
542 struct fuse_mount *fm = get_fuse_mount(inode);
543 struct fuse_file *ff = file->private_data;
545 struct fuse_fsync_in inarg;
547 memset(&inarg, 0, sizeof(inarg));
549 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
550 args.opcode = opcode;
551 args.nodeid = get_node_id(inode);
553 args.in_args[0].size = sizeof(inarg);
554 args.in_args[0].value = &inarg;
555 return fuse_simple_request(fm, &args);
558 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
561 struct inode *inode = file->f_mapping->host;
562 struct fuse_conn *fc = get_fuse_conn(inode);
565 if (fuse_is_bad(inode))
571 * Start writeback against all dirty pages of the inode, then
572 * wait for all outstanding writes, before sending the FSYNC
575 err = file_write_and_wait_range(file, start, end);
579 fuse_sync_writes(inode);
582 * Due to implementation of fuse writeback
583 * file_write_and_wait_range() does not catch errors.
584 * We have to do this directly after fuse_sync_writes()
586 err = file_check_and_advance_wb_err(file);
590 err = sync_inode_metadata(inode, 1);
597 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
598 if (err == -ENOSYS) {
608 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
609 size_t count, int opcode)
611 struct fuse_file *ff = file->private_data;
612 struct fuse_args *args = &ia->ap.args;
614 ia->read.in.fh = ff->fh;
615 ia->read.in.offset = pos;
616 ia->read.in.size = count;
617 ia->read.in.flags = file->f_flags;
618 args->opcode = opcode;
619 args->nodeid = ff->nodeid;
620 args->in_numargs = 1;
621 args->in_args[0].size = sizeof(ia->read.in);
622 args->in_args[0].value = &ia->read.in;
623 args->out_argvar = true;
624 args->out_numargs = 1;
625 args->out_args[0].size = count;
628 static void fuse_release_user_pages(struct fuse_args_pages *ap,
633 for (i = 0; i < ap->num_pages; i++) {
635 set_page_dirty_lock(ap->pages[i]);
636 put_page(ap->pages[i]);
640 static void fuse_io_release(struct kref *kref)
642 kfree(container_of(kref, struct fuse_io_priv, refcnt));
645 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
650 if (io->bytes >= 0 && io->write)
653 return io->bytes < 0 ? io->size : io->bytes;
657 * In case of short read, the caller sets 'pos' to the position of
658 * actual end of fuse request in IO request. Otherwise, if bytes_requested
659 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
662 * User requested DIO read of 64K. It was split into two 32K fuse requests,
663 * both submitted asynchronously. The first of them was ACKed by userspace as
664 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
665 * second request was ACKed as short, e.g. only 1K was read, resulting in
668 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
669 * will be equal to the length of the longest contiguous fragment of
670 * transferred data starting from the beginning of IO request.
672 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
676 spin_lock(&io->lock);
678 io->err = io->err ? : err;
679 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
683 if (!left && io->blocking)
685 spin_unlock(&io->lock);
687 if (!left && !io->blocking) {
688 ssize_t res = fuse_get_res_by_io(io);
691 struct inode *inode = file_inode(io->iocb->ki_filp);
692 struct fuse_conn *fc = get_fuse_conn(inode);
693 struct fuse_inode *fi = get_fuse_inode(inode);
695 spin_lock(&fi->lock);
696 fi->attr_version = atomic64_inc_return(&fc->attr_version);
697 spin_unlock(&fi->lock);
700 io->iocb->ki_complete(io->iocb, res);
703 kref_put(&io->refcnt, fuse_io_release);
706 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
709 struct fuse_io_args *ia;
711 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
714 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
724 static void fuse_io_free(struct fuse_io_args *ia)
730 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
733 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
734 struct fuse_io_priv *io = ia->io;
737 fuse_release_user_pages(&ia->ap, io->should_dirty);
741 } else if (io->write) {
742 if (ia->write.out.size > ia->write.in.size) {
744 } else if (ia->write.in.size != ia->write.out.size) {
745 pos = ia->write.in.offset - io->offset +
749 u32 outsize = args->out_args[0].size;
751 if (ia->read.in.size != outsize)
752 pos = ia->read.in.offset - io->offset + outsize;
755 fuse_aio_complete(io, err, pos);
759 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
760 struct fuse_io_args *ia, size_t num_bytes)
763 struct fuse_io_priv *io = ia->io;
765 spin_lock(&io->lock);
766 kref_get(&io->refcnt);
767 io->size += num_bytes;
769 spin_unlock(&io->lock);
771 ia->ap.args.end = fuse_aio_complete_req;
772 ia->ap.args.may_block = io->should_dirty;
773 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
775 fuse_aio_complete_req(fm, &ia->ap.args, err);
780 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
783 struct file *file = ia->io->iocb->ki_filp;
784 struct fuse_file *ff = file->private_data;
785 struct fuse_mount *fm = ff->fm;
787 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
789 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
790 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
794 return fuse_async_req_send(fm, ia, count);
796 return fuse_simple_request(fm, &ia->ap.args);
799 static void fuse_read_update_size(struct inode *inode, loff_t size,
802 struct fuse_conn *fc = get_fuse_conn(inode);
803 struct fuse_inode *fi = get_fuse_inode(inode);
805 spin_lock(&fi->lock);
806 if (attr_ver >= fi->attr_version && size < inode->i_size &&
807 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
808 fi->attr_version = atomic64_inc_return(&fc->attr_version);
809 i_size_write(inode, size);
811 spin_unlock(&fi->lock);
814 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
815 struct fuse_args_pages *ap)
817 struct fuse_conn *fc = get_fuse_conn(inode);
820 * If writeback_cache is enabled, a short read means there's a hole in
821 * the file. Some data after the hole is in page cache, but has not
822 * reached the client fs yet. So the hole is not present there.
824 if (!fc->writeback_cache) {
825 loff_t pos = page_offset(ap->pages[0]) + num_read;
826 fuse_read_update_size(inode, pos, attr_ver);
830 static int fuse_do_readpage(struct file *file, struct page *page)
832 struct inode *inode = page->mapping->host;
833 struct fuse_mount *fm = get_fuse_mount(inode);
834 loff_t pos = page_offset(page);
835 struct fuse_page_desc desc = { .length = PAGE_SIZE };
836 struct fuse_io_args ia = {
837 .ap.args.page_zeroing = true,
838 .ap.args.out_pages = true,
847 * Page writeback can extend beyond the lifetime of the
848 * page-cache page, so make sure we read a properly synced
851 fuse_wait_on_page_writeback(inode, page->index);
853 attr_ver = fuse_get_attr_version(fm->fc);
855 /* Don't overflow end offset */
856 if (pos + (desc.length - 1) == LLONG_MAX)
859 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
860 res = fuse_simple_request(fm, &ia.ap.args);
864 * Short read means EOF. If file size is larger, truncate it
866 if (res < desc.length)
867 fuse_short_read(inode, attr_ver, res, &ia.ap);
869 SetPageUptodate(page);
874 static int fuse_read_folio(struct file *file, struct folio *folio)
876 struct page *page = &folio->page;
877 struct inode *inode = page->mapping->host;
881 if (fuse_is_bad(inode))
884 err = fuse_do_readpage(file, page);
885 fuse_invalidate_atime(inode);
891 static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
895 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
896 struct fuse_args_pages *ap = &ia->ap;
897 size_t count = ia->read.in.size;
898 size_t num_read = args->out_args[0].size;
899 struct address_space *mapping = NULL;
901 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
902 mapping = ap->pages[i]->mapping;
905 struct inode *inode = mapping->host;
908 * Short read means EOF. If file size is larger, truncate it
910 if (!err && num_read < count)
911 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
913 fuse_invalidate_atime(inode);
916 for (i = 0; i < ap->num_pages; i++) {
917 struct page *page = ap->pages[i];
920 SetPageUptodate(page);
927 fuse_file_put(ia->ff, false, false);
932 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
934 struct fuse_file *ff = file->private_data;
935 struct fuse_mount *fm = ff->fm;
936 struct fuse_args_pages *ap = &ia->ap;
937 loff_t pos = page_offset(ap->pages[0]);
938 size_t count = ap->num_pages << PAGE_SHIFT;
942 ap->args.out_pages = true;
943 ap->args.page_zeroing = true;
944 ap->args.page_replace = true;
946 /* Don't overflow end offset */
947 if (pos + (count - 1) == LLONG_MAX) {
949 ap->descs[ap->num_pages - 1].length--;
951 WARN_ON((loff_t) (pos + count) < 0);
953 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
954 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
955 if (fm->fc->async_read) {
956 ia->ff = fuse_file_get(ff);
957 ap->args.end = fuse_readpages_end;
958 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
962 res = fuse_simple_request(fm, &ap->args);
963 err = res < 0 ? res : 0;
965 fuse_readpages_end(fm, &ap->args, err);
968 static void fuse_readahead(struct readahead_control *rac)
970 struct inode *inode = rac->mapping->host;
971 struct fuse_conn *fc = get_fuse_conn(inode);
972 unsigned int i, max_pages, nr_pages = 0;
974 if (fuse_is_bad(inode))
977 max_pages = min_t(unsigned int, fc->max_pages,
978 fc->max_read / PAGE_SIZE);
981 struct fuse_io_args *ia;
982 struct fuse_args_pages *ap;
984 if (fc->num_background >= fc->congestion_threshold &&
985 rac->ra->async_size >= readahead_count(rac))
987 * Congested and only async pages left, so skip the
992 nr_pages = readahead_count(rac) - nr_pages;
993 if (nr_pages > max_pages)
994 nr_pages = max_pages;
997 ia = fuse_io_alloc(NULL, nr_pages);
1001 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
1002 for (i = 0; i < nr_pages; i++) {
1003 fuse_wait_on_page_writeback(inode,
1004 readahead_index(rac) + i);
1005 ap->descs[i].length = PAGE_SIZE;
1007 ap->num_pages = nr_pages;
1008 fuse_send_readpages(ia, rac->file);
1012 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
1014 struct inode *inode = iocb->ki_filp->f_mapping->host;
1015 struct fuse_conn *fc = get_fuse_conn(inode);
1018 * In auto invalidate mode, always update attributes on read.
1019 * Otherwise, only update if we attempt to read past EOF (to ensure
1020 * i_size is up to date).
1022 if (fc->auto_inval_data ||
1023 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
1025 err = fuse_update_attributes(inode, iocb->ki_filp, STATX_SIZE);
1030 return generic_file_read_iter(iocb, to);
1033 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1034 loff_t pos, size_t count)
1036 struct fuse_args *args = &ia->ap.args;
1038 ia->write.in.fh = ff->fh;
1039 ia->write.in.offset = pos;
1040 ia->write.in.size = count;
1041 args->opcode = FUSE_WRITE;
1042 args->nodeid = ff->nodeid;
1043 args->in_numargs = 2;
1044 if (ff->fm->fc->minor < 9)
1045 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1047 args->in_args[0].size = sizeof(ia->write.in);
1048 args->in_args[0].value = &ia->write.in;
1049 args->in_args[1].size = count;
1050 args->out_numargs = 1;
1051 args->out_args[0].size = sizeof(ia->write.out);
1052 args->out_args[0].value = &ia->write.out;
1055 static unsigned int fuse_write_flags(struct kiocb *iocb)
1057 unsigned int flags = iocb->ki_filp->f_flags;
1059 if (iocb_is_dsync(iocb))
1061 if (iocb->ki_flags & IOCB_SYNC)
1067 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1068 size_t count, fl_owner_t owner)
1070 struct kiocb *iocb = ia->io->iocb;
1071 struct file *file = iocb->ki_filp;
1072 struct fuse_file *ff = file->private_data;
1073 struct fuse_mount *fm = ff->fm;
1074 struct fuse_write_in *inarg = &ia->write.in;
1077 fuse_write_args_fill(ia, ff, pos, count);
1078 inarg->flags = fuse_write_flags(iocb);
1079 if (owner != NULL) {
1080 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1081 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
1085 return fuse_async_req_send(fm, ia, count);
1087 err = fuse_simple_request(fm, &ia->ap.args);
1088 if (!err && ia->write.out.size > count)
1091 return err ?: ia->write.out.size;
1094 bool fuse_write_update_attr(struct inode *inode, loff_t pos, ssize_t written)
1096 struct fuse_conn *fc = get_fuse_conn(inode);
1097 struct fuse_inode *fi = get_fuse_inode(inode);
1100 spin_lock(&fi->lock);
1101 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1102 if (written > 0 && pos > inode->i_size) {
1103 i_size_write(inode, pos);
1106 spin_unlock(&fi->lock);
1108 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
1113 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1114 struct kiocb *iocb, struct inode *inode,
1115 loff_t pos, size_t count)
1117 struct fuse_args_pages *ap = &ia->ap;
1118 struct file *file = iocb->ki_filp;
1119 struct fuse_file *ff = file->private_data;
1120 struct fuse_mount *fm = ff->fm;
1121 unsigned int offset, i;
1125 for (i = 0; i < ap->num_pages; i++)
1126 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1128 fuse_write_args_fill(ia, ff, pos, count);
1129 ia->write.in.flags = fuse_write_flags(iocb);
1130 if (fm->fc->handle_killpriv_v2 && !capable(CAP_FSETID))
1131 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1133 err = fuse_simple_request(fm, &ap->args);
1134 if (!err && ia->write.out.size > count)
1137 short_write = ia->write.out.size < count;
1138 offset = ap->descs[0].offset;
1139 count = ia->write.out.size;
1140 for (i = 0; i < ap->num_pages; i++) {
1141 struct page *page = ap->pages[i];
1144 ClearPageUptodate(page);
1146 if (count >= PAGE_SIZE - offset)
1147 count -= PAGE_SIZE - offset;
1150 ClearPageUptodate(page);
1155 if (ia->write.page_locked && (i == ap->num_pages - 1))
1163 static ssize_t fuse_fill_write_pages(struct fuse_io_args *ia,
1164 struct address_space *mapping,
1165 struct iov_iter *ii, loff_t pos,
1166 unsigned int max_pages)
1168 struct fuse_args_pages *ap = &ia->ap;
1169 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1170 unsigned offset = pos & (PAGE_SIZE - 1);
1174 ap->args.in_pages = true;
1175 ap->descs[0].offset = offset;
1180 pgoff_t index = pos >> PAGE_SHIFT;
1181 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1182 iov_iter_count(ii));
1184 bytes = min_t(size_t, bytes, fc->max_write - count);
1188 if (fault_in_iov_iter_readable(ii, bytes))
1192 page = grab_cache_page_write_begin(mapping, index);
1196 if (mapping_writably_mapped(mapping))
1197 flush_dcache_page(page);
1199 tmp = copy_page_from_iter_atomic(page, offset, bytes, ii);
1200 flush_dcache_page(page);
1209 ap->pages[ap->num_pages] = page;
1210 ap->descs[ap->num_pages].length = tmp;
1216 if (offset == PAGE_SIZE)
1219 /* If we copied full page, mark it uptodate */
1220 if (tmp == PAGE_SIZE)
1221 SetPageUptodate(page);
1223 if (PageUptodate(page)) {
1226 ia->write.page_locked = true;
1229 if (!fc->big_writes)
1231 } while (iov_iter_count(ii) && count < fc->max_write &&
1232 ap->num_pages < max_pages && offset == 0);
1234 return count > 0 ? count : err;
1237 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1238 unsigned int max_pages)
1240 return min_t(unsigned int,
1241 ((pos + len - 1) >> PAGE_SHIFT) -
1242 (pos >> PAGE_SHIFT) + 1,
1246 static ssize_t fuse_perform_write(struct kiocb *iocb,
1247 struct address_space *mapping,
1248 struct iov_iter *ii, loff_t pos)
1250 struct inode *inode = mapping->host;
1251 struct fuse_conn *fc = get_fuse_conn(inode);
1252 struct fuse_inode *fi = get_fuse_inode(inode);
1256 if (inode->i_size < pos + iov_iter_count(ii))
1257 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1261 struct fuse_io_args ia = {};
1262 struct fuse_args_pages *ap = &ia.ap;
1263 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1266 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1272 count = fuse_fill_write_pages(&ia, mapping, ii, pos, nr_pages);
1276 err = fuse_send_write_pages(&ia, iocb, inode,
1279 size_t num_written = ia.write.out.size;
1284 /* break out of the loop on short write */
1285 if (num_written != count)
1290 } while (!err && iov_iter_count(ii));
1292 fuse_write_update_attr(inode, pos, res);
1293 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1295 return res > 0 ? res : err;
1298 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1300 struct file *file = iocb->ki_filp;
1301 struct address_space *mapping = file->f_mapping;
1302 ssize_t written = 0;
1303 ssize_t written_buffered = 0;
1304 struct inode *inode = mapping->host;
1306 struct fuse_conn *fc = get_fuse_conn(inode);
1309 if (fc->writeback_cache) {
1310 /* Update size (EOF optimization) and mode (SUID clearing) */
1311 err = fuse_update_attributes(mapping->host, file,
1312 STATX_SIZE | STATX_MODE);
1316 if (fc->handle_killpriv_v2 &&
1317 setattr_should_drop_suidgid(&nop_mnt_idmap,
1318 file_inode(file))) {
1322 return generic_file_write_iter(iocb, from);
1328 /* We can write back this queue in page reclaim */
1329 current->backing_dev_info = inode_to_bdi(inode);
1331 err = generic_write_checks(iocb, from);
1335 err = file_remove_privs(file);
1339 err = file_update_time(file);
1343 if (iocb->ki_flags & IOCB_DIRECT) {
1344 loff_t pos = iocb->ki_pos;
1345 written = generic_file_direct_write(iocb, from);
1346 if (written < 0 || !iov_iter_count(from))
1351 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1352 if (written_buffered < 0) {
1353 err = written_buffered;
1356 endbyte = pos + written_buffered - 1;
1358 err = filemap_write_and_wait_range(file->f_mapping, pos,
1363 invalidate_mapping_pages(file->f_mapping,
1365 endbyte >> PAGE_SHIFT);
1367 written += written_buffered;
1368 iocb->ki_pos = pos + written_buffered;
1370 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1372 iocb->ki_pos += written;
1375 current->backing_dev_info = NULL;
1376 inode_unlock(inode);
1378 written = generic_write_sync(iocb, written);
1380 return written ? written : err;
1383 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1385 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1388 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1391 return min(iov_iter_single_seg_count(ii), max_size);
1394 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1395 size_t *nbytesp, int write,
1396 unsigned int max_pages)
1398 size_t nbytes = 0; /* # bytes already packed in req */
1401 /* Special case for kernel I/O: can copy directly into the buffer */
1402 if (iov_iter_is_kvec(ii)) {
1403 unsigned long user_addr = fuse_get_user_addr(ii);
1404 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1407 ap->args.in_args[1].value = (void *) user_addr;
1409 ap->args.out_args[0].value = (void *) user_addr;
1411 iov_iter_advance(ii, frag_size);
1412 *nbytesp = frag_size;
1416 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1419 ret = iov_iter_get_pages2(ii, &ap->pages[ap->num_pages],
1421 max_pages - ap->num_pages,
1429 npages = DIV_ROUND_UP(ret, PAGE_SIZE);
1431 ap->descs[ap->num_pages].offset = start;
1432 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1434 ap->num_pages += npages;
1435 ap->descs[ap->num_pages - 1].length -=
1436 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1439 ap->args.user_pages = true;
1441 ap->args.in_pages = true;
1443 ap->args.out_pages = true;
1447 return ret < 0 ? ret : 0;
1450 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1451 loff_t *ppos, int flags)
1453 int write = flags & FUSE_DIO_WRITE;
1454 int cuse = flags & FUSE_DIO_CUSE;
1455 struct file *file = io->iocb->ki_filp;
1456 struct inode *inode = file->f_mapping->host;
1457 struct fuse_file *ff = file->private_data;
1458 struct fuse_conn *fc = ff->fm->fc;
1459 size_t nmax = write ? fc->max_write : fc->max_read;
1461 size_t count = iov_iter_count(iter);
1462 pgoff_t idx_from = pos >> PAGE_SHIFT;
1463 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1466 struct fuse_io_args *ia;
1467 unsigned int max_pages;
1469 max_pages = iov_iter_npages(iter, fc->max_pages);
1470 ia = fuse_io_alloc(io, max_pages);
1474 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1477 fuse_sync_writes(inode);
1479 inode_unlock(inode);
1482 io->should_dirty = !write && user_backed_iter(iter);
1485 fl_owner_t owner = current->files;
1486 size_t nbytes = min(count, nmax);
1488 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1494 if (!capable(CAP_FSETID))
1495 ia->write.in.write_flags |= FUSE_WRITE_KILL_SUIDGID;
1497 nres = fuse_send_write(ia, pos, nbytes, owner);
1499 nres = fuse_send_read(ia, pos, nbytes, owner);
1502 if (!io->async || nres < 0) {
1503 fuse_release_user_pages(&ia->ap, io->should_dirty);
1508 iov_iter_revert(iter, nbytes);
1512 WARN_ON(nres > nbytes);
1517 if (nres != nbytes) {
1518 iov_iter_revert(iter, nbytes - nres);
1522 max_pages = iov_iter_npages(iter, fc->max_pages);
1523 ia = fuse_io_alloc(io, max_pages);
1533 return res > 0 ? res : err;
1535 EXPORT_SYMBOL_GPL(fuse_direct_io);
1537 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1538 struct iov_iter *iter,
1542 struct inode *inode = file_inode(io->iocb->ki_filp);
1544 res = fuse_direct_io(io, iter, ppos, 0);
1546 fuse_invalidate_atime(inode);
1551 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1553 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1557 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1558 res = fuse_direct_IO(iocb, to);
1560 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1562 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1568 static bool fuse_direct_write_extending_i_size(struct kiocb *iocb,
1569 struct iov_iter *iter)
1571 struct inode *inode = file_inode(iocb->ki_filp);
1573 return iocb->ki_pos + iov_iter_count(iter) > i_size_read(inode);
1576 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1578 struct inode *inode = file_inode(iocb->ki_filp);
1579 struct file *file = iocb->ki_filp;
1580 struct fuse_file *ff = file->private_data;
1581 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1583 bool exclusive_lock =
1584 !(ff->open_flags & FOPEN_PARALLEL_DIRECT_WRITES) ||
1585 iocb->ki_flags & IOCB_APPEND ||
1586 fuse_direct_write_extending_i_size(iocb, from);
1589 * Take exclusive lock if
1590 * - Parallel direct writes are disabled - a user space decision
1591 * - Parallel direct writes are enabled and i_size is being extended.
1592 * This might not be needed at all, but needs further investigation.
1597 inode_lock_shared(inode);
1599 /* A race with truncate might have come up as the decision for
1600 * the lock type was done without holding the lock, check again.
1602 if (fuse_direct_write_extending_i_size(iocb, from)) {
1603 inode_unlock_shared(inode);
1605 exclusive_lock = true;
1609 res = generic_write_checks(iocb, from);
1611 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1612 res = fuse_direct_IO(iocb, from);
1614 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1616 fuse_write_update_attr(inode, iocb->ki_pos, res);
1620 inode_unlock(inode);
1622 inode_unlock_shared(inode);
1627 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1629 struct file *file = iocb->ki_filp;
1630 struct fuse_file *ff = file->private_data;
1631 struct inode *inode = file_inode(file);
1633 if (fuse_is_bad(inode))
1636 if (FUSE_IS_DAX(inode))
1637 return fuse_dax_read_iter(iocb, to);
1639 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1640 return fuse_cache_read_iter(iocb, to);
1642 return fuse_direct_read_iter(iocb, to);
1645 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1647 struct file *file = iocb->ki_filp;
1648 struct fuse_file *ff = file->private_data;
1649 struct inode *inode = file_inode(file);
1651 if (fuse_is_bad(inode))
1654 if (FUSE_IS_DAX(inode))
1655 return fuse_dax_write_iter(iocb, from);
1657 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1658 return fuse_cache_write_iter(iocb, from);
1660 return fuse_direct_write_iter(iocb, from);
1663 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1665 struct fuse_args_pages *ap = &wpa->ia.ap;
1669 fuse_sync_bucket_dec(wpa->bucket);
1671 for (i = 0; i < ap->num_pages; i++)
1672 __free_page(ap->pages[i]);
1675 fuse_file_put(wpa->ia.ff, false, false);
1681 static void fuse_writepage_finish(struct fuse_mount *fm,
1682 struct fuse_writepage_args *wpa)
1684 struct fuse_args_pages *ap = &wpa->ia.ap;
1685 struct inode *inode = wpa->inode;
1686 struct fuse_inode *fi = get_fuse_inode(inode);
1687 struct backing_dev_info *bdi = inode_to_bdi(inode);
1690 for (i = 0; i < ap->num_pages; i++) {
1691 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1692 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1693 wb_writeout_inc(&bdi->wb);
1695 wake_up(&fi->page_waitq);
1698 /* Called under fi->lock, may release and reacquire it */
1699 static void fuse_send_writepage(struct fuse_mount *fm,
1700 struct fuse_writepage_args *wpa, loff_t size)
1701 __releases(fi->lock)
1702 __acquires(fi->lock)
1704 struct fuse_writepage_args *aux, *next;
1705 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1706 struct fuse_write_in *inarg = &wpa->ia.write.in;
1707 struct fuse_args *args = &wpa->ia.ap.args;
1708 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1712 if (inarg->offset + data_size <= size) {
1713 inarg->size = data_size;
1714 } else if (inarg->offset < size) {
1715 inarg->size = size - inarg->offset;
1717 /* Got truncated off completely */
1721 args->in_args[1].size = inarg->size;
1723 args->nocreds = true;
1725 err = fuse_simple_background(fm, args, GFP_ATOMIC);
1726 if (err == -ENOMEM) {
1727 spin_unlock(&fi->lock);
1728 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
1729 spin_lock(&fi->lock);
1732 /* Fails on broken connection only */
1740 rb_erase(&wpa->writepages_entry, &fi->writepages);
1741 fuse_writepage_finish(fm, wpa);
1742 spin_unlock(&fi->lock);
1744 /* After fuse_writepage_finish() aux request list is private */
1745 for (aux = wpa->next; aux; aux = next) {
1748 fuse_writepage_free(aux);
1751 fuse_writepage_free(wpa);
1752 spin_lock(&fi->lock);
1756 * If fi->writectr is positive (no truncate or fsync going on) send
1757 * all queued writepage requests.
1759 * Called with fi->lock
1761 void fuse_flush_writepages(struct inode *inode)
1762 __releases(fi->lock)
1763 __acquires(fi->lock)
1765 struct fuse_mount *fm = get_fuse_mount(inode);
1766 struct fuse_inode *fi = get_fuse_inode(inode);
1767 loff_t crop = i_size_read(inode);
1768 struct fuse_writepage_args *wpa;
1770 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1771 wpa = list_entry(fi->queued_writes.next,
1772 struct fuse_writepage_args, queue_entry);
1773 list_del_init(&wpa->queue_entry);
1774 fuse_send_writepage(fm, wpa, crop);
1778 static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1779 struct fuse_writepage_args *wpa)
1781 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1782 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1783 struct rb_node **p = &root->rb_node;
1784 struct rb_node *parent = NULL;
1786 WARN_ON(!wpa->ia.ap.num_pages);
1788 struct fuse_writepage_args *curr;
1792 curr = rb_entry(parent, struct fuse_writepage_args,
1794 WARN_ON(curr->inode != wpa->inode);
1795 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1797 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1798 p = &(*p)->rb_right;
1799 else if (idx_to < curr_index)
1805 rb_link_node(&wpa->writepages_entry, parent, p);
1806 rb_insert_color(&wpa->writepages_entry, root);
1810 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1812 WARN_ON(fuse_insert_writeback(root, wpa));
1815 static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
1818 struct fuse_writepage_args *wpa =
1819 container_of(args, typeof(*wpa), ia.ap.args);
1820 struct inode *inode = wpa->inode;
1821 struct fuse_inode *fi = get_fuse_inode(inode);
1822 struct fuse_conn *fc = get_fuse_conn(inode);
1824 mapping_set_error(inode->i_mapping, error);
1826 * A writeback finished and this might have updated mtime/ctime on
1827 * server making local mtime/ctime stale. Hence invalidate attrs.
1828 * Do this only if writeback_cache is not enabled. If writeback_cache
1829 * is enabled, we trust local ctime/mtime.
1831 if (!fc->writeback_cache)
1832 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODIFY);
1833 spin_lock(&fi->lock);
1834 rb_erase(&wpa->writepages_entry, &fi->writepages);
1836 struct fuse_mount *fm = get_fuse_mount(inode);
1837 struct fuse_write_in *inarg = &wpa->ia.write.in;
1838 struct fuse_writepage_args *next = wpa->next;
1840 wpa->next = next->next;
1842 next->ia.ff = fuse_file_get(wpa->ia.ff);
1843 tree_insert(&fi->writepages, next);
1846 * Skip fuse_flush_writepages() to make it easy to crop requests
1847 * based on primary request size.
1849 * 1st case (trivial): there are no concurrent activities using
1850 * fuse_set/release_nowrite. Then we're on safe side because
1851 * fuse_flush_writepages() would call fuse_send_writepage()
1854 * 2nd case: someone called fuse_set_nowrite and it is waiting
1855 * now for completion of all in-flight requests. This happens
1856 * rarely and no more than once per page, so this should be
1859 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1860 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1861 * that fuse_set_nowrite returned implies that all in-flight
1862 * requests were completed along with all of their secondary
1863 * requests. Further primary requests are blocked by negative
1864 * writectr. Hence there cannot be any in-flight requests and
1865 * no invocations of fuse_writepage_end() while we're in
1866 * fuse_set_nowrite..fuse_release_nowrite section.
1868 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
1871 fuse_writepage_finish(fm, wpa);
1872 spin_unlock(&fi->lock);
1873 fuse_writepage_free(wpa);
1876 static struct fuse_file *__fuse_write_file_get(struct fuse_inode *fi)
1878 struct fuse_file *ff;
1880 spin_lock(&fi->lock);
1881 ff = list_first_entry_or_null(&fi->write_files, struct fuse_file,
1885 spin_unlock(&fi->lock);
1890 static struct fuse_file *fuse_write_file_get(struct fuse_inode *fi)
1892 struct fuse_file *ff = __fuse_write_file_get(fi);
1897 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1899 struct fuse_inode *fi = get_fuse_inode(inode);
1900 struct fuse_file *ff;
1904 * Inode is always written before the last reference is dropped and
1905 * hence this should not be reached from reclaim.
1907 * Writing back the inode from reclaim can deadlock if the request
1908 * processing itself needs an allocation. Allocations triggering
1909 * reclaim while serving a request can't be prevented, because it can
1910 * involve any number of unrelated userspace processes.
1912 WARN_ON(wbc->for_reclaim);
1914 ff = __fuse_write_file_get(fi);
1915 err = fuse_flush_times(inode, ff);
1917 fuse_file_put(ff, false, false);
1922 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1924 struct fuse_writepage_args *wpa;
1925 struct fuse_args_pages *ap;
1927 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1931 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1941 static void fuse_writepage_add_to_bucket(struct fuse_conn *fc,
1942 struct fuse_writepage_args *wpa)
1948 /* Prevent resurrection of dead bucket in unlikely race with syncfs */
1950 wpa->bucket = rcu_dereference(fc->curr_bucket);
1951 } while (unlikely(!atomic_inc_not_zero(&wpa->bucket->count)));
1955 static int fuse_writepage_locked(struct page *page)
1957 struct address_space *mapping = page->mapping;
1958 struct inode *inode = mapping->host;
1959 struct fuse_conn *fc = get_fuse_conn(inode);
1960 struct fuse_inode *fi = get_fuse_inode(inode);
1961 struct fuse_writepage_args *wpa;
1962 struct fuse_args_pages *ap;
1963 struct page *tmp_page;
1964 int error = -ENOMEM;
1966 set_page_writeback(page);
1968 wpa = fuse_writepage_args_alloc();
1973 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1978 wpa->ia.ff = fuse_write_file_get(fi);
1982 fuse_writepage_add_to_bucket(fc, wpa);
1983 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1985 copy_highpage(tmp_page, page);
1986 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1988 ap->args.in_pages = true;
1990 ap->pages[0] = tmp_page;
1991 ap->descs[0].offset = 0;
1992 ap->descs[0].length = PAGE_SIZE;
1993 ap->args.end = fuse_writepage_end;
1996 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1997 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1999 spin_lock(&fi->lock);
2000 tree_insert(&fi->writepages, wpa);
2001 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2002 fuse_flush_writepages(inode);
2003 spin_unlock(&fi->lock);
2005 end_page_writeback(page);
2010 __free_page(tmp_page);
2014 mapping_set_error(page->mapping, error);
2015 end_page_writeback(page);
2019 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
2021 struct fuse_conn *fc = get_fuse_conn(page->mapping->host);
2024 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
2026 * ->writepages() should be called for sync() and friends. We
2027 * should only get here on direct reclaim and then we are
2028 * allowed to skip a page which is already in flight
2030 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
2032 redirty_page_for_writepage(wbc, page);
2037 if (wbc->sync_mode == WB_SYNC_NONE &&
2038 fc->num_background >= fc->congestion_threshold)
2039 return AOP_WRITEPAGE_ACTIVATE;
2041 err = fuse_writepage_locked(page);
2047 struct fuse_fill_wb_data {
2048 struct fuse_writepage_args *wpa;
2049 struct fuse_file *ff;
2050 struct inode *inode;
2051 struct page **orig_pages;
2052 unsigned int max_pages;
2055 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
2057 struct fuse_args_pages *ap = &data->wpa->ia.ap;
2058 struct fuse_conn *fc = get_fuse_conn(data->inode);
2059 struct page **pages;
2060 struct fuse_page_desc *descs;
2061 unsigned int npages = min_t(unsigned int,
2062 max_t(unsigned int, data->max_pages * 2,
2063 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
2065 WARN_ON(npages <= data->max_pages);
2067 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
2071 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
2072 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
2076 data->max_pages = npages;
2081 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
2083 struct fuse_writepage_args *wpa = data->wpa;
2084 struct inode *inode = data->inode;
2085 struct fuse_inode *fi = get_fuse_inode(inode);
2086 int num_pages = wpa->ia.ap.num_pages;
2089 wpa->ia.ff = fuse_file_get(data->ff);
2090 spin_lock(&fi->lock);
2091 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
2092 fuse_flush_writepages(inode);
2093 spin_unlock(&fi->lock);
2095 for (i = 0; i < num_pages; i++)
2096 end_page_writeback(data->orig_pages[i]);
2100 * Check under fi->lock if the page is under writeback, and insert it onto the
2101 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2102 * one already added for a page at this offset. If there's none, then insert
2103 * this new request onto the auxiliary list, otherwise reuse the existing one by
2104 * swapping the new temp page with the old one.
2106 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
2109 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
2110 struct fuse_writepage_args *tmp;
2111 struct fuse_writepage_args *old_wpa;
2112 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
2114 WARN_ON(new_ap->num_pages != 0);
2115 new_ap->num_pages = 1;
2117 spin_lock(&fi->lock);
2118 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
2120 spin_unlock(&fi->lock);
2124 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2127 WARN_ON(tmp->inode != new_wpa->inode);
2128 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2129 if (curr_index == page->index) {
2130 WARN_ON(tmp->ia.ap.num_pages != 1);
2131 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2137 new_wpa->next = old_wpa->next;
2138 old_wpa->next = new_wpa;
2141 spin_unlock(&fi->lock);
2144 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2146 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2147 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2148 wb_writeout_inc(&bdi->wb);
2149 fuse_writepage_free(new_wpa);
2155 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2156 struct fuse_args_pages *ap,
2157 struct fuse_fill_wb_data *data)
2159 WARN_ON(!ap->num_pages);
2162 * Being under writeback is unlikely but possible. For example direct
2163 * read to an mmaped fuse file will set the page dirty twice; once when
2164 * the pages are faulted with get_user_pages(), and then after the read
2167 if (fuse_page_is_writeback(data->inode, page->index))
2170 /* Reached max pages */
2171 if (ap->num_pages == fc->max_pages)
2174 /* Reached max write bytes */
2175 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2179 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2182 /* Need to grow the pages array? If so, did the expansion fail? */
2183 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2189 static int fuse_writepages_fill(struct folio *folio,
2190 struct writeback_control *wbc, void *_data)
2192 struct fuse_fill_wb_data *data = _data;
2193 struct fuse_writepage_args *wpa = data->wpa;
2194 struct fuse_args_pages *ap = &wpa->ia.ap;
2195 struct inode *inode = data->inode;
2196 struct fuse_inode *fi = get_fuse_inode(inode);
2197 struct fuse_conn *fc = get_fuse_conn(inode);
2198 struct page *tmp_page;
2203 data->ff = fuse_write_file_get(fi);
2208 if (wpa && fuse_writepage_need_send(fc, &folio->page, ap, data)) {
2209 fuse_writepages_send(data);
2214 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2219 * The page must not be redirtied until the writeout is completed
2220 * (i.e. userspace has sent a reply to the write request). Otherwise
2221 * there could be more than one temporary page instance for each real
2224 * This is ensured by holding the page lock in page_mkwrite() while
2225 * checking fuse_page_is_writeback(). We already hold the page lock
2226 * since clear_page_dirty_for_io() and keep it held until we add the
2227 * request to the fi->writepages list and increment ap->num_pages.
2228 * After this fuse_page_is_writeback() will indicate that the page is
2229 * under writeback, so we can release the page lock.
2231 if (data->wpa == NULL) {
2233 wpa = fuse_writepage_args_alloc();
2235 __free_page(tmp_page);
2238 fuse_writepage_add_to_bucket(fc, wpa);
2240 data->max_pages = 1;
2243 fuse_write_args_fill(&wpa->ia, data->ff, folio_pos(folio), 0);
2244 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2246 ap->args.in_pages = true;
2247 ap->args.end = fuse_writepage_end;
2251 folio_start_writeback(folio);
2253 copy_highpage(tmp_page, &folio->page);
2254 ap->pages[ap->num_pages] = tmp_page;
2255 ap->descs[ap->num_pages].offset = 0;
2256 ap->descs[ap->num_pages].length = PAGE_SIZE;
2257 data->orig_pages[ap->num_pages] = &folio->page;
2259 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2260 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2265 * Protected by fi->lock against concurrent access by
2266 * fuse_page_is_writeback().
2268 spin_lock(&fi->lock);
2270 spin_unlock(&fi->lock);
2271 } else if (fuse_writepage_add(wpa, &folio->page)) {
2274 folio_end_writeback(folio);
2277 folio_unlock(folio);
2282 static int fuse_writepages(struct address_space *mapping,
2283 struct writeback_control *wbc)
2285 struct inode *inode = mapping->host;
2286 struct fuse_conn *fc = get_fuse_conn(inode);
2287 struct fuse_fill_wb_data data;
2291 if (fuse_is_bad(inode))
2294 if (wbc->sync_mode == WB_SYNC_NONE &&
2295 fc->num_background >= fc->congestion_threshold)
2303 data.orig_pages = kcalloc(fc->max_pages,
2304 sizeof(struct page *),
2306 if (!data.orig_pages)
2309 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2311 WARN_ON(!data.wpa->ia.ap.num_pages);
2312 fuse_writepages_send(&data);
2315 fuse_file_put(data.ff, false, false);
2317 kfree(data.orig_pages);
2323 * It's worthy to make sure that space is reserved on disk for the write,
2324 * but how to implement it without killing performance need more thinking.
2326 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2327 loff_t pos, unsigned len, struct page **pagep, void **fsdata)
2329 pgoff_t index = pos >> PAGE_SHIFT;
2330 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2335 WARN_ON(!fc->writeback_cache);
2337 page = grab_cache_page_write_begin(mapping, index);
2341 fuse_wait_on_page_writeback(mapping->host, page->index);
2343 if (PageUptodate(page) || len == PAGE_SIZE)
2346 * Check if the start this page comes after the end of file, in which
2347 * case the readpage can be optimized away.
2349 fsize = i_size_read(mapping->host);
2350 if (fsize <= (pos & PAGE_MASK)) {
2351 size_t off = pos & ~PAGE_MASK;
2353 zero_user_segment(page, 0, off);
2356 err = fuse_do_readpage(file, page);
2370 static int fuse_write_end(struct file *file, struct address_space *mapping,
2371 loff_t pos, unsigned len, unsigned copied,
2372 struct page *page, void *fsdata)
2374 struct inode *inode = page->mapping->host;
2376 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2381 if (!PageUptodate(page)) {
2382 /* Zero any unwritten bytes at the end of the page */
2383 size_t endoff = pos & ~PAGE_MASK;
2385 zero_user_segment(page, endoff, PAGE_SIZE);
2386 SetPageUptodate(page);
2389 if (pos > inode->i_size)
2390 i_size_write(inode, pos);
2392 set_page_dirty(page);
2401 static int fuse_launder_folio(struct folio *folio)
2404 if (folio_clear_dirty_for_io(folio)) {
2405 struct inode *inode = folio->mapping->host;
2407 /* Serialize with pending writeback for the same page */
2408 fuse_wait_on_page_writeback(inode, folio->index);
2409 err = fuse_writepage_locked(&folio->page);
2411 fuse_wait_on_page_writeback(inode, folio->index);
2417 * Write back dirty data/metadata now (there may not be any suitable
2418 * open files later for data)
2420 static void fuse_vma_close(struct vm_area_struct *vma)
2424 err = write_inode_now(vma->vm_file->f_mapping->host, 1);
2425 mapping_set_error(vma->vm_file->f_mapping, err);
2429 * Wait for writeback against this page to complete before allowing it
2430 * to be marked dirty again, and hence written back again, possibly
2431 * before the previous writepage completed.
2433 * Block here, instead of in ->writepage(), so that the userspace fs
2434 * can only block processes actually operating on the filesystem.
2436 * Otherwise unprivileged userspace fs would be able to block
2441 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2443 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2445 struct page *page = vmf->page;
2446 struct inode *inode = file_inode(vmf->vma->vm_file);
2448 file_update_time(vmf->vma->vm_file);
2450 if (page->mapping != inode->i_mapping) {
2452 return VM_FAULT_NOPAGE;
2455 fuse_wait_on_page_writeback(inode, page->index);
2456 return VM_FAULT_LOCKED;
2459 static const struct vm_operations_struct fuse_file_vm_ops = {
2460 .close = fuse_vma_close,
2461 .fault = filemap_fault,
2462 .map_pages = filemap_map_pages,
2463 .page_mkwrite = fuse_page_mkwrite,
2466 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2468 struct fuse_file *ff = file->private_data;
2470 /* DAX mmap is superior to direct_io mmap */
2471 if (FUSE_IS_DAX(file_inode(file)))
2472 return fuse_dax_mmap(file, vma);
2474 if (ff->open_flags & FOPEN_DIRECT_IO) {
2475 /* Can't provide the coherency needed for MAP_SHARED */
2476 if (vma->vm_flags & VM_MAYSHARE)
2479 invalidate_inode_pages2(file->f_mapping);
2481 return generic_file_mmap(file, vma);
2484 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2485 fuse_link_write_file(file);
2487 file_accessed(file);
2488 vma->vm_ops = &fuse_file_vm_ops;
2492 static int convert_fuse_file_lock(struct fuse_conn *fc,
2493 const struct fuse_file_lock *ffl,
2494 struct file_lock *fl)
2496 switch (ffl->type) {
2502 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2503 ffl->end < ffl->start)
2506 fl->fl_start = ffl->start;
2507 fl->fl_end = ffl->end;
2510 * Convert pid into init's pid namespace. The locks API will
2511 * translate it into the caller's pid namespace.
2514 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2521 fl->fl_type = ffl->type;
2525 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2526 const struct file_lock *fl, int opcode, pid_t pid,
2527 int flock, struct fuse_lk_in *inarg)
2529 struct inode *inode = file_inode(file);
2530 struct fuse_conn *fc = get_fuse_conn(inode);
2531 struct fuse_file *ff = file->private_data;
2533 memset(inarg, 0, sizeof(*inarg));
2535 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2536 inarg->lk.start = fl->fl_start;
2537 inarg->lk.end = fl->fl_end;
2538 inarg->lk.type = fl->fl_type;
2539 inarg->lk.pid = pid;
2541 inarg->lk_flags |= FUSE_LK_FLOCK;
2542 args->opcode = opcode;
2543 args->nodeid = get_node_id(inode);
2544 args->in_numargs = 1;
2545 args->in_args[0].size = sizeof(*inarg);
2546 args->in_args[0].value = inarg;
2549 static int fuse_getlk(struct file *file, struct file_lock *fl)
2551 struct inode *inode = file_inode(file);
2552 struct fuse_mount *fm = get_fuse_mount(inode);
2554 struct fuse_lk_in inarg;
2555 struct fuse_lk_out outarg;
2558 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2559 args.out_numargs = 1;
2560 args.out_args[0].size = sizeof(outarg);
2561 args.out_args[0].value = &outarg;
2562 err = fuse_simple_request(fm, &args);
2564 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2569 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2571 struct inode *inode = file_inode(file);
2572 struct fuse_mount *fm = get_fuse_mount(inode);
2574 struct fuse_lk_in inarg;
2575 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2576 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2577 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2580 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2581 /* NLM needs asynchronous locks, which we don't support yet */
2585 /* Unlock on close is handled by the flush method */
2586 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2589 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2590 err = fuse_simple_request(fm, &args);
2592 /* locking is restartable */
2599 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2601 struct inode *inode = file_inode(file);
2602 struct fuse_conn *fc = get_fuse_conn(inode);
2605 if (cmd == F_CANCELLK) {
2607 } else if (cmd == F_GETLK) {
2609 posix_test_lock(file, fl);
2612 err = fuse_getlk(file, fl);
2615 err = posix_lock_file(file, fl, NULL);
2617 err = fuse_setlk(file, fl, 0);
2622 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2624 struct inode *inode = file_inode(file);
2625 struct fuse_conn *fc = get_fuse_conn(inode);
2629 err = locks_lock_file_wait(file, fl);
2631 struct fuse_file *ff = file->private_data;
2633 /* emulate flock with POSIX locks */
2635 err = fuse_setlk(file, fl, 1);
2641 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2643 struct inode *inode = mapping->host;
2644 struct fuse_mount *fm = get_fuse_mount(inode);
2646 struct fuse_bmap_in inarg;
2647 struct fuse_bmap_out outarg;
2650 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2653 memset(&inarg, 0, sizeof(inarg));
2654 inarg.block = block;
2655 inarg.blocksize = inode->i_sb->s_blocksize;
2656 args.opcode = FUSE_BMAP;
2657 args.nodeid = get_node_id(inode);
2658 args.in_numargs = 1;
2659 args.in_args[0].size = sizeof(inarg);
2660 args.in_args[0].value = &inarg;
2661 args.out_numargs = 1;
2662 args.out_args[0].size = sizeof(outarg);
2663 args.out_args[0].value = &outarg;
2664 err = fuse_simple_request(fm, &args);
2666 fm->fc->no_bmap = 1;
2668 return err ? 0 : outarg.block;
2671 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2673 struct inode *inode = file->f_mapping->host;
2674 struct fuse_mount *fm = get_fuse_mount(inode);
2675 struct fuse_file *ff = file->private_data;
2677 struct fuse_lseek_in inarg = {
2682 struct fuse_lseek_out outarg;
2685 if (fm->fc->no_lseek)
2688 args.opcode = FUSE_LSEEK;
2689 args.nodeid = ff->nodeid;
2690 args.in_numargs = 1;
2691 args.in_args[0].size = sizeof(inarg);
2692 args.in_args[0].value = &inarg;
2693 args.out_numargs = 1;
2694 args.out_args[0].size = sizeof(outarg);
2695 args.out_args[0].value = &outarg;
2696 err = fuse_simple_request(fm, &args);
2698 if (err == -ENOSYS) {
2699 fm->fc->no_lseek = 1;
2705 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2708 err = fuse_update_attributes(inode, file, STATX_SIZE);
2710 return generic_file_llseek(file, offset, whence);
2715 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2718 struct inode *inode = file_inode(file);
2723 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2724 retval = generic_file_llseek(file, offset, whence);
2728 retval = fuse_update_attributes(inode, file, STATX_SIZE);
2730 retval = generic_file_llseek(file, offset, whence);
2731 inode_unlock(inode);
2736 retval = fuse_lseek(file, offset, whence);
2737 inode_unlock(inode);
2747 * All files which have been polled are linked to RB tree
2748 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2749 * find the matching one.
2751 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2752 struct rb_node **parent_out)
2754 struct rb_node **link = &fc->polled_files.rb_node;
2755 struct rb_node *last = NULL;
2758 struct fuse_file *ff;
2761 ff = rb_entry(last, struct fuse_file, polled_node);
2764 link = &last->rb_left;
2765 else if (kh > ff->kh)
2766 link = &last->rb_right;
2777 * The file is about to be polled. Make sure it's on the polled_files
2778 * RB tree. Note that files once added to the polled_files tree are
2779 * not removed before the file is released. This is because a file
2780 * polled once is likely to be polled again.
2782 static void fuse_register_polled_file(struct fuse_conn *fc,
2783 struct fuse_file *ff)
2785 spin_lock(&fc->lock);
2786 if (RB_EMPTY_NODE(&ff->polled_node)) {
2787 struct rb_node **link, *parent;
2789 link = fuse_find_polled_node(fc, ff->kh, &parent);
2791 rb_link_node(&ff->polled_node, parent, link);
2792 rb_insert_color(&ff->polled_node, &fc->polled_files);
2794 spin_unlock(&fc->lock);
2797 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2799 struct fuse_file *ff = file->private_data;
2800 struct fuse_mount *fm = ff->fm;
2801 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2802 struct fuse_poll_out outarg;
2806 if (fm->fc->no_poll)
2807 return DEFAULT_POLLMASK;
2809 poll_wait(file, &ff->poll_wait, wait);
2810 inarg.events = mangle_poll(poll_requested_events(wait));
2813 * Ask for notification iff there's someone waiting for it.
2814 * The client may ignore the flag and always notify.
2816 if (waitqueue_active(&ff->poll_wait)) {
2817 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2818 fuse_register_polled_file(fm->fc, ff);
2821 args.opcode = FUSE_POLL;
2822 args.nodeid = ff->nodeid;
2823 args.in_numargs = 1;
2824 args.in_args[0].size = sizeof(inarg);
2825 args.in_args[0].value = &inarg;
2826 args.out_numargs = 1;
2827 args.out_args[0].size = sizeof(outarg);
2828 args.out_args[0].value = &outarg;
2829 err = fuse_simple_request(fm, &args);
2832 return demangle_poll(outarg.revents);
2833 if (err == -ENOSYS) {
2834 fm->fc->no_poll = 1;
2835 return DEFAULT_POLLMASK;
2839 EXPORT_SYMBOL_GPL(fuse_file_poll);
2842 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2843 * wakes up the poll waiters.
2845 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2846 struct fuse_notify_poll_wakeup_out *outarg)
2848 u64 kh = outarg->kh;
2849 struct rb_node **link;
2851 spin_lock(&fc->lock);
2853 link = fuse_find_polled_node(fc, kh, NULL);
2855 struct fuse_file *ff;
2857 ff = rb_entry(*link, struct fuse_file, polled_node);
2858 wake_up_interruptible_sync(&ff->poll_wait);
2861 spin_unlock(&fc->lock);
2865 static void fuse_do_truncate(struct file *file)
2867 struct inode *inode = file->f_mapping->host;
2870 attr.ia_valid = ATTR_SIZE;
2871 attr.ia_size = i_size_read(inode);
2873 attr.ia_file = file;
2874 attr.ia_valid |= ATTR_FILE;
2876 fuse_do_setattr(file_dentry(file), &attr, file);
2879 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2881 return round_up(off, fc->max_pages << PAGE_SHIFT);
2885 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2887 DECLARE_COMPLETION_ONSTACK(wait);
2889 struct file *file = iocb->ki_filp;
2890 struct fuse_file *ff = file->private_data;
2892 struct inode *inode;
2894 size_t count = iov_iter_count(iter), shortened = 0;
2895 loff_t offset = iocb->ki_pos;
2896 struct fuse_io_priv *io;
2899 inode = file->f_mapping->host;
2900 i_size = i_size_read(inode);
2902 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
2905 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2908 spin_lock_init(&io->lock);
2909 kref_init(&io->refcnt);
2913 io->offset = offset;
2914 io->write = (iov_iter_rw(iter) == WRITE);
2917 * By default, we want to optimize all I/Os with async request
2918 * submission to the client filesystem if supported.
2920 io->async = ff->fm->fc->async_dio;
2922 io->blocking = is_sync_kiocb(iocb);
2924 /* optimization for short read */
2925 if (io->async && !io->write && offset + count > i_size) {
2926 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
2927 shortened = count - iov_iter_count(iter);
2932 * We cannot asynchronously extend the size of a file.
2933 * In such case the aio will behave exactly like sync io.
2935 if ((offset + count > i_size) && io->write)
2936 io->blocking = true;
2938 if (io->async && io->blocking) {
2940 * Additional reference to keep io around after
2941 * calling fuse_aio_complete()
2943 kref_get(&io->refcnt);
2947 if (iov_iter_rw(iter) == WRITE) {
2948 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2949 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
2951 ret = __fuse_direct_read(io, iter, &pos);
2953 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
2956 bool blocking = io->blocking;
2958 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2960 /* we have a non-extending, async request, so return */
2962 return -EIOCBQUEUED;
2964 wait_for_completion(&wait);
2965 ret = fuse_get_res_by_io(io);
2968 kref_put(&io->refcnt, fuse_io_release);
2970 if (iov_iter_rw(iter) == WRITE) {
2971 fuse_write_update_attr(inode, pos, ret);
2972 /* For extending writes we already hold exclusive lock */
2973 if (ret < 0 && offset + count > i_size)
2974 fuse_do_truncate(file);
2980 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
2982 int err = filemap_write_and_wait_range(inode->i_mapping, start, LLONG_MAX);
2985 fuse_sync_writes(inode);
2990 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2993 struct fuse_file *ff = file->private_data;
2994 struct inode *inode = file_inode(file);
2995 struct fuse_inode *fi = get_fuse_inode(inode);
2996 struct fuse_mount *fm = ff->fm;
2998 struct fuse_fallocate_in inarg = {
3005 bool block_faults = FUSE_IS_DAX(inode) &&
3006 (!(mode & FALLOC_FL_KEEP_SIZE) ||
3007 (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)));
3009 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
3010 FALLOC_FL_ZERO_RANGE))
3013 if (fm->fc->no_fallocate)
3018 filemap_invalidate_lock(inode->i_mapping);
3019 err = fuse_dax_break_layouts(inode, 0, 0);
3024 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE)) {
3025 loff_t endbyte = offset + length - 1;
3027 err = fuse_writeback_range(inode, offset, endbyte);
3032 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3033 offset + length > i_size_read(inode)) {
3034 err = inode_newsize_ok(inode, offset + length);
3039 err = file_modified(file);
3043 if (!(mode & FALLOC_FL_KEEP_SIZE))
3044 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3046 args.opcode = FUSE_FALLOCATE;
3047 args.nodeid = ff->nodeid;
3048 args.in_numargs = 1;
3049 args.in_args[0].size = sizeof(inarg);
3050 args.in_args[0].value = &inarg;
3051 err = fuse_simple_request(fm, &args);
3052 if (err == -ENOSYS) {
3053 fm->fc->no_fallocate = 1;
3059 /* we could have extended the file */
3060 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3061 if (fuse_write_update_attr(inode, offset + length, length))
3062 file_update_time(file);
3065 if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE))
3066 truncate_pagecache_range(inode, offset, offset + length - 1);
3068 fuse_invalidate_attr_mask(inode, FUSE_STATX_MODSIZE);
3071 if (!(mode & FALLOC_FL_KEEP_SIZE))
3072 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3075 filemap_invalidate_unlock(inode->i_mapping);
3077 inode_unlock(inode);
3079 fuse_flush_time_update(inode);
3084 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3085 struct file *file_out, loff_t pos_out,
3086 size_t len, unsigned int flags)
3088 struct fuse_file *ff_in = file_in->private_data;
3089 struct fuse_file *ff_out = file_out->private_data;
3090 struct inode *inode_in = file_inode(file_in);
3091 struct inode *inode_out = file_inode(file_out);
3092 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3093 struct fuse_mount *fm = ff_in->fm;
3094 struct fuse_conn *fc = fm->fc;
3096 struct fuse_copy_file_range_in inarg = {
3099 .nodeid_out = ff_out->nodeid,
3100 .fh_out = ff_out->fh,
3105 struct fuse_write_out outarg;
3107 /* mark unstable when write-back is not used, and file_out gets
3109 bool is_unstable = (!fc->writeback_cache) &&
3110 ((pos_out + len) > inode_out->i_size);
3112 if (fc->no_copy_file_range)
3115 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3118 inode_lock(inode_in);
3119 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3120 inode_unlock(inode_in);
3124 inode_lock(inode_out);
3126 err = file_modified(file_out);
3131 * Write out dirty pages in the destination file before sending the COPY
3132 * request to userspace. After the request is completed, truncate off
3133 * pages (including partial ones) from the cache that have been copied,
3134 * since these contain stale data at that point.
3136 * This should be mostly correct, but if the COPY writes to partial
3137 * pages (at the start or end) and the parts not covered by the COPY are
3138 * written through a memory map after calling fuse_writeback_range(),
3139 * then these partial page modifications will be lost on truncation.
3141 * It is unlikely that someone would rely on such mixed style
3142 * modifications. Yet this does give less guarantees than if the
3143 * copying was performed with write(2).
3145 * To fix this a mapping->invalidate_lock could be used to prevent new
3146 * faults while the copy is ongoing.
3148 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3153 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3155 args.opcode = FUSE_COPY_FILE_RANGE;
3156 args.nodeid = ff_in->nodeid;
3157 args.in_numargs = 1;
3158 args.in_args[0].size = sizeof(inarg);
3159 args.in_args[0].value = &inarg;
3160 args.out_numargs = 1;
3161 args.out_args[0].size = sizeof(outarg);
3162 args.out_args[0].value = &outarg;
3163 err = fuse_simple_request(fm, &args);
3164 if (err == -ENOSYS) {
3165 fc->no_copy_file_range = 1;
3171 truncate_inode_pages_range(inode_out->i_mapping,
3172 ALIGN_DOWN(pos_out, PAGE_SIZE),
3173 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3175 file_update_time(file_out);
3176 fuse_write_update_attr(inode_out, pos_out + outarg.size, outarg.size);
3181 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3183 inode_unlock(inode_out);
3184 file_accessed(file_in);
3186 fuse_flush_time_update(inode_out);
3191 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3192 struct file *dst_file, loff_t dst_off,
3193 size_t len, unsigned int flags)
3197 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3200 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3201 ret = generic_copy_file_range(src_file, src_off, dst_file,
3202 dst_off, len, flags);
3206 static const struct file_operations fuse_file_operations = {
3207 .llseek = fuse_file_llseek,
3208 .read_iter = fuse_file_read_iter,
3209 .write_iter = fuse_file_write_iter,
3210 .mmap = fuse_file_mmap,
3212 .flush = fuse_flush,
3213 .release = fuse_release,
3214 .fsync = fuse_fsync,
3215 .lock = fuse_file_lock,
3216 .get_unmapped_area = thp_get_unmapped_area,
3217 .flock = fuse_file_flock,
3218 .splice_read = generic_file_splice_read,
3219 .splice_write = iter_file_splice_write,
3220 .unlocked_ioctl = fuse_file_ioctl,
3221 .compat_ioctl = fuse_file_compat_ioctl,
3222 .poll = fuse_file_poll,
3223 .fallocate = fuse_file_fallocate,
3224 .copy_file_range = fuse_copy_file_range,
3227 static const struct address_space_operations fuse_file_aops = {
3228 .read_folio = fuse_read_folio,
3229 .readahead = fuse_readahead,
3230 .writepage = fuse_writepage,
3231 .writepages = fuse_writepages,
3232 .launder_folio = fuse_launder_folio,
3233 .dirty_folio = filemap_dirty_folio,
3235 .direct_IO = fuse_direct_IO,
3236 .write_begin = fuse_write_begin,
3237 .write_end = fuse_write_end,
3240 void fuse_init_file_inode(struct inode *inode, unsigned int flags)
3242 struct fuse_inode *fi = get_fuse_inode(inode);
3244 inode->i_fop = &fuse_file_operations;
3245 inode->i_data.a_ops = &fuse_file_aops;
3247 INIT_LIST_HEAD(&fi->write_files);
3248 INIT_LIST_HEAD(&fi->queued_writes);
3250 init_waitqueue_head(&fi->page_waitq);
3251 fi->writepages = RB_ROOT;
3253 if (IS_ENABLED(CONFIG_FUSE_DAX))
3254 fuse_dax_inode_init(inode, flags);