Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Oracle. All Rights Reserved. |
| 4 | * Author: Darrick J. Wong <darrick.wong@oracle.com> |
| 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" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 12 | #include "xfs_log_format.h" |
| 13 | #include "xfs_trans.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | #include "xfs_inode.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 15 | #include "xfs_quota.h" |
| 16 | #include "xfs_qm.h" |
| 17 | #include "xfs_errortag.h" |
| 18 | #include "xfs_error.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 19 | #include "scrub/scrub.h" |
| 20 | #include "scrub/common.h" |
| 21 | #include "scrub/trace.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 22 | #include "scrub/repair.h" |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 23 | #include "scrub/health.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | |
| 25 | /* |
| 26 | * Online Scrub and Repair |
| 27 | * |
| 28 | * Traditionally, XFS (the kernel driver) did not know how to check or |
| 29 | * repair on-disk data structures. That task was left to the xfs_check |
| 30 | * and xfs_repair tools, both of which require taking the filesystem |
| 31 | * offline for a thorough but time consuming examination. Online |
| 32 | * scrub & repair, on the other hand, enables us to check the metadata |
| 33 | * for obvious errors while carefully stepping around the filesystem's |
| 34 | * ongoing operations, locking rules, etc. |
| 35 | * |
| 36 | * Given that most XFS metadata consist of records stored in a btree, |
| 37 | * most of the checking functions iterate the btree blocks themselves |
| 38 | * looking for irregularities. When a record block is encountered, each |
| 39 | * record can be checked for obviously bad values. Record values can |
| 40 | * also be cross-referenced against other btrees to look for potential |
| 41 | * misunderstandings between pieces of metadata. |
| 42 | * |
| 43 | * It is expected that the checkers responsible for per-AG metadata |
| 44 | * structures will lock the AG headers (AGI, AGF, AGFL), iterate the |
| 45 | * metadata structure, and perform any relevant cross-referencing before |
| 46 | * unlocking the AG and returning the results to userspace. These |
| 47 | * scrubbers must not keep an AG locked for too long to avoid tying up |
| 48 | * the block and inode allocators. |
| 49 | * |
| 50 | * Block maps and b-trees rooted in an inode present a special challenge |
| 51 | * because they can involve extents from any AG. The general scrubber |
| 52 | * structure of lock -> check -> xref -> unlock still holds, but AG |
| 53 | * locking order rules /must/ be obeyed to avoid deadlocks. The |
| 54 | * ordering rule, of course, is that we must lock in increasing AG |
| 55 | * order. Helper functions are provided to track which AG headers we've |
| 56 | * already locked. If we detect an imminent locking order violation, we |
| 57 | * can signal a potential deadlock, in which case the scrubber can jump |
| 58 | * out to the top level, lock all the AGs in order, and retry the scrub. |
| 59 | * |
| 60 | * For file data (directories, extended attributes, symlinks) scrub, we |
| 61 | * can simply lock the inode and walk the data. For btree data |
| 62 | * (directories and attributes) we follow the same btree-scrubbing |
| 63 | * strategy outlined previously to check the records. |
| 64 | * |
| 65 | * We use a bit of trickery with transactions to avoid buffer deadlocks |
| 66 | * if there is a cycle in the metadata. The basic problem is that |
| 67 | * travelling down a btree involves locking the current buffer at each |
| 68 | * tree level. If a pointer should somehow point back to a buffer that |
| 69 | * we've already examined, we will deadlock due to the second buffer |
| 70 | * locking attempt. Note however that grabbing a buffer in transaction |
| 71 | * context links the locked buffer to the transaction. If we try to |
| 72 | * re-grab the buffer in the context of the same transaction, we avoid |
| 73 | * the second lock attempt and continue. Between the verifier and the |
| 74 | * scrubber, something will notice that something is amiss and report |
| 75 | * the corruption. Therefore, each scrubber will allocate an empty |
| 76 | * transaction, attach buffers to it, and cancel the transaction at the |
| 77 | * end of the scrub run. Cancelling a non-dirty transaction simply |
| 78 | * unlocks the buffers. |
| 79 | * |
| 80 | * There are four pieces of data that scrub can communicate to |
| 81 | * userspace. The first is the error code (errno), which can be used to |
| 82 | * communicate operational errors in performing the scrub. There are |
| 83 | * also three flags that can be set in the scrub context. If the data |
| 84 | * structure itself is corrupt, the CORRUPT flag will be set. If |
| 85 | * the metadata is correct but otherwise suboptimal, the PREEN flag |
| 86 | * will be set. |
| 87 | * |
| 88 | * We perform secondary validation of filesystem metadata by |
| 89 | * cross-referencing every record with all other available metadata. |
| 90 | * For example, for block mapping extents, we verify that there are no |
| 91 | * records in the free space and inode btrees corresponding to that |
| 92 | * space extent and that there is a corresponding entry in the reverse |
| 93 | * mapping btree. Inconsistent metadata is noted by setting the |
| 94 | * XCORRUPT flag; btree query function errors are noted by setting the |
| 95 | * XFAIL flag and deleting the cursor to prevent further attempts to |
| 96 | * cross-reference with a defective btree. |
| 97 | * |
| 98 | * If a piece of metadata proves corrupt or suboptimal, the userspace |
| 99 | * program can ask the kernel to apply some tender loving care (TLC) to |
| 100 | * the metadata object by setting the REPAIR flag and re-calling the |
| 101 | * scrub ioctl. "Corruption" is defined by metadata violating the |
| 102 | * on-disk specification; operations cannot continue if the violation is |
| 103 | * left untreated. It is possible for XFS to continue if an object is |
| 104 | * "suboptimal", however performance may be degraded. Repairs are |
| 105 | * usually performed by rebuilding the metadata entirely out of |
| 106 | * redundant metadata. Optimizing, on the other hand, can sometimes be |
| 107 | * done without rebuilding entire structures. |
| 108 | * |
| 109 | * Generally speaking, the repair code has the following code structure: |
| 110 | * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock. |
| 111 | * The first check helps us figure out if we need to rebuild or simply |
| 112 | * optimize the structure so that the rebuild knows what to do. The |
| 113 | * second check evaluates the completeness of the repair; that is what |
| 114 | * is reported to userspace. |
| 115 | * |
| 116 | * A quick note on symbol prefixes: |
| 117 | * - "xfs_" are general XFS symbols. |
| 118 | * - "xchk_" are symbols related to metadata checking. |
| 119 | * - "xrep_" are symbols related to metadata repair. |
| 120 | * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS. |
| 121 | */ |
| 122 | |
| 123 | /* |
| 124 | * Scrub probe -- userspace uses this to probe if we're willing to scrub |
| 125 | * or repair a given mountpoint. This will be used by xfs_scrub to |
| 126 | * probe the kernel's abilities to scrub (and repair) the metadata. We |
| 127 | * do this by validating the ioctl inputs from userspace, preparing the |
| 128 | * filesystem for a scrub (or a repair) operation, and immediately |
| 129 | * returning to userspace. Userspace can use the returned errno and |
| 130 | * structure state to decide (in broad terms) if scrub/repair are |
| 131 | * supported by the running kernel. |
| 132 | */ |
| 133 | static int |
| 134 | xchk_probe( |
| 135 | struct xfs_scrub *sc) |
| 136 | { |
| 137 | int error = 0; |
| 138 | |
| 139 | if (xchk_should_terminate(sc, &error)) |
| 140 | return error; |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | /* Scrub setup and teardown */ |
| 146 | |
| 147 | /* Free all the resources and finish the transactions. */ |
| 148 | STATIC int |
| 149 | xchk_teardown( |
| 150 | struct xfs_scrub *sc, |
| 151 | struct xfs_inode *ip_in, |
| 152 | int error) |
| 153 | { |
| 154 | xchk_ag_free(sc, &sc->sa); |
| 155 | if (sc->tp) { |
| 156 | if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) |
| 157 | error = xfs_trans_commit(sc->tp); |
| 158 | else |
| 159 | xfs_trans_cancel(sc->tp); |
| 160 | sc->tp = NULL; |
| 161 | } |
| 162 | if (sc->ip) { |
| 163 | if (sc->ilock_flags) |
| 164 | xfs_iunlock(sc->ip, sc->ilock_flags); |
| 165 | if (sc->ip != ip_in && |
| 166 | !xfs_internal_inum(sc->mp, sc->ip->i_ino)) |
| 167 | xfs_irele(sc->ip); |
| 168 | sc->ip = NULL; |
| 169 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 170 | sb_end_write(sc->mp->m_super); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 171 | if (sc->flags & XCHK_REAPING_DISABLED) |
| 172 | xchk_start_reaping(sc); |
| 173 | if (sc->flags & XCHK_HAS_QUOTAOFFLOCK) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 174 | mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 175 | sc->flags &= ~XCHK_HAS_QUOTAOFFLOCK; |
| 176 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 177 | if (sc->buf) { |
| 178 | kmem_free(sc->buf); |
| 179 | sc->buf = NULL; |
| 180 | } |
| 181 | return error; |
| 182 | } |
| 183 | |
| 184 | /* Scrubbing dispatch. */ |
| 185 | |
| 186 | static const struct xchk_meta_ops meta_scrub_ops[] = { |
| 187 | [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */ |
| 188 | .type = ST_NONE, |
| 189 | .setup = xchk_setup_fs, |
| 190 | .scrub = xchk_probe, |
| 191 | .repair = xrep_probe, |
| 192 | }, |
| 193 | [XFS_SCRUB_TYPE_SB] = { /* superblock */ |
| 194 | .type = ST_PERAG, |
| 195 | .setup = xchk_setup_fs, |
| 196 | .scrub = xchk_superblock, |
| 197 | .repair = xrep_superblock, |
| 198 | }, |
| 199 | [XFS_SCRUB_TYPE_AGF] = { /* agf */ |
| 200 | .type = ST_PERAG, |
| 201 | .setup = xchk_setup_fs, |
| 202 | .scrub = xchk_agf, |
| 203 | .repair = xrep_agf, |
| 204 | }, |
| 205 | [XFS_SCRUB_TYPE_AGFL]= { /* agfl */ |
| 206 | .type = ST_PERAG, |
| 207 | .setup = xchk_setup_fs, |
| 208 | .scrub = xchk_agfl, |
| 209 | .repair = xrep_agfl, |
| 210 | }, |
| 211 | [XFS_SCRUB_TYPE_AGI] = { /* agi */ |
| 212 | .type = ST_PERAG, |
| 213 | .setup = xchk_setup_fs, |
| 214 | .scrub = xchk_agi, |
| 215 | .repair = xrep_agi, |
| 216 | }, |
| 217 | [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */ |
| 218 | .type = ST_PERAG, |
| 219 | .setup = xchk_setup_ag_allocbt, |
| 220 | .scrub = xchk_bnobt, |
| 221 | .repair = xrep_notsupported, |
| 222 | }, |
| 223 | [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */ |
| 224 | .type = ST_PERAG, |
| 225 | .setup = xchk_setup_ag_allocbt, |
| 226 | .scrub = xchk_cntbt, |
| 227 | .repair = xrep_notsupported, |
| 228 | }, |
| 229 | [XFS_SCRUB_TYPE_INOBT] = { /* inobt */ |
| 230 | .type = ST_PERAG, |
| 231 | .setup = xchk_setup_ag_iallocbt, |
| 232 | .scrub = xchk_inobt, |
| 233 | .repair = xrep_notsupported, |
| 234 | }, |
| 235 | [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */ |
| 236 | .type = ST_PERAG, |
| 237 | .setup = xchk_setup_ag_iallocbt, |
| 238 | .scrub = xchk_finobt, |
| 239 | .has = xfs_sb_version_hasfinobt, |
| 240 | .repair = xrep_notsupported, |
| 241 | }, |
| 242 | [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */ |
| 243 | .type = ST_PERAG, |
| 244 | .setup = xchk_setup_ag_rmapbt, |
| 245 | .scrub = xchk_rmapbt, |
| 246 | .has = xfs_sb_version_hasrmapbt, |
| 247 | .repair = xrep_notsupported, |
| 248 | }, |
| 249 | [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */ |
| 250 | .type = ST_PERAG, |
| 251 | .setup = xchk_setup_ag_refcountbt, |
| 252 | .scrub = xchk_refcountbt, |
| 253 | .has = xfs_sb_version_hasreflink, |
| 254 | .repair = xrep_notsupported, |
| 255 | }, |
| 256 | [XFS_SCRUB_TYPE_INODE] = { /* inode record */ |
| 257 | .type = ST_INODE, |
| 258 | .setup = xchk_setup_inode, |
| 259 | .scrub = xchk_inode, |
| 260 | .repair = xrep_notsupported, |
| 261 | }, |
| 262 | [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */ |
| 263 | .type = ST_INODE, |
| 264 | .setup = xchk_setup_inode_bmap, |
| 265 | .scrub = xchk_bmap_data, |
| 266 | .repair = xrep_notsupported, |
| 267 | }, |
| 268 | [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */ |
| 269 | .type = ST_INODE, |
| 270 | .setup = xchk_setup_inode_bmap, |
| 271 | .scrub = xchk_bmap_attr, |
| 272 | .repair = xrep_notsupported, |
| 273 | }, |
| 274 | [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */ |
| 275 | .type = ST_INODE, |
| 276 | .setup = xchk_setup_inode_bmap, |
| 277 | .scrub = xchk_bmap_cow, |
| 278 | .repair = xrep_notsupported, |
| 279 | }, |
| 280 | [XFS_SCRUB_TYPE_DIR] = { /* directory */ |
| 281 | .type = ST_INODE, |
| 282 | .setup = xchk_setup_directory, |
| 283 | .scrub = xchk_directory, |
| 284 | .repair = xrep_notsupported, |
| 285 | }, |
| 286 | [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */ |
| 287 | .type = ST_INODE, |
| 288 | .setup = xchk_setup_xattr, |
| 289 | .scrub = xchk_xattr, |
| 290 | .repair = xrep_notsupported, |
| 291 | }, |
| 292 | [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */ |
| 293 | .type = ST_INODE, |
| 294 | .setup = xchk_setup_symlink, |
| 295 | .scrub = xchk_symlink, |
| 296 | .repair = xrep_notsupported, |
| 297 | }, |
| 298 | [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */ |
| 299 | .type = ST_INODE, |
| 300 | .setup = xchk_setup_parent, |
| 301 | .scrub = xchk_parent, |
| 302 | .repair = xrep_notsupported, |
| 303 | }, |
| 304 | [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */ |
| 305 | .type = ST_FS, |
| 306 | .setup = xchk_setup_rt, |
| 307 | .scrub = xchk_rtbitmap, |
| 308 | .has = xfs_sb_version_hasrealtime, |
| 309 | .repair = xrep_notsupported, |
| 310 | }, |
| 311 | [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */ |
| 312 | .type = ST_FS, |
| 313 | .setup = xchk_setup_rt, |
| 314 | .scrub = xchk_rtsummary, |
| 315 | .has = xfs_sb_version_hasrealtime, |
| 316 | .repair = xrep_notsupported, |
| 317 | }, |
| 318 | [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */ |
| 319 | .type = ST_FS, |
| 320 | .setup = xchk_setup_quota, |
| 321 | .scrub = xchk_quota, |
| 322 | .repair = xrep_notsupported, |
| 323 | }, |
| 324 | [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */ |
| 325 | .type = ST_FS, |
| 326 | .setup = xchk_setup_quota, |
| 327 | .scrub = xchk_quota, |
| 328 | .repair = xrep_notsupported, |
| 329 | }, |
| 330 | [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */ |
| 331 | .type = ST_FS, |
| 332 | .setup = xchk_setup_quota, |
| 333 | .scrub = xchk_quota, |
| 334 | .repair = xrep_notsupported, |
| 335 | }, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 336 | [XFS_SCRUB_TYPE_FSCOUNTERS] = { /* fs summary counters */ |
| 337 | .type = ST_FS, |
| 338 | .setup = xchk_setup_fscounters, |
| 339 | .scrub = xchk_fscounters, |
| 340 | .repair = xrep_notsupported, |
| 341 | }, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 342 | }; |
| 343 | |
| 344 | /* This isn't a stable feature, warn once per day. */ |
| 345 | static inline void |
| 346 | xchk_experimental_warning( |
| 347 | struct xfs_mount *mp) |
| 348 | { |
| 349 | static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT( |
| 350 | "xchk_warning", 86400 * HZ, 1); |
| 351 | ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE); |
| 352 | |
| 353 | if (__ratelimit(&scrub_warning)) |
| 354 | xfs_alert(mp, |
| 355 | "EXPERIMENTAL online scrub feature in use. Use at your own risk!"); |
| 356 | } |
| 357 | |
| 358 | static int |
| 359 | xchk_validate_inputs( |
| 360 | struct xfs_mount *mp, |
| 361 | struct xfs_scrub_metadata *sm) |
| 362 | { |
| 363 | int error; |
| 364 | const struct xchk_meta_ops *ops; |
| 365 | |
| 366 | error = -EINVAL; |
| 367 | /* Check our inputs. */ |
| 368 | sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; |
| 369 | if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN) |
| 370 | goto out; |
| 371 | /* sm_reserved[] must be zero */ |
| 372 | if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved))) |
| 373 | goto out; |
| 374 | |
| 375 | error = -ENOENT; |
| 376 | /* Do we know about this type of metadata? */ |
| 377 | if (sm->sm_type >= XFS_SCRUB_TYPE_NR) |
| 378 | goto out; |
| 379 | ops = &meta_scrub_ops[sm->sm_type]; |
| 380 | if (ops->setup == NULL || ops->scrub == NULL) |
| 381 | goto out; |
| 382 | /* Does this fs even support this type of metadata? */ |
| 383 | if (ops->has && !ops->has(&mp->m_sb)) |
| 384 | goto out; |
| 385 | |
| 386 | error = -EINVAL; |
| 387 | /* restricting fields must be appropriate for type */ |
| 388 | switch (ops->type) { |
| 389 | case ST_NONE: |
| 390 | case ST_FS: |
| 391 | if (sm->sm_ino || sm->sm_gen || sm->sm_agno) |
| 392 | goto out; |
| 393 | break; |
| 394 | case ST_PERAG: |
| 395 | if (sm->sm_ino || sm->sm_gen || |
| 396 | sm->sm_agno >= mp->m_sb.sb_agcount) |
| 397 | goto out; |
| 398 | break; |
| 399 | case ST_INODE: |
| 400 | if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino)) |
| 401 | goto out; |
| 402 | break; |
| 403 | default: |
| 404 | goto out; |
| 405 | } |
| 406 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 407 | /* |
| 408 | * We only want to repair read-write v5+ filesystems. Defer the check |
| 409 | * for ops->repair until after our scrub confirms that we need to |
| 410 | * perform repairs so that we avoid failing due to not supporting |
| 411 | * repairing an object that doesn't need repairs. |
| 412 | */ |
| 413 | if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { |
| 414 | error = -EOPNOTSUPP; |
| 415 | if (!xfs_sb_version_hascrc(&mp->m_sb)) |
| 416 | goto out; |
| 417 | |
| 418 | error = -EROFS; |
| 419 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
| 420 | goto out; |
| 421 | } |
| 422 | |
| 423 | error = 0; |
| 424 | out: |
| 425 | return error; |
| 426 | } |
| 427 | |
| 428 | #ifdef CONFIG_XFS_ONLINE_REPAIR |
| 429 | static inline void xchk_postmortem(struct xfs_scrub *sc) |
| 430 | { |
| 431 | /* |
| 432 | * Userspace asked us to repair something, we repaired it, rescanned |
| 433 | * it, and the rescan says it's still broken. Scream about this in |
| 434 | * the system logs. |
| 435 | */ |
| 436 | if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && |
| 437 | (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | |
| 438 | XFS_SCRUB_OFLAG_XCORRUPT))) |
| 439 | xrep_failure(sc->mp); |
| 440 | } |
| 441 | #else |
| 442 | static inline void xchk_postmortem(struct xfs_scrub *sc) |
| 443 | { |
| 444 | /* |
| 445 | * Userspace asked us to scrub something, it's broken, and we have no |
| 446 | * way of fixing it. Scream in the logs. |
| 447 | */ |
| 448 | if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | |
| 449 | XFS_SCRUB_OFLAG_XCORRUPT)) |
| 450 | xfs_alert_ratelimited(sc->mp, |
| 451 | "Corruption detected during scrub."); |
| 452 | } |
| 453 | #endif /* CONFIG_XFS_ONLINE_REPAIR */ |
| 454 | |
| 455 | /* Dispatch metadata scrubbing. */ |
| 456 | int |
| 457 | xfs_scrub_metadata( |
| 458 | struct xfs_inode *ip, |
| 459 | struct xfs_scrub_metadata *sm) |
| 460 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 461 | struct xfs_scrub sc = { |
| 462 | .mp = ip->i_mount, |
| 463 | .sm = sm, |
| 464 | .sa = { |
| 465 | .agno = NULLAGNUMBER, |
| 466 | }, |
| 467 | }; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 468 | struct xfs_mount *mp = ip->i_mount; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 469 | int error = 0; |
| 470 | |
| 471 | BUILD_BUG_ON(sizeof(meta_scrub_ops) != |
| 472 | (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR)); |
| 473 | |
| 474 | trace_xchk_start(ip, sm, error); |
| 475 | |
| 476 | /* Forbidden if we are shut down or mounted norecovery. */ |
| 477 | error = -ESHUTDOWN; |
| 478 | if (XFS_FORCED_SHUTDOWN(mp)) |
| 479 | goto out; |
| 480 | error = -ENOTRECOVERABLE; |
| 481 | if (mp->m_flags & XFS_MOUNT_NORECOVERY) |
| 482 | goto out; |
| 483 | |
| 484 | error = xchk_validate_inputs(mp, sm); |
| 485 | if (error) |
| 486 | goto out; |
| 487 | |
| 488 | xchk_experimental_warning(mp); |
| 489 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 490 | sc.ops = &meta_scrub_ops[sm->sm_type]; |
| 491 | sc.sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 492 | retry_op: |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 493 | /* |
| 494 | * If freeze runs concurrently with a scrub, the freeze can be delayed |
| 495 | * indefinitely as we walk the filesystem and iterate over metadata |
| 496 | * buffers. Freeze quiesces the log (which waits for the buffer LRU to |
| 497 | * be emptied) and that won't happen while checking is running. |
| 498 | */ |
| 499 | sb_start_write(mp->m_super); |
| 500 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 501 | /* Set up for the operation. */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 502 | error = sc.ops->setup(&sc, ip); |
| 503 | if (error) |
| 504 | goto out_teardown; |
| 505 | |
| 506 | /* Scrub for errors. */ |
| 507 | error = sc.ops->scrub(&sc); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 508 | if (!(sc.flags & XCHK_TRY_HARDER) && error == -EDEADLOCK) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 509 | /* |
| 510 | * Scrubbers return -EDEADLOCK to mean 'try harder'. |
| 511 | * Tear down everything we hold, then set up again with |
| 512 | * preparation for worst-case scenarios. |
| 513 | */ |
| 514 | error = xchk_teardown(&sc, ip, 0); |
| 515 | if (error) |
| 516 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 517 | sc.flags |= XCHK_TRY_HARDER; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 518 | goto retry_op; |
| 519 | } else if (error) |
| 520 | goto out_teardown; |
| 521 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 522 | xchk_update_health(&sc); |
| 523 | |
| 524 | if ((sc.sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && |
| 525 | !(sc.flags & XREP_ALREADY_FIXED)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 526 | bool needs_fix; |
| 527 | |
| 528 | /* Let debug users force us into the repair routines. */ |
| 529 | if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR)) |
| 530 | sc.sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; |
| 531 | |
| 532 | needs_fix = (sc.sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | |
| 533 | XFS_SCRUB_OFLAG_XCORRUPT | |
| 534 | XFS_SCRUB_OFLAG_PREEN)); |
| 535 | /* |
| 536 | * If userspace asked for a repair but it wasn't necessary, |
| 537 | * report that back to userspace. |
| 538 | */ |
| 539 | if (!needs_fix) { |
| 540 | sc.sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED; |
| 541 | goto out_nofix; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * If it's broken, userspace wants us to fix it, and we haven't |
| 546 | * already tried to fix it, then attempt a repair. |
| 547 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 548 | error = xrep_attempt(ip, &sc); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 549 | if (error == -EAGAIN) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 550 | /* |
| 551 | * Either the repair function succeeded or it couldn't |
| 552 | * get all the resources it needs; either way, we go |
| 553 | * back to the beginning and call the scrub function. |
| 554 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 555 | error = xchk_teardown(&sc, ip, 0); |
| 556 | if (error) { |
| 557 | xrep_failure(mp); |
| 558 | goto out; |
| 559 | } |
| 560 | goto retry_op; |
| 561 | } |
| 562 | } |
| 563 | |
| 564 | out_nofix: |
| 565 | xchk_postmortem(&sc); |
| 566 | out_teardown: |
| 567 | error = xchk_teardown(&sc, ip, error); |
| 568 | out: |
| 569 | trace_xchk_done(ip, sm, error); |
| 570 | if (error == -EFSCORRUPTED || error == -EFSBADCRC) { |
| 571 | sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; |
| 572 | error = 0; |
| 573 | } |
| 574 | return error; |
| 575 | } |