2 FUSE: Filesystem in Userspace
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>
18 static const struct file_operations fuse_direct_io_file_operations;
20 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
21 int opcode, struct fuse_open_out *outargp)
23 struct fuse_open_in inarg;
27 req = fuse_get_req(fc);
31 memset(&inarg, 0, sizeof(inarg));
32 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
33 if (!fc->atomic_o_trunc)
34 inarg.flags &= ~O_TRUNC;
35 req->in.h.opcode = opcode;
36 req->in.h.nodeid = nodeid;
38 req->in.args[0].size = sizeof(inarg);
39 req->in.args[0].value = &inarg;
41 req->out.args[0].size = sizeof(*outargp);
42 req->out.args[0].value = outargp;
43 fuse_request_send(fc, req);
44 err = req->out.h.error;
45 fuse_put_request(fc, req);
50 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
54 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
59 ff->reserved_req = fuse_request_alloc();
60 if (unlikely(!ff->reserved_req)) {
65 INIT_LIST_HEAD(&ff->write_entry);
66 atomic_set(&ff->count, 0);
67 RB_CLEAR_NODE(&ff->polled_node);
68 init_waitqueue_head(&ff->poll_wait);
72 spin_unlock(&fc->lock);
77 void fuse_file_free(struct fuse_file *ff)
79 fuse_request_free(ff->reserved_req);
83 struct fuse_file *fuse_file_get(struct fuse_file *ff)
85 atomic_inc(&ff->count);
89 static void fuse_release_async(struct work_struct *work)
95 req = container_of(work, struct fuse_req, misc.release.work);
96 path = req->misc.release.path;
97 fc = get_fuse_conn(path.dentry->d_inode);
99 fuse_put_request(fc, req);
103 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
105 if (fc->destroy_req) {
107 * If this is a fuseblk mount, then it's possible that
108 * releasing the path will result in releasing the
109 * super block and sending the DESTROY request. If
110 * the server is single threaded, this would hang.
111 * For this reason do the path_put() in a separate
114 atomic_inc(&req->count);
115 INIT_WORK(&req->misc.release.work, fuse_release_async);
116 schedule_work(&req->misc.release.work);
118 path_put(&req->misc.release.path);
122 static void fuse_file_put(struct fuse_file *ff, bool sync)
124 if (atomic_dec_and_test(&ff->count)) {
125 struct fuse_req *req = ff->reserved_req;
128 fuse_request_send(ff->fc, req);
129 path_put(&req->misc.release.path);
130 fuse_put_request(ff->fc, req);
132 req->end = fuse_release_end;
133 fuse_request_send_background(ff->fc, req);
139 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
142 struct fuse_open_out outarg;
143 struct fuse_file *ff;
145 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
147 ff = fuse_file_alloc(fc);
151 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
158 outarg.open_flags &= ~FOPEN_DIRECT_IO;
162 ff->open_flags = outarg.open_flags;
163 file->private_data = fuse_file_get(ff);
167 EXPORT_SYMBOL_GPL(fuse_do_open);
169 void fuse_finish_open(struct inode *inode, struct file *file)
171 struct fuse_file *ff = file->private_data;
172 struct fuse_conn *fc = get_fuse_conn(inode);
174 if (ff->open_flags & FOPEN_DIRECT_IO)
175 file->f_op = &fuse_direct_io_file_operations;
176 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
177 invalidate_inode_pages2(inode->i_mapping);
178 if (ff->open_flags & FOPEN_NONSEEKABLE)
179 nonseekable_open(inode, file);
180 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
181 struct fuse_inode *fi = get_fuse_inode(inode);
183 spin_lock(&fc->lock);
184 fi->attr_version = ++fc->attr_version;
185 i_size_write(inode, 0);
186 spin_unlock(&fc->lock);
187 fuse_invalidate_attr(inode);
191 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
193 struct fuse_conn *fc = get_fuse_conn(inode);
196 /* VFS checks this, but only _after_ ->open() */
197 if (file->f_flags & O_DIRECT)
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);
248 /* Hold vfsmount and dentry until release is finished */
249 path_get(&file->f_path);
250 req->misc.release.path = file->f_path;
253 * Normally this will send the RELEASE request, however if
254 * some asynchronous READ or WRITE requests are outstanding,
255 * the sending will be delayed.
257 * Make the release synchronous if this is a fuseblk mount,
258 * synchronous RELEASE is allowed (and desirable) in this case
259 * because the server can be trusted not to screw up.
261 fuse_file_put(ff, ff->fc->destroy_req != NULL);
264 static int fuse_open(struct inode *inode, struct file *file)
266 return fuse_open_common(inode, file, false);
269 static int fuse_release(struct inode *inode, struct file *file)
271 fuse_release_common(file, FUSE_RELEASE);
273 /* return value is ignored by VFS */
277 void fuse_sync_release(struct fuse_file *ff, int flags)
279 WARN_ON(atomic_read(&ff->count) > 1);
280 fuse_prepare_release(ff, flags, FUSE_RELEASE);
281 ff->reserved_req->force = 1;
282 fuse_request_send(ff->fc, ff->reserved_req);
283 fuse_put_request(ff->fc, ff->reserved_req);
286 EXPORT_SYMBOL_GPL(fuse_sync_release);
289 * Scramble the ID space with XTEA, so that the value of the files_struct
290 * pointer is not exposed to userspace.
292 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
294 u32 *k = fc->scramble_key;
295 u64 v = (unsigned long) id;
301 for (i = 0; i < 32; i++) {
302 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
304 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
307 return (u64) v0 + ((u64) v1 << 32);
311 * Check if page is under writeback
313 * This is currently done by walking the list of writepage requests
314 * for the inode, which can be pretty inefficient.
316 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
318 struct fuse_conn *fc = get_fuse_conn(inode);
319 struct fuse_inode *fi = get_fuse_inode(inode);
320 struct fuse_req *req;
323 spin_lock(&fc->lock);
324 list_for_each_entry(req, &fi->writepages, writepages_entry) {
327 BUG_ON(req->inode != inode);
328 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
329 if (curr_index == index) {
334 spin_unlock(&fc->lock);
340 * Wait for page writeback to be completed.
342 * Since fuse doesn't rely on the VM writeback tracking, this has to
343 * use some other means.
345 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
347 struct fuse_inode *fi = get_fuse_inode(inode);
349 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
353 static int fuse_flush(struct file *file, fl_owner_t id)
355 struct inode *inode = file->f_path.dentry->d_inode;
356 struct fuse_conn *fc = get_fuse_conn(inode);
357 struct fuse_file *ff = file->private_data;
358 struct fuse_req *req;
359 struct fuse_flush_in inarg;
362 if (is_bad_inode(inode))
368 req = fuse_get_req_nofail(fc, file);
369 memset(&inarg, 0, sizeof(inarg));
371 inarg.lock_owner = fuse_lock_owner_id(fc, id);
372 req->in.h.opcode = FUSE_FLUSH;
373 req->in.h.nodeid = get_node_id(inode);
375 req->in.args[0].size = sizeof(inarg);
376 req->in.args[0].value = &inarg;
378 fuse_request_send(fc, req);
379 err = req->out.h.error;
380 fuse_put_request(fc, req);
381 if (err == -ENOSYS) {
389 * Wait for all pending writepages on the inode to finish.
391 * This is currently done by blocking further writes with FUSE_NOWRITE
392 * and waiting for all sent writes to complete.
394 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
395 * could conflict with truncation.
397 static void fuse_sync_writes(struct inode *inode)
399 fuse_set_nowrite(inode);
400 fuse_release_nowrite(inode);
403 int fuse_fsync_common(struct file *file, int datasync, int isdir)
405 struct inode *inode = file->f_mapping->host;
406 struct fuse_conn *fc = get_fuse_conn(inode);
407 struct fuse_file *ff = file->private_data;
408 struct fuse_req *req;
409 struct fuse_fsync_in inarg;
412 if (is_bad_inode(inode))
415 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
419 * Start writeback against all dirty pages of the inode, then
420 * wait for all outstanding writes, before sending the FSYNC
423 err = write_inode_now(inode, 0);
427 fuse_sync_writes(inode);
429 req = fuse_get_req(fc);
433 memset(&inarg, 0, sizeof(inarg));
435 inarg.fsync_flags = datasync ? 1 : 0;
436 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
437 req->in.h.nodeid = get_node_id(inode);
439 req->in.args[0].size = sizeof(inarg);
440 req->in.args[0].value = &inarg;
441 fuse_request_send(fc, req);
442 err = req->out.h.error;
443 fuse_put_request(fc, req);
444 if (err == -ENOSYS) {
454 static int fuse_fsync(struct file *file, int datasync)
456 return fuse_fsync_common(file, datasync, 0);
459 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
460 size_t count, int opcode)
462 struct fuse_read_in *inarg = &req->misc.read.in;
463 struct fuse_file *ff = file->private_data;
468 inarg->flags = file->f_flags;
469 req->in.h.opcode = opcode;
470 req->in.h.nodeid = ff->nodeid;
472 req->in.args[0].size = sizeof(struct fuse_read_in);
473 req->in.args[0].value = inarg;
475 req->out.numargs = 1;
476 req->out.args[0].size = count;
479 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
480 loff_t pos, size_t count, fl_owner_t owner)
482 struct fuse_file *ff = file->private_data;
483 struct fuse_conn *fc = ff->fc;
485 fuse_read_fill(req, file, pos, count, FUSE_READ);
487 struct fuse_read_in *inarg = &req->misc.read.in;
489 inarg->read_flags |= FUSE_READ_LOCKOWNER;
490 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
492 fuse_request_send(fc, req);
493 return req->out.args[0].size;
496 static void fuse_read_update_size(struct inode *inode, loff_t size,
499 struct fuse_conn *fc = get_fuse_conn(inode);
500 struct fuse_inode *fi = get_fuse_inode(inode);
502 spin_lock(&fc->lock);
503 if (attr_ver == fi->attr_version && size < inode->i_size) {
504 fi->attr_version = ++fc->attr_version;
505 i_size_write(inode, size);
507 spin_unlock(&fc->lock);
510 static int fuse_readpage(struct file *file, struct page *page)
512 struct inode *inode = page->mapping->host;
513 struct fuse_conn *fc = get_fuse_conn(inode);
514 struct fuse_req *req;
516 loff_t pos = page_offset(page);
517 size_t count = PAGE_CACHE_SIZE;
522 if (is_bad_inode(inode))
526 * Page writeback can extend beyond the lifetime of the
527 * page-cache page, so make sure we read a properly synced
530 fuse_wait_on_page_writeback(inode, page->index);
532 req = fuse_get_req(fc);
537 attr_ver = fuse_get_attr_version(fc);
539 req->out.page_zeroing = 1;
540 req->out.argpages = 1;
542 req->pages[0] = page;
543 num_read = fuse_send_read(req, file, pos, count, NULL);
544 err = req->out.h.error;
545 fuse_put_request(fc, req);
549 * Short read means EOF. If file size is larger, truncate it
551 if (num_read < count)
552 fuse_read_update_size(inode, pos + num_read, attr_ver);
554 SetPageUptodate(page);
557 fuse_invalidate_attr(inode); /* atime changed */
563 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
566 size_t count = req->misc.read.in.size;
567 size_t num_read = req->out.args[0].size;
568 struct address_space *mapping = NULL;
570 for (i = 0; mapping == NULL && i < req->num_pages; i++)
571 mapping = req->pages[i]->mapping;
574 struct inode *inode = mapping->host;
577 * Short read means EOF. If file size is larger, truncate it
579 if (!req->out.h.error && num_read < count) {
582 pos = page_offset(req->pages[0]) + num_read;
583 fuse_read_update_size(inode, pos,
584 req->misc.read.attr_ver);
586 fuse_invalidate_attr(inode); /* atime changed */
589 for (i = 0; i < req->num_pages; i++) {
590 struct page *page = req->pages[i];
591 if (!req->out.h.error)
592 SetPageUptodate(page);
596 page_cache_release(page);
599 fuse_file_put(req->ff, false);
602 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
604 struct fuse_file *ff = file->private_data;
605 struct fuse_conn *fc = ff->fc;
606 loff_t pos = page_offset(req->pages[0]);
607 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
609 req->out.argpages = 1;
610 req->out.page_zeroing = 1;
611 req->out.page_replace = 1;
612 fuse_read_fill(req, file, pos, count, FUSE_READ);
613 req->misc.read.attr_ver = fuse_get_attr_version(fc);
614 if (fc->async_read) {
615 req->ff = fuse_file_get(ff);
616 req->end = fuse_readpages_end;
617 fuse_request_send_background(fc, req);
619 fuse_request_send(fc, req);
620 fuse_readpages_end(fc, req);
621 fuse_put_request(fc, req);
625 struct fuse_fill_data {
626 struct fuse_req *req;
631 static int fuse_readpages_fill(void *_data, struct page *page)
633 struct fuse_fill_data *data = _data;
634 struct fuse_req *req = data->req;
635 struct inode *inode = data->inode;
636 struct fuse_conn *fc = get_fuse_conn(inode);
638 fuse_wait_on_page_writeback(inode, page->index);
640 if (req->num_pages &&
641 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
642 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
643 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
644 fuse_send_readpages(req, data->file);
645 data->req = req = fuse_get_req(fc);
651 page_cache_get(page);
652 req->pages[req->num_pages] = page;
657 static int fuse_readpages(struct file *file, struct address_space *mapping,
658 struct list_head *pages, unsigned nr_pages)
660 struct inode *inode = mapping->host;
661 struct fuse_conn *fc = get_fuse_conn(inode);
662 struct fuse_fill_data data;
666 if (is_bad_inode(inode))
671 data.req = fuse_get_req(fc);
672 err = PTR_ERR(data.req);
673 if (IS_ERR(data.req))
676 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
678 if (data.req->num_pages)
679 fuse_send_readpages(data.req, file);
681 fuse_put_request(fc, data.req);
687 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
688 unsigned long nr_segs, loff_t pos)
690 struct inode *inode = iocb->ki_filp->f_mapping->host;
692 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
695 * If trying to read past EOF, make sure the i_size
696 * attribute is up-to-date.
698 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
703 return generic_file_aio_read(iocb, iov, nr_segs, pos);
706 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
707 loff_t pos, size_t count)
709 struct fuse_write_in *inarg = &req->misc.write.in;
710 struct fuse_write_out *outarg = &req->misc.write.out;
715 req->in.h.opcode = FUSE_WRITE;
716 req->in.h.nodeid = ff->nodeid;
718 if (ff->fc->minor < 9)
719 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
721 req->in.args[0].size = sizeof(struct fuse_write_in);
722 req->in.args[0].value = inarg;
723 req->in.args[1].size = count;
724 req->out.numargs = 1;
725 req->out.args[0].size = sizeof(struct fuse_write_out);
726 req->out.args[0].value = outarg;
729 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
730 loff_t pos, size_t count, fl_owner_t owner)
732 struct fuse_file *ff = file->private_data;
733 struct fuse_conn *fc = ff->fc;
734 struct fuse_write_in *inarg = &req->misc.write.in;
736 fuse_write_fill(req, ff, pos, count);
737 inarg->flags = file->f_flags;
739 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
740 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
742 fuse_request_send(fc, req);
743 return req->misc.write.out.size;
746 static int fuse_write_begin(struct file *file, struct address_space *mapping,
747 loff_t pos, unsigned len, unsigned flags,
748 struct page **pagep, void **fsdata)
750 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
752 *pagep = grab_cache_page_write_begin(mapping, index, flags);
758 void fuse_write_update_size(struct inode *inode, loff_t pos)
760 struct fuse_conn *fc = get_fuse_conn(inode);
761 struct fuse_inode *fi = get_fuse_inode(inode);
763 spin_lock(&fc->lock);
764 fi->attr_version = ++fc->attr_version;
765 if (pos > inode->i_size)
766 i_size_write(inode, pos);
767 spin_unlock(&fc->lock);
770 static int fuse_buffered_write(struct file *file, struct inode *inode,
771 loff_t pos, unsigned count, struct page *page)
775 struct fuse_conn *fc = get_fuse_conn(inode);
776 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
777 struct fuse_req *req;
779 if (is_bad_inode(inode))
783 * Make sure writepages on the same page are not mixed up with
786 fuse_wait_on_page_writeback(inode, page->index);
788 req = fuse_get_req(fc);
792 req->in.argpages = 1;
794 req->pages[0] = page;
795 req->page_offset = offset;
796 nres = fuse_send_write(req, file, pos, count, NULL);
797 err = req->out.h.error;
798 fuse_put_request(fc, req);
803 fuse_write_update_size(inode, pos);
804 if (count == PAGE_CACHE_SIZE)
805 SetPageUptodate(page);
807 fuse_invalidate_attr(inode);
808 return err ? err : nres;
811 static int fuse_write_end(struct file *file, struct address_space *mapping,
812 loff_t pos, unsigned len, unsigned copied,
813 struct page *page, void *fsdata)
815 struct inode *inode = mapping->host;
819 res = fuse_buffered_write(file, inode, pos, copied, page);
822 page_cache_release(page);
826 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
827 struct inode *inode, loff_t pos,
834 for (i = 0; i < req->num_pages; i++)
835 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
837 res = fuse_send_write(req, file, pos, count, NULL);
839 offset = req->page_offset;
841 for (i = 0; i < req->num_pages; i++) {
842 struct page *page = req->pages[i];
844 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
845 SetPageUptodate(page);
847 if (count > PAGE_CACHE_SIZE - offset)
848 count -= PAGE_CACHE_SIZE - offset;
854 page_cache_release(page);
860 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
861 struct address_space *mapping,
862 struct iov_iter *ii, loff_t pos)
864 struct fuse_conn *fc = get_fuse_conn(mapping->host);
865 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
869 req->in.argpages = 1;
870 req->page_offset = offset;
875 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
876 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
879 bytes = min_t(size_t, bytes, fc->max_write - count);
883 if (iov_iter_fault_in_readable(ii, bytes))
887 page = grab_cache_page_write_begin(mapping, index, 0);
891 if (mapping_writably_mapped(mapping))
892 flush_dcache_page(page);
895 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
897 flush_dcache_page(page);
901 page_cache_release(page);
902 bytes = min(bytes, iov_iter_single_seg_count(ii));
907 req->pages[req->num_pages] = page;
910 iov_iter_advance(ii, tmp);
914 if (offset == PAGE_CACHE_SIZE)
919 } while (iov_iter_count(ii) && count < fc->max_write &&
920 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
922 return count > 0 ? count : err;
925 static ssize_t fuse_perform_write(struct file *file,
926 struct address_space *mapping,
927 struct iov_iter *ii, loff_t pos)
929 struct inode *inode = mapping->host;
930 struct fuse_conn *fc = get_fuse_conn(inode);
934 if (is_bad_inode(inode))
938 struct fuse_req *req;
941 req = fuse_get_req(fc);
947 count = fuse_fill_write_pages(req, mapping, ii, pos);
953 num_written = fuse_send_write_pages(req, file, inode,
955 err = req->out.h.error;
960 /* break out of the loop on short write */
961 if (num_written != count)
965 fuse_put_request(fc, req);
966 } while (!err && iov_iter_count(ii));
969 fuse_write_update_size(inode, pos);
971 fuse_invalidate_attr(inode);
973 return res > 0 ? res : err;
976 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
977 unsigned long nr_segs, loff_t pos)
979 struct file *file = iocb->ki_filp;
980 struct address_space *mapping = file->f_mapping;
983 struct inode *inode = mapping->host;
987 WARN_ON(iocb->ki_pos != pos);
989 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
993 mutex_lock(&inode->i_mutex);
994 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
996 /* We can write back this queue in page reclaim */
997 current->backing_dev_info = mapping->backing_dev_info;
999 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1006 err = file_remove_suid(file);
1010 file_update_time(file);
1012 iov_iter_init(&i, iov, nr_segs, count, 0);
1013 written = fuse_perform_write(file, mapping, &i, pos);
1015 iocb->ki_pos = pos + written;
1018 current->backing_dev_info = NULL;
1019 mutex_unlock(&inode->i_mutex);
1021 return written ? written : err;
1024 static void fuse_release_user_pages(struct fuse_req *req, int write)
1028 for (i = 0; i < req->num_pages; i++) {
1029 struct page *page = req->pages[i];
1031 set_page_dirty_lock(page);
1036 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
1037 size_t *nbytesp, int write)
1039 size_t nbytes = *nbytesp;
1040 unsigned long user_addr = (unsigned long) buf;
1041 unsigned offset = user_addr & ~PAGE_MASK;
1044 /* Special case for kernel I/O: can copy directly into the buffer */
1045 if (segment_eq(get_fs(), KERNEL_DS)) {
1047 req->in.args[1].value = (void *) user_addr;
1049 req->out.args[0].value = (void *) user_addr;
1054 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
1055 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1056 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
1057 npages = get_user_pages_fast(user_addr, npages, !write, req->pages);
1061 req->num_pages = npages;
1062 req->page_offset = offset;
1065 req->in.argpages = 1;
1067 req->out.argpages = 1;
1069 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1070 *nbytesp = min(*nbytesp, nbytes);
1075 ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1076 size_t count, loff_t *ppos, int write)
1078 struct fuse_file *ff = file->private_data;
1079 struct fuse_conn *fc = ff->fc;
1080 size_t nmax = write ? fc->max_write : fc->max_read;
1083 struct fuse_req *req;
1085 req = fuse_get_req(fc);
1087 return PTR_ERR(req);
1091 fl_owner_t owner = current->files;
1092 size_t nbytes = min(count, nmax);
1093 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1100 nres = fuse_send_write(req, file, pos, nbytes, owner);
1102 nres = fuse_send_read(req, file, pos, nbytes, owner);
1104 fuse_release_user_pages(req, !write);
1105 if (req->out.h.error) {
1107 res = req->out.h.error;
1109 } else if (nres > nbytes) {
1120 fuse_put_request(fc, req);
1121 req = fuse_get_req(fc);
1127 fuse_put_request(fc, req);
1133 EXPORT_SYMBOL_GPL(fuse_direct_io);
1135 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1136 size_t count, loff_t *ppos)
1139 struct inode *inode = file->f_path.dentry->d_inode;
1141 if (is_bad_inode(inode))
1144 res = fuse_direct_io(file, buf, count, ppos, 0);
1146 fuse_invalidate_attr(inode);
1151 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1152 size_t count, loff_t *ppos)
1154 struct inode *inode = file->f_path.dentry->d_inode;
1157 if (is_bad_inode(inode))
1160 /* Don't allow parallel writes to the same file */
1161 mutex_lock(&inode->i_mutex);
1162 res = generic_write_checks(file, ppos, &count, 0);
1164 res = fuse_direct_io(file, buf, count, ppos, 1);
1166 fuse_write_update_size(inode, *ppos);
1168 mutex_unlock(&inode->i_mutex);
1170 fuse_invalidate_attr(inode);
1175 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1177 __free_page(req->pages[0]);
1178 fuse_file_put(req->ff, false);
1181 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1183 struct inode *inode = req->inode;
1184 struct fuse_inode *fi = get_fuse_inode(inode);
1185 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1187 list_del(&req->writepages_entry);
1188 dec_bdi_stat(bdi, BDI_WRITEBACK);
1189 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1190 bdi_writeout_inc(bdi);
1191 wake_up(&fi->page_waitq);
1194 /* Called under fc->lock, may release and reacquire it */
1195 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1196 __releases(fc->lock)
1197 __acquires(fc->lock)
1199 struct fuse_inode *fi = get_fuse_inode(req->inode);
1200 loff_t size = i_size_read(req->inode);
1201 struct fuse_write_in *inarg = &req->misc.write.in;
1206 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1207 inarg->size = PAGE_CACHE_SIZE;
1208 } else if (inarg->offset < size) {
1209 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1211 /* Got truncated off completely */
1215 req->in.args[1].size = inarg->size;
1217 fuse_request_send_background_locked(fc, req);
1221 fuse_writepage_finish(fc, req);
1222 spin_unlock(&fc->lock);
1223 fuse_writepage_free(fc, req);
1224 fuse_put_request(fc, req);
1225 spin_lock(&fc->lock);
1229 * If fi->writectr is positive (no truncate or fsync going on) send
1230 * all queued writepage requests.
1232 * Called with fc->lock
1234 void fuse_flush_writepages(struct inode *inode)
1235 __releases(fc->lock)
1236 __acquires(fc->lock)
1238 struct fuse_conn *fc = get_fuse_conn(inode);
1239 struct fuse_inode *fi = get_fuse_inode(inode);
1240 struct fuse_req *req;
1242 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1243 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1244 list_del_init(&req->list);
1245 fuse_send_writepage(fc, req);
1249 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1251 struct inode *inode = req->inode;
1252 struct fuse_inode *fi = get_fuse_inode(inode);
1254 mapping_set_error(inode->i_mapping, req->out.h.error);
1255 spin_lock(&fc->lock);
1257 fuse_writepage_finish(fc, req);
1258 spin_unlock(&fc->lock);
1259 fuse_writepage_free(fc, req);
1262 static int fuse_writepage_locked(struct page *page)
1264 struct address_space *mapping = page->mapping;
1265 struct inode *inode = mapping->host;
1266 struct fuse_conn *fc = get_fuse_conn(inode);
1267 struct fuse_inode *fi = get_fuse_inode(inode);
1268 struct fuse_req *req;
1269 struct fuse_file *ff;
1270 struct page *tmp_page;
1272 set_page_writeback(page);
1274 req = fuse_request_alloc_nofs();
1278 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1282 spin_lock(&fc->lock);
1283 BUG_ON(list_empty(&fi->write_files));
1284 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1285 req->ff = fuse_file_get(ff);
1286 spin_unlock(&fc->lock);
1288 fuse_write_fill(req, ff, page_offset(page), 0);
1290 copy_highpage(tmp_page, page);
1291 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1292 req->in.argpages = 1;
1294 req->pages[0] = tmp_page;
1295 req->page_offset = 0;
1296 req->end = fuse_writepage_end;
1299 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1300 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1301 end_page_writeback(page);
1303 spin_lock(&fc->lock);
1304 list_add(&req->writepages_entry, &fi->writepages);
1305 list_add_tail(&req->list, &fi->queued_writes);
1306 fuse_flush_writepages(inode);
1307 spin_unlock(&fc->lock);
1312 fuse_request_free(req);
1314 end_page_writeback(page);
1318 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1322 err = fuse_writepage_locked(page);
1328 static int fuse_launder_page(struct page *page)
1331 if (clear_page_dirty_for_io(page)) {
1332 struct inode *inode = page->mapping->host;
1333 err = fuse_writepage_locked(page);
1335 fuse_wait_on_page_writeback(inode, page->index);
1341 * Write back dirty pages now, because there may not be any suitable
1344 static void fuse_vma_close(struct vm_area_struct *vma)
1346 filemap_write_and_wait(vma->vm_file->f_mapping);
1350 * Wait for writeback against this page to complete before allowing it
1351 * to be marked dirty again, and hence written back again, possibly
1352 * before the previous writepage completed.
1354 * Block here, instead of in ->writepage(), so that the userspace fs
1355 * can only block processes actually operating on the filesystem.
1357 * Otherwise unprivileged userspace fs would be able to block
1362 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1364 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1366 struct page *page = vmf->page;
1368 * Don't use page->mapping as it may become NULL from a
1369 * concurrent truncate.
1371 struct inode *inode = vma->vm_file->f_mapping->host;
1373 fuse_wait_on_page_writeback(inode, page->index);
1377 static const struct vm_operations_struct fuse_file_vm_ops = {
1378 .close = fuse_vma_close,
1379 .fault = filemap_fault,
1380 .page_mkwrite = fuse_page_mkwrite,
1383 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1385 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1386 struct inode *inode = file->f_dentry->d_inode;
1387 struct fuse_conn *fc = get_fuse_conn(inode);
1388 struct fuse_inode *fi = get_fuse_inode(inode);
1389 struct fuse_file *ff = file->private_data;
1391 * file may be written through mmap, so chain it onto the
1392 * inodes's write_file list
1394 spin_lock(&fc->lock);
1395 if (list_empty(&ff->write_entry))
1396 list_add(&ff->write_entry, &fi->write_files);
1397 spin_unlock(&fc->lock);
1399 file_accessed(file);
1400 vma->vm_ops = &fuse_file_vm_ops;
1404 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1406 /* Can't provide the coherency needed for MAP_SHARED */
1407 if (vma->vm_flags & VM_MAYSHARE)
1410 invalidate_inode_pages2(file->f_mapping);
1412 return generic_file_mmap(file, vma);
1415 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1416 struct file_lock *fl)
1418 switch (ffl->type) {
1424 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1425 ffl->end < ffl->start)
1428 fl->fl_start = ffl->start;
1429 fl->fl_end = ffl->end;
1430 fl->fl_pid = ffl->pid;
1436 fl->fl_type = ffl->type;
1440 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1441 const struct file_lock *fl, int opcode, pid_t pid,
1444 struct inode *inode = file->f_path.dentry->d_inode;
1445 struct fuse_conn *fc = get_fuse_conn(inode);
1446 struct fuse_file *ff = file->private_data;
1447 struct fuse_lk_in *arg = &req->misc.lk_in;
1450 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1451 arg->lk.start = fl->fl_start;
1452 arg->lk.end = fl->fl_end;
1453 arg->lk.type = fl->fl_type;
1456 arg->lk_flags |= FUSE_LK_FLOCK;
1457 req->in.h.opcode = opcode;
1458 req->in.h.nodeid = get_node_id(inode);
1459 req->in.numargs = 1;
1460 req->in.args[0].size = sizeof(*arg);
1461 req->in.args[0].value = arg;
1464 static int fuse_getlk(struct file *file, struct file_lock *fl)
1466 struct inode *inode = file->f_path.dentry->d_inode;
1467 struct fuse_conn *fc = get_fuse_conn(inode);
1468 struct fuse_req *req;
1469 struct fuse_lk_out outarg;
1472 req = fuse_get_req(fc);
1474 return PTR_ERR(req);
1476 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1477 req->out.numargs = 1;
1478 req->out.args[0].size = sizeof(outarg);
1479 req->out.args[0].value = &outarg;
1480 fuse_request_send(fc, req);
1481 err = req->out.h.error;
1482 fuse_put_request(fc, req);
1484 err = convert_fuse_file_lock(&outarg.lk, fl);
1489 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1491 struct inode *inode = file->f_path.dentry->d_inode;
1492 struct fuse_conn *fc = get_fuse_conn(inode);
1493 struct fuse_req *req;
1494 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1495 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1498 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1499 /* NLM needs asynchronous locks, which we don't support yet */
1503 /* Unlock on close is handled by the flush method */
1504 if (fl->fl_flags & FL_CLOSE)
1507 req = fuse_get_req(fc);
1509 return PTR_ERR(req);
1511 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1512 fuse_request_send(fc, req);
1513 err = req->out.h.error;
1514 /* locking is restartable */
1517 fuse_put_request(fc, req);
1521 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1523 struct inode *inode = file->f_path.dentry->d_inode;
1524 struct fuse_conn *fc = get_fuse_conn(inode);
1527 if (cmd == F_CANCELLK) {
1529 } else if (cmd == F_GETLK) {
1531 posix_test_lock(file, fl);
1534 err = fuse_getlk(file, fl);
1537 err = posix_lock_file(file, fl, NULL);
1539 err = fuse_setlk(file, fl, 0);
1544 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1546 struct inode *inode = file->f_path.dentry->d_inode;
1547 struct fuse_conn *fc = get_fuse_conn(inode);
1551 err = flock_lock_file_wait(file, fl);
1553 /* emulate flock with POSIX locks */
1554 fl->fl_owner = (fl_owner_t) file;
1555 err = fuse_setlk(file, fl, 1);
1561 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1563 struct inode *inode = mapping->host;
1564 struct fuse_conn *fc = get_fuse_conn(inode);
1565 struct fuse_req *req;
1566 struct fuse_bmap_in inarg;
1567 struct fuse_bmap_out outarg;
1570 if (!inode->i_sb->s_bdev || fc->no_bmap)
1573 req = fuse_get_req(fc);
1577 memset(&inarg, 0, sizeof(inarg));
1578 inarg.block = block;
1579 inarg.blocksize = inode->i_sb->s_blocksize;
1580 req->in.h.opcode = FUSE_BMAP;
1581 req->in.h.nodeid = get_node_id(inode);
1582 req->in.numargs = 1;
1583 req->in.args[0].size = sizeof(inarg);
1584 req->in.args[0].value = &inarg;
1585 req->out.numargs = 1;
1586 req->out.args[0].size = sizeof(outarg);
1587 req->out.args[0].value = &outarg;
1588 fuse_request_send(fc, req);
1589 err = req->out.h.error;
1590 fuse_put_request(fc, req);
1594 return err ? 0 : outarg.block;
1597 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1600 struct inode *inode = file->f_path.dentry->d_inode;
1602 mutex_lock(&inode->i_mutex);
1603 if (origin != SEEK_CUR || origin != SEEK_SET) {
1604 retval = fuse_update_attributes(inode, NULL, file, NULL);
1611 offset += i_size_read(inode);
1614 offset += file->f_pos;
1617 if (offset >= i_size_read(inode)) {
1623 if (offset >= i_size_read(inode)) {
1627 offset = i_size_read(inode);
1631 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1632 if (offset != file->f_pos) {
1633 file->f_pos = offset;
1634 file->f_version = 0;
1639 mutex_unlock(&inode->i_mutex);
1643 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1644 unsigned int nr_segs, size_t bytes, bool to_user)
1652 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1654 while (iov_iter_count(&ii)) {
1655 struct page *page = pages[page_idx++];
1656 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1662 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1663 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1664 size_t copy = min(todo, iov_len);
1668 left = copy_from_user(kaddr, uaddr, copy);
1670 left = copy_to_user(uaddr, kaddr, copy);
1675 iov_iter_advance(&ii, copy);
1687 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1688 * ABI was defined to be 'struct iovec' which is different on 32bit
1689 * and 64bit. Fortunately we can determine which structure the server
1690 * used from the size of the reply.
1692 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1693 size_t transferred, unsigned count,
1696 #ifdef CONFIG_COMPAT
1697 if (count * sizeof(struct compat_iovec) == transferred) {
1698 struct compat_iovec *ciov = src;
1702 * With this interface a 32bit server cannot support
1703 * non-compat (i.e. ones coming from 64bit apps) ioctl
1709 for (i = 0; i < count; i++) {
1710 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1711 dst[i].iov_len = ciov[i].iov_len;
1717 if (count * sizeof(struct iovec) != transferred)
1720 memcpy(dst, src, transferred);
1724 /* Make sure iov_length() won't overflow */
1725 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1728 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1730 for (n = 0; n < count; n++) {
1731 if (iov->iov_len > (size_t) max)
1733 max -= iov->iov_len;
1738 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1739 void *src, size_t transferred, unsigned count,
1743 struct fuse_ioctl_iovec *fiov = src;
1745 if (fc->minor < 16) {
1746 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1750 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1753 for (i = 0; i < count; i++) {
1754 /* Did the server supply an inappropriate value? */
1755 if (fiov[i].base != (unsigned long) fiov[i].base ||
1756 fiov[i].len != (unsigned long) fiov[i].len)
1759 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1760 dst[i].iov_len = (size_t) fiov[i].len;
1762 #ifdef CONFIG_COMPAT
1764 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1765 (compat_size_t) dst[i].iov_len != fiov[i].len))
1775 * For ioctls, there is no generic way to determine how much memory
1776 * needs to be read and/or written. Furthermore, ioctls are allowed
1777 * to dereference the passed pointer, so the parameter requires deep
1778 * copying but FUSE has no idea whatsoever about what to copy in or
1781 * This is solved by allowing FUSE server to retry ioctl with
1782 * necessary in/out iovecs. Let's assume the ioctl implementation
1783 * needs to read in the following structure.
1790 * On the first callout to FUSE server, inarg->in_size and
1791 * inarg->out_size will be NULL; then, the server completes the ioctl
1792 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1793 * the actual iov array to
1795 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1797 * which tells FUSE to copy in the requested area and retry the ioctl.
1798 * On the second round, the server has access to the structure and
1799 * from that it can tell what to look for next, so on the invocation,
1800 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1802 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1803 * { .iov_base = a.buf, .iov_len = a.buflen } }
1805 * FUSE will copy both struct a and the pointed buffer from the
1806 * process doing the ioctl and retry ioctl with both struct a and the
1809 * This time, FUSE server has everything it needs and completes ioctl
1810 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1812 * Copying data out works the same way.
1814 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1815 * automatically initializes in and out iovs by decoding @cmd with
1816 * _IOC_* macros and the server is not allowed to request RETRY. This
1817 * limits ioctl data transfers to well-formed ioctls and is the forced
1818 * behavior for all FUSE servers.
1820 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1823 struct fuse_file *ff = file->private_data;
1824 struct fuse_conn *fc = ff->fc;
1825 struct fuse_ioctl_in inarg = {
1831 struct fuse_ioctl_out outarg;
1832 struct fuse_req *req = NULL;
1833 struct page **pages = NULL;
1834 struct iovec *iov_page = NULL;
1835 struct iovec *in_iov = NULL, *out_iov = NULL;
1836 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1837 size_t in_size, out_size, transferred;
1840 #if BITS_PER_LONG == 32
1841 inarg.flags |= FUSE_IOCTL_32BIT;
1843 if (flags & FUSE_IOCTL_COMPAT)
1844 inarg.flags |= FUSE_IOCTL_32BIT;
1847 /* assume all the iovs returned by client always fits in a page */
1848 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1851 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1852 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
1853 if (!pages || !iov_page)
1857 * If restricted, initialize IO parameters as encoded in @cmd.
1858 * RETRY from server is not allowed.
1860 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1861 struct iovec *iov = iov_page;
1863 iov->iov_base = (void __user *)arg;
1864 iov->iov_len = _IOC_SIZE(cmd);
1866 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1871 if (_IOC_DIR(cmd) & _IOC_READ) {
1878 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1879 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1882 * Out data can be used either for actual out data or iovs,
1883 * make sure there always is at least one page.
1885 out_size = max_t(size_t, out_size, PAGE_SIZE);
1886 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1888 /* make sure there are enough buffer pages and init request with them */
1890 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1892 while (num_pages < max_pages) {
1893 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1894 if (!pages[num_pages])
1899 req = fuse_get_req(fc);
1905 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1906 req->num_pages = num_pages;
1908 /* okay, let's send it to the client */
1909 req->in.h.opcode = FUSE_IOCTL;
1910 req->in.h.nodeid = ff->nodeid;
1911 req->in.numargs = 1;
1912 req->in.args[0].size = sizeof(inarg);
1913 req->in.args[0].value = &inarg;
1916 req->in.args[1].size = in_size;
1917 req->in.argpages = 1;
1919 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1925 req->out.numargs = 2;
1926 req->out.args[0].size = sizeof(outarg);
1927 req->out.args[0].value = &outarg;
1928 req->out.args[1].size = out_size;
1929 req->out.argpages = 1;
1930 req->out.argvar = 1;
1932 fuse_request_send(fc, req);
1933 err = req->out.h.error;
1934 transferred = req->out.args[1].size;
1935 fuse_put_request(fc, req);
1940 /* did it ask for retry? */
1941 if (outarg.flags & FUSE_IOCTL_RETRY) {
1944 /* no retry if in restricted mode */
1946 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1949 in_iovs = outarg.in_iovs;
1950 out_iovs = outarg.out_iovs;
1953 * Make sure things are in boundary, separate checks
1954 * are to protect against overflow.
1957 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1958 out_iovs > FUSE_IOCTL_MAX_IOV ||
1959 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1962 vaddr = kmap_atomic(pages[0], KM_USER0);
1963 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
1964 transferred, in_iovs + out_iovs,
1965 (flags & FUSE_IOCTL_COMPAT) != 0);
1966 kunmap_atomic(vaddr, KM_USER0);
1971 out_iov = in_iov + in_iovs;
1973 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1977 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1985 if (transferred > inarg.out_size)
1988 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1991 fuse_put_request(fc, req);
1992 free_page((unsigned long) iov_page);
1994 __free_page(pages[--num_pages]);
1997 return err ? err : outarg.result;
1999 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2001 static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
2002 unsigned long arg, unsigned int flags)
2004 struct inode *inode = file->f_dentry->d_inode;
2005 struct fuse_conn *fc = get_fuse_conn(inode);
2007 if (!fuse_allow_task(fc, current))
2010 if (is_bad_inode(inode))
2013 return fuse_do_ioctl(file, cmd, arg, flags);
2016 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2019 return fuse_file_ioctl_common(file, cmd, arg, 0);
2022 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2025 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2029 * All files which have been polled are linked to RB tree
2030 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2031 * find the matching one.
2033 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2034 struct rb_node **parent_out)
2036 struct rb_node **link = &fc->polled_files.rb_node;
2037 struct rb_node *last = NULL;
2040 struct fuse_file *ff;
2043 ff = rb_entry(last, struct fuse_file, polled_node);
2046 link = &last->rb_left;
2047 else if (kh > ff->kh)
2048 link = &last->rb_right;
2059 * The file is about to be polled. Make sure it's on the polled_files
2060 * RB tree. Note that files once added to the polled_files tree are
2061 * not removed before the file is released. This is because a file
2062 * polled once is likely to be polled again.
2064 static void fuse_register_polled_file(struct fuse_conn *fc,
2065 struct fuse_file *ff)
2067 spin_lock(&fc->lock);
2068 if (RB_EMPTY_NODE(&ff->polled_node)) {
2069 struct rb_node **link, *parent;
2071 link = fuse_find_polled_node(fc, ff->kh, &parent);
2073 rb_link_node(&ff->polled_node, parent, link);
2074 rb_insert_color(&ff->polled_node, &fc->polled_files);
2076 spin_unlock(&fc->lock);
2079 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2081 struct fuse_file *ff = file->private_data;
2082 struct fuse_conn *fc = ff->fc;
2083 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2084 struct fuse_poll_out outarg;
2085 struct fuse_req *req;
2089 return DEFAULT_POLLMASK;
2091 poll_wait(file, &ff->poll_wait, wait);
2094 * Ask for notification iff there's someone waiting for it.
2095 * The client may ignore the flag and always notify.
2097 if (waitqueue_active(&ff->poll_wait)) {
2098 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2099 fuse_register_polled_file(fc, ff);
2102 req = fuse_get_req(fc);
2106 req->in.h.opcode = FUSE_POLL;
2107 req->in.h.nodeid = ff->nodeid;
2108 req->in.numargs = 1;
2109 req->in.args[0].size = sizeof(inarg);
2110 req->in.args[0].value = &inarg;
2111 req->out.numargs = 1;
2112 req->out.args[0].size = sizeof(outarg);
2113 req->out.args[0].value = &outarg;
2114 fuse_request_send(fc, req);
2115 err = req->out.h.error;
2116 fuse_put_request(fc, req);
2119 return outarg.revents;
2120 if (err == -ENOSYS) {
2122 return DEFAULT_POLLMASK;
2126 EXPORT_SYMBOL_GPL(fuse_file_poll);
2129 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2130 * wakes up the poll waiters.
2132 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2133 struct fuse_notify_poll_wakeup_out *outarg)
2135 u64 kh = outarg->kh;
2136 struct rb_node **link;
2138 spin_lock(&fc->lock);
2140 link = fuse_find_polled_node(fc, kh, NULL);
2142 struct fuse_file *ff;
2144 ff = rb_entry(*link, struct fuse_file, polled_node);
2145 wake_up_interruptible_sync(&ff->poll_wait);
2148 spin_unlock(&fc->lock);
2152 static const struct file_operations fuse_file_operations = {
2153 .llseek = fuse_file_llseek,
2154 .read = do_sync_read,
2155 .aio_read = fuse_file_aio_read,
2156 .write = do_sync_write,
2157 .aio_write = fuse_file_aio_write,
2158 .mmap = fuse_file_mmap,
2160 .flush = fuse_flush,
2161 .release = fuse_release,
2162 .fsync = fuse_fsync,
2163 .lock = fuse_file_lock,
2164 .flock = fuse_file_flock,
2165 .splice_read = generic_file_splice_read,
2166 .unlocked_ioctl = fuse_file_ioctl,
2167 .compat_ioctl = fuse_file_compat_ioctl,
2168 .poll = fuse_file_poll,
2171 static const struct file_operations fuse_direct_io_file_operations = {
2172 .llseek = fuse_file_llseek,
2173 .read = fuse_direct_read,
2174 .write = fuse_direct_write,
2175 .mmap = fuse_direct_mmap,
2177 .flush = fuse_flush,
2178 .release = fuse_release,
2179 .fsync = fuse_fsync,
2180 .lock = fuse_file_lock,
2181 .flock = fuse_file_flock,
2182 .unlocked_ioctl = fuse_file_ioctl,
2183 .compat_ioctl = fuse_file_compat_ioctl,
2184 .poll = fuse_file_poll,
2185 /* no splice_read */
2188 static const struct address_space_operations fuse_file_aops = {
2189 .readpage = fuse_readpage,
2190 .writepage = fuse_writepage,
2191 .launder_page = fuse_launder_page,
2192 .write_begin = fuse_write_begin,
2193 .write_end = fuse_write_end,
2194 .readpages = fuse_readpages,
2195 .set_page_dirty = __set_page_dirty_nobuffers,
2199 void fuse_init_file_inode(struct inode *inode)
2201 inode->i_fop = &fuse_file_operations;
2202 inode->i_data.a_ops = &fuse_file_aops;