]> git.ipfire.org Git - people/ms/linux.git/blame - fs/xfs/scrub/agheader.c
xfs: dynamically allocate cursors based on maxlevels
[people/ms/linux.git] / fs / xfs / scrub / agheader.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0+
21fb4cb1
DW
2/*
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
21fb4cb1 4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
21fb4cb1
DW
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"
21fb4cb1 12#include "xfs_btree.h"
21fb4cb1 13#include "xfs_sb.h"
ab9d5dc5 14#include "xfs_alloc.h"
a12890ae 15#include "xfs_ialloc.h"
d852657c 16#include "xfs_rmap.h"
9bbafc71 17#include "xfs_ag.h"
21fb4cb1
DW
18#include "scrub/scrub.h"
19#include "scrub/common.h"
21fb4cb1 20
21fb4cb1
DW
21/* Superblock */
22
166d7641
DW
23/* Cross-reference with the other btrees. */
24STATIC void
c517b3aa 25xchk_superblock_xref(
1d8a748a 26 struct xfs_scrub *sc,
032d91f9 27 struct xfs_buf *bp)
166d7641 28{
032d91f9
DW
29 struct xfs_mount *mp = sc->mp;
30 xfs_agnumber_t agno = sc->sm->sm_agno;
31 xfs_agblock_t agbno;
32 int error;
52dc4b44 33
166d7641
DW
34 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
35 return;
52dc4b44
DW
36
37 agbno = XFS_SB_BLOCK(mp);
38
48c6615c 39 error = xchk_ag_init_existing(sc, agno, &sc->sa);
c517b3aa 40 if (!xchk_xref_process_error(sc, agno, agbno, &error))
52dc4b44
DW
41 return;
42
c517b3aa
DW
43 xchk_xref_is_used_space(sc, agbno, 1);
44 xchk_xref_is_not_inode_chunk(sc, agbno, 1);
7280feda 45 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
c517b3aa 46 xchk_xref_is_not_shared(sc, agbno, 1);
52dc4b44
DW
47
48 /* scrub teardown will take care of sc->sa for us */
166d7641
DW
49}
50
21fb4cb1
DW
51/*
52 * Scrub the filesystem superblock.
53 *
54 * Note: We do /not/ attempt to check AG 0's superblock. Mount is
55 * responsible for validating all the geometry information in sb 0, so
56 * if the filesystem is capable of initiating online scrub, then clearly
57 * sb 0 is ok and we can use its information to check everything else.
58 */
59int
c517b3aa 60xchk_superblock(
1d8a748a 61 struct xfs_scrub *sc)
21fb4cb1 62{
032d91f9
DW
63 struct xfs_mount *mp = sc->mp;
64 struct xfs_buf *bp;
65 struct xfs_dsb *sb;
48c6615c 66 struct xfs_perag *pag;
032d91f9
DW
67 xfs_agnumber_t agno;
68 uint32_t v2_ok;
69 __be32 features_mask;
70 int error;
71 __be16 vernum_mask;
21fb4cb1
DW
72
73 agno = sc->sm->sm_agno;
74 if (agno == 0)
75 return 0;
76
48c6615c
DW
77 /*
78 * Grab an active reference to the perag structure. If we can't get
79 * it, we're racing with something that's tearing down the AG, so
80 * signal that the AG no longer exists.
81 */
82 pag = xfs_perag_get(mp, agno);
83 if (!pag)
84 return -ENOENT;
85
689e11c8 86 error = xfs_sb_read_secondary(mp, sc->tp, agno, &bp);
e5b37faa
DW
87 /*
88 * The superblock verifier can return several different error codes
89 * if it thinks the superblock doesn't look right. For a mount these
90 * would all get bounced back to userspace, but if we're here then the
91 * fs mounted successfully, which means that this secondary superblock
92 * is simply incorrect. Treat all these codes the same way we treat
93 * any corruption.
94 */
95 switch (error) {
96 case -EINVAL: /* also -EWRONGFS */
97 case -ENOSYS:
98 case -EFBIG:
99 error = -EFSCORRUPTED;
53004ee7 100 fallthrough;
e5b37faa
DW
101 default:
102 break;
103 }
c517b3aa 104 if (!xchk_process_error(sc, agno, XFS_SB_BLOCK(mp), &error))
48c6615c 105 goto out_pag;
21fb4cb1 106
3e6e8afd 107 sb = bp->b_addr;
21fb4cb1
DW
108
109 /*
110 * Verify the geometries match. Fields that are permanently
111 * set by mkfs are checked; fields that can be updated later
112 * (and are not propagated to backup superblocks) are preen
113 * checked.
114 */
115 if (sb->sb_blocksize != cpu_to_be32(mp->m_sb.sb_blocksize))
c517b3aa 116 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
117
118 if (sb->sb_dblocks != cpu_to_be64(mp->m_sb.sb_dblocks))
c517b3aa 119 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
120
121 if (sb->sb_rblocks != cpu_to_be64(mp->m_sb.sb_rblocks))
c517b3aa 122 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
123
124 if (sb->sb_rextents != cpu_to_be64(mp->m_sb.sb_rextents))
c517b3aa 125 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
126
127 if (!uuid_equal(&sb->sb_uuid, &mp->m_sb.sb_uuid))
c517b3aa 128 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
129
130 if (sb->sb_logstart != cpu_to_be64(mp->m_sb.sb_logstart))
c517b3aa 131 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
132
133 if (sb->sb_rootino != cpu_to_be64(mp->m_sb.sb_rootino))
c517b3aa 134 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
135
136 if (sb->sb_rbmino != cpu_to_be64(mp->m_sb.sb_rbmino))
c517b3aa 137 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
138
139 if (sb->sb_rsumino != cpu_to_be64(mp->m_sb.sb_rsumino))
c517b3aa 140 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
141
142 if (sb->sb_rextsize != cpu_to_be32(mp->m_sb.sb_rextsize))
c517b3aa 143 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
144
145 if (sb->sb_agblocks != cpu_to_be32(mp->m_sb.sb_agblocks))
c517b3aa 146 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
147
148 if (sb->sb_agcount != cpu_to_be32(mp->m_sb.sb_agcount))
c517b3aa 149 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
150
151 if (sb->sb_rbmblocks != cpu_to_be32(mp->m_sb.sb_rbmblocks))
c517b3aa 152 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
153
154 if (sb->sb_logblocks != cpu_to_be32(mp->m_sb.sb_logblocks))
c517b3aa 155 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
156
157 /* Check sb_versionnum bits that are set at mkfs time. */
158 vernum_mask = cpu_to_be16(~XFS_SB_VERSION_OKBITS |
159 XFS_SB_VERSION_NUMBITS |
160 XFS_SB_VERSION_ALIGNBIT |
161 XFS_SB_VERSION_DALIGNBIT |
162 XFS_SB_VERSION_SHAREDBIT |
163 XFS_SB_VERSION_LOGV2BIT |
164 XFS_SB_VERSION_SECTORBIT |
165 XFS_SB_VERSION_EXTFLGBIT |
166 XFS_SB_VERSION_DIRV2BIT);
167 if ((sb->sb_versionnum & vernum_mask) !=
168 (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
c517b3aa 169 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
170
171 /* Check sb_versionnum bits that can be set after mkfs time. */
172 vernum_mask = cpu_to_be16(XFS_SB_VERSION_ATTRBIT |
173 XFS_SB_VERSION_NLINKBIT |
174 XFS_SB_VERSION_QUOTABIT);
175 if ((sb->sb_versionnum & vernum_mask) !=
176 (cpu_to_be16(mp->m_sb.sb_versionnum) & vernum_mask))
c517b3aa 177 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
178
179 if (sb->sb_sectsize != cpu_to_be16(mp->m_sb.sb_sectsize))
c517b3aa 180 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
181
182 if (sb->sb_inodesize != cpu_to_be16(mp->m_sb.sb_inodesize))
c517b3aa 183 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
184
185 if (sb->sb_inopblock != cpu_to_be16(mp->m_sb.sb_inopblock))
c517b3aa 186 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
187
188 if (memcmp(sb->sb_fname, mp->m_sb.sb_fname, sizeof(sb->sb_fname)))
c517b3aa 189 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
190
191 if (sb->sb_blocklog != mp->m_sb.sb_blocklog)
c517b3aa 192 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
193
194 if (sb->sb_sectlog != mp->m_sb.sb_sectlog)
c517b3aa 195 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
196
197 if (sb->sb_inodelog != mp->m_sb.sb_inodelog)
c517b3aa 198 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
199
200 if (sb->sb_inopblog != mp->m_sb.sb_inopblog)
c517b3aa 201 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
202
203 if (sb->sb_agblklog != mp->m_sb.sb_agblklog)
c517b3aa 204 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
205
206 if (sb->sb_rextslog != mp->m_sb.sb_rextslog)
c517b3aa 207 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
208
209 if (sb->sb_imax_pct != mp->m_sb.sb_imax_pct)
c517b3aa 210 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
211
212 /*
213 * Skip the summary counters since we track them in memory anyway.
214 * sb_icount, sb_ifree, sb_fdblocks, sb_frexents
215 */
216
217 if (sb->sb_uquotino != cpu_to_be64(mp->m_sb.sb_uquotino))
c517b3aa 218 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
219
220 if (sb->sb_gquotino != cpu_to_be64(mp->m_sb.sb_gquotino))
c517b3aa 221 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
222
223 /*
224 * Skip the quota flags since repair will force quotacheck.
225 * sb_qflags
226 */
227
228 if (sb->sb_flags != mp->m_sb.sb_flags)
c517b3aa 229 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
230
231 if (sb->sb_shared_vn != mp->m_sb.sb_shared_vn)
c517b3aa 232 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
233
234 if (sb->sb_inoalignmt != cpu_to_be32(mp->m_sb.sb_inoalignmt))
c517b3aa 235 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
236
237 if (sb->sb_unit != cpu_to_be32(mp->m_sb.sb_unit))
c517b3aa 238 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
239
240 if (sb->sb_width != cpu_to_be32(mp->m_sb.sb_width))
c517b3aa 241 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
242
243 if (sb->sb_dirblklog != mp->m_sb.sb_dirblklog)
c517b3aa 244 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
245
246 if (sb->sb_logsectlog != mp->m_sb.sb_logsectlog)
c517b3aa 247 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
248
249 if (sb->sb_logsectsize != cpu_to_be16(mp->m_sb.sb_logsectsize))
c517b3aa 250 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
251
252 if (sb->sb_logsunit != cpu_to_be32(mp->m_sb.sb_logsunit))
c517b3aa 253 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
254
255 /* Do we see any invalid bits in sb_features2? */
256 if (!xfs_sb_version_hasmorebits(&mp->m_sb)) {
257 if (sb->sb_features2 != 0)
c517b3aa 258 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
259 } else {
260 v2_ok = XFS_SB_VERSION2_OKBITS;
d6837c1a 261 if (xfs_sb_is_v5(&mp->m_sb))
21fb4cb1
DW
262 v2_ok |= XFS_SB_VERSION2_CRCBIT;
263
264 if (!!(sb->sb_features2 & cpu_to_be32(~v2_ok)))
c517b3aa 265 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
266
267 if (sb->sb_features2 != sb->sb_bad_features2)
c517b3aa 268 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
269 }
270
271 /* Check sb_features2 flags that are set at mkfs time. */
272 features_mask = cpu_to_be32(XFS_SB_VERSION2_LAZYSBCOUNTBIT |
273 XFS_SB_VERSION2_PROJID32BIT |
274 XFS_SB_VERSION2_CRCBIT |
275 XFS_SB_VERSION2_FTYPE);
276 if ((sb->sb_features2 & features_mask) !=
277 (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
c517b3aa 278 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
279
280 /* Check sb_features2 flags that can be set after mkfs time. */
281 features_mask = cpu_to_be32(XFS_SB_VERSION2_ATTR2BIT);
282 if ((sb->sb_features2 & features_mask) !=
283 (cpu_to_be32(mp->m_sb.sb_features2) & features_mask))
c517b3aa 284 xchk_block_set_corrupt(sc, bp);
21fb4cb1 285
38c26bfd 286 if (!xfs_has_crc(mp)) {
21fb4cb1
DW
287 /* all v5 fields must be zero */
288 if (memchr_inv(&sb->sb_features_compat, 0,
289 sizeof(struct xfs_dsb) -
290 offsetof(struct xfs_dsb, sb_features_compat)))
c517b3aa 291 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
292 } else {
293 /* Check compat flags; all are set at mkfs time. */
294 features_mask = cpu_to_be32(XFS_SB_FEAT_COMPAT_UNKNOWN);
295 if ((sb->sb_features_compat & features_mask) !=
296 (cpu_to_be32(mp->m_sb.sb_features_compat) & features_mask))
c517b3aa 297 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
298
299 /* Check ro compat flags; all are set at mkfs time. */
300 features_mask = cpu_to_be32(XFS_SB_FEAT_RO_COMPAT_UNKNOWN |
301 XFS_SB_FEAT_RO_COMPAT_FINOBT |
302 XFS_SB_FEAT_RO_COMPAT_RMAPBT |
303 XFS_SB_FEAT_RO_COMPAT_REFLINK);
304 if ((sb->sb_features_ro_compat & features_mask) !=
305 (cpu_to_be32(mp->m_sb.sb_features_ro_compat) &
306 features_mask))
c517b3aa 307 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
308
309 /* Check incompat flags; all are set at mkfs time. */
310 features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_UNKNOWN |
311 XFS_SB_FEAT_INCOMPAT_FTYPE |
312 XFS_SB_FEAT_INCOMPAT_SPINODES |
313 XFS_SB_FEAT_INCOMPAT_META_UUID);
314 if ((sb->sb_features_incompat & features_mask) !=
315 (cpu_to_be32(mp->m_sb.sb_features_incompat) &
316 features_mask))
c517b3aa 317 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
318
319 /* Check log incompat flags; all are set at mkfs time. */
320 features_mask = cpu_to_be32(XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN);
321 if ((sb->sb_features_log_incompat & features_mask) !=
322 (cpu_to_be32(mp->m_sb.sb_features_log_incompat) &
323 features_mask))
c517b3aa 324 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
325
326 /* Don't care about sb_crc */
327
328 if (sb->sb_spino_align != cpu_to_be32(mp->m_sb.sb_spino_align))
c517b3aa 329 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
330
331 if (sb->sb_pquotino != cpu_to_be64(mp->m_sb.sb_pquotino))
c517b3aa 332 xchk_block_set_preen(sc, bp);
21fb4cb1
DW
333
334 /* Don't care about sb_lsn */
335 }
336
38c26bfd 337 if (xfs_has_metauuid(mp)) {
21fb4cb1
DW
338 /* The metadata UUID must be the same for all supers */
339 if (!uuid_equal(&sb->sb_meta_uuid, &mp->m_sb.sb_meta_uuid))
c517b3aa 340 xchk_block_set_corrupt(sc, bp);
21fb4cb1
DW
341 }
342
343 /* Everything else must be zero. */
344 if (memchr_inv(sb + 1, 0,
345 BBTOB(bp->b_length) - sizeof(struct xfs_dsb)))
c517b3aa 346 xchk_block_set_corrupt(sc, bp);
21fb4cb1 347
c517b3aa 348 xchk_superblock_xref(sc, bp);
48c6615c
DW
349out_pag:
350 xfs_perag_put(pag);
21fb4cb1
DW
351 return error;
352}
ab9d5dc5
DW
353
354/* AGF */
355
52dc4b44
DW
356/* Tally freespace record lengths. */
357STATIC int
c517b3aa 358xchk_agf_record_bno_lengths(
52dc4b44 359 struct xfs_btree_cur *cur,
159eb69d 360 const struct xfs_alloc_rec_incore *rec,
52dc4b44
DW
361 void *priv)
362{
363 xfs_extlen_t *blocks = priv;
364
365 (*blocks) += rec->ar_blockcount;
366 return 0;
367}
368
369/* Check agf_freeblks */
370static inline void
c517b3aa 371xchk_agf_xref_freeblks(
1d8a748a 372 struct xfs_scrub *sc)
52dc4b44 373{
9798f615 374 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
032d91f9
DW
375 xfs_extlen_t blocks = 0;
376 int error;
52dc4b44
DW
377
378 if (!sc->sa.bno_cur)
379 return;
380
381 error = xfs_alloc_query_all(sc->sa.bno_cur,
c517b3aa
DW
382 xchk_agf_record_bno_lengths, &blocks);
383 if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
52dc4b44
DW
384 return;
385 if (blocks != be32_to_cpu(agf->agf_freeblks))
c517b3aa 386 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
52dc4b44
DW
387}
388
e1134b12
DW
389/* Cross reference the AGF with the cntbt (freespace by length btree) */
390static inline void
c517b3aa 391xchk_agf_xref_cntbt(
1d8a748a 392 struct xfs_scrub *sc)
e1134b12 393{
9798f615 394 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
032d91f9
DW
395 xfs_agblock_t agbno;
396 xfs_extlen_t blocks;
397 int have;
398 int error;
e1134b12
DW
399
400 if (!sc->sa.cnt_cur)
401 return;
402
403 /* Any freespace at all? */
404 error = xfs_alloc_lookup_le(sc->sa.cnt_cur, 0, -1U, &have);
c517b3aa 405 if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
e1134b12
DW
406 return;
407 if (!have) {
3d129e1b 408 if (agf->agf_freeblks != cpu_to_be32(0))
c517b3aa 409 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
e1134b12
DW
410 return;
411 }
412
413 /* Check agf_longest */
414 error = xfs_alloc_get_rec(sc->sa.cnt_cur, &agbno, &blocks, &have);
c517b3aa 415 if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
e1134b12
DW
416 return;
417 if (!have || blocks != be32_to_cpu(agf->agf_longest))
c517b3aa 418 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
e1134b12
DW
419}
420
d852657c
DW
421/* Check the btree block counts in the AGF against the btrees. */
422STATIC void
c517b3aa 423xchk_agf_xref_btreeblks(
1d8a748a 424 struct xfs_scrub *sc)
d852657c 425{
9798f615 426 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
032d91f9
DW
427 struct xfs_mount *mp = sc->mp;
428 xfs_agblock_t blocks;
429 xfs_agblock_t btreeblks;
430 int error;
d852657c 431
e6c01077 432 /* agf_btreeblks didn't exist before lazysbcount */
ebd9027d 433 if (!xfs_has_lazysbcount(sc->mp))
e6c01077
DW
434 return;
435
d852657c
DW
436 /* Check agf_rmap_blocks; set up for agf_btreeblks check */
437 if (sc->sa.rmap_cur) {
438 error = xfs_btree_count_blocks(sc->sa.rmap_cur, &blocks);
c517b3aa 439 if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
d852657c
DW
440 return;
441 btreeblks = blocks - 1;
442 if (blocks != be32_to_cpu(agf->agf_rmap_blocks))
c517b3aa 443 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
d852657c
DW
444 } else {
445 btreeblks = 0;
446 }
447
448 /*
449 * No rmap cursor; we can't xref if we have the rmapbt feature.
450 * We also can't do it if we're missing the free space btree cursors.
451 */
38c26bfd 452 if ((xfs_has_rmapbt(mp) && !sc->sa.rmap_cur) ||
d852657c
DW
453 !sc->sa.bno_cur || !sc->sa.cnt_cur)
454 return;
455
456 /* Check agf_btreeblks */
457 error = xfs_btree_count_blocks(sc->sa.bno_cur, &blocks);
c517b3aa 458 if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
d852657c
DW
459 return;
460 btreeblks += blocks - 1;
461
462 error = xfs_btree_count_blocks(sc->sa.cnt_cur, &blocks);
c517b3aa 463 if (!xchk_should_check_xref(sc, &error, &sc->sa.cnt_cur))
d852657c
DW
464 return;
465 btreeblks += blocks - 1;
466
467 if (btreeblks != be32_to_cpu(agf->agf_btreeblks))
c517b3aa 468 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
d852657c
DW
469}
470
f6d5fc21
DW
471/* Check agf_refcount_blocks against tree size */
472static inline void
c517b3aa 473xchk_agf_xref_refcblks(
1d8a748a 474 struct xfs_scrub *sc)
f6d5fc21 475{
9798f615 476 struct xfs_agf *agf = sc->sa.agf_bp->b_addr;
032d91f9
DW
477 xfs_agblock_t blocks;
478 int error;
f6d5fc21
DW
479
480 if (!sc->sa.refc_cur)
481 return;
482
483 error = xfs_btree_count_blocks(sc->sa.refc_cur, &blocks);
c517b3aa 484 if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
f6d5fc21
DW
485 return;
486 if (blocks != be32_to_cpu(agf->agf_refcount_blocks))
c517b3aa 487 xchk_block_xref_set_corrupt(sc, sc->sa.agf_bp);
f6d5fc21
DW
488}
489
166d7641
DW
490/* Cross-reference with the other btrees. */
491STATIC void
c517b3aa 492xchk_agf_xref(
1d8a748a 493 struct xfs_scrub *sc)
166d7641 494{
032d91f9
DW
495 struct xfs_mount *mp = sc->mp;
496 xfs_agblock_t agbno;
52dc4b44 497
166d7641
DW
498 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
499 return;
52dc4b44
DW
500
501 agbno = XFS_AGF_BLOCK(mp);
502
f53acfac 503 xchk_ag_btcur_init(sc, &sc->sa);
52dc4b44 504
c517b3aa
DW
505 xchk_xref_is_used_space(sc, agbno, 1);
506 xchk_agf_xref_freeblks(sc);
507 xchk_agf_xref_cntbt(sc);
508 xchk_xref_is_not_inode_chunk(sc, agbno, 1);
7280feda 509 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
c517b3aa
DW
510 xchk_agf_xref_btreeblks(sc);
511 xchk_xref_is_not_shared(sc, agbno, 1);
512 xchk_agf_xref_refcblks(sc);
52dc4b44
DW
513
514 /* scrub teardown will take care of sc->sa for us */
166d7641
DW
515}
516
ab9d5dc5
DW
517/* Scrub the AGF. */
518int
c517b3aa 519xchk_agf(
1d8a748a 520 struct xfs_scrub *sc)
ab9d5dc5 521{
032d91f9
DW
522 struct xfs_mount *mp = sc->mp;
523 struct xfs_agf *agf;
47cd97b5 524 struct xfs_perag *pag;
de9d2a78 525 xfs_agnumber_t agno = sc->sm->sm_agno;
032d91f9
DW
526 xfs_agblock_t agbno;
527 xfs_agblock_t eoag;
528 xfs_agblock_t agfl_first;
529 xfs_agblock_t agfl_last;
530 xfs_agblock_t agfl_count;
531 xfs_agblock_t fl_count;
532 int level;
533 int error = 0;
ab9d5dc5 534
de9d2a78 535 error = xchk_ag_read_headers(sc, agno, &sc->sa);
c517b3aa 536 if (!xchk_process_error(sc, agno, XFS_AGF_BLOCK(sc->mp), &error))
ab9d5dc5 537 goto out;
c517b3aa 538 xchk_buffer_recheck(sc, sc->sa.agf_bp);
ab9d5dc5 539
9798f615 540 agf = sc->sa.agf_bp->b_addr;
48c6615c 541 pag = sc->sa.pag;
ab9d5dc5
DW
542
543 /* Check the AG length */
544 eoag = be32_to_cpu(agf->agf_length);
545 if (eoag != xfs_ag_block_count(mp, agno))
c517b3aa 546 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
547
548 /* Check the AGF btree roots and levels */
549 agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_BNO]);
550 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 551 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
552
553 agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_CNT]);
554 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 555 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
556
557 level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_BNO]);
f4585e82 558 if (level <= 0 || level > mp->m_ag_maxlevels)
c517b3aa 559 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
560
561 level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_CNT]);
f4585e82 562 if (level <= 0 || level > mp->m_ag_maxlevels)
c517b3aa 563 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5 564
38c26bfd 565 if (xfs_has_rmapbt(mp)) {
ab9d5dc5
DW
566 agbno = be32_to_cpu(agf->agf_roots[XFS_BTNUM_RMAP]);
567 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 568 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
569
570 level = be32_to_cpu(agf->agf_levels[XFS_BTNUM_RMAP]);
f4585e82 571 if (level <= 0 || level > mp->m_rmap_maxlevels)
c517b3aa 572 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
573 }
574
38c26bfd 575 if (xfs_has_reflink(mp)) {
ab9d5dc5
DW
576 agbno = be32_to_cpu(agf->agf_refcount_root);
577 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 578 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
579
580 level = be32_to_cpu(agf->agf_refcount_level);
f4585e82 581 if (level <= 0 || level > mp->m_refc_maxlevels)
c517b3aa 582 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5
DW
583 }
584
585 /* Check the AGFL counters */
586 agfl_first = be32_to_cpu(agf->agf_flfirst);
587 agfl_last = be32_to_cpu(agf->agf_fllast);
588 agfl_count = be32_to_cpu(agf->agf_flcount);
589 if (agfl_last > agfl_first)
590 fl_count = agfl_last - agfl_first + 1;
591 else
a78ee256 592 fl_count = xfs_agfl_size(mp) - agfl_first + agfl_last + 1;
ab9d5dc5 593 if (agfl_count != 0 && fl_count != agfl_count)
c517b3aa 594 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ab9d5dc5 595
47cd97b5 596 /* Do the incore counters match? */
47cd97b5
DW
597 if (pag->pagf_freeblks != be32_to_cpu(agf->agf_freeblks))
598 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
599 if (pag->pagf_flcount != be32_to_cpu(agf->agf_flcount))
600 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
ebd9027d 601 if (xfs_has_lazysbcount(sc->mp) &&
e6c01077 602 pag->pagf_btreeblks != be32_to_cpu(agf->agf_btreeblks))
47cd97b5 603 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
47cd97b5 604
c517b3aa 605 xchk_agf_xref(sc);
ab9d5dc5
DW
606out:
607 return error;
608}
609
610/* AGFL */
611
c517b3aa 612struct xchk_agfl_info {
032d91f9
DW
613 unsigned int sz_entries;
614 unsigned int nr_entries;
615 xfs_agblock_t *entries;
1d8a748a 616 struct xfs_scrub *sc;
d44b47fd
DW
617};
618
166d7641
DW
619/* Cross-reference with the other btrees. */
620STATIC void
c517b3aa 621xchk_agfl_block_xref(
1d8a748a 622 struct xfs_scrub *sc,
7280feda 623 xfs_agblock_t agbno)
166d7641
DW
624{
625 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
626 return;
52dc4b44 627
c517b3aa
DW
628 xchk_xref_is_used_space(sc, agbno, 1);
629 xchk_xref_is_not_inode_chunk(sc, agbno, 1);
7280feda 630 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_AG);
c517b3aa 631 xchk_xref_is_not_shared(sc, agbno, 1);
166d7641
DW
632}
633
ab9d5dc5
DW
634/* Scrub an AGFL block. */
635STATIC int
c517b3aa 636xchk_agfl_block(
032d91f9
DW
637 struct xfs_mount *mp,
638 xfs_agblock_t agbno,
639 void *priv)
ab9d5dc5 640{
032d91f9 641 struct xchk_agfl_info *sai = priv;
1d8a748a 642 struct xfs_scrub *sc = sai->sc;
54406764 643 xfs_agnumber_t agno = sc->sa.pag->pag_agno;
ab9d5dc5 644
d44b47fd
DW
645 if (xfs_verify_agbno(mp, agno, agbno) &&
646 sai->nr_entries < sai->sz_entries)
647 sai->entries[sai->nr_entries++] = agbno;
648 else
c517b3aa 649 xchk_block_set_corrupt(sc, sc->sa.agfl_bp);
ab9d5dc5 650
7280feda 651 xchk_agfl_block_xref(sc, agbno);
166d7641 652
9f3a080e 653 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
e7ee96df 654 return -ECANCELED;
9f3a080e 655
ab9d5dc5
DW
656 return 0;
657}
658
d44b47fd 659static int
c517b3aa 660xchk_agblock_cmp(
d44b47fd
DW
661 const void *pa,
662 const void *pb)
663{
664 const xfs_agblock_t *a = pa;
665 const xfs_agblock_t *b = pb;
666
667 return (int)*a - (int)*b;
668}
669
166d7641
DW
670/* Cross-reference with the other btrees. */
671STATIC void
c517b3aa 672xchk_agfl_xref(
1d8a748a 673 struct xfs_scrub *sc)
166d7641 674{
032d91f9
DW
675 struct xfs_mount *mp = sc->mp;
676 xfs_agblock_t agbno;
52dc4b44 677
166d7641
DW
678 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
679 return;
52dc4b44
DW
680
681 agbno = XFS_AGFL_BLOCK(mp);
682
f53acfac 683 xchk_ag_btcur_init(sc, &sc->sa);
52dc4b44 684
c517b3aa
DW
685 xchk_xref_is_used_space(sc, agbno, 1);
686 xchk_xref_is_not_inode_chunk(sc, agbno, 1);
7280feda 687 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
c517b3aa 688 xchk_xref_is_not_shared(sc, agbno, 1);
52dc4b44
DW
689
690 /*
691 * Scrub teardown will take care of sc->sa for us. Leave sc->sa
692 * active so that the agfl block xref can use it too.
693 */
166d7641
DW
694}
695
ab9d5dc5
DW
696/* Scrub the AGFL. */
697int
c517b3aa 698xchk_agfl(
1d8a748a 699 struct xfs_scrub *sc)
ab9d5dc5 700{
032d91f9
DW
701 struct xchk_agfl_info sai;
702 struct xfs_agf *agf;
de9d2a78 703 xfs_agnumber_t agno = sc->sm->sm_agno;
032d91f9
DW
704 unsigned int agflcount;
705 unsigned int i;
706 int error;
ab9d5dc5 707
de9d2a78 708 error = xchk_ag_read_headers(sc, agno, &sc->sa);
c517b3aa 709 if (!xchk_process_error(sc, agno, XFS_AGFL_BLOCK(sc->mp), &error))
ab9d5dc5
DW
710 goto out;
711 if (!sc->sa.agf_bp)
712 return -EFSCORRUPTED;
c517b3aa 713 xchk_buffer_recheck(sc, sc->sa.agfl_bp);
ab9d5dc5 714
c517b3aa 715 xchk_agfl_xref(sc);
166d7641
DW
716
717 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
718 goto out;
719
d44b47fd 720 /* Allocate buffer to ensure uniqueness of AGFL entries. */
9798f615 721 agf = sc->sa.agf_bp->b_addr;
d44b47fd 722 agflcount = be32_to_cpu(agf->agf_flcount);
a78ee256 723 if (agflcount > xfs_agfl_size(sc->mp)) {
c517b3aa 724 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
d44b47fd
DW
725 goto out;
726 }
86516eff 727 memset(&sai, 0, sizeof(sai));
9f3a080e 728 sai.sc = sc;
d44b47fd 729 sai.sz_entries = agflcount;
631fc955
DW
730 sai.entries = kmem_zalloc(sizeof(xfs_agblock_t) * agflcount,
731 KM_MAYFAIL);
d44b47fd
DW
732 if (!sai.entries) {
733 error = -ENOMEM;
734 goto out;
735 }
736
ab9d5dc5 737 /* Check the blocks in the AGFL. */
9798f615 738 error = xfs_agfl_walk(sc->mp, sc->sa.agf_bp->b_addr,
c517b3aa 739 sc->sa.agfl_bp, xchk_agfl_block, &sai);
e7ee96df 740 if (error == -ECANCELED) {
9f3a080e
DW
741 error = 0;
742 goto out_free;
743 }
d44b47fd
DW
744 if (error)
745 goto out_free;
746
747 if (agflcount != sai.nr_entries) {
c517b3aa 748 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
d44b47fd
DW
749 goto out_free;
750 }
751
752 /* Sort entries, check for duplicates. */
753 sort(sai.entries, sai.nr_entries, sizeof(sai.entries[0]),
c517b3aa 754 xchk_agblock_cmp, NULL);
d44b47fd
DW
755 for (i = 1; i < sai.nr_entries; i++) {
756 if (sai.entries[i] == sai.entries[i - 1]) {
c517b3aa 757 xchk_block_set_corrupt(sc, sc->sa.agf_bp);
d44b47fd
DW
758 break;
759 }
760 }
761
762out_free:
763 kmem_free(sai.entries);
ab9d5dc5
DW
764out:
765 return error;
766}
a12890ae
DW
767
768/* AGI */
769
2e6f2756
DW
770/* Check agi_count/agi_freecount */
771static inline void
c517b3aa 772xchk_agi_xref_icounts(
1d8a748a 773 struct xfs_scrub *sc)
2e6f2756 774{
370c782b 775 struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
032d91f9
DW
776 xfs_agino_t icount;
777 xfs_agino_t freecount;
778 int error;
2e6f2756
DW
779
780 if (!sc->sa.ino_cur)
781 return;
782
783 error = xfs_ialloc_count_inodes(sc->sa.ino_cur, &icount, &freecount);
c517b3aa 784 if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
2e6f2756
DW
785 return;
786 if (be32_to_cpu(agi->agi_count) != icount ||
787 be32_to_cpu(agi->agi_freecount) != freecount)
c517b3aa 788 xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
2e6f2756
DW
789}
790
1dbbff02
DW
791/* Check agi_[fi]blocks against tree size */
792static inline void
793xchk_agi_xref_fiblocks(
794 struct xfs_scrub *sc)
795{
796 struct xfs_agi *agi = sc->sa.agi_bp->b_addr;
797 xfs_agblock_t blocks;
798 int error = 0;
799
ebd9027d 800 if (!xfs_has_inobtcounts(sc->mp))
1dbbff02
DW
801 return;
802
803 if (sc->sa.ino_cur) {
804 error = xfs_btree_count_blocks(sc->sa.ino_cur, &blocks);
805 if (!xchk_should_check_xref(sc, &error, &sc->sa.ino_cur))
806 return;
807 if (blocks != be32_to_cpu(agi->agi_iblocks))
808 xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
809 }
810
811 if (sc->sa.fino_cur) {
812 error = xfs_btree_count_blocks(sc->sa.fino_cur, &blocks);
813 if (!xchk_should_check_xref(sc, &error, &sc->sa.fino_cur))
814 return;
815 if (blocks != be32_to_cpu(agi->agi_fblocks))
816 xchk_block_xref_set_corrupt(sc, sc->sa.agi_bp);
817 }
818}
819
166d7641
DW
820/* Cross-reference with the other btrees. */
821STATIC void
c517b3aa 822xchk_agi_xref(
1d8a748a 823 struct xfs_scrub *sc)
166d7641 824{
032d91f9
DW
825 struct xfs_mount *mp = sc->mp;
826 xfs_agblock_t agbno;
52dc4b44 827
166d7641
DW
828 if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
829 return;
52dc4b44
DW
830
831 agbno = XFS_AGI_BLOCK(mp);
832
f53acfac 833 xchk_ag_btcur_init(sc, &sc->sa);
52dc4b44 834
c517b3aa
DW
835 xchk_xref_is_used_space(sc, agbno, 1);
836 xchk_xref_is_not_inode_chunk(sc, agbno, 1);
837 xchk_agi_xref_icounts(sc);
7280feda 838 xchk_xref_is_owned_by(sc, agbno, 1, &XFS_RMAP_OINFO_FS);
c517b3aa 839 xchk_xref_is_not_shared(sc, agbno, 1);
1dbbff02 840 xchk_agi_xref_fiblocks(sc);
52dc4b44
DW
841
842 /* scrub teardown will take care of sc->sa for us */
166d7641
DW
843}
844
a12890ae
DW
845/* Scrub the AGI. */
846int
c517b3aa 847xchk_agi(
1d8a748a 848 struct xfs_scrub *sc)
a12890ae 849{
032d91f9
DW
850 struct xfs_mount *mp = sc->mp;
851 struct xfs_agi *agi;
47cd97b5 852 struct xfs_perag *pag;
f4585e82 853 struct xfs_ino_geometry *igeo = M_IGEO(sc->mp);
de9d2a78 854 xfs_agnumber_t agno = sc->sm->sm_agno;
032d91f9
DW
855 xfs_agblock_t agbno;
856 xfs_agblock_t eoag;
857 xfs_agino_t agino;
858 xfs_agino_t first_agino;
859 xfs_agino_t last_agino;
860 xfs_agino_t icount;
861 int i;
862 int level;
863 int error = 0;
a12890ae 864
de9d2a78 865 error = xchk_ag_read_headers(sc, agno, &sc->sa);
c517b3aa 866 if (!xchk_process_error(sc, agno, XFS_AGI_BLOCK(sc->mp), &error))
a12890ae 867 goto out;
c517b3aa 868 xchk_buffer_recheck(sc, sc->sa.agi_bp);
a12890ae 869
370c782b 870 agi = sc->sa.agi_bp->b_addr;
48c6615c 871 pag = sc->sa.pag;
a12890ae
DW
872
873 /* Check the AG length */
874 eoag = be32_to_cpu(agi->agi_length);
875 if (eoag != xfs_ag_block_count(mp, agno))
c517b3aa 876 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
877
878 /* Check btree roots and levels */
879 agbno = be32_to_cpu(agi->agi_root);
880 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 881 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
882
883 level = be32_to_cpu(agi->agi_level);
f4585e82 884 if (level <= 0 || level > igeo->inobt_maxlevels)
c517b3aa 885 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae 886
38c26bfd 887 if (xfs_has_finobt(mp)) {
a12890ae
DW
888 agbno = be32_to_cpu(agi->agi_free_root);
889 if (!xfs_verify_agbno(mp, agno, agbno))
c517b3aa 890 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
891
892 level = be32_to_cpu(agi->agi_free_level);
f4585e82 893 if (level <= 0 || level > igeo->inobt_maxlevels)
c517b3aa 894 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
895 }
896
897 /* Check inode counters */
86210fbe 898 xfs_agino_range(mp, agno, &first_agino, &last_agino);
a12890ae
DW
899 icount = be32_to_cpu(agi->agi_count);
900 if (icount > last_agino - first_agino + 1 ||
901 icount < be32_to_cpu(agi->agi_freecount))
c517b3aa 902 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
903
904 /* Check inode pointers */
905 agino = be32_to_cpu(agi->agi_newino);
7d36c195 906 if (!xfs_verify_agino_or_null(mp, agno, agino))
c517b3aa 907 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
908
909 agino = be32_to_cpu(agi->agi_dirino);
7d36c195 910 if (!xfs_verify_agino_or_null(mp, agno, agino))
c517b3aa 911 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
912
913 /* Check unlinked inode buckets */
914 for (i = 0; i < XFS_AGI_UNLINKED_BUCKETS; i++) {
915 agino = be32_to_cpu(agi->agi_unlinked[i]);
7d36c195 916 if (!xfs_verify_agino_or_null(mp, agno, agino))
c517b3aa 917 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae
DW
918 }
919
920 if (agi->agi_pad32 != cpu_to_be32(0))
c517b3aa 921 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
a12890ae 922
47cd97b5 923 /* Do the incore counters match? */
47cd97b5
DW
924 if (pag->pagi_count != be32_to_cpu(agi->agi_count))
925 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
926 if (pag->pagi_freecount != be32_to_cpu(agi->agi_freecount))
927 xchk_block_set_corrupt(sc, sc->sa.agi_bp);
47cd97b5 928
c517b3aa 929 xchk_agi_xref(sc);
a12890ae
DW
930out:
931 return error;
932}