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>
16 #define PageUptodate(page) Page_Uptodate(page)
17 #define clear_page_dirty(page) ClearPageDirty(page)
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
);
25 struct fuse_open_in inarg
;
26 struct fuse_open_out outarg
;
30 /* VFS checks this, but only _after_ ->open() */
31 if (file
->f_flags
& O_DIRECT
)
34 err
= generic_file_open(inode
, file
);
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
);
46 req
= fuse_get_request(fc
);
51 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
55 ff
->release_req
= fuse_request_alloc();
56 if (!ff
->release_req
) {
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
);
67 req
->in
.args
[0].size
= sizeof(inarg
);
68 req
->in
.args
[0].value
= &inarg
;
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
;
75 fuse_request_free(ff
->release_req
);
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
))
82 invalidate_inode_pages(inode
->i_mapping
);
84 invalidate_inode_pages(inode
);
87 file
->private_data
= ff
;
91 fuse_put_request(fc
, req
);
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
;
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
);
108 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
109 req
->in
.args
[0].value
= inarg
;
110 request_send_background(fc
, req
);
113 /* Return value is ignored by VFS */
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
;
139 req
= fuse_get_request(fc
);
143 memset(&inarg
, 0, sizeof(inarg
));
145 req
->in
.h
.opcode
= FUSE_FLUSH
;
146 req
->in
.h
.nodeid
= get_node_id(inode
);
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
) {
162 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
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
;
172 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
175 req
= fuse_get_request(fc
);
179 memset(&inarg
, 0, sizeof(inarg
));
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
);
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
) {
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
,
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
));
219 req
->in
.h
.opcode
= isdir
? FUSE_READDIR
: FUSE_READ
;
220 req
->in
.h
.nodeid
= get_node_id(inode
);
224 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
225 req
->in
.args
[0].value
= &inarg
;
226 req
->out
.argpages
= 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
,
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
);
251 req
->out
.page_zeroing
= 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
);
258 SetPageUptodate(page
);
259 fuse_invalidate_attr(inode
); /* atime changed */
266 static int fuse_send_readpages(struct fuse_req
*req
, struct file
*file
,
269 loff_t pos
= (loff_t
) req
->pages
[0]->index
<< PAGE_CACHE_SHIFT
;
270 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
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
);
280 return req
->out
.h
.error
;
283 struct fuse_readpages_data
{
284 struct fuse_req
*req
;
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
);
305 fuse_reset_request(req
);
307 req
->pages
[req
->num_pages
] = page
;
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
;
321 data
.req
= fuse_get_request(fc
);
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 */
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
340 static int fuse_is_block_uptodate(struct inode
*inode
, unsigned start
,
345 for (index
= start
; index
< end
; index
++) {
346 struct page
*page
= find_get_page(inode
->i_mapping
, index
);
349 if (!PageUptodate(page
)) {
350 page_cache_release(page
);
353 page_cache_release(page
);
358 static int fuse_file_read_block(struct fuse_req
*req
, struct file
*file
,
359 struct inode
*inode
, unsigned start
,
368 for (index
= start
; index
< end
; index
++) {
369 struct page
*page
= grab_cache_page(inode
->i_mapping
, index
);
372 if (PageUptodate(page
)) {
374 page_cache_release(page
);
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
;
384 for (i
= 0; i
< req
->num_pages
; i
++) {
385 struct page
*page
= req
->pages
[i
];
388 SetPageUptodate(page
);
390 page_cache_release(page
);
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
);
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
);
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
);
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
))
424 fuse_reset_request(req
);
427 fuse_put_request(fc
, req
);
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
));
444 req
->in
.h
.opcode
= FUSE_WRITE
;
445 req
->in
.h
.nodeid
= get_node_id(inode
);
448 req
->in
.argpages
= 1;
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
);
460 static int fuse_prepare_write(struct file
*file
, struct page
*page
,
461 unsigned offset
, unsigned to
)
467 static int fuse_commit_write(struct file
*file
, struct page
*page
,
468 unsigned offset
, unsigned to
)
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
);
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
)
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
);
502 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
506 for (i
= 0; i
< req
->num_pages
; i
++) {
507 struct page
*page
= req
->pages
[i
];
509 set_page_dirty_lock(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
;
521 /* This doesn't work with nfsd */
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(¤t
->mm
->mmap_sem
);
529 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, write
,
530 0, req
->pages
, NULL
);
531 up_read(¤t
->mm
->mmap_sem
);
535 req
->num_pages
= npages
;
536 req
->page_offset
= offset
;
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
;
548 struct fuse_req
*req
= fuse_get_request(fc
);
555 size_t nbytes
= min(count
, nmax
);
556 int err
= fuse_get_user_pages(req
, buf
, nbytes
, !write
);
561 tmp
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
562 nbytes
= min(nbytes
, tmp
);
564 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
);
566 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
);
567 fuse_release_user_pages(req
, !write
);
568 if (req
->out
.h
.error
) {
570 res
= req
->out
.h
.error
;
572 } else if (nres
> nbytes
) {
583 fuse_reset_request(req
);
585 fuse_put_request(fc
, req
);
587 if (write
&& pos
> i_size_read(inode
))
588 i_size_write(inode
, pos
);
591 fuse_invalidate_attr(inode
);
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
;
607 /* Don't allow parallel writes to the same file */
609 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
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
) {
624 res
= fuse_file_bigread(file
, inode
, *ppos
, count
);
629 return generic_file_read(file
, buf
, count
, ppos
);
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
))
638 vma
->vm_flags
&= ~VM_MAYWRITE
;
640 return generic_file_mmap(file
, vma
);
644 static int fuse_set_page_dirty(struct page
*page
)
646 printk("fuse_set_page_dirty: should not happen\n");
652 static struct file_operations fuse_file_operations
= {
653 .llseek
= generic_file_llseek
,
655 .read
= generic_file_read
,
657 .read
= fuse_file_read
,
659 .write
= generic_file_write
,
660 .mmap
= fuse_file_mmap
,
663 .release
= fuse_release
,
666 .sendfile
= generic_file_sendfile
,
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
,
676 .release
= fuse_release
,
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
,
686 .readpages
= fuse_readpages
,
687 .set_page_dirty
= fuse_set_page_dirty
,
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
;