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>
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_sync(&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, struct dentry *de, int datasync,
406 struct inode *inode = de->d_inode;
407 struct fuse_conn *fc = get_fuse_conn(inode);
408 struct fuse_file *ff = file->private_data;
409 struct fuse_req *req;
410 struct fuse_fsync_in inarg;
413 if (is_bad_inode(inode))
416 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
420 * Start writeback against all dirty pages of the inode, then
421 * wait for all outstanding writes, before sending the FSYNC
424 err = write_inode_now(inode, 0);
428 fuse_sync_writes(inode);
430 req = fuse_get_req(fc);
434 memset(&inarg, 0, sizeof(inarg));
436 inarg.fsync_flags = datasync ? 1 : 0;
437 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
438 req->in.h.nodeid = get_node_id(inode);
440 req->in.args[0].size = sizeof(inarg);
441 req->in.args[0].value = &inarg;
442 fuse_request_send(fc, req);
443 err = req->out.h.error;
444 fuse_put_request(fc, req);
445 if (err == -ENOSYS) {
455 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
457 return fuse_fsync_common(file, de, datasync, 0);
460 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
461 size_t count, int opcode)
463 struct fuse_read_in *inarg = &req->misc.read.in;
464 struct fuse_file *ff = file->private_data;
469 inarg->flags = file->f_flags;
470 req->in.h.opcode = opcode;
471 req->in.h.nodeid = ff->nodeid;
473 req->in.args[0].size = sizeof(struct fuse_read_in);
474 req->in.args[0].value = inarg;
476 req->out.numargs = 1;
477 req->out.args[0].size = count;
480 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
481 loff_t pos, size_t count, fl_owner_t owner)
483 struct fuse_file *ff = file->private_data;
484 struct fuse_conn *fc = ff->fc;
486 fuse_read_fill(req, file, pos, count, FUSE_READ);
488 struct fuse_read_in *inarg = &req->misc.read.in;
490 inarg->read_flags |= FUSE_READ_LOCKOWNER;
491 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
493 fuse_request_send(fc, req);
494 return req->out.args[0].size;
497 static void fuse_read_update_size(struct inode *inode, loff_t size,
500 struct fuse_conn *fc = get_fuse_conn(inode);
501 struct fuse_inode *fi = get_fuse_inode(inode);
503 spin_lock(&fc->lock);
504 if (attr_ver == fi->attr_version && size < inode->i_size) {
505 fi->attr_version = ++fc->attr_version;
506 i_size_write(inode, size);
508 spin_unlock(&fc->lock);
511 static int fuse_readpage(struct file *file, struct page *page)
513 struct inode *inode = page->mapping->host;
514 struct fuse_conn *fc = get_fuse_conn(inode);
515 struct fuse_req *req;
517 loff_t pos = page_offset(page);
518 size_t count = PAGE_CACHE_SIZE;
523 if (is_bad_inode(inode))
527 * Page writeback can extend beyond the liftime of the
528 * page-cache page, so make sure we read a properly synced
531 fuse_wait_on_page_writeback(inode, page->index);
533 req = fuse_get_req(fc);
538 attr_ver = fuse_get_attr_version(fc);
540 req->out.page_zeroing = 1;
541 req->out.argpages = 1;
543 req->pages[0] = page;
544 num_read = fuse_send_read(req, file, pos, count, NULL);
545 err = req->out.h.error;
546 fuse_put_request(fc, req);
550 * Short read means EOF. If file size is larger, truncate it
552 if (num_read < count)
553 fuse_read_update_size(inode, pos + num_read, attr_ver);
555 SetPageUptodate(page);
558 fuse_invalidate_attr(inode); /* atime changed */
564 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
567 size_t count = req->misc.read.in.size;
568 size_t num_read = req->out.args[0].size;
569 struct inode *inode = req->pages[0]->mapping->host;
572 * Short read means EOF. If file size is larger, truncate it
574 if (!req->out.h.error && num_read < count) {
575 loff_t pos = page_offset(req->pages[0]) + num_read;
576 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
579 fuse_invalidate_attr(inode); /* atime changed */
581 for (i = 0; i < req->num_pages; i++) {
582 struct page *page = req->pages[i];
583 if (!req->out.h.error)
584 SetPageUptodate(page);
590 fuse_file_put(req->ff, false);
593 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
595 struct fuse_file *ff = file->private_data;
596 struct fuse_conn *fc = ff->fc;
597 loff_t pos = page_offset(req->pages[0]);
598 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
600 req->out.argpages = 1;
601 req->out.page_zeroing = 1;
602 fuse_read_fill(req, file, pos, count, FUSE_READ);
603 req->misc.read.attr_ver = fuse_get_attr_version(fc);
604 if (fc->async_read) {
605 req->ff = fuse_file_get(ff);
606 req->end = fuse_readpages_end;
607 fuse_request_send_background(fc, req);
609 fuse_request_send(fc, req);
610 fuse_readpages_end(fc, req);
611 fuse_put_request(fc, req);
615 struct fuse_fill_data {
616 struct fuse_req *req;
621 static int fuse_readpages_fill(void *_data, struct page *page)
623 struct fuse_fill_data *data = _data;
624 struct fuse_req *req = data->req;
625 struct inode *inode = data->inode;
626 struct fuse_conn *fc = get_fuse_conn(inode);
628 fuse_wait_on_page_writeback(inode, page->index);
630 if (req->num_pages &&
631 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
632 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
633 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
634 fuse_send_readpages(req, data->file);
635 data->req = req = fuse_get_req(fc);
641 req->pages[req->num_pages] = page;
646 static int fuse_readpages(struct file *file, struct address_space *mapping,
647 struct list_head *pages, unsigned nr_pages)
649 struct inode *inode = mapping->host;
650 struct fuse_conn *fc = get_fuse_conn(inode);
651 struct fuse_fill_data data;
655 if (is_bad_inode(inode))
660 data.req = fuse_get_req(fc);
661 err = PTR_ERR(data.req);
662 if (IS_ERR(data.req))
665 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
667 if (data.req->num_pages)
668 fuse_send_readpages(data.req, file);
670 fuse_put_request(fc, data.req);
676 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
677 unsigned long nr_segs, loff_t pos)
679 struct inode *inode = iocb->ki_filp->f_mapping->host;
681 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
684 * If trying to read past EOF, make sure the i_size
685 * attribute is up-to-date.
687 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
692 return generic_file_aio_read(iocb, iov, nr_segs, pos);
695 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
696 loff_t pos, size_t count)
698 struct fuse_write_in *inarg = &req->misc.write.in;
699 struct fuse_write_out *outarg = &req->misc.write.out;
704 req->in.h.opcode = FUSE_WRITE;
705 req->in.h.nodeid = ff->nodeid;
707 if (ff->fc->minor < 9)
708 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
710 req->in.args[0].size = sizeof(struct fuse_write_in);
711 req->in.args[0].value = inarg;
712 req->in.args[1].size = count;
713 req->out.numargs = 1;
714 req->out.args[0].size = sizeof(struct fuse_write_out);
715 req->out.args[0].value = outarg;
718 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
719 loff_t pos, size_t count, fl_owner_t owner)
721 struct fuse_file *ff = file->private_data;
722 struct fuse_conn *fc = ff->fc;
723 struct fuse_write_in *inarg = &req->misc.write.in;
725 fuse_write_fill(req, ff, pos, count);
726 inarg->flags = file->f_flags;
728 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
729 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
731 fuse_request_send(fc, req);
732 return req->misc.write.out.size;
735 static int fuse_write_begin(struct file *file, struct address_space *mapping,
736 loff_t pos, unsigned len, unsigned flags,
737 struct page **pagep, void **fsdata)
739 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
741 *pagep = grab_cache_page_write_begin(mapping, index, flags);
747 static void fuse_write_update_size(struct inode *inode, loff_t pos)
749 struct fuse_conn *fc = get_fuse_conn(inode);
750 struct fuse_inode *fi = get_fuse_inode(inode);
752 spin_lock(&fc->lock);
753 fi->attr_version = ++fc->attr_version;
754 if (pos > inode->i_size)
755 i_size_write(inode, pos);
756 spin_unlock(&fc->lock);
759 static int fuse_buffered_write(struct file *file, struct inode *inode,
760 loff_t pos, unsigned count, struct page *page)
764 struct fuse_conn *fc = get_fuse_conn(inode);
765 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
766 struct fuse_req *req;
768 if (is_bad_inode(inode))
772 * Make sure writepages on the same page are not mixed up with
775 fuse_wait_on_page_writeback(inode, page->index);
777 req = fuse_get_req(fc);
781 req->in.argpages = 1;
783 req->pages[0] = page;
784 req->page_offset = offset;
785 nres = fuse_send_write(req, file, pos, count, NULL);
786 err = req->out.h.error;
787 fuse_put_request(fc, req);
792 fuse_write_update_size(inode, pos);
793 if (count == PAGE_CACHE_SIZE)
794 SetPageUptodate(page);
796 fuse_invalidate_attr(inode);
797 return err ? err : nres;
800 static int fuse_write_end(struct file *file, struct address_space *mapping,
801 loff_t pos, unsigned len, unsigned copied,
802 struct page *page, void *fsdata)
804 struct inode *inode = mapping->host;
808 res = fuse_buffered_write(file, inode, pos, copied, page);
811 page_cache_release(page);
815 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
816 struct inode *inode, loff_t pos,
823 for (i = 0; i < req->num_pages; i++)
824 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
826 res = fuse_send_write(req, file, pos, count, NULL);
828 offset = req->page_offset;
830 for (i = 0; i < req->num_pages; i++) {
831 struct page *page = req->pages[i];
833 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
834 SetPageUptodate(page);
836 if (count > PAGE_CACHE_SIZE - offset)
837 count -= PAGE_CACHE_SIZE - offset;
843 page_cache_release(page);
849 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
850 struct address_space *mapping,
851 struct iov_iter *ii, loff_t pos)
853 struct fuse_conn *fc = get_fuse_conn(mapping->host);
854 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
858 req->in.argpages = 1;
859 req->page_offset = offset;
864 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
865 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
868 bytes = min_t(size_t, bytes, fc->max_write - count);
872 if (iov_iter_fault_in_readable(ii, bytes))
876 page = grab_cache_page_write_begin(mapping, index, 0);
880 if (mapping_writably_mapped(mapping))
881 flush_dcache_page(page);
884 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
886 flush_dcache_page(page);
890 page_cache_release(page);
891 bytes = min(bytes, iov_iter_single_seg_count(ii));
896 req->pages[req->num_pages] = page;
899 iov_iter_advance(ii, tmp);
903 if (offset == PAGE_CACHE_SIZE)
908 } while (iov_iter_count(ii) && count < fc->max_write &&
909 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
911 return count > 0 ? count : err;
914 static ssize_t fuse_perform_write(struct file *file,
915 struct address_space *mapping,
916 struct iov_iter *ii, loff_t pos)
918 struct inode *inode = mapping->host;
919 struct fuse_conn *fc = get_fuse_conn(inode);
923 if (is_bad_inode(inode))
927 struct fuse_req *req;
930 req = fuse_get_req(fc);
936 count = fuse_fill_write_pages(req, mapping, ii, pos);
942 num_written = fuse_send_write_pages(req, file, inode,
944 err = req->out.h.error;
949 /* break out of the loop on short write */
950 if (num_written != count)
954 fuse_put_request(fc, req);
955 } while (!err && iov_iter_count(ii));
958 fuse_write_update_size(inode, pos);
960 fuse_invalidate_attr(inode);
962 return res > 0 ? res : err;
965 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
966 unsigned long nr_segs, loff_t pos)
968 struct file *file = iocb->ki_filp;
969 struct address_space *mapping = file->f_mapping;
972 struct inode *inode = mapping->host;
976 WARN_ON(iocb->ki_pos != pos);
978 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
982 mutex_lock(&inode->i_mutex);
983 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
985 /* We can write back this queue in page reclaim */
986 current->backing_dev_info = mapping->backing_dev_info;
988 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
995 err = file_remove_suid(file);
999 file_update_time(file);
1001 iov_iter_init(&i, iov, nr_segs, count, 0);
1002 written = fuse_perform_write(file, mapping, &i, pos);
1004 iocb->ki_pos = pos + written;
1007 current->backing_dev_info = NULL;
1008 mutex_unlock(&inode->i_mutex);
1010 return written ? written : err;
1013 static void fuse_release_user_pages(struct fuse_req *req, int write)
1017 for (i = 0; i < req->num_pages; i++) {
1018 struct page *page = req->pages[i];
1020 set_page_dirty_lock(page);
1025 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
1026 size_t *nbytesp, int write)
1028 size_t nbytes = *nbytesp;
1029 unsigned long user_addr = (unsigned long) buf;
1030 unsigned offset = user_addr & ~PAGE_MASK;
1033 /* Special case for kernel I/O: can copy directly into the buffer */
1034 if (segment_eq(get_fs(), KERNEL_DS)) {
1036 req->in.args[1].value = (void *) user_addr;
1038 req->out.args[0].value = (void *) user_addr;
1043 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
1044 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1045 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
1046 down_read(¤t->mm->mmap_sem);
1047 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
1048 0, req->pages, NULL);
1049 up_read(¤t->mm->mmap_sem);
1053 req->num_pages = npages;
1054 req->page_offset = offset;
1057 req->in.argpages = 1;
1059 req->out.argpages = 1;
1061 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
1062 *nbytesp = min(*nbytesp, nbytes);
1067 ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1068 size_t count, loff_t *ppos, int write)
1070 struct fuse_file *ff = file->private_data;
1071 struct fuse_conn *fc = ff->fc;
1072 size_t nmax = write ? fc->max_write : fc->max_read;
1075 struct fuse_req *req;
1077 req = fuse_get_req(fc);
1079 return PTR_ERR(req);
1083 fl_owner_t owner = current->files;
1084 size_t nbytes = min(count, nmax);
1085 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1092 nres = fuse_send_write(req, file, pos, nbytes, owner);
1094 nres = fuse_send_read(req, file, pos, nbytes, owner);
1096 fuse_release_user_pages(req, !write);
1097 if (req->out.h.error) {
1099 res = req->out.h.error;
1101 } else if (nres > nbytes) {
1112 fuse_put_request(fc, req);
1113 req = fuse_get_req(fc);
1119 fuse_put_request(fc, req);
1125 EXPORT_SYMBOL_GPL(fuse_direct_io);
1127 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1128 size_t count, loff_t *ppos)
1131 struct inode *inode = file->f_path.dentry->d_inode;
1133 if (is_bad_inode(inode))
1136 res = fuse_direct_io(file, buf, count, ppos, 0);
1138 fuse_invalidate_attr(inode);
1143 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1144 size_t count, loff_t *ppos)
1146 struct inode *inode = file->f_path.dentry->d_inode;
1149 if (is_bad_inode(inode))
1152 /* Don't allow parallel writes to the same file */
1153 mutex_lock(&inode->i_mutex);
1154 res = generic_write_checks(file, ppos, &count, 0);
1156 res = fuse_direct_io(file, buf, count, ppos, 1);
1158 fuse_write_update_size(inode, *ppos);
1160 mutex_unlock(&inode->i_mutex);
1162 fuse_invalidate_attr(inode);
1167 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1169 __free_page(req->pages[0]);
1170 fuse_file_put(req->ff, false);
1173 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1175 struct inode *inode = req->inode;
1176 struct fuse_inode *fi = get_fuse_inode(inode);
1177 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1179 list_del(&req->writepages_entry);
1180 dec_bdi_stat(bdi, BDI_WRITEBACK);
1181 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1182 bdi_writeout_inc(bdi);
1183 wake_up(&fi->page_waitq);
1186 /* Called under fc->lock, may release and reacquire it */
1187 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1188 __releases(&fc->lock)
1189 __acquires(&fc->lock)
1191 struct fuse_inode *fi = get_fuse_inode(req->inode);
1192 loff_t size = i_size_read(req->inode);
1193 struct fuse_write_in *inarg = &req->misc.write.in;
1198 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1199 inarg->size = PAGE_CACHE_SIZE;
1200 } else if (inarg->offset < size) {
1201 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1203 /* Got truncated off completely */
1207 req->in.args[1].size = inarg->size;
1209 fuse_request_send_background_locked(fc, req);
1213 fuse_writepage_finish(fc, req);
1214 spin_unlock(&fc->lock);
1215 fuse_writepage_free(fc, req);
1216 fuse_put_request(fc, req);
1217 spin_lock(&fc->lock);
1221 * If fi->writectr is positive (no truncate or fsync going on) send
1222 * all queued writepage requests.
1224 * Called with fc->lock
1226 void fuse_flush_writepages(struct inode *inode)
1227 __releases(&fc->lock)
1228 __acquires(&fc->lock)
1230 struct fuse_conn *fc = get_fuse_conn(inode);
1231 struct fuse_inode *fi = get_fuse_inode(inode);
1232 struct fuse_req *req;
1234 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1235 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1236 list_del_init(&req->list);
1237 fuse_send_writepage(fc, req);
1241 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1243 struct inode *inode = req->inode;
1244 struct fuse_inode *fi = get_fuse_inode(inode);
1246 mapping_set_error(inode->i_mapping, req->out.h.error);
1247 spin_lock(&fc->lock);
1249 fuse_writepage_finish(fc, req);
1250 spin_unlock(&fc->lock);
1251 fuse_writepage_free(fc, req);
1254 static int fuse_writepage_locked(struct page *page)
1256 struct address_space *mapping = page->mapping;
1257 struct inode *inode = mapping->host;
1258 struct fuse_conn *fc = get_fuse_conn(inode);
1259 struct fuse_inode *fi = get_fuse_inode(inode);
1260 struct fuse_req *req;
1261 struct fuse_file *ff;
1262 struct page *tmp_page;
1264 set_page_writeback(page);
1266 req = fuse_request_alloc_nofs();
1270 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1274 spin_lock(&fc->lock);
1275 BUG_ON(list_empty(&fi->write_files));
1276 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1277 req->ff = fuse_file_get(ff);
1278 spin_unlock(&fc->lock);
1280 fuse_write_fill(req, ff, page_offset(page), 0);
1282 copy_highpage(tmp_page, page);
1283 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1284 req->in.argpages = 1;
1286 req->pages[0] = tmp_page;
1287 req->page_offset = 0;
1288 req->end = fuse_writepage_end;
1291 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1292 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1293 end_page_writeback(page);
1295 spin_lock(&fc->lock);
1296 list_add(&req->writepages_entry, &fi->writepages);
1297 list_add_tail(&req->list, &fi->queued_writes);
1298 fuse_flush_writepages(inode);
1299 spin_unlock(&fc->lock);
1304 fuse_request_free(req);
1306 end_page_writeback(page);
1310 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1314 err = fuse_writepage_locked(page);
1320 static int fuse_launder_page(struct page *page)
1323 if (clear_page_dirty_for_io(page)) {
1324 struct inode *inode = page->mapping->host;
1325 err = fuse_writepage_locked(page);
1327 fuse_wait_on_page_writeback(inode, page->index);
1333 * Write back dirty pages now, because there may not be any suitable
1336 static void fuse_vma_close(struct vm_area_struct *vma)
1338 filemap_write_and_wait(vma->vm_file->f_mapping);
1342 * Wait for writeback against this page to complete before allowing it
1343 * to be marked dirty again, and hence written back again, possibly
1344 * before the previous writepage completed.
1346 * Block here, instead of in ->writepage(), so that the userspace fs
1347 * can only block processes actually operating on the filesystem.
1349 * Otherwise unprivileged userspace fs would be able to block
1354 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1356 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1358 struct page *page = vmf->page;
1360 * Don't use page->mapping as it may become NULL from a
1361 * concurrent truncate.
1363 struct inode *inode = vma->vm_file->f_mapping->host;
1365 fuse_wait_on_page_writeback(inode, page->index);
1369 static const struct vm_operations_struct fuse_file_vm_ops = {
1370 .close = fuse_vma_close,
1371 .fault = filemap_fault,
1372 .page_mkwrite = fuse_page_mkwrite,
1375 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1377 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1378 struct inode *inode = file->f_dentry->d_inode;
1379 struct fuse_conn *fc = get_fuse_conn(inode);
1380 struct fuse_inode *fi = get_fuse_inode(inode);
1381 struct fuse_file *ff = file->private_data;
1383 * file may be written through mmap, so chain it onto the
1384 * inodes's write_file list
1386 spin_lock(&fc->lock);
1387 if (list_empty(&ff->write_entry))
1388 list_add(&ff->write_entry, &fi->write_files);
1389 spin_unlock(&fc->lock);
1391 file_accessed(file);
1392 vma->vm_ops = &fuse_file_vm_ops;
1396 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1398 /* Can't provide the coherency needed for MAP_SHARED */
1399 if (vma->vm_flags & VM_MAYSHARE)
1402 invalidate_inode_pages2(file->f_mapping);
1404 return generic_file_mmap(file, vma);
1407 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1408 struct file_lock *fl)
1410 switch (ffl->type) {
1416 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1417 ffl->end < ffl->start)
1420 fl->fl_start = ffl->start;
1421 fl->fl_end = ffl->end;
1422 fl->fl_pid = ffl->pid;
1428 fl->fl_type = ffl->type;
1432 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1433 const struct file_lock *fl, int opcode, pid_t pid,
1436 struct inode *inode = file->f_path.dentry->d_inode;
1437 struct fuse_conn *fc = get_fuse_conn(inode);
1438 struct fuse_file *ff = file->private_data;
1439 struct fuse_lk_in *arg = &req->misc.lk_in;
1442 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1443 arg->lk.start = fl->fl_start;
1444 arg->lk.end = fl->fl_end;
1445 arg->lk.type = fl->fl_type;
1448 arg->lk_flags |= FUSE_LK_FLOCK;
1449 req->in.h.opcode = opcode;
1450 req->in.h.nodeid = get_node_id(inode);
1451 req->in.numargs = 1;
1452 req->in.args[0].size = sizeof(*arg);
1453 req->in.args[0].value = arg;
1456 static int fuse_getlk(struct file *file, struct file_lock *fl)
1458 struct inode *inode = file->f_path.dentry->d_inode;
1459 struct fuse_conn *fc = get_fuse_conn(inode);
1460 struct fuse_req *req;
1461 struct fuse_lk_out outarg;
1464 req = fuse_get_req(fc);
1466 return PTR_ERR(req);
1468 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1469 req->out.numargs = 1;
1470 req->out.args[0].size = sizeof(outarg);
1471 req->out.args[0].value = &outarg;
1472 fuse_request_send(fc, req);
1473 err = req->out.h.error;
1474 fuse_put_request(fc, req);
1476 err = convert_fuse_file_lock(&outarg.lk, fl);
1481 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1483 struct inode *inode = file->f_path.dentry->d_inode;
1484 struct fuse_conn *fc = get_fuse_conn(inode);
1485 struct fuse_req *req;
1486 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1487 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1490 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1491 /* NLM needs asynchronous locks, which we don't support yet */
1495 /* Unlock on close is handled by the flush method */
1496 if (fl->fl_flags & FL_CLOSE)
1499 req = fuse_get_req(fc);
1501 return PTR_ERR(req);
1503 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1504 fuse_request_send(fc, req);
1505 err = req->out.h.error;
1506 /* locking is restartable */
1509 fuse_put_request(fc, req);
1513 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1515 struct inode *inode = file->f_path.dentry->d_inode;
1516 struct fuse_conn *fc = get_fuse_conn(inode);
1519 if (cmd == F_CANCELLK) {
1521 } else if (cmd == F_GETLK) {
1523 posix_test_lock(file, fl);
1526 err = fuse_getlk(file, fl);
1529 err = posix_lock_file(file, fl, NULL);
1531 err = fuse_setlk(file, fl, 0);
1536 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1538 struct inode *inode = file->f_path.dentry->d_inode;
1539 struct fuse_conn *fc = get_fuse_conn(inode);
1543 err = flock_lock_file_wait(file, fl);
1545 /* emulate flock with POSIX locks */
1546 fl->fl_owner = (fl_owner_t) file;
1547 err = fuse_setlk(file, fl, 1);
1553 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1555 struct inode *inode = mapping->host;
1556 struct fuse_conn *fc = get_fuse_conn(inode);
1557 struct fuse_req *req;
1558 struct fuse_bmap_in inarg;
1559 struct fuse_bmap_out outarg;
1562 if (!inode->i_sb->s_bdev || fc->no_bmap)
1565 req = fuse_get_req(fc);
1569 memset(&inarg, 0, sizeof(inarg));
1570 inarg.block = block;
1571 inarg.blocksize = inode->i_sb->s_blocksize;
1572 req->in.h.opcode = FUSE_BMAP;
1573 req->in.h.nodeid = get_node_id(inode);
1574 req->in.numargs = 1;
1575 req->in.args[0].size = sizeof(inarg);
1576 req->in.args[0].value = &inarg;
1577 req->out.numargs = 1;
1578 req->out.args[0].size = sizeof(outarg);
1579 req->out.args[0].value = &outarg;
1580 fuse_request_send(fc, req);
1581 err = req->out.h.error;
1582 fuse_put_request(fc, req);
1586 return err ? 0 : outarg.block;
1589 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1592 struct inode *inode = file->f_path.dentry->d_inode;
1594 mutex_lock(&inode->i_mutex);
1597 retval = fuse_update_attributes(inode, NULL, file, NULL);
1600 offset += i_size_read(inode);
1603 offset += file->f_pos;
1606 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1607 if (offset != file->f_pos) {
1608 file->f_pos = offset;
1609 file->f_version = 0;
1614 mutex_unlock(&inode->i_mutex);
1618 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1619 unsigned int nr_segs, size_t bytes, bool to_user)
1627 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1629 while (iov_iter_count(&ii)) {
1630 struct page *page = pages[page_idx++];
1631 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1634 kaddr = map = kmap(page);
1637 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1638 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1639 size_t copy = min(todo, iov_len);
1643 left = copy_from_user(kaddr, uaddr, copy);
1645 left = copy_to_user(uaddr, kaddr, copy);
1650 iov_iter_advance(&ii, copy);
1661 /* Make sure iov_length() won't overflow */
1662 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1665 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1667 for (n = 0; n < count; n++) {
1668 if (iov->iov_len > (size_t) max)
1670 max -= iov->iov_len;
1676 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1677 * ABI was defined to be 'struct iovec' which is different on 32bit
1678 * and 64bit. Fortunately we can determine which structure the server
1679 * used from the size of the reply.
1681 static int fuse_copy_ioctl_iovec(struct iovec *dst, void *src,
1682 size_t transferred, unsigned count,
1685 #ifdef CONFIG_COMPAT
1686 if (count * sizeof(struct compat_iovec) == transferred) {
1687 struct compat_iovec *ciov = src;
1691 * With this interface a 32bit server cannot support
1692 * non-compat (i.e. ones coming from 64bit apps) ioctl
1698 for (i = 0; i < count; i++) {
1699 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1700 dst[i].iov_len = ciov[i].iov_len;
1706 if (count * sizeof(struct iovec) != transferred)
1709 memcpy(dst, src, transferred);
1714 * For ioctls, there is no generic way to determine how much memory
1715 * needs to be read and/or written. Furthermore, ioctls are allowed
1716 * to dereference the passed pointer, so the parameter requires deep
1717 * copying but FUSE has no idea whatsoever about what to copy in or
1720 * This is solved by allowing FUSE server to retry ioctl with
1721 * necessary in/out iovecs. Let's assume the ioctl implementation
1722 * needs to read in the following structure.
1729 * On the first callout to FUSE server, inarg->in_size and
1730 * inarg->out_size will be NULL; then, the server completes the ioctl
1731 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1732 * the actual iov array to
1734 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1736 * which tells FUSE to copy in the requested area and retry the ioctl.
1737 * On the second round, the server has access to the structure and
1738 * from that it can tell what to look for next, so on the invocation,
1739 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1741 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1742 * { .iov_base = a.buf, .iov_len = a.buflen } }
1744 * FUSE will copy both struct a and the pointed buffer from the
1745 * process doing the ioctl and retry ioctl with both struct a and the
1748 * This time, FUSE server has everything it needs and completes ioctl
1749 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1751 * Copying data out works the same way.
1753 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1754 * automatically initializes in and out iovs by decoding @cmd with
1755 * _IOC_* macros and the server is not allowed to request RETRY. This
1756 * limits ioctl data transfers to well-formed ioctls and is the forced
1757 * behavior for all FUSE servers.
1759 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1762 struct fuse_file *ff = file->private_data;
1763 struct fuse_conn *fc = ff->fc;
1764 struct fuse_ioctl_in inarg = {
1770 struct fuse_ioctl_out outarg;
1771 struct fuse_req *req = NULL;
1772 struct page **pages = NULL;
1773 struct page *iov_page = NULL;
1774 struct iovec *in_iov = NULL, *out_iov = NULL;
1775 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1776 size_t in_size, out_size, transferred;
1779 /* assume all the iovs returned by client always fits in a page */
1780 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1783 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1784 iov_page = alloc_page(GFP_KERNEL);
1785 if (!pages || !iov_page)
1789 * If restricted, initialize IO parameters as encoded in @cmd.
1790 * RETRY from server is not allowed.
1792 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1793 struct iovec *iov = page_address(iov_page);
1795 iov->iov_base = (void __user *)arg;
1796 iov->iov_len = _IOC_SIZE(cmd);
1798 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1803 if (_IOC_DIR(cmd) & _IOC_READ) {
1810 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1811 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1814 * Out data can be used either for actual out data or iovs,
1815 * make sure there always is at least one page.
1817 out_size = max_t(size_t, out_size, PAGE_SIZE);
1818 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1820 /* make sure there are enough buffer pages and init request with them */
1822 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1824 while (num_pages < max_pages) {
1825 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1826 if (!pages[num_pages])
1831 req = fuse_get_req(fc);
1837 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1838 req->num_pages = num_pages;
1840 /* okay, let's send it to the client */
1841 req->in.h.opcode = FUSE_IOCTL;
1842 req->in.h.nodeid = ff->nodeid;
1843 req->in.numargs = 1;
1844 req->in.args[0].size = sizeof(inarg);
1845 req->in.args[0].value = &inarg;
1848 req->in.args[1].size = in_size;
1849 req->in.argpages = 1;
1851 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1857 req->out.numargs = 2;
1858 req->out.args[0].size = sizeof(outarg);
1859 req->out.args[0].value = &outarg;
1860 req->out.args[1].size = out_size;
1861 req->out.argpages = 1;
1862 req->out.argvar = 1;
1864 fuse_request_send(fc, req);
1865 err = req->out.h.error;
1866 transferred = req->out.args[1].size;
1867 fuse_put_request(fc, req);
1872 /* did it ask for retry? */
1873 if (outarg.flags & FUSE_IOCTL_RETRY) {
1876 /* no retry if in restricted mode */
1878 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1881 in_iovs = outarg.in_iovs;
1882 out_iovs = outarg.out_iovs;
1885 * Make sure things are in boundary, separate checks
1886 * are to protect against overflow.
1889 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1890 out_iovs > FUSE_IOCTL_MAX_IOV ||
1891 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1894 vaddr = kmap_atomic(pages[0], KM_USER0);
1895 err = fuse_copy_ioctl_iovec(page_address(iov_page), vaddr,
1896 transferred, in_iovs + out_iovs,
1897 (flags & FUSE_IOCTL_COMPAT) != 0);
1898 kunmap_atomic(vaddr, KM_USER0);
1902 in_iov = page_address(iov_page);
1903 out_iov = in_iov + in_iovs;
1905 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
1909 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
1917 if (transferred > inarg.out_size)
1920 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1923 fuse_put_request(fc, req);
1925 __free_page(iov_page);
1927 __free_page(pages[--num_pages]);
1930 return err ? err : outarg.result;
1932 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
1934 static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
1935 unsigned long arg, unsigned int flags)
1937 struct inode *inode = file->f_dentry->d_inode;
1938 struct fuse_conn *fc = get_fuse_conn(inode);
1940 if (!fuse_allow_task(fc, current))
1943 if (is_bad_inode(inode))
1946 return fuse_do_ioctl(file, cmd, arg, flags);
1949 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1952 return fuse_file_ioctl_common(file, cmd, arg, 0);
1955 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1958 return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
1962 * All files which have been polled are linked to RB tree
1963 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1964 * find the matching one.
1966 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1967 struct rb_node **parent_out)
1969 struct rb_node **link = &fc->polled_files.rb_node;
1970 struct rb_node *last = NULL;
1973 struct fuse_file *ff;
1976 ff = rb_entry(last, struct fuse_file, polled_node);
1979 link = &last->rb_left;
1980 else if (kh > ff->kh)
1981 link = &last->rb_right;
1992 * The file is about to be polled. Make sure it's on the polled_files
1993 * RB tree. Note that files once added to the polled_files tree are
1994 * not removed before the file is released. This is because a file
1995 * polled once is likely to be polled again.
1997 static void fuse_register_polled_file(struct fuse_conn *fc,
1998 struct fuse_file *ff)
2000 spin_lock(&fc->lock);
2001 if (RB_EMPTY_NODE(&ff->polled_node)) {
2002 struct rb_node **link, *parent;
2004 link = fuse_find_polled_node(fc, ff->kh, &parent);
2006 rb_link_node(&ff->polled_node, parent, link);
2007 rb_insert_color(&ff->polled_node, &fc->polled_files);
2009 spin_unlock(&fc->lock);
2012 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2014 struct fuse_file *ff = file->private_data;
2015 struct fuse_conn *fc = ff->fc;
2016 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2017 struct fuse_poll_out outarg;
2018 struct fuse_req *req;
2022 return DEFAULT_POLLMASK;
2024 poll_wait(file, &ff->poll_wait, wait);
2027 * Ask for notification iff there's someone waiting for it.
2028 * The client may ignore the flag and always notify.
2030 if (waitqueue_active(&ff->poll_wait)) {
2031 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2032 fuse_register_polled_file(fc, ff);
2035 req = fuse_get_req(fc);
2039 req->in.h.opcode = FUSE_POLL;
2040 req->in.h.nodeid = ff->nodeid;
2041 req->in.numargs = 1;
2042 req->in.args[0].size = sizeof(inarg);
2043 req->in.args[0].value = &inarg;
2044 req->out.numargs = 1;
2045 req->out.args[0].size = sizeof(outarg);
2046 req->out.args[0].value = &outarg;
2047 fuse_request_send(fc, req);
2048 err = req->out.h.error;
2049 fuse_put_request(fc, req);
2052 return outarg.revents;
2053 if (err == -ENOSYS) {
2055 return DEFAULT_POLLMASK;
2059 EXPORT_SYMBOL_GPL(fuse_file_poll);
2062 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2063 * wakes up the poll waiters.
2065 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2066 struct fuse_notify_poll_wakeup_out *outarg)
2068 u64 kh = outarg->kh;
2069 struct rb_node **link;
2071 spin_lock(&fc->lock);
2073 link = fuse_find_polled_node(fc, kh, NULL);
2075 struct fuse_file *ff;
2077 ff = rb_entry(*link, struct fuse_file, polled_node);
2078 wake_up_interruptible_sync(&ff->poll_wait);
2081 spin_unlock(&fc->lock);
2085 static const struct file_operations fuse_file_operations = {
2086 .llseek = fuse_file_llseek,
2087 .read = do_sync_read,
2088 .aio_read = fuse_file_aio_read,
2089 .write = do_sync_write,
2090 .aio_write = fuse_file_aio_write,
2091 .mmap = fuse_file_mmap,
2093 .flush = fuse_flush,
2094 .release = fuse_release,
2095 .fsync = fuse_fsync,
2096 .lock = fuse_file_lock,
2097 .flock = fuse_file_flock,
2098 .splice_read = generic_file_splice_read,
2099 .unlocked_ioctl = fuse_file_ioctl,
2100 .compat_ioctl = fuse_file_compat_ioctl,
2101 .poll = fuse_file_poll,
2104 static const struct file_operations fuse_direct_io_file_operations = {
2105 .llseek = fuse_file_llseek,
2106 .read = fuse_direct_read,
2107 .write = fuse_direct_write,
2108 .mmap = fuse_direct_mmap,
2110 .flush = fuse_flush,
2111 .release = fuse_release,
2112 .fsync = fuse_fsync,
2113 .lock = fuse_file_lock,
2114 .flock = fuse_file_flock,
2115 .unlocked_ioctl = fuse_file_ioctl,
2116 .compat_ioctl = fuse_file_compat_ioctl,
2117 .poll = fuse_file_poll,
2118 /* no splice_read */
2121 static const struct address_space_operations fuse_file_aops = {
2122 .readpage = fuse_readpage,
2123 .writepage = fuse_writepage,
2124 .launder_page = fuse_launder_page,
2125 .write_begin = fuse_write_begin,
2126 .write_end = fuse_write_end,
2127 .readpages = fuse_readpages,
2128 .set_page_dirty = __set_page_dirty_nobuffers,
2132 void fuse_init_file_inode(struct inode *inode)
2134 inode->i_fop = &fuse_file_operations;
2135 inode->i_data.a_ops = &fuse_file_aops;