xref: /freebsd/sys/contrib/openzfs/include/sys/range_tree.h (revision b59a0cde6a5253f94494397ce5b18dbfa071e08c)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or https://opensource.org/licenses/CDDL-1.0.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*
27  * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
28  */
29 
30 #ifndef _SYS_RANGE_TREE_H
31 #define	_SYS_RANGE_TREE_H
32 
33 #include <sys/btree.h>
34 #include <sys/dmu.h>
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #define	ZFS_RANGE_TREE_HISTOGRAM_SIZE	64
41 
42 typedef struct zfs_range_tree_ops zfs_range_tree_ops_t;
43 
44 typedef enum zfs_range_seg_type {
45 	ZFS_RANGE_SEG32,
46 	ZFS_RANGE_SEG64,
47 	ZFS_RANGE_SEG_GAP,
48 	ZFS_RANGE_SEG_NUM_TYPES,
49 } zfs_range_seg_type_t;
50 
51 /*
52  * Note: the range_tree may not be accessed concurrently; consumers
53  * must provide external locking if required.
54  */
55 typedef struct zfs_range_tree {
56 	zfs_btree_t	rt_root;	/* offset-ordered segment b-tree */
57 	uint64_t	rt_space;	/* sum of all segments in the map */
58 	zfs_range_seg_type_t rt_type;	/* type of zfs_range_seg_t in use */
59 	/*
60 	 * All data that is stored in the range tree must have a start higher
61 	 * than or equal to rt_start, and all sizes and offsets must be
62 	 * multiples of 1 << rt_shift.
63 	 */
64 	uint8_t		rt_shift;
65 	uint64_t	rt_start;
66 	const zfs_range_tree_ops_t *rt_ops;
67 	void		*rt_arg;
68 	uint64_t	rt_gap;		/* allowable inter-segment gap */
69 
70 	/*
71 	 * The rt_histogram maintains a histogram of ranges. Each bucket,
72 	 * rt_histogram[i], contains the number of ranges whose size is:
73 	 * 2^i <= size of range in bytes < 2^(i+1)
74 	 */
75 	uint64_t	rt_histogram[ZFS_RANGE_TREE_HISTOGRAM_SIZE];
76 } zfs_range_tree_t;
77 
78 typedef struct zfs_range_seg32 {
79 	uint32_t	rs_start;	/* starting offset of this segment */
80 	uint32_t	rs_end;		/* ending offset (non-inclusive) */
81 } zfs_range_seg32_t;
82 
83 /*
84  * Extremely large metaslabs, vdev-wide trees, and dnode-wide trees may
85  * require 64-bit integers for ranges.
86  */
87 typedef struct zfs_range_seg64 {
88 	uint64_t	rs_start;	/* starting offset of this segment */
89 	uint64_t	rs_end;		/* ending offset (non-inclusive) */
90 } zfs_range_seg64_t;
91 
92 typedef struct zfs_range_seg_gap {
93 	uint64_t	rs_start;	/* starting offset of this segment */
94 	uint64_t	rs_end;		/* ending offset (non-inclusive) */
95 	uint64_t	rs_fill;	/* actual fill if gap mode is on */
96 } zfs_range_seg_gap_t;
97 
98 /*
99  * This type needs to be the largest of the range segs, since it will be stack
100  * allocated and then cast the actual type to do tree operations.
101  */
102 typedef zfs_range_seg_gap_t zfs_range_seg_max_t;
103 
104 /*
105  * This is just for clarity of code purposes, so we can make it clear that a
106  * pointer is to a range seg of some type; when we need to do the actual math,
107  * we'll figure out the real type.
108  */
109 typedef void zfs_range_seg_t;
110 
111 struct zfs_range_tree_ops {
112 	void    (*rtop_create)(zfs_range_tree_t *rt, void *arg);
113 	void    (*rtop_destroy)(zfs_range_tree_t *rt, void *arg);
114 	void	(*rtop_add)(zfs_range_tree_t *rt, void *rs, void *arg);
115 	void    (*rtop_remove)(zfs_range_tree_t *rt, void *rs, void *arg);
116 	void	(*rtop_vacate)(zfs_range_tree_t *rt, void *arg);
117 };
118 
119 static inline uint64_t
zfs_rs_get_start_raw(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)120 zfs_rs_get_start_raw(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
121 {
122 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
123 	switch (rt->rt_type) {
124 	case ZFS_RANGE_SEG32:
125 		return (((const zfs_range_seg32_t *)rs)->rs_start);
126 	case ZFS_RANGE_SEG64:
127 		return (((const zfs_range_seg64_t *)rs)->rs_start);
128 	case ZFS_RANGE_SEG_GAP:
129 		return (((const zfs_range_seg_gap_t *)rs)->rs_start);
130 	default:
131 		VERIFY(0);
132 		return (0);
133 	}
134 }
135 
136 static inline uint64_t
zfs_rs_get_end_raw(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)137 zfs_rs_get_end_raw(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
138 {
139 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
140 	switch (rt->rt_type) {
141 	case ZFS_RANGE_SEG32:
142 		return (((const zfs_range_seg32_t *)rs)->rs_end);
143 	case ZFS_RANGE_SEG64:
144 		return (((const zfs_range_seg64_t *)rs)->rs_end);
145 	case ZFS_RANGE_SEG_GAP:
146 		return (((const zfs_range_seg_gap_t *)rs)->rs_end);
147 	default:
148 		VERIFY(0);
149 		return (0);
150 	}
151 }
152 
153 static inline uint64_t
zfs_rs_get_fill_raw(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)154 zfs_rs_get_fill_raw(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
155 {
156 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
157 	switch (rt->rt_type) {
158 	case ZFS_RANGE_SEG32: {
159 		const zfs_range_seg32_t *r32 = (const zfs_range_seg32_t *)rs;
160 		return (r32->rs_end - r32->rs_start);
161 	}
162 	case ZFS_RANGE_SEG64: {
163 		const zfs_range_seg64_t *r64 = (const zfs_range_seg64_t *)rs;
164 		return (r64->rs_end - r64->rs_start);
165 	}
166 	case ZFS_RANGE_SEG_GAP:
167 		return (((const zfs_range_seg_gap_t *)rs)->rs_fill);
168 	default:
169 		VERIFY(0);
170 		return (0);
171 	}
172 
173 }
174 
175 static inline uint64_t
zfs_rs_get_start(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)176 zfs_rs_get_start(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
177 {
178 	return ((zfs_rs_get_start_raw(rs, rt) << rt->rt_shift) + rt->rt_start);
179 }
180 
181 static inline uint64_t
zfs_rs_get_end(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)182 zfs_rs_get_end(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
183 {
184 	return ((zfs_rs_get_end_raw(rs, rt) << rt->rt_shift) + rt->rt_start);
185 }
186 
187 static inline uint64_t
zfs_rs_get_fill(const zfs_range_seg_t * rs,const zfs_range_tree_t * rt)188 zfs_rs_get_fill(const zfs_range_seg_t *rs, const zfs_range_tree_t *rt)
189 {
190 	return (zfs_rs_get_fill_raw(rs, rt) << rt->rt_shift);
191 }
192 
193 static inline void
zfs_rs_set_start_raw(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t start)194 zfs_rs_set_start_raw(zfs_range_seg_t *rs, zfs_range_tree_t *rt, uint64_t start)
195 {
196 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
197 	switch (rt->rt_type) {
198 	case ZFS_RANGE_SEG32:
199 		ASSERT3U(start, <=, UINT32_MAX);
200 		((zfs_range_seg32_t *)rs)->rs_start = (uint32_t)start;
201 		break;
202 	case ZFS_RANGE_SEG64:
203 		((zfs_range_seg64_t *)rs)->rs_start = start;
204 		break;
205 	case ZFS_RANGE_SEG_GAP:
206 		((zfs_range_seg_gap_t *)rs)->rs_start = start;
207 		break;
208 	default:
209 		VERIFY(0);
210 	}
211 }
212 
213 static inline void
zfs_rs_set_end_raw(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t end)214 zfs_rs_set_end_raw(zfs_range_seg_t *rs, zfs_range_tree_t *rt, uint64_t end)
215 {
216 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
217 	switch (rt->rt_type) {
218 	case ZFS_RANGE_SEG32:
219 		ASSERT3U(end, <=, UINT32_MAX);
220 		((zfs_range_seg32_t *)rs)->rs_end = (uint32_t)end;
221 		break;
222 	case ZFS_RANGE_SEG64:
223 		((zfs_range_seg64_t *)rs)->rs_end = end;
224 		break;
225 	case ZFS_RANGE_SEG_GAP:
226 		((zfs_range_seg_gap_t *)rs)->rs_end = end;
227 		break;
228 	default:
229 		VERIFY(0);
230 	}
231 }
232 
233 static inline void
zfs_zfs_rs_set_fill_raw(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t fill)234 zfs_zfs_rs_set_fill_raw(zfs_range_seg_t *rs, zfs_range_tree_t *rt,
235     uint64_t fill)
236 {
237 	ASSERT3U(rt->rt_type, <=, ZFS_RANGE_SEG_NUM_TYPES);
238 	switch (rt->rt_type) {
239 	case ZFS_RANGE_SEG32:
240 		/* fall through */
241 	case ZFS_RANGE_SEG64:
242 		ASSERT3U(fill, ==, zfs_rs_get_end_raw(rs, rt) -
243 		    zfs_rs_get_start_raw(rs, rt));
244 		break;
245 	case ZFS_RANGE_SEG_GAP:
246 		((zfs_range_seg_gap_t *)rs)->rs_fill = fill;
247 		break;
248 	default:
249 		VERIFY(0);
250 	}
251 }
252 
253 static inline void
zfs_rs_set_start(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t start)254 zfs_rs_set_start(zfs_range_seg_t *rs, zfs_range_tree_t *rt, uint64_t start)
255 {
256 	ASSERT3U(start, >=, rt->rt_start);
257 	ASSERT(IS_P2ALIGNED(start, 1ULL << rt->rt_shift));
258 	zfs_rs_set_start_raw(rs, rt, (start - rt->rt_start) >> rt->rt_shift);
259 }
260 
261 static inline void
zfs_rs_set_end(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t end)262 zfs_rs_set_end(zfs_range_seg_t *rs, zfs_range_tree_t *rt, uint64_t end)
263 {
264 	ASSERT3U(end, >=, rt->rt_start);
265 	ASSERT(IS_P2ALIGNED(end, 1ULL << rt->rt_shift));
266 	zfs_rs_set_end_raw(rs, rt, (end - rt->rt_start) >> rt->rt_shift);
267 }
268 
269 static inline void
zfs_rs_set_fill(zfs_range_seg_t * rs,zfs_range_tree_t * rt,uint64_t fill)270 zfs_rs_set_fill(zfs_range_seg_t *rs, zfs_range_tree_t *rt, uint64_t fill)
271 {
272 	ASSERT(IS_P2ALIGNED(fill, 1ULL << rt->rt_shift));
273 	zfs_zfs_rs_set_fill_raw(rs, rt, fill >> rt->rt_shift);
274 }
275 
276 typedef void zfs_range_tree_func_t(void *arg, uint64_t start, uint64_t size);
277 
278 zfs_range_tree_t *zfs_range_tree_create_gap(const zfs_range_tree_ops_t *ops,
279     zfs_range_seg_type_t type, void *arg, uint64_t start, uint64_t shift,
280     uint64_t gap);
281 zfs_range_tree_t *zfs_range_tree_create(const zfs_range_tree_ops_t *ops,
282     zfs_range_seg_type_t type, void *arg, uint64_t start, uint64_t shift);
283 void zfs_range_tree_destroy(zfs_range_tree_t *rt);
284 boolean_t zfs_range_tree_contains(zfs_range_tree_t *rt, uint64_t start,
285     uint64_t size);
286 zfs_range_seg_t *zfs_range_tree_find(zfs_range_tree_t *rt, uint64_t start,
287     uint64_t size);
288 boolean_t zfs_range_tree_find_in(zfs_range_tree_t *rt, uint64_t start,
289     uint64_t size, uint64_t *ostart, uint64_t *osize);
290 void zfs_range_tree_verify_not_present(zfs_range_tree_t *rt,
291     uint64_t start, uint64_t size);
292 void zfs_range_tree_resize_segment(zfs_range_tree_t *rt, zfs_range_seg_t *rs,
293     uint64_t newstart, uint64_t newsize);
294 uint64_t zfs_range_tree_space(zfs_range_tree_t *rt);
295 uint64_t zfs_range_tree_numsegs(zfs_range_tree_t *rt);
296 boolean_t zfs_range_tree_is_empty(zfs_range_tree_t *rt);
297 void zfs_range_tree_swap(zfs_range_tree_t **rtsrc, zfs_range_tree_t **rtdst);
298 void zfs_range_tree_stat_verify(zfs_range_tree_t *rt);
299 uint64_t zfs_range_tree_min(zfs_range_tree_t *rt);
300 uint64_t zfs_range_tree_max(zfs_range_tree_t *rt);
301 uint64_t zfs_range_tree_span(zfs_range_tree_t *rt);
302 
303 void zfs_range_tree_add(void *arg, uint64_t start, uint64_t size);
304 void zfs_range_tree_remove(void *arg, uint64_t start, uint64_t size);
305 void zfs_range_tree_remove_fill(zfs_range_tree_t *rt, uint64_t start,
306     uint64_t size);
307 void zfs_range_tree_adjust_fill(zfs_range_tree_t *rt, zfs_range_seg_t *rs,
308     int64_t delta);
309 void zfs_range_tree_clear(zfs_range_tree_t *rt, uint64_t start, uint64_t size);
310 
311 void zfs_range_tree_vacate(zfs_range_tree_t *rt, zfs_range_tree_func_t *func,
312     void *arg);
313 void zfs_range_tree_walk(zfs_range_tree_t *rt, zfs_range_tree_func_t *func,
314     void *arg);
315 zfs_range_seg_t *zfs_range_tree_first(zfs_range_tree_t *rt);
316 
317 void zfs_range_tree_remove_xor_add_segment(uint64_t start, uint64_t end,
318     zfs_range_tree_t *removefrom, zfs_range_tree_t *addto);
319 void zfs_range_tree_remove_xor_add(zfs_range_tree_t *rt,
320     zfs_range_tree_t *removefrom, zfs_range_tree_t *addto);
321 
322 #ifdef	__cplusplus
323 }
324 #endif
325 
326 #endif	/* _SYS_RANGE_TREE_H */
327