2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 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>
15 static struct file_operations fuse_direct_io_file_operations;
17 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
18 struct fuse_open_out *outargp)
20 struct fuse_conn *fc = get_fuse_conn(inode);
21 struct fuse_open_in inarg;
25 req = fuse_get_request(fc);
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
31 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
32 req->in.h.nodeid = get_node_id(inode);
35 req->in.args[0].size = sizeof(inarg);
36 req->in.args[0].value = &inarg;
38 req->out.args[0].size = sizeof(*outargp);
39 req->out.args[0].value = outargp;
40 request_send(fc, req);
41 err = req->out.h.error;
42 fuse_put_request(fc, req);
47 struct fuse_file *fuse_file_alloc(void)
50 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
52 ff->release_req = fuse_request_alloc();
53 if (!ff->release_req) {
61 void fuse_file_free(struct fuse_file *ff)
63 fuse_request_free(ff->release_req);
67 void fuse_finish_open(struct inode *inode, struct file *file,
68 struct fuse_file *ff, struct fuse_open_out *outarg)
70 if (outarg->open_flags & FOPEN_DIRECT_IO)
71 file->f_op = &fuse_direct_io_file_operations;
72 if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
73 invalidate_inode_pages(inode->i_mapping);
75 file->private_data = ff;
78 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
80 struct fuse_open_out outarg;
84 /* VFS checks this, but only _after_ ->open() */
85 if (file->f_flags & O_DIRECT)
88 err = generic_file_open(inode, file);
92 /* If opening the root node, no lookup has been performed on
93 it, so the attributes must be refreshed */
94 if (get_node_id(inode) == FUSE_ROOT_ID) {
95 err = fuse_do_getattr(inode);
100 ff = fuse_file_alloc();
104 err = fuse_send_open(inode, file, isdir, &outarg);
109 outarg.open_flags &= ~FOPEN_DIRECT_IO;
110 fuse_finish_open(inode, file, ff, &outarg);
116 /* Special case for failed iget in CREATE */
117 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
119 u64 nodeid = req->in.h.nodeid;
120 fuse_reset_request(req);
121 fuse_send_forget(fc, req, nodeid, 1);
124 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
125 u64 nodeid, struct inode *inode, int flags, int isdir)
127 struct fuse_req * req = ff->release_req;
128 struct fuse_release_in *inarg = &req->misc.release_in;
131 inarg->flags = flags;
132 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
133 req->in.h.nodeid = nodeid;
136 req->in.args[0].size = sizeof(struct fuse_release_in);
137 req->in.args[0].value = inarg;
138 request_send_background(fc, req);
140 req->end = fuse_release_end;
144 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
146 struct fuse_file *ff = file->private_data;
148 struct fuse_conn *fc = get_fuse_conn(inode);
149 u64 nodeid = get_node_id(inode);
150 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
153 /* Return value is ignored by VFS */
157 static int fuse_open(struct inode *inode, struct file *file)
159 return fuse_open_common(inode, file, 0);
162 static int fuse_release(struct inode *inode, struct file *file)
164 return fuse_release_common(inode, file, 0);
167 static int fuse_flush(struct file *file)
169 struct inode *inode = file->f_dentry->d_inode;
170 struct fuse_conn *fc = get_fuse_conn(inode);
171 struct fuse_file *ff = file->private_data;
172 struct fuse_req *req;
173 struct fuse_flush_in inarg;
176 if (is_bad_inode(inode))
182 req = fuse_get_request(fc);
186 memset(&inarg, 0, sizeof(inarg));
188 req->in.h.opcode = FUSE_FLUSH;
189 req->in.h.nodeid = get_node_id(inode);
193 req->in.args[0].size = sizeof(inarg);
194 req->in.args[0].value = &inarg;
195 request_send(fc, req);
196 err = req->out.h.error;
197 fuse_put_request(fc, req);
198 if (err == -ENOSYS) {
205 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
208 struct inode *inode = de->d_inode;
209 struct fuse_conn *fc = get_fuse_conn(inode);
210 struct fuse_file *ff = file->private_data;
211 struct fuse_req *req;
212 struct fuse_fsync_in inarg;
215 if (is_bad_inode(inode))
218 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
221 req = fuse_get_request(fc);
225 memset(&inarg, 0, sizeof(inarg));
227 inarg.fsync_flags = datasync ? 1 : 0;
228 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
229 req->in.h.nodeid = get_node_id(inode);
233 req->in.args[0].size = sizeof(inarg);
234 req->in.args[0].value = &inarg;
235 request_send(fc, req);
236 err = req->out.h.error;
237 fuse_put_request(fc, req);
238 if (err == -ENOSYS) {
248 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
250 return fuse_fsync_common(file, de, datasync, 0);
253 void fuse_read_fill(struct fuse_req *req, struct file *file,
254 struct inode *inode, loff_t pos, size_t count, int opcode)
256 struct fuse_file *ff = file->private_data;
257 struct fuse_read_in *inarg = &req->misc.read_in;
262 req->in.h.opcode = opcode;
263 req->in.h.nodeid = get_node_id(inode);
267 req->in.args[0].size = sizeof(struct fuse_read_in);
268 req->in.args[0].value = inarg;
269 req->out.argpages = 1;
271 req->out.numargs = 1;
272 req->out.args[0].size = count;
275 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
276 struct inode *inode, loff_t pos, size_t count)
278 struct fuse_conn *fc = get_fuse_conn(inode);
279 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
280 request_send(fc, req);
281 return req->out.args[0].size;
284 static int fuse_readpage(struct file *file, struct page *page)
286 struct inode *inode = page->mapping->host;
287 struct fuse_conn *fc = get_fuse_conn(inode);
288 struct fuse_req *req;
292 if (is_bad_inode(inode))
296 req = fuse_get_request(fc);
300 req->out.page_zeroing = 1;
302 req->pages[0] = page;
303 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
304 err = req->out.h.error;
305 fuse_put_request(fc, req);
307 SetPageUptodate(page);
308 fuse_invalidate_attr(inode); /* atime changed */
314 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
318 fuse_invalidate_attr(req->pages[0]->mapping->host); /* atime changed */
320 for (i = 0; i < req->num_pages; i++) {
321 struct page *page = req->pages[i];
322 if (!req->out.h.error)
323 SetPageUptodate(page);
328 fuse_put_request(fc, req);
331 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
334 struct fuse_conn *fc = get_fuse_conn(inode);
335 loff_t pos = page_offset(req->pages[0]);
336 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
337 req->out.page_zeroing = 1;
338 fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
339 if (fc->async_read) {
340 req->end = fuse_readpages_end;
341 request_send_background(fc, req);
343 request_send(fc, req);
344 fuse_readpages_end(fc, req);
348 struct fuse_readpages_data {
349 struct fuse_req *req;
354 static int fuse_readpages_fill(void *_data, struct page *page)
356 struct fuse_readpages_data *data = _data;
357 struct fuse_req *req = data->req;
358 struct inode *inode = data->inode;
359 struct fuse_conn *fc = get_fuse_conn(inode);
361 if (req->num_pages &&
362 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
363 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
364 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
365 fuse_send_readpages(req, data->file, inode);
366 data->req = req = fuse_get_request(fc);
372 req->pages[req->num_pages] = page;
377 static int fuse_readpages(struct file *file, struct address_space *mapping,
378 struct list_head *pages, unsigned nr_pages)
380 struct inode *inode = mapping->host;
381 struct fuse_conn *fc = get_fuse_conn(inode);
382 struct fuse_readpages_data data;
385 if (is_bad_inode(inode))
390 data.req = fuse_get_request(fc);
394 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
396 fuse_send_readpages(data.req, file, inode);
400 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
401 struct inode *inode, loff_t pos, size_t count)
403 struct fuse_conn *fc = get_fuse_conn(inode);
404 struct fuse_file *ff = file->private_data;
405 struct fuse_write_in inarg;
406 struct fuse_write_out outarg;
408 memset(&inarg, 0, sizeof(struct fuse_write_in));
412 req->in.h.opcode = FUSE_WRITE;
413 req->in.h.nodeid = get_node_id(inode);
416 req->in.argpages = 1;
418 req->in.args[0].size = sizeof(struct fuse_write_in);
419 req->in.args[0].value = &inarg;
420 req->in.args[1].size = count;
421 req->out.numargs = 1;
422 req->out.args[0].size = sizeof(struct fuse_write_out);
423 req->out.args[0].value = &outarg;
424 request_send(fc, req);
428 static int fuse_prepare_write(struct file *file, struct page *page,
429 unsigned offset, unsigned to)
435 static int fuse_commit_write(struct file *file, struct page *page,
436 unsigned offset, unsigned to)
440 unsigned count = to - offset;
441 struct inode *inode = page->mapping->host;
442 struct fuse_conn *fc = get_fuse_conn(inode);
443 loff_t pos = page_offset(page) + offset;
444 struct fuse_req *req;
446 if (is_bad_inode(inode))
449 req = fuse_get_request(fc);
454 req->pages[0] = page;
455 req->page_offset = offset;
456 nres = fuse_send_write(req, file, inode, pos, count);
457 err = req->out.h.error;
458 fuse_put_request(fc, req);
459 if (!err && nres != count)
463 if (pos > i_size_read(inode))
464 i_size_write(inode, pos);
466 if (offset == 0 && to == PAGE_CACHE_SIZE) {
467 clear_page_dirty(page);
468 SetPageUptodate(page);
471 fuse_invalidate_attr(inode);
475 static void fuse_release_user_pages(struct fuse_req *req, int write)
479 for (i = 0; i < req->num_pages; i++) {
480 struct page *page = req->pages[i];
482 set_page_dirty_lock(page);
487 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
488 unsigned nbytes, int write)
490 unsigned long user_addr = (unsigned long) buf;
491 unsigned offset = user_addr & ~PAGE_MASK;
494 /* This doesn't work with nfsd */
498 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
499 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
500 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
501 down_read(¤t->mm->mmap_sem);
502 npages = get_user_pages(current, current->mm, user_addr, npages, write,
503 0, req->pages, NULL);
504 up_read(¤t->mm->mmap_sem);
508 req->num_pages = npages;
509 req->page_offset = offset;
513 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
514 size_t count, loff_t *ppos, int write)
516 struct inode *inode = file->f_dentry->d_inode;
517 struct fuse_conn *fc = get_fuse_conn(inode);
518 size_t nmax = write ? fc->max_write : fc->max_read;
521 struct fuse_req *req;
523 if (is_bad_inode(inode))
526 req = fuse_get_request(fc);
532 size_t nbytes = min(count, nmax);
533 int err = fuse_get_user_pages(req, buf, nbytes, !write);
538 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
539 nbytes = min(count, nbytes);
541 nres = fuse_send_write(req, file, inode, pos, nbytes);
543 nres = fuse_send_read(req, file, inode, pos, nbytes);
544 fuse_release_user_pages(req, !write);
545 if (req->out.h.error) {
547 res = req->out.h.error;
549 } else if (nres > nbytes) {
560 fuse_reset_request(req);
562 fuse_put_request(fc, req);
564 if (write && pos > i_size_read(inode))
565 i_size_write(inode, pos);
568 fuse_invalidate_attr(inode);
573 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
574 size_t count, loff_t *ppos)
576 return fuse_direct_io(file, buf, count, ppos, 0);
579 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
580 size_t count, loff_t *ppos)
582 struct inode *inode = file->f_dentry->d_inode;
584 /* Don't allow parallel writes to the same file */
585 mutex_lock(&inode->i_mutex);
586 res = fuse_direct_io(file, buf, count, ppos, 1);
587 mutex_unlock(&inode->i_mutex);
591 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
593 if ((vma->vm_flags & VM_SHARED)) {
594 if ((vma->vm_flags & VM_WRITE))
597 vma->vm_flags &= ~VM_MAYWRITE;
599 return generic_file_mmap(file, vma);
602 static int fuse_set_page_dirty(struct page *page)
604 printk("fuse_set_page_dirty: should not happen\n");
609 static struct file_operations fuse_file_operations = {
610 .llseek = generic_file_llseek,
611 .read = generic_file_read,
612 .write = generic_file_write,
613 .mmap = fuse_file_mmap,
616 .release = fuse_release,
618 .sendfile = generic_file_sendfile,
621 static struct file_operations fuse_direct_io_file_operations = {
622 .llseek = generic_file_llseek,
623 .read = fuse_direct_read,
624 .write = fuse_direct_write,
627 .release = fuse_release,
629 /* no mmap and sendfile */
632 static struct address_space_operations fuse_file_aops = {
633 .readpage = fuse_readpage,
634 .prepare_write = fuse_prepare_write,
635 .commit_write = fuse_commit_write,
636 .readpages = fuse_readpages,
637 .set_page_dirty = fuse_set_page_dirty,
640 void fuse_init_file_inode(struct inode *inode)
642 inode->i_fop = &fuse_file_operations;
643 inode->i_data.a_ops = &fuse_file_aops;