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