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);
58 ff->reserved_req = fuse_request_alloc();
59 if (unlikely(!ff->reserved_req)) {
64 INIT_LIST_HEAD(&ff->write_entry);
65 atomic_set(&ff->count, 0);
66 RB_CLEAR_NODE(&ff->polled_node);
67 init_waitqueue_head(&ff->poll_wait);
71 spin_unlock(&fc->lock);
76 void fuse_file_free(struct fuse_file *ff)
78 fuse_request_free(ff->reserved_req);
82 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
84 atomic_inc(&ff->count);
88 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
90 path_put(&req->misc.release.path);
93 static void fuse_file_put(struct fuse_file *ff)
95 if (atomic_dec_and_test(&ff->count)) {
96 struct fuse_req *req = ff->reserved_req;
97 struct inode *inode = req->misc.release.path.dentry->d_inode;
98 struct fuse_conn *fc = get_fuse_conn(inode);
99 req->end = fuse_release_end;
100 fuse_request_send_background(fc, req);
105 void fuse_finish_open(struct inode *inode, struct file *file,
106 struct fuse_file *ff, struct fuse_open_out *outarg)
108 if (outarg->open_flags & FOPEN_DIRECT_IO)
109 file->f_op = &fuse_direct_io_file_operations;
110 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
111 invalidate_inode_pages2(inode->i_mapping);
112 if (outarg->open_flags & FOPEN_NONSEEKABLE)
113 nonseekable_open(inode, file);
115 ff->nodeid = get_node_id(inode);
116 file->private_data = fuse_file_get(ff);
119 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
121 struct fuse_conn *fc = get_fuse_conn(inode);
122 struct fuse_open_out outarg;
123 struct fuse_file *ff;
126 /* VFS checks this, but only _after_ ->open() */
127 if (file->f_flags & O_DIRECT)
130 err = generic_file_open(inode, file);
134 ff = fuse_file_alloc(fc);
138 err = fuse_send_open(inode, file, isdir, &outarg);
143 outarg.open_flags &= ~FOPEN_DIRECT_IO;
144 fuse_finish_open(inode, file, ff, &outarg);
150 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
152 struct fuse_req *req = ff->reserved_req;
153 struct fuse_release_in *inarg = &req->misc.release.in;
156 inarg->flags = flags;
157 req->in.h.opcode = opcode;
158 req->in.h.nodeid = nodeid;
160 req->in.args[0].size = sizeof(struct fuse_release_in);
161 req->in.args[0].value = inarg;
164 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
166 struct fuse_conn *fc;
167 struct fuse_file *ff;
168 struct fuse_req *req;
170 ff = file->private_data;
172 return 0; /* return value is ignored by VFS */
174 fc = get_fuse_conn(inode);
175 req = ff->reserved_req;
177 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
178 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
180 /* Hold vfsmount and dentry until release is finished */
181 path_get(&file->f_path);
182 req->misc.release.path = file->f_path;
184 spin_lock(&fc->lock);
185 list_del(&ff->write_entry);
186 if (!RB_EMPTY_NODE(&ff->polled_node))
187 rb_erase(&ff->polled_node, &fc->polled_files);
188 spin_unlock(&fc->lock);
190 wake_up_interruptible_sync(&ff->poll_wait);
192 * Normally this will send the RELEASE request, however if
193 * some asynchronous READ or WRITE requests are outstanding,
194 * the sending will be delayed.
200 static int fuse_open(struct inode *inode, struct file *file)
202 return fuse_open_common(inode, file, 0);
205 static int fuse_release(struct inode *inode, struct file *file)
207 return fuse_release_common(inode, file, 0);
211 * Scramble the ID space with XTEA, so that the value of the files_struct
212 * pointer is not exposed to userspace.
214 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
216 u32 *k = fc->scramble_key;
217 u64 v = (unsigned long) id;
223 for (i = 0; i < 32; i++) {
224 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
226 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
229 return (u64) v0 + ((u64) v1 << 32);
233 * Check if page is under writeback
235 * This is currently done by walking the list of writepage requests
236 * for the inode, which can be pretty inefficient.
238 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
240 struct fuse_conn *fc = get_fuse_conn(inode);
241 struct fuse_inode *fi = get_fuse_inode(inode);
242 struct fuse_req *req;
245 spin_lock(&fc->lock);
246 list_for_each_entry(req, &fi->writepages, writepages_entry) {
249 BUG_ON(req->inode != inode);
250 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
251 if (curr_index == index) {
256 spin_unlock(&fc->lock);
262 * Wait for page writeback to be completed.
264 * Since fuse doesn't rely on the VM writeback tracking, this has to
265 * use some other means.
267 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
269 struct fuse_inode *fi = get_fuse_inode(inode);
271 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
275 static int fuse_flush(struct file *file, fl_owner_t id)
277 struct inode *inode = file->f_path.dentry->d_inode;
278 struct fuse_conn *fc = get_fuse_conn(inode);
279 struct fuse_file *ff = file->private_data;
280 struct fuse_req *req;
281 struct fuse_flush_in inarg;
284 if (is_bad_inode(inode))
290 req = fuse_get_req_nofail(fc, file);
291 memset(&inarg, 0, sizeof(inarg));
293 inarg.lock_owner = fuse_lock_owner_id(fc, id);
294 req->in.h.opcode = FUSE_FLUSH;
295 req->in.h.nodeid = get_node_id(inode);
297 req->in.args[0].size = sizeof(inarg);
298 req->in.args[0].value = &inarg;
300 fuse_request_send(fc, req);
301 err = req->out.h.error;
302 fuse_put_request(fc, req);
303 if (err == -ENOSYS) {
311 * Wait for all pending writepages on the inode to finish.
313 * This is currently done by blocking further writes with FUSE_NOWRITE
314 * and waiting for all sent writes to complete.
316 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
317 * could conflict with truncation.
319 static void fuse_sync_writes(struct inode *inode)
321 fuse_set_nowrite(inode);
322 fuse_release_nowrite(inode);
325 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
328 struct inode *inode = de->d_inode;
329 struct fuse_conn *fc = get_fuse_conn(inode);
330 struct fuse_file *ff = file->private_data;
331 struct fuse_req *req;
332 struct fuse_fsync_in inarg;
335 if (is_bad_inode(inode))
338 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
342 * Start writeback against all dirty pages of the inode, then
343 * wait for all outstanding writes, before sending the FSYNC
346 err = write_inode_now(inode, 0);
350 fuse_sync_writes(inode);
352 req = fuse_get_req(fc);
356 memset(&inarg, 0, sizeof(inarg));
358 inarg.fsync_flags = datasync ? 1 : 0;
359 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
360 req->in.h.nodeid = get_node_id(inode);
362 req->in.args[0].size = sizeof(inarg);
363 req->in.args[0].value = &inarg;
364 fuse_request_send(fc, req);
365 err = req->out.h.error;
366 fuse_put_request(fc, req);
367 if (err == -ENOSYS) {
377 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
379 return fuse_fsync_common(file, de, datasync, 0);
382 void fuse_read_fill(struct fuse_req *req, struct file *file,
383 struct inode *inode, loff_t pos, size_t count, int opcode)
385 struct fuse_read_in *inarg = &req->misc.read.in;
386 struct fuse_file *ff = file->private_data;
391 inarg->flags = file->f_flags;
392 req->in.h.opcode = opcode;
393 req->in.h.nodeid = get_node_id(inode);
395 req->in.args[0].size = sizeof(struct fuse_read_in);
396 req->in.args[0].value = inarg;
398 req->out.numargs = 1;
399 req->out.args[0].size = count;
402 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
403 struct inode *inode, loff_t pos, size_t count,
406 struct fuse_conn *fc = get_fuse_conn(inode);
408 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
410 struct fuse_read_in *inarg = &req->misc.read.in;
412 inarg->read_flags |= FUSE_READ_LOCKOWNER;
413 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
415 fuse_request_send(fc, req);
416 return req->out.args[0].size;
419 static void fuse_read_update_size(struct inode *inode, loff_t size,
422 struct fuse_conn *fc = get_fuse_conn(inode);
423 struct fuse_inode *fi = get_fuse_inode(inode);
425 spin_lock(&fc->lock);
426 if (attr_ver == fi->attr_version && size < inode->i_size) {
427 fi->attr_version = ++fc->attr_version;
428 i_size_write(inode, size);
430 spin_unlock(&fc->lock);
433 static int fuse_readpage(struct file *file, struct page *page)
435 struct inode *inode = page->mapping->host;
436 struct fuse_conn *fc = get_fuse_conn(inode);
437 struct fuse_req *req;
439 loff_t pos = page_offset(page);
440 size_t count = PAGE_CACHE_SIZE;
445 if (is_bad_inode(inode))
449 * Page writeback can extend beyond the liftime of the
450 * page-cache page, so make sure we read a properly synced
453 fuse_wait_on_page_writeback(inode, page->index);
455 req = fuse_get_req(fc);
460 attr_ver = fuse_get_attr_version(fc);
462 req->out.page_zeroing = 1;
463 req->out.argpages = 1;
465 req->pages[0] = page;
466 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
467 err = req->out.h.error;
468 fuse_put_request(fc, req);
472 * Short read means EOF. If file size is larger, truncate it
474 if (num_read < count)
475 fuse_read_update_size(inode, pos + num_read, attr_ver);
477 SetPageUptodate(page);
480 fuse_invalidate_attr(inode); /* atime changed */
486 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
489 size_t count = req->misc.read.in.size;
490 size_t num_read = req->out.args[0].size;
491 struct inode *inode = req->pages[0]->mapping->host;
494 * Short read means EOF. If file size is larger, truncate it
496 if (!req->out.h.error && num_read < count) {
497 loff_t pos = page_offset(req->pages[0]) + num_read;
498 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
501 fuse_invalidate_attr(inode); /* atime changed */
503 for (i = 0; i < req->num_pages; i++) {
504 struct page *page = req->pages[i];
505 if (!req->out.h.error)
506 SetPageUptodate(page);
512 fuse_file_put(req->ff);
515 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
518 struct fuse_conn *fc = get_fuse_conn(inode);
519 loff_t pos = page_offset(req->pages[0]);
520 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
522 req->out.argpages = 1;
523 req->out.page_zeroing = 1;
524 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
525 req->misc.read.attr_ver = fuse_get_attr_version(fc);
526 if (fc->async_read) {
527 struct fuse_file *ff = file->private_data;
528 req->ff = fuse_file_get(ff);
529 req->end = fuse_readpages_end;
530 fuse_request_send_background(fc, req);
532 fuse_request_send(fc, req);
533 fuse_readpages_end(fc, req);
534 fuse_put_request(fc, req);
538 struct fuse_fill_data {
539 struct fuse_req *req;
544 static int fuse_readpages_fill(void *_data, struct page *page)
546 struct fuse_fill_data *data = _data;
547 struct fuse_req *req = data->req;
548 struct inode *inode = data->inode;
549 struct fuse_conn *fc = get_fuse_conn(inode);
551 fuse_wait_on_page_writeback(inode, page->index);
553 if (req->num_pages &&
554 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
555 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
556 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
557 fuse_send_readpages(req, data->file, inode);
558 data->req = req = fuse_get_req(fc);
564 req->pages[req->num_pages] = page;
569 static int fuse_readpages(struct file *file, struct address_space *mapping,
570 struct list_head *pages, unsigned nr_pages)
572 struct inode *inode = mapping->host;
573 struct fuse_conn *fc = get_fuse_conn(inode);
574 struct fuse_fill_data data;
578 if (is_bad_inode(inode))
583 data.req = fuse_get_req(fc);
584 err = PTR_ERR(data.req);
585 if (IS_ERR(data.req))
588 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
590 if (data.req->num_pages)
591 fuse_send_readpages(data.req, file, inode);
593 fuse_put_request(fc, data.req);
599 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
600 unsigned long nr_segs, loff_t pos)
602 struct inode *inode = iocb->ki_filp->f_mapping->host;
604 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
607 * If trying to read past EOF, make sure the i_size
608 * attribute is up-to-date.
610 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
615 return generic_file_aio_read(iocb, iov, nr_segs, pos);
618 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
619 struct inode *inode, loff_t pos, size_t count)
621 struct fuse_conn *fc = get_fuse_conn(inode);
622 struct fuse_write_in *inarg = &req->misc.write.in;
623 struct fuse_write_out *outarg = &req->misc.write.out;
628 req->in.h.opcode = FUSE_WRITE;
629 req->in.h.nodeid = get_node_id(inode);
632 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
634 req->in.args[0].size = sizeof(struct fuse_write_in);
635 req->in.args[0].value = inarg;
636 req->in.args[1].size = count;
637 req->out.numargs = 1;
638 req->out.args[0].size = sizeof(struct fuse_write_out);
639 req->out.args[0].value = outarg;
642 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
643 struct inode *inode, loff_t pos, size_t count,
646 struct fuse_conn *fc = get_fuse_conn(inode);
647 struct fuse_write_in *inarg = &req->misc.write.in;
649 fuse_write_fill(req, file->private_data, inode, pos, count);
650 inarg->flags = file->f_flags;
652 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
653 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
655 fuse_request_send(fc, req);
656 return req->misc.write.out.size;
659 static int fuse_write_begin(struct file *file, struct address_space *mapping,
660 loff_t pos, unsigned len, unsigned flags,
661 struct page **pagep, void **fsdata)
663 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
665 *pagep = grab_cache_page_write_begin(mapping, index, flags);
671 static void fuse_write_update_size(struct inode *inode, loff_t pos)
673 struct fuse_conn *fc = get_fuse_conn(inode);
674 struct fuse_inode *fi = get_fuse_inode(inode);
676 spin_lock(&fc->lock);
677 fi->attr_version = ++fc->attr_version;
678 if (pos > inode->i_size)
679 i_size_write(inode, pos);
680 spin_unlock(&fc->lock);
683 static int fuse_buffered_write(struct file *file, struct inode *inode,
684 loff_t pos, unsigned count, struct page *page)
688 struct fuse_conn *fc = get_fuse_conn(inode);
689 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
690 struct fuse_req *req;
692 if (is_bad_inode(inode))
696 * Make sure writepages on the same page are not mixed up with
699 fuse_wait_on_page_writeback(inode, page->index);
701 req = fuse_get_req(fc);
705 req->in.argpages = 1;
707 req->pages[0] = page;
708 req->page_offset = offset;
709 nres = fuse_send_write(req, file, inode, pos, count, NULL);
710 err = req->out.h.error;
711 fuse_put_request(fc, req);
716 fuse_write_update_size(inode, pos);
717 if (count == PAGE_CACHE_SIZE)
718 SetPageUptodate(page);
720 fuse_invalidate_attr(inode);
721 return err ? err : nres;
724 static int fuse_write_end(struct file *file, struct address_space *mapping,
725 loff_t pos, unsigned len, unsigned copied,
726 struct page *page, void *fsdata)
728 struct inode *inode = mapping->host;
732 res = fuse_buffered_write(file, inode, pos, copied, page);
735 page_cache_release(page);
739 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
740 struct inode *inode, loff_t pos,
747 for (i = 0; i < req->num_pages; i++)
748 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
750 res = fuse_send_write(req, file, inode, pos, count, NULL);
752 offset = req->page_offset;
754 for (i = 0; i < req->num_pages; i++) {
755 struct page *page = req->pages[i];
757 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
758 SetPageUptodate(page);
760 if (count > PAGE_CACHE_SIZE - offset)
761 count -= PAGE_CACHE_SIZE - offset;
767 page_cache_release(page);
773 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
774 struct address_space *mapping,
775 struct iov_iter *ii, loff_t pos)
777 struct fuse_conn *fc = get_fuse_conn(mapping->host);
778 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
782 req->in.argpages = 1;
783 req->page_offset = offset;
788 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
789 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
792 bytes = min_t(size_t, bytes, fc->max_write - count);
796 if (iov_iter_fault_in_readable(ii, bytes))
800 page = grab_cache_page_write_begin(mapping, index, 0);
805 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
807 flush_dcache_page(page);
811 page_cache_release(page);
812 bytes = min(bytes, iov_iter_single_seg_count(ii));
817 req->pages[req->num_pages] = page;
820 iov_iter_advance(ii, tmp);
824 if (offset == PAGE_CACHE_SIZE)
829 } while (iov_iter_count(ii) && count < fc->max_write &&
830 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
832 return count > 0 ? count : err;
835 static ssize_t fuse_perform_write(struct file *file,
836 struct address_space *mapping,
837 struct iov_iter *ii, loff_t pos)
839 struct inode *inode = mapping->host;
840 struct fuse_conn *fc = get_fuse_conn(inode);
844 if (is_bad_inode(inode))
848 struct fuse_req *req;
851 req = fuse_get_req(fc);
857 count = fuse_fill_write_pages(req, mapping, ii, pos);
863 num_written = fuse_send_write_pages(req, file, inode,
865 err = req->out.h.error;
870 /* break out of the loop on short write */
871 if (num_written != count)
875 fuse_put_request(fc, req);
876 } while (!err && iov_iter_count(ii));
879 fuse_write_update_size(inode, pos);
881 fuse_invalidate_attr(inode);
883 return res > 0 ? res : err;
886 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
887 unsigned long nr_segs, loff_t pos)
889 struct file *file = iocb->ki_filp;
890 struct address_space *mapping = file->f_mapping;
893 struct inode *inode = mapping->host;
897 WARN_ON(iocb->ki_pos != pos);
899 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
903 mutex_lock(&inode->i_mutex);
904 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
906 /* We can write back this queue in page reclaim */
907 current->backing_dev_info = mapping->backing_dev_info;
909 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
916 err = file_remove_suid(file);
920 file_update_time(file);
922 iov_iter_init(&i, iov, nr_segs, count, 0);
923 written = fuse_perform_write(file, mapping, &i, pos);
925 iocb->ki_pos = pos + written;
928 current->backing_dev_info = NULL;
929 mutex_unlock(&inode->i_mutex);
931 return written ? written : err;
934 static void fuse_release_user_pages(struct fuse_req *req, int write)
938 for (i = 0; i < req->num_pages; i++) {
939 struct page *page = req->pages[i];
941 set_page_dirty_lock(page);
946 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
947 size_t *nbytesp, int write)
949 size_t nbytes = *nbytesp;
950 unsigned long user_addr = (unsigned long) buf;
951 unsigned offset = user_addr & ~PAGE_MASK;
954 /* Special case for kernel I/O: can copy directly into the buffer */
955 if (segment_eq(get_fs(), KERNEL_DS)) {
957 req->in.args[1].value = (void *) user_addr;
959 req->out.args[0].value = (void *) user_addr;
964 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
965 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
966 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
967 down_read(¤t->mm->mmap_sem);
968 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
969 0, req->pages, NULL);
970 up_read(¤t->mm->mmap_sem);
974 req->num_pages = npages;
975 req->page_offset = offset;
978 req->in.argpages = 1;
980 req->out.argpages = 1;
982 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
983 *nbytesp = min(*nbytesp, nbytes);
988 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
989 size_t count, loff_t *ppos, int write)
991 struct inode *inode = file->f_path.dentry->d_inode;
992 struct fuse_conn *fc = get_fuse_conn(inode);
993 size_t nmax = write ? fc->max_write : fc->max_read;
996 struct fuse_req *req;
998 req = fuse_get_req(fc);
1000 return PTR_ERR(req);
1004 size_t nbytes = min(count, nmax);
1005 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1012 nres = fuse_send_write(req, file, inode, pos, nbytes,
1015 nres = fuse_send_read(req, file, inode, pos, nbytes,
1017 fuse_release_user_pages(req, !write);
1018 if (req->out.h.error) {
1020 res = req->out.h.error;
1022 } else if (nres > nbytes) {
1033 fuse_put_request(fc, req);
1034 req = fuse_get_req(fc);
1039 fuse_put_request(fc, req);
1046 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1047 size_t count, loff_t *ppos)
1050 struct inode *inode = file->f_path.dentry->d_inode;
1052 if (is_bad_inode(inode))
1055 res = fuse_direct_io(file, buf, count, ppos, 0);
1057 fuse_invalidate_attr(inode);
1062 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1063 size_t count, loff_t *ppos)
1065 struct inode *inode = file->f_path.dentry->d_inode;
1068 if (is_bad_inode(inode))
1071 /* Don't allow parallel writes to the same file */
1072 mutex_lock(&inode->i_mutex);
1073 res = generic_write_checks(file, ppos, &count, 0);
1075 res = fuse_direct_io(file, buf, count, ppos, 1);
1077 fuse_write_update_size(inode, *ppos);
1079 mutex_unlock(&inode->i_mutex);
1081 fuse_invalidate_attr(inode);
1086 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1088 __free_page(req->pages[0]);
1089 fuse_file_put(req->ff);
1092 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1094 struct inode *inode = req->inode;
1095 struct fuse_inode *fi = get_fuse_inode(inode);
1096 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1098 list_del(&req->writepages_entry);
1099 dec_bdi_stat(bdi, BDI_WRITEBACK);
1100 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1101 bdi_writeout_inc(bdi);
1102 wake_up(&fi->page_waitq);
1105 /* Called under fc->lock, may release and reacquire it */
1106 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1107 __releases(&fc->lock)
1108 __acquires(&fc->lock)
1110 struct fuse_inode *fi = get_fuse_inode(req->inode);
1111 loff_t size = i_size_read(req->inode);
1112 struct fuse_write_in *inarg = &req->misc.write.in;
1117 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1118 inarg->size = PAGE_CACHE_SIZE;
1119 } else if (inarg->offset < size) {
1120 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1122 /* Got truncated off completely */
1126 req->in.args[1].size = inarg->size;
1128 fuse_request_send_background_locked(fc, req);
1132 fuse_writepage_finish(fc, req);
1133 spin_unlock(&fc->lock);
1134 fuse_writepage_free(fc, req);
1135 fuse_put_request(fc, req);
1136 spin_lock(&fc->lock);
1140 * If fi->writectr is positive (no truncate or fsync going on) send
1141 * all queued writepage requests.
1143 * Called with fc->lock
1145 void fuse_flush_writepages(struct inode *inode)
1146 __releases(&fc->lock)
1147 __acquires(&fc->lock)
1149 struct fuse_conn *fc = get_fuse_conn(inode);
1150 struct fuse_inode *fi = get_fuse_inode(inode);
1151 struct fuse_req *req;
1153 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1154 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1155 list_del_init(&req->list);
1156 fuse_send_writepage(fc, req);
1160 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1162 struct inode *inode = req->inode;
1163 struct fuse_inode *fi = get_fuse_inode(inode);
1165 mapping_set_error(inode->i_mapping, req->out.h.error);
1166 spin_lock(&fc->lock);
1168 fuse_writepage_finish(fc, req);
1169 spin_unlock(&fc->lock);
1170 fuse_writepage_free(fc, req);
1173 static int fuse_writepage_locked(struct page *page)
1175 struct address_space *mapping = page->mapping;
1176 struct inode *inode = mapping->host;
1177 struct fuse_conn *fc = get_fuse_conn(inode);
1178 struct fuse_inode *fi = get_fuse_inode(inode);
1179 struct fuse_req *req;
1180 struct fuse_file *ff;
1181 struct page *tmp_page;
1183 set_page_writeback(page);
1185 req = fuse_request_alloc_nofs();
1189 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1193 spin_lock(&fc->lock);
1194 BUG_ON(list_empty(&fi->write_files));
1195 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1196 req->ff = fuse_file_get(ff);
1197 spin_unlock(&fc->lock);
1199 fuse_write_fill(req, ff, inode, page_offset(page), 0);
1201 copy_highpage(tmp_page, page);
1202 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1203 req->in.argpages = 1;
1205 req->pages[0] = tmp_page;
1206 req->page_offset = 0;
1207 req->end = fuse_writepage_end;
1210 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1211 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1212 end_page_writeback(page);
1214 spin_lock(&fc->lock);
1215 list_add(&req->writepages_entry, &fi->writepages);
1216 list_add_tail(&req->list, &fi->queued_writes);
1217 fuse_flush_writepages(inode);
1218 spin_unlock(&fc->lock);
1223 fuse_request_free(req);
1225 end_page_writeback(page);
1229 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1233 err = fuse_writepage_locked(page);
1239 static int fuse_launder_page(struct page *page)
1242 if (clear_page_dirty_for_io(page)) {
1243 struct inode *inode = page->mapping->host;
1244 err = fuse_writepage_locked(page);
1246 fuse_wait_on_page_writeback(inode, page->index);
1252 * Write back dirty pages now, because there may not be any suitable
1255 static void fuse_vma_close(struct vm_area_struct *vma)
1257 filemap_write_and_wait(vma->vm_file->f_mapping);
1261 * Wait for writeback against this page to complete before allowing it
1262 * to be marked dirty again, and hence written back again, possibly
1263 * before the previous writepage completed.
1265 * Block here, instead of in ->writepage(), so that the userspace fs
1266 * can only block processes actually operating on the filesystem.
1268 * Otherwise unprivileged userspace fs would be able to block
1273 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1275 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1277 struct page *page = vmf->page;
1279 * Don't use page->mapping as it may become NULL from a
1280 * concurrent truncate.
1282 struct inode *inode = vma->vm_file->f_mapping->host;
1284 fuse_wait_on_page_writeback(inode, page->index);
1288 static struct vm_operations_struct fuse_file_vm_ops = {
1289 .close = fuse_vma_close,
1290 .fault = filemap_fault,
1291 .page_mkwrite = fuse_page_mkwrite,
1294 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1296 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1297 struct inode *inode = file->f_dentry->d_inode;
1298 struct fuse_conn *fc = get_fuse_conn(inode);
1299 struct fuse_inode *fi = get_fuse_inode(inode);
1300 struct fuse_file *ff = file->private_data;
1302 * file may be written through mmap, so chain it onto the
1303 * inodes's write_file list
1305 spin_lock(&fc->lock);
1306 if (list_empty(&ff->write_entry))
1307 list_add(&ff->write_entry, &fi->write_files);
1308 spin_unlock(&fc->lock);
1310 file_accessed(file);
1311 vma->vm_ops = &fuse_file_vm_ops;
1315 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1317 /* Can't provide the coherency needed for MAP_SHARED */
1318 if (vma->vm_flags & VM_MAYSHARE)
1321 invalidate_inode_pages2(file->f_mapping);
1323 return generic_file_mmap(file, vma);
1326 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1327 struct file_lock *fl)
1329 switch (ffl->type) {
1335 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1336 ffl->end < ffl->start)
1339 fl->fl_start = ffl->start;
1340 fl->fl_end = ffl->end;
1341 fl->fl_pid = ffl->pid;
1347 fl->fl_type = ffl->type;
1351 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1352 const struct file_lock *fl, int opcode, pid_t pid,
1355 struct inode *inode = file->f_path.dentry->d_inode;
1356 struct fuse_conn *fc = get_fuse_conn(inode);
1357 struct fuse_file *ff = file->private_data;
1358 struct fuse_lk_in *arg = &req->misc.lk_in;
1361 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1362 arg->lk.start = fl->fl_start;
1363 arg->lk.end = fl->fl_end;
1364 arg->lk.type = fl->fl_type;
1367 arg->lk_flags |= FUSE_LK_FLOCK;
1368 req->in.h.opcode = opcode;
1369 req->in.h.nodeid = get_node_id(inode);
1370 req->in.numargs = 1;
1371 req->in.args[0].size = sizeof(*arg);
1372 req->in.args[0].value = arg;
1375 static int fuse_getlk(struct file *file, struct file_lock *fl)
1377 struct inode *inode = file->f_path.dentry->d_inode;
1378 struct fuse_conn *fc = get_fuse_conn(inode);
1379 struct fuse_req *req;
1380 struct fuse_lk_out outarg;
1383 req = fuse_get_req(fc);
1385 return PTR_ERR(req);
1387 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1388 req->out.numargs = 1;
1389 req->out.args[0].size = sizeof(outarg);
1390 req->out.args[0].value = &outarg;
1391 fuse_request_send(fc, req);
1392 err = req->out.h.error;
1393 fuse_put_request(fc, req);
1395 err = convert_fuse_file_lock(&outarg.lk, fl);
1400 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1402 struct inode *inode = file->f_path.dentry->d_inode;
1403 struct fuse_conn *fc = get_fuse_conn(inode);
1404 struct fuse_req *req;
1405 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1406 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1409 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1410 /* NLM needs asynchronous locks, which we don't support yet */
1414 /* Unlock on close is handled by the flush method */
1415 if (fl->fl_flags & FL_CLOSE)
1418 req = fuse_get_req(fc);
1420 return PTR_ERR(req);
1422 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1423 fuse_request_send(fc, req);
1424 err = req->out.h.error;
1425 /* locking is restartable */
1428 fuse_put_request(fc, req);
1432 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1434 struct inode *inode = file->f_path.dentry->d_inode;
1435 struct fuse_conn *fc = get_fuse_conn(inode);
1438 if (cmd == F_CANCELLK) {
1440 } else if (cmd == F_GETLK) {
1442 posix_test_lock(file, fl);
1445 err = fuse_getlk(file, fl);
1448 err = posix_lock_file(file, fl, NULL);
1450 err = fuse_setlk(file, fl, 0);
1455 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1457 struct inode *inode = file->f_path.dentry->d_inode;
1458 struct fuse_conn *fc = get_fuse_conn(inode);
1462 err = flock_lock_file_wait(file, fl);
1464 /* emulate flock with POSIX locks */
1465 fl->fl_owner = (fl_owner_t) file;
1466 err = fuse_setlk(file, fl, 1);
1472 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1474 struct inode *inode = mapping->host;
1475 struct fuse_conn *fc = get_fuse_conn(inode);
1476 struct fuse_req *req;
1477 struct fuse_bmap_in inarg;
1478 struct fuse_bmap_out outarg;
1481 if (!inode->i_sb->s_bdev || fc->no_bmap)
1484 req = fuse_get_req(fc);
1488 memset(&inarg, 0, sizeof(inarg));
1489 inarg.block = block;
1490 inarg.blocksize = inode->i_sb->s_blocksize;
1491 req->in.h.opcode = FUSE_BMAP;
1492 req->in.h.nodeid = get_node_id(inode);
1493 req->in.numargs = 1;
1494 req->in.args[0].size = sizeof(inarg);
1495 req->in.args[0].value = &inarg;
1496 req->out.numargs = 1;
1497 req->out.args[0].size = sizeof(outarg);
1498 req->out.args[0].value = &outarg;
1499 fuse_request_send(fc, req);
1500 err = req->out.h.error;
1501 fuse_put_request(fc, req);
1505 return err ? 0 : outarg.block;
1508 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1511 struct inode *inode = file->f_path.dentry->d_inode;
1513 mutex_lock(&inode->i_mutex);
1516 retval = fuse_update_attributes(inode, NULL, file, NULL);
1519 offset += i_size_read(inode);
1522 offset += file->f_pos;
1525 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1526 if (offset != file->f_pos) {
1527 file->f_pos = offset;
1528 file->f_version = 0;
1533 mutex_unlock(&inode->i_mutex);
1537 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1538 unsigned int nr_segs, size_t bytes, bool to_user)
1546 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1548 while (iov_iter_count(&ii)) {
1549 struct page *page = pages[page_idx++];
1550 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1553 kaddr = map = kmap(page);
1556 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1557 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1558 size_t copy = min(todo, iov_len);
1562 left = copy_from_user(kaddr, uaddr, copy);
1564 left = copy_to_user(uaddr, kaddr, copy);
1569 iov_iter_advance(&ii, copy);
1581 * For ioctls, there is no generic way to determine how much memory
1582 * needs to be read and/or written. Furthermore, ioctls are allowed
1583 * to dereference the passed pointer, so the parameter requires deep
1584 * copying but FUSE has no idea whatsoever about what to copy in or
1587 * This is solved by allowing FUSE server to retry ioctl with
1588 * necessary in/out iovecs. Let's assume the ioctl implementation
1589 * needs to read in the following structure.
1596 * On the first callout to FUSE server, inarg->in_size and
1597 * inarg->out_size will be NULL; then, the server completes the ioctl
1598 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1599 * the actual iov array to
1601 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1603 * which tells FUSE to copy in the requested area and retry the ioctl.
1604 * On the second round, the server has access to the structure and
1605 * from that it can tell what to look for next, so on the invocation,
1606 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1608 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1609 * { .iov_base = a.buf, .iov_len = a.buflen } }
1611 * FUSE will copy both struct a and the pointed buffer from the
1612 * process doing the ioctl and retry ioctl with both struct a and the
1615 * This time, FUSE server has everything it needs and completes ioctl
1616 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1618 * Copying data out works the same way.
1620 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1621 * automatically initializes in and out iovs by decoding @cmd with
1622 * _IOC_* macros and the server is not allowed to request RETRY. This
1623 * limits ioctl data transfers to well-formed ioctls and is the forced
1624 * behavior for all FUSE servers.
1626 static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
1627 unsigned long arg, unsigned int flags)
1629 struct inode *inode = file->f_dentry->d_inode;
1630 struct fuse_file *ff = file->private_data;
1631 struct fuse_conn *fc = get_fuse_conn(inode);
1632 struct fuse_ioctl_in inarg = {
1638 struct fuse_ioctl_out outarg;
1639 struct fuse_req *req = NULL;
1640 struct page **pages = NULL;
1641 struct page *iov_page = NULL;
1642 struct iovec *in_iov = NULL, *out_iov = NULL;
1643 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1644 size_t in_size, out_size, transferred;
1647 /* assume all the iovs returned by client always fits in a page */
1648 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1650 if (!fuse_allow_task(fc, current))
1654 if (is_bad_inode(inode))
1658 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1659 iov_page = alloc_page(GFP_KERNEL);
1660 if (!pages || !iov_page)
1664 * If restricted, initialize IO parameters as encoded in @cmd.
1665 * RETRY from server is not allowed.
1667 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1668 struct iovec *iov = page_address(iov_page);
1670 iov->iov_base = (void __user *)arg;
1671 iov->iov_len = _IOC_SIZE(cmd);
1673 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1678 if (_IOC_DIR(cmd) & _IOC_READ) {
1685 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1686 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1689 * Out data can be used either for actual out data or iovs,
1690 * make sure there always is at least one page.
1692 out_size = max_t(size_t, out_size, PAGE_SIZE);
1693 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1695 /* make sure there are enough buffer pages and init request with them */
1697 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1699 while (num_pages < max_pages) {
1700 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1701 if (!pages[num_pages])
1706 req = fuse_get_req(fc);
1712 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1713 req->num_pages = num_pages;
1715 /* okay, let's send it to the client */
1716 req->in.h.opcode = FUSE_IOCTL;
1717 req->in.h.nodeid = get_node_id(inode);
1718 req->in.numargs = 1;
1719 req->in.args[0].size = sizeof(inarg);
1720 req->in.args[0].value = &inarg;
1723 req->in.args[1].size = in_size;
1724 req->in.argpages = 1;
1726 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1732 req->out.numargs = 2;
1733 req->out.args[0].size = sizeof(outarg);
1734 req->out.args[0].value = &outarg;
1735 req->out.args[1].size = out_size;
1736 req->out.argpages = 1;
1737 req->out.argvar = 1;
1739 fuse_request_send(fc, req);
1740 err = req->out.h.error;
1741 transferred = req->out.args[1].size;
1742 fuse_put_request(fc, req);
1747 /* did it ask for retry? */
1748 if (outarg.flags & FUSE_IOCTL_RETRY) {
1751 /* no retry if in restricted mode */
1753 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1756 in_iovs = outarg.in_iovs;
1757 out_iovs = outarg.out_iovs;
1760 * Make sure things are in boundary, separate checks
1761 * are to protect against overflow.
1764 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1765 out_iovs > FUSE_IOCTL_MAX_IOV ||
1766 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1770 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1773 /* okay, copy in iovs and retry */
1774 vaddr = kmap_atomic(pages[0], KM_USER0);
1775 memcpy(page_address(iov_page), vaddr, transferred);
1776 kunmap_atomic(vaddr, KM_USER0);
1778 in_iov = page_address(iov_page);
1779 out_iov = in_iov + in_iovs;
1785 if (transferred > inarg.out_size)
1788 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1791 fuse_put_request(fc, req);
1793 __free_page(iov_page);
1795 __free_page(pages[--num_pages]);
1798 return err ? err : outarg.result;
1801 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1804 return fuse_file_do_ioctl(file, cmd, arg, 0);
1807 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1810 return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
1814 * All files which have been polled are linked to RB tree
1815 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1816 * find the matching one.
1818 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1819 struct rb_node **parent_out)
1821 struct rb_node **link = &fc->polled_files.rb_node;
1822 struct rb_node *last = NULL;
1825 struct fuse_file *ff;
1828 ff = rb_entry(last, struct fuse_file, polled_node);
1831 link = &last->rb_left;
1832 else if (kh > ff->kh)
1833 link = &last->rb_right;
1844 * The file is about to be polled. Make sure it's on the polled_files
1845 * RB tree. Note that files once added to the polled_files tree are
1846 * not removed before the file is released. This is because a file
1847 * polled once is likely to be polled again.
1849 static void fuse_register_polled_file(struct fuse_conn *fc,
1850 struct fuse_file *ff)
1852 spin_lock(&fc->lock);
1853 if (RB_EMPTY_NODE(&ff->polled_node)) {
1854 struct rb_node **link, *parent;
1856 link = fuse_find_polled_node(fc, ff->kh, &parent);
1858 rb_link_node(&ff->polled_node, parent, link);
1859 rb_insert_color(&ff->polled_node, &fc->polled_files);
1861 spin_unlock(&fc->lock);
1864 static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1866 struct inode *inode = file->f_dentry->d_inode;
1867 struct fuse_file *ff = file->private_data;
1868 struct fuse_conn *fc = get_fuse_conn(inode);
1869 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1870 struct fuse_poll_out outarg;
1871 struct fuse_req *req;
1875 return DEFAULT_POLLMASK;
1877 poll_wait(file, &ff->poll_wait, wait);
1880 * Ask for notification iff there's someone waiting for it.
1881 * The client may ignore the flag and always notify.
1883 if (waitqueue_active(&ff->poll_wait)) {
1884 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1885 fuse_register_polled_file(fc, ff);
1888 req = fuse_get_req(fc);
1890 return PTR_ERR(req);
1892 req->in.h.opcode = FUSE_POLL;
1893 req->in.h.nodeid = get_node_id(inode);
1894 req->in.numargs = 1;
1895 req->in.args[0].size = sizeof(inarg);
1896 req->in.args[0].value = &inarg;
1897 req->out.numargs = 1;
1898 req->out.args[0].size = sizeof(outarg);
1899 req->out.args[0].value = &outarg;
1900 fuse_request_send(fc, req);
1901 err = req->out.h.error;
1902 fuse_put_request(fc, req);
1905 return outarg.revents;
1906 if (err == -ENOSYS) {
1908 return DEFAULT_POLLMASK;
1914 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1915 * wakes up the poll waiters.
1917 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1918 struct fuse_notify_poll_wakeup_out *outarg)
1920 u64 kh = outarg->kh;
1921 struct rb_node **link;
1923 spin_lock(&fc->lock);
1925 link = fuse_find_polled_node(fc, kh, NULL);
1927 struct fuse_file *ff;
1929 ff = rb_entry(*link, struct fuse_file, polled_node);
1930 wake_up_interruptible_sync(&ff->poll_wait);
1933 spin_unlock(&fc->lock);
1937 static const struct file_operations fuse_file_operations = {
1938 .llseek = fuse_file_llseek,
1939 .read = do_sync_read,
1940 .aio_read = fuse_file_aio_read,
1941 .write = do_sync_write,
1942 .aio_write = fuse_file_aio_write,
1943 .mmap = fuse_file_mmap,
1945 .flush = fuse_flush,
1946 .release = fuse_release,
1947 .fsync = fuse_fsync,
1948 .lock = fuse_file_lock,
1949 .flock = fuse_file_flock,
1950 .splice_read = generic_file_splice_read,
1951 .unlocked_ioctl = fuse_file_ioctl,
1952 .compat_ioctl = fuse_file_compat_ioctl,
1953 .poll = fuse_file_poll,
1956 static const struct file_operations fuse_direct_io_file_operations = {
1957 .llseek = fuse_file_llseek,
1958 .read = fuse_direct_read,
1959 .write = fuse_direct_write,
1960 .mmap = fuse_direct_mmap,
1962 .flush = fuse_flush,
1963 .release = fuse_release,
1964 .fsync = fuse_fsync,
1965 .lock = fuse_file_lock,
1966 .flock = fuse_file_flock,
1967 .unlocked_ioctl = fuse_file_ioctl,
1968 .compat_ioctl = fuse_file_compat_ioctl,
1969 .poll = fuse_file_poll,
1970 /* no splice_read */
1973 static const struct address_space_operations fuse_file_aops = {
1974 .readpage = fuse_readpage,
1975 .writepage = fuse_writepage,
1976 .launder_page = fuse_launder_page,
1977 .write_begin = fuse_write_begin,
1978 .write_end = fuse_write_end,
1979 .readpages = fuse_readpages,
1980 .set_page_dirty = __set_page_dirty_nobuffers,
1984 void fuse_init_file_inode(struct inode *inode)
1986 inode->i_fop = &fuse_file_operations;
1987 inode->i_data.a_ops = &fuse_file_aops;