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;
166 if (is_bad_inode(inode))
172 req = fuse_get_request(fc);
176 memset(&inarg, 0, sizeof(inarg));
178 req->in.h.opcode = FUSE_FLUSH;
179 req->in.h.nodeid = get_node_id(inode);
183 req->in.args[0].size = sizeof(inarg);
184 req->in.args[0].value = &inarg;
185 request_send(fc, req);
186 err = req->out.h.error;
187 fuse_put_request(fc, req);
188 if (err == -ENOSYS) {
195 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
198 struct inode *inode = de->d_inode;
199 struct fuse_conn *fc = get_fuse_conn(inode);
200 struct fuse_file *ff = file->private_data;
201 struct fuse_req *req;
202 struct fuse_fsync_in inarg;
205 if (is_bad_inode(inode))
208 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
211 req = fuse_get_request(fc);
215 memset(&inarg, 0, sizeof(inarg));
217 inarg.fsync_flags = datasync ? 1 : 0;
218 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
219 req->in.h.nodeid = get_node_id(inode);
223 req->in.args[0].size = sizeof(inarg);
224 req->in.args[0].value = &inarg;
225 request_send(fc, req);
226 err = req->out.h.error;
227 fuse_put_request(fc, req);
228 if (err == -ENOSYS) {
238 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
240 return fuse_fsync_common(file, de, datasync, 0);
243 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
244 struct inode *inode, loff_t pos, size_t count,
247 struct fuse_conn *fc = get_fuse_conn(inode);
248 struct fuse_file *ff = file->private_data;
249 struct fuse_read_in inarg;
251 memset(&inarg, 0, sizeof(struct fuse_read_in));
255 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
256 req->in.h.nodeid = get_node_id(inode);
260 req->in.args[0].size = sizeof(struct fuse_read_in);
261 req->in.args[0].value = &inarg;
262 req->out.argpages = 1;
264 req->out.numargs = 1;
265 req->out.args[0].size = count;
266 request_send(fc, req);
267 return req->out.args[0].size;
270 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
271 struct inode *inode, loff_t pos,
274 return fuse_send_read_common(req, file, inode, pos, count, 0);
277 static int fuse_readpage(struct file *file, struct page *page)
279 struct inode *inode = page->mapping->host;
280 struct fuse_conn *fc = get_fuse_conn(inode);
281 struct fuse_req *req;
285 if (is_bad_inode(inode))
289 req = fuse_get_request(fc);
293 req->out.page_zeroing = 1;
295 req->pages[0] = page;
296 fuse_send_read(req, file, inode, page_offset(page), PAGE_CACHE_SIZE);
297 err = req->out.h.error;
298 fuse_put_request(fc, req);
300 SetPageUptodate(page);
301 fuse_invalidate_attr(inode); /* atime changed */
307 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
310 loff_t pos = page_offset(req->pages[0]);
311 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
313 req->out.page_zeroing = 1;
314 fuse_send_read(req, file, inode, pos, count);
315 for (i = 0; i < req->num_pages; i++) {
316 struct page *page = req->pages[i];
317 if (!req->out.h.error)
318 SetPageUptodate(page);
321 return req->out.h.error;
324 struct fuse_readpages_data {
325 struct fuse_req *req;
330 static int fuse_readpages_fill(void *_data, struct page *page)
332 struct fuse_readpages_data *data = _data;
333 struct fuse_req *req = data->req;
334 struct inode *inode = data->inode;
335 struct fuse_conn *fc = get_fuse_conn(inode);
337 if (req->num_pages &&
338 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
339 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
340 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
341 int err = fuse_send_readpages(req, data->file, inode);
346 fuse_reset_request(req);
348 req->pages[req->num_pages] = page;
353 static int fuse_readpages(struct file *file, struct address_space *mapping,
354 struct list_head *pages, unsigned nr_pages)
356 struct inode *inode = mapping->host;
357 struct fuse_conn *fc = get_fuse_conn(inode);
358 struct fuse_readpages_data data;
361 if (is_bad_inode(inode))
366 data.req = fuse_get_request(fc);
370 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
371 if (!err && data.req->num_pages)
372 err = fuse_send_readpages(data.req, file, inode);
373 fuse_put_request(fc, data.req);
374 fuse_invalidate_attr(inode); /* atime changed */
378 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
379 struct inode *inode, loff_t pos, size_t count)
381 struct fuse_conn *fc = get_fuse_conn(inode);
382 struct fuse_file *ff = file->private_data;
383 struct fuse_write_in inarg;
384 struct fuse_write_out outarg;
386 memset(&inarg, 0, sizeof(struct fuse_write_in));
390 req->in.h.opcode = FUSE_WRITE;
391 req->in.h.nodeid = get_node_id(inode);
394 req->in.argpages = 1;
396 req->in.args[0].size = sizeof(struct fuse_write_in);
397 req->in.args[0].value = &inarg;
398 req->in.args[1].size = count;
399 req->out.numargs = 1;
400 req->out.args[0].size = sizeof(struct fuse_write_out);
401 req->out.args[0].value = &outarg;
402 request_send(fc, req);
406 static int fuse_prepare_write(struct file *file, struct page *page,
407 unsigned offset, unsigned to)
413 static int fuse_commit_write(struct file *file, struct page *page,
414 unsigned offset, unsigned to)
418 unsigned count = to - offset;
419 struct inode *inode = page->mapping->host;
420 struct fuse_conn *fc = get_fuse_conn(inode);
421 loff_t pos = page_offset(page) + offset;
422 struct fuse_req *req;
424 if (is_bad_inode(inode))
427 req = fuse_get_request(fc);
432 req->pages[0] = page;
433 req->page_offset = offset;
434 nres = fuse_send_write(req, file, inode, pos, count);
435 err = req->out.h.error;
436 fuse_put_request(fc, req);
437 if (!err && nres != count)
441 if (pos > i_size_read(inode))
442 i_size_write(inode, pos);
444 if (offset == 0 && to == PAGE_CACHE_SIZE) {
445 clear_page_dirty(page);
446 SetPageUptodate(page);
449 fuse_invalidate_attr(inode);
453 static void fuse_release_user_pages(struct fuse_req *req, int write)
457 for (i = 0; i < req->num_pages; i++) {
458 struct page *page = req->pages[i];
460 set_page_dirty_lock(page);
465 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
466 unsigned nbytes, int write)
468 unsigned long user_addr = (unsigned long) buf;
469 unsigned offset = user_addr & ~PAGE_MASK;
472 /* This doesn't work with nfsd */
476 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
477 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
478 npages = min(max(npages, 1), FUSE_MAX_PAGES_PER_REQ);
479 down_read(¤t->mm->mmap_sem);
480 npages = get_user_pages(current, current->mm, user_addr, npages, write,
481 0, req->pages, NULL);
482 up_read(¤t->mm->mmap_sem);
486 req->num_pages = npages;
487 req->page_offset = offset;
491 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
492 size_t count, loff_t *ppos, int write)
494 struct inode *inode = file->f_dentry->d_inode;
495 struct fuse_conn *fc = get_fuse_conn(inode);
496 size_t nmax = write ? fc->max_write : fc->max_read;
499 struct fuse_req *req;
501 if (is_bad_inode(inode))
504 req = fuse_get_request(fc);
510 size_t nbytes = min(count, nmax);
511 int err = fuse_get_user_pages(req, buf, nbytes, !write);
516 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
517 nbytes = min(count, nbytes);
519 nres = fuse_send_write(req, file, inode, pos, nbytes);
521 nres = fuse_send_read(req, file, inode, pos, nbytes);
522 fuse_release_user_pages(req, !write);
523 if (req->out.h.error) {
525 res = req->out.h.error;
527 } else if (nres > nbytes) {
538 fuse_reset_request(req);
540 fuse_put_request(fc, req);
542 if (write && pos > i_size_read(inode))
543 i_size_write(inode, pos);
546 fuse_invalidate_attr(inode);
551 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
552 size_t count, loff_t *ppos)
554 return fuse_direct_io(file, buf, count, ppos, 0);
557 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
558 size_t count, loff_t *ppos)
560 struct inode *inode = file->f_dentry->d_inode;
562 /* Don't allow parallel writes to the same file */
563 mutex_lock(&inode->i_mutex);
564 res = fuse_direct_io(file, buf, count, ppos, 1);
565 mutex_unlock(&inode->i_mutex);
569 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
571 if ((vma->vm_flags & VM_SHARED)) {
572 if ((vma->vm_flags & VM_WRITE))
575 vma->vm_flags &= ~VM_MAYWRITE;
577 return generic_file_mmap(file, vma);
580 static int fuse_set_page_dirty(struct page *page)
582 printk("fuse_set_page_dirty: should not happen\n");
587 static struct file_operations fuse_file_operations = {
588 .llseek = generic_file_llseek,
589 .read = generic_file_read,
590 .write = generic_file_write,
591 .mmap = fuse_file_mmap,
594 .release = fuse_release,
596 .sendfile = generic_file_sendfile,
599 static struct file_operations fuse_direct_io_file_operations = {
600 .llseek = generic_file_llseek,
601 .read = fuse_direct_read,
602 .write = fuse_direct_write,
605 .release = fuse_release,
607 /* no mmap and sendfile */
610 static struct address_space_operations fuse_file_aops = {
611 .readpage = fuse_readpage,
612 .prepare_write = fuse_prepare_write,
613 .commit_write = fuse_commit_write,
614 .readpages = fuse_readpages,
615 .set_page_dirty = fuse_set_page_dirty,
618 void fuse_init_file_inode(struct inode *inode)
620 inode->i_fop = &fuse_file_operations;
621 inode->i_data.a_ops = &fuse_file_aops;