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