File Index
Files and directories
Sign in
FILE

xfs_sb.c

File preview with syntax highlighting.

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