2 FUSE: Filesystem in Userspace
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
22 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
25 struct fuse_open_in inarg;
28 memset(&inarg, 0, sizeof(inarg));
29 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
30 if (!fc->atomic_o_trunc)
31 inarg.flags &= ~O_TRUNC;
32 args.in.h.opcode = opcode;
33 args.in.h.nodeid = nodeid;
35 args.in.args[0].size = sizeof(inarg);
36 args.in.args[0].value = &inarg;
38 args.out.args[0].size = sizeof(*outargp);
39 args.out.args[0].value = outargp;
41 return fuse_simple_request(fc, &args);
44 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
48 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
53 ff->reserved_req = fuse_request_alloc(0);
54 if (unlikely(!ff->reserved_req)) {
59 INIT_LIST_HEAD(&ff->write_entry);
60 mutex_init(&ff->readdir.lock);
61 refcount_set(&ff->count, 1);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
65 ff->kh = atomic64_inc_return(&fc->khctr);
70 void fuse_file_free(struct fuse_file *ff)
72 fuse_request_free(ff->reserved_req);
73 mutex_destroy(&ff->readdir.lock);
77 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
79 refcount_inc(&ff->count);
83 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85 iput(req->misc.release.inode);
88 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
90 if (refcount_dec_and_test(&ff->count)) {
91 struct fuse_req *req = ff->reserved_req;
93 if (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 int 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));
395 * Wait for all pending writepages on the inode to finish.
397 * This is currently done by blocking further writes with FUSE_NOWRITE
398 * and waiting for all sent writes to complete.
400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
401 * could conflict with truncation.
403 static void fuse_sync_writes(struct inode *inode)
405 fuse_set_nowrite(inode);
406 fuse_release_nowrite(inode);
409 static int fuse_flush(struct file *file, fl_owner_t id)
411 struct inode *inode = file_inode(file);
412 struct fuse_conn *fc = get_fuse_conn(inode);
413 struct fuse_file *ff = file->private_data;
414 struct fuse_req *req;
415 struct fuse_flush_in inarg;
418 if (is_bad_inode(inode))
424 err = write_inode_now(inode, 1);
429 fuse_sync_writes(inode);
432 err = filemap_check_errors(file->f_mapping);
436 req = fuse_get_req_nofail_nopages(fc, file);
437 memset(&inarg, 0, sizeof(inarg));
439 inarg.lock_owner = fuse_lock_owner_id(fc, id);
440 req->in.h.opcode = FUSE_FLUSH;
441 req->in.h.nodeid = get_node_id(inode);
443 req->in.args[0].size = sizeof(inarg);
444 req->in.args[0].value = &inarg;
445 __set_bit(FR_FORCE, &req->flags);
446 fuse_request_send(fc, req);
447 err = req->out.h.error;
448 fuse_put_request(fc, req);
449 if (err == -ENOSYS) {
456 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
457 int datasync, int opcode)
459 struct inode *inode = file->f_mapping->host;
460 struct fuse_conn *fc = get_fuse_conn(inode);
461 struct fuse_file *ff = file->private_data;
463 struct fuse_fsync_in inarg;
465 memset(&inarg, 0, sizeof(inarg));
467 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
468 args.in.h.opcode = opcode;
469 args.in.h.nodeid = get_node_id(inode);
471 args.in.args[0].size = sizeof(inarg);
472 args.in.args[0].value = &inarg;
473 return fuse_simple_request(fc, &args);
476 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
479 struct inode *inode = file->f_mapping->host;
480 struct fuse_conn *fc = get_fuse_conn(inode);
483 if (is_bad_inode(inode))
489 * Start writeback against all dirty pages of the inode, then
490 * wait for all outstanding writes, before sending the FSYNC
493 err = file_write_and_wait_range(file, start, end);
497 fuse_sync_writes(inode);
500 * Due to implementation of fuse writeback
501 * file_write_and_wait_range() does not catch errors.
502 * We have to do this directly after fuse_sync_writes()
504 err = file_check_and_advance_wb_err(file);
508 err = sync_inode_metadata(inode, 1);
515 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
516 if (err == -ENOSYS) {
526 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
527 size_t count, int opcode)
529 struct fuse_read_in *inarg = &req->misc.read.in;
530 struct fuse_file *ff = file->private_data;
535 inarg->flags = file->f_flags;
536 req->in.h.opcode = opcode;
537 req->in.h.nodeid = ff->nodeid;
539 req->in.args[0].size = sizeof(struct fuse_read_in);
540 req->in.args[0].value = inarg;
542 req->out.numargs = 1;
543 req->out.args[0].size = count;
546 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
550 for (i = 0; i < req->num_pages; i++) {
551 struct page *page = req->pages[i];
553 set_page_dirty_lock(page);
558 static void fuse_io_release(struct kref *kref)
560 kfree(container_of(kref, struct fuse_io_priv, refcnt));
563 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
568 if (io->bytes >= 0 && io->write)
571 return io->bytes < 0 ? io->size : io->bytes;
575 * In case of short read, the caller sets 'pos' to the position of
576 * actual end of fuse request in IO request. Otherwise, if bytes_requested
577 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
580 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
581 * both submitted asynchronously. The first of them was ACKed by userspace as
582 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
583 * second request was ACKed as short, e.g. only 1K was read, resulting in
586 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
587 * will be equal to the length of the longest contiguous fragment of
588 * transferred data starting from the beginning of IO request.
590 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
594 spin_lock(&io->lock);
596 io->err = io->err ? : err;
597 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
601 if (!left && io->blocking)
603 spin_unlock(&io->lock);
605 if (!left && !io->blocking) {
606 ssize_t res = fuse_get_res_by_io(io);
609 struct inode *inode = file_inode(io->iocb->ki_filp);
610 struct fuse_conn *fc = get_fuse_conn(inode);
611 struct fuse_inode *fi = get_fuse_inode(inode);
613 spin_lock(&fi->lock);
614 fi->attr_version = atomic64_inc_return(&fc->attr_version);
615 spin_unlock(&fi->lock);
618 io->iocb->ki_complete(io->iocb, res, 0);
621 kref_put(&io->refcnt, fuse_io_release);
624 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
626 struct fuse_io_priv *io = req->io;
629 fuse_release_user_pages(req, io->should_dirty);
632 if (req->misc.write.in.size != req->misc.write.out.size)
633 pos = req->misc.write.in.offset - io->offset +
634 req->misc.write.out.size;
636 if (req->misc.read.in.size != req->out.args[0].size)
637 pos = req->misc.read.in.offset - io->offset +
638 req->out.args[0].size;
641 fuse_aio_complete(io, req->out.h.error, pos);
644 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
645 size_t num_bytes, struct fuse_io_priv *io)
647 spin_lock(&io->lock);
648 kref_get(&io->refcnt);
649 io->size += num_bytes;
651 spin_unlock(&io->lock);
654 req->end = fuse_aio_complete_req;
656 __fuse_get_request(req);
657 fuse_request_send_background(fc, req);
662 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
663 loff_t pos, size_t count, fl_owner_t owner)
665 struct file *file = io->iocb->ki_filp;
666 struct fuse_file *ff = file->private_data;
667 struct fuse_conn *fc = ff->fc;
669 fuse_read_fill(req, file, pos, count, FUSE_READ);
671 struct fuse_read_in *inarg = &req->misc.read.in;
673 inarg->read_flags |= FUSE_READ_LOCKOWNER;
674 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
678 return fuse_async_req_send(fc, req, count, io);
680 fuse_request_send(fc, req);
681 return req->out.args[0].size;
684 static void fuse_read_update_size(struct inode *inode, loff_t size,
687 struct fuse_conn *fc = get_fuse_conn(inode);
688 struct fuse_inode *fi = get_fuse_inode(inode);
690 spin_lock(&fi->lock);
691 if (attr_ver == fi->attr_version && size < inode->i_size &&
692 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
693 fi->attr_version = atomic64_inc_return(&fc->attr_version);
694 i_size_write(inode, size);
696 spin_unlock(&fi->lock);
699 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
702 size_t num_read = req->out.args[0].size;
703 struct fuse_conn *fc = get_fuse_conn(inode);
705 if (fc->writeback_cache) {
707 * A hole in a file. Some data after the hole are in page cache,
708 * but have not reached the client fs yet. So, the hole is not
712 int start_idx = num_read >> PAGE_SHIFT;
713 size_t off = num_read & (PAGE_SIZE - 1);
715 for (i = start_idx; i < req->num_pages; i++) {
716 zero_user_segment(req->pages[i], off, PAGE_SIZE);
720 loff_t pos = page_offset(req->pages[0]) + num_read;
721 fuse_read_update_size(inode, pos, attr_ver);
725 static int fuse_do_readpage(struct file *file, struct page *page)
728 struct fuse_io_priv io;
729 struct inode *inode = page->mapping->host;
730 struct fuse_conn *fc = get_fuse_conn(inode);
731 struct fuse_req *req;
733 loff_t pos = page_offset(page);
734 size_t count = PAGE_SIZE;
739 * Page writeback can extend beyond the lifetime of the
740 * page-cache page, so make sure we read a properly synced
743 fuse_wait_on_page_writeback(inode, page->index);
745 req = fuse_get_req(fc, 1);
749 attr_ver = fuse_get_attr_version(fc);
751 req->out.page_zeroing = 1;
752 req->out.argpages = 1;
754 req->pages[0] = page;
755 req->page_descs[0].length = count;
756 init_sync_kiocb(&iocb, file);
757 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
758 num_read = fuse_send_read(req, &io, pos, count, NULL);
759 err = req->out.h.error;
763 * Short read means EOF. If file size is larger, truncate it
765 if (num_read < count)
766 fuse_short_read(req, inode, attr_ver);
768 SetPageUptodate(page);
771 fuse_put_request(fc, req);
776 static int fuse_readpage(struct file *file, struct page *page)
778 struct inode *inode = page->mapping->host;
782 if (is_bad_inode(inode))
785 err = fuse_do_readpage(file, page);
786 fuse_invalidate_atime(inode);
792 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
795 size_t count = req->misc.read.in.size;
796 size_t num_read = req->out.args[0].size;
797 struct address_space *mapping = NULL;
799 for (i = 0; mapping == NULL && i < req->num_pages; i++)
800 mapping = req->pages[i]->mapping;
803 struct inode *inode = mapping->host;
806 * Short read means EOF. If file size is larger, truncate it
808 if (!req->out.h.error && num_read < count)
809 fuse_short_read(req, inode, req->misc.read.attr_ver);
811 fuse_invalidate_atime(inode);
814 for (i = 0; i < req->num_pages; i++) {
815 struct page *page = req->pages[i];
816 if (!req->out.h.error)
817 SetPageUptodate(page);
824 fuse_file_put(req->ff, false, false);
827 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
829 struct fuse_file *ff = file->private_data;
830 struct fuse_conn *fc = ff->fc;
831 loff_t pos = page_offset(req->pages[0]);
832 size_t count = req->num_pages << PAGE_SHIFT;
834 req->out.argpages = 1;
835 req->out.page_zeroing = 1;
836 req->out.page_replace = 1;
837 fuse_read_fill(req, file, pos, count, FUSE_READ);
838 req->misc.read.attr_ver = fuse_get_attr_version(fc);
839 if (fc->async_read) {
840 req->ff = fuse_file_get(ff);
841 req->end = fuse_readpages_end;
842 fuse_request_send_background(fc, req);
844 fuse_request_send(fc, req);
845 fuse_readpages_end(fc, req);
846 fuse_put_request(fc, req);
850 struct fuse_fill_data {
851 struct fuse_req *req;
857 static int fuse_readpages_fill(void *_data, struct page *page)
859 struct fuse_fill_data *data = _data;
860 struct fuse_req *req = data->req;
861 struct inode *inode = data->inode;
862 struct fuse_conn *fc = get_fuse_conn(inode);
864 fuse_wait_on_page_writeback(inode, page->index);
866 if (req->num_pages &&
867 (req->num_pages == fc->max_pages ||
868 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
869 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
870 unsigned int nr_alloc = min_t(unsigned int, data->nr_pages,
872 fuse_send_readpages(req, data->file);
874 req = fuse_get_req_for_background(fc, nr_alloc);
876 req = fuse_get_req(fc, nr_alloc);
885 if (WARN_ON(req->num_pages >= req->max_pages)) {
887 fuse_put_request(fc, req);
892 req->pages[req->num_pages] = page;
893 req->page_descs[req->num_pages].length = PAGE_SIZE;
899 static int fuse_readpages(struct file *file, struct address_space *mapping,
900 struct list_head *pages, unsigned nr_pages)
902 struct inode *inode = mapping->host;
903 struct fuse_conn *fc = get_fuse_conn(inode);
904 struct fuse_fill_data data;
906 unsigned int nr_alloc = min_t(unsigned int, nr_pages, fc->max_pages);
909 if (is_bad_inode(inode))
915 data.req = fuse_get_req_for_background(fc, nr_alloc);
917 data.req = fuse_get_req(fc, nr_alloc);
918 data.nr_pages = nr_pages;
919 err = PTR_ERR(data.req);
920 if (IS_ERR(data.req))
923 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
925 if (data.req->num_pages)
926 fuse_send_readpages(data.req, file);
928 fuse_put_request(fc, data.req);
934 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
936 struct inode *inode = iocb->ki_filp->f_mapping->host;
937 struct fuse_conn *fc = get_fuse_conn(inode);
940 * In auto invalidate mode, always update attributes on read.
941 * Otherwise, only update if we attempt to read past EOF (to ensure
942 * i_size is up to date).
944 if (fc->auto_inval_data ||
945 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
947 err = fuse_update_attributes(inode, iocb->ki_filp);
952 return generic_file_read_iter(iocb, to);
955 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
956 loff_t pos, size_t count)
958 struct fuse_write_in *inarg = &req->misc.write.in;
959 struct fuse_write_out *outarg = &req->misc.write.out;
964 req->in.h.opcode = FUSE_WRITE;
965 req->in.h.nodeid = ff->nodeid;
967 if (ff->fc->minor < 9)
968 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
970 req->in.args[0].size = sizeof(struct fuse_write_in);
971 req->in.args[0].value = inarg;
972 req->in.args[1].size = count;
973 req->out.numargs = 1;
974 req->out.args[0].size = sizeof(struct fuse_write_out);
975 req->out.args[0].value = outarg;
978 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
979 loff_t pos, size_t count, fl_owner_t owner)
981 struct kiocb *iocb = io->iocb;
982 struct file *file = iocb->ki_filp;
983 struct fuse_file *ff = file->private_data;
984 struct fuse_conn *fc = ff->fc;
985 struct fuse_write_in *inarg = &req->misc.write.in;
987 fuse_write_fill(req, ff, pos, count);
988 inarg->flags = file->f_flags;
989 if (iocb->ki_flags & IOCB_DSYNC)
990 inarg->flags |= O_DSYNC;
991 if (iocb->ki_flags & IOCB_SYNC)
992 inarg->flags |= O_SYNC;
994 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
995 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
999 return fuse_async_req_send(fc, req, count, io);
1001 fuse_request_send(fc, req);
1002 return req->misc.write.out.size;
1005 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1007 struct fuse_conn *fc = get_fuse_conn(inode);
1008 struct fuse_inode *fi = get_fuse_inode(inode);
1011 spin_lock(&fi->lock);
1012 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1013 if (pos > inode->i_size) {
1014 i_size_write(inode, pos);
1017 spin_unlock(&fi->lock);
1022 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1023 struct inode *inode, loff_t pos,
1029 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1031 for (i = 0; i < req->num_pages; i++)
1032 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1034 res = fuse_send_write(req, &io, pos, count, NULL);
1036 offset = req->page_descs[0].offset;
1038 for (i = 0; i < req->num_pages; i++) {
1039 struct page *page = req->pages[i];
1041 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1042 SetPageUptodate(page);
1044 if (count > PAGE_SIZE - offset)
1045 count -= PAGE_SIZE - offset;
1057 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1058 struct address_space *mapping,
1059 struct iov_iter *ii, loff_t pos)
1061 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1062 unsigned offset = pos & (PAGE_SIZE - 1);
1066 req->in.argpages = 1;
1067 req->page_descs[0].offset = offset;
1072 pgoff_t index = pos >> PAGE_SHIFT;
1073 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1074 iov_iter_count(ii));
1076 bytes = min_t(size_t, bytes, fc->max_write - count);
1080 if (iov_iter_fault_in_readable(ii, bytes))
1084 page = grab_cache_page_write_begin(mapping, index, 0);
1088 if (mapping_writably_mapped(mapping))
1089 flush_dcache_page(page);
1091 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1092 flush_dcache_page(page);
1094 iov_iter_advance(ii, tmp);
1098 bytes = min(bytes, iov_iter_single_seg_count(ii));
1103 req->pages[req->num_pages] = page;
1104 req->page_descs[req->num_pages].length = tmp;
1110 if (offset == PAGE_SIZE)
1113 if (!fc->big_writes)
1115 } while (iov_iter_count(ii) && count < fc->max_write &&
1116 req->num_pages < req->max_pages && offset == 0);
1118 return count > 0 ? count : err;
1121 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1122 unsigned int max_pages)
1124 return min_t(unsigned int,
1125 ((pos + len - 1) >> PAGE_SHIFT) -
1126 (pos >> PAGE_SHIFT) + 1,
1130 static ssize_t fuse_perform_write(struct kiocb *iocb,
1131 struct address_space *mapping,
1132 struct iov_iter *ii, loff_t pos)
1134 struct inode *inode = mapping->host;
1135 struct fuse_conn *fc = get_fuse_conn(inode);
1136 struct fuse_inode *fi = get_fuse_inode(inode);
1140 if (inode->i_size < pos + iov_iter_count(ii))
1141 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1144 struct fuse_req *req;
1146 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1149 req = fuse_get_req(fc, nr_pages);
1155 count = fuse_fill_write_pages(req, mapping, ii, pos);
1161 num_written = fuse_send_write_pages(req, iocb, inode,
1163 err = req->out.h.error;
1168 /* break out of the loop on short write */
1169 if (num_written != count)
1173 fuse_put_request(fc, req);
1174 } while (!err && iov_iter_count(ii));
1177 fuse_write_update_size(inode, pos);
1179 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1180 fuse_invalidate_attr(inode);
1182 return res > 0 ? res : err;
1185 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1187 struct file *file = iocb->ki_filp;
1188 struct address_space *mapping = file->f_mapping;
1189 ssize_t written = 0;
1190 ssize_t written_buffered = 0;
1191 struct inode *inode = mapping->host;
1195 if (get_fuse_conn(inode)->writeback_cache) {
1196 /* Update size (EOF optimization) and mode (SUID clearing) */
1197 err = fuse_update_attributes(mapping->host, file);
1201 return generic_file_write_iter(iocb, from);
1206 /* We can write back this queue in page reclaim */
1207 current->backing_dev_info = inode_to_bdi(inode);
1209 err = generic_write_checks(iocb, from);
1213 err = file_remove_privs(file);
1217 err = file_update_time(file);
1221 if (iocb->ki_flags & IOCB_DIRECT) {
1222 loff_t pos = iocb->ki_pos;
1223 written = generic_file_direct_write(iocb, from);
1224 if (written < 0 || !iov_iter_count(from))
1229 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1230 if (written_buffered < 0) {
1231 err = written_buffered;
1234 endbyte = pos + written_buffered - 1;
1236 err = filemap_write_and_wait_range(file->f_mapping, pos,
1241 invalidate_mapping_pages(file->f_mapping,
1243 endbyte >> PAGE_SHIFT);
1245 written += written_buffered;
1246 iocb->ki_pos = pos + written_buffered;
1248 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1250 iocb->ki_pos += written;
1253 current->backing_dev_info = NULL;
1254 inode_unlock(inode);
1256 written = generic_write_sync(iocb, written);
1258 return written ? written : err;
1261 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1262 unsigned index, unsigned nr_pages)
1266 for (i = index; i < index + nr_pages; i++)
1267 req->page_descs[i].length = PAGE_SIZE -
1268 req->page_descs[i].offset;
1271 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1273 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1276 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1279 return min(iov_iter_single_seg_count(ii), max_size);
1282 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1283 size_t *nbytesp, int write)
1285 size_t nbytes = 0; /* # bytes already packed in req */
1288 /* Special case for kernel I/O: can copy directly into the buffer */
1289 if (iov_iter_is_kvec(ii)) {
1290 unsigned long user_addr = fuse_get_user_addr(ii);
1291 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1294 req->in.args[1].value = (void *) user_addr;
1296 req->out.args[0].value = (void *) user_addr;
1298 iov_iter_advance(ii, frag_size);
1299 *nbytesp = frag_size;
1303 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1306 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1308 req->max_pages - req->num_pages,
1313 iov_iter_advance(ii, ret);
1317 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1319 req->page_descs[req->num_pages].offset = start;
1320 fuse_page_descs_length_init(req, req->num_pages, npages);
1322 req->num_pages += npages;
1323 req->page_descs[req->num_pages - 1].length -=
1324 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1328 req->in.argpages = 1;
1330 req->out.argpages = 1;
1334 return ret < 0 ? ret : 0;
1337 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1338 loff_t *ppos, int flags)
1340 int write = flags & FUSE_DIO_WRITE;
1341 int cuse = flags & FUSE_DIO_CUSE;
1342 struct file *file = io->iocb->ki_filp;
1343 struct inode *inode = file->f_mapping->host;
1344 struct fuse_file *ff = file->private_data;
1345 struct fuse_conn *fc = ff->fc;
1346 size_t nmax = write ? fc->max_write : fc->max_read;
1348 size_t count = iov_iter_count(iter);
1349 pgoff_t idx_from = pos >> PAGE_SHIFT;
1350 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1352 struct fuse_req *req;
1356 req = fuse_get_req_for_background(fc, iov_iter_npages(iter,
1359 req = fuse_get_req(fc, iov_iter_npages(iter, fc->max_pages));
1361 return PTR_ERR(req);
1363 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1366 fuse_sync_writes(inode);
1368 inode_unlock(inode);
1371 io->should_dirty = !write && iter_is_iovec(iter);
1374 fl_owner_t owner = current->files;
1375 size_t nbytes = min(count, nmax);
1376 err = fuse_get_user_pages(req, iter, &nbytes, write);
1381 if (!capable(CAP_FSETID)) {
1382 struct fuse_write_in *inarg;
1384 inarg = &req->misc.write.in;
1385 inarg->write_flags |= FUSE_WRITE_KILL_PRIV;
1387 nres = fuse_send_write(req, io, pos, nbytes, owner);
1389 nres = fuse_send_read(req, io, pos, nbytes, owner);
1393 fuse_release_user_pages(req, io->should_dirty);
1394 if (req->out.h.error) {
1395 err = req->out.h.error;
1397 } else if (nres > nbytes) {
1408 fuse_put_request(fc, req);
1410 req = fuse_get_req_for_background(fc,
1411 iov_iter_npages(iter, fc->max_pages));
1413 req = fuse_get_req(fc, iov_iter_npages(iter,
1420 fuse_put_request(fc, req);
1424 return res > 0 ? res : err;
1426 EXPORT_SYMBOL_GPL(fuse_direct_io);
1428 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1429 struct iov_iter *iter,
1433 struct inode *inode = file_inode(io->iocb->ki_filp);
1435 res = fuse_direct_io(io, iter, ppos, 0);
1437 fuse_invalidate_atime(inode);
1442 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1444 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1448 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1449 res = fuse_direct_IO(iocb, to);
1451 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1453 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1459 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1461 struct inode *inode = file_inode(iocb->ki_filp);
1462 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1465 /* Don't allow parallel writes to the same file */
1467 res = generic_write_checks(iocb, from);
1469 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1470 res = fuse_direct_IO(iocb, from);
1472 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1476 fuse_invalidate_attr(inode);
1478 fuse_write_update_size(inode, iocb->ki_pos);
1479 inode_unlock(inode);
1484 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1486 struct file *file = iocb->ki_filp;
1487 struct fuse_file *ff = file->private_data;
1489 if (is_bad_inode(file_inode(file)))
1492 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1493 return fuse_cache_read_iter(iocb, to);
1495 return fuse_direct_read_iter(iocb, to);
1498 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1500 struct file *file = iocb->ki_filp;
1501 struct fuse_file *ff = file->private_data;
1503 if (is_bad_inode(file_inode(file)))
1506 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1507 return fuse_cache_write_iter(iocb, from);
1509 return fuse_direct_write_iter(iocb, from);
1512 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1516 for (i = 0; i < req->num_pages; i++)
1517 __free_page(req->pages[i]);
1520 fuse_file_put(req->ff, false, false);
1523 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1525 struct inode *inode = req->inode;
1526 struct fuse_inode *fi = get_fuse_inode(inode);
1527 struct backing_dev_info *bdi = inode_to_bdi(inode);
1530 list_del(&req->writepages_entry);
1531 for (i = 0; i < req->num_pages; i++) {
1532 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1533 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1534 wb_writeout_inc(&bdi->wb);
1536 wake_up(&fi->page_waitq);
1539 /* Called under fi->lock, may release and reacquire it */
1540 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1542 __releases(fi->lock)
1543 __acquires(fi->lock)
1545 struct fuse_req *aux, *next;
1546 struct fuse_inode *fi = get_fuse_inode(req->inode);
1547 struct fuse_write_in *inarg = &req->misc.write.in;
1548 __u64 data_size = req->num_pages * PAGE_SIZE;
1551 if (inarg->offset + data_size <= size) {
1552 inarg->size = data_size;
1553 } else if (inarg->offset < size) {
1554 inarg->size = size - inarg->offset;
1556 /* Got truncated off completely */
1560 req->in.args[1].size = inarg->size;
1561 queued = fuse_request_queue_background(fc, req);
1562 /* Fails on broken connection only */
1563 if (unlikely(!queued))
1570 fuse_writepage_finish(fc, req);
1571 spin_unlock(&fi->lock);
1573 /* After fuse_writepage_finish() aux request list is private */
1574 for (aux = req->misc.write.next; aux; aux = next) {
1575 next = aux->misc.write.next;
1576 aux->misc.write.next = NULL;
1577 fuse_writepage_free(fc, aux);
1578 fuse_put_request(fc, aux);
1581 fuse_writepage_free(fc, req);
1582 fuse_put_request(fc, req);
1583 spin_lock(&fi->lock);
1587 * If fi->writectr is positive (no truncate or fsync going on) send
1588 * all queued writepage requests.
1590 * Called with fi->lock
1592 void fuse_flush_writepages(struct inode *inode)
1593 __releases(fi->lock)
1594 __acquires(fi->lock)
1596 struct fuse_conn *fc = get_fuse_conn(inode);
1597 struct fuse_inode *fi = get_fuse_inode(inode);
1598 loff_t crop = i_size_read(inode);
1599 struct fuse_req *req;
1601 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1602 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1603 list_del_init(&req->list);
1604 fuse_send_writepage(fc, req, crop);
1608 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1610 struct inode *inode = req->inode;
1611 struct fuse_inode *fi = get_fuse_inode(inode);
1613 mapping_set_error(inode->i_mapping, req->out.h.error);
1614 spin_lock(&fi->lock);
1615 while (req->misc.write.next) {
1616 struct fuse_conn *fc = get_fuse_conn(inode);
1617 struct fuse_write_in *inarg = &req->misc.write.in;
1618 struct fuse_req *next = req->misc.write.next;
1619 req->misc.write.next = next->misc.write.next;
1620 next->misc.write.next = NULL;
1621 next->ff = fuse_file_get(req->ff);
1622 list_add(&next->writepages_entry, &fi->writepages);
1625 * Skip fuse_flush_writepages() to make it easy to crop requests
1626 * based on primary request size.
1628 * 1st case (trivial): there are no concurrent activities using
1629 * fuse_set/release_nowrite. Then we're on safe side because
1630 * fuse_flush_writepages() would call fuse_send_writepage()
1633 * 2nd case: someone called fuse_set_nowrite and it is waiting
1634 * now for completion of all in-flight requests. This happens
1635 * rarely and no more than once per page, so this should be
1638 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1639 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1640 * that fuse_set_nowrite returned implies that all in-flight
1641 * requests were completed along with all of their secondary
1642 * requests. Further primary requests are blocked by negative
1643 * writectr. Hence there cannot be any in-flight requests and
1644 * no invocations of fuse_writepage_end() while we're in
1645 * fuse_set_nowrite..fuse_release_nowrite section.
1647 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1650 fuse_writepage_finish(fc, req);
1651 spin_unlock(&fi->lock);
1652 fuse_writepage_free(fc, req);
1655 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1656 struct fuse_inode *fi)
1658 struct fuse_file *ff = NULL;
1660 spin_lock(&fi->lock);
1661 if (!list_empty(&fi->write_files)) {
1662 ff = list_entry(fi->write_files.next, struct fuse_file,
1666 spin_unlock(&fi->lock);
1671 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1672 struct fuse_inode *fi)
1674 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1679 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1681 struct fuse_conn *fc = get_fuse_conn(inode);
1682 struct fuse_inode *fi = get_fuse_inode(inode);
1683 struct fuse_file *ff;
1686 ff = __fuse_write_file_get(fc, fi);
1687 err = fuse_flush_times(inode, ff);
1689 fuse_file_put(ff, false, false);
1694 static int fuse_writepage_locked(struct page *page)
1696 struct address_space *mapping = page->mapping;
1697 struct inode *inode = mapping->host;
1698 struct fuse_conn *fc = get_fuse_conn(inode);
1699 struct fuse_inode *fi = get_fuse_inode(inode);
1700 struct fuse_req *req;
1701 struct page *tmp_page;
1702 int error = -ENOMEM;
1704 set_page_writeback(page);
1706 req = fuse_request_alloc_nofs(1);
1710 /* writeback always goes to bg_queue */
1711 __set_bit(FR_BACKGROUND, &req->flags);
1712 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1717 req->ff = fuse_write_file_get(fc, fi);
1721 fuse_write_fill(req, req->ff, page_offset(page), 0);
1723 copy_highpage(tmp_page, page);
1724 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1725 req->misc.write.next = NULL;
1726 req->in.argpages = 1;
1728 req->pages[0] = tmp_page;
1729 req->page_descs[0].offset = 0;
1730 req->page_descs[0].length = PAGE_SIZE;
1731 req->end = fuse_writepage_end;
1734 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1735 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1737 spin_lock(&fi->lock);
1738 list_add(&req->writepages_entry, &fi->writepages);
1739 list_add_tail(&req->list, &fi->queued_writes);
1740 fuse_flush_writepages(inode);
1741 spin_unlock(&fi->lock);
1743 end_page_writeback(page);
1748 __free_page(tmp_page);
1750 fuse_request_free(req);
1752 mapping_set_error(page->mapping, error);
1753 end_page_writeback(page);
1757 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1761 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1763 * ->writepages() should be called for sync() and friends. We
1764 * should only get here on direct reclaim and then we are
1765 * allowed to skip a page which is already in flight
1767 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1769 redirty_page_for_writepage(wbc, page);
1773 err = fuse_writepage_locked(page);
1779 struct fuse_fill_wb_data {
1780 struct fuse_req *req;
1781 struct fuse_file *ff;
1782 struct inode *inode;
1783 struct page **orig_pages;
1786 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1788 struct fuse_req *req = data->req;
1789 struct inode *inode = data->inode;
1790 struct fuse_inode *fi = get_fuse_inode(inode);
1791 int num_pages = req->num_pages;
1794 req->ff = fuse_file_get(data->ff);
1795 spin_lock(&fi->lock);
1796 list_add_tail(&req->list, &fi->queued_writes);
1797 fuse_flush_writepages(inode);
1798 spin_unlock(&fi->lock);
1800 for (i = 0; i < num_pages; i++)
1801 end_page_writeback(data->orig_pages[i]);
1805 * First recheck under fi->lock if the offending offset is still under
1806 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1807 * one already added for a page at this offset. If there's none, then insert
1808 * this new request onto the auxiliary list, otherwise reuse the existing one by
1809 * copying the new page contents over to the old temporary page.
1811 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1814 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1815 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1816 struct fuse_req *tmp;
1817 struct fuse_req *old_req;
1819 WARN_ON(new_req->num_pages != 0);
1821 spin_lock(&fi->lock);
1822 list_del(&new_req->writepages_entry);
1823 old_req = fuse_find_writeback(fi, page->index, page->index);
1825 list_add(&new_req->writepages_entry, &fi->writepages);
1826 spin_unlock(&fi->lock);
1830 new_req->num_pages = 1;
1831 for (tmp = old_req->misc.write.next; tmp; tmp = tmp->misc.write.next) {
1834 WARN_ON(tmp->inode != new_req->inode);
1835 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1836 if (curr_index == page->index) {
1837 WARN_ON(tmp->num_pages != 1);
1838 WARN_ON(!test_bit(FR_PENDING, &tmp->flags));
1839 swap(tmp->pages[0], new_req->pages[0]);
1845 new_req->misc.write.next = old_req->misc.write.next;
1846 old_req->misc.write.next = new_req;
1849 spin_unlock(&fi->lock);
1852 struct backing_dev_info *bdi = inode_to_bdi(new_req->inode);
1854 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1855 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1856 wb_writeout_inc(&bdi->wb);
1857 fuse_writepage_free(fc, new_req);
1858 fuse_request_free(new_req);
1864 static int fuse_writepages_fill(struct page *page,
1865 struct writeback_control *wbc, void *_data)
1867 struct fuse_fill_wb_data *data = _data;
1868 struct fuse_req *req = data->req;
1869 struct inode *inode = data->inode;
1870 struct fuse_inode *fi = get_fuse_inode(inode);
1871 struct fuse_conn *fc = get_fuse_conn(inode);
1872 struct page *tmp_page;
1878 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1884 * Being under writeback is unlikely but possible. For example direct
1885 * read to an mmaped fuse file will set the page dirty twice; once when
1886 * the pages are faulted with get_user_pages(), and then after the read
1889 is_writeback = fuse_page_is_writeback(inode, page->index);
1891 if (req && req->num_pages &&
1892 (is_writeback || req->num_pages == fc->max_pages ||
1893 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1894 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1895 fuse_writepages_send(data);
1897 } else if (req && req->num_pages == req->max_pages) {
1898 if (!fuse_req_realloc_pages(fc, req, GFP_NOFS)) {
1899 fuse_writepages_send(data);
1900 req = data->req = NULL;
1905 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1910 * The page must not be redirtied until the writeout is completed
1911 * (i.e. userspace has sent a reply to the write request). Otherwise
1912 * there could be more than one temporary page instance for each real
1915 * This is ensured by holding the page lock in page_mkwrite() while
1916 * checking fuse_page_is_writeback(). We already hold the page lock
1917 * since clear_page_dirty_for_io() and keep it held until we add the
1918 * request to the fi->writepages list and increment req->num_pages.
1919 * After this fuse_page_is_writeback() will indicate that the page is
1920 * under writeback, so we can release the page lock.
1922 if (data->req == NULL) {
1923 struct fuse_inode *fi = get_fuse_inode(inode);
1926 req = fuse_request_alloc_nofs(FUSE_REQ_INLINE_PAGES);
1928 __free_page(tmp_page);
1932 fuse_write_fill(req, data->ff, page_offset(page), 0);
1933 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1934 req->misc.write.next = NULL;
1935 req->in.argpages = 1;
1936 __set_bit(FR_BACKGROUND, &req->flags);
1938 req->end = fuse_writepage_end;
1941 spin_lock(&fi->lock);
1942 list_add(&req->writepages_entry, &fi->writepages);
1943 spin_unlock(&fi->lock);
1947 set_page_writeback(page);
1949 copy_highpage(tmp_page, page);
1950 req->pages[req->num_pages] = tmp_page;
1951 req->page_descs[req->num_pages].offset = 0;
1952 req->page_descs[req->num_pages].length = PAGE_SIZE;
1954 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1955 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1958 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1959 end_page_writeback(page);
1963 data->orig_pages[req->num_pages] = page;
1966 * Protected by fi->lock against concurrent access by
1967 * fuse_page_is_writeback().
1969 spin_lock(&fi->lock);
1971 spin_unlock(&fi->lock);
1979 static int fuse_writepages(struct address_space *mapping,
1980 struct writeback_control *wbc)
1982 struct inode *inode = mapping->host;
1983 struct fuse_conn *fc = get_fuse_conn(inode);
1984 struct fuse_fill_wb_data data;
1988 if (is_bad_inode(inode))
1996 data.orig_pages = kcalloc(fc->max_pages,
1997 sizeof(struct page *),
1999 if (!data.orig_pages)
2002 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2004 /* Ignore errors if we can write at least one page */
2005 BUG_ON(!data.req->num_pages);
2006 fuse_writepages_send(&data);
2010 fuse_file_put(data.ff, false, false);
2012 kfree(data.orig_pages);
2018 * It's worthy to make sure that space is reserved on disk for the write,
2019 * but how to implement it without killing performance need more thinking.
2021 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2022 loff_t pos, unsigned len, unsigned flags,
2023 struct page **pagep, void **fsdata)
2025 pgoff_t index = pos >> PAGE_SHIFT;
2026 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2031 WARN_ON(!fc->writeback_cache);
2033 page = grab_cache_page_write_begin(mapping, index, flags);
2037 fuse_wait_on_page_writeback(mapping->host, page->index);
2039 if (PageUptodate(page) || len == PAGE_SIZE)
2042 * Check if the start this page comes after the end of file, in which
2043 * case the readpage can be optimized away.
2045 fsize = i_size_read(mapping->host);
2046 if (fsize <= (pos & PAGE_MASK)) {
2047 size_t off = pos & ~PAGE_MASK;
2049 zero_user_segment(page, 0, off);
2052 err = fuse_do_readpage(file, page);
2066 static int fuse_write_end(struct file *file, struct address_space *mapping,
2067 loff_t pos, unsigned len, unsigned copied,
2068 struct page *page, void *fsdata)
2070 struct inode *inode = page->mapping->host;
2072 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2076 if (!PageUptodate(page)) {
2077 /* Zero any unwritten bytes at the end of the page */
2078 size_t endoff = (pos + copied) & ~PAGE_MASK;
2080 zero_user_segment(page, endoff, PAGE_SIZE);
2081 SetPageUptodate(page);
2084 fuse_write_update_size(inode, pos + copied);
2085 set_page_dirty(page);
2094 static int fuse_launder_page(struct page *page)
2097 if (clear_page_dirty_for_io(page)) {
2098 struct inode *inode = page->mapping->host;
2099 err = fuse_writepage_locked(page);
2101 fuse_wait_on_page_writeback(inode, page->index);
2107 * Write back dirty pages now, because there may not be any suitable
2110 static void fuse_vma_close(struct vm_area_struct *vma)
2112 filemap_write_and_wait(vma->vm_file->f_mapping);
2116 * Wait for writeback against this page to complete before allowing it
2117 * to be marked dirty again, and hence written back again, possibly
2118 * before the previous writepage completed.
2120 * Block here, instead of in ->writepage(), so that the userspace fs
2121 * can only block processes actually operating on the filesystem.
2123 * Otherwise unprivileged userspace fs would be able to block
2128 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2130 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2132 struct page *page = vmf->page;
2133 struct inode *inode = file_inode(vmf->vma->vm_file);
2135 file_update_time(vmf->vma->vm_file);
2137 if (page->mapping != inode->i_mapping) {
2139 return VM_FAULT_NOPAGE;
2142 fuse_wait_on_page_writeback(inode, page->index);
2143 return VM_FAULT_LOCKED;
2146 static const struct vm_operations_struct fuse_file_vm_ops = {
2147 .close = fuse_vma_close,
2148 .fault = filemap_fault,
2149 .map_pages = filemap_map_pages,
2150 .page_mkwrite = fuse_page_mkwrite,
2153 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2155 struct fuse_file *ff = file->private_data;
2157 if (ff->open_flags & FOPEN_DIRECT_IO) {
2158 /* Can't provide the coherency needed for MAP_SHARED */
2159 if (vma->vm_flags & VM_MAYSHARE)
2162 invalidate_inode_pages2(file->f_mapping);
2164 return generic_file_mmap(file, vma);
2167 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2168 fuse_link_write_file(file);
2170 file_accessed(file);
2171 vma->vm_ops = &fuse_file_vm_ops;
2175 static int convert_fuse_file_lock(struct fuse_conn *fc,
2176 const struct fuse_file_lock *ffl,
2177 struct file_lock *fl)
2179 switch (ffl->type) {
2185 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2186 ffl->end < ffl->start)
2189 fl->fl_start = ffl->start;
2190 fl->fl_end = ffl->end;
2193 * Convert pid into init's pid namespace. The locks API will
2194 * translate it into the caller's pid namespace.
2197 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2204 fl->fl_type = ffl->type;
2208 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2209 const struct file_lock *fl, int opcode, pid_t pid,
2210 int flock, struct fuse_lk_in *inarg)
2212 struct inode *inode = file_inode(file);
2213 struct fuse_conn *fc = get_fuse_conn(inode);
2214 struct fuse_file *ff = file->private_data;
2216 memset(inarg, 0, sizeof(*inarg));
2218 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2219 inarg->lk.start = fl->fl_start;
2220 inarg->lk.end = fl->fl_end;
2221 inarg->lk.type = fl->fl_type;
2222 inarg->lk.pid = pid;
2224 inarg->lk_flags |= FUSE_LK_FLOCK;
2225 args->in.h.opcode = opcode;
2226 args->in.h.nodeid = get_node_id(inode);
2227 args->in.numargs = 1;
2228 args->in.args[0].size = sizeof(*inarg);
2229 args->in.args[0].value = inarg;
2232 static int fuse_getlk(struct file *file, struct file_lock *fl)
2234 struct inode *inode = file_inode(file);
2235 struct fuse_conn *fc = get_fuse_conn(inode);
2237 struct fuse_lk_in inarg;
2238 struct fuse_lk_out outarg;
2241 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2242 args.out.numargs = 1;
2243 args.out.args[0].size = sizeof(outarg);
2244 args.out.args[0].value = &outarg;
2245 err = fuse_simple_request(fc, &args);
2247 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2252 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2254 struct inode *inode = file_inode(file);
2255 struct fuse_conn *fc = get_fuse_conn(inode);
2257 struct fuse_lk_in inarg;
2258 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2259 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2260 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2263 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2264 /* NLM needs asynchronous locks, which we don't support yet */
2268 /* Unlock on close is handled by the flush method */
2269 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2272 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2273 err = fuse_simple_request(fc, &args);
2275 /* locking is restartable */
2282 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2284 struct inode *inode = file_inode(file);
2285 struct fuse_conn *fc = get_fuse_conn(inode);
2288 if (cmd == F_CANCELLK) {
2290 } else if (cmd == F_GETLK) {
2292 posix_test_lock(file, fl);
2295 err = fuse_getlk(file, fl);
2298 err = posix_lock_file(file, fl, NULL);
2300 err = fuse_setlk(file, fl, 0);
2305 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2307 struct inode *inode = file_inode(file);
2308 struct fuse_conn *fc = get_fuse_conn(inode);
2312 err = locks_lock_file_wait(file, fl);
2314 struct fuse_file *ff = file->private_data;
2316 /* emulate flock with POSIX locks */
2318 err = fuse_setlk(file, fl, 1);
2324 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2326 struct inode *inode = mapping->host;
2327 struct fuse_conn *fc = get_fuse_conn(inode);
2329 struct fuse_bmap_in inarg;
2330 struct fuse_bmap_out outarg;
2333 if (!inode->i_sb->s_bdev || fc->no_bmap)
2336 memset(&inarg, 0, sizeof(inarg));
2337 inarg.block = block;
2338 inarg.blocksize = inode->i_sb->s_blocksize;
2339 args.in.h.opcode = FUSE_BMAP;
2340 args.in.h.nodeid = get_node_id(inode);
2341 args.in.numargs = 1;
2342 args.in.args[0].size = sizeof(inarg);
2343 args.in.args[0].value = &inarg;
2344 args.out.numargs = 1;
2345 args.out.args[0].size = sizeof(outarg);
2346 args.out.args[0].value = &outarg;
2347 err = fuse_simple_request(fc, &args);
2351 return err ? 0 : outarg.block;
2354 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2356 struct inode *inode = file->f_mapping->host;
2357 struct fuse_conn *fc = get_fuse_conn(inode);
2358 struct fuse_file *ff = file->private_data;
2360 struct fuse_lseek_in inarg = {
2365 struct fuse_lseek_out outarg;
2371 args.in.h.opcode = FUSE_LSEEK;
2372 args.in.h.nodeid = ff->nodeid;
2373 args.in.numargs = 1;
2374 args.in.args[0].size = sizeof(inarg);
2375 args.in.args[0].value = &inarg;
2376 args.out.numargs = 1;
2377 args.out.args[0].size = sizeof(outarg);
2378 args.out.args[0].value = &outarg;
2379 err = fuse_simple_request(fc, &args);
2381 if (err == -ENOSYS) {
2388 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2391 err = fuse_update_attributes(inode, file);
2393 return generic_file_llseek(file, offset, whence);
2398 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2401 struct inode *inode = file_inode(file);
2406 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2407 retval = generic_file_llseek(file, offset, whence);
2411 retval = fuse_update_attributes(inode, file);
2413 retval = generic_file_llseek(file, offset, whence);
2414 inode_unlock(inode);
2419 retval = fuse_lseek(file, offset, whence);
2420 inode_unlock(inode);
2430 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2431 * ABI was defined to be 'struct iovec' which is different on 32bit
2432 * and 64bit. Fortunately we can determine which structure the server
2433 * used from the size of the reply.
2435 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2436 size_t transferred, unsigned count,
2439 #ifdef CONFIG_COMPAT
2440 if (count * sizeof(struct compat_iovec) == transferred) {
2441 struct compat_iovec *ciov = src;
2445 * With this interface a 32bit server cannot support
2446 * non-compat (i.e. ones coming from 64bit apps) ioctl
2452 for (i = 0; i < count; i++) {
2453 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2454 dst[i].iov_len = ciov[i].iov_len;
2460 if (count * sizeof(struct iovec) != transferred)
2463 memcpy(dst, src, transferred);
2467 /* Make sure iov_length() won't overflow */
2468 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2472 u32 max = fc->max_pages << PAGE_SHIFT;
2474 for (n = 0; n < count; n++, iov++) {
2475 if (iov->iov_len > (size_t) max)
2477 max -= iov->iov_len;
2482 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2483 void *src, size_t transferred, unsigned count,
2487 struct fuse_ioctl_iovec *fiov = src;
2489 if (fc->minor < 16) {
2490 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2494 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2497 for (i = 0; i < count; i++) {
2498 /* Did the server supply an inappropriate value? */
2499 if (fiov[i].base != (unsigned long) fiov[i].base ||
2500 fiov[i].len != (unsigned long) fiov[i].len)
2503 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2504 dst[i].iov_len = (size_t) fiov[i].len;
2506 #ifdef CONFIG_COMPAT
2508 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2509 (compat_size_t) dst[i].iov_len != fiov[i].len))
2519 * For ioctls, there is no generic way to determine how much memory
2520 * needs to be read and/or written. Furthermore, ioctls are allowed
2521 * to dereference the passed pointer, so the parameter requires deep
2522 * copying but FUSE has no idea whatsoever about what to copy in or
2525 * This is solved by allowing FUSE server to retry ioctl with
2526 * necessary in/out iovecs. Let's assume the ioctl implementation
2527 * needs to read in the following structure.
2534 * On the first callout to FUSE server, inarg->in_size and
2535 * inarg->out_size will be NULL; then, the server completes the ioctl
2536 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2537 * the actual iov array to
2539 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2541 * which tells FUSE to copy in the requested area and retry the ioctl.
2542 * On the second round, the server has access to the structure and
2543 * from that it can tell what to look for next, so on the invocation,
2544 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2546 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2547 * { .iov_base = a.buf, .iov_len = a.buflen } }
2549 * FUSE will copy both struct a and the pointed buffer from the
2550 * process doing the ioctl and retry ioctl with both struct a and the
2553 * This time, FUSE server has everything it needs and completes ioctl
2554 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2556 * Copying data out works the same way.
2558 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2559 * automatically initializes in and out iovs by decoding @cmd with
2560 * _IOC_* macros and the server is not allowed to request RETRY. This
2561 * limits ioctl data transfers to well-formed ioctls and is the forced
2562 * behavior for all FUSE servers.
2564 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2567 struct fuse_file *ff = file->private_data;
2568 struct fuse_conn *fc = ff->fc;
2569 struct fuse_ioctl_in inarg = {
2575 struct fuse_ioctl_out outarg;
2576 struct fuse_req *req = NULL;
2577 struct page **pages = NULL;
2578 struct iovec *iov_page = NULL;
2579 struct iovec *in_iov = NULL, *out_iov = NULL;
2580 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2581 size_t in_size, out_size, transferred, c;
2585 #if BITS_PER_LONG == 32
2586 inarg.flags |= FUSE_IOCTL_32BIT;
2588 if (flags & FUSE_IOCTL_COMPAT) {
2589 inarg.flags |= FUSE_IOCTL_32BIT;
2590 #ifdef CONFIG_X86_X32
2591 if (in_x32_syscall())
2592 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2597 /* assume all the iovs returned by client always fits in a page */
2598 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2601 pages = kcalloc(fc->max_pages, sizeof(pages[0]), GFP_KERNEL);
2602 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2603 if (!pages || !iov_page)
2607 * If restricted, initialize IO parameters as encoded in @cmd.
2608 * RETRY from server is not allowed.
2610 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2611 struct iovec *iov = iov_page;
2613 iov->iov_base = (void __user *)arg;
2614 iov->iov_len = _IOC_SIZE(cmd);
2616 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2621 if (_IOC_DIR(cmd) & _IOC_READ) {
2628 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2629 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2632 * Out data can be used either for actual out data or iovs,
2633 * make sure there always is at least one page.
2635 out_size = max_t(size_t, out_size, PAGE_SIZE);
2636 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2638 /* make sure there are enough buffer pages and init request with them */
2640 if (max_pages > fc->max_pages)
2642 while (num_pages < max_pages) {
2643 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2644 if (!pages[num_pages])
2649 req = fuse_get_req(fc, num_pages);
2655 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2656 req->num_pages = num_pages;
2657 fuse_page_descs_length_init(req, 0, req->num_pages);
2659 /* okay, let's send it to the client */
2660 req->in.h.opcode = FUSE_IOCTL;
2661 req->in.h.nodeid = ff->nodeid;
2662 req->in.numargs = 1;
2663 req->in.args[0].size = sizeof(inarg);
2664 req->in.args[0].value = &inarg;
2667 req->in.args[1].size = in_size;
2668 req->in.argpages = 1;
2671 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2672 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2673 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2674 if (c != PAGE_SIZE && iov_iter_count(&ii))
2679 req->out.numargs = 2;
2680 req->out.args[0].size = sizeof(outarg);
2681 req->out.args[0].value = &outarg;
2682 req->out.args[1].size = out_size;
2683 req->out.argpages = 1;
2684 req->out.argvar = 1;
2686 fuse_request_send(fc, req);
2687 err = req->out.h.error;
2688 transferred = req->out.args[1].size;
2689 fuse_put_request(fc, req);
2694 /* did it ask for retry? */
2695 if (outarg.flags & FUSE_IOCTL_RETRY) {
2698 /* no retry if in restricted mode */
2700 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2703 in_iovs = outarg.in_iovs;
2704 out_iovs = outarg.out_iovs;
2707 * Make sure things are in boundary, separate checks
2708 * are to protect against overflow.
2711 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2712 out_iovs > FUSE_IOCTL_MAX_IOV ||
2713 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2716 vaddr = kmap_atomic(pages[0]);
2717 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2718 transferred, in_iovs + out_iovs,
2719 (flags & FUSE_IOCTL_COMPAT) != 0);
2720 kunmap_atomic(vaddr);
2725 out_iov = in_iov + in_iovs;
2727 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2731 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2739 if (transferred > inarg.out_size)
2743 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2744 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2745 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2746 if (c != PAGE_SIZE && iov_iter_count(&ii))
2752 fuse_put_request(fc, req);
2753 free_page((unsigned long) iov_page);
2755 __free_page(pages[--num_pages]);
2758 return err ? err : outarg.result;
2760 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2762 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2763 unsigned long arg, unsigned int flags)
2765 struct inode *inode = file_inode(file);
2766 struct fuse_conn *fc = get_fuse_conn(inode);
2768 if (!fuse_allow_current_process(fc))
2771 if (is_bad_inode(inode))
2774 return fuse_do_ioctl(file, cmd, arg, flags);
2777 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2780 return fuse_ioctl_common(file, cmd, arg, 0);
2783 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2786 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2790 * All files which have been polled are linked to RB tree
2791 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2792 * find the matching one.
2794 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2795 struct rb_node **parent_out)
2797 struct rb_node **link = &fc->polled_files.rb_node;
2798 struct rb_node *last = NULL;
2801 struct fuse_file *ff;
2804 ff = rb_entry(last, struct fuse_file, polled_node);
2807 link = &last->rb_left;
2808 else if (kh > ff->kh)
2809 link = &last->rb_right;
2820 * The file is about to be polled. Make sure it's on the polled_files
2821 * RB tree. Note that files once added to the polled_files tree are
2822 * not removed before the file is released. This is because a file
2823 * polled once is likely to be polled again.
2825 static void fuse_register_polled_file(struct fuse_conn *fc,
2826 struct fuse_file *ff)
2828 spin_lock(&fc->lock);
2829 if (RB_EMPTY_NODE(&ff->polled_node)) {
2830 struct rb_node **link, *uninitialized_var(parent);
2832 link = fuse_find_polled_node(fc, ff->kh, &parent);
2834 rb_link_node(&ff->polled_node, parent, link);
2835 rb_insert_color(&ff->polled_node, &fc->polled_files);
2837 spin_unlock(&fc->lock);
2840 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2842 struct fuse_file *ff = file->private_data;
2843 struct fuse_conn *fc = ff->fc;
2844 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2845 struct fuse_poll_out outarg;
2850 return DEFAULT_POLLMASK;
2852 poll_wait(file, &ff->poll_wait, wait);
2853 inarg.events = mangle_poll(poll_requested_events(wait));
2856 * Ask for notification iff there's someone waiting for it.
2857 * The client may ignore the flag and always notify.
2859 if (waitqueue_active(&ff->poll_wait)) {
2860 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2861 fuse_register_polled_file(fc, ff);
2864 args.in.h.opcode = FUSE_POLL;
2865 args.in.h.nodeid = ff->nodeid;
2866 args.in.numargs = 1;
2867 args.in.args[0].size = sizeof(inarg);
2868 args.in.args[0].value = &inarg;
2869 args.out.numargs = 1;
2870 args.out.args[0].size = sizeof(outarg);
2871 args.out.args[0].value = &outarg;
2872 err = fuse_simple_request(fc, &args);
2875 return demangle_poll(outarg.revents);
2876 if (err == -ENOSYS) {
2878 return DEFAULT_POLLMASK;
2882 EXPORT_SYMBOL_GPL(fuse_file_poll);
2885 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2886 * wakes up the poll waiters.
2888 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2889 struct fuse_notify_poll_wakeup_out *outarg)
2891 u64 kh = outarg->kh;
2892 struct rb_node **link;
2894 spin_lock(&fc->lock);
2896 link = fuse_find_polled_node(fc, kh, NULL);
2898 struct fuse_file *ff;
2900 ff = rb_entry(*link, struct fuse_file, polled_node);
2901 wake_up_interruptible_sync(&ff->poll_wait);
2904 spin_unlock(&fc->lock);
2908 static void fuse_do_truncate(struct file *file)
2910 struct inode *inode = file->f_mapping->host;
2913 attr.ia_valid = ATTR_SIZE;
2914 attr.ia_size = i_size_read(inode);
2916 attr.ia_file = file;
2917 attr.ia_valid |= ATTR_FILE;
2919 fuse_do_setattr(file_dentry(file), &attr, file);
2922 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
2924 return round_up(off, fc->max_pages << PAGE_SHIFT);
2928 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2930 DECLARE_COMPLETION_ONSTACK(wait);
2932 struct file *file = iocb->ki_filp;
2933 struct fuse_file *ff = file->private_data;
2934 bool async_dio = ff->fc->async_dio;
2936 struct inode *inode;
2938 size_t count = iov_iter_count(iter);
2939 loff_t offset = iocb->ki_pos;
2940 struct fuse_io_priv *io;
2943 inode = file->f_mapping->host;
2944 i_size = i_size_read(inode);
2946 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2949 /* optimization for short read */
2950 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2951 if (offset >= i_size)
2953 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
2954 count = iov_iter_count(iter);
2957 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2960 spin_lock_init(&io->lock);
2961 kref_init(&io->refcnt);
2965 io->offset = offset;
2966 io->write = (iov_iter_rw(iter) == WRITE);
2969 * By default, we want to optimize all I/Os with async request
2970 * submission to the client filesystem if supported.
2972 io->async = async_dio;
2974 io->blocking = is_sync_kiocb(iocb);
2977 * We cannot asynchronously extend the size of a file.
2978 * In such case the aio will behave exactly like sync io.
2980 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2981 io->blocking = true;
2983 if (io->async && io->blocking) {
2985 * Additional reference to keep io around after
2986 * calling fuse_aio_complete()
2988 kref_get(&io->refcnt);
2992 if (iov_iter_rw(iter) == WRITE) {
2993 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2994 fuse_invalidate_attr(inode);
2996 ret = __fuse_direct_read(io, iter, &pos);
3000 bool blocking = io->blocking;
3002 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3004 /* we have a non-extending, async request, so return */
3006 return -EIOCBQUEUED;
3008 wait_for_completion(&wait);
3009 ret = fuse_get_res_by_io(io);
3012 kref_put(&io->refcnt, fuse_io_release);
3014 if (iov_iter_rw(iter) == WRITE) {
3016 fuse_write_update_size(inode, pos);
3017 else if (ret < 0 && offset + count > i_size)
3018 fuse_do_truncate(file);
3024 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3026 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3029 fuse_sync_writes(inode);
3034 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3037 struct fuse_file *ff = file->private_data;
3038 struct inode *inode = file_inode(file);
3039 struct fuse_inode *fi = get_fuse_inode(inode);
3040 struct fuse_conn *fc = ff->fc;
3042 struct fuse_fallocate_in inarg = {
3049 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3050 (mode & FALLOC_FL_PUNCH_HOLE);
3052 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3055 if (fc->no_fallocate)
3060 if (mode & FALLOC_FL_PUNCH_HOLE) {
3061 loff_t endbyte = offset + length - 1;
3063 err = fuse_writeback_range(inode, offset, endbyte);
3069 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3070 offset + length > i_size_read(inode)) {
3071 err = inode_newsize_ok(inode, offset + length);
3076 if (!(mode & FALLOC_FL_KEEP_SIZE))
3077 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3079 args.in.h.opcode = FUSE_FALLOCATE;
3080 args.in.h.nodeid = ff->nodeid;
3081 args.in.numargs = 1;
3082 args.in.args[0].size = sizeof(inarg);
3083 args.in.args[0].value = &inarg;
3084 err = fuse_simple_request(fc, &args);
3085 if (err == -ENOSYS) {
3086 fc->no_fallocate = 1;
3092 /* we could have extended the file */
3093 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3094 bool changed = fuse_write_update_size(inode, offset + length);
3096 if (changed && fc->writeback_cache)
3097 file_update_time(file);
3100 if (mode & FALLOC_FL_PUNCH_HOLE)
3101 truncate_pagecache_range(inode, offset, offset + length - 1);
3103 fuse_invalidate_attr(inode);
3106 if (!(mode & FALLOC_FL_KEEP_SIZE))
3107 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3110 inode_unlock(inode);
3115 static ssize_t fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3116 struct file *file_out, loff_t pos_out,
3117 size_t len, unsigned int flags)
3119 struct fuse_file *ff_in = file_in->private_data;
3120 struct fuse_file *ff_out = file_out->private_data;
3121 struct inode *inode_in = file_inode(file_in);
3122 struct inode *inode_out = file_inode(file_out);
3123 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3124 struct fuse_conn *fc = ff_in->fc;
3126 struct fuse_copy_file_range_in inarg = {
3129 .nodeid_out = ff_out->nodeid,
3130 .fh_out = ff_out->fh,
3135 struct fuse_write_out outarg;
3137 /* mark unstable when write-back is not used, and file_out gets
3139 bool is_unstable = (!fc->writeback_cache) &&
3140 ((pos_out + len) > inode_out->i_size);
3142 if (fc->no_copy_file_range)
3145 if (fc->writeback_cache) {
3146 inode_lock(inode_in);
3147 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
3148 inode_unlock(inode_in);
3153 inode_lock(inode_out);
3155 if (fc->writeback_cache) {
3156 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
3162 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3164 args.in.h.opcode = FUSE_COPY_FILE_RANGE;
3165 args.in.h.nodeid = ff_in->nodeid;
3166 args.in.numargs = 1;
3167 args.in.args[0].size = sizeof(inarg);
3168 args.in.args[0].value = &inarg;
3169 args.out.numargs = 1;
3170 args.out.args[0].size = sizeof(outarg);
3171 args.out.args[0].value = &outarg;
3172 err = fuse_simple_request(fc, &args);
3173 if (err == -ENOSYS) {
3174 fc->no_copy_file_range = 1;
3180 if (fc->writeback_cache) {
3181 fuse_write_update_size(inode_out, pos_out + outarg.size);
3182 file_update_time(file_out);
3185 fuse_invalidate_attr(inode_out);
3190 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3192 inode_unlock(inode_out);
3197 static const struct file_operations fuse_file_operations = {
3198 .llseek = fuse_file_llseek,
3199 .read_iter = fuse_file_read_iter,
3200 .write_iter = fuse_file_write_iter,
3201 .mmap = fuse_file_mmap,
3203 .flush = fuse_flush,
3204 .release = fuse_release,
3205 .fsync = fuse_fsync,
3206 .lock = fuse_file_lock,
3207 .flock = fuse_file_flock,
3208 .splice_read = generic_file_splice_read,
3209 .splice_write = iter_file_splice_write,
3210 .unlocked_ioctl = fuse_file_ioctl,
3211 .compat_ioctl = fuse_file_compat_ioctl,
3212 .poll = fuse_file_poll,
3213 .fallocate = fuse_file_fallocate,
3214 .copy_file_range = fuse_copy_file_range,
3217 static const struct address_space_operations fuse_file_aops = {
3218 .readpage = fuse_readpage,
3219 .writepage = fuse_writepage,
3220 .writepages = fuse_writepages,
3221 .launder_page = fuse_launder_page,
3222 .readpages = fuse_readpages,
3223 .set_page_dirty = __set_page_dirty_nobuffers,
3225 .direct_IO = fuse_direct_IO,
3226 .write_begin = fuse_write_begin,
3227 .write_end = fuse_write_end,
3230 void fuse_init_file_inode(struct inode *inode)
3232 struct fuse_inode *fi = get_fuse_inode(inode);
3234 inode->i_fop = &fuse_file_operations;
3235 inode->i_data.a_ops = &fuse_file_aops;
3237 INIT_LIST_HEAD(&fi->write_files);
3238 INIT_LIST_HEAD(&fi->queued_writes);
3240 init_waitqueue_head(&fi->page_waitq);
3241 INIT_LIST_HEAD(&fi->writepages);