Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0+
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright (C) 2017 Oracle.  All Rights Reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Author: Darrick J. Wong <darrick.wong@oracle.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include "xfs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include "xfs_fs.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include "xfs_shared.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "xfs_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "xfs_trans_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include "xfs_mount.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include "xfs_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include "xfs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "xfs_ialloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "xfs_rmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "scrub/scrub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "scrub/common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /* Superblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) /* Cross-reference with the other btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) xchk_superblock_xref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	struct xfs_buf		*bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 	xfs_agnumber_t		agno = sc->sm->sm_agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	agbno = XFS_SB_BLOCK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	error = xchk_ag_init(sc, agno, &sc->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	if (!xchk_xref_process_error(sc, agno, agbno, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	xchk_xref_is_used_space(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	xchk_xref_is_not_inode_chunk(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	xchk_xref_is_not_shared(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	/* scrub teardown will take care of sc->sa for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * Scrub the filesystem superblock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * Note: We do /not/ attempt to check AG 0's superblock.  Mount is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  * responsible for validating all the geometry information in sb 0, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * if the filesystem is capable of initiating online scrub, then clearly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  * sb 0 is ok and we can use its information to check everything else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) xchk_superblock(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	struct xfs_buf		*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	struct xfs_dsb		*sb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	uint32_t		v2_ok;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	__be32			features_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	__be16			vernum_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	agno = sc->sm->sm_agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (agno == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	error = xfs_sb_read_secondary(mp, sc->tp, agno, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	 * The superblock verifier can return several different error codes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	 * if it thinks the superblock doesn't look right.  For a mount these
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 	 * would all get bounced back to userspace, but if we're here then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 	 * fs mounted successfully, which means that this secondary superblock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	 * is simply incorrect.  Treat all these codes the same way we treat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	 * any corruption.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	case -EINVAL:	/* also -EWRONGFS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case -ENOSYS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	case -EFBIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 		error = -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	sb = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	 * Verify the geometries match.  Fields that are permanently
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	 * set by mkfs are checked; fields that can be updated later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	 * (and are not propagated to backup superblocks) are preen
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	 * checked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	/* Check sb_versionnum bits that are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				  XFS_SB_VERSION_NUMBITS |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 				  XFS_SB_VERSION_ALIGNBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 				  XFS_SB_VERSION_DALIGNBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 				  XFS_SB_VERSION_SHAREDBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 				  XFS_SB_VERSION_LOGV2BIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 				  XFS_SB_VERSION_SECTORBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 				  XFS_SB_VERSION_EXTFLGBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 				  XFS_SB_VERSION_DIRV2BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 	if ((sb->sb_versionnum & vernum_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	    (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 	/* Check sb_versionnum bits that can be set after mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 				  XFS_SB_VERSION_NLINKBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 				  XFS_SB_VERSION_QUOTABIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	if ((sb->sb_versionnum & vernum_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	    (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 	if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	 * Skip the summary counters since we track them in memory anyway.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	 * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 	if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 	 * Skip the quota flags since repair will force quotacheck.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 	 * sb_qflags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	if (sb->sb_flags != mp->m_sb.sb_flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 	if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 		xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	/* Do we see any invalid bits in sb_features2? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 		if (sb->sb_features2 != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		v2_ok = XFS_SB_VERSION2_OKBITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 		if (XFS_SB_VERSION_NUM(&mp->m_sb) >= XFS_SB_VERSION_5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 			v2_ok |= XFS_SB_VERSION2_CRCBIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		if (sb->sb_features2 != sb->sb_bad_features2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 			xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	/* Check sb_features2 flags that are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 	features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 				    XFS_SB_VERSION2_PROJID32BIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 				    XFS_SB_VERSION2_CRCBIT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 				    XFS_SB_VERSION2_FTYPE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 	if ((sb->sb_features2 & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	    (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	/* Check sb_features2 flags that can be set after mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 	features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 	if ((sb->sb_features2 & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 	    (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	if (!xfs_sb_version_hascrc(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		/* all v5 fields must be zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 		if (memchr_inv(&sb->sb_features_compat, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 				sizeof(struct xfs_dsb) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 				offsetof(struct xfs_dsb, sb_features_compat)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 		/* Check compat flags; all are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 		features_mask = cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 		if ((sb->sb_features_compat & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		    (cpu_to_be32(mp->m_sb.sb_features_compat) & features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 		/* Check ro compat flags; all are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 		features_mask = cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 					    XFS_SB_FEAT_RO_COMPAT_FINOBT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 					    XFS_SB_FEAT_RO_COMPAT_RMAPBT |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 					    XFS_SB_FEAT_RO_COMPAT_REFLINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 		if ((sb->sb_features_ro_compat & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 		    (cpu_to_be32(mp->m_sb.sb_features_ro_compat) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 		     features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		/* Check incompat flags; all are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 					    XFS_SB_FEAT_INCOMPAT_FTYPE |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 					    XFS_SB_FEAT_INCOMPAT_SPINODES |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 					    XFS_SB_FEAT_INCOMPAT_META_UUID);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		if ((sb->sb_features_incompat & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		    (cpu_to_be32(mp->m_sb.sb_features_incompat) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 		     features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 		/* Check log incompat flags; all are set at mkfs time. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 		if ((sb->sb_features_log_incompat & features_mask) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		    (cpu_to_be32(mp->m_sb.sb_features_log_incompat) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		     features_mask))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 		/* Don't care about sb_crc */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 		if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 		if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 			xchk_block_set_preen(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		/* Don't care about sb_lsn */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (xfs_sb_version_hasmetauuid(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		/* The metadata UUID must be the same for all supers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 			xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	/* Everything else must be zero. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 	if (memchr_inv(sb + 1, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 			BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 		xchk_block_set_corrupt(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	xchk_superblock_xref(sc, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) /* AGF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) /* Tally freespace record lengths. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) xchk_agf_record_bno_lengths(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	struct xfs_alloc_rec_incore	*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	void				*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 	xfs_extlen_t			*blocks = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	(*blocks) += rec->ar_blockcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Check agf_freeblks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) xchk_agf_xref_freeblks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	struct xfs_agf		*agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	xfs_extlen_t		blocks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	if (!sc->sa.bno_cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	error = xfs_alloc_query_all(sc->sa.bno_cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 			xchk_agf_record_bno_lengths, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 	if (blocks != be32_to_cpu(agf->agf_freeblks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 		xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) /* Cross reference the AGF with the cntbt (freespace by length btree) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) xchk_agf_xref_cntbt(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	struct xfs_agf		*agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 	xfs_extlen_t		blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	int			have;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 	if (!sc->sa.cnt_cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 	/* Any freespace at all? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 	if (!have) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 		if (agf->agf_freeblks != cpu_to_be32(0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 			xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	/* Check agf_longest */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 	error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 	if (!have || blocks != be32_to_cpu(agf->agf_longest))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) /* Check the btree block counts in the AGF against the btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) xchk_agf_xref_btreeblks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 	struct xfs_agf		*agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 	xfs_agblock_t		blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 	xfs_agblock_t		btreeblks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 	/* Check agf_rmap_blocks; set up for agf_btreeblks check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 	if (sc->sa.rmap_cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 		error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 		if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 		btreeblks = blocks - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 			xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 		btreeblks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	 * No rmap cursor; we can't xref if we have the rmapbt feature.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 	 * We also can't do it if we're missing the free space btree cursors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 	if ((xfs_sb_version_hasrmapbt(&mp->m_sb) && !sc->sa.rmap_cur) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) 	    !sc->sa.bno_cur || !sc->sa.cnt_cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	/* Check agf_btreeblks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 	error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	btreeblks += blocks - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 	error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	btreeblks += blocks - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 		xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) /* Check agf_refcount_blocks against tree size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) xchk_agf_xref_refcblks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 	struct xfs_agf		*agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 	xfs_agblock_t		blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	if (!sc->sa.refc_cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) /* Cross-reference with the other btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) xchk_agf_xref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 	agbno = XFS_AGF_BLOCK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	error = xchk_ag_btcur_init(sc, &sc->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 	xchk_xref_is_used_space(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 	xchk_agf_xref_freeblks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	xchk_agf_xref_cntbt(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	xchk_xref_is_not_inode_chunk(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 	xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	xchk_agf_xref_btreeblks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 	xchk_xref_is_not_shared(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	xchk_agf_xref_refcblks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	/* scrub teardown will take care of sc->sa for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) /* Scrub the AGF. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) xchk_agf(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 	struct xfs_agf		*agf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	struct xfs_perag	*pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	xfs_agblock_t		eoag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	xfs_agblock_t		agfl_first;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	xfs_agblock_t		agfl_last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	xfs_agblock_t		agfl_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 	xfs_agblock_t		fl_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	int			level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	agno = sc->sa.agno = sc->sm->sm_agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 			&sc->sa.agf_bp, &sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	xchk_buffer_recheck(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	/* Check the AG length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) 	eoag = be32_to_cpu(agf->agf_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	if (eoag != xfs_ag_block_count(mp, agno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	/* Check the AGF btree roots and levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 	if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 	level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 	if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 		agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 		if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 			xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 		level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 		if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 			xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	if (xfs_sb_version_hasreflink(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 		agbno = be32_to_cpu(agf->agf_refcount_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 		if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 			xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 		level = be32_to_cpu(agf->agf_refcount_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 		if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 			xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	/* Check the AGFL counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	agfl_first = be32_to_cpu(agf->agf_flfirst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	agfl_last = be32_to_cpu(agf->agf_fllast);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	agfl_count = be32_to_cpu(agf->agf_flcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 	if (agfl_last > agfl_first)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 		fl_count = agfl_last - agfl_first + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 		fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 	if (agfl_count != 0 && fl_count != agfl_count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	/* Do the incore counters match? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 	pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 	if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 	if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 	if (pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	xchk_agf_xref(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) /* AGFL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) struct xchk_agfl_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	unsigned int		sz_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	unsigned int		nr_entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) 	xfs_agblock_t		*entries;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	struct xfs_scrub	*sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) /* Cross-reference with the other btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) xchk_agfl_block_xref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	xfs_agblock_t		agbno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 	xchk_xref_is_used_space(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 	xchk_xref_is_not_inode_chunk(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 	xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 	xchk_xref_is_not_shared(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) /* Scrub an AGFL block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) xchk_agfl_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 	xfs_agblock_t		agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 	void			*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 	struct xchk_agfl_info	*sai = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 	struct xfs_scrub	*sc = sai->sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 	xfs_agnumber_t		agno = sc->sa.agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 	if (xfs_verify_agbno(mp, agno, agbno) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 	    sai->nr_entries < sai->sz_entries)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 		sai->entries[sai->nr_entries++] = agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 		xchk_block_set_corrupt(sc, sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) 	xchk_agfl_block_xref(sc, agbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) 		return -ECANCELED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) xchk_agblock_cmp(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) 	const void		*pa,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) 	const void		*pb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 	const xfs_agblock_t	*a = pa;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) 	const xfs_agblock_t	*b = pb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	return (int)*a - (int)*b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) /* Cross-reference with the other btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) xchk_agfl_xref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) 	agbno = XFS_AGFL_BLOCK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) 	error = xchk_ag_btcur_init(sc, &sc->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	xchk_xref_is_used_space(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	xchk_xref_is_not_inode_chunk(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	xchk_xref_is_not_shared(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	 * Scrub teardown will take care of sc->sa for us.  Leave sc->sa
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	 * active so that the agfl block xref can use it too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) /* Scrub the AGFL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) xchk_agfl(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	struct xchk_agfl_info	sai;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	struct xfs_agf		*agf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	unsigned int		agflcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	unsigned int		i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 	agno = sc->sa.agno = sc->sm->sm_agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 	error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 			&sc->sa.agf_bp, &sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 	if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 	if (!sc->sa.agf_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	xchk_buffer_recheck(sc, sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	xchk_agfl_xref(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	/* Allocate buffer to ensure uniqueness of AGFL entries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	agf = sc->sa.agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	agflcount = be32_to_cpu(agf->agf_flcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	if (agflcount > xfs_agfl_size(sc->mp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	memset(&sai, 0, sizeof(sai));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	sai.sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	sai.sz_entries = agflcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	sai.entries = kmem_zalloc(sizeof(xfs_agblock_t) * agflcount,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			KM_MAYFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (!sai.entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		error = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	/* Check the blocks in the AGFL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 			sc->sa.agfl_bp, xchk_agfl_block, &sai);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 	if (error == -ECANCELED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	if (agflcount != sai.nr_entries) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 		xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	/* Sort entries, check for duplicates. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 			xchk_agblock_cmp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	for (i = 1; i < sai.nr_entries; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		if (sai.entries[i] == sai.entries[i - 1]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 			xchk_block_set_corrupt(sc, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 	kmem_free(sai.entries);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) /* AGI */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) /* Check agi_count/agi_freecount */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) xchk_agi_xref_icounts(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 	struct xfs_agi		*agi = sc->sa.agi_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 	xfs_agino_t		icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 	xfs_agino_t		freecount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	if (!sc->sa.ino_cur)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	if (be32_to_cpu(agi->agi_count) != icount ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 	    be32_to_cpu(agi->agi_freecount) != freecount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 		xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) /* Check agi_[fi]blocks against tree size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) static inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) xchk_agi_xref_fiblocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	struct xfs_agi		*agi = sc->sa.agi_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	xfs_agblock_t		blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 	if (!xfs_sb_version_hasinobtcounts(&sc->mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 	if (sc->sa.ino_cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 		error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 		if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 		if (blocks != be32_to_cpu(agi->agi_iblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 			xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	if (sc->sa.fino_cur) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 		error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 		if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 		if (blocks != be32_to_cpu(agi->agi_fblocks))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 			xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) /* Cross-reference with the other btrees. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) STATIC void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) xchk_agi_xref(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) 	agbno = XFS_AGI_BLOCK(mp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) 	error = xchk_ag_btcur_init(sc, &sc->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	xchk_xref_is_used_space(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	xchk_xref_is_not_inode_chunk(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) 	xchk_agi_xref_icounts(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	xchk_xref_is_not_shared(sc, agbno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	xchk_agi_xref_fiblocks(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	/* scrub teardown will take care of sc->sa for us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) /* Scrub the AGI. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) xchk_agi(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 	struct xfs_mount	*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 	struct xfs_agi		*agi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 	struct xfs_perag	*pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 	xfs_agnumber_t		agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 	xfs_agblock_t		agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 	xfs_agblock_t		eoag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 	xfs_agino_t		agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 	xfs_agino_t		first_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 	xfs_agino_t		last_agino;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 	xfs_agino_t		icount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 	int			i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 	int			level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	agno = sc->sa.agno = sc->sm->sm_agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) 	error = xchk_ag_read_headers(sc, agno, &sc->sa.agi_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 			&sc->sa.agf_bp, &sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) 	if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) 	xchk_buffer_recheck(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	agi = sc->sa.agi_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	/* Check the AG length */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) 	eoag = be32_to_cpu(agi->agi_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	if (eoag != xfs_ag_block_count(mp, agno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	/* Check btree roots and levels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	agbno = be32_to_cpu(agi->agi_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 	if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 	level = be32_to_cpu(agi->agi_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	if (xfs_sb_version_hasfinobt(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 		agbno = be32_to_cpu(agi->agi_free_root);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		if (!xfs_verify_agbno(mp, agno, agbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 			xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		level = be32_to_cpu(agi->agi_free_level);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 		if (level <= 0 || level > XFS_BTREE_MAXLEVELS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 			xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	/* Check inode counters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 	xfs_agino_range(mp, agno, &first_agino, &last_agino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	icount = be32_to_cpu(agi->agi_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) 	if (icount > last_agino - first_agino + 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 	    icount < be32_to_cpu(agi->agi_freecount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) 	/* Check inode pointers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	agino = be32_to_cpu(agi->agi_newino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	if (!xfs_verify_agino_or_null(mp, agno, agino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 	agino = be32_to_cpu(agi->agi_dirino);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	if (!xfs_verify_agino_or_null(mp, agno, agino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 	/* Check unlinked inode buckets */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 		agino = be32_to_cpu(agi->agi_unlinked[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 		if (!xfs_verify_agino_or_null(mp, agno, agino))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 			xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 	if (agi->agi_pad32 != cpu_to_be32(0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) 	/* Do the incore counters match? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) 	pag = xfs_perag_get(mp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) 	if (pag->pagi_count != be32_to_cpu(agi->agi_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) 	if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) 		xchk_block_set_corrupt(sc, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) 	xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) 	xchk_agi_xref(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) }