1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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"
15 #include "xfs_log_format.h"
16 #include "xfs_trans.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"
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"
30 #include "xfs_rmap_btree.h"
31 #include "xfs_quota.h"
33 #include "xfs_errortag.h"
34 #include "xfs_error.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 #include "scrub/health.h"
46 * Online Scrub and Repair
48 * Traditionally, XFS (the kernel driver) did not know how to check or
49 * repair on-disk data structures. That task was left to the xfs_check
50 * and xfs_repair tools, both of which require taking the filesystem
51 * offline for a thorough but time consuming examination. Online
52 * scrub & repair, on the other hand, enables us to check the metadata
53 * for obvious errors while carefully stepping around the filesystem's
54 * ongoing operations, locking rules, etc.
56 * Given that most XFS metadata consist of records stored in a btree,
57 * most of the checking functions iterate the btree blocks themselves
58 * looking for irregularities. When a record block is encountered, each
59 * record can be checked for obviously bad values. Record values can
60 * also be cross-referenced against other btrees to look for potential
61 * misunderstandings between pieces of metadata.
63 * It is expected that the checkers responsible for per-AG metadata
64 * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
65 * metadata structure, and perform any relevant cross-referencing before
66 * unlocking the AG and returning the results to userspace. These
67 * scrubbers must not keep an AG locked for too long to avoid tying up
68 * the block and inode allocators.
70 * Block maps and b-trees rooted in an inode present a special challenge
71 * because they can involve extents from any AG. The general scrubber
72 * structure of lock -> check -> xref -> unlock still holds, but AG
73 * locking order rules /must/ be obeyed to avoid deadlocks. The
74 * ordering rule, of course, is that we must lock in increasing AG
75 * order. Helper functions are provided to track which AG headers we've
76 * already locked. If we detect an imminent locking order violation, we
77 * can signal a potential deadlock, in which case the scrubber can jump
78 * out to the top level, lock all the AGs in order, and retry the scrub.
80 * For file data (directories, extended attributes, symlinks) scrub, we
81 * can simply lock the inode and walk the data. For btree data
82 * (directories and attributes) we follow the same btree-scrubbing
83 * strategy outlined previously to check the records.
85 * We use a bit of trickery with transactions to avoid buffer deadlocks
86 * if there is a cycle in the metadata. The basic problem is that
87 * travelling down a btree involves locking the current buffer at each
88 * tree level. If a pointer should somehow point back to a buffer that
89 * we've already examined, we will deadlock due to the second buffer
90 * locking attempt. Note however that grabbing a buffer in transaction
91 * context links the locked buffer to the transaction. If we try to
92 * re-grab the buffer in the context of the same transaction, we avoid
93 * the second lock attempt and continue. Between the verifier and the
94 * scrubber, something will notice that something is amiss and report
95 * the corruption. Therefore, each scrubber will allocate an empty
96 * transaction, attach buffers to it, and cancel the transaction at the
97 * end of the scrub run. Cancelling a non-dirty transaction simply
98 * unlocks the buffers.
100 * There are four pieces of data that scrub can communicate to
101 * userspace. The first is the error code (errno), which can be used to
102 * communicate operational errors in performing the scrub. There are
103 * also three flags that can be set in the scrub context. If the data
104 * structure itself is corrupt, the CORRUPT flag will be set. If
105 * the metadata is correct but otherwise suboptimal, the PREEN flag
108 * We perform secondary validation of filesystem metadata by
109 * cross-referencing every record with all other available metadata.
110 * For example, for block mapping extents, we verify that there are no
111 * records in the free space and inode btrees corresponding to that
112 * space extent and that there is a corresponding entry in the reverse
113 * mapping btree. Inconsistent metadata is noted by setting the
114 * XCORRUPT flag; btree query function errors are noted by setting the
115 * XFAIL flag and deleting the cursor to prevent further attempts to
116 * cross-reference with a defective btree.
118 * If a piece of metadata proves corrupt or suboptimal, the userspace
119 * program can ask the kernel to apply some tender loving care (TLC) to
120 * the metadata object by setting the REPAIR flag and re-calling the
121 * scrub ioctl. "Corruption" is defined by metadata violating the
122 * on-disk specification; operations cannot continue if the violation is
123 * left untreated. It is possible for XFS to continue if an object is
124 * "suboptimal", however performance may be degraded. Repairs are
125 * usually performed by rebuilding the metadata entirely out of
126 * redundant metadata. Optimizing, on the other hand, can sometimes be
127 * done without rebuilding entire structures.
129 * Generally speaking, the repair code has the following code structure:
130 * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
131 * The first check helps us figure out if we need to rebuild or simply
132 * optimize the structure so that the rebuild knows what to do. The
133 * second check evaluates the completeness of the repair; that is what
134 * is reported to userspace.
136 * A quick note on symbol prefixes:
137 * - "xfs_" are general XFS symbols.
138 * - "xchk_" are symbols related to metadata checking.
139 * - "xrep_" are symbols related to metadata repair.
140 * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS.
144 * Scrub probe -- userspace uses this to probe if we're willing to scrub
145 * or repair a given mountpoint. This will be used by xfs_scrub to
146 * probe the kernel's abilities to scrub (and repair) the metadata. We
147 * do this by validating the ioctl inputs from userspace, preparing the
148 * filesystem for a scrub (or a repair) operation, and immediately
149 * returning to userspace. Userspace can use the returned errno and
150 * structure state to decide (in broad terms) if scrub/repair are
151 * supported by the running kernel.
155 struct xfs_scrub
*sc
)
159 if (xchk_should_terminate(sc
, &error
))
165 /* Scrub setup and teardown */
167 /* Free all the resources and finish the transactions. */
170 struct xfs_scrub
*sc
,
171 struct xfs_inode
*ip_in
,
174 xchk_ag_free(sc
, &sc
->sa
);
176 if (error
== 0 && (sc
->sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
))
177 error
= xfs_trans_commit(sc
->tp
);
179 xfs_trans_cancel(sc
->tp
);
184 xfs_iunlock(sc
->ip
, sc
->ilock_flags
);
185 if (sc
->ip
!= ip_in
&&
186 !xfs_internal_inum(sc
->mp
, sc
->ip
->i_ino
))
190 if (sc
->flags
& XCHK_REAPING_DISABLED
)
191 xchk_start_reaping(sc
);
192 if (sc
->flags
& XCHK_HAS_QUOTAOFFLOCK
) {
193 mutex_unlock(&sc
->mp
->m_quotainfo
->qi_quotaofflock
);
194 sc
->flags
&= ~XCHK_HAS_QUOTAOFFLOCK
;
203 /* Scrubbing dispatch. */
205 static const struct xchk_meta_ops meta_scrub_ops
[] = {
206 [XFS_SCRUB_TYPE_PROBE
] = { /* ioctl presence test */
208 .setup
= xchk_setup_fs
,
210 .repair
= xrep_probe
,
212 [XFS_SCRUB_TYPE_SB
] = { /* superblock */
214 .setup
= xchk_setup_fs
,
215 .scrub
= xchk_superblock
,
216 .repair
= xrep_superblock
,
218 [XFS_SCRUB_TYPE_AGF
] = { /* agf */
220 .setup
= xchk_setup_fs
,
224 [XFS_SCRUB_TYPE_AGFL
]= { /* agfl */
226 .setup
= xchk_setup_fs
,
230 [XFS_SCRUB_TYPE_AGI
] = { /* agi */
232 .setup
= xchk_setup_fs
,
236 [XFS_SCRUB_TYPE_BNOBT
] = { /* bnobt */
238 .setup
= xchk_setup_ag_allocbt
,
240 .repair
= xrep_notsupported
,
242 [XFS_SCRUB_TYPE_CNTBT
] = { /* cntbt */
244 .setup
= xchk_setup_ag_allocbt
,
246 .repair
= xrep_notsupported
,
248 [XFS_SCRUB_TYPE_INOBT
] = { /* inobt */
250 .setup
= xchk_setup_ag_iallocbt
,
252 .repair
= xrep_notsupported
,
254 [XFS_SCRUB_TYPE_FINOBT
] = { /* finobt */
256 .setup
= xchk_setup_ag_iallocbt
,
257 .scrub
= xchk_finobt
,
258 .has
= xfs_sb_version_hasfinobt
,
259 .repair
= xrep_notsupported
,
261 [XFS_SCRUB_TYPE_RMAPBT
] = { /* rmapbt */
263 .setup
= xchk_setup_ag_rmapbt
,
264 .scrub
= xchk_rmapbt
,
265 .has
= xfs_sb_version_hasrmapbt
,
266 .repair
= xrep_notsupported
,
268 [XFS_SCRUB_TYPE_REFCNTBT
] = { /* refcountbt */
270 .setup
= xchk_setup_ag_refcountbt
,
271 .scrub
= xchk_refcountbt
,
272 .has
= xfs_sb_version_hasreflink
,
273 .repair
= xrep_notsupported
,
275 [XFS_SCRUB_TYPE_INODE
] = { /* inode record */
277 .setup
= xchk_setup_inode
,
279 .repair
= xrep_notsupported
,
281 [XFS_SCRUB_TYPE_BMBTD
] = { /* inode data fork */
283 .setup
= xchk_setup_inode_bmap
,
284 .scrub
= xchk_bmap_data
,
285 .repair
= xrep_notsupported
,
287 [XFS_SCRUB_TYPE_BMBTA
] = { /* inode attr fork */
289 .setup
= xchk_setup_inode_bmap
,
290 .scrub
= xchk_bmap_attr
,
291 .repair
= xrep_notsupported
,
293 [XFS_SCRUB_TYPE_BMBTC
] = { /* inode CoW fork */
295 .setup
= xchk_setup_inode_bmap
,
296 .scrub
= xchk_bmap_cow
,
297 .repair
= xrep_notsupported
,
299 [XFS_SCRUB_TYPE_DIR
] = { /* directory */
301 .setup
= xchk_setup_directory
,
302 .scrub
= xchk_directory
,
303 .repair
= xrep_notsupported
,
305 [XFS_SCRUB_TYPE_XATTR
] = { /* extended attributes */
307 .setup
= xchk_setup_xattr
,
309 .repair
= xrep_notsupported
,
311 [XFS_SCRUB_TYPE_SYMLINK
] = { /* symbolic link */
313 .setup
= xchk_setup_symlink
,
314 .scrub
= xchk_symlink
,
315 .repair
= xrep_notsupported
,
317 [XFS_SCRUB_TYPE_PARENT
] = { /* parent pointers */
319 .setup
= xchk_setup_parent
,
320 .scrub
= xchk_parent
,
321 .repair
= xrep_notsupported
,
323 [XFS_SCRUB_TYPE_RTBITMAP
] = { /* realtime bitmap */
325 .setup
= xchk_setup_rt
,
326 .scrub
= xchk_rtbitmap
,
327 .has
= xfs_sb_version_hasrealtime
,
328 .repair
= xrep_notsupported
,
330 [XFS_SCRUB_TYPE_RTSUM
] = { /* realtime summary */
332 .setup
= xchk_setup_rt
,
333 .scrub
= xchk_rtsummary
,
334 .has
= xfs_sb_version_hasrealtime
,
335 .repair
= xrep_notsupported
,
337 [XFS_SCRUB_TYPE_UQUOTA
] = { /* user quota */
339 .setup
= xchk_setup_quota
,
341 .repair
= xrep_notsupported
,
343 [XFS_SCRUB_TYPE_GQUOTA
] = { /* group quota */
345 .setup
= xchk_setup_quota
,
347 .repair
= xrep_notsupported
,
349 [XFS_SCRUB_TYPE_PQUOTA
] = { /* project quota */
351 .setup
= xchk_setup_quota
,
353 .repair
= xrep_notsupported
,
355 [XFS_SCRUB_TYPE_FSCOUNTERS
] = { /* fs summary counters */
357 .setup
= xchk_setup_fscounters
,
358 .scrub
= xchk_fscounters
,
359 .repair
= xrep_notsupported
,
363 /* This isn't a stable feature, warn once per day. */
365 xchk_experimental_warning(
366 struct xfs_mount
*mp
)
368 static struct ratelimit_state scrub_warning
= RATELIMIT_STATE_INIT(
369 "xchk_warning", 86400 * HZ
, 1);
370 ratelimit_set_flags(&scrub_warning
, RATELIMIT_MSG_ON_RELEASE
);
372 if (__ratelimit(&scrub_warning
))
374 "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
378 xchk_validate_inputs(
379 struct xfs_mount
*mp
,
380 struct xfs_scrub_metadata
*sm
)
383 const struct xchk_meta_ops
*ops
;
386 /* Check our inputs. */
387 sm
->sm_flags
&= ~XFS_SCRUB_FLAGS_OUT
;
388 if (sm
->sm_flags
& ~XFS_SCRUB_FLAGS_IN
)
390 /* sm_reserved[] must be zero */
391 if (memchr_inv(sm
->sm_reserved
, 0, sizeof(sm
->sm_reserved
)))
395 /* Do we know about this type of metadata? */
396 if (sm
->sm_type
>= XFS_SCRUB_TYPE_NR
)
398 ops
= &meta_scrub_ops
[sm
->sm_type
];
399 if (ops
->setup
== NULL
|| ops
->scrub
== NULL
)
401 /* Does this fs even support this type of metadata? */
402 if (ops
->has
&& !ops
->has(&mp
->m_sb
))
406 /* restricting fields must be appropriate for type */
410 if (sm
->sm_ino
|| sm
->sm_gen
|| sm
->sm_agno
)
414 if (sm
->sm_ino
|| sm
->sm_gen
||
415 sm
->sm_agno
>= mp
->m_sb
.sb_agcount
)
419 if (sm
->sm_agno
|| (sm
->sm_gen
&& !sm
->sm_ino
))
427 * We only want to repair read-write v5+ filesystems. Defer the check
428 * for ops->repair until after our scrub confirms that we need to
429 * perform repairs so that we avoid failing due to not supporting
430 * repairing an object that doesn't need repairs.
432 if (sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) {
434 if (!xfs_sb_version_hascrc(&mp
->m_sb
))
438 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
447 #ifdef CONFIG_XFS_ONLINE_REPAIR
448 static inline void xchk_postmortem(struct xfs_scrub
*sc
)
451 * Userspace asked us to repair something, we repaired it, rescanned
452 * it, and the rescan says it's still broken. Scream about this in
455 if ((sc
->sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) &&
456 (sc
->sm
->sm_flags
& (XFS_SCRUB_OFLAG_CORRUPT
|
457 XFS_SCRUB_OFLAG_XCORRUPT
)))
458 xrep_failure(sc
->mp
);
461 static inline void xchk_postmortem(struct xfs_scrub
*sc
)
464 * Userspace asked us to scrub something, it's broken, and we have no
465 * way of fixing it. Scream in the logs.
467 if (sc
->sm
->sm_flags
& (XFS_SCRUB_OFLAG_CORRUPT
|
468 XFS_SCRUB_OFLAG_XCORRUPT
))
469 xfs_alert_ratelimited(sc
->mp
,
470 "Corruption detected during scrub.");
472 #endif /* CONFIG_XFS_ONLINE_REPAIR */
474 /* Dispatch metadata scrubbing. */
477 struct xfs_inode
*ip
,
478 struct xfs_scrub_metadata
*sm
)
480 struct xfs_scrub sc
= {
484 .agno
= NULLAGNUMBER
,
487 struct xfs_mount
*mp
= ip
->i_mount
;
490 BUILD_BUG_ON(sizeof(meta_scrub_ops
) !=
491 (sizeof(struct xchk_meta_ops
) * XFS_SCRUB_TYPE_NR
));
493 trace_xchk_start(ip
, sm
, error
);
495 /* Forbidden if we are shut down or mounted norecovery. */
497 if (XFS_FORCED_SHUTDOWN(mp
))
499 error
= -ENOTRECOVERABLE
;
500 if (mp
->m_flags
& XFS_MOUNT_NORECOVERY
)
503 error
= xchk_validate_inputs(mp
, sm
);
507 xchk_experimental_warning(mp
);
509 sc
.ops
= &meta_scrub_ops
[sm
->sm_type
];
510 sc
.sick_mask
= xchk_health_mask_for_scrub_type(sm
->sm_type
);
512 /* Set up for the operation. */
513 error
= sc
.ops
->setup(&sc
, ip
);
517 /* Scrub for errors. */
518 error
= sc
.ops
->scrub(&sc
);
519 if (!(sc
.flags
& XCHK_TRY_HARDER
) && error
== -EDEADLOCK
) {
521 * Scrubbers return -EDEADLOCK to mean 'try harder'.
522 * Tear down everything we hold, then set up again with
523 * preparation for worst-case scenarios.
525 error
= xchk_teardown(&sc
, ip
, 0);
528 sc
.flags
|= XCHK_TRY_HARDER
;
533 xchk_update_health(&sc
);
535 if ((sc
.sm
->sm_flags
& XFS_SCRUB_IFLAG_REPAIR
) &&
536 !(sc
.flags
& XREP_ALREADY_FIXED
)) {
539 /* Let debug users force us into the repair routines. */
540 if (XFS_TEST_ERROR(false, mp
, XFS_ERRTAG_FORCE_SCRUB_REPAIR
))
541 sc
.sm
->sm_flags
|= XFS_SCRUB_OFLAG_CORRUPT
;
543 needs_fix
= (sc
.sm
->sm_flags
& (XFS_SCRUB_OFLAG_CORRUPT
|
544 XFS_SCRUB_OFLAG_XCORRUPT
|
545 XFS_SCRUB_OFLAG_PREEN
));
547 * If userspace asked for a repair but it wasn't necessary,
548 * report that back to userspace.
551 sc
.sm
->sm_flags
|= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED
;
556 * If it's broken, userspace wants us to fix it, and we haven't
557 * already tried to fix it, then attempt a repair.
559 error
= xrep_attempt(ip
, &sc
);
560 if (error
== -EAGAIN
) {
562 * Either the repair function succeeded or it couldn't
563 * get all the resources it needs; either way, we go
564 * back to the beginning and call the scrub function.
566 error
= xchk_teardown(&sc
, ip
, 0);
576 xchk_postmortem(&sc
);
578 error
= xchk_teardown(&sc
, ip
, error
);
580 trace_xchk_done(ip
, sm
, error
);
581 if (error
== -EFSCORRUPTED
|| error
== -EFSBADCRC
) {
582 sm
->sm_flags
|= XFS_SCRUB_OFLAG_CORRUPT
;