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