[PATCH] fuse: add number of waiting requests attribute
[linux-2.6-block.git] / fs / fuse / inode.c
CommitLineData
d8a5ba45
MS
1/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
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>
d8a5ba45
MS
18#include <linux/parser.h>
19#include <linux/statfs.h>
20
21MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
22MODULE_DESCRIPTION("Filesystem in Userspace");
23MODULE_LICENSE("GPL");
24
25spinlock_t fuse_lock;
26static kmem_cache_t *fuse_inode_cachep;
f543f253
MS
27static struct subsystem connections_subsys;
28
29struct fuse_conn_attr {
30 struct attribute attr;
31 ssize_t (*show)(struct fuse_conn *, char *);
32 ssize_t (*store)(struct fuse_conn *, const char *, size_t);
33};
d8a5ba45
MS
34
35#define FUSE_SUPER_MAGIC 0x65735546
36
37struct fuse_mount_data {
38 int fd;
39 unsigned rootmode;
40 unsigned user_id;
87729a55 41 unsigned group_id;
5a533682
MS
42 unsigned fd_present : 1;
43 unsigned rootmode_present : 1;
44 unsigned user_id_present : 1;
45 unsigned group_id_present : 1;
1e9a4ed9 46 unsigned flags;
db50b96c 47 unsigned max_read;
d8a5ba45
MS
48};
49
50static struct inode *fuse_alloc_inode(struct super_block *sb)
51{
52 struct inode *inode;
53 struct fuse_inode *fi;
54
55 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
56 if (!inode)
57 return NULL;
58
59 fi = get_fuse_inode(inode);
60 fi->i_time = jiffies - 1;
61 fi->nodeid = 0;
9e6268db 62 fi->nlookup = 0;
e5e5558e
MS
63 fi->forget_req = fuse_request_alloc();
64 if (!fi->forget_req) {
65 kmem_cache_free(fuse_inode_cachep, inode);
66 return NULL;
67 }
d8a5ba45
MS
68
69 return inode;
70}
71
72static void fuse_destroy_inode(struct inode *inode)
73{
e5e5558e
MS
74 struct fuse_inode *fi = get_fuse_inode(inode);
75 if (fi->forget_req)
76 fuse_request_free(fi->forget_req);
d8a5ba45
MS
77 kmem_cache_free(fuse_inode_cachep, inode);
78}
79
80static void fuse_read_inode(struct inode *inode)
81{
82 /* No op */
83}
84
e5e5558e 85void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
9e6268db 86 unsigned long nodeid, u64 nlookup)
e5e5558e
MS
87{
88 struct fuse_forget_in *inarg = &req->misc.forget_in;
9e6268db 89 inarg->nlookup = nlookup;
e5e5558e
MS
90 req->in.h.opcode = FUSE_FORGET;
91 req->in.h.nodeid = nodeid;
92 req->in.numargs = 1;
93 req->in.args[0].size = sizeof(struct fuse_forget_in);
94 req->in.args[0].value = inarg;
95 request_send_noreply(fc, req);
96}
97
d8a5ba45
MS
98static void fuse_clear_inode(struct inode *inode)
99{
1e9a4ed9
MS
100 if (inode->i_sb->s_flags & MS_ACTIVE) {
101 struct fuse_conn *fc = get_fuse_conn(inode);
e5e5558e 102 struct fuse_inode *fi = get_fuse_inode(inode);
9e6268db 103 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
e5e5558e
MS
104 fi->forget_req = NULL;
105 }
d8a5ba45
MS
106}
107
108void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
109{
110 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
111 invalidate_inode_pages(inode->i_mapping);
112
113 inode->i_ino = attr->ino;
114 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
115 inode->i_nlink = attr->nlink;
116 inode->i_uid = attr->uid;
117 inode->i_gid = attr->gid;
118 i_size_write(inode, attr->size);
119 inode->i_blksize = PAGE_CACHE_SIZE;
120 inode->i_blocks = attr->blocks;
121 inode->i_atime.tv_sec = attr->atime;
122 inode->i_atime.tv_nsec = attr->atimensec;
123 inode->i_mtime.tv_sec = attr->mtime;
124 inode->i_mtime.tv_nsec = attr->mtimensec;
125 inode->i_ctime.tv_sec = attr->ctime;
126 inode->i_ctime.tv_nsec = attr->ctimensec;
127}
128
129static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
130{
131 inode->i_mode = attr->mode & S_IFMT;
132 i_size_write(inode, attr->size);
e5e5558e
MS
133 if (S_ISREG(inode->i_mode)) {
134 fuse_init_common(inode);
b6aeaded 135 fuse_init_file_inode(inode);
e5e5558e
MS
136 } else if (S_ISDIR(inode->i_mode))
137 fuse_init_dir(inode);
138 else if (S_ISLNK(inode->i_mode))
139 fuse_init_symlink(inode);
140 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
141 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
142 fuse_init_common(inode);
143 init_special_inode(inode, inode->i_mode,
144 new_decode_dev(attr->rdev));
39ee059a
MS
145 } else
146 BUG();
d8a5ba45
MS
147}
148
149static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
150{
151 unsigned long nodeid = *(unsigned long *) _nodeidp;
152 if (get_node_id(inode) == nodeid)
153 return 1;
154 else
155 return 0;
156}
157
158static int fuse_inode_set(struct inode *inode, void *_nodeidp)
159{
160 unsigned long nodeid = *(unsigned long *) _nodeidp;
161 get_fuse_inode(inode)->nodeid = nodeid;
162 return 0;
163}
164
165struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
9e6268db 166 int generation, struct fuse_attr *attr)
d8a5ba45
MS
167{
168 struct inode *inode;
9e6268db 169 struct fuse_inode *fi;
d8a5ba45
MS
170 struct fuse_conn *fc = get_fuse_conn_super(sb);
171 int retried = 0;
172
173 retry:
174 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
175 if (!inode)
176 return NULL;
177
178 if ((inode->i_state & I_NEW)) {
b36c31ba 179 inode->i_flags |= S_NOATIME|S_NOCMTIME;
d8a5ba45
MS
180 inode->i_generation = generation;
181 inode->i_data.backing_dev_info = &fc->bdi;
182 fuse_init_inode(inode, attr);
183 unlock_new_inode(inode);
184 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
185 BUG_ON(retried);
186 /* Inode has changed type, any I/O on the old should fail */
187 make_bad_inode(inode);
188 iput(inode);
189 retried = 1;
190 goto retry;
191 }
192
9e6268db
MS
193 fi = get_fuse_inode(inode);
194 fi->nlookup ++;
d8a5ba45 195 fuse_change_attributes(inode, attr);
d8a5ba45
MS
196 return inode;
197}
198
199static void fuse_put_super(struct super_block *sb)
200{
201 struct fuse_conn *fc = get_fuse_conn_super(sb);
202
1e9a4ed9
MS
203 down_write(&fc->sbput_sem);
204 while (!list_empty(&fc->background))
205 fuse_release_background(list_entry(fc->background.next,
206 struct fuse_req, bg_entry));
207
d8a5ba45 208 spin_lock(&fuse_lock);
1e9a4ed9 209 fc->mounted = 0;
9ba7cbba 210 fc->connected = 0;
f543f253
MS
211 spin_unlock(&fuse_lock);
212 up_write(&fc->sbput_sem);
334f485d
MS
213 /* Flush all readers on this fs */
214 wake_up_all(&fc->waitq);
f543f253
MS
215 kobject_del(&fc->kobj);
216 kobject_put(&fc->kobj);
d8a5ba45
MS
217}
218
e5e5558e
MS
219static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
220{
221 stbuf->f_type = FUSE_SUPER_MAGIC;
222 stbuf->f_bsize = attr->bsize;
de5f1202 223 stbuf->f_frsize = attr->frsize;
e5e5558e
MS
224 stbuf->f_blocks = attr->blocks;
225 stbuf->f_bfree = attr->bfree;
226 stbuf->f_bavail = attr->bavail;
227 stbuf->f_files = attr->files;
228 stbuf->f_ffree = attr->ffree;
229 stbuf->f_namelen = attr->namelen;
230 /* fsid is left zero */
231}
232
233static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
234{
235 struct fuse_conn *fc = get_fuse_conn_super(sb);
236 struct fuse_req *req;
237 struct fuse_statfs_out outarg;
238 int err;
239
240 req = fuse_get_request(fc);
241 if (!req)
7c352bdf 242 return -EINTR;
e5e5558e 243
de5f1202 244 memset(&outarg, 0, sizeof(outarg));
e5e5558e
MS
245 req->in.numargs = 0;
246 req->in.h.opcode = FUSE_STATFS;
247 req->out.numargs = 1;
de5f1202
MS
248 req->out.args[0].size =
249 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
e5e5558e
MS
250 req->out.args[0].value = &outarg;
251 request_send(fc, req);
252 err = req->out.h.error;
253 if (!err)
254 convert_fuse_statfs(buf, &outarg.st);
255 fuse_put_request(fc, req);
256 return err;
257}
258
d8a5ba45
MS
259enum {
260 OPT_FD,
261 OPT_ROOTMODE,
262 OPT_USER_ID,
87729a55 263 OPT_GROUP_ID,
d8a5ba45
MS
264 OPT_DEFAULT_PERMISSIONS,
265 OPT_ALLOW_OTHER,
db50b96c 266 OPT_MAX_READ,
d8a5ba45
MS
267 OPT_ERR
268};
269
270static match_table_t tokens = {
271 {OPT_FD, "fd=%u"},
272 {OPT_ROOTMODE, "rootmode=%o"},
273 {OPT_USER_ID, "user_id=%u"},
87729a55 274 {OPT_GROUP_ID, "group_id=%u"},
d8a5ba45
MS
275 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
276 {OPT_ALLOW_OTHER, "allow_other"},
db50b96c 277 {OPT_MAX_READ, "max_read=%u"},
d8a5ba45
MS
278 {OPT_ERR, NULL}
279};
280
281static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
282{
283 char *p;
284 memset(d, 0, sizeof(struct fuse_mount_data));
db50b96c 285 d->max_read = ~0;
d8a5ba45
MS
286
287 while ((p = strsep(&opt, ",")) != NULL) {
288 int token;
289 int value;
290 substring_t args[MAX_OPT_ARGS];
291 if (!*p)
292 continue;
293
294 token = match_token(p, tokens, args);
295 switch (token) {
296 case OPT_FD:
297 if (match_int(&args[0], &value))
298 return 0;
299 d->fd = value;
5a533682 300 d->fd_present = 1;
d8a5ba45
MS
301 break;
302
303 case OPT_ROOTMODE:
304 if (match_octal(&args[0], &value))
305 return 0;
306 d->rootmode = value;
5a533682 307 d->rootmode_present = 1;
d8a5ba45
MS
308 break;
309
310 case OPT_USER_ID:
311 if (match_int(&args[0], &value))
312 return 0;
313 d->user_id = value;
5a533682 314 d->user_id_present = 1;
d8a5ba45
MS
315 break;
316
87729a55
MS
317 case OPT_GROUP_ID:
318 if (match_int(&args[0], &value))
319 return 0;
320 d->group_id = value;
5a533682 321 d->group_id_present = 1;
87729a55
MS
322 break;
323
1e9a4ed9
MS
324 case OPT_DEFAULT_PERMISSIONS:
325 d->flags |= FUSE_DEFAULT_PERMISSIONS;
326 break;
327
328 case OPT_ALLOW_OTHER:
329 d->flags |= FUSE_ALLOW_OTHER;
330 break;
331
db50b96c
MS
332 case OPT_MAX_READ:
333 if (match_int(&args[0], &value))
334 return 0;
335 d->max_read = value;
336 break;
337
d8a5ba45
MS
338 default:
339 return 0;
340 }
341 }
5a533682
MS
342
343 if (!d->fd_present || !d->rootmode_present ||
344 !d->user_id_present || !d->group_id_present)
d8a5ba45
MS
345 return 0;
346
347 return 1;
348}
349
350static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
351{
352 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
353
354 seq_printf(m, ",user_id=%u", fc->user_id);
87729a55 355 seq_printf(m, ",group_id=%u", fc->group_id);
1e9a4ed9
MS
356 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
357 seq_puts(m, ",default_permissions");
358 if (fc->flags & FUSE_ALLOW_OTHER)
359 seq_puts(m, ",allow_other");
db50b96c
MS
360 if (fc->max_read != ~0)
361 seq_printf(m, ",max_read=%u", fc->max_read);
d8a5ba45
MS
362 return 0;
363}
364
f543f253 365static void fuse_conn_release(struct kobject *kobj)
d8a5ba45 366{
f543f253
MS
367 struct fuse_conn *fc = get_fuse_conn_kobj(kobj);
368
334f485d
MS
369 while (!list_empty(&fc->unused_list)) {
370 struct fuse_req *req;
371 req = list_entry(fc->unused_list.next, struct fuse_req, list);
372 list_del(&req->list);
373 fuse_request_free(req);
374 }
d8a5ba45
MS
375 kfree(fc);
376}
377
378static struct fuse_conn *new_conn(void)
379{
380 struct fuse_conn *fc;
381
6383bdaa 382 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
f543f253 383 if (fc) {
334f485d 384 int i;
334f485d
MS
385 init_waitqueue_head(&fc->waitq);
386 INIT_LIST_HEAD(&fc->pending);
387 INIT_LIST_HEAD(&fc->processing);
d77a1d5b 388 INIT_LIST_HEAD(&fc->io);
334f485d 389 INIT_LIST_HEAD(&fc->unused_list);
1e9a4ed9 390 INIT_LIST_HEAD(&fc->background);
6383bdaa 391 sema_init(&fc->outstanding_sem, 1); /* One for INIT */
1e9a4ed9 392 init_rwsem(&fc->sbput_sem);
f543f253
MS
393 kobj_set_kset_s(fc, connections_subsys);
394 kobject_init(&fc->kobj);
334f485d
MS
395 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
396 struct fuse_req *req = fuse_request_alloc();
397 if (!req) {
f543f253 398 kobject_put(&fc->kobj);
334f485d
MS
399 return NULL;
400 }
401 list_add(&req->list, &fc->unused_list);
402 }
d8a5ba45
MS
403 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
404 fc->bdi.unplug_io_fn = default_unplug_io_fn;
334f485d 405 fc->reqctr = 0;
d8a5ba45
MS
406 }
407 return fc;
408}
409
410static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
411{
412 struct fuse_conn *fc;
f543f253 413 int err;
d8a5ba45 414
f543f253 415 err = -EINVAL;
334f485d 416 if (file->f_op != &fuse_dev_operations)
f543f253
MS
417 goto out_err;
418
419 err = -ENOMEM;
d8a5ba45 420 fc = new_conn();
f543f253
MS
421 if (!fc)
422 goto out_err;
423
d8a5ba45 424 spin_lock(&fuse_lock);
f543f253
MS
425 err = -EINVAL;
426 if (file->private_data)
427 goto out_unlock;
428
429 kobject_get(&fc->kobj);
430 file->private_data = fc;
d8a5ba45
MS
431 spin_unlock(&fuse_lock);
432 return fc;
f543f253
MS
433
434 out_unlock:
435 spin_unlock(&fuse_lock);
436 kobject_put(&fc->kobj);
437 out_err:
438 return ERR_PTR(err);
d8a5ba45
MS
439}
440
441static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
442{
443 struct fuse_attr attr;
444 memset(&attr, 0, sizeof(attr));
445
446 attr.mode = mode;
447 attr.ino = FUSE_ROOT_ID;
9e6268db 448 return fuse_iget(sb, 1, 0, &attr);
d8a5ba45
MS
449}
450
451static struct super_operations fuse_super_operations = {
452 .alloc_inode = fuse_alloc_inode,
453 .destroy_inode = fuse_destroy_inode,
454 .read_inode = fuse_read_inode,
455 .clear_inode = fuse_clear_inode,
456 .put_super = fuse_put_super,
e5e5558e 457 .statfs = fuse_statfs,
d8a5ba45
MS
458 .show_options = fuse_show_options,
459};
460
f543f253
MS
461static unsigned long long conn_id(void)
462{
463 static unsigned long long ctr = 1;
464 unsigned long long val;
465 spin_lock(&fuse_lock);
466 val = ctr++;
467 spin_unlock(&fuse_lock);
468 return val;
469}
470
d8a5ba45
MS
471static int fuse_fill_super(struct super_block *sb, void *data, int silent)
472{
473 struct fuse_conn *fc;
474 struct inode *root;
475 struct fuse_mount_data d;
476 struct file *file;
f543f253 477 struct dentry *root_dentry;
d8a5ba45
MS
478 int err;
479
480 if (!parse_fuse_opt((char *) data, &d))
481 return -EINVAL;
482
483 sb->s_blocksize = PAGE_CACHE_SIZE;
484 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
485 sb->s_magic = FUSE_SUPER_MAGIC;
486 sb->s_op = &fuse_super_operations;
487 sb->s_maxbytes = MAX_LFS_FILESIZE;
488
489 file = fget(d.fd);
490 if (!file)
491 return -EINVAL;
492
493 fc = get_conn(file, sb);
494 fput(file);
334f485d
MS
495 if (IS_ERR(fc))
496 return PTR_ERR(fc);
d8a5ba45 497
1e9a4ed9 498 fc->flags = d.flags;
d8a5ba45 499 fc->user_id = d.user_id;
87729a55 500 fc->group_id = d.group_id;
db50b96c
MS
501 fc->max_read = d.max_read;
502 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
503 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
d8a5ba45 504
f543f253
MS
505 /* Used by get_root_inode() */
506 sb->s_fs_info = fc;
507
d8a5ba45
MS
508 err = -ENOMEM;
509 root = get_root_inode(sb, d.rootmode);
f543f253 510 if (!root)
d8a5ba45
MS
511 goto err;
512
f543f253
MS
513 root_dentry = d_alloc_root(root);
514 if (!root_dentry) {
d8a5ba45
MS
515 iput(root);
516 goto err;
517 }
f543f253
MS
518
519 err = kobject_set_name(&fc->kobj, "%llu", conn_id());
520 if (err)
521 goto err_put_root;
522
523 err = kobject_add(&fc->kobj);
524 if (err)
525 goto err_put_root;
526
527 sb->s_root = root_dentry;
528 spin_lock(&fuse_lock);
529 fc->mounted = 1;
530 fc->connected = 1;
531 spin_unlock(&fuse_lock);
532
334f485d 533 fuse_send_init(fc);
f543f253 534
d8a5ba45
MS
535 return 0;
536
f543f253
MS
537 err_put_root:
538 dput(root_dentry);
d8a5ba45 539 err:
f543f253 540 kobject_put(&fc->kobj);
d8a5ba45
MS
541 return err;
542}
543
544static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
545 int flags, const char *dev_name,
546 void *raw_data)
547{
548 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
549}
550
551static struct file_system_type fuse_fs_type = {
552 .owner = THIS_MODULE,
553 .name = "fuse",
554 .get_sb = fuse_get_sb,
555 .kill_sb = kill_anon_super,
556};
557
0cd5b885
MS
558static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page)
559{
560 return sprintf(page, "%i\n", atomic_read(&fc->num_waiting));
561}
562
563static struct fuse_conn_attr fuse_conn_waiting =
564 __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL);
565
f543f253 566static struct attribute *fuse_conn_attrs[] = {
0cd5b885 567 &fuse_conn_waiting.attr,
f543f253
MS
568 NULL,
569};
570
571static ssize_t fuse_conn_attr_show(struct kobject *kobj,
572 struct attribute *attr,
573 char *page)
574{
575 struct fuse_conn_attr *fca =
576 container_of(attr, struct fuse_conn_attr, attr);
577
578 if (fca->show)
579 return fca->show(get_fuse_conn_kobj(kobj), page);
580 else
581 return -EACCES;
582}
583
584static ssize_t fuse_conn_attr_store(struct kobject *kobj,
585 struct attribute *attr,
586 const char *page, size_t count)
587{
588 struct fuse_conn_attr *fca =
589 container_of(attr, struct fuse_conn_attr, attr);
590
591 if (fca->store)
592 return fca->store(get_fuse_conn_kobj(kobj), page, count);
593 else
594 return -EACCES;
595}
596
597static struct sysfs_ops fuse_conn_sysfs_ops = {
598 .show = &fuse_conn_attr_show,
599 .store = &fuse_conn_attr_store,
600};
601
602static struct kobj_type ktype_fuse_conn = {
603 .release = fuse_conn_release,
604 .sysfs_ops = &fuse_conn_sysfs_ops,
605 .default_attrs = fuse_conn_attrs,
606};
607
608static decl_subsys(fuse, NULL, NULL);
609static decl_subsys(connections, &ktype_fuse_conn, NULL);
610
d8a5ba45
MS
611static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
612 unsigned long flags)
613{
614 struct inode * inode = foo;
615
616 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
617 SLAB_CTOR_CONSTRUCTOR)
618 inode_init_once(inode);
619}
620
621static int __init fuse_fs_init(void)
622{
623 int err;
624
625 err = register_filesystem(&fuse_fs_type);
626 if (err)
627 printk("fuse: failed to register filesystem\n");
628 else {
629 fuse_inode_cachep = kmem_cache_create("fuse_inode",
630 sizeof(struct fuse_inode),
631 0, SLAB_HWCACHE_ALIGN,
632 fuse_inode_init_once, NULL);
633 if (!fuse_inode_cachep) {
634 unregister_filesystem(&fuse_fs_type);
635 err = -ENOMEM;
636 }
637 }
638
639 return err;
640}
641
642static void fuse_fs_cleanup(void)
643{
644 unregister_filesystem(&fuse_fs_type);
645 kmem_cache_destroy(fuse_inode_cachep);
646}
647
f543f253
MS
648static int fuse_sysfs_init(void)
649{
650 int err;
651
652 kset_set_kset_s(&fuse_subsys, fs_subsys);
653 err = subsystem_register(&fuse_subsys);
654 if (err)
655 goto out_err;
656
657 kset_set_kset_s(&connections_subsys, fuse_subsys);
658 err = subsystem_register(&connections_subsys);
659 if (err)
660 goto out_fuse_unregister;
661
662 return 0;
663
664 out_fuse_unregister:
665 subsystem_unregister(&fuse_subsys);
666 out_err:
667 return err;
668}
669
670static void fuse_sysfs_cleanup(void)
671{
672 subsystem_unregister(&connections_subsys);
673 subsystem_unregister(&fuse_subsys);
674}
675
d8a5ba45
MS
676static int __init fuse_init(void)
677{
678 int res;
679
680 printk("fuse init (API version %i.%i)\n",
681 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
682
683 spin_lock_init(&fuse_lock);
684 res = fuse_fs_init();
685 if (res)
686 goto err;
687
334f485d
MS
688 res = fuse_dev_init();
689 if (res)
690 goto err_fs_cleanup;
691
f543f253
MS
692 res = fuse_sysfs_init();
693 if (res)
694 goto err_dev_cleanup;
695
d8a5ba45
MS
696 return 0;
697
f543f253
MS
698 err_dev_cleanup:
699 fuse_dev_cleanup();
334f485d
MS
700 err_fs_cleanup:
701 fuse_fs_cleanup();
d8a5ba45
MS
702 err:
703 return res;
704}
705
706static void __exit fuse_exit(void)
707{
708 printk(KERN_DEBUG "fuse exit\n");
709
f543f253 710 fuse_sysfs_cleanup();
d8a5ba45 711 fuse_fs_cleanup();
334f485d 712 fuse_dev_cleanup();
d8a5ba45
MS
713}
714
715module_init(fuse_init);
716module_exit(fuse_exit);