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>
22 static struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
27 pages = kzalloc(npages * (sizeof(struct page *) +
28 sizeof(struct fuse_page_desc)), flags);
29 *desc = (void *) (pages + npages);
34 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 int opcode, struct fuse_open_out *outargp)
37 struct fuse_open_in inarg;
40 memset(&inarg, 0, sizeof(inarg));
41 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 if (!fc->atomic_o_trunc)
43 inarg.flags &= ~O_TRUNC;
47 args.in_args[0].size = sizeof(inarg);
48 args.in_args[0].value = &inarg;
50 args.out_args[0].size = sizeof(*outargp);
51 args.out_args[0].value = outargp;
53 return fuse_simple_request(fc, &args);
56 struct fuse_release_args {
57 struct fuse_args args;
58 struct fuse_release_in inarg;
62 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
66 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
71 ff->release_args = kzalloc(sizeof(*ff->release_args),
73 if (!ff->release_args) {
78 INIT_LIST_HEAD(&ff->write_entry);
79 mutex_init(&ff->readdir.lock);
80 refcount_set(&ff->count, 1);
81 RB_CLEAR_NODE(&ff->polled_node);
82 init_waitqueue_head(&ff->poll_wait);
84 ff->kh = atomic64_inc_return(&fc->khctr);
89 void fuse_file_free(struct fuse_file *ff)
91 kfree(ff->release_args);
92 mutex_destroy(&ff->readdir.lock);
96 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
98 refcount_inc(&ff->count);
102 static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
105 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
111 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
113 if (refcount_dec_and_test(&ff->count)) {
114 struct fuse_args *args = &ff->release_args->args;
116 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
117 /* Do nothing when client does not implement 'open' */
118 fuse_release_end(ff->fc, args, 0);
120 fuse_simple_request(ff->fc, args);
121 fuse_release_end(ff->fc, args, 0);
123 args->end = fuse_release_end;
124 if (fuse_simple_background(ff->fc, args,
125 GFP_KERNEL | __GFP_NOFAIL))
126 fuse_release_end(ff->fc, args, -ENOTCONN);
132 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
135 struct fuse_file *ff;
136 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
138 ff = fuse_file_alloc(fc);
143 /* Default for no-open */
144 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
145 if (isdir ? !fc->no_opendir : !fc->no_open) {
146 struct fuse_open_out outarg;
149 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
152 ff->open_flags = outarg.open_flags;
154 } else if (err != -ENOSYS) {
166 ff->open_flags &= ~FOPEN_DIRECT_IO;
169 file->private_data = ff;
173 EXPORT_SYMBOL_GPL(fuse_do_open);
175 static void fuse_link_write_file(struct file *file)
177 struct inode *inode = file_inode(file);
178 struct fuse_inode *fi = get_fuse_inode(inode);
179 struct fuse_file *ff = file->private_data;
181 * file may be written through mmap, so chain it onto the
182 * inodes's write_file list
184 spin_lock(&fi->lock);
185 if (list_empty(&ff->write_entry))
186 list_add(&ff->write_entry, &fi->write_files);
187 spin_unlock(&fi->lock);
190 void fuse_finish_open(struct inode *inode, struct file *file)
192 struct fuse_file *ff = file->private_data;
193 struct fuse_conn *fc = get_fuse_conn(inode);
195 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
196 invalidate_inode_pages2(inode->i_mapping);
197 if (ff->open_flags & FOPEN_STREAM)
198 stream_open(inode, file);
199 else if (ff->open_flags & FOPEN_NONSEEKABLE)
200 nonseekable_open(inode, file);
201 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 struct fuse_inode *fi = get_fuse_inode(inode);
204 spin_lock(&fi->lock);
205 fi->attr_version = atomic64_inc_return(&fc->attr_version);
206 i_size_write(inode, 0);
207 spin_unlock(&fi->lock);
208 fuse_invalidate_attr(inode);
209 if (fc->writeback_cache)
210 file_update_time(file);
212 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
213 fuse_link_write_file(file);
216 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
218 struct fuse_conn *fc = get_fuse_conn(inode);
220 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
221 fc->atomic_o_trunc &&
224 err = generic_file_open(inode, file);
228 if (is_wb_truncate) {
230 fuse_set_nowrite(inode);
233 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
236 fuse_finish_open(inode, file);
238 if (is_wb_truncate) {
239 fuse_release_nowrite(inode);
246 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
247 int flags, int opcode)
249 struct fuse_conn *fc = ff->fc;
250 struct fuse_release_args *ra = ff->release_args;
252 /* Inode is NULL on error path of fuse_create_open() */
254 spin_lock(&fi->lock);
255 list_del(&ff->write_entry);
256 spin_unlock(&fi->lock);
258 spin_lock(&fc->lock);
259 if (!RB_EMPTY_NODE(&ff->polled_node))
260 rb_erase(&ff->polled_node, &fc->polled_files);
261 spin_unlock(&fc->lock);
263 wake_up_interruptible_all(&ff->poll_wait);
265 ra->inarg.fh = ff->fh;
266 ra->inarg.flags = flags;
267 ra->args.in_numargs = 1;
268 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
269 ra->args.in_args[0].value = &ra->inarg;
270 ra->args.opcode = opcode;
271 ra->args.nodeid = ff->nodeid;
272 ra->args.force = true;
273 ra->args.nocreds = true;
276 void fuse_release_common(struct file *file, bool isdir)
278 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
279 struct fuse_file *ff = file->private_data;
280 struct fuse_release_args *ra = ff->release_args;
281 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
283 fuse_prepare_release(fi, ff, file->f_flags, opcode);
286 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
287 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
290 /* Hold inode until release is finished */
291 ra->inode = igrab(file_inode(file));
294 * Normally this will send the RELEASE request, however if
295 * some asynchronous READ or WRITE requests are outstanding,
296 * the sending will be delayed.
298 * Make the release synchronous if this is a fuseblk mount,
299 * synchronous RELEASE is allowed (and desirable) in this case
300 * because the server can be trusted not to screw up.
302 fuse_file_put(ff, ff->fc->destroy, isdir);
305 static int fuse_open(struct inode *inode, struct file *file)
307 return fuse_open_common(inode, file, false);
310 static int fuse_release(struct inode *inode, struct file *file)
312 struct fuse_conn *fc = get_fuse_conn(inode);
314 /* see fuse_vma_close() for !writeback_cache case */
315 if (fc->writeback_cache)
316 write_inode_now(inode, 1);
318 fuse_release_common(file, false);
320 /* return value is ignored by VFS */
324 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
326 WARN_ON(refcount_read(&ff->count) > 1);
327 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
329 * iput(NULL) is a no-op and since the refcount is 1 and everything's
330 * synchronous, we are fine with not doing igrab() here"
332 fuse_file_put(ff, true, false);
334 EXPORT_SYMBOL_GPL(fuse_sync_release);
337 * Scramble the ID space with XTEA, so that the value of the files_struct
338 * pointer is not exposed to userspace.
340 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
342 u32 *k = fc->scramble_key;
343 u64 v = (unsigned long) id;
349 for (i = 0; i < 32; i++) {
350 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
352 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
355 return (u64) v0 + ((u64) v1 << 32);
358 struct fuse_writepage_args {
359 struct fuse_io_args ia;
360 struct rb_node writepages_entry;
361 struct list_head queue_entry;
362 struct fuse_writepage_args *next;
366 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
367 pgoff_t idx_from, pgoff_t idx_to)
371 n = fi->writepages.rb_node;
374 struct fuse_writepage_args *wpa;
377 wpa = rb_entry(n, struct fuse_writepage_args, writepages_entry);
378 WARN_ON(get_fuse_inode(wpa->inode) != fi);
379 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
380 if (idx_from >= curr_index + wpa->ia.ap.num_pages)
382 else if (idx_to < curr_index)
391 * Check if any page in a range is under writeback
393 * This is currently done by walking the list of writepage requests
394 * for the inode, which can be pretty inefficient.
396 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
399 struct fuse_inode *fi = get_fuse_inode(inode);
402 spin_lock(&fi->lock);
403 found = fuse_find_writeback(fi, idx_from, idx_to);
404 spin_unlock(&fi->lock);
409 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
411 return fuse_range_is_writeback(inode, index, index);
415 * Wait for page writeback to be completed.
417 * Since fuse doesn't rely on the VM writeback tracking, this has to
418 * use some other means.
420 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
422 struct fuse_inode *fi = get_fuse_inode(inode);
424 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
428 * Wait for all pending writepages on the inode to finish.
430 * This is currently done by blocking further writes with FUSE_NOWRITE
431 * and waiting for all sent writes to complete.
433 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
434 * could conflict with truncation.
436 static void fuse_sync_writes(struct inode *inode)
438 fuse_set_nowrite(inode);
439 fuse_release_nowrite(inode);
442 static int fuse_flush(struct file *file, fl_owner_t id)
444 struct inode *inode = file_inode(file);
445 struct fuse_conn *fc = get_fuse_conn(inode);
446 struct fuse_file *ff = file->private_data;
447 struct fuse_flush_in inarg;
451 if (is_bad_inode(inode))
454 err = write_inode_now(inode, 1);
459 fuse_sync_writes(inode);
462 err = filemap_check_errors(file->f_mapping);
470 memset(&inarg, 0, sizeof(inarg));
472 inarg.lock_owner = fuse_lock_owner_id(fc, id);
473 args.opcode = FUSE_FLUSH;
474 args.nodeid = get_node_id(inode);
476 args.in_args[0].size = sizeof(inarg);
477 args.in_args[0].value = &inarg;
480 err = fuse_simple_request(fc, &args);
481 if (err == -ENOSYS) {
488 * In memory i_blocks is not maintained by fuse, if writeback cache is
489 * enabled, i_blocks from cached attr may not be accurate.
491 if (!err && fc->writeback_cache)
492 fuse_invalidate_attr(inode);
496 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
497 int datasync, int opcode)
499 struct inode *inode = file->f_mapping->host;
500 struct fuse_conn *fc = get_fuse_conn(inode);
501 struct fuse_file *ff = file->private_data;
503 struct fuse_fsync_in inarg;
505 memset(&inarg, 0, sizeof(inarg));
507 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
508 args.opcode = opcode;
509 args.nodeid = get_node_id(inode);
511 args.in_args[0].size = sizeof(inarg);
512 args.in_args[0].value = &inarg;
513 return fuse_simple_request(fc, &args);
516 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
519 struct inode *inode = file->f_mapping->host;
520 struct fuse_conn *fc = get_fuse_conn(inode);
523 if (is_bad_inode(inode))
529 * Start writeback against all dirty pages of the inode, then
530 * wait for all outstanding writes, before sending the FSYNC
533 err = file_write_and_wait_range(file, start, end);
537 fuse_sync_writes(inode);
540 * Due to implementation of fuse writeback
541 * file_write_and_wait_range() does not catch errors.
542 * We have to do this directly after fuse_sync_writes()
544 err = file_check_and_advance_wb_err(file);
548 err = sync_inode_metadata(inode, 1);
555 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
556 if (err == -ENOSYS) {
566 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
567 size_t count, int opcode)
569 struct fuse_file *ff = file->private_data;
570 struct fuse_args *args = &ia->ap.args;
572 ia->read.in.fh = ff->fh;
573 ia->read.in.offset = pos;
574 ia->read.in.size = count;
575 ia->read.in.flags = file->f_flags;
576 args->opcode = opcode;
577 args->nodeid = ff->nodeid;
578 args->in_numargs = 1;
579 args->in_args[0].size = sizeof(ia->read.in);
580 args->in_args[0].value = &ia->read.in;
581 args->out_argvar = true;
582 args->out_numargs = 1;
583 args->out_args[0].size = count;
586 static void fuse_release_user_pages(struct fuse_args_pages *ap,
591 for (i = 0; i < ap->num_pages; i++) {
593 set_page_dirty_lock(ap->pages[i]);
594 put_page(ap->pages[i]);
598 static void fuse_io_release(struct kref *kref)
600 kfree(container_of(kref, struct fuse_io_priv, refcnt));
603 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
608 if (io->bytes >= 0 && io->write)
611 return io->bytes < 0 ? io->size : io->bytes;
615 * In case of short read, the caller sets 'pos' to the position of
616 * actual end of fuse request in IO request. Otherwise, if bytes_requested
617 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
620 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
621 * both submitted asynchronously. The first of them was ACKed by userspace as
622 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
623 * second request was ACKed as short, e.g. only 1K was read, resulting in
626 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
627 * will be equal to the length of the longest contiguous fragment of
628 * transferred data starting from the beginning of IO request.
630 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
634 spin_lock(&io->lock);
636 io->err = io->err ? : err;
637 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
641 if (!left && io->blocking)
643 spin_unlock(&io->lock);
645 if (!left && !io->blocking) {
646 ssize_t res = fuse_get_res_by_io(io);
649 struct inode *inode = file_inode(io->iocb->ki_filp);
650 struct fuse_conn *fc = get_fuse_conn(inode);
651 struct fuse_inode *fi = get_fuse_inode(inode);
653 spin_lock(&fi->lock);
654 fi->attr_version = atomic64_inc_return(&fc->attr_version);
655 spin_unlock(&fi->lock);
658 io->iocb->ki_complete(io->iocb, res, 0);
661 kref_put(&io->refcnt, fuse_io_release);
664 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
667 struct fuse_io_args *ia;
669 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
672 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
682 static void fuse_io_free(struct fuse_io_args *ia)
688 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
691 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
692 struct fuse_io_priv *io = ia->io;
695 fuse_release_user_pages(&ia->ap, io->should_dirty);
699 } else if (io->write) {
700 if (ia->write.out.size > ia->write.in.size) {
702 } else if (ia->write.in.size != ia->write.out.size) {
703 pos = ia->write.in.offset - io->offset +
707 u32 outsize = args->out_args[0].size;
709 if (ia->read.in.size != outsize)
710 pos = ia->read.in.offset - io->offset + outsize;
713 fuse_aio_complete(io, err, pos);
717 static ssize_t fuse_async_req_send(struct fuse_conn *fc,
718 struct fuse_io_args *ia, size_t num_bytes)
721 struct fuse_io_priv *io = ia->io;
723 spin_lock(&io->lock);
724 kref_get(&io->refcnt);
725 io->size += num_bytes;
727 spin_unlock(&io->lock);
729 ia->ap.args.end = fuse_aio_complete_req;
730 ia->ap.args.may_block = io->should_dirty;
731 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
733 fuse_aio_complete_req(fc, &ia->ap.args, err);
738 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
741 struct file *file = ia->io->iocb->ki_filp;
742 struct fuse_file *ff = file->private_data;
743 struct fuse_conn *fc = ff->fc;
745 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
747 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
748 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
752 return fuse_async_req_send(fc, ia, count);
754 return fuse_simple_request(fc, &ia->ap.args);
757 static void fuse_read_update_size(struct inode *inode, loff_t size,
760 struct fuse_conn *fc = get_fuse_conn(inode);
761 struct fuse_inode *fi = get_fuse_inode(inode);
763 spin_lock(&fi->lock);
764 if (attr_ver == fi->attr_version && size < inode->i_size &&
765 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
766 fi->attr_version = atomic64_inc_return(&fc->attr_version);
767 i_size_write(inode, size);
769 spin_unlock(&fi->lock);
772 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
773 struct fuse_args_pages *ap)
775 struct fuse_conn *fc = get_fuse_conn(inode);
777 if (fc->writeback_cache) {
779 * A hole in a file. Some data after the hole are in page cache,
780 * but have not reached the client fs yet. So, the hole is not
784 int start_idx = num_read >> PAGE_SHIFT;
785 size_t off = num_read & (PAGE_SIZE - 1);
787 for (i = start_idx; i < ap->num_pages; i++) {
788 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
792 loff_t pos = page_offset(ap->pages[0]) + num_read;
793 fuse_read_update_size(inode, pos, attr_ver);
797 static int fuse_do_readpage(struct file *file, struct page *page)
799 struct inode *inode = page->mapping->host;
800 struct fuse_conn *fc = get_fuse_conn(inode);
801 loff_t pos = page_offset(page);
802 struct fuse_page_desc desc = { .length = PAGE_SIZE };
803 struct fuse_io_args ia = {
804 .ap.args.page_zeroing = true,
805 .ap.args.out_pages = true,
814 * Page writeback can extend beyond the lifetime of the
815 * page-cache page, so make sure we read a properly synced
818 fuse_wait_on_page_writeback(inode, page->index);
820 attr_ver = fuse_get_attr_version(fc);
822 /* Don't overflow end offset */
823 if (pos + (desc.length - 1) == LLONG_MAX)
826 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
827 res = fuse_simple_request(fc, &ia.ap.args);
831 * Short read means EOF. If file size is larger, truncate it
833 if (res < desc.length)
834 fuse_short_read(inode, attr_ver, res, &ia.ap);
836 SetPageUptodate(page);
841 static int fuse_readpage(struct file *file, struct page *page)
843 struct inode *inode = page->mapping->host;
847 if (is_bad_inode(inode))
850 err = fuse_do_readpage(file, page);
851 fuse_invalidate_atime(inode);
857 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
861 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
862 struct fuse_args_pages *ap = &ia->ap;
863 size_t count = ia->read.in.size;
864 size_t num_read = args->out_args[0].size;
865 struct address_space *mapping = NULL;
867 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
868 mapping = ap->pages[i]->mapping;
871 struct inode *inode = mapping->host;
874 * Short read means EOF. If file size is larger, truncate it
876 if (!err && num_read < count)
877 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
879 fuse_invalidate_atime(inode);
882 for (i = 0; i < ap->num_pages; i++) {
883 struct page *page = ap->pages[i];
886 SetPageUptodate(page);
893 fuse_file_put(ia->ff, false, false);
898 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
900 struct fuse_file *ff = file->private_data;
901 struct fuse_conn *fc = ff->fc;
902 struct fuse_args_pages *ap = &ia->ap;
903 loff_t pos = page_offset(ap->pages[0]);
904 size_t count = ap->num_pages << PAGE_SHIFT;
908 ap->args.out_pages = true;
909 ap->args.page_zeroing = true;
910 ap->args.page_replace = true;
912 /* Don't overflow end offset */
913 if (pos + (count - 1) == LLONG_MAX) {
915 ap->descs[ap->num_pages - 1].length--;
917 WARN_ON((loff_t) (pos + count) < 0);
919 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
920 ia->read.attr_ver = fuse_get_attr_version(fc);
921 if (fc->async_read) {
922 ia->ff = fuse_file_get(ff);
923 ap->args.end = fuse_readpages_end;
924 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
928 res = fuse_simple_request(fc, &ap->args);
929 err = res < 0 ? res : 0;
931 fuse_readpages_end(fc, &ap->args, err);
934 static void fuse_readahead(struct readahead_control *rac)
936 struct inode *inode = rac->mapping->host;
937 struct fuse_conn *fc = get_fuse_conn(inode);
938 unsigned int i, max_pages, nr_pages = 0;
940 if (is_bad_inode(inode))
943 max_pages = min_t(unsigned int, fc->max_pages,
944 fc->max_read / PAGE_SIZE);
947 struct fuse_io_args *ia;
948 struct fuse_args_pages *ap;
950 nr_pages = readahead_count(rac) - nr_pages;
951 if (nr_pages > max_pages)
952 nr_pages = max_pages;
955 ia = fuse_io_alloc(NULL, nr_pages);
959 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
960 for (i = 0; i < nr_pages; i++) {
961 fuse_wait_on_page_writeback(inode,
962 readahead_index(rac) + i);
963 ap->descs[i].length = PAGE_SIZE;
965 ap->num_pages = nr_pages;
966 fuse_send_readpages(ia, rac->file);
970 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
972 struct inode *inode = iocb->ki_filp->f_mapping->host;
973 struct fuse_conn *fc = get_fuse_conn(inode);
976 * In auto invalidate mode, always update attributes on read.
977 * Otherwise, only update if we attempt to read past EOF (to ensure
978 * i_size is up to date).
980 if (fc->auto_inval_data ||
981 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
983 err = fuse_update_attributes(inode, iocb->ki_filp);
988 return generic_file_read_iter(iocb, to);
991 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
992 loff_t pos, size_t count)
994 struct fuse_args *args = &ia->ap.args;
996 ia->write.in.fh = ff->fh;
997 ia->write.in.offset = pos;
998 ia->write.in.size = count;
999 args->opcode = FUSE_WRITE;
1000 args->nodeid = ff->nodeid;
1001 args->in_numargs = 2;
1002 if (ff->fc->minor < 9)
1003 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1005 args->in_args[0].size = sizeof(ia->write.in);
1006 args->in_args[0].value = &ia->write.in;
1007 args->in_args[1].size = count;
1008 args->out_numargs = 1;
1009 args->out_args[0].size = sizeof(ia->write.out);
1010 args->out_args[0].value = &ia->write.out;
1013 static unsigned int fuse_write_flags(struct kiocb *iocb)
1015 unsigned int flags = iocb->ki_filp->f_flags;
1017 if (iocb->ki_flags & IOCB_DSYNC)
1019 if (iocb->ki_flags & IOCB_SYNC)
1025 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1026 size_t count, fl_owner_t owner)
1028 struct kiocb *iocb = ia->io->iocb;
1029 struct file *file = iocb->ki_filp;
1030 struct fuse_file *ff = file->private_data;
1031 struct fuse_conn *fc = ff->fc;
1032 struct fuse_write_in *inarg = &ia->write.in;
1035 fuse_write_args_fill(ia, ff, pos, count);
1036 inarg->flags = fuse_write_flags(iocb);
1037 if (owner != NULL) {
1038 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1039 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1043 return fuse_async_req_send(fc, ia, count);
1045 err = fuse_simple_request(fc, &ia->ap.args);
1046 if (!err && ia->write.out.size > count)
1049 return err ?: ia->write.out.size;
1052 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1054 struct fuse_conn *fc = get_fuse_conn(inode);
1055 struct fuse_inode *fi = get_fuse_inode(inode);
1058 spin_lock(&fi->lock);
1059 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1060 if (pos > inode->i_size) {
1061 i_size_write(inode, pos);
1064 spin_unlock(&fi->lock);
1069 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1070 struct kiocb *iocb, struct inode *inode,
1071 loff_t pos, size_t count)
1073 struct fuse_args_pages *ap = &ia->ap;
1074 struct file *file = iocb->ki_filp;
1075 struct fuse_file *ff = file->private_data;
1076 struct fuse_conn *fc = ff->fc;
1077 unsigned int offset, i;
1080 for (i = 0; i < ap->num_pages; i++)
1081 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1083 fuse_write_args_fill(ia, ff, pos, count);
1084 ia->write.in.flags = fuse_write_flags(iocb);
1086 err = fuse_simple_request(fc, &ap->args);
1087 if (!err && ia->write.out.size > count)
1090 offset = ap->descs[0].offset;
1091 count = ia->write.out.size;
1092 for (i = 0; i < ap->num_pages; i++) {
1093 struct page *page = ap->pages[i];
1095 if (!err && !offset && count >= PAGE_SIZE)
1096 SetPageUptodate(page);
1098 if (count > PAGE_SIZE - offset)
1099 count -= PAGE_SIZE - offset;
1111 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1112 struct address_space *mapping,
1113 struct iov_iter *ii, loff_t pos,
1114 unsigned int max_pages)
1116 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1117 unsigned offset = pos & (PAGE_SIZE - 1);
1121 ap->args.in_pages = true;
1122 ap->descs[0].offset = offset;
1127 pgoff_t index = pos >> PAGE_SHIFT;
1128 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1129 iov_iter_count(ii));
1131 bytes = min_t(size_t, bytes, fc->max_write - count);
1135 if (iov_iter_fault_in_readable(ii, bytes))
1139 page = grab_cache_page_write_begin(mapping, index, 0);
1143 if (mapping_writably_mapped(mapping))
1144 flush_dcache_page(page);
1146 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1147 flush_dcache_page(page);
1149 iov_iter_advance(ii, tmp);
1153 bytes = min(bytes, iov_iter_single_seg_count(ii));
1158 ap->pages[ap->num_pages] = page;
1159 ap->descs[ap->num_pages].length = tmp;
1165 if (offset == PAGE_SIZE)
1168 if (!fc->big_writes)
1170 } while (iov_iter_count(ii) && count < fc->max_write &&
1171 ap->num_pages < max_pages && offset == 0);
1173 return count > 0 ? count : err;
1176 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1177 unsigned int max_pages)
1179 return min_t(unsigned int,
1180 ((pos + len - 1) >> PAGE_SHIFT) -
1181 (pos >> PAGE_SHIFT) + 1,
1185 static ssize_t fuse_perform_write(struct kiocb *iocb,
1186 struct address_space *mapping,
1187 struct iov_iter *ii, loff_t pos)
1189 struct inode *inode = mapping->host;
1190 struct fuse_conn *fc = get_fuse_conn(inode);
1191 struct fuse_inode *fi = get_fuse_inode(inode);
1195 if (inode->i_size < pos + iov_iter_count(ii))
1196 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1200 struct fuse_io_args ia = {};
1201 struct fuse_args_pages *ap = &ia.ap;
1202 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1205 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1211 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1215 err = fuse_send_write_pages(&ia, iocb, inode,
1218 size_t num_written = ia.write.out.size;
1223 /* break out of the loop on short write */
1224 if (num_written != count)
1229 } while (!err && iov_iter_count(ii));
1232 fuse_write_update_size(inode, pos);
1234 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1235 fuse_invalidate_attr(inode);
1237 return res > 0 ? res : err;
1240 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1242 struct file *file = iocb->ki_filp;
1243 struct address_space *mapping = file->f_mapping;
1244 ssize_t written = 0;
1245 ssize_t written_buffered = 0;
1246 struct inode *inode = mapping->host;
1250 if (get_fuse_conn(inode)->writeback_cache) {
1251 /* Update size (EOF optimization) and mode (SUID clearing) */
1252 err = fuse_update_attributes(mapping->host, file);
1256 return generic_file_write_iter(iocb, from);
1261 /* We can write back this queue in page reclaim */
1262 current->backing_dev_info = inode_to_bdi(inode);
1264 err = generic_write_checks(iocb, from);
1268 err = file_remove_privs(file);
1272 err = file_update_time(file);
1276 if (iocb->ki_flags & IOCB_DIRECT) {
1277 loff_t pos = iocb->ki_pos;
1278 written = generic_file_direct_write(iocb, from);
1279 if (written < 0 || !iov_iter_count(from))
1284 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1285 if (written_buffered < 0) {
1286 err = written_buffered;
1289 endbyte = pos + written_buffered - 1;
1291 err = filemap_write_and_wait_range(file->f_mapping, pos,
1296 invalidate_mapping_pages(file->f_mapping,
1298 endbyte >> PAGE_SHIFT);
1300 written += written_buffered;
1301 iocb->ki_pos = pos + written_buffered;
1303 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1305 iocb->ki_pos += written;
1308 current->backing_dev_info = NULL;
1309 inode_unlock(inode);
1311 written = generic_write_sync(iocb, written);
1313 return written ? written : err;
1316 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1318 unsigned int nr_pages)
1322 for (i = index; i < index + nr_pages; i++)
1323 descs[i].length = PAGE_SIZE - descs[i].offset;
1326 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1328 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1331 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1334 return min(iov_iter_single_seg_count(ii), max_size);
1337 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1338 size_t *nbytesp, int write,
1339 unsigned int max_pages)
1341 size_t nbytes = 0; /* # bytes already packed in req */
1344 /* Special case for kernel I/O: can copy directly into the buffer */
1345 if (iov_iter_is_kvec(ii)) {
1346 unsigned long user_addr = fuse_get_user_addr(ii);
1347 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1350 ap->args.in_args[1].value = (void *) user_addr;
1352 ap->args.out_args[0].value = (void *) user_addr;
1354 iov_iter_advance(ii, frag_size);
1355 *nbytesp = frag_size;
1359 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1362 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1364 max_pages - ap->num_pages,
1369 iov_iter_advance(ii, ret);
1373 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1375 ap->descs[ap->num_pages].offset = start;
1376 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1378 ap->num_pages += npages;
1379 ap->descs[ap->num_pages - 1].length -=
1380 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1384 ap->args.in_pages = true;
1386 ap->args.out_pages = true;
1390 return ret < 0 ? ret : 0;
1393 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1394 loff_t *ppos, int flags)
1396 int write = flags & FUSE_DIO_WRITE;
1397 int cuse = flags & FUSE_DIO_CUSE;
1398 struct file *file = io->iocb->ki_filp;
1399 struct inode *inode = file->f_mapping->host;
1400 struct fuse_file *ff = file->private_data;
1401 struct fuse_conn *fc = ff->fc;
1402 size_t nmax = write ? fc->max_write : fc->max_read;
1404 size_t count = iov_iter_count(iter);
1405 pgoff_t idx_from = pos >> PAGE_SHIFT;
1406 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1409 struct fuse_io_args *ia;
1410 unsigned int max_pages;
1412 max_pages = iov_iter_npages(iter, fc->max_pages);
1413 ia = fuse_io_alloc(io, max_pages);
1418 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1421 fuse_sync_writes(inode);
1423 inode_unlock(inode);
1426 io->should_dirty = !write && iter_is_iovec(iter);
1429 fl_owner_t owner = current->files;
1430 size_t nbytes = min(count, nmax);
1432 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1438 if (!capable(CAP_FSETID))
1439 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1441 nres = fuse_send_write(ia, pos, nbytes, owner);
1443 nres = fuse_send_read(ia, pos, nbytes, owner);
1446 if (!io->async || nres < 0) {
1447 fuse_release_user_pages(&ia->ap, io->should_dirty);
1452 iov_iter_revert(iter, nbytes);
1456 WARN_ON(nres > nbytes);
1461 if (nres != nbytes) {
1462 iov_iter_revert(iter, nbytes - nres);
1466 max_pages = iov_iter_npages(iter, fc->max_pages);
1467 ia = fuse_io_alloc(io, max_pages);
1477 return res > 0 ? res : err;
1479 EXPORT_SYMBOL_GPL(fuse_direct_io);
1481 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1482 struct iov_iter *iter,
1486 struct inode *inode = file_inode(io->iocb->ki_filp);
1488 res = fuse_direct_io(io, iter, ppos, 0);
1490 fuse_invalidate_atime(inode);
1495 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1497 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1501 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1502 res = fuse_direct_IO(iocb, to);
1504 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1506 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1512 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1514 struct inode *inode = file_inode(iocb->ki_filp);
1515 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1518 /* Don't allow parallel writes to the same file */
1520 res = generic_write_checks(iocb, from);
1522 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1523 res = fuse_direct_IO(iocb, from);
1525 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1529 fuse_invalidate_attr(inode);
1531 fuse_write_update_size(inode, iocb->ki_pos);
1532 inode_unlock(inode);
1537 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1539 struct file *file = iocb->ki_filp;
1540 struct fuse_file *ff = file->private_data;
1542 if (is_bad_inode(file_inode(file)))
1545 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1546 return fuse_cache_read_iter(iocb, to);
1548 return fuse_direct_read_iter(iocb, to);
1551 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1553 struct file *file = iocb->ki_filp;
1554 struct fuse_file *ff = file->private_data;
1556 if (is_bad_inode(file_inode(file)))
1559 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1560 return fuse_cache_write_iter(iocb, from);
1562 return fuse_direct_write_iter(iocb, from);
1565 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1567 struct fuse_args_pages *ap = &wpa->ia.ap;
1570 for (i = 0; i < ap->num_pages; i++)
1571 __free_page(ap->pages[i]);
1574 fuse_file_put(wpa->ia.ff, false, false);
1580 static void fuse_writepage_finish(struct fuse_conn *fc,
1581 struct fuse_writepage_args *wpa)
1583 struct fuse_args_pages *ap = &wpa->ia.ap;
1584 struct inode *inode = wpa->inode;
1585 struct fuse_inode *fi = get_fuse_inode(inode);
1586 struct backing_dev_info *bdi = inode_to_bdi(inode);
1589 rb_erase(&wpa->writepages_entry, &fi->writepages);
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 fuse_writepage_finish(fc, wpa);
1641 spin_unlock(&fi->lock);
1643 /* After fuse_writepage_finish() aux request list is private */
1644 for (aux = wpa->next; aux; aux = next) {
1647 fuse_writepage_free(aux);
1650 fuse_writepage_free(wpa);
1651 spin_lock(&fi->lock);
1655 * If fi->writectr is positive (no truncate or fsync going on) send
1656 * all queued writepage requests.
1658 * Called with fi->lock
1660 void fuse_flush_writepages(struct inode *inode)
1661 __releases(fi->lock)
1662 __acquires(fi->lock)
1664 struct fuse_conn *fc = get_fuse_conn(inode);
1665 struct fuse_inode *fi = get_fuse_inode(inode);
1666 loff_t crop = i_size_read(inode);
1667 struct fuse_writepage_args *wpa;
1669 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1670 wpa = list_entry(fi->queued_writes.next,
1671 struct fuse_writepage_args, queue_entry);
1672 list_del_init(&wpa->queue_entry);
1673 fuse_send_writepage(fc, wpa, crop);
1677 static void tree_insert(struct rb_root *root, struct fuse_writepage_args *wpa)
1679 pgoff_t idx_from = wpa->ia.write.in.offset >> PAGE_SHIFT;
1680 pgoff_t idx_to = idx_from + wpa->ia.ap.num_pages - 1;
1681 struct rb_node **p = &root->rb_node;
1682 struct rb_node *parent = NULL;
1684 WARN_ON(!wpa->ia.ap.num_pages);
1686 struct fuse_writepage_args *curr;
1690 curr = rb_entry(parent, struct fuse_writepage_args,
1692 WARN_ON(curr->inode != wpa->inode);
1693 curr_index = curr->ia.write.in.offset >> PAGE_SHIFT;
1695 if (idx_from >= curr_index + curr->ia.ap.num_pages)
1696 p = &(*p)->rb_right;
1697 else if (idx_to < curr_index)
1700 return (void) WARN_ON(true);
1703 rb_link_node(&wpa->writepages_entry, parent, p);
1704 rb_insert_color(&wpa->writepages_entry, root);
1707 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1710 struct fuse_writepage_args *wpa =
1711 container_of(args, typeof(*wpa), ia.ap.args);
1712 struct inode *inode = wpa->inode;
1713 struct fuse_inode *fi = get_fuse_inode(inode);
1715 mapping_set_error(inode->i_mapping, error);
1716 spin_lock(&fi->lock);
1718 struct fuse_conn *fc = get_fuse_conn(inode);
1719 struct fuse_write_in *inarg = &wpa->ia.write.in;
1720 struct fuse_writepage_args *next = wpa->next;
1722 wpa->next = next->next;
1724 next->ia.ff = fuse_file_get(wpa->ia.ff);
1725 tree_insert(&fi->writepages, next);
1728 * Skip fuse_flush_writepages() to make it easy to crop requests
1729 * based on primary request size.
1731 * 1st case (trivial): there are no concurrent activities using
1732 * fuse_set/release_nowrite. Then we're on safe side because
1733 * fuse_flush_writepages() would call fuse_send_writepage()
1736 * 2nd case: someone called fuse_set_nowrite and it is waiting
1737 * now for completion of all in-flight requests. This happens
1738 * rarely and no more than once per page, so this should be
1741 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1742 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1743 * that fuse_set_nowrite returned implies that all in-flight
1744 * requests were completed along with all of their secondary
1745 * requests. Further primary requests are blocked by negative
1746 * writectr. Hence there cannot be any in-flight requests and
1747 * no invocations of fuse_writepage_end() while we're in
1748 * fuse_set_nowrite..fuse_release_nowrite section.
1750 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1753 fuse_writepage_finish(fc, wpa);
1754 spin_unlock(&fi->lock);
1755 fuse_writepage_free(wpa);
1758 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1759 struct fuse_inode *fi)
1761 struct fuse_file *ff = NULL;
1763 spin_lock(&fi->lock);
1764 if (!list_empty(&fi->write_files)) {
1765 ff = list_entry(fi->write_files.next, struct fuse_file,
1769 spin_unlock(&fi->lock);
1774 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1775 struct fuse_inode *fi)
1777 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1782 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1784 struct fuse_conn *fc = get_fuse_conn(inode);
1785 struct fuse_inode *fi = get_fuse_inode(inode);
1786 struct fuse_file *ff;
1789 ff = __fuse_write_file_get(fc, fi);
1790 err = fuse_flush_times(inode, ff);
1792 fuse_file_put(ff, false, false);
1797 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1799 struct fuse_writepage_args *wpa;
1800 struct fuse_args_pages *ap;
1802 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1806 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1816 static int fuse_writepage_locked(struct page *page)
1818 struct address_space *mapping = page->mapping;
1819 struct inode *inode = mapping->host;
1820 struct fuse_conn *fc = get_fuse_conn(inode);
1821 struct fuse_inode *fi = get_fuse_inode(inode);
1822 struct fuse_writepage_args *wpa;
1823 struct fuse_args_pages *ap;
1824 struct page *tmp_page;
1825 int error = -ENOMEM;
1827 set_page_writeback(page);
1829 wpa = fuse_writepage_args_alloc();
1834 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1839 wpa->ia.ff = fuse_write_file_get(fc, fi);
1843 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1845 copy_highpage(tmp_page, page);
1846 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1848 ap->args.in_pages = true;
1850 ap->pages[0] = tmp_page;
1851 ap->descs[0].offset = 0;
1852 ap->descs[0].length = PAGE_SIZE;
1853 ap->args.end = fuse_writepage_end;
1856 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1857 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1859 spin_lock(&fi->lock);
1860 tree_insert(&fi->writepages, wpa);
1861 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1862 fuse_flush_writepages(inode);
1863 spin_unlock(&fi->lock);
1865 end_page_writeback(page);
1870 __free_page(tmp_page);
1874 mapping_set_error(page->mapping, error);
1875 end_page_writeback(page);
1879 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1883 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1885 * ->writepages() should be called for sync() and friends. We
1886 * should only get here on direct reclaim and then we are
1887 * allowed to skip a page which is already in flight
1889 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1891 redirty_page_for_writepage(wbc, page);
1896 err = fuse_writepage_locked(page);
1902 struct fuse_fill_wb_data {
1903 struct fuse_writepage_args *wpa;
1904 struct fuse_file *ff;
1905 struct inode *inode;
1906 struct page **orig_pages;
1907 unsigned int max_pages;
1910 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1912 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1913 struct fuse_conn *fc = get_fuse_conn(data->inode);
1914 struct page **pages;
1915 struct fuse_page_desc *descs;
1916 unsigned int npages = min_t(unsigned int,
1917 max_t(unsigned int, data->max_pages * 2,
1918 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1920 WARN_ON(npages <= data->max_pages);
1922 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1926 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1927 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1931 data->max_pages = npages;
1936 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1938 struct fuse_writepage_args *wpa = data->wpa;
1939 struct inode *inode = data->inode;
1940 struct fuse_inode *fi = get_fuse_inode(inode);
1941 int num_pages = wpa->ia.ap.num_pages;
1944 wpa->ia.ff = fuse_file_get(data->ff);
1945 spin_lock(&fi->lock);
1946 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1947 fuse_flush_writepages(inode);
1948 spin_unlock(&fi->lock);
1950 for (i = 0; i < num_pages; i++)
1951 end_page_writeback(data->orig_pages[i]);
1955 * First recheck under fi->lock if the offending offset is still under
1956 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1957 * one already added for a page at this offset. If there's none, then insert
1958 * this new request onto the auxiliary list, otherwise reuse the existing one by
1959 * copying the new page contents over to the old temporary page.
1961 static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
1964 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1965 struct fuse_writepage_args *tmp;
1966 struct fuse_writepage_args *old_wpa;
1967 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
1969 WARN_ON(new_ap->num_pages != 0);
1971 spin_lock(&fi->lock);
1972 rb_erase(&new_wpa->writepages_entry, &fi->writepages);
1973 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1975 tree_insert(&fi->writepages, new_wpa);
1976 spin_unlock(&fi->lock);
1980 new_ap->num_pages = 1;
1981 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
1984 WARN_ON(tmp->inode != new_wpa->inode);
1985 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
1986 if (curr_index == page->index) {
1987 WARN_ON(tmp->ia.ap.num_pages != 1);
1988 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
1994 new_wpa->next = old_wpa->next;
1995 old_wpa->next = new_wpa;
1998 spin_unlock(&fi->lock);
2001 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
2003 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
2004 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
2005 wb_writeout_inc(&bdi->wb);
2006 fuse_writepage_free(new_wpa);
2012 static int fuse_writepages_fill(struct page *page,
2013 struct writeback_control *wbc, void *_data)
2015 struct fuse_fill_wb_data *data = _data;
2016 struct fuse_writepage_args *wpa = data->wpa;
2017 struct fuse_args_pages *ap = &wpa->ia.ap;
2018 struct inode *inode = data->inode;
2019 struct fuse_inode *fi = get_fuse_inode(inode);
2020 struct fuse_conn *fc = get_fuse_conn(inode);
2021 struct page *tmp_page;
2027 data->ff = fuse_write_file_get(fc, fi);
2033 * Being under writeback is unlikely but possible. For example direct
2034 * read to an mmaped fuse file will set the page dirty twice; once when
2035 * the pages are faulted with get_user_pages(), and then after the read
2038 is_writeback = fuse_page_is_writeback(inode, page->index);
2040 if (wpa && ap->num_pages &&
2041 (is_writeback || ap->num_pages == fc->max_pages ||
2042 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2043 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
2044 fuse_writepages_send(data);
2046 } else if (wpa && ap->num_pages == data->max_pages) {
2047 if (!fuse_pages_realloc(data)) {
2048 fuse_writepages_send(data);
2054 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2059 * The page must not be redirtied until the writeout is completed
2060 * (i.e. userspace has sent a reply to the write request). Otherwise
2061 * there could be more than one temporary page instance for each real
2064 * This is ensured by holding the page lock in page_mkwrite() while
2065 * checking fuse_page_is_writeback(). We already hold the page lock
2066 * since clear_page_dirty_for_io() and keep it held until we add the
2067 * request to the fi->writepages list and increment ap->num_pages.
2068 * After this fuse_page_is_writeback() will indicate that the page is
2069 * under writeback, so we can release the page lock.
2071 if (data->wpa == NULL) {
2073 wpa = fuse_writepage_args_alloc();
2075 __free_page(tmp_page);
2078 data->max_pages = 1;
2081 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2082 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2084 ap->args.in_pages = true;
2085 ap->args.end = fuse_writepage_end;
2089 spin_lock(&fi->lock);
2090 tree_insert(&fi->writepages, wpa);
2091 spin_unlock(&fi->lock);
2095 set_page_writeback(page);
2097 copy_highpage(tmp_page, page);
2098 ap->pages[ap->num_pages] = tmp_page;
2099 ap->descs[ap->num_pages].offset = 0;
2100 ap->descs[ap->num_pages].length = PAGE_SIZE;
2102 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2103 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2106 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
2107 end_page_writeback(page);
2111 data->orig_pages[ap->num_pages] = page;
2114 * Protected by fi->lock against concurrent access by
2115 * fuse_page_is_writeback().
2117 spin_lock(&fi->lock);
2119 spin_unlock(&fi->lock);
2127 static int fuse_writepages(struct address_space *mapping,
2128 struct writeback_control *wbc)
2130 struct inode *inode = mapping->host;
2131 struct fuse_conn *fc = get_fuse_conn(inode);
2132 struct fuse_fill_wb_data data;
2136 if (is_bad_inode(inode))
2144 data.orig_pages = kcalloc(fc->max_pages,
2145 sizeof(struct page *),
2147 if (!data.orig_pages)
2150 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2152 /* Ignore errors if we can write at least one page */
2153 WARN_ON(!data.wpa->ia.ap.num_pages);
2154 fuse_writepages_send(&data);
2158 fuse_file_put(data.ff, false, false);
2160 kfree(data.orig_pages);
2166 * It's worthy to make sure that space is reserved on disk for the write,
2167 * but how to implement it without killing performance need more thinking.
2169 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2170 loff_t pos, unsigned len, unsigned flags,
2171 struct page **pagep, void **fsdata)
2173 pgoff_t index = pos >> PAGE_SHIFT;
2174 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2179 WARN_ON(!fc->writeback_cache);
2181 page = grab_cache_page_write_begin(mapping, index, flags);
2185 fuse_wait_on_page_writeback(mapping->host, page->index);
2187 if (PageUptodate(page) || len == PAGE_SIZE)
2190 * Check if the start this page comes after the end of file, in which
2191 * case the readpage can be optimized away.
2193 fsize = i_size_read(mapping->host);
2194 if (fsize <= (pos & PAGE_MASK)) {
2195 size_t off = pos & ~PAGE_MASK;
2197 zero_user_segment(page, 0, off);
2200 err = fuse_do_readpage(file, page);
2214 static int fuse_write_end(struct file *file, struct address_space *mapping,
2215 loff_t pos, unsigned len, unsigned copied,
2216 struct page *page, void *fsdata)
2218 struct inode *inode = page->mapping->host;
2220 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2224 if (!PageUptodate(page)) {
2225 /* Zero any unwritten bytes at the end of the page */
2226 size_t endoff = (pos + copied) & ~PAGE_MASK;
2228 zero_user_segment(page, endoff, PAGE_SIZE);
2229 SetPageUptodate(page);
2232 fuse_write_update_size(inode, pos + copied);
2233 set_page_dirty(page);
2242 static int fuse_launder_page(struct page *page)
2245 if (clear_page_dirty_for_io(page)) {
2246 struct inode *inode = page->mapping->host;
2247 err = fuse_writepage_locked(page);
2249 fuse_wait_on_page_writeback(inode, page->index);
2255 * Write back dirty pages now, because there may not be any suitable
2258 static void fuse_vma_close(struct vm_area_struct *vma)
2260 filemap_write_and_wait(vma->vm_file->f_mapping);
2264 * Wait for writeback against this page to complete before allowing it
2265 * to be marked dirty again, and hence written back again, possibly
2266 * before the previous writepage completed.
2268 * Block here, instead of in ->writepage(), so that the userspace fs
2269 * can only block processes actually operating on the filesystem.
2271 * Otherwise unprivileged userspace fs would be able to block
2276 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2278 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2280 struct page *page = vmf->page;
2281 struct inode *inode = file_inode(vmf->vma->vm_file);
2283 file_update_time(vmf->vma->vm_file);
2285 if (page->mapping != inode->i_mapping) {
2287 return VM_FAULT_NOPAGE;
2290 fuse_wait_on_page_writeback(inode, page->index);
2291 return VM_FAULT_LOCKED;
2294 static const struct vm_operations_struct fuse_file_vm_ops = {
2295 .close = fuse_vma_close,
2296 .fault = filemap_fault,
2297 .map_pages = filemap_map_pages,
2298 .page_mkwrite = fuse_page_mkwrite,
2301 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2303 struct fuse_file *ff = file->private_data;
2305 if (ff->open_flags & FOPEN_DIRECT_IO) {
2306 /* Can't provide the coherency needed for MAP_SHARED */
2307 if (vma->vm_flags & VM_MAYSHARE)
2310 invalidate_inode_pages2(file->f_mapping);
2312 return generic_file_mmap(file, vma);
2315 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2316 fuse_link_write_file(file);
2318 file_accessed(file);
2319 vma->vm_ops = &fuse_file_vm_ops;
2323 static int convert_fuse_file_lock(struct fuse_conn *fc,
2324 const struct fuse_file_lock *ffl,
2325 struct file_lock *fl)
2327 switch (ffl->type) {
2333 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2334 ffl->end < ffl->start)
2337 fl->fl_start = ffl->start;
2338 fl->fl_end = ffl->end;
2341 * Convert pid into init's pid namespace. The locks API will
2342 * translate it into the caller's pid namespace.
2345 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2352 fl->fl_type = ffl->type;
2356 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2357 const struct file_lock *fl, int opcode, pid_t pid,
2358 int flock, struct fuse_lk_in *inarg)
2360 struct inode *inode = file_inode(file);
2361 struct fuse_conn *fc = get_fuse_conn(inode);
2362 struct fuse_file *ff = file->private_data;
2364 memset(inarg, 0, sizeof(*inarg));
2366 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2367 inarg->lk.start = fl->fl_start;
2368 inarg->lk.end = fl->fl_end;
2369 inarg->lk.type = fl->fl_type;
2370 inarg->lk.pid = pid;
2372 inarg->lk_flags |= FUSE_LK_FLOCK;
2373 args->opcode = opcode;
2374 args->nodeid = get_node_id(inode);
2375 args->in_numargs = 1;
2376 args->in_args[0].size = sizeof(*inarg);
2377 args->in_args[0].value = inarg;
2380 static int fuse_getlk(struct file *file, struct file_lock *fl)
2382 struct inode *inode = file_inode(file);
2383 struct fuse_conn *fc = get_fuse_conn(inode);
2385 struct fuse_lk_in inarg;
2386 struct fuse_lk_out outarg;
2389 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2390 args.out_numargs = 1;
2391 args.out_args[0].size = sizeof(outarg);
2392 args.out_args[0].value = &outarg;
2393 err = fuse_simple_request(fc, &args);
2395 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2400 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2402 struct inode *inode = file_inode(file);
2403 struct fuse_conn *fc = get_fuse_conn(inode);
2405 struct fuse_lk_in inarg;
2406 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2407 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2408 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2411 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2412 /* NLM needs asynchronous locks, which we don't support yet */
2416 /* Unlock on close is handled by the flush method */
2417 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2420 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2421 err = fuse_simple_request(fc, &args);
2423 /* locking is restartable */
2430 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2432 struct inode *inode = file_inode(file);
2433 struct fuse_conn *fc = get_fuse_conn(inode);
2436 if (cmd == F_CANCELLK) {
2438 } else if (cmd == F_GETLK) {
2440 posix_test_lock(file, fl);
2443 err = fuse_getlk(file, fl);
2446 err = posix_lock_file(file, fl, NULL);
2448 err = fuse_setlk(file, fl, 0);
2453 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2455 struct inode *inode = file_inode(file);
2456 struct fuse_conn *fc = get_fuse_conn(inode);
2460 err = locks_lock_file_wait(file, fl);
2462 struct fuse_file *ff = file->private_data;
2464 /* emulate flock with POSIX locks */
2466 err = fuse_setlk(file, fl, 1);
2472 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2474 struct inode *inode = mapping->host;
2475 struct fuse_conn *fc = get_fuse_conn(inode);
2477 struct fuse_bmap_in inarg;
2478 struct fuse_bmap_out outarg;
2481 if (!inode->i_sb->s_bdev || fc->no_bmap)
2484 memset(&inarg, 0, sizeof(inarg));
2485 inarg.block = block;
2486 inarg.blocksize = inode->i_sb->s_blocksize;
2487 args.opcode = FUSE_BMAP;
2488 args.nodeid = get_node_id(inode);
2489 args.in_numargs = 1;
2490 args.in_args[0].size = sizeof(inarg);
2491 args.in_args[0].value = &inarg;
2492 args.out_numargs = 1;
2493 args.out_args[0].size = sizeof(outarg);
2494 args.out_args[0].value = &outarg;
2495 err = fuse_simple_request(fc, &args);
2499 return err ? 0 : outarg.block;
2502 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2504 struct inode *inode = file->f_mapping->host;
2505 struct fuse_conn *fc = get_fuse_conn(inode);
2506 struct fuse_file *ff = file->private_data;
2508 struct fuse_lseek_in inarg = {
2513 struct fuse_lseek_out outarg;
2519 args.opcode = FUSE_LSEEK;
2520 args.nodeid = ff->nodeid;
2521 args.in_numargs = 1;
2522 args.in_args[0].size = sizeof(inarg);
2523 args.in_args[0].value = &inarg;
2524 args.out_numargs = 1;
2525 args.out_args[0].size = sizeof(outarg);
2526 args.out_args[0].value = &outarg;
2527 err = fuse_simple_request(fc, &args);
2529 if (err == -ENOSYS) {
2536 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2539 err = fuse_update_attributes(inode, file);
2541 return generic_file_llseek(file, offset, whence);
2546 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2549 struct inode *inode = file_inode(file);
2554 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2555 retval = generic_file_llseek(file, offset, whence);
2559 retval = fuse_update_attributes(inode, file);
2561 retval = generic_file_llseek(file, offset, whence);
2562 inode_unlock(inode);
2567 retval = fuse_lseek(file, offset, whence);
2568 inode_unlock(inode);
2578 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2579 * ABI was defined to be 'struct iovec' which is different on 32bit
2580 * and 64bit. Fortunately we can determine which structure the server
2581 * used from the size of the reply.
2583 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2584 size_t transferred, unsigned count,
2587 #ifdef CONFIG_COMPAT
2588 if (count * sizeof(struct compat_iovec) == transferred) {
2589 struct compat_iovec *ciov = src;
2593 * With this interface a 32bit server cannot support
2594 * non-compat (i.e. ones coming from 64bit apps) ioctl
2600 for (i = 0; i < count; i++) {
2601 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2602 dst[i].iov_len = ciov[i].iov_len;
2608 if (count * sizeof(struct iovec) != transferred)
2611 memcpy(dst, src, transferred);
2615 /* Make sure iov_length() won't overflow */
2616 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2620 u32 max = fc->max_pages << PAGE_SHIFT;
2622 for (n = 0; n < count; n++, iov++) {
2623 if (iov->iov_len > (size_t) max)
2625 max -= iov->iov_len;
2630 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2631 void *src, size_t transferred, unsigned count,
2635 struct fuse_ioctl_iovec *fiov = src;
2637 if (fc->minor < 16) {
2638 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2642 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2645 for (i = 0; i < count; i++) {
2646 /* Did the server supply an inappropriate value? */
2647 if (fiov[i].base != (unsigned long) fiov[i].base ||
2648 fiov[i].len != (unsigned long) fiov[i].len)
2651 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2652 dst[i].iov_len = (size_t) fiov[i].len;
2654 #ifdef CONFIG_COMPAT
2656 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2657 (compat_size_t) dst[i].iov_len != fiov[i].len))
2667 * For ioctls, there is no generic way to determine how much memory
2668 * needs to be read and/or written. Furthermore, ioctls are allowed
2669 * to dereference the passed pointer, so the parameter requires deep
2670 * copying but FUSE has no idea whatsoever about what to copy in or
2673 * This is solved by allowing FUSE server to retry ioctl with
2674 * necessary in/out iovecs. Let's assume the ioctl implementation
2675 * needs to read in the following structure.
2682 * On the first callout to FUSE server, inarg->in_size and
2683 * inarg->out_size will be NULL; then, the server completes the ioctl
2684 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2685 * the actual iov array to
2687 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2689 * which tells FUSE to copy in the requested area and retry the ioctl.
2690 * On the second round, the server has access to the structure and
2691 * from that it can tell what to look for next, so on the invocation,
2692 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2694 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2695 * { .iov_base = a.buf, .iov_len = a.buflen } }
2697 * FUSE will copy both struct a and the pointed buffer from the
2698 * process doing the ioctl and retry ioctl with both struct a and the
2701 * This time, FUSE server has everything it needs and completes ioctl
2702 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2704 * Copying data out works the same way.
2706 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2707 * automatically initializes in and out iovs by decoding @cmd with
2708 * _IOC_* macros and the server is not allowed to request RETRY. This
2709 * limits ioctl data transfers to well-formed ioctls and is the forced
2710 * behavior for all FUSE servers.
2712 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2715 struct fuse_file *ff = file->private_data;
2716 struct fuse_conn *fc = ff->fc;
2717 struct fuse_ioctl_in inarg = {
2723 struct fuse_ioctl_out outarg;
2724 struct iovec *iov_page = NULL;
2725 struct iovec *in_iov = NULL, *out_iov = NULL;
2726 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2727 size_t in_size, out_size, c;
2728 ssize_t transferred;
2731 struct fuse_args_pages ap = {};
2733 #if BITS_PER_LONG == 32
2734 inarg.flags |= FUSE_IOCTL_32BIT;
2736 if (flags & FUSE_IOCTL_COMPAT) {
2737 inarg.flags |= FUSE_IOCTL_32BIT;
2738 #ifdef CONFIG_X86_X32
2739 if (in_x32_syscall())
2740 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2745 /* assume all the iovs returned by client always fits in a page */
2746 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2749 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2750 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2751 if (!ap.pages || !iov_page)
2754 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2757 * If restricted, initialize IO parameters as encoded in @cmd.
2758 * RETRY from server is not allowed.
2760 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2761 struct iovec *iov = iov_page;
2763 iov->iov_base = (void __user *)arg;
2764 iov->iov_len = _IOC_SIZE(cmd);
2766 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2771 if (_IOC_DIR(cmd) & _IOC_READ) {
2778 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2779 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2782 * Out data can be used either for actual out data or iovs,
2783 * make sure there always is at least one page.
2785 out_size = max_t(size_t, out_size, PAGE_SIZE);
2786 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2788 /* make sure there are enough buffer pages and init request with them */
2790 if (max_pages > fc->max_pages)
2792 while (ap.num_pages < max_pages) {
2793 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2794 if (!ap.pages[ap.num_pages])
2800 /* okay, let's send it to the client */
2801 ap.args.opcode = FUSE_IOCTL;
2802 ap.args.nodeid = ff->nodeid;
2803 ap.args.in_numargs = 1;
2804 ap.args.in_args[0].size = sizeof(inarg);
2805 ap.args.in_args[0].value = &inarg;
2807 ap.args.in_numargs++;
2808 ap.args.in_args[1].size = in_size;
2809 ap.args.in_pages = true;
2812 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2813 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2814 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2815 if (c != PAGE_SIZE && iov_iter_count(&ii))
2820 ap.args.out_numargs = 2;
2821 ap.args.out_args[0].size = sizeof(outarg);
2822 ap.args.out_args[0].value = &outarg;
2823 ap.args.out_args[1].size = out_size;
2824 ap.args.out_pages = true;
2825 ap.args.out_argvar = true;
2827 transferred = fuse_simple_request(fc, &ap.args);
2829 if (transferred < 0)
2832 /* did it ask for retry? */
2833 if (outarg.flags & FUSE_IOCTL_RETRY) {
2836 /* no retry if in restricted mode */
2838 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2841 in_iovs = outarg.in_iovs;
2842 out_iovs = outarg.out_iovs;
2845 * Make sure things are in boundary, separate checks
2846 * are to protect against overflow.
2849 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2850 out_iovs > FUSE_IOCTL_MAX_IOV ||
2851 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2854 vaddr = kmap_atomic(ap.pages[0]);
2855 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2856 transferred, in_iovs + out_iovs,
2857 (flags & FUSE_IOCTL_COMPAT) != 0);
2858 kunmap_atomic(vaddr);
2863 out_iov = in_iov + in_iovs;
2865 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2869 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2877 if (transferred > inarg.out_size)
2881 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2882 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2883 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2884 if (c != PAGE_SIZE && iov_iter_count(&ii))
2889 free_page((unsigned long) iov_page);
2890 while (ap.num_pages)
2891 __free_page(ap.pages[--ap.num_pages]);
2894 return err ? err : outarg.result;
2896 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2898 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2899 unsigned long arg, unsigned int flags)
2901 struct inode *inode = file_inode(file);
2902 struct fuse_conn *fc = get_fuse_conn(inode);
2904 if (!fuse_allow_current_process(fc))
2907 if (is_bad_inode(inode))
2910 return fuse_do_ioctl(file, cmd, arg, flags);
2913 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2916 return fuse_ioctl_common(file, cmd, arg, 0);
2919 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2922 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2926 * All files which have been polled are linked to RB tree
2927 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2928 * find the matching one.
2930 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2931 struct rb_node **parent_out)
2933 struct rb_node **link = &fc->polled_files.rb_node;
2934 struct rb_node *last = NULL;
2937 struct fuse_file *ff;
2940 ff = rb_entry(last, struct fuse_file, polled_node);
2943 link = &last->rb_left;
2944 else if (kh > ff->kh)
2945 link = &last->rb_right;
2956 * The file is about to be polled. Make sure it's on the polled_files
2957 * RB tree. Note that files once added to the polled_files tree are
2958 * not removed before the file is released. This is because a file
2959 * polled once is likely to be polled again.
2961 static void fuse_register_polled_file(struct fuse_conn *fc,
2962 struct fuse_file *ff)
2964 spin_lock(&fc->lock);
2965 if (RB_EMPTY_NODE(&ff->polled_node)) {
2966 struct rb_node **link, *parent;
2968 link = fuse_find_polled_node(fc, ff->kh, &parent);
2970 rb_link_node(&ff->polled_node, parent, link);
2971 rb_insert_color(&ff->polled_node, &fc->polled_files);
2973 spin_unlock(&fc->lock);
2976 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2978 struct fuse_file *ff = file->private_data;
2979 struct fuse_conn *fc = ff->fc;
2980 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2981 struct fuse_poll_out outarg;
2986 return DEFAULT_POLLMASK;
2988 poll_wait(file, &ff->poll_wait, wait);
2989 inarg.events = mangle_poll(poll_requested_events(wait));
2992 * Ask for notification iff there's someone waiting for it.
2993 * The client may ignore the flag and always notify.
2995 if (waitqueue_active(&ff->poll_wait)) {
2996 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2997 fuse_register_polled_file(fc, ff);
3000 args.opcode = FUSE_POLL;
3001 args.nodeid = ff->nodeid;
3002 args.in_numargs = 1;
3003 args.in_args[0].size = sizeof(inarg);
3004 args.in_args[0].value = &inarg;
3005 args.out_numargs = 1;
3006 args.out_args[0].size = sizeof(outarg);
3007 args.out_args[0].value = &outarg;
3008 err = fuse_simple_request(fc, &args);
3011 return demangle_poll(outarg.revents);
3012 if (err == -ENOSYS) {
3014 return DEFAULT_POLLMASK;
3018 EXPORT_SYMBOL_GPL(fuse_file_poll);
3021 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3022 * wakes up the poll waiters.
3024 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3025 struct fuse_notify_poll_wakeup_out *outarg)
3027 u64 kh = outarg->kh;
3028 struct rb_node **link;
3030 spin_lock(&fc->lock);
3032 link = fuse_find_polled_node(fc, kh, NULL);
3034 struct fuse_file *ff;
3036 ff = rb_entry(*link, struct fuse_file, polled_node);
3037 wake_up_interruptible_sync(&ff->poll_wait);
3040 spin_unlock(&fc->lock);
3044 static void fuse_do_truncate(struct file *file)
3046 struct inode *inode = file->f_mapping->host;
3049 attr.ia_valid = ATTR_SIZE;
3050 attr.ia_size = i_size_read(inode);
3052 attr.ia_file = file;
3053 attr.ia_valid |= ATTR_FILE;
3055 fuse_do_setattr(file_dentry(file), &attr, file);
3058 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3060 return round_up(off, fc->max_pages << PAGE_SHIFT);
3064 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3066 DECLARE_COMPLETION_ONSTACK(wait);
3068 struct file *file = iocb->ki_filp;
3069 struct fuse_file *ff = file->private_data;
3070 bool async_dio = ff->fc->async_dio;
3072 struct inode *inode;
3074 size_t count = iov_iter_count(iter);
3075 loff_t offset = iocb->ki_pos;
3076 struct fuse_io_priv *io;
3079 inode = file->f_mapping->host;
3080 i_size = i_size_read(inode);
3082 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
3085 /* optimization for short read */
3086 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
3087 if (offset >= i_size)
3089 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3090 count = iov_iter_count(iter);
3093 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3096 spin_lock_init(&io->lock);
3097 kref_init(&io->refcnt);
3101 io->offset = offset;
3102 io->write = (iov_iter_rw(iter) == WRITE);
3105 * By default, we want to optimize all I/Os with async request
3106 * submission to the client filesystem if supported.
3108 io->async = async_dio;
3110 io->blocking = is_sync_kiocb(iocb);
3113 * We cannot asynchronously extend the size of a file.
3114 * In such case the aio will behave exactly like sync io.
3116 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3117 io->blocking = true;
3119 if (io->async && io->blocking) {
3121 * Additional reference to keep io around after
3122 * calling fuse_aio_complete()
3124 kref_get(&io->refcnt);
3128 if (iov_iter_rw(iter) == WRITE) {
3129 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3130 fuse_invalidate_attr(inode);
3132 ret = __fuse_direct_read(io, iter, &pos);
3136 bool blocking = io->blocking;
3138 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3140 /* we have a non-extending, async request, so return */
3142 return -EIOCBQUEUED;
3144 wait_for_completion(&wait);
3145 ret = fuse_get_res_by_io(io);
3148 kref_put(&io->refcnt, fuse_io_release);
3150 if (iov_iter_rw(iter) == WRITE) {
3152 fuse_write_update_size(inode, pos);
3153 else if (ret < 0 && offset + count > i_size)
3154 fuse_do_truncate(file);
3160 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3162 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3165 fuse_sync_writes(inode);
3170 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3173 struct fuse_file *ff = file->private_data;
3174 struct inode *inode = file_inode(file);
3175 struct fuse_inode *fi = get_fuse_inode(inode);
3176 struct fuse_conn *fc = ff->fc;
3178 struct fuse_fallocate_in inarg = {
3185 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3186 (mode & FALLOC_FL_PUNCH_HOLE);
3188 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3191 if (fc->no_fallocate)
3196 if (mode & FALLOC_FL_PUNCH_HOLE) {
3197 loff_t endbyte = offset + length - 1;
3199 err = fuse_writeback_range(inode, offset, endbyte);
3205 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3206 offset + length > i_size_read(inode)) {
3207 err = inode_newsize_ok(inode, offset + length);
3212 if (!(mode & FALLOC_FL_KEEP_SIZE))
3213 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3215 args.opcode = FUSE_FALLOCATE;
3216 args.nodeid = ff->nodeid;
3217 args.in_numargs = 1;
3218 args.in_args[0].size = sizeof(inarg);
3219 args.in_args[0].value = &inarg;
3220 err = fuse_simple_request(fc, &args);
3221 if (err == -ENOSYS) {
3222 fc->no_fallocate = 1;
3228 /* we could have extended the file */
3229 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3230 bool changed = fuse_write_update_size(inode, offset + length);
3232 if (changed && fc->writeback_cache)
3233 file_update_time(file);
3236 if (mode & FALLOC_FL_PUNCH_HOLE)
3237 truncate_pagecache_range(inode, offset, offset + length - 1);
3239 fuse_invalidate_attr(inode);
3242 if (!(mode & FALLOC_FL_KEEP_SIZE))
3243 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3246 inode_unlock(inode);
3251 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3252 struct file *file_out, loff_t pos_out,
3253 size_t len, unsigned int flags)
3255 struct fuse_file *ff_in = file_in->private_data;
3256 struct fuse_file *ff_out = file_out->private_data;
3257 struct inode *inode_in = file_inode(file_in);
3258 struct inode *inode_out = file_inode(file_out);
3259 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3260 struct fuse_conn *fc = ff_in->fc;
3262 struct fuse_copy_file_range_in inarg = {
3265 .nodeid_out = ff_out->nodeid,
3266 .fh_out = ff_out->fh,
3271 struct fuse_write_out outarg;
3273 /* mark unstable when write-back is not used, and file_out gets
3275 bool is_unstable = (!fc->writeback_cache) &&
3276 ((pos_out + len) > inode_out->i_size);
3278 if (fc->no_copy_file_range)
3281 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3284 inode_lock(inode_in);
3285 err = fuse_writeback_range(inode_in, pos_in, pos_in + len - 1);
3286 inode_unlock(inode_in);
3290 inode_lock(inode_out);
3292 err = file_modified(file_out);
3297 * Write out dirty pages in the destination file before sending the COPY
3298 * request to userspace. After the request is completed, truncate off
3299 * pages (including partial ones) from the cache that have been copied,
3300 * since these contain stale data at that point.
3302 * This should be mostly correct, but if the COPY writes to partial
3303 * pages (at the start or end) and the parts not covered by the COPY are
3304 * written through a memory map after calling fuse_writeback_range(),
3305 * then these partial page modifications will be lost on truncation.
3307 * It is unlikely that someone would rely on such mixed style
3308 * modifications. Yet this does give less guarantees than if the
3309 * copying was performed with write(2).
3311 * To fix this a i_mmap_sem style lock could be used to prevent new
3312 * faults while the copy is ongoing.
3314 err = fuse_writeback_range(inode_out, pos_out, pos_out + len - 1);
3319 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3321 args.opcode = FUSE_COPY_FILE_RANGE;
3322 args.nodeid = ff_in->nodeid;
3323 args.in_numargs = 1;
3324 args.in_args[0].size = sizeof(inarg);
3325 args.in_args[0].value = &inarg;
3326 args.out_numargs = 1;
3327 args.out_args[0].size = sizeof(outarg);
3328 args.out_args[0].value = &outarg;
3329 err = fuse_simple_request(fc, &args);
3330 if (err == -ENOSYS) {
3331 fc->no_copy_file_range = 1;
3337 truncate_inode_pages_range(inode_out->i_mapping,
3338 ALIGN_DOWN(pos_out, PAGE_SIZE),
3339 ALIGN(pos_out + outarg.size, PAGE_SIZE) - 1);
3341 if (fc->writeback_cache) {
3342 fuse_write_update_size(inode_out, pos_out + outarg.size);
3343 file_update_time(file_out);
3346 fuse_invalidate_attr(inode_out);
3351 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3353 inode_unlock(inode_out);
3354 file_accessed(file_in);
3359 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3360 struct file *dst_file, loff_t dst_off,
3361 size_t len, unsigned int flags)
3365 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3368 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3369 ret = generic_copy_file_range(src_file, src_off, dst_file,
3370 dst_off, len, flags);
3374 static const struct file_operations fuse_file_operations = {
3375 .llseek = fuse_file_llseek,
3376 .read_iter = fuse_file_read_iter,
3377 .write_iter = fuse_file_write_iter,
3378 .mmap = fuse_file_mmap,
3380 .flush = fuse_flush,
3381 .release = fuse_release,
3382 .fsync = fuse_fsync,
3383 .lock = fuse_file_lock,
3384 .flock = fuse_file_flock,
3385 .splice_read = generic_file_splice_read,
3386 .splice_write = iter_file_splice_write,
3387 .unlocked_ioctl = fuse_file_ioctl,
3388 .compat_ioctl = fuse_file_compat_ioctl,
3389 .poll = fuse_file_poll,
3390 .fallocate = fuse_file_fallocate,
3391 .copy_file_range = fuse_copy_file_range,
3394 static const struct address_space_operations fuse_file_aops = {
3395 .readpage = fuse_readpage,
3396 .readahead = fuse_readahead,
3397 .writepage = fuse_writepage,
3398 .writepages = fuse_writepages,
3399 .launder_page = fuse_launder_page,
3400 .set_page_dirty = __set_page_dirty_nobuffers,
3402 .direct_IO = fuse_direct_IO,
3403 .write_begin = fuse_write_begin,
3404 .write_end = fuse_write_end,
3407 void fuse_init_file_inode(struct inode *inode)
3409 struct fuse_inode *fi = get_fuse_inode(inode);
3411 inode->i_fop = &fuse_file_operations;
3412 inode->i_data.a_ops = &fuse_file_aops;
3414 INIT_LIST_HEAD(&fi->write_files);
3415 INIT_LIST_HEAD(&fi->queued_writes);
3417 init_waitqueue_head(&fi->page_waitq);
3418 fi->writepages = RB_ROOT;