Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0-or-later
2 : /*
3 : * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 : * Author: Darrick J. Wong <djwong@kernel.org>
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_btree.h"
13 : #include "xfs_sb.h"
14 : #include "xfs_alloc.h"
15 : #include "xfs_ialloc.h"
16 : #include "xfs_rmap.h"
17 : #include "xfs_ag.h"
18 : #include "xfs_inode.h"
19 : #include "scrub/scrub.h"
20 : #include "scrub/common.h"
21 :
22 : int
23 3711005 : xchk_setup_agheader(
24 : struct xfs_scrub *sc)
25 : {
26 3711005 : if (xchk_need_intent_drain(sc))
27 84966 : xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
28 3711005 : return xchk_setup_fs(sc);
29 : }
30 :
31 : /* Superblock */
32 :
33 : /* Cross-reference with the other btrees. */
34 : STATIC void
35 883460 : xchk_superblock_xref(
36 : struct xfs_scrub *sc,
37 : struct xfs_buf *bp)
38 : {
39 883460 : struct xfs_mount *mp = sc->mp;
40 883460 : xfs_agnumber_t agno = sc->sm->sm_agno;
41 883460 : xfs_agblock_t agbno;
42 883460 : int error;
43 :
44 883460 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
45 7663 : return;
46 :
47 883497 : agbno = XFS_SB_BLOCK(mp);
48 :
49 883497 : error = xchk_ag_init_existing(sc, agno, &sc->sa);
50 884016 : if (!xchk_xref_process_error(sc, agno, agbno, &error))
51 : return;
52 :
53 875839 : xchk_xref_is_used_space(sc, agbno, 1);
54 875857 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
55 876364 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
56 875856 : xchk_xref_is_not_shared(sc, agbno, 1);
57 876103 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
58 :
59 : /* scrub teardown will take care of sc->sa for us */
60 : }
61 :
62 : /*
63 : * Scrub the filesystem superblock.
64 : *
65 : * Note: We do /not/ attempt to check AG 0's superblock. Mount is
66 : * responsible for validating all the geometry information in sb 0, so
67 : * if the filesystem is capable of initiating online scrub, then clearly
68 : * sb 0 is ok and we can use its information to check everything else.
69 : */
70 : int
71 985329 : xchk_superblock(
72 : struct xfs_scrub *sc)
73 : {
74 985329 : struct xfs_mount *mp = sc->mp;
75 985329 : struct xfs_buf *bp;
76 985329 : struct xfs_dsb *sb;
77 985329 : struct xfs_perag *pag;
78 985329 : xfs_agnumber_t agno;
79 985329 : uint32_t v2_ok;
80 985329 : __be32 features_mask;
81 985329 : int error;
82 985329 : __be16 vernum_mask;
83 :
84 985329 : agno = sc->sm->sm_agno;
85 985329 : if (agno == 0)
86 : return 0;
87 :
88 : /*
89 : * Grab an active reference to the perag structure. If we can't get
90 : * it, we're racing with something that's tearing down the AG, so
91 : * signal that the AG no longer exists.
92 : */
93 883662 : pag = xfs_perag_get(mp, agno);
94 884250 : if (!pag)
95 : return -ENOENT;
96 :
97 884250 : error = xfs_sb_read_secondary(mp, sc->tp, agno, &bp);
98 : /*
99 : * The superblock verifier can return several different error codes
100 : * if it thinks the superblock doesn't look right. For a mount these
101 : * would all get bounced back to userspace, but if we're here then the
102 : * fs mounted successfully, which means that this secondary superblock
103 : * is simply incorrect. Treat all these codes the same way we treat
104 : * any corruption.
105 : */
106 884047 : switch (error) {
107 11 : case -EINVAL: /* also -EWRONGFS */
108 : case -ENOSYS:
109 : case -EFBIG:
110 11 : error = -EFSCORRUPTED;
111 884047 : fallthrough;
112 : default:
113 884047 : break;
114 : }
115 884047 : if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
116 99 : goto out_pag;
117 :
118 883694 : sb = bp->b_addr;
119 :
120 : /*
121 : * Verify the geometries match. Fields that are permanently
122 : * set by mkfs are checked; fields that can be updated later
123 : * (and are not propagated to backup superblocks) are preen
124 : * checked.
125 : */
126 883694 : if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
127 0 : xchk_block_set_corrupt(sc, bp);
128 :
129 883694 : if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
130 0 : xchk_block_set_corrupt(sc, bp);
131 :
132 883694 : if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
133 0 : xchk_block_set_corrupt(sc, bp);
134 :
135 883694 : if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
136 0 : xchk_block_set_corrupt(sc, bp);
137 :
138 883694 : if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
139 0 : xchk_block_set_preen(sc, bp);
140 :
141 883675 : if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
142 0 : xchk_block_set_corrupt(sc, bp);
143 :
144 883675 : if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
145 109060 : xchk_block_set_preen(sc, bp);
146 :
147 883679 : if (xfs_has_metadir(sc->mp)) {
148 864080 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_metadirino))
149 238282 : xchk_block_set_preen(sc, bp);
150 : } else {
151 19599 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
152 7949 : xchk_block_set_preen(sc, bp);
153 :
154 19601 : if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
155 7951 : xchk_block_set_preen(sc, bp);
156 : }
157 :
158 883678 : if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
159 0 : xchk_block_set_corrupt(sc, bp);
160 :
161 883678 : if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
162 0 : xchk_block_set_corrupt(sc, bp);
163 :
164 883678 : if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
165 0 : xchk_block_set_corrupt(sc, bp);
166 :
167 883678 : if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
168 0 : xchk_block_set_corrupt(sc, bp);
169 :
170 883678 : if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
171 0 : xchk_block_set_corrupt(sc, bp);
172 :
173 : /* Check sb_versionnum bits that are set at mkfs time. */
174 883678 : vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS |
175 : XFS_SB_VERSION_NUMBITS |
176 : XFS_SB_VERSION_ALIGNBIT |
177 : XFS_SB_VERSION_DALIGNBIT |
178 : XFS_SB_VERSION_SHAREDBIT |
179 : XFS_SB_VERSION_LOGV2BIT |
180 : XFS_SB_VERSION_SECTORBIT |
181 : XFS_SB_VERSION_EXTFLGBIT |
182 : XFS_SB_VERSION_DIRV2BIT);
183 883678 : if ((sb->sb_versionnum & vernum_mask) !=
184 883678 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
185 0 : xchk_block_set_corrupt(sc, bp);
186 :
187 : /* Check sb_versionnum bits that can be set after mkfs time. */
188 883678 : vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
189 : XFS_SB_VERSION_NLINKBIT |
190 : XFS_SB_VERSION_QUOTABIT);
191 883678 : if ((sb->sb_versionnum & vernum_mask) !=
192 883678 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
193 238404 : xchk_block_set_preen(sc, bp);
194 :
195 883654 : if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
196 0 : xchk_block_set_corrupt(sc, bp);
197 :
198 883654 : if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
199 0 : xchk_block_set_corrupt(sc, bp);
200 :
201 883654 : if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
202 0 : xchk_block_set_corrupt(sc, bp);
203 :
204 1766764 : if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
205 99 : xchk_block_set_preen(sc, bp);
206 :
207 883382 : if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
208 0 : xchk_block_set_corrupt(sc, bp);
209 :
210 883382 : if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
211 0 : xchk_block_set_corrupt(sc, bp);
212 :
213 883382 : if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
214 0 : xchk_block_set_corrupt(sc, bp);
215 :
216 883382 : if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
217 0 : xchk_block_set_corrupt(sc, bp);
218 :
219 883382 : if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
220 0 : xchk_block_set_corrupt(sc, bp);
221 :
222 883382 : if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
223 0 : xchk_block_set_corrupt(sc, bp);
224 :
225 883382 : if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
226 0 : xchk_block_set_preen(sc, bp);
227 :
228 : /*
229 : * Skip the summary counters since we track them in memory anyway.
230 : * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
231 : */
232 :
233 883382 : if (!xfs_has_metadir(sc->mp)) {
234 19578 : if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
235 7938 : xchk_block_set_preen(sc, bp);
236 :
237 19577 : if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
238 7937 : xchk_block_set_preen(sc, bp);
239 : }
240 :
241 : /*
242 : * Skip the quota flags since repair will force quotacheck.
243 : * sb_qflags
244 : */
245 :
246 883384 : if (sb->sb_flags != mp->m_sb.sb_flags)
247 0 : xchk_block_set_corrupt(sc, bp);
248 :
249 883384 : if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
250 0 : xchk_block_set_corrupt(sc, bp);
251 :
252 883384 : if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
253 0 : xchk_block_set_corrupt(sc, bp);
254 :
255 883384 : if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
256 0 : xchk_block_set_preen(sc, bp);
257 :
258 883384 : if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
259 328 : xchk_block_set_preen(sc, bp);
260 :
261 883383 : if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
262 0 : xchk_block_set_corrupt(sc, bp);
263 :
264 883383 : if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
265 0 : xchk_block_set_corrupt(sc, bp);
266 :
267 883383 : if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
268 0 : xchk_block_set_corrupt(sc, bp);
269 :
270 883383 : if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
271 0 : xchk_block_set_corrupt(sc, bp);
272 :
273 : /* Do we see any invalid bits in sb_features2? */
274 883394 : if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
275 0 : if (sb->sb_features2 != 0)
276 0 : xchk_block_set_corrupt(sc, bp);
277 : } else {
278 883383 : v2_ok = XFS_SB_VERSION2_OKBITS;
279 883383 : if (xfs_sb_is_v5(&mp->m_sb))
280 883343 : v2_ok |= XFS_SB_VERSION2_CRCBIT;
281 :
282 883383 : if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
283 0 : xchk_block_set_corrupt(sc, bp);
284 :
285 883383 : if (sb->sb_features2 != sb->sb_bad_features2)
286 0 : xchk_block_set_preen(sc, bp);
287 : }
288 :
289 : /* Check sb_features2 flags that are set at mkfs time. */
290 883383 : features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
291 : XFS_SB_VERSION2_PROJID32BIT |
292 : XFS_SB_VERSION2_CRCBIT |
293 : XFS_SB_VERSION2_FTYPE);
294 883383 : if ((sb->sb_features2 & features_mask) !=
295 883383 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
296 0 : xchk_block_set_corrupt(sc, bp);
297 :
298 : /* Check sb_features2 flags that can be set after mkfs time. */
299 883383 : features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
300 883383 : if ((sb->sb_features2 & features_mask) !=
301 883383 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
302 0 : xchk_block_set_preen(sc, bp);
303 :
304 883179 : if (!xfs_has_crc(mp)) {
305 : /* all v5 fields must be zero */
306 22 : if (memchr_inv(&sb->sb_features_compat, 0,
307 : sizeof(struct xfs_dsb) -
308 : offsetof(struct xfs_dsb, sb_features_compat)))
309 0 : xchk_block_set_corrupt(sc, bp);
310 : } else {
311 : /* compat features must match */
312 883168 : if (sb->sb_features_compat !=
313 883168 : cpu_to_be32(mp->m_sb.sb_features_compat))
314 0 : xchk_block_set_corrupt(sc, bp);
315 :
316 : /* ro compat features must match */
317 883168 : if (sb->sb_features_ro_compat !=
318 883168 : cpu_to_be32(mp->m_sb.sb_features_ro_compat))
319 0 : xchk_block_set_corrupt(sc, bp);
320 :
321 : /*
322 : * NEEDSREPAIR is ignored on a secondary super, so we should
323 : * clear it when we find it, though it's not a corruption.
324 : */
325 883168 : features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
326 883168 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
327 883168 : sb->sb_features_incompat) & features_mask)
328 0 : xchk_block_set_preen(sc, bp);
329 :
330 : /* all other incompat features must match */
331 883168 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
332 883168 : sb->sb_features_incompat) & ~features_mask)
333 0 : xchk_block_set_corrupt(sc, bp);
334 :
335 : /*
336 : * log incompat features protect newer log record types from
337 : * older log recovery code. Log recovery doesn't check the
338 : * secondary supers, so we can clear these if needed.
339 : */
340 883168 : if (sb->sb_features_log_incompat)
341 25250 : xchk_block_set_preen(sc, bp);
342 :
343 : /* Don't care about sb_crc */
344 :
345 883171 : if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
346 0 : xchk_block_set_corrupt(sc, bp);
347 :
348 883171 : if (!xfs_has_metadir(sc->mp)) {
349 19554 : if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
350 7922 : xchk_block_set_preen(sc, bp);
351 : }
352 :
353 : /* Don't care about sb_lsn */
354 : }
355 :
356 883179 : if (xfs_has_metauuid(mp)) {
357 : /* The metadata UUID must be the same for all supers */
358 24 : if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
359 0 : xchk_block_set_corrupt(sc, bp);
360 : }
361 :
362 : /* Everything else must be zero. */
363 883061 : if (memchr_inv(sb + 1, 0,
364 883179 : BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
365 0 : xchk_block_set_corrupt(sc, bp);
366 :
367 883061 : xchk_superblock_xref(sc, bp);
368 883831 : out_pag:
369 883831 : xfs_perag_put(pag);
370 884131 : return error;
371 : }
372 :
373 : /* AGF */
374 :
375 : /* Tally freespace record lengths. */
376 : STATIC int
377 1718505563 : xchk_agf_record_bno_lengths(
378 : struct xfs_btree_cur *cur,
379 : const struct xfs_alloc_rec_incore *rec,
380 : void *priv)
381 : {
382 1718505563 : xfs_extlen_t *blocks = priv;
383 :
384 1718505563 : (*blocks) += rec->ar_blockcount;
385 1718505563 : return 0;
386 : }
387 :
388 : /* Check agf_freeblks */
389 : static inline void
390 844242 : xchk_agf_xref_freeblks(
391 : struct xfs_scrub *sc)
392 : {
393 844242 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
394 844242 : xfs_extlen_t blocks = 0;
395 844242 : int error;
396 :
397 844242 : if (!sc->sa.bno_cur)
398 0 : return;
399 :
400 844242 : error = xfs_alloc_query_all(sc->sa.bno_cur,
401 : xchk_agf_record_bno_lengths, &blocks);
402 844408 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
403 : return;
404 844149 : if (blocks != be32_to_cpu(agf->agf_freeblks))
405 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
406 : }
407 :
408 : /* Cross reference the AGF with the cntbt (freespace by length btree) */
409 : static inline void
410 843951 : xchk_agf_xref_cntbt(
411 : struct xfs_scrub *sc)
412 : {
413 843951 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
414 843951 : xfs_agblock_t agbno;
415 843951 : xfs_extlen_t blocks;
416 843951 : int have;
417 843951 : int error;
418 :
419 843951 : if (!sc->sa.cnt_cur)
420 12 : return;
421 :
422 : /* Any freespace at all? */
423 843951 : error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
424 844742 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
425 : return;
426 844501 : if (!have) {
427 12 : if (agf->agf_freeblks != cpu_to_be32(0))
428 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
429 12 : return;
430 : }
431 :
432 : /* Check agf_longest */
433 844489 : error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
434 844660 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
435 : return;
436 844533 : if (!have || blocks != be32_to_cpu(agf->agf_longest))
437 41 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
438 : }
439 :
440 : /* Check the btree block counts in the AGF against the btrees. */
441 : STATIC void
442 844323 : xchk_agf_xref_btreeblks(
443 : struct xfs_scrub *sc)
444 : {
445 844323 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
446 844323 : struct xfs_mount *mp = sc->mp;
447 844323 : xfs_agblock_t blocks;
448 844323 : xfs_agblock_t btreeblks;
449 844323 : int error;
450 :
451 : /* agf_btreeblks didn't exist before lazysbcount */
452 844323 : if (!xfs_has_lazysbcount(sc->mp))
453 5 : return;
454 :
455 : /* Check agf_rmap_blocks; set up for agf_btreeblks check */
456 844323 : if (sc->sa.rmap_cur) {
457 832580 : error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
458 832328 : if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
459 : return;
460 832088 : btreeblks = blocks - 1;
461 832088 : if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
462 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
463 : } else {
464 : btreeblks = 0;
465 : }
466 :
467 : /*
468 : * No rmap cursor; we can't xref if we have the rmapbt feature.
469 : * We also can't do it if we're missing the free space btree cursors.
470 : */
471 843831 : if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
472 843831 : !sc->sa.bno_cur || !sc->sa.cnt_cur)
473 : return;
474 :
475 : /* Check agf_btreeblks */
476 843831 : error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
477 844766 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
478 : return;
479 844718 : btreeblks += blocks - 1;
480 :
481 844718 : error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
482 845010 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
483 : return;
484 844962 : btreeblks += blocks - 1;
485 :
486 844962 : if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
487 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
488 : }
489 :
490 : /* Check agf_refcount_blocks against tree size */
491 : static inline void
492 844601 : xchk_agf_xref_refcblks(
493 : struct xfs_scrub *sc)
494 : {
495 844601 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
496 844601 : xfs_agblock_t blocks;
497 844601 : int error;
498 :
499 844601 : if (!sc->sa.refc_cur)
500 11611 : return;
501 :
502 832990 : error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
503 833215 : if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
504 : return;
505 833210 : if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
506 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
507 : }
508 :
509 : /* Cross-reference with the other btrees. */
510 : STATIC void
511 843693 : xchk_agf_xref(
512 : struct xfs_scrub *sc)
513 : {
514 843693 : struct xfs_mount *mp = sc->mp;
515 843693 : xfs_agblock_t agbno;
516 :
517 843693 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
518 : return;
519 :
520 843692 : agbno = XFS_AGF_BLOCK(mp);
521 :
522 843692 : xchk_ag_btcur_init(sc, &sc->sa);
523 :
524 844807 : xchk_xref_is_used_space(sc, agbno, 1);
525 844384 : xchk_agf_xref_freeblks(sc);
526 843933 : xchk_agf_xref_cntbt(sc);
527 844356 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
528 844889 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
529 844333 : xchk_agf_xref_btreeblks(sc);
530 844834 : xchk_xref_is_not_shared(sc, agbno, 1);
531 844478 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
532 844649 : xchk_agf_xref_refcblks(sc);
533 :
534 : /* scrub teardown will take care of sc->sa for us */
535 : }
536 :
537 : /* Scrub the AGF. */
538 : int
539 869321 : xchk_agf(
540 : struct xfs_scrub *sc)
541 : {
542 869321 : struct xfs_mount *mp = sc->mp;
543 869321 : struct xfs_agf *agf;
544 869321 : struct xfs_perag *pag;
545 869321 : xfs_agnumber_t agno = sc->sm->sm_agno;
546 869321 : xfs_agblock_t agbno;
547 869321 : xfs_agblock_t eoag;
548 869321 : xfs_agblock_t agfl_first;
549 869321 : xfs_agblock_t agfl_last;
550 869321 : xfs_agblock_t agfl_count;
551 869321 : xfs_agblock_t fl_count;
552 869321 : int level;
553 869321 : int error = 0;
554 :
555 869321 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
556 869757 : if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
557 25249 : goto out;
558 843907 : xchk_buffer_recheck(sc, sc->sa.agf_bp);
559 :
560 843505 : agf = sc->sa.agf_bp->b_addr;
561 843505 : pag = sc->sa.pag;
562 :
563 : /* Check the AG length */
564 843505 : eoag = be32_to_cpu(agf->agf_length);
565 843505 : if (eoag != pag->block_count)
566 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
567 :
568 : /* Check the AGF btree roots and levels */
569 843505 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
570 843505 : if (!xfs_verify_agbno(pag, agbno))
571 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
572 :
573 843333 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
574 843333 : if (!xfs_verify_agbno(pag, agbno))
575 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
576 :
577 843224 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
578 843224 : if (level <= 0 || level > mp->m_alloc_maxlevels)
579 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
580 :
581 843121 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
582 843121 : if (level <= 0 || level > mp->m_alloc_maxlevels)
583 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
584 :
585 843345 : if (xfs_has_rmapbt(mp)) {
586 831853 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
587 831853 : if (!xfs_verify_agbno(pag, agbno))
588 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
589 :
590 831630 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
591 831630 : if (level <= 0 || level > mp->m_rmap_maxlevels)
592 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
593 : }
594 :
595 843122 : if (xfs_has_reflink(mp)) {
596 831806 : agbno = be32_to_cpu(agf->agf_refcount_root);
597 831806 : if (!xfs_verify_agbno(pag, agbno))
598 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
599 :
600 831820 : level = be32_to_cpu(agf->agf_refcount_level);
601 831820 : if (level <= 0 || level > mp->m_refc_maxlevels)
602 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
603 : }
604 :
605 : /* Check the AGFL counters */
606 843136 : agfl_first = be32_to_cpu(agf->agf_flfirst);
607 843136 : agfl_last = be32_to_cpu(agf->agf_fllast);
608 843136 : agfl_count = be32_to_cpu(agf->agf_flcount);
609 843136 : if (agfl_last > agfl_first)
610 818059 : fl_count = agfl_last - agfl_first + 1;
611 : else
612 25077 : fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
613 843157 : if (agfl_count != 0 && fl_count != agfl_count)
614 1 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
615 :
616 : /* Do the incore counters match? */
617 843157 : if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
618 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
619 843157 : if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
620 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
621 843157 : if (xfs_has_lazysbcount(sc->mp) &&
622 842941 : pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
623 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
624 :
625 843157 : xchk_agf_xref(sc);
626 870072 : out:
627 870072 : return error;
628 : }
629 :
630 : /* AGFL */
631 :
632 : struct xchk_agfl_info {
633 : /* Number of AGFL entries that the AGF claims are in use. */
634 : unsigned int agflcount;
635 :
636 : /* Number of AGFL entries that we found. */
637 : unsigned int nr_entries;
638 :
639 : /* Buffer to hold AGFL entries for extent checking. */
640 : xfs_agblock_t *entries;
641 :
642 : struct xfs_buf *agfl_bp;
643 : struct xfs_scrub *sc;
644 : };
645 :
646 : /* Cross-reference with the other btrees. */
647 : STATIC void
648 6607244 : xchk_agfl_block_xref(
649 : struct xfs_scrub *sc,
650 : xfs_agblock_t agbno)
651 : {
652 6607244 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
653 : return;
654 :
655 6607221 : xchk_xref_is_used_space(sc, agbno, 1);
656 6607841 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
657 6608876 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
658 6607711 : xchk_xref_is_not_shared(sc, agbno, 1);
659 6607929 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
660 : }
661 :
662 : /* Scrub an AGFL block. */
663 : STATIC int
664 6607381 : xchk_agfl_block(
665 : struct xfs_mount *mp,
666 : xfs_agblock_t agbno,
667 : void *priv)
668 : {
669 6607381 : struct xchk_agfl_info *sai = priv;
670 6607381 : struct xfs_scrub *sc = sai->sc;
671 :
672 6607381 : if (xfs_verify_agbno(sc->sa.pag, agbno) &&
673 6607381 : sai->nr_entries < sai->agflcount)
674 6607315 : sai->entries[sai->nr_entries++] = agbno;
675 : else
676 69 : xchk_block_set_corrupt(sc, sai->agfl_bp);
677 :
678 6607315 : xchk_agfl_block_xref(sc, agbno);
679 :
680 6608178 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
681 0 : return -ECANCELED;
682 :
683 : return 0;
684 : }
685 :
686 : static int
687 18694860 : xchk_agblock_cmp(
688 : const void *pa,
689 : const void *pb)
690 : {
691 18694860 : const xfs_agblock_t *a = pa;
692 18694860 : const xfs_agblock_t *b = pb;
693 :
694 18694860 : return (int)*a - (int)*b;
695 : }
696 :
697 : /* Cross-reference with the other btrees. */
698 : STATIC void
699 889984 : xchk_agfl_xref(
700 : struct xfs_scrub *sc)
701 : {
702 889984 : struct xfs_mount *mp = sc->mp;
703 889984 : xfs_agblock_t agbno;
704 :
705 889984 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
706 : return;
707 :
708 889984 : agbno = XFS_AGFL_BLOCK(mp);
709 :
710 889984 : xchk_ag_btcur_init(sc, &sc->sa);
711 :
712 890149 : xchk_xref_is_used_space(sc, agbno, 1);
713 889561 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
714 890076 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
715 889494 : xchk_xref_is_not_shared(sc, agbno, 1);
716 889758 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
717 :
718 : /*
719 : * Scrub teardown will take care of sc->sa for us. Leave sc->sa
720 : * active so that the agfl block xref can use it too.
721 : */
722 : }
723 :
724 : /* Scrub the AGFL. */
725 : int
726 916348 : xchk_agfl(
727 : struct xfs_scrub *sc)
728 : {
729 916348 : struct xchk_agfl_info sai = {
730 : .sc = sc,
731 : };
732 916348 : struct xfs_agf *agf;
733 916348 : xfs_agnumber_t agno = sc->sm->sm_agno;
734 916348 : unsigned int i;
735 916348 : int error;
736 :
737 : /* Lock the AGF and AGI so that nobody can touch this AG. */
738 916348 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
739 916477 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
740 26735 : return error;
741 889242 : if (!sc->sa.agf_bp)
742 : return -EFSCORRUPTED;
743 :
744 : /* Try to read the AGFL, and verify its structure if we get it. */
745 889242 : error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &sai.agfl_bp);
746 890050 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
747 0 : return error;
748 890030 : xchk_buffer_recheck(sc, sai.agfl_bp);
749 :
750 890071 : xchk_agfl_xref(sc);
751 :
752 889857 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
753 0 : goto out;
754 :
755 : /* Allocate buffer to ensure uniqueness of AGFL entries. */
756 889857 : agf = sc->sa.agf_bp->b_addr;
757 889857 : sai.agflcount = be32_to_cpu(agf->agf_flcount);
758 889857 : if (sai.agflcount > xfs_agfl_size(sc->mp)) {
759 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
760 0 : goto out;
761 : }
762 889046 : sai.entries = kvcalloc(sai.agflcount, sizeof(xfs_agblock_t),
763 : XCHK_GFP_FLAGS);
764 889659 : if (!sai.entries) {
765 0 : error = -ENOMEM;
766 0 : goto out;
767 : }
768 :
769 : /* Check the blocks in the AGFL. */
770 889659 : error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr, sai.agfl_bp,
771 : xchk_agfl_block, &sai);
772 890038 : if (error == -ECANCELED) {
773 0 : error = 0;
774 0 : goto out_free;
775 : }
776 890038 : if (error)
777 0 : goto out_free;
778 :
779 890038 : if (sai.agflcount != sai.nr_entries) {
780 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
781 0 : goto out_free;
782 : }
783 :
784 : /* Sort entries, check for duplicates. */
785 890038 : sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
786 : xchk_agblock_cmp, NULL);
787 7515253 : for (i = 1; i < sai.nr_entries; i++) {
788 5736189 : if (sai.entries[i] == sai.entries[i - 1]) {
789 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
790 0 : break;
791 : }
792 : }
793 :
794 889026 : out_free:
795 889026 : kvfree(sai.entries);
796 889582 : out:
797 889582 : return error;
798 : }
799 :
800 : /* AGI */
801 :
802 : /* Check agi_count/agi_freecount */
803 : static inline void
804 911379 : xchk_agi_xref_icounts(
805 : struct xfs_scrub *sc)
806 : {
807 911379 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
808 911379 : xfs_agino_t icount;
809 911379 : xfs_agino_t freecount;
810 911379 : int error;
811 :
812 911379 : if (!sc->sa.ino_cur)
813 0 : return;
814 :
815 911379 : error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
816 910809 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
817 : return;
818 910723 : if (be32_to_cpu(agi->agi_count) != icount ||
819 910723 : be32_to_cpu(agi->agi_freecount) != freecount)
820 24 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
821 : }
822 :
823 : /* Check agi_[fi]blocks against tree size */
824 : static inline void
825 911311 : xchk_agi_xref_fiblocks(
826 : struct xfs_scrub *sc)
827 : {
828 911311 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
829 911311 : xfs_agblock_t blocks;
830 911311 : int error = 0;
831 :
832 911311 : if (!xfs_has_inobtcounts(sc->mp))
833 470 : return;
834 :
835 910841 : if (sc->sa.ino_cur) {
836 910792 : error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
837 910027 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
838 : return;
839 910107 : if (blocks != be32_to_cpu(agi->agi_iblocks))
840 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
841 : }
842 :
843 910156 : if (sc->sa.fino_cur) {
844 910176 : error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
845 910906 : if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
846 : return;
847 910870 : if (blocks != be32_to_cpu(agi->agi_fblocks))
848 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
849 : }
850 : }
851 :
852 : /* Cross-reference with the other btrees. */
853 : STATIC void
854 911119 : xchk_agi_xref(
855 : struct xfs_scrub *sc)
856 : {
857 911119 : struct xfs_mount *mp = sc->mp;
858 911119 : xfs_agblock_t agbno;
859 :
860 911119 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
861 : return;
862 :
863 911119 : agbno = XFS_AGI_BLOCK(mp);
864 :
865 911119 : xchk_ag_btcur_init(sc, &sc->sa);
866 :
867 911429 : xchk_xref_is_used_space(sc, agbno, 1);
868 911143 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
869 911456 : xchk_agi_xref_icounts(sc);
870 910325 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
871 911217 : xchk_xref_is_not_shared(sc, agbno, 1);
872 911295 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
873 911239 : xchk_agi_xref_fiblocks(sc);
874 :
875 : /* scrub teardown will take care of sc->sa for us */
876 : }
877 :
878 : /*
879 : * Check the unlinked buckets for links to bad inodes. We hold the AGI, so
880 : * there cannot be any threads updating unlinked list pointers in this AG.
881 : */
882 : STATIC void
883 911155 : xchk_iunlink(
884 : struct xfs_scrub *sc,
885 : struct xfs_agi *agi)
886 : {
887 911155 : unsigned int i;
888 911155 : struct xfs_inode *ip;
889 :
890 59114245 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
891 58203173 : xfs_agino_t agino = be32_to_cpu(agi->agi_unlinked[i]);
892 :
893 58218057 : while (agino != NULLAGINO) {
894 14967 : if (agino % XFS_AGI_UNLINKED_BUCKETS != i) {
895 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
896 0 : return;
897 : }
898 :
899 14967 : ip = xfs_iunlink_lookup(sc->sa.pag, agino);
900 15345 : if (!ip) {
901 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
902 0 : return;
903 : }
904 :
905 15345 : if (!xfs_inode_on_unlinked_list(ip)) {
906 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
907 0 : return;
908 : }
909 :
910 15345 : agino = ip->i_next_unlinked;
911 : }
912 : }
913 : }
914 :
915 : /* Scrub the AGI. */
916 : int
917 938647 : xchk_agi(
918 : struct xfs_scrub *sc)
919 : {
920 938647 : struct xfs_mount *mp = sc->mp;
921 938647 : struct xfs_agi *agi;
922 938647 : struct xfs_perag *pag;
923 938647 : struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
924 938647 : xfs_agnumber_t agno = sc->sm->sm_agno;
925 938647 : xfs_agblock_t agbno;
926 938647 : xfs_agblock_t eoag;
927 938647 : xfs_agino_t agino;
928 938647 : xfs_agino_t first_agino;
929 938647 : xfs_agino_t last_agino;
930 938647 : xfs_agino_t icount;
931 938647 : int i;
932 938647 : int level;
933 938647 : int error = 0;
934 :
935 938647 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
936 938499 : if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
937 27435 : goto out;
938 910669 : xchk_buffer_recheck(sc, sc->sa.agi_bp);
939 :
940 911166 : agi = sc->sa.agi_bp->b_addr;
941 911166 : pag = sc->sa.pag;
942 :
943 : /* Check the AG length */
944 911166 : eoag = be32_to_cpu(agi->agi_length);
945 911166 : if (eoag != pag->block_count)
946 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
947 :
948 : /* Check btree roots and levels */
949 911166 : agbno = be32_to_cpu(agi->agi_root);
950 911166 : if (!xfs_verify_agbno(pag, agbno))
951 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
952 :
953 911134 : level = be32_to_cpu(agi->agi_level);
954 911134 : if (level <= 0 || level > igeo->inobt_maxlevels)
955 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
956 :
957 910843 : if (xfs_has_finobt(mp)) {
958 910821 : agbno = be32_to_cpu(agi->agi_free_root);
959 910821 : if (!xfs_verify_agbno(pag, agbno))
960 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
961 :
962 910787 : level = be32_to_cpu(agi->agi_free_level);
963 910787 : if (level <= 0 || level > igeo->inobt_maxlevels)
964 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
965 : }
966 :
967 : /* Check inode counters */
968 910809 : xfs_agino_range(mp, agno, &first_agino, &last_agino);
969 910448 : icount = be32_to_cpu(agi->agi_count);
970 910448 : if (icount > last_agino - first_agino + 1 ||
971 910448 : icount < be32_to_cpu(agi->agi_freecount))
972 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
973 :
974 : /* Check inode pointers */
975 910414 : agino = be32_to_cpu(agi->agi_newino);
976 910414 : if (!xfs_verify_agino_or_null(pag, agino))
977 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
978 :
979 910384 : agino = be32_to_cpu(agi->agi_dirino);
980 910384 : if (!xfs_verify_agino_or_null(pag, agino))
981 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
982 :
983 : /* Check unlinked inode buckets */
984 59116161 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
985 58204811 : agino = be32_to_cpu(agi->agi_unlinked[i]);
986 58205777 : if (!xfs_verify_agino_or_null(pag, agino))
987 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
988 : }
989 :
990 911350 : if (agi->agi_pad32 != cpu_to_be32(0))
991 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
992 :
993 : /* Do the incore counters match? */
994 911350 : if (pag->pagi_count != be32_to_cpu(agi->agi_count))
995 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
996 911350 : if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
997 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
998 :
999 911350 : xchk_iunlink(sc, agi);
1000 :
1001 911111 : xchk_agi_xref(sc);
1002 938772 : out:
1003 938772 : return error;
1004 : }
|