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/moduleparam.h>
19 #include <linux/parser.h>
20 #include <linux/statfs.h>
22 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23 MODULE_DESCRIPTION("Filesystem in Userspace");
24 MODULE_LICENSE("GPL");
27 static kmem_cache_t *fuse_inode_cachep;
28 static int mount_count;
30 static int mount_max = 1000;
31 module_param(mount_max, int, 0644);
32 MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)");
34 #define FUSE_SUPER_MAGIC 0x65735546
36 struct fuse_mount_data {
42 static struct inode *fuse_alloc_inode(struct super_block *sb)
45 struct fuse_inode *fi;
47 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
51 fi = get_fuse_inode(inode);
52 fi->i_time = jiffies - 1;
58 static void fuse_destroy_inode(struct inode *inode)
60 kmem_cache_free(fuse_inode_cachep, inode);
63 static void fuse_read_inode(struct inode *inode)
68 static void fuse_clear_inode(struct inode *inode)
72 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
74 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
75 invalidate_inode_pages(inode->i_mapping);
77 inode->i_ino = attr->ino;
78 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
79 inode->i_nlink = attr->nlink;
80 inode->i_uid = attr->uid;
81 inode->i_gid = attr->gid;
82 i_size_write(inode, attr->size);
83 inode->i_blksize = PAGE_CACHE_SIZE;
84 inode->i_blocks = attr->blocks;
85 inode->i_atime.tv_sec = attr->atime;
86 inode->i_atime.tv_nsec = attr->atimensec;
87 inode->i_mtime.tv_sec = attr->mtime;
88 inode->i_mtime.tv_nsec = attr->mtimensec;
89 inode->i_ctime.tv_sec = attr->ctime;
90 inode->i_ctime.tv_nsec = attr->ctimensec;
93 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
95 inode->i_mode = attr->mode & S_IFMT;
96 i_size_write(inode, attr->size);
99 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
101 unsigned long nodeid = *(unsigned long *) _nodeidp;
102 if (get_node_id(inode) == nodeid)
108 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
110 unsigned long nodeid = *(unsigned long *) _nodeidp;
111 get_fuse_inode(inode)->nodeid = nodeid;
115 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
116 int generation, struct fuse_attr *attr, int version)
119 struct fuse_conn *fc = get_fuse_conn_super(sb);
123 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
127 if ((inode->i_state & I_NEW)) {
128 inode->i_generation = generation;
129 inode->i_data.backing_dev_info = &fc->bdi;
130 fuse_init_inode(inode, attr);
131 unlock_new_inode(inode);
132 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
134 /* Inode has changed type, any I/O on the old should fail */
135 make_bad_inode(inode);
141 fuse_change_attributes(inode, attr);
142 inode->i_version = version;
146 static void fuse_put_super(struct super_block *sb)
148 struct fuse_conn *fc = get_fuse_conn_super(sb);
150 spin_lock(&fuse_lock);
154 /* Flush all readers on this fs */
155 wake_up_all(&fc->waitq);
156 fuse_release_conn(fc);
157 *get_fuse_conn_super_p(sb) = NULL;
158 spin_unlock(&fuse_lock);
165 OPT_DEFAULT_PERMISSIONS,
172 static match_table_t tokens = {
174 {OPT_ROOTMODE, "rootmode=%o"},
175 {OPT_USER_ID, "user_id=%u"},
176 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
177 {OPT_ALLOW_OTHER, "allow_other"},
178 {OPT_ALLOW_ROOT, "allow_root"},
179 {OPT_KERNEL_CACHE, "kernel_cache"},
183 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
186 memset(d, 0, sizeof(struct fuse_mount_data));
189 while ((p = strsep(&opt, ",")) != NULL) {
192 substring_t args[MAX_OPT_ARGS];
196 token = match_token(p, tokens, args);
199 if (match_int(&args[0], &value))
205 if (match_octal(&args[0], &value))
211 if (match_int(&args[0], &value))
226 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
228 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
230 seq_printf(m, ",user_id=%u", fc->user_id);
234 static void free_conn(struct fuse_conn *fc)
236 while (!list_empty(&fc->unused_list)) {
237 struct fuse_req *req;
238 req = list_entry(fc->unused_list.next, struct fuse_req, list);
239 list_del(&req->list);
240 fuse_request_free(req);
245 /* Must be called with the fuse lock held */
246 void fuse_release_conn(struct fuse_conn *fc)
248 if (!fc->sb && !fc->file)
252 static struct fuse_conn *new_conn(void)
254 struct fuse_conn *fc;
256 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
259 memset(fc, 0, sizeof(*fc));
263 init_waitqueue_head(&fc->waitq);
264 INIT_LIST_HEAD(&fc->pending);
265 INIT_LIST_HEAD(&fc->processing);
266 INIT_LIST_HEAD(&fc->unused_list);
267 sema_init(&fc->outstanding_sem, 0);
268 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
269 struct fuse_req *req = fuse_request_alloc();
274 list_add(&req->list, &fc->unused_list);
276 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
277 fc->bdi.unplug_io_fn = default_unplug_io_fn;
283 static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
285 struct fuse_conn *fc;
287 if (file->f_op != &fuse_dev_operations)
288 return ERR_PTR(-EINVAL);
291 return ERR_PTR(-ENOMEM);
292 spin_lock(&fuse_lock);
293 if (file->private_data) {
295 fc = ERR_PTR(-EINVAL);
297 file->private_data = fc;
301 spin_unlock(&fuse_lock);
305 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
307 struct fuse_attr attr;
308 memset(&attr, 0, sizeof(attr));
311 attr.ino = FUSE_ROOT_ID;
312 return fuse_iget(sb, 1, 0, &attr, 0);
315 static struct super_operations fuse_super_operations = {
316 .alloc_inode = fuse_alloc_inode,
317 .destroy_inode = fuse_destroy_inode,
318 .read_inode = fuse_read_inode,
319 .clear_inode = fuse_clear_inode,
320 .put_super = fuse_put_super,
321 .show_options = fuse_show_options,
324 static int inc_mount_count(void)
327 spin_lock(&fuse_lock);
329 if (mount_max == -1 || mount_count <= mount_max)
331 spin_unlock(&fuse_lock);
335 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
337 struct fuse_conn *fc;
339 struct fuse_mount_data d;
343 if (!parse_fuse_opt((char *) data, &d))
346 sb->s_blocksize = PAGE_CACHE_SIZE;
347 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
348 sb->s_magic = FUSE_SUPER_MAGIC;
349 sb->s_op = &fuse_super_operations;
350 sb->s_maxbytes = MAX_LFS_FILESIZE;
356 fc = get_conn(file, sb);
361 fc->user_id = d.user_id;
363 *get_fuse_conn_super_p(sb) = fc;
366 if (!inc_mount_count() && current->uid != 0)
370 root = get_root_inode(sb, d.rootmode);
374 sb->s_root = d_alloc_root(root);
383 spin_lock(&fuse_lock);
386 fuse_release_conn(fc);
387 spin_unlock(&fuse_lock);
388 *get_fuse_conn_super_p(sb) = NULL;
392 static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
393 int flags, const char *dev_name,
396 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
399 static struct file_system_type fuse_fs_type = {
400 .owner = THIS_MODULE,
402 .get_sb = fuse_get_sb,
403 .kill_sb = kill_anon_super,
406 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
409 struct inode * inode = foo;
411 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
412 SLAB_CTOR_CONSTRUCTOR)
413 inode_init_once(inode);
416 static int __init fuse_fs_init(void)
420 err = register_filesystem(&fuse_fs_type);
422 printk("fuse: failed to register filesystem\n");
424 fuse_inode_cachep = kmem_cache_create("fuse_inode",
425 sizeof(struct fuse_inode),
426 0, SLAB_HWCACHE_ALIGN,
427 fuse_inode_init_once, NULL);
428 if (!fuse_inode_cachep) {
429 unregister_filesystem(&fuse_fs_type);
437 static void fuse_fs_cleanup(void)
439 unregister_filesystem(&fuse_fs_type);
440 kmem_cache_destroy(fuse_inode_cachep);
443 static int __init fuse_init(void)
447 printk("fuse init (API version %i.%i)\n",
448 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
450 spin_lock_init(&fuse_lock);
451 res = fuse_fs_init();
455 res = fuse_dev_init();
467 static void __exit fuse_exit(void)
469 printk(KERN_DEBUG "fuse exit\n");
475 module_init(fuse_init);
476 module_exit(fuse_exit);