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/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");
26 static kmem_cache_t *fuse_inode_cachep;
28 #define FUSE_SUPER_MAGIC 0x65735546
30 struct fuse_mount_data {
39 static struct inode *fuse_alloc_inode(struct super_block *sb)
42 struct fuse_inode *fi;
44 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
48 fi = get_fuse_inode(inode);
49 fi->i_time = jiffies - 1;
52 fi->forget_req = fuse_request_alloc();
53 if (!fi->forget_req) {
54 kmem_cache_free(fuse_inode_cachep, inode);
61 static void fuse_destroy_inode(struct inode *inode)
63 struct fuse_inode *fi = get_fuse_inode(inode);
65 fuse_request_free(fi->forget_req);
66 kmem_cache_free(fuse_inode_cachep, inode);
69 static void fuse_read_inode(struct inode *inode)
74 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
75 unsigned long nodeid, u64 nlookup)
77 struct fuse_forget_in *inarg = &req->misc.forget_in;
78 inarg->nlookup = nlookup;
79 req->in.h.opcode = FUSE_FORGET;
80 req->in.h.nodeid = nodeid;
82 req->in.args[0].size = sizeof(struct fuse_forget_in);
83 req->in.args[0].value = inarg;
84 request_send_noreply(fc, req);
87 static void fuse_clear_inode(struct inode *inode)
89 if (inode->i_sb->s_flags & MS_ACTIVE) {
90 struct fuse_conn *fc = get_fuse_conn(inode);
91 struct fuse_inode *fi = get_fuse_inode(inode);
92 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
93 fi->forget_req = NULL;
97 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
99 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
100 invalidate_inode_pages(inode->i_mapping);
102 inode->i_ino = attr->ino;
103 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
104 inode->i_nlink = attr->nlink;
105 inode->i_uid = attr->uid;
106 inode->i_gid = attr->gid;
107 i_size_write(inode, attr->size);
108 inode->i_blksize = PAGE_CACHE_SIZE;
109 inode->i_blocks = attr->blocks;
110 inode->i_atime.tv_sec = attr->atime;
111 inode->i_atime.tv_nsec = attr->atimensec;
112 inode->i_mtime.tv_sec = attr->mtime;
113 inode->i_mtime.tv_nsec = attr->mtimensec;
114 inode->i_ctime.tv_sec = attr->ctime;
115 inode->i_ctime.tv_nsec = attr->ctimensec;
118 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
120 inode->i_mode = attr->mode & S_IFMT;
121 i_size_write(inode, attr->size);
122 if (S_ISREG(inode->i_mode)) {
123 fuse_init_common(inode);
124 fuse_init_file_inode(inode);
125 } else if (S_ISDIR(inode->i_mode))
126 fuse_init_dir(inode);
127 else if (S_ISLNK(inode->i_mode))
128 fuse_init_symlink(inode);
129 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
130 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
131 fuse_init_common(inode);
132 init_special_inode(inode, inode->i_mode,
133 new_decode_dev(attr->rdev));
135 /* Don't let user create weird files */
136 inode->i_mode = S_IFREG;
137 fuse_init_common(inode);
138 fuse_init_file_inode(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)
162 struct fuse_inode *fi;
163 struct fuse_conn *fc = get_fuse_conn_super(sb);
167 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
171 if ((inode->i_state & I_NEW)) {
172 inode->i_generation = generation;
173 inode->i_data.backing_dev_info = &fc->bdi;
174 fuse_init_inode(inode, attr);
175 unlock_new_inode(inode);
176 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
178 /* Inode has changed type, any I/O on the old should fail */
179 make_bad_inode(inode);
185 fi = get_fuse_inode(inode);
187 fuse_change_attributes(inode, attr);
191 static void fuse_put_super(struct super_block *sb)
193 struct fuse_conn *fc = get_fuse_conn_super(sb);
195 down_write(&fc->sbput_sem);
196 while (!list_empty(&fc->background))
197 fuse_release_background(list_entry(fc->background.next,
198 struct fuse_req, bg_entry));
200 spin_lock(&fuse_lock);
205 /* Flush all readers on this fs */
206 wake_up_all(&fc->waitq);
207 up_write(&fc->sbput_sem);
208 fuse_release_conn(fc);
209 spin_unlock(&fuse_lock);
212 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
214 stbuf->f_type = FUSE_SUPER_MAGIC;
215 stbuf->f_bsize = attr->bsize;
216 stbuf->f_blocks = attr->blocks;
217 stbuf->f_bfree = attr->bfree;
218 stbuf->f_bavail = attr->bavail;
219 stbuf->f_files = attr->files;
220 stbuf->f_ffree = attr->ffree;
221 stbuf->f_namelen = attr->namelen;
222 /* fsid is left zero */
225 static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
227 struct fuse_conn *fc = get_fuse_conn_super(sb);
228 struct fuse_req *req;
229 struct fuse_statfs_out outarg;
232 req = fuse_get_request(fc);
237 req->in.h.opcode = FUSE_STATFS;
238 req->out.numargs = 1;
239 req->out.args[0].size = sizeof(outarg);
240 req->out.args[0].value = &outarg;
241 request_send(fc, req);
242 err = req->out.h.error;
244 convert_fuse_statfs(buf, &outarg.st);
245 fuse_put_request(fc, req);
254 OPT_DEFAULT_PERMISSIONS,
261 static match_table_t tokens = {
263 {OPT_ROOTMODE, "rootmode=%o"},
264 {OPT_USER_ID, "user_id=%u"},
265 {OPT_GROUP_ID, "group_id=%u"},
266 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
267 {OPT_ALLOW_OTHER, "allow_other"},
268 {OPT_KERNEL_CACHE, "kernel_cache"},
269 {OPT_MAX_READ, "max_read=%u"},
273 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
276 memset(d, 0, sizeof(struct fuse_mount_data));
280 while ((p = strsep(&opt, ",")) != NULL) {
283 substring_t args[MAX_OPT_ARGS];
287 token = match_token(p, tokens, args);
290 if (match_int(&args[0], &value))
296 if (match_octal(&args[0], &value))
302 if (match_int(&args[0], &value))
308 if (match_int(&args[0], &value))
313 case OPT_DEFAULT_PERMISSIONS:
314 d->flags |= FUSE_DEFAULT_PERMISSIONS;
317 case OPT_ALLOW_OTHER:
318 d->flags |= FUSE_ALLOW_OTHER;
321 case OPT_KERNEL_CACHE:
322 d->flags |= FUSE_KERNEL_CACHE;
326 if (match_int(&args[0], &value))
341 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
343 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
345 seq_printf(m, ",user_id=%u", fc->user_id);
346 seq_printf(m, ",group_id=%u", fc->group_id);
347 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
348 seq_puts(m, ",default_permissions");
349 if (fc->flags & FUSE_ALLOW_OTHER)
350 seq_puts(m, ",allow_other");
351 if (fc->flags & FUSE_KERNEL_CACHE)
352 seq_puts(m, ",kernel_cache");
353 if (fc->max_read != ~0)
354 seq_printf(m, ",max_read=%u", fc->max_read);
358 static void free_conn(struct fuse_conn *fc)
360 while (!list_empty(&fc->unused_list)) {
361 struct fuse_req *req;
362 req = list_entry(fc->unused_list.next, struct fuse_req, list);
363 list_del(&req->list);
364 fuse_request_free(req);
369 /* Must be called with the fuse lock held */
370 void fuse_release_conn(struct fuse_conn *fc)
377 static struct fuse_conn *new_conn(void)
379 struct fuse_conn *fc;
381 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
384 memset(fc, 0, sizeof(*fc));
385 init_waitqueue_head(&fc->waitq);
386 INIT_LIST_HEAD(&fc->pending);
387 INIT_LIST_HEAD(&fc->processing);
388 INIT_LIST_HEAD(&fc->unused_list);
389 INIT_LIST_HEAD(&fc->background);
390 sema_init(&fc->outstanding_sem, 0);
391 init_rwsem(&fc->sbput_sem);
392 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
393 struct fuse_req *req = fuse_request_alloc();
398 list_add(&req->list, &fc->unused_list);
400 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
401 fc->bdi.unplug_io_fn = default_unplug_io_fn;
407 static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
409 struct fuse_conn *fc;
411 if (file->f_op != &fuse_dev_operations)
412 return ERR_PTR(-EINVAL);
415 return ERR_PTR(-ENOMEM);
416 spin_lock(&fuse_lock);
417 if (file->private_data) {
419 fc = ERR_PTR(-EINVAL);
421 file->private_data = fc;
422 *get_fuse_conn_super_p(sb) = fc;
427 spin_unlock(&fuse_lock);
431 static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
433 struct fuse_attr attr;
434 memset(&attr, 0, sizeof(attr));
437 attr.ino = FUSE_ROOT_ID;
438 return fuse_iget(sb, 1, 0, &attr);
441 static struct super_operations fuse_super_operations = {
442 .alloc_inode = fuse_alloc_inode,
443 .destroy_inode = fuse_destroy_inode,
444 .read_inode = fuse_read_inode,
445 .clear_inode = fuse_clear_inode,
446 .put_super = fuse_put_super,
447 .statfs = fuse_statfs,
448 .show_options = fuse_show_options,
451 static int fuse_fill_super(struct super_block *sb, void *data, int silent)
453 struct fuse_conn *fc;
455 struct fuse_mount_data d;
459 if (!parse_fuse_opt((char *) data, &d))
462 sb->s_blocksize = PAGE_CACHE_SIZE;
463 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
464 sb->s_magic = FUSE_SUPER_MAGIC;
465 sb->s_op = &fuse_super_operations;
466 sb->s_maxbytes = MAX_LFS_FILESIZE;
472 fc = get_conn(file, sb);
478 fc->user_id = d.user_id;
479 fc->group_id = d.group_id;
480 fc->max_read = d.max_read;
481 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
482 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
485 root = get_root_inode(sb, d.rootmode);
489 sb->s_root = d_alloc_root(root);
498 spin_lock(&fuse_lock);
499 fuse_release_conn(fc);
500 spin_unlock(&fuse_lock);
504 static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
505 int flags, const char *dev_name,
508 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
511 static struct file_system_type fuse_fs_type = {
512 .owner = THIS_MODULE,
514 .get_sb = fuse_get_sb,
515 .kill_sb = kill_anon_super,
518 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
521 struct inode * inode = foo;
523 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
524 SLAB_CTOR_CONSTRUCTOR)
525 inode_init_once(inode);
528 static int __init fuse_fs_init(void)
532 err = register_filesystem(&fuse_fs_type);
534 printk("fuse: failed to register filesystem\n");
536 fuse_inode_cachep = kmem_cache_create("fuse_inode",
537 sizeof(struct fuse_inode),
538 0, SLAB_HWCACHE_ALIGN,
539 fuse_inode_init_once, NULL);
540 if (!fuse_inode_cachep) {
541 unregister_filesystem(&fuse_fs_type);
549 static void fuse_fs_cleanup(void)
551 unregister_filesystem(&fuse_fs_type);
552 kmem_cache_destroy(fuse_inode_cachep);
555 static int __init fuse_init(void)
559 printk("fuse init (API version %i.%i)\n",
560 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
562 spin_lock_init(&fuse_lock);
563 res = fuse_fs_init();
567 res = fuse_dev_init();
579 static void __exit fuse_exit(void)
581 printk(KERN_DEBUG "fuse exit\n");
587 module_init(fuse_init);
588 module_exit(fuse_exit);