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 struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
27 pages = kzalloc(npages * (sizeof(struct page *) +
28 sizeof(struct fuse_page_desc)), flags);
29 *desc = (void *) (pages + npages);
34 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 int opcode, struct fuse_open_out *outargp)
37 struct fuse_open_in inarg;
40 memset(&inarg, 0, sizeof(inarg));
41 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 if (!fc->atomic_o_trunc)
43 inarg.flags &= ~O_TRUNC;
47 args.in_args[0].size = sizeof(inarg);
48 args.in_args[0].value = &inarg;
50 args.out_args[0].size = sizeof(*outargp);
51 args.out_args[0].value = outargp;
53 return fuse_simple_request(fc, &args);
56 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
60 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
65 ff->reserved_req = fuse_request_alloc(0);
66 if (unlikely(!ff->reserved_req)) {
71 INIT_LIST_HEAD(&ff->write_entry);
72 mutex_init(&ff->readdir.lock);
73 refcount_set(&ff->count, 1);
74 RB_CLEAR_NODE(&ff->polled_node);
75 init_waitqueue_head(&ff->poll_wait);
77 ff->kh = atomic64_inc_return(&fc->khctr);
82 void fuse_file_free(struct fuse_file *ff)
84 fuse_request_free(ff->reserved_req);
85 mutex_destroy(&ff->readdir.lock);
89 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
91 refcount_inc(&ff->count);
95 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
97 iput(req->misc.release.inode);
100 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
102 if (refcount_dec_and_test(&ff->count)) {
103 struct fuse_req *req = ff->reserved_req;
105 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
107 * Drop the release request when client does not
110 __clear_bit(FR_BACKGROUND, &req->flags);
111 iput(req->misc.release.inode);
112 fuse_put_request(ff->fc, req);
114 __set_bit(FR_FORCE, &req->flags);
115 __clear_bit(FR_BACKGROUND, &req->flags);
116 fuse_request_send(ff->fc, req);
117 iput(req->misc.release.inode);
118 fuse_put_request(ff->fc, req);
120 req->end = fuse_release_end;
121 __set_bit(FR_BACKGROUND, &req->flags);
122 fuse_request_send_background(ff->fc, req);
128 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
131 struct fuse_file *ff;
132 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
134 ff = fuse_file_alloc(fc);
139 /* Default for no-open */
140 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
141 if (isdir ? !fc->no_opendir : !fc->no_open) {
142 struct fuse_open_out outarg;
145 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
148 ff->open_flags = outarg.open_flags;
150 } else if (err != -ENOSYS) {
162 ff->open_flags &= ~FOPEN_DIRECT_IO;
165 file->private_data = ff;
169 EXPORT_SYMBOL_GPL(fuse_do_open);
171 static void fuse_link_write_file(struct file *file)
173 struct inode *inode = file_inode(file);
174 struct fuse_inode *fi = get_fuse_inode(inode);
175 struct fuse_file *ff = file->private_data;
177 * file may be written through mmap, so chain it onto the
178 * inodes's write_file list
180 spin_lock(&fi->lock);
181 if (list_empty(&ff->write_entry))
182 list_add(&ff->write_entry, &fi->write_files);
183 spin_unlock(&fi->lock);
186 void fuse_finish_open(struct inode *inode, struct file *file)
188 struct fuse_file *ff = file->private_data;
189 struct fuse_conn *fc = get_fuse_conn(inode);
191 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
192 invalidate_inode_pages2(inode->i_mapping);
193 if (ff->open_flags & FOPEN_STREAM)
194 stream_open(inode, file);
195 else if (ff->open_flags & FOPEN_NONSEEKABLE)
196 nonseekable_open(inode, file);
197 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
198 struct fuse_inode *fi = get_fuse_inode(inode);
200 spin_lock(&fi->lock);
201 fi->attr_version = atomic64_inc_return(&fc->attr_version);
202 i_size_write(inode, 0);
203 spin_unlock(&fi->lock);
204 fuse_invalidate_attr(inode);
205 if (fc->writeback_cache)
206 file_update_time(file);
208 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
209 fuse_link_write_file(file);
212 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
214 struct fuse_conn *fc = get_fuse_conn(inode);
216 bool lock_inode = (file->f_flags & O_TRUNC) &&
217 fc->atomic_o_trunc &&
220 err = generic_file_open(inode, file);
227 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
230 fuse_finish_open(inode, file);
238 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
239 int flags, int opcode)
241 struct fuse_conn *fc = ff->fc;
242 struct fuse_req *req = ff->reserved_req;
243 struct fuse_release_in *inarg = &req->misc.release.in;
245 /* Inode is NULL on error path of fuse_create_open() */
247 spin_lock(&fi->lock);
248 list_del(&ff->write_entry);
249 spin_unlock(&fi->lock);
251 spin_lock(&fc->lock);
252 if (!RB_EMPTY_NODE(&ff->polled_node))
253 rb_erase(&ff->polled_node, &fc->polled_files);
254 spin_unlock(&fc->lock);
256 wake_up_interruptible_all(&ff->poll_wait);
259 inarg->flags = flags;
260 req->in.h.opcode = opcode;
261 req->in.h.nodeid = ff->nodeid;
263 req->in.args[0].size = sizeof(struct fuse_release_in);
264 req->in.args[0].value = inarg;
267 void fuse_release_common(struct file *file, bool isdir)
269 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
270 struct fuse_file *ff = file->private_data;
271 struct fuse_req *req = ff->reserved_req;
272 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
274 fuse_prepare_release(fi, ff, file->f_flags, opcode);
277 struct fuse_release_in *inarg = &req->misc.release.in;
278 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
279 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
282 /* Hold inode until release is finished */
283 req->misc.release.inode = igrab(file_inode(file));
286 * Normally this will send the RELEASE request, however if
287 * some asynchronous READ or WRITE requests are outstanding,
288 * the sending will be delayed.
290 * Make the release synchronous if this is a fuseblk mount,
291 * synchronous RELEASE is allowed (and desirable) in this case
292 * because the server can be trusted not to screw up.
294 fuse_file_put(ff, ff->fc->destroy, isdir);
297 static int fuse_open(struct inode *inode, struct file *file)
299 return fuse_open_common(inode, file, false);
302 static int fuse_release(struct inode *inode, struct file *file)
304 struct fuse_conn *fc = get_fuse_conn(inode);
306 /* see fuse_vma_close() for !writeback_cache case */
307 if (fc->writeback_cache)
308 write_inode_now(inode, 1);
310 fuse_release_common(file, false);
312 /* return value is ignored by VFS */
316 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
318 WARN_ON(refcount_read(&ff->count) > 1);
319 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
321 * iput(NULL) is a no-op and since the refcount is 1 and everything's
322 * synchronous, we are fine with not doing igrab() here"
324 fuse_file_put(ff, true, false);
326 EXPORT_SYMBOL_GPL(fuse_sync_release);
329 * Scramble the ID space with XTEA, so that the value of the files_struct
330 * pointer is not exposed to userspace.
332 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
334 u32 *k = fc->scramble_key;
335 u64 v = (unsigned long) id;
341 for (i = 0; i < 32; i++) {
342 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
344 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
347 return (u64) v0 + ((u64) v1 << 32);
350 static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
351 pgoff_t idx_from, pgoff_t idx_to)
353 struct fuse_req *req;
355 list_for_each_entry(req, &fi->writepages, writepages_entry) {
358 WARN_ON(get_fuse_inode(req->inode) != fi);
359 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
360 if (idx_from < curr_index + req->num_pages &&
361 curr_index <= idx_to) {
369 * Check if any page in a range is under writeback
371 * This is currently done by walking the list of writepage requests
372 * for the inode, which can be pretty inefficient.
374 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
377 struct fuse_inode *fi = get_fuse_inode(inode);
380 spin_lock(&fi->lock);
381 found = fuse_find_writeback(fi, idx_from, idx_to);
382 spin_unlock(&fi->lock);
387 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
389 return fuse_range_is_writeback(inode, index, index);
393 * Wait for page writeback to be completed.
395 * Since fuse doesn't rely on the VM writeback tracking, this has to
396 * use some other means.
398 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
400 struct fuse_inode *fi = get_fuse_inode(inode);
402 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
406 * Wait for all pending writepages on the inode to finish.
408 * This is currently done by blocking further writes with FUSE_NOWRITE
409 * and waiting for all sent writes to complete.
411 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
412 * could conflict with truncation.
414 static void fuse_sync_writes(struct inode *inode)
416 fuse_set_nowrite(inode);
417 fuse_release_nowrite(inode);
420 static int fuse_flush(struct file *file, fl_owner_t id)
422 struct inode *inode = file_inode(file);
423 struct fuse_conn *fc = get_fuse_conn(inode);
424 struct fuse_file *ff = file->private_data;
425 struct fuse_flush_in inarg;
429 if (is_bad_inode(inode))
435 err = write_inode_now(inode, 1);
440 fuse_sync_writes(inode);
443 err = filemap_check_errors(file->f_mapping);
447 memset(&inarg, 0, sizeof(inarg));
449 inarg.lock_owner = fuse_lock_owner_id(fc, id);
450 args.opcode = FUSE_FLUSH;
451 args.nodeid = get_node_id(inode);
453 args.in_args[0].size = sizeof(inarg);
454 args.in_args[0].value = &inarg;
457 err = fuse_simple_request(fc, &args);
458 if (err == -ENOSYS) {
465 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
466 int datasync, int opcode)
468 struct inode *inode = file->f_mapping->host;
469 struct fuse_conn *fc = get_fuse_conn(inode);
470 struct fuse_file *ff = file->private_data;
472 struct fuse_fsync_in inarg;
474 memset(&inarg, 0, sizeof(inarg));
476 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
477 args.opcode = opcode;
478 args.nodeid = get_node_id(inode);
480 args.in_args[0].size = sizeof(inarg);
481 args.in_args[0].value = &inarg;
482 return fuse_simple_request(fc, &args);
485 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
488 struct inode *inode = file->f_mapping->host;
489 struct fuse_conn *fc = get_fuse_conn(inode);
492 if (is_bad_inode(inode))
498 * Start writeback against all dirty pages of the inode, then
499 * wait for all outstanding writes, before sending the FSYNC
502 err = file_write_and_wait_range(file, start, end);
506 fuse_sync_writes(inode);
509 * Due to implementation of fuse writeback
510 * file_write_and_wait_range() does not catch errors.
511 * We have to do this directly after fuse_sync_writes()
513 err = file_check_and_advance_wb_err(file);
517 err = sync_inode_metadata(inode, 1);
524 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
525 if (err == -ENOSYS) {
535 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
536 size_t count, int opcode)
538 struct fuse_read_in *inarg = &req->misc.read.in;
539 struct fuse_file *ff = file->private_data;
544 inarg->flags = file->f_flags;
545 req->in.h.opcode = opcode;
546 req->in.h.nodeid = ff->nodeid;
548 req->in.args[0].size = sizeof(struct fuse_read_in);
549 req->in.args[0].value = inarg;
551 req->out.numargs = 1;
552 req->out.args[0].size = count;
555 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
559 for (i = 0; i < req->num_pages; i++) {
560 struct page *page = req->pages[i];
562 set_page_dirty_lock(page);
567 static void fuse_io_release(struct kref *kref)
569 kfree(container_of(kref, struct fuse_io_priv, refcnt));
572 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
577 if (io->bytes >= 0 && io->write)
580 return io->bytes < 0 ? io->size : io->bytes;
584 * In case of short read, the caller sets 'pos' to the position of
585 * actual end of fuse request in IO request. Otherwise, if bytes_requested
586 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
589 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
590 * both submitted asynchronously. The first of them was ACKed by userspace as
591 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
592 * second request was ACKed as short, e.g. only 1K was read, resulting in
595 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
596 * will be equal to the length of the longest contiguous fragment of
597 * transferred data starting from the beginning of IO request.
599 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
603 spin_lock(&io->lock);
605 io->err = io->err ? : err;
606 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
610 if (!left && io->blocking)
612 spin_unlock(&io->lock);
614 if (!left && !io->blocking) {
615 ssize_t res = fuse_get_res_by_io(io);
618 struct inode *inode = file_inode(io->iocb->ki_filp);
619 struct fuse_conn *fc = get_fuse_conn(inode);
620 struct fuse_inode *fi = get_fuse_inode(inode);
622 spin_lock(&fi->lock);
623 fi->attr_version = atomic64_inc_return(&fc->attr_version);
624 spin_unlock(&fi->lock);
627 io->iocb->ki_complete(io->iocb, res, 0);
630 kref_put(&io->refcnt, fuse_io_release);
633 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
635 struct fuse_io_priv *io = req->io;
638 fuse_release_user_pages(req, io->should_dirty);
641 if (req->misc.write.in.size != req->misc.write.out.size)
642 pos = req->misc.write.in.offset - io->offset +
643 req->misc.write.out.size;
645 if (req->misc.read.in.size != req->out.args[0].size)
646 pos = req->misc.read.in.offset - io->offset +
647 req->out.args[0].size;
650 fuse_aio_complete(io, req->out.h.error, pos);
653 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
654 size_t num_bytes, struct fuse_io_priv *io)
656 spin_lock(&io->lock);
657 kref_get(&io->refcnt);
658 io->size += num_bytes;
660 spin_unlock(&io->lock);
663 req->end = fuse_aio_complete_req;
665 __fuse_get_request(req);
666 fuse_request_send_background(fc, req);
671 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
672 loff_t pos, size_t count, fl_owner_t owner)
674 struct file *file = io->iocb->ki_filp;
675 struct fuse_file *ff = file->private_data;
676 struct fuse_conn *fc = ff->fc;
678 fuse_read_fill(req, file, pos, count, FUSE_READ);
680 struct fuse_read_in *inarg = &req->misc.read.in;
682 inarg->read_flags |= FUSE_READ_LOCKOWNER;
683 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
687 return fuse_async_req_send(fc, req, count, io);
689 fuse_request_send(fc, req);
690 return req->out.args[0].size;
693 static void fuse_read_update_size(struct inode *inode, loff_t size,
696 struct fuse_conn *fc = get_fuse_conn(inode);
697 struct fuse_inode *fi = get_fuse_inode(inode);
699 spin_lock(&fi->lock);
700 if (attr_ver == fi->attr_version && size < inode->i_size &&
701 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
702 fi->attr_version = atomic64_inc_return(&fc->attr_version);
703 i_size_write(inode, size);
705 spin_unlock(&fi->lock);
708 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
709 struct page **pages, unsigned int num_pages)
711 struct fuse_conn *fc = get_fuse_conn(inode);
713 if (fc->writeback_cache) {
715 * A hole in a file. Some data after the hole are in page cache,
716 * but have not reached the client fs yet. So, the hole is not
720 int start_idx = num_read >> PAGE_SHIFT;
721 size_t off = num_read & (PAGE_SIZE - 1);
723 for (i = start_idx; i < num_pages; i++) {
724 zero_user_segment(pages[i], off, PAGE_SIZE);
728 loff_t pos = page_offset(pages[0]) + num_read;
729 fuse_read_update_size(inode, pos, attr_ver);
733 static int fuse_do_readpage(struct file *file, struct page *page)
736 struct fuse_io_priv io;
737 struct inode *inode = page->mapping->host;
738 struct fuse_conn *fc = get_fuse_conn(inode);
739 struct fuse_req *req;
741 loff_t pos = page_offset(page);
742 size_t count = PAGE_SIZE;
747 * Page writeback can extend beyond the lifetime of the
748 * page-cache page, so make sure we read a properly synced
751 fuse_wait_on_page_writeback(inode, page->index);
753 req = fuse_get_req(fc, 1);
757 attr_ver = fuse_get_attr_version(fc);
759 req->out.page_zeroing = 1;
760 req->out.argpages = 1;
762 req->pages[0] = page;
763 req->page_descs[0].length = count;
764 init_sync_kiocb(&iocb, file);
765 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
766 num_read = fuse_send_read(req, &io, pos, count, NULL);
767 err = req->out.h.error;
771 * Short read means EOF. If file size is larger, truncate it
773 if (num_read < count)
774 fuse_short_read(inode, attr_ver, num_read, req->pages,
777 SetPageUptodate(page);
780 fuse_put_request(fc, req);
785 static int fuse_readpage(struct file *file, struct page *page)
787 struct inode *inode = page->mapping->host;
791 if (is_bad_inode(inode))
794 err = fuse_do_readpage(file, page);
795 fuse_invalidate_atime(inode);
801 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
804 size_t count = req->misc.read.in.size;
805 size_t num_read = req->out.args[0].size;
806 struct address_space *mapping = NULL;
808 for (i = 0; mapping == NULL && i < req->num_pages; i++)
809 mapping = req->pages[i]->mapping;
812 struct inode *inode = mapping->host;
815 * Short read means EOF. If file size is larger, truncate it
817 if (!req->out.h.error && num_read < count)
818 fuse_short_read(inode, req->misc.read.attr_ver,
819 num_read, req->pages, req->num_pages);
821 fuse_invalidate_atime(inode);
824 for (i = 0; i < req->num_pages; i++) {
825 struct page *page = req->pages[i];
826 if (!req->out.h.error)
827 SetPageUptodate(page);
834 fuse_file_put(req->ff, false, false);
837 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
839 struct fuse_file *ff = file->private_data;
840 struct fuse_conn *fc = ff->fc;
841 loff_t pos = page_offset(req->pages[0]);
842 size_t count = req->num_pages << PAGE_SHIFT;
844 req->out.argpages = 1;
845 req->out.page_zeroing = 1;
846 req->out.page_replace = 1;
847 fuse_read_fill(req, file, pos, count, FUSE_READ);
848 req->misc.read.attr_ver = fuse_get_attr_version(fc);
849 if (fc->async_read) {
850 req->ff = fuse_file_get(ff);
851 req->end = fuse_readpages_end;
852 fuse_request_send_background(fc, req);
854 fuse_request_send(fc, req);
855 fuse_readpages_end(fc, req);
856 fuse_put_request(fc, req);
860 struct fuse_fill_data {
861 struct fuse_req *req;
867 static int fuse_readpages_fill(void *_data, struct page *page)
869 struct fuse_fill_data *data = _data;
870 struct fuse_req *req = data->req;
871 struct inode *inode = data->inode;
872 struct fuse_conn *fc = get_fuse_conn(inode);
874 fuse_wait_on_page_writeback(inode, page->index);
876 if (req->num_pages &&
877 (req->num_pages == fc->max_pages ||
878 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
879 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
880 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
882 fuse_send_readpages(req, data->file);
884 req = fuse_get_req_for_background(fc, nr_alloc);
886 req = fuse_get_req(fc, nr_alloc);
895 if (WARN_ON(req->num_pages >= req->max_pages)) {
897 fuse_put_request(fc, req);
902 req->pages[req->num_pages] = page;
903 req->page_descs[req->num_pages].length = PAGE_SIZE;
909 static int fuse_readpages(struct file *file, struct address_space *mapping,
910 struct list_head *pages, unsigned nr_pages)
912 struct inode *inode = mapping->host;
913 struct fuse_conn *fc = get_fuse_conn(inode);
914 struct fuse_fill_data data;
916 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
919 if (is_bad_inode(inode))
925 data.req = fuse_get_req_for_background(fc, nr_alloc);
927 data.req = fuse_get_req(fc, nr_alloc);
928 data.nr_pages = nr_pages;
929 err = PTR_ERR(data.req);
930 if (IS_ERR(data.req))
933 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
935 if (data.req->num_pages)
936 fuse_send_readpages(data.req, file);
938 fuse_put_request(fc, data.req);
944 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
946 struct inode *inode = iocb->ki_filp->f_mapping->host;
947 struct fuse_conn *fc = get_fuse_conn(inode);
950 * In auto invalidate mode, always update attributes on read.
951 * Otherwise, only update if we attempt to read past EOF (to ensure
952 * i_size is up to date).
954 if (fc->auto_inval_data ||
955 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
957 err = fuse_update_attributes(inode, iocb->ki_filp);
962 return generic_file_read_iter(iocb, to);
965 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
966 loff_t pos, size_t count)
968 struct fuse_write_in *inarg = &req->misc.write.in;
969 struct fuse_write_out *outarg = &req->misc.write.out;
974 req->in.h.opcode = FUSE_WRITE;
975 req->in.h.nodeid = ff->nodeid;
977 if (ff->fc->minor < 9)
978 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
980 req->in.args[0].size = sizeof(struct fuse_write_in);
981 req->in.args[0].value = inarg;
982 req->in.args[1].size = count;
983 req->out.numargs = 1;
984 req->out.args[0].size = sizeof(struct fuse_write_out);
985 req->out.args[0].value = outarg;
988 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
989 loff_t pos, size_t count, fl_owner_t owner)
991 struct kiocb *iocb = io->iocb;
992 struct file *file = iocb->ki_filp;
993 struct fuse_file *ff = file->private_data;
994 struct fuse_conn *fc = ff->fc;
995 struct fuse_write_in *inarg = &req->misc.write.in;
997 fuse_write_fill(req, ff, pos, count);
998 inarg->flags = file->f_flags;
999 if (iocb->ki_flags & IOCB_DSYNC)
1000 inarg->flags |= O_DSYNC;
1001 if (iocb->ki_flags & IOCB_SYNC)
1002 inarg->flags |= O_SYNC;
1003 if (owner != NULL) {
1004 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1005 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1009 return fuse_async_req_send(fc, req, count, io);
1011 fuse_request_send(fc, req);
1012 return req->misc.write.out.size;
1015 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1017 struct fuse_conn *fc = get_fuse_conn(inode);
1018 struct fuse_inode *fi = get_fuse_inode(inode);
1021 spin_lock(&fi->lock);
1022 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1023 if (pos > inode->i_size) {
1024 i_size_write(inode, pos);
1027 spin_unlock(&fi->lock);
1032 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1033 struct inode *inode, loff_t pos,
1039 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1041 for (i = 0; i < req->num_pages; i++)
1042 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1044 res = fuse_send_write(req, &io, pos, count, NULL);
1046 offset = req->page_descs[0].offset;
1048 for (i = 0; i < req->num_pages; i++) {
1049 struct page *page = req->pages[i];
1051 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1052 SetPageUptodate(page);
1054 if (count > PAGE_SIZE - offset)
1055 count -= PAGE_SIZE - offset;
1067 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1068 struct address_space *mapping,
1069 struct iov_iter *ii, loff_t pos)
1071 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1072 unsigned offset = pos & (PAGE_SIZE - 1);
1076 req->in.argpages = 1;
1077 req->page_descs[0].offset = offset;
1082 pgoff_t index = pos >> PAGE_SHIFT;
1083 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1084 iov_iter_count(ii));
1086 bytes = min_t(size_t, bytes, fc->max_write - count);
1090 if (iov_iter_fault_in_readable(ii, bytes))
1094 page = grab_cache_page_write_begin(mapping, index, 0);
1098 if (mapping_writably_mapped(mapping))
1099 flush_dcache_page(page);
1101 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1102 flush_dcache_page(page);
1104 iov_iter_advance(ii, tmp);
1108 bytes = min(bytes, iov_iter_single_seg_count(ii));
1113 req->pages[req->num_pages] = page;
1114 req->page_descs[req->num_pages].length = tmp;
1120 if (offset == PAGE_SIZE)
1123 if (!fc->big_writes)
1125 } while (iov_iter_count(ii) && count < fc->max_write &&
1126 req->num_pages < req->max_pages && offset == 0);
1128 return count > 0 ? count : err;
1131 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1132 unsigned int max_pages)
1134 return min_t(unsigned int,
1135 ((pos + len - 1) >> PAGE_SHIFT) -
1136 (pos >> PAGE_SHIFT) + 1,
1140 static ssize_t fuse_perform_write(struct kiocb *iocb,
1141 struct address_space *mapping,
1142 struct iov_iter *ii, loff_t pos)
1144 struct inode *inode = mapping->host;
1145 struct fuse_conn *fc = get_fuse_conn(inode);
1146 struct fuse_inode *fi = get_fuse_inode(inode);
1150 if (inode->i_size < pos + iov_iter_count(ii))
1151 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1154 struct fuse_req *req;
1156 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1159 req = fuse_get_req(fc, nr_pages);
1165 count = fuse_fill_write_pages(req, mapping, ii, pos);
1171 num_written = fuse_send_write_pages(req, iocb, inode,
1173 err = req->out.h.error;
1178 /* break out of the loop on short write */
1179 if (num_written != count)
1183 fuse_put_request(fc, req);
1184 } while (!err && iov_iter_count(ii));
1187 fuse_write_update_size(inode, pos);
1189 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1190 fuse_invalidate_attr(inode);
1192 return res > 0 ? res : err;
1195 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1197 struct file *file = iocb->ki_filp;
1198 struct address_space *mapping = file->f_mapping;
1199 ssize_t written = 0;
1200 ssize_t written_buffered = 0;
1201 struct inode *inode = mapping->host;
1205 if (get_fuse_conn(inode)->writeback_cache) {
1206 /* Update size (EOF optimization) and mode (SUID clearing) */
1207 err = fuse_update_attributes(mapping->host, file);
1211 return generic_file_write_iter(iocb, from);
1216 /* We can write back this queue in page reclaim */
1217 current->backing_dev_info = inode_to_bdi(inode);
1219 err = generic_write_checks(iocb, from);
1223 err = file_remove_privs(file);
1227 err = file_update_time(file);
1231 if (iocb->ki_flags & IOCB_DIRECT) {
1232 loff_t pos = iocb->ki_pos;
1233 written = generic_file_direct_write(iocb, from);
1234 if (written < 0 || !iov_iter_count(from))
1239 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1240 if (written_buffered < 0) {
1241 err = written_buffered;
1244 endbyte = pos + written_buffered - 1;
1246 err = filemap_write_and_wait_range(file->f_mapping, pos,
1251 invalidate_mapping_pages(file->f_mapping,
1253 endbyte >> PAGE_SHIFT);
1255 written += written_buffered;
1256 iocb->ki_pos = pos + written_buffered;
1258 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1260 iocb->ki_pos += written;
1263 current->backing_dev_info = NULL;
1264 inode_unlock(inode);
1266 written = generic_write_sync(iocb, written);
1268 return written ? written : err;
1271 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1273 unsigned int nr_pages)
1277 for (i = index; i < index + nr_pages; i++)
1278 descs[i].length = PAGE_SIZE - descs[i].offset;
1281 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1283 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1286 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1289 return min(iov_iter_single_seg_count(ii), max_size);
1292 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1293 size_t *nbytesp, int write)
1295 size_t nbytes = 0; /* # bytes already packed in req */
1298 /* Special case for kernel I/O: can copy directly into the buffer */
1299 if (iov_iter_is_kvec(ii)) {
1300 unsigned long user_addr = fuse_get_user_addr(ii);
1301 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1304 req->in.args[1].value = (void *) user_addr;
1306 req->out.args[0].value = (void *) user_addr;
1308 iov_iter_advance(ii, frag_size);
1309 *nbytesp = frag_size;
1313 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1316 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1318 req->max_pages - req->num_pages,
1323 iov_iter_advance(ii, ret);
1327 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1329 req->page_descs[req->num_pages].offset = start;
1330 fuse_page_descs_length_init(req->page_descs, req->num_pages,
1333 req->num_pages += npages;
1334 req->page_descs[req->num_pages - 1].length -=
1335 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1339 req->in.argpages = 1;
1341 req->out.argpages = 1;
1345 return ret < 0 ? ret : 0;
1348 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1349 loff_t *ppos, int flags)
1351 int write = flags & FUSE_DIO_WRITE;
1352 int cuse = flags & FUSE_DIO_CUSE;
1353 struct file *file = io->iocb->ki_filp;
1354 struct inode *inode = file->f_mapping->host;
1355 struct fuse_file *ff = file->private_data;
1356 struct fuse_conn *fc = ff->fc;
1357 size_t nmax = write ? fc->max_write : fc->max_read;
1359 size_t count = iov_iter_count(iter);
1360 pgoff_t idx_from = pos >> PAGE_SHIFT;
1361 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1363 struct fuse_req *req;
1367 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1370 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
1372 return PTR_ERR(req);
1374 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1377 fuse_sync_writes(inode);
1379 inode_unlock(inode);
1382 io->should_dirty = !write && iter_is_iovec(iter);
1385 fl_owner_t owner = current->files;
1386 size_t nbytes = min(count, nmax);
1387 err = fuse_get_user_pages(req, iter, &nbytes, write);
1392 if (!capable(CAP_FSETID)) {
1393 struct fuse_write_in *inarg;
1395 inarg = &req->misc.write.in;
1396 inarg->write_flags |= FUSE_WRITE_KILL_PRIV;
1398 nres = fuse_send_write(req, io, pos, nbytes, owner);
1400 nres = fuse_send_read(req, io, pos, nbytes, owner);
1404 fuse_release_user_pages(req, io->should_dirty);
1405 if (req->out.h.error) {
1406 err = req->out.h.error;
1408 } else if (nres > nbytes) {
1419 fuse_put_request(fc, req);
1421 req = fuse_get_req_for_background(fc,
1422 iov_iter_npages(iter, fc->max_pages));
1424 req = fuse_get_req(fc, iov_iter_npages(iter,
1431 fuse_put_request(fc, req);
1435 return res > 0 ? res : err;
1437 EXPORT_SYMBOL_GPL(fuse_direct_io);
1439 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1440 struct iov_iter *iter,
1444 struct inode *inode = file_inode(io->iocb->ki_filp);
1446 res = fuse_direct_io(io, iter, ppos, 0);
1448 fuse_invalidate_atime(inode);
1453 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1455 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1459 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1460 res = fuse_direct_IO(iocb, to);
1462 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1464 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1470 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1472 struct inode *inode = file_inode(iocb->ki_filp);
1473 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1476 /* Don't allow parallel writes to the same file */
1478 res = generic_write_checks(iocb, from);
1480 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1481 res = fuse_direct_IO(iocb, from);
1483 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1487 fuse_invalidate_attr(inode);
1489 fuse_write_update_size(inode, iocb->ki_pos);
1490 inode_unlock(inode);
1495 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1497 struct file *file = iocb->ki_filp;
1498 struct fuse_file *ff = file->private_data;
1500 if (is_bad_inode(file_inode(file)))
1503 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1504 return fuse_cache_read_iter(iocb, to);
1506 return fuse_direct_read_iter(iocb, to);
1509 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1511 struct file *file = iocb->ki_filp;
1512 struct fuse_file *ff = file->private_data;
1514 if (is_bad_inode(file_inode(file)))
1517 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1518 return fuse_cache_write_iter(iocb, from);
1520 return fuse_direct_write_iter(iocb, from);
1523 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1527 for (i = 0; i < req->num_pages; i++)
1528 __free_page(req->pages[i]);
1531 fuse_file_put(req->ff, false, false);
1534 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1536 struct inode *inode = req->inode;
1537 struct fuse_inode *fi = get_fuse_inode(inode);
1538 struct backing_dev_info *bdi = inode_to_bdi(inode);
1541 list_del(&req->writepages_entry);
1542 for (i = 0; i < req->num_pages; i++) {
1543 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1544 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1545 wb_writeout_inc(&bdi->wb);
1547 wake_up(&fi->page_waitq);
1550 /* Called under fi->lock, may release and reacquire it */
1551 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1553 __releases(fi->lock)
1554 __acquires(fi->lock)
1556 struct fuse_req *aux, *next;
1557 struct fuse_inode *fi = get_fuse_inode(req->inode);
1558 struct fuse_write_in *inarg = &req->misc.write.in;
1559 __u64 data_size = req->num_pages * PAGE_SIZE;
1562 if (inarg->offset + data_size <= size) {
1563 inarg->size = data_size;
1564 } else if (inarg->offset < size) {
1565 inarg->size = size - inarg->offset;
1567 /* Got truncated off completely */
1571 req->in.args[1].size = inarg->size;
1572 queued = fuse_request_queue_background(fc, req);
1573 /* Fails on broken connection only */
1574 if (unlikely(!queued))
1581 fuse_writepage_finish(fc, req);
1582 spin_unlock(&fi->lock);
1584 /* After fuse_writepage_finish() aux request list is private */
1585 for (aux = req->misc.write.next; aux; aux = next) {
1586 next = aux->misc.write.next;
1587 aux->misc.write.next = NULL;
1588 fuse_writepage_free(fc, aux);
1589 fuse_put_request(fc, aux);
1592 fuse_writepage_free(fc, req);
1593 fuse_put_request(fc, req);
1594 spin_lock(&fi->lock);
1598 * If fi->writectr is positive (no truncate or fsync going on) send
1599 * all queued writepage requests.
1601 * Called with fi->lock
1603 void fuse_flush_writepages(struct inode *inode)
1604 __releases(fi->lock)
1605 __acquires(fi->lock)
1607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_inode *fi = get_fuse_inode(inode);
1609 loff_t crop = i_size_read(inode);
1610 struct fuse_req *req;
1612 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1613 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1614 list_del_init(&req->list);
1615 fuse_send_writepage(fc, req, crop);
1619 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1621 struct inode *inode = req->inode;
1622 struct fuse_inode *fi = get_fuse_inode(inode);
1624 mapping_set_error(inode->i_mapping, req->out.h.error);
1625 spin_lock(&fi->lock);
1626 while (req->misc.write.next) {
1627 struct fuse_conn *fc = get_fuse_conn(inode);
1628 struct fuse_write_in *inarg = &req->misc.write.in;
1629 struct fuse_req *next = req->misc.write.next;
1630 req->misc.write.next = next->misc.write.next;
1631 next->misc.write.next = NULL;
1632 next->ff = fuse_file_get(req->ff);
1633 list_add(&next->writepages_entry, &fi->writepages);
1636 * Skip fuse_flush_writepages() to make it easy to crop requests
1637 * based on primary request size.
1639 * 1st case (trivial): there are no concurrent activities using
1640 * fuse_set/release_nowrite. Then we're on safe side because
1641 * fuse_flush_writepages() would call fuse_send_writepage()
1644 * 2nd case: someone called fuse_set_nowrite and it is waiting
1645 * now for completion of all in-flight requests. This happens
1646 * rarely and no more than once per page, so this should be
1649 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1650 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1651 * that fuse_set_nowrite returned implies that all in-flight
1652 * requests were completed along with all of their secondary
1653 * requests. Further primary requests are blocked by negative
1654 * writectr. Hence there cannot be any in-flight requests and
1655 * no invocations of fuse_writepage_end() while we're in
1656 * fuse_set_nowrite..fuse_release_nowrite section.
1658 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1661 fuse_writepage_finish(fc, req);
1662 spin_unlock(&fi->lock);
1663 fuse_writepage_free(fc, req);
1666 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1667 struct fuse_inode *fi)
1669 struct fuse_file *ff = NULL;
1671 spin_lock(&fi->lock);
1672 if (!list_empty(&fi->write_files)) {
1673 ff = list_entry(fi->write_files.next, struct fuse_file,
1677 spin_unlock(&fi->lock);
1682 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1683 struct fuse_inode *fi)
1685 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1690 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1692 struct fuse_conn *fc = get_fuse_conn(inode);
1693 struct fuse_inode *fi = get_fuse_inode(inode);
1694 struct fuse_file *ff;
1697 ff = __fuse_write_file_get(fc, fi);
1698 err = fuse_flush_times(inode, ff);
1700 fuse_file_put(ff, false, false);
1705 static int fuse_writepage_locked(struct page *page)
1707 struct address_space *mapping = page->mapping;
1708 struct inode *inode = mapping->host;
1709 struct fuse_conn *fc = get_fuse_conn(inode);
1710 struct fuse_inode *fi = get_fuse_inode(inode);
1711 struct fuse_req *req;
1712 struct page *tmp_page;
1713 int error = -ENOMEM;
1715 set_page_writeback(page);
1717 req = fuse_request_alloc_nofs(1);
1721 /* writeback always goes to bg_queue */
1722 __set_bit(FR_BACKGROUND, &req->flags);
1723 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1728 req->ff = fuse_write_file_get(fc, fi);
1732 fuse_write_fill(req, req->ff, page_offset(page), 0);
1734 copy_highpage(tmp_page, page);
1735 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1736 req->misc.write.next = NULL;
1737 req->in.argpages = 1;
1739 req->pages[0] = tmp_page;
1740 req->page_descs[0].offset = 0;
1741 req->page_descs[0].length = PAGE_SIZE;
1742 req->end = fuse_writepage_end;
1745 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1746 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1748 spin_lock(&fi->lock);
1749 list_add(&req->writepages_entry, &fi->writepages);
1750 list_add_tail(&req->list, &fi->queued_writes);
1751 fuse_flush_writepages(inode);
1752 spin_unlock(&fi->lock);
1754 end_page_writeback(page);
1759 __free_page(tmp_page);
1761 fuse_request_free(req);
1763 mapping_set_error(page->mapping, error);
1764 end_page_writeback(page);
1768 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1772 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1774 * ->writepages() should be called for sync() and friends. We
1775 * should only get here on direct reclaim and then we are
1776 * allowed to skip a page which is already in flight
1778 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1780 redirty_page_for_writepage(wbc, page);
1784 err = fuse_writepage_locked(page);
1790 struct fuse_fill_wb_data {
1791 struct fuse_req *req;
1792 struct fuse_file *ff;
1793 struct inode *inode;
1794 struct page **orig_pages;
1797 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1799 struct fuse_req *req = data->req;
1800 struct inode *inode = data->inode;
1801 struct fuse_inode *fi = get_fuse_inode(inode);
1802 int num_pages = req->num_pages;
1805 req->ff = fuse_file_get(data->ff);
1806 spin_lock(&fi->lock);
1807 list_add_tail(&req->list, &fi->queued_writes);
1808 fuse_flush_writepages(inode);
1809 spin_unlock(&fi->lock);
1811 for (i = 0; i < num_pages; i++)
1812 end_page_writeback(data->orig_pages[i]);
1816 * First recheck under fi->lock if the offending offset is still under
1817 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1818 * one already added for a page at this offset. If there's none, then insert
1819 * this new request onto the auxiliary list, otherwise reuse the existing one by
1820 * copying the new page contents over to the old temporary page.
1822 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1825 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1826 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1827 struct fuse_req *tmp;
1828 struct fuse_req *old_req;
1830 WARN_ON(new_req->num_pages != 0);
1832 spin_lock(&fi->lock);
1833 list_del(&new_req->writepages_entry);
1834 old_req = fuse_find_writeback(fi, page->index, page->index);
1836 list_add(&new_req->writepages_entry, &fi->writepages);
1837 spin_unlock(&fi->lock);
1841 new_req->num_pages = 1;
1842 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
1845 WARN_ON(tmp->inode != new_req->inode);
1846 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1847 if (curr_index == page->index) {
1848 WARN_ON(tmp->num_pages != 1);
1849 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
1850 swap(tmp->pages[0], new_req->pages[0]);
1856 new_req->misc.write.next = old_req->misc.write.next;
1857 old_req->misc.write.next = new_req;
1860 spin_unlock(&fi->lock);
1863 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
1865 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1866 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1867 wb_writeout_inc(&bdi->wb);
1868 fuse_writepage_free(fc, new_req);
1869 fuse_request_free(new_req);
1875 static int fuse_writepages_fill(struct page *page,
1876 struct writeback_control *wbc, void *_data)
1878 struct fuse_fill_wb_data *data = _data;
1879 struct fuse_req *req = data->req;
1880 struct inode *inode = data->inode;
1881 struct fuse_inode *fi = get_fuse_inode(inode);
1882 struct fuse_conn *fc = get_fuse_conn(inode);
1883 struct page *tmp_page;
1889 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1895 * Being under writeback is unlikely but possible. For example direct
1896 * read to an mmaped fuse file will set the page dirty twice; once when
1897 * the pages are faulted with get_user_pages(), and then after the read
1900 is_writeback = fuse_page_is_writeback(inode, page->index);
1902 if (req && req->num_pages &&
1903 (is_writeback || req->num_pages == fc->max_pages ||
1904 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1905 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1906 fuse_writepages_send(data);
1908 } else if (req && req->num_pages == req->max_pages) {
1909 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1910 fuse_writepages_send(data);
1911 req = data->req = NULL;
1916 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1921 * The page must not be redirtied until the writeout is completed
1922 * (i.e. userspace has sent a reply to the write request). Otherwise
1923 * there could be more than one temporary page instance for each real
1926 * This is ensured by holding the page lock in page_mkwrite() while
1927 * checking fuse_page_is_writeback(). We already hold the page lock
1928 * since clear_page_dirty_for_io() and keep it held until we add the
1929 * request to the fi->writepages list and increment req->num_pages.
1930 * After this fuse_page_is_writeback() will indicate that the page is
1931 * under writeback, so we can release the page lock.
1933 if (data->req == NULL) {
1934 struct fuse_inode *fi = get_fuse_inode(inode);
1937 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
1939 __free_page(tmp_page);
1943 fuse_write_fill(req, data->ff, page_offset(page), 0);
1944 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1945 req->misc.write.next = NULL;
1946 req->in.argpages = 1;
1947 __set_bit(FR_BACKGROUND, &req->flags);
1949 req->end = fuse_writepage_end;
1952 spin_lock(&fi->lock);
1953 list_add(&req->writepages_entry, &fi->writepages);
1954 spin_unlock(&fi->lock);
1958 set_page_writeback(page);
1960 copy_highpage(tmp_page, page);
1961 req->pages[req->num_pages] = tmp_page;
1962 req->page_descs[req->num_pages].offset = 0;
1963 req->page_descs[req->num_pages].length = PAGE_SIZE;
1965 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1966 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1969 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1970 end_page_writeback(page);
1974 data->orig_pages[req->num_pages] = page;
1977 * Protected by fi->lock against concurrent access by
1978 * fuse_page_is_writeback().
1980 spin_lock(&fi->lock);
1982 spin_unlock(&fi->lock);
1990 static int fuse_writepages(struct address_space *mapping,
1991 struct writeback_control *wbc)
1993 struct inode *inode = mapping->host;
1994 struct fuse_conn *fc = get_fuse_conn(inode);
1995 struct fuse_fill_wb_data data;
1999 if (is_bad_inode(inode))
2007 data.orig_pages = kcalloc(fc->max_pages,
2008 sizeof(struct page *),
2010 if (!data.orig_pages)
2013 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2015 /* Ignore errors if we can write at least one page */
2016 BUG_ON(!data.req->num_pages);
2017 fuse_writepages_send(&data);
2021 fuse_file_put(data.ff, false, false);
2023 kfree(data.orig_pages);
2029 * It's worthy to make sure that space is reserved on disk for the write,
2030 * but how to implement it without killing performance need more thinking.
2032 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2033 loff_t pos, unsigned len, unsigned flags,
2034 struct page **pagep, void **fsdata)
2036 pgoff_t index = pos >> PAGE_SHIFT;
2037 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2042 WARN_ON(!fc->writeback_cache);
2044 page = grab_cache_page_write_begin(mapping, index, flags);
2048 fuse_wait_on_page_writeback(mapping->host, page->index);
2050 if (PageUptodate(page) || len == PAGE_SIZE)
2053 * Check if the start this page comes after the end of file, in which
2054 * case the readpage can be optimized away.
2056 fsize = i_size_read(mapping->host);
2057 if (fsize <= (pos & PAGE_MASK)) {
2058 size_t off = pos & ~PAGE_MASK;
2060 zero_user_segment(page, 0, off);
2063 err = fuse_do_readpage(file, page);
2077 static int fuse_write_end(struct file *file, struct address_space *mapping,
2078 loff_t pos, unsigned len, unsigned copied,
2079 struct page *page, void *fsdata)
2081 struct inode *inode = page->mapping->host;
2083 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2087 if (!PageUptodate(page)) {
2088 /* Zero any unwritten bytes at the end of the page */
2089 size_t endoff = (pos + copied) & ~PAGE_MASK;
2091 zero_user_segment(page, endoff, PAGE_SIZE);
2092 SetPageUptodate(page);
2095 fuse_write_update_size(inode, pos + copied);
2096 set_page_dirty(page);
2105 static int fuse_launder_page(struct page *page)
2108 if (clear_page_dirty_for_io(page)) {
2109 struct inode *inode = page->mapping->host;
2110 err = fuse_writepage_locked(page);
2112 fuse_wait_on_page_writeback(inode, page->index);
2118 * Write back dirty pages now, because there may not be any suitable
2121 static void fuse_vma_close(struct vm_area_struct *vma)
2123 filemap_write_and_wait(vma->vm_file->f_mapping);
2127 * Wait for writeback against this page to complete before allowing it
2128 * to be marked dirty again, and hence written back again, possibly
2129 * before the previous writepage completed.
2131 * Block here, instead of in ->writepage(), so that the userspace fs
2132 * can only block processes actually operating on the filesystem.
2134 * Otherwise unprivileged userspace fs would be able to block
2139 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2141 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2143 struct page *page = vmf->page;
2144 struct inode *inode = file_inode(vmf->vma->vm_file);
2146 file_update_time(vmf->vma->vm_file);
2148 if (page->mapping != inode->i_mapping) {
2150 return VM_FAULT_NOPAGE;
2153 fuse_wait_on_page_writeback(inode, page->index);
2154 return VM_FAULT_LOCKED;
2157 static const struct vm_operations_struct fuse_file_vm_ops = {
2158 .close = fuse_vma_close,
2159 .fault = filemap_fault,
2160 .map_pages = filemap_map_pages,
2161 .page_mkwrite = fuse_page_mkwrite,
2164 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2166 struct fuse_file *ff = file->private_data;
2168 if (ff->open_flags & FOPEN_DIRECT_IO) {
2169 /* Can't provide the coherency needed for MAP_SHARED */
2170 if (vma->vm_flags & VM_MAYSHARE)
2173 invalidate_inode_pages2(file->f_mapping);
2175 return generic_file_mmap(file, vma);
2178 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2179 fuse_link_write_file(file);
2181 file_accessed(file);
2182 vma->vm_ops = &fuse_file_vm_ops;
2186 static int convert_fuse_file_lock(struct fuse_conn *fc,
2187 const struct fuse_file_lock *ffl,
2188 struct file_lock *fl)
2190 switch (ffl->type) {
2196 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2197 ffl->end < ffl->start)
2200 fl->fl_start = ffl->start;
2201 fl->fl_end = ffl->end;
2204 * Convert pid into init's pid namespace. The locks API will
2205 * translate it into the caller's pid namespace.
2208 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2215 fl->fl_type = ffl->type;
2219 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2220 const struct file_lock *fl, int opcode, pid_t pid,
2221 int flock, struct fuse_lk_in *inarg)
2223 struct inode *inode = file_inode(file);
2224 struct fuse_conn *fc = get_fuse_conn(inode);
2225 struct fuse_file *ff = file->private_data;
2227 memset(inarg, 0, sizeof(*inarg));
2229 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2230 inarg->lk.start = fl->fl_start;
2231 inarg->lk.end = fl->fl_end;
2232 inarg->lk.type = fl->fl_type;
2233 inarg->lk.pid = pid;
2235 inarg->lk_flags |= FUSE_LK_FLOCK;
2236 args->opcode = opcode;
2237 args->nodeid = get_node_id(inode);
2238 args->in_numargs = 1;
2239 args->in_args[0].size = sizeof(*inarg);
2240 args->in_args[0].value = inarg;
2243 static int fuse_getlk(struct file *file, struct file_lock *fl)
2245 struct inode *inode = file_inode(file);
2246 struct fuse_conn *fc = get_fuse_conn(inode);
2248 struct fuse_lk_in inarg;
2249 struct fuse_lk_out outarg;
2252 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2253 args.out_numargs = 1;
2254 args.out_args[0].size = sizeof(outarg);
2255 args.out_args[0].value = &outarg;
2256 err = fuse_simple_request(fc, &args);
2258 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2263 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2265 struct inode *inode = file_inode(file);
2266 struct fuse_conn *fc = get_fuse_conn(inode);
2268 struct fuse_lk_in inarg;
2269 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2270 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2271 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2274 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2275 /* NLM needs asynchronous locks, which we don't support yet */
2279 /* Unlock on close is handled by the flush method */
2280 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2283 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2284 err = fuse_simple_request(fc, &args);
2286 /* locking is restartable */
2293 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2295 struct inode *inode = file_inode(file);
2296 struct fuse_conn *fc = get_fuse_conn(inode);
2299 if (cmd == F_CANCELLK) {
2301 } else if (cmd == F_GETLK) {
2303 posix_test_lock(file, fl);
2306 err = fuse_getlk(file, fl);
2309 err = posix_lock_file(file, fl, NULL);
2311 err = fuse_setlk(file, fl, 0);
2316 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2318 struct inode *inode = file_inode(file);
2319 struct fuse_conn *fc = get_fuse_conn(inode);
2323 err = locks_lock_file_wait(file, fl);
2325 struct fuse_file *ff = file->private_data;
2327 /* emulate flock with POSIX locks */
2329 err = fuse_setlk(file, fl, 1);
2335 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2337 struct inode *inode = mapping->host;
2338 struct fuse_conn *fc = get_fuse_conn(inode);
2340 struct fuse_bmap_in inarg;
2341 struct fuse_bmap_out outarg;
2344 if (!inode->i_sb->s_bdev || fc->no_bmap)
2347 memset(&inarg, 0, sizeof(inarg));
2348 inarg.block = block;
2349 inarg.blocksize = inode->i_sb->s_blocksize;
2350 args.opcode = FUSE_BMAP;
2351 args.nodeid = get_node_id(inode);
2352 args.in_numargs = 1;
2353 args.in_args[0].size = sizeof(inarg);
2354 args.in_args[0].value = &inarg;
2355 args.out_numargs = 1;
2356 args.out_args[0].size = sizeof(outarg);
2357 args.out_args[0].value = &outarg;
2358 err = fuse_simple_request(fc, &args);
2362 return err ? 0 : outarg.block;
2365 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2367 struct inode *inode = file->f_mapping->host;
2368 struct fuse_conn *fc = get_fuse_conn(inode);
2369 struct fuse_file *ff = file->private_data;
2371 struct fuse_lseek_in inarg = {
2376 struct fuse_lseek_out outarg;
2382 args.opcode = FUSE_LSEEK;
2383 args.nodeid = ff->nodeid;
2384 args.in_numargs = 1;
2385 args.in_args[0].size = sizeof(inarg);
2386 args.in_args[0].value = &inarg;
2387 args.out_numargs = 1;
2388 args.out_args[0].size = sizeof(outarg);
2389 args.out_args[0].value = &outarg;
2390 err = fuse_simple_request(fc, &args);
2392 if (err == -ENOSYS) {
2399 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2402 err = fuse_update_attributes(inode, file);
2404 return generic_file_llseek(file, offset, whence);
2409 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2412 struct inode *inode = file_inode(file);
2417 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2418 retval = generic_file_llseek(file, offset, whence);
2422 retval = fuse_update_attributes(inode, file);
2424 retval = generic_file_llseek(file, offset, whence);
2425 inode_unlock(inode);
2430 retval = fuse_lseek(file, offset, whence);
2431 inode_unlock(inode);
2441 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2442 * ABI was defined to be 'struct iovec' which is different on 32bit
2443 * and 64bit. Fortunately we can determine which structure the server
2444 * used from the size of the reply.
2446 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2447 size_t transferred, unsigned count,
2450 #ifdef CONFIG_COMPAT
2451 if (count * sizeof(struct compat_iovec) == transferred) {
2452 struct compat_iovec *ciov = src;
2456 * With this interface a 32bit server cannot support
2457 * non-compat (i.e. ones coming from 64bit apps) ioctl
2463 for (i = 0; i < count; i++) {
2464 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2465 dst[i].iov_len = ciov[i].iov_len;
2471 if (count * sizeof(struct iovec) != transferred)
2474 memcpy(dst, src, transferred);
2478 /* Make sure iov_length() won't overflow */
2479 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2483 u32 max = fc->max_pages << PAGE_SHIFT;
2485 for (n = 0; n < count; n++, iov++) {
2486 if (iov->iov_len > (size_t) max)
2488 max -= iov->iov_len;
2493 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2494 void *src, size_t transferred, unsigned count,
2498 struct fuse_ioctl_iovec *fiov = src;
2500 if (fc->minor < 16) {
2501 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2505 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2508 for (i = 0; i < count; i++) {
2509 /* Did the server supply an inappropriate value? */
2510 if (fiov[i].base != (unsigned long) fiov[i].base ||
2511 fiov[i].len != (unsigned long) fiov[i].len)
2514 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2515 dst[i].iov_len = (size_t) fiov[i].len;
2517 #ifdef CONFIG_COMPAT
2519 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2520 (compat_size_t) dst[i].iov_len != fiov[i].len))
2530 * For ioctls, there is no generic way to determine how much memory
2531 * needs to be read and/or written. Furthermore, ioctls are allowed
2532 * to dereference the passed pointer, so the parameter requires deep
2533 * copying but FUSE has no idea whatsoever about what to copy in or
2536 * This is solved by allowing FUSE server to retry ioctl with
2537 * necessary in/out iovecs. Let's assume the ioctl implementation
2538 * needs to read in the following structure.
2545 * On the first callout to FUSE server, inarg->in_size and
2546 * inarg->out_size will be NULL; then, the server completes the ioctl
2547 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2548 * the actual iov array to
2550 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2552 * which tells FUSE to copy in the requested area and retry the ioctl.
2553 * On the second round, the server has access to the structure and
2554 * from that it can tell what to look for next, so on the invocation,
2555 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2557 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2558 * { .iov_base = a.buf, .iov_len = a.buflen } }
2560 * FUSE will copy both struct a and the pointed buffer from the
2561 * process doing the ioctl and retry ioctl with both struct a and the
2564 * This time, FUSE server has everything it needs and completes ioctl
2565 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2567 * Copying data out works the same way.
2569 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2570 * automatically initializes in and out iovs by decoding @cmd with
2571 * _IOC_* macros and the server is not allowed to request RETRY. This
2572 * limits ioctl data transfers to well-formed ioctls and is the forced
2573 * behavior for all FUSE servers.
2575 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2578 struct fuse_file *ff = file->private_data;
2579 struct fuse_conn *fc = ff->fc;
2580 struct fuse_ioctl_in inarg = {
2586 struct fuse_ioctl_out outarg;
2587 struct iovec *iov_page = NULL;
2588 struct iovec *in_iov = NULL, *out_iov = NULL;
2589 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2590 size_t in_size, out_size, c;
2591 ssize_t transferred;
2594 struct fuse_args_pages ap = {};
2596 #if BITS_PER_LONG == 32
2597 inarg.flags |= FUSE_IOCTL_32BIT;
2599 if (flags & FUSE_IOCTL_COMPAT) {
2600 inarg.flags |= FUSE_IOCTL_32BIT;
2601 #ifdef CONFIG_X86_X32
2602 if (in_x32_syscall())
2603 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2608 /* assume all the iovs returned by client always fits in a page */
2609 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2612 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2613 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2614 if (!ap.pages || !iov_page)
2617 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2620 * If restricted, initialize IO parameters as encoded in @cmd.
2621 * RETRY from server is not allowed.
2623 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2624 struct iovec *iov = iov_page;
2626 iov->iov_base = (void __user *)arg;
2627 iov->iov_len = _IOC_SIZE(cmd);
2629 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2634 if (_IOC_DIR(cmd) & _IOC_READ) {
2641 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2642 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2645 * Out data can be used either for actual out data or iovs,
2646 * make sure there always is at least one page.
2648 out_size = max_t(size_t, out_size, PAGE_SIZE);
2649 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2651 /* make sure there are enough buffer pages and init request with them */
2653 if (max_pages > fc->max_pages)
2655 while (ap.num_pages < max_pages) {
2656 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2657 if (!ap.pages[ap.num_pages])
2663 /* okay, let's send it to the client */
2664 ap.args.opcode = FUSE_IOCTL;
2665 ap.args.nodeid = ff->nodeid;
2666 ap.args.in_numargs = 1;
2667 ap.args.in_args[0].size = sizeof(inarg);
2668 ap.args.in_args[0].value = &inarg;
2670 ap.args.in_numargs++;
2671 ap.args.in_args[1].size = in_size;
2672 ap.args.in_pages = true;
2675 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2676 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2677 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2678 if (c != PAGE_SIZE && iov_iter_count(&ii))
2683 ap.args.out_numargs = 2;
2684 ap.args.out_args[0].size = sizeof(outarg);
2685 ap.args.out_args[0].value = &outarg;
2686 ap.args.out_args[1].size = out_size;
2687 ap.args.out_pages = true;
2688 ap.args.out_argvar = true;
2690 transferred = fuse_simple_request(fc, &ap.args);
2692 if (transferred < 0)
2695 /* did it ask for retry? */
2696 if (outarg.flags & FUSE_IOCTL_RETRY) {
2699 /* no retry if in restricted mode */
2701 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2704 in_iovs = outarg.in_iovs;
2705 out_iovs = outarg.out_iovs;
2708 * Make sure things are in boundary, separate checks
2709 * are to protect against overflow.
2712 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2713 out_iovs > FUSE_IOCTL_MAX_IOV ||
2714 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2717 vaddr = kmap_atomic(ap.pages[0]);
2718 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2719 transferred, in_iovs + out_iovs,
2720 (flags & FUSE_IOCTL_COMPAT) != 0);
2721 kunmap_atomic(vaddr);
2726 out_iov = in_iov + in_iovs;
2728 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2732 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2740 if (transferred > inarg.out_size)
2744 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2745 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2746 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2747 if (c != PAGE_SIZE && iov_iter_count(&ii))
2752 free_page((unsigned long) iov_page);
2753 while (ap.num_pages)
2754 __free_page(ap.pages[--ap.num_pages]);
2757 return err ? err : outarg.result;
2759 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2761 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2762 unsigned long arg, unsigned int flags)
2764 struct inode *inode = file_inode(file);
2765 struct fuse_conn *fc = get_fuse_conn(inode);
2767 if (!fuse_allow_current_process(fc))
2770 if (is_bad_inode(inode))
2773 return fuse_do_ioctl(file, cmd, arg, flags);
2776 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2779 return fuse_ioctl_common(file, cmd, arg, 0);
2782 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2785 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2789 * All files which have been polled are linked to RB tree
2790 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2791 * find the matching one.
2793 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2794 struct rb_node **parent_out)
2796 struct rb_node **link = &fc->polled_files.rb_node;
2797 struct rb_node *last = NULL;
2800 struct fuse_file *ff;
2803 ff = rb_entry(last, struct fuse_file, polled_node);
2806 link = &last->rb_left;
2807 else if (kh > ff->kh)
2808 link = &last->rb_right;
2819 * The file is about to be polled. Make sure it's on the polled_files
2820 * RB tree. Note that files once added to the polled_files tree are
2821 * not removed before the file is released. This is because a file
2822 * polled once is likely to be polled again.
2824 static void fuse_register_polled_file(struct fuse_conn *fc,
2825 struct fuse_file *ff)
2827 spin_lock(&fc->lock);
2828 if (RB_EMPTY_NODE(&ff->polled_node)) {
2829 struct rb_node **link, *uninitialized_var(parent);
2831 link = fuse_find_polled_node(fc, ff->kh, &parent);
2833 rb_link_node(&ff->polled_node, parent, link);
2834 rb_insert_color(&ff->polled_node, &fc->polled_files);
2836 spin_unlock(&fc->lock);
2839 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2841 struct fuse_file *ff = file->private_data;
2842 struct fuse_conn *fc = ff->fc;
2843 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2844 struct fuse_poll_out outarg;
2849 return DEFAULT_POLLMASK;
2851 poll_wait(file, &ff->poll_wait, wait);
2852 inarg.events = mangle_poll(poll_requested_events(wait));
2855 * Ask for notification iff there's someone waiting for it.
2856 * The client may ignore the flag and always notify.
2858 if (waitqueue_active(&ff->poll_wait)) {
2859 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2860 fuse_register_polled_file(fc, ff);
2863 args.opcode = FUSE_POLL;
2864 args.nodeid = ff->nodeid;
2865 args.in_numargs = 1;
2866 args.in_args[0].size = sizeof(inarg);
2867 args.in_args[0].value = &inarg;
2868 args.out_numargs = 1;
2869 args.out_args[0].size = sizeof(outarg);
2870 args.out_args[0].value = &outarg;
2871 err = fuse_simple_request(fc, &args);
2874 return demangle_poll(outarg.revents);
2875 if (err == -ENOSYS) {
2877 return DEFAULT_POLLMASK;
2881 EXPORT_SYMBOL_GPL(fuse_file_poll);
2884 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2885 * wakes up the poll waiters.
2887 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2888 struct fuse_notify_poll_wakeup_out *outarg)
2890 u64 kh = outarg->kh;
2891 struct rb_node **link;
2893 spin_lock(&fc->lock);
2895 link = fuse_find_polled_node(fc, kh, NULL);
2897 struct fuse_file *ff;
2899 ff = rb_entry(*link, struct fuse_file, polled_node);
2900 wake_up_interruptible_sync(&ff->poll_wait);
2903 spin_unlock(&fc->lock);
2907 static void fuse_do_truncate(struct file *file)
2909 struct inode *inode = file->f_mapping->host;
2912 attr.ia_valid = ATTR_SIZE;
2913 attr.ia_size = i_size_read(inode);
2915 attr.ia_file = file;
2916 attr.ia_valid |= ATTR_FILE;
2918 fuse_do_setattr(file_dentry(file), &attr, file);
2921 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2923 return round_up(off, fc->max_pages << PAGE_SHIFT);
2927 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2929 DECLARE_COMPLETION_ONSTACK(wait);
2931 struct file *file = iocb->ki_filp;
2932 struct fuse_file *ff = file->private_data;
2933 bool async_dio = ff->fc->async_dio;
2935 struct inode *inode;
2937 size_t count = iov_iter_count(iter);
2938 loff_t offset = iocb->ki_pos;
2939 struct fuse_io_priv *io;
2942 inode = file->f_mapping->host;
2943 i_size = i_size_read(inode);
2945 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2948 /* optimization for short read */
2949 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2950 if (offset >= i_size)
2952 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
2953 count = iov_iter_count(iter);
2956 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2959 spin_lock_init(&io->lock);
2960 kref_init(&io->refcnt);
2964 io->offset = offset;
2965 io->write = (iov_iter_rw(iter) == WRITE);
2968 * By default, we want to optimize all I/Os with async request
2969 * submission to the client filesystem if supported.
2971 io->async = async_dio;
2973 io->blocking = is_sync_kiocb(iocb);
2976 * We cannot asynchronously extend the size of a file.
2977 * In such case the aio will behave exactly like sync io.
2979 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2980 io->blocking = true;
2982 if (io->async && io->blocking) {
2984 * Additional reference to keep io around after
2985 * calling fuse_aio_complete()
2987 kref_get(&io->refcnt);
2991 if (iov_iter_rw(iter) == WRITE) {
2992 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2993 fuse_invalidate_attr(inode);
2995 ret = __fuse_direct_read(io, iter, &pos);
2999 bool blocking = io->blocking;
3001 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3003 /* we have a non-extending, async request, so return */
3005 return -EIOCBQUEUED;
3007 wait_for_completion(&wait);
3008 ret = fuse_get_res_by_io(io);
3011 kref_put(&io->refcnt, fuse_io_release);
3013 if (iov_iter_rw(iter) == WRITE) {
3015 fuse_write_update_size(inode, pos);
3016 else if (ret < 0 && offset + count > i_size)
3017 fuse_do_truncate(file);
3023 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3025 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3028 fuse_sync_writes(inode);
3033 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3036 struct fuse_file *ff = file->private_data;
3037 struct inode *inode = file_inode(file);
3038 struct fuse_inode *fi = get_fuse_inode(inode);
3039 struct fuse_conn *fc = ff->fc;
3041 struct fuse_fallocate_in inarg = {
3048 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3049 (mode & FALLOC_FL_PUNCH_HOLE);
3051 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3054 if (fc->no_fallocate)
3059 if (mode & FALLOC_FL_PUNCH_HOLE) {
3060 loff_t endbyte = offset + length - 1;
3062 err = fuse_writeback_range(inode, offset, endbyte);
3068 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3069 offset + length > i_size_read(inode)) {
3070 err = inode_newsize_ok(inode, offset + length);
3075 if (!(mode & FALLOC_FL_KEEP_SIZE))
3076 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3078 args.opcode = FUSE_FALLOCATE;
3079 args.nodeid = ff->nodeid;
3080 args.in_numargs = 1;
3081 args.in_args[0].size = sizeof(inarg);
3082 args.in_args[0].value = &inarg;
3083 err = fuse_simple_request(fc, &args);
3084 if (err == -ENOSYS) {
3085 fc->no_fallocate = 1;
3091 /* we could have extended the file */
3092 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3093 bool changed = fuse_write_update_size(inode, offset + length);
3095 if (changed && fc->writeback_cache)
3096 file_update_time(file);
3099 if (mode & FALLOC_FL_PUNCH_HOLE)
3100 truncate_pagecache_range(inode, offset, offset + length - 1);
3102 fuse_invalidate_attr(inode);
3105 if (!(mode & FALLOC_FL_KEEP_SIZE))
3106 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3109 inode_unlock(inode);
3114 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3115 struct file *file_out, loff_t pos_out,
3116 size_t len, unsigned int flags)
3118 struct fuse_file *ff_in = file_in->private_data;
3119 struct fuse_file *ff_out = file_out->private_data;
3120 struct inode *inode_in = file_inode(file_in);
3121 struct inode *inode_out = file_inode(file_out);
3122 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3123 struct fuse_conn *fc = ff_in->fc;
3125 struct fuse_copy_file_range_in inarg = {
3128 .nodeid_out = ff_out->nodeid,
3129 .fh_out = ff_out->fh,
3134 struct fuse_write_out outarg;
3136 /* mark unstable when write-back is not used, and file_out gets
3138 bool is_unstable = (!fc->writeback_cache) &&
3139 ((pos_out + len) > inode_out->i_size);
3141 if (fc->no_copy_file_range)
3144 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3147 if (fc->writeback_cache) {
3148 inode_lock(inode_in);
3149 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
3150 inode_unlock(inode_in);
3155 inode_lock(inode_out);
3157 err = file_modified(file_out);
3161 if (fc->writeback_cache) {
3162 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
3168 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3170 args.opcode = FUSE_COPY_FILE_RANGE;
3171 args.nodeid = ff_in->nodeid;
3172 args.in_numargs = 1;
3173 args.in_args[0].size = sizeof(inarg);
3174 args.in_args[0].value = &inarg;
3175 args.out_numargs = 1;
3176 args.out_args[0].size = sizeof(outarg);
3177 args.out_args[0].value = &outarg;
3178 err = fuse_simple_request(fc, &args);
3179 if (err == -ENOSYS) {
3180 fc->no_copy_file_range = 1;
3186 if (fc->writeback_cache) {
3187 fuse_write_update_size(inode_out, pos_out + outarg.size);
3188 file_update_time(file_out);
3191 fuse_invalidate_attr(inode_out);
3196 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3198 inode_unlock(inode_out);
3199 file_accessed(file_in);
3204 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3205 struct file *dst_file, loff_t dst_off,
3206 size_t len, unsigned int flags)
3210 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3213 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3214 ret = generic_copy_file_range(src_file, src_off, dst_file,
3215 dst_off, len, flags);
3219 static const struct file_operations fuse_file_operations = {
3220 .llseek = fuse_file_llseek,
3221 .read_iter = fuse_file_read_iter,
3222 .write_iter = fuse_file_write_iter,
3223 .mmap = fuse_file_mmap,
3225 .flush = fuse_flush,
3226 .release = fuse_release,
3227 .fsync = fuse_fsync,
3228 .lock = fuse_file_lock,
3229 .flock = fuse_file_flock,
3230 .splice_read = generic_file_splice_read,
3231 .splice_write = iter_file_splice_write,
3232 .unlocked_ioctl = fuse_file_ioctl,
3233 .compat_ioctl = fuse_file_compat_ioctl,
3234 .poll = fuse_file_poll,
3235 .fallocate = fuse_file_fallocate,
3236 .copy_file_range = fuse_copy_file_range,
3239 static const struct address_space_operations fuse_file_aops = {
3240 .readpage = fuse_readpage,
3241 .writepage = fuse_writepage,
3242 .writepages = fuse_writepages,
3243 .launder_page = fuse_launder_page,
3244 .readpages = fuse_readpages,
3245 .set_page_dirty = __set_page_dirty_nobuffers,
3247 .direct_IO = fuse_direct_IO,
3248 .write_begin = fuse_write_begin,
3249 .write_end = fuse_write_end,
3252 void fuse_init_file_inode(struct inode *inode)
3254 struct fuse_inode *fi = get_fuse_inode(inode);
3256 inode->i_fop = &fuse_file_operations;
3257 inode->i_data.a_ops = &fuse_file_aops;
3259 INIT_LIST_HEAD(&fi->write_files);
3260 INIT_LIST_HEAD(&fi->queued_writes);
3262 init_waitqueue_head(&fi->page_waitq);
3263 INIT_LIST_HEAD(&fi->writepages);