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>
21 static const struct file_operations fuse_direct_io_file_operations;
23 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
26 struct fuse_open_in inarg;
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
33 args.in.h.opcode = opcode;
34 args.in.h.nodeid = nodeid;
36 args.in.args[0].size = sizeof(inarg);
37 args.in.args[0].value = &inarg;
39 args.out.args[0].size = sizeof(*outargp);
40 args.out.args[0].value = outargp;
42 return fuse_simple_request(fc, &args);
45 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
49 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
54 ff->reserved_req = fuse_request_alloc(0);
55 if (unlikely(!ff->reserved_req)) {
60 INIT_LIST_HEAD(&ff->write_entry);
61 atomic_set(&ff->count, 0);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
67 spin_unlock(&fc->lock);
72 void fuse_file_free(struct fuse_file *ff)
74 fuse_request_free(ff->reserved_req);
78 struct fuse_file *fuse_file_get(struct fuse_file *ff)
80 atomic_inc(&ff->count);
84 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
86 iput(req->misc.release.inode);
89 static void fuse_file_put(struct fuse_file *ff, bool sync)
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
94 if (ff->fc->no_open) {
96 * Drop the release request when client does not
99 __clear_bit(FR_BACKGROUND, &req->flags);
100 iput(req->misc.release.inode);
101 fuse_put_request(ff->fc, req);
103 __clear_bit(FR_BACKGROUND, &req->flags);
104 fuse_request_send(ff->fc, req);
105 iput(req->misc.release.inode);
106 fuse_put_request(ff->fc, req);
108 req->end = fuse_release_end;
109 __set_bit(FR_BACKGROUND, &req->flags);
110 fuse_request_send_background(ff->fc, req);
116 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
119 struct fuse_file *ff;
120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
122 ff = fuse_file_alloc(fc);
127 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
128 if (!fc->no_open || isdir) {
129 struct fuse_open_out outarg;
132 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
135 ff->open_flags = outarg.open_flags;
137 } else if (err != -ENOSYS || isdir) {
146 ff->open_flags &= ~FOPEN_DIRECT_IO;
149 file->private_data = fuse_file_get(ff);
153 EXPORT_SYMBOL_GPL(fuse_do_open);
155 static void fuse_link_write_file(struct file *file)
157 struct inode *inode = file_inode(file);
158 struct fuse_conn *fc = get_fuse_conn(inode);
159 struct fuse_inode *fi = get_fuse_inode(inode);
160 struct fuse_file *ff = file->private_data;
162 * file may be written through mmap, so chain it onto the
163 * inodes's write_file list
165 spin_lock(&fc->lock);
166 if (list_empty(&ff->write_entry))
167 list_add(&ff->write_entry, &fi->write_files);
168 spin_unlock(&fc->lock);
171 void fuse_finish_open(struct inode *inode, struct file *file)
173 struct fuse_file *ff = file->private_data;
174 struct fuse_conn *fc = get_fuse_conn(inode);
176 if (ff->open_flags & FOPEN_DIRECT_IO)
177 file->f_op = &fuse_direct_io_file_operations;
178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
179 invalidate_inode_pages2(inode->i_mapping);
180 if (ff->open_flags & FOPEN_NONSEEKABLE)
181 nonseekable_open(inode, file);
182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
190 if (fc->writeback_cache)
191 file_update_time(file);
193 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
194 fuse_link_write_file(file);
197 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
199 struct fuse_conn *fc = get_fuse_conn(inode);
201 bool lock_inode = (file->f_flags & O_TRUNC) &&
202 fc->atomic_o_trunc &&
205 err = generic_file_open(inode, file);
212 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
215 fuse_finish_open(inode, file);
223 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
225 struct fuse_conn *fc = ff->fc;
226 struct fuse_req *req = ff->reserved_req;
227 struct fuse_release_in *inarg = &req->misc.release.in;
229 spin_lock(&fc->lock);
230 list_del(&ff->write_entry);
231 if (!RB_EMPTY_NODE(&ff->polled_node))
232 rb_erase(&ff->polled_node, &fc->polled_files);
233 spin_unlock(&fc->lock);
235 wake_up_interruptible_all(&ff->poll_wait);
238 inarg->flags = flags;
239 req->in.h.opcode = opcode;
240 req->in.h.nodeid = ff->nodeid;
242 req->in.args[0].size = sizeof(struct fuse_release_in);
243 req->in.args[0].value = inarg;
246 void fuse_release_common(struct file *file, int opcode)
248 struct fuse_file *ff;
249 struct fuse_req *req;
251 ff = file->private_data;
255 req = ff->reserved_req;
256 fuse_prepare_release(ff, file->f_flags, opcode);
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
276 fuse_file_put(ff, ff->fc->destroy_req != NULL);
279 static int fuse_open(struct inode *inode, struct file *file)
281 return fuse_open_common(inode, file, false);
284 static int fuse_release(struct inode *inode, struct file *file)
286 struct fuse_conn *fc = get_fuse_conn(inode);
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
290 write_inode_now(inode, 1);
292 fuse_release_common(file, FUSE_RELEASE);
294 /* return value is ignored by VFS */
298 void fuse_sync_release(struct fuse_file *ff, int flags)
300 WARN_ON(atomic_read(&ff->count) > 1);
301 fuse_prepare_release(ff, flags, FUSE_RELEASE);
302 __set_bit(FR_FORCE, &ff->reserved_req->flags);
303 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
304 fuse_request_send(ff->fc, ff->reserved_req);
305 fuse_put_request(ff->fc, ff->reserved_req);
308 EXPORT_SYMBOL_GPL(fuse_sync_release);
311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
314 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
329 return (u64) v0 + ((u64) v1 << 32);
333 * Check if any page in a range is under writeback
335 * This is currently done by walking the list of writepage requests
336 * for the inode, which can be pretty inefficient.
338 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
341 struct fuse_conn *fc = get_fuse_conn(inode);
342 struct fuse_inode *fi = get_fuse_inode(inode);
343 struct fuse_req *req;
346 spin_lock(&fc->lock);
347 list_for_each_entry(req, &fi->writepages, writepages_entry) {
350 BUG_ON(req->inode != inode);
351 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
352 if (idx_from < curr_index + req->num_pages &&
353 curr_index <= idx_to) {
358 spin_unlock(&fc->lock);
363 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
365 return fuse_range_is_writeback(inode, index, index);
369 * Wait for page writeback to be completed.
371 * Since fuse doesn't rely on the VM writeback tracking, this has to
372 * use some other means.
374 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
376 struct fuse_inode *fi = get_fuse_inode(inode);
378 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
383 * Wait for all pending writepages on the inode to finish.
385 * This is currently done by blocking further writes with FUSE_NOWRITE
386 * and waiting for all sent writes to complete.
388 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
389 * could conflict with truncation.
391 static void fuse_sync_writes(struct inode *inode)
393 fuse_set_nowrite(inode);
394 fuse_release_nowrite(inode);
397 static int fuse_flush(struct file *file, fl_owner_t id)
399 struct inode *inode = file_inode(file);
400 struct fuse_conn *fc = get_fuse_conn(inode);
401 struct fuse_file *ff = file->private_data;
402 struct fuse_req *req;
403 struct fuse_flush_in inarg;
406 if (is_bad_inode(inode))
412 err = write_inode_now(inode, 1);
417 fuse_sync_writes(inode);
420 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
421 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
423 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
424 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
429 req = fuse_get_req_nofail_nopages(fc, file);
430 memset(&inarg, 0, sizeof(inarg));
432 inarg.lock_owner = fuse_lock_owner_id(fc, id);
433 req->in.h.opcode = FUSE_FLUSH;
434 req->in.h.nodeid = get_node_id(inode);
436 req->in.args[0].size = sizeof(inarg);
437 req->in.args[0].value = &inarg;
438 __set_bit(FR_FORCE, &req->flags);
439 fuse_request_send(fc, req);
440 err = req->out.h.error;
441 fuse_put_request(fc, req);
442 if (err == -ENOSYS) {
449 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
450 int datasync, int isdir)
452 struct inode *inode = file->f_mapping->host;
453 struct fuse_conn *fc = get_fuse_conn(inode);
454 struct fuse_file *ff = file->private_data;
456 struct fuse_fsync_in inarg;
459 if (is_bad_inode(inode))
465 * Start writeback against all dirty pages of the inode, then
466 * wait for all outstanding writes, before sending the FSYNC
469 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
473 fuse_sync_writes(inode);
476 * Due to implementation of fuse writeback
477 * filemap_write_and_wait_range() does not catch errors.
478 * We have to do this directly after fuse_sync_writes()
480 if (test_bit(AS_ENOSPC, &file->f_mapping->flags) &&
481 test_and_clear_bit(AS_ENOSPC, &file->f_mapping->flags))
483 if (test_bit(AS_EIO, &file->f_mapping->flags) &&
484 test_and_clear_bit(AS_EIO, &file->f_mapping->flags))
489 err = sync_inode_metadata(inode, 1);
493 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
496 memset(&inarg, 0, sizeof(inarg));
498 inarg.fsync_flags = datasync ? 1 : 0;
499 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
500 args.in.h.nodeid = get_node_id(inode);
502 args.in.args[0].size = sizeof(inarg);
503 args.in.args[0].value = &inarg;
504 err = fuse_simple_request(fc, &args);
505 if (err == -ENOSYS) {
517 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
520 return fuse_fsync_common(file, start, end, datasync, 0);
523 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
524 size_t count, int opcode)
526 struct fuse_read_in *inarg = &req->misc.read.in;
527 struct fuse_file *ff = file->private_data;
532 inarg->flags = file->f_flags;
533 req->in.h.opcode = opcode;
534 req->in.h.nodeid = ff->nodeid;
536 req->in.args[0].size = sizeof(struct fuse_read_in);
537 req->in.args[0].value = inarg;
539 req->out.numargs = 1;
540 req->out.args[0].size = count;
543 static void fuse_release_user_pages(struct fuse_req *req, int write)
547 for (i = 0; i < req->num_pages; i++) {
548 struct page *page = req->pages[i];
550 set_page_dirty_lock(page);
555 static void fuse_io_release(struct kref *kref)
557 kfree(container_of(kref, struct fuse_io_priv, refcnt));
560 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
565 if (io->bytes >= 0 && io->write)
568 return io->bytes < 0 ? io->size : io->bytes;
572 * In case of short read, the caller sets 'pos' to the position of
573 * actual end of fuse request in IO request. Otherwise, if bytes_requested
574 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
577 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
578 * both submitted asynchronously. The first of them was ACKed by userspace as
579 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
580 * second request was ACKed as short, e.g. only 1K was read, resulting in
583 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
584 * will be equal to the length of the longest contiguous fragment of
585 * transferred data starting from the beginning of IO request.
587 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
591 spin_lock(&io->lock);
593 io->err = io->err ? : err;
594 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
598 if (!left && io->blocking)
600 spin_unlock(&io->lock);
602 if (!left && !io->blocking) {
603 ssize_t res = fuse_get_res_by_io(io);
606 struct inode *inode = file_inode(io->iocb->ki_filp);
607 struct fuse_conn *fc = get_fuse_conn(inode);
608 struct fuse_inode *fi = get_fuse_inode(inode);
610 spin_lock(&fc->lock);
611 fi->attr_version = ++fc->attr_version;
612 spin_unlock(&fc->lock);
615 io->iocb->ki_complete(io->iocb, res, 0);
618 kref_put(&io->refcnt, fuse_io_release);
621 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
623 struct fuse_io_priv *io = req->io;
626 fuse_release_user_pages(req, !io->write);
629 if (req->misc.write.in.size != req->misc.write.out.size)
630 pos = req->misc.write.in.offset - io->offset +
631 req->misc.write.out.size;
633 if (req->misc.read.in.size != req->out.args[0].size)
634 pos = req->misc.read.in.offset - io->offset +
635 req->out.args[0].size;
638 fuse_aio_complete(io, req->out.h.error, pos);
641 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
642 size_t num_bytes, struct fuse_io_priv *io)
644 spin_lock(&io->lock);
645 kref_get(&io->refcnt);
646 io->size += num_bytes;
648 spin_unlock(&io->lock);
651 req->end = fuse_aio_complete_req;
653 __fuse_get_request(req);
654 fuse_request_send_background(fc, req);
659 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
660 loff_t pos, size_t count, fl_owner_t owner)
662 struct file *file = io->file;
663 struct fuse_file *ff = file->private_data;
664 struct fuse_conn *fc = ff->fc;
666 fuse_read_fill(req, file, pos, count, FUSE_READ);
668 struct fuse_read_in *inarg = &req->misc.read.in;
670 inarg->read_flags |= FUSE_READ_LOCKOWNER;
671 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
675 return fuse_async_req_send(fc, req, count, io);
677 fuse_request_send(fc, req);
678 return req->out.args[0].size;
681 static void fuse_read_update_size(struct inode *inode, loff_t size,
684 struct fuse_conn *fc = get_fuse_conn(inode);
685 struct fuse_inode *fi = get_fuse_inode(inode);
687 spin_lock(&fc->lock);
688 if (attr_ver == fi->attr_version && size < inode->i_size &&
689 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
690 fi->attr_version = ++fc->attr_version;
691 i_size_write(inode, size);
693 spin_unlock(&fc->lock);
696 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
699 size_t num_read = req->out.args[0].size;
700 struct fuse_conn *fc = get_fuse_conn(inode);
702 if (fc->writeback_cache) {
704 * A hole in a file. Some data after the hole are in page cache,
705 * but have not reached the client fs yet. So, the hole is not
709 int start_idx = num_read >> PAGE_SHIFT;
710 size_t off = num_read & (PAGE_SIZE - 1);
712 for (i = start_idx; i < req->num_pages; i++) {
713 zero_user_segment(req->pages[i], off, PAGE_SIZE);
717 loff_t pos = page_offset(req->pages[0]) + num_read;
718 fuse_read_update_size(inode, pos, attr_ver);
722 static int fuse_do_readpage(struct file *file, struct page *page)
724 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
725 struct inode *inode = page->mapping->host;
726 struct fuse_conn *fc = get_fuse_conn(inode);
727 struct fuse_req *req;
729 loff_t pos = page_offset(page);
730 size_t count = PAGE_SIZE;
735 * Page writeback can extend beyond the lifetime of the
736 * page-cache page, so make sure we read a properly synced
739 fuse_wait_on_page_writeback(inode, page->index);
741 req = fuse_get_req(fc, 1);
745 attr_ver = fuse_get_attr_version(fc);
747 req->out.page_zeroing = 1;
748 req->out.argpages = 1;
750 req->pages[0] = page;
751 req->page_descs[0].length = count;
752 num_read = fuse_send_read(req, &io, pos, count, NULL);
753 err = req->out.h.error;
757 * Short read means EOF. If file size is larger, truncate it
759 if (num_read < count)
760 fuse_short_read(req, inode, attr_ver);
762 SetPageUptodate(page);
765 fuse_put_request(fc, req);
770 static int fuse_readpage(struct file *file, struct page *page)
772 struct inode *inode = page->mapping->host;
776 if (is_bad_inode(inode))
779 err = fuse_do_readpage(file, page);
780 fuse_invalidate_atime(inode);
786 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
789 size_t count = req->misc.read.in.size;
790 size_t num_read = req->out.args[0].size;
791 struct address_space *mapping = NULL;
793 for (i = 0; mapping == NULL && i < req->num_pages; i++)
794 mapping = req->pages[i]->mapping;
797 struct inode *inode = mapping->host;
800 * Short read means EOF. If file size is larger, truncate it
802 if (!req->out.h.error && num_read < count)
803 fuse_short_read(req, inode, req->misc.read.attr_ver);
805 fuse_invalidate_atime(inode);
808 for (i = 0; i < req->num_pages; i++) {
809 struct page *page = req->pages[i];
810 if (!req->out.h.error)
811 SetPageUptodate(page);
818 fuse_file_put(req->ff, false);
821 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
823 struct fuse_file *ff = file->private_data;
824 struct fuse_conn *fc = ff->fc;
825 loff_t pos = page_offset(req->pages[0]);
826 size_t count = req->num_pages << PAGE_SHIFT;
828 req->out.argpages = 1;
829 req->out.page_zeroing = 1;
830 req->out.page_replace = 1;
831 fuse_read_fill(req, file, pos, count, FUSE_READ);
832 req->misc.read.attr_ver = fuse_get_attr_version(fc);
833 if (fc->async_read) {
834 req->ff = fuse_file_get(ff);
835 req->end = fuse_readpages_end;
836 fuse_request_send_background(fc, req);
838 fuse_request_send(fc, req);
839 fuse_readpages_end(fc, req);
840 fuse_put_request(fc, req);
844 struct fuse_fill_data {
845 struct fuse_req *req;
851 static int fuse_readpages_fill(void *_data, struct page *page)
853 struct fuse_fill_data *data = _data;
854 struct fuse_req *req = data->req;
855 struct inode *inode = data->inode;
856 struct fuse_conn *fc = get_fuse_conn(inode);
858 fuse_wait_on_page_writeback(inode, page->index);
860 if (req->num_pages &&
861 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
862 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
863 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
864 int nr_alloc = min_t(unsigned, data->nr_pages,
865 FUSE_MAX_PAGES_PER_REQ);
866 fuse_send_readpages(req, data->file);
868 req = fuse_get_req_for_background(fc, nr_alloc);
870 req = fuse_get_req(fc, nr_alloc);
879 if (WARN_ON(req->num_pages >= req->max_pages)) {
880 fuse_put_request(fc, req);
885 req->pages[req->num_pages] = page;
886 req->page_descs[req->num_pages].length = PAGE_SIZE;
892 static int fuse_readpages(struct file *file, struct address_space *mapping,
893 struct list_head *pages, unsigned nr_pages)
895 struct inode *inode = mapping->host;
896 struct fuse_conn *fc = get_fuse_conn(inode);
897 struct fuse_fill_data data;
899 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
902 if (is_bad_inode(inode))
908 data.req = fuse_get_req_for_background(fc, nr_alloc);
910 data.req = fuse_get_req(fc, nr_alloc);
911 data.nr_pages = nr_pages;
912 err = PTR_ERR(data.req);
913 if (IS_ERR(data.req))
916 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
918 if (data.req->num_pages)
919 fuse_send_readpages(data.req, file);
921 fuse_put_request(fc, data.req);
927 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
929 struct inode *inode = iocb->ki_filp->f_mapping->host;
930 struct fuse_conn *fc = get_fuse_conn(inode);
933 * In auto invalidate mode, always update attributes on read.
934 * Otherwise, only update if we attempt to read past EOF (to ensure
935 * i_size is up to date).
937 if (fc->auto_inval_data ||
938 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
940 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
945 return generic_file_read_iter(iocb, to);
948 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
949 loff_t pos, size_t count)
951 struct fuse_write_in *inarg = &req->misc.write.in;
952 struct fuse_write_out *outarg = &req->misc.write.out;
957 req->in.h.opcode = FUSE_WRITE;
958 req->in.h.nodeid = ff->nodeid;
960 if (ff->fc->minor < 9)
961 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
963 req->in.args[0].size = sizeof(struct fuse_write_in);
964 req->in.args[0].value = inarg;
965 req->in.args[1].size = count;
966 req->out.numargs = 1;
967 req->out.args[0].size = sizeof(struct fuse_write_out);
968 req->out.args[0].value = outarg;
971 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
972 loff_t pos, size_t count, fl_owner_t owner)
974 struct file *file = io->file;
975 struct fuse_file *ff = file->private_data;
976 struct fuse_conn *fc = ff->fc;
977 struct fuse_write_in *inarg = &req->misc.write.in;
979 fuse_write_fill(req, ff, pos, count);
980 inarg->flags = file->f_flags;
982 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
983 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
987 return fuse_async_req_send(fc, req, count, io);
989 fuse_request_send(fc, req);
990 return req->misc.write.out.size;
993 bool fuse_write_update_size(struct inode *inode, loff_t pos)
995 struct fuse_conn *fc = get_fuse_conn(inode);
996 struct fuse_inode *fi = get_fuse_inode(inode);
999 spin_lock(&fc->lock);
1000 fi->attr_version = ++fc->attr_version;
1001 if (pos > inode->i_size) {
1002 i_size_write(inode, pos);
1005 spin_unlock(&fc->lock);
1010 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1011 struct inode *inode, loff_t pos,
1017 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1019 for (i = 0; i < req->num_pages; i++)
1020 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1022 res = fuse_send_write(req, &io, pos, count, NULL);
1024 offset = req->page_descs[0].offset;
1026 for (i = 0; i < req->num_pages; i++) {
1027 struct page *page = req->pages[i];
1029 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1030 SetPageUptodate(page);
1032 if (count > PAGE_SIZE - offset)
1033 count -= PAGE_SIZE - offset;
1045 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1046 struct address_space *mapping,
1047 struct iov_iter *ii, loff_t pos)
1049 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1050 unsigned offset = pos & (PAGE_SIZE - 1);
1054 req->in.argpages = 1;
1055 req->page_descs[0].offset = offset;
1060 pgoff_t index = pos >> PAGE_SHIFT;
1061 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1062 iov_iter_count(ii));
1064 bytes = min_t(size_t, bytes, fc->max_write - count);
1068 if (iov_iter_fault_in_readable(ii, bytes))
1072 page = grab_cache_page_write_begin(mapping, index, 0);
1076 if (mapping_writably_mapped(mapping))
1077 flush_dcache_page(page);
1079 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1080 flush_dcache_page(page);
1082 iov_iter_advance(ii, tmp);
1086 bytes = min(bytes, iov_iter_single_seg_count(ii));
1091 req->pages[req->num_pages] = page;
1092 req->page_descs[req->num_pages].length = tmp;
1098 if (offset == PAGE_SIZE)
1101 if (!fc->big_writes)
1103 } while (iov_iter_count(ii) && count < fc->max_write &&
1104 req->num_pages < req->max_pages && offset == 0);
1106 return count > 0 ? count : err;
1109 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1111 return min_t(unsigned,
1112 ((pos + len - 1) >> PAGE_SHIFT) -
1113 (pos >> PAGE_SHIFT) + 1,
1114 FUSE_MAX_PAGES_PER_REQ);
1117 static ssize_t fuse_perform_write(struct file *file,
1118 struct address_space *mapping,
1119 struct iov_iter *ii, loff_t pos)
1121 struct inode *inode = mapping->host;
1122 struct fuse_conn *fc = get_fuse_conn(inode);
1123 struct fuse_inode *fi = get_fuse_inode(inode);
1127 if (is_bad_inode(inode))
1130 if (inode->i_size < pos + iov_iter_count(ii))
1131 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1134 struct fuse_req *req;
1136 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1138 req = fuse_get_req(fc, nr_pages);
1144 count = fuse_fill_write_pages(req, mapping, ii, pos);
1150 num_written = fuse_send_write_pages(req, file, inode,
1152 err = req->out.h.error;
1157 /* break out of the loop on short write */
1158 if (num_written != count)
1162 fuse_put_request(fc, req);
1163 } while (!err && iov_iter_count(ii));
1166 fuse_write_update_size(inode, pos);
1168 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1169 fuse_invalidate_attr(inode);
1171 return res > 0 ? res : err;
1174 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1176 struct file *file = iocb->ki_filp;
1177 struct address_space *mapping = file->f_mapping;
1178 ssize_t written = 0;
1179 ssize_t written_buffered = 0;
1180 struct inode *inode = mapping->host;
1184 if (get_fuse_conn(inode)->writeback_cache) {
1185 /* Update size (EOF optimization) and mode (SUID clearing) */
1186 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1190 return generic_file_write_iter(iocb, from);
1195 /* We can write back this queue in page reclaim */
1196 current->backing_dev_info = inode_to_bdi(inode);
1198 err = generic_write_checks(iocb, from);
1202 err = file_remove_privs(file);
1206 err = file_update_time(file);
1210 if (iocb->ki_flags & IOCB_DIRECT) {
1211 loff_t pos = iocb->ki_pos;
1212 written = generic_file_direct_write(iocb, from);
1213 if (written < 0 || !iov_iter_count(from))
1218 written_buffered = fuse_perform_write(file, mapping, from, pos);
1219 if (written_buffered < 0) {
1220 err = written_buffered;
1223 endbyte = pos + written_buffered - 1;
1225 err = filemap_write_and_wait_range(file->f_mapping, pos,
1230 invalidate_mapping_pages(file->f_mapping,
1232 endbyte >> PAGE_SHIFT);
1234 written += written_buffered;
1235 iocb->ki_pos = pos + written_buffered;
1237 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
1239 iocb->ki_pos += written;
1242 current->backing_dev_info = NULL;
1243 inode_unlock(inode);
1245 return written ? written : err;
1248 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1249 unsigned index, unsigned nr_pages)
1253 for (i = index; i < index + nr_pages; i++)
1254 req->page_descs[i].length = PAGE_SIZE -
1255 req->page_descs[i].offset;
1258 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1260 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1263 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1266 return min(iov_iter_single_seg_count(ii), max_size);
1269 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1270 size_t *nbytesp, int write)
1272 size_t nbytes = 0; /* # bytes already packed in req */
1275 /* Special case for kernel I/O: can copy directly into the buffer */
1276 if (ii->type & ITER_KVEC) {
1277 unsigned long user_addr = fuse_get_user_addr(ii);
1278 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1281 req->in.args[1].value = (void *) user_addr;
1283 req->out.args[0].value = (void *) user_addr;
1285 iov_iter_advance(ii, frag_size);
1286 *nbytesp = frag_size;
1290 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1293 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1295 req->max_pages - req->num_pages,
1300 iov_iter_advance(ii, ret);
1304 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1306 req->page_descs[req->num_pages].offset = start;
1307 fuse_page_descs_length_init(req, req->num_pages, npages);
1309 req->num_pages += npages;
1310 req->page_descs[req->num_pages - 1].length -=
1311 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1315 req->in.argpages = 1;
1317 req->out.argpages = 1;
1321 return ret < 0 ? ret : 0;
1324 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1326 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1329 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1330 loff_t *ppos, int flags)
1332 int write = flags & FUSE_DIO_WRITE;
1333 int cuse = flags & FUSE_DIO_CUSE;
1334 struct file *file = io->file;
1335 struct inode *inode = file->f_mapping->host;
1336 struct fuse_file *ff = file->private_data;
1337 struct fuse_conn *fc = ff->fc;
1338 size_t nmax = write ? fc->max_write : fc->max_read;
1340 size_t count = iov_iter_count(iter);
1341 pgoff_t idx_from = pos >> PAGE_SHIFT;
1342 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1344 struct fuse_req *req;
1348 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1350 req = fuse_get_req(fc, fuse_iter_npages(iter));
1352 return PTR_ERR(req);
1354 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1357 fuse_sync_writes(inode);
1359 inode_unlock(inode);
1364 fl_owner_t owner = current->files;
1365 size_t nbytes = min(count, nmax);
1366 err = fuse_get_user_pages(req, iter, &nbytes, write);
1371 nres = fuse_send_write(req, io, pos, nbytes, owner);
1373 nres = fuse_send_read(req, io, pos, nbytes, owner);
1376 fuse_release_user_pages(req, !write);
1377 if (req->out.h.error) {
1378 err = req->out.h.error;
1380 } else if (nres > nbytes) {
1391 fuse_put_request(fc, req);
1393 req = fuse_get_req_for_background(fc,
1394 fuse_iter_npages(iter));
1396 req = fuse_get_req(fc, fuse_iter_npages(iter));
1402 fuse_put_request(fc, req);
1406 return res > 0 ? res : err;
1408 EXPORT_SYMBOL_GPL(fuse_direct_io);
1410 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1411 struct iov_iter *iter,
1415 struct file *file = io->file;
1416 struct inode *inode = file_inode(file);
1418 if (is_bad_inode(inode))
1421 res = fuse_direct_io(io, iter, ppos, 0);
1423 fuse_invalidate_attr(inode);
1428 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1430 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
1431 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1434 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1436 struct file *file = iocb->ki_filp;
1437 struct inode *inode = file_inode(file);
1438 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1441 if (is_bad_inode(inode))
1444 /* Don't allow parallel writes to the same file */
1446 res = generic_write_checks(iocb, from);
1448 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1449 fuse_invalidate_attr(inode);
1451 fuse_write_update_size(inode, iocb->ki_pos);
1452 inode_unlock(inode);
1457 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1461 for (i = 0; i < req->num_pages; i++)
1462 __free_page(req->pages[i]);
1465 fuse_file_put(req->ff, false);
1468 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1470 struct inode *inode = req->inode;
1471 struct fuse_inode *fi = get_fuse_inode(inode);
1472 struct backing_dev_info *bdi = inode_to_bdi(inode);
1475 list_del(&req->writepages_entry);
1476 for (i = 0; i < req->num_pages; i++) {
1477 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1478 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1479 wb_writeout_inc(&bdi->wb);
1481 wake_up(&fi->page_waitq);
1484 /* Called under fc->lock, may release and reacquire it */
1485 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1487 __releases(fc->lock)
1488 __acquires(fc->lock)
1490 struct fuse_inode *fi = get_fuse_inode(req->inode);
1491 struct fuse_write_in *inarg = &req->misc.write.in;
1492 __u64 data_size = req->num_pages * PAGE_SIZE;
1497 if (inarg->offset + data_size <= size) {
1498 inarg->size = data_size;
1499 } else if (inarg->offset < size) {
1500 inarg->size = size - inarg->offset;
1502 /* Got truncated off completely */
1506 req->in.args[1].size = inarg->size;
1508 fuse_request_send_background_locked(fc, req);
1512 fuse_writepage_finish(fc, req);
1513 spin_unlock(&fc->lock);
1514 fuse_writepage_free(fc, req);
1515 fuse_put_request(fc, req);
1516 spin_lock(&fc->lock);
1520 * If fi->writectr is positive (no truncate or fsync going on) send
1521 * all queued writepage requests.
1523 * Called with fc->lock
1525 void fuse_flush_writepages(struct inode *inode)
1526 __releases(fc->lock)
1527 __acquires(fc->lock)
1529 struct fuse_conn *fc = get_fuse_conn(inode);
1530 struct fuse_inode *fi = get_fuse_inode(inode);
1531 size_t crop = i_size_read(inode);
1532 struct fuse_req *req;
1534 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1535 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1536 list_del_init(&req->list);
1537 fuse_send_writepage(fc, req, crop);
1541 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1543 struct inode *inode = req->inode;
1544 struct fuse_inode *fi = get_fuse_inode(inode);
1546 mapping_set_error(inode->i_mapping, req->out.h.error);
1547 spin_lock(&fc->lock);
1548 while (req->misc.write.next) {
1549 struct fuse_conn *fc = get_fuse_conn(inode);
1550 struct fuse_write_in *inarg = &req->misc.write.in;
1551 struct fuse_req *next = req->misc.write.next;
1552 req->misc.write.next = next->misc.write.next;
1553 next->misc.write.next = NULL;
1554 next->ff = fuse_file_get(req->ff);
1555 list_add(&next->writepages_entry, &fi->writepages);
1558 * Skip fuse_flush_writepages() to make it easy to crop requests
1559 * based on primary request size.
1561 * 1st case (trivial): there are no concurrent activities using
1562 * fuse_set/release_nowrite. Then we're on safe side because
1563 * fuse_flush_writepages() would call fuse_send_writepage()
1566 * 2nd case: someone called fuse_set_nowrite and it is waiting
1567 * now for completion of all in-flight requests. This happens
1568 * rarely and no more than once per page, so this should be
1571 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1572 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1573 * that fuse_set_nowrite returned implies that all in-flight
1574 * requests were completed along with all of their secondary
1575 * requests. Further primary requests are blocked by negative
1576 * writectr. Hence there cannot be any in-flight requests and
1577 * no invocations of fuse_writepage_end() while we're in
1578 * fuse_set_nowrite..fuse_release_nowrite section.
1580 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1583 fuse_writepage_finish(fc, req);
1584 spin_unlock(&fc->lock);
1585 fuse_writepage_free(fc, req);
1588 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1589 struct fuse_inode *fi)
1591 struct fuse_file *ff = NULL;
1593 spin_lock(&fc->lock);
1594 if (!list_empty(&fi->write_files)) {
1595 ff = list_entry(fi->write_files.next, struct fuse_file,
1599 spin_unlock(&fc->lock);
1604 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1605 struct fuse_inode *fi)
1607 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1612 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1614 struct fuse_conn *fc = get_fuse_conn(inode);
1615 struct fuse_inode *fi = get_fuse_inode(inode);
1616 struct fuse_file *ff;
1619 ff = __fuse_write_file_get(fc, fi);
1620 err = fuse_flush_times(inode, ff);
1622 fuse_file_put(ff, 0);
1627 static int fuse_writepage_locked(struct page *page)
1629 struct address_space *mapping = page->mapping;
1630 struct inode *inode = mapping->host;
1631 struct fuse_conn *fc = get_fuse_conn(inode);
1632 struct fuse_inode *fi = get_fuse_inode(inode);
1633 struct fuse_req *req;
1634 struct page *tmp_page;
1635 int error = -ENOMEM;
1637 set_page_writeback(page);
1639 req = fuse_request_alloc_nofs(1);
1643 /* writeback always goes to bg_queue */
1644 __set_bit(FR_BACKGROUND, &req->flags);
1645 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1650 req->ff = fuse_write_file_get(fc, fi);
1654 fuse_write_fill(req, req->ff, page_offset(page), 0);
1656 copy_highpage(tmp_page, page);
1657 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1658 req->misc.write.next = NULL;
1659 req->in.argpages = 1;
1661 req->pages[0] = tmp_page;
1662 req->page_descs[0].offset = 0;
1663 req->page_descs[0].length = PAGE_SIZE;
1664 req->end = fuse_writepage_end;
1667 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1668 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1670 spin_lock(&fc->lock);
1671 list_add(&req->writepages_entry, &fi->writepages);
1672 list_add_tail(&req->list, &fi->queued_writes);
1673 fuse_flush_writepages(inode);
1674 spin_unlock(&fc->lock);
1676 end_page_writeback(page);
1681 __free_page(tmp_page);
1683 fuse_request_free(req);
1685 end_page_writeback(page);
1689 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1693 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1695 * ->writepages() should be called for sync() and friends. We
1696 * should only get here on direct reclaim and then we are
1697 * allowed to skip a page which is already in flight
1699 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1701 redirty_page_for_writepage(wbc, page);
1705 err = fuse_writepage_locked(page);
1711 struct fuse_fill_wb_data {
1712 struct fuse_req *req;
1713 struct fuse_file *ff;
1714 struct inode *inode;
1715 struct page **orig_pages;
1718 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1720 struct fuse_req *req = data->req;
1721 struct inode *inode = data->inode;
1722 struct fuse_conn *fc = get_fuse_conn(inode);
1723 struct fuse_inode *fi = get_fuse_inode(inode);
1724 int num_pages = req->num_pages;
1727 req->ff = fuse_file_get(data->ff);
1728 spin_lock(&fc->lock);
1729 list_add_tail(&req->list, &fi->queued_writes);
1730 fuse_flush_writepages(inode);
1731 spin_unlock(&fc->lock);
1733 for (i = 0; i < num_pages; i++)
1734 end_page_writeback(data->orig_pages[i]);
1737 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1740 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1741 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1742 struct fuse_req *tmp;
1743 struct fuse_req *old_req;
1747 BUG_ON(new_req->num_pages != 0);
1749 spin_lock(&fc->lock);
1750 list_del(&new_req->writepages_entry);
1751 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1752 BUG_ON(old_req->inode != new_req->inode);
1753 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1754 if (curr_index <= page->index &&
1755 page->index < curr_index + old_req->num_pages) {
1761 list_add(&new_req->writepages_entry, &fi->writepages);
1765 new_req->num_pages = 1;
1766 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1767 BUG_ON(tmp->inode != new_req->inode);
1768 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1769 if (tmp->num_pages == 1 &&
1770 curr_index == page->index) {
1775 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1776 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1778 copy_highpage(old_req->pages[0], page);
1779 spin_unlock(&fc->lock);
1781 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1782 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
1783 wb_writeout_inc(&bdi->wb);
1784 fuse_writepage_free(fc, new_req);
1785 fuse_request_free(new_req);
1788 new_req->misc.write.next = old_req->misc.write.next;
1789 old_req->misc.write.next = new_req;
1792 spin_unlock(&fc->lock);
1797 static int fuse_writepages_fill(struct page *page,
1798 struct writeback_control *wbc, void *_data)
1800 struct fuse_fill_wb_data *data = _data;
1801 struct fuse_req *req = data->req;
1802 struct inode *inode = data->inode;
1803 struct fuse_conn *fc = get_fuse_conn(inode);
1804 struct page *tmp_page;
1810 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1816 * Being under writeback is unlikely but possible. For example direct
1817 * read to an mmaped fuse file will set the page dirty twice; once when
1818 * the pages are faulted with get_user_pages(), and then after the read
1821 is_writeback = fuse_page_is_writeback(inode, page->index);
1823 if (req && req->num_pages &&
1824 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1825 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1826 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1827 fuse_writepages_send(data);
1831 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1836 * The page must not be redirtied until the writeout is completed
1837 * (i.e. userspace has sent a reply to the write request). Otherwise
1838 * there could be more than one temporary page instance for each real
1841 * This is ensured by holding the page lock in page_mkwrite() while
1842 * checking fuse_page_is_writeback(). We already hold the page lock
1843 * since clear_page_dirty_for_io() and keep it held until we add the
1844 * request to the fi->writepages list and increment req->num_pages.
1845 * After this fuse_page_is_writeback() will indicate that the page is
1846 * under writeback, so we can release the page lock.
1848 if (data->req == NULL) {
1849 struct fuse_inode *fi = get_fuse_inode(inode);
1852 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1854 __free_page(tmp_page);
1858 fuse_write_fill(req, data->ff, page_offset(page), 0);
1859 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1860 req->misc.write.next = NULL;
1861 req->in.argpages = 1;
1862 __set_bit(FR_BACKGROUND, &req->flags);
1864 req->end = fuse_writepage_end;
1867 spin_lock(&fc->lock);
1868 list_add(&req->writepages_entry, &fi->writepages);
1869 spin_unlock(&fc->lock);
1873 set_page_writeback(page);
1875 copy_highpage(tmp_page, page);
1876 req->pages[req->num_pages] = tmp_page;
1877 req->page_descs[req->num_pages].offset = 0;
1878 req->page_descs[req->num_pages].length = PAGE_SIZE;
1880 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1881 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1884 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1885 end_page_writeback(page);
1889 data->orig_pages[req->num_pages] = page;
1892 * Protected by fc->lock against concurrent access by
1893 * fuse_page_is_writeback().
1895 spin_lock(&fc->lock);
1897 spin_unlock(&fc->lock);
1905 static int fuse_writepages(struct address_space *mapping,
1906 struct writeback_control *wbc)
1908 struct inode *inode = mapping->host;
1909 struct fuse_fill_wb_data data;
1913 if (is_bad_inode(inode))
1921 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1922 sizeof(struct page *),
1924 if (!data.orig_pages)
1927 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1929 /* Ignore errors if we can write at least one page */
1930 BUG_ON(!data.req->num_pages);
1931 fuse_writepages_send(&data);
1935 fuse_file_put(data.ff, false);
1937 kfree(data.orig_pages);
1943 * It's worthy to make sure that space is reserved on disk for the write,
1944 * but how to implement it without killing performance need more thinking.
1946 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1947 loff_t pos, unsigned len, unsigned flags,
1948 struct page **pagep, void **fsdata)
1950 pgoff_t index = pos >> PAGE_SHIFT;
1951 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1956 WARN_ON(!fc->writeback_cache);
1958 page = grab_cache_page_write_begin(mapping, index, flags);
1962 fuse_wait_on_page_writeback(mapping->host, page->index);
1964 if (PageUptodate(page) || len == PAGE_SIZE)
1967 * Check if the start this page comes after the end of file, in which
1968 * case the readpage can be optimized away.
1970 fsize = i_size_read(mapping->host);
1971 if (fsize <= (pos & PAGE_MASK)) {
1972 size_t off = pos & ~PAGE_MASK;
1974 zero_user_segment(page, 0, off);
1977 err = fuse_do_readpage(file, page);
1991 static int fuse_write_end(struct file *file, struct address_space *mapping,
1992 loff_t pos, unsigned len, unsigned copied,
1993 struct page *page, void *fsdata)
1995 struct inode *inode = page->mapping->host;
1997 if (!PageUptodate(page)) {
1998 /* Zero any unwritten bytes at the end of the page */
1999 size_t endoff = (pos + copied) & ~PAGE_MASK;
2001 zero_user_segment(page, endoff, PAGE_SIZE);
2002 SetPageUptodate(page);
2005 fuse_write_update_size(inode, pos + copied);
2006 set_page_dirty(page);
2013 static int fuse_launder_page(struct page *page)
2016 if (clear_page_dirty_for_io(page)) {
2017 struct inode *inode = page->mapping->host;
2018 err = fuse_writepage_locked(page);
2020 fuse_wait_on_page_writeback(inode, page->index);
2026 * Write back dirty pages now, because there may not be any suitable
2029 static void fuse_vma_close(struct vm_area_struct *vma)
2031 filemap_write_and_wait(vma->vm_file->f_mapping);
2035 * Wait for writeback against this page to complete before allowing it
2036 * to be marked dirty again, and hence written back again, possibly
2037 * before the previous writepage completed.
2039 * Block here, instead of in ->writepage(), so that the userspace fs
2040 * can only block processes actually operating on the filesystem.
2042 * Otherwise unprivileged userspace fs would be able to block
2047 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2049 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2051 struct page *page = vmf->page;
2052 struct inode *inode = file_inode(vma->vm_file);
2054 file_update_time(vma->vm_file);
2056 if (page->mapping != inode->i_mapping) {
2058 return VM_FAULT_NOPAGE;
2061 fuse_wait_on_page_writeback(inode, page->index);
2062 return VM_FAULT_LOCKED;
2065 static const struct vm_operations_struct fuse_file_vm_ops = {
2066 .close = fuse_vma_close,
2067 .fault = filemap_fault,
2068 .map_pages = filemap_map_pages,
2069 .page_mkwrite = fuse_page_mkwrite,
2072 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2074 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2075 fuse_link_write_file(file);
2077 file_accessed(file);
2078 vma->vm_ops = &fuse_file_vm_ops;
2082 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2084 /* Can't provide the coherency needed for MAP_SHARED */
2085 if (vma->vm_flags & VM_MAYSHARE)
2088 invalidate_inode_pages2(file->f_mapping);
2090 return generic_file_mmap(file, vma);
2093 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2094 struct file_lock *fl)
2096 switch (ffl->type) {
2102 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2103 ffl->end < ffl->start)
2106 fl->fl_start = ffl->start;
2107 fl->fl_end = ffl->end;
2108 fl->fl_pid = ffl->pid;
2114 fl->fl_type = ffl->type;
2118 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2119 const struct file_lock *fl, int opcode, pid_t pid,
2120 int flock, struct fuse_lk_in *inarg)
2122 struct inode *inode = file_inode(file);
2123 struct fuse_conn *fc = get_fuse_conn(inode);
2124 struct fuse_file *ff = file->private_data;
2126 memset(inarg, 0, sizeof(*inarg));
2128 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2129 inarg->lk.start = fl->fl_start;
2130 inarg->lk.end = fl->fl_end;
2131 inarg->lk.type = fl->fl_type;
2132 inarg->lk.pid = pid;
2134 inarg->lk_flags |= FUSE_LK_FLOCK;
2135 args->in.h.opcode = opcode;
2136 args->in.h.nodeid = get_node_id(inode);
2137 args->in.numargs = 1;
2138 args->in.args[0].size = sizeof(*inarg);
2139 args->in.args[0].value = inarg;
2142 static int fuse_getlk(struct file *file, struct file_lock *fl)
2144 struct inode *inode = file_inode(file);
2145 struct fuse_conn *fc = get_fuse_conn(inode);
2147 struct fuse_lk_in inarg;
2148 struct fuse_lk_out outarg;
2151 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2152 args.out.numargs = 1;
2153 args.out.args[0].size = sizeof(outarg);
2154 args.out.args[0].value = &outarg;
2155 err = fuse_simple_request(fc, &args);
2157 err = convert_fuse_file_lock(&outarg.lk, fl);
2162 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2164 struct inode *inode = file_inode(file);
2165 struct fuse_conn *fc = get_fuse_conn(inode);
2167 struct fuse_lk_in inarg;
2168 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2169 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2172 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2173 /* NLM needs asynchronous locks, which we don't support yet */
2177 /* Unlock on close is handled by the flush method */
2178 if (fl->fl_flags & FL_CLOSE)
2181 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2182 err = fuse_simple_request(fc, &args);
2184 /* locking is restartable */
2191 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2193 struct inode *inode = file_inode(file);
2194 struct fuse_conn *fc = get_fuse_conn(inode);
2197 if (cmd == F_CANCELLK) {
2199 } else if (cmd == F_GETLK) {
2201 posix_test_lock(file, fl);
2204 err = fuse_getlk(file, fl);
2207 err = posix_lock_file(file, fl, NULL);
2209 err = fuse_setlk(file, fl, 0);
2214 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2216 struct inode *inode = file_inode(file);
2217 struct fuse_conn *fc = get_fuse_conn(inode);
2221 err = locks_lock_file_wait(file, fl);
2223 struct fuse_file *ff = file->private_data;
2225 /* emulate flock with POSIX locks */
2227 err = fuse_setlk(file, fl, 1);
2233 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2235 struct inode *inode = mapping->host;
2236 struct fuse_conn *fc = get_fuse_conn(inode);
2238 struct fuse_bmap_in inarg;
2239 struct fuse_bmap_out outarg;
2242 if (!inode->i_sb->s_bdev || fc->no_bmap)
2245 memset(&inarg, 0, sizeof(inarg));
2246 inarg.block = block;
2247 inarg.blocksize = inode->i_sb->s_blocksize;
2248 args.in.h.opcode = FUSE_BMAP;
2249 args.in.h.nodeid = get_node_id(inode);
2250 args.in.numargs = 1;
2251 args.in.args[0].size = sizeof(inarg);
2252 args.in.args[0].value = &inarg;
2253 args.out.numargs = 1;
2254 args.out.args[0].size = sizeof(outarg);
2255 args.out.args[0].value = &outarg;
2256 err = fuse_simple_request(fc, &args);
2260 return err ? 0 : outarg.block;
2263 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2265 struct inode *inode = file->f_mapping->host;
2266 struct fuse_conn *fc = get_fuse_conn(inode);
2267 struct fuse_file *ff = file->private_data;
2269 struct fuse_lseek_in inarg = {
2274 struct fuse_lseek_out outarg;
2280 args.in.h.opcode = FUSE_LSEEK;
2281 args.in.h.nodeid = ff->nodeid;
2282 args.in.numargs = 1;
2283 args.in.args[0].size = sizeof(inarg);
2284 args.in.args[0].value = &inarg;
2285 args.out.numargs = 1;
2286 args.out.args[0].size = sizeof(outarg);
2287 args.out.args[0].value = &outarg;
2288 err = fuse_simple_request(fc, &args);
2290 if (err == -ENOSYS) {
2297 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2300 err = fuse_update_attributes(inode, NULL, file, NULL);
2302 return generic_file_llseek(file, offset, whence);
2307 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2310 struct inode *inode = file_inode(file);
2315 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2316 retval = generic_file_llseek(file, offset, whence);
2320 retval = fuse_update_attributes(inode, NULL, file, NULL);
2322 retval = generic_file_llseek(file, offset, whence);
2323 inode_unlock(inode);
2328 retval = fuse_lseek(file, offset, whence);
2329 inode_unlock(inode);
2338 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2339 unsigned int nr_segs, size_t bytes, bool to_user)
2347 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
2349 while (iov_iter_count(&ii)) {
2350 struct page *page = pages[page_idx++];
2351 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
2357 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2358 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2359 size_t copy = min(todo, iov_len);
2363 left = copy_from_user(kaddr, uaddr, copy);
2365 left = copy_to_user(uaddr, kaddr, copy);
2370 iov_iter_advance(&ii, copy);
2382 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2383 * ABI was defined to be 'struct iovec' which is different on 32bit
2384 * and 64bit. Fortunately we can determine which structure the server
2385 * used from the size of the reply.
2387 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2388 size_t transferred, unsigned count,
2391 #ifdef CONFIG_COMPAT
2392 if (count * sizeof(struct compat_iovec) == transferred) {
2393 struct compat_iovec *ciov = src;
2397 * With this interface a 32bit server cannot support
2398 * non-compat (i.e. ones coming from 64bit apps) ioctl
2404 for (i = 0; i < count; i++) {
2405 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2406 dst[i].iov_len = ciov[i].iov_len;
2412 if (count * sizeof(struct iovec) != transferred)
2415 memcpy(dst, src, transferred);
2419 /* Make sure iov_length() won't overflow */
2420 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2423 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2425 for (n = 0; n < count; n++, iov++) {
2426 if (iov->iov_len > (size_t) max)
2428 max -= iov->iov_len;
2433 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2434 void *src, size_t transferred, unsigned count,
2438 struct fuse_ioctl_iovec *fiov = src;
2440 if (fc->minor < 16) {
2441 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2445 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2448 for (i = 0; i < count; i++) {
2449 /* Did the server supply an inappropriate value? */
2450 if (fiov[i].base != (unsigned long) fiov[i].base ||
2451 fiov[i].len != (unsigned long) fiov[i].len)
2454 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2455 dst[i].iov_len = (size_t) fiov[i].len;
2457 #ifdef CONFIG_COMPAT
2459 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2460 (compat_size_t) dst[i].iov_len != fiov[i].len))
2470 * For ioctls, there is no generic way to determine how much memory
2471 * needs to be read and/or written. Furthermore, ioctls are allowed
2472 * to dereference the passed pointer, so the parameter requires deep
2473 * copying but FUSE has no idea whatsoever about what to copy in or
2476 * This is solved by allowing FUSE server to retry ioctl with
2477 * necessary in/out iovecs. Let's assume the ioctl implementation
2478 * needs to read in the following structure.
2485 * On the first callout to FUSE server, inarg->in_size and
2486 * inarg->out_size will be NULL; then, the server completes the ioctl
2487 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2488 * the actual iov array to
2490 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2492 * which tells FUSE to copy in the requested area and retry the ioctl.
2493 * On the second round, the server has access to the structure and
2494 * from that it can tell what to look for next, so on the invocation,
2495 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2497 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2498 * { .iov_base = a.buf, .iov_len = a.buflen } }
2500 * FUSE will copy both struct a and the pointed buffer from the
2501 * process doing the ioctl and retry ioctl with both struct a and the
2504 * This time, FUSE server has everything it needs and completes ioctl
2505 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2507 * Copying data out works the same way.
2509 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2510 * automatically initializes in and out iovs by decoding @cmd with
2511 * _IOC_* macros and the server is not allowed to request RETRY. This
2512 * limits ioctl data transfers to well-formed ioctls and is the forced
2513 * behavior for all FUSE servers.
2515 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2518 struct fuse_file *ff = file->private_data;
2519 struct fuse_conn *fc = ff->fc;
2520 struct fuse_ioctl_in inarg = {
2526 struct fuse_ioctl_out outarg;
2527 struct fuse_req *req = NULL;
2528 struct page **pages = NULL;
2529 struct iovec *iov_page = NULL;
2530 struct iovec *in_iov = NULL, *out_iov = NULL;
2531 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2532 size_t in_size, out_size, transferred;
2535 #if BITS_PER_LONG == 32
2536 inarg.flags |= FUSE_IOCTL_32BIT;
2538 if (flags & FUSE_IOCTL_COMPAT)
2539 inarg.flags |= FUSE_IOCTL_32BIT;
2542 /* assume all the iovs returned by client always fits in a page */
2543 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2546 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2547 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2548 if (!pages || !iov_page)
2552 * If restricted, initialize IO parameters as encoded in @cmd.
2553 * RETRY from server is not allowed.
2555 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2556 struct iovec *iov = iov_page;
2558 iov->iov_base = (void __user *)arg;
2559 iov->iov_len = _IOC_SIZE(cmd);
2561 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2566 if (_IOC_DIR(cmd) & _IOC_READ) {
2573 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2574 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2577 * Out data can be used either for actual out data or iovs,
2578 * make sure there always is at least one page.
2580 out_size = max_t(size_t, out_size, PAGE_SIZE);
2581 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2583 /* make sure there are enough buffer pages and init request with them */
2585 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2587 while (num_pages < max_pages) {
2588 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2589 if (!pages[num_pages])
2594 req = fuse_get_req(fc, num_pages);
2600 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2601 req->num_pages = num_pages;
2602 fuse_page_descs_length_init(req, 0, req->num_pages);
2604 /* okay, let's send it to the client */
2605 req->in.h.opcode = FUSE_IOCTL;
2606 req->in.h.nodeid = ff->nodeid;
2607 req->in.numargs = 1;
2608 req->in.args[0].size = sizeof(inarg);
2609 req->in.args[0].value = &inarg;
2612 req->in.args[1].size = in_size;
2613 req->in.argpages = 1;
2615 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2621 req->out.numargs = 2;
2622 req->out.args[0].size = sizeof(outarg);
2623 req->out.args[0].value = &outarg;
2624 req->out.args[1].size = out_size;
2625 req->out.argpages = 1;
2626 req->out.argvar = 1;
2628 fuse_request_send(fc, req);
2629 err = req->out.h.error;
2630 transferred = req->out.args[1].size;
2631 fuse_put_request(fc, req);
2636 /* did it ask for retry? */
2637 if (outarg.flags & FUSE_IOCTL_RETRY) {
2640 /* no retry if in restricted mode */
2642 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2645 in_iovs = outarg.in_iovs;
2646 out_iovs = outarg.out_iovs;
2649 * Make sure things are in boundary, separate checks
2650 * are to protect against overflow.
2653 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2654 out_iovs > FUSE_IOCTL_MAX_IOV ||
2655 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2658 vaddr = kmap_atomic(pages[0]);
2659 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2660 transferred, in_iovs + out_iovs,
2661 (flags & FUSE_IOCTL_COMPAT) != 0);
2662 kunmap_atomic(vaddr);
2667 out_iov = in_iov + in_iovs;
2669 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2673 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2681 if (transferred > inarg.out_size)
2684 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2687 fuse_put_request(fc, req);
2688 free_page((unsigned long) iov_page);
2690 __free_page(pages[--num_pages]);
2693 return err ? err : outarg.result;
2695 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2697 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2698 unsigned long arg, unsigned int flags)
2700 struct inode *inode = file_inode(file);
2701 struct fuse_conn *fc = get_fuse_conn(inode);
2703 if (!fuse_allow_current_process(fc))
2706 if (is_bad_inode(inode))
2709 return fuse_do_ioctl(file, cmd, arg, flags);
2712 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2715 return fuse_ioctl_common(file, cmd, arg, 0);
2718 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2721 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2725 * All files which have been polled are linked to RB tree
2726 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2727 * find the matching one.
2729 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2730 struct rb_node **parent_out)
2732 struct rb_node **link = &fc->polled_files.rb_node;
2733 struct rb_node *last = NULL;
2736 struct fuse_file *ff;
2739 ff = rb_entry(last, struct fuse_file, polled_node);
2742 link = &last->rb_left;
2743 else if (kh > ff->kh)
2744 link = &last->rb_right;
2755 * The file is about to be polled. Make sure it's on the polled_files
2756 * RB tree. Note that files once added to the polled_files tree are
2757 * not removed before the file is released. This is because a file
2758 * polled once is likely to be polled again.
2760 static void fuse_register_polled_file(struct fuse_conn *fc,
2761 struct fuse_file *ff)
2763 spin_lock(&fc->lock);
2764 if (RB_EMPTY_NODE(&ff->polled_node)) {
2765 struct rb_node **link, *uninitialized_var(parent);
2767 link = fuse_find_polled_node(fc, ff->kh, &parent);
2769 rb_link_node(&ff->polled_node, parent, link);
2770 rb_insert_color(&ff->polled_node, &fc->polled_files);
2772 spin_unlock(&fc->lock);
2775 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2777 struct fuse_file *ff = file->private_data;
2778 struct fuse_conn *fc = ff->fc;
2779 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2780 struct fuse_poll_out outarg;
2785 return DEFAULT_POLLMASK;
2787 poll_wait(file, &ff->poll_wait, wait);
2788 inarg.events = (__u32)poll_requested_events(wait);
2791 * Ask for notification iff there's someone waiting for it.
2792 * The client may ignore the flag and always notify.
2794 if (waitqueue_active(&ff->poll_wait)) {
2795 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2796 fuse_register_polled_file(fc, ff);
2799 args.in.h.opcode = FUSE_POLL;
2800 args.in.h.nodeid = ff->nodeid;
2801 args.in.numargs = 1;
2802 args.in.args[0].size = sizeof(inarg);
2803 args.in.args[0].value = &inarg;
2804 args.out.numargs = 1;
2805 args.out.args[0].size = sizeof(outarg);
2806 args.out.args[0].value = &outarg;
2807 err = fuse_simple_request(fc, &args);
2810 return outarg.revents;
2811 if (err == -ENOSYS) {
2813 return DEFAULT_POLLMASK;
2817 EXPORT_SYMBOL_GPL(fuse_file_poll);
2820 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2821 * wakes up the poll waiters.
2823 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2824 struct fuse_notify_poll_wakeup_out *outarg)
2826 u64 kh = outarg->kh;
2827 struct rb_node **link;
2829 spin_lock(&fc->lock);
2831 link = fuse_find_polled_node(fc, kh, NULL);
2833 struct fuse_file *ff;
2835 ff = rb_entry(*link, struct fuse_file, polled_node);
2836 wake_up_interruptible_sync(&ff->poll_wait);
2839 spin_unlock(&fc->lock);
2843 static void fuse_do_truncate(struct file *file)
2845 struct inode *inode = file->f_mapping->host;
2848 attr.ia_valid = ATTR_SIZE;
2849 attr.ia_size = i_size_read(inode);
2851 attr.ia_file = file;
2852 attr.ia_valid |= ATTR_FILE;
2854 fuse_do_setattr(inode, &attr, file);
2857 static inline loff_t fuse_round_up(loff_t off)
2859 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2863 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2865 DECLARE_COMPLETION_ONSTACK(wait);
2867 struct file *file = iocb->ki_filp;
2868 struct fuse_file *ff = file->private_data;
2869 bool async_dio = ff->fc->async_dio;
2871 struct inode *inode;
2873 size_t count = iov_iter_count(iter);
2874 loff_t offset = iocb->ki_pos;
2875 struct fuse_io_priv *io;
2878 inode = file->f_mapping->host;
2879 i_size = i_size_read(inode);
2881 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2884 /* optimization for short read */
2885 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2886 if (offset >= i_size)
2888 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2889 count = iov_iter_count(iter);
2892 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2895 spin_lock_init(&io->lock);
2896 kref_init(&io->refcnt);
2900 io->offset = offset;
2901 io->write = (iov_iter_rw(iter) == WRITE);
2905 * By default, we want to optimize all I/Os with async request
2906 * submission to the client filesystem if supported.
2908 io->async = async_dio;
2910 io->blocking = is_sync_kiocb(iocb);
2913 * We cannot asynchronously extend the size of a file.
2914 * In such case the aio will behave exactly like sync io.
2916 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2917 io->blocking = true;
2919 if (io->async && io->blocking) {
2921 * Additional reference to keep io around after
2922 * calling fuse_aio_complete()
2924 kref_get(&io->refcnt);
2928 if (iov_iter_rw(iter) == WRITE) {
2929 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2930 fuse_invalidate_attr(inode);
2932 ret = __fuse_direct_read(io, iter, &pos);
2936 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2938 /* we have a non-extending, async request, so return */
2940 return -EIOCBQUEUED;
2942 wait_for_completion(&wait);
2943 ret = fuse_get_res_by_io(io);
2946 kref_put(&io->refcnt, fuse_io_release);
2948 if (iov_iter_rw(iter) == WRITE) {
2950 fuse_write_update_size(inode, pos);
2951 else if (ret < 0 && offset + count > i_size)
2952 fuse_do_truncate(file);
2958 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2961 struct fuse_file *ff = file->private_data;
2962 struct inode *inode = file_inode(file);
2963 struct fuse_inode *fi = get_fuse_inode(inode);
2964 struct fuse_conn *fc = ff->fc;
2966 struct fuse_fallocate_in inarg = {
2973 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2974 (mode & FALLOC_FL_PUNCH_HOLE);
2976 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2979 if (fc->no_fallocate)
2984 if (mode & FALLOC_FL_PUNCH_HOLE) {
2985 loff_t endbyte = offset + length - 1;
2986 err = filemap_write_and_wait_range(inode->i_mapping,
2991 fuse_sync_writes(inode);
2995 if (!(mode & FALLOC_FL_KEEP_SIZE))
2996 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2998 args.in.h.opcode = FUSE_FALLOCATE;
2999 args.in.h.nodeid = ff->nodeid;
3000 args.in.numargs = 1;
3001 args.in.args[0].size = sizeof(inarg);
3002 args.in.args[0].value = &inarg;
3003 err = fuse_simple_request(fc, &args);
3004 if (err == -ENOSYS) {
3005 fc->no_fallocate = 1;
3011 /* we could have extended the file */
3012 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3013 bool changed = fuse_write_update_size(inode, offset + length);
3015 if (changed && fc->writeback_cache)
3016 file_update_time(file);
3019 if (mode & FALLOC_FL_PUNCH_HOLE)
3020 truncate_pagecache_range(inode, offset, offset + length - 1);
3022 fuse_invalidate_attr(inode);
3025 if (!(mode & FALLOC_FL_KEEP_SIZE))
3026 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3029 inode_unlock(inode);
3034 static const struct file_operations fuse_file_operations = {
3035 .llseek = fuse_file_llseek,
3036 .read_iter = fuse_file_read_iter,
3037 .write_iter = fuse_file_write_iter,
3038 .mmap = fuse_file_mmap,
3040 .flush = fuse_flush,
3041 .release = fuse_release,
3042 .fsync = fuse_fsync,
3043 .lock = fuse_file_lock,
3044 .flock = fuse_file_flock,
3045 .splice_read = generic_file_splice_read,
3046 .unlocked_ioctl = fuse_file_ioctl,
3047 .compat_ioctl = fuse_file_compat_ioctl,
3048 .poll = fuse_file_poll,
3049 .fallocate = fuse_file_fallocate,
3052 static const struct file_operations fuse_direct_io_file_operations = {
3053 .llseek = fuse_file_llseek,
3054 .read_iter = fuse_direct_read_iter,
3055 .write_iter = fuse_direct_write_iter,
3056 .mmap = fuse_direct_mmap,
3058 .flush = fuse_flush,
3059 .release = fuse_release,
3060 .fsync = fuse_fsync,
3061 .lock = fuse_file_lock,
3062 .flock = fuse_file_flock,
3063 .unlocked_ioctl = fuse_file_ioctl,
3064 .compat_ioctl = fuse_file_compat_ioctl,
3065 .poll = fuse_file_poll,
3066 .fallocate = fuse_file_fallocate,
3067 /* no splice_read */
3070 static const struct address_space_operations fuse_file_aops = {
3071 .readpage = fuse_readpage,
3072 .writepage = fuse_writepage,
3073 .writepages = fuse_writepages,
3074 .launder_page = fuse_launder_page,
3075 .readpages = fuse_readpages,
3076 .set_page_dirty = __set_page_dirty_nobuffers,
3078 .direct_IO = fuse_direct_IO,
3079 .write_begin = fuse_write_begin,
3080 .write_end = fuse_write_end,
3083 void fuse_init_file_inode(struct inode *inode)
3085 inode->i_fop = &fuse_file_operations;
3086 inode->i_data.a_ops = &fuse_file_aops;