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 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
19 struct fuse_conn *fc = get_fuse_conn(inode);
21 struct fuse_open_in inarg;
22 struct fuse_open_out outarg;
26 /* VFS checks this, but only _after_ ->open() */
27 if (file->f_flags & O_DIRECT)
30 err = generic_file_open(inode, file);
34 /* If opening the root node, no lookup has been performed on
35 it, so the attributes must be refreshed */
36 if (get_node_id(inode) == FUSE_ROOT_ID) {
37 int err = fuse_do_getattr(inode);
42 req = fuse_get_request(fc);
47 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
51 ff->release_req = fuse_request_alloc();
52 if (!ff->release_req) {
57 memset(&inarg, 0, sizeof(inarg));
58 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
59 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
60 req->in.h.nodeid = get_node_id(inode);
63 req->in.args[0].size = sizeof(inarg);
64 req->in.args[0].value = &inarg;
66 req->out.args[0].size = sizeof(outarg);
67 req->out.args[0].value = &outarg;
68 request_send(fc, req);
69 err = req->out.h.error;
71 fuse_request_free(ff->release_req);
74 if (!isdir && (outarg.open_flags & FOPEN_DIRECT_IO))
75 file->f_op = &fuse_direct_io_file_operations;
76 if (!(outarg.open_flags & FOPEN_KEEP_CACHE))
77 invalidate_inode_pages(inode->i_mapping);
79 file->private_data = ff;
83 fuse_put_request(fc, req);
87 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
89 struct fuse_conn *fc = get_fuse_conn(inode);
90 struct fuse_file *ff = file->private_data;
91 struct fuse_req *req = ff->release_req;
92 struct fuse_release_in *inarg = &req->misc.release_in;
95 inarg->flags = file->f_flags & ~O_EXCL;
96 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
97 req->in.h.nodeid = get_node_id(inode);
100 req->in.args[0].size = sizeof(struct fuse_release_in);
101 req->in.args[0].value = inarg;
102 request_send_background(fc, req);
105 /* Return value is ignored by VFS */
109 static int fuse_open(struct inode *inode, struct file *file)
111 return fuse_open_common(inode, file, 0);
114 static int fuse_release(struct inode *inode, struct file *file)
116 return fuse_release_common(inode, file, 0);
119 static int fuse_flush(struct file *file)
121 struct inode *inode = file->f_dentry->d_inode;
122 struct fuse_conn *fc = get_fuse_conn(inode);
123 struct fuse_file *ff = file->private_data;
124 struct fuse_req *req;
125 struct fuse_flush_in inarg;
131 req = fuse_get_request(fc);
135 memset(&inarg, 0, sizeof(inarg));
137 req->in.h.opcode = FUSE_FLUSH;
138 req->in.h.nodeid = get_node_id(inode);
142 req->in.args[0].size = sizeof(inarg);
143 req->in.args[0].value = &inarg;
144 request_send(fc, req);
145 err = req->out.h.error;
146 fuse_put_request(fc, req);
147 if (err == -ENOSYS) {
154 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
157 struct inode *inode = de->d_inode;
158 struct fuse_conn *fc = get_fuse_conn(inode);
159 struct fuse_file *ff = file->private_data;
160 struct fuse_req *req;
161 struct fuse_fsync_in inarg;
164 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
167 req = fuse_get_request(fc);
171 memset(&inarg, 0, sizeof(inarg));
173 inarg.fsync_flags = datasync ? 1 : 0;
174 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
175 req->in.h.nodeid = get_node_id(inode);
179 req->in.args[0].size = sizeof(inarg);
180 req->in.args[0].value = &inarg;
181 request_send(fc, req);
182 err = req->out.h.error;
183 fuse_put_request(fc, req);
184 if (err == -ENOSYS) {
194 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
196 return fuse_fsync_common(file, de, datasync, 0);
199 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
200 struct inode *inode, loff_t pos, size_t count,
203 struct fuse_conn *fc = get_fuse_conn(inode);
204 struct fuse_file *ff = file->private_data;
205 struct fuse_read_in inarg;
207 memset(&inarg, 0, sizeof(struct fuse_read_in));
211 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
212 req->in.h.nodeid = get_node_id(inode);
216 req->in.args[0].size = sizeof(struct fuse_read_in);
217 req->in.args[0].value = &inarg;
218 req->out.argpages = 1;
220 req->out.numargs = 1;
221 req->out.args[0].size = count;
222 request_send(fc, req);
223 return req->out.args[0].size;
226 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
227 struct inode *inode, loff_t pos,
230 return fuse_send_read_common(req, file, inode, pos, count, 0);
233 static int fuse_readpage(struct file *file, struct page *page)
235 struct inode *inode = page->mapping->host;
236 struct fuse_conn *fc = get_fuse_conn(inode);
237 loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
238 struct fuse_req *req = fuse_get_request(fc);
243 req->out.page_zeroing = 1;
245 req->pages[0] = page;
246 fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
247 err = req->out.h.error;
248 fuse_put_request(fc, req);
250 SetPageUptodate(page);
251 fuse_invalidate_attr(inode); /* atime changed */
257 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
260 loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT;
261 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
263 req->out.page_zeroing = 1;
264 fuse_send_read(req, file, inode, pos, count);
265 for (i = 0; i < req->num_pages; i++) {
266 struct page *page = req->pages[i];
267 if (!req->out.h.error)
268 SetPageUptodate(page);
271 return req->out.h.error;
274 struct fuse_readpages_data {
275 struct fuse_req *req;
280 static int fuse_readpages_fill(void *_data, struct page *page)
282 struct fuse_readpages_data *data = _data;
283 struct fuse_req *req = data->req;
284 struct inode *inode = data->inode;
285 struct fuse_conn *fc = get_fuse_conn(inode);
287 if (req->num_pages &&
288 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
289 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
290 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
291 int err = fuse_send_readpages(req, data->file, inode);
296 fuse_reset_request(req);
298 req->pages[req->num_pages] = page;
303 static int fuse_readpages(struct file *file, struct address_space *mapping,
304 struct list_head *pages, unsigned nr_pages)
306 struct inode *inode = mapping->host;
307 struct fuse_conn *fc = get_fuse_conn(inode);
308 struct fuse_readpages_data data;
312 data.req = fuse_get_request(fc);
316 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
317 if (!err && data.req->num_pages)
318 err = fuse_send_readpages(data.req, file, inode);
319 fuse_put_request(fc, data.req);
320 fuse_invalidate_attr(inode); /* atime changed */
324 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
325 struct inode *inode, loff_t pos, size_t count)
327 struct fuse_conn *fc = get_fuse_conn(inode);
328 struct fuse_file *ff = file->private_data;
329 struct fuse_write_in inarg;
330 struct fuse_write_out outarg;
332 memset(&inarg, 0, sizeof(struct fuse_write_in));
336 req->in.h.opcode = FUSE_WRITE;
337 req->in.h.nodeid = get_node_id(inode);
340 req->in.argpages = 1;
342 req->in.args[0].size = sizeof(struct fuse_write_in);
343 req->in.args[0].value = &inarg;
344 req->in.args[1].size = count;
345 req->out.numargs = 1;
346 req->out.args[0].size = sizeof(struct fuse_write_out);
347 req->out.args[0].value = &outarg;
348 request_send(fc, req);
352 static int fuse_prepare_write(struct file *file, struct page *page,
353 unsigned offset, unsigned to)
359 static int fuse_commit_write(struct file *file, struct page *page,
360 unsigned offset, unsigned to)
364 unsigned count = to - offset;
365 struct inode *inode = page->mapping->host;
366 struct fuse_conn *fc = get_fuse_conn(inode);
367 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
368 struct fuse_req *req = fuse_get_request(fc);
373 req->pages[0] = page;
374 req->page_offset = offset;
375 nres = fuse_send_write(req, file, inode, pos, count);
376 err = req->out.h.error;
377 fuse_put_request(fc, req);
378 if (!err && nres != count)
382 if (pos > i_size_read(inode))
383 i_size_write(inode, pos);
385 if (offset == 0 && to == PAGE_CACHE_SIZE) {
386 clear_page_dirty(page);
387 SetPageUptodate(page);
390 fuse_invalidate_attr(inode);
394 static void fuse_release_user_pages(struct fuse_req *req, int write)
398 for (i = 0; i < req->num_pages; i++) {
399 struct page *page = req->pages[i];
401 set_page_dirty_lock(page);
406 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
407 unsigned nbytes, int write)
409 unsigned long user_addr = (unsigned long) buf;
410 unsigned offset = user_addr & ~PAGE_MASK;
413 /* This doesn't work with nfsd */
417 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
418 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
419 npages = min(npages, FUSE_MAX_PAGES_PER_REQ);
420 down_read(¤t->mm->mmap_sem);
421 npages = get_user_pages(current, current->mm, user_addr, npages, write,
422 0, req->pages, NULL);
423 up_read(¤t->mm->mmap_sem);
427 req->num_pages = npages;
428 req->page_offset = offset;
432 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
433 size_t count, loff_t *ppos, int write)
435 struct inode *inode = file->f_dentry->d_inode;
436 struct fuse_conn *fc = get_fuse_conn(inode);
437 size_t nmax = write ? fc->max_write : fc->max_read;
440 struct fuse_req *req = fuse_get_request(fc);
447 size_t nbytes = min(count, nmax);
448 int err = fuse_get_user_pages(req, buf, nbytes, !write);
453 tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset;
454 nbytes = min(nbytes, tmp);
456 nres = fuse_send_write(req, file, inode, pos, nbytes);
458 nres = fuse_send_read(req, file, inode, pos, nbytes);
459 fuse_release_user_pages(req, !write);
460 if (req->out.h.error) {
462 res = req->out.h.error;
464 } else if (nres > nbytes) {
475 fuse_reset_request(req);
477 fuse_put_request(fc, req);
479 if (write && pos > i_size_read(inode))
480 i_size_write(inode, pos);
483 fuse_invalidate_attr(inode);
488 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
489 size_t count, loff_t *ppos)
491 return fuse_direct_io(file, buf, count, ppos, 0);
494 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
495 size_t count, loff_t *ppos)
497 struct inode *inode = file->f_dentry->d_inode;
499 /* Don't allow parallel writes to the same file */
501 res = fuse_direct_io(file, buf, count, ppos, 1);
506 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
508 if ((vma->vm_flags & VM_SHARED)) {
509 if ((vma->vm_flags & VM_WRITE))
512 vma->vm_flags &= ~VM_MAYWRITE;
514 return generic_file_mmap(file, vma);
517 static int fuse_set_page_dirty(struct page *page)
519 printk("fuse_set_page_dirty: should not happen\n");
524 static struct file_operations fuse_file_operations = {
525 .llseek = generic_file_llseek,
526 .read = generic_file_read,
527 .write = generic_file_write,
528 .mmap = fuse_file_mmap,
531 .release = fuse_release,
533 .sendfile = generic_file_sendfile,
536 static struct file_operations fuse_direct_io_file_operations = {
537 .llseek = generic_file_llseek,
538 .read = fuse_direct_read,
539 .write = fuse_direct_write,
542 .release = fuse_release,
544 /* no mmap and sendfile */
547 static struct address_space_operations fuse_file_aops = {
548 .readpage = fuse_readpage,
549 .prepare_write = fuse_prepare_write,
550 .commit_write = fuse_commit_write,
551 .readpages = fuse_readpages,
552 .set_page_dirty = fuse_set_page_dirty,
555 void fuse_init_file_inode(struct inode *inode)
557 inode->i_fop = &fuse_file_operations;
558 inode->i_data.a_ops = &fuse_file_aops;