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