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