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/aio.h>
19 #include <linux/falloc.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;
30 req = fuse_get_req_nopages(fc);
34 memset(&inarg, 0, sizeof(inarg));
35 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
36 if (!fc->atomic_o_trunc)
37 inarg.flags &= ~O_TRUNC;
38 req->in.h.opcode = opcode;
39 req->in.h.nodeid = nodeid;
41 req->in.args[0].size = sizeof(inarg);
42 req->in.args[0].value = &inarg;
44 req->out.args[0].size = sizeof(*outargp);
45 req->out.args[0].value = outargp;
46 fuse_request_send(fc, req);
47 err = req->out.h.error;
48 fuse_put_request(fc, req);
53 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
57 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
62 ff->reserved_req = fuse_request_alloc(0);
63 if (unlikely(!ff->reserved_req)) {
68 INIT_LIST_HEAD(&ff->write_entry);
69 atomic_set(&ff->count, 0);
70 RB_CLEAR_NODE(&ff->polled_node);
71 init_waitqueue_head(&ff->poll_wait);
75 spin_unlock(&fc->lock);
80 void fuse_file_free(struct fuse_file *ff)
82 fuse_request_free(ff->reserved_req);
86 struct fuse_file *fuse_file_get(struct fuse_file *ff)
88 atomic_inc(&ff->count);
92 static void fuse_release_async(struct work_struct *work)
98 req = container_of(work, struct fuse_req, misc.release.work);
99 path = req->misc.release.path;
100 fc = get_fuse_conn(path.dentry->d_inode);
102 fuse_put_request(fc, req);
106 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
108 if (fc->destroy_req) {
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
117 atomic_inc(&req->count);
118 INIT_WORK(&req->misc.release.work, fuse_release_async);
119 schedule_work(&req->misc.release.work);
121 path_put(&req->misc.release.path);
125 static void fuse_file_put(struct fuse_file *ff, bool sync)
127 if (atomic_dec_and_test(&ff->count)) {
128 struct fuse_req *req = ff->reserved_req;
130 if (ff->fc->no_open) {
132 * Drop the release request when client does not
136 path_put(&req->misc.release.path);
137 fuse_put_request(ff->fc, req);
140 fuse_request_send(ff->fc, req);
141 path_put(&req->misc.release.path);
142 fuse_put_request(ff->fc, req);
144 req->end = fuse_release_end;
146 fuse_request_send_background(ff->fc, req);
152 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
155 struct fuse_file *ff;
156 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
158 ff = fuse_file_alloc(fc);
163 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
164 if (!fc->no_open || isdir) {
165 struct fuse_open_out outarg;
168 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
171 ff->open_flags = outarg.open_flags;
173 } else if (err != -ENOSYS || isdir) {
182 ff->open_flags &= ~FOPEN_DIRECT_IO;
185 file->private_data = fuse_file_get(ff);
189 EXPORT_SYMBOL_GPL(fuse_do_open);
191 static void fuse_link_write_file(struct file *file)
193 struct inode *inode = file_inode(file);
194 struct fuse_conn *fc = get_fuse_conn(inode);
195 struct fuse_inode *fi = get_fuse_inode(inode);
196 struct fuse_file *ff = file->private_data;
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
201 spin_lock(&fc->lock);
202 if (list_empty(&ff->write_entry))
203 list_add(&ff->write_entry, &fi->write_files);
204 spin_unlock(&fc->lock);
207 void fuse_finish_open(struct inode *inode, struct file *file)
209 struct fuse_file *ff = file->private_data;
210 struct fuse_conn *fc = get_fuse_conn(inode);
212 if (ff->open_flags & FOPEN_DIRECT_IO)
213 file->f_op = &fuse_direct_io_file_operations;
214 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
215 invalidate_inode_pages2(inode->i_mapping);
216 if (ff->open_flags & FOPEN_NONSEEKABLE)
217 nonseekable_open(inode, file);
218 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
219 struct fuse_inode *fi = get_fuse_inode(inode);
221 spin_lock(&fc->lock);
222 fi->attr_version = ++fc->attr_version;
223 i_size_write(inode, 0);
224 spin_unlock(&fc->lock);
225 fuse_invalidate_attr(inode);
229 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
231 struct fuse_conn *fc = get_fuse_conn(inode);
234 err = generic_file_open(inode, file);
238 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
242 fuse_finish_open(inode, file);
247 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
249 struct fuse_conn *fc = ff->fc;
250 struct fuse_req *req = ff->reserved_req;
251 struct fuse_release_in *inarg = &req->misc.release.in;
253 spin_lock(&fc->lock);
254 list_del(&ff->write_entry);
255 if (!RB_EMPTY_NODE(&ff->polled_node))
256 rb_erase(&ff->polled_node, &fc->polled_files);
257 spin_unlock(&fc->lock);
259 wake_up_interruptible_all(&ff->poll_wait);
262 inarg->flags = flags;
263 req->in.h.opcode = opcode;
264 req->in.h.nodeid = ff->nodeid;
266 req->in.args[0].size = sizeof(struct fuse_release_in);
267 req->in.args[0].value = inarg;
270 void fuse_release_common(struct file *file, int opcode)
272 struct fuse_file *ff;
273 struct fuse_req *req;
275 ff = file->private_data;
279 req = ff->reserved_req;
280 fuse_prepare_release(ff, file->f_flags, opcode);
283 struct fuse_release_in *inarg = &req->misc.release.in;
284 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
285 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
288 /* Hold vfsmount and dentry until release is finished */
289 path_get(&file->f_path);
290 req->misc.release.path = file->f_path;
293 * Normally this will send the RELEASE request, however if
294 * some asynchronous READ or WRITE requests are outstanding,
295 * the sending will be delayed.
297 * Make the release synchronous if this is a fuseblk mount,
298 * synchronous RELEASE is allowed (and desirable) in this case
299 * because the server can be trusted not to screw up.
301 fuse_file_put(ff, ff->fc->destroy_req != NULL);
304 static int fuse_open(struct inode *inode, struct file *file)
306 return fuse_open_common(inode, file, false);
309 static int fuse_release(struct inode *inode, struct file *file)
311 struct fuse_conn *fc = get_fuse_conn(inode);
313 /* see fuse_vma_close() for !writeback_cache case */
314 if (fc->writeback_cache)
315 filemap_write_and_wait(file->f_mapping);
317 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
318 fuse_flush_mtime(file, true);
320 fuse_release_common(file, FUSE_RELEASE);
322 /* return value is ignored by VFS */
326 void fuse_sync_release(struct fuse_file *ff, int flags)
328 WARN_ON(atomic_read(&ff->count) > 1);
329 fuse_prepare_release(ff, flags, FUSE_RELEASE);
330 ff->reserved_req->force = 1;
331 ff->reserved_req->background = 0;
332 fuse_request_send(ff->fc, ff->reserved_req);
333 fuse_put_request(ff->fc, ff->reserved_req);
336 EXPORT_SYMBOL_GPL(fuse_sync_release);
339 * Scramble the ID space with XTEA, so that the value of the files_struct
340 * pointer is not exposed to userspace.
342 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
344 u32 *k = fc->scramble_key;
345 u64 v = (unsigned long) id;
351 for (i = 0; i < 32; i++) {
352 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
354 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
357 return (u64) v0 + ((u64) v1 << 32);
361 * Check if page is under writeback
363 * This is currently done by walking the list of writepage requests
364 * for the inode, which can be pretty inefficient.
366 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
368 struct fuse_conn *fc = get_fuse_conn(inode);
369 struct fuse_inode *fi = get_fuse_inode(inode);
370 struct fuse_req *req;
373 spin_lock(&fc->lock);
374 list_for_each_entry(req, &fi->writepages, writepages_entry) {
377 BUG_ON(req->inode != inode);
378 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
379 if (curr_index <= index &&
380 index < curr_index + req->num_pages) {
385 spin_unlock(&fc->lock);
391 * Wait for page writeback to be completed.
393 * Since fuse doesn't rely on the VM writeback tracking, this has to
394 * use some other means.
396 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
398 struct fuse_inode *fi = get_fuse_inode(inode);
400 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
404 static int fuse_flush(struct file *file, fl_owner_t id)
406 struct inode *inode = file_inode(file);
407 struct fuse_conn *fc = get_fuse_conn(inode);
408 struct fuse_file *ff = file->private_data;
409 struct fuse_req *req;
410 struct fuse_flush_in inarg;
413 if (is_bad_inode(inode))
419 req = fuse_get_req_nofail_nopages(fc, file);
420 memset(&inarg, 0, sizeof(inarg));
422 inarg.lock_owner = fuse_lock_owner_id(fc, id);
423 req->in.h.opcode = FUSE_FLUSH;
424 req->in.h.nodeid = get_node_id(inode);
426 req->in.args[0].size = sizeof(inarg);
427 req->in.args[0].value = &inarg;
429 fuse_request_send(fc, req);
430 err = req->out.h.error;
431 fuse_put_request(fc, req);
432 if (err == -ENOSYS) {
440 * Wait for all pending writepages on the inode to finish.
442 * This is currently done by blocking further writes with FUSE_NOWRITE
443 * and waiting for all sent writes to complete.
445 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
446 * could conflict with truncation.
448 static void fuse_sync_writes(struct inode *inode)
450 fuse_set_nowrite(inode);
451 fuse_release_nowrite(inode);
454 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
455 int datasync, int isdir)
457 struct inode *inode = file->f_mapping->host;
458 struct fuse_conn *fc = get_fuse_conn(inode);
459 struct fuse_file *ff = file->private_data;
460 struct fuse_req *req;
461 struct fuse_fsync_in inarg;
464 if (is_bad_inode(inode))
467 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
471 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
474 mutex_lock(&inode->i_mutex);
477 * Start writeback against all dirty pages of the inode, then
478 * wait for all outstanding writes, before sending the FSYNC
481 err = write_inode_now(inode, 0);
485 fuse_sync_writes(inode);
487 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
488 int err = fuse_flush_mtime(file, false);
493 req = fuse_get_req_nopages(fc);
499 memset(&inarg, 0, sizeof(inarg));
501 inarg.fsync_flags = datasync ? 1 : 0;
502 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
503 req->in.h.nodeid = get_node_id(inode);
505 req->in.args[0].size = sizeof(inarg);
506 req->in.args[0].value = &inarg;
507 fuse_request_send(fc, req);
508 err = req->out.h.error;
509 fuse_put_request(fc, req);
510 if (err == -ENOSYS) {
518 mutex_unlock(&inode->i_mutex);
522 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
525 return fuse_fsync_common(file, start, end, datasync, 0);
528 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
529 size_t count, int opcode)
531 struct fuse_read_in *inarg = &req->misc.read.in;
532 struct fuse_file *ff = file->private_data;
537 inarg->flags = file->f_flags;
538 req->in.h.opcode = opcode;
539 req->in.h.nodeid = ff->nodeid;
541 req->in.args[0].size = sizeof(struct fuse_read_in);
542 req->in.args[0].value = inarg;
544 req->out.numargs = 1;
545 req->out.args[0].size = count;
548 static void fuse_release_user_pages(struct fuse_req *req, int write)
552 for (i = 0; i < req->num_pages; i++) {
553 struct page *page = req->pages[i];
555 set_page_dirty_lock(page);
561 * In case of short read, the caller sets 'pos' to the position of
562 * actual end of fuse request in IO request. Otherwise, if bytes_requested
563 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
566 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
567 * both submitted asynchronously. The first of them was ACKed by userspace as
568 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
569 * second request was ACKed as short, e.g. only 1K was read, resulting in
572 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
573 * will be equal to the length of the longest contiguous fragment of
574 * transferred data starting from the beginning of IO request.
576 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
580 spin_lock(&io->lock);
582 io->err = io->err ? : err;
583 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
587 spin_unlock(&io->lock);
594 else if (io->bytes >= 0 && io->write)
597 res = io->bytes < 0 ? io->size : io->bytes;
599 if (!is_sync_kiocb(io->iocb)) {
600 struct inode *inode = file_inode(io->iocb->ki_filp);
601 struct fuse_conn *fc = get_fuse_conn(inode);
602 struct fuse_inode *fi = get_fuse_inode(inode);
604 spin_lock(&fc->lock);
605 fi->attr_version = ++fc->attr_version;
606 spin_unlock(&fc->lock);
610 aio_complete(io->iocb, res, 0);
615 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
617 struct fuse_io_priv *io = req->io;
620 fuse_release_user_pages(req, !io->write);
623 if (req->misc.write.in.size != req->misc.write.out.size)
624 pos = req->misc.write.in.offset - io->offset +
625 req->misc.write.out.size;
627 if (req->misc.read.in.size != req->out.args[0].size)
628 pos = req->misc.read.in.offset - io->offset +
629 req->out.args[0].size;
632 fuse_aio_complete(io, req->out.h.error, pos);
635 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
636 size_t num_bytes, struct fuse_io_priv *io)
638 spin_lock(&io->lock);
639 io->size += num_bytes;
641 spin_unlock(&io->lock);
644 req->end = fuse_aio_complete_req;
646 __fuse_get_request(req);
647 fuse_request_send_background(fc, req);
652 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
653 loff_t pos, size_t count, fl_owner_t owner)
655 struct file *file = io->file;
656 struct fuse_file *ff = file->private_data;
657 struct fuse_conn *fc = ff->fc;
659 fuse_read_fill(req, file, pos, count, FUSE_READ);
661 struct fuse_read_in *inarg = &req->misc.read.in;
663 inarg->read_flags |= FUSE_READ_LOCKOWNER;
664 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
668 return fuse_async_req_send(fc, req, count, io);
670 fuse_request_send(fc, req);
671 return req->out.args[0].size;
674 static void fuse_read_update_size(struct inode *inode, loff_t size,
677 struct fuse_conn *fc = get_fuse_conn(inode);
678 struct fuse_inode *fi = get_fuse_inode(inode);
680 spin_lock(&fc->lock);
681 if (attr_ver == fi->attr_version && size < inode->i_size &&
682 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
683 fi->attr_version = ++fc->attr_version;
684 i_size_write(inode, size);
686 spin_unlock(&fc->lock);
689 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
692 size_t num_read = req->out.args[0].size;
693 struct fuse_conn *fc = get_fuse_conn(inode);
695 if (fc->writeback_cache) {
697 * A hole in a file. Some data after the hole are in page cache,
698 * but have not reached the client fs yet. So, the hole is not
702 int start_idx = num_read >> PAGE_CACHE_SHIFT;
703 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
705 for (i = start_idx; i < req->num_pages; i++) {
706 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
710 loff_t pos = page_offset(req->pages[0]) + num_read;
711 fuse_read_update_size(inode, pos, attr_ver);
715 static int fuse_do_readpage(struct file *file, struct page *page)
717 struct fuse_io_priv io = { .async = 0, .file = file };
718 struct inode *inode = page->mapping->host;
719 struct fuse_conn *fc = get_fuse_conn(inode);
720 struct fuse_req *req;
722 loff_t pos = page_offset(page);
723 size_t count = PAGE_CACHE_SIZE;
728 * Page writeback can extend beyond the lifetime of the
729 * page-cache page, so make sure we read a properly synced
732 fuse_wait_on_page_writeback(inode, page->index);
734 req = fuse_get_req(fc, 1);
738 attr_ver = fuse_get_attr_version(fc);
740 req->out.page_zeroing = 1;
741 req->out.argpages = 1;
743 req->pages[0] = page;
744 req->page_descs[0].length = count;
745 num_read = fuse_send_read(req, &io, pos, count, NULL);
746 err = req->out.h.error;
750 * Short read means EOF. If file size is larger, truncate it
752 if (num_read < count)
753 fuse_short_read(req, inode, attr_ver);
755 SetPageUptodate(page);
758 fuse_put_request(fc, req);
763 static int fuse_readpage(struct file *file, struct page *page)
765 struct inode *inode = page->mapping->host;
769 if (is_bad_inode(inode))
772 err = fuse_do_readpage(file, page);
773 fuse_invalidate_atime(inode);
779 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
782 size_t count = req->misc.read.in.size;
783 size_t num_read = req->out.args[0].size;
784 struct address_space *mapping = NULL;
786 for (i = 0; mapping == NULL && i < req->num_pages; i++)
787 mapping = req->pages[i]->mapping;
790 struct inode *inode = mapping->host;
793 * Short read means EOF. If file size is larger, truncate it
795 if (!req->out.h.error && num_read < count)
796 fuse_short_read(req, inode, req->misc.read.attr_ver);
798 fuse_invalidate_atime(inode);
801 for (i = 0; i < req->num_pages; i++) {
802 struct page *page = req->pages[i];
803 if (!req->out.h.error)
804 SetPageUptodate(page);
808 page_cache_release(page);
811 fuse_file_put(req->ff, false);
814 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
816 struct fuse_file *ff = file->private_data;
817 struct fuse_conn *fc = ff->fc;
818 loff_t pos = page_offset(req->pages[0]);
819 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
821 req->out.argpages = 1;
822 req->out.page_zeroing = 1;
823 req->out.page_replace = 1;
824 fuse_read_fill(req, file, pos, count, FUSE_READ);
825 req->misc.read.attr_ver = fuse_get_attr_version(fc);
826 if (fc->async_read) {
827 req->ff = fuse_file_get(ff);
828 req->end = fuse_readpages_end;
829 fuse_request_send_background(fc, req);
831 fuse_request_send(fc, req);
832 fuse_readpages_end(fc, req);
833 fuse_put_request(fc, req);
837 struct fuse_fill_data {
838 struct fuse_req *req;
844 static int fuse_readpages_fill(void *_data, struct page *page)
846 struct fuse_fill_data *data = _data;
847 struct fuse_req *req = data->req;
848 struct inode *inode = data->inode;
849 struct fuse_conn *fc = get_fuse_conn(inode);
851 fuse_wait_on_page_writeback(inode, page->index);
853 if (req->num_pages &&
854 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
855 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
856 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
857 int nr_alloc = min_t(unsigned, data->nr_pages,
858 FUSE_MAX_PAGES_PER_REQ);
859 fuse_send_readpages(req, data->file);
861 req = fuse_get_req_for_background(fc, nr_alloc);
863 req = fuse_get_req(fc, nr_alloc);
872 if (WARN_ON(req->num_pages >= req->max_pages)) {
873 fuse_put_request(fc, req);
877 page_cache_get(page);
878 req->pages[req->num_pages] = page;
879 req->page_descs[req->num_pages].length = PAGE_SIZE;
885 static int fuse_readpages(struct file *file, struct address_space *mapping,
886 struct list_head *pages, unsigned nr_pages)
888 struct inode *inode = mapping->host;
889 struct fuse_conn *fc = get_fuse_conn(inode);
890 struct fuse_fill_data data;
892 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
895 if (is_bad_inode(inode))
901 data.req = fuse_get_req_for_background(fc, nr_alloc);
903 data.req = fuse_get_req(fc, nr_alloc);
904 data.nr_pages = nr_pages;
905 err = PTR_ERR(data.req);
906 if (IS_ERR(data.req))
909 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
911 if (data.req->num_pages)
912 fuse_send_readpages(data.req, file);
914 fuse_put_request(fc, data.req);
920 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
921 unsigned long nr_segs, loff_t pos)
923 struct inode *inode = iocb->ki_filp->f_mapping->host;
924 struct fuse_conn *fc = get_fuse_conn(inode);
927 * In auto invalidate mode, always update attributes on read.
928 * Otherwise, only update if we attempt to read past EOF (to ensure
929 * i_size is up to date).
931 if (fc->auto_inval_data ||
932 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
934 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
939 return generic_file_aio_read(iocb, iov, nr_segs, pos);
942 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
943 loff_t pos, size_t count)
945 struct fuse_write_in *inarg = &req->misc.write.in;
946 struct fuse_write_out *outarg = &req->misc.write.out;
951 req->in.h.opcode = FUSE_WRITE;
952 req->in.h.nodeid = ff->nodeid;
954 if (ff->fc->minor < 9)
955 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
957 req->in.args[0].size = sizeof(struct fuse_write_in);
958 req->in.args[0].value = inarg;
959 req->in.args[1].size = count;
960 req->out.numargs = 1;
961 req->out.args[0].size = sizeof(struct fuse_write_out);
962 req->out.args[0].value = outarg;
965 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
966 loff_t pos, size_t count, fl_owner_t owner)
968 struct file *file = io->file;
969 struct fuse_file *ff = file->private_data;
970 struct fuse_conn *fc = ff->fc;
971 struct fuse_write_in *inarg = &req->misc.write.in;
973 fuse_write_fill(req, ff, pos, count);
974 inarg->flags = file->f_flags;
976 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
977 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
981 return fuse_async_req_send(fc, req, count, io);
983 fuse_request_send(fc, req);
984 return req->misc.write.out.size;
987 bool fuse_write_update_size(struct inode *inode, loff_t pos)
989 struct fuse_conn *fc = get_fuse_conn(inode);
990 struct fuse_inode *fi = get_fuse_inode(inode);
993 spin_lock(&fc->lock);
994 fi->attr_version = ++fc->attr_version;
995 if (pos > inode->i_size) {
996 i_size_write(inode, pos);
999 spin_unlock(&fc->lock);
1004 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1005 struct inode *inode, loff_t pos,
1011 struct fuse_io_priv io = { .async = 0, .file = file };
1013 for (i = 0; i < req->num_pages; i++)
1014 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1016 res = fuse_send_write(req, &io, pos, count, NULL);
1018 offset = req->page_descs[0].offset;
1020 for (i = 0; i < req->num_pages; i++) {
1021 struct page *page = req->pages[i];
1023 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1024 SetPageUptodate(page);
1026 if (count > PAGE_CACHE_SIZE - offset)
1027 count -= PAGE_CACHE_SIZE - offset;
1033 page_cache_release(page);
1039 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1040 struct address_space *mapping,
1041 struct iov_iter *ii, loff_t pos)
1043 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1044 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1048 req->in.argpages = 1;
1049 req->page_descs[0].offset = offset;
1054 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1055 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1056 iov_iter_count(ii));
1058 bytes = min_t(size_t, bytes, fc->max_write - count);
1062 if (iov_iter_fault_in_readable(ii, bytes))
1066 page = grab_cache_page_write_begin(mapping, index, 0);
1070 if (mapping_writably_mapped(mapping))
1071 flush_dcache_page(page);
1073 pagefault_disable();
1074 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1076 flush_dcache_page(page);
1078 mark_page_accessed(page);
1082 page_cache_release(page);
1083 bytes = min(bytes, iov_iter_single_seg_count(ii));
1088 req->pages[req->num_pages] = page;
1089 req->page_descs[req->num_pages].length = tmp;
1092 iov_iter_advance(ii, tmp);
1096 if (offset == PAGE_CACHE_SIZE)
1099 if (!fc->big_writes)
1101 } while (iov_iter_count(ii) && count < fc->max_write &&
1102 req->num_pages < req->max_pages && offset == 0);
1104 return count > 0 ? count : err;
1107 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1109 return min_t(unsigned,
1110 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1111 (pos >> PAGE_CACHE_SHIFT) + 1,
1112 FUSE_MAX_PAGES_PER_REQ);
1115 static ssize_t fuse_perform_write(struct file *file,
1116 struct address_space *mapping,
1117 struct iov_iter *ii, loff_t pos)
1119 struct inode *inode = mapping->host;
1120 struct fuse_conn *fc = get_fuse_conn(inode);
1121 struct fuse_inode *fi = get_fuse_inode(inode);
1125 if (is_bad_inode(inode))
1128 if (inode->i_size < pos + iov_iter_count(ii))
1129 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1132 struct fuse_req *req;
1134 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1136 req = fuse_get_req(fc, nr_pages);
1142 count = fuse_fill_write_pages(req, mapping, ii, pos);
1148 num_written = fuse_send_write_pages(req, file, inode,
1150 err = req->out.h.error;
1155 /* break out of the loop on short write */
1156 if (num_written != count)
1160 fuse_put_request(fc, req);
1161 } while (!err && iov_iter_count(ii));
1164 fuse_write_update_size(inode, pos);
1166 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1167 fuse_invalidate_attr(inode);
1169 return res > 0 ? res : err;
1172 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1173 unsigned long nr_segs, loff_t pos)
1175 struct file *file = iocb->ki_filp;
1176 struct address_space *mapping = file->f_mapping;
1179 ssize_t written = 0;
1180 ssize_t written_buffered = 0;
1181 struct inode *inode = mapping->host;
1186 WARN_ON(iocb->ki_pos != pos);
1189 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1194 mutex_lock(&inode->i_mutex);
1196 /* We can write back this queue in page reclaim */
1197 current->backing_dev_info = mapping->backing_dev_info;
1199 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1206 err = file_remove_suid(file);
1210 err = file_update_time(file);
1214 if (file->f_flags & O_DIRECT) {
1215 written = generic_file_direct_write(iocb, iov, &nr_segs,
1218 if (written < 0 || written == count)
1224 iov_iter_init(&i, iov, nr_segs, count, written);
1225 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1226 if (written_buffered < 0) {
1227 err = written_buffered;
1230 endbyte = pos + written_buffered - 1;
1232 err = filemap_write_and_wait_range(file->f_mapping, pos,
1237 invalidate_mapping_pages(file->f_mapping,
1238 pos >> PAGE_CACHE_SHIFT,
1239 endbyte >> PAGE_CACHE_SHIFT);
1241 written += written_buffered;
1242 iocb->ki_pos = pos + written_buffered;
1244 iov_iter_init(&i, iov, nr_segs, count, 0);
1245 written = fuse_perform_write(file, mapping, &i, pos);
1247 iocb->ki_pos = pos + written;
1250 current->backing_dev_info = NULL;
1251 mutex_unlock(&inode->i_mutex);
1253 return written ? written : err;
1256 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1257 unsigned index, unsigned nr_pages)
1261 for (i = index; i < index + nr_pages; i++)
1262 req->page_descs[i].length = PAGE_SIZE -
1263 req->page_descs[i].offset;
1266 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1268 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1271 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1274 return min(iov_iter_single_seg_count(ii), max_size);
1277 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1278 size_t *nbytesp, int write)
1280 size_t nbytes = 0; /* # bytes already packed in req */
1282 /* Special case for kernel I/O: can copy directly into the buffer */
1283 if (segment_eq(get_fs(), KERNEL_DS)) {
1284 unsigned long user_addr = fuse_get_user_addr(ii);
1285 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1288 req->in.args[1].value = (void *) user_addr;
1290 req->out.args[0].value = (void *) user_addr;
1292 iov_iter_advance(ii, frag_size);
1293 *nbytesp = frag_size;
1297 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1299 unsigned long user_addr = fuse_get_user_addr(ii);
1300 unsigned offset = user_addr & ~PAGE_MASK;
1301 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1304 unsigned n = req->max_pages - req->num_pages;
1305 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1307 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1308 npages = clamp(npages, 1U, n);
1310 ret = get_user_pages_fast(user_addr, npages, !write,
1311 &req->pages[req->num_pages]);
1316 frag_size = min_t(size_t, frag_size,
1317 (npages << PAGE_SHIFT) - offset);
1318 iov_iter_advance(ii, frag_size);
1320 req->page_descs[req->num_pages].offset = offset;
1321 fuse_page_descs_length_init(req, req->num_pages, npages);
1323 req->num_pages += npages;
1324 req->page_descs[req->num_pages - 1].length -=
1325 (npages << PAGE_SHIFT) - offset - frag_size;
1327 nbytes += frag_size;
1331 req->in.argpages = 1;
1333 req->out.argpages = 1;
1340 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1342 struct iov_iter ii = *ii_p;
1345 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1346 unsigned long user_addr = fuse_get_user_addr(&ii);
1347 unsigned offset = user_addr & ~PAGE_MASK;
1348 size_t frag_size = iov_iter_single_seg_count(&ii);
1350 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1351 iov_iter_advance(&ii, frag_size);
1354 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1357 ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
1358 unsigned long nr_segs, size_t count, loff_t *ppos,
1361 struct file *file = io->file;
1362 struct fuse_file *ff = file->private_data;
1363 struct fuse_conn *fc = ff->fc;
1364 size_t nmax = write ? fc->max_write : fc->max_read;
1367 struct fuse_req *req;
1370 iov_iter_init(&ii, iov, nr_segs, count, 0);
1373 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1375 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1377 return PTR_ERR(req);
1381 fl_owner_t owner = current->files;
1382 size_t nbytes = min(count, nmax);
1383 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
1390 nres = fuse_send_write(req, io, pos, nbytes, owner);
1392 nres = fuse_send_read(req, io, pos, nbytes, owner);
1395 fuse_release_user_pages(req, !write);
1396 if (req->out.h.error) {
1398 res = req->out.h.error;
1400 } else if (nres > nbytes) {
1410 fuse_put_request(fc, req);
1412 req = fuse_get_req_for_background(fc,
1413 fuse_iter_npages(&ii));
1415 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1421 fuse_put_request(fc, req);
1427 EXPORT_SYMBOL_GPL(fuse_direct_io);
1429 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1430 const struct iovec *iov,
1431 unsigned long nr_segs, loff_t *ppos,
1435 struct file *file = io->file;
1436 struct inode *inode = file_inode(file);
1438 if (is_bad_inode(inode))
1441 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
1443 fuse_invalidate_attr(inode);
1448 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1449 size_t count, loff_t *ppos)
1451 struct fuse_io_priv io = { .async = 0, .file = file };
1452 struct iovec iov = { .iov_base = buf, .iov_len = count };
1453 return __fuse_direct_read(&io, &iov, 1, ppos, count);
1456 static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1457 const struct iovec *iov,
1458 unsigned long nr_segs, loff_t *ppos)
1460 struct file *file = io->file;
1461 struct inode *inode = file_inode(file);
1462 size_t count = iov_length(iov, nr_segs);
1465 res = generic_write_checks(file, ppos, &count, 0);
1467 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
1469 fuse_invalidate_attr(inode);
1474 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1475 size_t count, loff_t *ppos)
1477 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
1478 struct inode *inode = file_inode(file);
1480 struct fuse_io_priv io = { .async = 0, .file = file };
1482 if (is_bad_inode(inode))
1485 /* Don't allow parallel writes to the same file */
1486 mutex_lock(&inode->i_mutex);
1487 res = __fuse_direct_write(&io, &iov, 1, ppos);
1489 fuse_write_update_size(inode, *ppos);
1490 mutex_unlock(&inode->i_mutex);
1495 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1499 for (i = 0; i < req->num_pages; i++)
1500 __free_page(req->pages[i]);
1503 fuse_file_put(req->ff, false);
1506 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1508 struct inode *inode = req->inode;
1509 struct fuse_inode *fi = get_fuse_inode(inode);
1510 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1513 list_del(&req->writepages_entry);
1514 for (i = 0; i < req->num_pages; i++) {
1515 dec_bdi_stat(bdi, BDI_WRITEBACK);
1516 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1517 bdi_writeout_inc(bdi);
1519 wake_up(&fi->page_waitq);
1522 /* Called under fc->lock, may release and reacquire it */
1523 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1525 __releases(fc->lock)
1526 __acquires(fc->lock)
1528 struct fuse_inode *fi = get_fuse_inode(req->inode);
1529 struct fuse_write_in *inarg = &req->misc.write.in;
1530 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
1535 if (inarg->offset + data_size <= size) {
1536 inarg->size = data_size;
1537 } else if (inarg->offset < size) {
1538 inarg->size = size - inarg->offset;
1540 /* Got truncated off completely */
1544 req->in.args[1].size = inarg->size;
1546 fuse_request_send_background_locked(fc, req);
1550 fuse_writepage_finish(fc, req);
1551 spin_unlock(&fc->lock);
1552 fuse_writepage_free(fc, req);
1553 fuse_put_request(fc, req);
1554 spin_lock(&fc->lock);
1558 * If fi->writectr is positive (no truncate or fsync going on) send
1559 * all queued writepage requests.
1561 * Called with fc->lock
1563 void fuse_flush_writepages(struct inode *inode)
1564 __releases(fc->lock)
1565 __acquires(fc->lock)
1567 struct fuse_conn *fc = get_fuse_conn(inode);
1568 struct fuse_inode *fi = get_fuse_inode(inode);
1569 size_t crop = i_size_read(inode);
1570 struct fuse_req *req;
1572 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1573 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1574 list_del_init(&req->list);
1575 fuse_send_writepage(fc, req, crop);
1579 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1581 struct inode *inode = req->inode;
1582 struct fuse_inode *fi = get_fuse_inode(inode);
1584 mapping_set_error(inode->i_mapping, req->out.h.error);
1585 spin_lock(&fc->lock);
1586 while (req->misc.write.next) {
1587 struct fuse_conn *fc = get_fuse_conn(inode);
1588 struct fuse_write_in *inarg = &req->misc.write.in;
1589 struct fuse_req *next = req->misc.write.next;
1590 req->misc.write.next = next->misc.write.next;
1591 next->misc.write.next = NULL;
1592 next->ff = fuse_file_get(req->ff);
1593 list_add(&next->writepages_entry, &fi->writepages);
1596 * Skip fuse_flush_writepages() to make it easy to crop requests
1597 * based on primary request size.
1599 * 1st case (trivial): there are no concurrent activities using
1600 * fuse_set/release_nowrite. Then we're on safe side because
1601 * fuse_flush_writepages() would call fuse_send_writepage()
1604 * 2nd case: someone called fuse_set_nowrite and it is waiting
1605 * now for completion of all in-flight requests. This happens
1606 * rarely and no more than once per page, so this should be
1609 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1610 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1611 * that fuse_set_nowrite returned implies that all in-flight
1612 * requests were completed along with all of their secondary
1613 * requests. Further primary requests are blocked by negative
1614 * writectr. Hence there cannot be any in-flight requests and
1615 * no invocations of fuse_writepage_end() while we're in
1616 * fuse_set_nowrite..fuse_release_nowrite section.
1618 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1621 fuse_writepage_finish(fc, req);
1622 spin_unlock(&fc->lock);
1623 fuse_writepage_free(fc, req);
1626 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1627 struct fuse_inode *fi)
1629 struct fuse_file *ff = NULL;
1631 spin_lock(&fc->lock);
1632 if (!WARN_ON(list_empty(&fi->write_files))) {
1633 ff = list_entry(fi->write_files.next, struct fuse_file,
1637 spin_unlock(&fc->lock);
1642 static int fuse_writepage_locked(struct page *page)
1644 struct address_space *mapping = page->mapping;
1645 struct inode *inode = mapping->host;
1646 struct fuse_conn *fc = get_fuse_conn(inode);
1647 struct fuse_inode *fi = get_fuse_inode(inode);
1648 struct fuse_req *req;
1649 struct page *tmp_page;
1650 int error = -ENOMEM;
1652 set_page_writeback(page);
1654 req = fuse_request_alloc_nofs(1);
1658 req->background = 1; /* writeback always goes to bg_queue */
1659 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1664 req->ff = fuse_write_file_get(fc, fi);
1668 fuse_write_fill(req, req->ff, page_offset(page), 0);
1670 copy_highpage(tmp_page, page);
1671 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1672 req->misc.write.next = NULL;
1673 req->in.argpages = 1;
1675 req->pages[0] = tmp_page;
1676 req->page_descs[0].offset = 0;
1677 req->page_descs[0].length = PAGE_SIZE;
1678 req->end = fuse_writepage_end;
1681 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1682 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1684 spin_lock(&fc->lock);
1685 list_add(&req->writepages_entry, &fi->writepages);
1686 list_add_tail(&req->list, &fi->queued_writes);
1687 fuse_flush_writepages(inode);
1688 spin_unlock(&fc->lock);
1690 end_page_writeback(page);
1695 fuse_request_free(req);
1697 end_page_writeback(page);
1701 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1705 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1707 * ->writepages() should be called for sync() and friends. We
1708 * should only get here on direct reclaim and then we are
1709 * allowed to skip a page which is already in flight
1711 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1713 redirty_page_for_writepage(wbc, page);
1717 err = fuse_writepage_locked(page);
1723 struct fuse_fill_wb_data {
1724 struct fuse_req *req;
1725 struct fuse_file *ff;
1726 struct inode *inode;
1727 struct page **orig_pages;
1730 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1732 struct fuse_req *req = data->req;
1733 struct inode *inode = data->inode;
1734 struct fuse_conn *fc = get_fuse_conn(inode);
1735 struct fuse_inode *fi = get_fuse_inode(inode);
1736 int num_pages = req->num_pages;
1739 req->ff = fuse_file_get(data->ff);
1740 spin_lock(&fc->lock);
1741 list_add_tail(&req->list, &fi->queued_writes);
1742 fuse_flush_writepages(inode);
1743 spin_unlock(&fc->lock);
1745 for (i = 0; i < num_pages; i++)
1746 end_page_writeback(data->orig_pages[i]);
1749 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1752 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1753 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1754 struct fuse_req *tmp;
1755 struct fuse_req *old_req;
1759 BUG_ON(new_req->num_pages != 0);
1761 spin_lock(&fc->lock);
1762 list_del(&new_req->writepages_entry);
1763 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1764 BUG_ON(old_req->inode != new_req->inode);
1765 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1766 if (curr_index <= page->index &&
1767 page->index < curr_index + old_req->num_pages) {
1773 list_add(&new_req->writepages_entry, &fi->writepages);
1777 new_req->num_pages = 1;
1778 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1779 BUG_ON(tmp->inode != new_req->inode);
1780 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1781 if (tmp->num_pages == 1 &&
1782 curr_index == page->index) {
1787 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1788 old_req->state == FUSE_REQ_PENDING)) {
1789 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1791 copy_highpage(old_req->pages[0], page);
1792 spin_unlock(&fc->lock);
1794 dec_bdi_stat(bdi, BDI_WRITEBACK);
1795 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
1796 bdi_writeout_inc(bdi);
1797 fuse_writepage_free(fc, new_req);
1798 fuse_request_free(new_req);
1801 new_req->misc.write.next = old_req->misc.write.next;
1802 old_req->misc.write.next = new_req;
1805 spin_unlock(&fc->lock);
1810 static int fuse_writepages_fill(struct page *page,
1811 struct writeback_control *wbc, void *_data)
1813 struct fuse_fill_wb_data *data = _data;
1814 struct fuse_req *req = data->req;
1815 struct inode *inode = data->inode;
1816 struct fuse_conn *fc = get_fuse_conn(inode);
1817 struct page *tmp_page;
1823 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1829 * Being under writeback is unlikely but possible. For example direct
1830 * read to an mmaped fuse file will set the page dirty twice; once when
1831 * the pages are faulted with get_user_pages(), and then after the read
1834 is_writeback = fuse_page_is_writeback(inode, page->index);
1836 if (req && req->num_pages &&
1837 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1838 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1839 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1840 fuse_writepages_send(data);
1844 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1849 * The page must not be redirtied until the writeout is completed
1850 * (i.e. userspace has sent a reply to the write request). Otherwise
1851 * there could be more than one temporary page instance for each real
1854 * This is ensured by holding the page lock in page_mkwrite() while
1855 * checking fuse_page_is_writeback(). We already hold the page lock
1856 * since clear_page_dirty_for_io() and keep it held until we add the
1857 * request to the fi->writepages list and increment req->num_pages.
1858 * After this fuse_page_is_writeback() will indicate that the page is
1859 * under writeback, so we can release the page lock.
1861 if (data->req == NULL) {
1862 struct fuse_inode *fi = get_fuse_inode(inode);
1865 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1867 __free_page(tmp_page);
1871 fuse_write_fill(req, data->ff, page_offset(page), 0);
1872 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1873 req->misc.write.next = NULL;
1874 req->in.argpages = 1;
1875 req->background = 1;
1877 req->end = fuse_writepage_end;
1880 spin_lock(&fc->lock);
1881 list_add(&req->writepages_entry, &fi->writepages);
1882 spin_unlock(&fc->lock);
1886 set_page_writeback(page);
1888 copy_highpage(tmp_page, page);
1889 req->pages[req->num_pages] = tmp_page;
1890 req->page_descs[req->num_pages].offset = 0;
1891 req->page_descs[req->num_pages].length = PAGE_SIZE;
1893 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1894 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1897 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1898 end_page_writeback(page);
1902 data->orig_pages[req->num_pages] = page;
1905 * Protected by fc->lock against concurrent access by
1906 * fuse_page_is_writeback().
1908 spin_lock(&fc->lock);
1910 spin_unlock(&fc->lock);
1918 static int fuse_writepages(struct address_space *mapping,
1919 struct writeback_control *wbc)
1921 struct inode *inode = mapping->host;
1922 struct fuse_fill_wb_data data;
1926 if (is_bad_inode(inode))
1934 data.orig_pages = kzalloc(sizeof(struct page *) *
1935 FUSE_MAX_PAGES_PER_REQ,
1937 if (!data.orig_pages)
1940 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1942 /* Ignore errors if we can write at least one page */
1943 BUG_ON(!data.req->num_pages);
1944 fuse_writepages_send(&data);
1948 fuse_file_put(data.ff, false);
1950 kfree(data.orig_pages);
1956 * It's worthy to make sure that space is reserved on disk for the write,
1957 * but how to implement it without killing performance need more thinking.
1959 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1960 loff_t pos, unsigned len, unsigned flags,
1961 struct page **pagep, void **fsdata)
1963 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1964 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
1969 WARN_ON(!fc->writeback_cache);
1971 page = grab_cache_page_write_begin(mapping, index, flags);
1975 fuse_wait_on_page_writeback(mapping->host, page->index);
1977 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
1980 * Check if the start this page comes after the end of file, in which
1981 * case the readpage can be optimized away.
1983 fsize = i_size_read(mapping->host);
1984 if (fsize <= (pos & PAGE_CACHE_MASK)) {
1985 size_t off = pos & ~PAGE_CACHE_MASK;
1987 zero_user_segment(page, 0, off);
1990 err = fuse_do_readpage(file, page);
1999 page_cache_release(page);
2004 static int fuse_write_end(struct file *file, struct address_space *mapping,
2005 loff_t pos, unsigned len, unsigned copied,
2006 struct page *page, void *fsdata)
2008 struct inode *inode = page->mapping->host;
2010 if (!PageUptodate(page)) {
2011 /* Zero any unwritten bytes at the end of the page */
2012 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2014 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2015 SetPageUptodate(page);
2018 fuse_write_update_size(inode, pos + copied);
2019 set_page_dirty(page);
2021 page_cache_release(page);
2026 static int fuse_launder_page(struct page *page)
2029 if (clear_page_dirty_for_io(page)) {
2030 struct inode *inode = page->mapping->host;
2031 err = fuse_writepage_locked(page);
2033 fuse_wait_on_page_writeback(inode, page->index);
2039 * Write back dirty pages now, because there may not be any suitable
2042 static void fuse_vma_close(struct vm_area_struct *vma)
2044 filemap_write_and_wait(vma->vm_file->f_mapping);
2048 * Wait for writeback against this page to complete before allowing it
2049 * to be marked dirty again, and hence written back again, possibly
2050 * before the previous writepage completed.
2052 * Block here, instead of in ->writepage(), so that the userspace fs
2053 * can only block processes actually operating on the filesystem.
2055 * Otherwise unprivileged userspace fs would be able to block
2060 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2062 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2064 struct page *page = vmf->page;
2065 struct inode *inode = file_inode(vma->vm_file);
2067 file_update_time(vma->vm_file);
2069 if (page->mapping != inode->i_mapping) {
2071 return VM_FAULT_NOPAGE;
2074 fuse_wait_on_page_writeback(inode, page->index);
2075 return VM_FAULT_LOCKED;
2078 static const struct vm_operations_struct fuse_file_vm_ops = {
2079 .close = fuse_vma_close,
2080 .fault = filemap_fault,
2081 .page_mkwrite = fuse_page_mkwrite,
2082 .remap_pages = generic_file_remap_pages,
2085 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2087 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2088 fuse_link_write_file(file);
2090 file_accessed(file);
2091 vma->vm_ops = &fuse_file_vm_ops;
2095 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2097 /* Can't provide the coherency needed for MAP_SHARED */
2098 if (vma->vm_flags & VM_MAYSHARE)
2101 invalidate_inode_pages2(file->f_mapping);
2103 return generic_file_mmap(file, vma);
2106 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2107 struct file_lock *fl)
2109 switch (ffl->type) {
2115 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2116 ffl->end < ffl->start)
2119 fl->fl_start = ffl->start;
2120 fl->fl_end = ffl->end;
2121 fl->fl_pid = ffl->pid;
2127 fl->fl_type = ffl->type;
2131 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
2132 const struct file_lock *fl, int opcode, pid_t pid,
2135 struct inode *inode = file_inode(file);
2136 struct fuse_conn *fc = get_fuse_conn(inode);
2137 struct fuse_file *ff = file->private_data;
2138 struct fuse_lk_in *arg = &req->misc.lk_in;
2141 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2142 arg->lk.start = fl->fl_start;
2143 arg->lk.end = fl->fl_end;
2144 arg->lk.type = fl->fl_type;
2147 arg->lk_flags |= FUSE_LK_FLOCK;
2148 req->in.h.opcode = opcode;
2149 req->in.h.nodeid = get_node_id(inode);
2150 req->in.numargs = 1;
2151 req->in.args[0].size = sizeof(*arg);
2152 req->in.args[0].value = arg;
2155 static int fuse_getlk(struct file *file, struct file_lock *fl)
2157 struct inode *inode = file_inode(file);
2158 struct fuse_conn *fc = get_fuse_conn(inode);
2159 struct fuse_req *req;
2160 struct fuse_lk_out outarg;
2163 req = fuse_get_req_nopages(fc);
2165 return PTR_ERR(req);
2167 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
2168 req->out.numargs = 1;
2169 req->out.args[0].size = sizeof(outarg);
2170 req->out.args[0].value = &outarg;
2171 fuse_request_send(fc, req);
2172 err = req->out.h.error;
2173 fuse_put_request(fc, req);
2175 err = convert_fuse_file_lock(&outarg.lk, fl);
2180 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2182 struct inode *inode = file_inode(file);
2183 struct fuse_conn *fc = get_fuse_conn(inode);
2184 struct fuse_req *req;
2185 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2186 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2189 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2190 /* NLM needs asynchronous locks, which we don't support yet */
2194 /* Unlock on close is handled by the flush method */
2195 if (fl->fl_flags & FL_CLOSE)
2198 req = fuse_get_req_nopages(fc);
2200 return PTR_ERR(req);
2202 fuse_lk_fill(req, file, fl, opcode, pid, flock);
2203 fuse_request_send(fc, req);
2204 err = req->out.h.error;
2205 /* locking is restartable */
2208 fuse_put_request(fc, req);
2212 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2214 struct inode *inode = file_inode(file);
2215 struct fuse_conn *fc = get_fuse_conn(inode);
2218 if (cmd == F_CANCELLK) {
2220 } else if (cmd == F_GETLK) {
2222 posix_test_lock(file, fl);
2225 err = fuse_getlk(file, fl);
2228 err = posix_lock_file(file, fl, NULL);
2230 err = fuse_setlk(file, fl, 0);
2235 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2237 struct inode *inode = file_inode(file);
2238 struct fuse_conn *fc = get_fuse_conn(inode);
2242 err = flock_lock_file_wait(file, fl);
2244 struct fuse_file *ff = file->private_data;
2246 /* emulate flock with POSIX locks */
2247 fl->fl_owner = (fl_owner_t) file;
2249 err = fuse_setlk(file, fl, 1);
2255 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2257 struct inode *inode = mapping->host;
2258 struct fuse_conn *fc = get_fuse_conn(inode);
2259 struct fuse_req *req;
2260 struct fuse_bmap_in inarg;
2261 struct fuse_bmap_out outarg;
2264 if (!inode->i_sb->s_bdev || fc->no_bmap)
2267 req = fuse_get_req_nopages(fc);
2271 memset(&inarg, 0, sizeof(inarg));
2272 inarg.block = block;
2273 inarg.blocksize = inode->i_sb->s_blocksize;
2274 req->in.h.opcode = FUSE_BMAP;
2275 req->in.h.nodeid = get_node_id(inode);
2276 req->in.numargs = 1;
2277 req->in.args[0].size = sizeof(inarg);
2278 req->in.args[0].value = &inarg;
2279 req->out.numargs = 1;
2280 req->out.args[0].size = sizeof(outarg);
2281 req->out.args[0].value = &outarg;
2282 fuse_request_send(fc, req);
2283 err = req->out.h.error;
2284 fuse_put_request(fc, req);
2288 return err ? 0 : outarg.block;
2291 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2294 struct inode *inode = file_inode(file);
2296 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2297 if (whence == SEEK_CUR || whence == SEEK_SET)
2298 return generic_file_llseek(file, offset, whence);
2300 mutex_lock(&inode->i_mutex);
2301 retval = fuse_update_attributes(inode, NULL, file, NULL);
2303 retval = generic_file_llseek(file, offset, whence);
2304 mutex_unlock(&inode->i_mutex);
2309 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2310 unsigned int nr_segs, size_t bytes, bool to_user)
2318 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2320 while (iov_iter_count(&ii)) {
2321 struct page *page = pages[page_idx++];
2322 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
2328 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2329 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2330 size_t copy = min(todo, iov_len);
2334 left = copy_from_user(kaddr, uaddr, copy);
2336 left = copy_to_user(uaddr, kaddr, copy);
2341 iov_iter_advance(&ii, copy);
2353 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2354 * ABI was defined to be 'struct iovec' which is different on 32bit
2355 * and 64bit. Fortunately we can determine which structure the server
2356 * used from the size of the reply.
2358 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2359 size_t transferred, unsigned count,
2362 #ifdef CONFIG_COMPAT
2363 if (count * sizeof(struct compat_iovec) == transferred) {
2364 struct compat_iovec *ciov = src;
2368 * With this interface a 32bit server cannot support
2369 * non-compat (i.e. ones coming from 64bit apps) ioctl
2375 for (i = 0; i < count; i++) {
2376 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2377 dst[i].iov_len = ciov[i].iov_len;
2383 if (count * sizeof(struct iovec) != transferred)
2386 memcpy(dst, src, transferred);
2390 /* Make sure iov_length() won't overflow */
2391 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2394 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2396 for (n = 0; n < count; n++, iov++) {
2397 if (iov->iov_len > (size_t) max)
2399 max -= iov->iov_len;
2404 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2405 void *src, size_t transferred, unsigned count,
2409 struct fuse_ioctl_iovec *fiov = src;
2411 if (fc->minor < 16) {
2412 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2416 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2419 for (i = 0; i < count; i++) {
2420 /* Did the server supply an inappropriate value? */
2421 if (fiov[i].base != (unsigned long) fiov[i].base ||
2422 fiov[i].len != (unsigned long) fiov[i].len)
2425 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2426 dst[i].iov_len = (size_t) fiov[i].len;
2428 #ifdef CONFIG_COMPAT
2430 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2431 (compat_size_t) dst[i].iov_len != fiov[i].len))
2441 * For ioctls, there is no generic way to determine how much memory
2442 * needs to be read and/or written. Furthermore, ioctls are allowed
2443 * to dereference the passed pointer, so the parameter requires deep
2444 * copying but FUSE has no idea whatsoever about what to copy in or
2447 * This is solved by allowing FUSE server to retry ioctl with
2448 * necessary in/out iovecs. Let's assume the ioctl implementation
2449 * needs to read in the following structure.
2456 * On the first callout to FUSE server, inarg->in_size and
2457 * inarg->out_size will be NULL; then, the server completes the ioctl
2458 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2459 * the actual iov array to
2461 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2463 * which tells FUSE to copy in the requested area and retry the ioctl.
2464 * On the second round, the server has access to the structure and
2465 * from that it can tell what to look for next, so on the invocation,
2466 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2468 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2469 * { .iov_base = a.buf, .iov_len = a.buflen } }
2471 * FUSE will copy both struct a and the pointed buffer from the
2472 * process doing the ioctl and retry ioctl with both struct a and the
2475 * This time, FUSE server has everything it needs and completes ioctl
2476 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2478 * Copying data out works the same way.
2480 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2481 * automatically initializes in and out iovs by decoding @cmd with
2482 * _IOC_* macros and the server is not allowed to request RETRY. This
2483 * limits ioctl data transfers to well-formed ioctls and is the forced
2484 * behavior for all FUSE servers.
2486 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2489 struct fuse_file *ff = file->private_data;
2490 struct fuse_conn *fc = ff->fc;
2491 struct fuse_ioctl_in inarg = {
2497 struct fuse_ioctl_out outarg;
2498 struct fuse_req *req = NULL;
2499 struct page **pages = NULL;
2500 struct iovec *iov_page = NULL;
2501 struct iovec *in_iov = NULL, *out_iov = NULL;
2502 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2503 size_t in_size, out_size, transferred;
2506 #if BITS_PER_LONG == 32
2507 inarg.flags |= FUSE_IOCTL_32BIT;
2509 if (flags & FUSE_IOCTL_COMPAT)
2510 inarg.flags |= FUSE_IOCTL_32BIT;
2513 /* assume all the iovs returned by client always fits in a page */
2514 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2517 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2518 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2519 if (!pages || !iov_page)
2523 * If restricted, initialize IO parameters as encoded in @cmd.
2524 * RETRY from server is not allowed.
2526 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2527 struct iovec *iov = iov_page;
2529 iov->iov_base = (void __user *)arg;
2530 iov->iov_len = _IOC_SIZE(cmd);
2532 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2537 if (_IOC_DIR(cmd) & _IOC_READ) {
2544 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2545 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2548 * Out data can be used either for actual out data or iovs,
2549 * make sure there always is at least one page.
2551 out_size = max_t(size_t, out_size, PAGE_SIZE);
2552 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2554 /* make sure there are enough buffer pages and init request with them */
2556 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2558 while (num_pages < max_pages) {
2559 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2560 if (!pages[num_pages])
2565 req = fuse_get_req(fc, num_pages);
2571 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2572 req->num_pages = num_pages;
2573 fuse_page_descs_length_init(req, 0, req->num_pages);
2575 /* okay, let's send it to the client */
2576 req->in.h.opcode = FUSE_IOCTL;
2577 req->in.h.nodeid = ff->nodeid;
2578 req->in.numargs = 1;
2579 req->in.args[0].size = sizeof(inarg);
2580 req->in.args[0].value = &inarg;
2583 req->in.args[1].size = in_size;
2584 req->in.argpages = 1;
2586 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2592 req->out.numargs = 2;
2593 req->out.args[0].size = sizeof(outarg);
2594 req->out.args[0].value = &outarg;
2595 req->out.args[1].size = out_size;
2596 req->out.argpages = 1;
2597 req->out.argvar = 1;
2599 fuse_request_send(fc, req);
2600 err = req->out.h.error;
2601 transferred = req->out.args[1].size;
2602 fuse_put_request(fc, req);
2607 /* did it ask for retry? */
2608 if (outarg.flags & FUSE_IOCTL_RETRY) {
2611 /* no retry if in restricted mode */
2613 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2616 in_iovs = outarg.in_iovs;
2617 out_iovs = outarg.out_iovs;
2620 * Make sure things are in boundary, separate checks
2621 * are to protect against overflow.
2624 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2625 out_iovs > FUSE_IOCTL_MAX_IOV ||
2626 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2629 vaddr = kmap_atomic(pages[0]);
2630 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2631 transferred, in_iovs + out_iovs,
2632 (flags & FUSE_IOCTL_COMPAT) != 0);
2633 kunmap_atomic(vaddr);
2638 out_iov = in_iov + in_iovs;
2640 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2644 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2652 if (transferred > inarg.out_size)
2655 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2658 fuse_put_request(fc, req);
2659 free_page((unsigned long) iov_page);
2661 __free_page(pages[--num_pages]);
2664 return err ? err : outarg.result;
2666 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2668 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2669 unsigned long arg, unsigned int flags)
2671 struct inode *inode = file_inode(file);
2672 struct fuse_conn *fc = get_fuse_conn(inode);
2674 if (!fuse_allow_current_process(fc))
2677 if (is_bad_inode(inode))
2680 return fuse_do_ioctl(file, cmd, arg, flags);
2683 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2686 return fuse_ioctl_common(file, cmd, arg, 0);
2689 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2692 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2696 * All files which have been polled are linked to RB tree
2697 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2698 * find the matching one.
2700 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2701 struct rb_node **parent_out)
2703 struct rb_node **link = &fc->polled_files.rb_node;
2704 struct rb_node *last = NULL;
2707 struct fuse_file *ff;
2710 ff = rb_entry(last, struct fuse_file, polled_node);
2713 link = &last->rb_left;
2714 else if (kh > ff->kh)
2715 link = &last->rb_right;
2726 * The file is about to be polled. Make sure it's on the polled_files
2727 * RB tree. Note that files once added to the polled_files tree are
2728 * not removed before the file is released. This is because a file
2729 * polled once is likely to be polled again.
2731 static void fuse_register_polled_file(struct fuse_conn *fc,
2732 struct fuse_file *ff)
2734 spin_lock(&fc->lock);
2735 if (RB_EMPTY_NODE(&ff->polled_node)) {
2736 struct rb_node **link, *parent;
2738 link = fuse_find_polled_node(fc, ff->kh, &parent);
2740 rb_link_node(&ff->polled_node, parent, link);
2741 rb_insert_color(&ff->polled_node, &fc->polled_files);
2743 spin_unlock(&fc->lock);
2746 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2748 struct fuse_file *ff = file->private_data;
2749 struct fuse_conn *fc = ff->fc;
2750 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2751 struct fuse_poll_out outarg;
2752 struct fuse_req *req;
2756 return DEFAULT_POLLMASK;
2758 poll_wait(file, &ff->poll_wait, wait);
2759 inarg.events = (__u32)poll_requested_events(wait);
2762 * Ask for notification iff there's someone waiting for it.
2763 * The client may ignore the flag and always notify.
2765 if (waitqueue_active(&ff->poll_wait)) {
2766 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2767 fuse_register_polled_file(fc, ff);
2770 req = fuse_get_req_nopages(fc);
2774 req->in.h.opcode = FUSE_POLL;
2775 req->in.h.nodeid = ff->nodeid;
2776 req->in.numargs = 1;
2777 req->in.args[0].size = sizeof(inarg);
2778 req->in.args[0].value = &inarg;
2779 req->out.numargs = 1;
2780 req->out.args[0].size = sizeof(outarg);
2781 req->out.args[0].value = &outarg;
2782 fuse_request_send(fc, req);
2783 err = req->out.h.error;
2784 fuse_put_request(fc, req);
2787 return outarg.revents;
2788 if (err == -ENOSYS) {
2790 return DEFAULT_POLLMASK;
2794 EXPORT_SYMBOL_GPL(fuse_file_poll);
2797 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2798 * wakes up the poll waiters.
2800 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2801 struct fuse_notify_poll_wakeup_out *outarg)
2803 u64 kh = outarg->kh;
2804 struct rb_node **link;
2806 spin_lock(&fc->lock);
2808 link = fuse_find_polled_node(fc, kh, NULL);
2810 struct fuse_file *ff;
2812 ff = rb_entry(*link, struct fuse_file, polled_node);
2813 wake_up_interruptible_sync(&ff->poll_wait);
2816 spin_unlock(&fc->lock);
2820 static void fuse_do_truncate(struct file *file)
2822 struct inode *inode = file->f_mapping->host;
2825 attr.ia_valid = ATTR_SIZE;
2826 attr.ia_size = i_size_read(inode);
2828 attr.ia_file = file;
2829 attr.ia_valid |= ATTR_FILE;
2831 fuse_do_setattr(inode, &attr, file);
2834 static inline loff_t fuse_round_up(loff_t off)
2836 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2840 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2841 loff_t offset, unsigned long nr_segs)
2844 struct file *file = iocb->ki_filp;
2845 struct fuse_file *ff = file->private_data;
2846 bool async_dio = ff->fc->async_dio;
2848 struct inode *inode;
2850 size_t count = iov_length(iov, nr_segs);
2851 struct fuse_io_priv *io;
2854 inode = file->f_mapping->host;
2855 i_size = i_size_read(inode);
2857 if ((rw == READ) && (offset > i_size))
2860 /* optimization for short read */
2861 if (async_dio && rw != WRITE && offset + count > i_size) {
2862 if (offset >= i_size)
2864 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
2867 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2870 spin_lock_init(&io->lock);
2874 io->offset = offset;
2875 io->write = (rw == WRITE);
2879 * By default, we want to optimize all I/Os with async request
2880 * submission to the client filesystem if supported.
2882 io->async = async_dio;
2886 * We cannot asynchronously extend the size of a file. We have no method
2887 * to wait on real async I/O requests, so we must submit this request
2890 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
2894 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
2896 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
2899 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2901 /* we have a non-extending, async request, so return */
2902 if (!is_sync_kiocb(iocb))
2903 return -EIOCBQUEUED;
2905 ret = wait_on_sync_kiocb(iocb);
2912 fuse_write_update_size(inode, pos);
2913 else if (ret < 0 && offset + count > i_size)
2914 fuse_do_truncate(file);
2920 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2923 struct fuse_file *ff = file->private_data;
2924 struct inode *inode = file->f_inode;
2925 struct fuse_inode *fi = get_fuse_inode(inode);
2926 struct fuse_conn *fc = ff->fc;
2927 struct fuse_req *req;
2928 struct fuse_fallocate_in inarg = {
2935 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2936 (mode & FALLOC_FL_PUNCH_HOLE);
2938 if (fc->no_fallocate)
2942 mutex_lock(&inode->i_mutex);
2943 if (mode & FALLOC_FL_PUNCH_HOLE) {
2944 loff_t endbyte = offset + length - 1;
2945 err = filemap_write_and_wait_range(inode->i_mapping,
2950 fuse_sync_writes(inode);
2954 if (!(mode & FALLOC_FL_KEEP_SIZE))
2955 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
2957 req = fuse_get_req_nopages(fc);
2963 req->in.h.opcode = FUSE_FALLOCATE;
2964 req->in.h.nodeid = ff->nodeid;
2965 req->in.numargs = 1;
2966 req->in.args[0].size = sizeof(inarg);
2967 req->in.args[0].value = &inarg;
2968 fuse_request_send(fc, req);
2969 err = req->out.h.error;
2970 if (err == -ENOSYS) {
2971 fc->no_fallocate = 1;
2974 fuse_put_request(fc, req);
2979 /* we could have extended the file */
2980 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
2981 bool changed = fuse_write_update_size(inode, offset + length);
2983 if (changed && fc->writeback_cache) {
2984 struct fuse_inode *fi = get_fuse_inode(inode);
2986 inode->i_mtime = current_fs_time(inode->i_sb);
2987 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
2991 if (mode & FALLOC_FL_PUNCH_HOLE)
2992 truncate_pagecache_range(inode, offset, offset + length - 1);
2994 fuse_invalidate_attr(inode);
2997 if (!(mode & FALLOC_FL_KEEP_SIZE))
2998 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3001 mutex_unlock(&inode->i_mutex);
3006 static const struct file_operations fuse_file_operations = {
3007 .llseek = fuse_file_llseek,
3008 .read = do_sync_read,
3009 .aio_read = fuse_file_aio_read,
3010 .write = do_sync_write,
3011 .aio_write = fuse_file_aio_write,
3012 .mmap = fuse_file_mmap,
3014 .flush = fuse_flush,
3015 .release = fuse_release,
3016 .fsync = fuse_fsync,
3017 .lock = fuse_file_lock,
3018 .flock = fuse_file_flock,
3019 .splice_read = generic_file_splice_read,
3020 .unlocked_ioctl = fuse_file_ioctl,
3021 .compat_ioctl = fuse_file_compat_ioctl,
3022 .poll = fuse_file_poll,
3023 .fallocate = fuse_file_fallocate,
3026 static const struct file_operations fuse_direct_io_file_operations = {
3027 .llseek = fuse_file_llseek,
3028 .read = fuse_direct_read,
3029 .write = fuse_direct_write,
3030 .mmap = fuse_direct_mmap,
3032 .flush = fuse_flush,
3033 .release = fuse_release,
3034 .fsync = fuse_fsync,
3035 .lock = fuse_file_lock,
3036 .flock = fuse_file_flock,
3037 .unlocked_ioctl = fuse_file_ioctl,
3038 .compat_ioctl = fuse_file_compat_ioctl,
3039 .poll = fuse_file_poll,
3040 .fallocate = fuse_file_fallocate,
3041 /* no splice_read */
3044 static const struct address_space_operations fuse_file_aops = {
3045 .readpage = fuse_readpage,
3046 .writepage = fuse_writepage,
3047 .writepages = fuse_writepages,
3048 .launder_page = fuse_launder_page,
3049 .readpages = fuse_readpages,
3050 .set_page_dirty = __set_page_dirty_nobuffers,
3052 .direct_IO = fuse_direct_IO,
3053 .write_begin = fuse_write_begin,
3054 .write_end = fuse_write_end,
3057 void fuse_init_file_inode(struct inode *inode)
3059 inode->i_fop = &fuse_file_operations;
3060 inode->i_data.a_ops = &fuse_file_aops;