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 2822505 : xchk_setup_agheader(
24 : struct xfs_scrub *sc)
25 : {
26 2822505 : if (xchk_need_intent_drain(sc))
27 82921 : xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
28 2822505 : return xchk_setup_fs(sc);
29 : }
30 :
31 : /* Superblock */
32 :
33 : /* Cross-reference with the other btrees. */
34 : STATIC void
35 814150 : xchk_superblock_xref(
36 : struct xfs_scrub *sc,
37 : struct xfs_buf *bp)
38 : {
39 814150 : struct xfs_mount *mp = sc->mp;
40 814150 : xfs_agnumber_t agno = sc->sm->sm_agno;
41 814150 : xfs_agblock_t agbno;
42 814150 : int error;
43 :
44 814150 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
45 6865 : return;
46 :
47 814225 : agbno = XFS_SB_BLOCK(mp);
48 :
49 814225 : error = xchk_ag_init_existing(sc, agno, &sc->sa);
50 814561 : if (!xchk_xref_process_error(sc, agno, agbno, &error))
51 : return;
52 :
53 807280 : xchk_xref_is_used_space(sc, agbno, 1);
54 807089 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
55 807711 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
56 807300 : xchk_xref_is_not_shared(sc, agbno, 1);
57 807376 : 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 916792 : xchk_superblock(
72 : struct xfs_scrub *sc)
73 : {
74 916792 : struct xfs_mount *mp = sc->mp;
75 916792 : struct xfs_buf *bp;
76 916792 : struct xfs_dsb *sb;
77 916792 : struct xfs_perag *pag;
78 916792 : xfs_agnumber_t agno;
79 916792 : uint32_t v2_ok;
80 916792 : __be32 features_mask;
81 916792 : int error;
82 916792 : __be16 vernum_mask;
83 :
84 916792 : agno = sc->sm->sm_agno;
85 916792 : 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 814417 : pag = xfs_perag_get(mp, agno);
94 814736 : if (!pag)
95 : return -ENOENT;
96 :
97 814736 : 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 814645 : switch (error) {
107 11 : case -EINVAL: /* also -EWRONGFS */
108 : case -ENOSYS:
109 : case -EFBIG:
110 11 : error = -EFSCORRUPTED;
111 814645 : fallthrough;
112 : default:
113 814645 : break;
114 : }
115 814645 : if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
116 99 : goto out_pag;
117 :
118 814324 : 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 814324 : if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
127 0 : xchk_block_set_corrupt(sc, bp);
128 :
129 814324 : if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
130 0 : xchk_block_set_corrupt(sc, bp);
131 :
132 814324 : if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
133 0 : xchk_block_set_corrupt(sc, bp);
134 :
135 814324 : if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
136 0 : xchk_block_set_corrupt(sc, bp);
137 :
138 814324 : if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
139 0 : xchk_block_set_preen(sc, bp);
140 :
141 814530 : if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
142 0 : xchk_block_set_corrupt(sc, bp);
143 :
144 814530 : if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
145 104608 : xchk_block_set_preen(sc, bp);
146 :
147 814538 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
148 244797 : xchk_block_set_preen(sc, bp);
149 :
150 814483 : if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
151 244720 : xchk_block_set_preen(sc, bp);
152 :
153 814484 : if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
154 0 : xchk_block_set_corrupt(sc, bp);
155 :
156 814484 : if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
157 0 : xchk_block_set_corrupt(sc, bp);
158 :
159 814484 : if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
160 0 : xchk_block_set_corrupt(sc, bp);
161 :
162 814484 : if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
163 0 : xchk_block_set_corrupt(sc, bp);
164 :
165 814484 : if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
166 0 : xchk_block_set_corrupt(sc, bp);
167 :
168 : /* Check sb_versionnum bits that are set at mkfs time. */
169 814484 : vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS |
170 : XFS_SB_VERSION_NUMBITS |
171 : XFS_SB_VERSION_ALIGNBIT |
172 : XFS_SB_VERSION_DALIGNBIT |
173 : XFS_SB_VERSION_SHAREDBIT |
174 : XFS_SB_VERSION_LOGV2BIT |
175 : XFS_SB_VERSION_SECTORBIT |
176 : XFS_SB_VERSION_EXTFLGBIT |
177 : XFS_SB_VERSION_DIRV2BIT);
178 814484 : if ((sb->sb_versionnum & vernum_mask) !=
179 814484 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
180 0 : xchk_block_set_corrupt(sc, bp);
181 :
182 : /* Check sb_versionnum bits that can be set after mkfs time. */
183 814484 : vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
184 : XFS_SB_VERSION_NLINKBIT |
185 : XFS_SB_VERSION_QUOTABIT);
186 814484 : if ((sb->sb_versionnum & vernum_mask) !=
187 814484 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
188 137573 : xchk_block_set_preen(sc, bp);
189 :
190 814433 : if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
191 0 : xchk_block_set_corrupt(sc, bp);
192 :
193 814433 : if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
194 0 : xchk_block_set_corrupt(sc, bp);
195 :
196 814433 : if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
197 0 : xchk_block_set_corrupt(sc, bp);
198 :
199 1628866 : if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
200 99 : xchk_block_set_preen(sc, bp);
201 :
202 814433 : if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
203 0 : xchk_block_set_corrupt(sc, bp);
204 :
205 814433 : if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
206 0 : xchk_block_set_corrupt(sc, bp);
207 :
208 814433 : if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
209 0 : xchk_block_set_corrupt(sc, bp);
210 :
211 814433 : if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
212 0 : xchk_block_set_corrupt(sc, bp);
213 :
214 814433 : if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
215 0 : xchk_block_set_corrupt(sc, bp);
216 :
217 814433 : if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
218 0 : xchk_block_set_corrupt(sc, bp);
219 :
220 814433 : if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
221 0 : xchk_block_set_preen(sc, bp);
222 :
223 : /*
224 : * Skip the summary counters since we track them in memory anyway.
225 : * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
226 : */
227 :
228 814433 : if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
229 244799 : xchk_block_set_preen(sc, bp);
230 :
231 814307 : if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
232 244747 : xchk_block_set_preen(sc, bp);
233 :
234 : /*
235 : * Skip the quota flags since repair will force quotacheck.
236 : * sb_qflags
237 : */
238 :
239 814267 : if (sb->sb_flags != mp->m_sb.sb_flags)
240 0 : xchk_block_set_corrupt(sc, bp);
241 :
242 814267 : if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
243 0 : xchk_block_set_corrupt(sc, bp);
244 :
245 814267 : if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
246 0 : xchk_block_set_corrupt(sc, bp);
247 :
248 814267 : if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
249 0 : xchk_block_set_preen(sc, bp);
250 :
251 814267 : if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
252 330 : xchk_block_set_preen(sc, bp);
253 :
254 814267 : if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
255 0 : xchk_block_set_corrupt(sc, bp);
256 :
257 814267 : if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
258 0 : xchk_block_set_corrupt(sc, bp);
259 :
260 814267 : if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
261 0 : xchk_block_set_corrupt(sc, bp);
262 :
263 814267 : if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
264 0 : xchk_block_set_corrupt(sc, bp);
265 :
266 : /* Do we see any invalid bits in sb_features2? */
267 814278 : if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
268 0 : if (sb->sb_features2 != 0)
269 0 : xchk_block_set_corrupt(sc, bp);
270 : } else {
271 814267 : v2_ok = XFS_SB_VERSION2_OKBITS;
272 814267 : if (xfs_sb_is_v5(&mp->m_sb))
273 814215 : v2_ok |= XFS_SB_VERSION2_CRCBIT;
274 :
275 814267 : if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
276 0 : xchk_block_set_corrupt(sc, bp);
277 :
278 814267 : if (sb->sb_features2 != sb->sb_bad_features2)
279 0 : xchk_block_set_preen(sc, bp);
280 : }
281 :
282 : /* Check sb_features2 flags that are set at mkfs time. */
283 814267 : features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
284 : XFS_SB_VERSION2_PROJID32BIT |
285 : XFS_SB_VERSION2_CRCBIT |
286 : XFS_SB_VERSION2_FTYPE);
287 814267 : if ((sb->sb_features2 & features_mask) !=
288 814267 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
289 0 : xchk_block_set_corrupt(sc, bp);
290 :
291 : /* Check sb_features2 flags that can be set after mkfs time. */
292 814267 : features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
293 814267 : if ((sb->sb_features2 & features_mask) !=
294 814267 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
295 0 : xchk_block_set_preen(sc, bp);
296 :
297 814041 : if (!xfs_has_crc(mp)) {
298 : /* all v5 fields must be zero */
299 22 : if (memchr_inv(&sb->sb_features_compat, 0,
300 : sizeof(struct xfs_dsb) -
301 : offsetof(struct xfs_dsb, sb_features_compat)))
302 0 : xchk_block_set_corrupt(sc, bp);
303 : } else {
304 : /* compat features must match */
305 814030 : if (sb->sb_features_compat !=
306 814030 : cpu_to_be32(mp->m_sb.sb_features_compat))
307 0 : xchk_block_set_corrupt(sc, bp);
308 :
309 : /* ro compat features must match */
310 814030 : if (sb->sb_features_ro_compat !=
311 814030 : cpu_to_be32(mp->m_sb.sb_features_ro_compat))
312 0 : xchk_block_set_corrupt(sc, bp);
313 :
314 : /*
315 : * NEEDSREPAIR is ignored on a secondary super, so we should
316 : * clear it when we find it, though it's not a corruption.
317 : */
318 814030 : features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
319 814030 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
320 814030 : sb->sb_features_incompat) & features_mask)
321 0 : xchk_block_set_preen(sc, bp);
322 :
323 : /* all other incompat features must match */
324 814030 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
325 814030 : sb->sb_features_incompat) & ~features_mask)
326 0 : xchk_block_set_corrupt(sc, bp);
327 :
328 : /*
329 : * log incompat features protect newer log record types from
330 : * older log recovery code. Log recovery doesn't check the
331 : * secondary supers, so we can clear these if needed.
332 : */
333 814030 : if (sb->sb_features_log_incompat)
334 14786 : xchk_block_set_preen(sc, bp);
335 :
336 : /* Don't care about sb_crc */
337 :
338 814034 : if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
339 0 : xchk_block_set_corrupt(sc, bp);
340 :
341 814034 : if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
342 244627 : xchk_block_set_preen(sc, bp);
343 :
344 : /* Don't care about sb_lsn */
345 : }
346 :
347 814008 : if (xfs_has_metauuid(mp)) {
348 : /* The metadata UUID must be the same for all supers */
349 24 : if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
350 0 : xchk_block_set_corrupt(sc, bp);
351 : }
352 :
353 : /* Everything else must be zero. */
354 813910 : if (memchr_inv(sb + 1, 0,
355 814008 : BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
356 0 : xchk_block_set_corrupt(sc, bp);
357 :
358 813910 : xchk_superblock_xref(sc, bp);
359 814442 : out_pag:
360 814442 : xfs_perag_put(pag);
361 814632 : return error;
362 : }
363 :
364 : /* AGF */
365 :
366 : /* Tally freespace record lengths. */
367 : STATIC int
368 1875808763 : xchk_agf_record_bno_lengths(
369 : struct xfs_btree_cur *cur,
370 : const struct xfs_alloc_rec_incore *rec,
371 : void *priv)
372 : {
373 1875808763 : xfs_extlen_t *blocks = priv;
374 :
375 1875808763 : (*blocks) += rec->ar_blockcount;
376 1875808763 : return 0;
377 : }
378 :
379 : /* Check agf_freeblks */
380 : static inline void
381 582093 : xchk_agf_xref_freeblks(
382 : struct xfs_scrub *sc)
383 : {
384 582093 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
385 582093 : xfs_extlen_t blocks = 0;
386 582093 : int error;
387 :
388 582093 : if (!sc->sa.bno_cur)
389 0 : return;
390 :
391 582093 : error = xfs_alloc_query_all(sc->sa.bno_cur,
392 : xchk_agf_record_bno_lengths, &blocks);
393 582226 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
394 : return;
395 582064 : if (blocks != be32_to_cpu(agf->agf_freeblks))
396 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
397 : }
398 :
399 : /* Cross reference the AGF with the cntbt (freespace by length btree) */
400 : static inline void
401 582070 : xchk_agf_xref_cntbt(
402 : struct xfs_scrub *sc)
403 : {
404 582070 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
405 582070 : xfs_agblock_t agbno;
406 582070 : xfs_extlen_t blocks;
407 582070 : int have;
408 582070 : int error;
409 :
410 582070 : if (!sc->sa.cnt_cur)
411 12 : return;
412 :
413 : /* Any freespace at all? */
414 582070 : error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
415 582429 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
416 : return;
417 582141 : if (!have) {
418 12 : if (agf->agf_freeblks != cpu_to_be32(0))
419 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
420 12 : return;
421 : }
422 :
423 : /* Check agf_longest */
424 582129 : error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
425 582348 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
426 : return;
427 582166 : if (!have || blocks != be32_to_cpu(agf->agf_longest))
428 4 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
429 : }
430 :
431 : /* Check the btree block counts in the AGF against the btrees. */
432 : STATIC void
433 582163 : xchk_agf_xref_btreeblks(
434 : struct xfs_scrub *sc)
435 : {
436 582163 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
437 582163 : struct xfs_mount *mp = sc->mp;
438 582163 : xfs_agblock_t blocks;
439 582163 : xfs_agblock_t btreeblks;
440 582163 : int error;
441 :
442 : /* agf_btreeblks didn't exist before lazysbcount */
443 582163 : if (!xfs_has_lazysbcount(sc->mp))
444 3 : return;
445 :
446 : /* Check agf_rmap_blocks; set up for agf_btreeblks check */
447 582163 : if (sc->sa.rmap_cur) {
448 416668 : error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
449 416570 : if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
450 : return;
451 416469 : btreeblks = blocks - 1;
452 416469 : if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
453 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
454 : } else {
455 : btreeblks = 0;
456 : }
457 :
458 : /*
459 : * No rmap cursor; we can't xref if we have the rmapbt feature.
460 : * We also can't do it if we're missing the free space btree cursors.
461 : */
462 581964 : if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
463 581964 : !sc->sa.bno_cur || !sc->sa.cnt_cur)
464 : return;
465 :
466 : /* Check agf_btreeblks */
467 581964 : error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
468 582253 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
469 : return;
470 582198 : btreeblks += blocks - 1;
471 :
472 582198 : error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
473 582636 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
474 : return;
475 582490 : btreeblks += blocks - 1;
476 :
477 582490 : if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
478 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
479 : }
480 :
481 : /* Check agf_refcount_blocks against tree size */
482 : static inline void
483 582166 : xchk_agf_xref_refcblks(
484 : struct xfs_scrub *sc)
485 : {
486 582166 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
487 582166 : xfs_agblock_t blocks;
488 582166 : int error;
489 :
490 582166 : if (!sc->sa.refc_cur)
491 165441 : return;
492 :
493 416725 : error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
494 416898 : if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
495 : return;
496 416873 : if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
497 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
498 : }
499 :
500 : /* Cross-reference with the other btrees. */
501 : STATIC void
502 582032 : xchk_agf_xref(
503 : struct xfs_scrub *sc)
504 : {
505 582032 : struct xfs_mount *mp = sc->mp;
506 582032 : xfs_agblock_t agbno;
507 :
508 582032 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
509 : return;
510 :
511 582032 : agbno = XFS_AGF_BLOCK(mp);
512 :
513 582032 : xchk_ag_btcur_init(sc, &sc->sa);
514 :
515 582535 : xchk_xref_is_used_space(sc, agbno, 1);
516 582158 : xchk_agf_xref_freeblks(sc);
517 582197 : xchk_agf_xref_cntbt(sc);
518 582220 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
519 582633 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
520 582301 : xchk_agf_xref_btreeblks(sc);
521 582381 : xchk_xref_is_not_shared(sc, agbno, 1);
522 582213 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
523 582287 : xchk_agf_xref_refcblks(sc);
524 :
525 : /* scrub teardown will take care of sc->sa for us */
526 : }
527 :
528 : /* Scrub the AGF. */
529 : int
530 606688 : xchk_agf(
531 : struct xfs_scrub *sc)
532 : {
533 606688 : struct xfs_mount *mp = sc->mp;
534 606688 : struct xfs_agf *agf;
535 606688 : struct xfs_perag *pag;
536 606688 : xfs_agnumber_t agno = sc->sm->sm_agno;
537 606688 : xfs_agblock_t agbno;
538 606688 : xfs_agblock_t eoag;
539 606688 : xfs_agblock_t agfl_first;
540 606688 : xfs_agblock_t agfl_last;
541 606688 : xfs_agblock_t agfl_count;
542 606688 : xfs_agblock_t fl_count;
543 606688 : int level;
544 606688 : int error = 0;
545 :
546 606688 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
547 606913 : if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
548 24366 : goto out;
549 582241 : xchk_buffer_recheck(sc, sc->sa.agf_bp);
550 :
551 581680 : agf = sc->sa.agf_bp->b_addr;
552 581680 : pag = sc->sa.pag;
553 :
554 : /* Check the AG length */
555 581680 : eoag = be32_to_cpu(agf->agf_length);
556 581680 : if (eoag != pag->block_count)
557 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
558 :
559 : /* Check the AGF btree roots and levels */
560 581680 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
561 581680 : if (!xfs_verify_agbno(pag, agbno))
562 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
563 :
564 581987 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
565 581987 : if (!xfs_verify_agbno(pag, agbno))
566 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
567 :
568 581994 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
569 581994 : if (level <= 0 || level > mp->m_alloc_maxlevels)
570 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
571 :
572 581936 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
573 581936 : if (level <= 0 || level > mp->m_alloc_maxlevels)
574 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
575 :
576 581820 : if (xfs_has_rmapbt(mp)) {
577 416638 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
578 416638 : if (!xfs_verify_agbno(pag, agbno))
579 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
580 :
581 416617 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
582 416617 : if (level <= 0 || level > mp->m_rmap_maxlevels)
583 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
584 : }
585 :
586 581799 : if (xfs_has_reflink(mp)) {
587 416529 : agbno = be32_to_cpu(agf->agf_refcount_root);
588 416529 : if (!xfs_verify_agbno(pag, agbno))
589 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
590 :
591 416458 : level = be32_to_cpu(agf->agf_refcount_level);
592 416458 : if (level <= 0 || level > mp->m_refc_maxlevels)
593 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
594 : }
595 :
596 : /* Check the AGFL counters */
597 581728 : agfl_first = be32_to_cpu(agf->agf_flfirst);
598 581728 : agfl_last = be32_to_cpu(agf->agf_fllast);
599 581728 : agfl_count = be32_to_cpu(agf->agf_flcount);
600 581728 : if (agfl_last > agfl_first)
601 566005 : fl_count = agfl_last - agfl_first + 1;
602 : else
603 15723 : fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
604 581727 : if (agfl_count != 0 && fl_count != agfl_count)
605 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
606 :
607 : /* Do the incore counters match? */
608 581727 : if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
609 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
610 581727 : if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
611 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
612 581727 : if (xfs_has_lazysbcount(sc->mp) &&
613 581547 : pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
614 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
615 :
616 581727 : xchk_agf_xref(sc);
617 606603 : out:
618 606603 : return error;
619 : }
620 :
621 : /* AGFL */
622 :
623 : struct xchk_agfl_info {
624 : /* Number of AGFL entries that the AGF claims are in use. */
625 : unsigned int agflcount;
626 :
627 : /* Number of AGFL entries that we found. */
628 : unsigned int nr_entries;
629 :
630 : /* Buffer to hold AGFL entries for extent checking. */
631 : xfs_agblock_t *entries;
632 :
633 : struct xfs_buf *agfl_bp;
634 : struct xfs_scrub *sc;
635 : };
636 :
637 : /* Cross-reference with the other btrees. */
638 : STATIC void
639 4390744 : xchk_agfl_block_xref(
640 : struct xfs_scrub *sc,
641 : xfs_agblock_t agbno)
642 : {
643 4390744 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
644 : return;
645 :
646 4390804 : xchk_xref_is_used_space(sc, agbno, 1);
647 4390966 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
648 4391830 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
649 4391384 : xchk_xref_is_not_shared(sc, agbno, 1);
650 4391430 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
651 : }
652 :
653 : /* Scrub an AGFL block. */
654 : STATIC int
655 4390824 : xchk_agfl_block(
656 : struct xfs_mount *mp,
657 : xfs_agblock_t agbno,
658 : void *priv)
659 : {
660 4390824 : struct xchk_agfl_info *sai = priv;
661 4390824 : struct xfs_scrub *sc = sai->sc;
662 :
663 4390824 : if (xfs_verify_agbno(sc->sa.pag, agbno) &&
664 4390824 : sai->nr_entries < sai->agflcount)
665 4390806 : sai->entries[sai->nr_entries++] = agbno;
666 : else
667 29 : xchk_block_set_corrupt(sc, sai->agfl_bp);
668 :
669 4390806 : xchk_agfl_block_xref(sc, agbno);
670 :
671 4391584 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
672 0 : return -ECANCELED;
673 :
674 : return 0;
675 : }
676 :
677 : static int
678 12244883 : xchk_agblock_cmp(
679 : const void *pa,
680 : const void *pb)
681 : {
682 12244883 : const xfs_agblock_t *a = pa;
683 12244883 : const xfs_agblock_t *b = pb;
684 :
685 12244883 : return (int)*a - (int)*b;
686 : }
687 :
688 : /* Cross-reference with the other btrees. */
689 : STATIC void
690 621512 : xchk_agfl_xref(
691 : struct xfs_scrub *sc)
692 : {
693 621512 : struct xfs_mount *mp = sc->mp;
694 621512 : xfs_agblock_t agbno;
695 :
696 621512 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
697 : return;
698 :
699 621512 : agbno = XFS_AGFL_BLOCK(mp);
700 :
701 621512 : xchk_ag_btcur_init(sc, &sc->sa);
702 :
703 621563 : xchk_xref_is_used_space(sc, agbno, 1);
704 621032 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
705 621552 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
706 621235 : xchk_xref_is_not_shared(sc, agbno, 1);
707 621271 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
708 :
709 : /*
710 : * Scrub teardown will take care of sc->sa for us. Leave sc->sa
711 : * active so that the agfl block xref can use it too.
712 : */
713 : }
714 :
715 : /* Scrub the AGFL. */
716 : int
717 648014 : xchk_agfl(
718 : struct xfs_scrub *sc)
719 : {
720 648014 : struct xchk_agfl_info sai = {
721 : .sc = sc,
722 : };
723 648014 : struct xfs_agf *agf;
724 648014 : xfs_agnumber_t agno = sc->sm->sm_agno;
725 648014 : unsigned int i;
726 648014 : int error;
727 :
728 : /* Lock the AGF and AGI so that nobody can touch this AG. */
729 648014 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
730 648160 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
731 26911 : return error;
732 620885 : if (!sc->sa.agf_bp)
733 : return -EFSCORRUPTED;
734 :
735 : /* Try to read the AGFL, and verify its structure if we get it. */
736 620885 : error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &sai.agfl_bp);
737 621494 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
738 0 : return error;
739 621416 : xchk_buffer_recheck(sc, sai.agfl_bp);
740 :
741 621535 : xchk_agfl_xref(sc);
742 :
743 621247 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
744 0 : goto out;
745 :
746 : /* Allocate buffer to ensure uniqueness of AGFL entries. */
747 621247 : agf = sc->sa.agf_bp->b_addr;
748 621247 : sai.agflcount = be32_to_cpu(agf->agf_flcount);
749 621247 : if (sai.agflcount > xfs_agfl_size(sc->mp)) {
750 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
751 0 : goto out;
752 : }
753 620792 : sai.entries = kvcalloc(sai.agflcount, sizeof(xfs_agblock_t),
754 : XCHK_GFP_FLAGS);
755 621352 : if (!sai.entries) {
756 0 : error = -ENOMEM;
757 0 : goto out;
758 : }
759 :
760 : /* Check the blocks in the AGFL. */
761 621352 : error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr, sai.agfl_bp,
762 : xchk_agfl_block, &sai);
763 621514 : if (error == -ECANCELED) {
764 0 : error = 0;
765 0 : goto out_free;
766 : }
767 621514 : if (error)
768 0 : goto out_free;
769 :
770 621514 : if (sai.agflcount != sai.nr_entries) {
771 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
772 0 : goto out_free;
773 : }
774 :
775 : /* Sort entries, check for duplicates. */
776 621514 : sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
777 : xchk_agblock_cmp, NULL);
778 5022252 : for (i = 1; i < sai.nr_entries; i++) {
779 3780222 : if (sai.entries[i] == sai.entries[i - 1]) {
780 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
781 0 : break;
782 : }
783 : }
784 :
785 620516 : out_free:
786 620516 : kvfree(sai.entries);
787 621149 : out:
788 621149 : return error;
789 : }
790 :
791 : /* AGI */
792 :
793 : /* Check agi_count/agi_freecount */
794 : static inline void
795 623663 : xchk_agi_xref_icounts(
796 : struct xfs_scrub *sc)
797 : {
798 623663 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
799 623663 : xfs_agino_t icount;
800 623663 : xfs_agino_t freecount;
801 623663 : int error;
802 :
803 623663 : if (!sc->sa.ino_cur)
804 0 : return;
805 :
806 623663 : error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
807 623098 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
808 : return;
809 622959 : if (be32_to_cpu(agi->agi_count) != icount ||
810 622959 : be32_to_cpu(agi->agi_freecount) != freecount)
811 13 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
812 : }
813 :
814 : /* Check agi_[fi]blocks against tree size */
815 : static inline void
816 623321 : xchk_agi_xref_fiblocks(
817 : struct xfs_scrub *sc)
818 : {
819 623321 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
820 623321 : xfs_agblock_t blocks;
821 623321 : int error = 0;
822 :
823 623321 : if (!xfs_has_inobtcounts(sc->mp))
824 420 : return;
825 :
826 622901 : if (sc->sa.ino_cur) {
827 622903 : error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
828 622643 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
829 : return;
830 622461 : if (blocks != be32_to_cpu(agi->agi_iblocks))
831 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
832 : }
833 :
834 622459 : if (sc->sa.fino_cur) {
835 622453 : error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
836 623236 : if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
837 : return;
838 623164 : if (blocks != be32_to_cpu(agi->agi_fblocks))
839 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
840 : }
841 : }
842 :
843 : /* Cross-reference with the other btrees. */
844 : STATIC void
845 623433 : xchk_agi_xref(
846 : struct xfs_scrub *sc)
847 : {
848 623433 : struct xfs_mount *mp = sc->mp;
849 623433 : xfs_agblock_t agbno;
850 :
851 623433 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
852 : return;
853 :
854 623433 : agbno = XFS_AGI_BLOCK(mp);
855 :
856 623433 : xchk_ag_btcur_init(sc, &sc->sa);
857 :
858 623764 : xchk_xref_is_used_space(sc, agbno, 1);
859 623244 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
860 623719 : xchk_agi_xref_icounts(sc);
861 622872 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
862 623115 : xchk_xref_is_not_shared(sc, agbno, 1);
863 623305 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
864 623310 : xchk_agi_xref_fiblocks(sc);
865 :
866 : /* scrub teardown will take care of sc->sa for us */
867 : }
868 :
869 : /*
870 : * Check the unlinked buckets for links to bad inodes. We hold the AGI, so
871 : * there cannot be any threads updating unlinked list pointers in this AG.
872 : */
873 : STATIC void
874 623413 : xchk_iunlink(
875 : struct xfs_scrub *sc,
876 : struct xfs_agi *agi)
877 : {
878 623413 : unsigned int i;
879 623413 : struct xfs_inode *ip;
880 :
881 40441211 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
882 39817853 : xfs_agino_t agino = be32_to_cpu(agi->agi_unlinked[i]);
883 :
884 39826616 : while (agino != NULLAGINO) {
885 8818 : if (agino % XFS_AGI_UNLINKED_BUCKETS != i) {
886 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
887 0 : return;
888 : }
889 :
890 8818 : ip = xfs_iunlink_lookup(sc->sa.pag, agino);
891 8743 : if (!ip) {
892 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
893 0 : return;
894 : }
895 :
896 8743 : if (!xfs_inode_on_unlinked_list(ip)) {
897 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
898 0 : return;
899 : }
900 :
901 8743 : agino = ip->i_next_unlinked;
902 : }
903 : }
904 : }
905 :
906 : /* Scrub the AGI. */
907 : int
908 650067 : xchk_agi(
909 : struct xfs_scrub *sc)
910 : {
911 650067 : struct xfs_mount *mp = sc->mp;
912 650067 : struct xfs_agi *agi;
913 650067 : struct xfs_perag *pag;
914 650067 : struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
915 650067 : xfs_agnumber_t agno = sc->sm->sm_agno;
916 650067 : xfs_agblock_t agbno;
917 650067 : xfs_agblock_t eoag;
918 650067 : xfs_agino_t agino;
919 650067 : xfs_agino_t first_agino;
920 650067 : xfs_agino_t last_agino;
921 650067 : xfs_agino_t icount;
922 650067 : int i;
923 650067 : int level;
924 650067 : int error = 0;
925 :
926 650067 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
927 650158 : if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
928 26698 : goto out;
929 623009 : xchk_buffer_recheck(sc, sc->sa.agi_bp);
930 :
931 623589 : agi = sc->sa.agi_bp->b_addr;
932 623589 : pag = sc->sa.pag;
933 :
934 : /* Check the AG length */
935 623589 : eoag = be32_to_cpu(agi->agi_length);
936 623589 : if (eoag != pag->block_count)
937 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
938 :
939 : /* Check btree roots and levels */
940 623589 : agbno = be32_to_cpu(agi->agi_root);
941 623589 : if (!xfs_verify_agbno(pag, agbno))
942 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
943 :
944 623565 : level = be32_to_cpu(agi->agi_level);
945 623565 : if (level <= 0 || level > igeo->inobt_maxlevels)
946 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
947 :
948 623390 : if (xfs_has_finobt(mp)) {
949 623368 : agbno = be32_to_cpu(agi->agi_free_root);
950 623368 : if (!xfs_verify_agbno(pag, agbno))
951 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
952 :
953 623320 : level = be32_to_cpu(agi->agi_free_level);
954 623320 : if (level <= 0 || level > igeo->inobt_maxlevels)
955 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
956 : }
957 :
958 : /* Check inode counters */
959 623342 : xfs_agino_range(mp, agno, &first_agino, &last_agino);
960 622767 : icount = be32_to_cpu(agi->agi_count);
961 622767 : if (icount > last_agino - first_agino + 1 ||
962 622767 : icount < be32_to_cpu(agi->agi_freecount))
963 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
964 :
965 : /* Check inode pointers */
966 622850 : agino = be32_to_cpu(agi->agi_newino);
967 622850 : if (!xfs_verify_agino_or_null(pag, agino))
968 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
969 :
970 622769 : agino = be32_to_cpu(agi->agi_dirino);
971 622769 : if (!xfs_verify_agino_or_null(pag, agino))
972 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
973 :
974 : /* Check unlinked inode buckets */
975 40446322 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
976 39822696 : agino = be32_to_cpu(agi->agi_unlinked[i]);
977 39823553 : if (!xfs_verify_agino_or_null(pag, agino))
978 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
979 : }
980 :
981 623626 : if (agi->agi_pad32 != cpu_to_be32(0))
982 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
983 :
984 : /* Do the incore counters match? */
985 623626 : if (pag->pagi_count != be32_to_cpu(agi->agi_count))
986 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
987 623626 : if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
988 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
989 :
990 623626 : xchk_iunlink(sc, agi);
991 :
992 623386 : xchk_agi_xref(sc);
993 650138 : out:
994 650138 : return error;
995 : }
|