1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/fs/super.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * 7 * super.c contains code to handle: - mount structures 8 * - super-block tables 9 * - filesystem drivers list 10 * - mount system call 11 * - umount system call 12 * - ustat system call 13 * 14 * GK 2/5/95 - Changed to support mounting the root fs via NFS 15 * 16 * Added kerneld support: Jacques Gelinas and Bjorn Ekwall 17 * Added change_root: Werner Almesberger & Hans Lermen, Feb '96 18 * Added options to /proc/mounts: 19 * Torbjörn Lindh (torbjorn.lindh@gopta.se), April 14, 1996. 20 * Added devfs support: Richard Gooch <rgooch@atnf.csiro.au>, 13-JAN-1998 21 * Heavily rewritten for 'one fs - one tree' dcache architecture. AV, Mar 2000 22 */ 23 24 #include <linux/export.h> 25 #include <linux/slab.h> 26 #include <linux/blkdev.h> 27 #include <linux/memcontrol.h> 28 #include <linux/rhashtable.h> 29 #include <linux/mount.h> 30 #include <linux/security.h> 31 #include <linux/writeback.h> /* for the emergency remount stuff */ 32 #include <linux/idr.h> 33 #include <linux/mutex.h> 34 #include <linux/backing-dev.h> 35 #include <linux/rculist_bl.h> 36 #include <linux/fscrypt.h> 37 #include <linux/fsnotify.h> 38 #include <linux/lockdep.h> 39 #include <linux/user_namespace.h> 40 #include <linux/fs_context.h> 41 #include <linux/fserror.h> 42 #include <uapi/linux/mount.h> 43 #include "internal.h" 44 45 static int thaw_super_locked(struct super_block *sb, enum freeze_holder who, 46 const void *freeze_owner); 47 48 static LIST_HEAD(super_blocks); 49 static DEFINE_SPINLOCK(sb_lock); 50 51 static char *sb_writers_name[SB_FREEZE_LEVELS] = { 52 "sb_writers", 53 "sb_pagefaults", 54 "sb_internal", 55 }; 56 57 static inline void __super_lock(struct super_block *sb, bool excl) 58 { 59 if (excl) 60 down_write(&sb->s_umount); 61 else 62 down_read(&sb->s_umount); 63 } 64 65 static inline void super_unlock(struct super_block *sb, bool excl) 66 { 67 if (excl) 68 up_write(&sb->s_umount); 69 else 70 up_read(&sb->s_umount); 71 } 72 73 static inline void __super_lock_excl(struct super_block *sb) 74 { 75 __super_lock(sb, true); 76 } 77 78 static inline void super_unlock_excl(struct super_block *sb) 79 { 80 super_unlock(sb, true); 81 } 82 83 static inline void super_unlock_shared(struct super_block *sb) 84 { 85 super_unlock(sb, false); 86 } 87 88 static bool super_flags(const struct super_block *sb, unsigned int flags) 89 { 90 /* 91 * Pairs with smp_store_release() in super_wake() and ensures 92 * that we see @flags after we're woken. 93 */ 94 return smp_load_acquire(&sb->s_flags) & flags; 95 } 96 97 /** 98 * super_lock - wait for superblock to become ready and lock it 99 * @sb: superblock to wait for 100 * @excl: whether exclusive access is required 101 * 102 * If the superblock has neither passed through vfs_get_tree() or 103 * generic_shutdown_super() yet wait for it to happen. Either superblock 104 * creation will succeed and SB_BORN is set by vfs_get_tree() or we're 105 * woken and we'll see SB_DYING. 106 * 107 * The caller must have acquired a temporary reference on @sb->s_passive. 108 * 109 * Return: The function returns true if SB_BORN was set and with 110 * s_umount held. The function returns false if SB_DYING was 111 * set and without s_umount held. 112 */ 113 static __must_check bool super_lock(struct super_block *sb, bool excl) 114 { 115 lockdep_assert_not_held(&sb->s_umount); 116 117 /* wait until the superblock is ready or dying */ 118 wait_var_event(&sb->s_flags, super_flags(sb, SB_BORN | SB_DYING)); 119 120 /* Don't pointlessly acquire s_umount. */ 121 if (super_flags(sb, SB_DYING)) 122 return false; 123 124 __super_lock(sb, excl); 125 126 /* 127 * Has gone through generic_shutdown_super() in the meantime. 128 * @sb->s_root is NULL and @sb->s_active is 0. No one needs to 129 * grab a reference to this. Tell them so. 130 */ 131 if (sb->s_flags & SB_DYING) { 132 super_unlock(sb, excl); 133 return false; 134 } 135 136 WARN_ON_ONCE(!(sb->s_flags & SB_BORN)); 137 return true; 138 } 139 140 /* wait and try to acquire read-side of @sb->s_umount */ 141 static inline bool super_lock_shared(struct super_block *sb) 142 { 143 return super_lock(sb, false); 144 } 145 146 /* wait and try to acquire write-side of @sb->s_umount */ 147 static inline bool super_lock_excl(struct super_block *sb) 148 { 149 return super_lock(sb, true); 150 } 151 152 /* wake waiters */ 153 #define SUPER_WAKE_FLAGS (SB_BORN | SB_DYING | SB_DEAD) 154 static void super_wake(struct super_block *sb, unsigned int flag) 155 { 156 WARN_ON_ONCE((flag & ~SUPER_WAKE_FLAGS)); 157 WARN_ON_ONCE(hweight32(flag & SUPER_WAKE_FLAGS) > 1); 158 159 /* 160 * Pairs with smp_load_acquire() in super_lock() to make sure 161 * all initializations in the superblock are seen by the user 162 * seeing SB_BORN sent. 163 */ 164 smp_store_release(&sb->s_flags, sb->s_flags | flag); 165 /* 166 * Pairs with the barrier in prepare_to_wait_event() to make sure 167 * ___wait_var_event() either sees SB_BORN set or 168 * waitqueue_active() check in wake_up_var() sees the waiter. 169 */ 170 smp_mb(); 171 wake_up_var(&sb->s_flags); 172 } 173 174 /* 175 * One thing we have to be careful of with a per-sb shrinker is that we don't 176 * drop the last active reference to the superblock from within the shrinker. 177 * If that happens we could trigger unregistering the shrinker from within the 178 * shrinker path and that leads to deadlock on the shrinker_mutex. Hence we 179 * take a passive reference to the superblock to avoid this from occurring. 180 */ 181 static unsigned long super_cache_scan(struct shrinker *shrink, 182 struct shrink_control *sc) 183 { 184 struct super_block *sb; 185 long fs_objects = 0; 186 long total_objects; 187 long freed = 0; 188 long dentries; 189 long inodes; 190 191 sb = shrink->private_data; 192 193 /* 194 * Deadlock avoidance. We may hold various FS locks, and we don't want 195 * to recurse into the FS that called us in clear_inode() and friends.. 196 */ 197 if (!(sc->gfp_mask & __GFP_FS)) 198 return SHRINK_STOP; 199 200 if (!super_trylock_shared(sb)) 201 return SHRINK_STOP; 202 203 if (sb->s_op->nr_cached_objects) 204 fs_objects = sb->s_op->nr_cached_objects(sb, sc); 205 206 inodes = list_lru_shrink_count(&sb->s_inode_lru, sc); 207 dentries = list_lru_shrink_count(&sb->s_dentry_lru, sc); 208 total_objects = dentries + inodes + fs_objects; 209 if (!total_objects) 210 total_objects = 1; 211 212 /* proportion the scan between the caches */ 213 dentries = mult_frac(sc->nr_to_scan, dentries, total_objects); 214 inodes = mult_frac(sc->nr_to_scan, inodes, total_objects); 215 fs_objects = mult_frac(sc->nr_to_scan, fs_objects, total_objects); 216 217 /* 218 * prune the dcache first as the icache is pinned by it, then 219 * prune the icache, followed by the filesystem specific caches 220 * 221 * Ensure that we always scan at least one object - memcg kmem 222 * accounting uses this to fully empty the caches. 223 */ 224 sc->nr_to_scan = dentries + 1; 225 freed = prune_dcache_sb(sb, sc); 226 sc->nr_to_scan = inodes + 1; 227 freed += prune_icache_sb(sb, sc); 228 229 if (fs_objects) { 230 sc->nr_to_scan = fs_objects + 1; 231 freed += sb->s_op->free_cached_objects(sb, sc); 232 } 233 234 super_unlock_shared(sb); 235 return freed; 236 } 237 238 static unsigned long super_cache_count(struct shrinker *shrink, 239 struct shrink_control *sc) 240 { 241 struct super_block *sb; 242 long total_objects = 0; 243 244 sb = shrink->private_data; 245 246 /* 247 * We don't call super_trylock_shared() here as it is a scalability 248 * bottleneck, so we're exposed to partial setup state. The shrinker 249 * rwsem does not protect filesystem operations backing 250 * list_lru_shrink_count() or s_op->nr_cached_objects(). Counts can 251 * change between super_cache_count and super_cache_scan, so we really 252 * don't need locks here. 253 * 254 * However, if we are currently mounting the superblock, the underlying 255 * filesystem might be in a state of partial construction and hence it 256 * is dangerous to access it. super_trylock_shared() uses a SB_BORN check 257 * to avoid this situation, so do the same here. The memory barrier is 258 * matched with the one in mount_fs() as we don't hold locks here. 259 */ 260 if (!(sb->s_flags & SB_BORN)) 261 return 0; 262 smp_rmb(); 263 264 if (sb->s_op && sb->s_op->nr_cached_objects) 265 total_objects = sb->s_op->nr_cached_objects(sb, sc); 266 267 total_objects += list_lru_shrink_count(&sb->s_dentry_lru, sc); 268 total_objects += list_lru_shrink_count(&sb->s_inode_lru, sc); 269 270 if (!total_objects) 271 return SHRINK_EMPTY; 272 273 total_objects = vfs_pressure_ratio(total_objects); 274 return total_objects; 275 } 276 277 static struct super_dev *super_dev_alloc(dev_t dev, struct super_block *sb); 278 279 static void destroy_super_work(struct work_struct *work) 280 { 281 struct super_block *s = container_of(work, struct super_block, 282 destroy_work); 283 fsnotify_sb_free(s); 284 security_sb_free(s); 285 put_user_ns(s->s_user_ns); 286 /* Only an unregistered entry is still owned by the superblock. */ 287 kfree(s->s_super_dev); 288 kfree(s->s_subtype); 289 for (int i = 0; i < SB_FREEZE_LEVELS; i++) 290 percpu_free_rwsem(&s->s_writers.rw_sem[i]); 291 kfree(s); 292 } 293 294 static void destroy_super_rcu(struct rcu_head *head) 295 { 296 struct super_block *s = container_of(head, struct super_block, rcu); 297 INIT_WORK(&s->destroy_work, destroy_super_work); 298 schedule_work(&s->destroy_work); 299 } 300 301 /* Free a superblock that has never been seen by anyone */ 302 static void destroy_unused_super(struct super_block *s) 303 { 304 if (!s) 305 return; 306 super_unlock_excl(s); 307 list_lru_destroy(&s->s_dentry_lru); 308 list_lru_destroy(&s->s_inode_lru); 309 shrinker_free(s->s_shrink); 310 /* no delays needed */ 311 destroy_super_work(&s->destroy_work); 312 } 313 314 /** 315 * alloc_super - create new superblock 316 * @type: filesystem type superblock should belong to 317 * @flags: the mount flags 318 * @user_ns: User namespace for the super_block 319 * 320 * Allocates and initializes a new &struct super_block. alloc_super() 321 * returns a pointer new superblock or %NULL if allocation had failed. 322 */ 323 static struct super_block *alloc_super(struct file_system_type *type, int flags, 324 struct user_namespace *user_ns) 325 { 326 struct super_block *s = kzalloc_obj(struct super_block); 327 static const struct super_operations default_op; 328 int i; 329 330 if (!s) 331 return NULL; 332 333 s->s_user_ns = get_user_ns(user_ns); 334 init_rwsem(&s->s_umount); 335 lockdep_set_class(&s->s_umount, &type->s_umount_key); 336 /* 337 * sget_fc() can have s_umount recursion. 338 * 339 * When it cannot find a suitable sb, it allocates a new 340 * one (this one), and tries again to find a suitable old 341 * one. 342 * 343 * In case that succeeds, it will acquire the s_umount 344 * lock of the old one. Since these are clearly distrinct 345 * locks, and this object isn't exposed yet, there's no 346 * risk of deadlocks. 347 * 348 * Annotate this by putting this lock in a different 349 * subclass. 350 */ 351 down_write_nested(&s->s_umount, SINGLE_DEPTH_NESTING); 352 353 if (security_sb_alloc(s)) 354 goto fail; 355 356 for (i = 0; i < SB_FREEZE_LEVELS; i++) { 357 if (__percpu_init_rwsem(&s->s_writers.rw_sem[i], 358 sb_writers_name[i], 359 &type->s_writers_key[i])) 360 goto fail; 361 } 362 s->s_bdi = &noop_backing_dev_info; 363 s->s_flags = flags; 364 if (s->s_user_ns != &init_user_ns) 365 s->s_iflags |= SB_I_NODEV; 366 INIT_HLIST_NODE(&s->s_instances); 367 INIT_HLIST_BL_HEAD(&s->s_roots); 368 spin_lock_init(&s->s_roots_lock); 369 mutex_init(&s->s_sync_lock); 370 INIT_LIST_HEAD(&s->s_inodes); 371 spin_lock_init(&s->s_inode_list_lock); 372 INIT_LIST_HEAD(&s->s_inodes_wb); 373 spin_lock_init(&s->s_inode_wblist_lock); 374 fserror_mount(s); 375 376 refcount_set(&s->s_passive, 1); 377 atomic_set(&s->s_active, 1); 378 mutex_init(&s->s_vfs_rename_mutex); 379 lockdep_set_class(&s->s_vfs_rename_mutex, &type->s_vfs_rename_key); 380 init_rwsem(&s->s_dquot.dqio_sem); 381 s->s_maxbytes = MAX_NON_LFS; 382 s->s_op = &default_op; 383 s->s_time_gran = 1000000000; 384 s->s_time_min = TIME64_MIN; 385 s->s_time_max = TIME64_MAX; 386 387 s->s_shrink = shrinker_alloc(SHRINKER_NUMA_AWARE | SHRINKER_MEMCG_AWARE, 388 "sb-%s", type->name); 389 if (!s->s_shrink) 390 goto fail; 391 392 s->s_shrink->scan_objects = super_cache_scan; 393 s->s_shrink->count_objects = super_cache_count; 394 s->s_shrink->batch = 1024; 395 s->s_shrink->private_data = s; 396 397 if (list_lru_init_memcg(&s->s_dentry_lru, s->s_shrink)) 398 goto fail; 399 if (list_lru_init_memcg(&s->s_inode_lru, s->s_shrink)) 400 goto fail; 401 s->s_super_dev = super_dev_alloc(0, s); 402 if (!s->s_super_dev) 403 goto fail; 404 405 s->s_min_writeback_pages = MIN_WRITEBACK_PAGES; 406 return s; 407 408 fail: 409 destroy_unused_super(s); 410 return NULL; 411 } 412 413 /* Superblock refcounting */ 414 415 /* 416 * Drop a superblock's passive reference. Must be called WITHOUT sb_lock held; 417 * put_super() acquires sb_lock itself when the final reference is dropped. 418 */ 419 void put_super(struct super_block *s) 420 { 421 if (refcount_dec_and_test(&s->s_passive)) { 422 423 spin_lock(&sb_lock); 424 list_del_init(&s->s_list); 425 spin_unlock(&sb_lock); 426 427 WARN_ON(s->s_dentry_lru.node); 428 WARN_ON(s->s_inode_lru.node); 429 WARN_ON(s->s_mounts); 430 call_rcu(&s->rcu, destroy_super_rcu); 431 } 432 } 433 434 struct super_dev { 435 dev_t sd_dev; 436 struct super_block *sd_sb; 437 refcount_t sd_ref; 438 struct rhlist_head sd_node; 439 struct rcu_head sd_rcu; 440 }; 441 442 static struct rhltable super_dev_table; 443 static const struct rhashtable_params super_dev_params = { 444 .key_len = sizeof(dev_t), 445 .key_offset = offsetof(struct super_dev, sd_dev), 446 .head_offset = offsetof(struct super_dev, sd_node), 447 }; 448 449 static struct super_dev *super_dev_alloc(dev_t dev, struct super_block *sb) 450 { 451 struct super_dev *fsd; 452 453 fsd = kzalloc_obj(*fsd); 454 if (!fsd) 455 return NULL; 456 fsd->sd_dev = dev; 457 fsd->sd_sb = sb; 458 refcount_set(&fsd->sd_ref, 1); 459 return fsd; 460 } 461 462 static void super_dev_put(struct super_dev *fsd) 463 { 464 /* Unlink only once unpinned, so a cursor never resumes from a removed node. */ 465 if (fsd && refcount_dec_and_test(&fsd->sd_ref)) { 466 rhltable_remove(&super_dev_table, &fsd->sd_node, super_dev_params); 467 put_super(fsd->sd_sb); 468 kfree_rcu(fsd, sd_rcu); 469 } 470 } 471 472 void __init super_dev_init(void) 473 { 474 if (rhltable_init(&super_dev_table, &super_dev_params)) 475 panic("VFS: Cannot initialise super_dev_table\n"); 476 } 477 478 static int super_dev_insert(struct super_dev *fsd) 479 { 480 int err; 481 482 err = rhltable_insert(&super_dev_table, &fsd->sd_node, super_dev_params); 483 if (!err) 484 refcount_inc(&fsd->sd_sb->s_passive); 485 return err; 486 } 487 488 /* Register @sb under @sb->s_dev as the final fallible act of a set callback. */ 489 static int super_dev_register(struct super_block *sb) 490 { 491 struct super_dev *fsd = sb->s_super_dev; 492 int err; 493 494 lockdep_assert_held(&sb_lock); 495 VFS_WARN_ON_ONCE(!sb->s_dev); 496 VFS_WARN_ON_ONCE(!fsd || fsd->sd_dev); 497 498 fsd->sd_dev = sb->s_dev; 499 err = super_dev_insert(fsd); 500 if (err) 501 fsd->sd_dev = 0; 502 return err; 503 } 504 505 static struct super_dev *super_dev_get(struct rhlist_head *pos) 506 { 507 struct super_dev *sb_dev; 508 509 for (; pos; pos = rcu_dereference_all(pos->next)) { 510 sb_dev = container_of(pos, struct super_dev, sd_node); 511 if (refcount_inc_not_zero(&sb_dev->sd_ref)) 512 return sb_dev; 513 } 514 return NULL; 515 } 516 517 static struct super_dev *super_dev_first(dev_t dev) 518 { 519 struct super_dev *sb_dev; 520 521 rcu_read_lock(); 522 sb_dev = super_dev_get(rhltable_lookup(&super_dev_table, &dev, super_dev_params)); 523 rcu_read_unlock(); 524 return sb_dev; 525 } 526 527 static struct super_dev *super_dev_next(struct super_dev *prev) 528 { 529 struct super_dev *sb_dev; 530 531 rcu_read_lock(); 532 sb_dev = super_dev_get(rcu_dereference_all(prev->sd_node.next)); 533 rcu_read_unlock(); 534 535 super_dev_put(prev); 536 return sb_dev; 537 } 538 539 static void kill_super_notify(struct super_block *sb) 540 { 541 lockdep_assert_not_held(&sb->s_umount); 542 543 /* already notified earlier */ 544 if (sb->s_flags & SB_DEAD) 545 return; 546 547 /* 548 * Remove it from @fs_supers so it isn't found by new 549 * sget_fc() walkers anymore. Any concurrent mounter still 550 * managing to grab a temporary reference is guaranteed to 551 * already see SB_DYING and will wait until we notify them about 552 * SB_DEAD. 553 */ 554 spin_lock(&sb_lock); 555 hlist_del_init(&sb->s_instances); 556 spin_unlock(&sb_lock); 557 558 /* Drop sget_fc()'s claim; a never-registered entry stays with the sb. */ 559 if (sb->s_super_dev->sd_dev) { 560 super_dev_put(sb->s_super_dev); 561 sb->s_super_dev = NULL; 562 } 563 564 /* 565 * Let concurrent mounts know that this thing is really dead. 566 * We don't need @sb->s_umount here as every concurrent caller 567 * will see SB_DYING and either discard the superblock or wait 568 * for SB_DEAD. 569 */ 570 super_wake(sb, SB_DEAD); 571 } 572 573 /** 574 * deactivate_locked_super - drop an active reference to superblock 575 * @s: superblock to deactivate 576 * 577 * Drops an active reference to superblock, converting it into a temporary 578 * one if there is no other active references left. In that case we 579 * tell fs driver to shut it down and drop the temporary reference we 580 * had just acquired. 581 * 582 * Caller holds exclusive lock on superblock; that lock is released. 583 */ 584 void deactivate_locked_super(struct super_block *s) 585 { 586 struct file_system_type *fs = s->s_type; 587 if (atomic_dec_and_test(&s->s_active)) { 588 shrinker_free(s->s_shrink); 589 fs->kill_sb(s); 590 591 kill_super_notify(s); 592 593 /* list_lru_destroy() may sleep; put_super() callers may not. */ 594 list_lru_destroy(&s->s_dentry_lru); 595 list_lru_destroy(&s->s_inode_lru); 596 597 put_filesystem(fs); 598 put_super(s); 599 } else { 600 super_unlock_excl(s); 601 } 602 } 603 604 EXPORT_SYMBOL(deactivate_locked_super); 605 606 /** 607 * deactivate_super - drop an active reference to superblock 608 * @s: superblock to deactivate 609 * 610 * Variant of deactivate_locked_super(), except that superblock is *not* 611 * locked by caller. If we are going to drop the final active reference, 612 * lock will be acquired prior to that. 613 */ 614 void deactivate_super(struct super_block *s) 615 { 616 if (!atomic_add_unless(&s->s_active, -1, 1)) { 617 __super_lock_excl(s); 618 deactivate_locked_super(s); 619 } 620 } 621 622 EXPORT_SYMBOL(deactivate_super); 623 624 /** 625 * grab_super - acquire an active reference to a superblock 626 * @sb: superblock to acquire 627 * 628 * Acquire a temporary reference on a superblock and try to trade it for 629 * an active reference. This is used in sget_fc() to wait for a 630 * superblock to either become SB_BORN or for it to pass through 631 * sb->kill() and be marked as SB_DEAD. 632 * 633 * Return: This returns true if an active reference could be acquired, 634 * false if not. 635 */ 636 static bool grab_super(struct super_block *sb) 637 { 638 bool locked; 639 640 refcount_inc(&sb->s_passive); 641 spin_unlock(&sb_lock); 642 locked = super_lock_excl(sb); 643 if (locked) { 644 if (atomic_inc_not_zero(&sb->s_active)) { 645 put_super(sb); 646 return true; 647 } 648 super_unlock_excl(sb); 649 } 650 wait_var_event(&sb->s_flags, super_flags(sb, SB_DEAD)); 651 put_super(sb); 652 return false; 653 } 654 655 /* 656 * super_trylock_shared - try to grab ->s_umount shared 657 * @sb: reference we are trying to grab 658 * 659 * Try to prevent fs shutdown. This is used in places where we 660 * cannot take an active reference but we need to ensure that the 661 * filesystem is not shut down while we are working on it. It returns 662 * false if we cannot acquire s_umount or if we lose the race and 663 * filesystem already got into shutdown, and returns true with the s_umount 664 * lock held in read mode in case of success. On successful return, 665 * the caller must drop the s_umount lock when done. 666 * 667 * Note that unlike get_super() et.al. this one does *not* bump ->s_passive. 668 * The reason why it's safe is that we are OK with doing trylock instead 669 * of down_read(). There's a couple of places that are OK with that, but 670 * it's very much not a general-purpose interface. 671 */ 672 bool super_trylock_shared(struct super_block *sb) 673 { 674 if (down_read_trylock(&sb->s_umount)) { 675 if (!(sb->s_flags & SB_DYING) && sb->s_root && 676 (sb->s_flags & SB_BORN)) 677 return true; 678 super_unlock_shared(sb); 679 } 680 681 return false; 682 } 683 684 /** 685 * retire_super - prevents superblock from being reused 686 * @sb: superblock to retire 687 * 688 * The function marks superblock to be ignored in superblock test, which 689 * prevents it from being reused for any new mounts. If the superblock has 690 * a private bdi, it also unregisters it, but doesn't reduce the refcount 691 * of the superblock to prevent potential races. The refcount is reduced 692 * by generic_shutdown_super(). The function can not be called 693 * concurrently with generic_shutdown_super(). It is safe to call the 694 * function multiple times, subsequent calls have no effect. 695 * 696 * The marker will affect the re-use only for block-device-based 697 * superblocks. Other superblocks will still get marked if this function 698 * is used, but that will not affect their reusability. 699 */ 700 void retire_super(struct super_block *sb) 701 { 702 WARN_ON(!sb->s_bdev); 703 __super_lock_excl(sb); 704 if (sb->s_iflags & SB_I_PERSB_BDI) { 705 bdi_unregister(sb->s_bdi); 706 sb->s_iflags &= ~SB_I_PERSB_BDI; 707 } 708 sb->s_iflags |= SB_I_RETIRED; 709 super_unlock_excl(sb); 710 } 711 EXPORT_SYMBOL(retire_super); 712 713 /** 714 * generic_shutdown_super - common helper for ->kill_sb() 715 * @sb: superblock to kill 716 * 717 * generic_shutdown_super() does all fs-independent work on superblock 718 * shutdown. Typical ->kill_sb() should pick all fs-specific objects 719 * that need destruction out of superblock, call generic_shutdown_super() 720 * and release aforementioned objects. Note: dentries and inodes _are_ 721 * taken care of and do not need specific handling. 722 * 723 * Upon calling this function, the filesystem may no longer alter or 724 * rearrange the set of dentries belonging to this super_block, nor may it 725 * change the attachments of dentries to inodes. 726 */ 727 void generic_shutdown_super(struct super_block *sb) 728 { 729 const struct super_operations *sop = sb->s_op; 730 731 if (sb->s_root) { 732 fsnotify_sb_delete(sb); 733 shrink_dcache_for_umount(sb); 734 sync_filesystem(sb); 735 sb->s_flags &= ~SB_ACTIVE; 736 737 fserror_unmount(sb); 738 cgroup_writeback_umount(sb); 739 740 /* Evict all inodes with zero refcount. */ 741 evict_inodes(sb); 742 743 /* 744 * Clean up and evict any inodes that still have references due 745 * to the security policy. 746 */ 747 security_sb_delete(sb); 748 749 if (sb->s_dio_done_wq) { 750 destroy_workqueue(sb->s_dio_done_wq); 751 sb->s_dio_done_wq = NULL; 752 } 753 754 if (sop->put_super) 755 sop->put_super(sb); 756 757 /* 758 * Now that all potentially-encrypted inodes have been evicted, 759 * the fscrypt keyring can be destroyed. 760 */ 761 fscrypt_destroy_keyring(sb); 762 763 if (CHECK_DATA_CORRUPTION(!list_empty(&sb->s_inodes), NULL, 764 "VFS: Busy inodes after unmount of %s (%s)", 765 sb->s_id, sb->s_type->name)) { 766 /* 767 * Adding a proper bailout path here would be hard, but 768 * we can at least make it more likely that a later 769 * iput_final() or such crashes cleanly. 770 */ 771 struct inode *inode; 772 773 spin_lock(&sb->s_inode_list_lock); 774 list_for_each_entry(inode, &sb->s_inodes, i_sb_list) { 775 inode->i_op = VFS_PTR_POISON; 776 inode->i_sb = VFS_PTR_POISON; 777 inode->i_mapping = VFS_PTR_POISON; 778 } 779 spin_unlock(&sb->s_inode_list_lock); 780 } 781 } 782 /* 783 * Broadcast to everyone that grabbed a temporary reference to this 784 * superblock before we removed it from @fs_supers that the superblock 785 * is dying. Every walker of @fs_supers outside of sget_fc() will now 786 * discard this superblock and treat it as dead. 787 * 788 * We leave the superblock on @fs_supers so it can be found by 789 * sget_fc() until we passed sb->kill_sb(). 790 */ 791 super_wake(sb, SB_DYING); 792 super_unlock_excl(sb); 793 if (sb->s_bdi != &noop_backing_dev_info) { 794 if (sb->s_iflags & SB_I_PERSB_BDI) 795 bdi_unregister(sb->s_bdi); 796 bdi_put(sb->s_bdi); 797 sb->s_bdi = &noop_backing_dev_info; 798 } 799 } 800 801 EXPORT_SYMBOL(generic_shutdown_super); 802 803 bool mount_capable(struct fs_context *fc) 804 { 805 if (!(fc->fs_type->fs_flags & FS_USERNS_MOUNT)) 806 return capable(CAP_SYS_ADMIN); 807 else 808 return ns_capable(fc->user_ns, CAP_SYS_ADMIN); 809 } 810 811 /** 812 * sget_fc - Find or create a superblock 813 * @fc: Filesystem context. 814 * @test: Comparison callback 815 * @set: Setup callback 816 * 817 * Create a new superblock or find an existing one. 818 * 819 * The @test callback is used to find a matching existing superblock. 820 * Whether or not the requested parameters in @fc are taken into account 821 * is specific to the @test callback that is used. They may even be 822 * completely ignored. 823 * 824 * If an extant superblock is matched, it will be returned unless: 825 * 826 * (1) the namespace the filesystem context @fc and the extant 827 * superblock's namespace differ 828 * 829 * (2) the filesystem context @fc has requested that reusing an extant 830 * superblock is not allowed 831 * 832 * In both cases EBUSY will be returned. 833 * 834 * If no match is made, a new superblock will be allocated and basic 835 * initialisation will be performed (s_type, s_fs_info and s_id will be 836 * set and the @set callback will be invoked), the superblock will be 837 * published and it will be returned in a partially constructed state 838 * with SB_BORN and SB_ACTIVE as yet unset. 839 * 840 * Return: On success, an extant or newly created superblock is 841 * returned. On failure an error pointer is returned. 842 */ 843 struct super_block *sget_fc(struct fs_context *fc, 844 int (*test)(struct super_block *, struct fs_context *), 845 int (*set)(struct super_block *, struct fs_context *)) 846 { 847 struct super_block *s = NULL; 848 struct super_block *old; 849 struct user_namespace *user_ns = fc->global ? &init_user_ns : fc->user_ns; 850 int err; 851 852 /* 853 * Never allow s_user_ns != &init_user_ns when FS_USERNS_MOUNT or 854 * FS_USERNS_DELEGATABLE is not set, as the filesystem is likely 855 * unprepared to handle it. This can happen when fsconfig() is called 856 * from init_user_ns with an fs_fd opened in another user namespace. 857 */ 858 if (user_ns != &init_user_ns && 859 !(fc->fs_type->fs_flags & (FS_USERNS_MOUNT | FS_USERNS_DELEGATABLE))) { 860 errorfc(fc, "VFS: Mounting from non-initial user namespace is not allowed"); 861 return ERR_PTR(-EPERM); 862 } 863 864 retry: 865 spin_lock(&sb_lock); 866 if (test) { 867 hlist_for_each_entry(old, &fc->fs_type->fs_supers, s_instances) { 868 if (test(old, fc)) 869 goto share_extant_sb; 870 } 871 } 872 if (!s) { 873 spin_unlock(&sb_lock); 874 875 s = alloc_super(fc->fs_type, fc->sb_flags, user_ns); 876 if (!s) 877 return ERR_PTR(-ENOMEM); 878 goto retry; 879 } 880 881 s->s_fs_info = fc->s_fs_info; 882 err = set(s, fc); 883 if (err) { 884 VFS_WARN_ON_ONCE(s->s_super_dev->sd_dev); 885 s->s_fs_info = NULL; 886 spin_unlock(&sb_lock); 887 destroy_unused_super(s); 888 return ERR_PTR(err); 889 } 890 VFS_WARN_ON_ONCE(!s->s_super_dev->sd_dev); 891 fc->s_fs_info = NULL; 892 s->s_type = fc->fs_type; 893 s->s_iflags |= fc->s_iflags; 894 strscpy(s->s_id, s->s_type->name, sizeof(s->s_id)); 895 /* 896 * Make the superblock visible on @super_blocks and @fs_supers. 897 * It's in a nascent state and users should wait on SB_BORN or 898 * SB_DYING to be set. 899 */ 900 list_add_tail(&s->s_list, &super_blocks); 901 hlist_add_head(&s->s_instances, &s->s_type->fs_supers); 902 spin_unlock(&sb_lock); 903 get_filesystem(s->s_type); 904 shrinker_register(s->s_shrink); 905 return s; 906 907 share_extant_sb: 908 if (user_ns != old->s_user_ns || fc->exclusive) { 909 spin_unlock(&sb_lock); 910 destroy_unused_super(s); 911 if (fc->exclusive) 912 warnfc(fc, "reusing existing filesystem not allowed"); 913 else 914 warnfc(fc, "reusing existing filesystem in another namespace not allowed"); 915 return ERR_PTR(-EBUSY); 916 } 917 if (!grab_super(old)) 918 goto retry; 919 destroy_unused_super(s); 920 return old; 921 } 922 EXPORT_SYMBOL(sget_fc); 923 924 void drop_super(struct super_block *sb) 925 { 926 super_unlock_shared(sb); 927 put_super(sb); 928 } 929 930 EXPORT_SYMBOL(drop_super); 931 932 void drop_super_exclusive(struct super_block *sb) 933 { 934 super_unlock_excl(sb); 935 put_super(sb); 936 } 937 938 enum super_iter_flags_t { 939 SUPER_ITER_EXCL = (1U << 0), 940 SUPER_ITER_UNLOCKED = (1U << 1), 941 SUPER_ITER_REVERSE = (1U << 2), 942 }; 943 944 static inline struct super_block *first_super(enum super_iter_flags_t flags) 945 { 946 if (flags & SUPER_ITER_REVERSE) 947 return list_last_entry(&super_blocks, struct super_block, s_list); 948 return list_first_entry(&super_blocks, struct super_block, s_list); 949 } 950 951 static inline struct super_block *next_super(struct super_block *sb, 952 enum super_iter_flags_t flags) 953 { 954 if (flags & SUPER_ITER_REVERSE) 955 return list_prev_entry(sb, s_list); 956 return list_next_entry(sb, s_list); 957 } 958 959 static void __iterate_supers(void (*f)(struct super_block *, void *), void *arg, 960 enum super_iter_flags_t flags) 961 { 962 struct super_block *sb, *p = NULL; 963 bool excl = flags & SUPER_ITER_EXCL; 964 965 spin_lock(&sb_lock); 966 967 for (sb = first_super(flags); 968 !list_entry_is_head(sb, &super_blocks, s_list); 969 sb = next_super(sb, flags)) { 970 if (super_flags(sb, SB_DYING)) 971 continue; 972 973 if (!refcount_inc_not_zero(&sb->s_passive)) 974 continue; 975 976 spin_unlock(&sb_lock); 977 978 if (flags & SUPER_ITER_UNLOCKED) { 979 f(sb, arg); 980 } else if (super_lock(sb, excl)) { 981 f(sb, arg); 982 super_unlock(sb, excl); 983 } 984 985 if (p) 986 put_super(p); 987 p = sb; 988 spin_lock(&sb_lock); 989 } 990 spin_unlock(&sb_lock); 991 if (p) 992 put_super(p); 993 } 994 995 void iterate_supers(void (*f)(struct super_block *, void *), void *arg) 996 { 997 __iterate_supers(f, arg, 0); 998 } 999 1000 /** 1001 * iterate_supers_type - call function for superblocks of given type 1002 * @type: fs type 1003 * @f: function to call 1004 * @arg: argument to pass to it 1005 * 1006 * Scans the superblock list and calls given function, passing it 1007 * locked superblock and given argument. 1008 */ 1009 void iterate_supers_type(struct file_system_type *type, 1010 void (*f)(struct super_block *, void *), void *arg) 1011 { 1012 struct super_block *sb, *p = NULL; 1013 1014 spin_lock(&sb_lock); 1015 hlist_for_each_entry(sb, &type->fs_supers, s_instances) { 1016 bool locked; 1017 1018 if (super_flags(sb, SB_DYING)) 1019 continue; 1020 1021 if (!refcount_inc_not_zero(&sb->s_passive)) 1022 continue; 1023 1024 spin_unlock(&sb_lock); 1025 1026 locked = super_lock_shared(sb); 1027 if (locked) { 1028 f(sb, arg); 1029 super_unlock_shared(sb); 1030 } 1031 1032 if (p) 1033 put_super(p); 1034 p = sb; 1035 spin_lock(&sb_lock); 1036 } 1037 spin_unlock(&sb_lock); 1038 if (p) 1039 put_super(p); 1040 } 1041 1042 EXPORT_SYMBOL(iterate_supers_type); 1043 1044 struct super_block *user_get_super(dev_t dev, bool excl) 1045 { 1046 struct super_dev *sb_dev; 1047 1048 for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) { 1049 struct super_block *sb = sb_dev->sd_sb; 1050 1051 if (!super_lock(sb, excl)) 1052 continue; 1053 1054 /* The pinned entry holds a passive reference, take our own. */ 1055 refcount_inc(&sb->s_passive); 1056 super_dev_put(sb_dev); 1057 return sb; 1058 } 1059 return NULL; 1060 } 1061 1062 /** 1063 * reconfigure_super - asks filesystem to change superblock parameters 1064 * @fc: The superblock and configuration 1065 * 1066 * Alters the configuration parameters of a live superblock. 1067 */ 1068 int reconfigure_super(struct fs_context *fc) 1069 { 1070 struct super_block *sb = fc->root->d_sb; 1071 int retval; 1072 bool remount_ro = false; 1073 bool remount_rw = false; 1074 bool force = fc->sb_flags & SB_FORCE; 1075 1076 if (fc->sb_flags_mask & ~MS_RMT_MASK) 1077 return -EINVAL; 1078 if (sb->s_writers.frozen != SB_UNFROZEN) 1079 return -EBUSY; 1080 1081 retval = security_sb_remount(sb, fc->security); 1082 if (retval) 1083 return retval; 1084 1085 if (fc->sb_flags_mask & SB_RDONLY) { 1086 #ifdef CONFIG_BLOCK 1087 if (!(fc->sb_flags & SB_RDONLY) && sb->s_bdev && 1088 bdev_read_only(sb->s_bdev)) 1089 return -EACCES; 1090 #endif 1091 remount_rw = !(fc->sb_flags & SB_RDONLY) && sb_rdonly(sb); 1092 remount_ro = (fc->sb_flags & SB_RDONLY) && !sb_rdonly(sb); 1093 } 1094 1095 if (remount_ro) { 1096 if (!hlist_empty(&sb->s_pins)) { 1097 super_unlock_excl(sb); 1098 group_pin_kill(&sb->s_pins); 1099 __super_lock_excl(sb); 1100 if (!sb->s_root) 1101 return 0; 1102 if (sb->s_writers.frozen != SB_UNFROZEN) 1103 return -EBUSY; 1104 remount_ro = !sb_rdonly(sb); 1105 } 1106 } 1107 shrink_dcache_sb(sb); 1108 1109 /* If we are reconfiguring to RDONLY and current sb is read/write, 1110 * make sure there are no files open for writing. 1111 */ 1112 if (remount_ro) { 1113 if (force) { 1114 sb_start_ro_state_change(sb); 1115 } else { 1116 retval = sb_prepare_remount_readonly(sb); 1117 if (retval) 1118 return retval; 1119 } 1120 } else if (remount_rw) { 1121 /* 1122 * Protect filesystem's reconfigure code from writes from 1123 * userspace until reconfigure finishes. 1124 */ 1125 sb_start_ro_state_change(sb); 1126 } 1127 1128 if (fc->ops->reconfigure) { 1129 retval = fc->ops->reconfigure(fc); 1130 if (retval) { 1131 if (!force) 1132 goto cancel_readonly; 1133 /* If forced remount, go ahead despite any errors */ 1134 WARN(1, "forced remount of a %s fs returned %i\n", 1135 sb->s_type->name, retval); 1136 } 1137 } 1138 1139 WRITE_ONCE(sb->s_flags, ((sb->s_flags & ~fc->sb_flags_mask) | 1140 (fc->sb_flags & fc->sb_flags_mask))); 1141 sb_end_ro_state_change(sb); 1142 1143 /* 1144 * Some filesystems modify their metadata via some other path than the 1145 * bdev buffer cache (eg. use a private mapping, or directories in 1146 * pagecache, etc). Also file data modifications go via their own 1147 * mappings. So If we try to mount readonly then copy the filesystem 1148 * from bdev, we could get stale data, so invalidate it to give a best 1149 * effort at coherency. 1150 */ 1151 if (remount_ro && sb->s_bdev) 1152 invalidate_bdev(sb->s_bdev); 1153 return 0; 1154 1155 cancel_readonly: 1156 sb_end_ro_state_change(sb); 1157 return retval; 1158 } 1159 1160 static void do_emergency_remount_callback(struct super_block *sb, void *unused) 1161 { 1162 if (sb->s_bdev && !sb_rdonly(sb)) { 1163 struct fs_context *fc; 1164 1165 fc = fs_context_for_reconfigure(sb->s_root, 1166 SB_RDONLY | SB_FORCE, SB_RDONLY); 1167 if (!IS_ERR(fc)) { 1168 if (parse_monolithic_mount_data(fc, NULL) == 0) 1169 (void)reconfigure_super(fc); 1170 put_fs_context(fc); 1171 } 1172 } 1173 } 1174 1175 static void do_emergency_remount(struct work_struct *work) 1176 { 1177 __iterate_supers(do_emergency_remount_callback, NULL, 1178 SUPER_ITER_EXCL | SUPER_ITER_REVERSE); 1179 kfree(work); 1180 printk("Emergency Remount complete\n"); 1181 } 1182 1183 void emergency_remount(void) 1184 { 1185 struct work_struct *work; 1186 1187 work = kmalloc_obj(*work, GFP_ATOMIC); 1188 if (work) { 1189 INIT_WORK(work, do_emergency_remount); 1190 schedule_work(work); 1191 } 1192 } 1193 1194 static inline bool get_active_super(struct super_block *sb) 1195 { 1196 bool active = false; 1197 1198 if (super_lock_excl(sb)) { 1199 active = atomic_inc_not_zero(&sb->s_active); 1200 super_unlock_excl(sb); 1201 } 1202 return active; 1203 } 1204 1205 static void do_thaw_all_callback(struct super_block *sb, void *unused) 1206 { 1207 if (!get_active_super(sb)) 1208 return; 1209 1210 /* fs_bdev_thaw() acquires s_umount so it must not be held here */ 1211 if (IS_ENABLED(CONFIG_BLOCK)) 1212 while (sb->s_bdev && !bdev_thaw(sb->s_bdev)) 1213 pr_warn("Emergency Thaw on %pg\n", sb->s_bdev); 1214 1215 if (super_lock_excl(sb)) 1216 thaw_super_locked(sb, FREEZE_HOLDER_USERSPACE, NULL); 1217 deactivate_super(sb); 1218 } 1219 1220 static void do_thaw_all(struct work_struct *work) 1221 { 1222 __iterate_supers(do_thaw_all_callback, NULL, SUPER_ITER_UNLOCKED); 1223 kfree(work); 1224 printk(KERN_WARNING "Emergency Thaw complete\n"); 1225 } 1226 1227 /** 1228 * emergency_thaw_all -- forcibly thaw every frozen filesystem 1229 * 1230 * Used for emergency unfreeze of all filesystems via SysRq 1231 */ 1232 void emergency_thaw_all(void) 1233 { 1234 struct work_struct *work; 1235 1236 work = kmalloc_obj(*work, GFP_ATOMIC); 1237 if (work) { 1238 INIT_WORK(work, do_thaw_all); 1239 schedule_work(work); 1240 } 1241 } 1242 1243 static const char *filesystems_freeze_ptr = "filesystems_freeze"; 1244 1245 static void filesystems_freeze_callback(struct super_block *sb, void *freeze_all_ptr) 1246 { 1247 if (!sb->s_op->freeze_fs && !sb->s_op->freeze_super) 1248 return; 1249 1250 if (!freeze_all_ptr && !(sb->s_type->fs_flags & FS_POWER_FREEZE)) 1251 return; 1252 1253 if (!get_active_super(sb)) 1254 return; 1255 1256 if (sb->s_op->freeze_super) 1257 sb->s_op->freeze_super(sb, FREEZE_EXCL | FREEZE_HOLDER_KERNEL, 1258 filesystems_freeze_ptr); 1259 else 1260 freeze_super(sb, FREEZE_EXCL | FREEZE_HOLDER_KERNEL, 1261 filesystems_freeze_ptr); 1262 1263 deactivate_super(sb); 1264 } 1265 1266 void filesystems_freeze(bool freeze_all) 1267 { 1268 void *freeze_all_ptr = NULL; 1269 1270 if (freeze_all) 1271 freeze_all_ptr = &freeze_all; 1272 __iterate_supers(filesystems_freeze_callback, freeze_all_ptr, 1273 SUPER_ITER_UNLOCKED | SUPER_ITER_REVERSE); 1274 } 1275 1276 static void filesystems_thaw_callback(struct super_block *sb, void *unused) 1277 { 1278 if (!sb->s_op->freeze_fs && !sb->s_op->freeze_super) 1279 return; 1280 1281 if (!get_active_super(sb)) 1282 return; 1283 1284 if (sb->s_op->thaw_super) 1285 sb->s_op->thaw_super(sb, FREEZE_EXCL | FREEZE_HOLDER_KERNEL, 1286 filesystems_freeze_ptr); 1287 else 1288 thaw_super(sb, FREEZE_EXCL | FREEZE_HOLDER_KERNEL, 1289 filesystems_freeze_ptr); 1290 1291 deactivate_super(sb); 1292 } 1293 1294 void filesystems_thaw(void) 1295 { 1296 __iterate_supers(filesystems_thaw_callback, NULL, SUPER_ITER_UNLOCKED); 1297 } 1298 1299 static DEFINE_IDA(unnamed_dev_ida); 1300 1301 /** 1302 * get_anon_bdev - Allocate a block device for filesystems which don't have one. 1303 * @p: Pointer to a dev_t. 1304 * 1305 * Filesystems which don't use real block devices can call this function 1306 * to allocate a virtual block device. 1307 * 1308 * Context: Any context. Frequently called while holding sb_lock. 1309 * Return: 0 on success, -EMFILE if there are no anonymous bdevs left 1310 * or -ENOMEM if memory allocation failed. 1311 */ 1312 int get_anon_bdev(dev_t *p) 1313 { 1314 int dev; 1315 1316 /* 1317 * Many userspace utilities consider an FSID of 0 invalid. 1318 * Always return at least 1 from get_anon_bdev. 1319 */ 1320 dev = ida_alloc_range(&unnamed_dev_ida, 1, (1 << MINORBITS) - 1, 1321 GFP_ATOMIC); 1322 if (dev == -ENOSPC) 1323 dev = -EMFILE; 1324 if (dev < 0) 1325 return dev; 1326 1327 *p = MKDEV(0, dev); 1328 return 0; 1329 } 1330 EXPORT_SYMBOL(get_anon_bdev); 1331 1332 void free_anon_bdev(dev_t dev) 1333 { 1334 ida_free(&unnamed_dev_ida, MINOR(dev)); 1335 } 1336 EXPORT_SYMBOL(free_anon_bdev); 1337 1338 int set_anon_super(struct super_block *s, void *data) 1339 { 1340 int error; 1341 1342 error = get_anon_bdev(&s->s_dev); 1343 if (error) 1344 return error; 1345 1346 error = super_dev_register(s); 1347 if (error) 1348 free_anon_bdev(s->s_dev); 1349 return error; 1350 } 1351 EXPORT_SYMBOL(set_anon_super); 1352 1353 void kill_anon_super(struct super_block *sb) 1354 { 1355 dev_t dev = sb->s_dev; 1356 generic_shutdown_super(sb); 1357 kill_super_notify(sb); 1358 free_anon_bdev(dev); 1359 } 1360 EXPORT_SYMBOL(kill_anon_super); 1361 1362 int set_anon_super_fc(struct super_block *sb, struct fs_context *fc) 1363 { 1364 return set_anon_super(sb, NULL); 1365 } 1366 EXPORT_SYMBOL(set_anon_super_fc); 1367 1368 static int test_keyed_super(struct super_block *sb, struct fs_context *fc) 1369 { 1370 return sb->s_fs_info == fc->s_fs_info; 1371 } 1372 1373 static int test_single_super(struct super_block *s, struct fs_context *fc) 1374 { 1375 return 1; 1376 } 1377 1378 static int vfs_get_super(struct fs_context *fc, 1379 int (*test)(struct super_block *, struct fs_context *), 1380 int (*fill_super)(struct super_block *sb, 1381 struct fs_context *fc)) 1382 { 1383 struct super_block *sb; 1384 int err; 1385 1386 sb = sget_fc(fc, test, set_anon_super_fc); 1387 if (IS_ERR(sb)) 1388 return PTR_ERR(sb); 1389 1390 if (!sb->s_root) { 1391 err = fill_super(sb, fc); 1392 if (err) 1393 goto error; 1394 1395 sb->s_flags |= SB_ACTIVE; 1396 } 1397 1398 fc->root = dget(sb->s_root); 1399 return 0; 1400 1401 error: 1402 deactivate_locked_super(sb); 1403 return err; 1404 } 1405 1406 int get_tree_nodev(struct fs_context *fc, 1407 int (*fill_super)(struct super_block *sb, 1408 struct fs_context *fc)) 1409 { 1410 return vfs_get_super(fc, NULL, fill_super); 1411 } 1412 EXPORT_SYMBOL(get_tree_nodev); 1413 1414 int get_tree_single(struct fs_context *fc, 1415 int (*fill_super)(struct super_block *sb, 1416 struct fs_context *fc)) 1417 { 1418 return vfs_get_super(fc, test_single_super, fill_super); 1419 } 1420 EXPORT_SYMBOL(get_tree_single); 1421 1422 int get_tree_keyed(struct fs_context *fc, 1423 int (*fill_super)(struct super_block *sb, 1424 struct fs_context *fc), 1425 void *key) 1426 { 1427 fc->s_fs_info = key; 1428 return vfs_get_super(fc, test_keyed_super, fill_super); 1429 } 1430 EXPORT_SYMBOL(get_tree_keyed); 1431 1432 static int set_bdev_super(struct super_block *s, void *data) 1433 { 1434 s->s_dev = *(dev_t *)data; 1435 return super_dev_register(s); 1436 } 1437 1438 static int super_s_dev_set(struct super_block *s, struct fs_context *fc) 1439 { 1440 return set_bdev_super(s, fc->sget_key); 1441 } 1442 1443 static int super_s_dev_test(struct super_block *s, struct fs_context *fc) 1444 { 1445 return !(s->s_iflags & SB_I_RETIRED) && 1446 s->s_dev == *(dev_t *)fc->sget_key; 1447 } 1448 1449 /** 1450 * sget_dev - Find or create a superblock by device number 1451 * @fc: Filesystem context. 1452 * @dev: device number 1453 * 1454 * Find or create a superblock using the provided device number that 1455 * will be stored in fc->sget_key. 1456 * 1457 * If an extant superblock is matched, then that will be returned with 1458 * an elevated reference count that the caller must transfer or discard. 1459 * 1460 * If no match is made, a new superblock will be allocated and basic 1461 * initialisation will be performed (s_type, s_fs_info, s_id, s_dev will 1462 * be set). The superblock will be published and it will be returned in 1463 * a partially constructed state with SB_BORN and SB_ACTIVE as yet 1464 * unset. 1465 * 1466 * Return: an existing or newly created superblock on success, an error 1467 * pointer on failure. 1468 */ 1469 struct super_block *sget_dev(struct fs_context *fc, dev_t dev) 1470 { 1471 fc->sget_key = &dev; 1472 return sget_fc(fc, super_s_dev_test, super_s_dev_set); 1473 } 1474 EXPORT_SYMBOL(sget_dev); 1475 1476 #ifdef CONFIG_BLOCK 1477 static int fs_super_freeze(struct super_block *sb) 1478 { 1479 if (sb->s_op->freeze_super) 1480 return sb->s_op->freeze_super(sb, 1481 FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE, NULL); 1482 return freeze_super(sb, FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE, NULL); 1483 } 1484 1485 static int fs_super_thaw(struct super_block *sb) 1486 { 1487 if (sb->s_op->thaw_super) 1488 return sb->s_op->thaw_super(sb, 1489 FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE, NULL); 1490 return thaw_super(sb, FREEZE_MAY_NEST | FREEZE_HOLDER_USERSPACE, NULL); 1491 } 1492 1493 static void fs_bdev_mark_dead(struct block_device *bdev, bool surprise) 1494 { 1495 struct super_dev *sb_dev; 1496 dev_t dev = bdev->bd_dev; 1497 1498 mutex_unlock(&bdev->bd_holder_lock); 1499 1500 for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) { 1501 struct super_block *sb = sb_dev->sd_sb; 1502 1503 if (!super_lock_shared(sb)) 1504 continue; 1505 if (sb->s_root && (sb->s_flags & SB_ACTIVE)) { 1506 if (!sb->s_op->remove_bdev || 1507 sb->s_op->remove_bdev(sb, bdev)) { 1508 if (!surprise) 1509 sync_filesystem(sb); 1510 shrink_dcache_sb(sb); 1511 evict_inodes(sb); 1512 if (sb->s_op->shutdown) 1513 sb->s_op->shutdown(sb); 1514 } 1515 } 1516 super_unlock_shared(sb); 1517 } 1518 } 1519 1520 static void fs_bdev_sync(struct block_device *bdev) 1521 { 1522 struct super_dev *sb_dev; 1523 dev_t dev = bdev->bd_dev; 1524 1525 mutex_unlock(&bdev->bd_holder_lock); 1526 1527 for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) { 1528 struct super_block *sb = sb_dev->sd_sb; 1529 1530 if (!super_lock_shared(sb)) 1531 continue; 1532 if (sb->s_root && (sb->s_flags & SB_ACTIVE)) 1533 sync_filesystem(sb); 1534 super_unlock_shared(sb); 1535 } 1536 } 1537 1538 /** 1539 * fs_bdev_freeze - freeze every superblock using a block device 1540 * @bdev: block device 1541 * 1542 * Freeze each live superblock using @bdev. A superblock owning several block 1543 * devices is frozen once per device and stays frozen until all are thawed; the 1544 * block layer nests these freezes so the count stays balanced. 1545 * 1546 * Return: 0, or the error from the one superblock on a single-fs device. When 1547 * several superblocks share @bdev a per-superblock failure is swallowed 1548 * (see below), but a sync_blockdev() failure is always reported. 1549 */ 1550 static int fs_bdev_freeze(struct block_device *bdev) 1551 { 1552 dev_t dev = bdev->bd_dev; 1553 struct super_dev *sb_dev; 1554 unsigned int count = 0; 1555 int error = 0, err; 1556 1557 lockdep_assert_held(&bdev->bd_fsfreeze_mutex); 1558 1559 mutex_unlock(&bdev->bd_holder_lock); 1560 1561 for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) { 1562 if (!get_active_super(sb_dev->sd_sb)) 1563 continue; 1564 err = fs_super_freeze(sb_dev->sd_sb); 1565 if (err && !error) 1566 error = err; 1567 deactivate_super(sb_dev->sd_sb); 1568 count++; 1569 } 1570 1571 /* 1572 * When several superblocks share the device, keep it frozen even if some 1573 * of them failed to freeze and swallow the error: rolling the rest back 1574 * via thaw_super() can fail too, so neither is a clear win. A single 1575 * filesystem (count == 1) still reports its error. 1576 */ 1577 if (error && count > 1) 1578 error = 0; 1579 if (!error) 1580 error = sync_blockdev(bdev); 1581 return error; 1582 } 1583 1584 /** 1585 * fs_bdev_thaw - thaw every superblock using a block device 1586 * @bdev: block device 1587 * 1588 * The counterpart to fs_bdev_freeze(): thaw each live superblock using @bdev. 1589 * A zero return does not imply a superblock is fully unfrozen; it may have been 1590 * frozen more than once (by the kernel or via another device). 1591 * 1592 * Return: 0, or the first error on a single-fs device; a shared device swallows 1593 * per-superblock errors, as fs_bdev_freeze() does. 1594 */ 1595 static int fs_bdev_thaw(struct block_device *bdev) 1596 { 1597 dev_t dev = bdev->bd_dev; 1598 struct super_dev *sb_dev; 1599 unsigned int count = 0; 1600 int error = 0, err; 1601 1602 lockdep_assert_held(&bdev->bd_fsfreeze_mutex); 1603 1604 mutex_unlock(&bdev->bd_holder_lock); 1605 1606 for (sb_dev = super_dev_first(dev); sb_dev; sb_dev = super_dev_next(sb_dev)) { 1607 if (!get_active_super(sb_dev->sd_sb)) 1608 continue; 1609 err = fs_super_thaw(sb_dev->sd_sb); 1610 if (err && !error) 1611 error = err; 1612 deactivate_super(sb_dev->sd_sb); 1613 count++; 1614 } 1615 1616 /* Shared device: swallow per-superblock errors, like fs_bdev_freeze(). */ 1617 if (error && count > 1) 1618 error = 0; 1619 return error; 1620 } 1621 1622 static const struct blk_holder_ops fs_holder_ops = { 1623 .mark_dead = fs_bdev_mark_dead, 1624 .sync = fs_bdev_sync, 1625 .freeze = fs_bdev_freeze, 1626 .thaw = fs_bdev_thaw, 1627 }; 1628 1629 static struct super_dev *super_dev_lookup(dev_t dev, struct super_block *sb) 1630 { 1631 struct super_dev *it; 1632 struct rhlist_head *list, *pos; 1633 1634 RCU_LOCKDEP_WARN(!rcu_read_lock_held(), "suspicious super_dev_lookup() usage"); 1635 VFS_WARN_ON_ONCE(!dev); 1636 VFS_WARN_ON_ONCE(!sb); 1637 1638 list = rhltable_lookup(&super_dev_table, &dev, super_dev_params); 1639 rhl_for_each_entry_rcu(it, pos, list, sd_node) { 1640 if (it->sd_sb == sb) 1641 return it; 1642 } 1643 1644 return NULL; 1645 } 1646 1647 static int fs_bdev_register(struct file *bdev_file, struct super_block *sb) 1648 { 1649 struct super_dev *sb_dev __free(kfree) = NULL; 1650 dev_t dev = file_bdev(bdev_file)->bd_dev; 1651 int err; 1652 1653 scoped_guard(rcu) { 1654 sb_dev = super_dev_lookup(dev, sb); 1655 if (sb_dev && refcount_inc_not_zero(&sb_dev->sd_ref)) { 1656 retain_and_null_ptr(sb_dev); 1657 return 0; 1658 } 1659 } 1660 1661 sb_dev = super_dev_alloc(dev, sb); 1662 if (!sb_dev) 1663 return -ENOMEM; 1664 1665 err = super_dev_insert(sb_dev); 1666 if (err) 1667 return err; 1668 1669 /* Publish the entry before reading the count; pairs with bdev_freeze(). */ 1670 smp_mb(); 1671 if (atomic_read(&file_bdev(bdev_file)->bd_fsfreeze_count) > 0) { 1672 err = -EBUSY; 1673 super_dev_put(sb_dev); 1674 } 1675 1676 retain_and_null_ptr(sb_dev); 1677 return err; 1678 } 1679 1680 /** 1681 * fs_bdev_file_open_by_dev - claim a block device on behalf of a superblock 1682 * @dev: block device number 1683 * @mode: open mode 1684 * @holder: block-layer exclusivity token (a superblock, or the file_system_type 1685 * when the device may be shared by several superblocks of that type) 1686 * @sb: superblock to drive fs_holder_ops events for 1687 * 1688 * Open @dev with &fs_holder_ops and register that @sb uses it, so device 1689 * removal/sync/freeze/thaw are propagated to @sb (and any other superblock 1690 * sharing @dev). Must be paired with fs_bdev_file_release(). 1691 * 1692 * Return: an opened block-device file or an ERR_PTR(). 1693 */ 1694 struct file *fs_bdev_file_open_by_dev(dev_t dev, blk_mode_t mode, void *holder, 1695 struct super_block *sb) 1696 { 1697 struct file *bdev_file; 1698 int err; 1699 1700 bdev_file = bdev_file_open_by_dev(dev, mode, holder, &fs_holder_ops); 1701 if (IS_ERR(bdev_file)) 1702 return bdev_file; 1703 1704 err = fs_bdev_register(bdev_file, sb); 1705 if (err) { 1706 bdev_fput(bdev_file); 1707 return ERR_PTR(err); 1708 } 1709 return bdev_file; 1710 } 1711 EXPORT_SYMBOL_GPL(fs_bdev_file_open_by_dev); 1712 1713 /** 1714 * fs_bdev_file_open_by_path - claim a block device on behalf of a superblock 1715 * @path: path to the block device 1716 * @mode: open mode 1717 * @holder: block-layer exclusivity token (a superblock, or the file_system_type 1718 * when the device may be shared by several superblocks of that type) 1719 * @sb: superblock to drive fs_holder_ops events for 1720 * 1721 * Open the block device at @path with &fs_holder_ops and register that @sb 1722 * uses it, so device removal/sync/freeze/thaw are propagated to @sb (and any 1723 * other superblock sharing the device). Must be paired with 1724 * fs_bdev_file_release(). 1725 * 1726 * Return: an opened block-device file or an ERR_PTR(). 1727 */ 1728 struct file *fs_bdev_file_open_by_path(const char *path, blk_mode_t mode, 1729 void *holder, struct super_block *sb) 1730 { 1731 struct file *bdev_file; 1732 int err; 1733 1734 bdev_file = bdev_file_open_by_path(path, mode, holder, &fs_holder_ops); 1735 if (IS_ERR(bdev_file)) 1736 return bdev_file; 1737 1738 err = fs_bdev_register(bdev_file, sb); 1739 if (err) { 1740 bdev_fput(bdev_file); 1741 return ERR_PTR(err); 1742 } 1743 return bdev_file; 1744 } 1745 EXPORT_SYMBOL_GPL(fs_bdev_file_open_by_path); 1746 1747 /** 1748 * fs_bdev_unregister - drop a superblock's claim on a block device 1749 * @bdev_file: file returned by fs_bdev_file_open_by_{dev,path}() 1750 * @sb: superblock the device was claimed for 1751 * 1752 * The inverse of fs_bdev_register(): drop one claim on the {dev, @sb} entry 1753 * (the last claim unregisters it; a pinning cursor defers the actual unlink) 1754 * without closing the device. A caller that must act on the still-open device 1755 * between unregistering and closing - e.g. re-allow freezing one denied for a 1756 * membership change - pairs this with bdev_fput(). fs_bdev_file_release() is 1757 * the common unregister-and-close. 1758 */ 1759 void fs_bdev_unregister(struct file *bdev_file, struct super_block *sb) 1760 { 1761 dev_t dev = file_bdev(bdev_file)->bd_dev; 1762 struct super_dev *sb_dev; 1763 1764 rcu_read_lock(); 1765 sb_dev = super_dev_lookup(dev, sb); 1766 rcu_read_unlock(); 1767 super_dev_put(sb_dev); 1768 } 1769 EXPORT_SYMBOL_GPL(fs_bdev_unregister); 1770 1771 /** 1772 * fs_bdev_file_release - release a block device claimed for a superblock 1773 * @bdev_file: file returned by fs_bdev_file_open_by_{dev,path}() 1774 * @sb: superblock the device was claimed for 1775 * 1776 * Unregister the {dev, @sb} entry, then close the block device. 1777 */ 1778 void fs_bdev_file_release(struct file *bdev_file, struct super_block *sb) 1779 { 1780 fs_bdev_unregister(bdev_file, sb); 1781 bdev_fput(bdev_file); 1782 } 1783 EXPORT_SYMBOL_GPL(fs_bdev_file_release); 1784 1785 int setup_bdev_super(struct super_block *sb, int sb_flags, 1786 struct fs_context *fc) 1787 { 1788 blk_mode_t mode = sb_open_mode(sb_flags); 1789 struct file *bdev_file; 1790 struct block_device *bdev; 1791 1792 bdev_file = fs_bdev_file_open_by_dev(sb->s_dev, mode, sb, sb); 1793 if (IS_ERR(bdev_file)) { 1794 if (fc) 1795 errorf(fc, "%s: Can't open blockdev", fc->source); 1796 return PTR_ERR(bdev_file); 1797 } 1798 bdev = file_bdev(bdev_file); 1799 1800 /* 1801 * This really should be in blkdev_get_by_dev, but right now can't due 1802 * to legacy issues that require us to allow opening a block device node 1803 * writable from userspace even for a read-only block device. 1804 */ 1805 if ((mode & BLK_OPEN_WRITE) && bdev_read_only(bdev)) { 1806 fs_bdev_file_release(bdev_file, sb); 1807 return -EACCES; 1808 } 1809 1810 /* The sget_fc() entry is already published; pairs with bdev_freeze(). */ 1811 smp_mb(); 1812 if (atomic_read(&bdev->bd_fsfreeze_count) > 0) { 1813 if (fc) 1814 warnf(fc, "%pg: Can't mount, blockdev is frozen", bdev); 1815 fs_bdev_file_release(bdev_file, sb); 1816 return -EBUSY; 1817 } 1818 1819 spin_lock(&sb_lock); 1820 sb->s_bdev_file = bdev_file; 1821 sb->s_bdev = bdev; 1822 sb->s_bdi = bdi_get(bdev->bd_disk->bdi); 1823 if (bdev_stable_writes(bdev)) 1824 sb->s_iflags |= SB_I_STABLE_WRITES; 1825 spin_unlock(&sb_lock); 1826 1827 snprintf(sb->s_id, sizeof(sb->s_id), "%pg", bdev); 1828 shrinker_debugfs_rename(sb->s_shrink, "sb-%s:%s", sb->s_type->name, 1829 sb->s_id); 1830 sb_set_blocksize(sb, block_size(bdev)); 1831 return 0; 1832 } 1833 EXPORT_SYMBOL_GPL(setup_bdev_super); 1834 1835 /** 1836 * get_tree_bdev_flags - Get a superblock based on a single block device 1837 * @fc: The filesystem context holding the parameters 1838 * @fill_super: Helper to initialise a new superblock 1839 * @flags: GET_TREE_BDEV_* flags 1840 */ 1841 int get_tree_bdev_flags(struct fs_context *fc, 1842 int (*fill_super)(struct super_block *sb, 1843 struct fs_context *fc), unsigned int flags) 1844 { 1845 struct super_block *s; 1846 int error = 0; 1847 dev_t dev; 1848 1849 if (!fc->source) 1850 return invalf(fc, "No source specified"); 1851 1852 error = lookup_bdev(fc->source, &dev); 1853 if (error) { 1854 if (!(flags & GET_TREE_BDEV_QUIET_LOOKUP)) 1855 errorf(fc, "%s: Can't lookup blockdev", fc->source); 1856 return error; 1857 } 1858 fc->sb_flags |= SB_NOSEC; 1859 s = sget_dev(fc, dev); 1860 if (IS_ERR(s)) 1861 return PTR_ERR(s); 1862 1863 if (s->s_root) { 1864 /* Don't summarily change the RO/RW state. */ 1865 if ((fc->sb_flags ^ s->s_flags) & SB_RDONLY) { 1866 warnf(fc, "%pg: Can't mount, would change RO state", s->s_bdev); 1867 deactivate_locked_super(s); 1868 return -EBUSY; 1869 } 1870 } else { 1871 error = setup_bdev_super(s, fc->sb_flags, fc); 1872 if (!error) 1873 error = fill_super(s, fc); 1874 if (error) { 1875 deactivate_locked_super(s); 1876 return error; 1877 } 1878 s->s_flags |= SB_ACTIVE; 1879 } 1880 1881 BUG_ON(fc->root); 1882 fc->root = dget(s->s_root); 1883 return 0; 1884 } 1885 EXPORT_SYMBOL_GPL(get_tree_bdev_flags); 1886 1887 /** 1888 * get_tree_bdev - Get a superblock based on a single block device 1889 * @fc: The filesystem context holding the parameters 1890 * @fill_super: Helper to initialise a new superblock 1891 */ 1892 int get_tree_bdev(struct fs_context *fc, 1893 int (*fill_super)(struct super_block *, 1894 struct fs_context *)) 1895 { 1896 return get_tree_bdev_flags(fc, fill_super, 0); 1897 } 1898 EXPORT_SYMBOL(get_tree_bdev); 1899 1900 void kill_block_super(struct super_block *sb) 1901 { 1902 struct block_device *bdev = sb->s_bdev; 1903 1904 generic_shutdown_super(sb); 1905 if (bdev) { 1906 sync_blockdev(bdev); 1907 fs_bdev_file_release(sb->s_bdev_file, sb); 1908 } 1909 } 1910 1911 EXPORT_SYMBOL(kill_block_super); 1912 #endif 1913 1914 /** 1915 * vfs_get_tree - Get the mountable root 1916 * @fc: The superblock configuration context. 1917 * 1918 * The filesystem is invoked to get or create a superblock which can then later 1919 * be used for mounting. The filesystem places a pointer to the root to be 1920 * used for mounting in @fc->root. 1921 */ 1922 int vfs_get_tree(struct fs_context *fc) 1923 { 1924 struct super_block *sb; 1925 int error; 1926 1927 if (fc->root) 1928 return -EBUSY; 1929 1930 /* Get the mountable root in fc->root, with a ref on the root and a ref 1931 * on the superblock. 1932 */ 1933 error = fc->ops->get_tree(fc); 1934 if (error < 0) 1935 return error; 1936 1937 if (!fc->root) { 1938 pr_err("Filesystem %s get_tree() didn't set fc->root, returned %i\n", 1939 fc->fs_type->name, error); 1940 /* We don't know what the locking state of the superblock is - 1941 * if there is a superblock. 1942 */ 1943 BUG(); 1944 } 1945 1946 sb = fc->root->d_sb; 1947 WARN_ON(!sb->s_bdi); 1948 1949 /* 1950 * super_wake() contains a memory barrier which also care of 1951 * ordering for super_cache_count(). We place it before setting 1952 * SB_BORN as the data dependency between the two functions is 1953 * the superblock structure contents that we just set up, not 1954 * the SB_BORN flag. 1955 */ 1956 super_wake(sb, SB_BORN); 1957 1958 error = security_sb_set_mnt_opts(sb, fc->security, 0, NULL); 1959 if (unlikely(error)) { 1960 fc_drop_locked(fc); 1961 return error; 1962 } 1963 1964 /* 1965 * filesystems should never set s_maxbytes larger than MAX_LFS_FILESIZE 1966 * but s_maxbytes was an unsigned long long for many releases. Throw 1967 * this warning for a little while to try and catch filesystems that 1968 * violate this rule. 1969 */ 1970 WARN((sb->s_maxbytes < 0), "%s set sb->s_maxbytes to " 1971 "negative value (%lld)\n", fc->fs_type->name, sb->s_maxbytes); 1972 1973 return 0; 1974 } 1975 EXPORT_SYMBOL(vfs_get_tree); 1976 1977 /* 1978 * Setup private BDI for given superblock. It gets automatically cleaned up 1979 * in generic_shutdown_super(). 1980 */ 1981 int super_setup_bdi_name(struct super_block *sb, char *fmt, ...) 1982 { 1983 struct backing_dev_info *bdi; 1984 int err; 1985 va_list args; 1986 1987 bdi = bdi_alloc(NUMA_NO_NODE); 1988 if (!bdi) 1989 return -ENOMEM; 1990 1991 va_start(args, fmt); 1992 err = bdi_register_va(bdi, fmt, args); 1993 va_end(args); 1994 if (err) { 1995 bdi_put(bdi); 1996 return err; 1997 } 1998 WARN_ON(sb->s_bdi != &noop_backing_dev_info); 1999 sb->s_bdi = bdi; 2000 sb->s_iflags |= SB_I_PERSB_BDI; 2001 2002 return 0; 2003 } 2004 EXPORT_SYMBOL(super_setup_bdi_name); 2005 2006 /* 2007 * Setup private BDI for given superblock. I gets automatically cleaned up 2008 * in generic_shutdown_super(). 2009 */ 2010 int super_setup_bdi(struct super_block *sb) 2011 { 2012 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0); 2013 2014 return super_setup_bdi_name(sb, "%.28s-%ld", sb->s_type->name, 2015 atomic_long_inc_return(&bdi_seq)); 2016 } 2017 EXPORT_SYMBOL(super_setup_bdi); 2018 2019 /** 2020 * sb_wait_write - wait until all writers to given file system finish 2021 * @sb: the super for which we wait 2022 * @level: type of writers we wait for (normal vs page fault) 2023 * 2024 * This function waits until there are no writers of given type to given file 2025 * system. 2026 */ 2027 static void sb_wait_write(struct super_block *sb, int level) 2028 { 2029 percpu_down_write(sb->s_writers.rw_sem + level-1); 2030 } 2031 2032 /* 2033 * We are going to return to userspace and forget about these locks, the 2034 * ownership goes to the caller of thaw_super() which does unlock(). 2035 */ 2036 static void lockdep_sb_freeze_release(struct super_block *sb) 2037 { 2038 int level; 2039 2040 for (level = SB_FREEZE_LEVELS - 1; level >= 0; level--) 2041 percpu_rwsem_release(sb->s_writers.rw_sem + level, _THIS_IP_); 2042 } 2043 2044 /* 2045 * Tell lockdep we are holding these locks before we call ->unfreeze_fs(sb). 2046 */ 2047 static void lockdep_sb_freeze_acquire(struct super_block *sb) 2048 { 2049 int level; 2050 2051 for (level = 0; level < SB_FREEZE_LEVELS; ++level) 2052 percpu_rwsem_acquire(sb->s_writers.rw_sem + level, 0, _THIS_IP_); 2053 } 2054 2055 static void sb_freeze_unlock(struct super_block *sb, int level) 2056 { 2057 for (level--; level >= 0; level--) 2058 percpu_up_write(sb->s_writers.rw_sem + level); 2059 } 2060 2061 static int wait_for_partially_frozen(struct super_block *sb) 2062 { 2063 int ret = 0; 2064 2065 do { 2066 unsigned short old = sb->s_writers.frozen; 2067 2068 up_write(&sb->s_umount); 2069 ret = wait_var_event_killable(&sb->s_writers.frozen, 2070 sb->s_writers.frozen != old); 2071 down_write(&sb->s_umount); 2072 } while (ret == 0 && 2073 sb->s_writers.frozen != SB_UNFROZEN && 2074 sb->s_writers.frozen != SB_FREEZE_COMPLETE); 2075 2076 return ret; 2077 } 2078 2079 #define FREEZE_HOLDERS (FREEZE_HOLDER_KERNEL | FREEZE_HOLDER_USERSPACE) 2080 #define FREEZE_FLAGS (FREEZE_HOLDERS | FREEZE_MAY_NEST | FREEZE_EXCL) 2081 2082 static inline int freeze_inc(struct super_block *sb, enum freeze_holder who) 2083 { 2084 WARN_ON_ONCE((who & ~FREEZE_FLAGS)); 2085 WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1); 2086 2087 if (who & FREEZE_HOLDER_KERNEL) 2088 ++sb->s_writers.freeze_kcount; 2089 if (who & FREEZE_HOLDER_USERSPACE) 2090 ++sb->s_writers.freeze_ucount; 2091 return sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount; 2092 } 2093 2094 static inline int freeze_dec(struct super_block *sb, enum freeze_holder who) 2095 { 2096 WARN_ON_ONCE((who & ~FREEZE_FLAGS)); 2097 WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1); 2098 2099 if ((who & FREEZE_HOLDER_KERNEL) && sb->s_writers.freeze_kcount) 2100 --sb->s_writers.freeze_kcount; 2101 if ((who & FREEZE_HOLDER_USERSPACE) && sb->s_writers.freeze_ucount) 2102 --sb->s_writers.freeze_ucount; 2103 return sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount; 2104 } 2105 2106 static inline bool may_freeze(struct super_block *sb, enum freeze_holder who, 2107 const void *freeze_owner) 2108 { 2109 lockdep_assert_held(&sb->s_umount); 2110 2111 WARN_ON_ONCE((who & ~FREEZE_FLAGS)); 2112 WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1); 2113 2114 if (who & FREEZE_EXCL) { 2115 if (WARN_ON_ONCE(!(who & FREEZE_HOLDER_KERNEL))) 2116 return false; 2117 if (WARN_ON_ONCE(who & ~(FREEZE_EXCL | FREEZE_HOLDER_KERNEL))) 2118 return false; 2119 if (WARN_ON_ONCE(!freeze_owner)) 2120 return false; 2121 /* This freeze already has a specific owner. */ 2122 if (sb->s_writers.freeze_owner) 2123 return false; 2124 /* 2125 * This is already frozen multiple times so we're just 2126 * going to take a reference count and mark the freeze as 2127 * being owned by the caller. 2128 */ 2129 if (sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount) 2130 sb->s_writers.freeze_owner = freeze_owner; 2131 return true; 2132 } 2133 2134 if (who & FREEZE_HOLDER_KERNEL) 2135 return (who & FREEZE_MAY_NEST) || 2136 sb->s_writers.freeze_kcount == 0; 2137 if (who & FREEZE_HOLDER_USERSPACE) 2138 return (who & FREEZE_MAY_NEST) || 2139 sb->s_writers.freeze_ucount == 0; 2140 return false; 2141 } 2142 2143 static inline bool may_unfreeze(struct super_block *sb, enum freeze_holder who, 2144 const void *freeze_owner) 2145 { 2146 lockdep_assert_held(&sb->s_umount); 2147 2148 WARN_ON_ONCE((who & ~FREEZE_FLAGS)); 2149 WARN_ON_ONCE(hweight32(who & FREEZE_HOLDERS) > 1); 2150 2151 if (who & FREEZE_EXCL) { 2152 if (WARN_ON_ONCE(!(who & FREEZE_HOLDER_KERNEL))) 2153 return false; 2154 if (WARN_ON_ONCE(who & ~(FREEZE_EXCL | FREEZE_HOLDER_KERNEL))) 2155 return false; 2156 if (WARN_ON_ONCE(!freeze_owner)) 2157 return false; 2158 if (WARN_ON_ONCE(sb->s_writers.freeze_kcount == 0)) 2159 return false; 2160 /* This isn't exclusively frozen. */ 2161 if (!sb->s_writers.freeze_owner) 2162 return false; 2163 /* This isn't exclusively frozen by us. */ 2164 if (sb->s_writers.freeze_owner != freeze_owner) 2165 return false; 2166 /* 2167 * This is still frozen multiple times so we're just 2168 * going to drop our reference count and undo our 2169 * exclusive freeze. 2170 */ 2171 if ((sb->s_writers.freeze_kcount + sb->s_writers.freeze_ucount) > 1) 2172 sb->s_writers.freeze_owner = NULL; 2173 return true; 2174 } 2175 2176 if (who & FREEZE_HOLDER_KERNEL) { 2177 /* 2178 * Someone's trying to steal the reference belonging to 2179 * @sb->s_writers.freeze_owner. 2180 */ 2181 if (sb->s_writers.freeze_kcount == 1 && 2182 sb->s_writers.freeze_owner) 2183 return false; 2184 return sb->s_writers.freeze_kcount > 0; 2185 } 2186 2187 if (who & FREEZE_HOLDER_USERSPACE) 2188 return sb->s_writers.freeze_ucount > 0; 2189 2190 return false; 2191 } 2192 2193 /** 2194 * freeze_super - lock the filesystem and force it into a consistent state 2195 * @sb: the super to lock 2196 * @who: context that wants to freeze 2197 * @freeze_owner: owner of the freeze 2198 * 2199 * Syncs the super to make sure the filesystem is consistent and calls the fs's 2200 * freeze_fs. Subsequent calls to this without first thawing the fs may return 2201 * -EBUSY. 2202 * 2203 * @who should be: 2204 * * %FREEZE_HOLDER_USERSPACE if userspace wants to freeze the fs; 2205 * * %FREEZE_HOLDER_KERNEL if the kernel wants to freeze the fs. 2206 * * %FREEZE_MAY_NEST whether nesting freeze and thaw requests is allowed. 2207 * 2208 * The @who argument distinguishes between the kernel and userspace trying to 2209 * freeze the filesystem. Although there cannot be multiple kernel freezes or 2210 * multiple userspace freezes in effect at any given time, the kernel and 2211 * userspace can both hold a filesystem frozen. The filesystem remains frozen 2212 * until there are no kernel or userspace freezes in effect. 2213 * 2214 * A filesystem may hold multiple devices and thus a filesystems may be 2215 * frozen through the block layer via multiple block devices. In this 2216 * case the request is marked as being allowed to nest by passing 2217 * FREEZE_MAY_NEST. The filesystem remains frozen until all block 2218 * devices are unfrozen. If multiple freezes are attempted without 2219 * FREEZE_MAY_NEST -EBUSY will be returned. 2220 * 2221 * During this function, sb->s_writers.frozen goes through these values: 2222 * 2223 * SB_UNFROZEN: File system is normal, all writes progress as usual. 2224 * 2225 * SB_FREEZE_WRITE: The file system is in the process of being frozen. New 2226 * writes should be blocked, though page faults are still allowed. We wait for 2227 * all writes to complete and then proceed to the next stage. 2228 * 2229 * SB_FREEZE_PAGEFAULT: Freezing continues. Now also page faults are blocked 2230 * but internal fs threads can still modify the filesystem (although they 2231 * should not dirty new pages or inodes), writeback can run etc. After waiting 2232 * for all running page faults we sync the filesystem which will clean all 2233 * dirty pages and inodes (no new dirty pages or inodes can be created when 2234 * sync is running). 2235 * 2236 * SB_FREEZE_FS: The file system is frozen. Now all internal sources of fs 2237 * modification are blocked (e.g. XFS preallocation truncation on inode 2238 * reclaim). This is usually implemented by blocking new transactions for 2239 * filesystems that have them and need this additional guard. After all 2240 * internal writers are finished we call ->freeze_fs() to finish filesystem 2241 * freezing. Then we transition to SB_FREEZE_COMPLETE state. This state is 2242 * mostly auxiliary for filesystems to verify they do not modify frozen fs. 2243 * 2244 * sb->s_writers.frozen is protected by sb->s_umount. 2245 * 2246 * Return: If the freeze was successful zero is returned. If the freeze 2247 * failed a negative error code is returned. 2248 */ 2249 int freeze_super(struct super_block *sb, enum freeze_holder who, const void *freeze_owner) 2250 { 2251 int ret; 2252 2253 if (!super_lock_excl(sb)) { 2254 WARN_ONCE(1, "Dying superblock while freezing!"); 2255 return -EINVAL; 2256 } 2257 atomic_inc(&sb->s_active); 2258 2259 retry: 2260 if (sb->s_writers.frozen == SB_FREEZE_COMPLETE) { 2261 if (may_freeze(sb, who, freeze_owner)) 2262 ret = !!WARN_ON_ONCE(freeze_inc(sb, who) == 1); 2263 else 2264 ret = -EBUSY; 2265 /* All freezers share a single active reference. */ 2266 deactivate_locked_super(sb); 2267 return ret; 2268 } 2269 2270 if (sb->s_writers.frozen != SB_UNFROZEN) { 2271 ret = wait_for_partially_frozen(sb); 2272 if (ret) { 2273 deactivate_locked_super(sb); 2274 return ret; 2275 } 2276 2277 goto retry; 2278 } 2279 2280 if (sb_rdonly(sb)) { 2281 /* Nothing to do really... */ 2282 WARN_ON_ONCE(freeze_inc(sb, who) > 1); 2283 sb->s_writers.freeze_owner = freeze_owner; 2284 sb->s_writers.frozen = SB_FREEZE_COMPLETE; 2285 wake_up_var(&sb->s_writers.frozen); 2286 super_unlock_excl(sb); 2287 return 0; 2288 } 2289 2290 sb->s_writers.frozen = SB_FREEZE_WRITE; 2291 /* Release s_umount to preserve sb_start_write -> s_umount ordering */ 2292 super_unlock_excl(sb); 2293 sb_wait_write(sb, SB_FREEZE_WRITE); 2294 __super_lock_excl(sb); 2295 2296 /* Now we go and block page faults... */ 2297 sb->s_writers.frozen = SB_FREEZE_PAGEFAULT; 2298 sb_wait_write(sb, SB_FREEZE_PAGEFAULT); 2299 2300 /* All writers are done so after syncing there won't be dirty data */ 2301 ret = sync_filesystem(sb); 2302 if (ret) { 2303 sb->s_writers.frozen = SB_UNFROZEN; 2304 sb_freeze_unlock(sb, SB_FREEZE_PAGEFAULT); 2305 wake_up_var(&sb->s_writers.frozen); 2306 deactivate_locked_super(sb); 2307 return ret; 2308 } 2309 2310 /* Now wait for internal filesystem counter */ 2311 sb->s_writers.frozen = SB_FREEZE_FS; 2312 sb_wait_write(sb, SB_FREEZE_FS); 2313 2314 if (sb->s_op->freeze_fs) { 2315 ret = sb->s_op->freeze_fs(sb); 2316 if (ret) { 2317 printk(KERN_ERR 2318 "VFS:Filesystem freeze failed\n"); 2319 sb->s_writers.frozen = SB_UNFROZEN; 2320 sb_freeze_unlock(sb, SB_FREEZE_FS); 2321 wake_up_var(&sb->s_writers.frozen); 2322 deactivate_locked_super(sb); 2323 return ret; 2324 } 2325 } 2326 /* 2327 * For debugging purposes so that fs can warn if it sees write activity 2328 * when frozen is set to SB_FREEZE_COMPLETE, and for thaw_super(). 2329 */ 2330 WARN_ON_ONCE(freeze_inc(sb, who) > 1); 2331 sb->s_writers.freeze_owner = freeze_owner; 2332 sb->s_writers.frozen = SB_FREEZE_COMPLETE; 2333 wake_up_var(&sb->s_writers.frozen); 2334 lockdep_sb_freeze_release(sb); 2335 super_unlock_excl(sb); 2336 return 0; 2337 } 2338 EXPORT_SYMBOL(freeze_super); 2339 2340 /* 2341 * Undoes the effect of a freeze_super_locked call. If the filesystem is 2342 * frozen both by userspace and the kernel, a thaw call from either source 2343 * removes that state without releasing the other state or unlocking the 2344 * filesystem. 2345 */ 2346 static int thaw_super_locked(struct super_block *sb, enum freeze_holder who, 2347 const void *freeze_owner) 2348 { 2349 int error = -EINVAL; 2350 2351 if (sb->s_writers.frozen != SB_FREEZE_COMPLETE) 2352 goto out_unlock; 2353 2354 if (!may_unfreeze(sb, who, freeze_owner)) 2355 goto out_unlock; 2356 2357 /* 2358 * All freezers share a single active reference. If other freezers 2359 * remain, drop our hold and report success; the superblock stays 2360 * frozen until the last holder thaws it. 2361 */ 2362 if (freeze_dec(sb, who)) { 2363 error = 0; 2364 goto out_unlock; 2365 } 2366 2367 if (sb_rdonly(sb)) { 2368 sb->s_writers.frozen = SB_UNFROZEN; 2369 sb->s_writers.freeze_owner = NULL; 2370 wake_up_var(&sb->s_writers.frozen); 2371 goto out_deactivate; 2372 } 2373 2374 lockdep_sb_freeze_acquire(sb); 2375 2376 if (sb->s_op->unfreeze_fs) { 2377 error = sb->s_op->unfreeze_fs(sb); 2378 if (error) { 2379 pr_err("VFS: Filesystem thaw failed\n"); 2380 freeze_inc(sb, who); 2381 lockdep_sb_freeze_release(sb); 2382 goto out_unlock; 2383 } 2384 } 2385 2386 sb->s_writers.frozen = SB_UNFROZEN; 2387 sb->s_writers.freeze_owner = NULL; 2388 wake_up_var(&sb->s_writers.frozen); 2389 sb_freeze_unlock(sb, SB_FREEZE_FS); 2390 out_deactivate: 2391 deactivate_locked_super(sb); 2392 return 0; 2393 2394 out_unlock: 2395 super_unlock_excl(sb); 2396 return error; 2397 } 2398 2399 /** 2400 * thaw_super -- unlock filesystem 2401 * @sb: the super to thaw 2402 * @who: context that wants to freeze 2403 * @freeze_owner: owner of the freeze 2404 * 2405 * Unlocks the filesystem and marks it writeable again after freeze_super() 2406 * if there are no remaining freezes on the filesystem. 2407 * 2408 * @who should be: 2409 * * %FREEZE_HOLDER_USERSPACE if userspace wants to thaw the fs; 2410 * * %FREEZE_HOLDER_KERNEL if the kernel wants to thaw the fs. 2411 * * %FREEZE_MAY_NEST whether nesting freeze and thaw requests is allowed 2412 * 2413 * A filesystem may hold multiple devices and thus a filesystems may 2414 * have been frozen through the block layer via multiple block devices. 2415 * The filesystem remains frozen until all block devices are unfrozen. 2416 */ 2417 int thaw_super(struct super_block *sb, enum freeze_holder who, 2418 const void *freeze_owner) 2419 { 2420 if (!super_lock_excl(sb)) { 2421 WARN_ONCE(1, "Dying superblock while thawing!"); 2422 return -EINVAL; 2423 } 2424 return thaw_super_locked(sb, who, freeze_owner); 2425 } 2426 EXPORT_SYMBOL(thaw_super); 2427 2428 /* 2429 * Create workqueue for deferred direct IO completions. We allocate the 2430 * workqueue when it's first needed. This avoids creating workqueue for 2431 * filesystems that don't need it and also allows us to create the workqueue 2432 * late enough so the we can include s_id in the name of the workqueue. 2433 */ 2434 int sb_init_dio_done_wq(struct super_block *sb) 2435 { 2436 struct workqueue_struct *old; 2437 struct workqueue_struct *wq = alloc_workqueue("dio/%s", 2438 WQ_MEM_RECLAIM | WQ_PERCPU, 2439 0, 2440 sb->s_id); 2441 if (!wq) 2442 return -ENOMEM; 2443 2444 old = NULL; 2445 /* 2446 * This has to be atomic as more DIOs can race to create the workqueue 2447 */ 2448 if (!try_cmpxchg(&sb->s_dio_done_wq, &old, wq)) { 2449 /* Someone created workqueue before us? Free ours... */ 2450 destroy_workqueue(wq); 2451 } 2452 return 0; 2453 } 2454 EXPORT_SYMBOL_GPL(sb_init_dio_done_wq); 2455