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