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"
13 #include "xfs_alloc.h"
14 #include "xfs_ialloc.h"
15 #include "xfs_health.h"
16 #include "scrub/scrub.h"
17 #include "scrub/common.h"
18 #include "scrub/trace.h"
24 * The basics of filesystem summary counter checking are that we iterate the
25 * AGs counting the number of free blocks, free space btree blocks, per-AG
26 * reservations, inodes, delayed allocation reservations, and free inodes.
27 * Then we compare what we computed against the in-core counters.
29 * However, the reality is that summary counters are a tricky beast to check.
30 * While we /could/ freeze the filesystem and scramble around the AGs counting
31 * the free blocks, in practice we prefer not do that for a scan because
32 * freezing is costly. To get around this, we added a per-cpu counter of the
33 * delalloc reservations so that we can rotor around the AGs relatively
34 * quickly, and we allow the counts to be slightly off because we're not taking
35 * any locks while we do this.
37 * So the first thing we do is warm up the buffer cache in the setup routine by
38 * walking all the AGs to make sure the incore per-AG structure has been
39 * initialized. The expected value calculation then iterates the incore per-AG
40 * structures as quickly as it can. We snapshot the percpu counters before and
41 * after this operation and use the difference in counter values to guess at
42 * our tolerance for mismatch between expected and actual counter values.
46 * Since the expected value computation is lockless but only browses incore
47 * values, the percpu counters should be fairly close to each other. However,
48 * we'll allow ourselves to be off by at least this (arbitrary) amount.
50 #define XCHK_FSCOUNT_MIN_VARIANCE (512)
53 * Make sure the per-AG structure has been initialized from the on-disk header
54 * contents and trust that the incore counters match the ondisk counters. (The
55 * AGF and AGI scrubbers check them, and a normal xfs_scrub run checks the
56 * summary counters after checking all AG headers). Do this from the setup
57 * function so that the inner AG aggregation loop runs as quickly as possible.
59 * This function runs during the setup phase /before/ we start checking any
66 struct xfs_mount
*mp
= sc
->mp
;
67 struct xfs_buf
*agi_bp
= NULL
;
68 struct xfs_buf
*agf_bp
= NULL
;
69 struct xfs_perag
*pag
= NULL
;
73 for (agno
= 0; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
74 pag
= xfs_perag_get(mp
, agno
);
76 if (pag
->pagi_init
&& pag
->pagf_init
)
79 /* Lock both AG headers. */
80 error
= xfs_ialloc_read_agi(mp
, sc
->tp
, agno
, &agi_bp
);
83 error
= xfs_alloc_read_agf(mp
, sc
->tp
, agno
, 0, &agf_bp
);
88 * These are supposed to be initialized by the header read
91 error
= -EFSCORRUPTED
;
92 if (!pag
->pagi_init
|| !pag
->pagf_init
)
95 xfs_buf_relse(agf_bp
);
97 xfs_buf_relse(agi_bp
);
104 if (xchk_should_terminate(sc
, &error
))
109 xfs_buf_relse(agf_bp
);
111 xfs_buf_relse(agi_bp
);
118 xchk_setup_fscounters(
119 struct xfs_scrub
*sc
,
120 struct xfs_inode
*ip
)
122 struct xchk_fscounters
*fsc
;
125 sc
->buf
= kmem_zalloc(sizeof(struct xchk_fscounters
), 0);
130 xfs_icount_range(sc
->mp
, &fsc
->icount_min
, &fsc
->icount_max
);
132 /* We must get the incore counters set up before we can proceed. */
133 error
= xchk_fscount_warmup(sc
);
138 * Pause background reclaim while we're scrubbing to reduce the
139 * likelihood of background perturbations to the counters throwing off
142 xchk_stop_reaping(sc
);
144 return xchk_trans_alloc(sc
, 0);
148 * Calculate what the global in-core counters ought to be from the incore
149 * per-AG structure. Callers can compare this to the actual in-core counters
150 * to estimate by how much both in-core and on-disk counters need to be
154 xchk_fscount_aggregate_agcounts(
155 struct xfs_scrub
*sc
,
156 struct xchk_fscounters
*fsc
)
158 struct xfs_mount
*mp
= sc
->mp
;
159 struct xfs_perag
*pag
;
170 for (agno
= 0; agno
< mp
->m_sb
.sb_agcount
; agno
++) {
171 pag
= xfs_perag_get(mp
, agno
);
173 /* This somehow got unset since the warmup? */
174 if (!pag
->pagi_init
|| !pag
->pagf_init
) {
176 return -EFSCORRUPTED
;
179 /* Count all the inodes */
180 fsc
->icount
+= pag
->pagi_count
;
181 fsc
->ifree
+= pag
->pagi_freecount
;
183 /* Add up the free/freelist/bnobt/cntbt blocks */
184 fsc
->fdblocks
+= pag
->pagf_freeblks
;
185 fsc
->fdblocks
+= pag
->pagf_flcount
;
186 fsc
->fdblocks
+= pag
->pagf_btreeblks
;
189 * Per-AG reservations are taken out of the incore counters,
190 * so they must be left out of the free blocks computation.
192 fsc
->fdblocks
-= pag
->pag_meta_resv
.ar_reserved
;
193 fsc
->fdblocks
-= pag
->pag_rmapbt_resv
.ar_orig_reserved
;
197 if (xchk_should_terminate(sc
, &error
))
205 * The global incore space reservation is taken from the incore
206 * counters, so leave that out of the computation.
208 fsc
->fdblocks
-= mp
->m_resblks_avail
;
211 * Delayed allocation reservations are taken out of the incore counters
212 * but not recorded on disk, so leave them and their indlen blocks out
213 * of the computation.
215 delayed
= percpu_counter_sum(&mp
->m_delalloc_blks
);
216 fsc
->fdblocks
-= delayed
;
218 trace_xchk_fscounters_calc(mp
, fsc
->icount
, fsc
->ifree
, fsc
->fdblocks
,
222 /* Bail out if the values we compute are totally nonsense. */
223 if (fsc
->icount
< fsc
->icount_min
|| fsc
->icount
> fsc
->icount_max
||
224 fsc
->fdblocks
> mp
->m_sb
.sb_dblocks
||
225 fsc
->ifree
> fsc
->icount_max
)
226 return -EFSCORRUPTED
;
229 * If ifree > icount then we probably had some perturbation in the
230 * counters while we were calculating things. We'll try a few times
231 * to maintain ifree <= icount before giving up.
233 if (fsc
->ifree
> fsc
->icount
) {
236 xchk_set_incomplete(sc
);
244 * Is the @counter reasonably close to the @expected value?
246 * We neither locked nor froze anything in the filesystem while aggregating the
247 * per-AG data to compute the @expected value, which means that the counter
248 * could have changed. We know the @old_value of the summation of the counter
249 * before the aggregation, and we re-sum the counter now. If the expected
250 * value falls between the two summations, we're ok.
252 * Otherwise, we /might/ have a problem. If the change in the summations is
253 * more than we want to tolerate, the filesystem is probably busy and we should
254 * just send back INCOMPLETE and see if userspace will try again.
257 xchk_fscount_within_range(
258 struct xfs_scrub
*sc
,
259 const int64_t old_value
,
260 struct percpu_counter
*counter
,
263 int64_t min_value
, max_value
;
264 int64_t curr_value
= percpu_counter_sum(counter
);
266 trace_xchk_fscounters_within_range(sc
->mp
, expected
, curr_value
,
269 /* Negative values are always wrong. */
273 /* Exact matches are always ok. */
274 if (curr_value
== expected
)
277 min_value
= min(old_value
, curr_value
);
278 max_value
= max(old_value
, curr_value
);
280 /* Within the before-and-after range is ok. */
281 if (expected
>= min_value
&& expected
<= max_value
)
285 * If the difference between the two summations is too large, the fs
286 * might just be busy and so we'll mark the scrub incomplete. Return
287 * true here so that we don't mark the counter corrupt.
289 * XXX: In the future when userspace can grant scrub permission to
290 * quiesce the filesystem to solve the outsized variance problem, this
291 * check should be moved up and the return code changed to signal to
292 * userspace that we need quiesce permission.
294 if (max_value
- min_value
>= XCHK_FSCOUNT_MIN_VARIANCE
) {
295 xchk_set_incomplete(sc
);
302 /* Check the superblock counters. */
305 struct xfs_scrub
*sc
)
307 struct xfs_mount
*mp
= sc
->mp
;
308 struct xchk_fscounters
*fsc
= sc
->buf
;
309 int64_t icount
, ifree
, fdblocks
;
312 /* Snapshot the percpu counters. */
313 icount
= percpu_counter_sum(&mp
->m_icount
);
314 ifree
= percpu_counter_sum(&mp
->m_ifree
);
315 fdblocks
= percpu_counter_sum(&mp
->m_fdblocks
);
317 /* No negative values, please! */
318 if (icount
< 0 || ifree
< 0 || fdblocks
< 0)
319 xchk_set_corrupt(sc
);
321 /* See if icount is obviously wrong. */
322 if (icount
< fsc
->icount_min
|| icount
> fsc
->icount_max
)
323 xchk_set_corrupt(sc
);
325 /* See if fdblocks is obviously wrong. */
326 if (fdblocks
> mp
->m_sb
.sb_dblocks
)
327 xchk_set_corrupt(sc
);
330 * If ifree exceeds icount by more than the minimum variance then
331 * something's probably wrong with the counters.
333 if (ifree
> icount
&& ifree
- icount
> XCHK_FSCOUNT_MIN_VARIANCE
)
334 xchk_set_corrupt(sc
);
336 /* Walk the incore AG headers to calculate the expected counters. */
337 error
= xchk_fscount_aggregate_agcounts(sc
, fsc
);
338 if (!xchk_process_error(sc
, 0, XFS_SB_BLOCK(mp
), &error
))
340 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_INCOMPLETE
)
343 /* Compare the in-core counters with whatever we counted. */
344 if (!xchk_fscount_within_range(sc
, icount
, &mp
->m_icount
, fsc
->icount
))
345 xchk_set_corrupt(sc
);
347 if (!xchk_fscount_within_range(sc
, ifree
, &mp
->m_ifree
, fsc
->ifree
))
348 xchk_set_corrupt(sc
);
350 if (!xchk_fscount_within_range(sc
, fdblocks
, &mp
->m_fdblocks
,
352 xchk_set_corrupt(sc
);