xref: /openbmc/linux/fs/xfs/scrub/rmap.c (revision 249592bf)
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_rmap.h"
14 #include "xfs_refcount.h"
15 #include "scrub/scrub.h"
16 #include "scrub/common.h"
17 #include "scrub/btree.h"
18 
19 /*
20  * Set us up to scrub reverse mapping btrees.
21  */
22 int
23 xchk_setup_ag_rmapbt(
24 	struct xfs_scrub	*sc)
25 {
26 	return xchk_setup_ag_btree(sc, false);
27 }
28 
29 /* Reverse-mapping scrubber. */
30 
31 /* Cross-reference a rmap against the refcount btree. */
32 STATIC void
33 xchk_rmapbt_xref_refc(
34 	struct xfs_scrub	*sc,
35 	struct xfs_rmap_irec	*irec)
36 {
37 	xfs_agblock_t		fbno;
38 	xfs_extlen_t		flen;
39 	bool			non_inode;
40 	bool			is_bmbt;
41 	bool			is_attr;
42 	bool			is_unwritten;
43 	int			error;
44 
45 	if (!sc->sa.refc_cur || xchk_skip_xref(sc->sm))
46 		return;
47 
48 	non_inode = XFS_RMAP_NON_INODE_OWNER(irec->rm_owner);
49 	is_bmbt = irec->rm_flags & XFS_RMAP_BMBT_BLOCK;
50 	is_attr = irec->rm_flags & XFS_RMAP_ATTR_FORK;
51 	is_unwritten = irec->rm_flags & XFS_RMAP_UNWRITTEN;
52 
53 	/* If this is shared, must be a data fork extent. */
54 	error = xfs_refcount_find_shared(sc->sa.refc_cur, irec->rm_startblock,
55 			irec->rm_blockcount, &fbno, &flen, false);
56 	if (!xchk_should_check_xref(sc, &error, &sc->sa.refc_cur))
57 		return;
58 	if (flen != 0 && (non_inode || is_attr || is_bmbt || is_unwritten))
59 		xchk_btree_xref_set_corrupt(sc, sc->sa.refc_cur, 0);
60 }
61 
62 /* Cross-reference with the other btrees. */
63 STATIC void
64 xchk_rmapbt_xref(
65 	struct xfs_scrub	*sc,
66 	struct xfs_rmap_irec	*irec)
67 {
68 	xfs_agblock_t		agbno = irec->rm_startblock;
69 	xfs_extlen_t		len = irec->rm_blockcount;
70 
71 	if (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT)
72 		return;
73 
74 	xchk_xref_is_used_space(sc, agbno, len);
75 	if (irec->rm_owner == XFS_RMAP_OWN_INODES)
76 		xchk_xref_is_inode_chunk(sc, agbno, len);
77 	else
78 		xchk_xref_is_not_inode_chunk(sc, agbno, len);
79 	if (irec->rm_owner == XFS_RMAP_OWN_COW)
80 		xchk_xref_is_cow_staging(sc, irec->rm_startblock,
81 				irec->rm_blockcount);
82 	else
83 		xchk_rmapbt_xref_refc(sc, irec);
84 }
85 
86 /* Scrub an rmapbt record. */
87 STATIC int
88 xchk_rmapbt_rec(
89 	struct xchk_btree	*bs,
90 	union xfs_btree_rec	*rec)
91 {
92 	struct xfs_mount	*mp = bs->cur->bc_mp;
93 	struct xfs_rmap_irec	irec;
94 	xfs_agnumber_t		agno = bs->cur->bc_ag.agno;
95 	bool			non_inode;
96 	bool			is_unwritten;
97 	bool			is_bmbt;
98 	bool			is_attr;
99 	int			error;
100 
101 	error = xfs_rmap_btrec_to_irec(rec, &irec);
102 	if (!xchk_btree_process_error(bs->sc, bs->cur, 0, &error))
103 		goto out;
104 
105 	/* Check extent. */
106 	if (irec.rm_startblock + irec.rm_blockcount <= irec.rm_startblock)
107 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
108 
109 	if (irec.rm_owner == XFS_RMAP_OWN_FS) {
110 		/*
111 		 * xfs_verify_agbno returns false for static fs metadata.
112 		 * Since that only exists at the start of the AG, validate
113 		 * that by hand.
114 		 */
115 		if (irec.rm_startblock != 0 ||
116 		    irec.rm_blockcount != XFS_AGFL_BLOCK(mp) + 1)
117 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
118 	} else {
119 		/*
120 		 * Otherwise we must point somewhere past the static metadata
121 		 * but before the end of the FS.  Run the regular check.
122 		 */
123 		if (!xfs_verify_agbno(mp, agno, irec.rm_startblock) ||
124 		    !xfs_verify_agbno(mp, agno, irec.rm_startblock +
125 				irec.rm_blockcount - 1))
126 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
127 	}
128 
129 	/* Check flags. */
130 	non_inode = XFS_RMAP_NON_INODE_OWNER(irec.rm_owner);
131 	is_bmbt = irec.rm_flags & XFS_RMAP_BMBT_BLOCK;
132 	is_attr = irec.rm_flags & XFS_RMAP_ATTR_FORK;
133 	is_unwritten = irec.rm_flags & XFS_RMAP_UNWRITTEN;
134 
135 	if (is_bmbt && irec.rm_offset != 0)
136 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
137 
138 	if (non_inode && irec.rm_offset != 0)
139 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
140 
141 	if (is_unwritten && (is_bmbt || non_inode || is_attr))
142 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
143 
144 	if (non_inode && (is_bmbt || is_unwritten || is_attr))
145 		xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
146 
147 	if (!non_inode) {
148 		if (!xfs_verify_ino(mp, irec.rm_owner))
149 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
150 	} else {
151 		/* Non-inode owner within the magic values? */
152 		if (irec.rm_owner <= XFS_RMAP_OWN_MIN ||
153 		    irec.rm_owner > XFS_RMAP_OWN_FS)
154 			xchk_btree_set_corrupt(bs->sc, bs->cur, 0);
155 	}
156 
157 	xchk_rmapbt_xref(bs->sc, &irec);
158 out:
159 	return error;
160 }
161 
162 /* Scrub the rmap btree for some AG. */
163 int
164 xchk_rmapbt(
165 	struct xfs_scrub	*sc)
166 {
167 	return xchk_btree(sc, sc->sa.rmap_cur, xchk_rmapbt_rec,
168 			&XFS_RMAP_OINFO_AG, NULL);
169 }
170 
171 /* xref check that the extent is owned by a given owner */
172 static inline void
173 xchk_xref_check_owner(
174 	struct xfs_scrub		*sc,
175 	xfs_agblock_t			bno,
176 	xfs_extlen_t			len,
177 	const struct xfs_owner_info	*oinfo,
178 	bool				should_have_rmap)
179 {
180 	bool				has_rmap;
181 	int				error;
182 
183 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
184 		return;
185 
186 	error = xfs_rmap_record_exists(sc->sa.rmap_cur, bno, len, oinfo,
187 			&has_rmap);
188 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
189 		return;
190 	if (has_rmap != should_have_rmap)
191 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
192 }
193 
194 /* xref check that the extent is owned by a given owner */
195 void
196 xchk_xref_is_owned_by(
197 	struct xfs_scrub		*sc,
198 	xfs_agblock_t			bno,
199 	xfs_extlen_t			len,
200 	const struct xfs_owner_info	*oinfo)
201 {
202 	xchk_xref_check_owner(sc, bno, len, oinfo, true);
203 }
204 
205 /* xref check that the extent is not owned by a given owner */
206 void
207 xchk_xref_is_not_owned_by(
208 	struct xfs_scrub		*sc,
209 	xfs_agblock_t			bno,
210 	xfs_extlen_t			len,
211 	const struct xfs_owner_info	*oinfo)
212 {
213 	xchk_xref_check_owner(sc, bno, len, oinfo, false);
214 }
215 
216 /* xref check that the extent has no reverse mapping at all */
217 void
218 xchk_xref_has_no_owner(
219 	struct xfs_scrub	*sc,
220 	xfs_agblock_t		bno,
221 	xfs_extlen_t		len)
222 {
223 	bool			has_rmap;
224 	int			error;
225 
226 	if (!sc->sa.rmap_cur || xchk_skip_xref(sc->sm))
227 		return;
228 
229 	error = xfs_rmap_has_record(sc->sa.rmap_cur, bno, len, &has_rmap);
230 	if (!xchk_should_check_xref(sc, &error, &sc->sa.rmap_cur))
231 		return;
232 	if (has_rmap)
233 		xchk_btree_xref_set_corrupt(sc, sc->sa.rmap_cur, 0);
234 }
235