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;
130 fuse_request_send(ff->fc, req);
131 path_put(&req->misc.release.path);
132 fuse_put_request(ff->fc, req);
134 req->end = fuse_release_end;
135 fuse_request_send_background(ff->fc, req);
141 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
144 struct fuse_open_out outarg;
145 struct fuse_file *ff;
147 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
149 ff = fuse_file_alloc(fc);
153 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
160 outarg.open_flags &= ~FOPEN_DIRECT_IO;
164 ff->open_flags = outarg.open_flags;
165 file->private_data = fuse_file_get(ff);
169 EXPORT_SYMBOL_GPL(fuse_do_open);
171 void fuse_finish_open(struct inode *inode, struct file *file)
173 struct fuse_file *ff = file->private_data;
174 struct fuse_conn *fc = get_fuse_conn(inode);
176 if (ff->open_flags & FOPEN_DIRECT_IO)
177 file->f_op = &fuse_direct_io_file_operations;
178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
179 invalidate_inode_pages2(inode->i_mapping);
180 if (ff->open_flags & FOPEN_NONSEEKABLE)
181 nonseekable_open(inode, file);
182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
193 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
195 struct fuse_conn *fc = get_fuse_conn(inode);
198 err = generic_file_open(inode, file);
202 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
206 fuse_finish_open(inode, file);
211 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
213 struct fuse_conn *fc = ff->fc;
214 struct fuse_req *req = ff->reserved_req;
215 struct fuse_release_in *inarg = &req->misc.release.in;
217 spin_lock(&fc->lock);
218 list_del(&ff->write_entry);
219 if (!RB_EMPTY_NODE(&ff->polled_node))
220 rb_erase(&ff->polled_node, &fc->polled_files);
221 spin_unlock(&fc->lock);
223 wake_up_interruptible_all(&ff->poll_wait);
226 inarg->flags = flags;
227 req->in.h.opcode = opcode;
228 req->in.h.nodeid = ff->nodeid;
230 req->in.args[0].size = sizeof(struct fuse_release_in);
231 req->in.args[0].value = inarg;
234 void fuse_release_common(struct file *file, int opcode)
236 struct fuse_file *ff;
237 struct fuse_req *req;
239 ff = file->private_data;
243 req = ff->reserved_req;
244 fuse_prepare_release(ff, file->f_flags, opcode);
247 struct fuse_release_in *inarg = &req->misc.release.in;
248 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
249 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
252 /* Hold vfsmount and dentry until release is finished */
253 path_get(&file->f_path);
254 req->misc.release.path = file->f_path;
257 * Normally this will send the RELEASE request, however if
258 * some asynchronous READ or WRITE requests are outstanding,
259 * the sending will be delayed.
261 * Make the release synchronous if this is a fuseblk mount,
262 * synchronous RELEASE is allowed (and desirable) in this case
263 * because the server can be trusted not to screw up.
265 fuse_file_put(ff, ff->fc->destroy_req != NULL);
268 static int fuse_open(struct inode *inode, struct file *file)
270 return fuse_open_common(inode, file, false);
273 static int fuse_release(struct inode *inode, struct file *file)
275 fuse_release_common(file, FUSE_RELEASE);
277 /* return value is ignored by VFS */
281 void fuse_sync_release(struct fuse_file *ff, int flags)
283 WARN_ON(atomic_read(&ff->count) > 1);
284 fuse_prepare_release(ff, flags, FUSE_RELEASE);
285 ff->reserved_req->force = 1;
286 fuse_request_send(ff->fc, ff->reserved_req);
287 fuse_put_request(ff->fc, ff->reserved_req);
290 EXPORT_SYMBOL_GPL(fuse_sync_release);
293 * Scramble the ID space with XTEA, so that the value of the files_struct
294 * pointer is not exposed to userspace.
296 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
298 u32 *k = fc->scramble_key;
299 u64 v = (unsigned long) id;
305 for (i = 0; i < 32; i++) {
306 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
308 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
311 return (u64) v0 + ((u64) v1 << 32);
315 * Check if page is under writeback
317 * This is currently done by walking the list of writepage requests
318 * for the inode, which can be pretty inefficient.
320 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
322 struct fuse_conn *fc = get_fuse_conn(inode);
323 struct fuse_inode *fi = get_fuse_inode(inode);
324 struct fuse_req *req;
327 spin_lock(&fc->lock);
328 list_for_each_entry(req, &fi->writepages, writepages_entry) {
331 BUG_ON(req->inode != inode);
332 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
333 if (curr_index == index) {
338 spin_unlock(&fc->lock);
344 * Wait for page writeback to be completed.
346 * Since fuse doesn't rely on the VM writeback tracking, this has to
347 * use some other means.
349 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
351 struct fuse_inode *fi = get_fuse_inode(inode);
353 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
357 static int fuse_flush(struct file *file, fl_owner_t id)
359 struct inode *inode = file_inode(file);
360 struct fuse_conn *fc = get_fuse_conn(inode);
361 struct fuse_file *ff = file->private_data;
362 struct fuse_req *req;
363 struct fuse_flush_in inarg;
366 if (is_bad_inode(inode))
372 req = fuse_get_req_nofail_nopages(fc, file);
373 memset(&inarg, 0, sizeof(inarg));
375 inarg.lock_owner = fuse_lock_owner_id(fc, id);
376 req->in.h.opcode = FUSE_FLUSH;
377 req->in.h.nodeid = get_node_id(inode);
379 req->in.args[0].size = sizeof(inarg);
380 req->in.args[0].value = &inarg;
382 fuse_request_send(fc, req);
383 err = req->out.h.error;
384 fuse_put_request(fc, req);
385 if (err == -ENOSYS) {
393 * Wait for all pending writepages on the inode to finish.
395 * This is currently done by blocking further writes with FUSE_NOWRITE
396 * and waiting for all sent writes to complete.
398 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
399 * could conflict with truncation.
401 static void fuse_sync_writes(struct inode *inode)
403 fuse_set_nowrite(inode);
404 fuse_release_nowrite(inode);
407 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
408 int datasync, int isdir)
410 struct inode *inode = file->f_mapping->host;
411 struct fuse_conn *fc = get_fuse_conn(inode);
412 struct fuse_file *ff = file->private_data;
413 struct fuse_req *req;
414 struct fuse_fsync_in inarg;
417 if (is_bad_inode(inode))
420 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
424 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
427 mutex_lock(&inode->i_mutex);
430 * Start writeback against all dirty pages of the inode, then
431 * wait for all outstanding writes, before sending the FSYNC
434 err = write_inode_now(inode, 0);
438 fuse_sync_writes(inode);
440 req = fuse_get_req_nopages(fc);
446 memset(&inarg, 0, sizeof(inarg));
448 inarg.fsync_flags = datasync ? 1 : 0;
449 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
450 req->in.h.nodeid = get_node_id(inode);
452 req->in.args[0].size = sizeof(inarg);
453 req->in.args[0].value = &inarg;
454 fuse_request_send(fc, req);
455 err = req->out.h.error;
456 fuse_put_request(fc, req);
457 if (err == -ENOSYS) {
465 mutex_unlock(&inode->i_mutex);
469 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
472 return fuse_fsync_common(file, start, end, datasync, 0);
475 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
476 size_t count, int opcode)
478 struct fuse_read_in *inarg = &req->misc.read.in;
479 struct fuse_file *ff = file->private_data;
484 inarg->flags = file->f_flags;
485 req->in.h.opcode = opcode;
486 req->in.h.nodeid = ff->nodeid;
488 req->in.args[0].size = sizeof(struct fuse_read_in);
489 req->in.args[0].value = inarg;
491 req->out.numargs = 1;
492 req->out.args[0].size = count;
495 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
496 loff_t pos, size_t count, fl_owner_t owner)
498 struct fuse_file *ff = file->private_data;
499 struct fuse_conn *fc = ff->fc;
501 fuse_read_fill(req, file, pos, count, FUSE_READ);
503 struct fuse_read_in *inarg = &req->misc.read.in;
505 inarg->read_flags |= FUSE_READ_LOCKOWNER;
506 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
508 fuse_request_send(fc, req);
509 return req->out.args[0].size;
512 static void fuse_read_update_size(struct inode *inode, loff_t size,
515 struct fuse_conn *fc = get_fuse_conn(inode);
516 struct fuse_inode *fi = get_fuse_inode(inode);
518 spin_lock(&fc->lock);
519 if (attr_ver == fi->attr_version && size < inode->i_size) {
520 fi->attr_version = ++fc->attr_version;
521 i_size_write(inode, size);
523 spin_unlock(&fc->lock);
526 static int fuse_readpage(struct file *file, struct page *page)
528 struct inode *inode = page->mapping->host;
529 struct fuse_conn *fc = get_fuse_conn(inode);
530 struct fuse_req *req;
532 loff_t pos = page_offset(page);
533 size_t count = PAGE_CACHE_SIZE;
538 if (is_bad_inode(inode))
542 * Page writeback can extend beyond the lifetime of the
543 * page-cache page, so make sure we read a properly synced
546 fuse_wait_on_page_writeback(inode, page->index);
548 req = fuse_get_req(fc, 1);
553 attr_ver = fuse_get_attr_version(fc);
555 req->out.page_zeroing = 1;
556 req->out.argpages = 1;
558 req->pages[0] = page;
559 req->page_descs[0].length = count;
560 num_read = fuse_send_read(req, file, pos, count, NULL);
561 err = req->out.h.error;
562 fuse_put_request(fc, req);
566 * Short read means EOF. If file size is larger, truncate it
568 if (num_read < count)
569 fuse_read_update_size(inode, pos + num_read, attr_ver);
571 SetPageUptodate(page);
574 fuse_invalidate_attr(inode); /* atime changed */
580 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
583 size_t count = req->misc.read.in.size;
584 size_t num_read = req->out.args[0].size;
585 struct address_space *mapping = NULL;
587 for (i = 0; mapping == NULL && i < req->num_pages; i++)
588 mapping = req->pages[i]->mapping;
591 struct inode *inode = mapping->host;
594 * Short read means EOF. If file size is larger, truncate it
596 if (!req->out.h.error && num_read < count) {
599 pos = page_offset(req->pages[0]) + num_read;
600 fuse_read_update_size(inode, pos,
601 req->misc.read.attr_ver);
603 fuse_invalidate_attr(inode); /* atime changed */
606 for (i = 0; i < req->num_pages; i++) {
607 struct page *page = req->pages[i];
608 if (!req->out.h.error)
609 SetPageUptodate(page);
613 page_cache_release(page);
616 fuse_file_put(req->ff, false);
619 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
621 struct fuse_file *ff = file->private_data;
622 struct fuse_conn *fc = ff->fc;
623 loff_t pos = page_offset(req->pages[0]);
624 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
626 req->out.argpages = 1;
627 req->out.page_zeroing = 1;
628 req->out.page_replace = 1;
629 fuse_read_fill(req, file, pos, count, FUSE_READ);
630 req->misc.read.attr_ver = fuse_get_attr_version(fc);
631 if (fc->async_read) {
632 req->ff = fuse_file_get(ff);
633 req->end = fuse_readpages_end;
634 fuse_request_send_background(fc, req);
636 fuse_request_send(fc, req);
637 fuse_readpages_end(fc, req);
638 fuse_put_request(fc, req);
642 struct fuse_fill_data {
643 struct fuse_req *req;
649 static int fuse_readpages_fill(void *_data, struct page *page)
651 struct fuse_fill_data *data = _data;
652 struct fuse_req *req = data->req;
653 struct inode *inode = data->inode;
654 struct fuse_conn *fc = get_fuse_conn(inode);
656 fuse_wait_on_page_writeback(inode, page->index);
658 if (req->num_pages &&
659 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
660 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
661 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
662 int nr_alloc = min_t(unsigned, data->nr_pages,
663 FUSE_MAX_PAGES_PER_REQ);
664 fuse_send_readpages(req, data->file);
665 data->req = req = fuse_get_req(fc, nr_alloc);
672 if (WARN_ON(req->num_pages >= req->max_pages)) {
673 fuse_put_request(fc, req);
677 page_cache_get(page);
678 req->pages[req->num_pages] = page;
679 req->page_descs[req->num_pages].length = PAGE_SIZE;
685 static int fuse_readpages(struct file *file, struct address_space *mapping,
686 struct list_head *pages, unsigned nr_pages)
688 struct inode *inode = mapping->host;
689 struct fuse_conn *fc = get_fuse_conn(inode);
690 struct fuse_fill_data data;
692 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
695 if (is_bad_inode(inode))
700 data.req = fuse_get_req(fc, nr_alloc);
701 data.nr_pages = nr_pages;
702 err = PTR_ERR(data.req);
703 if (IS_ERR(data.req))
706 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
708 if (data.req->num_pages)
709 fuse_send_readpages(data.req, file);
711 fuse_put_request(fc, data.req);
717 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
718 unsigned long nr_segs, loff_t pos)
720 struct inode *inode = iocb->ki_filp->f_mapping->host;
721 struct fuse_conn *fc = get_fuse_conn(inode);
724 * In auto invalidate mode, always update attributes on read.
725 * Otherwise, only update if we attempt to read past EOF (to ensure
726 * i_size is up to date).
728 if (fc->auto_inval_data ||
729 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
731 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
736 return generic_file_aio_read(iocb, iov, nr_segs, pos);
739 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
740 loff_t pos, size_t count)
742 struct fuse_write_in *inarg = &req->misc.write.in;
743 struct fuse_write_out *outarg = &req->misc.write.out;
748 req->in.h.opcode = FUSE_WRITE;
749 req->in.h.nodeid = ff->nodeid;
751 if (ff->fc->minor < 9)
752 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
754 req->in.args[0].size = sizeof(struct fuse_write_in);
755 req->in.args[0].value = inarg;
756 req->in.args[1].size = count;
757 req->out.numargs = 1;
758 req->out.args[0].size = sizeof(struct fuse_write_out);
759 req->out.args[0].value = outarg;
762 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
763 loff_t pos, size_t count, fl_owner_t owner)
765 struct fuse_file *ff = file->private_data;
766 struct fuse_conn *fc = ff->fc;
767 struct fuse_write_in *inarg = &req->misc.write.in;
769 fuse_write_fill(req, ff, pos, count);
770 inarg->flags = file->f_flags;
772 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
773 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
775 fuse_request_send(fc, req);
776 return req->misc.write.out.size;
779 void fuse_write_update_size(struct inode *inode, loff_t pos)
781 struct fuse_conn *fc = get_fuse_conn(inode);
782 struct fuse_inode *fi = get_fuse_inode(inode);
784 spin_lock(&fc->lock);
785 fi->attr_version = ++fc->attr_version;
786 if (pos > inode->i_size)
787 i_size_write(inode, pos);
788 spin_unlock(&fc->lock);
791 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
792 struct inode *inode, loff_t pos,
799 for (i = 0; i < req->num_pages; i++)
800 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
802 res = fuse_send_write(req, file, pos, count, NULL);
804 offset = req->page_descs[0].offset;
806 for (i = 0; i < req->num_pages; i++) {
807 struct page *page = req->pages[i];
809 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
810 SetPageUptodate(page);
812 if (count > PAGE_CACHE_SIZE - offset)
813 count -= PAGE_CACHE_SIZE - offset;
819 page_cache_release(page);
825 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
826 struct address_space *mapping,
827 struct iov_iter *ii, loff_t pos)
829 struct fuse_conn *fc = get_fuse_conn(mapping->host);
830 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
834 req->in.argpages = 1;
835 req->page_descs[0].offset = offset;
840 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
841 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
844 bytes = min_t(size_t, bytes, fc->max_write - count);
848 if (iov_iter_fault_in_readable(ii, bytes))
852 page = grab_cache_page_write_begin(mapping, index, 0);
856 if (mapping_writably_mapped(mapping))
857 flush_dcache_page(page);
860 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
862 flush_dcache_page(page);
864 mark_page_accessed(page);
868 page_cache_release(page);
869 bytes = min(bytes, iov_iter_single_seg_count(ii));
874 req->pages[req->num_pages] = page;
875 req->page_descs[req->num_pages].length = tmp;
878 iov_iter_advance(ii, tmp);
882 if (offset == PAGE_CACHE_SIZE)
887 } while (iov_iter_count(ii) && count < fc->max_write &&
888 req->num_pages < req->max_pages && offset == 0);
890 return count > 0 ? count : err;
893 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
895 return min_t(unsigned,
896 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
897 (pos >> PAGE_CACHE_SHIFT) + 1,
898 FUSE_MAX_PAGES_PER_REQ);
901 static ssize_t fuse_perform_write(struct file *file,
902 struct address_space *mapping,
903 struct iov_iter *ii, loff_t pos)
905 struct inode *inode = mapping->host;
906 struct fuse_conn *fc = get_fuse_conn(inode);
910 if (is_bad_inode(inode))
914 struct fuse_req *req;
916 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
918 req = fuse_get_req(fc, nr_pages);
924 count = fuse_fill_write_pages(req, mapping, ii, pos);
930 num_written = fuse_send_write_pages(req, file, inode,
932 err = req->out.h.error;
937 /* break out of the loop on short write */
938 if (num_written != count)
942 fuse_put_request(fc, req);
943 } while (!err && iov_iter_count(ii));
946 fuse_write_update_size(inode, pos);
948 fuse_invalidate_attr(inode);
950 return res > 0 ? res : err;
953 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
954 unsigned long nr_segs, loff_t pos)
956 struct file *file = iocb->ki_filp;
957 struct address_space *mapping = file->f_mapping;
961 ssize_t written_buffered = 0;
962 struct inode *inode = mapping->host;
967 WARN_ON(iocb->ki_pos != pos);
970 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
975 mutex_lock(&inode->i_mutex);
977 /* We can write back this queue in page reclaim */
978 current->backing_dev_info = mapping->backing_dev_info;
980 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
987 err = file_remove_suid(file);
991 err = file_update_time(file);
995 if (file->f_flags & O_DIRECT) {
996 written = generic_file_direct_write(iocb, iov, &nr_segs,
999 if (written < 0 || written == count)
1005 iov_iter_init(&i, iov, nr_segs, count, written);
1006 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1007 if (written_buffered < 0) {
1008 err = written_buffered;
1011 endbyte = pos + written_buffered - 1;
1013 err = filemap_write_and_wait_range(file->f_mapping, pos,
1018 invalidate_mapping_pages(file->f_mapping,
1019 pos >> PAGE_CACHE_SHIFT,
1020 endbyte >> PAGE_CACHE_SHIFT);
1022 written += written_buffered;
1023 iocb->ki_pos = pos + written_buffered;
1025 iov_iter_init(&i, iov, nr_segs, count, 0);
1026 written = fuse_perform_write(file, mapping, &i, pos);
1028 iocb->ki_pos = pos + written;
1031 current->backing_dev_info = NULL;
1032 mutex_unlock(&inode->i_mutex);
1034 return written ? written : err;
1037 static void fuse_release_user_pages(struct fuse_req *req, int write)
1041 for (i = 0; i < req->num_pages; i++) {
1042 struct page *page = req->pages[i];
1044 set_page_dirty_lock(page);
1049 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1050 unsigned index, unsigned nr_pages)
1054 for (i = index; i < index + nr_pages; i++)
1055 req->page_descs[i].length = PAGE_SIZE -
1056 req->page_descs[i].offset;
1059 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1061 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1064 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1067 return min(iov_iter_single_seg_count(ii), max_size);
1070 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1071 size_t *nbytesp, int write)
1073 size_t nbytes = 0; /* # bytes already packed in req */
1075 /* Special case for kernel I/O: can copy directly into the buffer */
1076 if (segment_eq(get_fs(), KERNEL_DS)) {
1077 unsigned long user_addr = fuse_get_user_addr(ii);
1078 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1081 req->in.args[1].value = (void *) user_addr;
1083 req->out.args[0].value = (void *) user_addr;
1085 iov_iter_advance(ii, frag_size);
1086 *nbytesp = frag_size;
1090 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1092 unsigned long user_addr = fuse_get_user_addr(ii);
1093 unsigned offset = user_addr & ~PAGE_MASK;
1094 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1097 unsigned n = req->max_pages - req->num_pages;
1098 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1100 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1101 npages = clamp(npages, 1U, n);
1103 ret = get_user_pages_fast(user_addr, npages, !write,
1104 &req->pages[req->num_pages]);
1109 frag_size = min_t(size_t, frag_size,
1110 (npages << PAGE_SHIFT) - offset);
1111 iov_iter_advance(ii, frag_size);
1113 req->page_descs[req->num_pages].offset = offset;
1114 fuse_page_descs_length_init(req, req->num_pages, npages);
1116 req->num_pages += npages;
1117 req->page_descs[req->num_pages - 1].length -=
1118 (npages << PAGE_SHIFT) - offset - frag_size;
1120 nbytes += frag_size;
1124 req->in.argpages = 1;
1126 req->out.argpages = 1;
1133 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1135 struct iov_iter ii = *ii_p;
1138 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1139 unsigned long user_addr = fuse_get_user_addr(&ii);
1140 unsigned offset = user_addr & ~PAGE_MASK;
1141 size_t frag_size = iov_iter_single_seg_count(&ii);
1143 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1144 iov_iter_advance(&ii, frag_size);
1147 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1150 ssize_t fuse_direct_io(struct file *file, const struct iovec *iov,
1151 unsigned long nr_segs, size_t count, loff_t *ppos,
1154 struct fuse_file *ff = file->private_data;
1155 struct fuse_conn *fc = ff->fc;
1156 size_t nmax = write ? fc->max_write : fc->max_read;
1159 struct fuse_req *req;
1162 iov_iter_init(&ii, iov, nr_segs, count, 0);
1164 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1166 return PTR_ERR(req);
1170 fl_owner_t owner = current->files;
1171 size_t nbytes = min(count, nmax);
1172 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
1179 nres = fuse_send_write(req, file, pos, nbytes, owner);
1181 nres = fuse_send_read(req, file, pos, nbytes, owner);
1183 fuse_release_user_pages(req, !write);
1184 if (req->out.h.error) {
1186 res = req->out.h.error;
1188 } else if (nres > nbytes) {
1198 fuse_put_request(fc, req);
1199 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1205 fuse_put_request(fc, req);
1211 EXPORT_SYMBOL_GPL(fuse_direct_io);
1213 static ssize_t __fuse_direct_read(struct file *file, const struct iovec *iov,
1214 unsigned long nr_segs, loff_t *ppos)
1217 struct inode *inode = file_inode(file);
1219 if (is_bad_inode(inode))
1222 res = fuse_direct_io(file, iov, nr_segs, iov_length(iov, nr_segs),
1225 fuse_invalidate_attr(inode);
1230 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1231 size_t count, loff_t *ppos)
1233 struct iovec iov = { .iov_base = buf, .iov_len = count };
1234 return __fuse_direct_read(file, &iov, 1, ppos);
1237 static ssize_t __fuse_direct_write(struct file *file, const struct iovec *iov,
1238 unsigned long nr_segs, loff_t *ppos)
1240 struct inode *inode = file_inode(file);
1241 size_t count = iov_length(iov, nr_segs);
1244 res = generic_write_checks(file, ppos, &count, 0);
1246 res = fuse_direct_io(file, iov, nr_segs, count, ppos, 1);
1248 fuse_write_update_size(inode, *ppos);
1251 fuse_invalidate_attr(inode);
1256 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1257 size_t count, loff_t *ppos)
1259 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
1260 struct inode *inode = file_inode(file);
1263 if (is_bad_inode(inode))
1266 /* Don't allow parallel writes to the same file */
1267 mutex_lock(&inode->i_mutex);
1268 res = __fuse_direct_write(file, &iov, 1, ppos);
1269 mutex_unlock(&inode->i_mutex);
1274 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1276 __free_page(req->pages[0]);
1277 fuse_file_put(req->ff, false);
1280 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1282 struct inode *inode = req->inode;
1283 struct fuse_inode *fi = get_fuse_inode(inode);
1284 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1286 list_del(&req->writepages_entry);
1287 dec_bdi_stat(bdi, BDI_WRITEBACK);
1288 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1289 bdi_writeout_inc(bdi);
1290 wake_up(&fi->page_waitq);
1293 /* Called under fc->lock, may release and reacquire it */
1294 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1295 __releases(fc->lock)
1296 __acquires(fc->lock)
1298 struct fuse_inode *fi = get_fuse_inode(req->inode);
1299 loff_t size = i_size_read(req->inode);
1300 struct fuse_write_in *inarg = &req->misc.write.in;
1305 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1306 inarg->size = PAGE_CACHE_SIZE;
1307 } else if (inarg->offset < size) {
1308 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1310 /* Got truncated off completely */
1314 req->in.args[1].size = inarg->size;
1316 fuse_request_send_background_locked(fc, req);
1320 fuse_writepage_finish(fc, req);
1321 spin_unlock(&fc->lock);
1322 fuse_writepage_free(fc, req);
1323 fuse_put_request(fc, req);
1324 spin_lock(&fc->lock);
1328 * If fi->writectr is positive (no truncate or fsync going on) send
1329 * all queued writepage requests.
1331 * Called with fc->lock
1333 void fuse_flush_writepages(struct inode *inode)
1334 __releases(fc->lock)
1335 __acquires(fc->lock)
1337 struct fuse_conn *fc = get_fuse_conn(inode);
1338 struct fuse_inode *fi = get_fuse_inode(inode);
1339 struct fuse_req *req;
1341 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1342 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1343 list_del_init(&req->list);
1344 fuse_send_writepage(fc, req);
1348 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1350 struct inode *inode = req->inode;
1351 struct fuse_inode *fi = get_fuse_inode(inode);
1353 mapping_set_error(inode->i_mapping, req->out.h.error);
1354 spin_lock(&fc->lock);
1356 fuse_writepage_finish(fc, req);
1357 spin_unlock(&fc->lock);
1358 fuse_writepage_free(fc, req);
1361 static int fuse_writepage_locked(struct page *page)
1363 struct address_space *mapping = page->mapping;
1364 struct inode *inode = mapping->host;
1365 struct fuse_conn *fc = get_fuse_conn(inode);
1366 struct fuse_inode *fi = get_fuse_inode(inode);
1367 struct fuse_req *req;
1368 struct fuse_file *ff;
1369 struct page *tmp_page;
1371 set_page_writeback(page);
1373 req = fuse_request_alloc_nofs(1);
1377 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1381 spin_lock(&fc->lock);
1382 BUG_ON(list_empty(&fi->write_files));
1383 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1384 req->ff = fuse_file_get(ff);
1385 spin_unlock(&fc->lock);
1387 fuse_write_fill(req, ff, page_offset(page), 0);
1389 copy_highpage(tmp_page, page);
1390 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1391 req->in.argpages = 1;
1393 req->pages[0] = tmp_page;
1394 req->page_descs[0].offset = 0;
1395 req->page_descs[0].length = PAGE_SIZE;
1396 req->end = fuse_writepage_end;
1399 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1400 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1401 end_page_writeback(page);
1403 spin_lock(&fc->lock);
1404 list_add(&req->writepages_entry, &fi->writepages);
1405 list_add_tail(&req->list, &fi->queued_writes);
1406 fuse_flush_writepages(inode);
1407 spin_unlock(&fc->lock);
1412 fuse_request_free(req);
1414 end_page_writeback(page);
1418 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1422 err = fuse_writepage_locked(page);
1428 static int fuse_launder_page(struct page *page)
1431 if (clear_page_dirty_for_io(page)) {
1432 struct inode *inode = page->mapping->host;
1433 err = fuse_writepage_locked(page);
1435 fuse_wait_on_page_writeback(inode, page->index);
1441 * Write back dirty pages now, because there may not be any suitable
1444 static void fuse_vma_close(struct vm_area_struct *vma)
1446 filemap_write_and_wait(vma->vm_file->f_mapping);
1450 * Wait for writeback against this page to complete before allowing it
1451 * to be marked dirty again, and hence written back again, possibly
1452 * before the previous writepage completed.
1454 * Block here, instead of in ->writepage(), so that the userspace fs
1455 * can only block processes actually operating on the filesystem.
1457 * Otherwise unprivileged userspace fs would be able to block
1462 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1464 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1466 struct page *page = vmf->page;
1468 * Don't use page->mapping as it may become NULL from a
1469 * concurrent truncate.
1471 struct inode *inode = vma->vm_file->f_mapping->host;
1473 fuse_wait_on_page_writeback(inode, page->index);
1477 static const struct vm_operations_struct fuse_file_vm_ops = {
1478 .close = fuse_vma_close,
1479 .fault = filemap_fault,
1480 .page_mkwrite = fuse_page_mkwrite,
1481 .remap_pages = generic_file_remap_pages,
1484 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1486 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1487 struct inode *inode = file_inode(file);
1488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 struct fuse_inode *fi = get_fuse_inode(inode);
1490 struct fuse_file *ff = file->private_data;
1492 * file may be written through mmap, so chain it onto the
1493 * inodes's write_file list
1495 spin_lock(&fc->lock);
1496 if (list_empty(&ff->write_entry))
1497 list_add(&ff->write_entry, &fi->write_files);
1498 spin_unlock(&fc->lock);
1500 file_accessed(file);
1501 vma->vm_ops = &fuse_file_vm_ops;
1505 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1507 /* Can't provide the coherency needed for MAP_SHARED */
1508 if (vma->vm_flags & VM_MAYSHARE)
1511 invalidate_inode_pages2(file->f_mapping);
1513 return generic_file_mmap(file, vma);
1516 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1517 struct file_lock *fl)
1519 switch (ffl->type) {
1525 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1526 ffl->end < ffl->start)
1529 fl->fl_start = ffl->start;
1530 fl->fl_end = ffl->end;
1531 fl->fl_pid = ffl->pid;
1537 fl->fl_type = ffl->type;
1541 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1542 const struct file_lock *fl, int opcode, pid_t pid,
1545 struct inode *inode = file_inode(file);
1546 struct fuse_conn *fc = get_fuse_conn(inode);
1547 struct fuse_file *ff = file->private_data;
1548 struct fuse_lk_in *arg = &req->misc.lk_in;
1551 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1552 arg->lk.start = fl->fl_start;
1553 arg->lk.end = fl->fl_end;
1554 arg->lk.type = fl->fl_type;
1557 arg->lk_flags |= FUSE_LK_FLOCK;
1558 req->in.h.opcode = opcode;
1559 req->in.h.nodeid = get_node_id(inode);
1560 req->in.numargs = 1;
1561 req->in.args[0].size = sizeof(*arg);
1562 req->in.args[0].value = arg;
1565 static int fuse_getlk(struct file *file, struct file_lock *fl)
1567 struct inode *inode = file_inode(file);
1568 struct fuse_conn *fc = get_fuse_conn(inode);
1569 struct fuse_req *req;
1570 struct fuse_lk_out outarg;
1573 req = fuse_get_req_nopages(fc);
1575 return PTR_ERR(req);
1577 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1578 req->out.numargs = 1;
1579 req->out.args[0].size = sizeof(outarg);
1580 req->out.args[0].value = &outarg;
1581 fuse_request_send(fc, req);
1582 err = req->out.h.error;
1583 fuse_put_request(fc, req);
1585 err = convert_fuse_file_lock(&outarg.lk, fl);
1590 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1592 struct inode *inode = file_inode(file);
1593 struct fuse_conn *fc = get_fuse_conn(inode);
1594 struct fuse_req *req;
1595 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1596 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1599 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
1600 /* NLM needs asynchronous locks, which we don't support yet */
1604 /* Unlock on close is handled by the flush method */
1605 if (fl->fl_flags & FL_CLOSE)
1608 req = fuse_get_req_nopages(fc);
1610 return PTR_ERR(req);
1612 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1613 fuse_request_send(fc, req);
1614 err = req->out.h.error;
1615 /* locking is restartable */
1618 fuse_put_request(fc, req);
1622 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1624 struct inode *inode = file_inode(file);
1625 struct fuse_conn *fc = get_fuse_conn(inode);
1628 if (cmd == F_CANCELLK) {
1630 } else if (cmd == F_GETLK) {
1632 posix_test_lock(file, fl);
1635 err = fuse_getlk(file, fl);
1638 err = posix_lock_file(file, fl, NULL);
1640 err = fuse_setlk(file, fl, 0);
1645 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1647 struct inode *inode = file_inode(file);
1648 struct fuse_conn *fc = get_fuse_conn(inode);
1652 err = flock_lock_file_wait(file, fl);
1654 struct fuse_file *ff = file->private_data;
1656 /* emulate flock with POSIX locks */
1657 fl->fl_owner = (fl_owner_t) file;
1659 err = fuse_setlk(file, fl, 1);
1665 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1667 struct inode *inode = mapping->host;
1668 struct fuse_conn *fc = get_fuse_conn(inode);
1669 struct fuse_req *req;
1670 struct fuse_bmap_in inarg;
1671 struct fuse_bmap_out outarg;
1674 if (!inode->i_sb->s_bdev || fc->no_bmap)
1677 req = fuse_get_req_nopages(fc);
1681 memset(&inarg, 0, sizeof(inarg));
1682 inarg.block = block;
1683 inarg.blocksize = inode->i_sb->s_blocksize;
1684 req->in.h.opcode = FUSE_BMAP;
1685 req->in.h.nodeid = get_node_id(inode);
1686 req->in.numargs = 1;
1687 req->in.args[0].size = sizeof(inarg);
1688 req->in.args[0].value = &inarg;
1689 req->out.numargs = 1;
1690 req->out.args[0].size = sizeof(outarg);
1691 req->out.args[0].value = &outarg;
1692 fuse_request_send(fc, req);
1693 err = req->out.h.error;
1694 fuse_put_request(fc, req);
1698 return err ? 0 : outarg.block;
1701 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
1704 struct inode *inode = file_inode(file);
1706 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
1707 if (whence == SEEK_CUR || whence == SEEK_SET)
1708 return generic_file_llseek(file, offset, whence);
1710 mutex_lock(&inode->i_mutex);
1711 retval = fuse_update_attributes(inode, NULL, file, NULL);
1713 retval = generic_file_llseek(file, offset, whence);
1714 mutex_unlock(&inode->i_mutex);
1719 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1720 unsigned int nr_segs, size_t bytes, bool to_user)
1728 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1730 while (iov_iter_count(&ii)) {
1731 struct page *page = pages[page_idx++];
1732 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1738 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1739 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1740 size_t copy = min(todo, iov_len);
1744 left = copy_from_user(kaddr, uaddr, copy);
1746 left = copy_to_user(uaddr, kaddr, copy);
1751 iov_iter_advance(&ii, copy);
1763 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1764 * ABI was defined to be 'struct iovec' which is different on 32bit
1765 * and 64bit. Fortunately we can determine which structure the server
1766 * used from the size of the reply.
1768 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1769 size_t transferred, unsigned count,
1772 #ifdef CONFIG_COMPAT
1773 if (count * sizeof(struct compat_iovec) == transferred) {
1774 struct compat_iovec *ciov = src;
1778 * With this interface a 32bit server cannot support
1779 * non-compat (i.e. ones coming from 64bit apps) ioctl
1785 for (i = 0; i < count; i++) {
1786 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1787 dst[i].iov_len = ciov[i].iov_len;
1793 if (count * sizeof(struct iovec) != transferred)
1796 memcpy(dst, src, transferred);
1800 /* Make sure iov_length() won't overflow */
1801 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1804 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1806 for (n = 0; n < count; n++, iov++) {
1807 if (iov->iov_len > (size_t) max)
1809 max -= iov->iov_len;
1814 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1815 void *src, size_t transferred, unsigned count,
1819 struct fuse_ioctl_iovec *fiov = src;
1821 if (fc->minor < 16) {
1822 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1826 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1829 for (i = 0; i < count; i++) {
1830 /* Did the server supply an inappropriate value? */
1831 if (fiov[i].base != (unsigned long) fiov[i].base ||
1832 fiov[i].len != (unsigned long) fiov[i].len)
1835 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1836 dst[i].iov_len = (size_t) fiov[i].len;
1838 #ifdef CONFIG_COMPAT
1840 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1841 (compat_size_t) dst[i].iov_len != fiov[i].len))
1851 * For ioctls, there is no generic way to determine how much memory
1852 * needs to be read and/or written. Furthermore, ioctls are allowed
1853 * to dereference the passed pointer, so the parameter requires deep
1854 * copying but FUSE has no idea whatsoever about what to copy in or
1857 * This is solved by allowing FUSE server to retry ioctl with
1858 * necessary in/out iovecs. Let's assume the ioctl implementation
1859 * needs to read in the following structure.
1866 * On the first callout to FUSE server, inarg->in_size and
1867 * inarg->out_size will be NULL; then, the server completes the ioctl
1868 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1869 * the actual iov array to
1871 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1873 * which tells FUSE to copy in the requested area and retry the ioctl.
1874 * On the second round, the server has access to the structure and
1875 * from that it can tell what to look for next, so on the invocation,
1876 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1878 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1879 * { .iov_base = a.buf, .iov_len = a.buflen } }
1881 * FUSE will copy both struct a and the pointed buffer from the
1882 * process doing the ioctl and retry ioctl with both struct a and the
1885 * This time, FUSE server has everything it needs and completes ioctl
1886 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1888 * Copying data out works the same way.
1890 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1891 * automatically initializes in and out iovs by decoding @cmd with
1892 * _IOC_* macros and the server is not allowed to request RETRY. This
1893 * limits ioctl data transfers to well-formed ioctls and is the forced
1894 * behavior for all FUSE servers.
1896 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1899 struct fuse_file *ff = file->private_data;
1900 struct fuse_conn *fc = ff->fc;
1901 struct fuse_ioctl_in inarg = {
1907 struct fuse_ioctl_out outarg;
1908 struct fuse_req *req = NULL;
1909 struct page **pages = NULL;
1910 struct iovec *iov_page = NULL;
1911 struct iovec *in_iov = NULL, *out_iov = NULL;
1912 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1913 size_t in_size, out_size, transferred;
1916 #if BITS_PER_LONG == 32
1917 inarg.flags |= FUSE_IOCTL_32BIT;
1919 if (flags & FUSE_IOCTL_COMPAT)
1920 inarg.flags |= FUSE_IOCTL_32BIT;
1923 /* assume all the iovs returned by client always fits in a page */
1924 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1927 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
1928 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
1929 if (!pages || !iov_page)
1933 * If restricted, initialize IO parameters as encoded in @cmd.
1934 * RETRY from server is not allowed.
1936 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1937 struct iovec *iov = iov_page;
1939 iov->iov_base = (void __user *)arg;
1940 iov->iov_len = _IOC_SIZE(cmd);
1942 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1947 if (_IOC_DIR(cmd) & _IOC_READ) {
1954 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1955 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1958 * Out data can be used either for actual out data or iovs,
1959 * make sure there always is at least one page.
1961 out_size = max_t(size_t, out_size, PAGE_SIZE);
1962 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1964 /* make sure there are enough buffer pages and init request with them */
1966 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1968 while (num_pages < max_pages) {
1969 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1970 if (!pages[num_pages])
1975 req = fuse_get_req(fc, num_pages);
1981 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1982 req->num_pages = num_pages;
1983 fuse_page_descs_length_init(req, 0, req->num_pages);
1985 /* okay, let's send it to the client */
1986 req->in.h.opcode = FUSE_IOCTL;
1987 req->in.h.nodeid = ff->nodeid;
1988 req->in.numargs = 1;
1989 req->in.args[0].size = sizeof(inarg);
1990 req->in.args[0].value = &inarg;
1993 req->in.args[1].size = in_size;
1994 req->in.argpages = 1;
1996 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2002 req->out.numargs = 2;
2003 req->out.args[0].size = sizeof(outarg);
2004 req->out.args[0].value = &outarg;
2005 req->out.args[1].size = out_size;
2006 req->out.argpages = 1;
2007 req->out.argvar = 1;
2009 fuse_request_send(fc, req);
2010 err = req->out.h.error;
2011 transferred = req->out.args[1].size;
2012 fuse_put_request(fc, req);
2017 /* did it ask for retry? */
2018 if (outarg.flags & FUSE_IOCTL_RETRY) {
2021 /* no retry if in restricted mode */
2023 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2026 in_iovs = outarg.in_iovs;
2027 out_iovs = outarg.out_iovs;
2030 * Make sure things are in boundary, separate checks
2031 * are to protect against overflow.
2034 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2035 out_iovs > FUSE_IOCTL_MAX_IOV ||
2036 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2039 vaddr = kmap_atomic(pages[0]);
2040 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2041 transferred, in_iovs + out_iovs,
2042 (flags & FUSE_IOCTL_COMPAT) != 0);
2043 kunmap_atomic(vaddr);
2048 out_iov = in_iov + in_iovs;
2050 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2054 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2062 if (transferred > inarg.out_size)
2065 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2068 fuse_put_request(fc, req);
2069 free_page((unsigned long) iov_page);
2071 __free_page(pages[--num_pages]);
2074 return err ? err : outarg.result;
2076 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2078 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2079 unsigned long arg, unsigned int flags)
2081 struct inode *inode = file_inode(file);
2082 struct fuse_conn *fc = get_fuse_conn(inode);
2084 if (!fuse_allow_current_process(fc))
2087 if (is_bad_inode(inode))
2090 return fuse_do_ioctl(file, cmd, arg, flags);
2093 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2096 return fuse_ioctl_common(file, cmd, arg, 0);
2099 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2102 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2106 * All files which have been polled are linked to RB tree
2107 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2108 * find the matching one.
2110 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2111 struct rb_node **parent_out)
2113 struct rb_node **link = &fc->polled_files.rb_node;
2114 struct rb_node *last = NULL;
2117 struct fuse_file *ff;
2120 ff = rb_entry(last, struct fuse_file, polled_node);
2123 link = &last->rb_left;
2124 else if (kh > ff->kh)
2125 link = &last->rb_right;
2136 * The file is about to be polled. Make sure it's on the polled_files
2137 * RB tree. Note that files once added to the polled_files tree are
2138 * not removed before the file is released. This is because a file
2139 * polled once is likely to be polled again.
2141 static void fuse_register_polled_file(struct fuse_conn *fc,
2142 struct fuse_file *ff)
2144 spin_lock(&fc->lock);
2145 if (RB_EMPTY_NODE(&ff->polled_node)) {
2146 struct rb_node **link, *parent;
2148 link = fuse_find_polled_node(fc, ff->kh, &parent);
2150 rb_link_node(&ff->polled_node, parent, link);
2151 rb_insert_color(&ff->polled_node, &fc->polled_files);
2153 spin_unlock(&fc->lock);
2156 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2158 struct fuse_file *ff = file->private_data;
2159 struct fuse_conn *fc = ff->fc;
2160 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2161 struct fuse_poll_out outarg;
2162 struct fuse_req *req;
2166 return DEFAULT_POLLMASK;
2168 poll_wait(file, &ff->poll_wait, wait);
2169 inarg.events = (__u32)poll_requested_events(wait);
2172 * Ask for notification iff there's someone waiting for it.
2173 * The client may ignore the flag and always notify.
2175 if (waitqueue_active(&ff->poll_wait)) {
2176 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2177 fuse_register_polled_file(fc, ff);
2180 req = fuse_get_req_nopages(fc);
2184 req->in.h.opcode = FUSE_POLL;
2185 req->in.h.nodeid = ff->nodeid;
2186 req->in.numargs = 1;
2187 req->in.args[0].size = sizeof(inarg);
2188 req->in.args[0].value = &inarg;
2189 req->out.numargs = 1;
2190 req->out.args[0].size = sizeof(outarg);
2191 req->out.args[0].value = &outarg;
2192 fuse_request_send(fc, req);
2193 err = req->out.h.error;
2194 fuse_put_request(fc, req);
2197 return outarg.revents;
2198 if (err == -ENOSYS) {
2200 return DEFAULT_POLLMASK;
2204 EXPORT_SYMBOL_GPL(fuse_file_poll);
2207 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2208 * wakes up the poll waiters.
2210 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2211 struct fuse_notify_poll_wakeup_out *outarg)
2213 u64 kh = outarg->kh;
2214 struct rb_node **link;
2216 spin_lock(&fc->lock);
2218 link = fuse_find_polled_node(fc, kh, NULL);
2220 struct fuse_file *ff;
2222 ff = rb_entry(*link, struct fuse_file, polled_node);
2223 wake_up_interruptible_sync(&ff->poll_wait);
2226 spin_unlock(&fc->lock);
2231 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2232 loff_t offset, unsigned long nr_segs)
2235 struct file *file = NULL;
2238 file = iocb->ki_filp;
2242 ret = __fuse_direct_write(file, iov, nr_segs, &pos);
2244 ret = __fuse_direct_read(file, iov, nr_segs, &pos);
2249 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2252 struct fuse_file *ff = file->private_data;
2253 struct fuse_conn *fc = ff->fc;
2254 struct fuse_req *req;
2255 struct fuse_fallocate_in inarg = {
2263 if (fc->no_fallocate)
2266 req = fuse_get_req_nopages(fc);
2268 return PTR_ERR(req);
2270 req->in.h.opcode = FUSE_FALLOCATE;
2271 req->in.h.nodeid = ff->nodeid;
2272 req->in.numargs = 1;
2273 req->in.args[0].size = sizeof(inarg);
2274 req->in.args[0].value = &inarg;
2275 fuse_request_send(fc, req);
2276 err = req->out.h.error;
2277 if (err == -ENOSYS) {
2278 fc->no_fallocate = 1;
2281 fuse_put_request(fc, req);
2286 static const struct file_operations fuse_file_operations = {
2287 .llseek = fuse_file_llseek,
2288 .read = do_sync_read,
2289 .aio_read = fuse_file_aio_read,
2290 .write = do_sync_write,
2291 .aio_write = fuse_file_aio_write,
2292 .mmap = fuse_file_mmap,
2294 .flush = fuse_flush,
2295 .release = fuse_release,
2296 .fsync = fuse_fsync,
2297 .lock = fuse_file_lock,
2298 .flock = fuse_file_flock,
2299 .splice_read = generic_file_splice_read,
2300 .unlocked_ioctl = fuse_file_ioctl,
2301 .compat_ioctl = fuse_file_compat_ioctl,
2302 .poll = fuse_file_poll,
2303 .fallocate = fuse_file_fallocate,
2306 static const struct file_operations fuse_direct_io_file_operations = {
2307 .llseek = fuse_file_llseek,
2308 .read = fuse_direct_read,
2309 .write = fuse_direct_write,
2310 .mmap = fuse_direct_mmap,
2312 .flush = fuse_flush,
2313 .release = fuse_release,
2314 .fsync = fuse_fsync,
2315 .lock = fuse_file_lock,
2316 .flock = fuse_file_flock,
2317 .unlocked_ioctl = fuse_file_ioctl,
2318 .compat_ioctl = fuse_file_compat_ioctl,
2319 .poll = fuse_file_poll,
2320 .fallocate = fuse_file_fallocate,
2321 /* no splice_read */
2324 static const struct address_space_operations fuse_file_aops = {
2325 .readpage = fuse_readpage,
2326 .writepage = fuse_writepage,
2327 .launder_page = fuse_launder_page,
2328 .readpages = fuse_readpages,
2329 .set_page_dirty = __set_page_dirty_nobuffers,
2331 .direct_IO = fuse_direct_IO,
2334 void fuse_init_file_inode(struct inode *inode)
2336 inode->i_fop = &fuse_file_operations;
2337 inode->i_data.a_ops = &fuse_file_aops;