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
{
37 static struct inode
*fuse_alloc_inode(struct super_block
*sb
)
40 struct fuse_inode
*fi
;
42 inode
= kmem_cache_alloc(fuse_inode_cachep
, SLAB_KERNEL
);
46 fi
= get_fuse_inode(inode
);
47 fi
->i_time
= jiffies
- 1;
50 fi
->forget_req
= fuse_request_alloc();
51 if (!fi
->forget_req
) {
52 kmem_cache_free(fuse_inode_cachep
, inode
);
59 static void fuse_destroy_inode(struct inode
*inode
)
61 struct fuse_inode
*fi
= get_fuse_inode(inode
);
63 fuse_request_free(fi
->forget_req
);
64 kmem_cache_free(fuse_inode_cachep
, inode
);
67 static void fuse_read_inode(struct inode
*inode
)
72 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
73 unsigned long nodeid
, u64 nlookup
)
75 struct fuse_forget_in
*inarg
= &req
->misc
.forget_in
;
76 inarg
->nlookup
= nlookup
;
77 req
->in
.h
.opcode
= FUSE_FORGET
;
78 req
->in
.h
.nodeid
= nodeid
;
80 req
->in
.args
[0].size
= sizeof(struct fuse_forget_in
);
81 req
->in
.args
[0].value
= inarg
;
82 request_send_noreply(fc
, req
);
85 static void fuse_clear_inode(struct inode
*inode
)
87 if (inode
->i_sb
->s_flags
& MS_ACTIVE
) {
88 struct fuse_conn
*fc
= get_fuse_conn(inode
);
89 struct fuse_inode
*fi
= get_fuse_inode(inode
);
90 fuse_send_forget(fc
, fi
->forget_req
, fi
->nodeid
, fi
->nlookup
);
91 fi
->forget_req
= NULL
;
95 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
)
97 if (S_ISREG(inode
->i_mode
) && i_size_read(inode
) != attr
->size
)
98 invalidate_inode_pages(inode
->i_mapping
);
100 inode
->i_ino
= attr
->ino
;
101 inode
->i_mode
= (inode
->i_mode
& S_IFMT
) + (attr
->mode
& 07777);
102 inode
->i_nlink
= attr
->nlink
;
103 inode
->i_uid
= attr
->uid
;
104 inode
->i_gid
= attr
->gid
;
105 i_size_write(inode
, attr
->size
);
106 inode
->i_blksize
= PAGE_CACHE_SIZE
;
107 inode
->i_blocks
= attr
->blocks
;
108 inode
->i_atime
.tv_sec
= attr
->atime
;
109 inode
->i_atime
.tv_nsec
= attr
->atimensec
;
110 inode
->i_mtime
.tv_sec
= attr
->mtime
;
111 inode
->i_mtime
.tv_nsec
= attr
->mtimensec
;
112 inode
->i_ctime
.tv_sec
= attr
->ctime
;
113 inode
->i_ctime
.tv_nsec
= attr
->ctimensec
;
116 static void fuse_init_inode(struct inode
*inode
, struct fuse_attr
*attr
)
118 inode
->i_mode
= attr
->mode
& S_IFMT
;
119 i_size_write(inode
, attr
->size
);
120 if (S_ISREG(inode
->i_mode
)) {
121 fuse_init_common(inode
);
122 fuse_init_file_inode(inode
);
123 } else if (S_ISDIR(inode
->i_mode
))
124 fuse_init_dir(inode
);
125 else if (S_ISLNK(inode
->i_mode
))
126 fuse_init_symlink(inode
);
127 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
128 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
129 fuse_init_common(inode
);
130 init_special_inode(inode
, inode
->i_mode
,
131 new_decode_dev(attr
->rdev
));
133 /* Don't let user create weird files */
134 inode
->i_mode
= S_IFREG
;
135 fuse_init_common(inode
);
136 fuse_init_file_inode(inode
);
140 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
142 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
143 if (get_node_id(inode
) == nodeid
)
149 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
151 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
152 get_fuse_inode(inode
)->nodeid
= nodeid
;
156 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
157 int generation
, struct fuse_attr
*attr
)
160 struct fuse_inode
*fi
;
161 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
165 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
169 if ((inode
->i_state
& I_NEW
)) {
170 inode
->i_generation
= generation
;
171 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
172 fuse_init_inode(inode
, attr
);
173 unlock_new_inode(inode
);
174 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
176 /* Inode has changed type, any I/O on the old should fail */
177 make_bad_inode(inode
);
183 fi
= get_fuse_inode(inode
);
185 fuse_change_attributes(inode
, attr
);
189 static void fuse_put_super(struct super_block
*sb
)
191 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
193 down_write(&fc
->sbput_sem
);
194 while (!list_empty(&fc
->background
))
195 fuse_release_background(list_entry(fc
->background
.next
,
196 struct fuse_req
, bg_entry
));
198 spin_lock(&fuse_lock
);
202 /* Flush all readers on this fs */
203 wake_up_all(&fc
->waitq
);
204 up_write(&fc
->sbput_sem
);
205 fuse_release_conn(fc
);
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
,
256 static match_table_t tokens
= {
258 {OPT_ROOTMODE
, "rootmode=%o"},
259 {OPT_USER_ID
, "user_id=%u"},
260 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
261 {OPT_ALLOW_OTHER
, "allow_other"},
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
))
299 case OPT_DEFAULT_PERMISSIONS
:
300 d
->flags
|= FUSE_DEFAULT_PERMISSIONS
;
303 case OPT_ALLOW_OTHER
:
304 d
->flags
|= FUSE_ALLOW_OTHER
;
307 case OPT_KERNEL_CACHE
:
308 d
->flags
|= FUSE_KERNEL_CACHE
;
321 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
323 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
325 seq_printf(m
, ",user_id=%u", fc
->user_id
);
326 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
)
327 seq_puts(m
, ",default_permissions");
328 if (fc
->flags
& FUSE_ALLOW_OTHER
)
329 seq_puts(m
, ",allow_other");
330 if (fc
->flags
& FUSE_KERNEL_CACHE
)
331 seq_puts(m
, ",kernel_cache");
335 static void free_conn(struct fuse_conn
*fc
)
337 while (!list_empty(&fc
->unused_list
)) {
338 struct fuse_req
*req
;
339 req
= list_entry(fc
->unused_list
.next
, struct fuse_req
, list
);
340 list_del(&req
->list
);
341 fuse_request_free(req
);
346 /* Must be called with the fuse lock held */
347 void fuse_release_conn(struct fuse_conn
*fc
)
354 static struct fuse_conn
*new_conn(void)
356 struct fuse_conn
*fc
;
358 fc
= kmalloc(sizeof(*fc
), GFP_KERNEL
);
361 memset(fc
, 0, sizeof(*fc
));
362 init_waitqueue_head(&fc
->waitq
);
363 INIT_LIST_HEAD(&fc
->pending
);
364 INIT_LIST_HEAD(&fc
->processing
);
365 INIT_LIST_HEAD(&fc
->unused_list
);
366 INIT_LIST_HEAD(&fc
->background
);
367 sema_init(&fc
->outstanding_sem
, 0);
368 init_rwsem(&fc
->sbput_sem
);
369 for (i
= 0; i
< FUSE_MAX_OUTSTANDING
; i
++) {
370 struct fuse_req
*req
= fuse_request_alloc();
375 list_add(&req
->list
, &fc
->unused_list
);
377 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
378 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
384 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
386 struct fuse_conn
*fc
;
388 if (file
->f_op
!= &fuse_dev_operations
)
389 return ERR_PTR(-EINVAL
);
392 return ERR_PTR(-ENOMEM
);
393 spin_lock(&fuse_lock
);
394 if (file
->private_data
) {
396 fc
= ERR_PTR(-EINVAL
);
398 file
->private_data
= fc
;
399 *get_fuse_conn_super_p(sb
) = fc
;
404 spin_unlock(&fuse_lock
);
408 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
410 struct fuse_attr attr
;
411 memset(&attr
, 0, sizeof(attr
));
414 attr
.ino
= FUSE_ROOT_ID
;
415 return fuse_iget(sb
, 1, 0, &attr
);
418 static struct super_operations fuse_super_operations
= {
419 .alloc_inode
= fuse_alloc_inode
,
420 .destroy_inode
= fuse_destroy_inode
,
421 .read_inode
= fuse_read_inode
,
422 .clear_inode
= fuse_clear_inode
,
423 .put_super
= fuse_put_super
,
424 .statfs
= fuse_statfs
,
425 .show_options
= fuse_show_options
,
428 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
430 struct fuse_conn
*fc
;
432 struct fuse_mount_data d
;
436 if (!parse_fuse_opt((char *) data
, &d
))
439 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
440 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
441 sb
->s_magic
= FUSE_SUPER_MAGIC
;
442 sb
->s_op
= &fuse_super_operations
;
443 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
449 fc
= get_conn(file
, sb
);
455 fc
->user_id
= d
.user_id
;
458 root
= get_root_inode(sb
, d
.rootmode
);
462 sb
->s_root
= d_alloc_root(root
);
471 spin_lock(&fuse_lock
);
472 fuse_release_conn(fc
);
473 spin_unlock(&fuse_lock
);
477 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
478 int flags
, const char *dev_name
,
481 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
484 static struct file_system_type fuse_fs_type
= {
485 .owner
= THIS_MODULE
,
487 .get_sb
= fuse_get_sb
,
488 .kill_sb
= kill_anon_super
,
491 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
494 struct inode
* inode
= foo
;
496 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
497 SLAB_CTOR_CONSTRUCTOR
)
498 inode_init_once(inode
);
501 static int __init
fuse_fs_init(void)
505 err
= register_filesystem(&fuse_fs_type
);
507 printk("fuse: failed to register filesystem\n");
509 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
510 sizeof(struct fuse_inode
),
511 0, SLAB_HWCACHE_ALIGN
,
512 fuse_inode_init_once
, NULL
);
513 if (!fuse_inode_cachep
) {
514 unregister_filesystem(&fuse_fs_type
);
522 static void fuse_fs_cleanup(void)
524 unregister_filesystem(&fuse_fs_type
);
525 kmem_cache_destroy(fuse_inode_cachep
);
528 static int __init
fuse_init(void)
532 printk("fuse init (API version %i.%i)\n",
533 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
535 spin_lock_init(&fuse_lock
);
536 res
= fuse_fs_init();
540 res
= fuse_dev_init();
552 static void __exit
fuse_exit(void)
554 printk(KERN_DEBUG
"fuse exit\n");
560 module_init(fuse_init
);
561 module_exit(fuse_exit
);