162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
462306a36Sopenharmony_ci * Author: Darrick J. Wong <djwong@kernel.org>
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci#ifndef __XFS_SCRUB_COMMON_H__
762306a36Sopenharmony_ci#define __XFS_SCRUB_COMMON_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci/*
1062306a36Sopenharmony_ci * We /could/ terminate a scrub/repair operation early.  If we're not
1162306a36Sopenharmony_ci * in a good place to continue (fatal signal, etc.) then bail out.
1262306a36Sopenharmony_ci * Note that we're careful not to make any judgements about *error.
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_cistatic inline bool
1562306a36Sopenharmony_cixchk_should_terminate(
1662306a36Sopenharmony_ci	struct xfs_scrub	*sc,
1762306a36Sopenharmony_ci	int			*error)
1862306a36Sopenharmony_ci{
1962306a36Sopenharmony_ci	/*
2062306a36Sopenharmony_ci	 * If preemption is disabled, we need to yield to the scheduler every
2162306a36Sopenharmony_ci	 * few seconds so that we don't run afoul of the soft lockup watchdog
2262306a36Sopenharmony_ci	 * or RCU stall detector.
2362306a36Sopenharmony_ci	 */
2462306a36Sopenharmony_ci	cond_resched();
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci	if (fatal_signal_pending(current)) {
2762306a36Sopenharmony_ci		if (*error == 0)
2862306a36Sopenharmony_ci			*error = -EINTR;
2962306a36Sopenharmony_ci		return true;
3062306a36Sopenharmony_ci	}
3162306a36Sopenharmony_ci	return false;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ciint xchk_trans_alloc(struct xfs_scrub *sc, uint resblks);
3562306a36Sopenharmony_civoid xchk_trans_cancel(struct xfs_scrub *sc);
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cibool xchk_process_error(struct xfs_scrub *sc, xfs_agnumber_t agno,
3862306a36Sopenharmony_ci		xfs_agblock_t bno, int *error);
3962306a36Sopenharmony_cibool xchk_fblock_process_error(struct xfs_scrub *sc, int whichfork,
4062306a36Sopenharmony_ci		xfs_fileoff_t offset, int *error);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cibool xchk_xref_process_error(struct xfs_scrub *sc,
4362306a36Sopenharmony_ci		xfs_agnumber_t agno, xfs_agblock_t bno, int *error);
4462306a36Sopenharmony_cibool xchk_fblock_xref_process_error(struct xfs_scrub *sc,
4562306a36Sopenharmony_ci		int whichfork, xfs_fileoff_t offset, int *error);
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_civoid xchk_block_set_preen(struct xfs_scrub *sc,
4862306a36Sopenharmony_ci		struct xfs_buf *bp);
4962306a36Sopenharmony_civoid xchk_ino_set_preen(struct xfs_scrub *sc, xfs_ino_t ino);
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_civoid xchk_set_corrupt(struct xfs_scrub *sc);
5262306a36Sopenharmony_civoid xchk_block_set_corrupt(struct xfs_scrub *sc,
5362306a36Sopenharmony_ci		struct xfs_buf *bp);
5462306a36Sopenharmony_civoid xchk_ino_set_corrupt(struct xfs_scrub *sc, xfs_ino_t ino);
5562306a36Sopenharmony_civoid xchk_fblock_set_corrupt(struct xfs_scrub *sc, int whichfork,
5662306a36Sopenharmony_ci		xfs_fileoff_t offset);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_civoid xchk_block_xref_set_corrupt(struct xfs_scrub *sc,
5962306a36Sopenharmony_ci		struct xfs_buf *bp);
6062306a36Sopenharmony_civoid xchk_ino_xref_set_corrupt(struct xfs_scrub *sc,
6162306a36Sopenharmony_ci		xfs_ino_t ino);
6262306a36Sopenharmony_civoid xchk_fblock_xref_set_corrupt(struct xfs_scrub *sc,
6362306a36Sopenharmony_ci		int whichfork, xfs_fileoff_t offset);
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_civoid xchk_ino_set_warning(struct xfs_scrub *sc, xfs_ino_t ino);
6662306a36Sopenharmony_civoid xchk_fblock_set_warning(struct xfs_scrub *sc, int whichfork,
6762306a36Sopenharmony_ci		xfs_fileoff_t offset);
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_civoid xchk_set_incomplete(struct xfs_scrub *sc);
7062306a36Sopenharmony_ciint xchk_checkpoint_log(struct xfs_mount *mp);
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci/* Are we set up for a cross-referencing check? */
7362306a36Sopenharmony_cibool xchk_should_check_xref(struct xfs_scrub *sc, int *error,
7462306a36Sopenharmony_ci			   struct xfs_btree_cur **curpp);
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci/* Setup functions */
7762306a36Sopenharmony_ciint xchk_setup_agheader(struct xfs_scrub *sc);
7862306a36Sopenharmony_ciint xchk_setup_fs(struct xfs_scrub *sc);
7962306a36Sopenharmony_ciint xchk_setup_ag_allocbt(struct xfs_scrub *sc);
8062306a36Sopenharmony_ciint xchk_setup_ag_iallocbt(struct xfs_scrub *sc);
8162306a36Sopenharmony_ciint xchk_setup_ag_rmapbt(struct xfs_scrub *sc);
8262306a36Sopenharmony_ciint xchk_setup_ag_refcountbt(struct xfs_scrub *sc);
8362306a36Sopenharmony_ciint xchk_setup_inode(struct xfs_scrub *sc);
8462306a36Sopenharmony_ciint xchk_setup_inode_bmap(struct xfs_scrub *sc);
8562306a36Sopenharmony_ciint xchk_setup_inode_bmap_data(struct xfs_scrub *sc);
8662306a36Sopenharmony_ciint xchk_setup_directory(struct xfs_scrub *sc);
8762306a36Sopenharmony_ciint xchk_setup_xattr(struct xfs_scrub *sc);
8862306a36Sopenharmony_ciint xchk_setup_symlink(struct xfs_scrub *sc);
8962306a36Sopenharmony_ciint xchk_setup_parent(struct xfs_scrub *sc);
9062306a36Sopenharmony_ci#ifdef CONFIG_XFS_RT
9162306a36Sopenharmony_ciint xchk_setup_rtbitmap(struct xfs_scrub *sc);
9262306a36Sopenharmony_ciint xchk_setup_rtsummary(struct xfs_scrub *sc);
9362306a36Sopenharmony_ci#else
9462306a36Sopenharmony_cistatic inline int
9562306a36Sopenharmony_cixchk_setup_rtbitmap(struct xfs_scrub *sc)
9662306a36Sopenharmony_ci{
9762306a36Sopenharmony_ci	return -ENOENT;
9862306a36Sopenharmony_ci}
9962306a36Sopenharmony_cistatic inline int
10062306a36Sopenharmony_cixchk_setup_rtsummary(struct xfs_scrub *sc)
10162306a36Sopenharmony_ci{
10262306a36Sopenharmony_ci	return -ENOENT;
10362306a36Sopenharmony_ci}
10462306a36Sopenharmony_ci#endif
10562306a36Sopenharmony_ci#ifdef CONFIG_XFS_QUOTA
10662306a36Sopenharmony_ciint xchk_setup_quota(struct xfs_scrub *sc);
10762306a36Sopenharmony_ci#else
10862306a36Sopenharmony_cistatic inline int
10962306a36Sopenharmony_cixchk_setup_quota(struct xfs_scrub *sc)
11062306a36Sopenharmony_ci{
11162306a36Sopenharmony_ci	return -ENOENT;
11262306a36Sopenharmony_ci}
11362306a36Sopenharmony_ci#endif
11462306a36Sopenharmony_ciint xchk_setup_fscounters(struct xfs_scrub *sc);
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_civoid xchk_ag_free(struct xfs_scrub *sc, struct xchk_ag *sa);
11762306a36Sopenharmony_ciint xchk_ag_init(struct xfs_scrub *sc, xfs_agnumber_t agno,
11862306a36Sopenharmony_ci		struct xchk_ag *sa);
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci/*
12162306a36Sopenharmony_ci * Grab all AG resources, treating the inability to grab the perag structure as
12262306a36Sopenharmony_ci * a fs corruption.  This is intended for callers checking an ondisk reference
12362306a36Sopenharmony_ci * to a given AG, which means that the AG must still exist.
12462306a36Sopenharmony_ci */
12562306a36Sopenharmony_cistatic inline int
12662306a36Sopenharmony_cixchk_ag_init_existing(
12762306a36Sopenharmony_ci	struct xfs_scrub	*sc,
12862306a36Sopenharmony_ci	xfs_agnumber_t		agno,
12962306a36Sopenharmony_ci	struct xchk_ag		*sa)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	int			error = xchk_ag_init(sc, agno, sa);
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	return error == -ENOENT ? -EFSCORRUPTED : error;
13462306a36Sopenharmony_ci}
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ciint xchk_ag_read_headers(struct xfs_scrub *sc, xfs_agnumber_t agno,
13762306a36Sopenharmony_ci		struct xchk_ag *sa);
13862306a36Sopenharmony_civoid xchk_ag_btcur_free(struct xchk_ag *sa);
13962306a36Sopenharmony_civoid xchk_ag_btcur_init(struct xfs_scrub *sc, struct xchk_ag *sa);
14062306a36Sopenharmony_ciint xchk_count_rmap_ownedby_ag(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
14162306a36Sopenharmony_ci		const struct xfs_owner_info *oinfo, xfs_filblks_t *blocks);
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ciint xchk_setup_ag_btree(struct xfs_scrub *sc, bool force_log);
14462306a36Sopenharmony_ciint xchk_iget_for_scrubbing(struct xfs_scrub *sc);
14562306a36Sopenharmony_ciint xchk_setup_inode_contents(struct xfs_scrub *sc, unsigned int resblks);
14662306a36Sopenharmony_ciint xchk_install_live_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
14762306a36Sopenharmony_ci
14862306a36Sopenharmony_civoid xchk_ilock(struct xfs_scrub *sc, unsigned int ilock_flags);
14962306a36Sopenharmony_cibool xchk_ilock_nowait(struct xfs_scrub *sc, unsigned int ilock_flags);
15062306a36Sopenharmony_civoid xchk_iunlock(struct xfs_scrub *sc, unsigned int ilock_flags);
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_civoid xchk_buffer_recheck(struct xfs_scrub *sc, struct xfs_buf *bp);
15362306a36Sopenharmony_ci
15462306a36Sopenharmony_ciint xchk_iget(struct xfs_scrub *sc, xfs_ino_t inum, struct xfs_inode **ipp);
15562306a36Sopenharmony_ciint xchk_iget_agi(struct xfs_scrub *sc, xfs_ino_t inum,
15662306a36Sopenharmony_ci		struct xfs_buf **agi_bpp, struct xfs_inode **ipp);
15762306a36Sopenharmony_civoid xchk_irele(struct xfs_scrub *sc, struct xfs_inode *ip);
15862306a36Sopenharmony_ciint xchk_install_handle_inode(struct xfs_scrub *sc, struct xfs_inode *ip);
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci/*
16162306a36Sopenharmony_ci * Don't bother cross-referencing if we already found corruption or cross
16262306a36Sopenharmony_ci * referencing discrepancies.
16362306a36Sopenharmony_ci */
16462306a36Sopenharmony_cistatic inline bool xchk_skip_xref(struct xfs_scrub_metadata *sm)
16562306a36Sopenharmony_ci{
16662306a36Sopenharmony_ci	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
16762306a36Sopenharmony_ci			       XFS_SCRUB_OFLAG_XCORRUPT);
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci#ifdef CONFIG_XFS_ONLINE_REPAIR
17162306a36Sopenharmony_ci/* Decide if a repair is required. */
17262306a36Sopenharmony_cistatic inline bool xchk_needs_repair(const struct xfs_scrub_metadata *sm)
17362306a36Sopenharmony_ci{
17462306a36Sopenharmony_ci	return sm->sm_flags & (XFS_SCRUB_OFLAG_CORRUPT |
17562306a36Sopenharmony_ci			       XFS_SCRUB_OFLAG_XCORRUPT |
17662306a36Sopenharmony_ci			       XFS_SCRUB_OFLAG_PREEN);
17762306a36Sopenharmony_ci}
17862306a36Sopenharmony_ci#else
17962306a36Sopenharmony_ci# define xchk_needs_repair(sc)		(false)
18062306a36Sopenharmony_ci#endif /* CONFIG_XFS_ONLINE_REPAIR */
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ciint xchk_metadata_inode_forks(struct xfs_scrub *sc);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci/*
18562306a36Sopenharmony_ci * Helper macros to allocate and format xfile description strings.
18662306a36Sopenharmony_ci * Callers must kfree the pointer returned.
18762306a36Sopenharmony_ci */
18862306a36Sopenharmony_ci#define xchk_xfile_descr(sc, fmt, ...) \
18962306a36Sopenharmony_ci	kasprintf(XCHK_GFP_FLAGS, "XFS (%s): " fmt, \
19062306a36Sopenharmony_ci			(sc)->mp->m_super->s_id, ##__VA_ARGS__)
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci/*
19362306a36Sopenharmony_ci * Setting up a hook to wait for intents to drain is costly -- we have to take
19462306a36Sopenharmony_ci * the CPU hotplug lock and force an i-cache flush on all CPUs once to set it
19562306a36Sopenharmony_ci * up, and again to tear it down.  These costs add up quickly, so we only want
19662306a36Sopenharmony_ci * to enable the drain waiter if the drain actually detected a conflict with
19762306a36Sopenharmony_ci * running intent chains.
19862306a36Sopenharmony_ci */
19962306a36Sopenharmony_cistatic inline bool xchk_need_intent_drain(struct xfs_scrub *sc)
20062306a36Sopenharmony_ci{
20162306a36Sopenharmony_ci	return sc->flags & XCHK_NEED_DRAIN;
20262306a36Sopenharmony_ci}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_civoid xchk_fsgates_enable(struct xfs_scrub *sc, unsigned int scrub_fshooks);
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciint xchk_inode_is_allocated(struct xfs_scrub *sc, xfs_agino_t agino,
20762306a36Sopenharmony_ci		bool *inuse);
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci#endif	/* __XFS_SCRUB_COMMON_H__ */
210