xref: /linux/fs/xfs/scrub/common.h (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1739a2fe0SDarrick J. Wong // SPDX-License-Identifier: GPL-2.0-or-later
2dcb660f9SDarrick J. Wong /*
3ecc73f8aSDarrick J. Wong  * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4739a2fe0SDarrick J. Wong  * Author: Darrick J. Wong <djwong@kernel.org>
5dcb660f9SDarrick J. Wong  */
6dcb660f9SDarrick J. Wong #ifndef __XFS_SCRUB_COMMON_H__
7dcb660f9SDarrick J. Wong #define __XFS_SCRUB_COMMON_H__
8dcb660f9SDarrick J. Wong 
91d8a748aSDarrick J. Wong int xchk_trans_alloc(struct xfs_scrub *sc, uint resblks);
10564fee6dSDarrick J. Wong int xchk_trans_alloc_empty(struct xfs_scrub *sc);
11302436c2SDarrick J. Wong void xchk_trans_cancel(struct xfs_scrub *sc);
12302436c2SDarrick J. Wong 
131d8a748aSDarrick J. Wong bool xchk_process_error(struct xfs_scrub *sc, xfs_agnumber_t agno,
144700d229SDarrick J. Wong 		xfs_agblock_t bno, int *error);
151d8a748aSDarrick J. Wong bool xchk_fblock_process_error(struct xfs_scrub *sc, int whichfork,
164700d229SDarrick J. Wong 		xfs_fileoff_t offset, int *error);
174700d229SDarrick J. Wong 
181d8a748aSDarrick J. Wong bool xchk_xref_process_error(struct xfs_scrub *sc,
1964b12563SDarrick J. Wong 		xfs_agnumber_t agno, xfs_agblock_t bno, int *error);
201d8a748aSDarrick J. Wong bool xchk_fblock_xref_process_error(struct xfs_scrub *sc,
2164b12563SDarrick J. Wong 		int whichfork, xfs_fileoff_t offset, int *error);
2264b12563SDarrick J. Wong 
231d8a748aSDarrick J. Wong void xchk_block_set_preen(struct xfs_scrub *sc,
244700d229SDarrick J. Wong 		struct xfs_buf *bp);
251d8a748aSDarrick J. Wong void xchk_ino_set_preen(struct xfs_scrub *sc, xfs_ino_t ino);
264700d229SDarrick J. Wong 
2775efa57dSDarrick J. Wong void xchk_set_corrupt(struct xfs_scrub *sc);
281d8a748aSDarrick J. Wong void xchk_block_set_corrupt(struct xfs_scrub *sc,
294700d229SDarrick J. Wong 		struct xfs_buf *bp);
301d8a748aSDarrick J. Wong void xchk_ino_set_corrupt(struct xfs_scrub *sc, xfs_ino_t ino);
311d8a748aSDarrick J. Wong void xchk_fblock_set_corrupt(struct xfs_scrub *sc, int whichfork,
324700d229SDarrick J. Wong 		xfs_fileoff_t offset);
3348dd9117SDarrick J. Wong #ifdef CONFIG_XFS_QUOTA
3448dd9117SDarrick J. Wong void xchk_qcheck_set_corrupt(struct xfs_scrub *sc, unsigned int dqtype,
3548dd9117SDarrick J. Wong 		xfs_dqid_t id);
3648dd9117SDarrick J. Wong #endif
374700d229SDarrick J. Wong 
381d8a748aSDarrick J. Wong void xchk_block_xref_set_corrupt(struct xfs_scrub *sc,
3964b12563SDarrick J. Wong 		struct xfs_buf *bp);
401d8a748aSDarrick J. Wong void xchk_ino_xref_set_corrupt(struct xfs_scrub *sc,
417e56d9eaSDarrick J. Wong 		xfs_ino_t ino);
421d8a748aSDarrick J. Wong void xchk_fblock_xref_set_corrupt(struct xfs_scrub *sc,
4364b12563SDarrick J. Wong 		int whichfork, xfs_fileoff_t offset);
4464b12563SDarrick J. Wong 
451d8a748aSDarrick J. Wong void xchk_ino_set_warning(struct xfs_scrub *sc, xfs_ino_t ino);
461d8a748aSDarrick J. Wong void xchk_fblock_set_warning(struct xfs_scrub *sc, int whichfork,
474700d229SDarrick J. Wong 		xfs_fileoff_t offset);
484700d229SDarrick J. Wong 
491d8a748aSDarrick J. Wong void xchk_set_incomplete(struct xfs_scrub *sc);
50c517b3aaSDarrick J. Wong int xchk_checkpoint_log(struct xfs_mount *mp);
514700d229SDarrick J. Wong 
5264b12563SDarrick J. Wong /* Are we set up for a cross-referencing check? */
531d8a748aSDarrick J. Wong bool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
5464b12563SDarrick J. Wong 			   struct xfs_btree_cur **curpp);
5564b12563SDarrick J. Wong 
xchk_setup_nothing(struct xfs_scrub * sc)56*0902819fSDarrick J. Wong static inline int xchk_setup_nothing(struct xfs_scrub *sc)
57*0902819fSDarrick J. Wong {
58*0902819fSDarrick J. Wong 	return -ENOENT;
59*0902819fSDarrick J. Wong }
60*0902819fSDarrick J. Wong 
61dcb660f9SDarrick J. Wong /* Setup functions */
62466c525dSDarrick J. Wong int xchk_setup_agheader(struct xfs_scrub *sc);
63026f57ebSDarrick J. Wong int xchk_setup_fs(struct xfs_scrub *sc);
64026f57ebSDarrick J. Wong int xchk_setup_ag_allocbt(struct xfs_scrub *sc);
65026f57ebSDarrick J. Wong int xchk_setup_ag_iallocbt(struct xfs_scrub *sc);
66026f57ebSDarrick J. Wong int xchk_setup_ag_rmapbt(struct xfs_scrub *sc);
67026f57ebSDarrick J. Wong int xchk_setup_ag_refcountbt(struct xfs_scrub *sc);
68026f57ebSDarrick J. Wong int xchk_setup_inode(struct xfs_scrub *sc);
69026f57ebSDarrick J. Wong int xchk_setup_inode_bmap(struct xfs_scrub *sc);
70026f57ebSDarrick J. Wong int xchk_setup_inode_bmap_data(struct xfs_scrub *sc);
71026f57ebSDarrick J. Wong int xchk_setup_directory(struct xfs_scrub *sc);
72026f57ebSDarrick J. Wong int xchk_setup_xattr(struct xfs_scrub *sc);
73026f57ebSDarrick J. Wong int xchk_setup_symlink(struct xfs_scrub *sc);
74026f57ebSDarrick J. Wong int xchk_setup_parent(struct xfs_scrub *sc);
75928b721aSDarrick J. Wong int xchk_setup_dirtree(struct xfs_scrub *sc);
7629b0767bSDarrick J. Wong #ifdef CONFIG_XFS_RT
77526aab5fSDarrick J. Wong int xchk_setup_rtbitmap(struct xfs_scrub *sc);
78526aab5fSDarrick J. Wong int xchk_setup_rtsummary(struct xfs_scrub *sc);
7929b0767bSDarrick J. Wong #else
80*0902819fSDarrick J. Wong # define xchk_setup_rtbitmap		xchk_setup_nothing
81*0902819fSDarrick J. Wong # define xchk_setup_rtsummary		xchk_setup_nothing
8229b0767bSDarrick J. Wong #endif
83c2fc338cSDarrick J. Wong #ifdef CONFIG_XFS_QUOTA
84259ba1d3SDarrick J. Wong int xchk_ino_dqattach(struct xfs_scrub *sc);
85026f57ebSDarrick J. Wong int xchk_setup_quota(struct xfs_scrub *sc);
8648dd9117SDarrick J. Wong int xchk_setup_quotacheck(struct xfs_scrub *sc);
87c2fc338cSDarrick J. Wong #else
88c2fc338cSDarrick J. Wong static inline int
xchk_ino_dqattach(struct xfs_scrub * sc)89259ba1d3SDarrick J. Wong xchk_ino_dqattach(struct xfs_scrub *sc)
90259ba1d3SDarrick J. Wong {
91259ba1d3SDarrick J. Wong 	return 0;
92259ba1d3SDarrick J. Wong }
93*0902819fSDarrick J. Wong # define xchk_setup_quota		xchk_setup_nothing
94*0902819fSDarrick J. Wong # define xchk_setup_quotacheck		xchk_setup_nothing
95c2fc338cSDarrick J. Wong #endif
96026f57ebSDarrick J. Wong int xchk_setup_fscounters(struct xfs_scrub *sc);
97f1184081SDarrick J. Wong int xchk_setup_nlinks(struct xfs_scrub *sc);
98dcb660f9SDarrick J. Wong 
991d8a748aSDarrick J. Wong void xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
1001d8a748aSDarrick J. Wong int xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
101c517b3aaSDarrick J. Wong 		struct xchk_ag *sa);
10232080a9bSDarrick J. Wong int xchk_perag_drain_and_lock(struct xfs_scrub *sc);
10348c6615cSDarrick J. Wong 
10448c6615cSDarrick J. Wong /*
10548c6615cSDarrick J. Wong  * Grab all AG resources, treating the inability to grab the perag structure as
10648c6615cSDarrick J. Wong  * a fs corruption.  This is intended for callers checking an ondisk reference
10748c6615cSDarrick J. Wong  * to a given AG, which means that the AG must still exist.
10848c6615cSDarrick J. Wong  */
10948c6615cSDarrick J. Wong static inline int
xchk_ag_init_existing(struct xfs_scrub * sc,xfs_agnumber_t agno,struct xchk_ag * sa)11048c6615cSDarrick J. Wong xchk_ag_init_existing(
11148c6615cSDarrick J. Wong 	struct xfs_scrub	*sc,
11248c6615cSDarrick J. Wong 	xfs_agnumber_t		agno,
11348c6615cSDarrick J. Wong 	struct xchk_ag		*sa)
11448c6615cSDarrick J. Wong {
11548c6615cSDarrick J. Wong 	int			error = xchk_ag_init(sc, agno, sa);
11648c6615cSDarrick J. Wong 
11748c6615cSDarrick J. Wong 	return error == -ENOENT ? -EFSCORRUPTED : error;
11848c6615cSDarrick J. Wong }
11948c6615cSDarrick J. Wong 
1201d8a748aSDarrick J. Wong int xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
121de9d2a78SDarrick J. Wong 		struct xchk_ag *sa);
122c517b3aaSDarrick J. Wong void xchk_ag_btcur_free(struct xchk_ag *sa);
123f53acfacSDarrick J. Wong void xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
124032d91f9SDarrick J. Wong int xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
12566e3237eSDarrick J. Wong 		const struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
126b6c1beb9SDarrick J. Wong 
127026f57ebSDarrick J. Wong int xchk_setup_ag_btree(struct xfs_scrub *sc, bool force_log);
12846e0dd89SDarrick J. Wong int xchk_iget_for_scrubbing(struct xfs_scrub *sc);
129026f57ebSDarrick J. Wong int xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
13017308539SDarrick J. Wong int xchk_install_live_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
131294012fbSDarrick J. Wong 
132294012fbSDarrick J. Wong void xchk_ilock(struct xfs_scrub *sc, unsigned int ilock_flags);
133294012fbSDarrick J. Wong bool xchk_ilock_nowait(struct xfs_scrub *sc, unsigned int ilock_flags);
134294012fbSDarrick J. Wong void xchk_iunlock(struct xfs_scrub *sc, unsigned int ilock_flags);
135294012fbSDarrick J. Wong 
1361d8a748aSDarrick J. Wong void xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
137efa7a99cSDarrick J. Wong 
1383f113c27SDarrick J. Wong /*
1393f113c27SDarrick J. Wong  * Grab the inode at @inum.  The caller must have created a scrub transaction
1403f113c27SDarrick J. Wong  * so that we can confirm the inumber by walking the inobt and not deadlock on
1413f113c27SDarrick J. Wong  * a loop in the inobt.
1423f113c27SDarrick J. Wong  */
143a03297a0SDarrick J. Wong int xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
144302436c2SDarrick J. Wong int xchk_iget_agi(struct xfs_scrub *sc, xfs_ino_t inum,
145302436c2SDarrick J. Wong 		struct xfs_buf **agi_bpp, struct xfs_inode **ipp);
146a03297a0SDarrick J. Wong void xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
14738bb1310SDarrick J. Wong int xchk_install_handle_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
148a03297a0SDarrick J. Wong 
1498389f3ffSDarrick J. Wong /*
1503f113c27SDarrick J. Wong  * Safe version of (untrusted) xchk_iget that uses an empty transaction to
1513f113c27SDarrick J. Wong  * avoid deadlocking on loops in the inobt.  This should only be used in a
1523f113c27SDarrick J. Wong  * scrub or repair setup routine, and only prior to grabbing a transaction.
1533f113c27SDarrick J. Wong  */
1543f113c27SDarrick J. Wong static inline int
xchk_iget_safe(struct xfs_scrub * sc,xfs_ino_t inum,struct xfs_inode ** ipp)1553f113c27SDarrick J. Wong xchk_iget_safe(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp)
1563f113c27SDarrick J. Wong {
1573f113c27SDarrick J. Wong 	int	error;
1583f113c27SDarrick J. Wong 
1593f113c27SDarrick J. Wong 	ASSERT(sc->tp == NULL);
1603f113c27SDarrick J. Wong 
1613f113c27SDarrick J. Wong 	error = xchk_trans_alloc(sc, 0);
1623f113c27SDarrick J. Wong 	if (error)
1633f113c27SDarrick J. Wong 		return error;
1643f113c27SDarrick J. Wong 	error = xchk_iget(sc, inum, ipp);
1653f113c27SDarrick J. Wong 	xchk_trans_cancel(sc);
1663f113c27SDarrick J. Wong 	return error;
1673f113c27SDarrick J. Wong }
1683f113c27SDarrick J. Wong 
1693f113c27SDarrick J. Wong /*
1708389f3ffSDarrick J. Wong  * Don't bother cross-referencing if we already found corruption or cross
1718389f3ffSDarrick J. Wong  * referencing discrepancies.
1728389f3ffSDarrick J. Wong  */
xchk_skip_xref(struct xfs_scrub_metadata * sm)173c517b3aaSDarrick J. Wong static inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
1748389f3ffSDarrick J. Wong {
1758389f3ffSDarrick J. Wong 	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
1768389f3ffSDarrick J. Wong 			       XFS_SCRUB_OFLAG_XCORRUPT);
1778389f3ffSDarrick J. Wong }
1788389f3ffSDarrick J. Wong 
1796c728952SDarrick J. Wong bool xchk_dir_looks_zapped(struct xfs_inode *dp);
18077ede5f4SDarrick J. Wong bool xchk_pptr_looks_zapped(struct xfs_inode *ip);
1816c728952SDarrick J. Wong 
1828336a64eSDarrick J. Wong #ifdef CONFIG_XFS_ONLINE_REPAIR
1838336a64eSDarrick J. Wong /* Decide if a repair is required. */
xchk_needs_repair(const struct xfs_scrub_metadata * sm)1848336a64eSDarrick J. Wong static inline bool xchk_needs_repair(const struct xfs_scrub_metadata *sm)
1858336a64eSDarrick J. Wong {
1868336a64eSDarrick J. Wong 	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
1878336a64eSDarrick J. Wong 			       XFS_SCRUB_OFLAG_XCORRUPT |
1888336a64eSDarrick J. Wong 			       XFS_SCRUB_OFLAG_PREEN);
1898336a64eSDarrick J. Wong }
1904bdfd7d1SDarrick J. Wong 
1914bdfd7d1SDarrick J. Wong /*
1924bdfd7d1SDarrick J. Wong  * "Should we prepare for a repair?"
1934bdfd7d1SDarrick J. Wong  *
1944bdfd7d1SDarrick J. Wong  * Return true if the caller permits us to repair metadata and we're not
1954bdfd7d1SDarrick J. Wong  * setting up for a post-repair evaluation.
1964bdfd7d1SDarrick J. Wong  */
xchk_could_repair(const struct xfs_scrub * sc)1974bdfd7d1SDarrick J. Wong static inline bool xchk_could_repair(const struct xfs_scrub *sc)
1984bdfd7d1SDarrick J. Wong {
1994bdfd7d1SDarrick J. Wong 	return (sc->sm->sm_flags & XFS_SCRUB_IFLAG_REPAIR) &&
2004bdfd7d1SDarrick J. Wong 		!(sc->flags & XREP_ALREADY_FIXED);
2014bdfd7d1SDarrick J. Wong }
2028336a64eSDarrick J. Wong #else
2038336a64eSDarrick J. Wong # define xchk_needs_repair(sc)		(false)
2044bdfd7d1SDarrick J. Wong # define xchk_could_repair(sc)		(false)
2058336a64eSDarrick J. Wong #endif /* CONFIG_XFS_ONLINE_REPAIR */
2068336a64eSDarrick J. Wong 
2071d8a748aSDarrick J. Wong int xchk_metadata_inode_forks(struct xfs_scrub *sc);
20887d9d609SDarrick J. Wong 
209466c525dSDarrick J. Wong /*
210526aab5fSDarrick J. Wong  * Helper macros to allocate and format xfile description strings.
211526aab5fSDarrick J. Wong  * Callers must kfree the pointer returned.
212526aab5fSDarrick J. Wong  */
213526aab5fSDarrick J. Wong #define xchk_xfile_descr(sc, fmt, ...) \
214526aab5fSDarrick J. Wong 	kasprintf(XCHK_GFP_FLAGS, "XFS (%s): " fmt, \
215526aab5fSDarrick J. Wong 			(sc)->mp->m_super->s_id, ##__VA_ARGS__)
2164bdfd7d1SDarrick J. Wong #define xchk_xfile_ag_descr(sc, fmt, ...) \
2174bdfd7d1SDarrick J. Wong 	kasprintf(XCHK_GFP_FLAGS, "XFS (%s): AG 0x%x " fmt, \
2184bdfd7d1SDarrick J. Wong 			(sc)->mp->m_super->s_id, \
2194bdfd7d1SDarrick J. Wong 			(sc)->sa.pag ? (sc)->sa.pag->pag_agno : (sc)->sm->sm_agno, \
2204bdfd7d1SDarrick J. Wong 			##__VA_ARGS__)
2218f71bedeSDarrick J. Wong #define xchk_xfile_ino_descr(sc, fmt, ...) \
2228f71bedeSDarrick J. Wong 	kasprintf(XCHK_GFP_FLAGS, "XFS (%s): inode 0x%llx " fmt, \
2238f71bedeSDarrick J. Wong 			(sc)->mp->m_super->s_id, \
2248f71bedeSDarrick J. Wong 			(sc)->ip ? (sc)->ip->i_ino : (sc)->sm->sm_ino, \
2258f71bedeSDarrick J. Wong 			##__VA_ARGS__)
226526aab5fSDarrick J. Wong 
227526aab5fSDarrick J. Wong /*
228466c525dSDarrick J. Wong  * Setting up a hook to wait for intents to drain is costly -- we have to take
229466c525dSDarrick J. Wong  * the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
230466c525dSDarrick J. Wong  * up, and again to tear it down.  These costs add up quickly, so we only want
231466c525dSDarrick J. Wong  * to enable the drain waiter if the drain actually detected a conflict with
232466c525dSDarrick J. Wong  * running intent chains.
233466c525dSDarrick J. Wong  */
xchk_need_intent_drain(struct xfs_scrub * sc)234466c525dSDarrick J. Wong static inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
235466c525dSDarrick J. Wong {
23688accf17SDarrick J. Wong 	return sc->flags & XCHK_NEED_DRAIN;
237466c525dSDarrick J. Wong }
238466c525dSDarrick J. Wong 
239466c525dSDarrick J. Wong void xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
240466c525dSDarrick J. Wong 
241369c001bSDarrick J. Wong int xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino,
242369c001bSDarrick J. Wong 		bool *inuse);
2430d296634SDarrick J. Wong 
244dcb660f9SDarrick J. Wong #endif	/* __XFS_SCRUB_COMMON_H__ */
245