1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 5 */ 6 #include "xfs.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_bit.h" 13 #include "xfs_sb.h" 14 #include "xfs_mount.h" 15 #include "xfs_inode.h" 16 #include "xfs_dir2.h" 17 #include "xfs_ialloc.h" 18 #include "xfs_alloc.h" 19 #include "xfs_rtalloc.h" 20 #include "xfs_bmap.h" 21 #include "xfs_trans.h" 22 #include "xfs_trans_priv.h" 23 #include "xfs_log.h" 24 #include "xfs_log_priv.h" 25 #include "xfs_error.h" 26 #include "xfs_quota.h" 27 #include "xfs_fsops.h" 28 #include "xfs_icache.h" 29 #include "xfs_sysfs.h" 30 #include "xfs_rmap_btree.h" 31 #include "xfs_refcount_btree.h" 32 #include "xfs_reflink.h" 33 #include "xfs_extent_busy.h" 34 #include "xfs_health.h" 35 #include "xfs_trace.h" 36 #include "xfs_ag.h" 37 #include "xfs_rtbitmap.h" 38 #include "xfs_metafile.h" 39 #include "xfs_rtgroup.h" 40 #include "xfs_rtrmap_btree.h" 41 #include "xfs_rtrefcount_btree.h" 42 #include "scrub/stats.h" 43 44 static DEFINE_MUTEX(xfs_uuid_table_mutex); 45 static int xfs_uuid_table_size; 46 static uuid_t *xfs_uuid_table; 47 48 void 49 xfs_uuid_table_free(void) 50 { 51 if (xfs_uuid_table_size == 0) 52 return; 53 kfree(xfs_uuid_table); 54 xfs_uuid_table = NULL; 55 xfs_uuid_table_size = 0; 56 } 57 58 /* 59 * See if the UUID is unique among mounted XFS filesystems. 60 * Mount fails if UUID is nil or a FS with the same UUID is already mounted. 61 */ 62 STATIC int 63 xfs_uuid_mount( 64 struct xfs_mount *mp) 65 { 66 uuid_t *uuid = &mp->m_sb.sb_uuid; 67 int hole, i; 68 69 /* Publish UUID in struct super_block */ 70 super_set_uuid(mp->m_super, uuid->b, sizeof(*uuid)); 71 72 if (xfs_has_nouuid(mp)) 73 return 0; 74 75 if (uuid_is_null(uuid)) { 76 xfs_warn(mp, "Filesystem has null UUID - can't mount"); 77 return -EINVAL; 78 } 79 80 mutex_lock(&xfs_uuid_table_mutex); 81 for (i = 0, hole = -1; i < xfs_uuid_table_size; i++) { 82 if (uuid_is_null(&xfs_uuid_table[i])) { 83 hole = i; 84 continue; 85 } 86 if (uuid_equal(uuid, &xfs_uuid_table[i])) 87 goto out_duplicate; 88 } 89 90 if (hole < 0) { 91 xfs_uuid_table = krealloc(xfs_uuid_table, 92 (xfs_uuid_table_size + 1) * sizeof(*xfs_uuid_table), 93 GFP_KERNEL | __GFP_NOFAIL); 94 hole = xfs_uuid_table_size++; 95 } 96 xfs_uuid_table[hole] = *uuid; 97 mutex_unlock(&xfs_uuid_table_mutex); 98 99 return 0; 100 101 out_duplicate: 102 mutex_unlock(&xfs_uuid_table_mutex); 103 xfs_warn(mp, "Filesystem has duplicate UUID %pU - can't mount", uuid); 104 return -EINVAL; 105 } 106 107 STATIC void 108 xfs_uuid_unmount( 109 struct xfs_mount *mp) 110 { 111 uuid_t *uuid = &mp->m_sb.sb_uuid; 112 int i; 113 114 if (xfs_has_nouuid(mp)) 115 return; 116 117 mutex_lock(&xfs_uuid_table_mutex); 118 for (i = 0; i < xfs_uuid_table_size; i++) { 119 if (uuid_is_null(&xfs_uuid_table[i])) 120 continue; 121 if (!uuid_equal(uuid, &xfs_uuid_table[i])) 122 continue; 123 memset(&xfs_uuid_table[i], 0, sizeof(uuid_t)); 124 break; 125 } 126 ASSERT(i < xfs_uuid_table_size); 127 mutex_unlock(&xfs_uuid_table_mutex); 128 } 129 130 /* 131 * Check size of device based on the (data/realtime) block count. 132 * Note: this check is used by the growfs code as well as mount. 133 */ 134 int 135 xfs_sb_validate_fsb_count( 136 xfs_sb_t *sbp, 137 uint64_t nblocks) 138 { 139 uint64_t max_bytes; 140 141 ASSERT(sbp->sb_blocklog >= BBSHIFT); 142 143 if (check_shl_overflow(nblocks, sbp->sb_blocklog, &max_bytes)) 144 return -EFBIG; 145 146 /* Limited by ULONG_MAX of page cache index */ 147 if (max_bytes >> PAGE_SHIFT > ULONG_MAX) 148 return -EFBIG; 149 return 0; 150 } 151 152 /* 153 * xfs_readsb 154 * 155 * Does the initial read of the superblock. 156 */ 157 int 158 xfs_readsb( 159 struct xfs_mount *mp, 160 int flags) 161 { 162 unsigned int sector_size; 163 struct xfs_buf *bp; 164 struct xfs_sb *sbp = &mp->m_sb; 165 int error; 166 int loud = !(flags & XFS_MFSI_QUIET); 167 const struct xfs_buf_ops *buf_ops; 168 169 ASSERT(mp->m_sb_bp == NULL); 170 ASSERT(mp->m_ddev_targp != NULL); 171 172 /* 173 * For the initial read, we must guess at the sector 174 * size based on the block device. It's enough to 175 * get the sb_sectsize out of the superblock and 176 * then reread with the proper length. 177 * We don't verify it yet, because it may not be complete. 178 */ 179 sector_size = xfs_getsize_buftarg(mp->m_ddev_targp); 180 buf_ops = NULL; 181 182 /* 183 * Allocate a (locked) buffer to hold the superblock. This will be kept 184 * around at all times to optimize access to the superblock. Therefore, 185 * set XBF_NO_IOACCT to make sure it doesn't hold the buftarg count 186 * elevated. 187 */ 188 reread: 189 error = xfs_buf_read_uncached(mp->m_ddev_targp, XFS_SB_DADDR, 190 BTOBB(sector_size), XBF_NO_IOACCT, &bp, 191 buf_ops); 192 if (error) { 193 if (loud) 194 xfs_warn(mp, "SB validate failed with error %d.", error); 195 /* bad CRC means corrupted metadata */ 196 if (error == -EFSBADCRC) 197 error = -EFSCORRUPTED; 198 return error; 199 } 200 201 /* 202 * Initialize the mount structure from the superblock. 203 */ 204 xfs_sb_from_disk(sbp, bp->b_addr); 205 206 /* 207 * If we haven't validated the superblock, do so now before we try 208 * to check the sector size and reread the superblock appropriately. 209 */ 210 if (sbp->sb_magicnum != XFS_SB_MAGIC) { 211 if (loud) 212 xfs_warn(mp, "Invalid superblock magic number"); 213 error = -EINVAL; 214 goto release_buf; 215 } 216 217 /* 218 * We must be able to do sector-sized and sector-aligned IO. 219 */ 220 if (sector_size > sbp->sb_sectsize) { 221 if (loud) 222 xfs_warn(mp, "device supports %u byte sectors (not %u)", 223 sector_size, sbp->sb_sectsize); 224 error = -ENOSYS; 225 goto release_buf; 226 } 227 228 if (buf_ops == NULL) { 229 /* 230 * Re-read the superblock so the buffer is correctly sized, 231 * and properly verified. 232 */ 233 xfs_buf_relse(bp); 234 sector_size = sbp->sb_sectsize; 235 buf_ops = loud ? &xfs_sb_buf_ops : &xfs_sb_quiet_buf_ops; 236 goto reread; 237 } 238 239 mp->m_features |= xfs_sb_version_to_features(sbp); 240 xfs_reinit_percpu_counters(mp); 241 242 /* 243 * If logged xattrs are enabled after log recovery finishes, then set 244 * the opstate so that log recovery will work properly. 245 */ 246 if (xfs_sb_version_haslogxattrs(&mp->m_sb)) 247 xfs_set_using_logged_xattrs(mp); 248 249 /* no need to be quiet anymore, so reset the buf ops */ 250 bp->b_ops = &xfs_sb_buf_ops; 251 252 mp->m_sb_bp = bp; 253 xfs_buf_unlock(bp); 254 return 0; 255 256 release_buf: 257 xfs_buf_relse(bp); 258 return error; 259 } 260 261 /* 262 * If the sunit/swidth change would move the precomputed root inode value, we 263 * must reject the ondisk change because repair will stumble over that. 264 * However, we allow the mount to proceed because we never rejected this 265 * combination before. Returns true to update the sb, false otherwise. 266 */ 267 static inline int 268 xfs_check_new_dalign( 269 struct xfs_mount *mp, 270 int new_dalign, 271 bool *update_sb) 272 { 273 struct xfs_sb *sbp = &mp->m_sb; 274 xfs_ino_t calc_ino; 275 276 calc_ino = xfs_ialloc_calc_rootino(mp, new_dalign); 277 trace_xfs_check_new_dalign(mp, new_dalign, calc_ino); 278 279 if (sbp->sb_rootino == calc_ino) { 280 *update_sb = true; 281 return 0; 282 } 283 284 xfs_warn(mp, 285 "Cannot change stripe alignment; would require moving root inode."); 286 287 /* 288 * XXX: Next time we add a new incompat feature, this should start 289 * returning -EINVAL to fail the mount. Until then, spit out a warning 290 * that we're ignoring the administrator's instructions. 291 */ 292 xfs_warn(mp, "Skipping superblock stripe alignment update."); 293 *update_sb = false; 294 return 0; 295 } 296 297 /* 298 * If we were provided with new sunit/swidth values as mount options, make sure 299 * that they pass basic alignment and superblock feature checks, and convert 300 * them into the same units (FSB) that everything else expects. This step 301 * /must/ be done before computing the inode geometry. 302 */ 303 STATIC int 304 xfs_validate_new_dalign( 305 struct xfs_mount *mp) 306 { 307 if (mp->m_dalign == 0) 308 return 0; 309 310 /* 311 * If stripe unit and stripe width are not multiples 312 * of the fs blocksize turn off alignment. 313 */ 314 if ((BBTOB(mp->m_dalign) & mp->m_blockmask) || 315 (BBTOB(mp->m_swidth) & mp->m_blockmask)) { 316 xfs_warn(mp, 317 "alignment check failed: sunit/swidth vs. blocksize(%d)", 318 mp->m_sb.sb_blocksize); 319 return -EINVAL; 320 } 321 322 /* 323 * Convert the stripe unit and width to FSBs. 324 */ 325 mp->m_dalign = XFS_BB_TO_FSBT(mp, mp->m_dalign); 326 if (mp->m_dalign && (mp->m_sb.sb_agblocks % mp->m_dalign)) { 327 xfs_warn(mp, 328 "alignment check failed: sunit/swidth vs. agsize(%d)", 329 mp->m_sb.sb_agblocks); 330 return -EINVAL; 331 } 332 333 if (!mp->m_dalign) { 334 xfs_warn(mp, 335 "alignment check failed: sunit(%d) less than bsize(%d)", 336 mp->m_dalign, mp->m_sb.sb_blocksize); 337 return -EINVAL; 338 } 339 340 mp->m_swidth = XFS_BB_TO_FSBT(mp, mp->m_swidth); 341 342 if (!xfs_has_dalign(mp)) { 343 xfs_warn(mp, 344 "cannot change alignment: superblock does not support data alignment"); 345 return -EINVAL; 346 } 347 348 return 0; 349 } 350 351 /* Update alignment values based on mount options and sb values. */ 352 STATIC int 353 xfs_update_alignment( 354 struct xfs_mount *mp) 355 { 356 struct xfs_sb *sbp = &mp->m_sb; 357 358 if (mp->m_dalign) { 359 bool update_sb; 360 int error; 361 362 if (sbp->sb_unit == mp->m_dalign && 363 sbp->sb_width == mp->m_swidth) 364 return 0; 365 366 error = xfs_check_new_dalign(mp, mp->m_dalign, &update_sb); 367 if (error || !update_sb) 368 return error; 369 370 sbp->sb_unit = mp->m_dalign; 371 sbp->sb_width = mp->m_swidth; 372 mp->m_update_sb = true; 373 } else if (!xfs_has_noalign(mp) && xfs_has_dalign(mp)) { 374 mp->m_dalign = sbp->sb_unit; 375 mp->m_swidth = sbp->sb_width; 376 } 377 378 return 0; 379 } 380 381 /* 382 * precalculate the low space thresholds for dynamic speculative preallocation. 383 */ 384 void 385 xfs_set_low_space_thresholds( 386 struct xfs_mount *mp) 387 { 388 uint64_t dblocks = mp->m_sb.sb_dblocks; 389 uint64_t rtexts = mp->m_sb.sb_rextents; 390 int i; 391 392 do_div(dblocks, 100); 393 do_div(rtexts, 100); 394 395 for (i = 0; i < XFS_LOWSP_MAX; i++) { 396 mp->m_low_space[i] = dblocks * (i + 1); 397 mp->m_low_rtexts[i] = rtexts * (i + 1); 398 } 399 } 400 401 /* 402 * Check that the data (and log if separate) is an ok size. 403 */ 404 STATIC int 405 xfs_check_sizes( 406 struct xfs_mount *mp) 407 { 408 struct xfs_buf *bp; 409 xfs_daddr_t d; 410 int error; 411 412 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks); 413 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_dblocks) { 414 xfs_warn(mp, "filesystem size mismatch detected"); 415 return -EFBIG; 416 } 417 error = xfs_buf_read_uncached(mp->m_ddev_targp, 418 d - XFS_FSS_TO_BB(mp, 1), 419 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); 420 if (error) { 421 xfs_warn(mp, "last sector read failed"); 422 return error; 423 } 424 xfs_buf_relse(bp); 425 426 if (mp->m_logdev_targp == mp->m_ddev_targp) 427 return 0; 428 429 d = (xfs_daddr_t)XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks); 430 if (XFS_BB_TO_FSB(mp, d) != mp->m_sb.sb_logblocks) { 431 xfs_warn(mp, "log size mismatch detected"); 432 return -EFBIG; 433 } 434 error = xfs_buf_read_uncached(mp->m_logdev_targp, 435 d - XFS_FSB_TO_BB(mp, 1), 436 XFS_FSB_TO_BB(mp, 1), 0, &bp, NULL); 437 if (error) { 438 xfs_warn(mp, "log device read failed"); 439 return error; 440 } 441 xfs_buf_relse(bp); 442 return 0; 443 } 444 445 /* 446 * Clear the quotaflags in memory and in the superblock. 447 */ 448 int 449 xfs_mount_reset_sbqflags( 450 struct xfs_mount *mp) 451 { 452 mp->m_qflags = 0; 453 454 /* It is OK to look at sb_qflags in the mount path without m_sb_lock. */ 455 if (mp->m_sb.sb_qflags == 0) 456 return 0; 457 spin_lock(&mp->m_sb_lock); 458 mp->m_sb.sb_qflags = 0; 459 spin_unlock(&mp->m_sb_lock); 460 461 if (!xfs_fs_writable(mp, SB_FREEZE_WRITE)) 462 return 0; 463 464 return xfs_sync_sb(mp, false); 465 } 466 467 uint64_t 468 xfs_default_resblks(xfs_mount_t *mp) 469 { 470 uint64_t resblks; 471 472 /* 473 * We default to 5% or 8192 fsbs of space reserved, whichever is 474 * smaller. This is intended to cover concurrent allocation 475 * transactions when we initially hit enospc. These each require a 4 476 * block reservation. Hence by default we cover roughly 2000 concurrent 477 * allocation reservations. 478 */ 479 resblks = mp->m_sb.sb_dblocks; 480 do_div(resblks, 20); 481 resblks = min_t(uint64_t, resblks, 8192); 482 return resblks; 483 } 484 485 /* Ensure the summary counts are correct. */ 486 STATIC int 487 xfs_check_summary_counts( 488 struct xfs_mount *mp) 489 { 490 int error = 0; 491 492 /* 493 * The AG0 superblock verifier rejects in-progress filesystems, 494 * so we should never see the flag set this far into mounting. 495 */ 496 if (mp->m_sb.sb_inprogress) { 497 xfs_err(mp, "sb_inprogress set after log recovery??"); 498 WARN_ON(1); 499 return -EFSCORRUPTED; 500 } 501 502 /* 503 * Now the log is mounted, we know if it was an unclean shutdown or 504 * not. If it was, with the first phase of recovery has completed, we 505 * have consistent AG blocks on disk. We have not recovered EFIs yet, 506 * but they are recovered transactionally in the second recovery phase 507 * later. 508 * 509 * If the log was clean when we mounted, we can check the summary 510 * counters. If any of them are obviously incorrect, we can recompute 511 * them from the AGF headers in the next step. 512 */ 513 if (xfs_is_clean(mp) && 514 (mp->m_sb.sb_fdblocks > mp->m_sb.sb_dblocks || 515 !xfs_verify_icount(mp, mp->m_sb.sb_icount) || 516 mp->m_sb.sb_ifree > mp->m_sb.sb_icount)) 517 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 518 519 /* 520 * We can safely re-initialise incore superblock counters from the 521 * per-ag data. These may not be correct if the filesystem was not 522 * cleanly unmounted, so we waited for recovery to finish before doing 523 * this. 524 * 525 * If the filesystem was cleanly unmounted or the previous check did 526 * not flag anything weird, then we can trust the values in the 527 * superblock to be correct and we don't need to do anything here. 528 * Otherwise, recalculate the summary counters. 529 */ 530 if ((xfs_has_lazysbcount(mp) && !xfs_is_clean(mp)) || 531 xfs_fs_has_sickness(mp, XFS_SICK_FS_COUNTERS)) { 532 error = xfs_initialize_perag_data(mp, mp->m_sb.sb_agcount); 533 if (error) 534 return error; 535 } 536 537 /* 538 * Older kernels misused sb_frextents to reflect both incore 539 * reservations made by running transactions and the actual count of 540 * free rt extents in the ondisk metadata. Transactions committed 541 * during runtime can therefore contain a superblock update that 542 * undercounts the number of free rt extents tracked in the rt bitmap. 543 * A clean unmount record will have the correct frextents value since 544 * there can be no other transactions running at that point. 545 * 546 * If we're mounting the rt volume after recovering the log, recompute 547 * frextents from the rtbitmap file to fix the inconsistency. 548 */ 549 if (xfs_has_realtime(mp) && !xfs_is_clean(mp)) { 550 error = xfs_rtalloc_reinit_frextents(mp); 551 if (error) 552 return error; 553 } 554 555 return 0; 556 } 557 558 static void 559 xfs_unmount_check( 560 struct xfs_mount *mp) 561 { 562 if (xfs_is_shutdown(mp)) 563 return; 564 565 if (percpu_counter_sum(&mp->m_ifree) > 566 percpu_counter_sum(&mp->m_icount)) { 567 xfs_alert(mp, "ifree/icount mismatch at unmount"); 568 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 569 } 570 } 571 572 /* 573 * Flush and reclaim dirty inodes in preparation for unmount. Inodes and 574 * internal inode structures can be sitting in the CIL and AIL at this point, 575 * so we need to unpin them, write them back and/or reclaim them before unmount 576 * can proceed. In other words, callers are required to have inactivated all 577 * inodes. 578 * 579 * An inode cluster that has been freed can have its buffer still pinned in 580 * memory because the transaction is still sitting in a iclog. The stale inodes 581 * on that buffer will be pinned to the buffer until the transaction hits the 582 * disk and the callbacks run. Pushing the AIL will skip the stale inodes and 583 * may never see the pinned buffer, so nothing will push out the iclog and 584 * unpin the buffer. 585 * 586 * Hence we need to force the log to unpin everything first. However, log 587 * forces don't wait for the discards they issue to complete, so we have to 588 * explicitly wait for them to complete here as well. 589 * 590 * Then we can tell the world we are unmounting so that error handling knows 591 * that the filesystem is going away and we should error out anything that we 592 * have been retrying in the background. This will prevent never-ending 593 * retries in AIL pushing from hanging the unmount. 594 * 595 * Finally, we can push the AIL to clean all the remaining dirty objects, then 596 * reclaim the remaining inodes that are still in memory at this point in time. 597 */ 598 static void 599 xfs_unmount_flush_inodes( 600 struct xfs_mount *mp) 601 { 602 xfs_log_force(mp, XFS_LOG_SYNC); 603 xfs_extent_busy_wait_all(mp); 604 flush_workqueue(xfs_discard_wq); 605 606 xfs_set_unmounting(mp); 607 608 xfs_ail_push_all_sync(mp->m_ail); 609 xfs_inodegc_stop(mp); 610 cancel_delayed_work_sync(&mp->m_reclaim_work); 611 xfs_reclaim_inodes(mp); 612 xfs_health_unmount(mp); 613 } 614 615 static void 616 xfs_mount_setup_inode_geom( 617 struct xfs_mount *mp) 618 { 619 struct xfs_ino_geometry *igeo = M_IGEO(mp); 620 621 igeo->attr_fork_offset = xfs_bmap_compute_attr_offset(mp); 622 ASSERT(igeo->attr_fork_offset < XFS_LITINO(mp)); 623 624 xfs_ialloc_setup_geometry(mp); 625 } 626 627 /* Mount the metadata directory tree root. */ 628 STATIC int 629 xfs_mount_setup_metadir( 630 struct xfs_mount *mp) 631 { 632 int error; 633 634 /* Load the metadata directory root inode into memory. */ 635 error = xfs_metafile_iget(mp, mp->m_sb.sb_metadirino, XFS_METAFILE_DIR, 636 &mp->m_metadirip); 637 if (error) 638 xfs_warn(mp, "Failed to load metadir root directory, error %d", 639 error); 640 return error; 641 } 642 643 /* Compute maximum possible height for per-AG btree types for this fs. */ 644 static inline void 645 xfs_agbtree_compute_maxlevels( 646 struct xfs_mount *mp) 647 { 648 unsigned int levels; 649 650 levels = max(mp->m_alloc_maxlevels, M_IGEO(mp)->inobt_maxlevels); 651 levels = max(levels, mp->m_rmap_maxlevels); 652 mp->m_agbtree_maxlevels = max(levels, mp->m_refc_maxlevels); 653 } 654 655 /* Compute maximum possible height for realtime btree types for this fs. */ 656 static inline void 657 xfs_rtbtree_compute_maxlevels( 658 struct xfs_mount *mp) 659 { 660 mp->m_rtbtree_maxlevels = max(mp->m_rtrmap_maxlevels, 661 mp->m_rtrefc_maxlevels); 662 } 663 664 /* 665 * This function does the following on an initial mount of a file system: 666 * - reads the superblock from disk and init the mount struct 667 * - if we're a 32-bit kernel, do a size check on the superblock 668 * so we don't mount terabyte filesystems 669 * - init mount struct realtime fields 670 * - allocate inode hash table for fs 671 * - init directory manager 672 * - perform recovery and init the log manager 673 */ 674 int 675 xfs_mountfs( 676 struct xfs_mount *mp) 677 { 678 struct xfs_sb *sbp = &(mp->m_sb); 679 struct xfs_inode *rip; 680 struct xfs_ino_geometry *igeo = M_IGEO(mp); 681 uint quotamount = 0; 682 uint quotaflags = 0; 683 int error = 0; 684 685 xfs_sb_mount_common(mp, sbp); 686 687 /* 688 * Check for a mismatched features2 values. Older kernels read & wrote 689 * into the wrong sb offset for sb_features2 on some platforms due to 690 * xfs_sb_t not being 64bit size aligned when sb_features2 was added, 691 * which made older superblock reading/writing routines swap it as a 692 * 64-bit value. 693 * 694 * For backwards compatibility, we make both slots equal. 695 * 696 * If we detect a mismatched field, we OR the set bits into the existing 697 * features2 field in case it has already been modified; we don't want 698 * to lose any features. We then update the bad location with the ORed 699 * value so that older kernels will see any features2 flags. The 700 * superblock writeback code ensures the new sb_features2 is copied to 701 * sb_bad_features2 before it is logged or written to disk. 702 */ 703 if (xfs_sb_has_mismatched_features2(sbp)) { 704 xfs_warn(mp, "correcting sb_features alignment problem"); 705 sbp->sb_features2 |= sbp->sb_bad_features2; 706 mp->m_update_sb = true; 707 } 708 709 710 /* always use v2 inodes by default now */ 711 if (!(mp->m_sb.sb_versionnum & XFS_SB_VERSION_NLINKBIT)) { 712 mp->m_sb.sb_versionnum |= XFS_SB_VERSION_NLINKBIT; 713 mp->m_features |= XFS_FEAT_NLINK; 714 mp->m_update_sb = true; 715 } 716 717 /* 718 * If we were given new sunit/swidth options, do some basic validation 719 * checks and convert the incore dalign and swidth values to the 720 * same units (FSB) that everything else uses. This /must/ happen 721 * before computing the inode geometry. 722 */ 723 error = xfs_validate_new_dalign(mp); 724 if (error) 725 goto out; 726 727 xfs_alloc_compute_maxlevels(mp); 728 xfs_bmap_compute_maxlevels(mp, XFS_DATA_FORK); 729 xfs_bmap_compute_maxlevels(mp, XFS_ATTR_FORK); 730 xfs_mount_setup_inode_geom(mp); 731 xfs_rmapbt_compute_maxlevels(mp); 732 xfs_rtrmapbt_compute_maxlevels(mp); 733 xfs_refcountbt_compute_maxlevels(mp); 734 xfs_rtrefcountbt_compute_maxlevels(mp); 735 736 xfs_agbtree_compute_maxlevels(mp); 737 xfs_rtbtree_compute_maxlevels(mp); 738 739 /* 740 * Check if sb_agblocks is aligned at stripe boundary. If sb_agblocks 741 * is NOT aligned turn off m_dalign since allocator alignment is within 742 * an ag, therefore ag has to be aligned at stripe boundary. Note that 743 * we must compute the free space and rmap btree geometry before doing 744 * this. 745 */ 746 error = xfs_update_alignment(mp); 747 if (error) 748 goto out; 749 750 /* enable fail_at_unmount as default */ 751 mp->m_fail_unmount = true; 752 753 super_set_sysfs_name_id(mp->m_super); 754 755 error = xfs_sysfs_init(&mp->m_kobj, &xfs_mp_ktype, 756 NULL, mp->m_super->s_id); 757 if (error) 758 goto out; 759 760 error = xfs_sysfs_init(&mp->m_stats.xs_kobj, &xfs_stats_ktype, 761 &mp->m_kobj, "stats"); 762 if (error) 763 goto out_remove_sysfs; 764 765 xchk_stats_register(mp->m_scrub_stats, mp->m_debugfs); 766 767 error = xfs_error_sysfs_init(mp); 768 if (error) 769 goto out_remove_scrub_stats; 770 771 error = xfs_errortag_init(mp); 772 if (error) 773 goto out_remove_error_sysfs; 774 775 error = xfs_uuid_mount(mp); 776 if (error) 777 goto out_remove_errortag; 778 779 /* 780 * Update the preferred write size based on the information from the 781 * on-disk superblock. 782 */ 783 mp->m_allocsize_log = 784 max_t(uint32_t, sbp->sb_blocklog, mp->m_allocsize_log); 785 mp->m_allocsize_blocks = 1U << (mp->m_allocsize_log - sbp->sb_blocklog); 786 787 /* set the low space thresholds for dynamic preallocation */ 788 xfs_set_low_space_thresholds(mp); 789 790 /* 791 * If enabled, sparse inode chunk alignment is expected to match the 792 * cluster size. Full inode chunk alignment must match the chunk size, 793 * but that is checked on sb read verification... 794 */ 795 if (xfs_has_sparseinodes(mp) && 796 mp->m_sb.sb_spino_align != 797 XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)) { 798 xfs_warn(mp, 799 "Sparse inode block alignment (%u) must match cluster size (%llu).", 800 mp->m_sb.sb_spino_align, 801 XFS_B_TO_FSBT(mp, igeo->inode_cluster_size_raw)); 802 error = -EINVAL; 803 goto out_remove_uuid; 804 } 805 806 /* 807 * Check that the data (and log if separate) is an ok size. 808 */ 809 error = xfs_check_sizes(mp); 810 if (error) 811 goto out_remove_uuid; 812 813 /* 814 * Initialize realtime fields in the mount structure 815 */ 816 error = xfs_rtmount_init(mp); 817 if (error) { 818 xfs_warn(mp, "RT mount failed"); 819 goto out_remove_uuid; 820 } 821 822 /* 823 * Copies the low order bits of the timestamp and the randomly 824 * set "sequence" number out of a UUID. 825 */ 826 mp->m_fixedfsid[0] = 827 (get_unaligned_be16(&sbp->sb_uuid.b[8]) << 16) | 828 get_unaligned_be16(&sbp->sb_uuid.b[4]); 829 mp->m_fixedfsid[1] = get_unaligned_be32(&sbp->sb_uuid.b[0]); 830 831 error = xfs_da_mount(mp); 832 if (error) { 833 xfs_warn(mp, "Failed dir/attr init: %d", error); 834 goto out_remove_uuid; 835 } 836 837 /* 838 * Initialize the precomputed transaction reservations values. 839 */ 840 xfs_trans_init(mp); 841 842 /* 843 * Allocate and initialize the per-ag data. 844 */ 845 error = xfs_initialize_perag(mp, 0, sbp->sb_agcount, 846 mp->m_sb.sb_dblocks, &mp->m_maxagi); 847 if (error) { 848 xfs_warn(mp, "Failed per-ag init: %d", error); 849 goto out_free_dir; 850 } 851 852 error = xfs_initialize_rtgroups(mp, 0, sbp->sb_rgcount, 853 mp->m_sb.sb_rextents); 854 if (error) { 855 xfs_warn(mp, "Failed rtgroup init: %d", error); 856 goto out_free_perag; 857 } 858 859 if (XFS_IS_CORRUPT(mp, !sbp->sb_logblocks)) { 860 xfs_warn(mp, "no log defined"); 861 error = -EFSCORRUPTED; 862 goto out_free_rtgroup; 863 } 864 865 error = xfs_inodegc_register_shrinker(mp); 866 if (error) 867 goto out_fail_wait; 868 869 /* 870 * If we're resuming quota status, pick up the preliminary qflags from 871 * the ondisk superblock so that we know if we should recover dquots. 872 */ 873 if (xfs_is_resuming_quotaon(mp)) 874 xfs_qm_resume_quotaon(mp); 875 876 /* 877 * Log's mount-time initialization. The first part of recovery can place 878 * some items on the AIL, to be handled when recovery is finished or 879 * cancelled. 880 */ 881 error = xfs_log_mount(mp, mp->m_logdev_targp, 882 XFS_FSB_TO_DADDR(mp, sbp->sb_logstart), 883 XFS_FSB_TO_BB(mp, sbp->sb_logblocks)); 884 if (error) { 885 xfs_warn(mp, "log mount failed"); 886 goto out_inodegc_shrinker; 887 } 888 889 /* 890 * If we're resuming quota status and recovered the log, re-sample the 891 * qflags from the ondisk superblock now that we've recovered it, just 892 * in case someone shut down enforcement just before a crash. 893 */ 894 if (xfs_clear_resuming_quotaon(mp) && xlog_recovery_needed(mp->m_log)) 895 xfs_qm_resume_quotaon(mp); 896 897 /* 898 * If logged xattrs are still enabled after log recovery finishes, then 899 * they'll be available until unmount. Otherwise, turn them off. 900 */ 901 if (xfs_sb_version_haslogxattrs(&mp->m_sb)) 902 xfs_set_using_logged_xattrs(mp); 903 else 904 xfs_clear_using_logged_xattrs(mp); 905 906 /* Enable background inode inactivation workers. */ 907 xfs_inodegc_start(mp); 908 xfs_blockgc_start(mp); 909 910 /* 911 * Now that we've recovered any pending superblock feature bit 912 * additions, we can finish setting up the attr2 behaviour for the 913 * mount. The noattr2 option overrides the superblock flag, so only 914 * check the superblock feature flag if the mount option is not set. 915 */ 916 if (xfs_has_noattr2(mp)) { 917 mp->m_features &= ~XFS_FEAT_ATTR2; 918 } else if (!xfs_has_attr2(mp) && 919 (mp->m_sb.sb_features2 & XFS_SB_VERSION2_ATTR2BIT)) { 920 mp->m_features |= XFS_FEAT_ATTR2; 921 } 922 923 if (xfs_has_metadir(mp)) { 924 error = xfs_mount_setup_metadir(mp); 925 if (error) 926 goto out_free_metadir; 927 } 928 929 /* 930 * Get and sanity-check the root inode. 931 * Save the pointer to it in the mount structure. 932 */ 933 error = xfs_iget(mp, NULL, sbp->sb_rootino, XFS_IGET_UNTRUSTED, 934 XFS_ILOCK_EXCL, &rip); 935 if (error) { 936 xfs_warn(mp, 937 "Failed to read root inode 0x%llx, error %d", 938 sbp->sb_rootino, -error); 939 goto out_free_metadir; 940 } 941 942 ASSERT(rip != NULL); 943 944 if (XFS_IS_CORRUPT(mp, !S_ISDIR(VFS_I(rip)->i_mode))) { 945 xfs_warn(mp, "corrupted root inode %llu: not a directory", 946 (unsigned long long)rip->i_ino); 947 xfs_iunlock(rip, XFS_ILOCK_EXCL); 948 error = -EFSCORRUPTED; 949 goto out_rele_rip; 950 } 951 mp->m_rootip = rip; /* save it */ 952 953 xfs_iunlock(rip, XFS_ILOCK_EXCL); 954 955 /* 956 * Initialize realtime inode pointers in the mount structure 957 */ 958 error = xfs_rtmount_inodes(mp); 959 if (error) { 960 /* 961 * Free up the root inode. 962 */ 963 xfs_warn(mp, "failed to read RT inodes"); 964 goto out_rele_rip; 965 } 966 967 /* Make sure the summary counts are ok. */ 968 error = xfs_check_summary_counts(mp); 969 if (error) 970 goto out_rtunmount; 971 972 /* 973 * If this is a read-only mount defer the superblock updates until 974 * the next remount into writeable mode. Otherwise we would never 975 * perform the update e.g. for the root filesystem. 976 */ 977 if (mp->m_update_sb && !xfs_is_readonly(mp)) { 978 error = xfs_sync_sb(mp, false); 979 if (error) { 980 xfs_warn(mp, "failed to write sb changes"); 981 goto out_rtunmount; 982 } 983 } 984 985 /* 986 * Initialise the XFS quota management subsystem for this mount 987 */ 988 if (XFS_IS_QUOTA_ON(mp)) { 989 error = xfs_qm_newmount(mp, "amount, "aflags); 990 if (error) 991 goto out_rtunmount; 992 } else { 993 /* 994 * If a file system had quotas running earlier, but decided to 995 * mount without -o uquota/pquota/gquota options, revoke the 996 * quotachecked license. 997 */ 998 if (mp->m_sb.sb_qflags & XFS_ALL_QUOTA_ACCT) { 999 xfs_notice(mp, "resetting quota flags"); 1000 error = xfs_mount_reset_sbqflags(mp); 1001 if (error) 1002 goto out_rtunmount; 1003 } 1004 } 1005 1006 /* 1007 * Finish recovering the file system. This part needed to be delayed 1008 * until after the root and real-time bitmap inodes were consistently 1009 * read in. Temporarily create per-AG space reservations for metadata 1010 * btree shape changes because space freeing transactions (for inode 1011 * inactivation) require the per-AG reservation in lieu of reserving 1012 * blocks. 1013 */ 1014 error = xfs_fs_reserve_ag_blocks(mp); 1015 if (error && error == -ENOSPC) 1016 xfs_warn(mp, 1017 "ENOSPC reserving per-AG metadata pool, log recovery may fail."); 1018 error = xfs_log_mount_finish(mp); 1019 xfs_fs_unreserve_ag_blocks(mp); 1020 if (error) { 1021 xfs_warn(mp, "log mount finish failed"); 1022 goto out_rtunmount; 1023 } 1024 1025 /* 1026 * Now the log is fully replayed, we can transition to full read-only 1027 * mode for read-only mounts. This will sync all the metadata and clean 1028 * the log so that the recovery we just performed does not have to be 1029 * replayed again on the next mount. 1030 * 1031 * We use the same quiesce mechanism as the rw->ro remount, as they are 1032 * semantically identical operations. 1033 */ 1034 if (xfs_is_readonly(mp) && !xfs_has_norecovery(mp)) 1035 xfs_log_clean(mp); 1036 1037 /* 1038 * Complete the quota initialisation, post-log-replay component. 1039 */ 1040 if (quotamount) { 1041 ASSERT(mp->m_qflags == 0); 1042 mp->m_qflags = quotaflags; 1043 1044 xfs_qm_mount_quotas(mp); 1045 } 1046 1047 /* 1048 * Now we are mounted, reserve a small amount of unused space for 1049 * privileged transactions. This is needed so that transaction 1050 * space required for critical operations can dip into this pool 1051 * when at ENOSPC. This is needed for operations like create with 1052 * attr, unwritten extent conversion at ENOSPC, etc. Data allocations 1053 * are not allowed to use this reserved space. 1054 * 1055 * This may drive us straight to ENOSPC on mount, but that implies 1056 * we were already there on the last unmount. Warn if this occurs. 1057 */ 1058 if (!xfs_is_readonly(mp)) { 1059 error = xfs_reserve_blocks(mp, xfs_default_resblks(mp)); 1060 if (error) 1061 xfs_warn(mp, 1062 "Unable to allocate reserve blocks. Continuing without reserve pool."); 1063 1064 /* Reserve AG blocks for future btree expansion. */ 1065 error = xfs_fs_reserve_ag_blocks(mp); 1066 if (error && error != -ENOSPC) 1067 goto out_agresv; 1068 } 1069 1070 return 0; 1071 1072 out_agresv: 1073 xfs_fs_unreserve_ag_blocks(mp); 1074 xfs_qm_unmount_quotas(mp); 1075 out_rtunmount: 1076 xfs_rtunmount_inodes(mp); 1077 out_rele_rip: 1078 xfs_irele(rip); 1079 /* Clean out dquots that might be in memory after quotacheck. */ 1080 xfs_qm_unmount(mp); 1081 out_free_metadir: 1082 if (mp->m_metadirip) 1083 xfs_irele(mp->m_metadirip); 1084 1085 /* 1086 * Inactivate all inodes that might still be in memory after a log 1087 * intent recovery failure so that reclaim can free them. Metadata 1088 * inodes and the root directory shouldn't need inactivation, but the 1089 * mount failed for some reason, so pull down all the state and flee. 1090 */ 1091 xfs_inodegc_flush(mp); 1092 1093 /* 1094 * Flush all inode reclamation work and flush the log. 1095 * We have to do this /after/ rtunmount and qm_unmount because those 1096 * two will have scheduled delayed reclaim for the rt/quota inodes. 1097 * 1098 * This is slightly different from the unmountfs call sequence 1099 * because we could be tearing down a partially set up mount. In 1100 * particular, if log_mount_finish fails we bail out without calling 1101 * qm_unmount_quotas and therefore rely on qm_unmount to release the 1102 * quota inodes. 1103 */ 1104 xfs_unmount_flush_inodes(mp); 1105 xfs_log_mount_cancel(mp); 1106 out_inodegc_shrinker: 1107 shrinker_free(mp->m_inodegc_shrinker); 1108 out_fail_wait: 1109 if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp) 1110 xfs_buftarg_drain(mp->m_logdev_targp); 1111 xfs_buftarg_drain(mp->m_ddev_targp); 1112 out_free_rtgroup: 1113 xfs_free_rtgroups(mp, 0, mp->m_sb.sb_rgcount); 1114 out_free_perag: 1115 xfs_free_perag_range(mp, 0, mp->m_sb.sb_agcount); 1116 out_free_dir: 1117 xfs_da_unmount(mp); 1118 out_remove_uuid: 1119 xfs_uuid_unmount(mp); 1120 out_remove_errortag: 1121 xfs_errortag_del(mp); 1122 out_remove_error_sysfs: 1123 xfs_error_sysfs_del(mp); 1124 out_remove_scrub_stats: 1125 xchk_stats_unregister(mp->m_scrub_stats); 1126 xfs_sysfs_del(&mp->m_stats.xs_kobj); 1127 out_remove_sysfs: 1128 xfs_sysfs_del(&mp->m_kobj); 1129 out: 1130 return error; 1131 } 1132 1133 /* 1134 * This flushes out the inodes,dquots and the superblock, unmounts the 1135 * log and makes sure that incore structures are freed. 1136 */ 1137 void 1138 xfs_unmountfs( 1139 struct xfs_mount *mp) 1140 { 1141 int error; 1142 1143 /* 1144 * Perform all on-disk metadata updates required to inactivate inodes 1145 * that the VFS evicted earlier in the unmount process. Freeing inodes 1146 * and discarding CoW fork preallocations can cause shape changes to 1147 * the free inode and refcount btrees, respectively, so we must finish 1148 * this before we discard the metadata space reservations. Metadata 1149 * inodes and the root directory do not require inactivation. 1150 */ 1151 xfs_inodegc_flush(mp); 1152 1153 xfs_blockgc_stop(mp); 1154 xfs_fs_unreserve_ag_blocks(mp); 1155 xfs_qm_unmount_quotas(mp); 1156 xfs_rtunmount_inodes(mp); 1157 xfs_irele(mp->m_rootip); 1158 if (mp->m_metadirip) 1159 xfs_irele(mp->m_metadirip); 1160 1161 xfs_unmount_flush_inodes(mp); 1162 1163 xfs_qm_unmount(mp); 1164 1165 /* 1166 * Unreserve any blocks we have so that when we unmount we don't account 1167 * the reserved free space as used. This is really only necessary for 1168 * lazy superblock counting because it trusts the incore superblock 1169 * counters to be absolutely correct on clean unmount. 1170 * 1171 * We don't bother correcting this elsewhere for lazy superblock 1172 * counting because on mount of an unclean filesystem we reconstruct the 1173 * correct counter value and this is irrelevant. 1174 * 1175 * For non-lazy counter filesystems, this doesn't matter at all because 1176 * we only every apply deltas to the superblock and hence the incore 1177 * value does not matter.... 1178 */ 1179 error = xfs_reserve_blocks(mp, 0); 1180 if (error) 1181 xfs_warn(mp, "Unable to free reserved block pool. " 1182 "Freespace may not be correct on next mount."); 1183 xfs_unmount_check(mp); 1184 1185 /* 1186 * Indicate that it's ok to clear log incompat bits before cleaning 1187 * the log and writing the unmount record. 1188 */ 1189 xfs_set_done_with_log_incompat(mp); 1190 xfs_log_unmount(mp); 1191 xfs_da_unmount(mp); 1192 xfs_uuid_unmount(mp); 1193 1194 #if defined(DEBUG) 1195 xfs_errortag_clearall(mp); 1196 #endif 1197 shrinker_free(mp->m_inodegc_shrinker); 1198 xfs_free_rtgroups(mp, 0, mp->m_sb.sb_rgcount); 1199 xfs_free_perag_range(mp, 0, mp->m_sb.sb_agcount); 1200 xfs_errortag_del(mp); 1201 xfs_error_sysfs_del(mp); 1202 xchk_stats_unregister(mp->m_scrub_stats); 1203 xfs_sysfs_del(&mp->m_stats.xs_kobj); 1204 xfs_sysfs_del(&mp->m_kobj); 1205 } 1206 1207 /* 1208 * Determine whether modifications can proceed. The caller specifies the minimum 1209 * freeze level for which modifications should not be allowed. This allows 1210 * certain operations to proceed while the freeze sequence is in progress, if 1211 * necessary. 1212 */ 1213 bool 1214 xfs_fs_writable( 1215 struct xfs_mount *mp, 1216 int level) 1217 { 1218 ASSERT(level > SB_UNFROZEN); 1219 if ((mp->m_super->s_writers.frozen >= level) || 1220 xfs_is_shutdown(mp) || xfs_is_readonly(mp)) 1221 return false; 1222 1223 return true; 1224 } 1225 1226 void 1227 xfs_add_freecounter( 1228 struct xfs_mount *mp, 1229 struct percpu_counter *counter, 1230 uint64_t delta) 1231 { 1232 bool has_resv_pool = (counter == &mp->m_fdblocks); 1233 uint64_t res_used; 1234 1235 /* 1236 * If the reserve pool is depleted, put blocks back into it first. 1237 * Most of the time the pool is full. 1238 */ 1239 if (!has_resv_pool || mp->m_resblks == mp->m_resblks_avail) { 1240 percpu_counter_add(counter, delta); 1241 return; 1242 } 1243 1244 spin_lock(&mp->m_sb_lock); 1245 res_used = mp->m_resblks - mp->m_resblks_avail; 1246 if (res_used > delta) { 1247 mp->m_resblks_avail += delta; 1248 } else { 1249 delta -= res_used; 1250 mp->m_resblks_avail = mp->m_resblks; 1251 percpu_counter_add(counter, delta); 1252 } 1253 spin_unlock(&mp->m_sb_lock); 1254 } 1255 1256 int 1257 xfs_dec_freecounter( 1258 struct xfs_mount *mp, 1259 struct percpu_counter *counter, 1260 uint64_t delta, 1261 bool rsvd) 1262 { 1263 int64_t lcounter; 1264 uint64_t set_aside = 0; 1265 s32 batch; 1266 bool has_resv_pool; 1267 1268 ASSERT(counter == &mp->m_fdblocks || counter == &mp->m_frextents); 1269 has_resv_pool = (counter == &mp->m_fdblocks); 1270 if (rsvd) 1271 ASSERT(has_resv_pool); 1272 1273 /* 1274 * Taking blocks away, need to be more accurate the closer we 1275 * are to zero. 1276 * 1277 * If the counter has a value of less than 2 * max batch size, 1278 * then make everything serialise as we are real close to 1279 * ENOSPC. 1280 */ 1281 if (__percpu_counter_compare(counter, 2 * XFS_FDBLOCKS_BATCH, 1282 XFS_FDBLOCKS_BATCH) < 0) 1283 batch = 1; 1284 else 1285 batch = XFS_FDBLOCKS_BATCH; 1286 1287 /* 1288 * Set aside allocbt blocks because these blocks are tracked as free 1289 * space but not available for allocation. Technically this means that a 1290 * single reservation cannot consume all remaining free space, but the 1291 * ratio of allocbt blocks to usable free blocks should be rather small. 1292 * The tradeoff without this is that filesystems that maintain high 1293 * perag block reservations can over reserve physical block availability 1294 * and fail physical allocation, which leads to much more serious 1295 * problems (i.e. transaction abort, pagecache discards, etc.) than 1296 * slightly premature -ENOSPC. 1297 */ 1298 if (has_resv_pool) 1299 set_aside = xfs_fdblocks_unavailable(mp); 1300 percpu_counter_add_batch(counter, -((int64_t)delta), batch); 1301 if (__percpu_counter_compare(counter, set_aside, 1302 XFS_FDBLOCKS_BATCH) >= 0) { 1303 /* we had space! */ 1304 return 0; 1305 } 1306 1307 /* 1308 * lock up the sb for dipping into reserves before releasing the space 1309 * that took us to ENOSPC. 1310 */ 1311 spin_lock(&mp->m_sb_lock); 1312 percpu_counter_add(counter, delta); 1313 if (!has_resv_pool || !rsvd) 1314 goto fdblocks_enospc; 1315 1316 lcounter = (long long)mp->m_resblks_avail - delta; 1317 if (lcounter >= 0) { 1318 mp->m_resblks_avail = lcounter; 1319 spin_unlock(&mp->m_sb_lock); 1320 return 0; 1321 } 1322 xfs_warn_once(mp, 1323 "Reserve blocks depleted! Consider increasing reserve pool size."); 1324 1325 fdblocks_enospc: 1326 spin_unlock(&mp->m_sb_lock); 1327 return -ENOSPC; 1328 } 1329 1330 /* 1331 * Used to free the superblock along various error paths. 1332 */ 1333 void 1334 xfs_freesb( 1335 struct xfs_mount *mp) 1336 { 1337 struct xfs_buf *bp = mp->m_sb_bp; 1338 1339 xfs_buf_lock(bp); 1340 mp->m_sb_bp = NULL; 1341 xfs_buf_relse(bp); 1342 } 1343 1344 /* 1345 * If the underlying (data/log/rt) device is readonly, there are some 1346 * operations that cannot proceed. 1347 */ 1348 int 1349 xfs_dev_is_read_only( 1350 struct xfs_mount *mp, 1351 char *message) 1352 { 1353 if (xfs_readonly_buftarg(mp->m_ddev_targp) || 1354 xfs_readonly_buftarg(mp->m_logdev_targp) || 1355 (mp->m_rtdev_targp && xfs_readonly_buftarg(mp->m_rtdev_targp))) { 1356 xfs_notice(mp, "%s required on read-only device.", message); 1357 xfs_notice(mp, "write access unavailable, cannot proceed."); 1358 return -EROFS; 1359 } 1360 return 0; 1361 } 1362 1363 /* Force the summary counters to be recalculated at next mount. */ 1364 void 1365 xfs_force_summary_recalc( 1366 struct xfs_mount *mp) 1367 { 1368 if (!xfs_has_lazysbcount(mp)) 1369 return; 1370 1371 xfs_fs_mark_sick(mp, XFS_SICK_FS_COUNTERS); 1372 } 1373 1374 /* 1375 * Enable a log incompat feature flag in the primary superblock. The caller 1376 * cannot have any other transactions in progress. 1377 */ 1378 int 1379 xfs_add_incompat_log_feature( 1380 struct xfs_mount *mp, 1381 uint32_t feature) 1382 { 1383 struct xfs_dsb *dsb; 1384 int error; 1385 1386 ASSERT(hweight32(feature) == 1); 1387 ASSERT(!(feature & XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)); 1388 1389 /* 1390 * Force the log to disk and kick the background AIL thread to reduce 1391 * the chances that the bwrite will stall waiting for the AIL to unpin 1392 * the primary superblock buffer. This isn't a data integrity 1393 * operation, so we don't need a synchronous push. 1394 */ 1395 error = xfs_log_force(mp, XFS_LOG_SYNC); 1396 if (error) 1397 return error; 1398 xfs_ail_push_all(mp->m_ail); 1399 1400 /* 1401 * Lock the primary superblock buffer to serialize all callers that 1402 * are trying to set feature bits. 1403 */ 1404 xfs_buf_lock(mp->m_sb_bp); 1405 xfs_buf_hold(mp->m_sb_bp); 1406 1407 if (xfs_is_shutdown(mp)) { 1408 error = -EIO; 1409 goto rele; 1410 } 1411 1412 if (xfs_sb_has_incompat_log_feature(&mp->m_sb, feature)) 1413 goto rele; 1414 1415 /* 1416 * Write the primary superblock to disk immediately, because we need 1417 * the log_incompat bit to be set in the primary super now to protect 1418 * the log items that we're going to commit later. 1419 */ 1420 dsb = mp->m_sb_bp->b_addr; 1421 xfs_sb_to_disk(dsb, &mp->m_sb); 1422 dsb->sb_features_log_incompat |= cpu_to_be32(feature); 1423 error = xfs_bwrite(mp->m_sb_bp); 1424 if (error) 1425 goto shutdown; 1426 1427 /* 1428 * Add the feature bits to the incore superblock before we unlock the 1429 * buffer. 1430 */ 1431 xfs_sb_add_incompat_log_features(&mp->m_sb, feature); 1432 xfs_buf_relse(mp->m_sb_bp); 1433 1434 /* Log the superblock to disk. */ 1435 return xfs_sync_sb(mp, false); 1436 shutdown: 1437 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR); 1438 rele: 1439 xfs_buf_relse(mp->m_sb_bp); 1440 return error; 1441 } 1442 1443 /* 1444 * Clear all the log incompat flags from the superblock. 1445 * 1446 * The caller cannot be in a transaction, must ensure that the log does not 1447 * contain any log items protected by any log incompat bit, and must ensure 1448 * that there are no other threads that depend on the state of the log incompat 1449 * feature flags in the primary super. 1450 * 1451 * Returns true if the superblock is dirty. 1452 */ 1453 bool 1454 xfs_clear_incompat_log_features( 1455 struct xfs_mount *mp) 1456 { 1457 bool ret = false; 1458 1459 if (!xfs_has_crc(mp) || 1460 !xfs_sb_has_incompat_log_feature(&mp->m_sb, 1461 XFS_SB_FEAT_INCOMPAT_LOG_ALL) || 1462 xfs_is_shutdown(mp) || 1463 !xfs_is_done_with_log_incompat(mp)) 1464 return false; 1465 1466 /* 1467 * Update the incore superblock. We synchronize on the primary super 1468 * buffer lock to be consistent with the add function, though at least 1469 * in theory this shouldn't be necessary. 1470 */ 1471 xfs_buf_lock(mp->m_sb_bp); 1472 xfs_buf_hold(mp->m_sb_bp); 1473 1474 if (xfs_sb_has_incompat_log_feature(&mp->m_sb, 1475 XFS_SB_FEAT_INCOMPAT_LOG_ALL)) { 1476 xfs_sb_remove_incompat_log_features(&mp->m_sb); 1477 ret = true; 1478 } 1479 1480 xfs_buf_relse(mp->m_sb_bp); 1481 return ret; 1482 } 1483 1484 /* 1485 * Update the in-core delayed block counter. 1486 * 1487 * We prefer to update the counter without having to take a spinlock for every 1488 * counter update (i.e. batching). Each change to delayed allocation 1489 * reservations can change can easily exceed the default percpu counter 1490 * batching, so we use a larger batch factor here. 1491 * 1492 * Note that we don't currently have any callers requiring fast summation 1493 * (e.g. percpu_counter_read) so we can use a big batch value here. 1494 */ 1495 #define XFS_DELALLOC_BATCH (4096) 1496 void 1497 xfs_mod_delalloc( 1498 struct xfs_inode *ip, 1499 int64_t data_delta, 1500 int64_t ind_delta) 1501 { 1502 struct xfs_mount *mp = ip->i_mount; 1503 1504 if (XFS_IS_REALTIME_INODE(ip)) { 1505 percpu_counter_add_batch(&mp->m_delalloc_rtextents, 1506 xfs_blen_to_rtbxlen(mp, data_delta), 1507 XFS_DELALLOC_BATCH); 1508 if (!ind_delta) 1509 return; 1510 data_delta = 0; 1511 } 1512 percpu_counter_add_batch(&mp->m_delalloc_blks, data_delta + ind_delta, 1513 XFS_DELALLOC_BATCH); 1514 } 1515