xref: /linux/fs/xfs/scrub/health.c (revision 22d55f02b8922a097cd4be1e2f131dfa7ef65901)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2019 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_health.h"
20 #include "scrub/scrub.h"
21 #include "scrub/health.h"
22 
23 /*
24  * Scrub and In-Core Filesystem Health Assessments
25  * ===============================================
26  *
27  * Online scrub and repair have the time and the ability to perform stronger
28  * checks than we can do from the metadata verifiers, because they can
29  * cross-reference records between data structures.  Therefore, scrub is in a
30  * good position to update the online filesystem health assessments to reflect
31  * the good/bad state of the data structure.
32  *
33  * We therefore extend scrub in the following ways to achieve this:
34  *
35  * 1. Create a "sick_mask" field in the scrub context.  When we're setting up a
36  * scrub call, set this to the default XFS_SICK_* flag(s) for the selected
37  * scrub type (call it A).  Scrub and repair functions can override the default
38  * sick_mask value if they choose.
39  *
40  * 2. If the scrubber returns a runtime error code, we exit making no changes
41  * to the incore sick state.
42  *
43  * 3. If the scrubber finds that A is clean, use sick_mask to clear the incore
44  * sick flags before exiting.
45  *
46  * 4. If the scrubber finds that A is corrupt, use sick_mask to set the incore
47  * sick flags.  If the user didn't want to repair then we exit, leaving the
48  * metadata structure unfixed and the sick flag set.
49  *
50  * 5. Now we know that A is corrupt and the user wants to repair, so run the
51  * repairer.  If the repairer returns an error code, we exit with that error
52  * code, having made no further changes to the incore sick state.
53  *
54  * 6. If repair rebuilds A correctly and the subsequent re-scrub of A is clean,
55  * use sick_mask to clear the incore sick flags.  This should have the effect
56  * that A is no longer marked sick.
57  *
58  * 7. If repair rebuilds A incorrectly, the re-scrub will find it corrupt and
59  * use sick_mask to set the incore sick flags.  This should have no externally
60  * visible effect since we already set them in step (4).
61  *
62  * There are some complications to this story, however.  For certain types of
63  * complementary metadata indices (e.g. inobt/finobt), it is easier to rebuild
64  * both structures at the same time.  The following principles apply to this
65  * type of repair strategy:
66  *
67  * 8. Any repair function that rebuilds multiple structures should update
68  * sick_mask_visible to reflect whatever other structures are rebuilt, and
69  * verify that all the rebuilt structures can pass a scrub check.  The outcomes
70  * of 5-7 still apply, but with a sick_mask that covers everything being
71  * rebuilt.
72  */
73 
74 /* Map our scrub type to a sick mask and a set of health update functions. */
75 
76 enum xchk_health_group {
77 	XHG_FS = 1,
78 	XHG_RT,
79 	XHG_AG,
80 	XHG_INO,
81 };
82 
83 struct xchk_health_map {
84 	enum xchk_health_group	group;
85 	unsigned int		sick_mask;
86 };
87 
88 static const struct xchk_health_map type_to_health_flag[XFS_SCRUB_TYPE_NR] = {
89 	[XFS_SCRUB_TYPE_SB]		= { XHG_AG,  XFS_SICK_AG_SB },
90 	[XFS_SCRUB_TYPE_AGF]		= { XHG_AG,  XFS_SICK_AG_AGF },
91 	[XFS_SCRUB_TYPE_AGFL]		= { XHG_AG,  XFS_SICK_AG_AGFL },
92 	[XFS_SCRUB_TYPE_AGI]		= { XHG_AG,  XFS_SICK_AG_AGI },
93 	[XFS_SCRUB_TYPE_BNOBT]		= { XHG_AG,  XFS_SICK_AG_BNOBT },
94 	[XFS_SCRUB_TYPE_CNTBT]		= { XHG_AG,  XFS_SICK_AG_CNTBT },
95 	[XFS_SCRUB_TYPE_INOBT]		= { XHG_AG,  XFS_SICK_AG_INOBT },
96 	[XFS_SCRUB_TYPE_FINOBT]		= { XHG_AG,  XFS_SICK_AG_FINOBT },
97 	[XFS_SCRUB_TYPE_RMAPBT]		= { XHG_AG,  XFS_SICK_AG_RMAPBT },
98 	[XFS_SCRUB_TYPE_REFCNTBT]	= { XHG_AG,  XFS_SICK_AG_REFCNTBT },
99 	[XFS_SCRUB_TYPE_INODE]		= { XHG_INO, XFS_SICK_INO_CORE },
100 	[XFS_SCRUB_TYPE_BMBTD]		= { XHG_INO, XFS_SICK_INO_BMBTD },
101 	[XFS_SCRUB_TYPE_BMBTA]		= { XHG_INO, XFS_SICK_INO_BMBTA },
102 	[XFS_SCRUB_TYPE_BMBTC]		= { XHG_INO, XFS_SICK_INO_BMBTC },
103 	[XFS_SCRUB_TYPE_DIR]		= { XHG_INO, XFS_SICK_INO_DIR },
104 	[XFS_SCRUB_TYPE_XATTR]		= { XHG_INO, XFS_SICK_INO_XATTR },
105 	[XFS_SCRUB_TYPE_SYMLINK]	= { XHG_INO, XFS_SICK_INO_SYMLINK },
106 	[XFS_SCRUB_TYPE_PARENT]		= { XHG_INO, XFS_SICK_INO_PARENT },
107 	[XFS_SCRUB_TYPE_RTBITMAP]	= { XHG_RT,  XFS_SICK_RT_BITMAP },
108 	[XFS_SCRUB_TYPE_RTSUM]		= { XHG_RT,  XFS_SICK_RT_SUMMARY },
109 	[XFS_SCRUB_TYPE_UQUOTA]		= { XHG_FS,  XFS_SICK_FS_UQUOTA },
110 	[XFS_SCRUB_TYPE_GQUOTA]		= { XHG_FS,  XFS_SICK_FS_GQUOTA },
111 	[XFS_SCRUB_TYPE_PQUOTA]		= { XHG_FS,  XFS_SICK_FS_PQUOTA },
112 	[XFS_SCRUB_TYPE_FSCOUNTERS]	= { XHG_FS,  XFS_SICK_FS_COUNTERS },
113 };
114 
115 /* Return the health status mask for this scrub type. */
116 unsigned int
117 xchk_health_mask_for_scrub_type(
118 	__u32			scrub_type)
119 {
120 	return type_to_health_flag[scrub_type].sick_mask;
121 }
122 
123 /*
124  * Update filesystem health assessments based on what we found and did.
125  *
126  * If the scrubber finds errors, we mark sick whatever's mentioned in
127  * sick_mask, no matter whether this is a first scan or an
128  * evaluation of repair effectiveness.
129  *
130  * Otherwise, no direct corruption was found, so mark whatever's in
131  * sick_mask as healthy.
132  */
133 void
134 xchk_update_health(
135 	struct xfs_scrub	*sc)
136 {
137 	struct xfs_perag	*pag;
138 	bool			bad;
139 
140 	if (!sc->sick_mask)
141 		return;
142 
143 	bad = (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT);
144 	switch (type_to_health_flag[sc->sm->sm_type].group) {
145 	case XHG_AG:
146 		pag = xfs_perag_get(sc->mp, sc->sm->sm_agno);
147 		if (bad)
148 			xfs_ag_mark_sick(pag, sc->sick_mask);
149 		else
150 			xfs_ag_mark_healthy(pag, sc->sick_mask);
151 		xfs_perag_put(pag);
152 		break;
153 	case XHG_INO:
154 		if (!sc->ip)
155 			return;
156 		if (bad)
157 			xfs_inode_mark_sick(sc->ip, sc->sick_mask);
158 		else
159 			xfs_inode_mark_healthy(sc->ip, sc->sick_mask);
160 		break;
161 	case XHG_FS:
162 		if (bad)
163 			xfs_fs_mark_sick(sc->mp, sc->sick_mask);
164 		else
165 			xfs_fs_mark_healthy(sc->mp, sc->sick_mask);
166 		break;
167 	case XHG_RT:
168 		if (bad)
169 			xfs_rt_mark_sick(sc->mp, sc->sick_mask);
170 		else
171 			xfs_rt_mark_healthy(sc->mp, sc->sick_mask);
172 		break;
173 	default:
174 		ASSERT(0);
175 		break;
176 	}
177 }
178 
179 /* Is the given per-AG btree healthy enough for scanning? */
180 bool
181 xchk_ag_btree_healthy_enough(
182 	struct xfs_scrub	*sc,
183 	struct xfs_perag	*pag,
184 	xfs_btnum_t		btnum)
185 {
186 	unsigned int		mask = 0;
187 
188 	/*
189 	 * We always want the cursor if it's the same type as whatever we're
190 	 * scrubbing, even if we already know the structure is corrupt.
191 	 *
192 	 * Otherwise, we're only interested in the btree for cross-referencing.
193 	 * If we know the btree is bad then don't bother, just set XFAIL.
194 	 */
195 	switch (btnum) {
196 	case XFS_BTNUM_BNO:
197 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT)
198 			return true;
199 		mask = XFS_SICK_AG_BNOBT;
200 		break;
201 	case XFS_BTNUM_CNT:
202 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_CNTBT)
203 			return true;
204 		mask = XFS_SICK_AG_CNTBT;
205 		break;
206 	case XFS_BTNUM_INO:
207 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT)
208 			return true;
209 		mask = XFS_SICK_AG_INOBT;
210 		break;
211 	case XFS_BTNUM_FINO:
212 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT)
213 			return true;
214 		mask = XFS_SICK_AG_FINOBT;
215 		break;
216 	case XFS_BTNUM_RMAP:
217 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_RMAPBT)
218 			return true;
219 		mask = XFS_SICK_AG_RMAPBT;
220 		break;
221 	case XFS_BTNUM_REFC:
222 		if (sc->sm->sm_type == XFS_SCRUB_TYPE_REFCNTBT)
223 			return true;
224 		mask = XFS_SICK_AG_REFCNTBT;
225 		break;
226 	default:
227 		ASSERT(0);
228 		return true;
229 	}
230 
231 	if (xfs_ag_has_sickness(pag, mask)) {
232 		sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL;
233 		return false;
234 	}
235 
236 	return true;
237 }
238