2 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_trans_resv.h"
25 #include "xfs_mount.h"
26 #include "xfs_defer.h"
27 #include "xfs_btree.h"
29 #include "xfs_log_format.h"
30 #include "xfs_trans.h"
32 #include "xfs_inode.h"
33 #include "xfs_alloc.h"
34 #include "xfs_ialloc.h"
36 #include "scrub/xfs_scrub.h"
37 #include "scrub/scrub.h"
38 #include "scrub/common.h"
39 #include "scrub/trace.h"
43 /* Cross-reference with the other btrees. */
45 xfs_scrub_superblock_xref(
46 struct xfs_scrub_context
*sc
,
49 struct xfs_owner_info oinfo
;
50 struct xfs_mount
*mp
= sc
->mp
;
51 xfs_agnumber_t agno
= sc
->sm
->sm_agno
;
55 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
58 agbno
= XFS_SB_BLOCK(mp
);
60 error
= xfs_scrub_ag_init(sc
, agno
, &sc
->sa
);
61 if (!xfs_scrub_xref_process_error(sc
, agno
, agbno
, &error
))
64 xfs_scrub_xref_is_used_space(sc
, agbno
, 1);
65 xfs_scrub_xref_is_not_inode_chunk(sc
, agbno
, 1);
66 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_FS
);
67 xfs_scrub_xref_is_owned_by(sc
, agbno
, 1, &oinfo
);
68 xfs_scrub_xref_is_not_shared(sc
, agbno
, 1);
70 /* scrub teardown will take care of sc->sa for us */
74 * Scrub the filesystem superblock.
76 * Note: We do /not/ attempt to check AG 0's superblock. Mount is
77 * responsible for validating all the geometry information in sb 0, so
78 * if the filesystem is capable of initiating online scrub, then clearly
79 * sb 0 is ok and we can use its information to check everything else.
83 struct xfs_scrub_context
*sc
)
85 struct xfs_mount
*mp
= sc
->mp
;
94 agno
= sc
->sm
->sm_agno
;
98 error
= xfs_sb_read_secondary(mp
, sc
->tp
, agno
, &bp
);
100 * The superblock verifier can return several different error codes
101 * if it thinks the superblock doesn't look right. For a mount these
102 * would all get bounced back to userspace, but if we're here then the
103 * fs mounted successfully, which means that this secondary superblock
104 * is simply incorrect. Treat all these codes the same way we treat
108 case -EINVAL
: /* also -EWRONGFS */
111 error
= -EFSCORRUPTED
;
115 if (!xfs_scrub_process_error(sc
, agno
, XFS_SB_BLOCK(mp
), &error
))
118 sb
= XFS_BUF_TO_SBP(bp
);
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
126 if (sb
->sb_blocksize
!= cpu_to_be32(mp
->m_sb
.sb_blocksize
))
127 xfs_scrub_block_set_corrupt(sc
, bp
);
129 if (sb
->sb_dblocks
!= cpu_to_be64(mp
->m_sb
.sb_dblocks
))
130 xfs_scrub_block_set_corrupt(sc
, bp
);
132 if (sb
->sb_rblocks
!= cpu_to_be64(mp
->m_sb
.sb_rblocks
))
133 xfs_scrub_block_set_corrupt(sc
, bp
);
135 if (sb
->sb_rextents
!= cpu_to_be64(mp
->m_sb
.sb_rextents
))
136 xfs_scrub_block_set_corrupt(sc
, bp
);
138 if (!uuid_equal(&sb
->sb_uuid
, &mp
->m_sb
.sb_uuid
))
139 xfs_scrub_block_set_preen(sc
, bp
);
141 if (sb
->sb_logstart
!= cpu_to_be64(mp
->m_sb
.sb_logstart
))
142 xfs_scrub_block_set_corrupt(sc
, bp
);
144 if (sb
->sb_rootino
!= cpu_to_be64(mp
->m_sb
.sb_rootino
))
145 xfs_scrub_block_set_preen(sc
, bp
);
147 if (sb
->sb_rbmino
!= cpu_to_be64(mp
->m_sb
.sb_rbmino
))
148 xfs_scrub_block_set_preen(sc
, bp
);
150 if (sb
->sb_rsumino
!= cpu_to_be64(mp
->m_sb
.sb_rsumino
))
151 xfs_scrub_block_set_preen(sc
, bp
);
153 if (sb
->sb_rextsize
!= cpu_to_be32(mp
->m_sb
.sb_rextsize
))
154 xfs_scrub_block_set_corrupt(sc
, bp
);
156 if (sb
->sb_agblocks
!= cpu_to_be32(mp
->m_sb
.sb_agblocks
))
157 xfs_scrub_block_set_corrupt(sc
, bp
);
159 if (sb
->sb_agcount
!= cpu_to_be32(mp
->m_sb
.sb_agcount
))
160 xfs_scrub_block_set_corrupt(sc
, bp
);
162 if (sb
->sb_rbmblocks
!= cpu_to_be32(mp
->m_sb
.sb_rbmblocks
))
163 xfs_scrub_block_set_corrupt(sc
, bp
);
165 if (sb
->sb_logblocks
!= cpu_to_be32(mp
->m_sb
.sb_logblocks
))
166 xfs_scrub_block_set_corrupt(sc
, bp
);
168 /* Check sb_versionnum bits that are set at mkfs time. */
169 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 if ((sb
->sb_versionnum
& vernum_mask
) !=
179 (cpu_to_be16(mp
->m_sb
.sb_versionnum
) & vernum_mask
))
180 xfs_scrub_block_set_corrupt(sc
, bp
);
182 /* Check sb_versionnum bits that can be set after mkfs time. */
183 vernum_mask
= cpu_to_be16(XFS_SB_VERSION_ATTRBIT
|
184 XFS_SB_VERSION_NLINKBIT
|
185 XFS_SB_VERSION_QUOTABIT
);
186 if ((sb
->sb_versionnum
& vernum_mask
) !=
187 (cpu_to_be16(mp
->m_sb
.sb_versionnum
) & vernum_mask
))
188 xfs_scrub_block_set_preen(sc
, bp
);
190 if (sb
->sb_sectsize
!= cpu_to_be16(mp
->m_sb
.sb_sectsize
))
191 xfs_scrub_block_set_corrupt(sc
, bp
);
193 if (sb
->sb_inodesize
!= cpu_to_be16(mp
->m_sb
.sb_inodesize
))
194 xfs_scrub_block_set_corrupt(sc
, bp
);
196 if (sb
->sb_inopblock
!= cpu_to_be16(mp
->m_sb
.sb_inopblock
))
197 xfs_scrub_block_set_corrupt(sc
, bp
);
199 if (memcmp(sb
->sb_fname
, mp
->m_sb
.sb_fname
, sizeof(sb
->sb_fname
)))
200 xfs_scrub_block_set_preen(sc
, bp
);
202 if (sb
->sb_blocklog
!= mp
->m_sb
.sb_blocklog
)
203 xfs_scrub_block_set_corrupt(sc
, bp
);
205 if (sb
->sb_sectlog
!= mp
->m_sb
.sb_sectlog
)
206 xfs_scrub_block_set_corrupt(sc
, bp
);
208 if (sb
->sb_inodelog
!= mp
->m_sb
.sb_inodelog
)
209 xfs_scrub_block_set_corrupt(sc
, bp
);
211 if (sb
->sb_inopblog
!= mp
->m_sb
.sb_inopblog
)
212 xfs_scrub_block_set_corrupt(sc
, bp
);
214 if (sb
->sb_agblklog
!= mp
->m_sb
.sb_agblklog
)
215 xfs_scrub_block_set_corrupt(sc
, bp
);
217 if (sb
->sb_rextslog
!= mp
->m_sb
.sb_rextslog
)
218 xfs_scrub_block_set_corrupt(sc
, bp
);
220 if (sb
->sb_imax_pct
!= mp
->m_sb
.sb_imax_pct
)
221 xfs_scrub_block_set_preen(sc
, bp
);
224 * Skip the summary counters since we track them in memory anyway.
225 * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
228 if (sb
->sb_uquotino
!= cpu_to_be64(mp
->m_sb
.sb_uquotino
))
229 xfs_scrub_block_set_preen(sc
, bp
);
231 if (sb
->sb_gquotino
!= cpu_to_be64(mp
->m_sb
.sb_gquotino
))
232 xfs_scrub_block_set_preen(sc
, bp
);
235 * Skip the quota flags since repair will force quotacheck.
239 if (sb
->sb_flags
!= mp
->m_sb
.sb_flags
)
240 xfs_scrub_block_set_corrupt(sc
, bp
);
242 if (sb
->sb_shared_vn
!= mp
->m_sb
.sb_shared_vn
)
243 xfs_scrub_block_set_corrupt(sc
, bp
);
245 if (sb
->sb_inoalignmt
!= cpu_to_be32(mp
->m_sb
.sb_inoalignmt
))
246 xfs_scrub_block_set_corrupt(sc
, bp
);
248 if (sb
->sb_unit
!= cpu_to_be32(mp
->m_sb
.sb_unit
))
249 xfs_scrub_block_set_preen(sc
, bp
);
251 if (sb
->sb_width
!= cpu_to_be32(mp
->m_sb
.sb_width
))
252 xfs_scrub_block_set_preen(sc
, bp
);
254 if (sb
->sb_dirblklog
!= mp
->m_sb
.sb_dirblklog
)
255 xfs_scrub_block_set_corrupt(sc
, bp
);
257 if (sb
->sb_logsectlog
!= mp
->m_sb
.sb_logsectlog
)
258 xfs_scrub_block_set_corrupt(sc
, bp
);
260 if (sb
->sb_logsectsize
!= cpu_to_be16(mp
->m_sb
.sb_logsectsize
))
261 xfs_scrub_block_set_corrupt(sc
, bp
);
263 if (sb
->sb_logsunit
!= cpu_to_be32(mp
->m_sb
.sb_logsunit
))
264 xfs_scrub_block_set_corrupt(sc
, bp
);
266 /* Do we see any invalid bits in sb_features2? */
267 if (!xfs_sb_version_hasmorebits(&mp
->m_sb
)) {
268 if (sb
->sb_features2
!= 0)
269 xfs_scrub_block_set_corrupt(sc
, bp
);
271 v2_ok
= XFS_SB_VERSION2_OKBITS
;
272 if (XFS_SB_VERSION_NUM(&mp
->m_sb
) >= XFS_SB_VERSION_5
)
273 v2_ok
|= XFS_SB_VERSION2_CRCBIT
;
275 if (!!(sb
->sb_features2
& cpu_to_be32(~v2_ok
)))
276 xfs_scrub_block_set_corrupt(sc
, bp
);
278 if (sb
->sb_features2
!= sb
->sb_bad_features2
)
279 xfs_scrub_block_set_preen(sc
, bp
);
282 /* Check sb_features2 flags that are set at mkfs time. */
283 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 if ((sb
->sb_features2
& features_mask
) !=
288 (cpu_to_be32(mp
->m_sb
.sb_features2
) & features_mask
))
289 xfs_scrub_block_set_corrupt(sc
, bp
);
291 /* Check sb_features2 flags that can be set after mkfs time. */
292 features_mask
= cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT
);
293 if ((sb
->sb_features2
& features_mask
) !=
294 (cpu_to_be32(mp
->m_sb
.sb_features2
) & features_mask
))
295 xfs_scrub_block_set_corrupt(sc
, bp
);
297 if (!xfs_sb_version_hascrc(&mp
->m_sb
)) {
298 /* all v5 fields must be zero */
299 if (memchr_inv(&sb
->sb_features_compat
, 0,
300 sizeof(struct xfs_dsb
) -
301 offsetof(struct xfs_dsb
, sb_features_compat
)))
302 xfs_scrub_block_set_corrupt(sc
, bp
);
304 /* Check compat flags; all are set at mkfs time. */
305 features_mask
= cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN
);
306 if ((sb
->sb_features_compat
& features_mask
) !=
307 (cpu_to_be32(mp
->m_sb
.sb_features_compat
) & features_mask
))
308 xfs_scrub_block_set_corrupt(sc
, bp
);
310 /* Check ro compat flags; all are set at mkfs time. */
311 features_mask
= cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN
|
312 XFS_SB_FEAT_RO_COMPAT_FINOBT
|
313 XFS_SB_FEAT_RO_COMPAT_RMAPBT
|
314 XFS_SB_FEAT_RO_COMPAT_REFLINK
);
315 if ((sb
->sb_features_ro_compat
& features_mask
) !=
316 (cpu_to_be32(mp
->m_sb
.sb_features_ro_compat
) &
318 xfs_scrub_block_set_corrupt(sc
, bp
);
320 /* Check incompat flags; all are set at mkfs time. */
321 features_mask
= cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN
|
322 XFS_SB_FEAT_INCOMPAT_FTYPE
|
323 XFS_SB_FEAT_INCOMPAT_SPINODES
|
324 XFS_SB_FEAT_INCOMPAT_META_UUID
);
325 if ((sb
->sb_features_incompat
& features_mask
) !=
326 (cpu_to_be32(mp
->m_sb
.sb_features_incompat
) &
328 xfs_scrub_block_set_corrupt(sc
, bp
);
330 /* Check log incompat flags; all are set at mkfs time. */
331 features_mask
= cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN
);
332 if ((sb
->sb_features_log_incompat
& features_mask
) !=
333 (cpu_to_be32(mp
->m_sb
.sb_features_log_incompat
) &
335 xfs_scrub_block_set_corrupt(sc
, bp
);
337 /* Don't care about sb_crc */
339 if (sb
->sb_spino_align
!= cpu_to_be32(mp
->m_sb
.sb_spino_align
))
340 xfs_scrub_block_set_corrupt(sc
, bp
);
342 if (sb
->sb_pquotino
!= cpu_to_be64(mp
->m_sb
.sb_pquotino
))
343 xfs_scrub_block_set_preen(sc
, bp
);
345 /* Don't care about sb_lsn */
348 if (xfs_sb_version_hasmetauuid(&mp
->m_sb
)) {
349 /* The metadata UUID must be the same for all supers */
350 if (!uuid_equal(&sb
->sb_meta_uuid
, &mp
->m_sb
.sb_meta_uuid
))
351 xfs_scrub_block_set_corrupt(sc
, bp
);
354 /* Everything else must be zero. */
355 if (memchr_inv(sb
+ 1, 0,
356 BBTOB(bp
->b_length
) - sizeof(struct xfs_dsb
)))
357 xfs_scrub_block_set_corrupt(sc
, bp
);
359 xfs_scrub_superblock_xref(sc
, bp
);
366 /* Tally freespace record lengths. */
368 xfs_scrub_agf_record_bno_lengths(
369 struct xfs_btree_cur
*cur
,
370 struct xfs_alloc_rec_incore
*rec
,
373 xfs_extlen_t
*blocks
= priv
;
375 (*blocks
) += rec
->ar_blockcount
;
379 /* Check agf_freeblks */
381 xfs_scrub_agf_xref_freeblks(
382 struct xfs_scrub_context
*sc
)
384 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
385 xfs_extlen_t blocks
= 0;
391 error
= xfs_alloc_query_all(sc
->sa
.bno_cur
,
392 xfs_scrub_agf_record_bno_lengths
, &blocks
);
393 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.bno_cur
))
395 if (blocks
!= be32_to_cpu(agf
->agf_freeblks
))
396 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
399 /* Cross reference the AGF with the cntbt (freespace by length btree) */
401 xfs_scrub_agf_xref_cntbt(
402 struct xfs_scrub_context
*sc
)
404 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
413 /* Any freespace at all? */
414 error
= xfs_alloc_lookup_le(sc
->sa
.cnt_cur
, 0, -1U, &have
);
415 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
418 if (agf
->agf_freeblks
!= be32_to_cpu(0))
419 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
423 /* Check agf_longest */
424 error
= xfs_alloc_get_rec(sc
->sa
.cnt_cur
, &agbno
, &blocks
, &have
);
425 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
427 if (!have
|| blocks
!= be32_to_cpu(agf
->agf_longest
))
428 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
431 /* Check the btree block counts in the AGF against the btrees. */
433 xfs_scrub_agf_xref_btreeblks(
434 struct xfs_scrub_context
*sc
)
436 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
437 struct xfs_mount
*mp
= sc
->mp
;
438 xfs_agblock_t blocks
;
439 xfs_agblock_t btreeblks
;
442 /* Check agf_rmap_blocks; set up for agf_btreeblks check */
443 if (sc
->sa
.rmap_cur
) {
444 error
= xfs_btree_count_blocks(sc
->sa
.rmap_cur
, &blocks
);
445 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.rmap_cur
))
447 btreeblks
= blocks
- 1;
448 if (blocks
!= be32_to_cpu(agf
->agf_rmap_blocks
))
449 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
455 * No rmap cursor; we can't xref if we have the rmapbt feature.
456 * We also can't do it if we're missing the free space btree cursors.
458 if ((xfs_sb_version_hasrmapbt(&mp
->m_sb
) && !sc
->sa
.rmap_cur
) ||
459 !sc
->sa
.bno_cur
|| !sc
->sa
.cnt_cur
)
462 /* Check agf_btreeblks */
463 error
= xfs_btree_count_blocks(sc
->sa
.bno_cur
, &blocks
);
464 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.bno_cur
))
466 btreeblks
+= blocks
- 1;
468 error
= xfs_btree_count_blocks(sc
->sa
.cnt_cur
, &blocks
);
469 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.cnt_cur
))
471 btreeblks
+= blocks
- 1;
473 if (btreeblks
!= be32_to_cpu(agf
->agf_btreeblks
))
474 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
477 /* Check agf_refcount_blocks against tree size */
479 xfs_scrub_agf_xref_refcblks(
480 struct xfs_scrub_context
*sc
)
482 struct xfs_agf
*agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
483 xfs_agblock_t blocks
;
486 if (!sc
->sa
.refc_cur
)
489 error
= xfs_btree_count_blocks(sc
->sa
.refc_cur
, &blocks
);
490 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.refc_cur
))
492 if (blocks
!= be32_to_cpu(agf
->agf_refcount_blocks
))
493 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agf_bp
);
496 /* Cross-reference with the other btrees. */
499 struct xfs_scrub_context
*sc
)
501 struct xfs_owner_info oinfo
;
502 struct xfs_mount
*mp
= sc
->mp
;
506 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
509 agbno
= XFS_AGF_BLOCK(mp
);
511 error
= xfs_scrub_ag_btcur_init(sc
, &sc
->sa
);
515 xfs_scrub_xref_is_used_space(sc
, agbno
, 1);
516 xfs_scrub_agf_xref_freeblks(sc
);
517 xfs_scrub_agf_xref_cntbt(sc
);
518 xfs_scrub_xref_is_not_inode_chunk(sc
, agbno
, 1);
519 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_FS
);
520 xfs_scrub_xref_is_owned_by(sc
, agbno
, 1, &oinfo
);
521 xfs_scrub_agf_xref_btreeblks(sc
);
522 xfs_scrub_xref_is_not_shared(sc
, agbno
, 1);
523 xfs_scrub_agf_xref_refcblks(sc
);
525 /* scrub teardown will take care of sc->sa for us */
531 struct xfs_scrub_context
*sc
)
533 struct xfs_mount
*mp
= sc
->mp
;
538 xfs_agblock_t agfl_first
;
539 xfs_agblock_t agfl_last
;
540 xfs_agblock_t agfl_count
;
541 xfs_agblock_t fl_count
;
545 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
546 error
= xfs_scrub_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
547 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
548 if (!xfs_scrub_process_error(sc
, agno
, XFS_AGF_BLOCK(sc
->mp
), &error
))
550 xfs_scrub_buffer_recheck(sc
, sc
->sa
.agf_bp
);
552 agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
554 /* Check the AG length */
555 eoag
= be32_to_cpu(agf
->agf_length
);
556 if (eoag
!= xfs_ag_block_count(mp
, agno
))
557 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
559 /* Check the AGF btree roots and levels */
560 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_BNO
]);
561 if (!xfs_verify_agbno(mp
, agno
, agbno
))
562 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
564 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_CNT
]);
565 if (!xfs_verify_agbno(mp
, agno
, agbno
))
566 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
568 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_BNO
]);
569 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
570 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
572 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_CNT
]);
573 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
574 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
576 if (xfs_sb_version_hasrmapbt(&mp
->m_sb
)) {
577 agbno
= be32_to_cpu(agf
->agf_roots
[XFS_BTNUM_RMAP
]);
578 if (!xfs_verify_agbno(mp
, agno
, agbno
))
579 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
581 level
= be32_to_cpu(agf
->agf_levels
[XFS_BTNUM_RMAP
]);
582 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
583 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
586 if (xfs_sb_version_hasreflink(&mp
->m_sb
)) {
587 agbno
= be32_to_cpu(agf
->agf_refcount_root
);
588 if (!xfs_verify_agbno(mp
, agno
, agbno
))
589 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
591 level
= be32_to_cpu(agf
->agf_refcount_level
);
592 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
593 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
596 /* Check the AGFL counters */
597 agfl_first
= be32_to_cpu(agf
->agf_flfirst
);
598 agfl_last
= be32_to_cpu(agf
->agf_fllast
);
599 agfl_count
= be32_to_cpu(agf
->agf_flcount
);
600 if (agfl_last
> agfl_first
)
601 fl_count
= agfl_last
- agfl_first
+ 1;
603 fl_count
= xfs_agfl_size(mp
) - agfl_first
+ agfl_last
+ 1;
604 if (agfl_count
!= 0 && fl_count
!= agfl_count
)
605 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
607 xfs_scrub_agf_xref(sc
);
614 struct xfs_scrub_agfl_info
{
615 struct xfs_owner_info oinfo
;
616 unsigned int sz_entries
;
617 unsigned int nr_entries
;
618 xfs_agblock_t
*entries
;
619 struct xfs_scrub_context
*sc
;
622 /* Cross-reference with the other btrees. */
624 xfs_scrub_agfl_block_xref(
625 struct xfs_scrub_context
*sc
,
627 struct xfs_owner_info
*oinfo
)
629 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
632 xfs_scrub_xref_is_used_space(sc
, agbno
, 1);
633 xfs_scrub_xref_is_not_inode_chunk(sc
, agbno
, 1);
634 xfs_scrub_xref_is_owned_by(sc
, agbno
, 1, oinfo
);
635 xfs_scrub_xref_is_not_shared(sc
, agbno
, 1);
638 /* Scrub an AGFL block. */
640 xfs_scrub_agfl_block(
641 struct xfs_mount
*mp
,
645 struct xfs_scrub_agfl_info
*sai
= priv
;
646 struct xfs_scrub_context
*sc
= sai
->sc
;
647 xfs_agnumber_t agno
= sc
->sa
.agno
;
649 if (xfs_verify_agbno(mp
, agno
, agbno
) &&
650 sai
->nr_entries
< sai
->sz_entries
)
651 sai
->entries
[sai
->nr_entries
++] = agbno
;
653 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agfl_bp
);
655 xfs_scrub_agfl_block_xref(sc
, agbno
, priv
);
657 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
658 return XFS_BTREE_QUERY_RANGE_ABORT
;
664 xfs_scrub_agblock_cmp(
668 const xfs_agblock_t
*a
= pa
;
669 const xfs_agblock_t
*b
= pb
;
671 return (int)*a
- (int)*b
;
674 /* Cross-reference with the other btrees. */
677 struct xfs_scrub_context
*sc
)
679 struct xfs_owner_info oinfo
;
680 struct xfs_mount
*mp
= sc
->mp
;
684 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
687 agbno
= XFS_AGFL_BLOCK(mp
);
689 error
= xfs_scrub_ag_btcur_init(sc
, &sc
->sa
);
693 xfs_scrub_xref_is_used_space(sc
, agbno
, 1);
694 xfs_scrub_xref_is_not_inode_chunk(sc
, agbno
, 1);
695 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_FS
);
696 xfs_scrub_xref_is_owned_by(sc
, agbno
, 1, &oinfo
);
697 xfs_scrub_xref_is_not_shared(sc
, agbno
, 1);
700 * Scrub teardown will take care of sc->sa for us. Leave sc->sa
701 * active so that the agfl block xref can use it too.
705 /* Scrub the AGFL. */
708 struct xfs_scrub_context
*sc
)
710 struct xfs_scrub_agfl_info sai
;
713 unsigned int agflcount
;
717 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
718 error
= xfs_scrub_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
719 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
720 if (!xfs_scrub_process_error(sc
, agno
, XFS_AGFL_BLOCK(sc
->mp
), &error
))
723 return -EFSCORRUPTED
;
724 xfs_scrub_buffer_recheck(sc
, sc
->sa
.agfl_bp
);
726 xfs_scrub_agfl_xref(sc
);
728 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
731 /* Allocate buffer to ensure uniqueness of AGFL entries. */
732 agf
= XFS_BUF_TO_AGF(sc
->sa
.agf_bp
);
733 agflcount
= be32_to_cpu(agf
->agf_flcount
);
734 if (agflcount
> xfs_agfl_size(sc
->mp
)) {
735 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
738 memset(&sai
, 0, sizeof(sai
));
740 sai
.sz_entries
= agflcount
;
741 sai
.entries
= kmem_zalloc(sizeof(xfs_agblock_t
) * agflcount
,
748 /* Check the blocks in the AGFL. */
749 xfs_rmap_ag_owner(&sai
.oinfo
, XFS_RMAP_OWN_AG
);
750 error
= xfs_agfl_walk(sc
->mp
, XFS_BUF_TO_AGF(sc
->sa
.agf_bp
),
751 sc
->sa
.agfl_bp
, xfs_scrub_agfl_block
, &sai
);
752 if (error
== XFS_BTREE_QUERY_RANGE_ABORT
) {
759 if (agflcount
!= sai
.nr_entries
) {
760 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
764 /* Sort entries, check for duplicates. */
765 sort(sai
.entries
, sai
.nr_entries
, sizeof(sai
.entries
[0]),
766 xfs_scrub_agblock_cmp
, NULL
);
767 for (i
= 1; i
< sai
.nr_entries
; i
++) {
768 if (sai
.entries
[i
] == sai
.entries
[i
- 1]) {
769 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agf_bp
);
775 kmem_free(sai
.entries
);
782 /* Check agi_count/agi_freecount */
784 xfs_scrub_agi_xref_icounts(
785 struct xfs_scrub_context
*sc
)
787 struct xfs_agi
*agi
= XFS_BUF_TO_AGI(sc
->sa
.agi_bp
);
789 xfs_agino_t freecount
;
795 error
= xfs_ialloc_count_inodes(sc
->sa
.ino_cur
, &icount
, &freecount
);
796 if (!xfs_scrub_should_check_xref(sc
, &error
, &sc
->sa
.ino_cur
))
798 if (be32_to_cpu(agi
->agi_count
) != icount
||
799 be32_to_cpu(agi
->agi_freecount
) != freecount
)
800 xfs_scrub_block_xref_set_corrupt(sc
, sc
->sa
.agi_bp
);
803 /* Cross-reference with the other btrees. */
806 struct xfs_scrub_context
*sc
)
808 struct xfs_owner_info oinfo
;
809 struct xfs_mount
*mp
= sc
->mp
;
813 if (sc
->sm
->sm_flags
& XFS_SCRUB_OFLAG_CORRUPT
)
816 agbno
= XFS_AGI_BLOCK(mp
);
818 error
= xfs_scrub_ag_btcur_init(sc
, &sc
->sa
);
822 xfs_scrub_xref_is_used_space(sc
, agbno
, 1);
823 xfs_scrub_xref_is_not_inode_chunk(sc
, agbno
, 1);
824 xfs_scrub_agi_xref_icounts(sc
);
825 xfs_rmap_ag_owner(&oinfo
, XFS_RMAP_OWN_FS
);
826 xfs_scrub_xref_is_owned_by(sc
, agbno
, 1, &oinfo
);
827 xfs_scrub_xref_is_not_shared(sc
, agbno
, 1);
829 /* scrub teardown will take care of sc->sa for us */
835 struct xfs_scrub_context
*sc
)
837 struct xfs_mount
*mp
= sc
->mp
;
843 xfs_agino_t first_agino
;
844 xfs_agino_t last_agino
;
850 agno
= sc
->sa
.agno
= sc
->sm
->sm_agno
;
851 error
= xfs_scrub_ag_read_headers(sc
, agno
, &sc
->sa
.agi_bp
,
852 &sc
->sa
.agf_bp
, &sc
->sa
.agfl_bp
);
853 if (!xfs_scrub_process_error(sc
, agno
, XFS_AGI_BLOCK(sc
->mp
), &error
))
855 xfs_scrub_buffer_recheck(sc
, sc
->sa
.agi_bp
);
857 agi
= XFS_BUF_TO_AGI(sc
->sa
.agi_bp
);
859 /* Check the AG length */
860 eoag
= be32_to_cpu(agi
->agi_length
);
861 if (eoag
!= xfs_ag_block_count(mp
, agno
))
862 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
864 /* Check btree roots and levels */
865 agbno
= be32_to_cpu(agi
->agi_root
);
866 if (!xfs_verify_agbno(mp
, agno
, agbno
))
867 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
869 level
= be32_to_cpu(agi
->agi_level
);
870 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
871 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
873 if (xfs_sb_version_hasfinobt(&mp
->m_sb
)) {
874 agbno
= be32_to_cpu(agi
->agi_free_root
);
875 if (!xfs_verify_agbno(mp
, agno
, agbno
))
876 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
878 level
= be32_to_cpu(agi
->agi_free_level
);
879 if (level
<= 0 || level
> XFS_BTREE_MAXLEVELS
)
880 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
883 /* Check inode counters */
884 xfs_ialloc_agino_range(mp
, agno
, &first_agino
, &last_agino
);
885 icount
= be32_to_cpu(agi
->agi_count
);
886 if (icount
> last_agino
- first_agino
+ 1 ||
887 icount
< be32_to_cpu(agi
->agi_freecount
))
888 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
890 /* Check inode pointers */
891 agino
= be32_to_cpu(agi
->agi_newino
);
892 if (agino
!= NULLAGINO
&& !xfs_verify_agino(mp
, agno
, agino
))
893 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
895 agino
= be32_to_cpu(agi
->agi_dirino
);
896 if (agino
!= NULLAGINO
&& !xfs_verify_agino(mp
, agno
, agino
))
897 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
899 /* Check unlinked inode buckets */
900 for (i
= 0; i
< XFS_AGI_UNLINKED_BUCKETS
; i
++) {
901 agino
= be32_to_cpu(agi
->agi_unlinked
[i
]);
902 if (agino
== NULLAGINO
)
904 if (!xfs_verify_agino(mp
, agno
, agino
))
905 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
908 if (agi
->agi_pad32
!= cpu_to_be32(0))
909 xfs_scrub_block_set_corrupt(sc
, sc
->sa
.agi_bp
);
911 xfs_scrub_agi_xref(sc
);