2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
22 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
25 struct fuse_open_in inarg;
28 memset(&inarg, 0, sizeof(inarg));
29 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
30 if (!fc->atomic_o_trunc)
31 inarg.flags &= ~O_TRUNC;
32 args.in.h.opcode = opcode;
33 args.in.h.nodeid = nodeid;
35 args.in.args[0].size = sizeof(inarg);
36 args.in.args[0].value = &inarg;
38 args.out.args[0].size = sizeof(*outargp);
39 args.out.args[0].value = outargp;
41 return fuse_simple_request(fc, &args);
44 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
48 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
53 ff->reserved_req = fuse_request_alloc(0);
54 if (unlikely(!ff->reserved_req)) {
59 INIT_LIST_HEAD(&ff->write_entry);
60 mutex_init(&ff->readdir.lock);
61 refcount_set(&ff->count, 1);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
65 ff->kh = atomic64_inc_return(&fc->khctr);
70 void fuse_file_free(struct fuse_file *ff)
72 fuse_request_free(ff->reserved_req);
73 mutex_destroy(&ff->readdir.lock);
77 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
79 refcount_inc(&ff->count);
83 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85 iput(req->misc.release.inode);
88 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
90 if (refcount_dec_and_test(&ff->count)) {
91 struct fuse_req *req = ff->reserved_req;
93 if (ff->fc->no_open && !isdir) {
95 * Drop the release request when client does not
98 __clear_bit(FR_BACKGROUND, &req->flags);
99 iput(req->misc.release.inode);
100 fuse_put_request(ff->fc, req);
102 __set_bit(FR_FORCE, &req->flags);
103 __clear_bit(FR_BACKGROUND, &req->flags);
104 fuse_request_send(ff->fc, req);
105 iput(req->misc.release.inode);
106 fuse_put_request(ff->fc, req);
108 req->end = fuse_release_end;
109 __set_bit(FR_BACKGROUND, &req->flags);
110 fuse_request_send_background(ff->fc, req);
116 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
119 struct fuse_file *ff;
120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
122 ff = fuse_file_alloc(fc);
127 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
128 if (!fc->no_open || isdir) {
129 struct fuse_open_out outarg;
132 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
135 ff->open_flags = outarg.open_flags;
137 } else if (err != -ENOSYS || isdir) {
146 ff->open_flags &= ~FOPEN_DIRECT_IO;
149 file->private_data = ff;
153 EXPORT_SYMBOL_GPL(fuse_do_open);
155 static void fuse_link_write_file(struct file *file)
157 struct inode *inode = file_inode(file);
158 struct fuse_inode *fi = get_fuse_inode(inode);
159 struct fuse_file *ff = file->private_data;
161 * file may be written through mmap, so chain it onto the
162 * inodes's write_file list
164 spin_lock(&fi->lock);
165 if (list_empty(&ff->write_entry))
166 list_add(&ff->write_entry, &fi->write_files);
167 spin_unlock(&fi->lock);
170 void fuse_finish_open(struct inode *inode, struct file *file)
172 struct fuse_file *ff = file->private_data;
173 struct fuse_conn *fc = get_fuse_conn(inode);
175 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
176 invalidate_inode_pages2(inode->i_mapping);
177 if (ff->open_flags & FOPEN_NONSEEKABLE)
178 nonseekable_open(inode, file);
179 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
180 struct fuse_inode *fi = get_fuse_inode(inode);
182 spin_lock(&fi->lock);
183 fi->attr_version = atomic64_inc_return(&fc->attr_version);
184 i_size_write(inode, 0);
185 spin_unlock(&fi->lock);
186 fuse_invalidate_attr(inode);
187 if (fc->writeback_cache)
188 file_update_time(file);
190 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
191 fuse_link_write_file(file);
194 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
196 struct fuse_conn *fc = get_fuse_conn(inode);
198 bool lock_inode = (file->f_flags & O_TRUNC) &&
199 fc->atomic_o_trunc &&
202 err = generic_file_open(inode, file);
209 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
212 fuse_finish_open(inode, file);
220 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
221 int flags, int opcode)
223 struct fuse_conn *fc = ff->fc;
224 struct fuse_req *req = ff->reserved_req;
225 struct fuse_release_in *inarg = &req->misc.release.in;
227 /* Inode is NULL on error path of fuse_create_open() */
229 spin_lock(&fi->lock);
230 list_del(&ff->write_entry);
231 spin_unlock(&fi->lock);
233 spin_lock(&fc->lock);
234 if (!RB_EMPTY_NODE(&ff->polled_node))
235 rb_erase(&ff->polled_node, &fc->polled_files);
236 spin_unlock(&fc->lock);
238 wake_up_interruptible_all(&ff->poll_wait);
241 inarg->flags = flags;
242 req->in.h.opcode = opcode;
243 req->in.h.nodeid = ff->nodeid;
245 req->in.args[0].size = sizeof(struct fuse_release_in);
246 req->in.args[0].value = inarg;
249 void fuse_release_common(struct file *file, bool isdir)
251 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
252 struct fuse_file *ff = file->private_data;
253 struct fuse_req *req = ff->reserved_req;
254 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
256 fuse_prepare_release(fi, ff, file->f_flags, opcode);
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
276 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
279 static int fuse_open(struct inode *inode, struct file *file)
281 return fuse_open_common(inode, file, false);
284 static int fuse_release(struct inode *inode, struct file *file)
286 struct fuse_conn *fc = get_fuse_conn(inode);
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
290 write_inode_now(inode, 1);
292 fuse_release_common(file, false);
294 /* return value is ignored by VFS */
298 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
300 WARN_ON(refcount_read(&ff->count) > 1);
301 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
303 * iput(NULL) is a no-op and since the refcount is 1 and everything's
304 * synchronous, we are fine with not doing igrab() here"
306 fuse_file_put(ff, true, false);
308 EXPORT_SYMBOL_GPL(fuse_sync_release);
311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
314 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
329 return (u64) v0 + ((u64) v1 << 32);
332 static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
333 pgoff_t idx_from, pgoff_t idx_to)
335 struct fuse_req *req;
337 list_for_each_entry(req, &fi->writepages, writepages_entry) {
340 WARN_ON(get_fuse_inode(req->inode) != fi);
341 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
342 if (idx_from < curr_index + req->num_pages &&
343 curr_index <= idx_to) {
351 * Check if any page in a range is under writeback
353 * This is currently done by walking the list of writepage requests
354 * for the inode, which can be pretty inefficient.
356 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
359 struct fuse_inode *fi = get_fuse_inode(inode);
362 spin_lock(&fi->lock);
363 found = fuse_find_writeback(fi, idx_from, idx_to);
364 spin_unlock(&fi->lock);
369 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
371 return fuse_range_is_writeback(inode, index, index);
375 * Wait for page writeback to be completed.
377 * Since fuse doesn't rely on the VM writeback tracking, this has to
378 * use some other means.
380 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
382 struct fuse_inode *fi = get_fuse_inode(inode);
384 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
389 * Wait for all pending writepages on the inode to finish.
391 * This is currently done by blocking further writes with FUSE_NOWRITE
392 * and waiting for all sent writes to complete.
394 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
395 * could conflict with truncation.
397 static void fuse_sync_writes(struct inode *inode)
399 fuse_set_nowrite(inode);
400 fuse_release_nowrite(inode);
403 static int fuse_flush(struct file *file, fl_owner_t id)
405 struct inode *inode = file_inode(file);
406 struct fuse_conn *fc = get_fuse_conn(inode);
407 struct fuse_file *ff = file->private_data;
408 struct fuse_req *req;
409 struct fuse_flush_in inarg;
412 if (is_bad_inode(inode))
418 err = write_inode_now(inode, 1);
423 fuse_sync_writes(inode);
426 err = filemap_check_errors(file->f_mapping);
430 req = fuse_get_req_nofail_nopages(fc, file);
431 memset(&inarg, 0, sizeof(inarg));
433 inarg.lock_owner = fuse_lock_owner_id(fc, id);
434 req->in.h.opcode = FUSE_FLUSH;
435 req->in.h.nodeid = get_node_id(inode);
437 req->in.args[0].size = sizeof(inarg);
438 req->in.args[0].value = &inarg;
439 __set_bit(FR_FORCE, &req->flags);
440 fuse_request_send(fc, req);
441 err = req->out.h.error;
442 fuse_put_request(fc, req);
443 if (err == -ENOSYS) {
450 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
451 int datasync, int opcode)
453 struct inode *inode = file->f_mapping->host;
454 struct fuse_conn *fc = get_fuse_conn(inode);
455 struct fuse_file *ff = file->private_data;
457 struct fuse_fsync_in inarg;
459 memset(&inarg, 0, sizeof(inarg));
461 inarg.fsync_flags = datasync ? 1 : 0;
462 args.in.h.opcode = opcode;
463 args.in.h.nodeid = get_node_id(inode);
465 args.in.args[0].size = sizeof(inarg);
466 args.in.args[0].value = &inarg;
467 return fuse_simple_request(fc, &args);
470 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
473 struct inode *inode = file->f_mapping->host;
474 struct fuse_conn *fc = get_fuse_conn(inode);
477 if (is_bad_inode(inode))
483 * Start writeback against all dirty pages of the inode, then
484 * wait for all outstanding writes, before sending the FSYNC
487 err = file_write_and_wait_range(file, start, end);
491 fuse_sync_writes(inode);
494 * Due to implementation of fuse writeback
495 * file_write_and_wait_range() does not catch errors.
496 * We have to do this directly after fuse_sync_writes()
498 err = file_check_and_advance_wb_err(file);
502 err = sync_inode_metadata(inode, 1);
509 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
510 if (err == -ENOSYS) {
520 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
521 size_t count, int opcode)
523 struct fuse_read_in *inarg = &req->misc.read.in;
524 struct fuse_file *ff = file->private_data;
529 inarg->flags = file->f_flags;
530 req->in.h.opcode = opcode;
531 req->in.h.nodeid = ff->nodeid;
533 req->in.args[0].size = sizeof(struct fuse_read_in);
534 req->in.args[0].value = inarg;
536 req->out.numargs = 1;
537 req->out.args[0].size = count;
540 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
544 for (i = 0; i < req->num_pages; i++) {
545 struct page *page = req->pages[i];
547 set_page_dirty_lock(page);
552 static void fuse_io_release(struct kref *kref)
554 kfree(container_of(kref, struct fuse_io_priv, refcnt));
557 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
562 if (io->bytes >= 0 && io->write)
565 return io->bytes < 0 ? io->size : io->bytes;
569 * In case of short read, the caller sets 'pos' to the position of
570 * actual end of fuse request in IO request. Otherwise, if bytes_requested
571 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
574 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
575 * both submitted asynchronously. The first of them was ACKed by userspace as
576 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
577 * second request was ACKed as short, e.g. only 1K was read, resulting in
580 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
581 * will be equal to the length of the longest contiguous fragment of
582 * transferred data starting from the beginning of IO request.
584 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
588 spin_lock(&io->lock);
590 io->err = io->err ? : err;
591 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
595 if (!left && io->blocking)
597 spin_unlock(&io->lock);
599 if (!left && !io->blocking) {
600 ssize_t res = fuse_get_res_by_io(io);
603 struct inode *inode = file_inode(io->iocb->ki_filp);
604 struct fuse_conn *fc = get_fuse_conn(inode);
605 struct fuse_inode *fi = get_fuse_inode(inode);
607 spin_lock(&fi->lock);
608 fi->attr_version = atomic64_inc_return(&fc->attr_version);
609 spin_unlock(&fi->lock);
612 io->iocb->ki_complete(io->iocb, res, 0);
615 kref_put(&io->refcnt, fuse_io_release);
618 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
620 struct fuse_io_priv *io = req->io;
623 fuse_release_user_pages(req, io->should_dirty);
626 if (req->misc.write.in.size != req->misc.write.out.size)
627 pos = req->misc.write.in.offset - io->offset +
628 req->misc.write.out.size;
630 if (req->misc.read.in.size != req->out.args[0].size)
631 pos = req->misc.read.in.offset - io->offset +
632 req->out.args[0].size;
635 fuse_aio_complete(io, req->out.h.error, pos);
638 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
639 size_t num_bytes, struct fuse_io_priv *io)
641 spin_lock(&io->lock);
642 kref_get(&io->refcnt);
643 io->size += num_bytes;
645 spin_unlock(&io->lock);
648 req->end = fuse_aio_complete_req;
650 __fuse_get_request(req);
651 fuse_request_send_background(fc, req);
656 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
657 loff_t pos, size_t count, fl_owner_t owner)
659 struct file *file = io->iocb->ki_filp;
660 struct fuse_file *ff = file->private_data;
661 struct fuse_conn *fc = ff->fc;
663 fuse_read_fill(req, file, pos, count, FUSE_READ);
665 struct fuse_read_in *inarg = &req->misc.read.in;
667 inarg->read_flags |= FUSE_READ_LOCKOWNER;
668 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
672 return fuse_async_req_send(fc, req, count, io);
674 fuse_request_send(fc, req);
675 return req->out.args[0].size;
678 static void fuse_read_update_size(struct inode *inode, loff_t size,
681 struct fuse_conn *fc = get_fuse_conn(inode);
682 struct fuse_inode *fi = get_fuse_inode(inode);
684 spin_lock(&fi->lock);
685 if (attr_ver == fi->attr_version && size < inode->i_size &&
686 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
687 fi->attr_version = atomic64_inc_return(&fc->attr_version);
688 i_size_write(inode, size);
690 spin_unlock(&fi->lock);
693 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
696 size_t num_read = req->out.args[0].size;
697 struct fuse_conn *fc = get_fuse_conn(inode);
699 if (fc->writeback_cache) {
701 * A hole in a file. Some data after the hole are in page cache,
702 * but have not reached the client fs yet. So, the hole is not
706 int start_idx = num_read >> PAGE_SHIFT;
707 size_t off = num_read & (PAGE_SIZE - 1);
709 for (i = start_idx; i < req->num_pages; i++) {
710 zero_user_segment(req->pages[i], off, PAGE_SIZE);
714 loff_t pos = page_offset(req->pages[0]) + num_read;
715 fuse_read_update_size(inode, pos, attr_ver);
719 static int fuse_do_readpage(struct file *file, struct page *page)
722 struct fuse_io_priv io;
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 init_sync_kiocb(&iocb, file);
751 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
752 num_read = fuse_send_read(req, &io, pos, count, NULL);
753 err = req->out.h.error;
757 * Short read means EOF. If file size is larger, truncate it
759 if (num_read < count)
760 fuse_short_read(req, inode, attr_ver);
762 SetPageUptodate(page);
765 fuse_put_request(fc, req);
770 static int fuse_readpage(struct file *file, struct page *page)
772 struct inode *inode = page->mapping->host;
776 if (is_bad_inode(inode))
779 err = fuse_do_readpage(file, page);
780 fuse_invalidate_atime(inode);
786 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
789 size_t count = req->misc.read.in.size;
790 size_t num_read = req->out.args[0].size;
791 struct address_space *mapping = NULL;
793 for (i = 0; mapping == NULL && i < req->num_pages; i++)
794 mapping = req->pages[i]->mapping;
797 struct inode *inode = mapping->host;
800 * Short read means EOF. If file size is larger, truncate it
802 if (!req->out.h.error && num_read < count)
803 fuse_short_read(req, inode, req->misc.read.attr_ver);
805 fuse_invalidate_atime(inode);
808 for (i = 0; i < req->num_pages; i++) {
809 struct page *page = req->pages[i];
810 if (!req->out.h.error)
811 SetPageUptodate(page);
818 fuse_file_put(req->ff, false, false);
821 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
823 struct fuse_file *ff = file->private_data;
824 struct fuse_conn *fc = ff->fc;
825 loff_t pos = page_offset(req->pages[0]);
826 size_t count = req->num_pages << PAGE_SHIFT;
828 req->out.argpages = 1;
829 req->out.page_zeroing = 1;
830 req->out.page_replace = 1;
831 fuse_read_fill(req, file, pos, count, FUSE_READ);
832 req->misc.read.attr_ver = fuse_get_attr_version(fc);
833 if (fc->async_read) {
834 req->ff = fuse_file_get(ff);
835 req->end = fuse_readpages_end;
836 fuse_request_send_background(fc, req);
838 fuse_request_send(fc, req);
839 fuse_readpages_end(fc, req);
840 fuse_put_request(fc, req);
844 struct fuse_fill_data {
845 struct fuse_req *req;
851 static int fuse_readpages_fill(void *_data, struct page *page)
853 struct fuse_fill_data *data = _data;
854 struct fuse_req *req = data->req;
855 struct inode *inode = data->inode;
856 struct fuse_conn *fc = get_fuse_conn(inode);
858 fuse_wait_on_page_writeback(inode, page->index);
860 if (req->num_pages &&
861 (req->num_pages == fc->max_pages ||
862 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
863 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
864 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
866 fuse_send_readpages(req, data->file);
868 req = fuse_get_req_for_background(fc, nr_alloc);
870 req = fuse_get_req(fc, nr_alloc);
879 if (WARN_ON(req->num_pages >= req->max_pages)) {
881 fuse_put_request(fc, req);
886 req->pages[req->num_pages] = page;
887 req->page_descs[req->num_pages].length = PAGE_SIZE;
893 static int fuse_readpages(struct file *file, struct address_space *mapping,
894 struct list_head *pages, unsigned nr_pages)
896 struct inode *inode = mapping->host;
897 struct fuse_conn *fc = get_fuse_conn(inode);
898 struct fuse_fill_data data;
900 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
903 if (is_bad_inode(inode))
909 data.req = fuse_get_req_for_background(fc, nr_alloc);
911 data.req = fuse_get_req(fc, nr_alloc);
912 data.nr_pages = nr_pages;
913 err = PTR_ERR(data.req);
914 if (IS_ERR(data.req))
917 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
919 if (data.req->num_pages)
920 fuse_send_readpages(data.req, file);
922 fuse_put_request(fc, data.req);
928 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
930 struct inode *inode = iocb->ki_filp->f_mapping->host;
931 struct fuse_conn *fc = get_fuse_conn(inode);
934 * In auto invalidate mode, always update attributes on read.
935 * Otherwise, only update if we attempt to read past EOF (to ensure
936 * i_size is up to date).
938 if (fc->auto_inval_data ||
939 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
941 err = fuse_update_attributes(inode, iocb->ki_filp);
946 return generic_file_read_iter(iocb, to);
949 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
950 loff_t pos, size_t count)
952 struct fuse_write_in *inarg = &req->misc.write.in;
953 struct fuse_write_out *outarg = &req->misc.write.out;
958 req->in.h.opcode = FUSE_WRITE;
959 req->in.h.nodeid = ff->nodeid;
961 if (ff->fc->minor < 9)
962 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
964 req->in.args[0].size = sizeof(struct fuse_write_in);
965 req->in.args[0].value = inarg;
966 req->in.args[1].size = count;
967 req->out.numargs = 1;
968 req->out.args[0].size = sizeof(struct fuse_write_out);
969 req->out.args[0].value = outarg;
972 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
973 loff_t pos, size_t count, fl_owner_t owner)
975 struct kiocb *iocb = io->iocb;
976 struct file *file = iocb->ki_filp;
977 struct fuse_file *ff = file->private_data;
978 struct fuse_conn *fc = ff->fc;
979 struct fuse_write_in *inarg = &req->misc.write.in;
981 fuse_write_fill(req, ff, pos, count);
982 inarg->flags = file->f_flags;
983 if (iocb->ki_flags & IOCB_DSYNC)
984 inarg->flags |= O_DSYNC;
985 if (iocb->ki_flags & IOCB_SYNC)
986 inarg->flags |= O_SYNC;
988 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
989 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
993 return fuse_async_req_send(fc, req, count, io);
995 fuse_request_send(fc, req);
996 return req->misc.write.out.size;
999 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1001 struct fuse_conn *fc = get_fuse_conn(inode);
1002 struct fuse_inode *fi = get_fuse_inode(inode);
1005 spin_lock(&fi->lock);
1006 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1007 if (pos > inode->i_size) {
1008 i_size_write(inode, pos);
1011 spin_unlock(&fi->lock);
1016 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1017 struct inode *inode, loff_t pos,
1023 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1025 for (i = 0; i < req->num_pages; i++)
1026 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1028 res = fuse_send_write(req, &io, pos, count, NULL);
1030 offset = req->page_descs[0].offset;
1032 for (i = 0; i < req->num_pages; i++) {
1033 struct page *page = req->pages[i];
1035 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1036 SetPageUptodate(page);
1038 if (count > PAGE_SIZE - offset)
1039 count -= PAGE_SIZE - offset;
1051 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1052 struct address_space *mapping,
1053 struct iov_iter *ii, loff_t pos)
1055 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1056 unsigned offset = pos & (PAGE_SIZE - 1);
1060 req->in.argpages = 1;
1061 req->page_descs[0].offset = offset;
1066 pgoff_t index = pos >> PAGE_SHIFT;
1067 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1068 iov_iter_count(ii));
1070 bytes = min_t(size_t, bytes, fc->max_write - count);
1074 if (iov_iter_fault_in_readable(ii, bytes))
1078 page = grab_cache_page_write_begin(mapping, index, 0);
1082 if (mapping_writably_mapped(mapping))
1083 flush_dcache_page(page);
1085 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1086 flush_dcache_page(page);
1088 iov_iter_advance(ii, tmp);
1092 bytes = min(bytes, iov_iter_single_seg_count(ii));
1097 req->pages[req->num_pages] = page;
1098 req->page_descs[req->num_pages].length = tmp;
1104 if (offset == PAGE_SIZE)
1107 if (!fc->big_writes)
1109 } while (iov_iter_count(ii) && count < fc->max_write &&
1110 req->num_pages < req->max_pages && offset == 0);
1112 return count > 0 ? count : err;
1115 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1116 unsigned int max_pages)
1118 return min_t(unsigned int,
1119 ((pos + len - 1) >> PAGE_SHIFT) -
1120 (pos >> PAGE_SHIFT) + 1,
1124 static ssize_t fuse_perform_write(struct kiocb *iocb,
1125 struct address_space *mapping,
1126 struct iov_iter *ii, loff_t pos)
1128 struct inode *inode = mapping->host;
1129 struct fuse_conn *fc = get_fuse_conn(inode);
1130 struct fuse_inode *fi = get_fuse_inode(inode);
1134 if (is_bad_inode(inode))
1137 if (inode->i_size < pos + iov_iter_count(ii))
1138 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1141 struct fuse_req *req;
1143 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1146 req = fuse_get_req(fc, nr_pages);
1152 count = fuse_fill_write_pages(req, mapping, ii, pos);
1158 num_written = fuse_send_write_pages(req, iocb, inode,
1160 err = req->out.h.error;
1165 /* break out of the loop on short write */
1166 if (num_written != count)
1170 fuse_put_request(fc, req);
1171 } while (!err && iov_iter_count(ii));
1174 fuse_write_update_size(inode, pos);
1176 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1177 fuse_invalidate_attr(inode);
1179 return res > 0 ? res : err;
1182 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1184 struct file *file = iocb->ki_filp;
1185 struct address_space *mapping = file->f_mapping;
1186 ssize_t written = 0;
1187 ssize_t written_buffered = 0;
1188 struct inode *inode = mapping->host;
1192 if (get_fuse_conn(inode)->writeback_cache) {
1193 /* Update size (EOF optimization) and mode (SUID clearing) */
1194 err = fuse_update_attributes(mapping->host, file);
1198 return generic_file_write_iter(iocb, from);
1203 /* We can write back this queue in page reclaim */
1204 current->backing_dev_info = inode_to_bdi(inode);
1206 err = generic_write_checks(iocb, from);
1210 err = file_remove_privs(file);
1214 err = file_update_time(file);
1218 if (iocb->ki_flags & IOCB_DIRECT) {
1219 loff_t pos = iocb->ki_pos;
1220 written = generic_file_direct_write(iocb, from);
1221 if (written < 0 || !iov_iter_count(from))
1226 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1227 if (written_buffered < 0) {
1228 err = written_buffered;
1231 endbyte = pos + written_buffered - 1;
1233 err = filemap_write_and_wait_range(file->f_mapping, pos,
1238 invalidate_mapping_pages(file->f_mapping,
1240 endbyte >> PAGE_SHIFT);
1242 written += written_buffered;
1243 iocb->ki_pos = pos + written_buffered;
1245 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1247 iocb->ki_pos += written;
1250 current->backing_dev_info = NULL;
1251 inode_unlock(inode);
1253 written = generic_write_sync(iocb, written);
1255 return written ? written : err;
1258 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1259 unsigned index, unsigned nr_pages)
1263 for (i = index; i < index + nr_pages; i++)
1264 req->page_descs[i].length = PAGE_SIZE -
1265 req->page_descs[i].offset;
1268 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1270 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1273 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1276 return min(iov_iter_single_seg_count(ii), max_size);
1279 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1280 size_t *nbytesp, int write)
1282 size_t nbytes = 0; /* # bytes already packed in req */
1285 /* Special case for kernel I/O: can copy directly into the buffer */
1286 if (iov_iter_is_kvec(ii)) {
1287 unsigned long user_addr = fuse_get_user_addr(ii);
1288 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1291 req->in.args[1].value = (void *) user_addr;
1293 req->out.args[0].value = (void *) user_addr;
1295 iov_iter_advance(ii, frag_size);
1296 *nbytesp = frag_size;
1300 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1303 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1305 req->max_pages - req->num_pages,
1310 iov_iter_advance(ii, ret);
1314 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1316 req->page_descs[req->num_pages].offset = start;
1317 fuse_page_descs_length_init(req, req->num_pages, npages);
1319 req->num_pages += npages;
1320 req->page_descs[req->num_pages - 1].length -=
1321 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1325 req->in.argpages = 1;
1327 req->out.argpages = 1;
1331 return ret < 0 ? ret : 0;
1334 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1335 loff_t *ppos, int flags)
1337 int write = flags & FUSE_DIO_WRITE;
1338 int cuse = flags & FUSE_DIO_CUSE;
1339 struct file *file = io->iocb->ki_filp;
1340 struct inode *inode = file->f_mapping->host;
1341 struct fuse_file *ff = file->private_data;
1342 struct fuse_conn *fc = ff->fc;
1343 size_t nmax = write ? fc->max_write : fc->max_read;
1345 size_t count = iov_iter_count(iter);
1346 pgoff_t idx_from = pos >> PAGE_SHIFT;
1347 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1349 struct fuse_req *req;
1353 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1356 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
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 iov_iter_npages(iter, fc->max_pages));
1403 req = fuse_get_req(fc, iov_iter_npages(iter,
1410 fuse_put_request(fc, req);
1414 return res > 0 ? res : err;
1416 EXPORT_SYMBOL_GPL(fuse_direct_io);
1418 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1419 struct iov_iter *iter,
1423 struct inode *inode = file_inode(io->iocb->ki_filp);
1425 if (is_bad_inode(inode))
1428 res = fuse_direct_io(io, iter, ppos, 0);
1430 fuse_invalidate_atime(inode);
1435 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1437 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1441 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1442 struct file *file = iocb->ki_filp;
1444 if (is_bad_inode(file_inode(file)))
1447 res = fuse_direct_IO(iocb, to);
1449 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1451 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1457 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1459 struct inode *inode = file_inode(iocb->ki_filp);
1460 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1463 if (is_bad_inode(inode))
1466 /* Don't allow parallel writes to the same file */
1468 res = generic_write_checks(iocb, from);
1470 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1471 res = fuse_direct_IO(iocb, from);
1473 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1477 fuse_invalidate_attr(inode);
1479 fuse_write_update_size(inode, iocb->ki_pos);
1480 inode_unlock(inode);
1485 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1487 struct fuse_file *ff = iocb->ki_filp->private_data;
1489 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1490 return fuse_cache_read_iter(iocb, to);
1492 return fuse_direct_read_iter(iocb, to);
1495 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1497 struct fuse_file *ff = iocb->ki_filp->private_data;
1499 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1500 return fuse_cache_write_iter(iocb, from);
1502 return fuse_direct_write_iter(iocb, from);
1505 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1509 for (i = 0; i < req->num_pages; i++)
1510 __free_page(req->pages[i]);
1513 fuse_file_put(req->ff, false, false);
1516 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1518 struct inode *inode = req->inode;
1519 struct fuse_inode *fi = get_fuse_inode(inode);
1520 struct backing_dev_info *bdi = inode_to_bdi(inode);
1523 list_del(&req->writepages_entry);
1524 for (i = 0; i < req->num_pages; i++) {
1525 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1526 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1527 wb_writeout_inc(&bdi->wb);
1529 wake_up(&fi->page_waitq);
1532 /* Called under fi->lock, may release and reacquire it */
1533 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1535 __releases(fi->lock)
1536 __acquires(fi->lock)
1538 struct fuse_req *aux, *next;
1539 struct fuse_inode *fi = get_fuse_inode(req->inode);
1540 struct fuse_write_in *inarg = &req->misc.write.in;
1541 __u64 data_size = req->num_pages * PAGE_SIZE;
1544 if (inarg->offset + data_size <= size) {
1545 inarg->size = data_size;
1546 } else if (inarg->offset < size) {
1547 inarg->size = size - inarg->offset;
1549 /* Got truncated off completely */
1553 req->in.args[1].size = inarg->size;
1554 queued = fuse_request_queue_background(fc, req);
1555 /* Fails on broken connection only */
1556 if (unlikely(!queued))
1563 fuse_writepage_finish(fc, req);
1564 spin_unlock(&fi->lock);
1566 /* After fuse_writepage_finish() aux request list is private */
1567 for (aux = req->misc.write.next; aux; aux = next) {
1568 next = aux->misc.write.next;
1569 aux->misc.write.next = NULL;
1570 fuse_writepage_free(fc, aux);
1571 fuse_put_request(fc, aux);
1574 fuse_writepage_free(fc, req);
1575 fuse_put_request(fc, req);
1576 spin_lock(&fi->lock);
1580 * If fi->writectr is positive (no truncate or fsync going on) send
1581 * all queued writepage requests.
1583 * Called with fi->lock
1585 void fuse_flush_writepages(struct inode *inode)
1586 __releases(fi->lock)
1587 __acquires(fi->lock)
1589 struct fuse_conn *fc = get_fuse_conn(inode);
1590 struct fuse_inode *fi = get_fuse_inode(inode);
1591 size_t crop = i_size_read(inode);
1592 struct fuse_req *req;
1594 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1595 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1596 list_del_init(&req->list);
1597 fuse_send_writepage(fc, req, crop);
1601 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1603 struct inode *inode = req->inode;
1604 struct fuse_inode *fi = get_fuse_inode(inode);
1606 mapping_set_error(inode->i_mapping, req->out.h.error);
1607 spin_lock(&fi->lock);
1608 while (req->misc.write.next) {
1609 struct fuse_conn *fc = get_fuse_conn(inode);
1610 struct fuse_write_in *inarg = &req->misc.write.in;
1611 struct fuse_req *next = req->misc.write.next;
1612 req->misc.write.next = next->misc.write.next;
1613 next->misc.write.next = NULL;
1614 next->ff = fuse_file_get(req->ff);
1615 list_add(&next->writepages_entry, &fi->writepages);
1618 * Skip fuse_flush_writepages() to make it easy to crop requests
1619 * based on primary request size.
1621 * 1st case (trivial): there are no concurrent activities using
1622 * fuse_set/release_nowrite. Then we're on safe side because
1623 * fuse_flush_writepages() would call fuse_send_writepage()
1626 * 2nd case: someone called fuse_set_nowrite and it is waiting
1627 * now for completion of all in-flight requests. This happens
1628 * rarely and no more than once per page, so this should be
1631 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1632 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1633 * that fuse_set_nowrite returned implies that all in-flight
1634 * requests were completed along with all of their secondary
1635 * requests. Further primary requests are blocked by negative
1636 * writectr. Hence there cannot be any in-flight requests and
1637 * no invocations of fuse_writepage_end() while we're in
1638 * fuse_set_nowrite..fuse_release_nowrite section.
1640 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1643 fuse_writepage_finish(fc, req);
1644 spin_unlock(&fi->lock);
1645 fuse_writepage_free(fc, req);
1648 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1649 struct fuse_inode *fi)
1651 struct fuse_file *ff = NULL;
1653 spin_lock(&fi->lock);
1654 if (!list_empty(&fi->write_files)) {
1655 ff = list_entry(fi->write_files.next, struct fuse_file,
1659 spin_unlock(&fi->lock);
1664 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1665 struct fuse_inode *fi)
1667 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1672 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1674 struct fuse_conn *fc = get_fuse_conn(inode);
1675 struct fuse_inode *fi = get_fuse_inode(inode);
1676 struct fuse_file *ff;
1679 ff = __fuse_write_file_get(fc, fi);
1680 err = fuse_flush_times(inode, ff);
1682 fuse_file_put(ff, false, false);
1687 static int fuse_writepage_locked(struct page *page)
1689 struct address_space *mapping = page->mapping;
1690 struct inode *inode = mapping->host;
1691 struct fuse_conn *fc = get_fuse_conn(inode);
1692 struct fuse_inode *fi = get_fuse_inode(inode);
1693 struct fuse_req *req;
1694 struct page *tmp_page;
1695 int error = -ENOMEM;
1697 set_page_writeback(page);
1699 req = fuse_request_alloc_nofs(1);
1703 /* writeback always goes to bg_queue */
1704 __set_bit(FR_BACKGROUND, &req->flags);
1705 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1710 req->ff = fuse_write_file_get(fc, fi);
1714 fuse_write_fill(req, req->ff, page_offset(page), 0);
1716 copy_highpage(tmp_page, page);
1717 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1718 req->misc.write.next = NULL;
1719 req->in.argpages = 1;
1721 req->pages[0] = tmp_page;
1722 req->page_descs[0].offset = 0;
1723 req->page_descs[0].length = PAGE_SIZE;
1724 req->end = fuse_writepage_end;
1727 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1728 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1730 spin_lock(&fi->lock);
1731 list_add(&req->writepages_entry, &fi->writepages);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fi->lock);
1736 end_page_writeback(page);
1741 __free_page(tmp_page);
1743 fuse_request_free(req);
1745 mapping_set_error(page->mapping, error);
1746 end_page_writeback(page);
1750 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1754 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1756 * ->writepages() should be called for sync() and friends. We
1757 * should only get here on direct reclaim and then we are
1758 * allowed to skip a page which is already in flight
1760 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1762 redirty_page_for_writepage(wbc, page);
1766 err = fuse_writepage_locked(page);
1772 struct fuse_fill_wb_data {
1773 struct fuse_req *req;
1774 struct fuse_file *ff;
1775 struct inode *inode;
1776 struct page **orig_pages;
1779 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1781 struct fuse_req *req = data->req;
1782 struct inode *inode = data->inode;
1783 struct fuse_inode *fi = get_fuse_inode(inode);
1784 int num_pages = req->num_pages;
1787 req->ff = fuse_file_get(data->ff);
1788 spin_lock(&fi->lock);
1789 list_add_tail(&req->list, &fi->queued_writes);
1790 fuse_flush_writepages(inode);
1791 spin_unlock(&fi->lock);
1793 for (i = 0; i < num_pages; i++)
1794 end_page_writeback(data->orig_pages[i]);
1798 * First recheck under fi->lock if the offending offset is still under
1799 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1800 * one already added for a page at this offset. If there's none, then insert
1801 * this new request onto the auxiliary list, otherwise reuse the existing one by
1802 * copying the new page contents over to the old temporary page.
1804 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1807 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1808 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1809 struct fuse_req *tmp;
1810 struct fuse_req *old_req;
1812 WARN_ON(new_req->num_pages != 0);
1814 spin_lock(&fi->lock);
1815 list_del(&new_req->writepages_entry);
1816 old_req = fuse_find_writeback(fi, page->index, page->index);
1818 list_add(&new_req->writepages_entry, &fi->writepages);
1819 spin_unlock(&fi->lock);
1823 new_req->num_pages = 1;
1824 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
1827 WARN_ON(tmp->inode != new_req->inode);
1828 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1829 if (curr_index == page->index) {
1830 WARN_ON(tmp->num_pages != 1);
1831 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
1832 swap(tmp->pages[0], new_req->pages[0]);
1838 new_req->misc.write.next = old_req->misc.write.next;
1839 old_req->misc.write.next = new_req;
1842 spin_unlock(&fi->lock);
1845 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
1847 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1848 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1849 wb_writeout_inc(&bdi->wb);
1850 fuse_writepage_free(fc, new_req);
1851 fuse_request_free(new_req);
1857 static int fuse_writepages_fill(struct page *page,
1858 struct writeback_control *wbc, void *_data)
1860 struct fuse_fill_wb_data *data = _data;
1861 struct fuse_req *req = data->req;
1862 struct inode *inode = data->inode;
1863 struct fuse_inode *fi = get_fuse_inode(inode);
1864 struct fuse_conn *fc = get_fuse_conn(inode);
1865 struct page *tmp_page;
1871 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1877 * Being under writeback is unlikely but possible. For example direct
1878 * read to an mmaped fuse file will set the page dirty twice; once when
1879 * the pages are faulted with get_user_pages(), and then after the read
1882 is_writeback = fuse_page_is_writeback(inode, page->index);
1884 if (req && req->num_pages &&
1885 (is_writeback || req->num_pages == fc->max_pages ||
1886 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1887 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1888 fuse_writepages_send(data);
1890 } else if (req && req->num_pages == req->max_pages) {
1891 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1892 fuse_writepages_send(data);
1893 req = data->req = NULL;
1898 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1903 * The page must not be redirtied until the writeout is completed
1904 * (i.e. userspace has sent a reply to the write request). Otherwise
1905 * there could be more than one temporary page instance for each real
1908 * This is ensured by holding the page lock in page_mkwrite() while
1909 * checking fuse_page_is_writeback(). We already hold the page lock
1910 * since clear_page_dirty_for_io() and keep it held until we add the
1911 * request to the fi->writepages list and increment req->num_pages.
1912 * After this fuse_page_is_writeback() will indicate that the page is
1913 * under writeback, so we can release the page lock.
1915 if (data->req == NULL) {
1916 struct fuse_inode *fi = get_fuse_inode(inode);
1919 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
1921 __free_page(tmp_page);
1925 fuse_write_fill(req, data->ff, page_offset(page), 0);
1926 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1927 req->misc.write.next = NULL;
1928 req->in.argpages = 1;
1929 __set_bit(FR_BACKGROUND, &req->flags);
1931 req->end = fuse_writepage_end;
1934 spin_lock(&fi->lock);
1935 list_add(&req->writepages_entry, &fi->writepages);
1936 spin_unlock(&fi->lock);
1940 set_page_writeback(page);
1942 copy_highpage(tmp_page, page);
1943 req->pages[req->num_pages] = tmp_page;
1944 req->page_descs[req->num_pages].offset = 0;
1945 req->page_descs[req->num_pages].length = PAGE_SIZE;
1947 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1948 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1951 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1952 end_page_writeback(page);
1956 data->orig_pages[req->num_pages] = page;
1959 * Protected by fi->lock against concurrent access by
1960 * fuse_page_is_writeback().
1962 spin_lock(&fi->lock);
1964 spin_unlock(&fi->lock);
1972 static int fuse_writepages(struct address_space *mapping,
1973 struct writeback_control *wbc)
1975 struct inode *inode = mapping->host;
1976 struct fuse_conn *fc = get_fuse_conn(inode);
1977 struct fuse_fill_wb_data data;
1981 if (is_bad_inode(inode))
1989 data.orig_pages = kcalloc(fc->max_pages,
1990 sizeof(struct page *),
1992 if (!data.orig_pages)
1995 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1997 /* Ignore errors if we can write at least one page */
1998 BUG_ON(!data.req->num_pages);
1999 fuse_writepages_send(&data);
2003 fuse_file_put(data.ff, false, false);
2005 kfree(data.orig_pages);
2011 * It's worthy to make sure that space is reserved on disk for the write,
2012 * but how to implement it without killing performance need more thinking.
2014 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2015 loff_t pos, unsigned len, unsigned flags,
2016 struct page **pagep, void **fsdata)
2018 pgoff_t index = pos >> PAGE_SHIFT;
2019 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2024 WARN_ON(!fc->writeback_cache);
2026 page = grab_cache_page_write_begin(mapping, index, flags);
2030 fuse_wait_on_page_writeback(mapping->host, page->index);
2032 if (PageUptodate(page) || len == PAGE_SIZE)
2035 * Check if the start this page comes after the end of file, in which
2036 * case the readpage can be optimized away.
2038 fsize = i_size_read(mapping->host);
2039 if (fsize <= (pos & PAGE_MASK)) {
2040 size_t off = pos & ~PAGE_MASK;
2042 zero_user_segment(page, 0, off);
2045 err = fuse_do_readpage(file, page);
2059 static int fuse_write_end(struct file *file, struct address_space *mapping,
2060 loff_t pos, unsigned len, unsigned copied,
2061 struct page *page, void *fsdata)
2063 struct inode *inode = page->mapping->host;
2065 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2069 if (!PageUptodate(page)) {
2070 /* Zero any unwritten bytes at the end of the page */
2071 size_t endoff = (pos + copied) & ~PAGE_MASK;
2073 zero_user_segment(page, endoff, PAGE_SIZE);
2074 SetPageUptodate(page);
2077 fuse_write_update_size(inode, pos + copied);
2078 set_page_dirty(page);
2087 static int fuse_launder_page(struct page *page)
2090 if (clear_page_dirty_for_io(page)) {
2091 struct inode *inode = page->mapping->host;
2092 err = fuse_writepage_locked(page);
2094 fuse_wait_on_page_writeback(inode, page->index);
2100 * Write back dirty pages now, because there may not be any suitable
2103 static void fuse_vma_close(struct vm_area_struct *vma)
2105 filemap_write_and_wait(vma->vm_file->f_mapping);
2109 * Wait for writeback against this page to complete before allowing it
2110 * to be marked dirty again, and hence written back again, possibly
2111 * before the previous writepage completed.
2113 * Block here, instead of in ->writepage(), so that the userspace fs
2114 * can only block processes actually operating on the filesystem.
2116 * Otherwise unprivileged userspace fs would be able to block
2121 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2123 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2125 struct page *page = vmf->page;
2126 struct inode *inode = file_inode(vmf->vma->vm_file);
2128 file_update_time(vmf->vma->vm_file);
2130 if (page->mapping != inode->i_mapping) {
2132 return VM_FAULT_NOPAGE;
2135 fuse_wait_on_page_writeback(inode, page->index);
2136 return VM_FAULT_LOCKED;
2139 static const struct vm_operations_struct fuse_file_vm_ops = {
2140 .close = fuse_vma_close,
2141 .fault = filemap_fault,
2142 .map_pages = filemap_map_pages,
2143 .page_mkwrite = fuse_page_mkwrite,
2146 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2148 struct fuse_file *ff = file->private_data;
2150 if (ff->open_flags & FOPEN_DIRECT_IO) {
2151 /* Can't provide the coherency needed for MAP_SHARED */
2152 if (vma->vm_flags & VM_MAYSHARE)
2155 invalidate_inode_pages2(file->f_mapping);
2157 return generic_file_mmap(file, vma);
2160 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2161 fuse_link_write_file(file);
2163 file_accessed(file);
2164 vma->vm_ops = &fuse_file_vm_ops;
2168 static int convert_fuse_file_lock(struct fuse_conn *fc,
2169 const struct fuse_file_lock *ffl,
2170 struct file_lock *fl)
2172 switch (ffl->type) {
2178 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2179 ffl->end < ffl->start)
2182 fl->fl_start = ffl->start;
2183 fl->fl_end = ffl->end;
2186 * Convert pid into init's pid namespace. The locks API will
2187 * translate it into the caller's pid namespace.
2190 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2197 fl->fl_type = ffl->type;
2201 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2202 const struct file_lock *fl, int opcode, pid_t pid,
2203 int flock, struct fuse_lk_in *inarg)
2205 struct inode *inode = file_inode(file);
2206 struct fuse_conn *fc = get_fuse_conn(inode);
2207 struct fuse_file *ff = file->private_data;
2209 memset(inarg, 0, sizeof(*inarg));
2211 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2212 inarg->lk.start = fl->fl_start;
2213 inarg->lk.end = fl->fl_end;
2214 inarg->lk.type = fl->fl_type;
2215 inarg->lk.pid = pid;
2217 inarg->lk_flags |= FUSE_LK_FLOCK;
2218 args->in.h.opcode = opcode;
2219 args->in.h.nodeid = get_node_id(inode);
2220 args->in.numargs = 1;
2221 args->in.args[0].size = sizeof(*inarg);
2222 args->in.args[0].value = inarg;
2225 static int fuse_getlk(struct file *file, struct file_lock *fl)
2227 struct inode *inode = file_inode(file);
2228 struct fuse_conn *fc = get_fuse_conn(inode);
2230 struct fuse_lk_in inarg;
2231 struct fuse_lk_out outarg;
2234 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2235 args.out.numargs = 1;
2236 args.out.args[0].size = sizeof(outarg);
2237 args.out.args[0].value = &outarg;
2238 err = fuse_simple_request(fc, &args);
2240 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2245 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2247 struct inode *inode = file_inode(file);
2248 struct fuse_conn *fc = get_fuse_conn(inode);
2250 struct fuse_lk_in inarg;
2251 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2252 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2253 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2256 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2257 /* NLM needs asynchronous locks, which we don't support yet */
2261 /* Unlock on close is handled by the flush method */
2262 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2265 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2266 err = fuse_simple_request(fc, &args);
2268 /* locking is restartable */
2275 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2277 struct inode *inode = file_inode(file);
2278 struct fuse_conn *fc = get_fuse_conn(inode);
2281 if (cmd == F_CANCELLK) {
2283 } else if (cmd == F_GETLK) {
2285 posix_test_lock(file, fl);
2288 err = fuse_getlk(file, fl);
2291 err = posix_lock_file(file, fl, NULL);
2293 err = fuse_setlk(file, fl, 0);
2298 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2300 struct inode *inode = file_inode(file);
2301 struct fuse_conn *fc = get_fuse_conn(inode);
2305 err = locks_lock_file_wait(file, fl);
2307 struct fuse_file *ff = file->private_data;
2309 /* emulate flock with POSIX locks */
2311 err = fuse_setlk(file, fl, 1);
2317 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2319 struct inode *inode = mapping->host;
2320 struct fuse_conn *fc = get_fuse_conn(inode);
2322 struct fuse_bmap_in inarg;
2323 struct fuse_bmap_out outarg;
2326 if (!inode->i_sb->s_bdev || fc->no_bmap)
2329 memset(&inarg, 0, sizeof(inarg));
2330 inarg.block = block;
2331 inarg.blocksize = inode->i_sb->s_blocksize;
2332 args.in.h.opcode = FUSE_BMAP;
2333 args.in.h.nodeid = get_node_id(inode);
2334 args.in.numargs = 1;
2335 args.in.args[0].size = sizeof(inarg);
2336 args.in.args[0].value = &inarg;
2337 args.out.numargs = 1;
2338 args.out.args[0].size = sizeof(outarg);
2339 args.out.args[0].value = &outarg;
2340 err = fuse_simple_request(fc, &args);
2344 return err ? 0 : outarg.block;
2347 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2349 struct inode *inode = file->f_mapping->host;
2350 struct fuse_conn *fc = get_fuse_conn(inode);
2351 struct fuse_file *ff = file->private_data;
2353 struct fuse_lseek_in inarg = {
2358 struct fuse_lseek_out outarg;
2364 args.in.h.opcode = FUSE_LSEEK;
2365 args.in.h.nodeid = ff->nodeid;
2366 args.in.numargs = 1;
2367 args.in.args[0].size = sizeof(inarg);
2368 args.in.args[0].value = &inarg;
2369 args.out.numargs = 1;
2370 args.out.args[0].size = sizeof(outarg);
2371 args.out.args[0].value = &outarg;
2372 err = fuse_simple_request(fc, &args);
2374 if (err == -ENOSYS) {
2381 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2384 err = fuse_update_attributes(inode, file);
2386 return generic_file_llseek(file, offset, whence);
2391 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2394 struct inode *inode = file_inode(file);
2399 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2400 retval = generic_file_llseek(file, offset, whence);
2404 retval = fuse_update_attributes(inode, file);
2406 retval = generic_file_llseek(file, offset, whence);
2407 inode_unlock(inode);
2412 retval = fuse_lseek(file, offset, whence);
2413 inode_unlock(inode);
2423 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2424 * ABI was defined to be 'struct iovec' which is different on 32bit
2425 * and 64bit. Fortunately we can determine which structure the server
2426 * used from the size of the reply.
2428 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2429 size_t transferred, unsigned count,
2432 #ifdef CONFIG_COMPAT
2433 if (count * sizeof(struct compat_iovec) == transferred) {
2434 struct compat_iovec *ciov = src;
2438 * With this interface a 32bit server cannot support
2439 * non-compat (i.e. ones coming from 64bit apps) ioctl
2445 for (i = 0; i < count; i++) {
2446 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2447 dst[i].iov_len = ciov[i].iov_len;
2453 if (count * sizeof(struct iovec) != transferred)
2456 memcpy(dst, src, transferred);
2460 /* Make sure iov_length() won't overflow */
2461 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2465 u32 max = fc->max_pages << PAGE_SHIFT;
2467 for (n = 0; n < count; n++, iov++) {
2468 if (iov->iov_len > (size_t) max)
2470 max -= iov->iov_len;
2475 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2476 void *src, size_t transferred, unsigned count,
2480 struct fuse_ioctl_iovec *fiov = src;
2482 if (fc->minor < 16) {
2483 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2487 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2490 for (i = 0; i < count; i++) {
2491 /* Did the server supply an inappropriate value? */
2492 if (fiov[i].base != (unsigned long) fiov[i].base ||
2493 fiov[i].len != (unsigned long) fiov[i].len)
2496 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2497 dst[i].iov_len = (size_t) fiov[i].len;
2499 #ifdef CONFIG_COMPAT
2501 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2502 (compat_size_t) dst[i].iov_len != fiov[i].len))
2512 * For ioctls, there is no generic way to determine how much memory
2513 * needs to be read and/or written. Furthermore, ioctls are allowed
2514 * to dereference the passed pointer, so the parameter requires deep
2515 * copying but FUSE has no idea whatsoever about what to copy in or
2518 * This is solved by allowing FUSE server to retry ioctl with
2519 * necessary in/out iovecs. Let's assume the ioctl implementation
2520 * needs to read in the following structure.
2527 * On the first callout to FUSE server, inarg->in_size and
2528 * inarg->out_size will be NULL; then, the server completes the ioctl
2529 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2530 * the actual iov array to
2532 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2534 * which tells FUSE to copy in the requested area and retry the ioctl.
2535 * On the second round, the server has access to the structure and
2536 * from that it can tell what to look for next, so on the invocation,
2537 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2539 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2540 * { .iov_base = a.buf, .iov_len = a.buflen } }
2542 * FUSE will copy both struct a and the pointed buffer from the
2543 * process doing the ioctl and retry ioctl with both struct a and the
2546 * This time, FUSE server has everything it needs and completes ioctl
2547 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2549 * Copying data out works the same way.
2551 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2552 * automatically initializes in and out iovs by decoding @cmd with
2553 * _IOC_* macros and the server is not allowed to request RETRY. This
2554 * limits ioctl data transfers to well-formed ioctls and is the forced
2555 * behavior for all FUSE servers.
2557 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2560 struct fuse_file *ff = file->private_data;
2561 struct fuse_conn *fc = ff->fc;
2562 struct fuse_ioctl_in inarg = {
2568 struct fuse_ioctl_out outarg;
2569 struct fuse_req *req = NULL;
2570 struct page **pages = NULL;
2571 struct iovec *iov_page = NULL;
2572 struct iovec *in_iov = NULL, *out_iov = NULL;
2573 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2574 size_t in_size, out_size, transferred, c;
2578 #if BITS_PER_LONG == 32
2579 inarg.flags |= FUSE_IOCTL_32BIT;
2581 if (flags & FUSE_IOCTL_COMPAT)
2582 inarg.flags |= FUSE_IOCTL_32BIT;
2585 /* assume all the iovs returned by client always fits in a page */
2586 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2589 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
2590 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2591 if (!pages || !iov_page)
2595 * If restricted, initialize IO parameters as encoded in @cmd.
2596 * RETRY from server is not allowed.
2598 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2599 struct iovec *iov = iov_page;
2601 iov->iov_base = (void __user *)arg;
2602 iov->iov_len = _IOC_SIZE(cmd);
2604 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2609 if (_IOC_DIR(cmd) & _IOC_READ) {
2616 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2617 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2620 * Out data can be used either for actual out data or iovs,
2621 * make sure there always is at least one page.
2623 out_size = max_t(size_t, out_size, PAGE_SIZE);
2624 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2626 /* make sure there are enough buffer pages and init request with them */
2628 if (max_pages > fc->max_pages)
2630 while (num_pages < max_pages) {
2631 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2632 if (!pages[num_pages])
2637 req = fuse_get_req(fc, num_pages);
2643 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2644 req->num_pages = num_pages;
2645 fuse_page_descs_length_init(req, 0, req->num_pages);
2647 /* okay, let's send it to the client */
2648 req->in.h.opcode = FUSE_IOCTL;
2649 req->in.h.nodeid = ff->nodeid;
2650 req->in.numargs = 1;
2651 req->in.args[0].size = sizeof(inarg);
2652 req->in.args[0].value = &inarg;
2655 req->in.args[1].size = in_size;
2656 req->in.argpages = 1;
2659 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2660 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2661 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2662 if (c != PAGE_SIZE && iov_iter_count(&ii))
2667 req->out.numargs = 2;
2668 req->out.args[0].size = sizeof(outarg);
2669 req->out.args[0].value = &outarg;
2670 req->out.args[1].size = out_size;
2671 req->out.argpages = 1;
2672 req->out.argvar = 1;
2674 fuse_request_send(fc, req);
2675 err = req->out.h.error;
2676 transferred = req->out.args[1].size;
2677 fuse_put_request(fc, req);
2682 /* did it ask for retry? */
2683 if (outarg.flags & FUSE_IOCTL_RETRY) {
2686 /* no retry if in restricted mode */
2688 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2691 in_iovs = outarg.in_iovs;
2692 out_iovs = outarg.out_iovs;
2695 * Make sure things are in boundary, separate checks
2696 * are to protect against overflow.
2699 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2700 out_iovs > FUSE_IOCTL_MAX_IOV ||
2701 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2704 vaddr = kmap_atomic(pages[0]);
2705 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2706 transferred, in_iovs + out_iovs,
2707 (flags & FUSE_IOCTL_COMPAT) != 0);
2708 kunmap_atomic(vaddr);
2713 out_iov = in_iov + in_iovs;
2715 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2719 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2727 if (transferred > inarg.out_size)
2731 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2732 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2733 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2734 if (c != PAGE_SIZE && iov_iter_count(&ii))
2740 fuse_put_request(fc, req);
2741 free_page((unsigned long) iov_page);
2743 __free_page(pages[--num_pages]);
2746 return err ? err : outarg.result;
2748 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2750 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2751 unsigned long arg, unsigned int flags)
2753 struct inode *inode = file_inode(file);
2754 struct fuse_conn *fc = get_fuse_conn(inode);
2756 if (!fuse_allow_current_process(fc))
2759 if (is_bad_inode(inode))
2762 return fuse_do_ioctl(file, cmd, arg, flags);
2765 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2768 return fuse_ioctl_common(file, cmd, arg, 0);
2771 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2774 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2778 * All files which have been polled are linked to RB tree
2779 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2780 * find the matching one.
2782 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2783 struct rb_node **parent_out)
2785 struct rb_node **link = &fc->polled_files.rb_node;
2786 struct rb_node *last = NULL;
2789 struct fuse_file *ff;
2792 ff = rb_entry(last, struct fuse_file, polled_node);
2795 link = &last->rb_left;
2796 else if (kh > ff->kh)
2797 link = &last->rb_right;
2808 * The file is about to be polled. Make sure it's on the polled_files
2809 * RB tree. Note that files once added to the polled_files tree are
2810 * not removed before the file is released. This is because a file
2811 * polled once is likely to be polled again.
2813 static void fuse_register_polled_file(struct fuse_conn *fc,
2814 struct fuse_file *ff)
2816 spin_lock(&fc->lock);
2817 if (RB_EMPTY_NODE(&ff->polled_node)) {
2818 struct rb_node **link, *uninitialized_var(parent);
2820 link = fuse_find_polled_node(fc, ff->kh, &parent);
2822 rb_link_node(&ff->polled_node, parent, link);
2823 rb_insert_color(&ff->polled_node, &fc->polled_files);
2825 spin_unlock(&fc->lock);
2828 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2830 struct fuse_file *ff = file->private_data;
2831 struct fuse_conn *fc = ff->fc;
2832 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2833 struct fuse_poll_out outarg;
2838 return DEFAULT_POLLMASK;
2840 poll_wait(file, &ff->poll_wait, wait);
2841 inarg.events = mangle_poll(poll_requested_events(wait));
2844 * Ask for notification iff there's someone waiting for it.
2845 * The client may ignore the flag and always notify.
2847 if (waitqueue_active(&ff->poll_wait)) {
2848 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2849 fuse_register_polled_file(fc, ff);
2852 args.in.h.opcode = FUSE_POLL;
2853 args.in.h.nodeid = ff->nodeid;
2854 args.in.numargs = 1;
2855 args.in.args[0].size = sizeof(inarg);
2856 args.in.args[0].value = &inarg;
2857 args.out.numargs = 1;
2858 args.out.args[0].size = sizeof(outarg);
2859 args.out.args[0].value = &outarg;
2860 err = fuse_simple_request(fc, &args);
2863 return demangle_poll(outarg.revents);
2864 if (err == -ENOSYS) {
2866 return DEFAULT_POLLMASK;
2870 EXPORT_SYMBOL_GPL(fuse_file_poll);
2873 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2874 * wakes up the poll waiters.
2876 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2877 struct fuse_notify_poll_wakeup_out *outarg)
2879 u64 kh = outarg->kh;
2880 struct rb_node **link;
2882 spin_lock(&fc->lock);
2884 link = fuse_find_polled_node(fc, kh, NULL);
2886 struct fuse_file *ff;
2888 ff = rb_entry(*link, struct fuse_file, polled_node);
2889 wake_up_interruptible_sync(&ff->poll_wait);
2892 spin_unlock(&fc->lock);
2896 static void fuse_do_truncate(struct file *file)
2898 struct inode *inode = file->f_mapping->host;
2901 attr.ia_valid = ATTR_SIZE;
2902 attr.ia_size = i_size_read(inode);
2904 attr.ia_file = file;
2905 attr.ia_valid |= ATTR_FILE;
2907 fuse_do_setattr(file_dentry(file), &attr, file);
2910 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2912 return round_up(off, fc->max_pages << PAGE_SHIFT);
2916 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2918 DECLARE_COMPLETION_ONSTACK(wait);
2920 struct file *file = iocb->ki_filp;
2921 struct fuse_file *ff = file->private_data;
2922 bool async_dio = ff->fc->async_dio;
2924 struct inode *inode;
2926 size_t count = iov_iter_count(iter);
2927 loff_t offset = iocb->ki_pos;
2928 struct fuse_io_priv *io;
2931 inode = file->f_mapping->host;
2932 i_size = i_size_read(inode);
2934 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2937 /* optimization for short read */
2938 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2939 if (offset >= i_size)
2941 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
2942 count = iov_iter_count(iter);
2945 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2948 spin_lock_init(&io->lock);
2949 kref_init(&io->refcnt);
2953 io->offset = offset;
2954 io->write = (iov_iter_rw(iter) == WRITE);
2957 * By default, we want to optimize all I/Os with async request
2958 * submission to the client filesystem if supported.
2960 io->async = async_dio;
2962 io->blocking = is_sync_kiocb(iocb);
2965 * We cannot asynchronously extend the size of a file.
2966 * In such case the aio will behave exactly like sync io.
2968 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2969 io->blocking = true;
2971 if (io->async && io->blocking) {
2973 * Additional reference to keep io around after
2974 * calling fuse_aio_complete()
2976 kref_get(&io->refcnt);
2980 if (iov_iter_rw(iter) == WRITE) {
2981 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2982 fuse_invalidate_attr(inode);
2984 ret = __fuse_direct_read(io, iter, &pos);
2988 bool blocking = io->blocking;
2990 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2992 /* we have a non-extending, async request, so return */
2994 return -EIOCBQUEUED;
2996 wait_for_completion(&wait);
2997 ret = fuse_get_res_by_io(io);
3000 kref_put(&io->refcnt, fuse_io_release);
3002 if (iov_iter_rw(iter) == WRITE) {
3004 fuse_write_update_size(inode, pos);
3005 else if (ret < 0 && offset + count > i_size)
3006 fuse_do_truncate(file);
3012 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3015 struct fuse_file *ff = file->private_data;
3016 struct inode *inode = file_inode(file);
3017 struct fuse_inode *fi = get_fuse_inode(inode);
3018 struct fuse_conn *fc = ff->fc;
3020 struct fuse_fallocate_in inarg = {
3027 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3028 (mode & FALLOC_FL_PUNCH_HOLE);
3030 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3033 if (fc->no_fallocate)
3038 if (mode & FALLOC_FL_PUNCH_HOLE) {
3039 loff_t endbyte = offset + length - 1;
3040 err = filemap_write_and_wait_range(inode->i_mapping,
3045 fuse_sync_writes(inode);
3049 if (!(mode & FALLOC_FL_KEEP_SIZE))
3050 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3052 args.in.h.opcode = FUSE_FALLOCATE;
3053 args.in.h.nodeid = ff->nodeid;
3054 args.in.numargs = 1;
3055 args.in.args[0].size = sizeof(inarg);
3056 args.in.args[0].value = &inarg;
3057 err = fuse_simple_request(fc, &args);
3058 if (err == -ENOSYS) {
3059 fc->no_fallocate = 1;
3065 /* we could have extended the file */
3066 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3067 bool changed = fuse_write_update_size(inode, offset + length);
3069 if (changed && fc->writeback_cache)
3070 file_update_time(file);
3073 if (mode & FALLOC_FL_PUNCH_HOLE)
3074 truncate_pagecache_range(inode, offset, offset + length - 1);
3076 fuse_invalidate_attr(inode);
3079 if (!(mode & FALLOC_FL_KEEP_SIZE))
3080 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3083 inode_unlock(inode);
3088 static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3089 struct file *file_out, loff_t pos_out,
3090 size_t len, unsigned int flags)
3092 struct fuse_file *ff_in = file_in->private_data;
3093 struct fuse_file *ff_out = file_out->private_data;
3094 struct inode *inode_out = file_inode(file_out);
3095 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3096 struct fuse_conn *fc = ff_in->fc;
3098 struct fuse_copy_file_range_in inarg = {
3101 .nodeid_out = ff_out->nodeid,
3102 .fh_out = ff_out->fh,
3107 struct fuse_write_out outarg;
3109 /* mark unstable when write-back is not used, and file_out gets
3111 bool is_unstable = (!fc->writeback_cache) &&
3112 ((pos_out + len) > inode_out->i_size);
3114 if (fc->no_copy_file_range)
3117 inode_lock(inode_out);
3119 if (fc->writeback_cache) {
3120 err = filemap_write_and_wait_range(inode_out->i_mapping,
3121 pos_out, pos_out + len);
3125 fuse_sync_writes(inode_out);
3129 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3131 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3132 args.in.h.nodeid = ff_in->nodeid;
3133 args.in.numargs = 1;
3134 args.in.args[0].size = sizeof(inarg);
3135 args.in.args[0].value = &inarg;
3136 args.out.numargs = 1;
3137 args.out.args[0].size = sizeof(outarg);
3138 args.out.args[0].value = &outarg;
3139 err = fuse_simple_request(fc, &args);
3140 if (err == -ENOSYS) {
3141 fc->no_copy_file_range = 1;
3147 if (fc->writeback_cache) {
3148 fuse_write_update_size(inode_out, pos_out + outarg.size);
3149 file_update_time(file_out);
3152 fuse_invalidate_attr(inode_out);
3157 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3159 inode_unlock(inode_out);
3164 static const struct file_operations fuse_file_operations = {
3165 .llseek = fuse_file_llseek,
3166 .read_iter = fuse_file_read_iter,
3167 .write_iter = fuse_file_write_iter,
3168 .mmap = fuse_file_mmap,
3170 .flush = fuse_flush,
3171 .release = fuse_release,
3172 .fsync = fuse_fsync,
3173 .lock = fuse_file_lock,
3174 .flock = fuse_file_flock,
3175 .splice_read = generic_file_splice_read,
3176 .splice_write = iter_file_splice_write,
3177 .unlocked_ioctl = fuse_file_ioctl,
3178 .compat_ioctl = fuse_file_compat_ioctl,
3179 .poll = fuse_file_poll,
3180 .fallocate = fuse_file_fallocate,
3181 .copy_file_range = fuse_copy_file_range,
3184 static const struct address_space_operations fuse_file_aops = {
3185 .readpage = fuse_readpage,
3186 .writepage = fuse_writepage,
3187 .writepages = fuse_writepages,
3188 .launder_page = fuse_launder_page,
3189 .readpages = fuse_readpages,
3190 .set_page_dirty = __set_page_dirty_nobuffers,
3192 .direct_IO = fuse_direct_IO,
3193 .write_begin = fuse_write_begin,
3194 .write_end = fuse_write_end,
3197 void fuse_init_file_inode(struct inode *inode)
3199 struct fuse_inode *fi = get_fuse_inode(inode);
3201 inode->i_fop = &fuse_file_operations;
3202 inode->i_data.a_ops = &fuse_file_aops;
3204 INIT_LIST_HEAD(&fi->write_files);
3205 INIT_LIST_HEAD(&fi->queued_writes);
3207 init_waitqueue_head(&fi->page_waitq);
3208 INIT_LIST_HEAD(&fi->writepages);