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_ialloc.h" 16 #include "xfs_alloc.h" 17 #include "xfs_error.h" 18 #include "xfs_trans.h" 19 #include "xfs_buf_item.h" 20 #include "xfs_bmap_btree.h" 21 #include "xfs_alloc_btree.h" 22 #include "xfs_log.h" 23 #include "xfs_rmap_btree.h" 24 #include "xfs_refcount_btree.h" 25 #include "xfs_da_format.h" 26 #include "xfs_health.h" 27 #include "xfs_ag.h" 28 #include "xfs_rtbitmap.h" 29 #include "xfs_exchrange.h" 30 31 /* 32 * Physical superblock buffer manipulations. Shared with libxfs in userspace. 33 */ 34 35 /* 36 * Check that all the V4 feature bits that the V5 filesystem format requires are 37 * correctly set. 38 */ 39 static bool 40 xfs_sb_validate_v5_features( 41 struct xfs_sb *sbp) 42 { 43 /* We must not have any unknown V4 feature bits set */ 44 if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) 45 return false; 46 47 /* 48 * The CRC bit is considered an invalid V4 flag, so we have to add it 49 * manually to the OKBITS mask. 50 */ 51 if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS | 52 XFS_SB_VERSION2_CRCBIT)) 53 return false; 54 55 /* Now check all the required V4 feature flags are set. */ 56 57 #define V5_VERS_FLAGS (XFS_SB_VERSION_NLINKBIT | \ 58 XFS_SB_VERSION_ALIGNBIT | \ 59 XFS_SB_VERSION_LOGV2BIT | \ 60 XFS_SB_VERSION_EXTFLGBIT | \ 61 XFS_SB_VERSION_DIRV2BIT | \ 62 XFS_SB_VERSION_MOREBITSBIT) 63 64 #define V5_FEAT_FLAGS (XFS_SB_VERSION2_LAZYSBCOUNTBIT | \ 65 XFS_SB_VERSION2_ATTR2BIT | \ 66 XFS_SB_VERSION2_PROJID32BIT | \ 67 XFS_SB_VERSION2_CRCBIT) 68 69 if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS) 70 return false; 71 if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS) 72 return false; 73 return true; 74 } 75 76 /* 77 * We current support XFS v5 formats with known features and v4 superblocks with 78 * at least V2 directories. 79 */ 80 bool 81 xfs_sb_good_version( 82 struct xfs_sb *sbp) 83 { 84 /* 85 * All v5 filesystems are supported, but we must check that all the 86 * required v4 feature flags are enabled correctly as the code checks 87 * those flags and not for v5 support. 88 */ 89 if (xfs_sb_is_v5(sbp)) 90 return xfs_sb_validate_v5_features(sbp); 91 92 /* versions prior to v4 are not supported */ 93 if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4) 94 return false; 95 96 /* We must not have any unknown v4 feature bits set */ 97 if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) || 98 ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) && 99 (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS))) 100 return false; 101 102 /* V4 filesystems need v2 directories and unwritten extents */ 103 if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT)) 104 return false; 105 if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)) 106 return false; 107 108 /* It's a supported v4 filesystem */ 109 return true; 110 } 111 112 uint64_t 113 xfs_sb_version_to_features( 114 struct xfs_sb *sbp) 115 { 116 uint64_t features = 0; 117 118 /* optional V4 features */ 119 if (sbp->sb_rblocks > 0) 120 features |= XFS_FEAT_REALTIME; 121 if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT) 122 features |= XFS_FEAT_NLINK; 123 if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT) 124 features |= XFS_FEAT_ATTR; 125 if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT) 126 features |= XFS_FEAT_QUOTA; 127 if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT) 128 features |= XFS_FEAT_ALIGN; 129 if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT) 130 features |= XFS_FEAT_LOGV2; 131 if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT) 132 features |= XFS_FEAT_DALIGN; 133 if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT) 134 features |= XFS_FEAT_EXTFLG; 135 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) 136 features |= XFS_FEAT_SECTOR; 137 if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT) 138 features |= XFS_FEAT_ASCIICI; 139 if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) { 140 if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT) 141 features |= XFS_FEAT_LAZYSBCOUNT; 142 if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT) 143 features |= XFS_FEAT_ATTR2; 144 if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT) 145 features |= XFS_FEAT_PROJID32; 146 if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE) 147 features |= XFS_FEAT_FTYPE; 148 } 149 150 if (!xfs_sb_is_v5(sbp)) 151 return features; 152 153 /* Always on V5 features */ 154 features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG | 155 XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 | 156 XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO; 157 158 /* Optional V5 features */ 159 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT) 160 features |= XFS_FEAT_FINOBT; 161 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT) 162 features |= XFS_FEAT_RMAPBT; 163 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK) 164 features |= XFS_FEAT_REFLINK; 165 if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT) 166 features |= XFS_FEAT_INOBTCNT; 167 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE) 168 features |= XFS_FEAT_FTYPE; 169 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) 170 features |= XFS_FEAT_SPINODES; 171 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID) 172 features |= XFS_FEAT_META_UUID; 173 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME) 174 features |= XFS_FEAT_BIGTIME; 175 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR) 176 features |= XFS_FEAT_NEEDSREPAIR; 177 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64) 178 features |= XFS_FEAT_NREXT64; 179 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_EXCHRANGE) 180 features |= XFS_FEAT_EXCHANGE_RANGE; 181 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_PARENT) 182 features |= XFS_FEAT_PARENT; 183 184 return features; 185 } 186 187 /* Check all the superblock fields we care about when reading one in. */ 188 STATIC int 189 xfs_validate_sb_read( 190 struct xfs_mount *mp, 191 struct xfs_sb *sbp) 192 { 193 if (!xfs_sb_is_v5(sbp)) 194 return 0; 195 196 /* 197 * Version 5 superblock feature mask validation. Reject combinations 198 * the kernel cannot support up front before checking anything else. 199 */ 200 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) { 201 xfs_warn(mp, 202 "Superblock has unknown compatible features (0x%x) enabled.", 203 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN)); 204 xfs_warn(mp, 205 "Using a more recent kernel is recommended."); 206 } 207 208 if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) { 209 xfs_alert(mp, 210 "Superblock has unknown read-only compatible features (0x%x) enabled.", 211 (sbp->sb_features_ro_compat & 212 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)); 213 if (!xfs_is_readonly(mp)) { 214 xfs_warn(mp, 215 "Attempted to mount read-only compatible filesystem read-write."); 216 xfs_warn(mp, 217 "Filesystem can only be safely mounted read only."); 218 219 return -EINVAL; 220 } 221 } 222 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) { 223 xfs_warn(mp, 224 "Superblock has unknown incompatible features (0x%x) enabled.", 225 (sbp->sb_features_incompat & 226 XFS_SB_FEAT_INCOMPAT_UNKNOWN)); 227 xfs_warn(mp, 228 "Filesystem cannot be safely mounted by this kernel."); 229 return -EINVAL; 230 } 231 232 return 0; 233 } 234 235 /* Check all the superblock fields we care about when writing one out. */ 236 STATIC int 237 xfs_validate_sb_write( 238 struct xfs_mount *mp, 239 struct xfs_buf *bp, 240 struct xfs_sb *sbp) 241 { 242 /* 243 * Carry out additional sb summary counter sanity checks when we write 244 * the superblock. We skip this in the read validator because there 245 * could be newer superblocks in the log and if the values are garbage 246 * even after replay we'll recalculate them at the end of log mount. 247 * 248 * mkfs has traditionally written zeroed counters to inprogress and 249 * secondary superblocks, so allow this usage to continue because 250 * we never read counters from such superblocks. 251 */ 252 if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress && 253 (sbp->sb_fdblocks > sbp->sb_dblocks || 254 !xfs_verify_icount(mp, sbp->sb_icount) || 255 sbp->sb_ifree > sbp->sb_icount)) { 256 xfs_warn(mp, "SB summary counter sanity check failed"); 257 return -EFSCORRUPTED; 258 } 259 260 if (!xfs_sb_is_v5(sbp)) 261 return 0; 262 263 /* 264 * Version 5 superblock feature mask validation. Reject combinations 265 * the kernel cannot support since we checked for unsupported bits in 266 * the read verifier, which means that memory is corrupt. 267 */ 268 if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) { 269 xfs_warn(mp, 270 "Corruption detected in superblock compatible features (0x%x)!", 271 (sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN)); 272 return -EFSCORRUPTED; 273 } 274 275 if (!xfs_is_readonly(mp) && 276 xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) { 277 xfs_alert(mp, 278 "Corruption detected in superblock read-only compatible features (0x%x)!", 279 (sbp->sb_features_ro_compat & 280 XFS_SB_FEAT_RO_COMPAT_UNKNOWN)); 281 return -EFSCORRUPTED; 282 } 283 if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) { 284 xfs_warn(mp, 285 "Corruption detected in superblock incompatible features (0x%x)!", 286 (sbp->sb_features_incompat & 287 XFS_SB_FEAT_INCOMPAT_UNKNOWN)); 288 return -EFSCORRUPTED; 289 } 290 if (xfs_sb_has_incompat_log_feature(sbp, 291 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) { 292 xfs_warn(mp, 293 "Corruption detected in superblock incompatible log features (0x%x)!", 294 (sbp->sb_features_log_incompat & 295 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)); 296 return -EFSCORRUPTED; 297 } 298 299 /* 300 * We can't read verify the sb LSN because the read verifier is called 301 * before the log is allocated and processed. We know the log is set up 302 * before write verifier calls, so check it here. 303 */ 304 if (!xfs_log_check_lsn(mp, sbp->sb_lsn)) 305 return -EFSCORRUPTED; 306 307 return 0; 308 } 309 310 /* Check the validity of the SB. */ 311 STATIC int 312 xfs_validate_sb_common( 313 struct xfs_mount *mp, 314 struct xfs_buf *bp, 315 struct xfs_sb *sbp) 316 { 317 struct xfs_dsb *dsb = bp->b_addr; 318 uint32_t agcount = 0; 319 uint32_t rem; 320 bool has_dalign; 321 322 if (!xfs_verify_magic(bp, dsb->sb_magicnum)) { 323 xfs_warn(mp, 324 "Superblock has bad magic number 0x%x. Not an XFS filesystem?", 325 be32_to_cpu(dsb->sb_magicnum)); 326 return -EWRONGFS; 327 } 328 329 if (!xfs_sb_good_version(sbp)) { 330 xfs_warn(mp, 331 "Superblock has unknown features enabled or corrupted feature masks."); 332 return -EWRONGFS; 333 } 334 335 /* 336 * Validate feature flags and state 337 */ 338 if (xfs_sb_is_v5(sbp)) { 339 if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) { 340 xfs_notice(mp, 341 "Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)", 342 sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE); 343 return -EFSCORRUPTED; 344 } 345 346 /* V5 has a separate project quota inode */ 347 if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) { 348 xfs_notice(mp, 349 "Version 5 of Super block has XFS_OQUOTA bits."); 350 return -EFSCORRUPTED; 351 } 352 353 /* 354 * Full inode chunks must be aligned to inode chunk size when 355 * sparse inodes are enabled to support the sparse chunk 356 * allocation algorithm and prevent overlapping inode records. 357 */ 358 if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) { 359 uint32_t align; 360 361 align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize 362 >> sbp->sb_blocklog; 363 if (sbp->sb_inoalignmt != align) { 364 xfs_warn(mp, 365 "Inode block alignment (%u) must match chunk size (%u) for sparse inodes.", 366 sbp->sb_inoalignmt, align); 367 return -EINVAL; 368 } 369 } 370 } else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD | 371 XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) { 372 xfs_notice(mp, 373 "Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits."); 374 return -EFSCORRUPTED; 375 } 376 377 if (unlikely( 378 sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) { 379 xfs_warn(mp, 380 "filesystem is marked as having an external log; " 381 "specify logdev on the mount command line."); 382 return -EINVAL; 383 } 384 385 if (unlikely( 386 sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) { 387 xfs_warn(mp, 388 "filesystem is marked as having an internal log; " 389 "do not specify logdev on the mount command line."); 390 return -EINVAL; 391 } 392 393 /* Compute agcount for this number of dblocks and agblocks */ 394 if (sbp->sb_agblocks) { 395 agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem); 396 if (rem) 397 agcount++; 398 } 399 400 /* 401 * More sanity checking. Most of these were stolen directly from 402 * xfs_repair. 403 */ 404 if (unlikely( 405 sbp->sb_agcount <= 0 || 406 sbp->sb_sectsize < XFS_MIN_SECTORSIZE || 407 sbp->sb_sectsize > XFS_MAX_SECTORSIZE || 408 sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG || 409 sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG || 410 sbp->sb_sectsize != (1 << sbp->sb_sectlog) || 411 sbp->sb_blocksize < XFS_MIN_BLOCKSIZE || 412 sbp->sb_blocksize > XFS_MAX_BLOCKSIZE || 413 sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG || 414 sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || 415 sbp->sb_blocksize != (1 << sbp->sb_blocklog) || 416 sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG || 417 sbp->sb_inodesize < XFS_DINODE_MIN_SIZE || 418 sbp->sb_inodesize > XFS_DINODE_MAX_SIZE || 419 sbp->sb_inodelog < XFS_DINODE_MIN_LOG || 420 sbp->sb_inodelog > XFS_DINODE_MAX_LOG || 421 sbp->sb_inodesize != (1 << sbp->sb_inodelog) || 422 sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) || 423 XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES || 424 XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES || 425 sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1 || 426 agcount == 0 || agcount != sbp->sb_agcount || 427 (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog) || 428 (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE) || 429 (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) || 430 (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */) || 431 sbp->sb_dblocks == 0 || 432 sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp) || 433 sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp) || 434 sbp->sb_shared_vn != 0)) { 435 xfs_notice(mp, "SB sanity check failed"); 436 return -EFSCORRUPTED; 437 } 438 439 /* 440 * Logs that are too large are not supported at all. Reject them 441 * outright. Logs that are too small are tolerated on v4 filesystems, 442 * but we can only check that when mounting the log. Hence we skip 443 * those checks here. 444 */ 445 if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) { 446 xfs_notice(mp, 447 "Log size 0x%x blocks too large, maximum size is 0x%llx blocks", 448 sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS); 449 return -EFSCORRUPTED; 450 } 451 452 if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) { 453 xfs_warn(mp, 454 "log size 0x%llx bytes too large, maximum size is 0x%llx bytes", 455 XFS_FSB_TO_B(mp, sbp->sb_logblocks), 456 XFS_MAX_LOG_BYTES); 457 return -EFSCORRUPTED; 458 } 459 460 /* 461 * Do not allow filesystems with corrupted log sector or stripe units to 462 * be mounted. We cannot safely size the iclogs or write to the log if 463 * the log stripe unit is not valid. 464 */ 465 if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) { 466 if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) { 467 xfs_notice(mp, 468 "log sector size in bytes/log2 (0x%x/0x%x) must match", 469 sbp->sb_logsectsize, 1U << sbp->sb_logsectlog); 470 return -EFSCORRUPTED; 471 } 472 } else if (sbp->sb_logsectsize || sbp->sb_logsectlog) { 473 xfs_notice(mp, 474 "log sector size in bytes/log2 (0x%x/0x%x) are not zero", 475 sbp->sb_logsectsize, sbp->sb_logsectlog); 476 return -EFSCORRUPTED; 477 } 478 479 if (sbp->sb_logsunit > 1) { 480 if (sbp->sb_logsunit % sbp->sb_blocksize) { 481 xfs_notice(mp, 482 "log stripe unit 0x%x bytes must be a multiple of block size", 483 sbp->sb_logsunit); 484 return -EFSCORRUPTED; 485 } 486 if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) { 487 xfs_notice(mp, 488 "log stripe unit 0x%x bytes over maximum size (0x%x bytes)", 489 sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE); 490 return -EFSCORRUPTED; 491 } 492 } 493 494 /* Validate the realtime geometry; stolen from xfs_repair */ 495 if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE || 496 sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) { 497 xfs_notice(mp, 498 "realtime extent sanity check failed"); 499 return -EFSCORRUPTED; 500 } 501 502 if (sbp->sb_rblocks == 0) { 503 if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 || 504 sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) { 505 xfs_notice(mp, 506 "realtime zeroed geometry check failed"); 507 return -EFSCORRUPTED; 508 } 509 } else { 510 uint64_t rexts; 511 uint64_t rbmblocks; 512 513 rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize); 514 rbmblocks = howmany_64(sbp->sb_rextents, 515 NBBY * sbp->sb_blocksize); 516 517 if (!xfs_validate_rtextents(rexts) || 518 sbp->sb_rextents != rexts || 519 sbp->sb_rextslog != xfs_compute_rextslog(rexts) || 520 sbp->sb_rbmblocks != rbmblocks) { 521 xfs_notice(mp, 522 "realtime geometry sanity check failed"); 523 return -EFSCORRUPTED; 524 } 525 } 526 527 /* 528 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign) 529 * would imply the image is corrupted. 530 */ 531 has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT; 532 if (!!sbp->sb_unit ^ has_dalign) { 533 xfs_notice(mp, "SB stripe alignment sanity check failed"); 534 return -EFSCORRUPTED; 535 } 536 537 if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit), 538 XFS_FSB_TO_B(mp, sbp->sb_width), 0, 539 xfs_buf_daddr(bp) == XFS_SB_DADDR, false)) 540 return -EFSCORRUPTED; 541 542 /* 543 * Currently only very few inode sizes are supported. 544 */ 545 switch (sbp->sb_inodesize) { 546 case 256: 547 case 512: 548 case 1024: 549 case 2048: 550 break; 551 default: 552 xfs_warn(mp, "inode size of %d bytes not supported", 553 sbp->sb_inodesize); 554 return -ENOSYS; 555 } 556 557 return 0; 558 } 559 560 void 561 xfs_sb_quota_from_disk(struct xfs_sb *sbp) 562 { 563 /* 564 * older mkfs doesn't initialize quota inodes to NULLFSINO. This 565 * leads to in-core values having two different values for a quota 566 * inode to be invalid: 0 and NULLFSINO. Change it to a single value 567 * NULLFSINO. 568 * 569 * Note that this change affect only the in-core values. These 570 * values are not written back to disk unless any quota information 571 * is written to the disk. Even in that case, sb_pquotino field is 572 * not written to disk unless the superblock supports pquotino. 573 */ 574 if (sbp->sb_uquotino == 0) 575 sbp->sb_uquotino = NULLFSINO; 576 if (sbp->sb_gquotino == 0) 577 sbp->sb_gquotino = NULLFSINO; 578 if (sbp->sb_pquotino == 0) 579 sbp->sb_pquotino = NULLFSINO; 580 581 /* 582 * We need to do these manipilations only if we are working 583 * with an older version of on-disk superblock. 584 */ 585 if (xfs_sb_is_v5(sbp)) 586 return; 587 588 if (sbp->sb_qflags & XFS_OQUOTA_ENFD) 589 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? 590 XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD; 591 if (sbp->sb_qflags & XFS_OQUOTA_CHKD) 592 sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ? 593 XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD; 594 sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD); 595 596 if (sbp->sb_qflags & XFS_PQUOTA_ACCT && 597 sbp->sb_gquotino != NULLFSINO) { 598 /* 599 * In older version of superblock, on-disk superblock only 600 * has sb_gquotino, and in-core superblock has both sb_gquotino 601 * and sb_pquotino. But, only one of them is supported at any 602 * point of time. So, if PQUOTA is set in disk superblock, 603 * copy over sb_gquotino to sb_pquotino. The NULLFSINO test 604 * above is to make sure we don't do this twice and wipe them 605 * both out! 606 */ 607 sbp->sb_pquotino = sbp->sb_gquotino; 608 sbp->sb_gquotino = NULLFSINO; 609 } 610 } 611 612 static void 613 __xfs_sb_from_disk( 614 struct xfs_sb *to, 615 struct xfs_dsb *from, 616 bool convert_xquota) 617 { 618 to->sb_magicnum = be32_to_cpu(from->sb_magicnum); 619 to->sb_blocksize = be32_to_cpu(from->sb_blocksize); 620 to->sb_dblocks = be64_to_cpu(from->sb_dblocks); 621 to->sb_rblocks = be64_to_cpu(from->sb_rblocks); 622 to->sb_rextents = be64_to_cpu(from->sb_rextents); 623 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); 624 to->sb_logstart = be64_to_cpu(from->sb_logstart); 625 to->sb_rootino = be64_to_cpu(from->sb_rootino); 626 to->sb_rbmino = be64_to_cpu(from->sb_rbmino); 627 to->sb_rsumino = be64_to_cpu(from->sb_rsumino); 628 to->sb_rextsize = be32_to_cpu(from->sb_rextsize); 629 to->sb_agblocks = be32_to_cpu(from->sb_agblocks); 630 to->sb_agcount = be32_to_cpu(from->sb_agcount); 631 to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks); 632 to->sb_logblocks = be32_to_cpu(from->sb_logblocks); 633 to->sb_versionnum = be16_to_cpu(from->sb_versionnum); 634 to->sb_sectsize = be16_to_cpu(from->sb_sectsize); 635 to->sb_inodesize = be16_to_cpu(from->sb_inodesize); 636 to->sb_inopblock = be16_to_cpu(from->sb_inopblock); 637 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); 638 to->sb_blocklog = from->sb_blocklog; 639 to->sb_sectlog = from->sb_sectlog; 640 to->sb_inodelog = from->sb_inodelog; 641 to->sb_inopblog = from->sb_inopblog; 642 to->sb_agblklog = from->sb_agblklog; 643 to->sb_rextslog = from->sb_rextslog; 644 to->sb_inprogress = from->sb_inprogress; 645 to->sb_imax_pct = from->sb_imax_pct; 646 to->sb_icount = be64_to_cpu(from->sb_icount); 647 to->sb_ifree = be64_to_cpu(from->sb_ifree); 648 to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks); 649 to->sb_frextents = be64_to_cpu(from->sb_frextents); 650 to->sb_uquotino = be64_to_cpu(from->sb_uquotino); 651 to->sb_gquotino = be64_to_cpu(from->sb_gquotino); 652 to->sb_qflags = be16_to_cpu(from->sb_qflags); 653 to->sb_flags = from->sb_flags; 654 to->sb_shared_vn = from->sb_shared_vn; 655 to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt); 656 to->sb_unit = be32_to_cpu(from->sb_unit); 657 to->sb_width = be32_to_cpu(from->sb_width); 658 to->sb_dirblklog = from->sb_dirblklog; 659 to->sb_logsectlog = from->sb_logsectlog; 660 to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize); 661 to->sb_logsunit = be32_to_cpu(from->sb_logsunit); 662 to->sb_features2 = be32_to_cpu(from->sb_features2); 663 to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2); 664 to->sb_features_compat = be32_to_cpu(from->sb_features_compat); 665 to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat); 666 to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat); 667 to->sb_features_log_incompat = 668 be32_to_cpu(from->sb_features_log_incompat); 669 /* crc is only used on disk, not in memory; just init to 0 here. */ 670 to->sb_crc = 0; 671 to->sb_spino_align = be32_to_cpu(from->sb_spino_align); 672 to->sb_pquotino = be64_to_cpu(from->sb_pquotino); 673 to->sb_lsn = be64_to_cpu(from->sb_lsn); 674 /* 675 * sb_meta_uuid is only on disk if it differs from sb_uuid and the 676 * feature flag is set; if not set we keep it only in memory. 677 */ 678 if (xfs_sb_is_v5(to) && 679 (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)) 680 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); 681 else 682 uuid_copy(&to->sb_meta_uuid, &from->sb_uuid); 683 /* Convert on-disk flags to in-memory flags? */ 684 if (convert_xquota) 685 xfs_sb_quota_from_disk(to); 686 } 687 688 void 689 xfs_sb_from_disk( 690 struct xfs_sb *to, 691 struct xfs_dsb *from) 692 { 693 __xfs_sb_from_disk(to, from, true); 694 } 695 696 static void 697 xfs_sb_quota_to_disk( 698 struct xfs_dsb *to, 699 struct xfs_sb *from) 700 { 701 uint16_t qflags = from->sb_qflags; 702 703 to->sb_uquotino = cpu_to_be64(from->sb_uquotino); 704 705 /* 706 * The in-memory superblock quota state matches the v5 on-disk format so 707 * just write them out and return 708 */ 709 if (xfs_sb_is_v5(from)) { 710 to->sb_qflags = cpu_to_be16(from->sb_qflags); 711 to->sb_gquotino = cpu_to_be64(from->sb_gquotino); 712 to->sb_pquotino = cpu_to_be64(from->sb_pquotino); 713 return; 714 } 715 716 /* 717 * For older superblocks (v4), the in-core version of sb_qflags do not 718 * have XFS_OQUOTA_* flags, whereas the on-disk version does. So, 719 * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags. 720 */ 721 qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD | 722 XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD); 723 724 if (from->sb_qflags & 725 (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD)) 726 qflags |= XFS_OQUOTA_ENFD; 727 if (from->sb_qflags & 728 (XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) 729 qflags |= XFS_OQUOTA_CHKD; 730 to->sb_qflags = cpu_to_be16(qflags); 731 732 /* 733 * GQUOTINO and PQUOTINO cannot be used together in versions 734 * of superblock that do not have pquotino. from->sb_flags 735 * tells us which quota is active and should be copied to 736 * disk. If neither are active, we should NULL the inode. 737 * 738 * In all cases, the separate pquotino must remain 0 because it 739 * is beyond the "end" of the valid non-pquotino superblock. 740 */ 741 if (from->sb_qflags & XFS_GQUOTA_ACCT) 742 to->sb_gquotino = cpu_to_be64(from->sb_gquotino); 743 else if (from->sb_qflags & XFS_PQUOTA_ACCT) 744 to->sb_gquotino = cpu_to_be64(from->sb_pquotino); 745 else { 746 /* 747 * We can't rely on just the fields being logged to tell us 748 * that it is safe to write NULLFSINO - we should only do that 749 * if quotas are not actually enabled. Hence only write 750 * NULLFSINO if both in-core quota inodes are NULL. 751 */ 752 if (from->sb_gquotino == NULLFSINO && 753 from->sb_pquotino == NULLFSINO) 754 to->sb_gquotino = cpu_to_be64(NULLFSINO); 755 } 756 757 to->sb_pquotino = 0; 758 } 759 760 void 761 xfs_sb_to_disk( 762 struct xfs_dsb *to, 763 struct xfs_sb *from) 764 { 765 xfs_sb_quota_to_disk(to, from); 766 767 to->sb_magicnum = cpu_to_be32(from->sb_magicnum); 768 to->sb_blocksize = cpu_to_be32(from->sb_blocksize); 769 to->sb_dblocks = cpu_to_be64(from->sb_dblocks); 770 to->sb_rblocks = cpu_to_be64(from->sb_rblocks); 771 to->sb_rextents = cpu_to_be64(from->sb_rextents); 772 memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid)); 773 to->sb_logstart = cpu_to_be64(from->sb_logstart); 774 to->sb_rootino = cpu_to_be64(from->sb_rootino); 775 to->sb_rbmino = cpu_to_be64(from->sb_rbmino); 776 to->sb_rsumino = cpu_to_be64(from->sb_rsumino); 777 to->sb_rextsize = cpu_to_be32(from->sb_rextsize); 778 to->sb_agblocks = cpu_to_be32(from->sb_agblocks); 779 to->sb_agcount = cpu_to_be32(from->sb_agcount); 780 to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks); 781 to->sb_logblocks = cpu_to_be32(from->sb_logblocks); 782 to->sb_versionnum = cpu_to_be16(from->sb_versionnum); 783 to->sb_sectsize = cpu_to_be16(from->sb_sectsize); 784 to->sb_inodesize = cpu_to_be16(from->sb_inodesize); 785 to->sb_inopblock = cpu_to_be16(from->sb_inopblock); 786 memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname)); 787 to->sb_blocklog = from->sb_blocklog; 788 to->sb_sectlog = from->sb_sectlog; 789 to->sb_inodelog = from->sb_inodelog; 790 to->sb_inopblog = from->sb_inopblog; 791 to->sb_agblklog = from->sb_agblklog; 792 to->sb_rextslog = from->sb_rextslog; 793 to->sb_inprogress = from->sb_inprogress; 794 to->sb_imax_pct = from->sb_imax_pct; 795 to->sb_icount = cpu_to_be64(from->sb_icount); 796 to->sb_ifree = cpu_to_be64(from->sb_ifree); 797 to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks); 798 to->sb_frextents = cpu_to_be64(from->sb_frextents); 799 800 to->sb_flags = from->sb_flags; 801 to->sb_shared_vn = from->sb_shared_vn; 802 to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt); 803 to->sb_unit = cpu_to_be32(from->sb_unit); 804 to->sb_width = cpu_to_be32(from->sb_width); 805 to->sb_dirblklog = from->sb_dirblklog; 806 to->sb_logsectlog = from->sb_logsectlog; 807 to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize); 808 to->sb_logsunit = cpu_to_be32(from->sb_logsunit); 809 810 /* 811 * We need to ensure that bad_features2 always matches features2. 812 * Hence we enforce that here rather than having to remember to do it 813 * everywhere else that updates features2. 814 */ 815 from->sb_bad_features2 = from->sb_features2; 816 to->sb_features2 = cpu_to_be32(from->sb_features2); 817 to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2); 818 819 if (!xfs_sb_is_v5(from)) 820 return; 821 822 to->sb_features_compat = cpu_to_be32(from->sb_features_compat); 823 to->sb_features_ro_compat = 824 cpu_to_be32(from->sb_features_ro_compat); 825 to->sb_features_incompat = 826 cpu_to_be32(from->sb_features_incompat); 827 to->sb_features_log_incompat = 828 cpu_to_be32(from->sb_features_log_incompat); 829 to->sb_spino_align = cpu_to_be32(from->sb_spino_align); 830 to->sb_lsn = cpu_to_be64(from->sb_lsn); 831 if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID) 832 uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid); 833 } 834 835 /* 836 * If the superblock has the CRC feature bit set or the CRC field is non-null, 837 * check that the CRC is valid. We check the CRC field is non-null because a 838 * single bit error could clear the feature bit and unused parts of the 839 * superblock are supposed to be zero. Hence a non-null crc field indicates that 840 * we've potentially lost a feature bit and we should check it anyway. 841 * 842 * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the 843 * last field in V4 secondary superblocks. So for secondary superblocks, 844 * we are more forgiving, and ignore CRC failures if the primary doesn't 845 * indicate that the fs version is V5. 846 */ 847 static void 848 xfs_sb_read_verify( 849 struct xfs_buf *bp) 850 { 851 struct xfs_sb sb; 852 struct xfs_mount *mp = bp->b_mount; 853 struct xfs_dsb *dsb = bp->b_addr; 854 int error; 855 856 /* 857 * open code the version check to avoid needing to convert the entire 858 * superblock from disk order just to check the version number 859 */ 860 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) && 861 (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) == 862 XFS_SB_VERSION_5) || 863 dsb->sb_crc != 0)) { 864 865 if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) { 866 /* Only fail bad secondaries on a known V5 filesystem */ 867 if (xfs_buf_daddr(bp) == XFS_SB_DADDR || 868 xfs_has_crc(mp)) { 869 error = -EFSBADCRC; 870 goto out_error; 871 } 872 } 873 } 874 875 /* 876 * Check all the superblock fields. Don't byteswap the xquota flags 877 * because _verify_common checks the on-disk values. 878 */ 879 __xfs_sb_from_disk(&sb, dsb, false); 880 error = xfs_validate_sb_common(mp, bp, &sb); 881 if (error) 882 goto out_error; 883 error = xfs_validate_sb_read(mp, &sb); 884 885 out_error: 886 if (error == -EFSCORRUPTED || error == -EFSBADCRC) 887 xfs_verifier_error(bp, error, __this_address); 888 else if (error) 889 xfs_buf_ioerror(bp, error); 890 } 891 892 /* 893 * We may be probed for a filesystem match, so we may not want to emit 894 * messages when the superblock buffer is not actually an XFS superblock. 895 * If we find an XFS superblock, then run a normal, noisy mount because we are 896 * really going to mount it and want to know about errors. 897 */ 898 static void 899 xfs_sb_quiet_read_verify( 900 struct xfs_buf *bp) 901 { 902 struct xfs_dsb *dsb = bp->b_addr; 903 904 if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) { 905 /* XFS filesystem, verify noisily! */ 906 xfs_sb_read_verify(bp); 907 return; 908 } 909 /* quietly fail */ 910 xfs_buf_ioerror(bp, -EWRONGFS); 911 } 912 913 static void 914 xfs_sb_write_verify( 915 struct xfs_buf *bp) 916 { 917 struct xfs_sb sb; 918 struct xfs_mount *mp = bp->b_mount; 919 struct xfs_buf_log_item *bip = bp->b_log_item; 920 struct xfs_dsb *dsb = bp->b_addr; 921 int error; 922 923 /* 924 * Check all the superblock fields. Don't byteswap the xquota flags 925 * because _verify_common checks the on-disk values. 926 */ 927 __xfs_sb_from_disk(&sb, dsb, false); 928 error = xfs_validate_sb_common(mp, bp, &sb); 929 if (error) 930 goto out_error; 931 error = xfs_validate_sb_write(mp, bp, &sb); 932 if (error) 933 goto out_error; 934 935 if (!xfs_sb_is_v5(&sb)) 936 return; 937 938 if (bip) 939 dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn); 940 941 xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF); 942 return; 943 944 out_error: 945 xfs_verifier_error(bp, error, __this_address); 946 } 947 948 const struct xfs_buf_ops xfs_sb_buf_ops = { 949 .name = "xfs_sb", 950 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) }, 951 .verify_read = xfs_sb_read_verify, 952 .verify_write = xfs_sb_write_verify, 953 }; 954 955 const struct xfs_buf_ops xfs_sb_quiet_buf_ops = { 956 .name = "xfs_sb_quiet", 957 .magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) }, 958 .verify_read = xfs_sb_quiet_read_verify, 959 .verify_write = xfs_sb_write_verify, 960 }; 961 962 /* 963 * xfs_mount_common 964 * 965 * Mount initialization code establishing various mount 966 * fields from the superblock associated with the given 967 * mount structure. 968 * 969 * Inode geometry are calculated in xfs_ialloc_setup_geometry. 970 */ 971 void 972 xfs_sb_mount_common( 973 struct xfs_mount *mp, 974 struct xfs_sb *sbp) 975 { 976 mp->m_agfrotor = 0; 977 atomic_set(&mp->m_agirotor, 0); 978 mp->m_maxagi = mp->m_sb.sb_agcount; 979 mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG; 980 mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT; 981 mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT; 982 mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1; 983 mp->m_blockmask = sbp->sb_blocksize - 1; 984 mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG; 985 mp->m_blockwmask = mp->m_blockwsize - 1; 986 mp->m_rtxblklog = log2_if_power2(sbp->sb_rextsize); 987 mp->m_rtxblkmask = mask64_if_power2(sbp->sb_rextsize); 988 989 mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1); 990 mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0); 991 mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2; 992 mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2; 993 994 mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1); 995 mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0); 996 mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2; 997 mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2; 998 999 mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1); 1000 mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0); 1001 mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2; 1002 mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2; 1003 1004 mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true); 1005 mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false); 1006 mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2; 1007 mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2; 1008 1009 mp->m_bsize = XFS_FSB_TO_BB(mp, 1); 1010 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp); 1011 mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp); 1012 } 1013 1014 /* 1015 * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock 1016 * into the superblock buffer to be logged. It does not provide the higher 1017 * level of locking that is needed to protect the in-core superblock from 1018 * concurrent access. 1019 */ 1020 void 1021 xfs_log_sb( 1022 struct xfs_trans *tp) 1023 { 1024 struct xfs_mount *mp = tp->t_mountp; 1025 struct xfs_buf *bp = xfs_trans_getsb(tp); 1026 1027 /* 1028 * Lazy sb counters don't update the in-core superblock so do that now. 1029 * If this is at unmount, the counters will be exactly correct, but at 1030 * any other time they will only be ballpark correct because of 1031 * reservations that have been taken out percpu counters. If we have an 1032 * unclean shutdown, this will be corrected by log recovery rebuilding 1033 * the counters from the AGF block counts. 1034 * 1035 * Do not update sb_frextents here because it is not part of the lazy 1036 * sb counters, despite having a percpu counter. It is always kept 1037 * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas() 1038 * and hence we don't need have to update it here. 1039 */ 1040 if (xfs_has_lazysbcount(mp)) { 1041 mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount); 1042 mp->m_sb.sb_ifree = min_t(uint64_t, 1043 percpu_counter_sum(&mp->m_ifree), 1044 mp->m_sb.sb_icount); 1045 mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks); 1046 } 1047 1048 xfs_sb_to_disk(bp->b_addr, &mp->m_sb); 1049 xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF); 1050 xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1); 1051 } 1052 1053 /* 1054 * xfs_sync_sb 1055 * 1056 * Sync the superblock to disk. 1057 * 1058 * Note that the caller is responsible for checking the frozen state of the 1059 * filesystem. This procedure uses the non-blocking transaction allocator and 1060 * thus will allow modifications to a frozen fs. This is required because this 1061 * code can be called during the process of freezing where use of the high-level 1062 * allocator would deadlock. 1063 */ 1064 int 1065 xfs_sync_sb( 1066 struct xfs_mount *mp, 1067 bool wait) 1068 { 1069 struct xfs_trans *tp; 1070 int error; 1071 1072 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 1073 XFS_TRANS_NO_WRITECOUNT, &tp); 1074 if (error) 1075 return error; 1076 1077 xfs_log_sb(tp); 1078 if (wait) 1079 xfs_trans_set_sync(tp); 1080 return xfs_trans_commit(tp); 1081 } 1082 1083 /* 1084 * Update all the secondary superblocks to match the new state of the primary. 1085 * Because we are completely overwriting all the existing fields in the 1086 * secondary superblock buffers, there is no need to read them in from disk. 1087 * Just get a new buffer, stamp it and write it. 1088 * 1089 * The sb buffers need to be cached here so that we serialise against other 1090 * operations that access the secondary superblocks, but we don't want to keep 1091 * them in memory once it is written so we mark it as a one-shot buffer. 1092 */ 1093 int 1094 xfs_update_secondary_sbs( 1095 struct xfs_mount *mp) 1096 { 1097 struct xfs_perag *pag; 1098 xfs_agnumber_t agno = 1; 1099 int saved_error = 0; 1100 int error = 0; 1101 LIST_HEAD (buffer_list); 1102 1103 /* update secondary superblocks. */ 1104 for_each_perag_from(mp, agno, pag) { 1105 struct xfs_buf *bp; 1106 1107 error = xfs_buf_get(mp->m_ddev_targp, 1108 XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR), 1109 XFS_FSS_TO_BB(mp, 1), &bp); 1110 /* 1111 * If we get an error reading or writing alternate superblocks, 1112 * continue. xfs_repair chooses the "best" superblock based 1113 * on most matches; if we break early, we'll leave more 1114 * superblocks un-updated than updated, and xfs_repair may 1115 * pick them over the properly-updated primary. 1116 */ 1117 if (error) { 1118 xfs_warn(mp, 1119 "error allocating secondary superblock for ag %d", 1120 pag->pag_agno); 1121 if (!saved_error) 1122 saved_error = error; 1123 continue; 1124 } 1125 1126 bp->b_ops = &xfs_sb_buf_ops; 1127 xfs_buf_oneshot(bp); 1128 xfs_buf_zero(bp, 0, BBTOB(bp->b_length)); 1129 xfs_sb_to_disk(bp->b_addr, &mp->m_sb); 1130 xfs_buf_delwri_queue(bp, &buffer_list); 1131 xfs_buf_relse(bp); 1132 1133 /* don't hold too many buffers at once */ 1134 if (agno % 16) 1135 continue; 1136 1137 error = xfs_buf_delwri_submit(&buffer_list); 1138 if (error) { 1139 xfs_warn(mp, 1140 "write error %d updating a secondary superblock near ag %d", 1141 error, pag->pag_agno); 1142 if (!saved_error) 1143 saved_error = error; 1144 continue; 1145 } 1146 } 1147 error = xfs_buf_delwri_submit(&buffer_list); 1148 if (error) { 1149 xfs_warn(mp, 1150 "write error %d updating a secondary superblock near ag %d", 1151 error, agno); 1152 } 1153 1154 return saved_error ? saved_error : error; 1155 } 1156 1157 /* 1158 * Same behavior as xfs_sync_sb, except that it is always synchronous and it 1159 * also writes the superblock buffer to disk sector 0 immediately. 1160 */ 1161 int 1162 xfs_sync_sb_buf( 1163 struct xfs_mount *mp) 1164 { 1165 struct xfs_trans *tp; 1166 struct xfs_buf *bp; 1167 int error; 1168 1169 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp); 1170 if (error) 1171 return error; 1172 1173 bp = xfs_trans_getsb(tp); 1174 xfs_log_sb(tp); 1175 xfs_trans_bhold(tp, bp); 1176 xfs_trans_set_sync(tp); 1177 error = xfs_trans_commit(tp); 1178 if (error) 1179 goto out; 1180 /* 1181 * write out the sb buffer to get the changes to disk 1182 */ 1183 error = xfs_bwrite(bp); 1184 out: 1185 xfs_buf_relse(bp); 1186 return error; 1187 } 1188 1189 void 1190 xfs_fs_geometry( 1191 struct xfs_mount *mp, 1192 struct xfs_fsop_geom *geo, 1193 int struct_version) 1194 { 1195 struct xfs_sb *sbp = &mp->m_sb; 1196 1197 memset(geo, 0, sizeof(struct xfs_fsop_geom)); 1198 1199 geo->blocksize = sbp->sb_blocksize; 1200 geo->rtextsize = sbp->sb_rextsize; 1201 geo->agblocks = sbp->sb_agblocks; 1202 geo->agcount = sbp->sb_agcount; 1203 geo->logblocks = sbp->sb_logblocks; 1204 geo->sectsize = sbp->sb_sectsize; 1205 geo->inodesize = sbp->sb_inodesize; 1206 geo->imaxpct = sbp->sb_imax_pct; 1207 geo->datablocks = sbp->sb_dblocks; 1208 geo->rtblocks = sbp->sb_rblocks; 1209 geo->rtextents = sbp->sb_rextents; 1210 geo->logstart = sbp->sb_logstart; 1211 BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid)); 1212 memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid)); 1213 1214 if (struct_version < 2) 1215 return; 1216 1217 geo->sunit = sbp->sb_unit; 1218 geo->swidth = sbp->sb_width; 1219 1220 if (struct_version < 3) 1221 return; 1222 1223 geo->version = XFS_FSOP_GEOM_VERSION; 1224 geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK | 1225 XFS_FSOP_GEOM_FLAGS_DIRV2 | 1226 XFS_FSOP_GEOM_FLAGS_EXTFLG; 1227 if (xfs_has_attr(mp)) 1228 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR; 1229 if (xfs_has_quota(mp)) 1230 geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA; 1231 if (xfs_has_align(mp)) 1232 geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN; 1233 if (xfs_has_dalign(mp)) 1234 geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN; 1235 if (xfs_has_asciici(mp)) 1236 geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI; 1237 if (xfs_has_lazysbcount(mp)) 1238 geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB; 1239 if (xfs_has_attr2(mp)) 1240 geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2; 1241 if (xfs_has_projid32(mp)) 1242 geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32; 1243 if (xfs_has_crc(mp)) 1244 geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB; 1245 if (xfs_has_ftype(mp)) 1246 geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE; 1247 if (xfs_has_finobt(mp)) 1248 geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT; 1249 if (xfs_has_sparseinodes(mp)) 1250 geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES; 1251 if (xfs_has_rmapbt(mp)) 1252 geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT; 1253 if (xfs_has_reflink(mp)) 1254 geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK; 1255 if (xfs_has_bigtime(mp)) 1256 geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME; 1257 if (xfs_has_inobtcounts(mp)) 1258 geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT; 1259 if (xfs_has_parent(mp)) 1260 geo->flags |= XFS_FSOP_GEOM_FLAGS_PARENT; 1261 if (xfs_has_sector(mp)) { 1262 geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR; 1263 geo->logsectsize = sbp->sb_logsectsize; 1264 } else { 1265 geo->logsectsize = BBSIZE; 1266 } 1267 if (xfs_has_large_extent_counts(mp)) 1268 geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64; 1269 if (xfs_has_exchange_range(mp)) 1270 geo->flags |= XFS_FSOP_GEOM_FLAGS_EXCHANGE_RANGE; 1271 geo->rtsectsize = sbp->sb_blocksize; 1272 geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp); 1273 1274 if (struct_version < 4) 1275 return; 1276 1277 if (xfs_has_logv2(mp)) 1278 geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2; 1279 1280 geo->logsunit = sbp->sb_logsunit; 1281 1282 if (struct_version < 5) 1283 return; 1284 1285 geo->version = XFS_FSOP_GEOM_VERSION_V5; 1286 } 1287 1288 /* Read a secondary superblock. */ 1289 int 1290 xfs_sb_read_secondary( 1291 struct xfs_mount *mp, 1292 struct xfs_trans *tp, 1293 xfs_agnumber_t agno, 1294 struct xfs_buf **bpp) 1295 { 1296 struct xfs_buf *bp; 1297 int error; 1298 1299 ASSERT(agno != 0 && agno != NULLAGNUMBER); 1300 error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, 1301 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)), 1302 XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops); 1303 if (xfs_metadata_is_sick(error)) 1304 xfs_agno_mark_sick(mp, agno, XFS_SICK_AG_SB); 1305 if (error) 1306 return error; 1307 xfs_buf_set_ref(bp, XFS_SSB_REF); 1308 *bpp = bp; 1309 return 0; 1310 } 1311 1312 /* Get an uninitialised secondary superblock buffer. */ 1313 int 1314 xfs_sb_get_secondary( 1315 struct xfs_mount *mp, 1316 struct xfs_trans *tp, 1317 xfs_agnumber_t agno, 1318 struct xfs_buf **bpp) 1319 { 1320 struct xfs_buf *bp; 1321 int error; 1322 1323 ASSERT(agno != 0 && agno != NULLAGNUMBER); 1324 error = xfs_trans_get_buf(tp, mp->m_ddev_targp, 1325 XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)), 1326 XFS_FSS_TO_BB(mp, 1), 0, &bp); 1327 if (error) 1328 return error; 1329 bp->b_ops = &xfs_sb_buf_ops; 1330 xfs_buf_oneshot(bp); 1331 *bpp = bp; 1332 return 0; 1333 } 1334 1335 /* 1336 * sunit, swidth, sectorsize(optional with 0) should be all in bytes, so users 1337 * won't be confused by values in error messages. This function returns false 1338 * if the stripe geometry is invalid and the caller is unable to repair the 1339 * stripe configuration later in the mount process. 1340 */ 1341 bool 1342 xfs_validate_stripe_geometry( 1343 struct xfs_mount *mp, 1344 __s64 sunit, 1345 __s64 swidth, 1346 int sectorsize, 1347 bool may_repair, 1348 bool silent) 1349 { 1350 if (swidth > INT_MAX) { 1351 if (!silent) 1352 xfs_notice(mp, 1353 "stripe width (%lld) is too large", swidth); 1354 goto check_override; 1355 } 1356 1357 if (sunit > swidth) { 1358 if (!silent) 1359 xfs_notice(mp, 1360 "stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth); 1361 goto check_override; 1362 } 1363 1364 if (sectorsize && (int)sunit % sectorsize) { 1365 if (!silent) 1366 xfs_notice(mp, 1367 "stripe unit (%lld) must be a multiple of the sector size (%d)", 1368 sunit, sectorsize); 1369 goto check_override; 1370 } 1371 1372 if (sunit && !swidth) { 1373 if (!silent) 1374 xfs_notice(mp, 1375 "invalid stripe unit (%lld) and stripe width of 0", sunit); 1376 goto check_override; 1377 } 1378 1379 if (!sunit && swidth) { 1380 if (!silent) 1381 xfs_notice(mp, 1382 "invalid stripe width (%lld) and stripe unit of 0", swidth); 1383 goto check_override; 1384 } 1385 1386 if (sunit && (int)swidth % (int)sunit) { 1387 if (!silent) 1388 xfs_notice(mp, 1389 "stripe width (%lld) must be a multiple of the stripe unit (%lld)", 1390 swidth, sunit); 1391 goto check_override; 1392 } 1393 return true; 1394 1395 check_override: 1396 if (!may_repair) 1397 return false; 1398 /* 1399 * During mount, mp->m_dalign will not be set unless the sunit mount 1400 * option was set. If it was set, ignore the bad stripe alignment values 1401 * and allow the validation and overwrite later in the mount process to 1402 * attempt to overwrite the bad stripe alignment values with the values 1403 * supplied by mount options. 1404 */ 1405 if (!mp->m_dalign) 1406 return false; 1407 if (!silent) 1408 xfs_notice(mp, 1409 "Will try to correct with specified mount options sunit (%d) and swidth (%d)", 1410 BBTOB(mp->m_dalign), BBTOB(mp->m_swidth)); 1411 return true; 1412 } 1413 1414 /* 1415 * Compute the maximum level number of the realtime summary file, as defined by 1416 * mkfs. The historic use of highbit32 on a 64-bit quantity prohibited correct 1417 * use of rt volumes with more than 2^32 extents. 1418 */ 1419 uint8_t 1420 xfs_compute_rextslog( 1421 xfs_rtbxlen_t rtextents) 1422 { 1423 if (!rtextents) 1424 return 0; 1425 return xfs_highbit64(rtextents); 1426 } 1427