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 } else if (S_ISDIR(inode
->i_mode
))
128 fuse_init_dir(inode
);
129 else if (S_ISLNK(inode
->i_mode
))
130 fuse_init_symlink(inode
);
131 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
132 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
133 fuse_init_common(inode
);
134 init_special_inode(inode
, inode
->i_mode
,
135 new_decode_dev(attr
->rdev
));
137 /* Don't let user create weird files */
138 inode
->i_mode
= S_IFREG
;
139 fuse_init_common(inode
);
143 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
145 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
146 if (get_node_id(inode
) == nodeid
)
152 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
154 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
155 get_fuse_inode(inode
)->nodeid
= nodeid
;
159 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
160 int generation
, struct fuse_attr
*attr
)
163 struct fuse_inode
*fi
;
164 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
168 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
172 if ((inode
->i_state
& I_NEW
)) {
173 inode
->i_generation
= generation
;
174 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
175 fuse_init_inode(inode
, attr
);
176 unlock_new_inode(inode
);
177 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
179 /* Inode has changed type, any I/O on the old should fail */
180 make_bad_inode(inode
);
186 fi
= get_fuse_inode(inode
);
188 fuse_change_attributes(inode
, attr
);
192 static void fuse_put_super(struct super_block
*sb
)
194 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
196 spin_lock(&fuse_lock
);
200 /* Flush all readers on this fs */
201 wake_up_all(&fc
->waitq
);
202 fuse_release_conn(fc
);
203 *get_fuse_conn_super_p(sb
) = NULL
;
204 spin_unlock(&fuse_lock
);
207 static void convert_fuse_statfs(struct kstatfs
*stbuf
, struct fuse_kstatfs
*attr
)
209 stbuf
->f_type
= FUSE_SUPER_MAGIC
;
210 stbuf
->f_bsize
= attr
->bsize
;
211 stbuf
->f_blocks
= attr
->blocks
;
212 stbuf
->f_bfree
= attr
->bfree
;
213 stbuf
->f_bavail
= attr
->bavail
;
214 stbuf
->f_files
= attr
->files
;
215 stbuf
->f_ffree
= attr
->ffree
;
216 stbuf
->f_namelen
= attr
->namelen
;
217 /* fsid is left zero */
220 static int fuse_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
222 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
223 struct fuse_req
*req
;
224 struct fuse_statfs_out outarg
;
227 req
= fuse_get_request(fc
);
232 req
->in
.h
.opcode
= FUSE_STATFS
;
233 req
->out
.numargs
= 1;
234 req
->out
.args
[0].size
= sizeof(outarg
);
235 req
->out
.args
[0].value
= &outarg
;
236 request_send(fc
, req
);
237 err
= req
->out
.h
.error
;
239 convert_fuse_statfs(buf
, &outarg
.st
);
240 fuse_put_request(fc
, req
);
248 OPT_DEFAULT_PERMISSIONS
,
255 static match_table_t tokens
= {
257 {OPT_ROOTMODE
, "rootmode=%o"},
258 {OPT_USER_ID
, "user_id=%u"},
259 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
260 {OPT_ALLOW_OTHER
, "allow_other"},
261 {OPT_ALLOW_ROOT
, "allow_root"},
262 {OPT_KERNEL_CACHE
, "kernel_cache"},
266 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
269 memset(d
, 0, sizeof(struct fuse_mount_data
));
272 while ((p
= strsep(&opt
, ",")) != NULL
) {
275 substring_t args
[MAX_OPT_ARGS
];
279 token
= match_token(p
, tokens
, args
);
282 if (match_int(&args
[0], &value
))
288 if (match_octal(&args
[0], &value
))
294 if (match_int(&args
[0], &value
))
309 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
311 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
313 seq_printf(m
, ",user_id=%u", fc
->user_id
);
317 static void free_conn(struct fuse_conn
*fc
)
319 while (!list_empty(&fc
->unused_list
)) {
320 struct fuse_req
*req
;
321 req
= list_entry(fc
->unused_list
.next
, struct fuse_req
, list
);
322 list_del(&req
->list
);
323 fuse_request_free(req
);
328 /* Must be called with the fuse lock held */
329 void fuse_release_conn(struct fuse_conn
*fc
)
331 if (!fc
->sb
&& !fc
->file
)
335 static struct fuse_conn
*new_conn(void)
337 struct fuse_conn
*fc
;
339 fc
= kmalloc(sizeof(*fc
), GFP_KERNEL
);
342 memset(fc
, 0, sizeof(*fc
));
346 init_waitqueue_head(&fc
->waitq
);
347 INIT_LIST_HEAD(&fc
->pending
);
348 INIT_LIST_HEAD(&fc
->processing
);
349 INIT_LIST_HEAD(&fc
->unused_list
);
350 sema_init(&fc
->outstanding_sem
, 0);
351 for (i
= 0; i
< FUSE_MAX_OUTSTANDING
; i
++) {
352 struct fuse_req
*req
= fuse_request_alloc();
357 list_add(&req
->list
, &fc
->unused_list
);
359 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
360 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
366 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
368 struct fuse_conn
*fc
;
370 if (file
->f_op
!= &fuse_dev_operations
)
371 return ERR_PTR(-EINVAL
);
374 return ERR_PTR(-ENOMEM
);
375 spin_lock(&fuse_lock
);
376 if (file
->private_data
) {
378 fc
= ERR_PTR(-EINVAL
);
380 file
->private_data
= fc
;
384 spin_unlock(&fuse_lock
);
388 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
390 struct fuse_attr attr
;
391 memset(&attr
, 0, sizeof(attr
));
394 attr
.ino
= FUSE_ROOT_ID
;
395 return fuse_iget(sb
, 1, 0, &attr
);
398 static struct super_operations fuse_super_operations
= {
399 .alloc_inode
= fuse_alloc_inode
,
400 .destroy_inode
= fuse_destroy_inode
,
401 .read_inode
= fuse_read_inode
,
402 .clear_inode
= fuse_clear_inode
,
403 .put_super
= fuse_put_super
,
404 .statfs
= fuse_statfs
,
405 .show_options
= fuse_show_options
,
408 static int inc_mount_count(void)
411 spin_lock(&fuse_lock
);
413 if (mount_max
== -1 || mount_count
<= mount_max
)
415 spin_unlock(&fuse_lock
);
419 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
421 struct fuse_conn
*fc
;
423 struct fuse_mount_data d
;
427 if (!parse_fuse_opt((char *) data
, &d
))
430 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
431 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
432 sb
->s_magic
= FUSE_SUPER_MAGIC
;
433 sb
->s_op
= &fuse_super_operations
;
434 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
440 fc
= get_conn(file
, sb
);
445 fc
->user_id
= d
.user_id
;
447 *get_fuse_conn_super_p(sb
) = fc
;
450 if (!inc_mount_count() && current
->uid
!= 0)
454 root
= get_root_inode(sb
, d
.rootmode
);
458 sb
->s_root
= d_alloc_root(root
);
467 spin_lock(&fuse_lock
);
470 fuse_release_conn(fc
);
471 spin_unlock(&fuse_lock
);
472 *get_fuse_conn_super_p(sb
) = NULL
;
476 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
477 int flags
, const char *dev_name
,
480 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
483 static struct file_system_type fuse_fs_type
= {
484 .owner
= THIS_MODULE
,
486 .get_sb
= fuse_get_sb
,
487 .kill_sb
= kill_anon_super
,
490 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
493 struct inode
* inode
= foo
;
495 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
496 SLAB_CTOR_CONSTRUCTOR
)
497 inode_init_once(inode
);
500 static int __init
fuse_fs_init(void)
504 err
= register_filesystem(&fuse_fs_type
);
506 printk("fuse: failed to register filesystem\n");
508 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
509 sizeof(struct fuse_inode
),
510 0, SLAB_HWCACHE_ALIGN
,
511 fuse_inode_init_once
, NULL
);
512 if (!fuse_inode_cachep
) {
513 unregister_filesystem(&fuse_fs_type
);
521 static void fuse_fs_cleanup(void)
523 unregister_filesystem(&fuse_fs_type
);
524 kmem_cache_destroy(fuse_inode_cachep
);
527 static int __init
fuse_init(void)
531 printk("fuse init (API version %i.%i)\n",
532 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
534 spin_lock_init(&fuse_lock
);
535 res
= fuse_fs_init();
539 res
= fuse_dev_init();
551 static void __exit
fuse_exit(void)
553 printk(KERN_DEBUG
"fuse exit\n");
559 module_init(fuse_init
);
560 module_exit(fuse_exit
);