xfs_health.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Oracle. All Rights Reserved.
  4. * Author: Darrick J. Wong <[email protected]>
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trace.h"
  15. #include "xfs_health.h"
  16. #include "xfs_ag.h"
  17. /*
  18. * Warn about metadata corruption that we detected but haven't fixed, and
  19. * make sure we're not sitting on anything that would get in the way of
  20. * recovery.
  21. */
  22. void
  23. xfs_health_unmount(
  24. struct xfs_mount *mp)
  25. {
  26. struct xfs_perag *pag;
  27. xfs_agnumber_t agno;
  28. unsigned int sick = 0;
  29. unsigned int checked = 0;
  30. bool warn = false;
  31. if (xfs_is_shutdown(mp))
  32. return;
  33. /* Measure AG corruption levels. */
  34. for_each_perag(mp, agno, pag) {
  35. xfs_ag_measure_sickness(pag, &sick, &checked);
  36. if (sick) {
  37. trace_xfs_ag_unfixed_corruption(mp, agno, sick);
  38. warn = true;
  39. }
  40. }
  41. /* Measure realtime volume corruption levels. */
  42. xfs_rt_measure_sickness(mp, &sick, &checked);
  43. if (sick) {
  44. trace_xfs_rt_unfixed_corruption(mp, sick);
  45. warn = true;
  46. }
  47. /*
  48. * Measure fs corruption and keep the sample around for the warning.
  49. * See the note below for why we exempt FS_COUNTERS.
  50. */
  51. xfs_fs_measure_sickness(mp, &sick, &checked);
  52. if (sick & ~XFS_SICK_FS_COUNTERS) {
  53. trace_xfs_fs_unfixed_corruption(mp, sick);
  54. warn = true;
  55. }
  56. if (warn) {
  57. xfs_warn(mp,
  58. "Uncorrected metadata errors detected; please run xfs_repair.");
  59. /*
  60. * We discovered uncorrected metadata problems at some point
  61. * during this filesystem mount and have advised the
  62. * administrator to run repair once the unmount completes.
  63. *
  64. * However, we must be careful -- when FSCOUNTERS are flagged
  65. * unhealthy, the unmount procedure omits writing the clean
  66. * unmount record to the log so that the next mount will run
  67. * recovery and recompute the summary counters. In other
  68. * words, we leave a dirty log to get the counters fixed.
  69. *
  70. * Unfortunately, xfs_repair cannot recover dirty logs, so if
  71. * there were filesystem problems, FSCOUNTERS was flagged, and
  72. * the administrator takes our advice to run xfs_repair,
  73. * they'll have to zap the log before repairing structures.
  74. * We don't really want to encourage this, so we mark the
  75. * FSCOUNTERS healthy so that a subsequent repair run won't see
  76. * a dirty log.
  77. */
  78. if (sick & XFS_SICK_FS_COUNTERS)
  79. xfs_fs_mark_healthy(mp, XFS_SICK_FS_COUNTERS);
  80. }
  81. }
  82. /* Mark unhealthy per-fs metadata. */
  83. void
  84. xfs_fs_mark_sick(
  85. struct xfs_mount *mp,
  86. unsigned int mask)
  87. {
  88. ASSERT(!(mask & ~XFS_SICK_FS_PRIMARY));
  89. trace_xfs_fs_mark_sick(mp, mask);
  90. spin_lock(&mp->m_sb_lock);
  91. mp->m_fs_sick |= mask;
  92. mp->m_fs_checked |= mask;
  93. spin_unlock(&mp->m_sb_lock);
  94. }
  95. /* Mark a per-fs metadata healed. */
  96. void
  97. xfs_fs_mark_healthy(
  98. struct xfs_mount *mp,
  99. unsigned int mask)
  100. {
  101. ASSERT(!(mask & ~XFS_SICK_FS_PRIMARY));
  102. trace_xfs_fs_mark_healthy(mp, mask);
  103. spin_lock(&mp->m_sb_lock);
  104. mp->m_fs_sick &= ~mask;
  105. mp->m_fs_checked |= mask;
  106. spin_unlock(&mp->m_sb_lock);
  107. }
  108. /* Sample which per-fs metadata are unhealthy. */
  109. void
  110. xfs_fs_measure_sickness(
  111. struct xfs_mount *mp,
  112. unsigned int *sick,
  113. unsigned int *checked)
  114. {
  115. spin_lock(&mp->m_sb_lock);
  116. *sick = mp->m_fs_sick;
  117. *checked = mp->m_fs_checked;
  118. spin_unlock(&mp->m_sb_lock);
  119. }
  120. /* Mark unhealthy realtime metadata. */
  121. void
  122. xfs_rt_mark_sick(
  123. struct xfs_mount *mp,
  124. unsigned int mask)
  125. {
  126. ASSERT(!(mask & ~XFS_SICK_RT_PRIMARY));
  127. trace_xfs_rt_mark_sick(mp, mask);
  128. spin_lock(&mp->m_sb_lock);
  129. mp->m_rt_sick |= mask;
  130. mp->m_rt_checked |= mask;
  131. spin_unlock(&mp->m_sb_lock);
  132. }
  133. /* Mark a realtime metadata healed. */
  134. void
  135. xfs_rt_mark_healthy(
  136. struct xfs_mount *mp,
  137. unsigned int mask)
  138. {
  139. ASSERT(!(mask & ~XFS_SICK_RT_PRIMARY));
  140. trace_xfs_rt_mark_healthy(mp, mask);
  141. spin_lock(&mp->m_sb_lock);
  142. mp->m_rt_sick &= ~mask;
  143. mp->m_rt_checked |= mask;
  144. spin_unlock(&mp->m_sb_lock);
  145. }
  146. /* Sample which realtime metadata are unhealthy. */
  147. void
  148. xfs_rt_measure_sickness(
  149. struct xfs_mount *mp,
  150. unsigned int *sick,
  151. unsigned int *checked)
  152. {
  153. spin_lock(&mp->m_sb_lock);
  154. *sick = mp->m_rt_sick;
  155. *checked = mp->m_rt_checked;
  156. spin_unlock(&mp->m_sb_lock);
  157. }
  158. /* Mark unhealthy per-ag metadata. */
  159. void
  160. xfs_ag_mark_sick(
  161. struct xfs_perag *pag,
  162. unsigned int mask)
  163. {
  164. ASSERT(!(mask & ~XFS_SICK_AG_PRIMARY));
  165. trace_xfs_ag_mark_sick(pag->pag_mount, pag->pag_agno, mask);
  166. spin_lock(&pag->pag_state_lock);
  167. pag->pag_sick |= mask;
  168. pag->pag_checked |= mask;
  169. spin_unlock(&pag->pag_state_lock);
  170. }
  171. /* Mark per-ag metadata ok. */
  172. void
  173. xfs_ag_mark_healthy(
  174. struct xfs_perag *pag,
  175. unsigned int mask)
  176. {
  177. ASSERT(!(mask & ~XFS_SICK_AG_PRIMARY));
  178. trace_xfs_ag_mark_healthy(pag->pag_mount, pag->pag_agno, mask);
  179. spin_lock(&pag->pag_state_lock);
  180. pag->pag_sick &= ~mask;
  181. pag->pag_checked |= mask;
  182. spin_unlock(&pag->pag_state_lock);
  183. }
  184. /* Sample which per-ag metadata are unhealthy. */
  185. void
  186. xfs_ag_measure_sickness(
  187. struct xfs_perag *pag,
  188. unsigned int *sick,
  189. unsigned int *checked)
  190. {
  191. spin_lock(&pag->pag_state_lock);
  192. *sick = pag->pag_sick;
  193. *checked = pag->pag_checked;
  194. spin_unlock(&pag->pag_state_lock);
  195. }
  196. /* Mark the unhealthy parts of an inode. */
  197. void
  198. xfs_inode_mark_sick(
  199. struct xfs_inode *ip,
  200. unsigned int mask)
  201. {
  202. ASSERT(!(mask & ~XFS_SICK_INO_PRIMARY));
  203. trace_xfs_inode_mark_sick(ip, mask);
  204. spin_lock(&ip->i_flags_lock);
  205. ip->i_sick |= mask;
  206. ip->i_checked |= mask;
  207. spin_unlock(&ip->i_flags_lock);
  208. /*
  209. * Keep this inode around so we don't lose the sickness report. Scrub
  210. * grabs inodes with DONTCACHE assuming that most inode are ok, which
  211. * is not the case here.
  212. */
  213. spin_lock(&VFS_I(ip)->i_lock);
  214. VFS_I(ip)->i_state &= ~I_DONTCACHE;
  215. spin_unlock(&VFS_I(ip)->i_lock);
  216. }
  217. /* Mark parts of an inode healed. */
  218. void
  219. xfs_inode_mark_healthy(
  220. struct xfs_inode *ip,
  221. unsigned int mask)
  222. {
  223. ASSERT(!(mask & ~XFS_SICK_INO_PRIMARY));
  224. trace_xfs_inode_mark_healthy(ip, mask);
  225. spin_lock(&ip->i_flags_lock);
  226. ip->i_sick &= ~mask;
  227. ip->i_checked |= mask;
  228. spin_unlock(&ip->i_flags_lock);
  229. }
  230. /* Sample which parts of an inode are unhealthy. */
  231. void
  232. xfs_inode_measure_sickness(
  233. struct xfs_inode *ip,
  234. unsigned int *sick,
  235. unsigned int *checked)
  236. {
  237. spin_lock(&ip->i_flags_lock);
  238. *sick = ip->i_sick;
  239. *checked = ip->i_checked;
  240. spin_unlock(&ip->i_flags_lock);
  241. }
  242. /* Mappings between internal sick masks and ioctl sick masks. */
  243. struct ioctl_sick_map {
  244. unsigned int sick_mask;
  245. unsigned int ioctl_mask;
  246. };
  247. static const struct ioctl_sick_map fs_map[] = {
  248. { XFS_SICK_FS_COUNTERS, XFS_FSOP_GEOM_SICK_COUNTERS},
  249. { XFS_SICK_FS_UQUOTA, XFS_FSOP_GEOM_SICK_UQUOTA },
  250. { XFS_SICK_FS_GQUOTA, XFS_FSOP_GEOM_SICK_GQUOTA },
  251. { XFS_SICK_FS_PQUOTA, XFS_FSOP_GEOM_SICK_PQUOTA },
  252. { 0, 0 },
  253. };
  254. static const struct ioctl_sick_map rt_map[] = {
  255. { XFS_SICK_RT_BITMAP, XFS_FSOP_GEOM_SICK_RT_BITMAP },
  256. { XFS_SICK_RT_SUMMARY, XFS_FSOP_GEOM_SICK_RT_SUMMARY },
  257. { 0, 0 },
  258. };
  259. static inline void
  260. xfgeo_health_tick(
  261. struct xfs_fsop_geom *geo,
  262. unsigned int sick,
  263. unsigned int checked,
  264. const struct ioctl_sick_map *m)
  265. {
  266. if (checked & m->sick_mask)
  267. geo->checked |= m->ioctl_mask;
  268. if (sick & m->sick_mask)
  269. geo->sick |= m->ioctl_mask;
  270. }
  271. /* Fill out fs geometry health info. */
  272. void
  273. xfs_fsop_geom_health(
  274. struct xfs_mount *mp,
  275. struct xfs_fsop_geom *geo)
  276. {
  277. const struct ioctl_sick_map *m;
  278. unsigned int sick;
  279. unsigned int checked;
  280. geo->sick = 0;
  281. geo->checked = 0;
  282. xfs_fs_measure_sickness(mp, &sick, &checked);
  283. for (m = fs_map; m->sick_mask; m++)
  284. xfgeo_health_tick(geo, sick, checked, m);
  285. xfs_rt_measure_sickness(mp, &sick, &checked);
  286. for (m = rt_map; m->sick_mask; m++)
  287. xfgeo_health_tick(geo, sick, checked, m);
  288. }
  289. static const struct ioctl_sick_map ag_map[] = {
  290. { XFS_SICK_AG_SB, XFS_AG_GEOM_SICK_SB },
  291. { XFS_SICK_AG_AGF, XFS_AG_GEOM_SICK_AGF },
  292. { XFS_SICK_AG_AGFL, XFS_AG_GEOM_SICK_AGFL },
  293. { XFS_SICK_AG_AGI, XFS_AG_GEOM_SICK_AGI },
  294. { XFS_SICK_AG_BNOBT, XFS_AG_GEOM_SICK_BNOBT },
  295. { XFS_SICK_AG_CNTBT, XFS_AG_GEOM_SICK_CNTBT },
  296. { XFS_SICK_AG_INOBT, XFS_AG_GEOM_SICK_INOBT },
  297. { XFS_SICK_AG_FINOBT, XFS_AG_GEOM_SICK_FINOBT },
  298. { XFS_SICK_AG_RMAPBT, XFS_AG_GEOM_SICK_RMAPBT },
  299. { XFS_SICK_AG_REFCNTBT, XFS_AG_GEOM_SICK_REFCNTBT },
  300. { 0, 0 },
  301. };
  302. /* Fill out ag geometry health info. */
  303. void
  304. xfs_ag_geom_health(
  305. struct xfs_perag *pag,
  306. struct xfs_ag_geometry *ageo)
  307. {
  308. const struct ioctl_sick_map *m;
  309. unsigned int sick;
  310. unsigned int checked;
  311. ageo->ag_sick = 0;
  312. ageo->ag_checked = 0;
  313. xfs_ag_measure_sickness(pag, &sick, &checked);
  314. for (m = ag_map; m->sick_mask; m++) {
  315. if (checked & m->sick_mask)
  316. ageo->ag_checked |= m->ioctl_mask;
  317. if (sick & m->sick_mask)
  318. ageo->ag_sick |= m->ioctl_mask;
  319. }
  320. }
  321. static const struct ioctl_sick_map ino_map[] = {
  322. { XFS_SICK_INO_CORE, XFS_BS_SICK_INODE },
  323. { XFS_SICK_INO_BMBTD, XFS_BS_SICK_BMBTD },
  324. { XFS_SICK_INO_BMBTA, XFS_BS_SICK_BMBTA },
  325. { XFS_SICK_INO_BMBTC, XFS_BS_SICK_BMBTC },
  326. { XFS_SICK_INO_DIR, XFS_BS_SICK_DIR },
  327. { XFS_SICK_INO_XATTR, XFS_BS_SICK_XATTR },
  328. { XFS_SICK_INO_SYMLINK, XFS_BS_SICK_SYMLINK },
  329. { XFS_SICK_INO_PARENT, XFS_BS_SICK_PARENT },
  330. { 0, 0 },
  331. };
  332. /* Fill out bulkstat health info. */
  333. void
  334. xfs_bulkstat_health(
  335. struct xfs_inode *ip,
  336. struct xfs_bulkstat *bs)
  337. {
  338. const struct ioctl_sick_map *m;
  339. unsigned int sick;
  340. unsigned int checked;
  341. bs->bs_sick = 0;
  342. bs->bs_checked = 0;
  343. xfs_inode_measure_sickness(ip, &sick, &checked);
  344. for (m = ino_map; m->sick_mask; m++) {
  345. if (checked & m->sick_mask)
  346. bs->bs_checked |= m->ioctl_mask;
  347. if (sick & m->sick_mask)
  348. bs->bs_sick |= m->ioctl_mask;
  349. }
  350. }