xref: /freebsd/sys/contrib/openzfs/module/zfs/btree.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1*61145dc2SMartin Matuska // SPDX-License-Identifier: CDDL-1.0
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy  * CDDL HEADER START
4eda14cbcSMatt Macy  *
5eda14cbcSMatt Macy  * This file and its contents are supplied under the terms of the
6eda14cbcSMatt Macy  * Common Development and Distribution License ("CDDL"), version 1.0.
7eda14cbcSMatt Macy  * You may only use this file in accordance with the terms of version
8eda14cbcSMatt Macy  * 1.0 of the CDDL.
9eda14cbcSMatt Macy  *
10eda14cbcSMatt Macy  * A full copy of the text of the CDDL should have accompanied this
11eda14cbcSMatt Macy  * source.  A copy of the CDDL is also available via the Internet at
12eda14cbcSMatt Macy  * http://www.illumos.org/license/CDDL.
13eda14cbcSMatt Macy  *
14eda14cbcSMatt Macy  * CDDL HEADER END
15eda14cbcSMatt Macy  */
16eda14cbcSMatt Macy /*
17eda14cbcSMatt Macy  * Copyright (c) 2019 by Delphix. All rights reserved.
18eda14cbcSMatt Macy  */
19eda14cbcSMatt Macy 
20eda14cbcSMatt Macy #include	<sys/btree.h>
21eda14cbcSMatt Macy #include	<sys/bitops.h>
22eda14cbcSMatt Macy #include	<sys/zfs_context.h>
23eda14cbcSMatt Macy 
24eda14cbcSMatt Macy kmem_cache_t *zfs_btree_leaf_cache;
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy /*
27eda14cbcSMatt Macy  * Control the extent of the verification that occurs when zfs_btree_verify is
28eda14cbcSMatt Macy  * called. Primarily used for debugging when extending the btree logic and
29eda14cbcSMatt Macy  * functionality. As the intensity is increased, new verification steps are
30eda14cbcSMatt Macy  * added. These steps are cumulative; intensity = 3 includes the intensity = 1
31eda14cbcSMatt Macy  * and intensity = 2 steps as well.
32eda14cbcSMatt Macy  *
33eda14cbcSMatt Macy  * Intensity 1: Verify that the tree's height is consistent throughout.
34eda14cbcSMatt Macy  * Intensity 2: Verify that a core node's children's parent pointers point
35eda14cbcSMatt Macy  * to the core node.
36eda14cbcSMatt Macy  * Intensity 3: Verify that the total number of elements in the tree matches the
37eda14cbcSMatt Macy  * sum of the number of elements in each node. Also verifies that each node's
38eda14cbcSMatt Macy  * count obeys the invariants (less than or equal to maximum value, greater than
39eda14cbcSMatt Macy  * or equal to half the maximum minus one).
40eda14cbcSMatt Macy  * Intensity 4: Verify that each element compares less than the element
41eda14cbcSMatt Macy  * immediately after it and greater than the one immediately before it using the
42eda14cbcSMatt Macy  * comparator function. For core nodes, also checks that each element is greater
43eda14cbcSMatt Macy  * than the last element in the first of the two nodes it separates, and less
44eda14cbcSMatt Macy  * than the first element in the second of the two nodes.
45eda14cbcSMatt Macy  * Intensity 5: Verifies, if ZFS_DEBUG is defined, that all unused memory inside
46eda14cbcSMatt Macy  * of each node is poisoned appropriately. Note that poisoning always occurs if
47eda14cbcSMatt Macy  * ZFS_DEBUG is set, so it is safe to set the intensity to 5 during normal
48eda14cbcSMatt Macy  * operation.
49eda14cbcSMatt Macy  *
50eda14cbcSMatt Macy  * Intensity 4 and 5 are particularly expensive to perform; the previous levels
51eda14cbcSMatt Macy  * are a few memory operations per node, while these levels require multiple
52eda14cbcSMatt Macy  * operations per element. In addition, when creating large btrees, these
53eda14cbcSMatt Macy  * operations are called at every step, resulting in extremely slow operation
54eda14cbcSMatt Macy  * (while the asymptotic complexity of the other steps is the same, the
55eda14cbcSMatt Macy  * importance of the constant factors cannot be denied).
56eda14cbcSMatt Macy  */
57c7046f76SMartin Matuska uint_t zfs_btree_verify_intensity = 0;
58eda14cbcSMatt Macy 
59eda14cbcSMatt Macy /*
60a0b956f5SMartin Matuska  * Convenience functions to silence warnings from memcpy/memmove's
61a0b956f5SMartin Matuska  * return values and change argument order to src, dest.
62eda14cbcSMatt Macy  */
63eda14cbcSMatt Macy static void
bcpy(const void * src,void * dest,size_t size)64a0b956f5SMartin Matuska bcpy(const void *src, void *dest, size_t size)
65a0b956f5SMartin Matuska {
66a0b956f5SMartin Matuska 	(void) memcpy(dest, src, size);
67a0b956f5SMartin Matuska }
68a0b956f5SMartin Matuska 
69a0b956f5SMartin Matuska static void
bmov(const void * src,void * dest,size_t size)70eda14cbcSMatt Macy bmov(const void *src, void *dest, size_t size)
71eda14cbcSMatt Macy {
72eda14cbcSMatt Macy 	(void) memmove(dest, src, size);
73eda14cbcSMatt Macy }
74eda14cbcSMatt Macy 
75a0b956f5SMartin Matuska static boolean_t
zfs_btree_is_core(struct zfs_btree_hdr * hdr)76a0b956f5SMartin Matuska zfs_btree_is_core(struct zfs_btree_hdr *hdr)
77a0b956f5SMartin Matuska {
78a0b956f5SMartin Matuska 	return (hdr->bth_first == -1);
79a0b956f5SMartin Matuska }
80a0b956f5SMartin Matuska 
81eda14cbcSMatt Macy #ifdef _ILP32
82eda14cbcSMatt Macy #define	BTREE_POISON 0xabadb10c
83eda14cbcSMatt Macy #else
84eda14cbcSMatt Macy #define	BTREE_POISON 0xabadb10cdeadbeef
85eda14cbcSMatt Macy #endif
86eda14cbcSMatt Macy 
87eda14cbcSMatt Macy static void
zfs_btree_poison_node(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)88eda14cbcSMatt Macy zfs_btree_poison_node(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
89eda14cbcSMatt Macy {
90eda14cbcSMatt Macy #ifdef ZFS_DEBUG
91eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
92a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
93eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
94a0b956f5SMartin Matuska 		for (uint32_t i = hdr->bth_count + 1; i <= BTREE_CORE_ELEMS;
95a0b956f5SMartin Matuska 		    i++) {
96eda14cbcSMatt Macy 			node->btc_children[i] =
97eda14cbcSMatt Macy 			    (zfs_btree_hdr_t *)BTREE_POISON;
98eda14cbcSMatt Macy 		}
99eda14cbcSMatt Macy 		(void) memset(node->btc_elems + hdr->bth_count * size, 0x0f,
100eda14cbcSMatt Macy 		    (BTREE_CORE_ELEMS - hdr->bth_count) * size);
101a0b956f5SMartin Matuska 	} else {
102a0b956f5SMartin Matuska 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
103a0b956f5SMartin Matuska 		(void) memset(leaf->btl_elems, 0x0f, hdr->bth_first * size);
104a0b956f5SMartin Matuska 		(void) memset(leaf->btl_elems +
105a0b956f5SMartin Matuska 		    (hdr->bth_first + hdr->bth_count) * size, 0x0f,
106dbd5678dSMartin Matuska 		    tree->bt_leaf_size - offsetof(zfs_btree_leaf_t, btl_elems) -
107a0b956f5SMartin Matuska 		    (hdr->bth_first + hdr->bth_count) * size);
108eda14cbcSMatt Macy 	}
109eda14cbcSMatt Macy #endif
110eda14cbcSMatt Macy }
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy static inline void
zfs_btree_poison_node_at(zfs_btree_t * tree,zfs_btree_hdr_t * hdr,uint32_t idx,uint32_t count)113eda14cbcSMatt Macy zfs_btree_poison_node_at(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
114a0b956f5SMartin Matuska     uint32_t idx, uint32_t count)
115eda14cbcSMatt Macy {
116eda14cbcSMatt Macy #ifdef ZFS_DEBUG
117eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
118a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
119a0b956f5SMartin Matuska 		ASSERT3U(idx, >=, hdr->bth_count);
120a0b956f5SMartin Matuska 		ASSERT3U(idx, <=, BTREE_CORE_ELEMS);
121a0b956f5SMartin Matuska 		ASSERT3U(idx + count, <=, BTREE_CORE_ELEMS);
122eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
123a0b956f5SMartin Matuska 		for (uint32_t i = 1; i <= count; i++) {
124a0b956f5SMartin Matuska 			node->btc_children[idx + i] =
125eda14cbcSMatt Macy 			    (zfs_btree_hdr_t *)BTREE_POISON;
126a0b956f5SMartin Matuska 		}
127a0b956f5SMartin Matuska 		(void) memset(node->btc_elems + idx * size, 0x0f, count * size);
128a0b956f5SMartin Matuska 	} else {
129a0b956f5SMartin Matuska 		ASSERT3U(idx, <=, tree->bt_leaf_cap);
130a0b956f5SMartin Matuska 		ASSERT3U(idx + count, <=, tree->bt_leaf_cap);
131a0b956f5SMartin Matuska 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
132a0b956f5SMartin Matuska 		(void) memset(leaf->btl_elems +
133a0b956f5SMartin Matuska 		    (hdr->bth_first + idx) * size, 0x0f, count * size);
134eda14cbcSMatt Macy 	}
135eda14cbcSMatt Macy #endif
136eda14cbcSMatt Macy }
137eda14cbcSMatt Macy 
138eda14cbcSMatt Macy static inline void
zfs_btree_verify_poison_at(zfs_btree_t * tree,zfs_btree_hdr_t * hdr,uint32_t idx)139eda14cbcSMatt Macy zfs_btree_verify_poison_at(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
140a0b956f5SMartin Matuska     uint32_t idx)
141eda14cbcSMatt Macy {
142eda14cbcSMatt Macy #ifdef ZFS_DEBUG
143eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
144a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
145a0b956f5SMartin Matuska 		ASSERT3U(idx, <, BTREE_CORE_ELEMS);
146eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
147eda14cbcSMatt Macy 		zfs_btree_hdr_t *cval = (zfs_btree_hdr_t *)BTREE_POISON;
148a0b956f5SMartin Matuska 		VERIFY3P(node->btc_children[idx + 1], ==, cval);
149a0b956f5SMartin Matuska 		for (size_t i = 0; i < size; i++)
150a0b956f5SMartin Matuska 			VERIFY3U(node->btc_elems[idx * size + i], ==, 0x0f);
151eda14cbcSMatt Macy 	} else  {
152a0b956f5SMartin Matuska 		ASSERT3U(idx, <, tree->bt_leaf_cap);
153eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
154a0b956f5SMartin Matuska 		if (idx >= tree->bt_leaf_cap - hdr->bth_first)
155a0b956f5SMartin Matuska 			return;
156a0b956f5SMartin Matuska 		for (size_t i = 0; i < size; i++) {
157a0b956f5SMartin Matuska 			VERIFY3U(leaf->btl_elems[(hdr->bth_first + idx)
158a0b956f5SMartin Matuska 			    * size + i], ==, 0x0f);
159a0b956f5SMartin Matuska 		}
160eda14cbcSMatt Macy 	}
161eda14cbcSMatt Macy #endif
162eda14cbcSMatt Macy }
163eda14cbcSMatt Macy 
164eda14cbcSMatt Macy void
zfs_btree_init(void)165eda14cbcSMatt Macy zfs_btree_init(void)
166eda14cbcSMatt Macy {
167eda14cbcSMatt Macy 	zfs_btree_leaf_cache = kmem_cache_create("zfs_btree_leaf_cache",
168a0b956f5SMartin Matuska 	    BTREE_LEAF_SIZE, 0, NULL, NULL, NULL, NULL, NULL, 0);
169eda14cbcSMatt Macy }
170eda14cbcSMatt Macy 
171eda14cbcSMatt Macy void
zfs_btree_fini(void)172eda14cbcSMatt Macy zfs_btree_fini(void)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy 	kmem_cache_destroy(zfs_btree_leaf_cache);
175eda14cbcSMatt Macy }
176eda14cbcSMatt Macy 
177dbd5678dSMartin Matuska static void *
zfs_btree_leaf_alloc(zfs_btree_t * tree)178dbd5678dSMartin Matuska zfs_btree_leaf_alloc(zfs_btree_t *tree)
179dbd5678dSMartin Matuska {
180dbd5678dSMartin Matuska 	if (tree->bt_leaf_size == BTREE_LEAF_SIZE)
181dbd5678dSMartin Matuska 		return (kmem_cache_alloc(zfs_btree_leaf_cache, KM_SLEEP));
182dbd5678dSMartin Matuska 	else
183dbd5678dSMartin Matuska 		return (kmem_alloc(tree->bt_leaf_size, KM_SLEEP));
184dbd5678dSMartin Matuska }
185dbd5678dSMartin Matuska 
186dbd5678dSMartin Matuska static void
zfs_btree_leaf_free(zfs_btree_t * tree,void * ptr)187dbd5678dSMartin Matuska zfs_btree_leaf_free(zfs_btree_t *tree, void *ptr)
188dbd5678dSMartin Matuska {
189dbd5678dSMartin Matuska 	if (tree->bt_leaf_size == BTREE_LEAF_SIZE)
190dbd5678dSMartin Matuska 		return (kmem_cache_free(zfs_btree_leaf_cache, ptr));
191dbd5678dSMartin Matuska 	else
192dbd5678dSMartin Matuska 		return (kmem_free(ptr, tree->bt_leaf_size));
193dbd5678dSMartin Matuska }
194dbd5678dSMartin Matuska 
195eda14cbcSMatt Macy void
zfs_btree_create(zfs_btree_t * tree,int (* compar)(const void *,const void *),bt_find_in_buf_f bt_find_in_buf,size_t size)196eda14cbcSMatt Macy zfs_btree_create(zfs_btree_t *tree, int (*compar) (const void *, const void *),
1974e8d558cSMartin Matuska     bt_find_in_buf_f bt_find_in_buf, size_t size)
198eda14cbcSMatt Macy {
1994e8d558cSMartin Matuska 	zfs_btree_create_custom(tree, compar, bt_find_in_buf, size,
2004e8d558cSMartin Matuska 	    BTREE_LEAF_SIZE);
201dbd5678dSMartin Matuska }
202eda14cbcSMatt Macy 
2034e8d558cSMartin Matuska static void *
2044e8d558cSMartin Matuska zfs_btree_find_in_buf(zfs_btree_t *tree, uint8_t *buf, uint32_t nelems,
2054e8d558cSMartin Matuska     const void *value, zfs_btree_index_t *where);
2064e8d558cSMartin Matuska 
207dbd5678dSMartin Matuska void
zfs_btree_create_custom(zfs_btree_t * tree,int (* compar)(const void *,const void *),bt_find_in_buf_f bt_find_in_buf,size_t size,size_t lsize)208dbd5678dSMartin Matuska zfs_btree_create_custom(zfs_btree_t *tree,
209dbd5678dSMartin Matuska     int (*compar) (const void *, const void *),
2104e8d558cSMartin Matuska     bt_find_in_buf_f bt_find_in_buf,
211dbd5678dSMartin Matuska     size_t size, size_t lsize)
212dbd5678dSMartin Matuska {
213dbd5678dSMartin Matuska 	size_t esize = lsize - offsetof(zfs_btree_leaf_t, btl_elems);
214dbd5678dSMartin Matuska 
215dbd5678dSMartin Matuska 	ASSERT3U(size, <=, esize / 2);
216da5137abSMartin Matuska 	memset(tree, 0, sizeof (*tree));
217eda14cbcSMatt Macy 	tree->bt_compar = compar;
2184e8d558cSMartin Matuska 	tree->bt_find_in_buf = (bt_find_in_buf == NULL) ?
2194e8d558cSMartin Matuska 	    zfs_btree_find_in_buf : bt_find_in_buf;
220eda14cbcSMatt Macy 	tree->bt_elem_size = size;
221dbd5678dSMartin Matuska 	tree->bt_leaf_size = lsize;
222aca928a5SMartin Matuska 	tree->bt_leaf_cap = P2ALIGN_TYPED(esize / size, 2, size_t);
223eda14cbcSMatt Macy 	tree->bt_height = -1;
224eda14cbcSMatt Macy 	tree->bt_bulk = NULL;
225eda14cbcSMatt Macy }
226eda14cbcSMatt Macy 
227eda14cbcSMatt Macy /*
228eda14cbcSMatt Macy  * Find value in the array of elements provided. Uses a simple binary search.
229eda14cbcSMatt Macy  */
230eda14cbcSMatt Macy static void *
zfs_btree_find_in_buf(zfs_btree_t * tree,uint8_t * buf,uint32_t nelems,const void * value,zfs_btree_index_t * where)231a0b956f5SMartin Matuska zfs_btree_find_in_buf(zfs_btree_t *tree, uint8_t *buf, uint32_t nelems,
232eda14cbcSMatt Macy     const void *value, zfs_btree_index_t *where)
233eda14cbcSMatt Macy {
234a0b956f5SMartin Matuska 	uint32_t max = nelems;
235a0b956f5SMartin Matuska 	uint32_t min = 0;
236eda14cbcSMatt Macy 	while (max > min) {
237a0b956f5SMartin Matuska 		uint32_t idx = (min + max) / 2;
238eda14cbcSMatt Macy 		uint8_t *cur = buf + idx * tree->bt_elem_size;
239eda14cbcSMatt Macy 		int comp = tree->bt_compar(cur, value);
240a0b956f5SMartin Matuska 		if (comp < 0) {
241eda14cbcSMatt Macy 			min = idx + 1;
242a0b956f5SMartin Matuska 		} else if (comp > 0) {
243eda14cbcSMatt Macy 			max = idx;
244eda14cbcSMatt Macy 		} else {
245eda14cbcSMatt Macy 			where->bti_offset = idx;
246eda14cbcSMatt Macy 			where->bti_before = B_FALSE;
247eda14cbcSMatt Macy 			return (cur);
248eda14cbcSMatt Macy 		}
249eda14cbcSMatt Macy 	}
250eda14cbcSMatt Macy 
251eda14cbcSMatt Macy 	where->bti_offset = max;
252eda14cbcSMatt Macy 	where->bti_before = B_TRUE;
253eda14cbcSMatt Macy 	return (NULL);
254eda14cbcSMatt Macy }
255eda14cbcSMatt Macy 
256eda14cbcSMatt Macy /*
257eda14cbcSMatt Macy  * Find the given value in the tree. where may be passed as null to use as a
258eda14cbcSMatt Macy  * membership test or if the btree is being used as a map.
259eda14cbcSMatt Macy  */
260eda14cbcSMatt Macy void *
zfs_btree_find(zfs_btree_t * tree,const void * value,zfs_btree_index_t * where)261eda14cbcSMatt Macy zfs_btree_find(zfs_btree_t *tree, const void *value, zfs_btree_index_t *where)
262eda14cbcSMatt Macy {
263eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
264eda14cbcSMatt Macy 		if (where != NULL) {
265eda14cbcSMatt Macy 			where->bti_node = NULL;
266eda14cbcSMatt Macy 			where->bti_offset = 0;
267eda14cbcSMatt Macy 		}
268eda14cbcSMatt Macy 		ASSERT0(tree->bt_num_elems);
269eda14cbcSMatt Macy 		return (NULL);
270eda14cbcSMatt Macy 	}
271eda14cbcSMatt Macy 
272eda14cbcSMatt Macy 	/*
273eda14cbcSMatt Macy 	 * If we're in bulk-insert mode, we check the last spot in the tree
274eda14cbcSMatt Macy 	 * and the last leaf in the tree before doing the normal search,
275eda14cbcSMatt Macy 	 * because for most workloads the vast majority of finds in
276eda14cbcSMatt Macy 	 * bulk-insert mode are to insert new elements.
277eda14cbcSMatt Macy 	 */
278eda14cbcSMatt Macy 	zfs_btree_index_t idx;
279a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
280eda14cbcSMatt Macy 	if (tree->bt_bulk != NULL) {
281eda14cbcSMatt Macy 		zfs_btree_leaf_t *last_leaf = tree->bt_bulk;
282a0b956f5SMartin Matuska 		int comp = tree->bt_compar(last_leaf->btl_elems +
283a0b956f5SMartin Matuska 		    (last_leaf->btl_hdr.bth_first +
284a0b956f5SMartin Matuska 		    last_leaf->btl_hdr.bth_count - 1) * size, value);
285a0b956f5SMartin Matuska 		if (comp < 0) {
286eda14cbcSMatt Macy 			/*
287eda14cbcSMatt Macy 			 * If what they're looking for is after the last
288eda14cbcSMatt Macy 			 * element, it's not in the tree.
289eda14cbcSMatt Macy 			 */
290eda14cbcSMatt Macy 			if (where != NULL) {
291eda14cbcSMatt Macy 				where->bti_node = (zfs_btree_hdr_t *)last_leaf;
292eda14cbcSMatt Macy 				where->bti_offset =
293eda14cbcSMatt Macy 				    last_leaf->btl_hdr.bth_count;
294eda14cbcSMatt Macy 				where->bti_before = B_TRUE;
295eda14cbcSMatt Macy 			}
296eda14cbcSMatt Macy 			return (NULL);
297a0b956f5SMartin Matuska 		} else if (comp == 0) {
298eda14cbcSMatt Macy 			if (where != NULL) {
299eda14cbcSMatt Macy 				where->bti_node = (zfs_btree_hdr_t *)last_leaf;
300eda14cbcSMatt Macy 				where->bti_offset =
301eda14cbcSMatt Macy 				    last_leaf->btl_hdr.bth_count - 1;
302eda14cbcSMatt Macy 				where->bti_before = B_FALSE;
303eda14cbcSMatt Macy 			}
304eda14cbcSMatt Macy 			return (last_leaf->btl_elems +
305a0b956f5SMartin Matuska 			    (last_leaf->btl_hdr.bth_first +
306a0b956f5SMartin Matuska 			    last_leaf->btl_hdr.bth_count - 1) * size);
307eda14cbcSMatt Macy 		}
308a0b956f5SMartin Matuska 		if (tree->bt_compar(last_leaf->btl_elems +
309a0b956f5SMartin Matuska 		    last_leaf->btl_hdr.bth_first * size, value) <= 0) {
310eda14cbcSMatt Macy 			/*
311eda14cbcSMatt Macy 			 * If what they're looking for is after the first
312eda14cbcSMatt Macy 			 * element in the last leaf, it's in the last leaf or
313eda14cbcSMatt Macy 			 * it's not in the tree.
314eda14cbcSMatt Macy 			 */
3154e8d558cSMartin Matuska 			void *d = tree->bt_find_in_buf(tree,
316a0b956f5SMartin Matuska 			    last_leaf->btl_elems +
317a0b956f5SMartin Matuska 			    last_leaf->btl_hdr.bth_first * size,
318a0b956f5SMartin Matuska 			    last_leaf->btl_hdr.bth_count, value, &idx);
319eda14cbcSMatt Macy 
320eda14cbcSMatt Macy 			if (where != NULL) {
321eda14cbcSMatt Macy 				idx.bti_node = (zfs_btree_hdr_t *)last_leaf;
322eda14cbcSMatt Macy 				*where = idx;
323eda14cbcSMatt Macy 			}
324eda14cbcSMatt Macy 			return (d);
325eda14cbcSMatt Macy 		}
326eda14cbcSMatt Macy 	}
327eda14cbcSMatt Macy 
328eda14cbcSMatt Macy 	zfs_btree_core_t *node = NULL;
329a0b956f5SMartin Matuska 	uint32_t child = 0;
330dbd5678dSMartin Matuska 	uint32_t depth = 0;
331eda14cbcSMatt Macy 
332eda14cbcSMatt Macy 	/*
333eda14cbcSMatt Macy 	 * Iterate down the tree, finding which child the value should be in
334eda14cbcSMatt Macy 	 * by comparing with the separators.
335eda14cbcSMatt Macy 	 */
336eda14cbcSMatt Macy 	for (node = (zfs_btree_core_t *)tree->bt_root; depth < tree->bt_height;
337eda14cbcSMatt Macy 	    node = (zfs_btree_core_t *)node->btc_children[child], depth++) {
338eda14cbcSMatt Macy 		ASSERT3P(node, !=, NULL);
3394e8d558cSMartin Matuska 		void *d = tree->bt_find_in_buf(tree, node->btc_elems,
340eda14cbcSMatt Macy 		    node->btc_hdr.bth_count, value, &idx);
341eda14cbcSMatt Macy 		EQUIV(d != NULL, !idx.bti_before);
342eda14cbcSMatt Macy 		if (d != NULL) {
343eda14cbcSMatt Macy 			if (where != NULL) {
344eda14cbcSMatt Macy 				idx.bti_node = (zfs_btree_hdr_t *)node;
345eda14cbcSMatt Macy 				*where = idx;
346eda14cbcSMatt Macy 			}
347eda14cbcSMatt Macy 			return (d);
348eda14cbcSMatt Macy 		}
349eda14cbcSMatt Macy 		ASSERT(idx.bti_before);
350eda14cbcSMatt Macy 		child = idx.bti_offset;
351eda14cbcSMatt Macy 	}
352eda14cbcSMatt Macy 
353eda14cbcSMatt Macy 	/*
354eda14cbcSMatt Macy 	 * The value is in this leaf, or it would be if it were in the
355eda14cbcSMatt Macy 	 * tree. Find its proper location and return it.
356eda14cbcSMatt Macy 	 */
357eda14cbcSMatt Macy 	zfs_btree_leaf_t *leaf = (depth == 0 ?
358eda14cbcSMatt Macy 	    (zfs_btree_leaf_t *)tree->bt_root : (zfs_btree_leaf_t *)node);
3594e8d558cSMartin Matuska 	void *d = tree->bt_find_in_buf(tree, leaf->btl_elems +
360a0b956f5SMartin Matuska 	    leaf->btl_hdr.bth_first * size,
361eda14cbcSMatt Macy 	    leaf->btl_hdr.bth_count, value, &idx);
362eda14cbcSMatt Macy 
363eda14cbcSMatt Macy 	if (where != NULL) {
364eda14cbcSMatt Macy 		idx.bti_node = (zfs_btree_hdr_t *)leaf;
365eda14cbcSMatt Macy 		*where = idx;
366eda14cbcSMatt Macy 	}
367eda14cbcSMatt Macy 
368eda14cbcSMatt Macy 	return (d);
369eda14cbcSMatt Macy }
370eda14cbcSMatt Macy 
371eda14cbcSMatt Macy /*
372eda14cbcSMatt Macy  * To explain the following functions, it is useful to understand the four
373eda14cbcSMatt Macy  * kinds of shifts used in btree operation. First, a shift is a movement of
374eda14cbcSMatt Macy  * elements within a node. It is used to create gaps for inserting new
375eda14cbcSMatt Macy  * elements and children, or cover gaps created when things are removed. A
376eda14cbcSMatt Macy  * shift has two fundamental properties, each of which can be one of two
377eda14cbcSMatt Macy  * values, making four types of shifts.  There is the direction of the shift
378eda14cbcSMatt Macy  * (left or right) and the shape of the shift (parallelogram or isoceles
379eda14cbcSMatt Macy  * trapezoid (shortened to trapezoid hereafter)). The shape distinction only
380eda14cbcSMatt Macy  * applies to shifts of core nodes.
381eda14cbcSMatt Macy  *
382eda14cbcSMatt Macy  * The names derive from the following imagining of the layout of a node:
383eda14cbcSMatt Macy  *
384eda14cbcSMatt Macy  *  Elements:       *   *   *   *   *   *   *   ...   *   *   *
385eda14cbcSMatt Macy  *  Children:     *   *   *   *   *   *   *   *   ...   *   *   *
386eda14cbcSMatt Macy  *
387eda14cbcSMatt Macy  * This layout follows from the fact that the elements act as separators
388eda14cbcSMatt Macy  * between pairs of children, and that children root subtrees "below" the
389eda14cbcSMatt Macy  * current node. A left and right shift are fairly self-explanatory; a left
390eda14cbcSMatt Macy  * shift moves things to the left, while a right shift moves things to the
391eda14cbcSMatt Macy  * right. A parallelogram shift is a shift with the same number of elements
392eda14cbcSMatt Macy  * and children being moved, while a trapezoid shift is a shift that moves one
393eda14cbcSMatt Macy  * more children than elements. An example follows:
394eda14cbcSMatt Macy  *
395eda14cbcSMatt Macy  * A parallelogram shift could contain the following:
396eda14cbcSMatt Macy  *      _______________
397eda14cbcSMatt Macy  *      \*   *   *   * \ *   *   *   ...   *   *   *
398eda14cbcSMatt Macy  *     * \ *   *   *   *\  *   *   *   ...   *   *   *
399eda14cbcSMatt Macy  *        ---------------
400eda14cbcSMatt Macy  * A trapezoid shift could contain the following:
401eda14cbcSMatt Macy  *          ___________
402eda14cbcSMatt Macy  *       * / *   *   * \ *   *   *   ...   *   *   *
403eda14cbcSMatt Macy  *     *  / *  *   *   *\  *   *   *   ...   *   *   *
404eda14cbcSMatt Macy  *        ---------------
405eda14cbcSMatt Macy  *
406eda14cbcSMatt Macy  * Note that a parallelogram shift is always shaped like a "left-leaning"
407eda14cbcSMatt Macy  * parallelogram, where the starting index of the children being moved is
408eda14cbcSMatt Macy  * always one higher than the starting index of the elements being moved. No
409eda14cbcSMatt Macy  * "right-leaning" parallelogram shifts are needed (shifts where the starting
410eda14cbcSMatt Macy  * element index and starting child index being moved are the same) to achieve
411eda14cbcSMatt Macy  * any btree operations, so we ignore them.
412eda14cbcSMatt Macy  */
413eda14cbcSMatt Macy 
414eda14cbcSMatt Macy enum bt_shift_shape {
415eda14cbcSMatt Macy 	BSS_TRAPEZOID,
416eda14cbcSMatt Macy 	BSS_PARALLELOGRAM
417eda14cbcSMatt Macy };
418eda14cbcSMatt Macy 
419eda14cbcSMatt Macy enum bt_shift_direction {
420eda14cbcSMatt Macy 	BSD_LEFT,
421eda14cbcSMatt Macy 	BSD_RIGHT
422eda14cbcSMatt Macy };
423eda14cbcSMatt Macy 
424eda14cbcSMatt Macy /*
425eda14cbcSMatt Macy  * Shift elements and children in the provided core node by off spots.  The
426eda14cbcSMatt Macy  * first element moved is idx, and count elements are moved. The shape of the
427eda14cbcSMatt Macy  * shift is determined by shape. The direction is determined by dir.
428eda14cbcSMatt Macy  */
429eda14cbcSMatt Macy static inline void
bt_shift_core(zfs_btree_t * tree,zfs_btree_core_t * node,uint32_t idx,uint32_t count,uint32_t off,enum bt_shift_shape shape,enum bt_shift_direction dir)430a0b956f5SMartin Matuska bt_shift_core(zfs_btree_t *tree, zfs_btree_core_t *node, uint32_t idx,
431a0b956f5SMartin Matuska     uint32_t count, uint32_t off, enum bt_shift_shape shape,
432eda14cbcSMatt Macy     enum bt_shift_direction dir)
433eda14cbcSMatt Macy {
434eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
435a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(&node->btc_hdr));
436eda14cbcSMatt Macy 
437eda14cbcSMatt Macy 	uint8_t *e_start = node->btc_elems + idx * size;
438a0b956f5SMartin Matuska 	uint8_t *e_out = (dir == BSD_LEFT ? e_start - off * size :
439a0b956f5SMartin Matuska 	    e_start + off * size);
440a0b956f5SMartin Matuska 	bmov(e_start, e_out, count * size);
441eda14cbcSMatt Macy 
442eda14cbcSMatt Macy 	zfs_btree_hdr_t **c_start = node->btc_children + idx +
443eda14cbcSMatt Macy 	    (shape == BSS_TRAPEZOID ? 0 : 1);
444eda14cbcSMatt Macy 	zfs_btree_hdr_t **c_out = (dir == BSD_LEFT ? c_start - off :
445eda14cbcSMatt Macy 	    c_start + off);
446a0b956f5SMartin Matuska 	uint32_t c_count = count + (shape == BSS_TRAPEZOID ? 1 : 0);
447eda14cbcSMatt Macy 	bmov(c_start, c_out, c_count * sizeof (*c_start));
448eda14cbcSMatt Macy }
449eda14cbcSMatt Macy 
450eda14cbcSMatt Macy /*
451eda14cbcSMatt Macy  * Shift elements and children in the provided core node left by one spot.
452eda14cbcSMatt Macy  * The first element moved is idx, and count elements are moved. The
453eda14cbcSMatt Macy  * shape of the shift is determined by trap; true if the shift is a trapezoid,
454eda14cbcSMatt Macy  * false if it is a parallelogram.
455eda14cbcSMatt Macy  */
456eda14cbcSMatt Macy static inline void
bt_shift_core_left(zfs_btree_t * tree,zfs_btree_core_t * node,uint32_t idx,uint32_t count,enum bt_shift_shape shape)457a0b956f5SMartin Matuska bt_shift_core_left(zfs_btree_t *tree, zfs_btree_core_t *node, uint32_t idx,
458a0b956f5SMartin Matuska     uint32_t count, enum bt_shift_shape shape)
459eda14cbcSMatt Macy {
460eda14cbcSMatt Macy 	bt_shift_core(tree, node, idx, count, 1, shape, BSD_LEFT);
461eda14cbcSMatt Macy }
462eda14cbcSMatt Macy 
463eda14cbcSMatt Macy /*
464eda14cbcSMatt Macy  * Shift elements and children in the provided core node right by one spot.
465eda14cbcSMatt Macy  * Starts with elements[idx] and children[idx] and one more child than element.
466eda14cbcSMatt Macy  */
467eda14cbcSMatt Macy static inline void
bt_shift_core_right(zfs_btree_t * tree,zfs_btree_core_t * node,uint32_t idx,uint32_t count,enum bt_shift_shape shape)468a0b956f5SMartin Matuska bt_shift_core_right(zfs_btree_t *tree, zfs_btree_core_t *node, uint32_t idx,
469a0b956f5SMartin Matuska     uint32_t count, enum bt_shift_shape shape)
470eda14cbcSMatt Macy {
471eda14cbcSMatt Macy 	bt_shift_core(tree, node, idx, count, 1, shape, BSD_RIGHT);
472eda14cbcSMatt Macy }
473eda14cbcSMatt Macy 
474eda14cbcSMatt Macy /*
475eda14cbcSMatt Macy  * Shift elements and children in the provided leaf node by off spots.
476eda14cbcSMatt Macy  * The first element moved is idx, and count elements are moved. The direction
477eda14cbcSMatt Macy  * is determined by left.
478eda14cbcSMatt Macy  */
479eda14cbcSMatt Macy static inline void
bt_shift_leaf(zfs_btree_t * tree,zfs_btree_leaf_t * node,uint32_t idx,uint32_t count,uint32_t off,enum bt_shift_direction dir)480a0b956f5SMartin Matuska bt_shift_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *node, uint32_t idx,
481a0b956f5SMartin Matuska     uint32_t count, uint32_t off, enum bt_shift_direction dir)
482eda14cbcSMatt Macy {
483eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
484a0b956f5SMartin Matuska 	zfs_btree_hdr_t *hdr = &node->btl_hdr;
485a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(hdr));
486eda14cbcSMatt Macy 
487a0b956f5SMartin Matuska 	if (count == 0)
488a0b956f5SMartin Matuska 		return;
489a0b956f5SMartin Matuska 	uint8_t *start = node->btl_elems + (hdr->bth_first + idx) * size;
490a0b956f5SMartin Matuska 	uint8_t *out = (dir == BSD_LEFT ? start - off * size :
491a0b956f5SMartin Matuska 	    start + off * size);
492eda14cbcSMatt Macy 	bmov(start, out, count * size);
493eda14cbcSMatt Macy }
494eda14cbcSMatt Macy 
495a0b956f5SMartin Matuska /*
496a0b956f5SMartin Matuska  * Grow leaf for n new elements before idx.
497a0b956f5SMartin Matuska  */
498a0b956f5SMartin Matuska static void
bt_grow_leaf(zfs_btree_t * tree,zfs_btree_leaf_t * leaf,uint32_t idx,uint32_t n)499a0b956f5SMartin Matuska bt_grow_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *leaf, uint32_t idx,
500a0b956f5SMartin Matuska     uint32_t n)
501eda14cbcSMatt Macy {
502a0b956f5SMartin Matuska 	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
503a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(hdr));
504a0b956f5SMartin Matuska 	ASSERT3U(idx, <=, hdr->bth_count);
505a0b956f5SMartin Matuska 	uint32_t capacity = tree->bt_leaf_cap;
506a0b956f5SMartin Matuska 	ASSERT3U(hdr->bth_count + n, <=, capacity);
507a0b956f5SMartin Matuska 	boolean_t cl = (hdr->bth_first >= n);
508a0b956f5SMartin Matuska 	boolean_t cr = (hdr->bth_first + hdr->bth_count + n <= capacity);
509a0b956f5SMartin Matuska 
510a0b956f5SMartin Matuska 	if (cl && (!cr || idx <= hdr->bth_count / 2)) {
511a0b956f5SMartin Matuska 		/* Grow left. */
512a0b956f5SMartin Matuska 		hdr->bth_first -= n;
513a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, n, idx, n, BSD_LEFT);
514a0b956f5SMartin Matuska 	} else if (cr) {
515a0b956f5SMartin Matuska 		/* Grow right. */
516a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, idx, hdr->bth_count - idx, n,
517a0b956f5SMartin Matuska 		    BSD_RIGHT);
518a0b956f5SMartin Matuska 	} else {
519a0b956f5SMartin Matuska 		/* Grow both ways. */
520a0b956f5SMartin Matuska 		uint32_t fn = hdr->bth_first -
521a0b956f5SMartin Matuska 		    (capacity - (hdr->bth_count + n)) / 2;
522a0b956f5SMartin Matuska 		hdr->bth_first -= fn;
523a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, fn, idx, fn, BSD_LEFT);
524a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, fn + idx, hdr->bth_count - idx,
525a0b956f5SMartin Matuska 		    n - fn, BSD_RIGHT);
526a0b956f5SMartin Matuska 	}
527a0b956f5SMartin Matuska 	hdr->bth_count += n;
528eda14cbcSMatt Macy }
529eda14cbcSMatt Macy 
530a0b956f5SMartin Matuska /*
531a0b956f5SMartin Matuska  * Shrink leaf for count elements starting from idx.
532a0b956f5SMartin Matuska  */
533a0b956f5SMartin Matuska static void
bt_shrink_leaf(zfs_btree_t * tree,zfs_btree_leaf_t * leaf,uint32_t idx,uint32_t n)534a0b956f5SMartin Matuska bt_shrink_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *leaf, uint32_t idx,
535a0b956f5SMartin Matuska     uint32_t n)
536eda14cbcSMatt Macy {
537a0b956f5SMartin Matuska 	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
538a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(hdr));
539a0b956f5SMartin Matuska 	ASSERT3U(idx, <=, hdr->bth_count);
540a0b956f5SMartin Matuska 	ASSERT3U(idx + n, <=, hdr->bth_count);
541a0b956f5SMartin Matuska 
542a0b956f5SMartin Matuska 	if (idx <= (hdr->bth_count - n) / 2) {
543a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, 0, idx, n, BSD_RIGHT);
544a0b956f5SMartin Matuska 		zfs_btree_poison_node_at(tree, hdr, 0, n);
545a0b956f5SMartin Matuska 		hdr->bth_first += n;
546a0b956f5SMartin Matuska 	} else {
547a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, idx + n, hdr->bth_count - idx - n, n,
548a0b956f5SMartin Matuska 		    BSD_LEFT);
549a0b956f5SMartin Matuska 		zfs_btree_poison_node_at(tree, hdr, hdr->bth_count - n, n);
550a0b956f5SMartin Matuska 	}
551a0b956f5SMartin Matuska 	hdr->bth_count -= n;
552eda14cbcSMatt Macy }
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy /*
555eda14cbcSMatt Macy  * Move children and elements from one core node to another. The shape
556eda14cbcSMatt Macy  * parameter behaves the same as it does in the shift logic.
557eda14cbcSMatt Macy  */
558eda14cbcSMatt Macy static inline void
bt_transfer_core(zfs_btree_t * tree,zfs_btree_core_t * source,uint32_t sidx,uint32_t count,zfs_btree_core_t * dest,uint32_t didx,enum bt_shift_shape shape)559a0b956f5SMartin Matuska bt_transfer_core(zfs_btree_t *tree, zfs_btree_core_t *source, uint32_t sidx,
560a0b956f5SMartin Matuska     uint32_t count, zfs_btree_core_t *dest, uint32_t didx,
561eda14cbcSMatt Macy     enum bt_shift_shape shape)
562eda14cbcSMatt Macy {
563eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
564a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(&source->btc_hdr));
565a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(&dest->btc_hdr));
566eda14cbcSMatt Macy 
567a0b956f5SMartin Matuska 	bcpy(source->btc_elems + sidx * size, dest->btc_elems + didx * size,
568eda14cbcSMatt Macy 	    count * size);
569eda14cbcSMatt Macy 
570a0b956f5SMartin Matuska 	uint32_t c_count = count + (shape == BSS_TRAPEZOID ? 1 : 0);
571a0b956f5SMartin Matuska 	bcpy(source->btc_children + sidx + (shape == BSS_TRAPEZOID ? 0 : 1),
572eda14cbcSMatt Macy 	    dest->btc_children + didx + (shape == BSS_TRAPEZOID ? 0 : 1),
573eda14cbcSMatt Macy 	    c_count * sizeof (*source->btc_children));
574eda14cbcSMatt Macy }
575eda14cbcSMatt Macy 
576eda14cbcSMatt Macy static inline void
bt_transfer_leaf(zfs_btree_t * tree,zfs_btree_leaf_t * source,uint32_t sidx,uint32_t count,zfs_btree_leaf_t * dest,uint32_t didx)577a0b956f5SMartin Matuska bt_transfer_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *source, uint32_t sidx,
578a0b956f5SMartin Matuska     uint32_t count, zfs_btree_leaf_t *dest, uint32_t didx)
579eda14cbcSMatt Macy {
580eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
581a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(&source->btl_hdr));
582a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(&dest->btl_hdr));
583eda14cbcSMatt Macy 
584a0b956f5SMartin Matuska 	bcpy(source->btl_elems + (source->btl_hdr.bth_first + sidx) * size,
585a0b956f5SMartin Matuska 	    dest->btl_elems + (dest->btl_hdr.bth_first + didx) * size,
586eda14cbcSMatt Macy 	    count * size);
587eda14cbcSMatt Macy }
588eda14cbcSMatt Macy 
589eda14cbcSMatt Macy /*
590eda14cbcSMatt Macy  * Find the first element in the subtree rooted at hdr, return its value and
591eda14cbcSMatt Macy  * put its location in where if non-null.
592eda14cbcSMatt Macy  */
593eda14cbcSMatt Macy static void *
zfs_btree_first_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr,zfs_btree_index_t * where)594a0b956f5SMartin Matuska zfs_btree_first_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
595a0b956f5SMartin Matuska     zfs_btree_index_t *where)
596eda14cbcSMatt Macy {
597eda14cbcSMatt Macy 	zfs_btree_hdr_t *node;
598eda14cbcSMatt Macy 
599a0b956f5SMartin Matuska 	for (node = hdr; zfs_btree_is_core(node);
600a0b956f5SMartin Matuska 	    node = ((zfs_btree_core_t *)node)->btc_children[0])
601eda14cbcSMatt Macy 		;
602eda14cbcSMatt Macy 
603a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(node));
604eda14cbcSMatt Macy 	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)node;
605eda14cbcSMatt Macy 	if (where != NULL) {
606eda14cbcSMatt Macy 		where->bti_node = node;
607eda14cbcSMatt Macy 		where->bti_offset = 0;
608eda14cbcSMatt Macy 		where->bti_before = B_FALSE;
609eda14cbcSMatt Macy 	}
610a0b956f5SMartin Matuska 	return (&leaf->btl_elems[node->bth_first * tree->bt_elem_size]);
611eda14cbcSMatt Macy }
612eda14cbcSMatt Macy 
613eda14cbcSMatt Macy /* Insert an element and a child into a core node at the given offset. */
614eda14cbcSMatt Macy static void
zfs_btree_insert_core_impl(zfs_btree_t * tree,zfs_btree_core_t * parent,uint32_t offset,zfs_btree_hdr_t * new_node,void * buf)615eda14cbcSMatt Macy zfs_btree_insert_core_impl(zfs_btree_t *tree, zfs_btree_core_t *parent,
616a0b956f5SMartin Matuska     uint32_t offset, zfs_btree_hdr_t *new_node, void *buf)
617eda14cbcSMatt Macy {
618a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
619eda14cbcSMatt Macy 	zfs_btree_hdr_t *par_hdr = &parent->btc_hdr;
620eda14cbcSMatt Macy 	ASSERT3P(par_hdr, ==, new_node->bth_parent);
621eda14cbcSMatt Macy 	ASSERT3U(par_hdr->bth_count, <, BTREE_CORE_ELEMS);
622eda14cbcSMatt Macy 
623eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity >= 5) {
624eda14cbcSMatt Macy 		zfs_btree_verify_poison_at(tree, par_hdr,
625eda14cbcSMatt Macy 		    par_hdr->bth_count);
626eda14cbcSMatt Macy 	}
627eda14cbcSMatt Macy 	/* Shift existing elements and children */
628a0b956f5SMartin Matuska 	uint32_t count = par_hdr->bth_count - offset;
629eda14cbcSMatt Macy 	bt_shift_core_right(tree, parent, offset, count,
630eda14cbcSMatt Macy 	    BSS_PARALLELOGRAM);
631eda14cbcSMatt Macy 
632eda14cbcSMatt Macy 	/* Insert new values */
633eda14cbcSMatt Macy 	parent->btc_children[offset + 1] = new_node;
634a0b956f5SMartin Matuska 	bcpy(buf, parent->btc_elems + offset * size, size);
635eda14cbcSMatt Macy 	par_hdr->bth_count++;
636eda14cbcSMatt Macy }
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy /*
639eda14cbcSMatt Macy  * Insert new_node into the parent of old_node directly after old_node, with
640eda14cbcSMatt Macy  * buf as the dividing element between the two.
641eda14cbcSMatt Macy  */
642eda14cbcSMatt Macy static void
zfs_btree_insert_into_parent(zfs_btree_t * tree,zfs_btree_hdr_t * old_node,zfs_btree_hdr_t * new_node,void * buf)643eda14cbcSMatt Macy zfs_btree_insert_into_parent(zfs_btree_t *tree, zfs_btree_hdr_t *old_node,
644eda14cbcSMatt Macy     zfs_btree_hdr_t *new_node, void *buf)
645eda14cbcSMatt Macy {
646eda14cbcSMatt Macy 	ASSERT3P(old_node->bth_parent, ==, new_node->bth_parent);
647a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
648eda14cbcSMatt Macy 	zfs_btree_core_t *parent = old_node->bth_parent;
649eda14cbcSMatt Macy 
650eda14cbcSMatt Macy 	/*
651eda14cbcSMatt Macy 	 * If this is the root node we were splitting, we create a new root
652eda14cbcSMatt Macy 	 * and increase the height of the tree.
653eda14cbcSMatt Macy 	 */
654eda14cbcSMatt Macy 	if (parent == NULL) {
655eda14cbcSMatt Macy 		ASSERT3P(old_node, ==, tree->bt_root);
656eda14cbcSMatt Macy 		tree->bt_num_nodes++;
657eda14cbcSMatt Macy 		zfs_btree_core_t *new_root =
658eda14cbcSMatt Macy 		    kmem_alloc(sizeof (zfs_btree_core_t) + BTREE_CORE_ELEMS *
659eda14cbcSMatt Macy 		    size, KM_SLEEP);
660eda14cbcSMatt Macy 		zfs_btree_hdr_t *new_root_hdr = &new_root->btc_hdr;
661eda14cbcSMatt Macy 		new_root_hdr->bth_parent = NULL;
662a0b956f5SMartin Matuska 		new_root_hdr->bth_first = -1;
663eda14cbcSMatt Macy 		new_root_hdr->bth_count = 1;
664eda14cbcSMatt Macy 
665eda14cbcSMatt Macy 		old_node->bth_parent = new_node->bth_parent = new_root;
666eda14cbcSMatt Macy 		new_root->btc_children[0] = old_node;
667eda14cbcSMatt Macy 		new_root->btc_children[1] = new_node;
668a0b956f5SMartin Matuska 		bcpy(buf, new_root->btc_elems, size);
669eda14cbcSMatt Macy 
670eda14cbcSMatt Macy 		tree->bt_height++;
671eda14cbcSMatt Macy 		tree->bt_root = new_root_hdr;
672eda14cbcSMatt Macy 		zfs_btree_poison_node(tree, new_root_hdr);
673eda14cbcSMatt Macy 		return;
674eda14cbcSMatt Macy 	}
675eda14cbcSMatt Macy 
676eda14cbcSMatt Macy 	/*
677eda14cbcSMatt Macy 	 * Since we have the new separator, binary search for where to put
678eda14cbcSMatt Macy 	 * new_node.
679eda14cbcSMatt Macy 	 */
680c03c5b1cSMartin Matuska 	zfs_btree_hdr_t *par_hdr = &parent->btc_hdr;
681eda14cbcSMatt Macy 	zfs_btree_index_t idx;
682a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(par_hdr));
6834e8d558cSMartin Matuska 	VERIFY3P(tree->bt_find_in_buf(tree, parent->btc_elems,
684eda14cbcSMatt Macy 	    par_hdr->bth_count, buf, &idx), ==, NULL);
685eda14cbcSMatt Macy 	ASSERT(idx.bti_before);
686a0b956f5SMartin Matuska 	uint32_t offset = idx.bti_offset;
687eda14cbcSMatt Macy 	ASSERT3U(offset, <=, par_hdr->bth_count);
688eda14cbcSMatt Macy 	ASSERT3P(parent->btc_children[offset], ==, old_node);
689eda14cbcSMatt Macy 
690eda14cbcSMatt Macy 	/*
691eda14cbcSMatt Macy 	 * If the parent isn't full, shift things to accommodate our insertions
692eda14cbcSMatt Macy 	 * and return.
693eda14cbcSMatt Macy 	 */
694eda14cbcSMatt Macy 	if (par_hdr->bth_count != BTREE_CORE_ELEMS) {
695eda14cbcSMatt Macy 		zfs_btree_insert_core_impl(tree, parent, offset, new_node, buf);
696eda14cbcSMatt Macy 		return;
697eda14cbcSMatt Macy 	}
698eda14cbcSMatt Macy 
699eda14cbcSMatt Macy 	/*
700eda14cbcSMatt Macy 	 * We need to split this core node into two. Currently there are
701eda14cbcSMatt Macy 	 * BTREE_CORE_ELEMS + 1 child nodes, and we are adding one for
702eda14cbcSMatt Macy 	 * BTREE_CORE_ELEMS + 2. Some of the children will be part of the
703eda14cbcSMatt Macy 	 * current node, and the others will be moved to the new core node.
704eda14cbcSMatt Macy 	 * There are BTREE_CORE_ELEMS + 1 elements including the new one. One
705eda14cbcSMatt Macy 	 * will be used as the new separator in our parent, and the others
706eda14cbcSMatt Macy 	 * will be split among the two core nodes.
707eda14cbcSMatt Macy 	 *
708eda14cbcSMatt Macy 	 * Usually we will split the node in half evenly, with
709eda14cbcSMatt Macy 	 * BTREE_CORE_ELEMS/2 elements in each node. If we're bulk loading, we
710eda14cbcSMatt Macy 	 * instead move only about a quarter of the elements (and children) to
711eda14cbcSMatt Macy 	 * the new node. Since the average state after a long time is a 3/4
712eda14cbcSMatt Macy 	 * full node, shortcutting directly to that state improves efficiency.
713eda14cbcSMatt Macy 	 *
714eda14cbcSMatt Macy 	 * We do this in two stages: first we split into two nodes, and then we
715eda14cbcSMatt Macy 	 * reuse our existing logic to insert the new element and child.
716eda14cbcSMatt Macy 	 */
717a0b956f5SMartin Matuska 	uint32_t move_count = MAX((BTREE_CORE_ELEMS / (tree->bt_bulk == NULL ?
718eda14cbcSMatt Macy 	    2 : 4)) - 1, 2);
719a0b956f5SMartin Matuska 	uint32_t keep_count = BTREE_CORE_ELEMS - move_count - 1;
720eda14cbcSMatt Macy 	ASSERT3U(BTREE_CORE_ELEMS - move_count, >=, 2);
721eda14cbcSMatt Macy 	tree->bt_num_nodes++;
722eda14cbcSMatt Macy 	zfs_btree_core_t *new_parent = kmem_alloc(sizeof (zfs_btree_core_t) +
723eda14cbcSMatt Macy 	    BTREE_CORE_ELEMS * size, KM_SLEEP);
724eda14cbcSMatt Macy 	zfs_btree_hdr_t *new_par_hdr = &new_parent->btc_hdr;
725eda14cbcSMatt Macy 	new_par_hdr->bth_parent = par_hdr->bth_parent;
726a0b956f5SMartin Matuska 	new_par_hdr->bth_first = -1;
727eda14cbcSMatt Macy 	new_par_hdr->bth_count = move_count;
728eda14cbcSMatt Macy 	zfs_btree_poison_node(tree, new_par_hdr);
729eda14cbcSMatt Macy 
730eda14cbcSMatt Macy 	par_hdr->bth_count = keep_count;
731eda14cbcSMatt Macy 
732eda14cbcSMatt Macy 	bt_transfer_core(tree, parent, keep_count + 1, move_count, new_parent,
733eda14cbcSMatt Macy 	    0, BSS_TRAPEZOID);
734eda14cbcSMatt Macy 
735eda14cbcSMatt Macy 	/* Store the new separator in a buffer. */
736eda14cbcSMatt Macy 	uint8_t *tmp_buf = kmem_alloc(size, KM_SLEEP);
737a0b956f5SMartin Matuska 	bcpy(parent->btc_elems + keep_count * size, tmp_buf,
738eda14cbcSMatt Macy 	    size);
739eda14cbcSMatt Macy 	zfs_btree_poison_node(tree, par_hdr);
740eda14cbcSMatt Macy 
741eda14cbcSMatt Macy 	if (offset < keep_count) {
742eda14cbcSMatt Macy 		/* Insert the new node into the left half */
743eda14cbcSMatt Macy 		zfs_btree_insert_core_impl(tree, parent, offset, new_node,
744eda14cbcSMatt Macy 		    buf);
745eda14cbcSMatt Macy 
746eda14cbcSMatt Macy 		/*
747eda14cbcSMatt Macy 		 * Move the new separator to the existing buffer.
748eda14cbcSMatt Macy 		 */
749a0b956f5SMartin Matuska 		bcpy(tmp_buf, buf, size);
750eda14cbcSMatt Macy 	} else if (offset > keep_count) {
751eda14cbcSMatt Macy 		/* Insert the new node into the right half */
752eda14cbcSMatt Macy 		new_node->bth_parent = new_parent;
753eda14cbcSMatt Macy 		zfs_btree_insert_core_impl(tree, new_parent,
754eda14cbcSMatt Macy 		    offset - keep_count - 1, new_node, buf);
755eda14cbcSMatt Macy 
756eda14cbcSMatt Macy 		/*
757eda14cbcSMatt Macy 		 * Move the new separator to the existing buffer.
758eda14cbcSMatt Macy 		 */
759a0b956f5SMartin Matuska 		bcpy(tmp_buf, buf, size);
760eda14cbcSMatt Macy 	} else {
761eda14cbcSMatt Macy 		/*
762eda14cbcSMatt Macy 		 * Move the new separator into the right half, and replace it
763eda14cbcSMatt Macy 		 * with buf. We also need to shift back the elements in the
764eda14cbcSMatt Macy 		 * right half to accommodate new_node.
765eda14cbcSMatt Macy 		 */
766eda14cbcSMatt Macy 		bt_shift_core_right(tree, new_parent, 0, move_count,
767eda14cbcSMatt Macy 		    BSS_TRAPEZOID);
768eda14cbcSMatt Macy 		new_parent->btc_children[0] = new_node;
769a0b956f5SMartin Matuska 		bcpy(tmp_buf, new_parent->btc_elems, size);
770eda14cbcSMatt Macy 		new_par_hdr->bth_count++;
771eda14cbcSMatt Macy 	}
772eda14cbcSMatt Macy 	kmem_free(tmp_buf, size);
773eda14cbcSMatt Macy 	zfs_btree_poison_node(tree, par_hdr);
774eda14cbcSMatt Macy 
775a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= new_parent->btc_hdr.bth_count; i++)
776eda14cbcSMatt Macy 		new_parent->btc_children[i]->bth_parent = new_parent;
777eda14cbcSMatt Macy 
778a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= parent->btc_hdr.bth_count; i++)
779eda14cbcSMatt Macy 		ASSERT3P(parent->btc_children[i]->bth_parent, ==, parent);
780eda14cbcSMatt Macy 
781eda14cbcSMatt Macy 	/*
782eda14cbcSMatt Macy 	 * Now that the node is split, we need to insert the new node into its
783eda14cbcSMatt Macy 	 * parent. This may cause further splitting.
784eda14cbcSMatt Macy 	 */
785eda14cbcSMatt Macy 	zfs_btree_insert_into_parent(tree, &parent->btc_hdr,
786eda14cbcSMatt Macy 	    &new_parent->btc_hdr, buf);
787eda14cbcSMatt Macy }
788eda14cbcSMatt Macy 
789eda14cbcSMatt Macy /* Insert an element into a leaf node at the given offset. */
790eda14cbcSMatt Macy static void
zfs_btree_insert_leaf_impl(zfs_btree_t * tree,zfs_btree_leaf_t * leaf,uint32_t idx,const void * value)791eda14cbcSMatt Macy zfs_btree_insert_leaf_impl(zfs_btree_t *tree, zfs_btree_leaf_t *leaf,
792a0b956f5SMartin Matuska     uint32_t idx, const void *value)
793eda14cbcSMatt Macy {
794a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
795eda14cbcSMatt Macy 	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
796a0b956f5SMartin Matuska 	ASSERT3U(leaf->btl_hdr.bth_count, <, tree->bt_leaf_cap);
797eda14cbcSMatt Macy 
798eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity >= 5) {
799eda14cbcSMatt Macy 		zfs_btree_verify_poison_at(tree, &leaf->btl_hdr,
800eda14cbcSMatt Macy 		    leaf->btl_hdr.bth_count);
801eda14cbcSMatt Macy 	}
802eda14cbcSMatt Macy 
803a0b956f5SMartin Matuska 	bt_grow_leaf(tree, leaf, idx, 1);
804a0b956f5SMartin Matuska 	uint8_t *start = leaf->btl_elems + (hdr->bth_first + idx) * size;
805a0b956f5SMartin Matuska 	bcpy(value, start, size);
806eda14cbcSMatt Macy }
807eda14cbcSMatt Macy 
808a0b956f5SMartin Matuska static void
809a0b956f5SMartin Matuska zfs_btree_verify_order_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr);
810a0b956f5SMartin Matuska 
811eda14cbcSMatt Macy /* Helper function for inserting a new value into leaf at the given index. */
812eda14cbcSMatt Macy static void
zfs_btree_insert_into_leaf(zfs_btree_t * tree,zfs_btree_leaf_t * leaf,const void * value,uint32_t idx)813eda14cbcSMatt Macy zfs_btree_insert_into_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *leaf,
814a0b956f5SMartin Matuska     const void *value, uint32_t idx)
815eda14cbcSMatt Macy {
816a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
817a0b956f5SMartin Matuska 	uint32_t capacity = tree->bt_leaf_cap;
818eda14cbcSMatt Macy 
819eda14cbcSMatt Macy 	/*
820eda14cbcSMatt Macy 	 * If the leaf isn't full, shift the elements after idx and insert
821eda14cbcSMatt Macy 	 * value.
822eda14cbcSMatt Macy 	 */
823eda14cbcSMatt Macy 	if (leaf->btl_hdr.bth_count != capacity) {
824eda14cbcSMatt Macy 		zfs_btree_insert_leaf_impl(tree, leaf, idx, value);
825eda14cbcSMatt Macy 		return;
826eda14cbcSMatt Macy 	}
827eda14cbcSMatt Macy 
828eda14cbcSMatt Macy 	/*
829eda14cbcSMatt Macy 	 * Otherwise, we split the leaf node into two nodes. If we're not bulk
830eda14cbcSMatt Macy 	 * inserting, each is of size (capacity / 2).  If we are bulk
831eda14cbcSMatt Macy 	 * inserting, we move a quarter of the elements to the new node so
832eda14cbcSMatt Macy 	 * inserts into the old node don't cause immediate splitting but the
833eda14cbcSMatt Macy 	 * tree stays relatively dense. Since the average state after a long
834eda14cbcSMatt Macy 	 * time is a 3/4 full node, shortcutting directly to that state
835eda14cbcSMatt Macy 	 * improves efficiency.  At the end of the bulk insertion process
836eda14cbcSMatt Macy 	 * we'll need to go through and fix up any nodes (the last leaf and
837eda14cbcSMatt Macy 	 * its ancestors, potentially) that are below the minimum.
838eda14cbcSMatt Macy 	 *
839eda14cbcSMatt Macy 	 * In either case, we're left with one extra element. The leftover
840eda14cbcSMatt Macy 	 * element will become the new dividing element between the two nodes.
841eda14cbcSMatt Macy 	 */
842a0b956f5SMartin Matuska 	uint32_t move_count = MAX(capacity / (tree->bt_bulk ? 4 : 2), 1) - 1;
843a0b956f5SMartin Matuska 	uint32_t keep_count = capacity - move_count - 1;
844a0b956f5SMartin Matuska 	ASSERT3U(keep_count, >=, 1);
845a0b956f5SMartin Matuska 	/* If we insert on left. move one more to keep leaves balanced.  */
846a0b956f5SMartin Matuska 	if (idx < keep_count) {
847a0b956f5SMartin Matuska 		keep_count--;
848a0b956f5SMartin Matuska 		move_count++;
849a0b956f5SMartin Matuska 	}
850eda14cbcSMatt Macy 	tree->bt_num_nodes++;
851dbd5678dSMartin Matuska 	zfs_btree_leaf_t *new_leaf = zfs_btree_leaf_alloc(tree);
852eda14cbcSMatt Macy 	zfs_btree_hdr_t *new_hdr = &new_leaf->btl_hdr;
853eda14cbcSMatt Macy 	new_hdr->bth_parent = leaf->btl_hdr.bth_parent;
854a0b956f5SMartin Matuska 	new_hdr->bth_first = (tree->bt_bulk ? 0 : capacity / 4) +
855a0b956f5SMartin Matuska 	    (idx >= keep_count && idx <= keep_count + move_count / 2);
856eda14cbcSMatt Macy 	new_hdr->bth_count = move_count;
857eda14cbcSMatt Macy 	zfs_btree_poison_node(tree, new_hdr);
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy 	if (tree->bt_bulk != NULL && leaf == tree->bt_bulk)
860eda14cbcSMatt Macy 		tree->bt_bulk = new_leaf;
861eda14cbcSMatt Macy 
862eda14cbcSMatt Macy 	/* Copy the back part to the new leaf. */
863a0b956f5SMartin Matuska 	bt_transfer_leaf(tree, leaf, keep_count + 1, move_count, new_leaf, 0);
864eda14cbcSMatt Macy 
865eda14cbcSMatt Macy 	/* We store the new separator in a buffer we control for simplicity. */
866eda14cbcSMatt Macy 	uint8_t *buf = kmem_alloc(size, KM_SLEEP);
867a0b956f5SMartin Matuska 	bcpy(leaf->btl_elems + (leaf->btl_hdr.bth_first + keep_count) * size,
868a0b956f5SMartin Matuska 	    buf, size);
869a0b956f5SMartin Matuska 
870a0b956f5SMartin Matuska 	bt_shrink_leaf(tree, leaf, keep_count, 1 + move_count);
871eda14cbcSMatt Macy 
872eda14cbcSMatt Macy 	if (idx < keep_count) {
873eda14cbcSMatt Macy 		/* Insert into the existing leaf. */
874eda14cbcSMatt Macy 		zfs_btree_insert_leaf_impl(tree, leaf, idx, value);
875eda14cbcSMatt Macy 	} else if (idx > keep_count) {
876eda14cbcSMatt Macy 		/* Insert into the new leaf. */
877eda14cbcSMatt Macy 		zfs_btree_insert_leaf_impl(tree, new_leaf, idx - keep_count -
878eda14cbcSMatt Macy 		    1, value);
879eda14cbcSMatt Macy 	} else {
880eda14cbcSMatt Macy 		/*
881a0b956f5SMartin Matuska 		 * Insert planned separator into the new leaf, and use
882a0b956f5SMartin Matuska 		 * the new value as the new separator.
883eda14cbcSMatt Macy 		 */
884a0b956f5SMartin Matuska 		zfs_btree_insert_leaf_impl(tree, new_leaf, 0, buf);
885a0b956f5SMartin Matuska 		bcpy(value, buf, size);
886eda14cbcSMatt Macy 	}
887eda14cbcSMatt Macy 
888eda14cbcSMatt Macy 	/*
889eda14cbcSMatt Macy 	 * Now that the node is split, we need to insert the new node into its
890eda14cbcSMatt Macy 	 * parent. This may cause further splitting, bur only of core nodes.
891eda14cbcSMatt Macy 	 */
892eda14cbcSMatt Macy 	zfs_btree_insert_into_parent(tree, &leaf->btl_hdr, &new_leaf->btl_hdr,
893eda14cbcSMatt Macy 	    buf);
894eda14cbcSMatt Macy 	kmem_free(buf, size);
895eda14cbcSMatt Macy }
896eda14cbcSMatt Macy 
897a0b956f5SMartin Matuska static uint32_t
zfs_btree_find_parent_idx(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)898eda14cbcSMatt Macy zfs_btree_find_parent_idx(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
899eda14cbcSMatt Macy {
900eda14cbcSMatt Macy 	void *buf;
901a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
902eda14cbcSMatt Macy 		buf = ((zfs_btree_core_t *)hdr)->btc_elems;
903eda14cbcSMatt Macy 	} else {
904a0b956f5SMartin Matuska 		buf = ((zfs_btree_leaf_t *)hdr)->btl_elems +
905a0b956f5SMartin Matuska 		    hdr->bth_first * tree->bt_elem_size;
906eda14cbcSMatt Macy 	}
907eda14cbcSMatt Macy 	zfs_btree_index_t idx;
908eda14cbcSMatt Macy 	zfs_btree_core_t *parent = hdr->bth_parent;
9094e8d558cSMartin Matuska 	VERIFY3P(tree->bt_find_in_buf(tree, parent->btc_elems,
910eda14cbcSMatt Macy 	    parent->btc_hdr.bth_count, buf, &idx), ==, NULL);
911eda14cbcSMatt Macy 	ASSERT(idx.bti_before);
912eda14cbcSMatt Macy 	ASSERT3U(idx.bti_offset, <=, parent->btc_hdr.bth_count);
913eda14cbcSMatt Macy 	ASSERT3P(parent->btc_children[idx.bti_offset], ==, hdr);
914eda14cbcSMatt Macy 	return (idx.bti_offset);
915eda14cbcSMatt Macy }
916eda14cbcSMatt Macy 
917eda14cbcSMatt Macy /*
918eda14cbcSMatt Macy  * Take the b-tree out of bulk insert mode. During bulk-insert mode, some
919eda14cbcSMatt Macy  * nodes may violate the invariant that non-root nodes must be at least half
920eda14cbcSMatt Macy  * full. All nodes violating this invariant should be the last node in their
921eda14cbcSMatt Macy  * particular level. To correct the invariant, we take values from their left
922eda14cbcSMatt Macy  * neighbor until they are half full. They must have a left neighbor at their
923eda14cbcSMatt Macy  * level because the last node at a level is not the first node unless it's
924eda14cbcSMatt Macy  * the root.
925eda14cbcSMatt Macy  */
926eda14cbcSMatt Macy static void
zfs_btree_bulk_finish(zfs_btree_t * tree)927eda14cbcSMatt Macy zfs_btree_bulk_finish(zfs_btree_t *tree)
928eda14cbcSMatt Macy {
929eda14cbcSMatt Macy 	ASSERT3P(tree->bt_bulk, !=, NULL);
930eda14cbcSMatt Macy 	ASSERT3P(tree->bt_root, !=, NULL);
931eda14cbcSMatt Macy 	zfs_btree_leaf_t *leaf = tree->bt_bulk;
932eda14cbcSMatt Macy 	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
933eda14cbcSMatt Macy 	zfs_btree_core_t *parent = hdr->bth_parent;
934a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
935a0b956f5SMartin Matuska 	uint32_t capacity = tree->bt_leaf_cap;
936eda14cbcSMatt Macy 
937eda14cbcSMatt Macy 	/*
938eda14cbcSMatt Macy 	 * The invariant doesn't apply to the root node, if that's the only
939eda14cbcSMatt Macy 	 * node in the tree we're done.
940eda14cbcSMatt Macy 	 */
941eda14cbcSMatt Macy 	if (parent == NULL) {
942eda14cbcSMatt Macy 		tree->bt_bulk = NULL;
943eda14cbcSMatt Macy 		return;
944eda14cbcSMatt Macy 	}
945eda14cbcSMatt Macy 
946eda14cbcSMatt Macy 	/* First, take elements to rebalance the leaf node. */
947eda14cbcSMatt Macy 	if (hdr->bth_count < capacity / 2) {
948eda14cbcSMatt Macy 		/*
949eda14cbcSMatt Macy 		 * First, find the left neighbor. The simplest way to do this
950eda14cbcSMatt Macy 		 * is to call zfs_btree_prev twice; the first time finds some
951eda14cbcSMatt Macy 		 * ancestor of this node, and the second time finds the left
952eda14cbcSMatt Macy 		 * neighbor. The ancestor found is the lowest common ancestor
953eda14cbcSMatt Macy 		 * of leaf and the neighbor.
954eda14cbcSMatt Macy 		 */
955eda14cbcSMatt Macy 		zfs_btree_index_t idx = {
956eda14cbcSMatt Macy 			.bti_node = hdr,
957eda14cbcSMatt Macy 			.bti_offset = 0
958eda14cbcSMatt Macy 		};
959eda14cbcSMatt Macy 		VERIFY3P(zfs_btree_prev(tree, &idx, &idx), !=, NULL);
960a0b956f5SMartin Matuska 		ASSERT(zfs_btree_is_core(idx.bti_node));
961eda14cbcSMatt Macy 		zfs_btree_core_t *common = (zfs_btree_core_t *)idx.bti_node;
962a0b956f5SMartin Matuska 		uint32_t common_idx = idx.bti_offset;
963eda14cbcSMatt Macy 
964eda14cbcSMatt Macy 		VERIFY3P(zfs_btree_prev(tree, &idx, &idx), !=, NULL);
965a0b956f5SMartin Matuska 		ASSERT(!zfs_btree_is_core(idx.bti_node));
966eda14cbcSMatt Macy 		zfs_btree_leaf_t *l_neighbor = (zfs_btree_leaf_t *)idx.bti_node;
967eda14cbcSMatt Macy 		zfs_btree_hdr_t *l_hdr = idx.bti_node;
968a0b956f5SMartin Matuska 		uint32_t move_count = (capacity / 2) - hdr->bth_count;
969eda14cbcSMatt Macy 		ASSERT3U(l_neighbor->btl_hdr.bth_count - move_count, >=,
970eda14cbcSMatt Macy 		    capacity / 2);
971eda14cbcSMatt Macy 
972eda14cbcSMatt Macy 		if (zfs_btree_verify_intensity >= 5) {
973a0b956f5SMartin Matuska 			for (uint32_t i = 0; i < move_count; i++) {
974eda14cbcSMatt Macy 				zfs_btree_verify_poison_at(tree, hdr,
975eda14cbcSMatt Macy 				    leaf->btl_hdr.bth_count + i);
976eda14cbcSMatt Macy 			}
977eda14cbcSMatt Macy 		}
978eda14cbcSMatt Macy 
979eda14cbcSMatt Macy 		/* First, shift elements in leaf back. */
980a0b956f5SMartin Matuska 		bt_grow_leaf(tree, leaf, 0, move_count);
981eda14cbcSMatt Macy 
982eda14cbcSMatt Macy 		/* Next, move the separator from the common ancestor to leaf. */
983a0b956f5SMartin Matuska 		uint8_t *separator = common->btc_elems + common_idx * size;
984a0b956f5SMartin Matuska 		uint8_t *out = leaf->btl_elems +
985a0b956f5SMartin Matuska 		    (hdr->bth_first + move_count - 1) * size;
986a0b956f5SMartin Matuska 		bcpy(separator, out, size);
987eda14cbcSMatt Macy 
988eda14cbcSMatt Macy 		/*
989eda14cbcSMatt Macy 		 * Now we move elements from the tail of the left neighbor to
990eda14cbcSMatt Macy 		 * fill the remaining spots in leaf.
991eda14cbcSMatt Macy 		 */
992eda14cbcSMatt Macy 		bt_transfer_leaf(tree, l_neighbor, l_hdr->bth_count -
993a0b956f5SMartin Matuska 		    (move_count - 1), move_count - 1, leaf, 0);
994eda14cbcSMatt Macy 
995eda14cbcSMatt Macy 		/*
996eda14cbcSMatt Macy 		 * Finally, move the new last element in the left neighbor to
997eda14cbcSMatt Macy 		 * the separator.
998eda14cbcSMatt Macy 		 */
999a0b956f5SMartin Matuska 		bcpy(l_neighbor->btl_elems + (l_hdr->bth_first +
1000a0b956f5SMartin Matuska 		    l_hdr->bth_count - move_count) * size, separator, size);
1001eda14cbcSMatt Macy 
1002eda14cbcSMatt Macy 		/* Adjust the node's counts, and we're done. */
1003a0b956f5SMartin Matuska 		bt_shrink_leaf(tree, l_neighbor, l_hdr->bth_count - move_count,
1004a0b956f5SMartin Matuska 		    move_count);
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 		ASSERT3U(l_hdr->bth_count, >=, capacity / 2);
1007eda14cbcSMatt Macy 		ASSERT3U(hdr->bth_count, >=, capacity / 2);
1008eda14cbcSMatt Macy 	}
1009eda14cbcSMatt Macy 
1010eda14cbcSMatt Macy 	/*
1011eda14cbcSMatt Macy 	 * Now we have to rebalance any ancestors of leaf that may also
1012eda14cbcSMatt Macy 	 * violate the invariant.
1013eda14cbcSMatt Macy 	 */
1014eda14cbcSMatt Macy 	capacity = BTREE_CORE_ELEMS;
1015eda14cbcSMatt Macy 	while (parent->btc_hdr.bth_parent != NULL) {
1016eda14cbcSMatt Macy 		zfs_btree_core_t *cur = parent;
1017eda14cbcSMatt Macy 		zfs_btree_hdr_t *hdr = &cur->btc_hdr;
1018eda14cbcSMatt Macy 		parent = hdr->bth_parent;
1019eda14cbcSMatt Macy 		/*
1020eda14cbcSMatt Macy 		 * If the invariant isn't violated, move on to the next
1021eda14cbcSMatt Macy 		 * ancestor.
1022eda14cbcSMatt Macy 		 */
1023eda14cbcSMatt Macy 		if (hdr->bth_count >= capacity / 2)
1024eda14cbcSMatt Macy 			continue;
1025eda14cbcSMatt Macy 
1026eda14cbcSMatt Macy 		/*
1027eda14cbcSMatt Macy 		 * Because the smallest number of nodes we can move when
1028eda14cbcSMatt Macy 		 * splitting is 2, we never need to worry about not having a
1029eda14cbcSMatt Macy 		 * left sibling (a sibling is a neighbor with the same parent).
1030eda14cbcSMatt Macy 		 */
1031a0b956f5SMartin Matuska 		uint32_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);
1032eda14cbcSMatt Macy 		ASSERT3U(parent_idx, >, 0);
1033eda14cbcSMatt Macy 		zfs_btree_core_t *l_neighbor =
1034eda14cbcSMatt Macy 		    (zfs_btree_core_t *)parent->btc_children[parent_idx - 1];
1035a0b956f5SMartin Matuska 		uint32_t move_count = (capacity / 2) - hdr->bth_count;
1036eda14cbcSMatt Macy 		ASSERT3U(l_neighbor->btc_hdr.bth_count - move_count, >=,
1037eda14cbcSMatt Macy 		    capacity / 2);
1038eda14cbcSMatt Macy 
1039eda14cbcSMatt Macy 		if (zfs_btree_verify_intensity >= 5) {
1040a0b956f5SMartin Matuska 			for (uint32_t i = 0; i < move_count; i++) {
1041eda14cbcSMatt Macy 				zfs_btree_verify_poison_at(tree, hdr,
1042eda14cbcSMatt Macy 				    hdr->bth_count + i);
1043eda14cbcSMatt Macy 			}
1044eda14cbcSMatt Macy 		}
1045eda14cbcSMatt Macy 		/* First, shift things in the right node back. */
1046eda14cbcSMatt Macy 		bt_shift_core(tree, cur, 0, hdr->bth_count, move_count,
1047eda14cbcSMatt Macy 		    BSS_TRAPEZOID, BSD_RIGHT);
1048eda14cbcSMatt Macy 
1049eda14cbcSMatt Macy 		/* Next, move the separator to the right node. */
1050eda14cbcSMatt Macy 		uint8_t *separator = parent->btc_elems + ((parent_idx - 1) *
1051eda14cbcSMatt Macy 		    size);
1052eda14cbcSMatt Macy 		uint8_t *e_out = cur->btc_elems + ((move_count - 1) * size);
1053a0b956f5SMartin Matuska 		bcpy(separator, e_out, size);
1054eda14cbcSMatt Macy 
1055eda14cbcSMatt Macy 		/*
1056eda14cbcSMatt Macy 		 * Now, move elements and children from the left node to the
1057eda14cbcSMatt Macy 		 * right.  We move one more child than elements.
1058eda14cbcSMatt Macy 		 */
1059eda14cbcSMatt Macy 		move_count--;
1060a0b956f5SMartin Matuska 		uint32_t move_idx = l_neighbor->btc_hdr.bth_count - move_count;
1061eda14cbcSMatt Macy 		bt_transfer_core(tree, l_neighbor, move_idx, move_count, cur, 0,
1062eda14cbcSMatt Macy 		    BSS_TRAPEZOID);
1063eda14cbcSMatt Macy 
1064eda14cbcSMatt Macy 		/*
1065eda14cbcSMatt Macy 		 * Finally, move the last element in the left node to the
1066eda14cbcSMatt Macy 		 * separator's position.
1067eda14cbcSMatt Macy 		 */
1068eda14cbcSMatt Macy 		move_idx--;
1069a0b956f5SMartin Matuska 		bcpy(l_neighbor->btc_elems + move_idx * size, separator, size);
1070eda14cbcSMatt Macy 
1071eda14cbcSMatt Macy 		l_neighbor->btc_hdr.bth_count -= move_count + 1;
1072eda14cbcSMatt Macy 		hdr->bth_count += move_count + 1;
1073eda14cbcSMatt Macy 
1074eda14cbcSMatt Macy 		ASSERT3U(l_neighbor->btc_hdr.bth_count, >=, capacity / 2);
1075eda14cbcSMatt Macy 		ASSERT3U(hdr->bth_count, >=, capacity / 2);
1076eda14cbcSMatt Macy 
1077eda14cbcSMatt Macy 		zfs_btree_poison_node(tree, &l_neighbor->btc_hdr);
1078eda14cbcSMatt Macy 
1079a0b956f5SMartin Matuska 		for (uint32_t i = 0; i <= hdr->bth_count; i++)
1080eda14cbcSMatt Macy 			cur->btc_children[i]->bth_parent = cur;
1081eda14cbcSMatt Macy 	}
1082eda14cbcSMatt Macy 
1083eda14cbcSMatt Macy 	tree->bt_bulk = NULL;
1084a0b956f5SMartin Matuska 	zfs_btree_verify(tree);
1085eda14cbcSMatt Macy }
1086eda14cbcSMatt Macy 
1087eda14cbcSMatt Macy /*
1088eda14cbcSMatt Macy  * Insert value into tree at the location specified by where.
1089eda14cbcSMatt Macy  */
1090eda14cbcSMatt Macy void
zfs_btree_add_idx(zfs_btree_t * tree,const void * value,const zfs_btree_index_t * where)1091eda14cbcSMatt Macy zfs_btree_add_idx(zfs_btree_t *tree, const void *value,
1092eda14cbcSMatt Macy     const zfs_btree_index_t *where)
1093eda14cbcSMatt Macy {
1094eda14cbcSMatt Macy 	zfs_btree_index_t idx = {0};
1095eda14cbcSMatt Macy 
1096eda14cbcSMatt Macy 	/* If we're not inserting in the last leaf, end bulk insert mode. */
1097eda14cbcSMatt Macy 	if (tree->bt_bulk != NULL) {
1098eda14cbcSMatt Macy 		if (where->bti_node != &tree->bt_bulk->btl_hdr) {
1099eda14cbcSMatt Macy 			zfs_btree_bulk_finish(tree);
1100eda14cbcSMatt Macy 			VERIFY3P(zfs_btree_find(tree, value, &idx), ==, NULL);
1101eda14cbcSMatt Macy 			where = &idx;
1102eda14cbcSMatt Macy 		}
1103eda14cbcSMatt Macy 	}
1104eda14cbcSMatt Macy 
1105eda14cbcSMatt Macy 	tree->bt_num_elems++;
1106eda14cbcSMatt Macy 	/*
1107eda14cbcSMatt Macy 	 * If this is the first element in the tree, create a leaf root node
1108eda14cbcSMatt Macy 	 * and add the value to it.
1109eda14cbcSMatt Macy 	 */
1110eda14cbcSMatt Macy 	if (where->bti_node == NULL) {
1111eda14cbcSMatt Macy 		ASSERT3U(tree->bt_num_elems, ==, 1);
1112eda14cbcSMatt Macy 		ASSERT3S(tree->bt_height, ==, -1);
1113eda14cbcSMatt Macy 		ASSERT3P(tree->bt_root, ==, NULL);
1114eda14cbcSMatt Macy 		ASSERT0(where->bti_offset);
1115eda14cbcSMatt Macy 
1116eda14cbcSMatt Macy 		tree->bt_num_nodes++;
1117dbd5678dSMartin Matuska 		zfs_btree_leaf_t *leaf = zfs_btree_leaf_alloc(tree);
1118eda14cbcSMatt Macy 		tree->bt_root = &leaf->btl_hdr;
1119eda14cbcSMatt Macy 		tree->bt_height++;
1120eda14cbcSMatt Macy 
1121eda14cbcSMatt Macy 		zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
1122eda14cbcSMatt Macy 		hdr->bth_parent = NULL;
1123a0b956f5SMartin Matuska 		hdr->bth_first = 0;
1124eda14cbcSMatt Macy 		hdr->bth_count = 0;
1125eda14cbcSMatt Macy 		zfs_btree_poison_node(tree, hdr);
1126eda14cbcSMatt Macy 
1127eda14cbcSMatt Macy 		zfs_btree_insert_into_leaf(tree, leaf, value, 0);
1128eda14cbcSMatt Macy 		tree->bt_bulk = leaf;
1129a0b956f5SMartin Matuska 	} else if (!zfs_btree_is_core(where->bti_node)) {
1130eda14cbcSMatt Macy 		/*
1131eda14cbcSMatt Macy 		 * If we're inserting into a leaf, go directly to the helper
1132eda14cbcSMatt Macy 		 * function.
1133eda14cbcSMatt Macy 		 */
1134eda14cbcSMatt Macy 		zfs_btree_insert_into_leaf(tree,
1135eda14cbcSMatt Macy 		    (zfs_btree_leaf_t *)where->bti_node, value,
1136eda14cbcSMatt Macy 		    where->bti_offset);
1137eda14cbcSMatt Macy 	} else {
1138eda14cbcSMatt Macy 		/*
1139eda14cbcSMatt Macy 		 * If we're inserting into a core node, we can't just shift
1140eda14cbcSMatt Macy 		 * the existing element in that slot in the same node without
1141eda14cbcSMatt Macy 		 * breaking our ordering invariants. Instead we place the new
1142eda14cbcSMatt Macy 		 * value in the node at that spot and then insert the old
1143eda14cbcSMatt Macy 		 * separator into the first slot in the subtree to the right.
1144eda14cbcSMatt Macy 		 */
1145eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)where->bti_node;
1146eda14cbcSMatt Macy 
1147eda14cbcSMatt Macy 		/*
1148eda14cbcSMatt Macy 		 * We can ignore bti_before, because either way the value
1149eda14cbcSMatt Macy 		 * should end up in bti_offset.
1150eda14cbcSMatt Macy 		 */
1151a0b956f5SMartin Matuska 		uint32_t off = where->bti_offset;
1152eda14cbcSMatt Macy 		zfs_btree_hdr_t *subtree = node->btc_children[off + 1];
1153eda14cbcSMatt Macy 		size_t size = tree->bt_elem_size;
1154eda14cbcSMatt Macy 		uint8_t *buf = kmem_alloc(size, KM_SLEEP);
1155a0b956f5SMartin Matuska 		bcpy(node->btc_elems + off * size, buf, size);
1156a0b956f5SMartin Matuska 		bcpy(value, node->btc_elems + off * size, size);
1157eda14cbcSMatt Macy 
1158eda14cbcSMatt Macy 		/*
1159eda14cbcSMatt Macy 		 * Find the first slot in the subtree to the right, insert
1160eda14cbcSMatt Macy 		 * there.
1161eda14cbcSMatt Macy 		 */
1162eda14cbcSMatt Macy 		zfs_btree_index_t new_idx;
1163a0b956f5SMartin Matuska 		VERIFY3P(zfs_btree_first_helper(tree, subtree, &new_idx), !=,
1164a0b956f5SMartin Matuska 		    NULL);
1165eda14cbcSMatt Macy 		ASSERT0(new_idx.bti_offset);
1166a0b956f5SMartin Matuska 		ASSERT(!zfs_btree_is_core(new_idx.bti_node));
1167eda14cbcSMatt Macy 		zfs_btree_insert_into_leaf(tree,
1168eda14cbcSMatt Macy 		    (zfs_btree_leaf_t *)new_idx.bti_node, buf, 0);
1169eda14cbcSMatt Macy 		kmem_free(buf, size);
1170eda14cbcSMatt Macy 	}
1171eda14cbcSMatt Macy 	zfs_btree_verify(tree);
1172eda14cbcSMatt Macy }
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy /*
1175eda14cbcSMatt Macy  * Return the first element in the tree, and put its location in where if
1176eda14cbcSMatt Macy  * non-null.
1177eda14cbcSMatt Macy  */
1178eda14cbcSMatt Macy void *
zfs_btree_first(zfs_btree_t * tree,zfs_btree_index_t * where)1179eda14cbcSMatt Macy zfs_btree_first(zfs_btree_t *tree, zfs_btree_index_t *where)
1180eda14cbcSMatt Macy {
1181eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
1182eda14cbcSMatt Macy 		ASSERT0(tree->bt_num_elems);
1183eda14cbcSMatt Macy 		return (NULL);
1184eda14cbcSMatt Macy 	}
1185a0b956f5SMartin Matuska 	return (zfs_btree_first_helper(tree, tree->bt_root, where));
1186eda14cbcSMatt Macy }
1187eda14cbcSMatt Macy 
1188eda14cbcSMatt Macy /*
1189eda14cbcSMatt Macy  * Find the last element in the subtree rooted at hdr, return its value and
1190eda14cbcSMatt Macy  * put its location in where if non-null.
1191eda14cbcSMatt Macy  */
1192eda14cbcSMatt Macy static void *
zfs_btree_last_helper(zfs_btree_t * btree,zfs_btree_hdr_t * hdr,zfs_btree_index_t * where)1193eda14cbcSMatt Macy zfs_btree_last_helper(zfs_btree_t *btree, zfs_btree_hdr_t *hdr,
1194eda14cbcSMatt Macy     zfs_btree_index_t *where)
1195eda14cbcSMatt Macy {
1196eda14cbcSMatt Macy 	zfs_btree_hdr_t *node;
1197eda14cbcSMatt Macy 
1198a0b956f5SMartin Matuska 	for (node = hdr; zfs_btree_is_core(node); node =
1199eda14cbcSMatt Macy 	    ((zfs_btree_core_t *)node)->btc_children[node->bth_count])
1200eda14cbcSMatt Macy 		;
1201eda14cbcSMatt Macy 
1202eda14cbcSMatt Macy 	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)node;
1203eda14cbcSMatt Macy 	if (where != NULL) {
1204eda14cbcSMatt Macy 		where->bti_node = node;
1205eda14cbcSMatt Macy 		where->bti_offset = node->bth_count - 1;
1206eda14cbcSMatt Macy 		where->bti_before = B_FALSE;
1207eda14cbcSMatt Macy 	}
1208a0b956f5SMartin Matuska 	return (leaf->btl_elems + (node->bth_first + node->bth_count - 1) *
1209a0b956f5SMartin Matuska 	    btree->bt_elem_size);
1210eda14cbcSMatt Macy }
1211eda14cbcSMatt Macy 
1212eda14cbcSMatt Macy /*
1213eda14cbcSMatt Macy  * Return the last element in the tree, and put its location in where if
1214eda14cbcSMatt Macy  * non-null.
1215eda14cbcSMatt Macy  */
1216eda14cbcSMatt Macy void *
zfs_btree_last(zfs_btree_t * tree,zfs_btree_index_t * where)1217eda14cbcSMatt Macy zfs_btree_last(zfs_btree_t *tree, zfs_btree_index_t *where)
1218eda14cbcSMatt Macy {
1219eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
1220eda14cbcSMatt Macy 		ASSERT0(tree->bt_num_elems);
1221eda14cbcSMatt Macy 		return (NULL);
1222eda14cbcSMatt Macy 	}
1223eda14cbcSMatt Macy 	return (zfs_btree_last_helper(tree, tree->bt_root, where));
1224eda14cbcSMatt Macy }
1225eda14cbcSMatt Macy 
1226eda14cbcSMatt Macy /*
1227eda14cbcSMatt Macy  * This function contains the logic to find the next node in the tree. A
1228eda14cbcSMatt Macy  * helper function is used because there are multiple internal consumemrs of
1229eda14cbcSMatt Macy  * this logic. The done_func is used by zfs_btree_destroy_nodes to clean up each
1230eda14cbcSMatt Macy  * node after we've finished with it.
1231eda14cbcSMatt Macy  */
1232eda14cbcSMatt Macy static void *
zfs_btree_next_helper(zfs_btree_t * tree,const zfs_btree_index_t * idx,zfs_btree_index_t * out_idx,void (* done_func)(zfs_btree_t *,zfs_btree_hdr_t *))1233eda14cbcSMatt Macy zfs_btree_next_helper(zfs_btree_t *tree, const zfs_btree_index_t *idx,
1234eda14cbcSMatt Macy     zfs_btree_index_t *out_idx,
1235eda14cbcSMatt Macy     void (*done_func)(zfs_btree_t *, zfs_btree_hdr_t *))
1236eda14cbcSMatt Macy {
1237eda14cbcSMatt Macy 	if (idx->bti_node == NULL) {
1238eda14cbcSMatt Macy 		ASSERT3S(tree->bt_height, ==, -1);
1239eda14cbcSMatt Macy 		return (NULL);
1240eda14cbcSMatt Macy 	}
1241eda14cbcSMatt Macy 
1242a0b956f5SMartin Matuska 	uint32_t offset = idx->bti_offset;
1243a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(idx->bti_node)) {
1244eda14cbcSMatt Macy 		/*
1245eda14cbcSMatt Macy 		 * When finding the next element of an element in a leaf,
1246eda14cbcSMatt Macy 		 * there are two cases. If the element isn't the last one in
1247eda14cbcSMatt Macy 		 * the leaf, in which case we just return the next element in
1248eda14cbcSMatt Macy 		 * the leaf. Otherwise, we need to traverse up our parents
1249eda14cbcSMatt Macy 		 * until we find one where our ancestor isn't the last child
1250eda14cbcSMatt Macy 		 * of its parent. Once we do, the next element is the
1251eda14cbcSMatt Macy 		 * separator after our ancestor in its parent.
1252eda14cbcSMatt Macy 		 */
1253eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
1254a0b956f5SMartin Matuska 		uint32_t new_off = offset + (idx->bti_before ? 0 : 1);
1255eda14cbcSMatt Macy 		if (leaf->btl_hdr.bth_count > new_off) {
1256eda14cbcSMatt Macy 			out_idx->bti_node = &leaf->btl_hdr;
1257eda14cbcSMatt Macy 			out_idx->bti_offset = new_off;
1258eda14cbcSMatt Macy 			out_idx->bti_before = B_FALSE;
1259a0b956f5SMartin Matuska 			return (leaf->btl_elems + (leaf->btl_hdr.bth_first +
1260a0b956f5SMartin Matuska 			    new_off) * tree->bt_elem_size);
1261eda14cbcSMatt Macy 		}
1262eda14cbcSMatt Macy 
1263eda14cbcSMatt Macy 		zfs_btree_hdr_t *prev = &leaf->btl_hdr;
1264eda14cbcSMatt Macy 		for (zfs_btree_core_t *node = leaf->btl_hdr.bth_parent;
1265eda14cbcSMatt Macy 		    node != NULL; node = node->btc_hdr.bth_parent) {
1266eda14cbcSMatt Macy 			zfs_btree_hdr_t *hdr = &node->btc_hdr;
1267a0b956f5SMartin Matuska 			ASSERT(zfs_btree_is_core(hdr));
1268a0b956f5SMartin Matuska 			uint32_t i = zfs_btree_find_parent_idx(tree, prev);
1269eda14cbcSMatt Macy 			if (done_func != NULL)
1270eda14cbcSMatt Macy 				done_func(tree, prev);
1271eda14cbcSMatt Macy 			if (i == hdr->bth_count) {
1272eda14cbcSMatt Macy 				prev = hdr;
1273eda14cbcSMatt Macy 				continue;
1274eda14cbcSMatt Macy 			}
1275eda14cbcSMatt Macy 			out_idx->bti_node = hdr;
1276eda14cbcSMatt Macy 			out_idx->bti_offset = i;
1277eda14cbcSMatt Macy 			out_idx->bti_before = B_FALSE;
1278eda14cbcSMatt Macy 			return (node->btc_elems + i * tree->bt_elem_size);
1279eda14cbcSMatt Macy 		}
1280eda14cbcSMatt Macy 		if (done_func != NULL)
1281eda14cbcSMatt Macy 			done_func(tree, prev);
1282eda14cbcSMatt Macy 		/*
1283eda14cbcSMatt Macy 		 * We've traversed all the way up and been at the end of the
1284eda14cbcSMatt Macy 		 * node every time, so this was the last element in the tree.
1285eda14cbcSMatt Macy 		 */
1286eda14cbcSMatt Macy 		return (NULL);
1287eda14cbcSMatt Macy 	}
1288eda14cbcSMatt Macy 
1289eda14cbcSMatt Macy 	/* If we were before an element in a core node, return that element. */
1290a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(idx->bti_node));
1291eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
1292eda14cbcSMatt Macy 	if (idx->bti_before) {
1293eda14cbcSMatt Macy 		out_idx->bti_before = B_FALSE;
1294eda14cbcSMatt Macy 		return (node->btc_elems + offset * tree->bt_elem_size);
1295eda14cbcSMatt Macy 	}
1296eda14cbcSMatt Macy 
1297eda14cbcSMatt Macy 	/*
1298eda14cbcSMatt Macy 	 * The next element from one in a core node is the first element in
1299eda14cbcSMatt Macy 	 * the subtree just to the right of the separator.
1300eda14cbcSMatt Macy 	 */
1301eda14cbcSMatt Macy 	zfs_btree_hdr_t *child = node->btc_children[offset + 1];
1302a0b956f5SMartin Matuska 	return (zfs_btree_first_helper(tree, child, out_idx));
1303eda14cbcSMatt Macy }
1304eda14cbcSMatt Macy 
1305eda14cbcSMatt Macy /*
1306eda14cbcSMatt Macy  * Return the next valued node in the tree.  The same address can be safely
1307eda14cbcSMatt Macy  * passed for idx and out_idx.
1308eda14cbcSMatt Macy  */
1309eda14cbcSMatt Macy void *
zfs_btree_next(zfs_btree_t * tree,const zfs_btree_index_t * idx,zfs_btree_index_t * out_idx)1310eda14cbcSMatt Macy zfs_btree_next(zfs_btree_t *tree, const zfs_btree_index_t *idx,
1311eda14cbcSMatt Macy     zfs_btree_index_t *out_idx)
1312eda14cbcSMatt Macy {
1313eda14cbcSMatt Macy 	return (zfs_btree_next_helper(tree, idx, out_idx, NULL));
1314eda14cbcSMatt Macy }
1315eda14cbcSMatt Macy 
1316eda14cbcSMatt Macy /*
1317eda14cbcSMatt Macy  * Return the previous valued node in the tree.  The same value can be safely
1318eda14cbcSMatt Macy  * passed for idx and out_idx.
1319eda14cbcSMatt Macy  */
1320eda14cbcSMatt Macy void *
zfs_btree_prev(zfs_btree_t * tree,const zfs_btree_index_t * idx,zfs_btree_index_t * out_idx)1321eda14cbcSMatt Macy zfs_btree_prev(zfs_btree_t *tree, const zfs_btree_index_t *idx,
1322eda14cbcSMatt Macy     zfs_btree_index_t *out_idx)
1323eda14cbcSMatt Macy {
1324eda14cbcSMatt Macy 	if (idx->bti_node == NULL) {
1325eda14cbcSMatt Macy 		ASSERT3S(tree->bt_height, ==, -1);
1326eda14cbcSMatt Macy 		return (NULL);
1327eda14cbcSMatt Macy 	}
1328eda14cbcSMatt Macy 
1329a0b956f5SMartin Matuska 	uint32_t offset = idx->bti_offset;
1330a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(idx->bti_node)) {
1331eda14cbcSMatt Macy 		/*
1332eda14cbcSMatt Macy 		 * When finding the previous element of an element in a leaf,
1333eda14cbcSMatt Macy 		 * there are two cases. If the element isn't the first one in
1334eda14cbcSMatt Macy 		 * the leaf, in which case we just return the previous element
1335eda14cbcSMatt Macy 		 * in the leaf. Otherwise, we need to traverse up our parents
1336eda14cbcSMatt Macy 		 * until we find one where our previous ancestor isn't the
1337eda14cbcSMatt Macy 		 * first child. Once we do, the previous element is the
1338eda14cbcSMatt Macy 		 * separator after our previous ancestor.
1339eda14cbcSMatt Macy 		 */
1340eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
1341eda14cbcSMatt Macy 		if (offset != 0) {
1342eda14cbcSMatt Macy 			out_idx->bti_node = &leaf->btl_hdr;
1343eda14cbcSMatt Macy 			out_idx->bti_offset = offset - 1;
1344eda14cbcSMatt Macy 			out_idx->bti_before = B_FALSE;
1345a0b956f5SMartin Matuska 			return (leaf->btl_elems + (leaf->btl_hdr.bth_first +
1346a0b956f5SMartin Matuska 			    offset - 1) * tree->bt_elem_size);
1347eda14cbcSMatt Macy 		}
1348eda14cbcSMatt Macy 		zfs_btree_hdr_t *prev = &leaf->btl_hdr;
1349eda14cbcSMatt Macy 		for (zfs_btree_core_t *node = leaf->btl_hdr.bth_parent;
1350eda14cbcSMatt Macy 		    node != NULL; node = node->btc_hdr.bth_parent) {
1351eda14cbcSMatt Macy 			zfs_btree_hdr_t *hdr = &node->btc_hdr;
1352a0b956f5SMartin Matuska 			ASSERT(zfs_btree_is_core(hdr));
1353a0b956f5SMartin Matuska 			uint32_t i = zfs_btree_find_parent_idx(tree, prev);
1354eda14cbcSMatt Macy 			if (i == 0) {
1355eda14cbcSMatt Macy 				prev = hdr;
1356eda14cbcSMatt Macy 				continue;
1357eda14cbcSMatt Macy 			}
1358eda14cbcSMatt Macy 			out_idx->bti_node = hdr;
1359eda14cbcSMatt Macy 			out_idx->bti_offset = i - 1;
1360eda14cbcSMatt Macy 			out_idx->bti_before = B_FALSE;
1361eda14cbcSMatt Macy 			return (node->btc_elems + (i - 1) * tree->bt_elem_size);
1362eda14cbcSMatt Macy 		}
1363eda14cbcSMatt Macy 		/*
1364eda14cbcSMatt Macy 		 * We've traversed all the way up and been at the start of the
1365eda14cbcSMatt Macy 		 * node every time, so this was the first node in the tree.
1366eda14cbcSMatt Macy 		 */
1367eda14cbcSMatt Macy 		return (NULL);
1368eda14cbcSMatt Macy 	}
1369eda14cbcSMatt Macy 
1370eda14cbcSMatt Macy 	/*
1371eda14cbcSMatt Macy 	 * The previous element from one in a core node is the last element in
1372eda14cbcSMatt Macy 	 * the subtree just to the left of the separator.
1373eda14cbcSMatt Macy 	 */
1374a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(idx->bti_node));
1375eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
1376eda14cbcSMatt Macy 	zfs_btree_hdr_t *child = node->btc_children[offset];
1377eda14cbcSMatt Macy 	return (zfs_btree_last_helper(tree, child, out_idx));
1378eda14cbcSMatt Macy }
1379eda14cbcSMatt Macy 
1380eda14cbcSMatt Macy /*
1381eda14cbcSMatt Macy  * Get the value at the provided index in the tree.
1382eda14cbcSMatt Macy  *
1383eda14cbcSMatt Macy  * Note that the value returned from this function can be mutated, but only
1384eda14cbcSMatt Macy  * if it will not change the ordering of the element with respect to any other
1385eda14cbcSMatt Macy  * elements that could be in the tree.
1386eda14cbcSMatt Macy  */
1387eda14cbcSMatt Macy void *
zfs_btree_get(zfs_btree_t * tree,zfs_btree_index_t * idx)1388eda14cbcSMatt Macy zfs_btree_get(zfs_btree_t *tree, zfs_btree_index_t *idx)
1389eda14cbcSMatt Macy {
1390eda14cbcSMatt Macy 	ASSERT(!idx->bti_before);
1391a0b956f5SMartin Matuska 	size_t size = tree->bt_elem_size;
1392a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(idx->bti_node)) {
1393eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
1394a0b956f5SMartin Matuska 		return (leaf->btl_elems + (leaf->btl_hdr.bth_first +
1395a0b956f5SMartin Matuska 		    idx->bti_offset) * size);
1396eda14cbcSMatt Macy 	}
1397eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
1398a0b956f5SMartin Matuska 	return (node->btc_elems + idx->bti_offset * size);
1399eda14cbcSMatt Macy }
1400eda14cbcSMatt Macy 
1401eda14cbcSMatt Macy /* Add the given value to the tree. Must not already be in the tree. */
1402eda14cbcSMatt Macy void
zfs_btree_add(zfs_btree_t * tree,const void * node)1403eda14cbcSMatt Macy zfs_btree_add(zfs_btree_t *tree, const void *node)
1404eda14cbcSMatt Macy {
1405eda14cbcSMatt Macy 	zfs_btree_index_t where = {0};
1406eda14cbcSMatt Macy 	VERIFY3P(zfs_btree_find(tree, node, &where), ==, NULL);
1407eda14cbcSMatt Macy 	zfs_btree_add_idx(tree, node, &where);
1408eda14cbcSMatt Macy }
1409eda14cbcSMatt Macy 
1410eda14cbcSMatt Macy /* Helper function to free a tree node. */
1411eda14cbcSMatt Macy static void
zfs_btree_node_destroy(zfs_btree_t * tree,zfs_btree_hdr_t * node)1412eda14cbcSMatt Macy zfs_btree_node_destroy(zfs_btree_t *tree, zfs_btree_hdr_t *node)
1413eda14cbcSMatt Macy {
1414eda14cbcSMatt Macy 	tree->bt_num_nodes--;
1415a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(node)) {
1416dbd5678dSMartin Matuska 		zfs_btree_leaf_free(tree, node);
1417eda14cbcSMatt Macy 	} else {
1418eda14cbcSMatt Macy 		kmem_free(node, sizeof (zfs_btree_core_t) +
1419eda14cbcSMatt Macy 		    BTREE_CORE_ELEMS * tree->bt_elem_size);
1420eda14cbcSMatt Macy 	}
1421eda14cbcSMatt Macy }
1422eda14cbcSMatt Macy 
1423eda14cbcSMatt Macy /*
1424eda14cbcSMatt Macy  * Remove the rm_hdr and the separator to its left from the parent node. The
1425eda14cbcSMatt Macy  * buffer that rm_hdr was stored in may already be freed, so its contents
1426eda14cbcSMatt Macy  * cannot be accessed.
1427eda14cbcSMatt Macy  */
1428eda14cbcSMatt Macy static void
zfs_btree_remove_from_node(zfs_btree_t * tree,zfs_btree_core_t * node,zfs_btree_hdr_t * rm_hdr)1429eda14cbcSMatt Macy zfs_btree_remove_from_node(zfs_btree_t *tree, zfs_btree_core_t *node,
1430eda14cbcSMatt Macy     zfs_btree_hdr_t *rm_hdr)
1431eda14cbcSMatt Macy {
1432eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
1433a0b956f5SMartin Matuska 	uint32_t min_count = (BTREE_CORE_ELEMS / 2) - 1;
1434eda14cbcSMatt Macy 	zfs_btree_hdr_t *hdr = &node->btc_hdr;
1435eda14cbcSMatt Macy 	/*
1436eda14cbcSMatt Macy 	 * If the node is the root node and rm_hdr is one of two children,
1437eda14cbcSMatt Macy 	 * promote the other child to the root.
1438eda14cbcSMatt Macy 	 */
1439eda14cbcSMatt Macy 	if (hdr->bth_parent == NULL && hdr->bth_count <= 1) {
1440eda14cbcSMatt Macy 		ASSERT3U(hdr->bth_count, ==, 1);
1441eda14cbcSMatt Macy 		ASSERT3P(tree->bt_root, ==, node);
1442eda14cbcSMatt Macy 		ASSERT3P(node->btc_children[1], ==, rm_hdr);
1443eda14cbcSMatt Macy 		tree->bt_root = node->btc_children[0];
1444eda14cbcSMatt Macy 		node->btc_children[0]->bth_parent = NULL;
1445eda14cbcSMatt Macy 		zfs_btree_node_destroy(tree, hdr);
1446eda14cbcSMatt Macy 		tree->bt_height--;
1447eda14cbcSMatt Macy 		return;
1448eda14cbcSMatt Macy 	}
1449eda14cbcSMatt Macy 
1450a0b956f5SMartin Matuska 	uint32_t idx;
1451eda14cbcSMatt Macy 	for (idx = 0; idx <= hdr->bth_count; idx++) {
1452eda14cbcSMatt Macy 		if (node->btc_children[idx] == rm_hdr)
1453eda14cbcSMatt Macy 			break;
1454eda14cbcSMatt Macy 	}
1455eda14cbcSMatt Macy 	ASSERT3U(idx, <=, hdr->bth_count);
1456eda14cbcSMatt Macy 
1457eda14cbcSMatt Macy 	/*
1458eda14cbcSMatt Macy 	 * If the node is the root or it has more than the minimum number of
1459eda14cbcSMatt Macy 	 * children, just remove the child and separator, and return.
1460eda14cbcSMatt Macy 	 */
1461eda14cbcSMatt Macy 	if (hdr->bth_parent == NULL ||
1462eda14cbcSMatt Macy 	    hdr->bth_count > min_count) {
1463eda14cbcSMatt Macy 		/*
1464eda14cbcSMatt Macy 		 * Shift the element and children to the right of rm_hdr to
1465eda14cbcSMatt Macy 		 * the left by one spot.
1466eda14cbcSMatt Macy 		 */
1467eda14cbcSMatt Macy 		bt_shift_core_left(tree, node, idx, hdr->bth_count - idx,
1468eda14cbcSMatt Macy 		    BSS_PARALLELOGRAM);
1469eda14cbcSMatt Macy 		hdr->bth_count--;
1470a0b956f5SMartin Matuska 		zfs_btree_poison_node_at(tree, hdr, hdr->bth_count, 1);
1471eda14cbcSMatt Macy 		return;
1472eda14cbcSMatt Macy 	}
1473eda14cbcSMatt Macy 
1474eda14cbcSMatt Macy 	ASSERT3U(hdr->bth_count, ==, min_count);
1475eda14cbcSMatt Macy 
1476eda14cbcSMatt Macy 	/*
1477eda14cbcSMatt Macy 	 * Now we try to take a node from a neighbor. We check left, then
1478eda14cbcSMatt Macy 	 * right. If the neighbor exists and has more than the minimum number
1479eda14cbcSMatt Macy 	 * of elements, we move the separator between us and them to our
1480eda14cbcSMatt Macy 	 * node, move their closest element (last for left, first for right)
1481eda14cbcSMatt Macy 	 * to the separator, and move their closest child to our node. Along
1482eda14cbcSMatt Macy 	 * the way we need to collapse the gap made by idx, and (for our right
1483eda14cbcSMatt Macy 	 * neighbor) the gap made by removing their first element and child.
1484eda14cbcSMatt Macy 	 *
1485eda14cbcSMatt Macy 	 * Note: this logic currently doesn't support taking from a neighbor
1486eda14cbcSMatt Macy 	 * that isn't a sibling (i.e. a neighbor with a different
1487eda14cbcSMatt Macy 	 * parent). This isn't critical functionality, but may be worth
1488eda14cbcSMatt Macy 	 * implementing in the future for completeness' sake.
1489eda14cbcSMatt Macy 	 */
1490eda14cbcSMatt Macy 	zfs_btree_core_t *parent = hdr->bth_parent;
1491a0b956f5SMartin Matuska 	uint32_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);
1492eda14cbcSMatt Macy 
1493eda14cbcSMatt Macy 	zfs_btree_hdr_t *l_hdr = (parent_idx == 0 ? NULL :
1494eda14cbcSMatt Macy 	    parent->btc_children[parent_idx - 1]);
1495eda14cbcSMatt Macy 	if (l_hdr != NULL && l_hdr->bth_count > min_count) {
1496eda14cbcSMatt Macy 		/* We can take a node from the left neighbor. */
1497a0b956f5SMartin Matuska 		ASSERT(zfs_btree_is_core(l_hdr));
1498eda14cbcSMatt Macy 		zfs_btree_core_t *neighbor = (zfs_btree_core_t *)l_hdr;
1499eda14cbcSMatt Macy 
1500eda14cbcSMatt Macy 		/*
1501eda14cbcSMatt Macy 		 * Start by shifting the elements and children in the current
1502eda14cbcSMatt Macy 		 * node to the right by one spot.
1503eda14cbcSMatt Macy 		 */
1504eda14cbcSMatt Macy 		bt_shift_core_right(tree, node, 0, idx - 1, BSS_TRAPEZOID);
1505eda14cbcSMatt Macy 
1506eda14cbcSMatt Macy 		/*
1507eda14cbcSMatt Macy 		 * Move the separator between node and neighbor to the first
1508eda14cbcSMatt Macy 		 * element slot in the current node.
1509eda14cbcSMatt Macy 		 */
1510eda14cbcSMatt Macy 		uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
1511eda14cbcSMatt Macy 		    size;
1512a0b956f5SMartin Matuska 		bcpy(separator, node->btc_elems, size);
1513eda14cbcSMatt Macy 
1514eda14cbcSMatt Macy 		/* Move the last child of neighbor to our first child slot. */
1515a0b956f5SMartin Matuska 		node->btc_children[0] =
1516a0b956f5SMartin Matuska 		    neighbor->btc_children[l_hdr->bth_count];
1517eda14cbcSMatt Macy 		node->btc_children[0]->bth_parent = node;
1518eda14cbcSMatt Macy 
1519eda14cbcSMatt Macy 		/* Move the last element of neighbor to the separator spot. */
1520eda14cbcSMatt Macy 		uint8_t *take_elem = neighbor->btc_elems +
1521eda14cbcSMatt Macy 		    (l_hdr->bth_count - 1) * size;
1522a0b956f5SMartin Matuska 		bcpy(take_elem, separator, size);
1523eda14cbcSMatt Macy 		l_hdr->bth_count--;
1524a0b956f5SMartin Matuska 		zfs_btree_poison_node_at(tree, l_hdr, l_hdr->bth_count, 1);
1525eda14cbcSMatt Macy 		return;
1526eda14cbcSMatt Macy 	}
1527eda14cbcSMatt Macy 
1528eda14cbcSMatt Macy 	zfs_btree_hdr_t *r_hdr = (parent_idx == parent->btc_hdr.bth_count ?
1529eda14cbcSMatt Macy 	    NULL : parent->btc_children[parent_idx + 1]);
1530eda14cbcSMatt Macy 	if (r_hdr != NULL && r_hdr->bth_count > min_count) {
1531eda14cbcSMatt Macy 		/* We can take a node from the right neighbor. */
1532a0b956f5SMartin Matuska 		ASSERT(zfs_btree_is_core(r_hdr));
1533eda14cbcSMatt Macy 		zfs_btree_core_t *neighbor = (zfs_btree_core_t *)r_hdr;
1534eda14cbcSMatt Macy 
1535eda14cbcSMatt Macy 		/*
1536eda14cbcSMatt Macy 		 * Shift elements in node left by one spot to overwrite rm_hdr
1537eda14cbcSMatt Macy 		 * and the separator before it.
1538eda14cbcSMatt Macy 		 */
1539eda14cbcSMatt Macy 		bt_shift_core_left(tree, node, idx, hdr->bth_count - idx,
1540eda14cbcSMatt Macy 		    BSS_PARALLELOGRAM);
1541eda14cbcSMatt Macy 
1542eda14cbcSMatt Macy 		/*
1543eda14cbcSMatt Macy 		 * Move the separator between node and neighbor to the last
1544eda14cbcSMatt Macy 		 * element spot in node.
1545eda14cbcSMatt Macy 		 */
1546eda14cbcSMatt Macy 		uint8_t *separator = parent->btc_elems + parent_idx * size;
1547a0b956f5SMartin Matuska 		bcpy(separator, node->btc_elems + (hdr->bth_count - 1) * size,
1548eda14cbcSMatt Macy 		    size);
1549eda14cbcSMatt Macy 
1550eda14cbcSMatt Macy 		/*
1551eda14cbcSMatt Macy 		 * Move the first child of neighbor to the last child spot in
1552eda14cbcSMatt Macy 		 * node.
1553eda14cbcSMatt Macy 		 */
1554a0b956f5SMartin Matuska 		node->btc_children[hdr->bth_count] = neighbor->btc_children[0];
1555eda14cbcSMatt Macy 		node->btc_children[hdr->bth_count]->bth_parent = node;
1556eda14cbcSMatt Macy 
1557eda14cbcSMatt Macy 		/* Move the first element of neighbor to the separator spot. */
1558eda14cbcSMatt Macy 		uint8_t *take_elem = neighbor->btc_elems;
1559a0b956f5SMartin Matuska 		bcpy(take_elem, separator, size);
1560eda14cbcSMatt Macy 		r_hdr->bth_count--;
1561eda14cbcSMatt Macy 
1562eda14cbcSMatt Macy 		/*
1563eda14cbcSMatt Macy 		 * Shift the elements and children of neighbor to cover the
1564eda14cbcSMatt Macy 		 * stolen elements.
1565eda14cbcSMatt Macy 		 */
1566eda14cbcSMatt Macy 		bt_shift_core_left(tree, neighbor, 1, r_hdr->bth_count,
1567eda14cbcSMatt Macy 		    BSS_TRAPEZOID);
1568a0b956f5SMartin Matuska 		zfs_btree_poison_node_at(tree, r_hdr, r_hdr->bth_count, 1);
1569eda14cbcSMatt Macy 		return;
1570eda14cbcSMatt Macy 	}
1571eda14cbcSMatt Macy 
1572eda14cbcSMatt Macy 	/*
1573eda14cbcSMatt Macy 	 * In this case, neither of our neighbors can spare an element, so we
1574eda14cbcSMatt Macy 	 * need to merge with one of them. We prefer the left one,
1575eda14cbcSMatt Macy 	 * arbitrarily. Move the separator into the leftmost merging node
1576eda14cbcSMatt Macy 	 * (which may be us or the left neighbor), and then move the right
1577eda14cbcSMatt Macy 	 * merging node's elements. Once that's done, we go back and delete
1578eda14cbcSMatt Macy 	 * the element we're removing. Finally, go into the parent and delete
1579eda14cbcSMatt Macy 	 * the right merging node and the separator. This may cause further
1580eda14cbcSMatt Macy 	 * merging.
1581eda14cbcSMatt Macy 	 */
1582eda14cbcSMatt Macy 	zfs_btree_hdr_t *new_rm_hdr, *keep_hdr;
1583a0b956f5SMartin Matuska 	uint32_t new_idx = idx;
1584eda14cbcSMatt Macy 	if (l_hdr != NULL) {
1585eda14cbcSMatt Macy 		keep_hdr = l_hdr;
1586eda14cbcSMatt Macy 		new_rm_hdr = hdr;
1587eda14cbcSMatt Macy 		new_idx += keep_hdr->bth_count + 1;
1588eda14cbcSMatt Macy 	} else {
1589eda14cbcSMatt Macy 		ASSERT3P(r_hdr, !=, NULL);
1590eda14cbcSMatt Macy 		keep_hdr = hdr;
1591eda14cbcSMatt Macy 		new_rm_hdr = r_hdr;
1592eda14cbcSMatt Macy 		parent_idx++;
1593eda14cbcSMatt Macy 	}
1594eda14cbcSMatt Macy 
1595a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(keep_hdr));
1596a0b956f5SMartin Matuska 	ASSERT(zfs_btree_is_core(new_rm_hdr));
1597eda14cbcSMatt Macy 
1598eda14cbcSMatt Macy 	zfs_btree_core_t *keep = (zfs_btree_core_t *)keep_hdr;
1599eda14cbcSMatt Macy 	zfs_btree_core_t *rm = (zfs_btree_core_t *)new_rm_hdr;
1600eda14cbcSMatt Macy 
1601eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity >= 5) {
1602a0b956f5SMartin Matuska 		for (uint32_t i = 0; i < new_rm_hdr->bth_count + 1; i++) {
1603eda14cbcSMatt Macy 			zfs_btree_verify_poison_at(tree, keep_hdr,
1604eda14cbcSMatt Macy 			    keep_hdr->bth_count + i);
1605eda14cbcSMatt Macy 		}
1606eda14cbcSMatt Macy 	}
1607eda14cbcSMatt Macy 
1608eda14cbcSMatt Macy 	/* Move the separator into the left node. */
1609eda14cbcSMatt Macy 	uint8_t *e_out = keep->btc_elems + keep_hdr->bth_count * size;
1610eda14cbcSMatt Macy 	uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
1611eda14cbcSMatt Macy 	    size;
1612a0b956f5SMartin Matuska 	bcpy(separator, e_out, size);
1613eda14cbcSMatt Macy 	keep_hdr->bth_count++;
1614eda14cbcSMatt Macy 
1615eda14cbcSMatt Macy 	/* Move all our elements and children into the left node. */
1616eda14cbcSMatt Macy 	bt_transfer_core(tree, rm, 0, new_rm_hdr->bth_count, keep,
1617eda14cbcSMatt Macy 	    keep_hdr->bth_count, BSS_TRAPEZOID);
1618eda14cbcSMatt Macy 
1619a0b956f5SMartin Matuska 	uint32_t old_count = keep_hdr->bth_count;
1620eda14cbcSMatt Macy 
1621eda14cbcSMatt Macy 	/* Update bookkeeping */
1622eda14cbcSMatt Macy 	keep_hdr->bth_count += new_rm_hdr->bth_count;
1623eda14cbcSMatt Macy 	ASSERT3U(keep_hdr->bth_count, ==, (min_count * 2) + 1);
1624eda14cbcSMatt Macy 
1625eda14cbcSMatt Macy 	/*
1626eda14cbcSMatt Macy 	 * Shift the element and children to the right of rm_hdr to
1627eda14cbcSMatt Macy 	 * the left by one spot.
1628eda14cbcSMatt Macy 	 */
1629eda14cbcSMatt Macy 	ASSERT3P(keep->btc_children[new_idx], ==, rm_hdr);
1630eda14cbcSMatt Macy 	bt_shift_core_left(tree, keep, new_idx, keep_hdr->bth_count - new_idx,
1631eda14cbcSMatt Macy 	    BSS_PARALLELOGRAM);
1632eda14cbcSMatt Macy 	keep_hdr->bth_count--;
1633eda14cbcSMatt Macy 
1634eda14cbcSMatt Macy 	/* Reparent all our children to point to the left node. */
1635eda14cbcSMatt Macy 	zfs_btree_hdr_t **new_start = keep->btc_children +
1636eda14cbcSMatt Macy 	    old_count - 1;
1637a0b956f5SMartin Matuska 	for (uint32_t i = 0; i < new_rm_hdr->bth_count + 1; i++)
1638eda14cbcSMatt Macy 		new_start[i]->bth_parent = keep;
1639a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= keep_hdr->bth_count; i++) {
1640eda14cbcSMatt Macy 		ASSERT3P(keep->btc_children[i]->bth_parent, ==, keep);
1641eda14cbcSMatt Macy 		ASSERT3P(keep->btc_children[i], !=, rm_hdr);
1642eda14cbcSMatt Macy 	}
1643a0b956f5SMartin Matuska 	zfs_btree_poison_node_at(tree, keep_hdr, keep_hdr->bth_count, 1);
1644eda14cbcSMatt Macy 
1645eda14cbcSMatt Macy 	new_rm_hdr->bth_count = 0;
1646eda14cbcSMatt Macy 	zfs_btree_remove_from_node(tree, parent, new_rm_hdr);
1647c7046f76SMartin Matuska 	zfs_btree_node_destroy(tree, new_rm_hdr);
1648eda14cbcSMatt Macy }
1649eda14cbcSMatt Macy 
1650eda14cbcSMatt Macy /* Remove the element at the specific location. */
1651eda14cbcSMatt Macy void
zfs_btree_remove_idx(zfs_btree_t * tree,zfs_btree_index_t * where)1652eda14cbcSMatt Macy zfs_btree_remove_idx(zfs_btree_t *tree, zfs_btree_index_t *where)
1653eda14cbcSMatt Macy {
1654eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
1655eda14cbcSMatt Macy 	zfs_btree_hdr_t *hdr = where->bti_node;
1656a0b956f5SMartin Matuska 	uint32_t idx = where->bti_offset;
1657eda14cbcSMatt Macy 
1658eda14cbcSMatt Macy 	ASSERT(!where->bti_before);
1659eda14cbcSMatt Macy 	if (tree->bt_bulk != NULL) {
1660eda14cbcSMatt Macy 		/*
1661eda14cbcSMatt Macy 		 * Leave bulk insert mode. Note that our index would be
1662eda14cbcSMatt Macy 		 * invalid after we correct the tree, so we copy the value
1663eda14cbcSMatt Macy 		 * we're planning to remove and find it again after
1664eda14cbcSMatt Macy 		 * bulk_finish.
1665eda14cbcSMatt Macy 		 */
1666eda14cbcSMatt Macy 		uint8_t *value = zfs_btree_get(tree, where);
1667eda14cbcSMatt Macy 		uint8_t *tmp = kmem_alloc(size, KM_SLEEP);
1668a0b956f5SMartin Matuska 		bcpy(value, tmp, size);
1669eda14cbcSMatt Macy 		zfs_btree_bulk_finish(tree);
1670eda14cbcSMatt Macy 		VERIFY3P(zfs_btree_find(tree, tmp, where), !=, NULL);
1671eda14cbcSMatt Macy 		kmem_free(tmp, size);
1672eda14cbcSMatt Macy 		hdr = where->bti_node;
1673eda14cbcSMatt Macy 		idx = where->bti_offset;
1674eda14cbcSMatt Macy 	}
1675eda14cbcSMatt Macy 
1676eda14cbcSMatt Macy 	tree->bt_num_elems--;
1677eda14cbcSMatt Macy 	/*
1678eda14cbcSMatt Macy 	 * If the element happens to be in a core node, we move a leaf node's
1679eda14cbcSMatt Macy 	 * element into its place and then remove the leaf node element. This
1680eda14cbcSMatt Macy 	 * makes the rebalance logic not need to be recursive both upwards and
1681eda14cbcSMatt Macy 	 * downwards.
1682eda14cbcSMatt Macy 	 */
1683a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
1684eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
1685eda14cbcSMatt Macy 		zfs_btree_hdr_t *left_subtree = node->btc_children[idx];
1686eda14cbcSMatt Macy 		void *new_value = zfs_btree_last_helper(tree, left_subtree,
1687eda14cbcSMatt Macy 		    where);
1688eda14cbcSMatt Macy 		ASSERT3P(new_value, !=, NULL);
1689eda14cbcSMatt Macy 
1690a0b956f5SMartin Matuska 		bcpy(new_value, node->btc_elems + idx * size, size);
1691eda14cbcSMatt Macy 
1692eda14cbcSMatt Macy 		hdr = where->bti_node;
1693eda14cbcSMatt Macy 		idx = where->bti_offset;
1694eda14cbcSMatt Macy 		ASSERT(!where->bti_before);
1695eda14cbcSMatt Macy 	}
1696eda14cbcSMatt Macy 
1697eda14cbcSMatt Macy 	/*
1698eda14cbcSMatt Macy 	 * First, we'll update the leaf's metadata. Then, we shift any
1699eda14cbcSMatt Macy 	 * elements after the idx to the left. After that, we rebalance if
1700eda14cbcSMatt Macy 	 * needed.
1701eda14cbcSMatt Macy 	 */
1702a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(hdr));
1703eda14cbcSMatt Macy 	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
1704eda14cbcSMatt Macy 	ASSERT3U(hdr->bth_count, >, 0);
1705eda14cbcSMatt Macy 
1706a0b956f5SMartin Matuska 	uint32_t min_count = (tree->bt_leaf_cap / 2) - 1;
1707eda14cbcSMatt Macy 
1708eda14cbcSMatt Macy 	/*
1709eda14cbcSMatt Macy 	 * If we're over the minimum size or this is the root, just overwrite
1710eda14cbcSMatt Macy 	 * the value and return.
1711eda14cbcSMatt Macy 	 */
1712eda14cbcSMatt Macy 	if (hdr->bth_count > min_count || hdr->bth_parent == NULL) {
1713a0b956f5SMartin Matuska 		bt_shrink_leaf(tree, leaf, idx, 1);
1714eda14cbcSMatt Macy 		if (hdr->bth_parent == NULL) {
1715eda14cbcSMatt Macy 			ASSERT0(tree->bt_height);
1716eda14cbcSMatt Macy 			if (hdr->bth_count == 0) {
1717eda14cbcSMatt Macy 				tree->bt_root = NULL;
1718eda14cbcSMatt Macy 				tree->bt_height--;
1719eda14cbcSMatt Macy 				zfs_btree_node_destroy(tree, &leaf->btl_hdr);
1720eda14cbcSMatt Macy 			}
1721eda14cbcSMatt Macy 		}
1722eda14cbcSMatt Macy 		zfs_btree_verify(tree);
1723eda14cbcSMatt Macy 		return;
1724eda14cbcSMatt Macy 	}
1725eda14cbcSMatt Macy 	ASSERT3U(hdr->bth_count, ==, min_count);
1726eda14cbcSMatt Macy 
1727eda14cbcSMatt Macy 	/*
1728eda14cbcSMatt Macy 	 * Now we try to take a node from a sibling. We check left, then
1729eda14cbcSMatt Macy 	 * right. If they exist and have more than the minimum number of
1730eda14cbcSMatt Macy 	 * elements, we move the separator between us and them to our node
1731eda14cbcSMatt Macy 	 * and move their closest element (last for left, first for right) to
1732eda14cbcSMatt Macy 	 * the separator. Along the way we need to collapse the gap made by
1733eda14cbcSMatt Macy 	 * idx, and (for our right neighbor) the gap made by removing their
1734eda14cbcSMatt Macy 	 * first element.
1735eda14cbcSMatt Macy 	 *
1736eda14cbcSMatt Macy 	 * Note: this logic currently doesn't support taking from a neighbor
1737eda14cbcSMatt Macy 	 * that isn't a sibling. This isn't critical functionality, but may be
1738eda14cbcSMatt Macy 	 * worth implementing in the future for completeness' sake.
1739eda14cbcSMatt Macy 	 */
1740eda14cbcSMatt Macy 	zfs_btree_core_t *parent = hdr->bth_parent;
1741a0b956f5SMartin Matuska 	uint32_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);
1742eda14cbcSMatt Macy 
1743eda14cbcSMatt Macy 	zfs_btree_hdr_t *l_hdr = (parent_idx == 0 ? NULL :
1744eda14cbcSMatt Macy 	    parent->btc_children[parent_idx - 1]);
1745eda14cbcSMatt Macy 	if (l_hdr != NULL && l_hdr->bth_count > min_count) {
1746eda14cbcSMatt Macy 		/* We can take a node from the left neighbor. */
1747a0b956f5SMartin Matuska 		ASSERT(!zfs_btree_is_core(l_hdr));
1748a0b956f5SMartin Matuska 		zfs_btree_leaf_t *neighbor = (zfs_btree_leaf_t *)l_hdr;
1749eda14cbcSMatt Macy 
1750eda14cbcSMatt Macy 		/*
1751eda14cbcSMatt Macy 		 * Move our elements back by one spot to make room for the
1752eda14cbcSMatt Macy 		 * stolen element and overwrite the element being removed.
1753eda14cbcSMatt Macy 		 */
1754a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, 0, idx, 1, BSD_RIGHT);
1755a0b956f5SMartin Matuska 
1756a0b956f5SMartin Matuska 		/* Move the separator to our first spot. */
1757eda14cbcSMatt Macy 		uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
1758eda14cbcSMatt Macy 		    size;
1759a0b956f5SMartin Matuska 		bcpy(separator, leaf->btl_elems + hdr->bth_first * size, size);
1760eda14cbcSMatt Macy 
1761eda14cbcSMatt Macy 		/* Move our neighbor's last element to the separator. */
1762a0b956f5SMartin Matuska 		uint8_t *take_elem = neighbor->btl_elems +
1763a0b956f5SMartin Matuska 		    (l_hdr->bth_first + l_hdr->bth_count - 1) * size;
1764a0b956f5SMartin Matuska 		bcpy(take_elem, separator, size);
1765eda14cbcSMatt Macy 
1766a0b956f5SMartin Matuska 		/* Delete our neighbor's last element. */
1767a0b956f5SMartin Matuska 		bt_shrink_leaf(tree, neighbor, l_hdr->bth_count - 1, 1);
1768eda14cbcSMatt Macy 		zfs_btree_verify(tree);
1769eda14cbcSMatt Macy 		return;
1770eda14cbcSMatt Macy 	}
1771eda14cbcSMatt Macy 
1772eda14cbcSMatt Macy 	zfs_btree_hdr_t *r_hdr = (parent_idx == parent->btc_hdr.bth_count ?
1773eda14cbcSMatt Macy 	    NULL : parent->btc_children[parent_idx + 1]);
1774eda14cbcSMatt Macy 	if (r_hdr != NULL && r_hdr->bth_count > min_count) {
1775eda14cbcSMatt Macy 		/* We can take a node from the right neighbor. */
1776a0b956f5SMartin Matuska 		ASSERT(!zfs_btree_is_core(r_hdr));
1777eda14cbcSMatt Macy 		zfs_btree_leaf_t *neighbor = (zfs_btree_leaf_t *)r_hdr;
1778eda14cbcSMatt Macy 
1779eda14cbcSMatt Macy 		/*
1780eda14cbcSMatt Macy 		 * Move our elements after the element being removed forwards
1781eda14cbcSMatt Macy 		 * by one spot to make room for the stolen element and
1782eda14cbcSMatt Macy 		 * overwrite the element being removed.
1783eda14cbcSMatt Macy 		 */
1784a0b956f5SMartin Matuska 		bt_shift_leaf(tree, leaf, idx + 1, hdr->bth_count - idx - 1,
1785a0b956f5SMartin Matuska 		    1, BSD_LEFT);
1786eda14cbcSMatt Macy 
1787eda14cbcSMatt Macy 		/* Move the separator between us to our last spot. */
1788a0b956f5SMartin Matuska 		uint8_t *separator = parent->btc_elems + parent_idx * size;
1789a0b956f5SMartin Matuska 		bcpy(separator, leaf->btl_elems + (hdr->bth_first +
1790a0b956f5SMartin Matuska 		    hdr->bth_count - 1) * size, size);
1791eda14cbcSMatt Macy 
1792eda14cbcSMatt Macy 		/* Move our neighbor's first element to the separator. */
1793a0b956f5SMartin Matuska 		uint8_t *take_elem = neighbor->btl_elems +
1794a0b956f5SMartin Matuska 		    r_hdr->bth_first * size;
1795a0b956f5SMartin Matuska 		bcpy(take_elem, separator, size);
1796eda14cbcSMatt Macy 
1797a0b956f5SMartin Matuska 		/* Delete our neighbor's first element. */
1798a0b956f5SMartin Matuska 		bt_shrink_leaf(tree, neighbor, 0, 1);
1799eda14cbcSMatt Macy 		zfs_btree_verify(tree);
1800eda14cbcSMatt Macy 		return;
1801eda14cbcSMatt Macy 	}
1802eda14cbcSMatt Macy 
1803eda14cbcSMatt Macy 	/*
1804eda14cbcSMatt Macy 	 * In this case, neither of our neighbors can spare an element, so we
1805a0b956f5SMartin Matuska 	 * need to merge with one of them. We prefer the left one, arbitrarily.
1806a0b956f5SMartin Matuska 	 * After remove we move the separator into the leftmost merging node
1807eda14cbcSMatt Macy 	 * (which may be us or the left neighbor), and then move the right
1808eda14cbcSMatt Macy 	 * merging node's elements. Once that's done, we go back and delete
1809eda14cbcSMatt Macy 	 * the element we're removing. Finally, go into the parent and delete
1810eda14cbcSMatt Macy 	 * the right merging node and the separator. This may cause further
1811eda14cbcSMatt Macy 	 * merging.
1812eda14cbcSMatt Macy 	 */
1813a0b956f5SMartin Matuska 	zfs_btree_hdr_t *rm_hdr, *k_hdr;
1814eda14cbcSMatt Macy 	if (l_hdr != NULL) {
1815a0b956f5SMartin Matuska 		k_hdr = l_hdr;
1816eda14cbcSMatt Macy 		rm_hdr = hdr;
1817eda14cbcSMatt Macy 	} else {
1818eda14cbcSMatt Macy 		ASSERT3P(r_hdr, !=, NULL);
1819a0b956f5SMartin Matuska 		k_hdr = hdr;
1820eda14cbcSMatt Macy 		rm_hdr = r_hdr;
1821eda14cbcSMatt Macy 		parent_idx++;
1822eda14cbcSMatt Macy 	}
1823a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(k_hdr));
1824a0b956f5SMartin Matuska 	ASSERT(!zfs_btree_is_core(rm_hdr));
1825a0b956f5SMartin Matuska 	ASSERT3U(k_hdr->bth_count, ==, min_count);
1826eda14cbcSMatt Macy 	ASSERT3U(rm_hdr->bth_count, ==, min_count);
1827a0b956f5SMartin Matuska 	zfs_btree_leaf_t *keep = (zfs_btree_leaf_t *)k_hdr;
1828eda14cbcSMatt Macy 	zfs_btree_leaf_t *rm = (zfs_btree_leaf_t *)rm_hdr;
1829eda14cbcSMatt Macy 
1830eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity >= 5) {
1831a0b956f5SMartin Matuska 		for (uint32_t i = 0; i < rm_hdr->bth_count + 1; i++) {
1832a0b956f5SMartin Matuska 			zfs_btree_verify_poison_at(tree, k_hdr,
1833a0b956f5SMartin Matuska 			    k_hdr->bth_count + i);
1834eda14cbcSMatt Macy 		}
1835eda14cbcSMatt Macy 	}
1836a0b956f5SMartin Matuska 
1837eda14cbcSMatt Macy 	/*
1838a0b956f5SMartin Matuska 	 * Remove the value from the node.  It will go below the minimum,
1839a0b956f5SMartin Matuska 	 * but we'll fix it in no time.
1840eda14cbcSMatt Macy 	 */
1841a0b956f5SMartin Matuska 	bt_shrink_leaf(tree, leaf, idx, 1);
1842a0b956f5SMartin Matuska 
1843a0b956f5SMartin Matuska 	/* Prepare space for elements to be moved from the right. */
1844a0b956f5SMartin Matuska 	uint32_t k_count = k_hdr->bth_count;
1845a0b956f5SMartin Matuska 	bt_grow_leaf(tree, keep, k_count, 1 + rm_hdr->bth_count);
1846a0b956f5SMartin Matuska 	ASSERT3U(k_hdr->bth_count, ==, min_count * 2);
1847a0b956f5SMartin Matuska 
1848a0b956f5SMartin Matuska 	/* Move the separator into the first open spot. */
1849a0b956f5SMartin Matuska 	uint8_t *out = keep->btl_elems + (k_hdr->bth_first + k_count) * size;
1850a0b956f5SMartin Matuska 	uint8_t *separator = parent->btc_elems + (parent_idx - 1) * size;
1851a0b956f5SMartin Matuska 	bcpy(separator, out, size);
1852eda14cbcSMatt Macy 
1853eda14cbcSMatt Macy 	/* Move our elements to the left neighbor. */
1854a0b956f5SMartin Matuska 	bt_transfer_leaf(tree, rm, 0, rm_hdr->bth_count, keep, k_count + 1);
1855a0b956f5SMartin Matuska 
1856eda14cbcSMatt Macy 	/* Remove the emptied node from the parent. */
1857eda14cbcSMatt Macy 	zfs_btree_remove_from_node(tree, parent, rm_hdr);
1858c7046f76SMartin Matuska 	zfs_btree_node_destroy(tree, rm_hdr);
1859eda14cbcSMatt Macy 	zfs_btree_verify(tree);
1860eda14cbcSMatt Macy }
1861eda14cbcSMatt Macy 
1862eda14cbcSMatt Macy /* Remove the given value from the tree. */
1863eda14cbcSMatt Macy void
zfs_btree_remove(zfs_btree_t * tree,const void * value)1864eda14cbcSMatt Macy zfs_btree_remove(zfs_btree_t *tree, const void *value)
1865eda14cbcSMatt Macy {
1866eda14cbcSMatt Macy 	zfs_btree_index_t where = {0};
1867eda14cbcSMatt Macy 	VERIFY3P(zfs_btree_find(tree, value, &where), !=, NULL);
1868eda14cbcSMatt Macy 	zfs_btree_remove_idx(tree, &where);
1869eda14cbcSMatt Macy }
1870eda14cbcSMatt Macy 
1871eda14cbcSMatt Macy /* Return the number of elements in the tree. */
1872eda14cbcSMatt Macy ulong_t
zfs_btree_numnodes(zfs_btree_t * tree)1873eda14cbcSMatt Macy zfs_btree_numnodes(zfs_btree_t *tree)
1874eda14cbcSMatt Macy {
1875eda14cbcSMatt Macy 	return (tree->bt_num_elems);
1876eda14cbcSMatt Macy }
1877eda14cbcSMatt Macy 
1878eda14cbcSMatt Macy /*
1879eda14cbcSMatt Macy  * This function is used to visit all the elements in the tree before
1880eda14cbcSMatt Macy  * destroying the tree. This allows the calling code to perform any cleanup it
1881eda14cbcSMatt Macy  * needs to do. This is more efficient than just removing the first element
1882eda14cbcSMatt Macy  * over and over, because it removes all rebalancing. Once the destroy_nodes()
1883eda14cbcSMatt Macy  * function has been called, no other btree operations are valid until it
1884eda14cbcSMatt Macy  * returns NULL, which point the only valid operation is zfs_btree_destroy().
1885eda14cbcSMatt Macy  *
1886eda14cbcSMatt Macy  * example:
1887eda14cbcSMatt Macy  *
1888eda14cbcSMatt Macy  *      zfs_btree_index_t *cookie = NULL;
1889eda14cbcSMatt Macy  *      my_data_t *node;
1890eda14cbcSMatt Macy  *
1891eda14cbcSMatt Macy  *      while ((node = zfs_btree_destroy_nodes(tree, &cookie)) != NULL)
1892eda14cbcSMatt Macy  *              free(node->ptr);
1893eda14cbcSMatt Macy  *      zfs_btree_destroy(tree);
1894eda14cbcSMatt Macy  *
1895eda14cbcSMatt Macy  */
1896eda14cbcSMatt Macy void *
zfs_btree_destroy_nodes(zfs_btree_t * tree,zfs_btree_index_t ** cookie)1897eda14cbcSMatt Macy zfs_btree_destroy_nodes(zfs_btree_t *tree, zfs_btree_index_t **cookie)
1898eda14cbcSMatt Macy {
1899eda14cbcSMatt Macy 	if (*cookie == NULL) {
1900eda14cbcSMatt Macy 		if (tree->bt_height == -1)
1901eda14cbcSMatt Macy 			return (NULL);
1902eda14cbcSMatt Macy 		*cookie = kmem_alloc(sizeof (**cookie), KM_SLEEP);
1903eda14cbcSMatt Macy 		return (zfs_btree_first(tree, *cookie));
1904eda14cbcSMatt Macy 	}
1905eda14cbcSMatt Macy 
1906eda14cbcSMatt Macy 	void *rval = zfs_btree_next_helper(tree, *cookie, *cookie,
1907eda14cbcSMatt Macy 	    zfs_btree_node_destroy);
1908eda14cbcSMatt Macy 	if (rval == NULL)   {
1909eda14cbcSMatt Macy 		tree->bt_root = NULL;
1910eda14cbcSMatt Macy 		tree->bt_height = -1;
1911eda14cbcSMatt Macy 		tree->bt_num_elems = 0;
1912eda14cbcSMatt Macy 		kmem_free(*cookie, sizeof (**cookie));
1913eda14cbcSMatt Macy 		tree->bt_bulk = NULL;
1914eda14cbcSMatt Macy 	}
1915eda14cbcSMatt Macy 	return (rval);
1916eda14cbcSMatt Macy }
1917eda14cbcSMatt Macy 
1918eda14cbcSMatt Macy static void
zfs_btree_clear_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)1919eda14cbcSMatt Macy zfs_btree_clear_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
1920eda14cbcSMatt Macy {
1921a0b956f5SMartin Matuska 	if (zfs_btree_is_core(hdr)) {
1922eda14cbcSMatt Macy 		zfs_btree_core_t *btc = (zfs_btree_core_t *)hdr;
1923a0b956f5SMartin Matuska 		for (uint32_t i = 0; i <= hdr->bth_count; i++)
1924eda14cbcSMatt Macy 			zfs_btree_clear_helper(tree, btc->btc_children[i]);
1925eda14cbcSMatt Macy 	}
1926eda14cbcSMatt Macy 
1927eda14cbcSMatt Macy 	zfs_btree_node_destroy(tree, hdr);
1928eda14cbcSMatt Macy }
1929eda14cbcSMatt Macy 
1930eda14cbcSMatt Macy void
zfs_btree_clear(zfs_btree_t * tree)1931eda14cbcSMatt Macy zfs_btree_clear(zfs_btree_t *tree)
1932eda14cbcSMatt Macy {
1933eda14cbcSMatt Macy 	if (tree->bt_root == NULL) {
1934eda14cbcSMatt Macy 		ASSERT0(tree->bt_num_elems);
1935eda14cbcSMatt Macy 		return;
1936eda14cbcSMatt Macy 	}
1937eda14cbcSMatt Macy 
1938eda14cbcSMatt Macy 	zfs_btree_clear_helper(tree, tree->bt_root);
1939eda14cbcSMatt Macy 	tree->bt_num_elems = 0;
1940eda14cbcSMatt Macy 	tree->bt_root = NULL;
1941eda14cbcSMatt Macy 	tree->bt_num_nodes = 0;
1942eda14cbcSMatt Macy 	tree->bt_height = -1;
1943eda14cbcSMatt Macy 	tree->bt_bulk = NULL;
1944eda14cbcSMatt Macy }
1945eda14cbcSMatt Macy 
1946eda14cbcSMatt Macy void
zfs_btree_destroy(zfs_btree_t * tree)1947eda14cbcSMatt Macy zfs_btree_destroy(zfs_btree_t *tree)
1948eda14cbcSMatt Macy {
1949eda14cbcSMatt Macy 	ASSERT0(tree->bt_num_elems);
1950eda14cbcSMatt Macy 	ASSERT3P(tree->bt_root, ==, NULL);
1951eda14cbcSMatt Macy }
1952eda14cbcSMatt Macy 
1953eda14cbcSMatt Macy /* Verify that every child of this node has the correct parent pointer. */
1954eda14cbcSMatt Macy static void
zfs_btree_verify_pointers_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)1955eda14cbcSMatt Macy zfs_btree_verify_pointers_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
1956eda14cbcSMatt Macy {
1957a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(hdr))
1958eda14cbcSMatt Macy 		return;
1959eda14cbcSMatt Macy 
1960eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
1961a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= hdr->bth_count; i++) {
1962eda14cbcSMatt Macy 		VERIFY3P(node->btc_children[i]->bth_parent, ==, hdr);
1963eda14cbcSMatt Macy 		zfs_btree_verify_pointers_helper(tree, node->btc_children[i]);
1964eda14cbcSMatt Macy 	}
1965eda14cbcSMatt Macy }
1966eda14cbcSMatt Macy 
1967eda14cbcSMatt Macy /* Verify that every node has the correct parent pointer. */
1968eda14cbcSMatt Macy static void
zfs_btree_verify_pointers(zfs_btree_t * tree)1969eda14cbcSMatt Macy zfs_btree_verify_pointers(zfs_btree_t *tree)
1970eda14cbcSMatt Macy {
1971eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
1972eda14cbcSMatt Macy 		VERIFY3P(tree->bt_root, ==, NULL);
1973eda14cbcSMatt Macy 		return;
1974eda14cbcSMatt Macy 	}
1975eda14cbcSMatt Macy 	VERIFY3P(tree->bt_root->bth_parent, ==, NULL);
1976eda14cbcSMatt Macy 	zfs_btree_verify_pointers_helper(tree, tree->bt_root);
1977eda14cbcSMatt Macy }
1978eda14cbcSMatt Macy 
1979eda14cbcSMatt Macy /*
1980eda14cbcSMatt Macy  * Verify that all the current node and its children satisfy the count
1981eda14cbcSMatt Macy  * invariants, and return the total count in the subtree rooted in this node.
1982eda14cbcSMatt Macy  */
1983eda14cbcSMatt Macy static uint64_t
zfs_btree_verify_counts_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)1984eda14cbcSMatt Macy zfs_btree_verify_counts_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
1985eda14cbcSMatt Macy {
1986a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(hdr)) {
1987c03c5b1cSMartin Matuska 		if (tree->bt_root != hdr && tree->bt_bulk &&
1988c03c5b1cSMartin Matuska 		    hdr != &tree->bt_bulk->btl_hdr) {
1989a0b956f5SMartin Matuska 			VERIFY3U(hdr->bth_count, >=, tree->bt_leaf_cap / 2 - 1);
1990eda14cbcSMatt Macy 		}
1991eda14cbcSMatt Macy 
1992eda14cbcSMatt Macy 		return (hdr->bth_count);
1993eda14cbcSMatt Macy 	} else {
1994eda14cbcSMatt Macy 
1995eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
1996eda14cbcSMatt Macy 		uint64_t ret = hdr->bth_count;
1997eda14cbcSMatt Macy 		if (tree->bt_root != hdr && tree->bt_bulk == NULL)
1998eda14cbcSMatt Macy 			VERIFY3P(hdr->bth_count, >=, BTREE_CORE_ELEMS / 2 - 1);
1999a0b956f5SMartin Matuska 		for (uint32_t i = 0; i <= hdr->bth_count; i++) {
2000eda14cbcSMatt Macy 			ret += zfs_btree_verify_counts_helper(tree,
2001eda14cbcSMatt Macy 			    node->btc_children[i]);
2002eda14cbcSMatt Macy 		}
2003eda14cbcSMatt Macy 
2004eda14cbcSMatt Macy 		return (ret);
2005eda14cbcSMatt Macy 	}
2006eda14cbcSMatt Macy }
2007eda14cbcSMatt Macy 
2008eda14cbcSMatt Macy /*
2009eda14cbcSMatt Macy  * Verify that all nodes satisfy the invariants and that the total number of
2010eda14cbcSMatt Macy  * elements is correct.
2011eda14cbcSMatt Macy  */
2012eda14cbcSMatt Macy static void
zfs_btree_verify_counts(zfs_btree_t * tree)2013eda14cbcSMatt Macy zfs_btree_verify_counts(zfs_btree_t *tree)
2014eda14cbcSMatt Macy {
2015eda14cbcSMatt Macy 	EQUIV(tree->bt_num_elems == 0, tree->bt_height == -1);
2016eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
2017eda14cbcSMatt Macy 		return;
2018eda14cbcSMatt Macy 	}
2019eda14cbcSMatt Macy 	VERIFY3P(zfs_btree_verify_counts_helper(tree, tree->bt_root), ==,
2020eda14cbcSMatt Macy 	    tree->bt_num_elems);
2021eda14cbcSMatt Macy }
2022eda14cbcSMatt Macy 
2023eda14cbcSMatt Macy /*
2024eda14cbcSMatt Macy  * Check that the subtree rooted at this node has a uniform height. Returns
2025eda14cbcSMatt Macy  * the number of nodes under this node, to help verify bt_num_nodes.
2026eda14cbcSMatt Macy  */
2027eda14cbcSMatt Macy static uint64_t
zfs_btree_verify_height_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr,int32_t height)2028eda14cbcSMatt Macy zfs_btree_verify_height_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
2029dbd5678dSMartin Matuska     int32_t height)
2030eda14cbcSMatt Macy {
2031a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(hdr)) {
2032eda14cbcSMatt Macy 		VERIFY0(height);
2033eda14cbcSMatt Macy 		return (1);
2034eda14cbcSMatt Macy 	}
2035eda14cbcSMatt Macy 
2036eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
2037eda14cbcSMatt Macy 	uint64_t ret = 1;
2038a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= hdr->bth_count; i++) {
2039eda14cbcSMatt Macy 		ret += zfs_btree_verify_height_helper(tree,
2040eda14cbcSMatt Macy 		    node->btc_children[i], height - 1);
2041eda14cbcSMatt Macy 	}
2042eda14cbcSMatt Macy 	return (ret);
2043eda14cbcSMatt Macy }
2044eda14cbcSMatt Macy 
2045eda14cbcSMatt Macy /*
2046eda14cbcSMatt Macy  * Check that the tree rooted at this node has a uniform height, and that the
2047eda14cbcSMatt Macy  * bt_height in the tree is correct.
2048eda14cbcSMatt Macy  */
2049eda14cbcSMatt Macy static void
zfs_btree_verify_height(zfs_btree_t * tree)2050eda14cbcSMatt Macy zfs_btree_verify_height(zfs_btree_t *tree)
2051eda14cbcSMatt Macy {
2052eda14cbcSMatt Macy 	EQUIV(tree->bt_height == -1, tree->bt_root == NULL);
2053eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
2054eda14cbcSMatt Macy 		return;
2055eda14cbcSMatt Macy 	}
2056eda14cbcSMatt Macy 
2057eda14cbcSMatt Macy 	VERIFY3U(zfs_btree_verify_height_helper(tree, tree->bt_root,
2058eda14cbcSMatt Macy 	    tree->bt_height), ==, tree->bt_num_nodes);
2059eda14cbcSMatt Macy }
2060eda14cbcSMatt Macy 
2061eda14cbcSMatt Macy /*
2062eda14cbcSMatt Macy  * Check that the elements in this node are sorted, and that if this is a core
2063eda14cbcSMatt Macy  * node, the separators are properly between the subtrees they separaate and
2064eda14cbcSMatt Macy  * that the children also satisfy this requirement.
2065eda14cbcSMatt Macy  */
2066eda14cbcSMatt Macy static void
zfs_btree_verify_order_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)2067eda14cbcSMatt Macy zfs_btree_verify_order_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
2068eda14cbcSMatt Macy {
2069eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
2070a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(hdr)) {
2071eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
2072a0b956f5SMartin Matuska 		for (uint32_t i = 1; i < hdr->bth_count; i++) {
2073a0b956f5SMartin Matuska 			VERIFY3S(tree->bt_compar(leaf->btl_elems +
2074a0b956f5SMartin Matuska 			    (hdr->bth_first + i - 1) * size,
2075a0b956f5SMartin Matuska 			    leaf->btl_elems +
2076a0b956f5SMartin Matuska 			    (hdr->bth_first + i) * size), ==, -1);
2077eda14cbcSMatt Macy 		}
2078eda14cbcSMatt Macy 		return;
2079eda14cbcSMatt Macy 	}
2080eda14cbcSMatt Macy 
2081eda14cbcSMatt Macy 	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
2082a0b956f5SMartin Matuska 	for (uint32_t i = 1; i < hdr->bth_count; i++) {
2083eda14cbcSMatt Macy 		VERIFY3S(tree->bt_compar(node->btc_elems + (i - 1) * size,
2084eda14cbcSMatt Macy 		    node->btc_elems + i * size), ==, -1);
2085eda14cbcSMatt Macy 	}
2086a0b956f5SMartin Matuska 	for (uint32_t i = 0; i < hdr->bth_count; i++) {
2087eda14cbcSMatt Macy 		uint8_t *left_child_last = NULL;
2088eda14cbcSMatt Macy 		zfs_btree_hdr_t *left_child_hdr = node->btc_children[i];
2089a0b956f5SMartin Matuska 		if (zfs_btree_is_core(left_child_hdr)) {
2090eda14cbcSMatt Macy 			zfs_btree_core_t *left_child =
2091eda14cbcSMatt Macy 			    (zfs_btree_core_t *)left_child_hdr;
2092eda14cbcSMatt Macy 			left_child_last = left_child->btc_elems +
2093eda14cbcSMatt Macy 			    (left_child_hdr->bth_count - 1) * size;
2094eda14cbcSMatt Macy 		} else {
2095eda14cbcSMatt Macy 			zfs_btree_leaf_t *left_child =
2096eda14cbcSMatt Macy 			    (zfs_btree_leaf_t *)left_child_hdr;
2097eda14cbcSMatt Macy 			left_child_last = left_child->btl_elems +
2098a0b956f5SMartin Matuska 			    (left_child_hdr->bth_first +
2099a0b956f5SMartin Matuska 			    left_child_hdr->bth_count - 1) * size;
2100eda14cbcSMatt Macy 		}
2101a0b956f5SMartin Matuska 		int comp = tree->bt_compar(node->btc_elems + i * size,
2102a0b956f5SMartin Matuska 		    left_child_last);
2103a0b956f5SMartin Matuska 		if (comp <= 0) {
2104eda14cbcSMatt Macy 			panic("btree: compar returned %d (expected 1) at "
2105a0b956f5SMartin Matuska 			    "%px %d: compar(%px,  %px)", comp, node, i,
2106a0b956f5SMartin Matuska 			    node->btc_elems + i * size, left_child_last);
2107eda14cbcSMatt Macy 		}
2108eda14cbcSMatt Macy 
2109eda14cbcSMatt Macy 		uint8_t *right_child_first = NULL;
2110eda14cbcSMatt Macy 		zfs_btree_hdr_t *right_child_hdr = node->btc_children[i + 1];
2111a0b956f5SMartin Matuska 		if (zfs_btree_is_core(right_child_hdr)) {
2112eda14cbcSMatt Macy 			zfs_btree_core_t *right_child =
2113eda14cbcSMatt Macy 			    (zfs_btree_core_t *)right_child_hdr;
2114eda14cbcSMatt Macy 			right_child_first = right_child->btc_elems;
2115eda14cbcSMatt Macy 		} else {
2116eda14cbcSMatt Macy 			zfs_btree_leaf_t *right_child =
2117eda14cbcSMatt Macy 			    (zfs_btree_leaf_t *)right_child_hdr;
2118a0b956f5SMartin Matuska 			right_child_first = right_child->btl_elems +
2119a0b956f5SMartin Matuska 			    right_child_hdr->bth_first * size;
2120eda14cbcSMatt Macy 		}
2121a0b956f5SMartin Matuska 		comp = tree->bt_compar(node->btc_elems + i * size,
2122a0b956f5SMartin Matuska 		    right_child_first);
2123a0b956f5SMartin Matuska 		if (comp >= 0) {
2124eda14cbcSMatt Macy 			panic("btree: compar returned %d (expected -1) at "
2125a0b956f5SMartin Matuska 			    "%px %d: compar(%px,  %px)", comp, node, i,
2126a0b956f5SMartin Matuska 			    node->btc_elems + i * size, right_child_first);
2127eda14cbcSMatt Macy 		}
2128eda14cbcSMatt Macy 	}
2129a0b956f5SMartin Matuska 	for (uint32_t i = 0; i <= hdr->bth_count; i++)
2130eda14cbcSMatt Macy 		zfs_btree_verify_order_helper(tree, node->btc_children[i]);
2131eda14cbcSMatt Macy }
2132eda14cbcSMatt Macy 
2133eda14cbcSMatt Macy /* Check that all elements in the tree are in sorted order. */
2134eda14cbcSMatt Macy static void
zfs_btree_verify_order(zfs_btree_t * tree)2135eda14cbcSMatt Macy zfs_btree_verify_order(zfs_btree_t *tree)
2136eda14cbcSMatt Macy {
2137eda14cbcSMatt Macy 	EQUIV(tree->bt_height == -1, tree->bt_root == NULL);
2138eda14cbcSMatt Macy 	if (tree->bt_height == -1) {
2139eda14cbcSMatt Macy 		return;
2140eda14cbcSMatt Macy 	}
2141eda14cbcSMatt Macy 
2142eda14cbcSMatt Macy 	zfs_btree_verify_order_helper(tree, tree->bt_root);
2143eda14cbcSMatt Macy }
2144eda14cbcSMatt Macy 
2145eda14cbcSMatt Macy #ifdef ZFS_DEBUG
2146eda14cbcSMatt Macy /* Check that all unused memory is poisoned correctly. */
2147eda14cbcSMatt Macy static void
zfs_btree_verify_poison_helper(zfs_btree_t * tree,zfs_btree_hdr_t * hdr)2148eda14cbcSMatt Macy zfs_btree_verify_poison_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
2149eda14cbcSMatt Macy {
2150eda14cbcSMatt Macy 	size_t size = tree->bt_elem_size;
2151a0b956f5SMartin Matuska 	if (!zfs_btree_is_core(hdr)) {
2152eda14cbcSMatt Macy 		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
2153a0b956f5SMartin Matuska 		for (size_t i = 0; i < hdr->bth_first * size; i++)
2154a0b956f5SMartin Matuska 			VERIFY3U(leaf->btl_elems[i], ==, 0x0f);
2155dbd5678dSMartin Matuska 		size_t esize = tree->bt_leaf_size -
2156dbd5678dSMartin Matuska 		    offsetof(zfs_btree_leaf_t, btl_elems);
2157a0b956f5SMartin Matuska 		for (size_t i = (hdr->bth_first + hdr->bth_count) * size;
2158dbd5678dSMartin Matuska 		    i < esize; i++)
2159a0b956f5SMartin Matuska 			VERIFY3U(leaf->btl_elems[i], ==, 0x0f);
2160eda14cbcSMatt Macy 	} else {
2161eda14cbcSMatt Macy 		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
2162a0b956f5SMartin Matuska 		for (size_t i = hdr->bth_count * size;
2163a0b956f5SMartin Matuska 		    i < BTREE_CORE_ELEMS * size; i++)
2164a0b956f5SMartin Matuska 			VERIFY3U(node->btc_elems[i], ==, 0x0f);
2165eda14cbcSMatt Macy 
2166a0b956f5SMartin Matuska 		for (uint32_t i = hdr->bth_count + 1; i <= BTREE_CORE_ELEMS;
2167a0b956f5SMartin Matuska 		    i++) {
2168eda14cbcSMatt Macy 			VERIFY3P(node->btc_children[i], ==,
2169eda14cbcSMatt Macy 			    (zfs_btree_hdr_t *)BTREE_POISON);
2170eda14cbcSMatt Macy 		}
2171eda14cbcSMatt Macy 
2172a0b956f5SMartin Matuska 		for (uint32_t i = 0; i <= hdr->bth_count; i++) {
2173eda14cbcSMatt Macy 			zfs_btree_verify_poison_helper(tree,
2174eda14cbcSMatt Macy 			    node->btc_children[i]);
2175eda14cbcSMatt Macy 		}
2176eda14cbcSMatt Macy 	}
2177eda14cbcSMatt Macy }
2178eda14cbcSMatt Macy #endif
2179eda14cbcSMatt Macy 
2180eda14cbcSMatt Macy /* Check that unused memory in the tree is still poisoned. */
2181eda14cbcSMatt Macy static void
zfs_btree_verify_poison(zfs_btree_t * tree)2182eda14cbcSMatt Macy zfs_btree_verify_poison(zfs_btree_t *tree)
2183eda14cbcSMatt Macy {
2184eda14cbcSMatt Macy #ifdef ZFS_DEBUG
2185eda14cbcSMatt Macy 	if (tree->bt_height == -1)
2186eda14cbcSMatt Macy 		return;
2187eda14cbcSMatt Macy 	zfs_btree_verify_poison_helper(tree, tree->bt_root);
2188eda14cbcSMatt Macy #endif
2189eda14cbcSMatt Macy }
2190eda14cbcSMatt Macy 
2191eda14cbcSMatt Macy void
zfs_btree_verify(zfs_btree_t * tree)2192eda14cbcSMatt Macy zfs_btree_verify(zfs_btree_t *tree)
2193eda14cbcSMatt Macy {
2194eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity == 0)
2195eda14cbcSMatt Macy 		return;
2196eda14cbcSMatt Macy 	zfs_btree_verify_height(tree);
2197eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity == 1)
2198eda14cbcSMatt Macy 		return;
2199eda14cbcSMatt Macy 	zfs_btree_verify_pointers(tree);
2200eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity == 2)
2201eda14cbcSMatt Macy 		return;
2202eda14cbcSMatt Macy 	zfs_btree_verify_counts(tree);
2203eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity == 3)
2204eda14cbcSMatt Macy 		return;
2205eda14cbcSMatt Macy 	zfs_btree_verify_order(tree);
2206eda14cbcSMatt Macy 
2207eda14cbcSMatt Macy 	if (zfs_btree_verify_intensity == 4)
2208eda14cbcSMatt Macy 		return;
2209eda14cbcSMatt Macy 	zfs_btree_verify_poison(tree);
2210eda14cbcSMatt Macy }
2211c7046f76SMartin Matuska 
2212c7046f76SMartin Matuska ZFS_MODULE_PARAM(zfs, zfs_, btree_verify_intensity, UINT, ZMOD_RW,
2213c7046f76SMartin Matuska 	"Enable btree verification. Levels above 4 require ZFS be built "
2214c7046f76SMartin Matuska 	"with debugging");
2215