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