GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / xfs / scrub / fscounters.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
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"
16 #include "xfs_ag.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
19 #include "scrub/trace.h"
20
21 /*
22  * FS Summary Counters
23  * ===================
24  *
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.
29  *
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.
37  *
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.
44  */
45
46 /*
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.
50  */
51 #define XCHK_FSCOUNT_MIN_VARIANCE       (512)
52
53 /*
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.
59  *
60  * This function runs during the setup phase /before/ we start checking any
61  * metadata.
62  */
63 STATIC int
64 xchk_fscount_warmup(
65         struct xfs_scrub        *sc)
66 {
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;
71         xfs_agnumber_t          agno;
72         int                     error = 0;
73
74         for_each_perag(mp, agno, pag) {
75                 if (xchk_should_terminate(sc, &error))
76                         break;
77                 if (pag->pagi_init && pag->pagf_init)
78                         continue;
79
80                 /* Lock both AG headers. */
81                 error = xfs_ialloc_read_agi(mp, sc->tp, agno, &agi_bp);
82                 if (error)
83                         break;
84                 error = xfs_alloc_read_agf(mp, sc->tp, agno, 0, &agf_bp);
85                 if (error)
86                         break;
87
88                 /*
89                  * These are supposed to be initialized by the header read
90                  * function.
91                  */
92                 if (!pag->pagi_init || !pag->pagf_init) {
93                         error = -EFSCORRUPTED;
94                         break;
95                 }
96
97                 xfs_buf_relse(agf_bp);
98                 agf_bp = NULL;
99                 xfs_buf_relse(agi_bp);
100                 agi_bp = NULL;
101         }
102
103         if (agf_bp)
104                 xfs_buf_relse(agf_bp);
105         if (agi_bp)
106                 xfs_buf_relse(agi_bp);
107         if (pag)
108                 xfs_perag_put(pag);
109         return error;
110 }
111
112 int
113 xchk_setup_fscounters(
114         struct xfs_scrub        *sc)
115 {
116         struct xchk_fscounters  *fsc;
117         int                     error;
118
119         sc->buf = kmem_zalloc(sizeof(struct xchk_fscounters), 0);
120         if (!sc->buf)
121                 return -ENOMEM;
122         fsc = sc->buf;
123
124         xfs_icount_range(sc->mp, &fsc->icount_min, &fsc->icount_max);
125
126         /* We must get the incore counters set up before we can proceed. */
127         error = xchk_fscount_warmup(sc);
128         if (error)
129                 return error;
130
131         return xchk_trans_alloc(sc, 0);
132 }
133
134 /* Count free space btree blocks manually for pre-lazysbcount filesystems. */
135 static int
136 xchk_fscount_btreeblks(
137         struct xfs_scrub        *sc,
138         struct xchk_fscounters  *fsc,
139         xfs_agnumber_t          agno)
140 {
141         xfs_extlen_t            blocks;
142         int                     error;
143
144         error = xchk_ag_init_existing(sc, agno, &sc->sa);
145         if (error)
146                 goto out_free;
147
148         error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
149         if (error)
150                 goto out_free;
151         fsc->fdblocks += blocks - 1;
152
153         error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
154         if (error)
155                 goto out_free;
156         fsc->fdblocks += blocks - 1;
157
158 out_free:
159         xchk_ag_free(sc, &sc->sa);
160         return error;
161 }
162
163 /*
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
167  * adjusted.
168  */
169 STATIC int
170 xchk_fscount_aggregate_agcounts(
171         struct xfs_scrub        *sc,
172         struct xchk_fscounters  *fsc)
173 {
174         struct xfs_mount        *mp = sc->mp;
175         struct xfs_perag        *pag;
176         uint64_t                delayed;
177         xfs_agnumber_t          agno;
178         int                     tries = 8;
179         int                     error = 0;
180
181 retry:
182         fsc->icount = 0;
183         fsc->ifree = 0;
184         fsc->fdblocks = 0;
185
186         for_each_perag(mp, agno, pag) {
187                 if (xchk_should_terminate(sc, &error))
188                         break;
189
190                 /* This somehow got unset since the warmup? */
191                 if (!pag->pagi_init || !pag->pagf_init) {
192                         error = -EFSCORRUPTED;
193                         break;
194                 }
195
196                 /* Count all the inodes */
197                 fsc->icount += pag->pagi_count;
198                 fsc->ifree += pag->pagi_freecount;
199
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;
205                 } else {
206                         error = xchk_fscount_btreeblks(sc, fsc, agno);
207                         if (error)
208                                 break;
209                 }
210
211                 /*
212                  * Per-AG reservations are taken out of the incore counters,
213                  * so they must be left out of the free blocks computation.
214                  */
215                 fsc->fdblocks -= pag->pag_meta_resv.ar_reserved;
216                 fsc->fdblocks -= pag->pag_rmapbt_resv.ar_orig_reserved;
217
218         }
219         if (pag)
220                 xfs_perag_put(pag);
221         if (error)
222                 return error;
223
224         /*
225          * The global incore space reservation is taken from the incore
226          * counters, so leave that out of the computation.
227          */
228         fsc->fdblocks -= mp->m_resblks_avail;
229
230         /*
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.
234          */
235         delayed = percpu_counter_sum(&mp->m_delalloc_blks);
236         fsc->fdblocks -= delayed;
237
238         trace_xchk_fscounters_calc(mp, fsc->icount, fsc->ifree, fsc->fdblocks,
239                         delayed);
240
241
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;
247
248         /*
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.
252          */
253         if (fsc->ifree > fsc->icount) {
254                 if (tries--)
255                         goto retry;
256                 xchk_set_incomplete(sc);
257                 return 0;
258         }
259
260         return 0;
261 }
262
263 /*
264  * Is the @counter reasonably close to the @expected value?
265  *
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.
271  *
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.
275  */
276 static inline bool
277 xchk_fscount_within_range(
278         struct xfs_scrub        *sc,
279         const int64_t           old_value,
280         struct percpu_counter   *counter,
281         uint64_t                expected)
282 {
283         int64_t                 min_value, max_value;
284         int64_t                 curr_value = percpu_counter_sum(counter);
285
286         trace_xchk_fscounters_within_range(sc->mp, expected, curr_value,
287                         old_value);
288
289         /* Negative values are always wrong. */
290         if (curr_value < 0)
291                 return false;
292
293         /* Exact matches are always ok. */
294         if (curr_value == expected)
295                 return true;
296
297         min_value = min(old_value, curr_value);
298         max_value = max(old_value, curr_value);
299
300         /* Within the before-and-after range is ok. */
301         if (expected >= min_value && expected <= max_value)
302                 return true;
303
304         /*
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.
308          *
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.
313          */
314         if (max_value - min_value >= XCHK_FSCOUNT_MIN_VARIANCE) {
315                 xchk_set_incomplete(sc);
316                 return true;
317         }
318
319         return false;
320 }
321
322 /* Check the superblock counters. */
323 int
324 xchk_fscounters(
325         struct xfs_scrub        *sc)
326 {
327         struct xfs_mount        *mp = sc->mp;
328         struct xchk_fscounters  *fsc = sc->buf;
329         int64_t                 icount, ifree, fdblocks;
330         int                     error;
331
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);
336
337         /* No negative values, please! */
338         if (icount < 0 || ifree < 0 || fdblocks < 0)
339                 xchk_set_corrupt(sc);
340
341         /* See if icount is obviously wrong. */
342         if (icount < fsc->icount_min || icount > fsc->icount_max)
343                 xchk_set_corrupt(sc);
344
345         /* See if fdblocks is obviously wrong. */
346         if (fdblocks > mp->m_sb.sb_dblocks)
347                 xchk_set_corrupt(sc);
348
349         /*
350          * XXX: We can't quiesce percpu counter updates, so exit early.
351          * This can be re-enabled when we gain exclusive freeze functionality.
352          */
353         return 0;
354
355         /*
356          * If ifree exceeds icount by more than the minimum variance then
357          * something's probably wrong with the counters.
358          */
359         if (ifree > icount && ifree - icount > XCHK_FSCOUNT_MIN_VARIANCE)
360                 xchk_set_corrupt(sc);
361
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))
365                 return error;
366         if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)
367                 return 0;
368
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);
372
373         if (!xchk_fscount_within_range(sc, ifree, &mp->m_ifree, fsc->ifree))
374                 xchk_set_corrupt(sc);
375
376         if (!xchk_fscount_within_range(sc, fdblocks, &mp->m_fdblocks,
377                         fsc->fdblocks))
378                 xchk_set_corrupt(sc);
379
380         return 0;
381 }