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;
54 fi->forget_req = fuse_request_alloc();
55 if (!fi->forget_req) {
56 kmem_cache_free(fuse_inode_cachep, inode);
63 static void fuse_destroy_inode(struct inode *inode)
65 struct fuse_inode *fi = get_fuse_inode(inode);
67 fuse_request_free(fi->forget_req);
68 kmem_cache_free(fuse_inode_cachep, inode);
71 static void fuse_read_inode(struct inode *inode)
76 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
77 unsigned long nodeid, int version)
79 struct fuse_forget_in *inarg = &req->misc.forget_in;
80 inarg->version = version;
81 req->in.h.opcode = FUSE_FORGET;
82 req->in.h.nodeid = nodeid;
84 req->in.args[0].size = sizeof(struct fuse_forget_in);
85 req->in.args[0].value = inarg;
86 request_send_noreply(fc, req);
89 static void fuse_clear_inode(struct inode *inode)
91 struct fuse_conn *fc = get_fuse_conn(inode);
93 struct fuse_inode *fi = get_fuse_inode(inode);
94 fuse_send_forget(fc, fi->forget_req, fi->nodeid, inode->i_version);
95 fi->forget_req = NULL;
99 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
101 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
102 invalidate_inode_pages(inode->i_mapping);
104 inode->i_ino = attr->ino;
105 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
106 inode->i_nlink = attr->nlink;
107 inode->i_uid = attr->uid;
108 inode->i_gid = attr->gid;
109 i_size_write(inode, attr->size);
110 inode->i_blksize = PAGE_CACHE_SIZE;
111 inode->i_blocks = attr->blocks;
112 inode->i_atime.tv_sec = attr->atime;
113 inode->i_atime.tv_nsec = attr->atimensec;
114 inode->i_mtime.tv_sec = attr->mtime;
115 inode->i_mtime.tv_nsec = attr->mtimensec;
116 inode->i_ctime.tv_sec = attr->ctime;
117 inode->i_ctime.tv_nsec = attr->ctimensec;
120 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
122 inode->i_mode = attr->mode & S_IFMT;
123 i_size_write(inode, attr->size);
124 if (S_ISREG(inode->i_mode)) {
125 fuse_init_common(inode);
126 } else if (S_ISDIR(inode->i_mode))
127 fuse_init_dir(inode);
128 else if (S_ISLNK(inode->i_mode))
129 fuse_init_symlink(inode);
130 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
131 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
132 fuse_init_common(inode);
133 init_special_inode(inode, inode->i_mode,
134 new_decode_dev(attr->rdev));
136 /* Don't let user create weird files */
137 inode->i_mode = S_IFREG;
138 fuse_init_common(inode);
142 static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
144 unsigned long nodeid = *(unsigned long *) _nodeidp;
145 if (get_node_id(inode) == nodeid)
151 static int fuse_inode_set(struct inode *inode, void *_nodeidp)
153 unsigned long nodeid = *(unsigned long *) _nodeidp;
154 get_fuse_inode(inode)->nodeid = nodeid;
158 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
159 int generation, struct fuse_attr *attr, int version)
162 struct fuse_conn *fc = get_fuse_conn_super(sb);
166 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
170 if ((inode->i_state & I_NEW)) {
171 inode->i_generation = generation;
172 inode->i_data.backing_dev_info = &fc->bdi;
173 fuse_init_inode(inode, attr);
174 unlock_new_inode(inode);
175 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
177 /* Inode has changed type, any I/O on the old should fail */
178 make_bad_inode(inode);
184 fuse_change_attributes(inode, attr);
185 inode->i_version = version;
189 static void fuse_put_super(struct super_block *sb)
191 struct fuse_conn *fc = get_fuse_conn_super(sb);
193 spin_lock(&fuse_lock);
197 /* Flush all readers on this fs */
198 wake_up_all(&fc->waitq);
199 fuse_release_conn(fc);
200 *get_fuse_conn_super_p(sb) = NULL;
201 spin_unlock(&fuse_lock);
204 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
206 stbuf->f_type = FUSE_SUPER_MAGIC;
207 stbuf->f_bsize = attr->bsize;
208 stbuf->f_blocks = attr->blocks;
209 stbuf->f_bfree = attr->bfree;
210 stbuf->f_bavail = attr->bavail;
211 stbuf->f_files = attr->files;
212 stbuf->f_ffree = attr->ffree;
213 stbuf->f_namelen = attr->namelen;
214 /* fsid is left zero */
217 static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
219 struct fuse_conn *fc = get_fuse_conn_super(sb);
220 struct fuse_req *req;
221 struct fuse_statfs_out outarg;
224 req = fuse_get_request(fc);
229 req->in.h.opcode = FUSE_STATFS;
230 req->out.numargs = 1;
231 req->out.args[0].size = sizeof(outarg);
232 req->out.args[0].value = &outarg;
233 request_send(fc, req);
234 err = req->out.h.error;
236 convert_fuse_statfs(buf, &outarg.st);
237 fuse_put_request(fc, req);
245 OPT_DEFAULT_PERMISSIONS,
252 static match_table_t tokens = {
254 {OPT_ROOTMODE, "rootmode=%o"},
255 {OPT_USER_ID, "user_id=%u"},
256 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
257 {OPT_ALLOW_OTHER, "allow_other"},
258 {OPT_ALLOW_ROOT, "allow_root"},
259 {OPT_KERNEL_CACHE, "kernel_cache"},
263 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
266 memset(d, 0, sizeof(struct fuse_mount_data));
269 while ((p = strsep(&opt, ",")) != NULL) {
272 substring_t args[MAX_OPT_ARGS];
276 token = match_token(p, tokens, args);
279 if (match_int(&args[0], &value))
285 if (match_octal(&args[0], &value))
291 if (match_int(&args[0], &value))
306 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
308 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
310 seq_printf(m, ",user_id=%u", fc->user_id);
314 static void free_conn(struct fuse_conn *fc)
316 while (!list_empty(&fc->unused_list)) {
317 struct fuse_req *req;
318 req = list_entry(fc->unused_list.next, struct fuse_req, list);
319 list_del(&req->list);
320 fuse_request_free(req);
325 /* Must be called with the fuse lock held */
326 void fuse_release_conn(struct fuse_conn *fc)
328 if (!fc->sb && !fc->file)
332 static struct fuse_conn *new_conn(void)
334 struct fuse_conn *fc;
336 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
339 memset(fc, 0, sizeof(*fc));
343 init_waitqueue_head(&fc->waitq);
344 INIT_LIST_HEAD(&fc->pending);
345 INIT_LIST_HEAD(&fc->processing);
346 INIT_LIST_HEAD(&fc->unused_list);
347 sema_init(&fc->outstanding_sem, 0);
348 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
349 struct fuse_req *req = fuse_request_alloc();
354 list_add(&req->list, &fc->unused_list);
356 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
357 fc->bdi.unplug_io_fn = default_unplug_io_fn;
363 static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
365 struct fuse_conn *fc;
367 if (file->f_op != &fuse_dev_operations)
368 return ERR_PTR(-EINVAL);
371 return ERR_PTR(-ENOMEM);
372 spin_lock(&fuse_lock);
373 if (file->private_data) {
375 fc = ERR_PTR(-EINVAL);
377 file->private_data = fc;
381 spin_unlock(&fuse_lock);
385 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
387 struct fuse_attr attr;
388 memset(&attr, 0, sizeof(attr));
391 attr.ino = FUSE_ROOT_ID;
392 return fuse_iget(sb, 1, 0, &attr, 0);
395 static struct super_operations fuse_super_operations = {
396 .alloc_inode = fuse_alloc_inode,
397 .destroy_inode = fuse_destroy_inode,
398 .read_inode = fuse_read_inode,
399 .clear_inode = fuse_clear_inode,
400 .put_super = fuse_put_super,
401 .statfs = fuse_statfs,
402 .show_options = fuse_show_options,
405 static int inc_mount_count(void)
408 spin_lock(&fuse_lock);
410 if (mount_max == -1 || mount_count <= mount_max)
412 spin_unlock(&fuse_lock);
416 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
418 struct fuse_conn *fc;
420 struct fuse_mount_data d;
424 if (!parse_fuse_opt((char *) data, &d))
427 sb->s_blocksize = PAGE_CACHE_SIZE;
428 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
429 sb->s_magic = FUSE_SUPER_MAGIC;
430 sb->s_op = &fuse_super_operations;
431 sb->s_maxbytes = MAX_LFS_FILESIZE;
437 fc = get_conn(file, sb);
442 fc->user_id = d.user_id;
444 *get_fuse_conn_super_p(sb) = fc;
447 if (!inc_mount_count() && current->uid != 0)
451 root = get_root_inode(sb, d.rootmode);
455 sb->s_root = d_alloc_root(root);
464 spin_lock(&fuse_lock);
467 fuse_release_conn(fc);
468 spin_unlock(&fuse_lock);
469 *get_fuse_conn_super_p(sb) = NULL;
473 static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
474 int flags, const char *dev_name,
477 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
480 static struct file_system_type fuse_fs_type = {
481 .owner = THIS_MODULE,
483 .get_sb = fuse_get_sb,
484 .kill_sb = kill_anon_super,
487 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
490 struct inode * inode = foo;
492 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
493 SLAB_CTOR_CONSTRUCTOR)
494 inode_init_once(inode);
497 static int __init fuse_fs_init(void)
501 err = register_filesystem(&fuse_fs_type);
503 printk("fuse: failed to register filesystem\n");
505 fuse_inode_cachep = kmem_cache_create("fuse_inode",
506 sizeof(struct fuse_inode),
507 0, SLAB_HWCACHE_ALIGN,
508 fuse_inode_init_once, NULL);
509 if (!fuse_inode_cachep) {
510 unregister_filesystem(&fuse_fs_type);
518 static void fuse_fs_cleanup(void)
520 unregister_filesystem(&fuse_fs_type);
521 kmem_cache_destroy(fuse_inode_cachep);
524 static int __init fuse_init(void)
528 printk("fuse init (API version %i.%i)\n",
529 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
531 spin_lock_init(&fuse_lock);
532 res = fuse_fs_init();
536 res = fuse_dev_init();
548 static void __exit fuse_exit(void)
550 printk(KERN_DEBUG "fuse exit\n");
556 module_init(fuse_init);
557 module_exit(fuse_exit);