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