xref: /freebsd-src/sys/contrib/openzfs/module/zfs/range_tree.c (revision 4e8d558c9d1cf3e7e424e3fb123b01979c3d57f2)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy /*
22eda14cbcSMatt Macy  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23eda14cbcSMatt Macy  * Use is subject to license terms.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy /*
26eda14cbcSMatt Macy  * Copyright (c) 2013, 2019 by Delphix. All rights reserved.
272c48331dSMatt Macy  * Copyright (c) 2015, Nexenta Systems, Inc. All rights reserved.
28eda14cbcSMatt Macy  */
29eda14cbcSMatt Macy 
30eda14cbcSMatt Macy #include <sys/zfs_context.h>
31eda14cbcSMatt Macy #include <sys/spa.h>
32eda14cbcSMatt Macy #include <sys/dmu.h>
33eda14cbcSMatt Macy #include <sys/dnode.h>
34eda14cbcSMatt Macy #include <sys/zio.h>
35eda14cbcSMatt Macy #include <sys/range_tree.h>
36eda14cbcSMatt Macy 
37eda14cbcSMatt Macy /*
38eda14cbcSMatt Macy  * Range trees are tree-based data structures that can be used to
39eda14cbcSMatt Macy  * track free space or generally any space allocation information.
40eda14cbcSMatt Macy  * A range tree keeps track of individual segments and automatically
41eda14cbcSMatt Macy  * provides facilities such as adjacent extent merging and extent
42eda14cbcSMatt Macy  * splitting in response to range add/remove requests.
43eda14cbcSMatt Macy  *
44eda14cbcSMatt Macy  * A range tree starts out completely empty, with no segments in it.
45eda14cbcSMatt Macy  * Adding an allocation via range_tree_add to the range tree can either:
46eda14cbcSMatt Macy  * 1) create a new extent
47eda14cbcSMatt Macy  * 2) extend an adjacent extent
48eda14cbcSMatt Macy  * 3) merge two adjacent extents
49eda14cbcSMatt Macy  * Conversely, removing an allocation via range_tree_remove can:
50eda14cbcSMatt Macy  * 1) completely remove an extent
51eda14cbcSMatt Macy  * 2) shorten an extent (if the allocation was near one of its ends)
52eda14cbcSMatt Macy  * 3) split an extent into two extents, in effect punching a hole
53eda14cbcSMatt Macy  *
54eda14cbcSMatt Macy  * A range tree is also capable of 'bridging' gaps when adding
55eda14cbcSMatt Macy  * allocations. This is useful for cases when close proximity of
56eda14cbcSMatt Macy  * allocations is an important detail that needs to be represented
57eda14cbcSMatt Macy  * in the range tree. See range_tree_set_gap(). The default behavior
58eda14cbcSMatt Macy  * is not to bridge gaps (i.e. the maximum allowed gap size is 0).
59eda14cbcSMatt Macy  *
60eda14cbcSMatt Macy  * In order to traverse a range tree, use either the range_tree_walk()
61eda14cbcSMatt Macy  * or range_tree_vacate() functions.
62eda14cbcSMatt Macy  *
63eda14cbcSMatt Macy  * To obtain more accurate information on individual segment
64eda14cbcSMatt Macy  * operations that the range tree performs "under the hood", you can
65eda14cbcSMatt Macy  * specify a set of callbacks by passing a range_tree_ops_t structure
66eda14cbcSMatt Macy  * to the range_tree_create function. Any callbacks that are non-NULL
67eda14cbcSMatt Macy  * are then called at the appropriate times.
68eda14cbcSMatt Macy  *
69eda14cbcSMatt Macy  * The range tree code also supports a special variant of range trees
70eda14cbcSMatt Macy  * that can bridge small gaps between segments. This kind of tree is used
71eda14cbcSMatt Macy  * by the dsl scanning code to group I/Os into mostly sequential chunks to
72eda14cbcSMatt Macy  * optimize disk performance. The code here attempts to do this with as
73eda14cbcSMatt Macy  * little memory and computational overhead as possible. One limitation of
74eda14cbcSMatt Macy  * this implementation is that segments of range trees with gaps can only
75eda14cbcSMatt Macy  * support removing complete segments.
76eda14cbcSMatt Macy  */
77eda14cbcSMatt Macy 
78eda14cbcSMatt Macy static inline void
rs_copy(range_seg_t * src,range_seg_t * dest,range_tree_t * rt)79eda14cbcSMatt Macy rs_copy(range_seg_t *src, range_seg_t *dest, range_tree_t *rt)
80eda14cbcSMatt Macy {
81da5137abSMartin Matuska 	ASSERT3U(rt->rt_type, <, RANGE_SEG_NUM_TYPES);
82eda14cbcSMatt Macy 	size_t size = 0;
83eda14cbcSMatt Macy 	switch (rt->rt_type) {
84eda14cbcSMatt Macy 	case RANGE_SEG32:
85eda14cbcSMatt Macy 		size = sizeof (range_seg32_t);
86eda14cbcSMatt Macy 		break;
87eda14cbcSMatt Macy 	case RANGE_SEG64:
88eda14cbcSMatt Macy 		size = sizeof (range_seg64_t);
89eda14cbcSMatt Macy 		break;
90eda14cbcSMatt Macy 	case RANGE_SEG_GAP:
91eda14cbcSMatt Macy 		size = sizeof (range_seg_gap_t);
92eda14cbcSMatt Macy 		break;
93eda14cbcSMatt Macy 	default:
94da5137abSMartin Matuska 		__builtin_unreachable();
95eda14cbcSMatt Macy 	}
96da5137abSMartin Matuska 	memcpy(dest, src, size);
97eda14cbcSMatt Macy }
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy void
range_tree_stat_verify(range_tree_t * rt)100eda14cbcSMatt Macy range_tree_stat_verify(range_tree_t *rt)
101eda14cbcSMatt Macy {
102eda14cbcSMatt Macy 	range_seg_t *rs;
103eda14cbcSMatt Macy 	zfs_btree_index_t where;
104eda14cbcSMatt Macy 	uint64_t hist[RANGE_TREE_HISTOGRAM_SIZE] = { 0 };
105eda14cbcSMatt Macy 	int i;
106eda14cbcSMatt Macy 
107eda14cbcSMatt Macy 	for (rs = zfs_btree_first(&rt->rt_root, &where); rs != NULL;
108eda14cbcSMatt Macy 	    rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
109eda14cbcSMatt Macy 		uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt);
110eda14cbcSMatt Macy 		int idx	= highbit64(size) - 1;
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 		hist[idx]++;
113eda14cbcSMatt Macy 		ASSERT3U(hist[idx], !=, 0);
114eda14cbcSMatt Macy 	}
115eda14cbcSMatt Macy 
116eda14cbcSMatt Macy 	for (i = 0; i < RANGE_TREE_HISTOGRAM_SIZE; i++) {
117eda14cbcSMatt Macy 		if (hist[i] != rt->rt_histogram[i]) {
118eda14cbcSMatt Macy 			zfs_dbgmsg("i=%d, hist=%px, hist=%llu, rt_hist=%llu",
11933b8c039SMartin Matuska 			    i, hist, (u_longlong_t)hist[i],
12033b8c039SMartin Matuska 			    (u_longlong_t)rt->rt_histogram[i]);
121eda14cbcSMatt Macy 		}
122eda14cbcSMatt Macy 		VERIFY3U(hist[i], ==, rt->rt_histogram[i]);
123eda14cbcSMatt Macy 	}
124eda14cbcSMatt Macy }
125eda14cbcSMatt Macy 
126eda14cbcSMatt Macy static void
range_tree_stat_incr(range_tree_t * rt,range_seg_t * rs)127eda14cbcSMatt Macy range_tree_stat_incr(range_tree_t *rt, range_seg_t *rs)
128eda14cbcSMatt Macy {
129eda14cbcSMatt Macy 	uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt);
130eda14cbcSMatt Macy 	int idx = highbit64(size) - 1;
131eda14cbcSMatt Macy 
132eda14cbcSMatt Macy 	ASSERT(size != 0);
133eda14cbcSMatt Macy 	ASSERT3U(idx, <,
134eda14cbcSMatt Macy 	    sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy 	rt->rt_histogram[idx]++;
137eda14cbcSMatt Macy 	ASSERT3U(rt->rt_histogram[idx], !=, 0);
138eda14cbcSMatt Macy }
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy static void
range_tree_stat_decr(range_tree_t * rt,range_seg_t * rs)141eda14cbcSMatt Macy range_tree_stat_decr(range_tree_t *rt, range_seg_t *rs)
142eda14cbcSMatt Macy {
143eda14cbcSMatt Macy 	uint64_t size = rs_get_end(rs, rt) - rs_get_start(rs, rt);
144eda14cbcSMatt Macy 	int idx = highbit64(size) - 1;
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy 	ASSERT(size != 0);
147eda14cbcSMatt Macy 	ASSERT3U(idx, <,
148eda14cbcSMatt Macy 	    sizeof (rt->rt_histogram) / sizeof (*rt->rt_histogram));
149eda14cbcSMatt Macy 
150eda14cbcSMatt Macy 	ASSERT3U(rt->rt_histogram[idx], !=, 0);
151eda14cbcSMatt Macy 	rt->rt_histogram[idx]--;
152eda14cbcSMatt Macy }
153eda14cbcSMatt Macy 
154*4e8d558cSMartin Matuska __attribute__((always_inline)) inline
155eda14cbcSMatt Macy static int
range_tree_seg32_compare(const void * x1,const void * x2)156eda14cbcSMatt Macy range_tree_seg32_compare(const void *x1, const void *x2)
157eda14cbcSMatt Macy {
158eda14cbcSMatt Macy 	const range_seg32_t *r1 = x1;
159eda14cbcSMatt Macy 	const range_seg32_t *r2 = x2;
160eda14cbcSMatt Macy 
161eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
162eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
165eda14cbcSMatt Macy }
166eda14cbcSMatt Macy 
167*4e8d558cSMartin Matuska __attribute__((always_inline)) inline
168eda14cbcSMatt Macy static int
range_tree_seg64_compare(const void * x1,const void * x2)169eda14cbcSMatt Macy range_tree_seg64_compare(const void *x1, const void *x2)
170eda14cbcSMatt Macy {
171eda14cbcSMatt Macy 	const range_seg64_t *r1 = x1;
172eda14cbcSMatt Macy 	const range_seg64_t *r2 = x2;
173eda14cbcSMatt Macy 
174eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
175eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
178eda14cbcSMatt Macy }
179eda14cbcSMatt Macy 
180*4e8d558cSMartin Matuska __attribute__((always_inline)) inline
181eda14cbcSMatt Macy static int
range_tree_seg_gap_compare(const void * x1,const void * x2)182eda14cbcSMatt Macy range_tree_seg_gap_compare(const void *x1, const void *x2)
183eda14cbcSMatt Macy {
184eda14cbcSMatt Macy 	const range_seg_gap_t *r1 = x1;
185eda14cbcSMatt Macy 	const range_seg_gap_t *r2 = x2;
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
188eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
191eda14cbcSMatt Macy }
192eda14cbcSMatt Macy 
ZFS_BTREE_FIND_IN_BUF_FUNC(range_tree_seg32_find_in_buf,range_seg32_t,range_tree_seg32_compare)193*4e8d558cSMartin Matuska ZFS_BTREE_FIND_IN_BUF_FUNC(range_tree_seg32_find_in_buf, range_seg32_t,
194*4e8d558cSMartin Matuska     range_tree_seg32_compare)
195*4e8d558cSMartin Matuska 
196*4e8d558cSMartin Matuska ZFS_BTREE_FIND_IN_BUF_FUNC(range_tree_seg64_find_in_buf, range_seg64_t,
197*4e8d558cSMartin Matuska     range_tree_seg64_compare)
198*4e8d558cSMartin Matuska 
199*4e8d558cSMartin Matuska ZFS_BTREE_FIND_IN_BUF_FUNC(range_tree_seg_gap_find_in_buf, range_seg_gap_t,
200*4e8d558cSMartin Matuska     range_tree_seg_gap_compare)
201*4e8d558cSMartin Matuska 
202eda14cbcSMatt Macy range_tree_t *
203a0b956f5SMartin Matuska range_tree_create_gap(const range_tree_ops_t *ops, range_seg_type_t type,
204a0b956f5SMartin Matuska     void *arg, uint64_t start, uint64_t shift, uint64_t gap)
205eda14cbcSMatt Macy {
206eda14cbcSMatt Macy 	range_tree_t *rt = kmem_zalloc(sizeof (range_tree_t), KM_SLEEP);
207eda14cbcSMatt Macy 
208eda14cbcSMatt Macy 	ASSERT3U(shift, <, 64);
209eda14cbcSMatt Macy 	ASSERT3U(type, <=, RANGE_SEG_NUM_TYPES);
210eda14cbcSMatt Macy 	size_t size;
211eda14cbcSMatt Macy 	int (*compare) (const void *, const void *);
212*4e8d558cSMartin Matuska 	bt_find_in_buf_f bt_find;
213eda14cbcSMatt Macy 	switch (type) {
214eda14cbcSMatt Macy 	case RANGE_SEG32:
215eda14cbcSMatt Macy 		size = sizeof (range_seg32_t);
216eda14cbcSMatt Macy 		compare = range_tree_seg32_compare;
217*4e8d558cSMartin Matuska 		bt_find = range_tree_seg32_find_in_buf;
218eda14cbcSMatt Macy 		break;
219eda14cbcSMatt Macy 	case RANGE_SEG64:
220eda14cbcSMatt Macy 		size = sizeof (range_seg64_t);
221eda14cbcSMatt Macy 		compare = range_tree_seg64_compare;
222*4e8d558cSMartin Matuska 		bt_find = range_tree_seg64_find_in_buf;
223eda14cbcSMatt Macy 		break;
224eda14cbcSMatt Macy 	case RANGE_SEG_GAP:
225eda14cbcSMatt Macy 		size = sizeof (range_seg_gap_t);
226eda14cbcSMatt Macy 		compare = range_tree_seg_gap_compare;
227*4e8d558cSMartin Matuska 		bt_find = range_tree_seg_gap_find_in_buf;
228eda14cbcSMatt Macy 		break;
229eda14cbcSMatt Macy 	default:
230eda14cbcSMatt Macy 		panic("Invalid range seg type %d", type);
231eda14cbcSMatt Macy 	}
232*4e8d558cSMartin Matuska 	zfs_btree_create(&rt->rt_root, compare, bt_find, size);
233eda14cbcSMatt Macy 
234eda14cbcSMatt Macy 	rt->rt_ops = ops;
235eda14cbcSMatt Macy 	rt->rt_gap = gap;
236eda14cbcSMatt Macy 	rt->rt_arg = arg;
237eda14cbcSMatt Macy 	rt->rt_type = type;
238eda14cbcSMatt Macy 	rt->rt_start = start;
239eda14cbcSMatt Macy 	rt->rt_shift = shift;
240eda14cbcSMatt Macy 
241eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_create != NULL)
242eda14cbcSMatt Macy 		rt->rt_ops->rtop_create(rt, rt->rt_arg);
243eda14cbcSMatt Macy 
244eda14cbcSMatt Macy 	return (rt);
245eda14cbcSMatt Macy }
246eda14cbcSMatt Macy 
247eda14cbcSMatt Macy range_tree_t *
range_tree_create(const range_tree_ops_t * ops,range_seg_type_t type,void * arg,uint64_t start,uint64_t shift)248e92ffd9bSMartin Matuska range_tree_create(const range_tree_ops_t *ops, range_seg_type_t type,
249eda14cbcSMatt Macy     void *arg, uint64_t start, uint64_t shift)
250eda14cbcSMatt Macy {
251a0b956f5SMartin Matuska 	return (range_tree_create_gap(ops, type, arg, start, shift, 0));
252eda14cbcSMatt Macy }
253eda14cbcSMatt Macy 
254eda14cbcSMatt Macy void
range_tree_destroy(range_tree_t * rt)255eda14cbcSMatt Macy range_tree_destroy(range_tree_t *rt)
256eda14cbcSMatt Macy {
257eda14cbcSMatt Macy 	VERIFY0(rt->rt_space);
258eda14cbcSMatt Macy 
259eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_destroy != NULL)
260eda14cbcSMatt Macy 		rt->rt_ops->rtop_destroy(rt, rt->rt_arg);
261eda14cbcSMatt Macy 
262eda14cbcSMatt Macy 	zfs_btree_destroy(&rt->rt_root);
263eda14cbcSMatt Macy 	kmem_free(rt, sizeof (*rt));
264eda14cbcSMatt Macy }
265eda14cbcSMatt Macy 
266eda14cbcSMatt Macy void
range_tree_adjust_fill(range_tree_t * rt,range_seg_t * rs,int64_t delta)267eda14cbcSMatt Macy range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta)
268eda14cbcSMatt Macy {
269eda14cbcSMatt Macy 	if (delta < 0 && delta * -1 >= rs_get_fill(rs, rt)) {
270eda14cbcSMatt Macy 		zfs_panic_recover("zfs: attempting to decrease fill to or "
271eda14cbcSMatt Macy 		    "below 0; probable double remove in segment [%llx:%llx]",
272eda14cbcSMatt Macy 		    (longlong_t)rs_get_start(rs, rt),
273eda14cbcSMatt Macy 		    (longlong_t)rs_get_end(rs, rt));
274eda14cbcSMatt Macy 	}
275eda14cbcSMatt Macy 	if (rs_get_fill(rs, rt) + delta > rs_get_end(rs, rt) -
276eda14cbcSMatt Macy 	    rs_get_start(rs, rt)) {
277eda14cbcSMatt Macy 		zfs_panic_recover("zfs: attempting to increase fill beyond "
278eda14cbcSMatt Macy 		    "max; probable double add in segment [%llx:%llx]",
279eda14cbcSMatt Macy 		    (longlong_t)rs_get_start(rs, rt),
280eda14cbcSMatt Macy 		    (longlong_t)rs_get_end(rs, rt));
281eda14cbcSMatt Macy 	}
282eda14cbcSMatt Macy 
283eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
284eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
285eda14cbcSMatt Macy 	rs_set_fill(rs, rt, rs_get_fill(rs, rt) + delta);
286eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
287eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
288eda14cbcSMatt Macy }
289eda14cbcSMatt Macy 
290eda14cbcSMatt Macy static void
range_tree_add_impl(void * arg,uint64_t start,uint64_t size,uint64_t fill)291eda14cbcSMatt Macy range_tree_add_impl(void *arg, uint64_t start, uint64_t size, uint64_t fill)
292eda14cbcSMatt Macy {
293eda14cbcSMatt Macy 	range_tree_t *rt = arg;
294eda14cbcSMatt Macy 	zfs_btree_index_t where;
295eda14cbcSMatt Macy 	range_seg_t *rs_before, *rs_after, *rs;
296eda14cbcSMatt Macy 	range_seg_max_t tmp, rsearch;
297eda14cbcSMatt Macy 	uint64_t end = start + size, gap = rt->rt_gap;
298eda14cbcSMatt Macy 	uint64_t bridge_size = 0;
299eda14cbcSMatt Macy 	boolean_t merge_before, merge_after;
300eda14cbcSMatt Macy 
301eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
302eda14cbcSMatt Macy 	ASSERT3U(fill, <=, size);
303eda14cbcSMatt Macy 	ASSERT3U(start + size, >, start);
304eda14cbcSMatt Macy 
305eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
306eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
307eda14cbcSMatt Macy 	rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
308eda14cbcSMatt Macy 
309eda14cbcSMatt Macy 	/*
310eda14cbcSMatt Macy 	 * If this is a gap-supporting range tree, it is possible that we
311eda14cbcSMatt Macy 	 * are inserting into an existing segment. In this case simply
312eda14cbcSMatt Macy 	 * bump the fill count and call the remove / add callbacks. If the
313eda14cbcSMatt Macy 	 * new range will extend an existing segment, we remove the
314eda14cbcSMatt Macy 	 * existing one, apply the new extent to it and re-insert it using
315eda14cbcSMatt Macy 	 * the normal code paths.
316eda14cbcSMatt Macy 	 */
317eda14cbcSMatt Macy 	if (rs != NULL) {
318eda14cbcSMatt Macy 		if (gap == 0) {
319eda14cbcSMatt Macy 			zfs_panic_recover("zfs: adding existent segment to "
320eda14cbcSMatt Macy 			    "range tree (offset=%llx size=%llx)",
321eda14cbcSMatt Macy 			    (longlong_t)start, (longlong_t)size);
322eda14cbcSMatt Macy 			return;
323eda14cbcSMatt Macy 		}
324eda14cbcSMatt Macy 		uint64_t rstart = rs_get_start(rs, rt);
325eda14cbcSMatt Macy 		uint64_t rend = rs_get_end(rs, rt);
326eda14cbcSMatt Macy 		if (rstart <= start && rend >= end) {
327eda14cbcSMatt Macy 			range_tree_adjust_fill(rt, rs, fill);
328eda14cbcSMatt Macy 			return;
329eda14cbcSMatt Macy 		}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
332eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
333eda14cbcSMatt Macy 
334eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs);
335eda14cbcSMatt Macy 		rt->rt_space -= rend - rstart;
336eda14cbcSMatt Macy 
337eda14cbcSMatt Macy 		fill += rs_get_fill(rs, rt);
338eda14cbcSMatt Macy 		start = MIN(start, rstart);
339eda14cbcSMatt Macy 		end = MAX(end, rend);
340eda14cbcSMatt Macy 		size = end - start;
341eda14cbcSMatt Macy 
342180f8225SMatt Macy 		zfs_btree_remove(&rt->rt_root, rs);
343eda14cbcSMatt Macy 		range_tree_add_impl(rt, start, size, fill);
344eda14cbcSMatt Macy 		return;
345eda14cbcSMatt Macy 	}
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	ASSERT3P(rs, ==, NULL);
348eda14cbcSMatt Macy 
349eda14cbcSMatt Macy 	/*
350eda14cbcSMatt Macy 	 * Determine whether or not we will have to merge with our neighbors.
351eda14cbcSMatt Macy 	 * If gap != 0, we might need to merge with our neighbors even if we
352eda14cbcSMatt Macy 	 * aren't directly touching.
353eda14cbcSMatt Macy 	 */
354eda14cbcSMatt Macy 	zfs_btree_index_t where_before, where_after;
355eda14cbcSMatt Macy 	rs_before = zfs_btree_prev(&rt->rt_root, &where, &where_before);
356eda14cbcSMatt Macy 	rs_after = zfs_btree_next(&rt->rt_root, &where, &where_after);
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy 	merge_before = (rs_before != NULL && rs_get_end(rs_before, rt) >=
359eda14cbcSMatt Macy 	    start - gap);
360eda14cbcSMatt Macy 	merge_after = (rs_after != NULL && rs_get_start(rs_after, rt) <= end +
361eda14cbcSMatt Macy 	    gap);
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	if (merge_before && gap != 0)
364eda14cbcSMatt Macy 		bridge_size += start - rs_get_end(rs_before, rt);
365eda14cbcSMatt Macy 	if (merge_after && gap != 0)
366eda14cbcSMatt Macy 		bridge_size += rs_get_start(rs_after, rt) - end;
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 	if (merge_before && merge_after) {
369eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) {
370eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
371eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
372eda14cbcSMatt Macy 		}
373eda14cbcSMatt Macy 
374eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_before);
375eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_after);
376eda14cbcSMatt Macy 
377eda14cbcSMatt Macy 		rs_copy(rs_after, &tmp, rt);
378eda14cbcSMatt Macy 		uint64_t before_start = rs_get_start_raw(rs_before, rt);
379eda14cbcSMatt Macy 		uint64_t before_fill = rs_get_fill(rs_before, rt);
380eda14cbcSMatt Macy 		uint64_t after_fill = rs_get_fill(rs_after, rt);
381eda14cbcSMatt Macy 		zfs_btree_remove_idx(&rt->rt_root, &where_before);
382eda14cbcSMatt Macy 
383eda14cbcSMatt Macy 		/*
384eda14cbcSMatt Macy 		 * We have to re-find the node because our old reference is
385eda14cbcSMatt Macy 		 * invalid as soon as we do any mutating btree operations.
386eda14cbcSMatt Macy 		 */
387eda14cbcSMatt Macy 		rs_after = zfs_btree_find(&rt->rt_root, &tmp, &where_after);
388dbd5678dSMartin Matuska 		ASSERT3P(rs_after, !=, NULL);
389eda14cbcSMatt Macy 		rs_set_start_raw(rs_after, rt, before_start);
390eda14cbcSMatt Macy 		rs_set_fill(rs_after, rt, after_fill + before_fill + fill);
391eda14cbcSMatt Macy 		rs = rs_after;
392eda14cbcSMatt Macy 	} else if (merge_before) {
393eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
394eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
395eda14cbcSMatt Macy 
396eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_before);
397eda14cbcSMatt Macy 
398eda14cbcSMatt Macy 		uint64_t before_fill = rs_get_fill(rs_before, rt);
399eda14cbcSMatt Macy 		rs_set_end(rs_before, rt, end);
400eda14cbcSMatt Macy 		rs_set_fill(rs_before, rt, before_fill + fill);
401eda14cbcSMatt Macy 		rs = rs_before;
402eda14cbcSMatt Macy 	} else if (merge_after) {
403eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
404eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
405eda14cbcSMatt Macy 
406eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_after);
407eda14cbcSMatt Macy 
408eda14cbcSMatt Macy 		uint64_t after_fill = rs_get_fill(rs_after, rt);
409eda14cbcSMatt Macy 		rs_set_start(rs_after, rt, start);
410eda14cbcSMatt Macy 		rs_set_fill(rs_after, rt, after_fill + fill);
411eda14cbcSMatt Macy 		rs = rs_after;
412eda14cbcSMatt Macy 	} else {
413eda14cbcSMatt Macy 		rs = &tmp;
414eda14cbcSMatt Macy 
415eda14cbcSMatt Macy 		rs_set_start(rs, rt, start);
416eda14cbcSMatt Macy 		rs_set_end(rs, rt, end);
417eda14cbcSMatt Macy 		rs_set_fill(rs, rt, fill);
418eda14cbcSMatt Macy 		zfs_btree_add_idx(&rt->rt_root, rs, &where);
419eda14cbcSMatt Macy 	}
420eda14cbcSMatt Macy 
421eda14cbcSMatt Macy 	if (gap != 0) {
422eda14cbcSMatt Macy 		ASSERT3U(rs_get_fill(rs, rt), <=, rs_get_end(rs, rt) -
423eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
424eda14cbcSMatt Macy 	} else {
425eda14cbcSMatt Macy 		ASSERT3U(rs_get_fill(rs, rt), ==, rs_get_end(rs, rt) -
426eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
427eda14cbcSMatt Macy 	}
428eda14cbcSMatt Macy 
429eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
430eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
431eda14cbcSMatt Macy 
432eda14cbcSMatt Macy 	range_tree_stat_incr(rt, rs);
433eda14cbcSMatt Macy 	rt->rt_space += size + bridge_size;
434eda14cbcSMatt Macy }
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy void
range_tree_add(void * arg,uint64_t start,uint64_t size)437eda14cbcSMatt Macy range_tree_add(void *arg, uint64_t start, uint64_t size)
438eda14cbcSMatt Macy {
439eda14cbcSMatt Macy 	range_tree_add_impl(arg, start, size, size);
440eda14cbcSMatt Macy }
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy static void
range_tree_remove_impl(range_tree_t * rt,uint64_t start,uint64_t size,boolean_t do_fill)443eda14cbcSMatt Macy range_tree_remove_impl(range_tree_t *rt, uint64_t start, uint64_t size,
444eda14cbcSMatt Macy     boolean_t do_fill)
445eda14cbcSMatt Macy {
446eda14cbcSMatt Macy 	zfs_btree_index_t where;
447eda14cbcSMatt Macy 	range_seg_t *rs;
448eda14cbcSMatt Macy 	range_seg_max_t rsearch, rs_tmp;
449eda14cbcSMatt Macy 	uint64_t end = start + size;
450eda14cbcSMatt Macy 	boolean_t left_over, right_over;
451eda14cbcSMatt Macy 
452eda14cbcSMatt Macy 	VERIFY3U(size, !=, 0);
453eda14cbcSMatt Macy 	VERIFY3U(size, <=, rt->rt_space);
454eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
455eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
456eda14cbcSMatt Macy 
457eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
458eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
459eda14cbcSMatt Macy 	rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
460eda14cbcSMatt Macy 
461eda14cbcSMatt Macy 	/* Make sure we completely overlap with someone */
462eda14cbcSMatt Macy 	if (rs == NULL) {
463eda14cbcSMatt Macy 		zfs_panic_recover("zfs: removing nonexistent segment from "
464eda14cbcSMatt Macy 		    "range tree (offset=%llx size=%llx)",
465eda14cbcSMatt Macy 		    (longlong_t)start, (longlong_t)size);
466eda14cbcSMatt Macy 		return;
467eda14cbcSMatt Macy 	}
468eda14cbcSMatt Macy 
469eda14cbcSMatt Macy 	/*
470eda14cbcSMatt Macy 	 * Range trees with gap support must only remove complete segments
471eda14cbcSMatt Macy 	 * from the tree. This allows us to maintain accurate fill accounting
472eda14cbcSMatt Macy 	 * and to ensure that bridged sections are not leaked. If we need to
473eda14cbcSMatt Macy 	 * remove less than the full segment, we can only adjust the fill count.
474eda14cbcSMatt Macy 	 */
475eda14cbcSMatt Macy 	if (rt->rt_gap != 0) {
476eda14cbcSMatt Macy 		if (do_fill) {
477eda14cbcSMatt Macy 			if (rs_get_fill(rs, rt) == size) {
478eda14cbcSMatt Macy 				start = rs_get_start(rs, rt);
479eda14cbcSMatt Macy 				end = rs_get_end(rs, rt);
480eda14cbcSMatt Macy 				size = end - start;
481eda14cbcSMatt Macy 			} else {
482eda14cbcSMatt Macy 				range_tree_adjust_fill(rt, rs, -size);
483eda14cbcSMatt Macy 				return;
484eda14cbcSMatt Macy 			}
485eda14cbcSMatt Macy 		} else if (rs_get_start(rs, rt) != start ||
486eda14cbcSMatt Macy 		    rs_get_end(rs, rt) != end) {
487eda14cbcSMatt Macy 			zfs_panic_recover("zfs: freeing partial segment of "
488eda14cbcSMatt Macy 			    "gap tree (offset=%llx size=%llx) of "
489eda14cbcSMatt Macy 			    "(offset=%llx size=%llx)",
490eda14cbcSMatt Macy 			    (longlong_t)start, (longlong_t)size,
491eda14cbcSMatt Macy 			    (longlong_t)rs_get_start(rs, rt),
492eda14cbcSMatt Macy 			    (longlong_t)rs_get_end(rs, rt) - rs_get_start(rs,
493eda14cbcSMatt Macy 			    rt));
494eda14cbcSMatt Macy 			return;
495eda14cbcSMatt Macy 		}
496eda14cbcSMatt Macy 	}
497eda14cbcSMatt Macy 
498eda14cbcSMatt Macy 	VERIFY3U(rs_get_start(rs, rt), <=, start);
499eda14cbcSMatt Macy 	VERIFY3U(rs_get_end(rs, rt), >=, end);
500eda14cbcSMatt Macy 
501eda14cbcSMatt Macy 	left_over = (rs_get_start(rs, rt) != start);
502eda14cbcSMatt Macy 	right_over = (rs_get_end(rs, rt) != end);
503eda14cbcSMatt Macy 
504eda14cbcSMatt Macy 	range_tree_stat_decr(rt, rs);
505eda14cbcSMatt Macy 
506eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
507eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy 	if (left_over && right_over) {
510eda14cbcSMatt Macy 		range_seg_max_t newseg;
511eda14cbcSMatt Macy 		rs_set_start(&newseg, rt, end);
512eda14cbcSMatt Macy 		rs_set_end_raw(&newseg, rt, rs_get_end_raw(rs, rt));
513eda14cbcSMatt Macy 		rs_set_fill(&newseg, rt, rs_get_end(rs, rt) - end);
514eda14cbcSMatt Macy 		range_tree_stat_incr(rt, &newseg);
515eda14cbcSMatt Macy 
516eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
517eda14cbcSMatt Macy 		rs_set_end(rs, rt, start);
518eda14cbcSMatt Macy 
519eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
520eda14cbcSMatt Macy 		if (zfs_btree_next(&rt->rt_root, &where, &where) != NULL)
521eda14cbcSMatt Macy 			zfs_btree_add_idx(&rt->rt_root, &newseg, &where);
522eda14cbcSMatt Macy 		else
523eda14cbcSMatt Macy 			zfs_btree_add(&rt->rt_root, &newseg);
524eda14cbcSMatt Macy 
525eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
526eda14cbcSMatt Macy 			rt->rt_ops->rtop_add(rt, &newseg, rt->rt_arg);
527eda14cbcSMatt Macy 	} else if (left_over) {
528eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
529eda14cbcSMatt Macy 		rs_set_end(rs, rt, start);
530eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
531eda14cbcSMatt Macy 	} else if (right_over) {
532eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
533eda14cbcSMatt Macy 		rs_set_start(rs, rt, end);
534eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
535eda14cbcSMatt Macy 	} else {
536eda14cbcSMatt Macy 		zfs_btree_remove_idx(&rt->rt_root, &where);
537eda14cbcSMatt Macy 		rs = NULL;
538eda14cbcSMatt Macy 	}
539eda14cbcSMatt Macy 
540eda14cbcSMatt Macy 	if (rs != NULL) {
541eda14cbcSMatt Macy 		/*
542eda14cbcSMatt Macy 		 * The fill of the leftover segment will always be equal to
543eda14cbcSMatt Macy 		 * the size, since we do not support removing partial segments
544eda14cbcSMatt Macy 		 * of range trees with gaps.
545eda14cbcSMatt Macy 		 */
546eda14cbcSMatt Macy 		rs_set_fill_raw(rs, rt, rs_get_end_raw(rs, rt) -
547eda14cbcSMatt Macy 		    rs_get_start_raw(rs, rt));
548eda14cbcSMatt Macy 		range_tree_stat_incr(rt, &rs_tmp);
549eda14cbcSMatt Macy 
550eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
551eda14cbcSMatt Macy 			rt->rt_ops->rtop_add(rt, &rs_tmp, rt->rt_arg);
552eda14cbcSMatt Macy 	}
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy 	rt->rt_space -= size;
555eda14cbcSMatt Macy }
556eda14cbcSMatt Macy 
557eda14cbcSMatt Macy void
range_tree_remove(void * arg,uint64_t start,uint64_t size)558eda14cbcSMatt Macy range_tree_remove(void *arg, uint64_t start, uint64_t size)
559eda14cbcSMatt Macy {
560eda14cbcSMatt Macy 	range_tree_remove_impl(arg, start, size, B_FALSE);
561eda14cbcSMatt Macy }
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy void
range_tree_remove_fill(range_tree_t * rt,uint64_t start,uint64_t size)564eda14cbcSMatt Macy range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size)
565eda14cbcSMatt Macy {
566eda14cbcSMatt Macy 	range_tree_remove_impl(rt, start, size, B_TRUE);
567eda14cbcSMatt Macy }
568eda14cbcSMatt Macy 
569eda14cbcSMatt Macy void
range_tree_resize_segment(range_tree_t * rt,range_seg_t * rs,uint64_t newstart,uint64_t newsize)570eda14cbcSMatt Macy range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs,
571eda14cbcSMatt Macy     uint64_t newstart, uint64_t newsize)
572eda14cbcSMatt Macy {
573eda14cbcSMatt Macy 	int64_t delta = newsize - (rs_get_end(rs, rt) - rs_get_start(rs, rt));
574eda14cbcSMatt Macy 
575eda14cbcSMatt Macy 	range_tree_stat_decr(rt, rs);
576eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
577eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	rs_set_start(rs, rt, newstart);
580eda14cbcSMatt Macy 	rs_set_end(rs, rt, newstart + newsize);
581eda14cbcSMatt Macy 
582eda14cbcSMatt Macy 	range_tree_stat_incr(rt, rs);
583eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
584eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy 	rt->rt_space += delta;
587eda14cbcSMatt Macy }
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy static range_seg_t *
range_tree_find_impl(range_tree_t * rt,uint64_t start,uint64_t size)590eda14cbcSMatt Macy range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size)
591eda14cbcSMatt Macy {
592eda14cbcSMatt Macy 	range_seg_max_t rsearch;
593eda14cbcSMatt Macy 	uint64_t end = start + size;
594eda14cbcSMatt Macy 
595eda14cbcSMatt Macy 	VERIFY(size != 0);
596eda14cbcSMatt Macy 
597eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
598eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
599eda14cbcSMatt Macy 	return (zfs_btree_find(&rt->rt_root, &rsearch, NULL));
600eda14cbcSMatt Macy }
601eda14cbcSMatt Macy 
602eda14cbcSMatt Macy range_seg_t *
range_tree_find(range_tree_t * rt,uint64_t start,uint64_t size)603eda14cbcSMatt Macy range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size)
604eda14cbcSMatt Macy {
605eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
606eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy 	range_seg_t *rs = range_tree_find_impl(rt, start, size);
609eda14cbcSMatt Macy 	if (rs != NULL && rs_get_start(rs, rt) <= start &&
610eda14cbcSMatt Macy 	    rs_get_end(rs, rt) >= start + size) {
611eda14cbcSMatt Macy 		return (rs);
612eda14cbcSMatt Macy 	}
613eda14cbcSMatt Macy 	return (NULL);
614eda14cbcSMatt Macy }
615eda14cbcSMatt Macy 
616eda14cbcSMatt Macy void
range_tree_verify_not_present(range_tree_t * rt,uint64_t off,uint64_t size)617eda14cbcSMatt Macy range_tree_verify_not_present(range_tree_t *rt, uint64_t off, uint64_t size)
618eda14cbcSMatt Macy {
619eda14cbcSMatt Macy 	range_seg_t *rs = range_tree_find(rt, off, size);
620eda14cbcSMatt Macy 	if (rs != NULL)
621eda14cbcSMatt Macy 		panic("segment already in tree; rs=%p", (void *)rs);
622eda14cbcSMatt Macy }
623eda14cbcSMatt Macy 
624eda14cbcSMatt Macy boolean_t
range_tree_contains(range_tree_t * rt,uint64_t start,uint64_t size)625eda14cbcSMatt Macy range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size)
626eda14cbcSMatt Macy {
627eda14cbcSMatt Macy 	return (range_tree_find(rt, start, size) != NULL);
628eda14cbcSMatt Macy }
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy /*
631eda14cbcSMatt Macy  * Returns the first subset of the given range which overlaps with the range
632eda14cbcSMatt Macy  * tree. Returns true if there is a segment in the range, and false if there
633eda14cbcSMatt Macy  * isn't.
634eda14cbcSMatt Macy  */
635eda14cbcSMatt Macy boolean_t
range_tree_find_in(range_tree_t * rt,uint64_t start,uint64_t size,uint64_t * ostart,uint64_t * osize)636eda14cbcSMatt Macy range_tree_find_in(range_tree_t *rt, uint64_t start, uint64_t size,
637eda14cbcSMatt Macy     uint64_t *ostart, uint64_t *osize)
638eda14cbcSMatt Macy {
639eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
640eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 	range_seg_max_t rsearch;
643eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
644eda14cbcSMatt Macy 	rs_set_end_raw(&rsearch, rt, rs_get_start_raw(&rsearch, rt) + 1);
645eda14cbcSMatt Macy 
646eda14cbcSMatt Macy 	zfs_btree_index_t where;
647eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
648eda14cbcSMatt Macy 	if (rs != NULL) {
649eda14cbcSMatt Macy 		*ostart = start;
650eda14cbcSMatt Macy 		*osize = MIN(size, rs_get_end(rs, rt) - start);
651eda14cbcSMatt Macy 		return (B_TRUE);
652eda14cbcSMatt Macy 	}
653eda14cbcSMatt Macy 
654eda14cbcSMatt Macy 	rs = zfs_btree_next(&rt->rt_root, &where, &where);
655eda14cbcSMatt Macy 	if (rs == NULL || rs_get_start(rs, rt) > start + size)
656eda14cbcSMatt Macy 		return (B_FALSE);
657eda14cbcSMatt Macy 
658eda14cbcSMatt Macy 	*ostart = rs_get_start(rs, rt);
659eda14cbcSMatt Macy 	*osize = MIN(start + size, rs_get_end(rs, rt)) -
660eda14cbcSMatt Macy 	    rs_get_start(rs, rt);
661eda14cbcSMatt Macy 	return (B_TRUE);
662eda14cbcSMatt Macy }
663eda14cbcSMatt Macy 
664eda14cbcSMatt Macy /*
665eda14cbcSMatt Macy  * Ensure that this range is not in the tree, regardless of whether
666eda14cbcSMatt Macy  * it is currently in the tree.
667eda14cbcSMatt Macy  */
668eda14cbcSMatt Macy void
range_tree_clear(range_tree_t * rt,uint64_t start,uint64_t size)669eda14cbcSMatt Macy range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size)
670eda14cbcSMatt Macy {
671eda14cbcSMatt Macy 	range_seg_t *rs;
672eda14cbcSMatt Macy 
673eda14cbcSMatt Macy 	if (size == 0)
674eda14cbcSMatt Macy 		return;
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
677eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
678eda14cbcSMatt Macy 
679eda14cbcSMatt Macy 	while ((rs = range_tree_find_impl(rt, start, size)) != NULL) {
680eda14cbcSMatt Macy 		uint64_t free_start = MAX(rs_get_start(rs, rt), start);
681eda14cbcSMatt Macy 		uint64_t free_end = MIN(rs_get_end(rs, rt), start + size);
682eda14cbcSMatt Macy 		range_tree_remove(rt, free_start, free_end - free_start);
683eda14cbcSMatt Macy 	}
684eda14cbcSMatt Macy }
685eda14cbcSMatt Macy 
686eda14cbcSMatt Macy void
range_tree_swap(range_tree_t ** rtsrc,range_tree_t ** rtdst)687eda14cbcSMatt Macy range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst)
688eda14cbcSMatt Macy {
689eda14cbcSMatt Macy 	range_tree_t *rt;
690eda14cbcSMatt Macy 
691eda14cbcSMatt Macy 	ASSERT0(range_tree_space(*rtdst));
692eda14cbcSMatt Macy 	ASSERT0(zfs_btree_numnodes(&(*rtdst)->rt_root));
693eda14cbcSMatt Macy 
694eda14cbcSMatt Macy 	rt = *rtsrc;
695eda14cbcSMatt Macy 	*rtsrc = *rtdst;
696eda14cbcSMatt Macy 	*rtdst = rt;
697eda14cbcSMatt Macy }
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy void
range_tree_vacate(range_tree_t * rt,range_tree_func_t * func,void * arg)700eda14cbcSMatt Macy range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg)
701eda14cbcSMatt Macy {
702eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_vacate != NULL)
703eda14cbcSMatt Macy 		rt->rt_ops->rtop_vacate(rt, rt->rt_arg);
704eda14cbcSMatt Macy 
705eda14cbcSMatt Macy 	if (func != NULL) {
706eda14cbcSMatt Macy 		range_seg_t *rs;
707eda14cbcSMatt Macy 		zfs_btree_index_t *cookie = NULL;
708eda14cbcSMatt Macy 
709eda14cbcSMatt Macy 		while ((rs = zfs_btree_destroy_nodes(&rt->rt_root, &cookie)) !=
710eda14cbcSMatt Macy 		    NULL) {
711eda14cbcSMatt Macy 			func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) -
712eda14cbcSMatt Macy 			    rs_get_start(rs, rt));
713eda14cbcSMatt Macy 		}
714eda14cbcSMatt Macy 	} else {
715eda14cbcSMatt Macy 		zfs_btree_clear(&rt->rt_root);
716eda14cbcSMatt Macy 	}
717eda14cbcSMatt Macy 
718da5137abSMartin Matuska 	memset(rt->rt_histogram, 0, sizeof (rt->rt_histogram));
719eda14cbcSMatt Macy 	rt->rt_space = 0;
720eda14cbcSMatt Macy }
721eda14cbcSMatt Macy 
722eda14cbcSMatt Macy void
range_tree_walk(range_tree_t * rt,range_tree_func_t * func,void * arg)723eda14cbcSMatt Macy range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg)
724eda14cbcSMatt Macy {
725eda14cbcSMatt Macy 	zfs_btree_index_t where;
726eda14cbcSMatt Macy 	for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where);
727eda14cbcSMatt Macy 	    rs != NULL; rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
728eda14cbcSMatt Macy 		func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) -
729eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
730eda14cbcSMatt Macy 	}
731eda14cbcSMatt Macy }
732eda14cbcSMatt Macy 
733eda14cbcSMatt Macy range_seg_t *
range_tree_first(range_tree_t * rt)734eda14cbcSMatt Macy range_tree_first(range_tree_t *rt)
735eda14cbcSMatt Macy {
736eda14cbcSMatt Macy 	return (zfs_btree_first(&rt->rt_root, NULL));
737eda14cbcSMatt Macy }
738eda14cbcSMatt Macy 
739eda14cbcSMatt Macy uint64_t
range_tree_space(range_tree_t * rt)740eda14cbcSMatt Macy range_tree_space(range_tree_t *rt)
741eda14cbcSMatt Macy {
742eda14cbcSMatt Macy 	return (rt->rt_space);
743eda14cbcSMatt Macy }
744eda14cbcSMatt Macy 
745eda14cbcSMatt Macy uint64_t
range_tree_numsegs(range_tree_t * rt)746eda14cbcSMatt Macy range_tree_numsegs(range_tree_t *rt)
747eda14cbcSMatt Macy {
748eda14cbcSMatt Macy 	return ((rt == NULL) ? 0 : zfs_btree_numnodes(&rt->rt_root));
749eda14cbcSMatt Macy }
750eda14cbcSMatt Macy 
751eda14cbcSMatt Macy boolean_t
range_tree_is_empty(range_tree_t * rt)752eda14cbcSMatt Macy range_tree_is_empty(range_tree_t *rt)
753eda14cbcSMatt Macy {
754eda14cbcSMatt Macy 	ASSERT(rt != NULL);
755eda14cbcSMatt Macy 	return (range_tree_space(rt) == 0);
756eda14cbcSMatt Macy }
757eda14cbcSMatt Macy 
758eda14cbcSMatt Macy /*
759eda14cbcSMatt Macy  * Remove any overlapping ranges between the given segment [start, end)
760eda14cbcSMatt Macy  * from removefrom. Add non-overlapping leftovers to addto.
761eda14cbcSMatt Macy  */
762eda14cbcSMatt Macy void
range_tree_remove_xor_add_segment(uint64_t start,uint64_t end,range_tree_t * removefrom,range_tree_t * addto)763eda14cbcSMatt Macy range_tree_remove_xor_add_segment(uint64_t start, uint64_t end,
764eda14cbcSMatt Macy     range_tree_t *removefrom, range_tree_t *addto)
765eda14cbcSMatt Macy {
766eda14cbcSMatt Macy 	zfs_btree_index_t where;
767eda14cbcSMatt Macy 	range_seg_max_t starting_rs;
768eda14cbcSMatt Macy 	rs_set_start(&starting_rs, removefrom, start);
769eda14cbcSMatt Macy 	rs_set_end_raw(&starting_rs, removefrom, rs_get_start_raw(&starting_rs,
770eda14cbcSMatt Macy 	    removefrom) + 1);
771eda14cbcSMatt Macy 
772eda14cbcSMatt Macy 	range_seg_t *curr = zfs_btree_find(&removefrom->rt_root,
773eda14cbcSMatt Macy 	    &starting_rs, &where);
774eda14cbcSMatt Macy 
775eda14cbcSMatt Macy 	if (curr == NULL)
776eda14cbcSMatt Macy 		curr = zfs_btree_next(&removefrom->rt_root, &where, &where);
777eda14cbcSMatt Macy 
778eda14cbcSMatt Macy 	range_seg_t *next;
779eda14cbcSMatt Macy 	for (; curr != NULL; curr = next) {
780eda14cbcSMatt Macy 		if (start == end)
781eda14cbcSMatt Macy 			return;
782eda14cbcSMatt Macy 		VERIFY3U(start, <, end);
783eda14cbcSMatt Macy 
784eda14cbcSMatt Macy 		/* there is no overlap */
785eda14cbcSMatt Macy 		if (end <= rs_get_start(curr, removefrom)) {
786eda14cbcSMatt Macy 			range_tree_add(addto, start, end - start);
787eda14cbcSMatt Macy 			return;
788eda14cbcSMatt Macy 		}
789eda14cbcSMatt Macy 
790eda14cbcSMatt Macy 		uint64_t overlap_start = MAX(rs_get_start(curr, removefrom),
791eda14cbcSMatt Macy 		    start);
792eda14cbcSMatt Macy 		uint64_t overlap_end = MIN(rs_get_end(curr, removefrom),
793eda14cbcSMatt Macy 		    end);
794eda14cbcSMatt Macy 		uint64_t overlap_size = overlap_end - overlap_start;
795eda14cbcSMatt Macy 		ASSERT3S(overlap_size, >, 0);
796eda14cbcSMatt Macy 		range_seg_max_t rs;
797eda14cbcSMatt Macy 		rs_copy(curr, &rs, removefrom);
798eda14cbcSMatt Macy 
799eda14cbcSMatt Macy 		range_tree_remove(removefrom, overlap_start, overlap_size);
800eda14cbcSMatt Macy 
801eda14cbcSMatt Macy 		if (start < overlap_start)
802eda14cbcSMatt Macy 			range_tree_add(addto, start, overlap_start - start);
803eda14cbcSMatt Macy 
804eda14cbcSMatt Macy 		start = overlap_end;
805eda14cbcSMatt Macy 		next = zfs_btree_find(&removefrom->rt_root, &rs, &where);
806eda14cbcSMatt Macy 		/*
807eda14cbcSMatt Macy 		 * If we find something here, we only removed part of the
808eda14cbcSMatt Macy 		 * curr segment. Either there's some left at the end
809eda14cbcSMatt Macy 		 * because we've reached the end of the range we're removing,
810eda14cbcSMatt Macy 		 * or there's some left at the start because we started
811eda14cbcSMatt Macy 		 * partway through the range.  Either way, we continue with
812eda14cbcSMatt Macy 		 * the loop. If it's the former, we'll return at the start of
813eda14cbcSMatt Macy 		 * the loop, and if it's the latter we'll see if there is more
814eda14cbcSMatt Macy 		 * area to process.
815eda14cbcSMatt Macy 		 */
816eda14cbcSMatt Macy 		if (next != NULL) {
817eda14cbcSMatt Macy 			ASSERT(start == end || start == rs_get_end(&rs,
818eda14cbcSMatt Macy 			    removefrom));
819eda14cbcSMatt Macy 		}
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 		next = zfs_btree_next(&removefrom->rt_root, &where, &where);
822eda14cbcSMatt Macy 	}
823eda14cbcSMatt Macy 	VERIFY3P(curr, ==, NULL);
824eda14cbcSMatt Macy 
825eda14cbcSMatt Macy 	if (start != end) {
826eda14cbcSMatt Macy 		VERIFY3U(start, <, end);
827eda14cbcSMatt Macy 		range_tree_add(addto, start, end - start);
828eda14cbcSMatt Macy 	} else {
829eda14cbcSMatt Macy 		VERIFY3U(start, ==, end);
830eda14cbcSMatt Macy 	}
831eda14cbcSMatt Macy }
832eda14cbcSMatt Macy 
833eda14cbcSMatt Macy /*
834eda14cbcSMatt Macy  * For each entry in rt, if it exists in removefrom, remove it
835eda14cbcSMatt Macy  * from removefrom. Otherwise, add it to addto.
836eda14cbcSMatt Macy  */
837eda14cbcSMatt Macy void
range_tree_remove_xor_add(range_tree_t * rt,range_tree_t * removefrom,range_tree_t * addto)838eda14cbcSMatt Macy range_tree_remove_xor_add(range_tree_t *rt, range_tree_t *removefrom,
839eda14cbcSMatt Macy     range_tree_t *addto)
840eda14cbcSMatt Macy {
841eda14cbcSMatt Macy 	zfs_btree_index_t where;
842eda14cbcSMatt Macy 	for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); rs;
843eda14cbcSMatt Macy 	    rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
844eda14cbcSMatt Macy 		range_tree_remove_xor_add_segment(rs_get_start(rs, rt),
845eda14cbcSMatt Macy 		    rs_get_end(rs, rt), removefrom, addto);
846eda14cbcSMatt Macy 	}
847eda14cbcSMatt Macy }
848eda14cbcSMatt Macy 
849eda14cbcSMatt Macy uint64_t
range_tree_min(range_tree_t * rt)850eda14cbcSMatt Macy range_tree_min(range_tree_t *rt)
851eda14cbcSMatt Macy {
852eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_first(&rt->rt_root, NULL);
853eda14cbcSMatt Macy 	return (rs != NULL ? rs_get_start(rs, rt) : 0);
854eda14cbcSMatt Macy }
855eda14cbcSMatt Macy 
856eda14cbcSMatt Macy uint64_t
range_tree_max(range_tree_t * rt)857eda14cbcSMatt Macy range_tree_max(range_tree_t *rt)
858eda14cbcSMatt Macy {
859eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_last(&rt->rt_root, NULL);
860eda14cbcSMatt Macy 	return (rs != NULL ? rs_get_end(rs, rt) : 0);
861eda14cbcSMatt Macy }
862eda14cbcSMatt Macy 
863eda14cbcSMatt Macy uint64_t
range_tree_span(range_tree_t * rt)864eda14cbcSMatt Macy range_tree_span(range_tree_t *rt)
865eda14cbcSMatt Macy {
866eda14cbcSMatt Macy 	return (range_tree_max(rt) - range_tree_min(rt));
867eda14cbcSMatt Macy }
868