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/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");
25 static kmem_cache_t
*fuse_inode_cachep
;
26 static struct subsystem connections_subsys
;
28 struct fuse_conn_attr
{
29 struct attribute attr
;
30 ssize_t (*show
)(struct fuse_conn
*, char *);
31 ssize_t (*store
)(struct fuse_conn
*, const char *, size_t);
34 #define FUSE_SUPER_MAGIC 0x65735546
36 struct fuse_mount_data
{
41 unsigned fd_present
: 1;
42 unsigned rootmode_present
: 1;
43 unsigned user_id_present
: 1;
44 unsigned group_id_present
: 1;
49 static struct inode
*fuse_alloc_inode(struct super_block
*sb
)
52 struct fuse_inode
*fi
;
54 inode
= kmem_cache_alloc(fuse_inode_cachep
, SLAB_KERNEL
);
58 fi
= get_fuse_inode(inode
);
59 fi
->i_time
= jiffies
- 1;
62 fi
->forget_req
= fuse_request_alloc();
63 if (!fi
->forget_req
) {
64 kmem_cache_free(fuse_inode_cachep
, inode
);
71 static void fuse_destroy_inode(struct inode
*inode
)
73 struct fuse_inode
*fi
= get_fuse_inode(inode
);
75 fuse_request_free(fi
->forget_req
);
76 kmem_cache_free(fuse_inode_cachep
, inode
);
79 static void fuse_read_inode(struct inode
*inode
)
84 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
85 unsigned long nodeid
, u64 nlookup
)
87 struct fuse_forget_in
*inarg
= &req
->misc
.forget_in
;
88 inarg
->nlookup
= nlookup
;
89 req
->in
.h
.opcode
= FUSE_FORGET
;
90 req
->in
.h
.nodeid
= nodeid
;
92 req
->in
.args
[0].size
= sizeof(struct fuse_forget_in
);
93 req
->in
.args
[0].value
= inarg
;
94 request_send_noreply(fc
, req
);
97 static void fuse_clear_inode(struct inode
*inode
)
99 if (inode
->i_sb
->s_flags
& MS_ACTIVE
) {
100 struct fuse_conn
*fc
= get_fuse_conn(inode
);
101 struct fuse_inode
*fi
= get_fuse_inode(inode
);
102 fuse_send_forget(fc
, fi
->forget_req
, fi
->nodeid
, fi
->nlookup
);
103 fi
->forget_req
= NULL
;
107 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
)
109 if (S_ISREG(inode
->i_mode
) && i_size_read(inode
) != attr
->size
)
110 invalidate_inode_pages(inode
->i_mapping
);
112 inode
->i_ino
= attr
->ino
;
113 inode
->i_mode
= (inode
->i_mode
& S_IFMT
) + (attr
->mode
& 07777);
114 inode
->i_nlink
= attr
->nlink
;
115 inode
->i_uid
= attr
->uid
;
116 inode
->i_gid
= attr
->gid
;
117 i_size_write(inode
, attr
->size
);
118 inode
->i_blksize
= PAGE_CACHE_SIZE
;
119 inode
->i_blocks
= attr
->blocks
;
120 inode
->i_atime
.tv_sec
= attr
->atime
;
121 inode
->i_atime
.tv_nsec
= attr
->atimensec
;
122 inode
->i_mtime
.tv_sec
= attr
->mtime
;
123 inode
->i_mtime
.tv_nsec
= attr
->mtimensec
;
124 inode
->i_ctime
.tv_sec
= attr
->ctime
;
125 inode
->i_ctime
.tv_nsec
= attr
->ctimensec
;
128 static void fuse_init_inode(struct inode
*inode
, struct fuse_attr
*attr
)
130 inode
->i_mode
= attr
->mode
& S_IFMT
;
131 i_size_write(inode
, attr
->size
);
132 if (S_ISREG(inode
->i_mode
)) {
133 fuse_init_common(inode
);
134 fuse_init_file_inode(inode
);
135 } else if (S_ISDIR(inode
->i_mode
))
136 fuse_init_dir(inode
);
137 else if (S_ISLNK(inode
->i_mode
))
138 fuse_init_symlink(inode
);
139 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
140 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
141 fuse_init_common(inode
);
142 init_special_inode(inode
, inode
->i_mode
,
143 new_decode_dev(attr
->rdev
));
148 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
150 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
151 if (get_node_id(inode
) == nodeid
)
157 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
159 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
160 get_fuse_inode(inode
)->nodeid
= nodeid
;
164 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
165 int generation
, struct fuse_attr
*attr
)
168 struct fuse_inode
*fi
;
169 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
173 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
177 if ((inode
->i_state
& I_NEW
)) {
178 inode
->i_flags
|= S_NOATIME
|S_NOCMTIME
;
179 inode
->i_generation
= generation
;
180 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
181 fuse_init_inode(inode
, attr
);
182 unlock_new_inode(inode
);
183 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
185 /* Inode has changed type, any I/O on the old should fail */
186 make_bad_inode(inode
);
192 fi
= get_fuse_inode(inode
);
194 fuse_change_attributes(inode
, attr
);
198 static void fuse_umount_begin(struct vfsmount
*vfsmnt
, int flags
)
200 if (flags
& MNT_FORCE
)
201 fuse_abort_conn(get_fuse_conn_super(vfsmnt
->mnt_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(fc
,
211 list_entry(fc
->background
.next
,
212 struct fuse_req
, bg_entry
));
214 spin_lock(&fc
->lock
);
217 spin_unlock(&fc
->lock
);
218 up_write(&fc
->sbput_sem
);
219 /* Flush all readers on this fs */
220 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
221 wake_up_all(&fc
->waitq
);
222 kobject_del(&fc
->kobj
);
223 kobject_put(&fc
->kobj
);
226 static void convert_fuse_statfs(struct kstatfs
*stbuf
, struct fuse_kstatfs
*attr
)
228 stbuf
->f_type
= FUSE_SUPER_MAGIC
;
229 stbuf
->f_bsize
= attr
->bsize
;
230 stbuf
->f_frsize
= attr
->frsize
;
231 stbuf
->f_blocks
= attr
->blocks
;
232 stbuf
->f_bfree
= attr
->bfree
;
233 stbuf
->f_bavail
= attr
->bavail
;
234 stbuf
->f_files
= attr
->files
;
235 stbuf
->f_ffree
= attr
->ffree
;
236 stbuf
->f_namelen
= attr
->namelen
;
237 /* fsid is left zero */
240 static int fuse_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
242 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
243 struct fuse_req
*req
;
244 struct fuse_statfs_out outarg
;
247 req
= fuse_get_req(fc
);
251 memset(&outarg
, 0, sizeof(outarg
));
253 req
->in
.h
.opcode
= FUSE_STATFS
;
254 req
->out
.numargs
= 1;
255 req
->out
.args
[0].size
=
256 fc
->minor
< 4 ? FUSE_COMPAT_STATFS_SIZE
: sizeof(outarg
);
257 req
->out
.args
[0].value
= &outarg
;
258 request_send(fc
, req
);
259 err
= req
->out
.h
.error
;
261 convert_fuse_statfs(buf
, &outarg
.st
);
262 fuse_put_request(fc
, req
);
271 OPT_DEFAULT_PERMISSIONS
,
277 static match_table_t tokens
= {
279 {OPT_ROOTMODE
, "rootmode=%o"},
280 {OPT_USER_ID
, "user_id=%u"},
281 {OPT_GROUP_ID
, "group_id=%u"},
282 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
283 {OPT_ALLOW_OTHER
, "allow_other"},
284 {OPT_MAX_READ
, "max_read=%u"},
288 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
291 memset(d
, 0, sizeof(struct fuse_mount_data
));
294 while ((p
= strsep(&opt
, ",")) != NULL
) {
297 substring_t args
[MAX_OPT_ARGS
];
301 token
= match_token(p
, tokens
, args
);
304 if (match_int(&args
[0], &value
))
311 if (match_octal(&args
[0], &value
))
314 d
->rootmode_present
= 1;
318 if (match_int(&args
[0], &value
))
321 d
->user_id_present
= 1;
325 if (match_int(&args
[0], &value
))
328 d
->group_id_present
= 1;
331 case OPT_DEFAULT_PERMISSIONS
:
332 d
->flags
|= FUSE_DEFAULT_PERMISSIONS
;
335 case OPT_ALLOW_OTHER
:
336 d
->flags
|= FUSE_ALLOW_OTHER
;
340 if (match_int(&args
[0], &value
))
350 if (!d
->fd_present
|| !d
->rootmode_present
||
351 !d
->user_id_present
|| !d
->group_id_present
)
357 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
359 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
361 seq_printf(m
, ",user_id=%u", fc
->user_id
);
362 seq_printf(m
, ",group_id=%u", fc
->group_id
);
363 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
)
364 seq_puts(m
, ",default_permissions");
365 if (fc
->flags
& FUSE_ALLOW_OTHER
)
366 seq_puts(m
, ",allow_other");
367 if (fc
->max_read
!= ~0)
368 seq_printf(m
, ",max_read=%u", fc
->max_read
);
372 static void fuse_conn_release(struct kobject
*kobj
)
374 kfree(get_fuse_conn_kobj(kobj
));
377 static struct fuse_conn
*new_conn(void)
379 struct fuse_conn
*fc
;
381 fc
= kzalloc(sizeof(*fc
), GFP_KERNEL
);
383 spin_lock_init(&fc
->lock
);
384 init_waitqueue_head(&fc
->waitq
);
385 init_waitqueue_head(&fc
->blocked_waitq
);
386 INIT_LIST_HEAD(&fc
->pending
);
387 INIT_LIST_HEAD(&fc
->processing
);
388 INIT_LIST_HEAD(&fc
->io
);
389 INIT_LIST_HEAD(&fc
->background
);
390 init_rwsem(&fc
->sbput_sem
);
391 kobj_set_kset_s(fc
, connections_subsys
);
392 kobject_init(&fc
->kobj
);
393 atomic_set(&fc
->num_waiting
, 0);
394 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
395 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
402 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
404 struct fuse_attr attr
;
405 memset(&attr
, 0, sizeof(attr
));
408 attr
.ino
= FUSE_ROOT_ID
;
409 return fuse_iget(sb
, 1, 0, &attr
);
412 static struct super_operations fuse_super_operations
= {
413 .alloc_inode
= fuse_alloc_inode
,
414 .destroy_inode
= fuse_destroy_inode
,
415 .read_inode
= fuse_read_inode
,
416 .clear_inode
= fuse_clear_inode
,
417 .put_super
= fuse_put_super
,
418 .umount_begin
= fuse_umount_begin
,
419 .statfs
= fuse_statfs
,
420 .show_options
= fuse_show_options
,
423 static void process_init_reply(struct fuse_conn
*fc
, struct fuse_req
*req
)
425 struct fuse_init_out
*arg
= &req
->misc
.init_out
;
427 if (req
->out
.h
.error
|| arg
->major
!= FUSE_KERNEL_VERSION
)
430 unsigned long ra_pages
;
432 if (arg
->minor
>= 6) {
433 ra_pages
= arg
->max_readahead
/ PAGE_CACHE_SIZE
;
434 if (arg
->flags
& FUSE_ASYNC_READ
)
437 ra_pages
= fc
->max_read
/ PAGE_CACHE_SIZE
;
439 fc
->bdi
.ra_pages
= min(fc
->bdi
.ra_pages
, ra_pages
);
440 fc
->minor
= arg
->minor
;
441 fc
->max_write
= arg
->minor
< 5 ? 4096 : arg
->max_write
;
443 fuse_put_request(fc
, req
);
445 wake_up_all(&fc
->blocked_waitq
);
448 static void fuse_send_init(struct fuse_conn
*fc
, struct fuse_req
*req
)
450 struct fuse_init_in
*arg
= &req
->misc
.init_in
;
452 arg
->major
= FUSE_KERNEL_VERSION
;
453 arg
->minor
= FUSE_KERNEL_MINOR_VERSION
;
454 arg
->max_readahead
= fc
->bdi
.ra_pages
* PAGE_CACHE_SIZE
;
455 arg
->flags
|= FUSE_ASYNC_READ
;
456 req
->in
.h
.opcode
= FUSE_INIT
;
458 req
->in
.args
[0].size
= sizeof(*arg
);
459 req
->in
.args
[0].value
= arg
;
460 req
->out
.numargs
= 1;
461 /* Variable length arguement used for backward compatibility
462 with interface version < 7.5. Rest of init_out is zeroed
463 by do_get_request(), so a short reply is not a problem */
465 req
->out
.args
[0].size
= sizeof(struct fuse_init_out
);
466 req
->out
.args
[0].value
= &req
->misc
.init_out
;
467 req
->end
= process_init_reply
;
468 request_send_background(fc
, req
);
471 static unsigned long long conn_id(void)
473 /* BKL is held for ->get_sb() */
474 static unsigned long long ctr
= 1;
478 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
480 struct fuse_conn
*fc
;
482 struct fuse_mount_data d
;
484 struct dentry
*root_dentry
;
485 struct fuse_req
*init_req
;
488 if (!parse_fuse_opt((char *) data
, &d
))
491 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
492 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
493 sb
->s_magic
= FUSE_SUPER_MAGIC
;
494 sb
->s_op
= &fuse_super_operations
;
495 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
501 if (file
->f_op
!= &fuse_dev_operations
)
509 fc
->user_id
= d
.user_id
;
510 fc
->group_id
= d
.group_id
;
511 fc
->max_read
= d
.max_read
;
513 /* Used by get_root_inode() */
517 root
= get_root_inode(sb
, d
.rootmode
);
521 root_dentry
= d_alloc_root(root
);
527 init_req
= fuse_request_alloc();
531 err
= kobject_set_name(&fc
->kobj
, "%llu", conn_id());
535 err
= kobject_add(&fc
->kobj
);
539 /* Setting file->private_data can't race with other mount()
540 instances, since BKL is held for ->get_sb() */
542 if (file
->private_data
)
543 goto err_kobject_del
;
545 sb
->s_root
= root_dentry
;
548 kobject_get(&fc
->kobj
);
549 file
->private_data
= fc
;
551 * atomic_dec_and_test() in fput() provides the necessary
552 * memory barrier for file->private_data to be visible on all
557 fuse_send_init(fc
, init_req
);
562 kobject_del(&fc
->kobj
);
564 fuse_request_free(init_req
);
569 kobject_put(&fc
->kobj
);
573 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
574 int flags
, const char *dev_name
,
577 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
580 static struct file_system_type fuse_fs_type
= {
581 .owner
= THIS_MODULE
,
583 .get_sb
= fuse_get_sb
,
584 .kill_sb
= kill_anon_super
,
587 static ssize_t
fuse_conn_waiting_show(struct fuse_conn
*fc
, char *page
)
589 return sprintf(page
, "%i\n", atomic_read(&fc
->num_waiting
));
592 static ssize_t
fuse_conn_abort_store(struct fuse_conn
*fc
, const char *page
,
599 static struct fuse_conn_attr fuse_conn_waiting
=
600 __ATTR(waiting
, 0400, fuse_conn_waiting_show
, NULL
);
601 static struct fuse_conn_attr fuse_conn_abort
=
602 __ATTR(abort
, 0600, NULL
, fuse_conn_abort_store
);
604 static struct attribute
*fuse_conn_attrs
[] = {
605 &fuse_conn_waiting
.attr
,
606 &fuse_conn_abort
.attr
,
610 static ssize_t
fuse_conn_attr_show(struct kobject
*kobj
,
611 struct attribute
*attr
,
614 struct fuse_conn_attr
*fca
=
615 container_of(attr
, struct fuse_conn_attr
, attr
);
618 return fca
->show(get_fuse_conn_kobj(kobj
), page
);
623 static ssize_t
fuse_conn_attr_store(struct kobject
*kobj
,
624 struct attribute
*attr
,
625 const char *page
, size_t count
)
627 struct fuse_conn_attr
*fca
=
628 container_of(attr
, struct fuse_conn_attr
, attr
);
631 return fca
->store(get_fuse_conn_kobj(kobj
), page
, count
);
636 static struct sysfs_ops fuse_conn_sysfs_ops
= {
637 .show
= &fuse_conn_attr_show
,
638 .store
= &fuse_conn_attr_store
,
641 static struct kobj_type ktype_fuse_conn
= {
642 .release
= fuse_conn_release
,
643 .sysfs_ops
= &fuse_conn_sysfs_ops
,
644 .default_attrs
= fuse_conn_attrs
,
647 static decl_subsys(fuse
, NULL
, NULL
);
648 static decl_subsys(connections
, &ktype_fuse_conn
, NULL
);
650 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
653 struct inode
* inode
= foo
;
655 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
656 SLAB_CTOR_CONSTRUCTOR
)
657 inode_init_once(inode
);
660 static int __init
fuse_fs_init(void)
664 err
= register_filesystem(&fuse_fs_type
);
666 printk("fuse: failed to register filesystem\n");
668 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
669 sizeof(struct fuse_inode
),
670 0, SLAB_HWCACHE_ALIGN
,
671 fuse_inode_init_once
, NULL
);
672 if (!fuse_inode_cachep
) {
673 unregister_filesystem(&fuse_fs_type
);
681 static void fuse_fs_cleanup(void)
683 unregister_filesystem(&fuse_fs_type
);
684 kmem_cache_destroy(fuse_inode_cachep
);
687 static int fuse_sysfs_init(void)
691 kset_set_kset_s(&fuse_subsys
, fs_subsys
);
692 err
= subsystem_register(&fuse_subsys
);
696 kset_set_kset_s(&connections_subsys
, fuse_subsys
);
697 err
= subsystem_register(&connections_subsys
);
699 goto out_fuse_unregister
;
704 subsystem_unregister(&fuse_subsys
);
709 static void fuse_sysfs_cleanup(void)
711 subsystem_unregister(&connections_subsys
);
712 subsystem_unregister(&fuse_subsys
);
715 static int __init
fuse_init(void)
719 printk("fuse init (API version %i.%i)\n",
720 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
722 res
= fuse_fs_init();
726 res
= fuse_dev_init();
730 res
= fuse_sysfs_init();
732 goto err_dev_cleanup
;
744 static void __exit
fuse_exit(void)
746 printk(KERN_DEBUG
"fuse exit\n");
748 fuse_sysfs_cleanup();
753 module_init(fuse_init
);
754 module_exit(fuse_exit
);