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 fuse_conn *fc, u64 nodeid, struct file *file,
19 int opcode, struct fuse_open_out *outargp)
21 struct fuse_open_in inarg;
25 req = fuse_get_req(fc);
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
33 req->in.h.opcode = opcode;
34 req->in.h.nodeid = nodeid;
36 req->in.args[0].size = sizeof(inarg);
37 req->in.args[0].value = &inarg;
39 req->out.args[0].size = sizeof(*outargp);
40 req->out.args[0].value = outargp;
41 fuse_request_send(fc, req);
42 err = req->out.h.error;
43 fuse_put_request(fc, req);
48 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
52 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 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 static int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
107 struct fuse_open_out outarg;
108 struct fuse_file *ff;
110 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
112 ff = fuse_file_alloc(fc);
116 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
123 outarg.open_flags &= ~FOPEN_DIRECT_IO;
127 ff->open_flags = outarg.open_flags;
128 file->private_data = fuse_file_get(ff);
133 void fuse_finish_open(struct inode *inode, struct file *file)
135 struct fuse_file *ff = file->private_data;
137 if (ff->open_flags & FOPEN_DIRECT_IO)
138 file->f_op = &fuse_direct_io_file_operations;
139 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
140 invalidate_inode_pages2(inode->i_mapping);
141 if (ff->open_flags & FOPEN_NONSEEKABLE)
142 nonseekable_open(inode, file);
145 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
147 struct fuse_conn *fc = get_fuse_conn(inode);
150 /* VFS checks this, but only _after_ ->open() */
151 if (file->f_flags & O_DIRECT)
154 err = generic_file_open(inode, file);
158 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
162 fuse_finish_open(inode, file);
167 void fuse_release_fill(struct fuse_file *ff, int flags, int opcode)
169 struct fuse_req *req = ff->reserved_req;
170 struct fuse_release_in *inarg = &req->misc.release.in;
173 inarg->flags = flags;
174 req->in.h.opcode = opcode;
175 req->in.h.nodeid = ff->nodeid;
177 req->in.args[0].size = sizeof(struct fuse_release_in);
178 req->in.args[0].value = inarg;
181 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
183 struct fuse_conn *fc;
184 struct fuse_file *ff;
185 struct fuse_req *req;
187 ff = file->private_data;
189 return 0; /* return value is ignored by VFS */
191 fc = get_fuse_conn(inode);
192 req = ff->reserved_req;
194 fuse_release_fill(ff, file->f_flags,
195 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
197 /* Hold vfsmount and dentry until release is finished */
198 path_get(&file->f_path);
199 req->misc.release.path = file->f_path;
201 spin_lock(&fc->lock);
202 list_del(&ff->write_entry);
203 if (!RB_EMPTY_NODE(&ff->polled_node))
204 rb_erase(&ff->polled_node, &fc->polled_files);
205 spin_unlock(&fc->lock);
207 wake_up_interruptible_sync(&ff->poll_wait);
209 * Normally this will send the RELEASE request, however if
210 * some asynchronous READ or WRITE requests are outstanding,
211 * the sending will be delayed.
217 static int fuse_open(struct inode *inode, struct file *file)
219 return fuse_open_common(inode, file, false);
222 static int fuse_release(struct inode *inode, struct file *file)
224 return fuse_release_common(inode, file, 0);
228 * Scramble the ID space with XTEA, so that the value of the files_struct
229 * pointer is not exposed to userspace.
231 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
233 u32 *k = fc->scramble_key;
234 u64 v = (unsigned long) id;
240 for (i = 0; i < 32; i++) {
241 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
243 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
246 return (u64) v0 + ((u64) v1 << 32);
250 * Check if page is under writeback
252 * This is currently done by walking the list of writepage requests
253 * for the inode, which can be pretty inefficient.
255 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
257 struct fuse_conn *fc = get_fuse_conn(inode);
258 struct fuse_inode *fi = get_fuse_inode(inode);
259 struct fuse_req *req;
262 spin_lock(&fc->lock);
263 list_for_each_entry(req, &fi->writepages, writepages_entry) {
266 BUG_ON(req->inode != inode);
267 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
268 if (curr_index == index) {
273 spin_unlock(&fc->lock);
279 * Wait for page writeback to be completed.
281 * Since fuse doesn't rely on the VM writeback tracking, this has to
282 * use some other means.
284 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
286 struct fuse_inode *fi = get_fuse_inode(inode);
288 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
292 static int fuse_flush(struct file *file, fl_owner_t id)
294 struct inode *inode = file->f_path.dentry->d_inode;
295 struct fuse_conn *fc = get_fuse_conn(inode);
296 struct fuse_file *ff = file->private_data;
297 struct fuse_req *req;
298 struct fuse_flush_in inarg;
301 if (is_bad_inode(inode))
307 req = fuse_get_req_nofail(fc, file);
308 memset(&inarg, 0, sizeof(inarg));
310 inarg.lock_owner = fuse_lock_owner_id(fc, id);
311 req->in.h.opcode = FUSE_FLUSH;
312 req->in.h.nodeid = get_node_id(inode);
314 req->in.args[0].size = sizeof(inarg);
315 req->in.args[0].value = &inarg;
317 fuse_request_send(fc, req);
318 err = req->out.h.error;
319 fuse_put_request(fc, req);
320 if (err == -ENOSYS) {
328 * Wait for all pending writepages on the inode to finish.
330 * This is currently done by blocking further writes with FUSE_NOWRITE
331 * and waiting for all sent writes to complete.
333 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
334 * could conflict with truncation.
336 static void fuse_sync_writes(struct inode *inode)
338 fuse_set_nowrite(inode);
339 fuse_release_nowrite(inode);
342 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
345 struct inode *inode = de->d_inode;
346 struct fuse_conn *fc = get_fuse_conn(inode);
347 struct fuse_file *ff = file->private_data;
348 struct fuse_req *req;
349 struct fuse_fsync_in inarg;
352 if (is_bad_inode(inode))
355 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
359 * Start writeback against all dirty pages of the inode, then
360 * wait for all outstanding writes, before sending the FSYNC
363 err = write_inode_now(inode, 0);
367 fuse_sync_writes(inode);
369 req = fuse_get_req(fc);
373 memset(&inarg, 0, sizeof(inarg));
375 inarg.fsync_flags = datasync ? 1 : 0;
376 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
377 req->in.h.nodeid = get_node_id(inode);
379 req->in.args[0].size = sizeof(inarg);
380 req->in.args[0].value = &inarg;
381 fuse_request_send(fc, req);
382 err = req->out.h.error;
383 fuse_put_request(fc, req);
384 if (err == -ENOSYS) {
394 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
396 return fuse_fsync_common(file, de, datasync, 0);
399 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
400 size_t count, int opcode)
402 struct fuse_read_in *inarg = &req->misc.read.in;
403 struct fuse_file *ff = file->private_data;
408 inarg->flags = file->f_flags;
409 req->in.h.opcode = opcode;
410 req->in.h.nodeid = ff->nodeid;
412 req->in.args[0].size = sizeof(struct fuse_read_in);
413 req->in.args[0].value = inarg;
415 req->out.numargs = 1;
416 req->out.args[0].size = count;
419 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
420 loff_t pos, size_t count, fl_owner_t owner)
422 struct fuse_file *ff = file->private_data;
423 struct fuse_conn *fc = ff->fc;
425 fuse_read_fill(req, file, pos, count, FUSE_READ);
427 struct fuse_read_in *inarg = &req->misc.read.in;
429 inarg->read_flags |= FUSE_READ_LOCKOWNER;
430 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
432 fuse_request_send(fc, req);
433 return req->out.args[0].size;
436 static void fuse_read_update_size(struct inode *inode, loff_t size,
439 struct fuse_conn *fc = get_fuse_conn(inode);
440 struct fuse_inode *fi = get_fuse_inode(inode);
442 spin_lock(&fc->lock);
443 if (attr_ver == fi->attr_version && size < inode->i_size) {
444 fi->attr_version = ++fc->attr_version;
445 i_size_write(inode, size);
447 spin_unlock(&fc->lock);
450 static int fuse_readpage(struct file *file, struct page *page)
452 struct inode *inode = page->mapping->host;
453 struct fuse_conn *fc = get_fuse_conn(inode);
454 struct fuse_req *req;
456 loff_t pos = page_offset(page);
457 size_t count = PAGE_CACHE_SIZE;
462 if (is_bad_inode(inode))
466 * Page writeback can extend beyond the liftime of the
467 * page-cache page, so make sure we read a properly synced
470 fuse_wait_on_page_writeback(inode, page->index);
472 req = fuse_get_req(fc);
477 attr_ver = fuse_get_attr_version(fc);
479 req->out.page_zeroing = 1;
480 req->out.argpages = 1;
482 req->pages[0] = page;
483 num_read = fuse_send_read(req, file, pos, count, NULL);
484 err = req->out.h.error;
485 fuse_put_request(fc, req);
489 * Short read means EOF. If file size is larger, truncate it
491 if (num_read < count)
492 fuse_read_update_size(inode, pos + num_read, attr_ver);
494 SetPageUptodate(page);
497 fuse_invalidate_attr(inode); /* atime changed */
503 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
506 size_t count = req->misc.read.in.size;
507 size_t num_read = req->out.args[0].size;
508 struct inode *inode = req->pages[0]->mapping->host;
511 * Short read means EOF. If file size is larger, truncate it
513 if (!req->out.h.error && num_read < count) {
514 loff_t pos = page_offset(req->pages[0]) + num_read;
515 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
518 fuse_invalidate_attr(inode); /* atime changed */
520 for (i = 0; i < req->num_pages; i++) {
521 struct page *page = req->pages[i];
522 if (!req->out.h.error)
523 SetPageUptodate(page);
529 fuse_file_put(req->ff);
532 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
534 struct fuse_file *ff = file->private_data;
535 struct fuse_conn *fc = ff->fc;
536 loff_t pos = page_offset(req->pages[0]);
537 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
539 req->out.argpages = 1;
540 req->out.page_zeroing = 1;
541 fuse_read_fill(req, file, pos, count, FUSE_READ);
542 req->misc.read.attr_ver = fuse_get_attr_version(fc);
543 if (fc->async_read) {
544 req->ff = fuse_file_get(ff);
545 req->end = fuse_readpages_end;
546 fuse_request_send_background(fc, req);
548 fuse_request_send(fc, req);
549 fuse_readpages_end(fc, req);
550 fuse_put_request(fc, req);
554 struct fuse_fill_data {
555 struct fuse_req *req;
560 static int fuse_readpages_fill(void *_data, struct page *page)
562 struct fuse_fill_data *data = _data;
563 struct fuse_req *req = data->req;
564 struct inode *inode = data->inode;
565 struct fuse_conn *fc = get_fuse_conn(inode);
567 fuse_wait_on_page_writeback(inode, page->index);
569 if (req->num_pages &&
570 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
571 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
572 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
573 fuse_send_readpages(req, data->file);
574 data->req = req = fuse_get_req(fc);
580 req->pages[req->num_pages] = page;
585 static int fuse_readpages(struct file *file, struct address_space *mapping,
586 struct list_head *pages, unsigned nr_pages)
588 struct inode *inode = mapping->host;
589 struct fuse_conn *fc = get_fuse_conn(inode);
590 struct fuse_fill_data data;
594 if (is_bad_inode(inode))
599 data.req = fuse_get_req(fc);
600 err = PTR_ERR(data.req);
601 if (IS_ERR(data.req))
604 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
606 if (data.req->num_pages)
607 fuse_send_readpages(data.req, file);
609 fuse_put_request(fc, data.req);
615 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
616 unsigned long nr_segs, loff_t pos)
618 struct inode *inode = iocb->ki_filp->f_mapping->host;
620 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
623 * If trying to read past EOF, make sure the i_size
624 * attribute is up-to-date.
626 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
631 return generic_file_aio_read(iocb, iov, nr_segs, pos);
634 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
635 loff_t pos, size_t count)
637 struct fuse_write_in *inarg = &req->misc.write.in;
638 struct fuse_write_out *outarg = &req->misc.write.out;
643 req->in.h.opcode = FUSE_WRITE;
644 req->in.h.nodeid = ff->nodeid;
646 if (ff->fc->minor < 9)
647 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
649 req->in.args[0].size = sizeof(struct fuse_write_in);
650 req->in.args[0].value = inarg;
651 req->in.args[1].size = count;
652 req->out.numargs = 1;
653 req->out.args[0].size = sizeof(struct fuse_write_out);
654 req->out.args[0].value = outarg;
657 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
658 loff_t pos, size_t count, fl_owner_t owner)
660 struct fuse_file *ff = file->private_data;
661 struct fuse_conn *fc = ff->fc;
662 struct fuse_write_in *inarg = &req->misc.write.in;
664 fuse_write_fill(req, ff, pos, count);
665 inarg->flags = file->f_flags;
667 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
668 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
670 fuse_request_send(fc, req);
671 return req->misc.write.out.size;
674 static int fuse_write_begin(struct file *file, struct address_space *mapping,
675 loff_t pos, unsigned len, unsigned flags,
676 struct page **pagep, void **fsdata)
678 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
680 *pagep = grab_cache_page_write_begin(mapping, index, flags);
686 static void fuse_write_update_size(struct inode *inode, loff_t pos)
688 struct fuse_conn *fc = get_fuse_conn(inode);
689 struct fuse_inode *fi = get_fuse_inode(inode);
691 spin_lock(&fc->lock);
692 fi->attr_version = ++fc->attr_version;
693 if (pos > inode->i_size)
694 i_size_write(inode, pos);
695 spin_unlock(&fc->lock);
698 static int fuse_buffered_write(struct file *file, struct inode *inode,
699 loff_t pos, unsigned count, struct page *page)
703 struct fuse_conn *fc = get_fuse_conn(inode);
704 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
705 struct fuse_req *req;
707 if (is_bad_inode(inode))
711 * Make sure writepages on the same page are not mixed up with
714 fuse_wait_on_page_writeback(inode, page->index);
716 req = fuse_get_req(fc);
720 req->in.argpages = 1;
722 req->pages[0] = page;
723 req->page_offset = offset;
724 nres = fuse_send_write(req, file, pos, count, NULL);
725 err = req->out.h.error;
726 fuse_put_request(fc, req);
731 fuse_write_update_size(inode, pos);
732 if (count == PAGE_CACHE_SIZE)
733 SetPageUptodate(page);
735 fuse_invalidate_attr(inode);
736 return err ? err : nres;
739 static int fuse_write_end(struct file *file, struct address_space *mapping,
740 loff_t pos, unsigned len, unsigned copied,
741 struct page *page, void *fsdata)
743 struct inode *inode = mapping->host;
747 res = fuse_buffered_write(file, inode, pos, copied, page);
750 page_cache_release(page);
754 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
755 struct inode *inode, loff_t pos,
762 for (i = 0; i < req->num_pages; i++)
763 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
765 res = fuse_send_write(req, file, pos, count, NULL);
767 offset = req->page_offset;
769 for (i = 0; i < req->num_pages; i++) {
770 struct page *page = req->pages[i];
772 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
773 SetPageUptodate(page);
775 if (count > PAGE_CACHE_SIZE - offset)
776 count -= PAGE_CACHE_SIZE - offset;
782 page_cache_release(page);
788 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
789 struct address_space *mapping,
790 struct iov_iter *ii, loff_t pos)
792 struct fuse_conn *fc = get_fuse_conn(mapping->host);
793 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
797 req->in.argpages = 1;
798 req->page_offset = offset;
803 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
804 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
807 bytes = min_t(size_t, bytes, fc->max_write - count);
811 if (iov_iter_fault_in_readable(ii, bytes))
815 page = grab_cache_page_write_begin(mapping, index, 0);
820 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
822 flush_dcache_page(page);
826 page_cache_release(page);
827 bytes = min(bytes, iov_iter_single_seg_count(ii));
832 req->pages[req->num_pages] = page;
835 iov_iter_advance(ii, tmp);
839 if (offset == PAGE_CACHE_SIZE)
844 } while (iov_iter_count(ii) && count < fc->max_write &&
845 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
847 return count > 0 ? count : err;
850 static ssize_t fuse_perform_write(struct file *file,
851 struct address_space *mapping,
852 struct iov_iter *ii, loff_t pos)
854 struct inode *inode = mapping->host;
855 struct fuse_conn *fc = get_fuse_conn(inode);
859 if (is_bad_inode(inode))
863 struct fuse_req *req;
866 req = fuse_get_req(fc);
872 count = fuse_fill_write_pages(req, mapping, ii, pos);
878 num_written = fuse_send_write_pages(req, file, inode,
880 err = req->out.h.error;
885 /* break out of the loop on short write */
886 if (num_written != count)
890 fuse_put_request(fc, req);
891 } while (!err && iov_iter_count(ii));
894 fuse_write_update_size(inode, pos);
896 fuse_invalidate_attr(inode);
898 return res > 0 ? res : err;
901 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
902 unsigned long nr_segs, loff_t pos)
904 struct file *file = iocb->ki_filp;
905 struct address_space *mapping = file->f_mapping;
908 struct inode *inode = mapping->host;
912 WARN_ON(iocb->ki_pos != pos);
914 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
918 mutex_lock(&inode->i_mutex);
919 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
921 /* We can write back this queue in page reclaim */
922 current->backing_dev_info = mapping->backing_dev_info;
924 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
931 err = file_remove_suid(file);
935 file_update_time(file);
937 iov_iter_init(&i, iov, nr_segs, count, 0);
938 written = fuse_perform_write(file, mapping, &i, pos);
940 iocb->ki_pos = pos + written;
943 current->backing_dev_info = NULL;
944 mutex_unlock(&inode->i_mutex);
946 return written ? written : err;
949 static void fuse_release_user_pages(struct fuse_req *req, int write)
953 for (i = 0; i < req->num_pages; i++) {
954 struct page *page = req->pages[i];
956 set_page_dirty_lock(page);
961 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
962 size_t *nbytesp, int write)
964 size_t nbytes = *nbytesp;
965 unsigned long user_addr = (unsigned long) buf;
966 unsigned offset = user_addr & ~PAGE_MASK;
969 /* Special case for kernel I/O: can copy directly into the buffer */
970 if (segment_eq(get_fs(), KERNEL_DS)) {
972 req->in.args[1].value = (void *) user_addr;
974 req->out.args[0].value = (void *) user_addr;
979 nbytes = min_t(size_t, nbytes, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
980 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
981 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
982 down_read(¤t->mm->mmap_sem);
983 npages = get_user_pages(current, current->mm, user_addr, npages, !write,
984 0, req->pages, NULL);
985 up_read(¤t->mm->mmap_sem);
989 req->num_pages = npages;
990 req->page_offset = offset;
993 req->in.argpages = 1;
995 req->out.argpages = 1;
997 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
998 *nbytesp = min(*nbytesp, nbytes);
1003 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
1004 size_t count, loff_t *ppos, int write)
1006 struct fuse_file *ff = file->private_data;
1007 struct fuse_conn *fc = ff->fc;
1008 size_t nmax = write ? fc->max_write : fc->max_read;
1011 struct fuse_req *req;
1013 req = fuse_get_req(fc);
1015 return PTR_ERR(req);
1019 fl_owner_t owner = current->files;
1020 size_t nbytes = min(count, nmax);
1021 int err = fuse_get_user_pages(req, buf, &nbytes, write);
1028 nres = fuse_send_write(req, file, pos, nbytes, owner);
1030 nres = fuse_send_read(req, file, pos, nbytes, owner);
1032 fuse_release_user_pages(req, !write);
1033 if (req->out.h.error) {
1035 res = req->out.h.error;
1037 } else if (nres > nbytes) {
1048 fuse_put_request(fc, req);
1049 req = fuse_get_req(fc);
1054 fuse_put_request(fc, req);
1061 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1062 size_t count, loff_t *ppos)
1065 struct inode *inode = file->f_path.dentry->d_inode;
1067 if (is_bad_inode(inode))
1070 res = fuse_direct_io(file, buf, count, ppos, 0);
1072 fuse_invalidate_attr(inode);
1077 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1078 size_t count, loff_t *ppos)
1080 struct inode *inode = file->f_path.dentry->d_inode;
1083 if (is_bad_inode(inode))
1086 /* Don't allow parallel writes to the same file */
1087 mutex_lock(&inode->i_mutex);
1088 res = generic_write_checks(file, ppos, &count, 0);
1090 res = fuse_direct_io(file, buf, count, ppos, 1);
1092 fuse_write_update_size(inode, *ppos);
1094 mutex_unlock(&inode->i_mutex);
1096 fuse_invalidate_attr(inode);
1101 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1103 __free_page(req->pages[0]);
1104 fuse_file_put(req->ff);
1107 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1109 struct inode *inode = req->inode;
1110 struct fuse_inode *fi = get_fuse_inode(inode);
1111 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1113 list_del(&req->writepages_entry);
1114 dec_bdi_stat(bdi, BDI_WRITEBACK);
1115 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1116 bdi_writeout_inc(bdi);
1117 wake_up(&fi->page_waitq);
1120 /* Called under fc->lock, may release and reacquire it */
1121 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1122 __releases(&fc->lock)
1123 __acquires(&fc->lock)
1125 struct fuse_inode *fi = get_fuse_inode(req->inode);
1126 loff_t size = i_size_read(req->inode);
1127 struct fuse_write_in *inarg = &req->misc.write.in;
1132 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1133 inarg->size = PAGE_CACHE_SIZE;
1134 } else if (inarg->offset < size) {
1135 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1137 /* Got truncated off completely */
1141 req->in.args[1].size = inarg->size;
1143 fuse_request_send_background_locked(fc, req);
1147 fuse_writepage_finish(fc, req);
1148 spin_unlock(&fc->lock);
1149 fuse_writepage_free(fc, req);
1150 fuse_put_request(fc, req);
1151 spin_lock(&fc->lock);
1155 * If fi->writectr is positive (no truncate or fsync going on) send
1156 * all queued writepage requests.
1158 * Called with fc->lock
1160 void fuse_flush_writepages(struct inode *inode)
1161 __releases(&fc->lock)
1162 __acquires(&fc->lock)
1164 struct fuse_conn *fc = get_fuse_conn(inode);
1165 struct fuse_inode *fi = get_fuse_inode(inode);
1166 struct fuse_req *req;
1168 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1169 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1170 list_del_init(&req->list);
1171 fuse_send_writepage(fc, req);
1175 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1177 struct inode *inode = req->inode;
1178 struct fuse_inode *fi = get_fuse_inode(inode);
1180 mapping_set_error(inode->i_mapping, req->out.h.error);
1181 spin_lock(&fc->lock);
1183 fuse_writepage_finish(fc, req);
1184 spin_unlock(&fc->lock);
1185 fuse_writepage_free(fc, req);
1188 static int fuse_writepage_locked(struct page *page)
1190 struct address_space *mapping = page->mapping;
1191 struct inode *inode = mapping->host;
1192 struct fuse_conn *fc = get_fuse_conn(inode);
1193 struct fuse_inode *fi = get_fuse_inode(inode);
1194 struct fuse_req *req;
1195 struct fuse_file *ff;
1196 struct page *tmp_page;
1198 set_page_writeback(page);
1200 req = fuse_request_alloc_nofs();
1204 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1208 spin_lock(&fc->lock);
1209 BUG_ON(list_empty(&fi->write_files));
1210 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1211 req->ff = fuse_file_get(ff);
1212 spin_unlock(&fc->lock);
1214 fuse_write_fill(req, ff, page_offset(page), 0);
1216 copy_highpage(tmp_page, page);
1217 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1218 req->in.argpages = 1;
1220 req->pages[0] = tmp_page;
1221 req->page_offset = 0;
1222 req->end = fuse_writepage_end;
1225 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1226 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1227 end_page_writeback(page);
1229 spin_lock(&fc->lock);
1230 list_add(&req->writepages_entry, &fi->writepages);
1231 list_add_tail(&req->list, &fi->queued_writes);
1232 fuse_flush_writepages(inode);
1233 spin_unlock(&fc->lock);
1238 fuse_request_free(req);
1240 end_page_writeback(page);
1244 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1248 err = fuse_writepage_locked(page);
1254 static int fuse_launder_page(struct page *page)
1257 if (clear_page_dirty_for_io(page)) {
1258 struct inode *inode = page->mapping->host;
1259 err = fuse_writepage_locked(page);
1261 fuse_wait_on_page_writeback(inode, page->index);
1267 * Write back dirty pages now, because there may not be any suitable
1270 static void fuse_vma_close(struct vm_area_struct *vma)
1272 filemap_write_and_wait(vma->vm_file->f_mapping);
1276 * Wait for writeback against this page to complete before allowing it
1277 * to be marked dirty again, and hence written back again, possibly
1278 * before the previous writepage completed.
1280 * Block here, instead of in ->writepage(), so that the userspace fs
1281 * can only block processes actually operating on the filesystem.
1283 * Otherwise unprivileged userspace fs would be able to block
1288 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1290 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1292 struct page *page = vmf->page;
1294 * Don't use page->mapping as it may become NULL from a
1295 * concurrent truncate.
1297 struct inode *inode = vma->vm_file->f_mapping->host;
1299 fuse_wait_on_page_writeback(inode, page->index);
1303 static struct vm_operations_struct fuse_file_vm_ops = {
1304 .close = fuse_vma_close,
1305 .fault = filemap_fault,
1306 .page_mkwrite = fuse_page_mkwrite,
1309 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1311 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1312 struct inode *inode = file->f_dentry->d_inode;
1313 struct fuse_conn *fc = get_fuse_conn(inode);
1314 struct fuse_inode *fi = get_fuse_inode(inode);
1315 struct fuse_file *ff = file->private_data;
1317 * file may be written through mmap, so chain it onto the
1318 * inodes's write_file list
1320 spin_lock(&fc->lock);
1321 if (list_empty(&ff->write_entry))
1322 list_add(&ff->write_entry, &fi->write_files);
1323 spin_unlock(&fc->lock);
1325 file_accessed(file);
1326 vma->vm_ops = &fuse_file_vm_ops;
1330 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1332 /* Can't provide the coherency needed for MAP_SHARED */
1333 if (vma->vm_flags & VM_MAYSHARE)
1336 invalidate_inode_pages2(file->f_mapping);
1338 return generic_file_mmap(file, vma);
1341 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1342 struct file_lock *fl)
1344 switch (ffl->type) {
1350 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1351 ffl->end < ffl->start)
1354 fl->fl_start = ffl->start;
1355 fl->fl_end = ffl->end;
1356 fl->fl_pid = ffl->pid;
1362 fl->fl_type = ffl->type;
1366 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1367 const struct file_lock *fl, int opcode, pid_t pid,
1370 struct inode *inode = file->f_path.dentry->d_inode;
1371 struct fuse_conn *fc = get_fuse_conn(inode);
1372 struct fuse_file *ff = file->private_data;
1373 struct fuse_lk_in *arg = &req->misc.lk_in;
1376 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1377 arg->lk.start = fl->fl_start;
1378 arg->lk.end = fl->fl_end;
1379 arg->lk.type = fl->fl_type;
1382 arg->lk_flags |= FUSE_LK_FLOCK;
1383 req->in.h.opcode = opcode;
1384 req->in.h.nodeid = get_node_id(inode);
1385 req->in.numargs = 1;
1386 req->in.args[0].size = sizeof(*arg);
1387 req->in.args[0].value = arg;
1390 static int fuse_getlk(struct file *file, struct file_lock *fl)
1392 struct inode *inode = file->f_path.dentry->d_inode;
1393 struct fuse_conn *fc = get_fuse_conn(inode);
1394 struct fuse_req *req;
1395 struct fuse_lk_out outarg;
1398 req = fuse_get_req(fc);
1400 return PTR_ERR(req);
1402 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1403 req->out.numargs = 1;
1404 req->out.args[0].size = sizeof(outarg);
1405 req->out.args[0].value = &outarg;
1406 fuse_request_send(fc, req);
1407 err = req->out.h.error;
1408 fuse_put_request(fc, req);
1410 err = convert_fuse_file_lock(&outarg.lk, fl);
1415 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1417 struct inode *inode = file->f_path.dentry->d_inode;
1418 struct fuse_conn *fc = get_fuse_conn(inode);
1419 struct fuse_req *req;
1420 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1421 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1424 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1425 /* NLM needs asynchronous locks, which we don't support yet */
1429 /* Unlock on close is handled by the flush method */
1430 if (fl->fl_flags & FL_CLOSE)
1433 req = fuse_get_req(fc);
1435 return PTR_ERR(req);
1437 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1438 fuse_request_send(fc, req);
1439 err = req->out.h.error;
1440 /* locking is restartable */
1443 fuse_put_request(fc, req);
1447 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1449 struct inode *inode = file->f_path.dentry->d_inode;
1450 struct fuse_conn *fc = get_fuse_conn(inode);
1453 if (cmd == F_CANCELLK) {
1455 } else if (cmd == F_GETLK) {
1457 posix_test_lock(file, fl);
1460 err = fuse_getlk(file, fl);
1463 err = posix_lock_file(file, fl, NULL);
1465 err = fuse_setlk(file, fl, 0);
1470 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1472 struct inode *inode = file->f_path.dentry->d_inode;
1473 struct fuse_conn *fc = get_fuse_conn(inode);
1477 err = flock_lock_file_wait(file, fl);
1479 /* emulate flock with POSIX locks */
1480 fl->fl_owner = (fl_owner_t) file;
1481 err = fuse_setlk(file, fl, 1);
1487 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1489 struct inode *inode = mapping->host;
1490 struct fuse_conn *fc = get_fuse_conn(inode);
1491 struct fuse_req *req;
1492 struct fuse_bmap_in inarg;
1493 struct fuse_bmap_out outarg;
1496 if (!inode->i_sb->s_bdev || fc->no_bmap)
1499 req = fuse_get_req(fc);
1503 memset(&inarg, 0, sizeof(inarg));
1504 inarg.block = block;
1505 inarg.blocksize = inode->i_sb->s_blocksize;
1506 req->in.h.opcode = FUSE_BMAP;
1507 req->in.h.nodeid = get_node_id(inode);
1508 req->in.numargs = 1;
1509 req->in.args[0].size = sizeof(inarg);
1510 req->in.args[0].value = &inarg;
1511 req->out.numargs = 1;
1512 req->out.args[0].size = sizeof(outarg);
1513 req->out.args[0].value = &outarg;
1514 fuse_request_send(fc, req);
1515 err = req->out.h.error;
1516 fuse_put_request(fc, req);
1520 return err ? 0 : outarg.block;
1523 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1526 struct inode *inode = file->f_path.dentry->d_inode;
1528 mutex_lock(&inode->i_mutex);
1531 retval = fuse_update_attributes(inode, NULL, file, NULL);
1534 offset += i_size_read(inode);
1537 offset += file->f_pos;
1540 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1541 if (offset != file->f_pos) {
1542 file->f_pos = offset;
1543 file->f_version = 0;
1548 mutex_unlock(&inode->i_mutex);
1552 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1553 unsigned int nr_segs, size_t bytes, bool to_user)
1561 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1563 while (iov_iter_count(&ii)) {
1564 struct page *page = pages[page_idx++];
1565 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1568 kaddr = map = kmap(page);
1571 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1572 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1573 size_t copy = min(todo, iov_len);
1577 left = copy_from_user(kaddr, uaddr, copy);
1579 left = copy_to_user(uaddr, kaddr, copy);
1584 iov_iter_advance(&ii, copy);
1596 * For ioctls, there is no generic way to determine how much memory
1597 * needs to be read and/or written. Furthermore, ioctls are allowed
1598 * to dereference the passed pointer, so the parameter requires deep
1599 * copying but FUSE has no idea whatsoever about what to copy in or
1602 * This is solved by allowing FUSE server to retry ioctl with
1603 * necessary in/out iovecs. Let's assume the ioctl implementation
1604 * needs to read in the following structure.
1611 * On the first callout to FUSE server, inarg->in_size and
1612 * inarg->out_size will be NULL; then, the server completes the ioctl
1613 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1614 * the actual iov array to
1616 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1618 * which tells FUSE to copy in the requested area and retry the ioctl.
1619 * On the second round, the server has access to the structure and
1620 * from that it can tell what to look for next, so on the invocation,
1621 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1623 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1624 * { .iov_base = a.buf, .iov_len = a.buflen } }
1626 * FUSE will copy both struct a and the pointed buffer from the
1627 * process doing the ioctl and retry ioctl with both struct a and the
1630 * This time, FUSE server has everything it needs and completes ioctl
1631 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1633 * Copying data out works the same way.
1635 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1636 * automatically initializes in and out iovs by decoding @cmd with
1637 * _IOC_* macros and the server is not allowed to request RETRY. This
1638 * limits ioctl data transfers to well-formed ioctls and is the forced
1639 * behavior for all FUSE servers.
1641 static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
1642 unsigned long arg, unsigned int flags)
1644 struct inode *inode = file->f_dentry->d_inode;
1645 struct fuse_file *ff = file->private_data;
1646 struct fuse_conn *fc = get_fuse_conn(inode);
1647 struct fuse_ioctl_in inarg = {
1653 struct fuse_ioctl_out outarg;
1654 struct fuse_req *req = NULL;
1655 struct page **pages = NULL;
1656 struct page *iov_page = NULL;
1657 struct iovec *in_iov = NULL, *out_iov = NULL;
1658 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1659 size_t in_size, out_size, transferred;
1662 /* assume all the iovs returned by client always fits in a page */
1663 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1665 if (!fuse_allow_task(fc, current))
1669 if (is_bad_inode(inode))
1673 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1674 iov_page = alloc_page(GFP_KERNEL);
1675 if (!pages || !iov_page)
1679 * If restricted, initialize IO parameters as encoded in @cmd.
1680 * RETRY from server is not allowed.
1682 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1683 struct iovec *iov = page_address(iov_page);
1685 iov->iov_base = (void __user *)arg;
1686 iov->iov_len = _IOC_SIZE(cmd);
1688 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1693 if (_IOC_DIR(cmd) & _IOC_READ) {
1700 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1701 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1704 * Out data can be used either for actual out data or iovs,
1705 * make sure there always is at least one page.
1707 out_size = max_t(size_t, out_size, PAGE_SIZE);
1708 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1710 /* make sure there are enough buffer pages and init request with them */
1712 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1714 while (num_pages < max_pages) {
1715 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1716 if (!pages[num_pages])
1721 req = fuse_get_req(fc);
1727 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1728 req->num_pages = num_pages;
1730 /* okay, let's send it to the client */
1731 req->in.h.opcode = FUSE_IOCTL;
1732 req->in.h.nodeid = get_node_id(inode);
1733 req->in.numargs = 1;
1734 req->in.args[0].size = sizeof(inarg);
1735 req->in.args[0].value = &inarg;
1738 req->in.args[1].size = in_size;
1739 req->in.argpages = 1;
1741 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1747 req->out.numargs = 2;
1748 req->out.args[0].size = sizeof(outarg);
1749 req->out.args[0].value = &outarg;
1750 req->out.args[1].size = out_size;
1751 req->out.argpages = 1;
1752 req->out.argvar = 1;
1754 fuse_request_send(fc, req);
1755 err = req->out.h.error;
1756 transferred = req->out.args[1].size;
1757 fuse_put_request(fc, req);
1762 /* did it ask for retry? */
1763 if (outarg.flags & FUSE_IOCTL_RETRY) {
1766 /* no retry if in restricted mode */
1768 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1771 in_iovs = outarg.in_iovs;
1772 out_iovs = outarg.out_iovs;
1775 * Make sure things are in boundary, separate checks
1776 * are to protect against overflow.
1779 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1780 out_iovs > FUSE_IOCTL_MAX_IOV ||
1781 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1785 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1788 /* okay, copy in iovs and retry */
1789 vaddr = kmap_atomic(pages[0], KM_USER0);
1790 memcpy(page_address(iov_page), vaddr, transferred);
1791 kunmap_atomic(vaddr, KM_USER0);
1793 in_iov = page_address(iov_page);
1794 out_iov = in_iov + in_iovs;
1800 if (transferred > inarg.out_size)
1803 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1806 fuse_put_request(fc, req);
1808 __free_page(iov_page);
1810 __free_page(pages[--num_pages]);
1813 return err ? err : outarg.result;
1816 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1819 return fuse_file_do_ioctl(file, cmd, arg, 0);
1822 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1825 return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
1829 * All files which have been polled are linked to RB tree
1830 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1831 * find the matching one.
1833 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1834 struct rb_node **parent_out)
1836 struct rb_node **link = &fc->polled_files.rb_node;
1837 struct rb_node *last = NULL;
1840 struct fuse_file *ff;
1843 ff = rb_entry(last, struct fuse_file, polled_node);
1846 link = &last->rb_left;
1847 else if (kh > ff->kh)
1848 link = &last->rb_right;
1859 * The file is about to be polled. Make sure it's on the polled_files
1860 * RB tree. Note that files once added to the polled_files tree are
1861 * not removed before the file is released. This is because a file
1862 * polled once is likely to be polled again.
1864 static void fuse_register_polled_file(struct fuse_conn *fc,
1865 struct fuse_file *ff)
1867 spin_lock(&fc->lock);
1868 if (RB_EMPTY_NODE(&ff->polled_node)) {
1869 struct rb_node **link, *parent;
1871 link = fuse_find_polled_node(fc, ff->kh, &parent);
1873 rb_link_node(&ff->polled_node, parent, link);
1874 rb_insert_color(&ff->polled_node, &fc->polled_files);
1876 spin_unlock(&fc->lock);
1879 static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1881 struct inode *inode = file->f_dentry->d_inode;
1882 struct fuse_file *ff = file->private_data;
1883 struct fuse_conn *fc = get_fuse_conn(inode);
1884 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1885 struct fuse_poll_out outarg;
1886 struct fuse_req *req;
1890 return DEFAULT_POLLMASK;
1892 poll_wait(file, &ff->poll_wait, wait);
1895 * Ask for notification iff there's someone waiting for it.
1896 * The client may ignore the flag and always notify.
1898 if (waitqueue_active(&ff->poll_wait)) {
1899 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1900 fuse_register_polled_file(fc, ff);
1903 req = fuse_get_req(fc);
1905 return PTR_ERR(req);
1907 req->in.h.opcode = FUSE_POLL;
1908 req->in.h.nodeid = get_node_id(inode);
1909 req->in.numargs = 1;
1910 req->in.args[0].size = sizeof(inarg);
1911 req->in.args[0].value = &inarg;
1912 req->out.numargs = 1;
1913 req->out.args[0].size = sizeof(outarg);
1914 req->out.args[0].value = &outarg;
1915 fuse_request_send(fc, req);
1916 err = req->out.h.error;
1917 fuse_put_request(fc, req);
1920 return outarg.revents;
1921 if (err == -ENOSYS) {
1923 return DEFAULT_POLLMASK;
1929 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1930 * wakes up the poll waiters.
1932 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1933 struct fuse_notify_poll_wakeup_out *outarg)
1935 u64 kh = outarg->kh;
1936 struct rb_node **link;
1938 spin_lock(&fc->lock);
1940 link = fuse_find_polled_node(fc, kh, NULL);
1942 struct fuse_file *ff;
1944 ff = rb_entry(*link, struct fuse_file, polled_node);
1945 wake_up_interruptible_sync(&ff->poll_wait);
1948 spin_unlock(&fc->lock);
1952 static const struct file_operations fuse_file_operations = {
1953 .llseek = fuse_file_llseek,
1954 .read = do_sync_read,
1955 .aio_read = fuse_file_aio_read,
1956 .write = do_sync_write,
1957 .aio_write = fuse_file_aio_write,
1958 .mmap = fuse_file_mmap,
1960 .flush = fuse_flush,
1961 .release = fuse_release,
1962 .fsync = fuse_fsync,
1963 .lock = fuse_file_lock,
1964 .flock = fuse_file_flock,
1965 .splice_read = generic_file_splice_read,
1966 .unlocked_ioctl = fuse_file_ioctl,
1967 .compat_ioctl = fuse_file_compat_ioctl,
1968 .poll = fuse_file_poll,
1971 static const struct file_operations fuse_direct_io_file_operations = {
1972 .llseek = fuse_file_llseek,
1973 .read = fuse_direct_read,
1974 .write = fuse_direct_write,
1975 .mmap = fuse_direct_mmap,
1977 .flush = fuse_flush,
1978 .release = fuse_release,
1979 .fsync = fuse_fsync,
1980 .lock = fuse_file_lock,
1981 .flock = fuse_file_flock,
1982 .unlocked_ioctl = fuse_file_ioctl,
1983 .compat_ioctl = fuse_file_compat_ioctl,
1984 .poll = fuse_file_poll,
1985 /* no splice_read */
1988 static const struct address_space_operations fuse_file_aops = {
1989 .readpage = fuse_readpage,
1990 .writepage = fuse_writepage,
1991 .launder_page = fuse_launder_page,
1992 .write_begin = fuse_write_begin,
1993 .write_end = fuse_write_end,
1994 .readpages = fuse_readpages,
1995 .set_page_dirty = __set_page_dirty_nobuffers,
1999 void fuse_init_file_inode(struct inode *inode)
2001 inode->i_fop = &fuse_file_operations;
2002 inode->i_data.a_ops = &fuse_file_aops;