blob: f229d6962643a5a824baf0cd2e556c4524cd2c0f [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>
18#include <linux/moduleparam.h>
19#include <linux/parser.h>
20#include <linux/statfs.h>
21
22MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23MODULE_DESCRIPTION("Filesystem in Userspace");
24MODULE_LICENSE("GPL");
25
26spinlock_t fuse_lock;
27static kmem_cache_t *fuse_inode_cachep;
28static int mount_count;
29
30static int mount_max = 1000;
31module_param(mount_max, int, 0644);
32MODULE_PARM_DESC(mount_max, "Maximum number of FUSE mounts allowed, if -1 then unlimited (default: 1000)");
33
34#define FUSE_SUPER_MAGIC 0x65735546
35
36struct fuse_mount_data {
37 int fd;
38 unsigned rootmode;
39 unsigned user_id;
40};
41
42static 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;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070054 fi->nlookup = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070055 fi->forget_req = fuse_request_alloc();
56 if (!fi->forget_req) {
57 kmem_cache_free(fuse_inode_cachep, inode);
58 return NULL;
59 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070060
61 return inode;
62}
63
64static void fuse_destroy_inode(struct inode *inode)
65{
Miklos Szeredie5e55582005-09-09 13:10:28 -070066 struct fuse_inode *fi = get_fuse_inode(inode);
67 if (fi->forget_req)
68 fuse_request_free(fi->forget_req);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070069 kmem_cache_free(fuse_inode_cachep, inode);
70}
71
72static void fuse_read_inode(struct inode *inode)
73{
74 /* No op */
75}
76
Miklos Szeredie5e55582005-09-09 13:10:28 -070077void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070078 unsigned long nodeid, u64 nlookup)
Miklos Szeredie5e55582005-09-09 13:10:28 -070079{
80 struct fuse_forget_in *inarg = &req->misc.forget_in;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070081 inarg->nlookup = nlookup;
Miklos Szeredie5e55582005-09-09 13:10:28 -070082 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
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070090static void fuse_clear_inode(struct inode *inode)
91{
Miklos Szeredie5e55582005-09-09 13:10:28 -070092 struct fuse_conn *fc = get_fuse_conn(inode);
93 if (fc) {
94 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070095 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -070096 fi->forget_req = NULL;
97 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070098}
99
100void 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
121static 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);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700125 if (S_ISREG(inode->i_mode)) {
126 fuse_init_common(inode);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700127 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700128 } 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);
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700141 fuse_init_file_inode(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700142 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700143}
144
145static 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
154static 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
161struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700162 int generation, struct fuse_attr *attr)
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700163{
164 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700165 struct fuse_inode *fi;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700166 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
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700188 fi = get_fuse_inode(inode);
189 fi->nlookup ++;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700190 fuse_change_attributes(inode, attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700191 return inode;
192}
193
194static 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;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700202 /* Flush all readers on this fs */
203 wake_up_all(&fc->waitq);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700204 fuse_release_conn(fc);
205 *get_fuse_conn_super_p(sb) = NULL;
206 spin_unlock(&fuse_lock);
207}
208
Miklos Szeredie5e55582005-09-09 13:10:28 -0700209static 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
222static 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
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700246enum {
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
257static 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
268static 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
311static 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
Miklos Szeredi334f4852005-09-09 13:10:27 -0700319static void free_conn(struct fuse_conn *fc)
320{
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 }
327 kfree(fc);
328}
329
330/* Must be called with the fuse lock held */
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700331void fuse_release_conn(struct fuse_conn *fc)
332{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700333 if (!fc->sb && !fc->file)
334 free_conn(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700335}
336
337static struct fuse_conn *new_conn(void)
338{
339 struct fuse_conn *fc;
340
341 fc = kmalloc(sizeof(*fc), GFP_KERNEL);
342 if (fc != NULL) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343 int i;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700344 memset(fc, 0, sizeof(*fc));
345 fc->sb = NULL;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700346 fc->file = NULL;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700347 fc->user_id = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700348 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 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700361 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE;
362 fc->bdi.unplug_io_fn = default_unplug_io_fn;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363 fc->reqctr = 0;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700364 }
365 return fc;
366}
367
368static struct fuse_conn *get_conn(struct file *file, struct super_block *sb)
369{
370 struct fuse_conn *fc;
371
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372 if (file->f_op != &fuse_dev_operations)
373 return ERR_PTR(-EINVAL);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700374 fc = new_conn();
375 if (fc == NULL)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376 return ERR_PTR(-ENOMEM);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700377 spin_lock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378 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 }
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700386 spin_unlock(&fuse_lock);
387 return fc;
388}
389
390static 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;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700397 return fuse_iget(sb, 1, 0, &attr);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700398}
399
400static 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,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700406 .statfs = fuse_statfs,
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700407 .show_options = fuse_show_options,
408};
409
410static 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
421static 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);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700444 if (IS_ERR(fc))
445 return PTR_ERR(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700446
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 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465 fuse_send_init(fc);
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700466 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
478static 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
485static 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
492static 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
502static 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
523static void fuse_fs_cleanup(void)
524{
525 unregister_filesystem(&fuse_fs_type);
526 kmem_cache_destroy(fuse_inode_cachep);
527}
528
529static 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
Miklos Szeredi334f4852005-09-09 13:10:27 -0700541 res = fuse_dev_init();
542 if (res)
543 goto err_fs_cleanup;
544
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700545 return 0;
546
Miklos Szeredi334f4852005-09-09 13:10:27 -0700547 err_fs_cleanup:
548 fuse_fs_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700549 err:
550 return res;
551}
552
553static void __exit fuse_exit(void)
554{
555 printk(KERN_DEBUG "fuse exit\n");
556
557 fuse_fs_cleanup();
Miklos Szeredi334f4852005-09-09 13:10:27 -0700558 fuse_dev_cleanup();
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700559}
560
561module_init(fuse_init);
562module_exit(fuse_exit);