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 1837720 : xchk_setup_agheader(
24 : struct xfs_scrub *sc)
25 : {
26 1837720 : if (xchk_need_intent_drain(sc))
27 21873 : xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
28 1837720 : return xchk_setup_fs(sc);
29 : }
30 :
31 : /* Superblock */
32 :
33 : /* Cross-reference with the other btrees. */
34 : STATIC void
35 423831 : xchk_superblock_xref(
36 : struct xfs_scrub *sc,
37 : struct xfs_buf *bp)
38 : {
39 423831 : struct xfs_mount *mp = sc->mp;
40 423831 : xfs_agnumber_t agno = sc->sm->sm_agno;
41 423831 : xfs_agblock_t agbno;
42 423831 : int error;
43 :
44 423831 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
45 2315 : return;
46 :
47 423831 : agbno = XFS_SB_BLOCK(mp);
48 :
49 423831 : error = xchk_ag_init_existing(sc, agno, &sc->sa);
50 423831 : if (!xchk_xref_process_error(sc, agno, agbno, &error))
51 : return;
52 :
53 421516 : xchk_xref_is_used_space(sc, agbno, 1);
54 421509 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
55 421516 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
56 421510 : xchk_xref_is_not_shared(sc, agbno, 1);
57 421513 : 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 473163 : xchk_superblock(
72 : struct xfs_scrub *sc)
73 : {
74 473163 : struct xfs_mount *mp = sc->mp;
75 473163 : struct xfs_buf *bp;
76 473163 : struct xfs_dsb *sb;
77 473163 : struct xfs_perag *pag;
78 473163 : xfs_agnumber_t agno;
79 473163 : uint32_t v2_ok;
80 473163 : __be32 features_mask;
81 473163 : int error;
82 473163 : __be16 vernum_mask;
83 :
84 473163 : agno = sc->sm->sm_agno;
85 473163 : 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 423848 : pag = xfs_perag_get(mp, agno);
94 423846 : if (!pag)
95 : return -ENOENT;
96 :
97 423846 : 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 423848 : switch (error) {
107 2 : case -EINVAL: /* also -EWRONGFS */
108 : case -ENOSYS:
109 : case -EFBIG:
110 2 : error = -EFSCORRUPTED;
111 423848 : fallthrough;
112 : default:
113 423848 : break;
114 : }
115 423848 : if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
116 18 : goto out_pag;
117 :
118 423831 : 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 847662 : if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
127 0 : xchk_block_set_corrupt(sc, bp);
128 :
129 423831 : if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
130 0 : xchk_block_set_corrupt(sc, bp);
131 :
132 423832 : if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
133 0 : xchk_block_set_corrupt(sc, bp);
134 :
135 423831 : if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
136 0 : xchk_block_set_corrupt(sc, bp);
137 :
138 423831 : if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
139 0 : xchk_block_set_preen(sc, bp);
140 :
141 423831 : if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
142 0 : xchk_block_set_corrupt(sc, bp);
143 :
144 423831 : if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
145 41650 : xchk_block_set_preen(sc, bp);
146 :
147 423832 : if (xfs_has_metadir(sc->mp)) {
148 423342 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_metadirino))
149 118072 : xchk_block_set_preen(sc, bp);
150 : } else {
151 490 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
152 436 : xchk_block_set_preen(sc, bp);
153 :
154 490 : if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
155 436 : xchk_block_set_preen(sc, bp);
156 : }
157 :
158 847664 : if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
159 0 : xchk_block_set_corrupt(sc, bp);
160 :
161 847664 : if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
162 0 : xchk_block_set_corrupt(sc, bp);
163 :
164 847662 : if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
165 0 : xchk_block_set_corrupt(sc, bp);
166 :
167 847662 : if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
168 0 : xchk_block_set_corrupt(sc, bp);
169 :
170 847662 : 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 423831 : 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 847662 : if ((sb->sb_versionnum & vernum_mask) !=
184 423831 : (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 423831 : vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
189 : XFS_SB_VERSION_NLINKBIT |
190 : XFS_SB_VERSION_QUOTABIT);
191 847662 : if ((sb->sb_versionnum & vernum_mask) !=
192 423831 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
193 118374 : xchk_block_set_preen(sc, bp);
194 :
195 423831 : if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
196 0 : xchk_block_set_corrupt(sc, bp);
197 :
198 423832 : if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
199 0 : xchk_block_set_corrupt(sc, bp);
200 :
201 423832 : if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
202 0 : xchk_block_set_corrupt(sc, bp);
203 :
204 847662 : if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
205 18 : xchk_block_set_preen(sc, bp);
206 :
207 423831 : if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
208 0 : xchk_block_set_corrupt(sc, bp);
209 :
210 423831 : if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
211 0 : xchk_block_set_corrupt(sc, bp);
212 :
213 423831 : if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
214 0 : xchk_block_set_corrupt(sc, bp);
215 :
216 423831 : if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
217 0 : xchk_block_set_corrupt(sc, bp);
218 :
219 423831 : if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
220 0 : xchk_block_set_corrupt(sc, bp);
221 :
222 423831 : if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
223 0 : xchk_block_set_corrupt(sc, bp);
224 :
225 423831 : 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 423831 : if (!xfs_has_metadir(sc->mp)) {
234 490 : if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
235 436 : xchk_block_set_preen(sc, bp);
236 :
237 490 : if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
238 436 : 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 423831 : if (sb->sb_flags != mp->m_sb.sb_flags)
247 0 : xchk_block_set_corrupt(sc, bp);
248 :
249 423831 : if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
250 0 : xchk_block_set_corrupt(sc, bp);
251 :
252 847662 : if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
253 0 : xchk_block_set_corrupt(sc, bp);
254 :
255 847662 : if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
256 0 : xchk_block_set_preen(sc, bp);
257 :
258 847662 : if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
259 60 : xchk_block_set_preen(sc, bp);
260 :
261 423831 : if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
262 0 : xchk_block_set_corrupt(sc, bp);
263 :
264 423831 : if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
265 0 : xchk_block_set_corrupt(sc, bp);
266 :
267 423830 : if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
268 0 : xchk_block_set_corrupt(sc, bp);
269 :
270 847664 : 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 423834 : 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 423832 : v2_ok = XFS_SB_VERSION2_OKBITS;
279 423832 : if (xfs_sb_is_v5(&mp->m_sb))
280 423830 : v2_ok |= XFS_SB_VERSION2_CRCBIT;
281 :
282 847664 : if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
283 0 : xchk_block_set_corrupt(sc, bp);
284 :
285 423832 : 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 423832 : 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 423832 : if ((sb->sb_features2 & features_mask) !=
295 423832 : (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 423832 : features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
300 423832 : if ((sb->sb_features2 & features_mask) !=
301 423832 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
302 0 : xchk_block_set_preen(sc, bp);
303 :
304 423831 : if (!xfs_has_crc(mp)) {
305 : /* all v5 fields must be zero */
306 4 : 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 423829 : if (sb->sb_features_compat !=
313 423829 : 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 423829 : if (sb->sb_features_ro_compat !=
318 423829 : 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 423829 : features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
326 423829 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
327 423829 : sb->sb_features_incompat) & features_mask)
328 0 : xchk_block_set_preen(sc, bp);
329 :
330 : /* all other incompat features must match */
331 423827 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
332 423827 : 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 423827 : if (sb->sb_features_log_incompat)
341 4964 : xchk_block_set_preen(sc, bp);
342 :
343 : /* Don't care about sb_crc */
344 :
345 847656 : if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
346 0 : xchk_block_set_corrupt(sc, bp);
347 :
348 423828 : if (!xfs_has_metadir(sc->mp)) {
349 488 : if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
350 434 : xchk_block_set_preen(sc, bp);
351 : }
352 :
353 : /* Don't care about sb_lsn */
354 : }
355 :
356 423830 : if (xfs_has_metauuid(mp)) {
357 : /* The metadata UUID must be the same for all supers */
358 6 : 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 423831 : if (memchr_inv(sb + 1, 0,
364 423830 : BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
365 0 : xchk_block_set_corrupt(sc, bp);
366 :
367 423831 : xchk_superblock_xref(sc, bp);
368 423850 : out_pag:
369 423850 : xfs_perag_put(pag);
370 423850 : return error;
371 : }
372 :
373 : /* AGF */
374 :
375 : /* Tally freespace record lengths. */
376 : STATIC int
377 762908653 : xchk_agf_record_bno_lengths(
378 : struct xfs_btree_cur *cur,
379 : const struct xfs_alloc_rec_incore *rec,
380 : void *priv)
381 : {
382 762908653 : xfs_extlen_t *blocks = priv;
383 :
384 762908653 : (*blocks) += rec->ar_blockcount;
385 762908653 : return 0;
386 : }
387 :
388 : /* Check agf_freeblks */
389 : static inline void
390 438202 : xchk_agf_xref_freeblks(
391 : struct xfs_scrub *sc)
392 : {
393 438202 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
394 438202 : xfs_extlen_t blocks = 0;
395 438202 : int error;
396 :
397 438202 : if (!sc->sa.bno_cur)
398 0 : return;
399 :
400 438202 : error = xfs_alloc_query_all(sc->sa.bno_cur,
401 : xchk_agf_record_bno_lengths, &blocks);
402 438202 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
403 : return;
404 876406 : 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 438201 : xchk_agf_xref_cntbt(
411 : struct xfs_scrub *sc)
412 : {
413 438201 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
414 438201 : xfs_agblock_t agbno;
415 438201 : xfs_extlen_t blocks;
416 438201 : int have;
417 438201 : int error;
418 :
419 438201 : if (!sc->sa.cnt_cur)
420 0 : return;
421 :
422 : /* Any freespace at all? */
423 438201 : error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
424 438202 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
425 : return;
426 438197 : if (!have) {
427 0 : if (agf->agf_freeblks != cpu_to_be32(0))
428 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
429 0 : return;
430 : }
431 :
432 : /* Check agf_longest */
433 438197 : error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
434 438198 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
435 : return;
436 876404 : if (!have || blocks != be32_to_cpu(agf->agf_longest))
437 0 : 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 438202 : xchk_agf_xref_btreeblks(
443 : struct xfs_scrub *sc)
444 : {
445 438202 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
446 438202 : struct xfs_mount *mp = sc->mp;
447 438202 : xfs_agblock_t blocks;
448 438202 : xfs_agblock_t btreeblks;
449 438202 : int error;
450 :
451 : /* agf_btreeblks didn't exist before lazysbcount */
452 438202 : if (!xfs_has_lazysbcount(sc->mp))
453 0 : return;
454 :
455 : /* Check agf_rmap_blocks; set up for agf_btreeblks check */
456 438202 : if (sc->sa.rmap_cur) {
457 438182 : error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
458 438180 : if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
459 : return;
460 438183 : btreeblks = blocks - 1;
461 876366 : 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 438203 : if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
472 438203 : !sc->sa.bno_cur || !sc->sa.cnt_cur)
473 : return;
474 :
475 : /* Check agf_btreeblks */
476 438203 : error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
477 438200 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
478 : return;
479 438202 : btreeblks += blocks - 1;
480 :
481 438202 : error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
482 438198 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
483 : return;
484 438202 : btreeblks += blocks - 1;
485 :
486 876404 : 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 438203 : xchk_agf_xref_refcblks(
493 : struct xfs_scrub *sc)
494 : {
495 438203 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
496 438203 : xfs_agblock_t blocks;
497 438203 : int error;
498 :
499 438203 : if (!sc->sa.refc_cur)
500 28 : return;
501 :
502 438175 : error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
503 438175 : if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
504 : return;
505 876350 : 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 438200 : xchk_agf_xref(
512 : struct xfs_scrub *sc)
513 : {
514 438200 : struct xfs_mount *mp = sc->mp;
515 438200 : xfs_agblock_t agbno;
516 :
517 438200 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
518 : return;
519 :
520 438200 : agbno = XFS_AGF_BLOCK(mp);
521 :
522 438200 : xchk_ag_btcur_init(sc, &sc->sa);
523 :
524 438199 : xchk_xref_is_used_space(sc, agbno, 1);
525 438203 : xchk_agf_xref_freeblks(sc);
526 438202 : xchk_agf_xref_cntbt(sc);
527 438201 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
528 438202 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
529 438202 : xchk_agf_xref_btreeblks(sc);
530 438199 : xchk_xref_is_not_shared(sc, agbno, 1);
531 438203 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
532 438203 : 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 444620 : xchk_agf(
540 : struct xfs_scrub *sc)
541 : {
542 444620 : struct xfs_mount *mp = sc->mp;
543 444620 : struct xfs_agf *agf;
544 444620 : struct xfs_perag *pag;
545 444620 : xfs_agnumber_t agno = sc->sm->sm_agno;
546 444620 : xfs_agblock_t agbno;
547 444620 : xfs_agblock_t eoag;
548 444620 : xfs_agblock_t agfl_first;
549 444620 : xfs_agblock_t agfl_last;
550 444620 : xfs_agblock_t agfl_count;
551 444620 : xfs_agblock_t fl_count;
552 444620 : int level;
553 444620 : int error = 0;
554 :
555 444620 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
556 444624 : if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
557 6421 : goto out;
558 438198 : xchk_buffer_recheck(sc, sc->sa.agf_bp);
559 :
560 438195 : agf = sc->sa.agf_bp->b_addr;
561 438195 : pag = sc->sa.pag;
562 :
563 : /* Check the AG length */
564 438195 : eoag = be32_to_cpu(agf->agf_length);
565 438195 : 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 438196 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
570 438196 : if (!xfs_verify_agbno(pag, agbno))
571 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
572 :
573 438196 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
574 438196 : if (!xfs_verify_agbno(pag, agbno))
575 1 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
576 :
577 438197 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
578 438197 : if (level <= 0 || level > mp->m_alloc_maxlevels)
579 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
580 :
581 438198 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
582 438198 : if (level <= 0 || level > mp->m_alloc_maxlevels)
583 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
584 :
585 438201 : if (xfs_has_rmapbt(mp)) {
586 438181 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
587 438181 : if (!xfs_verify_agbno(pag, agbno))
588 1 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
589 :
590 438179 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
591 438179 : if (level <= 0 || level > mp->m_rmap_maxlevels)
592 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
593 : }
594 :
595 438199 : if (xfs_has_reflink(mp)) {
596 438171 : agbno = be32_to_cpu(agf->agf_refcount_root);
597 438171 : if (!xfs_verify_agbno(pag, agbno))
598 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
599 :
600 438173 : level = be32_to_cpu(agf->agf_refcount_level);
601 438173 : 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 438201 : agfl_first = be32_to_cpu(agf->agf_flfirst);
607 438201 : agfl_last = be32_to_cpu(agf->agf_fllast);
608 438201 : agfl_count = be32_to_cpu(agf->agf_flcount);
609 438201 : if (agfl_last > agfl_first)
610 433392 : fl_count = agfl_last - agfl_first + 1;
611 : else
612 4809 : fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
613 438201 : if (agfl_count != 0 && fl_count != agfl_count)
614 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
615 :
616 : /* Do the incore counters match? */
617 876400 : if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
618 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
619 876398 : if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
620 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
621 876397 : if (xfs_has_lazysbcount(sc->mp) &&
622 438198 : pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
623 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
624 :
625 438199 : xchk_agf_xref(sc);
626 444624 : out:
627 444624 : 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 3660188 : xchk_agfl_block_xref(
649 : struct xfs_scrub *sc,
650 : xfs_agblock_t agbno)
651 : {
652 3660188 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
653 : return;
654 :
655 3660188 : xchk_xref_is_used_space(sc, agbno, 1);
656 3660192 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
657 3660190 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
658 3660191 : xchk_xref_is_not_shared(sc, agbno, 1);
659 3660192 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
660 : }
661 :
662 : /* Scrub an AGFL block. */
663 : STATIC int
664 3660188 : xchk_agfl_block(
665 : struct xfs_mount *mp,
666 : xfs_agblock_t agbno,
667 : void *priv)
668 : {
669 3660188 : struct xchk_agfl_info *sai = priv;
670 3660188 : struct xfs_scrub *sc = sai->sc;
671 :
672 3660188 : if (xfs_verify_agbno(sc->sa.pag, agbno) &&
673 3660188 : sai->nr_entries < sai->agflcount)
674 3660190 : sai->entries[sai->nr_entries++] = agbno;
675 : else
676 1 : xchk_block_set_corrupt(sc, sai->agfl_bp);
677 :
678 3660190 : xchk_agfl_block_xref(sc, agbno);
679 :
680 3660194 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
681 0 : return -ECANCELED;
682 :
683 : return 0;
684 : }
685 :
686 : static int
687 10689997 : xchk_agblock_cmp(
688 : const void *pa,
689 : const void *pb)
690 : {
691 10689997 : const xfs_agblock_t *a = pa;
692 10689997 : const xfs_agblock_t *b = pb;
693 :
694 10689997 : return (int)*a - (int)*b;
695 : }
696 :
697 : /* Cross-reference with the other btrees. */
698 : STATIC void
699 461151 : xchk_agfl_xref(
700 : struct xfs_scrub *sc)
701 : {
702 461151 : struct xfs_mount *mp = sc->mp;
703 461151 : xfs_agblock_t agbno;
704 :
705 461151 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
706 : return;
707 :
708 461151 : agbno = XFS_AGFL_BLOCK(mp);
709 :
710 461151 : xchk_ag_btcur_init(sc, &sc->sa);
711 :
712 461150 : xchk_xref_is_used_space(sc, agbno, 1);
713 461151 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
714 461151 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
715 461151 : xchk_xref_is_not_shared(sc, agbno, 1);
716 461150 : 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 467762 : xchk_agfl(
727 : struct xfs_scrub *sc)
728 : {
729 467762 : struct xchk_agfl_info sai = {
730 : .sc = sc,
731 : };
732 467762 : struct xfs_agf *agf;
733 467762 : xfs_agnumber_t agno = sc->sm->sm_agno;
734 467762 : unsigned int i;
735 467762 : int error;
736 :
737 : /* Lock the AGF and AGI so that nobody can touch this AG. */
738 467762 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
739 467762 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
740 6611 : return error;
741 461151 : if (!sc->sa.agf_bp)
742 : return -EFSCORRUPTED;
743 :
744 : /* Try to read the AGFL, and verify its structure if we get it. */
745 461151 : error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &sai.agfl_bp);
746 461151 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
747 0 : return error;
748 461151 : xchk_buffer_recheck(sc, sai.agfl_bp);
749 :
750 461151 : xchk_agfl_xref(sc);
751 :
752 461151 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
753 0 : goto out;
754 :
755 : /* Allocate buffer to ensure uniqueness of AGFL entries. */
756 461151 : agf = sc->sa.agf_bp->b_addr;
757 461151 : sai.agflcount = be32_to_cpu(agf->agf_flcount);
758 461151 : 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 461151 : sai.entries = kvcalloc(sai.agflcount, sizeof(xfs_agblock_t),
763 : XCHK_GFP_FLAGS);
764 461149 : if (!sai.entries) {
765 0 : error = -ENOMEM;
766 0 : goto out;
767 : }
768 :
769 : /* Check the blocks in the AGFL. */
770 461149 : error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr, sai.agfl_bp,
771 : xchk_agfl_block, &sai);
772 461151 : if (error == -ECANCELED) {
773 0 : error = 0;
774 0 : goto out_free;
775 : }
776 461151 : if (error)
777 0 : goto out_free;
778 :
779 461151 : 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 461151 : sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
786 : xchk_agblock_cmp, NULL);
787 4126142 : for (i = 1; i < sai.nr_entries; i++) {
788 3203842 : 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 461149 : out_free:
795 461149 : kvfree(sai.entries);
796 461150 : out:
797 461150 : return error;
798 : }
799 :
800 : /* AGI */
801 :
802 : /* Check agi_count/agi_freecount */
803 : static inline void
804 445067 : xchk_agi_xref_icounts(
805 : struct xfs_scrub *sc)
806 : {
807 445067 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
808 445067 : xfs_agino_t icount;
809 445067 : xfs_agino_t freecount;
810 445067 : int error;
811 :
812 445067 : if (!sc->sa.ino_cur)
813 0 : return;
814 :
815 445067 : error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
816 445066 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
817 : return;
818 890134 : if (be32_to_cpu(agi->agi_count) != icount ||
819 890130 : be32_to_cpu(agi->agi_freecount) != freecount)
820 1 : 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 445067 : xchk_agi_xref_fiblocks(
826 : struct xfs_scrub *sc)
827 : {
828 445067 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
829 445067 : xfs_agblock_t blocks;
830 445067 : int error = 0;
831 :
832 445067 : if (!xfs_has_inobtcounts(sc->mp))
833 44 : return;
834 :
835 445023 : if (sc->sa.ino_cur) {
836 445023 : error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
837 445023 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
838 : return;
839 890046 : if (blocks != be32_to_cpu(agi->agi_iblocks))
840 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
841 : }
842 :
843 445023 : if (sc->sa.fino_cur) {
844 445023 : error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
845 445023 : if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
846 : return;
847 890046 : 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 445066 : xchk_agi_xref(
855 : struct xfs_scrub *sc)
856 : {
857 445066 : struct xfs_mount *mp = sc->mp;
858 445066 : xfs_agblock_t agbno;
859 :
860 445066 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
861 : return;
862 :
863 445067 : agbno = XFS_AGI_BLOCK(mp);
864 :
865 445067 : xchk_ag_btcur_init(sc, &sc->sa);
866 :
867 445066 : xchk_xref_is_used_space(sc, agbno, 1);
868 445067 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
869 445067 : xchk_agi_xref_icounts(sc);
870 445064 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
871 445066 : xchk_xref_is_not_shared(sc, agbno, 1);
872 445067 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
873 445067 : 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 445139 : xchk_iunlink(
884 : struct xfs_scrub *sc,
885 : struct xfs_agi *agi)
886 : {
887 445139 : unsigned int i;
888 445139 : struct xfs_inode *ip;
889 :
890 28928924 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
891 28483858 : xfs_agino_t agino = be32_to_cpu(agi->agi_unlinked[i]);
892 :
893 28492076 : while (agino != NULLAGINO) {
894 8291 : if (agino % XFS_AGI_UNLINKED_BUCKETS != i) {
895 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
896 0 : return;
897 : }
898 :
899 8291 : ip = xfs_iunlink_lookup(sc->sa.pag, agino);
900 8218 : if (!ip) {
901 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
902 0 : return;
903 : }
904 :
905 8218 : if (!xfs_inode_on_unlinked_list(ip)) {
906 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
907 0 : return;
908 : }
909 :
910 8218 : agino = ip->i_next_unlinked;
911 : }
912 : }
913 : }
914 :
915 : /* Scrub the AGI. */
916 : int
917 452204 : xchk_agi(
918 : struct xfs_scrub *sc)
919 : {
920 452204 : struct xfs_mount *mp = sc->mp;
921 452204 : struct xfs_agi *agi;
922 452204 : struct xfs_perag *pag;
923 452204 : struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
924 452204 : xfs_agnumber_t agno = sc->sm->sm_agno;
925 452204 : xfs_agblock_t agbno;
926 452204 : xfs_agblock_t eoag;
927 452204 : xfs_agino_t agino;
928 452204 : xfs_agino_t first_agino;
929 452204 : xfs_agino_t last_agino;
930 452204 : xfs_agino_t icount;
931 452204 : int i;
932 452204 : int level;
933 452204 : int error = 0;
934 :
935 452204 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
936 452205 : if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
937 7138 : goto out;
938 445067 : xchk_buffer_recheck(sc, sc->sa.agi_bp);
939 :
940 445066 : agi = sc->sa.agi_bp->b_addr;
941 445066 : pag = sc->sa.pag;
942 :
943 : /* Check the AG length */
944 445066 : eoag = be32_to_cpu(agi->agi_length);
945 445066 : if (eoag != pag->block_count)
946 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
947 :
948 : /* Check btree roots and levels */
949 445067 : agbno = be32_to_cpu(agi->agi_root);
950 445067 : if (!xfs_verify_agbno(pag, agbno))
951 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
952 :
953 445067 : level = be32_to_cpu(agi->agi_level);
954 445067 : if (level <= 0 || level > igeo->inobt_maxlevels)
955 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
956 :
957 445066 : if (xfs_has_finobt(mp)) {
958 445062 : agbno = be32_to_cpu(agi->agi_free_root);
959 445062 : if (!xfs_verify_agbno(pag, agbno))
960 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
961 :
962 445061 : level = be32_to_cpu(agi->agi_free_level);
963 445061 : 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 445065 : xfs_agino_range(mp, agno, &first_agino, &last_agino);
969 445065 : icount = be32_to_cpu(agi->agi_count);
970 890130 : if (icount > last_agino - first_agino + 1 ||
971 445065 : icount < be32_to_cpu(agi->agi_freecount))
972 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
973 :
974 : /* Check inode pointers */
975 445061 : agino = be32_to_cpu(agi->agi_newino);
976 445061 : if (!xfs_verify_agino_or_null(pag, agino))
977 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
978 :
979 445066 : agino = be32_to_cpu(agi->agi_dirino);
980 445066 : if (!xfs_verify_agino_or_null(pag, agino))
981 1 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
982 :
983 : /* Check unlinked inode buckets */
984 28928944 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
985 28483878 : agino = be32_to_cpu(agi->agi_unlinked[i]);
986 28483878 : if (!xfs_verify_agino_or_null(pag, agino))
987 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
988 : }
989 :
990 445066 : 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 890132 : if (pag->pagi_count != be32_to_cpu(agi->agi_count))
995 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
996 890132 : if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
997 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
998 :
999 445066 : xchk_iunlink(sc, agi);
1000 :
1001 445066 : xchk_agi_xref(sc);
1002 452205 : out:
1003 452205 : return error;
1004 : }
|