xref: /linux/fs/xfs/libxfs/xfs_health.h (revision 7f71507851fc7764b36a3221839607d3a45c2025)
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 #ifndef __XFS_HEALTH_H__
7 #define __XFS_HEALTH_H__
8 
9 struct xfs_group;
10 
11 /*
12  * In-Core Filesystem Health Assessments
13  * =====================================
14  *
15  * We'd like to be able to summarize the current health status of the
16  * filesystem so that the administrator knows when it's necessary to schedule
17  * some downtime for repairs.  Until then, we would also like to avoid abrupt
18  * shutdowns due to corrupt metadata.
19  *
20  * The online scrub feature evaluates the health of all filesystem metadata.
21  * When scrub detects corruption in a piece of metadata it will set the
22  * corresponding sickness flag, and repair will clear it if successful.  If
23  * problems remain at unmount time, we can also request manual intervention by
24  * logging a notice to run xfs_repair.
25  *
26  * Each health tracking group uses a pair of fields for reporting.  The
27  * "checked" field tell us if a given piece of metadata has ever been examined,
28  * and the "sick" field tells us if that piece was found to need repairs.
29  * Therefore we can conclude that for a given sick flag value:
30  *
31  *  - checked && sick   => metadata needs repair
32  *  - checked && !sick  => metadata is ok
33  *  - !checked && sick  => errors have been observed during normal operation,
34  *                         but the metadata has not been checked thoroughly
35  *  - !checked && !sick => has not been examined since mount
36  *
37  * Evidence of health problems can be sorted into three basic categories:
38  *
39  * a) Primary evidence, which signals that something is defective within the
40  *    general grouping of metadata.
41  *
42  * b) Secondary evidence, which are side effects of primary problem but are
43  *    not themselves problems.  These can be forgotten when the primary
44  *    health problems are addressed.
45  *
46  * c) Indirect evidence, which points to something being wrong in another
47  *    group, but we had to release resources and this is all that's left of
48  *    that state.
49  */
50 
51 struct xfs_mount;
52 struct xfs_perag;
53 struct xfs_inode;
54 struct xfs_fsop_geom;
55 struct xfs_btree_cur;
56 struct xfs_da_args;
57 struct xfs_rtgroup;
58 
59 /* Observable health issues for metadata spanning the entire filesystem. */
60 #define XFS_SICK_FS_COUNTERS	(1 << 0)  /* summary counters */
61 #define XFS_SICK_FS_UQUOTA	(1 << 1)  /* user quota */
62 #define XFS_SICK_FS_GQUOTA	(1 << 2)  /* group quota */
63 #define XFS_SICK_FS_PQUOTA	(1 << 3)  /* project quota */
64 #define XFS_SICK_FS_QUOTACHECK	(1 << 4)  /* quota counts */
65 #define XFS_SICK_FS_NLINKS	(1 << 5)  /* inode link counts */
66 #define XFS_SICK_FS_METADIR	(1 << 6)  /* metadata directory tree */
67 #define XFS_SICK_FS_METAPATH	(1 << 7)  /* metadata directory tree path */
68 
69 /* Observable health issues for realtime group metadata. */
70 #define XFS_SICK_RG_SUPER	(1 << 0)  /* rt group superblock */
71 #define XFS_SICK_RG_BITMAP	(1 << 1)  /* rt group bitmap */
72 #define XFS_SICK_RG_SUMMARY	(1 << 2)  /* rt groups summary */
73 
74 /* Observable health issues for AG metadata. */
75 #define XFS_SICK_AG_SB		(1 << 0)  /* superblock */
76 #define XFS_SICK_AG_AGF		(1 << 1)  /* AGF header */
77 #define XFS_SICK_AG_AGFL	(1 << 2)  /* AGFL header */
78 #define XFS_SICK_AG_AGI		(1 << 3)  /* AGI header */
79 #define XFS_SICK_AG_BNOBT	(1 << 4)  /* free space by block */
80 #define XFS_SICK_AG_CNTBT	(1 << 5)  /* free space by length */
81 #define XFS_SICK_AG_INOBT	(1 << 6)  /* inode index */
82 #define XFS_SICK_AG_FINOBT	(1 << 7)  /* free inode index */
83 #define XFS_SICK_AG_RMAPBT	(1 << 8)  /* reverse mappings */
84 #define XFS_SICK_AG_REFCNTBT	(1 << 9)  /* reference counts */
85 #define XFS_SICK_AG_INODES	(1 << 10) /* inactivated bad inodes */
86 
87 /* Observable health issues for inode metadata. */
88 #define XFS_SICK_INO_CORE	(1 << 0)  /* inode core */
89 #define XFS_SICK_INO_BMBTD	(1 << 1)  /* data fork */
90 #define XFS_SICK_INO_BMBTA	(1 << 2)  /* attr fork */
91 #define XFS_SICK_INO_BMBTC	(1 << 3)  /* cow fork */
92 #define XFS_SICK_INO_DIR	(1 << 4)  /* directory */
93 #define XFS_SICK_INO_XATTR	(1 << 5)  /* extended attributes */
94 #define XFS_SICK_INO_SYMLINK	(1 << 6)  /* symbolic link remote target */
95 #define XFS_SICK_INO_PARENT	(1 << 7)  /* parent pointers */
96 
97 #define XFS_SICK_INO_BMBTD_ZAPPED	(1 << 8)  /* data fork erased */
98 #define XFS_SICK_INO_BMBTA_ZAPPED	(1 << 9)  /* attr fork erased */
99 #define XFS_SICK_INO_DIR_ZAPPED		(1 << 10) /* directory erased */
100 #define XFS_SICK_INO_SYMLINK_ZAPPED	(1 << 11) /* symlink erased */
101 
102 /* Don't propagate sick status to ag health summary during inactivation */
103 #define XFS_SICK_INO_FORGET	(1 << 12)
104 #define XFS_SICK_INO_DIRTREE	(1 << 13)  /* directory tree structure */
105 
106 /* Primary evidence of health problems in a given group. */
107 #define XFS_SICK_FS_PRIMARY	(XFS_SICK_FS_COUNTERS | \
108 				 XFS_SICK_FS_UQUOTA | \
109 				 XFS_SICK_FS_GQUOTA | \
110 				 XFS_SICK_FS_PQUOTA | \
111 				 XFS_SICK_FS_QUOTACHECK | \
112 				 XFS_SICK_FS_NLINKS | \
113 				 XFS_SICK_FS_METADIR | \
114 				 XFS_SICK_FS_METAPATH)
115 
116 #define XFS_SICK_RG_PRIMARY	(XFS_SICK_RG_SUPER | \
117 				 XFS_SICK_RG_BITMAP | \
118 				 XFS_SICK_RG_SUMMARY)
119 
120 #define XFS_SICK_AG_PRIMARY	(XFS_SICK_AG_SB | \
121 				 XFS_SICK_AG_AGF | \
122 				 XFS_SICK_AG_AGFL | \
123 				 XFS_SICK_AG_AGI | \
124 				 XFS_SICK_AG_BNOBT | \
125 				 XFS_SICK_AG_CNTBT | \
126 				 XFS_SICK_AG_INOBT | \
127 				 XFS_SICK_AG_FINOBT | \
128 				 XFS_SICK_AG_RMAPBT | \
129 				 XFS_SICK_AG_REFCNTBT)
130 
131 #define XFS_SICK_INO_PRIMARY	(XFS_SICK_INO_CORE | \
132 				 XFS_SICK_INO_BMBTD | \
133 				 XFS_SICK_INO_BMBTA | \
134 				 XFS_SICK_INO_BMBTC | \
135 				 XFS_SICK_INO_DIR | \
136 				 XFS_SICK_INO_XATTR | \
137 				 XFS_SICK_INO_SYMLINK | \
138 				 XFS_SICK_INO_PARENT | \
139 				 XFS_SICK_INO_DIRTREE)
140 
141 #define XFS_SICK_INO_ZAPPED	(XFS_SICK_INO_BMBTD_ZAPPED | \
142 				 XFS_SICK_INO_BMBTA_ZAPPED | \
143 				 XFS_SICK_INO_DIR_ZAPPED | \
144 				 XFS_SICK_INO_SYMLINK_ZAPPED)
145 
146 /* Secondary state related to (but not primary evidence of) health problems. */
147 #define XFS_SICK_FS_SECONDARY	(0)
148 #define XFS_SICK_RG_SECONDARY	(0)
149 #define XFS_SICK_AG_SECONDARY	(0)
150 #define XFS_SICK_INO_SECONDARY	(XFS_SICK_INO_FORGET)
151 
152 /* Evidence of health problems elsewhere. */
153 #define XFS_SICK_FS_INDIRECT	(0)
154 #define XFS_SICK_RG_INDIRECT	(0)
155 #define XFS_SICK_AG_INDIRECT	(XFS_SICK_AG_INODES)
156 #define XFS_SICK_INO_INDIRECT	(0)
157 
158 /* All health masks. */
159 #define XFS_SICK_FS_ALL		(XFS_SICK_FS_PRIMARY | \
160 				 XFS_SICK_FS_SECONDARY | \
161 				 XFS_SICK_FS_INDIRECT)
162 
163 #define XFS_SICK_RG_ALL		(XFS_SICK_RG_PRIMARY | \
164 				 XFS_SICK_RG_SECONDARY | \
165 				 XFS_SICK_RG_INDIRECT)
166 
167 #define XFS_SICK_AG_ALL		(XFS_SICK_AG_PRIMARY | \
168 				 XFS_SICK_AG_SECONDARY | \
169 				 XFS_SICK_AG_INDIRECT)
170 
171 #define XFS_SICK_INO_ALL	(XFS_SICK_INO_PRIMARY | \
172 				 XFS_SICK_INO_SECONDARY | \
173 				 XFS_SICK_INO_INDIRECT | \
174 				 XFS_SICK_INO_ZAPPED)
175 
176 /*
177  * These functions must be provided by the xfs implementation.  Function
178  * behavior with respect to the first argument should be as follows:
179  *
180  * xfs_*_mark_sick:        Set the sick flags and do not set checked flags.
181  *                         Runtime code should call this upon encountering
182  *                         a corruption.
183  *
184  * xfs_*_mark_corrupt:     Set the sick and checked flags simultaneously.
185  *                         Fsck tools should call this when corruption is
186  *                         found.
187  *
188  * xfs_*_mark_healthy:     Clear the sick flags and set the checked flags.
189  *                         Fsck tools should call this after correcting errors.
190  *
191  * xfs_*_measure_sickness: Return the sick and check status in the provided
192  *                         out parameters.
193  */
194 
195 void xfs_fs_mark_sick(struct xfs_mount *mp, unsigned int mask);
196 void xfs_fs_mark_corrupt(struct xfs_mount *mp, unsigned int mask);
197 void xfs_fs_mark_healthy(struct xfs_mount *mp, unsigned int mask);
198 void xfs_fs_measure_sickness(struct xfs_mount *mp, unsigned int *sick,
199 		unsigned int *checked);
200 
201 void xfs_rgno_mark_sick(struct xfs_mount *mp, xfs_rgnumber_t rgno,
202 		unsigned int mask);
203 
204 void xfs_agno_mark_sick(struct xfs_mount *mp, xfs_agnumber_t agno,
205 		unsigned int mask);
206 void xfs_group_mark_sick(struct xfs_group *xg, unsigned int mask);
207 #define xfs_ag_mark_sick(pag, mask) \
208 	xfs_group_mark_sick(pag_group(pag), (mask))
209 void xfs_group_mark_corrupt(struct xfs_group *xg, unsigned int mask);
210 void xfs_group_mark_healthy(struct xfs_group *xg, unsigned int mask);
211 void xfs_group_measure_sickness(struct xfs_group *xg, unsigned int *sick,
212 		unsigned int *checked);
213 
214 void xfs_inode_mark_sick(struct xfs_inode *ip, unsigned int mask);
215 void xfs_inode_mark_corrupt(struct xfs_inode *ip, unsigned int mask);
216 void xfs_inode_mark_healthy(struct xfs_inode *ip, unsigned int mask);
217 void xfs_inode_measure_sickness(struct xfs_inode *ip, unsigned int *sick,
218 		unsigned int *checked);
219 
220 void xfs_health_unmount(struct xfs_mount *mp);
221 void xfs_bmap_mark_sick(struct xfs_inode *ip, int whichfork);
222 void xfs_btree_mark_sick(struct xfs_btree_cur *cur);
223 void xfs_dirattr_mark_sick(struct xfs_inode *ip, int whichfork);
224 void xfs_da_mark_sick(struct xfs_da_args *args);
225 
226 /* Now some helpers. */
227 
228 static inline bool
229 xfs_fs_has_sickness(struct xfs_mount *mp, unsigned int mask)
230 {
231 	unsigned int	sick, checked;
232 
233 	xfs_fs_measure_sickness(mp, &sick, &checked);
234 	return sick & mask;
235 }
236 
237 static inline bool
238 xfs_group_has_sickness(
239 	struct xfs_group	*xg,
240 	unsigned int		mask)
241 {
242 	unsigned int		sick, checked;
243 
244 	xfs_group_measure_sickness(xg, &sick, &checked);
245 	return sick & mask;
246 }
247 
248 #define xfs_ag_has_sickness(pag, mask) \
249 	xfs_group_has_sickness(pag_group(pag), (mask))
250 #define xfs_ag_is_healthy(pag) \
251 	(!xfs_ag_has_sickness((pag), UINT_MAX))
252 
253 #define xfs_rtgroup_has_sickness(rtg, mask) \
254 	xfs_group_has_sickness(rtg_group(rtg), (mask))
255 #define xfs_rtgroup_is_healthy(rtg) \
256 	(!xfs_rtgroup_has_sickness((rtg), UINT_MAX))
257 
258 static inline bool
259 xfs_inode_has_sickness(struct xfs_inode *ip, unsigned int mask)
260 {
261 	unsigned int	sick, checked;
262 
263 	xfs_inode_measure_sickness(ip, &sick, &checked);
264 	return sick & mask;
265 }
266 
267 static inline bool
268 xfs_fs_is_healthy(struct xfs_mount *mp)
269 {
270 	return !xfs_fs_has_sickness(mp, -1U);
271 }
272 
273 static inline bool
274 xfs_inode_is_healthy(struct xfs_inode *ip)
275 {
276 	return !xfs_inode_has_sickness(ip, -1U);
277 }
278 
279 void xfs_fsop_geom_health(struct xfs_mount *mp, struct xfs_fsop_geom *geo);
280 void xfs_ag_geom_health(struct xfs_perag *pag, struct xfs_ag_geometry *ageo);
281 void xfs_rtgroup_geom_health(struct xfs_rtgroup *rtg,
282 		struct xfs_rtgroup_geometry *rgeo);
283 void xfs_bulkstat_health(struct xfs_inode *ip, struct xfs_bulkstat *bs);
284 
285 #define xfs_metadata_is_sick(error) \
286 	(unlikely((error) == -EFSCORRUPTED || (error) == -EFSBADCRC))
287 
288 #endif	/* __XFS_HEALTH_H__ */
289