Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
/*
 * CDDL HEADER START
 *
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2019 by Delphix. All rights reserved.
 */

#include	<sys/btree.h>
#include	<sys/bitops.h>
#include	<sys/zfs_context.h>

kmem_cache_t *zfs_btree_leaf_cache;

/*
 * Control the extent of the verification that occurs when zfs_btree_verify is
 * called. Primarily used for debugging when extending the btree logic and
 * functionality. As the intensity is increased, new verification steps are
 * added. These steps are cumulative; intensity = 3 includes the intensity = 1
 * and intensity = 2 steps as well.
 *
 * Intensity 1: Verify that the tree's height is consistent throughout.
 * Intensity 2: Verify that a core node's children's parent pointers point
 * to the core node.
 * Intensity 3: Verify that the total number of elements in the tree matches the
 * sum of the number of elements in each node. Also verifies that each node's
 * count obeys the invariants (less than or equal to maximum value, greater than
 * or equal to half the maximum minus one).
 * Intensity 4: Verify that each element compares less than the element
 * immediately after it and greater than the one immediately before it using the
 * comparator function. For core nodes, also checks that each element is greater
 * than the last element in the first of the two nodes it separates, and less
 * than the first element in the second of the two nodes.
 * Intensity 5: Verifies, if ZFS_DEBUG is defined, that all unused memory inside
 * of each node is poisoned appropriately. Note that poisoning always occurs if
 * ZFS_DEBUG is set, so it is safe to set the intensity to 5 during normal
 * operation.
 *
 * Intensity 4 and 5 are particularly expensive to perform; the previous levels
 * are a few memory operations per node, while these levels require multiple
 * operations per element. In addition, when creating large btrees, these
 * operations are called at every step, resulting in extremely slow operation
 * (while the asymptotic complexity of the other steps is the same, the
 * importance of the constant factors cannot be denied).
 */
int zfs_btree_verify_intensity = 0;

/*
 * A convenience function to silence warnings from memmove's return value and
 * change argument order to src, dest.
 */
static void
bmov(const void *src, void *dest, size_t size)
{
	(void) memmove(dest, src, size);
}

#ifdef _ILP32
#define	BTREE_POISON 0xabadb10c
#else
#define	BTREE_POISON 0xabadb10cdeadbeef
#endif

static void
zfs_btree_poison_node(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
#ifdef ZFS_DEBUG
	size_t size = tree->bt_elem_size;
	if (!hdr->bth_core) {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
		(void) memset(leaf->btl_elems + hdr->bth_count * size, 0x0f,
		    BTREE_LEAF_SIZE - sizeof (zfs_btree_hdr_t) -
		    hdr->bth_count * size);
	} else {
		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		for (int i = hdr->bth_count + 1; i <= BTREE_CORE_ELEMS; i++) {
			node->btc_children[i] =
			    (zfs_btree_hdr_t *)BTREE_POISON;
		}
		(void) memset(node->btc_elems + hdr->bth_count * size, 0x0f,
		    (BTREE_CORE_ELEMS - hdr->bth_count) * size);
	}
#endif
}

static inline void
zfs_btree_poison_node_at(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
    uint64_t offset)
{
#ifdef ZFS_DEBUG
	size_t size = tree->bt_elem_size;
	ASSERT3U(offset, >=, hdr->bth_count);
	if (!hdr->bth_core) {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
		(void) memset(leaf->btl_elems + offset * size, 0x0f, size);
	} else {
		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		node->btc_children[offset + 1] =
		    (zfs_btree_hdr_t *)BTREE_POISON;
		(void) memset(node->btc_elems + offset * size, 0x0f, size);
	}
#endif
}

static inline void
zfs_btree_verify_poison_at(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
    uint64_t offset)
{
#ifdef ZFS_DEBUG
	size_t size = tree->bt_elem_size;
	uint8_t eval = 0x0f;
	if (hdr->bth_core) {
		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		zfs_btree_hdr_t *cval = (zfs_btree_hdr_t *)BTREE_POISON;
		VERIFY3P(node->btc_children[offset + 1], ==, cval);
		for (int i = 0; i < size; i++)
			VERIFY3U(node->btc_elems[offset * size + i], ==, eval);
	} else  {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
		for (int i = 0; i < size; i++)
			VERIFY3U(leaf->btl_elems[offset * size + i], ==, eval);
	}
#endif
}

void
zfs_btree_init(void)
{
	zfs_btree_leaf_cache = kmem_cache_create("zfs_btree_leaf_cache",
	    BTREE_LEAF_SIZE, 0, NULL, NULL, NULL, NULL,
	    NULL, 0);
}

void
zfs_btree_fini(void)
{
	kmem_cache_destroy(zfs_btree_leaf_cache);
}

void
zfs_btree_create(zfs_btree_t *tree, int (*compar) (const void *, const void *),
    size_t size)
{
	/*
	 * We need a minimmum of 4 elements so that when we split a node we
	 * always have at least two elements in each node. This simplifies the
	 * logic in zfs_btree_bulk_finish, since it means the last leaf will
	 * always have a left sibling to share with (unless it's the root).
	 */
	ASSERT3U(size, <=, (BTREE_LEAF_SIZE - sizeof (zfs_btree_hdr_t)) / 4);

	bzero(tree, sizeof (*tree));
	tree->bt_compar = compar;
	tree->bt_elem_size = size;
	tree->bt_height = -1;
	tree->bt_bulk = NULL;
}

/*
 * Find value in the array of elements provided. Uses a simple binary search.
 */
static void *
zfs_btree_find_in_buf(zfs_btree_t *tree, uint8_t *buf, uint64_t nelems,
    const void *value, zfs_btree_index_t *where)
{
	uint64_t max = nelems;
	uint64_t min = 0;
	while (max > min) {
		uint64_t idx = (min + max) / 2;
		uint8_t *cur = buf + idx * tree->bt_elem_size;
		int comp = tree->bt_compar(cur, value);
		if (comp == -1) {
			min = idx + 1;
		} else if (comp == 1) {
			max = idx;
		} else {
			ASSERT0(comp);
			where->bti_offset = idx;
			where->bti_before = B_FALSE;
			return (cur);
		}
	}

	where->bti_offset = max;
	where->bti_before = B_TRUE;
	return (NULL);
}

/*
 * Find the given value in the tree. where may be passed as null to use as a
 * membership test or if the btree is being used as a map.
 */
void *
zfs_btree_find(zfs_btree_t *tree, const void *value, zfs_btree_index_t *where)
{
	if (tree->bt_height == -1) {
		if (where != NULL) {
			where->bti_node = NULL;
			where->bti_offset = 0;
		}
		ASSERT0(tree->bt_num_elems);
		return (NULL);
	}

	/*
	 * If we're in bulk-insert mode, we check the last spot in the tree
	 * and the last leaf in the tree before doing the normal search,
	 * because for most workloads the vast majority of finds in
	 * bulk-insert mode are to insert new elements.
	 */
	zfs_btree_index_t idx;
	if (tree->bt_bulk != NULL) {
		zfs_btree_leaf_t *last_leaf = tree->bt_bulk;
		int compar = tree->bt_compar(last_leaf->btl_elems +
		    ((last_leaf->btl_hdr.bth_count - 1) * tree->bt_elem_size),
		    value);
		if (compar < 0) {
			/*
			 * If what they're looking for is after the last
			 * element, it's not in the tree.
			 */
			if (where != NULL) {
				where->bti_node = (zfs_btree_hdr_t *)last_leaf;
				where->bti_offset =
				    last_leaf->btl_hdr.bth_count;
				where->bti_before = B_TRUE;
			}
			return (NULL);
		} else if (compar == 0) {
			if (where != NULL) {
				where->bti_node = (zfs_btree_hdr_t *)last_leaf;
				where->bti_offset =
				    last_leaf->btl_hdr.bth_count - 1;
				where->bti_before = B_FALSE;
			}
			return (last_leaf->btl_elems +
			    ((last_leaf->btl_hdr.bth_count - 1) *
			    tree->bt_elem_size));
		}
		if (tree->bt_compar(last_leaf->btl_elems, value) <= 0) {
			/*
			 * If what they're looking for is after the first
			 * element in the last leaf, it's in the last leaf or
			 * it's not in the tree.
			 */
			void *d = zfs_btree_find_in_buf(tree,
			    last_leaf->btl_elems, last_leaf->btl_hdr.bth_count,
			    value, &idx);

			if (where != NULL) {
				idx.bti_node = (zfs_btree_hdr_t *)last_leaf;
				*where = idx;
			}
			return (d);
		}
	}

	zfs_btree_core_t *node = NULL;
	uint64_t child = 0;
	uint64_t depth = 0;

	/*
	 * Iterate down the tree, finding which child the value should be in
	 * by comparing with the separators.
	 */
	for (node = (zfs_btree_core_t *)tree->bt_root; depth < tree->bt_height;
	    node = (zfs_btree_core_t *)node->btc_children[child], depth++) {
		ASSERT3P(node, !=, NULL);
		void *d = zfs_btree_find_in_buf(tree, node->btc_elems,
		    node->btc_hdr.bth_count, value, &idx);
		EQUIV(d != NULL, !idx.bti_before);
		if (d != NULL) {
			if (where != NULL) {
				idx.bti_node = (zfs_btree_hdr_t *)node;
				*where = idx;
			}
			return (d);
		}
		ASSERT(idx.bti_before);
		child = idx.bti_offset;
	}

	/*
	 * The value is in this leaf, or it would be if it were in the
	 * tree. Find its proper location and return it.
	 */
	zfs_btree_leaf_t *leaf = (depth == 0 ?
	    (zfs_btree_leaf_t *)tree->bt_root : (zfs_btree_leaf_t *)node);
	void *d = zfs_btree_find_in_buf(tree, leaf->btl_elems,
	    leaf->btl_hdr.bth_count, value, &idx);

	if (where != NULL) {
		idx.bti_node = (zfs_btree_hdr_t *)leaf;
		*where = idx;
	}

	return (d);
}

/*
 * To explain the following functions, it is useful to understand the four
 * kinds of shifts used in btree operation. First, a shift is a movement of
 * elements within a node. It is used to create gaps for inserting new
 * elements and children, or cover gaps created when things are removed. A
 * shift has two fundamental properties, each of which can be one of two
 * values, making four types of shifts.  There is the direction of the shift
 * (left or right) and the shape of the shift (parallelogram or isoceles
 * trapezoid (shortened to trapezoid hereafter)). The shape distinction only
 * applies to shifts of core nodes.
 *
 * The names derive from the following imagining of the layout of a node:
 *
 *  Elements:       *   *   *   *   *   *   *   ...   *   *   *
 *  Children:     *   *   *   *   *   *   *   *   ...   *   *   *
 *
 * This layout follows from the fact that the elements act as separators
 * between pairs of children, and that children root subtrees "below" the
 * current node. A left and right shift are fairly self-explanatory; a left
 * shift moves things to the left, while a right shift moves things to the
 * right. A parallelogram shift is a shift with the same number of elements
 * and children being moved, while a trapezoid shift is a shift that moves one
 * more children than elements. An example follows:
 *
 * A parallelogram shift could contain the following:
 *      _______________
 *      \*   *   *   * \ *   *   *   ...   *   *   *
 *     * \ *   *   *   *\  *   *   *   ...   *   *   *
 *        ---------------
 * A trapezoid shift could contain the following:
 *          ___________
 *       * / *   *   * \ *   *   *   ...   *   *   *
 *     *  / *  *   *   *\  *   *   *   ...   *   *   *
 *        ---------------
 *
 * Note that a parallelogram shift is always shaped like a "left-leaning"
 * parallelogram, where the starting index of the children being moved is
 * always one higher than the starting index of the elements being moved. No
 * "right-leaning" parallelogram shifts are needed (shifts where the starting
 * element index and starting child index being moved are the same) to achieve
 * any btree operations, so we ignore them.
 */

enum bt_shift_shape {
	BSS_TRAPEZOID,
	BSS_PARALLELOGRAM
};

enum bt_shift_direction {
	BSD_LEFT,
	BSD_RIGHT
};

/*
 * Shift elements and children in the provided core node by off spots.  The
 * first element moved is idx, and count elements are moved. The shape of the
 * shift is determined by shape. The direction is determined by dir.
 */
static inline void
bt_shift_core(zfs_btree_t *tree, zfs_btree_core_t *node, uint64_t idx,
    uint64_t count, uint64_t off, enum bt_shift_shape shape,
    enum bt_shift_direction dir)
{
	size_t size = tree->bt_elem_size;
	ASSERT(node->btc_hdr.bth_core);

	uint8_t *e_start = node->btc_elems + idx * size;
	int sign = (dir == BSD_LEFT ? -1 : +1);
	uint8_t *e_out = e_start + sign * off * size;
	uint64_t e_count = count;
	bmov(e_start, e_out, e_count * size);

	zfs_btree_hdr_t **c_start = node->btc_children + idx +
	    (shape == BSS_TRAPEZOID ? 0 : 1);
	zfs_btree_hdr_t **c_out = (dir == BSD_LEFT ? c_start - off :
	    c_start + off);
	uint64_t c_count = count + (shape == BSS_TRAPEZOID ? 1 : 0);
	bmov(c_start, c_out, c_count * sizeof (*c_start));
}

/*
 * Shift elements and children in the provided core node left by one spot.
 * The first element moved is idx, and count elements are moved. The
 * shape of the shift is determined by trap; true if the shift is a trapezoid,
 * false if it is a parallelogram.
 */
static inline void
bt_shift_core_left(zfs_btree_t *tree, zfs_btree_core_t *node, uint64_t idx,
    uint64_t count, enum bt_shift_shape shape)
{
	bt_shift_core(tree, node, idx, count, 1, shape, BSD_LEFT);
}

/*
 * Shift elements and children in the provided core node right by one spot.
 * Starts with elements[idx] and children[idx] and one more child than element.
 */
static inline void
bt_shift_core_right(zfs_btree_t *tree, zfs_btree_core_t *node, uint64_t idx,
    uint64_t count, enum bt_shift_shape shape)
{
	bt_shift_core(tree, node, idx, count, 1, shape, BSD_RIGHT);
}

/*
 * Shift elements and children in the provided leaf node by off spots.
 * The first element moved is idx, and count elements are moved. The direction
 * is determined by left.
 */
static inline void
bt_shift_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *node, uint64_t idx,
    uint64_t count, uint64_t off, enum bt_shift_direction dir)
{
	size_t size = tree->bt_elem_size;
	ASSERT(!node->btl_hdr.bth_core);

	uint8_t *start = node->btl_elems + idx * size;
	int sign = (dir == BSD_LEFT ? -1 : +1);
	uint8_t *out = start + sign * off * size;
	bmov(start, out, count * size);
}

static inline void
bt_shift_leaf_right(zfs_btree_t *tree, zfs_btree_leaf_t *leaf, uint64_t idx,
    uint64_t count)
{
	bt_shift_leaf(tree, leaf, idx, count, 1, BSD_RIGHT);
}

static inline void
bt_shift_leaf_left(zfs_btree_t *tree, zfs_btree_leaf_t *leaf, uint64_t idx,
    uint64_t count)
{
	bt_shift_leaf(tree, leaf, idx, count, 1, BSD_LEFT);
}

/*
 * Move children and elements from one core node to another. The shape
 * parameter behaves the same as it does in the shift logic.
 */
static inline void
bt_transfer_core(zfs_btree_t *tree, zfs_btree_core_t *source, uint64_t sidx,
    uint64_t count, zfs_btree_core_t *dest, uint64_t didx,
    enum bt_shift_shape shape)
{
	size_t size = tree->bt_elem_size;
	ASSERT(source->btc_hdr.bth_core);
	ASSERT(dest->btc_hdr.bth_core);

	bmov(source->btc_elems + sidx * size, dest->btc_elems + didx * size,
	    count * size);

	uint64_t c_count = count + (shape == BSS_TRAPEZOID ? 1 : 0);
	bmov(source->btc_children + sidx + (shape == BSS_TRAPEZOID ? 0 : 1),
	    dest->btc_children + didx + (shape == BSS_TRAPEZOID ? 0 : 1),
	    c_count * sizeof (*source->btc_children));
}

static inline void
bt_transfer_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *source, uint64_t sidx,
    uint64_t count, zfs_btree_leaf_t *dest, uint64_t didx)
{
	size_t size = tree->bt_elem_size;
	ASSERT(!source->btl_hdr.bth_core);
	ASSERT(!dest->btl_hdr.bth_core);

	bmov(source->btl_elems + sidx * size, dest->btl_elems + didx * size,
	    count * size);
}

/*
 * Find the first element in the subtree rooted at hdr, return its value and
 * put its location in where if non-null.
 */
static void *
zfs_btree_first_helper(zfs_btree_hdr_t *hdr, zfs_btree_index_t *where)
{
	zfs_btree_hdr_t *node;

	for (node = hdr; node->bth_core; node =
	    ((zfs_btree_core_t *)node)->btc_children[0])
		;

	ASSERT(!node->bth_core);
	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)node;
	if (where != NULL) {
		where->bti_node = node;
		where->bti_offset = 0;
		where->bti_before = B_FALSE;
	}
	return (&leaf->btl_elems[0]);
}

/* Insert an element and a child into a core node at the given offset. */
static void
zfs_btree_insert_core_impl(zfs_btree_t *tree, zfs_btree_core_t *parent,
    uint64_t offset, zfs_btree_hdr_t *new_node, void *buf)
{
	uint64_t size = tree->bt_elem_size;
	zfs_btree_hdr_t *par_hdr = &parent->btc_hdr;
	ASSERT3P(par_hdr, ==, new_node->bth_parent);
	ASSERT3U(par_hdr->bth_count, <, BTREE_CORE_ELEMS);

	if (zfs_btree_verify_intensity >= 5) {
		zfs_btree_verify_poison_at(tree, par_hdr,
		    par_hdr->bth_count);
	}
	/* Shift existing elements and children */
	uint64_t count = par_hdr->bth_count - offset;
	bt_shift_core_right(tree, parent, offset, count,
	    BSS_PARALLELOGRAM);

	/* Insert new values */
	parent->btc_children[offset + 1] = new_node;
	bmov(buf, parent->btc_elems + offset * size, size);
	par_hdr->bth_count++;
}

/*
 * Insert new_node into the parent of old_node directly after old_node, with
 * buf as the dividing element between the two.
 */
static void
zfs_btree_insert_into_parent(zfs_btree_t *tree, zfs_btree_hdr_t *old_node,
    zfs_btree_hdr_t *new_node, void *buf)
{
	ASSERT3P(old_node->bth_parent, ==, new_node->bth_parent);
	uint64_t size = tree->bt_elem_size;
	zfs_btree_core_t *parent = old_node->bth_parent;
	zfs_btree_hdr_t *par_hdr = &parent->btc_hdr;

	/*
	 * If this is the root node we were splitting, we create a new root
	 * and increase the height of the tree.
	 */
	if (parent == NULL) {
		ASSERT3P(old_node, ==, tree->bt_root);
		tree->bt_num_nodes++;
		zfs_btree_core_t *new_root =
		    kmem_alloc(sizeof (zfs_btree_core_t) + BTREE_CORE_ELEMS *
		    size, KM_SLEEP);
		zfs_btree_hdr_t *new_root_hdr = &new_root->btc_hdr;
		new_root_hdr->bth_parent = NULL;
		new_root_hdr->bth_core = B_TRUE;
		new_root_hdr->bth_count = 1;

		old_node->bth_parent = new_node->bth_parent = new_root;
		new_root->btc_children[0] = old_node;
		new_root->btc_children[1] = new_node;
		bmov(buf, new_root->btc_elems, size);

		tree->bt_height++;
		tree->bt_root = new_root_hdr;
		zfs_btree_poison_node(tree, new_root_hdr);
		return;
	}

	/*
	 * Since we have the new separator, binary search for where to put
	 * new_node.
	 */
	zfs_btree_index_t idx;
	ASSERT(par_hdr->bth_core);
	VERIFY3P(zfs_btree_find_in_buf(tree, parent->btc_elems,
	    par_hdr->bth_count, buf, &idx), ==, NULL);
	ASSERT(idx.bti_before);
	uint64_t offset = idx.bti_offset;
	ASSERT3U(offset, <=, par_hdr->bth_count);
	ASSERT3P(parent->btc_children[offset], ==, old_node);

	/*
	 * If the parent isn't full, shift things to accommodate our insertions
	 * and return.
	 */
	if (par_hdr->bth_count != BTREE_CORE_ELEMS) {
		zfs_btree_insert_core_impl(tree, parent, offset, new_node, buf);
		return;
	}

	/*
	 * We need to split this core node into two. Currently there are
	 * BTREE_CORE_ELEMS + 1 child nodes, and we are adding one for
	 * BTREE_CORE_ELEMS + 2. Some of the children will be part of the
	 * current node, and the others will be moved to the new core node.
	 * There are BTREE_CORE_ELEMS + 1 elements including the new one. One
	 * will be used as the new separator in our parent, and the others
	 * will be split among the two core nodes.
	 *
	 * Usually we will split the node in half evenly, with
	 * BTREE_CORE_ELEMS/2 elements in each node. If we're bulk loading, we
	 * instead move only about a quarter of the elements (and children) to
	 * the new node. Since the average state after a long time is a 3/4
	 * full node, shortcutting directly to that state improves efficiency.
	 *
	 * We do this in two stages: first we split into two nodes, and then we
	 * reuse our existing logic to insert the new element and child.
	 */
	uint64_t move_count = MAX((BTREE_CORE_ELEMS / (tree->bt_bulk == NULL ?
	    2 : 4)) - 1, 2);
	uint64_t keep_count = BTREE_CORE_ELEMS - move_count - 1;
	ASSERT3U(BTREE_CORE_ELEMS - move_count, >=, 2);
	tree->bt_num_nodes++;
	zfs_btree_core_t *new_parent = kmem_alloc(sizeof (zfs_btree_core_t) +
	    BTREE_CORE_ELEMS * size, KM_SLEEP);
	zfs_btree_hdr_t *new_par_hdr = &new_parent->btc_hdr;
	new_par_hdr->bth_parent = par_hdr->bth_parent;
	new_par_hdr->bth_core = B_TRUE;
	new_par_hdr->bth_count = move_count;
	zfs_btree_poison_node(tree, new_par_hdr);

	par_hdr->bth_count = keep_count;

	bt_transfer_core(tree, parent, keep_count + 1, move_count, new_parent,
	    0, BSS_TRAPEZOID);

	/* Store the new separator in a buffer. */
	uint8_t *tmp_buf = kmem_alloc(size, KM_SLEEP);
	bmov(parent->btc_elems + keep_count * size, tmp_buf,
	    size);
	zfs_btree_poison_node(tree, par_hdr);

	if (offset < keep_count) {
		/* Insert the new node into the left half */
		zfs_btree_insert_core_impl(tree, parent, offset, new_node,
		    buf);

		/*
		 * Move the new separator to the existing buffer.
		 */
		bmov(tmp_buf, buf, size);
	} else if (offset > keep_count) {
		/* Insert the new node into the right half */
		new_node->bth_parent = new_parent;
		zfs_btree_insert_core_impl(tree, new_parent,
		    offset - keep_count - 1, new_node, buf);

		/*
		 * Move the new separator to the existing buffer.
		 */
		bmov(tmp_buf, buf, size);
	} else {
		/*
		 * Move the new separator into the right half, and replace it
		 * with buf. We also need to shift back the elements in the
		 * right half to accommodate new_node.
		 */
		bt_shift_core_right(tree, new_parent, 0, move_count,
		    BSS_TRAPEZOID);
		new_parent->btc_children[0] = new_node;
		bmov(tmp_buf, new_parent->btc_elems, size);
		new_par_hdr->bth_count++;
	}
	kmem_free(tmp_buf, size);
	zfs_btree_poison_node(tree, par_hdr);

	for (int i = 0; i <= new_parent->btc_hdr.bth_count; i++)
		new_parent->btc_children[i]->bth_parent = new_parent;

	for (int i = 0; i <= parent->btc_hdr.bth_count; i++)
		ASSERT3P(parent->btc_children[i]->bth_parent, ==, parent);

	/*
	 * Now that the node is split, we need to insert the new node into its
	 * parent. This may cause further splitting.
	 */
	zfs_btree_insert_into_parent(tree, &parent->btc_hdr,
	    &new_parent->btc_hdr, buf);
}

/* Insert an element into a leaf node at the given offset. */
static void
zfs_btree_insert_leaf_impl(zfs_btree_t *tree, zfs_btree_leaf_t *leaf,
    uint64_t idx, const void *value)
{
	uint64_t size = tree->bt_elem_size;
	uint8_t *start = leaf->btl_elems + (idx * size);
	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
	uint64_t capacity __maybe_unused = P2ALIGN((BTREE_LEAF_SIZE -
	    sizeof (zfs_btree_hdr_t)) / size, 2);
	uint64_t count = leaf->btl_hdr.bth_count - idx;
	ASSERT3U(leaf->btl_hdr.bth_count, <, capacity);

	if (zfs_btree_verify_intensity >= 5) {
		zfs_btree_verify_poison_at(tree, &leaf->btl_hdr,
		    leaf->btl_hdr.bth_count);
	}

	bt_shift_leaf_right(tree, leaf, idx, count);
	bmov(value, start, size);
	hdr->bth_count++;
}

/* Helper function for inserting a new value into leaf at the given index. */
static void
zfs_btree_insert_into_leaf(zfs_btree_t *tree, zfs_btree_leaf_t *leaf,
    const void *value, uint64_t idx)
{
	uint64_t size = tree->bt_elem_size;
	uint64_t capacity = P2ALIGN((BTREE_LEAF_SIZE -
	    sizeof (zfs_btree_hdr_t)) / size, 2);

	/*
	 * If the leaf isn't full, shift the elements after idx and insert
	 * value.
	 */
	if (leaf->btl_hdr.bth_count != capacity) {
		zfs_btree_insert_leaf_impl(tree, leaf, idx, value);
		return;
	}

	/*
	 * Otherwise, we split the leaf node into two nodes. If we're not bulk
	 * inserting, each is of size (capacity / 2).  If we are bulk
	 * inserting, we move a quarter of the elements to the new node so
	 * inserts into the old node don't cause immediate splitting but the
	 * tree stays relatively dense. Since the average state after a long
	 * time is a 3/4 full node, shortcutting directly to that state
	 * improves efficiency.  At the end of the bulk insertion process
	 * we'll need to go through and fix up any nodes (the last leaf and
	 * its ancestors, potentially) that are below the minimum.
	 *
	 * In either case, we're left with one extra element. The leftover
	 * element will become the new dividing element between the two nodes.
	 */
	uint64_t move_count = MAX(capacity / (tree->bt_bulk == NULL ? 2 : 4) -
	    1, 2);
	uint64_t keep_count = capacity - move_count - 1;
	ASSERT3U(capacity - move_count, >=, 2);
	tree->bt_num_nodes++;
	zfs_btree_leaf_t *new_leaf = kmem_cache_alloc(zfs_btree_leaf_cache,
	    KM_SLEEP);
	zfs_btree_hdr_t *new_hdr = &new_leaf->btl_hdr;
	new_hdr->bth_parent = leaf->btl_hdr.bth_parent;
	new_hdr->bth_core = B_FALSE;
	new_hdr->bth_count = move_count;
	zfs_btree_poison_node(tree, new_hdr);

	leaf->btl_hdr.bth_count = keep_count;

	if (tree->bt_bulk != NULL && leaf == tree->bt_bulk)
		tree->bt_bulk = new_leaf;

	/* Copy the back part to the new leaf. */
	bt_transfer_leaf(tree, leaf, keep_count + 1, move_count, new_leaf,
	    0);

	/* We store the new separator in a buffer we control for simplicity. */
	uint8_t *buf = kmem_alloc(size, KM_SLEEP);
	bmov(leaf->btl_elems + (keep_count * size), buf, size);
	zfs_btree_poison_node(tree, &leaf->btl_hdr);

	if (idx < keep_count) {
		/* Insert into the existing leaf. */
		zfs_btree_insert_leaf_impl(tree, leaf, idx, value);
	} else if (idx > keep_count) {
		/* Insert into the new leaf. */
		zfs_btree_insert_leaf_impl(tree, new_leaf, idx - keep_count -
		    1, value);
	} else {
		/*
		 * Shift the elements in the new leaf to make room for the
		 * separator, and use the new value as the new separator.
		 */
		bt_shift_leaf_right(tree, new_leaf, 0, move_count);
		bmov(buf, new_leaf->btl_elems, size);
		bmov(value, buf, size);
		new_hdr->bth_count++;
	}

	/*
	 * Now that the node is split, we need to insert the new node into its
	 * parent. This may cause further splitting, bur only of core nodes.
	 */
	zfs_btree_insert_into_parent(tree, &leaf->btl_hdr, &new_leaf->btl_hdr,
	    buf);
	kmem_free(buf, size);
}

static uint64_t
zfs_btree_find_parent_idx(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	void *buf;
	if (hdr->bth_core) {
		buf = ((zfs_btree_core_t *)hdr)->btc_elems;
	} else {
		buf = ((zfs_btree_leaf_t *)hdr)->btl_elems;
	}
	zfs_btree_index_t idx;
	zfs_btree_core_t *parent = hdr->bth_parent;
	VERIFY3P(zfs_btree_find_in_buf(tree, parent->btc_elems,
	    parent->btc_hdr.bth_count, buf, &idx), ==, NULL);
	ASSERT(idx.bti_before);
	ASSERT3U(idx.bti_offset, <=, parent->btc_hdr.bth_count);
	ASSERT3P(parent->btc_children[idx.bti_offset], ==, hdr);
	return (idx.bti_offset);
}

/*
 * Take the b-tree out of bulk insert mode. During bulk-insert mode, some
 * nodes may violate the invariant that non-root nodes must be at least half
 * full. All nodes violating this invariant should be the last node in their
 * particular level. To correct the invariant, we take values from their left
 * neighbor until they are half full. They must have a left neighbor at their
 * level because the last node at a level is not the first node unless it's
 * the root.
 */
static void
zfs_btree_bulk_finish(zfs_btree_t *tree)
{
	ASSERT3P(tree->bt_bulk, !=, NULL);
	ASSERT3P(tree->bt_root, !=, NULL);
	zfs_btree_leaf_t *leaf = tree->bt_bulk;
	zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
	zfs_btree_core_t *parent = hdr->bth_parent;
	uint64_t size = tree->bt_elem_size;
	uint64_t capacity = P2ALIGN((BTREE_LEAF_SIZE -
	    sizeof (zfs_btree_hdr_t)) / size, 2);

	/*
	 * The invariant doesn't apply to the root node, if that's the only
	 * node in the tree we're done.
	 */
	if (parent == NULL) {
		tree->bt_bulk = NULL;
		return;
	}

	/* First, take elements to rebalance the leaf node. */
	if (hdr->bth_count < capacity / 2) {
		/*
		 * First, find the left neighbor. The simplest way to do this
		 * is to call zfs_btree_prev twice; the first time finds some
		 * ancestor of this node, and the second time finds the left
		 * neighbor. The ancestor found is the lowest common ancestor
		 * of leaf and the neighbor.
		 */
		zfs_btree_index_t idx = {
			.bti_node = hdr,
			.bti_offset = 0
		};
		VERIFY3P(zfs_btree_prev(tree, &idx, &idx), !=, NULL);
		ASSERT(idx.bti_node->bth_core);
		zfs_btree_core_t *common = (zfs_btree_core_t *)idx.bti_node;
		uint64_t common_idx = idx.bti_offset;

		VERIFY3P(zfs_btree_prev(tree, &idx, &idx), !=, NULL);
		ASSERT(!idx.bti_node->bth_core);
		zfs_btree_leaf_t *l_neighbor = (zfs_btree_leaf_t *)idx.bti_node;
		zfs_btree_hdr_t *l_hdr = idx.bti_node;
		uint64_t move_count = (capacity / 2) - hdr->bth_count;
		ASSERT3U(l_neighbor->btl_hdr.bth_count - move_count, >=,
		    capacity / 2);

		if (zfs_btree_verify_intensity >= 5) {
			for (int i = 0; i < move_count; i++) {
				zfs_btree_verify_poison_at(tree, hdr,
				    leaf->btl_hdr.bth_count + i);
			}
		}

		/* First, shift elements in leaf back. */
		bt_shift_leaf(tree, leaf, 0, hdr->bth_count, move_count,
		    BSD_RIGHT);

		/* Next, move the separator from the common ancestor to leaf. */
		uint8_t *separator = common->btc_elems + (common_idx * size);
		uint8_t *out = leaf->btl_elems + ((move_count - 1) * size);
		bmov(separator, out, size);
		move_count--;

		/*
		 * Now we move elements from the tail of the left neighbor to
		 * fill the remaining spots in leaf.
		 */
		bt_transfer_leaf(tree, l_neighbor, l_hdr->bth_count -
		    move_count, move_count, leaf, 0);

		/*
		 * Finally, move the new last element in the left neighbor to
		 * the separator.
		 */
		bmov(l_neighbor->btl_elems + (l_hdr->bth_count -
		    move_count - 1) * size, separator, size);

		/* Adjust the node's counts, and we're done. */
		l_hdr->bth_count -= move_count + 1;
		hdr->bth_count += move_count + 1;

		ASSERT3U(l_hdr->bth_count, >=, capacity / 2);
		ASSERT3U(hdr->bth_count, >=, capacity / 2);
		zfs_btree_poison_node(tree, l_hdr);
	}

	/*
	 * Now we have to rebalance any ancestors of leaf that may also
	 * violate the invariant.
	 */
	capacity = BTREE_CORE_ELEMS;
	while (parent->btc_hdr.bth_parent != NULL) {
		zfs_btree_core_t *cur = parent;
		zfs_btree_hdr_t *hdr = &cur->btc_hdr;
		parent = hdr->bth_parent;
		/*
		 * If the invariant isn't violated, move on to the next
		 * ancestor.
		 */
		if (hdr->bth_count >= capacity / 2)
			continue;

		/*
		 * Because the smallest number of nodes we can move when
		 * splitting is 2, we never need to worry about not having a
		 * left sibling (a sibling is a neighbor with the same parent).
		 */
		uint64_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);
		ASSERT3U(parent_idx, >, 0);
		zfs_btree_core_t *l_neighbor =
		    (zfs_btree_core_t *)parent->btc_children[parent_idx - 1];
		uint64_t move_count = (capacity / 2) - hdr->bth_count;
		ASSERT3U(l_neighbor->btc_hdr.bth_count - move_count, >=,
		    capacity / 2);

		if (zfs_btree_verify_intensity >= 5) {
			for (int i = 0; i < move_count; i++) {
				zfs_btree_verify_poison_at(tree, hdr,
				    hdr->bth_count + i);
			}
		}
		/* First, shift things in the right node back. */
		bt_shift_core(tree, cur, 0, hdr->bth_count, move_count,
		    BSS_TRAPEZOID, BSD_RIGHT);

		/* Next, move the separator to the right node. */
		uint8_t *separator = parent->btc_elems + ((parent_idx - 1) *
		    size);
		uint8_t *e_out = cur->btc_elems + ((move_count - 1) * size);
		bmov(separator, e_out, size);

		/*
		 * Now, move elements and children from the left node to the
		 * right.  We move one more child than elements.
		 */
		move_count--;
		uint64_t move_idx = l_neighbor->btc_hdr.bth_count - move_count;
		bt_transfer_core(tree, l_neighbor, move_idx, move_count, cur, 0,
		    BSS_TRAPEZOID);

		/*
		 * Finally, move the last element in the left node to the
		 * separator's position.
		 */
		move_idx--;
		bmov(l_neighbor->btc_elems + move_idx * size, separator, size);

		l_neighbor->btc_hdr.bth_count -= move_count + 1;
		hdr->bth_count += move_count + 1;

		ASSERT3U(l_neighbor->btc_hdr.bth_count, >=, capacity / 2);
		ASSERT3U(hdr->bth_count, >=, capacity / 2);

		zfs_btree_poison_node(tree, &l_neighbor->btc_hdr);

		for (int i = 0; i <= hdr->bth_count; i++)
			cur->btc_children[i]->bth_parent = cur;
	}

	tree->bt_bulk = NULL;
}

/*
 * Insert value into tree at the location specified by where.
 */
void
zfs_btree_add_idx(zfs_btree_t *tree, const void *value,
    const zfs_btree_index_t *where)
{
	zfs_btree_index_t idx = {0};

	/* If we're not inserting in the last leaf, end bulk insert mode. */
	if (tree->bt_bulk != NULL) {
		if (where->bti_node != &tree->bt_bulk->btl_hdr) {
			zfs_btree_bulk_finish(tree);
			VERIFY3P(zfs_btree_find(tree, value, &idx), ==, NULL);
			where = &idx;
		}
	}

	tree->bt_num_elems++;
	/*
	 * If this is the first element in the tree, create a leaf root node
	 * and add the value to it.
	 */
	if (where->bti_node == NULL) {
		ASSERT3U(tree->bt_num_elems, ==, 1);
		ASSERT3S(tree->bt_height, ==, -1);
		ASSERT3P(tree->bt_root, ==, NULL);
		ASSERT0(where->bti_offset);

		tree->bt_num_nodes++;
		zfs_btree_leaf_t *leaf = kmem_cache_alloc(zfs_btree_leaf_cache,
		    KM_SLEEP);
		tree->bt_root = &leaf->btl_hdr;
		tree->bt_height++;

		zfs_btree_hdr_t *hdr = &leaf->btl_hdr;
		hdr->bth_parent = NULL;
		hdr->bth_core = B_FALSE;
		hdr->bth_count = 0;
		zfs_btree_poison_node(tree, hdr);

		zfs_btree_insert_into_leaf(tree, leaf, value, 0);
		tree->bt_bulk = leaf;
	} else if (!where->bti_node->bth_core) {
		/*
		 * If we're inserting into a leaf, go directly to the helper
		 * function.
		 */
		zfs_btree_insert_into_leaf(tree,
		    (zfs_btree_leaf_t *)where->bti_node, value,
		    where->bti_offset);
	} else {
		/*
		 * If we're inserting into a core node, we can't just shift
		 * the existing element in that slot in the same node without
		 * breaking our ordering invariants. Instead we place the new
		 * value in the node at that spot and then insert the old
		 * separator into the first slot in the subtree to the right.
		 */
		ASSERT(where->bti_node->bth_core);
		zfs_btree_core_t *node = (zfs_btree_core_t *)where->bti_node;

		/*
		 * We can ignore bti_before, because either way the value
		 * should end up in bti_offset.
		 */
		uint64_t off = where->bti_offset;
		zfs_btree_hdr_t *subtree = node->btc_children[off + 1];
		size_t size = tree->bt_elem_size;
		uint8_t *buf = kmem_alloc(size, KM_SLEEP);
		bmov(node->btc_elems + off * size, buf, size);
		bmov(value, node->btc_elems + off * size, size);

		/*
		 * Find the first slot in the subtree to the right, insert
		 * there.
		 */
		zfs_btree_index_t new_idx;
		VERIFY3P(zfs_btree_first_helper(subtree, &new_idx), !=, NULL);
		ASSERT0(new_idx.bti_offset);
		ASSERT(!new_idx.bti_node->bth_core);
		zfs_btree_insert_into_leaf(tree,
		    (zfs_btree_leaf_t *)new_idx.bti_node, buf, 0);
		kmem_free(buf, size);
	}
	zfs_btree_verify(tree);
}

/*
 * Return the first element in the tree, and put its location in where if
 * non-null.
 */
void *
zfs_btree_first(zfs_btree_t *tree, zfs_btree_index_t *where)
{
	if (tree->bt_height == -1) {
		ASSERT0(tree->bt_num_elems);
		return (NULL);
	}
	return (zfs_btree_first_helper(tree->bt_root, where));
}

/*
 * Find the last element in the subtree rooted at hdr, return its value and
 * put its location in where if non-null.
 */
static void *
zfs_btree_last_helper(zfs_btree_t *btree, zfs_btree_hdr_t *hdr,
    zfs_btree_index_t *where)
{
	zfs_btree_hdr_t *node;

	for (node = hdr; node->bth_core; node =
	    ((zfs_btree_core_t *)node)->btc_children[node->bth_count])
		;

	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)node;
	if (where != NULL) {
		where->bti_node = node;
		where->bti_offset = node->bth_count - 1;
		where->bti_before = B_FALSE;
	}
	return (leaf->btl_elems + (node->bth_count - 1) * btree->bt_elem_size);
}

/*
 * Return the last element in the tree, and put its location in where if
 * non-null.
 */
void *
zfs_btree_last(zfs_btree_t *tree, zfs_btree_index_t *where)
{
	if (tree->bt_height == -1) {
		ASSERT0(tree->bt_num_elems);
		return (NULL);
	}
	return (zfs_btree_last_helper(tree, tree->bt_root, where));
}

/*
 * This function contains the logic to find the next node in the tree. A
 * helper function is used because there are multiple internal consumemrs of
 * this logic. The done_func is used by zfs_btree_destroy_nodes to clean up each
 * node after we've finished with it.
 */
static void *
zfs_btree_next_helper(zfs_btree_t *tree, const zfs_btree_index_t *idx,
    zfs_btree_index_t *out_idx,
    void (*done_func)(zfs_btree_t *, zfs_btree_hdr_t *))
{
	if (idx->bti_node == NULL) {
		ASSERT3S(tree->bt_height, ==, -1);
		return (NULL);
	}

	uint64_t offset = idx->bti_offset;
	if (!idx->bti_node->bth_core) {
		/*
		 * When finding the next element of an element in a leaf,
		 * there are two cases. If the element isn't the last one in
		 * the leaf, in which case we just return the next element in
		 * the leaf. Otherwise, we need to traverse up our parents
		 * until we find one where our ancestor isn't the last child
		 * of its parent. Once we do, the next element is the
		 * separator after our ancestor in its parent.
		 */
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
		uint64_t new_off = offset + (idx->bti_before ? 0 : 1);
		if (leaf->btl_hdr.bth_count > new_off) {
			out_idx->bti_node = &leaf->btl_hdr;
			out_idx->bti_offset = new_off;
			out_idx->bti_before = B_FALSE;
			return (leaf->btl_elems + new_off * tree->bt_elem_size);
		}

		zfs_btree_hdr_t *prev = &leaf->btl_hdr;
		for (zfs_btree_core_t *node = leaf->btl_hdr.bth_parent;
		    node != NULL; node = node->btc_hdr.bth_parent) {
			zfs_btree_hdr_t *hdr = &node->btc_hdr;
			ASSERT(hdr->bth_core);
			uint64_t i = zfs_btree_find_parent_idx(tree, prev);
			if (done_func != NULL)
				done_func(tree, prev);
			if (i == hdr->bth_count) {
				prev = hdr;
				continue;
			}
			out_idx->bti_node = hdr;
			out_idx->bti_offset = i;
			out_idx->bti_before = B_FALSE;
			return (node->btc_elems + i * tree->bt_elem_size);
		}
		if (done_func != NULL)
			done_func(tree, prev);
		/*
		 * We've traversed all the way up and been at the end of the
		 * node every time, so this was the last element in the tree.
		 */
		return (NULL);
	}

	/* If we were before an element in a core node, return that element. */
	ASSERT(idx->bti_node->bth_core);
	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
	if (idx->bti_before) {
		out_idx->bti_before = B_FALSE;
		return (node->btc_elems + offset * tree->bt_elem_size);
	}

	/*
	 * The next element from one in a core node is the first element in
	 * the subtree just to the right of the separator.
	 */
	zfs_btree_hdr_t *child = node->btc_children[offset + 1];
	return (zfs_btree_first_helper(child, out_idx));
}

/*
 * Return the next valued node in the tree.  The same address can be safely
 * passed for idx and out_idx.
 */
void *
zfs_btree_next(zfs_btree_t *tree, const zfs_btree_index_t *idx,
    zfs_btree_index_t *out_idx)
{
	return (zfs_btree_next_helper(tree, idx, out_idx, NULL));
}

/*
 * Return the previous valued node in the tree.  The same value can be safely
 * passed for idx and out_idx.
 */
void *
zfs_btree_prev(zfs_btree_t *tree, const zfs_btree_index_t *idx,
    zfs_btree_index_t *out_idx)
{
	if (idx->bti_node == NULL) {
		ASSERT3S(tree->bt_height, ==, -1);
		return (NULL);
	}

	uint64_t offset = idx->bti_offset;
	if (!idx->bti_node->bth_core) {
		/*
		 * When finding the previous element of an element in a leaf,
		 * there are two cases. If the element isn't the first one in
		 * the leaf, in which case we just return the previous element
		 * in the leaf. Otherwise, we need to traverse up our parents
		 * until we find one where our previous ancestor isn't the
		 * first child. Once we do, the previous element is the
		 * separator after our previous ancestor.
		 */
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
		if (offset != 0) {
			out_idx->bti_node = &leaf->btl_hdr;
			out_idx->bti_offset = offset - 1;
			out_idx->bti_before = B_FALSE;
			return (leaf->btl_elems + (offset - 1) *
			    tree->bt_elem_size);
		}
		zfs_btree_hdr_t *prev = &leaf->btl_hdr;
		for (zfs_btree_core_t *node = leaf->btl_hdr.bth_parent;
		    node != NULL; node = node->btc_hdr.bth_parent) {
			zfs_btree_hdr_t *hdr = &node->btc_hdr;
			ASSERT(hdr->bth_core);
			uint64_t i = zfs_btree_find_parent_idx(tree, prev);
			if (i == 0) {
				prev = hdr;
				continue;
			}
			out_idx->bti_node = hdr;
			out_idx->bti_offset = i - 1;
			out_idx->bti_before = B_FALSE;
			return (node->btc_elems + (i - 1) * tree->bt_elem_size);
		}
		/*
		 * We've traversed all the way up and been at the start of the
		 * node every time, so this was the first node in the tree.
		 */
		return (NULL);
	}

	/*
	 * The previous element from one in a core node is the last element in
	 * the subtree just to the left of the separator.
	 */
	ASSERT(idx->bti_node->bth_core);
	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
	zfs_btree_hdr_t *child = node->btc_children[offset];
	return (zfs_btree_last_helper(tree, child, out_idx));
}

/*
 * Get the value at the provided index in the tree.
 *
 * Note that the value returned from this function can be mutated, but only
 * if it will not change the ordering of the element with respect to any other
 * elements that could be in the tree.
 */
void *
zfs_btree_get(zfs_btree_t *tree, zfs_btree_index_t *idx)
{
	ASSERT(!idx->bti_before);
	if (!idx->bti_node->bth_core) {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)idx->bti_node;
		return (leaf->btl_elems + idx->bti_offset * tree->bt_elem_size);
	}
	ASSERT(idx->bti_node->bth_core);
	zfs_btree_core_t *node = (zfs_btree_core_t *)idx->bti_node;
	return (node->btc_elems + idx->bti_offset * tree->bt_elem_size);
}

/* Add the given value to the tree. Must not already be in the tree. */
void
zfs_btree_add(zfs_btree_t *tree, const void *node)
{
	zfs_btree_index_t where = {0};
	VERIFY3P(zfs_btree_find(tree, node, &where), ==, NULL);
	zfs_btree_add_idx(tree, node, &where);
}

/* Helper function to free a tree node. */
static void
zfs_btree_node_destroy(zfs_btree_t *tree, zfs_btree_hdr_t *node)
{
	tree->bt_num_nodes--;
	if (!node->bth_core) {
		kmem_cache_free(zfs_btree_leaf_cache, node);
	} else {
		kmem_free(node, sizeof (zfs_btree_core_t) +
		    BTREE_CORE_ELEMS * tree->bt_elem_size);
	}
}

/*
 * Remove the rm_hdr and the separator to its left from the parent node. The
 * buffer that rm_hdr was stored in may already be freed, so its contents
 * cannot be accessed.
 */
static void
zfs_btree_remove_from_node(zfs_btree_t *tree, zfs_btree_core_t *node,
    zfs_btree_hdr_t *rm_hdr)
{
	size_t size = tree->bt_elem_size;
	uint64_t min_count = (BTREE_CORE_ELEMS / 2) - 1;
	zfs_btree_hdr_t *hdr = &node->btc_hdr;
	/*
	 * If the node is the root node and rm_hdr is one of two children,
	 * promote the other child to the root.
	 */
	if (hdr->bth_parent == NULL && hdr->bth_count <= 1) {
		ASSERT3U(hdr->bth_count, ==, 1);
		ASSERT3P(tree->bt_root, ==, node);
		ASSERT3P(node->btc_children[1], ==, rm_hdr);
		tree->bt_root = node->btc_children[0];
		node->btc_children[0]->bth_parent = NULL;
		zfs_btree_node_destroy(tree, hdr);
		tree->bt_height--;
		return;
	}

	uint64_t idx;
	for (idx = 0; idx <= hdr->bth_count; idx++) {
		if (node->btc_children[idx] == rm_hdr)
			break;
	}
	ASSERT3U(idx, <=, hdr->bth_count);

	/*
	 * If the node is the root or it has more than the minimum number of
	 * children, just remove the child and separator, and return.
	 */
	if (hdr->bth_parent == NULL ||
	    hdr->bth_count > min_count) {
		/*
		 * Shift the element and children to the right of rm_hdr to
		 * the left by one spot.
		 */
		bt_shift_core_left(tree, node, idx, hdr->bth_count - idx,
		    BSS_PARALLELOGRAM);
		hdr->bth_count--;
		zfs_btree_poison_node_at(tree, hdr, hdr->bth_count);
		return;
	}

	ASSERT3U(hdr->bth_count, ==, min_count);

	/*
	 * Now we try to take a node from a neighbor. We check left, then
	 * right. If the neighbor exists and has more than the minimum number
	 * of elements, we move the separator between us and them to our
	 * node, move their closest element (last for left, first for right)
	 * to the separator, and move their closest child to our node. Along
	 * the way we need to collapse the gap made by idx, and (for our right
	 * neighbor) the gap made by removing their first element and child.
	 *
	 * Note: this logic currently doesn't support taking from a neighbor
	 * that isn't a sibling (i.e. a neighbor with a different
	 * parent). This isn't critical functionality, but may be worth
	 * implementing in the future for completeness' sake.
	 */
	zfs_btree_core_t *parent = hdr->bth_parent;
	uint64_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);

	zfs_btree_hdr_t *l_hdr = (parent_idx == 0 ? NULL :
	    parent->btc_children[parent_idx - 1]);
	if (l_hdr != NULL && l_hdr->bth_count > min_count) {
		/* We can take a node from the left neighbor. */
		ASSERT(l_hdr->bth_core);
		zfs_btree_core_t *neighbor = (zfs_btree_core_t *)l_hdr;

		/*
		 * Start by shifting the elements and children in the current
		 * node to the right by one spot.
		 */
		bt_shift_core_right(tree, node, 0, idx - 1, BSS_TRAPEZOID);

		/*
		 * Move the separator between node and neighbor to the first
		 * element slot in the current node.
		 */
		uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
		    size;
		bmov(separator, node->btc_elems, size);

		/* Move the last child of neighbor to our first child slot. */
		zfs_btree_hdr_t **take_child = neighbor->btc_children +
		    l_hdr->bth_count;
		bmov(take_child, node->btc_children, sizeof (*take_child));
		node->btc_children[0]->bth_parent = node;

		/* Move the last element of neighbor to the separator spot. */
		uint8_t *take_elem = neighbor->btc_elems +
		    (l_hdr->bth_count - 1) * size;
		bmov(take_elem, separator, size);
		l_hdr->bth_count--;
		zfs_btree_poison_node_at(tree, l_hdr, l_hdr->bth_count);
		return;
	}

	zfs_btree_hdr_t *r_hdr = (parent_idx == parent->btc_hdr.bth_count ?
	    NULL : parent->btc_children[parent_idx + 1]);
	if (r_hdr != NULL && r_hdr->bth_count > min_count) {
		/* We can take a node from the right neighbor. */
		ASSERT(r_hdr->bth_core);
		zfs_btree_core_t *neighbor = (zfs_btree_core_t *)r_hdr;

		/*
		 * Shift elements in node left by one spot to overwrite rm_hdr
		 * and the separator before it.
		 */
		bt_shift_core_left(tree, node, idx, hdr->bth_count - idx,
		    BSS_PARALLELOGRAM);

		/*
		 * Move the separator between node and neighbor to the last
		 * element spot in node.
		 */
		uint8_t *separator = parent->btc_elems + parent_idx * size;
		bmov(separator, node->btc_elems + (hdr->bth_count - 1) * size,
		    size);

		/*
		 * Move the first child of neighbor to the last child spot in
		 * node.
		 */
		zfs_btree_hdr_t **take_child = neighbor->btc_children;
		bmov(take_child, node->btc_children + hdr->bth_count,
		    sizeof (*take_child));
		node->btc_children[hdr->bth_count]->bth_parent = node;

		/* Move the first element of neighbor to the separator spot. */
		uint8_t *take_elem = neighbor->btc_elems;
		bmov(take_elem, separator, size);
		r_hdr->bth_count--;

		/*
		 * Shift the elements and children of neighbor to cover the
		 * stolen elements.
		 */
		bt_shift_core_left(tree, neighbor, 1, r_hdr->bth_count,
		    BSS_TRAPEZOID);
		zfs_btree_poison_node_at(tree, r_hdr, r_hdr->bth_count);
		return;
	}

	/*
	 * In this case, neither of our neighbors can spare an element, so we
	 * need to merge with one of them. We prefer the left one,
	 * arbitrarily. Move the separator into the leftmost merging node
	 * (which may be us or the left neighbor), and then move the right
	 * merging node's elements. Once that's done, we go back and delete
	 * the element we're removing. Finally, go into the parent and delete
	 * the right merging node and the separator. This may cause further
	 * merging.
	 */
	zfs_btree_hdr_t *new_rm_hdr, *keep_hdr;
	uint64_t new_idx = idx;
	if (l_hdr != NULL) {
		keep_hdr = l_hdr;
		new_rm_hdr = hdr;
		new_idx += keep_hdr->bth_count + 1;
	} else {
		ASSERT3P(r_hdr, !=, NULL);
		keep_hdr = hdr;
		new_rm_hdr = r_hdr;
		parent_idx++;
	}

	ASSERT(keep_hdr->bth_core);
	ASSERT(new_rm_hdr->bth_core);

	zfs_btree_core_t *keep = (zfs_btree_core_t *)keep_hdr;
	zfs_btree_core_t *rm = (zfs_btree_core_t *)new_rm_hdr;

	if (zfs_btree_verify_intensity >= 5) {
		for (int i = 0; i < new_rm_hdr->bth_count + 1; i++) {
			zfs_btree_verify_poison_at(tree, keep_hdr,
			    keep_hdr->bth_count + i);
		}
	}

	/* Move the separator into the left node. */
	uint8_t *e_out = keep->btc_elems + keep_hdr->bth_count * size;
	uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
	    size;
	bmov(separator, e_out, size);
	keep_hdr->bth_count++;

	/* Move all our elements and children into the left node. */
	bt_transfer_core(tree, rm, 0, new_rm_hdr->bth_count, keep,
	    keep_hdr->bth_count, BSS_TRAPEZOID);

	uint64_t old_count = keep_hdr->bth_count;

	/* Update bookkeeping */
	keep_hdr->bth_count += new_rm_hdr->bth_count;
	ASSERT3U(keep_hdr->bth_count, ==, (min_count * 2) + 1);

	/*
	 * Shift the element and children to the right of rm_hdr to
	 * the left by one spot.
	 */
	ASSERT3P(keep->btc_children[new_idx], ==, rm_hdr);
	bt_shift_core_left(tree, keep, new_idx, keep_hdr->bth_count - new_idx,
	    BSS_PARALLELOGRAM);
	keep_hdr->bth_count--;

	/* Reparent all our children to point to the left node. */
	zfs_btree_hdr_t **new_start = keep->btc_children +
	    old_count - 1;
	for (int i = 0; i < new_rm_hdr->bth_count + 1; i++)
		new_start[i]->bth_parent = keep;
	for (int i = 0; i <= keep_hdr->bth_count; i++) {
		ASSERT3P(keep->btc_children[i]->bth_parent, ==, keep);
		ASSERT3P(keep->btc_children[i], !=, rm_hdr);
	}
	zfs_btree_poison_node_at(tree, keep_hdr, keep_hdr->bth_count);

	new_rm_hdr->bth_count = 0;
	zfs_btree_node_destroy(tree, new_rm_hdr);
	zfs_btree_remove_from_node(tree, parent, new_rm_hdr);
}

/* Remove the element at the specific location. */
void
zfs_btree_remove_idx(zfs_btree_t *tree, zfs_btree_index_t *where)
{
	size_t size = tree->bt_elem_size;
	zfs_btree_hdr_t *hdr = where->bti_node;
	uint64_t idx = where->bti_offset;
	uint64_t capacity = P2ALIGN((BTREE_LEAF_SIZE -
	    sizeof (zfs_btree_hdr_t)) / size, 2);

	ASSERT(!where->bti_before);
	if (tree->bt_bulk != NULL) {
		/*
		 * Leave bulk insert mode. Note that our index would be
		 * invalid after we correct the tree, so we copy the value
		 * we're planning to remove and find it again after
		 * bulk_finish.
		 */
		uint8_t *value = zfs_btree_get(tree, where);
		uint8_t *tmp = kmem_alloc(size, KM_SLEEP);
		bmov(value, tmp, size);
		zfs_btree_bulk_finish(tree);
		VERIFY3P(zfs_btree_find(tree, tmp, where), !=, NULL);
		kmem_free(tmp, size);
		hdr = where->bti_node;
		idx = where->bti_offset;
	}

	tree->bt_num_elems--;
	/*
	 * If the element happens to be in a core node, we move a leaf node's
	 * element into its place and then remove the leaf node element. This
	 * makes the rebalance logic not need to be recursive both upwards and
	 * downwards.
	 */
	if (hdr->bth_core) {
		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		zfs_btree_hdr_t *left_subtree = node->btc_children[idx];
		void *new_value = zfs_btree_last_helper(tree, left_subtree,
		    where);
		ASSERT3P(new_value, !=, NULL);

		bmov(new_value, node->btc_elems + idx * size, size);

		hdr = where->bti_node;
		idx = where->bti_offset;
		ASSERT(!where->bti_before);
	}

	/*
	 * First, we'll update the leaf's metadata. Then, we shift any
	 * elements after the idx to the left. After that, we rebalance if
	 * needed.
	 */
	ASSERT(!hdr->bth_core);
	zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
	ASSERT3U(hdr->bth_count, >, 0);

	uint64_t min_count = (capacity / 2) - 1;

	/*
	 * If we're over the minimum size or this is the root, just overwrite
	 * the value and return.
	 */
	if (hdr->bth_count > min_count || hdr->bth_parent == NULL) {
		hdr->bth_count--;
		bt_shift_leaf_left(tree, leaf, idx + 1, hdr->bth_count - idx);
		if (hdr->bth_parent == NULL) {
			ASSERT0(tree->bt_height);
			if (hdr->bth_count == 0) {
				tree->bt_root = NULL;
				tree->bt_height--;
				zfs_btree_node_destroy(tree, &leaf->btl_hdr);
			}
		}
		if (tree->bt_root != NULL)
			zfs_btree_poison_node_at(tree, hdr, hdr->bth_count);
		zfs_btree_verify(tree);
		return;
	}
	ASSERT3U(hdr->bth_count, ==, min_count);

	/*
	 * Now we try to take a node from a sibling. We check left, then
	 * right. If they exist and have more than the minimum number of
	 * elements, we move the separator between us and them to our node
	 * and move their closest element (last for left, first for right) to
	 * the separator. Along the way we need to collapse the gap made by
	 * idx, and (for our right neighbor) the gap made by removing their
	 * first element.
	 *
	 * Note: this logic currently doesn't support taking from a neighbor
	 * that isn't a sibling. This isn't critical functionality, but may be
	 * worth implementing in the future for completeness' sake.
	 */
	zfs_btree_core_t *parent = hdr->bth_parent;
	uint64_t parent_idx = zfs_btree_find_parent_idx(tree, hdr);

	zfs_btree_hdr_t *l_hdr = (parent_idx == 0 ? NULL :
	    parent->btc_children[parent_idx - 1]);
	if (l_hdr != NULL && l_hdr->bth_count > min_count) {
		/* We can take a node from the left neighbor. */
		ASSERT(!l_hdr->bth_core);

		/*
		 * Move our elements back by one spot to make room for the
		 * stolen element and overwrite the element being removed.
		 */
		bt_shift_leaf_right(tree, leaf, 0, idx);
		uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
		    size;
		uint8_t *take_elem = ((zfs_btree_leaf_t *)l_hdr)->btl_elems +
		    (l_hdr->bth_count - 1) * size;
		/* Move the separator to our first spot. */
		bmov(separator, leaf->btl_elems, size);

		/* Move our neighbor's last element to the separator. */
		bmov(take_elem, separator, size);

		/* Update the bookkeeping. */
		l_hdr->bth_count--;
		zfs_btree_poison_node_at(tree, l_hdr, l_hdr->bth_count);

		zfs_btree_verify(tree);
		return;
	}

	zfs_btree_hdr_t *r_hdr = (parent_idx == parent->btc_hdr.bth_count ?
	    NULL : parent->btc_children[parent_idx + 1]);
	if (r_hdr != NULL && r_hdr->bth_count > min_count) {
		/* We can take a node from the right neighbor. */
		ASSERT(!r_hdr->bth_core);
		zfs_btree_leaf_t *neighbor = (zfs_btree_leaf_t *)r_hdr;

		/*
		 * Move our elements after the element being removed forwards
		 * by one spot to make room for the stolen element and
		 * overwrite the element being removed.
		 */
		bt_shift_leaf_left(tree, leaf, idx + 1, hdr->bth_count - idx -
		    1);

		uint8_t *separator = parent->btc_elems + parent_idx * size;
		uint8_t *take_elem = ((zfs_btree_leaf_t *)r_hdr)->btl_elems;
		/* Move the separator between us to our last spot. */
		bmov(separator, leaf->btl_elems + (hdr->bth_count - 1) * size,
		    size);

		/* Move our neighbor's first element to the separator. */
		bmov(take_elem, separator, size);

		/* Update the bookkeeping. */
		r_hdr->bth_count--;

		/*
		 * Move our neighbors elements forwards to overwrite the
		 * stolen element.
		 */
		bt_shift_leaf_left(tree, neighbor, 1, r_hdr->bth_count);
		zfs_btree_poison_node_at(tree, r_hdr, r_hdr->bth_count);
		zfs_btree_verify(tree);
		return;
	}

	/*
	 * In this case, neither of our neighbors can spare an element, so we
	 * need to merge with one of them. We prefer the left one,
	 * arbitrarily. Move the separator into the leftmost merging node
	 * (which may be us or the left neighbor), and then move the right
	 * merging node's elements. Once that's done, we go back and delete
	 * the element we're removing. Finally, go into the parent and delete
	 * the right merging node and the separator. This may cause further
	 * merging.
	 */
	zfs_btree_hdr_t *rm_hdr, *keep_hdr;
	uint64_t new_idx = idx;
	if (l_hdr != NULL) {
		keep_hdr = l_hdr;
		rm_hdr = hdr;
		new_idx += keep_hdr->bth_count + 1; // 449
	} else {
		ASSERT3P(r_hdr, !=, NULL);
		keep_hdr = hdr;
		rm_hdr = r_hdr;
		parent_idx++;
	}

	ASSERT(!keep_hdr->bth_core);
	ASSERT(!rm_hdr->bth_core);
	ASSERT3U(keep_hdr->bth_count, ==, min_count);
	ASSERT3U(rm_hdr->bth_count, ==, min_count);

	zfs_btree_leaf_t *keep = (zfs_btree_leaf_t *)keep_hdr;
	zfs_btree_leaf_t *rm = (zfs_btree_leaf_t *)rm_hdr;

	if (zfs_btree_verify_intensity >= 5) {
		for (int i = 0; i < rm_hdr->bth_count + 1; i++) {
			zfs_btree_verify_poison_at(tree, keep_hdr,
			    keep_hdr->bth_count + i);
		}
	}
	/*
	 * Move the separator into the first open spot in the left
	 * neighbor.
	 */
	uint8_t *out = keep->btl_elems + keep_hdr->bth_count * size;
	uint8_t *separator = parent->btc_elems + (parent_idx - 1) *
	    size;
	bmov(separator, out, size);
	keep_hdr->bth_count++;

	/* Move our elements to the left neighbor. */
	bt_transfer_leaf(tree, rm, 0, rm_hdr->bth_count, keep,
	    keep_hdr->bth_count);

	/* Update the bookkeeping. */
	keep_hdr->bth_count += rm_hdr->bth_count;
	ASSERT3U(keep_hdr->bth_count, ==, min_count * 2 + 1);

	/* Remove the value from the node */
	keep_hdr->bth_count--;
	bt_shift_leaf_left(tree, keep, new_idx + 1, keep_hdr->bth_count -
	    new_idx);
	zfs_btree_poison_node_at(tree, keep_hdr, keep_hdr->bth_count);

	rm_hdr->bth_count = 0;
	zfs_btree_node_destroy(tree, rm_hdr);
	/* Remove the emptied node from the parent. */
	zfs_btree_remove_from_node(tree, parent, rm_hdr);
	zfs_btree_verify(tree);
}

/* Remove the given value from the tree. */
void
zfs_btree_remove(zfs_btree_t *tree, const void *value)
{
	zfs_btree_index_t where = {0};
	VERIFY3P(zfs_btree_find(tree, value, &where), !=, NULL);
	zfs_btree_remove_idx(tree, &where);
}

/* Return the number of elements in the tree. */
ulong_t
zfs_btree_numnodes(zfs_btree_t *tree)
{
	return (tree->bt_num_elems);
}

/*
 * This function is used to visit all the elements in the tree before
 * destroying the tree. This allows the calling code to perform any cleanup it
 * needs to do. This is more efficient than just removing the first element
 * over and over, because it removes all rebalancing. Once the destroy_nodes()
 * function has been called, no other btree operations are valid until it
 * returns NULL, which point the only valid operation is zfs_btree_destroy().
 *
 * example:
 *
 *      zfs_btree_index_t *cookie = NULL;
 *      my_data_t *node;
 *
 *      while ((node = zfs_btree_destroy_nodes(tree, &cookie)) != NULL)
 *              free(node->ptr);
 *      zfs_btree_destroy(tree);
 *
 */
void *
zfs_btree_destroy_nodes(zfs_btree_t *tree, zfs_btree_index_t **cookie)
{
	if (*cookie == NULL) {
		if (tree->bt_height == -1)
			return (NULL);
		*cookie = kmem_alloc(sizeof (**cookie), KM_SLEEP);
		return (zfs_btree_first(tree, *cookie));
	}

	void *rval = zfs_btree_next_helper(tree, *cookie, *cookie,
	    zfs_btree_node_destroy);
	if (rval == NULL)   {
		tree->bt_root = NULL;
		tree->bt_height = -1;
		tree->bt_num_elems = 0;
		kmem_free(*cookie, sizeof (**cookie));
		tree->bt_bulk = NULL;
	}
	return (rval);
}

static void
zfs_btree_clear_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	if (hdr->bth_core) {
		zfs_btree_core_t *btc = (zfs_btree_core_t *)hdr;
		for (int i = 0; i <= hdr->bth_count; i++) {
			zfs_btree_clear_helper(tree, btc->btc_children[i]);
		}
	}

	zfs_btree_node_destroy(tree, hdr);
}

void
zfs_btree_clear(zfs_btree_t *tree)
{
	if (tree->bt_root == NULL) {
		ASSERT0(tree->bt_num_elems);
		return;
	}

	zfs_btree_clear_helper(tree, tree->bt_root);
	tree->bt_num_elems = 0;
	tree->bt_root = NULL;
	tree->bt_num_nodes = 0;
	tree->bt_height = -1;
	tree->bt_bulk = NULL;
}

void
zfs_btree_destroy(zfs_btree_t *tree)
{
	ASSERT0(tree->bt_num_elems);
	ASSERT3P(tree->bt_root, ==, NULL);
}

/* Verify that every child of this node has the correct parent pointer. */
static void
zfs_btree_verify_pointers_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	if (!hdr->bth_core)
		return;

	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
	for (int i = 0; i <= hdr->bth_count; i++) {
		VERIFY3P(node->btc_children[i]->bth_parent, ==, hdr);
		zfs_btree_verify_pointers_helper(tree, node->btc_children[i]);
	}
}

/* Verify that every node has the correct parent pointer. */
static void
zfs_btree_verify_pointers(zfs_btree_t *tree)
{
	if (tree->bt_height == -1) {
		VERIFY3P(tree->bt_root, ==, NULL);
		return;
	}
	VERIFY3P(tree->bt_root->bth_parent, ==, NULL);
	zfs_btree_verify_pointers_helper(tree, tree->bt_root);
}

/*
 * Verify that all the current node and its children satisfy the count
 * invariants, and return the total count in the subtree rooted in this node.
 */
static uint64_t
zfs_btree_verify_counts_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	if (!hdr->bth_core) {
		if (tree->bt_root != hdr && hdr != &tree->bt_bulk->btl_hdr) {
			uint64_t capacity = P2ALIGN((BTREE_LEAF_SIZE -
			    sizeof (zfs_btree_hdr_t)) / tree->bt_elem_size, 2);
			VERIFY3U(hdr->bth_count, >=, (capacity / 2) - 1);
		}

		return (hdr->bth_count);
	} else {

		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		uint64_t ret = hdr->bth_count;
		if (tree->bt_root != hdr && tree->bt_bulk == NULL)
			VERIFY3P(hdr->bth_count, >=, BTREE_CORE_ELEMS / 2 - 1);
		for (int i = 0; i <= hdr->bth_count; i++) {
			ret += zfs_btree_verify_counts_helper(tree,
			    node->btc_children[i]);
		}

		return (ret);
	}
}

/*
 * Verify that all nodes satisfy the invariants and that the total number of
 * elements is correct.
 */
static void
zfs_btree_verify_counts(zfs_btree_t *tree)
{
	EQUIV(tree->bt_num_elems == 0, tree->bt_height == -1);
	if (tree->bt_height == -1) {
		return;
	}
	VERIFY3P(zfs_btree_verify_counts_helper(tree, tree->bt_root), ==,
	    tree->bt_num_elems);
}

/*
 * Check that the subtree rooted at this node has a uniform height. Returns
 * the number of nodes under this node, to help verify bt_num_nodes.
 */
static uint64_t
zfs_btree_verify_height_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr,
    int64_t height)
{
	if (!hdr->bth_core) {
		VERIFY0(height);
		return (1);
	}

	VERIFY(hdr->bth_core);
	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
	uint64_t ret = 1;
	for (int i = 0; i <= hdr->bth_count; i++) {
		ret += zfs_btree_verify_height_helper(tree,
		    node->btc_children[i], height - 1);
	}
	return (ret);
}

/*
 * Check that the tree rooted at this node has a uniform height, and that the
 * bt_height in the tree is correct.
 */
static void
zfs_btree_verify_height(zfs_btree_t *tree)
{
	EQUIV(tree->bt_height == -1, tree->bt_root == NULL);
	if (tree->bt_height == -1) {
		return;
	}

	VERIFY3U(zfs_btree_verify_height_helper(tree, tree->bt_root,
	    tree->bt_height), ==, tree->bt_num_nodes);
}

/*
 * Check that the elements in this node are sorted, and that if this is a core
 * node, the separators are properly between the subtrees they separaate and
 * that the children also satisfy this requirement.
 */
static void
zfs_btree_verify_order_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	size_t size = tree->bt_elem_size;
	if (!hdr->bth_core) {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
		for (int i = 1; i < hdr->bth_count; i++) {
			VERIFY3S(tree->bt_compar(leaf->btl_elems + (i - 1) *
			    size, leaf->btl_elems + i * size), ==, -1);
		}
		return;
	}

	zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
	for (int i = 1; i < hdr->bth_count; i++) {
		VERIFY3S(tree->bt_compar(node->btc_elems + (i - 1) * size,
		    node->btc_elems + i * size), ==, -1);
	}
	for (int i = 0; i < hdr->bth_count; i++) {
		uint8_t *left_child_last = NULL;
		zfs_btree_hdr_t *left_child_hdr = node->btc_children[i];
		if (left_child_hdr->bth_core) {
			zfs_btree_core_t *left_child =
			    (zfs_btree_core_t *)left_child_hdr;
			left_child_last = left_child->btc_elems +
			    (left_child_hdr->bth_count - 1) * size;
		} else {
			zfs_btree_leaf_t *left_child =
			    (zfs_btree_leaf_t *)left_child_hdr;
			left_child_last = left_child->btl_elems +
			    (left_child_hdr->bth_count - 1) * size;
		}
		if (tree->bt_compar(node->btc_elems + i * size,
		    left_child_last) != 1) {
			panic("btree: compar returned %d (expected 1) at "
			    "%px %d: compar(%px,  %px)", tree->bt_compar(
			    node->btc_elems + i * size, left_child_last),
			    (void *)node, i, (void *)(node->btc_elems + i *
			    size), (void *)left_child_last);
		}

		uint8_t *right_child_first = NULL;
		zfs_btree_hdr_t *right_child_hdr = node->btc_children[i + 1];
		if (right_child_hdr->bth_core) {
			zfs_btree_core_t *right_child =
			    (zfs_btree_core_t *)right_child_hdr;
			right_child_first = right_child->btc_elems;
		} else {
			zfs_btree_leaf_t *right_child =
			    (zfs_btree_leaf_t *)right_child_hdr;
			right_child_first = right_child->btl_elems;
		}
		if (tree->bt_compar(node->btc_elems + i * size,
		    right_child_first) != -1) {
			panic("btree: compar returned %d (expected -1) at "
			    "%px %d: compar(%px,  %px)", tree->bt_compar(
			    node->btc_elems + i * size, right_child_first),
			    (void *)node, i, (void *)(node->btc_elems + i *
			    size), (void *)right_child_first);
		}
	}
	for (int i = 0; i <= hdr->bth_count; i++) {
		zfs_btree_verify_order_helper(tree, node->btc_children[i]);
	}
}

/* Check that all elements in the tree are in sorted order. */
static void
zfs_btree_verify_order(zfs_btree_t *tree)
{
	EQUIV(tree->bt_height == -1, tree->bt_root == NULL);
	if (tree->bt_height == -1) {
		return;
	}

	zfs_btree_verify_order_helper(tree, tree->bt_root);
}

#ifdef ZFS_DEBUG
/* Check that all unused memory is poisoned correctly. */
static void
zfs_btree_verify_poison_helper(zfs_btree_t *tree, zfs_btree_hdr_t *hdr)
{
	size_t size = tree->bt_elem_size;
	if (!hdr->bth_core) {
		zfs_btree_leaf_t *leaf = (zfs_btree_leaf_t *)hdr;
		uint8_t val = 0x0f;
		for (int i = hdr->bth_count * size; i < BTREE_LEAF_SIZE -
		    sizeof (zfs_btree_hdr_t); i++) {
			VERIFY3U(leaf->btl_elems[i], ==, val);
		}
	} else {
		zfs_btree_core_t *node = (zfs_btree_core_t *)hdr;
		uint8_t val = 0x0f;
		for (int i = hdr->bth_count * size; i < BTREE_CORE_ELEMS * size;
		    i++) {
			VERIFY3U(node->btc_elems[i], ==, val);
		}

		for (int i = hdr->bth_count + 1; i <= BTREE_CORE_ELEMS; i++) {
			VERIFY3P(node->btc_children[i], ==,
			    (zfs_btree_hdr_t *)BTREE_POISON);
		}

		for (int i = 0; i <= hdr->bth_count; i++) {
			zfs_btree_verify_poison_helper(tree,
			    node->btc_children[i]);
		}
	}
}
#endif

/* Check that unused memory in the tree is still poisoned. */
static void
zfs_btree_verify_poison(zfs_btree_t *tree)
{
#ifdef ZFS_DEBUG
	if (tree->bt_height == -1)
		return;
	zfs_btree_verify_poison_helper(tree, tree->bt_root);
#endif
}

void
zfs_btree_verify(zfs_btree_t *tree)
{
	if (zfs_btree_verify_intensity == 0)
		return;
	zfs_btree_verify_height(tree);
	if (zfs_btree_verify_intensity == 1)
		return;
	zfs_btree_verify_pointers(tree);
	if (zfs_btree_verify_intensity == 2)
		return;
	zfs_btree_verify_counts(tree);
	if (zfs_btree_verify_intensity == 3)
		return;
	zfs_btree_verify_order(tree);

	if (zfs_btree_verify_intensity == 4)
		return;
	zfs_btree_verify_poison(tree);
}