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>
16 static const struct file_operations fuse_direct_io_file_operations;
18 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19 struct fuse_open_out *outargp)
21 struct fuse_conn *fc = get_fuse_conn(inode);
22 struct fuse_open_in inarg;
26 req = fuse_get_req(fc);
30 memset(&inarg, 0, sizeof(inarg));
31 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32 if (!fc->atomic_o_trunc)
33 inarg.flags &= ~O_TRUNC;
34 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
35 req->in.h.nodeid = get_node_id(inode);
37 req->in.args[0].size = sizeof(inarg);
38 req->in.args[0].value = &inarg;
40 req->out.args[0].size = sizeof(*outargp);
41 req->out.args[0].value = outargp;
42 fuse_request_send(fc, req);
43 err = req->out.h.error;
44 fuse_put_request(fc, req);
49 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
53 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
57 ff->reserved_req = fuse_request_alloc();
58 if (unlikely(!ff->reserved_req)) {
63 INIT_LIST_HEAD(&ff->write_entry);
64 atomic_set(&ff->count, 0);
65 RB_CLEAR_NODE(&ff->polled_node);
66 init_waitqueue_head(&ff->poll_wait);
70 spin_unlock(&fc->lock);
75 void fuse_file_free(struct fuse_file *ff)
77 fuse_request_free(ff->reserved_req);
81 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
83 atomic_inc(&ff->count);
87 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
89 path_put(&req->misc.release.path);
92 static void fuse_file_put(struct fuse_file *ff)
94 if (atomic_dec_and_test(&ff->count)) {
95 struct fuse_req *req = ff->reserved_req;
96 struct inode *inode = req->misc.release.path.dentry->d_inode;
97 struct fuse_conn *fc = get_fuse_conn(inode);
98 req->end = fuse_release_end;
99 fuse_request_send_background(fc, req);
104 void fuse_finish_open(struct inode *inode, struct file *file,
105 struct fuse_file *ff, struct fuse_open_out *outarg)
107 if (outarg->open_flags & FOPEN_DIRECT_IO)
108 file->f_op = &fuse_direct_io_file_operations;
109 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
110 invalidate_inode_pages2(inode->i_mapping);
111 if (outarg->open_flags & FOPEN_NONSEEKABLE)
112 nonseekable_open(inode, file);
114 file->private_data = fuse_file_get(ff);
117 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
119 struct fuse_conn *fc = get_fuse_conn(inode);
120 struct fuse_open_out outarg;
121 struct fuse_file *ff;
124 /* VFS checks this, but only _after_ ->open() */
125 if (file->f_flags & O_DIRECT)
128 err = generic_file_open(inode, file);
132 ff = fuse_file_alloc(fc);
136 err = fuse_send_open(inode, file, isdir, &outarg);
141 outarg.open_flags &= ~FOPEN_DIRECT_IO;
142 fuse_finish_open(inode, file, ff, &outarg);
148 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
150 struct fuse_req *req = ff->reserved_req;
151 struct fuse_release_in *inarg = &req->misc.release.in;
154 inarg->flags = flags;
155 req->in.h.opcode = opcode;
156 req->in.h.nodeid = nodeid;
158 req->in.args[0].size = sizeof(struct fuse_release_in);
159 req->in.args[0].value = inarg;
162 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
164 struct fuse_conn *fc;
165 struct fuse_file *ff;
166 struct fuse_req *req;
168 ff = file->private_data;
170 return 0; /* return value is ignored by VFS */
172 fc = get_fuse_conn(inode);
173 req = ff->reserved_req;
175 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
176 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
178 /* Hold vfsmount and dentry until release is finished */
179 path_get(&file->f_path);
180 req->misc.release.path = file->f_path;
182 spin_lock(&fc->lock);
183 list_del(&ff->write_entry);
184 if (!RB_EMPTY_NODE(&ff->polled_node))
185 rb_erase(&ff->polled_node, &fc->polled_files);
186 spin_unlock(&fc->lock);
188 wake_up_interruptible_sync(&ff->poll_wait);
190 * Normally this will send the RELEASE request, however if
191 * some asynchronous READ or WRITE requests are outstanding,
192 * the sending will be delayed.
198 static int fuse_open(struct inode *inode, struct file *file)
200 return fuse_open_common(inode, file, 0);
203 static int fuse_release(struct inode *inode, struct file *file)
205 return fuse_release_common(inode, file, 0);
209 * Scramble the ID space with XTEA, so that the value of the files_struct
210 * pointer is not exposed to userspace.
212 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
214 u32 *k = fc->scramble_key;
215 u64 v = (unsigned long) id;
221 for (i = 0; i < 32; i++) {
222 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
224 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
227 return (u64) v0 + ((u64) v1 << 32);
231 * Check if page is under writeback
233 * This is currently done by walking the list of writepage requests
234 * for the inode, which can be pretty inefficient.
236 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
238 struct fuse_conn *fc = get_fuse_conn(inode);
239 struct fuse_inode *fi = get_fuse_inode(inode);
240 struct fuse_req *req;
243 spin_lock(&fc->lock);
244 list_for_each_entry(req, &fi->writepages, writepages_entry) {
247 BUG_ON(req->inode != inode);
248 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
249 if (curr_index == index) {
254 spin_unlock(&fc->lock);
260 * Wait for page writeback to be completed.
262 * Since fuse doesn't rely on the VM writeback tracking, this has to
263 * use some other means.
265 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
267 struct fuse_inode *fi = get_fuse_inode(inode);
269 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
273 static int fuse_flush(struct file *file, fl_owner_t id)
275 struct inode *inode = file->f_path.dentry->d_inode;
276 struct fuse_conn *fc = get_fuse_conn(inode);
277 struct fuse_file *ff = file->private_data;
278 struct fuse_req *req;
279 struct fuse_flush_in inarg;
282 if (is_bad_inode(inode))
288 req = fuse_get_req_nofail(fc, file);
289 memset(&inarg, 0, sizeof(inarg));
291 inarg.lock_owner = fuse_lock_owner_id(fc, id);
292 req->in.h.opcode = FUSE_FLUSH;
293 req->in.h.nodeid = get_node_id(inode);
295 req->in.args[0].size = sizeof(inarg);
296 req->in.args[0].value = &inarg;
298 fuse_request_send(fc, req);
299 err = req->out.h.error;
300 fuse_put_request(fc, req);
301 if (err == -ENOSYS) {
309 * Wait for all pending writepages on the inode to finish.
311 * This is currently done by blocking further writes with FUSE_NOWRITE
312 * and waiting for all sent writes to complete.
314 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
315 * could conflict with truncation.
317 static void fuse_sync_writes(struct inode *inode)
319 fuse_set_nowrite(inode);
320 fuse_release_nowrite(inode);
323 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
326 struct inode *inode = de->d_inode;
327 struct fuse_conn *fc = get_fuse_conn(inode);
328 struct fuse_file *ff = file->private_data;
329 struct fuse_req *req;
330 struct fuse_fsync_in inarg;
333 if (is_bad_inode(inode))
336 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
340 * Start writeback against all dirty pages of the inode, then
341 * wait for all outstanding writes, before sending the FSYNC
344 err = write_inode_now(inode, 0);
348 fuse_sync_writes(inode);
350 req = fuse_get_req(fc);
354 memset(&inarg, 0, sizeof(inarg));
356 inarg.fsync_flags = datasync ? 1 : 0;
357 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
358 req->in.h.nodeid = get_node_id(inode);
360 req->in.args[0].size = sizeof(inarg);
361 req->in.args[0].value = &inarg;
362 fuse_request_send(fc, req);
363 err = req->out.h.error;
364 fuse_put_request(fc, req);
365 if (err == -ENOSYS) {
375 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
377 return fuse_fsync_common(file, de, datasync, 0);
380 void fuse_read_fill(struct fuse_req *req, struct file *file,
381 struct inode *inode, loff_t pos, size_t count, int opcode)
383 struct fuse_read_in *inarg = &req->misc.read.in;
384 struct fuse_file *ff = file->private_data;
389 inarg->flags = file->f_flags;
390 req->in.h.opcode = opcode;
391 req->in.h.nodeid = get_node_id(inode);
393 req->in.args[0].size = sizeof(struct fuse_read_in);
394 req->in.args[0].value = inarg;
396 req->out.numargs = 1;
397 req->out.args[0].size = count;
400 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
401 struct inode *inode, loff_t pos, size_t count,
404 struct fuse_conn *fc = get_fuse_conn(inode);
406 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
408 struct fuse_read_in *inarg = &req->misc.read.in;
410 inarg->read_flags |= FUSE_READ_LOCKOWNER;
411 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
413 fuse_request_send(fc, req);
414 return req->out.args[0].size;
417 static void fuse_read_update_size(struct inode *inode, loff_t size,
420 struct fuse_conn *fc = get_fuse_conn(inode);
421 struct fuse_inode *fi = get_fuse_inode(inode);
423 spin_lock(&fc->lock);
424 if (attr_ver == fi->attr_version && size < inode->i_size) {
425 fi->attr_version = ++fc->attr_version;
426 i_size_write(inode, size);
428 spin_unlock(&fc->lock);
431 static int fuse_readpage(struct file *file, struct page *page)
433 struct inode *inode = page->mapping->host;
434 struct fuse_conn *fc = get_fuse_conn(inode);
435 struct fuse_req *req;
437 loff_t pos = page_offset(page);
438 size_t count = PAGE_CACHE_SIZE;
443 if (is_bad_inode(inode))
447 * Page writeback can extend beyond the liftime of the
448 * page-cache page, so make sure we read a properly synced
451 fuse_wait_on_page_writeback(inode, page->index);
453 req = fuse_get_req(fc);
458 attr_ver = fuse_get_attr_version(fc);
460 req->out.page_zeroing = 1;
461 req->out.argpages = 1;
463 req->pages[0] = page;
464 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
465 err = req->out.h.error;
466 fuse_put_request(fc, req);
470 * Short read means EOF. If file size is larger, truncate it
472 if (num_read < count)
473 fuse_read_update_size(inode, pos + num_read, attr_ver);
475 SetPageUptodate(page);
478 fuse_invalidate_attr(inode); /* atime changed */
484 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
487 size_t count = req->misc.read.in.size;
488 size_t num_read = req->out.args[0].size;
489 struct inode *inode = req->pages[0]->mapping->host;
492 * Short read means EOF. If file size is larger, truncate it
494 if (!req->out.h.error && num_read < count) {
495 loff_t pos = page_offset(req->pages[0]) + num_read;
496 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
499 fuse_invalidate_attr(inode); /* atime changed */
501 for (i = 0; i < req->num_pages; i++) {
502 struct page *page = req->pages[i];
503 if (!req->out.h.error)
504 SetPageUptodate(page);
510 fuse_file_put(req->ff);
513 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
516 struct fuse_conn *fc = get_fuse_conn(inode);
517 loff_t pos = page_offset(req->pages[0]);
518 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
520 req->out.argpages = 1;
521 req->out.page_zeroing = 1;
522 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
523 req->misc.read.attr_ver = fuse_get_attr_version(fc);
524 if (fc->async_read) {
525 struct fuse_file *ff = file->private_data;
526 req->ff = fuse_file_get(ff);
527 req->end = fuse_readpages_end;
528 fuse_request_send_background(fc, req);
530 fuse_request_send(fc, req);
531 fuse_readpages_end(fc, req);
532 fuse_put_request(fc, req);
536 struct fuse_fill_data {
537 struct fuse_req *req;
542 static int fuse_readpages_fill(void *_data, struct page *page)
544 struct fuse_fill_data *data = _data;
545 struct fuse_req *req = data->req;
546 struct inode *inode = data->inode;
547 struct fuse_conn *fc = get_fuse_conn(inode);
549 fuse_wait_on_page_writeback(inode, page->index);
551 if (req->num_pages &&
552 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
553 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
554 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
555 fuse_send_readpages(req, data->file, inode);
556 data->req = req = fuse_get_req(fc);
562 req->pages[req->num_pages] = page;
567 static int fuse_readpages(struct file *file, struct address_space *mapping,
568 struct list_head *pages, unsigned nr_pages)
570 struct inode *inode = mapping->host;
571 struct fuse_conn *fc = get_fuse_conn(inode);
572 struct fuse_fill_data data;
576 if (is_bad_inode(inode))
581 data.req = fuse_get_req(fc);
582 err = PTR_ERR(data.req);
583 if (IS_ERR(data.req))
586 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
588 if (data.req->num_pages)
589 fuse_send_readpages(data.req, file, inode);
591 fuse_put_request(fc, data.req);
597 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
598 unsigned long nr_segs, loff_t pos)
600 struct inode *inode = iocb->ki_filp->f_mapping->host;
602 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
605 * If trying to read past EOF, make sure the i_size
606 * attribute is up-to-date.
608 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
613 return generic_file_aio_read(iocb, iov, nr_segs, pos);
616 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
617 struct inode *inode, loff_t pos, size_t count)
619 struct fuse_conn *fc = get_fuse_conn(inode);
620 struct fuse_write_in *inarg = &req->misc.write.in;
621 struct fuse_write_out *outarg = &req->misc.write.out;
626 req->in.h.opcode = FUSE_WRITE;
627 req->in.h.nodeid = get_node_id(inode);
630 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
632 req->in.args[0].size = sizeof(struct fuse_write_in);
633 req->in.args[0].value = inarg;
634 req->in.args[1].size = count;
635 req->out.numargs = 1;
636 req->out.args[0].size = sizeof(struct fuse_write_out);
637 req->out.args[0].value = outarg;
640 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
641 struct inode *inode, loff_t pos, size_t count,
644 struct fuse_conn *fc = get_fuse_conn(inode);
645 struct fuse_write_in *inarg = &req->misc.write.in;
647 fuse_write_fill(req, file->private_data, inode, pos, count);
648 inarg->flags = file->f_flags;
650 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
651 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
653 fuse_request_send(fc, req);
654 return req->misc.write.out.size;
657 static int fuse_write_begin(struct file *file, struct address_space *mapping,
658 loff_t pos, unsigned len, unsigned flags,
659 struct page **pagep, void **fsdata)
661 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
663 *pagep = grab_cache_page_write_begin(mapping, index, flags);
669 static void fuse_write_update_size(struct inode *inode, loff_t pos)
671 struct fuse_conn *fc = get_fuse_conn(inode);
672 struct fuse_inode *fi = get_fuse_inode(inode);
674 spin_lock(&fc->lock);
675 fi->attr_version = ++fc->attr_version;
676 if (pos > inode->i_size)
677 i_size_write(inode, pos);
678 spin_unlock(&fc->lock);
681 static int fuse_buffered_write(struct file *file, struct inode *inode,
682 loff_t pos, unsigned count, struct page *page)
686 struct fuse_conn *fc = get_fuse_conn(inode);
687 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
688 struct fuse_req *req;
690 if (is_bad_inode(inode))
694 * Make sure writepages on the same page are not mixed up with
697 fuse_wait_on_page_writeback(inode, page->index);
699 req = fuse_get_req(fc);
703 req->in.argpages = 1;
705 req->pages[0] = page;
706 req->page_offset = offset;
707 nres = fuse_send_write(req, file, inode, pos, count, NULL);
708 err = req->out.h.error;
709 fuse_put_request(fc, req);
714 fuse_write_update_size(inode, pos);
715 if (count == PAGE_CACHE_SIZE)
716 SetPageUptodate(page);
718 fuse_invalidate_attr(inode);
719 return err ? err : nres;
722 static int fuse_write_end(struct file *file, struct address_space *mapping,
723 loff_t pos, unsigned len, unsigned copied,
724 struct page *page, void *fsdata)
726 struct inode *inode = mapping->host;
730 res = fuse_buffered_write(file, inode, pos, copied, page);
733 page_cache_release(page);
737 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
738 struct inode *inode, loff_t pos,
745 for (i = 0; i < req->num_pages; i++)
746 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
748 res = fuse_send_write(req, file, inode, pos, count, NULL);
750 offset = req->page_offset;
752 for (i = 0; i < req->num_pages; i++) {
753 struct page *page = req->pages[i];
755 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
756 SetPageUptodate(page);
758 if (count > PAGE_CACHE_SIZE - offset)
759 count -= PAGE_CACHE_SIZE - offset;
765 page_cache_release(page);
771 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
772 struct address_space *mapping,
773 struct iov_iter *ii, loff_t pos)
775 struct fuse_conn *fc = get_fuse_conn(mapping->host);
776 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
780 req->in.argpages = 1;
781 req->page_offset = offset;
786 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
787 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
790 bytes = min_t(size_t, bytes, fc->max_write - count);
794 if (iov_iter_fault_in_readable(ii, bytes))
798 page = grab_cache_page_write_begin(mapping, index, 0);
803 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
805 flush_dcache_page(page);
809 page_cache_release(page);
810 bytes = min(bytes, iov_iter_single_seg_count(ii));
815 req->pages[req->num_pages] = page;
818 iov_iter_advance(ii, tmp);
822 if (offset == PAGE_CACHE_SIZE)
827 } while (iov_iter_count(ii) && count < fc->max_write &&
828 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
830 return count > 0 ? count : err;
833 static ssize_t fuse_perform_write(struct file *file,
834 struct address_space *mapping,
835 struct iov_iter *ii, loff_t pos)
837 struct inode *inode = mapping->host;
838 struct fuse_conn *fc = get_fuse_conn(inode);
842 if (is_bad_inode(inode))
846 struct fuse_req *req;
849 req = fuse_get_req(fc);
855 count = fuse_fill_write_pages(req, mapping, ii, pos);
861 num_written = fuse_send_write_pages(req, file, inode,
863 err = req->out.h.error;
868 /* break out of the loop on short write */
869 if (num_written != count)
873 fuse_put_request(fc, req);
874 } while (!err && iov_iter_count(ii));
877 fuse_write_update_size(inode, pos);
879 fuse_invalidate_attr(inode);
881 return res > 0 ? res : err;
884 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
885 unsigned long nr_segs, loff_t pos)
887 struct file *file = iocb->ki_filp;
888 struct address_space *mapping = file->f_mapping;
891 struct inode *inode = mapping->host;
895 WARN_ON(iocb->ki_pos != pos);
897 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
901 mutex_lock(&inode->i_mutex);
902 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
904 /* We can write back this queue in page reclaim */
905 current->backing_dev_info = mapping->backing_dev_info;
907 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
914 err = file_remove_suid(file);
918 file_update_time(file);
920 iov_iter_init(&i, iov, nr_segs, count, 0);
921 written = fuse_perform_write(file, mapping, &i, pos);
923 iocb->ki_pos = pos + written;
926 current->backing_dev_info = NULL;
927 mutex_unlock(&inode->i_mutex);
929 return written ? written : err;
932 static void fuse_release_user_pages(struct fuse_req *req, int write)
936 for (i = 0; i < req->num_pages; i++) {
937 struct page *page = req->pages[i];
939 set_page_dirty_lock(page);
944 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
945 size_t *nbytesp, int write)
947 size_t nbytes = *nbytesp;
948 unsigned long user_addr = (unsigned long) buf;
949 unsigned offset = user_addr & ~PAGE_MASK;
952 /* Special case for kernel I/O: can copy directly into the buffer */
953 if (segment_eq(get_fs(), KERNEL_DS)) {
955 req->in.args[1].value = (void *) user_addr;
957 req->out.args[0].value = (void *) user_addr;
962 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
963 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
964 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
965 down_read(¤t->mm->mmap_sem);
966 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
967 0, req->pages, NULL);
968 up_read(¤t->mm->mmap_sem);
972 req->num_pages = npages;
973 req->page_offset = offset;
976 req->in.argpages = 1;
978 req->out.argpages = 1;
980 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
981 *nbytesp = min(*nbytesp, nbytes);
986 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
987 size_t count, loff_t *ppos, int write)
989 struct inode *inode = file->f_path.dentry->d_inode;
990 struct fuse_conn *fc = get_fuse_conn(inode);
991 size_t nmax = write ? fc->max_write : fc->max_read;
994 struct fuse_req *req;
996 if (is_bad_inode(inode))
999 req = fuse_get_req(fc);
1001 return PTR_ERR(req);
1005 size_t nbytes = min(count, nmax);
1006 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1013 nres = fuse_send_write(req, file, inode, pos, nbytes,
1016 nres = fuse_send_read(req, file, inode, pos, nbytes,
1018 fuse_release_user_pages(req, !write);
1019 if (req->out.h.error) {
1021 res = req->out.h.error;
1023 } else if (nres > nbytes) {
1034 fuse_put_request(fc, req);
1035 req = fuse_get_req(fc);
1040 fuse_put_request(fc, req);
1043 fuse_write_update_size(inode, pos);
1046 fuse_invalidate_attr(inode);
1051 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1052 size_t count, loff_t *ppos)
1054 return fuse_direct_io(file, buf, count, ppos, 0);
1057 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1058 size_t count, loff_t *ppos)
1060 struct inode *inode = file->f_path.dentry->d_inode;
1062 /* Don't allow parallel writes to the same file */
1063 mutex_lock(&inode->i_mutex);
1064 res = generic_write_checks(file, ppos, &count, 0);
1066 res = fuse_direct_io(file, buf, count, ppos, 1);
1067 mutex_unlock(&inode->i_mutex);
1071 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1073 __free_page(req->pages[0]);
1074 fuse_file_put(req->ff);
1077 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1079 struct inode *inode = req->inode;
1080 struct fuse_inode *fi = get_fuse_inode(inode);
1081 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1083 list_del(&req->writepages_entry);
1084 dec_bdi_stat(bdi, BDI_WRITEBACK);
1085 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1086 bdi_writeout_inc(bdi);
1087 wake_up(&fi->page_waitq);
1090 /* Called under fc->lock, may release and reacquire it */
1091 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1092 __releases(&fc->lock)
1093 __acquires(&fc->lock)
1095 struct fuse_inode *fi = get_fuse_inode(req->inode);
1096 loff_t size = i_size_read(req->inode);
1097 struct fuse_write_in *inarg = &req->misc.write.in;
1102 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1103 inarg->size = PAGE_CACHE_SIZE;
1104 } else if (inarg->offset < size) {
1105 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1107 /* Got truncated off completely */
1111 req->in.args[1].size = inarg->size;
1113 fuse_request_send_background_locked(fc, req);
1117 fuse_writepage_finish(fc, req);
1118 spin_unlock(&fc->lock);
1119 fuse_writepage_free(fc, req);
1120 fuse_put_request(fc, req);
1121 spin_lock(&fc->lock);
1125 * If fi->writectr is positive (no truncate or fsync going on) send
1126 * all queued writepage requests.
1128 * Called with fc->lock
1130 void fuse_flush_writepages(struct inode *inode)
1131 __releases(&fc->lock)
1132 __acquires(&fc->lock)
1134 struct fuse_conn *fc = get_fuse_conn(inode);
1135 struct fuse_inode *fi = get_fuse_inode(inode);
1136 struct fuse_req *req;
1138 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1139 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1140 list_del_init(&req->list);
1141 fuse_send_writepage(fc, req);
1145 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1147 struct inode *inode = req->inode;
1148 struct fuse_inode *fi = get_fuse_inode(inode);
1150 mapping_set_error(inode->i_mapping, req->out.h.error);
1151 spin_lock(&fc->lock);
1153 fuse_writepage_finish(fc, req);
1154 spin_unlock(&fc->lock);
1155 fuse_writepage_free(fc, req);
1158 static int fuse_writepage_locked(struct page *page)
1160 struct address_space *mapping = page->mapping;
1161 struct inode *inode = mapping->host;
1162 struct fuse_conn *fc = get_fuse_conn(inode);
1163 struct fuse_inode *fi = get_fuse_inode(inode);
1164 struct fuse_req *req;
1165 struct fuse_file *ff;
1166 struct page *tmp_page;
1168 set_page_writeback(page);
1170 req = fuse_request_alloc_nofs();
1174 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1178 spin_lock(&fc->lock);
1179 BUG_ON(list_empty(&fi->write_files));
1180 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1181 req->ff = fuse_file_get(ff);
1182 spin_unlock(&fc->lock);
1184 fuse_write_fill(req, ff, inode, page_offset(page), 0);
1186 copy_highpage(tmp_page, page);
1187 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1188 req->in.argpages = 1;
1190 req->pages[0] = tmp_page;
1191 req->page_offset = 0;
1192 req->end = fuse_writepage_end;
1195 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1196 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1197 end_page_writeback(page);
1199 spin_lock(&fc->lock);
1200 list_add(&req->writepages_entry, &fi->writepages);
1201 list_add_tail(&req->list, &fi->queued_writes);
1202 fuse_flush_writepages(inode);
1203 spin_unlock(&fc->lock);
1208 fuse_request_free(req);
1210 end_page_writeback(page);
1214 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1218 err = fuse_writepage_locked(page);
1224 static int fuse_launder_page(struct page *page)
1227 if (clear_page_dirty_for_io(page)) {
1228 struct inode *inode = page->mapping->host;
1229 err = fuse_writepage_locked(page);
1231 fuse_wait_on_page_writeback(inode, page->index);
1237 * Write back dirty pages now, because there may not be any suitable
1240 static void fuse_vma_close(struct vm_area_struct *vma)
1242 filemap_write_and_wait(vma->vm_file->f_mapping);
1246 * Wait for writeback against this page to complete before allowing it
1247 * to be marked dirty again, and hence written back again, possibly
1248 * before the previous writepage completed.
1250 * Block here, instead of in ->writepage(), so that the userspace fs
1251 * can only block processes actually operating on the filesystem.
1253 * Otherwise unprivileged userspace fs would be able to block
1258 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1260 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1262 struct page *page = vmf->page;
1264 * Don't use page->mapping as it may become NULL from a
1265 * concurrent truncate.
1267 struct inode *inode = vma->vm_file->f_mapping->host;
1269 fuse_wait_on_page_writeback(inode, page->index);
1273 static struct vm_operations_struct fuse_file_vm_ops = {
1274 .close = fuse_vma_close,
1275 .fault = filemap_fault,
1276 .page_mkwrite = fuse_page_mkwrite,
1279 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1281 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1282 struct inode *inode = file->f_dentry->d_inode;
1283 struct fuse_conn *fc = get_fuse_conn(inode);
1284 struct fuse_inode *fi = get_fuse_inode(inode);
1285 struct fuse_file *ff = file->private_data;
1287 * file may be written through mmap, so chain it onto the
1288 * inodes's write_file list
1290 spin_lock(&fc->lock);
1291 if (list_empty(&ff->write_entry))
1292 list_add(&ff->write_entry, &fi->write_files);
1293 spin_unlock(&fc->lock);
1295 file_accessed(file);
1296 vma->vm_ops = &fuse_file_vm_ops;
1300 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1302 /* Can't provide the coherency needed for MAP_SHARED */
1303 if (vma->vm_flags & VM_MAYSHARE)
1306 invalidate_inode_pages2(file->f_mapping);
1308 return generic_file_mmap(file, vma);
1311 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1312 struct file_lock *fl)
1314 switch (ffl->type) {
1320 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1321 ffl->end < ffl->start)
1324 fl->fl_start = ffl->start;
1325 fl->fl_end = ffl->end;
1326 fl->fl_pid = ffl->pid;
1332 fl->fl_type = ffl->type;
1336 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1337 const struct file_lock *fl, int opcode, pid_t pid,
1340 struct inode *inode = file->f_path.dentry->d_inode;
1341 struct fuse_conn *fc = get_fuse_conn(inode);
1342 struct fuse_file *ff = file->private_data;
1343 struct fuse_lk_in *arg = &req->misc.lk_in;
1346 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1347 arg->lk.start = fl->fl_start;
1348 arg->lk.end = fl->fl_end;
1349 arg->lk.type = fl->fl_type;
1352 arg->lk_flags |= FUSE_LK_FLOCK;
1353 req->in.h.opcode = opcode;
1354 req->in.h.nodeid = get_node_id(inode);
1355 req->in.numargs = 1;
1356 req->in.args[0].size = sizeof(*arg);
1357 req->in.args[0].value = arg;
1360 static int fuse_getlk(struct file *file, struct file_lock *fl)
1362 struct inode *inode = file->f_path.dentry->d_inode;
1363 struct fuse_conn *fc = get_fuse_conn(inode);
1364 struct fuse_req *req;
1365 struct fuse_lk_out outarg;
1368 req = fuse_get_req(fc);
1370 return PTR_ERR(req);
1372 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1373 req->out.numargs = 1;
1374 req->out.args[0].size = sizeof(outarg);
1375 req->out.args[0].value = &outarg;
1376 fuse_request_send(fc, req);
1377 err = req->out.h.error;
1378 fuse_put_request(fc, req);
1380 err = convert_fuse_file_lock(&outarg.lk, fl);
1385 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1387 struct inode *inode = file->f_path.dentry->d_inode;
1388 struct fuse_conn *fc = get_fuse_conn(inode);
1389 struct fuse_req *req;
1390 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1391 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1394 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1395 /* NLM needs asynchronous locks, which we don't support yet */
1399 /* Unlock on close is handled by the flush method */
1400 if (fl->fl_flags & FL_CLOSE)
1403 req = fuse_get_req(fc);
1405 return PTR_ERR(req);
1407 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1408 fuse_request_send(fc, req);
1409 err = req->out.h.error;
1410 /* locking is restartable */
1413 fuse_put_request(fc, req);
1417 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1419 struct inode *inode = file->f_path.dentry->d_inode;
1420 struct fuse_conn *fc = get_fuse_conn(inode);
1423 if (cmd == F_CANCELLK) {
1425 } else if (cmd == F_GETLK) {
1427 posix_test_lock(file, fl);
1430 err = fuse_getlk(file, fl);
1433 err = posix_lock_file(file, fl, NULL);
1435 err = fuse_setlk(file, fl, 0);
1440 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1442 struct inode *inode = file->f_path.dentry->d_inode;
1443 struct fuse_conn *fc = get_fuse_conn(inode);
1447 err = flock_lock_file_wait(file, fl);
1449 /* emulate flock with POSIX locks */
1450 fl->fl_owner = (fl_owner_t) file;
1451 err = fuse_setlk(file, fl, 1);
1457 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1459 struct inode *inode = mapping->host;
1460 struct fuse_conn *fc = get_fuse_conn(inode);
1461 struct fuse_req *req;
1462 struct fuse_bmap_in inarg;
1463 struct fuse_bmap_out outarg;
1466 if (!inode->i_sb->s_bdev || fc->no_bmap)
1469 req = fuse_get_req(fc);
1473 memset(&inarg, 0, sizeof(inarg));
1474 inarg.block = block;
1475 inarg.blocksize = inode->i_sb->s_blocksize;
1476 req->in.h.opcode = FUSE_BMAP;
1477 req->in.h.nodeid = get_node_id(inode);
1478 req->in.numargs = 1;
1479 req->in.args[0].size = sizeof(inarg);
1480 req->in.args[0].value = &inarg;
1481 req->out.numargs = 1;
1482 req->out.args[0].size = sizeof(outarg);
1483 req->out.args[0].value = &outarg;
1484 fuse_request_send(fc, req);
1485 err = req->out.h.error;
1486 fuse_put_request(fc, req);
1490 return err ? 0 : outarg.block;
1493 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1496 struct inode *inode = file->f_path.dentry->d_inode;
1498 mutex_lock(&inode->i_mutex);
1501 retval = fuse_update_attributes(inode, NULL, file, NULL);
1504 offset += i_size_read(inode);
1507 offset += file->f_pos;
1510 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1511 if (offset != file->f_pos) {
1512 file->f_pos = offset;
1513 file->f_version = 0;
1518 mutex_unlock(&inode->i_mutex);
1522 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1523 unsigned int nr_segs, size_t bytes, bool to_user)
1531 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1533 while (iov_iter_count(&ii)) {
1534 struct page *page = pages[page_idx++];
1535 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1538 kaddr = map = kmap(page);
1541 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1542 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1543 size_t copy = min(todo, iov_len);
1547 left = copy_from_user(kaddr, uaddr, copy);
1549 left = copy_to_user(uaddr, kaddr, copy);
1554 iov_iter_advance(&ii, copy);
1566 * For ioctls, there is no generic way to determine how much memory
1567 * needs to be read and/or written. Furthermore, ioctls are allowed
1568 * to dereference the passed pointer, so the parameter requires deep
1569 * copying but FUSE has no idea whatsoever about what to copy in or
1572 * This is solved by allowing FUSE server to retry ioctl with
1573 * necessary in/out iovecs. Let's assume the ioctl implementation
1574 * needs to read in the following structure.
1581 * On the first callout to FUSE server, inarg->in_size and
1582 * inarg->out_size will be NULL; then, the server completes the ioctl
1583 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1584 * the actual iov array to
1586 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1588 * which tells FUSE to copy in the requested area and retry the ioctl.
1589 * On the second round, the server has access to the structure and
1590 * from that it can tell what to look for next, so on the invocation,
1591 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1593 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1594 * { .iov_base = a.buf, .iov_len = a.buflen } }
1596 * FUSE will copy both struct a and the pointed buffer from the
1597 * process doing the ioctl and retry ioctl with both struct a and the
1600 * This time, FUSE server has everything it needs and completes ioctl
1601 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1603 * Copying data out works the same way.
1605 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1606 * automatically initializes in and out iovs by decoding @cmd with
1607 * _IOC_* macros and the server is not allowed to request RETRY. This
1608 * limits ioctl data transfers to well-formed ioctls and is the forced
1609 * behavior for all FUSE servers.
1611 static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
1612 unsigned long arg, unsigned int flags)
1614 struct inode *inode = file->f_dentry->d_inode;
1615 struct fuse_file *ff = file->private_data;
1616 struct fuse_conn *fc = get_fuse_conn(inode);
1617 struct fuse_ioctl_in inarg = {
1623 struct fuse_ioctl_out outarg;
1624 struct fuse_req *req = NULL;
1625 struct page **pages = NULL;
1626 struct page *iov_page = NULL;
1627 struct iovec *in_iov = NULL, *out_iov = NULL;
1628 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1629 size_t in_size, out_size, transferred;
1632 /* assume all the iovs returned by client always fits in a page */
1633 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1635 if (!fuse_allow_task(fc, current))
1639 if (is_bad_inode(inode))
1643 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1644 iov_page = alloc_page(GFP_KERNEL);
1645 if (!pages || !iov_page)
1649 * If restricted, initialize IO parameters as encoded in @cmd.
1650 * RETRY from server is not allowed.
1652 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1653 struct iovec *iov = page_address(iov_page);
1655 iov->iov_base = (void __user *)arg;
1656 iov->iov_len = _IOC_SIZE(cmd);
1658 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1663 if (_IOC_DIR(cmd) & _IOC_READ) {
1670 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1671 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1674 * Out data can be used either for actual out data or iovs,
1675 * make sure there always is at least one page.
1677 out_size = max_t(size_t, out_size, PAGE_SIZE);
1678 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1680 /* make sure there are enough buffer pages and init request with them */
1682 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1684 while (num_pages < max_pages) {
1685 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1686 if (!pages[num_pages])
1691 req = fuse_get_req(fc);
1697 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1698 req->num_pages = num_pages;
1700 /* okay, let's send it to the client */
1701 req->in.h.opcode = FUSE_IOCTL;
1702 req->in.h.nodeid = get_node_id(inode);
1703 req->in.numargs = 1;
1704 req->in.args[0].size = sizeof(inarg);
1705 req->in.args[0].value = &inarg;
1708 req->in.args[1].size = in_size;
1709 req->in.argpages = 1;
1711 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1717 req->out.numargs = 2;
1718 req->out.args[0].size = sizeof(outarg);
1719 req->out.args[0].value = &outarg;
1720 req->out.args[1].size = out_size;
1721 req->out.argpages = 1;
1722 req->out.argvar = 1;
1724 fuse_request_send(fc, req);
1725 err = req->out.h.error;
1726 transferred = req->out.args[1].size;
1727 fuse_put_request(fc, req);
1732 /* did it ask for retry? */
1733 if (outarg.flags & FUSE_IOCTL_RETRY) {
1736 /* no retry if in restricted mode */
1738 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1741 in_iovs = outarg.in_iovs;
1742 out_iovs = outarg.out_iovs;
1745 * Make sure things are in boundary, separate checks
1746 * are to protect against overflow.
1749 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1750 out_iovs > FUSE_IOCTL_MAX_IOV ||
1751 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1755 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1758 /* okay, copy in iovs and retry */
1759 vaddr = kmap_atomic(pages[0], KM_USER0);
1760 memcpy(page_address(iov_page), vaddr, transferred);
1761 kunmap_atomic(vaddr, KM_USER0);
1763 in_iov = page_address(iov_page);
1764 out_iov = in_iov + in_iovs;
1770 if (transferred > inarg.out_size)
1773 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1776 fuse_put_request(fc, req);
1778 __free_page(iov_page);
1780 __free_page(pages[--num_pages]);
1783 return err ? err : outarg.result;
1786 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1789 return fuse_file_do_ioctl(file, cmd, arg, 0);
1792 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1795 return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
1799 * All files which have been polled are linked to RB tree
1800 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1801 * find the matching one.
1803 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1804 struct rb_node **parent_out)
1806 struct rb_node **link = &fc->polled_files.rb_node;
1807 struct rb_node *last = NULL;
1810 struct fuse_file *ff;
1813 ff = rb_entry(last, struct fuse_file, polled_node);
1816 link = &last->rb_left;
1817 else if (kh > ff->kh)
1818 link = &last->rb_right;
1829 * The file is about to be polled. Make sure it's on the polled_files
1830 * RB tree. Note that files once added to the polled_files tree are
1831 * not removed before the file is released. This is because a file
1832 * polled once is likely to be polled again.
1834 static void fuse_register_polled_file(struct fuse_conn *fc,
1835 struct fuse_file *ff)
1837 spin_lock(&fc->lock);
1838 if (RB_EMPTY_NODE(&ff->polled_node)) {
1839 struct rb_node **link, *parent;
1841 link = fuse_find_polled_node(fc, ff->kh, &parent);
1843 rb_link_node(&ff->polled_node, parent, link);
1844 rb_insert_color(&ff->polled_node, &fc->polled_files);
1846 spin_unlock(&fc->lock);
1849 static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1851 struct inode *inode = file->f_dentry->d_inode;
1852 struct fuse_file *ff = file->private_data;
1853 struct fuse_conn *fc = get_fuse_conn(inode);
1854 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1855 struct fuse_poll_out outarg;
1856 struct fuse_req *req;
1860 return DEFAULT_POLLMASK;
1862 poll_wait(file, &ff->poll_wait, wait);
1865 * Ask for notification iff there's someone waiting for it.
1866 * The client may ignore the flag and always notify.
1868 if (waitqueue_active(&ff->poll_wait)) {
1869 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1870 fuse_register_polled_file(fc, ff);
1873 req = fuse_get_req(fc);
1875 return PTR_ERR(req);
1877 req->in.h.opcode = FUSE_POLL;
1878 req->in.h.nodeid = get_node_id(inode);
1879 req->in.numargs = 1;
1880 req->in.args[0].size = sizeof(inarg);
1881 req->in.args[0].value = &inarg;
1882 req->out.numargs = 1;
1883 req->out.args[0].size = sizeof(outarg);
1884 req->out.args[0].value = &outarg;
1885 fuse_request_send(fc, req);
1886 err = req->out.h.error;
1887 fuse_put_request(fc, req);
1890 return outarg.revents;
1891 if (err == -ENOSYS) {
1893 return DEFAULT_POLLMASK;
1899 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1900 * wakes up the poll waiters.
1902 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1903 struct fuse_notify_poll_wakeup_out *outarg)
1905 u64 kh = outarg->kh;
1906 struct rb_node **link;
1908 spin_lock(&fc->lock);
1910 link = fuse_find_polled_node(fc, kh, NULL);
1912 struct fuse_file *ff;
1914 ff = rb_entry(*link, struct fuse_file, polled_node);
1915 wake_up_interruptible_sync(&ff->poll_wait);
1918 spin_unlock(&fc->lock);
1922 static const struct file_operations fuse_file_operations = {
1923 .llseek = fuse_file_llseek,
1924 .read = do_sync_read,
1925 .aio_read = fuse_file_aio_read,
1926 .write = do_sync_write,
1927 .aio_write = fuse_file_aio_write,
1928 .mmap = fuse_file_mmap,
1930 .flush = fuse_flush,
1931 .release = fuse_release,
1932 .fsync = fuse_fsync,
1933 .lock = fuse_file_lock,
1934 .flock = fuse_file_flock,
1935 .splice_read = generic_file_splice_read,
1936 .unlocked_ioctl = fuse_file_ioctl,
1937 .compat_ioctl = fuse_file_compat_ioctl,
1938 .poll = fuse_file_poll,
1941 static const struct file_operations fuse_direct_io_file_operations = {
1942 .llseek = fuse_file_llseek,
1943 .read = fuse_direct_read,
1944 .write = fuse_direct_write,
1945 .mmap = fuse_direct_mmap,
1947 .flush = fuse_flush,
1948 .release = fuse_release,
1949 .fsync = fuse_fsync,
1950 .lock = fuse_file_lock,
1951 .flock = fuse_file_flock,
1952 .unlocked_ioctl = fuse_file_ioctl,
1953 .compat_ioctl = fuse_file_compat_ioctl,
1954 .poll = fuse_file_poll,
1955 /* no splice_read */
1958 static const struct address_space_operations fuse_file_aops = {
1959 .readpage = fuse_readpage,
1960 .writepage = fuse_writepage,
1961 .launder_page = fuse_launder_page,
1962 .write_begin = fuse_write_begin,
1963 .write_end = fuse_write_end,
1964 .readpages = fuse_readpages,
1965 .set_page_dirty = __set_page_dirty_nobuffers,
1969 void fuse_init_file_inode(struct inode *inode)
1971 inode->i_fop = &fuse_file_operations;
1972 inode->i_data.a_ops = &fuse_file_aops;