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