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 int error) 153 { 154 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file)); 155 156 xchk_ag_free(sc, &sc->sa); 157 if (sc->tp) { 158 if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)) 159 error = xfs_trans_commit(sc->tp); 160 else 161 xfs_trans_cancel(sc->tp); 162 sc->tp = NULL; 163 } 164 if (sc->ip) { 165 if (sc->ilock_flags) 166 xfs_iunlock(sc->ip, sc->ilock_flags); 167 if (sc->ip != ip_in && 168 !xfs_internal_inum(sc->mp, sc->ip->i_ino)) 169 xfs_irele(sc->ip); 170 sc->ip = NULL; 171 } 172 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) 173 mnt_drop_write_file(sc->file); 174 if (sc->flags & XCHK_REAPING_DISABLED) 175 xchk_start_reaping(sc); 176 if (sc->flags & XCHK_HAS_QUOTAOFFLOCK) { 177 mutex_unlock(&sc->mp->m_quotainfo->qi_quotaofflock); 178 sc->flags &= ~XCHK_HAS_QUOTAOFFLOCK; 179 } 180 if (sc->buf) { 181 kmem_free(sc->buf); 182 sc->buf = NULL; 183 } 184 return error; 185 } 186 187 /* Scrubbing dispatch. */ 188 189 static const struct xchk_meta_ops meta_scrub_ops[] = { 190 [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */ 191 .type = ST_NONE, 192 .setup = xchk_setup_fs, 193 .scrub = xchk_probe, 194 .repair = xrep_probe, 195 }, 196 [XFS_SCRUB_TYPE_SB] = { /* superblock */ 197 .type = ST_PERAG, 198 .setup = xchk_setup_fs, 199 .scrub = xchk_superblock, 200 .repair = xrep_superblock, 201 }, 202 [XFS_SCRUB_TYPE_AGF] = { /* agf */ 203 .type = ST_PERAG, 204 .setup = xchk_setup_fs, 205 .scrub = xchk_agf, 206 .repair = xrep_agf, 207 }, 208 [XFS_SCRUB_TYPE_AGFL]= { /* agfl */ 209 .type = ST_PERAG, 210 .setup = xchk_setup_fs, 211 .scrub = xchk_agfl, 212 .repair = xrep_agfl, 213 }, 214 [XFS_SCRUB_TYPE_AGI] = { /* agi */ 215 .type = ST_PERAG, 216 .setup = xchk_setup_fs, 217 .scrub = xchk_agi, 218 .repair = xrep_agi, 219 }, 220 [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */ 221 .type = ST_PERAG, 222 .setup = xchk_setup_ag_allocbt, 223 .scrub = xchk_bnobt, 224 .repair = xrep_notsupported, 225 }, 226 [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */ 227 .type = ST_PERAG, 228 .setup = xchk_setup_ag_allocbt, 229 .scrub = xchk_cntbt, 230 .repair = xrep_notsupported, 231 }, 232 [XFS_SCRUB_TYPE_INOBT] = { /* inobt */ 233 .type = ST_PERAG, 234 .setup = xchk_setup_ag_iallocbt, 235 .scrub = xchk_inobt, 236 .repair = xrep_notsupported, 237 }, 238 [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */ 239 .type = ST_PERAG, 240 .setup = xchk_setup_ag_iallocbt, 241 .scrub = xchk_finobt, 242 .has = xfs_has_finobt, 243 .repair = xrep_notsupported, 244 }, 245 [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */ 246 .type = ST_PERAG, 247 .setup = xchk_setup_ag_rmapbt, 248 .scrub = xchk_rmapbt, 249 .has = xfs_has_rmapbt, 250 .repair = xrep_notsupported, 251 }, 252 [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */ 253 .type = ST_PERAG, 254 .setup = xchk_setup_ag_refcountbt, 255 .scrub = xchk_refcountbt, 256 .has = xfs_has_reflink, 257 .repair = xrep_notsupported, 258 }, 259 [XFS_SCRUB_TYPE_INODE] = { /* inode record */ 260 .type = ST_INODE, 261 .setup = xchk_setup_inode, 262 .scrub = xchk_inode, 263 .repair = xrep_notsupported, 264 }, 265 [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */ 266 .type = ST_INODE, 267 .setup = xchk_setup_inode_bmap, 268 .scrub = xchk_bmap_data, 269 .repair = xrep_notsupported, 270 }, 271 [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */ 272 .type = ST_INODE, 273 .setup = xchk_setup_inode_bmap, 274 .scrub = xchk_bmap_attr, 275 .repair = xrep_notsupported, 276 }, 277 [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */ 278 .type = ST_INODE, 279 .setup = xchk_setup_inode_bmap, 280 .scrub = xchk_bmap_cow, 281 .repair = xrep_notsupported, 282 }, 283 [XFS_SCRUB_TYPE_DIR] = { /* directory */ 284 .type = ST_INODE, 285 .setup = xchk_setup_directory, 286 .scrub = xchk_directory, 287 .repair = xrep_notsupported, 288 }, 289 [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */ 290 .type = ST_INODE, 291 .setup = xchk_setup_xattr, 292 .scrub = xchk_xattr, 293 .repair = xrep_notsupported, 294 }, 295 [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */ 296 .type = ST_INODE, 297 .setup = xchk_setup_symlink, 298 .scrub = xchk_symlink, 299 .repair = xrep_notsupported, 300 }, 301 [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */ 302 .type = ST_INODE, 303 .setup = xchk_setup_parent, 304 .scrub = xchk_parent, 305 .repair = xrep_notsupported, 306 }, 307 [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */ 308 .type = ST_FS, 309 .setup = xchk_setup_rt, 310 .scrub = xchk_rtbitmap, 311 .has = xfs_has_realtime, 312 .repair = xrep_notsupported, 313 }, 314 [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */ 315 .type = ST_FS, 316 .setup = xchk_setup_rt, 317 .scrub = xchk_rtsummary, 318 .has = xfs_has_realtime, 319 .repair = xrep_notsupported, 320 }, 321 [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */ 322 .type = ST_FS, 323 .setup = xchk_setup_quota, 324 .scrub = xchk_quota, 325 .repair = xrep_notsupported, 326 }, 327 [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */ 328 .type = ST_FS, 329 .setup = xchk_setup_quota, 330 .scrub = xchk_quota, 331 .repair = xrep_notsupported, 332 }, 333 [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */ 334 .type = ST_FS, 335 .setup = xchk_setup_quota, 336 .scrub = xchk_quota, 337 .repair = xrep_notsupported, 338 }, 339 [XFS_SCRUB_TYPE_FSCOUNTERS] = { /* fs summary counters */ 340 .type = ST_FS, 341 .setup = xchk_setup_fscounters, 342 .scrub = xchk_fscounters, 343 .repair = xrep_notsupported, 344 }, 345 }; 346 347 /* This isn't a stable feature, warn once per day. */ 348 static inline void 349 xchk_experimental_warning( 350 struct xfs_mount *mp) 351 { 352 static struct ratelimit_state scrub_warning = RATELIMIT_STATE_INIT( 353 "xchk_warning", 86400 * HZ, 1); 354 ratelimit_set_flags(&scrub_warning, RATELIMIT_MSG_ON_RELEASE); 355 356 if (__ratelimit(&scrub_warning)) 357 xfs_alert(mp, 358 "EXPERIMENTAL online scrub feature in use. Use at your own risk!"); 359 } 360 361 static int 362 xchk_validate_inputs( 363 struct xfs_mount *mp, 364 struct xfs_scrub_metadata *sm) 365 { 366 int error; 367 const struct xchk_meta_ops *ops; 368 369 error = -EINVAL; 370 /* Check our inputs. */ 371 sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT; 372 if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN) 373 goto out; 374 /* sm_reserved[] must be zero */ 375 if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved))) 376 goto out; 377 378 error = -ENOENT; 379 /* Do we know about this type of metadata? */ 380 if (sm->sm_type >= XFS_SCRUB_TYPE_NR) 381 goto out; 382 ops = &meta_scrub_ops[sm->sm_type]; 383 if (ops->setup == NULL || ops->scrub == NULL) 384 goto out; 385 /* Does this fs even support this type of metadata? */ 386 if (ops->has && !ops->has(mp)) 387 goto out; 388 389 error = -EINVAL; 390 /* restricting fields must be appropriate for type */ 391 switch (ops->type) { 392 case ST_NONE: 393 case ST_FS: 394 if (sm->sm_ino || sm->sm_gen || sm->sm_agno) 395 goto out; 396 break; 397 case ST_PERAG: 398 if (sm->sm_ino || sm->sm_gen || 399 sm->sm_agno >= mp->m_sb.sb_agcount) 400 goto out; 401 break; 402 case ST_INODE: 403 if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino)) 404 goto out; 405 break; 406 default: 407 goto out; 408 } 409 410 /* 411 * We only want to repair read-write v5+ filesystems. Defer the check 412 * for ops->repair until after our scrub confirms that we need to 413 * perform repairs so that we avoid failing due to not supporting 414 * repairing an object that doesn't need repairs. 415 */ 416 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { 417 error = -EOPNOTSUPP; 418 if (!xfs_has_crc(mp)) 419 goto out; 420 421 error = -EROFS; 422 if (xfs_is_readonly(mp)) 423 goto out; 424 } 425 426 error = 0; 427 out: 428 return error; 429 } 430 431 #ifdef CONFIG_XFS_ONLINE_REPAIR 432 static inline void xchk_postmortem(struct xfs_scrub *sc) 433 { 434 /* 435 * Userspace asked us to repair something, we repaired it, rescanned 436 * it, and the rescan says it's still broken. Scream about this in 437 * the system logs. 438 */ 439 if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && 440 (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 441 XFS_SCRUB_OFLAG_XCORRUPT))) 442 xrep_failure(sc->mp); 443 } 444 #else 445 static inline void xchk_postmortem(struct xfs_scrub *sc) 446 { 447 /* 448 * Userspace asked us to scrub something, it's broken, and we have no 449 * way of fixing it. Scream in the logs. 450 */ 451 if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 452 XFS_SCRUB_OFLAG_XCORRUPT)) 453 xfs_alert_ratelimited(sc->mp, 454 "Corruption detected during scrub."); 455 } 456 #endif /* CONFIG_XFS_ONLINE_REPAIR */ 457 458 /* Dispatch metadata scrubbing. */ 459 int 460 xfs_scrub_metadata( 461 struct file *file, 462 struct xfs_scrub_metadata *sm) 463 { 464 struct xfs_scrub *sc; 465 struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount; 466 int error = 0; 467 468 BUILD_BUG_ON(sizeof(meta_scrub_ops) != 469 (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR)); 470 471 trace_xchk_start(XFS_I(file_inode(file)), sm, error); 472 473 /* Forbidden if we are shut down or mounted norecovery. */ 474 error = -ESHUTDOWN; 475 if (xfs_is_shutdown(mp)) 476 goto out; 477 error = -ENOTRECOVERABLE; 478 if (xfs_has_norecovery(mp)) 479 goto out; 480 481 error = xchk_validate_inputs(mp, sm); 482 if (error) 483 goto out; 484 485 xchk_experimental_warning(mp); 486 487 sc = kmem_zalloc(sizeof(struct xfs_scrub), KM_NOFS | KM_MAYFAIL); 488 if (!sc) { 489 error = -ENOMEM; 490 goto out; 491 } 492 493 sc->mp = mp; 494 sc->file = file; 495 sc->sm = sm; 496 sc->ops = &meta_scrub_ops[sm->sm_type]; 497 sc->sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type); 498 retry_op: 499 /* 500 * When repairs are allowed, prevent freezing or readonly remount while 501 * scrub is running with a real transaction. 502 */ 503 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) { 504 error = mnt_want_write_file(sc->file); 505 if (error) 506 goto out_sc; 507 } 508 509 /* Set up for the operation. */ 510 error = sc->ops->setup(sc); 511 if (error) 512 goto out_teardown; 513 514 /* Scrub for errors. */ 515 error = sc->ops->scrub(sc); 516 if (!(sc->flags & XCHK_TRY_HARDER) && error == -EDEADLOCK) { 517 /* 518 * Scrubbers return -EDEADLOCK to mean 'try harder'. 519 * Tear down everything we hold, then set up again with 520 * preparation for worst-case scenarios. 521 */ 522 error = xchk_teardown(sc, 0); 523 if (error) 524 goto out_sc; 525 sc->flags |= XCHK_TRY_HARDER; 526 goto retry_op; 527 } else if (error || (sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE)) 528 goto out_teardown; 529 530 xchk_update_health(sc); 531 532 if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) && 533 !(sc->flags & XREP_ALREADY_FIXED)) { 534 bool needs_fix; 535 536 /* Let debug users force us into the repair routines. */ 537 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR)) 538 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 539 540 needs_fix = (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT | 541 XFS_SCRUB_OFLAG_XCORRUPT | 542 XFS_SCRUB_OFLAG_PREEN)); 543 /* 544 * If userspace asked for a repair but it wasn't necessary, 545 * report that back to userspace. 546 */ 547 if (!needs_fix) { 548 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED; 549 goto out_nofix; 550 } 551 552 /* 553 * If it's broken, userspace wants us to fix it, and we haven't 554 * already tried to fix it, then attempt a repair. 555 */ 556 error = xrep_attempt(sc); 557 if (error == -EAGAIN) { 558 /* 559 * Either the repair function succeeded or it couldn't 560 * get all the resources it needs; either way, we go 561 * back to the beginning and call the scrub function. 562 */ 563 error = xchk_teardown(sc, 0); 564 if (error) { 565 xrep_failure(mp); 566 goto out_sc; 567 } 568 goto retry_op; 569 } 570 } 571 572 out_nofix: 573 xchk_postmortem(sc); 574 out_teardown: 575 error = xchk_teardown(sc, error); 576 out_sc: 577 kmem_free(sc); 578 out: 579 trace_xchk_done(XFS_I(file_inode(file)), sm, error); 580 if (error == -EFSCORRUPTED || error == -EFSBADCRC) { 581 sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT; 582 error = 0; 583 } 584 return error; 585 } 586