summaryrefslogtreecommitdiffstats
path: root/src/H5B2int.c
blob: 01acc5646d366eecaf1006499ca9fb9e6a1858e3 (plain)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the COPYING file, which can be found at the root of the source code       *
 * distribution tree, or in https://www.hdfgroup.org/licenses.               *
 * If you do not have access to either file, you may request a copy from     *
 * help@hdfgroup.org.                                                        *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

/*-------------------------------------------------------------------------
 *
 * Created:		H5B2int.c
 *			Feb 27 2006
 *			Quincey Koziol
 *
 * Purpose:		Internal routines for managing v2 B-trees.
 *
 *-------------------------------------------------------------------------
 */

/****************/
/* Module Setup */
/****************/

#include "H5B2module.h" /* This source code file is part of the H5B2 module */

/***********/
/* Headers */
/***********/
#include "H5private.h"   /* Generic Functions			*/
#include "H5B2pkg.h"     /* v2 B-trees				*/
#include "H5Eprivate.h"  /* Error handling		  	*/
#include "H5MMprivate.h" /* Memory management			*/
#include "H5VMprivate.h" /* Vectors and arrays 			*/

/****************/
/* Local Macros */
/****************/

/******************/
/* Local Typedefs */
/******************/

/********************/
/* Package Typedefs */
/********************/

/********************/
/* Local Prototypes */
/********************/
static herr_t H5B2__update_child_flush_depends(H5B2_hdr_t *hdr, unsigned depth, H5B2_node_ptr_t *node_ptrs,
                                               unsigned start_idx, unsigned end_idx, void *old_parent,
                                               void *new_parent);

/*********************/
/* Package Variables */
/*********************/

/*****************************/
/* Library Private Variables */
/*****************************/

/*******************/
/* Local Variables */
/*******************/

/* Declare a free list to manage the 'H5B2_node_info_t' sequence information */
H5FL_SEQ_EXTERN(H5B2_node_info_t);

/*-------------------------------------------------------------------------
 * Function:	H5B2__locate_record
 *
 * Purpose:	Performs a binary search to locate a record in a sorted
 *              array of records.
 *
 *              Sets *IDX to location of record greater than or equal to
 *              record to locate.
 *
 * Return:	Comparison value for insertion location.  Negative for record
 *              to locate being less than value in *IDX.  Zero for record to
 *              locate equal to value in *IDX.  Positive for record to locate
 *              being greater than value in *IDX (which should only happen when
 *              record to locate is greater than all records to search).
 *
 * Programmer:	Quincey Koziol
 *		Feb  3 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__locate_record(const H5B2_class_t *type, unsigned nrec, size_t *rec_off, const uint8_t *native,
                    const void *udata, unsigned *idx, int *cmp)
{
    unsigned lo        = 0, hi; /* Low & high index values */
    unsigned my_idx    = 0;     /* Final index value */
    herr_t   ret_value = SUCCEED;

    FUNC_ENTER_PACKAGE

    *cmp = -1;

    hi = nrec;
    while (lo < hi && *cmp) {
        my_idx = (lo + hi) / 2;
        if ((type->compare)(udata, native + rec_off[my_idx], cmp) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTCOMPARE, FAIL, "can't compare btree2 records")
        if (*cmp < 0)
            hi = my_idx;
        else
            lo = my_idx + 1;
    } /* end while */

    *idx = my_idx;

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__locate_record */

/*-------------------------------------------------------------------------
 * Function:	H5B2__split1
 *
 * Purpose:	Perform a 1->2 node split
 *
 * Return:	Success:	Non-negative
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Aug 28 2006
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__split1(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node_ptr,
             unsigned *parent_cache_info_flags_ptr, H5B2_internal_t *internal, unsigned *internal_flags_ptr,
             unsigned idx)
{
    const H5AC_class_t *child_class;                             /* Pointer to child node's class info */
    haddr_t   left_addr = HADDR_UNDEF, right_addr = HADDR_UNDEF; /* Addresses of left & right child nodes */
    void     *left_child = NULL, *right_child = NULL;            /* Pointers to child nodes */
    uint16_t *left_nrec, *right_nrec;                            /* Pointers to child # of records */
    uint8_t  *left_native, *right_native;                        /* Pointers to childs' native records */
    H5B2_node_ptr_t *left_node_ptrs  = NULL,
                    *right_node_ptrs = NULL; /* Pointers to childs' node pointer info */
    uint16_t mid_record;                     /* Index of "middle" record in current node */
    uint16_t old_node_nrec;                  /* Number of records in internal node split */
    unsigned left_child_flags  = H5AC__NO_FLAGS_SET,
             right_child_flags = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    herr_t ret_value           = SUCCEED;            /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(internal);
    HDassert(internal_flags_ptr);

    /* Slide records in parent node up one space, to make room for promoted record */
    if (idx < internal->nrec) {
        HDmemmove(H5B2_INT_NREC(internal, hdr, idx + 1), H5B2_INT_NREC(internal, hdr, idx),
                  hdr->cls->nrec_size * (internal->nrec - idx));
        HDmemmove(&(internal->node_ptrs[idx + 2]), &(internal->node_ptrs[idx + 1]),
                  sizeof(H5B2_node_ptr_t) * (internal->nrec - idx));
    } /* end if */

    /* Check for the kind of B-tree node to split */
    if (depth > 1) {
        H5B2_internal_t *left_int = NULL, *right_int = NULL; /* Pointers to old & new internal nodes */

        /* Create new internal node */
        internal->node_ptrs[idx + 1].all_nrec = internal->node_ptrs[idx + 1].node_nrec = 0;
        if (H5B2__create_internal(hdr, internal, &(internal->node_ptrs[idx + 1]), (uint16_t)(depth - 1)) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create new internal node")

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_INT;

        /* Protect both leaves */
        /* (Shadow left node if doing SWMR writes) */
        if (NULL ==
            (left_int = H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx],
                                               (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_int = H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx + 1],
                                                        (uint16_t)(depth - 1), FALSE, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child      = left_int;
        right_child     = right_int;
        left_nrec       = &(left_int->nrec);
        right_nrec      = &(right_int->nrec);
        left_native     = left_int->int_native;
        right_native    = right_int->int_native;
        left_node_ptrs  = left_int->node_ptrs;
        right_node_ptrs = right_int->node_ptrs;
    } /* end if */
    else {
        H5B2_leaf_t *left_leaf = NULL, *right_leaf = NULL; /* Pointers to old & new leaf nodes */

        /* Create new leaf node */
        internal->node_ptrs[idx + 1].all_nrec = internal->node_ptrs[idx + 1].node_nrec = 0;
        if (H5B2__create_leaf(hdr, internal, &(internal->node_ptrs[idx + 1])) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create new leaf node")

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_LEAF;

        /* Protect both leaves */
        /* (Shadow the left node if doing SWMR writes) */
        if (NULL == (left_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx], hdr->swmr_write,
                                                    H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx + 1], FALSE,
                                                     H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child   = left_leaf;
        right_child  = right_leaf;
        left_nrec    = &(left_leaf->nrec);
        right_nrec   = &(right_leaf->nrec);
        left_native  = left_leaf->leaf_native;
        right_native = right_leaf->leaf_native;
    } /* end if */

    /* Get the number of records in node to split */
    old_node_nrec = internal->node_ptrs[idx].node_nrec;

    /* Determine "middle" record to promote to internal node */
    mid_record = (uint16_t)(old_node_nrec / 2);

    /* Copy "upper half" of records to new child */
    H5MM_memcpy(H5B2_NAT_NREC(right_native, hdr, 0),
                H5B2_NAT_NREC(left_native, hdr, mid_record + (unsigned)1),
                hdr->cls->nrec_size * (old_node_nrec - (mid_record + (unsigned)1)));

    /* Copy "upper half" of node pointers, if the node is an internal node */
    if (depth > 1)
        H5MM_memcpy(&(right_node_ptrs[0]), &(left_node_ptrs[mid_record + (unsigned)1]),
                    sizeof(H5B2_node_ptr_t) * (size_t)(old_node_nrec - mid_record));

    /* Copy "middle" record to internal node */
    H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx), H5B2_NAT_NREC(left_native, hdr, mid_record),
                hdr->cls->nrec_size);

    /* Mark nodes as dirty */
    left_child_flags |= H5AC__DIRTIED_FLAG;
    right_child_flags |= H5AC__DIRTIED_FLAG;

    /* Update record counts in child nodes */
    internal->node_ptrs[idx].node_nrec = *left_nrec = mid_record;
    internal->node_ptrs[idx + 1].node_nrec = *right_nrec = (uint16_t)(old_node_nrec - (mid_record + 1));

    /* Determine total number of records in new child nodes */
    if (depth > 1) {
        unsigned u;                  /* Local index variable */
        hsize_t  new_left_all_nrec;  /* New total number of records in left child */
        hsize_t  new_right_all_nrec; /* New total number of records in right child */

        /* Compute total of all records in each child node */
        new_left_all_nrec = internal->node_ptrs[idx].node_nrec;
        for (u = 0; u < (*left_nrec + (unsigned)1); u++)
            new_left_all_nrec += left_node_ptrs[u].all_nrec;

        new_right_all_nrec = internal->node_ptrs[idx + 1].node_nrec;
        for (u = 0; u < (*right_nrec + (unsigned)1); u++)
            new_right_all_nrec += right_node_ptrs[u].all_nrec;

        internal->node_ptrs[idx].all_nrec     = new_left_all_nrec;
        internal->node_ptrs[idx + 1].all_nrec = new_right_all_nrec;
    } /* end if */
    else {
        internal->node_ptrs[idx].all_nrec     = internal->node_ptrs[idx].node_nrec;
        internal->node_ptrs[idx + 1].all_nrec = internal->node_ptrs[idx + 1].node_nrec;
    } /* end else */

    /* Update # of records in parent node */
    internal->nrec++;

    /* Mark parent as dirty */
    *internal_flags_ptr |= H5AC__DIRTIED_FLAG;

    /* Update grandparent info */
    curr_node_ptr->node_nrec++;

    /* Mark grandparent as dirty, if given */
    if (parent_cache_info_flags_ptr)
        *parent_cache_info_flags_ptr |= H5AC__DIRTIED_FLAG;

    /* Update flush dependencies for grandchildren, if using SWMR */
    if (hdr->swmr_write && depth > 1)
        if (H5B2__update_child_flush_depends(hdr, depth, right_node_ptrs, 0, (unsigned)(*right_nrec + 1),
                                             left_child, right_child) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1) {
        H5B2__assert_internal2(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)left_child,
                               (H5B2_internal_t *)right_child);
        H5B2__assert_internal2(internal->node_ptrs[idx + 1].all_nrec, hdr, (H5B2_internal_t *)right_child,
                               (H5B2_internal_t *)left_child);
    } /* end if */
    else {
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)left_child, (H5B2_leaf_t *)right_child);
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)right_child);
    }  /* end else */
#endif /* H5B2_DEBUG */

done:
    /* Release child nodes (marked as dirty) */
    if (left_child && H5AC_unprotect(hdr->f, child_class, left_addr, left_child, left_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree leaf node")
    if (right_child && H5AC_unprotect(hdr->f, child_class, right_addr, right_child, right_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree leaf node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__split1() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__split_root
 *
 * Purpose:	Split the root node
 *
 * Return:	Success:	Non-negative
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Feb  3 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__split_root(H5B2_hdr_t *hdr)
{
    H5B2_internal_t *new_root       = NULL;               /* Pointer to new root node */
    unsigned         new_root_flags = H5AC__NO_FLAGS_SET; /* Cache flags for new root node */
    H5B2_node_ptr_t  old_root_ptr;                        /* Old node pointer to root node in B-tree */
    size_t           sz_max_nrec;                         /* Temporary variable for range checking */
    unsigned         u_max_nrec_size;                     /* Temporary variable for range checking */
    herr_t           ret_value = SUCCEED;                 /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);

    /* Update depth of B-tree */
    hdr->depth++;

    /* Re-allocate array of node info structs */
    if (NULL ==
        (hdr->node_info = H5FL_SEQ_REALLOC(H5B2_node_info_t, hdr->node_info, (size_t)(hdr->depth + 1))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL, "memory allocation failed")

    /* Update node info for new depth of tree */
    sz_max_nrec = H5B2_NUM_INT_REC(hdr, hdr->depth);
    H5_CHECKED_ASSIGN(hdr->node_info[hdr->depth].max_nrec, unsigned, sz_max_nrec, size_t)
    hdr->node_info[hdr->depth].split_nrec = (hdr->node_info[hdr->depth].max_nrec * hdr->split_percent) / 100;
    hdr->node_info[hdr->depth].merge_nrec = (hdr->node_info[hdr->depth].max_nrec * hdr->merge_percent) / 100;
    hdr->node_info[hdr->depth].cum_max_nrec =
        ((hdr->node_info[hdr->depth].max_nrec + 1) * hdr->node_info[hdr->depth - 1].cum_max_nrec) +
        hdr->node_info[hdr->depth].max_nrec;
    u_max_nrec_size = H5VM_limit_enc_size((uint64_t)hdr->node_info[hdr->depth].cum_max_nrec);
    H5_CHECKED_ASSIGN(hdr->node_info[hdr->depth].cum_max_nrec_size, uint8_t, u_max_nrec_size, unsigned)
    if (NULL == (hdr->node_info[hdr->depth].nat_rec_fac =
                     H5FL_fac_init(hdr->cls->nrec_size * hdr->node_info[hdr->depth].max_nrec)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL, "can't create node native key block factory")
    if (NULL == (hdr->node_info[hdr->depth].node_ptr_fac =
                     H5FL_fac_init(sizeof(H5B2_node_ptr_t) * (hdr->node_info[hdr->depth].max_nrec + 1))))
        HGOTO_ERROR(H5E_RESOURCE, H5E_CANTINIT, FAIL,
                    "can't create internal 'branch' node node pointer block factory")

    /* Keep old root node pointer info */
    old_root_ptr = hdr->root;

    /* Create new internal node to use as root */
    hdr->root.node_nrec = 0;
    if (H5B2__create_internal(hdr, hdr, &(hdr->root), hdr->depth) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create new internal node")

    /* Protect new root node */
    if (NULL ==
        (new_root = H5B2__protect_internal(hdr, hdr, &hdr->root, hdr->depth, FALSE, H5AC__NO_FLAGS_SET)))
        HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")

    /* Set first node pointer in root node to old root node pointer info */
    new_root->node_ptrs[0] = old_root_ptr;

    /* Split original root node */
    if (H5B2__split1(hdr, hdr->depth, &(hdr->root), NULL, new_root, &new_root_flags, 0) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to split old root node")

done:
    /* Release new root node (marked as dirty) */
    if (new_root && H5AC_unprotect(hdr->f, H5AC_BT2_INT, hdr->root.addr, new_root, new_root_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree internal node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__split_root() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__redistribute2
 *
 * Purpose:	Redistribute records between two nodes
 *
 * Return:	Success:	Non-negative
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Feb  9 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__redistribute2(H5B2_hdr_t *hdr, uint16_t depth, H5B2_internal_t *internal, unsigned idx)
{
    const H5AC_class_t *child_class;                             /* Pointer to child node's class info */
    haddr_t   left_addr = HADDR_UNDEF, right_addr = HADDR_UNDEF; /* Addresses of left & right child nodes */
    void     *left_child = NULL, *right_child = NULL;            /* Pointers to child nodes */
    uint16_t *left_nrec, *right_nrec;                            /* Pointers to child # of records */
    uint8_t  *left_native, *right_native;                        /* Pointers to childs' native records */
    H5B2_node_ptr_t *left_node_ptrs  = NULL,
                    *right_node_ptrs = NULL;            /* Pointers to childs' node pointer info */
    hssize_t left_moved_nrec = 0, right_moved_nrec = 0; /* Number of records moved, for internal redistrib */
    unsigned left_child_flags  = H5AC__NO_FLAGS_SET,
             right_child_flags = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    herr_t ret_value           = SUCCEED;            /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(internal);

    /* Check for the kind of B-tree node to redistribute */
    if (depth > 1) {
        H5B2_internal_t *left_internal;  /* Pointer to left internal node */
        H5B2_internal_t *right_internal; /* Pointer to right internal node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_INT;

        /* Lock left & right B-tree child nodes */
        /* (Shadow both nodes if doing SWMR writes) */
        if (NULL == (left_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx + 1],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child      = left_internal;
        right_child     = right_internal;
        left_nrec       = &(left_internal->nrec);
        right_nrec      = &(right_internal->nrec);
        left_native     = left_internal->int_native;
        right_native    = right_internal->int_native;
        left_node_ptrs  = left_internal->node_ptrs;
        right_node_ptrs = right_internal->node_ptrs;
    } /* end if */
    else {
        H5B2_leaf_t *left_leaf;  /* Pointer to left leaf node */
        H5B2_leaf_t *right_leaf; /* Pointer to right leaf node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_LEAF;

        /* Lock left & right B-tree child nodes */
        /* (Shadow both nodes if doing SWMR writes) */
        if (NULL == (left_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx], hdr->swmr_write,
                                                    H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx + 1],
                                                     hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child   = left_leaf;
        right_child  = right_leaf;
        left_nrec    = &(left_leaf->nrec);
        right_nrec   = &(right_leaf->nrec);
        left_native  = left_leaf->leaf_native;
        right_native = right_leaf->leaf_native;
    } /* end else */

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1) {
        H5B2__assert_internal2(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)left_child,
                               (H5B2_internal_t *)right_child);
        H5B2__assert_internal2(internal->node_ptrs[idx + 1].all_nrec, hdr, (H5B2_internal_t *)right_child,
                               (H5B2_internal_t *)left_child);
    } /* end if */
    else {
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)left_child, (H5B2_leaf_t *)right_child);
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)right_child);
    }  /* end else */
#endif /* H5B2_DEBUG */

    /* Determine whether to shuffle records left or right */
    if (*left_nrec < *right_nrec) {
        /* Moving record from right node to left */

        uint16_t new_right_nrec =
            (uint16_t)((*left_nrec + *right_nrec) / 2); /* New number of records for right child */
        uint16_t move_nrec =
            (uint16_t)(*right_nrec - new_right_nrec); /* Number of records to move from right node to left */

        /* Copy record from parent node down into left child */
        H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec), H5B2_INT_NREC(internal, hdr, idx),
                    hdr->cls->nrec_size);

        /* See if we need to move records from right node */
        if (move_nrec > 1)
            H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, (*left_nrec + 1)),
                        H5B2_NAT_NREC(right_native, hdr, 0), hdr->cls->nrec_size * (size_t)(move_nrec - 1));

        /* Move record from right node into parent node */
        H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx), H5B2_NAT_NREC(right_native, hdr, (move_nrec - 1)),
                    hdr->cls->nrec_size);

        /* Slide records in right node down */
        HDmemmove(H5B2_NAT_NREC(right_native, hdr, 0), H5B2_NAT_NREC(right_native, hdr, move_nrec),
                  hdr->cls->nrec_size * new_right_nrec);

        /* Handle node pointers, if we have an internal node */
        if (depth > 1) {
            hsize_t  moved_nrec = move_nrec; /* Total number of records moved, for internal redistrib */
            unsigned u;                      /* Local index variable */

            /* Count the number of records being moved */
            for (u = 0; u < move_nrec; u++)
                moved_nrec += right_node_ptrs[u].all_nrec;
            H5_CHECKED_ASSIGN(left_moved_nrec, hssize_t, moved_nrec, hsize_t)
            right_moved_nrec -= (hssize_t)moved_nrec;

            /* Copy node pointers from right node to left */
            H5MM_memcpy(&(left_node_ptrs[*left_nrec + 1]), &(right_node_ptrs[0]),
                        sizeof(H5B2_node_ptr_t) * move_nrec);

            /* Slide node pointers in right node down */
            HDmemmove(&(right_node_ptrs[0]), &(right_node_ptrs[move_nrec]),
                      sizeof(H5B2_node_ptr_t) * (new_right_nrec + (unsigned)1));
        } /* end if */

        /* Update flush dependencies for grandchildren, if using SWMR */
        if (hdr->swmr_write && depth > 1)
            if (H5B2__update_child_flush_depends(hdr, depth, left_node_ptrs, (unsigned)(*left_nrec + 1),
                                                 (unsigned)(*left_nrec + move_nrec + 1), right_child,
                                                 left_child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

        /* Update number of records in child nodes */
        *left_nrec  = (uint16_t)(*left_nrec + move_nrec);
        *right_nrec = new_right_nrec;

        /* Mark nodes as dirty */
        left_child_flags |= H5AC__DIRTIED_FLAG;
        right_child_flags |= H5AC__DIRTIED_FLAG;
    } /* end if */
    else {
        /* Moving record from left node to right */

        uint16_t new_left_nrec =
            (uint16_t)((*left_nrec + *right_nrec) / 2); /* New number of records for left child */
        uint16_t move_nrec =
            (uint16_t)(*left_nrec - new_left_nrec); /* Number of records to move from left node to right */

        /* Sanity check */
        HDassert(*left_nrec > *right_nrec);

        /* Slide records in right node up */
        HDmemmove(H5B2_NAT_NREC(right_native, hdr, move_nrec), H5B2_NAT_NREC(right_native, hdr, 0),
                  hdr->cls->nrec_size * (*right_nrec));

        /* Copy record from parent node down into right child */
        H5MM_memcpy(H5B2_NAT_NREC(right_native, hdr, (move_nrec - 1)), H5B2_INT_NREC(internal, hdr, idx),
                    hdr->cls->nrec_size);

        /* See if we need to move records from left node */
        if (move_nrec > 1)
            H5MM_memcpy(H5B2_NAT_NREC(right_native, hdr, 0),
                        H5B2_NAT_NREC(left_native, hdr, ((*left_nrec - move_nrec) + 1)),
                        hdr->cls->nrec_size * (size_t)(move_nrec - 1));

        /* Move record from left node into parent node */
        H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx),
                    H5B2_NAT_NREC(left_native, hdr, (*left_nrec - move_nrec)), hdr->cls->nrec_size);

        /* Handle node pointers, if we have an internal node */
        if (depth > 1) {
            hsize_t  moved_nrec = move_nrec; /* Total number of records moved, for internal redistrib */
            unsigned u;                      /* Local index variable */

            /* Slide node pointers in right node up */
            HDmemmove(&(right_node_ptrs[move_nrec]), &(right_node_ptrs[0]),
                      sizeof(H5B2_node_ptr_t) * (size_t)(*right_nrec + 1));

            /* Copy node pointers from left node to right */
            H5MM_memcpy(&(right_node_ptrs[0]), &(left_node_ptrs[new_left_nrec + 1]),
                        sizeof(H5B2_node_ptr_t) * move_nrec);

            /* Count the number of records being moved */
            for (u = 0; u < move_nrec; u++)
                moved_nrec += right_node_ptrs[u].all_nrec;
            left_moved_nrec -= (hssize_t)moved_nrec;
            H5_CHECKED_ASSIGN(right_moved_nrec, hssize_t, moved_nrec, hsize_t)
        } /* end if */

        /* Update flush dependencies for grandchildren, if using SWMR */
        if (hdr->swmr_write && depth > 1)
            if (H5B2__update_child_flush_depends(hdr, depth, right_node_ptrs, 0, (unsigned)move_nrec,
                                                 left_child, right_child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

        /* Update number of records in child nodes */
        *left_nrec  = new_left_nrec;
        *right_nrec = (uint16_t)(*right_nrec + move_nrec);

        /* Mark nodes as dirty */
        left_child_flags |= H5AC__DIRTIED_FLAG;
        right_child_flags |= H5AC__DIRTIED_FLAG;
    } /* end else */

    /* Update # of records in child nodes */
    internal->node_ptrs[idx].node_nrec     = *left_nrec;
    internal->node_ptrs[idx + 1].node_nrec = *right_nrec;

    /* Update total # of records in child B-trees */
    if (depth > 1) {
        internal->node_ptrs[idx].all_nrec =
            (hsize_t)((hssize_t)internal->node_ptrs[idx].all_nrec + left_moved_nrec);
        internal->node_ptrs[idx + 1].all_nrec =
            (hsize_t)((hssize_t)internal->node_ptrs[idx + 1].all_nrec + right_moved_nrec);
    } /* end if */
    else {
        internal->node_ptrs[idx].all_nrec     = internal->node_ptrs[idx].node_nrec;
        internal->node_ptrs[idx + 1].all_nrec = internal->node_ptrs[idx + 1].node_nrec;
    } /* end else */

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1) {
        H5B2__assert_internal2(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)left_child,
                               (H5B2_internal_t *)right_child);
        H5B2__assert_internal2(internal->node_ptrs[idx + 1].all_nrec, hdr, (H5B2_internal_t *)right_child,
                               (H5B2_internal_t *)left_child);
    } /* end if */
    else {
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)left_child, (H5B2_leaf_t *)right_child);
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)right_child);
    }  /* end else */
#endif /* H5B2_DEBUG */

done:
    /* Release child nodes (marked as dirty) */
    if (left_child && H5AC_unprotect(hdr->f, child_class, left_addr, left_child, left_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")
    if (right_child && H5AC_unprotect(hdr->f, child_class, right_addr, right_child, right_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__redistribute2() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__redistribute3
 *
 * Purpose:	Redistribute records between three nodes
 *
 * Return:	Success:	Non-negative
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Feb  9 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__redistribute3(H5B2_hdr_t *hdr, uint16_t depth, H5B2_internal_t *internal, unsigned *internal_flags_ptr,
                    unsigned idx)
{
    H5B2_node_ptr_t *left_node_ptrs      = NULL,
                    *right_node_ptrs     = NULL;                 /* Pointers to childs' node pointer info */
    H5B2_node_ptr_t    *middle_node_ptrs = NULL;                 /* Pointers to childs' node pointer info */
    const H5AC_class_t *child_class;                             /* Pointer to child node's class info */
    haddr_t   left_addr = HADDR_UNDEF, right_addr = HADDR_UNDEF; /* Addresses of left & right child nodes */
    haddr_t   middle_addr = HADDR_UNDEF;                         /* Address of middle child node */
    void     *left_child = NULL, *right_child = NULL;            /* Pointers to child nodes */
    void     *middle_child = NULL;                               /* Pointers to middle child node */
    uint16_t *left_nrec, *right_nrec;                            /* Pointers to child # of records */
    uint16_t *middle_nrec;                                       /* Pointers to middle child # of records */
    uint8_t  *left_native, *right_native;                        /* Pointers to childs' native records */
    uint8_t  *middle_native;                             /* Pointers to middle child's native records */
    hssize_t  left_moved_nrec = 0, right_moved_nrec = 0; /* Number of records moved, for internal split */
    hssize_t  middle_moved_nrec = 0;                     /* Number of records moved, for internal split */
    unsigned  left_child_flags  = H5AC__NO_FLAGS_SET,
             right_child_flags  = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    unsigned middle_child_flags = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    herr_t   ret_value          = SUCCEED;            /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(internal);
    HDassert(internal_flags_ptr);

    /* Check for the kind of B-tree node to redistribute */
    if (depth > 1) {
        H5B2_internal_t *left_internal;   /* Pointer to left internal node */
        H5B2_internal_t *middle_internal; /* Pointer to middle internal node */
        H5B2_internal_t *right_internal;  /* Pointer to right internal node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_INT;

        /* Lock B-tree child nodes */
        /* (Shadow all nodes if doing SWMR writes) */
        if (NULL == (left_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx - 1],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        left_addr = internal->node_ptrs[idx - 1].addr;
        if (NULL == (middle_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        middle_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx + 1],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child       = left_internal;
        middle_child     = middle_internal;
        right_child      = right_internal;
        left_nrec        = &(left_internal->nrec);
        middle_nrec      = &(middle_internal->nrec);
        right_nrec       = &(right_internal->nrec);
        left_native      = left_internal->int_native;
        middle_native    = middle_internal->int_native;
        right_native     = right_internal->int_native;
        left_node_ptrs   = left_internal->node_ptrs;
        middle_node_ptrs = middle_internal->node_ptrs;
        right_node_ptrs  = right_internal->node_ptrs;
    } /* end if */
    else {
        H5B2_leaf_t *left_leaf;   /* Pointer to left leaf node */
        H5B2_leaf_t *middle_leaf; /* Pointer to middle leaf node */
        H5B2_leaf_t *right_leaf;  /* Pointer to right leaf node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_LEAF;

        /* Lock B-tree child nodes */
        /* (Shadow all nodes if doing SWMR writes) */
        if (NULL == (left_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx - 1],
                                                    hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx - 1].addr;
        if (NULL == (middle_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx],
                                                      hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        middle_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx + 1],
                                                     hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for child nodes */
        left_child    = left_leaf;
        middle_child  = middle_leaf;
        right_child   = right_leaf;
        left_nrec     = &(left_leaf->nrec);
        middle_nrec   = &(middle_leaf->nrec);
        right_nrec    = &(right_leaf->nrec);
        left_native   = left_leaf->leaf_native;
        middle_native = middle_leaf->leaf_native;
        right_native  = right_leaf->leaf_native;
    } /* end else */

    /* Redistribute records */
    {
        /* Compute new # of records in each node */
        unsigned total_nrec      = (unsigned)(*left_nrec + *middle_nrec + *right_nrec + 2);
        uint16_t new_middle_nrec = (uint16_t)((total_nrec - 2) / 3);
        uint16_t new_left_nrec   = (uint16_t)(((total_nrec - 2) - new_middle_nrec) / 2);
        uint16_t new_right_nrec  = (uint16_t)((total_nrec - 2) - (unsigned)(new_left_nrec + new_middle_nrec));
        uint16_t curr_middle_nrec = *middle_nrec;

        /* Sanity check rounding */
        HDassert(new_middle_nrec <= new_left_nrec);
        HDassert(new_middle_nrec <= new_right_nrec);

        /* Move records into left node */
        if (new_left_nrec > *left_nrec) {
            uint16_t moved_middle_nrec = 0; /* Number of records moved into left node */

            /* Move left parent record down to left node */
            H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec), H5B2_INT_NREC(internal, hdr, idx - 1),
                        hdr->cls->nrec_size);

            /* Move records from middle node into left node */
            if ((new_left_nrec - 1) > *left_nrec) {
                moved_middle_nrec = (uint16_t)(new_left_nrec - (*left_nrec + 1));
                H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec + 1),
                            H5B2_NAT_NREC(middle_native, hdr, 0), hdr->cls->nrec_size * moved_middle_nrec);
            } /* end if */

            /* Move record from middle node up to parent node */
            H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx - 1),
                        H5B2_NAT_NREC(middle_native, hdr, moved_middle_nrec), hdr->cls->nrec_size);
            moved_middle_nrec++;

            /* Slide records in middle node down */
            HDmemmove(H5B2_NAT_NREC(middle_native, hdr, 0),
                      H5B2_NAT_NREC(middle_native, hdr, moved_middle_nrec),
                      hdr->cls->nrec_size * (size_t)(*middle_nrec - moved_middle_nrec));

            /* Move node pointers also if this is an internal node */
            if (depth > 1) {
                hsize_t  moved_nrec; /* Total number of records moved, for internal redistrib */
                unsigned move_nptrs; /* Number of node pointers to move */
                unsigned u;          /* Local index variable */

                /* Move middle node pointers into left node */
                move_nptrs = (unsigned)(new_left_nrec - *left_nrec);
                H5MM_memcpy(&(left_node_ptrs[*left_nrec + 1]), &(middle_node_ptrs[0]),
                            sizeof(H5B2_node_ptr_t) * move_nptrs);

                /* Count the number of records being moved into the left node */
                for (u = 0, moved_nrec = 0; u < move_nptrs; u++)
                    moved_nrec += middle_node_ptrs[u].all_nrec;
                left_moved_nrec = (hssize_t)(moved_nrec + move_nptrs);
                middle_moved_nrec -= (hssize_t)(moved_nrec + move_nptrs);

                /* Slide the node pointers in middle node down */
                HDmemmove(&(middle_node_ptrs[0]), &(middle_node_ptrs[move_nptrs]),
                          sizeof(H5B2_node_ptr_t) * ((*middle_nrec - move_nptrs) + 1));
            } /* end if */

            /* Update flush dependencies for grandchildren, if using SWMR */
            if (hdr->swmr_write && depth > 1)
                if (H5B2__update_child_flush_depends(hdr, depth, left_node_ptrs, (unsigned)(*left_nrec + 1),
                                                     (unsigned)(*left_nrec + moved_middle_nrec + 1),
                                                     middle_child, left_child) < 0)
                    HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

            /* Update the current number of records in middle node */
            curr_middle_nrec = (uint16_t)(curr_middle_nrec - moved_middle_nrec);

            /* Mark nodes as dirty */
            left_child_flags |= H5AC__DIRTIED_FLAG;
            middle_child_flags |= H5AC__DIRTIED_FLAG;
        } /* end if */

        /* Move records into right node */
        if (new_right_nrec > *right_nrec) {
            unsigned right_nrec_move =
                (unsigned)(new_right_nrec - *right_nrec); /* Number of records to move out of right node */

            /* Slide records in right node up */
            HDmemmove(H5B2_NAT_NREC(right_native, hdr, right_nrec_move), H5B2_NAT_NREC(right_native, hdr, 0),
                      hdr->cls->nrec_size * (*right_nrec));

            /* Move right parent record down to right node */
            H5MM_memcpy(H5B2_NAT_NREC(right_native, hdr, right_nrec_move - 1),
                        H5B2_INT_NREC(internal, hdr, idx), hdr->cls->nrec_size);

            /* Move records from middle node into right node */
            if (right_nrec_move > 1)
                H5MM_memcpy(H5B2_NAT_NREC(right_native, hdr, 0),
                            H5B2_NAT_NREC(middle_native, hdr, ((curr_middle_nrec - right_nrec_move) + 1)),
                            hdr->cls->nrec_size * (right_nrec_move - 1));

            /* Move record from middle node up to parent node */
            H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx),
                        H5B2_NAT_NREC(middle_native, hdr, (curr_middle_nrec - right_nrec_move)),
                        hdr->cls->nrec_size);

            /* Move node pointers also if this is an internal node */
            if (depth > 1) {
                hsize_t  moved_nrec; /* Total number of records moved, for internal redistrib */
                unsigned u;          /* Local index variable */

                /* Slide the node pointers in right node up */
                HDmemmove(&(right_node_ptrs[right_nrec_move]), &(right_node_ptrs[0]),
                          sizeof(H5B2_node_ptr_t) * (size_t)(*right_nrec + 1));

                /* Move middle node pointers into right node */
                H5MM_memcpy(&(right_node_ptrs[0]),
                            &(middle_node_ptrs[(curr_middle_nrec - right_nrec_move) + 1]),
                            sizeof(H5B2_node_ptr_t) * right_nrec_move);

                /* Count the number of records being moved into the right node */
                for (u = 0, moved_nrec = 0; u < right_nrec_move; u++)
                    moved_nrec += right_node_ptrs[u].all_nrec;
                right_moved_nrec = (hssize_t)(moved_nrec + right_nrec_move);
                middle_moved_nrec -= (hssize_t)(moved_nrec + right_nrec_move);
            } /* end if */

            /* Update flush dependencies for grandchildren, if using SWMR */
            if (hdr->swmr_write && depth > 1)
                if (H5B2__update_child_flush_depends(hdr, depth, right_node_ptrs, 0,
                                                     (unsigned)right_nrec_move, middle_child,
                                                     right_child) < 0)
                    HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

            /* Update the current number of records in middle node */
            curr_middle_nrec = (uint16_t)(curr_middle_nrec - right_nrec_move);

            /* Mark nodes as dirty */
            middle_child_flags |= H5AC__DIRTIED_FLAG;
            right_child_flags |= H5AC__DIRTIED_FLAG;
        } /* end if */

        /* Move records out of left node */
        if (new_left_nrec < *left_nrec) {
            unsigned left_nrec_move =
                (unsigned)(*left_nrec - new_left_nrec); /* Number of records to move out of left node */

            /* Slide middle records up */
            HDmemmove(H5B2_NAT_NREC(middle_native, hdr, left_nrec_move), H5B2_NAT_NREC(middle_native, hdr, 0),
                      hdr->cls->nrec_size * curr_middle_nrec);

            /* Move left parent record down to middle node */
            H5MM_memcpy(H5B2_NAT_NREC(middle_native, hdr, left_nrec_move - 1),
                        H5B2_INT_NREC(internal, hdr, idx - 1), hdr->cls->nrec_size);

            /* Move left records to middle node */
            if (left_nrec_move > 1)
                HDmemmove(H5B2_NAT_NREC(middle_native, hdr, 0),
                          H5B2_NAT_NREC(left_native, hdr, new_left_nrec + 1),
                          hdr->cls->nrec_size * (left_nrec_move - 1));

            /* Move left parent record up from left node */
            H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx - 1), H5B2_NAT_NREC(left_native, hdr, new_left_nrec),
                        hdr->cls->nrec_size);

            /* Move node pointers also if this is an internal node */
            if (depth > 1) {
                hsize_t  moved_nrec; /* Total number of records moved, for internal redistrib */
                unsigned u;          /* Local index variable */

                /* Slide the node pointers in middle node up */
                HDmemmove(&(middle_node_ptrs[left_nrec_move]), &(middle_node_ptrs[0]),
                          sizeof(H5B2_node_ptr_t) * (size_t)(curr_middle_nrec + 1));

                /* Move left node pointers into middle node */
                H5MM_memcpy(&(middle_node_ptrs[0]), &(left_node_ptrs[new_left_nrec + 1]),
                            sizeof(H5B2_node_ptr_t) * left_nrec_move);

                /* Count the number of records being moved into the left node */
                for (u = 0, moved_nrec = 0; u < left_nrec_move; u++)
                    moved_nrec += middle_node_ptrs[u].all_nrec;
                left_moved_nrec -= (hssize_t)(moved_nrec + left_nrec_move);
                middle_moved_nrec += (hssize_t)(moved_nrec + left_nrec_move);
            } /* end if */

            /* Update flush dependencies for grandchildren, if using SWMR */
            if (hdr->swmr_write && depth > 1)
                if (H5B2__update_child_flush_depends(hdr, depth, middle_node_ptrs, 0,
                                                     (unsigned)left_nrec_move, left_child, middle_child) < 0)
                    HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

            /* Update the current number of records in middle node */
            curr_middle_nrec = (uint16_t)(curr_middle_nrec + left_nrec_move);

            /* Mark nodes as dirty */
            left_child_flags |= H5AC__DIRTIED_FLAG;
            middle_child_flags |= H5AC__DIRTIED_FLAG;
        } /* end if */

        /* Move records out of right node */
        if (new_right_nrec < *right_nrec) {
            unsigned right_nrec_move =
                (unsigned)(*right_nrec - new_right_nrec); /* Number of records to move out of right node */

            /* Move right parent record down to middle node */
            H5MM_memcpy(H5B2_NAT_NREC(middle_native, hdr, curr_middle_nrec),
                        H5B2_INT_NREC(internal, hdr, idx), hdr->cls->nrec_size);

            /* Move right records to middle node */
            HDmemmove(H5B2_NAT_NREC(middle_native, hdr, (curr_middle_nrec + 1)),
                      H5B2_NAT_NREC(right_native, hdr, 0), hdr->cls->nrec_size * (right_nrec_move - 1));

            /* Move right parent record up from right node */
            H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx),
                        H5B2_NAT_NREC(right_native, hdr, right_nrec_move - 1), hdr->cls->nrec_size);

            /* Slide right records down */
            HDmemmove(H5B2_NAT_NREC(right_native, hdr, 0), H5B2_NAT_NREC(right_native, hdr, right_nrec_move),
                      hdr->cls->nrec_size * new_right_nrec);

            /* Move node pointers also if this is an internal node */
            if (depth > 1) {
                hsize_t  moved_nrec; /* Total number of records moved, for internal redistrib */
                unsigned u;          /* Local index variable */

                /* Move right node pointers into middle node */
                H5MM_memcpy(&(middle_node_ptrs[curr_middle_nrec + 1]), &(right_node_ptrs[0]),
                            sizeof(H5B2_node_ptr_t) * right_nrec_move);

                /* Count the number of records being moved into the right node */
                for (u = 0, moved_nrec = 0; u < right_nrec_move; u++)
                    moved_nrec += right_node_ptrs[u].all_nrec;
                right_moved_nrec -= (hssize_t)(moved_nrec + right_nrec_move);
                middle_moved_nrec += (hssize_t)(moved_nrec + right_nrec_move);

                /* Slide the node pointers in right node down */
                HDmemmove(&(right_node_ptrs[0]), &(right_node_ptrs[right_nrec_move]),
                          sizeof(H5B2_node_ptr_t) * (size_t)(new_right_nrec + 1));
            } /* end if */

            /* Update flush dependencies for grandchildren, if using SWMR */
            if (hdr->swmr_write && depth > 1)
                if (H5B2__update_child_flush_depends(
                        hdr, depth, middle_node_ptrs, (unsigned)(curr_middle_nrec + 1),
                        (unsigned)(curr_middle_nrec + right_nrec_move + 1), right_child, middle_child) < 0)
                    HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

            /* Mark nodes as dirty */
            middle_child_flags |= H5AC__DIRTIED_FLAG;
            right_child_flags |= H5AC__DIRTIED_FLAG;
        } /* end if */

        /* Update # of records in nodes */
        *left_nrec   = new_left_nrec;
        *middle_nrec = new_middle_nrec;
        *right_nrec  = new_right_nrec;
    } /* end block */

    /* Update # of records in child nodes */
    internal->node_ptrs[idx - 1].node_nrec = *left_nrec;
    internal->node_ptrs[idx].node_nrec     = *middle_nrec;
    internal->node_ptrs[idx + 1].node_nrec = *right_nrec;

    /* Update total # of records in child B-trees */
    if (depth > 1) {
        internal->node_ptrs[idx - 1].all_nrec =
            (hsize_t)((hssize_t)internal->node_ptrs[idx - 1].all_nrec + left_moved_nrec);
        internal->node_ptrs[idx].all_nrec =
            (hsize_t)((hssize_t)internal->node_ptrs[idx].all_nrec + middle_moved_nrec);
        internal->node_ptrs[idx + 1].all_nrec =
            (hsize_t)((hssize_t)internal->node_ptrs[idx + 1].all_nrec + right_moved_nrec);
    } /* end if */
    else {
        internal->node_ptrs[idx - 1].all_nrec = internal->node_ptrs[idx - 1].node_nrec;
        internal->node_ptrs[idx].all_nrec     = internal->node_ptrs[idx].node_nrec;
        internal->node_ptrs[idx + 1].all_nrec = internal->node_ptrs[idx + 1].node_nrec;
    } /* end else */

    /* Mark parent as dirty */
    *internal_flags_ptr |= H5AC__DIRTIED_FLAG;

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1) {
        H5B2__assert_internal2(internal->node_ptrs[idx - 1].all_nrec, hdr, (H5B2_internal_t *)left_child,
                               (H5B2_internal_t *)middle_child);
        H5B2__assert_internal2(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)middle_child,
                               (H5B2_internal_t *)left_child);
        H5B2__assert_internal2(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)middle_child,
                               (H5B2_internal_t *)right_child);
        H5B2__assert_internal2(internal->node_ptrs[idx + 1].all_nrec, hdr, (H5B2_internal_t *)right_child,
                               (H5B2_internal_t *)middle_child);
    } /* end if */
    else {
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)left_child, (H5B2_leaf_t *)middle_child);
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)middle_child, (H5B2_leaf_t *)right_child);
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)right_child);
    }  /* end else */
#endif /* H5B2_DEBUG */

done:
    /* Unlock child nodes (marked as dirty) */
    if (left_child && H5AC_unprotect(hdr->f, child_class, left_addr, left_child, left_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")
    if (middle_child &&
        H5AC_unprotect(hdr->f, child_class, middle_addr, middle_child, middle_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")
    if (right_child && H5AC_unprotect(hdr->f, child_class, right_addr, right_child, right_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__redistribute3() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__merge2
 *
 * Purpose:	Perform a 2->1 node merge
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Mar  4 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__merge2(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node_ptr,
             unsigned *parent_cache_info_flags_ptr, H5B2_internal_t *internal, unsigned *internal_flags_ptr,
             unsigned idx)
{
    const H5AC_class_t *child_class;                             /* Pointer to child node's class info */
    haddr_t   left_addr = HADDR_UNDEF, right_addr = HADDR_UNDEF; /* Addresses of left & right child nodes */
    void     *left_child = NULL, *right_child = NULL;            /* Pointers to left & right child nodes */
    uint16_t *left_nrec, *right_nrec;     /* Pointers to left & right child # of records */
    uint8_t  *left_native, *right_native; /* Pointers to left & right children's native records */
    H5B2_node_ptr_t *left_node_ptrs  = NULL,
                    *right_node_ptrs = NULL; /* Pointers to childs' node pointer info */
    unsigned left_child_flags        = H5AC__NO_FLAGS_SET,
             right_child_flags       = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    herr_t ret_value                 = SUCCEED;            /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(curr_node_ptr);
    HDassert(internal);
    HDassert(internal_flags_ptr);

    /* Check for the kind of B-tree node to split */
    if (depth > 1) {
        H5B2_internal_t *left_internal;  /* Pointer to left internal node */
        H5B2_internal_t *right_internal; /* Pointer to right internal node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_INT;

        /* Lock left & right B-tree child nodes */
        /* (Shadow the left node if doing SWMR writes) */
        if (NULL == (left_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL ==
            (right_internal = H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx + 1],
                                                     (uint16_t)(depth - 1), FALSE, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for accessing child node information */
        left_child      = left_internal;
        right_child     = right_internal;
        left_nrec       = &(left_internal->nrec);
        right_nrec      = &(right_internal->nrec);
        left_native     = left_internal->int_native;
        right_native    = right_internal->int_native;
        left_node_ptrs  = left_internal->node_ptrs;
        right_node_ptrs = right_internal->node_ptrs;
    } /* end if */
    else {
        H5B2_leaf_t *left_leaf;  /* Pointer to left leaf node */
        H5B2_leaf_t *right_leaf; /* Pointer to right leaf node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_LEAF;

        /* Lock left & right B-tree child nodes */
        /* (Shadow the left node if doing SWMR writes) */
        if (NULL == (left_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx], hdr->swmr_write,
                                                    H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx + 1], FALSE,
                                                     H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for accessing child node information */
        left_child   = left_leaf;
        right_child  = right_leaf;
        left_nrec    = &(left_leaf->nrec);
        right_nrec   = &(right_leaf->nrec);
        left_native  = left_leaf->leaf_native;
        right_native = right_leaf->leaf_native;
    } /* end else */

    /* Redistribute records into left node */
    {
        /* Copy record from parent node to proper location */
        H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec), H5B2_INT_NREC(internal, hdr, idx),
                    hdr->cls->nrec_size);

        /* Copy records from right node to left node */
        H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec + 1), H5B2_NAT_NREC(right_native, hdr, 0),
                    hdr->cls->nrec_size * (*right_nrec));

        /* Copy node pointers from right node into left node */
        if (depth > 1)
            H5MM_memcpy(&(left_node_ptrs[*left_nrec + 1]), &(right_node_ptrs[0]),
                        sizeof(H5B2_node_ptr_t) * (size_t)(*right_nrec + 1));

        /* Update flush dependencies for grandchildren, if using SWMR */
        if (hdr->swmr_write && depth > 1)
            if (H5B2__update_child_flush_depends(hdr, depth, left_node_ptrs, (unsigned)(*left_nrec + 1),
                                                 (unsigned)(*left_nrec + *right_nrec + 2), right_child,
                                                 left_child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

        /* Update # of records in left node */
        *left_nrec = (uint16_t)(*left_nrec + *right_nrec + 1);

        /* Mark nodes as dirty */
        left_child_flags |= H5AC__DIRTIED_FLAG;
        right_child_flags |= H5AC__DELETED_FLAG;
        if (!(hdr->swmr_write))
            right_child_flags |= H5AC__DIRTIED_FLAG | H5AC__FREE_FILE_SPACE_FLAG;
    } /* end block */

    /* Update # of records in child nodes */
    internal->node_ptrs[idx].node_nrec = *left_nrec;

    /* Update total # of records in child B-trees */
    internal->node_ptrs[idx].all_nrec += internal->node_ptrs[idx + 1].all_nrec + 1;

    /* Slide records in parent node down, to eliminate demoted record */
    if ((idx + 1) < internal->nrec) {
        HDmemmove(H5B2_INT_NREC(internal, hdr, idx), H5B2_INT_NREC(internal, hdr, idx + 1),
                  hdr->cls->nrec_size * (internal->nrec - (idx + 1)));
        HDmemmove(&(internal->node_ptrs[idx + 1]), &(internal->node_ptrs[idx + 2]),
                  sizeof(H5B2_node_ptr_t) * (internal->nrec - (idx + 1)));
    } /* end if */

    /* Update # of records in parent node */
    internal->nrec--;

    /* Mark parent as dirty */
    *internal_flags_ptr |= H5AC__DIRTIED_FLAG;

    /* Update grandparent info */
    curr_node_ptr->node_nrec--;

    /* Mark grandparent as dirty, if given */
    if (parent_cache_info_flags_ptr)
        *parent_cache_info_flags_ptr |= H5AC__DIRTIED_FLAG;

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1)
        H5B2__assert_internal(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)left_child);
    else
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)left_child);
#endif /* H5B2_DEBUG */

done:
    /* Unlock left node (marked as dirty) */
    if (left_child && H5AC_unprotect(hdr->f, child_class, left_addr, left_child, left_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    /* Delete right node & remove from cache (marked as dirty) */
    if (right_child && H5AC_unprotect(hdr->f, child_class, right_addr, right_child, right_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__merge2() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__merge3
 *
 * Purpose:	Perform a 3->2 node merge
 *
 * Return:	Success:	Non-negative
 *
 *		Failure:	Negative
 *
 * Programmer:	Quincey Koziol
 *		Mar  4 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__merge3(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node_ptr,
             unsigned *parent_cache_info_flags_ptr, H5B2_internal_t *internal, unsigned *internal_flags_ptr,
             unsigned idx)
{
    const H5AC_class_t *child_class;                             /* Pointer to child node's class info */
    haddr_t   left_addr = HADDR_UNDEF, right_addr = HADDR_UNDEF; /* Addresses of left & right child nodes */
    haddr_t   middle_addr = HADDR_UNDEF;                         /* Address of middle child node */
    void     *left_child = NULL, *right_child = NULL;            /* Pointers to left & right child nodes */
    void     *middle_child = NULL;                               /* Pointer to middle child node */
    uint16_t *left_nrec, *right_nrec;     /* Pointers to left & right child # of records */
    uint16_t *middle_nrec;                /* Pointer to middle child # of records */
    uint8_t  *left_native, *right_native; /* Pointers to left & right children's native records */
    uint8_t  *middle_native;              /* Pointer to middle child's native records */
    H5B2_node_ptr_t *left_node_ptrs   = NULL,
                    *right_node_ptrs  = NULL; /* Pointers to childs' node pointer info */
    H5B2_node_ptr_t *middle_node_ptrs = NULL; /* Pointer to child's node pointer info */
    hsize_t          middle_moved_nrec;       /* Number of records moved, for internal split */
    unsigned         left_child_flags = H5AC__NO_FLAGS_SET,
             right_child_flags        = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    unsigned middle_child_flags       = H5AC__NO_FLAGS_SET; /* Flags for unprotecting child nodes */
    herr_t   ret_value                = SUCCEED;            /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(curr_node_ptr);
    HDassert(internal);
    HDassert(internal_flags_ptr);

    /* Check for the kind of B-tree node to split */
    if (depth > 1) {
        H5B2_internal_t *left_internal;   /* Pointer to left internal node */
        H5B2_internal_t *middle_internal; /* Pointer to middle internal node */
        H5B2_internal_t *right_internal;  /* Pointer to right internal node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_INT;

        /* Lock B-tree child nodes */
        /* (Shadow left and middle nodes if doing SWMR writes) */
        if (NULL == (left_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx - 1],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        left_addr = internal->node_ptrs[idx - 1].addr;
        if (NULL == (middle_internal =
                         H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx],
                                                (uint16_t)(depth - 1), hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        middle_addr = internal->node_ptrs[idx].addr;
        if (NULL ==
            (right_internal = H5B2__protect_internal(hdr, internal, &internal->node_ptrs[idx + 1],
                                                     (uint16_t)(depth - 1), FALSE, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for accessing child node information */
        left_child       = left_internal;
        middle_child     = middle_internal;
        right_child      = right_internal;
        left_nrec        = &(left_internal->nrec);
        middle_nrec      = &(middle_internal->nrec);
        right_nrec       = &(right_internal->nrec);
        left_native      = left_internal->int_native;
        middle_native    = middle_internal->int_native;
        right_native     = right_internal->int_native;
        left_node_ptrs   = left_internal->node_ptrs;
        middle_node_ptrs = middle_internal->node_ptrs;
        right_node_ptrs  = right_internal->node_ptrs;
    } /* end if */
    else {
        H5B2_leaf_t *left_leaf;   /* Pointer to left leaf node */
        H5B2_leaf_t *middle_leaf; /* Pointer to middle leaf node */
        H5B2_leaf_t *right_leaf;  /* Pointer to right leaf node */

        /* Setup information for unlocking child nodes */
        child_class = H5AC_BT2_LEAF;

        /* Lock B-tree child nodes */
        /* (Shadow left and middle nodes if doing SWMR writes) */
        if (NULL == (left_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx - 1],
                                                    hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        left_addr = internal->node_ptrs[idx - 1].addr;
        if (NULL == (middle_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx],
                                                      hdr->swmr_write, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        middle_addr = internal->node_ptrs[idx].addr;
        if (NULL == (right_leaf = H5B2__protect_leaf(hdr, internal, &internal->node_ptrs[idx + 1], FALSE,
                                                     H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
        right_addr = internal->node_ptrs[idx + 1].addr;

        /* More setup for accessing child node information */
        left_child    = left_leaf;
        middle_child  = middle_leaf;
        right_child   = right_leaf;
        left_nrec     = &(left_leaf->nrec);
        middle_nrec   = &(middle_leaf->nrec);
        right_nrec    = &(right_leaf->nrec);
        left_native   = left_leaf->leaf_native;
        middle_native = middle_leaf->leaf_native;
        right_native  = right_leaf->leaf_native;
    } /* end else */

    /* Redistribute records into left node */
    {
        unsigned total_nrec       = (unsigned)(*left_nrec + *middle_nrec + *right_nrec + 2);
        unsigned middle_nrec_move = ((total_nrec - 1) / 2) - *left_nrec;

        /* Set the base number of records moved from middle node */
        middle_moved_nrec = middle_nrec_move;

        /* Copy record from parent node to proper location in left node */
        H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec), H5B2_INT_NREC(internal, hdr, idx - 1),
                    hdr->cls->nrec_size);

        /* Copy records from middle node to left node */
        H5MM_memcpy(H5B2_NAT_NREC(left_native, hdr, *left_nrec + 1), H5B2_NAT_NREC(middle_native, hdr, 0),
                    hdr->cls->nrec_size * (middle_nrec_move - 1));

        /* Copy record from middle node to proper location in parent node */
        H5MM_memcpy(H5B2_INT_NREC(internal, hdr, idx - 1),
                    H5B2_NAT_NREC(middle_native, hdr, (middle_nrec_move - 1)), hdr->cls->nrec_size);

        /* Slide records in middle node down */
        HDmemmove(H5B2_NAT_NREC(middle_native, hdr, 0), H5B2_NAT_NREC(middle_native, hdr, middle_nrec_move),
                  hdr->cls->nrec_size * (*middle_nrec - middle_nrec_move));

        /* Move node pointers also if this is an internal node */
        if (depth > 1) {
            unsigned u; /* Local index variable */

            /* Copy node pointers from middle node into left node */
            H5MM_memcpy(&(left_node_ptrs[*left_nrec + 1]), &(middle_node_ptrs[0]),
                        sizeof(H5B2_node_ptr_t) * middle_nrec_move);

            /* Count the number of records being moved into the left node */
            for (u = 0; u < middle_nrec_move; u++)
                middle_moved_nrec += middle_node_ptrs[u].all_nrec;

            /* Slide the node pointers in middle node down */
            HDmemmove(&(middle_node_ptrs[0]), &(middle_node_ptrs[middle_nrec_move]),
                      sizeof(H5B2_node_ptr_t) * (size_t)((unsigned)(*middle_nrec + 1) - middle_nrec_move));
        } /* end if */

        /* Update flush dependencies for grandchildren, if using SWMR */
        if (hdr->swmr_write && depth > 1)
            if (H5B2__update_child_flush_depends(hdr, depth, left_node_ptrs, (unsigned)(*left_nrec + 1),
                                                 (unsigned)(*left_nrec + middle_nrec_move + 1), middle_child,
                                                 left_child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

        /* Update # of records in left & middle nodes */
        *left_nrec   = (uint16_t)(*left_nrec + middle_nrec_move);
        *middle_nrec = (uint16_t)(*middle_nrec - middle_nrec_move);

        /* Mark nodes as dirty */
        left_child_flags |= H5AC__DIRTIED_FLAG;
        middle_child_flags |= H5AC__DIRTIED_FLAG;
    } /* end block */

    /* Redistribute records into middle node */
    {
        /* Copy record from parent node to proper location in middle node */
        H5MM_memcpy(H5B2_NAT_NREC(middle_native, hdr, *middle_nrec), H5B2_INT_NREC(internal, hdr, idx),
                    hdr->cls->nrec_size);

        /* Copy records from right node to middle node */
        H5MM_memcpy(H5B2_NAT_NREC(middle_native, hdr, *middle_nrec + 1), H5B2_NAT_NREC(right_native, hdr, 0),
                    hdr->cls->nrec_size * (*right_nrec));

        /* Move node pointers also if this is an internal node */
        if (depth > 1)
            /* Copy node pointers from right node into middle node */
            H5MM_memcpy(&(middle_node_ptrs[*middle_nrec + 1]), &(right_node_ptrs[0]),
                        sizeof(H5B2_node_ptr_t) * (size_t)(*right_nrec + 1));

        /* Update flush dependencies for grandchildren, if using SWMR */
        if (hdr->swmr_write && depth > 1)
            if (H5B2__update_child_flush_depends(hdr, depth, middle_node_ptrs, (unsigned)(*middle_nrec + 1),
                                                 (unsigned)(*middle_nrec + *right_nrec + 2), right_child,
                                                 middle_child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child nodes to new parent")

        /* Update # of records in middle node */
        *middle_nrec = (uint16_t)(*middle_nrec + (*right_nrec + 1));

        /* Mark nodes as dirty */
        middle_child_flags |= H5AC__DIRTIED_FLAG;
        right_child_flags |= H5AC__DELETED_FLAG;
        if (!(hdr->swmr_write))
            right_child_flags |= H5AC__DIRTIED_FLAG | H5AC__FREE_FILE_SPACE_FLAG;
    } /* end block */

    /* Update # of records in child nodes */
    internal->node_ptrs[idx - 1].node_nrec = *left_nrec;
    internal->node_ptrs[idx].node_nrec     = *middle_nrec;

    /* Update total # of records in child B-trees */
    internal->node_ptrs[idx - 1].all_nrec += middle_moved_nrec;
    internal->node_ptrs[idx].all_nrec += (internal->node_ptrs[idx + 1].all_nrec + 1) - middle_moved_nrec;

    /* Slide records in parent node down, to eliminate demoted record */
    if ((idx + 1) < internal->nrec) {
        HDmemmove(H5B2_INT_NREC(internal, hdr, idx), H5B2_INT_NREC(internal, hdr, idx + 1),
                  hdr->cls->nrec_size * (internal->nrec - (idx + 1)));
        HDmemmove(&(internal->node_ptrs[idx + 1]), &(internal->node_ptrs[idx + 2]),
                  sizeof(H5B2_node_ptr_t) * (internal->nrec - (idx + 1)));
    } /* end if */

    /* Update # of records in parent node */
    internal->nrec--;

    /* Mark parent as dirty */
    *internal_flags_ptr |= H5AC__DIRTIED_FLAG;

    /* Update grandparent info */
    curr_node_ptr->node_nrec--;

    /* Mark grandparent as dirty, if given */
    if (parent_cache_info_flags_ptr)
        *parent_cache_info_flags_ptr |= H5AC__DIRTIED_FLAG;

#ifdef H5B2_DEBUG
    H5B2__assert_internal((hsize_t)0, hdr, internal);
    if (depth > 1) {
        H5B2__assert_internal2(internal->node_ptrs[idx - 1].all_nrec, hdr, (H5B2_internal_t *)left_child,
                               (H5B2_internal_t *)middle_child);
        H5B2__assert_internal(internal->node_ptrs[idx].all_nrec, hdr, (H5B2_internal_t *)middle_child);
    } /* end if */
    else {
        H5B2__assert_leaf2(hdr, (H5B2_leaf_t *)left_child, (H5B2_leaf_t *)middle_child);
        H5B2__assert_leaf(hdr, (H5B2_leaf_t *)middle_child);
    }  /* end else */
#endif /* H5B2_DEBUG */

done:
    /* Unlock left & middle nodes (marked as dirty) */
    if (left_child && H5AC_unprotect(hdr->f, child_class, left_addr, left_child, left_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")
    if (middle_child &&
        H5AC_unprotect(hdr->f, child_class, middle_addr, middle_child, middle_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    /* Delete right node & remove from cache (marked as dirty) */
    if (right_child && H5AC_unprotect(hdr->f, child_class, right_addr, right_child, right_child_flags) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree child node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__merge3() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__insert
 *
 * Purpose:	Adds a new record to the B-tree.
 *
 * Return:	Non-negative on success/Negative on failure
 *
 * Programmer:	Quincey Koziol
 *		Dec 23 2015
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__insert(H5B2_hdr_t *hdr, void *udata)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(udata);

    /* Check if the root node is allocated yet */
    if (!H5F_addr_defined(hdr->root.addr)) {
        /* Create root node as leaf node in B-tree */
        if (H5B2__create_leaf(hdr, hdr, &(hdr->root)) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINIT, FAIL, "unable to create root node")
    } /* end if */
    /* Check if we need to split the root node (equiv. to a 1->2 node split) */
    else if (hdr->root.node_nrec == hdr->node_info[hdr->depth].split_nrec) {
        /* Split root node */
        if (H5B2__split_root(hdr) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTSPLIT, FAIL, "unable to split root node")
    } /* end if */

    /* Attempt to insert record into B-tree */
    if (hdr->depth > 0) {
        if (H5B2__insert_internal(hdr, hdr->depth, NULL, &hdr->root, H5B2_POS_ROOT, hdr, udata) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, FAIL, "unable to insert record into B-tree internal node")
    } /* end if */
    else {
        if (H5B2__insert_leaf(hdr, &hdr->root, H5B2_POS_ROOT, hdr, udata) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTINSERT, FAIL, "unable to insert record into B-tree leaf node")
    } /* end else */

    /* Mark B-tree header as dirty */
    if (H5B2__hdr_dirty(hdr) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTMARKDIRTY, FAIL, "unable to mark B-tree header dirty")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* H5B2__insert() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__iterate_node
 *
 * Purpose:	Iterate over all the records from a B-tree node, in "in-order"
 *		order, making a callback for each record.
 *
 *              If the callback returns non-zero, the iteration breaks out
 *              without finishing all the records.
 *
 * Return:	Value from callback, non-negative on success, negative on error
 *
 * Programmer:	Quincey Koziol
 *		Feb 11 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__iterate_node(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node, void *parent,
                   H5B2_operator_t op, void *op_data)
{
    const H5AC_class_t *curr_node_class = NULL;   /* Pointer to current node's class info */
    void               *node            = NULL;   /* Pointers to current node */
    uint8_t            *node_native;              /* Pointers to node's native records */
    uint8_t            *native      = NULL;       /* Pointers to copy of node's native records */
    H5B2_node_ptr_t    *node_ptrs   = NULL;       /* Pointers to node's node pointers */
    hbool_t             node_pinned = FALSE;      /* Whether node is pinned */
    unsigned            u;                        /* Local index */
    herr_t              ret_value = H5_ITER_CONT; /* Iterator return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(curr_node);
    HDassert(op);

    /* Protect current node & set up variables */
    if (depth > 0) {
        H5B2_internal_t *internal; /* Pointer to internal node */

        /* Lock the current B-tree node */
        if (NULL ==
            (internal = H5B2__protect_internal(hdr, parent, curr_node, depth, FALSE, H5AC__READ_ONLY_FLAG)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")

        /* Set up information about current node */
        curr_node_class = H5AC_BT2_INT;
        node            = internal;
        node_native     = internal->int_native;

        /* Allocate space for the node pointers in memory */
        if (NULL == (node_ptrs = (H5B2_node_ptr_t *)H5FL_FAC_MALLOC(hdr->node_info[depth].node_ptr_fac)))
            HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
                        "memory allocation failed for B-tree internal node pointers")

        /* Copy the node pointers */
        H5MM_memcpy(node_ptrs, internal->node_ptrs,
                    (sizeof(H5B2_node_ptr_t) * (size_t)(curr_node->node_nrec + 1)));
    } /* end if */
    else {
        H5B2_leaf_t *leaf; /* Pointer to leaf node */

        /* Lock the current B-tree node */
        if (NULL == (leaf = H5B2__protect_leaf(hdr, parent, (H5B2_node_ptr_t *)curr_node, FALSE,
                                               H5AC__READ_ONLY_FLAG)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")

        /* Set up information about current node */
        curr_node_class = H5AC_BT2_LEAF;
        node            = leaf;
        node_native     = leaf->leaf_native;
    } /* end else */

    /* Allocate space for the native keys in memory */
    if (NULL == (native = (uint8_t *)H5FL_FAC_MALLOC(hdr->node_info[depth].nat_rec_fac)))
        HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, FAIL,
                    "memory allocation failed for B-tree internal native keys")

    /* Copy the native keys */
    H5MM_memcpy(native, node_native, (hdr->cls->nrec_size * curr_node->node_nrec));

    /* Unlock the node */
    if (H5AC_unprotect(hdr->f, curr_node_class, curr_node->addr, node,
                       (unsigned)(hdr->swmr_write ? H5AC__PIN_ENTRY_FLAG : H5AC__NO_FLAGS_SET)) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")
    if (hdr->swmr_write)
        node_pinned = TRUE;
    else
        node = NULL;

    /* Iterate through records, in order */
    for (u = 0; u < curr_node->node_nrec && !ret_value; u++) {
        /* Descend into child node, if current node is an internal node */
        if (depth > 0)
            if ((ret_value =
                     H5B2__iterate_node(hdr, (uint16_t)(depth - 1), &(node_ptrs[u]), node, op, op_data)) < 0)
                HERROR(H5E_BTREE, H5E_CANTLIST, "node iteration failed");

        /* Make callback for current record */
        if (!ret_value)
            if ((ret_value = (op)(H5B2_NAT_NREC(native, hdr, u), op_data)) < 0)
                HERROR(H5E_BTREE, H5E_CANTLIST, "iterator function failed");
    } /* end for */

    /* Descend into last child node, if current node is an internal node */
    if (!ret_value && depth > 0)
        if ((ret_value = H5B2__iterate_node(hdr, (uint16_t)(depth - 1), &(node_ptrs[u]), node, op, op_data)) <
            0)
            HERROR(H5E_BTREE, H5E_CANTLIST, "node iteration failed");

done:
    /* Unpin the node if it was pinned */
    if (node_pinned && H5AC_unpin_entry(node) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPIN, FAIL, "can't unpin node")

    /* Release the node pointers & native records, if they were copied */
    if (node_ptrs)
        node_ptrs = (H5B2_node_ptr_t *)H5FL_FAC_FREE(hdr->node_info[depth].node_ptr_fac, node_ptrs);
    if (native)
        native = (uint8_t *)H5FL_FAC_FREE(hdr->node_info[depth].nat_rec_fac, native);

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5B2__iterate_node() */

/*-------------------------------------------------------------------------
 * Function:	H5B2__delete_node
 *
 * Purpose:	Iterate over all the nodes in a B-tree node deleting them
 *		after they no longer have any children
 *
 * Return:	Value from callback, non-negative on success, negative on error
 *
 * Programmer:	Quincey Koziol
 *		Mar  9 2005
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__delete_node(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node, void *parent, H5B2_remove_t op,
                  void *op_data)
{
    const H5AC_class_t *curr_node_class = NULL; /* Pointer to current node's class info */
    void               *node            = NULL; /* Pointers to current node */
    uint8_t            *native;                 /* Pointers to node's native records */
    herr_t              ret_value = SUCCEED;    /* Return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(curr_node);

    if (depth > 0) {
        H5B2_internal_t *internal; /* Pointer to internal node */
        unsigned         u;        /* Local index */

        /* Lock the current B-tree node */
        if (NULL ==
            (internal = H5B2__protect_internal(hdr, parent, curr_node, depth, FALSE, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")

        /* Set up information about current node */
        curr_node_class = H5AC_BT2_INT;
        node            = internal;
        native          = internal->int_native;

        /* Descend into children */
        for (u = 0; u < internal->nrec + (unsigned)1; u++)
            if (H5B2__delete_node(hdr, (uint16_t)(depth - 1), &(internal->node_ptrs[u]), internal, op,
                                  op_data) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTLIST, FAIL, "node descent failed")
    } /* end if */
    else {
        H5B2_leaf_t *leaf; /* Pointer to leaf node */

        /* Lock the current B-tree node */
        if (NULL ==
            (leaf = H5B2__protect_leaf(hdr, parent, (H5B2_node_ptr_t *)curr_node, FALSE, H5AC__NO_FLAGS_SET)))
            HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")

        /* Set up information about current node */
        curr_node_class = H5AC_BT2_LEAF;
        node            = leaf;
        native          = leaf->leaf_native;
    } /* end else */

    /* If there's a callback defined, iterate over the records in this node */
    if (op) {
        unsigned u; /* Local index */

        /* Iterate through records in this node */
        for (u = 0; u < curr_node->node_nrec; u++) {
            /* Make callback for each record */
            if ((op)(H5B2_NAT_NREC(native, hdr, u), op_data) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTLIST, FAIL, "iterator function failed")
        } /* end for */
    }     /* end if */

done:
    /* Unlock & delete current node */
    if (node && H5AC_unprotect(
                    hdr->f, curr_node_class, curr_node->addr, node,
                    (unsigned)(H5AC__DELETED_FLAG | (hdr->swmr_write ? 0 : H5AC__FREE_FILE_SPACE_FLAG))) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5B2__delete_node() */

/*-------------------------------------------------------------------------
 * Function:    H5B2__node_size
 *
 * Purpose:     Iterate over all the records from a B-tree node, collecting
 *		btree storage info.
 *
 * Return:      non-negative on success, negative on error
 *
 * Programmer:  Vailin Choi
 *              July 12 2007
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__node_size(H5B2_hdr_t *hdr, uint16_t depth, H5B2_node_ptr_t *curr_node, void *parent,
                hsize_t *btree_size)
{
    H5B2_internal_t *internal  = NULL;    /* Pointer to internal node */
    herr_t           ret_value = SUCCEED; /* Iterator return value */

    FUNC_ENTER_PACKAGE

    /* Check arguments. */
    HDassert(hdr);
    HDassert(curr_node);
    HDassert(btree_size);
    HDassert(depth > 0);

    /* Lock the current B-tree node */
    if (NULL ==
        (internal = H5B2__protect_internal(hdr, parent, curr_node, depth, FALSE, H5AC__READ_ONLY_FLAG)))
        HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")

    /* Recursively descend into child nodes, if we are above the "twig" level in the B-tree */
    if (depth > 1) {
        unsigned u; /* Local index */

        /* Descend into children */
        for (u = 0; u < internal->nrec + (unsigned)1; u++)
            if (H5B2__node_size(hdr, (uint16_t)(depth - 1), &(internal->node_ptrs[u]), internal, btree_size) <
                0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTLIST, FAIL, "node iteration failed")
    }    /* end if */
    else /* depth is 1: count all the leaf nodes from this node */
        *btree_size += (hsize_t)(internal->nrec + 1) * hdr->node_size;

    /* Count this node */
    *btree_size += hdr->node_size;

done:
    if (internal && H5AC_unprotect(hdr->f, H5AC_BT2_INT, curr_node->addr, internal, H5AC__NO_FLAGS_SET) < 0)
        HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* H5B2__node_size() */

/*-------------------------------------------------------------------------
 * Function:    H5B2__create_flush_depend
 *
 * Purpose:     Create a flush dependency between two data structure components
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Dana Robinson
 *              Fall 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__create_flush_depend(H5AC_info_t *parent_entry, H5AC_info_t *child_entry)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /* Sanity check */
    HDassert(parent_entry);
    HDassert(child_entry);

    /* Create a flush dependency between parent and child entry */
    if (H5AC_create_flush_dependency(parent_entry, child_entry) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTDEPEND, FAIL, "unable to create flush dependency")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__create_flush_depend() */

/*-------------------------------------------------------------------------
 * Function:    H5B2__update_flush_depend
 *
 * Purpose:     Update flush dependencies for children of a node
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Quincey Koziol
 *		Dec  1 2016
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__update_flush_depend(H5B2_hdr_t *hdr, unsigned depth, H5B2_node_ptr_t *node_ptr, void *old_parent,
                          void *new_parent)
{
    const H5AC_class_t *child_class;           /* Pointer to child node's class info */
    void               *child       = NULL;    /* Pointer to child node */
    unsigned            node_status = 0;       /* Node's status in the metadata cache */
    herr_t              ret_value   = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity checks */
    HDassert(hdr);
    HDassert(depth > 0);
    HDassert(node_ptr);
    HDassert(old_parent);
    HDassert(new_parent);

    /* Check the node's entry status in the metadata cache */
    if (H5AC_get_entry_status(hdr->f, node_ptr->addr, &node_status) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTGET, FAIL, "unable to check status of B-tree node")

    /* If the node is in the cache, check for retargeting its parent */
    if (node_status & H5AC_ES__IN_CACHE) {
        void  **parent_ptr  = NULL;  /* Pointer to child node's parent */
        hbool_t update_deps = FALSE; /* Whether to update flush dependencies */

        /* Get child node pointer */
        if (depth > 1) {
            H5B2_internal_t *child_int;

            /* Protect child */
            if (NULL == (child_int = H5B2__protect_internal(hdr, new_parent, node_ptr, (uint16_t)(depth - 1),
                                                            FALSE, H5AC__NO_FLAGS_SET)))
                HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree internal node")
            child_class = H5AC_BT2_INT;
            child       = child_int;

            if (child_int->parent == old_parent) {
                parent_ptr  = &child_int->parent;
                update_deps = TRUE;
            } /* end if */
            else
                HDassert(child_int->parent == new_parent);
        } /* end if */
        else {
            H5B2_leaf_t *child_leaf;

            /* Protect child */
            if (NULL == (child_leaf = H5B2__protect_leaf(hdr, new_parent, (H5B2_node_ptr_t *)node_ptr, FALSE,
                                                         H5AC__NO_FLAGS_SET)))
                HGOTO_ERROR(H5E_BTREE, H5E_CANTPROTECT, FAIL, "unable to protect B-tree leaf node")
            child_class = H5AC_BT2_LEAF;
            child       = child_leaf;

            if (child_leaf->parent == old_parent) {
                parent_ptr  = &child_leaf->parent;
                update_deps = TRUE;
            } /* end if */
            else
                HDassert(child_leaf->parent == new_parent);
        } /* end else */

        /* Update flush dependencies if necessary */
        if (update_deps) {
            /* Sanity check */
            HDassert(parent_ptr);

            /* Switch the flush dependency for the node */
            if (H5B2__destroy_flush_depend((H5AC_info_t *)old_parent, (H5AC_info_t *)child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency")
            *parent_ptr = new_parent;
            if (H5B2__create_flush_depend((H5AC_info_t *)new_parent, (H5AC_info_t *)child) < 0)
                HGOTO_ERROR(H5E_BTREE, H5E_CANTDEPEND, FAIL, "unable to create flush dependency")
        } /* end if */
    }     /* end if */

done:
    /* Unprotect the child */
    if (child)
        if (H5AC_unprotect(hdr->f, child_class, node_ptr->addr, child, H5AC__NO_FLAGS_SET) < 0)
            HDONE_ERROR(H5E_BTREE, H5E_CANTUNPROTECT, FAIL, "unable to release B-tree node")

    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__update_flush_depend() */

/*-------------------------------------------------------------------------
 * Function:    H5B2__update_child_flush_depends
 *
 * Purpose:     Update flush dependencies for children of a node
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Quincey Koziol
 *		Dec  1 2016
 *
 *-------------------------------------------------------------------------
 */
static herr_t
H5B2__update_child_flush_depends(H5B2_hdr_t *hdr, unsigned depth, H5B2_node_ptr_t *node_ptrs,
                                 unsigned start_idx, unsigned end_idx, void *old_parent, void *new_parent)
{
    unsigned u;                   /* Local index variable */
    herr_t   ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_PACKAGE

    /* Sanity checks */
    HDassert(hdr);
    HDassert(depth > 1);
    HDassert(node_ptrs);
    HDassert(start_idx <= end_idx);
    HDassert(old_parent);
    HDassert(new_parent);

    /* Loop over children */
    for (u = start_idx; u < end_idx; u++)
        /* Update parent for children */
        if (H5B2__update_flush_depend(hdr, depth - 1, &node_ptrs[u], old_parent, new_parent) < 0)
            HGOTO_ERROR(H5E_BTREE, H5E_CANTUPDATE, FAIL, "unable to update child node to new parent")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__update_child_flush_depends() */

/*-------------------------------------------------------------------------
 * Function:    H5B2__destroy_flush_depend
 *
 * Purpose:     Destroy a flush dependency between two data structure components
 *
 * Return:      SUCCEED/FAIL
 *
 * Programmer:  Dana Robinson
 *              Fall 2012
 *
 *-------------------------------------------------------------------------
 */
herr_t
H5B2__destroy_flush_depend(H5AC_info_t *parent_entry, H5AC_info_t *child_entry)
{
    herr_t ret_value = SUCCEED; /* Return value */

    FUNC_ENTER_NOAPI_NOINIT

    /* Sanity check */
    HDassert(parent_entry);
    HDassert(child_entry);

    /* Destroy a flush dependency between parent and child entry */
    if (H5AC_destroy_flush_dependency(parent_entry, child_entry) < 0)
        HGOTO_ERROR(H5E_BTREE, H5E_CANTUNDEPEND, FAIL, "unable to destroy flush dependency")

done:
    FUNC_LEAVE_NOAPI(ret_value)
} /* end H5B2__destroy_flush_depend() */