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