1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2017-2023 Oracle.  All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
5 */
6#ifndef __XFS_SCRUB_BTREE_H__
7#define __XFS_SCRUB_BTREE_H__
8
9/* btree scrub */
10
11/* Check for btree operation errors. */
12bool xchk_btree_process_error(struct xfs_scrub *sc,
13		struct xfs_btree_cur *cur, int level, int *error);
14
15/* Check for btree xref operation errors. */
16bool xchk_btree_xref_process_error(struct xfs_scrub *sc,
17		struct xfs_btree_cur *cur, int level, int *error);
18
19/* Check for btree corruption. */
20void xchk_btree_set_corrupt(struct xfs_scrub *sc,
21		struct xfs_btree_cur *cur, int level);
22void xchk_btree_set_preen(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
23		int level);
24
25/* Check for btree xref discrepancies. */
26void xchk_btree_xref_set_corrupt(struct xfs_scrub *sc,
27		struct xfs_btree_cur *cur, int level);
28
29struct xchk_btree;
30typedef int (*xchk_btree_rec_fn)(
31	struct xchk_btree		*bs,
32	const union xfs_btree_rec	*rec);
33
34struct xchk_btree_key {
35	union xfs_btree_key		key;
36	bool				valid;
37};
38
39struct xchk_btree {
40	/* caller-provided scrub state */
41	struct xfs_scrub		*sc;
42	struct xfs_btree_cur		*cur;
43	xchk_btree_rec_fn		scrub_rec;
44	const struct xfs_owner_info	*oinfo;
45	void				*private;
46
47	/* internal scrub state */
48	bool				lastrec_valid;
49	union xfs_btree_rec		lastrec;
50	struct list_head		to_check;
51
52	/* this element must come last! */
53	struct xchk_btree_key		lastkey[];
54};
55
56/*
57 * Calculate the size of a xchk_btree structure.  There are nlevels-1 slots for
58 * keys because we track leaf records separately in lastrec.
59 */
60static inline size_t
61xchk_btree_sizeof(unsigned int nlevels)
62{
63	return struct_size_t(struct xchk_btree, lastkey, nlevels - 1);
64}
65
66int xchk_btree(struct xfs_scrub *sc, struct xfs_btree_cur *cur,
67		xchk_btree_rec_fn scrub_fn, const struct xfs_owner_info *oinfo,
68		void *private);
69
70#endif /* __XFS_SCRUB_BTREE_H__ */
71