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 struct list_head fuse_conn_list;
26 DEFINE_MUTEX(fuse_mutex);
28 #define FUSE_SUPER_MAGIC 0x65735546
30 struct fuse_mount_data {
35 unsigned fd_present : 1;
36 unsigned rootmode_present : 1;
37 unsigned user_id_present : 1;
38 unsigned group_id_present : 1;
43 static struct inode *fuse_alloc_inode(struct super_block *sb)
46 struct fuse_inode *fi;
48 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
52 fi = get_fuse_inode(inode);
53 fi->i_time = jiffies - 1;
56 fi->forget_req = fuse_request_alloc();
57 if (!fi->forget_req) {
58 kmem_cache_free(fuse_inode_cachep, inode);
65 static void fuse_destroy_inode(struct inode *inode)
67 struct fuse_inode *fi = get_fuse_inode(inode);
69 fuse_request_free(fi->forget_req);
70 kmem_cache_free(fuse_inode_cachep, inode);
73 static void fuse_read_inode(struct inode *inode)
78 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
79 unsigned long nodeid, u64 nlookup)
81 struct fuse_forget_in *inarg = &req->misc.forget_in;
82 inarg->nlookup = nlookup;
83 req->in.h.opcode = FUSE_FORGET;
84 req->in.h.nodeid = nodeid;
86 req->in.args[0].size = sizeof(struct fuse_forget_in);
87 req->in.args[0].value = inarg;
88 request_send_noreply(fc, req);
91 static void fuse_clear_inode(struct inode *inode)
93 if (inode->i_sb->s_flags & MS_ACTIVE) {
94 struct fuse_conn *fc = get_fuse_conn(inode);
95 struct fuse_inode *fi = get_fuse_inode(inode);
96 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
97 fi->forget_req = NULL;
101 static int fuse_remount_fs(struct super_block *sb, int *flags, char *data)
103 if (*flags & MS_MANDLOCK)
109 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
111 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
112 invalidate_inode_pages(inode->i_mapping);
114 inode->i_ino = attr->ino;
115 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
116 inode->i_nlink = attr->nlink;
117 inode->i_uid = attr->uid;
118 inode->i_gid = attr->gid;
119 i_size_write(inode, attr->size);
120 inode->i_blksize = PAGE_CACHE_SIZE;
121 inode->i_blocks = attr->blocks;
122 inode->i_atime.tv_sec = attr->atime;
123 inode->i_atime.tv_nsec = attr->atimensec;
124 inode->i_mtime.tv_sec = attr->mtime;
125 inode->i_mtime.tv_nsec = attr->mtimensec;
126 inode->i_ctime.tv_sec = attr->ctime;
127 inode->i_ctime.tv_nsec = attr->ctimensec;
130 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
132 inode->i_mode = attr->mode & S_IFMT;
133 i_size_write(inode, attr->size);
134 if (S_ISREG(inode->i_mode)) {
135 fuse_init_common(inode);
136 fuse_init_file_inode(inode);
137 } else if (S_ISDIR(inode->i_mode))
138 fuse_init_dir(inode);
139 else if (S_ISLNK(inode->i_mode))
140 fuse_init_symlink(inode);
141 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
142 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
143 fuse_init_common(inode);
144 init_special_inode(inode, inode->i_mode,
145 new_decode_dev(attr->rdev));
150 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
152 unsigned long nodeid = *(unsigned long *) _nodeidp;
153 if (get_node_id(inode) == nodeid)
159 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
161 unsigned long nodeid = *(unsigned long *) _nodeidp;
162 get_fuse_inode(inode)->nodeid = nodeid;
166 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
167 int generation, struct fuse_attr *attr)
170 struct fuse_inode *fi;
171 struct fuse_conn *fc = get_fuse_conn_super(sb);
175 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
179 if ((inode->i_state & I_NEW)) {
180 inode->i_flags |= S_NOATIME|S_NOCMTIME;
181 inode->i_generation = generation;
182 inode->i_data.backing_dev_info = &fc->bdi;
183 fuse_init_inode(inode, attr);
184 unlock_new_inode(inode);
185 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
187 /* Inode has changed type, any I/O on the old should fail */
188 make_bad_inode(inode);
194 fi = get_fuse_inode(inode);
196 fuse_change_attributes(inode, attr);
200 static void fuse_umount_begin(struct super_block *sb)
202 fuse_abort_conn(get_fuse_conn_super(sb));
205 static void fuse_put_super(struct super_block *sb)
207 struct fuse_conn *fc = get_fuse_conn_super(sb);
209 spin_lock(&fc->lock);
212 spin_unlock(&fc->lock);
213 /* Flush all readers on this fs */
214 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
215 wake_up_all(&fc->waitq);
216 wake_up_all(&fc->blocked_waitq);
217 mutex_lock(&fuse_mutex);
218 list_del(&fc->entry);
219 fuse_ctl_remove_conn(fc);
220 mutex_unlock(&fuse_mutex);
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 dentry *dentry, struct kstatfs *buf)
240 struct super_block *sb = dentry->d_sb;
241 struct fuse_conn *fc = get_fuse_conn_super(sb);
242 struct fuse_req *req;
243 struct fuse_statfs_out outarg;
246 req = fuse_get_req(fc);
250 memset(&outarg, 0, sizeof(outarg));
252 req->in.h.opcode = FUSE_STATFS;
253 req->out.numargs = 1;
254 req->out.args[0].size =
255 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
256 req->out.args[0].value = &outarg;
257 request_send(fc, req);
258 err = req->out.h.error;
260 convert_fuse_statfs(buf, &outarg.st);
261 fuse_put_request(fc, req);
270 OPT_DEFAULT_PERMISSIONS,
276 static match_table_t tokens = {
278 {OPT_ROOTMODE, "rootmode=%o"},
279 {OPT_USER_ID, "user_id=%u"},
280 {OPT_GROUP_ID, "group_id=%u"},
281 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
282 {OPT_ALLOW_OTHER, "allow_other"},
283 {OPT_MAX_READ, "max_read=%u"},
287 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
290 memset(d, 0, sizeof(struct fuse_mount_data));
293 while ((p = strsep(&opt, ",")) != NULL) {
296 substring_t args[MAX_OPT_ARGS];
300 token = match_token(p, tokens, args);
303 if (match_int(&args[0], &value))
310 if (match_octal(&args[0], &value))
313 d->rootmode_present = 1;
317 if (match_int(&args[0], &value))
320 d->user_id_present = 1;
324 if (match_int(&args[0], &value))
327 d->group_id_present = 1;
330 case OPT_DEFAULT_PERMISSIONS:
331 d->flags |= FUSE_DEFAULT_PERMISSIONS;
334 case OPT_ALLOW_OTHER:
335 d->flags |= FUSE_ALLOW_OTHER;
339 if (match_int(&args[0], &value))
349 if (!d->fd_present || !d->rootmode_present ||
350 !d->user_id_present || !d->group_id_present)
356 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
358 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
360 seq_printf(m, ",user_id=%u", fc->user_id);
361 seq_printf(m, ",group_id=%u", fc->group_id);
362 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
363 seq_puts(m, ",default_permissions");
364 if (fc->flags & FUSE_ALLOW_OTHER)
365 seq_puts(m, ",allow_other");
366 if (fc->max_read != ~0)
367 seq_printf(m, ",max_read=%u", fc->max_read);
371 static struct fuse_conn *new_conn(void)
373 struct fuse_conn *fc;
375 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
377 spin_lock_init(&fc->lock);
378 atomic_set(&fc->count, 1);
379 init_waitqueue_head(&fc->waitq);
380 init_waitqueue_head(&fc->blocked_waitq);
381 INIT_LIST_HEAD(&fc->pending);
382 INIT_LIST_HEAD(&fc->processing);
383 INIT_LIST_HEAD(&fc->io);
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 void fuse_conn_put(struct fuse_conn *fc)
395 if (atomic_dec_and_test(&fc->count))
399 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
401 atomic_inc(&fc->count);
405 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
407 struct fuse_attr attr;
408 memset(&attr, 0, sizeof(attr));
411 attr.ino = FUSE_ROOT_ID;
412 return fuse_iget(sb, 1, 0, &attr);
415 static struct super_operations fuse_super_operations = {
416 .alloc_inode = fuse_alloc_inode,
417 .destroy_inode = fuse_destroy_inode,
418 .read_inode = fuse_read_inode,
419 .clear_inode = fuse_clear_inode,
420 .remount_fs = fuse_remount_fs,
421 .put_super = fuse_put_super,
422 .umount_begin = fuse_umount_begin,
423 .statfs = fuse_statfs,
424 .show_options = fuse_show_options,
427 static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
429 struct fuse_init_out *arg = &req->misc.init_out;
431 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
434 unsigned long ra_pages;
436 if (arg->minor >= 6) {
437 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
438 if (arg->flags & FUSE_ASYNC_READ)
440 if (!(arg->flags & FUSE_POSIX_LOCKS))
443 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
447 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
448 fc->minor = arg->minor;
449 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
451 fuse_put_request(fc, req);
453 wake_up_all(&fc->blocked_waitq);
456 static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
458 struct fuse_init_in *arg = &req->misc.init_in;
460 arg->major = FUSE_KERNEL_VERSION;
461 arg->minor = FUSE_KERNEL_MINOR_VERSION;
462 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
463 arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS;
464 req->in.h.opcode = FUSE_INIT;
466 req->in.args[0].size = sizeof(*arg);
467 req->in.args[0].value = arg;
468 req->out.numargs = 1;
469 /* Variable length arguement used for backward compatibility
470 with interface version < 7.5. Rest of init_out is zeroed
471 by do_get_request(), so a short reply is not a problem */
473 req->out.args[0].size = sizeof(struct fuse_init_out);
474 req->out.args[0].value = &req->misc.init_out;
475 req->end = process_init_reply;
476 request_send_background(fc, req);
479 static u64 conn_id(void)
485 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
487 struct fuse_conn *fc;
489 struct fuse_mount_data d;
491 struct dentry *root_dentry;
492 struct fuse_req *init_req;
495 if (sb->s_flags & MS_MANDLOCK)
498 if (!parse_fuse_opt((char *) data, &d))
501 sb->s_blocksize = PAGE_CACHE_SIZE;
502 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
503 sb->s_magic = FUSE_SUPER_MAGIC;
504 sb->s_op = &fuse_super_operations;
505 sb->s_maxbytes = MAX_LFS_FILESIZE;
511 if (file->f_op != &fuse_dev_operations)
519 fc->user_id = d.user_id;
520 fc->group_id = d.group_id;
521 fc->max_read = d.max_read;
523 /* Used by get_root_inode() */
527 root = get_root_inode(sb, d.rootmode);
531 root_dentry = d_alloc_root(root);
537 init_req = fuse_request_alloc();
541 mutex_lock(&fuse_mutex);
543 if (file->private_data)
547 err = fuse_ctl_add_conn(fc);
551 list_add_tail(&fc->entry, &fuse_conn_list);
552 sb->s_root = root_dentry;
554 file->private_data = fuse_conn_get(fc);
555 mutex_unlock(&fuse_mutex);
557 * atomic_dec_and_test() in fput() provides the necessary
558 * memory barrier for file->private_data to be visible on all
563 fuse_send_init(fc, init_req);
568 mutex_unlock(&fuse_mutex);
569 fuse_request_free(init_req);
578 static int fuse_get_sb(struct file_system_type *fs_type,
579 int flags, const char *dev_name,
580 void *raw_data, struct vfsmount *mnt)
582 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt);
585 static struct file_system_type fuse_fs_type = {
586 .owner = THIS_MODULE,
588 .get_sb = fuse_get_sb,
589 .kill_sb = kill_anon_super,
592 static decl_subsys(fuse, NULL, NULL);
593 static decl_subsys(connections, NULL, NULL);
595 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
598 struct inode * inode = foo;
600 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
601 SLAB_CTOR_CONSTRUCTOR)
602 inode_init_once(inode);
605 static int __init fuse_fs_init(void)
609 err = register_filesystem(&fuse_fs_type);
611 printk("fuse: failed to register filesystem\n");
613 fuse_inode_cachep = kmem_cache_create("fuse_inode",
614 sizeof(struct fuse_inode),
615 0, SLAB_HWCACHE_ALIGN,
616 fuse_inode_init_once, NULL);
617 if (!fuse_inode_cachep) {
618 unregister_filesystem(&fuse_fs_type);
626 static void fuse_fs_cleanup(void)
628 unregister_filesystem(&fuse_fs_type);
629 kmem_cache_destroy(fuse_inode_cachep);
632 static int fuse_sysfs_init(void)
636 kset_set_kset_s(&fuse_subsys, fs_subsys);
637 err = subsystem_register(&fuse_subsys);
641 kset_set_kset_s(&connections_subsys, fuse_subsys);
642 err = subsystem_register(&connections_subsys);
644 goto out_fuse_unregister;
649 subsystem_unregister(&fuse_subsys);
654 static void fuse_sysfs_cleanup(void)
656 subsystem_unregister(&connections_subsys);
657 subsystem_unregister(&fuse_subsys);
660 static int __init fuse_init(void)
664 printk("fuse init (API version %i.%i)\n",
665 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
667 INIT_LIST_HEAD(&fuse_conn_list);
668 res = fuse_fs_init();
672 res = fuse_dev_init();
676 res = fuse_sysfs_init();
678 goto err_dev_cleanup;
680 res = fuse_ctl_init();
682 goto err_sysfs_cleanup;
687 fuse_sysfs_cleanup();
696 static void __exit fuse_exit(void)
698 printk(KERN_DEBUG "fuse exit\n");
701 fuse_sysfs_cleanup();
706 module_init(fuse_init);
707 module_exit(fuse_exit);