xref: /freebsd/sys/contrib/openzfs/module/zfs/range_tree.c (revision dbd5678dca91abcefe8d046aa2f9b66497a95ffb)
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
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
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
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
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 
154eda14cbcSMatt Macy static int
155eda14cbcSMatt Macy range_tree_seg32_compare(const void *x1, const void *x2)
156eda14cbcSMatt Macy {
157eda14cbcSMatt Macy 	const range_seg32_t *r1 = x1;
158eda14cbcSMatt Macy 	const range_seg32_t *r2 = x2;
159eda14cbcSMatt Macy 
160eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
161eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
164eda14cbcSMatt Macy }
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy static int
167eda14cbcSMatt Macy range_tree_seg64_compare(const void *x1, const void *x2)
168eda14cbcSMatt Macy {
169eda14cbcSMatt Macy 	const range_seg64_t *r1 = x1;
170eda14cbcSMatt Macy 	const range_seg64_t *r2 = x2;
171eda14cbcSMatt Macy 
172eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
173eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
174eda14cbcSMatt Macy 
175eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
176eda14cbcSMatt Macy }
177eda14cbcSMatt Macy 
178eda14cbcSMatt Macy static int
179eda14cbcSMatt Macy range_tree_seg_gap_compare(const void *x1, const void *x2)
180eda14cbcSMatt Macy {
181eda14cbcSMatt Macy 	const range_seg_gap_t *r1 = x1;
182eda14cbcSMatt Macy 	const range_seg_gap_t *r2 = x2;
183eda14cbcSMatt Macy 
184eda14cbcSMatt Macy 	ASSERT3U(r1->rs_start, <=, r1->rs_end);
185eda14cbcSMatt Macy 	ASSERT3U(r2->rs_start, <=, r2->rs_end);
186eda14cbcSMatt Macy 
187eda14cbcSMatt Macy 	return ((r1->rs_start >= r2->rs_end) - (r1->rs_end <= r2->rs_start));
188eda14cbcSMatt Macy }
189eda14cbcSMatt Macy 
190eda14cbcSMatt Macy range_tree_t *
191a0b956f5SMartin Matuska range_tree_create_gap(const range_tree_ops_t *ops, range_seg_type_t type,
192a0b956f5SMartin Matuska     void *arg, uint64_t start, uint64_t shift, uint64_t gap)
193eda14cbcSMatt Macy {
194eda14cbcSMatt Macy 	range_tree_t *rt = kmem_zalloc(sizeof (range_tree_t), KM_SLEEP);
195eda14cbcSMatt Macy 
196eda14cbcSMatt Macy 	ASSERT3U(shift, <, 64);
197eda14cbcSMatt Macy 	ASSERT3U(type, <=, RANGE_SEG_NUM_TYPES);
198eda14cbcSMatt Macy 	size_t size;
199eda14cbcSMatt Macy 	int (*compare) (const void *, const void *);
200eda14cbcSMatt Macy 	switch (type) {
201eda14cbcSMatt Macy 	case RANGE_SEG32:
202eda14cbcSMatt Macy 		size = sizeof (range_seg32_t);
203eda14cbcSMatt Macy 		compare = range_tree_seg32_compare;
204eda14cbcSMatt Macy 		break;
205eda14cbcSMatt Macy 	case RANGE_SEG64:
206eda14cbcSMatt Macy 		size = sizeof (range_seg64_t);
207eda14cbcSMatt Macy 		compare = range_tree_seg64_compare;
208eda14cbcSMatt Macy 		break;
209eda14cbcSMatt Macy 	case RANGE_SEG_GAP:
210eda14cbcSMatt Macy 		size = sizeof (range_seg_gap_t);
211eda14cbcSMatt Macy 		compare = range_tree_seg_gap_compare;
212eda14cbcSMatt Macy 		break;
213eda14cbcSMatt Macy 	default:
214eda14cbcSMatt Macy 		panic("Invalid range seg type %d", type);
215eda14cbcSMatt Macy 	}
216eda14cbcSMatt Macy 	zfs_btree_create(&rt->rt_root, compare, size);
217eda14cbcSMatt Macy 
218eda14cbcSMatt Macy 	rt->rt_ops = ops;
219eda14cbcSMatt Macy 	rt->rt_gap = gap;
220eda14cbcSMatt Macy 	rt->rt_arg = arg;
221eda14cbcSMatt Macy 	rt->rt_type = type;
222eda14cbcSMatt Macy 	rt->rt_start = start;
223eda14cbcSMatt Macy 	rt->rt_shift = shift;
224eda14cbcSMatt Macy 
225eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_create != NULL)
226eda14cbcSMatt Macy 		rt->rt_ops->rtop_create(rt, rt->rt_arg);
227eda14cbcSMatt Macy 
228eda14cbcSMatt Macy 	return (rt);
229eda14cbcSMatt Macy }
230eda14cbcSMatt Macy 
231eda14cbcSMatt Macy range_tree_t *
232e92ffd9bSMartin Matuska range_tree_create(const range_tree_ops_t *ops, range_seg_type_t type,
233eda14cbcSMatt Macy     void *arg, uint64_t start, uint64_t shift)
234eda14cbcSMatt Macy {
235a0b956f5SMartin Matuska 	return (range_tree_create_gap(ops, type, arg, start, shift, 0));
236eda14cbcSMatt Macy }
237eda14cbcSMatt Macy 
238eda14cbcSMatt Macy void
239eda14cbcSMatt Macy range_tree_destroy(range_tree_t *rt)
240eda14cbcSMatt Macy {
241eda14cbcSMatt Macy 	VERIFY0(rt->rt_space);
242eda14cbcSMatt Macy 
243eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_destroy != NULL)
244eda14cbcSMatt Macy 		rt->rt_ops->rtop_destroy(rt, rt->rt_arg);
245eda14cbcSMatt Macy 
246eda14cbcSMatt Macy 	zfs_btree_destroy(&rt->rt_root);
247eda14cbcSMatt Macy 	kmem_free(rt, sizeof (*rt));
248eda14cbcSMatt Macy }
249eda14cbcSMatt Macy 
250eda14cbcSMatt Macy void
251eda14cbcSMatt Macy range_tree_adjust_fill(range_tree_t *rt, range_seg_t *rs, int64_t delta)
252eda14cbcSMatt Macy {
253eda14cbcSMatt Macy 	if (delta < 0 && delta * -1 >= rs_get_fill(rs, rt)) {
254eda14cbcSMatt Macy 		zfs_panic_recover("zfs: attempting to decrease fill to or "
255eda14cbcSMatt Macy 		    "below 0; probable double remove in segment [%llx:%llx]",
256eda14cbcSMatt Macy 		    (longlong_t)rs_get_start(rs, rt),
257eda14cbcSMatt Macy 		    (longlong_t)rs_get_end(rs, rt));
258eda14cbcSMatt Macy 	}
259eda14cbcSMatt Macy 	if (rs_get_fill(rs, rt) + delta > rs_get_end(rs, rt) -
260eda14cbcSMatt Macy 	    rs_get_start(rs, rt)) {
261eda14cbcSMatt Macy 		zfs_panic_recover("zfs: attempting to increase fill beyond "
262eda14cbcSMatt Macy 		    "max; probable double add in segment [%llx:%llx]",
263eda14cbcSMatt Macy 		    (longlong_t)rs_get_start(rs, rt),
264eda14cbcSMatt Macy 		    (longlong_t)rs_get_end(rs, rt));
265eda14cbcSMatt Macy 	}
266eda14cbcSMatt Macy 
267eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
268eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
269eda14cbcSMatt Macy 	rs_set_fill(rs, rt, rs_get_fill(rs, rt) + delta);
270eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
271eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
272eda14cbcSMatt Macy }
273eda14cbcSMatt Macy 
274eda14cbcSMatt Macy static void
275eda14cbcSMatt Macy range_tree_add_impl(void *arg, uint64_t start, uint64_t size, uint64_t fill)
276eda14cbcSMatt Macy {
277eda14cbcSMatt Macy 	range_tree_t *rt = arg;
278eda14cbcSMatt Macy 	zfs_btree_index_t where;
279eda14cbcSMatt Macy 	range_seg_t *rs_before, *rs_after, *rs;
280eda14cbcSMatt Macy 	range_seg_max_t tmp, rsearch;
281eda14cbcSMatt Macy 	uint64_t end = start + size, gap = rt->rt_gap;
282eda14cbcSMatt Macy 	uint64_t bridge_size = 0;
283eda14cbcSMatt Macy 	boolean_t merge_before, merge_after;
284eda14cbcSMatt Macy 
285eda14cbcSMatt Macy 	ASSERT3U(size, !=, 0);
286eda14cbcSMatt Macy 	ASSERT3U(fill, <=, size);
287eda14cbcSMatt Macy 	ASSERT3U(start + size, >, start);
288eda14cbcSMatt Macy 
289eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
290eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
291eda14cbcSMatt Macy 	rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
292eda14cbcSMatt Macy 
293eda14cbcSMatt Macy 	/*
294eda14cbcSMatt Macy 	 * If this is a gap-supporting range tree, it is possible that we
295eda14cbcSMatt Macy 	 * are inserting into an existing segment. In this case simply
296eda14cbcSMatt Macy 	 * bump the fill count and call the remove / add callbacks. If the
297eda14cbcSMatt Macy 	 * new range will extend an existing segment, we remove the
298eda14cbcSMatt Macy 	 * existing one, apply the new extent to it and re-insert it using
299eda14cbcSMatt Macy 	 * the normal code paths.
300eda14cbcSMatt Macy 	 */
301eda14cbcSMatt Macy 	if (rs != NULL) {
302eda14cbcSMatt Macy 		if (gap == 0) {
303eda14cbcSMatt Macy 			zfs_panic_recover("zfs: adding existent segment to "
304eda14cbcSMatt Macy 			    "range tree (offset=%llx size=%llx)",
305eda14cbcSMatt Macy 			    (longlong_t)start, (longlong_t)size);
306eda14cbcSMatt Macy 			return;
307eda14cbcSMatt Macy 		}
308eda14cbcSMatt Macy 		uint64_t rstart = rs_get_start(rs, rt);
309eda14cbcSMatt Macy 		uint64_t rend = rs_get_end(rs, rt);
310eda14cbcSMatt Macy 		if (rstart <= start && rend >= end) {
311eda14cbcSMatt Macy 			range_tree_adjust_fill(rt, rs, fill);
312eda14cbcSMatt Macy 			return;
313eda14cbcSMatt Macy 		}
314eda14cbcSMatt Macy 
315eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
316eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
317eda14cbcSMatt Macy 
318eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs);
319eda14cbcSMatt Macy 		rt->rt_space -= rend - rstart;
320eda14cbcSMatt Macy 
321eda14cbcSMatt Macy 		fill += rs_get_fill(rs, rt);
322eda14cbcSMatt Macy 		start = MIN(start, rstart);
323eda14cbcSMatt Macy 		end = MAX(end, rend);
324eda14cbcSMatt Macy 		size = end - start;
325eda14cbcSMatt Macy 
326180f8225SMatt Macy 		zfs_btree_remove(&rt->rt_root, rs);
327eda14cbcSMatt Macy 		range_tree_add_impl(rt, start, size, fill);
328eda14cbcSMatt Macy 		return;
329eda14cbcSMatt Macy 	}
330eda14cbcSMatt Macy 
331eda14cbcSMatt Macy 	ASSERT3P(rs, ==, NULL);
332eda14cbcSMatt Macy 
333eda14cbcSMatt Macy 	/*
334eda14cbcSMatt Macy 	 * Determine whether or not we will have to merge with our neighbors.
335eda14cbcSMatt Macy 	 * If gap != 0, we might need to merge with our neighbors even if we
336eda14cbcSMatt Macy 	 * aren't directly touching.
337eda14cbcSMatt Macy 	 */
338eda14cbcSMatt Macy 	zfs_btree_index_t where_before, where_after;
339eda14cbcSMatt Macy 	rs_before = zfs_btree_prev(&rt->rt_root, &where, &where_before);
340eda14cbcSMatt Macy 	rs_after = zfs_btree_next(&rt->rt_root, &where, &where_after);
341eda14cbcSMatt Macy 
342eda14cbcSMatt Macy 	merge_before = (rs_before != NULL && rs_get_end(rs_before, rt) >=
343eda14cbcSMatt Macy 	    start - gap);
344eda14cbcSMatt Macy 	merge_after = (rs_after != NULL && rs_get_start(rs_after, rt) <= end +
345eda14cbcSMatt Macy 	    gap);
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 	if (merge_before && gap != 0)
348eda14cbcSMatt Macy 		bridge_size += start - rs_get_end(rs_before, rt);
349eda14cbcSMatt Macy 	if (merge_after && gap != 0)
350eda14cbcSMatt Macy 		bridge_size += rs_get_start(rs_after, rt) - end;
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy 	if (merge_before && merge_after) {
353eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL) {
354eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
355eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
356eda14cbcSMatt Macy 		}
357eda14cbcSMatt Macy 
358eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_before);
359eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_after);
360eda14cbcSMatt Macy 
361eda14cbcSMatt Macy 		rs_copy(rs_after, &tmp, rt);
362eda14cbcSMatt Macy 		uint64_t before_start = rs_get_start_raw(rs_before, rt);
363eda14cbcSMatt Macy 		uint64_t before_fill = rs_get_fill(rs_before, rt);
364eda14cbcSMatt Macy 		uint64_t after_fill = rs_get_fill(rs_after, rt);
365eda14cbcSMatt Macy 		zfs_btree_remove_idx(&rt->rt_root, &where_before);
366eda14cbcSMatt Macy 
367eda14cbcSMatt Macy 		/*
368eda14cbcSMatt Macy 		 * We have to re-find the node because our old reference is
369eda14cbcSMatt Macy 		 * invalid as soon as we do any mutating btree operations.
370eda14cbcSMatt Macy 		 */
371eda14cbcSMatt Macy 		rs_after = zfs_btree_find(&rt->rt_root, &tmp, &where_after);
372*dbd5678dSMartin Matuska 		ASSERT3P(rs_after, !=, NULL);
373eda14cbcSMatt Macy 		rs_set_start_raw(rs_after, rt, before_start);
374eda14cbcSMatt Macy 		rs_set_fill(rs_after, rt, after_fill + before_fill + fill);
375eda14cbcSMatt Macy 		rs = rs_after;
376eda14cbcSMatt Macy 	} else if (merge_before) {
377eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
378eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_before, rt->rt_arg);
379eda14cbcSMatt Macy 
380eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_before);
381eda14cbcSMatt Macy 
382eda14cbcSMatt Macy 		uint64_t before_fill = rs_get_fill(rs_before, rt);
383eda14cbcSMatt Macy 		rs_set_end(rs_before, rt, end);
384eda14cbcSMatt Macy 		rs_set_fill(rs_before, rt, before_fill + fill);
385eda14cbcSMatt Macy 		rs = rs_before;
386eda14cbcSMatt Macy 	} else if (merge_after) {
387eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
388eda14cbcSMatt Macy 			rt->rt_ops->rtop_remove(rt, rs_after, rt->rt_arg);
389eda14cbcSMatt Macy 
390eda14cbcSMatt Macy 		range_tree_stat_decr(rt, rs_after);
391eda14cbcSMatt Macy 
392eda14cbcSMatt Macy 		uint64_t after_fill = rs_get_fill(rs_after, rt);
393eda14cbcSMatt Macy 		rs_set_start(rs_after, rt, start);
394eda14cbcSMatt Macy 		rs_set_fill(rs_after, rt, after_fill + fill);
395eda14cbcSMatt Macy 		rs = rs_after;
396eda14cbcSMatt Macy 	} else {
397eda14cbcSMatt Macy 		rs = &tmp;
398eda14cbcSMatt Macy 
399eda14cbcSMatt Macy 		rs_set_start(rs, rt, start);
400eda14cbcSMatt Macy 		rs_set_end(rs, rt, end);
401eda14cbcSMatt Macy 		rs_set_fill(rs, rt, fill);
402eda14cbcSMatt Macy 		zfs_btree_add_idx(&rt->rt_root, rs, &where);
403eda14cbcSMatt Macy 	}
404eda14cbcSMatt Macy 
405eda14cbcSMatt Macy 	if (gap != 0) {
406eda14cbcSMatt Macy 		ASSERT3U(rs_get_fill(rs, rt), <=, rs_get_end(rs, rt) -
407eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
408eda14cbcSMatt Macy 	} else {
409eda14cbcSMatt Macy 		ASSERT3U(rs_get_fill(rs, rt), ==, rs_get_end(rs, rt) -
410eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
411eda14cbcSMatt Macy 	}
412eda14cbcSMatt Macy 
413eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
414eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
415eda14cbcSMatt Macy 
416eda14cbcSMatt Macy 	range_tree_stat_incr(rt, rs);
417eda14cbcSMatt Macy 	rt->rt_space += size + bridge_size;
418eda14cbcSMatt Macy }
419eda14cbcSMatt Macy 
420eda14cbcSMatt Macy void
421eda14cbcSMatt Macy range_tree_add(void *arg, uint64_t start, uint64_t size)
422eda14cbcSMatt Macy {
423eda14cbcSMatt Macy 	range_tree_add_impl(arg, start, size, size);
424eda14cbcSMatt Macy }
425eda14cbcSMatt Macy 
426eda14cbcSMatt Macy static void
427eda14cbcSMatt Macy range_tree_remove_impl(range_tree_t *rt, uint64_t start, uint64_t size,
428eda14cbcSMatt Macy     boolean_t do_fill)
429eda14cbcSMatt Macy {
430eda14cbcSMatt Macy 	zfs_btree_index_t where;
431eda14cbcSMatt Macy 	range_seg_t *rs;
432eda14cbcSMatt Macy 	range_seg_max_t rsearch, rs_tmp;
433eda14cbcSMatt Macy 	uint64_t end = start + size;
434eda14cbcSMatt Macy 	boolean_t left_over, right_over;
435eda14cbcSMatt Macy 
436eda14cbcSMatt Macy 	VERIFY3U(size, !=, 0);
437eda14cbcSMatt Macy 	VERIFY3U(size, <=, rt->rt_space);
438eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
439eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
440eda14cbcSMatt Macy 
441eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
442eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
443eda14cbcSMatt Macy 	rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
444eda14cbcSMatt Macy 
445eda14cbcSMatt Macy 	/* Make sure we completely overlap with someone */
446eda14cbcSMatt Macy 	if (rs == NULL) {
447eda14cbcSMatt Macy 		zfs_panic_recover("zfs: removing nonexistent segment from "
448eda14cbcSMatt Macy 		    "range tree (offset=%llx size=%llx)",
449eda14cbcSMatt Macy 		    (longlong_t)start, (longlong_t)size);
450eda14cbcSMatt Macy 		return;
451eda14cbcSMatt Macy 	}
452eda14cbcSMatt Macy 
453eda14cbcSMatt Macy 	/*
454eda14cbcSMatt Macy 	 * Range trees with gap support must only remove complete segments
455eda14cbcSMatt Macy 	 * from the tree. This allows us to maintain accurate fill accounting
456eda14cbcSMatt Macy 	 * and to ensure that bridged sections are not leaked. If we need to
457eda14cbcSMatt Macy 	 * remove less than the full segment, we can only adjust the fill count.
458eda14cbcSMatt Macy 	 */
459eda14cbcSMatt Macy 	if (rt->rt_gap != 0) {
460eda14cbcSMatt Macy 		if (do_fill) {
461eda14cbcSMatt Macy 			if (rs_get_fill(rs, rt) == size) {
462eda14cbcSMatt Macy 				start = rs_get_start(rs, rt);
463eda14cbcSMatt Macy 				end = rs_get_end(rs, rt);
464eda14cbcSMatt Macy 				size = end - start;
465eda14cbcSMatt Macy 			} else {
466eda14cbcSMatt Macy 				range_tree_adjust_fill(rt, rs, -size);
467eda14cbcSMatt Macy 				return;
468eda14cbcSMatt Macy 			}
469eda14cbcSMatt Macy 		} else if (rs_get_start(rs, rt) != start ||
470eda14cbcSMatt Macy 		    rs_get_end(rs, rt) != end) {
471eda14cbcSMatt Macy 			zfs_panic_recover("zfs: freeing partial segment of "
472eda14cbcSMatt Macy 			    "gap tree (offset=%llx size=%llx) of "
473eda14cbcSMatt Macy 			    "(offset=%llx size=%llx)",
474eda14cbcSMatt Macy 			    (longlong_t)start, (longlong_t)size,
475eda14cbcSMatt Macy 			    (longlong_t)rs_get_start(rs, rt),
476eda14cbcSMatt Macy 			    (longlong_t)rs_get_end(rs, rt) - rs_get_start(rs,
477eda14cbcSMatt Macy 			    rt));
478eda14cbcSMatt Macy 			return;
479eda14cbcSMatt Macy 		}
480eda14cbcSMatt Macy 	}
481eda14cbcSMatt Macy 
482eda14cbcSMatt Macy 	VERIFY3U(rs_get_start(rs, rt), <=, start);
483eda14cbcSMatt Macy 	VERIFY3U(rs_get_end(rs, rt), >=, end);
484eda14cbcSMatt Macy 
485eda14cbcSMatt Macy 	left_over = (rs_get_start(rs, rt) != start);
486eda14cbcSMatt Macy 	right_over = (rs_get_end(rs, rt) != end);
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	range_tree_stat_decr(rt, rs);
489eda14cbcSMatt Macy 
490eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
491eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
492eda14cbcSMatt Macy 
493eda14cbcSMatt Macy 	if (left_over && right_over) {
494eda14cbcSMatt Macy 		range_seg_max_t newseg;
495eda14cbcSMatt Macy 		rs_set_start(&newseg, rt, end);
496eda14cbcSMatt Macy 		rs_set_end_raw(&newseg, rt, rs_get_end_raw(rs, rt));
497eda14cbcSMatt Macy 		rs_set_fill(&newseg, rt, rs_get_end(rs, rt) - end);
498eda14cbcSMatt Macy 		range_tree_stat_incr(rt, &newseg);
499eda14cbcSMatt Macy 
500eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
501eda14cbcSMatt Macy 		rs_set_end(rs, rt, start);
502eda14cbcSMatt Macy 
503eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
504eda14cbcSMatt Macy 		if (zfs_btree_next(&rt->rt_root, &where, &where) != NULL)
505eda14cbcSMatt Macy 			zfs_btree_add_idx(&rt->rt_root, &newseg, &where);
506eda14cbcSMatt Macy 		else
507eda14cbcSMatt Macy 			zfs_btree_add(&rt->rt_root, &newseg);
508eda14cbcSMatt Macy 
509eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
510eda14cbcSMatt Macy 			rt->rt_ops->rtop_add(rt, &newseg, rt->rt_arg);
511eda14cbcSMatt Macy 	} else if (left_over) {
512eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
513eda14cbcSMatt Macy 		rs_set_end(rs, rt, start);
514eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
515eda14cbcSMatt Macy 	} else if (right_over) {
516eda14cbcSMatt Macy 		// This modifies the buffer already inside the range tree
517eda14cbcSMatt Macy 		rs_set_start(rs, rt, end);
518eda14cbcSMatt Macy 		rs_copy(rs, &rs_tmp, rt);
519eda14cbcSMatt Macy 	} else {
520eda14cbcSMatt Macy 		zfs_btree_remove_idx(&rt->rt_root, &where);
521eda14cbcSMatt Macy 		rs = NULL;
522eda14cbcSMatt Macy 	}
523eda14cbcSMatt Macy 
524eda14cbcSMatt Macy 	if (rs != NULL) {
525eda14cbcSMatt Macy 		/*
526eda14cbcSMatt Macy 		 * The fill of the leftover segment will always be equal to
527eda14cbcSMatt Macy 		 * the size, since we do not support removing partial segments
528eda14cbcSMatt Macy 		 * of range trees with gaps.
529eda14cbcSMatt Macy 		 */
530eda14cbcSMatt Macy 		rs_set_fill_raw(rs, rt, rs_get_end_raw(rs, rt) -
531eda14cbcSMatt Macy 		    rs_get_start_raw(rs, rt));
532eda14cbcSMatt Macy 		range_tree_stat_incr(rt, &rs_tmp);
533eda14cbcSMatt Macy 
534eda14cbcSMatt Macy 		if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
535eda14cbcSMatt Macy 			rt->rt_ops->rtop_add(rt, &rs_tmp, rt->rt_arg);
536eda14cbcSMatt Macy 	}
537eda14cbcSMatt Macy 
538eda14cbcSMatt Macy 	rt->rt_space -= size;
539eda14cbcSMatt Macy }
540eda14cbcSMatt Macy 
541eda14cbcSMatt Macy void
542eda14cbcSMatt Macy range_tree_remove(void *arg, uint64_t start, uint64_t size)
543eda14cbcSMatt Macy {
544eda14cbcSMatt Macy 	range_tree_remove_impl(arg, start, size, B_FALSE);
545eda14cbcSMatt Macy }
546eda14cbcSMatt Macy 
547eda14cbcSMatt Macy void
548eda14cbcSMatt Macy range_tree_remove_fill(range_tree_t *rt, uint64_t start, uint64_t size)
549eda14cbcSMatt Macy {
550eda14cbcSMatt Macy 	range_tree_remove_impl(rt, start, size, B_TRUE);
551eda14cbcSMatt Macy }
552eda14cbcSMatt Macy 
553eda14cbcSMatt Macy void
554eda14cbcSMatt Macy range_tree_resize_segment(range_tree_t *rt, range_seg_t *rs,
555eda14cbcSMatt Macy     uint64_t newstart, uint64_t newsize)
556eda14cbcSMatt Macy {
557eda14cbcSMatt Macy 	int64_t delta = newsize - (rs_get_end(rs, rt) - rs_get_start(rs, rt));
558eda14cbcSMatt Macy 
559eda14cbcSMatt Macy 	range_tree_stat_decr(rt, rs);
560eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_remove != NULL)
561eda14cbcSMatt Macy 		rt->rt_ops->rtop_remove(rt, rs, rt->rt_arg);
562eda14cbcSMatt Macy 
563eda14cbcSMatt Macy 	rs_set_start(rs, rt, newstart);
564eda14cbcSMatt Macy 	rs_set_end(rs, rt, newstart + newsize);
565eda14cbcSMatt Macy 
566eda14cbcSMatt Macy 	range_tree_stat_incr(rt, rs);
567eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_add != NULL)
568eda14cbcSMatt Macy 		rt->rt_ops->rtop_add(rt, rs, rt->rt_arg);
569eda14cbcSMatt Macy 
570eda14cbcSMatt Macy 	rt->rt_space += delta;
571eda14cbcSMatt Macy }
572eda14cbcSMatt Macy 
573eda14cbcSMatt Macy static range_seg_t *
574eda14cbcSMatt Macy range_tree_find_impl(range_tree_t *rt, uint64_t start, uint64_t size)
575eda14cbcSMatt Macy {
576eda14cbcSMatt Macy 	range_seg_max_t rsearch;
577eda14cbcSMatt Macy 	uint64_t end = start + size;
578eda14cbcSMatt Macy 
579eda14cbcSMatt Macy 	VERIFY(size != 0);
580eda14cbcSMatt Macy 
581eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
582eda14cbcSMatt Macy 	rs_set_end(&rsearch, rt, end);
583eda14cbcSMatt Macy 	return (zfs_btree_find(&rt->rt_root, &rsearch, NULL));
584eda14cbcSMatt Macy }
585eda14cbcSMatt Macy 
586eda14cbcSMatt Macy range_seg_t *
587eda14cbcSMatt Macy range_tree_find(range_tree_t *rt, uint64_t start, uint64_t size)
588eda14cbcSMatt Macy {
589eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
590eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
591eda14cbcSMatt Macy 
592eda14cbcSMatt Macy 	range_seg_t *rs = range_tree_find_impl(rt, start, size);
593eda14cbcSMatt Macy 	if (rs != NULL && rs_get_start(rs, rt) <= start &&
594eda14cbcSMatt Macy 	    rs_get_end(rs, rt) >= start + size) {
595eda14cbcSMatt Macy 		return (rs);
596eda14cbcSMatt Macy 	}
597eda14cbcSMatt Macy 	return (NULL);
598eda14cbcSMatt Macy }
599eda14cbcSMatt Macy 
600eda14cbcSMatt Macy void
601eda14cbcSMatt Macy range_tree_verify_not_present(range_tree_t *rt, uint64_t off, uint64_t size)
602eda14cbcSMatt Macy {
603eda14cbcSMatt Macy 	range_seg_t *rs = range_tree_find(rt, off, size);
604eda14cbcSMatt Macy 	if (rs != NULL)
605eda14cbcSMatt Macy 		panic("segment already in tree; rs=%p", (void *)rs);
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy 
608eda14cbcSMatt Macy boolean_t
609eda14cbcSMatt Macy range_tree_contains(range_tree_t *rt, uint64_t start, uint64_t size)
610eda14cbcSMatt Macy {
611eda14cbcSMatt Macy 	return (range_tree_find(rt, start, size) != NULL);
612eda14cbcSMatt Macy }
613eda14cbcSMatt Macy 
614eda14cbcSMatt Macy /*
615eda14cbcSMatt Macy  * Returns the first subset of the given range which overlaps with the range
616eda14cbcSMatt Macy  * tree. Returns true if there is a segment in the range, and false if there
617eda14cbcSMatt Macy  * isn't.
618eda14cbcSMatt Macy  */
619eda14cbcSMatt Macy boolean_t
620eda14cbcSMatt Macy range_tree_find_in(range_tree_t *rt, uint64_t start, uint64_t size,
621eda14cbcSMatt Macy     uint64_t *ostart, uint64_t *osize)
622eda14cbcSMatt Macy {
623eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
624eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
625eda14cbcSMatt Macy 
626eda14cbcSMatt Macy 	range_seg_max_t rsearch;
627eda14cbcSMatt Macy 	rs_set_start(&rsearch, rt, start);
628eda14cbcSMatt Macy 	rs_set_end_raw(&rsearch, rt, rs_get_start_raw(&rsearch, rt) + 1);
629eda14cbcSMatt Macy 
630eda14cbcSMatt Macy 	zfs_btree_index_t where;
631eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_find(&rt->rt_root, &rsearch, &where);
632eda14cbcSMatt Macy 	if (rs != NULL) {
633eda14cbcSMatt Macy 		*ostart = start;
634eda14cbcSMatt Macy 		*osize = MIN(size, rs_get_end(rs, rt) - start);
635eda14cbcSMatt Macy 		return (B_TRUE);
636eda14cbcSMatt Macy 	}
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	rs = zfs_btree_next(&rt->rt_root, &where, &where);
639eda14cbcSMatt Macy 	if (rs == NULL || rs_get_start(rs, rt) > start + size)
640eda14cbcSMatt Macy 		return (B_FALSE);
641eda14cbcSMatt Macy 
642eda14cbcSMatt Macy 	*ostart = rs_get_start(rs, rt);
643eda14cbcSMatt Macy 	*osize = MIN(start + size, rs_get_end(rs, rt)) -
644eda14cbcSMatt Macy 	    rs_get_start(rs, rt);
645eda14cbcSMatt Macy 	return (B_TRUE);
646eda14cbcSMatt Macy }
647eda14cbcSMatt Macy 
648eda14cbcSMatt Macy /*
649eda14cbcSMatt Macy  * Ensure that this range is not in the tree, regardless of whether
650eda14cbcSMatt Macy  * it is currently in the tree.
651eda14cbcSMatt Macy  */
652eda14cbcSMatt Macy void
653eda14cbcSMatt Macy range_tree_clear(range_tree_t *rt, uint64_t start, uint64_t size)
654eda14cbcSMatt Macy {
655eda14cbcSMatt Macy 	range_seg_t *rs;
656eda14cbcSMatt Macy 
657eda14cbcSMatt Macy 	if (size == 0)
658eda14cbcSMatt Macy 		return;
659eda14cbcSMatt Macy 
660eda14cbcSMatt Macy 	if (rt->rt_type == RANGE_SEG64)
661eda14cbcSMatt Macy 		ASSERT3U(start + size, >, start);
662eda14cbcSMatt Macy 
663eda14cbcSMatt Macy 	while ((rs = range_tree_find_impl(rt, start, size)) != NULL) {
664eda14cbcSMatt Macy 		uint64_t free_start = MAX(rs_get_start(rs, rt), start);
665eda14cbcSMatt Macy 		uint64_t free_end = MIN(rs_get_end(rs, rt), start + size);
666eda14cbcSMatt Macy 		range_tree_remove(rt, free_start, free_end - free_start);
667eda14cbcSMatt Macy 	}
668eda14cbcSMatt Macy }
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy void
671eda14cbcSMatt Macy range_tree_swap(range_tree_t **rtsrc, range_tree_t **rtdst)
672eda14cbcSMatt Macy {
673eda14cbcSMatt Macy 	range_tree_t *rt;
674eda14cbcSMatt Macy 
675eda14cbcSMatt Macy 	ASSERT0(range_tree_space(*rtdst));
676eda14cbcSMatt Macy 	ASSERT0(zfs_btree_numnodes(&(*rtdst)->rt_root));
677eda14cbcSMatt Macy 
678eda14cbcSMatt Macy 	rt = *rtsrc;
679eda14cbcSMatt Macy 	*rtsrc = *rtdst;
680eda14cbcSMatt Macy 	*rtdst = rt;
681eda14cbcSMatt Macy }
682eda14cbcSMatt Macy 
683eda14cbcSMatt Macy void
684eda14cbcSMatt Macy range_tree_vacate(range_tree_t *rt, range_tree_func_t *func, void *arg)
685eda14cbcSMatt Macy {
686eda14cbcSMatt Macy 	if (rt->rt_ops != NULL && rt->rt_ops->rtop_vacate != NULL)
687eda14cbcSMatt Macy 		rt->rt_ops->rtop_vacate(rt, rt->rt_arg);
688eda14cbcSMatt Macy 
689eda14cbcSMatt Macy 	if (func != NULL) {
690eda14cbcSMatt Macy 		range_seg_t *rs;
691eda14cbcSMatt Macy 		zfs_btree_index_t *cookie = NULL;
692eda14cbcSMatt Macy 
693eda14cbcSMatt Macy 		while ((rs = zfs_btree_destroy_nodes(&rt->rt_root, &cookie)) !=
694eda14cbcSMatt Macy 		    NULL) {
695eda14cbcSMatt Macy 			func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) -
696eda14cbcSMatt Macy 			    rs_get_start(rs, rt));
697eda14cbcSMatt Macy 		}
698eda14cbcSMatt Macy 	} else {
699eda14cbcSMatt Macy 		zfs_btree_clear(&rt->rt_root);
700eda14cbcSMatt Macy 	}
701eda14cbcSMatt Macy 
702da5137abSMartin Matuska 	memset(rt->rt_histogram, 0, sizeof (rt->rt_histogram));
703eda14cbcSMatt Macy 	rt->rt_space = 0;
704eda14cbcSMatt Macy }
705eda14cbcSMatt Macy 
706eda14cbcSMatt Macy void
707eda14cbcSMatt Macy range_tree_walk(range_tree_t *rt, range_tree_func_t *func, void *arg)
708eda14cbcSMatt Macy {
709eda14cbcSMatt Macy 	zfs_btree_index_t where;
710eda14cbcSMatt Macy 	for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where);
711eda14cbcSMatt Macy 	    rs != NULL; rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
712eda14cbcSMatt Macy 		func(arg, rs_get_start(rs, rt), rs_get_end(rs, rt) -
713eda14cbcSMatt Macy 		    rs_get_start(rs, rt));
714eda14cbcSMatt Macy 	}
715eda14cbcSMatt Macy }
716eda14cbcSMatt Macy 
717eda14cbcSMatt Macy range_seg_t *
718eda14cbcSMatt Macy range_tree_first(range_tree_t *rt)
719eda14cbcSMatt Macy {
720eda14cbcSMatt Macy 	return (zfs_btree_first(&rt->rt_root, NULL));
721eda14cbcSMatt Macy }
722eda14cbcSMatt Macy 
723eda14cbcSMatt Macy uint64_t
724eda14cbcSMatt Macy range_tree_space(range_tree_t *rt)
725eda14cbcSMatt Macy {
726eda14cbcSMatt Macy 	return (rt->rt_space);
727eda14cbcSMatt Macy }
728eda14cbcSMatt Macy 
729eda14cbcSMatt Macy uint64_t
730eda14cbcSMatt Macy range_tree_numsegs(range_tree_t *rt)
731eda14cbcSMatt Macy {
732eda14cbcSMatt Macy 	return ((rt == NULL) ? 0 : zfs_btree_numnodes(&rt->rt_root));
733eda14cbcSMatt Macy }
734eda14cbcSMatt Macy 
735eda14cbcSMatt Macy boolean_t
736eda14cbcSMatt Macy range_tree_is_empty(range_tree_t *rt)
737eda14cbcSMatt Macy {
738eda14cbcSMatt Macy 	ASSERT(rt != NULL);
739eda14cbcSMatt Macy 	return (range_tree_space(rt) == 0);
740eda14cbcSMatt Macy }
741eda14cbcSMatt Macy 
742eda14cbcSMatt Macy /*
743eda14cbcSMatt Macy  * Remove any overlapping ranges between the given segment [start, end)
744eda14cbcSMatt Macy  * from removefrom. Add non-overlapping leftovers to addto.
745eda14cbcSMatt Macy  */
746eda14cbcSMatt Macy void
747eda14cbcSMatt Macy range_tree_remove_xor_add_segment(uint64_t start, uint64_t end,
748eda14cbcSMatt Macy     range_tree_t *removefrom, range_tree_t *addto)
749eda14cbcSMatt Macy {
750eda14cbcSMatt Macy 	zfs_btree_index_t where;
751eda14cbcSMatt Macy 	range_seg_max_t starting_rs;
752eda14cbcSMatt Macy 	rs_set_start(&starting_rs, removefrom, start);
753eda14cbcSMatt Macy 	rs_set_end_raw(&starting_rs, removefrom, rs_get_start_raw(&starting_rs,
754eda14cbcSMatt Macy 	    removefrom) + 1);
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy 	range_seg_t *curr = zfs_btree_find(&removefrom->rt_root,
757eda14cbcSMatt Macy 	    &starting_rs, &where);
758eda14cbcSMatt Macy 
759eda14cbcSMatt Macy 	if (curr == NULL)
760eda14cbcSMatt Macy 		curr = zfs_btree_next(&removefrom->rt_root, &where, &where);
761eda14cbcSMatt Macy 
762eda14cbcSMatt Macy 	range_seg_t *next;
763eda14cbcSMatt Macy 	for (; curr != NULL; curr = next) {
764eda14cbcSMatt Macy 		if (start == end)
765eda14cbcSMatt Macy 			return;
766eda14cbcSMatt Macy 		VERIFY3U(start, <, end);
767eda14cbcSMatt Macy 
768eda14cbcSMatt Macy 		/* there is no overlap */
769eda14cbcSMatt Macy 		if (end <= rs_get_start(curr, removefrom)) {
770eda14cbcSMatt Macy 			range_tree_add(addto, start, end - start);
771eda14cbcSMatt Macy 			return;
772eda14cbcSMatt Macy 		}
773eda14cbcSMatt Macy 
774eda14cbcSMatt Macy 		uint64_t overlap_start = MAX(rs_get_start(curr, removefrom),
775eda14cbcSMatt Macy 		    start);
776eda14cbcSMatt Macy 		uint64_t overlap_end = MIN(rs_get_end(curr, removefrom),
777eda14cbcSMatt Macy 		    end);
778eda14cbcSMatt Macy 		uint64_t overlap_size = overlap_end - overlap_start;
779eda14cbcSMatt Macy 		ASSERT3S(overlap_size, >, 0);
780eda14cbcSMatt Macy 		range_seg_max_t rs;
781eda14cbcSMatt Macy 		rs_copy(curr, &rs, removefrom);
782eda14cbcSMatt Macy 
783eda14cbcSMatt Macy 		range_tree_remove(removefrom, overlap_start, overlap_size);
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 		if (start < overlap_start)
786eda14cbcSMatt Macy 			range_tree_add(addto, start, overlap_start - start);
787eda14cbcSMatt Macy 
788eda14cbcSMatt Macy 		start = overlap_end;
789eda14cbcSMatt Macy 		next = zfs_btree_find(&removefrom->rt_root, &rs, &where);
790eda14cbcSMatt Macy 		/*
791eda14cbcSMatt Macy 		 * If we find something here, we only removed part of the
792eda14cbcSMatt Macy 		 * curr segment. Either there's some left at the end
793eda14cbcSMatt Macy 		 * because we've reached the end of the range we're removing,
794eda14cbcSMatt Macy 		 * or there's some left at the start because we started
795eda14cbcSMatt Macy 		 * partway through the range.  Either way, we continue with
796eda14cbcSMatt Macy 		 * the loop. If it's the former, we'll return at the start of
797eda14cbcSMatt Macy 		 * the loop, and if it's the latter we'll see if there is more
798eda14cbcSMatt Macy 		 * area to process.
799eda14cbcSMatt Macy 		 */
800eda14cbcSMatt Macy 		if (next != NULL) {
801eda14cbcSMatt Macy 			ASSERT(start == end || start == rs_get_end(&rs,
802eda14cbcSMatt Macy 			    removefrom));
803eda14cbcSMatt Macy 		}
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy 		next = zfs_btree_next(&removefrom->rt_root, &where, &where);
806eda14cbcSMatt Macy 	}
807eda14cbcSMatt Macy 	VERIFY3P(curr, ==, NULL);
808eda14cbcSMatt Macy 
809eda14cbcSMatt Macy 	if (start != end) {
810eda14cbcSMatt Macy 		VERIFY3U(start, <, end);
811eda14cbcSMatt Macy 		range_tree_add(addto, start, end - start);
812eda14cbcSMatt Macy 	} else {
813eda14cbcSMatt Macy 		VERIFY3U(start, ==, end);
814eda14cbcSMatt Macy 	}
815eda14cbcSMatt Macy }
816eda14cbcSMatt Macy 
817eda14cbcSMatt Macy /*
818eda14cbcSMatt Macy  * For each entry in rt, if it exists in removefrom, remove it
819eda14cbcSMatt Macy  * from removefrom. Otherwise, add it to addto.
820eda14cbcSMatt Macy  */
821eda14cbcSMatt Macy void
822eda14cbcSMatt Macy range_tree_remove_xor_add(range_tree_t *rt, range_tree_t *removefrom,
823eda14cbcSMatt Macy     range_tree_t *addto)
824eda14cbcSMatt Macy {
825eda14cbcSMatt Macy 	zfs_btree_index_t where;
826eda14cbcSMatt Macy 	for (range_seg_t *rs = zfs_btree_first(&rt->rt_root, &where); rs;
827eda14cbcSMatt Macy 	    rs = zfs_btree_next(&rt->rt_root, &where, &where)) {
828eda14cbcSMatt Macy 		range_tree_remove_xor_add_segment(rs_get_start(rs, rt),
829eda14cbcSMatt Macy 		    rs_get_end(rs, rt), removefrom, addto);
830eda14cbcSMatt Macy 	}
831eda14cbcSMatt Macy }
832eda14cbcSMatt Macy 
833eda14cbcSMatt Macy uint64_t
834eda14cbcSMatt Macy range_tree_min(range_tree_t *rt)
835eda14cbcSMatt Macy {
836eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_first(&rt->rt_root, NULL);
837eda14cbcSMatt Macy 	return (rs != NULL ? rs_get_start(rs, rt) : 0);
838eda14cbcSMatt Macy }
839eda14cbcSMatt Macy 
840eda14cbcSMatt Macy uint64_t
841eda14cbcSMatt Macy range_tree_max(range_tree_t *rt)
842eda14cbcSMatt Macy {
843eda14cbcSMatt Macy 	range_seg_t *rs = zfs_btree_last(&rt->rt_root, NULL);
844eda14cbcSMatt Macy 	return (rs != NULL ? rs_get_end(rs, rt) : 0);
845eda14cbcSMatt Macy }
846eda14cbcSMatt Macy 
847eda14cbcSMatt Macy uint64_t
848eda14cbcSMatt Macy range_tree_span(range_tree_t *rt)
849eda14cbcSMatt Macy {
850eda14cbcSMatt Macy 	return (range_tree_max(rt) - range_tree_min(rt));
851eda14cbcSMatt Macy }
852