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>
20 static const struct file_operations fuse_direct_io_file_operations;
22 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
25 struct fuse_open_in inarg;
29 req = fuse_get_req_nopages(fc);
33 memset(&inarg, 0, sizeof(inarg));
34 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
35 if (!fc->atomic_o_trunc)
36 inarg.flags &= ~O_TRUNC;
37 req->in.h.opcode = opcode;
38 req->in.h.nodeid = nodeid;
40 req->in.args[0].size = sizeof(inarg);
41 req->in.args[0].value = &inarg;
43 req->out.args[0].size = sizeof(*outargp);
44 req->out.args[0].value = outargp;
45 fuse_request_send(fc, req);
46 err = req->out.h.error;
47 fuse_put_request(fc, req);
52 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
56 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
61 ff->reserved_req = fuse_request_alloc(0);
62 if (unlikely(!ff->reserved_req)) {
67 INIT_LIST_HEAD(&ff->write_entry);
68 atomic_set(&ff->count, 0);
69 RB_CLEAR_NODE(&ff->polled_node);
70 init_waitqueue_head(&ff->poll_wait);
74 spin_unlock(&fc->lock);
79 void fuse_file_free(struct fuse_file *ff)
81 fuse_request_free(ff->reserved_req);
85 struct fuse_file *fuse_file_get(struct fuse_file *ff)
87 atomic_inc(&ff->count);
91 static void fuse_release_async(struct work_struct *work)
97 req = container_of(work, struct fuse_req, misc.release.work);
98 path = req->misc.release.path;
99 fc = get_fuse_conn(path.dentry->d_inode);
101 fuse_put_request(fc, req);
105 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
107 if (fc->destroy_req) {
109 * If this is a fuseblk mount, then it's possible that
110 * releasing the path will result in releasing the
111 * super block and sending the DESTROY request. If
112 * the server is single threaded, this would hang.
113 * For this reason do the path_put() in a separate
116 atomic_inc(&req->count);
117 INIT_WORK(&req->misc.release.work, fuse_release_async);
118 schedule_work(&req->misc.release.work);
120 path_put(&req->misc.release.path);
124 static void fuse_file_put(struct fuse_file *ff, bool sync)
126 if (atomic_dec_and_test(&ff->count)) {
127 struct fuse_req *req = ff->reserved_req;
131 fuse_request_send(ff->fc, req);
132 path_put(&req->misc.release.path);
133 fuse_put_request(ff->fc, req);
135 req->end = fuse_release_end;
137 fuse_request_send_background(ff->fc, req);
143 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
146 struct fuse_open_out outarg;
147 struct fuse_file *ff;
149 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
151 ff = fuse_file_alloc(fc);
155 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
162 outarg.open_flags &= ~FOPEN_DIRECT_IO;
166 ff->open_flags = outarg.open_flags;
167 file->private_data = fuse_file_get(ff);
171 EXPORT_SYMBOL_GPL(fuse_do_open);
173 void fuse_finish_open(struct inode *inode, struct file *file)
175 struct fuse_file *ff = file->private_data;
176 struct fuse_conn *fc = get_fuse_conn(inode);
178 if (ff->open_flags & FOPEN_DIRECT_IO)
179 file->f_op = &fuse_direct_io_file_operations;
180 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
181 invalidate_inode_pages2(inode->i_mapping);
182 if (ff->open_flags & FOPEN_NONSEEKABLE)
183 nonseekable_open(inode, file);
184 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
185 struct fuse_inode *fi = get_fuse_inode(inode);
187 spin_lock(&fc->lock);
188 fi->attr_version = ++fc->attr_version;
189 i_size_write(inode, 0);
190 spin_unlock(&fc->lock);
191 fuse_invalidate_attr(inode);
195 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
197 struct fuse_conn *fc = get_fuse_conn(inode);
200 err = generic_file_open(inode, file);
204 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
208 fuse_finish_open(inode, file);
213 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
215 struct fuse_conn *fc = ff->fc;
216 struct fuse_req *req = ff->reserved_req;
217 struct fuse_release_in *inarg = &req->misc.release.in;
219 spin_lock(&fc->lock);
220 list_del(&ff->write_entry);
221 if (!RB_EMPTY_NODE(&ff->polled_node))
222 rb_erase(&ff->polled_node, &fc->polled_files);
223 spin_unlock(&fc->lock);
225 wake_up_interruptible_all(&ff->poll_wait);
228 inarg->flags = flags;
229 req->in.h.opcode = opcode;
230 req->in.h.nodeid = ff->nodeid;
232 req->in.args[0].size = sizeof(struct fuse_release_in);
233 req->in.args[0].value = inarg;
236 void fuse_release_common(struct file *file, int opcode)
238 struct fuse_file *ff;
239 struct fuse_req *req;
241 ff = file->private_data;
245 req = ff->reserved_req;
246 fuse_prepare_release(ff, file->f_flags, opcode);
249 struct fuse_release_in *inarg = &req->misc.release.in;
250 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
251 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
254 /* Hold vfsmount and dentry until release is finished */
255 path_get(&file->f_path);
256 req->misc.release.path = file->f_path;
259 * Normally this will send the RELEASE request, however if
260 * some asynchronous READ or WRITE requests are outstanding,
261 * the sending will be delayed.
263 * Make the release synchronous if this is a fuseblk mount,
264 * synchronous RELEASE is allowed (and desirable) in this case
265 * because the server can be trusted not to screw up.
267 fuse_file_put(ff, ff->fc->destroy_req != NULL);
270 static int fuse_open(struct inode *inode, struct file *file)
272 return fuse_open_common(inode, file, false);
275 static int fuse_release(struct inode *inode, struct file *file)
277 fuse_release_common(file, FUSE_RELEASE);
279 /* return value is ignored by VFS */
283 void fuse_sync_release(struct fuse_file *ff, int flags)
285 WARN_ON(atomic_read(&ff->count) > 1);
286 fuse_prepare_release(ff, flags, FUSE_RELEASE);
287 ff->reserved_req->force = 1;
288 ff->reserved_req->background = 0;
289 fuse_request_send(ff->fc, ff->reserved_req);
290 fuse_put_request(ff->fc, ff->reserved_req);
293 EXPORT_SYMBOL_GPL(fuse_sync_release);
296 * Scramble the ID space with XTEA, so that the value of the files_struct
297 * pointer is not exposed to userspace.
299 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
301 u32 *k = fc->scramble_key;
302 u64 v = (unsigned long) id;
308 for (i = 0; i < 32; i++) {
309 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
311 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
314 return (u64) v0 + ((u64) v1 << 32);
318 * Check if page is under writeback
320 * This is currently done by walking the list of writepage requests
321 * for the inode, which can be pretty inefficient.
323 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
325 struct fuse_conn *fc = get_fuse_conn(inode);
326 struct fuse_inode *fi = get_fuse_inode(inode);
327 struct fuse_req *req;
330 spin_lock(&fc->lock);
331 list_for_each_entry(req, &fi->writepages, writepages_entry) {
334 BUG_ON(req->inode != inode);
335 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
336 if (curr_index == index) {
341 spin_unlock(&fc->lock);
347 * Wait for page writeback to be completed.
349 * Since fuse doesn't rely on the VM writeback tracking, this has to
350 * use some other means.
352 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
354 struct fuse_inode *fi = get_fuse_inode(inode);
356 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
360 static int fuse_flush(struct file *file, fl_owner_t id)
362 struct inode *inode = file_inode(file);
363 struct fuse_conn *fc = get_fuse_conn(inode);
364 struct fuse_file *ff = file->private_data;
365 struct fuse_req *req;
366 struct fuse_flush_in inarg;
369 if (is_bad_inode(inode))
375 req = fuse_get_req_nofail_nopages(fc, file);
376 memset(&inarg, 0, sizeof(inarg));
378 inarg.lock_owner = fuse_lock_owner_id(fc, id);
379 req->in.h.opcode = FUSE_FLUSH;
380 req->in.h.nodeid = get_node_id(inode);
382 req->in.args[0].size = sizeof(inarg);
383 req->in.args[0].value = &inarg;
385 fuse_request_send(fc, req);
386 err = req->out.h.error;
387 fuse_put_request(fc, req);
388 if (err == -ENOSYS) {
396 * Wait for all pending writepages on the inode to finish.
398 * This is currently done by blocking further writes with FUSE_NOWRITE
399 * and waiting for all sent writes to complete.
401 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
402 * could conflict with truncation.
404 static void fuse_sync_writes(struct inode *inode)
406 fuse_set_nowrite(inode);
407 fuse_release_nowrite(inode);
410 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
411 int datasync, int isdir)
413 struct inode *inode = file->f_mapping->host;
414 struct fuse_conn *fc = get_fuse_conn(inode);
415 struct fuse_file *ff = file->private_data;
416 struct fuse_req *req;
417 struct fuse_fsync_in inarg;
420 if (is_bad_inode(inode))
423 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
427 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
430 mutex_lock(&inode->i_mutex);
433 * Start writeback against all dirty pages of the inode, then
434 * wait for all outstanding writes, before sending the FSYNC
437 err = write_inode_now(inode, 0);
441 fuse_sync_writes(inode);
443 req = fuse_get_req_nopages(fc);
449 memset(&inarg, 0, sizeof(inarg));
451 inarg.fsync_flags = datasync ? 1 : 0;
452 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
453 req->in.h.nodeid = get_node_id(inode);
455 req->in.args[0].size = sizeof(inarg);
456 req->in.args[0].value = &inarg;
457 fuse_request_send(fc, req);
458 err = req->out.h.error;
459 fuse_put_request(fc, req);
460 if (err == -ENOSYS) {
468 mutex_unlock(&inode->i_mutex);
472 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
475 return fuse_fsync_common(file, start, end, datasync, 0);
478 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
479 size_t count, int opcode)
481 struct fuse_read_in *inarg = &req->misc.read.in;
482 struct fuse_file *ff = file->private_data;
487 inarg->flags = file->f_flags;
488 req->in.h.opcode = opcode;
489 req->in.h.nodeid = ff->nodeid;
491 req->in.args[0].size = sizeof(struct fuse_read_in);
492 req->in.args[0].value = inarg;
494 req->out.numargs = 1;
495 req->out.args[0].size = count;
498 static void fuse_release_user_pages(struct fuse_req *req, int write)
502 for (i = 0; i < req->num_pages; i++) {
503 struct page *page = req->pages[i];
505 set_page_dirty_lock(page);
511 * In case of short read, the caller sets 'pos' to the position of
512 * actual end of fuse request in IO request. Otherwise, if bytes_requested
513 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
516 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
517 * both submitted asynchronously. The first of them was ACKed by userspace as
518 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
519 * second request was ACKed as short, e.g. only 1K was read, resulting in
522 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
523 * will be equal to the length of the longest contiguous fragment of
524 * transferred data starting from the beginning of IO request.
526 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
530 spin_lock(&io->lock);
532 io->err = io->err ? : err;
533 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
537 spin_unlock(&io->lock);
544 else if (io->bytes >= 0 && io->write)
547 res = io->bytes < 0 ? io->size : io->bytes;
549 if (!is_sync_kiocb(io->iocb)) {
550 struct path *path = &io->iocb->ki_filp->f_path;
551 struct inode *inode = path->dentry->d_inode;
552 struct fuse_conn *fc = get_fuse_conn(inode);
553 struct fuse_inode *fi = get_fuse_inode(inode);
555 spin_lock(&fc->lock);
556 fi->attr_version = ++fc->attr_version;
557 spin_unlock(&fc->lock);
561 aio_complete(io->iocb, res, 0);
566 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
568 struct fuse_io_priv *io = req->io;
571 fuse_release_user_pages(req, !io->write);
574 if (req->misc.write.in.size != req->misc.write.out.size)
575 pos = req->misc.write.in.offset - io->offset +
576 req->misc.write.out.size;
578 if (req->misc.read.in.size != req->out.args[0].size)
579 pos = req->misc.read.in.offset - io->offset +
580 req->out.args[0].size;
583 fuse_aio_complete(io, req->out.h.error, pos);
586 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
587 size_t num_bytes, struct fuse_io_priv *io)
589 spin_lock(&io->lock);
590 io->size += num_bytes;
592 spin_unlock(&io->lock);
595 req->end = fuse_aio_complete_req;
597 __fuse_get_request(req);
598 fuse_request_send_background(fc, req);
603 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
604 loff_t pos, size_t count, fl_owner_t owner)
606 struct file *file = io->file;
607 struct fuse_file *ff = file->private_data;
608 struct fuse_conn *fc = ff->fc;
610 fuse_read_fill(req, file, pos, count, FUSE_READ);
612 struct fuse_read_in *inarg = &req->misc.read.in;
614 inarg->read_flags |= FUSE_READ_LOCKOWNER;
615 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
619 return fuse_async_req_send(fc, req, count, io);
621 fuse_request_send(fc, req);
622 return req->out.args[0].size;
625 static void fuse_read_update_size(struct inode *inode, loff_t size,
628 struct fuse_conn *fc = get_fuse_conn(inode);
629 struct fuse_inode *fi = get_fuse_inode(inode);
631 spin_lock(&fc->lock);
632 if (attr_ver == fi->attr_version && size < inode->i_size) {
633 fi->attr_version = ++fc->attr_version;
634 i_size_write(inode, size);
636 spin_unlock(&fc->lock);
639 static int fuse_readpage(struct file *file, struct page *page)
641 struct fuse_io_priv io = { .async = 0, .file = file };
642 struct inode *inode = page->mapping->host;
643 struct fuse_conn *fc = get_fuse_conn(inode);
644 struct fuse_req *req;
646 loff_t pos = page_offset(page);
647 size_t count = PAGE_CACHE_SIZE;
652 if (is_bad_inode(inode))
656 * Page writeback can extend beyond the lifetime of the
657 * page-cache page, so make sure we read a properly synced
660 fuse_wait_on_page_writeback(inode, page->index);
662 req = fuse_get_req(fc, 1);
667 attr_ver = fuse_get_attr_version(fc);
669 req->out.page_zeroing = 1;
670 req->out.argpages = 1;
672 req->pages[0] = page;
673 req->page_descs[0].length = count;
674 num_read = fuse_send_read(req, &io, pos, count, NULL);
675 err = req->out.h.error;
676 fuse_put_request(fc, req);
680 * Short read means EOF. If file size is larger, truncate it
682 if (num_read < count)
683 fuse_read_update_size(inode, pos + num_read, attr_ver);
685 SetPageUptodate(page);
688 fuse_invalidate_attr(inode); /* atime changed */
694 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
697 size_t count = req->misc.read.in.size;
698 size_t num_read = req->out.args[0].size;
699 struct address_space *mapping = NULL;
701 for (i = 0; mapping == NULL && i < req->num_pages; i++)
702 mapping = req->pages[i]->mapping;
705 struct inode *inode = mapping->host;
708 * Short read means EOF. If file size is larger, truncate it
710 if (!req->out.h.error && num_read < count) {
713 pos = page_offset(req->pages[0]) + num_read;
714 fuse_read_update_size(inode, pos,
715 req->misc.read.attr_ver);
717 fuse_invalidate_attr(inode); /* atime changed */
720 for (i = 0; i < req->num_pages; i++) {
721 struct page *page = req->pages[i];
722 if (!req->out.h.error)
723 SetPageUptodate(page);
727 page_cache_release(page);
730 fuse_file_put(req->ff, false);
733 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
735 struct fuse_file *ff = file->private_data;
736 struct fuse_conn *fc = ff->fc;
737 loff_t pos = page_offset(req->pages[0]);
738 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
740 req->out.argpages = 1;
741 req->out.page_zeroing = 1;
742 req->out.page_replace = 1;
743 fuse_read_fill(req, file, pos, count, FUSE_READ);
744 req->misc.read.attr_ver = fuse_get_attr_version(fc);
745 if (fc->async_read) {
746 req->ff = fuse_file_get(ff);
747 req->end = fuse_readpages_end;
748 fuse_request_send_background(fc, req);
750 fuse_request_send(fc, req);
751 fuse_readpages_end(fc, req);
752 fuse_put_request(fc, req);
756 struct fuse_fill_data {
757 struct fuse_req *req;
763 static int fuse_readpages_fill(void *_data, struct page *page)
765 struct fuse_fill_data *data = _data;
766 struct fuse_req *req = data->req;
767 struct inode *inode = data->inode;
768 struct fuse_conn *fc = get_fuse_conn(inode);
770 fuse_wait_on_page_writeback(inode, page->index);
772 if (req->num_pages &&
773 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
774 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
775 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
776 int nr_alloc = min_t(unsigned, data->nr_pages,
777 FUSE_MAX_PAGES_PER_REQ);
778 fuse_send_readpages(req, data->file);
780 req = fuse_get_req_for_background(fc, nr_alloc);
782 req = fuse_get_req(fc, nr_alloc);
791 if (WARN_ON(req->num_pages >= req->max_pages)) {
792 fuse_put_request(fc, req);
796 page_cache_get(page);
797 req->pages[req->num_pages] = page;
798 req->page_descs[req->num_pages].length = PAGE_SIZE;
804 static int fuse_readpages(struct file *file, struct address_space *mapping,
805 struct list_head *pages, unsigned nr_pages)
807 struct inode *inode = mapping->host;
808 struct fuse_conn *fc = get_fuse_conn(inode);
809 struct fuse_fill_data data;
811 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
814 if (is_bad_inode(inode))
820 data.req = fuse_get_req_for_background(fc, nr_alloc);
822 data.req = fuse_get_req(fc, nr_alloc);
823 data.nr_pages = nr_pages;
824 err = PTR_ERR(data.req);
825 if (IS_ERR(data.req))
828 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
830 if (data.req->num_pages)
831 fuse_send_readpages(data.req, file);
833 fuse_put_request(fc, data.req);
839 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
840 unsigned long nr_segs, loff_t pos)
842 struct inode *inode = iocb->ki_filp->f_mapping->host;
843 struct fuse_conn *fc = get_fuse_conn(inode);
846 * In auto invalidate mode, always update attributes on read.
847 * Otherwise, only update if we attempt to read past EOF (to ensure
848 * i_size is up to date).
850 if (fc->auto_inval_data ||
851 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
853 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
858 return generic_file_aio_read(iocb, iov, nr_segs, pos);
861 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
862 loff_t pos, size_t count)
864 struct fuse_write_in *inarg = &req->misc.write.in;
865 struct fuse_write_out *outarg = &req->misc.write.out;
870 req->in.h.opcode = FUSE_WRITE;
871 req->in.h.nodeid = ff->nodeid;
873 if (ff->fc->minor < 9)
874 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
876 req->in.args[0].size = sizeof(struct fuse_write_in);
877 req->in.args[0].value = inarg;
878 req->in.args[1].size = count;
879 req->out.numargs = 1;
880 req->out.args[0].size = sizeof(struct fuse_write_out);
881 req->out.args[0].value = outarg;
884 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
885 loff_t pos, size_t count, fl_owner_t owner)
887 struct file *file = io->file;
888 struct fuse_file *ff = file->private_data;
889 struct fuse_conn *fc = ff->fc;
890 struct fuse_write_in *inarg = &req->misc.write.in;
892 fuse_write_fill(req, ff, pos, count);
893 inarg->flags = file->f_flags;
895 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
896 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
900 return fuse_async_req_send(fc, req, count, io);
902 fuse_request_send(fc, req);
903 return req->misc.write.out.size;
906 void fuse_write_update_size(struct inode *inode, loff_t pos)
908 struct fuse_conn *fc = get_fuse_conn(inode);
909 struct fuse_inode *fi = get_fuse_inode(inode);
911 spin_lock(&fc->lock);
912 fi->attr_version = ++fc->attr_version;
913 if (pos > inode->i_size)
914 i_size_write(inode, pos);
915 spin_unlock(&fc->lock);
918 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
919 struct inode *inode, loff_t pos,
925 struct fuse_io_priv io = { .async = 0, .file = file };
927 for (i = 0; i < req->num_pages; i++)
928 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
930 res = fuse_send_write(req, &io, pos, count, NULL);
932 offset = req->page_descs[0].offset;
934 for (i = 0; i < req->num_pages; i++) {
935 struct page *page = req->pages[i];
937 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
938 SetPageUptodate(page);
940 if (count > PAGE_CACHE_SIZE - offset)
941 count -= PAGE_CACHE_SIZE - offset;
947 page_cache_release(page);
953 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
954 struct address_space *mapping,
955 struct iov_iter *ii, loff_t pos)
957 struct fuse_conn *fc = get_fuse_conn(mapping->host);
958 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
962 req->in.argpages = 1;
963 req->page_descs[0].offset = offset;
968 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
969 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
972 bytes = min_t(size_t, bytes, fc->max_write - count);
976 if (iov_iter_fault_in_readable(ii, bytes))
980 page = grab_cache_page_write_begin(mapping, index, 0);
984 if (mapping_writably_mapped(mapping))
985 flush_dcache_page(page);
988 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
990 flush_dcache_page(page);
992 mark_page_accessed(page);
996 page_cache_release(page);
997 bytes = min(bytes, iov_iter_single_seg_count(ii));
1002 req->pages[req->num_pages] = page;
1003 req->page_descs[req->num_pages].length = tmp;
1006 iov_iter_advance(ii, tmp);
1010 if (offset == PAGE_CACHE_SIZE)
1013 if (!fc->big_writes)
1015 } while (iov_iter_count(ii) && count < fc->max_write &&
1016 req->num_pages < req->max_pages && offset == 0);
1018 return count > 0 ? count : err;
1021 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1023 return min_t(unsigned,
1024 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1025 (pos >> PAGE_CACHE_SHIFT) + 1,
1026 FUSE_MAX_PAGES_PER_REQ);
1029 static ssize_t fuse_perform_write(struct file *file,
1030 struct address_space *mapping,
1031 struct iov_iter *ii, loff_t pos)
1033 struct inode *inode = mapping->host;
1034 struct fuse_conn *fc = get_fuse_conn(inode);
1038 if (is_bad_inode(inode))
1042 struct fuse_req *req;
1044 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1046 req = fuse_get_req(fc, nr_pages);
1052 count = fuse_fill_write_pages(req, mapping, ii, pos);
1058 num_written = fuse_send_write_pages(req, file, inode,
1060 err = req->out.h.error;
1065 /* break out of the loop on short write */
1066 if (num_written != count)
1070 fuse_put_request(fc, req);
1071 } while (!err && iov_iter_count(ii));
1074 fuse_write_update_size(inode, pos);
1076 fuse_invalidate_attr(inode);
1078 return res > 0 ? res : err;
1081 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1082 unsigned long nr_segs, loff_t pos)
1084 struct file *file = iocb->ki_filp;
1085 struct address_space *mapping = file->f_mapping;
1088 ssize_t written = 0;
1089 ssize_t written_buffered = 0;
1090 struct inode *inode = mapping->host;
1095 WARN_ON(iocb->ki_pos != pos);
1098 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1103 mutex_lock(&inode->i_mutex);
1105 /* We can write back this queue in page reclaim */
1106 current->backing_dev_info = mapping->backing_dev_info;
1108 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1115 err = file_remove_suid(file);
1119 err = file_update_time(file);
1123 if (file->f_flags & O_DIRECT) {
1124 written = generic_file_direct_write(iocb, iov, &nr_segs,
1127 if (written < 0 || written == count)
1133 iov_iter_init(&i, iov, nr_segs, count, written);
1134 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1135 if (written_buffered < 0) {
1136 err = written_buffered;
1139 endbyte = pos + written_buffered - 1;
1141 err = filemap_write_and_wait_range(file->f_mapping, pos,
1146 invalidate_mapping_pages(file->f_mapping,
1147 pos >> PAGE_CACHE_SHIFT,
1148 endbyte >> PAGE_CACHE_SHIFT);
1150 written += written_buffered;
1151 iocb->ki_pos = pos + written_buffered;
1153 iov_iter_init(&i, iov, nr_segs, count, 0);
1154 written = fuse_perform_write(file, mapping, &i, pos);
1156 iocb->ki_pos = pos + written;
1159 current->backing_dev_info = NULL;
1160 mutex_unlock(&inode->i_mutex);
1162 return written ? written : err;
1165 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1166 unsigned index, unsigned nr_pages)
1170 for (i = index; i < index + nr_pages; i++)
1171 req->page_descs[i].length = PAGE_SIZE -
1172 req->page_descs[i].offset;
1175 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1177 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1180 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1183 return min(iov_iter_single_seg_count(ii), max_size);
1186 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1187 size_t *nbytesp, int write)
1189 size_t nbytes = 0; /* # bytes already packed in req */
1191 /* Special case for kernel I/O: can copy directly into the buffer */
1192 if (segment_eq(get_fs(), KERNEL_DS)) {
1193 unsigned long user_addr = fuse_get_user_addr(ii);
1194 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1197 req->in.args[1].value = (void *) user_addr;
1199 req->out.args[0].value = (void *) user_addr;
1201 iov_iter_advance(ii, frag_size);
1202 *nbytesp = frag_size;
1206 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1208 unsigned long user_addr = fuse_get_user_addr(ii);
1209 unsigned offset = user_addr & ~PAGE_MASK;
1210 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1213 unsigned n = req->max_pages - req->num_pages;
1214 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1216 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1217 npages = clamp(npages, 1U, n);
1219 ret = get_user_pages_fast(user_addr, npages, !write,
1220 &req->pages[req->num_pages]);
1225 frag_size = min_t(size_t, frag_size,
1226 (npages << PAGE_SHIFT) - offset);
1227 iov_iter_advance(ii, frag_size);
1229 req->page_descs[req->num_pages].offset = offset;
1230 fuse_page_descs_length_init(req, req->num_pages, npages);
1232 req->num_pages += npages;
1233 req->page_descs[req->num_pages - 1].length -=
1234 (npages << PAGE_SHIFT) - offset - frag_size;
1236 nbytes += frag_size;
1240 req->in.argpages = 1;
1242 req->out.argpages = 1;
1249 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1251 struct iov_iter ii = *ii_p;
1254 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1255 unsigned long user_addr = fuse_get_user_addr(&ii);
1256 unsigned offset = user_addr & ~PAGE_MASK;
1257 size_t frag_size = iov_iter_single_seg_count(&ii);
1259 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1260 iov_iter_advance(&ii, frag_size);
1263 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1266 ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
1267 unsigned long nr_segs, size_t count, loff_t *ppos,
1270 struct file *file = io->file;
1271 struct fuse_file *ff = file->private_data;
1272 struct fuse_conn *fc = ff->fc;
1273 size_t nmax = write ? fc->max_write : fc->max_read;
1276 struct fuse_req *req;
1279 iov_iter_init(&ii, iov, nr_segs, count, 0);
1281 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1283 return PTR_ERR(req);
1287 fl_owner_t owner = current->files;
1288 size_t nbytes = min(count, nmax);
1289 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
1296 nres = fuse_send_write(req, io, pos, nbytes, owner);
1298 nres = fuse_send_read(req, io, pos, nbytes, owner);
1301 fuse_release_user_pages(req, !write);
1302 if (req->out.h.error) {
1304 res = req->out.h.error;
1306 } else if (nres > nbytes) {
1316 fuse_put_request(fc, req);
1317 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1323 fuse_put_request(fc, req);
1329 EXPORT_SYMBOL_GPL(fuse_direct_io);
1331 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1332 const struct iovec *iov,
1333 unsigned long nr_segs, loff_t *ppos,
1337 struct file *file = io->file;
1338 struct inode *inode = file_inode(file);
1340 if (is_bad_inode(inode))
1343 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
1345 fuse_invalidate_attr(inode);
1350 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1351 size_t count, loff_t *ppos)
1353 struct fuse_io_priv io = { .async = 0, .file = file };
1354 struct iovec iov = { .iov_base = buf, .iov_len = count };
1355 return __fuse_direct_read(&io, &iov, 1, ppos, count);
1358 static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1359 const struct iovec *iov,
1360 unsigned long nr_segs, loff_t *ppos)
1362 struct file *file = io->file;
1363 struct inode *inode = file_inode(file);
1364 size_t count = iov_length(iov, nr_segs);
1367 res = generic_write_checks(file, ppos, &count, 0);
1369 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 1);
1371 fuse_invalidate_attr(inode);
1376 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1377 size_t count, loff_t *ppos)
1379 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
1380 struct inode *inode = file_inode(file);
1382 struct fuse_io_priv io = { .async = 0, .file = file };
1384 if (is_bad_inode(inode))
1387 /* Don't allow parallel writes to the same file */
1388 mutex_lock(&inode->i_mutex);
1389 res = __fuse_direct_write(&io, &iov, 1, ppos);
1391 fuse_write_update_size(inode, *ppos);
1392 mutex_unlock(&inode->i_mutex);
1397 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1399 __free_page(req->pages[0]);
1400 fuse_file_put(req->ff, false);
1403 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1405 struct inode *inode = req->inode;
1406 struct fuse_inode *fi = get_fuse_inode(inode);
1407 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1409 list_del(&req->writepages_entry);
1410 dec_bdi_stat(bdi, BDI_WRITEBACK);
1411 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1412 bdi_writeout_inc(bdi);
1413 wake_up(&fi->page_waitq);
1416 /* Called under fc->lock, may release and reacquire it */
1417 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1418 __releases(fc->lock)
1419 __acquires(fc->lock)
1421 struct fuse_inode *fi = get_fuse_inode(req->inode);
1422 loff_t size = i_size_read(req->inode);
1423 struct fuse_write_in *inarg = &req->misc.write.in;
1428 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1429 inarg->size = PAGE_CACHE_SIZE;
1430 } else if (inarg->offset < size) {
1431 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1433 /* Got truncated off completely */
1437 req->in.args[1].size = inarg->size;
1439 fuse_request_send_background_locked(fc, req);
1443 fuse_writepage_finish(fc, req);
1444 spin_unlock(&fc->lock);
1445 fuse_writepage_free(fc, req);
1446 fuse_put_request(fc, req);
1447 spin_lock(&fc->lock);
1451 * If fi->writectr is positive (no truncate or fsync going on) send
1452 * all queued writepage requests.
1454 * Called with fc->lock
1456 void fuse_flush_writepages(struct inode *inode)
1457 __releases(fc->lock)
1458 __acquires(fc->lock)
1460 struct fuse_conn *fc = get_fuse_conn(inode);
1461 struct fuse_inode *fi = get_fuse_inode(inode);
1462 struct fuse_req *req;
1464 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1465 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1466 list_del_init(&req->list);
1467 fuse_send_writepage(fc, req);
1471 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1473 struct inode *inode = req->inode;
1474 struct fuse_inode *fi = get_fuse_inode(inode);
1476 mapping_set_error(inode->i_mapping, req->out.h.error);
1477 spin_lock(&fc->lock);
1479 fuse_writepage_finish(fc, req);
1480 spin_unlock(&fc->lock);
1481 fuse_writepage_free(fc, req);
1484 static int fuse_writepage_locked(struct page *page)
1486 struct address_space *mapping = page->mapping;
1487 struct inode *inode = mapping->host;
1488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 struct fuse_inode *fi = get_fuse_inode(inode);
1490 struct fuse_req *req;
1491 struct fuse_file *ff;
1492 struct page *tmp_page;
1494 set_page_writeback(page);
1496 req = fuse_request_alloc_nofs(1);
1500 req->background = 1; /* writeback always goes to bg_queue */
1501 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1505 spin_lock(&fc->lock);
1506 BUG_ON(list_empty(&fi->write_files));
1507 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1508 req->ff = fuse_file_get(ff);
1509 spin_unlock(&fc->lock);
1511 fuse_write_fill(req, ff, page_offset(page), 0);
1513 copy_highpage(tmp_page, page);
1514 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1515 req->in.argpages = 1;
1517 req->pages[0] = tmp_page;
1518 req->page_descs[0].offset = 0;
1519 req->page_descs[0].length = PAGE_SIZE;
1520 req->end = fuse_writepage_end;
1523 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1524 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1525 end_page_writeback(page);
1527 spin_lock(&fc->lock);
1528 list_add(&req->writepages_entry, &fi->writepages);
1529 list_add_tail(&req->list, &fi->queued_writes);
1530 fuse_flush_writepages(inode);
1531 spin_unlock(&fc->lock);
1536 fuse_request_free(req);
1538 end_page_writeback(page);
1542 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1546 err = fuse_writepage_locked(page);
1552 static int fuse_launder_page(struct page *page)
1555 if (clear_page_dirty_for_io(page)) {
1556 struct inode *inode = page->mapping->host;
1557 err = fuse_writepage_locked(page);
1559 fuse_wait_on_page_writeback(inode, page->index);
1565 * Write back dirty pages now, because there may not be any suitable
1568 static void fuse_vma_close(struct vm_area_struct *vma)
1570 filemap_write_and_wait(vma->vm_file->f_mapping);
1574 * Wait for writeback against this page to complete before allowing it
1575 * to be marked dirty again, and hence written back again, possibly
1576 * before the previous writepage completed.
1578 * Block here, instead of in ->writepage(), so that the userspace fs
1579 * can only block processes actually operating on the filesystem.
1581 * Otherwise unprivileged userspace fs would be able to block
1586 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1588 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1590 struct page *page = vmf->page;
1592 * Don't use page->mapping as it may become NULL from a
1593 * concurrent truncate.
1595 struct inode *inode = vma->vm_file->f_mapping->host;
1597 fuse_wait_on_page_writeback(inode, page->index);
1601 static const struct vm_operations_struct fuse_file_vm_ops = {
1602 .close = fuse_vma_close,
1603 .fault = filemap_fault,
1604 .page_mkwrite = fuse_page_mkwrite,
1605 .remap_pages = generic_file_remap_pages,
1608 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1610 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1611 struct inode *inode = file_inode(file);
1612 struct fuse_conn *fc = get_fuse_conn(inode);
1613 struct fuse_inode *fi = get_fuse_inode(inode);
1614 struct fuse_file *ff = file->private_data;
1616 * file may be written through mmap, so chain it onto the
1617 * inodes's write_file list
1619 spin_lock(&fc->lock);
1620 if (list_empty(&ff->write_entry))
1621 list_add(&ff->write_entry, &fi->write_files);
1622 spin_unlock(&fc->lock);
1624 file_accessed(file);
1625 vma->vm_ops = &fuse_file_vm_ops;
1629 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1631 /* Can't provide the coherency needed for MAP_SHARED */
1632 if (vma->vm_flags & VM_MAYSHARE)
1635 invalidate_inode_pages2(file->f_mapping);
1637 return generic_file_mmap(file, vma);
1640 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1641 struct file_lock *fl)
1643 switch (ffl->type) {
1649 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1650 ffl->end < ffl->start)
1653 fl->fl_start = ffl->start;
1654 fl->fl_end = ffl->end;
1655 fl->fl_pid = ffl->pid;
1661 fl->fl_type = ffl->type;
1665 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1666 const struct file_lock *fl, int opcode, pid_t pid,
1669 struct inode *inode = file_inode(file);
1670 struct fuse_conn *fc = get_fuse_conn(inode);
1671 struct fuse_file *ff = file->private_data;
1672 struct fuse_lk_in *arg = &req->misc.lk_in;
1675 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1676 arg->lk.start = fl->fl_start;
1677 arg->lk.end = fl->fl_end;
1678 arg->lk.type = fl->fl_type;
1681 arg->lk_flags |= FUSE_LK_FLOCK;
1682 req->in.h.opcode = opcode;
1683 req->in.h.nodeid = get_node_id(inode);
1684 req->in.numargs = 1;
1685 req->in.args[0].size = sizeof(*arg);
1686 req->in.args[0].value = arg;
1689 static int fuse_getlk(struct file *file, struct file_lock *fl)
1691 struct inode *inode = file_inode(file);
1692 struct fuse_conn *fc = get_fuse_conn(inode);
1693 struct fuse_req *req;
1694 struct fuse_lk_out outarg;
1697 req = fuse_get_req_nopages(fc);
1699 return PTR_ERR(req);
1701 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1702 req->out.numargs = 1;
1703 req->out.args[0].size = sizeof(outarg);
1704 req->out.args[0].value = &outarg;
1705 fuse_request_send(fc, req);
1706 err = req->out.h.error;
1707 fuse_put_request(fc, req);
1709 err = convert_fuse_file_lock(&outarg.lk, fl);
1714 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1716 struct inode *inode = file_inode(file);
1717 struct fuse_conn *fc = get_fuse_conn(inode);
1718 struct fuse_req *req;
1719 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1720 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1723 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
1724 /* NLM needs asynchronous locks, which we don't support yet */
1728 /* Unlock on close is handled by the flush method */
1729 if (fl->fl_flags & FL_CLOSE)
1732 req = fuse_get_req_nopages(fc);
1734 return PTR_ERR(req);
1736 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1737 fuse_request_send(fc, req);
1738 err = req->out.h.error;
1739 /* locking is restartable */
1742 fuse_put_request(fc, req);
1746 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1748 struct inode *inode = file_inode(file);
1749 struct fuse_conn *fc = get_fuse_conn(inode);
1752 if (cmd == F_CANCELLK) {
1754 } else if (cmd == F_GETLK) {
1756 posix_test_lock(file, fl);
1759 err = fuse_getlk(file, fl);
1762 err = posix_lock_file(file, fl, NULL);
1764 err = fuse_setlk(file, fl, 0);
1769 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1771 struct inode *inode = file_inode(file);
1772 struct fuse_conn *fc = get_fuse_conn(inode);
1776 err = flock_lock_file_wait(file, fl);
1778 struct fuse_file *ff = file->private_data;
1780 /* emulate flock with POSIX locks */
1781 fl->fl_owner = (fl_owner_t) file;
1783 err = fuse_setlk(file, fl, 1);
1789 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1791 struct inode *inode = mapping->host;
1792 struct fuse_conn *fc = get_fuse_conn(inode);
1793 struct fuse_req *req;
1794 struct fuse_bmap_in inarg;
1795 struct fuse_bmap_out outarg;
1798 if (!inode->i_sb->s_bdev || fc->no_bmap)
1801 req = fuse_get_req_nopages(fc);
1805 memset(&inarg, 0, sizeof(inarg));
1806 inarg.block = block;
1807 inarg.blocksize = inode->i_sb->s_blocksize;
1808 req->in.h.opcode = FUSE_BMAP;
1809 req->in.h.nodeid = get_node_id(inode);
1810 req->in.numargs = 1;
1811 req->in.args[0].size = sizeof(inarg);
1812 req->in.args[0].value = &inarg;
1813 req->out.numargs = 1;
1814 req->out.args[0].size = sizeof(outarg);
1815 req->out.args[0].value = &outarg;
1816 fuse_request_send(fc, req);
1817 err = req->out.h.error;
1818 fuse_put_request(fc, req);
1822 return err ? 0 : outarg.block;
1825 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
1828 struct inode *inode = file_inode(file);
1830 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
1831 if (whence == SEEK_CUR || whence == SEEK_SET)
1832 return generic_file_llseek(file, offset, whence);
1834 mutex_lock(&inode->i_mutex);
1835 retval = fuse_update_attributes(inode, NULL, file, NULL);
1837 retval = generic_file_llseek(file, offset, whence);
1838 mutex_unlock(&inode->i_mutex);
1843 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1844 unsigned int nr_segs, size_t bytes, bool to_user)
1852 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1854 while (iov_iter_count(&ii)) {
1855 struct page *page = pages[page_idx++];
1856 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1862 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1863 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1864 size_t copy = min(todo, iov_len);
1868 left = copy_from_user(kaddr, uaddr, copy);
1870 left = copy_to_user(uaddr, kaddr, copy);
1875 iov_iter_advance(&ii, copy);
1887 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1888 * ABI was defined to be 'struct iovec' which is different on 32bit
1889 * and 64bit. Fortunately we can determine which structure the server
1890 * used from the size of the reply.
1892 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1893 size_t transferred, unsigned count,
1896 #ifdef CONFIG_COMPAT
1897 if (count * sizeof(struct compat_iovec) == transferred) {
1898 struct compat_iovec *ciov = src;
1902 * With this interface a 32bit server cannot support
1903 * non-compat (i.e. ones coming from 64bit apps) ioctl
1909 for (i = 0; i < count; i++) {
1910 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1911 dst[i].iov_len = ciov[i].iov_len;
1917 if (count * sizeof(struct iovec) != transferred)
1920 memcpy(dst, src, transferred);
1924 /* Make sure iov_length() won't overflow */
1925 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1928 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1930 for (n = 0; n < count; n++, iov++) {
1931 if (iov->iov_len > (size_t) max)
1933 max -= iov->iov_len;
1938 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1939 void *src, size_t transferred, unsigned count,
1943 struct fuse_ioctl_iovec *fiov = src;
1945 if (fc->minor < 16) {
1946 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1950 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1953 for (i = 0; i < count; i++) {
1954 /* Did the server supply an inappropriate value? */
1955 if (fiov[i].base != (unsigned long) fiov[i].base ||
1956 fiov[i].len != (unsigned long) fiov[i].len)
1959 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1960 dst[i].iov_len = (size_t) fiov[i].len;
1962 #ifdef CONFIG_COMPAT
1964 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1965 (compat_size_t) dst[i].iov_len != fiov[i].len))
1975 * For ioctls, there is no generic way to determine how much memory
1976 * needs to be read and/or written. Furthermore, ioctls are allowed
1977 * to dereference the passed pointer, so the parameter requires deep
1978 * copying but FUSE has no idea whatsoever about what to copy in or
1981 * This is solved by allowing FUSE server to retry ioctl with
1982 * necessary in/out iovecs. Let's assume the ioctl implementation
1983 * needs to read in the following structure.
1990 * On the first callout to FUSE server, inarg->in_size and
1991 * inarg->out_size will be NULL; then, the server completes the ioctl
1992 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1993 * the actual iov array to
1995 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1997 * which tells FUSE to copy in the requested area and retry the ioctl.
1998 * On the second round, the server has access to the structure and
1999 * from that it can tell what to look for next, so on the invocation,
2000 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2002 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2003 * { .iov_base = a.buf, .iov_len = a.buflen } }
2005 * FUSE will copy both struct a and the pointed buffer from the
2006 * process doing the ioctl and retry ioctl with both struct a and the
2009 * This time, FUSE server has everything it needs and completes ioctl
2010 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2012 * Copying data out works the same way.
2014 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2015 * automatically initializes in and out iovs by decoding @cmd with
2016 * _IOC_* macros and the server is not allowed to request RETRY. This
2017 * limits ioctl data transfers to well-formed ioctls and is the forced
2018 * behavior for all FUSE servers.
2020 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2023 struct fuse_file *ff = file->private_data;
2024 struct fuse_conn *fc = ff->fc;
2025 struct fuse_ioctl_in inarg = {
2031 struct fuse_ioctl_out outarg;
2032 struct fuse_req *req = NULL;
2033 struct page **pages = NULL;
2034 struct iovec *iov_page = NULL;
2035 struct iovec *in_iov = NULL, *out_iov = NULL;
2036 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2037 size_t in_size, out_size, transferred;
2040 #if BITS_PER_LONG == 32
2041 inarg.flags |= FUSE_IOCTL_32BIT;
2043 if (flags & FUSE_IOCTL_COMPAT)
2044 inarg.flags |= FUSE_IOCTL_32BIT;
2047 /* assume all the iovs returned by client always fits in a page */
2048 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2051 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2052 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2053 if (!pages || !iov_page)
2057 * If restricted, initialize IO parameters as encoded in @cmd.
2058 * RETRY from server is not allowed.
2060 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2061 struct iovec *iov = iov_page;
2063 iov->iov_base = (void __user *)arg;
2064 iov->iov_len = _IOC_SIZE(cmd);
2066 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2071 if (_IOC_DIR(cmd) & _IOC_READ) {
2078 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2079 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2082 * Out data can be used either for actual out data or iovs,
2083 * make sure there always is at least one page.
2085 out_size = max_t(size_t, out_size, PAGE_SIZE);
2086 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2088 /* make sure there are enough buffer pages and init request with them */
2090 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2092 while (num_pages < max_pages) {
2093 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2094 if (!pages[num_pages])
2099 req = fuse_get_req(fc, num_pages);
2105 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2106 req->num_pages = num_pages;
2107 fuse_page_descs_length_init(req, 0, req->num_pages);
2109 /* okay, let's send it to the client */
2110 req->in.h.opcode = FUSE_IOCTL;
2111 req->in.h.nodeid = ff->nodeid;
2112 req->in.numargs = 1;
2113 req->in.args[0].size = sizeof(inarg);
2114 req->in.args[0].value = &inarg;
2117 req->in.args[1].size = in_size;
2118 req->in.argpages = 1;
2120 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2126 req->out.numargs = 2;
2127 req->out.args[0].size = sizeof(outarg);
2128 req->out.args[0].value = &outarg;
2129 req->out.args[1].size = out_size;
2130 req->out.argpages = 1;
2131 req->out.argvar = 1;
2133 fuse_request_send(fc, req);
2134 err = req->out.h.error;
2135 transferred = req->out.args[1].size;
2136 fuse_put_request(fc, req);
2141 /* did it ask for retry? */
2142 if (outarg.flags & FUSE_IOCTL_RETRY) {
2145 /* no retry if in restricted mode */
2147 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2150 in_iovs = outarg.in_iovs;
2151 out_iovs = outarg.out_iovs;
2154 * Make sure things are in boundary, separate checks
2155 * are to protect against overflow.
2158 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2159 out_iovs > FUSE_IOCTL_MAX_IOV ||
2160 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2163 vaddr = kmap_atomic(pages[0]);
2164 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2165 transferred, in_iovs + out_iovs,
2166 (flags & FUSE_IOCTL_COMPAT) != 0);
2167 kunmap_atomic(vaddr);
2172 out_iov = in_iov + in_iovs;
2174 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2178 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2186 if (transferred > inarg.out_size)
2189 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2192 fuse_put_request(fc, req);
2193 free_page((unsigned long) iov_page);
2195 __free_page(pages[--num_pages]);
2198 return err ? err : outarg.result;
2200 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2202 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2203 unsigned long arg, unsigned int flags)
2205 struct inode *inode = file_inode(file);
2206 struct fuse_conn *fc = get_fuse_conn(inode);
2208 if (!fuse_allow_current_process(fc))
2211 if (is_bad_inode(inode))
2214 return fuse_do_ioctl(file, cmd, arg, flags);
2217 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2220 return fuse_ioctl_common(file, cmd, arg, 0);
2223 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2226 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2230 * All files which have been polled are linked to RB tree
2231 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2232 * find the matching one.
2234 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2235 struct rb_node **parent_out)
2237 struct rb_node **link = &fc->polled_files.rb_node;
2238 struct rb_node *last = NULL;
2241 struct fuse_file *ff;
2244 ff = rb_entry(last, struct fuse_file, polled_node);
2247 link = &last->rb_left;
2248 else if (kh > ff->kh)
2249 link = &last->rb_right;
2260 * The file is about to be polled. Make sure it's on the polled_files
2261 * RB tree. Note that files once added to the polled_files tree are
2262 * not removed before the file is released. This is because a file
2263 * polled once is likely to be polled again.
2265 static void fuse_register_polled_file(struct fuse_conn *fc,
2266 struct fuse_file *ff)
2268 spin_lock(&fc->lock);
2269 if (RB_EMPTY_NODE(&ff->polled_node)) {
2270 struct rb_node **link, *parent;
2272 link = fuse_find_polled_node(fc, ff->kh, &parent);
2274 rb_link_node(&ff->polled_node, parent, link);
2275 rb_insert_color(&ff->polled_node, &fc->polled_files);
2277 spin_unlock(&fc->lock);
2280 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2282 struct fuse_file *ff = file->private_data;
2283 struct fuse_conn *fc = ff->fc;
2284 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2285 struct fuse_poll_out outarg;
2286 struct fuse_req *req;
2290 return DEFAULT_POLLMASK;
2292 poll_wait(file, &ff->poll_wait, wait);
2293 inarg.events = (__u32)poll_requested_events(wait);
2296 * Ask for notification iff there's someone waiting for it.
2297 * The client may ignore the flag and always notify.
2299 if (waitqueue_active(&ff->poll_wait)) {
2300 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2301 fuse_register_polled_file(fc, ff);
2304 req = fuse_get_req_nopages(fc);
2308 req->in.h.opcode = FUSE_POLL;
2309 req->in.h.nodeid = ff->nodeid;
2310 req->in.numargs = 1;
2311 req->in.args[0].size = sizeof(inarg);
2312 req->in.args[0].value = &inarg;
2313 req->out.numargs = 1;
2314 req->out.args[0].size = sizeof(outarg);
2315 req->out.args[0].value = &outarg;
2316 fuse_request_send(fc, req);
2317 err = req->out.h.error;
2318 fuse_put_request(fc, req);
2321 return outarg.revents;
2322 if (err == -ENOSYS) {
2324 return DEFAULT_POLLMASK;
2328 EXPORT_SYMBOL_GPL(fuse_file_poll);
2331 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2332 * wakes up the poll waiters.
2334 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2335 struct fuse_notify_poll_wakeup_out *outarg)
2337 u64 kh = outarg->kh;
2338 struct rb_node **link;
2340 spin_lock(&fc->lock);
2342 link = fuse_find_polled_node(fc, kh, NULL);
2344 struct fuse_file *ff;
2346 ff = rb_entry(*link, struct fuse_file, polled_node);
2347 wake_up_interruptible_sync(&ff->poll_wait);
2350 spin_unlock(&fc->lock);
2354 static void fuse_do_truncate(struct file *file)
2356 struct inode *inode = file->f_mapping->host;
2359 attr.ia_valid = ATTR_SIZE;
2360 attr.ia_size = i_size_read(inode);
2362 attr.ia_file = file;
2363 attr.ia_valid |= ATTR_FILE;
2365 fuse_do_setattr(inode, &attr, file);
2369 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2370 loff_t offset, unsigned long nr_segs)
2373 struct file *file = iocb->ki_filp;
2374 struct fuse_file *ff = file->private_data;
2376 struct inode *inode;
2378 size_t count = iov_length(iov, nr_segs);
2379 struct fuse_io_priv *io;
2382 inode = file->f_mapping->host;
2383 i_size = i_size_read(inode);
2385 /* optimization for short read */
2386 if (rw != WRITE && offset + count > i_size) {
2387 if (offset >= i_size)
2389 count = i_size - offset;
2392 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2395 spin_lock_init(&io->lock);
2399 io->offset = offset;
2400 io->write = (rw == WRITE);
2404 * By default, we want to optimize all I/Os with async request
2405 * submission to the client filesystem if supported.
2407 io->async = ff->fc->async_dio;
2411 * We cannot asynchronously extend the size of a file. We have no method
2412 * to wait on real async I/O requests, so we must submit this request
2415 if (!is_sync_kiocb(iocb) && (offset + count > i_size))
2419 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
2421 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
2424 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2426 /* we have a non-extending, async request, so return */
2427 if (ret > 0 && !is_sync_kiocb(iocb))
2428 return -EIOCBQUEUED;
2430 ret = wait_on_sync_kiocb(iocb);
2437 fuse_write_update_size(inode, pos);
2438 else if (ret < 0 && offset + count > i_size)
2439 fuse_do_truncate(file);
2445 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2448 struct fuse_file *ff = file->private_data;
2449 struct fuse_conn *fc = ff->fc;
2450 struct fuse_req *req;
2451 struct fuse_fallocate_in inarg = {
2459 if (fc->no_fallocate)
2462 req = fuse_get_req_nopages(fc);
2464 return PTR_ERR(req);
2466 req->in.h.opcode = FUSE_FALLOCATE;
2467 req->in.h.nodeid = ff->nodeid;
2468 req->in.numargs = 1;
2469 req->in.args[0].size = sizeof(inarg);
2470 req->in.args[0].value = &inarg;
2471 fuse_request_send(fc, req);
2472 err = req->out.h.error;
2473 if (err == -ENOSYS) {
2474 fc->no_fallocate = 1;
2477 fuse_put_request(fc, req);
2482 static const struct file_operations fuse_file_operations = {
2483 .llseek = fuse_file_llseek,
2484 .read = do_sync_read,
2485 .aio_read = fuse_file_aio_read,
2486 .write = do_sync_write,
2487 .aio_write = fuse_file_aio_write,
2488 .mmap = fuse_file_mmap,
2490 .flush = fuse_flush,
2491 .release = fuse_release,
2492 .fsync = fuse_fsync,
2493 .lock = fuse_file_lock,
2494 .flock = fuse_file_flock,
2495 .splice_read = generic_file_splice_read,
2496 .unlocked_ioctl = fuse_file_ioctl,
2497 .compat_ioctl = fuse_file_compat_ioctl,
2498 .poll = fuse_file_poll,
2499 .fallocate = fuse_file_fallocate,
2502 static const struct file_operations fuse_direct_io_file_operations = {
2503 .llseek = fuse_file_llseek,
2504 .read = fuse_direct_read,
2505 .write = fuse_direct_write,
2506 .mmap = fuse_direct_mmap,
2508 .flush = fuse_flush,
2509 .release = fuse_release,
2510 .fsync = fuse_fsync,
2511 .lock = fuse_file_lock,
2512 .flock = fuse_file_flock,
2513 .unlocked_ioctl = fuse_file_ioctl,
2514 .compat_ioctl = fuse_file_compat_ioctl,
2515 .poll = fuse_file_poll,
2516 .fallocate = fuse_file_fallocate,
2517 /* no splice_read */
2520 static const struct address_space_operations fuse_file_aops = {
2521 .readpage = fuse_readpage,
2522 .writepage = fuse_writepage,
2523 .launder_page = fuse_launder_page,
2524 .readpages = fuse_readpages,
2525 .set_page_dirty = __set_page_dirty_nobuffers,
2527 .direct_IO = fuse_direct_IO,
2530 void fuse_init_file_inode(struct inode *inode)
2532 inode->i_fop = &fuse_file_operations;
2533 inode->i_data.a_ops = &fuse_file_aops;