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