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"
27 * Online Scrub and Repair
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.
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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.
136 struct xfs_scrub *sc)
140 if (xchk_should_terminate(sc, &error))
146 /* Scrub setup and teardown */
149 xchk_fsgates_disable(
150 struct xfs_scrub *sc)
152 if (!(sc->flags & XCHK_FSGATES_ALL))
155 trace_xchk_fsgates_disable(sc, sc->flags & XCHK_FSGATES_ALL);
157 if (sc->flags & XCHK_FSGATES_DRAIN)
158 xfs_drain_wait_disable();
160 sc->flags &= ~XCHK_FSGATES_ALL;
163 /* Free all the resources and finish the transactions. */
166 struct xfs_scrub *sc,
169 struct xfs_inode *ip_in = XFS_I(file_inode(sc->file));
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 xfs_iunlock(sc->ip, sc->ilock_flags);
182 if (sc->ip != ip_in &&
183 !xfs_internal_inum(sc->mp, sc->ip->i_ino))
184 xchk_irele(sc, sc->ip);
187 if (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR)
188 mnt_drop_write_file(sc->file);
191 sc->buf_cleanup(sc->buf);
193 sc->buf_cleanup = NULL;
197 xchk_fsgates_disable(sc);
201 /* Scrubbing dispatch. */
203 static const struct xchk_meta_ops meta_scrub_ops[] = {
204 [XFS_SCRUB_TYPE_PROBE] = { /* ioctl presence test */
206 .setup = xchk_setup_fs,
208 .repair = xrep_probe,
210 [XFS_SCRUB_TYPE_SB] = { /* superblock */
212 .setup = xchk_setup_agheader,
213 .scrub = xchk_superblock,
214 .repair = xrep_superblock,
216 [XFS_SCRUB_TYPE_AGF] = { /* agf */
218 .setup = xchk_setup_agheader,
222 [XFS_SCRUB_TYPE_AGFL]= { /* agfl */
224 .setup = xchk_setup_agheader,
228 [XFS_SCRUB_TYPE_AGI] = { /* agi */
230 .setup = xchk_setup_agheader,
234 [XFS_SCRUB_TYPE_BNOBT] = { /* bnobt */
236 .setup = xchk_setup_ag_allocbt,
238 .repair = xrep_notsupported,
240 [XFS_SCRUB_TYPE_CNTBT] = { /* cntbt */
242 .setup = xchk_setup_ag_allocbt,
244 .repair = xrep_notsupported,
246 [XFS_SCRUB_TYPE_INOBT] = { /* inobt */
248 .setup = xchk_setup_ag_iallocbt,
250 .repair = xrep_notsupported,
252 [XFS_SCRUB_TYPE_FINOBT] = { /* finobt */
254 .setup = xchk_setup_ag_iallocbt,
255 .scrub = xchk_finobt,
256 .has = xfs_has_finobt,
257 .repair = xrep_notsupported,
259 [XFS_SCRUB_TYPE_RMAPBT] = { /* rmapbt */
261 .setup = xchk_setup_ag_rmapbt,
262 .scrub = xchk_rmapbt,
263 .has = xfs_has_rmapbt,
264 .repair = xrep_notsupported,
266 [XFS_SCRUB_TYPE_REFCNTBT] = { /* refcountbt */
268 .setup = xchk_setup_ag_refcountbt,
269 .scrub = xchk_refcountbt,
270 .has = xfs_has_reflink,
271 .repair = xrep_notsupported,
273 [XFS_SCRUB_TYPE_INODE] = { /* inode record */
275 .setup = xchk_setup_inode,
277 .repair = xrep_notsupported,
279 [XFS_SCRUB_TYPE_BMBTD] = { /* inode data fork */
281 .setup = xchk_setup_inode_bmap,
282 .scrub = xchk_bmap_data,
283 .repair = xrep_notsupported,
285 [XFS_SCRUB_TYPE_BMBTA] = { /* inode attr fork */
287 .setup = xchk_setup_inode_bmap,
288 .scrub = xchk_bmap_attr,
289 .repair = xrep_notsupported,
291 [XFS_SCRUB_TYPE_BMBTC] = { /* inode CoW fork */
293 .setup = xchk_setup_inode_bmap,
294 .scrub = xchk_bmap_cow,
295 .repair = xrep_notsupported,
297 [XFS_SCRUB_TYPE_DIR] = { /* directory */
299 .setup = xchk_setup_directory,
300 .scrub = xchk_directory,
301 .repair = xrep_notsupported,
303 [XFS_SCRUB_TYPE_XATTR] = { /* extended attributes */
305 .setup = xchk_setup_xattr,
307 .repair = xrep_notsupported,
309 [XFS_SCRUB_TYPE_SYMLINK] = { /* symbolic link */
311 .setup = xchk_setup_symlink,
312 .scrub = xchk_symlink,
313 .repair = xrep_notsupported,
315 [XFS_SCRUB_TYPE_PARENT] = { /* parent pointers */
317 .setup = xchk_setup_parent,
318 .scrub = xchk_parent,
319 .repair = xrep_notsupported,
321 [XFS_SCRUB_TYPE_RTBITMAP] = { /* realtime bitmap */
323 .setup = xchk_setup_rt,
324 .scrub = xchk_rtbitmap,
325 .has = xfs_has_realtime,
326 .repair = xrep_notsupported,
328 [XFS_SCRUB_TYPE_RTSUM] = { /* realtime summary */
330 .setup = xchk_setup_rt,
331 .scrub = xchk_rtsummary,
332 .has = xfs_has_realtime,
333 .repair = xrep_notsupported,
335 [XFS_SCRUB_TYPE_UQUOTA] = { /* user quota */
337 .setup = xchk_setup_quota,
339 .repair = xrep_notsupported,
341 [XFS_SCRUB_TYPE_GQUOTA] = { /* group quota */
343 .setup = xchk_setup_quota,
345 .repair = xrep_notsupported,
347 [XFS_SCRUB_TYPE_PQUOTA] = { /* project quota */
349 .setup = xchk_setup_quota,
351 .repair = xrep_notsupported,
353 [XFS_SCRUB_TYPE_FSCOUNTERS] = { /* fs summary counters */
355 .setup = xchk_setup_fscounters,
356 .scrub = xchk_fscounters,
357 .repair = xrep_notsupported,
362 xchk_validate_inputs(
363 struct xfs_mount *mp,
364 struct xfs_scrub_metadata *sm)
367 const struct xchk_meta_ops *ops;
370 /* Check our inputs. */
371 sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
372 if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
374 /* sm_reserved[] must be zero */
375 if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved)))
379 /* Do we know about this type of metadata? */
380 if (sm->sm_type >= XFS_SCRUB_TYPE_NR)
382 ops = &meta_scrub_ops[sm->sm_type];
383 if (ops->setup == NULL || ops->scrub == NULL)
385 /* Does this fs even support this type of metadata? */
386 if (ops->has && !ops->has(mp))
390 /* restricting fields must be appropriate for type */
394 if (sm->sm_ino || sm->sm_gen || sm->sm_agno)
398 if (sm->sm_ino || sm->sm_gen ||
399 sm->sm_agno >= mp->m_sb.sb_agcount)
403 if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino))
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.
416 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
418 if (!xfs_has_crc(mp))
422 if (xfs_is_readonly(mp))
431 #ifdef CONFIG_XFS_ONLINE_REPAIR
432 static inline void xchk_postmortem(struct xfs_scrub *sc)
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
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);
445 static inline void xchk_postmortem(struct xfs_scrub *sc)
448 * Userspace asked us to scrub something, it's broken, and we have no
449 * way of fixing it. Scream in the logs.
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.");
456 #endif /* CONFIG_XFS_ONLINE_REPAIR */
458 /* Dispatch metadata scrubbing. */
462 struct xfs_scrub_metadata *sm)
464 struct xfs_scrub *sc;
465 struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
468 BUILD_BUG_ON(sizeof(meta_scrub_ops) !=
469 (sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR));
471 trace_xchk_start(XFS_I(file_inode(file)), sm, error);
473 /* Forbidden if we are shut down or mounted norecovery. */
475 if (xfs_is_shutdown(mp))
477 error = -ENOTRECOVERABLE;
478 if (xfs_has_norecovery(mp))
481 error = xchk_validate_inputs(mp, sm);
485 xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SCRUB,
486 "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
488 sc = kzalloc(sizeof(struct xfs_scrub), XCHK_GFP_FLAGS);
497 sc->ops = &meta_scrub_ops[sm->sm_type];
498 sc->sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type);
501 * When repairs are allowed, prevent freezing or readonly remount while
502 * scrub is running with a real transaction.
504 if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
505 error = mnt_want_write_file(sc->file);
510 /* Set up for the operation. */
511 error = sc->ops->setup(sc);
512 if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
514 if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
519 /* Scrub for errors. */
520 error = sc->ops->scrub(sc);
521 if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
523 if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
525 if (error || (sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE))
528 xchk_update_health(sc);
530 if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
531 !(sc->flags & XREP_ALREADY_FIXED)) {
534 /* Let debug users force us into the repair routines. */
535 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_FORCE_SCRUB_REPAIR))
536 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
538 needs_fix = (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
539 XFS_SCRUB_OFLAG_XCORRUPT |
540 XFS_SCRUB_OFLAG_PREEN));
542 * If userspace asked for a repair but it wasn't necessary,
543 * report that back to userspace.
546 sc->sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED;
551 * If it's broken, userspace wants us to fix it, and we haven't
552 * already tried to fix it, then attempt a repair.
554 error = xrep_attempt(sc);
555 if (error == -EAGAIN) {
557 * Either the repair function succeeded or it couldn't
558 * get all the resources it needs; either way, we go
559 * back to the beginning and call the scrub function.
561 error = xchk_teardown(sc, 0);
573 error = xchk_teardown(sc, error);
577 trace_xchk_done(XFS_I(file_inode(file)), sm, error);
578 if (error == -EFSCORRUPTED || error == -EFSBADCRC) {
579 sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
584 error = xchk_teardown(sc, 0);
587 sc->flags |= XCHK_NEED_DRAIN;
591 * Scrubbers return -EDEADLOCK to mean 'try harder'. Tear down
592 * everything we hold, then set up again with preparation for
593 * worst-case scenarios.
595 error = xchk_teardown(sc, 0);
598 sc->flags |= XCHK_TRY_HARDER;