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)
52 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
54 ff->reserved_req = fuse_request_alloc();
55 if (!ff->reserved_req) {
59 INIT_LIST_HEAD(&ff->write_entry);
60 atomic_set(&ff->count, 0);
63 spin_unlock(&fc->lock);
65 RB_CLEAR_NODE(&ff->polled_node);
66 init_waitqueue_head(&ff->poll_wait);
71 void fuse_file_free(struct fuse_file *ff)
73 fuse_request_free(ff->reserved_req);
77 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
79 atomic_inc(&ff->count);
83 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85 dput(req->misc.release.dentry);
86 mntput(req->misc.release.vfsmount);
89 static void fuse_file_put(struct fuse_file *ff)
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
93 struct inode *inode = req->misc.release.dentry->d_inode;
94 struct fuse_conn *fc = get_fuse_conn(inode);
95 req->end = fuse_release_end;
96 fuse_request_send_background(fc, req);
101 void fuse_finish_open(struct inode *inode, struct file *file,
102 struct fuse_file *ff, struct fuse_open_out *outarg)
104 if (outarg->open_flags & FOPEN_DIRECT_IO)
105 file->f_op = &fuse_direct_io_file_operations;
106 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
107 invalidate_inode_pages2(inode->i_mapping);
108 if (outarg->open_flags & FOPEN_NONSEEKABLE)
109 nonseekable_open(inode, file);
111 file->private_data = fuse_file_get(ff);
114 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
116 struct fuse_conn *fc = get_fuse_conn(inode);
117 struct fuse_open_out outarg;
118 struct fuse_file *ff;
121 /* VFS checks this, but only _after_ ->open() */
122 if (file->f_flags & O_DIRECT)
125 err = generic_file_open(inode, file);
129 ff = fuse_file_alloc(fc);
133 err = fuse_send_open(inode, file, isdir, &outarg);
138 outarg.open_flags &= ~FOPEN_DIRECT_IO;
139 fuse_finish_open(inode, file, ff, &outarg);
145 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
147 struct fuse_req *req = ff->reserved_req;
148 struct fuse_release_in *inarg = &req->misc.release.in;
151 inarg->flags = flags;
152 req->in.h.opcode = opcode;
153 req->in.h.nodeid = nodeid;
155 req->in.args[0].size = sizeof(struct fuse_release_in);
156 req->in.args[0].value = inarg;
159 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
161 struct fuse_file *ff = file->private_data;
163 struct fuse_conn *fc = get_fuse_conn(inode);
164 struct fuse_req *req = ff->reserved_req;
166 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
167 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
169 /* Hold vfsmount and dentry until release is finished */
170 req->misc.release.vfsmount = mntget(file->f_path.mnt);
171 req->misc.release.dentry = dget(file->f_path.dentry);
173 spin_lock(&fc->lock);
174 list_del(&ff->write_entry);
175 if (!RB_EMPTY_NODE(&ff->polled_node))
176 rb_erase(&ff->polled_node, &fc->polled_files);
177 spin_unlock(&fc->lock);
179 wake_up_interruptible_sync(&ff->poll_wait);
181 * Normally this will send the RELEASE request,
182 * however if some asynchronous READ or WRITE requests
183 * are outstanding, the sending will be delayed
188 /* Return value is ignored by VFS */
192 static int fuse_open(struct inode *inode, struct file *file)
194 return fuse_open_common(inode, file, 0);
197 static int fuse_release(struct inode *inode, struct file *file)
199 return fuse_release_common(inode, file, 0);
203 * Scramble the ID space with XTEA, so that the value of the files_struct
204 * pointer is not exposed to userspace.
206 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
208 u32 *k = fc->scramble_key;
209 u64 v = (unsigned long) id;
215 for (i = 0; i < 32; i++) {
216 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
218 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
221 return (u64) v0 + ((u64) v1 << 32);
225 * Check if page is under writeback
227 * This is currently done by walking the list of writepage requests
228 * for the inode, which can be pretty inefficient.
230 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
232 struct fuse_conn *fc = get_fuse_conn(inode);
233 struct fuse_inode *fi = get_fuse_inode(inode);
234 struct fuse_req *req;
237 spin_lock(&fc->lock);
238 list_for_each_entry(req, &fi->writepages, writepages_entry) {
241 BUG_ON(req->inode != inode);
242 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
243 if (curr_index == index) {
248 spin_unlock(&fc->lock);
254 * Wait for page writeback to be completed.
256 * Since fuse doesn't rely on the VM writeback tracking, this has to
257 * use some other means.
259 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
261 struct fuse_inode *fi = get_fuse_inode(inode);
263 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
267 static int fuse_flush(struct file *file, fl_owner_t id)
269 struct inode *inode = file->f_path.dentry->d_inode;
270 struct fuse_conn *fc = get_fuse_conn(inode);
271 struct fuse_file *ff = file->private_data;
272 struct fuse_req *req;
273 struct fuse_flush_in inarg;
276 if (is_bad_inode(inode))
282 req = fuse_get_req_nofail(fc, file);
283 memset(&inarg, 0, sizeof(inarg));
285 inarg.lock_owner = fuse_lock_owner_id(fc, id);
286 req->in.h.opcode = FUSE_FLUSH;
287 req->in.h.nodeid = get_node_id(inode);
289 req->in.args[0].size = sizeof(inarg);
290 req->in.args[0].value = &inarg;
292 fuse_request_send(fc, req);
293 err = req->out.h.error;
294 fuse_put_request(fc, req);
295 if (err == -ENOSYS) {
303 * Wait for all pending writepages on the inode to finish.
305 * This is currently done by blocking further writes with FUSE_NOWRITE
306 * and waiting for all sent writes to complete.
308 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
309 * could conflict with truncation.
311 static void fuse_sync_writes(struct inode *inode)
313 fuse_set_nowrite(inode);
314 fuse_release_nowrite(inode);
317 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
320 struct inode *inode = de->d_inode;
321 struct fuse_conn *fc = get_fuse_conn(inode);
322 struct fuse_file *ff = file->private_data;
323 struct fuse_req *req;
324 struct fuse_fsync_in inarg;
327 if (is_bad_inode(inode))
330 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
334 * Start writeback against all dirty pages of the inode, then
335 * wait for all outstanding writes, before sending the FSYNC
338 err = write_inode_now(inode, 0);
342 fuse_sync_writes(inode);
344 req = fuse_get_req(fc);
348 memset(&inarg, 0, sizeof(inarg));
350 inarg.fsync_flags = datasync ? 1 : 0;
351 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
352 req->in.h.nodeid = get_node_id(inode);
354 req->in.args[0].size = sizeof(inarg);
355 req->in.args[0].value = &inarg;
356 fuse_request_send(fc, req);
357 err = req->out.h.error;
358 fuse_put_request(fc, req);
359 if (err == -ENOSYS) {
369 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
371 return fuse_fsync_common(file, de, datasync, 0);
374 void fuse_read_fill(struct fuse_req *req, struct file *file,
375 struct inode *inode, loff_t pos, size_t count, int opcode)
377 struct fuse_read_in *inarg = &req->misc.read.in;
378 struct fuse_file *ff = file->private_data;
383 inarg->flags = file->f_flags;
384 req->in.h.opcode = opcode;
385 req->in.h.nodeid = get_node_id(inode);
387 req->in.args[0].size = sizeof(struct fuse_read_in);
388 req->in.args[0].value = inarg;
389 req->out.argpages = 1;
391 req->out.numargs = 1;
392 req->out.args[0].size = count;
395 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
396 struct inode *inode, loff_t pos, size_t count,
399 struct fuse_conn *fc = get_fuse_conn(inode);
401 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
403 struct fuse_read_in *inarg = &req->misc.read.in;
405 inarg->read_flags |= FUSE_READ_LOCKOWNER;
406 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
408 fuse_request_send(fc, req);
409 return req->out.args[0].size;
412 static void fuse_read_update_size(struct inode *inode, loff_t size,
415 struct fuse_conn *fc = get_fuse_conn(inode);
416 struct fuse_inode *fi = get_fuse_inode(inode);
418 spin_lock(&fc->lock);
419 if (attr_ver == fi->attr_version && size < inode->i_size) {
420 fi->attr_version = ++fc->attr_version;
421 i_size_write(inode, size);
423 spin_unlock(&fc->lock);
426 static int fuse_readpage(struct file *file, struct page *page)
428 struct inode *inode = page->mapping->host;
429 struct fuse_conn *fc = get_fuse_conn(inode);
430 struct fuse_req *req;
432 loff_t pos = page_offset(page);
433 size_t count = PAGE_CACHE_SIZE;
438 if (is_bad_inode(inode))
442 * Page writeback can extend beyond the liftime of the
443 * page-cache page, so make sure we read a properly synced
446 fuse_wait_on_page_writeback(inode, page->index);
448 req = fuse_get_req(fc);
453 attr_ver = fuse_get_attr_version(fc);
455 req->out.page_zeroing = 1;
457 req->pages[0] = page;
458 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
459 err = req->out.h.error;
460 fuse_put_request(fc, req);
464 * Short read means EOF. If file size is larger, truncate it
466 if (num_read < count)
467 fuse_read_update_size(inode, pos + num_read, attr_ver);
469 SetPageUptodate(page);
472 fuse_invalidate_attr(inode); /* atime changed */
478 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
481 size_t count = req->misc.read.in.size;
482 size_t num_read = req->out.args[0].size;
483 struct inode *inode = req->pages[0]->mapping->host;
486 * Short read means EOF. If file size is larger, truncate it
488 if (!req->out.h.error && num_read < count) {
489 loff_t pos = page_offset(req->pages[0]) + num_read;
490 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
493 fuse_invalidate_attr(inode); /* atime changed */
495 for (i = 0; i < req->num_pages; i++) {
496 struct page *page = req->pages[i];
497 if (!req->out.h.error)
498 SetPageUptodate(page);
504 fuse_file_put(req->ff);
507 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
510 struct fuse_conn *fc = get_fuse_conn(inode);
511 loff_t pos = page_offset(req->pages[0]);
512 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
513 req->out.page_zeroing = 1;
514 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
515 req->misc.read.attr_ver = fuse_get_attr_version(fc);
516 if (fc->async_read) {
517 struct fuse_file *ff = file->private_data;
518 req->ff = fuse_file_get(ff);
519 req->end = fuse_readpages_end;
520 fuse_request_send_background(fc, req);
522 fuse_request_send(fc, req);
523 fuse_readpages_end(fc, req);
524 fuse_put_request(fc, req);
528 struct fuse_fill_data {
529 struct fuse_req *req;
534 static int fuse_readpages_fill(void *_data, struct page *page)
536 struct fuse_fill_data *data = _data;
537 struct fuse_req *req = data->req;
538 struct inode *inode = data->inode;
539 struct fuse_conn *fc = get_fuse_conn(inode);
541 fuse_wait_on_page_writeback(inode, page->index);
543 if (req->num_pages &&
544 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
545 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
546 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
547 fuse_send_readpages(req, data->file, inode);
548 data->req = req = fuse_get_req(fc);
554 req->pages[req->num_pages] = page;
559 static int fuse_readpages(struct file *file, struct address_space *mapping,
560 struct list_head *pages, unsigned nr_pages)
562 struct inode *inode = mapping->host;
563 struct fuse_conn *fc = get_fuse_conn(inode);
564 struct fuse_fill_data data;
568 if (is_bad_inode(inode))
573 data.req = fuse_get_req(fc);
574 err = PTR_ERR(data.req);
575 if (IS_ERR(data.req))
578 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
580 if (data.req->num_pages)
581 fuse_send_readpages(data.req, file, inode);
583 fuse_put_request(fc, data.req);
589 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
590 unsigned long nr_segs, loff_t pos)
592 struct inode *inode = iocb->ki_filp->f_mapping->host;
594 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
597 * If trying to read past EOF, make sure the i_size
598 * attribute is up-to-date.
600 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
605 return generic_file_aio_read(iocb, iov, nr_segs, pos);
608 static void fuse_write_fill(struct fuse_req *req, struct file *file,
609 struct fuse_file *ff, struct inode *inode,
610 loff_t pos, size_t count, int writepage)
612 struct fuse_conn *fc = get_fuse_conn(inode);
613 struct fuse_write_in *inarg = &req->misc.write.in;
614 struct fuse_write_out *outarg = &req->misc.write.out;
616 memset(inarg, 0, sizeof(struct fuse_write_in));
620 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
621 inarg->flags = file ? file->f_flags : 0;
622 req->in.h.opcode = FUSE_WRITE;
623 req->in.h.nodeid = get_node_id(inode);
624 req->in.argpages = 1;
627 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
629 req->in.args[0].size = sizeof(struct fuse_write_in);
630 req->in.args[0].value = inarg;
631 req->in.args[1].size = count;
632 req->out.numargs = 1;
633 req->out.args[0].size = sizeof(struct fuse_write_out);
634 req->out.args[0].value = outarg;
637 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
638 struct inode *inode, loff_t pos, size_t count,
641 struct fuse_conn *fc = get_fuse_conn(inode);
642 fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
644 struct fuse_write_in *inarg = &req->misc.write.in;
645 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
646 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
648 fuse_request_send(fc, req);
649 return req->misc.write.out.size;
652 static int fuse_write_begin(struct file *file, struct address_space *mapping,
653 loff_t pos, unsigned len, unsigned flags,
654 struct page **pagep, void **fsdata)
656 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
658 *pagep = grab_cache_page_write_begin(mapping, index, flags);
664 static void fuse_write_update_size(struct inode *inode, loff_t pos)
666 struct fuse_conn *fc = get_fuse_conn(inode);
667 struct fuse_inode *fi = get_fuse_inode(inode);
669 spin_lock(&fc->lock);
670 fi->attr_version = ++fc->attr_version;
671 if (pos > inode->i_size)
672 i_size_write(inode, pos);
673 spin_unlock(&fc->lock);
676 static int fuse_buffered_write(struct file *file, struct inode *inode,
677 loff_t pos, unsigned count, struct page *page)
681 struct fuse_conn *fc = get_fuse_conn(inode);
682 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
683 struct fuse_req *req;
685 if (is_bad_inode(inode))
689 * Make sure writepages on the same page are not mixed up with
692 fuse_wait_on_page_writeback(inode, page->index);
694 req = fuse_get_req(fc);
699 req->pages[0] = page;
700 req->page_offset = offset;
701 nres = fuse_send_write(req, file, inode, pos, count, NULL);
702 err = req->out.h.error;
703 fuse_put_request(fc, req);
708 fuse_write_update_size(inode, pos);
709 if (count == PAGE_CACHE_SIZE)
710 SetPageUptodate(page);
712 fuse_invalidate_attr(inode);
713 return err ? err : nres;
716 static int fuse_write_end(struct file *file, struct address_space *mapping,
717 loff_t pos, unsigned len, unsigned copied,
718 struct page *page, void *fsdata)
720 struct inode *inode = mapping->host;
724 res = fuse_buffered_write(file, inode, pos, copied, page);
727 page_cache_release(page);
731 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
732 struct inode *inode, loff_t pos,
739 for (i = 0; i < req->num_pages; i++)
740 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
742 res = fuse_send_write(req, file, inode, pos, count, NULL);
744 offset = req->page_offset;
746 for (i = 0; i < req->num_pages; i++) {
747 struct page *page = req->pages[i];
749 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
750 SetPageUptodate(page);
752 if (count > PAGE_CACHE_SIZE - offset)
753 count -= PAGE_CACHE_SIZE - offset;
759 page_cache_release(page);
765 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
766 struct address_space *mapping,
767 struct iov_iter *ii, loff_t pos)
769 struct fuse_conn *fc = get_fuse_conn(mapping->host);
770 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
774 req->page_offset = offset;
779 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
780 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
783 bytes = min_t(size_t, bytes, fc->max_write - count);
787 if (iov_iter_fault_in_readable(ii, bytes))
791 page = grab_cache_page_write_begin(mapping, index, 0);
796 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
798 flush_dcache_page(page);
802 page_cache_release(page);
803 bytes = min(bytes, iov_iter_single_seg_count(ii));
808 req->pages[req->num_pages] = page;
811 iov_iter_advance(ii, tmp);
815 if (offset == PAGE_CACHE_SIZE)
820 } while (iov_iter_count(ii) && count < fc->max_write &&
821 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
823 return count > 0 ? count : err;
826 static ssize_t fuse_perform_write(struct file *file,
827 struct address_space *mapping,
828 struct iov_iter *ii, loff_t pos)
830 struct inode *inode = mapping->host;
831 struct fuse_conn *fc = get_fuse_conn(inode);
835 if (is_bad_inode(inode))
839 struct fuse_req *req;
842 req = fuse_get_req(fc);
848 count = fuse_fill_write_pages(req, mapping, ii, pos);
854 num_written = fuse_send_write_pages(req, file, inode,
856 err = req->out.h.error;
861 /* break out of the loop on short write */
862 if (num_written != count)
866 fuse_put_request(fc, req);
867 } while (!err && iov_iter_count(ii));
870 fuse_write_update_size(inode, pos);
872 fuse_invalidate_attr(inode);
874 return res > 0 ? res : err;
877 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
878 unsigned long nr_segs, loff_t pos)
880 struct file *file = iocb->ki_filp;
881 struct address_space *mapping = file->f_mapping;
884 struct inode *inode = mapping->host;
888 WARN_ON(iocb->ki_pos != pos);
890 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
894 mutex_lock(&inode->i_mutex);
895 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
897 /* We can write back this queue in page reclaim */
898 current->backing_dev_info = mapping->backing_dev_info;
900 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
907 err = file_remove_suid(file);
911 file_update_time(file);
913 iov_iter_init(&i, iov, nr_segs, count, 0);
914 written = fuse_perform_write(file, mapping, &i, pos);
916 iocb->ki_pos = pos + written;
919 current->backing_dev_info = NULL;
920 mutex_unlock(&inode->i_mutex);
922 return written ? written : err;
925 static void fuse_release_user_pages(struct fuse_req *req, int write)
929 for (i = 0; i < req->num_pages; i++) {
930 struct page *page = req->pages[i];
932 set_page_dirty_lock(page);
937 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
938 unsigned nbytes, int write)
940 unsigned long user_addr = (unsigned long) buf;
941 unsigned offset = user_addr & ~PAGE_MASK;
944 /* This doesn't work with nfsd */
948 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
949 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
950 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
951 down_read(¤t->mm->mmap_sem);
952 npages = get_user_pages(current, current->mm, user_addr, npages, write,
953 0, req->pages, NULL);
954 up_read(¤t->mm->mmap_sem);
958 req->num_pages = npages;
959 req->page_offset = offset;
963 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
964 size_t count, loff_t *ppos, int write)
966 struct inode *inode = file->f_path.dentry->d_inode;
967 struct fuse_conn *fc = get_fuse_conn(inode);
968 size_t nmax = write ? fc->max_write : fc->max_read;
971 struct fuse_req *req;
973 if (is_bad_inode(inode))
976 req = fuse_get_req(fc);
982 size_t nbytes_limit = min(count, nmax);
984 int err = fuse_get_user_pages(req, buf, nbytes_limit, !write);
989 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
990 nbytes = min(nbytes_limit, nbytes);
992 nres = fuse_send_write(req, file, inode, pos, nbytes,
995 nres = fuse_send_read(req, file, inode, pos, nbytes,
997 fuse_release_user_pages(req, !write);
998 if (req->out.h.error) {
1000 res = req->out.h.error;
1002 } else if (nres > nbytes) {
1013 fuse_put_request(fc, req);
1014 req = fuse_get_req(fc);
1019 fuse_put_request(fc, req);
1022 fuse_write_update_size(inode, pos);
1025 fuse_invalidate_attr(inode);
1030 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1031 size_t count, loff_t *ppos)
1033 return fuse_direct_io(file, buf, count, ppos, 0);
1036 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1037 size_t count, loff_t *ppos)
1039 struct inode *inode = file->f_path.dentry->d_inode;
1041 /* Don't allow parallel writes to the same file */
1042 mutex_lock(&inode->i_mutex);
1043 res = generic_write_checks(file, ppos, &count, 0);
1045 res = fuse_direct_io(file, buf, count, ppos, 1);
1046 mutex_unlock(&inode->i_mutex);
1050 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1052 __free_page(req->pages[0]);
1053 fuse_file_put(req->ff);
1056 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1058 struct inode *inode = req->inode;
1059 struct fuse_inode *fi = get_fuse_inode(inode);
1060 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1062 list_del(&req->writepages_entry);
1063 dec_bdi_stat(bdi, BDI_WRITEBACK);
1064 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1065 bdi_writeout_inc(bdi);
1066 wake_up(&fi->page_waitq);
1069 /* Called under fc->lock, may release and reacquire it */
1070 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1071 __releases(&fc->lock)
1072 __acquires(&fc->lock)
1074 struct fuse_inode *fi = get_fuse_inode(req->inode);
1075 loff_t size = i_size_read(req->inode);
1076 struct fuse_write_in *inarg = &req->misc.write.in;
1081 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1082 inarg->size = PAGE_CACHE_SIZE;
1083 } else if (inarg->offset < size) {
1084 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1086 /* Got truncated off completely */
1090 req->in.args[1].size = inarg->size;
1092 fuse_request_send_background_locked(fc, req);
1096 fuse_writepage_finish(fc, req);
1097 spin_unlock(&fc->lock);
1098 fuse_writepage_free(fc, req);
1099 fuse_put_request(fc, req);
1100 spin_lock(&fc->lock);
1104 * If fi->writectr is positive (no truncate or fsync going on) send
1105 * all queued writepage requests.
1107 * Called with fc->lock
1109 void fuse_flush_writepages(struct inode *inode)
1110 __releases(&fc->lock)
1111 __acquires(&fc->lock)
1113 struct fuse_conn *fc = get_fuse_conn(inode);
1114 struct fuse_inode *fi = get_fuse_inode(inode);
1115 struct fuse_req *req;
1117 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1118 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1119 list_del_init(&req->list);
1120 fuse_send_writepage(fc, req);
1124 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1126 struct inode *inode = req->inode;
1127 struct fuse_inode *fi = get_fuse_inode(inode);
1129 mapping_set_error(inode->i_mapping, req->out.h.error);
1130 spin_lock(&fc->lock);
1132 fuse_writepage_finish(fc, req);
1133 spin_unlock(&fc->lock);
1134 fuse_writepage_free(fc, req);
1137 static int fuse_writepage_locked(struct page *page)
1139 struct address_space *mapping = page->mapping;
1140 struct inode *inode = mapping->host;
1141 struct fuse_conn *fc = get_fuse_conn(inode);
1142 struct fuse_inode *fi = get_fuse_inode(inode);
1143 struct fuse_req *req;
1144 struct fuse_file *ff;
1145 struct page *tmp_page;
1147 set_page_writeback(page);
1149 req = fuse_request_alloc_nofs();
1153 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1157 spin_lock(&fc->lock);
1158 BUG_ON(list_empty(&fi->write_files));
1159 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1160 req->ff = fuse_file_get(ff);
1161 spin_unlock(&fc->lock);
1163 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
1165 copy_highpage(tmp_page, page);
1167 req->pages[0] = tmp_page;
1168 req->page_offset = 0;
1169 req->end = fuse_writepage_end;
1172 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1173 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1174 end_page_writeback(page);
1176 spin_lock(&fc->lock);
1177 list_add(&req->writepages_entry, &fi->writepages);
1178 list_add_tail(&req->list, &fi->queued_writes);
1179 fuse_flush_writepages(inode);
1180 spin_unlock(&fc->lock);
1185 fuse_request_free(req);
1187 end_page_writeback(page);
1191 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1195 err = fuse_writepage_locked(page);
1201 static int fuse_launder_page(struct page *page)
1204 if (clear_page_dirty_for_io(page)) {
1205 struct inode *inode = page->mapping->host;
1206 err = fuse_writepage_locked(page);
1208 fuse_wait_on_page_writeback(inode, page->index);
1214 * Write back dirty pages now, because there may not be any suitable
1217 static void fuse_vma_close(struct vm_area_struct *vma)
1219 filemap_write_and_wait(vma->vm_file->f_mapping);
1223 * Wait for writeback against this page to complete before allowing it
1224 * to be marked dirty again, and hence written back again, possibly
1225 * before the previous writepage completed.
1227 * Block here, instead of in ->writepage(), so that the userspace fs
1228 * can only block processes actually operating on the filesystem.
1230 * Otherwise unprivileged userspace fs would be able to block
1235 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1237 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1239 struct page *page = vmf->page;
1241 * Don't use page->mapping as it may become NULL from a
1242 * concurrent truncate.
1244 struct inode *inode = vma->vm_file->f_mapping->host;
1246 fuse_wait_on_page_writeback(inode, page->index);
1250 static struct vm_operations_struct fuse_file_vm_ops = {
1251 .close = fuse_vma_close,
1252 .fault = filemap_fault,
1253 .page_mkwrite = fuse_page_mkwrite,
1256 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1258 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1259 struct inode *inode = file->f_dentry->d_inode;
1260 struct fuse_conn *fc = get_fuse_conn(inode);
1261 struct fuse_inode *fi = get_fuse_inode(inode);
1262 struct fuse_file *ff = file->private_data;
1264 * file may be written through mmap, so chain it onto the
1265 * inodes's write_file list
1267 spin_lock(&fc->lock);
1268 if (list_empty(&ff->write_entry))
1269 list_add(&ff->write_entry, &fi->write_files);
1270 spin_unlock(&fc->lock);
1272 file_accessed(file);
1273 vma->vm_ops = &fuse_file_vm_ops;
1277 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1278 struct file_lock *fl)
1280 switch (ffl->type) {
1286 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1287 ffl->end < ffl->start)
1290 fl->fl_start = ffl->start;
1291 fl->fl_end = ffl->end;
1292 fl->fl_pid = ffl->pid;
1298 fl->fl_type = ffl->type;
1302 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1303 const struct file_lock *fl, int opcode, pid_t pid,
1306 struct inode *inode = file->f_path.dentry->d_inode;
1307 struct fuse_conn *fc = get_fuse_conn(inode);
1308 struct fuse_file *ff = file->private_data;
1309 struct fuse_lk_in *arg = &req->misc.lk_in;
1312 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1313 arg->lk.start = fl->fl_start;
1314 arg->lk.end = fl->fl_end;
1315 arg->lk.type = fl->fl_type;
1318 arg->lk_flags |= FUSE_LK_FLOCK;
1319 req->in.h.opcode = opcode;
1320 req->in.h.nodeid = get_node_id(inode);
1321 req->in.numargs = 1;
1322 req->in.args[0].size = sizeof(*arg);
1323 req->in.args[0].value = arg;
1326 static int fuse_getlk(struct file *file, struct file_lock *fl)
1328 struct inode *inode = file->f_path.dentry->d_inode;
1329 struct fuse_conn *fc = get_fuse_conn(inode);
1330 struct fuse_req *req;
1331 struct fuse_lk_out outarg;
1334 req = fuse_get_req(fc);
1336 return PTR_ERR(req);
1338 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1339 req->out.numargs = 1;
1340 req->out.args[0].size = sizeof(outarg);
1341 req->out.args[0].value = &outarg;
1342 fuse_request_send(fc, req);
1343 err = req->out.h.error;
1344 fuse_put_request(fc, req);
1346 err = convert_fuse_file_lock(&outarg.lk, fl);
1351 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1353 struct inode *inode = file->f_path.dentry->d_inode;
1354 struct fuse_conn *fc = get_fuse_conn(inode);
1355 struct fuse_req *req;
1356 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1357 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1360 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1361 /* NLM needs asynchronous locks, which we don't support yet */
1365 /* Unlock on close is handled by the flush method */
1366 if (fl->fl_flags & FL_CLOSE)
1369 req = fuse_get_req(fc);
1371 return PTR_ERR(req);
1373 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1374 fuse_request_send(fc, req);
1375 err = req->out.h.error;
1376 /* locking is restartable */
1379 fuse_put_request(fc, req);
1383 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1385 struct inode *inode = file->f_path.dentry->d_inode;
1386 struct fuse_conn *fc = get_fuse_conn(inode);
1389 if (cmd == F_CANCELLK) {
1391 } else if (cmd == F_GETLK) {
1393 posix_test_lock(file, fl);
1396 err = fuse_getlk(file, fl);
1399 err = posix_lock_file(file, fl, NULL);
1401 err = fuse_setlk(file, fl, 0);
1406 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1408 struct inode *inode = file->f_path.dentry->d_inode;
1409 struct fuse_conn *fc = get_fuse_conn(inode);
1413 err = flock_lock_file_wait(file, fl);
1415 /* emulate flock with POSIX locks */
1416 fl->fl_owner = (fl_owner_t) file;
1417 err = fuse_setlk(file, fl, 1);
1423 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1425 struct inode *inode = mapping->host;
1426 struct fuse_conn *fc = get_fuse_conn(inode);
1427 struct fuse_req *req;
1428 struct fuse_bmap_in inarg;
1429 struct fuse_bmap_out outarg;
1432 if (!inode->i_sb->s_bdev || fc->no_bmap)
1435 req = fuse_get_req(fc);
1439 memset(&inarg, 0, sizeof(inarg));
1440 inarg.block = block;
1441 inarg.blocksize = inode->i_sb->s_blocksize;
1442 req->in.h.opcode = FUSE_BMAP;
1443 req->in.h.nodeid = get_node_id(inode);
1444 req->in.numargs = 1;
1445 req->in.args[0].size = sizeof(inarg);
1446 req->in.args[0].value = &inarg;
1447 req->out.numargs = 1;
1448 req->out.args[0].size = sizeof(outarg);
1449 req->out.args[0].value = &outarg;
1450 fuse_request_send(fc, req);
1451 err = req->out.h.error;
1452 fuse_put_request(fc, req);
1456 return err ? 0 : outarg.block;
1459 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1462 struct inode *inode = file->f_path.dentry->d_inode;
1464 mutex_lock(&inode->i_mutex);
1467 retval = fuse_update_attributes(inode, NULL, file, NULL);
1470 offset += i_size_read(inode);
1473 offset += file->f_pos;
1476 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1477 if (offset != file->f_pos) {
1478 file->f_pos = offset;
1479 file->f_version = 0;
1484 mutex_unlock(&inode->i_mutex);
1488 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1489 unsigned int nr_segs, size_t bytes, bool to_user)
1497 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1499 while (iov_iter_count(&ii)) {
1500 struct page *page = pages[page_idx++];
1501 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1504 kaddr = map = kmap(page);
1507 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1508 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1509 size_t copy = min(todo, iov_len);
1513 left = copy_from_user(kaddr, uaddr, copy);
1515 left = copy_to_user(uaddr, kaddr, copy);
1520 iov_iter_advance(&ii, copy);
1532 * For ioctls, there is no generic way to determine how much memory
1533 * needs to be read and/or written. Furthermore, ioctls are allowed
1534 * to dereference the passed pointer, so the parameter requires deep
1535 * copying but FUSE has no idea whatsoever about what to copy in or
1538 * This is solved by allowing FUSE server to retry ioctl with
1539 * necessary in/out iovecs. Let's assume the ioctl implementation
1540 * needs to read in the following structure.
1547 * On the first callout to FUSE server, inarg->in_size and
1548 * inarg->out_size will be NULL; then, the server completes the ioctl
1549 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1550 * the actual iov array to
1552 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1554 * which tells FUSE to copy in the requested area and retry the ioctl.
1555 * On the second round, the server has access to the structure and
1556 * from that it can tell what to look for next, so on the invocation,
1557 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1559 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1560 * { .iov_base = a.buf, .iov_len = a.buflen } }
1562 * FUSE will copy both struct a and the pointed buffer from the
1563 * process doing the ioctl and retry ioctl with both struct a and the
1566 * This time, FUSE server has everything it needs and completes ioctl
1567 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1569 * Copying data out works the same way.
1571 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1572 * automatically initializes in and out iovs by decoding @cmd with
1573 * _IOC_* macros and the server is not allowed to request RETRY. This
1574 * limits ioctl data transfers to well-formed ioctls and is the forced
1575 * behavior for all FUSE servers.
1577 static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
1578 unsigned long arg, unsigned int flags)
1580 struct inode *inode = file->f_dentry->d_inode;
1581 struct fuse_file *ff = file->private_data;
1582 struct fuse_conn *fc = get_fuse_conn(inode);
1583 struct fuse_ioctl_in inarg = {
1589 struct fuse_ioctl_out outarg;
1590 struct fuse_req *req = NULL;
1591 struct page **pages = NULL;
1592 struct page *iov_page = NULL;
1593 struct iovec *in_iov = NULL, *out_iov = NULL;
1594 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1595 size_t in_size, out_size, transferred;
1598 /* assume all the iovs returned by client always fits in a page */
1599 BUILD_BUG_ON(sizeof(struct iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1601 if (!fuse_allow_task(fc, current))
1605 if (is_bad_inode(inode))
1609 pages = kzalloc(sizeof(pages[0]) * FUSE_MAX_PAGES_PER_REQ, GFP_KERNEL);
1610 iov_page = alloc_page(GFP_KERNEL);
1611 if (!pages || !iov_page)
1615 * If restricted, initialize IO parameters as encoded in @cmd.
1616 * RETRY from server is not allowed.
1618 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1619 struct iovec *iov = page_address(iov_page);
1621 iov->iov_base = (void __user *)arg;
1622 iov->iov_len = _IOC_SIZE(cmd);
1624 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1629 if (_IOC_DIR(cmd) & _IOC_READ) {
1636 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1637 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1640 * Out data can be used either for actual out data or iovs,
1641 * make sure there always is at least one page.
1643 out_size = max_t(size_t, out_size, PAGE_SIZE);
1644 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1646 /* make sure there are enough buffer pages and init request with them */
1648 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1650 while (num_pages < max_pages) {
1651 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1652 if (!pages[num_pages])
1657 req = fuse_get_req(fc);
1663 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1664 req->num_pages = num_pages;
1666 /* okay, let's send it to the client */
1667 req->in.h.opcode = FUSE_IOCTL;
1668 req->in.h.nodeid = get_node_id(inode);
1669 req->in.numargs = 1;
1670 req->in.args[0].size = sizeof(inarg);
1671 req->in.args[0].value = &inarg;
1674 req->in.args[1].size = in_size;
1675 req->in.argpages = 1;
1677 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1683 req->out.numargs = 2;
1684 req->out.args[0].size = sizeof(outarg);
1685 req->out.args[0].value = &outarg;
1686 req->out.args[1].size = out_size;
1687 req->out.argpages = 1;
1688 req->out.argvar = 1;
1690 fuse_request_send(fc, req);
1691 err = req->out.h.error;
1692 transferred = req->out.args[1].size;
1693 fuse_put_request(fc, req);
1698 /* did it ask for retry? */
1699 if (outarg.flags & FUSE_IOCTL_RETRY) {
1702 /* no retry if in restricted mode */
1704 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
1707 in_iovs = outarg.in_iovs;
1708 out_iovs = outarg.out_iovs;
1711 * Make sure things are in boundary, separate checks
1712 * are to protect against overflow.
1715 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
1716 out_iovs > FUSE_IOCTL_MAX_IOV ||
1717 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
1721 if ((in_iovs + out_iovs) * sizeof(struct iovec) != transferred)
1724 /* okay, copy in iovs and retry */
1725 vaddr = kmap_atomic(pages[0], KM_USER0);
1726 memcpy(page_address(iov_page), vaddr, transferred);
1727 kunmap_atomic(vaddr, KM_USER0);
1729 in_iov = page_address(iov_page);
1730 out_iov = in_iov + in_iovs;
1736 if (transferred > inarg.out_size)
1739 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
1742 fuse_put_request(fc, req);
1744 __free_page(iov_page);
1746 __free_page(pages[--num_pages]);
1749 return err ? err : outarg.result;
1752 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
1755 return fuse_file_do_ioctl(file, cmd, arg, 0);
1758 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
1761 return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
1765 * All files which have been polled are linked to RB tree
1766 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1767 * find the matching one.
1769 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
1770 struct rb_node **parent_out)
1772 struct rb_node **link = &fc->polled_files.rb_node;
1773 struct rb_node *last = NULL;
1776 struct fuse_file *ff;
1779 ff = rb_entry(last, struct fuse_file, polled_node);
1782 link = &last->rb_left;
1783 else if (kh > ff->kh)
1784 link = &last->rb_right;
1795 * The file is about to be polled. Make sure it's on the polled_files
1796 * RB tree. Note that files once added to the polled_files tree are
1797 * not removed before the file is released. This is because a file
1798 * polled once is likely to be polled again.
1800 static void fuse_register_polled_file(struct fuse_conn *fc,
1801 struct fuse_file *ff)
1803 spin_lock(&fc->lock);
1804 if (RB_EMPTY_NODE(&ff->polled_node)) {
1805 struct rb_node **link, *parent;
1807 link = fuse_find_polled_node(fc, ff->kh, &parent);
1809 rb_link_node(&ff->polled_node, parent, link);
1810 rb_insert_color(&ff->polled_node, &fc->polled_files);
1812 spin_unlock(&fc->lock);
1815 static unsigned fuse_file_poll(struct file *file, poll_table *wait)
1817 struct inode *inode = file->f_dentry->d_inode;
1818 struct fuse_file *ff = file->private_data;
1819 struct fuse_conn *fc = get_fuse_conn(inode);
1820 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
1821 struct fuse_poll_out outarg;
1822 struct fuse_req *req;
1826 return DEFAULT_POLLMASK;
1828 poll_wait(file, &ff->poll_wait, wait);
1831 * Ask for notification iff there's someone waiting for it.
1832 * The client may ignore the flag and always notify.
1834 if (waitqueue_active(&ff->poll_wait)) {
1835 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
1836 fuse_register_polled_file(fc, ff);
1839 req = fuse_get_req(fc);
1841 return PTR_ERR(req);
1843 req->in.h.opcode = FUSE_POLL;
1844 req->in.h.nodeid = get_node_id(inode);
1845 req->in.numargs = 1;
1846 req->in.args[0].size = sizeof(inarg);
1847 req->in.args[0].value = &inarg;
1848 req->out.numargs = 1;
1849 req->out.args[0].size = sizeof(outarg);
1850 req->out.args[0].value = &outarg;
1851 fuse_request_send(fc, req);
1852 err = req->out.h.error;
1853 fuse_put_request(fc, req);
1856 return outarg.revents;
1857 if (err == -ENOSYS) {
1859 return DEFAULT_POLLMASK;
1865 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1866 * wakes up the poll waiters.
1868 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
1869 struct fuse_notify_poll_wakeup_out *outarg)
1871 u64 kh = outarg->kh;
1872 struct rb_node **link;
1874 spin_lock(&fc->lock);
1876 link = fuse_find_polled_node(fc, kh, NULL);
1878 struct fuse_file *ff;
1880 ff = rb_entry(*link, struct fuse_file, polled_node);
1881 wake_up_interruptible_sync(&ff->poll_wait);
1884 spin_unlock(&fc->lock);
1888 static const struct file_operations fuse_file_operations = {
1889 .llseek = fuse_file_llseek,
1890 .read = do_sync_read,
1891 .aio_read = fuse_file_aio_read,
1892 .write = do_sync_write,
1893 .aio_write = fuse_file_aio_write,
1894 .mmap = fuse_file_mmap,
1896 .flush = fuse_flush,
1897 .release = fuse_release,
1898 .fsync = fuse_fsync,
1899 .lock = fuse_file_lock,
1900 .flock = fuse_file_flock,
1901 .splice_read = generic_file_splice_read,
1902 .unlocked_ioctl = fuse_file_ioctl,
1903 .compat_ioctl = fuse_file_compat_ioctl,
1904 .poll = fuse_file_poll,
1907 static const struct file_operations fuse_direct_io_file_operations = {
1908 .llseek = fuse_file_llseek,
1909 .read = fuse_direct_read,
1910 .write = fuse_direct_write,
1912 .flush = fuse_flush,
1913 .release = fuse_release,
1914 .fsync = fuse_fsync,
1915 .lock = fuse_file_lock,
1916 .flock = fuse_file_flock,
1917 .unlocked_ioctl = fuse_file_ioctl,
1918 .compat_ioctl = fuse_file_compat_ioctl,
1919 .poll = fuse_file_poll,
1920 /* no mmap and splice_read */
1923 static const struct address_space_operations fuse_file_aops = {
1924 .readpage = fuse_readpage,
1925 .writepage = fuse_writepage,
1926 .launder_page = fuse_launder_page,
1927 .write_begin = fuse_write_begin,
1928 .write_end = fuse_write_end,
1929 .readpages = fuse_readpages,
1930 .set_page_dirty = __set_page_dirty_nobuffers,
1934 void fuse_init_file_inode(struct inode *inode)
1936 inode->i_fop = &fuse_file_operations;
1937 inode->i_data.a_ops = &fuse_file_aops;