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
{
38 static struct inode
*fuse_alloc_inode(struct super_block
*sb
)
41 struct fuse_inode
*fi
;
43 inode
= kmem_cache_alloc(fuse_inode_cachep
, SLAB_KERNEL
);
47 fi
= get_fuse_inode(inode
);
48 fi
->i_time
= jiffies
- 1;
51 fi
->forget_req
= fuse_request_alloc();
52 if (!fi
->forget_req
) {
53 kmem_cache_free(fuse_inode_cachep
, inode
);
60 static void fuse_destroy_inode(struct inode
*inode
)
62 struct fuse_inode
*fi
= get_fuse_inode(inode
);
64 fuse_request_free(fi
->forget_req
);
65 kmem_cache_free(fuse_inode_cachep
, inode
);
68 static void fuse_read_inode(struct inode
*inode
)
73 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
74 unsigned long nodeid
, u64 nlookup
)
76 struct fuse_forget_in
*inarg
= &req
->misc
.forget_in
;
77 inarg
->nlookup
= nlookup
;
78 req
->in
.h
.opcode
= FUSE_FORGET
;
79 req
->in
.h
.nodeid
= nodeid
;
81 req
->in
.args
[0].size
= sizeof(struct fuse_forget_in
);
82 req
->in
.args
[0].value
= inarg
;
83 request_send_noreply(fc
, req
);
86 static void fuse_clear_inode(struct inode
*inode
)
88 if (inode
->i_sb
->s_flags
& MS_ACTIVE
) {
89 struct fuse_conn
*fc
= get_fuse_conn(inode
);
90 struct fuse_inode
*fi
= get_fuse_inode(inode
);
91 fuse_send_forget(fc
, fi
->forget_req
, fi
->nodeid
, fi
->nlookup
);
92 fi
->forget_req
= NULL
;
96 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
)
98 if (S_ISREG(inode
->i_mode
) && i_size_read(inode
) != attr
->size
)
99 invalidate_inode_pages(inode
->i_mapping
);
101 inode
->i_ino
= attr
->ino
;
102 inode
->i_mode
= (inode
->i_mode
& S_IFMT
) + (attr
->mode
& 07777);
103 inode
->i_nlink
= attr
->nlink
;
104 inode
->i_uid
= attr
->uid
;
105 inode
->i_gid
= attr
->gid
;
106 i_size_write(inode
, attr
->size
);
107 inode
->i_blksize
= PAGE_CACHE_SIZE
;
108 inode
->i_blocks
= attr
->blocks
;
109 inode
->i_atime
.tv_sec
= attr
->atime
;
110 inode
->i_atime
.tv_nsec
= attr
->atimensec
;
111 inode
->i_mtime
.tv_sec
= attr
->mtime
;
112 inode
->i_mtime
.tv_nsec
= attr
->mtimensec
;
113 inode
->i_ctime
.tv_sec
= attr
->ctime
;
114 inode
->i_ctime
.tv_nsec
= attr
->ctimensec
;
117 static void fuse_init_inode(struct inode
*inode
, struct fuse_attr
*attr
)
119 inode
->i_mode
= attr
->mode
& S_IFMT
;
120 i_size_write(inode
, attr
->size
);
121 if (S_ISREG(inode
->i_mode
)) {
122 fuse_init_common(inode
);
123 fuse_init_file_inode(inode
);
124 } else if (S_ISDIR(inode
->i_mode
))
125 fuse_init_dir(inode
);
126 else if (S_ISLNK(inode
->i_mode
))
127 fuse_init_symlink(inode
);
128 else if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
) ||
129 S_ISFIFO(inode
->i_mode
) || S_ISSOCK(inode
->i_mode
)) {
130 fuse_init_common(inode
);
131 init_special_inode(inode
, inode
->i_mode
,
132 new_decode_dev(attr
->rdev
));
134 /* Don't let user create weird files */
135 inode
->i_mode
= S_IFREG
;
136 fuse_init_common(inode
);
137 fuse_init_file_inode(inode
);
141 static int fuse_inode_eq(struct inode
*inode
, void *_nodeidp
)
143 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
144 if (get_node_id(inode
) == nodeid
)
150 static int fuse_inode_set(struct inode
*inode
, void *_nodeidp
)
152 unsigned long nodeid
= *(unsigned long *) _nodeidp
;
153 get_fuse_inode(inode
)->nodeid
= nodeid
;
157 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
158 int generation
, struct fuse_attr
*attr
)
161 struct fuse_inode
*fi
;
162 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
166 inode
= iget5_locked(sb
, nodeid
, fuse_inode_eq
, fuse_inode_set
, &nodeid
);
170 if ((inode
->i_state
& I_NEW
)) {
171 inode
->i_generation
= generation
;
172 inode
->i_data
.backing_dev_info
= &fc
->bdi
;
173 fuse_init_inode(inode
, attr
);
174 unlock_new_inode(inode
);
175 } else if ((inode
->i_mode
^ attr
->mode
) & S_IFMT
) {
177 /* Inode has changed type, any I/O on the old should fail */
178 make_bad_inode(inode
);
184 fi
= get_fuse_inode(inode
);
186 fuse_change_attributes(inode
, attr
);
190 static void fuse_put_super(struct super_block
*sb
)
192 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
194 down_write(&fc
->sbput_sem
);
195 while (!list_empty(&fc
->background
))
196 fuse_release_background(list_entry(fc
->background
.next
,
197 struct fuse_req
, bg_entry
));
199 spin_lock(&fuse_lock
);
203 /* Flush all readers on this fs */
204 wake_up_all(&fc
->waitq
);
205 up_write(&fc
->sbput_sem
);
206 fuse_release_conn(fc
);
207 spin_unlock(&fuse_lock
);
210 static void convert_fuse_statfs(struct kstatfs
*stbuf
, struct fuse_kstatfs
*attr
)
212 stbuf
->f_type
= FUSE_SUPER_MAGIC
;
213 stbuf
->f_bsize
= attr
->bsize
;
214 stbuf
->f_blocks
= attr
->blocks
;
215 stbuf
->f_bfree
= attr
->bfree
;
216 stbuf
->f_bavail
= attr
->bavail
;
217 stbuf
->f_files
= attr
->files
;
218 stbuf
->f_ffree
= attr
->ffree
;
219 stbuf
->f_namelen
= attr
->namelen
;
220 /* fsid is left zero */
223 static int fuse_statfs(struct super_block
*sb
, struct kstatfs
*buf
)
225 struct fuse_conn
*fc
= get_fuse_conn_super(sb
);
226 struct fuse_req
*req
;
227 struct fuse_statfs_out outarg
;
230 req
= fuse_get_request(fc
);
235 req
->in
.h
.opcode
= FUSE_STATFS
;
236 req
->out
.numargs
= 1;
237 req
->out
.args
[0].size
= sizeof(outarg
);
238 req
->out
.args
[0].value
= &outarg
;
239 request_send(fc
, req
);
240 err
= req
->out
.h
.error
;
242 convert_fuse_statfs(buf
, &outarg
.st
);
243 fuse_put_request(fc
, req
);
251 OPT_DEFAULT_PERMISSIONS
,
258 static match_table_t tokens
= {
260 {OPT_ROOTMODE
, "rootmode=%o"},
261 {OPT_USER_ID
, "user_id=%u"},
262 {OPT_DEFAULT_PERMISSIONS
, "default_permissions"},
263 {OPT_ALLOW_OTHER
, "allow_other"},
264 {OPT_KERNEL_CACHE
, "kernel_cache"},
265 {OPT_MAX_READ
, "max_read=%u"},
269 static int parse_fuse_opt(char *opt
, struct fuse_mount_data
*d
)
272 memset(d
, 0, sizeof(struct fuse_mount_data
));
276 while ((p
= strsep(&opt
, ",")) != NULL
) {
279 substring_t args
[MAX_OPT_ARGS
];
283 token
= match_token(p
, tokens
, args
);
286 if (match_int(&args
[0], &value
))
292 if (match_octal(&args
[0], &value
))
298 if (match_int(&args
[0], &value
))
303 case OPT_DEFAULT_PERMISSIONS
:
304 d
->flags
|= FUSE_DEFAULT_PERMISSIONS
;
307 case OPT_ALLOW_OTHER
:
308 d
->flags
|= FUSE_ALLOW_OTHER
;
311 case OPT_KERNEL_CACHE
:
312 d
->flags
|= FUSE_KERNEL_CACHE
;
316 if (match_int(&args
[0], &value
))
331 static int fuse_show_options(struct seq_file
*m
, struct vfsmount
*mnt
)
333 struct fuse_conn
*fc
= get_fuse_conn_super(mnt
->mnt_sb
);
335 seq_printf(m
, ",user_id=%u", fc
->user_id
);
336 if (fc
->flags
& FUSE_DEFAULT_PERMISSIONS
)
337 seq_puts(m
, ",default_permissions");
338 if (fc
->flags
& FUSE_ALLOW_OTHER
)
339 seq_puts(m
, ",allow_other");
340 if (fc
->flags
& FUSE_KERNEL_CACHE
)
341 seq_puts(m
, ",kernel_cache");
342 if (fc
->max_read
!= ~0)
343 seq_printf(m
, ",max_read=%u", fc
->max_read
);
347 static void free_conn(struct fuse_conn
*fc
)
349 while (!list_empty(&fc
->unused_list
)) {
350 struct fuse_req
*req
;
351 req
= list_entry(fc
->unused_list
.next
, struct fuse_req
, list
);
352 list_del(&req
->list
);
353 fuse_request_free(req
);
358 /* Must be called with the fuse lock held */
359 void fuse_release_conn(struct fuse_conn
*fc
)
366 static struct fuse_conn
*new_conn(void)
368 struct fuse_conn
*fc
;
370 fc
= kmalloc(sizeof(*fc
), GFP_KERNEL
);
373 memset(fc
, 0, sizeof(*fc
));
374 init_waitqueue_head(&fc
->waitq
);
375 INIT_LIST_HEAD(&fc
->pending
);
376 INIT_LIST_HEAD(&fc
->processing
);
377 INIT_LIST_HEAD(&fc
->unused_list
);
378 INIT_LIST_HEAD(&fc
->background
);
379 sema_init(&fc
->outstanding_sem
, 0);
380 init_rwsem(&fc
->sbput_sem
);
381 for (i
= 0; i
< FUSE_MAX_OUTSTANDING
; i
++) {
382 struct fuse_req
*req
= fuse_request_alloc();
387 list_add(&req
->list
, &fc
->unused_list
);
389 fc
->bdi
.ra_pages
= (VM_MAX_READAHEAD
* 1024) / PAGE_CACHE_SIZE
;
390 fc
->bdi
.unplug_io_fn
= default_unplug_io_fn
;
396 static struct fuse_conn
*get_conn(struct file
*file
, struct super_block
*sb
)
398 struct fuse_conn
*fc
;
400 if (file
->f_op
!= &fuse_dev_operations
)
401 return ERR_PTR(-EINVAL
);
404 return ERR_PTR(-ENOMEM
);
405 spin_lock(&fuse_lock
);
406 if (file
->private_data
) {
408 fc
= ERR_PTR(-EINVAL
);
410 file
->private_data
= fc
;
411 *get_fuse_conn_super_p(sb
) = fc
;
416 spin_unlock(&fuse_lock
);
420 static struct inode
*get_root_inode(struct super_block
*sb
, unsigned mode
)
422 struct fuse_attr attr
;
423 memset(&attr
, 0, sizeof(attr
));
426 attr
.ino
= FUSE_ROOT_ID
;
427 return fuse_iget(sb
, 1, 0, &attr
);
430 static struct super_operations fuse_super_operations
= {
431 .alloc_inode
= fuse_alloc_inode
,
432 .destroy_inode
= fuse_destroy_inode
,
433 .read_inode
= fuse_read_inode
,
434 .clear_inode
= fuse_clear_inode
,
435 .put_super
= fuse_put_super
,
436 .statfs
= fuse_statfs
,
437 .show_options
= fuse_show_options
,
440 static int fuse_fill_super(struct super_block
*sb
, void *data
, int silent
)
442 struct fuse_conn
*fc
;
444 struct fuse_mount_data d
;
448 if (!parse_fuse_opt((char *) data
, &d
))
451 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
452 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
453 sb
->s_magic
= FUSE_SUPER_MAGIC
;
454 sb
->s_op
= &fuse_super_operations
;
455 sb
->s_maxbytes
= MAX_LFS_FILESIZE
;
461 fc
= get_conn(file
, sb
);
467 fc
->user_id
= d
.user_id
;
468 fc
->max_read
= d
.max_read
;
469 if (fc
->max_read
/ PAGE_CACHE_SIZE
< fc
->bdi
.ra_pages
)
470 fc
->bdi
.ra_pages
= fc
->max_read
/ PAGE_CACHE_SIZE
;
473 root
= get_root_inode(sb
, d
.rootmode
);
477 sb
->s_root
= d_alloc_root(root
);
486 spin_lock(&fuse_lock
);
487 fuse_release_conn(fc
);
488 spin_unlock(&fuse_lock
);
492 static struct super_block
*fuse_get_sb(struct file_system_type
*fs_type
,
493 int flags
, const char *dev_name
,
496 return get_sb_nodev(fs_type
, flags
, raw_data
, fuse_fill_super
);
499 static struct file_system_type fuse_fs_type
= {
500 .owner
= THIS_MODULE
,
502 .get_sb
= fuse_get_sb
,
503 .kill_sb
= kill_anon_super
,
506 static void fuse_inode_init_once(void *foo
, kmem_cache_t
*cachep
,
509 struct inode
* inode
= foo
;
511 if ((flags
& (SLAB_CTOR_VERIFY
|SLAB_CTOR_CONSTRUCTOR
)) ==
512 SLAB_CTOR_CONSTRUCTOR
)
513 inode_init_once(inode
);
516 static int __init
fuse_fs_init(void)
520 err
= register_filesystem(&fuse_fs_type
);
522 printk("fuse: failed to register filesystem\n");
524 fuse_inode_cachep
= kmem_cache_create("fuse_inode",
525 sizeof(struct fuse_inode
),
526 0, SLAB_HWCACHE_ALIGN
,
527 fuse_inode_init_once
, NULL
);
528 if (!fuse_inode_cachep
) {
529 unregister_filesystem(&fuse_fs_type
);
537 static void fuse_fs_cleanup(void)
539 unregister_filesystem(&fuse_fs_type
);
540 kmem_cache_destroy(fuse_inode_cachep
);
543 static int __init
fuse_init(void)
547 printk("fuse init (API version %i.%i)\n",
548 FUSE_KERNEL_VERSION
, FUSE_KERNEL_MINOR_VERSION
);
550 spin_lock_init(&fuse_lock
);
551 res
= fuse_fs_init();
555 res
= fuse_dev_init();
567 static void __exit
fuse_exit(void)
569 printk(KERN_DEBUG
"fuse exit\n");
575 module_init(fuse_init
);
576 module_exit(fuse_exit
);