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;
55 fi
->forget_req
= fuse_request_alloc();
56 if (!fi
->forget_req
) {
57 kmem_cache_free(fuse_inode_cachep
, inode
);
64 static void fuse_destroy_inode(struct inode
*inode
)
66 struct fuse_inode
*fi
= get_fuse_inode(inode
);
68 fuse_request_free(fi
->forget_req
);
69 kmem_cache_free(fuse_inode_cachep
, inode
);
72 static void fuse_read_inode(struct inode
*inode
)
77 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
78 unsigned long nodeid
, u64 nlookup
)
80 struct fuse_forget_in
*inarg
= &req
->misc
.forget_in
;
81 inarg
->nlookup
= nlookup
;
82 req
->in
.h
.opcode
= FUSE_FORGET
;
83 req
->in
.h
.nodeid
= nodeid
;
85 req
->in
.args
[0].size
= sizeof(struct fuse_forget_in
);
86 req
->in
.args
[0].value
= inarg
;
87 request_send_noreply(fc
, req
);
90 static void fuse_clear_inode(struct inode
*inode
)
92 struct fuse_conn
*fc
= get_fuse_conn(inode
);
94 struct fuse_inode
*fi
= get_fuse_inode(inode
);
95 fuse_send_forget(fc
, fi
->forget_req
, fi
->nodeid
, fi
->nlookup
);
96 fi
->forget_req
= NULL
;
100 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
)
102 if (S_ISREG(inode
->i_mode
) && i_size_read(inode
) != attr
->size
)
103 invalidate_inode_pages(inode
->i_mapping
);
105 inode
->i_ino
= attr
->ino
;
106 inode
->i_mode
= (inode
->i_mode
& S_IFMT
) + (attr
->mode
& 07777);
107 inode
->i_nlink
= attr
->nlink
;
108 inode
->i_uid
= attr
->uid
;
109 inode
->i_gid
= attr
->gid
;
110 i_size_write(inode
, attr
->size
);
111 inode
->i_blksize
= PAGE_CACHE_SIZE
;
112 inode
->i_blocks
= attr
->blocks
;
113 inode
->i_atime
.tv_sec
= attr
->atime
;
114 inode
->i_atime
.tv_nsec
= attr
->atimensec
;
115 inode
->i_mtime
.tv_sec
= attr
->mtime
;
116 inode
->i_mtime
.tv_nsec
= attr
->mtimensec
;
117 inode
->i_ctime
.tv_sec
= attr
->ctime
;
118 inode
->i_ctime
.tv_nsec
= attr
->ctimensec
;
121 static void fuse_init_inode(struct inode
*inode
, struct fuse_attr
*attr
)
123 inode
->i_mode
= attr
->mode
& S_IFMT
;
124 i_size_write(inode
, attr
->size
);
125 if (S_ISREG(inode
->i_mode
)) {
126 fuse_init_common(inode
);
127 fuse_init_file_inode(inode
);
128 } else if (S_ISDIR(inode
->i_mode
))
129 fuse_init_dir(inode
);
130 else if (S_ISLNK(inode
->i_mode
))
131 fuse_init_symlink(inode
);
132 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
133 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
134 fuse_init_common(inode
);
135 init_special_inode(inode
, inode
->i_mode
,
136 new_decode_dev(attr
->rdev
));
138 /* Don't let user create weird files */
139 inode
->i_mode
= S_IFREG
;
140 fuse_init_common(inode
);
141 fuse_init_file_inode(inode
);
145 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
147 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
148 if (get_node_id(inode
) == nodeid
)
154 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
156 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
157 get_fuse_inode(inode
)->nodeid
= nodeid
;
161 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
162 int generation
, struct fuse_attr
*attr
)
165 struct fuse_inode
*fi
;
166 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
170 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
174 if ((inode
->i_state
& I_NEW
)) {
175 inode
->i_generation
= generation
;
176 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
177 fuse_init_inode(inode
, attr
);
178 unlock_new_inode(inode
);
179 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
181 /* Inode has changed type, any I/O on the old should fail */
182 make_bad_inode(inode
);
188 fi
= get_fuse_inode(inode
);
190 fuse_change_attributes(inode
, attr
);
194 static void fuse_put_super(struct super_block
*sb
)
196 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
198 spin_lock(&fuse_lock
);
202 /* Flush all readers on this fs */
203 wake_up_all(&fc
->waitq
);
204 fuse_release_conn(fc
);
205 *get_fuse_conn_super_p(sb
) = NULL
;
206 spin_unlock(&fuse_lock
);
209 static void convert_fuse_statfs(struct kstatfs
*stbuf
, struct fuse_kstatfs
*attr
)
211 stbuf
->f_type
= FUSE_SUPER_MAGIC
;
212 stbuf
->f_bsize
= attr
->bsize
;
213 stbuf
->f_blocks
= attr
->blocks
;
214 stbuf
->f_bfree
= attr
->bfree
;
215 stbuf
->f_bavail
= attr
->bavail
;
216 stbuf
->f_files
= attr
->files
;
217 stbuf
->f_ffree
= attr
->ffree
;
218 stbuf
->f_namelen
= attr
->namelen
;
219 /* fsid is left zero */
222 static int fuse_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
224 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
225 struct fuse_req
*req
;
226 struct fuse_statfs_out outarg
;
229 req
= fuse_get_request(fc
);
234 req
->in
.h
.opcode
= FUSE_STATFS
;
235 req
->out
.numargs
= 1;
236 req
->out
.args
[0].size
= sizeof(outarg
);
237 req
->out
.args
[0].value
= &outarg
;
238 request_send(fc
, req
);
239 err
= req
->out
.h
.error
;
241 convert_fuse_statfs(buf
, &outarg
.st
);
242 fuse_put_request(fc
, req
);
250 OPT_DEFAULT_PERMISSIONS
,
257 static match_table_t tokens
= {
259 {OPT_ROOTMODE
, "rootmode=%o"},
260 {OPT_USER_ID
, "user_id=%u"},
261 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
262 {OPT_ALLOW_OTHER
, "allow_other"},
263 {OPT_ALLOW_ROOT
, "allow_root"},
264 {OPT_KERNEL_CACHE
, "kernel_cache"},
268 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
271 memset(d
, 0, sizeof(struct fuse_mount_data
));
274 while ((p
= strsep(&opt
, ",")) != NULL
) {
277 substring_t args
[MAX_OPT_ARGS
];
281 token
= match_token(p
, tokens
, args
);
284 if (match_int(&args
[0], &value
))
290 if (match_octal(&args
[0], &value
))
296 if (match_int(&args
[0], &value
))
311 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
313 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
315 seq_printf(m
, ",user_id=%u", fc
->user_id
);
319 static void free_conn(struct fuse_conn
*fc
)
321 while (!list_empty(&fc
->unused_list
)) {
322 struct fuse_req
*req
;
323 req
= list_entry(fc
->unused_list
.next
, struct fuse_req
, list
);
324 list_del(&req
->list
);
325 fuse_request_free(req
);
330 /* Must be called with the fuse lock held */
331 void fuse_release_conn(struct fuse_conn
*fc
)
333 if (!fc
->sb
&& !fc
->file
)
337 static struct fuse_conn
*new_conn(void)
339 struct fuse_conn
*fc
;
341 fc
= kmalloc(sizeof(*fc
), GFP_KERNEL
);
344 memset(fc
, 0, sizeof(*fc
));
348 init_waitqueue_head(&fc
->waitq
);
349 INIT_LIST_HEAD(&fc
->pending
);
350 INIT_LIST_HEAD(&fc
->processing
);
351 INIT_LIST_HEAD(&fc
->unused_list
);
352 sema_init(&fc
->outstanding_sem
, 0);
353 for (i
= 0; i
< FUSE_MAX_OUTSTANDING
; i
++) {
354 struct fuse_req
*req
= fuse_request_alloc();
359 list_add(&req
->list
, &fc
->unused_list
);
361 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
362 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
368 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
370 struct fuse_conn
*fc
;
372 if (file
->f_op
!= &fuse_dev_operations
)
373 return ERR_PTR(-EINVAL
);
376 return ERR_PTR(-ENOMEM
);
377 spin_lock(&fuse_lock
);
378 if (file
->private_data
) {
380 fc
= ERR_PTR(-EINVAL
);
382 file
->private_data
= fc
;
386 spin_unlock(&fuse_lock
);
390 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
392 struct fuse_attr attr
;
393 memset(&attr
, 0, sizeof(attr
));
396 attr
.ino
= FUSE_ROOT_ID
;
397 return fuse_iget(sb
, 1, 0, &attr
);
400 static struct super_operations fuse_super_operations
= {
401 .alloc_inode
= fuse_alloc_inode
,
402 .destroy_inode
= fuse_destroy_inode
,
403 .read_inode
= fuse_read_inode
,
404 .clear_inode
= fuse_clear_inode
,
405 .put_super
= fuse_put_super
,
406 .statfs
= fuse_statfs
,
407 .show_options
= fuse_show_options
,
410 static int inc_mount_count(void)
413 spin_lock(&fuse_lock
);
415 if (mount_max
== -1 || mount_count
<= mount_max
)
417 spin_unlock(&fuse_lock
);
421 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
423 struct fuse_conn
*fc
;
425 struct fuse_mount_data d
;
429 if (!parse_fuse_opt((char *) data
, &d
))
432 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
433 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
434 sb
->s_magic
= FUSE_SUPER_MAGIC
;
435 sb
->s_op
= &fuse_super_operations
;
436 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
442 fc
= get_conn(file
, sb
);
447 fc
->user_id
= d
.user_id
;
449 *get_fuse_conn_super_p(sb
) = fc
;
452 if (!inc_mount_count() && current
->uid
!= 0)
456 root
= get_root_inode(sb
, d
.rootmode
);
460 sb
->s_root
= d_alloc_root(root
);
469 spin_lock(&fuse_lock
);
472 fuse_release_conn(fc
);
473 spin_unlock(&fuse_lock
);
474 *get_fuse_conn_super_p(sb
) = NULL
;
478 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
479 int flags
, const char *dev_name
,
482 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
485 static struct file_system_type fuse_fs_type
= {
486 .owner
= THIS_MODULE
,
488 .get_sb
= fuse_get_sb
,
489 .kill_sb
= kill_anon_super
,
492 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
495 struct inode
* inode
= foo
;
497 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
498 SLAB_CTOR_CONSTRUCTOR
)
499 inode_init_once(inode
);
502 static int __init
fuse_fs_init(void)
506 err
= register_filesystem(&fuse_fs_type
);
508 printk("fuse: failed to register filesystem\n");
510 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
511 sizeof(struct fuse_inode
),
512 0, SLAB_HWCACHE_ALIGN
,
513 fuse_inode_init_once
, NULL
);
514 if (!fuse_inode_cachep
) {
515 unregister_filesystem(&fuse_fs_type
);
523 static void fuse_fs_cleanup(void)
525 unregister_filesystem(&fuse_fs_type
);
526 kmem_cache_destroy(fuse_inode_cachep
);
529 static int __init
fuse_init(void)
533 printk("fuse init (API version %i.%i)\n",
534 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
536 spin_lock_init(&fuse_lock
);
537 res
= fuse_fs_init();
541 res
= fuse_dev_init();
553 static void __exit
fuse_exit(void)
555 printk(KERN_DEBUG
"fuse exit\n");
561 module_init(fuse_init
);
562 module_exit(fuse_exit
);