fix
[fuse.git] / kernel / file.c
blob19bd296635a39ec567612b4df7e6b66ee763e8a7
1 /*
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.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
15 #ifndef KERNEL_2_6
16 #define PageUptodate(page) Page_Uptodate(page)
17 #define clear_page_dirty(page) ClearPageDirty(page)
18 #endif
19 static struct file_operations fuse_direct_io_file_operations;
21 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
23 struct fuse_conn *fc = get_fuse_conn(inode);
24 struct fuse_req *req;
25 struct fuse_open_in inarg;
26 struct fuse_open_out outarg;
27 struct fuse_file *ff;
28 int err;
30 /* VFS checks this, but only _after_ ->open() */
31 if (file->f_flags & O_DIRECT)
32 return -EINVAL;
34 err = generic_file_open(inode, file);
35 if (err)
36 return err;
38 /* If opening the root node, no lookup has been performed on
39 it, so the attributes must be refreshed */
40 if (get_node_id(inode) == FUSE_ROOT_ID) {
41 int err = fuse_do_getattr(inode);
42 if (err)
43 return err;
46 req = fuse_get_request(fc);
47 if (!req)
48 return -EINTR;
50 err = -ENOMEM;
51 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
52 if (!ff)
53 goto out_put_request;
55 ff->release_req = fuse_request_alloc();
56 if (!ff->release_req) {
57 kfree(ff);
58 goto out_put_request;
61 memset(&inarg, 0, sizeof(inarg));
62 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
63 req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
64 req->in.h.nodeid = get_node_id(inode);
65 req->inode = inode;
66 req->in.numargs = 1;
67 req->in.args[0].size = sizeof(inarg);
68 req->in.args[0].value = &inarg;
69 req->out.numargs = 1;
70 req->out.args[0].size = sizeof(outarg);
71 req->out.args[0].value = &outarg;
72 request_send(fc, req);
73 err = req->out.h.error;
74 if (err) {
75 fuse_request_free(ff->release_req);
76 kfree(ff);
77 } else {
78 if (!isdir && (outarg.open_flags & FOPEN_DIRECT_IO))
79 file->f_op = &fuse_direct_io_file_operations;
80 if (!(outarg.open_flags & FOPEN_KEEP_CACHE))
81 #ifdef KERNEL_2_6
82 invalidate_inode_pages(inode->i_mapping);
83 #else
84 invalidate_inode_pages(inode);
85 #endif
86 ff->fh = outarg.fh;
87 file->private_data = ff;
90 out_put_request:
91 fuse_put_request(fc, req);
92 return err;
95 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
97 struct fuse_conn *fc = get_fuse_conn(inode);
98 struct fuse_file *ff = file->private_data;
99 struct fuse_req *req = ff->release_req;
100 struct fuse_release_in *inarg = &req->misc.release_in;
102 inarg->fh = ff->fh;
103 inarg->flags = file->f_flags & ~O_EXCL;
104 req->in.h.opcode = isdir ? FUSE_RELEASEDIR : FUSE_RELEASE;
105 req->in.h.nodeid = get_node_id(inode);
106 req->inode = inode;
107 req->in.numargs = 1;
108 req->in.args[0].size = sizeof(struct fuse_release_in);
109 req->in.args[0].value = inarg;
110 request_send_background(fc, req);
111 kfree(ff);
113 /* Return value is ignored by VFS */
114 return 0;
117 static int fuse_open(struct inode *inode, struct file *file)
119 return fuse_open_common(inode, file, 0);
122 static int fuse_release(struct inode *inode, struct file *file)
124 return fuse_release_common(inode, file, 0);
127 static int fuse_flush(struct file *file)
129 struct inode *inode = file->f_dentry->d_inode;
130 struct fuse_conn *fc = get_fuse_conn(inode);
131 struct fuse_file *ff = file->private_data;
132 struct fuse_req *req;
133 struct fuse_flush_in inarg;
134 int err;
136 if (fc->no_flush)
137 return 0;
139 req = fuse_get_request(fc);
140 if (!req)
141 return -EINTR;
143 memset(&inarg, 0, sizeof(inarg));
144 inarg.fh = ff->fh;
145 req->in.h.opcode = FUSE_FLUSH;
146 req->in.h.nodeid = get_node_id(inode);
147 req->inode = inode;
148 req->file = file;
149 req->in.numargs = 1;
150 req->in.args[0].size = sizeof(inarg);
151 req->in.args[0].value = &inarg;
152 request_send(fc, req);
153 err = req->out.h.error;
154 fuse_put_request(fc, req);
155 if (err == -ENOSYS) {
156 fc->no_flush = 1;
157 err = 0;
159 return err;
162 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
163 int isdir)
165 struct inode *inode = de->d_inode;
166 struct fuse_conn *fc = get_fuse_conn(inode);
167 struct fuse_file *ff = file->private_data;
168 struct fuse_req *req;
169 struct fuse_fsync_in inarg;
170 int err;
172 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
173 return 0;
175 req = fuse_get_request(fc);
176 if (!req)
177 return -EINTR;
179 memset(&inarg, 0, sizeof(inarg));
180 inarg.fh = ff->fh;
181 inarg.fsync_flags = datasync ? 1 : 0;
182 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
183 req->in.h.nodeid = get_node_id(inode);
184 req->inode = inode;
185 req->file = file;
186 req->in.numargs = 1;
187 req->in.args[0].size = sizeof(inarg);
188 req->in.args[0].value = &inarg;
189 request_send(fc, req);
190 err = req->out.h.error;
191 fuse_put_request(fc, req);
192 if (err == -ENOSYS) {
193 if (isdir)
194 fc->no_fsyncdir = 1;
195 else
196 fc->no_fsync = 1;
197 err = 0;
199 return err;
202 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
204 return fuse_fsync_common(file, de, datasync, 0);
207 size_t fuse_send_read_common(struct fuse_req *req, struct file *file,
208 struct inode *inode, loff_t pos, size_t count,
209 int isdir)
211 struct fuse_conn *fc = get_fuse_conn(inode);
212 struct fuse_file *ff = file->private_data;
213 struct fuse_read_in inarg;
215 memset(&inarg, 0, sizeof(struct fuse_read_in));
216 inarg.fh = ff->fh;
217 inarg.offset = pos;
218 inarg.size = count;
219 req->in.h.opcode = isdir ? FUSE_READDIR : FUSE_READ;
220 req->in.h.nodeid = get_node_id(inode);
221 req->inode = inode;
222 req->file = file;
223 req->in.numargs = 1;
224 req->in.args[0].size = sizeof(struct fuse_read_in);
225 req->in.args[0].value = &inarg;
226 req->out.argpages = 1;
227 req->out.argvar = 1;
228 req->out.numargs = 1;
229 req->out.args[0].size = count;
230 request_send(fc, req);
231 return req->out.args[0].size;
234 static inline size_t fuse_send_read(struct fuse_req *req, struct file *file,
235 struct inode *inode, loff_t pos,
236 size_t count)
238 return fuse_send_read_common(req, file, inode, pos, count, 0);
241 static int fuse_readpage(struct file *file, struct page *page)
243 struct inode *inode = page->mapping->host;
244 struct fuse_conn *fc = get_fuse_conn(inode);
245 loff_t pos = (loff_t) page->index << PAGE_CACHE_SHIFT;
246 struct fuse_req *req = fuse_get_request(fc);
247 int err = -EINTR;
248 if (!req)
249 goto out;
251 req->out.page_zeroing = 1;
252 req->num_pages = 1;
253 req->pages[0] = page;
254 fuse_send_read(req, file, inode, pos, PAGE_CACHE_SIZE);
255 err = req->out.h.error;
256 fuse_put_request(fc, req);
257 if (!err)
258 SetPageUptodate(page);
259 fuse_invalidate_attr(inode); /* atime changed */
260 out:
261 unlock_page(page);
262 return err;
265 #ifdef KERNEL_2_6
266 static int fuse_send_readpages(struct fuse_req *req, struct file *file,
267 struct inode *inode)
269 loff_t pos = (loff_t) req->pages[0]->index << PAGE_CACHE_SHIFT;
270 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
271 unsigned i;
272 req->out.page_zeroing = 1;
273 fuse_send_read(req, file, inode, pos, count);
274 for (i = 0; i < req->num_pages; i++) {
275 struct page *page = req->pages[i];
276 if (!req->out.h.error)
277 SetPageUptodate(page);
278 unlock_page(page);
280 return req->out.h.error;
283 struct fuse_readpages_data {
284 struct fuse_req *req;
285 struct file *file;
286 struct inode *inode;
289 static int fuse_readpages_fill(void *_data, struct page *page)
291 struct fuse_readpages_data *data = _data;
292 struct fuse_req *req = data->req;
293 struct inode *inode = data->inode;
294 struct fuse_conn *fc = get_fuse_conn(inode);
296 if (req->num_pages &&
297 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
298 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
299 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
300 int err = fuse_send_readpages(req, data->file, inode);
301 if (err) {
302 unlock_page(page);
303 return err;
305 fuse_reset_request(req);
307 req->pages[req->num_pages] = page;
308 req->num_pages ++;
309 return 0;
312 static int fuse_readpages(struct file *file, struct address_space *mapping,
313 struct list_head *pages, unsigned nr_pages)
315 struct inode *inode = mapping->host;
316 struct fuse_conn *fc = get_fuse_conn(inode);
317 struct fuse_readpages_data data;
318 int err;
319 data.file = file;
320 data.inode = inode;
321 data.req = fuse_get_request(fc);
322 if (!data.req)
323 return -EINTR;
325 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
326 if (!err && data.req->num_pages)
327 err = fuse_send_readpages(data.req, file, inode);
328 fuse_put_request(fc, data.req);
329 fuse_invalidate_attr(inode); /* atime changed */
330 return err;
332 #else /* KERNEL_2_6 */
333 #define FUSE_BLOCK_SHIFT 16
334 #define FUSE_BLOCK_SIZE (1UL << FUSE_BLOCK_SHIFT)
335 #define FUSE_BLOCK_MASK (~(FUSE_BLOCK_SIZE-1))
336 #if (1UL << (FUSE_BLOCK_SHIFT - PAGE_CACHE_SHIFT)) > FUSE_MAX_PAGES_PER_REQ
337 #error FUSE_BLOCK_SHIFT too large
338 #endif
340 static int fuse_is_block_uptodate(struct inode *inode, unsigned start,
341 unsigned end)
343 int index;
345 for (index = start; index < end; index++) {
346 struct page *page = find_get_page(inode->i_mapping, index);
347 if (!page)
348 return 0;
349 if (!PageUptodate(page)) {
350 page_cache_release(page);
351 return 0;
353 page_cache_release(page);
355 return 1;
358 static int fuse_file_read_block(struct fuse_req *req, struct file *file,
359 struct inode *inode, unsigned start,
360 unsigned end)
362 loff_t pos;
363 size_t count;
364 int index;
365 int err = -EBUSY;
366 int i;
368 for (index = start; index < end; index++) {
369 struct page *page = grab_cache_page(inode->i_mapping, index);
370 if (!page)
371 goto out;
372 if (PageUptodate(page)) {
373 unlock_page(page);
374 page_cache_release(page);
375 page = NULL;
377 req->pages[req->num_pages++] = page;
379 pos = (loff_t) start << PAGE_CACHE_SHIFT;
380 count = req->num_pages << PAGE_CACHE_SHIFT;
381 fuse_send_read(req, file, inode, pos, count);
382 err = req->out.h.error;
383 out:
384 for (i = 0; i < req->num_pages; i++) {
385 struct page *page = req->pages[i];
386 if (page) {
387 if (!err)
388 SetPageUptodate(page);
389 unlock_page(page);
390 page_cache_release(page);
393 return err;
396 static int fuse_file_bigread(struct file *file, struct inode *inode,
397 loff_t pos, size_t count)
399 struct fuse_conn *fc = get_fuse_conn(inode);
400 unsigned starti;
401 unsigned endi;
402 unsigned nexti;
403 struct fuse_req *req;
404 loff_t size = i_size_read(inode);
405 loff_t end = (pos + count + FUSE_BLOCK_SIZE - 1) & FUSE_BLOCK_MASK;
406 end = min(end, size);
407 if (end <= pos)
408 return 0;
410 starti = (pos & FUSE_BLOCK_MASK) >> PAGE_CACHE_SHIFT;
411 endi = (end + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
413 req = fuse_get_request(fc);
414 if (!req)
415 return -EINTR;
417 for (; starti < endi; starti = nexti) {
418 nexti = starti + (FUSE_BLOCK_SIZE >> PAGE_CACHE_SHIFT);
419 nexti = min(nexti, endi);
420 if (!fuse_is_block_uptodate(inode, starti, nexti)) {
421 if (fuse_file_read_block(req, file, inode, starti, nexti))
422 break;
424 fuse_reset_request(req);
427 fuse_put_request(fc, req);
428 return 0;
430 #endif /* KERNEL_2_6 */
432 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
433 struct inode *inode, loff_t pos, size_t count)
435 struct fuse_conn *fc = get_fuse_conn(inode);
436 struct fuse_file *ff = file->private_data;
437 struct fuse_write_in inarg;
438 struct fuse_write_out outarg;
440 memset(&inarg, 0, sizeof(struct fuse_write_in));
441 inarg.fh = ff->fh;
442 inarg.offset = pos;
443 inarg.size = count;
444 req->in.h.opcode = FUSE_WRITE;
445 req->in.h.nodeid = get_node_id(inode);
446 req->inode = inode;
447 req->file = file;
448 req->in.argpages = 1;
449 req->in.numargs = 2;
450 req->in.args[0].size = sizeof(struct fuse_write_in);
451 req->in.args[0].value = &inarg;
452 req->in.args[1].size = count;
453 req->out.numargs = 1;
454 req->out.args[0].size = sizeof(struct fuse_write_out);
455 req->out.args[0].value = &outarg;
456 request_send(fc, req);
457 return outarg.size;
460 static int fuse_prepare_write(struct file *file, struct page *page,
461 unsigned offset, unsigned to)
463 /* No op */
464 return 0;
467 static int fuse_commit_write(struct file *file, struct page *page,
468 unsigned offset, unsigned to)
470 int err;
471 size_t nres;
472 unsigned count = to - offset;
473 struct inode *inode = page->mapping->host;
474 struct fuse_conn *fc = get_fuse_conn(inode);
475 loff_t pos = ((loff_t) page->index << PAGE_CACHE_SHIFT) + offset;
476 struct fuse_req *req = fuse_get_request(fc);
477 if (!req)
478 return -EINTR;
480 req->num_pages = 1;
481 req->pages[0] = page;
482 req->page_offset = offset;
483 nres = fuse_send_write(req, file, inode, pos, count);
484 err = req->out.h.error;
485 fuse_put_request(fc, req);
486 if (!err && nres != count)
487 err = -EIO;
488 if (!err) {
489 pos += count;
490 if (pos > i_size_read(inode))
491 i_size_write(inode, pos);
493 if (offset == 0 && to == PAGE_CACHE_SIZE) {
494 clear_page_dirty(page);
495 SetPageUptodate(page);
498 fuse_invalidate_attr(inode);
499 return err;
502 static void fuse_release_user_pages(struct fuse_req *req, int write)
504 unsigned i;
506 for (i = 0; i < req->num_pages; i++) {
507 struct page *page = req->pages[i];
508 if (write)
509 set_page_dirty_lock(page);
510 put_page(page);
514 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
515 unsigned nbytes, int write)
517 unsigned long user_addr = (unsigned long) buf;
518 unsigned offset = user_addr & ~PAGE_MASK;
519 int npages;
521 /* This doesn't work with nfsd */
522 if (!current->mm)
523 return -EPERM;
525 nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
526 npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
527 npages = min(npages, FUSE_MAX_PAGES_PER_REQ);
528 down_read(&current->mm->mmap_sem);
529 npages = get_user_pages(current, current->mm, user_addr, npages, write,
530 0, req->pages, NULL);
531 up_read(&current->mm->mmap_sem);
532 if (npages < 0)
533 return npages;
535 req->num_pages = npages;
536 req->page_offset = offset;
537 return 0;
540 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
541 size_t count, loff_t *ppos, int write)
543 struct inode *inode = file->f_dentry->d_inode;
544 struct fuse_conn *fc = get_fuse_conn(inode);
545 size_t nmax = write ? fc->max_write : fc->max_read;
546 loff_t pos = *ppos;
547 ssize_t res = 0;
548 struct fuse_req *req = fuse_get_request(fc);
549 if (!req)
550 return -EINTR;
552 while (count) {
553 size_t tmp;
554 size_t nres;
555 size_t nbytes = min(count, nmax);
556 int err = fuse_get_user_pages(req, buf, nbytes, !write);
557 if (err) {
558 res = err;
559 break;
561 tmp = (req->num_pages << PAGE_SHIFT) - req->page_offset;
562 nbytes = min(nbytes, tmp);
563 if (write)
564 nres = fuse_send_write(req, file, inode, pos, nbytes);
565 else
566 nres = fuse_send_read(req, file, inode, pos, nbytes);
567 fuse_release_user_pages(req, !write);
568 if (req->out.h.error) {
569 if (!res)
570 res = req->out.h.error;
571 break;
572 } else if (nres > nbytes) {
573 res = -EIO;
574 break;
576 count -= nres;
577 res += nres;
578 pos += nres;
579 buf += nres;
580 if (nres != nbytes)
581 break;
582 if (count)
583 fuse_reset_request(req);
585 fuse_put_request(fc, req);
586 if (res > 0) {
587 if (write && pos > i_size_read(inode))
588 i_size_write(inode, pos);
589 *ppos = pos;
591 fuse_invalidate_attr(inode);
593 return res;
596 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
597 size_t count, loff_t *ppos)
599 return fuse_direct_io(file, buf, count, ppos, 0);
602 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
603 size_t count, loff_t *ppos)
605 struct inode *inode = file->f_dentry->d_inode;
606 ssize_t res;
607 /* Don't allow parallel writes to the same file */
608 down(&inode->i_sem);
609 res = fuse_direct_io(file, buf, count, ppos, 1);
610 up(&inode->i_sem);
611 return res;
614 #ifndef KERNEL_2_6
615 static ssize_t fuse_file_read(struct file *file, char __user *buf,
616 size_t count, loff_t *ppos)
618 struct inode *inode = file->f_dentry->d_inode;
619 struct fuse_conn *fc = get_fuse_conn(inode);
621 if (fc->flags & FUSE_LARGE_READ) {
622 int res;
623 down(&inode->i_sem);
624 res = fuse_file_bigread(file, inode, *ppos, count);
625 up(&inode->i_sem);
626 if (res)
627 return res;
629 return generic_file_read(file, buf, count, ppos);
631 #endif
632 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
634 if ((vma->vm_flags & VM_SHARED)) {
635 if ((vma->vm_flags & VM_WRITE))
636 return -ENODEV;
637 else
638 vma->vm_flags &= ~VM_MAYWRITE;
640 return generic_file_mmap(file, vma);
643 #ifdef KERNEL_2_6
644 static int fuse_set_page_dirty(struct page *page)
646 printk("fuse_set_page_dirty: should not happen\n");
647 dump_stack();
648 return 0;
650 #endif
652 static struct file_operations fuse_file_operations = {
653 .llseek = generic_file_llseek,
654 #ifdef KERNEL_2_6
655 .read = generic_file_read,
656 #else
657 .read = fuse_file_read,
658 #endif
659 .write = generic_file_write,
660 .mmap = fuse_file_mmap,
661 .open = fuse_open,
662 .flush = fuse_flush,
663 .release = fuse_release,
664 .fsync = fuse_fsync,
665 #ifdef KERNEL_2_6
666 .sendfile = generic_file_sendfile,
667 #endif
670 static struct file_operations fuse_direct_io_file_operations = {
671 .llseek = generic_file_llseek,
672 .read = fuse_direct_read,
673 .write = fuse_direct_write,
674 .open = fuse_open,
675 .flush = fuse_flush,
676 .release = fuse_release,
677 .fsync = fuse_fsync,
678 /* no mmap and sendfile */
681 static struct address_space_operations fuse_file_aops = {
682 .readpage = fuse_readpage,
683 .prepare_write = fuse_prepare_write,
684 .commit_write = fuse_commit_write,
685 #ifdef KERNEL_2_6
686 .readpages = fuse_readpages,
687 .set_page_dirty = fuse_set_page_dirty,
688 #endif
691 void fuse_init_file_inode(struct inode *inode)
693 inode->i_fop = &fuse_file_operations;
694 inode->i_data.a_ops = &fuse_file_aops;