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