1 /* 2 FUSE: Filesystem in Userspace 3 Copyright (C) 2001-2008 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/moduleparam.h> 18 #include <linux/fs_context.h> 19 #include <linux/fs_parser.h> 20 #include <linux/statfs.h> 21 #include <linux/random.h> 22 #include <linux/sched.h> 23 #include <linux/exportfs.h> 24 #include <linux/posix_acl.h> 25 #include <linux/pid_namespace.h> 26 27 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); 28 MODULE_DESCRIPTION("Filesystem in Userspace"); 29 MODULE_LICENSE("GPL"); 30 31 static struct kmem_cache *fuse_inode_cachep; 32 struct list_head fuse_conn_list; 33 DEFINE_MUTEX(fuse_mutex); 34 35 static int set_global_limit(const char *val, const struct kernel_param *kp); 36 37 unsigned max_user_bgreq; 38 module_param_call(max_user_bgreq, set_global_limit, param_get_uint, 39 &max_user_bgreq, 0644); 40 __MODULE_PARM_TYPE(max_user_bgreq, "uint"); 41 MODULE_PARM_DESC(max_user_bgreq, 42 "Global limit for the maximum number of backgrounded requests an " 43 "unprivileged user can set"); 44 45 unsigned max_user_congthresh; 46 module_param_call(max_user_congthresh, set_global_limit, param_get_uint, 47 &max_user_congthresh, 0644); 48 __MODULE_PARM_TYPE(max_user_congthresh, "uint"); 49 MODULE_PARM_DESC(max_user_congthresh, 50 "Global limit for the maximum congestion threshold an " 51 "unprivileged user can set"); 52 53 #define FUSE_SUPER_MAGIC 0x65735546 54 55 #define FUSE_DEFAULT_BLKSIZE 512 56 57 /** Maximum number of outstanding background requests */ 58 #define FUSE_DEFAULT_MAX_BACKGROUND 12 59 60 /** Congestion starts at 75% of maximum */ 61 #define FUSE_DEFAULT_CONGESTION_THRESHOLD (FUSE_DEFAULT_MAX_BACKGROUND * 3 / 4) 62 63 #ifdef CONFIG_BLOCK 64 static struct file_system_type fuseblk_fs_type; 65 #endif 66 67 struct fuse_forget_link *fuse_alloc_forget(void) 68 { 69 return kzalloc(sizeof(struct fuse_forget_link), GFP_KERNEL_ACCOUNT); 70 } 71 72 static struct inode *fuse_alloc_inode(struct super_block *sb) 73 { 74 struct fuse_inode *fi; 75 76 fi = kmem_cache_alloc(fuse_inode_cachep, GFP_KERNEL); 77 if (!fi) 78 return NULL; 79 80 fi->i_time = 0; 81 fi->inval_mask = 0; 82 fi->nodeid = 0; 83 fi->nlookup = 0; 84 fi->attr_version = 0; 85 fi->orig_ino = 0; 86 fi->state = 0; 87 mutex_init(&fi->mutex); 88 spin_lock_init(&fi->lock); 89 fi->forget = fuse_alloc_forget(); 90 if (!fi->forget) 91 goto out_free; 92 93 if (IS_ENABLED(CONFIG_FUSE_DAX) && !fuse_dax_inode_alloc(sb, fi)) 94 goto out_free_forget; 95 96 return &fi->inode; 97 98 out_free_forget: 99 kfree(fi->forget); 100 out_free: 101 kmem_cache_free(fuse_inode_cachep, fi); 102 return NULL; 103 } 104 105 static void fuse_free_inode(struct inode *inode) 106 { 107 struct fuse_inode *fi = get_fuse_inode(inode); 108 109 mutex_destroy(&fi->mutex); 110 kfree(fi->forget); 111 #ifdef CONFIG_FUSE_DAX 112 kfree(fi->dax); 113 #endif 114 kmem_cache_free(fuse_inode_cachep, fi); 115 } 116 117 static void fuse_evict_inode(struct inode *inode) 118 { 119 struct fuse_inode *fi = get_fuse_inode(inode); 120 121 /* Will write inode on close/munmap and in all other dirtiers */ 122 WARN_ON(inode->i_state & I_DIRTY_INODE); 123 124 truncate_inode_pages_final(&inode->i_data); 125 clear_inode(inode); 126 if (inode->i_sb->s_flags & SB_ACTIVE) { 127 struct fuse_conn *fc = get_fuse_conn(inode); 128 129 if (FUSE_IS_DAX(inode)) 130 fuse_dax_inode_cleanup(inode); 131 if (fi->nlookup) { 132 fuse_queue_forget(fc, fi->forget, fi->nodeid, 133 fi->nlookup); 134 fi->forget = NULL; 135 } 136 } 137 if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) { 138 WARN_ON(!list_empty(&fi->write_files)); 139 WARN_ON(!list_empty(&fi->queued_writes)); 140 } 141 } 142 143 static int fuse_reconfigure(struct fs_context *fsc) 144 { 145 struct super_block *sb = fsc->root->d_sb; 146 147 sync_filesystem(sb); 148 if (fsc->sb_flags & SB_MANDLOCK) 149 return -EINVAL; 150 151 return 0; 152 } 153 154 /* 155 * ino_t is 32-bits on 32-bit arch. We have to squash the 64-bit value down 156 * so that it will fit. 157 */ 158 static ino_t fuse_squash_ino(u64 ino64) 159 { 160 ino_t ino = (ino_t) ino64; 161 if (sizeof(ino_t) < sizeof(u64)) 162 ino ^= ino64 >> (sizeof(u64) - sizeof(ino_t)) * 8; 163 return ino; 164 } 165 166 void fuse_change_attributes_common(struct inode *inode, struct fuse_attr *attr, 167 u64 attr_valid) 168 { 169 struct fuse_conn *fc = get_fuse_conn(inode); 170 struct fuse_inode *fi = get_fuse_inode(inode); 171 172 lockdep_assert_held(&fi->lock); 173 174 fi->attr_version = atomic64_inc_return(&fc->attr_version); 175 fi->i_time = attr_valid; 176 WRITE_ONCE(fi->inval_mask, 0); 177 178 inode->i_ino = fuse_squash_ino(attr->ino); 179 inode->i_mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); 180 set_nlink(inode, attr->nlink); 181 inode->i_uid = make_kuid(fc->user_ns, attr->uid); 182 inode->i_gid = make_kgid(fc->user_ns, attr->gid); 183 inode->i_blocks = attr->blocks; 184 inode->i_atime.tv_sec = attr->atime; 185 inode->i_atime.tv_nsec = attr->atimensec; 186 /* mtime from server may be stale due to local buffered write */ 187 if (!fc->writeback_cache || !S_ISREG(inode->i_mode)) { 188 inode->i_mtime.tv_sec = attr->mtime; 189 inode->i_mtime.tv_nsec = attr->mtimensec; 190 inode->i_ctime.tv_sec = attr->ctime; 191 inode->i_ctime.tv_nsec = attr->ctimensec; 192 } 193 194 if (attr->blksize != 0) 195 inode->i_blkbits = ilog2(attr->blksize); 196 else 197 inode->i_blkbits = inode->i_sb->s_blocksize_bits; 198 199 /* 200 * Don't set the sticky bit in i_mode, unless we want the VFS 201 * to check permissions. This prevents failures due to the 202 * check in may_delete(). 203 */ 204 fi->orig_i_mode = inode->i_mode; 205 if (!fc->default_permissions) 206 inode->i_mode &= ~S_ISVTX; 207 208 fi->orig_ino = attr->ino; 209 210 /* 211 * We are refreshing inode data and it is possible that another 212 * client set suid/sgid or security.capability xattr. So clear 213 * S_NOSEC. Ideally, we could have cleared it only if suid/sgid 214 * was set or if security.capability xattr was set. But we don't 215 * know if security.capability has been set or not. So clear it 216 * anyway. Its less efficient but should be safe. 217 */ 218 inode->i_flags &= ~S_NOSEC; 219 } 220 221 void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr, 222 u64 attr_valid, u64 attr_version) 223 { 224 struct fuse_conn *fc = get_fuse_conn(inode); 225 struct fuse_inode *fi = get_fuse_inode(inode); 226 bool is_wb = fc->writeback_cache && S_ISREG(inode->i_mode); 227 loff_t oldsize; 228 struct timespec64 old_mtime; 229 230 spin_lock(&fi->lock); 231 /* 232 * In case of writeback_cache enabled, writes update mtime, ctime and 233 * may update i_size. In these cases trust the cached value in the 234 * inode. 235 */ 236 if (is_wb) { 237 attr->size = i_size_read(inode); 238 attr->mtime = inode->i_mtime.tv_sec; 239 attr->mtimensec = inode->i_mtime.tv_nsec; 240 attr->ctime = inode->i_ctime.tv_sec; 241 attr->ctimensec = inode->i_ctime.tv_nsec; 242 } 243 244 if ((attr_version != 0 && fi->attr_version > attr_version) || 245 test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) { 246 spin_unlock(&fi->lock); 247 return; 248 } 249 250 old_mtime = inode->i_mtime; 251 fuse_change_attributes_common(inode, attr, attr_valid); 252 253 oldsize = inode->i_size; 254 /* 255 * In case of writeback_cache enabled, the cached writes beyond EOF 256 * extend local i_size without keeping userspace server in sync. So, 257 * attr->size coming from server can be stale. We cannot trust it. 258 */ 259 if (!is_wb) 260 i_size_write(inode, attr->size); 261 spin_unlock(&fi->lock); 262 263 if (!is_wb && S_ISREG(inode->i_mode)) { 264 bool inval = false; 265 266 if (oldsize != attr->size) { 267 truncate_pagecache(inode, attr->size); 268 if (!fc->explicit_inval_data) 269 inval = true; 270 } else if (fc->auto_inval_data) { 271 struct timespec64 new_mtime = { 272 .tv_sec = attr->mtime, 273 .tv_nsec = attr->mtimensec, 274 }; 275 276 /* 277 * Auto inval mode also checks and invalidates if mtime 278 * has changed. 279 */ 280 if (!timespec64_equal(&old_mtime, &new_mtime)) 281 inval = true; 282 } 283 284 if (inval) 285 invalidate_inode_pages2(inode->i_mapping); 286 } 287 } 288 289 static void fuse_init_inode(struct inode *inode, struct fuse_attr *attr) 290 { 291 inode->i_mode = attr->mode & S_IFMT; 292 inode->i_size = attr->size; 293 inode->i_mtime.tv_sec = attr->mtime; 294 inode->i_mtime.tv_nsec = attr->mtimensec; 295 inode->i_ctime.tv_sec = attr->ctime; 296 inode->i_ctime.tv_nsec = attr->ctimensec; 297 if (S_ISREG(inode->i_mode)) { 298 fuse_init_common(inode); 299 fuse_init_file_inode(inode); 300 } else if (S_ISDIR(inode->i_mode)) 301 fuse_init_dir(inode); 302 else if (S_ISLNK(inode->i_mode)) 303 fuse_init_symlink(inode); 304 else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) || 305 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { 306 fuse_init_common(inode); 307 init_special_inode(inode, inode->i_mode, 308 new_decode_dev(attr->rdev)); 309 } else 310 BUG(); 311 } 312 313 static int fuse_inode_eq(struct inode *inode, void *_nodeidp) 314 { 315 u64 nodeid = *(u64 *) _nodeidp; 316 if (get_node_id(inode) == nodeid) 317 return 1; 318 else 319 return 0; 320 } 321 322 static int fuse_inode_set(struct inode *inode, void *_nodeidp) 323 { 324 u64 nodeid = *(u64 *) _nodeidp; 325 get_fuse_inode(inode)->nodeid = nodeid; 326 return 0; 327 } 328 329 struct inode *fuse_iget(struct super_block *sb, u64 nodeid, 330 int generation, struct fuse_attr *attr, 331 u64 attr_valid, u64 attr_version) 332 { 333 struct inode *inode; 334 struct fuse_inode *fi; 335 struct fuse_conn *fc = get_fuse_conn_super(sb); 336 337 /* 338 * Auto mount points get their node id from the submount root, which is 339 * not a unique identifier within this filesystem. 340 * 341 * To avoid conflicts, do not place submount points into the inode hash 342 * table. 343 */ 344 if (fc->auto_submounts && (attr->flags & FUSE_ATTR_SUBMOUNT) && 345 S_ISDIR(attr->mode)) { 346 inode = new_inode(sb); 347 if (!inode) 348 return NULL; 349 350 fuse_init_inode(inode, attr); 351 get_fuse_inode(inode)->nodeid = nodeid; 352 inode->i_flags |= S_AUTOMOUNT; 353 goto done; 354 } 355 356 retry: 357 inode = iget5_locked(sb, nodeid, fuse_inode_eq, fuse_inode_set, &nodeid); 358 if (!inode) 359 return NULL; 360 361 if ((inode->i_state & I_NEW)) { 362 inode->i_flags |= S_NOATIME; 363 if (!fc->writeback_cache || !S_ISREG(attr->mode)) 364 inode->i_flags |= S_NOCMTIME; 365 inode->i_generation = generation; 366 fuse_init_inode(inode, attr); 367 unlock_new_inode(inode); 368 } else if (fuse_stale_inode(inode, generation, attr)) { 369 /* nodeid was reused, any I/O on the old inode should fail */ 370 fuse_make_bad(inode); 371 iput(inode); 372 goto retry; 373 } 374 done: 375 fi = get_fuse_inode(inode); 376 spin_lock(&fi->lock); 377 fi->nlookup++; 378 spin_unlock(&fi->lock); 379 fuse_change_attributes(inode, attr, attr_valid, attr_version); 380 381 return inode; 382 } 383 384 struct inode *fuse_ilookup(struct fuse_conn *fc, u64 nodeid, 385 struct fuse_mount **fm) 386 { 387 struct fuse_mount *fm_iter; 388 struct inode *inode; 389 390 WARN_ON(!rwsem_is_locked(&fc->killsb)); 391 list_for_each_entry(fm_iter, &fc->mounts, fc_entry) { 392 if (!fm_iter->sb) 393 continue; 394 395 inode = ilookup5(fm_iter->sb, nodeid, fuse_inode_eq, &nodeid); 396 if (inode) { 397 if (fm) 398 *fm = fm_iter; 399 return inode; 400 } 401 } 402 403 return NULL; 404 } 405 406 int fuse_reverse_inval_inode(struct fuse_conn *fc, u64 nodeid, 407 loff_t offset, loff_t len) 408 { 409 struct fuse_inode *fi; 410 struct inode *inode; 411 pgoff_t pg_start; 412 pgoff_t pg_end; 413 414 inode = fuse_ilookup(fc, nodeid, NULL); 415 if (!inode) 416 return -ENOENT; 417 418 fi = get_fuse_inode(inode); 419 spin_lock(&fi->lock); 420 fi->attr_version = atomic64_inc_return(&fc->attr_version); 421 spin_unlock(&fi->lock); 422 423 fuse_invalidate_attr(inode); 424 forget_all_cached_acls(inode); 425 if (offset >= 0) { 426 pg_start = offset >> PAGE_SHIFT; 427 if (len <= 0) 428 pg_end = -1; 429 else 430 pg_end = (offset + len - 1) >> PAGE_SHIFT; 431 invalidate_inode_pages2_range(inode->i_mapping, 432 pg_start, pg_end); 433 } 434 iput(inode); 435 return 0; 436 } 437 438 bool fuse_lock_inode(struct inode *inode) 439 { 440 bool locked = false; 441 442 if (!get_fuse_conn(inode)->parallel_dirops) { 443 mutex_lock(&get_fuse_inode(inode)->mutex); 444 locked = true; 445 } 446 447 return locked; 448 } 449 450 void fuse_unlock_inode(struct inode *inode, bool locked) 451 { 452 if (locked) 453 mutex_unlock(&get_fuse_inode(inode)->mutex); 454 } 455 456 static void fuse_umount_begin(struct super_block *sb) 457 { 458 struct fuse_conn *fc = get_fuse_conn_super(sb); 459 460 if (!fc->no_force_umount) 461 fuse_abort_conn(fc); 462 } 463 464 static void fuse_send_destroy(struct fuse_mount *fm) 465 { 466 if (fm->fc->conn_init) { 467 FUSE_ARGS(args); 468 469 args.opcode = FUSE_DESTROY; 470 args.force = true; 471 args.nocreds = true; 472 fuse_simple_request(fm, &args); 473 } 474 } 475 476 static void convert_fuse_statfs(struct kstatfs *stbuf, struct fuse_kstatfs *attr) 477 { 478 stbuf->f_type = FUSE_SUPER_MAGIC; 479 stbuf->f_bsize = attr->bsize; 480 stbuf->f_frsize = attr->frsize; 481 stbuf->f_blocks = attr->blocks; 482 stbuf->f_bfree = attr->bfree; 483 stbuf->f_bavail = attr->bavail; 484 stbuf->f_files = attr->files; 485 stbuf->f_ffree = attr->ffree; 486 stbuf->f_namelen = attr->namelen; 487 /* fsid is left zero */ 488 } 489 490 static int fuse_statfs(struct dentry *dentry, struct kstatfs *buf) 491 { 492 struct super_block *sb = dentry->d_sb; 493 struct fuse_mount *fm = get_fuse_mount_super(sb); 494 FUSE_ARGS(args); 495 struct fuse_statfs_out outarg; 496 int err; 497 498 if (!fuse_allow_current_process(fm->fc)) { 499 buf->f_type = FUSE_SUPER_MAGIC; 500 return 0; 501 } 502 503 memset(&outarg, 0, sizeof(outarg)); 504 args.in_numargs = 0; 505 args.opcode = FUSE_STATFS; 506 args.nodeid = get_node_id(d_inode(dentry)); 507 args.out_numargs = 1; 508 args.out_args[0].size = sizeof(outarg); 509 args.out_args[0].value = &outarg; 510 err = fuse_simple_request(fm, &args); 511 if (!err) 512 convert_fuse_statfs(buf, &outarg.st); 513 return err; 514 } 515 516 static struct fuse_sync_bucket *fuse_sync_bucket_alloc(void) 517 { 518 struct fuse_sync_bucket *bucket; 519 520 bucket = kzalloc(sizeof(*bucket), GFP_KERNEL | __GFP_NOFAIL); 521 if (bucket) { 522 init_waitqueue_head(&bucket->waitq); 523 /* Initial active count */ 524 atomic_set(&bucket->count, 1); 525 } 526 return bucket; 527 } 528 529 static void fuse_sync_fs_writes(struct fuse_conn *fc) 530 { 531 struct fuse_sync_bucket *bucket, *new_bucket; 532 int count; 533 534 new_bucket = fuse_sync_bucket_alloc(); 535 spin_lock(&fc->lock); 536 bucket = rcu_dereference_protected(fc->curr_bucket, 1); 537 count = atomic_read(&bucket->count); 538 WARN_ON(count < 1); 539 /* No outstanding writes? */ 540 if (count == 1) { 541 spin_unlock(&fc->lock); 542 kfree(new_bucket); 543 return; 544 } 545 546 /* 547 * Completion of new bucket depends on completion of this bucket, so add 548 * one more count. 549 */ 550 atomic_inc(&new_bucket->count); 551 rcu_assign_pointer(fc->curr_bucket, new_bucket); 552 spin_unlock(&fc->lock); 553 /* 554 * Drop initial active count. At this point if all writes in this and 555 * ancestor buckets complete, the count will go to zero and this task 556 * will be woken up. 557 */ 558 atomic_dec(&bucket->count); 559 560 wait_event(bucket->waitq, atomic_read(&bucket->count) == 0); 561 562 /* Drop temp count on descendant bucket */ 563 fuse_sync_bucket_dec(new_bucket); 564 kfree_rcu(bucket, rcu); 565 } 566 567 static int fuse_sync_fs(struct super_block *sb, int wait) 568 { 569 struct fuse_mount *fm = get_fuse_mount_super(sb); 570 struct fuse_conn *fc = fm->fc; 571 struct fuse_syncfs_in inarg; 572 FUSE_ARGS(args); 573 int err; 574 575 /* 576 * Userspace cannot handle the wait == 0 case. Avoid a 577 * gratuitous roundtrip. 578 */ 579 if (!wait) 580 return 0; 581 582 /* The filesystem is being unmounted. Nothing to do. */ 583 if (!sb->s_root) 584 return 0; 585 586 if (!fc->sync_fs) 587 return 0; 588 589 fuse_sync_fs_writes(fc); 590 591 memset(&inarg, 0, sizeof(inarg)); 592 args.in_numargs = 1; 593 args.in_args[0].size = sizeof(inarg); 594 args.in_args[0].value = &inarg; 595 args.opcode = FUSE_SYNCFS; 596 args.nodeid = get_node_id(sb->s_root->d_inode); 597 args.out_numargs = 0; 598 599 err = fuse_simple_request(fm, &args); 600 if (err == -ENOSYS) { 601 fc->sync_fs = 0; 602 err = 0; 603 } 604 605 return err; 606 } 607 608 enum { 609 OPT_SOURCE, 610 OPT_SUBTYPE, 611 OPT_FD, 612 OPT_ROOTMODE, 613 OPT_USER_ID, 614 OPT_GROUP_ID, 615 OPT_DEFAULT_PERMISSIONS, 616 OPT_ALLOW_OTHER, 617 OPT_MAX_READ, 618 OPT_BLKSIZE, 619 OPT_ERR 620 }; 621 622 static const struct fs_parameter_spec fuse_fs_parameters[] = { 623 fsparam_string ("source", OPT_SOURCE), 624 fsparam_u32 ("fd", OPT_FD), 625 fsparam_u32oct ("rootmode", OPT_ROOTMODE), 626 fsparam_u32 ("user_id", OPT_USER_ID), 627 fsparam_u32 ("group_id", OPT_GROUP_ID), 628 fsparam_flag ("default_permissions", OPT_DEFAULT_PERMISSIONS), 629 fsparam_flag ("allow_other", OPT_ALLOW_OTHER), 630 fsparam_u32 ("max_read", OPT_MAX_READ), 631 fsparam_u32 ("blksize", OPT_BLKSIZE), 632 fsparam_string ("subtype", OPT_SUBTYPE), 633 {} 634 }; 635 636 static int fuse_parse_param(struct fs_context *fsc, struct fs_parameter *param) 637 { 638 struct fs_parse_result result; 639 struct fuse_fs_context *ctx = fsc->fs_private; 640 int opt; 641 642 if (fsc->purpose == FS_CONTEXT_FOR_RECONFIGURE) { 643 /* 644 * Ignore options coming from mount(MS_REMOUNT) for backward 645 * compatibility. 646 */ 647 if (fsc->oldapi) 648 return 0; 649 650 return invalfc(fsc, "No changes allowed in reconfigure"); 651 } 652 653 opt = fs_parse(fsc, fuse_fs_parameters, param, &result); 654 if (opt < 0) 655 return opt; 656 657 switch (opt) { 658 case OPT_SOURCE: 659 if (fsc->source) 660 return invalfc(fsc, "Multiple sources specified"); 661 fsc->source = param->string; 662 param->string = NULL; 663 break; 664 665 case OPT_SUBTYPE: 666 if (ctx->subtype) 667 return invalfc(fsc, "Multiple subtypes specified"); 668 ctx->subtype = param->string; 669 param->string = NULL; 670 return 0; 671 672 case OPT_FD: 673 ctx->fd = result.uint_32; 674 ctx->fd_present = true; 675 break; 676 677 case OPT_ROOTMODE: 678 if (!fuse_valid_type(result.uint_32)) 679 return invalfc(fsc, "Invalid rootmode"); 680 ctx->rootmode = result.uint_32; 681 ctx->rootmode_present = true; 682 break; 683 684 case OPT_USER_ID: 685 ctx->user_id = make_kuid(fsc->user_ns, result.uint_32); 686 if (!uid_valid(ctx->user_id)) 687 return invalfc(fsc, "Invalid user_id"); 688 ctx->user_id_present = true; 689 break; 690 691 case OPT_GROUP_ID: 692 ctx->group_id = make_kgid(fsc->user_ns, result.uint_32); 693 if (!gid_valid(ctx->group_id)) 694 return invalfc(fsc, "Invalid group_id"); 695 ctx->group_id_present = true; 696 break; 697 698 case OPT_DEFAULT_PERMISSIONS: 699 ctx->default_permissions = true; 700 break; 701 702 case OPT_ALLOW_OTHER: 703 ctx->allow_other = true; 704 break; 705 706 case OPT_MAX_READ: 707 ctx->max_read = result.uint_32; 708 break; 709 710 case OPT_BLKSIZE: 711 if (!ctx->is_bdev) 712 return invalfc(fsc, "blksize only supported for fuseblk"); 713 ctx->blksize = result.uint_32; 714 break; 715 716 default: 717 return -EINVAL; 718 } 719 720 return 0; 721 } 722 723 static void fuse_free_fsc(struct fs_context *fsc) 724 { 725 struct fuse_fs_context *ctx = fsc->fs_private; 726 727 if (ctx) { 728 kfree(ctx->subtype); 729 kfree(ctx); 730 } 731 } 732 733 static int fuse_show_options(struct seq_file *m, struct dentry *root) 734 { 735 struct super_block *sb = root->d_sb; 736 struct fuse_conn *fc = get_fuse_conn_super(sb); 737 738 if (fc->legacy_opts_show) { 739 seq_printf(m, ",user_id=%u", 740 from_kuid_munged(fc->user_ns, fc->user_id)); 741 seq_printf(m, ",group_id=%u", 742 from_kgid_munged(fc->user_ns, fc->group_id)); 743 if (fc->default_permissions) 744 seq_puts(m, ",default_permissions"); 745 if (fc->allow_other) 746 seq_puts(m, ",allow_other"); 747 if (fc->max_read != ~0) 748 seq_printf(m, ",max_read=%u", fc->max_read); 749 if (sb->s_bdev && sb->s_blocksize != FUSE_DEFAULT_BLKSIZE) 750 seq_printf(m, ",blksize=%lu", sb->s_blocksize); 751 } 752 #ifdef CONFIG_FUSE_DAX 753 if (fc->dax) 754 seq_puts(m, ",dax"); 755 #endif 756 757 return 0; 758 } 759 760 static void fuse_iqueue_init(struct fuse_iqueue *fiq, 761 const struct fuse_iqueue_ops *ops, 762 void *priv) 763 { 764 memset(fiq, 0, sizeof(struct fuse_iqueue)); 765 spin_lock_init(&fiq->lock); 766 init_waitqueue_head(&fiq->waitq); 767 INIT_LIST_HEAD(&fiq->pending); 768 INIT_LIST_HEAD(&fiq->interrupts); 769 fiq->forget_list_tail = &fiq->forget_list_head; 770 fiq->connected = 1; 771 fiq->ops = ops; 772 fiq->priv = priv; 773 } 774 775 static void fuse_pqueue_init(struct fuse_pqueue *fpq) 776 { 777 unsigned int i; 778 779 spin_lock_init(&fpq->lock); 780 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++) 781 INIT_LIST_HEAD(&fpq->processing[i]); 782 INIT_LIST_HEAD(&fpq->io); 783 fpq->connected = 1; 784 } 785 786 void fuse_conn_init(struct fuse_conn *fc, struct fuse_mount *fm, 787 struct user_namespace *user_ns, 788 const struct fuse_iqueue_ops *fiq_ops, void *fiq_priv) 789 { 790 memset(fc, 0, sizeof(*fc)); 791 spin_lock_init(&fc->lock); 792 spin_lock_init(&fc->bg_lock); 793 init_rwsem(&fc->killsb); 794 refcount_set(&fc->count, 1); 795 atomic_set(&fc->dev_count, 1); 796 init_waitqueue_head(&fc->blocked_waitq); 797 fuse_iqueue_init(&fc->iq, fiq_ops, fiq_priv); 798 INIT_LIST_HEAD(&fc->bg_queue); 799 INIT_LIST_HEAD(&fc->entry); 800 INIT_LIST_HEAD(&fc->devices); 801 atomic_set(&fc->num_waiting, 0); 802 fc->max_background = FUSE_DEFAULT_MAX_BACKGROUND; 803 fc->congestion_threshold = FUSE_DEFAULT_CONGESTION_THRESHOLD; 804 atomic64_set(&fc->khctr, 0); 805 fc->polled_files = RB_ROOT; 806 fc->blocked = 0; 807 fc->initialized = 0; 808 fc->connected = 1; 809 atomic64_set(&fc->attr_version, 1); 810 get_random_bytes(&fc->scramble_key, sizeof(fc->scramble_key)); 811 fc->pid_ns = get_pid_ns(task_active_pid_ns(current)); 812 fc->user_ns = get_user_ns(user_ns); 813 fc->max_pages = FUSE_DEFAULT_MAX_PAGES_PER_REQ; 814 fc->max_pages_limit = FUSE_MAX_MAX_PAGES; 815 816 INIT_LIST_HEAD(&fc->mounts); 817 list_add(&fm->fc_entry, &fc->mounts); 818 fm->fc = fc; 819 } 820 EXPORT_SYMBOL_GPL(fuse_conn_init); 821 822 void fuse_conn_put(struct fuse_conn *fc) 823 { 824 if (refcount_dec_and_test(&fc->count)) { 825 struct fuse_iqueue *fiq = &fc->iq; 826 struct fuse_sync_bucket *bucket; 827 828 if (IS_ENABLED(CONFIG_FUSE_DAX)) 829 fuse_dax_conn_free(fc); 830 if (fiq->ops->release) 831 fiq->ops->release(fiq); 832 put_pid_ns(fc->pid_ns); 833 put_user_ns(fc->user_ns); 834 bucket = rcu_dereference_protected(fc->curr_bucket, 1); 835 if (bucket) { 836 WARN_ON(atomic_read(&bucket->count) != 1); 837 kfree(bucket); 838 } 839 fc->release(fc); 840 } 841 } 842 EXPORT_SYMBOL_GPL(fuse_conn_put); 843 844 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc) 845 { 846 refcount_inc(&fc->count); 847 return fc; 848 } 849 EXPORT_SYMBOL_GPL(fuse_conn_get); 850 851 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode) 852 { 853 struct fuse_attr attr; 854 memset(&attr, 0, sizeof(attr)); 855 856 attr.mode = mode; 857 attr.ino = FUSE_ROOT_ID; 858 attr.nlink = 1; 859 return fuse_iget(sb, 1, 0, &attr, 0, 0); 860 } 861 862 struct fuse_inode_handle { 863 u64 nodeid; 864 u32 generation; 865 }; 866 867 static struct dentry *fuse_get_dentry(struct super_block *sb, 868 struct fuse_inode_handle *handle) 869 { 870 struct fuse_conn *fc = get_fuse_conn_super(sb); 871 struct inode *inode; 872 struct dentry *entry; 873 int err = -ESTALE; 874 875 if (handle->nodeid == 0) 876 goto out_err; 877 878 inode = ilookup5(sb, handle->nodeid, fuse_inode_eq, &handle->nodeid); 879 if (!inode) { 880 struct fuse_entry_out outarg; 881 const struct qstr name = QSTR_INIT(".", 1); 882 883 if (!fc->export_support) 884 goto out_err; 885 886 err = fuse_lookup_name(sb, handle->nodeid, &name, &outarg, 887 &inode); 888 if (err && err != -ENOENT) 889 goto out_err; 890 if (err || !inode) { 891 err = -ESTALE; 892 goto out_err; 893 } 894 err = -EIO; 895 if (get_node_id(inode) != handle->nodeid) 896 goto out_iput; 897 } 898 err = -ESTALE; 899 if (inode->i_generation != handle->generation) 900 goto out_iput; 901 902 entry = d_obtain_alias(inode); 903 if (!IS_ERR(entry) && get_node_id(inode) != FUSE_ROOT_ID) 904 fuse_invalidate_entry_cache(entry); 905 906 return entry; 907 908 out_iput: 909 iput(inode); 910 out_err: 911 return ERR_PTR(err); 912 } 913 914 static int fuse_encode_fh(struct inode *inode, u32 *fh, int *max_len, 915 struct inode *parent) 916 { 917 int len = parent ? 6 : 3; 918 u64 nodeid; 919 u32 generation; 920 921 if (*max_len < len) { 922 *max_len = len; 923 return FILEID_INVALID; 924 } 925 926 nodeid = get_fuse_inode(inode)->nodeid; 927 generation = inode->i_generation; 928 929 fh[0] = (u32)(nodeid >> 32); 930 fh[1] = (u32)(nodeid & 0xffffffff); 931 fh[2] = generation; 932 933 if (parent) { 934 nodeid = get_fuse_inode(parent)->nodeid; 935 generation = parent->i_generation; 936 937 fh[3] = (u32)(nodeid >> 32); 938 fh[4] = (u32)(nodeid & 0xffffffff); 939 fh[5] = generation; 940 } 941 942 *max_len = len; 943 return parent ? 0x82 : 0x81; 944 } 945 946 static struct dentry *fuse_fh_to_dentry(struct super_block *sb, 947 struct fid *fid, int fh_len, int fh_type) 948 { 949 struct fuse_inode_handle handle; 950 951 if ((fh_type != 0x81 && fh_type != 0x82) || fh_len < 3) 952 return NULL; 953 954 handle.nodeid = (u64) fid->raw[0] << 32; 955 handle.nodeid |= (u64) fid->raw[1]; 956 handle.generation = fid->raw[2]; 957 return fuse_get_dentry(sb, &handle); 958 } 959 960 static struct dentry *fuse_fh_to_parent(struct super_block *sb, 961 struct fid *fid, int fh_len, int fh_type) 962 { 963 struct fuse_inode_handle parent; 964 965 if (fh_type != 0x82 || fh_len < 6) 966 return NULL; 967 968 parent.nodeid = (u64) fid->raw[3] << 32; 969 parent.nodeid |= (u64) fid->raw[4]; 970 parent.generation = fid->raw[5]; 971 return fuse_get_dentry(sb, &parent); 972 } 973 974 static struct dentry *fuse_get_parent(struct dentry *child) 975 { 976 struct inode *child_inode = d_inode(child); 977 struct fuse_conn *fc = get_fuse_conn(child_inode); 978 struct inode *inode; 979 struct dentry *parent; 980 struct fuse_entry_out outarg; 981 int err; 982 983 if (!fc->export_support) 984 return ERR_PTR(-ESTALE); 985 986 err = fuse_lookup_name(child_inode->i_sb, get_node_id(child_inode), 987 &dotdot_name, &outarg, &inode); 988 if (err) { 989 if (err == -ENOENT) 990 return ERR_PTR(-ESTALE); 991 return ERR_PTR(err); 992 } 993 994 parent = d_obtain_alias(inode); 995 if (!IS_ERR(parent) && get_node_id(inode) != FUSE_ROOT_ID) 996 fuse_invalidate_entry_cache(parent); 997 998 return parent; 999 } 1000 1001 static const struct export_operations fuse_export_operations = { 1002 .fh_to_dentry = fuse_fh_to_dentry, 1003 .fh_to_parent = fuse_fh_to_parent, 1004 .encode_fh = fuse_encode_fh, 1005 .get_parent = fuse_get_parent, 1006 }; 1007 1008 static const struct super_operations fuse_super_operations = { 1009 .alloc_inode = fuse_alloc_inode, 1010 .free_inode = fuse_free_inode, 1011 .evict_inode = fuse_evict_inode, 1012 .write_inode = fuse_write_inode, 1013 .drop_inode = generic_delete_inode, 1014 .umount_begin = fuse_umount_begin, 1015 .statfs = fuse_statfs, 1016 .sync_fs = fuse_sync_fs, 1017 .show_options = fuse_show_options, 1018 }; 1019 1020 static void sanitize_global_limit(unsigned *limit) 1021 { 1022 /* 1023 * The default maximum number of async requests is calculated to consume 1024 * 1/2^13 of the total memory, assuming 392 bytes per request. 1025 */ 1026 if (*limit == 0) 1027 *limit = ((totalram_pages() << PAGE_SHIFT) >> 13) / 392; 1028 1029 if (*limit >= 1 << 16) 1030 *limit = (1 << 16) - 1; 1031 } 1032 1033 static int set_global_limit(const char *val, const struct kernel_param *kp) 1034 { 1035 int rv; 1036 1037 rv = param_set_uint(val, kp); 1038 if (rv) 1039 return rv; 1040 1041 sanitize_global_limit((unsigned *)kp->arg); 1042 1043 return 0; 1044 } 1045 1046 static void process_init_limits(struct fuse_conn *fc, struct fuse_init_out *arg) 1047 { 1048 int cap_sys_admin = capable(CAP_SYS_ADMIN); 1049 1050 if (arg->minor < 13) 1051 return; 1052 1053 sanitize_global_limit(&max_user_bgreq); 1054 sanitize_global_limit(&max_user_congthresh); 1055 1056 spin_lock(&fc->bg_lock); 1057 if (arg->max_background) { 1058 fc->max_background = arg->max_background; 1059 1060 if (!cap_sys_admin && fc->max_background > max_user_bgreq) 1061 fc->max_background = max_user_bgreq; 1062 } 1063 if (arg->congestion_threshold) { 1064 fc->congestion_threshold = arg->congestion_threshold; 1065 1066 if (!cap_sys_admin && 1067 fc->congestion_threshold > max_user_congthresh) 1068 fc->congestion_threshold = max_user_congthresh; 1069 } 1070 spin_unlock(&fc->bg_lock); 1071 } 1072 1073 struct fuse_init_args { 1074 struct fuse_args args; 1075 struct fuse_init_in in; 1076 struct fuse_init_out out; 1077 }; 1078 1079 static void process_init_reply(struct fuse_mount *fm, struct fuse_args *args, 1080 int error) 1081 { 1082 struct fuse_conn *fc = fm->fc; 1083 struct fuse_init_args *ia = container_of(args, typeof(*ia), args); 1084 struct fuse_init_out *arg = &ia->out; 1085 bool ok = true; 1086 1087 if (error || arg->major != FUSE_KERNEL_VERSION) 1088 ok = false; 1089 else { 1090 unsigned long ra_pages; 1091 1092 process_init_limits(fc, arg); 1093 1094 if (arg->minor >= 6) { 1095 ra_pages = arg->max_readahead / PAGE_SIZE; 1096 if (arg->flags & FUSE_ASYNC_READ) 1097 fc->async_read = 1; 1098 if (!(arg->flags & FUSE_POSIX_LOCKS)) 1099 fc->no_lock = 1; 1100 if (arg->minor >= 17) { 1101 if (!(arg->flags & FUSE_FLOCK_LOCKS)) 1102 fc->no_flock = 1; 1103 } else { 1104 if (!(arg->flags & FUSE_POSIX_LOCKS)) 1105 fc->no_flock = 1; 1106 } 1107 if (arg->flags & FUSE_ATOMIC_O_TRUNC) 1108 fc->atomic_o_trunc = 1; 1109 if (arg->minor >= 9) { 1110 /* LOOKUP has dependency on proto version */ 1111 if (arg->flags & FUSE_EXPORT_SUPPORT) 1112 fc->export_support = 1; 1113 } 1114 if (arg->flags & FUSE_BIG_WRITES) 1115 fc->big_writes = 1; 1116 if (arg->flags & FUSE_DONT_MASK) 1117 fc->dont_mask = 1; 1118 if (arg->flags & FUSE_AUTO_INVAL_DATA) 1119 fc->auto_inval_data = 1; 1120 else if (arg->flags & FUSE_EXPLICIT_INVAL_DATA) 1121 fc->explicit_inval_data = 1; 1122 if (arg->flags & FUSE_DO_READDIRPLUS) { 1123 fc->do_readdirplus = 1; 1124 if (arg->flags & FUSE_READDIRPLUS_AUTO) 1125 fc->readdirplus_auto = 1; 1126 } 1127 if (arg->flags & FUSE_ASYNC_DIO) 1128 fc->async_dio = 1; 1129 if (arg->flags & FUSE_WRITEBACK_CACHE) 1130 fc->writeback_cache = 1; 1131 if (arg->flags & FUSE_PARALLEL_DIROPS) 1132 fc->parallel_dirops = 1; 1133 if (arg->flags & FUSE_HANDLE_KILLPRIV) 1134 fc->handle_killpriv = 1; 1135 if (arg->time_gran && arg->time_gran <= 1000000000) 1136 fm->sb->s_time_gran = arg->time_gran; 1137 if ((arg->flags & FUSE_POSIX_ACL)) { 1138 fc->default_permissions = 1; 1139 fc->posix_acl = 1; 1140 fm->sb->s_xattr = fuse_acl_xattr_handlers; 1141 } 1142 if (arg->flags & FUSE_CACHE_SYMLINKS) 1143 fc->cache_symlinks = 1; 1144 if (arg->flags & FUSE_ABORT_ERROR) 1145 fc->abort_err = 1; 1146 if (arg->flags & FUSE_MAX_PAGES) { 1147 fc->max_pages = 1148 min_t(unsigned int, fc->max_pages_limit, 1149 max_t(unsigned int, arg->max_pages, 1)); 1150 } 1151 if (IS_ENABLED(CONFIG_FUSE_DAX) && 1152 arg->flags & FUSE_MAP_ALIGNMENT && 1153 !fuse_dax_check_alignment(fc, arg->map_alignment)) { 1154 ok = false; 1155 } 1156 if (arg->flags & FUSE_HANDLE_KILLPRIV_V2) { 1157 fc->handle_killpriv_v2 = 1; 1158 fm->sb->s_flags |= SB_NOSEC; 1159 } 1160 if (arg->flags & FUSE_SETXATTR_EXT) 1161 fc->setxattr_ext = 1; 1162 } else { 1163 ra_pages = fc->max_read / PAGE_SIZE; 1164 fc->no_lock = 1; 1165 fc->no_flock = 1; 1166 } 1167 1168 fm->sb->s_bdi->ra_pages = 1169 min(fm->sb->s_bdi->ra_pages, ra_pages); 1170 fc->minor = arg->minor; 1171 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write; 1172 fc->max_write = max_t(unsigned, 4096, fc->max_write); 1173 fc->conn_init = 1; 1174 } 1175 kfree(ia); 1176 1177 if (!ok) { 1178 fc->conn_init = 0; 1179 fc->conn_error = 1; 1180 } 1181 1182 fuse_set_initialized(fc); 1183 wake_up_all(&fc->blocked_waitq); 1184 } 1185 1186 void fuse_send_init(struct fuse_mount *fm) 1187 { 1188 struct fuse_init_args *ia; 1189 1190 ia = kzalloc(sizeof(*ia), GFP_KERNEL | __GFP_NOFAIL); 1191 1192 ia->in.major = FUSE_KERNEL_VERSION; 1193 ia->in.minor = FUSE_KERNEL_MINOR_VERSION; 1194 ia->in.max_readahead = fm->sb->s_bdi->ra_pages * PAGE_SIZE; 1195 ia->in.flags |= 1196 FUSE_ASYNC_READ | FUSE_POSIX_LOCKS | FUSE_ATOMIC_O_TRUNC | 1197 FUSE_EXPORT_SUPPORT | FUSE_BIG_WRITES | FUSE_DONT_MASK | 1198 FUSE_SPLICE_WRITE | FUSE_SPLICE_MOVE | FUSE_SPLICE_READ | 1199 FUSE_FLOCK_LOCKS | FUSE_HAS_IOCTL_DIR | FUSE_AUTO_INVAL_DATA | 1200 FUSE_DO_READDIRPLUS | FUSE_READDIRPLUS_AUTO | FUSE_ASYNC_DIO | 1201 FUSE_WRITEBACK_CACHE | FUSE_NO_OPEN_SUPPORT | 1202 FUSE_PARALLEL_DIROPS | FUSE_HANDLE_KILLPRIV | FUSE_POSIX_ACL | 1203 FUSE_ABORT_ERROR | FUSE_MAX_PAGES | FUSE_CACHE_SYMLINKS | 1204 FUSE_NO_OPENDIR_SUPPORT | FUSE_EXPLICIT_INVAL_DATA | 1205 FUSE_HANDLE_KILLPRIV_V2 | FUSE_SETXATTR_EXT; 1206 #ifdef CONFIG_FUSE_DAX 1207 if (fm->fc->dax) 1208 ia->in.flags |= FUSE_MAP_ALIGNMENT; 1209 #endif 1210 if (fm->fc->auto_submounts) 1211 ia->in.flags |= FUSE_SUBMOUNTS; 1212 1213 ia->args.opcode = FUSE_INIT; 1214 ia->args.in_numargs = 1; 1215 ia->args.in_args[0].size = sizeof(ia->in); 1216 ia->args.in_args[0].value = &ia->in; 1217 ia->args.out_numargs = 1; 1218 /* Variable length argument used for backward compatibility 1219 with interface version < 7.5. Rest of init_out is zeroed 1220 by do_get_request(), so a short reply is not a problem */ 1221 ia->args.out_argvar = true; 1222 ia->args.out_args[0].size = sizeof(ia->out); 1223 ia->args.out_args[0].value = &ia->out; 1224 ia->args.force = true; 1225 ia->args.nocreds = true; 1226 ia->args.end = process_init_reply; 1227 1228 if (fuse_simple_background(fm, &ia->args, GFP_KERNEL) != 0) 1229 process_init_reply(fm, &ia->args, -ENOTCONN); 1230 } 1231 EXPORT_SYMBOL_GPL(fuse_send_init); 1232 1233 void fuse_free_conn(struct fuse_conn *fc) 1234 { 1235 WARN_ON(!list_empty(&fc->devices)); 1236 kfree_rcu(fc, rcu); 1237 } 1238 EXPORT_SYMBOL_GPL(fuse_free_conn); 1239 1240 static int fuse_bdi_init(struct fuse_conn *fc, struct super_block *sb) 1241 { 1242 int err; 1243 char *suffix = ""; 1244 1245 if (sb->s_bdev) { 1246 suffix = "-fuseblk"; 1247 /* 1248 * sb->s_bdi points to blkdev's bdi however we want to redirect 1249 * it to our private bdi... 1250 */ 1251 bdi_put(sb->s_bdi); 1252 sb->s_bdi = &noop_backing_dev_info; 1253 } 1254 err = super_setup_bdi_name(sb, "%u:%u%s", MAJOR(fc->dev), 1255 MINOR(fc->dev), suffix); 1256 if (err) 1257 return err; 1258 1259 /* fuse does it's own writeback accounting */ 1260 sb->s_bdi->capabilities &= ~BDI_CAP_WRITEBACK_ACCT; 1261 sb->s_bdi->capabilities |= BDI_CAP_STRICTLIMIT; 1262 1263 /* 1264 * For a single fuse filesystem use max 1% of dirty + 1265 * writeback threshold. 1266 * 1267 * This gives about 1M of write buffer for memory maps on a 1268 * machine with 1G and 10% dirty_ratio, which should be more 1269 * than enough. 1270 * 1271 * Privileged users can raise it by writing to 1272 * 1273 * /sys/class/bdi/<bdi>/max_ratio 1274 */ 1275 bdi_set_max_ratio(sb->s_bdi, 1); 1276 1277 return 0; 1278 } 1279 1280 struct fuse_dev *fuse_dev_alloc(void) 1281 { 1282 struct fuse_dev *fud; 1283 struct list_head *pq; 1284 1285 fud = kzalloc(sizeof(struct fuse_dev), GFP_KERNEL); 1286 if (!fud) 1287 return NULL; 1288 1289 pq = kcalloc(FUSE_PQ_HASH_SIZE, sizeof(struct list_head), GFP_KERNEL); 1290 if (!pq) { 1291 kfree(fud); 1292 return NULL; 1293 } 1294 1295 fud->pq.processing = pq; 1296 fuse_pqueue_init(&fud->pq); 1297 1298 return fud; 1299 } 1300 EXPORT_SYMBOL_GPL(fuse_dev_alloc); 1301 1302 void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc) 1303 { 1304 fud->fc = fuse_conn_get(fc); 1305 spin_lock(&fc->lock); 1306 list_add_tail(&fud->entry, &fc->devices); 1307 spin_unlock(&fc->lock); 1308 } 1309 EXPORT_SYMBOL_GPL(fuse_dev_install); 1310 1311 struct fuse_dev *fuse_dev_alloc_install(struct fuse_conn *fc) 1312 { 1313 struct fuse_dev *fud; 1314 1315 fud = fuse_dev_alloc(); 1316 if (!fud) 1317 return NULL; 1318 1319 fuse_dev_install(fud, fc); 1320 return fud; 1321 } 1322 EXPORT_SYMBOL_GPL(fuse_dev_alloc_install); 1323 1324 void fuse_dev_free(struct fuse_dev *fud) 1325 { 1326 struct fuse_conn *fc = fud->fc; 1327 1328 if (fc) { 1329 spin_lock(&fc->lock); 1330 list_del(&fud->entry); 1331 spin_unlock(&fc->lock); 1332 1333 fuse_conn_put(fc); 1334 } 1335 kfree(fud->pq.processing); 1336 kfree(fud); 1337 } 1338 EXPORT_SYMBOL_GPL(fuse_dev_free); 1339 1340 static void fuse_fill_attr_from_inode(struct fuse_attr *attr, 1341 const struct fuse_inode *fi) 1342 { 1343 *attr = (struct fuse_attr){ 1344 .ino = fi->inode.i_ino, 1345 .size = fi->inode.i_size, 1346 .blocks = fi->inode.i_blocks, 1347 .atime = fi->inode.i_atime.tv_sec, 1348 .mtime = fi->inode.i_mtime.tv_sec, 1349 .ctime = fi->inode.i_ctime.tv_sec, 1350 .atimensec = fi->inode.i_atime.tv_nsec, 1351 .mtimensec = fi->inode.i_mtime.tv_nsec, 1352 .ctimensec = fi->inode.i_ctime.tv_nsec, 1353 .mode = fi->inode.i_mode, 1354 .nlink = fi->inode.i_nlink, 1355 .uid = fi->inode.i_uid.val, 1356 .gid = fi->inode.i_gid.val, 1357 .rdev = fi->inode.i_rdev, 1358 .blksize = 1u << fi->inode.i_blkbits, 1359 }; 1360 } 1361 1362 static void fuse_sb_defaults(struct super_block *sb) 1363 { 1364 sb->s_magic = FUSE_SUPER_MAGIC; 1365 sb->s_op = &fuse_super_operations; 1366 sb->s_xattr = fuse_xattr_handlers; 1367 sb->s_maxbytes = MAX_LFS_FILESIZE; 1368 sb->s_time_gran = 1; 1369 sb->s_export_op = &fuse_export_operations; 1370 sb->s_iflags |= SB_I_IMA_UNVERIFIABLE_SIGNATURE; 1371 if (sb->s_user_ns != &init_user_ns) 1372 sb->s_iflags |= SB_I_UNTRUSTED_MOUNTER; 1373 sb->s_flags &= ~(SB_NOSEC | SB_I_VERSION); 1374 1375 /* 1376 * If we are not in the initial user namespace posix 1377 * acls must be translated. 1378 */ 1379 if (sb->s_user_ns != &init_user_ns) 1380 sb->s_xattr = fuse_no_acl_xattr_handlers; 1381 } 1382 1383 static int fuse_fill_super_submount(struct super_block *sb, 1384 struct fuse_inode *parent_fi) 1385 { 1386 struct fuse_mount *fm = get_fuse_mount_super(sb); 1387 struct super_block *parent_sb = parent_fi->inode.i_sb; 1388 struct fuse_attr root_attr; 1389 struct inode *root; 1390 1391 fuse_sb_defaults(sb); 1392 fm->sb = sb; 1393 1394 WARN_ON(sb->s_bdi != &noop_backing_dev_info); 1395 sb->s_bdi = bdi_get(parent_sb->s_bdi); 1396 1397 sb->s_xattr = parent_sb->s_xattr; 1398 sb->s_time_gran = parent_sb->s_time_gran; 1399 sb->s_blocksize = parent_sb->s_blocksize; 1400 sb->s_blocksize_bits = parent_sb->s_blocksize_bits; 1401 sb->s_subtype = kstrdup(parent_sb->s_subtype, GFP_KERNEL); 1402 if (parent_sb->s_subtype && !sb->s_subtype) 1403 return -ENOMEM; 1404 1405 fuse_fill_attr_from_inode(&root_attr, parent_fi); 1406 root = fuse_iget(sb, parent_fi->nodeid, 0, &root_attr, 0, 0); 1407 /* 1408 * This inode is just a duplicate, so it is not looked up and 1409 * its nlookup should not be incremented. fuse_iget() does 1410 * that, though, so undo it here. 1411 */ 1412 get_fuse_inode(root)->nlookup--; 1413 sb->s_d_op = &fuse_dentry_operations; 1414 sb->s_root = d_make_root(root); 1415 if (!sb->s_root) 1416 return -ENOMEM; 1417 1418 return 0; 1419 } 1420 1421 /* Filesystem context private data holds the FUSE inode of the mount point */ 1422 static int fuse_get_tree_submount(struct fs_context *fsc) 1423 { 1424 struct fuse_mount *fm; 1425 struct fuse_inode *mp_fi = fsc->fs_private; 1426 struct fuse_conn *fc = get_fuse_conn(&mp_fi->inode); 1427 struct super_block *sb; 1428 int err; 1429 1430 fm = kzalloc(sizeof(struct fuse_mount), GFP_KERNEL); 1431 if (!fm) 1432 return -ENOMEM; 1433 1434 fm->fc = fuse_conn_get(fc); 1435 fsc->s_fs_info = fm; 1436 sb = sget_fc(fsc, NULL, set_anon_super_fc); 1437 if (fsc->s_fs_info) 1438 fuse_mount_destroy(fm); 1439 if (IS_ERR(sb)) 1440 return PTR_ERR(sb); 1441 1442 /* Initialize superblock, making @mp_fi its root */ 1443 err = fuse_fill_super_submount(sb, mp_fi); 1444 if (err) { 1445 deactivate_locked_super(sb); 1446 return err; 1447 } 1448 1449 down_write(&fc->killsb); 1450 list_add_tail(&fm->fc_entry, &fc->mounts); 1451 up_write(&fc->killsb); 1452 1453 sb->s_flags |= SB_ACTIVE; 1454 fsc->root = dget(sb->s_root); 1455 1456 return 0; 1457 } 1458 1459 static const struct fs_context_operations fuse_context_submount_ops = { 1460 .get_tree = fuse_get_tree_submount, 1461 }; 1462 1463 int fuse_init_fs_context_submount(struct fs_context *fsc) 1464 { 1465 fsc->ops = &fuse_context_submount_ops; 1466 return 0; 1467 } 1468 EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount); 1469 1470 int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx) 1471 { 1472 struct fuse_dev *fud = NULL; 1473 struct fuse_mount *fm = get_fuse_mount_super(sb); 1474 struct fuse_conn *fc = fm->fc; 1475 struct inode *root; 1476 struct dentry *root_dentry; 1477 int err; 1478 1479 err = -EINVAL; 1480 if (sb->s_flags & SB_MANDLOCK) 1481 goto err; 1482 1483 rcu_assign_pointer(fc->curr_bucket, fuse_sync_bucket_alloc()); 1484 fuse_sb_defaults(sb); 1485 1486 if (ctx->is_bdev) { 1487 #ifdef CONFIG_BLOCK 1488 err = -EINVAL; 1489 if (!sb_set_blocksize(sb, ctx->blksize)) 1490 goto err; 1491 #endif 1492 } else { 1493 sb->s_blocksize = PAGE_SIZE; 1494 sb->s_blocksize_bits = PAGE_SHIFT; 1495 } 1496 1497 sb->s_subtype = ctx->subtype; 1498 ctx->subtype = NULL; 1499 if (IS_ENABLED(CONFIG_FUSE_DAX)) { 1500 err = fuse_dax_conn_alloc(fc, ctx->dax_dev); 1501 if (err) 1502 goto err; 1503 } 1504 1505 if (ctx->fudptr) { 1506 err = -ENOMEM; 1507 fud = fuse_dev_alloc_install(fc); 1508 if (!fud) 1509 goto err_free_dax; 1510 } 1511 1512 fc->dev = sb->s_dev; 1513 fm->sb = sb; 1514 err = fuse_bdi_init(fc, sb); 1515 if (err) 1516 goto err_dev_free; 1517 1518 /* Handle umasking inside the fuse code */ 1519 if (sb->s_flags & SB_POSIXACL) 1520 fc->dont_mask = 1; 1521 sb->s_flags |= SB_POSIXACL; 1522 1523 fc->default_permissions = ctx->default_permissions; 1524 fc->allow_other = ctx->allow_other; 1525 fc->user_id = ctx->user_id; 1526 fc->group_id = ctx->group_id; 1527 fc->legacy_opts_show = ctx->legacy_opts_show; 1528 fc->max_read = max_t(unsigned int, 4096, ctx->max_read); 1529 fc->destroy = ctx->destroy; 1530 fc->no_control = ctx->no_control; 1531 fc->no_force_umount = ctx->no_force_umount; 1532 1533 err = -ENOMEM; 1534 root = fuse_get_root_inode(sb, ctx->rootmode); 1535 sb->s_d_op = &fuse_root_dentry_operations; 1536 root_dentry = d_make_root(root); 1537 if (!root_dentry) 1538 goto err_dev_free; 1539 /* Root dentry doesn't have .d_revalidate */ 1540 sb->s_d_op = &fuse_dentry_operations; 1541 1542 mutex_lock(&fuse_mutex); 1543 err = -EINVAL; 1544 if (ctx->fudptr && *ctx->fudptr) 1545 goto err_unlock; 1546 1547 err = fuse_ctl_add_conn(fc); 1548 if (err) 1549 goto err_unlock; 1550 1551 list_add_tail(&fc->entry, &fuse_conn_list); 1552 sb->s_root = root_dentry; 1553 if (ctx->fudptr) 1554 *ctx->fudptr = fud; 1555 mutex_unlock(&fuse_mutex); 1556 return 0; 1557 1558 err_unlock: 1559 mutex_unlock(&fuse_mutex); 1560 dput(root_dentry); 1561 err_dev_free: 1562 if (fud) 1563 fuse_dev_free(fud); 1564 err_free_dax: 1565 if (IS_ENABLED(CONFIG_FUSE_DAX)) 1566 fuse_dax_conn_free(fc); 1567 err: 1568 return err; 1569 } 1570 EXPORT_SYMBOL_GPL(fuse_fill_super_common); 1571 1572 static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc) 1573 { 1574 struct fuse_fs_context *ctx = fsc->fs_private; 1575 int err; 1576 1577 if (!ctx->file || !ctx->rootmode_present || 1578 !ctx->user_id_present || !ctx->group_id_present) 1579 return -EINVAL; 1580 1581 /* 1582 * Require mount to happen from the same user namespace which 1583 * opened /dev/fuse to prevent potential attacks. 1584 */ 1585 if ((ctx->file->f_op != &fuse_dev_operations) || 1586 (ctx->file->f_cred->user_ns != sb->s_user_ns)) 1587 return -EINVAL; 1588 ctx->fudptr = &ctx->file->private_data; 1589 1590 err = fuse_fill_super_common(sb, ctx); 1591 if (err) 1592 return err; 1593 /* file->private_data shall be visible on all CPUs after this */ 1594 smp_mb(); 1595 fuse_send_init(get_fuse_mount_super(sb)); 1596 return 0; 1597 } 1598 1599 /* 1600 * This is the path where user supplied an already initialized fuse dev. In 1601 * this case never create a new super if the old one is gone. 1602 */ 1603 static int fuse_set_no_super(struct super_block *sb, struct fs_context *fsc) 1604 { 1605 return -ENOTCONN; 1606 } 1607 1608 static int fuse_test_super(struct super_block *sb, struct fs_context *fsc) 1609 { 1610 1611 return fsc->sget_key == get_fuse_conn_super(sb); 1612 } 1613 1614 static int fuse_get_tree(struct fs_context *fsc) 1615 { 1616 struct fuse_fs_context *ctx = fsc->fs_private; 1617 struct fuse_dev *fud; 1618 struct fuse_conn *fc; 1619 struct fuse_mount *fm; 1620 struct super_block *sb; 1621 int err; 1622 1623 fc = kmalloc(sizeof(*fc), GFP_KERNEL); 1624 if (!fc) 1625 return -ENOMEM; 1626 1627 fm = kzalloc(sizeof(*fm), GFP_KERNEL); 1628 if (!fm) { 1629 kfree(fc); 1630 return -ENOMEM; 1631 } 1632 1633 fuse_conn_init(fc, fm, fsc->user_ns, &fuse_dev_fiq_ops, NULL); 1634 fc->release = fuse_free_conn; 1635 1636 fsc->s_fs_info = fm; 1637 1638 if (ctx->fd_present) 1639 ctx->file = fget(ctx->fd); 1640 1641 if (IS_ENABLED(CONFIG_BLOCK) && ctx->is_bdev) { 1642 err = get_tree_bdev(fsc, fuse_fill_super); 1643 goto out; 1644 } 1645 /* 1646 * While block dev mount can be initialized with a dummy device fd 1647 * (found by device name), normal fuse mounts can't 1648 */ 1649 err = -EINVAL; 1650 if (!ctx->file) 1651 goto out; 1652 1653 /* 1654 * Allow creating a fuse mount with an already initialized fuse 1655 * connection 1656 */ 1657 fud = READ_ONCE(ctx->file->private_data); 1658 if (ctx->file->f_op == &fuse_dev_operations && fud) { 1659 fsc->sget_key = fud->fc; 1660 sb = sget_fc(fsc, fuse_test_super, fuse_set_no_super); 1661 err = PTR_ERR_OR_ZERO(sb); 1662 if (!IS_ERR(sb)) 1663 fsc->root = dget(sb->s_root); 1664 } else { 1665 err = get_tree_nodev(fsc, fuse_fill_super); 1666 } 1667 out: 1668 if (fsc->s_fs_info) 1669 fuse_mount_destroy(fm); 1670 if (ctx->file) 1671 fput(ctx->file); 1672 return err; 1673 } 1674 1675 static const struct fs_context_operations fuse_context_ops = { 1676 .free = fuse_free_fsc, 1677 .parse_param = fuse_parse_param, 1678 .reconfigure = fuse_reconfigure, 1679 .get_tree = fuse_get_tree, 1680 }; 1681 1682 /* 1683 * Set up the filesystem mount context. 1684 */ 1685 static int fuse_init_fs_context(struct fs_context *fsc) 1686 { 1687 struct fuse_fs_context *ctx; 1688 1689 ctx = kzalloc(sizeof(struct fuse_fs_context), GFP_KERNEL); 1690 if (!ctx) 1691 return -ENOMEM; 1692 1693 ctx->max_read = ~0; 1694 ctx->blksize = FUSE_DEFAULT_BLKSIZE; 1695 ctx->legacy_opts_show = true; 1696 1697 #ifdef CONFIG_BLOCK 1698 if (fsc->fs_type == &fuseblk_fs_type) { 1699 ctx->is_bdev = true; 1700 ctx->destroy = true; 1701 } 1702 #endif 1703 1704 fsc->fs_private = ctx; 1705 fsc->ops = &fuse_context_ops; 1706 return 0; 1707 } 1708 1709 bool fuse_mount_remove(struct fuse_mount *fm) 1710 { 1711 struct fuse_conn *fc = fm->fc; 1712 bool last = false; 1713 1714 down_write(&fc->killsb); 1715 list_del_init(&fm->fc_entry); 1716 if (list_empty(&fc->mounts)) 1717 last = true; 1718 up_write(&fc->killsb); 1719 1720 return last; 1721 } 1722 EXPORT_SYMBOL_GPL(fuse_mount_remove); 1723 1724 void fuse_conn_destroy(struct fuse_mount *fm) 1725 { 1726 struct fuse_conn *fc = fm->fc; 1727 1728 if (fc->destroy) 1729 fuse_send_destroy(fm); 1730 1731 fuse_abort_conn(fc); 1732 fuse_wait_aborted(fc); 1733 1734 if (!list_empty(&fc->entry)) { 1735 mutex_lock(&fuse_mutex); 1736 list_del(&fc->entry); 1737 fuse_ctl_remove_conn(fc); 1738 mutex_unlock(&fuse_mutex); 1739 } 1740 } 1741 EXPORT_SYMBOL_GPL(fuse_conn_destroy); 1742 1743 static void fuse_sb_destroy(struct super_block *sb) 1744 { 1745 struct fuse_mount *fm = get_fuse_mount_super(sb); 1746 bool last; 1747 1748 if (sb->s_root) { 1749 last = fuse_mount_remove(fm); 1750 if (last) 1751 fuse_conn_destroy(fm); 1752 } 1753 } 1754 1755 void fuse_mount_destroy(struct fuse_mount *fm) 1756 { 1757 fuse_conn_put(fm->fc); 1758 kfree(fm); 1759 } 1760 EXPORT_SYMBOL(fuse_mount_destroy); 1761 1762 static void fuse_kill_sb_anon(struct super_block *sb) 1763 { 1764 fuse_sb_destroy(sb); 1765 kill_anon_super(sb); 1766 fuse_mount_destroy(get_fuse_mount_super(sb)); 1767 } 1768 1769 static struct file_system_type fuse_fs_type = { 1770 .owner = THIS_MODULE, 1771 .name = "fuse", 1772 .fs_flags = FS_HAS_SUBTYPE | FS_USERNS_MOUNT, 1773 .init_fs_context = fuse_init_fs_context, 1774 .parameters = fuse_fs_parameters, 1775 .kill_sb = fuse_kill_sb_anon, 1776 }; 1777 MODULE_ALIAS_FS("fuse"); 1778 1779 #ifdef CONFIG_BLOCK 1780 static void fuse_kill_sb_blk(struct super_block *sb) 1781 { 1782 fuse_sb_destroy(sb); 1783 kill_block_super(sb); 1784 fuse_mount_destroy(get_fuse_mount_super(sb)); 1785 } 1786 1787 static struct file_system_type fuseblk_fs_type = { 1788 .owner = THIS_MODULE, 1789 .name = "fuseblk", 1790 .init_fs_context = fuse_init_fs_context, 1791 .parameters = fuse_fs_parameters, 1792 .kill_sb = fuse_kill_sb_blk, 1793 .fs_flags = FS_REQUIRES_DEV | FS_HAS_SUBTYPE, 1794 }; 1795 MODULE_ALIAS_FS("fuseblk"); 1796 1797 static inline int register_fuseblk(void) 1798 { 1799 return register_filesystem(&fuseblk_fs_type); 1800 } 1801 1802 static inline void unregister_fuseblk(void) 1803 { 1804 unregister_filesystem(&fuseblk_fs_type); 1805 } 1806 #else 1807 static inline int register_fuseblk(void) 1808 { 1809 return 0; 1810 } 1811 1812 static inline void unregister_fuseblk(void) 1813 { 1814 } 1815 #endif 1816 1817 static void fuse_inode_init_once(void *foo) 1818 { 1819 struct inode *inode = foo; 1820 1821 inode_init_once(inode); 1822 } 1823 1824 static int __init fuse_fs_init(void) 1825 { 1826 int err; 1827 1828 fuse_inode_cachep = kmem_cache_create("fuse_inode", 1829 sizeof(struct fuse_inode), 0, 1830 SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT|SLAB_RECLAIM_ACCOUNT, 1831 fuse_inode_init_once); 1832 err = -ENOMEM; 1833 if (!fuse_inode_cachep) 1834 goto out; 1835 1836 err = register_fuseblk(); 1837 if (err) 1838 goto out2; 1839 1840 err = register_filesystem(&fuse_fs_type); 1841 if (err) 1842 goto out3; 1843 1844 return 0; 1845 1846 out3: 1847 unregister_fuseblk(); 1848 out2: 1849 kmem_cache_destroy(fuse_inode_cachep); 1850 out: 1851 return err; 1852 } 1853 1854 static void fuse_fs_cleanup(void) 1855 { 1856 unregister_filesystem(&fuse_fs_type); 1857 unregister_fuseblk(); 1858 1859 /* 1860 * Make sure all delayed rcu free inodes are flushed before we 1861 * destroy cache. 1862 */ 1863 rcu_barrier(); 1864 kmem_cache_destroy(fuse_inode_cachep); 1865 } 1866 1867 static struct kobject *fuse_kobj; 1868 1869 static int fuse_sysfs_init(void) 1870 { 1871 int err; 1872 1873 fuse_kobj = kobject_create_and_add("fuse", fs_kobj); 1874 if (!fuse_kobj) { 1875 err = -ENOMEM; 1876 goto out_err; 1877 } 1878 1879 err = sysfs_create_mount_point(fuse_kobj, "connections"); 1880 if (err) 1881 goto out_fuse_unregister; 1882 1883 return 0; 1884 1885 out_fuse_unregister: 1886 kobject_put(fuse_kobj); 1887 out_err: 1888 return err; 1889 } 1890 1891 static void fuse_sysfs_cleanup(void) 1892 { 1893 sysfs_remove_mount_point(fuse_kobj, "connections"); 1894 kobject_put(fuse_kobj); 1895 } 1896 1897 static int __init fuse_init(void) 1898 { 1899 int res; 1900 1901 pr_info("init (API version %i.%i)\n", 1902 FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); 1903 1904 INIT_LIST_HEAD(&fuse_conn_list); 1905 res = fuse_fs_init(); 1906 if (res) 1907 goto err; 1908 1909 res = fuse_dev_init(); 1910 if (res) 1911 goto err_fs_cleanup; 1912 1913 res = fuse_sysfs_init(); 1914 if (res) 1915 goto err_dev_cleanup; 1916 1917 res = fuse_ctl_init(); 1918 if (res) 1919 goto err_sysfs_cleanup; 1920 1921 sanitize_global_limit(&max_user_bgreq); 1922 sanitize_global_limit(&max_user_congthresh); 1923 1924 return 0; 1925 1926 err_sysfs_cleanup: 1927 fuse_sysfs_cleanup(); 1928 err_dev_cleanup: 1929 fuse_dev_cleanup(); 1930 err_fs_cleanup: 1931 fuse_fs_cleanup(); 1932 err: 1933 return res; 1934 } 1935 1936 static void __exit fuse_exit(void) 1937 { 1938 pr_debug("exit\n"); 1939 1940 fuse_ctl_cleanup(); 1941 fuse_sysfs_cleanup(); 1942 fuse_fs_cleanup(); 1943 fuse_dev_cleanup(); 1944 } 1945 1946 module_init(fuse_init); 1947 module_exit(fuse_exit); 1948