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