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_SCRUB_H__
762306a36Sopenharmony_ci#define __XFS_SCRUB_SCRUB_H__
862306a36Sopenharmony_ci
962306a36Sopenharmony_cistruct xfs_scrub;
1062306a36Sopenharmony_ci
1162306a36Sopenharmony_ci/*
1262306a36Sopenharmony_ci * Standard flags for allocating memory within scrub.  NOFS context is
1362306a36Sopenharmony_ci * configured by the process allocation scope.  Scrub and repair must be able
1462306a36Sopenharmony_ci * to back out gracefully if there isn't enough memory.  Force-cast to avoid
1562306a36Sopenharmony_ci * complaints from static checkers.
1662306a36Sopenharmony_ci */
1762306a36Sopenharmony_ci#define XCHK_GFP_FLAGS	((__force gfp_t)(GFP_KERNEL | __GFP_NOWARN | \
1862306a36Sopenharmony_ci					 __GFP_RETRY_MAYFAIL))
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Type info and names for the scrub types. */
2162306a36Sopenharmony_cienum xchk_type {
2262306a36Sopenharmony_ci	ST_NONE = 1,	/* disabled */
2362306a36Sopenharmony_ci	ST_PERAG,	/* per-AG metadata */
2462306a36Sopenharmony_ci	ST_FS,		/* per-FS metadata */
2562306a36Sopenharmony_ci	ST_INODE,	/* per-inode metadata */
2662306a36Sopenharmony_ci};
2762306a36Sopenharmony_ci
2862306a36Sopenharmony_cistruct xchk_meta_ops {
2962306a36Sopenharmony_ci	/* Acquire whatever resources are needed for the operation. */
3062306a36Sopenharmony_ci	int		(*setup)(struct xfs_scrub *sc);
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	/* Examine metadata for errors. */
3362306a36Sopenharmony_ci	int		(*scrub)(struct xfs_scrub *);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci	/* Repair or optimize the metadata. */
3662306a36Sopenharmony_ci	int		(*repair)(struct xfs_scrub *);
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci	/* Decide if we even have this piece of metadata. */
3962306a36Sopenharmony_ci	bool		(*has)(struct xfs_mount *);
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci	/* type describing required/allowed inputs */
4262306a36Sopenharmony_ci	enum xchk_type	type;
4362306a36Sopenharmony_ci};
4462306a36Sopenharmony_ci
4562306a36Sopenharmony_ci/* Buffer pointers and btree cursors for an entire AG. */
4662306a36Sopenharmony_cistruct xchk_ag {
4762306a36Sopenharmony_ci	struct xfs_perag	*pag;
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ci	/* AG btree roots */
5062306a36Sopenharmony_ci	struct xfs_buf		*agf_bp;
5162306a36Sopenharmony_ci	struct xfs_buf		*agi_bp;
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	/* AG btrees */
5462306a36Sopenharmony_ci	struct xfs_btree_cur	*bno_cur;
5562306a36Sopenharmony_ci	struct xfs_btree_cur	*cnt_cur;
5662306a36Sopenharmony_ci	struct xfs_btree_cur	*ino_cur;
5762306a36Sopenharmony_ci	struct xfs_btree_cur	*fino_cur;
5862306a36Sopenharmony_ci	struct xfs_btree_cur	*rmap_cur;
5962306a36Sopenharmony_ci	struct xfs_btree_cur	*refc_cur;
6062306a36Sopenharmony_ci};
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistruct xfs_scrub {
6362306a36Sopenharmony_ci	/* General scrub state. */
6462306a36Sopenharmony_ci	struct xfs_mount		*mp;
6562306a36Sopenharmony_ci	struct xfs_scrub_metadata	*sm;
6662306a36Sopenharmony_ci	const struct xchk_meta_ops	*ops;
6762306a36Sopenharmony_ci	struct xfs_trans		*tp;
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ci	/* File that scrub was called with. */
7062306a36Sopenharmony_ci	struct file			*file;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	/*
7362306a36Sopenharmony_ci	 * File that is undergoing the scrub operation.  This can differ from
7462306a36Sopenharmony_ci	 * the file that scrub was called with if we're checking file-based fs
7562306a36Sopenharmony_ci	 * metadata (e.g. rt bitmaps) or if we're doing a scrub-by-handle for
7662306a36Sopenharmony_ci	 * something that can't be opened directly (e.g. symlinks).
7762306a36Sopenharmony_ci	 */
7862306a36Sopenharmony_ci	struct xfs_inode		*ip;
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci	/* Kernel memory buffer used by scrubbers; freed at teardown. */
8162306a36Sopenharmony_ci	void				*buf;
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	/*
8462306a36Sopenharmony_ci	 * Clean up resources owned by whatever is in the buffer.  Cleanup can
8562306a36Sopenharmony_ci	 * be deferred with this hook as a means for scrub functions to pass
8662306a36Sopenharmony_ci	 * data to repair functions.  This function must not free the buffer
8762306a36Sopenharmony_ci	 * itself.
8862306a36Sopenharmony_ci	 */
8962306a36Sopenharmony_ci	void				(*buf_cleanup)(void *buf);
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	/* xfile used by the scrubbers; freed at teardown. */
9262306a36Sopenharmony_ci	struct xfile			*xfile;
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci	/* Lock flags for @ip. */
9562306a36Sopenharmony_ci	uint				ilock_flags;
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_ci	/* See the XCHK/XREP state flags below. */
9862306a36Sopenharmony_ci	unsigned int			flags;
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci	/*
10162306a36Sopenharmony_ci	 * The XFS_SICK_* flags that correspond to the metadata being scrubbed
10262306a36Sopenharmony_ci	 * or repaired.  We will use this mask to update the in-core fs health
10362306a36Sopenharmony_ci	 * status with whatever we find.
10462306a36Sopenharmony_ci	 */
10562306a36Sopenharmony_ci	unsigned int			sick_mask;
10662306a36Sopenharmony_ci
10762306a36Sopenharmony_ci	/* State tracking for single-AG operations. */
10862306a36Sopenharmony_ci	struct xchk_ag			sa;
10962306a36Sopenharmony_ci};
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci/* XCHK state flags grow up from zero, XREP state flags grown down from 2^31 */
11262306a36Sopenharmony_ci#define XCHK_TRY_HARDER		(1U << 0)  /* can't get resources, try again */
11362306a36Sopenharmony_ci#define XCHK_HAVE_FREEZE_PROT	(1U << 1)  /* do we have freeze protection? */
11462306a36Sopenharmony_ci#define XCHK_FSGATES_DRAIN	(1U << 2)  /* defer ops draining enabled */
11562306a36Sopenharmony_ci#define XCHK_NEED_DRAIN		(1U << 3)  /* scrub needs to drain defer ops */
11662306a36Sopenharmony_ci#define XREP_ALREADY_FIXED	(1U << 31) /* checking our repair work */
11762306a36Sopenharmony_ci
11862306a36Sopenharmony_ci/*
11962306a36Sopenharmony_ci * The XCHK_FSGATES* flags reflect functionality in the main filesystem that
12062306a36Sopenharmony_ci * are only enabled for this particular online fsck.  When not in use, the
12162306a36Sopenharmony_ci * features are gated off via dynamic code patching, which is why the state
12262306a36Sopenharmony_ci * must be enabled during scrub setup and can only be torn down afterwards.
12362306a36Sopenharmony_ci */
12462306a36Sopenharmony_ci#define XCHK_FSGATES_ALL	(XCHK_FSGATES_DRAIN)
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci/* Metadata scrubbers */
12762306a36Sopenharmony_ciint xchk_tester(struct xfs_scrub *sc);
12862306a36Sopenharmony_ciint xchk_superblock(struct xfs_scrub *sc);
12962306a36Sopenharmony_ciint xchk_agf(struct xfs_scrub *sc);
13062306a36Sopenharmony_ciint xchk_agfl(struct xfs_scrub *sc);
13162306a36Sopenharmony_ciint xchk_agi(struct xfs_scrub *sc);
13262306a36Sopenharmony_ciint xchk_bnobt(struct xfs_scrub *sc);
13362306a36Sopenharmony_ciint xchk_cntbt(struct xfs_scrub *sc);
13462306a36Sopenharmony_ciint xchk_inobt(struct xfs_scrub *sc);
13562306a36Sopenharmony_ciint xchk_finobt(struct xfs_scrub *sc);
13662306a36Sopenharmony_ciint xchk_rmapbt(struct xfs_scrub *sc);
13762306a36Sopenharmony_ciint xchk_refcountbt(struct xfs_scrub *sc);
13862306a36Sopenharmony_ciint xchk_inode(struct xfs_scrub *sc);
13962306a36Sopenharmony_ciint xchk_bmap_data(struct xfs_scrub *sc);
14062306a36Sopenharmony_ciint xchk_bmap_attr(struct xfs_scrub *sc);
14162306a36Sopenharmony_ciint xchk_bmap_cow(struct xfs_scrub *sc);
14262306a36Sopenharmony_ciint xchk_directory(struct xfs_scrub *sc);
14362306a36Sopenharmony_ciint xchk_xattr(struct xfs_scrub *sc);
14462306a36Sopenharmony_ciint xchk_symlink(struct xfs_scrub *sc);
14562306a36Sopenharmony_ciint xchk_parent(struct xfs_scrub *sc);
14662306a36Sopenharmony_ci#ifdef CONFIG_XFS_RT
14762306a36Sopenharmony_ciint xchk_rtbitmap(struct xfs_scrub *sc);
14862306a36Sopenharmony_ciint xchk_rtsummary(struct xfs_scrub *sc);
14962306a36Sopenharmony_ci#else
15062306a36Sopenharmony_cistatic inline int
15162306a36Sopenharmony_cixchk_rtbitmap(struct xfs_scrub *sc)
15262306a36Sopenharmony_ci{
15362306a36Sopenharmony_ci	return -ENOENT;
15462306a36Sopenharmony_ci}
15562306a36Sopenharmony_cistatic inline int
15662306a36Sopenharmony_cixchk_rtsummary(struct xfs_scrub *sc)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	return -ENOENT;
15962306a36Sopenharmony_ci}
16062306a36Sopenharmony_ci#endif
16162306a36Sopenharmony_ci#ifdef CONFIG_XFS_QUOTA
16262306a36Sopenharmony_ciint xchk_quota(struct xfs_scrub *sc);
16362306a36Sopenharmony_ci#else
16462306a36Sopenharmony_cistatic inline int
16562306a36Sopenharmony_cixchk_quota(struct xfs_scrub *sc)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	return -ENOENT;
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci#endif
17062306a36Sopenharmony_ciint xchk_fscounters(struct xfs_scrub *sc);
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci/* cross-referencing helpers */
17362306a36Sopenharmony_civoid xchk_xref_is_used_space(struct xfs_scrub *sc, xfs_agblock_t agbno,
17462306a36Sopenharmony_ci		xfs_extlen_t len);
17562306a36Sopenharmony_civoid xchk_xref_is_not_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
17662306a36Sopenharmony_ci		xfs_extlen_t len);
17762306a36Sopenharmony_civoid xchk_xref_is_inode_chunk(struct xfs_scrub *sc, xfs_agblock_t agbno,
17862306a36Sopenharmony_ci		xfs_extlen_t len);
17962306a36Sopenharmony_civoid xchk_xref_is_only_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
18062306a36Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo);
18162306a36Sopenharmony_civoid xchk_xref_is_not_owned_by(struct xfs_scrub *sc, xfs_agblock_t agbno,
18262306a36Sopenharmony_ci		xfs_extlen_t len, const struct xfs_owner_info *oinfo);
18362306a36Sopenharmony_civoid xchk_xref_has_no_owner(struct xfs_scrub *sc, xfs_agblock_t agbno,
18462306a36Sopenharmony_ci		xfs_extlen_t len);
18562306a36Sopenharmony_civoid xchk_xref_is_cow_staging(struct xfs_scrub *sc, xfs_agblock_t bno,
18662306a36Sopenharmony_ci		xfs_extlen_t len);
18762306a36Sopenharmony_civoid xchk_xref_is_not_shared(struct xfs_scrub *sc, xfs_agblock_t bno,
18862306a36Sopenharmony_ci		xfs_extlen_t len);
18962306a36Sopenharmony_civoid xchk_xref_is_not_cow_staging(struct xfs_scrub *sc, xfs_agblock_t bno,
19062306a36Sopenharmony_ci		xfs_extlen_t len);
19162306a36Sopenharmony_ci#ifdef CONFIG_XFS_RT
19262306a36Sopenharmony_civoid xchk_xref_is_used_rt_space(struct xfs_scrub *sc, xfs_rtblock_t rtbno,
19362306a36Sopenharmony_ci		xfs_extlen_t len);
19462306a36Sopenharmony_ci#else
19562306a36Sopenharmony_ci# define xchk_xref_is_used_rt_space(sc, rtbno, len) do { } while (0)
19662306a36Sopenharmony_ci#endif
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci#endif	/* __XFS_SCRUB_SCRUB_H__ */
199