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>
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 list_head 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)
369 struct fuse_writepage_args *wpa;
371 list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
374 WARN_ON(get_fuse_inode(wpa->inode) != fi);
375 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
376 if (idx_from < curr_index + wpa->ia.ap.num_pages &&
377 curr_index <= idx_to) {
385 * Check if any page in a range is under writeback
387 * This is currently done by walking the list of writepage requests
388 * for the inode, which can be pretty inefficient.
390 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
393 struct fuse_inode *fi = get_fuse_inode(inode);
396 spin_lock(&fi->lock);
397 found = fuse_find_writeback(fi, idx_from, idx_to);
398 spin_unlock(&fi->lock);
403 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
405 return fuse_range_is_writeback(inode, index, index);
409 * Wait for page writeback to be completed.
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
414 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
416 struct fuse_inode *fi = get_fuse_inode(inode);
418 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
422 * Wait for all pending writepages on the inode to finish.
424 * This is currently done by blocking further writes with FUSE_NOWRITE
425 * and waiting for all sent writes to complete.
427 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
428 * could conflict with truncation.
430 static void fuse_sync_writes(struct inode *inode)
432 fuse_set_nowrite(inode);
433 fuse_release_nowrite(inode);
436 static int fuse_flush(struct file *file, fl_owner_t id)
438 struct inode *inode = file_inode(file);
439 struct fuse_conn *fc = get_fuse_conn(inode);
440 struct fuse_file *ff = file->private_data;
441 struct fuse_flush_in inarg;
445 if (is_bad_inode(inode))
451 err = write_inode_now(inode, 1);
456 fuse_sync_writes(inode);
459 err = filemap_check_errors(file->f_mapping);
463 memset(&inarg, 0, sizeof(inarg));
465 inarg.lock_owner = fuse_lock_owner_id(fc, id);
466 args.opcode = FUSE_FLUSH;
467 args.nodeid = get_node_id(inode);
469 args.in_args[0].size = sizeof(inarg);
470 args.in_args[0].value = &inarg;
473 err = fuse_simple_request(fc, &args);
474 if (err == -ENOSYS) {
481 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
482 int datasync, int opcode)
484 struct inode *inode = file->f_mapping->host;
485 struct fuse_conn *fc = get_fuse_conn(inode);
486 struct fuse_file *ff = file->private_data;
488 struct fuse_fsync_in inarg;
490 memset(&inarg, 0, sizeof(inarg));
492 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
493 args.opcode = opcode;
494 args.nodeid = get_node_id(inode);
496 args.in_args[0].size = sizeof(inarg);
497 args.in_args[0].value = &inarg;
498 return fuse_simple_request(fc, &args);
501 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
504 struct inode *inode = file->f_mapping->host;
505 struct fuse_conn *fc = get_fuse_conn(inode);
508 if (is_bad_inode(inode))
514 * Start writeback against all dirty pages of the inode, then
515 * wait for all outstanding writes, before sending the FSYNC
518 err = file_write_and_wait_range(file, start, end);
522 fuse_sync_writes(inode);
525 * Due to implementation of fuse writeback
526 * file_write_and_wait_range() does not catch errors.
527 * We have to do this directly after fuse_sync_writes()
529 err = file_check_and_advance_wb_err(file);
533 err = sync_inode_metadata(inode, 1);
540 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
541 if (err == -ENOSYS) {
551 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
552 size_t count, int opcode)
554 struct fuse_file *ff = file->private_data;
555 struct fuse_args *args = &ia->ap.args;
557 ia->read.in.fh = ff->fh;
558 ia->read.in.offset = pos;
559 ia->read.in.size = count;
560 ia->read.in.flags = file->f_flags;
561 args->opcode = opcode;
562 args->nodeid = ff->nodeid;
563 args->in_numargs = 1;
564 args->in_args[0].size = sizeof(ia->read.in);
565 args->in_args[0].value = &ia->read.in;
566 args->out_argvar = true;
567 args->out_numargs = 1;
568 args->out_args[0].size = count;
571 static void fuse_release_user_pages(struct fuse_args_pages *ap,
576 for (i = 0; i < ap->num_pages; i++) {
578 set_page_dirty_lock(ap->pages[i]);
579 put_page(ap->pages[i]);
583 static void fuse_io_release(struct kref *kref)
585 kfree(container_of(kref, struct fuse_io_priv, refcnt));
588 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
593 if (io->bytes >= 0 && io->write)
596 return io->bytes < 0 ? io->size : io->bytes;
600 * In case of short read, the caller sets 'pos' to the position of
601 * actual end of fuse request in IO request. Otherwise, if bytes_requested
602 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
605 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
606 * both submitted asynchronously. The first of them was ACKed by userspace as
607 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
608 * second request was ACKed as short, e.g. only 1K was read, resulting in
611 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
612 * will be equal to the length of the longest contiguous fragment of
613 * transferred data starting from the beginning of IO request.
615 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
619 spin_lock(&io->lock);
621 io->err = io->err ? : err;
622 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
626 if (!left && io->blocking)
628 spin_unlock(&io->lock);
630 if (!left && !io->blocking) {
631 ssize_t res = fuse_get_res_by_io(io);
634 struct inode *inode = file_inode(io->iocb->ki_filp);
635 struct fuse_conn *fc = get_fuse_conn(inode);
636 struct fuse_inode *fi = get_fuse_inode(inode);
638 spin_lock(&fi->lock);
639 fi->attr_version = atomic64_inc_return(&fc->attr_version);
640 spin_unlock(&fi->lock);
643 io->iocb->ki_complete(io->iocb, res, 0);
646 kref_put(&io->refcnt, fuse_io_release);
649 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
652 struct fuse_io_args *ia;
654 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
657 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
667 static void fuse_io_free(struct fuse_io_args *ia)
673 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
676 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
677 struct fuse_io_priv *io = ia->io;
680 fuse_release_user_pages(&ia->ap, io->should_dirty);
684 } else if (io->write) {
685 if (ia->write.out.size > ia->write.in.size) {
687 } else if (ia->write.in.size != ia->write.out.size) {
688 pos = ia->write.in.offset - io->offset +
692 u32 outsize = args->out_args[0].size;
694 if (ia->read.in.size != outsize)
695 pos = ia->read.in.offset - io->offset + outsize;
698 fuse_aio_complete(io, err, pos);
702 static ssize_t fuse_async_req_send(struct fuse_conn *fc,
703 struct fuse_io_args *ia, size_t num_bytes)
706 struct fuse_io_priv *io = ia->io;
708 spin_lock(&io->lock);
709 kref_get(&io->refcnt);
710 io->size += num_bytes;
712 spin_unlock(&io->lock);
714 ia->ap.args.end = fuse_aio_complete_req;
715 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
717 fuse_aio_complete_req(fc, &ia->ap.args, err);
722 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
725 struct file *file = ia->io->iocb->ki_filp;
726 struct fuse_file *ff = file->private_data;
727 struct fuse_conn *fc = ff->fc;
729 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
731 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
732 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
736 return fuse_async_req_send(fc, ia, count);
738 return fuse_simple_request(fc, &ia->ap.args);
741 static void fuse_read_update_size(struct inode *inode, loff_t size,
744 struct fuse_conn *fc = get_fuse_conn(inode);
745 struct fuse_inode *fi = get_fuse_inode(inode);
747 spin_lock(&fi->lock);
748 if (attr_ver == fi->attr_version && size < inode->i_size &&
749 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
750 fi->attr_version = atomic64_inc_return(&fc->attr_version);
751 i_size_write(inode, size);
753 spin_unlock(&fi->lock);
756 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
757 struct fuse_args_pages *ap)
759 struct fuse_conn *fc = get_fuse_conn(inode);
761 if (fc->writeback_cache) {
763 * A hole in a file. Some data after the hole are in page cache,
764 * but have not reached the client fs yet. So, the hole is not
768 int start_idx = num_read >> PAGE_SHIFT;
769 size_t off = num_read & (PAGE_SIZE - 1);
771 for (i = start_idx; i < ap->num_pages; i++) {
772 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
776 loff_t pos = page_offset(ap->pages[0]) + num_read;
777 fuse_read_update_size(inode, pos, attr_ver);
781 static int fuse_do_readpage(struct file *file, struct page *page)
783 struct inode *inode = page->mapping->host;
784 struct fuse_conn *fc = get_fuse_conn(inode);
785 loff_t pos = page_offset(page);
786 struct fuse_page_desc desc = { .length = PAGE_SIZE };
787 struct fuse_io_args ia = {
788 .ap.args.page_zeroing = true,
789 .ap.args.out_pages = true,
798 * Page writeback can extend beyond the lifetime of the
799 * page-cache page, so make sure we read a properly synced
802 fuse_wait_on_page_writeback(inode, page->index);
804 attr_ver = fuse_get_attr_version(fc);
806 /* Don't overflow end offset */
807 if (pos + (desc.length - 1) == LLONG_MAX)
810 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
811 res = fuse_simple_request(fc, &ia.ap.args);
815 * Short read means EOF. If file size is larger, truncate it
817 if (res < desc.length)
818 fuse_short_read(inode, attr_ver, res, &ia.ap);
820 SetPageUptodate(page);
825 static int fuse_readpage(struct file *file, struct page *page)
827 struct inode *inode = page->mapping->host;
831 if (is_bad_inode(inode))
834 err = fuse_do_readpage(file, page);
835 fuse_invalidate_atime(inode);
841 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
845 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
846 struct fuse_args_pages *ap = &ia->ap;
847 size_t count = ia->read.in.size;
848 size_t num_read = args->out_args[0].size;
849 struct address_space *mapping = NULL;
851 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
852 mapping = ap->pages[i]->mapping;
855 struct inode *inode = mapping->host;
858 * Short read means EOF. If file size is larger, truncate it
860 if (!err && num_read < count)
861 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
863 fuse_invalidate_atime(inode);
866 for (i = 0; i < ap->num_pages; i++) {
867 struct page *page = ap->pages[i];
870 SetPageUptodate(page);
877 fuse_file_put(ia->ff, false, false);
882 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
884 struct fuse_file *ff = file->private_data;
885 struct fuse_conn *fc = ff->fc;
886 struct fuse_args_pages *ap = &ia->ap;
887 loff_t pos = page_offset(ap->pages[0]);
888 size_t count = ap->num_pages << PAGE_SHIFT;
892 ap->args.out_pages = true;
893 ap->args.page_zeroing = true;
894 ap->args.page_replace = true;
896 /* Don't overflow end offset */
897 if (pos + (count - 1) == LLONG_MAX) {
899 ap->descs[ap->num_pages - 1].length--;
901 WARN_ON((loff_t) (pos + count) < 0);
903 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
904 ia->read.attr_ver = fuse_get_attr_version(fc);
905 if (fc->async_read) {
906 ia->ff = fuse_file_get(ff);
907 ap->args.end = fuse_readpages_end;
908 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
912 res = fuse_simple_request(fc, &ap->args);
913 err = res < 0 ? res : 0;
915 fuse_readpages_end(fc, &ap->args, err);
918 static void fuse_readahead(struct readahead_control *rac)
920 struct inode *inode = rac->mapping->host;
921 struct fuse_conn *fc = get_fuse_conn(inode);
922 unsigned int i, max_pages, nr_pages = 0;
924 if (is_bad_inode(inode))
927 max_pages = min_t(unsigned int, fc->max_pages,
928 fc->max_read / PAGE_SIZE);
931 struct fuse_io_args *ia;
932 struct fuse_args_pages *ap;
934 nr_pages = readahead_count(rac) - nr_pages;
935 if (nr_pages > max_pages)
936 nr_pages = max_pages;
939 ia = fuse_io_alloc(NULL, nr_pages);
943 nr_pages = __readahead_batch(rac, ap->pages, nr_pages);
944 for (i = 0; i < nr_pages; i++) {
945 fuse_wait_on_page_writeback(inode,
946 readahead_index(rac) + i);
947 ap->descs[i].length = PAGE_SIZE;
949 ap->num_pages = nr_pages;
950 fuse_send_readpages(ia, rac->file);
954 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
956 struct inode *inode = iocb->ki_filp->f_mapping->host;
957 struct fuse_conn *fc = get_fuse_conn(inode);
960 * In auto invalidate mode, always update attributes on read.
961 * Otherwise, only update if we attempt to read past EOF (to ensure
962 * i_size is up to date).
964 if (fc->auto_inval_data ||
965 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
967 err = fuse_update_attributes(inode, iocb->ki_filp);
972 return generic_file_read_iter(iocb, to);
975 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
976 loff_t pos, size_t count)
978 struct fuse_args *args = &ia->ap.args;
980 ia->write.in.fh = ff->fh;
981 ia->write.in.offset = pos;
982 ia->write.in.size = count;
983 args->opcode = FUSE_WRITE;
984 args->nodeid = ff->nodeid;
985 args->in_numargs = 2;
986 if (ff->fc->minor < 9)
987 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
989 args->in_args[0].size = sizeof(ia->write.in);
990 args->in_args[0].value = &ia->write.in;
991 args->in_args[1].size = count;
992 args->out_numargs = 1;
993 args->out_args[0].size = sizeof(ia->write.out);
994 args->out_args[0].value = &ia->write.out;
997 static unsigned int fuse_write_flags(struct kiocb *iocb)
999 unsigned int flags = iocb->ki_filp->f_flags;
1001 if (iocb->ki_flags & IOCB_DSYNC)
1003 if (iocb->ki_flags & IOCB_SYNC)
1009 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1010 size_t count, fl_owner_t owner)
1012 struct kiocb *iocb = ia->io->iocb;
1013 struct file *file = iocb->ki_filp;
1014 struct fuse_file *ff = file->private_data;
1015 struct fuse_conn *fc = ff->fc;
1016 struct fuse_write_in *inarg = &ia->write.in;
1019 fuse_write_args_fill(ia, ff, pos, count);
1020 inarg->flags = fuse_write_flags(iocb);
1021 if (owner != NULL) {
1022 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1023 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1027 return fuse_async_req_send(fc, ia, count);
1029 err = fuse_simple_request(fc, &ia->ap.args);
1030 if (!err && ia->write.out.size > count)
1033 return err ?: ia->write.out.size;
1036 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1038 struct fuse_conn *fc = get_fuse_conn(inode);
1039 struct fuse_inode *fi = get_fuse_inode(inode);
1042 spin_lock(&fi->lock);
1043 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1044 if (pos > inode->i_size) {
1045 i_size_write(inode, pos);
1048 spin_unlock(&fi->lock);
1053 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1054 struct kiocb *iocb, struct inode *inode,
1055 loff_t pos, size_t count)
1057 struct fuse_args_pages *ap = &ia->ap;
1058 struct file *file = iocb->ki_filp;
1059 struct fuse_file *ff = file->private_data;
1060 struct fuse_conn *fc = ff->fc;
1061 unsigned int offset, i;
1064 for (i = 0; i < ap->num_pages; i++)
1065 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1067 fuse_write_args_fill(ia, ff, pos, count);
1068 ia->write.in.flags = fuse_write_flags(iocb);
1070 err = fuse_simple_request(fc, &ap->args);
1071 if (!err && ia->write.out.size > count)
1074 offset = ap->descs[0].offset;
1075 count = ia->write.out.size;
1076 for (i = 0; i < ap->num_pages; i++) {
1077 struct page *page = ap->pages[i];
1079 if (!err && !offset && count >= PAGE_SIZE)
1080 SetPageUptodate(page);
1082 if (count > PAGE_SIZE - offset)
1083 count -= PAGE_SIZE - offset;
1095 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1096 struct address_space *mapping,
1097 struct iov_iter *ii, loff_t pos,
1098 unsigned int max_pages)
1100 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1101 unsigned offset = pos & (PAGE_SIZE - 1);
1105 ap->args.in_pages = true;
1106 ap->descs[0].offset = offset;
1111 pgoff_t index = pos >> PAGE_SHIFT;
1112 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1113 iov_iter_count(ii));
1115 bytes = min_t(size_t, bytes, fc->max_write - count);
1119 if (iov_iter_fault_in_readable(ii, bytes))
1123 page = grab_cache_page_write_begin(mapping, index, 0);
1127 if (mapping_writably_mapped(mapping))
1128 flush_dcache_page(page);
1130 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1131 flush_dcache_page(page);
1133 iov_iter_advance(ii, tmp);
1137 bytes = min(bytes, iov_iter_single_seg_count(ii));
1142 ap->pages[ap->num_pages] = page;
1143 ap->descs[ap->num_pages].length = tmp;
1149 if (offset == PAGE_SIZE)
1152 if (!fc->big_writes)
1154 } while (iov_iter_count(ii) && count < fc->max_write &&
1155 ap->num_pages < max_pages && offset == 0);
1157 return count > 0 ? count : err;
1160 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1161 unsigned int max_pages)
1163 return min_t(unsigned int,
1164 ((pos + len - 1) >> PAGE_SHIFT) -
1165 (pos >> PAGE_SHIFT) + 1,
1169 static ssize_t fuse_perform_write(struct kiocb *iocb,
1170 struct address_space *mapping,
1171 struct iov_iter *ii, loff_t pos)
1173 struct inode *inode = mapping->host;
1174 struct fuse_conn *fc = get_fuse_conn(inode);
1175 struct fuse_inode *fi = get_fuse_inode(inode);
1179 if (inode->i_size < pos + iov_iter_count(ii))
1180 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1184 struct fuse_io_args ia = {};
1185 struct fuse_args_pages *ap = &ia.ap;
1186 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1189 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1195 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1199 err = fuse_send_write_pages(&ia, iocb, inode,
1202 size_t num_written = ia.write.out.size;
1207 /* break out of the loop on short write */
1208 if (num_written != count)
1213 } while (!err && iov_iter_count(ii));
1216 fuse_write_update_size(inode, pos);
1218 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1219 fuse_invalidate_attr(inode);
1221 return res > 0 ? res : err;
1224 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1226 struct file *file = iocb->ki_filp;
1227 struct address_space *mapping = file->f_mapping;
1228 ssize_t written = 0;
1229 ssize_t written_buffered = 0;
1230 struct inode *inode = mapping->host;
1234 if (get_fuse_conn(inode)->writeback_cache) {
1235 /* Update size (EOF optimization) and mode (SUID clearing) */
1236 err = fuse_update_attributes(mapping->host, file);
1240 return generic_file_write_iter(iocb, from);
1245 /* We can write back this queue in page reclaim */
1246 current->backing_dev_info = inode_to_bdi(inode);
1248 err = generic_write_checks(iocb, from);
1252 err = file_remove_privs(file);
1256 err = file_update_time(file);
1260 if (iocb->ki_flags & IOCB_DIRECT) {
1261 loff_t pos = iocb->ki_pos;
1262 written = generic_file_direct_write(iocb, from);
1263 if (written < 0 || !iov_iter_count(from))
1268 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1269 if (written_buffered < 0) {
1270 err = written_buffered;
1273 endbyte = pos + written_buffered - 1;
1275 err = filemap_write_and_wait_range(file->f_mapping, pos,
1280 invalidate_mapping_pages(file->f_mapping,
1282 endbyte >> PAGE_SHIFT);
1284 written += written_buffered;
1285 iocb->ki_pos = pos + written_buffered;
1287 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1289 iocb->ki_pos += written;
1292 current->backing_dev_info = NULL;
1293 inode_unlock(inode);
1295 written = generic_write_sync(iocb, written);
1297 return written ? written : err;
1300 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1302 unsigned int nr_pages)
1306 for (i = index; i < index + nr_pages; i++)
1307 descs[i].length = PAGE_SIZE - descs[i].offset;
1310 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1312 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1315 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1318 return min(iov_iter_single_seg_count(ii), max_size);
1321 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1322 size_t *nbytesp, int write,
1323 unsigned int max_pages)
1325 size_t nbytes = 0; /* # bytes already packed in req */
1328 /* Special case for kernel I/O: can copy directly into the buffer */
1329 if (iov_iter_is_kvec(ii)) {
1330 unsigned long user_addr = fuse_get_user_addr(ii);
1331 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1334 ap->args.in_args[1].value = (void *) user_addr;
1336 ap->args.out_args[0].value = (void *) user_addr;
1338 iov_iter_advance(ii, frag_size);
1339 *nbytesp = frag_size;
1343 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1346 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1348 max_pages - ap->num_pages,
1353 iov_iter_advance(ii, ret);
1357 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1359 ap->descs[ap->num_pages].offset = start;
1360 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1362 ap->num_pages += npages;
1363 ap->descs[ap->num_pages - 1].length -=
1364 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1368 ap->args.in_pages = true;
1370 ap->args.out_pages = true;
1374 return ret < 0 ? ret : 0;
1377 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1378 loff_t *ppos, int flags)
1380 int write = flags & FUSE_DIO_WRITE;
1381 int cuse = flags & FUSE_DIO_CUSE;
1382 struct file *file = io->iocb->ki_filp;
1383 struct inode *inode = file->f_mapping->host;
1384 struct fuse_file *ff = file->private_data;
1385 struct fuse_conn *fc = ff->fc;
1386 size_t nmax = write ? fc->max_write : fc->max_read;
1388 size_t count = iov_iter_count(iter);
1389 pgoff_t idx_from = pos >> PAGE_SHIFT;
1390 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1393 struct fuse_io_args *ia;
1394 unsigned int max_pages;
1396 max_pages = iov_iter_npages(iter, fc->max_pages);
1397 ia = fuse_io_alloc(io, max_pages);
1402 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1405 fuse_sync_writes(inode);
1407 inode_unlock(inode);
1410 io->should_dirty = !write && iter_is_iovec(iter);
1413 fl_owner_t owner = current->files;
1414 size_t nbytes = min(count, nmax);
1416 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1422 if (!capable(CAP_FSETID))
1423 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1425 nres = fuse_send_write(ia, pos, nbytes, owner);
1427 nres = fuse_send_read(ia, pos, nbytes, owner);
1430 if (!io->async || nres < 0) {
1431 fuse_release_user_pages(&ia->ap, io->should_dirty);
1436 iov_iter_revert(iter, nbytes);
1440 WARN_ON(nres > nbytes);
1445 if (nres != nbytes) {
1446 iov_iter_revert(iter, nbytes - nres);
1450 max_pages = iov_iter_npages(iter, fc->max_pages);
1451 ia = fuse_io_alloc(io, max_pages);
1461 return res > 0 ? res : err;
1463 EXPORT_SYMBOL_GPL(fuse_direct_io);
1465 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1466 struct iov_iter *iter,
1470 struct inode *inode = file_inode(io->iocb->ki_filp);
1472 res = fuse_direct_io(io, iter, ppos, 0);
1474 fuse_invalidate_atime(inode);
1479 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1481 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1485 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1486 res = fuse_direct_IO(iocb, to);
1488 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1490 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1496 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1498 struct inode *inode = file_inode(iocb->ki_filp);
1499 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1502 /* Don't allow parallel writes to the same file */
1504 res = generic_write_checks(iocb, from);
1506 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1507 res = fuse_direct_IO(iocb, from);
1509 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1513 fuse_invalidate_attr(inode);
1515 fuse_write_update_size(inode, iocb->ki_pos);
1516 inode_unlock(inode);
1521 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1523 struct file *file = iocb->ki_filp;
1524 struct fuse_file *ff = file->private_data;
1526 if (is_bad_inode(file_inode(file)))
1529 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1530 return fuse_cache_read_iter(iocb, to);
1532 return fuse_direct_read_iter(iocb, to);
1535 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1537 struct file *file = iocb->ki_filp;
1538 struct fuse_file *ff = file->private_data;
1540 if (is_bad_inode(file_inode(file)))
1543 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1544 return fuse_cache_write_iter(iocb, from);
1546 return fuse_direct_write_iter(iocb, from);
1549 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1551 struct fuse_args_pages *ap = &wpa->ia.ap;
1554 for (i = 0; i < ap->num_pages; i++)
1555 __free_page(ap->pages[i]);
1558 fuse_file_put(wpa->ia.ff, false, false);
1564 static void fuse_writepage_finish(struct fuse_conn *fc,
1565 struct fuse_writepage_args *wpa)
1567 struct fuse_args_pages *ap = &wpa->ia.ap;
1568 struct inode *inode = wpa->inode;
1569 struct fuse_inode *fi = get_fuse_inode(inode);
1570 struct backing_dev_info *bdi = inode_to_bdi(inode);
1573 list_del(&wpa->writepages_entry);
1574 for (i = 0; i < ap->num_pages; i++) {
1575 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1576 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1577 wb_writeout_inc(&bdi->wb);
1579 wake_up(&fi->page_waitq);
1582 /* Called under fi->lock, may release and reacquire it */
1583 static void fuse_send_writepage(struct fuse_conn *fc,
1584 struct fuse_writepage_args *wpa, loff_t size)
1585 __releases(fi->lock)
1586 __acquires(fi->lock)
1588 struct fuse_writepage_args *aux, *next;
1589 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1590 struct fuse_write_in *inarg = &wpa->ia.write.in;
1591 struct fuse_args *args = &wpa->ia.ap.args;
1592 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1596 if (inarg->offset + data_size <= size) {
1597 inarg->size = data_size;
1598 } else if (inarg->offset < size) {
1599 inarg->size = size - inarg->offset;
1601 /* Got truncated off completely */
1605 args->in_args[1].size = inarg->size;
1607 args->nocreds = true;
1609 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1610 if (err == -ENOMEM) {
1611 spin_unlock(&fi->lock);
1612 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1613 spin_lock(&fi->lock);
1616 /* Fails on broken connection only */
1624 fuse_writepage_finish(fc, wpa);
1625 spin_unlock(&fi->lock);
1627 /* After fuse_writepage_finish() aux request list is private */
1628 for (aux = wpa->next; aux; aux = next) {
1631 fuse_writepage_free(aux);
1634 fuse_writepage_free(wpa);
1635 spin_lock(&fi->lock);
1639 * If fi->writectr is positive (no truncate or fsync going on) send
1640 * all queued writepage requests.
1642 * Called with fi->lock
1644 void fuse_flush_writepages(struct inode *inode)
1645 __releases(fi->lock)
1646 __acquires(fi->lock)
1648 struct fuse_conn *fc = get_fuse_conn(inode);
1649 struct fuse_inode *fi = get_fuse_inode(inode);
1650 loff_t crop = i_size_read(inode);
1651 struct fuse_writepage_args *wpa;
1653 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1654 wpa = list_entry(fi->queued_writes.next,
1655 struct fuse_writepage_args, queue_entry);
1656 list_del_init(&wpa->queue_entry);
1657 fuse_send_writepage(fc, wpa, crop);
1661 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1664 struct fuse_writepage_args *wpa =
1665 container_of(args, typeof(*wpa), ia.ap.args);
1666 struct inode *inode = wpa->inode;
1667 struct fuse_inode *fi = get_fuse_inode(inode);
1669 mapping_set_error(inode->i_mapping, error);
1670 spin_lock(&fi->lock);
1672 struct fuse_conn *fc = get_fuse_conn(inode);
1673 struct fuse_write_in *inarg = &wpa->ia.write.in;
1674 struct fuse_writepage_args *next = wpa->next;
1676 wpa->next = next->next;
1678 next->ia.ff = fuse_file_get(wpa->ia.ff);
1679 list_add(&next->writepages_entry, &fi->writepages);
1682 * Skip fuse_flush_writepages() to make it easy to crop requests
1683 * based on primary request size.
1685 * 1st case (trivial): there are no concurrent activities using
1686 * fuse_set/release_nowrite. Then we're on safe side because
1687 * fuse_flush_writepages() would call fuse_send_writepage()
1690 * 2nd case: someone called fuse_set_nowrite and it is waiting
1691 * now for completion of all in-flight requests. This happens
1692 * rarely and no more than once per page, so this should be
1695 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1696 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1697 * that fuse_set_nowrite returned implies that all in-flight
1698 * requests were completed along with all of their secondary
1699 * requests. Further primary requests are blocked by negative
1700 * writectr. Hence there cannot be any in-flight requests and
1701 * no invocations of fuse_writepage_end() while we're in
1702 * fuse_set_nowrite..fuse_release_nowrite section.
1704 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1707 fuse_writepage_finish(fc, wpa);
1708 spin_unlock(&fi->lock);
1709 fuse_writepage_free(wpa);
1712 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1713 struct fuse_inode *fi)
1715 struct fuse_file *ff = NULL;
1717 spin_lock(&fi->lock);
1718 if (!list_empty(&fi->write_files)) {
1719 ff = list_entry(fi->write_files.next, struct fuse_file,
1723 spin_unlock(&fi->lock);
1728 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1729 struct fuse_inode *fi)
1731 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1736 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1738 struct fuse_conn *fc = get_fuse_conn(inode);
1739 struct fuse_inode *fi = get_fuse_inode(inode);
1740 struct fuse_file *ff;
1743 ff = __fuse_write_file_get(fc, fi);
1744 err = fuse_flush_times(inode, ff);
1746 fuse_file_put(ff, false, false);
1751 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1753 struct fuse_writepage_args *wpa;
1754 struct fuse_args_pages *ap;
1756 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1760 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1770 static int fuse_writepage_locked(struct page *page)
1772 struct address_space *mapping = page->mapping;
1773 struct inode *inode = mapping->host;
1774 struct fuse_conn *fc = get_fuse_conn(inode);
1775 struct fuse_inode *fi = get_fuse_inode(inode);
1776 struct fuse_writepage_args *wpa;
1777 struct fuse_args_pages *ap;
1778 struct page *tmp_page;
1779 int error = -ENOMEM;
1781 set_page_writeback(page);
1783 wpa = fuse_writepage_args_alloc();
1788 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1793 wpa->ia.ff = fuse_write_file_get(fc, fi);
1797 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1799 copy_highpage(tmp_page, page);
1800 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1802 ap->args.in_pages = true;
1804 ap->pages[0] = tmp_page;
1805 ap->descs[0].offset = 0;
1806 ap->descs[0].length = PAGE_SIZE;
1807 ap->args.end = fuse_writepage_end;
1810 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1811 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1813 spin_lock(&fi->lock);
1814 list_add(&wpa->writepages_entry, &fi->writepages);
1815 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1816 fuse_flush_writepages(inode);
1817 spin_unlock(&fi->lock);
1819 end_page_writeback(page);
1824 __free_page(tmp_page);
1828 mapping_set_error(page->mapping, error);
1829 end_page_writeback(page);
1833 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1837 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1839 * ->writepages() should be called for sync() and friends. We
1840 * should only get here on direct reclaim and then we are
1841 * allowed to skip a page which is already in flight
1843 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1845 redirty_page_for_writepage(wbc, page);
1850 err = fuse_writepage_locked(page);
1856 struct fuse_fill_wb_data {
1857 struct fuse_writepage_args *wpa;
1858 struct fuse_file *ff;
1859 struct inode *inode;
1860 struct page **orig_pages;
1861 unsigned int max_pages;
1864 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1866 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1867 struct fuse_conn *fc = get_fuse_conn(data->inode);
1868 struct page **pages;
1869 struct fuse_page_desc *descs;
1870 unsigned int npages = min_t(unsigned int,
1871 max_t(unsigned int, data->max_pages * 2,
1872 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1874 WARN_ON(npages <= data->max_pages);
1876 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1880 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1881 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1885 data->max_pages = npages;
1890 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1892 struct fuse_writepage_args *wpa = data->wpa;
1893 struct inode *inode = data->inode;
1894 struct fuse_inode *fi = get_fuse_inode(inode);
1895 int num_pages = wpa->ia.ap.num_pages;
1898 wpa->ia.ff = fuse_file_get(data->ff);
1899 spin_lock(&fi->lock);
1900 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1901 fuse_flush_writepages(inode);
1902 spin_unlock(&fi->lock);
1904 for (i = 0; i < num_pages; i++)
1905 end_page_writeback(data->orig_pages[i]);
1909 * First recheck under fi->lock if the offending offset is still under
1910 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1911 * one already added for a page at this offset. If there's none, then insert
1912 * this new request onto the auxiliary list, otherwise reuse the existing one by
1913 * copying the new page contents over to the old temporary page.
1915 static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
1918 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1919 struct fuse_writepage_args *tmp;
1920 struct fuse_writepage_args *old_wpa;
1921 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
1923 WARN_ON(new_ap->num_pages != 0);
1925 spin_lock(&fi->lock);
1926 list_del(&new_wpa->writepages_entry);
1927 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1929 list_add(&new_wpa->writepages_entry, &fi->writepages);
1930 spin_unlock(&fi->lock);
1934 new_ap->num_pages = 1;
1935 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
1938 WARN_ON(tmp->inode != new_wpa->inode);
1939 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
1940 if (curr_index == page->index) {
1941 WARN_ON(tmp->ia.ap.num_pages != 1);
1942 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
1948 new_wpa->next = old_wpa->next;
1949 old_wpa->next = new_wpa;
1952 spin_unlock(&fi->lock);
1955 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
1957 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1958 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
1959 wb_writeout_inc(&bdi->wb);
1960 fuse_writepage_free(new_wpa);
1966 static int fuse_writepages_fill(struct page *page,
1967 struct writeback_control *wbc, void *_data)
1969 struct fuse_fill_wb_data *data = _data;
1970 struct fuse_writepage_args *wpa = data->wpa;
1971 struct fuse_args_pages *ap = &wpa->ia.ap;
1972 struct inode *inode = data->inode;
1973 struct fuse_inode *fi = get_fuse_inode(inode);
1974 struct fuse_conn *fc = get_fuse_conn(inode);
1975 struct page *tmp_page;
1981 data->ff = fuse_write_file_get(fc, fi);
1987 * Being under writeback is unlikely but possible. For example direct
1988 * read to an mmaped fuse file will set the page dirty twice; once when
1989 * the pages are faulted with get_user_pages(), and then after the read
1992 is_writeback = fuse_page_is_writeback(inode, page->index);
1994 if (wpa && ap->num_pages &&
1995 (is_writeback || ap->num_pages == fc->max_pages ||
1996 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1997 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
1998 fuse_writepages_send(data);
2000 } else if (wpa && ap->num_pages == data->max_pages) {
2001 if (!fuse_pages_realloc(data)) {
2002 fuse_writepages_send(data);
2008 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2013 * The page must not be redirtied until the writeout is completed
2014 * (i.e. userspace has sent a reply to the write request). Otherwise
2015 * there could be more than one temporary page instance for each real
2018 * This is ensured by holding the page lock in page_mkwrite() while
2019 * checking fuse_page_is_writeback(). We already hold the page lock
2020 * since clear_page_dirty_for_io() and keep it held until we add the
2021 * request to the fi->writepages list and increment ap->num_pages.
2022 * After this fuse_page_is_writeback() will indicate that the page is
2023 * under writeback, so we can release the page lock.
2025 if (data->wpa == NULL) {
2027 wpa = fuse_writepage_args_alloc();
2029 __free_page(tmp_page);
2032 data->max_pages = 1;
2035 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2036 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2038 ap->args.in_pages = true;
2039 ap->args.end = fuse_writepage_end;
2043 spin_lock(&fi->lock);
2044 list_add(&wpa->writepages_entry, &fi->writepages);
2045 spin_unlock(&fi->lock);
2049 set_page_writeback(page);
2051 copy_highpage(tmp_page, page);
2052 ap->pages[ap->num_pages] = tmp_page;
2053 ap->descs[ap->num_pages].offset = 0;
2054 ap->descs[ap->num_pages].length = PAGE_SIZE;
2056 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2057 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2060 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
2061 end_page_writeback(page);
2065 data->orig_pages[ap->num_pages] = page;
2068 * Protected by fi->lock against concurrent access by
2069 * fuse_page_is_writeback().
2071 spin_lock(&fi->lock);
2073 spin_unlock(&fi->lock);
2081 static int fuse_writepages(struct address_space *mapping,
2082 struct writeback_control *wbc)
2084 struct inode *inode = mapping->host;
2085 struct fuse_conn *fc = get_fuse_conn(inode);
2086 struct fuse_fill_wb_data data;
2090 if (is_bad_inode(inode))
2098 data.orig_pages = kcalloc(fc->max_pages,
2099 sizeof(struct page *),
2101 if (!data.orig_pages)
2104 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2106 /* Ignore errors if we can write at least one page */
2107 WARN_ON(!data.wpa->ia.ap.num_pages);
2108 fuse_writepages_send(&data);
2112 fuse_file_put(data.ff, false, false);
2114 kfree(data.orig_pages);
2120 * It's worthy to make sure that space is reserved on disk for the write,
2121 * but how to implement it without killing performance need more thinking.
2123 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2124 loff_t pos, unsigned len, unsigned flags,
2125 struct page **pagep, void **fsdata)
2127 pgoff_t index = pos >> PAGE_SHIFT;
2128 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2133 WARN_ON(!fc->writeback_cache);
2135 page = grab_cache_page_write_begin(mapping, index, flags);
2139 fuse_wait_on_page_writeback(mapping->host, page->index);
2141 if (PageUptodate(page) || len == PAGE_SIZE)
2144 * Check if the start this page comes after the end of file, in which
2145 * case the readpage can be optimized away.
2147 fsize = i_size_read(mapping->host);
2148 if (fsize <= (pos & PAGE_MASK)) {
2149 size_t off = pos & ~PAGE_MASK;
2151 zero_user_segment(page, 0, off);
2154 err = fuse_do_readpage(file, page);
2168 static int fuse_write_end(struct file *file, struct address_space *mapping,
2169 loff_t pos, unsigned len, unsigned copied,
2170 struct page *page, void *fsdata)
2172 struct inode *inode = page->mapping->host;
2174 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2178 if (!PageUptodate(page)) {
2179 /* Zero any unwritten bytes at the end of the page */
2180 size_t endoff = (pos + copied) & ~PAGE_MASK;
2182 zero_user_segment(page, endoff, PAGE_SIZE);
2183 SetPageUptodate(page);
2186 fuse_write_update_size(inode, pos + copied);
2187 set_page_dirty(page);
2196 static int fuse_launder_page(struct page *page)
2199 if (clear_page_dirty_for_io(page)) {
2200 struct inode *inode = page->mapping->host;
2201 err = fuse_writepage_locked(page);
2203 fuse_wait_on_page_writeback(inode, page->index);
2209 * Write back dirty pages now, because there may not be any suitable
2212 static void fuse_vma_close(struct vm_area_struct *vma)
2214 filemap_write_and_wait(vma->vm_file->f_mapping);
2218 * Wait for writeback against this page to complete before allowing it
2219 * to be marked dirty again, and hence written back again, possibly
2220 * before the previous writepage completed.
2222 * Block here, instead of in ->writepage(), so that the userspace fs
2223 * can only block processes actually operating on the filesystem.
2225 * Otherwise unprivileged userspace fs would be able to block
2230 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2232 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2234 struct page *page = vmf->page;
2235 struct inode *inode = file_inode(vmf->vma->vm_file);
2237 file_update_time(vmf->vma->vm_file);
2239 if (page->mapping != inode->i_mapping) {
2241 return VM_FAULT_NOPAGE;
2244 fuse_wait_on_page_writeback(inode, page->index);
2245 return VM_FAULT_LOCKED;
2248 static const struct vm_operations_struct fuse_file_vm_ops = {
2249 .close = fuse_vma_close,
2250 .fault = filemap_fault,
2251 .map_pages = filemap_map_pages,
2252 .page_mkwrite = fuse_page_mkwrite,
2255 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2257 struct fuse_file *ff = file->private_data;
2259 if (ff->open_flags & FOPEN_DIRECT_IO) {
2260 /* Can't provide the coherency needed for MAP_SHARED */
2261 if (vma->vm_flags & VM_MAYSHARE)
2264 invalidate_inode_pages2(file->f_mapping);
2266 return generic_file_mmap(file, vma);
2269 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2270 fuse_link_write_file(file);
2272 file_accessed(file);
2273 vma->vm_ops = &fuse_file_vm_ops;
2277 static int convert_fuse_file_lock(struct fuse_conn *fc,
2278 const struct fuse_file_lock *ffl,
2279 struct file_lock *fl)
2281 switch (ffl->type) {
2287 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2288 ffl->end < ffl->start)
2291 fl->fl_start = ffl->start;
2292 fl->fl_end = ffl->end;
2295 * Convert pid into init's pid namespace. The locks API will
2296 * translate it into the caller's pid namespace.
2299 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2306 fl->fl_type = ffl->type;
2310 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2311 const struct file_lock *fl, int opcode, pid_t pid,
2312 int flock, struct fuse_lk_in *inarg)
2314 struct inode *inode = file_inode(file);
2315 struct fuse_conn *fc = get_fuse_conn(inode);
2316 struct fuse_file *ff = file->private_data;
2318 memset(inarg, 0, sizeof(*inarg));
2320 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2321 inarg->lk.start = fl->fl_start;
2322 inarg->lk.end = fl->fl_end;
2323 inarg->lk.type = fl->fl_type;
2324 inarg->lk.pid = pid;
2326 inarg->lk_flags |= FUSE_LK_FLOCK;
2327 args->opcode = opcode;
2328 args->nodeid = get_node_id(inode);
2329 args->in_numargs = 1;
2330 args->in_args[0].size = sizeof(*inarg);
2331 args->in_args[0].value = inarg;
2334 static int fuse_getlk(struct file *file, struct file_lock *fl)
2336 struct inode *inode = file_inode(file);
2337 struct fuse_conn *fc = get_fuse_conn(inode);
2339 struct fuse_lk_in inarg;
2340 struct fuse_lk_out outarg;
2343 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2344 args.out_numargs = 1;
2345 args.out_args[0].size = sizeof(outarg);
2346 args.out_args[0].value = &outarg;
2347 err = fuse_simple_request(fc, &args);
2349 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2354 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2356 struct inode *inode = file_inode(file);
2357 struct fuse_conn *fc = get_fuse_conn(inode);
2359 struct fuse_lk_in inarg;
2360 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2361 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2362 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2365 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2366 /* NLM needs asynchronous locks, which we don't support yet */
2370 /* Unlock on close is handled by the flush method */
2371 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2374 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2375 err = fuse_simple_request(fc, &args);
2377 /* locking is restartable */
2384 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2386 struct inode *inode = file_inode(file);
2387 struct fuse_conn *fc = get_fuse_conn(inode);
2390 if (cmd == F_CANCELLK) {
2392 } else if (cmd == F_GETLK) {
2394 posix_test_lock(file, fl);
2397 err = fuse_getlk(file, fl);
2400 err = posix_lock_file(file, fl, NULL);
2402 err = fuse_setlk(file, fl, 0);
2407 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2409 struct inode *inode = file_inode(file);
2410 struct fuse_conn *fc = get_fuse_conn(inode);
2414 err = locks_lock_file_wait(file, fl);
2416 struct fuse_file *ff = file->private_data;
2418 /* emulate flock with POSIX locks */
2420 err = fuse_setlk(file, fl, 1);
2426 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2428 struct inode *inode = mapping->host;
2429 struct fuse_conn *fc = get_fuse_conn(inode);
2431 struct fuse_bmap_in inarg;
2432 struct fuse_bmap_out outarg;
2435 if (!inode->i_sb->s_bdev || fc->no_bmap)
2438 memset(&inarg, 0, sizeof(inarg));
2439 inarg.block = block;
2440 inarg.blocksize = inode->i_sb->s_blocksize;
2441 args.opcode = FUSE_BMAP;
2442 args.nodeid = get_node_id(inode);
2443 args.in_numargs = 1;
2444 args.in_args[0].size = sizeof(inarg);
2445 args.in_args[0].value = &inarg;
2446 args.out_numargs = 1;
2447 args.out_args[0].size = sizeof(outarg);
2448 args.out_args[0].value = &outarg;
2449 err = fuse_simple_request(fc, &args);
2453 return err ? 0 : outarg.block;
2456 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2458 struct inode *inode = file->f_mapping->host;
2459 struct fuse_conn *fc = get_fuse_conn(inode);
2460 struct fuse_file *ff = file->private_data;
2462 struct fuse_lseek_in inarg = {
2467 struct fuse_lseek_out outarg;
2473 args.opcode = FUSE_LSEEK;
2474 args.nodeid = ff->nodeid;
2475 args.in_numargs = 1;
2476 args.in_args[0].size = sizeof(inarg);
2477 args.in_args[0].value = &inarg;
2478 args.out_numargs = 1;
2479 args.out_args[0].size = sizeof(outarg);
2480 args.out_args[0].value = &outarg;
2481 err = fuse_simple_request(fc, &args);
2483 if (err == -ENOSYS) {
2490 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2493 err = fuse_update_attributes(inode, file);
2495 return generic_file_llseek(file, offset, whence);
2500 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2503 struct inode *inode = file_inode(file);
2508 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2509 retval = generic_file_llseek(file, offset, whence);
2513 retval = fuse_update_attributes(inode, file);
2515 retval = generic_file_llseek(file, offset, whence);
2516 inode_unlock(inode);
2521 retval = fuse_lseek(file, offset, whence);
2522 inode_unlock(inode);
2532 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2533 * ABI was defined to be 'struct iovec' which is different on 32bit
2534 * and 64bit. Fortunately we can determine which structure the server
2535 * used from the size of the reply.
2537 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2538 size_t transferred, unsigned count,
2541 #ifdef CONFIG_COMPAT
2542 if (count * sizeof(struct compat_iovec) == transferred) {
2543 struct compat_iovec *ciov = src;
2547 * With this interface a 32bit server cannot support
2548 * non-compat (i.e. ones coming from 64bit apps) ioctl
2554 for (i = 0; i < count; i++) {
2555 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2556 dst[i].iov_len = ciov[i].iov_len;
2562 if (count * sizeof(struct iovec) != transferred)
2565 memcpy(dst, src, transferred);
2569 /* Make sure iov_length() won't overflow */
2570 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2574 u32 max = fc->max_pages << PAGE_SHIFT;
2576 for (n = 0; n < count; n++, iov++) {
2577 if (iov->iov_len > (size_t) max)
2579 max -= iov->iov_len;
2584 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2585 void *src, size_t transferred, unsigned count,
2589 struct fuse_ioctl_iovec *fiov = src;
2591 if (fc->minor < 16) {
2592 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2596 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2599 for (i = 0; i < count; i++) {
2600 /* Did the server supply an inappropriate value? */
2601 if (fiov[i].base != (unsigned long) fiov[i].base ||
2602 fiov[i].len != (unsigned long) fiov[i].len)
2605 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2606 dst[i].iov_len = (size_t) fiov[i].len;
2608 #ifdef CONFIG_COMPAT
2610 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2611 (compat_size_t) dst[i].iov_len != fiov[i].len))
2621 * For ioctls, there is no generic way to determine how much memory
2622 * needs to be read and/or written. Furthermore, ioctls are allowed
2623 * to dereference the passed pointer, so the parameter requires deep
2624 * copying but FUSE has no idea whatsoever about what to copy in or
2627 * This is solved by allowing FUSE server to retry ioctl with
2628 * necessary in/out iovecs. Let's assume the ioctl implementation
2629 * needs to read in the following structure.
2636 * On the first callout to FUSE server, inarg->in_size and
2637 * inarg->out_size will be NULL; then, the server completes the ioctl
2638 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2639 * the actual iov array to
2641 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2643 * which tells FUSE to copy in the requested area and retry the ioctl.
2644 * On the second round, the server has access to the structure and
2645 * from that it can tell what to look for next, so on the invocation,
2646 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2648 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2649 * { .iov_base = a.buf, .iov_len = a.buflen } }
2651 * FUSE will copy both struct a and the pointed buffer from the
2652 * process doing the ioctl and retry ioctl with both struct a and the
2655 * This time, FUSE server has everything it needs and completes ioctl
2656 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2658 * Copying data out works the same way.
2660 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2661 * automatically initializes in and out iovs by decoding @cmd with
2662 * _IOC_* macros and the server is not allowed to request RETRY. This
2663 * limits ioctl data transfers to well-formed ioctls and is the forced
2664 * behavior for all FUSE servers.
2666 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2669 struct fuse_file *ff = file->private_data;
2670 struct fuse_conn *fc = ff->fc;
2671 struct fuse_ioctl_in inarg = {
2677 struct fuse_ioctl_out outarg;
2678 struct iovec *iov_page = NULL;
2679 struct iovec *in_iov = NULL, *out_iov = NULL;
2680 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2681 size_t in_size, out_size, c;
2682 ssize_t transferred;
2685 struct fuse_args_pages ap = {};
2687 #if BITS_PER_LONG == 32
2688 inarg.flags |= FUSE_IOCTL_32BIT;
2690 if (flags & FUSE_IOCTL_COMPAT) {
2691 inarg.flags |= FUSE_IOCTL_32BIT;
2692 #ifdef CONFIG_X86_X32
2693 if (in_x32_syscall())
2694 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2699 /* assume all the iovs returned by client always fits in a page */
2700 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2703 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2704 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2705 if (!ap.pages || !iov_page)
2708 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2711 * If restricted, initialize IO parameters as encoded in @cmd.
2712 * RETRY from server is not allowed.
2714 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2715 struct iovec *iov = iov_page;
2717 iov->iov_base = (void __user *)arg;
2718 iov->iov_len = _IOC_SIZE(cmd);
2720 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2725 if (_IOC_DIR(cmd) & _IOC_READ) {
2732 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2733 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2736 * Out data can be used either for actual out data or iovs,
2737 * make sure there always is at least one page.
2739 out_size = max_t(size_t, out_size, PAGE_SIZE);
2740 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2742 /* make sure there are enough buffer pages and init request with them */
2744 if (max_pages > fc->max_pages)
2746 while (ap.num_pages < max_pages) {
2747 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2748 if (!ap.pages[ap.num_pages])
2754 /* okay, let's send it to the client */
2755 ap.args.opcode = FUSE_IOCTL;
2756 ap.args.nodeid = ff->nodeid;
2757 ap.args.in_numargs = 1;
2758 ap.args.in_args[0].size = sizeof(inarg);
2759 ap.args.in_args[0].value = &inarg;
2761 ap.args.in_numargs++;
2762 ap.args.in_args[1].size = in_size;
2763 ap.args.in_pages = true;
2766 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2767 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2768 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2769 if (c != PAGE_SIZE && iov_iter_count(&ii))
2774 ap.args.out_numargs = 2;
2775 ap.args.out_args[0].size = sizeof(outarg);
2776 ap.args.out_args[0].value = &outarg;
2777 ap.args.out_args[1].size = out_size;
2778 ap.args.out_pages = true;
2779 ap.args.out_argvar = true;
2781 transferred = fuse_simple_request(fc, &ap.args);
2783 if (transferred < 0)
2786 /* did it ask for retry? */
2787 if (outarg.flags & FUSE_IOCTL_RETRY) {
2790 /* no retry if in restricted mode */
2792 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2795 in_iovs = outarg.in_iovs;
2796 out_iovs = outarg.out_iovs;
2799 * Make sure things are in boundary, separate checks
2800 * are to protect against overflow.
2803 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2804 out_iovs > FUSE_IOCTL_MAX_IOV ||
2805 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2808 vaddr = kmap_atomic(ap.pages[0]);
2809 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2810 transferred, in_iovs + out_iovs,
2811 (flags & FUSE_IOCTL_COMPAT) != 0);
2812 kunmap_atomic(vaddr);
2817 out_iov = in_iov + in_iovs;
2819 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2823 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2831 if (transferred > inarg.out_size)
2835 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2836 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2837 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2838 if (c != PAGE_SIZE && iov_iter_count(&ii))
2843 free_page((unsigned long) iov_page);
2844 while (ap.num_pages)
2845 __free_page(ap.pages[--ap.num_pages]);
2848 return err ? err : outarg.result;
2850 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2852 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2853 unsigned long arg, unsigned int flags)
2855 struct inode *inode = file_inode(file);
2856 struct fuse_conn *fc = get_fuse_conn(inode);
2858 if (!fuse_allow_current_process(fc))
2861 if (is_bad_inode(inode))
2864 return fuse_do_ioctl(file, cmd, arg, flags);
2867 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2870 return fuse_ioctl_common(file, cmd, arg, 0);
2873 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2876 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2880 * All files which have been polled are linked to RB tree
2881 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2882 * find the matching one.
2884 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2885 struct rb_node **parent_out)
2887 struct rb_node **link = &fc->polled_files.rb_node;
2888 struct rb_node *last = NULL;
2891 struct fuse_file *ff;
2894 ff = rb_entry(last, struct fuse_file, polled_node);
2897 link = &last->rb_left;
2898 else if (kh > ff->kh)
2899 link = &last->rb_right;
2910 * The file is about to be polled. Make sure it's on the polled_files
2911 * RB tree. Note that files once added to the polled_files tree are
2912 * not removed before the file is released. This is because a file
2913 * polled once is likely to be polled again.
2915 static void fuse_register_polled_file(struct fuse_conn *fc,
2916 struct fuse_file *ff)
2918 spin_lock(&fc->lock);
2919 if (RB_EMPTY_NODE(&ff->polled_node)) {
2920 struct rb_node **link, *uninitialized_var(parent);
2922 link = fuse_find_polled_node(fc, ff->kh, &parent);
2924 rb_link_node(&ff->polled_node, parent, link);
2925 rb_insert_color(&ff->polled_node, &fc->polled_files);
2927 spin_unlock(&fc->lock);
2930 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2932 struct fuse_file *ff = file->private_data;
2933 struct fuse_conn *fc = ff->fc;
2934 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2935 struct fuse_poll_out outarg;
2940 return DEFAULT_POLLMASK;
2942 poll_wait(file, &ff->poll_wait, wait);
2943 inarg.events = mangle_poll(poll_requested_events(wait));
2946 * Ask for notification iff there's someone waiting for it.
2947 * The client may ignore the flag and always notify.
2949 if (waitqueue_active(&ff->poll_wait)) {
2950 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2951 fuse_register_polled_file(fc, ff);
2954 args.opcode = FUSE_POLL;
2955 args.nodeid = ff->nodeid;
2956 args.in_numargs = 1;
2957 args.in_args[0].size = sizeof(inarg);
2958 args.in_args[0].value = &inarg;
2959 args.out_numargs = 1;
2960 args.out_args[0].size = sizeof(outarg);
2961 args.out_args[0].value = &outarg;
2962 err = fuse_simple_request(fc, &args);
2965 return demangle_poll(outarg.revents);
2966 if (err == -ENOSYS) {
2968 return DEFAULT_POLLMASK;
2972 EXPORT_SYMBOL_GPL(fuse_file_poll);
2975 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2976 * wakes up the poll waiters.
2978 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2979 struct fuse_notify_poll_wakeup_out *outarg)
2981 u64 kh = outarg->kh;
2982 struct rb_node **link;
2984 spin_lock(&fc->lock);
2986 link = fuse_find_polled_node(fc, kh, NULL);
2988 struct fuse_file *ff;
2990 ff = rb_entry(*link, struct fuse_file, polled_node);
2991 wake_up_interruptible_sync(&ff->poll_wait);
2994 spin_unlock(&fc->lock);
2998 static void fuse_do_truncate(struct file *file)
3000 struct inode *inode = file->f_mapping->host;
3003 attr.ia_valid = ATTR_SIZE;
3004 attr.ia_size = i_size_read(inode);
3006 attr.ia_file = file;
3007 attr.ia_valid |= ATTR_FILE;
3009 fuse_do_setattr(file_dentry(file), &attr, file);
3012 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3014 return round_up(off, fc->max_pages << PAGE_SHIFT);
3018 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3020 DECLARE_COMPLETION_ONSTACK(wait);
3022 struct file *file = iocb->ki_filp;
3023 struct fuse_file *ff = file->private_data;
3024 bool async_dio = ff->fc->async_dio;
3026 struct inode *inode;
3028 size_t count = iov_iter_count(iter);
3029 loff_t offset = iocb->ki_pos;
3030 struct fuse_io_priv *io;
3033 inode = file->f_mapping->host;
3034 i_size = i_size_read(inode);
3036 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
3039 /* optimization for short read */
3040 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
3041 if (offset >= i_size)
3043 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3044 count = iov_iter_count(iter);
3047 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3050 spin_lock_init(&io->lock);
3051 kref_init(&io->refcnt);
3055 io->offset = offset;
3056 io->write = (iov_iter_rw(iter) == WRITE);
3059 * By default, we want to optimize all I/Os with async request
3060 * submission to the client filesystem if supported.
3062 io->async = async_dio;
3064 io->blocking = is_sync_kiocb(iocb);
3067 * We cannot asynchronously extend the size of a file.
3068 * In such case the aio will behave exactly like sync io.
3070 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3071 io->blocking = true;
3073 if (io->async && io->blocking) {
3075 * Additional reference to keep io around after
3076 * calling fuse_aio_complete()
3078 kref_get(&io->refcnt);
3082 if (iov_iter_rw(iter) == WRITE) {
3083 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3084 fuse_invalidate_attr(inode);
3086 ret = __fuse_direct_read(io, iter, &pos);
3090 bool blocking = io->blocking;
3092 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3094 /* we have a non-extending, async request, so return */
3096 return -EIOCBQUEUED;
3098 wait_for_completion(&wait);
3099 ret = fuse_get_res_by_io(io);
3102 kref_put(&io->refcnt, fuse_io_release);
3104 if (iov_iter_rw(iter) == WRITE) {
3106 fuse_write_update_size(inode, pos);
3107 else if (ret < 0 && offset + count > i_size)
3108 fuse_do_truncate(file);
3114 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3116 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3119 fuse_sync_writes(inode);
3124 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3127 struct fuse_file *ff = file->private_data;
3128 struct inode *inode = file_inode(file);
3129 struct fuse_inode *fi = get_fuse_inode(inode);
3130 struct fuse_conn *fc = ff->fc;
3132 struct fuse_fallocate_in inarg = {
3139 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3140 (mode & FALLOC_FL_PUNCH_HOLE);
3142 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3145 if (fc->no_fallocate)
3150 if (mode & FALLOC_FL_PUNCH_HOLE) {
3151 loff_t endbyte = offset + length - 1;
3153 err = fuse_writeback_range(inode, offset, endbyte);
3159 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3160 offset + length > i_size_read(inode)) {
3161 err = inode_newsize_ok(inode, offset + length);
3166 if (!(mode & FALLOC_FL_KEEP_SIZE))
3167 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3169 args.opcode = FUSE_FALLOCATE;
3170 args.nodeid = ff->nodeid;
3171 args.in_numargs = 1;
3172 args.in_args[0].size = sizeof(inarg);
3173 args.in_args[0].value = &inarg;
3174 err = fuse_simple_request(fc, &args);
3175 if (err == -ENOSYS) {
3176 fc->no_fallocate = 1;
3182 /* we could have extended the file */
3183 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3184 bool changed = fuse_write_update_size(inode, offset + length);
3186 if (changed && fc->writeback_cache)
3187 file_update_time(file);
3190 if (mode & FALLOC_FL_PUNCH_HOLE)
3191 truncate_pagecache_range(inode, offset, offset + length - 1);
3193 fuse_invalidate_attr(inode);
3196 if (!(mode & FALLOC_FL_KEEP_SIZE))
3197 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3200 inode_unlock(inode);
3205 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3206 struct file *file_out, loff_t pos_out,
3207 size_t len, unsigned int flags)
3209 struct fuse_file *ff_in = file_in->private_data;
3210 struct fuse_file *ff_out = file_out->private_data;
3211 struct inode *inode_in = file_inode(file_in);
3212 struct inode *inode_out = file_inode(file_out);
3213 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3214 struct fuse_conn *fc = ff_in->fc;
3216 struct fuse_copy_file_range_in inarg = {
3219 .nodeid_out = ff_out->nodeid,
3220 .fh_out = ff_out->fh,
3225 struct fuse_write_out outarg;
3227 /* mark unstable when write-back is not used, and file_out gets
3229 bool is_unstable = (!fc->writeback_cache) &&
3230 ((pos_out + len) > inode_out->i_size);
3232 if (fc->no_copy_file_range)
3235 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3238 if (fc->writeback_cache) {
3239 inode_lock(inode_in);
3240 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
3241 inode_unlock(inode_in);
3246 inode_lock(inode_out);
3248 err = file_modified(file_out);
3252 if (fc->writeback_cache) {
3253 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
3259 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3261 args.opcode = FUSE_COPY_FILE_RANGE;
3262 args.nodeid = ff_in->nodeid;
3263 args.in_numargs = 1;
3264 args.in_args[0].size = sizeof(inarg);
3265 args.in_args[0].value = &inarg;
3266 args.out_numargs = 1;
3267 args.out_args[0].size = sizeof(outarg);
3268 args.out_args[0].value = &outarg;
3269 err = fuse_simple_request(fc, &args);
3270 if (err == -ENOSYS) {
3271 fc->no_copy_file_range = 1;
3277 if (fc->writeback_cache) {
3278 fuse_write_update_size(inode_out, pos_out + outarg.size);
3279 file_update_time(file_out);
3282 fuse_invalidate_attr(inode_out);
3287 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3289 inode_unlock(inode_out);
3290 file_accessed(file_in);
3295 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3296 struct file *dst_file, loff_t dst_off,
3297 size_t len, unsigned int flags)
3301 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3304 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3305 ret = generic_copy_file_range(src_file, src_off, dst_file,
3306 dst_off, len, flags);
3310 static const struct file_operations fuse_file_operations = {
3311 .llseek = fuse_file_llseek,
3312 .read_iter = fuse_file_read_iter,
3313 .write_iter = fuse_file_write_iter,
3314 .mmap = fuse_file_mmap,
3316 .flush = fuse_flush,
3317 .release = fuse_release,
3318 .fsync = fuse_fsync,
3319 .lock = fuse_file_lock,
3320 .flock = fuse_file_flock,
3321 .splice_read = generic_file_splice_read,
3322 .splice_write = iter_file_splice_write,
3323 .unlocked_ioctl = fuse_file_ioctl,
3324 .compat_ioctl = fuse_file_compat_ioctl,
3325 .poll = fuse_file_poll,
3326 .fallocate = fuse_file_fallocate,
3327 .copy_file_range = fuse_copy_file_range,
3330 static const struct address_space_operations fuse_file_aops = {
3331 .readpage = fuse_readpage,
3332 .readahead = fuse_readahead,
3333 .writepage = fuse_writepage,
3334 .writepages = fuse_writepages,
3335 .launder_page = fuse_launder_page,
3336 .set_page_dirty = __set_page_dirty_nobuffers,
3338 .direct_IO = fuse_direct_IO,
3339 .write_begin = fuse_write_begin,
3340 .write_end = fuse_write_end,
3343 void fuse_init_file_inode(struct inode *inode)
3345 struct fuse_inode *fi = get_fuse_inode(inode);
3347 inode->i_fop = &fuse_file_operations;
3348 inode->i_data.a_ops = &fuse_file_aops;
3350 INIT_LIST_HEAD(&fi->write_files);
3351 INIT_LIST_HEAD(&fi->queued_writes);
3353 init_waitqueue_head(&fi->page_waitq);
3354 INIT_LIST_HEAD(&fi->writepages);