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