blob: e69a546844d0f23770f3dd5e690f20284a27cd01 [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 Szeredi5a533682005-09-09 13:10:34 -070035 unsigned fd_present : 1;
36 unsigned rootmode_present : 1;
37 unsigned user_id_present : 1;
38 unsigned group_id_present : 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070039 unsigned flags;
Miklos Szeredidb50b962005-09-09 13:10:33 -070040 unsigned max_read;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070041};
42
43static struct inode *fuse_alloc_inode(struct super_block *sb)
44{
45 struct inode *inode;
46 struct fuse_inode *fi;
47
48 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL);
49 if (!inode)
50 return NULL;
51
52 fi = get_fuse_inode(inode);
53 fi->i_time = jiffies - 1;
54 fi->nodeid = 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070055 fi->nlookup = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070056 fi->forget_req = fuse_request_alloc();
57 if (!fi->forget_req) {
58 kmem_cache_free(fuse_inode_cachep, inode);
59 return NULL;
60 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070061
62 return inode;
63}
64
65static void fuse_destroy_inode(struct inode *inode)
66{
Miklos Szeredie5e55582005-09-09 13:10:28 -070067 struct fuse_inode *fi = get_fuse_inode(inode);
68 if (fi->forget_req)
69 fuse_request_free(fi->forget_req);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070070 kmem_cache_free(fuse_inode_cachep, inode);
71}
72
73static void fuse_read_inode(struct inode *inode)
74{
75 /* No op */
76}
77
Miklos Szeredie5e55582005-09-09 13:10:28 -070078void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070079 unsigned long nodeid, u64 nlookup)
Miklos Szeredie5e55582005-09-09 13:10:28 -070080{
81 struct fuse_forget_in *inarg = &req->misc.forget_in;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070082 inarg->nlookup = nlookup;
Miklos Szeredie5e55582005-09-09 13:10:28 -070083 req->in.h.opcode = FUSE_FORGET;
84 req->in.h.nodeid = nodeid;
85 req->in.numargs = 1;
86 req->in.args[0].size = sizeof(struct fuse_forget_in);
87 req->in.args[0].value = inarg;
88 request_send_noreply(fc, req);
89}
90
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070091static void fuse_clear_inode(struct inode *inode)
92{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070093 if (inode->i_sb->s_flags & MS_ACTIVE) {
94 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -070095 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070096 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -070097 fi->forget_req = NULL;
98 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070099}
100
101void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr)
102{
103 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size)
104 invalidate_inode_pages(inode->i_mapping);
105
106 inode->i_ino = attr->ino;
107 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777);
108 inode->i_nlink = attr->nlink;
109 inode->i_uid = attr->uid;
110 inode->i_gid = attr->gid;
111 i_size_write(inode, attr->size);
112 inode->i_blksize = PAGE_CACHE_SIZE;
113 inode->i_blocks = attr->blocks;
114 inode->i_atime.tv_sec = attr->atime;
115 inode->i_atime.tv_nsec = attr->atimensec;
116 inode->i_mtime.tv_sec = attr->mtime;
117 inode->i_mtime.tv_nsec = attr->mtimensec;
118 inode->i_ctime.tv_sec = attr->ctime;
119 inode->i_ctime.tv_nsec = attr->ctimensec;
120}
121
122static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr)
123{
124 inode->i_mode = attr->mode & S_IFMT;
125 i_size_write(inode, attr->size);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700126 if (S_ISREG(inode->i_mode)) {
127 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700128 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700129 } else if (S_ISDIR(inode->i_mode))
130 fuse_init_dir(inode);
131 else if (S_ISLNK(inode->i_mode))
132 fuse_init_symlink(inode);
133 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
134 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
135 fuse_init_common(inode);
136 init_special_inode(inode, inode->i_mode,
137 new_decode_dev(attr->rdev));
138 } else {
139 /* Don't let user create weird files */
140 inode->i_mode = S_IFREG;
141 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700142 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700143 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700144}
145
146static int fuse_inode_eq(struct inode *inode, void *_nodeidp)
147{
148 unsigned long nodeid = *(unsigned long *) _nodeidp;
149 if (get_node_id(inode) == nodeid)
150 return 1;
151 else
152 return 0;
153}
154
155static int fuse_inode_set(struct inode *inode, void *_nodeidp)
156{
157 unsigned long nodeid = *(unsigned long *) _nodeidp;
158 get_fuse_inode(inode)->nodeid = nodeid;
159 return 0;
160}
161
162struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700163 int generation, struct fuse_attr *attr)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700164{
165 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700166 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700167 struct fuse_conn *fc = get_fuse_conn_super(sb);
168 int retried = 0;
169
170 retry:
171 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid);
172 if (!inode)
173 return NULL;
174
175 if ((inode->i_state & I_NEW)) {
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700176 inode->i_flags |= S_NOATIME|S_NOCMTIME;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700177 inode->i_generation = generation;
178 inode->i_data.backing_dev_info = &fc->bdi;
179 fuse_init_inode(inode, attr);
180 unlock_new_inode(inode);
181 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
182 BUG_ON(retried);
183 /* Inode has changed type, any I/O on the old should fail */
184 make_bad_inode(inode);
185 iput(inode);
186 retried = 1;
187 goto retry;
188 }
189
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700190 fi = get_fuse_inode(inode);
191 fi->nlookup ++;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700192 fuse_change_attributes(inode, attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700193 return inode;
194}
195
196static void fuse_put_super(struct super_block *sb)
197{
198 struct fuse_conn *fc = get_fuse_conn_super(sb);
199
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700200 down_write(&fc->sbput_sem);
201 while (!list_empty(&fc->background))
202 fuse_release_background(list_entry(fc->background.next,
203 struct fuse_req, bg_entry));
204
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700205 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700206 fc->mounted = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700207 fc->user_id = 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700208 fc->group_id = 0;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700209 fc->flags = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700210 /* Flush all readers on this fs */
211 wake_up_all(&fc->waitq);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700212 up_write(&fc->sbput_sem);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700213 fuse_release_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700214 spin_unlock(&fuse_lock);
215}
216
Miklos Szeredie5e55582005-09-09 13:10:28 -0700217static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
218{
219 stbuf->f_type = FUSE_SUPER_MAGIC;
220 stbuf->f_bsize = attr->bsize;
221 stbuf->f_blocks = attr->blocks;
222 stbuf->f_bfree = attr->bfree;
223 stbuf->f_bavail = attr->bavail;
224 stbuf->f_files = attr->files;
225 stbuf->f_ffree = attr->ffree;
226 stbuf->f_namelen = attr->namelen;
227 /* fsid is left zero */
228}
229
230static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
231{
232 struct fuse_conn *fc = get_fuse_conn_super(sb);
233 struct fuse_req *req;
234 struct fuse_statfs_out outarg;
235 int err;
236
237 req = fuse_get_request(fc);
238 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700239 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700240
241 req->in.numargs = 0;
242 req->in.h.opcode = FUSE_STATFS;
243 req->out.numargs = 1;
244 req->out.args[0].size = sizeof(outarg);
245 req->out.args[0].value = &outarg;
246 request_send(fc, req);
247 err = req->out.h.error;
248 if (!err)
249 convert_fuse_statfs(buf, &outarg.st);
250 fuse_put_request(fc, req);
251 return err;
252}
253
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700254enum {
255 OPT_FD,
256 OPT_ROOTMODE,
257 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700258 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700259 OPT_DEFAULT_PERMISSIONS,
260 OPT_ALLOW_OTHER,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700261 OPT_MAX_READ,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700262 OPT_ERR
263};
264
265static match_table_t tokens = {
266 {OPT_FD, "fd=%u"},
267 {OPT_ROOTMODE, "rootmode=%o"},
268 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi87729a52005-09-09 13:10:34 -0700269 {OPT_GROUP_ID, "group_id=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700270 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
271 {OPT_ALLOW_OTHER, "allow_other"},
Miklos Szeredidb50b962005-09-09 13:10:33 -0700272 {OPT_MAX_READ, "max_read=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700273 {OPT_ERR, NULL}
274};
275
276static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
277{
278 char *p;
279 memset(d, 0, sizeof(struct fuse_mount_data));
Miklos Szeredidb50b962005-09-09 13:10:33 -0700280 d->max_read = ~0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700281
282 while ((p = strsep(&opt, ",")) != NULL) {
283 int token;
284 int value;
285 substring_t args[MAX_OPT_ARGS];
286 if (!*p)
287 continue;
288
289 token = match_token(p, tokens, args);
290 switch (token) {
291 case OPT_FD:
292 if (match_int(&args[0], &value))
293 return 0;
294 d->fd = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700295 d->fd_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700296 break;
297
298 case OPT_ROOTMODE:
299 if (match_octal(&args[0], &value))
300 return 0;
301 d->rootmode = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700302 d->rootmode_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700303 break;
304
305 case OPT_USER_ID:
306 if (match_int(&args[0], &value))
307 return 0;
308 d->user_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700309 d->user_id_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700310 break;
311
Miklos Szeredi87729a52005-09-09 13:10:34 -0700312 case OPT_GROUP_ID:
313 if (match_int(&args[0], &value))
314 return 0;
315 d->group_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700316 d->group_id_present = 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700317 break;
318
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700319 case OPT_DEFAULT_PERMISSIONS:
320 d->flags |= FUSE_DEFAULT_PERMISSIONS;
321 break;
322
323 case OPT_ALLOW_OTHER:
324 d->flags |= FUSE_ALLOW_OTHER;
325 break;
326
Miklos Szeredidb50b962005-09-09 13:10:33 -0700327 case OPT_MAX_READ:
328 if (match_int(&args[0], &value))
329 return 0;
330 d->max_read = value;
331 break;
332
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700333 default:
334 return 0;
335 }
336 }
Miklos Szeredi5a533682005-09-09 13:10:34 -0700337
338 if (!d->fd_present || !d->rootmode_present ||
339 !d->user_id_present || !d->group_id_present)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700340 return 0;
341
342 return 1;
343}
344
345static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
346{
347 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
348
349 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi87729a52005-09-09 13:10:34 -0700350 seq_printf(m, ",group_id=%u", fc->group_id);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700351 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
352 seq_puts(m, ",default_permissions");
353 if (fc->flags & FUSE_ALLOW_OTHER)
354 seq_puts(m, ",allow_other");
Miklos Szeredidb50b962005-09-09 13:10:33 -0700355 if (fc->max_read != ~0)
356 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700357 return 0;
358}
359
Miklos Szeredi334f4852005-09-09 13:10:27 -0700360static void free_conn(struct fuse_conn *fc)
361{
362 while (!list_empty(&fc->unused_list)) {
363 struct fuse_req *req;
364 req = list_entry(fc->unused_list.next, struct fuse_req, list);
365 list_del(&req->list);
366 fuse_request_free(req);
367 }
368 kfree(fc);
369}
370
371/* Must be called with the fuse lock held */
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700372void fuse_release_conn(struct fuse_conn *fc)
373{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700374 fc->count--;
375 if (!fc->count)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376 free_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700377}
378
379static struct fuse_conn *new_conn(void)
380{
381 struct fuse_conn *fc;
382
383 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
384 if (fc != NULL) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700385 int i;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700386 memset(fc, 0, sizeof(*fc));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700387 init_waitqueue_head(&fc->waitq);
388 INIT_LIST_HEAD(&fc->pending);
389 INIT_LIST_HEAD(&fc->processing);
390 INIT_LIST_HEAD(&fc->unused_list);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700391 INIT_LIST_HEAD(&fc->background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700392 sema_init(&fc->outstanding_sem, 0);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700393 init_rwsem(&fc->sbput_sem);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700394 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
395 struct fuse_req *req = fuse_request_alloc();
396 if (!req) {
397 free_conn(fc);
398 return NULL;
399 }
400 list_add(&req->list, &fc->unused_list);
401 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700402 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
403 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700404 fc->reqctr = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700405 }
406 return fc;
407}
408
409static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
410{
411 struct fuse_conn *fc;
412
Miklos Szeredi334f4852005-09-09 13:10:27 -0700413 if (file->f_op != &fuse_dev_operations)
414 return ERR_PTR(-EINVAL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700415 fc = new_conn();
416 if (fc == NULL)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417 return ERR_PTR(-ENOMEM);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700418 spin_lock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700419 if (file->private_data) {
420 free_conn(fc);
421 fc = ERR_PTR(-EINVAL);
422 } else {
423 file->private_data = fc;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700424 *get_fuse_conn_super_p(sb) = fc;
425 fc->mounted = 1;
426 fc->connected = 1;
427 fc->count = 2;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700428 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700429 spin_unlock(&fuse_lock);
430 return fc;
431}
432
433static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
434{
435 struct fuse_attr attr;
436 memset(&attr, 0, sizeof(attr));
437
438 attr.mode = mode;
439 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700440 return fuse_iget(sb, 1, 0, &attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700441}
442
443static struct super_operations fuse_super_operations = {
444 .alloc_inode = fuse_alloc_inode,
445 .destroy_inode = fuse_destroy_inode,
446 .read_inode = fuse_read_inode,
447 .clear_inode = fuse_clear_inode,
448 .put_super = fuse_put_super,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700449 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700450 .show_options = fuse_show_options,
451};
452
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700453static int fuse_fill_super(struct super_block *sb, void *data, int silent)
454{
455 struct fuse_conn *fc;
456 struct inode *root;
457 struct fuse_mount_data d;
458 struct file *file;
459 int err;
460
461 if (!parse_fuse_opt((char *) data, &d))
462 return -EINVAL;
463
464 sb->s_blocksize = PAGE_CACHE_SIZE;
465 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
466 sb->s_magic = FUSE_SUPER_MAGIC;
467 sb->s_op = &fuse_super_operations;
468 sb->s_maxbytes = MAX_LFS_FILESIZE;
469
470 file = fget(d.fd);
471 if (!file)
472 return -EINVAL;
473
474 fc = get_conn(file, sb);
475 fput(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700476 if (IS_ERR(fc))
477 return PTR_ERR(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700478
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700479 fc->flags = d.flags;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700480 fc->user_id = d.user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700481 fc->group_id = d.group_id;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700482 fc->max_read = d.max_read;
483 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
484 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700485 fc->max_write = FUSE_MAX_IN / 2;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700486
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700487 err = -ENOMEM;
488 root = get_root_inode(sb, d.rootmode);
489 if (root == NULL)
490 goto err;
491
492 sb->s_root = d_alloc_root(root);
493 if (!sb->s_root) {
494 iput(root);
495 goto err;
496 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700497 fuse_send_init(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700498 return 0;
499
500 err:
501 spin_lock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700502 fuse_release_conn(fc);
503 spin_unlock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700504 return err;
505}
506
507static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
508 int flags, const char *dev_name,
509 void *raw_data)
510{
511 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
512}
513
514static struct file_system_type fuse_fs_type = {
515 .owner = THIS_MODULE,
516 .name = "fuse",
517 .get_sb = fuse_get_sb,
518 .kill_sb = kill_anon_super,
519};
520
521static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
522 unsigned long flags)
523{
524 struct inode * inode = foo;
525
526 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
527 SLAB_CTOR_CONSTRUCTOR)
528 inode_init_once(inode);
529}
530
531static int __init fuse_fs_init(void)
532{
533 int err;
534
535 err = register_filesystem(&fuse_fs_type);
536 if (err)
537 printk("fuse: failed to register filesystem\n");
538 else {
539 fuse_inode_cachep = kmem_cache_create("fuse_inode",
540 sizeof(struct fuse_inode),
541 0, SLAB_HWCACHE_ALIGN,
542 fuse_inode_init_once, NULL);
543 if (!fuse_inode_cachep) {
544 unregister_filesystem(&fuse_fs_type);
545 err = -ENOMEM;
546 }
547 }
548
549 return err;
550}
551
552static void fuse_fs_cleanup(void)
553{
554 unregister_filesystem(&fuse_fs_type);
555 kmem_cache_destroy(fuse_inode_cachep);
556}
557
558static int __init fuse_init(void)
559{
560 int res;
561
562 printk("fuse init (API version %i.%i)\n",
563 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
564
565 spin_lock_init(&fuse_lock);
566 res = fuse_fs_init();
567 if (res)
568 goto err;
569
Miklos Szeredi334f4852005-09-09 13:10:27 -0700570 res = fuse_dev_init();
571 if (res)
572 goto err_fs_cleanup;
573
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700574 return 0;
575
Miklos Szeredi334f4852005-09-09 13:10:27 -0700576 err_fs_cleanup:
577 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700578 err:
579 return res;
580}
581
582static void __exit fuse_exit(void)
583{
584 printk(KERN_DEBUG "fuse exit\n");
585
586 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700587 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700588}
589
590module_init(fuse_init);
591module_exit(fuse_exit);