]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/fuse/inode.c
[PATCH] fuse: simplify locking
[mirror_ubuntu-bionic-kernel.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
69a53bf2
MS
199static void fuse_umount_begin(struct super_block *sb)
200{
201 fuse_abort_conn(get_fuse_conn_super(sb));
202}
203
d8a5ba45
MS
204static void fuse_put_super(struct super_block *sb)
205{
206 struct fuse_conn *fc = get_fuse_conn_super(sb);
207
1e9a4ed9
MS
208 down_write(&fc->sbput_sem);
209 while (!list_empty(&fc->background))
210 fuse_release_background(list_entry(fc->background.next,
211 struct fuse_req, bg_entry));
212
d8a5ba45 213 spin_lock(&fuse_lock);
1e9a4ed9 214 fc->mounted = 0;
9ba7cbba 215 fc->connected = 0;
f543f253
MS
216 spin_unlock(&fuse_lock);
217 up_write(&fc->sbput_sem);
334f485d 218 /* Flush all readers on this fs */
385a17bf 219 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
334f485d 220 wake_up_all(&fc->waitq);
f543f253
MS
221 kobject_del(&fc->kobj);
222 kobject_put(&fc->kobj);
d8a5ba45
MS
223}
224
e5e5558e
MS
225static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
226{
227 stbuf->f_type = FUSE_SUPER_MAGIC;
228 stbuf->f_bsize = attr->bsize;
de5f1202 229 stbuf->f_frsize = attr->frsize;
e5e5558e
MS
230 stbuf->f_blocks = attr->blocks;
231 stbuf->f_bfree = attr->bfree;
232 stbuf->f_bavail = attr->bavail;
233 stbuf->f_files = attr->files;
234 stbuf->f_ffree = attr->ffree;
235 stbuf->f_namelen = attr->namelen;
236 /* fsid is left zero */
237}
238
239static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
240{
241 struct fuse_conn *fc = get_fuse_conn_super(sb);
242 struct fuse_req *req;
243 struct fuse_statfs_out outarg;
244 int err;
245
246 req = fuse_get_request(fc);
247 if (!req)
7c352bdf 248 return -EINTR;
e5e5558e 249
de5f1202 250 memset(&outarg, 0, sizeof(outarg));
e5e5558e
MS
251 req->in.numargs = 0;
252 req->in.h.opcode = FUSE_STATFS;
253 req->out.numargs = 1;
de5f1202
MS
254 req->out.args[0].size =
255 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
e5e5558e
MS
256 req->out.args[0].value = &outarg;
257 request_send(fc, req);
258 err = req->out.h.error;
259 if (!err)
260 convert_fuse_statfs(buf, &outarg.st);
261 fuse_put_request(fc, req);
262 return err;
263}
264
d8a5ba45
MS
265enum {
266 OPT_FD,
267 OPT_ROOTMODE,
268 OPT_USER_ID,
87729a55 269 OPT_GROUP_ID,
d8a5ba45
MS
270 OPT_DEFAULT_PERMISSIONS,
271 OPT_ALLOW_OTHER,
db50b96c 272 OPT_MAX_READ,
d8a5ba45
MS
273 OPT_ERR
274};
275
276static match_table_t tokens = {
277 {OPT_FD, "fd=%u"},
278 {OPT_ROOTMODE, "rootmode=%o"},
279 {OPT_USER_ID, "user_id=%u"},
87729a55 280 {OPT_GROUP_ID, "group_id=%u"},
d8a5ba45
MS
281 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
282 {OPT_ALLOW_OTHER, "allow_other"},
db50b96c 283 {OPT_MAX_READ, "max_read=%u"},
d8a5ba45
MS
284 {OPT_ERR, NULL}
285};
286
287static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
288{
289 char *p;
290 memset(d, 0, sizeof(struct fuse_mount_data));
db50b96c 291 d->max_read = ~0;
d8a5ba45
MS
292
293 while ((p = strsep(&opt, ",")) != NULL) {
294 int token;
295 int value;
296 substring_t args[MAX_OPT_ARGS];
297 if (!*p)
298 continue;
299
300 token = match_token(p, tokens, args);
301 switch (token) {
302 case OPT_FD:
303 if (match_int(&args[0], &value))
304 return 0;
305 d->fd = value;
5a533682 306 d->fd_present = 1;
d8a5ba45
MS
307 break;
308
309 case OPT_ROOTMODE:
310 if (match_octal(&args[0], &value))
311 return 0;
312 d->rootmode = value;
5a533682 313 d->rootmode_present = 1;
d8a5ba45
MS
314 break;
315
316 case OPT_USER_ID:
317 if (match_int(&args[0], &value))
318 return 0;
319 d->user_id = value;
5a533682 320 d->user_id_present = 1;
d8a5ba45
MS
321 break;
322
87729a55
MS
323 case OPT_GROUP_ID:
324 if (match_int(&args[0], &value))
325 return 0;
326 d->group_id = value;
5a533682 327 d->group_id_present = 1;
87729a55
MS
328 break;
329
1e9a4ed9
MS
330 case OPT_DEFAULT_PERMISSIONS:
331 d->flags |= FUSE_DEFAULT_PERMISSIONS;
332 break;
333
334 case OPT_ALLOW_OTHER:
335 d->flags |= FUSE_ALLOW_OTHER;
336 break;
337
db50b96c
MS
338 case OPT_MAX_READ:
339 if (match_int(&args[0], &value))
340 return 0;
341 d->max_read = value;
342 break;
343
d8a5ba45
MS
344 default:
345 return 0;
346 }
347 }
5a533682
MS
348
349 if (!d->fd_present || !d->rootmode_present ||
350 !d->user_id_present || !d->group_id_present)
d8a5ba45
MS
351 return 0;
352
353 return 1;
354}
355
356static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
357{
358 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
359
360 seq_printf(m, ",user_id=%u", fc->user_id);
87729a55 361 seq_printf(m, ",group_id=%u", fc->group_id);
1e9a4ed9
MS
362 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
363 seq_puts(m, ",default_permissions");
364 if (fc->flags & FUSE_ALLOW_OTHER)
365 seq_puts(m, ",allow_other");
db50b96c
MS
366 if (fc->max_read != ~0)
367 seq_printf(m, ",max_read=%u", fc->max_read);
d8a5ba45
MS
368 return 0;
369}
370
f543f253 371static void fuse_conn_release(struct kobject *kobj)
d8a5ba45 372{
f543f253
MS
373 struct fuse_conn *fc = get_fuse_conn_kobj(kobj);
374
334f485d
MS
375 while (!list_empty(&fc->unused_list)) {
376 struct fuse_req *req;
377 req = list_entry(fc->unused_list.next, struct fuse_req, list);
378 list_del(&req->list);
379 fuse_request_free(req);
380 }
d8a5ba45
MS
381 kfree(fc);
382}
383
384static struct fuse_conn *new_conn(void)
385{
386 struct fuse_conn *fc;
387
6383bdaa 388 fc = kzalloc(sizeof(*fc), GFP_KERNEL);
f543f253 389 if (fc) {
334f485d 390 int i;
334f485d
MS
391 init_waitqueue_head(&fc->waitq);
392 INIT_LIST_HEAD(&fc->pending);
393 INIT_LIST_HEAD(&fc->processing);
d77a1d5b 394 INIT_LIST_HEAD(&fc->io);
334f485d 395 INIT_LIST_HEAD(&fc->unused_list);
1e9a4ed9 396 INIT_LIST_HEAD(&fc->background);
6383bdaa 397 sema_init(&fc->outstanding_sem, 1); /* One for INIT */
1e9a4ed9 398 init_rwsem(&fc->sbput_sem);
f543f253
MS
399 kobj_set_kset_s(fc, connections_subsys);
400 kobject_init(&fc->kobj);
095da6cb 401 atomic_set(&fc->num_waiting, 0);
334f485d
MS
402 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
403 struct fuse_req *req = fuse_request_alloc();
404 if (!req) {
f543f253 405 kobject_put(&fc->kobj);
334f485d
MS
406 return NULL;
407 }
408 list_add(&req->list, &fc->unused_list);
409 }
d8a5ba45
MS
410 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
411 fc->bdi.unplug_io_fn = default_unplug_io_fn;
334f485d 412 fc->reqctr = 0;
d8a5ba45
MS
413 }
414 return fc;
415}
416
d8a5ba45
MS
417static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
418{
419 struct fuse_attr attr;
420 memset(&attr, 0, sizeof(attr));
421
422 attr.mode = mode;
423 attr.ino = FUSE_ROOT_ID;
9e6268db 424 return fuse_iget(sb, 1, 0, &attr);
d8a5ba45
MS
425}
426
427static struct super_operations fuse_super_operations = {
428 .alloc_inode = fuse_alloc_inode,
429 .destroy_inode = fuse_destroy_inode,
430 .read_inode = fuse_read_inode,
431 .clear_inode = fuse_clear_inode,
432 .put_super = fuse_put_super,
69a53bf2 433 .umount_begin = fuse_umount_begin,
e5e5558e 434 .statfs = fuse_statfs,
d8a5ba45
MS
435 .show_options = fuse_show_options,
436};
437
9b9a0469
MS
438static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
439{
440 int i;
441 struct fuse_init_out *arg = &req->misc.init_out;
442
443 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
444 fc->conn_error = 1;
445 else {
9cd68455
MS
446 unsigned long ra_pages;
447
448 if (arg->minor >= 6) {
449 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE;
450 if (arg->flags & FUSE_ASYNC_READ)
451 fc->async_read = 1;
452 } else
453 ra_pages = fc->max_read / PAGE_CACHE_SIZE;
454
455 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages);
9b9a0469
MS
456 fc->minor = arg->minor;
457 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
458 }
459
460 /* After INIT reply is received other requests can go
461 out. So do (FUSE_MAX_OUTSTANDING - 1) number of
462 up()s on outstanding_sem. The last up() is done in
463 fuse_putback_request() */
464 for (i = 1; i < FUSE_MAX_OUTSTANDING; i++)
465 up(&fc->outstanding_sem);
466
467 fuse_put_request(fc, req);
468}
469
470static void fuse_send_init(struct fuse_conn *fc)
471{
472 /* This is called from fuse_read_super() so there's guaranteed
473 to be exactly one request available */
474 struct fuse_req *req = fuse_get_request(fc);
475 struct fuse_init_in *arg = &req->misc.init_in;
095da6cb 476
9b9a0469
MS
477 arg->major = FUSE_KERNEL_VERSION;
478 arg->minor = FUSE_KERNEL_MINOR_VERSION;
9cd68455
MS
479 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE;
480 arg->flags |= FUSE_ASYNC_READ;
9b9a0469
MS
481 req->in.h.opcode = FUSE_INIT;
482 req->in.numargs = 1;
483 req->in.args[0].size = sizeof(*arg);
484 req->in.args[0].value = arg;
485 req->out.numargs = 1;
486 /* Variable length arguement used for backward compatibility
487 with interface version < 7.5. Rest of init_out is zeroed
488 by do_get_request(), so a short reply is not a problem */
489 req->out.argvar = 1;
490 req->out.args[0].size = sizeof(struct fuse_init_out);
491 req->out.args[0].value = &req->misc.init_out;
492 req->end = process_init_reply;
493 request_send_background(fc, req);
494}
495
f543f253
MS
496static unsigned long long conn_id(void)
497{
0720b315 498 /* BKL is held for ->get_sb() */
f543f253 499 static unsigned long long ctr = 1;
0720b315 500 return ctr++;
f543f253
MS
501}
502
d8a5ba45
MS
503static int fuse_fill_super(struct super_block *sb, void *data, int silent)
504{
505 struct fuse_conn *fc;
506 struct inode *root;
507 struct fuse_mount_data d;
508 struct file *file;
f543f253 509 struct dentry *root_dentry;
d8a5ba45
MS
510 int err;
511
512 if (!parse_fuse_opt((char *) data, &d))
513 return -EINVAL;
514
515 sb->s_blocksize = PAGE_CACHE_SIZE;
516 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
517 sb->s_magic = FUSE_SUPER_MAGIC;
518 sb->s_op = &fuse_super_operations;
519 sb->s_maxbytes = MAX_LFS_FILESIZE;
520
521 file = fget(d.fd);
522 if (!file)
523 return -EINVAL;
524
0720b315
MS
525 if (file->f_op != &fuse_dev_operations)
526 return -EINVAL;
527
528 /* Setting file->private_data can't race with other mount()
529 instances, since BKL is held for ->get_sb() */
530 if (file->private_data)
531 return -EINVAL;
532
533 fc = new_conn();
534 if (!fc)
535 return -ENOMEM;
d8a5ba45 536
1e9a4ed9 537 fc->flags = d.flags;
d8a5ba45 538 fc->user_id = d.user_id;
87729a55 539 fc->group_id = d.group_id;
db50b96c 540 fc->max_read = d.max_read;
d8a5ba45 541
f543f253
MS
542 /* Used by get_root_inode() */
543 sb->s_fs_info = fc;
544
d8a5ba45
MS
545 err = -ENOMEM;
546 root = get_root_inode(sb, d.rootmode);
f543f253 547 if (!root)
d8a5ba45
MS
548 goto err;
549
f543f253
MS
550 root_dentry = d_alloc_root(root);
551 if (!root_dentry) {
d8a5ba45
MS
552 iput(root);
553 goto err;
554 }
f543f253
MS
555
556 err = kobject_set_name(&fc->kobj, "%llu", conn_id());
557 if (err)
558 goto err_put_root;
559
560 err = kobject_add(&fc->kobj);
561 if (err)
562 goto err_put_root;
563
564 sb->s_root = root_dentry;
f543f253
MS
565 fc->mounted = 1;
566 fc->connected = 1;
0720b315
MS
567 kobject_get(&fc->kobj);
568 file->private_data = fc;
569 /*
570 * atomic_dec_and_test() in fput() provides the necessary
571 * memory barrier for file->private_data to be visible on all
572 * CPUs after this
573 */
574 fput(file);
f543f253 575
334f485d 576 fuse_send_init(fc);
f543f253 577
d8a5ba45
MS
578 return 0;
579
f543f253
MS
580 err_put_root:
581 dput(root_dentry);
d8a5ba45 582 err:
0720b315 583 fput(file);
f543f253 584 kobject_put(&fc->kobj);
d8a5ba45
MS
585 return err;
586}
587
588static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
589 int flags, const char *dev_name,
590 void *raw_data)
591{
592 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
593}
594
595static struct file_system_type fuse_fs_type = {
596 .owner = THIS_MODULE,
597 .name = "fuse",
598 .get_sb = fuse_get_sb,
599 .kill_sb = kill_anon_super,
600};
601
0cd5b885
MS
602static ssize_t fuse_conn_waiting_show(struct fuse_conn *fc, char *page)
603{
604 return sprintf(page, "%i\n", atomic_read(&fc->num_waiting));
605}
606
69a53bf2
MS
607static ssize_t fuse_conn_abort_store(struct fuse_conn *fc, const char *page,
608 size_t count)
609{
610 fuse_abort_conn(fc);
611 return count;
612}
613
0cd5b885
MS
614static struct fuse_conn_attr fuse_conn_waiting =
615 __ATTR(waiting, 0400, fuse_conn_waiting_show, NULL);
69a53bf2
MS
616static struct fuse_conn_attr fuse_conn_abort =
617 __ATTR(abort, 0600, NULL, fuse_conn_abort_store);
0cd5b885 618
f543f253 619static struct attribute *fuse_conn_attrs[] = {
0cd5b885 620 &fuse_conn_waiting.attr,
69a53bf2 621 &fuse_conn_abort.attr,
f543f253
MS
622 NULL,
623};
624
625static ssize_t fuse_conn_attr_show(struct kobject *kobj,
626 struct attribute *attr,
627 char *page)
628{
629 struct fuse_conn_attr *fca =
630 container_of(attr, struct fuse_conn_attr, attr);
631
632 if (fca->show)
633 return fca->show(get_fuse_conn_kobj(kobj), page);
634 else
635 return -EACCES;
636}
637
638static ssize_t fuse_conn_attr_store(struct kobject *kobj,
639 struct attribute *attr,
640 const char *page, size_t count)
641{
642 struct fuse_conn_attr *fca =
643 container_of(attr, struct fuse_conn_attr, attr);
644
645 if (fca->store)
646 return fca->store(get_fuse_conn_kobj(kobj), page, count);
647 else
648 return -EACCES;
649}
650
651static struct sysfs_ops fuse_conn_sysfs_ops = {
652 .show = &fuse_conn_attr_show,
653 .store = &fuse_conn_attr_store,
654};
655
656static struct kobj_type ktype_fuse_conn = {
657 .release = fuse_conn_release,
658 .sysfs_ops = &fuse_conn_sysfs_ops,
659 .default_attrs = fuse_conn_attrs,
660};
661
662static decl_subsys(fuse, NULL, NULL);
663static decl_subsys(connections, &ktype_fuse_conn, NULL);
664
d8a5ba45
MS
665static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
666 unsigned long flags)
667{
668 struct inode * inode = foo;
669
670 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
671 SLAB_CTOR_CONSTRUCTOR)
672 inode_init_once(inode);
673}
674
675static int __init fuse_fs_init(void)
676{
677 int err;
678
679 err = register_filesystem(&fuse_fs_type);
680 if (err)
681 printk("fuse: failed to register filesystem\n");
682 else {
683 fuse_inode_cachep = kmem_cache_create("fuse_inode",
684 sizeof(struct fuse_inode),
685 0, SLAB_HWCACHE_ALIGN,
686 fuse_inode_init_once, NULL);
687 if (!fuse_inode_cachep) {
688 unregister_filesystem(&fuse_fs_type);
689 err = -ENOMEM;
690 }
691 }
692
693 return err;
694}
695
696static void fuse_fs_cleanup(void)
697{
698 unregister_filesystem(&fuse_fs_type);
699 kmem_cache_destroy(fuse_inode_cachep);
700}
701
f543f253
MS
702static int fuse_sysfs_init(void)
703{
704 int err;
705
706 kset_set_kset_s(&fuse_subsys, fs_subsys);
707 err = subsystem_register(&fuse_subsys);
708 if (err)
709 goto out_err;
710
711 kset_set_kset_s(&connections_subsys, fuse_subsys);
712 err = subsystem_register(&connections_subsys);
713 if (err)
714 goto out_fuse_unregister;
715
716 return 0;
717
718 out_fuse_unregister:
719 subsystem_unregister(&fuse_subsys);
720 out_err:
721 return err;
722}
723
724static void fuse_sysfs_cleanup(void)
725{
726 subsystem_unregister(&connections_subsys);
727 subsystem_unregister(&fuse_subsys);
728}
729
d8a5ba45
MS
730static int __init fuse_init(void)
731{
732 int res;
733
734 printk("fuse init (API version %i.%i)\n",
735 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
736
737 spin_lock_init(&fuse_lock);
738 res = fuse_fs_init();
739 if (res)
740 goto err;
741
334f485d
MS
742 res = fuse_dev_init();
743 if (res)
744 goto err_fs_cleanup;
745
f543f253
MS
746 res = fuse_sysfs_init();
747 if (res)
748 goto err_dev_cleanup;
749
d8a5ba45
MS
750 return 0;
751
f543f253
MS
752 err_dev_cleanup:
753 fuse_dev_cleanup();
334f485d
MS
754 err_fs_cleanup:
755 fuse_fs_cleanup();
d8a5ba45
MS
756 err:
757 return res;
758}
759
760static void __exit fuse_exit(void)
761{
762 printk(KERN_DEBUG "fuse exit\n");
763
f543f253 764 fuse_sysfs_cleanup();
d8a5ba45 765 fuse_fs_cleanup();
334f485d 766 fuse_dev_cleanup();
d8a5ba45
MS
767}
768
769module_init(fuse_init);
770module_exit(fuse_exit);