xref: /openbmc/linux/fs/xfs/scrub/ialloc.c (revision 242cdad8)
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_defer.h"
13 #include "xfs_btree.h"
14 #include "xfs_bit.h"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.h"
17 #include "xfs_sb.h"
18 #include "xfs_inode.h"
19 #include "xfs_alloc.h"
20 #include "xfs_ialloc.h"
21 #include "xfs_ialloc_btree.h"
22 #include "xfs_icache.h"
23 #include "xfs_rmap.h"
24 #include "xfs_log.h"
25 #include "xfs_trans_priv.h"
26 #include "scrub/xfs_scrub.h"
27 #include "scrub/scrub.h"
28 #include "scrub/common.h"
29 #include "scrub/btree.h"
30 #include "scrub/trace.h"
31 
32 /*
33  * Set us up to scrub inode btrees.
34  * If we detect a discrepancy between the inobt and the inode,
35  * try again after forcing logged inode cores out to disk.
36  */
37 int
38 xchk_setup_ag_iallocbt(
39 	struct xfs_scrub	*sc,
40 	struct xfs_inode	*ip)
41 {
42 	return xchk_setup_ag_btree(sc, ip, sc->try_harder);
43 }
44 
45 /* Inode btree scrubber. */
46 
47 /*
48  * If we're checking the finobt, cross-reference with the inobt.
49  * Otherwise we're checking the inobt; if there is an finobt, make sure
50  * we have a record or not depending on freecount.
51  */
52 static inline void
53 xchk_iallocbt_chunk_xref_other(
54 	struct xfs_scrub		*sc,
55 	struct xfs_inobt_rec_incore	*irec,
56 	xfs_agino_t			agino)
57 {
58 	struct xfs_btree_cur		**pcur;
59 	bool				has_irec;
60 	int				error;
61 
62 	if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
63 		pcur = &sc->sa.ino_cur;
64 	else
65 		pcur = &sc->sa.fino_cur;
66 	if (!(*pcur))
67 		return;
68 	error = xfs_ialloc_has_inode_record(*pcur, agino, agino, &has_irec);
69 	if (!xchk_should_check_xref(sc, &error, pcur))
70 		return;
71 	if (((irec->ir_freecount > 0 && !has_irec) ||
72 	     (irec->ir_freecount == 0 && has_irec)))
73 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
74 }
75 
76 /* Cross-reference with the other btrees. */
77 STATIC void
78 xchk_iallocbt_chunk_xref(
79 	struct xfs_scrub		*sc,
80 	struct xfs_inobt_rec_incore	*irec,
81 	xfs_agino_t			agino,
82 	xfs_agblock_t			agbno,
83 	xfs_extlen_t			len)
84 {
85 	struct xfs_owner_info		oinfo;
86 
87 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
88 		return;
89 
90 	xchk_xref_is_used_space(sc, agbno, len);
91 	xchk_iallocbt_chunk_xref_other(sc, irec, agino);
92 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
93 	xchk_xref_is_owned_by(sc, agbno, len, &oinfo);
94 	xchk_xref_is_not_shared(sc, agbno, len);
95 }
96 
97 /* Is this chunk worth checking? */
98 STATIC bool
99 xchk_iallocbt_chunk(
100 	struct xchk_btree		*bs,
101 	struct xfs_inobt_rec_incore	*irec,
102 	xfs_agino_t			agino,
103 	xfs_extlen_t			len)
104 {
105 	struct xfs_mount		*mp = bs->cur->bc_mp;
106 	xfs_agnumber_t			agno = bs->cur->bc_private.a.agno;
107 	xfs_agblock_t			bno;
108 
109 	bno = XFS_AGINO_TO_AGBNO(mp, agino);
110 	if (bno + len <= bno ||
111 	    !xfs_verify_agbno(mp, agno, bno) ||
112 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
113 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
114 
115 	xchk_iallocbt_chunk_xref(bs->sc, irec, agino, bno, len);
116 
117 	return true;
118 }
119 
120 /* Count the number of free inodes. */
121 static unsigned int
122 xchk_iallocbt_freecount(
123 	xfs_inofree_t			freemask)
124 {
125 	BUILD_BUG_ON(sizeof(freemask) != sizeof(__u64));
126 	return hweight64(freemask);
127 }
128 
129 /* Check a particular inode with ir_free. */
130 STATIC int
131 xchk_iallocbt_check_cluster_freemask(
132 	struct xchk_btree		*bs,
133 	xfs_ino_t			fsino,
134 	xfs_agino_t			chunkino,
135 	xfs_agino_t			clusterino,
136 	struct xfs_inobt_rec_incore	*irec,
137 	struct xfs_buf			*bp)
138 {
139 	struct xfs_dinode		*dip;
140 	struct xfs_mount		*mp = bs->cur->bc_mp;
141 	bool				inode_is_free = false;
142 	bool				freemask_ok;
143 	bool				inuse;
144 	int				error = 0;
145 
146 	if (xchk_should_terminate(bs->sc, &error))
147 		return error;
148 
149 	dip = xfs_buf_offset(bp, clusterino * mp->m_sb.sb_inodesize);
150 	if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC ||
151 	    (dip->di_version >= 3 &&
152 	     be64_to_cpu(dip->di_ino) != fsino + clusterino)) {
153 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
154 		goto out;
155 	}
156 
157 	if (irec->ir_free & XFS_INOBT_MASK(chunkino + clusterino))
158 		inode_is_free = true;
159 	error = xfs_icache_inode_is_allocated(mp, bs->cur->bc_tp,
160 			fsino + clusterino, &inuse);
161 	if (error == -ENODATA) {
162 		/* Not cached, just read the disk buffer */
163 		freemask_ok = inode_is_free ^ !!(dip->di_mode);
164 		if (!bs->sc->try_harder && !freemask_ok)
165 			return -EDEADLOCK;
166 	} else if (error < 0) {
167 		/*
168 		 * Inode is only half assembled, or there was an IO error,
169 		 * or the verifier failed, so don't bother trying to check.
170 		 * The inode scrubber can deal with this.
171 		 */
172 		goto out;
173 	} else {
174 		/* Inode is all there. */
175 		freemask_ok = inode_is_free ^ inuse;
176 	}
177 	if (!freemask_ok)
178 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
179 out:
180 	return 0;
181 }
182 
183 /* Make sure the free mask is consistent with what the inodes think. */
184 STATIC int
185 xchk_iallocbt_check_freemask(
186 	struct xchk_btree		*bs,
187 	struct xfs_inobt_rec_incore	*irec)
188 {
189 	struct xfs_owner_info		oinfo;
190 	struct xfs_imap			imap;
191 	struct xfs_mount		*mp = bs->cur->bc_mp;
192 	struct xfs_dinode		*dip;
193 	struct xfs_buf			*bp;
194 	xfs_ino_t			fsino;
195 	xfs_agino_t			nr_inodes;
196 	xfs_agino_t			agino;
197 	xfs_agino_t			chunkino;
198 	xfs_agino_t			clusterino;
199 	xfs_agblock_t			agbno;
200 	int				blks_per_cluster;
201 	uint16_t			holemask;
202 	uint16_t			ir_holemask;
203 	int				error = 0;
204 
205 	/* Make sure the freemask matches the inode records. */
206 	blks_per_cluster = xfs_icluster_size_fsb(mp);
207 	nr_inodes = XFS_OFFBNO_TO_AGINO(mp, blks_per_cluster, 0);
208 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
209 
210 	for (agino = irec->ir_startino;
211 	     agino < irec->ir_startino + XFS_INODES_PER_CHUNK;
212 	     agino += blks_per_cluster * mp->m_sb.sb_inopblock) {
213 		fsino = XFS_AGINO_TO_INO(mp, bs->cur->bc_private.a.agno, agino);
214 		chunkino = agino - irec->ir_startino;
215 		agbno = XFS_AGINO_TO_AGBNO(mp, agino);
216 
217 		/* Compute the holemask mask for this cluster. */
218 		for (clusterino = 0, holemask = 0; clusterino < nr_inodes;
219 		     clusterino += XFS_INODES_PER_HOLEMASK_BIT)
220 			holemask |= XFS_INOBT_MASK((chunkino + clusterino) /
221 					XFS_INODES_PER_HOLEMASK_BIT);
222 
223 		/* The whole cluster must be a hole or not a hole. */
224 		ir_holemask = (irec->ir_holemask & holemask);
225 		if (ir_holemask != holemask && ir_holemask != 0) {
226 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
227 			continue;
228 		}
229 
230 		/* If any part of this is a hole, skip it. */
231 		if (ir_holemask) {
232 			xchk_xref_is_not_owned_by(bs->sc, agbno,
233 					blks_per_cluster, &oinfo);
234 			continue;
235 		}
236 
237 		xchk_xref_is_owned_by(bs->sc, agbno, blks_per_cluster,
238 				&oinfo);
239 
240 		/* Grab the inode cluster buffer. */
241 		imap.im_blkno = XFS_AGB_TO_DADDR(mp, bs->cur->bc_private.a.agno,
242 				agbno);
243 		imap.im_len = XFS_FSB_TO_BB(mp, blks_per_cluster);
244 		imap.im_boffset = 0;
245 
246 		error = xfs_imap_to_bp(mp, bs->cur->bc_tp, &imap,
247 				&dip, &bp, 0, 0);
248 		if (!xchk_btree_xref_process_error(bs->sc, bs->cur, 0,
249 				&error))
250 			continue;
251 
252 		/* Which inodes are free? */
253 		for (clusterino = 0; clusterino < nr_inodes; clusterino++) {
254 			error = xchk_iallocbt_check_cluster_freemask(bs,
255 					fsino, chunkino, clusterino, irec, bp);
256 			if (error) {
257 				xfs_trans_brelse(bs->cur->bc_tp, bp);
258 				return error;
259 			}
260 		}
261 
262 		xfs_trans_brelse(bs->cur->bc_tp, bp);
263 	}
264 
265 	return error;
266 }
267 
268 /* Scrub an inobt/finobt record. */
269 STATIC int
270 xchk_iallocbt_rec(
271 	struct xchk_btree		*bs,
272 	union xfs_btree_rec		*rec)
273 {
274 	struct xfs_mount		*mp = bs->cur->bc_mp;
275 	xfs_filblks_t			*inode_blocks = bs->private;
276 	struct xfs_inobt_rec_incore	irec;
277 	uint64_t			holes;
278 	xfs_agnumber_t			agno = bs->cur->bc_private.a.agno;
279 	xfs_agino_t			agino;
280 	xfs_agblock_t			agbno;
281 	xfs_extlen_t			len;
282 	int				holecount;
283 	int				i;
284 	int				error = 0;
285 	unsigned int			real_freecount;
286 	uint16_t			holemask;
287 
288 	xfs_inobt_btrec_to_irec(mp, rec, &irec);
289 
290 	if (irec.ir_count > XFS_INODES_PER_CHUNK ||
291 	    irec.ir_freecount > XFS_INODES_PER_CHUNK)
292 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
293 
294 	real_freecount = irec.ir_freecount +
295 			(XFS_INODES_PER_CHUNK - irec.ir_count);
296 	if (real_freecount != xchk_iallocbt_freecount(irec.ir_free))
297 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
298 
299 	agino = irec.ir_startino;
300 	/* Record has to be properly aligned within the AG. */
301 	if (!xfs_verify_agino(mp, agno, agino) ||
302 	    !xfs_verify_agino(mp, agno, agino + XFS_INODES_PER_CHUNK - 1)) {
303 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
304 		goto out;
305 	}
306 
307 	/* Make sure this record is aligned to cluster and inoalignmnt size. */
308 	agbno = XFS_AGINO_TO_AGBNO(mp, irec.ir_startino);
309 	if ((agbno & (xfs_ialloc_cluster_alignment(mp) - 1)) ||
310 	    (agbno & (xfs_icluster_size_fsb(mp) - 1)))
311 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
312 
313 	*inode_blocks += XFS_B_TO_FSB(mp,
314 			irec.ir_count * mp->m_sb.sb_inodesize);
315 
316 	/* Handle non-sparse inodes */
317 	if (!xfs_inobt_issparse(irec.ir_holemask)) {
318 		len = XFS_B_TO_FSB(mp,
319 				XFS_INODES_PER_CHUNK * mp->m_sb.sb_inodesize);
320 		if (irec.ir_count != XFS_INODES_PER_CHUNK)
321 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
322 
323 		if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
324 			goto out;
325 		goto check_freemask;
326 	}
327 
328 	/* Check each chunk of a sparse inode cluster. */
329 	holemask = irec.ir_holemask;
330 	holecount = 0;
331 	len = XFS_B_TO_FSB(mp,
332 			XFS_INODES_PER_HOLEMASK_BIT * mp->m_sb.sb_inodesize);
333 	holes = ~xfs_inobt_irec_to_allocmask(&irec);
334 	if ((holes & irec.ir_free) != holes ||
335 	    irec.ir_freecount > irec.ir_count)
336 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
337 
338 	for (i = 0; i < XFS_INOBT_HOLEMASK_BITS; i++) {
339 		if (holemask & 1)
340 			holecount += XFS_INODES_PER_HOLEMASK_BIT;
341 		else if (!xchk_iallocbt_chunk(bs, &irec, agino, len))
342 			break;
343 		holemask >>= 1;
344 		agino += XFS_INODES_PER_HOLEMASK_BIT;
345 	}
346 
347 	if (holecount > XFS_INODES_PER_CHUNK ||
348 	    holecount + irec.ir_count != XFS_INODES_PER_CHUNK)
349 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
350 
351 check_freemask:
352 	error = xchk_iallocbt_check_freemask(bs, &irec);
353 	if (error)
354 		goto out;
355 
356 out:
357 	return error;
358 }
359 
360 /*
361  * Make sure the inode btrees are as large as the rmap thinks they are.
362  * Don't bother if we're missing btree cursors, as we're already corrupt.
363  */
364 STATIC void
365 xchk_iallocbt_xref_rmap_btreeblks(
366 	struct xfs_scrub	*sc,
367 	int			which)
368 {
369 	struct xfs_owner_info	oinfo;
370 	xfs_filblks_t		blocks;
371 	xfs_extlen_t		inobt_blocks = 0;
372 	xfs_extlen_t		finobt_blocks = 0;
373 	int			error;
374 
375 	if (!sc->sa.ino_cur || !sc->sa.rmap_cur ||
376 	    (xfs_sb_version_hasfinobt(&sc->mp->m_sb) && !sc->sa.fino_cur) ||
377 	    xchk_skip_xref(sc->sm))
378 		return;
379 
380 	/* Check that we saw as many inobt blocks as the rmap says. */
381 	error = xfs_btree_count_blocks(sc->sa.ino_cur, &inobt_blocks);
382 	if (!xchk_process_error(sc, 0, 0, &error))
383 		return;
384 
385 	if (sc->sa.fino_cur) {
386 		error = xfs_btree_count_blocks(sc->sa.fino_cur, &finobt_blocks);
387 		if (!xchk_process_error(sc, 0, 0, &error))
388 			return;
389 	}
390 
391 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
392 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
393 			&blocks);
394 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
395 		return;
396 	if (blocks != inobt_blocks + finobt_blocks)
397 		xchk_btree_set_corrupt(sc, sc->sa.ino_cur, 0);
398 }
399 
400 /*
401  * Make sure that the inobt records point to the same number of blocks as
402  * the rmap says are owned by inodes.
403  */
404 STATIC void
405 xchk_iallocbt_xref_rmap_inodes(
406 	struct xfs_scrub	*sc,
407 	int			which,
408 	xfs_filblks_t		inode_blocks)
409 {
410 	struct xfs_owner_info	oinfo;
411 	xfs_filblks_t		blocks;
412 	int			error;
413 
414 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
415 		return;
416 
417 	/* Check that we saw as many inode blocks as the rmap knows about. */
418 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INODES);
419 	error = xchk_count_rmap_ownedby_ag(sc, sc->sa.rmap_cur, &oinfo,
420 			&blocks);
421 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
422 		return;
423 	if (blocks != inode_blocks)
424 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
425 }
426 
427 /* Scrub the inode btrees for some AG. */
428 STATIC int
429 xchk_iallocbt(
430 	struct xfs_scrub	*sc,
431 	xfs_btnum_t		which)
432 {
433 	struct xfs_btree_cur	*cur;
434 	struct xfs_owner_info	oinfo;
435 	xfs_filblks_t		inode_blocks = 0;
436 	int			error;
437 
438 	xfs_rmap_ag_owner(&oinfo, XFS_RMAP_OWN_INOBT);
439 	cur = which == XFS_BTNUM_INO ? sc->sa.ino_cur : sc->sa.fino_cur;
440 	error = xchk_btree(sc, cur, xchk_iallocbt_rec, &oinfo,
441 			&inode_blocks);
442 	if (error)
443 		return error;
444 
445 	xchk_iallocbt_xref_rmap_btreeblks(sc, which);
446 
447 	/*
448 	 * If we're scrubbing the inode btree, inode_blocks is the number of
449 	 * blocks pointed to by all the inode chunk records.  Therefore, we
450 	 * should compare to the number of inode chunk blocks that the rmap
451 	 * knows about.  We can't do this for the finobt since it only points
452 	 * to inode chunks with free inodes.
453 	 */
454 	if (which == XFS_BTNUM_INO)
455 		xchk_iallocbt_xref_rmap_inodes(sc, which, inode_blocks);
456 
457 	return error;
458 }
459 
460 int
461 xchk_inobt(
462 	struct xfs_scrub	*sc)
463 {
464 	return xchk_iallocbt(sc, XFS_BTNUM_INO);
465 }
466 
467 int
468 xchk_finobt(
469 	struct xfs_scrub	*sc)
470 {
471 	return xchk_iallocbt(sc, XFS_BTNUM_FINO);
472 }
473 
474 /* See if an inode btree has (or doesn't have) an inode chunk record. */
475 static inline void
476 xchk_xref_inode_check(
477 	struct xfs_scrub	*sc,
478 	xfs_agblock_t		agbno,
479 	xfs_extlen_t		len,
480 	struct xfs_btree_cur	**icur,
481 	bool			should_have_inodes)
482 {
483 	bool			has_inodes;
484 	int			error;
485 
486 	if (!(*icur) || xchk_skip_xref(sc->sm))
487 		return;
488 
489 	error = xfs_ialloc_has_inodes_at_extent(*icur, agbno, len, &has_inodes);
490 	if (!xchk_should_check_xref(sc, &error, icur))
491 		return;
492 	if (has_inodes != should_have_inodes)
493 		xchk_btree_xref_set_corrupt(sc, *icur, 0);
494 }
495 
496 /* xref check that the extent is not covered by inodes */
497 void
498 xchk_xref_is_not_inode_chunk(
499 	struct xfs_scrub	*sc,
500 	xfs_agblock_t		agbno,
501 	xfs_extlen_t		len)
502 {
503 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, false);
504 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.fino_cur, false);
505 }
506 
507 /* xref check that the extent is covered by inodes */
508 void
509 xchk_xref_is_inode_chunk(
510 	struct xfs_scrub	*sc,
511 	xfs_agblock_t		agbno,
512 	xfs_extlen_t		len)
513 {
514 	xchk_xref_inode_check(sc, agbno, len, &sc->sa.ino_cur, true);
515 }
516