2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 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>
14 #include <linux/sched.h>
16 static const struct file_operations fuse_direct_io_file_operations
;
18 static int fuse_send_open(struct inode
*inode
, struct file
*file
, int isdir
,
19 struct fuse_open_out
*outargp
)
21 struct fuse_conn
*fc
= get_fuse_conn(inode
);
22 struct fuse_open_in inarg
;
26 req
= fuse_get_req(fc
);
30 memset(&inarg
, 0, sizeof(inarg
));
31 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
32 req
->in
.h
.opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
33 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
->reserved_req
= fuse_request_alloc();
53 if (!ff
->reserved_req
) {
61 void fuse_file_free(struct fuse_file
*ff
)
63 fuse_request_free(ff
->reserved_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_mapping_pages(inode
->i_mapping
, 0, -1);
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 struct fuse_req
*fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
,
119 struct fuse_req
*req
= ff
->reserved_req
;
120 struct fuse_release_in
*inarg
= &req
->misc
.release_in
;
123 inarg
->flags
= flags
;
124 req
->in
.h
.opcode
= opcode
;
125 req
->in
.h
.nodeid
= nodeid
;
127 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
128 req
->in
.args
[0].value
= inarg
;
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 struct fuse_req
*req
;
141 req
= fuse_release_fill(ff
, get_node_id(inode
), file
->f_flags
,
142 isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
);
144 /* Hold vfsmount and dentry until release is finished */
145 req
->vfsmount
= mntget(file
->f_path
.mnt
);
146 req
->dentry
= dget(file
->f_path
.dentry
);
147 request_send_background(fc
, req
);
150 /* Return value is ignored by VFS */
154 static int fuse_open(struct inode
*inode
, struct file
*file
)
156 return fuse_open_common(inode
, file
, 0);
159 static int fuse_release(struct inode
*inode
, struct file
*file
)
161 return fuse_release_common(inode
, file
, 0);
165 * Scramble the ID space with XTEA, so that the value of the files_struct
166 * pointer is not exposed to userspace.
168 static u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
170 u32
*k
= fc
->scramble_key
;
171 u64 v
= (unsigned long) id
;
177 for (i
= 0; i
< 32; i
++) {
178 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
180 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
183 return (u64
) v0
+ ((u64
) v1
<< 32);
186 static int fuse_flush(struct file
*file
, fl_owner_t id
)
188 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
189 struct fuse_conn
*fc
= get_fuse_conn(inode
);
190 struct fuse_file
*ff
= file
->private_data
;
191 struct fuse_req
*req
;
192 struct fuse_flush_in inarg
;
195 if (is_bad_inode(inode
))
201 req
= fuse_get_req_nofail(fc
, file
);
202 memset(&inarg
, 0, sizeof(inarg
));
204 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
205 req
->in
.h
.opcode
= FUSE_FLUSH
;
206 req
->in
.h
.nodeid
= get_node_id(inode
);
208 req
->in
.args
[0].size
= sizeof(inarg
);
209 req
->in
.args
[0].value
= &inarg
;
211 request_send(fc
, req
);
212 err
= req
->out
.h
.error
;
213 fuse_put_request(fc
, req
);
214 if (err
== -ENOSYS
) {
221 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
224 struct inode
*inode
= de
->d_inode
;
225 struct fuse_conn
*fc
= get_fuse_conn(inode
);
226 struct fuse_file
*ff
= file
->private_data
;
227 struct fuse_req
*req
;
228 struct fuse_fsync_in inarg
;
231 if (is_bad_inode(inode
))
234 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
237 req
= fuse_get_req(fc
);
241 memset(&inarg
, 0, sizeof(inarg
));
243 inarg
.fsync_flags
= datasync
? 1 : 0;
244 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
245 req
->in
.h
.nodeid
= get_node_id(inode
);
247 req
->in
.args
[0].size
= sizeof(inarg
);
248 req
->in
.args
[0].value
= &inarg
;
249 request_send(fc
, req
);
250 err
= req
->out
.h
.error
;
251 fuse_put_request(fc
, req
);
252 if (err
== -ENOSYS
) {
262 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
264 return fuse_fsync_common(file
, de
, datasync
, 0);
267 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
268 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
)
270 struct fuse_file
*ff
= file
->private_data
;
271 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
276 req
->in
.h
.opcode
= opcode
;
277 req
->in
.h
.nodeid
= get_node_id(inode
);
279 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
280 req
->in
.args
[0].value
= inarg
;
281 req
->out
.argpages
= 1;
283 req
->out
.numargs
= 1;
284 req
->out
.args
[0].size
= count
;
287 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
288 struct inode
*inode
, loff_t pos
, size_t count
)
290 struct fuse_conn
*fc
= get_fuse_conn(inode
);
291 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
292 request_send(fc
, req
);
293 return req
->out
.args
[0].size
;
296 static int fuse_readpage(struct file
*file
, struct page
*page
)
298 struct inode
*inode
= page
->mapping
->host
;
299 struct fuse_conn
*fc
= get_fuse_conn(inode
);
300 struct fuse_req
*req
;
304 if (is_bad_inode(inode
))
307 req
= fuse_get_req(fc
);
312 req
->out
.page_zeroing
= 1;
314 req
->pages
[0] = page
;
315 fuse_send_read(req
, file
, inode
, page_offset(page
), PAGE_CACHE_SIZE
);
316 err
= req
->out
.h
.error
;
317 fuse_put_request(fc
, req
);
319 SetPageUptodate(page
);
320 fuse_invalidate_attr(inode
); /* atime changed */
326 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
330 fuse_invalidate_attr(req
->pages
[0]->mapping
->host
); /* atime changed */
332 for (i
= 0; i
< req
->num_pages
; i
++) {
333 struct page
*page
= req
->pages
[i
];
334 if (!req
->out
.h
.error
)
335 SetPageUptodate(page
);
340 fuse_put_request(fc
, req
);
343 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
,
346 struct fuse_conn
*fc
= get_fuse_conn(inode
);
347 loff_t pos
= page_offset(req
->pages
[0]);
348 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
349 req
->out
.page_zeroing
= 1;
350 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
351 if (fc
->async_read
) {
354 req
->end
= fuse_readpages_end
;
355 request_send_background(fc
, req
);
357 request_send(fc
, req
);
358 fuse_readpages_end(fc
, req
);
362 struct fuse_readpages_data
{
363 struct fuse_req
*req
;
368 static int fuse_readpages_fill(void *_data
, struct page
*page
)
370 struct fuse_readpages_data
*data
= _data
;
371 struct fuse_req
*req
= data
->req
;
372 struct inode
*inode
= data
->inode
;
373 struct fuse_conn
*fc
= get_fuse_conn(inode
);
375 if (req
->num_pages
&&
376 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
377 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
378 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
379 fuse_send_readpages(req
, data
->file
, inode
);
380 data
->req
= req
= fuse_get_req(fc
);
386 req
->pages
[req
->num_pages
] = page
;
391 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
392 struct list_head
*pages
, unsigned nr_pages
)
394 struct inode
*inode
= mapping
->host
;
395 struct fuse_conn
*fc
= get_fuse_conn(inode
);
396 struct fuse_readpages_data data
;
400 if (is_bad_inode(inode
))
405 data
.req
= fuse_get_req(fc
);
406 err
= PTR_ERR(data
.req
);
407 if (IS_ERR(data
.req
))
410 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
412 if (data
.req
->num_pages
)
413 fuse_send_readpages(data
.req
, file
, inode
);
415 fuse_put_request(fc
, data
.req
);
421 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
422 struct inode
*inode
, loff_t pos
, size_t count
)
424 struct fuse_conn
*fc
= get_fuse_conn(inode
);
425 struct fuse_file
*ff
= file
->private_data
;
426 struct fuse_write_in inarg
;
427 struct fuse_write_out outarg
;
429 memset(&inarg
, 0, sizeof(struct fuse_write_in
));
433 req
->in
.h
.opcode
= FUSE_WRITE
;
434 req
->in
.h
.nodeid
= get_node_id(inode
);
435 req
->in
.argpages
= 1;
437 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
438 req
->in
.args
[0].value
= &inarg
;
439 req
->in
.args
[1].size
= count
;
440 req
->out
.numargs
= 1;
441 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
442 req
->out
.args
[0].value
= &outarg
;
443 request_send(fc
, req
);
447 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
448 loff_t pos
, unsigned len
, unsigned flags
,
449 struct page
**pagep
, void **fsdata
)
451 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
453 *pagep
= __grab_cache_page(mapping
, index
);
459 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
460 loff_t pos
, unsigned count
, struct page
*page
)
464 struct fuse_conn
*fc
= get_fuse_conn(inode
);
465 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
466 struct fuse_req
*req
;
468 if (is_bad_inode(inode
))
471 req
= fuse_get_req(fc
);
476 req
->pages
[0] = page
;
477 req
->page_offset
= offset
;
478 nres
= fuse_send_write(req
, file
, inode
, pos
, count
);
479 err
= req
->out
.h
.error
;
480 fuse_put_request(fc
, req
);
485 spin_lock(&fc
->lock
);
486 if (pos
> inode
->i_size
)
487 i_size_write(inode
, pos
);
488 spin_unlock(&fc
->lock
);
490 if (count
== PAGE_CACHE_SIZE
)
491 SetPageUptodate(page
);
493 fuse_invalidate_attr(inode
);
494 return err
? err
: nres
;
497 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
498 loff_t pos
, unsigned len
, unsigned copied
,
499 struct page
*page
, void *fsdata
)
501 struct inode
*inode
= mapping
->host
;
505 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
508 page_cache_release(page
);
512 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
516 for (i
= 0; i
< req
->num_pages
; i
++) {
517 struct page
*page
= req
->pages
[i
];
519 set_page_dirty_lock(page
);
524 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
525 unsigned nbytes
, int write
)
527 unsigned long user_addr
= (unsigned long) buf
;
528 unsigned offset
= user_addr
& ~PAGE_MASK
;
531 /* This doesn't work with nfsd */
535 nbytes
= min(nbytes
, (unsigned) FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
536 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
537 npages
= min(max(npages
, 1), FUSE_MAX_PAGES_PER_REQ
);
538 down_read(¤t
->mm
->mmap_sem
);
539 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, write
,
540 0, req
->pages
, NULL
);
541 up_read(¤t
->mm
->mmap_sem
);
545 req
->num_pages
= npages
;
546 req
->page_offset
= offset
;
550 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
551 size_t count
, loff_t
*ppos
, int write
)
553 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
554 struct fuse_conn
*fc
= get_fuse_conn(inode
);
555 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
558 struct fuse_req
*req
;
560 if (is_bad_inode(inode
))
563 req
= fuse_get_req(fc
);
569 size_t nbytes
= min(count
, nmax
);
570 int err
= fuse_get_user_pages(req
, buf
, nbytes
, !write
);
575 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
576 nbytes
= min(count
, nbytes
);
578 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
);
580 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
);
581 fuse_release_user_pages(req
, !write
);
582 if (req
->out
.h
.error
) {
584 res
= req
->out
.h
.error
;
586 } else if (nres
> nbytes
) {
597 fuse_put_request(fc
, req
);
598 req
= fuse_get_req(fc
);
603 fuse_put_request(fc
, req
);
606 spin_lock(&fc
->lock
);
607 if (pos
> inode
->i_size
)
608 i_size_write(inode
, pos
);
609 spin_unlock(&fc
->lock
);
613 fuse_invalidate_attr(inode
);
618 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
619 size_t count
, loff_t
*ppos
)
621 return fuse_direct_io(file
, buf
, count
, ppos
, 0);
624 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
625 size_t count
, loff_t
*ppos
)
627 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
629 /* Don't allow parallel writes to the same file */
630 mutex_lock(&inode
->i_mutex
);
631 res
= generic_write_checks(file
, ppos
, &count
, 0);
633 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
634 mutex_unlock(&inode
->i_mutex
);
638 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
640 if ((vma
->vm_flags
& VM_SHARED
)) {
641 if ((vma
->vm_flags
& VM_WRITE
))
644 vma
->vm_flags
&= ~VM_MAYWRITE
;
646 return generic_file_mmap(file
, vma
);
649 static int fuse_set_page_dirty(struct page
*page
)
651 printk("fuse_set_page_dirty: should not happen\n");
656 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
657 struct file_lock
*fl
)
665 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
666 ffl
->end
< ffl
->start
)
669 fl
->fl_start
= ffl
->start
;
670 fl
->fl_end
= ffl
->end
;
671 fl
->fl_pid
= ffl
->pid
;
677 fl
->fl_type
= ffl
->type
;
681 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
682 const struct file_lock
*fl
, int opcode
, pid_t pid
)
684 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
685 struct fuse_conn
*fc
= get_fuse_conn(inode
);
686 struct fuse_file
*ff
= file
->private_data
;
687 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
690 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
691 arg
->lk
.start
= fl
->fl_start
;
692 arg
->lk
.end
= fl
->fl_end
;
693 arg
->lk
.type
= fl
->fl_type
;
695 req
->in
.h
.opcode
= opcode
;
696 req
->in
.h
.nodeid
= get_node_id(inode
);
698 req
->in
.args
[0].size
= sizeof(*arg
);
699 req
->in
.args
[0].value
= arg
;
702 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
704 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
705 struct fuse_conn
*fc
= get_fuse_conn(inode
);
706 struct fuse_req
*req
;
707 struct fuse_lk_out outarg
;
710 req
= fuse_get_req(fc
);
714 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0);
715 req
->out
.numargs
= 1;
716 req
->out
.args
[0].size
= sizeof(outarg
);
717 req
->out
.args
[0].value
= &outarg
;
718 request_send(fc
, req
);
719 err
= req
->out
.h
.error
;
720 fuse_put_request(fc
, req
);
722 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
727 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
)
729 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
730 struct fuse_conn
*fc
= get_fuse_conn(inode
);
731 struct fuse_req
*req
;
732 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
733 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
736 /* Unlock on close is handled by the flush method */
737 if (fl
->fl_flags
& FL_CLOSE
)
740 req
= fuse_get_req(fc
);
744 fuse_lk_fill(req
, file
, fl
, opcode
, pid
);
745 request_send(fc
, req
);
746 err
= req
->out
.h
.error
;
747 /* locking is restartable */
750 fuse_put_request(fc
, req
);
754 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
756 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
757 struct fuse_conn
*fc
= get_fuse_conn(inode
);
760 if (cmd
== F_GETLK
) {
762 posix_test_lock(file
, fl
);
765 err
= fuse_getlk(file
, fl
);
768 err
= posix_lock_file_wait(file
, fl
);
770 err
= fuse_setlk(file
, fl
);
775 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
777 struct inode
*inode
= mapping
->host
;
778 struct fuse_conn
*fc
= get_fuse_conn(inode
);
779 struct fuse_req
*req
;
780 struct fuse_bmap_in inarg
;
781 struct fuse_bmap_out outarg
;
784 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
787 req
= fuse_get_req(fc
);
791 memset(&inarg
, 0, sizeof(inarg
));
793 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
794 req
->in
.h
.opcode
= FUSE_BMAP
;
795 req
->in
.h
.nodeid
= get_node_id(inode
);
797 req
->in
.args
[0].size
= sizeof(inarg
);
798 req
->in
.args
[0].value
= &inarg
;
799 req
->out
.numargs
= 1;
800 req
->out
.args
[0].size
= sizeof(outarg
);
801 req
->out
.args
[0].value
= &outarg
;
802 request_send(fc
, req
);
803 err
= req
->out
.h
.error
;
804 fuse_put_request(fc
, req
);
808 return err
? 0 : outarg
.block
;
811 static const struct file_operations fuse_file_operations
= {
812 .llseek
= generic_file_llseek
,
813 .read
= do_sync_read
,
814 .aio_read
= generic_file_aio_read
,
815 .write
= do_sync_write
,
816 .aio_write
= generic_file_aio_write
,
817 .mmap
= fuse_file_mmap
,
820 .release
= fuse_release
,
822 .lock
= fuse_file_lock
,
823 .splice_read
= generic_file_splice_read
,
826 static const struct file_operations fuse_direct_io_file_operations
= {
827 .llseek
= generic_file_llseek
,
828 .read
= fuse_direct_read
,
829 .write
= fuse_direct_write
,
832 .release
= fuse_release
,
834 .lock
= fuse_file_lock
,
835 /* no mmap and splice_read */
838 static const struct address_space_operations fuse_file_aops
= {
839 .readpage
= fuse_readpage
,
840 .write_begin
= fuse_write_begin
,
841 .write_end
= fuse_write_end
,
842 .readpages
= fuse_readpages
,
843 .set_page_dirty
= fuse_set_page_dirty
,
847 void fuse_init_file_inode(struct inode
*inode
)
849 inode
->i_fop
= &fuse_file_operations
;
850 inode
->i_data
.a_ops
= &fuse_file_aops
;