Linux Audio

Check our new training course

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