1 // SPDX-License-Identifier: GPL-2.0-or-later 1 2 /* 3 * Copyright (C) 2019-2023 Oracle. All Rights 4 * Author: Darrick J. Wong <djwong@kernel.org> 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_btree.h" 13 #include "xfs_ag.h" 14 #include "xfs_health.h" 15 #include "scrub/scrub.h" 16 #include "scrub/health.h" 17 #include "scrub/common.h" 18 19 /* 20 * Scrub and In-Core Filesystem Health Assessm 21 * =========================================== 22 * 23 * Online scrub and repair have the time and t 24 * checks than we can do from the metadata ver 25 * cross-reference records between data struct 26 * good position to update the online filesyst 27 * the good/bad state of the data structure. 28 * 29 * We therefore extend scrub in the following 30 * 31 * 1. Create a "sick_mask" field in the scrub 32 * scrub call, set this to the default XFS_SIC 33 * scrub type (call it A). Scrub and repair f 34 * sick_mask value if they choose. 35 * 36 * 2. If the scrubber returns a runtime error 37 * to the incore sick state. 38 * 39 * 3. If the scrubber finds that A is clean, u 40 * sick flags before exiting. 41 * 42 * 4. If the scrubber finds that A is corrupt, 43 * sick flags. If the user didn't want to rep 44 * metadata structure unfixed and the sick fla 45 * 46 * 5. Now we know that A is corrupt and the us 47 * repairer. If the repairer returns an error 48 * code, having made no further changes to the 49 * 50 * 6. If repair rebuilds A correctly and the s 51 * use sick_mask to clear the incore sick flag 52 * that A is no longer marked sick. 53 * 54 * 7. If repair rebuilds A incorrectly, the re 55 * use sick_mask to set the incore sick flags. 56 * visible effect since we already set them in 57 * 58 * There are some complications to this story, 59 * complementary metadata indices (e.g. inobt/ 60 * both structures at the same time. The foll 61 * type of repair strategy: 62 * 63 * 8. Any repair function that rebuilds multip 64 * sick_mask_visible to reflect whatever other 65 * verify that all the rebuilt structures can 66 * of 5-7 still apply, but with a sick_mask th 67 * rebuilt. 68 */ 69 70 /* Map our scrub type to a sick mask and a set 71 72 enum xchk_health_group { 73 XHG_FS = 1, 74 XHG_RT, 75 XHG_AG, 76 XHG_INO, 77 }; 78 79 struct xchk_health_map { 80 enum xchk_health_group group; 81 unsigned int sick_mask; 82 }; 83 84 static const struct xchk_health_map type_to_he 85 [XFS_SCRUB_TYPE_SB] = { XH 86 [XFS_SCRUB_TYPE_AGF] = { XH 87 [XFS_SCRUB_TYPE_AGFL] = { XH 88 [XFS_SCRUB_TYPE_AGI] = { XH 89 [XFS_SCRUB_TYPE_BNOBT] = { XH 90 [XFS_SCRUB_TYPE_CNTBT] = { XH 91 [XFS_SCRUB_TYPE_INOBT] = { XH 92 [XFS_SCRUB_TYPE_FINOBT] = { XH 93 [XFS_SCRUB_TYPE_RMAPBT] = { XH 94 [XFS_SCRUB_TYPE_REFCNTBT] = { XH 95 [XFS_SCRUB_TYPE_INODE] = { XH 96 [XFS_SCRUB_TYPE_BMBTD] = { XH 97 [XFS_SCRUB_TYPE_BMBTA] = { XH 98 [XFS_SCRUB_TYPE_BMBTC] = { XH 99 [XFS_SCRUB_TYPE_DIR] = { XH 100 [XFS_SCRUB_TYPE_XATTR] = { XH 101 [XFS_SCRUB_TYPE_SYMLINK] = { XH 102 [XFS_SCRUB_TYPE_PARENT] = { XH 103 [XFS_SCRUB_TYPE_RTBITMAP] = { XH 104 [XFS_SCRUB_TYPE_RTSUM] = { XH 105 [XFS_SCRUB_TYPE_UQUOTA] = { XH 106 [XFS_SCRUB_TYPE_GQUOTA] = { XH 107 [XFS_SCRUB_TYPE_PQUOTA] = { XH 108 [XFS_SCRUB_TYPE_FSCOUNTERS] = { XH 109 [XFS_SCRUB_TYPE_QUOTACHECK] = { XH 110 [XFS_SCRUB_TYPE_NLINKS] = { XH 111 [XFS_SCRUB_TYPE_DIRTREE] = { XH 112 }; 113 114 /* Return the health status mask for this scru 115 unsigned int 116 xchk_health_mask_for_scrub_type( 117 __u32 scrub_type) 118 { 119 return type_to_health_flag[scrub_type] 120 } 121 122 /* 123 * If the scrub state is clean, add @mask to t 124 * additional sick flags from the metadata obj 125 */ 126 void 127 xchk_mark_healthy_if_clean( 128 struct xfs_scrub *sc, 129 unsigned int mask) 130 { 131 if (!(sc->sm->sm_flags & (XFS_SCRUB_OF 132 XFS_SCRUB_OF 133 sc->sick_mask |= mask; 134 } 135 136 /* 137 * If we're scrubbing a piece of file metadata 138 * like it has been zapped? Skip the check if 139 * and are revalidating it. 140 */ 141 bool 142 xchk_file_looks_zapped( 143 struct xfs_scrub *sc, 144 unsigned int mask) 145 { 146 ASSERT((mask & ~XFS_SICK_INO_ZAPPED) = 147 148 if (sc->flags & XREP_ALREADY_FIXED) 149 return false; 150 151 return xfs_inode_has_sickness(sc->ip, 152 } 153 154 /* 155 * Scrub gave the filesystem a clean bill of h 156 * markers of past problems (at least for the 157 * healthy again. 158 */ 159 STATIC void 160 xchk_mark_all_healthy( 161 struct xfs_mount *mp) 162 { 163 struct xfs_perag *pag; 164 xfs_agnumber_t agno; 165 166 xfs_fs_mark_healthy(mp, XFS_SICK_FS_IN 167 xfs_rt_mark_healthy(mp, XFS_SICK_RT_IN 168 for_each_perag(mp, agno, pag) 169 xfs_ag_mark_healthy(pag, XFS_S 170 } 171 172 /* 173 * Update filesystem health assessments based 174 * 175 * If the scrubber finds errors, we mark sick 176 * sick_mask, no matter whether this is a firs 177 * evaluation of repair effectiveness. 178 * 179 * Otherwise, no direct corruption was found, 180 * sick_mask as healthy. 181 */ 182 void 183 xchk_update_health( 184 struct xfs_scrub *sc) 185 { 186 struct xfs_perag *pag; 187 bool bad; 188 189 /* 190 * The HEALTHY scrub type is a request 191 * indirect flags after a clean scan o 192 * there's no sick flag defined for it 193 * mask check. 194 */ 195 if (sc->sm->sm_type == XFS_SCRUB_TYPE_ 196 !(sc->sm->sm_flags & XFS_SCRUB_OFL 197 xchk_mark_all_healthy(sc->mp); 198 return; 199 } 200 201 if (!sc->sick_mask) 202 return; 203 204 bad = (sc->sm->sm_flags & (XFS_SCRUB_O 205 XFS_SCRUB_O 206 switch (type_to_health_flag[sc->sm->sm 207 case XHG_AG: 208 pag = xfs_perag_get(sc->mp, sc 209 if (bad) 210 xfs_ag_mark_corrupt(pa 211 else 212 xfs_ag_mark_healthy(pa 213 xfs_perag_put(pag); 214 break; 215 case XHG_INO: 216 if (!sc->ip) 217 return; 218 if (bad) { 219 unsigned int mask = 220 221 /* 222 * If we're coming in 223 * sickness flags to p 224 * status if the inode 225 * fix it. 226 */ 227 if (sc->sm->sm_flags & 228 mask |= XFS_SI 229 xfs_inode_mark_corrupt 230 } else 231 xfs_inode_mark_healthy 232 break; 233 case XHG_FS: 234 if (bad) 235 xfs_fs_mark_corrupt(sc 236 else 237 xfs_fs_mark_healthy(sc 238 break; 239 case XHG_RT: 240 if (bad) 241 xfs_rt_mark_corrupt(sc 242 else 243 xfs_rt_mark_healthy(sc 244 break; 245 default: 246 ASSERT(0); 247 break; 248 } 249 } 250 251 /* Is the given per-AG btree healthy enough fo 252 void 253 xchk_ag_btree_del_cursor_if_sick( 254 struct xfs_scrub *sc, 255 struct xfs_btree_cur **curp, 256 unsigned int sm_type) 257 { 258 unsigned int mask = (*curp) 259 260 /* 261 * We always want the cursor if it's t 262 * scrubbing, even if we already know 263 * 264 * Otherwise, we're only interested in 265 * If we know the btree is bad then do 266 */ 267 if (sc->sm->sm_type == sm_type) 268 return; 269 270 /* 271 * If we just repaired some AG metadat 272 * the per-AG metadata types that were 273 * the filesystem health query because 274 * health status and we want everythin 275 */ 276 if ((sc->flags & XREP_ALREADY_FIXED) & 277 type_to_health_flag[sc->sm->sm_typ 278 mask &= ~sc->sick_mask; 279 280 if (xfs_ag_has_sickness((*curp)->bc_ag 281 sc->sm->sm_flags |= XFS_SCRUB_ 282 xfs_btree_del_cursor(*curp, XF 283 *curp = NULL; 284 } 285 } 286 287 /* 288 * Quick scan to double-check that there isn't 289 * primary health problems. If we're still cl 290 * take care of clearing the indirect evidence 291 */ 292 int 293 xchk_health_record( 294 struct xfs_scrub *sc) 295 { 296 struct xfs_mount *mp = sc->mp; 297 struct xfs_perag *pag; 298 xfs_agnumber_t agno; 299 300 unsigned int sick; 301 unsigned int checked; 302 303 xfs_fs_measure_sickness(mp, &sick, &ch 304 if (sick & XFS_SICK_FS_PRIMARY) 305 xchk_set_corrupt(sc); 306 307 xfs_rt_measure_sickness(mp, &sick, &ch 308 if (sick & XFS_SICK_RT_PRIMARY) 309 xchk_set_corrupt(sc); 310 311 for_each_perag(mp, agno, pag) { 312 xfs_ag_measure_sickness(pag, & 313 if (sick & XFS_SICK_AG_PRIMARY 314 xchk_set_corrupt(sc); 315 } 316 317 return 0; 318 } 319
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.