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/file.h>
14 #include <linux/mount.h>
15 #include <linux/seq_file.h>
16 #include <linux/init.h>
17 #include <linux/module.h>
18 #include <linux/parser.h>
19 #include <linux/statfs.h>
21 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22 MODULE_DESCRIPTION("Filesystem in Userspace");
23 MODULE_LICENSE("GPL");
26 static kmem_cache_t
*fuse_inode_cachep
;
27 static struct subsystem connections_subsys
;
29 struct fuse_conn_attr
{
30 struct attribute attr
;
31 ssize_t (*show
)(struct fuse_conn
*, char *);
32 ssize_t (*store
)(struct fuse_conn
*, const char *, size_t);
35 #define FUSE_SUPER_MAGIC 0x65735546
37 struct fuse_mount_data
{
42 unsigned fd_present
: 1;
43 unsigned rootmode_present
: 1;
44 unsigned user_id_present
: 1;
45 unsigned group_id_present
: 1;
50 static struct inode
*fuse_alloc_inode(struct super_block
*sb
)
53 struct fuse_inode
*fi
;
55 inode
= kmem_cache_alloc(fuse_inode_cachep
, SLAB_KERNEL
);
59 fi
= get_fuse_inode(inode
);
60 fi
->i_time
= jiffies
- 1;
63 fi
->forget_req
= fuse_request_alloc();
64 if (!fi
->forget_req
) {
65 kmem_cache_free(fuse_inode_cachep
, inode
);
72 static void fuse_destroy_inode(struct inode
*inode
)
74 struct fuse_inode
*fi
= get_fuse_inode(inode
);
76 fuse_request_free(fi
->forget_req
);
77 kmem_cache_free(fuse_inode_cachep
, inode
);
80 static void fuse_read_inode(struct inode
*inode
)
85 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
86 unsigned long nodeid
, u64 nlookup
)
88 struct fuse_forget_in
*inarg
= &req
->misc
.forget_in
;
89 inarg
->nlookup
= nlookup
;
90 req
->in
.h
.opcode
= FUSE_FORGET
;
91 req
->in
.h
.nodeid
= nodeid
;
93 req
->in
.args
[0].size
= sizeof(struct fuse_forget_in
);
94 req
->in
.args
[0].value
= inarg
;
95 request_send_noreply(fc
, req
);
98 static void fuse_clear_inode(struct inode
*inode
)
100 if (inode
->i_sb
->s_flags
& MS_ACTIVE
) {
101 struct fuse_conn
*fc
= get_fuse_conn(inode
);
102 struct fuse_inode
*fi
= get_fuse_inode(inode
);
103 fuse_send_forget(fc
, fi
->forget_req
, fi
->nodeid
, fi
->nlookup
);
104 fi
->forget_req
= NULL
;
108 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
)
110 if (S_ISREG(inode
->i_mode
) && i_size_read(inode
) != attr
->size
)
111 invalidate_inode_pages(inode
->i_mapping
);
113 inode
->i_ino
= attr
->ino
;
114 inode
->i_mode
= (inode
->i_mode
& S_IFMT
) + (attr
->mode
& 07777);
115 inode
->i_nlink
= attr
->nlink
;
116 inode
->i_uid
= attr
->uid
;
117 inode
->i_gid
= attr
->gid
;
118 i_size_write(inode
, attr
->size
);
119 inode
->i_blksize
= PAGE_CACHE_SIZE
;
120 inode
->i_blocks
= attr
->blocks
;
121 inode
->i_atime
.tv_sec
= attr
->atime
;
122 inode
->i_atime
.tv_nsec
= attr
->atimensec
;
123 inode
->i_mtime
.tv_sec
= attr
->mtime
;
124 inode
->i_mtime
.tv_nsec
= attr
->mtimensec
;
125 inode
->i_ctime
.tv_sec
= attr
->ctime
;
126 inode
->i_ctime
.tv_nsec
= attr
->ctimensec
;
129 static void fuse_init_inode(struct inode
*inode
, struct fuse_attr
*attr
)
131 inode
->i_mode
= attr
->mode
& S_IFMT
;
132 i_size_write(inode
, attr
->size
);
133 if (S_ISREG(inode
->i_mode
)) {
134 fuse_init_common(inode
);
135 fuse_init_file_inode(inode
);
136 } else if (S_ISDIR(inode
->i_mode
))
137 fuse_init_dir(inode
);
138 else if (S_ISLNK(inode
->i_mode
))
139 fuse_init_symlink(inode
);
140 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
141 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
142 fuse_init_common(inode
);
143 init_special_inode(inode
, inode
->i_mode
,
144 new_decode_dev(attr
->rdev
));
149 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
151 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
152 if (get_node_id(inode
) == nodeid
)
158 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
160 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
161 get_fuse_inode(inode
)->nodeid
= nodeid
;
165 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
166 int generation
, struct fuse_attr
*attr
)
169 struct fuse_inode
*fi
;
170 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
174 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
178 if ((inode
->i_state
& I_NEW
)) {
179 inode
->i_flags
|= S_NOATIME
|S_NOCMTIME
;
180 inode
->i_generation
= generation
;
181 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
182 fuse_init_inode(inode
, attr
);
183 unlock_new_inode(inode
);
184 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
186 /* Inode has changed type, any I/O on the old should fail */
187 make_bad_inode(inode
);
193 fi
= get_fuse_inode(inode
);
195 fuse_change_attributes(inode
, attr
);
199 static void fuse_umount_begin(struct super_block
*sb
)
201 fuse_abort_conn(get_fuse_conn_super(sb
));
204 static void fuse_put_super(struct super_block
*sb
)
206 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
208 down_write(&fc
->sbput_sem
);
209 while (!list_empty(&fc
->background
))
210 fuse_release_background(list_entry(fc
->background
.next
,
211 struct fuse_req
, bg_entry
));
213 spin_lock(&fuse_lock
);
216 spin_unlock(&fuse_lock
);
217 up_write(&fc
->sbput_sem
);
218 /* Flush all readers on this fs */
219 wake_up_all(&fc
->waitq
);
220 kobject_del(&fc
->kobj
);
221 kobject_put(&fc
->kobj
);
224 static void convert_fuse_statfs(struct kstatfs
*stbuf
, struct fuse_kstatfs
*attr
)
226 stbuf
->f_type
= FUSE_SUPER_MAGIC
;
227 stbuf
->f_bsize
= attr
->bsize
;
228 stbuf
->f_frsize
= attr
->frsize
;
229 stbuf
->f_blocks
= attr
->blocks
;
230 stbuf
->f_bfree
= attr
->bfree
;
231 stbuf
->f_bavail
= attr
->bavail
;
232 stbuf
->f_files
= attr
->files
;
233 stbuf
->f_ffree
= attr
->ffree
;
234 stbuf
->f_namelen
= attr
->namelen
;
235 /* fsid is left zero */
238 static int fuse_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
240 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
241 struct fuse_req
*req
;
242 struct fuse_statfs_out outarg
;
245 req
= fuse_get_request(fc
);
249 memset(&outarg
, 0, sizeof(outarg
));
251 req
->in
.h
.opcode
= FUSE_STATFS
;
252 req
->out
.numargs
= 1;
253 req
->out
.args
[0].size
=
254 fc
->minor
< 4 ? FUSE_COMPAT_STATFS_SIZE
: sizeof(outarg
);
255 req
->out
.args
[0].value
= &outarg
;
256 request_send(fc
, req
);
257 err
= req
->out
.h
.error
;
259 convert_fuse_statfs(buf
, &outarg
.st
);
260 fuse_put_request(fc
, req
);
269 OPT_DEFAULT_PERMISSIONS
,
275 static match_table_t tokens
= {
277 {OPT_ROOTMODE
, "rootmode=%o"},
278 {OPT_USER_ID
, "user_id=%u"},
279 {OPT_GROUP_ID
, "group_id=%u"},
280 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
281 {OPT_ALLOW_OTHER
, "allow_other"},
282 {OPT_MAX_READ
, "max_read=%u"},
286 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
289 memset(d
, 0, sizeof(struct fuse_mount_data
));
292 while ((p
= strsep(&opt
, ",")) != NULL
) {
295 substring_t args
[MAX_OPT_ARGS
];
299 token
= match_token(p
, tokens
, args
);
302 if (match_int(&args
[0], &value
))
309 if (match_octal(&args
[0], &value
))
312 d
->rootmode_present
= 1;
316 if (match_int(&args
[0], &value
))
319 d
->user_id_present
= 1;
323 if (match_int(&args
[0], &value
))
326 d
->group_id_present
= 1;
329 case OPT_DEFAULT_PERMISSIONS
:
330 d
->flags
|= FUSE_DEFAULT_PERMISSIONS
;
333 case OPT_ALLOW_OTHER
:
334 d
->flags
|= FUSE_ALLOW_OTHER
;
338 if (match_int(&args
[0], &value
))
348 if (!d
->fd_present
|| !d
->rootmode_present
||
349 !d
->user_id_present
|| !d
->group_id_present
)
355 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
357 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
359 seq_printf(m
, ",user_id=%u", fc
->user_id
);
360 seq_printf(m
, ",group_id=%u", fc
->group_id
);
361 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
)
362 seq_puts(m
, ",default_permissions");
363 if (fc
->flags
& FUSE_ALLOW_OTHER
)
364 seq_puts(m
, ",allow_other");
365 if (fc
->max_read
!= ~0)
366 seq_printf(m
, ",max_read=%u", fc
->max_read
);
370 static void fuse_conn_release(struct kobject
*kobj
)
372 struct fuse_conn
*fc
= get_fuse_conn_kobj(kobj
);
374 while (!list_empty(&fc
->unused_list
)) {
375 struct fuse_req
*req
;
376 req
= list_entry(fc
->unused_list
.next
, struct fuse_req
, list
);
377 list_del(&req
->list
);
378 fuse_request_free(req
);
383 static struct fuse_conn
*new_conn(void)
385 struct fuse_conn
*fc
;
387 fc
= kzalloc(sizeof(*fc
), GFP_KERNEL
);
390 init_waitqueue_head(&fc
->waitq
);
391 INIT_LIST_HEAD(&fc
->pending
);
392 INIT_LIST_HEAD(&fc
->processing
);
393 INIT_LIST_HEAD(&fc
->io
);
394 INIT_LIST_HEAD(&fc
->unused_list
);
395 INIT_LIST_HEAD(&fc
->background
);
396 sema_init(&fc
->outstanding_sem
, 1); /* One for INIT */
397 init_rwsem(&fc
->sbput_sem
);
398 kobj_set_kset_s(fc
, connections_subsys
);
399 kobject_init(&fc
->kobj
);
400 atomic_set(&fc
->num_waiting
, 0);
401 for (i
= 0; i
< FUSE_MAX_OUTSTANDING
; i
++) {
402 struct fuse_req
*req
= fuse_request_alloc();
404 kobject_put(&fc
->kobj
);
407 list_add(&req
->list
, &fc
->unused_list
);
409 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
410 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
416 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
418 struct fuse_conn
*fc
;
422 if (file
->f_op
!= &fuse_dev_operations
)
430 spin_lock(&fuse_lock
);
432 if (file
->private_data
)
435 kobject_get(&fc
->kobj
);
436 file
->private_data
= fc
;
437 spin_unlock(&fuse_lock
);
441 spin_unlock(&fuse_lock
);
442 kobject_put(&fc
->kobj
);
447 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
449 struct fuse_attr attr
;
450 memset(&attr
, 0, sizeof(attr
));
453 attr
.ino
= FUSE_ROOT_ID
;
454 return fuse_iget(sb
, 1, 0, &attr
);
457 static struct super_operations fuse_super_operations
= {
458 .alloc_inode
= fuse_alloc_inode
,
459 .destroy_inode
= fuse_destroy_inode
,
460 .read_inode
= fuse_read_inode
,
461 .clear_inode
= fuse_clear_inode
,
462 .put_super
= fuse_put_super
,
463 .umount_begin
= fuse_umount_begin
,
464 .statfs
= fuse_statfs
,
465 .show_options
= fuse_show_options
,
468 static void process_init_reply(struct fuse_conn
*fc
, struct fuse_req
*req
)
471 struct fuse_init_out
*arg
= &req
->misc
.init_out
;
473 if (req
->out
.h
.error
|| arg
->major
!= FUSE_KERNEL_VERSION
)
476 unsigned long ra_pages
;
478 if (arg
->minor
>= 6) {
479 ra_pages
= arg
->max_readahead
/ PAGE_CACHE_SIZE
;
480 if (arg
->flags
& FUSE_ASYNC_READ
)
483 ra_pages
= fc
->max_read
/ PAGE_CACHE_SIZE
;
485 fc
->bdi
.ra_pages
= min(fc
->bdi
.ra_pages
, ra_pages
);
486 fc
->minor
= arg
->minor
;
487 fc
->max_write
= arg
->minor
< 5 ? 4096 : arg
->max_write
;
490 /* After INIT reply is received other requests can go
491 out. So do (FUSE_MAX_OUTSTANDING - 1) number of
492 up()s on outstanding_sem. The last up() is done in
493 fuse_putback_request() */
494 for (i
= 1; i
< FUSE_MAX_OUTSTANDING
; i
++)
495 up(&fc
->outstanding_sem
);
497 fuse_put_request(fc
, req
);
500 static void fuse_send_init(struct fuse_conn
*fc
)
502 /* This is called from fuse_read_super() so there's guaranteed
503 to be exactly one request available */
504 struct fuse_req
*req
= fuse_get_request(fc
);
505 struct fuse_init_in
*arg
= &req
->misc
.init_in
;
507 arg
->major
= FUSE_KERNEL_VERSION
;
508 arg
->minor
= FUSE_KERNEL_MINOR_VERSION
;
509 arg
->max_readahead
= fc
->bdi
.ra_pages
* PAGE_CACHE_SIZE
;
510 arg
->flags
|= FUSE_ASYNC_READ
;
511 req
->in
.h
.opcode
= FUSE_INIT
;
513 req
->in
.args
[0].size
= sizeof(*arg
);
514 req
->in
.args
[0].value
= arg
;
515 req
->out
.numargs
= 1;
516 /* Variable length arguement used for backward compatibility
517 with interface version < 7.5. Rest of init_out is zeroed
518 by do_get_request(), so a short reply is not a problem */
520 req
->out
.args
[0].size
= sizeof(struct fuse_init_out
);
521 req
->out
.args
[0].value
= &req
->misc
.init_out
;
522 req
->end
= process_init_reply
;
523 request_send_background(fc
, req
);
526 static unsigned long long conn_id(void)
528 static unsigned long long ctr
= 1;
529 unsigned long long val
;
530 spin_lock(&fuse_lock
);
532 spin_unlock(&fuse_lock
);
536 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
538 struct fuse_conn
*fc
;
540 struct fuse_mount_data d
;
542 struct dentry
*root_dentry
;
545 if (!parse_fuse_opt((char *) data
, &d
))
548 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
549 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
550 sb
->s_magic
= FUSE_SUPER_MAGIC
;
551 sb
->s_op
= &fuse_super_operations
;
552 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
558 fc
= get_conn(file
, sb
);
564 fc
->user_id
= d
.user_id
;
565 fc
->group_id
= d
.group_id
;
566 fc
->max_read
= d
.max_read
;
568 /* Used by get_root_inode() */
572 root
= get_root_inode(sb
, d
.rootmode
);
576 root_dentry
= d_alloc_root(root
);
582 err
= kobject_set_name(&fc
->kobj
, "%llu", conn_id());
586 err
= kobject_add(&fc
->kobj
);
590 sb
->s_root
= root_dentry
;
591 spin_lock(&fuse_lock
);
594 spin_unlock(&fuse_lock
);
603 kobject_put(&fc
->kobj
);
607 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
608 int flags
, const char *dev_name
,
611 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
614 static struct file_system_type fuse_fs_type
= {
615 .owner
= THIS_MODULE
,
617 .get_sb
= fuse_get_sb
,
618 .kill_sb
= kill_anon_super
,
621 static ssize_t
fuse_conn_waiting_show(struct fuse_conn
*fc
, char *page
)
623 return sprintf(page
, "%i\n", atomic_read(&fc
->num_waiting
));
626 static ssize_t
fuse_conn_abort_store(struct fuse_conn
*fc
, const char *page
,
633 static struct fuse_conn_attr fuse_conn_waiting
=
634 __ATTR(waiting
, 0400, fuse_conn_waiting_show
, NULL
);
635 static struct fuse_conn_attr fuse_conn_abort
=
636 __ATTR(abort
, 0600, NULL
, fuse_conn_abort_store
);
638 static struct attribute
*fuse_conn_attrs
[] = {
639 &fuse_conn_waiting
.attr
,
640 &fuse_conn_abort
.attr
,
644 static ssize_t
fuse_conn_attr_show(struct kobject
*kobj
,
645 struct attribute
*attr
,
648 struct fuse_conn_attr
*fca
=
649 container_of(attr
, struct fuse_conn_attr
, attr
);
652 return fca
->show(get_fuse_conn_kobj(kobj
), page
);
657 static ssize_t
fuse_conn_attr_store(struct kobject
*kobj
,
658 struct attribute
*attr
,
659 const char *page
, size_t count
)
661 struct fuse_conn_attr
*fca
=
662 container_of(attr
, struct fuse_conn_attr
, attr
);
665 return fca
->store(get_fuse_conn_kobj(kobj
), page
, count
);
670 static struct sysfs_ops fuse_conn_sysfs_ops
= {
671 .show
= &fuse_conn_attr_show
,
672 .store
= &fuse_conn_attr_store
,
675 static struct kobj_type ktype_fuse_conn
= {
676 .release
= fuse_conn_release
,
677 .sysfs_ops
= &fuse_conn_sysfs_ops
,
678 .default_attrs
= fuse_conn_attrs
,
681 static decl_subsys(fuse
, NULL
, NULL
);
682 static decl_subsys(connections
, &ktype_fuse_conn
, NULL
);
684 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
687 struct inode
* inode
= foo
;
689 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
690 SLAB_CTOR_CONSTRUCTOR
)
691 inode_init_once(inode
);
694 static int __init
fuse_fs_init(void)
698 err
= register_filesystem(&fuse_fs_type
);
700 printk("fuse: failed to register filesystem\n");
702 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
703 sizeof(struct fuse_inode
),
704 0, SLAB_HWCACHE_ALIGN
,
705 fuse_inode_init_once
, NULL
);
706 if (!fuse_inode_cachep
) {
707 unregister_filesystem(&fuse_fs_type
);
715 static void fuse_fs_cleanup(void)
717 unregister_filesystem(&fuse_fs_type
);
718 kmem_cache_destroy(fuse_inode_cachep
);
721 static int fuse_sysfs_init(void)
725 kset_set_kset_s(&fuse_subsys
, fs_subsys
);
726 err
= subsystem_register(&fuse_subsys
);
730 kset_set_kset_s(&connections_subsys
, fuse_subsys
);
731 err
= subsystem_register(&connections_subsys
);
733 goto out_fuse_unregister
;
738 subsystem_unregister(&fuse_subsys
);
743 static void fuse_sysfs_cleanup(void)
745 subsystem_unregister(&connections_subsys
);
746 subsystem_unregister(&fuse_subsys
);
749 static int __init
fuse_init(void)
753 printk("fuse init (API version %i.%i)\n",
754 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
756 spin_lock_init(&fuse_lock
);
757 res
= fuse_fs_init();
761 res
= fuse_dev_init();
765 res
= fuse_sysfs_init();
767 goto err_dev_cleanup
;
779 static void __exit
fuse_exit(void)
781 printk(KERN_DEBUG
"fuse exit\n");
783 fuse_sysfs_cleanup();
788 module_init(fuse_init
);
789 module_exit(fuse_exit
);