xref: /linux/fs/xfs/scrub/scrub.c (revision bbeb1219eeeeab7ef302fdaedee71b08e413a04c)
1739a2fe0SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
236fd6e86SDarrick J. Wong /*
3ecc73f8aSDarrick J. Wong  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4739a2fe0SDarrick J. Wong  * Author: Darrick J. Wong <djwong@kernel.org>
536fd6e86SDarrick J. Wong  */
636fd6e86SDarrick J. Wong #include "xfs.h"
736fd6e86SDarrick J. Wong #include "xfs_fs.h"
836fd6e86SDarrick J. Wong #include "xfs_shared.h"
936fd6e86SDarrick J. Wong #include "xfs_format.h"
1036fd6e86SDarrick J. Wong #include "xfs_trans_resv.h"
1136fd6e86SDarrick J. Wong #include "xfs_mount.h"
1236fd6e86SDarrick J. Wong #include "xfs_log_format.h"
1336fd6e86SDarrick J. Wong #include "xfs_trans.h"
1436fd6e86SDarrick J. Wong #include "xfs_inode.h"
15eb41c93fSDarrick J. Wong #include "xfs_quota.h"
16eb41c93fSDarrick J. Wong #include "xfs_qm.h"
175f213ddbSDarrick J. Wong #include "xfs_scrub.h"
18a095686aSDarrick J. Wong #include "xfs_buf_mem.h"
197e1b84b2SDarrick J. Wong #include "xfs_rmap.h"
2084c14ee3SDarrick J. Wong #include "xfs_exchrange.h"
21abf039e2SDarrick J. Wong #include "xfs_exchmaps.h"
227be3d20bSDarrick J. Wong #include "xfs_dir2.h"
237be3d20bSDarrick J. Wong #include "xfs_parent.h"
24c77b3758SDarrick J. Wong #include "xfs_icache.h"
2536fd6e86SDarrick J. Wong #include "scrub/scrub.h"
26dcb660f9SDarrick J. Wong #include "scrub/common.h"
2736fd6e86SDarrick J. Wong #include "scrub/trace.h"
2884d42ea6SDarrick J. Wong #include "scrub/repair.h"
294860a05dSDarrick J. Wong #include "scrub/health.h"
30d7a74cadSDarrick J. Wong #include "scrub/stats.h"
31526aab5fSDarrick J. Wong #include "scrub/xfile.h"
3284c14ee3SDarrick J. Wong #include "scrub/tempfile.h"
331e58a8ccSDarrick J. Wong #include "scrub/orphanage.h"
3436fd6e86SDarrick J. Wong 
35a5637186SDarrick J. Wong /*
36a5637186SDarrick J. Wong  * Online Scrub and Repair
37a5637186SDarrick J. Wong  *
38a5637186SDarrick J. Wong  * Traditionally, XFS (the kernel driver) did not know how to check or
39a5637186SDarrick J. Wong  * repair on-disk data structures.  That task was left to the xfs_check
40a5637186SDarrick J. Wong  * and xfs_repair tools, both of which require taking the filesystem
41a5637186SDarrick J. Wong  * offline for a thorough but time consuming examination.  Online
42a5637186SDarrick J. Wong  * scrub & repair, on the other hand, enables us to check the metadata
43a5637186SDarrick J. Wong  * for obvious errors while carefully stepping around the filesystem's
44a5637186SDarrick J. Wong  * ongoing operations, locking rules, etc.
45a5637186SDarrick J. Wong  *
46a5637186SDarrick J. Wong  * Given that most XFS metadata consist of records stored in a btree,
47a5637186SDarrick J. Wong  * most of the checking functions iterate the btree blocks themselves
48a5637186SDarrick J. Wong  * looking for irregularities.  When a record block is encountered, each
49a5637186SDarrick J. Wong  * record can be checked for obviously bad values.  Record values can
50a5637186SDarrick J. Wong  * also be cross-referenced against other btrees to look for potential
51a5637186SDarrick J. Wong  * misunderstandings between pieces of metadata.
52a5637186SDarrick J. Wong  *
53a5637186SDarrick J. Wong  * It is expected that the checkers responsible for per-AG metadata
54a5637186SDarrick J. Wong  * structures will lock the AG headers (AGI, AGF, AGFL), iterate the
55a5637186SDarrick J. Wong  * metadata structure, and perform any relevant cross-referencing before
56a5637186SDarrick J. Wong  * unlocking the AG and returning the results to userspace.  These
57a5637186SDarrick J. Wong  * scrubbers must not keep an AG locked for too long to avoid tying up
58a5637186SDarrick J. Wong  * the block and inode allocators.
59a5637186SDarrick J. Wong  *
60a5637186SDarrick J. Wong  * Block maps and b-trees rooted in an inode present a special challenge
61a5637186SDarrick J. Wong  * because they can involve extents from any AG.  The general scrubber
62a5637186SDarrick J. Wong  * structure of lock -> check -> xref -> unlock still holds, but AG
63a5637186SDarrick J. Wong  * locking order rules /must/ be obeyed to avoid deadlocks.  The
64a5637186SDarrick J. Wong  * ordering rule, of course, is that we must lock in increasing AG
65a5637186SDarrick J. Wong  * order.  Helper functions are provided to track which AG headers we've
66a5637186SDarrick J. Wong  * already locked.  If we detect an imminent locking order violation, we
67a5637186SDarrick J. Wong  * can signal a potential deadlock, in which case the scrubber can jump
68a5637186SDarrick J. Wong  * out to the top level, lock all the AGs in order, and retry the scrub.
69a5637186SDarrick J. Wong  *
70a5637186SDarrick J. Wong  * For file data (directories, extended attributes, symlinks) scrub, we
71a5637186SDarrick J. Wong  * can simply lock the inode and walk the data.  For btree data
72a5637186SDarrick J. Wong  * (directories and attributes) we follow the same btree-scrubbing
73a5637186SDarrick J. Wong  * strategy outlined previously to check the records.
74a5637186SDarrick J. Wong  *
75a5637186SDarrick J. Wong  * We use a bit of trickery with transactions to avoid buffer deadlocks
76a5637186SDarrick J. Wong  * if there is a cycle in the metadata.  The basic problem is that
77a5637186SDarrick J. Wong  * travelling down a btree involves locking the current buffer at each
78a5637186SDarrick J. Wong  * tree level.  If a pointer should somehow point back to a buffer that
79a5637186SDarrick J. Wong  * we've already examined, we will deadlock due to the second buffer
80a5637186SDarrick J. Wong  * locking attempt.  Note however that grabbing a buffer in transaction
81a5637186SDarrick J. Wong  * context links the locked buffer to the transaction.  If we try to
82a5637186SDarrick J. Wong  * re-grab the buffer in the context of the same transaction, we avoid
83a5637186SDarrick J. Wong  * the second lock attempt and continue.  Between the verifier and the
84a5637186SDarrick J. Wong  * scrubber, something will notice that something is amiss and report
85a5637186SDarrick J. Wong  * the corruption.  Therefore, each scrubber will allocate an empty
86a5637186SDarrick J. Wong  * transaction, attach buffers to it, and cancel the transaction at the
87a5637186SDarrick J. Wong  * end of the scrub run.  Cancelling a non-dirty transaction simply
88a5637186SDarrick J. Wong  * unlocks the buffers.
89a5637186SDarrick J. Wong  *
90a5637186SDarrick J. Wong  * There are four pieces of data that scrub can communicate to
91a5637186SDarrick J. Wong  * userspace.  The first is the error code (errno), which can be used to
92a5637186SDarrick J. Wong  * communicate operational errors in performing the scrub.  There are
93a5637186SDarrick J. Wong  * also three flags that can be set in the scrub context.  If the data
94a5637186SDarrick J. Wong  * structure itself is corrupt, the CORRUPT flag will be set.  If
95a5637186SDarrick J. Wong  * the metadata is correct but otherwise suboptimal, the PREEN flag
96a5637186SDarrick J. Wong  * will be set.
9764b12563SDarrick J. Wong  *
9864b12563SDarrick J. Wong  * We perform secondary validation of filesystem metadata by
9964b12563SDarrick J. Wong  * cross-referencing every record with all other available metadata.
10064b12563SDarrick J. Wong  * For example, for block mapping extents, we verify that there are no
10164b12563SDarrick J. Wong  * records in the free space and inode btrees corresponding to that
10264b12563SDarrick J. Wong  * space extent and that there is a corresponding entry in the reverse
10364b12563SDarrick J. Wong  * mapping btree.  Inconsistent metadata is noted by setting the
10464b12563SDarrick J. Wong  * XCORRUPT flag; btree query function errors are noted by setting the
10564b12563SDarrick J. Wong  * XFAIL flag and deleting the cursor to prevent further attempts to
10664b12563SDarrick J. Wong  * cross-reference with a defective btree.
10784d42ea6SDarrick J. Wong  *
10884d42ea6SDarrick J. Wong  * If a piece of metadata proves corrupt or suboptimal, the userspace
10984d42ea6SDarrick J. Wong  * program can ask the kernel to apply some tender loving care (TLC) to
11084d42ea6SDarrick J. Wong  * the metadata object by setting the REPAIR flag and re-calling the
11184d42ea6SDarrick J. Wong  * scrub ioctl.  "Corruption" is defined by metadata violating the
11284d42ea6SDarrick J. Wong  * on-disk specification; operations cannot continue if the violation is
11384d42ea6SDarrick J. Wong  * left untreated.  It is possible for XFS to continue if an object is
11484d42ea6SDarrick J. Wong  * "suboptimal", however performance may be degraded.  Repairs are
11584d42ea6SDarrick J. Wong  * usually performed by rebuilding the metadata entirely out of
11684d42ea6SDarrick J. Wong  * redundant metadata.  Optimizing, on the other hand, can sometimes be
11784d42ea6SDarrick J. Wong  * done without rebuilding entire structures.
11884d42ea6SDarrick J. Wong  *
11984d42ea6SDarrick J. Wong  * Generally speaking, the repair code has the following code structure:
12084d42ea6SDarrick J. Wong  * Lock -> scrub -> repair -> commit -> re-lock -> re-scrub -> unlock.
12184d42ea6SDarrick J. Wong  * The first check helps us figure out if we need to rebuild or simply
12284d42ea6SDarrick J. Wong  * optimize the structure so that the rebuild knows what to do.  The
12384d42ea6SDarrick J. Wong  * second check evaluates the completeness of the repair; that is what
12484d42ea6SDarrick J. Wong  * is reported to userspace.
125c517b3aaSDarrick J. Wong  *
126c517b3aaSDarrick J. Wong  * A quick note on symbol prefixes:
127c517b3aaSDarrick J. Wong  * - "xfs_" are general XFS symbols.
128c517b3aaSDarrick J. Wong  * - "xchk_" are symbols related to metadata checking.
129c517b3aaSDarrick J. Wong  * - "xrep_" are symbols related to metadata repair.
130c517b3aaSDarrick J. Wong  * - "xfs_scrub_" are symbols that tie online fsck to the rest of XFS.
131a5637186SDarrick J. Wong  */
132a5637186SDarrick J. Wong 
133dcb660f9SDarrick J. Wong /*
134dcb660f9SDarrick J. Wong  * Scrub probe -- userspace uses this to probe if we're willing to scrub
135dcb660f9SDarrick J. Wong  * or repair a given mountpoint.  This will be used by xfs_scrub to
136dcb660f9SDarrick J. Wong  * probe the kernel's abilities to scrub (and repair) the metadata.  We
137dcb660f9SDarrick J. Wong  * do this by validating the ioctl inputs from userspace, preparing the
138dcb660f9SDarrick J. Wong  * filesystem for a scrub (or a repair) operation, and immediately
139dcb660f9SDarrick J. Wong  * returning to userspace.  Userspace can use the returned errno and
140dcb660f9SDarrick J. Wong  * structure state to decide (in broad terms) if scrub/repair are
141dcb660f9SDarrick J. Wong  * supported by the running kernel.
142dcb660f9SDarrick J. Wong  */
14388aa5de4SChristoph Hellwig static int
xchk_probe(struct xfs_scrub * sc)144c517b3aaSDarrick J. Wong xchk_probe(
1451d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc)
146dcb660f9SDarrick J. Wong {
147dcb660f9SDarrick J. Wong 	int			error = 0;
148dcb660f9SDarrick J. Wong 
149c517b3aaSDarrick J. Wong 	if (xchk_should_terminate(sc, &error))
150dcb660f9SDarrick J. Wong 		return error;
151dcb660f9SDarrick J. Wong 
152dcb660f9SDarrick J. Wong 	return 0;
153dcb660f9SDarrick J. Wong }
154dcb660f9SDarrick J. Wong 
155a5637186SDarrick J. Wong /* Scrub setup and teardown */
156a5637186SDarrick J. Wong 
157466c525dSDarrick J. Wong static inline void
xchk_fsgates_disable(struct xfs_scrub * sc)158466c525dSDarrick J. Wong xchk_fsgates_disable(
159466c525dSDarrick J. Wong 	struct xfs_scrub	*sc)
160466c525dSDarrick J. Wong {
1616d335233SDarrick J. Wong 	if (!(sc->flags & XCHK_FSGATES_ALL))
162466c525dSDarrick J. Wong 		return;
163466c525dSDarrick J. Wong 
1646d335233SDarrick J. Wong 	trace_xchk_fsgates_disable(sc, sc->flags & XCHK_FSGATES_ALL);
165466c525dSDarrick J. Wong 
166466c525dSDarrick J. Wong 	if (sc->flags & XCHK_FSGATES_DRAIN)
167466c525dSDarrick J. Wong 		xfs_drain_wait_disable();
168466c525dSDarrick J. Wong 
16920049187SDarrick J. Wong 	if (sc->flags & XCHK_FSGATES_QUOTA)
17020049187SDarrick J. Wong 		xfs_dqtrx_hook_disable();
17120049187SDarrick J. Wong 
17286a1746eSDarrick J. Wong 	if (sc->flags & XCHK_FSGATES_DIRENTS)
17386a1746eSDarrick J. Wong 		xfs_dir_hook_disable();
17486a1746eSDarrick J. Wong 
1757e1b84b2SDarrick J. Wong 	if (sc->flags & XCHK_FSGATES_RMAP)
1767e1b84b2SDarrick J. Wong 		xfs_rmap_hook_disable();
1777e1b84b2SDarrick J. Wong 
1786d335233SDarrick J. Wong 	sc->flags &= ~XCHK_FSGATES_ALL;
179466c525dSDarrick J. Wong }
180466c525dSDarrick J. Wong 
1811a5f6e08SDarrick J. Wong /* Free the resources associated with a scrub subtype. */
1821a5f6e08SDarrick J. Wong void
xchk_scrub_free_subord(struct xfs_scrub_subord * sub)1831a5f6e08SDarrick J. Wong xchk_scrub_free_subord(
1841a5f6e08SDarrick J. Wong 	struct xfs_scrub_subord	*sub)
1851a5f6e08SDarrick J. Wong {
1861a5f6e08SDarrick J. Wong 	struct xfs_scrub	*sc = sub->parent_sc;
1871a5f6e08SDarrick J. Wong 
1881a5f6e08SDarrick J. Wong 	ASSERT(sc->ip == sub->sc.ip);
1891a5f6e08SDarrick J. Wong 	ASSERT(sc->orphanage == sub->sc.orphanage);
1901a5f6e08SDarrick J. Wong 	ASSERT(sc->tempip == sub->sc.tempip);
1911a5f6e08SDarrick J. Wong 
1921a5f6e08SDarrick J. Wong 	sc->sm->sm_type = sub->old_smtype;
1931a5f6e08SDarrick J. Wong 	sc->sm->sm_flags = sub->old_smflags |
1941a5f6e08SDarrick J. Wong 				(sc->sm->sm_flags & XFS_SCRUB_FLAGS_OUT);
1951a5f6e08SDarrick J. Wong 	sc->tp = sub->sc.tp;
1961a5f6e08SDarrick J. Wong 
1971a5f6e08SDarrick J. Wong 	if (sub->sc.buf) {
1981a5f6e08SDarrick J. Wong 		if (sub->sc.buf_cleanup)
1991a5f6e08SDarrick J. Wong 			sub->sc.buf_cleanup(sub->sc.buf);
2001a5f6e08SDarrick J. Wong 		kvfree(sub->sc.buf);
2011a5f6e08SDarrick J. Wong 	}
2021a5f6e08SDarrick J. Wong 	if (sub->sc.xmbtp)
2031a5f6e08SDarrick J. Wong 		xmbuf_free(sub->sc.xmbtp);
2041a5f6e08SDarrick J. Wong 	if (sub->sc.xfile)
2051a5f6e08SDarrick J. Wong 		xfile_destroy(sub->sc.xfile);
2061a5f6e08SDarrick J. Wong 
2071a5f6e08SDarrick J. Wong 	sc->ilock_flags = sub->sc.ilock_flags;
2081a5f6e08SDarrick J. Wong 	sc->orphanage_ilock_flags = sub->sc.orphanage_ilock_flags;
2091a5f6e08SDarrick J. Wong 	sc->temp_ilock_flags = sub->sc.temp_ilock_flags;
2101a5f6e08SDarrick J. Wong 
2111a5f6e08SDarrick J. Wong 	kfree(sub);
2121a5f6e08SDarrick J. Wong }
2131a5f6e08SDarrick J. Wong 
214a5637186SDarrick J. Wong /* Free all the resources and finish the transactions. */
215a5637186SDarrick J. Wong STATIC int
xchk_teardown(struct xfs_scrub * sc,int error)216c517b3aaSDarrick J. Wong xchk_teardown(
2171d8a748aSDarrick J. Wong 	struct xfs_scrub	*sc,
218a5637186SDarrick J. Wong 	int			error)
219a5637186SDarrick J. Wong {
220c517b3aaSDarrick J. Wong 	xchk_ag_free(sc, &sc->sa);
221a5637186SDarrick J. Wong 	if (sc->tp) {
22284d42ea6SDarrick J. Wong 		if (error == 0 && (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
22384d42ea6SDarrick J. Wong 			error = xfs_trans_commit(sc->tp);
22484d42ea6SDarrick J. Wong 		else
225a5637186SDarrick J. Wong 			xfs_trans_cancel(sc->tp);
226a5637186SDarrick J. Wong 		sc->tp = NULL;
227a5637186SDarrick J. Wong 	}
22880e4e126SDarrick J. Wong 	if (sc->ip) {
229638a7174SDarrick J. Wong 		if (sc->ilock_flags)
230294012fbSDarrick J. Wong 			xchk_iunlock(sc, sc->ilock_flags);
231a03297a0SDarrick J. Wong 		xchk_irele(sc, sc->ip);
23280e4e126SDarrick J. Wong 		sc->ip = NULL;
23380e4e126SDarrick J. Wong 	}
234ce85a1e0SDarrick J. Wong 	if (sc->flags & XCHK_HAVE_FREEZE_PROT) {
235ce85a1e0SDarrick J. Wong 		sc->flags &= ~XCHK_HAVE_FREEZE_PROT;
23671bddbccSDarrick J. Wong 		mnt_drop_write_file(sc->file);
237ce85a1e0SDarrick J. Wong 	}
238a095686aSDarrick J. Wong 	if (sc->xmbtp) {
239a095686aSDarrick J. Wong 		xmbuf_free(sc->xmbtp);
240a095686aSDarrick J. Wong 		sc->xmbtp = NULL;
241a095686aSDarrick J. Wong 	}
242526aab5fSDarrick J. Wong 	if (sc->xfile) {
243526aab5fSDarrick J. Wong 		xfile_destroy(sc->xfile);
244526aab5fSDarrick J. Wong 		sc->xfile = NULL;
245526aab5fSDarrick J. Wong 	}
246eec0482eSDarrick J. Wong 	if (sc->buf) {
24791781ff5SDarrick J. Wong 		if (sc->buf_cleanup)
24891781ff5SDarrick J. Wong 			sc->buf_cleanup(sc->buf);
249306195f3SDarrick J. Wong 		kvfree(sc->buf);
25091781ff5SDarrick J. Wong 		sc->buf_cleanup = NULL;
251eec0482eSDarrick J. Wong 		sc->buf = NULL;
252eec0482eSDarrick J. Wong 	}
253466c525dSDarrick J. Wong 
25484c14ee3SDarrick J. Wong 	xrep_tempfile_rele(sc);
2551e58a8ccSDarrick J. Wong 	xrep_orphanage_rele(sc);
256466c525dSDarrick J. Wong 	xchk_fsgates_disable(sc);
257a5637186SDarrick J. Wong 	return error;
258a5637186SDarrick J. Wong }
259a5637186SDarrick J. Wong 
260a5637186SDarrick J. Wong /* Scrubbing dispatch. */
261a5637186SDarrick J. Wong 
262c517b3aaSDarrick J. Wong static const struct xchk_meta_ops meta_scrub_ops[] = {
263bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_PROBE] = {	/* ioctl presence test */
2648e630837SEric Sandeen 		.type	= ST_NONE,
265c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_fs,
266c517b3aaSDarrick J. Wong 		.scrub	= xchk_probe,
267b5e2196eSDarrick J. Wong 		.repair = xrep_probe,
268dcb660f9SDarrick J. Wong 	},
269bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_SB] = {		/* superblock */
2708e630837SEric Sandeen 		.type	= ST_PERAG,
271466c525dSDarrick J. Wong 		.setup	= xchk_setup_agheader,
272c517b3aaSDarrick J. Wong 		.scrub	= xchk_superblock,
273b5e2196eSDarrick J. Wong 		.repair	= xrep_superblock,
27421fb4cb1SDarrick J. Wong 	},
275bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_AGF] = {	/* agf */
2768e630837SEric Sandeen 		.type	= ST_PERAG,
277466c525dSDarrick J. Wong 		.setup	= xchk_setup_agheader,
278c517b3aaSDarrick J. Wong 		.scrub	= xchk_agf,
279f9ed6debSDarrick J. Wong 		.repair	= xrep_agf,
280ab9d5dc5SDarrick J. Wong 	},
281bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_AGFL]= {	/* agfl */
2828e630837SEric Sandeen 		.type	= ST_PERAG,
283466c525dSDarrick J. Wong 		.setup	= xchk_setup_agheader,
284c517b3aaSDarrick J. Wong 		.scrub	= xchk_agfl,
2850e93d3f4SDarrick J. Wong 		.repair	= xrep_agfl,
286ab9d5dc5SDarrick J. Wong 	},
287bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_AGI] = {	/* agi */
2888e630837SEric Sandeen 		.type	= ST_PERAG,
289466c525dSDarrick J. Wong 		.setup	= xchk_setup_agheader,
290c517b3aaSDarrick J. Wong 		.scrub	= xchk_agi,
29113942aa9SDarrick J. Wong 		.repair	= xrep_agi,
292a12890aeSDarrick J. Wong 	},
293bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_BNOBT] = {	/* bnobt */
2948e630837SEric Sandeen 		.type	= ST_PERAG,
295c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_allocbt,
2968bd0bf57SDarrick J. Wong 		.scrub	= xchk_allocbt,
2974bdfd7d1SDarrick J. Wong 		.repair	= xrep_allocbt,
2984bdfd7d1SDarrick J. Wong 		.repair_eval = xrep_revalidate_allocbt,
299efa7a99cSDarrick J. Wong 	},
300bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_CNTBT] = {	/* cntbt */
3018e630837SEric Sandeen 		.type	= ST_PERAG,
302c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_allocbt,
3038bd0bf57SDarrick J. Wong 		.scrub	= xchk_allocbt,
3044bdfd7d1SDarrick J. Wong 		.repair	= xrep_allocbt,
3054bdfd7d1SDarrick J. Wong 		.repair_eval = xrep_revalidate_allocbt,
306efa7a99cSDarrick J. Wong 	},
307bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_INOBT] = {	/* inobt */
3088e630837SEric Sandeen 		.type	= ST_PERAG,
309c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_iallocbt,
3108bd0bf57SDarrick J. Wong 		.scrub	= xchk_iallocbt,
311dbfbf3bdSDarrick J. Wong 		.repair	= xrep_iallocbt,
312dbfbf3bdSDarrick J. Wong 		.repair_eval = xrep_revalidate_iallocbt,
3133daa6641SDarrick J. Wong 	},
314bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_FINOBT] = {	/* finobt */
3158e630837SEric Sandeen 		.type	= ST_PERAG,
316c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_iallocbt,
3178bd0bf57SDarrick J. Wong 		.scrub	= xchk_iallocbt,
31855fafb31SDave Chinner 		.has	= xfs_has_finobt,
319dbfbf3bdSDarrick J. Wong 		.repair	= xrep_iallocbt,
320dbfbf3bdSDarrick J. Wong 		.repair_eval = xrep_revalidate_iallocbt,
3213daa6641SDarrick J. Wong 	},
322bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_RMAPBT] = {	/* rmapbt */
3238e630837SEric Sandeen 		.type	= ST_PERAG,
324c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_rmapbt,
325c517b3aaSDarrick J. Wong 		.scrub	= xchk_rmapbt,
32655fafb31SDave Chinner 		.has	= xfs_has_rmapbt,
32732080a9bSDarrick J. Wong 		.repair	= xrep_rmapbt,
328c7e693d9SDarrick J. Wong 	},
329bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_REFCNTBT] = {	/* refcountbt */
3308e630837SEric Sandeen 		.type	= ST_PERAG,
331c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_ag_refcountbt,
332c517b3aaSDarrick J. Wong 		.scrub	= xchk_refcountbt,
33355fafb31SDave Chinner 		.has	= xfs_has_reflink,
3349099cd38SDarrick J. Wong 		.repair	= xrep_refcountbt,
335edc09b52SDarrick J. Wong 	},
336bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_INODE] = {	/* inode record */
3378e630837SEric Sandeen 		.type	= ST_INODE,
338c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_inode,
339c517b3aaSDarrick J. Wong 		.scrub	= xchk_inode,
3402d295fe6SDarrick J. Wong 		.repair	= xrep_inode,
34180e4e126SDarrick J. Wong 	},
342bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_BMBTD] = {	/* inode data fork */
3438e630837SEric Sandeen 		.type	= ST_INODE,
344c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_inode_bmap,
345c517b3aaSDarrick J. Wong 		.scrub	= xchk_bmap_data,
3468f71bedeSDarrick J. Wong 		.repair	= xrep_bmap_data,
34799d9d8d0SDarrick J. Wong 	},
348bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_BMBTA] = {	/* inode attr fork */
3498e630837SEric Sandeen 		.type	= ST_INODE,
350c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_inode_bmap,
351c517b3aaSDarrick J. Wong 		.scrub	= xchk_bmap_attr,
3528f71bedeSDarrick J. Wong 		.repair	= xrep_bmap_attr,
35399d9d8d0SDarrick J. Wong 	},
354bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_BMBTC] = {	/* inode CoW fork */
3558e630837SEric Sandeen 		.type	= ST_INODE,
356c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_inode_bmap,
357c517b3aaSDarrick J. Wong 		.scrub	= xchk_bmap_cow,
358dbbdbd00SDarrick J. Wong 		.repair	= xrep_bmap_cow,
35999d9d8d0SDarrick J. Wong 	},
360bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_DIR] = {	/* directory */
3618e630837SEric Sandeen 		.type	= ST_INODE,
362c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_directory,
363c517b3aaSDarrick J. Wong 		.scrub	= xchk_directory,
364b1991ee3SDarrick J. Wong 		.repair	= xrep_directory,
365a5c46e5eSDarrick J. Wong 	},
366bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_XATTR] = {	/* extended attributes */
3678e630837SEric Sandeen 		.type	= ST_INODE,
368c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_xattr,
369c517b3aaSDarrick J. Wong 		.scrub	= xchk_xattr,
370e47dcf11SDarrick J. Wong 		.repair	= xrep_xattr,
371eec0482eSDarrick J. Wong 	},
372bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_SYMLINK] = {	/* symbolic link */
3738e630837SEric Sandeen 		.type	= ST_INODE,
374c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_symlink,
375c517b3aaSDarrick J. Wong 		.scrub	= xchk_symlink,
3762651923dSDarrick J. Wong 		.repair	= xrep_symlink,
3772a721dbbSDarrick J. Wong 	},
378bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_PARENT] = {	/* parent pointers */
3798e630837SEric Sandeen 		.type	= ST_INODE,
380c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_parent,
381c517b3aaSDarrick J. Wong 		.scrub	= xchk_parent,
382cc22edabSDarrick J. Wong 		.repair	= xrep_parent,
3830f28b257SDarrick J. Wong 	},
384bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_RTBITMAP] = {	/* realtime bitmap */
3858e630837SEric Sandeen 		.type	= ST_FS,
386526aab5fSDarrick J. Wong 		.setup	= xchk_setup_rtbitmap,
387c517b3aaSDarrick J. Wong 		.scrub	= xchk_rtbitmap,
388ffd37b22SDarrick J. Wong 		.repair	= xrep_rtbitmap,
38929b0767bSDarrick J. Wong 	},
390bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_RTSUM] = {	/* realtime summary */
3918e630837SEric Sandeen 		.type	= ST_FS,
392526aab5fSDarrick J. Wong 		.setup	= xchk_setup_rtsummary,
393c517b3aaSDarrick J. Wong 		.scrub	= xchk_rtsummary,
394abf039e2SDarrick J. Wong 		.repair	= xrep_rtsummary,
39529b0767bSDarrick J. Wong 	},
396bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_UQUOTA] = {	/* user quota */
3978e630837SEric Sandeen 		.type	= ST_FS,
398c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_quota,
399c517b3aaSDarrick J. Wong 		.scrub	= xchk_quota,
400a5b91555SDarrick J. Wong 		.repair	= xrep_quota,
401c2fc338cSDarrick J. Wong 	},
402bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_GQUOTA] = {	/* group quota */
4038e630837SEric Sandeen 		.type	= ST_FS,
404c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_quota,
405c517b3aaSDarrick J. Wong 		.scrub	= xchk_quota,
406a5b91555SDarrick J. Wong 		.repair	= xrep_quota,
407c2fc338cSDarrick J. Wong 	},
408bfb3e9b9SEric Sandeen 	[XFS_SCRUB_TYPE_PQUOTA] = {	/* project quota */
4098e630837SEric Sandeen 		.type	= ST_FS,
410c517b3aaSDarrick J. Wong 		.setup	= xchk_setup_quota,
411c517b3aaSDarrick J. Wong 		.scrub	= xchk_quota,
412a5b91555SDarrick J. Wong 		.repair	= xrep_quota,
413c2fc338cSDarrick J. Wong 	},
41475efa57dSDarrick J. Wong 	[XFS_SCRUB_TYPE_FSCOUNTERS] = {	/* fs summary counters */
41575efa57dSDarrick J. Wong 		.type	= ST_FS,
41675efa57dSDarrick J. Wong 		.setup	= xchk_setup_fscounters,
41775efa57dSDarrick J. Wong 		.scrub	= xchk_fscounters,
4184ed080cdSDarrick J. Wong 		.repair	= xrep_fscounters,
41975efa57dSDarrick J. Wong 	},
42048dd9117SDarrick J. Wong 	[XFS_SCRUB_TYPE_QUOTACHECK] = {	/* quota counters */
42148dd9117SDarrick J. Wong 		.type	= ST_FS,
42248dd9117SDarrick J. Wong 		.setup	= xchk_setup_quotacheck,
42348dd9117SDarrick J. Wong 		.scrub	= xchk_quotacheck,
42496ed2ae4SDarrick J. Wong 		.repair	= xrep_quotacheck,
42548dd9117SDarrick J. Wong 	},
426f1184081SDarrick J. Wong 	[XFS_SCRUB_TYPE_NLINKS] = {	/* inode link counts */
427f1184081SDarrick J. Wong 		.type	= ST_FS,
428f1184081SDarrick J. Wong 		.setup	= xchk_setup_nlinks,
429f1184081SDarrick J. Wong 		.scrub	= xchk_nlinks,
4306b631c60SDarrick J. Wong 		.repair	= xrep_nlinks,
431f1184081SDarrick J. Wong 	},
432a1f3e0ccSDarrick J. Wong 	[XFS_SCRUB_TYPE_HEALTHY] = {	/* fs healthy; clean all reminders */
433a1f3e0ccSDarrick J. Wong 		.type	= ST_FS,
434a1f3e0ccSDarrick J. Wong 		.setup	= xchk_setup_fs,
435a1f3e0ccSDarrick J. Wong 		.scrub	= xchk_health_record,
436a1f3e0ccSDarrick J. Wong 		.repair = xrep_notsupported,
437a1f3e0ccSDarrick J. Wong 	},
438928b721aSDarrick J. Wong 	[XFS_SCRUB_TYPE_DIRTREE] = {	/* directory tree structure */
439928b721aSDarrick J. Wong 		.type	= ST_INODE,
440928b721aSDarrick J. Wong 		.setup	= xchk_setup_dirtree,
441928b721aSDarrick J. Wong 		.scrub	= xchk_dirtree,
442928b721aSDarrick J. Wong 		.has	= xfs_has_parent,
4433f31406aSDarrick J. Wong 		.repair	= xrep_dirtree,
444928b721aSDarrick J. Wong 	},
445a5637186SDarrick J. Wong };
446a5637186SDarrick J. Wong 
4470a085ddfSEric Sandeen static int
xchk_validate_inputs(struct xfs_mount * mp,struct xfs_scrub_metadata * sm)448c517b3aaSDarrick J. Wong xchk_validate_inputs(
4490a085ddfSEric Sandeen 	struct xfs_mount		*mp,
4500a085ddfSEric Sandeen 	struct xfs_scrub_metadata	*sm)
4510a085ddfSEric Sandeen {
4520a085ddfSEric Sandeen 	int				error;
453c517b3aaSDarrick J. Wong 	const struct xchk_meta_ops	*ops;
4540a085ddfSEric Sandeen 
4550a085ddfSEric Sandeen 	error = -EINVAL;
4560a085ddfSEric Sandeen 	/* Check our inputs. */
4570a085ddfSEric Sandeen 	sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
4580a085ddfSEric Sandeen 	if (sm->sm_flags & ~XFS_SCRUB_FLAGS_IN)
4590a085ddfSEric Sandeen 		goto out;
4608e630837SEric Sandeen 	/* sm_reserved[] must be zero */
4610a085ddfSEric Sandeen 	if (memchr_inv(sm->sm_reserved, 0, sizeof(sm->sm_reserved)))
4620a085ddfSEric Sandeen 		goto out;
4630a085ddfSEric Sandeen 
4640a085ddfSEric Sandeen 	error = -ENOENT;
4650a085ddfSEric Sandeen 	/* Do we know about this type of metadata? */
4660a085ddfSEric Sandeen 	if (sm->sm_type >= XFS_SCRUB_TYPE_NR)
4670a085ddfSEric Sandeen 		goto out;
4680a085ddfSEric Sandeen 	ops = &meta_scrub_ops[sm->sm_type];
4690a085ddfSEric Sandeen 	if (ops->setup == NULL || ops->scrub == NULL)
4700a085ddfSEric Sandeen 		goto out;
4710a085ddfSEric Sandeen 	/* Does this fs even support this type of metadata? */
47255fafb31SDave Chinner 	if (ops->has && !ops->has(mp))
4730a085ddfSEric Sandeen 		goto out;
4740a085ddfSEric Sandeen 
4758e630837SEric Sandeen 	error = -EINVAL;
4768e630837SEric Sandeen 	/* restricting fields must be appropriate for type */
4778e630837SEric Sandeen 	switch (ops->type) {
4788e630837SEric Sandeen 	case ST_NONE:
4798e630837SEric Sandeen 	case ST_FS:
4808e630837SEric Sandeen 		if (sm->sm_ino || sm->sm_gen || sm->sm_agno)
4818e630837SEric Sandeen 			goto out;
4828e630837SEric Sandeen 		break;
4838e630837SEric Sandeen 	case ST_PERAG:
4848e630837SEric Sandeen 		if (sm->sm_ino || sm->sm_gen ||
4858e630837SEric Sandeen 		    sm->sm_agno >= mp->m_sb.sb_agcount)
4868e630837SEric Sandeen 			goto out;
4878e630837SEric Sandeen 		break;
4888e630837SEric Sandeen 	case ST_INODE:
4898e630837SEric Sandeen 		if (sm->sm_agno || (sm->sm_gen && !sm->sm_ino))
4908e630837SEric Sandeen 			goto out;
4918e630837SEric Sandeen 		break;
4928e630837SEric Sandeen 	default:
4938e630837SEric Sandeen 		goto out;
4948e630837SEric Sandeen 	}
4958e630837SEric Sandeen 
4965c83df2eSDarrick J. Wong 	/* No rebuild without repair. */
4975c83df2eSDarrick J. Wong 	if ((sm->sm_flags & XFS_SCRUB_IFLAG_FORCE_REBUILD) &&
4985c83df2eSDarrick J. Wong 	    !(sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR))
4995c83df2eSDarrick J. Wong 		return -EINVAL;
5005c83df2eSDarrick J. Wong 
50184d42ea6SDarrick J. Wong 	/*
50284d42ea6SDarrick J. Wong 	 * We only want to repair read-write v5+ filesystems.  Defer the check
50384d42ea6SDarrick J. Wong 	 * for ops->repair until after our scrub confirms that we need to
50484d42ea6SDarrick J. Wong 	 * perform repairs so that we avoid failing due to not supporting
50584d42ea6SDarrick J. Wong 	 * repairing an object that doesn't need repairs.
50684d42ea6SDarrick J. Wong 	 */
50784d42ea6SDarrick J. Wong 	if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
50884d42ea6SDarrick J. Wong 		error = -EOPNOTSUPP;
50938c26bfdSDave Chinner 		if (!xfs_has_crc(mp))
5100a085ddfSEric Sandeen 			goto out;
5110a085ddfSEric Sandeen 
51284d42ea6SDarrick J. Wong 		error = -EROFS;
5132e973b2cSDave Chinner 		if (xfs_is_readonly(mp))
51484d42ea6SDarrick J. Wong 			goto out;
51584d42ea6SDarrick J. Wong 	}
51684d42ea6SDarrick J. Wong 
5170a085ddfSEric Sandeen 	error = 0;
5180a085ddfSEric Sandeen out:
5190a085ddfSEric Sandeen 	return error;
5200a085ddfSEric Sandeen }
5210a085ddfSEric Sandeen 
52284d42ea6SDarrick J. Wong #ifdef CONFIG_XFS_ONLINE_REPAIR
xchk_postmortem(struct xfs_scrub * sc)5231d8a748aSDarrick J. Wong static inline void xchk_postmortem(struct xfs_scrub *sc)
52484d42ea6SDarrick J. Wong {
52584d42ea6SDarrick J. Wong 	/*
52684d42ea6SDarrick J. Wong 	 * Userspace asked us to repair something, we repaired it, rescanned
52784d42ea6SDarrick J. Wong 	 * it, and the rescan says it's still broken.  Scream about this in
52884d42ea6SDarrick J. Wong 	 * the system logs.
52984d42ea6SDarrick J. Wong 	 */
53084d42ea6SDarrick J. Wong 	if ((sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
53184d42ea6SDarrick J. Wong 	    (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
53284d42ea6SDarrick J. Wong 				 XFS_SCRUB_OFLAG_XCORRUPT)))
533b5e2196eSDarrick J. Wong 		xrep_failure(sc->mp);
53484d42ea6SDarrick J. Wong }
53584d42ea6SDarrick J. Wong #else
xchk_postmortem(struct xfs_scrub * sc)5361d8a748aSDarrick J. Wong static inline void xchk_postmortem(struct xfs_scrub *sc)
53784d42ea6SDarrick J. Wong {
53884d42ea6SDarrick J. Wong 	/*
53984d42ea6SDarrick J. Wong 	 * Userspace asked us to scrub something, it's broken, and we have no
54084d42ea6SDarrick J. Wong 	 * way of fixing it.  Scream in the logs.
54184d42ea6SDarrick J. Wong 	 */
54284d42ea6SDarrick J. Wong 	if (sc->sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
54384d42ea6SDarrick J. Wong 				XFS_SCRUB_OFLAG_XCORRUPT))
54484d42ea6SDarrick J. Wong 		xfs_alert_ratelimited(sc->mp,
54584d42ea6SDarrick J. Wong 				"Corruption detected during scrub.");
54684d42ea6SDarrick J. Wong }
54784d42ea6SDarrick J. Wong #endif /* CONFIG_XFS_ONLINE_REPAIR */
54884d42ea6SDarrick J. Wong 
5491a5f6e08SDarrick J. Wong /*
5501a5f6e08SDarrick J. Wong  * Create a new scrub context from an existing one, but with a different scrub
5511a5f6e08SDarrick J. Wong  * type.
5521a5f6e08SDarrick J. Wong  */
5531a5f6e08SDarrick J. Wong struct xfs_scrub_subord *
xchk_scrub_create_subord(struct xfs_scrub * sc,unsigned int subtype)5541a5f6e08SDarrick J. Wong xchk_scrub_create_subord(
5551a5f6e08SDarrick J. Wong 	struct xfs_scrub	*sc,
5561a5f6e08SDarrick J. Wong 	unsigned int		subtype)
5571a5f6e08SDarrick J. Wong {
5581a5f6e08SDarrick J. Wong 	struct xfs_scrub_subord	*sub;
5591a5f6e08SDarrick J. Wong 
5601a5f6e08SDarrick J. Wong 	sub = kzalloc(sizeof(*sub), XCHK_GFP_FLAGS);
5611a5f6e08SDarrick J. Wong 	if (!sub)
5621a5f6e08SDarrick J. Wong 		return ERR_PTR(-ENOMEM);
5631a5f6e08SDarrick J. Wong 
5641a5f6e08SDarrick J. Wong 	sub->old_smtype = sc->sm->sm_type;
5651a5f6e08SDarrick J. Wong 	sub->old_smflags = sc->sm->sm_flags;
5661a5f6e08SDarrick J. Wong 	sub->parent_sc = sc;
5671a5f6e08SDarrick J. Wong 	memcpy(&sub->sc, sc, sizeof(struct xfs_scrub));
5681a5f6e08SDarrick J. Wong 	sub->sc.ops = &meta_scrub_ops[subtype];
5691a5f6e08SDarrick J. Wong 	sub->sc.sm->sm_type = subtype;
5701a5f6e08SDarrick J. Wong 	sub->sc.sm->sm_flags &= ~XFS_SCRUB_FLAGS_OUT;
5711a5f6e08SDarrick J. Wong 	sub->sc.buf = NULL;
5721a5f6e08SDarrick J. Wong 	sub->sc.buf_cleanup = NULL;
5731a5f6e08SDarrick J. Wong 	sub->sc.xfile = NULL;
5741a5f6e08SDarrick J. Wong 	sub->sc.xmbtp = NULL;
5751a5f6e08SDarrick J. Wong 
5761a5f6e08SDarrick J. Wong 	return sub;
5771a5f6e08SDarrick J. Wong }
5781a5f6e08SDarrick J. Wong 
57936fd6e86SDarrick J. Wong /* Dispatch metadata scrubbing. */
580be7cf174SDarrick J. Wong STATIC int
xfs_scrub_metadata(struct file * file,struct xfs_scrub_metadata * sm)58136fd6e86SDarrick J. Wong xfs_scrub_metadata(
58271bddbccSDarrick J. Wong 	struct file			*file,
58336fd6e86SDarrick J. Wong 	struct xfs_scrub_metadata	*sm)
58436fd6e86SDarrick J. Wong {
585d7a74cadSDarrick J. Wong 	struct xchk_stats_run		run = { };
586510a28e1SDarrick J. Wong 	struct xfs_scrub		*sc;
587026f57ebSDarrick J. Wong 	struct xfs_mount		*mp = XFS_I(file_inode(file))->i_mount;
588d7a74cadSDarrick J. Wong 	u64				check_start;
589a5637186SDarrick J. Wong 	int				error = 0;
590a5637186SDarrick J. Wong 
591bfb3e9b9SEric Sandeen 	BUILD_BUG_ON(sizeof(meta_scrub_ops) !=
592c517b3aaSDarrick J. Wong 		(sizeof(struct xchk_meta_ops) * XFS_SCRUB_TYPE_NR));
593bfb3e9b9SEric Sandeen 
594026f57ebSDarrick J. Wong 	trace_xchk_start(XFS_I(file_inode(file)), sm, error);
595a5637186SDarrick J. Wong 
596a5637186SDarrick J. Wong 	/* Forbidden if we are shut down or mounted norecovery. */
597a5637186SDarrick J. Wong 	error = -ESHUTDOWN;
59875c8c50fSDave Chinner 	if (xfs_is_shutdown(mp))
599a5637186SDarrick J. Wong 		goto out;
600a5637186SDarrick J. Wong 	error = -ENOTRECOVERABLE;
6010560f31aSDave Chinner 	if (xfs_has_norecovery(mp))
602a5637186SDarrick J. Wong 		goto out;
603a5637186SDarrick J. Wong 
604c517b3aaSDarrick J. Wong 	error = xchk_validate_inputs(mp, sm);
6050a085ddfSEric Sandeen 	if (error)
606a5637186SDarrick J. Wong 		goto out;
607a5637186SDarrick J. Wong 
608df5660cfSDarrick J. Wong 	xfs_warn_mount(mp, XFS_OPSTATE_WARNED_SCRUB,
609df5660cfSDarrick J. Wong  "EXPERIMENTAL online scrub feature in use. Use at your own risk!");
610a5637186SDarrick J. Wong 
611306195f3SDarrick J. Wong 	sc = kzalloc(sizeof(struct xfs_scrub), XCHK_GFP_FLAGS);
612510a28e1SDarrick J. Wong 	if (!sc) {
613510a28e1SDarrick J. Wong 		error = -ENOMEM;
614510a28e1SDarrick J. Wong 		goto out;
615510a28e1SDarrick J. Wong 	}
616510a28e1SDarrick J. Wong 
617510a28e1SDarrick J. Wong 	sc->mp = mp;
618510a28e1SDarrick J. Wong 	sc->file = file;
619510a28e1SDarrick J. Wong 	sc->sm = sm;
620510a28e1SDarrick J. Wong 	sc->ops = &meta_scrub_ops[sm->sm_type];
621510a28e1SDarrick J. Wong 	sc->sick_mask = xchk_health_mask_for_scrub_type(sm->sm_type);
622271557deSDarrick J. Wong 	sc->relax = INIT_XCHK_RELAX;
623a5637186SDarrick J. Wong retry_op:
62427fb5a72SDarrick J. Wong 	/*
62571bddbccSDarrick J. Wong 	 * When repairs are allowed, prevent freezing or readonly remount while
62671bddbccSDarrick J. Wong 	 * scrub is running with a real transaction.
62727fb5a72SDarrick J. Wong 	 */
62871bddbccSDarrick J. Wong 	if (sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) {
629510a28e1SDarrick J. Wong 		error = mnt_want_write_file(sc->file);
63071bddbccSDarrick J. Wong 		if (error)
631510a28e1SDarrick J. Wong 			goto out_sc;
632ce85a1e0SDarrick J. Wong 
633ce85a1e0SDarrick J. Wong 		sc->flags |= XCHK_HAVE_FREEZE_PROT;
63471bddbccSDarrick J. Wong 	}
63527fb5a72SDarrick J. Wong 
636a5637186SDarrick J. Wong 	/* Set up for the operation. */
637510a28e1SDarrick J. Wong 	error = sc->ops->setup(sc);
6383f64c718SDarrick J. Wong 	if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
6393f64c718SDarrick J. Wong 		goto try_harder;
64088accf17SDarrick J. Wong 	if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
64188accf17SDarrick J. Wong 		goto need_drain;
642a5637186SDarrick J. Wong 	if (error)
643a5637186SDarrick J. Wong 		goto out_teardown;
644a5637186SDarrick J. Wong 
645a5637186SDarrick J. Wong 	/* Scrub for errors. */
646d7a74cadSDarrick J. Wong 	check_start = xchk_stats_now();
6474bdfd7d1SDarrick J. Wong 	if ((sc->flags & XREP_ALREADY_FIXED) && sc->ops->repair_eval != NULL)
6484bdfd7d1SDarrick J. Wong 		error = sc->ops->repair_eval(sc);
6494bdfd7d1SDarrick J. Wong 	else
650510a28e1SDarrick J. Wong 		error = sc->ops->scrub(sc);
651d7a74cadSDarrick J. Wong 	run.scrub_ns += xchk_stats_elapsed_ns(check_start);
6523f64c718SDarrick J. Wong 	if (error == -EDEADLOCK && !(sc->flags & XCHK_TRY_HARDER))
6533f64c718SDarrick J. Wong 		goto try_harder;
65488accf17SDarrick J. Wong 	if (error == -ECHRNG && !(sc->flags & XCHK_NEED_DRAIN))
65588accf17SDarrick J. Wong 		goto need_drain;
6563f64c718SDarrick J. Wong 	if (error || (sm->sm_flags & XFS_SCRUB_OFLAG_INCOMPLETE))
657a5637186SDarrick J. Wong 		goto out_teardown;
658a5637186SDarrick J. Wong 
659510a28e1SDarrick J. Wong 	xchk_update_health(sc);
6604860a05dSDarrick J. Wong 
6614bdfd7d1SDarrick J. Wong 	if (xchk_could_repair(sc)) {
66284d42ea6SDarrick J. Wong 		/*
66384d42ea6SDarrick J. Wong 		 * If userspace asked for a repair but it wasn't necessary,
66484d42ea6SDarrick J. Wong 		 * report that back to userspace.
66584d42ea6SDarrick J. Wong 		 */
66648a72f60SDarrick J. Wong 		if (!xrep_will_attempt(sc)) {
667510a28e1SDarrick J. Wong 			sc->sm->sm_flags |= XFS_SCRUB_OFLAG_NO_REPAIR_NEEDED;
66884d42ea6SDarrick J. Wong 			goto out_nofix;
66984d42ea6SDarrick J. Wong 		}
67084d42ea6SDarrick J. Wong 
67184d42ea6SDarrick J. Wong 		/*
67284d42ea6SDarrick J. Wong 		 * If it's broken, userspace wants us to fix it, and we haven't
67384d42ea6SDarrick J. Wong 		 * already tried to fix it, then attempt a repair.
67484d42ea6SDarrick J. Wong 		 */
675d7a74cadSDarrick J. Wong 		error = xrep_attempt(sc, &run);
67684d42ea6SDarrick J. Wong 		if (error == -EAGAIN) {
6779d71e155SDarrick J. Wong 			/*
6789d71e155SDarrick J. Wong 			 * Either the repair function succeeded or it couldn't
6799d71e155SDarrick J. Wong 			 * get all the resources it needs; either way, we go
6809d71e155SDarrick J. Wong 			 * back to the beginning and call the scrub function.
6819d71e155SDarrick J. Wong 			 */
682510a28e1SDarrick J. Wong 			error = xchk_teardown(sc, 0);
68384d42ea6SDarrick J. Wong 			if (error) {
684b5e2196eSDarrick J. Wong 				xrep_failure(mp);
685510a28e1SDarrick J. Wong 				goto out_sc;
68684d42ea6SDarrick J. Wong 			}
68784d42ea6SDarrick J. Wong 			goto retry_op;
68884d42ea6SDarrick J. Wong 		}
68984d42ea6SDarrick J. Wong 	}
69084d42ea6SDarrick J. Wong 
69184d42ea6SDarrick J. Wong out_nofix:
692510a28e1SDarrick J. Wong 	xchk_postmortem(sc);
693a5637186SDarrick J. Wong out_teardown:
694510a28e1SDarrick J. Wong 	error = xchk_teardown(sc, error);
695510a28e1SDarrick J. Wong out_sc:
696e0319282SDarrick J. Wong 	if (error != -ENOENT)
697e0319282SDarrick J. Wong 		xchk_stats_merge(mp, sm, &run);
698306195f3SDarrick J. Wong 	kfree(sc);
699a5637186SDarrick J. Wong out:
700026f57ebSDarrick J. Wong 	trace_xchk_done(XFS_I(file_inode(file)), sm, error);
701a5637186SDarrick J. Wong 	if (error == -EFSCORRUPTED || error == -EFSBADCRC) {
702a5637186SDarrick J. Wong 		sm->sm_flags |= XFS_SCRUB_OFLAG_CORRUPT;
703a5637186SDarrick J. Wong 		error = 0;
704a5637186SDarrick J. Wong 	}
705a5637186SDarrick J. Wong 	return error;
70688accf17SDarrick J. Wong need_drain:
70788accf17SDarrick J. Wong 	error = xchk_teardown(sc, 0);
70888accf17SDarrick J. Wong 	if (error)
70988accf17SDarrick J. Wong 		goto out_sc;
71088accf17SDarrick J. Wong 	sc->flags |= XCHK_NEED_DRAIN;
711d7a74cadSDarrick J. Wong 	run.retries++;
71288accf17SDarrick J. Wong 	goto retry_op;
7133f64c718SDarrick J. Wong try_harder:
7143f64c718SDarrick J. Wong 	/*
7153f64c718SDarrick J. Wong 	 * Scrubbers return -EDEADLOCK to mean 'try harder'.  Tear down
7163f64c718SDarrick J. Wong 	 * everything we hold, then set up again with preparation for
7173f64c718SDarrick J. Wong 	 * worst-case scenarios.
7183f64c718SDarrick J. Wong 	 */
7193f64c718SDarrick J. Wong 	error = xchk_teardown(sc, 0);
7203f64c718SDarrick J. Wong 	if (error)
7213f64c718SDarrick J. Wong 		goto out_sc;
7223f64c718SDarrick J. Wong 	sc->flags |= XCHK_TRY_HARDER;
723d7a74cadSDarrick J. Wong 	run.retries++;
7243f64c718SDarrick J. Wong 	goto retry_op;
72536fd6e86SDarrick J. Wong }
726be7cf174SDarrick J. Wong 
727be7cf174SDarrick J. Wong /* Scrub one aspect of one piece of metadata. */
728be7cf174SDarrick J. Wong int
xfs_ioc_scrub_metadata(struct file * file,void __user * arg)729be7cf174SDarrick J. Wong xfs_ioc_scrub_metadata(
730be7cf174SDarrick J. Wong 	struct file			*file,
731be7cf174SDarrick J. Wong 	void				__user *arg)
732be7cf174SDarrick J. Wong {
733be7cf174SDarrick J. Wong 	struct xfs_scrub_metadata	scrub;
734be7cf174SDarrick J. Wong 	int				error;
735be7cf174SDarrick J. Wong 
736be7cf174SDarrick J. Wong 	if (!capable(CAP_SYS_ADMIN))
737be7cf174SDarrick J. Wong 		return -EPERM;
738be7cf174SDarrick J. Wong 
739be7cf174SDarrick J. Wong 	if (copy_from_user(&scrub, arg, sizeof(scrub)))
740be7cf174SDarrick J. Wong 		return -EFAULT;
741be7cf174SDarrick J. Wong 
742be7cf174SDarrick J. Wong 	error = xfs_scrub_metadata(file, &scrub);
743be7cf174SDarrick J. Wong 	if (error)
744be7cf174SDarrick J. Wong 		return error;
745be7cf174SDarrick J. Wong 
746be7cf174SDarrick J. Wong 	if (copy_to_user(arg, &scrub, sizeof(scrub)))
747be7cf174SDarrick J. Wong 		return -EFAULT;
748be7cf174SDarrick J. Wong 
749be7cf174SDarrick J. Wong 	return 0;
750be7cf174SDarrick J. Wong }
751c77b3758SDarrick J. Wong 
752c77b3758SDarrick J. Wong /* Decide if there have been any scrub failures up to this point. */
753c77b3758SDarrick J. Wong static inline int
xfs_scrubv_check_barrier(struct xfs_mount * mp,const struct xfs_scrub_vec * vectors,const struct xfs_scrub_vec * stop_vec)754c77b3758SDarrick J. Wong xfs_scrubv_check_barrier(
755c77b3758SDarrick J. Wong 	struct xfs_mount		*mp,
756c77b3758SDarrick J. Wong 	const struct xfs_scrub_vec	*vectors,
757c77b3758SDarrick J. Wong 	const struct xfs_scrub_vec	*stop_vec)
758c77b3758SDarrick J. Wong {
759c77b3758SDarrick J. Wong 	const struct xfs_scrub_vec	*v;
760c77b3758SDarrick J. Wong 	__u32				failmask;
761c77b3758SDarrick J. Wong 
762c77b3758SDarrick J. Wong 	failmask = stop_vec->sv_flags & XFS_SCRUB_FLAGS_OUT;
763c77b3758SDarrick J. Wong 
764c77b3758SDarrick J. Wong 	for (v = vectors; v < stop_vec; v++) {
765c77b3758SDarrick J. Wong 		if (v->sv_type == XFS_SCRUB_TYPE_BARRIER)
766c77b3758SDarrick J. Wong 			continue;
767c77b3758SDarrick J. Wong 
768c77b3758SDarrick J. Wong 		/*
769c77b3758SDarrick J. Wong 		 * Runtime errors count as a previous failure, except the ones
770c77b3758SDarrick J. Wong 		 * used to ask userspace to retry.
771c77b3758SDarrick J. Wong 		 */
772c77b3758SDarrick J. Wong 		switch (v->sv_ret) {
773c77b3758SDarrick J. Wong 		case -EBUSY:
774c77b3758SDarrick J. Wong 		case -ENOENT:
775c77b3758SDarrick J. Wong 		case -EUSERS:
776c77b3758SDarrick J. Wong 		case 0:
777c77b3758SDarrick J. Wong 			break;
778c77b3758SDarrick J. Wong 		default:
779c77b3758SDarrick J. Wong 			return -ECANCELED;
780c77b3758SDarrick J. Wong 		}
781c77b3758SDarrick J. Wong 
782c77b3758SDarrick J. Wong 		/*
783c77b3758SDarrick J. Wong 		 * If any of the out-flags on the scrub vector match the mask
784c77b3758SDarrick J. Wong 		 * that was set on the barrier vector, that's a previous fail.
785c77b3758SDarrick J. Wong 		 */
786c77b3758SDarrick J. Wong 		if (v->sv_flags & failmask)
787c77b3758SDarrick J. Wong 			return -ECANCELED;
788c77b3758SDarrick J. Wong 	}
789c77b3758SDarrick J. Wong 
790c77b3758SDarrick J. Wong 	return 0;
791c77b3758SDarrick J. Wong }
792c77b3758SDarrick J. Wong 
7934ad350acSDarrick J. Wong /*
7944ad350acSDarrick J. Wong  * If the caller provided us with a nonzero inode number that isn't the ioctl
7954ad350acSDarrick J. Wong  * file, try to grab a reference to it to eliminate all further untrusted inode
7964ad350acSDarrick J. Wong  * lookups.  If we can't get the inode, let each scrub function try again.
7974ad350acSDarrick J. Wong  */
7984ad350acSDarrick J. Wong STATIC struct xfs_inode *
xchk_scrubv_open_by_handle(struct xfs_mount * mp,const struct xfs_scrub_vec_head * head)7994ad350acSDarrick J. Wong xchk_scrubv_open_by_handle(
8004ad350acSDarrick J. Wong 	struct xfs_mount		*mp,
8014ad350acSDarrick J. Wong 	const struct xfs_scrub_vec_head	*head)
8024ad350acSDarrick J. Wong {
8034ad350acSDarrick J. Wong 	struct xfs_trans		*tp;
8044ad350acSDarrick J. Wong 	struct xfs_inode		*ip;
8054ad350acSDarrick J. Wong 	int				error;
8064ad350acSDarrick J. Wong 
8074ad350acSDarrick J. Wong 	error = xfs_trans_alloc_empty(mp, &tp);
8084ad350acSDarrick J. Wong 	if (error)
8094ad350acSDarrick J. Wong 		return NULL;
8104ad350acSDarrick J. Wong 
8114ad350acSDarrick J. Wong 	error = xfs_iget(mp, tp, head->svh_ino, XCHK_IGET_FLAGS, 0, &ip);
8124ad350acSDarrick J. Wong 	xfs_trans_cancel(tp);
8134ad350acSDarrick J. Wong 	if (error)
8144ad350acSDarrick J. Wong 		return NULL;
8154ad350acSDarrick J. Wong 
8164ad350acSDarrick J. Wong 	if (VFS_I(ip)->i_generation != head->svh_gen) {
8174ad350acSDarrick J. Wong 		xfs_irele(ip);
8184ad350acSDarrick J. Wong 		return NULL;
8194ad350acSDarrick J. Wong 	}
8204ad350acSDarrick J. Wong 
8214ad350acSDarrick J. Wong 	return ip;
8224ad350acSDarrick J. Wong }
8234ad350acSDarrick J. Wong 
824c77b3758SDarrick J. Wong /* Vectored scrub implementation to reduce ioctl calls. */
825c77b3758SDarrick J. Wong int
xfs_ioc_scrubv_metadata(struct file * file,void __user * arg)826c77b3758SDarrick J. Wong xfs_ioc_scrubv_metadata(
827c77b3758SDarrick J. Wong 	struct file			*file,
828c77b3758SDarrick J. Wong 	void				__user *arg)
829c77b3758SDarrick J. Wong {
830c77b3758SDarrick J. Wong 	struct xfs_scrub_vec_head	head;
831c77b3758SDarrick J. Wong 	struct xfs_scrub_vec_head	__user *uhead = arg;
832c77b3758SDarrick J. Wong 	struct xfs_scrub_vec		*vectors;
833c77b3758SDarrick J. Wong 	struct xfs_scrub_vec		__user *uvectors;
834c77b3758SDarrick J. Wong 	struct xfs_inode		*ip_in = XFS_I(file_inode(file));
835c77b3758SDarrick J. Wong 	struct xfs_mount		*mp = ip_in->i_mount;
8364ad350acSDarrick J. Wong 	struct xfs_inode		*handle_ip = NULL;
837c77b3758SDarrick J. Wong 	struct xfs_scrub_vec		*v;
838c77b3758SDarrick J. Wong 	size_t				vec_bytes;
839c77b3758SDarrick J. Wong 	unsigned int			i;
840c77b3758SDarrick J. Wong 	int				error = 0;
841c77b3758SDarrick J. Wong 
842c77b3758SDarrick J. Wong 	if (!capable(CAP_SYS_ADMIN))
843c77b3758SDarrick J. Wong 		return -EPERM;
844c77b3758SDarrick J. Wong 
845c77b3758SDarrick J. Wong 	if (copy_from_user(&head, uhead, sizeof(head)))
846c77b3758SDarrick J. Wong 		return -EFAULT;
847c77b3758SDarrick J. Wong 
848c77b3758SDarrick J. Wong 	if (head.svh_reserved)
849c77b3758SDarrick J. Wong 		return -EINVAL;
850c77b3758SDarrick J. Wong 	if (head.svh_flags & ~XFS_SCRUB_VEC_FLAGS_ALL)
851c77b3758SDarrick J. Wong 		return -EINVAL;
852c77b3758SDarrick J. Wong 	if (head.svh_nr == 0)
853c77b3758SDarrick J. Wong 		return 0;
854c77b3758SDarrick J. Wong 
855c77b3758SDarrick J. Wong 	vec_bytes = array_size(head.svh_nr, sizeof(struct xfs_scrub_vec));
856c77b3758SDarrick J. Wong 	if (vec_bytes > PAGE_SIZE)
857c77b3758SDarrick J. Wong 		return -ENOMEM;
858c77b3758SDarrick J. Wong 
859*95b19e2fSDarrick J. Wong 	uvectors = u64_to_user_ptr(head.svh_vectors);
860c77b3758SDarrick J. Wong 	vectors = memdup_user(uvectors, vec_bytes);
861c77b3758SDarrick J. Wong 	if (IS_ERR(vectors))
862c77b3758SDarrick J. Wong 		return PTR_ERR(vectors);
863c77b3758SDarrick J. Wong 
864c77b3758SDarrick J. Wong 	trace_xchk_scrubv_start(ip_in, &head);
865c77b3758SDarrick J. Wong 
866c77b3758SDarrick J. Wong 	for (i = 0, v = vectors; i < head.svh_nr; i++, v++) {
867c77b3758SDarrick J. Wong 		if (v->sv_reserved) {
868c77b3758SDarrick J. Wong 			error = -EINVAL;
869c77b3758SDarrick J. Wong 			goto out_free;
870c77b3758SDarrick J. Wong 		}
871c77b3758SDarrick J. Wong 
872c77b3758SDarrick J. Wong 		if (v->sv_type == XFS_SCRUB_TYPE_BARRIER &&
873c77b3758SDarrick J. Wong 		    (v->sv_flags & ~XFS_SCRUB_FLAGS_OUT)) {
874c77b3758SDarrick J. Wong 			error = -EINVAL;
875c77b3758SDarrick J. Wong 			goto out_free;
876c77b3758SDarrick J. Wong 		}
877c77b3758SDarrick J. Wong 
878c77b3758SDarrick J. Wong 		trace_xchk_scrubv_item(mp, &head, i, v);
879c77b3758SDarrick J. Wong 	}
880c77b3758SDarrick J. Wong 
8814ad350acSDarrick J. Wong 	/*
8824ad350acSDarrick J. Wong 	 * If the caller wants us to do a scrub-by-handle and the file used to
8834ad350acSDarrick J. Wong 	 * call the ioctl is not the same file, load the incore inode and pin
8844ad350acSDarrick J. Wong 	 * it across all the scrubv actions to avoid repeated UNTRUSTED
8854ad350acSDarrick J. Wong 	 * lookups.  The reference is not passed to deeper layers of scrub
8864ad350acSDarrick J. Wong 	 * because each scrubber gets to decide its own strategy and return
8874ad350acSDarrick J. Wong 	 * values for getting an inode.
8884ad350acSDarrick J. Wong 	 */
8894ad350acSDarrick J. Wong 	if (head.svh_ino && head.svh_ino != ip_in->i_ino)
8904ad350acSDarrick J. Wong 		handle_ip = xchk_scrubv_open_by_handle(mp, &head);
8914ad350acSDarrick J. Wong 
892c77b3758SDarrick J. Wong 	/* Run all the scrubbers. */
893c77b3758SDarrick J. Wong 	for (i = 0, v = vectors; i < head.svh_nr; i++, v++) {
894c77b3758SDarrick J. Wong 		struct xfs_scrub_metadata	sm = {
895c77b3758SDarrick J. Wong 			.sm_type		= v->sv_type,
896c77b3758SDarrick J. Wong 			.sm_flags		= v->sv_flags,
897c77b3758SDarrick J. Wong 			.sm_ino			= head.svh_ino,
898c77b3758SDarrick J. Wong 			.sm_gen			= head.svh_gen,
899c77b3758SDarrick J. Wong 			.sm_agno		= head.svh_agno,
900c77b3758SDarrick J. Wong 		};
901c77b3758SDarrick J. Wong 
902c77b3758SDarrick J. Wong 		if (v->sv_type == XFS_SCRUB_TYPE_BARRIER) {
903c77b3758SDarrick J. Wong 			v->sv_ret = xfs_scrubv_check_barrier(mp, vectors, v);
904c77b3758SDarrick J. Wong 			if (v->sv_ret) {
905c77b3758SDarrick J. Wong 				trace_xchk_scrubv_barrier_fail(mp, &head, i, v);
906c77b3758SDarrick J. Wong 				break;
907c77b3758SDarrick J. Wong 			}
908c77b3758SDarrick J. Wong 
909c77b3758SDarrick J. Wong 			continue;
910c77b3758SDarrick J. Wong 		}
911c77b3758SDarrick J. Wong 
912c77b3758SDarrick J. Wong 		v->sv_ret = xfs_scrub_metadata(file, &sm);
913c77b3758SDarrick J. Wong 		v->sv_flags = sm.sm_flags;
914c77b3758SDarrick J. Wong 
915c77b3758SDarrick J. Wong 		trace_xchk_scrubv_outcome(mp, &head, i, v);
916c77b3758SDarrick J. Wong 
917c77b3758SDarrick J. Wong 		if (head.svh_rest_us) {
918c77b3758SDarrick J. Wong 			ktime_t		expires;
919c77b3758SDarrick J. Wong 
920c77b3758SDarrick J. Wong 			expires = ktime_add_ns(ktime_get(),
921c77b3758SDarrick J. Wong 					head.svh_rest_us * 1000);
922c77b3758SDarrick J. Wong 			set_current_state(TASK_KILLABLE);
923c77b3758SDarrick J. Wong 			schedule_hrtimeout(&expires, HRTIMER_MODE_ABS);
924c77b3758SDarrick J. Wong 		}
925c77b3758SDarrick J. Wong 
926c77b3758SDarrick J. Wong 		if (fatal_signal_pending(current)) {
927c77b3758SDarrick J. Wong 			error = -EINTR;
928c77b3758SDarrick J. Wong 			goto out_free;
929c77b3758SDarrick J. Wong 		}
930c77b3758SDarrick J. Wong 	}
931c77b3758SDarrick J. Wong 
932c77b3758SDarrick J. Wong 	if (copy_to_user(uvectors, vectors, vec_bytes) ||
933c77b3758SDarrick J. Wong 	    copy_to_user(uhead, &head, sizeof(head))) {
934c77b3758SDarrick J. Wong 		error = -EFAULT;
935c77b3758SDarrick J. Wong 		goto out_free;
936c77b3758SDarrick J. Wong 	}
937c77b3758SDarrick J. Wong 
938c77b3758SDarrick J. Wong out_free:
9394ad350acSDarrick J. Wong 	if (handle_ip)
9404ad350acSDarrick J. Wong 		xfs_irele(handle_ip);
941c77b3758SDarrick J. Wong 	kfree(vectors);
942c77b3758SDarrick J. Wong 	return error;
943c77b3758SDarrick J. Wong }
944