14860a05dSDarrick J. Wong // SPDX-License-Identifier: GPL-2.0+ 24860a05dSDarrick J. Wong /* 34860a05dSDarrick J. Wong * Copyright (C) 2019 Oracle. All Rights Reserved. 44860a05dSDarrick J. Wong * Author: Darrick J. Wong <darrick.wong@oracle.com> 54860a05dSDarrick J. Wong */ 64860a05dSDarrick J. Wong #include "xfs.h" 74860a05dSDarrick J. Wong #include "xfs_fs.h" 84860a05dSDarrick J. Wong #include "xfs_shared.h" 94860a05dSDarrick J. Wong #include "xfs_format.h" 104860a05dSDarrick J. Wong #include "xfs_trans_resv.h" 114860a05dSDarrick J. Wong #include "xfs_mount.h" 124860a05dSDarrick J. Wong #include "xfs_defer.h" 134860a05dSDarrick J. Wong #include "xfs_btree.h" 144860a05dSDarrick J. Wong #include "xfs_bit.h" 154860a05dSDarrick J. Wong #include "xfs_log_format.h" 164860a05dSDarrick J. Wong #include "xfs_trans.h" 174860a05dSDarrick J. Wong #include "xfs_sb.h" 184860a05dSDarrick J. Wong #include "xfs_inode.h" 194860a05dSDarrick J. Wong #include "xfs_health.h" 204860a05dSDarrick J. Wong #include "scrub/scrub.h" 214860a05dSDarrick J. Wong #include "scrub/health.h" 224860a05dSDarrick J. Wong 234860a05dSDarrick J. Wong /* 244860a05dSDarrick J. Wong * Scrub and In-Core Filesystem Health Assessments 254860a05dSDarrick J. Wong * =============================================== 264860a05dSDarrick J. Wong * 274860a05dSDarrick J. Wong * Online scrub and repair have the time and the ability to perform stronger 284860a05dSDarrick J. Wong * checks than we can do from the metadata verifiers, because they can 294860a05dSDarrick J. Wong * cross-reference records between data structures. Therefore, scrub is in a 304860a05dSDarrick J. Wong * good position to update the online filesystem health assessments to reflect 314860a05dSDarrick J. Wong * the good/bad state of the data structure. 324860a05dSDarrick J. Wong * 334860a05dSDarrick J. Wong * We therefore extend scrub in the following ways to achieve this: 344860a05dSDarrick J. Wong * 354860a05dSDarrick J. Wong * 1. Create a "sick_mask" field in the scrub context. When we're setting up a 364860a05dSDarrick J. Wong * scrub call, set this to the default XFS_SICK_* flag(s) for the selected 374860a05dSDarrick J. Wong * scrub type (call it A). Scrub and repair functions can override the default 384860a05dSDarrick J. Wong * sick_mask value if they choose. 394860a05dSDarrick J. Wong * 404860a05dSDarrick J. Wong * 2. If the scrubber returns a runtime error code, we exit making no changes 414860a05dSDarrick J. Wong * to the incore sick state. 424860a05dSDarrick J. Wong * 434860a05dSDarrick J. Wong * 3. If the scrubber finds that A is clean, use sick_mask to clear the incore 444860a05dSDarrick J. Wong * sick flags before exiting. 454860a05dSDarrick J. Wong * 464860a05dSDarrick J. Wong * 4. If the scrubber finds that A is corrupt, use sick_mask to set the incore 474860a05dSDarrick J. Wong * sick flags. If the user didn't want to repair then we exit, leaving the 484860a05dSDarrick J. Wong * metadata structure unfixed and the sick flag set. 494860a05dSDarrick J. Wong * 504860a05dSDarrick J. Wong * 5. Now we know that A is corrupt and the user wants to repair, so run the 514860a05dSDarrick J. Wong * repairer. If the repairer returns an error code, we exit with that error 524860a05dSDarrick J. Wong * code, having made no further changes to the incore sick state. 534860a05dSDarrick J. Wong * 544860a05dSDarrick J. Wong * 6. If repair rebuilds A correctly and the subsequent re-scrub of A is clean, 554860a05dSDarrick J. Wong * use sick_mask to clear the incore sick flags. This should have the effect 564860a05dSDarrick J. Wong * that A is no longer marked sick. 574860a05dSDarrick J. Wong * 584860a05dSDarrick J. Wong * 7. If repair rebuilds A incorrectly, the re-scrub will find it corrupt and 594860a05dSDarrick J. Wong * use sick_mask to set the incore sick flags. This should have no externally 604860a05dSDarrick J. Wong * visible effect since we already set them in step (4). 614860a05dSDarrick J. Wong * 624860a05dSDarrick J. Wong * There are some complications to this story, however. For certain types of 634860a05dSDarrick J. Wong * complementary metadata indices (e.g. inobt/finobt), it is easier to rebuild 644860a05dSDarrick J. Wong * both structures at the same time. The following principles apply to this 654860a05dSDarrick J. Wong * type of repair strategy: 664860a05dSDarrick J. Wong * 674860a05dSDarrick J. Wong * 8. Any repair function that rebuilds multiple structures should update 684860a05dSDarrick J. Wong * sick_mask_visible to reflect whatever other structures are rebuilt, and 694860a05dSDarrick J. Wong * verify that all the rebuilt structures can pass a scrub check. The outcomes 704860a05dSDarrick J. Wong * of 5-7 still apply, but with a sick_mask that covers everything being 714860a05dSDarrick J. Wong * rebuilt. 724860a05dSDarrick J. Wong */ 734860a05dSDarrick J. Wong 744860a05dSDarrick J. Wong /* Map our scrub type to a sick mask and a set of health update functions. */ 754860a05dSDarrick J. Wong 764860a05dSDarrick J. Wong enum xchk_health_group { 774860a05dSDarrick J. Wong XHG_FS = 1, 784860a05dSDarrick J. Wong XHG_RT, 794860a05dSDarrick J. Wong XHG_AG, 804860a05dSDarrick J. Wong XHG_INO, 814860a05dSDarrick J. Wong }; 824860a05dSDarrick J. Wong 834860a05dSDarrick J. Wong struct xchk_health_map { 844860a05dSDarrick J. Wong enum xchk_health_group group; 854860a05dSDarrick J. Wong unsigned int sick_mask; 864860a05dSDarrick J. Wong }; 874860a05dSDarrick J. Wong 884860a05dSDarrick J. Wong static const struct xchk_health_map type_to_health_flag[XFS_SCRUB_TYPE_NR] = { 894860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_SB] = { XHG_AG, XFS_SICK_AG_SB }, 904860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_AGF] = { XHG_AG, XFS_SICK_AG_AGF }, 914860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_AGFL] = { XHG_AG, XFS_SICK_AG_AGFL }, 924860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_AGI] = { XHG_AG, XFS_SICK_AG_AGI }, 934860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_BNOBT] = { XHG_AG, XFS_SICK_AG_BNOBT }, 944860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_CNTBT] = { XHG_AG, XFS_SICK_AG_CNTBT }, 954860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_INOBT] = { XHG_AG, XFS_SICK_AG_INOBT }, 964860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_FINOBT] = { XHG_AG, XFS_SICK_AG_FINOBT }, 974860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_RMAPBT] = { XHG_AG, XFS_SICK_AG_RMAPBT }, 984860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_REFCNTBT] = { XHG_AG, XFS_SICK_AG_REFCNTBT }, 994860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_INODE] = { XHG_INO, XFS_SICK_INO_CORE }, 1004860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_BMBTD] = { XHG_INO, XFS_SICK_INO_BMBTD }, 1014860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_BMBTA] = { XHG_INO, XFS_SICK_INO_BMBTA }, 1024860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_BMBTC] = { XHG_INO, XFS_SICK_INO_BMBTC }, 1034860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_DIR] = { XHG_INO, XFS_SICK_INO_DIR }, 1044860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_XATTR] = { XHG_INO, XFS_SICK_INO_XATTR }, 1054860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_SYMLINK] = { XHG_INO, XFS_SICK_INO_SYMLINK }, 1064860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_PARENT] = { XHG_INO, XFS_SICK_INO_PARENT }, 1074860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_RTBITMAP] = { XHG_RT, XFS_SICK_RT_BITMAP }, 1084860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_RTSUM] = { XHG_RT, XFS_SICK_RT_SUMMARY }, 1094860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_UQUOTA] = { XHG_FS, XFS_SICK_FS_UQUOTA }, 1104860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_GQUOTA] = { XHG_FS, XFS_SICK_FS_GQUOTA }, 1114860a05dSDarrick J. Wong [XFS_SCRUB_TYPE_PQUOTA] = { XHG_FS, XFS_SICK_FS_PQUOTA }, 1124860a05dSDarrick J. Wong }; 1134860a05dSDarrick J. Wong 1144860a05dSDarrick J. Wong /* Return the health status mask for this scrub type. */ 1154860a05dSDarrick J. Wong unsigned int 1164860a05dSDarrick J. Wong xchk_health_mask_for_scrub_type( 1174860a05dSDarrick J. Wong __u32 scrub_type) 1184860a05dSDarrick J. Wong { 1194860a05dSDarrick J. Wong return type_to_health_flag[scrub_type].sick_mask; 1204860a05dSDarrick J. Wong } 1214860a05dSDarrick J. Wong 1224860a05dSDarrick J. Wong /* 1234860a05dSDarrick J. Wong * Update filesystem health assessments based on what we found and did. 1244860a05dSDarrick J. Wong * 1254860a05dSDarrick J. Wong * If the scrubber finds errors, we mark sick whatever's mentioned in 1264860a05dSDarrick J. Wong * sick_mask, no matter whether this is a first scan or an 1274860a05dSDarrick J. Wong * evaluation of repair effectiveness. 1284860a05dSDarrick J. Wong * 1294860a05dSDarrick J. Wong * Otherwise, no direct corruption was found, so mark whatever's in 1304860a05dSDarrick J. Wong * sick_mask as healthy. 1314860a05dSDarrick J. Wong */ 1324860a05dSDarrick J. Wong void 1334860a05dSDarrick J. Wong xchk_update_health( 1344860a05dSDarrick J. Wong struct xfs_scrub *sc) 1354860a05dSDarrick J. Wong { 1364860a05dSDarrick J. Wong struct xfs_perag *pag; 1374860a05dSDarrick J. Wong bool bad; 1384860a05dSDarrick J. Wong 1394860a05dSDarrick J. Wong if (!sc->sick_mask) 1404860a05dSDarrick J. Wong return; 1414860a05dSDarrick J. Wong 1424860a05dSDarrick J. Wong bad = (sc->sm->sm_flags & XFS_SCRUB_OFLAG_CORRUPT); 1434860a05dSDarrick J. Wong switch (type_to_health_flag[sc->sm->sm_type].group) { 1444860a05dSDarrick J. Wong case XHG_AG: 1454860a05dSDarrick J. Wong pag = xfs_perag_get(sc->mp, sc->sm->sm_agno); 1464860a05dSDarrick J. Wong if (bad) 1474860a05dSDarrick J. Wong xfs_ag_mark_sick(pag, sc->sick_mask); 1484860a05dSDarrick J. Wong else 1494860a05dSDarrick J. Wong xfs_ag_mark_healthy(pag, sc->sick_mask); 1504860a05dSDarrick J. Wong xfs_perag_put(pag); 1514860a05dSDarrick J. Wong break; 1524860a05dSDarrick J. Wong case XHG_INO: 1534860a05dSDarrick J. Wong if (!sc->ip) 1544860a05dSDarrick J. Wong return; 1554860a05dSDarrick J. Wong if (bad) 1564860a05dSDarrick J. Wong xfs_inode_mark_sick(sc->ip, sc->sick_mask); 1574860a05dSDarrick J. Wong else 1584860a05dSDarrick J. Wong xfs_inode_mark_healthy(sc->ip, sc->sick_mask); 1594860a05dSDarrick J. Wong break; 1604860a05dSDarrick J. Wong case XHG_FS: 1614860a05dSDarrick J. Wong if (bad) 1624860a05dSDarrick J. Wong xfs_fs_mark_sick(sc->mp, sc->sick_mask); 1634860a05dSDarrick J. Wong else 1644860a05dSDarrick J. Wong xfs_fs_mark_healthy(sc->mp, sc->sick_mask); 1654860a05dSDarrick J. Wong break; 1664860a05dSDarrick J. Wong case XHG_RT: 1674860a05dSDarrick J. Wong if (bad) 1684860a05dSDarrick J. Wong xfs_rt_mark_sick(sc->mp, sc->sick_mask); 1694860a05dSDarrick J. Wong else 1704860a05dSDarrick J. Wong xfs_rt_mark_healthy(sc->mp, sc->sick_mask); 1714860a05dSDarrick J. Wong break; 1724860a05dSDarrick J. Wong default: 1734860a05dSDarrick J. Wong ASSERT(0); 1744860a05dSDarrick J. Wong break; 1754860a05dSDarrick J. Wong } 1764860a05dSDarrick J. Wong } 177*4fb7951fSDarrick J. Wong 178*4fb7951fSDarrick J. Wong /* Is the given per-AG btree healthy enough for scanning? */ 179*4fb7951fSDarrick J. Wong bool 180*4fb7951fSDarrick J. Wong xchk_ag_btree_healthy_enough( 181*4fb7951fSDarrick J. Wong struct xfs_scrub *sc, 182*4fb7951fSDarrick J. Wong struct xfs_perag *pag, 183*4fb7951fSDarrick J. Wong xfs_btnum_t btnum) 184*4fb7951fSDarrick J. Wong { 185*4fb7951fSDarrick J. Wong unsigned int mask = 0; 186*4fb7951fSDarrick J. Wong 187*4fb7951fSDarrick J. Wong /* 188*4fb7951fSDarrick J. Wong * We always want the cursor if it's the same type as whatever we're 189*4fb7951fSDarrick J. Wong * scrubbing, even if we already know the structure is corrupt. 190*4fb7951fSDarrick J. Wong * 191*4fb7951fSDarrick J. Wong * Otherwise, we're only interested in the btree for cross-referencing. 192*4fb7951fSDarrick J. Wong * If we know the btree is bad then don't bother, just set XFAIL. 193*4fb7951fSDarrick J. Wong */ 194*4fb7951fSDarrick J. Wong switch (btnum) { 195*4fb7951fSDarrick J. Wong case XFS_BTNUM_BNO: 196*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_BNOBT) 197*4fb7951fSDarrick J. Wong return true; 198*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_BNOBT; 199*4fb7951fSDarrick J. Wong break; 200*4fb7951fSDarrick J. Wong case XFS_BTNUM_CNT: 201*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_CNTBT) 202*4fb7951fSDarrick J. Wong return true; 203*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_CNTBT; 204*4fb7951fSDarrick J. Wong break; 205*4fb7951fSDarrick J. Wong case XFS_BTNUM_INO: 206*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_INOBT) 207*4fb7951fSDarrick J. Wong return true; 208*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_INOBT; 209*4fb7951fSDarrick J. Wong break; 210*4fb7951fSDarrick J. Wong case XFS_BTNUM_FINO: 211*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_FINOBT) 212*4fb7951fSDarrick J. Wong return true; 213*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_FINOBT; 214*4fb7951fSDarrick J. Wong break; 215*4fb7951fSDarrick J. Wong case XFS_BTNUM_RMAP: 216*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_RMAPBT) 217*4fb7951fSDarrick J. Wong return true; 218*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_RMAPBT; 219*4fb7951fSDarrick J. Wong break; 220*4fb7951fSDarrick J. Wong case XFS_BTNUM_REFC: 221*4fb7951fSDarrick J. Wong if (sc->sm->sm_type == XFS_SCRUB_TYPE_REFCNTBT) 222*4fb7951fSDarrick J. Wong return true; 223*4fb7951fSDarrick J. Wong mask = XFS_SICK_AG_REFCNTBT; 224*4fb7951fSDarrick J. Wong break; 225*4fb7951fSDarrick J. Wong default: 226*4fb7951fSDarrick J. Wong ASSERT(0); 227*4fb7951fSDarrick J. Wong return true; 228*4fb7951fSDarrick J. Wong } 229*4fb7951fSDarrick J. Wong 230*4fb7951fSDarrick J. Wong if (xfs_ag_has_sickness(pag, mask)) { 231*4fb7951fSDarrick J. Wong sc->sm->sm_flags |= XFS_SCRUB_OFLAG_XFAIL; 232*4fb7951fSDarrick J. Wong return false; 233*4fb7951fSDarrick J. Wong } 234*4fb7951fSDarrick J. Wong 235*4fb7951fSDarrick J. Wong return true; 236*4fb7951fSDarrick J. Wong } 237