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 1612818 : xchk_setup_agheader(
24 : struct xfs_scrub *sc)
25 : {
26 1612818 : if (xchk_need_intent_drain(sc))
27 20212 : xchk_fsgates_enable(sc, XCHK_FSGATES_DRAIN);
28 1612818 : return xchk_setup_fs(sc);
29 : }
30 :
31 : /* Superblock */
32 :
33 : /* Cross-reference with the other btrees. */
34 : STATIC void
35 444679 : xchk_superblock_xref(
36 : struct xfs_scrub *sc,
37 : struct xfs_buf *bp)
38 : {
39 444679 : struct xfs_mount *mp = sc->mp;
40 444679 : xfs_agnumber_t agno = sc->sm->sm_agno;
41 444679 : xfs_agblock_t agbno;
42 444679 : int error;
43 :
44 444679 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
45 2478 : return;
46 :
47 444679 : agbno = XFS_SB_BLOCK(mp);
48 :
49 444679 : error = xchk_ag_init_existing(sc, agno, &sc->sa);
50 444680 : if (!xchk_xref_process_error(sc, agno, agbno, &error))
51 : return;
52 :
53 442204 : xchk_xref_is_used_space(sc, agbno, 1);
54 442194 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
55 442207 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
56 442195 : xchk_xref_is_not_shared(sc, agbno, 1);
57 442198 : 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 502012 : xchk_superblock(
72 : struct xfs_scrub *sc)
73 : {
74 502012 : struct xfs_mount *mp = sc->mp;
75 502012 : struct xfs_buf *bp;
76 502012 : struct xfs_dsb *sb;
77 502012 : struct xfs_perag *pag;
78 502012 : xfs_agnumber_t agno;
79 502012 : uint32_t v2_ok;
80 502012 : __be32 features_mask;
81 502012 : int error;
82 502012 : __be16 vernum_mask;
83 :
84 502012 : agno = sc->sm->sm_agno;
85 502012 : 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 444699 : pag = xfs_perag_get(mp, agno);
94 444699 : if (!pag)
95 : return -ENOENT;
96 :
97 444699 : 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 444702 : switch (error) {
107 2 : case -EINVAL: /* also -EWRONGFS */
108 : case -ENOSYS:
109 : case -EFBIG:
110 2 : error = -EFSCORRUPTED;
111 444702 : fallthrough;
112 : default:
113 444702 : break;
114 : }
115 444702 : if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
116 18 : goto out_pag;
117 :
118 444683 : 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 889366 : if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
127 0 : xchk_block_set_corrupt(sc, bp);
128 :
129 444683 : if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
130 0 : xchk_block_set_corrupt(sc, bp);
131 :
132 444685 : if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
133 0 : xchk_block_set_corrupt(sc, bp);
134 :
135 444684 : if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
136 0 : xchk_block_set_corrupt(sc, bp);
137 :
138 444684 : if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
139 0 : xchk_block_set_preen(sc, bp);
140 :
141 444686 : if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
142 0 : xchk_block_set_corrupt(sc, bp);
143 :
144 444686 : if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
145 47631 : xchk_block_set_preen(sc, bp);
146 :
147 444684 : if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
148 136456 : xchk_block_set_preen(sc, bp);
149 :
150 444684 : if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
151 136456 : xchk_block_set_preen(sc, bp);
152 :
153 889366 : if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
154 0 : xchk_block_set_corrupt(sc, bp);
155 :
156 889366 : if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
157 0 : xchk_block_set_corrupt(sc, bp);
158 :
159 889370 : if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
160 0 : xchk_block_set_corrupt(sc, bp);
161 :
162 889370 : if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
163 0 : xchk_block_set_corrupt(sc, bp);
164 :
165 889372 : 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 444686 : 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 889372 : if ((sb->sb_versionnum & vernum_mask) !=
179 444686 : (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 444686 : vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
184 : XFS_SB_VERSION_NLINKBIT |
185 : XFS_SB_VERSION_QUOTABIT);
186 889372 : if ((sb->sb_versionnum & vernum_mask) !=
187 444686 : (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
188 69183 : xchk_block_set_preen(sc, bp);
189 :
190 444684 : if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
191 0 : xchk_block_set_corrupt(sc, bp);
192 :
193 444684 : if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
194 0 : xchk_block_set_corrupt(sc, bp);
195 :
196 444684 : if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
197 0 : xchk_block_set_corrupt(sc, bp);
198 :
199 889360 : if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
200 18 : xchk_block_set_preen(sc, bp);
201 :
202 444680 : if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
203 0 : xchk_block_set_corrupt(sc, bp);
204 :
205 444680 : if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
206 0 : xchk_block_set_corrupt(sc, bp);
207 :
208 444680 : if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
209 0 : xchk_block_set_corrupt(sc, bp);
210 :
211 444680 : if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
212 0 : xchk_block_set_corrupt(sc, bp);
213 :
214 444680 : if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
215 0 : xchk_block_set_corrupt(sc, bp);
216 :
217 444680 : if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
218 0 : xchk_block_set_corrupt(sc, bp);
219 :
220 444680 : 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 444680 : if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
229 136454 : xchk_block_set_preen(sc, bp);
230 :
231 444679 : if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
232 136455 : xchk_block_set_preen(sc, bp);
233 :
234 : /*
235 : * Skip the quota flags since repair will force quotacheck.
236 : * sb_qflags
237 : */
238 :
239 444679 : if (sb->sb_flags != mp->m_sb.sb_flags)
240 0 : xchk_block_set_corrupt(sc, bp);
241 :
242 444679 : if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
243 0 : xchk_block_set_corrupt(sc, bp);
244 :
245 889364 : if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
246 0 : xchk_block_set_corrupt(sc, bp);
247 :
248 889366 : if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
249 0 : xchk_block_set_preen(sc, bp);
250 :
251 889368 : if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
252 60 : xchk_block_set_preen(sc, bp);
253 :
254 444684 : if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
255 0 : xchk_block_set_corrupt(sc, bp);
256 :
257 444684 : if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
258 0 : xchk_block_set_corrupt(sc, bp);
259 :
260 444683 : if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
261 0 : xchk_block_set_corrupt(sc, bp);
262 :
263 889364 : 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 444683 : 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 444681 : v2_ok = XFS_SB_VERSION2_OKBITS;
272 444681 : if (xfs_sb_is_v5(&mp->m_sb))
273 444682 : v2_ok |= XFS_SB_VERSION2_CRCBIT;
274 :
275 889362 : if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
276 0 : xchk_block_set_corrupt(sc, bp);
277 :
278 444681 : 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 444681 : 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 444681 : if ((sb->sb_features2 & features_mask) !=
288 444681 : (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 444683 : features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
293 444683 : if ((sb->sb_features2 & features_mask) !=
294 444683 : (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
295 0 : xchk_block_set_preen(sc, bp);
296 :
297 444683 : if (!xfs_has_crc(mp)) {
298 : /* all v5 fields must be zero */
299 4 : 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 444681 : if (sb->sb_features_compat !=
306 444681 : 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 444682 : if (sb->sb_features_ro_compat !=
311 444682 : 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 444683 : features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_NEEDSREPAIR);
319 444683 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
320 444683 : sb->sb_features_incompat) & features_mask)
321 0 : xchk_block_set_preen(sc, bp);
322 :
323 : /* all other incompat features must match */
324 444683 : if ((cpu_to_be32(mp->m_sb.sb_features_incompat) ^
325 444683 : 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 444683 : if (sb->sb_features_log_incompat)
334 4832 : xchk_block_set_preen(sc, bp);
335 :
336 : /* Don't care about sb_crc */
337 :
338 889368 : if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
339 0 : xchk_block_set_corrupt(sc, bp);
340 :
341 444684 : if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
342 136455 : xchk_block_set_preen(sc, bp);
343 :
344 : /* Don't care about sb_lsn */
345 : }
346 :
347 444677 : if (xfs_has_metauuid(mp)) {
348 : /* The metadata UUID must be the same for all supers */
349 6 : 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 444678 : if (memchr_inv(sb + 1, 0,
355 444677 : BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
356 0 : xchk_block_set_corrupt(sc, bp);
357 :
358 444678 : xchk_superblock_xref(sc, bp);
359 444695 : out_pag:
360 444695 : xfs_perag_put(pag);
361 444696 : return error;
362 : }
363 :
364 : /* AGF */
365 :
366 : /* Tally freespace record lengths. */
367 : STATIC int
368 771687103 : xchk_agf_record_bno_lengths(
369 : struct xfs_btree_cur *cur,
370 : const struct xfs_alloc_rec_incore *rec,
371 : void *priv)
372 : {
373 771687103 : xfs_extlen_t *blocks = priv;
374 :
375 771687103 : (*blocks) += rec->ar_blockcount;
376 771687103 : return 0;
377 : }
378 :
379 : /* Check agf_freeblks */
380 : static inline void
381 355838 : xchk_agf_xref_freeblks(
382 : struct xfs_scrub *sc)
383 : {
384 355838 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
385 355838 : xfs_extlen_t blocks = 0;
386 355838 : int error;
387 :
388 355838 : if (!sc->sa.bno_cur)
389 0 : return;
390 :
391 355838 : error = xfs_alloc_query_all(sc->sa.bno_cur,
392 : xchk_agf_record_bno_lengths, &blocks);
393 355837 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
394 : return;
395 711676 : 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 355836 : xchk_agf_xref_cntbt(
402 : struct xfs_scrub *sc)
403 : {
404 355836 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
405 355836 : xfs_agblock_t agbno;
406 355836 : xfs_extlen_t blocks;
407 355836 : int have;
408 355836 : int error;
409 :
410 355836 : if (!sc->sa.cnt_cur)
411 0 : return;
412 :
413 : /* Any freespace at all? */
414 355836 : error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
415 355838 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
416 : return;
417 355835 : if (!have) {
418 0 : if (agf->agf_freeblks != cpu_to_be32(0))
419 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
420 0 : return;
421 : }
422 :
423 : /* Check agf_longest */
424 355835 : error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
425 355834 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
426 : return;
427 711674 : if (!have || blocks != be32_to_cpu(agf->agf_longest))
428 0 : 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 355836 : xchk_agf_xref_btreeblks(
434 : struct xfs_scrub *sc)
435 : {
436 355836 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
437 355836 : struct xfs_mount *mp = sc->mp;
438 355836 : xfs_agblock_t blocks;
439 355836 : xfs_agblock_t btreeblks;
440 355836 : int error;
441 :
442 : /* agf_btreeblks didn't exist before lazysbcount */
443 355836 : if (!xfs_has_lazysbcount(sc->mp))
444 0 : return;
445 :
446 : /* Check agf_rmap_blocks; set up for agf_btreeblks check */
447 355836 : if (sc->sa.rmap_cur) {
448 247428 : error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
449 247427 : if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
450 : return;
451 247428 : btreeblks = blocks - 1;
452 494856 : 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 355836 : if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
463 355836 : !sc->sa.bno_cur || !sc->sa.cnt_cur)
464 : return;
465 :
466 : /* Check agf_btreeblks */
467 355836 : error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
468 355836 : if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
469 : return;
470 355837 : btreeblks += blocks - 1;
471 :
472 355837 : error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
473 355837 : if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
474 : return;
475 355838 : btreeblks += blocks - 1;
476 :
477 711676 : 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 355838 : xchk_agf_xref_refcblks(
484 : struct xfs_scrub *sc)
485 : {
486 355838 : struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
487 355838 : xfs_agblock_t blocks;
488 355838 : int error;
489 :
490 355838 : if (!sc->sa.refc_cur)
491 108418 : return;
492 :
493 247420 : error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
494 247420 : if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
495 : return;
496 494840 : 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 355834 : xchk_agf_xref(
503 : struct xfs_scrub *sc)
504 : {
505 355834 : struct xfs_mount *mp = sc->mp;
506 355834 : xfs_agblock_t agbno;
507 :
508 355834 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
509 : return;
510 :
511 355835 : agbno = XFS_AGF_BLOCK(mp);
512 :
513 355835 : xchk_ag_btcur_init(sc, &sc->sa);
514 :
515 355833 : xchk_xref_is_used_space(sc, agbno, 1);
516 355838 : xchk_agf_xref_freeblks(sc);
517 355837 : xchk_agf_xref_cntbt(sc);
518 355834 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
519 355838 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
520 355838 : xchk_agf_xref_btreeblks(sc);
521 355836 : xchk_xref_is_not_shared(sc, agbno, 1);
522 355837 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
523 355838 : 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 361838 : xchk_agf(
531 : struct xfs_scrub *sc)
532 : {
533 361838 : struct xfs_mount *mp = sc->mp;
534 361838 : struct xfs_agf *agf;
535 361838 : struct xfs_perag *pag;
536 361838 : xfs_agnumber_t agno = sc->sm->sm_agno;
537 361838 : xfs_agblock_t agbno;
538 361838 : xfs_agblock_t eoag;
539 361838 : xfs_agblock_t agfl_first;
540 361838 : xfs_agblock_t agfl_last;
541 361838 : xfs_agblock_t agfl_count;
542 361838 : xfs_agblock_t fl_count;
543 361838 : int level;
544 361838 : int error = 0;
545 :
546 361838 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
547 361844 : if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
548 6006 : goto out;
549 355837 : xchk_buffer_recheck(sc, sc->sa.agf_bp);
550 :
551 355833 : agf = sc->sa.agf_bp->b_addr;
552 355833 : pag = sc->sa.pag;
553 :
554 : /* Check the AG length */
555 355833 : eoag = be32_to_cpu(agf->agf_length);
556 355833 : 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 355833 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
561 355833 : if (!xfs_verify_agbno(pag, agbno))
562 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
563 :
564 355832 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
565 355832 : if (!xfs_verify_agbno(pag, agbno))
566 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
567 :
568 355829 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
569 355829 : if (level <= 0 || level > mp->m_alloc_maxlevels)
570 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
571 :
572 355832 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
573 355832 : if (level <= 0 || level > mp->m_alloc_maxlevels)
574 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
575 :
576 355831 : if (xfs_has_rmapbt(mp)) {
577 247425 : agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
578 247425 : if (!xfs_verify_agbno(pag, agbno))
579 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
580 :
581 247425 : level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
582 247425 : if (level <= 0 || level > mp->m_rmap_maxlevels)
583 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
584 : }
585 :
586 355831 : if (xfs_has_reflink(mp)) {
587 247416 : agbno = be32_to_cpu(agf->agf_refcount_root);
588 247416 : if (!xfs_verify_agbno(pag, agbno))
589 3 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
590 :
591 247419 : level = be32_to_cpu(agf->agf_refcount_level);
592 247419 : 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 355834 : agfl_first = be32_to_cpu(agf->agf_flfirst);
598 355834 : agfl_last = be32_to_cpu(agf->agf_fllast);
599 355834 : agfl_count = be32_to_cpu(agf->agf_flcount);
600 355834 : if (agfl_last > agfl_first)
601 351026 : fl_count = agfl_last - agfl_first + 1;
602 : else
603 4808 : fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
604 355834 : 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 711668 : if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
609 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
610 711664 : if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
611 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
612 711662 : if (xfs_has_lazysbcount(sc->mp) &&
613 355830 : pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
614 0 : xchk_block_set_corrupt(sc, sc->sa.agf_bp);
615 :
616 355832 : xchk_agf_xref(sc);
617 361842 : out:
618 361842 : 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 2511828 : xchk_agfl_block_xref(
640 : struct xfs_scrub *sc,
641 : xfs_agblock_t agbno)
642 : {
643 2511828 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
644 : return;
645 :
646 2511826 : xchk_xref_is_used_space(sc, agbno, 1);
647 2511836 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
648 2511831 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
649 2511836 : xchk_xref_is_not_shared(sc, agbno, 1);
650 2511837 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
651 : }
652 :
653 : /* Scrub an AGFL block. */
654 : STATIC int
655 2511829 : xchk_agfl_block(
656 : struct xfs_mount *mp,
657 : xfs_agblock_t agbno,
658 : void *priv)
659 : {
660 2511829 : struct xchk_agfl_info *sai = priv;
661 2511829 : struct xfs_scrub *sc = sai->sc;
662 :
663 2511829 : if (xfs_verify_agbno(sc->sa.pag, agbno) &&
664 2511829 : sai->nr_entries < sai->agflcount)
665 2511829 : sai->entries[sai->nr_entries++] = agbno;
666 : else
667 0 : xchk_block_set_corrupt(sc, sai->agfl_bp);
668 :
669 2511829 : xchk_agfl_block_xref(sc, agbno);
670 :
671 2511836 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
672 0 : return -ECANCELED;
673 :
674 : return 0;
675 : }
676 :
677 : static int
678 7086109 : xchk_agblock_cmp(
679 : const void *pa,
680 : const void *pb)
681 : {
682 7086109 : const xfs_agblock_t *a = pa;
683 7086109 : const xfs_agblock_t *b = pb;
684 :
685 7086109 : return (int)*a - (int)*b;
686 : }
687 :
688 : /* Cross-reference with the other btrees. */
689 : STATIC void
690 347554 : xchk_agfl_xref(
691 : struct xfs_scrub *sc)
692 : {
693 347554 : struct xfs_mount *mp = sc->mp;
694 347554 : xfs_agblock_t agbno;
695 :
696 347554 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
697 : return;
698 :
699 347554 : agbno = XFS_AGFL_BLOCK(mp);
700 :
701 347554 : xchk_ag_btcur_init(sc, &sc->sa);
702 :
703 347552 : xchk_xref_is_used_space(sc, agbno, 1);
704 347554 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
705 347554 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
706 347554 : xchk_xref_is_not_shared(sc, agbno, 1);
707 347553 : 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 353427 : xchk_agfl(
718 : struct xfs_scrub *sc)
719 : {
720 353427 : struct xchk_agfl_info sai = {
721 : .sc = sc,
722 : };
723 353427 : struct xfs_agf *agf;
724 353427 : xfs_agnumber_t agno = sc->sm->sm_agno;
725 353427 : unsigned int i;
726 353427 : int error;
727 :
728 : /* Lock the AGF and AGI so that nobody can touch this AG. */
729 353427 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
730 353428 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
731 5874 : return error;
732 347554 : if (!sc->sa.agf_bp)
733 : return -EFSCORRUPTED;
734 :
735 : /* Try to read the AGFL, and verify its structure if we get it. */
736 347554 : error = xfs_alloc_read_agfl(sc->sa.pag, sc->tp, &sai.agfl_bp);
737 347554 : if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
738 0 : return error;
739 347554 : xchk_buffer_recheck(sc, sai.agfl_bp);
740 :
741 347553 : xchk_agfl_xref(sc);
742 :
743 347554 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
744 0 : goto out;
745 :
746 : /* Allocate buffer to ensure uniqueness of AGFL entries. */
747 347554 : agf = sc->sa.agf_bp->b_addr;
748 347554 : sai.agflcount = be32_to_cpu(agf->agf_flcount);
749 347554 : 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 347553 : sai.entries = kvcalloc(sai.agflcount, sizeof(xfs_agblock_t),
754 : XCHK_GFP_FLAGS);
755 347553 : if (!sai.entries) {
756 0 : error = -ENOMEM;
757 0 : goto out;
758 : }
759 :
760 : /* Check the blocks in the AGFL. */
761 347553 : error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr, sai.agfl_bp,
762 : xchk_agfl_block, &sai);
763 347554 : if (error == -ECANCELED) {
764 0 : error = 0;
765 0 : goto out_free;
766 : }
767 347554 : if (error)
768 0 : goto out_free;
769 :
770 347554 : 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 347554 : sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
777 : xchk_agblock_cmp, NULL);
778 2864199 : for (i = 1; i < sai.nr_entries; i++) {
779 2169091 : 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 347554 : out_free:
786 347554 : kvfree(sai.entries);
787 347554 : out:
788 347554 : return error;
789 : }
790 :
791 : /* AGI */
792 :
793 : /* Check agi_count/agi_freecount */
794 : static inline void
795 388794 : xchk_agi_xref_icounts(
796 : struct xfs_scrub *sc)
797 : {
798 388794 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
799 388794 : xfs_agino_t icount;
800 388794 : xfs_agino_t freecount;
801 388794 : int error;
802 :
803 388794 : if (!sc->sa.ino_cur)
804 0 : return;
805 :
806 388794 : error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
807 388794 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
808 : return;
809 777588 : if (be32_to_cpu(agi->agi_count) != icount ||
810 777588 : be32_to_cpu(agi->agi_freecount) != freecount)
811 0 : 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 388794 : xchk_agi_xref_fiblocks(
817 : struct xfs_scrub *sc)
818 : {
819 388794 : struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
820 388794 : xfs_agblock_t blocks;
821 388794 : int error = 0;
822 :
823 388794 : if (!xfs_has_inobtcounts(sc->mp))
824 44 : return;
825 :
826 388750 : if (sc->sa.ino_cur) {
827 388750 : error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
828 388750 : if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
829 : return;
830 777500 : if (blocks != be32_to_cpu(agi->agi_iblocks))
831 0 : xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
832 : }
833 :
834 388750 : if (sc->sa.fino_cur) {
835 388750 : error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
836 388749 : if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
837 : return;
838 777500 : 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 388793 : xchk_agi_xref(
846 : struct xfs_scrub *sc)
847 : {
848 388793 : struct xfs_mount *mp = sc->mp;
849 388793 : xfs_agblock_t agbno;
850 :
851 388793 : if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
852 : return;
853 :
854 388793 : agbno = XFS_AGI_BLOCK(mp);
855 :
856 388793 : xchk_ag_btcur_init(sc, &sc->sa);
857 :
858 388792 : xchk_xref_is_used_space(sc, agbno, 1);
859 388794 : xchk_xref_is_not_inode_chunk(sc, agbno, 1);
860 388794 : xchk_agi_xref_icounts(sc);
861 388794 : xchk_xref_is_only_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
862 388793 : xchk_xref_is_not_shared(sc, agbno, 1);
863 388794 : xchk_xref_is_not_cow_staging(sc, agbno, 1);
864 388794 : 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 388784 : xchk_iunlink(
875 : struct xfs_scrub *sc,
876 : struct xfs_agi *agi)
877 : {
878 388784 : unsigned int i;
879 388784 : struct xfs_inode *ip;
880 :
881 25271226 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
882 24882434 : xfs_agino_t agino = be32_to_cpu(agi->agi_unlinked[i]);
883 :
884 24887304 : while (agino != NULLAGINO) {
885 4862 : if (agino % XFS_AGI_UNLINKED_BUCKETS != i) {
886 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
887 0 : return;
888 : }
889 :
890 4862 : ip = xfs_iunlink_lookup(sc->sa.pag, agino);
891 4870 : if (!ip) {
892 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
893 0 : return;
894 : }
895 :
896 4870 : if (!xfs_inode_on_unlinked_list(ip)) {
897 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
898 0 : return;
899 : }
900 :
901 4870 : agino = ip->i_next_unlinked;
902 : }
903 : }
904 : }
905 :
906 : /* Scrub the AGI. */
907 : int
908 395571 : xchk_agi(
909 : struct xfs_scrub *sc)
910 : {
911 395571 : struct xfs_mount *mp = sc->mp;
912 395571 : struct xfs_agi *agi;
913 395571 : struct xfs_perag *pag;
914 395571 : struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
915 395571 : xfs_agnumber_t agno = sc->sm->sm_agno;
916 395571 : xfs_agblock_t agbno;
917 395571 : xfs_agblock_t eoag;
918 395571 : xfs_agino_t agino;
919 395571 : xfs_agino_t first_agino;
920 395571 : xfs_agino_t last_agino;
921 395571 : xfs_agino_t icount;
922 395571 : int i;
923 395571 : int level;
924 395571 : int error = 0;
925 :
926 395571 : error = xchk_ag_read_headers(sc, agno, &sc->sa);
927 395571 : if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
928 6777 : goto out;
929 388794 : xchk_buffer_recheck(sc, sc->sa.agi_bp);
930 :
931 388794 : agi = sc->sa.agi_bp->b_addr;
932 388794 : pag = sc->sa.pag;
933 :
934 : /* Check the AG length */
935 388794 : eoag = be32_to_cpu(agi->agi_length);
936 388794 : if (eoag != pag->block_count)
937 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
938 :
939 : /* Check btree roots and levels */
940 388794 : agbno = be32_to_cpu(agi->agi_root);
941 388794 : if (!xfs_verify_agbno(pag, agbno))
942 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
943 :
944 388793 : level = be32_to_cpu(agi->agi_level);
945 388793 : if (level <= 0 || level > igeo->inobt_maxlevels)
946 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
947 :
948 388794 : if (xfs_has_finobt(mp)) {
949 388790 : agbno = be32_to_cpu(agi->agi_free_root);
950 388790 : if (!xfs_verify_agbno(pag, agbno))
951 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
952 :
953 388789 : level = be32_to_cpu(agi->agi_free_level);
954 388789 : 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 388793 : xfs_agino_range(mp, agno, &first_agino, &last_agino);
960 388788 : icount = be32_to_cpu(agi->agi_count);
961 777576 : if (icount > last_agino - first_agino + 1 ||
962 388788 : icount < be32_to_cpu(agi->agi_freecount))
963 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
964 :
965 : /* Check inode pointers */
966 388792 : agino = be32_to_cpu(agi->agi_newino);
967 388792 : if (!xfs_verify_agino_or_null(pag, agino))
968 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
969 :
970 388791 : agino = be32_to_cpu(agi->agi_dirino);
971 388791 : if (!xfs_verify_agino_or_null(pag, agino))
972 2 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
973 :
974 : /* Check unlinked inode buckets */
975 25271145 : for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
976 24882352 : agino = be32_to_cpu(agi->agi_unlinked[i]);
977 24882352 : if (!xfs_verify_agino_or_null(pag, agino))
978 19 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
979 : }
980 :
981 388793 : 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 777586 : if (pag->pagi_count != be32_to_cpu(agi->agi_count))
986 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
987 777584 : if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
988 0 : xchk_block_set_corrupt(sc, sc->sa.agi_bp);
989 :
990 388792 : xchk_iunlink(sc, agi);
991 :
992 388793 : xchk_agi_xref(sc);
993 395570 : out:
994 395570 : return error;
995 : }
|