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