xref: /kernel/linux/linux-6.6/fs/xfs/libxfs/xfs_sb.c (revision 62306a36)
162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2000-2005 Silicon Graphics, Inc.
462306a36Sopenharmony_ci * All Rights Reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#include "xfs.h"
762306a36Sopenharmony_ci#include "xfs_fs.h"
862306a36Sopenharmony_ci#include "xfs_shared.h"
962306a36Sopenharmony_ci#include "xfs_format.h"
1062306a36Sopenharmony_ci#include "xfs_log_format.h"
1162306a36Sopenharmony_ci#include "xfs_trans_resv.h"
1262306a36Sopenharmony_ci#include "xfs_bit.h"
1362306a36Sopenharmony_ci#include "xfs_sb.h"
1462306a36Sopenharmony_ci#include "xfs_mount.h"
1562306a36Sopenharmony_ci#include "xfs_ialloc.h"
1662306a36Sopenharmony_ci#include "xfs_alloc.h"
1762306a36Sopenharmony_ci#include "xfs_error.h"
1862306a36Sopenharmony_ci#include "xfs_trans.h"
1962306a36Sopenharmony_ci#include "xfs_buf_item.h"
2062306a36Sopenharmony_ci#include "xfs_bmap_btree.h"
2162306a36Sopenharmony_ci#include "xfs_alloc_btree.h"
2262306a36Sopenharmony_ci#include "xfs_log.h"
2362306a36Sopenharmony_ci#include "xfs_rmap_btree.h"
2462306a36Sopenharmony_ci#include "xfs_refcount_btree.h"
2562306a36Sopenharmony_ci#include "xfs_da_format.h"
2662306a36Sopenharmony_ci#include "xfs_health.h"
2762306a36Sopenharmony_ci#include "xfs_ag.h"
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci/*
3062306a36Sopenharmony_ci * Physical superblock buffer manipulations. Shared with libxfs in userspace.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci/*
3462306a36Sopenharmony_ci * Check that all the V4 feature bits that the V5 filesystem format requires are
3562306a36Sopenharmony_ci * correctly set.
3662306a36Sopenharmony_ci */
3762306a36Sopenharmony_cistatic bool
3862306a36Sopenharmony_cixfs_sb_validate_v5_features(
3962306a36Sopenharmony_ci	struct xfs_sb	*sbp)
4062306a36Sopenharmony_ci{
4162306a36Sopenharmony_ci	/* We must not have any unknown V4 feature bits set */
4262306a36Sopenharmony_ci	if (sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS)
4362306a36Sopenharmony_ci		return false;
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci	/*
4662306a36Sopenharmony_ci	 * The CRC bit is considered an invalid V4 flag, so we have to add it
4762306a36Sopenharmony_ci	 * manually to the OKBITS mask.
4862306a36Sopenharmony_ci	 */
4962306a36Sopenharmony_ci	if (sbp->sb_features2 & ~(XFS_SB_VERSION2_OKBITS |
5062306a36Sopenharmony_ci				  XFS_SB_VERSION2_CRCBIT))
5162306a36Sopenharmony_ci		return false;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* Now check all the required V4 feature flags are set. */
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define V5_VERS_FLAGS	(XFS_SB_VERSION_NLINKBIT	| \
5662306a36Sopenharmony_ci			XFS_SB_VERSION_ALIGNBIT		| \
5762306a36Sopenharmony_ci			XFS_SB_VERSION_LOGV2BIT		| \
5862306a36Sopenharmony_ci			XFS_SB_VERSION_EXTFLGBIT	| \
5962306a36Sopenharmony_ci			XFS_SB_VERSION_DIRV2BIT		| \
6062306a36Sopenharmony_ci			XFS_SB_VERSION_MOREBITSBIT)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define V5_FEAT_FLAGS	(XFS_SB_VERSION2_LAZYSBCOUNTBIT	| \
6362306a36Sopenharmony_ci			XFS_SB_VERSION2_ATTR2BIT	| \
6462306a36Sopenharmony_ci			XFS_SB_VERSION2_PROJID32BIT	| \
6562306a36Sopenharmony_ci			XFS_SB_VERSION2_CRCBIT)
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	if ((sbp->sb_versionnum & V5_VERS_FLAGS) != V5_VERS_FLAGS)
6862306a36Sopenharmony_ci		return false;
6962306a36Sopenharmony_ci	if ((sbp->sb_features2 & V5_FEAT_FLAGS) != V5_FEAT_FLAGS)
7062306a36Sopenharmony_ci		return false;
7162306a36Sopenharmony_ci	return true;
7262306a36Sopenharmony_ci}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci/*
7562306a36Sopenharmony_ci * We current support XFS v5 formats with known features and v4 superblocks with
7662306a36Sopenharmony_ci * at least V2 directories.
7762306a36Sopenharmony_ci */
7862306a36Sopenharmony_cibool
7962306a36Sopenharmony_cixfs_sb_good_version(
8062306a36Sopenharmony_ci	struct xfs_sb	*sbp)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	/*
8362306a36Sopenharmony_ci	 * All v5 filesystems are supported, but we must check that all the
8462306a36Sopenharmony_ci	 * required v4 feature flags are enabled correctly as the code checks
8562306a36Sopenharmony_ci	 * those flags and not for v5 support.
8662306a36Sopenharmony_ci	 */
8762306a36Sopenharmony_ci	if (xfs_sb_is_v5(sbp))
8862306a36Sopenharmony_ci		return xfs_sb_validate_v5_features(sbp);
8962306a36Sopenharmony_ci
9062306a36Sopenharmony_ci	/* versions prior to v4 are not supported */
9162306a36Sopenharmony_ci	if (XFS_SB_VERSION_NUM(sbp) != XFS_SB_VERSION_4)
9262306a36Sopenharmony_ci		return false;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* We must not have any unknown v4 feature bits set */
9562306a36Sopenharmony_ci	if ((sbp->sb_versionnum & ~XFS_SB_VERSION_OKBITS) ||
9662306a36Sopenharmony_ci	    ((sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) &&
9762306a36Sopenharmony_ci	     (sbp->sb_features2 & ~XFS_SB_VERSION2_OKBITS)))
9862306a36Sopenharmony_ci		return false;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/* V4 filesystems need v2 directories and unwritten extents */
10162306a36Sopenharmony_ci	if (!(sbp->sb_versionnum & XFS_SB_VERSION_DIRV2BIT))
10262306a36Sopenharmony_ci		return false;
10362306a36Sopenharmony_ci	if (!(sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT))
10462306a36Sopenharmony_ci		return false;
10562306a36Sopenharmony_ci
10662306a36Sopenharmony_ci	/* It's a supported v4 filesystem */
10762306a36Sopenharmony_ci	return true;
10862306a36Sopenharmony_ci}
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciuint64_t
11162306a36Sopenharmony_cixfs_sb_version_to_features(
11262306a36Sopenharmony_ci	struct xfs_sb	*sbp)
11362306a36Sopenharmony_ci{
11462306a36Sopenharmony_ci	uint64_t	features = 0;
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci	/* optional V4 features */
11762306a36Sopenharmony_ci	if (sbp->sb_rblocks > 0)
11862306a36Sopenharmony_ci		features |= XFS_FEAT_REALTIME;
11962306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_NLINKBIT)
12062306a36Sopenharmony_ci		features |= XFS_FEAT_NLINK;
12162306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_ATTRBIT)
12262306a36Sopenharmony_ci		features |= XFS_FEAT_ATTR;
12362306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_QUOTABIT)
12462306a36Sopenharmony_ci		features |= XFS_FEAT_QUOTA;
12562306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_ALIGNBIT)
12662306a36Sopenharmony_ci		features |= XFS_FEAT_ALIGN;
12762306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_LOGV2BIT)
12862306a36Sopenharmony_ci		features |= XFS_FEAT_LOGV2;
12962306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT)
13062306a36Sopenharmony_ci		features |= XFS_FEAT_DALIGN;
13162306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_EXTFLGBIT)
13262306a36Sopenharmony_ci		features |= XFS_FEAT_EXTFLG;
13362306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT)
13462306a36Sopenharmony_ci		features |= XFS_FEAT_SECTOR;
13562306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_BORGBIT)
13662306a36Sopenharmony_ci		features |= XFS_FEAT_ASCIICI;
13762306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_MOREBITSBIT) {
13862306a36Sopenharmony_ci		if (sbp->sb_features2 & XFS_SB_VERSION2_LAZYSBCOUNTBIT)
13962306a36Sopenharmony_ci			features |= XFS_FEAT_LAZYSBCOUNT;
14062306a36Sopenharmony_ci		if (sbp->sb_features2 & XFS_SB_VERSION2_ATTR2BIT)
14162306a36Sopenharmony_ci			features |= XFS_FEAT_ATTR2;
14262306a36Sopenharmony_ci		if (sbp->sb_features2 & XFS_SB_VERSION2_PROJID32BIT)
14362306a36Sopenharmony_ci			features |= XFS_FEAT_PROJID32;
14462306a36Sopenharmony_ci		if (sbp->sb_features2 & XFS_SB_VERSION2_FTYPE)
14562306a36Sopenharmony_ci			features |= XFS_FEAT_FTYPE;
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_ci	if (!xfs_sb_is_v5(sbp))
14962306a36Sopenharmony_ci		return features;
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci	/* Always on V5 features */
15262306a36Sopenharmony_ci	features |= XFS_FEAT_ALIGN | XFS_FEAT_LOGV2 | XFS_FEAT_EXTFLG |
15362306a36Sopenharmony_ci		    XFS_FEAT_LAZYSBCOUNT | XFS_FEAT_ATTR2 | XFS_FEAT_PROJID32 |
15462306a36Sopenharmony_ci		    XFS_FEAT_V3INODES | XFS_FEAT_CRC | XFS_FEAT_PQUOTINO;
15562306a36Sopenharmony_ci
15662306a36Sopenharmony_ci	/* Optional V5 features */
15762306a36Sopenharmony_ci	if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_FINOBT)
15862306a36Sopenharmony_ci		features |= XFS_FEAT_FINOBT;
15962306a36Sopenharmony_ci	if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_RMAPBT)
16062306a36Sopenharmony_ci		features |= XFS_FEAT_RMAPBT;
16162306a36Sopenharmony_ci	if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_REFLINK)
16262306a36Sopenharmony_ci		features |= XFS_FEAT_REFLINK;
16362306a36Sopenharmony_ci	if (sbp->sb_features_ro_compat & XFS_SB_FEAT_RO_COMPAT_INOBTCNT)
16462306a36Sopenharmony_ci		features |= XFS_FEAT_INOBTCNT;
16562306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_FTYPE)
16662306a36Sopenharmony_ci		features |= XFS_FEAT_FTYPE;
16762306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES)
16862306a36Sopenharmony_ci		features |= XFS_FEAT_SPINODES;
16962306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
17062306a36Sopenharmony_ci		features |= XFS_FEAT_META_UUID;
17162306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_BIGTIME)
17262306a36Sopenharmony_ci		features |= XFS_FEAT_BIGTIME;
17362306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR)
17462306a36Sopenharmony_ci		features |= XFS_FEAT_NEEDSREPAIR;
17562306a36Sopenharmony_ci	if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_NREXT64)
17662306a36Sopenharmony_ci		features |= XFS_FEAT_NREXT64;
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	return features;
17962306a36Sopenharmony_ci}
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci/* Check all the superblock fields we care about when reading one in. */
18262306a36Sopenharmony_ciSTATIC int
18362306a36Sopenharmony_cixfs_validate_sb_read(
18462306a36Sopenharmony_ci	struct xfs_mount	*mp,
18562306a36Sopenharmony_ci	struct xfs_sb		*sbp)
18662306a36Sopenharmony_ci{
18762306a36Sopenharmony_ci	if (!xfs_sb_is_v5(sbp))
18862306a36Sopenharmony_ci		return 0;
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	/*
19162306a36Sopenharmony_ci	 * Version 5 superblock feature mask validation. Reject combinations
19262306a36Sopenharmony_ci	 * the kernel cannot support up front before checking anything else.
19362306a36Sopenharmony_ci	 */
19462306a36Sopenharmony_ci	if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
19562306a36Sopenharmony_ci		xfs_warn(mp,
19662306a36Sopenharmony_ci"Superblock has unknown compatible features (0x%x) enabled.",
19762306a36Sopenharmony_ci			(sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
19862306a36Sopenharmony_ci		xfs_warn(mp,
19962306a36Sopenharmony_ci"Using a more recent kernel is recommended.");
20062306a36Sopenharmony_ci	}
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci	if (xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
20362306a36Sopenharmony_ci		xfs_alert(mp,
20462306a36Sopenharmony_ci"Superblock has unknown read-only compatible features (0x%x) enabled.",
20562306a36Sopenharmony_ci			(sbp->sb_features_ro_compat &
20662306a36Sopenharmony_ci					XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
20762306a36Sopenharmony_ci		if (!xfs_is_readonly(mp)) {
20862306a36Sopenharmony_ci			xfs_warn(mp,
20962306a36Sopenharmony_ci"Attempted to mount read-only compatible filesystem read-write.");
21062306a36Sopenharmony_ci			xfs_warn(mp,
21162306a36Sopenharmony_ci"Filesystem can only be safely mounted read only.");
21262306a36Sopenharmony_ci
21362306a36Sopenharmony_ci			return -EINVAL;
21462306a36Sopenharmony_ci		}
21562306a36Sopenharmony_ci	}
21662306a36Sopenharmony_ci	if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
21762306a36Sopenharmony_ci		xfs_warn(mp,
21862306a36Sopenharmony_ci"Superblock has unknown incompatible features (0x%x) enabled.",
21962306a36Sopenharmony_ci			(sbp->sb_features_incompat &
22062306a36Sopenharmony_ci					XFS_SB_FEAT_INCOMPAT_UNKNOWN));
22162306a36Sopenharmony_ci		xfs_warn(mp,
22262306a36Sopenharmony_ci"Filesystem cannot be safely mounted by this kernel.");
22362306a36Sopenharmony_ci		return -EINVAL;
22462306a36Sopenharmony_ci	}
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	return 0;
22762306a36Sopenharmony_ci}
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci/* Check all the superblock fields we care about when writing one out. */
23062306a36Sopenharmony_ciSTATIC int
23162306a36Sopenharmony_cixfs_validate_sb_write(
23262306a36Sopenharmony_ci	struct xfs_mount	*mp,
23362306a36Sopenharmony_ci	struct xfs_buf		*bp,
23462306a36Sopenharmony_ci	struct xfs_sb		*sbp)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	/*
23762306a36Sopenharmony_ci	 * Carry out additional sb summary counter sanity checks when we write
23862306a36Sopenharmony_ci	 * the superblock.  We skip this in the read validator because there
23962306a36Sopenharmony_ci	 * could be newer superblocks in the log and if the values are garbage
24062306a36Sopenharmony_ci	 * even after replay we'll recalculate them at the end of log mount.
24162306a36Sopenharmony_ci	 *
24262306a36Sopenharmony_ci	 * mkfs has traditionally written zeroed counters to inprogress and
24362306a36Sopenharmony_ci	 * secondary superblocks, so allow this usage to continue because
24462306a36Sopenharmony_ci	 * we never read counters from such superblocks.
24562306a36Sopenharmony_ci	 */
24662306a36Sopenharmony_ci	if (xfs_buf_daddr(bp) == XFS_SB_DADDR && !sbp->sb_inprogress &&
24762306a36Sopenharmony_ci	    (sbp->sb_fdblocks > sbp->sb_dblocks ||
24862306a36Sopenharmony_ci	     !xfs_verify_icount(mp, sbp->sb_icount) ||
24962306a36Sopenharmony_ci	     sbp->sb_ifree > sbp->sb_icount)) {
25062306a36Sopenharmony_ci		xfs_warn(mp, "SB summary counter sanity check failed");
25162306a36Sopenharmony_ci		return -EFSCORRUPTED;
25262306a36Sopenharmony_ci	}
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	if (!xfs_sb_is_v5(sbp))
25562306a36Sopenharmony_ci		return 0;
25662306a36Sopenharmony_ci
25762306a36Sopenharmony_ci	/*
25862306a36Sopenharmony_ci	 * Version 5 superblock feature mask validation. Reject combinations
25962306a36Sopenharmony_ci	 * the kernel cannot support since we checked for unsupported bits in
26062306a36Sopenharmony_ci	 * the read verifier, which means that memory is corrupt.
26162306a36Sopenharmony_ci	 */
26262306a36Sopenharmony_ci	if (xfs_sb_has_compat_feature(sbp, XFS_SB_FEAT_COMPAT_UNKNOWN)) {
26362306a36Sopenharmony_ci		xfs_warn(mp,
26462306a36Sopenharmony_ci"Corruption detected in superblock compatible features (0x%x)!",
26562306a36Sopenharmony_ci			(sbp->sb_features_compat & XFS_SB_FEAT_COMPAT_UNKNOWN));
26662306a36Sopenharmony_ci		return -EFSCORRUPTED;
26762306a36Sopenharmony_ci	}
26862306a36Sopenharmony_ci
26962306a36Sopenharmony_ci	if (!xfs_is_readonly(mp) &&
27062306a36Sopenharmony_ci	    xfs_sb_has_ro_compat_feature(sbp, XFS_SB_FEAT_RO_COMPAT_UNKNOWN)) {
27162306a36Sopenharmony_ci		xfs_alert(mp,
27262306a36Sopenharmony_ci"Corruption detected in superblock read-only compatible features (0x%x)!",
27362306a36Sopenharmony_ci			(sbp->sb_features_ro_compat &
27462306a36Sopenharmony_ci					XFS_SB_FEAT_RO_COMPAT_UNKNOWN));
27562306a36Sopenharmony_ci		return -EFSCORRUPTED;
27662306a36Sopenharmony_ci	}
27762306a36Sopenharmony_ci	if (xfs_sb_has_incompat_feature(sbp, XFS_SB_FEAT_INCOMPAT_UNKNOWN)) {
27862306a36Sopenharmony_ci		xfs_warn(mp,
27962306a36Sopenharmony_ci"Corruption detected in superblock incompatible features (0x%x)!",
28062306a36Sopenharmony_ci			(sbp->sb_features_incompat &
28162306a36Sopenharmony_ci					XFS_SB_FEAT_INCOMPAT_UNKNOWN));
28262306a36Sopenharmony_ci		return -EFSCORRUPTED;
28362306a36Sopenharmony_ci	}
28462306a36Sopenharmony_ci	if (xfs_sb_has_incompat_log_feature(sbp,
28562306a36Sopenharmony_ci			XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
28662306a36Sopenharmony_ci		xfs_warn(mp,
28762306a36Sopenharmony_ci"Corruption detected in superblock incompatible log features (0x%x)!",
28862306a36Sopenharmony_ci			(sbp->sb_features_log_incompat &
28962306a36Sopenharmony_ci					XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
29062306a36Sopenharmony_ci		return -EFSCORRUPTED;
29162306a36Sopenharmony_ci	}
29262306a36Sopenharmony_ci
29362306a36Sopenharmony_ci	/*
29462306a36Sopenharmony_ci	 * We can't read verify the sb LSN because the read verifier is called
29562306a36Sopenharmony_ci	 * before the log is allocated and processed. We know the log is set up
29662306a36Sopenharmony_ci	 * before write verifier calls, so check it here.
29762306a36Sopenharmony_ci	 */
29862306a36Sopenharmony_ci	if (!xfs_log_check_lsn(mp, sbp->sb_lsn))
29962306a36Sopenharmony_ci		return -EFSCORRUPTED;
30062306a36Sopenharmony_ci
30162306a36Sopenharmony_ci	return 0;
30262306a36Sopenharmony_ci}
30362306a36Sopenharmony_ci
30462306a36Sopenharmony_ci/* Check the validity of the SB. */
30562306a36Sopenharmony_ciSTATIC int
30662306a36Sopenharmony_cixfs_validate_sb_common(
30762306a36Sopenharmony_ci	struct xfs_mount	*mp,
30862306a36Sopenharmony_ci	struct xfs_buf		*bp,
30962306a36Sopenharmony_ci	struct xfs_sb		*sbp)
31062306a36Sopenharmony_ci{
31162306a36Sopenharmony_ci	struct xfs_dsb		*dsb = bp->b_addr;
31262306a36Sopenharmony_ci	uint32_t		agcount = 0;
31362306a36Sopenharmony_ci	uint32_t		rem;
31462306a36Sopenharmony_ci	bool			has_dalign;
31562306a36Sopenharmony_ci
31662306a36Sopenharmony_ci	if (!xfs_verify_magic(bp, dsb->sb_magicnum)) {
31762306a36Sopenharmony_ci		xfs_warn(mp,
31862306a36Sopenharmony_ci"Superblock has bad magic number 0x%x. Not an XFS filesystem?",
31962306a36Sopenharmony_ci			be32_to_cpu(dsb->sb_magicnum));
32062306a36Sopenharmony_ci		return -EWRONGFS;
32162306a36Sopenharmony_ci	}
32262306a36Sopenharmony_ci
32362306a36Sopenharmony_ci	if (!xfs_sb_good_version(sbp)) {
32462306a36Sopenharmony_ci		xfs_warn(mp,
32562306a36Sopenharmony_ci"Superblock has unknown features enabled or corrupted feature masks.");
32662306a36Sopenharmony_ci		return -EWRONGFS;
32762306a36Sopenharmony_ci	}
32862306a36Sopenharmony_ci
32962306a36Sopenharmony_ci	/*
33062306a36Sopenharmony_ci	 * Validate feature flags and state
33162306a36Sopenharmony_ci	 */
33262306a36Sopenharmony_ci	if (xfs_sb_is_v5(sbp)) {
33362306a36Sopenharmony_ci		if (sbp->sb_blocksize < XFS_MIN_CRC_BLOCKSIZE) {
33462306a36Sopenharmony_ci			xfs_notice(mp,
33562306a36Sopenharmony_ci"Block size (%u bytes) too small for Version 5 superblock (minimum %d bytes)",
33662306a36Sopenharmony_ci				sbp->sb_blocksize, XFS_MIN_CRC_BLOCKSIZE);
33762306a36Sopenharmony_ci			return -EFSCORRUPTED;
33862306a36Sopenharmony_ci		}
33962306a36Sopenharmony_ci
34062306a36Sopenharmony_ci		/* V5 has a separate project quota inode */
34162306a36Sopenharmony_ci		if (sbp->sb_qflags & (XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD)) {
34262306a36Sopenharmony_ci			xfs_notice(mp,
34362306a36Sopenharmony_ci			   "Version 5 of Super block has XFS_OQUOTA bits.");
34462306a36Sopenharmony_ci			return -EFSCORRUPTED;
34562306a36Sopenharmony_ci		}
34662306a36Sopenharmony_ci
34762306a36Sopenharmony_ci		/*
34862306a36Sopenharmony_ci		 * Full inode chunks must be aligned to inode chunk size when
34962306a36Sopenharmony_ci		 * sparse inodes are enabled to support the sparse chunk
35062306a36Sopenharmony_ci		 * allocation algorithm and prevent overlapping inode records.
35162306a36Sopenharmony_ci		 */
35262306a36Sopenharmony_ci		if (sbp->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_SPINODES) {
35362306a36Sopenharmony_ci			uint32_t	align;
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_ci			align = XFS_INODES_PER_CHUNK * sbp->sb_inodesize
35662306a36Sopenharmony_ci					>> sbp->sb_blocklog;
35762306a36Sopenharmony_ci			if (sbp->sb_inoalignmt != align) {
35862306a36Sopenharmony_ci				xfs_warn(mp,
35962306a36Sopenharmony_ci"Inode block alignment (%u) must match chunk size (%u) for sparse inodes.",
36062306a36Sopenharmony_ci					 sbp->sb_inoalignmt, align);
36162306a36Sopenharmony_ci				return -EINVAL;
36262306a36Sopenharmony_ci			}
36362306a36Sopenharmony_ci		}
36462306a36Sopenharmony_ci	} else if (sbp->sb_qflags & (XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD |
36562306a36Sopenharmony_ci				XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD)) {
36662306a36Sopenharmony_ci			xfs_notice(mp,
36762306a36Sopenharmony_ci"Superblock earlier than Version 5 has XFS_{P|G}QUOTA_{ENFD|CHKD} bits.");
36862306a36Sopenharmony_ci			return -EFSCORRUPTED;
36962306a36Sopenharmony_ci	}
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	if (unlikely(
37262306a36Sopenharmony_ci	    sbp->sb_logstart == 0 && mp->m_logdev_targp == mp->m_ddev_targp)) {
37362306a36Sopenharmony_ci		xfs_warn(mp,
37462306a36Sopenharmony_ci		"filesystem is marked as having an external log; "
37562306a36Sopenharmony_ci		"specify logdev on the mount command line.");
37662306a36Sopenharmony_ci		return -EINVAL;
37762306a36Sopenharmony_ci	}
37862306a36Sopenharmony_ci
37962306a36Sopenharmony_ci	if (unlikely(
38062306a36Sopenharmony_ci	    sbp->sb_logstart != 0 && mp->m_logdev_targp != mp->m_ddev_targp)) {
38162306a36Sopenharmony_ci		xfs_warn(mp,
38262306a36Sopenharmony_ci		"filesystem is marked as having an internal log; "
38362306a36Sopenharmony_ci		"do not specify logdev on the mount command line.");
38462306a36Sopenharmony_ci		return -EINVAL;
38562306a36Sopenharmony_ci	}
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci	/* Compute agcount for this number of dblocks and agblocks */
38862306a36Sopenharmony_ci	if (sbp->sb_agblocks) {
38962306a36Sopenharmony_ci		agcount = div_u64_rem(sbp->sb_dblocks, sbp->sb_agblocks, &rem);
39062306a36Sopenharmony_ci		if (rem)
39162306a36Sopenharmony_ci			agcount++;
39262306a36Sopenharmony_ci	}
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci	/*
39562306a36Sopenharmony_ci	 * More sanity checking.  Most of these were stolen directly from
39662306a36Sopenharmony_ci	 * xfs_repair.
39762306a36Sopenharmony_ci	 */
39862306a36Sopenharmony_ci	if (unlikely(
39962306a36Sopenharmony_ci	    sbp->sb_agcount <= 0					||
40062306a36Sopenharmony_ci	    sbp->sb_sectsize < XFS_MIN_SECTORSIZE			||
40162306a36Sopenharmony_ci	    sbp->sb_sectsize > XFS_MAX_SECTORSIZE			||
40262306a36Sopenharmony_ci	    sbp->sb_sectlog < XFS_MIN_SECTORSIZE_LOG			||
40362306a36Sopenharmony_ci	    sbp->sb_sectlog > XFS_MAX_SECTORSIZE_LOG			||
40462306a36Sopenharmony_ci	    sbp->sb_sectsize != (1 << sbp->sb_sectlog)			||
40562306a36Sopenharmony_ci	    sbp->sb_blocksize < XFS_MIN_BLOCKSIZE			||
40662306a36Sopenharmony_ci	    sbp->sb_blocksize > XFS_MAX_BLOCKSIZE			||
40762306a36Sopenharmony_ci	    sbp->sb_blocklog < XFS_MIN_BLOCKSIZE_LOG			||
40862306a36Sopenharmony_ci	    sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG			||
40962306a36Sopenharmony_ci	    sbp->sb_blocksize != (1 << sbp->sb_blocklog)		||
41062306a36Sopenharmony_ci	    sbp->sb_dirblklog + sbp->sb_blocklog > XFS_MAX_BLOCKSIZE_LOG ||
41162306a36Sopenharmony_ci	    sbp->sb_inodesize < XFS_DINODE_MIN_SIZE			||
41262306a36Sopenharmony_ci	    sbp->sb_inodesize > XFS_DINODE_MAX_SIZE			||
41362306a36Sopenharmony_ci	    sbp->sb_inodelog < XFS_DINODE_MIN_LOG			||
41462306a36Sopenharmony_ci	    sbp->sb_inodelog > XFS_DINODE_MAX_LOG			||
41562306a36Sopenharmony_ci	    sbp->sb_inodesize != (1 << sbp->sb_inodelog)		||
41662306a36Sopenharmony_ci	    sbp->sb_inopblock != howmany(sbp->sb_blocksize,sbp->sb_inodesize) ||
41762306a36Sopenharmony_ci	    XFS_FSB_TO_B(mp, sbp->sb_agblocks) < XFS_MIN_AG_BYTES	||
41862306a36Sopenharmony_ci	    XFS_FSB_TO_B(mp, sbp->sb_agblocks) > XFS_MAX_AG_BYTES	||
41962306a36Sopenharmony_ci	    sbp->sb_agblklog != xfs_highbit32(sbp->sb_agblocks - 1) + 1	||
42062306a36Sopenharmony_ci	    agcount == 0 || agcount != sbp->sb_agcount			||
42162306a36Sopenharmony_ci	    (sbp->sb_blocklog - sbp->sb_inodelog != sbp->sb_inopblog)	||
42262306a36Sopenharmony_ci	    (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE)	||
42362306a36Sopenharmony_ci	    (sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE)	||
42462306a36Sopenharmony_ci	    (sbp->sb_imax_pct > 100 /* zero sb_imax_pct is valid */)	||
42562306a36Sopenharmony_ci	    sbp->sb_dblocks == 0					||
42662306a36Sopenharmony_ci	    sbp->sb_dblocks > XFS_MAX_DBLOCKS(sbp)			||
42762306a36Sopenharmony_ci	    sbp->sb_dblocks < XFS_MIN_DBLOCKS(sbp)			||
42862306a36Sopenharmony_ci	    sbp->sb_shared_vn != 0)) {
42962306a36Sopenharmony_ci		xfs_notice(mp, "SB sanity check failed");
43062306a36Sopenharmony_ci		return -EFSCORRUPTED;
43162306a36Sopenharmony_ci	}
43262306a36Sopenharmony_ci
43362306a36Sopenharmony_ci	/*
43462306a36Sopenharmony_ci	 * Logs that are too large are not supported at all. Reject them
43562306a36Sopenharmony_ci	 * outright. Logs that are too small are tolerated on v4 filesystems,
43662306a36Sopenharmony_ci	 * but we can only check that when mounting the log. Hence we skip
43762306a36Sopenharmony_ci	 * those checks here.
43862306a36Sopenharmony_ci	 */
43962306a36Sopenharmony_ci	if (sbp->sb_logblocks > XFS_MAX_LOG_BLOCKS) {
44062306a36Sopenharmony_ci		xfs_notice(mp,
44162306a36Sopenharmony_ci		"Log size 0x%x blocks too large, maximum size is 0x%llx blocks",
44262306a36Sopenharmony_ci			 sbp->sb_logblocks, XFS_MAX_LOG_BLOCKS);
44362306a36Sopenharmony_ci		return -EFSCORRUPTED;
44462306a36Sopenharmony_ci	}
44562306a36Sopenharmony_ci
44662306a36Sopenharmony_ci	if (XFS_FSB_TO_B(mp, sbp->sb_logblocks) > XFS_MAX_LOG_BYTES) {
44762306a36Sopenharmony_ci		xfs_warn(mp,
44862306a36Sopenharmony_ci		"log size 0x%llx bytes too large, maximum size is 0x%llx bytes",
44962306a36Sopenharmony_ci			 XFS_FSB_TO_B(mp, sbp->sb_logblocks),
45062306a36Sopenharmony_ci			 XFS_MAX_LOG_BYTES);
45162306a36Sopenharmony_ci		return -EFSCORRUPTED;
45262306a36Sopenharmony_ci	}
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_ci	/*
45562306a36Sopenharmony_ci	 * Do not allow filesystems with corrupted log sector or stripe units to
45662306a36Sopenharmony_ci	 * be mounted. We cannot safely size the iclogs or write to the log if
45762306a36Sopenharmony_ci	 * the log stripe unit is not valid.
45862306a36Sopenharmony_ci	 */
45962306a36Sopenharmony_ci	if (sbp->sb_versionnum & XFS_SB_VERSION_SECTORBIT) {
46062306a36Sopenharmony_ci		if (sbp->sb_logsectsize != (1U << sbp->sb_logsectlog)) {
46162306a36Sopenharmony_ci			xfs_notice(mp,
46262306a36Sopenharmony_ci			"log sector size in bytes/log2 (0x%x/0x%x) must match",
46362306a36Sopenharmony_ci				sbp->sb_logsectsize, 1U << sbp->sb_logsectlog);
46462306a36Sopenharmony_ci			return -EFSCORRUPTED;
46562306a36Sopenharmony_ci		}
46662306a36Sopenharmony_ci	} else if (sbp->sb_logsectsize || sbp->sb_logsectlog) {
46762306a36Sopenharmony_ci		xfs_notice(mp,
46862306a36Sopenharmony_ci		"log sector size in bytes/log2 (0x%x/0x%x) are not zero",
46962306a36Sopenharmony_ci			sbp->sb_logsectsize, sbp->sb_logsectlog);
47062306a36Sopenharmony_ci		return -EFSCORRUPTED;
47162306a36Sopenharmony_ci	}
47262306a36Sopenharmony_ci
47362306a36Sopenharmony_ci	if (sbp->sb_logsunit > 1) {
47462306a36Sopenharmony_ci		if (sbp->sb_logsunit % sbp->sb_blocksize) {
47562306a36Sopenharmony_ci			xfs_notice(mp,
47662306a36Sopenharmony_ci		"log stripe unit 0x%x bytes must be a multiple of block size",
47762306a36Sopenharmony_ci				sbp->sb_logsunit);
47862306a36Sopenharmony_ci			return -EFSCORRUPTED;
47962306a36Sopenharmony_ci		}
48062306a36Sopenharmony_ci		if (sbp->sb_logsunit > XLOG_MAX_RECORD_BSIZE) {
48162306a36Sopenharmony_ci			xfs_notice(mp,
48262306a36Sopenharmony_ci		"log stripe unit 0x%x bytes over maximum size (0x%x bytes)",
48362306a36Sopenharmony_ci				sbp->sb_logsunit, XLOG_MAX_RECORD_BSIZE);
48462306a36Sopenharmony_ci			return -EFSCORRUPTED;
48562306a36Sopenharmony_ci		}
48662306a36Sopenharmony_ci	}
48762306a36Sopenharmony_ci
48862306a36Sopenharmony_ci	/* Validate the realtime geometry; stolen from xfs_repair */
48962306a36Sopenharmony_ci	if (sbp->sb_rextsize * sbp->sb_blocksize > XFS_MAX_RTEXTSIZE ||
49062306a36Sopenharmony_ci	    sbp->sb_rextsize * sbp->sb_blocksize < XFS_MIN_RTEXTSIZE) {
49162306a36Sopenharmony_ci		xfs_notice(mp,
49262306a36Sopenharmony_ci			"realtime extent sanity check failed");
49362306a36Sopenharmony_ci		return -EFSCORRUPTED;
49462306a36Sopenharmony_ci	}
49562306a36Sopenharmony_ci
49662306a36Sopenharmony_ci	if (sbp->sb_rblocks == 0) {
49762306a36Sopenharmony_ci		if (sbp->sb_rextents != 0 || sbp->sb_rbmblocks != 0 ||
49862306a36Sopenharmony_ci		    sbp->sb_rextslog != 0 || sbp->sb_frextents != 0) {
49962306a36Sopenharmony_ci			xfs_notice(mp,
50062306a36Sopenharmony_ci				"realtime zeroed geometry check failed");
50162306a36Sopenharmony_ci			return -EFSCORRUPTED;
50262306a36Sopenharmony_ci		}
50362306a36Sopenharmony_ci	} else {
50462306a36Sopenharmony_ci		uint64_t	rexts;
50562306a36Sopenharmony_ci		uint64_t	rbmblocks;
50662306a36Sopenharmony_ci
50762306a36Sopenharmony_ci		rexts = div_u64(sbp->sb_rblocks, sbp->sb_rextsize);
50862306a36Sopenharmony_ci		rbmblocks = howmany_64(sbp->sb_rextents,
50962306a36Sopenharmony_ci				       NBBY * sbp->sb_blocksize);
51062306a36Sopenharmony_ci
51162306a36Sopenharmony_ci		if (sbp->sb_rextents != rexts ||
51262306a36Sopenharmony_ci		    sbp->sb_rextslog != xfs_highbit32(sbp->sb_rextents) ||
51362306a36Sopenharmony_ci		    sbp->sb_rbmblocks != rbmblocks) {
51462306a36Sopenharmony_ci			xfs_notice(mp,
51562306a36Sopenharmony_ci				"realtime geometry sanity check failed");
51662306a36Sopenharmony_ci			return -EFSCORRUPTED;
51762306a36Sopenharmony_ci		}
51862306a36Sopenharmony_ci	}
51962306a36Sopenharmony_ci
52062306a36Sopenharmony_ci	/*
52162306a36Sopenharmony_ci	 * Either (sb_unit and !hasdalign) or (!sb_unit and hasdalign)
52262306a36Sopenharmony_ci	 * would imply the image is corrupted.
52362306a36Sopenharmony_ci	 */
52462306a36Sopenharmony_ci	has_dalign = sbp->sb_versionnum & XFS_SB_VERSION_DALIGNBIT;
52562306a36Sopenharmony_ci	if (!!sbp->sb_unit ^ has_dalign) {
52662306a36Sopenharmony_ci		xfs_notice(mp, "SB stripe alignment sanity check failed");
52762306a36Sopenharmony_ci		return -EFSCORRUPTED;
52862306a36Sopenharmony_ci	}
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	if (!xfs_validate_stripe_geometry(mp, XFS_FSB_TO_B(mp, sbp->sb_unit),
53162306a36Sopenharmony_ci			XFS_FSB_TO_B(mp, sbp->sb_width), 0, false))
53262306a36Sopenharmony_ci		return -EFSCORRUPTED;
53362306a36Sopenharmony_ci
53462306a36Sopenharmony_ci	/*
53562306a36Sopenharmony_ci	 * Currently only very few inode sizes are supported.
53662306a36Sopenharmony_ci	 */
53762306a36Sopenharmony_ci	switch (sbp->sb_inodesize) {
53862306a36Sopenharmony_ci	case 256:
53962306a36Sopenharmony_ci	case 512:
54062306a36Sopenharmony_ci	case 1024:
54162306a36Sopenharmony_ci	case 2048:
54262306a36Sopenharmony_ci		break;
54362306a36Sopenharmony_ci	default:
54462306a36Sopenharmony_ci		xfs_warn(mp, "inode size of %d bytes not supported",
54562306a36Sopenharmony_ci				sbp->sb_inodesize);
54662306a36Sopenharmony_ci		return -ENOSYS;
54762306a36Sopenharmony_ci	}
54862306a36Sopenharmony_ci
54962306a36Sopenharmony_ci	return 0;
55062306a36Sopenharmony_ci}
55162306a36Sopenharmony_ci
55262306a36Sopenharmony_civoid
55362306a36Sopenharmony_cixfs_sb_quota_from_disk(struct xfs_sb *sbp)
55462306a36Sopenharmony_ci{
55562306a36Sopenharmony_ci	/*
55662306a36Sopenharmony_ci	 * older mkfs doesn't initialize quota inodes to NULLFSINO. This
55762306a36Sopenharmony_ci	 * leads to in-core values having two different values for a quota
55862306a36Sopenharmony_ci	 * inode to be invalid: 0 and NULLFSINO. Change it to a single value
55962306a36Sopenharmony_ci	 * NULLFSINO.
56062306a36Sopenharmony_ci	 *
56162306a36Sopenharmony_ci	 * Note that this change affect only the in-core values. These
56262306a36Sopenharmony_ci	 * values are not written back to disk unless any quota information
56362306a36Sopenharmony_ci	 * is written to the disk. Even in that case, sb_pquotino field is
56462306a36Sopenharmony_ci	 * not written to disk unless the superblock supports pquotino.
56562306a36Sopenharmony_ci	 */
56662306a36Sopenharmony_ci	if (sbp->sb_uquotino == 0)
56762306a36Sopenharmony_ci		sbp->sb_uquotino = NULLFSINO;
56862306a36Sopenharmony_ci	if (sbp->sb_gquotino == 0)
56962306a36Sopenharmony_ci		sbp->sb_gquotino = NULLFSINO;
57062306a36Sopenharmony_ci	if (sbp->sb_pquotino == 0)
57162306a36Sopenharmony_ci		sbp->sb_pquotino = NULLFSINO;
57262306a36Sopenharmony_ci
57362306a36Sopenharmony_ci	/*
57462306a36Sopenharmony_ci	 * We need to do these manipilations only if we are working
57562306a36Sopenharmony_ci	 * with an older version of on-disk superblock.
57662306a36Sopenharmony_ci	 */
57762306a36Sopenharmony_ci	if (xfs_sb_is_v5(sbp))
57862306a36Sopenharmony_ci		return;
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci	if (sbp->sb_qflags & XFS_OQUOTA_ENFD)
58162306a36Sopenharmony_ci		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
58262306a36Sopenharmony_ci					XFS_PQUOTA_ENFD : XFS_GQUOTA_ENFD;
58362306a36Sopenharmony_ci	if (sbp->sb_qflags & XFS_OQUOTA_CHKD)
58462306a36Sopenharmony_ci		sbp->sb_qflags |= (sbp->sb_qflags & XFS_PQUOTA_ACCT) ?
58562306a36Sopenharmony_ci					XFS_PQUOTA_CHKD : XFS_GQUOTA_CHKD;
58662306a36Sopenharmony_ci	sbp->sb_qflags &= ~(XFS_OQUOTA_ENFD | XFS_OQUOTA_CHKD);
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	if (sbp->sb_qflags & XFS_PQUOTA_ACCT &&
58962306a36Sopenharmony_ci	    sbp->sb_gquotino != NULLFSINO)  {
59062306a36Sopenharmony_ci		/*
59162306a36Sopenharmony_ci		 * In older version of superblock, on-disk superblock only
59262306a36Sopenharmony_ci		 * has sb_gquotino, and in-core superblock has both sb_gquotino
59362306a36Sopenharmony_ci		 * and sb_pquotino. But, only one of them is supported at any
59462306a36Sopenharmony_ci		 * point of time. So, if PQUOTA is set in disk superblock,
59562306a36Sopenharmony_ci		 * copy over sb_gquotino to sb_pquotino.  The NULLFSINO test
59662306a36Sopenharmony_ci		 * above is to make sure we don't do this twice and wipe them
59762306a36Sopenharmony_ci		 * both out!
59862306a36Sopenharmony_ci		 */
59962306a36Sopenharmony_ci		sbp->sb_pquotino = sbp->sb_gquotino;
60062306a36Sopenharmony_ci		sbp->sb_gquotino = NULLFSINO;
60162306a36Sopenharmony_ci	}
60262306a36Sopenharmony_ci}
60362306a36Sopenharmony_ci
60462306a36Sopenharmony_cistatic void
60562306a36Sopenharmony_ci__xfs_sb_from_disk(
60662306a36Sopenharmony_ci	struct xfs_sb	*to,
60762306a36Sopenharmony_ci	struct xfs_dsb	*from,
60862306a36Sopenharmony_ci	bool		convert_xquota)
60962306a36Sopenharmony_ci{
61062306a36Sopenharmony_ci	to->sb_magicnum = be32_to_cpu(from->sb_magicnum);
61162306a36Sopenharmony_ci	to->sb_blocksize = be32_to_cpu(from->sb_blocksize);
61262306a36Sopenharmony_ci	to->sb_dblocks = be64_to_cpu(from->sb_dblocks);
61362306a36Sopenharmony_ci	to->sb_rblocks = be64_to_cpu(from->sb_rblocks);
61462306a36Sopenharmony_ci	to->sb_rextents = be64_to_cpu(from->sb_rextents);
61562306a36Sopenharmony_ci	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
61662306a36Sopenharmony_ci	to->sb_logstart = be64_to_cpu(from->sb_logstart);
61762306a36Sopenharmony_ci	to->sb_rootino = be64_to_cpu(from->sb_rootino);
61862306a36Sopenharmony_ci	to->sb_rbmino = be64_to_cpu(from->sb_rbmino);
61962306a36Sopenharmony_ci	to->sb_rsumino = be64_to_cpu(from->sb_rsumino);
62062306a36Sopenharmony_ci	to->sb_rextsize = be32_to_cpu(from->sb_rextsize);
62162306a36Sopenharmony_ci	to->sb_agblocks = be32_to_cpu(from->sb_agblocks);
62262306a36Sopenharmony_ci	to->sb_agcount = be32_to_cpu(from->sb_agcount);
62362306a36Sopenharmony_ci	to->sb_rbmblocks = be32_to_cpu(from->sb_rbmblocks);
62462306a36Sopenharmony_ci	to->sb_logblocks = be32_to_cpu(from->sb_logblocks);
62562306a36Sopenharmony_ci	to->sb_versionnum = be16_to_cpu(from->sb_versionnum);
62662306a36Sopenharmony_ci	to->sb_sectsize = be16_to_cpu(from->sb_sectsize);
62762306a36Sopenharmony_ci	to->sb_inodesize = be16_to_cpu(from->sb_inodesize);
62862306a36Sopenharmony_ci	to->sb_inopblock = be16_to_cpu(from->sb_inopblock);
62962306a36Sopenharmony_ci	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
63062306a36Sopenharmony_ci	to->sb_blocklog = from->sb_blocklog;
63162306a36Sopenharmony_ci	to->sb_sectlog = from->sb_sectlog;
63262306a36Sopenharmony_ci	to->sb_inodelog = from->sb_inodelog;
63362306a36Sopenharmony_ci	to->sb_inopblog = from->sb_inopblog;
63462306a36Sopenharmony_ci	to->sb_agblklog = from->sb_agblklog;
63562306a36Sopenharmony_ci	to->sb_rextslog = from->sb_rextslog;
63662306a36Sopenharmony_ci	to->sb_inprogress = from->sb_inprogress;
63762306a36Sopenharmony_ci	to->sb_imax_pct = from->sb_imax_pct;
63862306a36Sopenharmony_ci	to->sb_icount = be64_to_cpu(from->sb_icount);
63962306a36Sopenharmony_ci	to->sb_ifree = be64_to_cpu(from->sb_ifree);
64062306a36Sopenharmony_ci	to->sb_fdblocks = be64_to_cpu(from->sb_fdblocks);
64162306a36Sopenharmony_ci	to->sb_frextents = be64_to_cpu(from->sb_frextents);
64262306a36Sopenharmony_ci	to->sb_uquotino = be64_to_cpu(from->sb_uquotino);
64362306a36Sopenharmony_ci	to->sb_gquotino = be64_to_cpu(from->sb_gquotino);
64462306a36Sopenharmony_ci	to->sb_qflags = be16_to_cpu(from->sb_qflags);
64562306a36Sopenharmony_ci	to->sb_flags = from->sb_flags;
64662306a36Sopenharmony_ci	to->sb_shared_vn = from->sb_shared_vn;
64762306a36Sopenharmony_ci	to->sb_inoalignmt = be32_to_cpu(from->sb_inoalignmt);
64862306a36Sopenharmony_ci	to->sb_unit = be32_to_cpu(from->sb_unit);
64962306a36Sopenharmony_ci	to->sb_width = be32_to_cpu(from->sb_width);
65062306a36Sopenharmony_ci	to->sb_dirblklog = from->sb_dirblklog;
65162306a36Sopenharmony_ci	to->sb_logsectlog = from->sb_logsectlog;
65262306a36Sopenharmony_ci	to->sb_logsectsize = be16_to_cpu(from->sb_logsectsize);
65362306a36Sopenharmony_ci	to->sb_logsunit = be32_to_cpu(from->sb_logsunit);
65462306a36Sopenharmony_ci	to->sb_features2 = be32_to_cpu(from->sb_features2);
65562306a36Sopenharmony_ci	to->sb_bad_features2 = be32_to_cpu(from->sb_bad_features2);
65662306a36Sopenharmony_ci	to->sb_features_compat = be32_to_cpu(from->sb_features_compat);
65762306a36Sopenharmony_ci	to->sb_features_ro_compat = be32_to_cpu(from->sb_features_ro_compat);
65862306a36Sopenharmony_ci	to->sb_features_incompat = be32_to_cpu(from->sb_features_incompat);
65962306a36Sopenharmony_ci	to->sb_features_log_incompat =
66062306a36Sopenharmony_ci				be32_to_cpu(from->sb_features_log_incompat);
66162306a36Sopenharmony_ci	/* crc is only used on disk, not in memory; just init to 0 here. */
66262306a36Sopenharmony_ci	to->sb_crc = 0;
66362306a36Sopenharmony_ci	to->sb_spino_align = be32_to_cpu(from->sb_spino_align);
66462306a36Sopenharmony_ci	to->sb_pquotino = be64_to_cpu(from->sb_pquotino);
66562306a36Sopenharmony_ci	to->sb_lsn = be64_to_cpu(from->sb_lsn);
66662306a36Sopenharmony_ci	/*
66762306a36Sopenharmony_ci	 * sb_meta_uuid is only on disk if it differs from sb_uuid and the
66862306a36Sopenharmony_ci	 * feature flag is set; if not set we keep it only in memory.
66962306a36Sopenharmony_ci	 */
67062306a36Sopenharmony_ci	if (xfs_sb_is_v5(to) &&
67162306a36Sopenharmony_ci	    (to->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID))
67262306a36Sopenharmony_ci		uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
67362306a36Sopenharmony_ci	else
67462306a36Sopenharmony_ci		uuid_copy(&to->sb_meta_uuid, &from->sb_uuid);
67562306a36Sopenharmony_ci	/* Convert on-disk flags to in-memory flags? */
67662306a36Sopenharmony_ci	if (convert_xquota)
67762306a36Sopenharmony_ci		xfs_sb_quota_from_disk(to);
67862306a36Sopenharmony_ci}
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_civoid
68162306a36Sopenharmony_cixfs_sb_from_disk(
68262306a36Sopenharmony_ci	struct xfs_sb	*to,
68362306a36Sopenharmony_ci	struct xfs_dsb	*from)
68462306a36Sopenharmony_ci{
68562306a36Sopenharmony_ci	__xfs_sb_from_disk(to, from, true);
68662306a36Sopenharmony_ci}
68762306a36Sopenharmony_ci
68862306a36Sopenharmony_cistatic void
68962306a36Sopenharmony_cixfs_sb_quota_to_disk(
69062306a36Sopenharmony_ci	struct xfs_dsb	*to,
69162306a36Sopenharmony_ci	struct xfs_sb	*from)
69262306a36Sopenharmony_ci{
69362306a36Sopenharmony_ci	uint16_t	qflags = from->sb_qflags;
69462306a36Sopenharmony_ci
69562306a36Sopenharmony_ci	to->sb_uquotino = cpu_to_be64(from->sb_uquotino);
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci	/*
69862306a36Sopenharmony_ci	 * The in-memory superblock quota state matches the v5 on-disk format so
69962306a36Sopenharmony_ci	 * just write them out and return
70062306a36Sopenharmony_ci	 */
70162306a36Sopenharmony_ci	if (xfs_sb_is_v5(from)) {
70262306a36Sopenharmony_ci		to->sb_qflags = cpu_to_be16(from->sb_qflags);
70362306a36Sopenharmony_ci		to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
70462306a36Sopenharmony_ci		to->sb_pquotino = cpu_to_be64(from->sb_pquotino);
70562306a36Sopenharmony_ci		return;
70662306a36Sopenharmony_ci	}
70762306a36Sopenharmony_ci
70862306a36Sopenharmony_ci	/*
70962306a36Sopenharmony_ci	 * For older superblocks (v4), the in-core version of sb_qflags do not
71062306a36Sopenharmony_ci	 * have XFS_OQUOTA_* flags, whereas the on-disk version does.  So,
71162306a36Sopenharmony_ci	 * convert incore XFS_{PG}QUOTA_* flags to on-disk XFS_OQUOTA_* flags.
71262306a36Sopenharmony_ci	 */
71362306a36Sopenharmony_ci	qflags &= ~(XFS_PQUOTA_ENFD | XFS_PQUOTA_CHKD |
71462306a36Sopenharmony_ci			XFS_GQUOTA_ENFD | XFS_GQUOTA_CHKD);
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci	if (from->sb_qflags &
71762306a36Sopenharmony_ci			(XFS_PQUOTA_ENFD | XFS_GQUOTA_ENFD))
71862306a36Sopenharmony_ci		qflags |= XFS_OQUOTA_ENFD;
71962306a36Sopenharmony_ci	if (from->sb_qflags &
72062306a36Sopenharmony_ci			(XFS_PQUOTA_CHKD | XFS_GQUOTA_CHKD))
72162306a36Sopenharmony_ci		qflags |= XFS_OQUOTA_CHKD;
72262306a36Sopenharmony_ci	to->sb_qflags = cpu_to_be16(qflags);
72362306a36Sopenharmony_ci
72462306a36Sopenharmony_ci	/*
72562306a36Sopenharmony_ci	 * GQUOTINO and PQUOTINO cannot be used together in versions
72662306a36Sopenharmony_ci	 * of superblock that do not have pquotino. from->sb_flags
72762306a36Sopenharmony_ci	 * tells us which quota is active and should be copied to
72862306a36Sopenharmony_ci	 * disk. If neither are active, we should NULL the inode.
72962306a36Sopenharmony_ci	 *
73062306a36Sopenharmony_ci	 * In all cases, the separate pquotino must remain 0 because it
73162306a36Sopenharmony_ci	 * is beyond the "end" of the valid non-pquotino superblock.
73262306a36Sopenharmony_ci	 */
73362306a36Sopenharmony_ci	if (from->sb_qflags & XFS_GQUOTA_ACCT)
73462306a36Sopenharmony_ci		to->sb_gquotino = cpu_to_be64(from->sb_gquotino);
73562306a36Sopenharmony_ci	else if (from->sb_qflags & XFS_PQUOTA_ACCT)
73662306a36Sopenharmony_ci		to->sb_gquotino = cpu_to_be64(from->sb_pquotino);
73762306a36Sopenharmony_ci	else {
73862306a36Sopenharmony_ci		/*
73962306a36Sopenharmony_ci		 * We can't rely on just the fields being logged to tell us
74062306a36Sopenharmony_ci		 * that it is safe to write NULLFSINO - we should only do that
74162306a36Sopenharmony_ci		 * if quotas are not actually enabled. Hence only write
74262306a36Sopenharmony_ci		 * NULLFSINO if both in-core quota inodes are NULL.
74362306a36Sopenharmony_ci		 */
74462306a36Sopenharmony_ci		if (from->sb_gquotino == NULLFSINO &&
74562306a36Sopenharmony_ci		    from->sb_pquotino == NULLFSINO)
74662306a36Sopenharmony_ci			to->sb_gquotino = cpu_to_be64(NULLFSINO);
74762306a36Sopenharmony_ci	}
74862306a36Sopenharmony_ci
74962306a36Sopenharmony_ci	to->sb_pquotino = 0;
75062306a36Sopenharmony_ci}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_civoid
75362306a36Sopenharmony_cixfs_sb_to_disk(
75462306a36Sopenharmony_ci	struct xfs_dsb	*to,
75562306a36Sopenharmony_ci	struct xfs_sb	*from)
75662306a36Sopenharmony_ci{
75762306a36Sopenharmony_ci	xfs_sb_quota_to_disk(to, from);
75862306a36Sopenharmony_ci
75962306a36Sopenharmony_ci	to->sb_magicnum = cpu_to_be32(from->sb_magicnum);
76062306a36Sopenharmony_ci	to->sb_blocksize = cpu_to_be32(from->sb_blocksize);
76162306a36Sopenharmony_ci	to->sb_dblocks = cpu_to_be64(from->sb_dblocks);
76262306a36Sopenharmony_ci	to->sb_rblocks = cpu_to_be64(from->sb_rblocks);
76362306a36Sopenharmony_ci	to->sb_rextents = cpu_to_be64(from->sb_rextents);
76462306a36Sopenharmony_ci	memcpy(&to->sb_uuid, &from->sb_uuid, sizeof(to->sb_uuid));
76562306a36Sopenharmony_ci	to->sb_logstart = cpu_to_be64(from->sb_logstart);
76662306a36Sopenharmony_ci	to->sb_rootino = cpu_to_be64(from->sb_rootino);
76762306a36Sopenharmony_ci	to->sb_rbmino = cpu_to_be64(from->sb_rbmino);
76862306a36Sopenharmony_ci	to->sb_rsumino = cpu_to_be64(from->sb_rsumino);
76962306a36Sopenharmony_ci	to->sb_rextsize = cpu_to_be32(from->sb_rextsize);
77062306a36Sopenharmony_ci	to->sb_agblocks = cpu_to_be32(from->sb_agblocks);
77162306a36Sopenharmony_ci	to->sb_agcount = cpu_to_be32(from->sb_agcount);
77262306a36Sopenharmony_ci	to->sb_rbmblocks = cpu_to_be32(from->sb_rbmblocks);
77362306a36Sopenharmony_ci	to->sb_logblocks = cpu_to_be32(from->sb_logblocks);
77462306a36Sopenharmony_ci	to->sb_versionnum = cpu_to_be16(from->sb_versionnum);
77562306a36Sopenharmony_ci	to->sb_sectsize = cpu_to_be16(from->sb_sectsize);
77662306a36Sopenharmony_ci	to->sb_inodesize = cpu_to_be16(from->sb_inodesize);
77762306a36Sopenharmony_ci	to->sb_inopblock = cpu_to_be16(from->sb_inopblock);
77862306a36Sopenharmony_ci	memcpy(&to->sb_fname, &from->sb_fname, sizeof(to->sb_fname));
77962306a36Sopenharmony_ci	to->sb_blocklog = from->sb_blocklog;
78062306a36Sopenharmony_ci	to->sb_sectlog = from->sb_sectlog;
78162306a36Sopenharmony_ci	to->sb_inodelog = from->sb_inodelog;
78262306a36Sopenharmony_ci	to->sb_inopblog = from->sb_inopblog;
78362306a36Sopenharmony_ci	to->sb_agblklog = from->sb_agblklog;
78462306a36Sopenharmony_ci	to->sb_rextslog = from->sb_rextslog;
78562306a36Sopenharmony_ci	to->sb_inprogress = from->sb_inprogress;
78662306a36Sopenharmony_ci	to->sb_imax_pct = from->sb_imax_pct;
78762306a36Sopenharmony_ci	to->sb_icount = cpu_to_be64(from->sb_icount);
78862306a36Sopenharmony_ci	to->sb_ifree = cpu_to_be64(from->sb_ifree);
78962306a36Sopenharmony_ci	to->sb_fdblocks = cpu_to_be64(from->sb_fdblocks);
79062306a36Sopenharmony_ci	to->sb_frextents = cpu_to_be64(from->sb_frextents);
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci	to->sb_flags = from->sb_flags;
79362306a36Sopenharmony_ci	to->sb_shared_vn = from->sb_shared_vn;
79462306a36Sopenharmony_ci	to->sb_inoalignmt = cpu_to_be32(from->sb_inoalignmt);
79562306a36Sopenharmony_ci	to->sb_unit = cpu_to_be32(from->sb_unit);
79662306a36Sopenharmony_ci	to->sb_width = cpu_to_be32(from->sb_width);
79762306a36Sopenharmony_ci	to->sb_dirblklog = from->sb_dirblklog;
79862306a36Sopenharmony_ci	to->sb_logsectlog = from->sb_logsectlog;
79962306a36Sopenharmony_ci	to->sb_logsectsize = cpu_to_be16(from->sb_logsectsize);
80062306a36Sopenharmony_ci	to->sb_logsunit = cpu_to_be32(from->sb_logsunit);
80162306a36Sopenharmony_ci
80262306a36Sopenharmony_ci	/*
80362306a36Sopenharmony_ci	 * We need to ensure that bad_features2 always matches features2.
80462306a36Sopenharmony_ci	 * Hence we enforce that here rather than having to remember to do it
80562306a36Sopenharmony_ci	 * everywhere else that updates features2.
80662306a36Sopenharmony_ci	 */
80762306a36Sopenharmony_ci	from->sb_bad_features2 = from->sb_features2;
80862306a36Sopenharmony_ci	to->sb_features2 = cpu_to_be32(from->sb_features2);
80962306a36Sopenharmony_ci	to->sb_bad_features2 = cpu_to_be32(from->sb_bad_features2);
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci	if (!xfs_sb_is_v5(from))
81262306a36Sopenharmony_ci		return;
81362306a36Sopenharmony_ci
81462306a36Sopenharmony_ci	to->sb_features_compat = cpu_to_be32(from->sb_features_compat);
81562306a36Sopenharmony_ci	to->sb_features_ro_compat =
81662306a36Sopenharmony_ci			cpu_to_be32(from->sb_features_ro_compat);
81762306a36Sopenharmony_ci	to->sb_features_incompat =
81862306a36Sopenharmony_ci			cpu_to_be32(from->sb_features_incompat);
81962306a36Sopenharmony_ci	to->sb_features_log_incompat =
82062306a36Sopenharmony_ci			cpu_to_be32(from->sb_features_log_incompat);
82162306a36Sopenharmony_ci	to->sb_spino_align = cpu_to_be32(from->sb_spino_align);
82262306a36Sopenharmony_ci	to->sb_lsn = cpu_to_be64(from->sb_lsn);
82362306a36Sopenharmony_ci	if (from->sb_features_incompat & XFS_SB_FEAT_INCOMPAT_META_UUID)
82462306a36Sopenharmony_ci		uuid_copy(&to->sb_meta_uuid, &from->sb_meta_uuid);
82562306a36Sopenharmony_ci}
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci/*
82862306a36Sopenharmony_ci * If the superblock has the CRC feature bit set or the CRC field is non-null,
82962306a36Sopenharmony_ci * check that the CRC is valid.  We check the CRC field is non-null because a
83062306a36Sopenharmony_ci * single bit error could clear the feature bit and unused parts of the
83162306a36Sopenharmony_ci * superblock are supposed to be zero. Hence a non-null crc field indicates that
83262306a36Sopenharmony_ci * we've potentially lost a feature bit and we should check it anyway.
83362306a36Sopenharmony_ci *
83462306a36Sopenharmony_ci * However, past bugs (i.e. in growfs) left non-zeroed regions beyond the
83562306a36Sopenharmony_ci * last field in V4 secondary superblocks.  So for secondary superblocks,
83662306a36Sopenharmony_ci * we are more forgiving, and ignore CRC failures if the primary doesn't
83762306a36Sopenharmony_ci * indicate that the fs version is V5.
83862306a36Sopenharmony_ci */
83962306a36Sopenharmony_cistatic void
84062306a36Sopenharmony_cixfs_sb_read_verify(
84162306a36Sopenharmony_ci	struct xfs_buf		*bp)
84262306a36Sopenharmony_ci{
84362306a36Sopenharmony_ci	struct xfs_sb		sb;
84462306a36Sopenharmony_ci	struct xfs_mount	*mp = bp->b_mount;
84562306a36Sopenharmony_ci	struct xfs_dsb		*dsb = bp->b_addr;
84662306a36Sopenharmony_ci	int			error;
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci	/*
84962306a36Sopenharmony_ci	 * open code the version check to avoid needing to convert the entire
85062306a36Sopenharmony_ci	 * superblock from disk order just to check the version number
85162306a36Sopenharmony_ci	 */
85262306a36Sopenharmony_ci	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC) &&
85362306a36Sopenharmony_ci	    (((be16_to_cpu(dsb->sb_versionnum) & XFS_SB_VERSION_NUMBITS) ==
85462306a36Sopenharmony_ci						XFS_SB_VERSION_5) ||
85562306a36Sopenharmony_ci	     dsb->sb_crc != 0)) {
85662306a36Sopenharmony_ci
85762306a36Sopenharmony_ci		if (!xfs_buf_verify_cksum(bp, XFS_SB_CRC_OFF)) {
85862306a36Sopenharmony_ci			/* Only fail bad secondaries on a known V5 filesystem */
85962306a36Sopenharmony_ci			if (xfs_buf_daddr(bp) == XFS_SB_DADDR ||
86062306a36Sopenharmony_ci			    xfs_has_crc(mp)) {
86162306a36Sopenharmony_ci				error = -EFSBADCRC;
86262306a36Sopenharmony_ci				goto out_error;
86362306a36Sopenharmony_ci			}
86462306a36Sopenharmony_ci		}
86562306a36Sopenharmony_ci	}
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci	/*
86862306a36Sopenharmony_ci	 * Check all the superblock fields.  Don't byteswap the xquota flags
86962306a36Sopenharmony_ci	 * because _verify_common checks the on-disk values.
87062306a36Sopenharmony_ci	 */
87162306a36Sopenharmony_ci	__xfs_sb_from_disk(&sb, dsb, false);
87262306a36Sopenharmony_ci	error = xfs_validate_sb_common(mp, bp, &sb);
87362306a36Sopenharmony_ci	if (error)
87462306a36Sopenharmony_ci		goto out_error;
87562306a36Sopenharmony_ci	error = xfs_validate_sb_read(mp, &sb);
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_ciout_error:
87862306a36Sopenharmony_ci	if (error == -EFSCORRUPTED || error == -EFSBADCRC)
87962306a36Sopenharmony_ci		xfs_verifier_error(bp, error, __this_address);
88062306a36Sopenharmony_ci	else if (error)
88162306a36Sopenharmony_ci		xfs_buf_ioerror(bp, error);
88262306a36Sopenharmony_ci}
88362306a36Sopenharmony_ci
88462306a36Sopenharmony_ci/*
88562306a36Sopenharmony_ci * We may be probed for a filesystem match, so we may not want to emit
88662306a36Sopenharmony_ci * messages when the superblock buffer is not actually an XFS superblock.
88762306a36Sopenharmony_ci * If we find an XFS superblock, then run a normal, noisy mount because we are
88862306a36Sopenharmony_ci * really going to mount it and want to know about errors.
88962306a36Sopenharmony_ci */
89062306a36Sopenharmony_cistatic void
89162306a36Sopenharmony_cixfs_sb_quiet_read_verify(
89262306a36Sopenharmony_ci	struct xfs_buf	*bp)
89362306a36Sopenharmony_ci{
89462306a36Sopenharmony_ci	struct xfs_dsb	*dsb = bp->b_addr;
89562306a36Sopenharmony_ci
89662306a36Sopenharmony_ci	if (dsb->sb_magicnum == cpu_to_be32(XFS_SB_MAGIC)) {
89762306a36Sopenharmony_ci		/* XFS filesystem, verify noisily! */
89862306a36Sopenharmony_ci		xfs_sb_read_verify(bp);
89962306a36Sopenharmony_ci		return;
90062306a36Sopenharmony_ci	}
90162306a36Sopenharmony_ci	/* quietly fail */
90262306a36Sopenharmony_ci	xfs_buf_ioerror(bp, -EWRONGFS);
90362306a36Sopenharmony_ci}
90462306a36Sopenharmony_ci
90562306a36Sopenharmony_cistatic void
90662306a36Sopenharmony_cixfs_sb_write_verify(
90762306a36Sopenharmony_ci	struct xfs_buf		*bp)
90862306a36Sopenharmony_ci{
90962306a36Sopenharmony_ci	struct xfs_sb		sb;
91062306a36Sopenharmony_ci	struct xfs_mount	*mp = bp->b_mount;
91162306a36Sopenharmony_ci	struct xfs_buf_log_item	*bip = bp->b_log_item;
91262306a36Sopenharmony_ci	struct xfs_dsb		*dsb = bp->b_addr;
91362306a36Sopenharmony_ci	int			error;
91462306a36Sopenharmony_ci
91562306a36Sopenharmony_ci	/*
91662306a36Sopenharmony_ci	 * Check all the superblock fields.  Don't byteswap the xquota flags
91762306a36Sopenharmony_ci	 * because _verify_common checks the on-disk values.
91862306a36Sopenharmony_ci	 */
91962306a36Sopenharmony_ci	__xfs_sb_from_disk(&sb, dsb, false);
92062306a36Sopenharmony_ci	error = xfs_validate_sb_common(mp, bp, &sb);
92162306a36Sopenharmony_ci	if (error)
92262306a36Sopenharmony_ci		goto out_error;
92362306a36Sopenharmony_ci	error = xfs_validate_sb_write(mp, bp, &sb);
92462306a36Sopenharmony_ci	if (error)
92562306a36Sopenharmony_ci		goto out_error;
92662306a36Sopenharmony_ci
92762306a36Sopenharmony_ci	if (!xfs_sb_is_v5(&sb))
92862306a36Sopenharmony_ci		return;
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_ci	if (bip)
93162306a36Sopenharmony_ci		dsb->sb_lsn = cpu_to_be64(bip->bli_item.li_lsn);
93262306a36Sopenharmony_ci
93362306a36Sopenharmony_ci	xfs_buf_update_cksum(bp, XFS_SB_CRC_OFF);
93462306a36Sopenharmony_ci	return;
93562306a36Sopenharmony_ci
93662306a36Sopenharmony_ciout_error:
93762306a36Sopenharmony_ci	xfs_verifier_error(bp, error, __this_address);
93862306a36Sopenharmony_ci}
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ciconst struct xfs_buf_ops xfs_sb_buf_ops = {
94162306a36Sopenharmony_ci	.name = "xfs_sb",
94262306a36Sopenharmony_ci	.magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
94362306a36Sopenharmony_ci	.verify_read = xfs_sb_read_verify,
94462306a36Sopenharmony_ci	.verify_write = xfs_sb_write_verify,
94562306a36Sopenharmony_ci};
94662306a36Sopenharmony_ci
94762306a36Sopenharmony_ciconst struct xfs_buf_ops xfs_sb_quiet_buf_ops = {
94862306a36Sopenharmony_ci	.name = "xfs_sb_quiet",
94962306a36Sopenharmony_ci	.magic = { cpu_to_be32(XFS_SB_MAGIC), cpu_to_be32(XFS_SB_MAGIC) },
95062306a36Sopenharmony_ci	.verify_read = xfs_sb_quiet_read_verify,
95162306a36Sopenharmony_ci	.verify_write = xfs_sb_write_verify,
95262306a36Sopenharmony_ci};
95362306a36Sopenharmony_ci
95462306a36Sopenharmony_ci/*
95562306a36Sopenharmony_ci * xfs_mount_common
95662306a36Sopenharmony_ci *
95762306a36Sopenharmony_ci * Mount initialization code establishing various mount
95862306a36Sopenharmony_ci * fields from the superblock associated with the given
95962306a36Sopenharmony_ci * mount structure.
96062306a36Sopenharmony_ci *
96162306a36Sopenharmony_ci * Inode geometry are calculated in xfs_ialloc_setup_geometry.
96262306a36Sopenharmony_ci */
96362306a36Sopenharmony_civoid
96462306a36Sopenharmony_cixfs_sb_mount_common(
96562306a36Sopenharmony_ci	struct xfs_mount	*mp,
96662306a36Sopenharmony_ci	struct xfs_sb		*sbp)
96762306a36Sopenharmony_ci{
96862306a36Sopenharmony_ci	mp->m_agfrotor = 0;
96962306a36Sopenharmony_ci	atomic_set(&mp->m_agirotor, 0);
97062306a36Sopenharmony_ci	mp->m_maxagi = mp->m_sb.sb_agcount;
97162306a36Sopenharmony_ci	mp->m_blkbit_log = sbp->sb_blocklog + XFS_NBBYLOG;
97262306a36Sopenharmony_ci	mp->m_blkbb_log = sbp->sb_blocklog - BBSHIFT;
97362306a36Sopenharmony_ci	mp->m_sectbb_log = sbp->sb_sectlog - BBSHIFT;
97462306a36Sopenharmony_ci	mp->m_agno_log = xfs_highbit32(sbp->sb_agcount - 1) + 1;
97562306a36Sopenharmony_ci	mp->m_blockmask = sbp->sb_blocksize - 1;
97662306a36Sopenharmony_ci	mp->m_blockwsize = sbp->sb_blocksize >> XFS_WORDLOG;
97762306a36Sopenharmony_ci	mp->m_blockwmask = mp->m_blockwsize - 1;
97862306a36Sopenharmony_ci
97962306a36Sopenharmony_ci	mp->m_alloc_mxr[0] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 1);
98062306a36Sopenharmony_ci	mp->m_alloc_mxr[1] = xfs_allocbt_maxrecs(mp, sbp->sb_blocksize, 0);
98162306a36Sopenharmony_ci	mp->m_alloc_mnr[0] = mp->m_alloc_mxr[0] / 2;
98262306a36Sopenharmony_ci	mp->m_alloc_mnr[1] = mp->m_alloc_mxr[1] / 2;
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci	mp->m_bmap_dmxr[0] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 1);
98562306a36Sopenharmony_ci	mp->m_bmap_dmxr[1] = xfs_bmbt_maxrecs(mp, sbp->sb_blocksize, 0);
98662306a36Sopenharmony_ci	mp->m_bmap_dmnr[0] = mp->m_bmap_dmxr[0] / 2;
98762306a36Sopenharmony_ci	mp->m_bmap_dmnr[1] = mp->m_bmap_dmxr[1] / 2;
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	mp->m_rmap_mxr[0] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 1);
99062306a36Sopenharmony_ci	mp->m_rmap_mxr[1] = xfs_rmapbt_maxrecs(sbp->sb_blocksize, 0);
99162306a36Sopenharmony_ci	mp->m_rmap_mnr[0] = mp->m_rmap_mxr[0] / 2;
99262306a36Sopenharmony_ci	mp->m_rmap_mnr[1] = mp->m_rmap_mxr[1] / 2;
99362306a36Sopenharmony_ci
99462306a36Sopenharmony_ci	mp->m_refc_mxr[0] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, true);
99562306a36Sopenharmony_ci	mp->m_refc_mxr[1] = xfs_refcountbt_maxrecs(sbp->sb_blocksize, false);
99662306a36Sopenharmony_ci	mp->m_refc_mnr[0] = mp->m_refc_mxr[0] / 2;
99762306a36Sopenharmony_ci	mp->m_refc_mnr[1] = mp->m_refc_mxr[1] / 2;
99862306a36Sopenharmony_ci
99962306a36Sopenharmony_ci	mp->m_bsize = XFS_FSB_TO_BB(mp, 1);
100062306a36Sopenharmony_ci	mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
100162306a36Sopenharmony_ci	mp->m_ag_max_usable = xfs_alloc_ag_max_usable(mp);
100262306a36Sopenharmony_ci}
100362306a36Sopenharmony_ci
100462306a36Sopenharmony_ci/*
100562306a36Sopenharmony_ci * xfs_log_sb() can be used to copy arbitrary changes to the in-core superblock
100662306a36Sopenharmony_ci * into the superblock buffer to be logged.  It does not provide the higher
100762306a36Sopenharmony_ci * level of locking that is needed to protect the in-core superblock from
100862306a36Sopenharmony_ci * concurrent access.
100962306a36Sopenharmony_ci */
101062306a36Sopenharmony_civoid
101162306a36Sopenharmony_cixfs_log_sb(
101262306a36Sopenharmony_ci	struct xfs_trans	*tp)
101362306a36Sopenharmony_ci{
101462306a36Sopenharmony_ci	struct xfs_mount	*mp = tp->t_mountp;
101562306a36Sopenharmony_ci	struct xfs_buf		*bp = xfs_trans_getsb(tp);
101662306a36Sopenharmony_ci
101762306a36Sopenharmony_ci	/*
101862306a36Sopenharmony_ci	 * Lazy sb counters don't update the in-core superblock so do that now.
101962306a36Sopenharmony_ci	 * If this is at unmount, the counters will be exactly correct, but at
102062306a36Sopenharmony_ci	 * any other time they will only be ballpark correct because of
102162306a36Sopenharmony_ci	 * reservations that have been taken out percpu counters. If we have an
102262306a36Sopenharmony_ci	 * unclean shutdown, this will be corrected by log recovery rebuilding
102362306a36Sopenharmony_ci	 * the counters from the AGF block counts.
102462306a36Sopenharmony_ci	 *
102562306a36Sopenharmony_ci	 * Do not update sb_frextents here because it is not part of the lazy
102662306a36Sopenharmony_ci	 * sb counters, despite having a percpu counter. It is always kept
102762306a36Sopenharmony_ci	 * consistent with the ondisk rtbitmap by xfs_trans_apply_sb_deltas()
102862306a36Sopenharmony_ci	 * and hence we don't need have to update it here.
102962306a36Sopenharmony_ci	 */
103062306a36Sopenharmony_ci	if (xfs_has_lazysbcount(mp)) {
103162306a36Sopenharmony_ci		mp->m_sb.sb_icount = percpu_counter_sum(&mp->m_icount);
103262306a36Sopenharmony_ci		mp->m_sb.sb_ifree = min_t(uint64_t,
103362306a36Sopenharmony_ci				percpu_counter_sum(&mp->m_ifree),
103462306a36Sopenharmony_ci				mp->m_sb.sb_icount);
103562306a36Sopenharmony_ci		mp->m_sb.sb_fdblocks = percpu_counter_sum(&mp->m_fdblocks);
103662306a36Sopenharmony_ci	}
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci	xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
103962306a36Sopenharmony_ci	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SB_BUF);
104062306a36Sopenharmony_ci	xfs_trans_log_buf(tp, bp, 0, sizeof(struct xfs_dsb) - 1);
104162306a36Sopenharmony_ci}
104262306a36Sopenharmony_ci
104362306a36Sopenharmony_ci/*
104462306a36Sopenharmony_ci * xfs_sync_sb
104562306a36Sopenharmony_ci *
104662306a36Sopenharmony_ci * Sync the superblock to disk.
104762306a36Sopenharmony_ci *
104862306a36Sopenharmony_ci * Note that the caller is responsible for checking the frozen state of the
104962306a36Sopenharmony_ci * filesystem. This procedure uses the non-blocking transaction allocator and
105062306a36Sopenharmony_ci * thus will allow modifications to a frozen fs. This is required because this
105162306a36Sopenharmony_ci * code can be called during the process of freezing where use of the high-level
105262306a36Sopenharmony_ci * allocator would deadlock.
105362306a36Sopenharmony_ci */
105462306a36Sopenharmony_ciint
105562306a36Sopenharmony_cixfs_sync_sb(
105662306a36Sopenharmony_ci	struct xfs_mount	*mp,
105762306a36Sopenharmony_ci	bool			wait)
105862306a36Sopenharmony_ci{
105962306a36Sopenharmony_ci	struct xfs_trans	*tp;
106062306a36Sopenharmony_ci	int			error;
106162306a36Sopenharmony_ci
106262306a36Sopenharmony_ci	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0,
106362306a36Sopenharmony_ci			XFS_TRANS_NO_WRITECOUNT, &tp);
106462306a36Sopenharmony_ci	if (error)
106562306a36Sopenharmony_ci		return error;
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci	xfs_log_sb(tp);
106862306a36Sopenharmony_ci	if (wait)
106962306a36Sopenharmony_ci		xfs_trans_set_sync(tp);
107062306a36Sopenharmony_ci	return xfs_trans_commit(tp);
107162306a36Sopenharmony_ci}
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci/*
107462306a36Sopenharmony_ci * Update all the secondary superblocks to match the new state of the primary.
107562306a36Sopenharmony_ci * Because we are completely overwriting all the existing fields in the
107662306a36Sopenharmony_ci * secondary superblock buffers, there is no need to read them in from disk.
107762306a36Sopenharmony_ci * Just get a new buffer, stamp it and write it.
107862306a36Sopenharmony_ci *
107962306a36Sopenharmony_ci * The sb buffers need to be cached here so that we serialise against other
108062306a36Sopenharmony_ci * operations that access the secondary superblocks, but we don't want to keep
108162306a36Sopenharmony_ci * them in memory once it is written so we mark it as a one-shot buffer.
108262306a36Sopenharmony_ci */
108362306a36Sopenharmony_ciint
108462306a36Sopenharmony_cixfs_update_secondary_sbs(
108562306a36Sopenharmony_ci	struct xfs_mount	*mp)
108662306a36Sopenharmony_ci{
108762306a36Sopenharmony_ci	struct xfs_perag	*pag;
108862306a36Sopenharmony_ci	xfs_agnumber_t		agno = 1;
108962306a36Sopenharmony_ci	int			saved_error = 0;
109062306a36Sopenharmony_ci	int			error = 0;
109162306a36Sopenharmony_ci	LIST_HEAD		(buffer_list);
109262306a36Sopenharmony_ci
109362306a36Sopenharmony_ci	/* update secondary superblocks. */
109462306a36Sopenharmony_ci	for_each_perag_from(mp, agno, pag) {
109562306a36Sopenharmony_ci		struct xfs_buf		*bp;
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci		error = xfs_buf_get(mp->m_ddev_targp,
109862306a36Sopenharmony_ci				 XFS_AG_DADDR(mp, pag->pag_agno, XFS_SB_DADDR),
109962306a36Sopenharmony_ci				 XFS_FSS_TO_BB(mp, 1), &bp);
110062306a36Sopenharmony_ci		/*
110162306a36Sopenharmony_ci		 * If we get an error reading or writing alternate superblocks,
110262306a36Sopenharmony_ci		 * continue.  xfs_repair chooses the "best" superblock based
110362306a36Sopenharmony_ci		 * on most matches; if we break early, we'll leave more
110462306a36Sopenharmony_ci		 * superblocks un-updated than updated, and xfs_repair may
110562306a36Sopenharmony_ci		 * pick them over the properly-updated primary.
110662306a36Sopenharmony_ci		 */
110762306a36Sopenharmony_ci		if (error) {
110862306a36Sopenharmony_ci			xfs_warn(mp,
110962306a36Sopenharmony_ci		"error allocating secondary superblock for ag %d",
111062306a36Sopenharmony_ci				pag->pag_agno);
111162306a36Sopenharmony_ci			if (!saved_error)
111262306a36Sopenharmony_ci				saved_error = error;
111362306a36Sopenharmony_ci			continue;
111462306a36Sopenharmony_ci		}
111562306a36Sopenharmony_ci
111662306a36Sopenharmony_ci		bp->b_ops = &xfs_sb_buf_ops;
111762306a36Sopenharmony_ci		xfs_buf_oneshot(bp);
111862306a36Sopenharmony_ci		xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
111962306a36Sopenharmony_ci		xfs_sb_to_disk(bp->b_addr, &mp->m_sb);
112062306a36Sopenharmony_ci		xfs_buf_delwri_queue(bp, &buffer_list);
112162306a36Sopenharmony_ci		xfs_buf_relse(bp);
112262306a36Sopenharmony_ci
112362306a36Sopenharmony_ci		/* don't hold too many buffers at once */
112462306a36Sopenharmony_ci		if (agno % 16)
112562306a36Sopenharmony_ci			continue;
112662306a36Sopenharmony_ci
112762306a36Sopenharmony_ci		error = xfs_buf_delwri_submit(&buffer_list);
112862306a36Sopenharmony_ci		if (error) {
112962306a36Sopenharmony_ci			xfs_warn(mp,
113062306a36Sopenharmony_ci		"write error %d updating a secondary superblock near ag %d",
113162306a36Sopenharmony_ci				error, pag->pag_agno);
113262306a36Sopenharmony_ci			if (!saved_error)
113362306a36Sopenharmony_ci				saved_error = error;
113462306a36Sopenharmony_ci			continue;
113562306a36Sopenharmony_ci		}
113662306a36Sopenharmony_ci	}
113762306a36Sopenharmony_ci	error = xfs_buf_delwri_submit(&buffer_list);
113862306a36Sopenharmony_ci	if (error) {
113962306a36Sopenharmony_ci		xfs_warn(mp,
114062306a36Sopenharmony_ci		"write error %d updating a secondary superblock near ag %d",
114162306a36Sopenharmony_ci			error, agno);
114262306a36Sopenharmony_ci	}
114362306a36Sopenharmony_ci
114462306a36Sopenharmony_ci	return saved_error ? saved_error : error;
114562306a36Sopenharmony_ci}
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_ci/*
114862306a36Sopenharmony_ci * Same behavior as xfs_sync_sb, except that it is always synchronous and it
114962306a36Sopenharmony_ci * also writes the superblock buffer to disk sector 0 immediately.
115062306a36Sopenharmony_ci */
115162306a36Sopenharmony_ciint
115262306a36Sopenharmony_cixfs_sync_sb_buf(
115362306a36Sopenharmony_ci	struct xfs_mount	*mp)
115462306a36Sopenharmony_ci{
115562306a36Sopenharmony_ci	struct xfs_trans	*tp;
115662306a36Sopenharmony_ci	struct xfs_buf		*bp;
115762306a36Sopenharmony_ci	int			error;
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci	error = xfs_trans_alloc(mp, &M_RES(mp)->tr_sb, 0, 0, 0, &tp);
116062306a36Sopenharmony_ci	if (error)
116162306a36Sopenharmony_ci		return error;
116262306a36Sopenharmony_ci
116362306a36Sopenharmony_ci	bp = xfs_trans_getsb(tp);
116462306a36Sopenharmony_ci	xfs_log_sb(tp);
116562306a36Sopenharmony_ci	xfs_trans_bhold(tp, bp);
116662306a36Sopenharmony_ci	xfs_trans_set_sync(tp);
116762306a36Sopenharmony_ci	error = xfs_trans_commit(tp);
116862306a36Sopenharmony_ci	if (error)
116962306a36Sopenharmony_ci		goto out;
117062306a36Sopenharmony_ci	/*
117162306a36Sopenharmony_ci	 * write out the sb buffer to get the changes to disk
117262306a36Sopenharmony_ci	 */
117362306a36Sopenharmony_ci	error = xfs_bwrite(bp);
117462306a36Sopenharmony_ciout:
117562306a36Sopenharmony_ci	xfs_buf_relse(bp);
117662306a36Sopenharmony_ci	return error;
117762306a36Sopenharmony_ci}
117862306a36Sopenharmony_ci
117962306a36Sopenharmony_civoid
118062306a36Sopenharmony_cixfs_fs_geometry(
118162306a36Sopenharmony_ci	struct xfs_mount	*mp,
118262306a36Sopenharmony_ci	struct xfs_fsop_geom	*geo,
118362306a36Sopenharmony_ci	int			struct_version)
118462306a36Sopenharmony_ci{
118562306a36Sopenharmony_ci	struct xfs_sb		*sbp = &mp->m_sb;
118662306a36Sopenharmony_ci
118762306a36Sopenharmony_ci	memset(geo, 0, sizeof(struct xfs_fsop_geom));
118862306a36Sopenharmony_ci
118962306a36Sopenharmony_ci	geo->blocksize = sbp->sb_blocksize;
119062306a36Sopenharmony_ci	geo->rtextsize = sbp->sb_rextsize;
119162306a36Sopenharmony_ci	geo->agblocks = sbp->sb_agblocks;
119262306a36Sopenharmony_ci	geo->agcount = sbp->sb_agcount;
119362306a36Sopenharmony_ci	geo->logblocks = sbp->sb_logblocks;
119462306a36Sopenharmony_ci	geo->sectsize = sbp->sb_sectsize;
119562306a36Sopenharmony_ci	geo->inodesize = sbp->sb_inodesize;
119662306a36Sopenharmony_ci	geo->imaxpct = sbp->sb_imax_pct;
119762306a36Sopenharmony_ci	geo->datablocks = sbp->sb_dblocks;
119862306a36Sopenharmony_ci	geo->rtblocks = sbp->sb_rblocks;
119962306a36Sopenharmony_ci	geo->rtextents = sbp->sb_rextents;
120062306a36Sopenharmony_ci	geo->logstart = sbp->sb_logstart;
120162306a36Sopenharmony_ci	BUILD_BUG_ON(sizeof(geo->uuid) != sizeof(sbp->sb_uuid));
120262306a36Sopenharmony_ci	memcpy(geo->uuid, &sbp->sb_uuid, sizeof(sbp->sb_uuid));
120362306a36Sopenharmony_ci
120462306a36Sopenharmony_ci	if (struct_version < 2)
120562306a36Sopenharmony_ci		return;
120662306a36Sopenharmony_ci
120762306a36Sopenharmony_ci	geo->sunit = sbp->sb_unit;
120862306a36Sopenharmony_ci	geo->swidth = sbp->sb_width;
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	if (struct_version < 3)
121162306a36Sopenharmony_ci		return;
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci	geo->version = XFS_FSOP_GEOM_VERSION;
121462306a36Sopenharmony_ci	geo->flags = XFS_FSOP_GEOM_FLAGS_NLINK |
121562306a36Sopenharmony_ci		     XFS_FSOP_GEOM_FLAGS_DIRV2 |
121662306a36Sopenharmony_ci		     XFS_FSOP_GEOM_FLAGS_EXTFLG;
121762306a36Sopenharmony_ci	if (xfs_has_attr(mp))
121862306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR;
121962306a36Sopenharmony_ci	if (xfs_has_quota(mp))
122062306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_QUOTA;
122162306a36Sopenharmony_ci	if (xfs_has_align(mp))
122262306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_IALIGN;
122362306a36Sopenharmony_ci	if (xfs_has_dalign(mp))
122462306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_DALIGN;
122562306a36Sopenharmony_ci	if (xfs_has_asciici(mp))
122662306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_DIRV2CI;
122762306a36Sopenharmony_ci	if (xfs_has_lazysbcount(mp))
122862306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_LAZYSB;
122962306a36Sopenharmony_ci	if (xfs_has_attr2(mp))
123062306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_ATTR2;
123162306a36Sopenharmony_ci	if (xfs_has_projid32(mp))
123262306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_PROJID32;
123362306a36Sopenharmony_ci	if (xfs_has_crc(mp))
123462306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_V5SB;
123562306a36Sopenharmony_ci	if (xfs_has_ftype(mp))
123662306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_FTYPE;
123762306a36Sopenharmony_ci	if (xfs_has_finobt(mp))
123862306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_FINOBT;
123962306a36Sopenharmony_ci	if (xfs_has_sparseinodes(mp))
124062306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_SPINODES;
124162306a36Sopenharmony_ci	if (xfs_has_rmapbt(mp))
124262306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_RMAPBT;
124362306a36Sopenharmony_ci	if (xfs_has_reflink(mp))
124462306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_REFLINK;
124562306a36Sopenharmony_ci	if (xfs_has_bigtime(mp))
124662306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_BIGTIME;
124762306a36Sopenharmony_ci	if (xfs_has_inobtcounts(mp))
124862306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_INOBTCNT;
124962306a36Sopenharmony_ci	if (xfs_has_sector(mp)) {
125062306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_SECTOR;
125162306a36Sopenharmony_ci		geo->logsectsize = sbp->sb_logsectsize;
125262306a36Sopenharmony_ci	} else {
125362306a36Sopenharmony_ci		geo->logsectsize = BBSIZE;
125462306a36Sopenharmony_ci	}
125562306a36Sopenharmony_ci	if (xfs_has_large_extent_counts(mp))
125662306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_NREXT64;
125762306a36Sopenharmony_ci	geo->rtsectsize = sbp->sb_blocksize;
125862306a36Sopenharmony_ci	geo->dirblocksize = xfs_dir2_dirblock_bytes(sbp);
125962306a36Sopenharmony_ci
126062306a36Sopenharmony_ci	if (struct_version < 4)
126162306a36Sopenharmony_ci		return;
126262306a36Sopenharmony_ci
126362306a36Sopenharmony_ci	if (xfs_has_logv2(mp))
126462306a36Sopenharmony_ci		geo->flags |= XFS_FSOP_GEOM_FLAGS_LOGV2;
126562306a36Sopenharmony_ci
126662306a36Sopenharmony_ci	geo->logsunit = sbp->sb_logsunit;
126762306a36Sopenharmony_ci
126862306a36Sopenharmony_ci	if (struct_version < 5)
126962306a36Sopenharmony_ci		return;
127062306a36Sopenharmony_ci
127162306a36Sopenharmony_ci	geo->version = XFS_FSOP_GEOM_VERSION_V5;
127262306a36Sopenharmony_ci}
127362306a36Sopenharmony_ci
127462306a36Sopenharmony_ci/* Read a secondary superblock. */
127562306a36Sopenharmony_ciint
127662306a36Sopenharmony_cixfs_sb_read_secondary(
127762306a36Sopenharmony_ci	struct xfs_mount	*mp,
127862306a36Sopenharmony_ci	struct xfs_trans	*tp,
127962306a36Sopenharmony_ci	xfs_agnumber_t		agno,
128062306a36Sopenharmony_ci	struct xfs_buf		**bpp)
128162306a36Sopenharmony_ci{
128262306a36Sopenharmony_ci	struct xfs_buf		*bp;
128362306a36Sopenharmony_ci	int			error;
128462306a36Sopenharmony_ci
128562306a36Sopenharmony_ci	ASSERT(agno != 0 && agno != NULLAGNUMBER);
128662306a36Sopenharmony_ci	error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp,
128762306a36Sopenharmony_ci			XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
128862306a36Sopenharmony_ci			XFS_FSS_TO_BB(mp, 1), 0, &bp, &xfs_sb_buf_ops);
128962306a36Sopenharmony_ci	if (error)
129062306a36Sopenharmony_ci		return error;
129162306a36Sopenharmony_ci	xfs_buf_set_ref(bp, XFS_SSB_REF);
129262306a36Sopenharmony_ci	*bpp = bp;
129362306a36Sopenharmony_ci	return 0;
129462306a36Sopenharmony_ci}
129562306a36Sopenharmony_ci
129662306a36Sopenharmony_ci/* Get an uninitialised secondary superblock buffer. */
129762306a36Sopenharmony_ciint
129862306a36Sopenharmony_cixfs_sb_get_secondary(
129962306a36Sopenharmony_ci	struct xfs_mount	*mp,
130062306a36Sopenharmony_ci	struct xfs_trans	*tp,
130162306a36Sopenharmony_ci	xfs_agnumber_t		agno,
130262306a36Sopenharmony_ci	struct xfs_buf		**bpp)
130362306a36Sopenharmony_ci{
130462306a36Sopenharmony_ci	struct xfs_buf		*bp;
130562306a36Sopenharmony_ci	int			error;
130662306a36Sopenharmony_ci
130762306a36Sopenharmony_ci	ASSERT(agno != 0 && agno != NULLAGNUMBER);
130862306a36Sopenharmony_ci	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
130962306a36Sopenharmony_ci			XFS_AG_DADDR(mp, agno, XFS_SB_BLOCK(mp)),
131062306a36Sopenharmony_ci			XFS_FSS_TO_BB(mp, 1), 0, &bp);
131162306a36Sopenharmony_ci	if (error)
131262306a36Sopenharmony_ci		return error;
131362306a36Sopenharmony_ci	bp->b_ops = &xfs_sb_buf_ops;
131462306a36Sopenharmony_ci	xfs_buf_oneshot(bp);
131562306a36Sopenharmony_ci	*bpp = bp;
131662306a36Sopenharmony_ci	return 0;
131762306a36Sopenharmony_ci}
131862306a36Sopenharmony_ci
131962306a36Sopenharmony_ci/*
132062306a36Sopenharmony_ci * sunit, swidth, sectorsize(optional with 0) should be all in bytes,
132162306a36Sopenharmony_ci * so users won't be confused by values in error messages.
132262306a36Sopenharmony_ci */
132362306a36Sopenharmony_cibool
132462306a36Sopenharmony_cixfs_validate_stripe_geometry(
132562306a36Sopenharmony_ci	struct xfs_mount	*mp,
132662306a36Sopenharmony_ci	__s64			sunit,
132762306a36Sopenharmony_ci	__s64			swidth,
132862306a36Sopenharmony_ci	int			sectorsize,
132962306a36Sopenharmony_ci	bool			silent)
133062306a36Sopenharmony_ci{
133162306a36Sopenharmony_ci	if (swidth > INT_MAX) {
133262306a36Sopenharmony_ci		if (!silent)
133362306a36Sopenharmony_ci			xfs_notice(mp,
133462306a36Sopenharmony_ci"stripe width (%lld) is too large", swidth);
133562306a36Sopenharmony_ci		return false;
133662306a36Sopenharmony_ci	}
133762306a36Sopenharmony_ci
133862306a36Sopenharmony_ci	if (sunit > swidth) {
133962306a36Sopenharmony_ci		if (!silent)
134062306a36Sopenharmony_ci			xfs_notice(mp,
134162306a36Sopenharmony_ci"stripe unit (%lld) is larger than the stripe width (%lld)", sunit, swidth);
134262306a36Sopenharmony_ci		return false;
134362306a36Sopenharmony_ci	}
134462306a36Sopenharmony_ci
134562306a36Sopenharmony_ci	if (sectorsize && (int)sunit % sectorsize) {
134662306a36Sopenharmony_ci		if (!silent)
134762306a36Sopenharmony_ci			xfs_notice(mp,
134862306a36Sopenharmony_ci"stripe unit (%lld) must be a multiple of the sector size (%d)",
134962306a36Sopenharmony_ci				   sunit, sectorsize);
135062306a36Sopenharmony_ci		return false;
135162306a36Sopenharmony_ci	}
135262306a36Sopenharmony_ci
135362306a36Sopenharmony_ci	if (sunit && !swidth) {
135462306a36Sopenharmony_ci		if (!silent)
135562306a36Sopenharmony_ci			xfs_notice(mp,
135662306a36Sopenharmony_ci"invalid stripe unit (%lld) and stripe width of 0", sunit);
135762306a36Sopenharmony_ci		return false;
135862306a36Sopenharmony_ci	}
135962306a36Sopenharmony_ci
136062306a36Sopenharmony_ci	if (!sunit && swidth) {
136162306a36Sopenharmony_ci		if (!silent)
136262306a36Sopenharmony_ci			xfs_notice(mp,
136362306a36Sopenharmony_ci"invalid stripe width (%lld) and stripe unit of 0", swidth);
136462306a36Sopenharmony_ci		return false;
136562306a36Sopenharmony_ci	}
136662306a36Sopenharmony_ci
136762306a36Sopenharmony_ci	if (sunit && (int)swidth % (int)sunit) {
136862306a36Sopenharmony_ci		if (!silent)
136962306a36Sopenharmony_ci			xfs_notice(mp,
137062306a36Sopenharmony_ci"stripe width (%lld) must be a multiple of the stripe unit (%lld)",
137162306a36Sopenharmony_ci				   swidth, sunit);
137262306a36Sopenharmony_ci		return false;
137362306a36Sopenharmony_ci	}
137462306a36Sopenharmony_ci	return true;
137562306a36Sopenharmony_ci}
1376