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 fuse_release_conn(fc
);
155 *get_fuse_conn_super_p(sb
) = NULL
;
156 spin_unlock(&fuse_lock
);
163 OPT_DEFAULT_PERMISSIONS
,
170 static match_table_t tokens
= {
172 {OPT_ROOTMODE
, "rootmode=%o"},
173 {OPT_USER_ID
, "user_id=%u"},
174 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
175 {OPT_ALLOW_OTHER
, "allow_other"},
176 {OPT_ALLOW_ROOT
, "allow_root"},
177 {OPT_KERNEL_CACHE
, "kernel_cache"},
181 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
184 memset(d
, 0, sizeof(struct fuse_mount_data
));
187 while ((p
= strsep(&opt
, ",")) != NULL
) {
190 substring_t args
[MAX_OPT_ARGS
];
194 token
= match_token(p
, tokens
, args
);
197 if (match_int(&args
[0], &value
))
203 if (match_octal(&args
[0], &value
))
209 if (match_int(&args
[0], &value
))
224 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
226 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
228 seq_printf(m
, ",user_id=%u", fc
->user_id
);
232 void fuse_release_conn(struct fuse_conn
*fc
)
237 static struct fuse_conn
*new_conn(void)
239 struct fuse_conn
*fc
;
241 fc
= kmalloc(sizeof(*fc
), GFP_KERNEL
);
243 memset(fc
, 0, sizeof(*fc
));
246 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
247 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
252 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
254 struct fuse_conn
*fc
;
259 spin_lock(&fuse_lock
);
261 spin_unlock(&fuse_lock
);
265 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
267 struct fuse_attr attr
;
268 memset(&attr
, 0, sizeof(attr
));
271 attr
.ino
= FUSE_ROOT_ID
;
272 return fuse_iget(sb
, 1, 0, &attr
, 0);
275 static struct super_operations fuse_super_operations
= {
276 .alloc_inode
= fuse_alloc_inode
,
277 .destroy_inode
= fuse_destroy_inode
,
278 .read_inode
= fuse_read_inode
,
279 .clear_inode
= fuse_clear_inode
,
280 .put_super
= fuse_put_super
,
281 .show_options
= fuse_show_options
,
284 static int inc_mount_count(void)
287 spin_lock(&fuse_lock
);
289 if (mount_max
== -1 || mount_count
<= mount_max
)
291 spin_unlock(&fuse_lock
);
295 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
297 struct fuse_conn
*fc
;
299 struct fuse_mount_data d
;
303 if (!parse_fuse_opt((char *) data
, &d
))
306 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
307 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
308 sb
->s_magic
= FUSE_SUPER_MAGIC
;
309 sb
->s_op
= &fuse_super_operations
;
310 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
316 fc
= get_conn(file
, sb
);
321 fc
->user_id
= d
.user_id
;
323 *get_fuse_conn_super_p(sb
) = fc
;
326 if (!inc_mount_count() && current
->uid
!= 0)
330 root
= get_root_inode(sb
, d
.rootmode
);
334 sb
->s_root
= d_alloc_root(root
);
342 spin_lock(&fuse_lock
);
345 fuse_release_conn(fc
);
346 spin_unlock(&fuse_lock
);
347 *get_fuse_conn_super_p(sb
) = NULL
;
351 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
352 int flags
, const char *dev_name
,
355 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
358 static struct file_system_type fuse_fs_type
= {
359 .owner
= THIS_MODULE
,
361 .get_sb
= fuse_get_sb
,
362 .kill_sb
= kill_anon_super
,
365 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
368 struct inode
* inode
= foo
;
370 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
371 SLAB_CTOR_CONSTRUCTOR
)
372 inode_init_once(inode
);
375 static int __init
fuse_fs_init(void)
379 err
= register_filesystem(&fuse_fs_type
);
381 printk("fuse: failed to register filesystem\n");
383 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
384 sizeof(struct fuse_inode
),
385 0, SLAB_HWCACHE_ALIGN
,
386 fuse_inode_init_once
, NULL
);
387 if (!fuse_inode_cachep
) {
388 unregister_filesystem(&fuse_fs_type
);
396 static void fuse_fs_cleanup(void)
398 unregister_filesystem(&fuse_fs_type
);
399 kmem_cache_destroy(fuse_inode_cachep
);
402 static int __init
fuse_init(void)
406 printk("fuse init (API version %i.%i)\n",
407 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
409 spin_lock_init(&fuse_lock
);
410 res
= fuse_fs_init();
420 static void __exit
fuse_exit(void)
422 printk(KERN_DEBUG
"fuse exit\n");
427 module_init(fuse_init
);
428 module_exit(fuse_exit
);