1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017-2023 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
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"
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 #include "scrub/stats.h"
26 #include "scrub/xfile.h"
29 * Online Scrub and Repair
31 * Traditionally, XFS (the kernel driver) did not know how to check or
32 * repair on-disk data structures. That task was left to the xfs_check
33 * and xfs_repair tools, both of which require taking the filesystem
34 * offline for a thorough but time consuming examination. Online
35 * scrub & repair, on the other hand, enables us to check the metadata
36 * for obvious errors while carefully stepping around the filesystem's
37 * ongoing operations, locking rules, etc.
39 * Given that most XFS metadata consist of records stored in a btree,
40 * most of the checking functions iterate the btree blocks themselves
41 * looking for irregularities. When a record block is encountered, each
42 * record can be checked for obviously bad values. Record values can
43 * also be cross-referenced against other btrees to look for potential
44 * misunderstandings between pieces of metadata.
46 * It is expected that the checkers responsible for per-AG metadata
47 * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
48 * metadata structure, and perform any relevant cross-referencing before
49 * unlocking the AG and returning the results to userspace. These
50 * scrubbers must not keep an AG locked for too long to avoid tying up
51 * the block and inode allocators.
53 * Block maps and b-trees rooted in an inode present a special challenge
54 * because they can involve extents from any AG. The general scrubber
55 * structure of lock -> check -> xref -> unlock still holds, but AG
56 * locking order rules /must/ be obeyed to avoid deadlocks. The
57 * ordering rule, of course, is that we must lock in increasing AG
58 * order. Helper functions are provided to track which AG headers we've
59 * already locked. If we detect an imminent locking order violation, we
60 * can signal a potential deadlock, in which case the scrubber can jump
61 * out to the top level, lock all the AGs in order, and retry the scrub.
63 * For file data (directories, extended attributes, symlinks) scrub, we
64 * can simply lock the inode and walk the data. For btree data
65 * (directories and attributes) we follow the same btree-scrubbing
66 * strategy outlined previously to check the records.
68 * We use a bit of trickery with transactions to avoid buffer deadlocks
69 * if there is a cycle in the metadata. The basic problem is that
70 * travelling down a btree involves locking the current buffer at each
71 * tree level. If a pointer should somehow point back to a buffer that
72 * we've already examined, we will deadlock due to the second buffer
73 * locking attempt. Note however that grabbing a buffer in transaction
74 * context links the locked buffer to the transaction. If we try to
75 * re-grab the buffer in the context of the same transaction, we avoid
76 * the second lock attempt and continue. Between the verifier and the
77 * scrubber, something will notice that something is amiss and report
78 * the corruption. Therefore, each scrubber will allocate an empty
79 * transaction, attach buffers to it, and cancel the transaction at the
80 * end of the scrub run. Cancelling a non-dirty transaction simply
81 * unlocks the buffers.
83 * There are four pieces of data that scrub can communicate to
84 * userspace. The first is the error code (errno), which can be used to
85 * communicate operational errors in performing the scrub. There are
86 * also three flags that can be set in the scrub context. If the data
87 * structure itself is corrupt, the CORRUPT flag will be set. If
88 * the metadata is correct but otherwise suboptimal, the PREEN flag
91 * We perform secondary validation of filesystem metadata by
92 * cross-referencing every record with all other available metadata.
93 * For example, for block mapping extents, we verify that there are no
94 * records in the free space and inode btrees corresponding to that
95 * space extent and that there is a corresponding entry in the reverse
96 * mapping btree. Inconsistent metadata is noted by setting the
97 * XCORRUPT flag; btree query function errors are noted by setting the
98 * XFAIL flag and deleting the cursor to prevent further attempts to
99 * cross-reference with a defective btree.
101 * If a piece of metadata proves corrupt or suboptimal, the userspace
102 * program can ask the kernel to apply some tender loving care (TLC) to
103 * the metadata object by setting the REPAIR flag and re-calling the
104 * scrub ioctl. "Corruption" is defined by metadata violating the
105 * on-disk specification; operations cannot continue if the violation is
106 * left untreated. It is possible for XFS to continue if an object is
107 * "suboptimal", however performance may be degraded. Repairs are
108 * usually performed by rebuilding the metadata entirely out of
109 * redundant metadata. Optimizing, on the other hand, can sometimes be
110 * done without rebuilding entire structures.
112 * Generally speaking, the repair code has the following code structure:
113 * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
114 * The first check helps us figure out if we need to rebuild or simply
115 * optimize the structure so that the rebuild knows what to do. The
116 * second check evaluates the completeness of the repair; that is what
117 * is reported to userspace.
119 * A quick note on symbol prefixes:
120 * - "xfs_" are general XFS symbols.
121 * - "xchk_" are symbols related to metadata checking.
122 * - "xrep_" are symbols related to metadata repair.
123 * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS.
127 * Scrub probe -- userspace uses this to probe if we're willing to scrub
128 * or repair a given mountpoint. This will be used by xfs_scrub to
129 * probe the kernel's abilities to scrub (and repair) the metadata. We
130 * do this by validating the ioctl inputs from userspace, preparing the
131 * filesystem for a scrub (or a repair) operation, and immediately
132 * returning to userspace. Userspace can use the returned errno and
133 * structure state to decide (in broad terms) if scrub/repair are
134 * supported by the running kernel.
138 struct xfs_scrub *sc)
142 if (xchk_should_terminate(sc, &error))
148 /* Scrub setup and teardown */
151 xchk_fsgates_disable(
152 struct xfs_scrub *sc)
154 if (!(sc->flags & XCHK_FSGATES_ALL))
157 trace_xchk_fsgates_disable(sc, sc->flags & XCHK_FSGATES_ALL);
159 if (sc->flags & XCHK_FSGATES_DRAIN)
160 xfs_drain_wait_disable();
162 sc->flags &= ~XCHK_FSGATES_ALL;
165 /* Free all the resources and finish the transactions. */
168 struct xfs_scrub *sc,
171 xchk_ag_free(sc, &sc->sa);
173 if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
174 error = xfs_trans_commit(sc->tp);
176 xfs_trans_cancel(sc->tp);
181 xchk_iunlock(sc, sc->ilock_flags);
182 xchk_irele(sc, sc->ip);
185 if (sc->flags & XCHK_HAVE_FREEZE_PROT) {
186 sc->flags &= ~XCHK_HAVE_FREEZE_PROT;
187 mnt_drop_write_file(sc->file);
190 xfile_destroy(sc->xfile);
195 sc->buf_cleanup(sc->buf);
197 sc->buf_cleanup = NULL;
201 xchk_fsgates_disable(sc);
205 /* Scrubbing dispatch. */
207 static const struct xchk_meta_ops meta_scrub_ops[] = {
208 [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */
210 .setup = xchk_setup_fs,
212 .repair = xrep_probe,
214 [XFS_SCRUB_TYPE_SB] = { /* superblock */
216 .setup = xchk_setup_agheader,
217 .scrub = xchk_superblock,
218 .repair = xrep_superblock,
220 [XFS_SCRUB_TYPE_AGF] = { /* agf */
222 .setup = xchk_setup_agheader,
226 [XFS_SCRUB_TYPE_AGFL]= { /* agfl */
228 .setup = xchk_setup_agheader,
232 [XFS_SCRUB_TYPE_AGI] = { /* agi */
234 .setup = xchk_setup_agheader,
238 [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */
240 .setup = xchk_setup_ag_allocbt,
242 .repair = xrep_notsupported,
244 [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */
246 .setup = xchk_setup_ag_allocbt,
248 .repair = xrep_notsupported,
250 [XFS_SCRUB_TYPE_INOBT] = { /* inobt */
252 .setup = xchk_setup_ag_iallocbt,
254 .repair = xrep_notsupported,
256 [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */
258 .setup = xchk_setup_ag_iallocbt,
259 .scrub = xchk_finobt,
260 .has = xfs_has_finobt,
261 .repair = xrep_notsupported,
263 [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */
265 .setup = xchk_setup_ag_rmapbt,
266 .scrub = xchk_rmapbt,
267 .has = xfs_has_rmapbt,
268 .repair = xrep_notsupported,
270 [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */
272 .setup = xchk_setup_ag_refcountbt,
273 .scrub = xchk_refcountbt,
274 .has = xfs_has_reflink,
275 .repair = xrep_notsupported,
277 [XFS_SCRUB_TYPE_INODE] = { /* inode record */
279 .setup = xchk_setup_inode,
281 .repair = xrep_notsupported,
283 [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */
285 .setup = xchk_setup_inode_bmap,
286 .scrub = xchk_bmap_data,
287 .repair = xrep_notsupported,
289 [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */
291 .setup = xchk_setup_inode_bmap,
292 .scrub = xchk_bmap_attr,
293 .repair = xrep_notsupported,
295 [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */
297 .setup = xchk_setup_inode_bmap,
298 .scrub = xchk_bmap_cow,
299 .repair = xrep_notsupported,
301 [XFS_SCRUB_TYPE_DIR] = { /* directory */
303 .setup = xchk_setup_directory,
304 .scrub = xchk_directory,
305 .repair = xrep_notsupported,
307 [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */
309 .setup = xchk_setup_xattr,
311 .repair = xrep_notsupported,
313 [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */
315 .setup = xchk_setup_symlink,
316 .scrub = xchk_symlink,
317 .repair = xrep_notsupported,
319 [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */
321 .setup = xchk_setup_parent,
322 .scrub = xchk_parent,
323 .repair = xrep_notsupported,
325 [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */
327 .setup = xchk_setup_rtbitmap,
328 .scrub = xchk_rtbitmap,
329 .has = xfs_has_realtime,
330 .repair = xrep_notsupported,
332 [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */
334 .setup = xchk_setup_rtsummary,
335 .scrub = xchk_rtsummary,
336 .has = xfs_has_realtime,
337 .repair = xrep_notsupported,
339 [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */
341 .setup = xchk_setup_quota,
343 .repair = xrep_notsupported,
345 [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */
347 .setup = xchk_setup_quota,
349 .repair = xrep_notsupported,
351 [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */
353 .setup = xchk_setup_quota,
355 .repair = xrep_notsupported,
357 [XFS_SCRUB_TYPE_FSCOUNTERS] = { /* fs summary counters */
359 .setup = xchk_setup_fscounters,
360 .scrub = xchk_fscounters,
361 .repair = xrep_notsupported,
366 xchk_validate_inputs(
367 struct xfs_mount *mp,
368 struct xfs_scrub_metadata *sm)
371 const struct xchk_meta_ops *ops;
374 /* Check our inputs. */
375 sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
376 if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
378 /* sm_reserved[] must be zero */
379 if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved)))
383 /* Do we know about this type of metadata? */
384 if (sm->sm_type >= XFS_SCRUB_TYPE_NR)
386 ops = &meta_scrub_ops[sm->sm_type];
387 if (ops->setup == NULL || ops->scrub == NULL)
389 /* Does this fs even support this type of metadata? */
390 if (ops->has && !ops->has(mp))
394 /* restricting fields must be appropriate for type */
398 if (sm->sm_ino || sm->sm_gen || sm->sm_agno)
402 if (sm->sm_ino || sm->sm_gen ||
403 sm->sm_agno >= mp->m_sb.sb_agcount)
407 if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino))
414 /* No rebuild without repair. */
415 if ((sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD) &&
416 !(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
420 * We only want to repair read-write v5+ filesystems. Defer the check
421 * for ops->repair until after our scrub confirms that we need to
422 * perform repairs so that we avoid failing due to not supporting
423 * repairing an object that doesn't need repairs.
425 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
427 if (!xfs_has_crc(mp))
431 if (xfs_is_readonly(mp))
440 #ifdef CONFIG_XFS_ONLINE_REPAIR
441 static inline void xchk_postmortem(struct xfs_scrub *sc)
444 * Userspace asked us to repair something, we repaired it, rescanned
445 * it, and the rescan says it's still broken. Scream about this in
448 if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
449 (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
450 XFS_SCRUB_OFLAG_XCORRUPT)))
451 xrep_failure(sc->mp);
454 static inline void xchk_postmortem(struct xfs_scrub *sc)
457 * Userspace asked us to scrub something, it's broken, and we have no
458 * way of fixing it. Scream in the logs.
460 if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
461 XFS_SCRUB_OFLAG_XCORRUPT))
462 xfs_alert_ratelimited(sc->mp,
463 "Corruption detected during scrub.");
465 #endif /* CONFIG_XFS_ONLINE_REPAIR */
467 /* Dispatch metadata scrubbing. */
471 struct xfs_scrub_metadata *sm)
473 struct xchk_stats_run run = { };
474 struct xfs_scrub *sc;
475 struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
479 BUILD_BUG_ON(sizeof(meta_scrub_ops) !=
480 (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR));
482 trace_xchk_start(XFS_I(file_inode(file)), sm, error);
484 /* Forbidden if we are shut down or mounted norecovery. */
486 if (xfs_is_shutdown(mp))
488 error = -ENOTRECOVERABLE;
489 if (xfs_has_norecovery(mp))
492 error = xchk_validate_inputs(mp, sm);
496 xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SCRUB,
497 "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
499 sc = kzalloc(sizeof(struct xfs_scrub), XCHK_GFP_FLAGS);
508 sc->ops = &meta_scrub_ops[sm->sm_type];
509 sc->sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type);
512 * When repairs are allowed, prevent freezing or readonly remount while
513 * scrub is running with a real transaction.
515 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
516 error = mnt_want_write_file(sc->file);
520 sc->flags |= XCHK_HAVE_FREEZE_PROT;
523 /* Set up for the operation. */
524 error = sc->ops->setup(sc);
525 if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
527 if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
532 /* Scrub for errors. */
533 check_start = xchk_stats_now();
534 error = sc->ops->scrub(sc);
535 run.scrub_ns += xchk_stats_elapsed_ns(check_start);
536 if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
538 if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
540 if (error || (sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE))
543 xchk_update_health(sc);
545 if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
546 !(sc->flags & XREP_ALREADY_FIXED)) {
547 bool needs_fix = xchk_needs_repair(sc->sm);
549 /* Userspace asked us to rebuild the structure regardless. */
550 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD)
553 /* Let debug users force us into the repair routines. */
554 if (XFS_TEST_ERROR(needs_fix, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR))
558 * If userspace asked for a repair but it wasn't necessary,
559 * report that back to userspace.
562 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED;
567 * If it's broken, userspace wants us to fix it, and we haven't
568 * already tried to fix it, then attempt a repair.
570 error = xrep_attempt(sc, &run);
571 if (error == -EAGAIN) {
573 * Either the repair function succeeded or it couldn't
574 * get all the resources it needs; either way, we go
575 * back to the beginning and call the scrub function.
577 error = xchk_teardown(sc, 0);
589 error = xchk_teardown(sc, error);
591 if (error != -ENOENT)
592 xchk_stats_merge(mp, sm, &run);
595 trace_xchk_done(XFS_I(file_inode(file)), sm, error);
596 if (error == -EFSCORRUPTED || error == -EFSBADCRC) {
597 sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
602 error = xchk_teardown(sc, 0);
605 sc->flags |= XCHK_NEED_DRAIN;
610 * Scrubbers return -EDEADLOCK to mean 'try harder'. Tear down
611 * everything we hold, then set up again with preparation for
612 * worst-case scenarios.
614 error = xchk_teardown(sc, 0);
617 sc->flags |= XCHK_TRY_HARDER;