2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 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);
82 fuse_put_request(fc, req);
85 static void fuse_file_put(struct fuse_file *ff)
87 if (atomic_dec_and_test(&ff->count)) {
88 struct fuse_req *req = ff->reserved_req;
89 struct inode *inode = req->misc.release.dentry->d_inode;
90 struct fuse_conn *fc = get_fuse_conn(inode);
91 req->end = fuse_release_end;
92 request_send_background(fc, req);
97 void fuse_finish_open(struct inode *inode, struct file *file,
98 struct fuse_file *ff, struct fuse_open_out *outarg)
100 if (outarg->open_flags & FOPEN_DIRECT_IO)
101 file->f_op = &fuse_direct_io_file_operations;
102 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
103 invalidate_inode_pages2(inode->i_mapping);
104 if (outarg->open_flags & FOPEN_NONSEEKABLE)
105 nonseekable_open(inode, file);
107 file->private_data = fuse_file_get(ff);
110 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
112 struct fuse_open_out outarg;
113 struct fuse_file *ff;
116 /* VFS checks this, but only _after_ ->open() */
117 if (file->f_flags & O_DIRECT)
120 err = generic_file_open(inode, file);
124 ff = fuse_file_alloc();
128 err = fuse_send_open(inode, file, isdir, &outarg);
133 outarg.open_flags &= ~FOPEN_DIRECT_IO;
134 fuse_finish_open(inode, file, ff, &outarg);
140 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
142 struct fuse_req *req = ff->reserved_req;
143 struct fuse_release_in *inarg = &req->misc.release.in;
146 inarg->flags = flags;
147 req->in.h.opcode = opcode;
148 req->in.h.nodeid = nodeid;
150 req->in.args[0].size = sizeof(struct fuse_release_in);
151 req->in.args[0].value = inarg;
154 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
156 struct fuse_file *ff = file->private_data;
158 struct fuse_conn *fc = get_fuse_conn(inode);
159 struct fuse_req *req = ff->reserved_req;
161 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
162 isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
164 /* Hold vfsmount and dentry until release is finished */
165 req->misc.release.vfsmount = mntget(file->f_path.mnt);
166 req->misc.release.dentry = dget(file->f_path.dentry);
168 spin_lock(&fc->lock);
169 list_del(&ff->write_entry);
170 spin_unlock(&fc->lock);
172 * Normally this will send the RELEASE request,
173 * however if some asynchronous READ or WRITE requests
174 * are outstanding, the sending will be delayed
179 /* Return value is ignored by VFS */
183 static int fuse_open(struct inode *inode, struct file *file)
185 return fuse_open_common(inode, file, 0);
188 static int fuse_release(struct inode *inode, struct file *file)
190 return fuse_release_common(inode, file, 0);
194 * Scramble the ID space with XTEA, so that the value of the files_struct
195 * pointer is not exposed to userspace.
197 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
199 u32 *k = fc->scramble_key;
200 u64 v = (unsigned long) id;
206 for (i = 0; i < 32; i++) {
207 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
209 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
212 return (u64) v0 + ((u64) v1 << 32);
216 * Check if page is under writeback
218 * This is currently done by walking the list of writepage requests
219 * for the inode, which can be pretty inefficient.
221 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
223 struct fuse_conn *fc = get_fuse_conn(inode);
224 struct fuse_inode *fi = get_fuse_inode(inode);
225 struct fuse_req *req;
228 spin_lock(&fc->lock);
229 list_for_each_entry(req, &fi->writepages, writepages_entry) {
232 BUG_ON(req->inode != inode);
233 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
234 if (curr_index == index) {
239 spin_unlock(&fc->lock);
245 * Wait for page writeback to be completed.
247 * Since fuse doesn't rely on the VM writeback tracking, this has to
248 * use some other means.
250 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
252 struct fuse_inode *fi = get_fuse_inode(inode);
254 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
258 static int fuse_flush(struct file *file, fl_owner_t id)
260 struct inode *inode = file->f_path.dentry->d_inode;
261 struct fuse_conn *fc = get_fuse_conn(inode);
262 struct fuse_file *ff = file->private_data;
263 struct fuse_req *req;
264 struct fuse_flush_in inarg;
267 if (is_bad_inode(inode))
273 req = fuse_get_req_nofail(fc, file);
274 memset(&inarg, 0, sizeof(inarg));
276 inarg.lock_owner = fuse_lock_owner_id(fc, id);
277 req->in.h.opcode = FUSE_FLUSH;
278 req->in.h.nodeid = get_node_id(inode);
280 req->in.args[0].size = sizeof(inarg);
281 req->in.args[0].value = &inarg;
283 request_send(fc, req);
284 err = req->out.h.error;
285 fuse_put_request(fc, req);
286 if (err == -ENOSYS) {
294 * Wait for all pending writepages on the inode to finish.
296 * This is currently done by blocking further writes with FUSE_NOWRITE
297 * and waiting for all sent writes to complete.
299 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
300 * could conflict with truncation.
302 static void fuse_sync_writes(struct inode *inode)
304 fuse_set_nowrite(inode);
305 fuse_release_nowrite(inode);
308 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
311 struct inode *inode = de->d_inode;
312 struct fuse_conn *fc = get_fuse_conn(inode);
313 struct fuse_file *ff = file->private_data;
314 struct fuse_req *req;
315 struct fuse_fsync_in inarg;
318 if (is_bad_inode(inode))
321 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
325 * Start writeback against all dirty pages of the inode, then
326 * wait for all outstanding writes, before sending the FSYNC
329 err = write_inode_now(inode, 0);
333 fuse_sync_writes(inode);
335 req = fuse_get_req(fc);
339 memset(&inarg, 0, sizeof(inarg));
341 inarg.fsync_flags = datasync ? 1 : 0;
342 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
343 req->in.h.nodeid = get_node_id(inode);
345 req->in.args[0].size = sizeof(inarg);
346 req->in.args[0].value = &inarg;
347 request_send(fc, req);
348 err = req->out.h.error;
349 fuse_put_request(fc, req);
350 if (err == -ENOSYS) {
360 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
362 return fuse_fsync_common(file, de, datasync, 0);
365 void fuse_read_fill(struct fuse_req *req, struct file *file,
366 struct inode *inode, loff_t pos, size_t count, int opcode)
368 struct fuse_read_in *inarg = &req->misc.read.in;
369 struct fuse_file *ff = file->private_data;
374 inarg->flags = file->f_flags;
375 req->in.h.opcode = opcode;
376 req->in.h.nodeid = get_node_id(inode);
378 req->in.args[0].size = sizeof(struct fuse_read_in);
379 req->in.args[0].value = inarg;
380 req->out.argpages = 1;
382 req->out.numargs = 1;
383 req->out.args[0].size = count;
386 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
387 struct inode *inode, loff_t pos, size_t count,
390 struct fuse_conn *fc = get_fuse_conn(inode);
392 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
394 struct fuse_read_in *inarg = &req->misc.read.in;
396 inarg->read_flags |= FUSE_READ_LOCKOWNER;
397 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
399 request_send(fc, req);
400 return req->out.args[0].size;
403 static void fuse_read_update_size(struct inode *inode, loff_t size,
406 struct fuse_conn *fc = get_fuse_conn(inode);
407 struct fuse_inode *fi = get_fuse_inode(inode);
409 spin_lock(&fc->lock);
410 if (attr_ver == fi->attr_version && size < inode->i_size) {
411 fi->attr_version = ++fc->attr_version;
412 i_size_write(inode, size);
414 spin_unlock(&fc->lock);
417 static int fuse_readpage(struct file *file, struct page *page)
419 struct inode *inode = page->mapping->host;
420 struct fuse_conn *fc = get_fuse_conn(inode);
421 struct fuse_req *req;
423 loff_t pos = page_offset(page);
424 size_t count = PAGE_CACHE_SIZE;
429 if (is_bad_inode(inode))
433 * Page writeback can extend beyond the liftime of the
434 * page-cache page, so make sure we read a properly synced
437 fuse_wait_on_page_writeback(inode, page->index);
439 req = fuse_get_req(fc);
444 attr_ver = fuse_get_attr_version(fc);
446 req->out.page_zeroing = 1;
448 req->pages[0] = page;
449 num_read = fuse_send_read(req, file, inode, pos, count, NULL);
450 err = req->out.h.error;
451 fuse_put_request(fc, req);
455 * Short read means EOF. If file size is larger, truncate it
457 if (num_read < count)
458 fuse_read_update_size(inode, pos + num_read, attr_ver);
460 SetPageUptodate(page);
463 fuse_invalidate_attr(inode); /* atime changed */
469 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
472 size_t count = req->misc.read.in.size;
473 size_t num_read = req->out.args[0].size;
474 struct inode *inode = req->pages[0]->mapping->host;
477 * Short read means EOF. If file size is larger, truncate it
479 if (!req->out.h.error && num_read < count) {
480 loff_t pos = page_offset(req->pages[0]) + num_read;
481 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
484 fuse_invalidate_attr(inode); /* atime changed */
486 for (i = 0; i < req->num_pages; i++) {
487 struct page *page = req->pages[i];
488 if (!req->out.h.error)
489 SetPageUptodate(page);
495 fuse_file_put(req->ff);
496 fuse_put_request(fc, req);
499 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
502 struct fuse_conn *fc = get_fuse_conn(inode);
503 loff_t pos = page_offset(req->pages[0]);
504 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
505 req->out.page_zeroing = 1;
506 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
507 req->misc.read.attr_ver = fuse_get_attr_version(fc);
508 if (fc->async_read) {
509 struct fuse_file *ff = file->private_data;
510 req->ff = fuse_file_get(ff);
511 req->end = fuse_readpages_end;
512 request_send_background(fc, req);
514 request_send(fc, req);
515 fuse_readpages_end(fc, req);
519 struct fuse_fill_data {
520 struct fuse_req *req;
525 static int fuse_readpages_fill(void *_data, struct page *page)
527 struct fuse_fill_data *data = _data;
528 struct fuse_req *req = data->req;
529 struct inode *inode = data->inode;
530 struct fuse_conn *fc = get_fuse_conn(inode);
532 fuse_wait_on_page_writeback(inode, page->index);
534 if (req->num_pages &&
535 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
536 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
537 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
538 fuse_send_readpages(req, data->file, inode);
539 data->req = req = fuse_get_req(fc);
545 req->pages[req->num_pages] = page;
550 static int fuse_readpages(struct file *file, struct address_space *mapping,
551 struct list_head *pages, unsigned nr_pages)
553 struct inode *inode = mapping->host;
554 struct fuse_conn *fc = get_fuse_conn(inode);
555 struct fuse_fill_data data;
559 if (is_bad_inode(inode))
564 data.req = fuse_get_req(fc);
565 err = PTR_ERR(data.req);
566 if (IS_ERR(data.req))
569 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
571 if (data.req->num_pages)
572 fuse_send_readpages(data.req, file, inode);
574 fuse_put_request(fc, data.req);
580 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
581 unsigned long nr_segs, loff_t pos)
583 struct inode *inode = iocb->ki_filp->f_mapping->host;
585 if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
588 * If trying to read past EOF, make sure the i_size
589 * attribute is up-to-date.
591 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
596 return generic_file_aio_read(iocb, iov, nr_segs, pos);
599 static void fuse_write_fill(struct fuse_req *req, struct file *file,
600 struct fuse_file *ff, struct inode *inode,
601 loff_t pos, size_t count, int writepage)
603 struct fuse_conn *fc = get_fuse_conn(inode);
604 struct fuse_write_in *inarg = &req->misc.write.in;
605 struct fuse_write_out *outarg = &req->misc.write.out;
607 memset(inarg, 0, sizeof(struct fuse_write_in));
611 inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
612 inarg->flags = file ? file->f_flags : 0;
613 req->in.h.opcode = FUSE_WRITE;
614 req->in.h.nodeid = get_node_id(inode);
615 req->in.argpages = 1;
618 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
620 req->in.args[0].size = sizeof(struct fuse_write_in);
621 req->in.args[0].value = inarg;
622 req->in.args[1].size = count;
623 req->out.numargs = 1;
624 req->out.args[0].size = sizeof(struct fuse_write_out);
625 req->out.args[0].value = outarg;
628 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
629 struct inode *inode, loff_t pos, size_t count,
632 struct fuse_conn *fc = get_fuse_conn(inode);
633 fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
635 struct fuse_write_in *inarg = &req->misc.write.in;
636 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
637 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
639 request_send(fc, req);
640 return req->misc.write.out.size;
643 static int fuse_write_begin(struct file *file, struct address_space *mapping,
644 loff_t pos, unsigned len, unsigned flags,
645 struct page **pagep, void **fsdata)
647 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
649 *pagep = __grab_cache_page(mapping, index);
655 static void fuse_write_update_size(struct inode *inode, loff_t pos)
657 struct fuse_conn *fc = get_fuse_conn(inode);
658 struct fuse_inode *fi = get_fuse_inode(inode);
660 spin_lock(&fc->lock);
661 fi->attr_version = ++fc->attr_version;
662 if (pos > inode->i_size)
663 i_size_write(inode, pos);
664 spin_unlock(&fc->lock);
667 static int fuse_buffered_write(struct file *file, struct inode *inode,
668 loff_t pos, unsigned count, struct page *page)
672 struct fuse_conn *fc = get_fuse_conn(inode);
673 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
674 struct fuse_req *req;
676 if (is_bad_inode(inode))
680 * Make sure writepages on the same page are not mixed up with
683 fuse_wait_on_page_writeback(inode, page->index);
685 req = fuse_get_req(fc);
690 req->pages[0] = page;
691 req->page_offset = offset;
692 nres = fuse_send_write(req, file, inode, pos, count, NULL);
693 err = req->out.h.error;
694 fuse_put_request(fc, req);
699 fuse_write_update_size(inode, pos);
700 if (count == PAGE_CACHE_SIZE)
701 SetPageUptodate(page);
703 fuse_invalidate_attr(inode);
704 return err ? err : nres;
707 static int fuse_write_end(struct file *file, struct address_space *mapping,
708 loff_t pos, unsigned len, unsigned copied,
709 struct page *page, void *fsdata)
711 struct inode *inode = mapping->host;
715 res = fuse_buffered_write(file, inode, pos, copied, page);
718 page_cache_release(page);
722 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
723 struct inode *inode, loff_t pos,
730 for (i = 0; i < req->num_pages; i++)
731 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
733 res = fuse_send_write(req, file, inode, pos, count, NULL);
735 offset = req->page_offset;
737 for (i = 0; i < req->num_pages; i++) {
738 struct page *page = req->pages[i];
740 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
741 SetPageUptodate(page);
743 if (count > PAGE_CACHE_SIZE - offset)
744 count -= PAGE_CACHE_SIZE - offset;
750 page_cache_release(page);
756 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
757 struct address_space *mapping,
758 struct iov_iter *ii, loff_t pos)
760 struct fuse_conn *fc = get_fuse_conn(mapping->host);
761 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
765 req->page_offset = offset;
770 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
771 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
774 bytes = min_t(size_t, bytes, fc->max_write - count);
778 if (iov_iter_fault_in_readable(ii, bytes))
782 page = __grab_cache_page(mapping, index);
787 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
789 flush_dcache_page(page);
793 page_cache_release(page);
794 bytes = min(bytes, iov_iter_single_seg_count(ii));
799 req->pages[req->num_pages] = page;
802 iov_iter_advance(ii, tmp);
806 if (offset == PAGE_CACHE_SIZE)
811 } while (iov_iter_count(ii) && count < fc->max_write &&
812 req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
814 return count > 0 ? count : err;
817 static ssize_t fuse_perform_write(struct file *file,
818 struct address_space *mapping,
819 struct iov_iter *ii, loff_t pos)
821 struct inode *inode = mapping->host;
822 struct fuse_conn *fc = get_fuse_conn(inode);
826 if (is_bad_inode(inode))
830 struct fuse_req *req;
833 req = fuse_get_req(fc);
839 count = fuse_fill_write_pages(req, mapping, ii, pos);
845 num_written = fuse_send_write_pages(req, file, inode,
847 err = req->out.h.error;
852 /* break out of the loop on short write */
853 if (num_written != count)
857 fuse_put_request(fc, req);
858 } while (!err && iov_iter_count(ii));
861 fuse_write_update_size(inode, pos);
863 fuse_invalidate_attr(inode);
865 return res > 0 ? res : err;
868 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
869 unsigned long nr_segs, loff_t pos)
871 struct file *file = iocb->ki_filp;
872 struct address_space *mapping = file->f_mapping;
875 struct inode *inode = mapping->host;
879 WARN_ON(iocb->ki_pos != pos);
881 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
885 mutex_lock(&inode->i_mutex);
886 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
888 /* We can write back this queue in page reclaim */
889 current->backing_dev_info = mapping->backing_dev_info;
891 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
898 err = file_remove_suid(file);
902 file_update_time(file);
904 iov_iter_init(&i, iov, nr_segs, count, 0);
905 written = fuse_perform_write(file, mapping, &i, pos);
907 iocb->ki_pos = pos + written;
910 current->backing_dev_info = NULL;
911 mutex_unlock(&inode->i_mutex);
913 return written ? written : err;
916 static void fuse_release_user_pages(struct fuse_req *req, int write)
920 for (i = 0; i < req->num_pages; i++) {
921 struct page *page = req->pages[i];
923 set_page_dirty_lock(page);
928 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
929 unsigned nbytes, int write)
931 unsigned long user_addr = (unsigned long) buf;
932 unsigned offset = user_addr & ~PAGE_MASK;
935 /* This doesn't work with nfsd */
939 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
940 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
941 npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
942 down_read(¤t->mm->mmap_sem);
943 npages = get_user_pages(current, current->mm, user_addr, npages, write,
944 0, req->pages, NULL);
945 up_read(¤t->mm->mmap_sem);
949 req->num_pages = npages;
950 req->page_offset = offset;
954 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
955 size_t count, loff_t *ppos, int write)
957 struct inode *inode = file->f_path.dentry->d_inode;
958 struct fuse_conn *fc = get_fuse_conn(inode);
959 size_t nmax = write ? fc->max_write : fc->max_read;
962 struct fuse_req *req;
964 if (is_bad_inode(inode))
967 req = fuse_get_req(fc);
973 size_t nbytes_limit = min(count, nmax);
975 int err = fuse_get_user_pages(req, buf, nbytes_limit, !write);
980 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
981 nbytes = min(nbytes_limit, nbytes);
983 nres = fuse_send_write(req, file, inode, pos, nbytes,
986 nres = fuse_send_read(req, file, inode, pos, nbytes,
988 fuse_release_user_pages(req, !write);
989 if (req->out.h.error) {
991 res = req->out.h.error;
993 } else if (nres > nbytes) {
1004 fuse_put_request(fc, req);
1005 req = fuse_get_req(fc);
1010 fuse_put_request(fc, req);
1013 fuse_write_update_size(inode, pos);
1016 fuse_invalidate_attr(inode);
1021 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1022 size_t count, loff_t *ppos)
1024 return fuse_direct_io(file, buf, count, ppos, 0);
1027 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1028 size_t count, loff_t *ppos)
1030 struct inode *inode = file->f_path.dentry->d_inode;
1032 /* Don't allow parallel writes to the same file */
1033 mutex_lock(&inode->i_mutex);
1034 res = generic_write_checks(file, ppos, &count, 0);
1036 res = fuse_direct_io(file, buf, count, ppos, 1);
1037 mutex_unlock(&inode->i_mutex);
1041 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1043 __free_page(req->pages[0]);
1044 fuse_file_put(req->ff);
1045 fuse_put_request(fc, req);
1048 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1050 struct inode *inode = req->inode;
1051 struct fuse_inode *fi = get_fuse_inode(inode);
1052 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1054 list_del(&req->writepages_entry);
1055 dec_bdi_stat(bdi, BDI_WRITEBACK);
1056 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1057 bdi_writeout_inc(bdi);
1058 wake_up(&fi->page_waitq);
1061 /* Called under fc->lock, may release and reacquire it */
1062 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1064 struct fuse_inode *fi = get_fuse_inode(req->inode);
1065 loff_t size = i_size_read(req->inode);
1066 struct fuse_write_in *inarg = &req->misc.write.in;
1071 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1072 inarg->size = PAGE_CACHE_SIZE;
1073 } else if (inarg->offset < size) {
1074 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1076 /* Got truncated off completely */
1080 req->in.args[1].size = inarg->size;
1082 request_send_background_locked(fc, req);
1086 fuse_writepage_finish(fc, req);
1087 spin_unlock(&fc->lock);
1088 fuse_writepage_free(fc, req);
1089 spin_lock(&fc->lock);
1093 * If fi->writectr is positive (no truncate or fsync going on) send
1094 * all queued writepage requests.
1096 * Called with fc->lock
1098 void fuse_flush_writepages(struct inode *inode)
1100 struct fuse_conn *fc = get_fuse_conn(inode);
1101 struct fuse_inode *fi = get_fuse_inode(inode);
1102 struct fuse_req *req;
1104 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1105 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1106 list_del_init(&req->list);
1107 fuse_send_writepage(fc, req);
1111 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1113 struct inode *inode = req->inode;
1114 struct fuse_inode *fi = get_fuse_inode(inode);
1116 mapping_set_error(inode->i_mapping, req->out.h.error);
1117 spin_lock(&fc->lock);
1119 fuse_writepage_finish(fc, req);
1120 spin_unlock(&fc->lock);
1121 fuse_writepage_free(fc, req);
1124 static int fuse_writepage_locked(struct page *page)
1126 struct address_space *mapping = page->mapping;
1127 struct inode *inode = mapping->host;
1128 struct fuse_conn *fc = get_fuse_conn(inode);
1129 struct fuse_inode *fi = get_fuse_inode(inode);
1130 struct fuse_req *req;
1131 struct fuse_file *ff;
1132 struct page *tmp_page;
1134 set_page_writeback(page);
1136 req = fuse_request_alloc_nofs();
1140 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1144 spin_lock(&fc->lock);
1145 BUG_ON(list_empty(&fi->write_files));
1146 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1147 req->ff = fuse_file_get(ff);
1148 spin_unlock(&fc->lock);
1150 fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
1152 copy_highpage(tmp_page, page);
1154 req->pages[0] = tmp_page;
1155 req->page_offset = 0;
1156 req->end = fuse_writepage_end;
1159 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1160 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1161 end_page_writeback(page);
1163 spin_lock(&fc->lock);
1164 list_add(&req->writepages_entry, &fi->writepages);
1165 list_add_tail(&req->list, &fi->queued_writes);
1166 fuse_flush_writepages(inode);
1167 spin_unlock(&fc->lock);
1172 fuse_request_free(req);
1174 end_page_writeback(page);
1178 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1182 err = fuse_writepage_locked(page);
1188 static int fuse_launder_page(struct page *page)
1191 if (clear_page_dirty_for_io(page)) {
1192 struct inode *inode = page->mapping->host;
1193 err = fuse_writepage_locked(page);
1195 fuse_wait_on_page_writeback(inode, page->index);
1201 * Write back dirty pages now, because there may not be any suitable
1204 static void fuse_vma_close(struct vm_area_struct *vma)
1206 filemap_write_and_wait(vma->vm_file->f_mapping);
1210 * Wait for writeback against this page to complete before allowing it
1211 * to be marked dirty again, and hence written back again, possibly
1212 * before the previous writepage completed.
1214 * Block here, instead of in ->writepage(), so that the userspace fs
1215 * can only block processes actually operating on the filesystem.
1217 * Otherwise unprivileged userspace fs would be able to block
1222 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1224 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1227 * Don't use page->mapping as it may become NULL from a
1228 * concurrent truncate.
1230 struct inode *inode = vma->vm_file->f_mapping->host;
1232 fuse_wait_on_page_writeback(inode, page->index);
1236 static struct vm_operations_struct fuse_file_vm_ops = {
1237 .close = fuse_vma_close,
1238 .fault = filemap_fault,
1239 .page_mkwrite = fuse_page_mkwrite,
1242 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1244 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1245 struct inode *inode = file->f_dentry->d_inode;
1246 struct fuse_conn *fc = get_fuse_conn(inode);
1247 struct fuse_inode *fi = get_fuse_inode(inode);
1248 struct fuse_file *ff = file->private_data;
1250 * file may be written through mmap, so chain it onto the
1251 * inodes's write_file list
1253 spin_lock(&fc->lock);
1254 if (list_empty(&ff->write_entry))
1255 list_add(&ff->write_entry, &fi->write_files);
1256 spin_unlock(&fc->lock);
1258 file_accessed(file);
1259 vma->vm_ops = &fuse_file_vm_ops;
1263 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1264 struct file_lock *fl)
1266 switch (ffl->type) {
1272 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1273 ffl->end < ffl->start)
1276 fl->fl_start = ffl->start;
1277 fl->fl_end = ffl->end;
1278 fl->fl_pid = ffl->pid;
1284 fl->fl_type = ffl->type;
1288 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1289 const struct file_lock *fl, int opcode, pid_t pid,
1292 struct inode *inode = file->f_path.dentry->d_inode;
1293 struct fuse_conn *fc = get_fuse_conn(inode);
1294 struct fuse_file *ff = file->private_data;
1295 struct fuse_lk_in *arg = &req->misc.lk_in;
1298 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1299 arg->lk.start = fl->fl_start;
1300 arg->lk.end = fl->fl_end;
1301 arg->lk.type = fl->fl_type;
1304 arg->lk_flags |= FUSE_LK_FLOCK;
1305 req->in.h.opcode = opcode;
1306 req->in.h.nodeid = get_node_id(inode);
1307 req->in.numargs = 1;
1308 req->in.args[0].size = sizeof(*arg);
1309 req->in.args[0].value = arg;
1312 static int fuse_getlk(struct file *file, struct file_lock *fl)
1314 struct inode *inode = file->f_path.dentry->d_inode;
1315 struct fuse_conn *fc = get_fuse_conn(inode);
1316 struct fuse_req *req;
1317 struct fuse_lk_out outarg;
1320 req = fuse_get_req(fc);
1322 return PTR_ERR(req);
1324 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1325 req->out.numargs = 1;
1326 req->out.args[0].size = sizeof(outarg);
1327 req->out.args[0].value = &outarg;
1328 request_send(fc, req);
1329 err = req->out.h.error;
1330 fuse_put_request(fc, req);
1332 err = convert_fuse_file_lock(&outarg.lk, fl);
1337 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1339 struct inode *inode = file->f_path.dentry->d_inode;
1340 struct fuse_conn *fc = get_fuse_conn(inode);
1341 struct fuse_req *req;
1342 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1343 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1346 if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1347 /* NLM needs asynchronous locks, which we don't support yet */
1351 /* Unlock on close is handled by the flush method */
1352 if (fl->fl_flags & FL_CLOSE)
1355 req = fuse_get_req(fc);
1357 return PTR_ERR(req);
1359 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1360 request_send(fc, req);
1361 err = req->out.h.error;
1362 /* locking is restartable */
1365 fuse_put_request(fc, req);
1369 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1371 struct inode *inode = file->f_path.dentry->d_inode;
1372 struct fuse_conn *fc = get_fuse_conn(inode);
1375 if (cmd == F_CANCELLK) {
1377 } else if (cmd == F_GETLK) {
1379 posix_test_lock(file, fl);
1382 err = fuse_getlk(file, fl);
1385 err = posix_lock_file(file, fl, NULL);
1387 err = fuse_setlk(file, fl, 0);
1392 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1394 struct inode *inode = file->f_path.dentry->d_inode;
1395 struct fuse_conn *fc = get_fuse_conn(inode);
1399 err = flock_lock_file_wait(file, fl);
1401 /* emulate flock with POSIX locks */
1402 fl->fl_owner = (fl_owner_t) file;
1403 err = fuse_setlk(file, fl, 1);
1409 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1411 struct inode *inode = mapping->host;
1412 struct fuse_conn *fc = get_fuse_conn(inode);
1413 struct fuse_req *req;
1414 struct fuse_bmap_in inarg;
1415 struct fuse_bmap_out outarg;
1418 if (!inode->i_sb->s_bdev || fc->no_bmap)
1421 req = fuse_get_req(fc);
1425 memset(&inarg, 0, sizeof(inarg));
1426 inarg.block = block;
1427 inarg.blocksize = inode->i_sb->s_blocksize;
1428 req->in.h.opcode = FUSE_BMAP;
1429 req->in.h.nodeid = get_node_id(inode);
1430 req->in.numargs = 1;
1431 req->in.args[0].size = sizeof(inarg);
1432 req->in.args[0].value = &inarg;
1433 req->out.numargs = 1;
1434 req->out.args[0].size = sizeof(outarg);
1435 req->out.args[0].value = &outarg;
1436 request_send(fc, req);
1437 err = req->out.h.error;
1438 fuse_put_request(fc, req);
1442 return err ? 0 : outarg.block;
1445 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1448 struct inode *inode = file->f_path.dentry->d_inode;
1450 mutex_lock(&inode->i_mutex);
1453 retval = fuse_update_attributes(inode, NULL, file, NULL);
1456 offset += i_size_read(inode);
1459 offset += file->f_pos;
1462 if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1463 if (offset != file->f_pos) {
1464 file->f_pos = offset;
1465 file->f_version = 0;
1469 mutex_unlock(&inode->i_mutex);
1473 static const struct file_operations fuse_file_operations = {
1474 .llseek = fuse_file_llseek,
1475 .read = do_sync_read,
1476 .aio_read = fuse_file_aio_read,
1477 .write = do_sync_write,
1478 .aio_write = fuse_file_aio_write,
1479 .mmap = fuse_file_mmap,
1481 .flush = fuse_flush,
1482 .release = fuse_release,
1483 .fsync = fuse_fsync,
1484 .lock = fuse_file_lock,
1485 .flock = fuse_file_flock,
1486 .splice_read = generic_file_splice_read,
1489 static const struct file_operations fuse_direct_io_file_operations = {
1490 .llseek = fuse_file_llseek,
1491 .read = fuse_direct_read,
1492 .write = fuse_direct_write,
1494 .flush = fuse_flush,
1495 .release = fuse_release,
1496 .fsync = fuse_fsync,
1497 .lock = fuse_file_lock,
1498 .flock = fuse_file_flock,
1499 /* no mmap and splice_read */
1502 static const struct address_space_operations fuse_file_aops = {
1503 .readpage = fuse_readpage,
1504 .writepage = fuse_writepage,
1505 .launder_page = fuse_launder_page,
1506 .write_begin = fuse_write_begin,
1507 .write_end = fuse_write_end,
1508 .readpages = fuse_readpages,
1509 .set_page_dirty = __set_page_dirty_nobuffers,
1513 void fuse_init_file_inode(struct inode *inode)
1515 inode->i_fop = &fuse_file_operations;
1516 inode->i_data.a_ops = &fuse_file_aops;