1 /* 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2006 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/seq_file.h> 15 #include <linux/init.h> 16 #include <linux/module.h> 17 #include <linux/parser.h> 18 #include <linux/statfs.h> 19 #include <linux/random.h> 20 21 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 22 MODULE_DESCRIPTION("Filesystem in Userspace"); 23 MODULE_LICENSE("GPL"); 24 25 static kmem_cache_t *fuse_inode_cachep; 26 struct list_head fuse_conn_list; 27 DEFINE_MUTEX(fuse_mutex); 28 29 #define FUSE_SUPER_MAGIC 0x65735546 30 31 struct fuse_mount_data { 32 int fd; 33 unsigned rootmode; 34 unsigned user_id; 35 unsigned group_id; 36 unsigned fd_present : 1; 37 unsigned rootmode_present : 1; 38 unsigned user_id_present : 1; 39 unsigned group_id_present : 1; 40 unsigned flags; 41 unsigned max_read; 42 }; 43 44 static struct inode *fuse_alloc_inode(struct super_block *sb) 45 { 46 struct inode *inode; 47 struct fuse_inode *fi; 48 49 inode = kmem_cache_alloc(fuse_inode_cachep, SLAB_KERNEL); 50 if (!inode) 51 return NULL; 52 53 fi = get_fuse_inode(inode); 54 fi->i_time = 0; 55 fi->nodeid = 0; 56 fi->nlookup = 0; 57 fi->forget_req = fuse_request_alloc(); 58 if (!fi->forget_req) { 59 kmem_cache_free(fuse_inode_cachep, inode); 60 return NULL; 61 } 62 63 return inode; 64 } 65 66 static void fuse_destroy_inode(struct inode *inode) 67 { 68 struct fuse_inode *fi = get_fuse_inode(inode); 69 if (fi->forget_req) 70 fuse_request_free(fi->forget_req); 71 kmem_cache_free(fuse_inode_cachep, inode); 72 } 73 74 static void fuse_read_inode(struct inode *inode) 75 { 76 /* No op */ 77 } 78 79 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, 80 unsigned long nodeid, u64 nlookup) 81 { 82 struct fuse_forget_in *inarg = &req->misc.forget_in; 83 inarg->nlookup = nlookup; 84 req->in.h.opcode = FUSE_FORGET; 85 req->in.h.nodeid = nodeid; 86 req->in.numargs = 1; 87 req->in.args[0].size = sizeof(struct fuse_forget_in); 88 req->in.args[0].value = inarg; 89 request_send_noreply(fc, req); 90 } 91 92 static void fuse_clear_inode(struct inode *inode) 93 { 94 if (inode->i_sb->s_flags & MS_ACTIVE) { 95 struct fuse_conn *fc = get_fuse_conn(inode); 96 struct fuse_inode *fi = get_fuse_inode(inode); 97 fuse_send_forget(fc, fi->forget_req, fi->nodeid, fi->nlookup); 98 fi->forget_req = NULL; 99 } 100 } 101 102 static int fuse_remount_fs(struct super_block *sb, int *flags, char *data) 103 { 104 if (*flags & MS_MANDLOCK) 105 return -EINVAL; 106 107 return 0; 108 } 109 110 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr) 111 { 112 struct fuse_conn *fc = get_fuse_conn(inode); 113 if (S_ISREG(inode->i_mode) && i_size_read(inode) != attr->size) 114 invalidate_inode_pages(inode->i_mapping); 115 116 inode->i_ino = attr->ino; 117 inode->i_mode = (inode->i_mode & S_IFMT) + (attr->mode & 07777); 118 inode->i_nlink = attr->nlink; 119 inode->i_uid = attr->uid; 120 inode->i_gid = attr->gid; 121 spin_lock(&fc->lock); 122 i_size_write(inode, attr->size); 123 spin_unlock(&fc->lock); 124 inode->i_blocks = attr->blocks; 125 inode->i_atime.tv_sec = attr->atime; 126 inode->i_atime.tv_nsec = attr->atimensec; 127 inode->i_mtime.tv_sec = attr->mtime; 128 inode->i_mtime.tv_nsec = attr->mtimensec; 129 inode->i_ctime.tv_sec = attr->ctime; 130 inode->i_ctime.tv_nsec = attr->ctimensec; 131 } 132 133 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) 134 { 135 inode->i_mode = attr->mode & S_IFMT; 136 inode->i_size = attr->size; 137 if (S_ISREG(inode->i_mode)) { 138 fuse_init_common(inode); 139 fuse_init_file_inode(inode); 140 } else if (S_ISDIR(inode->i_mode)) 141 fuse_init_dir(inode); 142 else if (S_ISLNK(inode->i_mode)) 143 fuse_init_symlink(inode); 144 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 145 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 146 fuse_init_common(inode); 147 init_special_inode(inode, inode->i_mode, 148 new_decode_dev(attr->rdev)); 149 } else 150 BUG(); 151 } 152 153 static int fuse_inode_eq(struct inode *inode, void *_nodeidp) 154 { 155 unsigned long nodeid = *(unsigned long *) _nodeidp; 156 if (get_node_id(inode) == nodeid) 157 return 1; 158 else 159 return 0; 160 } 161 162 static int fuse_inode_set(struct inode *inode, void *_nodeidp) 163 { 164 unsigned long nodeid = *(unsigned long *) _nodeidp; 165 get_fuse_inode(inode)->nodeid = nodeid; 166 return 0; 167 } 168 169 struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, 170 int generation, struct fuse_attr *attr) 171 { 172 struct inode *inode; 173 struct fuse_inode *fi; 174 struct fuse_conn *fc = get_fuse_conn_super(sb); 175 int retried = 0; 176 177 retry: 178 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); 179 if (!inode) 180 return NULL; 181 182 if ((inode->i_state & I_NEW)) { 183 inode->i_flags |= S_NOATIME|S_NOCMTIME; 184 inode->i_generation = generation; 185 inode->i_data.backing_dev_info = &fc->bdi; 186 fuse_init_inode(inode, attr); 187 unlock_new_inode(inode); 188 } else if ((inode->i_mode ^ attr->mode) & S_IFMT) { 189 BUG_ON(retried); 190 /* Inode has changed type, any I/O on the old should fail */ 191 make_bad_inode(inode); 192 iput(inode); 193 retried = 1; 194 goto retry; 195 } 196 197 fi = get_fuse_inode(inode); 198 spin_lock(&fc->lock); 199 fi->nlookup ++; 200 spin_unlock(&fc->lock); 201 fuse_change_attributes(inode, attr); 202 return inode; 203 } 204 205 static void fuse_umount_begin(struct vfsmount *vfsmnt, int flags) 206 { 207 if (flags & MNT_FORCE) 208 fuse_abort_conn(get_fuse_conn_super(vfsmnt->mnt_sb)); 209 } 210 211 static void fuse_put_super(struct super_block *sb) 212 { 213 struct fuse_conn *fc = get_fuse_conn_super(sb); 214 215 spin_lock(&fc->lock); 216 fc->connected = 0; 217 fc->blocked = 0; 218 spin_unlock(&fc->lock); 219 /* Flush all readers on this fs */ 220 kill_fasync(&fc->fasync, SIGIO, POLL_IN); 221 wake_up_all(&fc->waitq); 222 wake_up_all(&fc->blocked_waitq); 223 mutex_lock(&fuse_mutex); 224 list_del(&fc->entry); 225 fuse_ctl_remove_conn(fc); 226 mutex_unlock(&fuse_mutex); 227 fuse_conn_put(fc); 228 } 229 230 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) 231 { 232 stbuf->f_type = FUSE_SUPER_MAGIC; 233 stbuf->f_bsize = attr->bsize; 234 stbuf->f_frsize = attr->frsize; 235 stbuf->f_blocks = attr->blocks; 236 stbuf->f_bfree = attr->bfree; 237 stbuf->f_bavail = attr->bavail; 238 stbuf->f_files = attr->files; 239 stbuf->f_ffree = attr->ffree; 240 stbuf->f_namelen = attr->namelen; 241 /* fsid is left zero */ 242 } 243 244 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) 245 { 246 struct super_block *sb = dentry->d_sb; 247 struct fuse_conn *fc = get_fuse_conn_super(sb); 248 struct fuse_req *req; 249 struct fuse_statfs_out outarg; 250 int err; 251 252 req = fuse_get_req(fc); 253 if (IS_ERR(req)) 254 return PTR_ERR(req); 255 256 memset(&outarg, 0, sizeof(outarg)); 257 req->in.numargs = 0; 258 req->in.h.opcode = FUSE_STATFS; 259 req->in.h.nodeid = get_node_id(dentry->d_inode); 260 req->out.numargs = 1; 261 req->out.args[0].size = 262 fc->minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(outarg); 263 req->out.args[0].value = &outarg; 264 request_send(fc, req); 265 err = req->out.h.error; 266 if (!err) 267 convert_fuse_statfs(buf, &outarg.st); 268 fuse_put_request(fc, req); 269 return err; 270 } 271 272 enum { 273 OPT_FD, 274 OPT_ROOTMODE, 275 OPT_USER_ID, 276 OPT_GROUP_ID, 277 OPT_DEFAULT_PERMISSIONS, 278 OPT_ALLOW_OTHER, 279 OPT_MAX_READ, 280 OPT_ERR 281 }; 282 283 static match_table_t tokens = { 284 {OPT_FD, "fd=%u"}, 285 {OPT_ROOTMODE, "rootmode=%o"}, 286 {OPT_USER_ID, "user_id=%u"}, 287 {OPT_GROUP_ID, "group_id=%u"}, 288 {OPT_DEFAULT_PERMISSIONS, "default_permissions"}, 289 {OPT_ALLOW_OTHER, "allow_other"}, 290 {OPT_MAX_READ, "max_read=%u"}, 291 {OPT_ERR, NULL} 292 }; 293 294 static int parse_fuse_opt(char *opt, struct fuse_mount_data *d) 295 { 296 char *p; 297 memset(d, 0, sizeof(struct fuse_mount_data)); 298 d->max_read = ~0; 299 300 while ((p = strsep(&opt, ",")) != NULL) { 301 int token; 302 int value; 303 substring_t args[MAX_OPT_ARGS]; 304 if (!*p) 305 continue; 306 307 token = match_token(p, tokens, args); 308 switch (token) { 309 case OPT_FD: 310 if (match_int(&args[0], &value)) 311 return 0; 312 d->fd = value; 313 d->fd_present = 1; 314 break; 315 316 case OPT_ROOTMODE: 317 if (match_octal(&args[0], &value)) 318 return 0; 319 d->rootmode = value; 320 d->rootmode_present = 1; 321 break; 322 323 case OPT_USER_ID: 324 if (match_int(&args[0], &value)) 325 return 0; 326 d->user_id = value; 327 d->user_id_present = 1; 328 break; 329 330 case OPT_GROUP_ID: 331 if (match_int(&args[0], &value)) 332 return 0; 333 d->group_id = value; 334 d->group_id_present = 1; 335 break; 336 337 case OPT_DEFAULT_PERMISSIONS: 338 d->flags |= FUSE_DEFAULT_PERMISSIONS; 339 break; 340 341 case OPT_ALLOW_OTHER: 342 d->flags |= FUSE_ALLOW_OTHER; 343 break; 344 345 case OPT_MAX_READ: 346 if (match_int(&args[0], &value)) 347 return 0; 348 d->max_read = value; 349 break; 350 351 default: 352 return 0; 353 } 354 } 355 356 if (!d->fd_present || !d->rootmode_present || 357 !d->user_id_present || !d->group_id_present) 358 return 0; 359 360 return 1; 361 } 362 363 static int fuse_show_options(struct seq_file *m, struct vfsmount *mnt) 364 { 365 struct fuse_conn *fc = get_fuse_conn_super(mnt->mnt_sb); 366 367 seq_printf(m, ",user_id=%u", fc->user_id); 368 seq_printf(m, ",group_id=%u", fc->group_id); 369 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) 370 seq_puts(m, ",default_permissions"); 371 if (fc->flags & FUSE_ALLOW_OTHER) 372 seq_puts(m, ",allow_other"); 373 if (fc->max_read != ~0) 374 seq_printf(m, ",max_read=%u", fc->max_read); 375 return 0; 376 } 377 378 static struct fuse_conn *new_conn(void) 379 { 380 struct fuse_conn *fc; 381 382 fc = kzalloc(sizeof(*fc), GFP_KERNEL); 383 if (fc) { 384 spin_lock_init(&fc->lock); 385 atomic_set(&fc->count, 1); 386 init_waitqueue_head(&fc->waitq); 387 init_waitqueue_head(&fc->blocked_waitq); 388 INIT_LIST_HEAD(&fc->pending); 389 INIT_LIST_HEAD(&fc->processing); 390 INIT_LIST_HEAD(&fc->io); 391 INIT_LIST_HEAD(&fc->interrupts); 392 atomic_set(&fc->num_waiting, 0); 393 fc->bdi.ra_pages = (VM_MAX_READAHEAD * 1024) / PAGE_CACHE_SIZE; 394 fc->bdi.unplug_io_fn = default_unplug_io_fn; 395 fc->reqctr = 0; 396 fc->blocked = 1; 397 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key)); 398 } 399 return fc; 400 } 401 402 void fuse_conn_put(struct fuse_conn *fc) 403 { 404 if (atomic_dec_and_test(&fc->count)) 405 kfree(fc); 406 } 407 408 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc) 409 { 410 atomic_inc(&fc->count); 411 return fc; 412 } 413 414 static struct inode *get_root_inode(struct super_block *sb, unsigned mode) 415 { 416 struct fuse_attr attr; 417 memset(&attr, 0, sizeof(attr)); 418 419 attr.mode = mode; 420 attr.ino = FUSE_ROOT_ID; 421 return fuse_iget(sb, 1, 0, &attr); 422 } 423 424 static struct super_operations fuse_super_operations = { 425 .alloc_inode = fuse_alloc_inode, 426 .destroy_inode = fuse_destroy_inode, 427 .read_inode = fuse_read_inode, 428 .clear_inode = fuse_clear_inode, 429 .remount_fs = fuse_remount_fs, 430 .put_super = fuse_put_super, 431 .umount_begin = fuse_umount_begin, 432 .statfs = fuse_statfs, 433 .show_options = fuse_show_options, 434 }; 435 436 static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req) 437 { 438 struct fuse_init_out *arg = &req->misc.init_out; 439 440 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION) 441 fc->conn_error = 1; 442 else { 443 unsigned long ra_pages; 444 445 if (arg->minor >= 6) { 446 ra_pages = arg->max_readahead / PAGE_CACHE_SIZE; 447 if (arg->flags & FUSE_ASYNC_READ) 448 fc->async_read = 1; 449 if (!(arg->flags & FUSE_POSIX_LOCKS)) 450 fc->no_lock = 1; 451 } else { 452 ra_pages = fc->max_read / PAGE_CACHE_SIZE; 453 fc->no_lock = 1; 454 } 455 456 fc->bdi.ra_pages = min(fc->bdi.ra_pages, ra_pages); 457 fc->minor = arg->minor; 458 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; 459 } 460 fuse_put_request(fc, req); 461 fc->blocked = 0; 462 wake_up_all(&fc->blocked_waitq); 463 } 464 465 static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req) 466 { 467 struct fuse_init_in *arg = &req->misc.init_in; 468 469 arg->major = FUSE_KERNEL_VERSION; 470 arg->minor = FUSE_KERNEL_MINOR_VERSION; 471 arg->max_readahead = fc->bdi.ra_pages * PAGE_CACHE_SIZE; 472 arg->flags |= FUSE_ASYNC_READ | FUSE_POSIX_LOCKS; 473 req->in.h.opcode = FUSE_INIT; 474 req->in.numargs = 1; 475 req->in.args[0].size = sizeof(*arg); 476 req->in.args[0].value = arg; 477 req->out.numargs = 1; 478 /* Variable length arguement used for backward compatibility 479 with interface version < 7.5. Rest of init_out is zeroed 480 by do_get_request(), so a short reply is not a problem */ 481 req->out.argvar = 1; 482 req->out.args[0].size = sizeof(struct fuse_init_out); 483 req->out.args[0].value = &req->misc.init_out; 484 req->end = process_init_reply; 485 request_send_background(fc, req); 486 } 487 488 static u64 conn_id(void) 489 { 490 static u64 ctr = 1; 491 return ctr++; 492 } 493 494 static int fuse_fill_super(struct super_block *sb, void *data, int silent) 495 { 496 struct fuse_conn *fc; 497 struct inode *root; 498 struct fuse_mount_data d; 499 struct file *file; 500 struct dentry *root_dentry; 501 struct fuse_req *init_req; 502 int err; 503 504 if (sb->s_flags & MS_MANDLOCK) 505 return -EINVAL; 506 507 if (!parse_fuse_opt((char *) data, &d)) 508 return -EINVAL; 509 510 sb->s_blocksize = PAGE_CACHE_SIZE; 511 sb->s_blocksize_bits = PAGE_CACHE_SHIFT; 512 sb->s_magic = FUSE_SUPER_MAGIC; 513 sb->s_op = &fuse_super_operations; 514 sb->s_maxbytes = MAX_LFS_FILESIZE; 515 516 file = fget(d.fd); 517 if (!file) 518 return -EINVAL; 519 520 if (file->f_op != &fuse_dev_operations) 521 return -EINVAL; 522 523 fc = new_conn(); 524 if (!fc) 525 return -ENOMEM; 526 527 fc->flags = d.flags; 528 fc->user_id = d.user_id; 529 fc->group_id = d.group_id; 530 fc->max_read = d.max_read; 531 532 /* Used by get_root_inode() */ 533 sb->s_fs_info = fc; 534 535 err = -ENOMEM; 536 root = get_root_inode(sb, d.rootmode); 537 if (!root) 538 goto err; 539 540 root_dentry = d_alloc_root(root); 541 if (!root_dentry) { 542 iput(root); 543 goto err; 544 } 545 546 init_req = fuse_request_alloc(); 547 if (!init_req) 548 goto err_put_root; 549 550 mutex_lock(&fuse_mutex); 551 err = -EINVAL; 552 if (file->private_data) 553 goto err_unlock; 554 555 fc->id = conn_id(); 556 err = fuse_ctl_add_conn(fc); 557 if (err) 558 goto err_unlock; 559 560 list_add_tail(&fc->entry, &fuse_conn_list); 561 sb->s_root = root_dentry; 562 fc->connected = 1; 563 file->private_data = fuse_conn_get(fc); 564 mutex_unlock(&fuse_mutex); 565 /* 566 * atomic_dec_and_test() in fput() provides the necessary 567 * memory barrier for file->private_data to be visible on all 568 * CPUs after this 569 */ 570 fput(file); 571 572 fuse_send_init(fc, init_req); 573 574 return 0; 575 576 err_unlock: 577 mutex_unlock(&fuse_mutex); 578 fuse_request_free(init_req); 579 err_put_root: 580 dput(root_dentry); 581 err: 582 fput(file); 583 fuse_conn_put(fc); 584 return err; 585 } 586 587 static int fuse_get_sb(struct file_system_type *fs_type, 588 int flags, const char *dev_name, 589 void *raw_data, struct vfsmount *mnt) 590 { 591 return get_sb_nodev(fs_type, flags, raw_data, fuse_fill_super, mnt); 592 } 593 594 static struct file_system_type fuse_fs_type = { 595 .owner = THIS_MODULE, 596 .name = "fuse", 597 .get_sb = fuse_get_sb, 598 .kill_sb = kill_anon_super, 599 }; 600 601 static decl_subsys(fuse, NULL, NULL); 602 static decl_subsys(connections, NULL, NULL); 603 604 static void fuse_inode_init_once(void *foo, kmem_cache_t *cachep, 605 unsigned long flags) 606 { 607 struct inode * inode = foo; 608 609 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == 610 SLAB_CTOR_CONSTRUCTOR) 611 inode_init_once(inode); 612 } 613 614 static int __init fuse_fs_init(void) 615 { 616 int err; 617 618 err = register_filesystem(&fuse_fs_type); 619 if (err) 620 printk("fuse: failed to register filesystem\n"); 621 else { 622 fuse_inode_cachep = kmem_cache_create("fuse_inode", 623 sizeof(struct fuse_inode), 624 0, SLAB_HWCACHE_ALIGN, 625 fuse_inode_init_once, NULL); 626 if (!fuse_inode_cachep) { 627 unregister_filesystem(&fuse_fs_type); 628 err = -ENOMEM; 629 } 630 } 631 632 return err; 633 } 634 635 static void fuse_fs_cleanup(void) 636 { 637 unregister_filesystem(&fuse_fs_type); 638 kmem_cache_destroy(fuse_inode_cachep); 639 } 640 641 static int fuse_sysfs_init(void) 642 { 643 int err; 644 645 kset_set_kset_s(&fuse_subsys, fs_subsys); 646 err = subsystem_register(&fuse_subsys); 647 if (err) 648 goto out_err; 649 650 kset_set_kset_s(&connections_subsys, fuse_subsys); 651 err = subsystem_register(&connections_subsys); 652 if (err) 653 goto out_fuse_unregister; 654 655 return 0; 656 657 out_fuse_unregister: 658 subsystem_unregister(&fuse_subsys); 659 out_err: 660 return err; 661 } 662 663 static void fuse_sysfs_cleanup(void) 664 { 665 subsystem_unregister(&connections_subsys); 666 subsystem_unregister(&fuse_subsys); 667 } 668 669 static int __init fuse_init(void) 670 { 671 int res; 672 673 printk("fuse init (API version %i.%i)\n", 674 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); 675 676 INIT_LIST_HEAD(&fuse_conn_list); 677 res = fuse_fs_init(); 678 if (res) 679 goto err; 680 681 res = fuse_dev_init(); 682 if (res) 683 goto err_fs_cleanup; 684 685 res = fuse_sysfs_init(); 686 if (res) 687 goto err_dev_cleanup; 688 689 res = fuse_ctl_init(); 690 if (res) 691 goto err_sysfs_cleanup; 692 693 return 0; 694 695 err_sysfs_cleanup: 696 fuse_sysfs_cleanup(); 697 err_dev_cleanup: 698 fuse_dev_cleanup(); 699 err_fs_cleanup: 700 fuse_fs_cleanup(); 701 err: 702 return res; 703 } 704 705 static void __exit fuse_exit(void) 706 { 707 printk(KERN_DEBUG "fuse exit\n"); 708 709 fuse_ctl_cleanup(); 710 fuse_sysfs_cleanup(); 711 fuse_fs_cleanup(); 712 fuse_dev_cleanup(); 713 } 714 715 module_init(fuse_init); 716 module_exit(fuse_exit); 717