1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_trans_resv.h"
11 #include "xfs_mount.h"
12 #include "xfs_alloc.h"
13 #include "xfs_ialloc.h"
14 #include "xfs_health.h"
15 #include "xfs_btree.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
19 #include "scrub/trace.h"
25 * The basics of filesystem summary counter checking are that we iterate the
26 * AGs counting the number of free blocks, free space btree blocks, per-AG
27 * reservations, inodes, delayed allocation reservations, and free inodes.
28 * Then we compare what we computed against the in-core counters.
30 * However, the reality is that summary counters are a tricky beast to check.
31 * While we /could/ freeze the filesystem and scramble around the AGs counting
32 * the free blocks, in practice we prefer not do that for a scan because
33 * freezing is costly. To get around this, we added a per-cpu counter of the
34 * delalloc reservations so that we can rotor around the AGs relatively
35 * quickly, and we allow the counts to be slightly off because we're not taking
36 * any locks while we do this.
38 * So the first thing we do is warm up the buffer cache in the setup routine by
39 * walking all the AGs to make sure the incore per-AG structure has been
40 * initialized. The expected value calculation then iterates the incore per-AG
41 * structures as quickly as it can. We snapshot the percpu counters before and
42 * after this operation and use the difference in counter values to guess at
43 * our tolerance for mismatch between expected and actual counter values.
47 * Since the expected value computation is lockless but only browses incore
48 * values, the percpu counters should be fairly close to each other. However,
49 * we'll allow ourselves to be off by at least this (arbitrary) amount.
51 #define XCHK_FSCOUNT_MIN_VARIANCE (512)
54 * Make sure the per-AG structure has been initialized from the on-disk header
55 * contents and trust that the incore counters match the ondisk counters. (The
56 * AGF and AGI scrubbers check them, and a normal xfs_scrub run checks the
57 * summary counters after checking all AG headers). Do this from the setup
58 * function so that the inner AG aggregation loop runs as quickly as possible.
60 * This function runs during the setup phase /before/ we start checking any
67 struct xfs_mount *mp = sc->mp;
68 struct xfs_buf *agi_bp = NULL;
69 struct xfs_buf *agf_bp = NULL;
70 struct xfs_perag *pag = NULL;
74 for_each_perag(mp, agno, pag) {
75 if (xchk_should_terminate(sc, &error))
77 if (pag->pagi_init && pag->pagf_init)
80 /* Lock both AG headers. */
81 error = xfs_ialloc_read_agi(mp, sc->tp, agno, &agi_bp);
84 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, &agf_bp);
89 * These are supposed to be initialized by the header read
92 if (!pag->pagi_init || !pag->pagf_init) {
93 error = -EFSCORRUPTED;
97 xfs_buf_relse(agf_bp);
99 xfs_buf_relse(agi_bp);
104 xfs_buf_relse(agf_bp);
106 xfs_buf_relse(agi_bp);
113 xchk_setup_fscounters(
114 struct xfs_scrub *sc)
116 struct xchk_fscounters *fsc;
119 sc->buf = kmem_zalloc(sizeof(struct xchk_fscounters), 0);
124 xfs_icount_range(sc->mp, &fsc->icount_min, &fsc->icount_max);
126 /* We must get the incore counters set up before we can proceed. */
127 error = xchk_fscount_warmup(sc);
131 return xchk_trans_alloc(sc, 0);
134 /* Count free space btree blocks manually for pre-lazysbcount filesystems. */
136 xchk_fscount_btreeblks(
137 struct xfs_scrub *sc,
138 struct xchk_fscounters *fsc,
144 error = xchk_ag_init_existing(sc, agno, &sc->sa);
148 error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
151 fsc->fdblocks += blocks - 1;
153 error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
156 fsc->fdblocks += blocks - 1;
159 xchk_ag_free(sc, &sc->sa);
164 * Calculate what the global in-core counters ought to be from the incore
165 * per-AG structure. Callers can compare this to the actual in-core counters
166 * to estimate by how much both in-core and on-disk counters need to be
170 xchk_fscount_aggregate_agcounts(
171 struct xfs_scrub *sc,
172 struct xchk_fscounters *fsc)
174 struct xfs_mount *mp = sc->mp;
175 struct xfs_perag *pag;
186 for_each_perag(mp, agno, pag) {
187 if (xchk_should_terminate(sc, &error))
190 /* This somehow got unset since the warmup? */
191 if (!pag->pagi_init || !pag->pagf_init) {
192 error = -EFSCORRUPTED;
196 /* Count all the inodes */
197 fsc->icount += pag->pagi_count;
198 fsc->ifree += pag->pagi_freecount;
200 /* Add up the free/freelist/bnobt/cntbt blocks */
201 fsc->fdblocks += pag->pagf_freeblks;
202 fsc->fdblocks += pag->pagf_flcount;
203 if (xfs_has_lazysbcount(sc->mp)) {
204 fsc->fdblocks += pag->pagf_btreeblks;
206 error = xchk_fscount_btreeblks(sc, fsc, agno);
212 * Per-AG reservations are taken out of the incore counters,
213 * so they must be left out of the free blocks computation.
215 fsc->fdblocks -= pag->pag_meta_resv.ar_reserved;
216 fsc->fdblocks -= pag->pag_rmapbt_resv.ar_orig_reserved;
225 * The global incore space reservation is taken from the incore
226 * counters, so leave that out of the computation.
228 fsc->fdblocks -= mp->m_resblks_avail;
231 * Delayed allocation reservations are taken out of the incore counters
232 * but not recorded on disk, so leave them and their indlen blocks out
233 * of the computation.
235 delayed = percpu_counter_sum(&mp->m_delalloc_blks);
236 fsc->fdblocks -= delayed;
238 trace_xchk_fscounters_calc(mp, fsc->icount, fsc->ifree, fsc->fdblocks,
242 /* Bail out if the values we compute are totally nonsense. */
243 if (fsc->icount < fsc->icount_min || fsc->icount > fsc->icount_max ||
244 fsc->fdblocks > mp->m_sb.sb_dblocks ||
245 fsc->ifree > fsc->icount_max)
246 return -EFSCORRUPTED;
249 * If ifree > icount then we probably had some perturbation in the
250 * counters while we were calculating things. We'll try a few times
251 * to maintain ifree <= icount before giving up.
253 if (fsc->ifree > fsc->icount) {
256 xchk_set_incomplete(sc);
264 * Is the @counter reasonably close to the @expected value?
266 * We neither locked nor froze anything in the filesystem while aggregating the
267 * per-AG data to compute the @expected value, which means that the counter
268 * could have changed. We know the @old_value of the summation of the counter
269 * before the aggregation, and we re-sum the counter now. If the expected
270 * value falls between the two summations, we're ok.
272 * Otherwise, we /might/ have a problem. If the change in the summations is
273 * more than we want to tolerate, the filesystem is probably busy and we should
274 * just send back INCOMPLETE and see if userspace will try again.
277 xchk_fscount_within_range(
278 struct xfs_scrub *sc,
279 const int64_t old_value,
280 struct percpu_counter *counter,
283 int64_t min_value, max_value;
284 int64_t curr_value = percpu_counter_sum(counter);
286 trace_xchk_fscounters_within_range(sc->mp, expected, curr_value,
289 /* Negative values are always wrong. */
293 /* Exact matches are always ok. */
294 if (curr_value == expected)
297 min_value = min(old_value, curr_value);
298 max_value = max(old_value, curr_value);
300 /* Within the before-and-after range is ok. */
301 if (expected >= min_value && expected <= max_value)
305 * If the difference between the two summations is too large, the fs
306 * might just be busy and so we'll mark the scrub incomplete. Return
307 * true here so that we don't mark the counter corrupt.
309 * XXX: In the future when userspace can grant scrub permission to
310 * quiesce the filesystem to solve the outsized variance problem, this
311 * check should be moved up and the return code changed to signal to
312 * userspace that we need quiesce permission.
314 if (max_value - min_value >= XCHK_FSCOUNT_MIN_VARIANCE) {
315 xchk_set_incomplete(sc);
322 /* Check the superblock counters. */
325 struct xfs_scrub *sc)
327 struct xfs_mount *mp = sc->mp;
328 struct xchk_fscounters *fsc = sc->buf;
329 int64_t icount, ifree, fdblocks;
332 /* Snapshot the percpu counters. */
333 icount = percpu_counter_sum(&mp->m_icount);
334 ifree = percpu_counter_sum(&mp->m_ifree);
335 fdblocks = percpu_counter_sum(&mp->m_fdblocks);
337 /* No negative values, please! */
338 if (icount < 0 || ifree < 0 || fdblocks < 0)
339 xchk_set_corrupt(sc);
341 /* See if icount is obviously wrong. */
342 if (icount < fsc->icount_min || icount > fsc->icount_max)
343 xchk_set_corrupt(sc);
345 /* See if fdblocks is obviously wrong. */
346 if (fdblocks > mp->m_sb.sb_dblocks)
347 xchk_set_corrupt(sc);
350 * XXX: We can't quiesce percpu counter updates, so exit early.
351 * This can be re-enabled when we gain exclusive freeze functionality.
356 * If ifree exceeds icount by more than the minimum variance then
357 * something's probably wrong with the counters.
359 if (ifree > icount && ifree - icount > XCHK_FSCOUNT_MIN_VARIANCE)
360 xchk_set_corrupt(sc);
362 /* Walk the incore AG headers to calculate the expected counters. */
363 error = xchk_fscount_aggregate_agcounts(sc, fsc);
364 if (!xchk_process_error(sc, 0, XFS_SB_BLOCK(mp), &error))
366 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)
369 /* Compare the in-core counters with whatever we counted. */
370 if (!xchk_fscount_within_range(sc, icount, &mp->m_icount, fsc->icount))
371 xchk_set_corrupt(sc);
373 if (!xchk_fscount_within_range(sc, ifree, &mp->m_ifree, fsc->ifree))
374 xchk_set_corrupt(sc);
376 if (!xchk_fscount_within_range(sc, fdblocks, &mp->m_fdblocks,
378 xchk_set_corrupt(sc);