blob: c8e54c0658f1920e88c13717a8cef1c4ab61dd8e [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
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>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070018#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;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070027
28#define FUSE_SUPER_MAGIC 0x65735546
29
30struct fuse_mount_data {
31 int fd;
32 unsigned rootmode;
33 unsigned user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -070034 unsigned group_id;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070035 unsigned flags;
Miklos Szeredidb50b962005-09-09 13:10:33 -070036 unsigned max_read;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070037};
38
39static struct inode *fuse_alloc_inode(struct super_block *sb)
40{
41 struct inode *inode;
42 struct fuse_inode *fi;
43
44 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
45 if (!inode)
46 return NULL;
47
48 fi = get_fuse_inode(inode);
49 fi->i_time = jiffies - 1;
50 fi->nodeid = 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070051 fi->nlookup = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070052 fi->forget_req = fuse_request_alloc();
53 if (!fi->forget_req) {
54 kmem_cache_free(fuse_inode_cachep, inode);
55 return NULL;
56 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070057
58 return inode;
59}
60
61static void fuse_destroy_inode(struct inode *inode)
62{
Miklos Szeredie5e55582005-09-09 13:10:28 -070063 struct fuse_inode *fi = get_fuse_inode(inode);
64 if (fi->forget_req)
65 fuse_request_free(fi->forget_req);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070066 kmem_cache_free(fuse_inode_cachep, inode);
67}
68
69static void fuse_read_inode(struct inode *inode)
70{
71 /* No op */
72}
73
Miklos Szeredie5e55582005-09-09 13:10:28 -070074void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070075 unsigned long nodeid, u64 nlookup)
Miklos Szeredie5e55582005-09-09 13:10:28 -070076{
77 struct fuse_forget_in *inarg = &req->misc.forget_in;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070078 inarg->nlookup = nlookup;
Miklos Szeredie5e55582005-09-09 13:10:28 -070079 req->in.h.opcode = FUSE_FORGET;
80 req->in.h.nodeid = nodeid;
81 req->in.numargs = 1;
82 req->in.args[0].size = sizeof(struct fuse_forget_in);
83 req->in.args[0].value = inarg;
84 request_send_noreply(fc, req);
85}
86
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070087static void fuse_clear_inode(struct inode *inode)
88{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070089 if (inode->i_sb->s_flags & MS_ACTIVE) {
90 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -070091 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070092 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -070093 fi->forget_req = NULL;
94 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070095}
96
97void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
98{
99 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
100 invalidate_inode_pages(inode->i_mapping);
101
102 inode->i_ino = attr->ino;
103 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
104 inode->i_nlink = attr->nlink;
105 inode->i_uid = attr->uid;
106 inode->i_gid = attr->gid;
107 i_size_write(inode, attr->size);
108 inode->i_blksize = PAGE_CACHE_SIZE;
109 inode->i_blocks = attr->blocks;
110 inode->i_atime.tv_sec = attr->atime;
111 inode->i_atime.tv_nsec = attr->atimensec;
112 inode->i_mtime.tv_sec = attr->mtime;
113 inode->i_mtime.tv_nsec = attr->mtimensec;
114 inode->i_ctime.tv_sec = attr->ctime;
115 inode->i_ctime.tv_nsec = attr->ctimensec;
116}
117
118static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
119{
120 inode->i_mode = attr->mode & S_IFMT;
121 i_size_write(inode, attr->size);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700122 if (S_ISREG(inode->i_mode)) {
123 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700124 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700125 } else if (S_ISDIR(inode->i_mode))
126 fuse_init_dir(inode);
127 else if (S_ISLNK(inode->i_mode))
128 fuse_init_symlink(inode);
129 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
130 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
131 fuse_init_common(inode);
132 init_special_inode(inode, inode->i_mode,
133 new_decode_dev(attr->rdev));
134 } else {
135 /* Don't let user create weird files */
136 inode->i_mode = S_IFREG;
137 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700138 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700139 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700140}
141
142static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
143{
144 unsigned long nodeid = *(unsigned long *) _nodeidp;
145 if (get_node_id(inode) == nodeid)
146 return 1;
147 else
148 return 0;
149}
150
151static int fuse_inode_set(struct inode *inode, void *_nodeidp)
152{
153 unsigned long nodeid = *(unsigned long *) _nodeidp;
154 get_fuse_inode(inode)->nodeid = nodeid;
155 return 0;
156}
157
158struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700159 int generation, struct fuse_attr *attr)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700160{
161 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700162 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700163 struct fuse_conn *fc = get_fuse_conn_super(sb);
164 int retried = 0;
165
166 retry:
167 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
168 if (!inode)
169 return NULL;
170
171 if ((inode->i_state & I_NEW)) {
172 inode->i_generation = generation;
173 inode->i_data.backing_dev_info = &fc->bdi;
174 fuse_init_inode(inode, attr);
175 unlock_new_inode(inode);
176 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
177 BUG_ON(retried);
178 /* Inode has changed type, any I/O on the old should fail */
179 make_bad_inode(inode);
180 iput(inode);
181 retried = 1;
182 goto retry;
183 }
184
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700185 fi = get_fuse_inode(inode);
186 fi->nlookup ++;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700187 fuse_change_attributes(inode, attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700188 return inode;
189}
190
191static void fuse_put_super(struct super_block *sb)
192{
193 struct fuse_conn *fc = get_fuse_conn_super(sb);
194
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700195 down_write(&fc->sbput_sem);
196 while (!list_empty(&fc->background))
197 fuse_release_background(list_entry(fc->background.next,
198 struct fuse_req, bg_entry));
199
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700200 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700201 fc->mounted = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700202 fc->user_id = 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700203 fc->group_id = 0;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700204 fc->flags = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700205 /* Flush all readers on this fs */
206 wake_up_all(&fc->waitq);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700207 up_write(&fc->sbput_sem);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700208 fuse_release_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700209 spin_unlock(&fuse_lock);
210}
211
Miklos Szeredie5e55582005-09-09 13:10:28 -0700212static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
213{
214 stbuf->f_type = FUSE_SUPER_MAGIC;
215 stbuf->f_bsize = attr->bsize;
216 stbuf->f_blocks = attr->blocks;
217 stbuf->f_bfree = attr->bfree;
218 stbuf->f_bavail = attr->bavail;
219 stbuf->f_files = attr->files;
220 stbuf->f_ffree = attr->ffree;
221 stbuf->f_namelen = attr->namelen;
222 /* fsid is left zero */
223}
224
225static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
226{
227 struct fuse_conn *fc = get_fuse_conn_super(sb);
228 struct fuse_req *req;
229 struct fuse_statfs_out outarg;
230 int err;
231
232 req = fuse_get_request(fc);
233 if (!req)
234 return -ERESTARTSYS;
235
236 req->in.numargs = 0;
237 req->in.h.opcode = FUSE_STATFS;
238 req->out.numargs = 1;
239 req->out.args[0].size = sizeof(outarg);
240 req->out.args[0].value = &outarg;
241 request_send(fc, req);
242 err = req->out.h.error;
243 if (!err)
244 convert_fuse_statfs(buf, &outarg.st);
245 fuse_put_request(fc, req);
246 return err;
247}
248
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700249enum {
250 OPT_FD,
251 OPT_ROOTMODE,
252 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700253 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700254 OPT_DEFAULT_PERMISSIONS,
255 OPT_ALLOW_OTHER,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700256 OPT_KERNEL_CACHE,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700257 OPT_MAX_READ,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700258 OPT_ERR
259};
260
261static match_table_t tokens = {
262 {OPT_FD, "fd=%u"},
263 {OPT_ROOTMODE, "rootmode=%o"},
264 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi87729a52005-09-09 13:10:34 -0700265 {OPT_GROUP_ID, "group_id=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700266 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
267 {OPT_ALLOW_OTHER, "allow_other"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700268 {OPT_KERNEL_CACHE, "kernel_cache"},
Miklos Szeredidb50b962005-09-09 13:10:33 -0700269 {OPT_MAX_READ, "max_read=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700270 {OPT_ERR, NULL}
271};
272
273static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
274{
275 char *p;
276 memset(d, 0, sizeof(struct fuse_mount_data));
277 d->fd = -1;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700278 d->max_read = ~0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700279
280 while ((p = strsep(&opt, ",")) != NULL) {
281 int token;
282 int value;
283 substring_t args[MAX_OPT_ARGS];
284 if (!*p)
285 continue;
286
287 token = match_token(p, tokens, args);
288 switch (token) {
289 case OPT_FD:
290 if (match_int(&args[0], &value))
291 return 0;
292 d->fd = value;
293 break;
294
295 case OPT_ROOTMODE:
296 if (match_octal(&args[0], &value))
297 return 0;
298 d->rootmode = value;
299 break;
300
301 case OPT_USER_ID:
302 if (match_int(&args[0], &value))
303 return 0;
304 d->user_id = value;
305 break;
306
Miklos Szeredi87729a52005-09-09 13:10:34 -0700307 case OPT_GROUP_ID:
308 if (match_int(&args[0], &value))
309 return 0;
310 d->group_id = value;
311 break;
312
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700313 case OPT_DEFAULT_PERMISSIONS:
314 d->flags |= FUSE_DEFAULT_PERMISSIONS;
315 break;
316
317 case OPT_ALLOW_OTHER:
318 d->flags |= FUSE_ALLOW_OTHER;
319 break;
320
321 case OPT_KERNEL_CACHE:
322 d->flags |= FUSE_KERNEL_CACHE;
323 break;
324
Miklos Szeredidb50b962005-09-09 13:10:33 -0700325 case OPT_MAX_READ:
326 if (match_int(&args[0], &value))
327 return 0;
328 d->max_read = value;
329 break;
330
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700331 default:
332 return 0;
333 }
334 }
335 if (d->fd == -1)
336 return 0;
337
338 return 1;
339}
340
341static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
342{
343 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
344
345 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi87729a52005-09-09 13:10:34 -0700346 seq_printf(m, ",group_id=%u", fc->group_id);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700347 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
348 seq_puts(m, ",default_permissions");
349 if (fc->flags & FUSE_ALLOW_OTHER)
350 seq_puts(m, ",allow_other");
351 if (fc->flags & FUSE_KERNEL_CACHE)
352 seq_puts(m, ",kernel_cache");
Miklos Szeredidb50b962005-09-09 13:10:33 -0700353 if (fc->max_read != ~0)
354 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700355 return 0;
356}
357
Miklos Szeredi334f4852005-09-09 13:10:27 -0700358static void free_conn(struct fuse_conn *fc)
359{
360 while (!list_empty(&fc->unused_list)) {
361 struct fuse_req *req;
362 req = list_entry(fc->unused_list.next, struct fuse_req, list);
363 list_del(&req->list);
364 fuse_request_free(req);
365 }
366 kfree(fc);
367}
368
369/* Must be called with the fuse lock held */
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700370void fuse_release_conn(struct fuse_conn *fc)
371{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700372 fc->count--;
373 if (!fc->count)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700374 free_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700375}
376
377static struct fuse_conn *new_conn(void)
378{
379 struct fuse_conn *fc;
380
381 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
382 if (fc != NULL) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700383 int i;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700384 memset(fc, 0, sizeof(*fc));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700385 init_waitqueue_head(&fc->waitq);
386 INIT_LIST_HEAD(&fc->pending);
387 INIT_LIST_HEAD(&fc->processing);
388 INIT_LIST_HEAD(&fc->unused_list);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700389 INIT_LIST_HEAD(&fc->background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700390 sema_init(&fc->outstanding_sem, 0);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700391 init_rwsem(&fc->sbput_sem);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700392 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
393 struct fuse_req *req = fuse_request_alloc();
394 if (!req) {
395 free_conn(fc);
396 return NULL;
397 }
398 list_add(&req->list, &fc->unused_list);
399 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700400 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
401 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700402 fc->reqctr = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700403 }
404 return fc;
405}
406
407static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
408{
409 struct fuse_conn *fc;
410
Miklos Szeredi334f4852005-09-09 13:10:27 -0700411 if (file->f_op != &fuse_dev_operations)
412 return ERR_PTR(-EINVAL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700413 fc = new_conn();
414 if (fc == NULL)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700415 return ERR_PTR(-ENOMEM);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700416 spin_lock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417 if (file->private_data) {
418 free_conn(fc);
419 fc = ERR_PTR(-EINVAL);
420 } else {
421 file->private_data = fc;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700422 *get_fuse_conn_super_p(sb) = fc;
423 fc->mounted = 1;
424 fc->connected = 1;
425 fc->count = 2;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700427 spin_unlock(&fuse_lock);
428 return fc;
429}
430
431static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
432{
433 struct fuse_attr attr;
434 memset(&attr, 0, sizeof(attr));
435
436 attr.mode = mode;
437 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700438 return fuse_iget(sb, 1, 0, &attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700439}
440
441static struct super_operations fuse_super_operations = {
442 .alloc_inode = fuse_alloc_inode,
443 .destroy_inode = fuse_destroy_inode,
444 .read_inode = fuse_read_inode,
445 .clear_inode = fuse_clear_inode,
446 .put_super = fuse_put_super,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700447 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700448 .show_options = fuse_show_options,
449};
450
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700451static int fuse_fill_super(struct super_block *sb, void *data, int silent)
452{
453 struct fuse_conn *fc;
454 struct inode *root;
455 struct fuse_mount_data d;
456 struct file *file;
457 int err;
458
459 if (!parse_fuse_opt((char *) data, &d))
460 return -EINVAL;
461
462 sb->s_blocksize = PAGE_CACHE_SIZE;
463 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
464 sb->s_magic = FUSE_SUPER_MAGIC;
465 sb->s_op = &fuse_super_operations;
466 sb->s_maxbytes = MAX_LFS_FILESIZE;
467
468 file = fget(d.fd);
469 if (!file)
470 return -EINVAL;
471
472 fc = get_conn(file, sb);
473 fput(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474 if (IS_ERR(fc))
475 return PTR_ERR(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700476
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700477 fc->flags = d.flags;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700478 fc->user_id = d.user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700479 fc->group_id = d.group_id;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700480 fc->max_read = d.max_read;
481 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
482 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700483
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700484 err = -ENOMEM;
485 root = get_root_inode(sb, d.rootmode);
486 if (root == NULL)
487 goto err;
488
489 sb->s_root = d_alloc_root(root);
490 if (!sb->s_root) {
491 iput(root);
492 goto err;
493 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700494 fuse_send_init(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700495 return 0;
496
497 err:
498 spin_lock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700499 fuse_release_conn(fc);
500 spin_unlock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700501 return err;
502}
503
504static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
505 int flags, const char *dev_name,
506 void *raw_data)
507{
508 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
509}
510
511static struct file_system_type fuse_fs_type = {
512 .owner = THIS_MODULE,
513 .name = "fuse",
514 .get_sb = fuse_get_sb,
515 .kill_sb = kill_anon_super,
516};
517
518static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
519 unsigned long flags)
520{
521 struct inode * inode = foo;
522
523 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
524 SLAB_CTOR_CONSTRUCTOR)
525 inode_init_once(inode);
526}
527
528static int __init fuse_fs_init(void)
529{
530 int err;
531
532 err = register_filesystem(&fuse_fs_type);
533 if (err)
534 printk("fuse: failed to register filesystem\n");
535 else {
536 fuse_inode_cachep = kmem_cache_create("fuse_inode",
537 sizeof(struct fuse_inode),
538 0, SLAB_HWCACHE_ALIGN,
539 fuse_inode_init_once, NULL);
540 if (!fuse_inode_cachep) {
541 unregister_filesystem(&fuse_fs_type);
542 err = -ENOMEM;
543 }
544 }
545
546 return err;
547}
548
549static void fuse_fs_cleanup(void)
550{
551 unregister_filesystem(&fuse_fs_type);
552 kmem_cache_destroy(fuse_inode_cachep);
553}
554
555static int __init fuse_init(void)
556{
557 int res;
558
559 printk("fuse init (API version %i.%i)\n",
560 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
561
562 spin_lock_init(&fuse_lock);
563 res = fuse_fs_init();
564 if (res)
565 goto err;
566
Miklos Szeredi334f4852005-09-09 13:10:27 -0700567 res = fuse_dev_init();
568 if (res)
569 goto err_fs_cleanup;
570
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700571 return 0;
572
Miklos Szeredi334f4852005-09-09 13:10:27 -0700573 err_fs_cleanup:
574 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700575 err:
576 return res;
577}
578
579static void __exit fuse_exit(void)
580{
581 printk(KERN_DEBUG "fuse exit\n");
582
583 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700584 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700585}
586
587module_init(fuse_init);
588module_exit(fuse_exit);