xref: /openbmc/linux/fs/xfs/scrub/fscounters.c (revision e147a756ab263f9d10eafd08b79b9fac1b08e56c)
175efa57dSDarrick J. Wong // SPDX-License-Identifier: GPL-2.0+
275efa57dSDarrick J. Wong /*
375efa57dSDarrick J. Wong  * Copyright (C) 2019 Oracle.  All Rights Reserved.
475efa57dSDarrick J. Wong  * Author: Darrick J. Wong <darrick.wong@oracle.com>
575efa57dSDarrick J. Wong  */
675efa57dSDarrick J. Wong #include "xfs.h"
775efa57dSDarrick J. Wong #include "xfs_fs.h"
875efa57dSDarrick J. Wong #include "xfs_shared.h"
975efa57dSDarrick J. Wong #include "xfs_format.h"
1075efa57dSDarrick J. Wong #include "xfs_trans_resv.h"
1175efa57dSDarrick J. Wong #include "xfs_mount.h"
1275efa57dSDarrick J. Wong #include "xfs_sb.h"
1375efa57dSDarrick J. Wong #include "xfs_alloc.h"
1475efa57dSDarrick J. Wong #include "xfs_ialloc.h"
1575efa57dSDarrick J. Wong #include "xfs_health.h"
16*e147a756SDarrick J. Wong #include "xfs_btree.h"
1775efa57dSDarrick J. Wong #include "scrub/scrub.h"
1875efa57dSDarrick J. Wong #include "scrub/common.h"
1975efa57dSDarrick J. Wong #include "scrub/trace.h"
2075efa57dSDarrick J. Wong 
2175efa57dSDarrick J. Wong /*
2275efa57dSDarrick J. Wong  * FS Summary Counters
2375efa57dSDarrick J. Wong  * ===================
2475efa57dSDarrick J. Wong  *
2575efa57dSDarrick J. Wong  * The basics of filesystem summary counter checking are that we iterate the
2675efa57dSDarrick J. Wong  * AGs counting the number of free blocks, free space btree blocks, per-AG
2775efa57dSDarrick J. Wong  * reservations, inodes, delayed allocation reservations, and free inodes.
2875efa57dSDarrick J. Wong  * Then we compare what we computed against the in-core counters.
2975efa57dSDarrick J. Wong  *
3075efa57dSDarrick J. Wong  * However, the reality is that summary counters are a tricky beast to check.
3175efa57dSDarrick J. Wong  * While we /could/ freeze the filesystem and scramble around the AGs counting
3275efa57dSDarrick J. Wong  * the free blocks, in practice we prefer not do that for a scan because
3375efa57dSDarrick J. Wong  * freezing is costly.  To get around this, we added a per-cpu counter of the
3475efa57dSDarrick J. Wong  * delalloc reservations so that we can rotor around the AGs relatively
3575efa57dSDarrick J. Wong  * quickly, and we allow the counts to be slightly off because we're not taking
3675efa57dSDarrick J. Wong  * any locks while we do this.
3775efa57dSDarrick J. Wong  *
3875efa57dSDarrick J. Wong  * So the first thing we do is warm up the buffer cache in the setup routine by
3975efa57dSDarrick J. Wong  * walking all the AGs to make sure the incore per-AG structure has been
4075efa57dSDarrick J. Wong  * initialized.  The expected value calculation then iterates the incore per-AG
4175efa57dSDarrick J. Wong  * structures as quickly as it can.  We snapshot the percpu counters before and
4275efa57dSDarrick J. Wong  * after this operation and use the difference in counter values to guess at
4375efa57dSDarrick J. Wong  * our tolerance for mismatch between expected and actual counter values.
4475efa57dSDarrick J. Wong  */
4575efa57dSDarrick J. Wong 
4675efa57dSDarrick J. Wong /*
4775efa57dSDarrick J. Wong  * Since the expected value computation is lockless but only browses incore
4875efa57dSDarrick J. Wong  * values, the percpu counters should be fairly close to each other.  However,
4975efa57dSDarrick J. Wong  * we'll allow ourselves to be off by at least this (arbitrary) amount.
5075efa57dSDarrick J. Wong  */
5175efa57dSDarrick J. Wong #define XCHK_FSCOUNT_MIN_VARIANCE	(512)
5275efa57dSDarrick J. Wong 
5375efa57dSDarrick J. Wong /*
5475efa57dSDarrick J. Wong  * Make sure the per-AG structure has been initialized from the on-disk header
5575efa57dSDarrick J. Wong  * contents and trust that the incore counters match the ondisk counters.  (The
5675efa57dSDarrick J. Wong  * AGF and AGI scrubbers check them, and a normal xfs_scrub run checks the
5775efa57dSDarrick J. Wong  * summary counters after checking all AG headers).  Do this from the setup
5875efa57dSDarrick J. Wong  * function so that the inner AG aggregation loop runs as quickly as possible.
5975efa57dSDarrick J. Wong  *
6075efa57dSDarrick J. Wong  * This function runs during the setup phase /before/ we start checking any
6175efa57dSDarrick J. Wong  * metadata.
6275efa57dSDarrick J. Wong  */
6375efa57dSDarrick J. Wong STATIC int
6475efa57dSDarrick J. Wong xchk_fscount_warmup(
6575efa57dSDarrick J. Wong 	struct xfs_scrub	*sc)
6675efa57dSDarrick J. Wong {
6775efa57dSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
6875efa57dSDarrick J. Wong 	struct xfs_buf		*agi_bp = NULL;
6975efa57dSDarrick J. Wong 	struct xfs_buf		*agf_bp = NULL;
7075efa57dSDarrick J. Wong 	struct xfs_perag	*pag = NULL;
7175efa57dSDarrick J. Wong 	xfs_agnumber_t		agno;
7275efa57dSDarrick J. Wong 	int			error = 0;
7375efa57dSDarrick J. Wong 
7475efa57dSDarrick J. Wong 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
7575efa57dSDarrick J. Wong 		pag = xfs_perag_get(mp, agno);
7675efa57dSDarrick J. Wong 
7775efa57dSDarrick J. Wong 		if (pag->pagi_init && pag->pagf_init)
7875efa57dSDarrick J. Wong 			goto next_loop_perag;
7975efa57dSDarrick J. Wong 
8075efa57dSDarrick J. Wong 		/* Lock both AG headers. */
8175efa57dSDarrick J. Wong 		error = xfs_ialloc_read_agi(mp, sc->tp, agno, &agi_bp);
8275efa57dSDarrick J. Wong 		if (error)
8375efa57dSDarrick J. Wong 			break;
8475efa57dSDarrick J. Wong 		error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, &agf_bp);
8575efa57dSDarrick J. Wong 		if (error)
8675efa57dSDarrick J. Wong 			break;
8775efa57dSDarrick J. Wong 
8875efa57dSDarrick J. Wong 		/*
8975efa57dSDarrick J. Wong 		 * These are supposed to be initialized by the header read
9075efa57dSDarrick J. Wong 		 * function.
9175efa57dSDarrick J. Wong 		 */
9275efa57dSDarrick J. Wong 		error = -EFSCORRUPTED;
9375efa57dSDarrick J. Wong 		if (!pag->pagi_init || !pag->pagf_init)
9475efa57dSDarrick J. Wong 			break;
9575efa57dSDarrick J. Wong 
9675efa57dSDarrick J. Wong 		xfs_buf_relse(agf_bp);
9775efa57dSDarrick J. Wong 		agf_bp = NULL;
9875efa57dSDarrick J. Wong 		xfs_buf_relse(agi_bp);
9975efa57dSDarrick J. Wong 		agi_bp = NULL;
10075efa57dSDarrick J. Wong next_loop_perag:
10175efa57dSDarrick J. Wong 		xfs_perag_put(pag);
10275efa57dSDarrick J. Wong 		pag = NULL;
10375efa57dSDarrick J. Wong 		error = 0;
10475efa57dSDarrick J. Wong 
1058ef34723SDarrick J. Wong 		if (xchk_should_terminate(sc, &error))
10675efa57dSDarrick J. Wong 			break;
10775efa57dSDarrick J. Wong 	}
10875efa57dSDarrick J. Wong 
10975efa57dSDarrick J. Wong 	if (agf_bp)
11075efa57dSDarrick J. Wong 		xfs_buf_relse(agf_bp);
11175efa57dSDarrick J. Wong 	if (agi_bp)
11275efa57dSDarrick J. Wong 		xfs_buf_relse(agi_bp);
11375efa57dSDarrick J. Wong 	if (pag)
11475efa57dSDarrick J. Wong 		xfs_perag_put(pag);
11575efa57dSDarrick J. Wong 	return error;
11675efa57dSDarrick J. Wong }
11775efa57dSDarrick J. Wong 
11875efa57dSDarrick J. Wong int
11975efa57dSDarrick J. Wong xchk_setup_fscounters(
120026f57ebSDarrick J. Wong 	struct xfs_scrub	*sc)
12175efa57dSDarrick J. Wong {
12275efa57dSDarrick J. Wong 	struct xchk_fscounters	*fsc;
12375efa57dSDarrick J. Wong 	int			error;
12475efa57dSDarrick J. Wong 
125707e0ddaSTetsuo Handa 	sc->buf = kmem_zalloc(sizeof(struct xchk_fscounters), 0);
12675efa57dSDarrick J. Wong 	if (!sc->buf)
12775efa57dSDarrick J. Wong 		return -ENOMEM;
12875efa57dSDarrick J. Wong 	fsc = sc->buf;
12975efa57dSDarrick J. Wong 
13075efa57dSDarrick J. Wong 	xfs_icount_range(sc->mp, &fsc->icount_min, &fsc->icount_max);
13175efa57dSDarrick J. Wong 
13275efa57dSDarrick J. Wong 	/* We must get the incore counters set up before we can proceed. */
13375efa57dSDarrick J. Wong 	error = xchk_fscount_warmup(sc);
13475efa57dSDarrick J. Wong 	if (error)
13575efa57dSDarrick J. Wong 		return error;
13675efa57dSDarrick J. Wong 
13775efa57dSDarrick J. Wong 	/*
13875efa57dSDarrick J. Wong 	 * Pause background reclaim while we're scrubbing to reduce the
13975efa57dSDarrick J. Wong 	 * likelihood of background perturbations to the counters throwing off
14075efa57dSDarrick J. Wong 	 * our calculations.
14175efa57dSDarrick J. Wong 	 */
14275efa57dSDarrick J. Wong 	xchk_stop_reaping(sc);
14375efa57dSDarrick J. Wong 
14475efa57dSDarrick J. Wong 	return xchk_trans_alloc(sc, 0);
14575efa57dSDarrick J. Wong }
14675efa57dSDarrick J. Wong 
147*e147a756SDarrick J. Wong /* Count free space btree blocks manually for pre-lazysbcount filesystems. */
148*e147a756SDarrick J. Wong static int
149*e147a756SDarrick J. Wong xchk_fscount_btreeblks(
150*e147a756SDarrick J. Wong 	struct xfs_scrub	*sc,
151*e147a756SDarrick J. Wong 	struct xchk_fscounters	*fsc,
152*e147a756SDarrick J. Wong 	xfs_agnumber_t		agno)
153*e147a756SDarrick J. Wong {
154*e147a756SDarrick J. Wong 	xfs_extlen_t		blocks;
155*e147a756SDarrick J. Wong 	int			error;
156*e147a756SDarrick J. Wong 
157*e147a756SDarrick J. Wong 	error = xchk_ag_init(sc, agno, &sc->sa);
158*e147a756SDarrick J. Wong 	if (error)
159*e147a756SDarrick J. Wong 		return error;
160*e147a756SDarrick J. Wong 
161*e147a756SDarrick J. Wong 	error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
162*e147a756SDarrick J. Wong 	if (error)
163*e147a756SDarrick J. Wong 		goto out_free;
164*e147a756SDarrick J. Wong 	fsc->fdblocks += blocks - 1;
165*e147a756SDarrick J. Wong 
166*e147a756SDarrick J. Wong 	error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
167*e147a756SDarrick J. Wong 	if (error)
168*e147a756SDarrick J. Wong 		goto out_free;
169*e147a756SDarrick J. Wong 	fsc->fdblocks += blocks - 1;
170*e147a756SDarrick J. Wong 
171*e147a756SDarrick J. Wong out_free:
172*e147a756SDarrick J. Wong 	xchk_ag_free(sc, &sc->sa);
173*e147a756SDarrick J. Wong 	return error;
174*e147a756SDarrick J. Wong }
175*e147a756SDarrick J. Wong 
17675efa57dSDarrick J. Wong /*
17775efa57dSDarrick J. Wong  * Calculate what the global in-core counters ought to be from the incore
17875efa57dSDarrick J. Wong  * per-AG structure.  Callers can compare this to the actual in-core counters
17975efa57dSDarrick J. Wong  * to estimate by how much both in-core and on-disk counters need to be
18075efa57dSDarrick J. Wong  * adjusted.
18175efa57dSDarrick J. Wong  */
18275efa57dSDarrick J. Wong STATIC int
18375efa57dSDarrick J. Wong xchk_fscount_aggregate_agcounts(
18475efa57dSDarrick J. Wong 	struct xfs_scrub	*sc,
18575efa57dSDarrick J. Wong 	struct xchk_fscounters	*fsc)
18675efa57dSDarrick J. Wong {
18775efa57dSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
18875efa57dSDarrick J. Wong 	struct xfs_perag	*pag;
18975efa57dSDarrick J. Wong 	uint64_t		delayed;
19075efa57dSDarrick J. Wong 	xfs_agnumber_t		agno;
19175efa57dSDarrick J. Wong 	int			tries = 8;
1928ef34723SDarrick J. Wong 	int			error = 0;
19375efa57dSDarrick J. Wong 
19475efa57dSDarrick J. Wong retry:
19575efa57dSDarrick J. Wong 	fsc->icount = 0;
19675efa57dSDarrick J. Wong 	fsc->ifree = 0;
19775efa57dSDarrick J. Wong 	fsc->fdblocks = 0;
19875efa57dSDarrick J. Wong 
19975efa57dSDarrick J. Wong 	for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
20075efa57dSDarrick J. Wong 		pag = xfs_perag_get(mp, agno);
20175efa57dSDarrick J. Wong 
20275efa57dSDarrick J. Wong 		/* This somehow got unset since the warmup? */
20375efa57dSDarrick J. Wong 		if (!pag->pagi_init || !pag->pagf_init) {
20475efa57dSDarrick J. Wong 			xfs_perag_put(pag);
20575efa57dSDarrick J. Wong 			return -EFSCORRUPTED;
20675efa57dSDarrick J. Wong 		}
20775efa57dSDarrick J. Wong 
20875efa57dSDarrick J. Wong 		/* Count all the inodes */
20975efa57dSDarrick J. Wong 		fsc->icount += pag->pagi_count;
21075efa57dSDarrick J. Wong 		fsc->ifree += pag->pagi_freecount;
21175efa57dSDarrick J. Wong 
21275efa57dSDarrick J. Wong 		/* Add up the free/freelist/bnobt/cntbt blocks */
21375efa57dSDarrick J. Wong 		fsc->fdblocks += pag->pagf_freeblks;
21475efa57dSDarrick J. Wong 		fsc->fdblocks += pag->pagf_flcount;
215*e147a756SDarrick J. Wong 		if (xfs_sb_version_haslazysbcount(&sc->mp->m_sb)) {
21675efa57dSDarrick J. Wong 			fsc->fdblocks += pag->pagf_btreeblks;
217*e147a756SDarrick J. Wong 		} else {
218*e147a756SDarrick J. Wong 			error = xchk_fscount_btreeblks(sc, fsc, agno);
219*e147a756SDarrick J. Wong 			if (error) {
220*e147a756SDarrick J. Wong 				xfs_perag_put(pag);
221*e147a756SDarrick J. Wong 				break;
222*e147a756SDarrick J. Wong 			}
223*e147a756SDarrick J. Wong 		}
22475efa57dSDarrick J. Wong 
22575efa57dSDarrick J. Wong 		/*
22675efa57dSDarrick J. Wong 		 * Per-AG reservations are taken out of the incore counters,
22775efa57dSDarrick J. Wong 		 * so they must be left out of the free blocks computation.
22875efa57dSDarrick J. Wong 		 */
22975efa57dSDarrick J. Wong 		fsc->fdblocks -= pag->pag_meta_resv.ar_reserved;
23075efa57dSDarrick J. Wong 		fsc->fdblocks -= pag->pag_rmapbt_resv.ar_orig_reserved;
23175efa57dSDarrick J. Wong 
23275efa57dSDarrick J. Wong 		xfs_perag_put(pag);
23375efa57dSDarrick J. Wong 
2348ef34723SDarrick J. Wong 		if (xchk_should_terminate(sc, &error))
23575efa57dSDarrick J. Wong 			break;
23675efa57dSDarrick J. Wong 	}
23775efa57dSDarrick J. Wong 
2388ef34723SDarrick J. Wong 	if (error)
2398ef34723SDarrick J. Wong 		return error;
2408ef34723SDarrick J. Wong 
24175efa57dSDarrick J. Wong 	/*
24275efa57dSDarrick J. Wong 	 * The global incore space reservation is taken from the incore
24375efa57dSDarrick J. Wong 	 * counters, so leave that out of the computation.
24475efa57dSDarrick J. Wong 	 */
24575efa57dSDarrick J. Wong 	fsc->fdblocks -= mp->m_resblks_avail;
24675efa57dSDarrick J. Wong 
24775efa57dSDarrick J. Wong 	/*
24875efa57dSDarrick J. Wong 	 * Delayed allocation reservations are taken out of the incore counters
24975efa57dSDarrick J. Wong 	 * but not recorded on disk, so leave them and their indlen blocks out
25075efa57dSDarrick J. Wong 	 * of the computation.
25175efa57dSDarrick J. Wong 	 */
25275efa57dSDarrick J. Wong 	delayed = percpu_counter_sum(&mp->m_delalloc_blks);
25375efa57dSDarrick J. Wong 	fsc->fdblocks -= delayed;
25475efa57dSDarrick J. Wong 
25575efa57dSDarrick J. Wong 	trace_xchk_fscounters_calc(mp, fsc->icount, fsc->ifree, fsc->fdblocks,
25675efa57dSDarrick J. Wong 			delayed);
25775efa57dSDarrick J. Wong 
25875efa57dSDarrick J. Wong 
25975efa57dSDarrick J. Wong 	/* Bail out if the values we compute are totally nonsense. */
26075efa57dSDarrick J. Wong 	if (fsc->icount < fsc->icount_min || fsc->icount > fsc->icount_max ||
26175efa57dSDarrick J. Wong 	    fsc->fdblocks > mp->m_sb.sb_dblocks ||
26275efa57dSDarrick J. Wong 	    fsc->ifree > fsc->icount_max)
26375efa57dSDarrick J. Wong 		return -EFSCORRUPTED;
26475efa57dSDarrick J. Wong 
26575efa57dSDarrick J. Wong 	/*
26675efa57dSDarrick J. Wong 	 * If ifree > icount then we probably had some perturbation in the
26775efa57dSDarrick J. Wong 	 * counters while we were calculating things.  We'll try a few times
26875efa57dSDarrick J. Wong 	 * to maintain ifree <= icount before giving up.
26975efa57dSDarrick J. Wong 	 */
27075efa57dSDarrick J. Wong 	if (fsc->ifree > fsc->icount) {
27175efa57dSDarrick J. Wong 		if (tries--)
27275efa57dSDarrick J. Wong 			goto retry;
27375efa57dSDarrick J. Wong 		xchk_set_incomplete(sc);
27475efa57dSDarrick J. Wong 		return 0;
27575efa57dSDarrick J. Wong 	}
27675efa57dSDarrick J. Wong 
27775efa57dSDarrick J. Wong 	return 0;
27875efa57dSDarrick J. Wong }
27975efa57dSDarrick J. Wong 
28075efa57dSDarrick J. Wong /*
28175efa57dSDarrick J. Wong  * Is the @counter reasonably close to the @expected value?
28275efa57dSDarrick J. Wong  *
28375efa57dSDarrick J. Wong  * We neither locked nor froze anything in the filesystem while aggregating the
28475efa57dSDarrick J. Wong  * per-AG data to compute the @expected value, which means that the counter
28575efa57dSDarrick J. Wong  * could have changed.  We know the @old_value of the summation of the counter
28675efa57dSDarrick J. Wong  * before the aggregation, and we re-sum the counter now.  If the expected
28775efa57dSDarrick J. Wong  * value falls between the two summations, we're ok.
28875efa57dSDarrick J. Wong  *
28975efa57dSDarrick J. Wong  * Otherwise, we /might/ have a problem.  If the change in the summations is
29075efa57dSDarrick J. Wong  * more than we want to tolerate, the filesystem is probably busy and we should
29175efa57dSDarrick J. Wong  * just send back INCOMPLETE and see if userspace will try again.
29275efa57dSDarrick J. Wong  */
29375efa57dSDarrick J. Wong static inline bool
29475efa57dSDarrick J. Wong xchk_fscount_within_range(
29575efa57dSDarrick J. Wong 	struct xfs_scrub	*sc,
29675efa57dSDarrick J. Wong 	const int64_t		old_value,
29775efa57dSDarrick J. Wong 	struct percpu_counter	*counter,
29875efa57dSDarrick J. Wong 	uint64_t		expected)
29975efa57dSDarrick J. Wong {
30075efa57dSDarrick J. Wong 	int64_t			min_value, max_value;
30175efa57dSDarrick J. Wong 	int64_t			curr_value = percpu_counter_sum(counter);
30275efa57dSDarrick J. Wong 
30375efa57dSDarrick J. Wong 	trace_xchk_fscounters_within_range(sc->mp, expected, curr_value,
30475efa57dSDarrick J. Wong 			old_value);
30575efa57dSDarrick J. Wong 
30675efa57dSDarrick J. Wong 	/* Negative values are always wrong. */
30775efa57dSDarrick J. Wong 	if (curr_value < 0)
30875efa57dSDarrick J. Wong 		return false;
30975efa57dSDarrick J. Wong 
31075efa57dSDarrick J. Wong 	/* Exact matches are always ok. */
31175efa57dSDarrick J. Wong 	if (curr_value == expected)
31275efa57dSDarrick J. Wong 		return true;
31375efa57dSDarrick J. Wong 
31475efa57dSDarrick J. Wong 	min_value = min(old_value, curr_value);
31575efa57dSDarrick J. Wong 	max_value = max(old_value, curr_value);
31675efa57dSDarrick J. Wong 
31775efa57dSDarrick J. Wong 	/* Within the before-and-after range is ok. */
31875efa57dSDarrick J. Wong 	if (expected >= min_value && expected <= max_value)
31975efa57dSDarrick J. Wong 		return true;
32075efa57dSDarrick J. Wong 
32175efa57dSDarrick J. Wong 	/*
32275efa57dSDarrick J. Wong 	 * If the difference between the two summations is too large, the fs
32375efa57dSDarrick J. Wong 	 * might just be busy and so we'll mark the scrub incomplete.  Return
32475efa57dSDarrick J. Wong 	 * true here so that we don't mark the counter corrupt.
32575efa57dSDarrick J. Wong 	 *
32675efa57dSDarrick J. Wong 	 * XXX: In the future when userspace can grant scrub permission to
32775efa57dSDarrick J. Wong 	 * quiesce the filesystem to solve the outsized variance problem, this
32875efa57dSDarrick J. Wong 	 * check should be moved up and the return code changed to signal to
32975efa57dSDarrick J. Wong 	 * userspace that we need quiesce permission.
33075efa57dSDarrick J. Wong 	 */
33175efa57dSDarrick J. Wong 	if (max_value - min_value >= XCHK_FSCOUNT_MIN_VARIANCE) {
33275efa57dSDarrick J. Wong 		xchk_set_incomplete(sc);
33375efa57dSDarrick J. Wong 		return true;
33475efa57dSDarrick J. Wong 	}
33575efa57dSDarrick J. Wong 
33675efa57dSDarrick J. Wong 	return false;
33775efa57dSDarrick J. Wong }
33875efa57dSDarrick J. Wong 
33975efa57dSDarrick J. Wong /* Check the superblock counters. */
34075efa57dSDarrick J. Wong int
34175efa57dSDarrick J. Wong xchk_fscounters(
34275efa57dSDarrick J. Wong 	struct xfs_scrub	*sc)
34375efa57dSDarrick J. Wong {
34475efa57dSDarrick J. Wong 	struct xfs_mount	*mp = sc->mp;
34575efa57dSDarrick J. Wong 	struct xchk_fscounters	*fsc = sc->buf;
34675efa57dSDarrick J. Wong 	int64_t			icount, ifree, fdblocks;
34775efa57dSDarrick J. Wong 	int			error;
34875efa57dSDarrick J. Wong 
34975efa57dSDarrick J. Wong 	/* Snapshot the percpu counters. */
35075efa57dSDarrick J. Wong 	icount = percpu_counter_sum(&mp->m_icount);
35175efa57dSDarrick J. Wong 	ifree = percpu_counter_sum(&mp->m_ifree);
35275efa57dSDarrick J. Wong 	fdblocks = percpu_counter_sum(&mp->m_fdblocks);
35375efa57dSDarrick J. Wong 
35475efa57dSDarrick J. Wong 	/* No negative values, please! */
35575efa57dSDarrick J. Wong 	if (icount < 0 || ifree < 0 || fdblocks < 0)
35675efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
35775efa57dSDarrick J. Wong 
35875efa57dSDarrick J. Wong 	/* See if icount is obviously wrong. */
35975efa57dSDarrick J. Wong 	if (icount < fsc->icount_min || icount > fsc->icount_max)
36075efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
36175efa57dSDarrick J. Wong 
36275efa57dSDarrick J. Wong 	/* See if fdblocks is obviously wrong. */
36375efa57dSDarrick J. Wong 	if (fdblocks > mp->m_sb.sb_dblocks)
36475efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
36575efa57dSDarrick J. Wong 
36675efa57dSDarrick J. Wong 	/*
36775efa57dSDarrick J. Wong 	 * If ifree exceeds icount by more than the minimum variance then
36875efa57dSDarrick J. Wong 	 * something's probably wrong with the counters.
36975efa57dSDarrick J. Wong 	 */
37075efa57dSDarrick J. Wong 	if (ifree > icount && ifree - icount > XCHK_FSCOUNT_MIN_VARIANCE)
37175efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
37275efa57dSDarrick J. Wong 
37375efa57dSDarrick J. Wong 	/* Walk the incore AG headers to calculate the expected counters. */
37475efa57dSDarrick J. Wong 	error = xchk_fscount_aggregate_agcounts(sc, fsc);
37575efa57dSDarrick J. Wong 	if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
37675efa57dSDarrick J. Wong 		return error;
37775efa57dSDarrick J. Wong 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)
37875efa57dSDarrick J. Wong 		return 0;
37975efa57dSDarrick J. Wong 
38075efa57dSDarrick J. Wong 	/* Compare the in-core counters with whatever we counted. */
38175efa57dSDarrick J. Wong 	if (!xchk_fscount_within_range(sc, icount, &mp->m_icount, fsc->icount))
38275efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
38375efa57dSDarrick J. Wong 
38475efa57dSDarrick J. Wong 	if (!xchk_fscount_within_range(sc, ifree, &mp->m_ifree, fsc->ifree))
38575efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
38675efa57dSDarrick J. Wong 
38775efa57dSDarrick J. Wong 	if (!xchk_fscount_within_range(sc, fdblocks, &mp->m_fdblocks,
38875efa57dSDarrick J. Wong 			fsc->fdblocks))
38975efa57dSDarrick J. Wong 		xchk_set_corrupt(sc);
39075efa57dSDarrick J. Wong 
39175efa57dSDarrick J. Wong 	return 0;
39275efa57dSDarrick J. Wong }
393