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/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/falloc.h>
19 #include <linux/uio.h>
22 static const struct file_operations fuse_direct_io_file_operations;
24 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
25 int opcode, struct fuse_open_out *outargp)
27 struct fuse_open_in inarg;
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
34 args.in.h.opcode = opcode;
35 args.in.h.nodeid = nodeid;
37 args.in.args[0].size = sizeof(inarg);
38 args.in.args[0].value = &inarg;
40 args.out.args[0].size = sizeof(*outargp);
41 args.out.args[0].value = outargp;
43 return fuse_simple_request(fc, &args);
46 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
50 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
55 ff->reserved_req = fuse_request_alloc(0);
56 if (unlikely(!ff->reserved_req)) {
61 INIT_LIST_HEAD(&ff->write_entry);
62 atomic_set(&ff->count, 0);
63 RB_CLEAR_NODE(&ff->polled_node);
64 init_waitqueue_head(&ff->poll_wait);
68 spin_unlock(&fc->lock);
73 void fuse_file_free(struct fuse_file *ff)
75 fuse_request_free(ff->reserved_req);
79 struct fuse_file *fuse_file_get(struct fuse_file *ff)
81 atomic_inc(&ff->count);
85 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
87 iput(req->misc.release.inode);
90 static void fuse_file_put(struct fuse_file *ff, bool sync)
92 if (atomic_dec_and_test(&ff->count)) {
93 struct fuse_req *req = ff->reserved_req;
95 if (ff->fc->no_open) {
97 * Drop the release request when client does not
100 __clear_bit(FR_BACKGROUND, &req->flags);
101 iput(req->misc.release.inode);
102 fuse_put_request(ff->fc, req);
104 __set_bit(FR_FORCE, &req->flags);
105 __clear_bit(FR_BACKGROUND, &req->flags);
106 fuse_request_send(ff->fc, req);
107 iput(req->misc.release.inode);
108 fuse_put_request(ff->fc, req);
110 req->end = fuse_release_end;
111 __set_bit(FR_BACKGROUND, &req->flags);
112 fuse_request_send_background(ff->fc, req);
118 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
121 struct fuse_file *ff;
122 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
124 ff = fuse_file_alloc(fc);
129 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
130 if (!fc->no_open || isdir) {
131 struct fuse_open_out outarg;
134 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
137 ff->open_flags = outarg.open_flags;
139 } else if (err != -ENOSYS || isdir) {
148 ff->open_flags &= ~FOPEN_DIRECT_IO;
151 file->private_data = fuse_file_get(ff);
155 EXPORT_SYMBOL_GPL(fuse_do_open);
157 static void fuse_link_write_file(struct file *file)
159 struct inode *inode = file_inode(file);
160 struct fuse_conn *fc = get_fuse_conn(inode);
161 struct fuse_inode *fi = get_fuse_inode(inode);
162 struct fuse_file *ff = file->private_data;
164 * file may be written through mmap, so chain it onto the
165 * inodes's write_file list
167 spin_lock(&fc->lock);
168 if (list_empty(&ff->write_entry))
169 list_add(&ff->write_entry, &fi->write_files);
170 spin_unlock(&fc->lock);
173 void fuse_finish_open(struct inode *inode, struct file *file)
175 struct fuse_file *ff = file->private_data;
176 struct fuse_conn *fc = get_fuse_conn(inode);
178 if (ff->open_flags & FOPEN_DIRECT_IO)
179 file->f_op = &fuse_direct_io_file_operations;
180 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
181 invalidate_inode_pages2(inode->i_mapping);
182 if (ff->open_flags & FOPEN_STREAM)
183 stream_open(inode, file);
184 else if (ff->open_flags & FOPEN_NONSEEKABLE)
185 nonseekable_open(inode, file);
186 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
187 struct fuse_inode *fi = get_fuse_inode(inode);
189 spin_lock(&fc->lock);
190 fi->attr_version = ++fc->attr_version;
191 i_size_write(inode, 0);
192 spin_unlock(&fc->lock);
193 fuse_invalidate_attr(inode);
194 if (fc->writeback_cache)
195 file_update_time(file);
197 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
198 fuse_link_write_file(file);
201 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
203 struct fuse_conn *fc = get_fuse_conn(inode);
205 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
206 fc->atomic_o_trunc &&
209 err = generic_file_open(inode, file);
213 if (is_wb_truncate) {
215 fuse_set_nowrite(inode);
218 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
221 fuse_finish_open(inode, file);
223 if (is_wb_truncate) {
224 fuse_release_nowrite(inode);
231 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
233 struct fuse_conn *fc = ff->fc;
234 struct fuse_req *req = ff->reserved_req;
235 struct fuse_release_in *inarg = &req->misc.release.in;
237 spin_lock(&fc->lock);
238 list_del(&ff->write_entry);
239 if (!RB_EMPTY_NODE(&ff->polled_node))
240 rb_erase(&ff->polled_node, &fc->polled_files);
241 spin_unlock(&fc->lock);
243 wake_up_interruptible_all(&ff->poll_wait);
246 inarg->flags = flags;
247 req->in.h.opcode = opcode;
248 req->in.h.nodeid = ff->nodeid;
250 req->in.args[0].size = sizeof(struct fuse_release_in);
251 req->in.args[0].value = inarg;
254 void fuse_release_common(struct file *file, int opcode)
256 struct fuse_file *ff;
257 struct fuse_req *req;
259 ff = file->private_data;
263 req = ff->reserved_req;
264 fuse_prepare_release(ff, file->f_flags, opcode);
267 struct fuse_release_in *inarg = &req->misc.release.in;
268 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
269 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
272 /* Hold inode until release is finished */
273 req->misc.release.inode = igrab(file_inode(file));
276 * Normally this will send the RELEASE request, however if
277 * some asynchronous READ or WRITE requests are outstanding,
278 * the sending will be delayed.
280 * Make the release synchronous if this is a fuseblk mount,
281 * synchronous RELEASE is allowed (and desirable) in this case
282 * because the server can be trusted not to screw up.
284 fuse_file_put(ff, ff->fc->destroy_req != NULL);
287 static int fuse_open(struct inode *inode, struct file *file)
289 return fuse_open_common(inode, file, false);
292 static int fuse_release(struct inode *inode, struct file *file)
294 struct fuse_conn *fc = get_fuse_conn(inode);
296 /* see fuse_vma_close() for !writeback_cache case */
297 if (fc->writeback_cache)
298 write_inode_now(inode, 1);
300 fuse_release_common(file, FUSE_RELEASE);
302 /* return value is ignored by VFS */
306 void fuse_sync_release(struct fuse_file *ff, int flags)
308 WARN_ON(atomic_read(&ff->count) > 1);
309 fuse_prepare_release(ff, flags, FUSE_RELEASE);
310 __set_bit(FR_FORCE, &ff->reserved_req->flags);
311 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
312 fuse_request_send(ff->fc, ff->reserved_req);
313 fuse_put_request(ff->fc, ff->reserved_req);
316 EXPORT_SYMBOL_GPL(fuse_sync_release);
319 * Scramble the ID space with XTEA, so that the value of the files_struct
320 * pointer is not exposed to userspace.
322 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
324 u32 *k = fc->scramble_key;
325 u64 v = (unsigned long) id;
331 for (i = 0; i < 32; i++) {
332 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
334 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
337 return (u64) v0 + ((u64) v1 << 32);
341 * Check if any page in a range is under writeback
343 * This is currently done by walking the list of writepage requests
344 * for the inode, which can be pretty inefficient.
346 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
349 struct fuse_conn *fc = get_fuse_conn(inode);
350 struct fuse_inode *fi = get_fuse_inode(inode);
351 struct fuse_req *req;
354 spin_lock(&fc->lock);
355 list_for_each_entry(req, &fi->writepages, writepages_entry) {
358 BUG_ON(req->inode != inode);
359 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
360 if (idx_from < curr_index + req->num_pages &&
361 curr_index <= idx_to) {
366 spin_unlock(&fc->lock);
371 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
373 return fuse_range_is_writeback(inode, index, index);
377 * Wait for page writeback to be completed.
379 * Since fuse doesn't rely on the VM writeback tracking, this has to
380 * use some other means.
382 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
384 struct fuse_inode *fi = get_fuse_inode(inode);
386 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
391 * Wait for all pending writepages on the inode to finish.
393 * This is currently done by blocking further writes with FUSE_NOWRITE
394 * and waiting for all sent writes to complete.
396 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
397 * could conflict with truncation.
399 static void fuse_sync_writes(struct inode *inode)
401 fuse_set_nowrite(inode);
402 fuse_release_nowrite(inode);
405 static int fuse_flush(struct file *file, fl_owner_t id)
407 struct inode *inode = file_inode(file);
408 struct fuse_conn *fc = get_fuse_conn(inode);
409 struct fuse_file *ff = file->private_data;
410 struct fuse_req *req;
411 struct fuse_flush_in inarg;
414 if (is_bad_inode(inode))
420 err = write_inode_now(inode, 1);
425 fuse_sync_writes(inode);
428 err = filemap_check_errors(file->f_mapping);
432 req = fuse_get_req_nofail_nopages(fc, file);
433 memset(&inarg, 0, sizeof(inarg));
435 inarg.lock_owner = fuse_lock_owner_id(fc, id);
436 req->in.h.opcode = FUSE_FLUSH;
437 req->in.h.nodeid = get_node_id(inode);
439 req->in.args[0].size = sizeof(inarg);
440 req->in.args[0].value = &inarg;
441 __set_bit(FR_FORCE, &req->flags);
442 fuse_request_send(fc, req);
443 err = req->out.h.error;
444 fuse_put_request(fc, req);
445 if (err == -ENOSYS) {
452 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
453 int datasync, int isdir)
455 struct inode *inode = file->f_mapping->host;
456 struct fuse_conn *fc = get_fuse_conn(inode);
457 struct fuse_file *ff = file->private_data;
459 struct fuse_fsync_in inarg;
462 if (is_bad_inode(inode))
468 * Start writeback against all dirty pages of the inode, then
469 * wait for all outstanding writes, before sending the FSYNC
472 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
476 fuse_sync_writes(inode);
479 * Due to implementation of fuse writeback
480 * filemap_write_and_wait_range() does not catch errors.
481 * We have to do this directly after fuse_sync_writes()
483 err = filemap_check_errors(file->f_mapping);
487 err = sync_inode_metadata(inode, 1);
491 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
494 memset(&inarg, 0, sizeof(inarg));
496 inarg.fsync_flags = datasync ? 1 : 0;
497 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
498 args.in.h.nodeid = get_node_id(inode);
500 args.in.args[0].size = sizeof(inarg);
501 args.in.args[0].value = &inarg;
502 err = fuse_simple_request(fc, &args);
503 if (err == -ENOSYS) {
515 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
518 return fuse_fsync_common(file, start, end, datasync, 0);
521 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
522 size_t count, int opcode)
524 struct fuse_read_in *inarg = &req->misc.read.in;
525 struct fuse_file *ff = file->private_data;
530 inarg->flags = file->f_flags;
531 req->in.h.opcode = opcode;
532 req->in.h.nodeid = ff->nodeid;
534 req->in.args[0].size = sizeof(struct fuse_read_in);
535 req->in.args[0].value = inarg;
537 req->out.numargs = 1;
538 req->out.args[0].size = count;
541 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
545 for (i = 0; i < req->num_pages; i++) {
546 struct page *page = req->pages[i];
548 set_page_dirty_lock(page);
553 static void fuse_io_release(struct kref *kref)
555 kfree(container_of(kref, struct fuse_io_priv, refcnt));
558 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
563 if (io->bytes >= 0 && io->write)
566 return io->bytes < 0 ? io->size : io->bytes;
570 * In case of short read, the caller sets 'pos' to the position of
571 * actual end of fuse request in IO request. Otherwise, if bytes_requested
572 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
575 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
576 * both submitted asynchronously. The first of them was ACKed by userspace as
577 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
578 * second request was ACKed as short, e.g. only 1K was read, resulting in
581 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
582 * will be equal to the length of the longest contiguous fragment of
583 * transferred data starting from the beginning of IO request.
585 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
589 spin_lock(&io->lock);
591 io->err = io->err ? : err;
592 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
596 if (!left && io->blocking)
598 spin_unlock(&io->lock);
600 if (!left && !io->blocking) {
601 ssize_t res = fuse_get_res_by_io(io);
604 struct inode *inode = file_inode(io->iocb->ki_filp);
605 struct fuse_conn *fc = get_fuse_conn(inode);
606 struct fuse_inode *fi = get_fuse_inode(inode);
608 spin_lock(&fc->lock);
609 fi->attr_version = ++fc->attr_version;
610 spin_unlock(&fc->lock);
613 io->iocb->ki_complete(io->iocb, res, 0);
616 kref_put(&io->refcnt, fuse_io_release);
619 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
621 struct fuse_io_priv *io = req->io;
624 fuse_release_user_pages(req, !io->write);
627 if (req->misc.write.in.size != req->misc.write.out.size)
628 pos = req->misc.write.in.offset - io->offset +
629 req->misc.write.out.size;
631 if (req->misc.read.in.size != req->out.args[0].size)
632 pos = req->misc.read.in.offset - io->offset +
633 req->out.args[0].size;
636 fuse_aio_complete(io, req->out.h.error, pos);
639 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
640 size_t num_bytes, struct fuse_io_priv *io)
642 spin_lock(&io->lock);
643 kref_get(&io->refcnt);
644 io->size += num_bytes;
646 spin_unlock(&io->lock);
649 req->end = fuse_aio_complete_req;
651 __fuse_get_request(req);
652 fuse_request_send_background(fc, req);
657 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
658 loff_t pos, size_t count, fl_owner_t owner)
660 struct file *file = io->file;
661 struct fuse_file *ff = file->private_data;
662 struct fuse_conn *fc = ff->fc;
664 fuse_read_fill(req, file, pos, count, FUSE_READ);
666 struct fuse_read_in *inarg = &req->misc.read.in;
668 inarg->read_flags |= FUSE_READ_LOCKOWNER;
669 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
673 return fuse_async_req_send(fc, req, count, io);
675 fuse_request_send(fc, req);
676 return req->out.args[0].size;
679 static void fuse_read_update_size(struct inode *inode, loff_t size,
682 struct fuse_conn *fc = get_fuse_conn(inode);
683 struct fuse_inode *fi = get_fuse_inode(inode);
685 spin_lock(&fc->lock);
686 if (attr_ver == fi->attr_version && size < inode->i_size &&
687 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
688 fi->attr_version = ++fc->attr_version;
689 i_size_write(inode, size);
691 spin_unlock(&fc->lock);
694 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
697 size_t num_read = req->out.args[0].size;
698 struct fuse_conn *fc = get_fuse_conn(inode);
700 if (fc->writeback_cache) {
702 * A hole in a file. Some data after the hole are in page cache,
703 * but have not reached the client fs yet. So, the hole is not
707 int start_idx = num_read >> PAGE_SHIFT;
708 size_t off = num_read & (PAGE_SIZE - 1);
710 for (i = start_idx; i < req->num_pages; i++) {
711 zero_user_segment(req->pages[i], off, PAGE_SIZE);
715 loff_t pos = page_offset(req->pages[0]) + num_read;
716 fuse_read_update_size(inode, pos, attr_ver);
720 static int fuse_do_readpage(struct file *file, struct page *page)
722 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
723 struct inode *inode = page->mapping->host;
724 struct fuse_conn *fc = get_fuse_conn(inode);
725 struct fuse_req *req;
727 loff_t pos = page_offset(page);
728 size_t count = PAGE_SIZE;
733 * Page writeback can extend beyond the lifetime of the
734 * page-cache page, so make sure we read a properly synced
737 fuse_wait_on_page_writeback(inode, page->index);
739 req = fuse_get_req(fc, 1);
743 attr_ver = fuse_get_attr_version(fc);
745 req->out.page_zeroing = 1;
746 req->out.argpages = 1;
748 req->pages[0] = page;
749 req->page_descs[0].length = count;
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);
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, NULL, iocb->ki_filp, NULL);
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 file *file = io->file;
974 struct fuse_file *ff = file->private_data;
975 struct fuse_conn *fc = ff->fc;
976 struct fuse_write_in *inarg = &req->misc.write.in;
978 fuse_write_fill(req, ff, pos, count);
979 inarg->flags = file->f_flags;
981 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
982 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
986 return fuse_async_req_send(fc, req, count, io);
988 fuse_request_send(fc, req);
989 return req->misc.write.out.size;
992 bool fuse_write_update_size(struct inode *inode, loff_t pos)
994 struct fuse_conn *fc = get_fuse_conn(inode);
995 struct fuse_inode *fi = get_fuse_inode(inode);
998 spin_lock(&fc->lock);
999 fi->attr_version = ++fc->attr_version;
1000 if (pos > inode->i_size) {
1001 i_size_write(inode, pos);
1004 spin_unlock(&fc->lock);
1009 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1010 struct inode *inode, loff_t pos,
1016 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1018 for (i = 0; i < req->num_pages; i++)
1019 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1021 res = fuse_send_write(req, &io, pos, count, NULL);
1023 offset = req->page_descs[0].offset;
1025 for (i = 0; i < req->num_pages; i++) {
1026 struct page *page = req->pages[i];
1028 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1029 SetPageUptodate(page);
1031 if (count > PAGE_SIZE - offset)
1032 count -= PAGE_SIZE - offset;
1044 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1045 struct address_space *mapping,
1046 struct iov_iter *ii, loff_t pos)
1048 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1049 unsigned offset = pos & (PAGE_SIZE - 1);
1053 req->in.argpages = 1;
1054 req->page_descs[0].offset = offset;
1059 pgoff_t index = pos >> PAGE_SHIFT;
1060 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1061 iov_iter_count(ii));
1063 bytes = min_t(size_t, bytes, fc->max_write - count);
1067 if (iov_iter_fault_in_readable(ii, bytes))
1071 page = grab_cache_page_write_begin(mapping, index, 0);
1075 if (mapping_writably_mapped(mapping))
1076 flush_dcache_page(page);
1078 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1079 flush_dcache_page(page);
1081 iov_iter_advance(ii, tmp);
1085 bytes = min(bytes, iov_iter_single_seg_count(ii));
1090 req->pages[req->num_pages] = page;
1091 req->page_descs[req->num_pages].length = tmp;
1097 if (offset == PAGE_SIZE)
1100 if (!fc->big_writes)
1102 } while (iov_iter_count(ii) && count < fc->max_write &&
1103 req->num_pages < req->max_pages && offset == 0);
1105 return count > 0 ? count : err;
1108 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1110 return min_t(unsigned,
1111 ((pos + len - 1) >> PAGE_SHIFT) -
1112 (pos >> PAGE_SHIFT) + 1,
1113 FUSE_MAX_PAGES_PER_REQ);
1116 static ssize_t fuse_perform_write(struct file *file,
1117 struct address_space *mapping,
1118 struct iov_iter *ii, loff_t pos)
1120 struct inode *inode = mapping->host;
1121 struct fuse_conn *fc = get_fuse_conn(inode);
1122 struct fuse_inode *fi = get_fuse_inode(inode);
1126 if (is_bad_inode(inode))
1129 if (inode->i_size < pos + iov_iter_count(ii))
1130 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1133 struct fuse_req *req;
1135 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1137 req = fuse_get_req(fc, nr_pages);
1143 count = fuse_fill_write_pages(req, mapping, ii, pos);
1149 num_written = fuse_send_write_pages(req, file, inode,
1151 err = req->out.h.error;
1156 /* break out of the loop on short write */
1157 if (num_written != count)
1161 fuse_put_request(fc, req);
1162 } while (!err && iov_iter_count(ii));
1165 fuse_write_update_size(inode, pos);
1167 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1168 fuse_invalidate_attr(inode);
1170 return res > 0 ? res : err;
1173 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1175 struct file *file = iocb->ki_filp;
1176 struct address_space *mapping = file->f_mapping;
1177 ssize_t written = 0;
1178 ssize_t written_buffered = 0;
1179 struct inode *inode = mapping->host;
1183 if (get_fuse_conn(inode)->writeback_cache) {
1184 /* Update size (EOF optimization) and mode (SUID clearing) */
1185 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1189 return generic_file_write_iter(iocb, from);
1194 /* We can write back this queue in page reclaim */
1195 current->backing_dev_info = inode_to_bdi(inode);
1197 err = generic_write_checks(iocb, from);
1201 err = file_remove_privs(file);
1205 err = file_update_time(file);
1209 if (iocb->ki_flags & IOCB_DIRECT) {
1210 loff_t pos = iocb->ki_pos;
1211 written = generic_file_direct_write(iocb, from);
1212 if (written < 0 || !iov_iter_count(from))
1217 written_buffered = fuse_perform_write(file, mapping, from, pos);
1218 if (written_buffered < 0) {
1219 err = written_buffered;
1222 endbyte = pos + written_buffered - 1;
1224 err = filemap_write_and_wait_range(file->f_mapping, pos,
1229 invalidate_mapping_pages(file->f_mapping,
1231 endbyte >> PAGE_SHIFT);
1233 written += written_buffered;
1234 iocb->ki_pos = pos + written_buffered;
1236 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
1238 iocb->ki_pos += written;
1241 current->backing_dev_info = NULL;
1242 inode_unlock(inode);
1244 return written ? written : err;
1247 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1248 unsigned index, unsigned nr_pages)
1252 for (i = index; i < index + nr_pages; i++)
1253 req->page_descs[i].length = PAGE_SIZE -
1254 req->page_descs[i].offset;
1257 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1259 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1262 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1265 return min(iov_iter_single_seg_count(ii), max_size);
1268 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1269 size_t *nbytesp, int write)
1271 size_t nbytes = 0; /* # bytes already packed in req */
1274 /* Special case for kernel I/O: can copy directly into the buffer */
1275 if (ii->type & ITER_KVEC) {
1276 unsigned long user_addr = fuse_get_user_addr(ii);
1277 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1280 req->in.args[1].value = (void *) user_addr;
1282 req->out.args[0].value = (void *) user_addr;
1284 iov_iter_advance(ii, frag_size);
1285 *nbytesp = frag_size;
1289 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1292 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1294 req->max_pages - req->num_pages,
1299 iov_iter_advance(ii, ret);
1303 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1305 req->page_descs[req->num_pages].offset = start;
1306 fuse_page_descs_length_init(req, req->num_pages, npages);
1308 req->num_pages += npages;
1309 req->page_descs[req->num_pages - 1].length -=
1310 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1314 req->in.argpages = 1;
1316 req->out.argpages = 1;
1320 return ret < 0 ? ret : 0;
1323 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1325 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1328 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1329 loff_t *ppos, int flags)
1331 int write = flags & FUSE_DIO_WRITE;
1332 bool should_dirty = !write && iter_is_iovec(iter);
1333 int cuse = flags & FUSE_DIO_CUSE;
1334 struct file *file = io->file;
1335 struct inode *inode = file->f_mapping->host;
1336 struct fuse_file *ff = file->private_data;
1337 struct fuse_conn *fc = ff->fc;
1338 size_t nmax = write ? fc->max_write : fc->max_read;
1340 size_t count = iov_iter_count(iter);
1341 pgoff_t idx_from = pos >> PAGE_SHIFT;
1342 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1344 struct fuse_req *req;
1348 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1350 req = fuse_get_req(fc, fuse_iter_npages(iter));
1352 return PTR_ERR(req);
1354 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1357 fuse_sync_writes(inode);
1359 inode_unlock(inode);
1364 fl_owner_t owner = current->files;
1365 size_t nbytes = min(count, nmax);
1366 err = fuse_get_user_pages(req, iter, &nbytes, write);
1371 nres = fuse_send_write(req, io, pos, nbytes, owner);
1373 nres = fuse_send_read(req, io, pos, nbytes, owner);
1376 fuse_release_user_pages(req, should_dirty);
1377 if (req->out.h.error) {
1378 err = req->out.h.error;
1380 } else if (nres > nbytes) {
1391 fuse_put_request(fc, req);
1393 req = fuse_get_req_for_background(fc,
1394 fuse_iter_npages(iter));
1396 req = fuse_get_req(fc, fuse_iter_npages(iter));
1402 fuse_put_request(fc, req);
1406 return res > 0 ? res : err;
1408 EXPORT_SYMBOL_GPL(fuse_direct_io);
1410 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1411 struct iov_iter *iter,
1415 struct file *file = io->file;
1416 struct inode *inode = file_inode(file);
1418 if (is_bad_inode(inode))
1421 res = fuse_direct_io(io, iter, ppos, 0);
1423 fuse_invalidate_attr(inode);
1428 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1430 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
1431 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1434 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1436 struct file *file = iocb->ki_filp;
1437 struct inode *inode = file_inode(file);
1438 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1441 if (is_bad_inode(inode))
1444 /* Don't allow parallel writes to the same file */
1446 res = generic_write_checks(iocb, from);
1448 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1449 fuse_invalidate_attr(inode);
1451 fuse_write_update_size(inode, iocb->ki_pos);
1452 inode_unlock(inode);
1457 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1461 for (i = 0; i < req->num_pages; i++)
1462 __free_page(req->pages[i]);
1465 fuse_file_put(req->ff, false);
1468 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1470 struct inode *inode = req->inode;
1471 struct fuse_inode *fi = get_fuse_inode(inode);
1472 struct backing_dev_info *bdi = inode_to_bdi(inode);
1475 list_del(&req->writepages_entry);
1476 for (i = 0; i < req->num_pages; i++) {
1477 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1478 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1479 wb_writeout_inc(&bdi->wb);
1481 wake_up(&fi->page_waitq);
1484 /* Called under fc->lock, may release and reacquire it */
1485 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1487 __releases(fc->lock)
1488 __acquires(fc->lock)
1490 struct fuse_inode *fi = get_fuse_inode(req->inode);
1491 struct fuse_write_in *inarg = &req->misc.write.in;
1492 __u64 data_size = req->num_pages * PAGE_SIZE;
1497 if (inarg->offset + data_size <= size) {
1498 inarg->size = data_size;
1499 } else if (inarg->offset < size) {
1500 inarg->size = size - inarg->offset;
1502 /* Got truncated off completely */
1506 req->in.args[1].size = inarg->size;
1508 fuse_request_send_background_locked(fc, req);
1512 fuse_writepage_finish(fc, req);
1513 spin_unlock(&fc->lock);
1514 fuse_writepage_free(fc, req);
1515 fuse_put_request(fc, req);
1516 spin_lock(&fc->lock);
1520 * If fi->writectr is positive (no truncate or fsync going on) send
1521 * all queued writepage requests.
1523 * Called with fc->lock
1525 void fuse_flush_writepages(struct inode *inode)
1526 __releases(fc->lock)
1527 __acquires(fc->lock)
1529 struct fuse_conn *fc = get_fuse_conn(inode);
1530 struct fuse_inode *fi = get_fuse_inode(inode);
1531 loff_t crop = i_size_read(inode);
1532 struct fuse_req *req;
1534 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1535 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1536 list_del_init(&req->list);
1537 fuse_send_writepage(fc, req, crop);
1541 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1543 struct inode *inode = req->inode;
1544 struct fuse_inode *fi = get_fuse_inode(inode);
1546 mapping_set_error(inode->i_mapping, req->out.h.error);
1547 spin_lock(&fc->lock);
1548 while (req->misc.write.next) {
1549 struct fuse_conn *fc = get_fuse_conn(inode);
1550 struct fuse_write_in *inarg = &req->misc.write.in;
1551 struct fuse_req *next = req->misc.write.next;
1552 req->misc.write.next = next->misc.write.next;
1553 next->misc.write.next = NULL;
1554 next->ff = fuse_file_get(req->ff);
1555 list_add(&next->writepages_entry, &fi->writepages);
1558 * Skip fuse_flush_writepages() to make it easy to crop requests
1559 * based on primary request size.
1561 * 1st case (trivial): there are no concurrent activities using
1562 * fuse_set/release_nowrite. Then we're on safe side because
1563 * fuse_flush_writepages() would call fuse_send_writepage()
1566 * 2nd case: someone called fuse_set_nowrite and it is waiting
1567 * now for completion of all in-flight requests. This happens
1568 * rarely and no more than once per page, so this should be
1571 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1572 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1573 * that fuse_set_nowrite returned implies that all in-flight
1574 * requests were completed along with all of their secondary
1575 * requests. Further primary requests are blocked by negative
1576 * writectr. Hence there cannot be any in-flight requests and
1577 * no invocations of fuse_writepage_end() while we're in
1578 * fuse_set_nowrite..fuse_release_nowrite section.
1580 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1583 fuse_writepage_finish(fc, req);
1584 spin_unlock(&fc->lock);
1585 fuse_writepage_free(fc, req);
1588 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1589 struct fuse_inode *fi)
1591 struct fuse_file *ff = NULL;
1593 spin_lock(&fc->lock);
1594 if (!list_empty(&fi->write_files)) {
1595 ff = list_entry(fi->write_files.next, struct fuse_file,
1599 spin_unlock(&fc->lock);
1604 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1605 struct fuse_inode *fi)
1607 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1612 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1614 struct fuse_conn *fc = get_fuse_conn(inode);
1615 struct fuse_inode *fi = get_fuse_inode(inode);
1616 struct fuse_file *ff;
1619 ff = __fuse_write_file_get(fc, fi);
1620 err = fuse_flush_times(inode, ff);
1622 fuse_file_put(ff, 0);
1627 static int fuse_writepage_locked(struct page *page)
1629 struct address_space *mapping = page->mapping;
1630 struct inode *inode = mapping->host;
1631 struct fuse_conn *fc = get_fuse_conn(inode);
1632 struct fuse_inode *fi = get_fuse_inode(inode);
1633 struct fuse_req *req;
1634 struct page *tmp_page;
1635 int error = -ENOMEM;
1637 set_page_writeback(page);
1639 req = fuse_request_alloc_nofs(1);
1643 /* writeback always goes to bg_queue */
1644 __set_bit(FR_BACKGROUND, &req->flags);
1645 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1650 req->ff = fuse_write_file_get(fc, fi);
1654 fuse_write_fill(req, req->ff, page_offset(page), 0);
1656 copy_highpage(tmp_page, page);
1657 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1658 req->misc.write.next = NULL;
1659 req->in.argpages = 1;
1661 req->pages[0] = tmp_page;
1662 req->page_descs[0].offset = 0;
1663 req->page_descs[0].length = PAGE_SIZE;
1664 req->end = fuse_writepage_end;
1667 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1668 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1670 spin_lock(&fc->lock);
1671 list_add(&req->writepages_entry, &fi->writepages);
1672 list_add_tail(&req->list, &fi->queued_writes);
1673 fuse_flush_writepages(inode);
1674 spin_unlock(&fc->lock);
1676 end_page_writeback(page);
1681 __free_page(tmp_page);
1683 fuse_request_free(req);
1685 end_page_writeback(page);
1689 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1693 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1695 * ->writepages() should be called for sync() and friends. We
1696 * should only get here on direct reclaim and then we are
1697 * allowed to skip a page which is already in flight
1699 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1701 redirty_page_for_writepage(wbc, page);
1706 err = fuse_writepage_locked(page);
1712 struct fuse_fill_wb_data {
1713 struct fuse_req *req;
1714 struct fuse_file *ff;
1715 struct inode *inode;
1716 struct page **orig_pages;
1719 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1721 struct fuse_req *req = data->req;
1722 struct inode *inode = data->inode;
1723 struct fuse_conn *fc = get_fuse_conn(inode);
1724 struct fuse_inode *fi = get_fuse_inode(inode);
1725 int num_pages = req->num_pages;
1728 req->ff = fuse_file_get(data->ff);
1729 spin_lock(&fc->lock);
1730 list_add_tail(&req->list, &fi->queued_writes);
1731 fuse_flush_writepages(inode);
1732 spin_unlock(&fc->lock);
1734 for (i = 0; i < num_pages; i++)
1735 end_page_writeback(data->orig_pages[i]);
1738 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1741 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1742 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1743 struct fuse_req *tmp;
1744 struct fuse_req *old_req;
1748 BUG_ON(new_req->num_pages != 0);
1750 spin_lock(&fc->lock);
1751 list_del(&new_req->writepages_entry);
1752 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1753 BUG_ON(old_req->inode != new_req->inode);
1754 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1755 if (curr_index <= page->index &&
1756 page->index < curr_index + old_req->num_pages) {
1762 list_add(&new_req->writepages_entry, &fi->writepages);
1766 new_req->num_pages = 1;
1767 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1768 BUG_ON(tmp->inode != new_req->inode);
1769 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1770 if (tmp->num_pages == 1 &&
1771 curr_index == page->index) {
1776 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1777 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1779 copy_highpage(old_req->pages[0], page);
1780 spin_unlock(&fc->lock);
1782 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1783 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1784 wb_writeout_inc(&bdi->wb);
1785 fuse_writepage_free(fc, new_req);
1786 fuse_request_free(new_req);
1789 new_req->misc.write.next = old_req->misc.write.next;
1790 old_req->misc.write.next = new_req;
1793 spin_unlock(&fc->lock);
1798 static int fuse_writepages_fill(struct page *page,
1799 struct writeback_control *wbc, void *_data)
1801 struct fuse_fill_wb_data *data = _data;
1802 struct fuse_req *req = data->req;
1803 struct inode *inode = data->inode;
1804 struct fuse_conn *fc = get_fuse_conn(inode);
1805 struct page *tmp_page;
1811 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1817 * Being under writeback is unlikely but possible. For example direct
1818 * read to an mmaped fuse file will set the page dirty twice; once when
1819 * the pages are faulted with get_user_pages(), and then after the read
1822 is_writeback = fuse_page_is_writeback(inode, page->index);
1824 if (req && req->num_pages &&
1825 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1826 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1827 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1828 fuse_writepages_send(data);
1832 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1837 * The page must not be redirtied until the writeout is completed
1838 * (i.e. userspace has sent a reply to the write request). Otherwise
1839 * there could be more than one temporary page instance for each real
1842 * This is ensured by holding the page lock in page_mkwrite() while
1843 * checking fuse_page_is_writeback(). We already hold the page lock
1844 * since clear_page_dirty_for_io() and keep it held until we add the
1845 * request to the fi->writepages list and increment req->num_pages.
1846 * After this fuse_page_is_writeback() will indicate that the page is
1847 * under writeback, so we can release the page lock.
1849 if (data->req == NULL) {
1850 struct fuse_inode *fi = get_fuse_inode(inode);
1853 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1855 __free_page(tmp_page);
1859 fuse_write_fill(req, data->ff, page_offset(page), 0);
1860 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1861 req->misc.write.next = NULL;
1862 req->in.argpages = 1;
1863 __set_bit(FR_BACKGROUND, &req->flags);
1865 req->end = fuse_writepage_end;
1868 spin_lock(&fc->lock);
1869 list_add(&req->writepages_entry, &fi->writepages);
1870 spin_unlock(&fc->lock);
1874 set_page_writeback(page);
1876 copy_highpage(tmp_page, page);
1877 req->pages[req->num_pages] = tmp_page;
1878 req->page_descs[req->num_pages].offset = 0;
1879 req->page_descs[req->num_pages].length = PAGE_SIZE;
1881 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1882 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1885 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1886 end_page_writeback(page);
1890 data->orig_pages[req->num_pages] = page;
1893 * Protected by fc->lock against concurrent access by
1894 * fuse_page_is_writeback().
1896 spin_lock(&fc->lock);
1898 spin_unlock(&fc->lock);
1906 static int fuse_writepages(struct address_space *mapping,
1907 struct writeback_control *wbc)
1909 struct inode *inode = mapping->host;
1910 struct fuse_fill_wb_data data;
1914 if (is_bad_inode(inode))
1922 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1923 sizeof(struct page *),
1925 if (!data.orig_pages)
1928 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1930 /* Ignore errors if we can write at least one page */
1931 BUG_ON(!data.req->num_pages);
1932 fuse_writepages_send(&data);
1936 fuse_file_put(data.ff, false);
1938 kfree(data.orig_pages);
1944 * It's worthy to make sure that space is reserved on disk for the write,
1945 * but how to implement it without killing performance need more thinking.
1947 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1948 loff_t pos, unsigned len, unsigned flags,
1949 struct page **pagep, void **fsdata)
1951 pgoff_t index = pos >> PAGE_SHIFT;
1952 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1957 WARN_ON(!fc->writeback_cache);
1959 page = grab_cache_page_write_begin(mapping, index, flags);
1963 fuse_wait_on_page_writeback(mapping->host, page->index);
1965 if (PageUptodate(page) || len == PAGE_SIZE)
1968 * Check if the start this page comes after the end of file, in which
1969 * case the readpage can be optimized away.
1971 fsize = i_size_read(mapping->host);
1972 if (fsize <= (pos & PAGE_MASK)) {
1973 size_t off = pos & ~PAGE_MASK;
1975 zero_user_segment(page, 0, off);
1978 err = fuse_do_readpage(file, page);
1992 static int fuse_write_end(struct file *file, struct address_space *mapping,
1993 loff_t pos, unsigned len, unsigned copied,
1994 struct page *page, void *fsdata)
1996 struct inode *inode = page->mapping->host;
1998 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2002 if (!PageUptodate(page)) {
2003 /* Zero any unwritten bytes at the end of the page */
2004 size_t endoff = (pos + copied) & ~PAGE_MASK;
2006 zero_user_segment(page, endoff, PAGE_SIZE);
2007 SetPageUptodate(page);
2010 fuse_write_update_size(inode, pos + copied);
2011 set_page_dirty(page);
2020 static int fuse_launder_page(struct page *page)
2023 if (clear_page_dirty_for_io(page)) {
2024 struct inode *inode = page->mapping->host;
2025 err = fuse_writepage_locked(page);
2027 fuse_wait_on_page_writeback(inode, page->index);
2033 * Write back dirty pages now, because there may not be any suitable
2036 static void fuse_vma_close(struct vm_area_struct *vma)
2038 filemap_write_and_wait(vma->vm_file->f_mapping);
2042 * Wait for writeback against this page to complete before allowing it
2043 * to be marked dirty again, and hence written back again, possibly
2044 * before the previous writepage completed.
2046 * Block here, instead of in ->writepage(), so that the userspace fs
2047 * can only block processes actually operating on the filesystem.
2049 * Otherwise unprivileged userspace fs would be able to block
2054 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2056 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2058 struct page *page = vmf->page;
2059 struct inode *inode = file_inode(vma->vm_file);
2061 file_update_time(vma->vm_file);
2063 if (page->mapping != inode->i_mapping) {
2065 return VM_FAULT_NOPAGE;
2068 fuse_wait_on_page_writeback(inode, page->index);
2069 return VM_FAULT_LOCKED;
2072 static const struct vm_operations_struct fuse_file_vm_ops = {
2073 .close = fuse_vma_close,
2074 .fault = filemap_fault,
2075 .map_pages = filemap_map_pages,
2076 .page_mkwrite = fuse_page_mkwrite,
2079 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2081 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2082 fuse_link_write_file(file);
2084 file_accessed(file);
2085 vma->vm_ops = &fuse_file_vm_ops;
2089 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2091 /* Can't provide the coherency needed for MAP_SHARED */
2092 if (vma->vm_flags & VM_MAYSHARE)
2095 invalidate_inode_pages2(file->f_mapping);
2097 return generic_file_mmap(file, vma);
2100 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2101 struct file_lock *fl)
2103 switch (ffl->type) {
2109 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2110 ffl->end < ffl->start)
2113 fl->fl_start = ffl->start;
2114 fl->fl_end = ffl->end;
2115 fl->fl_pid = ffl->pid;
2121 fl->fl_type = ffl->type;
2125 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2126 const struct file_lock *fl, int opcode, pid_t pid,
2127 int flock, struct fuse_lk_in *inarg)
2129 struct inode *inode = file_inode(file);
2130 struct fuse_conn *fc = get_fuse_conn(inode);
2131 struct fuse_file *ff = file->private_data;
2133 memset(inarg, 0, sizeof(*inarg));
2135 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2136 inarg->lk.start = fl->fl_start;
2137 inarg->lk.end = fl->fl_end;
2138 inarg->lk.type = fl->fl_type;
2139 inarg->lk.pid = pid;
2141 inarg->lk_flags |= FUSE_LK_FLOCK;
2142 args->in.h.opcode = opcode;
2143 args->in.h.nodeid = get_node_id(inode);
2144 args->in.numargs = 1;
2145 args->in.args[0].size = sizeof(*inarg);
2146 args->in.args[0].value = inarg;
2149 static int fuse_getlk(struct file *file, struct file_lock *fl)
2151 struct inode *inode = file_inode(file);
2152 struct fuse_conn *fc = get_fuse_conn(inode);
2154 struct fuse_lk_in inarg;
2155 struct fuse_lk_out outarg;
2158 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2159 args.out.numargs = 1;
2160 args.out.args[0].size = sizeof(outarg);
2161 args.out.args[0].value = &outarg;
2162 err = fuse_simple_request(fc, &args);
2164 err = convert_fuse_file_lock(&outarg.lk, fl);
2169 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2171 struct inode *inode = file_inode(file);
2172 struct fuse_conn *fc = get_fuse_conn(inode);
2174 struct fuse_lk_in inarg;
2175 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2176 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2179 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2180 /* NLM needs asynchronous locks, which we don't support yet */
2184 /* Unlock on close is handled by the flush method */
2185 if (fl->fl_flags & FL_CLOSE)
2188 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2189 err = fuse_simple_request(fc, &args);
2191 /* locking is restartable */
2198 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2200 struct inode *inode = file_inode(file);
2201 struct fuse_conn *fc = get_fuse_conn(inode);
2204 if (cmd == F_CANCELLK) {
2206 } else if (cmd == F_GETLK) {
2208 posix_test_lock(file, fl);
2211 err = fuse_getlk(file, fl);
2214 err = posix_lock_file(file, fl, NULL);
2216 err = fuse_setlk(file, fl, 0);
2221 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2223 struct inode *inode = file_inode(file);
2224 struct fuse_conn *fc = get_fuse_conn(inode);
2228 err = locks_lock_file_wait(file, fl);
2230 struct fuse_file *ff = file->private_data;
2232 /* emulate flock with POSIX locks */
2234 err = fuse_setlk(file, fl, 1);
2240 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2242 struct inode *inode = mapping->host;
2243 struct fuse_conn *fc = get_fuse_conn(inode);
2245 struct fuse_bmap_in inarg;
2246 struct fuse_bmap_out outarg;
2249 if (!inode->i_sb->s_bdev || fc->no_bmap)
2252 memset(&inarg, 0, sizeof(inarg));
2253 inarg.block = block;
2254 inarg.blocksize = inode->i_sb->s_blocksize;
2255 args.in.h.opcode = FUSE_BMAP;
2256 args.in.h.nodeid = get_node_id(inode);
2257 args.in.numargs = 1;
2258 args.in.args[0].size = sizeof(inarg);
2259 args.in.args[0].value = &inarg;
2260 args.out.numargs = 1;
2261 args.out.args[0].size = sizeof(outarg);
2262 args.out.args[0].value = &outarg;
2263 err = fuse_simple_request(fc, &args);
2267 return err ? 0 : outarg.block;
2270 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2272 struct inode *inode = file->f_mapping->host;
2273 struct fuse_conn *fc = get_fuse_conn(inode);
2274 struct fuse_file *ff = file->private_data;
2276 struct fuse_lseek_in inarg = {
2281 struct fuse_lseek_out outarg;
2287 args.in.h.opcode = FUSE_LSEEK;
2288 args.in.h.nodeid = ff->nodeid;
2289 args.in.numargs = 1;
2290 args.in.args[0].size = sizeof(inarg);
2291 args.in.args[0].value = &inarg;
2292 args.out.numargs = 1;
2293 args.out.args[0].size = sizeof(outarg);
2294 args.out.args[0].value = &outarg;
2295 err = fuse_simple_request(fc, &args);
2297 if (err == -ENOSYS) {
2304 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2307 err = fuse_update_attributes(inode, NULL, file, NULL);
2309 return generic_file_llseek(file, offset, whence);
2314 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2317 struct inode *inode = file_inode(file);
2322 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2323 retval = generic_file_llseek(file, offset, whence);
2327 retval = fuse_update_attributes(inode, NULL, file, NULL);
2329 retval = generic_file_llseek(file, offset, whence);
2330 inode_unlock(inode);
2335 retval = fuse_lseek(file, offset, whence);
2336 inode_unlock(inode);
2346 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2347 * ABI was defined to be 'struct iovec' which is different on 32bit
2348 * and 64bit. Fortunately we can determine which structure the server
2349 * used from the size of the reply.
2351 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2352 size_t transferred, unsigned count,
2355 #ifdef CONFIG_COMPAT
2356 if (count * sizeof(struct compat_iovec) == transferred) {
2357 struct compat_iovec *ciov = src;
2361 * With this interface a 32bit server cannot support
2362 * non-compat (i.e. ones coming from 64bit apps) ioctl
2368 for (i = 0; i < count; i++) {
2369 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2370 dst[i].iov_len = ciov[i].iov_len;
2376 if (count * sizeof(struct iovec) != transferred)
2379 memcpy(dst, src, transferred);
2383 /* Make sure iov_length() won't overflow */
2384 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2387 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2389 for (n = 0; n < count; n++, iov++) {
2390 if (iov->iov_len > (size_t) max)
2392 max -= iov->iov_len;
2397 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2398 void *src, size_t transferred, unsigned count,
2402 struct fuse_ioctl_iovec *fiov = src;
2404 if (fc->minor < 16) {
2405 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2409 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2412 for (i = 0; i < count; i++) {
2413 /* Did the server supply an inappropriate value? */
2414 if (fiov[i].base != (unsigned long) fiov[i].base ||
2415 fiov[i].len != (unsigned long) fiov[i].len)
2418 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2419 dst[i].iov_len = (size_t) fiov[i].len;
2421 #ifdef CONFIG_COMPAT
2423 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2424 (compat_size_t) dst[i].iov_len != fiov[i].len))
2434 * For ioctls, there is no generic way to determine how much memory
2435 * needs to be read and/or written. Furthermore, ioctls are allowed
2436 * to dereference the passed pointer, so the parameter requires deep
2437 * copying but FUSE has no idea whatsoever about what to copy in or
2440 * This is solved by allowing FUSE server to retry ioctl with
2441 * necessary in/out iovecs. Let's assume the ioctl implementation
2442 * needs to read in the following structure.
2449 * On the first callout to FUSE server, inarg->in_size and
2450 * inarg->out_size will be NULL; then, the server completes the ioctl
2451 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2452 * the actual iov array to
2454 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2456 * which tells FUSE to copy in the requested area and retry the ioctl.
2457 * On the second round, the server has access to the structure and
2458 * from that it can tell what to look for next, so on the invocation,
2459 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2461 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2462 * { .iov_base = a.buf, .iov_len = a.buflen } }
2464 * FUSE will copy both struct a and the pointed buffer from the
2465 * process doing the ioctl and retry ioctl with both struct a and the
2468 * This time, FUSE server has everything it needs and completes ioctl
2469 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2471 * Copying data out works the same way.
2473 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2474 * automatically initializes in and out iovs by decoding @cmd with
2475 * _IOC_* macros and the server is not allowed to request RETRY. This
2476 * limits ioctl data transfers to well-formed ioctls and is the forced
2477 * behavior for all FUSE servers.
2479 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2482 struct fuse_file *ff = file->private_data;
2483 struct fuse_conn *fc = ff->fc;
2484 struct fuse_ioctl_in inarg = {
2490 struct fuse_ioctl_out outarg;
2491 struct fuse_req *req = NULL;
2492 struct page **pages = NULL;
2493 struct iovec *iov_page = NULL;
2494 struct iovec *in_iov = NULL, *out_iov = NULL;
2495 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2496 size_t in_size, out_size, transferred, c;
2500 #if BITS_PER_LONG == 32
2501 inarg.flags |= FUSE_IOCTL_32BIT;
2503 if (flags & FUSE_IOCTL_COMPAT)
2504 inarg.flags |= FUSE_IOCTL_32BIT;
2507 /* assume all the iovs returned by client always fits in a page */
2508 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2511 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2512 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2513 if (!pages || !iov_page)
2517 * If restricted, initialize IO parameters as encoded in @cmd.
2518 * RETRY from server is not allowed.
2520 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2521 struct iovec *iov = iov_page;
2523 iov->iov_base = (void __user *)arg;
2526 case FS_IOC_GETFLAGS:
2527 case FS_IOC_SETFLAGS:
2528 iov->iov_len = sizeof(int);
2531 iov->iov_len = _IOC_SIZE(cmd);
2535 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2540 if (_IOC_DIR(cmd) & _IOC_READ) {
2547 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2548 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2551 * Out data can be used either for actual out data or iovs,
2552 * make sure there always is at least one page.
2554 out_size = max_t(size_t, out_size, PAGE_SIZE);
2555 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2557 /* make sure there are enough buffer pages and init request with them */
2559 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2561 while (num_pages < max_pages) {
2562 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2563 if (!pages[num_pages])
2568 req = fuse_get_req(fc, num_pages);
2574 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2575 req->num_pages = num_pages;
2576 fuse_page_descs_length_init(req, 0, req->num_pages);
2578 /* okay, let's send it to the client */
2579 req->in.h.opcode = FUSE_IOCTL;
2580 req->in.h.nodeid = ff->nodeid;
2581 req->in.numargs = 1;
2582 req->in.args[0].size = sizeof(inarg);
2583 req->in.args[0].value = &inarg;
2586 req->in.args[1].size = in_size;
2587 req->in.argpages = 1;
2590 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2591 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2592 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2593 if (c != PAGE_SIZE && iov_iter_count(&ii))
2598 req->out.numargs = 2;
2599 req->out.args[0].size = sizeof(outarg);
2600 req->out.args[0].value = &outarg;
2601 req->out.args[1].size = out_size;
2602 req->out.argpages = 1;
2603 req->out.argvar = 1;
2605 fuse_request_send(fc, req);
2606 err = req->out.h.error;
2607 transferred = req->out.args[1].size;
2608 fuse_put_request(fc, req);
2613 /* did it ask for retry? */
2614 if (outarg.flags & FUSE_IOCTL_RETRY) {
2617 /* no retry if in restricted mode */
2619 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2622 in_iovs = outarg.in_iovs;
2623 out_iovs = outarg.out_iovs;
2626 * Make sure things are in boundary, separate checks
2627 * are to protect against overflow.
2630 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2631 out_iovs > FUSE_IOCTL_MAX_IOV ||
2632 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2635 vaddr = kmap_atomic(pages[0]);
2636 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2637 transferred, in_iovs + out_iovs,
2638 (flags & FUSE_IOCTL_COMPAT) != 0);
2639 kunmap_atomic(vaddr);
2644 out_iov = in_iov + in_iovs;
2646 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2650 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2658 if (transferred > inarg.out_size)
2662 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2663 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2664 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2665 if (c != PAGE_SIZE && iov_iter_count(&ii))
2671 fuse_put_request(fc, req);
2672 free_page((unsigned long) iov_page);
2674 __free_page(pages[--num_pages]);
2677 return err ? err : outarg.result;
2679 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2681 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2682 unsigned long arg, unsigned int flags)
2684 struct inode *inode = file_inode(file);
2685 struct fuse_conn *fc = get_fuse_conn(inode);
2687 if (!fuse_allow_current_process(fc))
2690 if (is_bad_inode(inode))
2693 return fuse_do_ioctl(file, cmd, arg, flags);
2696 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2699 return fuse_ioctl_common(file, cmd, arg, 0);
2702 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2705 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2709 * All files which have been polled are linked to RB tree
2710 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2711 * find the matching one.
2713 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2714 struct rb_node **parent_out)
2716 struct rb_node **link = &fc->polled_files.rb_node;
2717 struct rb_node *last = NULL;
2720 struct fuse_file *ff;
2723 ff = rb_entry(last, struct fuse_file, polled_node);
2726 link = &last->rb_left;
2727 else if (kh > ff->kh)
2728 link = &last->rb_right;
2739 * The file is about to be polled. Make sure it's on the polled_files
2740 * RB tree. Note that files once added to the polled_files tree are
2741 * not removed before the file is released. This is because a file
2742 * polled once is likely to be polled again.
2744 static void fuse_register_polled_file(struct fuse_conn *fc,
2745 struct fuse_file *ff)
2747 spin_lock(&fc->lock);
2748 if (RB_EMPTY_NODE(&ff->polled_node)) {
2749 struct rb_node **link, *uninitialized_var(parent);
2751 link = fuse_find_polled_node(fc, ff->kh, &parent);
2753 rb_link_node(&ff->polled_node, parent, link);
2754 rb_insert_color(&ff->polled_node, &fc->polled_files);
2756 spin_unlock(&fc->lock);
2759 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2761 struct fuse_file *ff = file->private_data;
2762 struct fuse_conn *fc = ff->fc;
2763 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2764 struct fuse_poll_out outarg;
2769 return DEFAULT_POLLMASK;
2771 poll_wait(file, &ff->poll_wait, wait);
2772 inarg.events = (__u32)poll_requested_events(wait);
2775 * Ask for notification iff there's someone waiting for it.
2776 * The client may ignore the flag and always notify.
2778 if (waitqueue_active(&ff->poll_wait)) {
2779 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2780 fuse_register_polled_file(fc, ff);
2783 args.in.h.opcode = FUSE_POLL;
2784 args.in.h.nodeid = ff->nodeid;
2785 args.in.numargs = 1;
2786 args.in.args[0].size = sizeof(inarg);
2787 args.in.args[0].value = &inarg;
2788 args.out.numargs = 1;
2789 args.out.args[0].size = sizeof(outarg);
2790 args.out.args[0].value = &outarg;
2791 err = fuse_simple_request(fc, &args);
2794 return outarg.revents;
2795 if (err == -ENOSYS) {
2797 return DEFAULT_POLLMASK;
2801 EXPORT_SYMBOL_GPL(fuse_file_poll);
2804 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2805 * wakes up the poll waiters.
2807 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2808 struct fuse_notify_poll_wakeup_out *outarg)
2810 u64 kh = outarg->kh;
2811 struct rb_node **link;
2813 spin_lock(&fc->lock);
2815 link = fuse_find_polled_node(fc, kh, NULL);
2817 struct fuse_file *ff;
2819 ff = rb_entry(*link, struct fuse_file, polled_node);
2820 wake_up_interruptible_sync(&ff->poll_wait);
2823 spin_unlock(&fc->lock);
2827 static void fuse_do_truncate(struct file *file)
2829 struct inode *inode = file->f_mapping->host;
2832 attr.ia_valid = ATTR_SIZE;
2833 attr.ia_size = i_size_read(inode);
2835 attr.ia_file = file;
2836 attr.ia_valid |= ATTR_FILE;
2838 fuse_do_setattr(file_dentry(file), &attr, file);
2841 static inline loff_t fuse_round_up(loff_t off)
2843 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2847 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2849 DECLARE_COMPLETION_ONSTACK(wait);
2851 struct file *file = iocb->ki_filp;
2852 struct fuse_file *ff = file->private_data;
2853 bool async_dio = ff->fc->async_dio;
2855 struct inode *inode;
2857 size_t count = iov_iter_count(iter);
2858 loff_t offset = iocb->ki_pos;
2859 struct fuse_io_priv *io;
2862 inode = file->f_mapping->host;
2863 i_size = i_size_read(inode);
2865 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2868 /* optimization for short read */
2869 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2870 if (offset >= i_size)
2872 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2873 count = iov_iter_count(iter);
2876 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2879 spin_lock_init(&io->lock);
2880 kref_init(&io->refcnt);
2884 io->offset = offset;
2885 io->write = (iov_iter_rw(iter) == WRITE);
2889 * By default, we want to optimize all I/Os with async request
2890 * submission to the client filesystem if supported.
2892 io->async = async_dio;
2894 io->blocking = is_sync_kiocb(iocb);
2897 * We cannot asynchronously extend the size of a file.
2898 * In such case the aio will behave exactly like sync io.
2900 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2901 io->blocking = true;
2903 if (io->async && io->blocking) {
2905 * Additional reference to keep io around after
2906 * calling fuse_aio_complete()
2908 kref_get(&io->refcnt);
2912 if (iov_iter_rw(iter) == WRITE) {
2913 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2914 fuse_invalidate_attr(inode);
2916 ret = __fuse_direct_read(io, iter, &pos);
2920 bool blocking = io->blocking;
2922 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2924 /* we have a non-extending, async request, so return */
2926 return -EIOCBQUEUED;
2928 wait_for_completion(&wait);
2929 ret = fuse_get_res_by_io(io);
2932 kref_put(&io->refcnt, fuse_io_release);
2934 if (iov_iter_rw(iter) == WRITE) {
2936 fuse_write_update_size(inode, pos);
2937 else if (ret < 0 && offset + count > i_size)
2938 fuse_do_truncate(file);
2944 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2947 struct fuse_file *ff = file->private_data;
2948 struct inode *inode = file_inode(file);
2949 struct fuse_inode *fi = get_fuse_inode(inode);
2950 struct fuse_conn *fc = ff->fc;
2952 struct fuse_fallocate_in inarg = {
2959 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2960 (mode & FALLOC_FL_PUNCH_HOLE);
2962 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2965 if (fc->no_fallocate)
2970 if (mode & FALLOC_FL_PUNCH_HOLE) {
2971 loff_t endbyte = offset + length - 1;
2972 err = filemap_write_and_wait_range(inode->i_mapping,
2977 fuse_sync_writes(inode);
2981 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2982 offset + length > i_size_read(inode)) {
2983 err = inode_newsize_ok(inode, offset + length);
2988 if (!(mode & FALLOC_FL_KEEP_SIZE))
2989 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2991 args.in.h.opcode = FUSE_FALLOCATE;
2992 args.in.h.nodeid = ff->nodeid;
2993 args.in.numargs = 1;
2994 args.in.args[0].size = sizeof(inarg);
2995 args.in.args[0].value = &inarg;
2996 err = fuse_simple_request(fc, &args);
2997 if (err == -ENOSYS) {
2998 fc->no_fallocate = 1;
3004 /* we could have extended the file */
3005 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3006 bool changed = fuse_write_update_size(inode, offset + length);
3008 if (changed && fc->writeback_cache)
3009 file_update_time(file);
3012 if (mode & FALLOC_FL_PUNCH_HOLE)
3013 truncate_pagecache_range(inode, offset, offset + length - 1);
3015 fuse_invalidate_attr(inode);
3018 if (!(mode & FALLOC_FL_KEEP_SIZE))
3019 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3022 inode_unlock(inode);
3027 static const struct file_operations fuse_file_operations = {
3028 .llseek = fuse_file_llseek,
3029 .read_iter = fuse_file_read_iter,
3030 .write_iter = fuse_file_write_iter,
3031 .mmap = fuse_file_mmap,
3033 .flush = fuse_flush,
3034 .release = fuse_release,
3035 .fsync = fuse_fsync,
3036 .lock = fuse_file_lock,
3037 .flock = fuse_file_flock,
3038 .splice_read = generic_file_splice_read,
3039 .unlocked_ioctl = fuse_file_ioctl,
3040 .compat_ioctl = fuse_file_compat_ioctl,
3041 .poll = fuse_file_poll,
3042 .fallocate = fuse_file_fallocate,
3045 static const struct file_operations fuse_direct_io_file_operations = {
3046 .llseek = fuse_file_llseek,
3047 .read_iter = fuse_direct_read_iter,
3048 .write_iter = fuse_direct_write_iter,
3049 .mmap = fuse_direct_mmap,
3051 .flush = fuse_flush,
3052 .release = fuse_release,
3053 .fsync = fuse_fsync,
3054 .lock = fuse_file_lock,
3055 .flock = fuse_file_flock,
3056 .unlocked_ioctl = fuse_file_ioctl,
3057 .compat_ioctl = fuse_file_compat_ioctl,
3058 .poll = fuse_file_poll,
3059 .fallocate = fuse_file_fallocate,
3060 /* no splice_read */
3063 static const struct address_space_operations fuse_file_aops = {
3064 .readpage = fuse_readpage,
3065 .writepage = fuse_writepage,
3066 .writepages = fuse_writepages,
3067 .launder_page = fuse_launder_page,
3068 .readpages = fuse_readpages,
3069 .set_page_dirty = __set_page_dirty_nobuffers,
3071 .direct_IO = fuse_direct_IO,
3072 .write_begin = fuse_write_begin,
3073 .write_end = fuse_write_end,
3076 void fuse_init_file_inode(struct inode *inode)
3078 inode->i_fop = &fuse_file_operations;
3079 inode->i_data.a_ops = &fuse_file_aops;