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 void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
117 u64 nodeid, struct inode *inode, int flags, int isdir)
119 struct fuse_req * req = ff->release_req;
120 struct fuse_release_in *inarg = &req->misc.release_in;
123 inarg->flags = flags;
124 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
125 req->in.h.nodeid = nodeid;
128 req->in.args[0].size = sizeof(struct fuse_release_in);
129 req->in.args[0].value = inarg;
130 request_send_background(fc, req);
134 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
136 struct fuse_file *ff = file->private_data;
138 struct fuse_conn *fc = get_fuse_conn(inode);
139 u64 nodeid = get_node_id(inode);
140 fuse_send_release(fc, ff, nodeid, inode, file->f_flags, isdir);
143 /* Return value is ignored by VFS */
147 static int fuse_open(struct inode *inode, struct file *file)
149 return fuse_open_common(inode, file, 0);
152 static int fuse_release(struct inode *inode, struct file *file)
154 return fuse_release_common(inode, file, 0);
157 static int fuse_flush(struct file *file)
159 struct inode *inode = file->f_dentry->d_inode;
160 struct fuse_conn *fc = get_fuse_conn(inode);
161 struct fuse_file *ff = file->private_data;
162 struct fuse_req *req;
163 struct fuse_flush_in inarg;
169 req = fuse_get_request(fc);
173 memset(&inarg, 0, sizeof(inarg));
175 req->in.h.opcode = FUSE_FLUSH;
176 req->in.h.nodeid = get_node_id(inode);
180 req->in.args[0].size = sizeof(inarg);
181 req->in.args[0].value = &inarg;
182 request_send(fc, req);
183 err = req->out.h.error;
184 fuse_put_request(fc, req);
185 if (err == -ENOSYS) {
192 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
195 struct inode *inode = de->d_inode;
196 struct fuse_conn *fc = get_fuse_conn(inode);
197 struct fuse_file *ff = file->private_data;
198 struct fuse_req *req;
199 struct fuse_fsync_in inarg;
202 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
205 req = fuse_get_request(fc);
209 memset(&inarg, 0, sizeof(inarg));
211 inarg.fsync_flags = datasync ? 1 : 0;
212 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
213 req->in.h.nodeid = get_node_id(inode);
217 req->in.args[0].size = sizeof(inarg);
218 req->in.args[0].value = &inarg;
219 request_send(fc, req);
220 err = req->out.h.error;
221 fuse_put_request(fc, req);
222 if (err == -ENOSYS) {
232 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
234 return fuse_fsync_common(file, de, datasync, 0);
237 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
238 struct inode *inode, loff_t pos, size_t count,
241 struct fuse_conn *fc = get_fuse_conn(inode);
242 struct fuse_file *ff = file->private_data;
243 struct fuse_read_in inarg;
245 memset(&inarg, 0, sizeof(struct fuse_read_in));
249 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
250 req->in.h.nodeid = get_node_id(inode);
254 req->in.args[0].size = sizeof(struct fuse_read_in);
255 req->in.args[0].value = &inarg;
256 req->out.argpages = 1;
258 req->out.numargs = 1;
259 req->out.args[0].size = count;
260 request_send(fc, req);
261 return req->out.args[0].size;
264 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
265 struct inode *inode, loff_t pos,
268 return fuse_send_read_common(req, file, inode, pos, count, 0);
271 static int fuse_readpage(struct file *file, struct page *page)
273 struct inode *inode = page->mapping->host;
274 struct fuse_conn *fc = get_fuse_conn(inode);
275 loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
276 struct fuse_req *req = fuse_get_request(fc);
281 req->out.page_zeroing = 1;
283 req->pages[0] = page;
284 fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
285 err = req->out.h.error;
286 fuse_put_request(fc, req);
288 SetPageUptodate(page);
289 fuse_invalidate_attr(inode); /* atime changed */
295 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
298 loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT;
299 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
301 req->out.page_zeroing = 1;
302 fuse_send_read(req, file, inode, pos, count);
303 for (i = 0; i < req->num_pages; i++) {
304 struct page *page = req->pages[i];
305 if (!req->out.h.error)
306 SetPageUptodate(page);
309 return req->out.h.error;
312 struct fuse_readpages_data {
313 struct fuse_req *req;
318 static int fuse_readpages_fill(void *_data, struct page *page)
320 struct fuse_readpages_data *data = _data;
321 struct fuse_req *req = data->req;
322 struct inode *inode = data->inode;
323 struct fuse_conn *fc = get_fuse_conn(inode);
325 if (req->num_pages &&
326 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
327 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
328 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
329 int err = fuse_send_readpages(req, data->file, inode);
334 fuse_reset_request(req);
336 req->pages[req->num_pages] = page;
341 static int fuse_readpages(struct file *file, struct address_space *mapping,
342 struct list_head *pages, unsigned nr_pages)
344 struct inode *inode = mapping->host;
345 struct fuse_conn *fc = get_fuse_conn(inode);
346 struct fuse_readpages_data data;
350 data.req = fuse_get_request(fc);
354 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
355 if (!err && data.req->num_pages)
356 err = fuse_send_readpages(data.req, file, inode);
357 fuse_put_request(fc, data.req);
358 fuse_invalidate_attr(inode); /* atime changed */
362 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
363 struct inode *inode, loff_t pos, size_t count)
365 struct fuse_conn *fc = get_fuse_conn(inode);
366 struct fuse_file *ff = file->private_data;
367 struct fuse_write_in inarg;
368 struct fuse_write_out outarg;
370 memset(&inarg, 0, sizeof(struct fuse_write_in));
374 req->in.h.opcode = FUSE_WRITE;
375 req->in.h.nodeid = get_node_id(inode);
378 req->in.argpages = 1;
380 req->in.args[0].size = sizeof(struct fuse_write_in);
381 req->in.args[0].value = &inarg;
382 req->in.args[1].size = count;
383 req->out.numargs = 1;
384 req->out.args[0].size = sizeof(struct fuse_write_out);
385 req->out.args[0].value = &outarg;
386 request_send(fc, req);
390 static int fuse_prepare_write(struct file *file, struct page *page,
391 unsigned offset, unsigned to)
397 static int fuse_commit_write(struct file *file, struct page *page,
398 unsigned offset, unsigned to)
402 unsigned count = to - offset;
403 struct inode *inode = page->mapping->host;
404 struct fuse_conn *fc = get_fuse_conn(inode);
405 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
406 struct fuse_req *req = fuse_get_request(fc);
411 req->pages[0] = page;
412 req->page_offset = offset;
413 nres = fuse_send_write(req, file, inode, pos, count);
414 err = req->out.h.error;
415 fuse_put_request(fc, req);
416 if (!err && nres != count)
420 if (pos > i_size_read(inode))
421 i_size_write(inode, pos);
423 if (offset == 0 && to == PAGE_CACHE_SIZE) {
424 clear_page_dirty(page);
425 SetPageUptodate(page);
428 fuse_invalidate_attr(inode);
432 static void fuse_release_user_pages(struct fuse_req *req, int write)
436 for (i = 0; i < req->num_pages; i++) {
437 struct page *page = req->pages[i];
439 set_page_dirty_lock(page);
444 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
445 unsigned nbytes, int write)
447 unsigned long user_addr = (unsigned long) buf;
448 unsigned offset = user_addr & ~PAGE_MASK;
451 /* This doesn't work with nfsd */
455 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
456 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
457 npages = min(npages, FUSE_MAX_PAGES_PER_REQ);
458 down_read(¤t->mm->mmap_sem);
459 npages = get_user_pages(current, current->mm, user_addr, npages, write,
460 0, req->pages, NULL);
461 up_read(¤t->mm->mmap_sem);
465 req->num_pages = npages;
466 req->page_offset = offset;
470 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
471 size_t count, loff_t *ppos, int write)
473 struct inode *inode = file->f_dentry->d_inode;
474 struct fuse_conn *fc = get_fuse_conn(inode);
475 size_t nmax = write ? fc->max_write : fc->max_read;
478 struct fuse_req *req = fuse_get_request(fc);
485 size_t nbytes = min(count, nmax);
486 int err = fuse_get_user_pages(req, buf, nbytes, !write);
491 tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset;
492 nbytes = min(nbytes, tmp);
494 nres = fuse_send_write(req, file, inode, pos, nbytes);
496 nres = fuse_send_read(req, file, inode, pos, nbytes);
497 fuse_release_user_pages(req, !write);
498 if (req->out.h.error) {
500 res = req->out.h.error;
502 } else if (nres > nbytes) {
513 fuse_reset_request(req);
515 fuse_put_request(fc, req);
517 if (write && pos > i_size_read(inode))
518 i_size_write(inode, pos);
521 fuse_invalidate_attr(inode);
526 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
527 size_t count, loff_t *ppos)
529 return fuse_direct_io(file, buf, count, ppos, 0);
532 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
533 size_t count, loff_t *ppos)
535 struct inode *inode = file->f_dentry->d_inode;
537 /* Don't allow parallel writes to the same file */
539 res = fuse_direct_io(file, buf, count, ppos, 1);
544 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
546 if ((vma->vm_flags & VM_SHARED)) {
547 if ((vma->vm_flags & VM_WRITE))
550 vma->vm_flags &= ~VM_MAYWRITE;
552 return generic_file_mmap(file, vma);
555 static int fuse_set_page_dirty(struct page *page)
557 printk("fuse_set_page_dirty: should not happen\n");
562 static struct file_operations fuse_file_operations = {
563 .llseek = generic_file_llseek,
564 .read = generic_file_read,
565 .write = generic_file_write,
566 .mmap = fuse_file_mmap,
569 .release = fuse_release,
571 .sendfile = generic_file_sendfile,
574 static struct file_operations fuse_direct_io_file_operations = {
575 .llseek = generic_file_llseek,
576 .read = fuse_direct_read,
577 .write = fuse_direct_write,
580 .release = fuse_release,
582 /* no mmap and sendfile */
585 static struct address_space_operations fuse_file_aops = {
586 .readpage = fuse_readpage,
587 .prepare_write = fuse_prepare_write,
588 .commit_write = fuse_commit_write,
589 .readpages = fuse_readpages,
590 .set_page_dirty = fuse_set_page_dirty,
593 void fuse_init_file_inode(struct inode *inode)
595 inode->i_fop = &fuse_file_operations;
596 inode->i_data.a_ops = &fuse_file_aops;