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_alloc.h"
14 #include "xfs_rmap.h"
15 #include "scrub/scrub.h"
16 #include "scrub/common.h"
17 #include "scrub/btree.h"
18 
19 /*
20  * Set us up to scrub free space btrees.
21  */
22 int
xchk_setup_ag_allocbt(struct xfs_scrub * sc)23 xchk_setup_ag_allocbt(
24 	struct xfs_scrub	*sc)
25 {
26 	return xchk_setup_ag_btree(sc, false);
27 }
28 
29 /* Free space btree scrubber. */
30 /*
31  * Ensure there's a corresponding cntbt/bnobt record matching this
32  * bnobt/cntbt record, respectively.
33  */
34 STATIC void
xchk_allocbt_xref_other(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)35 xchk_allocbt_xref_other(
36 	struct xfs_scrub	*sc,
37 	xfs_agblock_t		agbno,
38 	xfs_extlen_t		len)
39 {
40 	struct xfs_btree_cur	**pcur;
41 	xfs_agblock_t		fbno;
42 	xfs_extlen_t		flen;
43 	int			has_otherrec;
44 	int			error;
45 
46 	if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
47 		pcur = &sc->sa.cnt_cur;
48 	else
49 		pcur = &sc->sa.bno_cur;
50 	if (!*pcur || xchk_skip_xref(sc->sm))
51 		return;
52 
53 	error = xfs_alloc_lookup_le(*pcur, agbno, len, &has_otherrec);
54 	if (!xchk_should_check_xref(sc, &error, pcur))
55 		return;
56 	if (!has_otherrec) {
57 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
58 		return;
59 	}
60 
61 	error = xfs_alloc_get_rec(*pcur, &fbno, &flen, &has_otherrec);
62 	if (!xchk_should_check_xref(sc, &error, pcur))
63 		return;
64 	if (!has_otherrec) {
65 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
66 		return;
67 	}
68 
69 	if (fbno != agbno || flen != len)
70 		xchk_btree_xref_set_corrupt(sc, *pcur, 0);
71 }
72 
73 /* Cross-reference with the other btrees. */
74 STATIC void
xchk_allocbt_xref(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)75 xchk_allocbt_xref(
76 	struct xfs_scrub	*sc,
77 	xfs_agblock_t		agbno,
78 	xfs_extlen_t		len)
79 {
80 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
81 		return;
82 
83 	xchk_allocbt_xref_other(sc, agbno, len);
84 	xchk_xref_is_not_inode_chunk(sc, agbno, len);
85 	xchk_xref_has_no_owner(sc, agbno, len);
86 	xchk_xref_is_not_shared(sc, agbno, len);
87 }
88 
89 /* Scrub a bnobt/cntbt record. */
90 STATIC int
xchk_allocbt_rec(struct xchk_btree * bs,union xfs_btree_rec * rec)91 xchk_allocbt_rec(
92 	struct xchk_btree	*bs,
93 	union xfs_btree_rec	*rec)
94 {
95 	struct xfs_mount	*mp = bs->cur->bc_mp;
96 	xfs_agnumber_t		agno = bs->cur->bc_ag.agno;
97 	xfs_agblock_t		bno;
98 	xfs_extlen_t		len;
99 
100 	bno = be32_to_cpu(rec->alloc.ar_startblock);
101 	len = be32_to_cpu(rec->alloc.ar_blockcount);
102 
103 	if (bno + len <= bno ||
104 	    !xfs_verify_agbno(mp, agno, bno) ||
105 	    !xfs_verify_agbno(mp, agno, bno + len - 1))
106 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
107 
108 	xchk_allocbt_xref(bs->sc, bno, len);
109 
110 	return 0;
111 }
112 
113 /* Scrub the freespace btrees for some AG. */
114 STATIC int
xchk_allocbt(struct xfs_scrub * sc,xfs_btnum_t which)115 xchk_allocbt(
116 	struct xfs_scrub	*sc,
117 	xfs_btnum_t		which)
118 {
119 	struct xfs_btree_cur	*cur;
120 
121 	cur = which == XFS_BTNUM_BNO ? sc->sa.bno_cur : sc->sa.cnt_cur;
122 	return xchk_btree(sc, cur, xchk_allocbt_rec, &XFS_RMAP_OINFO_AG, NULL);
123 }
124 
125 int
xchk_bnobt(struct xfs_scrub * sc)126 xchk_bnobt(
127 	struct xfs_scrub	*sc)
128 {
129 	return xchk_allocbt(sc, XFS_BTNUM_BNO);
130 }
131 
132 int
xchk_cntbt(struct xfs_scrub * sc)133 xchk_cntbt(
134 	struct xfs_scrub	*sc)
135 {
136 	return xchk_allocbt(sc, XFS_BTNUM_CNT);
137 }
138 
139 /* xref check that the extent is not free */
140 void
xchk_xref_is_used_space(struct xfs_scrub * sc,xfs_agblock_t agbno,xfs_extlen_t len)141 xchk_xref_is_used_space(
142 	struct xfs_scrub	*sc,
143 	xfs_agblock_t		agbno,
144 	xfs_extlen_t		len)
145 {
146 	bool			is_freesp;
147 	int			error;
148 
149 	if (!sc->sa.bno_cur || xchk_skip_xref(sc->sm))
150 		return;
151 
152 	error = xfs_alloc_has_record(sc->sa.bno_cur, agbno, len, &is_freesp);
153 	if (!xchk_should_check_xref(sc, &error, &sc->sa.bno_cur))
154 		return;
155 	if (is_freesp)
156 		xchk_btree_xref_set_corrupt(sc, sc->sa.bno_cur, 0);
157 }
158