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 request_send(fc, req);
43 err = req->out.h.error;
44 fuse_put_request(fc, req);
49 struct fuse_file *fuse_file_alloc(void)
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);
66 void fuse_file_free(struct fuse_file *ff)
68 fuse_request_free(ff->reserved_req);
72 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
74 atomic_inc(&ff->count);
78 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
80 dput(req->misc.release.dentry);
81 mntput(req->misc.release.vfsmount);
84 static void fuse_file_put(struct fuse_file *ff)
86 if (atomic_dec_and_test(&ff->count)) {
87 struct fuse_req *req = ff->reserved_req;
88 struct inode *inode = req->misc.release.dentry->d_inode;
89 struct fuse_conn *fc = get_fuse_conn(inode);
90 req->end = fuse_release_end;
91 request_send_background(fc, req);
96 void fuse_finish_open(struct inode *inode, struct file *file,
97 struct fuse_file *ff, struct fuse_open_out *outarg)
99 if (outarg->open_flags & FOPEN_DIRECT_IO)
100 file->f_op = &fuse_direct_io_file_operations;
101 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
102 invalidate_inode_pages2(inode->i_mapping);
103 if (outarg->open_flags & FOPEN_NONSEEKABLE)
104 nonseekable_open(inode, file);
106 file->private_data = fuse_file_get(ff);
109 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
111 struct fuse_open_out outarg;
112 struct fuse_file *ff;
115 /* VFS checks this, but only _after_ ->open() */
116 if (file->f_flags & O_DIRECT)
119 err = generic_file_open(inode, file);
123 ff = fuse_file_alloc();
127 err = fuse_send_open(inode, file, isdir, &outarg);
132 outarg.open_flags &= ~FOPEN_DIRECT_IO;
133 fuse_finish_open(inode, file, ff, &outarg);
139 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
141 struct fuse_req *req = ff->reserved_req;
142 struct fuse_release_in *inarg = &req->misc.release.in;
145 inarg->flags = flags;
146 req->in.h.opcode = opcode;
147 req->in.h.nodeid = nodeid;
149 req->in.args[0].size = sizeof(struct fuse_release_in);
150 req->in.args[0].value = inarg;
153 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
155 struct fuse_file *ff = file->private_data;
157 struct fuse_conn *fc = get_fuse_conn(inode);
158 struct fuse_req *req = ff->reserved_req;
160 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
161 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
163 /* Hold vfsmount and dentry until release is finished */
164 req->misc.release.vfsmount = mntget(file->f_path.mnt);
165 req->misc.release.dentry = dget(file->f_path.dentry);
167 spin_lock(&fc->lock);
168 list_del(&ff->write_entry);
169 spin_unlock(&fc->lock);
171 * Normally this will send the RELEASE request,
172 * however if some asynchronous READ or WRITE requests
173 * are outstanding, the sending will be delayed
178 /* Return value is ignored by VFS */
182 static int fuse_open(struct inode *inode, struct file *file)
184 return fuse_open_common(inode, file, 0);
187 static int fuse_release(struct inode *inode, struct file *file)
189 return fuse_release_common(inode, file, 0);
193 * Scramble the ID space with XTEA, so that the value of the files_struct
194 * pointer is not exposed to userspace.
196 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
198 u32 *k = fc->scramble_key;
199 u64 v = (unsigned long) id;
205 for (i = 0; i < 32; i++) {
206 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
208 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
211 return (u64) v0 + ((u64) v1 << 32);
215 * Check if page is under writeback
217 * This is currently done by walking the list of writepage requests
218 * for the inode, which can be pretty inefficient.
220 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
222 struct fuse_conn *fc = get_fuse_conn(inode);
223 struct fuse_inode *fi = get_fuse_inode(inode);
224 struct fuse_req *req;
227 spin_lock(&fc->lock);
228 list_for_each_entry(req, &fi->writepages, writepages_entry) {
231 BUG_ON(req->inode != inode);
232 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
233 if (curr_index == index) {
238 spin_unlock(&fc->lock);
244 * Wait for page writeback to be completed.
246 * Since fuse doesn't rely on the VM writeback tracking, this has to
247 * use some other means.
249 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
251 struct fuse_inode *fi = get_fuse_inode(inode);
253 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
257 static int fuse_flush(struct file *file, fl_owner_t id)
259 struct inode *inode = file->f_path.dentry->d_inode;
260 struct fuse_conn *fc = get_fuse_conn(inode);
261 struct fuse_file *ff = file->private_data;
262 struct fuse_req *req;
263 struct fuse_flush_in inarg;
266 if (is_bad_inode(inode))
272 req = fuse_get_req_nofail(fc, file);
273 memset(&inarg, 0, sizeof(inarg));
275 inarg.lock_owner = fuse_lock_owner_id(fc, id);
276 req->in.h.opcode = FUSE_FLUSH;
277 req->in.h.nodeid = get_node_id(inode);
279 req->in.args[0].size = sizeof(inarg);
280 req->in.args[0].value = &inarg;
282 request_send(fc, req);
283 err = req->out.h.error;
284 fuse_put_request(fc, req);
285 if (err == -ENOSYS) {
293 * Wait for all pending writepages on the inode to finish.
295 * This is currently done by blocking further writes with FUSE_NOWRITE
296 * and waiting for all sent writes to complete.
298 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
299 * could conflict with truncation.
301 static void fuse_sync_writes(struct inode *inode)
303 fuse_set_nowrite(inode);
304 fuse_release_nowrite(inode);
307 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
310 struct inode *inode = de->d_inode;
311 struct fuse_conn *fc = get_fuse_conn(inode);
312 struct fuse_file *ff = file->private_data;
313 struct fuse_req *req;
314 struct fuse_fsync_in inarg;
317 if (is_bad_inode(inode))
320 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
324 * Start writeback against all dirty pages of the inode, then
325 * wait for all outstanding writes, before sending the FSYNC
328 err = write_inode_now(inode, 0);
332 fuse_sync_writes(inode);
334 req = fuse_get_req(fc);
338 memset(&inarg, 0, sizeof(inarg));
340 inarg.fsync_flags = datasync ? 1 : 0;
341 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
342 req->in.h.nodeid = get_node_id(inode);
344 req->in.args[0].size = sizeof(inarg);
345 req->in.args[0].value = &inarg;
346 request_send(fc, req);
347 err = req->out.h.error;
348 fuse_put_request(fc, req);
349 if (err == -ENOSYS) {
359 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
361 return fuse_fsync_common(file, de, datasync, 0);
364 void fuse_read_fill(struct fuse_req *req, struct file *file,
365 struct inode *inode, loff_t pos, size_t count, int opcode)
367 struct fuse_read_in *inarg = &req->misc.read.in;
368 struct fuse_file *ff = file->private_data;
373 inarg->flags = file->f_flags;
374 req->in.h.opcode = opcode;
375 req->in.h.nodeid = get_node_id(inode);
377 req->in.args[0].size = sizeof(struct fuse_read_in);
378 req->in.args[0].value = inarg;
379 req->out.argpages = 1;
381 req->out.numargs = 1;
382 req->out.args[0].size = count;
385 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
386 struct inode *inode, loff_t pos, size_t count,
389 struct fuse_conn *fc = get_fuse_conn(inode);
391 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
393 struct fuse_read_in *inarg = &req->misc.read.in;
395 inarg->read_flags |= FUSE_READ_LOCKOWNER;
396 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
398 request_send(fc, req);
399 return req->out.args[0].size;
402 static void fuse_read_update_size(struct inode *inode, loff_t size,
405 struct fuse_conn *fc = get_fuse_conn(inode);
406 struct fuse_inode *fi = get_fuse_inode(inode);
408 spin_lock(&fc->lock);
409 if (attr_ver == fi->attr_version && size < inode->i_size) {
410 fi->attr_version = ++fc->attr_version;
411 i_size_write(inode, size);
413 spin_unlock(&fc->lock);
416 static int fuse_readpage(struct file *file, struct page *page)
418 struct inode *inode = page->mapping->host;
419 struct fuse_conn *fc = get_fuse_conn(inode);
420 struct fuse_req *req;
422 loff_t pos = page_offset(page);
423 size_t count = PAGE_CACHE_SIZE;
428 if (is_bad_inode(inode))
432 * Page writeback can extend beyond the liftime of the
433 * page-cache page, so make sure we read a properly synced
436 fuse_wait_on_page_writeback(inode, page->index);
438 req = fuse_get_req(fc);
443 attr_ver = fuse_get_attr_version(fc);
445 req->out.page_zeroing = 1;
447 req->pages[0] = page;
448 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
449 err = req->out.h.error;
450 fuse_put_request(fc, req);
454 * Short read means EOF. If file size is larger, truncate it
456 if (num_read < count)
457 fuse_read_update_size(inode, pos + num_read, attr_ver);
459 SetPageUptodate(page);
462 fuse_invalidate_attr(inode); /* atime changed */
468 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
471 size_t count = req->misc.read.in.size;
472 size_t num_read = req->out.args[0].size;
473 struct inode *inode = req->pages[0]->mapping->host;
476 * Short read means EOF. If file size is larger, truncate it
478 if (!req->out.h.error && num_read < count) {
479 loff_t pos = page_offset(req->pages[0]) + num_read;
480 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
483 fuse_invalidate_attr(inode); /* atime changed */
485 for (i = 0; i < req->num_pages; i++) {
486 struct page *page = req->pages[i];
487 if (!req->out.h.error)
488 SetPageUptodate(page);
494 fuse_file_put(req->ff);
497 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
500 struct fuse_conn *fc = get_fuse_conn(inode);
501 loff_t pos = page_offset(req->pages[0]);
502 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
503 req->out.page_zeroing = 1;
504 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
505 req->misc.read.attr_ver = fuse_get_attr_version(fc);
506 if (fc->async_read) {
507 struct fuse_file *ff = file->private_data;
508 req->ff = fuse_file_get(ff);
509 req->end = fuse_readpages_end;
510 request_send_background(fc, req);
512 request_send(fc, req);
513 fuse_readpages_end(fc, req);
514 fuse_put_request(fc, req);
518 struct fuse_fill_data {
519 struct fuse_req *req;
524 static int fuse_readpages_fill(void *_data, struct page *page)
526 struct fuse_fill_data *data = _data;
527 struct fuse_req *req = data->req;
528 struct inode *inode = data->inode;
529 struct fuse_conn *fc = get_fuse_conn(inode);
531 fuse_wait_on_page_writeback(inode, page->index);
533 if (req->num_pages &&
534 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
535 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
536 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
537 fuse_send_readpages(req, data->file, inode);
538 data->req = req = fuse_get_req(fc);
544 req->pages[req->num_pages] = page;
549 static int fuse_readpages(struct file *file, struct address_space *mapping,
550 struct list_head *pages, unsigned nr_pages)
552 struct inode *inode = mapping->host;
553 struct fuse_conn *fc = get_fuse_conn(inode);
554 struct fuse_fill_data data;
558 if (is_bad_inode(inode))
563 data.req = fuse_get_req(fc);
564 err = PTR_ERR(data.req);
565 if (IS_ERR(data.req))
568 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
570 if (data.req->num_pages)
571 fuse_send_readpages(data.req, file, inode);
573 fuse_put_request(fc, data.req);
579 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
580 unsigned long nr_segs, loff_t pos)
582 struct inode *inode = iocb->ki_filp->f_mapping->host;
584 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
587 * If trying to read past EOF, make sure the i_size
588 * attribute is up-to-date.
590 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
595 return generic_file_aio_read(iocb, iov, nr_segs, pos);
598 static void fuse_write_fill(struct fuse_req *req, struct file *file,
599 struct fuse_file *ff, struct inode *inode,
600 loff_t pos, size_t count, int writepage)
602 struct fuse_conn *fc = get_fuse_conn(inode);
603 struct fuse_write_in *inarg = &req->misc.write.in;
604 struct fuse_write_out *outarg = &req->misc.write.out;
606 memset(inarg, 0, sizeof(struct fuse_write_in));
610 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
611 inarg->flags = file ? file->f_flags : 0;
612 req->in.h.opcode = FUSE_WRITE;
613 req->in.h.nodeid = get_node_id(inode);
614 req->in.argpages = 1;
617 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
619 req->in.args[0].size = sizeof(struct fuse_write_in);
620 req->in.args[0].value = inarg;
621 req->in.args[1].size = count;
622 req->out.numargs = 1;
623 req->out.args[0].size = sizeof(struct fuse_write_out);
624 req->out.args[0].value = outarg;
627 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
628 struct inode *inode, loff_t pos, size_t count,
631 struct fuse_conn *fc = get_fuse_conn(inode);
632 fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
634 struct fuse_write_in *inarg = &req->misc.write.in;
635 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
636 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
638 request_send(fc, req);
639 return req->misc.write.out.size;
642 static int fuse_write_begin(struct file *file, struct address_space *mapping,
643 loff_t pos, unsigned len, unsigned flags,
644 struct page **pagep, void **fsdata)
646 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
648 *pagep = __grab_cache_page(mapping, index);
654 static void fuse_write_update_size(struct inode *inode, loff_t pos)
656 struct fuse_conn *fc = get_fuse_conn(inode);
657 struct fuse_inode *fi = get_fuse_inode(inode);
659 spin_lock(&fc->lock);
660 fi->attr_version = ++fc->attr_version;
661 if (pos > inode->i_size)
662 i_size_write(inode, pos);
663 spin_unlock(&fc->lock);
666 static int fuse_buffered_write(struct file *file, struct inode *inode,
667 loff_t pos, unsigned count, struct page *page)
671 struct fuse_conn *fc = get_fuse_conn(inode);
672 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
673 struct fuse_req *req;
675 if (is_bad_inode(inode))
679 * Make sure writepages on the same page are not mixed up with
682 fuse_wait_on_page_writeback(inode, page->index);
684 req = fuse_get_req(fc);
689 req->pages[0] = page;
690 req->page_offset = offset;
691 nres = fuse_send_write(req, file, inode, pos, count, NULL);
692 err = req->out.h.error;
693 fuse_put_request(fc, req);
698 fuse_write_update_size(inode, pos);
699 if (count == PAGE_CACHE_SIZE)
700 SetPageUptodate(page);
702 fuse_invalidate_attr(inode);
703 return err ? err : nres;
706 static int fuse_write_end(struct file *file, struct address_space *mapping,
707 loff_t pos, unsigned len, unsigned copied,
708 struct page *page, void *fsdata)
710 struct inode *inode = mapping->host;
714 res = fuse_buffered_write(file, inode, pos, copied, page);
717 page_cache_release(page);
721 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
722 struct inode *inode, loff_t pos,
729 for (i = 0; i < req->num_pages; i++)
730 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
732 res = fuse_send_write(req, file, inode, pos, count, NULL);
734 offset = req->page_offset;
736 for (i = 0; i < req->num_pages; i++) {
737 struct page *page = req->pages[i];
739 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
740 SetPageUptodate(page);
742 if (count > PAGE_CACHE_SIZE - offset)
743 count -= PAGE_CACHE_SIZE - offset;
749 page_cache_release(page);
755 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
756 struct address_space *mapping,
757 struct iov_iter *ii, loff_t pos)
759 struct fuse_conn *fc = get_fuse_conn(mapping->host);
760 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
764 req->page_offset = offset;
769 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
770 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
773 bytes = min_t(size_t, bytes, fc->max_write - count);
777 if (iov_iter_fault_in_readable(ii, bytes))
781 page = __grab_cache_page(mapping, index);
786 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
788 flush_dcache_page(page);
792 page_cache_release(page);
793 bytes = min(bytes, iov_iter_single_seg_count(ii));
798 req->pages[req->num_pages] = page;
801 iov_iter_advance(ii, tmp);
805 if (offset == PAGE_CACHE_SIZE)
810 } while (iov_iter_count(ii) && count < fc->max_write &&
811 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
813 return count > 0 ? count : err;
816 static ssize_t fuse_perform_write(struct file *file,
817 struct address_space *mapping,
818 struct iov_iter *ii, loff_t pos)
820 struct inode *inode = mapping->host;
821 struct fuse_conn *fc = get_fuse_conn(inode);
825 if (is_bad_inode(inode))
829 struct fuse_req *req;
832 req = fuse_get_req(fc);
838 count = fuse_fill_write_pages(req, mapping, ii, pos);
844 num_written = fuse_send_write_pages(req, file, inode,
846 err = req->out.h.error;
851 /* break out of the loop on short write */
852 if (num_written != count)
856 fuse_put_request(fc, req);
857 } while (!err && iov_iter_count(ii));
860 fuse_write_update_size(inode, pos);
862 fuse_invalidate_attr(inode);
864 return res > 0 ? res : err;
867 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
868 unsigned long nr_segs, loff_t pos)
870 struct file *file = iocb->ki_filp;
871 struct address_space *mapping = file->f_mapping;
874 struct inode *inode = mapping->host;
878 WARN_ON(iocb->ki_pos != pos);
880 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
884 mutex_lock(&inode->i_mutex);
885 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
887 /* We can write back this queue in page reclaim */
888 current->backing_dev_info = mapping->backing_dev_info;
890 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
897 err = file_remove_suid(file);
901 file_update_time(file);
903 iov_iter_init(&i, iov, nr_segs, count, 0);
904 written = fuse_perform_write(file, mapping, &i, pos);
906 iocb->ki_pos = pos + written;
909 current->backing_dev_info = NULL;
910 mutex_unlock(&inode->i_mutex);
912 return written ? written : err;
915 static void fuse_release_user_pages(struct fuse_req *req, int write)
919 for (i = 0; i < req->num_pages; i++) {
920 struct page *page = req->pages[i];
922 set_page_dirty_lock(page);
927 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
928 unsigned nbytes, int write)
930 unsigned long user_addr = (unsigned long) buf;
931 unsigned offset = user_addr & ~PAGE_MASK;
934 /* This doesn't work with nfsd */
938 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
939 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
940 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
941 down_read(¤t->mm->mmap_sem);
942 npages = get_user_pages(current, current->mm, user_addr, npages, write,
943 0, req->pages, NULL);
944 up_read(¤t->mm->mmap_sem);
948 req->num_pages = npages;
949 req->page_offset = offset;
953 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
954 size_t count, loff_t *ppos, int write)
956 struct inode *inode = file->f_path.dentry->d_inode;
957 struct fuse_conn *fc = get_fuse_conn(inode);
958 size_t nmax = write ? fc->max_write : fc->max_read;
961 struct fuse_req *req;
963 if (is_bad_inode(inode))
966 req = fuse_get_req(fc);
972 size_t nbytes_limit = min(count, nmax);
974 int err = fuse_get_user_pages(req, buf, nbytes_limit, !write);
979 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
980 nbytes = min(nbytes_limit, nbytes);
982 nres = fuse_send_write(req, file, inode, pos, nbytes,
985 nres = fuse_send_read(req, file, inode, pos, nbytes,
987 fuse_release_user_pages(req, !write);
988 if (req->out.h.error) {
990 res = req->out.h.error;
992 } else if (nres > nbytes) {
1003 fuse_put_request(fc, req);
1004 req = fuse_get_req(fc);
1009 fuse_put_request(fc, req);
1012 fuse_write_update_size(inode, pos);
1015 fuse_invalidate_attr(inode);
1020 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1021 size_t count, loff_t *ppos)
1023 return fuse_direct_io(file, buf, count, ppos, 0);
1026 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1027 size_t count, loff_t *ppos)
1029 struct inode *inode = file->f_path.dentry->d_inode;
1031 /* Don't allow parallel writes to the same file */
1032 mutex_lock(&inode->i_mutex);
1033 res = generic_write_checks(file, ppos, &count, 0);
1035 res = fuse_direct_io(file, buf, count, ppos, 1);
1036 mutex_unlock(&inode->i_mutex);
1040 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1042 __free_page(req->pages[0]);
1043 fuse_file_put(req->ff);
1046 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1048 struct inode *inode = req->inode;
1049 struct fuse_inode *fi = get_fuse_inode(inode);
1050 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1052 list_del(&req->writepages_entry);
1053 dec_bdi_stat(bdi, BDI_WRITEBACK);
1054 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1055 bdi_writeout_inc(bdi);
1056 wake_up(&fi->page_waitq);
1059 /* Called under fc->lock, may release and reacquire it */
1060 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1062 struct fuse_inode *fi = get_fuse_inode(req->inode);
1063 loff_t size = i_size_read(req->inode);
1064 struct fuse_write_in *inarg = &req->misc.write.in;
1069 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1070 inarg->size = PAGE_CACHE_SIZE;
1071 } else if (inarg->offset < size) {
1072 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1074 /* Got truncated off completely */
1078 req->in.args[1].size = inarg->size;
1080 request_send_background_locked(fc, req);
1084 fuse_writepage_finish(fc, req);
1085 spin_unlock(&fc->lock);
1086 fuse_writepage_free(fc, req);
1087 fuse_put_request(fc, req);
1088 spin_lock(&fc->lock);
1092 * If fi->writectr is positive (no truncate or fsync going on) send
1093 * all queued writepage requests.
1095 * Called with fc->lock
1097 void fuse_flush_writepages(struct inode *inode)
1099 struct fuse_conn *fc = get_fuse_conn(inode);
1100 struct fuse_inode *fi = get_fuse_inode(inode);
1101 struct fuse_req *req;
1103 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1104 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1105 list_del_init(&req->list);
1106 fuse_send_writepage(fc, req);
1110 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1112 struct inode *inode = req->inode;
1113 struct fuse_inode *fi = get_fuse_inode(inode);
1115 mapping_set_error(inode->i_mapping, req->out.h.error);
1116 spin_lock(&fc->lock);
1118 fuse_writepage_finish(fc, req);
1119 spin_unlock(&fc->lock);
1120 fuse_writepage_free(fc, req);
1123 static int fuse_writepage_locked(struct page *page)
1125 struct address_space *mapping = page->mapping;
1126 struct inode *inode = mapping->host;
1127 struct fuse_conn *fc = get_fuse_conn(inode);
1128 struct fuse_inode *fi = get_fuse_inode(inode);
1129 struct fuse_req *req;
1130 struct fuse_file *ff;
1131 struct page *tmp_page;
1133 set_page_writeback(page);
1135 req = fuse_request_alloc_nofs();
1139 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1143 spin_lock(&fc->lock);
1144 BUG_ON(list_empty(&fi->write_files));
1145 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1146 req->ff = fuse_file_get(ff);
1147 spin_unlock(&fc->lock);
1149 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
1151 copy_highpage(tmp_page, page);
1153 req->pages[0] = tmp_page;
1154 req->page_offset = 0;
1155 req->end = fuse_writepage_end;
1158 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1159 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1160 end_page_writeback(page);
1162 spin_lock(&fc->lock);
1163 list_add(&req->writepages_entry, &fi->writepages);
1164 list_add_tail(&req->list, &fi->queued_writes);
1165 fuse_flush_writepages(inode);
1166 spin_unlock(&fc->lock);
1171 fuse_request_free(req);
1173 end_page_writeback(page);
1177 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1181 err = fuse_writepage_locked(page);
1187 static int fuse_launder_page(struct page *page)
1190 if (clear_page_dirty_for_io(page)) {
1191 struct inode *inode = page->mapping->host;
1192 err = fuse_writepage_locked(page);
1194 fuse_wait_on_page_writeback(inode, page->index);
1200 * Write back dirty pages now, because there may not be any suitable
1203 static void fuse_vma_close(struct vm_area_struct *vma)
1205 filemap_write_and_wait(vma->vm_file->f_mapping);
1209 * Wait for writeback against this page to complete before allowing it
1210 * to be marked dirty again, and hence written back again, possibly
1211 * before the previous writepage completed.
1213 * Block here, instead of in ->writepage(), so that the userspace fs
1214 * can only block processes actually operating on the filesystem.
1216 * Otherwise unprivileged userspace fs would be able to block
1221 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1223 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1226 * Don't use page->mapping as it may become NULL from a
1227 * concurrent truncate.
1229 struct inode *inode = vma->vm_file->f_mapping->host;
1231 fuse_wait_on_page_writeback(inode, page->index);
1235 static struct vm_operations_struct fuse_file_vm_ops = {
1236 .close = fuse_vma_close,
1237 .fault = filemap_fault,
1238 .page_mkwrite = fuse_page_mkwrite,
1241 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1243 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1244 struct inode *inode = file->f_dentry->d_inode;
1245 struct fuse_conn *fc = get_fuse_conn(inode);
1246 struct fuse_inode *fi = get_fuse_inode(inode);
1247 struct fuse_file *ff = file->private_data;
1249 * file may be written through mmap, so chain it onto the
1250 * inodes's write_file list
1252 spin_lock(&fc->lock);
1253 if (list_empty(&ff->write_entry))
1254 list_add(&ff->write_entry, &fi->write_files);
1255 spin_unlock(&fc->lock);
1257 file_accessed(file);
1258 vma->vm_ops = &fuse_file_vm_ops;
1262 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1263 struct file_lock *fl)
1265 switch (ffl->type) {
1271 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1272 ffl->end < ffl->start)
1275 fl->fl_start = ffl->start;
1276 fl->fl_end = ffl->end;
1277 fl->fl_pid = ffl->pid;
1283 fl->fl_type = ffl->type;
1287 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1288 const struct file_lock *fl, int opcode, pid_t pid,
1291 struct inode *inode = file->f_path.dentry->d_inode;
1292 struct fuse_conn *fc = get_fuse_conn(inode);
1293 struct fuse_file *ff = file->private_data;
1294 struct fuse_lk_in *arg = &req->misc.lk_in;
1297 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1298 arg->lk.start = fl->fl_start;
1299 arg->lk.end = fl->fl_end;
1300 arg->lk.type = fl->fl_type;
1303 arg->lk_flags |= FUSE_LK_FLOCK;
1304 req->in.h.opcode = opcode;
1305 req->in.h.nodeid = get_node_id(inode);
1306 req->in.numargs = 1;
1307 req->in.args[0].size = sizeof(*arg);
1308 req->in.args[0].value = arg;
1311 static int fuse_getlk(struct file *file, struct file_lock *fl)
1313 struct inode *inode = file->f_path.dentry->d_inode;
1314 struct fuse_conn *fc = get_fuse_conn(inode);
1315 struct fuse_req *req;
1316 struct fuse_lk_out outarg;
1319 req = fuse_get_req(fc);
1321 return PTR_ERR(req);
1323 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1324 req->out.numargs = 1;
1325 req->out.args[0].size = sizeof(outarg);
1326 req->out.args[0].value = &outarg;
1327 request_send(fc, req);
1328 err = req->out.h.error;
1329 fuse_put_request(fc, req);
1331 err = convert_fuse_file_lock(&outarg.lk, fl);
1336 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1338 struct inode *inode = file->f_path.dentry->d_inode;
1339 struct fuse_conn *fc = get_fuse_conn(inode);
1340 struct fuse_req *req;
1341 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1342 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1345 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1346 /* NLM needs asynchronous locks, which we don't support yet */
1350 /* Unlock on close is handled by the flush method */
1351 if (fl->fl_flags & FL_CLOSE)
1354 req = fuse_get_req(fc);
1356 return PTR_ERR(req);
1358 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1359 request_send(fc, req);
1360 err = req->out.h.error;
1361 /* locking is restartable */
1364 fuse_put_request(fc, req);
1368 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1370 struct inode *inode = file->f_path.dentry->d_inode;
1371 struct fuse_conn *fc = get_fuse_conn(inode);
1374 if (cmd == F_CANCELLK) {
1376 } else if (cmd == F_GETLK) {
1378 posix_test_lock(file, fl);
1381 err = fuse_getlk(file, fl);
1384 err = posix_lock_file(file, fl, NULL);
1386 err = fuse_setlk(file, fl, 0);
1391 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1393 struct inode *inode = file->f_path.dentry->d_inode;
1394 struct fuse_conn *fc = get_fuse_conn(inode);
1398 err = flock_lock_file_wait(file, fl);
1400 /* emulate flock with POSIX locks */
1401 fl->fl_owner = (fl_owner_t) file;
1402 err = fuse_setlk(file, fl, 1);
1408 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1410 struct inode *inode = mapping->host;
1411 struct fuse_conn *fc = get_fuse_conn(inode);
1412 struct fuse_req *req;
1413 struct fuse_bmap_in inarg;
1414 struct fuse_bmap_out outarg;
1417 if (!inode->i_sb->s_bdev || fc->no_bmap)
1420 req = fuse_get_req(fc);
1424 memset(&inarg, 0, sizeof(inarg));
1425 inarg.block = block;
1426 inarg.blocksize = inode->i_sb->s_blocksize;
1427 req->in.h.opcode = FUSE_BMAP;
1428 req->in.h.nodeid = get_node_id(inode);
1429 req->in.numargs = 1;
1430 req->in.args[0].size = sizeof(inarg);
1431 req->in.args[0].value = &inarg;
1432 req->out.numargs = 1;
1433 req->out.args[0].size = sizeof(outarg);
1434 req->out.args[0].value = &outarg;
1435 request_send(fc, req);
1436 err = req->out.h.error;
1437 fuse_put_request(fc, req);
1441 return err ? 0 : outarg.block;
1444 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1447 struct inode *inode = file->f_path.dentry->d_inode;
1449 mutex_lock(&inode->i_mutex);
1452 retval = fuse_update_attributes(inode, NULL, file, NULL);
1455 offset += i_size_read(inode);
1458 offset += file->f_pos;
1461 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1462 if (offset != file->f_pos) {
1463 file->f_pos = offset;
1464 file->f_version = 0;
1468 mutex_unlock(&inode->i_mutex);
1472 static const struct file_operations fuse_file_operations = {
1473 .llseek = fuse_file_llseek,
1474 .read = do_sync_read,
1475 .aio_read = fuse_file_aio_read,
1476 .write = do_sync_write,
1477 .aio_write = fuse_file_aio_write,
1478 .mmap = fuse_file_mmap,
1480 .flush = fuse_flush,
1481 .release = fuse_release,
1482 .fsync = fuse_fsync,
1483 .lock = fuse_file_lock,
1484 .flock = fuse_file_flock,
1485 .splice_read = generic_file_splice_read,
1488 static const struct file_operations fuse_direct_io_file_operations = {
1489 .llseek = fuse_file_llseek,
1490 .read = fuse_direct_read,
1491 .write = fuse_direct_write,
1493 .flush = fuse_flush,
1494 .release = fuse_release,
1495 .fsync = fuse_fsync,
1496 .lock = fuse_file_lock,
1497 .flock = fuse_file_flock,
1498 /* no mmap and splice_read */
1501 static const struct address_space_operations fuse_file_aops = {
1502 .readpage = fuse_readpage,
1503 .writepage = fuse_writepage,
1504 .launder_page = fuse_launder_page,
1505 .write_begin = fuse_write_begin,
1506 .write_end = fuse_write_end,
1507 .readpages = fuse_readpages,
1508 .set_page_dirty = __set_page_dirty_nobuffers,
1512 void fuse_init_file_inode(struct inode *inode)
1514 inode->i_fop = &fuse_file_operations;
1515 inode->i_data.a_ops = &fuse_file_aops;