1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2019 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trace.h"
16 #include "xfs_health.h"
19 * Warn about metadata corruption that we detected but haven't fixed, and
20 * make sure we're not sitting on anything that would get in the way of
27 struct xfs_perag *pag;
29 unsigned int sick = 0;
30 unsigned int checked = 0;
33 if (XFS_FORCED_SHUTDOWN(mp))
36 /* Measure AG corruption levels. */
37 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
38 pag = xfs_perag_get(mp, agno);
39 xfs_ag_measure_sickness(pag, &sick, &checked);
41 trace_xfs_ag_unfixed_corruption(mp, agno, sick);
47 /* Measure realtime volume corruption levels. */
48 xfs_rt_measure_sickness(mp, &sick, &checked);
50 trace_xfs_rt_unfixed_corruption(mp, sick);
55 * Measure fs corruption and keep the sample around for the warning.
56 * See the note below for why we exempt FS_COUNTERS.
58 xfs_fs_measure_sickness(mp, &sick, &checked);
59 if (sick & ~XFS_SICK_FS_COUNTERS) {
60 trace_xfs_fs_unfixed_corruption(mp, sick);
66 "Uncorrected metadata errors detected; please run xfs_repair.");
69 * We discovered uncorrected metadata problems at some point
70 * during this filesystem mount and have advised the
71 * administrator to run repair once the unmount completes.
73 * However, we must be careful -- when FSCOUNTERS are flagged
74 * unhealthy, the unmount procedure omits writing the clean
75 * unmount record to the log so that the next mount will run
76 * recovery and recompute the summary counters. In other
77 * words, we leave a dirty log to get the counters fixed.
79 * Unfortunately, xfs_repair cannot recover dirty logs, so if
80 * there were filesystem problems, FSCOUNTERS was flagged, and
81 * the administrator takes our advice to run xfs_repair,
82 * they'll have to zap the log before repairing structures.
83 * We don't really want to encourage this, so we mark the
84 * FSCOUNTERS healthy so that a subsequent repair run won't see
87 if (sick & XFS_SICK_FS_COUNTERS)
88 xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
92 /* Mark unhealthy per-fs metadata. */
98 ASSERT(!(mask & ~XFS_SICK_FS_PRIMARY));
99 trace_xfs_fs_mark_sick(mp, mask);
101 spin_lock(&mp->m_sb_lock);
102 mp->m_fs_sick |= mask;
103 mp->m_fs_checked |= mask;
104 spin_unlock(&mp->m_sb_lock);
107 /* Mark a per-fs metadata healed. */
110 struct xfs_mount *mp,
113 ASSERT(!(mask & ~XFS_SICK_FS_PRIMARY));
114 trace_xfs_fs_mark_healthy(mp, mask);
116 spin_lock(&mp->m_sb_lock);
117 mp->m_fs_sick &= ~mask;
118 mp->m_fs_checked |= mask;
119 spin_unlock(&mp->m_sb_lock);
122 /* Sample which per-fs metadata are unhealthy. */
124 xfs_fs_measure_sickness(
125 struct xfs_mount *mp,
127 unsigned int *checked)
129 spin_lock(&mp->m_sb_lock);
130 *sick = mp->m_fs_sick;
131 *checked = mp->m_fs_checked;
132 spin_unlock(&mp->m_sb_lock);
135 /* Mark unhealthy realtime metadata. */
138 struct xfs_mount *mp,
141 ASSERT(!(mask & ~XFS_SICK_RT_PRIMARY));
142 trace_xfs_rt_mark_sick(mp, mask);
144 spin_lock(&mp->m_sb_lock);
145 mp->m_rt_sick |= mask;
146 mp->m_rt_checked |= mask;
147 spin_unlock(&mp->m_sb_lock);
150 /* Mark a realtime metadata healed. */
153 struct xfs_mount *mp,
156 ASSERT(!(mask & ~XFS_SICK_RT_PRIMARY));
157 trace_xfs_rt_mark_healthy(mp, mask);
159 spin_lock(&mp->m_sb_lock);
160 mp->m_rt_sick &= ~mask;
161 mp->m_rt_checked |= mask;
162 spin_unlock(&mp->m_sb_lock);
165 /* Sample which realtime metadata are unhealthy. */
167 xfs_rt_measure_sickness(
168 struct xfs_mount *mp,
170 unsigned int *checked)
172 spin_lock(&mp->m_sb_lock);
173 *sick = mp->m_rt_sick;
174 *checked = mp->m_rt_checked;
175 spin_unlock(&mp->m_sb_lock);
178 /* Mark unhealthy per-ag metadata. */
181 struct xfs_perag *pag,
184 ASSERT(!(mask & ~XFS_SICK_AG_PRIMARY));
185 trace_xfs_ag_mark_sick(pag->pag_mount, pag->pag_agno, mask);
187 spin_lock(&pag->pag_state_lock);
188 pag->pag_sick |= mask;
189 pag->pag_checked |= mask;
190 spin_unlock(&pag->pag_state_lock);
193 /* Mark per-ag metadata ok. */
196 struct xfs_perag *pag,
199 ASSERT(!(mask & ~XFS_SICK_AG_PRIMARY));
200 trace_xfs_ag_mark_healthy(pag->pag_mount, pag->pag_agno, mask);
202 spin_lock(&pag->pag_state_lock);
203 pag->pag_sick &= ~mask;
204 pag->pag_checked |= mask;
205 spin_unlock(&pag->pag_state_lock);
208 /* Sample which per-ag metadata are unhealthy. */
210 xfs_ag_measure_sickness(
211 struct xfs_perag *pag,
213 unsigned int *checked)
215 spin_lock(&pag->pag_state_lock);
216 *sick = pag->pag_sick;
217 *checked = pag->pag_checked;
218 spin_unlock(&pag->pag_state_lock);
221 /* Mark the unhealthy parts of an inode. */
224 struct xfs_inode *ip,
227 ASSERT(!(mask & ~XFS_SICK_INO_PRIMARY));
228 trace_xfs_inode_mark_sick(ip, mask);
230 spin_lock(&ip->i_flags_lock);
232 ip->i_checked |= mask;
233 spin_unlock(&ip->i_flags_lock);
236 /* Mark parts of an inode healed. */
238 xfs_inode_mark_healthy(
239 struct xfs_inode *ip,
242 ASSERT(!(mask & ~XFS_SICK_INO_PRIMARY));
243 trace_xfs_inode_mark_healthy(ip, mask);
245 spin_lock(&ip->i_flags_lock);
247 ip->i_checked |= mask;
248 spin_unlock(&ip->i_flags_lock);
251 /* Sample which parts of an inode are unhealthy. */
253 xfs_inode_measure_sickness(
254 struct xfs_inode *ip,
256 unsigned int *checked)
258 spin_lock(&ip->i_flags_lock);
260 *checked = ip->i_checked;
261 spin_unlock(&ip->i_flags_lock);
264 /* Mappings between internal sick masks and ioctl sick masks. */
266 struct ioctl_sick_map {
267 unsigned int sick_mask;
268 unsigned int ioctl_mask;
271 static const struct ioctl_sick_map fs_map[] = {
272 { XFS_SICK_FS_COUNTERS, XFS_FSOP_GEOM_SICK_COUNTERS},
273 { XFS_SICK_FS_UQUOTA, XFS_FSOP_GEOM_SICK_UQUOTA },
274 { XFS_SICK_FS_GQUOTA, XFS_FSOP_GEOM_SICK_GQUOTA },
275 { XFS_SICK_FS_PQUOTA, XFS_FSOP_GEOM_SICK_PQUOTA },
279 static const struct ioctl_sick_map rt_map[] = {
280 { XFS_SICK_RT_BITMAP, XFS_FSOP_GEOM_SICK_RT_BITMAP },
281 { XFS_SICK_RT_SUMMARY, XFS_FSOP_GEOM_SICK_RT_SUMMARY },
287 struct xfs_fsop_geom *geo,
289 unsigned int checked,
290 const struct ioctl_sick_map *m)
292 if (checked & m->sick_mask)
293 geo->checked |= m->ioctl_mask;
294 if (sick & m->sick_mask)
295 geo->sick |= m->ioctl_mask;
298 /* Fill out fs geometry health info. */
300 xfs_fsop_geom_health(
301 struct xfs_mount *mp,
302 struct xfs_fsop_geom *geo)
304 const struct ioctl_sick_map *m;
306 unsigned int checked;
311 xfs_fs_measure_sickness(mp, &sick, &checked);
312 for (m = fs_map; m->sick_mask; m++)
313 xfgeo_health_tick(geo, sick, checked, m);
315 xfs_rt_measure_sickness(mp, &sick, &checked);
316 for (m = rt_map; m->sick_mask; m++)
317 xfgeo_health_tick(geo, sick, checked, m);
320 static const struct ioctl_sick_map ag_map[] = {
321 { XFS_SICK_AG_SB, XFS_AG_GEOM_SICK_SB },
322 { XFS_SICK_AG_AGF, XFS_AG_GEOM_SICK_AGF },
323 { XFS_SICK_AG_AGFL, XFS_AG_GEOM_SICK_AGFL },
324 { XFS_SICK_AG_AGI, XFS_AG_GEOM_SICK_AGI },
325 { XFS_SICK_AG_BNOBT, XFS_AG_GEOM_SICK_BNOBT },
326 { XFS_SICK_AG_CNTBT, XFS_AG_GEOM_SICK_CNTBT },
327 { XFS_SICK_AG_INOBT, XFS_AG_GEOM_SICK_INOBT },
328 { XFS_SICK_AG_FINOBT, XFS_AG_GEOM_SICK_FINOBT },
329 { XFS_SICK_AG_RMAPBT, XFS_AG_GEOM_SICK_RMAPBT },
330 { XFS_SICK_AG_REFCNTBT, XFS_AG_GEOM_SICK_REFCNTBT },
334 /* Fill out ag geometry health info. */
337 struct xfs_perag *pag,
338 struct xfs_ag_geometry *ageo)
340 const struct ioctl_sick_map *m;
342 unsigned int checked;
345 ageo->ag_checked = 0;
347 xfs_ag_measure_sickness(pag, &sick, &checked);
348 for (m = ag_map; m->sick_mask; m++) {
349 if (checked & m->sick_mask)
350 ageo->ag_checked |= m->ioctl_mask;
351 if (sick & m->sick_mask)
352 ageo->ag_sick |= m->ioctl_mask;
356 static const struct ioctl_sick_map ino_map[] = {
357 { XFS_SICK_INO_CORE, XFS_BS_SICK_INODE },
358 { XFS_SICK_INO_BMBTD, XFS_BS_SICK_BMBTD },
359 { XFS_SICK_INO_BMBTA, XFS_BS_SICK_BMBTA },
360 { XFS_SICK_INO_BMBTC, XFS_BS_SICK_BMBTC },
361 { XFS_SICK_INO_DIR, XFS_BS_SICK_DIR },
362 { XFS_SICK_INO_XATTR, XFS_BS_SICK_XATTR },
363 { XFS_SICK_INO_SYMLINK, XFS_BS_SICK_SYMLINK },
364 { XFS_SICK_INO_PARENT, XFS_BS_SICK_PARENT },
368 /* Fill out bulkstat health info. */
371 struct xfs_inode *ip,
372 struct xfs_bulkstat *bs)
374 const struct ioctl_sick_map *m;
376 unsigned int checked;
381 xfs_inode_measure_sickness(ip, &sick, &checked);
382 for (m = ino_map; m->sick_mask; m++) {
383 if (checked & m->sick_mask)
384 bs->bs_checked |= m->ioctl_mask;
385 if (sick & m->sick_mask)
386 bs->bs_sick |= m->ioctl_mask;