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;
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 (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
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 /* Default for no-open */
128 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
129 if (isdir ? !fc->no_opendir : !fc->no_open) {
130 struct fuse_open_out outarg;
133 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
136 ff->open_flags = outarg.open_flags;
138 } else if (err != -ENOSYS) {
150 ff->open_flags &= ~FOPEN_DIRECT_IO;
153 file->private_data = ff;
157 EXPORT_SYMBOL_GPL(fuse_do_open);
159 static void fuse_link_write_file(struct file *file)
161 struct inode *inode = file_inode(file);
162 struct fuse_inode *fi = get_fuse_inode(inode);
163 struct fuse_file *ff = file->private_data;
165 * file may be written through mmap, so chain it onto the
166 * inodes's write_file list
168 spin_lock(&fi->lock);
169 if (list_empty(&ff->write_entry))
170 list_add(&ff->write_entry, &fi->write_files);
171 spin_unlock(&fi->lock);
174 void fuse_finish_open(struct inode *inode, struct file *file)
176 struct fuse_file *ff = file->private_data;
177 struct fuse_conn *fc = get_fuse_conn(inode);
179 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
180 invalidate_inode_pages2(inode->i_mapping);
181 if (ff->open_flags & FOPEN_STREAM)
182 stream_open(inode, file);
183 else if (ff->open_flags & FOPEN_NONSEEKABLE)
184 nonseekable_open(inode, file);
185 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
186 struct fuse_inode *fi = get_fuse_inode(inode);
188 spin_lock(&fi->lock);
189 fi->attr_version = atomic64_inc_return(&fc->attr_version);
190 i_size_write(inode, 0);
191 spin_unlock(&fi->lock);
192 fuse_invalidate_attr(inode);
193 if (fc->writeback_cache)
194 file_update_time(file);
196 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
197 fuse_link_write_file(file);
200 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
202 struct fuse_conn *fc = get_fuse_conn(inode);
204 bool lock_inode = (file->f_flags & O_TRUNC) &&
205 fc->atomic_o_trunc &&
208 err = generic_file_open(inode, file);
215 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
218 fuse_finish_open(inode, file);
226 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
227 int flags, int opcode)
229 struct fuse_conn *fc = ff->fc;
230 struct fuse_req *req = ff->reserved_req;
231 struct fuse_release_in *inarg = &req->misc.release.in;
233 /* Inode is NULL on error path of fuse_create_open() */
235 spin_lock(&fi->lock);
236 list_del(&ff->write_entry);
237 spin_unlock(&fi->lock);
239 spin_lock(&fc->lock);
240 if (!RB_EMPTY_NODE(&ff->polled_node))
241 rb_erase(&ff->polled_node, &fc->polled_files);
242 spin_unlock(&fc->lock);
244 wake_up_interruptible_all(&ff->poll_wait);
247 inarg->flags = flags;
248 req->in.h.opcode = opcode;
249 req->in.h.nodeid = ff->nodeid;
251 req->in.args[0].size = sizeof(struct fuse_release_in);
252 req->in.args[0].value = inarg;
255 void fuse_release_common(struct file *file, bool isdir)
257 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
258 struct fuse_file *ff = file->private_data;
259 struct fuse_req *req = ff->reserved_req;
260 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
262 fuse_prepare_release(fi, ff, file->f_flags, opcode);
265 struct fuse_release_in *inarg = &req->misc.release.in;
266 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
267 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
270 /* Hold inode until release is finished */
271 req->misc.release.inode = igrab(file_inode(file));
274 * Normally this will send the RELEASE request, however if
275 * some asynchronous READ or WRITE requests are outstanding,
276 * the sending will be delayed.
278 * Make the release synchronous if this is a fuseblk mount,
279 * synchronous RELEASE is allowed (and desirable) in this case
280 * because the server can be trusted not to screw up.
282 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
285 static int fuse_open(struct inode *inode, struct file *file)
287 return fuse_open_common(inode, file, false);
290 static int fuse_release(struct inode *inode, struct file *file)
292 struct fuse_conn *fc = get_fuse_conn(inode);
294 /* see fuse_vma_close() for !writeback_cache case */
295 if (fc->writeback_cache)
296 write_inode_now(inode, 1);
298 fuse_release_common(file, false);
300 /* return value is ignored by VFS */
304 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
306 WARN_ON(refcount_read(&ff->count) > 1);
307 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
309 * iput(NULL) is a no-op and since the refcount is 1 and everything's
310 * synchronous, we are fine with not doing igrab() here"
312 fuse_file_put(ff, true, false);
314 EXPORT_SYMBOL_GPL(fuse_sync_release);
317 * Scramble the ID space with XTEA, so that the value of the files_struct
318 * pointer is not exposed to userspace.
320 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
322 u32 *k = fc->scramble_key;
323 u64 v = (unsigned long) id;
329 for (i = 0; i < 32; i++) {
330 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
332 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
335 return (u64) v0 + ((u64) v1 << 32);
338 static struct fuse_req *fuse_find_writeback(struct fuse_inode *fi,
339 pgoff_t idx_from, pgoff_t idx_to)
341 struct fuse_req *req;
343 list_for_each_entry(req, &fi->writepages, writepages_entry) {
346 WARN_ON(get_fuse_inode(req->inode) != fi);
347 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
348 if (idx_from < curr_index + req->num_pages &&
349 curr_index <= idx_to) {
357 * Check if any page in a range is under writeback
359 * This is currently done by walking the list of writepage requests
360 * for the inode, which can be pretty inefficient.
362 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
365 struct fuse_inode *fi = get_fuse_inode(inode);
368 spin_lock(&fi->lock);
369 found = fuse_find_writeback(fi, idx_from, idx_to);
370 spin_unlock(&fi->lock);
375 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
377 return fuse_range_is_writeback(inode, index, index);
381 * Wait for page writeback to be completed.
383 * Since fuse doesn't rely on the VM writeback tracking, this has to
384 * use some other means.
386 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
388 struct fuse_inode *fi = get_fuse_inode(inode);
390 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
394 * Wait for all pending writepages on the inode to finish.
396 * This is currently done by blocking further writes with FUSE_NOWRITE
397 * and waiting for all sent writes to complete.
399 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
400 * could conflict with truncation.
402 static void fuse_sync_writes(struct inode *inode)
404 fuse_set_nowrite(inode);
405 fuse_release_nowrite(inode);
408 static int fuse_flush(struct file *file, fl_owner_t id)
410 struct inode *inode = file_inode(file);
411 struct fuse_conn *fc = get_fuse_conn(inode);
412 struct fuse_file *ff = file->private_data;
413 struct fuse_req *req;
414 struct fuse_flush_in inarg;
417 if (is_bad_inode(inode))
423 err = write_inode_now(inode, 1);
428 fuse_sync_writes(inode);
431 err = filemap_check_errors(file->f_mapping);
435 req = fuse_get_req_nofail_nopages(fc);
436 memset(&inarg, 0, sizeof(inarg));
438 inarg.lock_owner = fuse_lock_owner_id(fc, id);
439 req->in.h.opcode = FUSE_FLUSH;
440 req->in.h.nodeid = get_node_id(inode);
442 req->in.args[0].size = sizeof(inarg);
443 req->in.args[0].value = &inarg;
444 __set_bit(FR_FORCE, &req->flags);
445 fuse_request_send(fc, req);
446 err = req->out.h.error;
447 fuse_put_request(fc, req);
448 if (err == -ENOSYS) {
455 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
456 int datasync, int opcode)
458 struct inode *inode = file->f_mapping->host;
459 struct fuse_conn *fc = get_fuse_conn(inode);
460 struct fuse_file *ff = file->private_data;
462 struct fuse_fsync_in inarg;
464 memset(&inarg, 0, sizeof(inarg));
466 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
467 args.opcode = opcode;
468 args.nodeid = get_node_id(inode);
470 args.in_args[0].size = sizeof(inarg);
471 args.in_args[0].value = &inarg;
472 return fuse_simple_request(fc, &args);
475 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
478 struct inode *inode = file->f_mapping->host;
479 struct fuse_conn *fc = get_fuse_conn(inode);
482 if (is_bad_inode(inode))
488 * Start writeback against all dirty pages of the inode, then
489 * wait for all outstanding writes, before sending the FSYNC
492 err = file_write_and_wait_range(file, start, end);
496 fuse_sync_writes(inode);
499 * Due to implementation of fuse writeback
500 * file_write_and_wait_range() does not catch errors.
501 * We have to do this directly after fuse_sync_writes()
503 err = file_check_and_advance_wb_err(file);
507 err = sync_inode_metadata(inode, 1);
514 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
515 if (err == -ENOSYS) {
525 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
526 size_t count, int opcode)
528 struct fuse_read_in *inarg = &req->misc.read.in;
529 struct fuse_file *ff = file->private_data;
534 inarg->flags = file->f_flags;
535 req->in.h.opcode = opcode;
536 req->in.h.nodeid = ff->nodeid;
538 req->in.args[0].size = sizeof(struct fuse_read_in);
539 req->in.args[0].value = inarg;
541 req->out.numargs = 1;
542 req->out.args[0].size = count;
545 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
549 for (i = 0; i < req->num_pages; i++) {
550 struct page *page = req->pages[i];
552 set_page_dirty_lock(page);
557 static void fuse_io_release(struct kref *kref)
559 kfree(container_of(kref, struct fuse_io_priv, refcnt));
562 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
567 if (io->bytes >= 0 && io->write)
570 return io->bytes < 0 ? io->size : io->bytes;
574 * In case of short read, the caller sets 'pos' to the position of
575 * actual end of fuse request in IO request. Otherwise, if bytes_requested
576 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
579 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
580 * both submitted asynchronously. The first of them was ACKed by userspace as
581 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
582 * second request was ACKed as short, e.g. only 1K was read, resulting in
585 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
586 * will be equal to the length of the longest contiguous fragment of
587 * transferred data starting from the beginning of IO request.
589 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
593 spin_lock(&io->lock);
595 io->err = io->err ? : err;
596 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
600 if (!left && io->blocking)
602 spin_unlock(&io->lock);
604 if (!left && !io->blocking) {
605 ssize_t res = fuse_get_res_by_io(io);
608 struct inode *inode = file_inode(io->iocb->ki_filp);
609 struct fuse_conn *fc = get_fuse_conn(inode);
610 struct fuse_inode *fi = get_fuse_inode(inode);
612 spin_lock(&fi->lock);
613 fi->attr_version = atomic64_inc_return(&fc->attr_version);
614 spin_unlock(&fi->lock);
617 io->iocb->ki_complete(io->iocb, res, 0);
620 kref_put(&io->refcnt, fuse_io_release);
623 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
625 struct fuse_io_priv *io = req->io;
628 fuse_release_user_pages(req, io->should_dirty);
631 if (req->misc.write.in.size != req->misc.write.out.size)
632 pos = req->misc.write.in.offset - io->offset +
633 req->misc.write.out.size;
635 if (req->misc.read.in.size != req->out.args[0].size)
636 pos = req->misc.read.in.offset - io->offset +
637 req->out.args[0].size;
640 fuse_aio_complete(io, req->out.h.error, pos);
643 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
644 size_t num_bytes, struct fuse_io_priv *io)
646 spin_lock(&io->lock);
647 kref_get(&io->refcnt);
648 io->size += num_bytes;
650 spin_unlock(&io->lock);
653 req->end = fuse_aio_complete_req;
655 __fuse_get_request(req);
656 fuse_request_send_background(fc, req);
661 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
662 loff_t pos, size_t count, fl_owner_t owner)
664 struct file *file = io->iocb->ki_filp;
665 struct fuse_file *ff = file->private_data;
666 struct fuse_conn *fc = ff->fc;
668 fuse_read_fill(req, file, pos, count, FUSE_READ);
670 struct fuse_read_in *inarg = &req->misc.read.in;
672 inarg->read_flags |= FUSE_READ_LOCKOWNER;
673 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
677 return fuse_async_req_send(fc, req, count, io);
679 fuse_request_send(fc, req);
680 return req->out.args[0].size;
683 static void fuse_read_update_size(struct inode *inode, loff_t size,
686 struct fuse_conn *fc = get_fuse_conn(inode);
687 struct fuse_inode *fi = get_fuse_inode(inode);
689 spin_lock(&fi->lock);
690 if (attr_ver == fi->attr_version && size < inode->i_size &&
691 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
692 fi->attr_version = atomic64_inc_return(&fc->attr_version);
693 i_size_write(inode, size);
695 spin_unlock(&fi->lock);
698 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
701 size_t num_read = req->out.args[0].size;
702 struct fuse_conn *fc = get_fuse_conn(inode);
704 if (fc->writeback_cache) {
706 * A hole in a file. Some data after the hole are in page cache,
707 * but have not reached the client fs yet. So, the hole is not
711 int start_idx = num_read >> PAGE_SHIFT;
712 size_t off = num_read & (PAGE_SIZE - 1);
714 for (i = start_idx; i < req->num_pages; i++) {
715 zero_user_segment(req->pages[i], off, PAGE_SIZE);
719 loff_t pos = page_offset(req->pages[0]) + num_read;
720 fuse_read_update_size(inode, pos, attr_ver);
724 static int fuse_do_readpage(struct file *file, struct page *page)
727 struct fuse_io_priv io;
728 struct inode *inode = page->mapping->host;
729 struct fuse_conn *fc = get_fuse_conn(inode);
730 struct fuse_req *req;
732 loff_t pos = page_offset(page);
733 size_t count = PAGE_SIZE;
738 * Page writeback can extend beyond the lifetime of the
739 * page-cache page, so make sure we read a properly synced
742 fuse_wait_on_page_writeback(inode, page->index);
744 req = fuse_get_req(fc, 1);
748 attr_ver = fuse_get_attr_version(fc);
750 req->out.page_zeroing = 1;
751 req->out.argpages = 1;
753 req->pages[0] = page;
754 req->page_descs[0].length = count;
755 init_sync_kiocb(&iocb, file);
756 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
757 num_read = fuse_send_read(req, &io, pos, count, NULL);
758 err = req->out.h.error;
762 * Short read means EOF. If file size is larger, truncate it
764 if (num_read < count)
765 fuse_short_read(req, inode, attr_ver);
767 SetPageUptodate(page);
770 fuse_put_request(fc, req);
775 static int fuse_readpage(struct file *file, struct page *page)
777 struct inode *inode = page->mapping->host;
781 if (is_bad_inode(inode))
784 err = fuse_do_readpage(file, page);
785 fuse_invalidate_atime(inode);
791 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
794 size_t count = req->misc.read.in.size;
795 size_t num_read = req->out.args[0].size;
796 struct address_space *mapping = NULL;
798 for (i = 0; mapping == NULL && i < req->num_pages; i++)
799 mapping = req->pages[i]->mapping;
802 struct inode *inode = mapping->host;
805 * Short read means EOF. If file size is larger, truncate it
807 if (!req->out.h.error && num_read < count)
808 fuse_short_read(req, inode, req->misc.read.attr_ver);
810 fuse_invalidate_atime(inode);
813 for (i = 0; i < req->num_pages; i++) {
814 struct page *page = req->pages[i];
815 if (!req->out.h.error)
816 SetPageUptodate(page);
823 fuse_file_put(req->ff, false, false);
826 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
828 struct fuse_file *ff = file->private_data;
829 struct fuse_conn *fc = ff->fc;
830 loff_t pos = page_offset(req->pages[0]);
831 size_t count = req->num_pages << PAGE_SHIFT;
833 req->out.argpages = 1;
834 req->out.page_zeroing = 1;
835 req->out.page_replace = 1;
836 fuse_read_fill(req, file, pos, count, FUSE_READ);
837 req->misc.read.attr_ver = fuse_get_attr_version(fc);
838 if (fc->async_read) {
839 req->ff = fuse_file_get(ff);
840 req->end = fuse_readpages_end;
841 fuse_request_send_background(fc, req);
843 fuse_request_send(fc, req);
844 fuse_readpages_end(fc, req);
845 fuse_put_request(fc, req);
849 struct fuse_fill_data {
850 struct fuse_req *req;
856 static int fuse_readpages_fill(void *_data, struct page *page)
858 struct fuse_fill_data *data = _data;
859 struct fuse_req *req = data->req;
860 struct inode *inode = data->inode;
861 struct fuse_conn *fc = get_fuse_conn(inode);
863 fuse_wait_on_page_writeback(inode, page->index);
865 if (req->num_pages &&
866 (req->num_pages == fc->max_pages ||
867 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
868 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
869 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
871 fuse_send_readpages(req, data->file);
873 req = fuse_get_req_for_background(fc, nr_alloc);
875 req = fuse_get_req(fc, nr_alloc);
884 if (WARN_ON(req->num_pages >= req->max_pages)) {
886 fuse_put_request(fc, req);
891 req->pages[req->num_pages] = page;
892 req->page_descs[req->num_pages].length = PAGE_SIZE;
898 static int fuse_readpages(struct file *file, struct address_space *mapping,
899 struct list_head *pages, unsigned nr_pages)
901 struct inode *inode = mapping->host;
902 struct fuse_conn *fc = get_fuse_conn(inode);
903 struct fuse_fill_data data;
905 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
908 if (is_bad_inode(inode))
914 data.req = fuse_get_req_for_background(fc, nr_alloc);
916 data.req = fuse_get_req(fc, nr_alloc);
917 data.nr_pages = nr_pages;
918 err = PTR_ERR(data.req);
919 if (IS_ERR(data.req))
922 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
924 if (data.req->num_pages)
925 fuse_send_readpages(data.req, file);
927 fuse_put_request(fc, data.req);
933 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
935 struct inode *inode = iocb->ki_filp->f_mapping->host;
936 struct fuse_conn *fc = get_fuse_conn(inode);
939 * In auto invalidate mode, always update attributes on read.
940 * Otherwise, only update if we attempt to read past EOF (to ensure
941 * i_size is up to date).
943 if (fc->auto_inval_data ||
944 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
946 err = fuse_update_attributes(inode, iocb->ki_filp);
951 return generic_file_read_iter(iocb, to);
954 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
955 loff_t pos, size_t count)
957 struct fuse_write_in *inarg = &req->misc.write.in;
958 struct fuse_write_out *outarg = &req->misc.write.out;
963 req->in.h.opcode = FUSE_WRITE;
964 req->in.h.nodeid = ff->nodeid;
966 if (ff->fc->minor < 9)
967 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
969 req->in.args[0].size = sizeof(struct fuse_write_in);
970 req->in.args[0].value = inarg;
971 req->in.args[1].size = count;
972 req->out.numargs = 1;
973 req->out.args[0].size = sizeof(struct fuse_write_out);
974 req->out.args[0].value = outarg;
977 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
978 loff_t pos, size_t count, fl_owner_t owner)
980 struct kiocb *iocb = io->iocb;
981 struct file *file = iocb->ki_filp;
982 struct fuse_file *ff = file->private_data;
983 struct fuse_conn *fc = ff->fc;
984 struct fuse_write_in *inarg = &req->misc.write.in;
986 fuse_write_fill(req, ff, pos, count);
987 inarg->flags = file->f_flags;
988 if (iocb->ki_flags & IOCB_DSYNC)
989 inarg->flags |= O_DSYNC;
990 if (iocb->ki_flags & IOCB_SYNC)
991 inarg->flags |= O_SYNC;
993 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
994 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
998 return fuse_async_req_send(fc, req, count, io);
1000 fuse_request_send(fc, req);
1001 return req->misc.write.out.size;
1004 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1006 struct fuse_conn *fc = get_fuse_conn(inode);
1007 struct fuse_inode *fi = get_fuse_inode(inode);
1010 spin_lock(&fi->lock);
1011 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1012 if (pos > inode->i_size) {
1013 i_size_write(inode, pos);
1016 spin_unlock(&fi->lock);
1021 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1022 struct inode *inode, loff_t pos,
1028 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1030 for (i = 0; i < req->num_pages; i++)
1031 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1033 res = fuse_send_write(req, &io, pos, count, NULL);
1035 offset = req->page_descs[0].offset;
1037 for (i = 0; i < req->num_pages; i++) {
1038 struct page *page = req->pages[i];
1040 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1041 SetPageUptodate(page);
1043 if (count > PAGE_SIZE - offset)
1044 count -= PAGE_SIZE - offset;
1056 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1057 struct address_space *mapping,
1058 struct iov_iter *ii, loff_t pos)
1060 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1061 unsigned offset = pos & (PAGE_SIZE - 1);
1065 req->in.argpages = 1;
1066 req->page_descs[0].offset = offset;
1071 pgoff_t index = pos >> PAGE_SHIFT;
1072 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1073 iov_iter_count(ii));
1075 bytes = min_t(size_t, bytes, fc->max_write - count);
1079 if (iov_iter_fault_in_readable(ii, bytes))
1083 page = grab_cache_page_write_begin(mapping, index, 0);
1087 if (mapping_writably_mapped(mapping))
1088 flush_dcache_page(page);
1090 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1091 flush_dcache_page(page);
1093 iov_iter_advance(ii, tmp);
1097 bytes = min(bytes, iov_iter_single_seg_count(ii));
1102 req->pages[req->num_pages] = page;
1103 req->page_descs[req->num_pages].length = tmp;
1109 if (offset == PAGE_SIZE)
1112 if (!fc->big_writes)
1114 } while (iov_iter_count(ii) && count < fc->max_write &&
1115 req->num_pages < req->max_pages && offset == 0);
1117 return count > 0 ? count : err;
1120 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1121 unsigned int max_pages)
1123 return min_t(unsigned int,
1124 ((pos + len - 1) >> PAGE_SHIFT) -
1125 (pos >> PAGE_SHIFT) + 1,
1129 static ssize_t fuse_perform_write(struct kiocb *iocb,
1130 struct address_space *mapping,
1131 struct iov_iter *ii, loff_t pos)
1133 struct inode *inode = mapping->host;
1134 struct fuse_conn *fc = get_fuse_conn(inode);
1135 struct fuse_inode *fi = get_fuse_inode(inode);
1139 if (inode->i_size < pos + iov_iter_count(ii))
1140 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1143 struct fuse_req *req;
1145 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1148 req = fuse_get_req(fc, nr_pages);
1154 count = fuse_fill_write_pages(req, mapping, ii, pos);
1160 num_written = fuse_send_write_pages(req, iocb, inode,
1162 err = req->out.h.error;
1167 /* break out of the loop on short write */
1168 if (num_written != count)
1172 fuse_put_request(fc, req);
1173 } while (!err && iov_iter_count(ii));
1176 fuse_write_update_size(inode, pos);
1178 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1179 fuse_invalidate_attr(inode);
1181 return res > 0 ? res : err;
1184 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1186 struct file *file = iocb->ki_filp;
1187 struct address_space *mapping = file->f_mapping;
1188 ssize_t written = 0;
1189 ssize_t written_buffered = 0;
1190 struct inode *inode = mapping->host;
1194 if (get_fuse_conn(inode)->writeback_cache) {
1195 /* Update size (EOF optimization) and mode (SUID clearing) */
1196 err = fuse_update_attributes(mapping->host, file);
1200 return generic_file_write_iter(iocb, from);
1205 /* We can write back this queue in page reclaim */
1206 current->backing_dev_info = inode_to_bdi(inode);
1208 err = generic_write_checks(iocb, from);
1212 err = file_remove_privs(file);
1216 err = file_update_time(file);
1220 if (iocb->ki_flags & IOCB_DIRECT) {
1221 loff_t pos = iocb->ki_pos;
1222 written = generic_file_direct_write(iocb, from);
1223 if (written < 0 || !iov_iter_count(from))
1228 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1229 if (written_buffered < 0) {
1230 err = written_buffered;
1233 endbyte = pos + written_buffered - 1;
1235 err = filemap_write_and_wait_range(file->f_mapping, pos,
1240 invalidate_mapping_pages(file->f_mapping,
1242 endbyte >> PAGE_SHIFT);
1244 written += written_buffered;
1245 iocb->ki_pos = pos + written_buffered;
1247 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1249 iocb->ki_pos += written;
1252 current->backing_dev_info = NULL;
1253 inode_unlock(inode);
1255 written = generic_write_sync(iocb, written);
1257 return written ? written : err;
1260 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1261 unsigned index, unsigned nr_pages)
1265 for (i = index; i < index + nr_pages; i++)
1266 req->page_descs[i].length = PAGE_SIZE -
1267 req->page_descs[i].offset;
1270 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1272 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1275 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1278 return min(iov_iter_single_seg_count(ii), max_size);
1281 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1282 size_t *nbytesp, int write)
1284 size_t nbytes = 0; /* # bytes already packed in req */
1287 /* Special case for kernel I/O: can copy directly into the buffer */
1288 if (iov_iter_is_kvec(ii)) {
1289 unsigned long user_addr = fuse_get_user_addr(ii);
1290 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1293 req->in.args[1].value = (void *) user_addr;
1295 req->out.args[0].value = (void *) user_addr;
1297 iov_iter_advance(ii, frag_size);
1298 *nbytesp = frag_size;
1302 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1305 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1307 req->max_pages - req->num_pages,
1312 iov_iter_advance(ii, ret);
1316 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1318 req->page_descs[req->num_pages].offset = start;
1319 fuse_page_descs_length_init(req, req->num_pages, npages);
1321 req->num_pages += npages;
1322 req->page_descs[req->num_pages - 1].length -=
1323 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1327 req->in.argpages = 1;
1329 req->out.argpages = 1;
1333 return ret < 0 ? ret : 0;
1336 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1337 loff_t *ppos, int flags)
1339 int write = flags & FUSE_DIO_WRITE;
1340 int cuse = flags & FUSE_DIO_CUSE;
1341 struct file *file = io->iocb->ki_filp;
1342 struct inode *inode = file->f_mapping->host;
1343 struct fuse_file *ff = file->private_data;
1344 struct fuse_conn *fc = ff->fc;
1345 size_t nmax = write ? fc->max_write : fc->max_read;
1347 size_t count = iov_iter_count(iter);
1348 pgoff_t idx_from = pos >> PAGE_SHIFT;
1349 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1351 struct fuse_req *req;
1355 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1358 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
1360 return PTR_ERR(req);
1362 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1365 fuse_sync_writes(inode);
1367 inode_unlock(inode);
1370 io->should_dirty = !write && iter_is_iovec(iter);
1373 fl_owner_t owner = current->files;
1374 size_t nbytes = min(count, nmax);
1375 err = fuse_get_user_pages(req, iter, &nbytes, write);
1380 if (!capable(CAP_FSETID)) {
1381 struct fuse_write_in *inarg;
1383 inarg = &req->misc.write.in;
1384 inarg->write_flags |= FUSE_WRITE_KILL_PRIV;
1386 nres = fuse_send_write(req, io, pos, nbytes, owner);
1388 nres = fuse_send_read(req, io, pos, nbytes, owner);
1392 fuse_release_user_pages(req, io->should_dirty);
1393 if (req->out.h.error) {
1394 err = req->out.h.error;
1396 } else if (nres > nbytes) {
1407 fuse_put_request(fc, req);
1409 req = fuse_get_req_for_background(fc,
1410 iov_iter_npages(iter, fc->max_pages));
1412 req = fuse_get_req(fc, iov_iter_npages(iter,
1419 fuse_put_request(fc, req);
1423 return res > 0 ? res : err;
1425 EXPORT_SYMBOL_GPL(fuse_direct_io);
1427 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1428 struct iov_iter *iter,
1432 struct inode *inode = file_inode(io->iocb->ki_filp);
1434 res = fuse_direct_io(io, iter, ppos, 0);
1436 fuse_invalidate_atime(inode);
1441 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1443 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1447 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1448 res = fuse_direct_IO(iocb, to);
1450 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1452 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1458 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1460 struct inode *inode = file_inode(iocb->ki_filp);
1461 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1464 /* Don't allow parallel writes to the same file */
1466 res = generic_write_checks(iocb, from);
1468 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1469 res = fuse_direct_IO(iocb, from);
1471 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1475 fuse_invalidate_attr(inode);
1477 fuse_write_update_size(inode, iocb->ki_pos);
1478 inode_unlock(inode);
1483 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1485 struct file *file = iocb->ki_filp;
1486 struct fuse_file *ff = file->private_data;
1488 if (is_bad_inode(file_inode(file)))
1491 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1492 return fuse_cache_read_iter(iocb, to);
1494 return fuse_direct_read_iter(iocb, to);
1497 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1499 struct file *file = iocb->ki_filp;
1500 struct fuse_file *ff = file->private_data;
1502 if (is_bad_inode(file_inode(file)))
1505 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1506 return fuse_cache_write_iter(iocb, from);
1508 return fuse_direct_write_iter(iocb, from);
1511 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1515 for (i = 0; i < req->num_pages; i++)
1516 __free_page(req->pages[i]);
1519 fuse_file_put(req->ff, false, false);
1522 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1524 struct inode *inode = req->inode;
1525 struct fuse_inode *fi = get_fuse_inode(inode);
1526 struct backing_dev_info *bdi = inode_to_bdi(inode);
1529 list_del(&req->writepages_entry);
1530 for (i = 0; i < req->num_pages; i++) {
1531 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1532 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1533 wb_writeout_inc(&bdi->wb);
1535 wake_up(&fi->page_waitq);
1538 /* Called under fi->lock, may release and reacquire it */
1539 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1541 __releases(fi->lock)
1542 __acquires(fi->lock)
1544 struct fuse_req *aux, *next;
1545 struct fuse_inode *fi = get_fuse_inode(req->inode);
1546 struct fuse_write_in *inarg = &req->misc.write.in;
1547 __u64 data_size = req->num_pages * PAGE_SIZE;
1550 if (inarg->offset + data_size <= size) {
1551 inarg->size = data_size;
1552 } else if (inarg->offset < size) {
1553 inarg->size = size - inarg->offset;
1555 /* Got truncated off completely */
1559 req->in.args[1].size = inarg->size;
1560 queued = fuse_request_queue_background(fc, req);
1561 /* Fails on broken connection only */
1562 if (unlikely(!queued))
1569 fuse_writepage_finish(fc, req);
1570 spin_unlock(&fi->lock);
1572 /* After fuse_writepage_finish() aux request list is private */
1573 for (aux = req->misc.write.next; aux; aux = next) {
1574 next = aux->misc.write.next;
1575 aux->misc.write.next = NULL;
1576 fuse_writepage_free(fc, aux);
1577 fuse_put_request(fc, aux);
1580 fuse_writepage_free(fc, req);
1581 fuse_put_request(fc, req);
1582 spin_lock(&fi->lock);
1586 * If fi->writectr is positive (no truncate or fsync going on) send
1587 * all queued writepage requests.
1589 * Called with fi->lock
1591 void fuse_flush_writepages(struct inode *inode)
1592 __releases(fi->lock)
1593 __acquires(fi->lock)
1595 struct fuse_conn *fc = get_fuse_conn(inode);
1596 struct fuse_inode *fi = get_fuse_inode(inode);
1597 loff_t crop = i_size_read(inode);
1598 struct fuse_req *req;
1600 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1601 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1602 list_del_init(&req->list);
1603 fuse_send_writepage(fc, req, crop);
1607 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1609 struct inode *inode = req->inode;
1610 struct fuse_inode *fi = get_fuse_inode(inode);
1612 mapping_set_error(inode->i_mapping, req->out.h.error);
1613 spin_lock(&fi->lock);
1614 while (req->misc.write.next) {
1615 struct fuse_conn *fc = get_fuse_conn(inode);
1616 struct fuse_write_in *inarg = &req->misc.write.in;
1617 struct fuse_req *next = req->misc.write.next;
1618 req->misc.write.next = next->misc.write.next;
1619 next->misc.write.next = NULL;
1620 next->ff = fuse_file_get(req->ff);
1621 list_add(&next->writepages_entry, &fi->writepages);
1624 * Skip fuse_flush_writepages() to make it easy to crop requests
1625 * based on primary request size.
1627 * 1st case (trivial): there are no concurrent activities using
1628 * fuse_set/release_nowrite. Then we're on safe side because
1629 * fuse_flush_writepages() would call fuse_send_writepage()
1632 * 2nd case: someone called fuse_set_nowrite and it is waiting
1633 * now for completion of all in-flight requests. This happens
1634 * rarely and no more than once per page, so this should be
1637 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1638 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1639 * that fuse_set_nowrite returned implies that all in-flight
1640 * requests were completed along with all of their secondary
1641 * requests. Further primary requests are blocked by negative
1642 * writectr. Hence there cannot be any in-flight requests and
1643 * no invocations of fuse_writepage_end() while we're in
1644 * fuse_set_nowrite..fuse_release_nowrite section.
1646 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1649 fuse_writepage_finish(fc, req);
1650 spin_unlock(&fi->lock);
1651 fuse_writepage_free(fc, req);
1654 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1655 struct fuse_inode *fi)
1657 struct fuse_file *ff = NULL;
1659 spin_lock(&fi->lock);
1660 if (!list_empty(&fi->write_files)) {
1661 ff = list_entry(fi->write_files.next, struct fuse_file,
1665 spin_unlock(&fi->lock);
1670 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1671 struct fuse_inode *fi)
1673 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1678 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1680 struct fuse_conn *fc = get_fuse_conn(inode);
1681 struct fuse_inode *fi = get_fuse_inode(inode);
1682 struct fuse_file *ff;
1685 ff = __fuse_write_file_get(fc, fi);
1686 err = fuse_flush_times(inode, ff);
1688 fuse_file_put(ff, false, false);
1693 static int fuse_writepage_locked(struct page *page)
1695 struct address_space *mapping = page->mapping;
1696 struct inode *inode = mapping->host;
1697 struct fuse_conn *fc = get_fuse_conn(inode);
1698 struct fuse_inode *fi = get_fuse_inode(inode);
1699 struct fuse_req *req;
1700 struct page *tmp_page;
1701 int error = -ENOMEM;
1703 set_page_writeback(page);
1705 req = fuse_request_alloc_nofs(1);
1709 /* writeback always goes to bg_queue */
1710 __set_bit(FR_BACKGROUND, &req->flags);
1711 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1716 req->ff = fuse_write_file_get(fc, fi);
1720 fuse_write_fill(req, req->ff, page_offset(page), 0);
1722 copy_highpage(tmp_page, page);
1723 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1724 req->misc.write.next = NULL;
1725 req->in.argpages = 1;
1727 req->pages[0] = tmp_page;
1728 req->page_descs[0].offset = 0;
1729 req->page_descs[0].length = PAGE_SIZE;
1730 req->end = fuse_writepage_end;
1733 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1734 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1736 spin_lock(&fi->lock);
1737 list_add(&req->writepages_entry, &fi->writepages);
1738 list_add_tail(&req->list, &fi->queued_writes);
1739 fuse_flush_writepages(inode);
1740 spin_unlock(&fi->lock);
1742 end_page_writeback(page);
1747 __free_page(tmp_page);
1749 fuse_request_free(req);
1751 mapping_set_error(page->mapping, error);
1752 end_page_writeback(page);
1756 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1760 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1762 * ->writepages() should be called for sync() and friends. We
1763 * should only get here on direct reclaim and then we are
1764 * allowed to skip a page which is already in flight
1766 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1768 redirty_page_for_writepage(wbc, page);
1772 err = fuse_writepage_locked(page);
1778 struct fuse_fill_wb_data {
1779 struct fuse_req *req;
1780 struct fuse_file *ff;
1781 struct inode *inode;
1782 struct page **orig_pages;
1785 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1787 struct fuse_req *req = data->req;
1788 struct inode *inode = data->inode;
1789 struct fuse_inode *fi = get_fuse_inode(inode);
1790 int num_pages = req->num_pages;
1793 req->ff = fuse_file_get(data->ff);
1794 spin_lock(&fi->lock);
1795 list_add_tail(&req->list, &fi->queued_writes);
1796 fuse_flush_writepages(inode);
1797 spin_unlock(&fi->lock);
1799 for (i = 0; i < num_pages; i++)
1800 end_page_writeback(data->orig_pages[i]);
1804 * First recheck under fi->lock if the offending offset is still under
1805 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1806 * one already added for a page at this offset. If there's none, then insert
1807 * this new request onto the auxiliary list, otherwise reuse the existing one by
1808 * copying the new page contents over to the old temporary page.
1810 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1813 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1814 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1815 struct fuse_req *tmp;
1816 struct fuse_req *old_req;
1818 WARN_ON(new_req->num_pages != 0);
1820 spin_lock(&fi->lock);
1821 list_del(&new_req->writepages_entry);
1822 old_req = fuse_find_writeback(fi, page->index, page->index);
1824 list_add(&new_req->writepages_entry, &fi->writepages);
1825 spin_unlock(&fi->lock);
1829 new_req->num_pages = 1;
1830 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
1833 WARN_ON(tmp->inode != new_req->inode);
1834 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1835 if (curr_index == page->index) {
1836 WARN_ON(tmp->num_pages != 1);
1837 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
1838 swap(tmp->pages[0], new_req->pages[0]);
1844 new_req->misc.write.next = old_req->misc.write.next;
1845 old_req->misc.write.next = new_req;
1848 spin_unlock(&fi->lock);
1851 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
1853 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1854 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1855 wb_writeout_inc(&bdi->wb);
1856 fuse_writepage_free(fc, new_req);
1857 fuse_request_free(new_req);
1863 static int fuse_writepages_fill(struct page *page,
1864 struct writeback_control *wbc, void *_data)
1866 struct fuse_fill_wb_data *data = _data;
1867 struct fuse_req *req = data->req;
1868 struct inode *inode = data->inode;
1869 struct fuse_inode *fi = get_fuse_inode(inode);
1870 struct fuse_conn *fc = get_fuse_conn(inode);
1871 struct page *tmp_page;
1877 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1883 * Being under writeback is unlikely but possible. For example direct
1884 * read to an mmaped fuse file will set the page dirty twice; once when
1885 * the pages are faulted with get_user_pages(), and then after the read
1888 is_writeback = fuse_page_is_writeback(inode, page->index);
1890 if (req && req->num_pages &&
1891 (is_writeback || req->num_pages == fc->max_pages ||
1892 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1893 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1894 fuse_writepages_send(data);
1896 } else if (req && req->num_pages == req->max_pages) {
1897 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1898 fuse_writepages_send(data);
1899 req = data->req = NULL;
1904 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1909 * The page must not be redirtied until the writeout is completed
1910 * (i.e. userspace has sent a reply to the write request). Otherwise
1911 * there could be more than one temporary page instance for each real
1914 * This is ensured by holding the page lock in page_mkwrite() while
1915 * checking fuse_page_is_writeback(). We already hold the page lock
1916 * since clear_page_dirty_for_io() and keep it held until we add the
1917 * request to the fi->writepages list and increment req->num_pages.
1918 * After this fuse_page_is_writeback() will indicate that the page is
1919 * under writeback, so we can release the page lock.
1921 if (data->req == NULL) {
1922 struct fuse_inode *fi = get_fuse_inode(inode);
1925 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
1927 __free_page(tmp_page);
1931 fuse_write_fill(req, data->ff, page_offset(page), 0);
1932 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1933 req->misc.write.next = NULL;
1934 req->in.argpages = 1;
1935 __set_bit(FR_BACKGROUND, &req->flags);
1937 req->end = fuse_writepage_end;
1940 spin_lock(&fi->lock);
1941 list_add(&req->writepages_entry, &fi->writepages);
1942 spin_unlock(&fi->lock);
1946 set_page_writeback(page);
1948 copy_highpage(tmp_page, page);
1949 req->pages[req->num_pages] = tmp_page;
1950 req->page_descs[req->num_pages].offset = 0;
1951 req->page_descs[req->num_pages].length = PAGE_SIZE;
1953 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1954 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1957 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1958 end_page_writeback(page);
1962 data->orig_pages[req->num_pages] = page;
1965 * Protected by fi->lock against concurrent access by
1966 * fuse_page_is_writeback().
1968 spin_lock(&fi->lock);
1970 spin_unlock(&fi->lock);
1978 static int fuse_writepages(struct address_space *mapping,
1979 struct writeback_control *wbc)
1981 struct inode *inode = mapping->host;
1982 struct fuse_conn *fc = get_fuse_conn(inode);
1983 struct fuse_fill_wb_data data;
1987 if (is_bad_inode(inode))
1995 data.orig_pages = kcalloc(fc->max_pages,
1996 sizeof(struct page *),
1998 if (!data.orig_pages)
2001 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2003 /* Ignore errors if we can write at least one page */
2004 BUG_ON(!data.req->num_pages);
2005 fuse_writepages_send(&data);
2009 fuse_file_put(data.ff, false, false);
2011 kfree(data.orig_pages);
2017 * It's worthy to make sure that space is reserved on disk for the write,
2018 * but how to implement it without killing performance need more thinking.
2020 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2021 loff_t pos, unsigned len, unsigned flags,
2022 struct page **pagep, void **fsdata)
2024 pgoff_t index = pos >> PAGE_SHIFT;
2025 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2030 WARN_ON(!fc->writeback_cache);
2032 page = grab_cache_page_write_begin(mapping, index, flags);
2036 fuse_wait_on_page_writeback(mapping->host, page->index);
2038 if (PageUptodate(page) || len == PAGE_SIZE)
2041 * Check if the start this page comes after the end of file, in which
2042 * case the readpage can be optimized away.
2044 fsize = i_size_read(mapping->host);
2045 if (fsize <= (pos & PAGE_MASK)) {
2046 size_t off = pos & ~PAGE_MASK;
2048 zero_user_segment(page, 0, off);
2051 err = fuse_do_readpage(file, page);
2065 static int fuse_write_end(struct file *file, struct address_space *mapping,
2066 loff_t pos, unsigned len, unsigned copied,
2067 struct page *page, void *fsdata)
2069 struct inode *inode = page->mapping->host;
2071 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2075 if (!PageUptodate(page)) {
2076 /* Zero any unwritten bytes at the end of the page */
2077 size_t endoff = (pos + copied) & ~PAGE_MASK;
2079 zero_user_segment(page, endoff, PAGE_SIZE);
2080 SetPageUptodate(page);
2083 fuse_write_update_size(inode, pos + copied);
2084 set_page_dirty(page);
2093 static int fuse_launder_page(struct page *page)
2096 if (clear_page_dirty_for_io(page)) {
2097 struct inode *inode = page->mapping->host;
2098 err = fuse_writepage_locked(page);
2100 fuse_wait_on_page_writeback(inode, page->index);
2106 * Write back dirty pages now, because there may not be any suitable
2109 static void fuse_vma_close(struct vm_area_struct *vma)
2111 filemap_write_and_wait(vma->vm_file->f_mapping);
2115 * Wait for writeback against this page to complete before allowing it
2116 * to be marked dirty again, and hence written back again, possibly
2117 * before the previous writepage completed.
2119 * Block here, instead of in ->writepage(), so that the userspace fs
2120 * can only block processes actually operating on the filesystem.
2122 * Otherwise unprivileged userspace fs would be able to block
2127 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2129 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2131 struct page *page = vmf->page;
2132 struct inode *inode = file_inode(vmf->vma->vm_file);
2134 file_update_time(vmf->vma->vm_file);
2136 if (page->mapping != inode->i_mapping) {
2138 return VM_FAULT_NOPAGE;
2141 fuse_wait_on_page_writeback(inode, page->index);
2142 return VM_FAULT_LOCKED;
2145 static const struct vm_operations_struct fuse_file_vm_ops = {
2146 .close = fuse_vma_close,
2147 .fault = filemap_fault,
2148 .map_pages = filemap_map_pages,
2149 .page_mkwrite = fuse_page_mkwrite,
2152 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2154 struct fuse_file *ff = file->private_data;
2156 if (ff->open_flags & FOPEN_DIRECT_IO) {
2157 /* Can't provide the coherency needed for MAP_SHARED */
2158 if (vma->vm_flags & VM_MAYSHARE)
2161 invalidate_inode_pages2(file->f_mapping);
2163 return generic_file_mmap(file, vma);
2166 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2167 fuse_link_write_file(file);
2169 file_accessed(file);
2170 vma->vm_ops = &fuse_file_vm_ops;
2174 static int convert_fuse_file_lock(struct fuse_conn *fc,
2175 const struct fuse_file_lock *ffl,
2176 struct file_lock *fl)
2178 switch (ffl->type) {
2184 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2185 ffl->end < ffl->start)
2188 fl->fl_start = ffl->start;
2189 fl->fl_end = ffl->end;
2192 * Convert pid into init's pid namespace. The locks API will
2193 * translate it into the caller's pid namespace.
2196 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2203 fl->fl_type = ffl->type;
2207 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2208 const struct file_lock *fl, int opcode, pid_t pid,
2209 int flock, struct fuse_lk_in *inarg)
2211 struct inode *inode = file_inode(file);
2212 struct fuse_conn *fc = get_fuse_conn(inode);
2213 struct fuse_file *ff = file->private_data;
2215 memset(inarg, 0, sizeof(*inarg));
2217 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2218 inarg->lk.start = fl->fl_start;
2219 inarg->lk.end = fl->fl_end;
2220 inarg->lk.type = fl->fl_type;
2221 inarg->lk.pid = pid;
2223 inarg->lk_flags |= FUSE_LK_FLOCK;
2224 args->opcode = opcode;
2225 args->nodeid = get_node_id(inode);
2226 args->in_numargs = 1;
2227 args->in_args[0].size = sizeof(*inarg);
2228 args->in_args[0].value = inarg;
2231 static int fuse_getlk(struct file *file, struct file_lock *fl)
2233 struct inode *inode = file_inode(file);
2234 struct fuse_conn *fc = get_fuse_conn(inode);
2236 struct fuse_lk_in inarg;
2237 struct fuse_lk_out outarg;
2240 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2241 args.out_numargs = 1;
2242 args.out_args[0].size = sizeof(outarg);
2243 args.out_args[0].value = &outarg;
2244 err = fuse_simple_request(fc, &args);
2246 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2251 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2253 struct inode *inode = file_inode(file);
2254 struct fuse_conn *fc = get_fuse_conn(inode);
2256 struct fuse_lk_in inarg;
2257 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2258 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2259 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2262 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2263 /* NLM needs asynchronous locks, which we don't support yet */
2267 /* Unlock on close is handled by the flush method */
2268 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2271 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2272 err = fuse_simple_request(fc, &args);
2274 /* locking is restartable */
2281 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2283 struct inode *inode = file_inode(file);
2284 struct fuse_conn *fc = get_fuse_conn(inode);
2287 if (cmd == F_CANCELLK) {
2289 } else if (cmd == F_GETLK) {
2291 posix_test_lock(file, fl);
2294 err = fuse_getlk(file, fl);
2297 err = posix_lock_file(file, fl, NULL);
2299 err = fuse_setlk(file, fl, 0);
2304 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2306 struct inode *inode = file_inode(file);
2307 struct fuse_conn *fc = get_fuse_conn(inode);
2311 err = locks_lock_file_wait(file, fl);
2313 struct fuse_file *ff = file->private_data;
2315 /* emulate flock with POSIX locks */
2317 err = fuse_setlk(file, fl, 1);
2323 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2325 struct inode *inode = mapping->host;
2326 struct fuse_conn *fc = get_fuse_conn(inode);
2328 struct fuse_bmap_in inarg;
2329 struct fuse_bmap_out outarg;
2332 if (!inode->i_sb->s_bdev || fc->no_bmap)
2335 memset(&inarg, 0, sizeof(inarg));
2336 inarg.block = block;
2337 inarg.blocksize = inode->i_sb->s_blocksize;
2338 args.opcode = FUSE_BMAP;
2339 args.nodeid = get_node_id(inode);
2340 args.in_numargs = 1;
2341 args.in_args[0].size = sizeof(inarg);
2342 args.in_args[0].value = &inarg;
2343 args.out_numargs = 1;
2344 args.out_args[0].size = sizeof(outarg);
2345 args.out_args[0].value = &outarg;
2346 err = fuse_simple_request(fc, &args);
2350 return err ? 0 : outarg.block;
2353 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2355 struct inode *inode = file->f_mapping->host;
2356 struct fuse_conn *fc = get_fuse_conn(inode);
2357 struct fuse_file *ff = file->private_data;
2359 struct fuse_lseek_in inarg = {
2364 struct fuse_lseek_out outarg;
2370 args.opcode = FUSE_LSEEK;
2371 args.nodeid = ff->nodeid;
2372 args.in_numargs = 1;
2373 args.in_args[0].size = sizeof(inarg);
2374 args.in_args[0].value = &inarg;
2375 args.out_numargs = 1;
2376 args.out_args[0].size = sizeof(outarg);
2377 args.out_args[0].value = &outarg;
2378 err = fuse_simple_request(fc, &args);
2380 if (err == -ENOSYS) {
2387 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2390 err = fuse_update_attributes(inode, file);
2392 return generic_file_llseek(file, offset, whence);
2397 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2400 struct inode *inode = file_inode(file);
2405 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2406 retval = generic_file_llseek(file, offset, whence);
2410 retval = fuse_update_attributes(inode, file);
2412 retval = generic_file_llseek(file, offset, whence);
2413 inode_unlock(inode);
2418 retval = fuse_lseek(file, offset, whence);
2419 inode_unlock(inode);
2429 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2430 * ABI was defined to be 'struct iovec' which is different on 32bit
2431 * and 64bit. Fortunately we can determine which structure the server
2432 * used from the size of the reply.
2434 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2435 size_t transferred, unsigned count,
2438 #ifdef CONFIG_COMPAT
2439 if (count * sizeof(struct compat_iovec) == transferred) {
2440 struct compat_iovec *ciov = src;
2444 * With this interface a 32bit server cannot support
2445 * non-compat (i.e. ones coming from 64bit apps) ioctl
2451 for (i = 0; i < count; i++) {
2452 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2453 dst[i].iov_len = ciov[i].iov_len;
2459 if (count * sizeof(struct iovec) != transferred)
2462 memcpy(dst, src, transferred);
2466 /* Make sure iov_length() won't overflow */
2467 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2471 u32 max = fc->max_pages << PAGE_SHIFT;
2473 for (n = 0; n < count; n++, iov++) {
2474 if (iov->iov_len > (size_t) max)
2476 max -= iov->iov_len;
2481 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2482 void *src, size_t transferred, unsigned count,
2486 struct fuse_ioctl_iovec *fiov = src;
2488 if (fc->minor < 16) {
2489 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2493 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2496 for (i = 0; i < count; i++) {
2497 /* Did the server supply an inappropriate value? */
2498 if (fiov[i].base != (unsigned long) fiov[i].base ||
2499 fiov[i].len != (unsigned long) fiov[i].len)
2502 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2503 dst[i].iov_len = (size_t) fiov[i].len;
2505 #ifdef CONFIG_COMPAT
2507 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2508 (compat_size_t) dst[i].iov_len != fiov[i].len))
2518 * For ioctls, there is no generic way to determine how much memory
2519 * needs to be read and/or written. Furthermore, ioctls are allowed
2520 * to dereference the passed pointer, so the parameter requires deep
2521 * copying but FUSE has no idea whatsoever about what to copy in or
2524 * This is solved by allowing FUSE server to retry ioctl with
2525 * necessary in/out iovecs. Let's assume the ioctl implementation
2526 * needs to read in the following structure.
2533 * On the first callout to FUSE server, inarg->in_size and
2534 * inarg->out_size will be NULL; then, the server completes the ioctl
2535 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2536 * the actual iov array to
2538 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2540 * which tells FUSE to copy in the requested area and retry the ioctl.
2541 * On the second round, the server has access to the structure and
2542 * from that it can tell what to look for next, so on the invocation,
2543 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2545 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2546 * { .iov_base = a.buf, .iov_len = a.buflen } }
2548 * FUSE will copy both struct a and the pointed buffer from the
2549 * process doing the ioctl and retry ioctl with both struct a and the
2552 * This time, FUSE server has everything it needs and completes ioctl
2553 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2555 * Copying data out works the same way.
2557 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2558 * automatically initializes in and out iovs by decoding @cmd with
2559 * _IOC_* macros and the server is not allowed to request RETRY. This
2560 * limits ioctl data transfers to well-formed ioctls and is the forced
2561 * behavior for all FUSE servers.
2563 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2566 struct fuse_file *ff = file->private_data;
2567 struct fuse_conn *fc = ff->fc;
2568 struct fuse_ioctl_in inarg = {
2574 struct fuse_ioctl_out outarg;
2575 struct fuse_req *req = NULL;
2576 struct page **pages = NULL;
2577 struct iovec *iov_page = NULL;
2578 struct iovec *in_iov = NULL, *out_iov = NULL;
2579 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2580 size_t in_size, out_size, transferred, c;
2584 #if BITS_PER_LONG == 32
2585 inarg.flags |= FUSE_IOCTL_32BIT;
2587 if (flags & FUSE_IOCTL_COMPAT) {
2588 inarg.flags |= FUSE_IOCTL_32BIT;
2589 #ifdef CONFIG_X86_X32
2590 if (in_x32_syscall())
2591 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2596 /* assume all the iovs returned by client always fits in a page */
2597 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2600 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
2601 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2602 if (!pages || !iov_page)
2606 * If restricted, initialize IO parameters as encoded in @cmd.
2607 * RETRY from server is not allowed.
2609 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2610 struct iovec *iov = iov_page;
2612 iov->iov_base = (void __user *)arg;
2613 iov->iov_len = _IOC_SIZE(cmd);
2615 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2620 if (_IOC_DIR(cmd) & _IOC_READ) {
2627 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2628 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2631 * Out data can be used either for actual out data or iovs,
2632 * make sure there always is at least one page.
2634 out_size = max_t(size_t, out_size, PAGE_SIZE);
2635 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2637 /* make sure there are enough buffer pages and init request with them */
2639 if (max_pages > fc->max_pages)
2641 while (num_pages < max_pages) {
2642 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2643 if (!pages[num_pages])
2648 req = fuse_get_req(fc, num_pages);
2654 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2655 req->num_pages = num_pages;
2656 fuse_page_descs_length_init(req, 0, req->num_pages);
2658 /* okay, let's send it to the client */
2659 req->in.h.opcode = FUSE_IOCTL;
2660 req->in.h.nodeid = ff->nodeid;
2661 req->in.numargs = 1;
2662 req->in.args[0].size = sizeof(inarg);
2663 req->in.args[0].value = &inarg;
2666 req->in.args[1].size = in_size;
2667 req->in.argpages = 1;
2670 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2671 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2672 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2673 if (c != PAGE_SIZE && iov_iter_count(&ii))
2678 req->out.numargs = 2;
2679 req->out.args[0].size = sizeof(outarg);
2680 req->out.args[0].value = &outarg;
2681 req->out.args[1].size = out_size;
2682 req->out.argpages = 1;
2683 req->out.argvar = 1;
2685 fuse_request_send(fc, req);
2686 err = req->out.h.error;
2687 transferred = req->out.args[1].size;
2688 fuse_put_request(fc, req);
2693 /* did it ask for retry? */
2694 if (outarg.flags & FUSE_IOCTL_RETRY) {
2697 /* no retry if in restricted mode */
2699 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2702 in_iovs = outarg.in_iovs;
2703 out_iovs = outarg.out_iovs;
2706 * Make sure things are in boundary, separate checks
2707 * are to protect against overflow.
2710 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2711 out_iovs > FUSE_IOCTL_MAX_IOV ||
2712 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2715 vaddr = kmap_atomic(pages[0]);
2716 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2717 transferred, in_iovs + out_iovs,
2718 (flags & FUSE_IOCTL_COMPAT) != 0);
2719 kunmap_atomic(vaddr);
2724 out_iov = in_iov + in_iovs;
2726 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2730 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2738 if (transferred > inarg.out_size)
2742 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2743 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2744 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2745 if (c != PAGE_SIZE && iov_iter_count(&ii))
2751 fuse_put_request(fc, req);
2752 free_page((unsigned long) iov_page);
2754 __free_page(pages[--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);