xref: /openbmc/linux/fs/xfs/scrub/bitmap.h (revision 9b68f30b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2018-2023 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <djwong@kernel.org>
5  */
6 #ifndef __XFS_SCRUB_BITMAP_H__
7 #define __XFS_SCRUB_BITMAP_H__
8 
9 struct xbitmap {
10 	struct rb_root_cached	xb_root;
11 };
12 
13 void xbitmap_init(struct xbitmap *bitmap);
14 void xbitmap_destroy(struct xbitmap *bitmap);
15 
16 int xbitmap_clear(struct xbitmap *bitmap, uint64_t start, uint64_t len);
17 int xbitmap_set(struct xbitmap *bitmap, uint64_t start, uint64_t len);
18 int xbitmap_disunion(struct xbitmap *bitmap, struct xbitmap *sub);
19 int xbitmap_set_btcur_path(struct xbitmap *bitmap,
20 		struct xfs_btree_cur *cur);
21 int xbitmap_set_btblocks(struct xbitmap *bitmap,
22 		struct xfs_btree_cur *cur);
23 uint64_t xbitmap_hweight(struct xbitmap *bitmap);
24 
25 /*
26  * Return codes for the bitmap iterator functions are 0 to continue iterating,
27  * and non-zero to stop iterating.  Any non-zero value will be passed up to the
28  * iteration caller.  The special value -ECANCELED can be used to stop
29  * iteration, because neither bitmap iterator ever generates that error code on
30  * its own.  Callers must not modify the bitmap while walking it.
31  */
32 typedef int (*xbitmap_walk_fn)(uint64_t start, uint64_t len, void *priv);
33 int xbitmap_walk(struct xbitmap *bitmap, xbitmap_walk_fn fn,
34 		void *priv);
35 
36 typedef int (*xbitmap_walk_bits_fn)(uint64_t bit, void *priv);
37 int xbitmap_walk_bits(struct xbitmap *bitmap, xbitmap_walk_bits_fn fn,
38 		void *priv);
39 
40 bool xbitmap_empty(struct xbitmap *bitmap);
41 bool xbitmap_test(struct xbitmap *bitmap, uint64_t start, uint64_t *len);
42 
43 /* Bitmaps, but for type-checked for xfs_agblock_t */
44 
45 struct xagb_bitmap {
46 	struct xbitmap	agbitmap;
47 };
48 
49 static inline void xagb_bitmap_init(struct xagb_bitmap *bitmap)
50 {
51 	xbitmap_init(&bitmap->agbitmap);
52 }
53 
54 static inline void xagb_bitmap_destroy(struct xagb_bitmap *bitmap)
55 {
56 	xbitmap_destroy(&bitmap->agbitmap);
57 }
58 
59 static inline int xagb_bitmap_clear(struct xagb_bitmap *bitmap,
60 		xfs_agblock_t start, xfs_extlen_t len)
61 {
62 	return xbitmap_clear(&bitmap->agbitmap, start, len);
63 }
64 static inline int xagb_bitmap_set(struct xagb_bitmap *bitmap,
65 		xfs_agblock_t start, xfs_extlen_t len)
66 {
67 	return xbitmap_set(&bitmap->agbitmap, start, len);
68 }
69 
70 static inline bool
71 xagb_bitmap_test(
72 	struct xagb_bitmap	*bitmap,
73 	xfs_agblock_t		start,
74 	xfs_extlen_t		*len)
75 {
76 	uint64_t		biglen = *len;
77 	bool			ret;
78 
79 	ret = xbitmap_test(&bitmap->agbitmap, start, &biglen);
80 
81 	if (start + biglen >= UINT_MAX) {
82 		ASSERT(0);
83 		biglen = UINT_MAX - start;
84 	}
85 
86 	*len = biglen;
87 	return ret;
88 }
89 
90 static inline int xagb_bitmap_disunion(struct xagb_bitmap *bitmap,
91 		struct xagb_bitmap *sub)
92 {
93 	return xbitmap_disunion(&bitmap->agbitmap, &sub->agbitmap);
94 }
95 
96 static inline uint32_t xagb_bitmap_hweight(struct xagb_bitmap *bitmap)
97 {
98 	return xbitmap_hweight(&bitmap->agbitmap);
99 }
100 static inline bool xagb_bitmap_empty(struct xagb_bitmap *bitmap)
101 {
102 	return xbitmap_empty(&bitmap->agbitmap);
103 }
104 
105 static inline int xagb_bitmap_walk(struct xagb_bitmap *bitmap,
106 		xbitmap_walk_fn fn, void *priv)
107 {
108 	return xbitmap_walk(&bitmap->agbitmap, fn, priv);
109 }
110 
111 int xagb_bitmap_set_btblocks(struct xagb_bitmap *bitmap,
112 		struct xfs_btree_cur *cur);
113 
114 #endif	/* __XFS_SCRUB_BITMAP_H__ */
115