• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_log_format.h"
14 #include "xfs_trans.h"
15 #include "xfs_inode.h"
16 #include "xfs_ialloc.h"
17 #include "xfs_ialloc_btree.h"
18 #include "xfs_icache.h"
19 #include "xfs_rmap.h"
20 #include "scrub/scrub.h"
21 #include "scrub/common.h"
22 #include "scrub/btree.h"
23 #include "scrub/trace.h"
24 
25 /*
26  * Set us up to scrub inode btrees.
27  * If we detect a discrepancy between the inobt and the inode,
28  * try again after forcing logged inode cores out to disk.
29  */
30 int
xchk_setup_ag_iallocbt(struct xfs_scrub * sc,struct xfs_inode * ip)31 xchk_setup_ag_iallocbt(
32 	struct xfs_scrub	*sc,
33 	struct xfs_inode	*ip)
34 {
35 	return xchk_setup_ag_btree(sc, ip, sc->flags & XCHK_TRY_HARDER);
36 }
37 
38 /* Inode btree scrubber. */
39 
40 struct xchk_iallocbt {
41 	/* Number of inodes we see while scanning inobt. */
42 	unsigned long long	inodes;
43 
44 	/* Expected next startino, for big block filesystems. */
45 	xfs_agino_t		next_startino;
46 
47 	/* Expected end of the current inode cluster. */
48 	xfs_agino_t		next_cluster_ino;
49 };
50 
51 /*
52  * If we're checking the finobt, cross-reference with the inobt.
53  * Otherwise we're checking the inobt; if there is an finobt, make sure
54  * we have a record or not depending on freecount.
55  */
56 static inline void
xchk_iallocbt_chunk_xref_other(struct xfs_scrub * sc,struct xfs_inobt_rec_incore * irec,xfs_agino_t agino)57 xchk_iallocbt_chunk_xref_other(
58 	struct xfs_scrub		*sc,
59 	struct xfs_inobt_rec_incore	*irec,
60 	xfs_agino_t			agino)
61 {
62 	struct xfs_btree_cur		**pcur;
63 	bool				has_irec;
64 	int				error;
65 
66 	if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
67 		pcur = &sc->sa.ino_cur;
68 	else
69 		pcur = &sc->sa.fino_cur;
70 	if (!(*pcur))
71 		return;
72 	error = xfs_ialloc_has_inode_record(*pcur, agino, agino, &has_irec);
73 	if (!xchk_should_check_xref(sc, &error, pcur))
74 		return;
75 	if (((irec->ir_freecount > 0 && !has_irec) ||
76 	     (irec->ir_freecount == 0 && has_irec)))
77 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
78 }
79 
80 /* Cross-reference with the other btrees. */
81 STATIC void
xchk_iallocbt_chunk_xref(struct xfs_scrub * sc,struct xfs_inobt_rec_incore * irec,xfs_agino_t agino,xfs_agblock_t agbno,xfs_extlen_t len)82 xchk_iallocbt_chunk_xref(
83 	struct xfs_scrub		*sc,
84 	struct xfs_inobt_rec_incore	*irec,
85 	xfs_agino_t			agino,
86 	xfs_agblock_t			agbno,
87 	xfs_extlen_t			len)
88 {
89 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
90 		return;
91 
92 	xchk_xref_is_used_space(sc, agbno, len);
93 	xchk_iallocbt_chunk_xref_other(sc, irec, agino);
94 	xchk_xref_is_owned_by(sc, agbno, len, &XFS_RMAP_OINFO_INODES);
95 	xchk_xref_is_not_shared(sc, agbno, len);
96 }
97 
98 /* Is this chunk worth checking? */
99 STATIC bool
xchk_iallocbt_chunk(struct xchk_btree * bs,struct xfs_inobt_rec_incore * irec,xfs_agino_t agino,xfs_extlen_t len)100 xchk_iallocbt_chunk(
101 	struct xchk_btree		*bs,
102 	struct xfs_inobt_rec_incore	*irec,
103 	xfs_agino_t			agino,
104 	xfs_extlen_t			len)
105 {
106 	struct xfs_mount		*mp = bs->cur->bc_mp;
107 	xfs_agnumber_t			agno = bs->cur->bc_ag.agno;
108 	xfs_agblock_t			bno;
109 
110 	bno = XFS_AGINO_TO_AGBNO(mp, agino);
111 	if (bno + len <= bno ||
112 	    !xfs_verify_agbno(mp, agno, bno) ||
113 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
114 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
115 
116 	xchk_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len);
117 
118 	return true;
119 }
120 
121 /* Count the number of free inodes. */
122 static unsigned int
xchk_iallocbt_freecount(xfs_inofree_t freemask)123 xchk_iallocbt_freecount(
124 	xfs_inofree_t			freemask)
125 {
126 	BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64));
127 	return hweight64(freemask);
128 }
129 
130 /*
131  * Check that an inode's allocation status matches ir_free in the inobt
132  * record.  First we try querying the in-core inode state, and if the inode
133  * isn't loaded we examine the on-disk inode directly.
134  *
135  * Since there can be 1:M and M:1 mappings between inobt records and inode
136  * clusters, we pass in the inode location information as an inobt record;
137  * the index of an inode cluster within the inobt record (as well as the
138  * cluster buffer itself); and the index of the inode within the cluster.
139  *
140  * @irec is the inobt record.
141  * @irec_ino is the inode offset from the start of the record.
142  * @dip is the on-disk inode.
143  */
144 STATIC int
xchk_iallocbt_check_cluster_ifree(struct xchk_btree * bs,struct xfs_inobt_rec_incore * irec,unsigned int irec_ino,struct xfs_dinode * dip)145 xchk_iallocbt_check_cluster_ifree(
146 	struct xchk_btree		*bs,
147 	struct xfs_inobt_rec_incore	*irec,
148 	unsigned int			irec_ino,
149 	struct xfs_dinode		*dip)
150 {
151 	struct xfs_mount		*mp = bs->cur->bc_mp;
152 	xfs_ino_t			fsino;
153 	xfs_agino_t			agino;
154 	bool				irec_free;
155 	bool				ino_inuse;
156 	bool				freemask_ok;
157 	int				error = 0;
158 
159 	if (xchk_should_terminate(bs->sc, &error))
160 		return error;
161 
162 	/*
163 	 * Given an inobt record and the offset of an inode from the start of
164 	 * the record, compute which fs inode we're talking about.
165 	 */
166 	agino = irec->ir_startino + irec_ino;
167 	fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_ag.agno, agino);
168 	irec_free = (irec->ir_free & XFS_INOBT_MASK(irec_ino));
169 
170 	if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
171 	    (dip->di_version >= 3 && be64_to_cpu(dip->di_ino) != fsino)) {
172 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
173 		goto out;
174 	}
175 
176 	error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp, fsino,
177 			&ino_inuse);
178 	if (error == -ENODATA) {
179 		/* Not cached, just read the disk buffer */
180 		freemask_ok = irec_free ^ !!(dip->di_mode);
181 		if (!(bs->sc->flags & XCHK_TRY_HARDER) && !freemask_ok)
182 			return -EDEADLOCK;
183 	} else if (error < 0) {
184 		/*
185 		 * Inode is only half assembled, or there was an IO error,
186 		 * or the verifier failed, so don't bother trying to check.
187 		 * The inode scrubber can deal with this.
188 		 */
189 		goto out;
190 	} else {
191 		/* Inode is all there. */
192 		freemask_ok = irec_free ^ ino_inuse;
193 	}
194 	if (!freemask_ok)
195 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
196 out:
197 	return 0;
198 }
199 
200 /*
201  * Check that the holemask and freemask of a hypothetical inode cluster match
202  * what's actually on disk.  If sparse inodes are enabled, the cluster does
203  * not actually have to map to inodes if the corresponding holemask bit is set.
204  *
205  * @cluster_base is the first inode in the cluster within the @irec.
206  */
207 STATIC int
xchk_iallocbt_check_cluster(struct xchk_btree * bs,struct xfs_inobt_rec_incore * irec,unsigned int cluster_base)208 xchk_iallocbt_check_cluster(
209 	struct xchk_btree		*bs,
210 	struct xfs_inobt_rec_incore	*irec,
211 	unsigned int			cluster_base)
212 {
213 	struct xfs_imap			imap;
214 	struct xfs_mount		*mp = bs->cur->bc_mp;
215 	struct xfs_dinode		*dip;
216 	struct xfs_buf			*cluster_bp;
217 	unsigned int			nr_inodes;
218 	xfs_agnumber_t			agno = bs->cur->bc_ag.agno;
219 	xfs_agblock_t			agbno;
220 	unsigned int			cluster_index;
221 	uint16_t			cluster_mask = 0;
222 	uint16_t			ir_holemask;
223 	int				error = 0;
224 
225 	nr_inodes = min_t(unsigned int, XFS_INODES_PER_CHUNK,
226 			M_IGEO(mp)->inodes_per_cluster);
227 
228 	/* Map this inode cluster */
229 	agbno = XFS_AGINO_TO_AGBNO(mp, irec->ir_startino + cluster_base);
230 
231 	/* Compute a bitmask for this cluster that can be used for holemask. */
232 	for (cluster_index = 0;
233 	     cluster_index < nr_inodes;
234 	     cluster_index += XFS_INODES_PER_HOLEMASK_BIT)
235 		cluster_mask |= XFS_INOBT_MASK((cluster_base + cluster_index) /
236 				XFS_INODES_PER_HOLEMASK_BIT);
237 
238 	/*
239 	 * Map the first inode of this cluster to a buffer and offset.
240 	 * Be careful about inobt records that don't align with the start of
241 	 * the inode buffer when block sizes are large enough to hold multiple
242 	 * inode chunks.  When this happens, cluster_base will be zero but
243 	 * ir_startino can be large enough to make im_boffset nonzero.
244 	 */
245 	ir_holemask = (irec->ir_holemask & cluster_mask);
246 	imap.im_blkno = XFS_AGB_TO_DADDR(mp, agno, agbno);
247 	imap.im_len = XFS_FSB_TO_BB(mp, M_IGEO(mp)->blocks_per_cluster);
248 	imap.im_boffset = XFS_INO_TO_OFFSET(mp, irec->ir_startino) <<
249 			mp->m_sb.sb_inodelog;
250 
251 	if (imap.im_boffset != 0 && cluster_base != 0) {
252 		ASSERT(imap.im_boffset == 0 || cluster_base == 0);
253 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
254 		return 0;
255 	}
256 
257 	trace_xchk_iallocbt_check_cluster(mp, agno, irec->ir_startino,
258 			imap.im_blkno, imap.im_len, cluster_base, nr_inodes,
259 			cluster_mask, ir_holemask,
260 			XFS_INO_TO_OFFSET(mp, irec->ir_startino +
261 					  cluster_base));
262 
263 	/* The whole cluster must be a hole or not a hole. */
264 	if (ir_holemask != cluster_mask && ir_holemask != 0) {
265 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
266 		return 0;
267 	}
268 
269 	/* If any part of this is a hole, skip it. */
270 	if (ir_holemask) {
271 		xchk_xref_is_not_owned_by(bs->sc, agbno,
272 				M_IGEO(mp)->blocks_per_cluster,
273 				&XFS_RMAP_OINFO_INODES);
274 		return 0;
275 	}
276 
277 	xchk_xref_is_owned_by(bs->sc, agbno, M_IGEO(mp)->blocks_per_cluster,
278 			&XFS_RMAP_OINFO_INODES);
279 
280 	/* Grab the inode cluster buffer. */
281 	error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap, &dip, &cluster_bp, 0);
282 	if (!xchk_btree_xref_process_error(bs->sc, bs->cur, 0, &error))
283 		return error;
284 
285 	/* Check free status of each inode within this cluster. */
286 	for (cluster_index = 0; cluster_index < nr_inodes; cluster_index++) {
287 		struct xfs_dinode	*dip;
288 
289 		if (imap.im_boffset >= BBTOB(cluster_bp->b_length)) {
290 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
291 			break;
292 		}
293 
294 		dip = xfs_buf_offset(cluster_bp, imap.im_boffset);
295 		error = xchk_iallocbt_check_cluster_ifree(bs, irec,
296 				cluster_base + cluster_index, dip);
297 		if (error)
298 			break;
299 		imap.im_boffset += mp->m_sb.sb_inodesize;
300 	}
301 
302 	xfs_trans_brelse(bs->cur->bc_tp, cluster_bp);
303 	return error;
304 }
305 
306 /*
307  * For all the inode clusters that could map to this inobt record, make sure
308  * that the holemask makes sense and that the allocation status of each inode
309  * matches the freemask.
310  */
311 STATIC int
xchk_iallocbt_check_clusters(struct xchk_btree * bs,struct xfs_inobt_rec_incore * irec)312 xchk_iallocbt_check_clusters(
313 	struct xchk_btree		*bs,
314 	struct xfs_inobt_rec_incore	*irec)
315 {
316 	unsigned int			cluster_base;
317 	int				error = 0;
318 
319 	/*
320 	 * For the common case where this inobt record maps to multiple inode
321 	 * clusters this will call _check_cluster for each cluster.
322 	 *
323 	 * For the case that multiple inobt records map to a single cluster,
324 	 * this will call _check_cluster once.
325 	 */
326 	for (cluster_base = 0;
327 	     cluster_base < XFS_INODES_PER_CHUNK;
328 	     cluster_base += M_IGEO(bs->sc->mp)->inodes_per_cluster) {
329 		error = xchk_iallocbt_check_cluster(bs, irec, cluster_base);
330 		if (error)
331 			break;
332 	}
333 
334 	return error;
335 }
336 
337 /*
338  * Make sure this inode btree record is aligned properly.  Because a fs block
339  * contains multiple inodes, we check that the inobt record is aligned to the
340  * correct inode, not just the correct block on disk.  This results in a finer
341  * grained corruption check.
342  */
343 STATIC void
xchk_iallocbt_rec_alignment(struct xchk_btree * bs,struct xfs_inobt_rec_incore * irec)344 xchk_iallocbt_rec_alignment(
345 	struct xchk_btree		*bs,
346 	struct xfs_inobt_rec_incore	*irec)
347 {
348 	struct xfs_mount		*mp = bs->sc->mp;
349 	struct xchk_iallocbt		*iabt = bs->private;
350 	struct xfs_ino_geometry		*igeo = M_IGEO(mp);
351 
352 	/*
353 	 * finobt records have different positioning requirements than inobt
354 	 * records: each finobt record must have a corresponding inobt record.
355 	 * That is checked in the xref function, so for now we only catch the
356 	 * obvious case where the record isn't at all aligned properly.
357 	 *
358 	 * Note that if a fs block contains more than a single chunk of inodes,
359 	 * we will have finobt records only for those chunks containing free
360 	 * inodes, and therefore expect chunk alignment of finobt records.
361 	 * Otherwise, we expect that the finobt record is aligned to the
362 	 * cluster alignment as told by the superblock.
363 	 */
364 	if (bs->cur->bc_btnum == XFS_BTNUM_FINO) {
365 		unsigned int	imask;
366 
367 		imask = min_t(unsigned int, XFS_INODES_PER_CHUNK,
368 				igeo->cluster_align_inodes) - 1;
369 		if (irec->ir_startino & imask)
370 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
371 		return;
372 	}
373 
374 	if (iabt->next_startino != NULLAGINO) {
375 		/*
376 		 * We're midway through a cluster of inodes that is mapped by
377 		 * multiple inobt records.  Did we get the record for the next
378 		 * irec in the sequence?
379 		 */
380 		if (irec->ir_startino != iabt->next_startino) {
381 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
382 			return;
383 		}
384 
385 		iabt->next_startino += XFS_INODES_PER_CHUNK;
386 
387 		/* Are we done with the cluster? */
388 		if (iabt->next_startino >= iabt->next_cluster_ino) {
389 			iabt->next_startino = NULLAGINO;
390 			iabt->next_cluster_ino = NULLAGINO;
391 		}
392 		return;
393 	}
394 
395 	/* inobt records must be aligned to cluster and inoalignmnt size. */
396 	if (irec->ir_startino & (igeo->cluster_align_inodes - 1)) {
397 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
398 		return;
399 	}
400 
401 	if (irec->ir_startino & (igeo->inodes_per_cluster - 1)) {
402 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
403 		return;
404 	}
405 
406 	if (igeo->inodes_per_cluster <= XFS_INODES_PER_CHUNK)
407 		return;
408 
409 	/*
410 	 * If this is the start of an inode cluster that can be mapped by
411 	 * multiple inobt records, the next inobt record must follow exactly
412 	 * after this one.
413 	 */
414 	iabt->next_startino = irec->ir_startino + XFS_INODES_PER_CHUNK;
415 	iabt->next_cluster_ino = irec->ir_startino + igeo->inodes_per_cluster;
416 }
417 
418 /* Scrub an inobt/finobt record. */
419 STATIC int
xchk_iallocbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)420 xchk_iallocbt_rec(
421 	struct xchk_btree		*bs,
422 	union xfs_btree_rec		*rec)
423 {
424 	struct xfs_mount		*mp = bs->cur->bc_mp;
425 	struct xchk_iallocbt		*iabt = bs->private;
426 	struct xfs_inobt_rec_incore	irec;
427 	uint64_t			holes;
428 	xfs_agnumber_t			agno = bs->cur->bc_ag.agno;
429 	xfs_agino_t			agino;
430 	xfs_extlen_t			len;
431 	int				holecount;
432 	int				i;
433 	int				error = 0;
434 	unsigned int			real_freecount;
435 	uint16_t			holemask;
436 
437 	xfs_inobt_btrec_to_irec(mp, rec, &irec);
438 
439 	if (irec.ir_count > XFS_INODES_PER_CHUNK ||
440 	    irec.ir_freecount > XFS_INODES_PER_CHUNK)
441 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
442 
443 	real_freecount = irec.ir_freecount +
444 			(XFS_INODES_PER_CHUNK - irec.ir_count);
445 	if (real_freecount != xchk_iallocbt_freecount(irec.ir_free))
446 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
447 
448 	agino = irec.ir_startino;
449 	/* Record has to be properly aligned within the AG. */
450 	if (!xfs_verify_agino(mp, agno, agino) ||
451 	    !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) {
452 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
453 		goto out;
454 	}
455 
456 	xchk_iallocbt_rec_alignment(bs, &irec);
457 	if (bs->sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
458 		goto out;
459 
460 	iabt->inodes += irec.ir_count;
461 
462 	/* Handle non-sparse inodes */
463 	if (!xfs_inobt_issparse(irec.ir_holemask)) {
464 		len = XFS_B_TO_FSB(mp,
465 				XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize);
466 		if (irec.ir_count != XFS_INODES_PER_CHUNK)
467 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
468 
469 		if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
470 			goto out;
471 		goto check_clusters;
472 	}
473 
474 	/* Check each chunk of a sparse inode cluster. */
475 	holemask = irec.ir_holemask;
476 	holecount = 0;
477 	len = XFS_B_TO_FSB(mp,
478 			XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize);
479 	holes = ~xfs_inobt_irec_to_allocmask(&irec);
480 	if ((holes & irec.ir_free) != holes ||
481 	    irec.ir_freecount > irec.ir_count)
482 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
483 
484 	for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
485 		if (holemask & 1)
486 			holecount += XFS_INODES_PER_HOLEMASK_BIT;
487 		else if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
488 			break;
489 		holemask >>= 1;
490 		agino += XFS_INODES_PER_HOLEMASK_BIT;
491 	}
492 
493 	if (holecount > XFS_INODES_PER_CHUNK ||
494 	    holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
495 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
496 
497 check_clusters:
498 	error = xchk_iallocbt_check_clusters(bs, &irec);
499 	if (error)
500 		goto out;
501 
502 out:
503 	return error;
504 }
505 
506 /*
507  * Make sure the inode btrees are as large as the rmap thinks they are.
508  * Don't bother if we're missing btree cursors, as we're already corrupt.
509  */
510 STATIC void
xchk_iallocbt_xref_rmap_btreeblks(struct xfs_scrub * sc,int which)511 xchk_iallocbt_xref_rmap_btreeblks(
512 	struct xfs_scrub	*sc,
513 	int			which)
514 {
515 	xfs_filblks_t		blocks;
516 	xfs_extlen_t		inobt_blocks = 0;
517 	xfs_extlen_t		finobt_blocks = 0;
518 	int			error;
519 
520 	if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
521 	    (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->sa.fino_cur) ||
522 	    xchk_skip_xref(sc->sm))
523 		return;
524 
525 	/* Check that we saw as many inobt blocks as the rmap says. */
526 	error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
527 	if (!xchk_process_error(sc, 0, 0, &error))
528 		return;
529 
530 	if (sc->sa.fino_cur) {
531 		error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
532 		if (!xchk_process_error(sc, 0, 0, &error))
533 			return;
534 	}
535 
536 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
537 			&XFS_RMAP_OINFO_INOBT, &blocks);
538 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
539 		return;
540 	if (blocks != inobt_blocks + finobt_blocks)
541 		xchk_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
542 }
543 
544 /*
545  * Make sure that the inobt records point to the same number of blocks as
546  * the rmap says are owned by inodes.
547  */
548 STATIC void
xchk_iallocbt_xref_rmap_inodes(struct xfs_scrub * sc,int which,unsigned long long inodes)549 xchk_iallocbt_xref_rmap_inodes(
550 	struct xfs_scrub	*sc,
551 	int			which,
552 	unsigned long long	inodes)
553 {
554 	xfs_filblks_t		blocks;
555 	xfs_filblks_t		inode_blocks;
556 	int			error;
557 
558 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
559 		return;
560 
561 	/* Check that we saw as many inode blocks as the rmap knows about. */
562 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur,
563 			&XFS_RMAP_OINFO_INODES, &blocks);
564 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
565 		return;
566 	inode_blocks = XFS_B_TO_FSB(sc->mp, inodes * sc->mp->m_sb.sb_inodesize);
567 	if (blocks != inode_blocks)
568 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
569 }
570 
571 /* Scrub the inode btrees for some AG. */
572 STATIC int
xchk_iallocbt(struct xfs_scrub * sc,xfs_btnum_t which)573 xchk_iallocbt(
574 	struct xfs_scrub	*sc,
575 	xfs_btnum_t		which)
576 {
577 	struct xfs_btree_cur	*cur;
578 	struct xchk_iallocbt	iabt = {
579 		.inodes		= 0,
580 		.next_startino	= NULLAGINO,
581 		.next_cluster_ino = NULLAGINO,
582 	};
583 	int			error;
584 
585 	cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur;
586 	error = xchk_btree(sc, cur, xchk_iallocbt_rec, &XFS_RMAP_OINFO_INOBT,
587 			&iabt);
588 	if (error)
589 		return error;
590 
591 	xchk_iallocbt_xref_rmap_btreeblks(sc, which);
592 
593 	/*
594 	 * If we're scrubbing the inode btree, inode_blocks is the number of
595 	 * blocks pointed to by all the inode chunk records.  Therefore, we
596 	 * should compare to the number of inode chunk blocks that the rmap
597 	 * knows about.  We can't do this for the finobt since it only points
598 	 * to inode chunks with free inodes.
599 	 */
600 	if (which == XFS_BTNUM_INO)
601 		xchk_iallocbt_xref_rmap_inodes(sc, which, iabt.inodes);
602 
603 	return error;
604 }
605 
606 int
xchk_inobt(struct xfs_scrub * sc)607 xchk_inobt(
608 	struct xfs_scrub	*sc)
609 {
610 	return xchk_iallocbt(sc, XFS_BTNUM_INO);
611 }
612 
613 int
xchk_finobt(struct xfs_scrub * sc)614 xchk_finobt(
615 	struct xfs_scrub	*sc)
616 {
617 	return xchk_iallocbt(sc, XFS_BTNUM_FINO);
618 }
619 
620 /* See if an inode btree has (or doesn't have) an inode chunk record. */
621 static inline void
xchk_xref_inode_check(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len,struct xfs_btree_cur ** icur,bool should_have_inodes)622 xchk_xref_inode_check(
623 	struct xfs_scrub	*sc,
624 	xfs_agblock_t		agbno,
625 	xfs_extlen_t		len,
626 	struct xfs_btree_cur	**icur,
627 	bool			should_have_inodes)
628 {
629 	bool			has_inodes;
630 	int			error;
631 
632 	if (!(*icur) || xchk_skip_xref(sc->sm))
633 		return;
634 
635 	error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes);
636 	if (!xchk_should_check_xref(sc, &error, icur))
637 		return;
638 	if (has_inodes != should_have_inodes)
639 		xchk_btree_xref_set_corrupt(sc, *icur, 0);
640 }
641 
642 /* xref check that the extent is not covered by inodes */
643 void
xchk_xref_is_not_inode_chunk(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)644 xchk_xref_is_not_inode_chunk(
645 	struct xfs_scrub	*sc,
646 	xfs_agblock_t		agbno,
647 	xfs_extlen_t		len)
648 {
649 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false);
650 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false);
651 }
652 
653 /* xref check that the extent is covered by inodes */
654 void
xchk_xref_is_inode_chunk(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)655 xchk_xref_is_inode_chunk(
656 	struct xfs_scrub	*sc,
657 	xfs_agblock_t		agbno,
658 	xfs_extlen_t		len)
659 {
660 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true);
661 }
662