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) 2018 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_log_format.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include "xfs_trans.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include "xfs_sb.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include "xfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include "xfs_alloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include "xfs_alloc_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include "xfs_ialloc.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "xfs_ialloc_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "xfs_rmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "xfs_rmap_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "xfs_refcount_btree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #include "xfs_extent_busy.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #include "xfs_ag_resv.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #include "xfs_quota.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #include "scrub/scrub.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #include "scrub/common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) #include "scrub/trace.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) #include "scrub/repair.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include "scrub/bitmap.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * Attempt to repair some metadata, if the metadata is corrupt and userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * told us to fix it.  This function returns -EAGAIN to mean "re-run scrub",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * and will set *fixed to true if it thinks it repaired anything.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) xrep_attempt(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	struct xfs_inode	*ip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	trace_xrep_attempt(ip, sc->sm, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	xchk_ag_btcur_free(&sc->sa);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	/* Repair whatever's broken. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	ASSERT(sc->ops->repair);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	error = sc->ops->repair(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	trace_xrep_done(ip, sc->sm, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 		 * Repair succeeded.  Commit the fixes and perform a second
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		 * scrub so that we can tell userspace if we fixed the problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		sc->sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 		sc->flags |= XREP_ALREADY_FIXED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	case -EDEADLOCK:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	case -EAGAIN:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		/* Tell the caller to try again having grabbed all the locks. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (!(sc->flags & XCHK_TRY_HARDER)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			sc->flags |= XCHK_TRY_HARDER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		 * We tried harder but still couldn't grab all the resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		 * we needed to fix it.  The corruption has not been fixed,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		 * so report back to userspace.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		return -EFSCORRUPTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * Complain about unfixable problems in the filesystem.  We don't log
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  * corruptions when IFLAG_REPAIR wasn't set on the assumption that the driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83)  * program is xfs_scrub, which will call back with IFLAG_REPAIR set if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84)  * administrator isn't running xfs_scrub in no-repairs mode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86)  * Use this helper function because _ratelimited silently declares a static
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87)  * structure to track rate limiting information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) xrep_failure(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct xfs_mount	*mp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	xfs_alert_ratelimited(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) "Corruption not fixed during online repair.  Unmount and run xfs_repair.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) }
^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)  * Repair probe -- userspace uses this to probe if we're willing to repair a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * given mountpoint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) xrep_probe(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	int			error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	if (xchk_should_terminate(sc, &error))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)  * Roll a transaction, keeping the AG headers locked and reinitializing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * the btree cursors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) xrep_roll_ag_trans(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	/* Keep the AG header buffers locked so we can keep going. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	if (sc->sa.agi_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		xfs_trans_bhold(sc->tp, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 	if (sc->sa.agf_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 		xfs_trans_bhold(sc->tp, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	if (sc->sa.agfl_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 		xfs_trans_bhold(sc->tp, sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	 * Roll the transaction.  We still own the buffer and the buffer lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 	 * regardless of whether or not the roll succeeds.  If the roll fails,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	 * the buffers will be released during teardown on our way out of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 	 * kernel.  If it succeeds, we join them to the new transaction and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	 * move on.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	error = xfs_trans_roll(&sc->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	/* Join AG headers to the new transaction. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	if (sc->sa.agi_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		xfs_trans_bjoin(sc->tp, sc->sa.agi_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (sc->sa.agf_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		xfs_trans_bjoin(sc->tp, sc->sa.agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	if (sc->sa.agfl_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		xfs_trans_bjoin(sc->tp, sc->sa.agfl_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)  * Does the given AG have enough space to rebuild a btree?  Neither AG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)  * reservation can be critical, and we must have enough space (factoring
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * in AG reservations) to construct a whole btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) bool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) xrep_ag_has_space(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	struct xfs_perag	*pag,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	xfs_extlen_t		nr_blocks,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 	enum xfs_ag_resv_type	type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 	return  !xfs_ag_resv_critical(pag, XFS_AG_RESV_RMAPBT) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 		!xfs_ag_resv_critical(pag, XFS_AG_RESV_METADATA) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 		pag->pagf_freeblks > xfs_ag_resv_needed(pag, type) + nr_blocks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * Figure out how many blocks to reserve for an AG repair.  We calculate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * worst case estimate for the number of blocks we'd need to rebuild one of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  * any type of per-AG btree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) xfs_extlen_t
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) xrep_calc_ag_resblks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct xfs_scrub		*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	struct xfs_mount		*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	struct xfs_scrub_metadata	*sm = sc->sm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 	struct xfs_perag		*pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	struct xfs_buf			*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	xfs_agino_t			icount = NULLAGINO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	xfs_extlen_t			aglen = NULLAGBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	xfs_extlen_t			usedlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 	xfs_extlen_t			freelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 	xfs_extlen_t			bnobt_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	xfs_extlen_t			inobt_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 	xfs_extlen_t			rmapbt_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	xfs_extlen_t			refcbt_sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 	if (!(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 	pag = xfs_perag_get(mp, sm->sm_agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	if (pag->pagi_init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 		/* Use in-core icount if possible. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		icount = pag->pagi_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		/* Try to get the actual counters from disk. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		error = xfs_ialloc_read_agi(mp, NULL, sm->sm_agno, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 		if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 			icount = pag->pagi_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 			xfs_buf_relse(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	/* Now grab the block counters from the AGF. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	error = xfs_alloc_read_agf(mp, NULL, sm->sm_agno, 0, &bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (!error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		struct xfs_agf	*agf = bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		aglen = be32_to_cpu(agf->agf_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 		freelen = be32_to_cpu(agf->agf_freeblks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		usedlen = aglen - freelen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 		xfs_buf_relse(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	xfs_perag_put(pag);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	/* If the icount is impossible, make some worst-case assumptions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	if (icount == NULLAGINO ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) 	    !xfs_verify_agino(mp, sm->sm_agno, icount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		xfs_agino_t	first, last;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 		xfs_agino_range(mp, sm->sm_agno, &first, &last);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 		icount = last - first + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 	/* If the block counts are impossible, make worst-case assumptions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	if (aglen == NULLAGBLOCK ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 	    aglen != xfs_ag_block_count(mp, sm->sm_agno) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	    freelen >= aglen) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 		aglen = xfs_ag_block_count(mp, sm->sm_agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 		freelen = aglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		usedlen = aglen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	trace_xrep_calc_ag_resblks(mp, sm->sm_agno, icount, aglen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 			freelen, usedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	 * Figure out how many blocks we'd need worst case to rebuild
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	 * each type of btree.  Note that we can only rebuild the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	 * bnobt/cntbt or inobt/finobt as pairs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 	bnobt_sz = 2 * xfs_allocbt_calc_size(mp, freelen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 	if (xfs_sb_version_hassparseinodes(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		inobt_sz = xfs_iallocbt_calc_size(mp, icount /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 				XFS_INODES_PER_HOLEMASK_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		inobt_sz = xfs_iallocbt_calc_size(mp, icount /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 				XFS_INODES_PER_CHUNK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 	if (xfs_sb_version_hasfinobt(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		inobt_sz *= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	if (xfs_sb_version_hasreflink(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 		refcbt_sz = xfs_refcountbt_calc_size(mp, usedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		refcbt_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 	if (xfs_sb_version_hasrmapbt(&mp->m_sb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		 * Guess how many blocks we need to rebuild the rmapbt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 		 * For non-reflink filesystems we can't have more records than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		 * used blocks.  However, with reflink it's possible to have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 		 * more than one rmap record per AG block.  We don't know how
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		 * many rmaps there could be in the AG, so we start off with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 		 * what we hope is an generous over-estimation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 		if (xfs_sb_version_hasreflink(&mp->m_sb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 			rmapbt_sz = xfs_rmapbt_calc_size(mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 					(unsigned long long)aglen * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 			rmapbt_sz = xfs_rmapbt_calc_size(mp, usedlen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		rmapbt_sz = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	trace_xrep_calc_ag_resblks_btsize(mp, sm->sm_agno, bnobt_sz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 			inobt_sz, rmapbt_sz, refcbt_sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	return max(max(bnobt_sz, inobt_sz), max(rmapbt_sz, refcbt_sz));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) /* Allocate a block in an AG. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) xrep_alloc_ag_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	const struct xfs_owner_info	*oinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	xfs_fsblock_t			*fsbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 	enum xfs_ag_resv_type		resv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 	struct xfs_alloc_arg		args = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 	xfs_agblock_t			bno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	switch (resv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	case XFS_AG_RESV_AGFL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 	case XFS_AG_RESV_RMAPBT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 		error = xfs_alloc_get_freelist(sc->tp, sc->sa.agf_bp, &bno, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		if (bno == NULLAGBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 			return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		xfs_extent_busy_reuse(sc->mp, sc->sa.agno, bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 				1, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 		*fsbno = XFS_AGB_TO_FSB(sc->mp, sc->sa.agno, bno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 		if (resv == XFS_AG_RESV_RMAPBT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 			xfs_ag_resv_rmapbt_alloc(sc->mp, sc->sa.agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	args.tp = sc->tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	args.mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	args.oinfo = *oinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	args.fsbno = XFS_AGB_TO_FSB(args.mp, sc->sa.agno, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	args.minlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	args.maxlen = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	args.prod = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	args.type = XFS_ALLOCTYPE_THIS_AG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	args.resv = resv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 	error = xfs_alloc_vextent(&args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	if (args.fsbno == NULLFSBLOCK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 		return -ENOSPC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	ASSERT(args.len == 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 	*fsbno = args.fsbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) /* Initialize a new AG btree root block with zero entries. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) xrep_init_btblock(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	xfs_fsblock_t			fsb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	struct xfs_buf			**bpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	xfs_btnum_t			btnum,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 	const struct xfs_buf_ops	*ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 	struct xfs_trans		*tp = sc->tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	struct xfs_mount		*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	struct xfs_buf			*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) 	trace_xrep_init_btblock(mp, XFS_FSB_TO_AGNO(mp, fsb),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 			XFS_FSB_TO_AGBNO(mp, fsb), btnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	ASSERT(XFS_FSB_TO_AGNO(mp, fsb) == sc->sa.agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	error = xfs_trans_get_buf(tp, mp->m_ddev_targp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 			XFS_FSB_TO_DADDR(mp, fsb), XFS_FSB_TO_BB(mp, 1), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) 			&bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) 	xfs_buf_zero(bp, 0, BBTOB(bp->b_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) 	xfs_btree_init_block(mp, bp, btnum, 0, 0, sc->sa.agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) 	xfs_trans_buf_set_type(tp, bp, XFS_BLFT_BTREE_BUF);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) 	xfs_trans_log_buf(tp, bp, 0, BBTOB(bp->b_length) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) 	bp->b_ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 	*bpp = bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  * Reconstructing per-AG Btrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * When a space btree is corrupt, we don't bother trying to fix it.  Instead,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  * we scan secondary space metadata to derive the records that should be in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * the damaged btree, initialize a fresh btree root, and insert the records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  * Note that for rebuilding the rmapbt we scan all the primary data to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)  * generate the new records.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)  * However, that leaves the matter of removing all the metadata describing the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)  * old broken structure.  For primary metadata we use the rmap data to collect
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)  * every extent with a matching rmap owner (bitmap); we then iterate all other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379)  * metadata structures with the same rmap owner to collect the extents that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)  * cannot be removed (sublist).  We then subtract sublist from bitmap to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381)  * derive the blocks that were used by the old btree.  These blocks can be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)  * reaped.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)  * For rmapbt reconstructions we must use different tactics for extent
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385)  * collection.  First we iterate all primary metadata (this excludes the old
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)  * rmapbt, obviously) to generate new rmap records.  The gaps in the rmap
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)  * records are collected as bitmap.  The bnobt records are collected as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)  * sublist.  As with the other btrees we subtract sublist from bitmap, and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)  * result (since the rmapbt lives in the free space) are the blocks from the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)  * old rmapbt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)  * Disposal of Blocks from Old per-AG Btrees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)  * Now that we've constructed a new btree to replace the damaged one, we want
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)  * to dispose of the blocks that (we think) the old btree was using.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)  * Previously, we used the rmapbt to collect the extents (bitmap) with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)  * rmap owner corresponding to the tree we rebuilt, collected extents for any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)  * blocks with the same rmap owner that are owned by another data structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)  * (sublist), and subtracted sublist from bitmap.  In theory the extents
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400)  * remaining in bitmap are the old btree's blocks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402)  * Unfortunately, it's possible that the btree was crosslinked with other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)  * blocks on disk.  The rmap data can tell us if there are multiple owners, so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)  * if the rmapbt says there is an owner of this block other than @oinfo, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)  * the block is crosslinked.  Remove the reverse mapping and continue.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)  * If there is one rmap record, we can free the block, which removes the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)  * reverse mapping but doesn't add the block to the free space.  Our repair
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)  * strategy is to hope the other metadata objects crosslinked on this block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)  * will be rebuilt (atop different blocks), thereby removing all the cross
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)  * links.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413)  * If there are no rmap records at all, we also free the block.  If the btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414)  * being rebuilt lives in the free space (bnobt/cntbt/rmapbt) then there isn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)  * supposed to be a rmap record and everything is ok.  For other btrees there
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)  * had to have been an rmap entry for the block to have ended up on @bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)  * so if it's gone now there's something wrong and the fs will shut down.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)  * Note: If there are multiple rmap records with only the same rmap owner as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420)  * the btree we're trying to rebuild and the block is indeed owned by another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)  * data structure with the same rmap owner, then the block will be in sublist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422)  * and therefore doesn't need disposal.  If there are multiple rmap records
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)  * with only the same rmap owner but the block is not owned by something with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)  * the same rmap owner, the block will be freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426)  * The caller is responsible for locking the AG headers for the entire rebuild
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)  * operation so that nothing else can sneak in and change the AG state while
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)  * we're not looking.  We also assume that the caller already invalidated any
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)  * buffers associated with @bitmap.
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433)  * Invalidate buffers for per-AG btree blocks we're dumping.  This function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)  * is not intended for use with file data repairs; we have bunmapi for that.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) xrep_invalidate_blocks(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	struct xbitmap		*bitmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	struct xbitmap_range	*bmr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) 	struct xbitmap_range	*n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 	struct xfs_buf		*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) 	xfs_fsblock_t		fsbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) 	 * For each block in each extent, see if there's an incore buffer for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) 	 * exactly that block; if so, invalidate it.  The buffer cache only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) 	 * lets us look for one buffer at a time, so we have to look one block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) 	 * at a time.  Avoid invalidating AG headers and post-EOFS blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) 	 * because we never own those; and if we can't TRYLOCK the buffer we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) 	 * assume it's owned by someone else.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) 	for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) 		/* Skip AG headers and post-EOFS blocks */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) 		if (!xfs_verify_fsbno(sc->mp, fsbno))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) 		bp = xfs_buf_incore(sc->mp->m_ddev_targp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) 				XFS_FSB_TO_DADDR(sc->mp, fsbno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) 				XFS_FSB_TO_BB(sc->mp, 1), XBF_TRYLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 		if (bp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) 			xfs_trans_bjoin(sc->tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 			xfs_trans_binval(sc->tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) /* Ensure the freelist is the correct size. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) xrep_fix_freelist(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	bool			can_shrink)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 	struct xfs_alloc_arg	args = {0};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 	args.mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 	args.tp = sc->tp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 	args.agno = sc->sa.agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	args.alignment = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 	args.pag = sc->sa.pag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	return xfs_alloc_fix_freelist(&args,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 			can_shrink ? 0 : XFS_ALLOC_FLAG_NOSHRINK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)  * Put a block back on the AGFL.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) xrep_put_freelist(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	xfs_agblock_t		agbno)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	/* Make sure there's space on the freelist. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 	error = xrep_fix_freelist(sc, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 	 * Since we're "freeing" a lost block onto the AGFL, we have to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 	 * create an rmap for the block prior to merging it or else other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 	 * parts will break.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	error = xfs_rmap_alloc(sc->tp, sc->sa.agf_bp, sc->sa.agno, agbno, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 			&XFS_RMAP_OINFO_AG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 	/* Put the block on the AGFL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) 	error = xfs_alloc_put_freelist(sc->tp, sc->sa.agf_bp, sc->sa.agfl_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 			agbno, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	xfs_extent_busy_insert(sc->tp, sc->sa.agno, agbno, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 			XFS_EXTENT_BUSY_SKIP_DISCARD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) /* Dispose of a single block. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) xrep_reap_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) 	xfs_fsblock_t			fsbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 	const struct xfs_owner_info	*oinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) 	enum xfs_ag_resv_type		resv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) 	struct xfs_btree_cur		*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) 	struct xfs_buf			*agf_bp = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) 	xfs_agnumber_t			agno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) 	xfs_agblock_t			agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) 	bool				has_other_rmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	agno = XFS_FSB_TO_AGNO(sc->mp, fsbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) 	agbno = XFS_FSB_TO_AGBNO(sc->mp, fsbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) 	 * If we are repairing per-inode metadata, we need to read in the AGF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) 	 * buffer.  Otherwise, we're repairing a per-AG structure, so reuse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) 	 * the AGF buffer that the setup functions already grabbed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) 	if (sc->ip) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) 		error = xfs_alloc_read_agf(sc->mp, sc->tp, agno, 0, &agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 		agf_bp = sc->sa.agf_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) 	cur = xfs_rmapbt_init_cursor(sc->mp, sc->tp, agf_bp, agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) 	/* Can we find any other rmappings? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) 	error = xfs_rmap_has_other_keys(cur, agbno, 1, oinfo, &has_other_rmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) 	xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 		goto out_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 	 * If there are other rmappings, this block is cross linked and must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	 * not be freed.  Remove the reverse mapping and move on.  Otherwise,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 	 * we were the only owner of the block, so free the extent, which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	 * also remove the rmap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	 * XXX: XFS doesn't support detecting the case where a single block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	 * metadata structure is crosslinked with a multi-block structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	 * because the buffer cache doesn't detect aliasing problems, so we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 	 * can't fix 100% of crosslinking problems (yet).  The verifiers will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	 * blow on writeout, the filesystem will shut down, and the admin gets
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) 	 * to run xfs_repair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) 	if (has_other_rmap)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) 		error = xfs_rmap_free(sc->tp, agf_bp, agno, agbno, 1, oinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) 	else if (resv == XFS_AG_RESV_AGFL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) 		error = xrep_put_freelist(sc, agbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) 		error = xfs_free_extent(sc->tp, fsbno, 1, oinfo, resv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) 	if (agf_bp != sc->sa.agf_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) 		xfs_trans_brelse(sc->tp, agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) 	if (sc->ip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) 		return xfs_trans_roll_inode(&sc->tp, sc->ip);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) 	return xrep_roll_ag_trans(sc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) out_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) 	if (agf_bp != sc->sa.agf_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) 		xfs_trans_brelse(sc->tp, agf_bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* Dispose of every block of every extent in the bitmap. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) xrep_reap_extents(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) 	struct xbitmap			*bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) 	const struct xfs_owner_info	*oinfo,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) 	enum xfs_ag_resv_type		type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) 	struct xbitmap_range		*bmr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) 	struct xbitmap_range		*n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) 	xfs_fsblock_t			fsbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) 	ASSERT(xfs_sb_version_hasrmapbt(&sc->mp->m_sb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) 	for_each_xbitmap_block(fsbno, bmr, n, bitmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) 		ASSERT(sc->ip != NULL ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) 		       XFS_FSB_TO_AGNO(sc->mp, fsbno) == sc->sa.agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) 		trace_xrep_dispose_btree_extent(sc->mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 				XFS_FSB_TO_AGNO(sc->mp, fsbno),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 				XFS_FSB_TO_AGBNO(sc->mp, fsbno), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 		error = xrep_reap_block(sc, fsbno, oinfo, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)  * Finding per-AG Btree Roots for AGF/AGI Reconstruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629)  * If the AGF or AGI become slightly corrupted, it may be necessary to rebuild
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630)  * the AG headers by using the rmap data to rummage through the AG looking for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631)  * btree roots.  This is not guaranteed to work if the AG is heavily damaged
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)  * or the rmap data are corrupt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634)  * Callers of xrep_find_ag_btree_roots must lock the AGF and AGFL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)  * buffers if the AGF is being rebuilt; or the AGF and AGI buffers if the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636)  * AGI is being rebuilt.  It must maintain these locks until it's safe for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637)  * other threads to change the btrees' shapes.  The caller provides
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)  * information about the btrees to look for by passing in an array of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639)  * xrep_find_ag_btree with the (rmap owner, buf_ops, magic) fields set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)  * The (root, height) fields will be set on return if anything is found.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641)  * last element of the array should have a NULL buf_ops to mark the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642)  * array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644)  * For every rmapbt record matching any of the rmap owners in btree_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)  * read each block referenced by the rmap record.  If the block is a btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646)  * block from this filesystem matching any of the magic numbers and has a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647)  * level higher than what we've already seen, remember the block and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648)  * height of the tree required to have such a block.  When the call completes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)  * we return the highest block we've found for each btree description; those
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650)  * should be the roots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) struct xrep_findroot {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) 	struct xfs_scrub		*sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) 	struct xfs_buf			*agfl_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) 	struct xfs_agf			*agf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) 	struct xrep_find_ag_btree	*btree_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) /* See if our block is in the AGFL. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) xrep_findroot_agfl_walk(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) 	struct xfs_mount	*mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) 	xfs_agblock_t		bno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) 	void			*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) 	xfs_agblock_t		*agbno = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669) 	return (*agbno == bno) ? -ECANCELED : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) /* Does this block match the btree information passed in? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) xrep_findroot_block(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) 	struct xrep_findroot		*ri,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) 	struct xrep_find_ag_btree	*fab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) 	uint64_t			owner,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) 	xfs_agblock_t			agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) 	bool				*done_with_block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) 	struct xfs_mount		*mp = ri->sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) 	struct xfs_buf			*bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) 	struct xfs_btree_block		*btblock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) 	xfs_daddr_t			daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) 	int				block_level;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) 	daddr = XFS_AGB_TO_DADDR(mp, ri->sc->sa.agno, agbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) 	 * Blocks in the AGFL have stale contents that might just happen to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) 	 * have a matching magic and uuid.  We don't want to pull these blocks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) 	 * in as part of a tree root, so we have to filter out the AGFL stuff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) 	 * here.  If the AGFL looks insane we'll just refuse to repair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) 	if (owner == XFS_RMAP_OWN_AG) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) 		error = xfs_agfl_walk(mp, ri->agf, ri->agfl_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) 				xrep_findroot_agfl_walk, &agbno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) 		if (error == -ECANCELED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) 		if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) 			return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) 	 * Read the buffer into memory so that we can see if it's a match for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) 	 * our btree type.  We have no clue if it is beforehand, and we want to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) 	 * avoid xfs_trans_read_buf's behavior of dumping the DONE state (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) 	 * will cause needless disk reads in subsequent calls to this function)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) 	 * and logging metadata verifier failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) 	 * Therefore, pass in NULL buffer ops.  If the buffer was already in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) 	 * memory from some other caller it will already have b_ops assigned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) 	 * If it was in memory from a previous unsuccessful findroot_block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) 	 * call, the buffer won't have b_ops but it should be clean and ready
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) 	 * for us to try to verify if the read call succeeds.  The same applies
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) 	 * if the buffer wasn't in memory at all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) 	 * Note: If we never match a btree type with this buffer, it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) 	 * left in memory with NULL b_ops.  This shouldn't be a problem unless
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) 	 * the buffer gets written.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) 	error = xfs_trans_read_buf(mp, ri->sc->tp, mp->m_ddev_targp, daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) 			mp->m_bsize, 0, &bp, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) 	if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) 		return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) 	/* Ensure the block magic matches the btree type we're looking for. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) 	btblock = XFS_BUF_TO_BLOCK(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) 	ASSERT(fab->buf_ops->magic[1] != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) 	if (btblock->bb_magic != fab->buf_ops->magic[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) 	 * If the buffer already has ops applied and they're not the ones for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) 	 * this btree type, we know this block doesn't match the btree and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) 	 * can bail out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) 	 * If the buffer ops match ours, someone else has already validated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) 	 * the block for us, so we can move on to checking if this is a root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) 	 * block candidate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) 	 * If the buffer does not have ops, nobody has successfully validated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) 	 * the contents and the buffer cannot be dirty.  If the magic, uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) 	 * and structure match this btree type then we'll move on to checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) 	 * if it's a root block candidate.  If there is no match, bail out.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) 	if (bp->b_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) 		if (bp->b_ops != fab->buf_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) 		ASSERT(!xfs_trans_buf_is_dirty(bp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) 		if (!uuid_equal(&btblock->bb_u.s.bb_uuid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) 				&mp->m_sb.sb_meta_uuid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) 		 * Read verifiers can reference b_ops, so we set the pointer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) 		 * here.  If the verifier fails we'll reset the buffer state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) 		 * to what it was before we touched the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) 		bp->b_ops = fab->buf_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) 		fab->buf_ops->verify_read(bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) 		if (bp->b_error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) 			bp->b_ops = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) 			bp->b_error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) 		 * Some read verifiers will (re)set b_ops, so we must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) 		 * careful not to change b_ops after running the verifier.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) 	 * This block passes the magic/uuid and verifier tests for this btree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) 	 * type.  We don't need the caller to try the other tree types.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) 	*done_with_block = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) 	 * Compare this btree block's level to the height of the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) 	 * candidate root block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) 	 * If the level matches the root we found previously, throw away both
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) 	 * blocks because there can't be two candidate roots.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) 	 * If level is lower in the tree than the root we found previously,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) 	 * ignore this block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) 	block_level = xfs_btree_get_level(btblock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) 	if (block_level + 1 == fab->height) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) 		fab->root = NULLAGBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) 	} else if (block_level < fab->height) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) 	 * This is the highest block in the tree that we've found so far.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) 	 * Update the btree height to reflect what we've learned from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) 	 * block.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) 	fab->height = block_level + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) 	 * If this block doesn't have sibling pointers, then it's the new root
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) 	 * block candidate.  Otherwise, the root will be found farther up the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) 	 * tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) 	if (btblock->bb_u.s.bb_leftsib == cpu_to_be32(NULLAGBLOCK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) 	    btblock->bb_u.s.bb_rightsib == cpu_to_be32(NULLAGBLOCK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) 		fab->root = agbno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) 		fab->root = NULLAGBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) 	trace_xrep_findroot_block(mp, ri->sc->sa.agno, agbno,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) 			be32_to_cpu(btblock->bb_magic), fab->height - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) 	xfs_trans_brelse(ri->sc->tp, bp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825)  * Do any of the blocks in this rmap record match one of the btrees we're
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)  * looking for?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) STATIC int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) xrep_findroot_rmap(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) 	struct xfs_btree_cur		*cur,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) 	struct xfs_rmap_irec		*rec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) 	void				*priv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) 	struct xrep_findroot		*ri = priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) 	struct xrep_find_ag_btree	*fab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) 	xfs_agblock_t			b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) 	bool				done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) 	int				error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) 	/* Ignore anything that isn't AG metadata. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) 	if (!XFS_RMAP_NON_INODE_OWNER(rec->rm_owner))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) 	/* Otherwise scan each block + btree type. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) 	for (b = 0; b < rec->rm_blockcount; b++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) 		done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) 		for (fab = ri->btree_info; fab->buf_ops; fab++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) 			if (rec->rm_owner != fab->rmap_owner)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) 			error = xrep_findroot_block(ri, fab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) 					rec->rm_owner, rec->rm_startblock + b,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) 					&done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) 			if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) 				return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) 			if (done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) /* Find the roots of the per-AG btrees described in btree_info. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) xrep_find_ag_btree_roots(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) 	struct xfs_scrub		*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) 	struct xfs_buf			*agf_bp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) 	struct xrep_find_ag_btree	*btree_info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) 	struct xfs_buf			*agfl_bp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) 	struct xfs_mount		*mp = sc->mp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) 	struct xrep_findroot		ri;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) 	struct xrep_find_ag_btree	*fab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) 	struct xfs_btree_cur		*cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) 	int				error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) 	ASSERT(xfs_buf_islocked(agf_bp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) 	ASSERT(agfl_bp == NULL || xfs_buf_islocked(agfl_bp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) 	ri.sc = sc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) 	ri.btree_info = btree_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) 	ri.agf = agf_bp->b_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) 	ri.agfl_bp = agfl_bp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) 	for (fab = btree_info; fab->buf_ops; fab++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) 		ASSERT(agfl_bp || fab->rmap_owner != XFS_RMAP_OWN_AG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) 		ASSERT(XFS_RMAP_NON_INODE_OWNER(fab->rmap_owner));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) 		fab->root = NULLAGBLOCK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) 		fab->height = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) 	cur = xfs_rmapbt_init_cursor(mp, sc->tp, agf_bp, sc->sa.agno);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) 	error = xfs_rmap_query_all(cur, xrep_findroot_rmap, &ri);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) 	xfs_btree_del_cursor(cur, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) /* Force a quotacheck the next time we mount. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) xrep_force_quotacheck(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) 	struct xfs_scrub	*sc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) 	xfs_dqtype_t		type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) 	uint			flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) 	flag = xfs_quota_chkd_flag(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) 	if (!(flag & sc->mp->m_qflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) 	sc->mp->m_qflags &= ~flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) 	spin_lock(&sc->mp->m_sb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) 	sc->mp->m_sb.sb_qflags &= ~flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) 	spin_unlock(&sc->mp->m_sb_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) 	xfs_log_sb(sc->tp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918)  * Attach dquots to this inode, or schedule quotacheck to fix them.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920)  * This function ensures that the appropriate dquots are attached to an inode.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921)  * We cannot allow the dquot code to allocate an on-disk dquot block here
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922)  * because we're already in transaction context with the inode locked.  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)  * on-disk dquot should already exist anyway.  If the quota code signals
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924)  * corruption or missing quota information, schedule quotacheck, which will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925)  * repair corruptions in the quota metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) xrep_ino_dqattach(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) 	struct xfs_scrub	*sc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) 	int			error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) 	error = xfs_qm_dqattach_locked(sc->ip, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) 	switch (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) 	case -EFSBADCRC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) 	case -EFSCORRUPTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) 	case -ENOENT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) 		xfs_err_ratelimited(sc->mp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) "inode %llu repair encountered quota error %d, quotacheck forced.",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) 				(unsigned long long)sc->ip->i_ino, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) 		if (XFS_IS_UQUOTA_ON(sc->mp) && !sc->ip->i_udquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) 			xrep_force_quotacheck(sc, XFS_DQTYPE_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) 		if (XFS_IS_GQUOTA_ON(sc->mp) && !sc->ip->i_gdquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) 			xrep_force_quotacheck(sc, XFS_DQTYPE_GROUP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) 		if (XFS_IS_PQUOTA_ON(sc->mp) && !sc->ip->i_pdquot)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) 			xrep_force_quotacheck(sc, XFS_DQTYPE_PROJ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) 		/* fall through */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) 	case -ESRCH:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) 		error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) 	return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) }