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