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