2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/falloc.h>
19 #include <linux/uio.h>
22 static const struct file_operations fuse_direct_io_file_operations;
24 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
25 int opcode, struct fuse_open_out *outargp)
27 struct fuse_open_in inarg;
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
34 args.in.h.opcode = opcode;
35 args.in.h.nodeid = nodeid;
37 args.in.args[0].size = sizeof(inarg);
38 args.in.args[0].value = &inarg;
40 args.out.args[0].size = sizeof(*outargp);
41 args.out.args[0].value = outargp;
43 return fuse_simple_request(fc, &args);
46 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
50 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL);
55 ff->reserved_req = fuse_request_alloc(0);
56 if (unlikely(!ff->reserved_req)) {
61 INIT_LIST_HEAD(&ff->write_entry);
62 refcount_set(&ff->count, 1);
63 RB_CLEAR_NODE(&ff->polled_node);
64 init_waitqueue_head(&ff->poll_wait);
68 spin_unlock(&fc->lock);
73 void fuse_file_free(struct fuse_file *ff)
75 fuse_request_free(ff->reserved_req);
79 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
81 refcount_inc(&ff->count);
85 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
87 iput(req->misc.release.inode);
90 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
92 if (refcount_dec_and_test(&ff->count)) {
93 struct fuse_req *req = ff->reserved_req;
95 if (ff->fc->no_open && !isdir) {
97 * Drop the release request when client does not
100 __clear_bit(FR_BACKGROUND, &req->flags);
101 iput(req->misc.release.inode);
102 fuse_put_request(ff->fc, req);
104 __set_bit(FR_FORCE, &req->flags);
105 __clear_bit(FR_BACKGROUND, &req->flags);
106 fuse_request_send(ff->fc, req);
107 iput(req->misc.release.inode);
108 fuse_put_request(ff->fc, req);
110 req->end = fuse_release_end;
111 __set_bit(FR_BACKGROUND, &req->flags);
112 fuse_request_send_background(ff->fc, req);
118 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
121 struct fuse_file *ff;
122 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
124 ff = fuse_file_alloc(fc);
129 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
130 if (!fc->no_open || isdir) {
131 struct fuse_open_out outarg;
134 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
137 ff->open_flags = outarg.open_flags;
139 } else if (err != -ENOSYS || isdir) {
148 ff->open_flags &= ~FOPEN_DIRECT_IO;
151 file->private_data = ff;
155 EXPORT_SYMBOL_GPL(fuse_do_open);
157 static void fuse_link_write_file(struct file *file)
159 struct inode *inode = file_inode(file);
160 struct fuse_conn *fc = get_fuse_conn(inode);
161 struct fuse_inode *fi = get_fuse_inode(inode);
162 struct fuse_file *ff = file->private_data;
164 * file may be written through mmap, so chain it onto the
165 * inodes's write_file list
167 spin_lock(&fc->lock);
168 if (list_empty(&ff->write_entry))
169 list_add(&ff->write_entry, &fi->write_files);
170 spin_unlock(&fc->lock);
173 void fuse_finish_open(struct inode *inode, struct file *file)
175 struct fuse_file *ff = file->private_data;
176 struct fuse_conn *fc = get_fuse_conn(inode);
178 if (ff->open_flags & FOPEN_DIRECT_IO)
179 file->f_op = &fuse_direct_io_file_operations;
180 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
181 invalidate_inode_pages2(inode->i_mapping);
182 if (ff->open_flags & FOPEN_STREAM)
183 stream_open(inode, file);
184 else if (ff->open_flags & FOPEN_NONSEEKABLE)
185 nonseekable_open(inode, file);
186 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
187 struct fuse_inode *fi = get_fuse_inode(inode);
189 spin_lock(&fc->lock);
190 fi->attr_version = ++fc->attr_version;
191 i_size_write(inode, 0);
192 spin_unlock(&fc->lock);
193 fuse_invalidate_attr(inode);
194 if (fc->writeback_cache)
195 file_update_time(file);
197 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
198 fuse_link_write_file(file);
201 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
203 struct fuse_conn *fc = get_fuse_conn(inode);
205 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
206 fc->atomic_o_trunc &&
209 err = generic_file_open(inode, file);
213 if (is_wb_truncate) {
215 fuse_set_nowrite(inode);
218 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
221 fuse_finish_open(inode, file);
223 if (is_wb_truncate) {
224 fuse_release_nowrite(inode);
231 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
233 struct fuse_conn *fc = ff->fc;
234 struct fuse_req *req = ff->reserved_req;
235 struct fuse_release_in *inarg = &req->misc.release.in;
237 spin_lock(&fc->lock);
238 list_del(&ff->write_entry);
239 if (!RB_EMPTY_NODE(&ff->polled_node))
240 rb_erase(&ff->polled_node, &fc->polled_files);
241 spin_unlock(&fc->lock);
243 wake_up_interruptible_all(&ff->poll_wait);
246 inarg->flags = flags;
247 req->in.h.opcode = opcode;
248 req->in.h.nodeid = ff->nodeid;
250 req->in.args[0].size = sizeof(struct fuse_release_in);
251 req->in.args[0].value = inarg;
254 void fuse_release_common(struct file *file, bool isdir)
256 struct fuse_file *ff = file->private_data;
257 struct fuse_req *req = ff->reserved_req;
258 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
260 fuse_prepare_release(ff, file->f_flags, opcode);
263 struct fuse_release_in *inarg = &req->misc.release.in;
264 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
265 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
268 /* Hold inode until release is finished */
269 req->misc.release.inode = igrab(file_inode(file));
272 * Normally this will send the RELEASE request, however if
273 * some asynchronous READ or WRITE requests are outstanding,
274 * the sending will be delayed.
276 * Make the release synchronous if this is a fuseblk mount,
277 * synchronous RELEASE is allowed (and desirable) in this case
278 * because the server can be trusted not to screw up.
280 fuse_file_put(ff, ff->fc->destroy_req != NULL, isdir);
283 static int fuse_open(struct inode *inode, struct file *file)
285 return fuse_open_common(inode, file, false);
288 static int fuse_release(struct inode *inode, struct file *file)
290 struct fuse_conn *fc = get_fuse_conn(inode);
292 /* see fuse_vma_close() for !writeback_cache case */
293 if (fc->writeback_cache)
294 write_inode_now(inode, 1);
296 fuse_release_common(file, false);
298 /* return value is ignored by VFS */
302 void fuse_sync_release(struct fuse_file *ff, int flags)
304 WARN_ON(refcount_read(&ff->count) > 1);
305 fuse_prepare_release(ff, flags, FUSE_RELEASE);
307 * iput(NULL) is a no-op and since the refcount is 1 and everything's
308 * synchronous, we are fine with not doing igrab() here"
310 fuse_file_put(ff, true, false);
312 EXPORT_SYMBOL_GPL(fuse_sync_release);
315 * Scramble the ID space with XTEA, so that the value of the files_struct
316 * pointer is not exposed to userspace.
318 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
320 u32 *k = fc->scramble_key;
321 u64 v = (unsigned long) id;
327 for (i = 0; i < 32; i++) {
328 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
330 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
333 return (u64) v0 + ((u64) v1 << 32);
337 * Check if any page in a range is under writeback
339 * This is currently done by walking the list of writepage requests
340 * for the inode, which can be pretty inefficient.
342 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
345 struct fuse_conn *fc = get_fuse_conn(inode);
346 struct fuse_inode *fi = get_fuse_inode(inode);
347 struct fuse_req *req;
350 spin_lock(&fc->lock);
351 list_for_each_entry(req, &fi->writepages, writepages_entry) {
354 BUG_ON(req->inode != inode);
355 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
356 if (idx_from < curr_index + req->num_pages &&
357 curr_index <= idx_to) {
362 spin_unlock(&fc->lock);
367 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
369 return fuse_range_is_writeback(inode, index, index);
373 * Wait for page writeback to be completed.
375 * Since fuse doesn't rely on the VM writeback tracking, this has to
376 * use some other means.
378 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
380 struct fuse_inode *fi = get_fuse_inode(inode);
382 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
387 * Wait for all pending writepages on the inode to finish.
389 * This is currently done by blocking further writes with FUSE_NOWRITE
390 * and waiting for all sent writes to complete.
392 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
393 * could conflict with truncation.
395 static void fuse_sync_writes(struct inode *inode)
397 fuse_set_nowrite(inode);
398 fuse_release_nowrite(inode);
401 static int fuse_flush(struct file *file, fl_owner_t id)
403 struct inode *inode = file_inode(file);
404 struct fuse_conn *fc = get_fuse_conn(inode);
405 struct fuse_file *ff = file->private_data;
406 struct fuse_req *req;
407 struct fuse_flush_in inarg;
410 if (is_bad_inode(inode))
416 err = write_inode_now(inode, 1);
421 fuse_sync_writes(inode);
424 err = filemap_check_errors(file->f_mapping);
428 req = fuse_get_req_nofail_nopages(fc, file);
429 memset(&inarg, 0, sizeof(inarg));
431 inarg.lock_owner = fuse_lock_owner_id(fc, id);
432 req->in.h.opcode = FUSE_FLUSH;
433 req->in.h.nodeid = get_node_id(inode);
435 req->in.args[0].size = sizeof(inarg);
436 req->in.args[0].value = &inarg;
437 __set_bit(FR_FORCE, &req->flags);
438 fuse_request_send(fc, req);
439 err = req->out.h.error;
440 fuse_put_request(fc, req);
441 if (err == -ENOSYS) {
448 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
449 int datasync, int isdir)
451 struct inode *inode = file->f_mapping->host;
452 struct fuse_conn *fc = get_fuse_conn(inode);
453 struct fuse_file *ff = file->private_data;
455 struct fuse_fsync_in inarg;
458 if (is_bad_inode(inode))
464 * Start writeback against all dirty pages of the inode, then
465 * wait for all outstanding writes, before sending the FSYNC
468 err = file_write_and_wait_range(file, start, end);
472 fuse_sync_writes(inode);
475 * Due to implementation of fuse writeback
476 * file_write_and_wait_range() does not catch errors.
477 * We have to do this directly after fuse_sync_writes()
479 err = file_check_and_advance_wb_err(file);
483 err = sync_inode_metadata(inode, 1);
487 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
490 memset(&inarg, 0, sizeof(inarg));
492 inarg.fsync_flags = datasync ? 1 : 0;
493 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
494 args.in.h.nodeid = get_node_id(inode);
496 args.in.args[0].size = sizeof(inarg);
497 args.in.args[0].value = &inarg;
498 err = fuse_simple_request(fc, &args);
499 if (err == -ENOSYS) {
511 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
514 return fuse_fsync_common(file, start, end, datasync, 0);
517 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
518 size_t count, int opcode)
520 struct fuse_read_in *inarg = &req->misc.read.in;
521 struct fuse_file *ff = file->private_data;
526 inarg->flags = file->f_flags;
527 req->in.h.opcode = opcode;
528 req->in.h.nodeid = ff->nodeid;
530 req->in.args[0].size = sizeof(struct fuse_read_in);
531 req->in.args[0].value = inarg;
533 req->out.numargs = 1;
534 req->out.args[0].size = count;
537 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
541 for (i = 0; i < req->num_pages; i++) {
542 struct page *page = req->pages[i];
544 set_page_dirty_lock(page);
549 static void fuse_io_release(struct kref *kref)
551 kfree(container_of(kref, struct fuse_io_priv, refcnt));
554 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
559 if (io->bytes >= 0 && io->write)
562 return io->bytes < 0 ? io->size : io->bytes;
566 * In case of short read, the caller sets 'pos' to the position of
567 * actual end of fuse request in IO request. Otherwise, if bytes_requested
568 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
571 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
572 * both submitted asynchronously. The first of them was ACKed by userspace as
573 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
574 * second request was ACKed as short, e.g. only 1K was read, resulting in
577 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
578 * will be equal to the length of the longest contiguous fragment of
579 * transferred data starting from the beginning of IO request.
581 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
585 spin_lock(&io->lock);
587 io->err = io->err ? : err;
588 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
592 if (!left && io->blocking)
594 spin_unlock(&io->lock);
596 if (!left && !io->blocking) {
597 ssize_t res = fuse_get_res_by_io(io);
600 struct inode *inode = file_inode(io->iocb->ki_filp);
601 struct fuse_conn *fc = get_fuse_conn(inode);
602 struct fuse_inode *fi = get_fuse_inode(inode);
604 spin_lock(&fc->lock);
605 fi->attr_version = ++fc->attr_version;
606 spin_unlock(&fc->lock);
609 io->iocb->ki_complete(io->iocb, res, 0);
612 kref_put(&io->refcnt, fuse_io_release);
615 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
617 struct fuse_io_priv *io = req->io;
620 fuse_release_user_pages(req, io->should_dirty);
623 if (req->misc.write.in.size != req->misc.write.out.size)
624 pos = req->misc.write.in.offset - io->offset +
625 req->misc.write.out.size;
627 if (req->misc.read.in.size != req->out.args[0].size)
628 pos = req->misc.read.in.offset - io->offset +
629 req->out.args[0].size;
632 fuse_aio_complete(io, req->out.h.error, pos);
635 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
636 size_t num_bytes, struct fuse_io_priv *io)
638 spin_lock(&io->lock);
639 kref_get(&io->refcnt);
640 io->size += num_bytes;
642 spin_unlock(&io->lock);
645 req->end = fuse_aio_complete_req;
647 __fuse_get_request(req);
648 fuse_request_send_background(fc, req);
653 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
654 loff_t pos, size_t count, fl_owner_t owner)
656 struct file *file = io->iocb->ki_filp;
657 struct fuse_file *ff = file->private_data;
658 struct fuse_conn *fc = ff->fc;
660 fuse_read_fill(req, file, pos, count, FUSE_READ);
662 struct fuse_read_in *inarg = &req->misc.read.in;
664 inarg->read_flags |= FUSE_READ_LOCKOWNER;
665 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
669 return fuse_async_req_send(fc, req, count, io);
671 fuse_request_send(fc, req);
672 return req->out.args[0].size;
675 static void fuse_read_update_size(struct inode *inode, loff_t size,
678 struct fuse_conn *fc = get_fuse_conn(inode);
679 struct fuse_inode *fi = get_fuse_inode(inode);
681 spin_lock(&fc->lock);
682 if (attr_ver == fi->attr_version && size < inode->i_size &&
683 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
684 fi->attr_version = ++fc->attr_version;
685 i_size_write(inode, size);
687 spin_unlock(&fc->lock);
690 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
693 size_t num_read = req->out.args[0].size;
694 struct fuse_conn *fc = get_fuse_conn(inode);
696 if (fc->writeback_cache) {
698 * A hole in a file. Some data after the hole are in page cache,
699 * but have not reached the client fs yet. So, the hole is not
703 int start_idx = num_read >> PAGE_SHIFT;
704 size_t off = num_read & (PAGE_SIZE - 1);
706 for (i = start_idx; i < req->num_pages; i++) {
707 zero_user_segment(req->pages[i], off, PAGE_SIZE);
711 loff_t pos = page_offset(req->pages[0]) + num_read;
712 fuse_read_update_size(inode, pos, attr_ver);
716 static int fuse_do_readpage(struct file *file, struct page *page)
719 struct fuse_io_priv io;
720 struct inode *inode = page->mapping->host;
721 struct fuse_conn *fc = get_fuse_conn(inode);
722 struct fuse_req *req;
724 loff_t pos = page_offset(page);
725 size_t count = PAGE_SIZE;
730 * Page writeback can extend beyond the lifetime of the
731 * page-cache page, so make sure we read a properly synced
734 fuse_wait_on_page_writeback(inode, page->index);
736 req = fuse_get_req(fc, 1);
740 attr_ver = fuse_get_attr_version(fc);
742 req->out.page_zeroing = 1;
743 req->out.argpages = 1;
745 req->pages[0] = page;
746 req->page_descs[0].length = count;
747 init_sync_kiocb(&iocb, file);
748 io = (struct fuse_io_priv) FUSE_IO_PRIV_SYNC(&iocb);
749 num_read = fuse_send_read(req, &io, pos, count, NULL);
750 err = req->out.h.error;
754 * Short read means EOF. If file size is larger, truncate it
756 if (num_read < count)
757 fuse_short_read(req, inode, attr_ver);
759 SetPageUptodate(page);
762 fuse_put_request(fc, req);
767 static int fuse_readpage(struct file *file, struct page *page)
769 struct inode *inode = page->mapping->host;
773 if (is_bad_inode(inode))
776 err = fuse_do_readpage(file, page);
777 fuse_invalidate_atime(inode);
783 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
786 size_t count = req->misc.read.in.size;
787 size_t num_read = req->out.args[0].size;
788 struct address_space *mapping = NULL;
790 for (i = 0; mapping == NULL && i < req->num_pages; i++)
791 mapping = req->pages[i]->mapping;
794 struct inode *inode = mapping->host;
797 * Short read means EOF. If file size is larger, truncate it
799 if (!req->out.h.error && num_read < count)
800 fuse_short_read(req, inode, req->misc.read.attr_ver);
802 fuse_invalidate_atime(inode);
805 for (i = 0; i < req->num_pages; i++) {
806 struct page *page = req->pages[i];
807 if (!req->out.h.error)
808 SetPageUptodate(page);
815 fuse_file_put(req->ff, false, false);
818 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
820 struct fuse_file *ff = file->private_data;
821 struct fuse_conn *fc = ff->fc;
822 loff_t pos = page_offset(req->pages[0]);
823 size_t count = req->num_pages << PAGE_SHIFT;
825 req->out.argpages = 1;
826 req->out.page_zeroing = 1;
827 req->out.page_replace = 1;
828 fuse_read_fill(req, file, pos, count, FUSE_READ);
829 req->misc.read.attr_ver = fuse_get_attr_version(fc);
830 if (fc->async_read) {
831 req->ff = fuse_file_get(ff);
832 req->end = fuse_readpages_end;
833 fuse_request_send_background(fc, req);
835 fuse_request_send(fc, req);
836 fuse_readpages_end(fc, req);
837 fuse_put_request(fc, req);
841 struct fuse_fill_data {
842 struct fuse_req *req;
848 static int fuse_readpages_fill(void *_data, struct page *page)
850 struct fuse_fill_data *data = _data;
851 struct fuse_req *req = data->req;
852 struct inode *inode = data->inode;
853 struct fuse_conn *fc = get_fuse_conn(inode);
855 fuse_wait_on_page_writeback(inode, page->index);
857 if (req->num_pages &&
858 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
859 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
860 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
861 int nr_alloc = min_t(unsigned, data->nr_pages,
862 FUSE_MAX_PAGES_PER_REQ);
863 fuse_send_readpages(req, data->file);
865 req = fuse_get_req_for_background(fc, nr_alloc);
867 req = fuse_get_req(fc, nr_alloc);
876 if (WARN_ON(req->num_pages >= req->max_pages)) {
878 fuse_put_request(fc, req);
883 req->pages[req->num_pages] = page;
884 req->page_descs[req->num_pages].length = PAGE_SIZE;
890 static int fuse_readpages(struct file *file, struct address_space *mapping,
891 struct list_head *pages, unsigned nr_pages)
893 struct inode *inode = mapping->host;
894 struct fuse_conn *fc = get_fuse_conn(inode);
895 struct fuse_fill_data data;
897 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
900 if (is_bad_inode(inode))
906 data.req = fuse_get_req_for_background(fc, nr_alloc);
908 data.req = fuse_get_req(fc, nr_alloc);
909 data.nr_pages = nr_pages;
910 err = PTR_ERR(data.req);
911 if (IS_ERR(data.req))
914 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
916 if (data.req->num_pages)
917 fuse_send_readpages(data.req, file);
919 fuse_put_request(fc, data.req);
925 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
927 struct inode *inode = iocb->ki_filp->f_mapping->host;
928 struct fuse_conn *fc = get_fuse_conn(inode);
931 * In auto invalidate mode, always update attributes on read.
932 * Otherwise, only update if we attempt to read past EOF (to ensure
933 * i_size is up to date).
935 if (fc->auto_inval_data ||
936 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
938 err = fuse_update_attributes(inode, iocb->ki_filp);
943 return generic_file_read_iter(iocb, to);
946 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
947 loff_t pos, size_t count)
949 struct fuse_write_in *inarg = &req->misc.write.in;
950 struct fuse_write_out *outarg = &req->misc.write.out;
955 req->in.h.opcode = FUSE_WRITE;
956 req->in.h.nodeid = ff->nodeid;
958 if (ff->fc->minor < 9)
959 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
961 req->in.args[0].size = sizeof(struct fuse_write_in);
962 req->in.args[0].value = inarg;
963 req->in.args[1].size = count;
964 req->out.numargs = 1;
965 req->out.args[0].size = sizeof(struct fuse_write_out);
966 req->out.args[0].value = outarg;
969 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
970 loff_t pos, size_t count, fl_owner_t owner)
972 struct kiocb *iocb = io->iocb;
973 struct file *file = iocb->ki_filp;
974 struct fuse_file *ff = file->private_data;
975 struct fuse_conn *fc = ff->fc;
976 struct fuse_write_in *inarg = &req->misc.write.in;
978 fuse_write_fill(req, ff, pos, count);
979 inarg->flags = file->f_flags;
980 if (iocb->ki_flags & IOCB_DSYNC)
981 inarg->flags |= O_DSYNC;
982 if (iocb->ki_flags & IOCB_SYNC)
983 inarg->flags |= O_SYNC;
985 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
986 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
990 return fuse_async_req_send(fc, req, count, io);
992 fuse_request_send(fc, req);
993 return req->misc.write.out.size;
996 bool fuse_write_update_size(struct inode *inode, loff_t pos)
998 struct fuse_conn *fc = get_fuse_conn(inode);
999 struct fuse_inode *fi = get_fuse_inode(inode);
1002 spin_lock(&fc->lock);
1003 fi->attr_version = ++fc->attr_version;
1004 if (pos > inode->i_size) {
1005 i_size_write(inode, pos);
1008 spin_unlock(&fc->lock);
1013 static size_t fuse_send_write_pages(struct fuse_req *req, struct kiocb *iocb,
1014 struct inode *inode, loff_t pos,
1020 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1022 for (i = 0; i < req->num_pages; i++)
1023 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1025 res = fuse_send_write(req, &io, pos, count, NULL);
1027 offset = req->page_descs[0].offset;
1029 for (i = 0; i < req->num_pages; i++) {
1030 struct page *page = req->pages[i];
1032 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1033 SetPageUptodate(page);
1035 if (count > PAGE_SIZE - offset)
1036 count -= PAGE_SIZE - offset;
1048 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1049 struct address_space *mapping,
1050 struct iov_iter *ii, loff_t pos)
1052 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1053 unsigned offset = pos & (PAGE_SIZE - 1);
1057 req->in.argpages = 1;
1058 req->page_descs[0].offset = offset;
1063 pgoff_t index = pos >> PAGE_SHIFT;
1064 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1065 iov_iter_count(ii));
1067 bytes = min_t(size_t, bytes, fc->max_write - count);
1071 if (iov_iter_fault_in_readable(ii, bytes))
1075 page = grab_cache_page_write_begin(mapping, index, 0);
1079 if (mapping_writably_mapped(mapping))
1080 flush_dcache_page(page);
1082 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1083 flush_dcache_page(page);
1085 iov_iter_advance(ii, tmp);
1089 bytes = min(bytes, iov_iter_single_seg_count(ii));
1094 req->pages[req->num_pages] = page;
1095 req->page_descs[req->num_pages].length = tmp;
1101 if (offset == PAGE_SIZE)
1104 if (!fc->big_writes)
1106 } while (iov_iter_count(ii) && count < fc->max_write &&
1107 req->num_pages < req->max_pages && offset == 0);
1109 return count > 0 ? count : err;
1112 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1114 return min_t(unsigned,
1115 ((pos + len - 1) >> PAGE_SHIFT) -
1116 (pos >> PAGE_SHIFT) + 1,
1117 FUSE_MAX_PAGES_PER_REQ);
1120 static ssize_t fuse_perform_write(struct kiocb *iocb,
1121 struct address_space *mapping,
1122 struct iov_iter *ii, loff_t pos)
1124 struct inode *inode = mapping->host;
1125 struct fuse_conn *fc = get_fuse_conn(inode);
1126 struct fuse_inode *fi = get_fuse_inode(inode);
1130 if (is_bad_inode(inode))
1133 if (inode->i_size < pos + iov_iter_count(ii))
1134 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1137 struct fuse_req *req;
1139 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1141 req = fuse_get_req(fc, nr_pages);
1147 count = fuse_fill_write_pages(req, mapping, ii, pos);
1153 num_written = fuse_send_write_pages(req, iocb, inode,
1155 err = req->out.h.error;
1160 /* break out of the loop on short write */
1161 if (num_written != count)
1165 fuse_put_request(fc, req);
1166 } while (!err && iov_iter_count(ii));
1169 fuse_write_update_size(inode, pos);
1171 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1172 fuse_invalidate_attr(inode);
1174 return res > 0 ? res : err;
1177 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1179 struct file *file = iocb->ki_filp;
1180 struct address_space *mapping = file->f_mapping;
1181 ssize_t written = 0;
1182 ssize_t written_buffered = 0;
1183 struct inode *inode = mapping->host;
1187 if (get_fuse_conn(inode)->writeback_cache) {
1188 /* Update size (EOF optimization) and mode (SUID clearing) */
1189 err = fuse_update_attributes(mapping->host, file);
1193 return generic_file_write_iter(iocb, from);
1198 /* We can write back this queue in page reclaim */
1199 current->backing_dev_info = inode_to_bdi(inode);
1201 err = generic_write_checks(iocb, from);
1205 err = file_remove_privs(file);
1209 err = file_update_time(file);
1213 if (iocb->ki_flags & IOCB_DIRECT) {
1214 loff_t pos = iocb->ki_pos;
1215 written = generic_file_direct_write(iocb, from);
1216 if (written < 0 || !iov_iter_count(from))
1221 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1222 if (written_buffered < 0) {
1223 err = written_buffered;
1226 endbyte = pos + written_buffered - 1;
1228 err = filemap_write_and_wait_range(file->f_mapping, pos,
1233 invalidate_mapping_pages(file->f_mapping,
1235 endbyte >> PAGE_SHIFT);
1237 written += written_buffered;
1238 iocb->ki_pos = pos + written_buffered;
1240 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1242 iocb->ki_pos += written;
1245 current->backing_dev_info = NULL;
1246 inode_unlock(inode);
1248 written = generic_write_sync(iocb, written);
1250 return written ? written : err;
1253 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1254 unsigned index, unsigned nr_pages)
1258 for (i = index; i < index + nr_pages; i++)
1259 req->page_descs[i].length = PAGE_SIZE -
1260 req->page_descs[i].offset;
1263 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1265 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1268 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1271 return min(iov_iter_single_seg_count(ii), max_size);
1274 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1275 size_t *nbytesp, int write)
1277 size_t nbytes = 0; /* # bytes already packed in req */
1280 /* Special case for kernel I/O: can copy directly into the buffer */
1281 if (ii->type & ITER_KVEC) {
1282 unsigned long user_addr = fuse_get_user_addr(ii);
1283 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1286 req->in.args[1].value = (void *) user_addr;
1288 req->out.args[0].value = (void *) user_addr;
1290 iov_iter_advance(ii, frag_size);
1291 *nbytesp = frag_size;
1295 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1298 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1300 req->max_pages - req->num_pages,
1305 iov_iter_advance(ii, ret);
1309 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1311 req->page_descs[req->num_pages].offset = start;
1312 fuse_page_descs_length_init(req, req->num_pages, npages);
1314 req->num_pages += npages;
1315 req->page_descs[req->num_pages - 1].length -=
1316 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1320 req->in.argpages = 1;
1322 req->out.argpages = 1;
1326 return ret < 0 ? ret : 0;
1329 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1331 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1334 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1335 loff_t *ppos, int flags)
1337 int write = flags & FUSE_DIO_WRITE;
1338 int cuse = flags & FUSE_DIO_CUSE;
1339 struct file *file = io->iocb->ki_filp;
1340 struct inode *inode = file->f_mapping->host;
1341 struct fuse_file *ff = file->private_data;
1342 struct fuse_conn *fc = ff->fc;
1343 size_t nmax = write ? fc->max_write : fc->max_read;
1345 size_t count = iov_iter_count(iter);
1346 pgoff_t idx_from = pos >> PAGE_SHIFT;
1347 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1349 struct fuse_req *req;
1353 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1355 req = fuse_get_req(fc, fuse_iter_npages(iter));
1357 return PTR_ERR(req);
1359 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1362 fuse_sync_writes(inode);
1364 inode_unlock(inode);
1367 io->should_dirty = !write && iter_is_iovec(iter);
1370 fl_owner_t owner = current->files;
1371 size_t nbytes = min(count, nmax);
1372 err = fuse_get_user_pages(req, iter, &nbytes, write);
1377 nres = fuse_send_write(req, io, pos, nbytes, owner);
1379 nres = fuse_send_read(req, io, pos, nbytes, owner);
1382 fuse_release_user_pages(req, io->should_dirty);
1383 if (req->out.h.error) {
1384 err = req->out.h.error;
1386 } else if (nres > nbytes) {
1397 fuse_put_request(fc, req);
1399 req = fuse_get_req_for_background(fc,
1400 fuse_iter_npages(iter));
1402 req = fuse_get_req(fc, fuse_iter_npages(iter));
1408 fuse_put_request(fc, req);
1412 return res > 0 ? res : err;
1414 EXPORT_SYMBOL_GPL(fuse_direct_io);
1416 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1417 struct iov_iter *iter,
1421 struct inode *inode = file_inode(io->iocb->ki_filp);
1423 if (is_bad_inode(inode))
1426 res = fuse_direct_io(io, iter, ppos, 0);
1428 fuse_invalidate_attr(inode);
1433 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1435 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1436 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1439 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1441 struct inode *inode = file_inode(iocb->ki_filp);
1442 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1445 if (is_bad_inode(inode))
1448 /* Don't allow parallel writes to the same file */
1450 res = generic_write_checks(iocb, from);
1452 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1453 fuse_invalidate_attr(inode);
1455 fuse_write_update_size(inode, iocb->ki_pos);
1456 inode_unlock(inode);
1461 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1465 for (i = 0; i < req->num_pages; i++)
1466 __free_page(req->pages[i]);
1469 fuse_file_put(req->ff, false, false);
1472 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1474 struct inode *inode = req->inode;
1475 struct fuse_inode *fi = get_fuse_inode(inode);
1476 struct backing_dev_info *bdi = inode_to_bdi(inode);
1479 list_del(&req->writepages_entry);
1480 for (i = 0; i < req->num_pages; i++) {
1481 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1482 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1483 wb_writeout_inc(&bdi->wb);
1485 wake_up(&fi->page_waitq);
1488 /* Called under fc->lock, may release and reacquire it */
1489 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1491 __releases(fc->lock)
1492 __acquires(fc->lock)
1494 struct fuse_inode *fi = get_fuse_inode(req->inode);
1495 struct fuse_write_in *inarg = &req->misc.write.in;
1496 __u64 data_size = req->num_pages * PAGE_SIZE;
1501 if (inarg->offset + data_size <= size) {
1502 inarg->size = data_size;
1503 } else if (inarg->offset < size) {
1504 inarg->size = size - inarg->offset;
1506 /* Got truncated off completely */
1510 req->in.args[1].size = inarg->size;
1512 fuse_request_send_background_locked(fc, req);
1516 fuse_writepage_finish(fc, req);
1517 spin_unlock(&fc->lock);
1518 fuse_writepage_free(fc, req);
1519 fuse_put_request(fc, req);
1520 spin_lock(&fc->lock);
1524 * If fi->writectr is positive (no truncate or fsync going on) send
1525 * all queued writepage requests.
1527 * Called with fc->lock
1529 void fuse_flush_writepages(struct inode *inode)
1530 __releases(fc->lock)
1531 __acquires(fc->lock)
1533 struct fuse_conn *fc = get_fuse_conn(inode);
1534 struct fuse_inode *fi = get_fuse_inode(inode);
1535 loff_t crop = i_size_read(inode);
1536 struct fuse_req *req;
1538 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1539 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1540 list_del_init(&req->list);
1541 fuse_send_writepage(fc, req, crop);
1545 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1547 struct inode *inode = req->inode;
1548 struct fuse_inode *fi = get_fuse_inode(inode);
1550 mapping_set_error(inode->i_mapping, req->out.h.error);
1551 spin_lock(&fc->lock);
1552 while (req->misc.write.next) {
1553 struct fuse_conn *fc = get_fuse_conn(inode);
1554 struct fuse_write_in *inarg = &req->misc.write.in;
1555 struct fuse_req *next = req->misc.write.next;
1556 req->misc.write.next = next->misc.write.next;
1557 next->misc.write.next = NULL;
1558 next->ff = fuse_file_get(req->ff);
1559 list_add(&next->writepages_entry, &fi->writepages);
1562 * Skip fuse_flush_writepages() to make it easy to crop requests
1563 * based on primary request size.
1565 * 1st case (trivial): there are no concurrent activities using
1566 * fuse_set/release_nowrite. Then we're on safe side because
1567 * fuse_flush_writepages() would call fuse_send_writepage()
1570 * 2nd case: someone called fuse_set_nowrite and it is waiting
1571 * now for completion of all in-flight requests. This happens
1572 * rarely and no more than once per page, so this should be
1575 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1576 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1577 * that fuse_set_nowrite returned implies that all in-flight
1578 * requests were completed along with all of their secondary
1579 * requests. Further primary requests are blocked by negative
1580 * writectr. Hence there cannot be any in-flight requests and
1581 * no invocations of fuse_writepage_end() while we're in
1582 * fuse_set_nowrite..fuse_release_nowrite section.
1584 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1587 fuse_writepage_finish(fc, req);
1588 spin_unlock(&fc->lock);
1589 fuse_writepage_free(fc, req);
1592 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1593 struct fuse_inode *fi)
1595 struct fuse_file *ff = NULL;
1597 spin_lock(&fc->lock);
1598 if (!list_empty(&fi->write_files)) {
1599 ff = list_entry(fi->write_files.next, struct fuse_file,
1603 spin_unlock(&fc->lock);
1608 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1609 struct fuse_inode *fi)
1611 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1616 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1618 struct fuse_conn *fc = get_fuse_conn(inode);
1619 struct fuse_inode *fi = get_fuse_inode(inode);
1620 struct fuse_file *ff;
1623 ff = __fuse_write_file_get(fc, fi);
1624 err = fuse_flush_times(inode, ff);
1626 fuse_file_put(ff, false, false);
1631 static int fuse_writepage_locked(struct page *page)
1633 struct address_space *mapping = page->mapping;
1634 struct inode *inode = mapping->host;
1635 struct fuse_conn *fc = get_fuse_conn(inode);
1636 struct fuse_inode *fi = get_fuse_inode(inode);
1637 struct fuse_req *req;
1638 struct page *tmp_page;
1639 int error = -ENOMEM;
1641 set_page_writeback(page);
1643 req = fuse_request_alloc_nofs(1);
1647 /* writeback always goes to bg_queue */
1648 __set_bit(FR_BACKGROUND, &req->flags);
1649 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1654 req->ff = fuse_write_file_get(fc, fi);
1658 fuse_write_fill(req, req->ff, page_offset(page), 0);
1660 copy_highpage(tmp_page, page);
1661 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1662 req->misc.write.next = NULL;
1663 req->in.argpages = 1;
1665 req->pages[0] = tmp_page;
1666 req->page_descs[0].offset = 0;
1667 req->page_descs[0].length = PAGE_SIZE;
1668 req->end = fuse_writepage_end;
1671 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1672 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1674 spin_lock(&fc->lock);
1675 list_add(&req->writepages_entry, &fi->writepages);
1676 list_add_tail(&req->list, &fi->queued_writes);
1677 fuse_flush_writepages(inode);
1678 spin_unlock(&fc->lock);
1680 end_page_writeback(page);
1685 __free_page(tmp_page);
1687 fuse_request_free(req);
1689 mapping_set_error(page->mapping, error);
1690 end_page_writeback(page);
1694 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1698 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1700 * ->writepages() should be called for sync() and friends. We
1701 * should only get here on direct reclaim and then we are
1702 * allowed to skip a page which is already in flight
1704 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1706 redirty_page_for_writepage(wbc, page);
1711 err = fuse_writepage_locked(page);
1717 struct fuse_fill_wb_data {
1718 struct fuse_req *req;
1719 struct fuse_file *ff;
1720 struct inode *inode;
1721 struct page **orig_pages;
1724 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1726 struct fuse_req *req = data->req;
1727 struct inode *inode = data->inode;
1728 struct fuse_conn *fc = get_fuse_conn(inode);
1729 struct fuse_inode *fi = get_fuse_inode(inode);
1730 int num_pages = req->num_pages;
1733 req->ff = fuse_file_get(data->ff);
1734 spin_lock(&fc->lock);
1735 list_add_tail(&req->list, &fi->queued_writes);
1736 fuse_flush_writepages(inode);
1737 spin_unlock(&fc->lock);
1739 for (i = 0; i < num_pages; i++)
1740 end_page_writeback(data->orig_pages[i]);
1743 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1746 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1747 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1748 struct fuse_req *tmp;
1749 struct fuse_req *old_req;
1753 BUG_ON(new_req->num_pages != 0);
1755 spin_lock(&fc->lock);
1756 list_del(&new_req->writepages_entry);
1757 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1758 BUG_ON(old_req->inode != new_req->inode);
1759 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1760 if (curr_index <= page->index &&
1761 page->index < curr_index + old_req->num_pages) {
1767 list_add(&new_req->writepages_entry, &fi->writepages);
1771 new_req->num_pages = 1;
1772 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1773 BUG_ON(tmp->inode != new_req->inode);
1774 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1775 if (tmp->num_pages == 1 &&
1776 curr_index == page->index) {
1781 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1782 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1784 copy_highpage(old_req->pages[0], page);
1785 spin_unlock(&fc->lock);
1787 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1788 dec_node_page_state(new_req->pages[0], NR_WRITEBACK_TEMP);
1789 wb_writeout_inc(&bdi->wb);
1790 fuse_writepage_free(fc, new_req);
1791 fuse_request_free(new_req);
1794 new_req->misc.write.next = old_req->misc.write.next;
1795 old_req->misc.write.next = new_req;
1798 spin_unlock(&fc->lock);
1803 static int fuse_writepages_fill(struct page *page,
1804 struct writeback_control *wbc, void *_data)
1806 struct fuse_fill_wb_data *data = _data;
1807 struct fuse_req *req = data->req;
1808 struct inode *inode = data->inode;
1809 struct fuse_conn *fc = get_fuse_conn(inode);
1810 struct page *tmp_page;
1816 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1822 * Being under writeback is unlikely but possible. For example direct
1823 * read to an mmaped fuse file will set the page dirty twice; once when
1824 * the pages are faulted with get_user_pages(), and then after the read
1827 is_writeback = fuse_page_is_writeback(inode, page->index);
1829 if (req && req->num_pages &&
1830 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1831 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1832 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1833 fuse_writepages_send(data);
1837 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1842 * The page must not be redirtied until the writeout is completed
1843 * (i.e. userspace has sent a reply to the write request). Otherwise
1844 * there could be more than one temporary page instance for each real
1847 * This is ensured by holding the page lock in page_mkwrite() while
1848 * checking fuse_page_is_writeback(). We already hold the page lock
1849 * since clear_page_dirty_for_io() and keep it held until we add the
1850 * request to the fi->writepages list and increment req->num_pages.
1851 * After this fuse_page_is_writeback() will indicate that the page is
1852 * under writeback, so we can release the page lock.
1854 if (data->req == NULL) {
1855 struct fuse_inode *fi = get_fuse_inode(inode);
1858 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1860 __free_page(tmp_page);
1864 fuse_write_fill(req, data->ff, page_offset(page), 0);
1865 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1866 req->misc.write.next = NULL;
1867 req->in.argpages = 1;
1868 __set_bit(FR_BACKGROUND, &req->flags);
1870 req->end = fuse_writepage_end;
1873 spin_lock(&fc->lock);
1874 list_add(&req->writepages_entry, &fi->writepages);
1875 spin_unlock(&fc->lock);
1879 set_page_writeback(page);
1881 copy_highpage(tmp_page, page);
1882 req->pages[req->num_pages] = tmp_page;
1883 req->page_descs[req->num_pages].offset = 0;
1884 req->page_descs[req->num_pages].length = PAGE_SIZE;
1886 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1887 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1890 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1891 end_page_writeback(page);
1895 data->orig_pages[req->num_pages] = page;
1898 * Protected by fc->lock against concurrent access by
1899 * fuse_page_is_writeback().
1901 spin_lock(&fc->lock);
1903 spin_unlock(&fc->lock);
1911 static int fuse_writepages(struct address_space *mapping,
1912 struct writeback_control *wbc)
1914 struct inode *inode = mapping->host;
1915 struct fuse_fill_wb_data data;
1919 if (is_bad_inode(inode))
1927 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1928 sizeof(struct page *),
1930 if (!data.orig_pages)
1933 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1935 /* Ignore errors if we can write at least one page */
1936 BUG_ON(!data.req->num_pages);
1937 fuse_writepages_send(&data);
1941 fuse_file_put(data.ff, false, false);
1943 kfree(data.orig_pages);
1949 * It's worthy to make sure that space is reserved on disk for the write,
1950 * but how to implement it without killing performance need more thinking.
1952 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1953 loff_t pos, unsigned len, unsigned flags,
1954 struct page **pagep, void **fsdata)
1956 pgoff_t index = pos >> PAGE_SHIFT;
1957 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1962 WARN_ON(!fc->writeback_cache);
1964 page = grab_cache_page_write_begin(mapping, index, flags);
1968 fuse_wait_on_page_writeback(mapping->host, page->index);
1970 if (PageUptodate(page) || len == PAGE_SIZE)
1973 * Check if the start this page comes after the end of file, in which
1974 * case the readpage can be optimized away.
1976 fsize = i_size_read(mapping->host);
1977 if (fsize <= (pos & PAGE_MASK)) {
1978 size_t off = pos & ~PAGE_MASK;
1980 zero_user_segment(page, 0, off);
1983 err = fuse_do_readpage(file, page);
1997 static int fuse_write_end(struct file *file, struct address_space *mapping,
1998 loff_t pos, unsigned len, unsigned copied,
1999 struct page *page, void *fsdata)
2001 struct inode *inode = page->mapping->host;
2003 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2007 if (!PageUptodate(page)) {
2008 /* Zero any unwritten bytes at the end of the page */
2009 size_t endoff = (pos + copied) & ~PAGE_MASK;
2011 zero_user_segment(page, endoff, PAGE_SIZE);
2012 SetPageUptodate(page);
2015 fuse_write_update_size(inode, pos + copied);
2016 set_page_dirty(page);
2025 static int fuse_launder_page(struct page *page)
2028 if (clear_page_dirty_for_io(page)) {
2029 struct inode *inode = page->mapping->host;
2030 err = fuse_writepage_locked(page);
2032 fuse_wait_on_page_writeback(inode, page->index);
2038 * Write back dirty pages now, because there may not be any suitable
2041 static void fuse_vma_close(struct vm_area_struct *vma)
2043 filemap_write_and_wait(vma->vm_file->f_mapping);
2047 * Wait for writeback against this page to complete before allowing it
2048 * to be marked dirty again, and hence written back again, possibly
2049 * before the previous writepage completed.
2051 * Block here, instead of in ->writepage(), so that the userspace fs
2052 * can only block processes actually operating on the filesystem.
2054 * Otherwise unprivileged userspace fs would be able to block
2059 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2061 static int fuse_page_mkwrite(struct vm_fault *vmf)
2063 struct page *page = vmf->page;
2064 struct inode *inode = file_inode(vmf->vma->vm_file);
2066 file_update_time(vmf->vma->vm_file);
2068 if (page->mapping != inode->i_mapping) {
2070 return VM_FAULT_NOPAGE;
2073 fuse_wait_on_page_writeback(inode, page->index);
2074 return VM_FAULT_LOCKED;
2077 static const struct vm_operations_struct fuse_file_vm_ops = {
2078 .close = fuse_vma_close,
2079 .fault = filemap_fault,
2080 .map_pages = filemap_map_pages,
2081 .page_mkwrite = fuse_page_mkwrite,
2084 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2086 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2087 fuse_link_write_file(file);
2089 file_accessed(file);
2090 vma->vm_ops = &fuse_file_vm_ops;
2094 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2096 /* Can't provide the coherency needed for MAP_SHARED */
2097 if (vma->vm_flags & VM_MAYSHARE)
2100 invalidate_inode_pages2(file->f_mapping);
2102 return generic_file_mmap(file, vma);
2105 static int convert_fuse_file_lock(struct fuse_conn *fc,
2106 const struct fuse_file_lock *ffl,
2107 struct file_lock *fl)
2109 switch (ffl->type) {
2115 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2116 ffl->end < ffl->start)
2119 fl->fl_start = ffl->start;
2120 fl->fl_end = ffl->end;
2123 * Convert pid into init's pid namespace. The locks API will
2124 * translate it into the caller's pid namespace.
2127 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2134 fl->fl_type = ffl->type;
2138 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2139 const struct file_lock *fl, int opcode, pid_t pid,
2140 int flock, struct fuse_lk_in *inarg)
2142 struct inode *inode = file_inode(file);
2143 struct fuse_conn *fc = get_fuse_conn(inode);
2144 struct fuse_file *ff = file->private_data;
2146 memset(inarg, 0, sizeof(*inarg));
2148 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2149 inarg->lk.start = fl->fl_start;
2150 inarg->lk.end = fl->fl_end;
2151 inarg->lk.type = fl->fl_type;
2152 inarg->lk.pid = pid;
2154 inarg->lk_flags |= FUSE_LK_FLOCK;
2155 args->in.h.opcode = opcode;
2156 args->in.h.nodeid = get_node_id(inode);
2157 args->in.numargs = 1;
2158 args->in.args[0].size = sizeof(*inarg);
2159 args->in.args[0].value = inarg;
2162 static int fuse_getlk(struct file *file, struct file_lock *fl)
2164 struct inode *inode = file_inode(file);
2165 struct fuse_conn *fc = get_fuse_conn(inode);
2167 struct fuse_lk_in inarg;
2168 struct fuse_lk_out outarg;
2171 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2172 args.out.numargs = 1;
2173 args.out.args[0].size = sizeof(outarg);
2174 args.out.args[0].value = &outarg;
2175 err = fuse_simple_request(fc, &args);
2177 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2182 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2184 struct inode *inode = file_inode(file);
2185 struct fuse_conn *fc = get_fuse_conn(inode);
2187 struct fuse_lk_in inarg;
2188 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2189 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2190 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2193 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2194 /* NLM needs asynchronous locks, which we don't support yet */
2198 /* Unlock on close is handled by the flush method */
2199 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2202 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2203 err = fuse_simple_request(fc, &args);
2205 /* locking is restartable */
2212 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2214 struct inode *inode = file_inode(file);
2215 struct fuse_conn *fc = get_fuse_conn(inode);
2218 if (cmd == F_CANCELLK) {
2220 } else if (cmd == F_GETLK) {
2222 posix_test_lock(file, fl);
2225 err = fuse_getlk(file, fl);
2228 err = posix_lock_file(file, fl, NULL);
2230 err = fuse_setlk(file, fl, 0);
2235 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2237 struct inode *inode = file_inode(file);
2238 struct fuse_conn *fc = get_fuse_conn(inode);
2242 err = locks_lock_file_wait(file, fl);
2244 struct fuse_file *ff = file->private_data;
2246 /* emulate flock with POSIX locks */
2248 err = fuse_setlk(file, fl, 1);
2254 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2256 struct inode *inode = mapping->host;
2257 struct fuse_conn *fc = get_fuse_conn(inode);
2259 struct fuse_bmap_in inarg;
2260 struct fuse_bmap_out outarg;
2263 if (!inode->i_sb->s_bdev || fc->no_bmap)
2266 memset(&inarg, 0, sizeof(inarg));
2267 inarg.block = block;
2268 inarg.blocksize = inode->i_sb->s_blocksize;
2269 args.in.h.opcode = FUSE_BMAP;
2270 args.in.h.nodeid = get_node_id(inode);
2271 args.in.numargs = 1;
2272 args.in.args[0].size = sizeof(inarg);
2273 args.in.args[0].value = &inarg;
2274 args.out.numargs = 1;
2275 args.out.args[0].size = sizeof(outarg);
2276 args.out.args[0].value = &outarg;
2277 err = fuse_simple_request(fc, &args);
2281 return err ? 0 : outarg.block;
2284 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2286 struct inode *inode = file->f_mapping->host;
2287 struct fuse_conn *fc = get_fuse_conn(inode);
2288 struct fuse_file *ff = file->private_data;
2290 struct fuse_lseek_in inarg = {
2295 struct fuse_lseek_out outarg;
2301 args.in.h.opcode = FUSE_LSEEK;
2302 args.in.h.nodeid = ff->nodeid;
2303 args.in.numargs = 1;
2304 args.in.args[0].size = sizeof(inarg);
2305 args.in.args[0].value = &inarg;
2306 args.out.numargs = 1;
2307 args.out.args[0].size = sizeof(outarg);
2308 args.out.args[0].value = &outarg;
2309 err = fuse_simple_request(fc, &args);
2311 if (err == -ENOSYS) {
2318 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2321 err = fuse_update_attributes(inode, file);
2323 return generic_file_llseek(file, offset, whence);
2328 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2331 struct inode *inode = file_inode(file);
2336 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2337 retval = generic_file_llseek(file, offset, whence);
2341 retval = fuse_update_attributes(inode, file);
2343 retval = generic_file_llseek(file, offset, whence);
2344 inode_unlock(inode);
2349 retval = fuse_lseek(file, offset, whence);
2350 inode_unlock(inode);
2360 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2361 * ABI was defined to be 'struct iovec' which is different on 32bit
2362 * and 64bit. Fortunately we can determine which structure the server
2363 * used from the size of the reply.
2365 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2366 size_t transferred, unsigned count,
2369 #ifdef CONFIG_COMPAT
2370 if (count * sizeof(struct compat_iovec) == transferred) {
2371 struct compat_iovec *ciov = src;
2375 * With this interface a 32bit server cannot support
2376 * non-compat (i.e. ones coming from 64bit apps) ioctl
2382 for (i = 0; i < count; i++) {
2383 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2384 dst[i].iov_len = ciov[i].iov_len;
2390 if (count * sizeof(struct iovec) != transferred)
2393 memcpy(dst, src, transferred);
2397 /* Make sure iov_length() won't overflow */
2398 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2401 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2403 for (n = 0; n < count; n++, iov++) {
2404 if (iov->iov_len > (size_t) max)
2406 max -= iov->iov_len;
2411 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2412 void *src, size_t transferred, unsigned count,
2416 struct fuse_ioctl_iovec *fiov = src;
2418 if (fc->minor < 16) {
2419 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2423 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2426 for (i = 0; i < count; i++) {
2427 /* Did the server supply an inappropriate value? */
2428 if (fiov[i].base != (unsigned long) fiov[i].base ||
2429 fiov[i].len != (unsigned long) fiov[i].len)
2432 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2433 dst[i].iov_len = (size_t) fiov[i].len;
2435 #ifdef CONFIG_COMPAT
2437 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2438 (compat_size_t) dst[i].iov_len != fiov[i].len))
2448 * For ioctls, there is no generic way to determine how much memory
2449 * needs to be read and/or written. Furthermore, ioctls are allowed
2450 * to dereference the passed pointer, so the parameter requires deep
2451 * copying but FUSE has no idea whatsoever about what to copy in or
2454 * This is solved by allowing FUSE server to retry ioctl with
2455 * necessary in/out iovecs. Let's assume the ioctl implementation
2456 * needs to read in the following structure.
2463 * On the first callout to FUSE server, inarg->in_size and
2464 * inarg->out_size will be NULL; then, the server completes the ioctl
2465 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2466 * the actual iov array to
2468 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2470 * which tells FUSE to copy in the requested area and retry the ioctl.
2471 * On the second round, the server has access to the structure and
2472 * from that it can tell what to look for next, so on the invocation,
2473 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2475 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2476 * { .iov_base = a.buf, .iov_len = a.buflen } }
2478 * FUSE will copy both struct a and the pointed buffer from the
2479 * process doing the ioctl and retry ioctl with both struct a and the
2482 * This time, FUSE server has everything it needs and completes ioctl
2483 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2485 * Copying data out works the same way.
2487 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2488 * automatically initializes in and out iovs by decoding @cmd with
2489 * _IOC_* macros and the server is not allowed to request RETRY. This
2490 * limits ioctl data transfers to well-formed ioctls and is the forced
2491 * behavior for all FUSE servers.
2493 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2496 struct fuse_file *ff = file->private_data;
2497 struct fuse_conn *fc = ff->fc;
2498 struct fuse_ioctl_in inarg = {
2504 struct fuse_ioctl_out outarg;
2505 struct fuse_req *req = NULL;
2506 struct page **pages = NULL;
2507 struct iovec *iov_page = NULL;
2508 struct iovec *in_iov = NULL, *out_iov = NULL;
2509 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2510 size_t in_size, out_size, transferred, c;
2514 #if BITS_PER_LONG == 32
2515 inarg.flags |= FUSE_IOCTL_32BIT;
2517 if (flags & FUSE_IOCTL_COMPAT)
2518 inarg.flags |= FUSE_IOCTL_32BIT;
2521 /* assume all the iovs returned by client always fits in a page */
2522 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2525 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2526 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2527 if (!pages || !iov_page)
2531 * If restricted, initialize IO parameters as encoded in @cmd.
2532 * RETRY from server is not allowed.
2534 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2535 struct iovec *iov = iov_page;
2537 iov->iov_base = (void __user *)arg;
2540 case FS_IOC_GETFLAGS:
2541 case FS_IOC_SETFLAGS:
2542 iov->iov_len = sizeof(int);
2545 iov->iov_len = _IOC_SIZE(cmd);
2549 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2554 if (_IOC_DIR(cmd) & _IOC_READ) {
2561 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2562 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2565 * Out data can be used either for actual out data or iovs,
2566 * make sure there always is at least one page.
2568 out_size = max_t(size_t, out_size, PAGE_SIZE);
2569 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2571 /* make sure there are enough buffer pages and init request with them */
2573 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2575 while (num_pages < max_pages) {
2576 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2577 if (!pages[num_pages])
2582 req = fuse_get_req(fc, num_pages);
2588 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2589 req->num_pages = num_pages;
2590 fuse_page_descs_length_init(req, 0, req->num_pages);
2592 /* okay, let's send it to the client */
2593 req->in.h.opcode = FUSE_IOCTL;
2594 req->in.h.nodeid = ff->nodeid;
2595 req->in.numargs = 1;
2596 req->in.args[0].size = sizeof(inarg);
2597 req->in.args[0].value = &inarg;
2600 req->in.args[1].size = in_size;
2601 req->in.argpages = 1;
2604 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2605 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2606 c = copy_page_from_iter(pages[i], 0, PAGE_SIZE, &ii);
2607 if (c != PAGE_SIZE && iov_iter_count(&ii))
2612 req->out.numargs = 2;
2613 req->out.args[0].size = sizeof(outarg);
2614 req->out.args[0].value = &outarg;
2615 req->out.args[1].size = out_size;
2616 req->out.argpages = 1;
2617 req->out.argvar = 1;
2619 fuse_request_send(fc, req);
2620 err = req->out.h.error;
2621 transferred = req->out.args[1].size;
2622 fuse_put_request(fc, req);
2627 /* did it ask for retry? */
2628 if (outarg.flags & FUSE_IOCTL_RETRY) {
2631 /* no retry if in restricted mode */
2633 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2636 in_iovs = outarg.in_iovs;
2637 out_iovs = outarg.out_iovs;
2640 * Make sure things are in boundary, separate checks
2641 * are to protect against overflow.
2644 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2645 out_iovs > FUSE_IOCTL_MAX_IOV ||
2646 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2649 vaddr = kmap_atomic(pages[0]);
2650 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2651 transferred, in_iovs + out_iovs,
2652 (flags & FUSE_IOCTL_COMPAT) != 0);
2653 kunmap_atomic(vaddr);
2658 out_iov = in_iov + in_iovs;
2660 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2664 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2672 if (transferred > inarg.out_size)
2676 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2677 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= num_pages); i++) {
2678 c = copy_page_to_iter(pages[i], 0, PAGE_SIZE, &ii);
2679 if (c != PAGE_SIZE && iov_iter_count(&ii))
2685 fuse_put_request(fc, req);
2686 free_page((unsigned long) iov_page);
2688 __free_page(pages[--num_pages]);
2691 return err ? err : outarg.result;
2693 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2695 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2696 unsigned long arg, unsigned int flags)
2698 struct inode *inode = file_inode(file);
2699 struct fuse_conn *fc = get_fuse_conn(inode);
2701 if (!fuse_allow_current_process(fc))
2704 if (is_bad_inode(inode))
2707 return fuse_do_ioctl(file, cmd, arg, flags);
2710 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2713 return fuse_ioctl_common(file, cmd, arg, 0);
2716 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2719 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2723 * All files which have been polled are linked to RB tree
2724 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2725 * find the matching one.
2727 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2728 struct rb_node **parent_out)
2730 struct rb_node **link = &fc->polled_files.rb_node;
2731 struct rb_node *last = NULL;
2734 struct fuse_file *ff;
2737 ff = rb_entry(last, struct fuse_file, polled_node);
2740 link = &last->rb_left;
2741 else if (kh > ff->kh)
2742 link = &last->rb_right;
2753 * The file is about to be polled. Make sure it's on the polled_files
2754 * RB tree. Note that files once added to the polled_files tree are
2755 * not removed before the file is released. This is because a file
2756 * polled once is likely to be polled again.
2758 static void fuse_register_polled_file(struct fuse_conn *fc,
2759 struct fuse_file *ff)
2761 spin_lock(&fc->lock);
2762 if (RB_EMPTY_NODE(&ff->polled_node)) {
2763 struct rb_node **link, *uninitialized_var(parent);
2765 link = fuse_find_polled_node(fc, ff->kh, &parent);
2767 rb_link_node(&ff->polled_node, parent, link);
2768 rb_insert_color(&ff->polled_node, &fc->polled_files);
2770 spin_unlock(&fc->lock);
2773 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2775 struct fuse_file *ff = file->private_data;
2776 struct fuse_conn *fc = ff->fc;
2777 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2778 struct fuse_poll_out outarg;
2783 return DEFAULT_POLLMASK;
2785 poll_wait(file, &ff->poll_wait, wait);
2786 inarg.events = (__u32)poll_requested_events(wait);
2789 * Ask for notification iff there's someone waiting for it.
2790 * The client may ignore the flag and always notify.
2792 if (waitqueue_active(&ff->poll_wait)) {
2793 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2794 fuse_register_polled_file(fc, ff);
2797 args.in.h.opcode = FUSE_POLL;
2798 args.in.h.nodeid = ff->nodeid;
2799 args.in.numargs = 1;
2800 args.in.args[0].size = sizeof(inarg);
2801 args.in.args[0].value = &inarg;
2802 args.out.numargs = 1;
2803 args.out.args[0].size = sizeof(outarg);
2804 args.out.args[0].value = &outarg;
2805 err = fuse_simple_request(fc, &args);
2808 return outarg.revents;
2809 if (err == -ENOSYS) {
2811 return DEFAULT_POLLMASK;
2815 EXPORT_SYMBOL_GPL(fuse_file_poll);
2818 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2819 * wakes up the poll waiters.
2821 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2822 struct fuse_notify_poll_wakeup_out *outarg)
2824 u64 kh = outarg->kh;
2825 struct rb_node **link;
2827 spin_lock(&fc->lock);
2829 link = fuse_find_polled_node(fc, kh, NULL);
2831 struct fuse_file *ff;
2833 ff = rb_entry(*link, struct fuse_file, polled_node);
2834 wake_up_interruptible_sync(&ff->poll_wait);
2837 spin_unlock(&fc->lock);
2841 static void fuse_do_truncate(struct file *file)
2843 struct inode *inode = file->f_mapping->host;
2846 attr.ia_valid = ATTR_SIZE;
2847 attr.ia_size = i_size_read(inode);
2849 attr.ia_file = file;
2850 attr.ia_valid |= ATTR_FILE;
2852 fuse_do_setattr(file_dentry(file), &attr, file);
2855 static inline loff_t fuse_round_up(loff_t off)
2857 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2861 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2863 DECLARE_COMPLETION_ONSTACK(wait);
2865 struct file *file = iocb->ki_filp;
2866 struct fuse_file *ff = file->private_data;
2867 bool async_dio = ff->fc->async_dio;
2869 struct inode *inode;
2871 size_t count = iov_iter_count(iter);
2872 loff_t offset = iocb->ki_pos;
2873 struct fuse_io_priv *io;
2876 inode = file->f_mapping->host;
2877 i_size = i_size_read(inode);
2879 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2882 /* optimization for short read */
2883 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2884 if (offset >= i_size)
2886 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2887 count = iov_iter_count(iter);
2890 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2893 spin_lock_init(&io->lock);
2894 kref_init(&io->refcnt);
2898 io->offset = offset;
2899 io->write = (iov_iter_rw(iter) == WRITE);
2902 * By default, we want to optimize all I/Os with async request
2903 * submission to the client filesystem if supported.
2905 io->async = async_dio;
2907 io->blocking = is_sync_kiocb(iocb);
2910 * We cannot asynchronously extend the size of a file.
2911 * In such case the aio will behave exactly like sync io.
2913 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2914 io->blocking = true;
2916 if (io->async && io->blocking) {
2918 * Additional reference to keep io around after
2919 * calling fuse_aio_complete()
2921 kref_get(&io->refcnt);
2925 if (iov_iter_rw(iter) == WRITE) {
2926 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2927 fuse_invalidate_attr(inode);
2929 ret = __fuse_direct_read(io, iter, &pos);
2933 bool blocking = io->blocking;
2935 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2937 /* we have a non-extending, async request, so return */
2939 return -EIOCBQUEUED;
2941 wait_for_completion(&wait);
2942 ret = fuse_get_res_by_io(io);
2945 kref_put(&io->refcnt, fuse_io_release);
2947 if (iov_iter_rw(iter) == WRITE) {
2949 fuse_write_update_size(inode, pos);
2950 else if (ret < 0 && offset + count > i_size)
2951 fuse_do_truncate(file);
2957 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2960 struct fuse_file *ff = file->private_data;
2961 struct inode *inode = file_inode(file);
2962 struct fuse_inode *fi = get_fuse_inode(inode);
2963 struct fuse_conn *fc = ff->fc;
2965 struct fuse_fallocate_in inarg = {
2972 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2973 (mode & FALLOC_FL_PUNCH_HOLE);
2975 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2978 if (fc->no_fallocate)
2983 if (mode & FALLOC_FL_PUNCH_HOLE) {
2984 loff_t endbyte = offset + length - 1;
2985 err = filemap_write_and_wait_range(inode->i_mapping,
2990 fuse_sync_writes(inode);
2994 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
2995 offset + length > i_size_read(inode)) {
2996 err = inode_newsize_ok(inode, offset + length);
3001 if (!(mode & FALLOC_FL_KEEP_SIZE))
3002 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3004 args.in.h.opcode = FUSE_FALLOCATE;
3005 args.in.h.nodeid = ff->nodeid;
3006 args.in.numargs = 1;
3007 args.in.args[0].size = sizeof(inarg);
3008 args.in.args[0].value = &inarg;
3009 err = fuse_simple_request(fc, &args);
3010 if (err == -ENOSYS) {
3011 fc->no_fallocate = 1;
3017 /* we could have extended the file */
3018 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3019 bool changed = fuse_write_update_size(inode, offset + length);
3021 if (changed && fc->writeback_cache)
3022 file_update_time(file);
3025 if (mode & FALLOC_FL_PUNCH_HOLE)
3026 truncate_pagecache_range(inode, offset, offset + length - 1);
3028 fuse_invalidate_attr(inode);
3031 if (!(mode & FALLOC_FL_KEEP_SIZE))
3032 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3035 inode_unlock(inode);
3040 static const struct file_operations fuse_file_operations = {
3041 .llseek = fuse_file_llseek,
3042 .read_iter = fuse_file_read_iter,
3043 .write_iter = fuse_file_write_iter,
3044 .mmap = fuse_file_mmap,
3046 .flush = fuse_flush,
3047 .release = fuse_release,
3048 .fsync = fuse_fsync,
3049 .lock = fuse_file_lock,
3050 .flock = fuse_file_flock,
3051 .splice_read = generic_file_splice_read,
3052 .unlocked_ioctl = fuse_file_ioctl,
3053 .compat_ioctl = fuse_file_compat_ioctl,
3054 .poll = fuse_file_poll,
3055 .fallocate = fuse_file_fallocate,
3058 static const struct file_operations fuse_direct_io_file_operations = {
3059 .llseek = fuse_file_llseek,
3060 .read_iter = fuse_direct_read_iter,
3061 .write_iter = fuse_direct_write_iter,
3062 .mmap = fuse_direct_mmap,
3064 .flush = fuse_flush,
3065 .release = fuse_release,
3066 .fsync = fuse_fsync,
3067 .lock = fuse_file_lock,
3068 .flock = fuse_file_flock,
3069 .unlocked_ioctl = fuse_file_ioctl,
3070 .compat_ioctl = fuse_file_compat_ioctl,
3071 .poll = fuse_file_poll,
3072 .fallocate = fuse_file_fallocate,
3073 /* no splice_read */
3076 static const struct address_space_operations fuse_file_aops = {
3077 .readpage = fuse_readpage,
3078 .writepage = fuse_writepage,
3079 .writepages = fuse_writepages,
3080 .launder_page = fuse_launder_page,
3081 .readpages = fuse_readpages,
3082 .set_page_dirty = __set_page_dirty_nobuffers,
3084 .direct_IO = fuse_direct_IO,
3085 .write_begin = fuse_write_begin,
3086 .write_end = fuse_write_end,
3089 void fuse_init_file_inode(struct inode *inode)
3091 inode->i_fop = &fuse_file_operations;
3092 inode->i_data.a_ops = &fuse_file_aops;