2 FUSE: Filesystem in Userspace
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/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
23 static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
24 struct fuse_page_desc **desc)
28 pages = kzalloc(npages * (sizeof(struct page *) +
29 sizeof(struct fuse_page_desc)), flags);
30 *desc = (void *) (pages + npages);
35 static int fuse_send_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
36 int opcode, struct fuse_open_out *outargp)
38 struct fuse_open_in inarg;
41 memset(&inarg, 0, sizeof(inarg));
42 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
43 if (!fm->fc->atomic_o_trunc)
44 inarg.flags &= ~O_TRUNC;
48 args.in_args[0].size = sizeof(inarg);
49 args.in_args[0].value = &inarg;
51 args.out_args[0].size = sizeof(*outargp);
52 args.out_args[0].value = outargp;
54 return fuse_simple_request(fm, &args);
57 struct fuse_release_args {
58 struct fuse_args args;
59 struct fuse_release_in inarg;
63 struct fuse_file *fuse_file_alloc(struct fuse_mount *fm)
67 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
72 ff->release_args = kzalloc(sizeof(*ff->release_args),
74 if (!ff->release_args) {
79 INIT_LIST_HEAD(&ff->write_entry);
80 mutex_init(&ff->readdir.lock);
81 refcount_set(&ff->count, 1);
82 RB_CLEAR_NODE(&ff->polled_node);
83 init_waitqueue_head(&ff->poll_wait);
85 ff->kh = atomic64_inc_return(&fm->fc->khctr);
90 void fuse_file_free(struct fuse_file *ff)
92 kfree(ff->release_args);
93 mutex_destroy(&ff->readdir.lock);
97 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
99 refcount_inc(&ff->count);
103 static void fuse_release_end(struct fuse_mount *fm, struct fuse_args *args,
106 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
112 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
114 if (refcount_dec_and_test(&ff->count)) {
115 struct fuse_args *args = &ff->release_args->args;
117 if (isdir ? ff->fm->fc->no_opendir : ff->fm->fc->no_open) {
118 /* Do nothing when client does not implement 'open' */
119 fuse_release_end(ff->fm, args, 0);
121 fuse_simple_request(ff->fm, args);
122 fuse_release_end(ff->fm, args, 0);
124 args->end = fuse_release_end;
125 if (fuse_simple_background(ff->fm, args,
126 GFP_KERNEL | __GFP_NOFAIL))
127 fuse_release_end(ff->fm, args, -ENOTCONN);
133 int fuse_do_open(struct fuse_mount *fm, u64 nodeid, struct file *file,
136 struct fuse_conn *fc = fm->fc;
137 struct fuse_file *ff;
138 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
140 ff = fuse_file_alloc(fm);
145 /* Default for no-open */
146 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
147 if (isdir ? !fc->no_opendir : !fc->no_open) {
148 struct fuse_open_out outarg;
151 err = fuse_send_open(fm, nodeid, file, opcode, &outarg);
154 ff->open_flags = outarg.open_flags;
156 } else if (err != -ENOSYS) {
168 ff->open_flags &= ~FOPEN_DIRECT_IO;
171 file->private_data = ff;
175 EXPORT_SYMBOL_GPL(fuse_do_open);
177 static void fuse_link_write_file(struct file *file)
179 struct inode *inode = file_inode(file);
180 struct fuse_inode *fi = get_fuse_inode(inode);
181 struct fuse_file *ff = file->private_data;
183 * file may be written through mmap, so chain it onto the
184 * inodes's write_file list
186 spin_lock(&fi->lock);
187 if (list_empty(&ff->write_entry))
188 list_add(&ff->write_entry, &fi->write_files);
189 spin_unlock(&fi->lock);
192 void fuse_finish_open(struct inode *inode, struct file *file)
194 struct fuse_file *ff = file->private_data;
195 struct fuse_conn *fc = get_fuse_conn(inode);
197 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
198 invalidate_inode_pages2(inode->i_mapping);
199 if (ff->open_flags & FOPEN_STREAM)
200 stream_open(inode, file);
201 else if (ff->open_flags & FOPEN_NONSEEKABLE)
202 nonseekable_open(inode, file);
203 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
204 struct fuse_inode *fi = get_fuse_inode(inode);
206 spin_lock(&fi->lock);
207 fi->attr_version = atomic64_inc_return(&fc->attr_version);
208 i_size_write(inode, 0);
209 spin_unlock(&fi->lock);
210 fuse_invalidate_attr(inode);
211 if (fc->writeback_cache)
212 file_update_time(file);
214 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
215 fuse_link_write_file(file);
218 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
220 struct fuse_mount *fm = get_fuse_mount(inode);
221 struct fuse_conn *fc = fm->fc;
223 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
224 fc->atomic_o_trunc &&
226 bool dax_truncate = (file->f_flags & O_TRUNC) &&
227 fc->atomic_o_trunc && FUSE_IS_DAX(inode);
229 err = generic_file_open(inode, file);
233 if (is_wb_truncate || dax_truncate) {
235 fuse_set_nowrite(inode);
239 down_write(&get_fuse_inode(inode)->i_mmap_sem);
240 err = fuse_dax_break_layouts(inode, 0, 0);
245 err = fuse_do_open(fm, get_node_id(inode), file, isdir);
247 fuse_finish_open(inode, file);
251 up_write(&get_fuse_inode(inode)->i_mmap_sem);
253 if (is_wb_truncate | dax_truncate) {
254 fuse_release_nowrite(inode);
261 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
262 int flags, int opcode)
264 struct fuse_conn *fc = ff->fm->fc;
265 struct fuse_release_args *ra = ff->release_args;
267 /* Inode is NULL on error path of fuse_create_open() */
269 spin_lock(&fi->lock);
270 list_del(&ff->write_entry);
271 spin_unlock(&fi->lock);
273 spin_lock(&fc->lock);
274 if (!RB_EMPTY_NODE(&ff->polled_node))
275 rb_erase(&ff->polled_node, &fc->polled_files);
276 spin_unlock(&fc->lock);
278 wake_up_interruptible_all(&ff->poll_wait);
280 ra->inarg.fh = ff->fh;
281 ra->inarg.flags = flags;
282 ra->args.in_numargs = 1;
283 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
284 ra->args.in_args[0].value = &ra->inarg;
285 ra->args.opcode = opcode;
286 ra->args.nodeid = ff->nodeid;
287 ra->args.force = true;
288 ra->args.nocreds = true;
291 void fuse_release_common(struct file *file, bool isdir)
293 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
294 struct fuse_file *ff = file->private_data;
295 struct fuse_release_args *ra = ff->release_args;
296 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
298 fuse_prepare_release(fi, ff, file->f_flags, opcode);
301 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
302 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fm->fc,
305 /* Hold inode until release is finished */
306 ra->inode = igrab(file_inode(file));
309 * Normally this will send the RELEASE request, however if
310 * some asynchronous READ or WRITE requests are outstanding,
311 * the sending will be delayed.
313 * Make the release synchronous if this is a fuseblk mount,
314 * synchronous RELEASE is allowed (and desirable) in this case
315 * because the server can be trusted not to screw up.
317 fuse_file_put(ff, ff->fm->fc->destroy, isdir);
320 static int fuse_open(struct inode *inode, struct file *file)
322 return fuse_open_common(inode, file, false);
325 static int fuse_release(struct inode *inode, struct file *file)
327 struct fuse_conn *fc = get_fuse_conn(inode);
329 /* see fuse_vma_close() for !writeback_cache case */
330 if (fc->writeback_cache)
331 write_inode_now(inode, 1);
333 fuse_release_common(file, false);
335 /* return value is ignored by VFS */
339 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
341 WARN_ON(refcount_read(&ff->count) > 1);
342 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
344 * iput(NULL) is a no-op and since the refcount is 1 and everything's
345 * synchronous, we are fine with not doing igrab() here"
347 fuse_file_put(ff, true, false);
349 EXPORT_SYMBOL_GPL(fuse_sync_release);
352 * Scramble the ID space with XTEA, so that the value of the files_struct
353 * pointer is not exposed to userspace.
355 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
357 u32 *k = fc->scramble_key;
358 u64 v = (unsigned long) id;
364 for (i = 0; i < 32; i++) {
365 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
367 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
370 return (u64) v0 + ((u64) v1 << 32);
373 struct fuse_writepage_args {
374 struct fuse_io_args ia;
375 struct rb_node writepages_entry;
376 struct list_head queue_entry;
377 struct fuse_writepage_args *next;
381 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
382 pgoff_t idx_from, pgoff_t idx_to)
386 n = fi->writepages.rb_node;
389 struct fuse_writepage_args *wpa;
392 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
393 WARN_ON(get_fuse_inode(wpa->inode) != fi);
394 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
395 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
397 else if (idx_to < curr_index)
406 * Check if any page in a range is under writeback
408 * This is currently done by walking the list of writepage requests
409 * for the inode, which can be pretty inefficient.
411 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
414 struct fuse_inode *fi = get_fuse_inode(inode);
417 spin_lock(&fi->lock);
418 found = fuse_find_writeback(fi, idx_from, idx_to);
419 spin_unlock(&fi->lock);
424 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
426 return fuse_range_is_writeback(inode, index, index);
430 * Wait for page writeback to be completed.
432 * Since fuse doesn't rely on the VM writeback tracking, this has to
433 * use some other means.
435 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
437 struct fuse_inode *fi = get_fuse_inode(inode);
439 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
443 * Wait for all pending writepages on the inode to finish.
445 * This is currently done by blocking further writes with FUSE_NOWRITE
446 * and waiting for all sent writes to complete.
448 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
449 * could conflict with truncation.
451 static void fuse_sync_writes(struct inode *inode)
453 fuse_set_nowrite(inode);
454 fuse_release_nowrite(inode);
457 static int fuse_flush(struct file *file, fl_owner_t id)
459 struct inode *inode = file_inode(file);
460 struct fuse_mount *fm = get_fuse_mount(inode);
461 struct fuse_file *ff = file->private_data;
462 struct fuse_flush_in inarg;
466 if (is_bad_inode(inode))
469 err = write_inode_now(inode, 1);
474 fuse_sync_writes(inode);
477 err = filemap_check_errors(file->f_mapping);
482 if (fm->fc->no_flush)
485 memset(&inarg, 0, sizeof(inarg));
487 inarg.lock_owner = fuse_lock_owner_id(fm->fc, id);
488 args.opcode = FUSE_FLUSH;
489 args.nodeid = get_node_id(inode);
491 args.in_args[0].size = sizeof(inarg);
492 args.in_args[0].value = &inarg;
495 err = fuse_simple_request(fm, &args);
496 if (err == -ENOSYS) {
497 fm->fc->no_flush = 1;
503 * In memory i_blocks is not maintained by fuse, if writeback cache is
504 * enabled, i_blocks from cached attr may not be accurate.
506 if (!err && fm->fc->writeback_cache)
507 fuse_invalidate_attr(inode);
511 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
512 int datasync, int opcode)
514 struct inode *inode = file->f_mapping->host;
515 struct fuse_mount *fm = get_fuse_mount(inode);
516 struct fuse_file *ff = file->private_data;
518 struct fuse_fsync_in inarg;
520 memset(&inarg, 0, sizeof(inarg));
522 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
523 args.opcode = opcode;
524 args.nodeid = get_node_id(inode);
526 args.in_args[0].size = sizeof(inarg);
527 args.in_args[0].value = &inarg;
528 return fuse_simple_request(fm, &args);
531 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
534 struct inode *inode = file->f_mapping->host;
535 struct fuse_conn *fc = get_fuse_conn(inode);
538 if (is_bad_inode(inode))
544 * Start writeback against all dirty pages of the inode, then
545 * wait for all outstanding writes, before sending the FSYNC
548 err = file_write_and_wait_range(file, start, end);
552 fuse_sync_writes(inode);
555 * Due to implementation of fuse writeback
556 * file_write_and_wait_range() does not catch errors.
557 * We have to do this directly after fuse_sync_writes()
559 err = file_check_and_advance_wb_err(file);
563 err = sync_inode_metadata(inode, 1);
570 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
571 if (err == -ENOSYS) {
581 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
582 size_t count, int opcode)
584 struct fuse_file *ff = file->private_data;
585 struct fuse_args *args = &ia->ap.args;
587 ia->read.in.fh = ff->fh;
588 ia->read.in.offset = pos;
589 ia->read.in.size = count;
590 ia->read.in.flags = file->f_flags;
591 args->opcode = opcode;
592 args->nodeid = ff->nodeid;
593 args->in_numargs = 1;
594 args->in_args[0].size = sizeof(ia->read.in);
595 args->in_args[0].value = &ia->read.in;
596 args->out_argvar = true;
597 args->out_numargs = 1;
598 args->out_args[0].size = count;
601 static void fuse_release_user_pages(struct fuse_args_pages *ap,
606 for (i = 0; i < ap->num_pages; i++) {
608 set_page_dirty_lock(ap->pages[i]);
609 put_page(ap->pages[i]);
613 static void fuse_io_release(struct kref *kref)
615 kfree(container_of(kref, struct fuse_io_priv, refcnt));
618 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
623 if (io->bytes >= 0 && io->write)
626 return io->bytes < 0 ? io->size : io->bytes;
630 * In case of short read, the caller sets 'pos' to the position of
631 * actual end of fuse request in IO request. Otherwise, if bytes_requested
632 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
635 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
636 * both submitted asynchronously. The first of them was ACKed by userspace as
637 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
638 * second request was ACKed as short, e.g. only 1K was read, resulting in
641 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
642 * will be equal to the length of the longest contiguous fragment of
643 * transferred data starting from the beginning of IO request.
645 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
649 spin_lock(&io->lock);
651 io->err = io->err ? : err;
652 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
656 if (!left && io->blocking)
658 spin_unlock(&io->lock);
660 if (!left && !io->blocking) {
661 ssize_t res = fuse_get_res_by_io(io);
664 struct inode *inode = file_inode(io->iocb->ki_filp);
665 struct fuse_conn *fc = get_fuse_conn(inode);
666 struct fuse_inode *fi = get_fuse_inode(inode);
668 spin_lock(&fi->lock);
669 fi->attr_version = atomic64_inc_return(&fc->attr_version);
670 spin_unlock(&fi->lock);
673 io->iocb->ki_complete(io->iocb, res, 0);
676 kref_put(&io->refcnt, fuse_io_release);
679 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
682 struct fuse_io_args *ia;
684 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
687 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
697 static void fuse_io_free(struct fuse_io_args *ia)
703 static void fuse_aio_complete_req(struct fuse_mount *fm, struct fuse_args *args,
706 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
707 struct fuse_io_priv *io = ia->io;
710 fuse_release_user_pages(&ia->ap, io->should_dirty);
714 } else if (io->write) {
715 if (ia->write.out.size > ia->write.in.size) {
717 } else if (ia->write.in.size != ia->write.out.size) {
718 pos = ia->write.in.offset - io->offset +
722 u32 outsize = args->out_args[0].size;
724 if (ia->read.in.size != outsize)
725 pos = ia->read.in.offset - io->offset + outsize;
728 fuse_aio_complete(io, err, pos);
732 static ssize_t fuse_async_req_send(struct fuse_mount *fm,
733 struct fuse_io_args *ia, size_t num_bytes)
736 struct fuse_io_priv *io = ia->io;
738 spin_lock(&io->lock);
739 kref_get(&io->refcnt);
740 io->size += num_bytes;
742 spin_unlock(&io->lock);
744 ia->ap.args.end = fuse_aio_complete_req;
745 ia->ap.args.may_block = io->should_dirty;
746 err = fuse_simple_background(fm, &ia->ap.args, GFP_KERNEL);
748 fuse_aio_complete_req(fm, &ia->ap.args, err);
753 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
756 struct file *file = ia->io->iocb->ki_filp;
757 struct fuse_file *ff = file->private_data;
758 struct fuse_mount *fm = ff->fm;
760 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
762 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
763 ia->read.in.lock_owner = fuse_lock_owner_id(fm->fc, owner);
767 return fuse_async_req_send(fm, ia, count);
769 return fuse_simple_request(fm, &ia->ap.args);
772 static void fuse_read_update_size(struct inode *inode, loff_t size,
775 struct fuse_conn *fc = get_fuse_conn(inode);
776 struct fuse_inode *fi = get_fuse_inode(inode);
778 spin_lock(&fi->lock);
779 if (attr_ver == fi->attr_version && size < inode->i_size &&
780 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
781 fi->attr_version = atomic64_inc_return(&fc->attr_version);
782 i_size_write(inode, size);
784 spin_unlock(&fi->lock);
787 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
788 struct fuse_args_pages *ap)
790 struct fuse_conn *fc = get_fuse_conn(inode);
792 if (fc->writeback_cache) {
794 * A hole in a file. Some data after the hole are in page cache,
795 * but have not reached the client fs yet. So, the hole is not
799 int start_idx = num_read >> PAGE_SHIFT;
800 size_t off = num_read & (PAGE_SIZE - 1);
802 for (i = start_idx; i < ap->num_pages; i++) {
803 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
807 loff_t pos = page_offset(ap->pages[0]) + num_read;
808 fuse_read_update_size(inode, pos, attr_ver);
812 static int fuse_do_readpage(struct file *file, struct page *page)
814 struct inode *inode = page->mapping->host;
815 struct fuse_mount *fm = get_fuse_mount(inode);
816 loff_t pos = page_offset(page);
817 struct fuse_page_desc desc = { .length = PAGE_SIZE };
818 struct fuse_io_args ia = {
819 .ap.args.page_zeroing = true,
820 .ap.args.out_pages = true,
829 * Page writeback can extend beyond the lifetime of the
830 * page-cache page, so make sure we read a properly synced
833 fuse_wait_on_page_writeback(inode, page->index);
835 attr_ver = fuse_get_attr_version(fm->fc);
837 /* Don't overflow end offset */
838 if (pos + (desc.length - 1) == LLONG_MAX)
841 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
842 res = fuse_simple_request(fm, &ia.ap.args);
846 * Short read means EOF. If file size is larger, truncate it
848 if (res < desc.length)
849 fuse_short_read(inode, attr_ver, res, &ia.ap);
851 SetPageUptodate(page);
856 static int fuse_readpage(struct file *file, struct page *page)
858 struct inode *inode = page->mapping->host;
862 if (is_bad_inode(inode))
865 err = fuse_do_readpage(file, page);
866 fuse_invalidate_atime(inode);
872 static void fuse_readpages_end(struct fuse_mount *fm, struct fuse_args *args,
876 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
877 struct fuse_args_pages *ap = &ia->ap;
878 size_t count = ia->read.in.size;
879 size_t num_read = args->out_args[0].size;
880 struct address_space *mapping = NULL;
882 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
883 mapping = ap->pages[i]->mapping;
886 struct inode *inode = mapping->host;
889 * Short read means EOF. If file size is larger, truncate it
891 if (!err && num_read < count)
892 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
894 fuse_invalidate_atime(inode);
897 for (i = 0; i < ap->num_pages; i++) {
898 struct page *page = ap->pages[i];
901 SetPageUptodate(page);
908 fuse_file_put(ia->ff, false, false);
913 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
915 struct fuse_file *ff = file->private_data;
916 struct fuse_mount *fm = ff->fm;
917 struct fuse_args_pages *ap = &ia->ap;
918 loff_t pos = page_offset(ap->pages[0]);
919 size_t count = ap->num_pages << PAGE_SHIFT;
923 ap->args.out_pages = true;
924 ap->args.page_zeroing = true;
925 ap->args.page_replace = true;
927 /* Don't overflow end offset */
928 if (pos + (count - 1) == LLONG_MAX) {
930 ap->descs[ap->num_pages - 1].length--;
932 WARN_ON((loff_t) (pos + count) < 0);
934 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
935 ia->read.attr_ver = fuse_get_attr_version(fm->fc);
936 if (fm->fc->async_read) {
937 ia->ff = fuse_file_get(ff);
938 ap->args.end = fuse_readpages_end;
939 err = fuse_simple_background(fm, &ap->args, GFP_KERNEL);
943 res = fuse_simple_request(fm, &ap->args);
944 err = res < 0 ? res : 0;
946 fuse_readpages_end(fm, &ap->args, err);
949 static void fuse_readahead(struct readahead_control *rac)
951 struct inode *inode = rac->mapping->host;
952 struct fuse_conn *fc = get_fuse_conn(inode);
953 unsigned int i, max_pages, nr_pages = 0;
955 if (is_bad_inode(inode))
958 max_pages = min_t(unsigned int, fc->max_pages,
959 fc->max_read / PAGE_SIZE);
962 struct fuse_io_args *ia;
963 struct fuse_args_pages *ap;
965 nr_pages = readahead_count(rac) - nr_pages;
966 if (nr_pages > max_pages)
967 nr_pages = max_pages;
970 ia = fuse_io_alloc(NULL, nr_pages);
974 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
975 for (i = 0; i < nr_pages; i++) {
976 fuse_wait_on_page_writeback(inode,
977 readahead_index(rac) + i);
978 ap->descs[i].length = PAGE_SIZE;
980 ap->num_pages = nr_pages;
981 fuse_send_readpages(ia, rac->file);
985 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
987 struct inode *inode = iocb->ki_filp->f_mapping->host;
988 struct fuse_conn *fc = get_fuse_conn(inode);
991 * In auto invalidate mode, always update attributes on read.
992 * Otherwise, only update if we attempt to read past EOF (to ensure
993 * i_size is up to date).
995 if (fc->auto_inval_data ||
996 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
998 err = fuse_update_attributes(inode, iocb->ki_filp);
1003 return generic_file_read_iter(iocb, to);
1006 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1007 loff_t pos, size_t count)
1009 struct fuse_args *args = &ia->ap.args;
1011 ia->write.in.fh = ff->fh;
1012 ia->write.in.offset = pos;
1013 ia->write.in.size = count;
1014 args->opcode = FUSE_WRITE;
1015 args->nodeid = ff->nodeid;
1016 args->in_numargs = 2;
1017 if (ff->fm->fc->minor < 9)
1018 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1020 args->in_args[0].size = sizeof(ia->write.in);
1021 args->in_args[0].value = &ia->write.in;
1022 args->in_args[1].size = count;
1023 args->out_numargs = 1;
1024 args->out_args[0].size = sizeof(ia->write.out);
1025 args->out_args[0].value = &ia->write.out;
1028 static unsigned int fuse_write_flags(struct kiocb *iocb)
1030 unsigned int flags = iocb->ki_filp->f_flags;
1032 if (iocb->ki_flags & IOCB_DSYNC)
1034 if (iocb->ki_flags & IOCB_SYNC)
1040 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1041 size_t count, fl_owner_t owner)
1043 struct kiocb *iocb = ia->io->iocb;
1044 struct file *file = iocb->ki_filp;
1045 struct fuse_file *ff = file->private_data;
1046 struct fuse_mount *fm = ff->fm;
1047 struct fuse_write_in *inarg = &ia->write.in;
1050 fuse_write_args_fill(ia, ff, pos, count);
1051 inarg->flags = fuse_write_flags(iocb);
1052 if (owner != NULL) {
1053 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1054 inarg->lock_owner = fuse_lock_owner_id(fm->fc, owner);
1058 return fuse_async_req_send(fm, ia, count);
1060 err = fuse_simple_request(fm, &ia->ap.args);
1061 if (!err && ia->write.out.size > count)
1064 return err ?: ia->write.out.size;
1067 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1069 struct fuse_conn *fc = get_fuse_conn(inode);
1070 struct fuse_inode *fi = get_fuse_inode(inode);
1073 spin_lock(&fi->lock);
1074 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1075 if (pos > inode->i_size) {
1076 i_size_write(inode, pos);
1079 spin_unlock(&fi->lock);
1084 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1085 struct kiocb *iocb, struct inode *inode,
1086 loff_t pos, size_t count)
1088 struct fuse_args_pages *ap = &ia->ap;
1089 struct file *file = iocb->ki_filp;
1090 struct fuse_file *ff = file->private_data;
1091 struct fuse_mount *fm = ff->fm;
1092 unsigned int offset, i;
1095 for (i = 0; i < ap->num_pages; i++)
1096 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1098 fuse_write_args_fill(ia, ff, pos, count);
1099 ia->write.in.flags = fuse_write_flags(iocb);
1101 err = fuse_simple_request(fm, &ap->args);
1102 if (!err && ia->write.out.size > count)
1105 offset = ap->descs[0].offset;
1106 count = ia->write.out.size;
1107 for (i = 0; i < ap->num_pages; i++) {
1108 struct page *page = ap->pages[i];
1110 if (!err && !offset && count >= PAGE_SIZE)
1111 SetPageUptodate(page);
1113 if (count > PAGE_SIZE - offset)
1114 count -= PAGE_SIZE - offset;
1126 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1127 struct address_space *mapping,
1128 struct iov_iter *ii, loff_t pos,
1129 unsigned int max_pages)
1131 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1132 unsigned offset = pos & (PAGE_SIZE - 1);
1136 ap->args.in_pages = true;
1137 ap->descs[0].offset = offset;
1142 pgoff_t index = pos >> PAGE_SHIFT;
1143 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1144 iov_iter_count(ii));
1146 bytes = min_t(size_t, bytes, fc->max_write - count);
1150 if (iov_iter_fault_in_readable(ii, bytes))
1154 page = grab_cache_page_write_begin(mapping, index, 0);
1158 if (mapping_writably_mapped(mapping))
1159 flush_dcache_page(page);
1161 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1162 flush_dcache_page(page);
1164 iov_iter_advance(ii, tmp);
1168 bytes = min(bytes, iov_iter_single_seg_count(ii));
1173 ap->pages[ap->num_pages] = page;
1174 ap->descs[ap->num_pages].length = tmp;
1180 if (offset == PAGE_SIZE)
1183 if (!fc->big_writes)
1185 } while (iov_iter_count(ii) && count < fc->max_write &&
1186 ap->num_pages < max_pages && offset == 0);
1188 return count > 0 ? count : err;
1191 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1192 unsigned int max_pages)
1194 return min_t(unsigned int,
1195 ((pos + len - 1) >> PAGE_SHIFT) -
1196 (pos >> PAGE_SHIFT) + 1,
1200 static ssize_t fuse_perform_write(struct kiocb *iocb,
1201 struct address_space *mapping,
1202 struct iov_iter *ii, loff_t pos)
1204 struct inode *inode = mapping->host;
1205 struct fuse_conn *fc = get_fuse_conn(inode);
1206 struct fuse_inode *fi = get_fuse_inode(inode);
1210 if (inode->i_size < pos + iov_iter_count(ii))
1211 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1215 struct fuse_io_args ia = {};
1216 struct fuse_args_pages *ap = &ia.ap;
1217 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1220 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1226 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1230 err = fuse_send_write_pages(&ia, iocb, inode,
1233 size_t num_written = ia.write.out.size;
1238 /* break out of the loop on short write */
1239 if (num_written != count)
1244 } while (!err && iov_iter_count(ii));
1247 fuse_write_update_size(inode, pos);
1249 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1250 fuse_invalidate_attr(inode);
1252 return res > 0 ? res : err;
1255 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1257 struct file *file = iocb->ki_filp;
1258 struct address_space *mapping = file->f_mapping;
1259 ssize_t written = 0;
1260 ssize_t written_buffered = 0;
1261 struct inode *inode = mapping->host;
1265 if (get_fuse_conn(inode)->writeback_cache) {
1266 /* Update size (EOF optimization) and mode (SUID clearing) */
1267 err = fuse_update_attributes(mapping->host, file);
1271 return generic_file_write_iter(iocb, from);
1276 /* We can write back this queue in page reclaim */
1277 current->backing_dev_info = inode_to_bdi(inode);
1279 err = generic_write_checks(iocb, from);
1283 err = file_remove_privs(file);
1287 err = file_update_time(file);
1291 if (iocb->ki_flags & IOCB_DIRECT) {
1292 loff_t pos = iocb->ki_pos;
1293 written = generic_file_direct_write(iocb, from);
1294 if (written < 0 || !iov_iter_count(from))
1299 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1300 if (written_buffered < 0) {
1301 err = written_buffered;
1304 endbyte = pos + written_buffered - 1;
1306 err = filemap_write_and_wait_range(file->f_mapping, pos,
1311 invalidate_mapping_pages(file->f_mapping,
1313 endbyte >> PAGE_SHIFT);
1315 written += written_buffered;
1316 iocb->ki_pos = pos + written_buffered;
1318 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1320 iocb->ki_pos += written;
1323 current->backing_dev_info = NULL;
1324 inode_unlock(inode);
1326 written = generic_write_sync(iocb, written);
1328 return written ? written : err;
1331 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1333 unsigned int nr_pages)
1337 for (i = index; i < index + nr_pages; i++)
1338 descs[i].length = PAGE_SIZE - descs[i].offset;
1341 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1343 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1346 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1349 return min(iov_iter_single_seg_count(ii), max_size);
1352 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1353 size_t *nbytesp, int write,
1354 unsigned int max_pages)
1356 size_t nbytes = 0; /* # bytes already packed in req */
1359 /* Special case for kernel I/O: can copy directly into the buffer */
1360 if (iov_iter_is_kvec(ii)) {
1361 unsigned long user_addr = fuse_get_user_addr(ii);
1362 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1365 ap->args.in_args[1].value = (void *) user_addr;
1367 ap->args.out_args[0].value = (void *) user_addr;
1369 iov_iter_advance(ii, frag_size);
1370 *nbytesp = frag_size;
1374 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1377 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1379 max_pages - ap->num_pages,
1384 iov_iter_advance(ii, ret);
1388 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1390 ap->descs[ap->num_pages].offset = start;
1391 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1393 ap->num_pages += npages;
1394 ap->descs[ap->num_pages - 1].length -=
1395 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1399 ap->args.in_pages = true;
1401 ap->args.out_pages = true;
1405 return ret < 0 ? ret : 0;
1408 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1409 loff_t *ppos, int flags)
1411 int write = flags & FUSE_DIO_WRITE;
1412 int cuse = flags & FUSE_DIO_CUSE;
1413 struct file *file = io->iocb->ki_filp;
1414 struct inode *inode = file->f_mapping->host;
1415 struct fuse_file *ff = file->private_data;
1416 struct fuse_conn *fc = ff->fm->fc;
1417 size_t nmax = write ? fc->max_write : fc->max_read;
1419 size_t count = iov_iter_count(iter);
1420 pgoff_t idx_from = pos >> PAGE_SHIFT;
1421 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1424 struct fuse_io_args *ia;
1425 unsigned int max_pages;
1427 max_pages = iov_iter_npages(iter, fc->max_pages);
1428 ia = fuse_io_alloc(io, max_pages);
1433 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1436 fuse_sync_writes(inode);
1438 inode_unlock(inode);
1441 io->should_dirty = !write && iter_is_iovec(iter);
1444 fl_owner_t owner = current->files;
1445 size_t nbytes = min(count, nmax);
1447 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1453 if (!capable(CAP_FSETID))
1454 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1456 nres = fuse_send_write(ia, pos, nbytes, owner);
1458 nres = fuse_send_read(ia, pos, nbytes, owner);
1461 if (!io->async || nres < 0) {
1462 fuse_release_user_pages(&ia->ap, io->should_dirty);
1467 iov_iter_revert(iter, nbytes);
1471 WARN_ON(nres > nbytes);
1476 if (nres != nbytes) {
1477 iov_iter_revert(iter, nbytes - nres);
1481 max_pages = iov_iter_npages(iter, fc->max_pages);
1482 ia = fuse_io_alloc(io, max_pages);
1492 return res > 0 ? res : err;
1494 EXPORT_SYMBOL_GPL(fuse_direct_io);
1496 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1497 struct iov_iter *iter,
1501 struct inode *inode = file_inode(io->iocb->ki_filp);
1503 res = fuse_direct_io(io, iter, ppos, 0);
1505 fuse_invalidate_atime(inode);
1510 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1512 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1516 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1517 res = fuse_direct_IO(iocb, to);
1519 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1521 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1527 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1529 struct inode *inode = file_inode(iocb->ki_filp);
1530 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1533 /* Don't allow parallel writes to the same file */
1535 res = generic_write_checks(iocb, from);
1537 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1538 res = fuse_direct_IO(iocb, from);
1540 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1544 fuse_invalidate_attr(inode);
1546 fuse_write_update_size(inode, iocb->ki_pos);
1547 inode_unlock(inode);
1552 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1554 struct file *file = iocb->ki_filp;
1555 struct fuse_file *ff = file->private_data;
1556 struct inode *inode = file_inode(file);
1558 if (is_bad_inode(inode))
1561 if (FUSE_IS_DAX(inode))
1562 return fuse_dax_read_iter(iocb, to);
1564 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1565 return fuse_cache_read_iter(iocb, to);
1567 return fuse_direct_read_iter(iocb, to);
1570 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1572 struct file *file = iocb->ki_filp;
1573 struct fuse_file *ff = file->private_data;
1574 struct inode *inode = file_inode(file);
1576 if (is_bad_inode(inode))
1579 if (FUSE_IS_DAX(inode))
1580 return fuse_dax_write_iter(iocb, from);
1582 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1583 return fuse_cache_write_iter(iocb, from);
1585 return fuse_direct_write_iter(iocb, from);
1588 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1590 struct fuse_args_pages *ap = &wpa->ia.ap;
1593 for (i = 0; i < ap->num_pages; i++)
1594 __free_page(ap->pages[i]);
1597 fuse_file_put(wpa->ia.ff, false, false);
1603 static void fuse_writepage_finish(struct fuse_mount *fm,
1604 struct fuse_writepage_args *wpa)
1606 struct fuse_args_pages *ap = &wpa->ia.ap;
1607 struct inode *inode = wpa->inode;
1608 struct fuse_inode *fi = get_fuse_inode(inode);
1609 struct backing_dev_info *bdi = inode_to_bdi(inode);
1612 for (i = 0; i < ap->num_pages; i++) {
1613 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1614 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1615 wb_writeout_inc(&bdi->wb);
1617 wake_up(&fi->page_waitq);
1620 /* Called under fi->lock, may release and reacquire it */
1621 static void fuse_send_writepage(struct fuse_mount *fm,
1622 struct fuse_writepage_args *wpa, loff_t size)
1623 __releases(fi->lock)
1624 __acquires(fi->lock)
1626 struct fuse_writepage_args *aux, *next;
1627 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1628 struct fuse_write_in *inarg = &wpa->ia.write.in;
1629 struct fuse_args *args = &wpa->ia.ap.args;
1630 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1634 if (inarg->offset + data_size <= size) {
1635 inarg->size = data_size;
1636 } else if (inarg->offset < size) {
1637 inarg->size = size - inarg->offset;
1639 /* Got truncated off completely */
1643 args->in_args[1].size = inarg->size;
1645 args->nocreds = true;
1647 err = fuse_simple_background(fm, args, GFP_ATOMIC);
1648 if (err == -ENOMEM) {
1649 spin_unlock(&fi->lock);
1650 err = fuse_simple_background(fm, args, GFP_NOFS | __GFP_NOFAIL);
1651 spin_lock(&fi->lock);
1654 /* Fails on broken connection only */
1662 rb_erase(&wpa->writepages_entry, &fi->writepages);
1663 fuse_writepage_finish(fm, wpa);
1664 spin_unlock(&fi->lock);
1666 /* After fuse_writepage_finish() aux request list is private */
1667 for (aux = wpa->next; aux; aux = next) {
1670 fuse_writepage_free(aux);
1673 fuse_writepage_free(wpa);
1674 spin_lock(&fi->lock);
1678 * If fi->writectr is positive (no truncate or fsync going on) send
1679 * all queued writepage requests.
1681 * Called with fi->lock
1683 void fuse_flush_writepages(struct inode *inode)
1684 __releases(fi->lock)
1685 __acquires(fi->lock)
1687 struct fuse_mount *fm = get_fuse_mount(inode);
1688 struct fuse_inode *fi = get_fuse_inode(inode);
1689 loff_t crop = i_size_read(inode);
1690 struct fuse_writepage_args *wpa;
1692 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1693 wpa = list_entry(fi->queued_writes.next,
1694 struct fuse_writepage_args, queue_entry);
1695 list_del_init(&wpa->queue_entry);
1696 fuse_send_writepage(fm, wpa, crop);
1700 static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1701 struct fuse_writepage_args *wpa)
1703 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1704 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1705 struct rb_node **p = &root->rb_node;
1706 struct rb_node *parent = NULL;
1708 WARN_ON(!wpa->ia.ap.num_pages);
1710 struct fuse_writepage_args *curr;
1714 curr = rb_entry(parent, struct fuse_writepage_args,
1716 WARN_ON(curr->inode != wpa->inode);
1717 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1719 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1720 p = &(*p)->rb_right;
1721 else if (idx_to < curr_index)
1727 rb_link_node(&wpa->writepages_entry, parent, p);
1728 rb_insert_color(&wpa->writepages_entry, root);
1732 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1734 WARN_ON(fuse_insert_writeback(root, wpa));
1737 static void fuse_writepage_end(struct fuse_mount *fm, struct fuse_args *args,
1740 struct fuse_writepage_args *wpa =
1741 container_of(args, typeof(*wpa), ia.ap.args);
1742 struct inode *inode = wpa->inode;
1743 struct fuse_inode *fi = get_fuse_inode(inode);
1745 mapping_set_error(inode->i_mapping, error);
1746 spin_lock(&fi->lock);
1747 rb_erase(&wpa->writepages_entry, &fi->writepages);
1749 struct fuse_mount *fm = get_fuse_mount(inode);
1750 struct fuse_write_in *inarg = &wpa->ia.write.in;
1751 struct fuse_writepage_args *next = wpa->next;
1753 wpa->next = next->next;
1755 next->ia.ff = fuse_file_get(wpa->ia.ff);
1756 tree_insert(&fi->writepages, next);
1759 * Skip fuse_flush_writepages() to make it easy to crop requests
1760 * based on primary request size.
1762 * 1st case (trivial): there are no concurrent activities using
1763 * fuse_set/release_nowrite. Then we're on safe side because
1764 * fuse_flush_writepages() would call fuse_send_writepage()
1767 * 2nd case: someone called fuse_set_nowrite and it is waiting
1768 * now for completion of all in-flight requests. This happens
1769 * rarely and no more than once per page, so this should be
1772 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1773 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1774 * that fuse_set_nowrite returned implies that all in-flight
1775 * requests were completed along with all of their secondary
1776 * requests. Further primary requests are blocked by negative
1777 * writectr. Hence there cannot be any in-flight requests and
1778 * no invocations of fuse_writepage_end() while we're in
1779 * fuse_set_nowrite..fuse_release_nowrite section.
1781 fuse_send_writepage(fm, next, inarg->offset + inarg->size);
1784 fuse_writepage_finish(fm, wpa);
1785 spin_unlock(&fi->lock);
1786 fuse_writepage_free(wpa);
1789 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1790 struct fuse_inode *fi)
1792 struct fuse_file *ff = NULL;
1794 spin_lock(&fi->lock);
1795 if (!list_empty(&fi->write_files)) {
1796 ff = list_entry(fi->write_files.next, struct fuse_file,
1800 spin_unlock(&fi->lock);
1805 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1806 struct fuse_inode *fi)
1808 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1813 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1815 struct fuse_conn *fc = get_fuse_conn(inode);
1816 struct fuse_inode *fi = get_fuse_inode(inode);
1817 struct fuse_file *ff;
1820 ff = __fuse_write_file_get(fc, fi);
1821 err = fuse_flush_times(inode, ff);
1823 fuse_file_put(ff, false, false);
1828 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1830 struct fuse_writepage_args *wpa;
1831 struct fuse_args_pages *ap;
1833 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1837 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1847 static int fuse_writepage_locked(struct page *page)
1849 struct address_space *mapping = page->mapping;
1850 struct inode *inode = mapping->host;
1851 struct fuse_conn *fc = get_fuse_conn(inode);
1852 struct fuse_inode *fi = get_fuse_inode(inode);
1853 struct fuse_writepage_args *wpa;
1854 struct fuse_args_pages *ap;
1855 struct page *tmp_page;
1856 int error = -ENOMEM;
1858 set_page_writeback(page);
1860 wpa = fuse_writepage_args_alloc();
1865 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1870 wpa->ia.ff = fuse_write_file_get(fc, fi);
1874 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1876 copy_highpage(tmp_page, page);
1877 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1879 ap->args.in_pages = true;
1881 ap->pages[0] = tmp_page;
1882 ap->descs[0].offset = 0;
1883 ap->descs[0].length = PAGE_SIZE;
1884 ap->args.end = fuse_writepage_end;
1887 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1888 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1890 spin_lock(&fi->lock);
1891 tree_insert(&fi->writepages, wpa);
1892 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1893 fuse_flush_writepages(inode);
1894 spin_unlock(&fi->lock);
1896 end_page_writeback(page);
1901 __free_page(tmp_page);
1905 mapping_set_error(page->mapping, error);
1906 end_page_writeback(page);
1910 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1914 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1916 * ->writepages() should be called for sync() and friends. We
1917 * should only get here on direct reclaim and then we are
1918 * allowed to skip a page which is already in flight
1920 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1922 redirty_page_for_writepage(wbc, page);
1927 err = fuse_writepage_locked(page);
1933 struct fuse_fill_wb_data {
1934 struct fuse_writepage_args *wpa;
1935 struct fuse_file *ff;
1936 struct inode *inode;
1937 struct page **orig_pages;
1938 unsigned int max_pages;
1941 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1943 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1944 struct fuse_conn *fc = get_fuse_conn(data->inode);
1945 struct page **pages;
1946 struct fuse_page_desc *descs;
1947 unsigned int npages = min_t(unsigned int,
1948 max_t(unsigned int, data->max_pages * 2,
1949 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1951 WARN_ON(npages <= data->max_pages);
1953 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1957 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1958 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1962 data->max_pages = npages;
1967 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1969 struct fuse_writepage_args *wpa = data->wpa;
1970 struct inode *inode = data->inode;
1971 struct fuse_inode *fi = get_fuse_inode(inode);
1972 int num_pages = wpa->ia.ap.num_pages;
1975 wpa->ia.ff = fuse_file_get(data->ff);
1976 spin_lock(&fi->lock);
1977 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1978 fuse_flush_writepages(inode);
1979 spin_unlock(&fi->lock);
1981 for (i = 0; i < num_pages; i++)
1982 end_page_writeback(data->orig_pages[i]);
1986 * Check under fi->lock if the page is under writeback, and insert it onto the
1987 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
1988 * one already added for a page at this offset. If there's none, then insert
1989 * this new request onto the auxiliary list, otherwise reuse the existing one by
1990 * swapping the new temp page with the old one.
1992 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
1995 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1996 struct fuse_writepage_args *tmp;
1997 struct fuse_writepage_args *old_wpa;
1998 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
2000 WARN_ON(new_ap->num_pages != 0);
2001 new_ap->num_pages = 1;
2003 spin_lock(&fi->lock);
2004 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
2006 spin_unlock(&fi->lock);
2010 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
2013 WARN_ON(tmp->inode != new_wpa->inode);
2014 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
2015 if (curr_index == page->index) {
2016 WARN_ON(tmp->ia.ap.num_pages != 1);
2017 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2023 new_wpa->next = old_wpa->next;
2024 old_wpa->next = new_wpa;
2027 spin_unlock(&fi->lock);
2030 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2032 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2033 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2034 wb_writeout_inc(&bdi->wb);
2035 fuse_writepage_free(new_wpa);
2041 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2042 struct fuse_args_pages *ap,
2043 struct fuse_fill_wb_data *data)
2045 WARN_ON(!ap->num_pages);
2048 * Being under writeback is unlikely but possible. For example direct
2049 * read to an mmaped fuse file will set the page dirty twice; once when
2050 * the pages are faulted with get_user_pages(), and then after the read
2053 if (fuse_page_is_writeback(data->inode, page->index))
2056 /* Reached max pages */
2057 if (ap->num_pages == fc->max_pages)
2060 /* Reached max write bytes */
2061 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2065 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2068 /* Need to grow the pages array? If so, did the expansion fail? */
2069 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2075 static int fuse_writepages_fill(struct page *page,
2076 struct writeback_control *wbc, void *_data)
2078 struct fuse_fill_wb_data *data = _data;
2079 struct fuse_writepage_args *wpa = data->wpa;
2080 struct fuse_args_pages *ap = &wpa->ia.ap;
2081 struct inode *inode = data->inode;
2082 struct fuse_inode *fi = get_fuse_inode(inode);
2083 struct fuse_conn *fc = get_fuse_conn(inode);
2084 struct page *tmp_page;
2089 data->ff = fuse_write_file_get(fc, fi);
2094 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
2095 fuse_writepages_send(data);
2100 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2105 * The page must not be redirtied until the writeout is completed
2106 * (i.e. userspace has sent a reply to the write request). Otherwise
2107 * there could be more than one temporary page instance for each real
2110 * This is ensured by holding the page lock in page_mkwrite() while
2111 * checking fuse_page_is_writeback(). We already hold the page lock
2112 * since clear_page_dirty_for_io() and keep it held until we add the
2113 * request to the fi->writepages list and increment ap->num_pages.
2114 * After this fuse_page_is_writeback() will indicate that the page is
2115 * under writeback, so we can release the page lock.
2117 if (data->wpa == NULL) {
2119 wpa = fuse_writepage_args_alloc();
2121 __free_page(tmp_page);
2124 data->max_pages = 1;
2127 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2128 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2130 ap->args.in_pages = true;
2131 ap->args.end = fuse_writepage_end;
2135 set_page_writeback(page);
2137 copy_highpage(tmp_page, page);
2138 ap->pages[ap->num_pages] = tmp_page;
2139 ap->descs[ap->num_pages].offset = 0;
2140 ap->descs[ap->num_pages].length = PAGE_SIZE;
2141 data->orig_pages[ap->num_pages] = page;
2143 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2144 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2149 * Protected by fi->lock against concurrent access by
2150 * fuse_page_is_writeback().
2152 spin_lock(&fi->lock);
2154 spin_unlock(&fi->lock);
2155 } else if (fuse_writepage_add(wpa, page)) {
2158 end_page_writeback(page);
2166 static int fuse_writepages(struct address_space *mapping,
2167 struct writeback_control *wbc)
2169 struct inode *inode = mapping->host;
2170 struct fuse_conn *fc = get_fuse_conn(inode);
2171 struct fuse_fill_wb_data data;
2175 if (is_bad_inode(inode))
2183 data.orig_pages = kcalloc(fc->max_pages,
2184 sizeof(struct page *),
2186 if (!data.orig_pages)
2189 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2191 WARN_ON(!data.wpa->ia.ap.num_pages);
2192 fuse_writepages_send(&data);
2195 fuse_file_put(data.ff, false, false);
2197 kfree(data.orig_pages);
2203 * It's worthy to make sure that space is reserved on disk for the write,
2204 * but how to implement it without killing performance need more thinking.
2206 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2207 loff_t pos, unsigned len, unsigned flags,
2208 struct page **pagep, void **fsdata)
2210 pgoff_t index = pos >> PAGE_SHIFT;
2211 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2216 WARN_ON(!fc->writeback_cache);
2218 page = grab_cache_page_write_begin(mapping, index, flags);
2222 fuse_wait_on_page_writeback(mapping->host, page->index);
2224 if (PageUptodate(page) || len == PAGE_SIZE)
2227 * Check if the start this page comes after the end of file, in which
2228 * case the readpage can be optimized away.
2230 fsize = i_size_read(mapping->host);
2231 if (fsize <= (pos & PAGE_MASK)) {
2232 size_t off = pos & ~PAGE_MASK;
2234 zero_user_segment(page, 0, off);
2237 err = fuse_do_readpage(file, page);
2251 static int fuse_write_end(struct file *file, struct address_space *mapping,
2252 loff_t pos, unsigned len, unsigned copied,
2253 struct page *page, void *fsdata)
2255 struct inode *inode = page->mapping->host;
2257 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2261 if (!PageUptodate(page)) {
2262 /* Zero any unwritten bytes at the end of the page */
2263 size_t endoff = (pos + copied) & ~PAGE_MASK;
2265 zero_user_segment(page, endoff, PAGE_SIZE);
2266 SetPageUptodate(page);
2269 fuse_write_update_size(inode, pos + copied);
2270 set_page_dirty(page);
2279 static int fuse_launder_page(struct page *page)
2282 if (clear_page_dirty_for_io(page)) {
2283 struct inode *inode = page->mapping->host;
2284 err = fuse_writepage_locked(page);
2286 fuse_wait_on_page_writeback(inode, page->index);
2292 * Write back dirty pages now, because there may not be any suitable
2295 static void fuse_vma_close(struct vm_area_struct *vma)
2297 filemap_write_and_wait(vma->vm_file->f_mapping);
2301 * Wait for writeback against this page to complete before allowing it
2302 * to be marked dirty again, and hence written back again, possibly
2303 * before the previous writepage completed.
2305 * Block here, instead of in ->writepage(), so that the userspace fs
2306 * can only block processes actually operating on the filesystem.
2308 * Otherwise unprivileged userspace fs would be able to block
2313 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2315 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2317 struct page *page = vmf->page;
2318 struct inode *inode = file_inode(vmf->vma->vm_file);
2320 file_update_time(vmf->vma->vm_file);
2322 if (page->mapping != inode->i_mapping) {
2324 return VM_FAULT_NOPAGE;
2327 fuse_wait_on_page_writeback(inode, page->index);
2328 return VM_FAULT_LOCKED;
2331 static const struct vm_operations_struct fuse_file_vm_ops = {
2332 .close = fuse_vma_close,
2333 .fault = filemap_fault,
2334 .map_pages = filemap_map_pages,
2335 .page_mkwrite = fuse_page_mkwrite,
2338 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2340 struct fuse_file *ff = file->private_data;
2342 /* DAX mmap is superior to direct_io mmap */
2343 if (FUSE_IS_DAX(file_inode(file)))
2344 return fuse_dax_mmap(file, vma);
2346 if (ff->open_flags & FOPEN_DIRECT_IO) {
2347 /* Can't provide the coherency needed for MAP_SHARED */
2348 if (vma->vm_flags & VM_MAYSHARE)
2351 invalidate_inode_pages2(file->f_mapping);
2353 return generic_file_mmap(file, vma);
2356 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2357 fuse_link_write_file(file);
2359 file_accessed(file);
2360 vma->vm_ops = &fuse_file_vm_ops;
2364 static int convert_fuse_file_lock(struct fuse_conn *fc,
2365 const struct fuse_file_lock *ffl,
2366 struct file_lock *fl)
2368 switch (ffl->type) {
2374 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2375 ffl->end < ffl->start)
2378 fl->fl_start = ffl->start;
2379 fl->fl_end = ffl->end;
2382 * Convert pid into init's pid namespace. The locks API will
2383 * translate it into the caller's pid namespace.
2386 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2393 fl->fl_type = ffl->type;
2397 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2398 const struct file_lock *fl, int opcode, pid_t pid,
2399 int flock, struct fuse_lk_in *inarg)
2401 struct inode *inode = file_inode(file);
2402 struct fuse_conn *fc = get_fuse_conn(inode);
2403 struct fuse_file *ff = file->private_data;
2405 memset(inarg, 0, sizeof(*inarg));
2407 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2408 inarg->lk.start = fl->fl_start;
2409 inarg->lk.end = fl->fl_end;
2410 inarg->lk.type = fl->fl_type;
2411 inarg->lk.pid = pid;
2413 inarg->lk_flags |= FUSE_LK_FLOCK;
2414 args->opcode = opcode;
2415 args->nodeid = get_node_id(inode);
2416 args->in_numargs = 1;
2417 args->in_args[0].size = sizeof(*inarg);
2418 args->in_args[0].value = inarg;
2421 static int fuse_getlk(struct file *file, struct file_lock *fl)
2423 struct inode *inode = file_inode(file);
2424 struct fuse_mount *fm = get_fuse_mount(inode);
2426 struct fuse_lk_in inarg;
2427 struct fuse_lk_out outarg;
2430 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2431 args.out_numargs = 1;
2432 args.out_args[0].size = sizeof(outarg);
2433 args.out_args[0].value = &outarg;
2434 err = fuse_simple_request(fm, &args);
2436 err = convert_fuse_file_lock(fm->fc, &outarg.lk, fl);
2441 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2443 struct inode *inode = file_inode(file);
2444 struct fuse_mount *fm = get_fuse_mount(inode);
2446 struct fuse_lk_in inarg;
2447 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2448 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2449 pid_t pid_nr = pid_nr_ns(pid, fm->fc->pid_ns);
2452 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2453 /* NLM needs asynchronous locks, which we don't support yet */
2457 /* Unlock on close is handled by the flush method */
2458 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2461 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2462 err = fuse_simple_request(fm, &args);
2464 /* locking is restartable */
2471 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2473 struct inode *inode = file_inode(file);
2474 struct fuse_conn *fc = get_fuse_conn(inode);
2477 if (cmd == F_CANCELLK) {
2479 } else if (cmd == F_GETLK) {
2481 posix_test_lock(file, fl);
2484 err = fuse_getlk(file, fl);
2487 err = posix_lock_file(file, fl, NULL);
2489 err = fuse_setlk(file, fl, 0);
2494 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2496 struct inode *inode = file_inode(file);
2497 struct fuse_conn *fc = get_fuse_conn(inode);
2501 err = locks_lock_file_wait(file, fl);
2503 struct fuse_file *ff = file->private_data;
2505 /* emulate flock with POSIX locks */
2507 err = fuse_setlk(file, fl, 1);
2513 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2515 struct inode *inode = mapping->host;
2516 struct fuse_mount *fm = get_fuse_mount(inode);
2518 struct fuse_bmap_in inarg;
2519 struct fuse_bmap_out outarg;
2522 if (!inode->i_sb->s_bdev || fm->fc->no_bmap)
2525 memset(&inarg, 0, sizeof(inarg));
2526 inarg.block = block;
2527 inarg.blocksize = inode->i_sb->s_blocksize;
2528 args.opcode = FUSE_BMAP;
2529 args.nodeid = get_node_id(inode);
2530 args.in_numargs = 1;
2531 args.in_args[0].size = sizeof(inarg);
2532 args.in_args[0].value = &inarg;
2533 args.out_numargs = 1;
2534 args.out_args[0].size = sizeof(outarg);
2535 args.out_args[0].value = &outarg;
2536 err = fuse_simple_request(fm, &args);
2538 fm->fc->no_bmap = 1;
2540 return err ? 0 : outarg.block;
2543 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2545 struct inode *inode = file->f_mapping->host;
2546 struct fuse_mount *fm = get_fuse_mount(inode);
2547 struct fuse_file *ff = file->private_data;
2549 struct fuse_lseek_in inarg = {
2554 struct fuse_lseek_out outarg;
2557 if (fm->fc->no_lseek)
2560 args.opcode = FUSE_LSEEK;
2561 args.nodeid = ff->nodeid;
2562 args.in_numargs = 1;
2563 args.in_args[0].size = sizeof(inarg);
2564 args.in_args[0].value = &inarg;
2565 args.out_numargs = 1;
2566 args.out_args[0].size = sizeof(outarg);
2567 args.out_args[0].value = &outarg;
2568 err = fuse_simple_request(fm, &args);
2570 if (err == -ENOSYS) {
2571 fm->fc->no_lseek = 1;
2577 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2580 err = fuse_update_attributes(inode, file);
2582 return generic_file_llseek(file, offset, whence);
2587 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2590 struct inode *inode = file_inode(file);
2595 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2596 retval = generic_file_llseek(file, offset, whence);
2600 retval = fuse_update_attributes(inode, file);
2602 retval = generic_file_llseek(file, offset, whence);
2603 inode_unlock(inode);
2608 retval = fuse_lseek(file, offset, whence);
2609 inode_unlock(inode);
2619 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2620 * ABI was defined to be 'struct iovec' which is different on 32bit
2621 * and 64bit. Fortunately we can determine which structure the server
2622 * used from the size of the reply.
2624 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2625 size_t transferred, unsigned count,
2628 #ifdef CONFIG_COMPAT
2629 if (count * sizeof(struct compat_iovec) == transferred) {
2630 struct compat_iovec *ciov = src;
2634 * With this interface a 32bit server cannot support
2635 * non-compat (i.e. ones coming from 64bit apps) ioctl
2641 for (i = 0; i < count; i++) {
2642 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2643 dst[i].iov_len = ciov[i].iov_len;
2649 if (count * sizeof(struct iovec) != transferred)
2652 memcpy(dst, src, transferred);
2656 /* Make sure iov_length() won't overflow */
2657 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2661 u32 max = fc->max_pages << PAGE_SHIFT;
2663 for (n = 0; n < count; n++, iov++) {
2664 if (iov->iov_len > (size_t) max)
2666 max -= iov->iov_len;
2671 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2672 void *src, size_t transferred, unsigned count,
2676 struct fuse_ioctl_iovec *fiov = src;
2678 if (fc->minor < 16) {
2679 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2683 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2686 for (i = 0; i < count; i++) {
2687 /* Did the server supply an inappropriate value? */
2688 if (fiov[i].base != (unsigned long) fiov[i].base ||
2689 fiov[i].len != (unsigned long) fiov[i].len)
2692 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2693 dst[i].iov_len = (size_t) fiov[i].len;
2695 #ifdef CONFIG_COMPAT
2697 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2698 (compat_size_t) dst[i].iov_len != fiov[i].len))
2708 * For ioctls, there is no generic way to determine how much memory
2709 * needs to be read and/or written. Furthermore, ioctls are allowed
2710 * to dereference the passed pointer, so the parameter requires deep
2711 * copying but FUSE has no idea whatsoever about what to copy in or
2714 * This is solved by allowing FUSE server to retry ioctl with
2715 * necessary in/out iovecs. Let's assume the ioctl implementation
2716 * needs to read in the following structure.
2723 * On the first callout to FUSE server, inarg->in_size and
2724 * inarg->out_size will be NULL; then, the server completes the ioctl
2725 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2726 * the actual iov array to
2728 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2730 * which tells FUSE to copy in the requested area and retry the ioctl.
2731 * On the second round, the server has access to the structure and
2732 * from that it can tell what to look for next, so on the invocation,
2733 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2735 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2736 * { .iov_base = a.buf, .iov_len = a.buflen } }
2738 * FUSE will copy both struct a and the pointed buffer from the
2739 * process doing the ioctl and retry ioctl with both struct a and the
2742 * This time, FUSE server has everything it needs and completes ioctl
2743 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2745 * Copying data out works the same way.
2747 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2748 * automatically initializes in and out iovs by decoding @cmd with
2749 * _IOC_* macros and the server is not allowed to request RETRY. This
2750 * limits ioctl data transfers to well-formed ioctls and is the forced
2751 * behavior for all FUSE servers.
2753 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2756 struct fuse_file *ff = file->private_data;
2757 struct fuse_mount *fm = ff->fm;
2758 struct fuse_ioctl_in inarg = {
2764 struct fuse_ioctl_out outarg;
2765 struct iovec *iov_page = NULL;
2766 struct iovec *in_iov = NULL, *out_iov = NULL;
2767 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2768 size_t in_size, out_size, c;
2769 ssize_t transferred;
2772 struct fuse_args_pages ap = {};
2774 #if BITS_PER_LONG == 32
2775 inarg.flags |= FUSE_IOCTL_32BIT;
2777 if (flags & FUSE_IOCTL_COMPAT) {
2778 inarg.flags |= FUSE_IOCTL_32BIT;
2779 #ifdef CONFIG_X86_X32
2780 if (in_x32_syscall())
2781 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2786 /* assume all the iovs returned by client always fits in a page */
2787 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2790 ap.pages = fuse_pages_alloc(fm->fc->max_pages, GFP_KERNEL, &ap.descs);
2791 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2792 if (!ap.pages || !iov_page)
2795 fuse_page_descs_length_init(ap.descs, 0, fm->fc->max_pages);
2798 * If restricted, initialize IO parameters as encoded in @cmd.
2799 * RETRY from server is not allowed.
2801 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2802 struct iovec *iov = iov_page;
2804 iov->iov_base = (void __user *)arg;
2807 case FS_IOC_GETFLAGS:
2808 case FS_IOC_SETFLAGS:
2809 iov->iov_len = sizeof(int);
2812 iov->iov_len = _IOC_SIZE(cmd);
2816 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2821 if (_IOC_DIR(cmd) & _IOC_READ) {
2828 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2829 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2832 * Out data can be used either for actual out data or iovs,
2833 * make sure there always is at least one page.
2835 out_size = max_t(size_t, out_size, PAGE_SIZE);
2836 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2838 /* make sure there are enough buffer pages and init request with them */
2840 if (max_pages > fm->fc->max_pages)
2842 while (ap.num_pages < max_pages) {
2843 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2844 if (!ap.pages[ap.num_pages])
2850 /* okay, let's send it to the client */
2851 ap.args.opcode = FUSE_IOCTL;
2852 ap.args.nodeid = ff->nodeid;
2853 ap.args.in_numargs = 1;
2854 ap.args.in_args[0].size = sizeof(inarg);
2855 ap.args.in_args[0].value = &inarg;
2857 ap.args.in_numargs++;
2858 ap.args.in_args[1].size = in_size;
2859 ap.args.in_pages = true;
2862 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2863 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2864 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2865 if (c != PAGE_SIZE && iov_iter_count(&ii))
2870 ap.args.out_numargs = 2;
2871 ap.args.out_args[0].size = sizeof(outarg);
2872 ap.args.out_args[0].value = &outarg;
2873 ap.args.out_args[1].size = out_size;
2874 ap.args.out_pages = true;
2875 ap.args.out_argvar = true;
2877 transferred = fuse_simple_request(fm, &ap.args);
2879 if (transferred < 0)
2882 /* did it ask for retry? */
2883 if (outarg.flags & FUSE_IOCTL_RETRY) {
2886 /* no retry if in restricted mode */
2888 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2891 in_iovs = outarg.in_iovs;
2892 out_iovs = outarg.out_iovs;
2895 * Make sure things are in boundary, separate checks
2896 * are to protect against overflow.
2899 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2900 out_iovs > FUSE_IOCTL_MAX_IOV ||
2901 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2904 vaddr = kmap_atomic(ap.pages[0]);
2905 err = fuse_copy_ioctl_iovec(fm->fc, iov_page, vaddr,
2906 transferred, in_iovs + out_iovs,
2907 (flags & FUSE_IOCTL_COMPAT) != 0);
2908 kunmap_atomic(vaddr);
2913 out_iov = in_iov + in_iovs;
2915 err = fuse_verify_ioctl_iov(fm->fc, in_iov, in_iovs);
2919 err = fuse_verify_ioctl_iov(fm->fc, out_iov, out_iovs);
2927 if (transferred > inarg.out_size)
2931 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2932 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2933 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2934 if (c != PAGE_SIZE && iov_iter_count(&ii))
2939 free_page((unsigned long) iov_page);
2940 while (ap.num_pages)
2941 __free_page(ap.pages[--ap.num_pages]);
2944 return err ? err : outarg.result;
2946 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2948 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2949 unsigned long arg, unsigned int flags)
2951 struct inode *inode = file_inode(file);
2952 struct fuse_conn *fc = get_fuse_conn(inode);
2954 if (!fuse_allow_current_process(fc))
2957 if (is_bad_inode(inode))
2960 return fuse_do_ioctl(file, cmd, arg, flags);
2963 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2966 return fuse_ioctl_common(file, cmd, arg, 0);
2969 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2972 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2976 * All files which have been polled are linked to RB tree
2977 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2978 * find the matching one.
2980 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2981 struct rb_node **parent_out)
2983 struct rb_node **link = &fc->polled_files.rb_node;
2984 struct rb_node *last = NULL;
2987 struct fuse_file *ff;
2990 ff = rb_entry(last, struct fuse_file, polled_node);
2993 link = &last->rb_left;
2994 else if (kh > ff->kh)
2995 link = &last->rb_right;
3006 * The file is about to be polled. Make sure it's on the polled_files
3007 * RB tree. Note that files once added to the polled_files tree are
3008 * not removed before the file is released. This is because a file
3009 * polled once is likely to be polled again.
3011 static void fuse_register_polled_file(struct fuse_conn *fc,
3012 struct fuse_file *ff)
3014 spin_lock(&fc->lock);
3015 if (RB_EMPTY_NODE(&ff->polled_node)) {
3016 struct rb_node **link, *parent;
3018 link = fuse_find_polled_node(fc, ff->kh, &parent);
3020 rb_link_node(&ff->polled_node, parent, link);
3021 rb_insert_color(&ff->polled_node, &fc->polled_files);
3023 spin_unlock(&fc->lock);
3026 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
3028 struct fuse_file *ff = file->private_data;
3029 struct fuse_mount *fm = ff->fm;
3030 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
3031 struct fuse_poll_out outarg;
3035 if (fm->fc->no_poll)
3036 return DEFAULT_POLLMASK;
3038 poll_wait(file, &ff->poll_wait, wait);
3039 inarg.events = mangle_poll(poll_requested_events(wait));
3042 * Ask for notification iff there's someone waiting for it.
3043 * The client may ignore the flag and always notify.
3045 if (waitqueue_active(&ff->poll_wait)) {
3046 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
3047 fuse_register_polled_file(fm->fc, ff);
3050 args.opcode = FUSE_POLL;
3051 args.nodeid = ff->nodeid;
3052 args.in_numargs = 1;
3053 args.in_args[0].size = sizeof(inarg);
3054 args.in_args[0].value = &inarg;
3055 args.out_numargs = 1;
3056 args.out_args[0].size = sizeof(outarg);
3057 args.out_args[0].value = &outarg;
3058 err = fuse_simple_request(fm, &args);
3061 return demangle_poll(outarg.revents);
3062 if (err == -ENOSYS) {
3063 fm->fc->no_poll = 1;
3064 return DEFAULT_POLLMASK;
3068 EXPORT_SYMBOL_GPL(fuse_file_poll);
3071 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3072 * wakes up the poll waiters.
3074 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3075 struct fuse_notify_poll_wakeup_out *outarg)
3077 u64 kh = outarg->kh;
3078 struct rb_node **link;
3080 spin_lock(&fc->lock);
3082 link = fuse_find_polled_node(fc, kh, NULL);
3084 struct fuse_file *ff;
3086 ff = rb_entry(*link, struct fuse_file, polled_node);
3087 wake_up_interruptible_sync(&ff->poll_wait);
3090 spin_unlock(&fc->lock);
3094 static void fuse_do_truncate(struct file *file)
3096 struct inode *inode = file->f_mapping->host;
3099 attr.ia_valid = ATTR_SIZE;
3100 attr.ia_size = i_size_read(inode);
3102 attr.ia_file = file;
3103 attr.ia_valid |= ATTR_FILE;
3105 fuse_do_setattr(file_dentry(file), &attr, file);
3108 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3110 return round_up(off, fc->max_pages << PAGE_SHIFT);
3114 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3116 DECLARE_COMPLETION_ONSTACK(wait);
3118 struct file *file = iocb->ki_filp;
3119 struct fuse_file *ff = file->private_data;
3121 struct inode *inode;
3123 size_t count = iov_iter_count(iter), shortened = 0;
3124 loff_t offset = iocb->ki_pos;
3125 struct fuse_io_priv *io;
3128 inode = file->f_mapping->host;
3129 i_size = i_size_read(inode);
3131 if ((iov_iter_rw(iter) == READ) && (offset >= i_size))
3134 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3137 spin_lock_init(&io->lock);
3138 kref_init(&io->refcnt);
3142 io->offset = offset;
3143 io->write = (iov_iter_rw(iter) == WRITE);
3146 * By default, we want to optimize all I/Os with async request
3147 * submission to the client filesystem if supported.
3149 io->async = ff->fm->fc->async_dio;
3151 io->blocking = is_sync_kiocb(iocb);
3153 /* optimization for short read */
3154 if (io->async && !io->write && offset + count > i_size) {
3155 iov_iter_truncate(iter, fuse_round_up(ff->fm->fc, i_size - offset));
3156 shortened = count - iov_iter_count(iter);
3161 * We cannot asynchronously extend the size of a file.
3162 * In such case the aio will behave exactly like sync io.
3164 if ((offset + count > i_size) && io->write)
3165 io->blocking = true;
3167 if (io->async && io->blocking) {
3169 * Additional reference to keep io around after
3170 * calling fuse_aio_complete()
3172 kref_get(&io->refcnt);
3176 if (iov_iter_rw(iter) == WRITE) {
3177 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3178 fuse_invalidate_attr(inode);
3180 ret = __fuse_direct_read(io, iter, &pos);
3182 iov_iter_reexpand(iter, iov_iter_count(iter) + shortened);
3185 bool blocking = io->blocking;
3187 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3189 /* we have a non-extending, async request, so return */
3191 return -EIOCBQUEUED;
3193 wait_for_completion(&wait);
3194 ret = fuse_get_res_by_io(io);
3197 kref_put(&io->refcnt, fuse_io_release);
3199 if (iov_iter_rw(iter) == WRITE) {
3201 fuse_write_update_size(inode, pos);
3202 else if (ret < 0 && offset + count > i_size)
3203 fuse_do_truncate(file);
3209 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3211 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3214 fuse_sync_writes(inode);
3219 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3222 struct fuse_file *ff = file->private_data;
3223 struct inode *inode = file_inode(file);
3224 struct fuse_inode *fi = get_fuse_inode(inode);
3225 struct fuse_mount *fm = ff->fm;
3227 struct fuse_fallocate_in inarg = {
3234 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3235 (mode & FALLOC_FL_PUNCH_HOLE);
3237 bool block_faults = FUSE_IS_DAX(inode) && lock_inode;
3239 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3242 if (fm->fc->no_fallocate)
3248 down_write(&fi->i_mmap_sem);
3249 err = fuse_dax_break_layouts(inode, 0, 0);
3254 if (mode & FALLOC_FL_PUNCH_HOLE) {
3255 loff_t endbyte = offset + length - 1;
3257 err = fuse_writeback_range(inode, offset, endbyte);
3263 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3264 offset + length > i_size_read(inode)) {
3265 err = inode_newsize_ok(inode, offset + length);
3270 if (!(mode & FALLOC_FL_KEEP_SIZE))
3271 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3273 args.opcode = FUSE_FALLOCATE;
3274 args.nodeid = ff->nodeid;
3275 args.in_numargs = 1;
3276 args.in_args[0].size = sizeof(inarg);
3277 args.in_args[0].value = &inarg;
3278 err = fuse_simple_request(fm, &args);
3279 if (err == -ENOSYS) {
3280 fm->fc->no_fallocate = 1;
3286 /* we could have extended the file */
3287 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3288 bool changed = fuse_write_update_size(inode, offset + length);
3290 if (changed && fm->fc->writeback_cache)
3291 file_update_time(file);
3294 if (mode & FALLOC_FL_PUNCH_HOLE)
3295 truncate_pagecache_range(inode, offset, offset + length - 1);
3297 fuse_invalidate_attr(inode);
3300 if (!(mode & FALLOC_FL_KEEP_SIZE))
3301 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3304 up_write(&fi->i_mmap_sem);
3307 inode_unlock(inode);
3312 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3313 struct file *file_out, loff_t pos_out,
3314 size_t len, unsigned int flags)
3316 struct fuse_file *ff_in = file_in->private_data;
3317 struct fuse_file *ff_out = file_out->private_data;
3318 struct inode *inode_in = file_inode(file_in);
3319 struct inode *inode_out = file_inode(file_out);
3320 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3321 struct fuse_mount *fm = ff_in->fm;
3322 struct fuse_conn *fc = fm->fc;
3324 struct fuse_copy_file_range_in inarg = {
3327 .nodeid_out = ff_out->nodeid,
3328 .fh_out = ff_out->fh,
3333 struct fuse_write_out outarg;
3335 /* mark unstable when write-back is not used, and file_out gets
3337 bool is_unstable = (!fc->writeback_cache) &&
3338 ((pos_out + len) > inode_out->i_size);
3340 if (fc->no_copy_file_range)
3343 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3346 inode_lock(inode_in);
3347 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3348 inode_unlock(inode_in);
3352 inode_lock(inode_out);
3354 err = file_modified(file_out);
3359 * Write out dirty pages in the destination file before sending the COPY
3360 * request to userspace. After the request is completed, truncate off
3361 * pages (including partial ones) from the cache that have been copied,
3362 * since these contain stale data at that point.
3364 * This should be mostly correct, but if the COPY writes to partial
3365 * pages (at the start or end) and the parts not covered by the COPY are
3366 * written through a memory map after calling fuse_writeback_range(),
3367 * then these partial page modifications will be lost on truncation.
3369 * It is unlikely that someone would rely on such mixed style
3370 * modifications. Yet this does give less guarantees than if the
3371 * copying was performed with write(2).
3373 * To fix this a i_mmap_sem style lock could be used to prevent new
3374 * faults while the copy is ongoing.
3376 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3381 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3383 args.opcode = FUSE_COPY_FILE_RANGE;
3384 args.nodeid = ff_in->nodeid;
3385 args.in_numargs = 1;
3386 args.in_args[0].size = sizeof(inarg);
3387 args.in_args[0].value = &inarg;
3388 args.out_numargs = 1;
3389 args.out_args[0].size = sizeof(outarg);
3390 args.out_args[0].value = &outarg;
3391 err = fuse_simple_request(fm, &args);
3392 if (err == -ENOSYS) {
3393 fc->no_copy_file_range = 1;
3399 truncate_inode_pages_range(inode_out->i_mapping,
3400 ALIGN_DOWN(pos_out, PAGE_SIZE),
3401 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3403 if (fc->writeback_cache) {
3404 fuse_write_update_size(inode_out, pos_out + outarg.size);
3405 file_update_time(file_out);
3408 fuse_invalidate_attr(inode_out);
3413 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3415 inode_unlock(inode_out);
3416 file_accessed(file_in);
3421 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3422 struct file *dst_file, loff_t dst_off,
3423 size_t len, unsigned int flags)
3427 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3430 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3431 ret = generic_copy_file_range(src_file, src_off, dst_file,
3432 dst_off, len, flags);
3436 static const struct file_operations fuse_file_operations = {
3437 .llseek = fuse_file_llseek,
3438 .read_iter = fuse_file_read_iter,
3439 .write_iter = fuse_file_write_iter,
3440 .mmap = fuse_file_mmap,
3442 .flush = fuse_flush,
3443 .release = fuse_release,
3444 .fsync = fuse_fsync,
3445 .lock = fuse_file_lock,
3446 .get_unmapped_area = thp_get_unmapped_area,
3447 .flock = fuse_file_flock,
3448 .splice_read = generic_file_splice_read,
3449 .splice_write = iter_file_splice_write,
3450 .unlocked_ioctl = fuse_file_ioctl,
3451 .compat_ioctl = fuse_file_compat_ioctl,
3452 .poll = fuse_file_poll,
3453 .fallocate = fuse_file_fallocate,
3454 .copy_file_range = fuse_copy_file_range,
3457 static const struct address_space_operations fuse_file_aops = {
3458 .readpage = fuse_readpage,
3459 .readahead = fuse_readahead,
3460 .writepage = fuse_writepage,
3461 .writepages = fuse_writepages,
3462 .launder_page = fuse_launder_page,
3463 .set_page_dirty = __set_page_dirty_nobuffers,
3465 .direct_IO = fuse_direct_IO,
3466 .write_begin = fuse_write_begin,
3467 .write_end = fuse_write_end,
3470 void fuse_init_file_inode(struct inode *inode)
3472 struct fuse_inode *fi = get_fuse_inode(inode);
3474 inode->i_fop = &fuse_file_operations;
3475 inode->i_data.a_ops = &fuse_file_aops;
3477 INIT_LIST_HEAD(&fi->write_files);
3478 INIT_LIST_HEAD(&fi->queued_writes);
3480 init_waitqueue_head(&fi->page_waitq);
3481 fi->writepages = RB_ROOT;
3483 if (IS_ENABLED(CONFIG_FUSE_DAX))
3484 fuse_dax_inode_init(inode);