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