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/seq_file.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/parser.h>
18 #include <linux/statfs.h>
20 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
21 MODULE_DESCRIPTION("Filesystem in Userspace");
22 MODULE_LICENSE("GPL");
24 static kmem_cache_t *fuse_inode_cachep;
25 static struct subsystem connections_subsys;
27 struct fuse_conn_attr {
28 struct attribute attr;
29 ssize_t (*show)(struct fuse_conn *, char *);
30 ssize_t (*store)(struct fuse_conn *, const char *, size_t);
33 #define FUSE_SUPER_MAGIC 0x65735546
35 struct fuse_mount_data {
40 unsigned fd_present : 1;
41 unsigned rootmode_present : 1;
42 unsigned user_id_present : 1;
43 unsigned group_id_present : 1;
48 static struct inode *fuse_alloc_inode(struct super_block *sb)
51 struct fuse_inode *fi;
53 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
57 fi = get_fuse_inode(inode);
58 fi->i_time = jiffies - 1;
61 fi->forget_req = fuse_request_alloc();
62 if (!fi->forget_req) {
63 kmem_cache_free(fuse_inode_cachep, inode);
70 static void fuse_destroy_inode(struct inode *inode)
72 struct fuse_inode *fi = get_fuse_inode(inode);
74 fuse_request_free(fi->forget_req);
75 kmem_cache_free(fuse_inode_cachep, inode);
78 static void fuse_read_inode(struct inode *inode)
83 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
84 unsigned long nodeid, u64 nlookup)
86 struct fuse_forget_in *inarg = &req->misc.forget_in;
87 inarg->nlookup = nlookup;
88 req->in.h.opcode = FUSE_FORGET;
89 req->in.h.nodeid = nodeid;
91 req->in.args[0].size = sizeof(struct fuse_forget_in);
92 req->in.args[0].value = inarg;
93 request_send_noreply(fc, req);
96 static void fuse_clear_inode(struct inode *inode)
98 if (inode->i_sb->s_flags & MS_ACTIVE) {
99 struct fuse_conn *fc = get_fuse_conn(inode);
100 struct fuse_inode *fi = get_fuse_inode(inode);
101 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
102 fi->forget_req = NULL;
106 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
108 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
109 invalidate_inode_pages(inode->i_mapping);
111 inode->i_ino = attr->ino;
112 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
113 inode->i_nlink = attr->nlink;
114 inode->i_uid = attr->uid;
115 inode->i_gid = attr->gid;
116 i_size_write(inode, attr->size);
117 inode->i_blksize = PAGE_CACHE_SIZE;
118 inode->i_blocks = attr->blocks;
119 inode->i_atime.tv_sec = attr->atime;
120 inode->i_atime.tv_nsec = attr->atimensec;
121 inode->i_mtime.tv_sec = attr->mtime;
122 inode->i_mtime.tv_nsec = attr->mtimensec;
123 inode->i_ctime.tv_sec = attr->ctime;
124 inode->i_ctime.tv_nsec = attr->ctimensec;
127 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
129 inode->i_mode = attr->mode & S_IFMT;
130 i_size_write(inode, attr->size);
131 if (S_ISREG(inode->i_mode)) {
132 fuse_init_common(inode);
133 fuse_init_file_inode(inode);
134 } else if (S_ISDIR(inode->i_mode))
135 fuse_init_dir(inode);
136 else if (S_ISLNK(inode->i_mode))
137 fuse_init_symlink(inode);
138 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
139 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
140 fuse_init_common(inode);
141 init_special_inode(inode, inode->i_mode,
142 new_decode_dev(attr->rdev));
147 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
149 unsigned long nodeid = *(unsigned long *) _nodeidp;
150 if (get_node_id(inode) == nodeid)
156 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
158 unsigned long nodeid = *(unsigned long *) _nodeidp;
159 get_fuse_inode(inode)->nodeid = nodeid;
163 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
164 int generation, struct fuse_attr *attr)
167 struct fuse_inode *fi;
168 struct fuse_conn *fc = get_fuse_conn_super(sb);
172 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
176 if ((inode->i_state & I_NEW)) {
177 inode->i_flags |= S_NOATIME|S_NOCMTIME;
178 inode->i_generation = generation;
179 inode->i_data.backing_dev_info = &fc->bdi;
180 fuse_init_inode(inode, attr);
181 unlock_new_inode(inode);
182 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
184 /* Inode has changed type, any I/O on the old should fail */
185 make_bad_inode(inode);
191 fi = get_fuse_inode(inode);
193 fuse_change_attributes(inode, attr);
197 static void fuse_umount_begin(struct super_block *sb)
199 fuse_abort_conn(get_fuse_conn_super(sb));
202 static void fuse_put_super(struct super_block *sb)
204 struct fuse_conn *fc = get_fuse_conn_super(sb);
206 spin_lock(&fc->lock);
209 spin_unlock(&fc->lock);
210 /* Flush all readers on this fs */
211 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
212 wake_up_all(&fc->waitq);
213 wake_up_all(&fc->blocked_waitq);
214 kobject_del(&fc->kobj);
215 kobject_put(&fc->kobj);
218 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
220 stbuf->f_type = FUSE_SUPER_MAGIC;
221 stbuf->f_bsize = attr->bsize;
222 stbuf->f_frsize = attr->frsize;
223 stbuf->f_blocks = attr->blocks;
224 stbuf->f_bfree = attr->bfree;
225 stbuf->f_bavail = attr->bavail;
226 stbuf->f_files = attr->files;
227 stbuf->f_ffree = attr->ffree;
228 stbuf->f_namelen = attr->namelen;
229 /* fsid is left zero */
232 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf)
234 struct super_block *sb = dentry->d_sb;
235 struct fuse_conn *fc = get_fuse_conn_super(sb);
236 struct fuse_req *req;
237 struct fuse_statfs_out outarg;
240 req = fuse_get_req(fc);
244 memset(&outarg, 0, sizeof(outarg));
246 req->in.h.opcode = FUSE_STATFS;
247 req->out.numargs = 1;
248 req->out.args[0].size =
249 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
250 req->out.args[0].value = &outarg;
251 request_send(fc, req);
252 err = req->out.h.error;
254 convert_fuse_statfs(buf, &outarg.st);
255 fuse_put_request(fc, req);
264 OPT_DEFAULT_PERMISSIONS,
270 static match_table_t tokens = {
272 {OPT_ROOTMODE, "rootmode=%o"},
273 {OPT_USER_ID, "user_id=%u"},
274 {OPT_GROUP_ID, "group_id=%u"},
275 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
276 {OPT_ALLOW_OTHER, "allow_other"},
277 {OPT_MAX_READ, "max_read=%u"},
281 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
284 memset(d, 0, sizeof(struct fuse_mount_data));
287 while ((p = strsep(&opt, ",")) != NULL) {
290 substring_t args[MAX_OPT_ARGS];
294 token = match_token(p, tokens, args);
297 if (match_int(&args[0], &value))
304 if (match_octal(&args[0], &value))
307 d->rootmode_present = 1;
311 if (match_int(&args[0], &value))
314 d->user_id_present = 1;
318 if (match_int(&args[0], &value))
321 d->group_id_present = 1;
324 case OPT_DEFAULT_PERMISSIONS:
325 d->flags |= FUSE_DEFAULT_PERMISSIONS;
328 case OPT_ALLOW_OTHER:
329 d->flags |= FUSE_ALLOW_OTHER;
333 if (match_int(&args[0], &value))
343 if (!d->fd_present || !d->rootmode_present ||
344 !d->user_id_present || !d->group_id_present)
350 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
352 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
354 seq_printf(m, ",user_id=%u", fc->user_id);
355 seq_printf(m, ",group_id=%u", fc->group_id);
356 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
357 seq_puts(m, ",default_permissions");
358 if (fc->flags & FUSE_ALLOW_OTHER)
359 seq_puts(m, ",allow_other");
360 if (fc->max_read != ~0)
361 seq_printf(m, ",max_read=%u", fc->max_read);
365 static void fuse_conn_release(struct kobject *kobj)
367 kfree(get_fuse_conn_kobj(kobj));
370 static struct fuse_conn *new_conn(void)
372 struct fuse_conn *fc;
374 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
376 spin_lock_init(&fc->lock);
377 init_waitqueue_head(&fc->waitq);
378 init_waitqueue_head(&fc->blocked_waitq);
379 INIT_LIST_HEAD(&fc->pending);
380 INIT_LIST_HEAD(&fc->processing);
381 INIT_LIST_HEAD(&fc->io);
382 kobj_set_kset_s(fc, connections_subsys);
383 kobject_init(&fc->kobj);
384 atomic_set(&fc->num_waiting, 0);
385 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
386 fc->bdi.unplug_io_fn = default_unplug_io_fn;
393 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
395 struct fuse_attr attr;
396 memset(&attr, 0, sizeof(attr));
399 attr.ino = FUSE_ROOT_ID;
400 return fuse_iget(sb, 1, 0, &attr);
403 static struct super_operations fuse_super_operations = {
404 .alloc_inode = fuse_alloc_inode,
405 .destroy_inode = fuse_destroy_inode,
406 .read_inode = fuse_read_inode,
407 .clear_inode = fuse_clear_inode,
408 .put_super = fuse_put_super,
409 .umount_begin = fuse_umount_begin,
410 .statfs = fuse_statfs,
411 .show_options = fuse_show_options,
414 static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
416 struct fuse_init_out *arg = &req->misc.init_out;
418 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
421 unsigned long ra_pages;
423 if (arg->minor >= 6) {
424 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
425 if (arg->flags & FUSE_ASYNC_READ)
428 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
430 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
431 fc->minor = arg->minor;
432 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
434 fuse_put_request(fc, req);
436 wake_up_all(&fc->blocked_waitq);
439 static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
441 struct fuse_init_in *arg = &req->misc.init_in;
443 arg->major = FUSE_KERNEL_VERSION;
444 arg->minor = FUSE_KERNEL_MINOR_VERSION;
445 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
446 arg->flags |= FUSE_ASYNC_READ;
447 req->in.h.opcode = FUSE_INIT;
449 req->in.args[0].size = sizeof(*arg);
450 req->in.args[0].value = arg;
451 req->out.numargs = 1;
452 /* Variable length arguement used for backward compatibility
453 with interface version < 7.5. Rest of init_out is zeroed
454 by do_get_request(), so a short reply is not a problem */
456 req->out.args[0].size = sizeof(struct fuse_init_out);
457 req->out.args[0].value = &req->misc.init_out;
458 req->end = process_init_reply;
459 request_send_background(fc, req);
462 static unsigned long long conn_id(void)
464 /* BKL is held for ->get_sb() */
465 static unsigned long long ctr = 1;
469 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
471 struct fuse_conn *fc;
473 struct fuse_mount_data d;
475 struct dentry *root_dentry;
476 struct fuse_req *init_req;
479 if (!parse_fuse_opt((char *) data, &d))
482 sb->s_blocksize = PAGE_CACHE_SIZE;
483 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
484 sb->s_magic = FUSE_SUPER_MAGIC;
485 sb->s_op = &fuse_super_operations;
486 sb->s_maxbytes = MAX_LFS_FILESIZE;
492 if (file->f_op != &fuse_dev_operations)
500 fc->user_id = d.user_id;
501 fc->group_id = d.group_id;
502 fc->max_read = d.max_read;
504 /* Used by get_root_inode() */
508 root = get_root_inode(sb, d.rootmode);
512 root_dentry = d_alloc_root(root);
518 init_req = fuse_request_alloc();
522 err = kobject_set_name(&fc->kobj, "%llu", conn_id());
526 err = kobject_add(&fc->kobj);
530 /* Setting file->private_data can't race with other mount()
531 instances, since BKL is held for ->get_sb() */
533 if (file->private_data)
534 goto err_kobject_del;
536 sb->s_root = root_dentry;
538 kobject_get(&fc->kobj);
539 file->private_data = fc;
541 * atomic_dec_and_test() in fput() provides the necessary
542 * memory barrier for file->private_data to be visible on all
547 fuse_send_init(fc, init_req);
552 kobject_del(&fc->kobj);
554 fuse_request_free(init_req);
559 kobject_put(&fc->kobj);
563 static int fuse_get_sb(struct file_system_type *fs_type,
564 int flags, const char *dev_name,
565 void *raw_data, struct vfsmount *mnt)
567 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
570 static struct file_system_type fuse_fs_type = {
571 .owner = THIS_MODULE,
573 .get_sb = fuse_get_sb,
574 .kill_sb = kill_anon_super,
577 static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page)
579 return sprintf(page, "%i\n", atomic_read(&fc->num_waiting));
582 static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page,
589 static struct fuse_conn_attr fuse_conn_waiting =
590 __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL);
591 static struct fuse_conn_attr fuse_conn_abort =
592 __ATTR(abort, 0600, NULL, fuse_conn_abort_store);
594 static struct attribute *fuse_conn_attrs[] = {
595 &fuse_conn_waiting.attr,
596 &fuse_conn_abort.attr,
600 static ssize_t fuse_conn_attr_show(struct kobject *kobj,
601 struct attribute *attr,
604 struct fuse_conn_attr *fca =
605 container_of(attr, struct fuse_conn_attr, attr);
608 return fca->show(get_fuse_conn_kobj(kobj), page);
613 static ssize_t fuse_conn_attr_store(struct kobject *kobj,
614 struct attribute *attr,
615 const char *page, size_t count)
617 struct fuse_conn_attr *fca =
618 container_of(attr, struct fuse_conn_attr, attr);
621 return fca->store(get_fuse_conn_kobj(kobj), page, count);
626 static struct sysfs_ops fuse_conn_sysfs_ops = {
627 .show = &fuse_conn_attr_show,
628 .store = &fuse_conn_attr_store,
631 static struct kobj_type ktype_fuse_conn = {
632 .release = fuse_conn_release,
633 .sysfs_ops = &fuse_conn_sysfs_ops,
634 .default_attrs = fuse_conn_attrs,
637 static decl_subsys(fuse, NULL, NULL);
638 static decl_subsys(connections, &ktype_fuse_conn, NULL);
640 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
643 struct inode * inode = foo;
645 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
646 SLAB_CTOR_CONSTRUCTOR)
647 inode_init_once(inode);
650 static int __init fuse_fs_init(void)
654 err = register_filesystem(&fuse_fs_type);
656 printk("fuse: failed to register filesystem\n");
658 fuse_inode_cachep = kmem_cache_create("fuse_inode",
659 sizeof(struct fuse_inode),
660 0, SLAB_HWCACHE_ALIGN,
661 fuse_inode_init_once, NULL);
662 if (!fuse_inode_cachep) {
663 unregister_filesystem(&fuse_fs_type);
671 static void fuse_fs_cleanup(void)
673 unregister_filesystem(&fuse_fs_type);
674 kmem_cache_destroy(fuse_inode_cachep);
677 static int fuse_sysfs_init(void)
681 kset_set_kset_s(&fuse_subsys, fs_subsys);
682 err = subsystem_register(&fuse_subsys);
686 kset_set_kset_s(&connections_subsys, fuse_subsys);
687 err = subsystem_register(&connections_subsys);
689 goto out_fuse_unregister;
694 subsystem_unregister(&fuse_subsys);
699 static void fuse_sysfs_cleanup(void)
701 subsystem_unregister(&connections_subsys);
702 subsystem_unregister(&fuse_subsys);
705 static int __init fuse_init(void)
709 printk("fuse init (API version %i.%i)\n",
710 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
712 res = fuse_fs_init();
716 res = fuse_dev_init();
720 res = fuse_sysfs_init();
722 goto err_dev_cleanup;
734 static void __exit fuse_exit(void)
736 printk(KERN_DEBUG "fuse exit\n");
738 fuse_sysfs_cleanup();
743 module_init(fuse_init);
744 module_exit(fuse_exit);