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>
15 static const 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_req(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
);
34 req
->in
.args
[0].size
= sizeof(inarg
);
35 req
->in
.args
[0].value
= &inarg
;
37 req
->out
.args
[0].size
= sizeof(*outargp
);
38 req
->out
.args
[0].value
= outargp
;
39 request_send(fc
, req
);
40 err
= req
->out
.h
.error
;
41 fuse_put_request(fc
, req
);
46 struct fuse_file
*fuse_file_alloc(void)
49 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
51 ff
->release_req
= fuse_request_alloc();
52 if (!ff
->release_req
) {
60 void fuse_file_free(struct fuse_file
*ff
)
62 fuse_request_free(ff
->release_req
);
66 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
67 struct fuse_file
*ff
, struct fuse_open_out
*outarg
)
69 if (outarg
->open_flags
& FOPEN_DIRECT_IO
)
70 file
->f_op
= &fuse_direct_io_file_operations
;
71 if (!(outarg
->open_flags
& FOPEN_KEEP_CACHE
))
72 invalidate_inode_pages(inode
->i_mapping
);
74 file
->private_data
= ff
;
77 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
)
79 struct fuse_open_out outarg
;
83 /* VFS checks this, but only _after_ ->open() */
84 if (file
->f_flags
& O_DIRECT
)
87 err
= generic_file_open(inode
, file
);
91 /* If opening the root node, no lookup has been performed on
92 it, so the attributes must be refreshed */
93 if (get_node_id(inode
) == FUSE_ROOT_ID
) {
94 err
= fuse_do_getattr(inode
);
99 ff
= fuse_file_alloc();
103 err
= fuse_send_open(inode
, file
, isdir
, &outarg
);
108 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
109 fuse_finish_open(inode
, file
, ff
, &outarg
);
115 struct fuse_req
*fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
,
118 struct fuse_req
*req
= ff
->release_req
;
119 struct fuse_release_in
*inarg
= &req
->misc
.release_in
;
122 inarg
->flags
= flags
;
123 req
->in
.h
.opcode
= opcode
;
124 req
->in
.h
.nodeid
= nodeid
;
126 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
127 req
->in
.args
[0].value
= inarg
;
133 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
)
135 struct fuse_file
*ff
= file
->private_data
;
137 struct fuse_conn
*fc
= get_fuse_conn(inode
);
138 struct fuse_req
*req
;
140 req
= fuse_release_fill(ff
, get_node_id(inode
), file
->f_flags
,
141 isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
);
143 /* Hold vfsmount and dentry until release is finished */
144 req
->vfsmount
= mntget(file
->f_vfsmnt
);
145 req
->dentry
= dget(file
->f_dentry
);
146 request_send_background(fc
, req
);
149 /* Return value is ignored by VFS */
153 static int fuse_open(struct inode
*inode
, struct file
*file
)
155 return fuse_open_common(inode
, file
, 0);
158 static int fuse_release(struct inode
*inode
, struct file
*file
)
160 return fuse_release_common(inode
, file
, 0);
164 * It would be nice to scramble the ID space, so that the value of the
165 * files_struct pointer is not exposed to userspace. Symmetric crypto
166 * functions are overkill, since the inverse function doesn't need to
167 * be implemented (though it does have to exist). Is there something
170 static inline u64
fuse_lock_owner_id(fl_owner_t id
)
172 return (unsigned long) id
;
175 static int fuse_flush(struct file
*file
, fl_owner_t id
)
177 struct inode
*inode
= file
->f_dentry
->d_inode
;
178 struct fuse_conn
*fc
= get_fuse_conn(inode
);
179 struct fuse_file
*ff
= file
->private_data
;
180 struct fuse_req
*req
;
181 struct fuse_flush_in inarg
;
184 if (is_bad_inode(inode
))
190 req
= fuse_get_req(fc
);
194 memset(&inarg
, 0, sizeof(inarg
));
196 inarg
.lock_owner
= fuse_lock_owner_id(id
);
197 req
->in
.h
.opcode
= FUSE_FLUSH
;
198 req
->in
.h
.nodeid
= get_node_id(inode
);
200 req
->in
.args
[0].size
= sizeof(inarg
);
201 req
->in
.args
[0].value
= &inarg
;
203 request_send(fc
, req
);
204 err
= req
->out
.h
.error
;
205 fuse_put_request(fc
, req
);
206 if (err
== -ENOSYS
) {
213 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
216 struct inode
*inode
= de
->d_inode
;
217 struct fuse_conn
*fc
= get_fuse_conn(inode
);
218 struct fuse_file
*ff
= file
->private_data
;
219 struct fuse_req
*req
;
220 struct fuse_fsync_in inarg
;
223 if (is_bad_inode(inode
))
226 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
229 req
= fuse_get_req(fc
);
233 memset(&inarg
, 0, sizeof(inarg
));
235 inarg
.fsync_flags
= datasync
? 1 : 0;
236 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
237 req
->in
.h
.nodeid
= get_node_id(inode
);
239 req
->in
.args
[0].size
= sizeof(inarg
);
240 req
->in
.args
[0].value
= &inarg
;
241 request_send(fc
, req
);
242 err
= req
->out
.h
.error
;
243 fuse_put_request(fc
, req
);
244 if (err
== -ENOSYS
) {
254 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
256 return fuse_fsync_common(file
, de
, datasync
, 0);
259 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
260 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
)
262 struct fuse_file
*ff
= file
->private_data
;
263 struct fuse_read_in
*inarg
= &req
->misc
.read_in
;
268 req
->in
.h
.opcode
= opcode
;
269 req
->in
.h
.nodeid
= get_node_id(inode
);
271 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
272 req
->in
.args
[0].value
= inarg
;
273 req
->out
.argpages
= 1;
275 req
->out
.numargs
= 1;
276 req
->out
.args
[0].size
= count
;
279 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
280 struct inode
*inode
, loff_t pos
, size_t count
)
282 struct fuse_conn
*fc
= get_fuse_conn(inode
);
283 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
284 request_send(fc
, req
);
285 return req
->out
.args
[0].size
;
288 static int fuse_readpage(struct file
*file
, struct page
*page
)
290 struct inode
*inode
= page
->mapping
->host
;
291 struct fuse_conn
*fc
= get_fuse_conn(inode
);
292 struct fuse_req
*req
;
296 if (is_bad_inode(inode
))
299 req
= fuse_get_req(fc
);
304 req
->out
.page_zeroing
= 1;
306 req
->pages
[0] = page
;
307 fuse_send_read(req
, file
, inode
, page_offset(page
), PAGE_CACHE_SIZE
);
308 err
= req
->out
.h
.error
;
309 fuse_put_request(fc
, req
);
311 SetPageUptodate(page
);
312 fuse_invalidate_attr(inode
); /* atime changed */
318 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
322 fuse_invalidate_attr(req
->pages
[0]->mapping
->host
); /* atime changed */
324 for (i
= 0; i
< req
->num_pages
; i
++) {
325 struct page
*page
= req
->pages
[i
];
326 if (!req
->out
.h
.error
)
327 SetPageUptodate(page
);
332 fuse_put_request(fc
, req
);
335 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
,
338 struct fuse_conn
*fc
= get_fuse_conn(inode
);
339 loff_t pos
= page_offset(req
->pages
[0]);
340 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
341 req
->out
.page_zeroing
= 1;
342 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
343 if (fc
->async_read
) {
346 req
->end
= fuse_readpages_end
;
347 request_send_background(fc
, req
);
349 request_send(fc
, req
);
350 fuse_readpages_end(fc
, req
);
354 struct fuse_readpages_data
{
355 struct fuse_req
*req
;
360 static int fuse_readpages_fill(void *_data
, struct page
*page
)
362 struct fuse_readpages_data
*data
= _data
;
363 struct fuse_req
*req
= data
->req
;
364 struct inode
*inode
= data
->inode
;
365 struct fuse_conn
*fc
= get_fuse_conn(inode
);
367 if (req
->num_pages
&&
368 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
369 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
370 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
371 fuse_send_readpages(req
, data
->file
, inode
);
372 data
->req
= req
= fuse_get_req(fc
);
378 req
->pages
[req
->num_pages
] = page
;
383 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
384 struct list_head
*pages
, unsigned nr_pages
)
386 struct inode
*inode
= mapping
->host
;
387 struct fuse_conn
*fc
= get_fuse_conn(inode
);
388 struct fuse_readpages_data data
;
391 if (is_bad_inode(inode
))
396 data
.req
= fuse_get_req(fc
);
397 if (IS_ERR(data
.req
))
398 return PTR_ERR(data
.req
);
400 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
402 if (data
.req
->num_pages
)
403 fuse_send_readpages(data
.req
, file
, inode
);
405 fuse_put_request(fc
, data
.req
);
410 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
411 struct inode
*inode
, loff_t pos
, size_t count
)
413 struct fuse_conn
*fc
= get_fuse_conn(inode
);
414 struct fuse_file
*ff
= file
->private_data
;
415 struct fuse_write_in inarg
;
416 struct fuse_write_out outarg
;
418 memset(&inarg
, 0, sizeof(struct fuse_write_in
));
422 req
->in
.h
.opcode
= FUSE_WRITE
;
423 req
->in
.h
.nodeid
= get_node_id(inode
);
424 req
->in
.argpages
= 1;
426 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
427 req
->in
.args
[0].value
= &inarg
;
428 req
->in
.args
[1].size
= count
;
429 req
->out
.numargs
= 1;
430 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
431 req
->out
.args
[0].value
= &outarg
;
432 request_send(fc
, req
);
436 static int fuse_prepare_write(struct file
*file
, struct page
*page
,
437 unsigned offset
, unsigned to
)
443 static int fuse_commit_write(struct file
*file
, struct page
*page
,
444 unsigned offset
, unsigned to
)
448 unsigned count
= to
- offset
;
449 struct inode
*inode
= page
->mapping
->host
;
450 struct fuse_conn
*fc
= get_fuse_conn(inode
);
451 loff_t pos
= page_offset(page
) + offset
;
452 struct fuse_req
*req
;
454 if (is_bad_inode(inode
))
457 req
= fuse_get_req(fc
);
462 req
->pages
[0] = page
;
463 req
->page_offset
= offset
;
464 nres
= fuse_send_write(req
, file
, inode
, pos
, count
);
465 err
= req
->out
.h
.error
;
466 fuse_put_request(fc
, req
);
467 if (!err
&& nres
!= count
)
471 if (pos
> i_size_read(inode
))
472 i_size_write(inode
, pos
);
474 if (offset
== 0 && to
== PAGE_CACHE_SIZE
) {
475 clear_page_dirty(page
);
476 SetPageUptodate(page
);
479 fuse_invalidate_attr(inode
);
483 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
487 for (i
= 0; i
< req
->num_pages
; i
++) {
488 struct page
*page
= req
->pages
[i
];
490 set_page_dirty_lock(page
);
495 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
496 unsigned nbytes
, int write
)
498 unsigned long user_addr
= (unsigned long) buf
;
499 unsigned offset
= user_addr
& ~PAGE_MASK
;
502 /* This doesn't work with nfsd */
506 nbytes
= min(nbytes
, (unsigned) FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
507 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
508 npages
= min(max(npages
, 1), FUSE_MAX_PAGES_PER_REQ
);
509 down_read(¤t
->mm
->mmap_sem
);
510 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, write
,
511 0, req
->pages
, NULL
);
512 up_read(¤t
->mm
->mmap_sem
);
516 req
->num_pages
= npages
;
517 req
->page_offset
= offset
;
521 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
522 size_t count
, loff_t
*ppos
, int write
)
524 struct inode
*inode
= file
->f_dentry
->d_inode
;
525 struct fuse_conn
*fc
= get_fuse_conn(inode
);
526 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
529 struct fuse_req
*req
;
531 if (is_bad_inode(inode
))
534 req
= fuse_get_req(fc
);
540 size_t nbytes
= min(count
, nmax
);
541 int err
= fuse_get_user_pages(req
, buf
, nbytes
, !write
);
546 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
547 nbytes
= min(count
, nbytes
);
549 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
);
551 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
);
552 fuse_release_user_pages(req
, !write
);
553 if (req
->out
.h
.error
) {
555 res
= req
->out
.h
.error
;
557 } else if (nres
> nbytes
) {
568 fuse_put_request(fc
, req
);
569 req
= fuse_get_req(fc
);
574 fuse_put_request(fc
, req
);
576 if (write
&& pos
> i_size_read(inode
))
577 i_size_write(inode
, pos
);
580 fuse_invalidate_attr(inode
);
585 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
586 size_t count
, loff_t
*ppos
)
588 return fuse_direct_io(file
, buf
, count
, ppos
, 0);
591 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
592 size_t count
, loff_t
*ppos
)
594 struct inode
*inode
= file
->f_dentry
->d_inode
;
596 /* Don't allow parallel writes to the same file */
597 mutex_lock(&inode
->i_mutex
);
598 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
599 mutex_unlock(&inode
->i_mutex
);
603 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
605 if ((vma
->vm_flags
& VM_SHARED
)) {
606 if ((vma
->vm_flags
& VM_WRITE
))
609 vma
->vm_flags
&= ~VM_MAYWRITE
;
611 return generic_file_mmap(file
, vma
);
614 static int fuse_set_page_dirty(struct page
*page
)
616 printk("fuse_set_page_dirty: should not happen\n");
621 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
622 struct file_lock
*fl
)
630 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
631 ffl
->end
< ffl
->start
)
634 fl
->fl_start
= ffl
->start
;
635 fl
->fl_end
= ffl
->end
;
636 fl
->fl_pid
= ffl
->pid
;
642 fl
->fl_type
= ffl
->type
;
646 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
647 const struct file_lock
*fl
, int opcode
, pid_t pid
)
649 struct inode
*inode
= file
->f_dentry
->d_inode
;
650 struct fuse_file
*ff
= file
->private_data
;
651 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
654 arg
->owner
= fuse_lock_owner_id(fl
->fl_owner
);
655 arg
->lk
.start
= fl
->fl_start
;
656 arg
->lk
.end
= fl
->fl_end
;
657 arg
->lk
.type
= fl
->fl_type
;
659 req
->in
.h
.opcode
= opcode
;
660 req
->in
.h
.nodeid
= get_node_id(inode
);
662 req
->in
.args
[0].size
= sizeof(*arg
);
663 req
->in
.args
[0].value
= arg
;
666 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
668 struct inode
*inode
= file
->f_dentry
->d_inode
;
669 struct fuse_conn
*fc
= get_fuse_conn(inode
);
670 struct fuse_req
*req
;
671 struct fuse_lk_out outarg
;
674 req
= fuse_get_req(fc
);
678 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0);
679 req
->out
.numargs
= 1;
680 req
->out
.args
[0].size
= sizeof(outarg
);
681 req
->out
.args
[0].value
= &outarg
;
682 request_send(fc
, req
);
683 err
= req
->out
.h
.error
;
684 fuse_put_request(fc
, req
);
686 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
691 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
)
693 struct inode
*inode
= file
->f_dentry
->d_inode
;
694 struct fuse_conn
*fc
= get_fuse_conn(inode
);
695 struct fuse_req
*req
;
696 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
697 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
700 /* Unlock on close is handled by the flush method */
701 if (fl
->fl_flags
& FL_CLOSE
)
704 req
= fuse_get_req(fc
);
708 fuse_lk_fill(req
, file
, fl
, opcode
, pid
);
709 request_send(fc
, req
);
710 err
= req
->out
.h
.error
;
711 fuse_put_request(fc
, req
);
715 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
717 struct inode
*inode
= file
->f_dentry
->d_inode
;
718 struct fuse_conn
*fc
= get_fuse_conn(inode
);
721 if (cmd
== F_GETLK
) {
723 if (!posix_test_lock(file
, fl
, fl
))
724 fl
->fl_type
= F_UNLCK
;
727 err
= fuse_getlk(file
, fl
);
730 err
= posix_lock_file_wait(file
, fl
);
732 err
= fuse_setlk(file
, fl
);
737 static const struct file_operations fuse_file_operations
= {
738 .llseek
= generic_file_llseek
,
739 .read
= generic_file_read
,
740 .write
= generic_file_write
,
741 .mmap
= fuse_file_mmap
,
744 .release
= fuse_release
,
746 .lock
= fuse_file_lock
,
747 .sendfile
= generic_file_sendfile
,
750 static const struct file_operations fuse_direct_io_file_operations
= {
751 .llseek
= generic_file_llseek
,
752 .read
= fuse_direct_read
,
753 .write
= fuse_direct_write
,
756 .release
= fuse_release
,
758 .lock
= fuse_file_lock
,
759 /* no mmap and sendfile */
762 static struct address_space_operations fuse_file_aops
= {
763 .readpage
= fuse_readpage
,
764 .prepare_write
= fuse_prepare_write
,
765 .commit_write
= fuse_commit_write
,
766 .readpages
= fuse_readpages
,
767 .set_page_dirty
= fuse_set_page_dirty
,
770 void fuse_init_file_inode(struct inode
*inode
)
772 inode
->i_fop
= &fuse_file_operations
;
773 inode
->i_data
.a_ops
= &fuse_file_aops
;