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