2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
23 static const struct file_operations fuse_direct_io_file_operations;
25 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
26 int opcode, struct fuse_open_out *outargp)
28 struct fuse_open_in inarg;
31 memset(&inarg, 0, sizeof(inarg));
32 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
33 if (!fc->atomic_o_trunc)
34 inarg.flags &= ~O_TRUNC;
35 args.in.h.opcode = opcode;
36 args.in.h.nodeid = nodeid;
38 args.in.args[0].size = sizeof(inarg);
39 args.in.args[0].value = &inarg;
41 args.out.args[0].size = sizeof(*outargp);
42 args.out.args[0].value = outargp;
44 return fuse_simple_request(fc, &args);
47 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
51 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
56 ff->reserved_req = fuse_request_alloc(0);
57 if (unlikely(!ff->reserved_req)) {
62 INIT_LIST_HEAD(&ff->write_entry);
63 refcount_set(&ff->count, 1);
64 RB_CLEAR_NODE(&ff->polled_node);
65 init_waitqueue_head(&ff->poll_wait);
69 spin_unlock(&fc->lock);
74 void fuse_file_free(struct fuse_file *ff)
76 fuse_request_free(ff->reserved_req);
80 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
82 refcount_inc(&ff->count);
86 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
88 iput(req->misc.release.inode);
91 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
93 if (refcount_dec_and_test(&ff->count)) {
94 struct fuse_req *req = ff->reserved_req;
96 if (ff->fc->no_open && !isdir) {
98 * Drop the release request when client does not
101 __clear_bit(FR_BACKGROUND, &req->flags);
102 iput(req->misc.release.inode);
103 fuse_put_request(ff->fc, req);
105 __set_bit(FR_FORCE, &req->flags);
106 __clear_bit(FR_BACKGROUND, &req->flags);
107 fuse_request_send(ff->fc, req);
108 iput(req->misc.release.inode);
109 fuse_put_request(ff->fc, req);
111 req->end = fuse_release_end;
112 __set_bit(FR_BACKGROUND, &req->flags);
113 fuse_request_send_background(ff->fc, req);
119 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
122 struct fuse_file *ff;
123 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
125 ff = fuse_file_alloc(fc);
130 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
131 if (!fc->no_open || isdir) {
132 struct fuse_open_out outarg;
135 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
138 ff->open_flags = outarg.open_flags;
140 } else if (err != -ENOSYS || isdir) {
149 ff->open_flags &= ~FOPEN_DIRECT_IO;
152 file->private_data = ff;
156 EXPORT_SYMBOL_GPL(fuse_do_open);
158 static void fuse_link_write_file(struct file *file)
160 struct inode *inode = file_inode(file);
161 struct fuse_conn *fc = get_fuse_conn(inode);
162 struct fuse_inode *fi = get_fuse_inode(inode);
163 struct fuse_file *ff = file->private_data;
165 * file may be written through mmap, so chain it onto the
166 * inodes's write_file list
168 spin_lock(&fc->lock);
169 if (list_empty(&ff->write_entry))
170 list_add(&ff->write_entry, &fi->write_files);
171 spin_unlock(&fc->lock);
174 void fuse_finish_open(struct inode *inode, struct file *file)
176 struct fuse_file *ff = file->private_data;
177 struct fuse_conn *fc = get_fuse_conn(inode);
179 if (ff->open_flags & FOPEN_DIRECT_IO)
180 file->f_op = &fuse_direct_io_file_operations;
181 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
182 invalidate_inode_pages2(inode->i_mapping);
183 if (ff->open_flags & FOPEN_STREAM)
184 stream_open(inode, file);
185 else if (ff->open_flags & FOPEN_NONSEEKABLE)
186 nonseekable_open(inode, file);
187 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
188 struct fuse_inode *fi = get_fuse_inode(inode);
190 spin_lock(&fc->lock);
191 fi->attr_version = ++fc->attr_version;
192 i_size_write(inode, 0);
193 spin_unlock(&fc->lock);
194 fuse_invalidate_attr(inode);
195 if (fc->writeback_cache)
196 file_update_time(file);
198 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
199 fuse_link_write_file(file);
202 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
204 struct fuse_conn *fc = get_fuse_conn(inode);
206 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
207 fc->atomic_o_trunc &&
210 err = generic_file_open(inode, file);
214 if (is_wb_truncate) {
216 fuse_set_nowrite(inode);
219 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
222 fuse_finish_open(inode, file);
224 if (is_wb_truncate) {
225 fuse_release_nowrite(inode);
232 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
234 struct fuse_conn *fc = ff->fc;
235 struct fuse_req *req = ff->reserved_req;
236 struct fuse_release_in *inarg = &req->misc.release.in;
238 spin_lock(&fc->lock);
239 list_del(&ff->write_entry);
240 if (!RB_EMPTY_NODE(&ff->polled_node))
241 rb_erase(&ff->polled_node, &fc->polled_files);
242 spin_unlock(&fc->lock);
244 wake_up_interruptible_all(&ff->poll_wait);
247 inarg->flags = flags;
248 req->in.h.opcode = opcode;
249 req->in.h.nodeid = ff->nodeid;
251 req->in.args[0].size = sizeof(struct fuse_release_in);
252 req->in.args[0].value = inarg;
255 void fuse_release_common(struct file *file, bool isdir)
257 struct fuse_file *ff = file->private_data;
258 struct fuse_req *req = ff->reserved_req;
259 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
261 fuse_prepare_release(ff, file->f_flags, opcode);
264 struct fuse_release_in *inarg = &req->misc.release.in;
265 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
266 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
269 /* Hold inode until release is finished */
270 req->misc.release.inode = igrab(file_inode(file));
273 * Normally this will send the RELEASE request, however if
274 * some asynchronous READ or WRITE requests are outstanding,
275 * the sending will be delayed.
277 * Make the release synchronous if this is a fuseblk mount,
278 * synchronous RELEASE is allowed (and desirable) in this case
279 * because the server can be trusted not to screw up.
281 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
284 static int fuse_open(struct inode *inode, struct file *file)
286 return fuse_open_common(inode, file, false);
289 static int fuse_release(struct inode *inode, struct file *file)
291 struct fuse_conn *fc = get_fuse_conn(inode);
293 /* see fuse_vma_close() for !writeback_cache case */
294 if (fc->writeback_cache)
295 write_inode_now(inode, 1);
297 fuse_release_common(file, false);
299 /* return value is ignored by VFS */
303 void fuse_sync_release(struct fuse_file *ff, int flags)
305 WARN_ON(refcount_read(&ff->count) > 1);
306 fuse_prepare_release(ff, flags, FUSE_RELEASE);
308 * iput(NULL) is a no-op and since the refcount is 1 and everything's
309 * synchronous, we are fine with not doing igrab() here"
311 fuse_file_put(ff, true, false);
313 EXPORT_SYMBOL_GPL(fuse_sync_release);
316 * Scramble the ID space with XTEA, so that the value of the files_struct
317 * pointer is not exposed to userspace.
319 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
321 u32 *k = fc->scramble_key;
322 u64 v = (unsigned long) id;
328 for (i = 0; i < 32; i++) {
329 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
331 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
334 return (u64) v0 + ((u64) v1 << 32);
338 * Check if any page in a range is under writeback
340 * This is currently done by walking the list of writepage requests
341 * for the inode, which can be pretty inefficient.
343 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
346 struct fuse_conn *fc = get_fuse_conn(inode);
347 struct fuse_inode *fi = get_fuse_inode(inode);
348 struct fuse_req *req;
351 spin_lock(&fc->lock);
352 list_for_each_entry(req, &fi->writepages, writepages_entry) {
355 BUG_ON(req->inode != inode);
356 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
357 if (idx_from < curr_index + req->num_pages &&
358 curr_index <= idx_to) {
363 spin_unlock(&fc->lock);
368 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
370 return fuse_range_is_writeback(inode, index, index);
374 * Wait for page writeback to be completed.
376 * Since fuse doesn't rely on the VM writeback tracking, this has to
377 * use some other means.
379 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
381 struct fuse_inode *fi = get_fuse_inode(inode);
383 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
388 * Wait for all pending writepages on the inode to finish.
390 * This is currently done by blocking further writes with FUSE_NOWRITE
391 * and waiting for all sent writes to complete.
393 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
394 * could conflict with truncation.
396 static void fuse_sync_writes(struct inode *inode)
398 fuse_set_nowrite(inode);
399 fuse_release_nowrite(inode);
402 static int fuse_flush(struct file *file, fl_owner_t id)
404 struct inode *inode = file_inode(file);
405 struct fuse_conn *fc = get_fuse_conn(inode);
406 struct fuse_file *ff = file->private_data;
407 struct fuse_req *req;
408 struct fuse_flush_in inarg;
411 if (is_bad_inode(inode))
417 err = write_inode_now(inode, 1);
422 fuse_sync_writes(inode);
425 err = filemap_check_errors(file->f_mapping);
429 req = fuse_get_req_nofail_nopages(fc, file);
430 memset(&inarg, 0, sizeof(inarg));
432 inarg.lock_owner = fuse_lock_owner_id(fc, id);
433 req->in.h.opcode = FUSE_FLUSH;
434 req->in.h.nodeid = get_node_id(inode);
436 req->in.args[0].size = sizeof(inarg);
437 req->in.args[0].value = &inarg;
438 __set_bit(FR_FORCE, &req->flags);
439 fuse_request_send(fc, req);
440 err = req->out.h.error;
441 fuse_put_request(fc, req);
442 if (err == -ENOSYS) {
449 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
450 int datasync, int isdir)
452 struct inode *inode = file->f_mapping->host;
453 struct fuse_conn *fc = get_fuse_conn(inode);
454 struct fuse_file *ff = file->private_data;
456 struct fuse_fsync_in inarg;
459 if (is_bad_inode(inode))
465 * Start writeback against all dirty pages of the inode, then
466 * wait for all outstanding writes, before sending the FSYNC
469 err = file_write_and_wait_range(file, start, end);
473 fuse_sync_writes(inode);
476 * Due to implementation of fuse writeback
477 * file_write_and_wait_range() does not catch errors.
478 * We have to do this directly after fuse_sync_writes()
480 err = file_check_and_advance_wb_err(file);
484 err = sync_inode_metadata(inode, 1);
488 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
491 memset(&inarg, 0, sizeof(inarg));
493 inarg.fsync_flags = datasync ? 1 : 0;
494 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
495 args.in.h.nodeid = get_node_id(inode);
497 args.in.args[0].size = sizeof(inarg);
498 args.in.args[0].value = &inarg;
499 err = fuse_simple_request(fc, &args);
500 if (err == -ENOSYS) {
512 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
515 return fuse_fsync_common(file, start, end, datasync, 0);
518 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
519 size_t count, int opcode)
521 struct fuse_read_in *inarg = &req->misc.read.in;
522 struct fuse_file *ff = file->private_data;
527 inarg->flags = file->f_flags;
528 req->in.h.opcode = opcode;
529 req->in.h.nodeid = ff->nodeid;
531 req->in.args[0].size = sizeof(struct fuse_read_in);
532 req->in.args[0].value = inarg;
534 req->out.numargs = 1;
535 req->out.args[0].size = count;
538 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
542 for (i = 0; i < req->num_pages; i++) {
543 struct page *page = req->pages[i];
545 set_page_dirty_lock(page);
550 static void fuse_io_release(struct kref *kref)
552 kfree(container_of(kref, struct fuse_io_priv, refcnt));
555 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
560 if (io->bytes >= 0 && io->write)
563 return io->bytes < 0 ? io->size : io->bytes;
567 * In case of short read, the caller sets 'pos' to the position of
568 * actual end of fuse request in IO request. Otherwise, if bytes_requested
569 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
572 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
573 * both submitted asynchronously. The first of them was ACKed by userspace as
574 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
575 * second request was ACKed as short, e.g. only 1K was read, resulting in
578 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
579 * will be equal to the length of the longest contiguous fragment of
580 * transferred data starting from the beginning of IO request.
582 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
586 spin_lock(&io->lock);
588 io->err = io->err ? : err;
589 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
593 if (!left && io->blocking)
595 spin_unlock(&io->lock);
597 if (!left && !io->blocking) {
598 ssize_t res = fuse_get_res_by_io(io);
601 struct inode *inode = file_inode(io->iocb->ki_filp);
602 struct fuse_conn *fc = get_fuse_conn(inode);
603 struct fuse_inode *fi = get_fuse_inode(inode);
605 spin_lock(&fc->lock);
606 fi->attr_version = ++fc->attr_version;
607 spin_unlock(&fc->lock);
610 io->iocb->ki_complete(io->iocb, res, 0);
613 kref_put(&io->refcnt, fuse_io_release);
616 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
618 struct fuse_io_priv *io = req->io;
621 fuse_release_user_pages(req, io->should_dirty);
624 if (req->misc.write.in.size != req->misc.write.out.size)
625 pos = req->misc.write.in.offset - io->offset +
626 req->misc.write.out.size;
628 if (req->misc.read.in.size != req->out.args[0].size)
629 pos = req->misc.read.in.offset - io->offset +
630 req->out.args[0].size;
633 fuse_aio_complete(io, req->out.h.error, pos);
636 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
637 size_t num_bytes, struct fuse_io_priv *io)
639 spin_lock(&io->lock);
640 kref_get(&io->refcnt);
641 io->size += num_bytes;
643 spin_unlock(&io->lock);
646 req->end = fuse_aio_complete_req;
648 __fuse_get_request(req);
649 fuse_request_send_background(fc, req);
654 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
655 loff_t pos, size_t count, fl_owner_t owner)
657 struct file *file = io->iocb->ki_filp;
658 struct fuse_file *ff = file->private_data;
659 struct fuse_conn *fc = ff->fc;
661 fuse_read_fill(req, file, pos, count, FUSE_READ);
663 struct fuse_read_in *inarg = &req->misc.read.in;
665 inarg->read_flags |= FUSE_READ_LOCKOWNER;
666 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
670 return fuse_async_req_send(fc, req, count, io);
672 fuse_request_send(fc, req);
673 return req->out.args[0].size;
676 static void fuse_read_update_size(struct inode *inode, loff_t size,
679 struct fuse_conn *fc = get_fuse_conn(inode);
680 struct fuse_inode *fi = get_fuse_inode(inode);
682 spin_lock(&fc->lock);
683 if (attr_ver == fi->attr_version && size < inode->i_size &&
684 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
685 fi->attr_version = ++fc->attr_version;
686 i_size_write(inode, size);
688 spin_unlock(&fc->lock);
691 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
694 size_t num_read = req->out.args[0].size;
695 struct fuse_conn *fc = get_fuse_conn(inode);
697 if (fc->writeback_cache) {
699 * A hole in a file. Some data after the hole are in page cache,
700 * but have not reached the client fs yet. So, the hole is not
704 int start_idx = num_read >> PAGE_SHIFT;
705 size_t off = num_read & (PAGE_SIZE - 1);
707 for (i = start_idx; i < req->num_pages; i++) {
708 zero_user_segment(req->pages[i], off, PAGE_SIZE);
712 loff_t pos = page_offset(req->pages[0]) + num_read;
713 fuse_read_update_size(inode, pos, attr_ver);
717 static int fuse_do_readpage(struct file *file, struct page *page)
720 struct fuse_io_priv io;
721 struct inode *inode = page->mapping->host;
722 struct fuse_conn *fc = get_fuse_conn(inode);
723 struct fuse_req *req;
725 loff_t pos = page_offset(page);
726 size_t count = PAGE_SIZE;
731 * Page writeback can extend beyond the lifetime of the
732 * page-cache page, so make sure we read a properly synced
735 fuse_wait_on_page_writeback(inode, page->index);
737 req = fuse_get_req(fc, 1);
741 attr_ver = fuse_get_attr_version(fc);
743 req->out.page_zeroing = 1;
744 req->out.argpages = 1;
746 req->pages[0] = page;
747 req->page_descs[0].length = count;
748 init_sync_kiocb(&iocb, file);
749 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
750 num_read = fuse_send_read(req, &io, pos, count, NULL);
751 err = req->out.h.error;
755 * Short read means EOF. If file size is larger, truncate it
757 if (num_read < count)
758 fuse_short_read(req, inode, attr_ver);
760 SetPageUptodate(page);
763 fuse_put_request(fc, req);
768 static int fuse_readpage(struct file *file, struct page *page)
770 struct inode *inode = page->mapping->host;
774 if (is_bad_inode(inode))
777 err = fuse_do_readpage(file, page);
778 fuse_invalidate_atime(inode);
784 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
787 size_t count = req->misc.read.in.size;
788 size_t num_read = req->out.args[0].size;
789 struct address_space *mapping = NULL;
791 for (i = 0; mapping == NULL && i < req->num_pages; i++)
792 mapping = req->pages[i]->mapping;
795 struct inode *inode = mapping->host;
798 * Short read means EOF. If file size is larger, truncate it
800 if (!req->out.h.error && num_read < count)
801 fuse_short_read(req, inode, req->misc.read.attr_ver);
803 fuse_invalidate_atime(inode);
806 for (i = 0; i < req->num_pages; i++) {
807 struct page *page = req->pages[i];
808 if (!req->out.h.error)
809 SetPageUptodate(page);
816 fuse_file_put(req->ff, false, false);
819 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
821 struct fuse_file *ff = file->private_data;
822 struct fuse_conn *fc = ff->fc;
823 loff_t pos = page_offset(req->pages[0]);
824 size_t count = req->num_pages << PAGE_SHIFT;
826 req->out.argpages = 1;
827 req->out.page_zeroing = 1;
828 req->out.page_replace = 1;
829 fuse_read_fill(req, file, pos, count, FUSE_READ);
830 req->misc.read.attr_ver = fuse_get_attr_version(fc);
831 if (fc->async_read) {
832 req->ff = fuse_file_get(ff);
833 req->end = fuse_readpages_end;
834 fuse_request_send_background(fc, req);
836 fuse_request_send(fc, req);
837 fuse_readpages_end(fc, req);
838 fuse_put_request(fc, req);
842 struct fuse_fill_data {
843 struct fuse_req *req;
849 static int fuse_readpages_fill(void *_data, struct page *page)
851 struct fuse_fill_data *data = _data;
852 struct fuse_req *req = data->req;
853 struct inode *inode = data->inode;
854 struct fuse_conn *fc = get_fuse_conn(inode);
856 fuse_wait_on_page_writeback(inode, page->index);
858 if (req->num_pages &&
859 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
860 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
861 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
862 int nr_alloc = min_t(unsigned, data->nr_pages,
863 FUSE_MAX_PAGES_PER_REQ);
864 fuse_send_readpages(req, data->file);
866 req = fuse_get_req_for_background(fc, nr_alloc);
868 req = fuse_get_req(fc, nr_alloc);
877 if (WARN_ON(req->num_pages >= req->max_pages)) {
879 fuse_put_request(fc, req);
884 req->pages[req->num_pages] = page;
885 req->page_descs[req->num_pages].length = PAGE_SIZE;
891 static int fuse_readpages(struct file *file, struct address_space *mapping,
892 struct list_head *pages, unsigned nr_pages)
894 struct inode *inode = mapping->host;
895 struct fuse_conn *fc = get_fuse_conn(inode);
896 struct fuse_fill_data data;
898 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
901 if (is_bad_inode(inode))
907 data.req = fuse_get_req_for_background(fc, nr_alloc);
909 data.req = fuse_get_req(fc, nr_alloc);
910 data.nr_pages = nr_pages;
911 err = PTR_ERR(data.req);
912 if (IS_ERR(data.req))
915 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
917 if (data.req->num_pages)
918 fuse_send_readpages(data.req, file);
920 fuse_put_request(fc, data.req);
926 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
928 struct inode *inode = iocb->ki_filp->f_mapping->host;
929 struct fuse_conn *fc = get_fuse_conn(inode);
932 * In auto invalidate mode, always update attributes on read.
933 * Otherwise, only update if we attempt to read past EOF (to ensure
934 * i_size is up to date).
936 if (fc->auto_inval_data ||
937 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
939 err = fuse_update_attributes(inode, iocb->ki_filp);
944 return generic_file_read_iter(iocb, to);
947 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
948 loff_t pos, size_t count)
950 struct fuse_write_in *inarg = &req->misc.write.in;
951 struct fuse_write_out *outarg = &req->misc.write.out;
956 req->in.h.opcode = FUSE_WRITE;
957 req->in.h.nodeid = ff->nodeid;
959 if (ff->fc->minor < 9)
960 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
962 req->in.args[0].size = sizeof(struct fuse_write_in);
963 req->in.args[0].value = inarg;
964 req->in.args[1].size = count;
965 req->out.numargs = 1;
966 req->out.args[0].size = sizeof(struct fuse_write_out);
967 req->out.args[0].value = outarg;
970 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
971 loff_t pos, size_t count, fl_owner_t owner)
973 struct kiocb *iocb = io->iocb;
974 struct file *file = iocb->ki_filp;
975 struct fuse_file *ff = file->private_data;
976 struct fuse_conn *fc = ff->fc;
977 struct fuse_write_in *inarg = &req->misc.write.in;
979 fuse_write_fill(req, ff, pos, count);
980 inarg->flags = file->f_flags;
981 if (iocb->ki_flags & IOCB_DSYNC)
982 inarg->flags |= O_DSYNC;
983 if (iocb->ki_flags & IOCB_SYNC)
984 inarg->flags |= O_SYNC;
986 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
987 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
991 return fuse_async_req_send(fc, req, count, io);
993 fuse_request_send(fc, req);
994 return req->misc.write.out.size;
997 bool fuse_write_update_size(struct inode *inode, loff_t pos)
999 struct fuse_conn *fc = get_fuse_conn(inode);
1000 struct fuse_inode *fi = get_fuse_inode(inode);
1003 spin_lock(&fc->lock);
1004 fi->attr_version = ++fc->attr_version;
1005 if (pos > inode->i_size) {
1006 i_size_write(inode, pos);
1009 spin_unlock(&fc->lock);
1014 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1015 struct inode *inode, loff_t pos,
1021 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1023 for (i = 0; i < req->num_pages; i++)
1024 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1026 res = fuse_send_write(req, &io, pos, count, NULL);
1028 offset = req->page_descs[0].offset;
1030 for (i = 0; i < req->num_pages; i++) {
1031 struct page *page = req->pages[i];
1033 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1034 SetPageUptodate(page);
1036 if (count > PAGE_SIZE - offset)
1037 count -= PAGE_SIZE - offset;
1049 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1050 struct address_space *mapping,
1051 struct iov_iter *ii, loff_t pos)
1053 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1054 unsigned offset = pos & (PAGE_SIZE - 1);
1058 req->in.argpages = 1;
1059 req->page_descs[0].offset = offset;
1064 pgoff_t index = pos >> PAGE_SHIFT;
1065 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1066 iov_iter_count(ii));
1068 bytes = min_t(size_t, bytes, fc->max_write - count);
1072 if (iov_iter_fault_in_readable(ii, bytes))
1076 page = grab_cache_page_write_begin(mapping, index, 0);
1080 if (mapping_writably_mapped(mapping))
1081 flush_dcache_page(page);
1083 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1084 flush_dcache_page(page);
1086 iov_iter_advance(ii, tmp);
1090 bytes = min(bytes, iov_iter_single_seg_count(ii));
1095 req->pages[req->num_pages] = page;
1096 req->page_descs[req->num_pages].length = tmp;
1102 if (offset == PAGE_SIZE)
1105 if (!fc->big_writes)
1107 } while (iov_iter_count(ii) && count < fc->max_write &&
1108 req->num_pages < req->max_pages && offset == 0);
1110 return count > 0 ? count : err;
1113 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1115 return min_t(unsigned,
1116 ((pos + len - 1) >> PAGE_SHIFT) -
1117 (pos >> PAGE_SHIFT) + 1,
1118 FUSE_MAX_PAGES_PER_REQ);
1121 static ssize_t fuse_perform_write(struct kiocb *iocb,
1122 struct address_space *mapping,
1123 struct iov_iter *ii, loff_t pos)
1125 struct inode *inode = mapping->host;
1126 struct fuse_conn *fc = get_fuse_conn(inode);
1127 struct fuse_inode *fi = get_fuse_inode(inode);
1131 if (is_bad_inode(inode))
1134 if (inode->i_size < pos + iov_iter_count(ii))
1135 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1138 struct fuse_req *req;
1140 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1142 req = fuse_get_req(fc, nr_pages);
1148 count = fuse_fill_write_pages(req, mapping, ii, pos);
1154 num_written = fuse_send_write_pages(req, iocb, inode,
1156 err = req->out.h.error;
1161 /* break out of the loop on short write */
1162 if (num_written != count)
1166 fuse_put_request(fc, req);
1167 } while (!err && iov_iter_count(ii));
1170 fuse_write_update_size(inode, pos);
1172 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1173 fuse_invalidate_attr(inode);
1175 return res > 0 ? res : err;
1178 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1180 struct file *file = iocb->ki_filp;
1181 struct address_space *mapping = file->f_mapping;
1182 ssize_t written = 0;
1183 ssize_t written_buffered = 0;
1184 struct inode *inode = mapping->host;
1188 if (get_fuse_conn(inode)->writeback_cache) {
1189 /* Update size (EOF optimization) and mode (SUID clearing) */
1190 err = fuse_update_attributes(mapping->host, file);
1194 return generic_file_write_iter(iocb, from);
1199 /* We can write back this queue in page reclaim */
1200 current->backing_dev_info = inode_to_bdi(inode);
1202 err = generic_write_checks(iocb, from);
1206 err = file_remove_privs(file);
1210 err = file_update_time(file);
1214 if (iocb->ki_flags & IOCB_DIRECT) {
1215 loff_t pos = iocb->ki_pos;
1216 written = generic_file_direct_write(iocb, from);
1217 if (written < 0 || !iov_iter_count(from))
1222 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1223 if (written_buffered < 0) {
1224 err = written_buffered;
1227 endbyte = pos + written_buffered - 1;
1229 err = filemap_write_and_wait_range(file->f_mapping, pos,
1234 invalidate_mapping_pages(file->f_mapping,
1236 endbyte >> PAGE_SHIFT);
1238 written += written_buffered;
1239 iocb->ki_pos = pos + written_buffered;
1241 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1243 iocb->ki_pos += written;
1246 current->backing_dev_info = NULL;
1247 inode_unlock(inode);
1249 written = generic_write_sync(iocb, written);
1251 return written ? written : err;
1254 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1255 unsigned index, unsigned nr_pages)
1259 for (i = index; i < index + nr_pages; i++)
1260 req->page_descs[i].length = PAGE_SIZE -
1261 req->page_descs[i].offset;
1264 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1266 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1269 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1272 return min(iov_iter_single_seg_count(ii), max_size);
1275 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1276 size_t *nbytesp, int write)
1278 size_t nbytes = 0; /* # bytes already packed in req */
1281 /* Special case for kernel I/O: can copy directly into the buffer */
1282 if (ii->type & ITER_KVEC) {
1283 unsigned long user_addr = fuse_get_user_addr(ii);
1284 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1287 req->in.args[1].value = (void *) user_addr;
1289 req->out.args[0].value = (void *) user_addr;
1291 iov_iter_advance(ii, frag_size);
1292 *nbytesp = frag_size;
1296 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1299 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1301 req->max_pages - req->num_pages,
1306 iov_iter_advance(ii, ret);
1310 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1312 req->page_descs[req->num_pages].offset = start;
1313 fuse_page_descs_length_init(req, req->num_pages, npages);
1315 req->num_pages += npages;
1316 req->page_descs[req->num_pages - 1].length -=
1317 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1321 req->in.argpages = 1;
1323 req->out.argpages = 1;
1327 return ret < 0 ? ret : 0;
1330 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1332 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1335 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1336 loff_t *ppos, int flags)
1338 int write = flags & FUSE_DIO_WRITE;
1339 int cuse = flags & FUSE_DIO_CUSE;
1340 struct file *file = io->iocb->ki_filp;
1341 struct inode *inode = file->f_mapping->host;
1342 struct fuse_file *ff = file->private_data;
1343 struct fuse_conn *fc = ff->fc;
1344 size_t nmax = write ? fc->max_write : fc->max_read;
1346 size_t count = iov_iter_count(iter);
1347 pgoff_t idx_from = pos >> PAGE_SHIFT;
1348 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1350 struct fuse_req *req;
1354 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1356 req = fuse_get_req(fc, fuse_iter_npages(iter));
1358 return PTR_ERR(req);
1360 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1363 fuse_sync_writes(inode);
1365 inode_unlock(inode);
1368 io->should_dirty = !write && iter_is_iovec(iter);
1371 fl_owner_t owner = current->files;
1372 size_t nbytes = min(count, nmax);
1373 err = fuse_get_user_pages(req, iter, &nbytes, write);
1378 nres = fuse_send_write(req, io, pos, nbytes, owner);
1380 nres = fuse_send_read(req, io, pos, nbytes, owner);
1383 fuse_release_user_pages(req, io->should_dirty);
1384 if (req->out.h.error) {
1385 err = req->out.h.error;
1387 } else if (nres > nbytes) {
1398 fuse_put_request(fc, req);
1400 req = fuse_get_req_for_background(fc,
1401 fuse_iter_npages(iter));
1403 req = fuse_get_req(fc, fuse_iter_npages(iter));
1409 fuse_put_request(fc, req);
1413 return res > 0 ? res : err;
1415 EXPORT_SYMBOL_GPL(fuse_direct_io);
1417 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1418 struct iov_iter *iter,
1422 struct inode *inode = file_inode(io->iocb->ki_filp);
1424 if (is_bad_inode(inode))
1427 res = fuse_direct_io(io, iter, ppos, 0);
1429 fuse_invalidate_attr(inode);
1434 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1436 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1437 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1440 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1442 struct inode *inode = file_inode(iocb->ki_filp);
1443 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1446 if (is_bad_inode(inode))
1449 /* Don't allow parallel writes to the same file */
1451 res = generic_write_checks(iocb, from);
1453 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1454 fuse_invalidate_attr(inode);
1456 fuse_write_update_size(inode, iocb->ki_pos);
1457 inode_unlock(inode);
1462 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1466 for (i = 0; i < req->num_pages; i++)
1467 __free_page(req->pages[i]);
1470 fuse_file_put(req->ff, false, false);
1473 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1475 struct inode *inode = req->inode;
1476 struct fuse_inode *fi = get_fuse_inode(inode);
1477 struct backing_dev_info *bdi = inode_to_bdi(inode);
1480 list_del(&req->writepages_entry);
1481 for (i = 0; i < req->num_pages; i++) {
1482 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1483 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1484 wb_writeout_inc(&bdi->wb);
1486 wake_up(&fi->page_waitq);
1489 /* Called under fc->lock, may release and reacquire it */
1490 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1492 __releases(fc->lock)
1493 __acquires(fc->lock)
1495 struct fuse_inode *fi = get_fuse_inode(req->inode);
1496 struct fuse_write_in *inarg = &req->misc.write.in;
1497 __u64 data_size = req->num_pages * PAGE_SIZE;
1502 if (inarg->offset + data_size <= size) {
1503 inarg->size = data_size;
1504 } else if (inarg->offset < size) {
1505 inarg->size = size - inarg->offset;
1507 /* Got truncated off completely */
1511 req->in.args[1].size = inarg->size;
1513 fuse_request_send_background_locked(fc, req);
1517 fuse_writepage_finish(fc, req);
1518 spin_unlock(&fc->lock);
1519 fuse_writepage_free(fc, req);
1520 fuse_put_request(fc, req);
1521 spin_lock(&fc->lock);
1525 * If fi->writectr is positive (no truncate or fsync going on) send
1526 * all queued writepage requests.
1528 * Called with fc->lock
1530 void fuse_flush_writepages(struct inode *inode)
1531 __releases(fc->lock)
1532 __acquires(fc->lock)
1534 struct fuse_conn *fc = get_fuse_conn(inode);
1535 struct fuse_inode *fi = get_fuse_inode(inode);
1536 loff_t crop = i_size_read(inode);
1537 struct fuse_req *req;
1539 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1540 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1541 list_del_init(&req->list);
1542 fuse_send_writepage(fc, req, crop);
1546 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1548 struct inode *inode = req->inode;
1549 struct fuse_inode *fi = get_fuse_inode(inode);
1551 mapping_set_error(inode->i_mapping, req->out.h.error);
1552 spin_lock(&fc->lock);
1553 while (req->misc.write.next) {
1554 struct fuse_conn *fc = get_fuse_conn(inode);
1555 struct fuse_write_in *inarg = &req->misc.write.in;
1556 struct fuse_req *next = req->misc.write.next;
1557 req->misc.write.next = next->misc.write.next;
1558 next->misc.write.next = NULL;
1559 next->ff = fuse_file_get(req->ff);
1560 list_add(&next->writepages_entry, &fi->writepages);
1563 * Skip fuse_flush_writepages() to make it easy to crop requests
1564 * based on primary request size.
1566 * 1st case (trivial): there are no concurrent activities using
1567 * fuse_set/release_nowrite. Then we're on safe side because
1568 * fuse_flush_writepages() would call fuse_send_writepage()
1571 * 2nd case: someone called fuse_set_nowrite and it is waiting
1572 * now for completion of all in-flight requests. This happens
1573 * rarely and no more than once per page, so this should be
1576 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1577 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1578 * that fuse_set_nowrite returned implies that all in-flight
1579 * requests were completed along with all of their secondary
1580 * requests. Further primary requests are blocked by negative
1581 * writectr. Hence there cannot be any in-flight requests and
1582 * no invocations of fuse_writepage_end() while we're in
1583 * fuse_set_nowrite..fuse_release_nowrite section.
1585 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1588 fuse_writepage_finish(fc, req);
1589 spin_unlock(&fc->lock);
1590 fuse_writepage_free(fc, req);
1593 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1594 struct fuse_inode *fi)
1596 struct fuse_file *ff = NULL;
1598 spin_lock(&fc->lock);
1599 if (!list_empty(&fi->write_files)) {
1600 ff = list_entry(fi->write_files.next, struct fuse_file,
1604 spin_unlock(&fc->lock);
1609 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1610 struct fuse_inode *fi)
1612 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1617 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1619 struct fuse_conn *fc = get_fuse_conn(inode);
1620 struct fuse_inode *fi = get_fuse_inode(inode);
1621 struct fuse_file *ff;
1624 ff = __fuse_write_file_get(fc, fi);
1625 err = fuse_flush_times(inode, ff);
1627 fuse_file_put(ff, false, false);
1632 static int fuse_writepage_locked(struct page *page)
1634 struct address_space *mapping = page->mapping;
1635 struct inode *inode = mapping->host;
1636 struct fuse_conn *fc = get_fuse_conn(inode);
1637 struct fuse_inode *fi = get_fuse_inode(inode);
1638 struct fuse_req *req;
1639 struct page *tmp_page;
1640 int error = -ENOMEM;
1642 set_page_writeback(page);
1644 req = fuse_request_alloc_nofs(1);
1648 /* writeback always goes to bg_queue */
1649 __set_bit(FR_BACKGROUND, &req->flags);
1650 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1655 req->ff = fuse_write_file_get(fc, fi);
1659 fuse_write_fill(req, req->ff, page_offset(page), 0);
1661 copy_highpage(tmp_page, page);
1662 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1663 req->misc.write.next = NULL;
1664 req->in.argpages = 1;
1666 req->pages[0] = tmp_page;
1667 req->page_descs[0].offset = 0;
1668 req->page_descs[0].length = PAGE_SIZE;
1669 req->end = fuse_writepage_end;
1672 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1673 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1675 spin_lock(&fc->lock);
1676 list_add(&req->writepages_entry, &fi->writepages);
1677 list_add_tail(&req->list, &fi->queued_writes);
1678 fuse_flush_writepages(inode);
1679 spin_unlock(&fc->lock);
1681 end_page_writeback(page);
1686 __free_page(tmp_page);
1688 fuse_request_free(req);
1690 mapping_set_error(page->mapping, error);
1691 end_page_writeback(page);
1695 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1699 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1701 * ->writepages() should be called for sync() and friends. We
1702 * should only get here on direct reclaim and then we are
1703 * allowed to skip a page which is already in flight
1705 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1707 redirty_page_for_writepage(wbc, page);
1712 err = fuse_writepage_locked(page);
1718 struct fuse_fill_wb_data {
1719 struct fuse_req *req;
1720 struct fuse_file *ff;
1721 struct inode *inode;
1722 struct page **orig_pages;
1725 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1727 struct fuse_req *req = data->req;
1728 struct inode *inode = data->inode;
1729 struct fuse_conn *fc = get_fuse_conn(inode);
1730 struct fuse_inode *fi = get_fuse_inode(inode);
1731 int num_pages = req->num_pages;
1734 req->ff = fuse_file_get(data->ff);
1735 spin_lock(&fc->lock);
1736 list_add_tail(&req->list, &fi->queued_writes);
1737 fuse_flush_writepages(inode);
1738 spin_unlock(&fc->lock);
1740 for (i = 0; i < num_pages; i++)
1741 end_page_writeback(data->orig_pages[i]);
1744 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1747 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1748 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1749 struct fuse_req *tmp;
1750 struct fuse_req *old_req;
1754 BUG_ON(new_req->num_pages != 0);
1756 spin_lock(&fc->lock);
1757 list_del(&new_req->writepages_entry);
1758 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1759 BUG_ON(old_req->inode != new_req->inode);
1760 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1761 if (curr_index <= page->index &&
1762 page->index < curr_index + old_req->num_pages) {
1768 list_add(&new_req->writepages_entry, &fi->writepages);
1772 new_req->num_pages = 1;
1773 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1774 BUG_ON(tmp->inode != new_req->inode);
1775 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1776 if (tmp->num_pages == 1 &&
1777 curr_index == page->index) {
1782 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1783 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1785 copy_highpage(old_req->pages[0], page);
1786 spin_unlock(&fc->lock);
1788 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1789 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1790 wb_writeout_inc(&bdi->wb);
1791 fuse_writepage_free(fc, new_req);
1792 fuse_request_free(new_req);
1795 new_req->misc.write.next = old_req->misc.write.next;
1796 old_req->misc.write.next = new_req;
1799 spin_unlock(&fc->lock);
1804 static int fuse_writepages_fill(struct page *page,
1805 struct writeback_control *wbc, void *_data)
1807 struct fuse_fill_wb_data *data = _data;
1808 struct fuse_req *req = data->req;
1809 struct inode *inode = data->inode;
1810 struct fuse_conn *fc = get_fuse_conn(inode);
1811 struct page *tmp_page;
1817 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1823 * Being under writeback is unlikely but possible. For example direct
1824 * read to an mmaped fuse file will set the page dirty twice; once when
1825 * the pages are faulted with get_user_pages(), and then after the read
1828 is_writeback = fuse_page_is_writeback(inode, page->index);
1830 if (req && req->num_pages &&
1831 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1832 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1833 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1834 fuse_writepages_send(data);
1838 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1843 * The page must not be redirtied until the writeout is completed
1844 * (i.e. userspace has sent a reply to the write request). Otherwise
1845 * there could be more than one temporary page instance for each real
1848 * This is ensured by holding the page lock in page_mkwrite() while
1849 * checking fuse_page_is_writeback(). We already hold the page lock
1850 * since clear_page_dirty_for_io() and keep it held until we add the
1851 * request to the fi->writepages list and increment req->num_pages.
1852 * After this fuse_page_is_writeback() will indicate that the page is
1853 * under writeback, so we can release the page lock.
1855 if (data->req == NULL) {
1856 struct fuse_inode *fi = get_fuse_inode(inode);
1859 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1861 __free_page(tmp_page);
1865 fuse_write_fill(req, data->ff, page_offset(page), 0);
1866 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1867 req->misc.write.next = NULL;
1868 req->in.argpages = 1;
1869 __set_bit(FR_BACKGROUND, &req->flags);
1871 req->end = fuse_writepage_end;
1874 spin_lock(&fc->lock);
1875 list_add(&req->writepages_entry, &fi->writepages);
1876 spin_unlock(&fc->lock);
1880 set_page_writeback(page);
1882 copy_highpage(tmp_page, page);
1883 req->pages[req->num_pages] = tmp_page;
1884 req->page_descs[req->num_pages].offset = 0;
1885 req->page_descs[req->num_pages].length = PAGE_SIZE;
1887 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1888 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1891 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1892 end_page_writeback(page);
1896 data->orig_pages[req->num_pages] = page;
1899 * Protected by fc->lock against concurrent access by
1900 * fuse_page_is_writeback().
1902 spin_lock(&fc->lock);
1904 spin_unlock(&fc->lock);
1912 static int fuse_writepages(struct address_space *mapping,
1913 struct writeback_control *wbc)
1915 struct inode *inode = mapping->host;
1916 struct fuse_fill_wb_data data;
1920 if (is_bad_inode(inode))
1928 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1929 sizeof(struct page *),
1931 if (!data.orig_pages)
1934 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1936 /* Ignore errors if we can write at least one page */
1937 BUG_ON(!data.req->num_pages);
1938 fuse_writepages_send(&data);
1942 fuse_file_put(data.ff, false, false);
1944 kfree(data.orig_pages);
1950 * It's worthy to make sure that space is reserved on disk for the write,
1951 * but how to implement it without killing performance need more thinking.
1953 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1954 loff_t pos, unsigned len, unsigned flags,
1955 struct page **pagep, void **fsdata)
1957 pgoff_t index = pos >> PAGE_SHIFT;
1958 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1963 WARN_ON(!fc->writeback_cache);
1965 page = grab_cache_page_write_begin(mapping, index, flags);
1969 fuse_wait_on_page_writeback(mapping->host, page->index);
1971 if (PageUptodate(page) || len == PAGE_SIZE)
1974 * Check if the start this page comes after the end of file, in which
1975 * case the readpage can be optimized away.
1977 fsize = i_size_read(mapping->host);
1978 if (fsize <= (pos & PAGE_MASK)) {
1979 size_t off = pos & ~PAGE_MASK;
1981 zero_user_segment(page, 0, off);
1984 err = fuse_do_readpage(file, page);
1998 static int fuse_write_end(struct file *file, struct address_space *mapping,
1999 loff_t pos, unsigned len, unsigned copied,
2000 struct page *page, void *fsdata)
2002 struct inode *inode = page->mapping->host;
2004 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2008 if (!PageUptodate(page)) {
2009 /* Zero any unwritten bytes at the end of the page */
2010 size_t endoff = (pos + copied) & ~PAGE_MASK;
2012 zero_user_segment(page, endoff, PAGE_SIZE);
2013 SetPageUptodate(page);
2016 fuse_write_update_size(inode, pos + copied);
2017 set_page_dirty(page);
2026 static int fuse_launder_page(struct page *page)
2029 if (clear_page_dirty_for_io(page)) {
2030 struct inode *inode = page->mapping->host;
2031 err = fuse_writepage_locked(page);
2033 fuse_wait_on_page_writeback(inode, page->index);
2039 * Write back dirty pages now, because there may not be any suitable
2042 static void fuse_vma_close(struct vm_area_struct *vma)
2044 filemap_write_and_wait(vma->vm_file->f_mapping);
2048 * Wait for writeback against this page to complete before allowing it
2049 * to be marked dirty again, and hence written back again, possibly
2050 * before the previous writepage completed.
2052 * Block here, instead of in ->writepage(), so that the userspace fs
2053 * can only block processes actually operating on the filesystem.
2055 * Otherwise unprivileged userspace fs would be able to block
2060 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2062 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2064 struct page *page = vmf->page;
2065 struct inode *inode = file_inode(vmf->vma->vm_file);
2067 file_update_time(vmf->vma->vm_file);
2069 if (page->mapping != inode->i_mapping) {
2071 return VM_FAULT_NOPAGE;
2074 fuse_wait_on_page_writeback(inode, page->index);
2075 return VM_FAULT_LOCKED;
2078 static const struct vm_operations_struct fuse_file_vm_ops = {
2079 .close = fuse_vma_close,
2080 .fault = filemap_fault,
2081 .map_pages = filemap_map_pages,
2082 .page_mkwrite = fuse_page_mkwrite,
2085 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2087 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2088 fuse_link_write_file(file);
2090 file_accessed(file);
2091 vma->vm_ops = &fuse_file_vm_ops;
2095 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2097 /* Can't provide the coherency needed for MAP_SHARED */
2098 if (vma->vm_flags & VM_MAYSHARE)
2101 invalidate_inode_pages2(file->f_mapping);
2103 return generic_file_mmap(file, vma);
2106 static int convert_fuse_file_lock(struct fuse_conn *fc,
2107 const struct fuse_file_lock *ffl,
2108 struct file_lock *fl)
2110 switch (ffl->type) {
2116 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2117 ffl->end < ffl->start)
2120 fl->fl_start = ffl->start;
2121 fl->fl_end = ffl->end;
2124 * Convert pid into init's pid namespace. The locks API will
2125 * translate it into the caller's pid namespace.
2128 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2135 fl->fl_type = ffl->type;
2139 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2140 const struct file_lock *fl, int opcode, pid_t pid,
2141 int flock, struct fuse_lk_in *inarg)
2143 struct inode *inode = file_inode(file);
2144 struct fuse_conn *fc = get_fuse_conn(inode);
2145 struct fuse_file *ff = file->private_data;
2147 memset(inarg, 0, sizeof(*inarg));
2149 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2150 inarg->lk.start = fl->fl_start;
2151 inarg->lk.end = fl->fl_end;
2152 inarg->lk.type = fl->fl_type;
2153 inarg->lk.pid = pid;
2155 inarg->lk_flags |= FUSE_LK_FLOCK;
2156 args->in.h.opcode = opcode;
2157 args->in.h.nodeid = get_node_id(inode);
2158 args->in.numargs = 1;
2159 args->in.args[0].size = sizeof(*inarg);
2160 args->in.args[0].value = inarg;
2163 static int fuse_getlk(struct file *file, struct file_lock *fl)
2165 struct inode *inode = file_inode(file);
2166 struct fuse_conn *fc = get_fuse_conn(inode);
2168 struct fuse_lk_in inarg;
2169 struct fuse_lk_out outarg;
2172 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2173 args.out.numargs = 1;
2174 args.out.args[0].size = sizeof(outarg);
2175 args.out.args[0].value = &outarg;
2176 err = fuse_simple_request(fc, &args);
2178 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2183 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2185 struct inode *inode = file_inode(file);
2186 struct fuse_conn *fc = get_fuse_conn(inode);
2188 struct fuse_lk_in inarg;
2189 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2190 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2191 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2194 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2195 /* NLM needs asynchronous locks, which we don't support yet */
2199 /* Unlock on close is handled by the flush method */
2200 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2203 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2204 err = fuse_simple_request(fc, &args);
2206 /* locking is restartable */
2213 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2215 struct inode *inode = file_inode(file);
2216 struct fuse_conn *fc = get_fuse_conn(inode);
2219 if (cmd == F_CANCELLK) {
2221 } else if (cmd == F_GETLK) {
2223 posix_test_lock(file, fl);
2226 err = fuse_getlk(file, fl);
2229 err = posix_lock_file(file, fl, NULL);
2231 err = fuse_setlk(file, fl, 0);
2236 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2238 struct inode *inode = file_inode(file);
2239 struct fuse_conn *fc = get_fuse_conn(inode);
2243 err = locks_lock_file_wait(file, fl);
2245 struct fuse_file *ff = file->private_data;
2247 /* emulate flock with POSIX locks */
2249 err = fuse_setlk(file, fl, 1);
2255 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2257 struct inode *inode = mapping->host;
2258 struct fuse_conn *fc = get_fuse_conn(inode);
2260 struct fuse_bmap_in inarg;
2261 struct fuse_bmap_out outarg;
2264 if (!inode->i_sb->s_bdev || fc->no_bmap)
2267 memset(&inarg, 0, sizeof(inarg));
2268 inarg.block = block;
2269 inarg.blocksize = inode->i_sb->s_blocksize;
2270 args.in.h.opcode = FUSE_BMAP;
2271 args.in.h.nodeid = get_node_id(inode);
2272 args.in.numargs = 1;
2273 args.in.args[0].size = sizeof(inarg);
2274 args.in.args[0].value = &inarg;
2275 args.out.numargs = 1;
2276 args.out.args[0].size = sizeof(outarg);
2277 args.out.args[0].value = &outarg;
2278 err = fuse_simple_request(fc, &args);
2282 return err ? 0 : outarg.block;
2285 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2287 struct inode *inode = file->f_mapping->host;
2288 struct fuse_conn *fc = get_fuse_conn(inode);
2289 struct fuse_file *ff = file->private_data;
2291 struct fuse_lseek_in inarg = {
2296 struct fuse_lseek_out outarg;
2302 args.in.h.opcode = FUSE_LSEEK;
2303 args.in.h.nodeid = ff->nodeid;
2304 args.in.numargs = 1;
2305 args.in.args[0].size = sizeof(inarg);
2306 args.in.args[0].value = &inarg;
2307 args.out.numargs = 1;
2308 args.out.args[0].size = sizeof(outarg);
2309 args.out.args[0].value = &outarg;
2310 err = fuse_simple_request(fc, &args);
2312 if (err == -ENOSYS) {
2319 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2322 err = fuse_update_attributes(inode, file);
2324 return generic_file_llseek(file, offset, whence);
2329 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2332 struct inode *inode = file_inode(file);
2337 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2338 retval = generic_file_llseek(file, offset, whence);
2342 retval = fuse_update_attributes(inode, file);
2344 retval = generic_file_llseek(file, offset, whence);
2345 inode_unlock(inode);
2350 retval = fuse_lseek(file, offset, whence);
2351 inode_unlock(inode);
2361 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2362 * ABI was defined to be 'struct iovec' which is different on 32bit
2363 * and 64bit. Fortunately we can determine which structure the server
2364 * used from the size of the reply.
2366 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2367 size_t transferred, unsigned count,
2370 #ifdef CONFIG_COMPAT
2371 if (count * sizeof(struct compat_iovec) == transferred) {
2372 struct compat_iovec *ciov = src;
2376 * With this interface a 32bit server cannot support
2377 * non-compat (i.e. ones coming from 64bit apps) ioctl
2383 for (i = 0; i < count; i++) {
2384 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2385 dst[i].iov_len = ciov[i].iov_len;
2391 if (count * sizeof(struct iovec) != transferred)
2394 memcpy(dst, src, transferred);
2398 /* Make sure iov_length() won't overflow */
2399 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2402 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2404 for (n = 0; n < count; n++, iov++) {
2405 if (iov->iov_len > (size_t) max)
2407 max -= iov->iov_len;
2412 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2413 void *src, size_t transferred, unsigned count,
2417 struct fuse_ioctl_iovec *fiov = src;
2419 if (fc->minor < 16) {
2420 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2424 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2427 for (i = 0; i < count; i++) {
2428 /* Did the server supply an inappropriate value? */
2429 if (fiov[i].base != (unsigned long) fiov[i].base ||
2430 fiov[i].len != (unsigned long) fiov[i].len)
2433 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2434 dst[i].iov_len = (size_t) fiov[i].len;
2436 #ifdef CONFIG_COMPAT
2438 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2439 (compat_size_t) dst[i].iov_len != fiov[i].len))
2449 * For ioctls, there is no generic way to determine how much memory
2450 * needs to be read and/or written. Furthermore, ioctls are allowed
2451 * to dereference the passed pointer, so the parameter requires deep
2452 * copying but FUSE has no idea whatsoever about what to copy in or
2455 * This is solved by allowing FUSE server to retry ioctl with
2456 * necessary in/out iovecs. Let's assume the ioctl implementation
2457 * needs to read in the following structure.
2464 * On the first callout to FUSE server, inarg->in_size and
2465 * inarg->out_size will be NULL; then, the server completes the ioctl
2466 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2467 * the actual iov array to
2469 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2471 * which tells FUSE to copy in the requested area and retry the ioctl.
2472 * On the second round, the server has access to the structure and
2473 * from that it can tell what to look for next, so on the invocation,
2474 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2476 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2477 * { .iov_base = a.buf, .iov_len = a.buflen } }
2479 * FUSE will copy both struct a and the pointed buffer from the
2480 * process doing the ioctl and retry ioctl with both struct a and the
2483 * This time, FUSE server has everything it needs and completes ioctl
2484 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2486 * Copying data out works the same way.
2488 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2489 * automatically initializes in and out iovs by decoding @cmd with
2490 * _IOC_* macros and the server is not allowed to request RETRY. This
2491 * limits ioctl data transfers to well-formed ioctls and is the forced
2492 * behavior for all FUSE servers.
2494 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2497 struct fuse_file *ff = file->private_data;
2498 struct fuse_conn *fc = ff->fc;
2499 struct fuse_ioctl_in inarg = {
2505 struct fuse_ioctl_out outarg;
2506 struct fuse_req *req = NULL;
2507 struct page **pages = NULL;
2508 struct iovec *iov_page = NULL;
2509 struct iovec *in_iov = NULL, *out_iov = NULL;
2510 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2511 size_t in_size, out_size, transferred, c;
2515 #if BITS_PER_LONG == 32
2516 inarg.flags |= FUSE_IOCTL_32BIT;
2518 if (flags & FUSE_IOCTL_COMPAT)
2519 inarg.flags |= FUSE_IOCTL_32BIT;
2522 /* assume all the iovs returned by client always fits in a page */
2523 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2526 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2527 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2528 if (!pages || !iov_page)
2532 * If restricted, initialize IO parameters as encoded in @cmd.
2533 * RETRY from server is not allowed.
2535 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2536 struct iovec *iov = iov_page;
2538 iov->iov_base = (void __user *)arg;
2541 case FS_IOC_GETFLAGS:
2542 case FS_IOC_SETFLAGS:
2543 iov->iov_len = sizeof(int);
2546 iov->iov_len = _IOC_SIZE(cmd);
2550 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2555 if (_IOC_DIR(cmd) & _IOC_READ) {
2562 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2563 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2566 * Out data can be used either for actual out data or iovs,
2567 * make sure there always is at least one page.
2569 out_size = max_t(size_t, out_size, PAGE_SIZE);
2570 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2572 /* make sure there are enough buffer pages and init request with them */
2574 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2576 while (num_pages < max_pages) {
2577 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2578 if (!pages[num_pages])
2583 req = fuse_get_req(fc, num_pages);
2589 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2590 req->num_pages = num_pages;
2591 fuse_page_descs_length_init(req, 0, req->num_pages);
2593 /* okay, let's send it to the client */
2594 req->in.h.opcode = FUSE_IOCTL;
2595 req->in.h.nodeid = ff->nodeid;
2596 req->in.numargs = 1;
2597 req->in.args[0].size = sizeof(inarg);
2598 req->in.args[0].value = &inarg;
2601 req->in.args[1].size = in_size;
2602 req->in.argpages = 1;
2605 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2606 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2607 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2608 if (c != PAGE_SIZE && iov_iter_count(&ii))
2613 req->out.numargs = 2;
2614 req->out.args[0].size = sizeof(outarg);
2615 req->out.args[0].value = &outarg;
2616 req->out.args[1].size = out_size;
2617 req->out.argpages = 1;
2618 req->out.argvar = 1;
2620 fuse_request_send(fc, req);
2621 err = req->out.h.error;
2622 transferred = req->out.args[1].size;
2623 fuse_put_request(fc, req);
2628 /* did it ask for retry? */
2629 if (outarg.flags & FUSE_IOCTL_RETRY) {
2632 /* no retry if in restricted mode */
2634 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2637 in_iovs = outarg.in_iovs;
2638 out_iovs = outarg.out_iovs;
2641 * Make sure things are in boundary, separate checks
2642 * are to protect against overflow.
2645 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2646 out_iovs > FUSE_IOCTL_MAX_IOV ||
2647 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2650 vaddr = kmap_atomic(pages[0]);
2651 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2652 transferred, in_iovs + out_iovs,
2653 (flags & FUSE_IOCTL_COMPAT) != 0);
2654 kunmap_atomic(vaddr);
2659 out_iov = in_iov + in_iovs;
2661 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2665 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2673 if (transferred > inarg.out_size)
2677 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2678 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2679 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2680 if (c != PAGE_SIZE && iov_iter_count(&ii))
2686 fuse_put_request(fc, req);
2687 free_page((unsigned long) iov_page);
2689 __free_page(pages[--num_pages]);
2692 return err ? err : outarg.result;
2694 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2696 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2697 unsigned long arg, unsigned int flags)
2699 struct inode *inode = file_inode(file);
2700 struct fuse_conn *fc = get_fuse_conn(inode);
2702 if (!fuse_allow_current_process(fc))
2705 if (is_bad_inode(inode))
2708 return fuse_do_ioctl(file, cmd, arg, flags);
2711 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2714 return fuse_ioctl_common(file, cmd, arg, 0);
2717 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2720 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2724 * All files which have been polled are linked to RB tree
2725 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2726 * find the matching one.
2728 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2729 struct rb_node **parent_out)
2731 struct rb_node **link = &fc->polled_files.rb_node;
2732 struct rb_node *last = NULL;
2735 struct fuse_file *ff;
2738 ff = rb_entry(last, struct fuse_file, polled_node);
2741 link = &last->rb_left;
2742 else if (kh > ff->kh)
2743 link = &last->rb_right;
2754 * The file is about to be polled. Make sure it's on the polled_files
2755 * RB tree. Note that files once added to the polled_files tree are
2756 * not removed before the file is released. This is because a file
2757 * polled once is likely to be polled again.
2759 static void fuse_register_polled_file(struct fuse_conn *fc,
2760 struct fuse_file *ff)
2762 spin_lock(&fc->lock);
2763 if (RB_EMPTY_NODE(&ff->polled_node)) {
2764 struct rb_node **link, *uninitialized_var(parent);
2766 link = fuse_find_polled_node(fc, ff->kh, &parent);
2768 rb_link_node(&ff->polled_node, parent, link);
2769 rb_insert_color(&ff->polled_node, &fc->polled_files);
2771 spin_unlock(&fc->lock);
2774 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2776 struct fuse_file *ff = file->private_data;
2777 struct fuse_conn *fc = ff->fc;
2778 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2779 struct fuse_poll_out outarg;
2784 return DEFAULT_POLLMASK;
2786 poll_wait(file, &ff->poll_wait, wait);
2787 inarg.events = mangle_poll(poll_requested_events(wait));
2790 * Ask for notification iff there's someone waiting for it.
2791 * The client may ignore the flag and always notify.
2793 if (waitqueue_active(&ff->poll_wait)) {
2794 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2795 fuse_register_polled_file(fc, ff);
2798 args.in.h.opcode = FUSE_POLL;
2799 args.in.h.nodeid = ff->nodeid;
2800 args.in.numargs = 1;
2801 args.in.args[0].size = sizeof(inarg);
2802 args.in.args[0].value = &inarg;
2803 args.out.numargs = 1;
2804 args.out.args[0].size = sizeof(outarg);
2805 args.out.args[0].value = &outarg;
2806 err = fuse_simple_request(fc, &args);
2809 return demangle_poll(outarg.revents);
2810 if (err == -ENOSYS) {
2812 return DEFAULT_POLLMASK;
2816 EXPORT_SYMBOL_GPL(fuse_file_poll);
2819 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2820 * wakes up the poll waiters.
2822 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2823 struct fuse_notify_poll_wakeup_out *outarg)
2825 u64 kh = outarg->kh;
2826 struct rb_node **link;
2828 spin_lock(&fc->lock);
2830 link = fuse_find_polled_node(fc, kh, NULL);
2832 struct fuse_file *ff;
2834 ff = rb_entry(*link, struct fuse_file, polled_node);
2835 wake_up_interruptible_sync(&ff->poll_wait);
2838 spin_unlock(&fc->lock);
2842 static void fuse_do_truncate(struct file *file)
2844 struct inode *inode = file->f_mapping->host;
2847 attr.ia_valid = ATTR_SIZE;
2848 attr.ia_size = i_size_read(inode);
2850 attr.ia_file = file;
2851 attr.ia_valid |= ATTR_FILE;
2853 fuse_do_setattr(file_dentry(file), &attr, file);
2856 static inline loff_t fuse_round_up(loff_t off)
2858 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2862 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2864 DECLARE_COMPLETION_ONSTACK(wait);
2866 struct file *file = iocb->ki_filp;
2867 struct fuse_file *ff = file->private_data;
2868 bool async_dio = ff->fc->async_dio;
2870 struct inode *inode;
2872 size_t count = iov_iter_count(iter);
2873 loff_t offset = iocb->ki_pos;
2874 struct fuse_io_priv *io;
2877 inode = file->f_mapping->host;
2878 i_size = i_size_read(inode);
2880 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2883 /* optimization for short read */
2884 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2885 if (offset >= i_size)
2887 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2888 count = iov_iter_count(iter);
2891 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2894 spin_lock_init(&io->lock);
2895 kref_init(&io->refcnt);
2899 io->offset = offset;
2900 io->write = (iov_iter_rw(iter) == WRITE);
2903 * By default, we want to optimize all I/Os with async request
2904 * submission to the client filesystem if supported.
2906 io->async = async_dio;
2908 io->blocking = is_sync_kiocb(iocb);
2911 * We cannot asynchronously extend the size of a file.
2912 * In such case the aio will behave exactly like sync io.
2914 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2915 io->blocking = true;
2917 if (io->async && io->blocking) {
2919 * Additional reference to keep io around after
2920 * calling fuse_aio_complete()
2922 kref_get(&io->refcnt);
2926 if (iov_iter_rw(iter) == WRITE) {
2927 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2928 fuse_invalidate_attr(inode);
2930 ret = __fuse_direct_read(io, iter, &pos);
2934 bool blocking = io->blocking;
2936 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2938 /* we have a non-extending, async request, so return */
2940 return -EIOCBQUEUED;
2942 wait_for_completion(&wait);
2943 ret = fuse_get_res_by_io(io);
2946 kref_put(&io->refcnt, fuse_io_release);
2948 if (iov_iter_rw(iter) == WRITE) {
2950 fuse_write_update_size(inode, pos);
2951 else if (ret < 0 && offset + count > i_size)
2952 fuse_do_truncate(file);
2958 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2961 struct fuse_file *ff = file->private_data;
2962 struct inode *inode = file_inode(file);
2963 struct fuse_inode *fi = get_fuse_inode(inode);
2964 struct fuse_conn *fc = ff->fc;
2966 struct fuse_fallocate_in inarg = {
2973 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2974 (mode & FALLOC_FL_PUNCH_HOLE);
2976 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2979 if (fc->no_fallocate)
2984 if (mode & FALLOC_FL_PUNCH_HOLE) {
2985 loff_t endbyte = offset + length - 1;
2986 err = filemap_write_and_wait_range(inode->i_mapping,
2991 fuse_sync_writes(inode);
2995 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2996 offset + length > i_size_read(inode)) {
2997 err = inode_newsize_ok(inode, offset + length);
3002 if (!(mode & FALLOC_FL_KEEP_SIZE))
3003 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3005 args.in.h.opcode = FUSE_FALLOCATE;
3006 args.in.h.nodeid = ff->nodeid;
3007 args.in.numargs = 1;
3008 args.in.args[0].size = sizeof(inarg);
3009 args.in.args[0].value = &inarg;
3010 err = fuse_simple_request(fc, &args);
3011 if (err == -ENOSYS) {
3012 fc->no_fallocate = 1;
3018 /* we could have extended the file */
3019 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3020 bool changed = fuse_write_update_size(inode, offset + length);
3022 if (changed && fc->writeback_cache)
3023 file_update_time(file);
3026 if (mode & FALLOC_FL_PUNCH_HOLE)
3027 truncate_pagecache_range(inode, offset, offset + length - 1);
3029 fuse_invalidate_attr(inode);
3032 if (!(mode & FALLOC_FL_KEEP_SIZE))
3033 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3036 inode_unlock(inode);
3041 static const struct file_operations fuse_file_operations = {
3042 .llseek = fuse_file_llseek,
3043 .read_iter = fuse_file_read_iter,
3044 .write_iter = fuse_file_write_iter,
3045 .mmap = fuse_file_mmap,
3047 .flush = fuse_flush,
3048 .release = fuse_release,
3049 .fsync = fuse_fsync,
3050 .lock = fuse_file_lock,
3051 .flock = fuse_file_flock,
3052 .splice_read = generic_file_splice_read,
3053 .unlocked_ioctl = fuse_file_ioctl,
3054 .compat_ioctl = fuse_file_compat_ioctl,
3055 .poll = fuse_file_poll,
3056 .fallocate = fuse_file_fallocate,
3059 static const struct file_operations fuse_direct_io_file_operations = {
3060 .llseek = fuse_file_llseek,
3061 .read_iter = fuse_direct_read_iter,
3062 .write_iter = fuse_direct_write_iter,
3063 .mmap = fuse_direct_mmap,
3065 .flush = fuse_flush,
3066 .release = fuse_release,
3067 .fsync = fuse_fsync,
3068 .lock = fuse_file_lock,
3069 .flock = fuse_file_flock,
3070 .unlocked_ioctl = fuse_file_ioctl,
3071 .compat_ioctl = fuse_file_compat_ioctl,
3072 .poll = fuse_file_poll,
3073 .fallocate = fuse_file_fallocate,
3074 /* no splice_read */
3077 static const struct address_space_operations fuse_file_aops = {
3078 .readpage = fuse_readpage,
3079 .writepage = fuse_writepage,
3080 .writepages = fuse_writepages,
3081 .launder_page = fuse_launder_page,
3082 .readpages = fuse_readpages,
3083 .set_page_dirty = __set_page_dirty_nobuffers,
3085 .direct_IO = fuse_direct_IO,
3086 .write_begin = fuse_write_begin,
3087 .write_end = fuse_write_end,
3090 void fuse_init_file_inode(struct inode *inode)
3092 inode->i_fop = &fuse_file_operations;
3093 inode->i_data.a_ops = &fuse_file_aops;