Commit | Line | Data |
---|---|---|
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> | |
18 | #include <linux/moduleparam.h> | |
19 | #include <linux/parser.h> | |
20 | #include <linux/statfs.h> | |
21 | ||
22 | MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); | |
23 | MODULE_DESCRIPTION("Filesystem in Userspace"); | |
24 | MODULE_LICENSE("GPL"); | |
25 | ||
26 | spinlock_t fuse_lock; | |
27 | static kmem_cache_t *fuse_inode_cachep; | |
28 | static int mount_count; | |
29 | ||
30 | static int mount_max = 1000; | |
31 | module_param(mount_max, int, 0644); | |
32 | MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)"); | |
33 | ||
34 | #define FUSE_SUPER_MAGIC 0x65735546 | |
35 | ||
36 | struct fuse_mount_data { | |
37 | int fd; | |
38 | unsigned rootmode; | |
39 | unsigned user_id; | |
40 | }; | |
41 | ||
42 | static struct inode *fuse_alloc_inode(struct super_block *sb) | |
43 | { | |
44 | struct inode *inode; | |
45 | struct fuse_inode *fi; | |
46 | ||
47 | inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); | |
48 | if (!inode) | |
49 | return NULL; | |
50 | ||
51 | fi = get_fuse_inode(inode); | |
52 | fi->i_time = jiffies - 1; | |
53 | fi->nodeid = 0; | |
9e6268db | 54 | fi->nlookup = 0; |
e5e5558e MS |
55 | fi->forget_req = fuse_request_alloc(); |
56 | if (!fi->forget_req) { | |
57 | kmem_cache_free(fuse_inode_cachep, inode); | |
58 | return NULL; | |
59 | } | |
d8a5ba45 MS |
60 | |
61 | return inode; | |
62 | } | |
63 | ||
64 | static void fuse_destroy_inode(struct inode *inode) | |
65 | { | |
e5e5558e MS |
66 | struct fuse_inode *fi = get_fuse_inode(inode); |
67 | if (fi->forget_req) | |
68 | fuse_request_free(fi->forget_req); | |
d8a5ba45 MS |
69 | kmem_cache_free(fuse_inode_cachep, inode); |
70 | } | |
71 | ||
72 | static void fuse_read_inode(struct inode *inode) | |
73 | { | |
74 | /* No op */ | |
75 | } | |
76 | ||
e5e5558e | 77 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, |
9e6268db | 78 | unsigned long nodeid, u64 nlookup) |
e5e5558e MS |
79 | { |
80 | struct fuse_forget_in *inarg = &req->misc.forget_in; | |
9e6268db | 81 | inarg->nlookup = nlookup; |
e5e5558e MS |
82 | req->in.h.opcode = FUSE_FORGET; |
83 | req->in.h.nodeid = nodeid; | |
84 | req->in.numargs = 1; | |
85 | req->in.args[0].size = sizeof(struct fuse_forget_in); | |
86 | req->in.args[0].value = inarg; | |
87 | request_send_noreply(fc, req); | |
88 | } | |
89 | ||
d8a5ba45 MS |
90 | static void fuse_clear_inode(struct inode *inode) |
91 | { | |
e5e5558e MS |
92 | struct fuse_conn *fc = get_fuse_conn(inode); |
93 | if (fc) { | |
94 | struct fuse_inode *fi = get_fuse_inode(inode); | |
9e6268db | 95 | fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); |
e5e5558e MS |
96 | fi->forget_req = NULL; |
97 | } | |
d8a5ba45 MS |
98 | } |
99 | ||
100 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) | |
101 | { | |
102 | if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) | |
103 | invalidate_inode_pages(inode->i_mapping); | |
104 | ||
105 | inode->i_ino = attr->ino; | |
106 | inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); | |
107 | inode->i_nlink = attr->nlink; | |
108 | inode->i_uid = attr->uid; | |
109 | inode->i_gid = attr->gid; | |
110 | i_size_write(inode, attr->size); | |
111 | inode->i_blksize = PAGE_CACHE_SIZE; | |
112 | inode->i_blocks = attr->blocks; | |
113 | inode->i_atime.tv_sec = attr->atime; | |
114 | inode->i_atime.tv_nsec = attr->atimensec; | |
115 | inode->i_mtime.tv_sec = attr->mtime; | |
116 | inode->i_mtime.tv_nsec = attr->mtimensec; | |
117 | inode->i_ctime.tv_sec = attr->ctime; | |
118 | inode->i_ctime.tv_nsec = attr->ctimensec; | |
119 | } | |
120 | ||
121 | static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) | |
122 | { | |
123 | inode->i_mode = attr->mode & S_IFMT; | |
124 | i_size_write(inode, attr->size); | |
e5e5558e MS |
125 | if (S_ISREG(inode->i_mode)) { |
126 | fuse_init_common(inode); | |
b6aeaded | 127 | fuse_init_file_inode(inode); |
e5e5558e MS |
128 | } else if (S_ISDIR(inode->i_mode)) |
129 | fuse_init_dir(inode); | |
130 | else if (S_ISLNK(inode->i_mode)) | |
131 | fuse_init_symlink(inode); | |
132 | else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || | |
133 | S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
134 | fuse_init_common(inode); | |
135 | init_special_inode(inode, inode->i_mode, | |
136 | new_decode_dev(attr->rdev)); | |
137 | } else { | |
138 | /* Don't let user create weird files */ | |
139 | inode->i_mode = S_IFREG; | |
140 | fuse_init_common(inode); | |
b6aeaded | 141 | fuse_init_file_inode(inode); |
e5e5558e | 142 | } |
d8a5ba45 MS |
143 | } |
144 | ||
145 | static int fuse_inode_eq(struct inode *inode, void *_nodeidp) | |
146 | { | |
147 | unsigned long nodeid = *(unsigned long *) _nodeidp; | |
148 | if (get_node_id(inode) == nodeid) | |
149 | return 1; | |
150 | else | |
151 | return 0; | |
152 | } | |
153 | ||
154 | static int fuse_inode_set(struct inode *inode, void *_nodeidp) | |
155 | { | |
156 | unsigned long nodeid = *(unsigned long *) _nodeidp; | |
157 | get_fuse_inode(inode)->nodeid = nodeid; | |
158 | return 0; | |
159 | } | |
160 | ||
161 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | |
9e6268db | 162 | int generation, struct fuse_attr *attr) |
d8a5ba45 MS |
163 | { |
164 | struct inode *inode; | |
9e6268db | 165 | struct fuse_inode *fi; |
d8a5ba45 MS |
166 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
167 | int retried = 0; | |
168 | ||
169 | retry: | |
170 | inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); | |
171 | if (!inode) | |
172 | return NULL; | |
173 | ||
174 | if ((inode->i_state & I_NEW)) { | |
175 | inode->i_generation = generation; | |
176 | inode->i_data.backing_dev_info = &fc->bdi; | |
177 | fuse_init_inode(inode, attr); | |
178 | unlock_new_inode(inode); | |
179 | } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { | |
180 | BUG_ON(retried); | |
181 | /* Inode has changed type, any I/O on the old should fail */ | |
182 | make_bad_inode(inode); | |
183 | iput(inode); | |
184 | retried = 1; | |
185 | goto retry; | |
186 | } | |
187 | ||
9e6268db MS |
188 | fi = get_fuse_inode(inode); |
189 | fi->nlookup ++; | |
d8a5ba45 | 190 | fuse_change_attributes(inode, attr); |
d8a5ba45 MS |
191 | return inode; |
192 | } | |
193 | ||
194 | static void fuse_put_super(struct super_block *sb) | |
195 | { | |
196 | struct fuse_conn *fc = get_fuse_conn_super(sb); | |
197 | ||
198 | spin_lock(&fuse_lock); | |
199 | mount_count --; | |
200 | fc->sb = NULL; | |
201 | fc->user_id = 0; | |
334f485d MS |
202 | /* Flush all readers on this fs */ |
203 | wake_up_all(&fc->waitq); | |
d8a5ba45 MS |
204 | fuse_release_conn(fc); |
205 | *get_fuse_conn_super_p(sb) = NULL; | |
206 | spin_unlock(&fuse_lock); | |
207 | } | |
208 | ||
e5e5558e MS |
209 | static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) |
210 | { | |
211 | stbuf->f_type = FUSE_SUPER_MAGIC; | |
212 | stbuf->f_bsize = attr->bsize; | |
213 | stbuf->f_blocks = attr->blocks; | |
214 | stbuf->f_bfree = attr->bfree; | |
215 | stbuf->f_bavail = attr->bavail; | |
216 | stbuf->f_files = attr->files; | |
217 | stbuf->f_ffree = attr->ffree; | |
218 | stbuf->f_namelen = attr->namelen; | |
219 | /* fsid is left zero */ | |
220 | } | |
221 | ||
222 | static int fuse_statfs(struct super_block *sb, struct kstatfs *buf) | |
223 | { | |
224 | struct fuse_conn *fc = get_fuse_conn_super(sb); | |
225 | struct fuse_req *req; | |
226 | struct fuse_statfs_out outarg; | |
227 | int err; | |
228 | ||
229 | req = fuse_get_request(fc); | |
230 | if (!req) | |
231 | return -ERESTARTSYS; | |
232 | ||
233 | req->in.numargs = 0; | |
234 | req->in.h.opcode = FUSE_STATFS; | |
235 | req->out.numargs = 1; | |
236 | req->out.args[0].size = sizeof(outarg); | |
237 | req->out.args[0].value = &outarg; | |
238 | request_send(fc, req); | |
239 | err = req->out.h.error; | |
240 | if (!err) | |
241 | convert_fuse_statfs(buf, &outarg.st); | |
242 | fuse_put_request(fc, req); | |
243 | return err; | |
244 | } | |
245 | ||
d8a5ba45 MS |
246 | enum { |
247 | OPT_FD, | |
248 | OPT_ROOTMODE, | |
249 | OPT_USER_ID, | |
250 | OPT_DEFAULT_PERMISSIONS, | |
251 | OPT_ALLOW_OTHER, | |
252 | OPT_ALLOW_ROOT, | |
253 | OPT_KERNEL_CACHE, | |
254 | OPT_ERR | |
255 | }; | |
256 | ||
257 | static match_table_t tokens = { | |
258 | {OPT_FD, "fd=%u"}, | |
259 | {OPT_ROOTMODE, "rootmode=%o"}, | |
260 | {OPT_USER_ID, "user_id=%u"}, | |
261 | {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, | |
262 | {OPT_ALLOW_OTHER, "allow_other"}, | |
263 | {OPT_ALLOW_ROOT, "allow_root"}, | |
264 | {OPT_KERNEL_CACHE, "kernel_cache"}, | |
265 | {OPT_ERR, NULL} | |
266 | }; | |
267 | ||
268 | static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) | |
269 | { | |
270 | char *p; | |
271 | memset(d, 0, sizeof(struct fuse_mount_data)); | |
272 | d->fd = -1; | |
273 | ||
274 | while ((p = strsep(&opt, ",")) != NULL) { | |
275 | int token; | |
276 | int value; | |
277 | substring_t args[MAX_OPT_ARGS]; | |
278 | if (!*p) | |
279 | continue; | |
280 | ||
281 | token = match_token(p, tokens, args); | |
282 | switch (token) { | |
283 | case OPT_FD: | |
284 | if (match_int(&args[0], &value)) | |
285 | return 0; | |
286 | d->fd = value; | |
287 | break; | |
288 | ||
289 | case OPT_ROOTMODE: | |
290 | if (match_octal(&args[0], &value)) | |
291 | return 0; | |
292 | d->rootmode = value; | |
293 | break; | |
294 | ||
295 | case OPT_USER_ID: | |
296 | if (match_int(&args[0], &value)) | |
297 | return 0; | |
298 | d->user_id = value; | |
299 | break; | |
300 | ||
301 | default: | |
302 | return 0; | |
303 | } | |
304 | } | |
305 | if (d->fd == -1) | |
306 | return 0; | |
307 | ||
308 | return 1; | |
309 | } | |
310 | ||
311 | static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) | |
312 | { | |
313 | struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); | |
314 | ||
315 | seq_printf(m, ",user_id=%u", fc->user_id); | |
316 | return 0; | |
317 | } | |
318 | ||
334f485d | 319 | static void free_conn(struct fuse_conn *fc) |
d8a5ba45 | 320 | { |
334f485d MS |
321 | while (!list_empty(&fc->unused_list)) { |
322 | struct fuse_req *req; | |
323 | req = list_entry(fc->unused_list.next, struct fuse_req, list); | |
324 | list_del(&req->list); | |
325 | fuse_request_free(req); | |
326 | } | |
d8a5ba45 MS |
327 | kfree(fc); |
328 | } | |
329 | ||
334f485d MS |
330 | /* Must be called with the fuse lock held */ |
331 | void fuse_release_conn(struct fuse_conn *fc) | |
332 | { | |
333 | if (!fc->sb && !fc->file) | |
334 | free_conn(fc); | |
335 | } | |
336 | ||
d8a5ba45 MS |
337 | static struct fuse_conn *new_conn(void) |
338 | { | |
339 | struct fuse_conn *fc; | |
340 | ||
341 | fc = kmalloc(sizeof(*fc), GFP_KERNEL); | |
342 | if (fc != NULL) { | |
334f485d | 343 | int i; |
d8a5ba45 MS |
344 | memset(fc, 0, sizeof(*fc)); |
345 | fc->sb = NULL; | |
334f485d | 346 | fc->file = NULL; |
d8a5ba45 | 347 | fc->user_id = 0; |
334f485d MS |
348 | init_waitqueue_head(&fc->waitq); |
349 | INIT_LIST_HEAD(&fc->pending); | |
350 | INIT_LIST_HEAD(&fc->processing); | |
351 | INIT_LIST_HEAD(&fc->unused_list); | |
352 | sema_init(&fc->outstanding_sem, 0); | |
353 | for (i = 0; i < FUSE_MAX_OUTSTANDING; i++) { | |
354 | struct fuse_req *req = fuse_request_alloc(); | |
355 | if (!req) { | |
356 | free_conn(fc); | |
357 | return NULL; | |
358 | } | |
359 | list_add(&req->list, &fc->unused_list); | |
360 | } | |
d8a5ba45 MS |
361 | fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; |
362 | fc->bdi.unplug_io_fn = default_unplug_io_fn; | |
334f485d | 363 | fc->reqctr = 0; |
d8a5ba45 MS |
364 | } |
365 | return fc; | |
366 | } | |
367 | ||
368 | static struct fuse_conn *get_conn(struct file *file, struct super_block *sb) | |
369 | { | |
370 | struct fuse_conn *fc; | |
371 | ||
334f485d MS |
372 | if (file->f_op != &fuse_dev_operations) |
373 | return ERR_PTR(-EINVAL); | |
d8a5ba45 MS |
374 | fc = new_conn(); |
375 | if (fc == NULL) | |
334f485d | 376 | return ERR_PTR(-ENOMEM); |
d8a5ba45 | 377 | spin_lock(&fuse_lock); |
334f485d MS |
378 | if (file->private_data) { |
379 | free_conn(fc); | |
380 | fc = ERR_PTR(-EINVAL); | |
381 | } else { | |
382 | file->private_data = fc; | |
383 | fc->sb = sb; | |
384 | fc->file = file; | |
385 | } | |
d8a5ba45 MS |
386 | spin_unlock(&fuse_lock); |
387 | return fc; | |
388 | } | |
389 | ||
390 | static struct inode *get_root_inode(struct super_block *sb, unsigned mode) | |
391 | { | |
392 | struct fuse_attr attr; | |
393 | memset(&attr, 0, sizeof(attr)); | |
394 | ||
395 | attr.mode = mode; | |
396 | attr.ino = FUSE_ROOT_ID; | |
9e6268db | 397 | return fuse_iget(sb, 1, 0, &attr); |
d8a5ba45 MS |
398 | } |
399 | ||
400 | static struct super_operations fuse_super_operations = { | |
401 | .alloc_inode = fuse_alloc_inode, | |
402 | .destroy_inode = fuse_destroy_inode, | |
403 | .read_inode = fuse_read_inode, | |
404 | .clear_inode = fuse_clear_inode, | |
405 | .put_super = fuse_put_super, | |
e5e5558e | 406 | .statfs = fuse_statfs, |
d8a5ba45 MS |
407 | .show_options = fuse_show_options, |
408 | }; | |
409 | ||
410 | static int inc_mount_count(void) | |
411 | { | |
412 | int success = 0; | |
413 | spin_lock(&fuse_lock); | |
414 | mount_count ++; | |
415 | if (mount_max == -1 || mount_count <= mount_max) | |
416 | success = 1; | |
417 | spin_unlock(&fuse_lock); | |
418 | return success; | |
419 | } | |
420 | ||
421 | static int fuse_fill_super(struct super_block *sb, void *data, int silent) | |
422 | { | |
423 | struct fuse_conn *fc; | |
424 | struct inode *root; | |
425 | struct fuse_mount_data d; | |
426 | struct file *file; | |
427 | int err; | |
428 | ||
429 | if (!parse_fuse_opt((char *) data, &d)) | |
430 | return -EINVAL; | |
431 | ||
432 | sb->s_blocksize = PAGE_CACHE_SIZE; | |
433 | sb->s_blocksize_bits = PAGE_CACHE_SHIFT; | |
434 | sb->s_magic = FUSE_SUPER_MAGIC; | |
435 | sb->s_op = &fuse_super_operations; | |
436 | sb->s_maxbytes = MAX_LFS_FILESIZE; | |
437 | ||
438 | file = fget(d.fd); | |
439 | if (!file) | |
440 | return -EINVAL; | |
441 | ||
442 | fc = get_conn(file, sb); | |
443 | fput(file); | |
334f485d MS |
444 | if (IS_ERR(fc)) |
445 | return PTR_ERR(fc); | |
d8a5ba45 MS |
446 | |
447 | fc->user_id = d.user_id; | |
448 | ||
449 | *get_fuse_conn_super_p(sb) = fc; | |
450 | ||
451 | err = -ENFILE; | |
452 | if (!inc_mount_count() && current->uid != 0) | |
453 | goto err; | |
454 | ||
455 | err = -ENOMEM; | |
456 | root = get_root_inode(sb, d.rootmode); | |
457 | if (root == NULL) | |
458 | goto err; | |
459 | ||
460 | sb->s_root = d_alloc_root(root); | |
461 | if (!sb->s_root) { | |
462 | iput(root); | |
463 | goto err; | |
464 | } | |
334f485d | 465 | fuse_send_init(fc); |
d8a5ba45 MS |
466 | return 0; |
467 | ||
468 | err: | |
469 | spin_lock(&fuse_lock); | |
470 | mount_count --; | |
471 | fc->sb = NULL; | |
472 | fuse_release_conn(fc); | |
473 | spin_unlock(&fuse_lock); | |
474 | *get_fuse_conn_super_p(sb) = NULL; | |
475 | return err; | |
476 | } | |
477 | ||
478 | static struct super_block *fuse_get_sb(struct file_system_type *fs_type, | |
479 | int flags, const char *dev_name, | |
480 | void *raw_data) | |
481 | { | |
482 | return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super); | |
483 | } | |
484 | ||
485 | static struct file_system_type fuse_fs_type = { | |
486 | .owner = THIS_MODULE, | |
487 | .name = "fuse", | |
488 | .get_sb = fuse_get_sb, | |
489 | .kill_sb = kill_anon_super, | |
490 | }; | |
491 | ||
492 | static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, | |
493 | unsigned long flags) | |
494 | { | |
495 | struct inode * inode = foo; | |
496 | ||
497 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == | |
498 | SLAB_CTOR_CONSTRUCTOR) | |
499 | inode_init_once(inode); | |
500 | } | |
501 | ||
502 | static int __init fuse_fs_init(void) | |
503 | { | |
504 | int err; | |
505 | ||
506 | err = register_filesystem(&fuse_fs_type); | |
507 | if (err) | |
508 | printk("fuse: failed to register filesystem\n"); | |
509 | else { | |
510 | fuse_inode_cachep = kmem_cache_create("fuse_inode", | |
511 | sizeof(struct fuse_inode), | |
512 | 0, SLAB_HWCACHE_ALIGN, | |
513 | fuse_inode_init_once, NULL); | |
514 | if (!fuse_inode_cachep) { | |
515 | unregister_filesystem(&fuse_fs_type); | |
516 | err = -ENOMEM; | |
517 | } | |
518 | } | |
519 | ||
520 | return err; | |
521 | } | |
522 | ||
523 | static void fuse_fs_cleanup(void) | |
524 | { | |
525 | unregister_filesystem(&fuse_fs_type); | |
526 | kmem_cache_destroy(fuse_inode_cachep); | |
527 | } | |
528 | ||
529 | static int __init fuse_init(void) | |
530 | { | |
531 | int res; | |
532 | ||
533 | printk("fuse init (API version %i.%i)\n", | |
534 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); | |
535 | ||
536 | spin_lock_init(&fuse_lock); | |
537 | res = fuse_fs_init(); | |
538 | if (res) | |
539 | goto err; | |
540 | ||
334f485d MS |
541 | res = fuse_dev_init(); |
542 | if (res) | |
543 | goto err_fs_cleanup; | |
544 | ||
d8a5ba45 MS |
545 | return 0; |
546 | ||
334f485d MS |
547 | err_fs_cleanup: |
548 | fuse_fs_cleanup(); | |
d8a5ba45 MS |
549 | err: |
550 | return res; | |
551 | } | |
552 | ||
553 | static void __exit fuse_exit(void) | |
554 | { | |
555 | printk(KERN_DEBUG "fuse exit\n"); | |
556 | ||
557 | fuse_fs_cleanup(); | |
334f485d | 558 | fuse_dev_cleanup(); |
d8a5ba45 MS |
559 | } |
560 | ||
561 | module_init(fuse_init); | |
562 | module_exit(fuse_exit); |