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