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/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_conn *fc, 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 (!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(fc, &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_conn *fc)
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(&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_conn *fc, 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->fc->no_opendir : ff->fc->no_open) {
118 /* Do nothing when client does not implement 'open' */
119 fuse_release_end(ff->fc, args, 0);
121 fuse_simple_request(ff->fc, args);
122 fuse_release_end(ff->fc, args, 0);
124 args->end = fuse_release_end;
125 if (fuse_simple_background(ff->fc, args,
126 GFP_KERNEL | __GFP_NOFAIL))
127 fuse_release_end(ff->fc, args, -ENOTCONN);
133 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
136 struct fuse_file *ff;
137 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
139 ff = fuse_file_alloc(fc);
144 /* Default for no-open */
145 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
146 if (isdir ? !fc->no_opendir : !fc->no_open) {
147 struct fuse_open_out outarg;
150 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
153 ff->open_flags = outarg.open_flags;
155 } else if (err != -ENOSYS) {
167 ff->open_flags &= ~FOPEN_DIRECT_IO;
170 file->private_data = ff;
174 EXPORT_SYMBOL_GPL(fuse_do_open);
176 static void fuse_link_write_file(struct file *file)
178 struct inode *inode = file_inode(file);
179 struct fuse_inode *fi = get_fuse_inode(inode);
180 struct fuse_file *ff = file->private_data;
182 * file may be written through mmap, so chain it onto the
183 * inodes's write_file list
185 spin_lock(&fi->lock);
186 if (list_empty(&ff->write_entry))
187 list_add(&ff->write_entry, &fi->write_files);
188 spin_unlock(&fi->lock);
191 void fuse_finish_open(struct inode *inode, struct file *file)
193 struct fuse_file *ff = file->private_data;
194 struct fuse_conn *fc = get_fuse_conn(inode);
196 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
197 invalidate_inode_pages2(inode->i_mapping);
198 if (ff->open_flags & FOPEN_STREAM)
199 stream_open(inode, file);
200 else if (ff->open_flags & FOPEN_NONSEEKABLE)
201 nonseekable_open(inode, file);
202 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
203 struct fuse_inode *fi = get_fuse_inode(inode);
205 spin_lock(&fi->lock);
206 fi->attr_version = atomic64_inc_return(&fc->attr_version);
207 i_size_write(inode, 0);
208 spin_unlock(&fi->lock);
209 fuse_invalidate_attr(inode);
210 if (fc->writeback_cache)
211 file_update_time(file);
213 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
214 fuse_link_write_file(file);
217 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
219 struct fuse_conn *fc = get_fuse_conn(inode);
221 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
222 fc->atomic_o_trunc &&
225 err = generic_file_open(inode, file);
229 if (is_wb_truncate) {
231 fuse_set_nowrite(inode);
234 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
237 fuse_finish_open(inode, file);
239 if (is_wb_truncate) {
240 fuse_release_nowrite(inode);
247 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
248 int flags, int opcode)
250 struct fuse_conn *fc = ff->fc;
251 struct fuse_release_args *ra = ff->release_args;
253 /* Inode is NULL on error path of fuse_create_open() */
255 spin_lock(&fi->lock);
256 list_del(&ff->write_entry);
257 spin_unlock(&fi->lock);
259 spin_lock(&fc->lock);
260 if (!RB_EMPTY_NODE(&ff->polled_node))
261 rb_erase(&ff->polled_node, &fc->polled_files);
262 spin_unlock(&fc->lock);
264 wake_up_interruptible_all(&ff->poll_wait);
266 ra->inarg.fh = ff->fh;
267 ra->inarg.flags = flags;
268 ra->args.in_numargs = 1;
269 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
270 ra->args.in_args[0].value = &ra->inarg;
271 ra->args.opcode = opcode;
272 ra->args.nodeid = ff->nodeid;
273 ra->args.force = true;
274 ra->args.nocreds = true;
277 void fuse_release_common(struct file *file, bool isdir)
279 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
280 struct fuse_file *ff = file->private_data;
281 struct fuse_release_args *ra = ff->release_args;
282 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
284 fuse_prepare_release(fi, ff, file->f_flags, opcode);
287 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
288 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
291 /* Hold inode until release is finished */
292 ra->inode = igrab(file_inode(file));
295 * Normally this will send the RELEASE request, however if
296 * some asynchronous READ or WRITE requests are outstanding,
297 * the sending will be delayed.
299 * Make the release synchronous if this is a fuseblk mount,
300 * synchronous RELEASE is allowed (and desirable) in this case
301 * because the server can be trusted not to screw up.
303 fuse_file_put(ff, ff->fc->destroy, isdir);
306 static int fuse_open(struct inode *inode, struct file *file)
308 return fuse_open_common(inode, file, false);
311 static int fuse_release(struct inode *inode, struct file *file)
313 struct fuse_conn *fc = get_fuse_conn(inode);
315 /* see fuse_vma_close() for !writeback_cache case */
316 if (fc->writeback_cache)
317 write_inode_now(inode, 1);
319 fuse_release_common(file, false);
321 /* return value is ignored by VFS */
325 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
327 WARN_ON(refcount_read(&ff->count) > 1);
328 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
330 * iput(NULL) is a no-op and since the refcount is 1 and everything's
331 * synchronous, we are fine with not doing igrab() here"
333 fuse_file_put(ff, true, false);
335 EXPORT_SYMBOL_GPL(fuse_sync_release);
338 * Scramble the ID space with XTEA, so that the value of the files_struct
339 * pointer is not exposed to userspace.
341 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
343 u32 *k = fc->scramble_key;
344 u64 v = (unsigned long) id;
350 for (i = 0; i < 32; i++) {
351 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
353 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
356 return (u64) v0 + ((u64) v1 << 32);
359 struct fuse_writepage_args {
360 struct fuse_io_args ia;
361 struct rb_node writepages_entry;
362 struct list_head queue_entry;
363 struct fuse_writepage_args *next;
367 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
368 pgoff_t idx_from, pgoff_t idx_to)
372 n = fi->writepages.rb_node;
375 struct fuse_writepage_args *wpa;
378 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
379 WARN_ON(get_fuse_inode(wpa->inode) != fi);
380 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
381 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
383 else if (idx_to < curr_index)
392 * Check if any page in a range is under writeback
394 * This is currently done by walking the list of writepage requests
395 * for the inode, which can be pretty inefficient.
397 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
400 struct fuse_inode *fi = get_fuse_inode(inode);
403 spin_lock(&fi->lock);
404 found = fuse_find_writeback(fi, idx_from, idx_to);
405 spin_unlock(&fi->lock);
410 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
412 return fuse_range_is_writeback(inode, index, index);
416 * Wait for page writeback to be completed.
418 * Since fuse doesn't rely on the VM writeback tracking, this has to
419 * use some other means.
421 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
423 struct fuse_inode *fi = get_fuse_inode(inode);
425 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
429 * Wait for all pending writepages on the inode to finish.
431 * This is currently done by blocking further writes with FUSE_NOWRITE
432 * and waiting for all sent writes to complete.
434 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
435 * could conflict with truncation.
437 static void fuse_sync_writes(struct inode *inode)
439 fuse_set_nowrite(inode);
440 fuse_release_nowrite(inode);
443 static int fuse_flush(struct file *file, fl_owner_t id)
445 struct inode *inode = file_inode(file);
446 struct fuse_conn *fc = get_fuse_conn(inode);
447 struct fuse_file *ff = file->private_data;
448 struct fuse_flush_in inarg;
452 if (is_bad_inode(inode))
455 err = write_inode_now(inode, 1);
460 fuse_sync_writes(inode);
463 err = filemap_check_errors(file->f_mapping);
471 memset(&inarg, 0, sizeof(inarg));
473 inarg.lock_owner = fuse_lock_owner_id(fc, id);
474 args.opcode = FUSE_FLUSH;
475 args.nodeid = get_node_id(inode);
477 args.in_args[0].size = sizeof(inarg);
478 args.in_args[0].value = &inarg;
481 err = fuse_simple_request(fc, &args);
482 if (err == -ENOSYS) {
489 * In memory i_blocks is not maintained by fuse, if writeback cache is
490 * enabled, i_blocks from cached attr may not be accurate.
492 if (!err && fc->writeback_cache)
493 fuse_invalidate_attr(inode);
497 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
498 int datasync, int opcode)
500 struct inode *inode = file->f_mapping->host;
501 struct fuse_conn *fc = get_fuse_conn(inode);
502 struct fuse_file *ff = file->private_data;
504 struct fuse_fsync_in inarg;
506 memset(&inarg, 0, sizeof(inarg));
508 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
509 args.opcode = opcode;
510 args.nodeid = get_node_id(inode);
512 args.in_args[0].size = sizeof(inarg);
513 args.in_args[0].value = &inarg;
514 return fuse_simple_request(fc, &args);
517 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
520 struct inode *inode = file->f_mapping->host;
521 struct fuse_conn *fc = get_fuse_conn(inode);
524 if (is_bad_inode(inode))
530 * Start writeback against all dirty pages of the inode, then
531 * wait for all outstanding writes, before sending the FSYNC
534 err = file_write_and_wait_range(file, start, end);
538 fuse_sync_writes(inode);
541 * Due to implementation of fuse writeback
542 * file_write_and_wait_range() does not catch errors.
543 * We have to do this directly after fuse_sync_writes()
545 err = file_check_and_advance_wb_err(file);
549 err = sync_inode_metadata(inode, 1);
556 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
557 if (err == -ENOSYS) {
567 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
568 size_t count, int opcode)
570 struct fuse_file *ff = file->private_data;
571 struct fuse_args *args = &ia->ap.args;
573 ia->read.in.fh = ff->fh;
574 ia->read.in.offset = pos;
575 ia->read.in.size = count;
576 ia->read.in.flags = file->f_flags;
577 args->opcode = opcode;
578 args->nodeid = ff->nodeid;
579 args->in_numargs = 1;
580 args->in_args[0].size = sizeof(ia->read.in);
581 args->in_args[0].value = &ia->read.in;
582 args->out_argvar = true;
583 args->out_numargs = 1;
584 args->out_args[0].size = count;
587 static void fuse_release_user_pages(struct fuse_args_pages *ap,
592 for (i = 0; i < ap->num_pages; i++) {
594 set_page_dirty_lock(ap->pages[i]);
595 put_page(ap->pages[i]);
599 static void fuse_io_release(struct kref *kref)
601 kfree(container_of(kref, struct fuse_io_priv, refcnt));
604 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
609 if (io->bytes >= 0 && io->write)
612 return io->bytes < 0 ? io->size : io->bytes;
616 * In case of short read, the caller sets 'pos' to the position of
617 * actual end of fuse request in IO request. Otherwise, if bytes_requested
618 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
621 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
622 * both submitted asynchronously. The first of them was ACKed by userspace as
623 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
624 * second request was ACKed as short, e.g. only 1K was read, resulting in
627 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
628 * will be equal to the length of the longest contiguous fragment of
629 * transferred data starting from the beginning of IO request.
631 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
635 spin_lock(&io->lock);
637 io->err = io->err ? : err;
638 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
642 if (!left && io->blocking)
644 spin_unlock(&io->lock);
646 if (!left && !io->blocking) {
647 ssize_t res = fuse_get_res_by_io(io);
650 struct inode *inode = file_inode(io->iocb->ki_filp);
651 struct fuse_conn *fc = get_fuse_conn(inode);
652 struct fuse_inode *fi = get_fuse_inode(inode);
654 spin_lock(&fi->lock);
655 fi->attr_version = atomic64_inc_return(&fc->attr_version);
656 spin_unlock(&fi->lock);
659 io->iocb->ki_complete(io->iocb, res, 0);
662 kref_put(&io->refcnt, fuse_io_release);
665 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
668 struct fuse_io_args *ia;
670 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
673 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
683 static void fuse_io_free(struct fuse_io_args *ia)
689 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
692 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
693 struct fuse_io_priv *io = ia->io;
696 fuse_release_user_pages(&ia->ap, io->should_dirty);
700 } else if (io->write) {
701 if (ia->write.out.size > ia->write.in.size) {
703 } else if (ia->write.in.size != ia->write.out.size) {
704 pos = ia->write.in.offset - io->offset +
708 u32 outsize = args->out_args[0].size;
710 if (ia->read.in.size != outsize)
711 pos = ia->read.in.offset - io->offset + outsize;
714 fuse_aio_complete(io, err, pos);
718 static ssize_t fuse_async_req_send(struct fuse_conn *fc,
719 struct fuse_io_args *ia, size_t num_bytes)
722 struct fuse_io_priv *io = ia->io;
724 spin_lock(&io->lock);
725 kref_get(&io->refcnt);
726 io->size += num_bytes;
728 spin_unlock(&io->lock);
730 ia->ap.args.end = fuse_aio_complete_req;
731 ia->ap.args.may_block = io->should_dirty;
732 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
734 fuse_aio_complete_req(fc, &ia->ap.args, err);
739 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
742 struct file *file = ia->io->iocb->ki_filp;
743 struct fuse_file *ff = file->private_data;
744 struct fuse_conn *fc = ff->fc;
746 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
748 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
749 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
753 return fuse_async_req_send(fc, ia, count);
755 return fuse_simple_request(fc, &ia->ap.args);
758 static void fuse_read_update_size(struct inode *inode, loff_t size,
761 struct fuse_conn *fc = get_fuse_conn(inode);
762 struct fuse_inode *fi = get_fuse_inode(inode);
764 spin_lock(&fi->lock);
765 if (attr_ver == fi->attr_version && size < inode->i_size &&
766 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
767 fi->attr_version = atomic64_inc_return(&fc->attr_version);
768 i_size_write(inode, size);
770 spin_unlock(&fi->lock);
773 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
774 struct fuse_args_pages *ap)
776 struct fuse_conn *fc = get_fuse_conn(inode);
778 if (fc->writeback_cache) {
780 * A hole in a file. Some data after the hole are in page cache,
781 * but have not reached the client fs yet. So, the hole is not
785 int start_idx = num_read >> PAGE_SHIFT;
786 size_t off = num_read & (PAGE_SIZE - 1);
788 for (i = start_idx; i < ap->num_pages; i++) {
789 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
793 loff_t pos = page_offset(ap->pages[0]) + num_read;
794 fuse_read_update_size(inode, pos, attr_ver);
798 static int fuse_do_readpage(struct file *file, struct page *page)
800 struct inode *inode = page->mapping->host;
801 struct fuse_conn *fc = get_fuse_conn(inode);
802 loff_t pos = page_offset(page);
803 struct fuse_page_desc desc = { .length = PAGE_SIZE };
804 struct fuse_io_args ia = {
805 .ap.args.page_zeroing = true,
806 .ap.args.out_pages = true,
815 * Page writeback can extend beyond the lifetime of the
816 * page-cache page, so make sure we read a properly synced
819 fuse_wait_on_page_writeback(inode, page->index);
821 attr_ver = fuse_get_attr_version(fc);
823 /* Don't overflow end offset */
824 if (pos + (desc.length - 1) == LLONG_MAX)
827 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
828 res = fuse_simple_request(fc, &ia.ap.args);
832 * Short read means EOF. If file size is larger, truncate it
834 if (res < desc.length)
835 fuse_short_read(inode, attr_ver, res, &ia.ap);
837 SetPageUptodate(page);
842 static int fuse_readpage(struct file *file, struct page *page)
844 struct inode *inode = page->mapping->host;
848 if (is_bad_inode(inode))
851 err = fuse_do_readpage(file, page);
852 fuse_invalidate_atime(inode);
858 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
862 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
863 struct fuse_args_pages *ap = &ia->ap;
864 size_t count = ia->read.in.size;
865 size_t num_read = args->out_args[0].size;
866 struct address_space *mapping = NULL;
868 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
869 mapping = ap->pages[i]->mapping;
872 struct inode *inode = mapping->host;
875 * Short read means EOF. If file size is larger, truncate it
877 if (!err && num_read < count)
878 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
880 fuse_invalidate_atime(inode);
883 for (i = 0; i < ap->num_pages; i++) {
884 struct page *page = ap->pages[i];
887 SetPageUptodate(page);
894 fuse_file_put(ia->ff, false, false);
899 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
901 struct fuse_file *ff = file->private_data;
902 struct fuse_conn *fc = ff->fc;
903 struct fuse_args_pages *ap = &ia->ap;
904 loff_t pos = page_offset(ap->pages[0]);
905 size_t count = ap->num_pages << PAGE_SHIFT;
909 ap->args.out_pages = true;
910 ap->args.page_zeroing = true;
911 ap->args.page_replace = true;
913 /* Don't overflow end offset */
914 if (pos + (count - 1) == LLONG_MAX) {
916 ap->descs[ap->num_pages - 1].length--;
918 WARN_ON((loff_t) (pos + count) < 0);
920 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
921 ia->read.attr_ver = fuse_get_attr_version(fc);
922 if (fc->async_read) {
923 ia->ff = fuse_file_get(ff);
924 ap->args.end = fuse_readpages_end;
925 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
929 res = fuse_simple_request(fc, &ap->args);
930 err = res < 0 ? res : 0;
932 fuse_readpages_end(fc, &ap->args, err);
935 static void fuse_readahead(struct readahead_control *rac)
937 struct inode *inode = rac->mapping->host;
938 struct fuse_conn *fc = get_fuse_conn(inode);
939 unsigned int i, max_pages, nr_pages = 0;
941 if (is_bad_inode(inode))
944 max_pages = min_t(unsigned int, fc->max_pages,
945 fc->max_read / PAGE_SIZE);
948 struct fuse_io_args *ia;
949 struct fuse_args_pages *ap;
951 nr_pages = readahead_count(rac) - nr_pages;
952 if (nr_pages > max_pages)
953 nr_pages = max_pages;
956 ia = fuse_io_alloc(NULL, nr_pages);
960 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
961 for (i = 0; i < nr_pages; i++) {
962 fuse_wait_on_page_writeback(inode,
963 readahead_index(rac) + i);
964 ap->descs[i].length = PAGE_SIZE;
966 ap->num_pages = nr_pages;
967 fuse_send_readpages(ia, rac->file);
971 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
973 struct inode *inode = iocb->ki_filp->f_mapping->host;
974 struct fuse_conn *fc = get_fuse_conn(inode);
977 * In auto invalidate mode, always update attributes on read.
978 * Otherwise, only update if we attempt to read past EOF (to ensure
979 * i_size is up to date).
981 if (fc->auto_inval_data ||
982 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
984 err = fuse_update_attributes(inode, iocb->ki_filp);
989 return generic_file_read_iter(iocb, to);
992 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
993 loff_t pos, size_t count)
995 struct fuse_args *args = &ia->ap.args;
997 ia->write.in.fh = ff->fh;
998 ia->write.in.offset = pos;
999 ia->write.in.size = count;
1000 args->opcode = FUSE_WRITE;
1001 args->nodeid = ff->nodeid;
1002 args->in_numargs = 2;
1003 if (ff->fc->minor < 9)
1004 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1006 args->in_args[0].size = sizeof(ia->write.in);
1007 args->in_args[0].value = &ia->write.in;
1008 args->in_args[1].size = count;
1009 args->out_numargs = 1;
1010 args->out_args[0].size = sizeof(ia->write.out);
1011 args->out_args[0].value = &ia->write.out;
1014 static unsigned int fuse_write_flags(struct kiocb *iocb)
1016 unsigned int flags = iocb->ki_filp->f_flags;
1018 if (iocb->ki_flags & IOCB_DSYNC)
1020 if (iocb->ki_flags & IOCB_SYNC)
1026 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1027 size_t count, fl_owner_t owner)
1029 struct kiocb *iocb = ia->io->iocb;
1030 struct file *file = iocb->ki_filp;
1031 struct fuse_file *ff = file->private_data;
1032 struct fuse_conn *fc = ff->fc;
1033 struct fuse_write_in *inarg = &ia->write.in;
1036 fuse_write_args_fill(ia, ff, pos, count);
1037 inarg->flags = fuse_write_flags(iocb);
1038 if (owner != NULL) {
1039 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1040 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1044 return fuse_async_req_send(fc, ia, count);
1046 err = fuse_simple_request(fc, &ia->ap.args);
1047 if (!err && ia->write.out.size > count)
1050 return err ?: ia->write.out.size;
1053 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1055 struct fuse_conn *fc = get_fuse_conn(inode);
1056 struct fuse_inode *fi = get_fuse_inode(inode);
1059 spin_lock(&fi->lock);
1060 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1061 if (pos > inode->i_size) {
1062 i_size_write(inode, pos);
1065 spin_unlock(&fi->lock);
1070 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1071 struct kiocb *iocb, struct inode *inode,
1072 loff_t pos, size_t count)
1074 struct fuse_args_pages *ap = &ia->ap;
1075 struct file *file = iocb->ki_filp;
1076 struct fuse_file *ff = file->private_data;
1077 struct fuse_conn *fc = ff->fc;
1078 unsigned int offset, i;
1081 for (i = 0; i < ap->num_pages; i++)
1082 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1084 fuse_write_args_fill(ia, ff, pos, count);
1085 ia->write.in.flags = fuse_write_flags(iocb);
1087 err = fuse_simple_request(fc, &ap->args);
1088 if (!err && ia->write.out.size > count)
1091 offset = ap->descs[0].offset;
1092 count = ia->write.out.size;
1093 for (i = 0; i < ap->num_pages; i++) {
1094 struct page *page = ap->pages[i];
1096 if (!err && !offset && count >= PAGE_SIZE)
1097 SetPageUptodate(page);
1099 if (count > PAGE_SIZE - offset)
1100 count -= PAGE_SIZE - offset;
1112 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1113 struct address_space *mapping,
1114 struct iov_iter *ii, loff_t pos,
1115 unsigned int max_pages)
1117 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1118 unsigned offset = pos & (PAGE_SIZE - 1);
1122 ap->args.in_pages = true;
1123 ap->descs[0].offset = offset;
1128 pgoff_t index = pos >> PAGE_SHIFT;
1129 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1130 iov_iter_count(ii));
1132 bytes = min_t(size_t, bytes, fc->max_write - count);
1136 if (iov_iter_fault_in_readable(ii, bytes))
1140 page = grab_cache_page_write_begin(mapping, index, 0);
1144 if (mapping_writably_mapped(mapping))
1145 flush_dcache_page(page);
1147 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1148 flush_dcache_page(page);
1150 iov_iter_advance(ii, tmp);
1154 bytes = min(bytes, iov_iter_single_seg_count(ii));
1159 ap->pages[ap->num_pages] = page;
1160 ap->descs[ap->num_pages].length = tmp;
1166 if (offset == PAGE_SIZE)
1169 if (!fc->big_writes)
1171 } while (iov_iter_count(ii) && count < fc->max_write &&
1172 ap->num_pages < max_pages && offset == 0);
1174 return count > 0 ? count : err;
1177 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1178 unsigned int max_pages)
1180 return min_t(unsigned int,
1181 ((pos + len - 1) >> PAGE_SHIFT) -
1182 (pos >> PAGE_SHIFT) + 1,
1186 static ssize_t fuse_perform_write(struct kiocb *iocb,
1187 struct address_space *mapping,
1188 struct iov_iter *ii, loff_t pos)
1190 struct inode *inode = mapping->host;
1191 struct fuse_conn *fc = get_fuse_conn(inode);
1192 struct fuse_inode *fi = get_fuse_inode(inode);
1196 if (inode->i_size < pos + iov_iter_count(ii))
1197 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1201 struct fuse_io_args ia = {};
1202 struct fuse_args_pages *ap = &ia.ap;
1203 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1206 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1212 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1216 err = fuse_send_write_pages(&ia, iocb, inode,
1219 size_t num_written = ia.write.out.size;
1224 /* break out of the loop on short write */
1225 if (num_written != count)
1230 } while (!err && iov_iter_count(ii));
1233 fuse_write_update_size(inode, pos);
1235 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1236 fuse_invalidate_attr(inode);
1238 return res > 0 ? res : err;
1241 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1243 struct file *file = iocb->ki_filp;
1244 struct address_space *mapping = file->f_mapping;
1245 ssize_t written = 0;
1246 ssize_t written_buffered = 0;
1247 struct inode *inode = mapping->host;
1251 if (get_fuse_conn(inode)->writeback_cache) {
1252 /* Update size (EOF optimization) and mode (SUID clearing) */
1253 err = fuse_update_attributes(mapping->host, file);
1257 return generic_file_write_iter(iocb, from);
1262 /* We can write back this queue in page reclaim */
1263 current->backing_dev_info = inode_to_bdi(inode);
1265 err = generic_write_checks(iocb, from);
1269 err = file_remove_privs(file);
1273 err = file_update_time(file);
1277 if (iocb->ki_flags & IOCB_DIRECT) {
1278 loff_t pos = iocb->ki_pos;
1279 written = generic_file_direct_write(iocb, from);
1280 if (written < 0 || !iov_iter_count(from))
1285 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1286 if (written_buffered < 0) {
1287 err = written_buffered;
1290 endbyte = pos + written_buffered - 1;
1292 err = filemap_write_and_wait_range(file->f_mapping, pos,
1297 invalidate_mapping_pages(file->f_mapping,
1299 endbyte >> PAGE_SHIFT);
1301 written += written_buffered;
1302 iocb->ki_pos = pos + written_buffered;
1304 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1306 iocb->ki_pos += written;
1309 current->backing_dev_info = NULL;
1310 inode_unlock(inode);
1312 written = generic_write_sync(iocb, written);
1314 return written ? written : err;
1317 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1319 unsigned int nr_pages)
1323 for (i = index; i < index + nr_pages; i++)
1324 descs[i].length = PAGE_SIZE - descs[i].offset;
1327 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1329 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1332 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1335 return min(iov_iter_single_seg_count(ii), max_size);
1338 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1339 size_t *nbytesp, int write,
1340 unsigned int max_pages)
1342 size_t nbytes = 0; /* # bytes already packed in req */
1345 /* Special case for kernel I/O: can copy directly into the buffer */
1346 if (iov_iter_is_kvec(ii)) {
1347 unsigned long user_addr = fuse_get_user_addr(ii);
1348 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1351 ap->args.in_args[1].value = (void *) user_addr;
1353 ap->args.out_args[0].value = (void *) user_addr;
1355 iov_iter_advance(ii, frag_size);
1356 *nbytesp = frag_size;
1360 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1363 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1365 max_pages - ap->num_pages,
1370 iov_iter_advance(ii, ret);
1374 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1376 ap->descs[ap->num_pages].offset = start;
1377 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1379 ap->num_pages += npages;
1380 ap->descs[ap->num_pages - 1].length -=
1381 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1385 ap->args.in_pages = true;
1387 ap->args.out_pages = true;
1391 return ret < 0 ? ret : 0;
1394 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1395 loff_t *ppos, int flags)
1397 int write = flags & FUSE_DIO_WRITE;
1398 int cuse = flags & FUSE_DIO_CUSE;
1399 struct file *file = io->iocb->ki_filp;
1400 struct inode *inode = file->f_mapping->host;
1401 struct fuse_file *ff = file->private_data;
1402 struct fuse_conn *fc = ff->fc;
1403 size_t nmax = write ? fc->max_write : fc->max_read;
1405 size_t count = iov_iter_count(iter);
1406 pgoff_t idx_from = pos >> PAGE_SHIFT;
1407 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1410 struct fuse_io_args *ia;
1411 unsigned int max_pages;
1413 max_pages = iov_iter_npages(iter, fc->max_pages);
1414 ia = fuse_io_alloc(io, max_pages);
1419 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1422 fuse_sync_writes(inode);
1424 inode_unlock(inode);
1427 io->should_dirty = !write && iter_is_iovec(iter);
1430 fl_owner_t owner = current->files;
1431 size_t nbytes = min(count, nmax);
1433 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1439 if (!capable(CAP_FSETID))
1440 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1442 nres = fuse_send_write(ia, pos, nbytes, owner);
1444 nres = fuse_send_read(ia, pos, nbytes, owner);
1447 if (!io->async || nres < 0) {
1448 fuse_release_user_pages(&ia->ap, io->should_dirty);
1453 iov_iter_revert(iter, nbytes);
1457 WARN_ON(nres > nbytes);
1462 if (nres != nbytes) {
1463 iov_iter_revert(iter, nbytes - nres);
1467 max_pages = iov_iter_npages(iter, fc->max_pages);
1468 ia = fuse_io_alloc(io, max_pages);
1478 return res > 0 ? res : err;
1480 EXPORT_SYMBOL_GPL(fuse_direct_io);
1482 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1483 struct iov_iter *iter,
1487 struct inode *inode = file_inode(io->iocb->ki_filp);
1489 res = fuse_direct_io(io, iter, ppos, 0);
1491 fuse_invalidate_atime(inode);
1496 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1498 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1502 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1503 res = fuse_direct_IO(iocb, to);
1505 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1507 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1513 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1515 struct inode *inode = file_inode(iocb->ki_filp);
1516 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1519 /* Don't allow parallel writes to the same file */
1521 res = generic_write_checks(iocb, from);
1523 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1524 res = fuse_direct_IO(iocb, from);
1526 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1530 fuse_invalidate_attr(inode);
1532 fuse_write_update_size(inode, iocb->ki_pos);
1533 inode_unlock(inode);
1538 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1540 struct file *file = iocb->ki_filp;
1541 struct fuse_file *ff = file->private_data;
1543 if (is_bad_inode(file_inode(file)))
1546 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1547 return fuse_cache_read_iter(iocb, to);
1549 return fuse_direct_read_iter(iocb, to);
1552 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1554 struct file *file = iocb->ki_filp;
1555 struct fuse_file *ff = file->private_data;
1557 if (is_bad_inode(file_inode(file)))
1560 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1561 return fuse_cache_write_iter(iocb, from);
1563 return fuse_direct_write_iter(iocb, from);
1566 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1568 struct fuse_args_pages *ap = &wpa->ia.ap;
1571 for (i = 0; i < ap->num_pages; i++)
1572 __free_page(ap->pages[i]);
1575 fuse_file_put(wpa->ia.ff, false, false);
1581 static void fuse_writepage_finish(struct fuse_conn *fc,
1582 struct fuse_writepage_args *wpa)
1584 struct fuse_args_pages *ap = &wpa->ia.ap;
1585 struct inode *inode = wpa->inode;
1586 struct fuse_inode *fi = get_fuse_inode(inode);
1587 struct backing_dev_info *bdi = inode_to_bdi(inode);
1590 for (i = 0; i < ap->num_pages; i++) {
1591 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1592 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1593 wb_writeout_inc(&bdi->wb);
1595 wake_up(&fi->page_waitq);
1598 /* Called under fi->lock, may release and reacquire it */
1599 static void fuse_send_writepage(struct fuse_conn *fc,
1600 struct fuse_writepage_args *wpa, loff_t size)
1601 __releases(fi->lock)
1602 __acquires(fi->lock)
1604 struct fuse_writepage_args *aux, *next;
1605 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1606 struct fuse_write_in *inarg = &wpa->ia.write.in;
1607 struct fuse_args *args = &wpa->ia.ap.args;
1608 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1612 if (inarg->offset + data_size <= size) {
1613 inarg->size = data_size;
1614 } else if (inarg->offset < size) {
1615 inarg->size = size - inarg->offset;
1617 /* Got truncated off completely */
1621 args->in_args[1].size = inarg->size;
1623 args->nocreds = true;
1625 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1626 if (err == -ENOMEM) {
1627 spin_unlock(&fi->lock);
1628 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1629 spin_lock(&fi->lock);
1632 /* Fails on broken connection only */
1640 rb_erase(&wpa->writepages_entry, &fi->writepages);
1641 fuse_writepage_finish(fc, wpa);
1642 spin_unlock(&fi->lock);
1644 /* After fuse_writepage_finish() aux request list is private */
1645 for (aux = wpa->next; aux; aux = next) {
1648 fuse_writepage_free(aux);
1651 fuse_writepage_free(wpa);
1652 spin_lock(&fi->lock);
1656 * If fi->writectr is positive (no truncate or fsync going on) send
1657 * all queued writepage requests.
1659 * Called with fi->lock
1661 void fuse_flush_writepages(struct inode *inode)
1662 __releases(fi->lock)
1663 __acquires(fi->lock)
1665 struct fuse_conn *fc = get_fuse_conn(inode);
1666 struct fuse_inode *fi = get_fuse_inode(inode);
1667 loff_t crop = i_size_read(inode);
1668 struct fuse_writepage_args *wpa;
1670 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1671 wpa = list_entry(fi->queued_writes.next,
1672 struct fuse_writepage_args, queue_entry);
1673 list_del_init(&wpa->queue_entry);
1674 fuse_send_writepage(fc, wpa, crop);
1678 static struct fuse_writepage_args *fuse_insert_writeback(struct rb_root *root,
1679 struct fuse_writepage_args *wpa)
1681 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1682 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1683 struct rb_node **p = &root->rb_node;
1684 struct rb_node *parent = NULL;
1686 WARN_ON(!wpa->ia.ap.num_pages);
1688 struct fuse_writepage_args *curr;
1692 curr = rb_entry(parent, struct fuse_writepage_args,
1694 WARN_ON(curr->inode != wpa->inode);
1695 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1697 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1698 p = &(*p)->rb_right;
1699 else if (idx_to < curr_index)
1705 rb_link_node(&wpa->writepages_entry, parent, p);
1706 rb_insert_color(&wpa->writepages_entry, root);
1710 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1712 WARN_ON(fuse_insert_writeback(root, wpa));
1715 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1718 struct fuse_writepage_args *wpa =
1719 container_of(args, typeof(*wpa), ia.ap.args);
1720 struct inode *inode = wpa->inode;
1721 struct fuse_inode *fi = get_fuse_inode(inode);
1723 mapping_set_error(inode->i_mapping, error);
1724 spin_lock(&fi->lock);
1725 rb_erase(&wpa->writepages_entry, &fi->writepages);
1727 struct fuse_conn *fc = get_fuse_conn(inode);
1728 struct fuse_write_in *inarg = &wpa->ia.write.in;
1729 struct fuse_writepage_args *next = wpa->next;
1731 wpa->next = next->next;
1733 next->ia.ff = fuse_file_get(wpa->ia.ff);
1734 tree_insert(&fi->writepages, next);
1737 * Skip fuse_flush_writepages() to make it easy to crop requests
1738 * based on primary request size.
1740 * 1st case (trivial): there are no concurrent activities using
1741 * fuse_set/release_nowrite. Then we're on safe side because
1742 * fuse_flush_writepages() would call fuse_send_writepage()
1745 * 2nd case: someone called fuse_set_nowrite and it is waiting
1746 * now for completion of all in-flight requests. This happens
1747 * rarely and no more than once per page, so this should be
1750 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1751 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1752 * that fuse_set_nowrite returned implies that all in-flight
1753 * requests were completed along with all of their secondary
1754 * requests. Further primary requests are blocked by negative
1755 * writectr. Hence there cannot be any in-flight requests and
1756 * no invocations of fuse_writepage_end() while we're in
1757 * fuse_set_nowrite..fuse_release_nowrite section.
1759 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1762 fuse_writepage_finish(fc, wpa);
1763 spin_unlock(&fi->lock);
1764 fuse_writepage_free(wpa);
1767 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1768 struct fuse_inode *fi)
1770 struct fuse_file *ff = NULL;
1772 spin_lock(&fi->lock);
1773 if (!list_empty(&fi->write_files)) {
1774 ff = list_entry(fi->write_files.next, struct fuse_file,
1778 spin_unlock(&fi->lock);
1783 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1784 struct fuse_inode *fi)
1786 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1791 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1793 struct fuse_conn *fc = get_fuse_conn(inode);
1794 struct fuse_inode *fi = get_fuse_inode(inode);
1795 struct fuse_file *ff;
1798 ff = __fuse_write_file_get(fc, fi);
1799 err = fuse_flush_times(inode, ff);
1801 fuse_file_put(ff, false, false);
1806 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1808 struct fuse_writepage_args *wpa;
1809 struct fuse_args_pages *ap;
1811 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1815 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1825 static int fuse_writepage_locked(struct page *page)
1827 struct address_space *mapping = page->mapping;
1828 struct inode *inode = mapping->host;
1829 struct fuse_conn *fc = get_fuse_conn(inode);
1830 struct fuse_inode *fi = get_fuse_inode(inode);
1831 struct fuse_writepage_args *wpa;
1832 struct fuse_args_pages *ap;
1833 struct page *tmp_page;
1834 int error = -ENOMEM;
1836 set_page_writeback(page);
1838 wpa = fuse_writepage_args_alloc();
1843 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1848 wpa->ia.ff = fuse_write_file_get(fc, fi);
1852 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1854 copy_highpage(tmp_page, page);
1855 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1857 ap->args.in_pages = true;
1859 ap->pages[0] = tmp_page;
1860 ap->descs[0].offset = 0;
1861 ap->descs[0].length = PAGE_SIZE;
1862 ap->args.end = fuse_writepage_end;
1865 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1866 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1868 spin_lock(&fi->lock);
1869 tree_insert(&fi->writepages, wpa);
1870 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1871 fuse_flush_writepages(inode);
1872 spin_unlock(&fi->lock);
1874 end_page_writeback(page);
1879 __free_page(tmp_page);
1883 mapping_set_error(page->mapping, error);
1884 end_page_writeback(page);
1888 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1892 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1894 * ->writepages() should be called for sync() and friends. We
1895 * should only get here on direct reclaim and then we are
1896 * allowed to skip a page which is already in flight
1898 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1900 redirty_page_for_writepage(wbc, page);
1905 err = fuse_writepage_locked(page);
1911 struct fuse_fill_wb_data {
1912 struct fuse_writepage_args *wpa;
1913 struct fuse_file *ff;
1914 struct inode *inode;
1915 struct page **orig_pages;
1916 unsigned int max_pages;
1919 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1921 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1922 struct fuse_conn *fc = get_fuse_conn(data->inode);
1923 struct page **pages;
1924 struct fuse_page_desc *descs;
1925 unsigned int npages = min_t(unsigned int,
1926 max_t(unsigned int, data->max_pages * 2,
1927 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1929 WARN_ON(npages <= data->max_pages);
1931 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1935 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1936 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1940 data->max_pages = npages;
1945 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1947 struct fuse_writepage_args *wpa = data->wpa;
1948 struct inode *inode = data->inode;
1949 struct fuse_inode *fi = get_fuse_inode(inode);
1950 int num_pages = wpa->ia.ap.num_pages;
1953 wpa->ia.ff = fuse_file_get(data->ff);
1954 spin_lock(&fi->lock);
1955 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1956 fuse_flush_writepages(inode);
1957 spin_unlock(&fi->lock);
1959 for (i = 0; i < num_pages; i++)
1960 end_page_writeback(data->orig_pages[i]);
1964 * Check under fi->lock if the page is under writeback, and insert it onto the
1965 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
1966 * one already added for a page at this offset. If there's none, then insert
1967 * this new request onto the auxiliary list, otherwise reuse the existing one by
1968 * swapping the new temp page with the old one.
1970 static bool fuse_writepage_add(struct fuse_writepage_args *new_wpa,
1973 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1974 struct fuse_writepage_args *tmp;
1975 struct fuse_writepage_args *old_wpa;
1976 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
1978 WARN_ON(new_ap->num_pages != 0);
1979 new_ap->num_pages = 1;
1981 spin_lock(&fi->lock);
1982 old_wpa = fuse_insert_writeback(&fi->writepages, new_wpa);
1984 spin_unlock(&fi->lock);
1988 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
1991 WARN_ON(tmp->inode != new_wpa->inode);
1992 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
1993 if (curr_index == page->index) {
1994 WARN_ON(tmp->ia.ap.num_pages != 1);
1995 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
2001 new_wpa->next = old_wpa->next;
2002 old_wpa->next = new_wpa;
2005 spin_unlock(&fi->lock);
2008 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2010 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2011 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2012 wb_writeout_inc(&bdi->wb);
2013 fuse_writepage_free(new_wpa);
2019 static bool fuse_writepage_need_send(struct fuse_conn *fc, struct page *page,
2020 struct fuse_args_pages *ap,
2021 struct fuse_fill_wb_data *data)
2023 WARN_ON(!ap->num_pages);
2026 * Being under writeback is unlikely but possible. For example direct
2027 * read to an mmaped fuse file will set the page dirty twice; once when
2028 * the pages are faulted with get_user_pages(), and then after the read
2031 if (fuse_page_is_writeback(data->inode, page->index))
2034 /* Reached max pages */
2035 if (ap->num_pages == fc->max_pages)
2038 /* Reached max write bytes */
2039 if ((ap->num_pages + 1) * PAGE_SIZE > fc->max_write)
2043 if (data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)
2046 /* Need to grow the pages array? If so, did the expansion fail? */
2047 if (ap->num_pages == data->max_pages && !fuse_pages_realloc(data))
2053 static int fuse_writepages_fill(struct page *page,
2054 struct writeback_control *wbc, void *_data)
2056 struct fuse_fill_wb_data *data = _data;
2057 struct fuse_writepage_args *wpa = data->wpa;
2058 struct fuse_args_pages *ap = &wpa->ia.ap;
2059 struct inode *inode = data->inode;
2060 struct fuse_inode *fi = get_fuse_inode(inode);
2061 struct fuse_conn *fc = get_fuse_conn(inode);
2062 struct page *tmp_page;
2067 data->ff = fuse_write_file_get(fc, fi);
2072 if (wpa && fuse_writepage_need_send(fc, page, ap, data)) {
2073 fuse_writepages_send(data);
2078 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2083 * The page must not be redirtied until the writeout is completed
2084 * (i.e. userspace has sent a reply to the write request). Otherwise
2085 * there could be more than one temporary page instance for each real
2088 * This is ensured by holding the page lock in page_mkwrite() while
2089 * checking fuse_page_is_writeback(). We already hold the page lock
2090 * since clear_page_dirty_for_io() and keep it held until we add the
2091 * request to the fi->writepages list and increment ap->num_pages.
2092 * After this fuse_page_is_writeback() will indicate that the page is
2093 * under writeback, so we can release the page lock.
2095 if (data->wpa == NULL) {
2097 wpa = fuse_writepage_args_alloc();
2099 __free_page(tmp_page);
2102 data->max_pages = 1;
2105 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2106 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2108 ap->args.in_pages = true;
2109 ap->args.end = fuse_writepage_end;
2113 set_page_writeback(page);
2115 copy_highpage(tmp_page, page);
2116 ap->pages[ap->num_pages] = tmp_page;
2117 ap->descs[ap->num_pages].offset = 0;
2118 ap->descs[ap->num_pages].length = PAGE_SIZE;
2119 data->orig_pages[ap->num_pages] = page;
2121 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2122 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2127 * Protected by fi->lock against concurrent access by
2128 * fuse_page_is_writeback().
2130 spin_lock(&fi->lock);
2132 spin_unlock(&fi->lock);
2133 } else if (fuse_writepage_add(wpa, page)) {
2136 end_page_writeback(page);
2144 static int fuse_writepages(struct address_space *mapping,
2145 struct writeback_control *wbc)
2147 struct inode *inode = mapping->host;
2148 struct fuse_conn *fc = get_fuse_conn(inode);
2149 struct fuse_fill_wb_data data;
2153 if (is_bad_inode(inode))
2161 data.orig_pages = kcalloc(fc->max_pages,
2162 sizeof(struct page *),
2164 if (!data.orig_pages)
2167 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2169 WARN_ON(!data.wpa->ia.ap.num_pages);
2170 fuse_writepages_send(&data);
2173 fuse_file_put(data.ff, false, false);
2175 kfree(data.orig_pages);
2181 * It's worthy to make sure that space is reserved on disk for the write,
2182 * but how to implement it without killing performance need more thinking.
2184 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2185 loff_t pos, unsigned len, unsigned flags,
2186 struct page **pagep, void **fsdata)
2188 pgoff_t index = pos >> PAGE_SHIFT;
2189 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2194 WARN_ON(!fc->writeback_cache);
2196 page = grab_cache_page_write_begin(mapping, index, flags);
2200 fuse_wait_on_page_writeback(mapping->host, page->index);
2202 if (PageUptodate(page) || len == PAGE_SIZE)
2205 * Check if the start this page comes after the end of file, in which
2206 * case the readpage can be optimized away.
2208 fsize = i_size_read(mapping->host);
2209 if (fsize <= (pos & PAGE_MASK)) {
2210 size_t off = pos & ~PAGE_MASK;
2212 zero_user_segment(page, 0, off);
2215 err = fuse_do_readpage(file, page);
2229 static int fuse_write_end(struct file *file, struct address_space *mapping,
2230 loff_t pos, unsigned len, unsigned copied,
2231 struct page *page, void *fsdata)
2233 struct inode *inode = page->mapping->host;
2235 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2239 if (!PageUptodate(page)) {
2240 /* Zero any unwritten bytes at the end of the page */
2241 size_t endoff = (pos + copied) & ~PAGE_MASK;
2243 zero_user_segment(page, endoff, PAGE_SIZE);
2244 SetPageUptodate(page);
2247 fuse_write_update_size(inode, pos + copied);
2248 set_page_dirty(page);
2257 static int fuse_launder_page(struct page *page)
2260 if (clear_page_dirty_for_io(page)) {
2261 struct inode *inode = page->mapping->host;
2262 err = fuse_writepage_locked(page);
2264 fuse_wait_on_page_writeback(inode, page->index);
2270 * Write back dirty pages now, because there may not be any suitable
2273 static void fuse_vma_close(struct vm_area_struct *vma)
2275 filemap_write_and_wait(vma->vm_file->f_mapping);
2279 * Wait for writeback against this page to complete before allowing it
2280 * to be marked dirty again, and hence written back again, possibly
2281 * before the previous writepage completed.
2283 * Block here, instead of in ->writepage(), so that the userspace fs
2284 * can only block processes actually operating on the filesystem.
2286 * Otherwise unprivileged userspace fs would be able to block
2291 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2293 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2295 struct page *page = vmf->page;
2296 struct inode *inode = file_inode(vmf->vma->vm_file);
2298 file_update_time(vmf->vma->vm_file);
2300 if (page->mapping != inode->i_mapping) {
2302 return VM_FAULT_NOPAGE;
2305 fuse_wait_on_page_writeback(inode, page->index);
2306 return VM_FAULT_LOCKED;
2309 static const struct vm_operations_struct fuse_file_vm_ops = {
2310 .close = fuse_vma_close,
2311 .fault = filemap_fault,
2312 .map_pages = filemap_map_pages,
2313 .page_mkwrite = fuse_page_mkwrite,
2316 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2318 struct fuse_file *ff = file->private_data;
2320 if (ff->open_flags & FOPEN_DIRECT_IO) {
2321 /* Can't provide the coherency needed for MAP_SHARED */
2322 if (vma->vm_flags & VM_MAYSHARE)
2325 invalidate_inode_pages2(file->f_mapping);
2327 return generic_file_mmap(file, vma);
2330 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2331 fuse_link_write_file(file);
2333 file_accessed(file);
2334 vma->vm_ops = &fuse_file_vm_ops;
2338 static int convert_fuse_file_lock(struct fuse_conn *fc,
2339 const struct fuse_file_lock *ffl,
2340 struct file_lock *fl)
2342 switch (ffl->type) {
2348 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2349 ffl->end < ffl->start)
2352 fl->fl_start = ffl->start;
2353 fl->fl_end = ffl->end;
2356 * Convert pid into init's pid namespace. The locks API will
2357 * translate it into the caller's pid namespace.
2360 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2367 fl->fl_type = ffl->type;
2371 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2372 const struct file_lock *fl, int opcode, pid_t pid,
2373 int flock, struct fuse_lk_in *inarg)
2375 struct inode *inode = file_inode(file);
2376 struct fuse_conn *fc = get_fuse_conn(inode);
2377 struct fuse_file *ff = file->private_data;
2379 memset(inarg, 0, sizeof(*inarg));
2381 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2382 inarg->lk.start = fl->fl_start;
2383 inarg->lk.end = fl->fl_end;
2384 inarg->lk.type = fl->fl_type;
2385 inarg->lk.pid = pid;
2387 inarg->lk_flags |= FUSE_LK_FLOCK;
2388 args->opcode = opcode;
2389 args->nodeid = get_node_id(inode);
2390 args->in_numargs = 1;
2391 args->in_args[0].size = sizeof(*inarg);
2392 args->in_args[0].value = inarg;
2395 static int fuse_getlk(struct file *file, struct file_lock *fl)
2397 struct inode *inode = file_inode(file);
2398 struct fuse_conn *fc = get_fuse_conn(inode);
2400 struct fuse_lk_in inarg;
2401 struct fuse_lk_out outarg;
2404 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2405 args.out_numargs = 1;
2406 args.out_args[0].size = sizeof(outarg);
2407 args.out_args[0].value = &outarg;
2408 err = fuse_simple_request(fc, &args);
2410 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2415 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2417 struct inode *inode = file_inode(file);
2418 struct fuse_conn *fc = get_fuse_conn(inode);
2420 struct fuse_lk_in inarg;
2421 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2422 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2423 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2426 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2427 /* NLM needs asynchronous locks, which we don't support yet */
2431 /* Unlock on close is handled by the flush method */
2432 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2435 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2436 err = fuse_simple_request(fc, &args);
2438 /* locking is restartable */
2445 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2447 struct inode *inode = file_inode(file);
2448 struct fuse_conn *fc = get_fuse_conn(inode);
2451 if (cmd == F_CANCELLK) {
2453 } else if (cmd == F_GETLK) {
2455 posix_test_lock(file, fl);
2458 err = fuse_getlk(file, fl);
2461 err = posix_lock_file(file, fl, NULL);
2463 err = fuse_setlk(file, fl, 0);
2468 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2470 struct inode *inode = file_inode(file);
2471 struct fuse_conn *fc = get_fuse_conn(inode);
2475 err = locks_lock_file_wait(file, fl);
2477 struct fuse_file *ff = file->private_data;
2479 /* emulate flock with POSIX locks */
2481 err = fuse_setlk(file, fl, 1);
2487 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2489 struct inode *inode = mapping->host;
2490 struct fuse_conn *fc = get_fuse_conn(inode);
2492 struct fuse_bmap_in inarg;
2493 struct fuse_bmap_out outarg;
2496 if (!inode->i_sb->s_bdev || fc->no_bmap)
2499 memset(&inarg, 0, sizeof(inarg));
2500 inarg.block = block;
2501 inarg.blocksize = inode->i_sb->s_blocksize;
2502 args.opcode = FUSE_BMAP;
2503 args.nodeid = get_node_id(inode);
2504 args.in_numargs = 1;
2505 args.in_args[0].size = sizeof(inarg);
2506 args.in_args[0].value = &inarg;
2507 args.out_numargs = 1;
2508 args.out_args[0].size = sizeof(outarg);
2509 args.out_args[0].value = &outarg;
2510 err = fuse_simple_request(fc, &args);
2514 return err ? 0 : outarg.block;
2517 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2519 struct inode *inode = file->f_mapping->host;
2520 struct fuse_conn *fc = get_fuse_conn(inode);
2521 struct fuse_file *ff = file->private_data;
2523 struct fuse_lseek_in inarg = {
2528 struct fuse_lseek_out outarg;
2534 args.opcode = FUSE_LSEEK;
2535 args.nodeid = ff->nodeid;
2536 args.in_numargs = 1;
2537 args.in_args[0].size = sizeof(inarg);
2538 args.in_args[0].value = &inarg;
2539 args.out_numargs = 1;
2540 args.out_args[0].size = sizeof(outarg);
2541 args.out_args[0].value = &outarg;
2542 err = fuse_simple_request(fc, &args);
2544 if (err == -ENOSYS) {
2551 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2554 err = fuse_update_attributes(inode, file);
2556 return generic_file_llseek(file, offset, whence);
2561 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2564 struct inode *inode = file_inode(file);
2569 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2570 retval = generic_file_llseek(file, offset, whence);
2574 retval = fuse_update_attributes(inode, file);
2576 retval = generic_file_llseek(file, offset, whence);
2577 inode_unlock(inode);
2582 retval = fuse_lseek(file, offset, whence);
2583 inode_unlock(inode);
2593 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2594 * ABI was defined to be 'struct iovec' which is different on 32bit
2595 * and 64bit. Fortunately we can determine which structure the server
2596 * used from the size of the reply.
2598 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2599 size_t transferred, unsigned count,
2602 #ifdef CONFIG_COMPAT
2603 if (count * sizeof(struct compat_iovec) == transferred) {
2604 struct compat_iovec *ciov = src;
2608 * With this interface a 32bit server cannot support
2609 * non-compat (i.e. ones coming from 64bit apps) ioctl
2615 for (i = 0; i < count; i++) {
2616 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2617 dst[i].iov_len = ciov[i].iov_len;
2623 if (count * sizeof(struct iovec) != transferred)
2626 memcpy(dst, src, transferred);
2630 /* Make sure iov_length() won't overflow */
2631 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2635 u32 max = fc->max_pages << PAGE_SHIFT;
2637 for (n = 0; n < count; n++, iov++) {
2638 if (iov->iov_len > (size_t) max)
2640 max -= iov->iov_len;
2645 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2646 void *src, size_t transferred, unsigned count,
2650 struct fuse_ioctl_iovec *fiov = src;
2652 if (fc->minor < 16) {
2653 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2657 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2660 for (i = 0; i < count; i++) {
2661 /* Did the server supply an inappropriate value? */
2662 if (fiov[i].base != (unsigned long) fiov[i].base ||
2663 fiov[i].len != (unsigned long) fiov[i].len)
2666 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2667 dst[i].iov_len = (size_t) fiov[i].len;
2669 #ifdef CONFIG_COMPAT
2671 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2672 (compat_size_t) dst[i].iov_len != fiov[i].len))
2682 * For ioctls, there is no generic way to determine how much memory
2683 * needs to be read and/or written. Furthermore, ioctls are allowed
2684 * to dereference the passed pointer, so the parameter requires deep
2685 * copying but FUSE has no idea whatsoever about what to copy in or
2688 * This is solved by allowing FUSE server to retry ioctl with
2689 * necessary in/out iovecs. Let's assume the ioctl implementation
2690 * needs to read in the following structure.
2697 * On the first callout to FUSE server, inarg->in_size and
2698 * inarg->out_size will be NULL; then, the server completes the ioctl
2699 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2700 * the actual iov array to
2702 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2704 * which tells FUSE to copy in the requested area and retry the ioctl.
2705 * On the second round, the server has access to the structure and
2706 * from that it can tell what to look for next, so on the invocation,
2707 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2709 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2710 * { .iov_base = a.buf, .iov_len = a.buflen } }
2712 * FUSE will copy both struct a and the pointed buffer from the
2713 * process doing the ioctl and retry ioctl with both struct a and the
2716 * This time, FUSE server has everything it needs and completes ioctl
2717 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2719 * Copying data out works the same way.
2721 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2722 * automatically initializes in and out iovs by decoding @cmd with
2723 * _IOC_* macros and the server is not allowed to request RETRY. This
2724 * limits ioctl data transfers to well-formed ioctls and is the forced
2725 * behavior for all FUSE servers.
2727 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2730 struct fuse_file *ff = file->private_data;
2731 struct fuse_conn *fc = ff->fc;
2732 struct fuse_ioctl_in inarg = {
2738 struct fuse_ioctl_out outarg;
2739 struct iovec *iov_page = NULL;
2740 struct iovec *in_iov = NULL, *out_iov = NULL;
2741 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2742 size_t in_size, out_size, c;
2743 ssize_t transferred;
2746 struct fuse_args_pages ap = {};
2748 #if BITS_PER_LONG == 32
2749 inarg.flags |= FUSE_IOCTL_32BIT;
2751 if (flags & FUSE_IOCTL_COMPAT) {
2752 inarg.flags |= FUSE_IOCTL_32BIT;
2753 #ifdef CONFIG_X86_X32
2754 if (in_x32_syscall())
2755 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2760 /* assume all the iovs returned by client always fits in a page */
2761 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2764 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2765 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2766 if (!ap.pages || !iov_page)
2769 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2772 * If restricted, initialize IO parameters as encoded in @cmd.
2773 * RETRY from server is not allowed.
2775 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2776 struct iovec *iov = iov_page;
2778 iov->iov_base = (void __user *)arg;
2781 case FS_IOC_GETFLAGS:
2782 case FS_IOC_SETFLAGS:
2783 iov->iov_len = sizeof(int);
2786 iov->iov_len = _IOC_SIZE(cmd);
2790 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2795 if (_IOC_DIR(cmd) & _IOC_READ) {
2802 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2803 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2806 * Out data can be used either for actual out data or iovs,
2807 * make sure there always is at least one page.
2809 out_size = max_t(size_t, out_size, PAGE_SIZE);
2810 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2812 /* make sure there are enough buffer pages and init request with them */
2814 if (max_pages > fc->max_pages)
2816 while (ap.num_pages < max_pages) {
2817 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2818 if (!ap.pages[ap.num_pages])
2824 /* okay, let's send it to the client */
2825 ap.args.opcode = FUSE_IOCTL;
2826 ap.args.nodeid = ff->nodeid;
2827 ap.args.in_numargs = 1;
2828 ap.args.in_args[0].size = sizeof(inarg);
2829 ap.args.in_args[0].value = &inarg;
2831 ap.args.in_numargs++;
2832 ap.args.in_args[1].size = in_size;
2833 ap.args.in_pages = true;
2836 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2837 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2838 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2839 if (c != PAGE_SIZE && iov_iter_count(&ii))
2844 ap.args.out_numargs = 2;
2845 ap.args.out_args[0].size = sizeof(outarg);
2846 ap.args.out_args[0].value = &outarg;
2847 ap.args.out_args[1].size = out_size;
2848 ap.args.out_pages = true;
2849 ap.args.out_argvar = true;
2851 transferred = fuse_simple_request(fc, &ap.args);
2853 if (transferred < 0)
2856 /* did it ask for retry? */
2857 if (outarg.flags & FUSE_IOCTL_RETRY) {
2860 /* no retry if in restricted mode */
2862 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2865 in_iovs = outarg.in_iovs;
2866 out_iovs = outarg.out_iovs;
2869 * Make sure things are in boundary, separate checks
2870 * are to protect against overflow.
2873 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2874 out_iovs > FUSE_IOCTL_MAX_IOV ||
2875 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2878 vaddr = kmap_atomic(ap.pages[0]);
2879 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2880 transferred, in_iovs + out_iovs,
2881 (flags & FUSE_IOCTL_COMPAT) != 0);
2882 kunmap_atomic(vaddr);
2887 out_iov = in_iov + in_iovs;
2889 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2893 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2901 if (transferred > inarg.out_size)
2905 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2906 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2907 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2908 if (c != PAGE_SIZE && iov_iter_count(&ii))
2913 free_page((unsigned long) iov_page);
2914 while (ap.num_pages)
2915 __free_page(ap.pages[--ap.num_pages]);
2918 return err ? err : outarg.result;
2920 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2922 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2923 unsigned long arg, unsigned int flags)
2925 struct inode *inode = file_inode(file);
2926 struct fuse_conn *fc = get_fuse_conn(inode);
2928 if (!fuse_allow_current_process(fc))
2931 if (is_bad_inode(inode))
2934 return fuse_do_ioctl(file, cmd, arg, flags);
2937 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2940 return fuse_ioctl_common(file, cmd, arg, 0);
2943 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2946 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2950 * All files which have been polled are linked to RB tree
2951 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2952 * find the matching one.
2954 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2955 struct rb_node **parent_out)
2957 struct rb_node **link = &fc->polled_files.rb_node;
2958 struct rb_node *last = NULL;
2961 struct fuse_file *ff;
2964 ff = rb_entry(last, struct fuse_file, polled_node);
2967 link = &last->rb_left;
2968 else if (kh > ff->kh)
2969 link = &last->rb_right;
2980 * The file is about to be polled. Make sure it's on the polled_files
2981 * RB tree. Note that files once added to the polled_files tree are
2982 * not removed before the file is released. This is because a file
2983 * polled once is likely to be polled again.
2985 static void fuse_register_polled_file(struct fuse_conn *fc,
2986 struct fuse_file *ff)
2988 spin_lock(&fc->lock);
2989 if (RB_EMPTY_NODE(&ff->polled_node)) {
2990 struct rb_node **link, *parent;
2992 link = fuse_find_polled_node(fc, ff->kh, &parent);
2994 rb_link_node(&ff->polled_node, parent, link);
2995 rb_insert_color(&ff->polled_node, &fc->polled_files);
2997 spin_unlock(&fc->lock);
3000 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
3002 struct fuse_file *ff = file->private_data;
3003 struct fuse_conn *fc = ff->fc;
3004 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
3005 struct fuse_poll_out outarg;
3010 return DEFAULT_POLLMASK;
3012 poll_wait(file, &ff->poll_wait, wait);
3013 inarg.events = mangle_poll(poll_requested_events(wait));
3016 * Ask for notification iff there's someone waiting for it.
3017 * The client may ignore the flag and always notify.
3019 if (waitqueue_active(&ff->poll_wait)) {
3020 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
3021 fuse_register_polled_file(fc, ff);
3024 args.opcode = FUSE_POLL;
3025 args.nodeid = ff->nodeid;
3026 args.in_numargs = 1;
3027 args.in_args[0].size = sizeof(inarg);
3028 args.in_args[0].value = &inarg;
3029 args.out_numargs = 1;
3030 args.out_args[0].size = sizeof(outarg);
3031 args.out_args[0].value = &outarg;
3032 err = fuse_simple_request(fc, &args);
3035 return demangle_poll(outarg.revents);
3036 if (err == -ENOSYS) {
3038 return DEFAULT_POLLMASK;
3042 EXPORT_SYMBOL_GPL(fuse_file_poll);
3045 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3046 * wakes up the poll waiters.
3048 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3049 struct fuse_notify_poll_wakeup_out *outarg)
3051 u64 kh = outarg->kh;
3052 struct rb_node **link;
3054 spin_lock(&fc->lock);
3056 link = fuse_find_polled_node(fc, kh, NULL);
3058 struct fuse_file *ff;
3060 ff = rb_entry(*link, struct fuse_file, polled_node);
3061 wake_up_interruptible_sync(&ff->poll_wait);
3064 spin_unlock(&fc->lock);
3068 static void fuse_do_truncate(struct file *file)
3070 struct inode *inode = file->f_mapping->host;
3073 attr.ia_valid = ATTR_SIZE;
3074 attr.ia_size = i_size_read(inode);
3076 attr.ia_file = file;
3077 attr.ia_valid |= ATTR_FILE;
3079 fuse_do_setattr(file_dentry(file), &attr, file);
3082 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3084 return round_up(off, fc->max_pages << PAGE_SHIFT);
3088 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3090 DECLARE_COMPLETION_ONSTACK(wait);
3092 struct file *file = iocb->ki_filp;
3093 struct fuse_file *ff = file->private_data;
3094 bool async_dio = ff->fc->async_dio;
3096 struct inode *inode;
3098 size_t count = iov_iter_count(iter);
3099 loff_t offset = iocb->ki_pos;
3100 struct fuse_io_priv *io;
3103 inode = file->f_mapping->host;
3104 i_size = i_size_read(inode);
3106 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
3109 /* optimization for short read */
3110 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
3111 if (offset >= i_size)
3113 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3114 count = iov_iter_count(iter);
3117 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3120 spin_lock_init(&io->lock);
3121 kref_init(&io->refcnt);
3125 io->offset = offset;
3126 io->write = (iov_iter_rw(iter) == WRITE);
3129 * By default, we want to optimize all I/Os with async request
3130 * submission to the client filesystem if supported.
3132 io->async = async_dio;
3134 io->blocking = is_sync_kiocb(iocb);
3137 * We cannot asynchronously extend the size of a file.
3138 * In such case the aio will behave exactly like sync io.
3140 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3141 io->blocking = true;
3143 if (io->async && io->blocking) {
3145 * Additional reference to keep io around after
3146 * calling fuse_aio_complete()
3148 kref_get(&io->refcnt);
3152 if (iov_iter_rw(iter) == WRITE) {
3153 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3154 fuse_invalidate_attr(inode);
3156 ret = __fuse_direct_read(io, iter, &pos);
3160 bool blocking = io->blocking;
3162 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3164 /* we have a non-extending, async request, so return */
3166 return -EIOCBQUEUED;
3168 wait_for_completion(&wait);
3169 ret = fuse_get_res_by_io(io);
3172 kref_put(&io->refcnt, fuse_io_release);
3174 if (iov_iter_rw(iter) == WRITE) {
3176 fuse_write_update_size(inode, pos);
3177 else if (ret < 0 && offset + count > i_size)
3178 fuse_do_truncate(file);
3184 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3186 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3189 fuse_sync_writes(inode);
3194 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3197 struct fuse_file *ff = file->private_data;
3198 struct inode *inode = file_inode(file);
3199 struct fuse_inode *fi = get_fuse_inode(inode);
3200 struct fuse_conn *fc = ff->fc;
3202 struct fuse_fallocate_in inarg = {
3209 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3210 (mode & FALLOC_FL_PUNCH_HOLE);
3212 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3215 if (fc->no_fallocate)
3220 if (mode & FALLOC_FL_PUNCH_HOLE) {
3221 loff_t endbyte = offset + length - 1;
3223 err = fuse_writeback_range(inode, offset, endbyte);
3229 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3230 offset + length > i_size_read(inode)) {
3231 err = inode_newsize_ok(inode, offset + length);
3236 if (!(mode & FALLOC_FL_KEEP_SIZE))
3237 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3239 args.opcode = FUSE_FALLOCATE;
3240 args.nodeid = ff->nodeid;
3241 args.in_numargs = 1;
3242 args.in_args[0].size = sizeof(inarg);
3243 args.in_args[0].value = &inarg;
3244 err = fuse_simple_request(fc, &args);
3245 if (err == -ENOSYS) {
3246 fc->no_fallocate = 1;
3252 /* we could have extended the file */
3253 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3254 bool changed = fuse_write_update_size(inode, offset + length);
3256 if (changed && fc->writeback_cache)
3257 file_update_time(file);
3260 if (mode & FALLOC_FL_PUNCH_HOLE)
3261 truncate_pagecache_range(inode, offset, offset + length - 1);
3263 fuse_invalidate_attr(inode);
3266 if (!(mode & FALLOC_FL_KEEP_SIZE))
3267 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3270 inode_unlock(inode);
3275 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3276 struct file *file_out, loff_t pos_out,
3277 size_t len, unsigned int flags)
3279 struct fuse_file *ff_in = file_in->private_data;
3280 struct fuse_file *ff_out = file_out->private_data;
3281 struct inode *inode_in = file_inode(file_in);
3282 struct inode *inode_out = file_inode(file_out);
3283 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3284 struct fuse_conn *fc = ff_in->fc;
3286 struct fuse_copy_file_range_in inarg = {
3289 .nodeid_out = ff_out->nodeid,
3290 .fh_out = ff_out->fh,
3295 struct fuse_write_out outarg;
3297 /* mark unstable when write-back is not used, and file_out gets
3299 bool is_unstable = (!fc->writeback_cache) &&
3300 ((pos_out + len) > inode_out->i_size);
3302 if (fc->no_copy_file_range)
3305 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3308 inode_lock(inode_in);
3309 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3310 inode_unlock(inode_in);
3314 inode_lock(inode_out);
3316 err = file_modified(file_out);
3321 * Write out dirty pages in the destination file before sending the COPY
3322 * request to userspace. After the request is completed, truncate off
3323 * pages (including partial ones) from the cache that have been copied,
3324 * since these contain stale data at that point.
3326 * This should be mostly correct, but if the COPY writes to partial
3327 * pages (at the start or end) and the parts not covered by the COPY are
3328 * written through a memory map after calling fuse_writeback_range(),
3329 * then these partial page modifications will be lost on truncation.
3331 * It is unlikely that someone would rely on such mixed style
3332 * modifications. Yet this does give less guarantees than if the
3333 * copying was performed with write(2).
3335 * To fix this a i_mmap_sem style lock could be used to prevent new
3336 * faults while the copy is ongoing.
3338 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3343 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3345 args.opcode = FUSE_COPY_FILE_RANGE;
3346 args.nodeid = ff_in->nodeid;
3347 args.in_numargs = 1;
3348 args.in_args[0].size = sizeof(inarg);
3349 args.in_args[0].value = &inarg;
3350 args.out_numargs = 1;
3351 args.out_args[0].size = sizeof(outarg);
3352 args.out_args[0].value = &outarg;
3353 err = fuse_simple_request(fc, &args);
3354 if (err == -ENOSYS) {
3355 fc->no_copy_file_range = 1;
3361 truncate_inode_pages_range(inode_out->i_mapping,
3362 ALIGN_DOWN(pos_out, PAGE_SIZE),
3363 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3365 if (fc->writeback_cache) {
3366 fuse_write_update_size(inode_out, pos_out + outarg.size);
3367 file_update_time(file_out);
3370 fuse_invalidate_attr(inode_out);
3375 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3377 inode_unlock(inode_out);
3378 file_accessed(file_in);
3383 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3384 struct file *dst_file, loff_t dst_off,
3385 size_t len, unsigned int flags)
3389 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3392 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3393 ret = generic_copy_file_range(src_file, src_off, dst_file,
3394 dst_off, len, flags);
3398 static const struct file_operations fuse_file_operations = {
3399 .llseek = fuse_file_llseek,
3400 .read_iter = fuse_file_read_iter,
3401 .write_iter = fuse_file_write_iter,
3402 .mmap = fuse_file_mmap,
3404 .flush = fuse_flush,
3405 .release = fuse_release,
3406 .fsync = fuse_fsync,
3407 .lock = fuse_file_lock,
3408 .flock = fuse_file_flock,
3409 .splice_read = generic_file_splice_read,
3410 .splice_write = iter_file_splice_write,
3411 .unlocked_ioctl = fuse_file_ioctl,
3412 .compat_ioctl = fuse_file_compat_ioctl,
3413 .poll = fuse_file_poll,
3414 .fallocate = fuse_file_fallocate,
3415 .copy_file_range = fuse_copy_file_range,
3418 static const struct address_space_operations fuse_file_aops = {
3419 .readpage = fuse_readpage,
3420 .readahead = fuse_readahead,
3421 .writepage = fuse_writepage,
3422 .writepages = fuse_writepages,
3423 .launder_page = fuse_launder_page,
3424 .set_page_dirty = __set_page_dirty_nobuffers,
3426 .direct_IO = fuse_direct_IO,
3427 .write_begin = fuse_write_begin,
3428 .write_end = fuse_write_end,
3431 void fuse_init_file_inode(struct inode *inode)
3433 struct fuse_inode *fi = get_fuse_inode(inode);
3435 inode->i_fop = &fuse_file_operations;
3436 inode->i_data.a_ops = &fuse_file_aops;
3438 INIT_LIST_HEAD(&fi->write_files);
3439 INIT_LIST_HEAD(&fi->queued_writes);
3441 init_waitqueue_head(&fi->page_waitq);
3442 fi->writepages = RB_ROOT;