1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
14 #include "xfs_alloc.h"
15 #include "xfs_ialloc.h"
17 #include "scrub/scrub.h"
18 #include "scrub/common.h"
22 /* Cross-reference with the other btrees. */
28 struct xfs_mount
*mp
= sc
->mp
;
29 xfs_agnumber_t agno
= sc
->sm
->sm_agno
;
33 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
36 agbno
= XFS_SB_BLOCK(mp
);
38 error
= xchk_ag_init(sc
, agno
, &sc
->sa
);
39 if (!xchk_xref_process_error(sc
, agno
, agbno
, &error
))
42 xchk_xref_is_used_space(sc
, agbno
, 1);
43 xchk_xref_is_not_inode_chunk(sc
, agbno
, 1);
44 xchk_xref_is_owned_by(sc
, agbno
, 1, &XFS_RMAP_OINFO_FS
);
45 xchk_xref_is_not_shared(sc
, agbno
, 1);
47 /* scrub teardown will take care of sc->sa for us */
51 * Scrub the filesystem superblock.
53 * Note: We do /not/ attempt to check AG 0's superblock. Mount is
54 * responsible for validating all the geometry information in sb 0, so
55 * if the filesystem is capable of initiating online scrub, then clearly
56 * sb 0 is ok and we can use its information to check everything else.
62 struct xfs_mount
*mp
= sc
->mp
;
71 agno
= sc
->sm
->sm_agno
;
75 error
= xfs_sb_read_secondary(mp
, sc
->tp
, agno
, &bp
);
77 * The superblock verifier can return several different error codes
78 * if it thinks the superblock doesn't look right. For a mount these
79 * would all get bounced back to userspace, but if we're here then the
80 * fs mounted successfully, which means that this secondary superblock
81 * is simply incorrect. Treat all these codes the same way we treat
85 case -EINVAL
: /* also -EWRONGFS */
88 error
= -EFSCORRUPTED
;
92 if (!xchk_process_error(sc
, agno
, XFS_SB_BLOCK(mp
), &error
))
98 * Verify the geometries match. Fields that are permanently
99 * set by mkfs are checked; fields that can be updated later
100 * (and are not propagated to backup superblocks) are preen
103 if (sb
->sb_blocksize
!= cpu_to_be32(mp
->m_sb
.sb_blocksize
))
104 xchk_block_set_corrupt(sc
, bp
);
106 if (sb
->sb_dblocks
!= cpu_to_be64(mp
->m_sb
.sb_dblocks
))
107 xchk_block_set_corrupt(sc
, bp
);
109 if (sb
->sb_rblocks
!= cpu_to_be64(mp
->m_sb
.sb_rblocks
))
110 xchk_block_set_corrupt(sc
, bp
);
112 if (sb
->sb_rextents
!= cpu_to_be64(mp
->m_sb
.sb_rextents
))
113 xchk_block_set_corrupt(sc
, bp
);
115 if (!uuid_equal(&sb
->sb_uuid
, &mp
->m_sb
.sb_uuid
))
116 xchk_block_set_preen(sc
, bp
);
118 if (sb
->sb_logstart
!= cpu_to_be64(mp
->m_sb
.sb_logstart
))
119 xchk_block_set_corrupt(sc
, bp
);
121 if (sb
->sb_rootino
!= cpu_to_be64(mp
->m_sb
.sb_rootino
))
122 xchk_block_set_preen(sc
, bp
);
124 if (sb
->sb_rbmino
!= cpu_to_be64(mp
->m_sb
.sb_rbmino
))
125 xchk_block_set_preen(sc
, bp
);
127 if (sb
->sb_rsumino
!= cpu_to_be64(mp
->m_sb
.sb_rsumino
))
128 xchk_block_set_preen(sc
, bp
);
130 if (sb
->sb_rextsize
!= cpu_to_be32(mp
->m_sb
.sb_rextsize
))
131 xchk_block_set_corrupt(sc
, bp
);
133 if (sb
->sb_agblocks
!= cpu_to_be32(mp
->m_sb
.sb_agblocks
))
134 xchk_block_set_corrupt(sc
, bp
);
136 if (sb
->sb_agcount
!= cpu_to_be32(mp
->m_sb
.sb_agcount
))
137 xchk_block_set_corrupt(sc
, bp
);
139 if (sb
->sb_rbmblocks
!= cpu_to_be32(mp
->m_sb
.sb_rbmblocks
))
140 xchk_block_set_corrupt(sc
, bp
);
142 if (sb
->sb_logblocks
!= cpu_to_be32(mp
->m_sb
.sb_logblocks
))
143 xchk_block_set_corrupt(sc
, bp
);
145 /* Check sb_versionnum bits that are set at mkfs time. */
146 vernum_mask
= cpu_to_be16(~XFS_SB_VERSION_OKBITS
|
147 XFS_SB_VERSION_NUMBITS
|
148 XFS_SB_VERSION_ALIGNBIT
|
149 XFS_SB_VERSION_DALIGNBIT
|
150 XFS_SB_VERSION_SHAREDBIT
|
151 XFS_SB_VERSION_LOGV2BIT
|
152 XFS_SB_VERSION_SECTORBIT
|
153 XFS_SB_VERSION_EXTFLGBIT
|
154 XFS_SB_VERSION_DIRV2BIT
);
155 if ((sb
->sb_versionnum
& vernum_mask
) !=
156 (cpu_to_be16(mp
->m_sb
.sb_versionnum
) & vernum_mask
))
157 xchk_block_set_corrupt(sc
, bp
);
159 /* Check sb_versionnum bits that can be set after mkfs time. */
160 vernum_mask
= cpu_to_be16(XFS_SB_VERSION_ATTRBIT
|
161 XFS_SB_VERSION_NLINKBIT
|
162 XFS_SB_VERSION_QUOTABIT
);
163 if ((sb
->sb_versionnum
& vernum_mask
) !=
164 (cpu_to_be16(mp
->m_sb
.sb_versionnum
) & vernum_mask
))
165 xchk_block_set_preen(sc
, bp
);
167 if (sb
->sb_sectsize
!= cpu_to_be16(mp
->m_sb
.sb_sectsize
))
168 xchk_block_set_corrupt(sc
, bp
);
170 if (sb
->sb_inodesize
!= cpu_to_be16(mp
->m_sb
.sb_inodesize
))
171 xchk_block_set_corrupt(sc
, bp
);
173 if (sb
->sb_inopblock
!= cpu_to_be16(mp
->m_sb
.sb_inopblock
))
174 xchk_block_set_corrupt(sc
, bp
);
176 if (memcmp(sb
->sb_fname
, mp
->m_sb
.sb_fname
, sizeof(sb
->sb_fname
)))
177 xchk_block_set_preen(sc
, bp
);
179 if (sb
->sb_blocklog
!= mp
->m_sb
.sb_blocklog
)
180 xchk_block_set_corrupt(sc
, bp
);
182 if (sb
->sb_sectlog
!= mp
->m_sb
.sb_sectlog
)
183 xchk_block_set_corrupt(sc
, bp
);
185 if (sb
->sb_inodelog
!= mp
->m_sb
.sb_inodelog
)
186 xchk_block_set_corrupt(sc
, bp
);
188 if (sb
->sb_inopblog
!= mp
->m_sb
.sb_inopblog
)
189 xchk_block_set_corrupt(sc
, bp
);
191 if (sb
->sb_agblklog
!= mp
->m_sb
.sb_agblklog
)
192 xchk_block_set_corrupt(sc
, bp
);
194 if (sb
->sb_rextslog
!= mp
->m_sb
.sb_rextslog
)
195 xchk_block_set_corrupt(sc
, bp
);
197 if (sb
->sb_imax_pct
!= mp
->m_sb
.sb_imax_pct
)
198 xchk_block_set_preen(sc
, bp
);
201 * Skip the summary counters since we track them in memory anyway.
202 * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
205 if (sb
->sb_uquotino
!= cpu_to_be64(mp
->m_sb
.sb_uquotino
))
206 xchk_block_set_preen(sc
, bp
);
208 if (sb
->sb_gquotino
!= cpu_to_be64(mp
->m_sb
.sb_gquotino
))
209 xchk_block_set_preen(sc
, bp
);
212 * Skip the quota flags since repair will force quotacheck.
216 if (sb
->sb_flags
!= mp
->m_sb
.sb_flags
)
217 xchk_block_set_corrupt(sc
, bp
);
219 if (sb
->sb_shared_vn
!= mp
->m_sb
.sb_shared_vn
)
220 xchk_block_set_corrupt(sc
, bp
);
222 if (sb
->sb_inoalignmt
!= cpu_to_be32(mp
->m_sb
.sb_inoalignmt
))
223 xchk_block_set_corrupt(sc
, bp
);
225 if (sb
->sb_unit
!= cpu_to_be32(mp
->m_sb
.sb_unit
))
226 xchk_block_set_preen(sc
, bp
);
228 if (sb
->sb_width
!= cpu_to_be32(mp
->m_sb
.sb_width
))
229 xchk_block_set_preen(sc
, bp
);
231 if (sb
->sb_dirblklog
!= mp
->m_sb
.sb_dirblklog
)
232 xchk_block_set_corrupt(sc
, bp
);
234 if (sb
->sb_logsectlog
!= mp
->m_sb
.sb_logsectlog
)
235 xchk_block_set_corrupt(sc
, bp
);
237 if (sb
->sb_logsectsize
!= cpu_to_be16(mp
->m_sb
.sb_logsectsize
))
238 xchk_block_set_corrupt(sc
, bp
);
240 if (sb
->sb_logsunit
!= cpu_to_be32(mp
->m_sb
.sb_logsunit
))
241 xchk_block_set_corrupt(sc
, bp
);
243 /* Do we see any invalid bits in sb_features2? */
244 if (!xfs_sb_version_hasmorebits(&mp
->m_sb
)) {
245 if (sb
->sb_features2
!= 0)
246 xchk_block_set_corrupt(sc
, bp
);
248 v2_ok
= XFS_SB_VERSION2_OKBITS
;
249 if (XFS_SB_VERSION_NUM(&mp
->m_sb
) >= XFS_SB_VERSION_5
)
250 v2_ok
|= XFS_SB_VERSION2_CRCBIT
;
252 if (!!(sb
->sb_features2
& cpu_to_be32(~v2_ok
)))
253 xchk_block_set_corrupt(sc
, bp
);
255 if (sb
->sb_features2
!= sb
->sb_bad_features2
)
256 xchk_block_set_preen(sc
, bp
);
259 /* Check sb_features2 flags that are set at mkfs time. */
260 features_mask
= cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT
|
261 XFS_SB_VERSION2_PROJID32BIT
|
262 XFS_SB_VERSION2_CRCBIT
|
263 XFS_SB_VERSION2_FTYPE
);
264 if ((sb
->sb_features2
& features_mask
) !=
265 (cpu_to_be32(mp
->m_sb
.sb_features2
) & features_mask
))
266 xchk_block_set_corrupt(sc
, bp
);
268 /* Check sb_features2 flags that can be set after mkfs time. */
269 features_mask
= cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT
);
270 if ((sb
->sb_features2
& features_mask
) !=
271 (cpu_to_be32(mp
->m_sb
.sb_features2
) & features_mask
))
272 xchk_block_set_corrupt(sc
, bp
);
274 if (!xfs_sb_version_hascrc(&mp
->m_sb
)) {
275 /* all v5 fields must be zero */
276 if (memchr_inv(&sb
->sb_features_compat
, 0,
277 sizeof(struct xfs_dsb
) -
278 offsetof(struct xfs_dsb
, sb_features_compat
)))
279 xchk_block_set_corrupt(sc
, bp
);
281 /* Check compat flags; all are set at mkfs time. */
282 features_mask
= cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN
);
283 if ((sb
->sb_features_compat
& features_mask
) !=
284 (cpu_to_be32(mp
->m_sb
.sb_features_compat
) & features_mask
))
285 xchk_block_set_corrupt(sc
, bp
);
287 /* Check ro compat flags; all are set at mkfs time. */
288 features_mask
= cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN
|
289 XFS_SB_FEAT_RO_COMPAT_FINOBT
|
290 XFS_SB_FEAT_RO_COMPAT_RMAPBT
|
291 XFS_SB_FEAT_RO_COMPAT_REFLINK
);
292 if ((sb
->sb_features_ro_compat
& features_mask
) !=
293 (cpu_to_be32(mp
->m_sb
.sb_features_ro_compat
) &
295 xchk_block_set_corrupt(sc
, bp
);
297 /* Check incompat flags; all are set at mkfs time. */
298 features_mask
= cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN
|
299 XFS_SB_FEAT_INCOMPAT_FTYPE
|
300 XFS_SB_FEAT_INCOMPAT_SPINODES
|
301 XFS_SB_FEAT_INCOMPAT_META_UUID
);
302 if ((sb
->sb_features_incompat
& features_mask
) !=
303 (cpu_to_be32(mp
->m_sb
.sb_features_incompat
) &
305 xchk_block_set_corrupt(sc
, bp
);
307 /* Check log incompat flags; all are set at mkfs time. */
308 features_mask
= cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
);
309 if ((sb
->sb_features_log_incompat
& features_mask
) !=
310 (cpu_to_be32(mp
->m_sb
.sb_features_log_incompat
) &
312 xchk_block_set_corrupt(sc
, bp
);
314 /* Don't care about sb_crc */
316 if (sb
->sb_spino_align
!= cpu_to_be32(mp
->m_sb
.sb_spino_align
))
317 xchk_block_set_corrupt(sc
, bp
);
319 if (sb
->sb_pquotino
!= cpu_to_be64(mp
->m_sb
.sb_pquotino
))
320 xchk_block_set_preen(sc
, bp
);
322 /* Don't care about sb_lsn */
325 if (xfs_sb_version_hasmetauuid(&mp
->m_sb
)) {
326 /* The metadata UUID must be the same for all supers */
327 if (!uuid_equal(&sb
->sb_meta_uuid
, &mp
->m_sb
.sb_meta_uuid
))
328 xchk_block_set_corrupt(sc
, bp
);
331 /* Everything else must be zero. */
332 if (memchr_inv(sb
+ 1, 0,
333 BBTOB(bp
->b_length
) - sizeof(struct xfs_dsb
)))
334 xchk_block_set_corrupt(sc
, bp
);
336 xchk_superblock_xref(sc
, bp
);
343 /* Tally freespace record lengths. */
345 xchk_agf_record_bno_lengths(
346 struct xfs_btree_cur
*cur
,
347 struct xfs_alloc_rec_incore
*rec
,
350 xfs_extlen_t
*blocks
= priv
;
352 (*blocks
) += rec
->ar_blockcount
;
356 /* Check agf_freeblks */
358 xchk_agf_xref_freeblks(
359 struct xfs_scrub
*sc
)
361 struct xfs_agf
*agf
= sc
->sa
.agf_bp
->b_addr
;
362 xfs_extlen_t blocks
= 0;
368 error
= xfs_alloc_query_all(sc
->sa
.bno_cur
,
369 xchk_agf_record_bno_lengths
, &blocks
);
370 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.bno_cur
))
372 if (blocks
!= be32_to_cpu(agf
->agf_freeblks
))
373 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
376 /* Cross reference the AGF with the cntbt (freespace by length btree) */
379 struct xfs_scrub
*sc
)
381 struct xfs_agf
*agf
= sc
->sa
.agf_bp
->b_addr
;
390 /* Any freespace at all? */
391 error
= xfs_alloc_lookup_le(sc
->sa
.cnt_cur
, 0, -1U, &have
);
392 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
395 if (agf
->agf_freeblks
!= cpu_to_be32(0))
396 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
400 /* Check agf_longest */
401 error
= xfs_alloc_get_rec(sc
->sa
.cnt_cur
, &agbno
, &blocks
, &have
);
402 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
404 if (!have
|| blocks
!= be32_to_cpu(agf
->agf_longest
))
405 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
408 /* Check the btree block counts in the AGF against the btrees. */
410 xchk_agf_xref_btreeblks(
411 struct xfs_scrub
*sc
)
413 struct xfs_agf
*agf
= sc
->sa
.agf_bp
->b_addr
;
414 struct xfs_mount
*mp
= sc
->mp
;
415 xfs_agblock_t blocks
;
416 xfs_agblock_t btreeblks
;
419 /* Check agf_rmap_blocks; set up for agf_btreeblks check */
420 if (sc
->sa
.rmap_cur
) {
421 error
= xfs_btree_count_blocks(sc
->sa
.rmap_cur
, &blocks
);
422 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.rmap_cur
))
424 btreeblks
= blocks
- 1;
425 if (blocks
!= be32_to_cpu(agf
->agf_rmap_blocks
))
426 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
432 * No rmap cursor; we can't xref if we have the rmapbt feature.
433 * We also can't do it if we're missing the free space btree cursors.
435 if ((xfs_sb_version_hasrmapbt(&mp
->m_sb
) && !sc
->sa
.rmap_cur
) ||
436 !sc
->sa
.bno_cur
|| !sc
->sa
.cnt_cur
)
439 /* Check agf_btreeblks */
440 error
= xfs_btree_count_blocks(sc
->sa
.bno_cur
, &blocks
);
441 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.bno_cur
))
443 btreeblks
+= blocks
- 1;
445 error
= xfs_btree_count_blocks(sc
->sa
.cnt_cur
, &blocks
);
446 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
448 btreeblks
+= blocks
- 1;
450 if (btreeblks
!= be32_to_cpu(agf
->agf_btreeblks
))
451 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
454 /* Check agf_refcount_blocks against tree size */
456 xchk_agf_xref_refcblks(
457 struct xfs_scrub
*sc
)
459 struct xfs_agf
*agf
= sc
->sa
.agf_bp
->b_addr
;
460 xfs_agblock_t blocks
;
463 if (!sc
->sa
.refc_cur
)
466 error
= xfs_btree_count_blocks(sc
->sa
.refc_cur
, &blocks
);
467 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.refc_cur
))
469 if (blocks
!= be32_to_cpu(agf
->agf_refcount_blocks
))
470 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
473 /* Cross-reference with the other btrees. */
476 struct xfs_scrub
*sc
)
478 struct xfs_mount
*mp
= sc
->mp
;
482 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
485 agbno
= XFS_AGF_BLOCK(mp
);
487 error
= xchk_ag_btcur_init(sc
, &sc
->sa
);
491 xchk_xref_is_used_space(sc
, agbno
, 1);
492 xchk_agf_xref_freeblks(sc
);
493 xchk_agf_xref_cntbt(sc
);
494 xchk_xref_is_not_inode_chunk(sc
, agbno
, 1);
495 xchk_xref_is_owned_by(sc
, agbno
, 1, &XFS_RMAP_OINFO_FS
);
496 xchk_agf_xref_btreeblks(sc
);
497 xchk_xref_is_not_shared(sc
, agbno
, 1);
498 xchk_agf_xref_refcblks(sc
);
500 /* scrub teardown will take care of sc->sa for us */
506 struct xfs_scrub
*sc
)
508 struct xfs_mount
*mp
= sc
->mp
;
510 struct xfs_perag
*pag
;
514 xfs_agblock_t agfl_first
;
515 xfs_agblock_t agfl_last
;
516 xfs_agblock_t agfl_count
;
517 xfs_agblock_t fl_count
;
521 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
522 error
= xchk_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
523 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
524 if (!xchk_process_error(sc
, agno
, XFS_AGF_BLOCK(sc
->mp
), &error
))
526 xchk_buffer_recheck(sc
, sc
->sa
.agf_bp
);
528 agf
= sc
->sa
.agf_bp
->b_addr
;
530 /* Check the AG length */
531 eoag
= be32_to_cpu(agf
->agf_length
);
532 if (eoag
!= xfs_ag_block_count(mp
, agno
))
533 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
535 /* Check the AGF btree roots and levels */
536 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]);
537 if (!xfs_verify_agbno(mp
, agno
, agbno
))
538 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
540 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]);
541 if (!xfs_verify_agbno(mp
, agno
, agbno
))
542 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
544 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
545 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
546 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
548 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
549 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
550 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
552 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
)) {
553 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_RMAP
]);
554 if (!xfs_verify_agbno(mp
, agno
, agbno
))
555 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
557 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]);
558 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
559 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
562 if (xfs_sb_version_hasreflink(&mp
->m_sb
)) {
563 agbno
= be32_to_cpu(agf
->agf_refcount_root
);
564 if (!xfs_verify_agbno(mp
, agno
, agbno
))
565 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
567 level
= be32_to_cpu(agf
->agf_refcount_level
);
568 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
569 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
572 /* Check the AGFL counters */
573 agfl_first
= be32_to_cpu(agf
->agf_flfirst
);
574 agfl_last
= be32_to_cpu(agf
->agf_fllast
);
575 agfl_count
= be32_to_cpu(agf
->agf_flcount
);
576 if (agfl_last
> agfl_first
)
577 fl_count
= agfl_last
- agfl_first
+ 1;
579 fl_count
= xfs_agfl_size(mp
) - agfl_first
+ agfl_last
+ 1;
580 if (agfl_count
!= 0 && fl_count
!= agfl_count
)
581 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
583 /* Do the incore counters match? */
584 pag
= xfs_perag_get(mp
, agno
);
585 if (pag
->pagf_freeblks
!= be32_to_cpu(agf
->agf_freeblks
))
586 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
587 if (pag
->pagf_flcount
!= be32_to_cpu(agf
->agf_flcount
))
588 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
589 if (pag
->pagf_btreeblks
!= be32_to_cpu(agf
->agf_btreeblks
))
590 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
600 struct xchk_agfl_info
{
601 unsigned int sz_entries
;
602 unsigned int nr_entries
;
603 xfs_agblock_t
*entries
;
604 struct xfs_scrub
*sc
;
607 /* Cross-reference with the other btrees. */
609 xchk_agfl_block_xref(
610 struct xfs_scrub
*sc
,
613 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
616 xchk_xref_is_used_space(sc
, agbno
, 1);
617 xchk_xref_is_not_inode_chunk(sc
, agbno
, 1);
618 xchk_xref_is_owned_by(sc
, agbno
, 1, &XFS_RMAP_OINFO_AG
);
619 xchk_xref_is_not_shared(sc
, agbno
, 1);
622 /* Scrub an AGFL block. */
625 struct xfs_mount
*mp
,
629 struct xchk_agfl_info
*sai
= priv
;
630 struct xfs_scrub
*sc
= sai
->sc
;
631 xfs_agnumber_t agno
= sc
->sa
.agno
;
633 if (xfs_verify_agbno(mp
, agno
, agbno
) &&
634 sai
->nr_entries
< sai
->sz_entries
)
635 sai
->entries
[sai
->nr_entries
++] = agbno
;
637 xchk_block_set_corrupt(sc
, sc
->sa
.agfl_bp
);
639 xchk_agfl_block_xref(sc
, agbno
);
641 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
652 const xfs_agblock_t
*a
= pa
;
653 const xfs_agblock_t
*b
= pb
;
655 return (int)*a
- (int)*b
;
658 /* Cross-reference with the other btrees. */
661 struct xfs_scrub
*sc
)
663 struct xfs_mount
*mp
= sc
->mp
;
667 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
670 agbno
= XFS_AGFL_BLOCK(mp
);
672 error
= xchk_ag_btcur_init(sc
, &sc
->sa
);
676 xchk_xref_is_used_space(sc
, agbno
, 1);
677 xchk_xref_is_not_inode_chunk(sc
, agbno
, 1);
678 xchk_xref_is_owned_by(sc
, agbno
, 1, &XFS_RMAP_OINFO_FS
);
679 xchk_xref_is_not_shared(sc
, agbno
, 1);
682 * Scrub teardown will take care of sc->sa for us. Leave sc->sa
683 * active so that the agfl block xref can use it too.
687 /* Scrub the AGFL. */
690 struct xfs_scrub
*sc
)
692 struct xchk_agfl_info sai
;
695 unsigned int agflcount
;
699 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
700 error
= xchk_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
701 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
702 if (!xchk_process_error(sc
, agno
, XFS_AGFL_BLOCK(sc
->mp
), &error
))
705 return -EFSCORRUPTED
;
706 xchk_buffer_recheck(sc
, sc
->sa
.agfl_bp
);
710 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
713 /* Allocate buffer to ensure uniqueness of AGFL entries. */
714 agf
= sc
->sa
.agf_bp
->b_addr
;
715 agflcount
= be32_to_cpu(agf
->agf_flcount
);
716 if (agflcount
> xfs_agfl_size(sc
->mp
)) {
717 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
720 memset(&sai
, 0, sizeof(sai
));
722 sai
.sz_entries
= agflcount
;
723 sai
.entries
= kmem_zalloc(sizeof(xfs_agblock_t
) * agflcount
,
730 /* Check the blocks in the AGFL. */
731 error
= xfs_agfl_walk(sc
->mp
, sc
->sa
.agf_bp
->b_addr
,
732 sc
->sa
.agfl_bp
, xchk_agfl_block
, &sai
);
733 if (error
== -ECANCELED
) {
740 if (agflcount
!= sai
.nr_entries
) {
741 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
745 /* Sort entries, check for duplicates. */
746 sort(sai
.entries
, sai
.nr_entries
, sizeof(sai
.entries
[0]),
747 xchk_agblock_cmp
, NULL
);
748 for (i
= 1; i
< sai
.nr_entries
; i
++) {
749 if (sai
.entries
[i
] == sai
.entries
[i
- 1]) {
750 xchk_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
756 kmem_free(sai
.entries
);
763 /* Check agi_count/agi_freecount */
765 xchk_agi_xref_icounts(
766 struct xfs_scrub
*sc
)
768 struct xfs_agi
*agi
= sc
->sa
.agi_bp
->b_addr
;
770 xfs_agino_t freecount
;
776 error
= xfs_ialloc_count_inodes(sc
->sa
.ino_cur
, &icount
, &freecount
);
777 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.ino_cur
))
779 if (be32_to_cpu(agi
->agi_count
) != icount
||
780 be32_to_cpu(agi
->agi_freecount
) != freecount
)
781 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agi_bp
);
784 /* Check agi_[fi]blocks against tree size */
786 xchk_agi_xref_fiblocks(
787 struct xfs_scrub
*sc
)
789 struct xfs_agi
*agi
= sc
->sa
.agi_bp
->b_addr
;
790 xfs_agblock_t blocks
;
793 if (!xfs_sb_version_hasinobtcounts(&sc
->mp
->m_sb
))
796 if (sc
->sa
.ino_cur
) {
797 error
= xfs_btree_count_blocks(sc
->sa
.ino_cur
, &blocks
);
798 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.ino_cur
))
800 if (blocks
!= be32_to_cpu(agi
->agi_iblocks
))
801 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agi_bp
);
804 if (sc
->sa
.fino_cur
) {
805 error
= xfs_btree_count_blocks(sc
->sa
.fino_cur
, &blocks
);
806 if (!xchk_should_check_xref(sc
, &error
, &sc
->sa
.fino_cur
))
808 if (blocks
!= be32_to_cpu(agi
->agi_fblocks
))
809 xchk_block_xref_set_corrupt(sc
, sc
->sa
.agi_bp
);
813 /* Cross-reference with the other btrees. */
816 struct xfs_scrub
*sc
)
818 struct xfs_mount
*mp
= sc
->mp
;
822 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
825 agbno
= XFS_AGI_BLOCK(mp
);
827 error
= xchk_ag_btcur_init(sc
, &sc
->sa
);
831 xchk_xref_is_used_space(sc
, agbno
, 1);
832 xchk_xref_is_not_inode_chunk(sc
, agbno
, 1);
833 xchk_agi_xref_icounts(sc
);
834 xchk_xref_is_owned_by(sc
, agbno
, 1, &XFS_RMAP_OINFO_FS
);
835 xchk_xref_is_not_shared(sc
, agbno
, 1);
836 xchk_agi_xref_fiblocks(sc
);
838 /* scrub teardown will take care of sc->sa for us */
844 struct xfs_scrub
*sc
)
846 struct xfs_mount
*mp
= sc
->mp
;
848 struct xfs_perag
*pag
;
853 xfs_agino_t first_agino
;
854 xfs_agino_t last_agino
;
860 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
861 error
= xchk_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
862 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
863 if (!xchk_process_error(sc
, agno
, XFS_AGI_BLOCK(sc
->mp
), &error
))
865 xchk_buffer_recheck(sc
, sc
->sa
.agi_bp
);
867 agi
= sc
->sa
.agi_bp
->b_addr
;
869 /* Check the AG length */
870 eoag
= be32_to_cpu(agi
->agi_length
);
871 if (eoag
!= xfs_ag_block_count(mp
, agno
))
872 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
874 /* Check btree roots and levels */
875 agbno
= be32_to_cpu(agi
->agi_root
);
876 if (!xfs_verify_agbno(mp
, agno
, agbno
))
877 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
879 level
= be32_to_cpu(agi
->agi_level
);
880 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
881 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
883 if (xfs_sb_version_hasfinobt(&mp
->m_sb
)) {
884 agbno
= be32_to_cpu(agi
->agi_free_root
);
885 if (!xfs_verify_agbno(mp
, agno
, agbno
))
886 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
888 level
= be32_to_cpu(agi
->agi_free_level
);
889 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
890 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
893 /* Check inode counters */
894 xfs_agino_range(mp
, agno
, &first_agino
, &last_agino
);
895 icount
= be32_to_cpu(agi
->agi_count
);
896 if (icount
> last_agino
- first_agino
+ 1 ||
897 icount
< be32_to_cpu(agi
->agi_freecount
))
898 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
900 /* Check inode pointers */
901 agino
= be32_to_cpu(agi
->agi_newino
);
902 if (!xfs_verify_agino_or_null(mp
, agno
, agino
))
903 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
905 agino
= be32_to_cpu(agi
->agi_dirino
);
906 if (!xfs_verify_agino_or_null(mp
, agno
, agino
))
907 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
909 /* Check unlinked inode buckets */
910 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++) {
911 agino
= be32_to_cpu(agi
->agi_unlinked
[i
]);
912 if (!xfs_verify_agino_or_null(mp
, agno
, agino
))
913 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
916 if (agi
->agi_pad32
!= cpu_to_be32(0))
917 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
919 /* Do the incore counters match? */
920 pag
= xfs_perag_get(mp
, agno
);
921 if (pag
->pagi_count
!= be32_to_cpu(agi
->agi_count
))
922 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
923 if (pag
->pagi_freecount
!= be32_to_cpu(agi
->agi_freecount
))
924 xchk_block_set_corrupt(sc
, sc
->sa
.agi_bp
);