18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+ 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2017 Oracle. All Rights Reserved. 48c2ecf20Sopenharmony_ci * Author: Darrick J. Wong <darrick.wong@oracle.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci#include "xfs.h" 78c2ecf20Sopenharmony_ci#include "xfs_fs.h" 88c2ecf20Sopenharmony_ci#include "xfs_shared.h" 98c2ecf20Sopenharmony_ci#include "xfs_format.h" 108c2ecf20Sopenharmony_ci#include "xfs_trans_resv.h" 118c2ecf20Sopenharmony_ci#include "xfs_mount.h" 128c2ecf20Sopenharmony_ci#include "xfs_log_format.h" 138c2ecf20Sopenharmony_ci#include "xfs_trans.h" 148c2ecf20Sopenharmony_ci#include "xfs_inode.h" 158c2ecf20Sopenharmony_ci#include "xfs_quota.h" 168c2ecf20Sopenharmony_ci#include "xfs_qm.h" 178c2ecf20Sopenharmony_ci#include "xfs_errortag.h" 188c2ecf20Sopenharmony_ci#include "xfs_error.h" 198c2ecf20Sopenharmony_ci#include "xfs_scrub.h" 208c2ecf20Sopenharmony_ci#include "scrub/scrub.h" 218c2ecf20Sopenharmony_ci#include "scrub/common.h" 228c2ecf20Sopenharmony_ci#include "scrub/trace.h" 238c2ecf20Sopenharmony_ci#include "scrub/repair.h" 248c2ecf20Sopenharmony_ci#include "scrub/health.h" 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci/* 278c2ecf20Sopenharmony_ci * Online Scrub and Repair 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * Traditionally, XFS (the kernel driver) did not know how to check or 308c2ecf20Sopenharmony_ci * repair on-disk data structures. That task was left to the xfs_check 318c2ecf20Sopenharmony_ci * and xfs_repair tools, both of which require taking the filesystem 328c2ecf20Sopenharmony_ci * offline for a thorough but time consuming examination. Online 338c2ecf20Sopenharmony_ci * scrub & repair, on the other hand, enables us to check the metadata 348c2ecf20Sopenharmony_ci * for obvious errors while carefully stepping around the filesystem's 358c2ecf20Sopenharmony_ci * ongoing operations, locking rules, etc. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * Given that most XFS metadata consist of records stored in a btree, 388c2ecf20Sopenharmony_ci * most of the checking functions iterate the btree blocks themselves 398c2ecf20Sopenharmony_ci * looking for irregularities. When a record block is encountered, each 408c2ecf20Sopenharmony_ci * record can be checked for obviously bad values. Record values can 418c2ecf20Sopenharmony_ci * also be cross-referenced against other btrees to look for potential 428c2ecf20Sopenharmony_ci * misunderstandings between pieces of metadata. 438c2ecf20Sopenharmony_ci * 448c2ecf20Sopenharmony_ci * It is expected that the checkers responsible for per-AG metadata 458c2ecf20Sopenharmony_ci * structures will lock the AG headers (AGI, AGF, AGFL), iterate the 468c2ecf20Sopenharmony_ci * metadata structure, and perform any relevant cross-referencing before 478c2ecf20Sopenharmony_ci * unlocking the AG and returning the results to userspace. These 488c2ecf20Sopenharmony_ci * scrubbers must not keep an AG locked for too long to avoid tying up 498c2ecf20Sopenharmony_ci * the block and inode allocators. 508c2ecf20Sopenharmony_ci * 518c2ecf20Sopenharmony_ci * Block maps and b-trees rooted in an inode present a special challenge 528c2ecf20Sopenharmony_ci * because they can involve extents from any AG. The general scrubber 538c2ecf20Sopenharmony_ci * structure of lock -> check -> xref -> unlock still holds, but AG 548c2ecf20Sopenharmony_ci * locking order rules /must/ be obeyed to avoid deadlocks. The 558c2ecf20Sopenharmony_ci * ordering rule, of course, is that we must lock in increasing AG 568c2ecf20Sopenharmony_ci * order. Helper functions are provided to track which AG headers we've 578c2ecf20Sopenharmony_ci * already locked. If we detect an imminent locking order violation, we 588c2ecf20Sopenharmony_ci * can signal a potential deadlock, in which case the scrubber can jump 598c2ecf20Sopenharmony_ci * out to the top level, lock all the AGs in order, and retry the scrub. 608c2ecf20Sopenharmony_ci * 618c2ecf20Sopenharmony_ci * For file data (directories, extended attributes, symlinks) scrub, we 628c2ecf20Sopenharmony_ci * can simply lock the inode and walk the data. For btree data 638c2ecf20Sopenharmony_ci * (directories and attributes) we follow the same btree-scrubbing 648c2ecf20Sopenharmony_ci * strategy outlined previously to check the records. 658c2ecf20Sopenharmony_ci * 668c2ecf20Sopenharmony_ci * We use a bit of trickery with transactions to avoid buffer deadlocks 678c2ecf20Sopenharmony_ci * if there is a cycle in the metadata. The basic problem is that 688c2ecf20Sopenharmony_ci * travelling down a btree involves locking the current buffer at each 698c2ecf20Sopenharmony_ci * tree level. If a pointer should somehow point back to a buffer that 708c2ecf20Sopenharmony_ci * we've already examined, we will deadlock due to the second buffer 718c2ecf20Sopenharmony_ci * locking attempt. Note however that grabbing a buffer in transaction 728c2ecf20Sopenharmony_ci * context links the locked buffer to the transaction. If we try to 738c2ecf20Sopenharmony_ci * re-grab the buffer in the context of the same transaction, we avoid 748c2ecf20Sopenharmony_ci * the second lock attempt and continue. Between the verifier and the 758c2ecf20Sopenharmony_ci * scrubber, something will notice that something is amiss and report 768c2ecf20Sopenharmony_ci * the corruption. Therefore, each scrubber will allocate an empty 778c2ecf20Sopenharmony_ci * transaction, attach buffers to it, and cancel the transaction at the 788c2ecf20Sopenharmony_ci * end of the scrub run. Cancelling a non-dirty transaction simply 798c2ecf20Sopenharmony_ci * unlocks the buffers. 808c2ecf20Sopenharmony_ci * 818c2ecf20Sopenharmony_ci * There are four pieces of data that scrub can communicate to 828c2ecf20Sopenharmony_ci * userspace. The first is the error code (errno), which can be used to 838c2ecf20Sopenharmony_ci * communicate operational errors in performing the scrub. There are 848c2ecf20Sopenharmony_ci * also three flags that can be set in the scrub context. If the data 858c2ecf20Sopenharmony_ci * structure itself is corrupt, the CORRUPT flag will be set. If 868c2ecf20Sopenharmony_ci * the metadata is correct but otherwise suboptimal, the PREEN flag 878c2ecf20Sopenharmony_ci * will be set. 888c2ecf20Sopenharmony_ci * 898c2ecf20Sopenharmony_ci * We perform secondary validation of filesystem metadata by 908c2ecf20Sopenharmony_ci * cross-referencing every record with all other available metadata. 918c2ecf20Sopenharmony_ci * For example, for block mapping extents, we verify that there are no 928c2ecf20Sopenharmony_ci * records in the free space and inode btrees corresponding to that 938c2ecf20Sopenharmony_ci * space extent and that there is a corresponding entry in the reverse 948c2ecf20Sopenharmony_ci * mapping btree. Inconsistent metadata is noted by setting the 958c2ecf20Sopenharmony_ci * XCORRUPT flag; btree query function errors are noted by setting the 968c2ecf20Sopenharmony_ci * XFAIL flag and deleting the cursor to prevent further attempts to 978c2ecf20Sopenharmony_ci * cross-reference with a defective btree. 988c2ecf20Sopenharmony_ci * 998c2ecf20Sopenharmony_ci * If a piece of metadata proves corrupt or suboptimal, the userspace 1008c2ecf20Sopenharmony_ci * program can ask the kernel to apply some tender loving care (TLC) to 1018c2ecf20Sopenharmony_ci * the metadata object by setting the REPAIR flag and re-calling the 1028c2ecf20Sopenharmony_ci * scrub ioctl. "Corruption" is defined by metadata violating the 1038c2ecf20Sopenharmony_ci * on-disk specification; operations cannot continue if the violation is 1048c2ecf20Sopenharmony_ci * left untreated. It is possible for XFS to continue if an object is 1058c2ecf20Sopenharmony_ci * "suboptimal", however performance may be degraded. Repairs are 1068c2ecf20Sopenharmony_ci * usually performed by rebuilding the metadata entirely out of 1078c2ecf20Sopenharmony_ci * redundant metadata. Optimizing, on the other hand, can sometimes be 1088c2ecf20Sopenharmony_ci * done without rebuilding entire structures. 1098c2ecf20Sopenharmony_ci * 1108c2ecf20Sopenharmony_ci * Generally speaking, the repair code has the following code structure: 1118c2ecf20Sopenharmony_ci * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock. 1128c2ecf20Sopenharmony_ci * The first check helps us figure out if we need to rebuild or simply 1138c2ecf20Sopenharmony_ci * optimize the structure so that the rebuild knows what to do. The 1148c2ecf20Sopenharmony_ci * second check evaluates the completeness of the repair; that is what 1158c2ecf20Sopenharmony_ci * is reported to userspace. 1168c2ecf20Sopenharmony_ci * 1178c2ecf20Sopenharmony_ci * A quick note on symbol prefixes: 1188c2ecf20Sopenharmony_ci * - "xfs_" are general XFS symbols. 1198c2ecf20Sopenharmony_ci * - "xchk_" are symbols related to metadata checking. 1208c2ecf20Sopenharmony_ci * - "xrep_" are symbols related to metadata repair. 1218c2ecf20Sopenharmony_ci * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS. 1228c2ecf20Sopenharmony_ci */ 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci/* 1258c2ecf20Sopenharmony_ci * Scrub probe -- userspace uses this to probe if we're willing to scrub 1268c2ecf20Sopenharmony_ci * or repair a given mountpoint. This will be used by xfs_scrub to 1278c2ecf20Sopenharmony_ci * probe the kernel's abilities to scrub (and repair) the metadata. We 1288c2ecf20Sopenharmony_ci * do this by validating the ioctl inputs from userspace, preparing the 1298c2ecf20Sopenharmony_ci * filesystem for a scrub (or a repair) operation, and immediately 1308c2ecf20Sopenharmony_ci * returning to userspace. Userspace can use the returned errno and 1318c2ecf20Sopenharmony_ci * structure state to decide (in broad terms) if scrub/repair are 1328c2ecf20Sopenharmony_ci * supported by the running kernel. 1338c2ecf20Sopenharmony_ci */ 1348c2ecf20Sopenharmony_cistatic int 1358c2ecf20Sopenharmony_cixchk_probe( 1368c2ecf20Sopenharmony_ci struct xfs_scrub *sc) 1378c2ecf20Sopenharmony_ci{ 1388c2ecf20Sopenharmony_ci int error = 0; 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci if (xchk_should_terminate(sc, &error)) 1418c2ecf20Sopenharmony_ci return error; 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci return 0; 1448c2ecf20Sopenharmony_ci} 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci/* Scrub setup and teardown */ 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci/* Free all the resources and finish the transactions. */ 1498c2ecf20Sopenharmony_ciSTATIC int 1508c2ecf20Sopenharmony_cixchk_teardown( 1518c2ecf20Sopenharmony_ci struct xfs_scrub *sc, 1528c2ecf20Sopenharmony_ci struct xfs_inode *ip_in, 1538c2ecf20Sopenharmony_ci int error) 1548c2ecf20Sopenharmony_ci{ 1558c2ecf20Sopenharmony_ci xchk_ag_free(sc, &sc->sa); 1568c2ecf20Sopenharmony_ci if (sc->tp) { 1578c2ecf20Sopenharmony_ci if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) 1588c2ecf20Sopenharmony_ci error = xfs_trans_commit(sc->tp); 1598c2ecf20Sopenharmony_ci else 1608c2ecf20Sopenharmony_ci xfs_trans_cancel(sc->tp); 1618c2ecf20Sopenharmony_ci sc->tp = NULL; 1628c2ecf20Sopenharmony_ci } 1638c2ecf20Sopenharmony_ci if (sc->ip) { 1648c2ecf20Sopenharmony_ci if (sc->ilock_flags) 1658c2ecf20Sopenharmony_ci xfs_iunlock(sc->ip, sc->ilock_flags); 1668c2ecf20Sopenharmony_ci if (sc->ip != ip_in && 1678c2ecf20Sopenharmony_ci !xfs_internal_inum(sc->mp, sc->ip->i_ino)) 1688c2ecf20Sopenharmony_ci xfs_irele(sc->ip); 1698c2ecf20Sopenharmony_ci sc->ip = NULL; 1708c2ecf20Sopenharmony_ci } 1718c2ecf20Sopenharmony_ci sb_end_write(sc->mp->m_super); 1728c2ecf20Sopenharmony_ci if (sc->flags & XCHK_REAPING_DISABLED) 1738c2ecf20Sopenharmony_ci xchk_start_reaping(sc); 1748c2ecf20Sopenharmony_ci if (sc->flags & XCHK_HAS_QUOTAOFFLOCK) { 1758c2ecf20Sopenharmony_ci mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock); 1768c2ecf20Sopenharmony_ci sc->flags &= ~XCHK_HAS_QUOTAOFFLOCK; 1778c2ecf20Sopenharmony_ci } 1788c2ecf20Sopenharmony_ci if (sc->buf) { 1798c2ecf20Sopenharmony_ci kmem_free(sc->buf); 1808c2ecf20Sopenharmony_ci sc->buf = NULL; 1818c2ecf20Sopenharmony_ci } 1828c2ecf20Sopenharmony_ci return error; 1838c2ecf20Sopenharmony_ci} 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci/* Scrubbing dispatch. */ 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic const struct xchk_meta_ops meta_scrub_ops[] = { 1888c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */ 1898c2ecf20Sopenharmony_ci .type = ST_NONE, 1908c2ecf20Sopenharmony_ci .setup = xchk_setup_fs, 1918c2ecf20Sopenharmony_ci .scrub = xchk_probe, 1928c2ecf20Sopenharmony_ci .repair = xrep_probe, 1938c2ecf20Sopenharmony_ci }, 1948c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_SB] = { /* superblock */ 1958c2ecf20Sopenharmony_ci .type = ST_PERAG, 1968c2ecf20Sopenharmony_ci .setup = xchk_setup_fs, 1978c2ecf20Sopenharmony_ci .scrub = xchk_superblock, 1988c2ecf20Sopenharmony_ci .repair = xrep_superblock, 1998c2ecf20Sopenharmony_ci }, 2008c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_AGF] = { /* agf */ 2018c2ecf20Sopenharmony_ci .type = ST_PERAG, 2028c2ecf20Sopenharmony_ci .setup = xchk_setup_fs, 2038c2ecf20Sopenharmony_ci .scrub = xchk_agf, 2048c2ecf20Sopenharmony_ci .repair = xrep_agf, 2058c2ecf20Sopenharmony_ci }, 2068c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_AGFL]= { /* agfl */ 2078c2ecf20Sopenharmony_ci .type = ST_PERAG, 2088c2ecf20Sopenharmony_ci .setup = xchk_setup_fs, 2098c2ecf20Sopenharmony_ci .scrub = xchk_agfl, 2108c2ecf20Sopenharmony_ci .repair = xrep_agfl, 2118c2ecf20Sopenharmony_ci }, 2128c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_AGI] = { /* agi */ 2138c2ecf20Sopenharmony_ci .type = ST_PERAG, 2148c2ecf20Sopenharmony_ci .setup = xchk_setup_fs, 2158c2ecf20Sopenharmony_ci .scrub = xchk_agi, 2168c2ecf20Sopenharmony_ci .repair = xrep_agi, 2178c2ecf20Sopenharmony_ci }, 2188c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */ 2198c2ecf20Sopenharmony_ci .type = ST_PERAG, 2208c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_allocbt, 2218c2ecf20Sopenharmony_ci .scrub = xchk_bnobt, 2228c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2238c2ecf20Sopenharmony_ci }, 2248c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */ 2258c2ecf20Sopenharmony_ci .type = ST_PERAG, 2268c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_allocbt, 2278c2ecf20Sopenharmony_ci .scrub = xchk_cntbt, 2288c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2298c2ecf20Sopenharmony_ci }, 2308c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_INOBT] = { /* inobt */ 2318c2ecf20Sopenharmony_ci .type = ST_PERAG, 2328c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_iallocbt, 2338c2ecf20Sopenharmony_ci .scrub = xchk_inobt, 2348c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2358c2ecf20Sopenharmony_ci }, 2368c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */ 2378c2ecf20Sopenharmony_ci .type = ST_PERAG, 2388c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_iallocbt, 2398c2ecf20Sopenharmony_ci .scrub = xchk_finobt, 2408c2ecf20Sopenharmony_ci .has = xfs_sb_version_hasfinobt, 2418c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2428c2ecf20Sopenharmony_ci }, 2438c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */ 2448c2ecf20Sopenharmony_ci .type = ST_PERAG, 2458c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_rmapbt, 2468c2ecf20Sopenharmony_ci .scrub = xchk_rmapbt, 2478c2ecf20Sopenharmony_ci .has = xfs_sb_version_hasrmapbt, 2488c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2498c2ecf20Sopenharmony_ci }, 2508c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */ 2518c2ecf20Sopenharmony_ci .type = ST_PERAG, 2528c2ecf20Sopenharmony_ci .setup = xchk_setup_ag_refcountbt, 2538c2ecf20Sopenharmony_ci .scrub = xchk_refcountbt, 2548c2ecf20Sopenharmony_ci .has = xfs_sb_version_hasreflink, 2558c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2568c2ecf20Sopenharmony_ci }, 2578c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_INODE] = { /* inode record */ 2588c2ecf20Sopenharmony_ci .type = ST_INODE, 2598c2ecf20Sopenharmony_ci .setup = xchk_setup_inode, 2608c2ecf20Sopenharmony_ci .scrub = xchk_inode, 2618c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2628c2ecf20Sopenharmony_ci }, 2638c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */ 2648c2ecf20Sopenharmony_ci .type = ST_INODE, 2658c2ecf20Sopenharmony_ci .setup = xchk_setup_inode_bmap, 2668c2ecf20Sopenharmony_ci .scrub = xchk_bmap_data, 2678c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2688c2ecf20Sopenharmony_ci }, 2698c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */ 2708c2ecf20Sopenharmony_ci .type = ST_INODE, 2718c2ecf20Sopenharmony_ci .setup = xchk_setup_inode_bmap, 2728c2ecf20Sopenharmony_ci .scrub = xchk_bmap_attr, 2738c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2748c2ecf20Sopenharmony_ci }, 2758c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */ 2768c2ecf20Sopenharmony_ci .type = ST_INODE, 2778c2ecf20Sopenharmony_ci .setup = xchk_setup_inode_bmap, 2788c2ecf20Sopenharmony_ci .scrub = xchk_bmap_cow, 2798c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2808c2ecf20Sopenharmony_ci }, 2818c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_DIR] = { /* directory */ 2828c2ecf20Sopenharmony_ci .type = ST_INODE, 2838c2ecf20Sopenharmony_ci .setup = xchk_setup_directory, 2848c2ecf20Sopenharmony_ci .scrub = xchk_directory, 2858c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2868c2ecf20Sopenharmony_ci }, 2878c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */ 2888c2ecf20Sopenharmony_ci .type = ST_INODE, 2898c2ecf20Sopenharmony_ci .setup = xchk_setup_xattr, 2908c2ecf20Sopenharmony_ci .scrub = xchk_xattr, 2918c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2928c2ecf20Sopenharmony_ci }, 2938c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */ 2948c2ecf20Sopenharmony_ci .type = ST_INODE, 2958c2ecf20Sopenharmony_ci .setup = xchk_setup_symlink, 2968c2ecf20Sopenharmony_ci .scrub = xchk_symlink, 2978c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 2988c2ecf20Sopenharmony_ci }, 2998c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */ 3008c2ecf20Sopenharmony_ci .type = ST_INODE, 3018c2ecf20Sopenharmony_ci .setup = xchk_setup_parent, 3028c2ecf20Sopenharmony_ci .scrub = xchk_parent, 3038c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3048c2ecf20Sopenharmony_ci }, 3058c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */ 3068c2ecf20Sopenharmony_ci .type = ST_FS, 3078c2ecf20Sopenharmony_ci .setup = xchk_setup_rt, 3088c2ecf20Sopenharmony_ci .scrub = xchk_rtbitmap, 3098c2ecf20Sopenharmony_ci .has = xfs_sb_version_hasrealtime, 3108c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3118c2ecf20Sopenharmony_ci }, 3128c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */ 3138c2ecf20Sopenharmony_ci .type = ST_FS, 3148c2ecf20Sopenharmony_ci .setup = xchk_setup_rt, 3158c2ecf20Sopenharmony_ci .scrub = xchk_rtsummary, 3168c2ecf20Sopenharmony_ci .has = xfs_sb_version_hasrealtime, 3178c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3188c2ecf20Sopenharmony_ci }, 3198c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */ 3208c2ecf20Sopenharmony_ci .type = ST_FS, 3218c2ecf20Sopenharmony_ci .setup = xchk_setup_quota, 3228c2ecf20Sopenharmony_ci .scrub = xchk_quota, 3238c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3248c2ecf20Sopenharmony_ci }, 3258c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */ 3268c2ecf20Sopenharmony_ci .type = ST_FS, 3278c2ecf20Sopenharmony_ci .setup = xchk_setup_quota, 3288c2ecf20Sopenharmony_ci .scrub = xchk_quota, 3298c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3308c2ecf20Sopenharmony_ci }, 3318c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */ 3328c2ecf20Sopenharmony_ci .type = ST_FS, 3338c2ecf20Sopenharmony_ci .setup = xchk_setup_quota, 3348c2ecf20Sopenharmony_ci .scrub = xchk_quota, 3358c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3368c2ecf20Sopenharmony_ci }, 3378c2ecf20Sopenharmony_ci [XFS_SCRUB_TYPE_FSCOUNTERS] = { /* fs summary counters */ 3388c2ecf20Sopenharmony_ci .type = ST_FS, 3398c2ecf20Sopenharmony_ci .setup = xchk_setup_fscounters, 3408c2ecf20Sopenharmony_ci .scrub = xchk_fscounters, 3418c2ecf20Sopenharmony_ci .repair = xrep_notsupported, 3428c2ecf20Sopenharmony_ci }, 3438c2ecf20Sopenharmony_ci}; 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci/* This isn't a stable feature, warn once per day. */ 3468c2ecf20Sopenharmony_cistatic inline void 3478c2ecf20Sopenharmony_cixchk_experimental_warning( 3488c2ecf20Sopenharmony_ci struct xfs_mount *mp) 3498c2ecf20Sopenharmony_ci{ 3508c2ecf20Sopenharmony_ci static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT( 3518c2ecf20Sopenharmony_ci "xchk_warning", 86400 * HZ, 1); 3528c2ecf20Sopenharmony_ci ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE); 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci if (__ratelimit(&scrub_warning)) 3558c2ecf20Sopenharmony_ci xfs_alert(mp, 3568c2ecf20Sopenharmony_ci"EXPERIMENTAL online scrub feature in use. Use at your own risk!"); 3578c2ecf20Sopenharmony_ci} 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_cistatic int 3608c2ecf20Sopenharmony_cixchk_validate_inputs( 3618c2ecf20Sopenharmony_ci struct xfs_mount *mp, 3628c2ecf20Sopenharmony_ci struct xfs_scrub_metadata *sm) 3638c2ecf20Sopenharmony_ci{ 3648c2ecf20Sopenharmony_ci int error; 3658c2ecf20Sopenharmony_ci const struct xchk_meta_ops *ops; 3668c2ecf20Sopenharmony_ci 3678c2ecf20Sopenharmony_ci error = -EINVAL; 3688c2ecf20Sopenharmony_ci /* Check our inputs. */ 3698c2ecf20Sopenharmony_ci sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; 3708c2ecf20Sopenharmony_ci if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN) 3718c2ecf20Sopenharmony_ci goto out; 3728c2ecf20Sopenharmony_ci /* sm_reserved[] must be zero */ 3738c2ecf20Sopenharmony_ci if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved))) 3748c2ecf20Sopenharmony_ci goto out; 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci error = -ENOENT; 3778c2ecf20Sopenharmony_ci /* Do we know about this type of metadata? */ 3788c2ecf20Sopenharmony_ci if (sm->sm_type >= XFS_SCRUB_TYPE_NR) 3798c2ecf20Sopenharmony_ci goto out; 3808c2ecf20Sopenharmony_ci ops = &meta_scrub_ops[sm->sm_type]; 3818c2ecf20Sopenharmony_ci if (ops->setup == NULL || ops->scrub == NULL) 3828c2ecf20Sopenharmony_ci goto out; 3838c2ecf20Sopenharmony_ci /* Does this fs even support this type of metadata? */ 3848c2ecf20Sopenharmony_ci if (ops->has && !ops->has(&mp->m_sb)) 3858c2ecf20Sopenharmony_ci goto out; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci error = -EINVAL; 3888c2ecf20Sopenharmony_ci /* restricting fields must be appropriate for type */ 3898c2ecf20Sopenharmony_ci switch (ops->type) { 3908c2ecf20Sopenharmony_ci case ST_NONE: 3918c2ecf20Sopenharmony_ci case ST_FS: 3928c2ecf20Sopenharmony_ci if (sm->sm_ino || sm->sm_gen || sm->sm_agno) 3938c2ecf20Sopenharmony_ci goto out; 3948c2ecf20Sopenharmony_ci break; 3958c2ecf20Sopenharmony_ci case ST_PERAG: 3968c2ecf20Sopenharmony_ci if (sm->sm_ino || sm->sm_gen || 3978c2ecf20Sopenharmony_ci sm->sm_agno >= mp->m_sb.sb_agcount) 3988c2ecf20Sopenharmony_ci goto out; 3998c2ecf20Sopenharmony_ci break; 4008c2ecf20Sopenharmony_ci case ST_INODE: 4018c2ecf20Sopenharmony_ci if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino)) 4028c2ecf20Sopenharmony_ci goto out; 4038c2ecf20Sopenharmony_ci break; 4048c2ecf20Sopenharmony_ci default: 4058c2ecf20Sopenharmony_ci goto out; 4068c2ecf20Sopenharmony_ci } 4078c2ecf20Sopenharmony_ci 4088c2ecf20Sopenharmony_ci /* 4098c2ecf20Sopenharmony_ci * We only want to repair read-write v5+ filesystems. Defer the check 4108c2ecf20Sopenharmony_ci * for ops->repair until after our scrub confirms that we need to 4118c2ecf20Sopenharmony_ci * perform repairs so that we avoid failing due to not supporting 4128c2ecf20Sopenharmony_ci * repairing an object that doesn't need repairs. 4138c2ecf20Sopenharmony_ci */ 4148c2ecf20Sopenharmony_ci if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { 4158c2ecf20Sopenharmony_ci error = -EOPNOTSUPP; 4168c2ecf20Sopenharmony_ci if (!xfs_sb_version_hascrc(&mp->m_sb)) 4178c2ecf20Sopenharmony_ci goto out; 4188c2ecf20Sopenharmony_ci 4198c2ecf20Sopenharmony_ci error = -EROFS; 4208c2ecf20Sopenharmony_ci if (mp->m_flags & XFS_MOUNT_RDONLY) 4218c2ecf20Sopenharmony_ci goto out; 4228c2ecf20Sopenharmony_ci } 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_ci error = 0; 4258c2ecf20Sopenharmony_ciout: 4268c2ecf20Sopenharmony_ci return error; 4278c2ecf20Sopenharmony_ci} 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci#ifdef CONFIG_XFS_ONLINE_REPAIR 4308c2ecf20Sopenharmony_cistatic inline void xchk_postmortem(struct xfs_scrub *sc) 4318c2ecf20Sopenharmony_ci{ 4328c2ecf20Sopenharmony_ci /* 4338c2ecf20Sopenharmony_ci * Userspace asked us to repair something, we repaired it, rescanned 4348c2ecf20Sopenharmony_ci * it, and the rescan says it's still broken. Scream about this in 4358c2ecf20Sopenharmony_ci * the system logs. 4368c2ecf20Sopenharmony_ci */ 4378c2ecf20Sopenharmony_ci if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && 4388c2ecf20Sopenharmony_ci (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 4398c2ecf20Sopenharmony_ci XFS_SCRUB_OFLAG_XCORRUPT))) 4408c2ecf20Sopenharmony_ci xrep_failure(sc->mp); 4418c2ecf20Sopenharmony_ci} 4428c2ecf20Sopenharmony_ci#else 4438c2ecf20Sopenharmony_cistatic inline void xchk_postmortem(struct xfs_scrub *sc) 4448c2ecf20Sopenharmony_ci{ 4458c2ecf20Sopenharmony_ci /* 4468c2ecf20Sopenharmony_ci * Userspace asked us to scrub something, it's broken, and we have no 4478c2ecf20Sopenharmony_ci * way of fixing it. Scream in the logs. 4488c2ecf20Sopenharmony_ci */ 4498c2ecf20Sopenharmony_ci if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 4508c2ecf20Sopenharmony_ci XFS_SCRUB_OFLAG_XCORRUPT)) 4518c2ecf20Sopenharmony_ci xfs_alert_ratelimited(sc->mp, 4528c2ecf20Sopenharmony_ci "Corruption detected during scrub."); 4538c2ecf20Sopenharmony_ci} 4548c2ecf20Sopenharmony_ci#endif /* CONFIG_XFS_ONLINE_REPAIR */ 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_ci/* Dispatch metadata scrubbing. */ 4578c2ecf20Sopenharmony_ciint 4588c2ecf20Sopenharmony_cixfs_scrub_metadata( 4598c2ecf20Sopenharmony_ci struct xfs_inode *ip, 4608c2ecf20Sopenharmony_ci struct xfs_scrub_metadata *sm) 4618c2ecf20Sopenharmony_ci{ 4628c2ecf20Sopenharmony_ci struct xfs_scrub sc = { 4638c2ecf20Sopenharmony_ci .mp = ip->i_mount, 4648c2ecf20Sopenharmony_ci .sm = sm, 4658c2ecf20Sopenharmony_ci .sa = { 4668c2ecf20Sopenharmony_ci .agno = NULLAGNUMBER, 4678c2ecf20Sopenharmony_ci }, 4688c2ecf20Sopenharmony_ci }; 4698c2ecf20Sopenharmony_ci struct xfs_mount *mp = ip->i_mount; 4708c2ecf20Sopenharmony_ci int error = 0; 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci BUILD_BUG_ON(sizeof(meta_scrub_ops) != 4738c2ecf20Sopenharmony_ci (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR)); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci trace_xchk_start(ip, sm, error); 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci /* Forbidden if we are shut down or mounted norecovery. */ 4788c2ecf20Sopenharmony_ci error = -ESHUTDOWN; 4798c2ecf20Sopenharmony_ci if (XFS_FORCED_SHUTDOWN(mp)) 4808c2ecf20Sopenharmony_ci goto out; 4818c2ecf20Sopenharmony_ci error = -ENOTRECOVERABLE; 4828c2ecf20Sopenharmony_ci if (mp->m_flags & XFS_MOUNT_NORECOVERY) 4838c2ecf20Sopenharmony_ci goto out; 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci error = xchk_validate_inputs(mp, sm); 4868c2ecf20Sopenharmony_ci if (error) 4878c2ecf20Sopenharmony_ci goto out; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci xchk_experimental_warning(mp); 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci sc.ops = &meta_scrub_ops[sm->sm_type]; 4928c2ecf20Sopenharmony_ci sc.sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type); 4938c2ecf20Sopenharmony_ciretry_op: 4948c2ecf20Sopenharmony_ci /* 4958c2ecf20Sopenharmony_ci * If freeze runs concurrently with a scrub, the freeze can be delayed 4968c2ecf20Sopenharmony_ci * indefinitely as we walk the filesystem and iterate over metadata 4978c2ecf20Sopenharmony_ci * buffers. Freeze quiesces the log (which waits for the buffer LRU to 4988c2ecf20Sopenharmony_ci * be emptied) and that won't happen while checking is running. 4998c2ecf20Sopenharmony_ci */ 5008c2ecf20Sopenharmony_ci sb_start_write(mp->m_super); 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci /* Set up for the operation. */ 5038c2ecf20Sopenharmony_ci error = sc.ops->setup(&sc, ip); 5048c2ecf20Sopenharmony_ci if (error) 5058c2ecf20Sopenharmony_ci goto out_teardown; 5068c2ecf20Sopenharmony_ci 5078c2ecf20Sopenharmony_ci /* Scrub for errors. */ 5088c2ecf20Sopenharmony_ci error = sc.ops->scrub(&sc); 5098c2ecf20Sopenharmony_ci if (!(sc.flags & XCHK_TRY_HARDER) && error == -EDEADLOCK) { 5108c2ecf20Sopenharmony_ci /* 5118c2ecf20Sopenharmony_ci * Scrubbers return -EDEADLOCK to mean 'try harder'. 5128c2ecf20Sopenharmony_ci * Tear down everything we hold, then set up again with 5138c2ecf20Sopenharmony_ci * preparation for worst-case scenarios. 5148c2ecf20Sopenharmony_ci */ 5158c2ecf20Sopenharmony_ci error = xchk_teardown(&sc, ip, 0); 5168c2ecf20Sopenharmony_ci if (error) 5178c2ecf20Sopenharmony_ci goto out; 5188c2ecf20Sopenharmony_ci sc.flags |= XCHK_TRY_HARDER; 5198c2ecf20Sopenharmony_ci goto retry_op; 5208c2ecf20Sopenharmony_ci } else if (error) 5218c2ecf20Sopenharmony_ci goto out_teardown; 5228c2ecf20Sopenharmony_ci 5238c2ecf20Sopenharmony_ci xchk_update_health(&sc); 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci if ((sc.sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && 5268c2ecf20Sopenharmony_ci !(sc.flags & XREP_ALREADY_FIXED)) { 5278c2ecf20Sopenharmony_ci bool needs_fix; 5288c2ecf20Sopenharmony_ci 5298c2ecf20Sopenharmony_ci /* Let debug users force us into the repair routines. */ 5308c2ecf20Sopenharmony_ci if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR)) 5318c2ecf20Sopenharmony_ci sc.sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_ci needs_fix = (sc.sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 5348c2ecf20Sopenharmony_ci XFS_SCRUB_OFLAG_XCORRUPT | 5358c2ecf20Sopenharmony_ci XFS_SCRUB_OFLAG_PREEN)); 5368c2ecf20Sopenharmony_ci /* 5378c2ecf20Sopenharmony_ci * If userspace asked for a repair but it wasn't necessary, 5388c2ecf20Sopenharmony_ci * report that back to userspace. 5398c2ecf20Sopenharmony_ci */ 5408c2ecf20Sopenharmony_ci if (!needs_fix) { 5418c2ecf20Sopenharmony_ci sc.sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED; 5428c2ecf20Sopenharmony_ci goto out_nofix; 5438c2ecf20Sopenharmony_ci } 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ci /* 5468c2ecf20Sopenharmony_ci * If it's broken, userspace wants us to fix it, and we haven't 5478c2ecf20Sopenharmony_ci * already tried to fix it, then attempt a repair. 5488c2ecf20Sopenharmony_ci */ 5498c2ecf20Sopenharmony_ci error = xrep_attempt(ip, &sc); 5508c2ecf20Sopenharmony_ci if (error == -EAGAIN) { 5518c2ecf20Sopenharmony_ci /* 5528c2ecf20Sopenharmony_ci * Either the repair function succeeded or it couldn't 5538c2ecf20Sopenharmony_ci * get all the resources it needs; either way, we go 5548c2ecf20Sopenharmony_ci * back to the beginning and call the scrub function. 5558c2ecf20Sopenharmony_ci */ 5568c2ecf20Sopenharmony_ci error = xchk_teardown(&sc, ip, 0); 5578c2ecf20Sopenharmony_ci if (error) { 5588c2ecf20Sopenharmony_ci xrep_failure(mp); 5598c2ecf20Sopenharmony_ci goto out; 5608c2ecf20Sopenharmony_ci } 5618c2ecf20Sopenharmony_ci goto retry_op; 5628c2ecf20Sopenharmony_ci } 5638c2ecf20Sopenharmony_ci } 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ciout_nofix: 5668c2ecf20Sopenharmony_ci xchk_postmortem(&sc); 5678c2ecf20Sopenharmony_ciout_teardown: 5688c2ecf20Sopenharmony_ci error = xchk_teardown(&sc, ip, error); 5698c2ecf20Sopenharmony_ciout: 5708c2ecf20Sopenharmony_ci trace_xchk_done(ip, sm, error); 5718c2ecf20Sopenharmony_ci if (error == -EFSCORRUPTED || error == -EFSBADCRC) { 5728c2ecf20Sopenharmony_ci sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 5738c2ecf20Sopenharmony_ci error = 0; 5748c2ecf20Sopenharmony_ci } 5758c2ecf20Sopenharmony_ci return error; 5768c2ecf20Sopenharmony_ci} 577