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 struct page **fuse_pages_alloc(unsigned int npages, gfp_t flags,
23 struct fuse_page_desc **desc)
27 pages = kzalloc(npages * (sizeof(struct page *) +
28 sizeof(struct fuse_page_desc)), flags);
29 *desc = (void *) (pages + npages);
34 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
35 int opcode, struct fuse_open_out *outargp)
37 struct fuse_open_in inarg;
40 memset(&inarg, 0, sizeof(inarg));
41 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
42 if (!fc->atomic_o_trunc)
43 inarg.flags &= ~O_TRUNC;
47 args.in_args[0].size = sizeof(inarg);
48 args.in_args[0].value = &inarg;
50 args.out_args[0].size = sizeof(*outargp);
51 args.out_args[0].value = outargp;
53 return fuse_simple_request(fc, &args);
56 struct fuse_release_args {
57 struct fuse_args args;
58 struct fuse_release_in inarg;
62 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
66 ff = kzalloc(sizeof(struct fuse_file), GFP_KERNEL_ACCOUNT);
71 ff->release_args = kzalloc(sizeof(*ff->release_args),
73 if (!ff->release_args) {
78 INIT_LIST_HEAD(&ff->write_entry);
79 mutex_init(&ff->readdir.lock);
80 refcount_set(&ff->count, 1);
81 RB_CLEAR_NODE(&ff->polled_node);
82 init_waitqueue_head(&ff->poll_wait);
84 ff->kh = atomic64_inc_return(&fc->khctr);
89 void fuse_file_free(struct fuse_file *ff)
91 kfree(ff->release_args);
92 mutex_destroy(&ff->readdir.lock);
96 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
98 refcount_inc(&ff->count);
102 static void fuse_release_end(struct fuse_conn *fc, struct fuse_args *args,
105 struct fuse_release_args *ra = container_of(args, typeof(*ra), args);
111 static void fuse_file_put(struct fuse_file *ff, bool sync, bool isdir)
113 if (refcount_dec_and_test(&ff->count)) {
114 struct fuse_args *args = &ff->release_args->args;
116 if (isdir ? ff->fc->no_opendir : ff->fc->no_open) {
117 /* Do nothing when client does not implement 'open' */
118 fuse_release_end(ff->fc, args, 0);
120 fuse_simple_request(ff->fc, args);
121 fuse_release_end(ff->fc, args, 0);
123 args->end = fuse_release_end;
124 if (fuse_simple_background(ff->fc, args,
125 GFP_KERNEL | __GFP_NOFAIL))
126 fuse_release_end(ff->fc, args, -ENOTCONN);
132 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
135 struct fuse_file *ff;
136 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
138 ff = fuse_file_alloc(fc);
143 /* Default for no-open */
144 ff->open_flags = FOPEN_KEEP_CACHE | (isdir ? FOPEN_CACHE_DIR : 0);
145 if (isdir ? !fc->no_opendir : !fc->no_open) {
146 struct fuse_open_out outarg;
149 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
152 ff->open_flags = outarg.open_flags;
154 } else if (err != -ENOSYS) {
166 ff->open_flags &= ~FOPEN_DIRECT_IO;
169 file->private_data = ff;
173 EXPORT_SYMBOL_GPL(fuse_do_open);
175 static void fuse_link_write_file(struct file *file)
177 struct inode *inode = file_inode(file);
178 struct fuse_inode *fi = get_fuse_inode(inode);
179 struct fuse_file *ff = file->private_data;
181 * file may be written through mmap, so chain it onto the
182 * inodes's write_file list
184 spin_lock(&fi->lock);
185 if (list_empty(&ff->write_entry))
186 list_add(&ff->write_entry, &fi->write_files);
187 spin_unlock(&fi->lock);
190 void fuse_finish_open(struct inode *inode, struct file *file)
192 struct fuse_file *ff = file->private_data;
193 struct fuse_conn *fc = get_fuse_conn(inode);
195 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
196 invalidate_inode_pages2(inode->i_mapping);
197 if (ff->open_flags & FOPEN_STREAM)
198 stream_open(inode, file);
199 else if (ff->open_flags & FOPEN_NONSEEKABLE)
200 nonseekable_open(inode, file);
201 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
202 struct fuse_inode *fi = get_fuse_inode(inode);
204 spin_lock(&fi->lock);
205 fi->attr_version = atomic64_inc_return(&fc->attr_version);
206 i_size_write(inode, 0);
207 spin_unlock(&fi->lock);
208 fuse_invalidate_attr(inode);
209 if (fc->writeback_cache)
210 file_update_time(file);
212 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
213 fuse_link_write_file(file);
216 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
218 struct fuse_conn *fc = get_fuse_conn(inode);
220 bool is_wb_truncate = (file->f_flags & O_TRUNC) &&
221 fc->atomic_o_trunc &&
224 err = generic_file_open(inode, file);
228 if (is_wb_truncate) {
230 fuse_set_nowrite(inode);
233 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
236 fuse_finish_open(inode, file);
238 if (is_wb_truncate) {
239 fuse_release_nowrite(inode);
246 static void fuse_prepare_release(struct fuse_inode *fi, struct fuse_file *ff,
247 int flags, int opcode)
249 struct fuse_conn *fc = ff->fc;
250 struct fuse_release_args *ra = ff->release_args;
252 /* Inode is NULL on error path of fuse_create_open() */
254 spin_lock(&fi->lock);
255 list_del(&ff->write_entry);
256 spin_unlock(&fi->lock);
258 spin_lock(&fc->lock);
259 if (!RB_EMPTY_NODE(&ff->polled_node))
260 rb_erase(&ff->polled_node, &fc->polled_files);
261 spin_unlock(&fc->lock);
263 wake_up_interruptible_all(&ff->poll_wait);
265 ra->inarg.fh = ff->fh;
266 ra->inarg.flags = flags;
267 ra->args.in_numargs = 1;
268 ra->args.in_args[0].size = sizeof(struct fuse_release_in);
269 ra->args.in_args[0].value = &ra->inarg;
270 ra->args.opcode = opcode;
271 ra->args.nodeid = ff->nodeid;
272 ra->args.force = true;
273 ra->args.nocreds = true;
276 void fuse_release_common(struct file *file, bool isdir)
278 struct fuse_inode *fi = get_fuse_inode(file_inode(file));
279 struct fuse_file *ff = file->private_data;
280 struct fuse_release_args *ra = ff->release_args;
281 int opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
283 fuse_prepare_release(fi, ff, file->f_flags, opcode);
286 ra->inarg.release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
287 ra->inarg.lock_owner = fuse_lock_owner_id(ff->fc,
290 /* Hold inode until release is finished */
291 ra->inode = igrab(file_inode(file));
294 * Normally this will send the RELEASE request, however if
295 * some asynchronous READ or WRITE requests are outstanding,
296 * the sending will be delayed.
298 * Make the release synchronous if this is a fuseblk mount,
299 * synchronous RELEASE is allowed (and desirable) in this case
300 * because the server can be trusted not to screw up.
302 fuse_file_put(ff, ff->fc->destroy, isdir);
305 static int fuse_open(struct inode *inode, struct file *file)
307 return fuse_open_common(inode, file, false);
310 static int fuse_release(struct inode *inode, struct file *file)
312 struct fuse_conn *fc = get_fuse_conn(inode);
314 /* see fuse_vma_close() for !writeback_cache case */
315 if (fc->writeback_cache)
316 write_inode_now(inode, 1);
318 fuse_release_common(file, false);
320 /* return value is ignored by VFS */
324 void fuse_sync_release(struct fuse_inode *fi, struct fuse_file *ff, int flags)
326 WARN_ON(refcount_read(&ff->count) > 1);
327 fuse_prepare_release(fi, ff, flags, FUSE_RELEASE);
329 * iput(NULL) is a no-op and since the refcount is 1 and everything's
330 * synchronous, we are fine with not doing igrab() here"
332 fuse_file_put(ff, true, false);
334 EXPORT_SYMBOL_GPL(fuse_sync_release);
337 * Scramble the ID space with XTEA, so that the value of the files_struct
338 * pointer is not exposed to userspace.
340 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
342 u32 *k = fc->scramble_key;
343 u64 v = (unsigned long) id;
349 for (i = 0; i < 32; i++) {
350 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
352 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
355 return (u64) v0 + ((u64) v1 << 32);
358 struct fuse_writepage_args {
359 struct fuse_io_args ia;
360 struct list_head writepages_entry;
361 struct list_head queue_entry;
362 struct fuse_writepage_args *next;
366 static struct fuse_writepage_args *fuse_find_writeback(struct fuse_inode *fi,
367 pgoff_t idx_from, pgoff_t idx_to)
369 struct fuse_writepage_args *wpa;
371 list_for_each_entry(wpa, &fi->writepages, writepages_entry) {
374 WARN_ON(get_fuse_inode(wpa->inode) != fi);
375 curr_index = wpa->ia.write.in.offset >> PAGE_SHIFT;
376 if (idx_from < curr_index + wpa->ia.ap.num_pages &&
377 curr_index <= idx_to) {
385 * Check if any page in a range is under writeback
387 * This is currently done by walking the list of writepage requests
388 * for the inode, which can be pretty inefficient.
390 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
393 struct fuse_inode *fi = get_fuse_inode(inode);
396 spin_lock(&fi->lock);
397 found = fuse_find_writeback(fi, idx_from, idx_to);
398 spin_unlock(&fi->lock);
403 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
405 return fuse_range_is_writeback(inode, index, index);
409 * Wait for page writeback to be completed.
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
414 static void fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
416 struct fuse_inode *fi = get_fuse_inode(inode);
418 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
422 * Wait for all pending writepages on the inode to finish.
424 * This is currently done by blocking further writes with FUSE_NOWRITE
425 * and waiting for all sent writes to complete.
427 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
428 * could conflict with truncation.
430 static void fuse_sync_writes(struct inode *inode)
432 fuse_set_nowrite(inode);
433 fuse_release_nowrite(inode);
436 static int fuse_flush(struct file *file, fl_owner_t id)
438 struct inode *inode = file_inode(file);
439 struct fuse_conn *fc = get_fuse_conn(inode);
440 struct fuse_file *ff = file->private_data;
441 struct fuse_flush_in inarg;
445 if (is_bad_inode(inode))
451 err = write_inode_now(inode, 1);
456 fuse_sync_writes(inode);
459 err = filemap_check_errors(file->f_mapping);
463 memset(&inarg, 0, sizeof(inarg));
465 inarg.lock_owner = fuse_lock_owner_id(fc, id);
466 args.opcode = FUSE_FLUSH;
467 args.nodeid = get_node_id(inode);
469 args.in_args[0].size = sizeof(inarg);
470 args.in_args[0].value = &inarg;
473 err = fuse_simple_request(fc, &args);
474 if (err == -ENOSYS) {
481 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
482 int datasync, int opcode)
484 struct inode *inode = file->f_mapping->host;
485 struct fuse_conn *fc = get_fuse_conn(inode);
486 struct fuse_file *ff = file->private_data;
488 struct fuse_fsync_in inarg;
490 memset(&inarg, 0, sizeof(inarg));
492 inarg.fsync_flags = datasync ? FUSE_FSYNC_FDATASYNC : 0;
493 args.opcode = opcode;
494 args.nodeid = get_node_id(inode);
496 args.in_args[0].size = sizeof(inarg);
497 args.in_args[0].value = &inarg;
498 return fuse_simple_request(fc, &args);
501 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
504 struct inode *inode = file->f_mapping->host;
505 struct fuse_conn *fc = get_fuse_conn(inode);
508 if (is_bad_inode(inode))
514 * Start writeback against all dirty pages of the inode, then
515 * wait for all outstanding writes, before sending the FSYNC
518 err = file_write_and_wait_range(file, start, end);
522 fuse_sync_writes(inode);
525 * Due to implementation of fuse writeback
526 * file_write_and_wait_range() does not catch errors.
527 * We have to do this directly after fuse_sync_writes()
529 err = file_check_and_advance_wb_err(file);
533 err = sync_inode_metadata(inode, 1);
540 err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNC);
541 if (err == -ENOSYS) {
551 void fuse_read_args_fill(struct fuse_io_args *ia, struct file *file, loff_t pos,
552 size_t count, int opcode)
554 struct fuse_file *ff = file->private_data;
555 struct fuse_args *args = &ia->ap.args;
557 ia->read.in.fh = ff->fh;
558 ia->read.in.offset = pos;
559 ia->read.in.size = count;
560 ia->read.in.flags = file->f_flags;
561 args->opcode = opcode;
562 args->nodeid = ff->nodeid;
563 args->in_numargs = 1;
564 args->in_args[0].size = sizeof(ia->read.in);
565 args->in_args[0].value = &ia->read.in;
566 args->out_argvar = true;
567 args->out_numargs = 1;
568 args->out_args[0].size = count;
571 static void fuse_release_user_pages(struct fuse_args_pages *ap,
576 for (i = 0; i < ap->num_pages; i++) {
578 set_page_dirty_lock(ap->pages[i]);
579 put_page(ap->pages[i]);
583 static void fuse_io_release(struct kref *kref)
585 kfree(container_of(kref, struct fuse_io_priv, refcnt));
588 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
593 if (io->bytes >= 0 && io->write)
596 return io->bytes < 0 ? io->size : io->bytes;
600 * In case of short read, the caller sets 'pos' to the position of
601 * actual end of fuse request in IO request. Otherwise, if bytes_requested
602 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
605 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
606 * both submitted asynchronously. The first of them was ACKed by userspace as
607 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
608 * second request was ACKed as short, e.g. only 1K was read, resulting in
611 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
612 * will be equal to the length of the longest contiguous fragment of
613 * transferred data starting from the beginning of IO request.
615 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
619 spin_lock(&io->lock);
621 io->err = io->err ? : err;
622 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
626 if (!left && io->blocking)
628 spin_unlock(&io->lock);
630 if (!left && !io->blocking) {
631 ssize_t res = fuse_get_res_by_io(io);
634 struct inode *inode = file_inode(io->iocb->ki_filp);
635 struct fuse_conn *fc = get_fuse_conn(inode);
636 struct fuse_inode *fi = get_fuse_inode(inode);
638 spin_lock(&fi->lock);
639 fi->attr_version = atomic64_inc_return(&fc->attr_version);
640 spin_unlock(&fi->lock);
643 io->iocb->ki_complete(io->iocb, res, 0);
646 kref_put(&io->refcnt, fuse_io_release);
649 static struct fuse_io_args *fuse_io_alloc(struct fuse_io_priv *io,
652 struct fuse_io_args *ia;
654 ia = kzalloc(sizeof(*ia), GFP_KERNEL);
657 ia->ap.pages = fuse_pages_alloc(npages, GFP_KERNEL,
667 static void fuse_io_free(struct fuse_io_args *ia)
673 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_args *args,
676 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
677 struct fuse_io_priv *io = ia->io;
680 fuse_release_user_pages(&ia->ap, io->should_dirty);
684 } else if (io->write) {
685 if (ia->write.out.size > ia->write.in.size) {
687 } else if (ia->write.in.size != ia->write.out.size) {
688 pos = ia->write.in.offset - io->offset +
692 u32 outsize = args->out_args[0].size;
694 if (ia->read.in.size != outsize)
695 pos = ia->read.in.offset - io->offset + outsize;
698 fuse_aio_complete(io, err, pos);
702 static ssize_t fuse_async_req_send(struct fuse_conn *fc,
703 struct fuse_io_args *ia, size_t num_bytes)
706 struct fuse_io_priv *io = ia->io;
708 spin_lock(&io->lock);
709 kref_get(&io->refcnt);
710 io->size += num_bytes;
712 spin_unlock(&io->lock);
714 ia->ap.args.end = fuse_aio_complete_req;
715 err = fuse_simple_background(fc, &ia->ap.args, GFP_KERNEL);
717 return err ?: num_bytes;
720 static ssize_t fuse_send_read(struct fuse_io_args *ia, loff_t pos, size_t count,
723 struct file *file = ia->io->iocb->ki_filp;
724 struct fuse_file *ff = file->private_data;
725 struct fuse_conn *fc = ff->fc;
727 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
729 ia->read.in.read_flags |= FUSE_READ_LOCKOWNER;
730 ia->read.in.lock_owner = fuse_lock_owner_id(fc, owner);
734 return fuse_async_req_send(fc, ia, count);
736 return fuse_simple_request(fc, &ia->ap.args);
739 static void fuse_read_update_size(struct inode *inode, loff_t size,
742 struct fuse_conn *fc = get_fuse_conn(inode);
743 struct fuse_inode *fi = get_fuse_inode(inode);
745 spin_lock(&fi->lock);
746 if (attr_ver == fi->attr_version && size < inode->i_size &&
747 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
748 fi->attr_version = atomic64_inc_return(&fc->attr_version);
749 i_size_write(inode, size);
751 spin_unlock(&fi->lock);
754 static void fuse_short_read(struct inode *inode, u64 attr_ver, size_t num_read,
755 struct fuse_args_pages *ap)
757 struct fuse_conn *fc = get_fuse_conn(inode);
759 if (fc->writeback_cache) {
761 * A hole in a file. Some data after the hole are in page cache,
762 * but have not reached the client fs yet. So, the hole is not
766 int start_idx = num_read >> PAGE_SHIFT;
767 size_t off = num_read & (PAGE_SIZE - 1);
769 for (i = start_idx; i < ap->num_pages; i++) {
770 zero_user_segment(ap->pages[i], off, PAGE_SIZE);
774 loff_t pos = page_offset(ap->pages[0]) + num_read;
775 fuse_read_update_size(inode, pos, attr_ver);
779 static int fuse_do_readpage(struct file *file, struct page *page)
781 struct inode *inode = page->mapping->host;
782 struct fuse_conn *fc = get_fuse_conn(inode);
783 loff_t pos = page_offset(page);
784 struct fuse_page_desc desc = { .length = PAGE_SIZE };
785 struct fuse_io_args ia = {
786 .ap.args.page_zeroing = true,
787 .ap.args.out_pages = true,
796 * Page writeback can extend beyond the lifetime of the
797 * page-cache page, so make sure we read a properly synced
800 fuse_wait_on_page_writeback(inode, page->index);
802 attr_ver = fuse_get_attr_version(fc);
804 fuse_read_args_fill(&ia, file, pos, desc.length, FUSE_READ);
805 res = fuse_simple_request(fc, &ia.ap.args);
809 * Short read means EOF. If file size is larger, truncate it
811 if (res < desc.length)
812 fuse_short_read(inode, attr_ver, res, &ia.ap);
814 SetPageUptodate(page);
819 static int fuse_readpage(struct file *file, struct page *page)
821 struct inode *inode = page->mapping->host;
825 if (is_bad_inode(inode))
828 err = fuse_do_readpage(file, page);
829 fuse_invalidate_atime(inode);
835 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_args *args,
839 struct fuse_io_args *ia = container_of(args, typeof(*ia), ap.args);
840 struct fuse_args_pages *ap = &ia->ap;
841 size_t count = ia->read.in.size;
842 size_t num_read = args->out_args[0].size;
843 struct address_space *mapping = NULL;
845 for (i = 0; mapping == NULL && i < ap->num_pages; i++)
846 mapping = ap->pages[i]->mapping;
849 struct inode *inode = mapping->host;
852 * Short read means EOF. If file size is larger, truncate it
854 if (!err && num_read < count)
855 fuse_short_read(inode, ia->read.attr_ver, num_read, ap);
857 fuse_invalidate_atime(inode);
860 for (i = 0; i < ap->num_pages; i++) {
861 struct page *page = ap->pages[i];
864 SetPageUptodate(page);
871 fuse_file_put(ia->ff, false, false);
876 static void fuse_send_readpages(struct fuse_io_args *ia, struct file *file)
878 struct fuse_file *ff = file->private_data;
879 struct fuse_conn *fc = ff->fc;
880 struct fuse_args_pages *ap = &ia->ap;
881 loff_t pos = page_offset(ap->pages[0]);
882 size_t count = ap->num_pages << PAGE_SHIFT;
885 ap->args.out_pages = true;
886 ap->args.page_zeroing = true;
887 ap->args.page_replace = true;
888 fuse_read_args_fill(ia, file, pos, count, FUSE_READ);
889 ia->read.attr_ver = fuse_get_attr_version(fc);
890 if (fc->async_read) {
891 ia->ff = fuse_file_get(ff);
892 ap->args.end = fuse_readpages_end;
893 err = fuse_simple_background(fc, &ap->args, GFP_KERNEL);
897 err = fuse_simple_request(fc, &ap->args);
899 fuse_readpages_end(fc, &ap->args, err);
902 struct fuse_fill_data {
903 struct fuse_io_args *ia;
906 unsigned int nr_pages;
907 unsigned int max_pages;
910 static int fuse_readpages_fill(void *_data, struct page *page)
912 struct fuse_fill_data *data = _data;
913 struct fuse_io_args *ia = data->ia;
914 struct fuse_args_pages *ap = &ia->ap;
915 struct inode *inode = data->inode;
916 struct fuse_conn *fc = get_fuse_conn(inode);
918 fuse_wait_on_page_writeback(inode, page->index);
921 (ap->num_pages == fc->max_pages ||
922 (ap->num_pages + 1) * PAGE_SIZE > fc->max_read ||
923 ap->pages[ap->num_pages - 1]->index + 1 != page->index)) {
924 data->max_pages = min_t(unsigned int, data->nr_pages,
926 fuse_send_readpages(ia, data->file);
927 data->ia = ia = fuse_io_alloc(NULL, data->max_pages);
935 if (WARN_ON(ap->num_pages >= data->max_pages)) {
942 ap->pages[ap->num_pages] = page;
943 ap->descs[ap->num_pages].length = PAGE_SIZE;
949 static int fuse_readpages(struct file *file, struct address_space *mapping,
950 struct list_head *pages, unsigned nr_pages)
952 struct inode *inode = mapping->host;
953 struct fuse_conn *fc = get_fuse_conn(inode);
954 struct fuse_fill_data data;
958 if (is_bad_inode(inode))
963 data.nr_pages = nr_pages;
964 data.max_pages = min_t(unsigned int, nr_pages, fc->max_pages);
966 data.ia = fuse_io_alloc(NULL, data.max_pages);
971 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
973 if (data.ia->ap.num_pages)
974 fuse_send_readpages(data.ia, file);
976 fuse_io_free(data.ia);
982 static ssize_t fuse_cache_read_iter(struct kiocb *iocb, struct iov_iter *to)
984 struct inode *inode = iocb->ki_filp->f_mapping->host;
985 struct fuse_conn *fc = get_fuse_conn(inode);
988 * In auto invalidate mode, always update attributes on read.
989 * Otherwise, only update if we attempt to read past EOF (to ensure
990 * i_size is up to date).
992 if (fc->auto_inval_data ||
993 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
995 err = fuse_update_attributes(inode, iocb->ki_filp);
1000 return generic_file_read_iter(iocb, to);
1003 static void fuse_write_args_fill(struct fuse_io_args *ia, struct fuse_file *ff,
1004 loff_t pos, size_t count)
1006 struct fuse_args *args = &ia->ap.args;
1008 ia->write.in.fh = ff->fh;
1009 ia->write.in.offset = pos;
1010 ia->write.in.size = count;
1011 args->opcode = FUSE_WRITE;
1012 args->nodeid = ff->nodeid;
1013 args->in_numargs = 2;
1014 if (ff->fc->minor < 9)
1015 args->in_args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
1017 args->in_args[0].size = sizeof(ia->write.in);
1018 args->in_args[0].value = &ia->write.in;
1019 args->in_args[1].size = count;
1020 args->out_numargs = 1;
1021 args->out_args[0].size = sizeof(ia->write.out);
1022 args->out_args[0].value = &ia->write.out;
1025 static unsigned int fuse_write_flags(struct kiocb *iocb)
1027 unsigned int flags = iocb->ki_filp->f_flags;
1029 if (iocb->ki_flags & IOCB_DSYNC)
1031 if (iocb->ki_flags & IOCB_SYNC)
1037 static ssize_t fuse_send_write(struct fuse_io_args *ia, loff_t pos,
1038 size_t count, fl_owner_t owner)
1040 struct kiocb *iocb = ia->io->iocb;
1041 struct file *file = iocb->ki_filp;
1042 struct fuse_file *ff = file->private_data;
1043 struct fuse_conn *fc = ff->fc;
1044 struct fuse_write_in *inarg = &ia->write.in;
1047 fuse_write_args_fill(ia, ff, pos, count);
1048 inarg->flags = fuse_write_flags(iocb);
1049 if (owner != NULL) {
1050 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1051 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1055 return fuse_async_req_send(fc, ia, count);
1057 err = fuse_simple_request(fc, &ia->ap.args);
1058 if (!err && ia->write.out.size > count)
1061 return err ?: ia->write.out.size;
1064 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1066 struct fuse_conn *fc = get_fuse_conn(inode);
1067 struct fuse_inode *fi = get_fuse_inode(inode);
1070 spin_lock(&fi->lock);
1071 fi->attr_version = atomic64_inc_return(&fc->attr_version);
1072 if (pos > inode->i_size) {
1073 i_size_write(inode, pos);
1076 spin_unlock(&fi->lock);
1081 static ssize_t fuse_send_write_pages(struct fuse_io_args *ia,
1082 struct kiocb *iocb, struct inode *inode,
1083 loff_t pos, size_t count)
1085 struct fuse_args_pages *ap = &ia->ap;
1086 struct file *file = iocb->ki_filp;
1087 struct fuse_file *ff = file->private_data;
1088 struct fuse_conn *fc = ff->fc;
1089 unsigned int offset, i;
1092 for (i = 0; i < ap->num_pages; i++)
1093 fuse_wait_on_page_writeback(inode, ap->pages[i]->index);
1095 fuse_write_args_fill(ia, ff, pos, count);
1096 ia->write.in.flags = fuse_write_flags(iocb);
1098 err = fuse_simple_request(fc, &ap->args);
1100 offset = ap->descs[0].offset;
1101 count = ia->write.out.size;
1102 for (i = 0; i < ap->num_pages; i++) {
1103 struct page *page = ap->pages[i];
1105 if (!err && !offset && count >= PAGE_SIZE)
1106 SetPageUptodate(page);
1108 if (count > PAGE_SIZE - offset)
1109 count -= PAGE_SIZE - offset;
1121 static ssize_t fuse_fill_write_pages(struct fuse_args_pages *ap,
1122 struct address_space *mapping,
1123 struct iov_iter *ii, loff_t pos,
1124 unsigned int max_pages)
1126 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1127 unsigned offset = pos & (PAGE_SIZE - 1);
1131 ap->args.in_pages = true;
1132 ap->descs[0].offset = offset;
1137 pgoff_t index = pos >> PAGE_SHIFT;
1138 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1139 iov_iter_count(ii));
1141 bytes = min_t(size_t, bytes, fc->max_write - count);
1145 if (iov_iter_fault_in_readable(ii, bytes))
1149 page = grab_cache_page_write_begin(mapping, index, 0);
1153 if (mapping_writably_mapped(mapping))
1154 flush_dcache_page(page);
1156 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1157 flush_dcache_page(page);
1159 iov_iter_advance(ii, tmp);
1163 bytes = min(bytes, iov_iter_single_seg_count(ii));
1168 ap->pages[ap->num_pages] = page;
1169 ap->descs[ap->num_pages].length = tmp;
1175 if (offset == PAGE_SIZE)
1178 if (!fc->big_writes)
1180 } while (iov_iter_count(ii) && count < fc->max_write &&
1181 ap->num_pages < max_pages && offset == 0);
1183 return count > 0 ? count : err;
1186 static inline unsigned int fuse_wr_pages(loff_t pos, size_t len,
1187 unsigned int max_pages)
1189 return min_t(unsigned int,
1190 ((pos + len - 1) >> PAGE_SHIFT) -
1191 (pos >> PAGE_SHIFT) + 1,
1195 static ssize_t fuse_perform_write(struct kiocb *iocb,
1196 struct address_space *mapping,
1197 struct iov_iter *ii, loff_t pos)
1199 struct inode *inode = mapping->host;
1200 struct fuse_conn *fc = get_fuse_conn(inode);
1201 struct fuse_inode *fi = get_fuse_inode(inode);
1205 if (inode->i_size < pos + iov_iter_count(ii))
1206 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1210 struct fuse_io_args ia = {};
1211 struct fuse_args_pages *ap = &ia.ap;
1212 unsigned int nr_pages = fuse_wr_pages(pos, iov_iter_count(ii),
1215 ap->pages = fuse_pages_alloc(nr_pages, GFP_KERNEL, &ap->descs);
1221 count = fuse_fill_write_pages(ap, mapping, ii, pos, nr_pages);
1225 err = fuse_send_write_pages(&ia, iocb, inode,
1228 size_t num_written = ia.write.out.size;
1233 /* break out of the loop on short write */
1234 if (num_written != count)
1239 } while (!err && iov_iter_count(ii));
1242 fuse_write_update_size(inode, pos);
1244 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1245 fuse_invalidate_attr(inode);
1247 return res > 0 ? res : err;
1250 static ssize_t fuse_cache_write_iter(struct kiocb *iocb, struct iov_iter *from)
1252 struct file *file = iocb->ki_filp;
1253 struct address_space *mapping = file->f_mapping;
1254 ssize_t written = 0;
1255 ssize_t written_buffered = 0;
1256 struct inode *inode = mapping->host;
1260 if (get_fuse_conn(inode)->writeback_cache) {
1261 /* Update size (EOF optimization) and mode (SUID clearing) */
1262 err = fuse_update_attributes(mapping->host, file);
1266 return generic_file_write_iter(iocb, from);
1271 /* We can write back this queue in page reclaim */
1272 current->backing_dev_info = inode_to_bdi(inode);
1274 err = generic_write_checks(iocb, from);
1278 err = file_remove_privs(file);
1282 err = file_update_time(file);
1286 if (iocb->ki_flags & IOCB_DIRECT) {
1287 loff_t pos = iocb->ki_pos;
1288 written = generic_file_direct_write(iocb, from);
1289 if (written < 0 || !iov_iter_count(from))
1294 written_buffered = fuse_perform_write(iocb, mapping, from, pos);
1295 if (written_buffered < 0) {
1296 err = written_buffered;
1299 endbyte = pos + written_buffered - 1;
1301 err = filemap_write_and_wait_range(file->f_mapping, pos,
1306 invalidate_mapping_pages(file->f_mapping,
1308 endbyte >> PAGE_SHIFT);
1310 written += written_buffered;
1311 iocb->ki_pos = pos + written_buffered;
1313 written = fuse_perform_write(iocb, mapping, from, iocb->ki_pos);
1315 iocb->ki_pos += written;
1318 current->backing_dev_info = NULL;
1319 inode_unlock(inode);
1321 written = generic_write_sync(iocb, written);
1323 return written ? written : err;
1326 static inline void fuse_page_descs_length_init(struct fuse_page_desc *descs,
1328 unsigned int nr_pages)
1332 for (i = index; i < index + nr_pages; i++)
1333 descs[i].length = PAGE_SIZE - descs[i].offset;
1336 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1338 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1341 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1344 return min(iov_iter_single_seg_count(ii), max_size);
1347 static int fuse_get_user_pages(struct fuse_args_pages *ap, struct iov_iter *ii,
1348 size_t *nbytesp, int write,
1349 unsigned int max_pages)
1351 size_t nbytes = 0; /* # bytes already packed in req */
1354 /* Special case for kernel I/O: can copy directly into the buffer */
1355 if (iov_iter_is_kvec(ii)) {
1356 unsigned long user_addr = fuse_get_user_addr(ii);
1357 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1360 ap->args.in_args[1].value = (void *) user_addr;
1362 ap->args.out_args[0].value = (void *) user_addr;
1364 iov_iter_advance(ii, frag_size);
1365 *nbytesp = frag_size;
1369 while (nbytes < *nbytesp && ap->num_pages < max_pages) {
1372 ret = iov_iter_get_pages(ii, &ap->pages[ap->num_pages],
1374 max_pages - ap->num_pages,
1379 iov_iter_advance(ii, ret);
1383 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1385 ap->descs[ap->num_pages].offset = start;
1386 fuse_page_descs_length_init(ap->descs, ap->num_pages, npages);
1388 ap->num_pages += npages;
1389 ap->descs[ap->num_pages - 1].length -=
1390 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1394 ap->args.in_pages = 1;
1396 ap->args.out_pages = 1;
1400 return ret < 0 ? ret : 0;
1403 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1404 loff_t *ppos, int flags)
1406 int write = flags & FUSE_DIO_WRITE;
1407 int cuse = flags & FUSE_DIO_CUSE;
1408 struct file *file = io->iocb->ki_filp;
1409 struct inode *inode = file->f_mapping->host;
1410 struct fuse_file *ff = file->private_data;
1411 struct fuse_conn *fc = ff->fc;
1412 size_t nmax = write ? fc->max_write : fc->max_read;
1414 size_t count = iov_iter_count(iter);
1415 pgoff_t idx_from = pos >> PAGE_SHIFT;
1416 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1419 struct fuse_io_args *ia;
1420 unsigned int max_pages;
1422 max_pages = iov_iter_npages(iter, fc->max_pages);
1423 ia = fuse_io_alloc(io, max_pages);
1428 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1431 fuse_sync_writes(inode);
1433 inode_unlock(inode);
1436 io->should_dirty = !write && iter_is_iovec(iter);
1439 fl_owner_t owner = current->files;
1440 size_t nbytes = min(count, nmax);
1442 err = fuse_get_user_pages(&ia->ap, iter, &nbytes, write,
1448 if (!capable(CAP_FSETID))
1449 ia->write.in.write_flags |= FUSE_WRITE_KILL_PRIV;
1451 nres = fuse_send_write(ia, pos, nbytes, owner);
1453 nres = fuse_send_read(ia, pos, nbytes, owner);
1456 if (!io->async || nres < 0) {
1457 fuse_release_user_pages(&ia->ap, io->should_dirty);
1465 WARN_ON(nres > nbytes);
1473 max_pages = iov_iter_npages(iter, fc->max_pages);
1474 ia = fuse_io_alloc(io, max_pages);
1484 return res > 0 ? res : err;
1486 EXPORT_SYMBOL_GPL(fuse_direct_io);
1488 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1489 struct iov_iter *iter,
1493 struct inode *inode = file_inode(io->iocb->ki_filp);
1495 res = fuse_direct_io(io, iter, ppos, 0);
1497 fuse_invalidate_atime(inode);
1502 static ssize_t fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter);
1504 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1508 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1509 res = fuse_direct_IO(iocb, to);
1511 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1513 res = __fuse_direct_read(&io, to, &iocb->ki_pos);
1519 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1521 struct inode *inode = file_inode(iocb->ki_filp);
1522 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb);
1525 /* Don't allow parallel writes to the same file */
1527 res = generic_write_checks(iocb, from);
1529 if (!is_sync_kiocb(iocb) && iocb->ki_flags & IOCB_DIRECT) {
1530 res = fuse_direct_IO(iocb, from);
1532 res = fuse_direct_io(&io, from, &iocb->ki_pos,
1536 fuse_invalidate_attr(inode);
1538 fuse_write_update_size(inode, iocb->ki_pos);
1539 inode_unlock(inode);
1544 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
1546 struct file *file = iocb->ki_filp;
1547 struct fuse_file *ff = file->private_data;
1549 if (is_bad_inode(file_inode(file)))
1552 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1553 return fuse_cache_read_iter(iocb, to);
1555 return fuse_direct_read_iter(iocb, to);
1558 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1560 struct file *file = iocb->ki_filp;
1561 struct fuse_file *ff = file->private_data;
1563 if (is_bad_inode(file_inode(file)))
1566 if (!(ff->open_flags & FOPEN_DIRECT_IO))
1567 return fuse_cache_write_iter(iocb, from);
1569 return fuse_direct_write_iter(iocb, from);
1572 static void fuse_writepage_free(struct fuse_writepage_args *wpa)
1574 struct fuse_args_pages *ap = &wpa->ia.ap;
1577 for (i = 0; i < ap->num_pages; i++)
1578 __free_page(ap->pages[i]);
1581 fuse_file_put(wpa->ia.ff, false, false);
1587 static void fuse_writepage_finish(struct fuse_conn *fc,
1588 struct fuse_writepage_args *wpa)
1590 struct fuse_args_pages *ap = &wpa->ia.ap;
1591 struct inode *inode = wpa->inode;
1592 struct fuse_inode *fi = get_fuse_inode(inode);
1593 struct backing_dev_info *bdi = inode_to_bdi(inode);
1596 list_del(&wpa->writepages_entry);
1597 for (i = 0; i < ap->num_pages; i++) {
1598 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1599 dec_node_page_state(ap->pages[i], NR_WRITEBACK_TEMP);
1600 wb_writeout_inc(&bdi->wb);
1602 wake_up(&fi->page_waitq);
1605 /* Called under fi->lock, may release and reacquire it */
1606 static void fuse_send_writepage(struct fuse_conn *fc,
1607 struct fuse_writepage_args *wpa, loff_t size)
1608 __releases(fi->lock)
1609 __acquires(fi->lock)
1611 struct fuse_writepage_args *aux, *next;
1612 struct fuse_inode *fi = get_fuse_inode(wpa->inode);
1613 struct fuse_write_in *inarg = &wpa->ia.write.in;
1614 struct fuse_args *args = &wpa->ia.ap.args;
1615 __u64 data_size = wpa->ia.ap.num_pages * PAGE_SIZE;
1619 if (inarg->offset + data_size <= size) {
1620 inarg->size = data_size;
1621 } else if (inarg->offset < size) {
1622 inarg->size = size - inarg->offset;
1624 /* Got truncated off completely */
1628 args->in_args[1].size = inarg->size;
1630 args->nocreds = true;
1632 err = fuse_simple_background(fc, args, GFP_ATOMIC);
1633 if (err == -ENOMEM) {
1634 spin_unlock(&fi->lock);
1635 err = fuse_simple_background(fc, args, GFP_NOFS | __GFP_NOFAIL);
1636 spin_lock(&fi->lock);
1639 /* Fails on broken connection only */
1647 fuse_writepage_finish(fc, wpa);
1648 spin_unlock(&fi->lock);
1650 /* After fuse_writepage_finish() aux request list is private */
1651 for (aux = wpa->next; aux; aux = next) {
1654 fuse_writepage_free(aux);
1657 fuse_writepage_free(wpa);
1658 spin_lock(&fi->lock);
1662 * If fi->writectr is positive (no truncate or fsync going on) send
1663 * all queued writepage requests.
1665 * Called with fi->lock
1667 void fuse_flush_writepages(struct inode *inode)
1668 __releases(fi->lock)
1669 __acquires(fi->lock)
1671 struct fuse_conn *fc = get_fuse_conn(inode);
1672 struct fuse_inode *fi = get_fuse_inode(inode);
1673 loff_t crop = i_size_read(inode);
1674 struct fuse_writepage_args *wpa;
1676 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1677 wpa = list_entry(fi->queued_writes.next,
1678 struct fuse_writepage_args, queue_entry);
1679 list_del_init(&wpa->queue_entry);
1680 fuse_send_writepage(fc, wpa, crop);
1684 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_args *args,
1687 struct fuse_writepage_args *wpa =
1688 container_of(args, typeof(*wpa), ia.ap.args);
1689 struct inode *inode = wpa->inode;
1690 struct fuse_inode *fi = get_fuse_inode(inode);
1692 mapping_set_error(inode->i_mapping, error);
1693 spin_lock(&fi->lock);
1695 struct fuse_conn *fc = get_fuse_conn(inode);
1696 struct fuse_write_in *inarg = &wpa->ia.write.in;
1697 struct fuse_writepage_args *next = wpa->next;
1699 wpa->next = next->next;
1701 next->ia.ff = fuse_file_get(wpa->ia.ff);
1702 list_add(&next->writepages_entry, &fi->writepages);
1705 * Skip fuse_flush_writepages() to make it easy to crop requests
1706 * based on primary request size.
1708 * 1st case (trivial): there are no concurrent activities using
1709 * fuse_set/release_nowrite. Then we're on safe side because
1710 * fuse_flush_writepages() would call fuse_send_writepage()
1713 * 2nd case: someone called fuse_set_nowrite and it is waiting
1714 * now for completion of all in-flight requests. This happens
1715 * rarely and no more than once per page, so this should be
1718 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1719 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1720 * that fuse_set_nowrite returned implies that all in-flight
1721 * requests were completed along with all of their secondary
1722 * requests. Further primary requests are blocked by negative
1723 * writectr. Hence there cannot be any in-flight requests and
1724 * no invocations of fuse_writepage_end() while we're in
1725 * fuse_set_nowrite..fuse_release_nowrite section.
1727 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1730 fuse_writepage_finish(fc, wpa);
1731 spin_unlock(&fi->lock);
1732 fuse_writepage_free(wpa);
1735 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1736 struct fuse_inode *fi)
1738 struct fuse_file *ff = NULL;
1740 spin_lock(&fi->lock);
1741 if (!list_empty(&fi->write_files)) {
1742 ff = list_entry(fi->write_files.next, struct fuse_file,
1746 spin_unlock(&fi->lock);
1751 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1752 struct fuse_inode *fi)
1754 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1759 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1761 struct fuse_conn *fc = get_fuse_conn(inode);
1762 struct fuse_inode *fi = get_fuse_inode(inode);
1763 struct fuse_file *ff;
1766 ff = __fuse_write_file_get(fc, fi);
1767 err = fuse_flush_times(inode, ff);
1769 fuse_file_put(ff, false, false);
1774 static struct fuse_writepage_args *fuse_writepage_args_alloc(void)
1776 struct fuse_writepage_args *wpa;
1777 struct fuse_args_pages *ap;
1779 wpa = kzalloc(sizeof(*wpa), GFP_NOFS);
1783 ap->pages = fuse_pages_alloc(1, GFP_NOFS, &ap->descs);
1793 static int fuse_writepage_locked(struct page *page)
1795 struct address_space *mapping = page->mapping;
1796 struct inode *inode = mapping->host;
1797 struct fuse_conn *fc = get_fuse_conn(inode);
1798 struct fuse_inode *fi = get_fuse_inode(inode);
1799 struct fuse_writepage_args *wpa;
1800 struct fuse_args_pages *ap;
1801 struct page *tmp_page;
1802 int error = -ENOMEM;
1804 set_page_writeback(page);
1806 wpa = fuse_writepage_args_alloc();
1811 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1816 wpa->ia.ff = fuse_write_file_get(fc, fi);
1820 fuse_write_args_fill(&wpa->ia, wpa->ia.ff, page_offset(page), 0);
1822 copy_highpage(tmp_page, page);
1823 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
1825 ap->args.in_pages = true;
1827 ap->pages[0] = tmp_page;
1828 ap->descs[0].offset = 0;
1829 ap->descs[0].length = PAGE_SIZE;
1830 ap->args.end = fuse_writepage_end;
1833 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1834 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1836 spin_lock(&fi->lock);
1837 list_add(&wpa->writepages_entry, &fi->writepages);
1838 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1839 fuse_flush_writepages(inode);
1840 spin_unlock(&fi->lock);
1842 end_page_writeback(page);
1847 __free_page(tmp_page);
1851 mapping_set_error(page->mapping, error);
1852 end_page_writeback(page);
1856 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1860 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1862 * ->writepages() should be called for sync() and friends. We
1863 * should only get here on direct reclaim and then we are
1864 * allowed to skip a page which is already in flight
1866 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1868 redirty_page_for_writepage(wbc, page);
1873 err = fuse_writepage_locked(page);
1879 struct fuse_fill_wb_data {
1880 struct fuse_writepage_args *wpa;
1881 struct fuse_file *ff;
1882 struct inode *inode;
1883 struct page **orig_pages;
1884 unsigned int max_pages;
1887 static bool fuse_pages_realloc(struct fuse_fill_wb_data *data)
1889 struct fuse_args_pages *ap = &data->wpa->ia.ap;
1890 struct fuse_conn *fc = get_fuse_conn(data->inode);
1891 struct page **pages;
1892 struct fuse_page_desc *descs;
1893 unsigned int npages = min_t(unsigned int,
1894 max_t(unsigned int, data->max_pages * 2,
1895 FUSE_DEFAULT_MAX_PAGES_PER_REQ),
1897 WARN_ON(npages <= data->max_pages);
1899 pages = fuse_pages_alloc(npages, GFP_NOFS, &descs);
1903 memcpy(pages, ap->pages, sizeof(struct page *) * ap->num_pages);
1904 memcpy(descs, ap->descs, sizeof(struct fuse_page_desc) * ap->num_pages);
1908 data->max_pages = npages;
1913 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1915 struct fuse_writepage_args *wpa = data->wpa;
1916 struct inode *inode = data->inode;
1917 struct fuse_inode *fi = get_fuse_inode(inode);
1918 int num_pages = wpa->ia.ap.num_pages;
1921 wpa->ia.ff = fuse_file_get(data->ff);
1922 spin_lock(&fi->lock);
1923 list_add_tail(&wpa->queue_entry, &fi->queued_writes);
1924 fuse_flush_writepages(inode);
1925 spin_unlock(&fi->lock);
1927 for (i = 0; i < num_pages; i++)
1928 end_page_writeback(data->orig_pages[i]);
1932 * First recheck under fi->lock if the offending offset is still under
1933 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1934 * one already added for a page at this offset. If there's none, then insert
1935 * this new request onto the auxiliary list, otherwise reuse the existing one by
1936 * copying the new page contents over to the old temporary page.
1938 static bool fuse_writepage_in_flight(struct fuse_writepage_args *new_wpa,
1941 struct fuse_inode *fi = get_fuse_inode(new_wpa->inode);
1942 struct fuse_writepage_args *tmp;
1943 struct fuse_writepage_args *old_wpa;
1944 struct fuse_args_pages *new_ap = &new_wpa->ia.ap;
1946 WARN_ON(new_ap->num_pages != 0);
1948 spin_lock(&fi->lock);
1949 list_del(&new_wpa->writepages_entry);
1950 old_wpa = fuse_find_writeback(fi, page->index, page->index);
1952 list_add(&new_wpa->writepages_entry, &fi->writepages);
1953 spin_unlock(&fi->lock);
1957 new_ap->num_pages = 1;
1958 for (tmp = old_wpa->next; tmp; tmp = tmp->next) {
1961 WARN_ON(tmp->inode != new_wpa->inode);
1962 curr_index = tmp->ia.write.in.offset >> PAGE_SHIFT;
1963 if (curr_index == page->index) {
1964 WARN_ON(tmp->ia.ap.num_pages != 1);
1965 swap(tmp->ia.ap.pages[0], new_ap->pages[0]);
1971 new_wpa->next = old_wpa->next;
1972 old_wpa->next = new_wpa;
1975 spin_unlock(&fi->lock);
1978 struct backing_dev_info *bdi = inode_to_bdi(new_wpa->inode);
1980 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1981 dec_node_page_state(new_ap->pages[0], NR_WRITEBACK_TEMP);
1982 wb_writeout_inc(&bdi->wb);
1983 fuse_writepage_free(new_wpa);
1989 static int fuse_writepages_fill(struct page *page,
1990 struct writeback_control *wbc, void *_data)
1992 struct fuse_fill_wb_data *data = _data;
1993 struct fuse_writepage_args *wpa = data->wpa;
1994 struct fuse_args_pages *ap = &wpa->ia.ap;
1995 struct inode *inode = data->inode;
1996 struct fuse_inode *fi = get_fuse_inode(inode);
1997 struct fuse_conn *fc = get_fuse_conn(inode);
1998 struct page *tmp_page;
2004 data->ff = fuse_write_file_get(fc, fi);
2010 * Being under writeback is unlikely but possible. For example direct
2011 * read to an mmaped fuse file will set the page dirty twice; once when
2012 * the pages are faulted with get_user_pages(), and then after the read
2015 is_writeback = fuse_page_is_writeback(inode, page->index);
2017 if (wpa && ap->num_pages &&
2018 (is_writeback || ap->num_pages == fc->max_pages ||
2019 (ap->num_pages + 1) * PAGE_SIZE > fc->max_write ||
2020 data->orig_pages[ap->num_pages - 1]->index + 1 != page->index)) {
2021 fuse_writepages_send(data);
2023 } else if (wpa && ap->num_pages == data->max_pages) {
2024 if (!fuse_pages_realloc(data)) {
2025 fuse_writepages_send(data);
2031 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
2036 * The page must not be redirtied until the writeout is completed
2037 * (i.e. userspace has sent a reply to the write request). Otherwise
2038 * there could be more than one temporary page instance for each real
2041 * This is ensured by holding the page lock in page_mkwrite() while
2042 * checking fuse_page_is_writeback(). We already hold the page lock
2043 * since clear_page_dirty_for_io() and keep it held until we add the
2044 * request to the fi->writepages list and increment ap->num_pages.
2045 * After this fuse_page_is_writeback() will indicate that the page is
2046 * under writeback, so we can release the page lock.
2048 if (data->wpa == NULL) {
2050 wpa = fuse_writepage_args_alloc();
2052 __free_page(tmp_page);
2055 data->max_pages = 1;
2058 fuse_write_args_fill(&wpa->ia, data->ff, page_offset(page), 0);
2059 wpa->ia.write.in.write_flags |= FUSE_WRITE_CACHE;
2061 ap->args.in_pages = true;
2062 ap->args.end = fuse_writepage_end;
2066 spin_lock(&fi->lock);
2067 list_add(&wpa->writepages_entry, &fi->writepages);
2068 spin_unlock(&fi->lock);
2072 set_page_writeback(page);
2074 copy_highpage(tmp_page, page);
2075 ap->pages[ap->num_pages] = tmp_page;
2076 ap->descs[ap->num_pages].offset = 0;
2077 ap->descs[ap->num_pages].length = PAGE_SIZE;
2079 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
2080 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
2083 if (is_writeback && fuse_writepage_in_flight(wpa, page)) {
2084 end_page_writeback(page);
2088 data->orig_pages[ap->num_pages] = page;
2091 * Protected by fi->lock against concurrent access by
2092 * fuse_page_is_writeback().
2094 spin_lock(&fi->lock);
2096 spin_unlock(&fi->lock);
2104 static int fuse_writepages(struct address_space *mapping,
2105 struct writeback_control *wbc)
2107 struct inode *inode = mapping->host;
2108 struct fuse_conn *fc = get_fuse_conn(inode);
2109 struct fuse_fill_wb_data data;
2113 if (is_bad_inode(inode))
2121 data.orig_pages = kcalloc(fc->max_pages,
2122 sizeof(struct page *),
2124 if (!data.orig_pages)
2127 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
2129 /* Ignore errors if we can write at least one page */
2130 WARN_ON(!data.wpa->ia.ap.num_pages);
2131 fuse_writepages_send(&data);
2135 fuse_file_put(data.ff, false, false);
2137 kfree(data.orig_pages);
2143 * It's worthy to make sure that space is reserved on disk for the write,
2144 * but how to implement it without killing performance need more thinking.
2146 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2147 loff_t pos, unsigned len, unsigned flags,
2148 struct page **pagep, void **fsdata)
2150 pgoff_t index = pos >> PAGE_SHIFT;
2151 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
2156 WARN_ON(!fc->writeback_cache);
2158 page = grab_cache_page_write_begin(mapping, index, flags);
2162 fuse_wait_on_page_writeback(mapping->host, page->index);
2164 if (PageUptodate(page) || len == PAGE_SIZE)
2167 * Check if the start this page comes after the end of file, in which
2168 * case the readpage can be optimized away.
2170 fsize = i_size_read(mapping->host);
2171 if (fsize <= (pos & PAGE_MASK)) {
2172 size_t off = pos & ~PAGE_MASK;
2174 zero_user_segment(page, 0, off);
2177 err = fuse_do_readpage(file, page);
2191 static int fuse_write_end(struct file *file, struct address_space *mapping,
2192 loff_t pos, unsigned len, unsigned copied,
2193 struct page *page, void *fsdata)
2195 struct inode *inode = page->mapping->host;
2197 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2201 if (!PageUptodate(page)) {
2202 /* Zero any unwritten bytes at the end of the page */
2203 size_t endoff = (pos + copied) & ~PAGE_MASK;
2205 zero_user_segment(page, endoff, PAGE_SIZE);
2206 SetPageUptodate(page);
2209 fuse_write_update_size(inode, pos + copied);
2210 set_page_dirty(page);
2219 static int fuse_launder_page(struct page *page)
2222 if (clear_page_dirty_for_io(page)) {
2223 struct inode *inode = page->mapping->host;
2224 err = fuse_writepage_locked(page);
2226 fuse_wait_on_page_writeback(inode, page->index);
2232 * Write back dirty pages now, because there may not be any suitable
2235 static void fuse_vma_close(struct vm_area_struct *vma)
2237 filemap_write_and_wait(vma->vm_file->f_mapping);
2241 * Wait for writeback against this page to complete before allowing it
2242 * to be marked dirty again, and hence written back again, possibly
2243 * before the previous writepage completed.
2245 * Block here, instead of in ->writepage(), so that the userspace fs
2246 * can only block processes actually operating on the filesystem.
2248 * Otherwise unprivileged userspace fs would be able to block
2253 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2255 static vm_fault_t fuse_page_mkwrite(struct vm_fault *vmf)
2257 struct page *page = vmf->page;
2258 struct inode *inode = file_inode(vmf->vma->vm_file);
2260 file_update_time(vmf->vma->vm_file);
2262 if (page->mapping != inode->i_mapping) {
2264 return VM_FAULT_NOPAGE;
2267 fuse_wait_on_page_writeback(inode, page->index);
2268 return VM_FAULT_LOCKED;
2271 static const struct vm_operations_struct fuse_file_vm_ops = {
2272 .close = fuse_vma_close,
2273 .fault = filemap_fault,
2274 .map_pages = filemap_map_pages,
2275 .page_mkwrite = fuse_page_mkwrite,
2278 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2280 struct fuse_file *ff = file->private_data;
2282 if (ff->open_flags & FOPEN_DIRECT_IO) {
2283 /* Can't provide the coherency needed for MAP_SHARED */
2284 if (vma->vm_flags & VM_MAYSHARE)
2287 invalidate_inode_pages2(file->f_mapping);
2289 return generic_file_mmap(file, vma);
2292 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2293 fuse_link_write_file(file);
2295 file_accessed(file);
2296 vma->vm_ops = &fuse_file_vm_ops;
2300 static int convert_fuse_file_lock(struct fuse_conn *fc,
2301 const struct fuse_file_lock *ffl,
2302 struct file_lock *fl)
2304 switch (ffl->type) {
2310 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2311 ffl->end < ffl->start)
2314 fl->fl_start = ffl->start;
2315 fl->fl_end = ffl->end;
2318 * Convert pid into init's pid namespace. The locks API will
2319 * translate it into the caller's pid namespace.
2322 fl->fl_pid = pid_nr_ns(find_pid_ns(ffl->pid, fc->pid_ns), &init_pid_ns);
2329 fl->fl_type = ffl->type;
2333 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2334 const struct file_lock *fl, int opcode, pid_t pid,
2335 int flock, struct fuse_lk_in *inarg)
2337 struct inode *inode = file_inode(file);
2338 struct fuse_conn *fc = get_fuse_conn(inode);
2339 struct fuse_file *ff = file->private_data;
2341 memset(inarg, 0, sizeof(*inarg));
2343 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2344 inarg->lk.start = fl->fl_start;
2345 inarg->lk.end = fl->fl_end;
2346 inarg->lk.type = fl->fl_type;
2347 inarg->lk.pid = pid;
2349 inarg->lk_flags |= FUSE_LK_FLOCK;
2350 args->opcode = opcode;
2351 args->nodeid = get_node_id(inode);
2352 args->in_numargs = 1;
2353 args->in_args[0].size = sizeof(*inarg);
2354 args->in_args[0].value = inarg;
2357 static int fuse_getlk(struct file *file, struct file_lock *fl)
2359 struct inode *inode = file_inode(file);
2360 struct fuse_conn *fc = get_fuse_conn(inode);
2362 struct fuse_lk_in inarg;
2363 struct fuse_lk_out outarg;
2366 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2367 args.out_numargs = 1;
2368 args.out_args[0].size = sizeof(outarg);
2369 args.out_args[0].value = &outarg;
2370 err = fuse_simple_request(fc, &args);
2372 err = convert_fuse_file_lock(fc, &outarg.lk, fl);
2377 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2379 struct inode *inode = file_inode(file);
2380 struct fuse_conn *fc = get_fuse_conn(inode);
2382 struct fuse_lk_in inarg;
2383 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2384 struct pid *pid = fl->fl_type != F_UNLCK ? task_tgid(current) : NULL;
2385 pid_t pid_nr = pid_nr_ns(pid, fc->pid_ns);
2388 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2389 /* NLM needs asynchronous locks, which we don't support yet */
2393 /* Unlock on close is handled by the flush method */
2394 if ((fl->fl_flags & FL_CLOSE_POSIX) == FL_CLOSE_POSIX)
2397 fuse_lk_fill(&args, file, fl, opcode, pid_nr, flock, &inarg);
2398 err = fuse_simple_request(fc, &args);
2400 /* locking is restartable */
2407 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2409 struct inode *inode = file_inode(file);
2410 struct fuse_conn *fc = get_fuse_conn(inode);
2413 if (cmd == F_CANCELLK) {
2415 } else if (cmd == F_GETLK) {
2417 posix_test_lock(file, fl);
2420 err = fuse_getlk(file, fl);
2423 err = posix_lock_file(file, fl, NULL);
2425 err = fuse_setlk(file, fl, 0);
2430 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2432 struct inode *inode = file_inode(file);
2433 struct fuse_conn *fc = get_fuse_conn(inode);
2437 err = locks_lock_file_wait(file, fl);
2439 struct fuse_file *ff = file->private_data;
2441 /* emulate flock with POSIX locks */
2443 err = fuse_setlk(file, fl, 1);
2449 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2451 struct inode *inode = mapping->host;
2452 struct fuse_conn *fc = get_fuse_conn(inode);
2454 struct fuse_bmap_in inarg;
2455 struct fuse_bmap_out outarg;
2458 if (!inode->i_sb->s_bdev || fc->no_bmap)
2461 memset(&inarg, 0, sizeof(inarg));
2462 inarg.block = block;
2463 inarg.blocksize = inode->i_sb->s_blocksize;
2464 args.opcode = FUSE_BMAP;
2465 args.nodeid = get_node_id(inode);
2466 args.in_numargs = 1;
2467 args.in_args[0].size = sizeof(inarg);
2468 args.in_args[0].value = &inarg;
2469 args.out_numargs = 1;
2470 args.out_args[0].size = sizeof(outarg);
2471 args.out_args[0].value = &outarg;
2472 err = fuse_simple_request(fc, &args);
2476 return err ? 0 : outarg.block;
2479 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2481 struct inode *inode = file->f_mapping->host;
2482 struct fuse_conn *fc = get_fuse_conn(inode);
2483 struct fuse_file *ff = file->private_data;
2485 struct fuse_lseek_in inarg = {
2490 struct fuse_lseek_out outarg;
2496 args.opcode = FUSE_LSEEK;
2497 args.nodeid = ff->nodeid;
2498 args.in_numargs = 1;
2499 args.in_args[0].size = sizeof(inarg);
2500 args.in_args[0].value = &inarg;
2501 args.out_numargs = 1;
2502 args.out_args[0].size = sizeof(outarg);
2503 args.out_args[0].value = &outarg;
2504 err = fuse_simple_request(fc, &args);
2506 if (err == -ENOSYS) {
2513 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2516 err = fuse_update_attributes(inode, file);
2518 return generic_file_llseek(file, offset, whence);
2523 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2526 struct inode *inode = file_inode(file);
2531 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2532 retval = generic_file_llseek(file, offset, whence);
2536 retval = fuse_update_attributes(inode, file);
2538 retval = generic_file_llseek(file, offset, whence);
2539 inode_unlock(inode);
2544 retval = fuse_lseek(file, offset, whence);
2545 inode_unlock(inode);
2555 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2556 * ABI was defined to be 'struct iovec' which is different on 32bit
2557 * and 64bit. Fortunately we can determine which structure the server
2558 * used from the size of the reply.
2560 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2561 size_t transferred, unsigned count,
2564 #ifdef CONFIG_COMPAT
2565 if (count * sizeof(struct compat_iovec) == transferred) {
2566 struct compat_iovec *ciov = src;
2570 * With this interface a 32bit server cannot support
2571 * non-compat (i.e. ones coming from 64bit apps) ioctl
2577 for (i = 0; i < count; i++) {
2578 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2579 dst[i].iov_len = ciov[i].iov_len;
2585 if (count * sizeof(struct iovec) != transferred)
2588 memcpy(dst, src, transferred);
2592 /* Make sure iov_length() won't overflow */
2593 static int fuse_verify_ioctl_iov(struct fuse_conn *fc, struct iovec *iov,
2597 u32 max = fc->max_pages << PAGE_SHIFT;
2599 for (n = 0; n < count; n++, iov++) {
2600 if (iov->iov_len > (size_t) max)
2602 max -= iov->iov_len;
2607 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2608 void *src, size_t transferred, unsigned count,
2612 struct fuse_ioctl_iovec *fiov = src;
2614 if (fc->minor < 16) {
2615 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2619 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2622 for (i = 0; i < count; i++) {
2623 /* Did the server supply an inappropriate value? */
2624 if (fiov[i].base != (unsigned long) fiov[i].base ||
2625 fiov[i].len != (unsigned long) fiov[i].len)
2628 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2629 dst[i].iov_len = (size_t) fiov[i].len;
2631 #ifdef CONFIG_COMPAT
2633 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2634 (compat_size_t) dst[i].iov_len != fiov[i].len))
2644 * For ioctls, there is no generic way to determine how much memory
2645 * needs to be read and/or written. Furthermore, ioctls are allowed
2646 * to dereference the passed pointer, so the parameter requires deep
2647 * copying but FUSE has no idea whatsoever about what to copy in or
2650 * This is solved by allowing FUSE server to retry ioctl with
2651 * necessary in/out iovecs. Let's assume the ioctl implementation
2652 * needs to read in the following structure.
2659 * On the first callout to FUSE server, inarg->in_size and
2660 * inarg->out_size will be NULL; then, the server completes the ioctl
2661 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2662 * the actual iov array to
2664 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2666 * which tells FUSE to copy in the requested area and retry the ioctl.
2667 * On the second round, the server has access to the structure and
2668 * from that it can tell what to look for next, so on the invocation,
2669 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2671 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2672 * { .iov_base = a.buf, .iov_len = a.buflen } }
2674 * FUSE will copy both struct a and the pointed buffer from the
2675 * process doing the ioctl and retry ioctl with both struct a and the
2678 * This time, FUSE server has everything it needs and completes ioctl
2679 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2681 * Copying data out works the same way.
2683 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2684 * automatically initializes in and out iovs by decoding @cmd with
2685 * _IOC_* macros and the server is not allowed to request RETRY. This
2686 * limits ioctl data transfers to well-formed ioctls and is the forced
2687 * behavior for all FUSE servers.
2689 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2692 struct fuse_file *ff = file->private_data;
2693 struct fuse_conn *fc = ff->fc;
2694 struct fuse_ioctl_in inarg = {
2700 struct fuse_ioctl_out outarg;
2701 struct iovec *iov_page = NULL;
2702 struct iovec *in_iov = NULL, *out_iov = NULL;
2703 unsigned int in_iovs = 0, out_iovs = 0, max_pages;
2704 size_t in_size, out_size, c;
2705 ssize_t transferred;
2708 struct fuse_args_pages ap = {};
2710 #if BITS_PER_LONG == 32
2711 inarg.flags |= FUSE_IOCTL_32BIT;
2713 if (flags & FUSE_IOCTL_COMPAT) {
2714 inarg.flags |= FUSE_IOCTL_32BIT;
2715 #ifdef CONFIG_X86_X32
2716 if (in_x32_syscall())
2717 inarg.flags |= FUSE_IOCTL_COMPAT_X32;
2722 /* assume all the iovs returned by client always fits in a page */
2723 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2726 ap.pages = fuse_pages_alloc(fc->max_pages, GFP_KERNEL, &ap.descs);
2727 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2728 if (!ap.pages || !iov_page)
2731 fuse_page_descs_length_init(ap.descs, 0, fc->max_pages);
2734 * If restricted, initialize IO parameters as encoded in @cmd.
2735 * RETRY from server is not allowed.
2737 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2738 struct iovec *iov = iov_page;
2740 iov->iov_base = (void __user *)arg;
2741 iov->iov_len = _IOC_SIZE(cmd);
2743 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2748 if (_IOC_DIR(cmd) & _IOC_READ) {
2755 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2756 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2759 * Out data can be used either for actual out data or iovs,
2760 * make sure there always is at least one page.
2762 out_size = max_t(size_t, out_size, PAGE_SIZE);
2763 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2765 /* make sure there are enough buffer pages and init request with them */
2767 if (max_pages > fc->max_pages)
2769 while (ap.num_pages < max_pages) {
2770 ap.pages[ap.num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2771 if (!ap.pages[ap.num_pages])
2777 /* okay, let's send it to the client */
2778 ap.args.opcode = FUSE_IOCTL;
2779 ap.args.nodeid = ff->nodeid;
2780 ap.args.in_numargs = 1;
2781 ap.args.in_args[0].size = sizeof(inarg);
2782 ap.args.in_args[0].value = &inarg;
2784 ap.args.in_numargs++;
2785 ap.args.in_args[1].size = in_size;
2786 ap.args.in_pages = true;
2789 iov_iter_init(&ii, WRITE, in_iov, in_iovs, in_size);
2790 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2791 c = copy_page_from_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2792 if (c != PAGE_SIZE && iov_iter_count(&ii))
2797 ap.args.out_numargs = 2;
2798 ap.args.out_args[0].size = sizeof(outarg);
2799 ap.args.out_args[0].value = &outarg;
2800 ap.args.out_args[1].size = out_size;
2801 ap.args.out_pages = true;
2802 ap.args.out_argvar = true;
2804 transferred = fuse_simple_request(fc, &ap.args);
2806 if (transferred < 0)
2809 /* did it ask for retry? */
2810 if (outarg.flags & FUSE_IOCTL_RETRY) {
2813 /* no retry if in restricted mode */
2815 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2818 in_iovs = outarg.in_iovs;
2819 out_iovs = outarg.out_iovs;
2822 * Make sure things are in boundary, separate checks
2823 * are to protect against overflow.
2826 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2827 out_iovs > FUSE_IOCTL_MAX_IOV ||
2828 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2831 vaddr = kmap_atomic(ap.pages[0]);
2832 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2833 transferred, in_iovs + out_iovs,
2834 (flags & FUSE_IOCTL_COMPAT) != 0);
2835 kunmap_atomic(vaddr);
2840 out_iov = in_iov + in_iovs;
2842 err = fuse_verify_ioctl_iov(fc, in_iov, in_iovs);
2846 err = fuse_verify_ioctl_iov(fc, out_iov, out_iovs);
2854 if (transferred > inarg.out_size)
2858 iov_iter_init(&ii, READ, out_iov, out_iovs, transferred);
2859 for (i = 0; iov_iter_count(&ii) && !WARN_ON(i >= ap.num_pages); i++) {
2860 c = copy_page_to_iter(ap.pages[i], 0, PAGE_SIZE, &ii);
2861 if (c != PAGE_SIZE && iov_iter_count(&ii))
2866 free_page((unsigned long) iov_page);
2867 while (ap.num_pages)
2868 __free_page(ap.pages[--ap.num_pages]);
2871 return err ? err : outarg.result;
2873 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2875 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2876 unsigned long arg, unsigned int flags)
2878 struct inode *inode = file_inode(file);
2879 struct fuse_conn *fc = get_fuse_conn(inode);
2881 if (!fuse_allow_current_process(fc))
2884 if (is_bad_inode(inode))
2887 return fuse_do_ioctl(file, cmd, arg, flags);
2890 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2893 return fuse_ioctl_common(file, cmd, arg, 0);
2896 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2899 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2903 * All files which have been polled are linked to RB tree
2904 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2905 * find the matching one.
2907 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2908 struct rb_node **parent_out)
2910 struct rb_node **link = &fc->polled_files.rb_node;
2911 struct rb_node *last = NULL;
2914 struct fuse_file *ff;
2917 ff = rb_entry(last, struct fuse_file, polled_node);
2920 link = &last->rb_left;
2921 else if (kh > ff->kh)
2922 link = &last->rb_right;
2933 * The file is about to be polled. Make sure it's on the polled_files
2934 * RB tree. Note that files once added to the polled_files tree are
2935 * not removed before the file is released. This is because a file
2936 * polled once is likely to be polled again.
2938 static void fuse_register_polled_file(struct fuse_conn *fc,
2939 struct fuse_file *ff)
2941 spin_lock(&fc->lock);
2942 if (RB_EMPTY_NODE(&ff->polled_node)) {
2943 struct rb_node **link, *uninitialized_var(parent);
2945 link = fuse_find_polled_node(fc, ff->kh, &parent);
2947 rb_link_node(&ff->polled_node, parent, link);
2948 rb_insert_color(&ff->polled_node, &fc->polled_files);
2950 spin_unlock(&fc->lock);
2953 __poll_t fuse_file_poll(struct file *file, poll_table *wait)
2955 struct fuse_file *ff = file->private_data;
2956 struct fuse_conn *fc = ff->fc;
2957 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2958 struct fuse_poll_out outarg;
2963 return DEFAULT_POLLMASK;
2965 poll_wait(file, &ff->poll_wait, wait);
2966 inarg.events = mangle_poll(poll_requested_events(wait));
2969 * Ask for notification iff there's someone waiting for it.
2970 * The client may ignore the flag and always notify.
2972 if (waitqueue_active(&ff->poll_wait)) {
2973 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2974 fuse_register_polled_file(fc, ff);
2977 args.opcode = FUSE_POLL;
2978 args.nodeid = ff->nodeid;
2979 args.in_numargs = 1;
2980 args.in_args[0].size = sizeof(inarg);
2981 args.in_args[0].value = &inarg;
2982 args.out_numargs = 1;
2983 args.out_args[0].size = sizeof(outarg);
2984 args.out_args[0].value = &outarg;
2985 err = fuse_simple_request(fc, &args);
2988 return demangle_poll(outarg.revents);
2989 if (err == -ENOSYS) {
2991 return DEFAULT_POLLMASK;
2995 EXPORT_SYMBOL_GPL(fuse_file_poll);
2998 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2999 * wakes up the poll waiters.
3001 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
3002 struct fuse_notify_poll_wakeup_out *outarg)
3004 u64 kh = outarg->kh;
3005 struct rb_node **link;
3007 spin_lock(&fc->lock);
3009 link = fuse_find_polled_node(fc, kh, NULL);
3011 struct fuse_file *ff;
3013 ff = rb_entry(*link, struct fuse_file, polled_node);
3014 wake_up_interruptible_sync(&ff->poll_wait);
3017 spin_unlock(&fc->lock);
3021 static void fuse_do_truncate(struct file *file)
3023 struct inode *inode = file->f_mapping->host;
3026 attr.ia_valid = ATTR_SIZE;
3027 attr.ia_size = i_size_read(inode);
3029 attr.ia_file = file;
3030 attr.ia_valid |= ATTR_FILE;
3032 fuse_do_setattr(file_dentry(file), &attr, file);
3035 static inline loff_t fuse_round_up(struct fuse_conn *fc, loff_t off)
3037 return round_up(off, fc->max_pages << PAGE_SHIFT);
3041 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3043 DECLARE_COMPLETION_ONSTACK(wait);
3045 struct file *file = iocb->ki_filp;
3046 struct fuse_file *ff = file->private_data;
3047 bool async_dio = ff->fc->async_dio;
3049 struct inode *inode;
3051 size_t count = iov_iter_count(iter);
3052 loff_t offset = iocb->ki_pos;
3053 struct fuse_io_priv *io;
3056 inode = file->f_mapping->host;
3057 i_size = i_size_read(inode);
3059 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
3062 /* optimization for short read */
3063 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
3064 if (offset >= i_size)
3066 iov_iter_truncate(iter, fuse_round_up(ff->fc, i_size - offset));
3067 count = iov_iter_count(iter);
3070 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
3073 spin_lock_init(&io->lock);
3074 kref_init(&io->refcnt);
3078 io->offset = offset;
3079 io->write = (iov_iter_rw(iter) == WRITE);
3082 * By default, we want to optimize all I/Os with async request
3083 * submission to the client filesystem if supported.
3085 io->async = async_dio;
3087 io->blocking = is_sync_kiocb(iocb);
3090 * We cannot asynchronously extend the size of a file.
3091 * In such case the aio will behave exactly like sync io.
3093 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
3094 io->blocking = true;
3096 if (io->async && io->blocking) {
3098 * Additional reference to keep io around after
3099 * calling fuse_aio_complete()
3101 kref_get(&io->refcnt);
3105 if (iov_iter_rw(iter) == WRITE) {
3106 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
3107 fuse_invalidate_attr(inode);
3109 ret = __fuse_direct_read(io, iter, &pos);
3113 bool blocking = io->blocking;
3115 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
3117 /* we have a non-extending, async request, so return */
3119 return -EIOCBQUEUED;
3121 wait_for_completion(&wait);
3122 ret = fuse_get_res_by_io(io);
3125 kref_put(&io->refcnt, fuse_io_release);
3127 if (iov_iter_rw(iter) == WRITE) {
3129 fuse_write_update_size(inode, pos);
3130 else if (ret < 0 && offset + count > i_size)
3131 fuse_do_truncate(file);
3137 static int fuse_writeback_range(struct inode *inode, loff_t start, loff_t end)
3139 int err = filemap_write_and_wait_range(inode->i_mapping, start, end);
3142 fuse_sync_writes(inode);
3147 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
3150 struct fuse_file *ff = file->private_data;
3151 struct inode *inode = file_inode(file);
3152 struct fuse_inode *fi = get_fuse_inode(inode);
3153 struct fuse_conn *fc = ff->fc;
3155 struct fuse_fallocate_in inarg = {
3162 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
3163 (mode & FALLOC_FL_PUNCH_HOLE);
3165 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
3168 if (fc->no_fallocate)
3173 if (mode & FALLOC_FL_PUNCH_HOLE) {
3174 loff_t endbyte = offset + length - 1;
3176 err = fuse_writeback_range(inode, offset, endbyte);
3182 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
3183 offset + length > i_size_read(inode)) {
3184 err = inode_newsize_ok(inode, offset + length);
3189 if (!(mode & FALLOC_FL_KEEP_SIZE))
3190 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3192 args.opcode = FUSE_FALLOCATE;
3193 args.nodeid = ff->nodeid;
3194 args.in_numargs = 1;
3195 args.in_args[0].size = sizeof(inarg);
3196 args.in_args[0].value = &inarg;
3197 err = fuse_simple_request(fc, &args);
3198 if (err == -ENOSYS) {
3199 fc->no_fallocate = 1;
3205 /* we could have extended the file */
3206 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3207 bool changed = fuse_write_update_size(inode, offset + length);
3209 if (changed && fc->writeback_cache)
3210 file_update_time(file);
3213 if (mode & FALLOC_FL_PUNCH_HOLE)
3214 truncate_pagecache_range(inode, offset, offset + length - 1);
3216 fuse_invalidate_attr(inode);
3219 if (!(mode & FALLOC_FL_KEEP_SIZE))
3220 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3223 inode_unlock(inode);
3228 static ssize_t __fuse_copy_file_range(struct file *file_in, loff_t pos_in,
3229 struct file *file_out, loff_t pos_out,
3230 size_t len, unsigned int flags)
3232 struct fuse_file *ff_in = file_in->private_data;
3233 struct fuse_file *ff_out = file_out->private_data;
3234 struct inode *inode_in = file_inode(file_in);
3235 struct inode *inode_out = file_inode(file_out);
3236 struct fuse_inode *fi_out = get_fuse_inode(inode_out);
3237 struct fuse_conn *fc = ff_in->fc;
3239 struct fuse_copy_file_range_in inarg = {
3242 .nodeid_out = ff_out->nodeid,
3243 .fh_out = ff_out->fh,
3248 struct fuse_write_out outarg;
3250 /* mark unstable when write-back is not used, and file_out gets
3252 bool is_unstable = (!fc->writeback_cache) &&
3253 ((pos_out + len) > inode_out->i_size);
3255 if (fc->no_copy_file_range)
3258 if (file_inode(file_in)->i_sb != file_inode(file_out)->i_sb)
3261 if (fc->writeback_cache) {
3262 inode_lock(inode_in);
3263 err = fuse_writeback_range(inode_in, pos_in, pos_in + len);
3264 inode_unlock(inode_in);
3269 inode_lock(inode_out);
3271 err = file_modified(file_out);
3275 if (fc->writeback_cache) {
3276 err = fuse_writeback_range(inode_out, pos_out, pos_out + len);
3282 set_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3284 args.opcode = FUSE_COPY_FILE_RANGE;
3285 args.nodeid = ff_in->nodeid;
3286 args.in_numargs = 1;
3287 args.in_args[0].size = sizeof(inarg);
3288 args.in_args[0].value = &inarg;
3289 args.out_numargs = 1;
3290 args.out_args[0].size = sizeof(outarg);
3291 args.out_args[0].value = &outarg;
3292 err = fuse_simple_request(fc, &args);
3293 if (err == -ENOSYS) {
3294 fc->no_copy_file_range = 1;
3300 if (fc->writeback_cache) {
3301 fuse_write_update_size(inode_out, pos_out + outarg.size);
3302 file_update_time(file_out);
3305 fuse_invalidate_attr(inode_out);
3310 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi_out->state);
3312 inode_unlock(inode_out);
3313 file_accessed(file_in);
3318 static ssize_t fuse_copy_file_range(struct file *src_file, loff_t src_off,
3319 struct file *dst_file, loff_t dst_off,
3320 size_t len, unsigned int flags)
3324 ret = __fuse_copy_file_range(src_file, src_off, dst_file, dst_off,
3327 if (ret == -EOPNOTSUPP || ret == -EXDEV)
3328 ret = generic_copy_file_range(src_file, src_off, dst_file,
3329 dst_off, len, flags);
3333 static const struct file_operations fuse_file_operations = {
3334 .llseek = fuse_file_llseek,
3335 .read_iter = fuse_file_read_iter,
3336 .write_iter = fuse_file_write_iter,
3337 .mmap = fuse_file_mmap,
3339 .flush = fuse_flush,
3340 .release = fuse_release,
3341 .fsync = fuse_fsync,
3342 .lock = fuse_file_lock,
3343 .flock = fuse_file_flock,
3344 .splice_read = generic_file_splice_read,
3345 .splice_write = iter_file_splice_write,
3346 .unlocked_ioctl = fuse_file_ioctl,
3347 .compat_ioctl = fuse_file_compat_ioctl,
3348 .poll = fuse_file_poll,
3349 .fallocate = fuse_file_fallocate,
3350 .copy_file_range = fuse_copy_file_range,
3353 static const struct address_space_operations fuse_file_aops = {
3354 .readpage = fuse_readpage,
3355 .writepage = fuse_writepage,
3356 .writepages = fuse_writepages,
3357 .launder_page = fuse_launder_page,
3358 .readpages = fuse_readpages,
3359 .set_page_dirty = __set_page_dirty_nobuffers,
3361 .direct_IO = fuse_direct_IO,
3362 .write_begin = fuse_write_begin,
3363 .write_end = fuse_write_end,
3366 void fuse_init_file_inode(struct inode *inode)
3368 struct fuse_inode *fi = get_fuse_inode(inode);
3370 inode->i_fop = &fuse_file_operations;
3371 inode->i_data.a_ops = &fuse_file_aops;
3373 INIT_LIST_HEAD(&fi->write_files);
3374 INIT_LIST_HEAD(&fi->queued_writes);
3376 init_waitqueue_head(&fi->page_waitq);
3377 INIT_LIST_HEAD(&fi->writepages);