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 req = fuse_get_req_nofail_nopages(fc, file);
421 memset(&inarg, 0, sizeof(inarg));
423 inarg.lock_owner = fuse_lock_owner_id(fc, id);
424 req->in.h.opcode = FUSE_FLUSH;
425 req->in.h.nodeid = get_node_id(inode);
427 req->in.args[0].size = sizeof(inarg);
428 req->in.args[0].value = &inarg;
429 __set_bit(FR_FORCE, &req->flags);
430 fuse_request_send(fc, req);
431 err = req->out.h.error;
432 fuse_put_request(fc, req);
433 if (err == -ENOSYS) {
440 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
441 int datasync, int isdir)
443 struct inode *inode = file->f_mapping->host;
444 struct fuse_conn *fc = get_fuse_conn(inode);
445 struct fuse_file *ff = file->private_data;
447 struct fuse_fsync_in inarg;
450 if (is_bad_inode(inode))
456 * Start writeback against all dirty pages of the inode, then
457 * wait for all outstanding writes, before sending the FSYNC
460 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
464 fuse_sync_writes(inode);
465 err = sync_inode_metadata(inode, 1);
469 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
472 memset(&inarg, 0, sizeof(inarg));
474 inarg.fsync_flags = datasync ? 1 : 0;
475 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
476 args.in.h.nodeid = get_node_id(inode);
478 args.in.args[0].size = sizeof(inarg);
479 args.in.args[0].value = &inarg;
480 err = fuse_simple_request(fc, &args);
481 if (err == -ENOSYS) {
493 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
496 return fuse_fsync_common(file, start, end, datasync, 0);
499 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
500 size_t count, int opcode)
502 struct fuse_read_in *inarg = &req->misc.read.in;
503 struct fuse_file *ff = file->private_data;
508 inarg->flags = file->f_flags;
509 req->in.h.opcode = opcode;
510 req->in.h.nodeid = ff->nodeid;
512 req->in.args[0].size = sizeof(struct fuse_read_in);
513 req->in.args[0].value = inarg;
515 req->out.numargs = 1;
516 req->out.args[0].size = count;
519 static void fuse_release_user_pages(struct fuse_req *req, int write)
523 for (i = 0; i < req->num_pages; i++) {
524 struct page *page = req->pages[i];
526 set_page_dirty_lock(page);
531 static void fuse_io_release(struct kref *kref)
533 kfree(container_of(kref, struct fuse_io_priv, refcnt));
536 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
541 if (io->bytes >= 0 && io->write)
544 return io->bytes < 0 ? io->size : io->bytes;
548 * In case of short read, the caller sets 'pos' to the position of
549 * actual end of fuse request in IO request. Otherwise, if bytes_requested
550 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
553 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
554 * both submitted asynchronously. The first of them was ACKed by userspace as
555 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
556 * second request was ACKed as short, e.g. only 1K was read, resulting in
559 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
560 * will be equal to the length of the longest contiguous fragment of
561 * transferred data starting from the beginning of IO request.
563 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
567 spin_lock(&io->lock);
569 io->err = io->err ? : err;
570 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
574 if (!left && io->blocking)
576 spin_unlock(&io->lock);
578 if (!left && !io->blocking) {
579 ssize_t res = fuse_get_res_by_io(io);
582 struct inode *inode = file_inode(io->iocb->ki_filp);
583 struct fuse_conn *fc = get_fuse_conn(inode);
584 struct fuse_inode *fi = get_fuse_inode(inode);
586 spin_lock(&fc->lock);
587 fi->attr_version = ++fc->attr_version;
588 spin_unlock(&fc->lock);
591 io->iocb->ki_complete(io->iocb, res, 0);
594 kref_put(&io->refcnt, fuse_io_release);
597 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
599 struct fuse_io_priv *io = req->io;
602 fuse_release_user_pages(req, !io->write);
605 if (req->misc.write.in.size != req->misc.write.out.size)
606 pos = req->misc.write.in.offset - io->offset +
607 req->misc.write.out.size;
609 if (req->misc.read.in.size != req->out.args[0].size)
610 pos = req->misc.read.in.offset - io->offset +
611 req->out.args[0].size;
614 fuse_aio_complete(io, req->out.h.error, pos);
617 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
618 size_t num_bytes, struct fuse_io_priv *io)
620 spin_lock(&io->lock);
621 kref_get(&io->refcnt);
622 io->size += num_bytes;
624 spin_unlock(&io->lock);
627 req->end = fuse_aio_complete_req;
629 __fuse_get_request(req);
630 fuse_request_send_background(fc, req);
635 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
636 loff_t pos, size_t count, fl_owner_t owner)
638 struct file *file = io->file;
639 struct fuse_file *ff = file->private_data;
640 struct fuse_conn *fc = ff->fc;
642 fuse_read_fill(req, file, pos, count, FUSE_READ);
644 struct fuse_read_in *inarg = &req->misc.read.in;
646 inarg->read_flags |= FUSE_READ_LOCKOWNER;
647 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
651 return fuse_async_req_send(fc, req, count, io);
653 fuse_request_send(fc, req);
654 return req->out.args[0].size;
657 static void fuse_read_update_size(struct inode *inode, loff_t size,
660 struct fuse_conn *fc = get_fuse_conn(inode);
661 struct fuse_inode *fi = get_fuse_inode(inode);
663 spin_lock(&fc->lock);
664 if (attr_ver == fi->attr_version && size < inode->i_size &&
665 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
666 fi->attr_version = ++fc->attr_version;
667 i_size_write(inode, size);
669 spin_unlock(&fc->lock);
672 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
675 size_t num_read = req->out.args[0].size;
676 struct fuse_conn *fc = get_fuse_conn(inode);
678 if (fc->writeback_cache) {
680 * A hole in a file. Some data after the hole are in page cache,
681 * but have not reached the client fs yet. So, the hole is not
685 int start_idx = num_read >> PAGE_SHIFT;
686 size_t off = num_read & (PAGE_SIZE - 1);
688 for (i = start_idx; i < req->num_pages; i++) {
689 zero_user_segment(req->pages[i], off, PAGE_SIZE);
693 loff_t pos = page_offset(req->pages[0]) + num_read;
694 fuse_read_update_size(inode, pos, attr_ver);
698 static int fuse_do_readpage(struct file *file, struct page *page)
700 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
701 struct inode *inode = page->mapping->host;
702 struct fuse_conn *fc = get_fuse_conn(inode);
703 struct fuse_req *req;
705 loff_t pos = page_offset(page);
706 size_t count = PAGE_SIZE;
711 * Page writeback can extend beyond the lifetime of the
712 * page-cache page, so make sure we read a properly synced
715 fuse_wait_on_page_writeback(inode, page->index);
717 req = fuse_get_req(fc, 1);
721 attr_ver = fuse_get_attr_version(fc);
723 req->out.page_zeroing = 1;
724 req->out.argpages = 1;
726 req->pages[0] = page;
727 req->page_descs[0].length = count;
728 num_read = fuse_send_read(req, &io, pos, count, NULL);
729 err = req->out.h.error;
733 * Short read means EOF. If file size is larger, truncate it
735 if (num_read < count)
736 fuse_short_read(req, inode, attr_ver);
738 SetPageUptodate(page);
741 fuse_put_request(fc, req);
746 static int fuse_readpage(struct file *file, struct page *page)
748 struct inode *inode = page->mapping->host;
752 if (is_bad_inode(inode))
755 err = fuse_do_readpage(file, page);
756 fuse_invalidate_atime(inode);
762 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
765 size_t count = req->misc.read.in.size;
766 size_t num_read = req->out.args[0].size;
767 struct address_space *mapping = NULL;
769 for (i = 0; mapping == NULL && i < req->num_pages; i++)
770 mapping = req->pages[i]->mapping;
773 struct inode *inode = mapping->host;
776 * Short read means EOF. If file size is larger, truncate it
778 if (!req->out.h.error && num_read < count)
779 fuse_short_read(req, inode, req->misc.read.attr_ver);
781 fuse_invalidate_atime(inode);
784 for (i = 0; i < req->num_pages; i++) {
785 struct page *page = req->pages[i];
786 if (!req->out.h.error)
787 SetPageUptodate(page);
794 fuse_file_put(req->ff, false);
797 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
799 struct fuse_file *ff = file->private_data;
800 struct fuse_conn *fc = ff->fc;
801 loff_t pos = page_offset(req->pages[0]);
802 size_t count = req->num_pages << PAGE_SHIFT;
804 req->out.argpages = 1;
805 req->out.page_zeroing = 1;
806 req->out.page_replace = 1;
807 fuse_read_fill(req, file, pos, count, FUSE_READ);
808 req->misc.read.attr_ver = fuse_get_attr_version(fc);
809 if (fc->async_read) {
810 req->ff = fuse_file_get(ff);
811 req->end = fuse_readpages_end;
812 fuse_request_send_background(fc, req);
814 fuse_request_send(fc, req);
815 fuse_readpages_end(fc, req);
816 fuse_put_request(fc, req);
820 struct fuse_fill_data {
821 struct fuse_req *req;
827 static int fuse_readpages_fill(void *_data, struct page *page)
829 struct fuse_fill_data *data = _data;
830 struct fuse_req *req = data->req;
831 struct inode *inode = data->inode;
832 struct fuse_conn *fc = get_fuse_conn(inode);
834 fuse_wait_on_page_writeback(inode, page->index);
836 if (req->num_pages &&
837 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
838 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
839 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
840 int nr_alloc = min_t(unsigned, data->nr_pages,
841 FUSE_MAX_PAGES_PER_REQ);
842 fuse_send_readpages(req, data->file);
844 req = fuse_get_req_for_background(fc, nr_alloc);
846 req = fuse_get_req(fc, nr_alloc);
855 if (WARN_ON(req->num_pages >= req->max_pages)) {
856 fuse_put_request(fc, req);
861 req->pages[req->num_pages] = page;
862 req->page_descs[req->num_pages].length = PAGE_SIZE;
868 static int fuse_readpages(struct file *file, struct address_space *mapping,
869 struct list_head *pages, unsigned nr_pages)
871 struct inode *inode = mapping->host;
872 struct fuse_conn *fc = get_fuse_conn(inode);
873 struct fuse_fill_data data;
875 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
878 if (is_bad_inode(inode))
884 data.req = fuse_get_req_for_background(fc, nr_alloc);
886 data.req = fuse_get_req(fc, nr_alloc);
887 data.nr_pages = nr_pages;
888 err = PTR_ERR(data.req);
889 if (IS_ERR(data.req))
892 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
894 if (data.req->num_pages)
895 fuse_send_readpages(data.req, file);
897 fuse_put_request(fc, data.req);
903 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
905 struct inode *inode = iocb->ki_filp->f_mapping->host;
906 struct fuse_conn *fc = get_fuse_conn(inode);
909 * In auto invalidate mode, always update attributes on read.
910 * Otherwise, only update if we attempt to read past EOF (to ensure
911 * i_size is up to date).
913 if (fc->auto_inval_data ||
914 (iocb->ki_pos + iov_iter_count(to) > i_size_read(inode))) {
916 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
921 return generic_file_read_iter(iocb, to);
924 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
925 loff_t pos, size_t count)
927 struct fuse_write_in *inarg = &req->misc.write.in;
928 struct fuse_write_out *outarg = &req->misc.write.out;
933 req->in.h.opcode = FUSE_WRITE;
934 req->in.h.nodeid = ff->nodeid;
936 if (ff->fc->minor < 9)
937 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
939 req->in.args[0].size = sizeof(struct fuse_write_in);
940 req->in.args[0].value = inarg;
941 req->in.args[1].size = count;
942 req->out.numargs = 1;
943 req->out.args[0].size = sizeof(struct fuse_write_out);
944 req->out.args[0].value = outarg;
947 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
948 loff_t pos, size_t count, fl_owner_t owner)
950 struct file *file = io->file;
951 struct fuse_file *ff = file->private_data;
952 struct fuse_conn *fc = ff->fc;
953 struct fuse_write_in *inarg = &req->misc.write.in;
955 fuse_write_fill(req, ff, pos, count);
956 inarg->flags = file->f_flags;
958 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
959 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
963 return fuse_async_req_send(fc, req, count, io);
965 fuse_request_send(fc, req);
966 return req->misc.write.out.size;
969 bool fuse_write_update_size(struct inode *inode, loff_t pos)
971 struct fuse_conn *fc = get_fuse_conn(inode);
972 struct fuse_inode *fi = get_fuse_inode(inode);
975 spin_lock(&fc->lock);
976 fi->attr_version = ++fc->attr_version;
977 if (pos > inode->i_size) {
978 i_size_write(inode, pos);
981 spin_unlock(&fc->lock);
986 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
987 struct inode *inode, loff_t pos,
993 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
995 for (i = 0; i < req->num_pages; i++)
996 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
998 res = fuse_send_write(req, &io, pos, count, NULL);
1000 offset = req->page_descs[0].offset;
1002 for (i = 0; i < req->num_pages; i++) {
1003 struct page *page = req->pages[i];
1005 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1006 SetPageUptodate(page);
1008 if (count > PAGE_SIZE - offset)
1009 count -= PAGE_SIZE - offset;
1021 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1022 struct address_space *mapping,
1023 struct iov_iter *ii, loff_t pos)
1025 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1026 unsigned offset = pos & (PAGE_SIZE - 1);
1030 req->in.argpages = 1;
1031 req->page_descs[0].offset = offset;
1036 pgoff_t index = pos >> PAGE_SHIFT;
1037 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1038 iov_iter_count(ii));
1040 bytes = min_t(size_t, bytes, fc->max_write - count);
1044 if (iov_iter_fault_in_readable(ii, bytes))
1048 page = grab_cache_page_write_begin(mapping, index, 0);
1052 if (mapping_writably_mapped(mapping))
1053 flush_dcache_page(page);
1055 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1056 flush_dcache_page(page);
1058 iov_iter_advance(ii, tmp);
1062 bytes = min(bytes, iov_iter_single_seg_count(ii));
1067 req->pages[req->num_pages] = page;
1068 req->page_descs[req->num_pages].length = tmp;
1074 if (offset == PAGE_SIZE)
1077 if (!fc->big_writes)
1079 } while (iov_iter_count(ii) && count < fc->max_write &&
1080 req->num_pages < req->max_pages && offset == 0);
1082 return count > 0 ? count : err;
1085 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1087 return min_t(unsigned,
1088 ((pos + len - 1) >> PAGE_SHIFT) -
1089 (pos >> PAGE_SHIFT) + 1,
1090 FUSE_MAX_PAGES_PER_REQ);
1093 static ssize_t fuse_perform_write(struct file *file,
1094 struct address_space *mapping,
1095 struct iov_iter *ii, loff_t pos)
1097 struct inode *inode = mapping->host;
1098 struct fuse_conn *fc = get_fuse_conn(inode);
1099 struct fuse_inode *fi = get_fuse_inode(inode);
1103 if (is_bad_inode(inode))
1106 if (inode->i_size < pos + iov_iter_count(ii))
1107 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1110 struct fuse_req *req;
1112 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1114 req = fuse_get_req(fc, nr_pages);
1120 count = fuse_fill_write_pages(req, mapping, ii, pos);
1126 num_written = fuse_send_write_pages(req, file, inode,
1128 err = req->out.h.error;
1133 /* break out of the loop on short write */
1134 if (num_written != count)
1138 fuse_put_request(fc, req);
1139 } while (!err && iov_iter_count(ii));
1142 fuse_write_update_size(inode, pos);
1144 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1145 fuse_invalidate_attr(inode);
1147 return res > 0 ? res : err;
1150 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1152 struct file *file = iocb->ki_filp;
1153 struct address_space *mapping = file->f_mapping;
1154 ssize_t written = 0;
1155 ssize_t written_buffered = 0;
1156 struct inode *inode = mapping->host;
1160 if (get_fuse_conn(inode)->writeback_cache) {
1161 /* Update size (EOF optimization) and mode (SUID clearing) */
1162 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1166 return generic_file_write_iter(iocb, from);
1171 /* We can write back this queue in page reclaim */
1172 current->backing_dev_info = inode_to_bdi(inode);
1174 err = generic_write_checks(iocb, from);
1178 err = file_remove_privs(file);
1182 err = file_update_time(file);
1186 if (iocb->ki_flags & IOCB_DIRECT) {
1187 loff_t pos = iocb->ki_pos;
1188 written = generic_file_direct_write(iocb, from);
1189 if (written < 0 || !iov_iter_count(from))
1194 written_buffered = fuse_perform_write(file, mapping, from, pos);
1195 if (written_buffered < 0) {
1196 err = written_buffered;
1199 endbyte = pos + written_buffered - 1;
1201 err = filemap_write_and_wait_range(file->f_mapping, pos,
1206 invalidate_mapping_pages(file->f_mapping,
1208 endbyte >> PAGE_SHIFT);
1210 written += written_buffered;
1211 iocb->ki_pos = pos + written_buffered;
1213 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
1215 iocb->ki_pos += written;
1218 current->backing_dev_info = NULL;
1219 inode_unlock(inode);
1221 return written ? written : err;
1224 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1225 unsigned index, unsigned nr_pages)
1229 for (i = index; i < index + nr_pages; i++)
1230 req->page_descs[i].length = PAGE_SIZE -
1231 req->page_descs[i].offset;
1234 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1236 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1239 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1242 return min(iov_iter_single_seg_count(ii), max_size);
1245 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1246 size_t *nbytesp, int write)
1248 size_t nbytes = 0; /* # bytes already packed in req */
1251 /* Special case for kernel I/O: can copy directly into the buffer */
1252 if (ii->type & ITER_KVEC) {
1253 unsigned long user_addr = fuse_get_user_addr(ii);
1254 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1257 req->in.args[1].value = (void *) user_addr;
1259 req->out.args[0].value = (void *) user_addr;
1261 iov_iter_advance(ii, frag_size);
1262 *nbytesp = frag_size;
1266 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1269 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1271 req->max_pages - req->num_pages,
1276 iov_iter_advance(ii, ret);
1280 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1282 req->page_descs[req->num_pages].offset = start;
1283 fuse_page_descs_length_init(req, req->num_pages, npages);
1285 req->num_pages += npages;
1286 req->page_descs[req->num_pages - 1].length -=
1287 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1291 req->in.argpages = 1;
1293 req->out.argpages = 1;
1297 return ret < 0 ? ret : 0;
1300 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1302 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1305 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1306 loff_t *ppos, int flags)
1308 int write = flags & FUSE_DIO_WRITE;
1309 int cuse = flags & FUSE_DIO_CUSE;
1310 struct file *file = io->file;
1311 struct inode *inode = file->f_mapping->host;
1312 struct fuse_file *ff = file->private_data;
1313 struct fuse_conn *fc = ff->fc;
1314 size_t nmax = write ? fc->max_write : fc->max_read;
1316 size_t count = iov_iter_count(iter);
1317 pgoff_t idx_from = pos >> PAGE_SHIFT;
1318 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1320 struct fuse_req *req;
1324 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1326 req = fuse_get_req(fc, fuse_iter_npages(iter));
1328 return PTR_ERR(req);
1330 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1333 fuse_sync_writes(inode);
1335 inode_unlock(inode);
1340 fl_owner_t owner = current->files;
1341 size_t nbytes = min(count, nmax);
1342 err = fuse_get_user_pages(req, iter, &nbytes, write);
1347 nres = fuse_send_write(req, io, pos, nbytes, owner);
1349 nres = fuse_send_read(req, io, pos, nbytes, owner);
1352 fuse_release_user_pages(req, !write);
1353 if (req->out.h.error) {
1354 err = req->out.h.error;
1356 } else if (nres > nbytes) {
1367 fuse_put_request(fc, req);
1369 req = fuse_get_req_for_background(fc,
1370 fuse_iter_npages(iter));
1372 req = fuse_get_req(fc, fuse_iter_npages(iter));
1378 fuse_put_request(fc, req);
1382 return res > 0 ? res : err;
1384 EXPORT_SYMBOL_GPL(fuse_direct_io);
1386 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1387 struct iov_iter *iter,
1391 struct file *file = io->file;
1392 struct inode *inode = file_inode(file);
1394 if (is_bad_inode(inode))
1397 res = fuse_direct_io(io, iter, ppos, 0);
1399 fuse_invalidate_attr(inode);
1404 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1406 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
1407 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1410 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1412 struct file *file = iocb->ki_filp;
1413 struct inode *inode = file_inode(file);
1414 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1417 if (is_bad_inode(inode))
1420 /* Don't allow parallel writes to the same file */
1422 res = generic_write_checks(iocb, from);
1424 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1425 fuse_invalidate_attr(inode);
1427 fuse_write_update_size(inode, iocb->ki_pos);
1428 inode_unlock(inode);
1433 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1437 for (i = 0; i < req->num_pages; i++)
1438 __free_page(req->pages[i]);
1441 fuse_file_put(req->ff, false);
1444 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1446 struct inode *inode = req->inode;
1447 struct fuse_inode *fi = get_fuse_inode(inode);
1448 struct backing_dev_info *bdi = inode_to_bdi(inode);
1451 list_del(&req->writepages_entry);
1452 for (i = 0; i < req->num_pages; i++) {
1453 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1454 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1455 wb_writeout_inc(&bdi->wb);
1457 wake_up(&fi->page_waitq);
1460 /* Called under fc->lock, may release and reacquire it */
1461 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1463 __releases(fc->lock)
1464 __acquires(fc->lock)
1466 struct fuse_inode *fi = get_fuse_inode(req->inode);
1467 struct fuse_write_in *inarg = &req->misc.write.in;
1468 __u64 data_size = req->num_pages * PAGE_SIZE;
1473 if (inarg->offset + data_size <= size) {
1474 inarg->size = data_size;
1475 } else if (inarg->offset < size) {
1476 inarg->size = size - inarg->offset;
1478 /* Got truncated off completely */
1482 req->in.args[1].size = inarg->size;
1484 fuse_request_send_background_locked(fc, req);
1488 fuse_writepage_finish(fc, req);
1489 spin_unlock(&fc->lock);
1490 fuse_writepage_free(fc, req);
1491 fuse_put_request(fc, req);
1492 spin_lock(&fc->lock);
1496 * If fi->writectr is positive (no truncate or fsync going on) send
1497 * all queued writepage requests.
1499 * Called with fc->lock
1501 void fuse_flush_writepages(struct inode *inode)
1502 __releases(fc->lock)
1503 __acquires(fc->lock)
1505 struct fuse_conn *fc = get_fuse_conn(inode);
1506 struct fuse_inode *fi = get_fuse_inode(inode);
1507 size_t crop = i_size_read(inode);
1508 struct fuse_req *req;
1510 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1511 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1512 list_del_init(&req->list);
1513 fuse_send_writepage(fc, req, crop);
1517 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1519 struct inode *inode = req->inode;
1520 struct fuse_inode *fi = get_fuse_inode(inode);
1522 mapping_set_error(inode->i_mapping, req->out.h.error);
1523 spin_lock(&fc->lock);
1524 while (req->misc.write.next) {
1525 struct fuse_conn *fc = get_fuse_conn(inode);
1526 struct fuse_write_in *inarg = &req->misc.write.in;
1527 struct fuse_req *next = req->misc.write.next;
1528 req->misc.write.next = next->misc.write.next;
1529 next->misc.write.next = NULL;
1530 next->ff = fuse_file_get(req->ff);
1531 list_add(&next->writepages_entry, &fi->writepages);
1534 * Skip fuse_flush_writepages() to make it easy to crop requests
1535 * based on primary request size.
1537 * 1st case (trivial): there are no concurrent activities using
1538 * fuse_set/release_nowrite. Then we're on safe side because
1539 * fuse_flush_writepages() would call fuse_send_writepage()
1542 * 2nd case: someone called fuse_set_nowrite and it is waiting
1543 * now for completion of all in-flight requests. This happens
1544 * rarely and no more than once per page, so this should be
1547 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1548 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1549 * that fuse_set_nowrite returned implies that all in-flight
1550 * requests were completed along with all of their secondary
1551 * requests. Further primary requests are blocked by negative
1552 * writectr. Hence there cannot be any in-flight requests and
1553 * no invocations of fuse_writepage_end() while we're in
1554 * fuse_set_nowrite..fuse_release_nowrite section.
1556 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1559 fuse_writepage_finish(fc, req);
1560 spin_unlock(&fc->lock);
1561 fuse_writepage_free(fc, req);
1564 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1565 struct fuse_inode *fi)
1567 struct fuse_file *ff = NULL;
1569 spin_lock(&fc->lock);
1570 if (!list_empty(&fi->write_files)) {
1571 ff = list_entry(fi->write_files.next, struct fuse_file,
1575 spin_unlock(&fc->lock);
1580 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1581 struct fuse_inode *fi)
1583 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1588 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1590 struct fuse_conn *fc = get_fuse_conn(inode);
1591 struct fuse_inode *fi = get_fuse_inode(inode);
1592 struct fuse_file *ff;
1595 ff = __fuse_write_file_get(fc, fi);
1596 err = fuse_flush_times(inode, ff);
1598 fuse_file_put(ff, 0);
1603 static int fuse_writepage_locked(struct page *page)
1605 struct address_space *mapping = page->mapping;
1606 struct inode *inode = mapping->host;
1607 struct fuse_conn *fc = get_fuse_conn(inode);
1608 struct fuse_inode *fi = get_fuse_inode(inode);
1609 struct fuse_req *req;
1610 struct page *tmp_page;
1611 int error = -ENOMEM;
1613 set_page_writeback(page);
1615 req = fuse_request_alloc_nofs(1);
1619 /* writeback always goes to bg_queue */
1620 __set_bit(FR_BACKGROUND, &req->flags);
1621 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1626 req->ff = fuse_write_file_get(fc, fi);
1630 fuse_write_fill(req, req->ff, page_offset(page), 0);
1632 copy_highpage(tmp_page, page);
1633 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1634 req->misc.write.next = NULL;
1635 req->in.argpages = 1;
1637 req->pages[0] = tmp_page;
1638 req->page_descs[0].offset = 0;
1639 req->page_descs[0].length = PAGE_SIZE;
1640 req->end = fuse_writepage_end;
1643 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1644 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1646 spin_lock(&fc->lock);
1647 list_add(&req->writepages_entry, &fi->writepages);
1648 list_add_tail(&req->list, &fi->queued_writes);
1649 fuse_flush_writepages(inode);
1650 spin_unlock(&fc->lock);
1652 end_page_writeback(page);
1657 __free_page(tmp_page);
1659 fuse_request_free(req);
1661 end_page_writeback(page);
1665 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1669 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1671 * ->writepages() should be called for sync() and friends. We
1672 * should only get here on direct reclaim and then we are
1673 * allowed to skip a page which is already in flight
1675 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1677 redirty_page_for_writepage(wbc, page);
1681 err = fuse_writepage_locked(page);
1687 struct fuse_fill_wb_data {
1688 struct fuse_req *req;
1689 struct fuse_file *ff;
1690 struct inode *inode;
1691 struct page **orig_pages;
1694 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1696 struct fuse_req *req = data->req;
1697 struct inode *inode = data->inode;
1698 struct fuse_conn *fc = get_fuse_conn(inode);
1699 struct fuse_inode *fi = get_fuse_inode(inode);
1700 int num_pages = req->num_pages;
1703 req->ff = fuse_file_get(data->ff);
1704 spin_lock(&fc->lock);
1705 list_add_tail(&req->list, &fi->queued_writes);
1706 fuse_flush_writepages(inode);
1707 spin_unlock(&fc->lock);
1709 for (i = 0; i < num_pages; i++)
1710 end_page_writeback(data->orig_pages[i]);
1713 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1716 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1717 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1718 struct fuse_req *tmp;
1719 struct fuse_req *old_req;
1723 BUG_ON(new_req->num_pages != 0);
1725 spin_lock(&fc->lock);
1726 list_del(&new_req->writepages_entry);
1727 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1728 BUG_ON(old_req->inode != new_req->inode);
1729 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1730 if (curr_index <= page->index &&
1731 page->index < curr_index + old_req->num_pages) {
1737 list_add(&new_req->writepages_entry, &fi->writepages);
1741 new_req->num_pages = 1;
1742 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1743 BUG_ON(tmp->inode != new_req->inode);
1744 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1745 if (tmp->num_pages == 1 &&
1746 curr_index == page->index) {
1751 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1752 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1754 copy_highpage(old_req->pages[0], page);
1755 spin_unlock(&fc->lock);
1757 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1758 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
1759 wb_writeout_inc(&bdi->wb);
1760 fuse_writepage_free(fc, new_req);
1761 fuse_request_free(new_req);
1764 new_req->misc.write.next = old_req->misc.write.next;
1765 old_req->misc.write.next = new_req;
1768 spin_unlock(&fc->lock);
1773 static int fuse_writepages_fill(struct page *page,
1774 struct writeback_control *wbc, void *_data)
1776 struct fuse_fill_wb_data *data = _data;
1777 struct fuse_req *req = data->req;
1778 struct inode *inode = data->inode;
1779 struct fuse_conn *fc = get_fuse_conn(inode);
1780 struct page *tmp_page;
1786 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1792 * Being under writeback is unlikely but possible. For example direct
1793 * read to an mmaped fuse file will set the page dirty twice; once when
1794 * the pages are faulted with get_user_pages(), and then after the read
1797 is_writeback = fuse_page_is_writeback(inode, page->index);
1799 if (req && req->num_pages &&
1800 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1801 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1802 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1803 fuse_writepages_send(data);
1807 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1812 * The page must not be redirtied until the writeout is completed
1813 * (i.e. userspace has sent a reply to the write request). Otherwise
1814 * there could be more than one temporary page instance for each real
1817 * This is ensured by holding the page lock in page_mkwrite() while
1818 * checking fuse_page_is_writeback(). We already hold the page lock
1819 * since clear_page_dirty_for_io() and keep it held until we add the
1820 * request to the fi->writepages list and increment req->num_pages.
1821 * After this fuse_page_is_writeback() will indicate that the page is
1822 * under writeback, so we can release the page lock.
1824 if (data->req == NULL) {
1825 struct fuse_inode *fi = get_fuse_inode(inode);
1828 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1830 __free_page(tmp_page);
1834 fuse_write_fill(req, data->ff, page_offset(page), 0);
1835 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1836 req->misc.write.next = NULL;
1837 req->in.argpages = 1;
1838 __set_bit(FR_BACKGROUND, &req->flags);
1840 req->end = fuse_writepage_end;
1843 spin_lock(&fc->lock);
1844 list_add(&req->writepages_entry, &fi->writepages);
1845 spin_unlock(&fc->lock);
1849 set_page_writeback(page);
1851 copy_highpage(tmp_page, page);
1852 req->pages[req->num_pages] = tmp_page;
1853 req->page_descs[req->num_pages].offset = 0;
1854 req->page_descs[req->num_pages].length = PAGE_SIZE;
1856 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1857 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1860 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1861 end_page_writeback(page);
1865 data->orig_pages[req->num_pages] = page;
1868 * Protected by fc->lock against concurrent access by
1869 * fuse_page_is_writeback().
1871 spin_lock(&fc->lock);
1873 spin_unlock(&fc->lock);
1881 static int fuse_writepages(struct address_space *mapping,
1882 struct writeback_control *wbc)
1884 struct inode *inode = mapping->host;
1885 struct fuse_fill_wb_data data;
1889 if (is_bad_inode(inode))
1897 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1898 sizeof(struct page *),
1900 if (!data.orig_pages)
1903 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1905 /* Ignore errors if we can write at least one page */
1906 BUG_ON(!data.req->num_pages);
1907 fuse_writepages_send(&data);
1911 fuse_file_put(data.ff, false);
1913 kfree(data.orig_pages);
1919 * It's worthy to make sure that space is reserved on disk for the write,
1920 * but how to implement it without killing performance need more thinking.
1922 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1923 loff_t pos, unsigned len, unsigned flags,
1924 struct page **pagep, void **fsdata)
1926 pgoff_t index = pos >> PAGE_SHIFT;
1927 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1932 WARN_ON(!fc->writeback_cache);
1934 page = grab_cache_page_write_begin(mapping, index, flags);
1938 fuse_wait_on_page_writeback(mapping->host, page->index);
1940 if (PageUptodate(page) || len == PAGE_SIZE)
1943 * Check if the start this page comes after the end of file, in which
1944 * case the readpage can be optimized away.
1946 fsize = i_size_read(mapping->host);
1947 if (fsize <= (pos & PAGE_MASK)) {
1948 size_t off = pos & ~PAGE_MASK;
1950 zero_user_segment(page, 0, off);
1953 err = fuse_do_readpage(file, page);
1967 static int fuse_write_end(struct file *file, struct address_space *mapping,
1968 loff_t pos, unsigned len, unsigned copied,
1969 struct page *page, void *fsdata)
1971 struct inode *inode = page->mapping->host;
1973 if (!PageUptodate(page)) {
1974 /* Zero any unwritten bytes at the end of the page */
1975 size_t endoff = (pos + copied) & ~PAGE_MASK;
1977 zero_user_segment(page, endoff, PAGE_SIZE);
1978 SetPageUptodate(page);
1981 fuse_write_update_size(inode, pos + copied);
1982 set_page_dirty(page);
1989 static int fuse_launder_page(struct page *page)
1992 if (clear_page_dirty_for_io(page)) {
1993 struct inode *inode = page->mapping->host;
1994 err = fuse_writepage_locked(page);
1996 fuse_wait_on_page_writeback(inode, page->index);
2002 * Write back dirty pages now, because there may not be any suitable
2005 static void fuse_vma_close(struct vm_area_struct *vma)
2007 filemap_write_and_wait(vma->vm_file->f_mapping);
2011 * Wait for writeback against this page to complete before allowing it
2012 * to be marked dirty again, and hence written back again, possibly
2013 * before the previous writepage completed.
2015 * Block here, instead of in ->writepage(), so that the userspace fs
2016 * can only block processes actually operating on the filesystem.
2018 * Otherwise unprivileged userspace fs would be able to block
2023 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2025 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2027 struct page *page = vmf->page;
2028 struct inode *inode = file_inode(vma->vm_file);
2030 file_update_time(vma->vm_file);
2032 if (page->mapping != inode->i_mapping) {
2034 return VM_FAULT_NOPAGE;
2037 fuse_wait_on_page_writeback(inode, page->index);
2038 return VM_FAULT_LOCKED;
2041 static const struct vm_operations_struct fuse_file_vm_ops = {
2042 .close = fuse_vma_close,
2043 .fault = filemap_fault,
2044 .map_pages = filemap_map_pages,
2045 .page_mkwrite = fuse_page_mkwrite,
2048 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2050 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2051 fuse_link_write_file(file);
2053 file_accessed(file);
2054 vma->vm_ops = &fuse_file_vm_ops;
2058 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2060 /* Can't provide the coherency needed for MAP_SHARED */
2061 if (vma->vm_flags & VM_MAYSHARE)
2064 invalidate_inode_pages2(file->f_mapping);
2066 return generic_file_mmap(file, vma);
2069 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2070 struct file_lock *fl)
2072 switch (ffl->type) {
2078 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2079 ffl->end < ffl->start)
2082 fl->fl_start = ffl->start;
2083 fl->fl_end = ffl->end;
2084 fl->fl_pid = ffl->pid;
2090 fl->fl_type = ffl->type;
2094 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2095 const struct file_lock *fl, int opcode, pid_t pid,
2096 int flock, struct fuse_lk_in *inarg)
2098 struct inode *inode = file_inode(file);
2099 struct fuse_conn *fc = get_fuse_conn(inode);
2100 struct fuse_file *ff = file->private_data;
2102 memset(inarg, 0, sizeof(*inarg));
2104 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2105 inarg->lk.start = fl->fl_start;
2106 inarg->lk.end = fl->fl_end;
2107 inarg->lk.type = fl->fl_type;
2108 inarg->lk.pid = pid;
2110 inarg->lk_flags |= FUSE_LK_FLOCK;
2111 args->in.h.opcode = opcode;
2112 args->in.h.nodeid = get_node_id(inode);
2113 args->in.numargs = 1;
2114 args->in.args[0].size = sizeof(*inarg);
2115 args->in.args[0].value = inarg;
2118 static int fuse_getlk(struct file *file, struct file_lock *fl)
2120 struct inode *inode = file_inode(file);
2121 struct fuse_conn *fc = get_fuse_conn(inode);
2123 struct fuse_lk_in inarg;
2124 struct fuse_lk_out outarg;
2127 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2128 args.out.numargs = 1;
2129 args.out.args[0].size = sizeof(outarg);
2130 args.out.args[0].value = &outarg;
2131 err = fuse_simple_request(fc, &args);
2133 err = convert_fuse_file_lock(&outarg.lk, fl);
2138 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2140 struct inode *inode = file_inode(file);
2141 struct fuse_conn *fc = get_fuse_conn(inode);
2143 struct fuse_lk_in inarg;
2144 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2145 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2148 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2149 /* NLM needs asynchronous locks, which we don't support yet */
2153 /* Unlock on close is handled by the flush method */
2154 if (fl->fl_flags & FL_CLOSE)
2157 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2158 err = fuse_simple_request(fc, &args);
2160 /* locking is restartable */
2167 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2169 struct inode *inode = file_inode(file);
2170 struct fuse_conn *fc = get_fuse_conn(inode);
2173 if (cmd == F_CANCELLK) {
2175 } else if (cmd == F_GETLK) {
2177 posix_test_lock(file, fl);
2180 err = fuse_getlk(file, fl);
2183 err = posix_lock_file(file, fl, NULL);
2185 err = fuse_setlk(file, fl, 0);
2190 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2192 struct inode *inode = file_inode(file);
2193 struct fuse_conn *fc = get_fuse_conn(inode);
2197 err = locks_lock_file_wait(file, fl);
2199 struct fuse_file *ff = file->private_data;
2201 /* emulate flock with POSIX locks */
2203 err = fuse_setlk(file, fl, 1);
2209 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2211 struct inode *inode = mapping->host;
2212 struct fuse_conn *fc = get_fuse_conn(inode);
2214 struct fuse_bmap_in inarg;
2215 struct fuse_bmap_out outarg;
2218 if (!inode->i_sb->s_bdev || fc->no_bmap)
2221 memset(&inarg, 0, sizeof(inarg));
2222 inarg.block = block;
2223 inarg.blocksize = inode->i_sb->s_blocksize;
2224 args.in.h.opcode = FUSE_BMAP;
2225 args.in.h.nodeid = get_node_id(inode);
2226 args.in.numargs = 1;
2227 args.in.args[0].size = sizeof(inarg);
2228 args.in.args[0].value = &inarg;
2229 args.out.numargs = 1;
2230 args.out.args[0].size = sizeof(outarg);
2231 args.out.args[0].value = &outarg;
2232 err = fuse_simple_request(fc, &args);
2236 return err ? 0 : outarg.block;
2239 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2241 struct inode *inode = file->f_mapping->host;
2242 struct fuse_conn *fc = get_fuse_conn(inode);
2243 struct fuse_file *ff = file->private_data;
2245 struct fuse_lseek_in inarg = {
2250 struct fuse_lseek_out outarg;
2256 args.in.h.opcode = FUSE_LSEEK;
2257 args.in.h.nodeid = ff->nodeid;
2258 args.in.numargs = 1;
2259 args.in.args[0].size = sizeof(inarg);
2260 args.in.args[0].value = &inarg;
2261 args.out.numargs = 1;
2262 args.out.args[0].size = sizeof(outarg);
2263 args.out.args[0].value = &outarg;
2264 err = fuse_simple_request(fc, &args);
2266 if (err == -ENOSYS) {
2273 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2276 err = fuse_update_attributes(inode, NULL, file, NULL);
2278 return generic_file_llseek(file, offset, whence);
2283 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2286 struct inode *inode = file_inode(file);
2291 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2292 retval = generic_file_llseek(file, offset, whence);
2296 retval = fuse_update_attributes(inode, NULL, file, NULL);
2298 retval = generic_file_llseek(file, offset, whence);
2299 inode_unlock(inode);
2304 retval = fuse_lseek(file, offset, whence);
2305 inode_unlock(inode);
2314 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2315 unsigned int nr_segs, size_t bytes, bool to_user)
2323 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
2325 while (iov_iter_count(&ii)) {
2326 struct page *page = pages[page_idx++];
2327 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
2333 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2334 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2335 size_t copy = min(todo, iov_len);
2339 left = copy_from_user(kaddr, uaddr, copy);
2341 left = copy_to_user(uaddr, kaddr, copy);
2346 iov_iter_advance(&ii, copy);
2358 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2359 * ABI was defined to be 'struct iovec' which is different on 32bit
2360 * and 64bit. Fortunately we can determine which structure the server
2361 * used from the size of the reply.
2363 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2364 size_t transferred, unsigned count,
2367 #ifdef CONFIG_COMPAT
2368 if (count * sizeof(struct compat_iovec) == transferred) {
2369 struct compat_iovec *ciov = src;
2373 * With this interface a 32bit server cannot support
2374 * non-compat (i.e. ones coming from 64bit apps) ioctl
2380 for (i = 0; i < count; i++) {
2381 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2382 dst[i].iov_len = ciov[i].iov_len;
2388 if (count * sizeof(struct iovec) != transferred)
2391 memcpy(dst, src, transferred);
2395 /* Make sure iov_length() won't overflow */
2396 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2399 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2401 for (n = 0; n < count; n++, iov++) {
2402 if (iov->iov_len > (size_t) max)
2404 max -= iov->iov_len;
2409 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2410 void *src, size_t transferred, unsigned count,
2414 struct fuse_ioctl_iovec *fiov = src;
2416 if (fc->minor < 16) {
2417 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2421 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2424 for (i = 0; i < count; i++) {
2425 /* Did the server supply an inappropriate value? */
2426 if (fiov[i].base != (unsigned long) fiov[i].base ||
2427 fiov[i].len != (unsigned long) fiov[i].len)
2430 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2431 dst[i].iov_len = (size_t) fiov[i].len;
2433 #ifdef CONFIG_COMPAT
2435 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2436 (compat_size_t) dst[i].iov_len != fiov[i].len))
2446 * For ioctls, there is no generic way to determine how much memory
2447 * needs to be read and/or written. Furthermore, ioctls are allowed
2448 * to dereference the passed pointer, so the parameter requires deep
2449 * copying but FUSE has no idea whatsoever about what to copy in or
2452 * This is solved by allowing FUSE server to retry ioctl with
2453 * necessary in/out iovecs. Let's assume the ioctl implementation
2454 * needs to read in the following structure.
2461 * On the first callout to FUSE server, inarg->in_size and
2462 * inarg->out_size will be NULL; then, the server completes the ioctl
2463 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2464 * the actual iov array to
2466 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2468 * which tells FUSE to copy in the requested area and retry the ioctl.
2469 * On the second round, the server has access to the structure and
2470 * from that it can tell what to look for next, so on the invocation,
2471 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2473 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2474 * { .iov_base = a.buf, .iov_len = a.buflen } }
2476 * FUSE will copy both struct a and the pointed buffer from the
2477 * process doing the ioctl and retry ioctl with both struct a and the
2480 * This time, FUSE server has everything it needs and completes ioctl
2481 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2483 * Copying data out works the same way.
2485 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2486 * automatically initializes in and out iovs by decoding @cmd with
2487 * _IOC_* macros and the server is not allowed to request RETRY. This
2488 * limits ioctl data transfers to well-formed ioctls and is the forced
2489 * behavior for all FUSE servers.
2491 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2494 struct fuse_file *ff = file->private_data;
2495 struct fuse_conn *fc = ff->fc;
2496 struct fuse_ioctl_in inarg = {
2502 struct fuse_ioctl_out outarg;
2503 struct fuse_req *req = NULL;
2504 struct page **pages = NULL;
2505 struct iovec *iov_page = NULL;
2506 struct iovec *in_iov = NULL, *out_iov = NULL;
2507 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2508 size_t in_size, out_size, transferred;
2511 #if BITS_PER_LONG == 32
2512 inarg.flags |= FUSE_IOCTL_32BIT;
2514 if (flags & FUSE_IOCTL_COMPAT)
2515 inarg.flags |= FUSE_IOCTL_32BIT;
2518 /* assume all the iovs returned by client always fits in a page */
2519 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2522 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2523 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2524 if (!pages || !iov_page)
2528 * If restricted, initialize IO parameters as encoded in @cmd.
2529 * RETRY from server is not allowed.
2531 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2532 struct iovec *iov = iov_page;
2534 iov->iov_base = (void __user *)arg;
2535 iov->iov_len = _IOC_SIZE(cmd);
2537 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2542 if (_IOC_DIR(cmd) & _IOC_READ) {
2549 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2550 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2553 * Out data can be used either for actual out data or iovs,
2554 * make sure there always is at least one page.
2556 out_size = max_t(size_t, out_size, PAGE_SIZE);
2557 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2559 /* make sure there are enough buffer pages and init request with them */
2561 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2563 while (num_pages < max_pages) {
2564 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2565 if (!pages[num_pages])
2570 req = fuse_get_req(fc, num_pages);
2576 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2577 req->num_pages = num_pages;
2578 fuse_page_descs_length_init(req, 0, req->num_pages);
2580 /* okay, let's send it to the client */
2581 req->in.h.opcode = FUSE_IOCTL;
2582 req->in.h.nodeid = ff->nodeid;
2583 req->in.numargs = 1;
2584 req->in.args[0].size = sizeof(inarg);
2585 req->in.args[0].value = &inarg;
2588 req->in.args[1].size = in_size;
2589 req->in.argpages = 1;
2591 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2597 req->out.numargs = 2;
2598 req->out.args[0].size = sizeof(outarg);
2599 req->out.args[0].value = &outarg;
2600 req->out.args[1].size = out_size;
2601 req->out.argpages = 1;
2602 req->out.argvar = 1;
2604 fuse_request_send(fc, req);
2605 err = req->out.h.error;
2606 transferred = req->out.args[1].size;
2607 fuse_put_request(fc, req);
2612 /* did it ask for retry? */
2613 if (outarg.flags & FUSE_IOCTL_RETRY) {
2616 /* no retry if in restricted mode */
2618 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2621 in_iovs = outarg.in_iovs;
2622 out_iovs = outarg.out_iovs;
2625 * Make sure things are in boundary, separate checks
2626 * are to protect against overflow.
2629 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2630 out_iovs > FUSE_IOCTL_MAX_IOV ||
2631 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2634 vaddr = kmap_atomic(pages[0]);
2635 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2636 transferred, in_iovs + out_iovs,
2637 (flags & FUSE_IOCTL_COMPAT) != 0);
2638 kunmap_atomic(vaddr);
2643 out_iov = in_iov + in_iovs;
2645 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2649 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2657 if (transferred > inarg.out_size)
2660 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2663 fuse_put_request(fc, req);
2664 free_page((unsigned long) iov_page);
2666 __free_page(pages[--num_pages]);
2669 return err ? err : outarg.result;
2671 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2673 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2674 unsigned long arg, unsigned int flags)
2676 struct inode *inode = file_inode(file);
2677 struct fuse_conn *fc = get_fuse_conn(inode);
2679 if (!fuse_allow_current_process(fc))
2682 if (is_bad_inode(inode))
2685 return fuse_do_ioctl(file, cmd, arg, flags);
2688 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2691 return fuse_ioctl_common(file, cmd, arg, 0);
2694 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2697 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2701 * All files which have been polled are linked to RB tree
2702 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2703 * find the matching one.
2705 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2706 struct rb_node **parent_out)
2708 struct rb_node **link = &fc->polled_files.rb_node;
2709 struct rb_node *last = NULL;
2712 struct fuse_file *ff;
2715 ff = rb_entry(last, struct fuse_file, polled_node);
2718 link = &last->rb_left;
2719 else if (kh > ff->kh)
2720 link = &last->rb_right;
2731 * The file is about to be polled. Make sure it's on the polled_files
2732 * RB tree. Note that files once added to the polled_files tree are
2733 * not removed before the file is released. This is because a file
2734 * polled once is likely to be polled again.
2736 static void fuse_register_polled_file(struct fuse_conn *fc,
2737 struct fuse_file *ff)
2739 spin_lock(&fc->lock);
2740 if (RB_EMPTY_NODE(&ff->polled_node)) {
2741 struct rb_node **link, *uninitialized_var(parent);
2743 link = fuse_find_polled_node(fc, ff->kh, &parent);
2745 rb_link_node(&ff->polled_node, parent, link);
2746 rb_insert_color(&ff->polled_node, &fc->polled_files);
2748 spin_unlock(&fc->lock);
2751 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2753 struct fuse_file *ff = file->private_data;
2754 struct fuse_conn *fc = ff->fc;
2755 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2756 struct fuse_poll_out outarg;
2761 return DEFAULT_POLLMASK;
2763 poll_wait(file, &ff->poll_wait, wait);
2764 inarg.events = (__u32)poll_requested_events(wait);
2767 * Ask for notification iff there's someone waiting for it.
2768 * The client may ignore the flag and always notify.
2770 if (waitqueue_active(&ff->poll_wait)) {
2771 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2772 fuse_register_polled_file(fc, ff);
2775 args.in.h.opcode = FUSE_POLL;
2776 args.in.h.nodeid = ff->nodeid;
2777 args.in.numargs = 1;
2778 args.in.args[0].size = sizeof(inarg);
2779 args.in.args[0].value = &inarg;
2780 args.out.numargs = 1;
2781 args.out.args[0].size = sizeof(outarg);
2782 args.out.args[0].value = &outarg;
2783 err = fuse_simple_request(fc, &args);
2786 return outarg.revents;
2787 if (err == -ENOSYS) {
2789 return DEFAULT_POLLMASK;
2793 EXPORT_SYMBOL_GPL(fuse_file_poll);
2796 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2797 * wakes up the poll waiters.
2799 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2800 struct fuse_notify_poll_wakeup_out *outarg)
2802 u64 kh = outarg->kh;
2803 struct rb_node **link;
2805 spin_lock(&fc->lock);
2807 link = fuse_find_polled_node(fc, kh, NULL);
2809 struct fuse_file *ff;
2811 ff = rb_entry(*link, struct fuse_file, polled_node);
2812 wake_up_interruptible_sync(&ff->poll_wait);
2815 spin_unlock(&fc->lock);
2819 static void fuse_do_truncate(struct file *file)
2821 struct inode *inode = file->f_mapping->host;
2824 attr.ia_valid = ATTR_SIZE;
2825 attr.ia_size = i_size_read(inode);
2827 attr.ia_file = file;
2828 attr.ia_valid |= ATTR_FILE;
2830 fuse_do_setattr(inode, &attr, file);
2833 static inline loff_t fuse_round_up(loff_t off)
2835 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2839 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2841 DECLARE_COMPLETION_ONSTACK(wait);
2843 struct file *file = iocb->ki_filp;
2844 struct fuse_file *ff = file->private_data;
2845 bool async_dio = ff->fc->async_dio;
2847 struct inode *inode;
2849 size_t count = iov_iter_count(iter);
2850 loff_t offset = iocb->ki_pos;
2851 struct fuse_io_priv *io;
2854 inode = file->f_mapping->host;
2855 i_size = i_size_read(inode);
2857 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2860 /* optimization for short read */
2861 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2862 if (offset >= i_size)
2864 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2865 count = iov_iter_count(iter);
2868 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2871 spin_lock_init(&io->lock);
2872 kref_init(&io->refcnt);
2876 io->offset = offset;
2877 io->write = (iov_iter_rw(iter) == WRITE);
2881 * By default, we want to optimize all I/Os with async request
2882 * submission to the client filesystem if supported.
2884 io->async = async_dio;
2886 io->blocking = is_sync_kiocb(iocb);
2889 * We cannot asynchronously extend the size of a file.
2890 * In such case the aio will behave exactly like sync io.
2892 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2893 io->blocking = true;
2895 if (io->async && io->blocking) {
2897 * Additional reference to keep io around after
2898 * calling fuse_aio_complete()
2900 kref_get(&io->refcnt);
2904 if (iov_iter_rw(iter) == WRITE) {
2905 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2906 fuse_invalidate_attr(inode);
2908 ret = __fuse_direct_read(io, iter, &pos);
2912 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2914 /* we have a non-extending, async request, so return */
2916 return -EIOCBQUEUED;
2918 wait_for_completion(&wait);
2919 ret = fuse_get_res_by_io(io);
2922 kref_put(&io->refcnt, fuse_io_release);
2924 if (iov_iter_rw(iter) == WRITE) {
2926 fuse_write_update_size(inode, pos);
2927 else if (ret < 0 && offset + count > i_size)
2928 fuse_do_truncate(file);
2934 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2937 struct fuse_file *ff = file->private_data;
2938 struct inode *inode = file_inode(file);
2939 struct fuse_inode *fi = get_fuse_inode(inode);
2940 struct fuse_conn *fc = ff->fc;
2942 struct fuse_fallocate_in inarg = {
2949 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2950 (mode & FALLOC_FL_PUNCH_HOLE);
2952 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2955 if (fc->no_fallocate)
2960 if (mode & FALLOC_FL_PUNCH_HOLE) {
2961 loff_t endbyte = offset + length - 1;
2962 err = filemap_write_and_wait_range(inode->i_mapping,
2967 fuse_sync_writes(inode);
2971 if (!(mode & FALLOC_FL_KEEP_SIZE))
2972 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2974 args.in.h.opcode = FUSE_FALLOCATE;
2975 args.in.h.nodeid = ff->nodeid;
2976 args.in.numargs = 1;
2977 args.in.args[0].size = sizeof(inarg);
2978 args.in.args[0].value = &inarg;
2979 err = fuse_simple_request(fc, &args);
2980 if (err == -ENOSYS) {
2981 fc->no_fallocate = 1;
2987 /* we could have extended the file */
2988 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2989 bool changed = fuse_write_update_size(inode, offset + length);
2991 if (changed && fc->writeback_cache)
2992 file_update_time(file);
2995 if (mode & FALLOC_FL_PUNCH_HOLE)
2996 truncate_pagecache_range(inode, offset, offset + length - 1);
2998 fuse_invalidate_attr(inode);
3001 if (!(mode & FALLOC_FL_KEEP_SIZE))
3002 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3005 inode_unlock(inode);
3010 static const struct file_operations fuse_file_operations = {
3011 .llseek = fuse_file_llseek,
3012 .read_iter = fuse_file_read_iter,
3013 .write_iter = fuse_file_write_iter,
3014 .mmap = fuse_file_mmap,
3016 .flush = fuse_flush,
3017 .release = fuse_release,
3018 .fsync = fuse_fsync,
3019 .lock = fuse_file_lock,
3020 .flock = fuse_file_flock,
3021 .splice_read = generic_file_splice_read,
3022 .unlocked_ioctl = fuse_file_ioctl,
3023 .compat_ioctl = fuse_file_compat_ioctl,
3024 .poll = fuse_file_poll,
3025 .fallocate = fuse_file_fallocate,
3028 static const struct file_operations fuse_direct_io_file_operations = {
3029 .llseek = fuse_file_llseek,
3030 .read_iter = fuse_direct_read_iter,
3031 .write_iter = fuse_direct_write_iter,
3032 .mmap = fuse_direct_mmap,
3034 .flush = fuse_flush,
3035 .release = fuse_release,
3036 .fsync = fuse_fsync,
3037 .lock = fuse_file_lock,
3038 .flock = fuse_file_flock,
3039 .unlocked_ioctl = fuse_file_ioctl,
3040 .compat_ioctl = fuse_file_compat_ioctl,
3041 .poll = fuse_file_poll,
3042 .fallocate = fuse_file_fallocate,
3043 /* no splice_read */
3046 static const struct address_space_operations fuse_file_aops = {
3047 .readpage = fuse_readpage,
3048 .writepage = fuse_writepage,
3049 .writepages = fuse_writepages,
3050 .launder_page = fuse_launder_page,
3051 .readpages = fuse_readpages,
3052 .set_page_dirty = __set_page_dirty_nobuffers,
3054 .direct_IO = fuse_direct_IO,
3055 .write_begin = fuse_write_begin,
3056 .write_end = fuse_write_end,
3059 void fuse_init_file_inode(struct inode *inode)
3061 inode->i_fop = &fuse_file_operations;
3062 inode->i_data.a_ops = &fuse_file_aops;