Line data Source code
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 : uint64_t xbitmap_hweight(struct xbitmap *bitmap);
20 :
21 : /*
22 : * Return codes for the bitmap iterator functions are 0 to continue iterating,
23 : * and non-zero to stop iterating. Any non-zero value will be passed up to the
24 : * iteration caller. The special value -ECANCELED can be used to stop
25 : * iteration, because neither bitmap iterator ever generates that error code on
26 : * its own. Callers must not modify the bitmap while walking it.
27 : */
28 : typedef int (*xbitmap_walk_fn)(uint64_t start, uint64_t len, void *priv);
29 : int xbitmap_walk(struct xbitmap *bitmap, xbitmap_walk_fn fn,
30 : void *priv);
31 :
32 : bool xbitmap_empty(struct xbitmap *bitmap);
33 : bool xbitmap_test(struct xbitmap *bitmap, uint64_t start, uint64_t *len);
34 : uint64_t xbitmap_count_set_regions(struct xbitmap *bitmap);
35 :
36 : int xbitmap_take_first_set(struct xbitmap *bitmap, uint64_t start,
37 : uint64_t last, uint64_t *valp);
38 :
39 : /* Bitmaps, but for type-checked for xfs_agblock_t */
40 :
41 : struct xagb_bitmap {
42 : struct xbitmap agbitmap;
43 : };
44 :
45 : static inline void xagb_bitmap_init(struct xagb_bitmap *bitmap)
46 : {
47 28129332 : xbitmap_init(&bitmap->agbitmap);
48 31946 : }
49 :
50 : static inline void xagb_bitmap_destroy(struct xagb_bitmap *bitmap)
51 : {
52 28161199 : xbitmap_destroy(&bitmap->agbitmap);
53 : }
54 :
55 : static inline int xagb_bitmap_clear(struct xagb_bitmap *bitmap,
56 : xfs_agblock_t start, xfs_extlen_t len)
57 : {
58 17559982 : return xbitmap_clear(&bitmap->agbitmap, start, len);
59 : }
60 : static inline int xagb_bitmap_set(struct xagb_bitmap *bitmap,
61 : xfs_agblock_t start, xfs_extlen_t len)
62 : {
63 111238012 : return xbitmap_set(&bitmap->agbitmap, start, len);
64 : }
65 :
66 : static inline bool
67 7777367 : xagb_bitmap_test(
68 : struct xagb_bitmap *bitmap,
69 : xfs_agblock_t start,
70 : xfs_extlen_t *len)
71 : {
72 7777367 : uint64_t biglen = *len;
73 7777367 : bool ret;
74 :
75 7777367 : ret = xbitmap_test(&bitmap->agbitmap, start, &biglen);
76 :
77 7777975 : if (start + biglen >= UINT_MAX) {
78 0 : ASSERT(0);
79 0 : biglen = UINT_MAX - start;
80 : }
81 :
82 7777975 : *len = biglen;
83 7777975 : return ret;
84 : }
85 :
86 : static inline int xagb_bitmap_disunion(struct xagb_bitmap *bitmap,
87 : struct xagb_bitmap *sub)
88 : {
89 813876 : return xbitmap_disunion(&bitmap->agbitmap, &sub->agbitmap);
90 : }
91 :
92 : static inline uint32_t xagb_bitmap_count_set_regions(struct xagb_bitmap *bitmap)
93 : {
94 32767 : return xbitmap_count_set_regions(&bitmap->agbitmap);
95 : }
96 : static inline uint32_t xagb_bitmap_hweight(struct xagb_bitmap *bitmap)
97 : {
98 563706 : return xbitmap_hweight(&bitmap->agbitmap);
99 : }
100 : static inline bool xagb_bitmap_empty(struct xagb_bitmap *bitmap)
101 : {
102 32276 : 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 27744468 : 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 : int xagb_bitmap_set_btcur_path(struct xagb_bitmap *bitmap,
114 : struct xfs_btree_cur *cur);
115 :
116 : /* Bitmaps, but for type-checked for xfs_rgblock_t */
117 :
118 : struct xrgb_bitmap {
119 : struct xbitmap rgbitmap;
120 : };
121 :
122 : static inline void xrgb_bitmap_init(struct xrgb_bitmap *bitmap)
123 : {
124 11130 : xbitmap_init(&bitmap->rgbitmap);
125 : }
126 :
127 : static inline void xrgb_bitmap_destroy(struct xrgb_bitmap *bitmap)
128 : {
129 11129 : xbitmap_destroy(&bitmap->rgbitmap);
130 : }
131 :
132 : static inline int xrgb_bitmap_set(struct xrgb_bitmap *bitmap,
133 : xfs_rgblock_t start, xfs_extlen_t len)
134 : {
135 216969 : return xbitmap_set(&bitmap->rgbitmap, start, len);
136 : }
137 :
138 : static inline int xrgb_bitmap_walk(struct xrgb_bitmap *bitmap,
139 : xbitmap_walk_fn fn, void *priv)
140 : {
141 11129 : return xbitmap_walk(&bitmap->rgbitmap, fn, priv);
142 : }
143 :
144 : /* Bitmaps, but for type-checked for xfs_fsblock_t */
145 :
146 : struct xfsb_bitmap {
147 : struct xbitmap fsbitmap;
148 : };
149 :
150 : static inline void xfsb_bitmap_init(struct xfsb_bitmap *bitmap)
151 : {
152 3442704 : xbitmap_init(&bitmap->fsbitmap);
153 65382 : }
154 :
155 : static inline void xfsb_bitmap_destroy(struct xfsb_bitmap *bitmap)
156 : {
157 3445279 : xbitmap_destroy(&bitmap->fsbitmap);
158 65382 : }
159 :
160 : static inline int xfsb_bitmap_set(struct xfsb_bitmap *bitmap,
161 : xfs_fsblock_t start, xfs_filblks_t len)
162 : {
163 255324 : return xbitmap_set(&bitmap->fsbitmap, start, len);
164 : }
165 :
166 : static inline int xfsb_bitmap_walk(struct xfsb_bitmap *bitmap,
167 : xbitmap_walk_fn fn, void *priv)
168 : {
169 3445209 : return xbitmap_walk(&bitmap->fsbitmap, fn, priv);
170 : }
171 :
172 : /* Bitmaps, but for type-checked for xfs_ino_t */
173 :
174 : struct xino_bitmap {
175 : struct xbitmap inobitmap;
176 : };
177 :
178 : static inline void xino_bitmap_init(struct xino_bitmap *bitmap)
179 : {
180 18528851 : xbitmap_init(&bitmap->inobitmap);
181 : }
182 :
183 : static inline void xino_bitmap_destroy(struct xino_bitmap *bitmap)
184 : {
185 18806785 : xbitmap_destroy(&bitmap->inobitmap);
186 : }
187 :
188 : static inline int xino_bitmap_set(struct xino_bitmap *bitmap, xfs_ino_t ino)
189 : {
190 1545999291 : return xbitmap_set(&bitmap->inobitmap, ino, 1);
191 : }
192 :
193 : static inline int xino_bitmap_test(struct xino_bitmap *bitmap, xfs_ino_t ino)
194 : {
195 1578097289 : uint64_t len = 1;
196 :
197 1578097289 : return xbitmap_test(&bitmap->inobitmap, ino, &len);
198 : }
199 :
200 : /* Bitmaps, but for type-checked for xfs_rtblock_t */
201 :
202 : struct xrtb_bitmap {
203 : struct xbitmap rtbitmap;
204 : };
205 :
206 : static inline void xrtb_bitmap_init(struct xrtb_bitmap *bitmap)
207 : {
208 49018 : xbitmap_init(&bitmap->rtbitmap);
209 49021 : }
210 :
211 : static inline void xrtb_bitmap_destroy(struct xrtb_bitmap *bitmap)
212 : {
213 48997 : xbitmap_destroy(&bitmap->rtbitmap);
214 48995 : }
215 :
216 : static inline int xrtb_bitmap_set(struct xrtb_bitmap *bitmap,
217 : xfs_rtblock_t start, xfs_filblks_t len)
218 : {
219 5471 : return xbitmap_set(&bitmap->rtbitmap, start, len);
220 : }
221 :
222 : static inline int xrtb_bitmap_walk(struct xrtb_bitmap *bitmap,
223 : xbitmap_walk_fn fn, void *priv)
224 : {
225 49015 : return xbitmap_walk(&bitmap->rtbitmap, fn, priv);
226 : }
227 :
228 : #endif /* __XFS_SCRUB_BITMAP_H__ */
|