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