blob: 04c80cc957a3ee8f947227740ddf558e5ee868a8 [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));
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800138 } else
139 BUG();
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)) {
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700172 inode->i_flags |= S_NOATIME|S_NOCMTIME;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700173 inode->i_generation = generation;
174 inode->i_data.backing_dev_info = &fc->bdi;
175 fuse_init_inode(inode, attr);
176 unlock_new_inode(inode);
177 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) {
178 BUG_ON(retried);
179 /* Inode has changed type, any I/O on the old should fail */
180 make_bad_inode(inode);
181 iput(inode);
182 retried = 1;
183 goto retry;
184 }
185
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700186 fi = get_fuse_inode(inode);
187 fi->nlookup ++;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700188 fuse_change_attributes(inode, attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700189 return inode;
190}
191
192static void fuse_put_super(struct super_block *sb)
193{
194 struct fuse_conn *fc = get_fuse_conn_super(sb);
195
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700196 down_write(&fc->sbput_sem);
197 while (!list_empty(&fc->background))
198 fuse_release_background(list_entry(fc->background.next,
199 struct fuse_req, bg_entry));
200
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700201 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700202 fc->mounted = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700203 fc->user_id = 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700204 fc->group_id = 0;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700205 fc->flags = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700206 /* Flush all readers on this fs */
207 wake_up_all(&fc->waitq);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700208 up_write(&fc->sbput_sem);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700209 fuse_release_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700210 spin_unlock(&fuse_lock);
211}
212
Miklos Szeredie5e55582005-09-09 13:10:28 -0700213static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr)
214{
215 stbuf->f_type = FUSE_SUPER_MAGIC;
216 stbuf->f_bsize = attr->bsize;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800217 stbuf->f_frsize = attr->frsize;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700218 stbuf->f_blocks = attr->blocks;
219 stbuf->f_bfree = attr->bfree;
220 stbuf->f_bavail = attr->bavail;
221 stbuf->f_files = attr->files;
222 stbuf->f_ffree = attr->ffree;
223 stbuf->f_namelen = attr->namelen;
224 /* fsid is left zero */
225}
226
227static int fuse_statfs(struct super_block *sb, struct kstatfs *buf)
228{
229 struct fuse_conn *fc = get_fuse_conn_super(sb);
230 struct fuse_req *req;
231 struct fuse_statfs_out outarg;
232 int err;
233
234 req = fuse_get_request(fc);
235 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700236 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700237
Miklos Szeredide5f1202006-01-06 00:19:37 -0800238 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700239 req->in.numargs = 0;
240 req->in.h.opcode = FUSE_STATFS;
241 req->out.numargs = 1;
Miklos Szeredide5f1202006-01-06 00:19:37 -0800242 req->out.args[0].size =
243 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700244 req->out.args[0].value = &outarg;
245 request_send(fc, req);
246 err = req->out.h.error;
247 if (!err)
248 convert_fuse_statfs(buf, &outarg.st);
249 fuse_put_request(fc, req);
250 return err;
251}
252
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700253enum {
254 OPT_FD,
255 OPT_ROOTMODE,
256 OPT_USER_ID,
Miklos Szeredi87729a52005-09-09 13:10:34 -0700257 OPT_GROUP_ID,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700258 OPT_DEFAULT_PERMISSIONS,
259 OPT_ALLOW_OTHER,
Miklos Szeredidb50b962005-09-09 13:10:33 -0700260 OPT_MAX_READ,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700261 OPT_ERR
262};
263
264static match_table_t tokens = {
265 {OPT_FD, "fd=%u"},
266 {OPT_ROOTMODE, "rootmode=%o"},
267 {OPT_USER_ID, "user_id=%u"},
Miklos Szeredi87729a52005-09-09 13:10:34 -0700268 {OPT_GROUP_ID, "group_id=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700269 {OPT_DEFAULT_PERMISSIONS, "default_permissions"},
270 {OPT_ALLOW_OTHER, "allow_other"},
Miklos Szeredidb50b962005-09-09 13:10:33 -0700271 {OPT_MAX_READ, "max_read=%u"},
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700272 {OPT_ERR, NULL}
273};
274
275static int parse_fuse_opt(char *opt, struct fuse_mount_data *d)
276{
277 char *p;
278 memset(d, 0, sizeof(struct fuse_mount_data));
Miklos Szeredidb50b962005-09-09 13:10:33 -0700279 d->max_read = ~0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700280
281 while ((p = strsep(&opt, ",")) != NULL) {
282 int token;
283 int value;
284 substring_t args[MAX_OPT_ARGS];
285 if (!*p)
286 continue;
287
288 token = match_token(p, tokens, args);
289 switch (token) {
290 case OPT_FD:
291 if (match_int(&args[0], &value))
292 return 0;
293 d->fd = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700294 d->fd_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700295 break;
296
297 case OPT_ROOTMODE:
298 if (match_octal(&args[0], &value))
299 return 0;
300 d->rootmode = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700301 d->rootmode_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700302 break;
303
304 case OPT_USER_ID:
305 if (match_int(&args[0], &value))
306 return 0;
307 d->user_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700308 d->user_id_present = 1;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700309 break;
310
Miklos Szeredi87729a52005-09-09 13:10:34 -0700311 case OPT_GROUP_ID:
312 if (match_int(&args[0], &value))
313 return 0;
314 d->group_id = value;
Miklos Szeredi5a533682005-09-09 13:10:34 -0700315 d->group_id_present = 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700316 break;
317
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700318 case OPT_DEFAULT_PERMISSIONS:
319 d->flags |= FUSE_DEFAULT_PERMISSIONS;
320 break;
321
322 case OPT_ALLOW_OTHER:
323 d->flags |= FUSE_ALLOW_OTHER;
324 break;
325
Miklos Szeredidb50b962005-09-09 13:10:33 -0700326 case OPT_MAX_READ:
327 if (match_int(&args[0], &value))
328 return 0;
329 d->max_read = value;
330 break;
331
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700332 default:
333 return 0;
334 }
335 }
Miklos Szeredi5a533682005-09-09 13:10:34 -0700336
337 if (!d->fd_present || !d->rootmode_present ||
338 !d->user_id_present || !d->group_id_present)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700339 return 0;
340
341 return 1;
342}
343
344static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt)
345{
346 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb);
347
348 seq_printf(m, ",user_id=%u", fc->user_id);
Miklos Szeredi87729a52005-09-09 13:10:34 -0700349 seq_printf(m, ",group_id=%u", fc->group_id);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700350 if (fc->flags & FUSE_DEFAULT_PERMISSIONS)
351 seq_puts(m, ",default_permissions");
352 if (fc->flags & FUSE_ALLOW_OTHER)
353 seq_puts(m, ",allow_other");
Miklos Szeredidb50b962005-09-09 13:10:33 -0700354 if (fc->max_read != ~0)
355 seq_printf(m, ",max_read=%u", fc->max_read);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700356 return 0;
357}
358
Miklos Szeredi334f4852005-09-09 13:10:27 -0700359static void free_conn(struct fuse_conn *fc)
360{
361 while (!list_empty(&fc->unused_list)) {
362 struct fuse_req *req;
363 req = list_entry(fc->unused_list.next, struct fuse_req, list);
364 list_del(&req->list);
365 fuse_request_free(req);
366 }
367 kfree(fc);
368}
369
370/* Must be called with the fuse lock held */
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700371void fuse_release_conn(struct fuse_conn *fc)
372{
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700373 fc->count--;
374 if (!fc->count)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 free_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700376}
377
378static struct fuse_conn *new_conn(void)
379{
380 struct fuse_conn *fc;
381
382 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
383 if (fc != NULL) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700384 int i;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700385 memset(fc, 0, sizeof(*fc));
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386 init_waitqueue_head(&fc->waitq);
387 INIT_LIST_HEAD(&fc->pending);
388 INIT_LIST_HEAD(&fc->processing);
389 INIT_LIST_HEAD(&fc->unused_list);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700390 INIT_LIST_HEAD(&fc->background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700391 sema_init(&fc->outstanding_sem, 0);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700392 init_rwsem(&fc->sbput_sem);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700393 for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) {
394 struct fuse_req *req = fuse_request_alloc();
395 if (!req) {
396 free_conn(fc);
397 return NULL;
398 }
399 list_add(&req->list, &fc->unused_list);
400 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700401 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
402 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403 fc->reqctr = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700404 }
405 return fc;
406}
407
408static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
409{
410 struct fuse_conn *fc;
411
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412 if (file->f_op != &fuse_dev_operations)
413 return ERR_PTR(-EINVAL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700414 fc = new_conn();
415 if (fc == NULL)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700416 return ERR_PTR(-ENOMEM);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700417 spin_lock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700418 if (file->private_data) {
419 free_conn(fc);
420 fc = ERR_PTR(-EINVAL);
421 } else {
422 file->private_data = fc;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700423 *get_fuse_conn_super_p(sb) = fc;
424 fc->mounted = 1;
425 fc->connected = 1;
426 fc->count = 2;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700427 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700428 spin_unlock(&fuse_lock);
429 return fc;
430}
431
432static struct inode *get_root_inode(struct super_block *sb, unsigned mode)
433{
434 struct fuse_attr attr;
435 memset(&attr, 0, sizeof(attr));
436
437 attr.mode = mode;
438 attr.ino = FUSE_ROOT_ID;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700439 return fuse_iget(sb, 1, 0, &attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700440}
441
442static struct super_operations fuse_super_operations = {
443 .alloc_inode = fuse_alloc_inode,
444 .destroy_inode = fuse_destroy_inode,
445 .read_inode = fuse_read_inode,
446 .clear_inode = fuse_clear_inode,
447 .put_super = fuse_put_super,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700448 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700449 .show_options = fuse_show_options,
450};
451
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700452static int fuse_fill_super(struct super_block *sb, void *data, int silent)
453{
454 struct fuse_conn *fc;
455 struct inode *root;
456 struct fuse_mount_data d;
457 struct file *file;
458 int err;
459
460 if (!parse_fuse_opt((char *) data, &d))
461 return -EINVAL;
462
463 sb->s_blocksize = PAGE_CACHE_SIZE;
464 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
465 sb->s_magic = FUSE_SUPER_MAGIC;
466 sb->s_op = &fuse_super_operations;
467 sb->s_maxbytes = MAX_LFS_FILESIZE;
468
469 file = fget(d.fd);
470 if (!file)
471 return -EINVAL;
472
473 fc = get_conn(file, sb);
474 fput(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 if (IS_ERR(fc))
476 return PTR_ERR(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700477
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700478 fc->flags = d.flags;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700479 fc->user_id = d.user_id;
Miklos Szeredi87729a52005-09-09 13:10:34 -0700480 fc->group_id = d.group_id;
Miklos Szeredidb50b962005-09-09 13:10:33 -0700481 fc->max_read = d.max_read;
482 if (fc->max_read / PAGE_CACHE_SIZE < fc->bdi.ra_pages)
483 fc->bdi.ra_pages = fc->max_read / PAGE_CACHE_SIZE;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700484
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700485 err = -ENOMEM;
486 root = get_root_inode(sb, d.rootmode);
487 if (root == NULL)
488 goto err;
489
490 sb->s_root = d_alloc_root(root);
491 if (!sb->s_root) {
492 iput(root);
493 goto err;
494 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495 fuse_send_init(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700496 return 0;
497
498 err:
499 spin_lock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700500 fuse_release_conn(fc);
501 spin_unlock(&fuse_lock);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700502 return err;
503}
504
505static struct super_block *fuse_get_sb(struct file_system_type *fs_type,
506 int flags, const char *dev_name,
507 void *raw_data)
508{
509 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super);
510}
511
512static struct file_system_type fuse_fs_type = {
513 .owner = THIS_MODULE,
514 .name = "fuse",
515 .get_sb = fuse_get_sb,
516 .kill_sb = kill_anon_super,
517};
518
519static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep,
520 unsigned long flags)
521{
522 struct inode * inode = foo;
523
524 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
525 SLAB_CTOR_CONSTRUCTOR)
526 inode_init_once(inode);
527}
528
529static int __init fuse_fs_init(void)
530{
531 int err;
532
533 err = register_filesystem(&fuse_fs_type);
534 if (err)
535 printk("fuse: failed to register filesystem\n");
536 else {
537 fuse_inode_cachep = kmem_cache_create("fuse_inode",
538 sizeof(struct fuse_inode),
539 0, SLAB_HWCACHE_ALIGN,
540 fuse_inode_init_once, NULL);
541 if (!fuse_inode_cachep) {
542 unregister_filesystem(&fuse_fs_type);
543 err = -ENOMEM;
544 }
545 }
546
547 return err;
548}
549
550static void fuse_fs_cleanup(void)
551{
552 unregister_filesystem(&fuse_fs_type);
553 kmem_cache_destroy(fuse_inode_cachep);
554}
555
556static int __init fuse_init(void)
557{
558 int res;
559
560 printk("fuse init (API version %i.%i)\n",
561 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
562
563 spin_lock_init(&fuse_lock);
564 res = fuse_fs_init();
565 if (res)
566 goto err;
567
Miklos Szeredi334f4852005-09-09 13:10:27 -0700568 res = fuse_dev_init();
569 if (res)
570 goto err_fs_cleanup;
571
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700572 return 0;
573
Miklos Szeredi334f4852005-09-09 13:10:27 -0700574 err_fs_cleanup:
575 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700576 err:
577 return res;
578}
579
580static void __exit fuse_exit(void)
581{
582 printk(KERN_DEBUG "fuse exit\n");
583
584 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700585 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700586}
587
588module_init(fuse_init);
589module_exit(fuse_exit);