rust-hdf5 0.7.2

Pure Rust HDF5 library with full read/write and SWMR support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
17921
17922
17923
17924
17925
17926
17927
17928
17929
17930
17931
17932
17933
17934
17935
17936
17937
17938
17939
17940
17941
17942
17943
17944
17945
17946
17947
17948
17949
17950
17951
17952
17953
17954
17955
17956
17957
17958
17959
17960
17961
17962
17963
17964
17965
17966
17967
17968
17969
17970
17971
17972
17973
17974
17975
17976
17977
17978
17979
17980
17981
17982
17983
17984
17985
17986
17987
17988
17989
17990
17991
17992
17993
17994
17995
17996
17997
17998
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
18015
18016
18017
18018
18019
18020
18021
18022
18023
18024
18025
18026
18027
18028
18029
18030
18031
18032
18033
18034
18035
18036
18037
18038
18039
18040
18041
18042
18043
18044
18045
18046
18047
18048
18049
18050
18051
18052
18053
18054
18055
18056
18057
18058
18059
18060
18061
18062
18063
18064
18065
18066
18067
18068
18069
18070
18071
18072
18073
18074
18075
18076
18077
18078
18079
18080
18081
18082
18083
18084
18085
18086
18087
18088
18089
18090
18091
18092
18093
18094
18095
18096
18097
18098
18099
18100
18101
18102
18103
18104
18105
18106
18107
18108
18109
18110
18111
18112
18113
18114
18115
18116
18117
18118
18119
18120
18121
18122
18123
18124
18125
18126
18127
18128
18129
18130
18131
18132
18133
18134
18135
18136
18137
18138
18139
18140
18141
18142
18143
18144
18145
18146
18147
18148
18149
18150
18151
18152
18153
18154
18155
18156
18157
18158
18159
18160
18161
18162
18163
18164
18165
18166
18167
18168
18169
18170
18171
18172
18173
18174
18175
18176
18177
18178
18179
18180
18181
18182
18183
18184
18185
18186
18187
18188
18189
18190
18191
18192
18193
18194
18195
18196
18197
18198
18199
18200
18201
18202
18203
18204
18205
18206
18207
18208
18209
18210
18211
18212
18213
18214
18215
18216
18217
18218
18219
18220
18221
18222
18223
18224
18225
18226
18227
18228
18229
18230
18231
18232
18233
18234
18235
18236
18237
18238
18239
18240
18241
18242
18243
18244
18245
18246
18247
18248
18249
18250
18251
18252
18253
18254
18255
18256
18257
18258
18259
18260
18261
18262
18263
18264
18265
18266
18267
18268
18269
18270
18271
18272
18273
18274
18275
18276
18277
18278
18279
18280
18281
18282
18283
18284
18285
18286
18287
18288
18289
18290
18291
18292
18293
18294
18295
18296
18297
18298
18299
18300
18301
18302
18303
18304
18305
18306
18307
18308
18309
18310
18311
18312
18313
18314
18315
18316
18317
18318
18319
18320
18321
18322
18323
18324
18325
18326
18327
18328
18329
18330
18331
18332
18333
18334
18335
18336
18337
18338
18339
18340
18341
18342
18343
18344
18345
18346
18347
18348
18349
18350
18351
18352
18353
18354
18355
18356
18357
18358
18359
18360
18361
18362
18363
18364
18365
18366
18367
18368
18369
18370
18371
18372
18373
18374
18375
18376
18377
18378
18379
18380
18381
18382
18383
18384
18385
18386
18387
18388
18389
18390
18391
18392
18393
18394
18395
18396
18397
18398
18399
18400
18401
18402
18403
18404
18405
18406
18407
18408
18409
18410
18411
18412
18413
18414
18415
18416
18417
18418
18419
18420
18421
18422
18423
18424
18425
18426
18427
18428
18429
18430
18431
18432
18433
18434
18435
18436
18437
18438
18439
18440
18441
18442
18443
18444
18445
18446
18447
18448
18449
18450
18451
18452
18453
18454
18455
18456
18457
18458
18459
18460
18461
18462
18463
18464
18465
18466
18467
18468
18469
18470
18471
18472
18473
18474
18475
18476
18477
18478
18479
18480
18481
18482
18483
18484
18485
18486
18487
18488
18489
18490
18491
18492
18493
18494
18495
18496
18497
18498
18499
18500
18501
18502
18503
18504
18505
18506
18507
18508
18509
18510
18511
18512
18513
18514
18515
18516
18517
18518
18519
18520
18521
18522
18523
18524
18525
18526
18527
18528
18529
18530
18531
18532
18533
18534
18535
18536
18537
18538
18539
18540
18541
18542
18543
18544
18545
18546
18547
18548
18549
18550
18551
18552
18553
18554
18555
18556
18557
18558
18559
18560
18561
18562
18563
18564
18565
18566
18567
18568
18569
18570
18571
18572
18573
18574
18575
18576
18577
18578
18579
18580
18581
18582
18583
18584
18585
18586
18587
18588
18589
18590
18591
18592
18593
18594
18595
18596
18597
18598
18599
18600
18601
18602
18603
18604
18605
18606
18607
18608
18609
18610
18611
18612
18613
18614
18615
18616
18617
18618
18619
18620
18621
18622
18623
18624
18625
18626
18627
18628
18629
18630
18631
18632
18633
18634
18635
18636
18637
18638
18639
18640
18641
18642
18643
18644
18645
18646
18647
18648
18649
18650
18651
18652
18653
18654
18655
18656
18657
18658
18659
18660
18661
18662
18663
18664
18665
18666
18667
18668
18669
18670
18671
18672
18673
18674
18675
18676
18677
18678
18679
18680
18681
18682
18683
18684
18685
18686
18687
18688
18689
18690
18691
18692
18693
18694
18695
18696
18697
18698
18699
18700
18701
18702
18703
18704
18705
18706
18707
18708
18709
18710
18711
18712
18713
18714
18715
18716
18717
18718
18719
18720
18721
18722
18723
18724
18725
18726
18727
18728
18729
18730
18731
18732
18733
18734
18735
18736
18737
18738
18739
18740
18741
18742
18743
18744
18745
18746
18747
18748
18749
18750
18751
18752
18753
18754
18755
18756
18757
18758
18759
18760
18761
18762
18763
18764
18765
18766
18767
18768
18769
18770
18771
18772
18773
18774
18775
18776
18777
18778
18779
18780
18781
18782
18783
18784
18785
18786
18787
18788
18789
18790
18791
18792
18793
18794
18795
18796
18797
18798
18799
18800
18801
18802
18803
18804
18805
18806
18807
18808
18809
18810
18811
18812
18813
18814
18815
18816
18817
18818
18819
18820
18821
18822
18823
18824
18825
18826
18827
18828
18829
18830
18831
18832
18833
18834
18835
18836
18837
18838
18839
18840
18841
18842
18843
18844
18845
18846
18847
18848
18849
18850
18851
18852
18853
18854
18855
18856
18857
18858
18859
18860
18861
18862
18863
18864
18865
18866
18867
18868
18869
18870
18871
18872
18873
18874
18875
18876
18877
18878
18879
18880
18881
18882
18883
18884
18885
18886
18887
18888
18889
18890
18891
18892
18893
18894
18895
18896
18897
18898
18899
18900
18901
18902
18903
18904
18905
18906
18907
18908
18909
18910
18911
18912
18913
18914
18915
18916
18917
18918
18919
18920
18921
18922
18923
18924
18925
18926
18927
18928
18929
18930
18931
18932
18933
18934
18935
18936
18937
18938
18939
18940
18941
18942
18943
18944
18945
18946
18947
18948
18949
18950
18951
18952
18953
18954
18955
18956
18957
18958
18959
18960
18961
18962
18963
18964
18965
18966
18967
18968
18969
18970
18971
18972
18973
18974
18975
18976
18977
18978
18979
18980
18981
18982
18983
18984
18985
18986
18987
18988
18989
18990
18991
18992
18993
18994
18995
18996
18997
18998
18999
19000
19001
19002
19003
19004
19005
19006
19007
19008
19009
19010
19011
19012
19013
19014
19015
19016
19017
19018
19019
19020
19021
19022
19023
19024
19025
19026
19027
19028
19029
19030
19031
19032
19033
19034
19035
19036
19037
19038
19039
19040
19041
19042
19043
19044
19045
19046
19047
19048
19049
19050
19051
19052
19053
19054
19055
19056
19057
19058
19059
19060
19061
19062
19063
19064
19065
19066
19067
19068
19069
19070
19071
19072
19073
19074
19075
19076
19077
19078
19079
19080
19081
19082
19083
19084
19085
19086
19087
19088
19089
19090
19091
19092
19093
19094
19095
19096
19097
19098
19099
19100
19101
19102
19103
19104
19105
19106
19107
19108
19109
19110
19111
19112
19113
19114
19115
19116
19117
19118
19119
19120
19121
19122
19123
19124
19125
19126
19127
19128
19129
19130
19131
19132
19133
19134
19135
19136
19137
19138
19139
19140
19141
19142
19143
19144
19145
19146
19147
19148
19149
19150
19151
19152
19153
19154
19155
19156
19157
19158
19159
19160
19161
19162
19163
19164
19165
19166
19167
19168
19169
19170
19171
19172
19173
19174
19175
19176
19177
19178
19179
19180
19181
19182
19183
19184
19185
19186
19187
19188
19189
19190
19191
19192
19193
19194
19195
19196
19197
19198
19199
19200
19201
19202
19203
19204
19205
19206
19207
19208
19209
19210
19211
19212
19213
19214
19215
19216
19217
19218
19219
19220
19221
19222
19223
19224
19225
19226
19227
19228
19229
19230
19231
19232
19233
19234
19235
19236
19237
19238
19239
19240
19241
19242
19243
19244
19245
19246
19247
19248
19249
19250
19251
19252
19253
19254
19255
19256
19257
19258
19259
19260
19261
19262
19263
19264
19265
19266
19267
19268
19269
19270
19271
19272
19273
19274
19275
19276
19277
19278
19279
19280
19281
19282
19283
19284
19285
19286
19287
19288
19289
19290
19291
19292
19293
19294
19295
19296
19297
19298
19299
19300
19301
19302
19303
19304
19305
19306
19307
19308
19309
19310
19311
19312
19313
19314
19315
19316
19317
19318
19319
19320
19321
19322
19323
19324
19325
19326
19327
19328
19329
19330
19331
19332
19333
19334
19335
19336
19337
19338
19339
19340
19341
19342
19343
19344
19345
19346
19347
19348
19349
19350
19351
19352
19353
19354
19355
19356
19357
19358
19359
19360
19361
19362
19363
19364
19365
19366
19367
19368
19369
19370
19371
19372
19373
19374
19375
19376
19377
19378
19379
19380
19381
19382
19383
19384
19385
19386
19387
19388
19389
19390
19391
19392
19393
19394
19395
19396
19397
19398
19399
19400
19401
19402
19403
19404
19405
19406
19407
19408
19409
19410
19411
19412
19413
19414
19415
19416
19417
19418
19419
19420
19421
19422
19423
19424
19425
19426
19427
19428
19429
19430
19431
19432
19433
19434
19435
19436
19437
19438
19439
19440
19441
19442
19443
19444
19445
19446
19447
19448
19449
19450
19451
19452
19453
19454
19455
19456
19457
19458
19459
19460
19461
19462
19463
19464
19465
19466
19467
19468
19469
19470
19471
19472
19473
19474
19475
19476
19477
19478
19479
19480
19481
19482
19483
19484
19485
19486
19487
19488
19489
19490
19491
19492
19493
19494
19495
19496
19497
19498
19499
19500
19501
19502
19503
19504
19505
19506
19507
19508
19509
19510
19511
19512
19513
19514
19515
19516
19517
19518
19519
19520
19521
19522
19523
19524
19525
19526
19527
19528
19529
19530
19531
19532
19533
19534
19535
19536
19537
19538
19539
19540
19541
19542
19543
19544
19545
19546
19547
19548
19549
19550
19551
19552
19553
19554
19555
19556
19557
19558
19559
19560
19561
19562
19563
19564
19565
19566
19567
19568
19569
19570
19571
19572
19573
19574
19575
19576
19577
19578
19579
19580
19581
19582
19583
19584
19585
19586
19587
19588
19589
19590
19591
19592
19593
19594
19595
19596
19597
19598
19599
19600
19601
19602
19603
19604
19605
19606
19607
19608
19609
19610
19611
19612
19613
19614
19615
19616
19617
19618
19619
19620
19621
19622
19623
19624
19625
19626
19627
19628
19629
19630
19631
19632
19633
19634
19635
19636
19637
19638
19639
19640
19641
19642
19643
19644
19645
19646
19647
19648
19649
19650
19651
19652
19653
19654
19655
19656
19657
19658
19659
19660
19661
19662
19663
19664
19665
19666
19667
19668
19669
19670
19671
19672
19673
19674
19675
19676
19677
19678
19679
19680
19681
19682
19683
19684
19685
19686
19687
19688
19689
19690
19691
19692
19693
19694
19695
19696
19697
19698
19699
19700
19701
19702
19703
19704
19705
19706
19707
19708
19709
19710
19711
19712
19713
19714
19715
19716
19717
19718
19719
19720
19721
19722
19723
19724
19725
19726
19727
19728
19729
19730
19731
19732
19733
19734
19735
19736
19737
19738
19739
19740
19741
19742
19743
19744
19745
19746
19747
19748
19749
19750
19751
19752
19753
19754
19755
19756
19757
19758
19759
19760
19761
19762
19763
19764
19765
19766
19767
19768
19769
19770
19771
19772
19773
19774
19775
19776
19777
19778
19779
19780
19781
19782
19783
19784
19785
19786
19787
19788
19789
19790
19791
19792
19793
19794
19795
19796
19797
19798
19799
19800
19801
19802
19803
19804
19805
19806
19807
19808
19809
19810
19811
19812
19813
19814
19815
19816
19817
19818
19819
19820
19821
19822
19823
19824
19825
19826
19827
19828
19829
19830
19831
19832
19833
19834
19835
19836
19837
19838
19839
19840
19841
19842
19843
19844
19845
19846
19847
19848
19849
19850
19851
19852
19853
19854
19855
19856
19857
19858
19859
19860
19861
19862
19863
19864
19865
19866
19867
19868
19869
19870
19871
19872
19873
19874
19875
19876
19877
19878
19879
19880
19881
19882
19883
19884
19885
19886
19887
19888
19889
19890
19891
19892
19893
19894
19895
19896
19897
19898
19899
19900
19901
19902
19903
19904
19905
19906
19907
19908
19909
19910
19911
19912
19913
19914
19915
19916
19917
19918
19919
19920
19921
19922
19923
19924
19925
19926
19927
19928
19929
19930
19931
19932
19933
19934
19935
19936
19937
19938
19939
19940
19941
19942
19943
19944
19945
19946
19947
19948
19949
19950
19951
19952
19953
19954
19955
19956
19957
19958
19959
19960
19961
19962
19963
19964
19965
19966
19967
19968
19969
19970
19971
19972
19973
19974
19975
19976
19977
19978
19979
19980
19981
19982
19983
19984
19985
19986
19987
19988
19989
19990
19991
19992
19993
19994
19995
19996
19997
19998
19999
20000
20001
20002
20003
20004
20005
20006
20007
20008
20009
20010
20011
20012
20013
20014
20015
20016
20017
20018
20019
20020
20021
20022
20023
20024
20025
20026
20027
20028
20029
20030
20031
20032
20033
20034
20035
20036
20037
20038
20039
20040
20041
20042
20043
20044
20045
20046
20047
20048
20049
20050
20051
20052
20053
20054
20055
20056
20057
20058
20059
20060
20061
20062
20063
20064
20065
20066
20067
20068
20069
20070
20071
20072
20073
20074
20075
20076
20077
20078
20079
20080
20081
20082
20083
20084
20085
20086
20087
20088
20089
20090
20091
20092
20093
20094
20095
20096
20097
20098
20099
20100
20101
20102
20103
20104
20105
20106
20107
20108
20109
20110
20111
20112
20113
20114
20115
20116
20117
20118
20119
20120
20121
20122
20123
20124
20125
20126
20127
20128
20129
20130
20131
20132
20133
20134
20135
20136
20137
20138
20139
20140
20141
20142
20143
20144
20145
20146
20147
20148
20149
20150
20151
20152
20153
20154
20155
20156
20157
20158
20159
20160
20161
20162
20163
20164
20165
20166
20167
20168
20169
20170
20171
20172
20173
20174
20175
20176
20177
20178
20179
20180
20181
20182
20183
20184
20185
20186
20187
20188
20189
20190
20191
20192
20193
20194
20195
20196
20197
20198
20199
20200
20201
20202
20203
20204
20205
20206
20207
20208
20209
20210
20211
20212
20213
20214
20215
20216
20217
20218
20219
20220
20221
20222
20223
20224
20225
20226
20227
20228
20229
20230
20231
20232
20233
20234
20235
20236
20237
20238
20239
20240
20241
20242
20243
20244
20245
20246
20247
20248
20249
20250
20251
20252
20253
20254
20255
20256
20257
20258
20259
20260
20261
20262
20263
20264
20265
20266
20267
20268
20269
20270
20271
20272
20273
20274
20275
20276
20277
20278
20279
20280
20281
20282
20283
20284
20285
20286
20287
20288
20289
20290
20291
20292
20293
20294
20295
20296
20297
20298
20299
20300
20301
20302
20303
20304
20305
20306
20307
20308
20309
20310
20311
20312
20313
20314
20315
20316
20317
20318
20319
20320
20321
20322
20323
20324
20325
20326
20327
20328
20329
20330
20331
20332
20333
20334
20335
20336
20337
20338
20339
20340
20341
20342
20343
20344
20345
20346
20347
20348
20349
20350
20351
20352
20353
20354
20355
20356
20357
20358
20359
20360
20361
20362
20363
20364
20365
20366
20367
20368
20369
20370
20371
20372
20373
20374
20375
20376
20377
20378
20379
20380
20381
20382
20383
20384
20385
20386
20387
20388
20389
20390
20391
20392
20393
20394
20395
20396
20397
20398
20399
20400
20401
20402
20403
20404
20405
20406
20407
20408
20409
20410
20411
20412
20413
20414
20415
20416
20417
20418
20419
20420
20421
20422
20423
20424
20425
20426
20427
20428
20429
20430
20431
20432
20433
20434
20435
20436
20437
20438
20439
20440
20441
20442
20443
20444
20445
20446
20447
20448
20449
20450
20451
20452
20453
20454
20455
20456
20457
20458
20459
20460
20461
20462
20463
20464
20465
20466
20467
20468
20469
20470
20471
20472
20473
20474
20475
20476
20477
20478
20479
20480
20481
20482
20483
20484
20485
20486
20487
20488
20489
20490
20491
20492
20493
20494
20495
20496
20497
20498
20499
20500
20501
20502
20503
20504
20505
20506
20507
20508
20509
20510
20511
20512
20513
20514
20515
20516
20517
20518
20519
20520
20521
20522
20523
20524
20525
20526
20527
20528
20529
20530
20531
20532
20533
20534
20535
20536
20537
20538
20539
20540
20541
20542
20543
20544
20545
20546
20547
20548
20549
20550
20551
20552
20553
20554
20555
20556
20557
20558
20559
20560
20561
20562
20563
20564
20565
20566
20567
20568
20569
20570
20571
20572
20573
20574
20575
20576
20577
20578
20579
20580
20581
20582
20583
20584
20585
20586
20587
20588
20589
20590
20591
20592
20593
20594
20595
20596
20597
20598
20599
20600
20601
20602
20603
20604
20605
20606
20607
20608
20609
20610
20611
20612
20613
20614
20615
20616
20617
20618
20619
20620
20621
20622
20623
20624
20625
20626
20627
20628
20629
20630
20631
20632
20633
20634
20635
20636
20637
20638
20639
20640
20641
20642
20643
20644
20645
20646
20647
20648
20649
20650
20651
20652
20653
20654
20655
20656
20657
20658
20659
20660
20661
20662
20663
20664
20665
20666
20667
20668
20669
20670
20671
20672
20673
20674
20675
20676
20677
20678
20679
20680
20681
20682
20683
20684
20685
20686
20687
20688
20689
20690
20691
20692
20693
20694
20695
20696
20697
20698
20699
20700
20701
20702
20703
20704
20705
20706
20707
20708
20709
20710
20711
20712
20713
20714
20715
20716
20717
20718
20719
20720
20721
20722
20723
20724
20725
20726
20727
20728
20729
20730
20731
20732
20733
20734
20735
20736
20737
20738
20739
20740
20741
20742
20743
20744
20745
20746
20747
20748
20749
20750
20751
20752
20753
20754
20755
20756
20757
20758
20759
20760
20761
20762
20763
20764
20765
20766
20767
20768
20769
20770
20771
20772
20773
20774
20775
20776
20777
20778
20779
20780
20781
20782
20783
20784
20785
20786
20787
20788
20789
20790
20791
20792
20793
20794
20795
20796
20797
20798
20799
20800
20801
20802
20803
20804
20805
20806
20807
20808
20809
20810
20811
20812
20813
20814
20815
20816
20817
20818
20819
20820
20821
20822
20823
20824
20825
20826
20827
20828
20829
20830
20831
20832
20833
20834
20835
20836
20837
20838
20839
20840
20841
20842
20843
20844
20845
20846
20847
20848
20849
20850
20851
20852
20853
20854
20855
20856
20857
20858
20859
20860
20861
20862
20863
20864
20865
20866
20867
20868
20869
20870
20871
20872
20873
20874
20875
20876
20877
20878
20879
20880
20881
20882
20883
20884
20885
20886
20887
20888
20889
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
20906
20907
20908
20909
20910
20911
20912
20913
20914
20915
20916
20917
20918
20919
20920
20921
20922
20923
20924
20925
20926
20927
20928
20929
20930
20931
20932
20933
20934
20935
20936
20937
20938
20939
20940
20941
20942
20943
20944
20945
20946
20947
20948
20949
20950
20951
20952
20953
20954
20955
20956
20957
20958
20959
20960
20961
20962
20963
20964
20965
20966
20967
20968
20969
20970
20971
20972
20973
20974
20975
20976
20977
20978
20979
20980
20981
20982
20983
20984
20985
20986
20987
20988
20989
20990
20991
20992
20993
20994
20995
20996
20997
20998
20999
21000
21001
21002
21003
21004
21005
21006
21007
21008
21009
21010
21011
21012
21013
21014
21015
21016
21017
21018
21019
21020
21021
21022
21023
21024
21025
21026
21027
21028
21029
21030
21031
21032
21033
21034
21035
21036
21037
21038
21039
21040
21041
21042
21043
21044
21045
21046
21047
21048
21049
21050
21051
21052
21053
21054
21055
21056
21057
21058
21059
21060
21061
21062
21063
21064
21065
21066
21067
21068
21069
21070
21071
21072
21073
21074
21075
21076
21077
21078
21079
21080
21081
21082
21083
21084
21085
21086
21087
21088
21089
21090
21091
21092
21093
21094
21095
21096
21097
21098
21099
21100
21101
21102
21103
21104
21105
21106
21107
21108
21109
21110
21111
21112
21113
21114
21115
21116
21117
21118
21119
21120
21121
21122
21123
21124
21125
21126
21127
21128
21129
21130
21131
21132
21133
21134
21135
21136
21137
21138
21139
21140
21141
21142
21143
21144
21145
21146
21147
21148
21149
21150
21151
21152
21153
21154
21155
21156
21157
21158
21159
21160
21161
21162
21163
21164
21165
21166
21167
21168
21169
21170
21171
21172
21173
21174
21175
21176
21177
21178
21179
21180
21181
21182
21183
21184
21185
21186
21187
21188
21189
21190
21191
21192
21193
21194
21195
21196
21197
21198
21199
21200
21201
21202
21203
21204
21205
21206
21207
21208
21209
21210
//! HDF5 file writer.
//!
//! Produces a valid HDF5 file with superblock v3, a root group object header,
//! and datasets with contiguous or chunked storage. The output is readable by `h5dump`.

use std::collections::{HashMap, HashSet};
use std::path::{Path, PathBuf};

use crate::dataset::DatasetAccess;
use crate::format::btree_v1::{BTreeV1Config, ChunkBTreeV1Node, ChunkBTreeV1Tree, ChunkKey};
use crate::format::chunk_index::btree_v2::Bt2ChunkIndex;
use crate::format::chunk_index::extensible_array::{
    compute_chunk_size_len, compute_ndblk_addrs, compute_nsblk_addrs, EaDblkPath, EaGeometry,
    EaLoc, ExtensibleArrayDataBlock, ExtensibleArrayHeader, ExtensibleArrayIndexBlock,
    ExtensibleArraySuperBlock, FilteredChunkEntry, FilteredDataBlock, FilteredIndexBlock,
    EA_CLS_CHUNK, EA_CLS_FILT_CHUNK,
};
use crate::format::chunk_index::fixed_array::{
    decode_filtered_page, decode_unfiltered_page, encode_filtered_page, encode_unfiltered_page,
    FixedArrayDataBlock, FixedArrayFilteredChunkElement, FixedArrayHeader, FixedArrayPagedPrefix,
    FA_CLIENT_FILT_CHUNK,
};
use crate::format::creation_order::CreationOrder;
use crate::format::dense_attr::build_dense_attributes;
use crate::format::dense_link::build_dense_links;
use crate::format::free_space::{
    self, FreeSection, FreeSpaceClass, FreeSpaceHeader, FreeSpaceManager,
};
use crate::format::local_heap::{
    local_heap_header_size, LocalHeapHeader, LocalHeapImage, LOCAL_HEAP_FREE_NULL,
};
use crate::format::messages::attr_info::{next_creation_index, AttributeInfoMessage};
use crate::format::messages::attribute::{
    AttributeEntry, AttributeMessage, ATTR_FLAG_SPACE_SHARED, ATTR_FLAG_TYPE_SHARED,
};
use crate::format::messages::data_layout::{
    DataLayoutMessage, EarrayParams, FixedArrayParams, LAYOUT_VERSION_DEFAULT,
};
use crate::format::messages::dataspace::{DataspaceClass, DataspaceMessage};
use crate::format::messages::datatype::{ByteOrder, DatatypeMessage, ReferenceKind};
use crate::format::messages::external_file_list::{ExternalFileListMessage, UNLIMITED};
use crate::format::messages::fill_value::{
    FillValueMessage, FILL_TIME_ALLOC, FILL_TIME_IFSET, FILL_TIME_NEVER,
};
use crate::format::messages::filter::{self, FilterPipeline};
use crate::format::messages::group_info::GroupInfoMessage;
use crate::format::messages::link::{CharacterSet, LinkMessage, LinkTarget};
use crate::format::messages::link_info::LinkInfoMessage;
use crate::format::messages::mod_time::ModificationTime;
use crate::format::messages::superblock_ext::{
    FileSpaceInfoMessage, FileSpaceStrategy, SharedMessageTableMessage,
    DEFAULT_FILE_SPACE_PAGE_SIZE, FS_ADDR_COUNT_V1, PAGE_SIZE_MAX, PAGE_SIZE_MIN,
};
use crate::format::messages::virtual_mapping::{
    parse_source_name, VirtualMapping, VirtualMappingList,
};
use crate::format::messages::*;
use crate::format::object_header::{ObjectHeader, ObjectTimes, MAX_MESSAGE_SIZE};
use crate::format::reference::{
    encode_reference_element, encode_revised_blob, ReferenceElementImage, ReferenceTarget,
    REVISED_BLOB_TOKEN_OFFSET,
};
use crate::format::selection::Selection;
use crate::format::sohm::{
    type_flag, SharedMessagePointer, MAX_SOHM_INDEXES, SOHM_HEAP_ID_LEN, SOHM_POINTER_HEAP_ID_AT,
};
use crate::format::sohm_write::{
    build_shared_messages, NestedShare, SharedMessage, SohmIndexContent, SohmIndexSpec,
};
use crate::format::superblock::*;
use crate::format::{FormatContext, LibverBound, ObjectFormat, UNDEF_ADDR};

use crate::format::selection::check_hyperslab;
use crate::io::allocator::{FileAllocator, FreeBlock};
use crate::io::file_handle::FileHandle;
use crate::io::hyperslab::{for_each_contiguous_run, for_each_dual_run};
use crate::io::symbol_table_io::{free_stab, write_stab, Stab, StabExtents, StabLink, StabTarget};
use crate::io::{FileMeta, IoResult};

/// On-disk size in bytes of a fixed-array data block, for the layout (paged or
/// flat) implied by `hdr`.
///
/// Mirrors `H5FA_DBLOCK_SIZE` (`H5FApkg.h`):
///   - non-paged: `prefix + nelmts * raw_elmt_size + checksum`
///   - paged: `prefix + page_init_bitmap + nelmts * raw_elmt_size
///     + npages * checksum`, where the prefix checksum covers the bitmap.
///
/// `raw_elmt_size` is `sizeof_addr` for an unfiltered array, and
/// `sizeof_addr + chunk_size_len + 4` (the filtered element: address +
/// compressed size + filter mask) for a filtered array. libhdf5 carries this
/// value as `hdr->cparam.raw_elmt_size`, i.e. exactly `hdr.element_size`.
fn fixed_array_dblk_disk_size(ctx: &FormatContext, hdr: &FixedArrayHeader) -> u64 {
    let elem_size = hdr.element_size as u64;
    let sa = ctx.sizeof_addr as u64;
    let nelmts = hdr.num_elmts;
    // Common metadata prefix: signature(4) + version(1) + client_id(1) + header_addr(sa).
    let meta_prefix = 4 + 1 + 1 + sa;
    if hdr.is_paged() {
        let npages = hdr.npages();
        let bitmap_size = npages.div_ceil(8);
        // prefix (incl. its own 4-byte checksum) + elements + per-page checksums.
        (meta_prefix + bitmap_size + 4) + nelmts * elem_size + npages * 4
    } else {
        // prefix + elements + single 4-byte checksum.
        meta_prefix + nelmts * elem_size + 4
    }
}

/// A walk of a v2 B-tree: the file and node geometry the descent reads
/// through, and the two collections it fills — every node's raw record
/// bytes and every node block's address, the latter because `open_append`
/// needs it so the reconstructed [`Bt2DatasetInfo::node_addrs`] pool owns
/// the on-disk nodes (the next flush re-serializes the tree over them, and
/// a delete frees them).
///
/// `record_size`, `node_size` and `geo` are constant for the whole walk, so
/// [`descend`](Self::descend) takes only what changes per level: the node's
/// address, its depth, and how many records it holds.
struct Bt2Walk<'a> {
    handle: &'a FileHandle,
    ctx: &'a FormatContext,
    record_size: u16,
    node_size: u32,
    geo: &'a crate::format::chunk_index::btree_v2::Bt2Geometry,
    records: Vec<u8>,
    node_addrs: Vec<u64>,
}

impl<'a> Bt2Walk<'a> {
    fn new(
        handle: &'a FileHandle,
        ctx: &'a FormatContext,
        record_size: u16,
        node_size: u32,
        geo: &'a crate::format::chunk_index::btree_v2::Bt2Geometry,
    ) -> Self {
        Self {
            handle,
            ctx,
            record_size,
            node_size,
            geo,
            records: Vec::new(),
            node_addrs: Vec::new(),
        }
    }

    /// Walk the subtree rooted at `addr`, at depth `depth` with `nrec`
    /// records, collecting every node's raw record bytes and every node
    /// block's address.
    fn descend(&mut self, addr: u64, depth: u16, nrec: u16) -> IoResult<()> {
        use crate::format::chunk_index::btree_v2::{Bt2InternalNode, Bt2LeafNode};

        self.node_addrs.push(addr);
        let buf = self.handle.read_at_most(addr, self.node_size as usize)?;
        if depth == 0 {
            let leaf = Bt2LeafNode::decode(&buf, nrec, self.record_size)?;
            self.records.extend_from_slice(&leaf.record_data);
        } else {
            let node = Bt2InternalNode::decode(
                &buf,
                self.ctx,
                depth,
                nrec,
                self.record_size,
                self.geo.max_nrec_size,
                self.geo.child_total_size(depth),
            )?;
            // In-order: an internal node's records separate its children, so each
            // one belongs between the subtrees on either side of it.
            let children: Vec<(u64, u16)> = node
                .child_addrs
                .iter()
                .zip(node.child_nrecords.iter())
                .map(|(&a, &n)| (a, n))
                .collect();
            let rec = self.record_size as usize;
            for (i, (child_addr, child_nrec)) in children.into_iter().enumerate() {
                self.descend(child_addr, depth - 1, child_nrec)?;
                if let Some(record) = node.record_data.get(i * rec..(i + 1) * rec) {
                    self.records.extend_from_slice(record);
                }
            }
        }
        Ok(())
    }
}

/// A walk of a version-1 raw-data-chunk B-tree: the file and geometry the
/// descent reads through, and the two collections it fills.
///
/// The v1 counterpart of [`Bt2Walk`], and for the same reason: the
/// records are what [`BtreeV1DatasetInfo::build_tree`] bulk-loads on the next
/// flush, and the addresses are the block pool that flush re-serializes over,
/// so a reopened tree owns the nodes it found instead of leaking them and
/// allocating a second set beside them.
///
/// One value rather than nine parameters threaded through the recursion: only
/// `addr` and `depth` change between one level and the next, so they are what
/// [`descend`](Self::descend) takes and everything else lives here.
struct BtreeV1Walk<'a> {
    handle: &'a FileHandle,
    ctx: &'a FormatContext,
    config: &'a BTreeV1Config,
    /// The chunk edge lengths, *without* the trailing element-size dimension,
    /// so `chunk_dims.len()` is the rank the node keys are decoded at.
    chunk_dims: &'a [u64],
    file_size: u64,
    records: Vec<BtreeV1ChunkRecord>,
    node_addrs: Vec<u64>,
}

impl<'a> BtreeV1Walk<'a> {
    fn new(
        handle: &'a FileHandle,
        ctx: &'a FormatContext,
        config: &'a BTreeV1Config,
        chunk_dims: &'a [u64],
        file_size: u64,
    ) -> Self {
        Self {
            handle,
            ctx,
            config,
            chunk_dims,
            file_size,
            records: Vec::new(),
            node_addrs: Vec::new(),
        }
    }

    /// Walk the subtree rooted at `addr`, collecting every leaf entry as a
    /// [`BtreeV1ChunkRecord`] and every node block's address.
    ///
    /// Records come out in key order because a v1 B-tree's leaves are in key
    /// order and this descends left to right, which is what
    /// [`BtreeV1DatasetInfo::position`]'s binary search needs. The keys store
    /// element offsets (`scaled * chunk_dim`, `H5D__btree_encode_key`), so the
    /// grid position this records is the quotient.
    fn descend(&mut self, addr: u64, depth: u32) -> IoResult<()> {
        // The same bound the reader's walk uses: a node's level is one byte, so
        // no honest tree is deeper than that, and a cyclic index stops here.
        if depth > 256 {
            return Err(crate::io::IoError::InvalidState(
                "chunk B-tree v1 exceeds maximum depth".into(),
            ));
        }
        if addr == UNDEF_ADDR || addr >= self.file_size {
            return Ok(());
        }
        let rank = self.chunk_dims.len();
        let sa = self.ctx.sizeof_addr as usize;
        let node_size = self.config.chunk_btree_node_size(sa, rank);
        let buf = self.handle.read_at_most(addr, node_size)?;
        let node = ChunkBTreeV1Node::decode(&buf, sa, rank, self.config.chunk_max_entries())?;
        self.node_addrs.push(addr);

        if node.level == 0 {
            for (i, &child_addr) in node.children.iter().enumerate() {
                let key = &node.keys[i];
                let scaled: Vec<u64> = key.offsets[..rank]
                    .iter()
                    .zip(self.chunk_dims)
                    .map(|(&offset, &dim)| offset.checked_div(dim).unwrap_or(0))
                    .collect();
                self.records.push(BtreeV1ChunkRecord {
                    scaled,
                    address: child_addr,
                    nbytes: key.chunk_size,
                    filter_mask: key.filter_mask,
                });
            }
        } else {
            for &child_addr in &node.children {
                self.descend(child_addr, depth + 1)?;
            }
        }
        Ok(())
    }
}

/// Encode a fixed-array data block for the layout implied by `hdr`, using the
/// chunk addresses held in `dblk.elements` (unfiltered) or the filtered chunk
/// entries in `dblk.filtered_elements` (filtered, `client_id == 1`).
///
/// For the paged layout (`hdr.is_paged()`), emits the `FADB` prefix with a
/// page-init bitmap followed by `npages` checksummed element pages. A page is
/// marked initialized iff at least one of its chunk addresses is defined,
/// mirroring libhdf5's lazy `H5FA__dblk_page_create`. Uninitialized pages are
/// still written (all `UNDEF_ADDR`, valid checksum) so the file contains no
/// uninitialized bytes; the reader skips them via the bitmap.
fn encode_fixed_array_dblk(
    ctx: &FormatContext,
    hdr: &FixedArrayHeader,
    dblk: &FixedArrayDataBlock,
) -> Vec<u8> {
    let is_filtered = hdr.client_id == FA_CLIENT_FILT_CHUNK;
    let sa = ctx.sizeof_addr as usize;
    // chunk_size_len for filtered entries = element_size - sizeof_addr - 4.
    // libhdf5 carries element_size = sizeof_addr + chunk_size_len + 4.
    let chunk_size_len = (hdr.element_size as usize).saturating_sub(sa + 4);

    if !hdr.is_paged() {
        return if is_filtered {
            dblk.encode_filtered(ctx, chunk_size_len)
        } else {
            dblk.encode_unfiltered(ctx)
        };
    }

    let npages = hdr.npages() as usize;
    let dblk_page_nelmts = hdr.dblk_page_nelmts() as usize;

    // Build the page-init bitmap (MSB-first): a page is initialized iff any of
    // its elements points at a defined address.
    let mut bitmap = vec![0u8; npages.div_ceil(8)];
    let nelmts = if is_filtered {
        dblk.filtered_elements.len()
    } else {
        dblk.elements.len()
    };
    for p in 0..npages {
        let start = p * dblk_page_nelmts;
        let end = ((p + 1) * dblk_page_nelmts).min(nelmts);
        let initialized = if is_filtered {
            dblk.filtered_elements[start..end]
                .iter()
                .any(|e| e.address != UNDEF_ADDR)
        } else {
            dblk.elements[start..end].iter().any(|&a| a != UNDEF_ADDR)
        };
        if initialized {
            bitmap[p / 8] |= 0x80u8 >> (p % 8);
        }
    }

    let prefix = FixedArrayPagedPrefix {
        client_id: hdr.client_id,
        header_addr: dblk.header_addr,
        page_init_bitmap: bitmap,
        prefix_size: 4 + 1 + 1 + sa + npages.div_ceil(8) + 4,
    };

    let mut buf = prefix.encode(ctx);
    debug_assert_eq!(buf.len(), prefix.prefix_size);

    // Append each page: all pages use the full `dblk_page_nelmts` stride;
    // only the last page holds fewer elements (libhdf5 H5FA.c).
    for p in 0..npages {
        let start = p * dblk_page_nelmts;
        let end = ((p + 1) * dblk_page_nelmts).min(nelmts);
        if is_filtered {
            buf.extend_from_slice(&encode_filtered_page(
                &dblk.filtered_elements[start..end],
                ctx,
                chunk_size_len,
            ));
        } else {
            buf.extend_from_slice(&encode_unfiltered_page(&dblk.elements[start..end], ctx));
        }
    }
    buf
}

/// Decode a fixed-array data block for the layout implied by `hdr` — the
/// inverse of [`encode_fixed_array_dblk`], and the single decode dispatch
/// over non-paged/paged × unfiltered/filtered.
///
/// For the paged layout, pages whose bitmap bit is clear are skipped, not
/// decoded: libhdf5 never writes an uninitialized page, so its bytes are
/// arbitrary and carry no valid checksum. Their elements stay at the
/// undefined-address defaults, which is exactly what the bitmap means.
fn decode_fixed_array_dblk(
    ctx: &FormatContext,
    hdr: &FixedArrayHeader,
    buf: &[u8],
    chunk_size_len: usize,
) -> crate::format::FormatResult<FixedArrayDataBlock> {
    let is_filtered = hdr.client_id == FA_CLIENT_FILT_CHUNK;
    let num_elmts = hdr.num_elmts as usize;

    if !hdr.is_paged() {
        return if is_filtered {
            FixedArrayDataBlock::decode_filtered(buf, ctx, num_elmts, chunk_size_len)
        } else {
            FixedArrayDataBlock::decode_unfiltered(buf, ctx, num_elmts)
        };
    }

    let npages = hdr.npages() as usize;
    let dblk_page_nelmts = hdr.dblk_page_nelmts() as usize;
    let prefix = FixedArrayPagedPrefix::decode(buf, ctx, npages as u64)?;

    let mut dblk = if is_filtered {
        FixedArrayDataBlock::new_filtered(prefix.header_addr, num_elmts)
    } else {
        FixedArrayDataBlock::new_unfiltered(prefix.header_addr, num_elmts)
    };
    dblk.client_id = hdr.client_id;

    // Pages follow the prefix back to back; every page spans the full
    // `dblk_page_nelmts` stride except the last, which holds the remainder.
    let mut pos = prefix.prefix_size;
    for p in 0..npages {
        let start = p * dblk_page_nelmts;
        let end = ((p + 1) * dblk_page_nelmts).min(num_elmts);
        let nelmts = end - start;
        if prefix.page_initialized(p) {
            let page_buf = buf.get(pos..).unwrap_or(&[]);
            if is_filtered {
                let elems = decode_filtered_page(page_buf, ctx, nelmts, chunk_size_len)?;
                dblk.filtered_elements[start..end].clone_from_slice(&elems);
            } else {
                let addrs = decode_unfiltered_page(page_buf, ctx, nelmts)?;
                dblk.elements[start..end].copy_from_slice(&addrs);
            }
        }
        pos += nelmts * hdr.element_size as usize + 4;
    }
    Ok(dblk)
}

/// Interior-mutability cell for per-dataset write state, selected by feature.
///
/// This is the §5-B "cfg-selected interior types" from
/// `docs/threadsafe-fine-grained-locking.md`: the single-threaded build uses a
/// `RefCell` (zero overhead, no atomics), while the `threadsafe` build uses a
/// `Mutex` so two threads can write *different* datasets concurrently while the
/// same dataset's writes serialize. Call sites are identical across both via
/// [`Slot::lock`].
#[cfg(not(feature = "threadsafe"))]
pub(crate) struct Slot<T>(std::cell::RefCell<T>);

#[cfg(not(feature = "threadsafe"))]
impl<T> Slot<T> {
    pub(crate) fn new(value: T) -> Self {
        Slot(std::cell::RefCell::new(value))
    }
    /// Borrow the contents mutably (an uncontended `RefCell` borrow).
    pub(crate) fn lock(&self) -> std::cell::RefMut<'_, T> {
        self.0.borrow_mut()
    }
}

#[cfg(feature = "threadsafe")]
pub(crate) struct Slot<T>(std::sync::Mutex<T>);

#[cfg(feature = "threadsafe")]
impl<T> Slot<T> {
    pub(crate) fn new(value: T) -> Self {
        Slot(std::sync::Mutex::new(value))
    }
    /// Lock the contents. Different datasets hold different slots, so this
    /// only contends when two threads write the *same* dataset.
    pub(crate) fn lock(&self) -> std::sync::MutexGuard<'_, T> {
        self.0.lock().unwrap()
    }
}

/// Proof that the create gate (`create_lock`) is held and the new dataset's
/// name passed the uniqueness check. Only [`Hdf5Writer::begin_create`]
/// constructs one and [`Hdf5Writer::push_dataset`] demands one, so a creator
/// cannot reach the dataset registry while skipping either step. Carries
/// the canonical (link-resolved) name the creator must store, so the
/// registry only ever holds tree paths.
pub(crate) struct CreateGuard<'a> {
    #[cfg(not(feature = "threadsafe"))]
    _gate: std::cell::RefMut<'a, ()>,
    #[cfg(feature = "threadsafe")]
    _gate: std::sync::MutexGuard<'a, ()>,
    /// The dataset name with every group hard link in it resolved.
    pub(crate) name: String,
    /// The group that will hold the new dataset's link, resolved from the
    /// path components of `name`; `None` is the root group. Carried here so
    /// [`Hdf5Writer::push_dataset`] registers the child itself and no creator
    /// can leave a dataset whose name says one thing and whose parent group
    /// says another.
    pub(crate) parent: Option<usize>,
}

/// Reference-counted shared pointer, feature-selected. The single-thread
/// build uses `Rc` (no atomics); the `threadsafe` build uses `Arc` so a
/// dataset/group slot can be cloned out of the registry and locked on its
/// own — letting writes to *different* datasets proceed concurrently without
/// holding the registry lock. See `docs/threadsafe-fine-grained-locking.md`
/// (Stage 3).
#[cfg(not(feature = "threadsafe"))]
pub(crate) type Shared<T> = std::rc::Rc<T>;
#[cfg(feature = "threadsafe")]
pub(crate) type Shared<T> = std::sync::Arc<T>;

/// One dataset's cell in the registry: its metadata slot plus the operation
/// lock that serializes whole logical operations on it. Both live in one
/// allocation so they cannot fall out of step — every dataset has its op
/// lock by construction.
pub(crate) struct DatasetCell {
    /// Serializes one *whole* logical operation on this dataset.
    ///
    /// The metadata slot below serializes each individual acquisition, but a
    /// multi-acquisition operation — take the append buffer → write chunks →
    /// re-buffer the tail → extend, or flush-then-overwrite in a slice write
    /// — would interleave with a concurrent same-dataset operation *between*
    /// its acquisitions under `threadsafe`. Public write entries take this
    /// lock and delegate to `_inner` variants; `_inner` variants and the
    /// `pub(crate)` write helpers require the caller to hold it (or to hold
    /// the writer exclusively via `&mut`, as close and the SWMR wrapper do).
    ///
    /// Not reentrant: the single-thread build's `RefCell` panics instantly
    /// on a nested acquisition, so a missed entry/inner split fails loudly
    /// in every test run rather than deadlocking only under `threadsafe`.
    ///
    /// Lock order: `create_lock → op → registry spine → metadata slot`. An
    /// op lock is never held across another dataset's op lock, and no
    /// op-lock holder takes `create_lock`, so the order is acyclic.
    pub(crate) op: Slot<()>,
    info: Slot<DatasetInfo>,
}

impl DatasetCell {
    pub(crate) fn new(info: DatasetInfo) -> Self {
        DatasetCell {
            op: Slot::new(()),
            info: Slot::new(info),
        }
    }

    /// Borrow the metadata slot (a single acquisition; see [`Self::op`] for
    /// whole-operation serialization).
    #[cfg(not(feature = "threadsafe"))]
    pub(crate) fn lock(&self) -> std::cell::RefMut<'_, DatasetInfo> {
        self.info.lock()
    }

    /// Lock the metadata slot (a single acquisition; see [`Self::op`] for
    /// whole-operation serialization).
    #[cfg(feature = "threadsafe")]
    pub(crate) fn lock(&self) -> std::sync::MutexGuard<'_, DatasetInfo> {
        self.info.lock()
    }
}

/// A single dataset's [`DatasetCell`], reference-counted so a writer can
/// clone it out of the registry (releasing the registry lock) and then lock
/// just this one dataset. Two threads writing different datasets take
/// different `DatasetRef` locks and never contend; the same dataset's writes
/// serialize, which is required because one chunk index is not concurrently
/// mutable.
pub(crate) type DatasetRef = Shared<DatasetCell>;

/// A single group's metadata behind its own [`Slot`], reference-counted like
/// [`DatasetRef`].
pub(crate) type GroupRef = Shared<Slot<GroupInfo>>;

/// Appended frames held back until they complete a chunk.
///
/// The buffer is the sole authority for rows `base .. base + frames`: the
/// file's chunks do not hold them yet, and any operation that writes those
/// rows must go through [`Hdf5Writer::flush_append_buffer`] first. `base` is
/// recorded when the frames are buffered — never derived from the current
/// extent, which an `extend_dataset` can move independently.
pub struct AppendBuffer {
    /// Absolute row of the first buffered frame.
    pub base: u64,
    /// Number of buffered frames.
    pub frames: u64,
    /// The frames' bytes, `frames` whole rows, row-major.
    pub bytes: Vec<u8>,
}

/// One file a dataset's raw data lives in, as the writer holds it: the name
/// the I/O path opens, together with the local-heap offset the External File
/// List message stores that name as.
///
/// The two halves are one entry rather than two parallel lists because they
/// describe one slot — the message encodes `name_offset`, and every read or
/// write of the slot's bytes opens `name`; splitting them is what lets a
/// rewrite pair a name with another slot's offset.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExternalFile {
    /// The file name exactly as the heap stores it. Resolved against
    /// `HDF5_EXTFILE_PREFIX` at I/O time, never here — the same rule the read
    /// side follows.
    pub name: String,
    /// Where `name` sits in the local heap at [`ExternalStorage::heap_addr`].
    pub name_offset: u64,
    /// Byte offset within `name` where this slot's region begins.
    pub offset: u64,
    /// Bytes of the dataset's raw data this slot holds.
    pub size: u64,
}

/// A dataset whose contiguous raw data lives outside this file — the External
/// File List message (`H5O_EFL_ID`) and the local heap its names are in.
///
/// The data layout message of such a dataset still says `Contiguous`, with
/// its address left undefined: it is this message's presence that makes
/// libhdf5 route the dataset's I/O through `H5D_LOPS_EFL` (H5Dlayout.c).
#[derive(Debug, Clone)]
pub struct ExternalStorage {
    /// Address of the local heap header holding every slot's name.
    pub heap_addr: u64,
    /// The files, in the order their regions concatenate into the dataset's
    /// logical byte range.
    pub files: Vec<ExternalFile>,
    /// The prefix every one of those names is joined against, and the open
    /// that settled it. Lives here rather than on [`DatasetInfo`] so a
    /// dataset with no external storage cannot carry a prefix and a dataset
    /// with external storage cannot lack one.
    prefix: EfilePrefix,
}

/// The expanded external file prefix in force for one dataset, and the open
/// that decided it — libhdf5's `dset->shared->extfile_prefix`.
///
/// `H5D__build_file_prefix` runs it once per open of the shared info, from
/// the dapl of `H5D__create` (H5Dint.c:1318) or of the `H5D__open` that
/// found no shared info yet (:1537), and both `H5D__efl_read` and
/// `H5D__efl_write` then join against that one answer (H5Defl.c:315-317,
/// :429-431). Measured under libhdf5 1.14.6 and 2.0.0: `H5Dcreate2` with a
/// dapl naming a directory creates the raw data file there at `H5Dwrite`,
/// and `HDF5_EXTFILE_PREFIX` shadows that property on the write path exactly
/// as it does on the read path.
#[derive(Debug, Clone, Default)]
struct EfilePrefix {
    /// The expansion itself; `None` is "no prefix", which leaves a stored
    /// name to resolve against the process's current directory.
    expanded: Option<PathBuf>,
    /// The open that decided [`expanded`](Self::expanded). An expired handle
    /// means no open is holding the answer any more, so the next one settles
    /// it afresh — which is the state a dataset this session reopened starts
    /// in, `H5Fopen` opening no dataset of its own.
    open: std::sync::Weak<()>,
}

impl ExternalStorage {
    /// The message this storage encodes to (`H5O_efl_t`).
    fn message(&self) -> ExternalFileListMessage {
        ExternalFileListMessage {
            heap_addr: self.heap_addr,
            slots: self
                .files
                .iter()
                .map(
                    |f| crate::format::messages::external_file_list::ExternalFileSlot {
                        name_offset: f.name_offset,
                        offset: f.offset,
                        size: f.size,
                    },
                )
                .collect(),
        }
    }

    /// Bytes the slots reserve in total (`H5O_efl_total_size`), saturating
    /// rather than wrapping so an overflowing list reads as "as large as it
    /// gets" and passes any size check instead of failing one.
    fn total_size(&self) -> u64 {
        self.files
            .iter()
            .fold(0u64, |acc, f| acc.saturating_add(f.size))
    }
}

/// A dataset whose elements are read out of other datasets — the virtual
/// layout message (`H5D_VIRTUAL`) and the mapping list it points at.
///
/// The mappings live in one global heap object rather than in the header
/// (`H5D__virtual_store_layout`), so the layout message carries only its
/// address and index; the list itself is kept here so a rewrite of the header
/// can re-emit the message pointing at the same object.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VirtualStorage {
    /// Address of the global heap collection holding the mapping list.
    pub heap_addr: u64,
    /// Index of the mapping-list object within that collection.
    pub heap_index: u32,
    /// The mappings themselves, in the order they were declared — which is
    /// the order libhdf5 resolves overlapping ones in.
    pub mappings: Vec<VirtualMapping>,
}

/// Where a contiguous dataset's raw bytes live, read off its registry entry
/// so the write itself can run with the slot unlocked.
///
/// The one place the local-versus-external-versus-nowhere choice is made; see
/// [`DatasetInfo::contiguous_target`].
enum ContiguousTarget {
    /// A block in this file, starting at this address.
    Local(u64),
    /// The files an External File List names, in dataset order, and the
    /// prefix in force for the open doing the writing — carried together
    /// because a slot name means nothing without it.
    External {
        files: Vec<ExternalFile>,
        prefix: Option<PathBuf>,
    },
    /// Nowhere: the dataset is virtual, and every element of it is stored in
    /// whichever source dataset its mappings send that element to.
    Virtual,
}

/// What a writer-mode `H5Dataset` handle is built from — the shape and
/// element width it answers questions with, the chunk index it writes
/// through, and the open it holds.
pub(crate) struct DatasetHandleParts {
    pub(crate) shape: Vec<usize>,
    pub(crate) element_size: usize,
    /// `None` for storage that is not chunked.
    pub(crate) chunk_index: Option<ChunkIndexKind>,
    /// Keeps this open alive; see [`Hdf5Writer::bind_efile_prefix`].
    pub(crate) open: Option<crate::io::reader::DatasetOpenToken>,
}

impl ContiguousTarget {
    /// Whether this target is storage bytes can be written into at all —
    /// false only for [`ContiguousTarget::Virtual`], which names sources
    /// rather than storage.
    fn is_storage(&self) -> bool {
        !matches!(self, Self::Virtual)
    }
}

/// The one refusal of a write into a virtual dataset, so the two paths that
/// can reach one — [`Hdf5Writer::write_contiguous_bytes`] and the pre-insert
/// gate of [`Hdf5Writer::write_vlen_strings_slice`] — say the same thing.
///
/// libhdf5 does take this write, pushing each element through the mapping
/// that covers it into the source dataset holding it (`H5D__virtual_write`);
/// this writer never opens a source file, so it refuses rather than dropping
/// the bytes somewhere they cannot be read back from.
/// The legality checks `H5Pset_virtual` runs over one mapping —
/// `H5D_virtual_check_mapping_pre` and `H5D_virtual_check_mapping_post`
/// (H5Dvirtual.c).
///
/// The two upstream checks that need the *source dataset's* own extent (the
/// limited/limited element-count match, and a printf mapping's single-block
/// match) are not run here for the same reason upstream skips them when the
/// source space status is `H5O_VIRTUAL_STATUS_INVALID`: a mapping may name a
/// source that does not exist yet, and nothing here opens one.
fn check_virtual_mapping(dataset: &str, m: &VirtualMapping) -> IoResult<()> {
    for (which, sel) in [
        ("virtual", &m.virtual_selection),
        ("source", &m.source_selection),
    ] {
        if matches!(sel, Selection::Points(_)) {
            return Err(crate::io::IoError::Unsupported(format!(
                "virtual dataset '{dataset}' has a point {which} selection, which \
                 H5D_virtual_check_mapping_pre refuses for every virtual dataset mapping \
                 (\"point selections not currently supported with virtual datasets\")"
            )));
        }
    }

    let unlim_virtual = m.virtual_selection.unlim_dim().is_some();
    let unlim_source = m.source_selection.unlim_dim().is_some();

    // Both sides unbounded: the mapping grows with its source, so the slices
    // they exchange must be the same shape whatever either extent becomes.
    if unlim_virtual && unlim_source {
        if let (Some(v), Some(sr)) = (
            regular_hyperslab(&m.virtual_selection),
            regular_hyperslab(&m.source_selection),
        ) {
            let (nv, ns) = (v.num_elem_non_unlim(), sr.num_elem_non_unlim());
            if nv != ns {
                return Err(crate::io::IoError::InvalidState(format!(
                    "virtual dataset '{dataset}' maps an unlimited source selection onto an \
                     unlimited virtual selection, but a slice of the non-unlimited \
                     dimensions holds {ns:?} source elements and {nv:?} virtual ones"
                )));
            }
        }
    }

    // `H5D_virtual_check_mapping_post`: an unlimited virtual selection over a
    // limited source selection is the printf shape, where each block of the
    // virtual selection is filled by a *different* source dataset named by
    // substituting that block's index. It needs a `%b` to name them, and a
    // hyperslab virtual selection to have blocks at all; every other shape
    // needs the opposite, since a substitution with only one block to fill
    // has nothing to vary over.
    let nsubs = parse_source_name(&m.source_file_name)
        .and_then(|f| Ok(f.nsubs() + parse_source_name(&m.source_dset_name)?.nsubs()))
        .map_err(|e| {
            crate::io::IoError::InvalidState(format!(
                "virtual dataset '{dataset}' source name: {e}"
            ))
        })?;
    if unlim_virtual && !unlim_source {
        if nsubs == 0 {
            return Err(crate::io::IoError::InvalidState(format!(
                "virtual dataset '{dataset}' has an unlimited virtual selection, a limited \
                 source selection, and no printf specifiers in source names"
            )));
        }
        if !matches!(m.virtual_selection, Selection::Hyperslab { .. }) {
            return Err(crate::io::IoError::InvalidState(format!(
                "virtual dataset '{dataset}' has a printf mapping whose virtual selection is \
                 not a hyperslab; the substitution runs over the blocks of that hyperslab"
            )));
        }
    } else if nsubs > 0 {
        return Err(crate::io::IoError::InvalidState(format!(
            "virtual dataset '{dataset}' has printf specifier(s) in source name(s) without \
             an unlimited virtual selection and limited source selection"
        )));
    }
    Ok(())
}

/// The regular (start, stride, count, block) form behind a selection, or
/// `None` — the only form that can carry `H5S_UNLIMITED`, so every unlimited
/// check goes through it.
fn regular_hyperslab(sel: &Selection) -> Option<&crate::format::selection::RegularHyperslab> {
    match sel {
        Selection::Hyperslab {
            form: crate::format::selection::Hyperslab::Regular(r),
            ..
        } => Some(r),
        _ => None,
    }
}

fn virtual_write_refused() -> crate::io::IoError {
    crate::io::IoError::Unsupported(
        "cannot write into a virtual dataset: its elements live in the source datasets \
         its mappings name, and this writer does not write through to them — write the \
         source datasets themselves"
            .into(),
    )
}

/// Metadata for a dataset being written.
///
/// The whole struct lives behind a per-dataset [`Slot`] (via [`DatasetRef`]).
/// The streaming write path locks it only briefly — compression runs *outside*
/// the lock — so writes to different datasets do not contend, and a structural
/// op (create/delete) that scans names only momentarily touches a sibling
/// slot.
pub struct DatasetInfo {
    /// Link name within the root group.
    pub name: String,
    /// Element datatype.
    pub datatype: DatatypeMessage,
    /// The committed datatype this dataset shares, when it was created from
    /// one. The type itself stays in [`datatype`](Self::datatype) — the
    /// dataspace, the element width and every payload check need it — and
    /// this says the header must store a pointer to that object instead of a
    /// datatype message of its own.
    pub committed_type: Option<CommittedTypeRef>,
    /// Dataspace (dimensionality).
    pub dataspace: DataspaceMessage,
    /// The object format the reopen found this dataset's messages written in,
    /// `None` for a dataset this session created.
    ///
    /// A rewrite re-encodes the whole header — the shared-message table is
    /// laid out whole, so every heap ID moves and every header naming one has
    /// to be written again. Re-deriving the message format from the reopened
    /// session's bounds would upgrade messages the file already has, which
    /// libhdf5 never does: it grows a header in place and leaves every
    /// message it did not touch alone. The same rule the reopen already
    /// applies to a group it found in a symbol table
    /// ([`uses_symbol_table`](Hdf5Writer::uses_symbol_table)) — what the file
    /// says governs, not what this session's bound would have chosen.
    pub read_format: Option<ObjectFormat>,
    /// File offset of the dataset's object header (set during finalize).
    pub obj_header_addr: u64,
    /// File offset of the raw data block (contiguous only).
    pub data_addr: u64,
    /// Size of the raw data in bytes (contiguous only).
    pub data_size: u64,
    /// The raw data itself, for a compact dataset — the whole image, which
    /// [`build_dataset_header`](Hdf5Writer::build_dataset_header) puts inside
    /// the data layout message rather than in a block of its own. `Some` is
    /// what makes a dataset compact, and the buffer is created at its final
    /// length (filled, as `H5D__compact_fill` does, before any write), so it
    /// is also the dataset's byte count; `data_addr`/`data_size` stay at the
    /// "no block in the file" values a compact dataset shares with a NULL one.
    pub compact: Option<Vec<u8>>,
    /// The files this dataset's contiguous raw data lives in, when it lives
    /// outside this HDF5 file. `Some` is what makes a contiguous dataset
    /// externally stored: its `data_addr` stays [`UNDEF_ADDR`] and every byte
    /// goes to the files named here instead of to a block of this file's own.
    pub external: Option<ExternalStorage>,
    /// The source datasets this dataset's elements are read from, when it is
    /// virtual. `Some` is what makes it virtual, and it stores nothing of its
    /// own: `data_addr`/`data_size` keep the "no block in this file" values a
    /// compact dataset also has.
    pub virtual_storage: Option<VirtualStorage>,
    /// Chunked storage info (None for contiguous).
    pub chunked: Option<ChunkedDatasetInfo>,
    /// Fixed array chunked storage info.
    pub fixed_array: Option<FixedArrayDatasetInfo>,
    /// B-tree v2 chunked storage info.
    pub btree_v2: Option<Bt2DatasetInfo>,
    /// Implicit (no structure) chunked storage info.
    pub implicit: Option<ImplicitDatasetInfo>,
    /// Single-chunk chunked storage info: the whole (fixed) dataspace is
    /// exactly one chunk.
    pub single_chunk: Option<SingleChunkDatasetInfo>,
    /// Version-1 B-tree chunked storage info — the classic-format index.
    pub btree_v1: Option<BtreeV1DatasetInfo>,
    /// Appended frames not yet written to chunks, `None` when empty.
    pub append: Option<AppendBuffer>,
    /// Attributes attached to this dataset.
    pub attributes: Vec<AttributeEntry>,
    /// File offset where the dataset object header was written (for SWMR in-place rewrites).
    pub obj_header_written_addr: Option<u64>,
    /// Encoded size of the dataset object header (for verifying in-place rewrites fit).
    /// Every block the object's on-disk header occupies, chunk 0 first, or
    /// empty when it has none yet. A rewrite keeps chunk 0's block — its
    /// address is what every reference to the object holds — and frees the
    /// rest, so a continuation block left behind is space no free-space
    /// manager records.
    pub obj_header_blocks: crate::io::object_header_io::HeaderBlocks,
    /// Filter pipeline for compressed chunks.
    pub filter_pipeline: Option<FilterPipeline>,
    /// Soft-deleted: excluded from finalize output.
    pub deleted: bool,
    /// The dataspace extent changed this session (`extend_dataset` /
    /// `set_dataset_extent`). On a reopened dataset the finalize gate
    /// otherwise infers "modified" from `chunks_written` alone, and a
    /// session that only changed the extent would keep the old on-disk
    /// header — silently dropping the new shape.
    pub extent_dirty: bool,
    /// Something the object header encodes changed this session without
    /// touching the dataset's storage — an attribute set or removed, a fill
    /// value defined. See [`header_stale`](DatasetInfo::header_stale).
    pub header_dirty: bool,
    /// The hard link count the on-disk header was written with, so finalize
    /// can tell that this session changed it.
    ///
    /// A count, not a flag, because the count is what the header records and
    /// the ways to change it are many: creating a link, unlinking one,
    /// deleting a link's parent group, promoting a link to a primary name.
    /// Comparing the value closes all of them at once, where a dirty flag
    /// would have to be set at each and would be forgotten at the next one
    /// added.
    pub nlink_written: u32,
    /// When the link naming this dataset was created; see
    /// [`GroupInfo::creation_seq`].
    pub creation_seq: u64,
    /// How this dataset records creation order for its attributes — the
    /// file's creation-order policy captured when the dataset was created,
    /// the way libhdf5 captures the DCPL. A dataset holds no links, so only
    /// the attribute half of [`TrackOrder`] applies to it.
    pub track_attr_order: CreationOrder,
    /// User-defined fill value bytes (exactly one element wide). `None`
    /// means default zero-fill; `Some` is emitted as a `fill_defined = 2`
    /// fill-value message in the dataset object header.
    pub fill_value: Option<Vec<u8>>,
    /// Fill value write time (`H5Pset_fill_time`'s `H5D_fill_time_t`, one of
    /// [`FILL_TIME_ALLOC`], [`FILL_TIME_NEVER`], [`FILL_TIME_IFSET`]),
    /// emitted verbatim into the fill-value message's write-time field.
    /// Defaults to `FILL_TIME_IFSET`, `H5D_CRT_FILL_TIME_DEF` — what a fresh
    /// dataset creation property list carries until `set_dataset_fill_time`
    /// says otherwise.
    pub fill_time: u8,
    /// Layout message version for chunked storage: 4, or 5 when the chunk
    /// index encodes stored chunk sizes in a fixed `sizeof_size` field
    /// (libhdf5 2.0). Chosen at create by `Hdf5Writer::chunk_layout_version`,
    /// preserved from the file on reopen, and emitted verbatim at finalize.
    /// Contiguous datasets ignore it.
    pub layout_version: u8,
    /// The times this object tracks: `Some` exactly when it was created with
    /// `H5Pset_obj_track_times(true)`, `None` when it was not.
    ///
    /// One meaning on both header versions, which store them differently and
    /// store different amounts of them: a version-2 header keeps all four in
    /// its prefix, and a version-1 dataset keeps one, in an `H5O_MTIME_NEW`
    /// message. [`touch_oh`] is the single place that turns this into either
    /// of those, so the four fields are here whichever version the object
    /// has, exactly as `H5O_t` carries `atime`/`mtime`/`ctime`/`btime` for a
    /// version-1 header it never serialises them from.
    pub times: Option<ObjectTimes>,
}

impl DatasetInfo {
    /// Which chunk index this dataset uses, `None` for storage that is not
    /// chunked — the one place the index-carrying fields are turned into an
    /// answer.
    ///
    /// INVARIANT: a chunk index added to this struct is added here. A site
    /// that spells the disjunction out itself is what classifies a new index
    /// as contiguous storage, and contiguous storage is read and written at
    /// [`data_addr`](Self::data_addr) — which a chunked dataset leaves
    /// undefined, so the misclassification is a read or a write at
    /// `UNDEF_ADDR` rather than an error.
    pub(crate) fn chunk_index_kind(&self) -> Option<ChunkIndexKind> {
        if self.chunked.is_some() {
            Some(ChunkIndexKind::ExtensibleArray)
        } else if self.fixed_array.is_some() {
            Some(ChunkIndexKind::FixedArray)
        } else if self.btree_v2.is_some() {
            Some(ChunkIndexKind::BtreeV2)
        } else if self.implicit.is_some() {
            Some(ChunkIndexKind::Implicit)
        } else if self.single_chunk.is_some() {
            Some(ChunkIndexKind::SingleChunk)
        } else if self.btree_v1.is_some() {
            Some(ChunkIndexKind::BtreeV1)
        } else {
            None
        }
    }

    /// Whether this dataset's raw data is stored in chunks — the question
    /// every storage-form test asks, asked in one place.
    pub(crate) fn is_chunked(&self) -> bool {
        self.chunk_index_kind().is_some()
    }

    /// Where this dataset's contiguous raw bytes live, or `None` when it has
    /// no contiguous storage to write into at all — a chunked dataset, a
    /// compact one (whose bytes *are* the layout message), or one whose block
    /// was never allocated.
    ///
    /// INVARIANT: every write of a contiguous dataset's raw bytes picks its
    /// destination here and reaches it through
    /// [`Hdf5Writer::write_contiguous_bytes`]. A site that read `data_addr`
    /// itself would write an externally-stored dataset's data into this file
    /// — at [`UNDEF_ADDR`], the far end of the address space — instead of into
    /// the files its header names, and would do the same to a virtual one,
    /// whose bytes are not this file's to write at all.
    ///
    /// Chunked storage is excluded through
    /// [`chunk_index_kind`](Self::chunk_index_kind) rather than by naming the
    /// index-carrying fields, so an index added to this struct cannot arrive
    /// here as contiguous storage: an implicit-indexed dataset reads
    /// `data_addr` as the base of its chunk grid, which as a contiguous
    /// destination would take a raw write meant for one chunk and lay it over
    /// the whole grid.
    fn contiguous_target(&self) -> Option<ContiguousTarget> {
        if self.is_chunked() || self.compact.is_some() {
            return None;
        }
        if self.virtual_storage.is_some() {
            return Some(ContiguousTarget::Virtual);
        }
        match &self.external {
            Some(ext) => Some(ContiguousTarget::External {
                files: ext.files.clone(),
                prefix: ext.prefix.expanded.clone(),
            }),
            None => {
                (self.data_addr != UNDEF_ADDR).then_some(ContiguousTarget::Local(self.data_addr))
            }
        }
    }

    /// The one run of file bytes an implicitly indexed dataset's chunk grid
    /// is — its start and its length — or `None` when the dataset is indexed
    /// some other way or its space is not allocated yet.
    ///
    /// That index has no per-chunk structure to hold an address in: every
    /// chunk sits at `data_addr + linear_index * chunk_bytes` and the grid is
    /// allocated whole at create (`H5D__none_idx_get_addr`, H5Dnone.c). So the
    /// run is file space this writer allocated, and it is the *only* storage a
    /// chunk of such a dataset can occupy — the builder refuses external and
    /// virtual storage together with chunked storage, which is why
    /// [`allocated_storage_run`](Self::allocated_storage_run) can name it
    /// [`ContiguousTarget::Local`] and no chunk write can reach the other two.
    fn implicit_grid(&self) -> Option<(u64, u64)> {
        let imp = self.implicit.as_ref()?;
        (imp.data_addr != UNDEF_ADDR).then_some((imp.data_addr, imp.data_size))
    }

    /// The run of raw storage this writer *allocated* for the dataset — the
    /// target to initialise it through and its size — or `None` when it
    /// allocated none.
    ///
    /// The two storage forms that are one run of bytes: a contiguous
    /// dataset's data block, and an implicitly indexed dataset's chunk grid.
    /// A compact dataset is excluded (its bytes are its layout message) and so
    /// is every other chunk index, whose chunks are placed one at a time.
    ///
    /// External storage is excluded because this writer does not allocate it:
    /// `H5D__alloc_storage` skips its whole body — the space reservation and
    /// the `H5D__init_storage` that would tile the fill value into it — for a
    /// dataset with an external file list or an empty extent, "we assume that
    /// external storage is already allocated by the caller, or at least will
    /// be before I/O is performed" (H5Dint.c:2270-2274). Measured under
    /// libhdf5 1.14.6 and 2.0.0: a user fill value, `H5D_FILL_TIME_ALLOC` and
    /// `H5D_ALLOC_TIME_EARLY` together leave the raw data file uncreated at
    /// `H5Dcreate2`, and a read before any write fails with "unable to open
    /// external raw data file" rather than reporting the fill.
    ///
    /// INVARIANT: only storage whose bytes this file owns is initialised as
    /// one run, so the allocate-time fill cannot reach the files an external
    /// file list names or the sources a virtual dataset maps.
    fn allocated_storage_run(&self) -> Option<(ContiguousTarget, u64)> {
        match self.implicit_grid() {
            Some((addr, size)) => Some((ContiguousTarget::Local(addr), size)),
            // Not a fallthrough for an unallocated implicit grid:
            // `contiguous_target` answers `None` for every chunked dataset.
            None => match self.contiguous_target() {
                Some(t @ ContiguousTarget::Local(_)) => Some((t, self.data_size)),
                _ => None,
            },
        }
    }

    /// Whether this session wrote chunk data or changed the extent, so the
    /// dataset's index structures have to be re-flushed.
    fn storage_dirty(&self) -> bool {
        self.chunked.as_ref().is_some_and(|c| c.chunks_written > 0)
            || self
                .fixed_array
                .as_ref()
                .is_some_and(|f| f.chunks_written > 0)
            || self.btree_v2.as_ref().is_some_and(|b| b.chunks_written > 0)
            || self.btree_v1.as_ref().is_some_and(|b| b.chunks_written > 0)
            || self
                .single_chunk
                .as_ref()
                .is_some_and(|s| s.chunks_written > 0)
            || self.extent_dirty
    }

    /// Whether a reopened dataset's on-disk object header no longer describes
    /// it.
    ///
    /// INVARIANT: every mutation of something `build_dataset_header` encodes
    /// must show up here. Finalize keeps the original header when this is
    /// false, so a change this misses is not deferred — it is discarded, with
    /// no error to say so. Attributes were the case that proved it: they are
    /// invisible to the chunk-write counters, so an attribute set on a
    /// reopened dataset vanished at close.
    fn header_stale(&self) -> bool {
        self.storage_dirty() || self.header_dirty
    }

    /// The same question for the one thing the dataset itself cannot see: how
    /// many hard links resolve to it. That count lives in the header — an
    /// Object Reference Count message in a version-2 header, the `nlink`
    /// prefix field of a version-1 one — but it is a property of the file's
    /// link graph, so the caller supplies today's value.
    fn header_stale_with(&self, nlink: u32) -> bool {
        self.header_stale() || nlink != self.nlink_written
    }

    /// Record that this dataset's on-disk object header was just written with
    /// `nlink` in it.
    ///
    /// INVARIANT: every write of a dataset object header passes through here.
    /// [`header_stale_with`](Self::header_stale_with) is the one authority for
    /// "does what is on disk still describe this dataset?", and it answers by
    /// comparing against [`nlink_written`](Self::nlink_written) — so a site
    /// that writes a header without saying so leaves that answer describing an
    /// older write. There are three writers: `finalize`, `finalize_for_swmr`
    /// and `write_dataset_header_inplace`. The last recorded nothing; it could
    /// not drift today only because a count it could write is a count that
    /// makes the header outgrow its block, which it refuses. That is a
    /// property of the reference-count message's size, not a rule anything
    /// states, and it is not what the field's definition rests on.
    fn header_written(&mut self, nlink: u32) {
        self.nlink_written = nlink;
    }
}

/// Runtime metadata for a chunked dataset.
pub struct ChunkedDatasetInfo {
    /// Chunk dimension sizes.
    pub chunk_dims: Vec<u64>,
    /// Extensible array parameters.
    pub earray_params: EarrayParams,
    /// File offset of the EA header.
    pub ea_header_addr: u64,
    /// File offset of the EA index block.
    pub ea_iblk_addr: u64,
    /// In-memory copy of the EA header (for updating statistics).
    pub ea_header: ExtensibleArrayHeader,
    /// In-memory copy of the EA index block (for unfiltered datasets).
    pub ea_iblk: ExtensibleArrayIndexBlock,
    /// Number of chunks written so far.
    pub chunks_written: u64,
    /// Filtered index block (for compressed datasets).
    pub filt_iblk: Option<FilteredIndexBlock>,
    /// chunk_size_len for filtered entries.
    pub chunk_size_len: u8,
}

/// Where a newly-created EA data block's address must be recorded.
enum DblkParent {
    /// Slot `index_block.dblk_addrs[idx]`.
    IndexBlock(usize),
    /// Slot `super_block.dblk_addrs[local_dblk]` of the super block at `sblk_addr`.
    SuperBlock {
        sblk_addr: u64,
        ndblks_in_sblk: usize,
        local_dblk: usize,
    },
}

/// Which attribute list an attribute operation targets: the root group's,
/// a group's (by full path), or a dataset's (by writer index).
#[derive(Clone, Copy)]
pub enum AttrTarget<'a> {
    /// The root group's (file-level) attributes.
    Root,
    /// A group's attributes, by full path.
    Group(&'a str),
    /// A dataset's attributes, by writer index.
    Dataset(usize),
}

/// Which chunk index a dataset uses.
///
/// The five above the line are what `H5D__layout_set_latest_indexing`
/// (H5Dlayout.c) picks between once the file format allows a version-4 data
/// layout message, in this precedence: a v2 B-tree for two or more unlimited
/// dimensions, an extensible array for exactly one, and — for a fixed shape —
/// the single-chunk index whenever exactly one chunk covers the whole
/// dataspace (`dims == max_dims == chunk_dims`, checked before either
/// alternative below and taken regardless of filter or allocation-time), else
/// the implicit index when nothing has to be recorded per chunk (no filter,
/// early allocation), else a fixed array. [`BtreeV1`](Self::BtreeV1) is not
/// one of them: it belongs to the version-3 layout message, and a file whose
/// superblock is older than version 2 can carry no other.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub(crate) enum ChunkIndexKind {
    ExtensibleArray,
    FixedArray,
    BtreeV2,
    Implicit,
    SingleChunk,
    BtreeV1,
}

/// A chunked dataset's grid geometry, snapshotted out of its slot.
///
/// The single owner of chunk-grid arithmetic: how many chunks span each
/// dimension, where a coordinate sits in the row-major order the array
/// indices record, and how many bytes one chunk holds.
struct ChunkGeometry {
    kind: ChunkIndexKind,
    dims: Vec<u64>,
    max_dims: Option<Vec<u64>>,
    chunk_dims: Vec<u64>,
    element_size: u64,
}

impl ChunkGeometry {
    /// Unfiltered byte size of one whole chunk.
    fn chunk_bytes(&self) -> u64 {
        self.chunk_dims.iter().product::<u64>() * self.element_size
    }

    /// Row-major position of `coords` in the chunk grid — the linear index an
    /// extensible or fixed array records the chunk under, computed against
    /// the maximum-extent grid by [`crate::io::chunk_grid::linear_index`].
    fn linear_index(&self, coords: &[u64]) -> IoResult<u64> {
        crate::io::chunk_grid::linear_index(
            &self.dims,
            self.max_dims.as_deref(),
            &self.chunk_dims,
            coords,
        )
    }
}

/// The refusal every attribute mutation gets while SWMR streaming is
/// active, from the two owners of attribute-list change
/// ([`Hdf5Writer::set_attribute`] and `evict_attr`).
fn swmr_attr_error(name: &str) -> crate::io::IoError {
    crate::io::IoError::InvalidState(format!(
        "cannot add or modify attribute '{name}' during SWMR streaming: object \
         headers are frozen while readers stream, and a superseded variable-length \
         value's heap storage could never be reclaimed; set attributes before \
         start_swmr (libhdf5 forbids attribute changes during SWMR writes too)"
    ))
}

/// Where an attribute arriving at [`Hdf5Writer::insert_attribute`] came from.
///
/// The variable-length setters have to evict before they allocate — the
/// free-before-alloc order — so by the time the replacement is inserted the
/// list no longer holds the entry it replaces, and the ordinary "already
/// present, so keep its index" test cannot see it. `H5A__attr_write` does not
/// create the attribute again, so the index travels with the eviction rather
/// than being stamped afresh; without it a rewritten attribute takes the set's
/// running maximum and moves to the end of the creation order.
#[derive(Debug, Clone, Copy)]
enum AttrOrigin {
    /// A new attribute, which takes the set's next creation index.
    Created,
    /// A value written over an attribute this writer has just evicted, which
    /// keeps that attribute's creation index — `None` when the object tracks
    /// no order, and so records none. An eviction that found nothing to remove
    /// answers `Created`: what follows it is a create like any other.
    Rewritten(Option<u16>),
}
use AttrOrigin::{Created, Rewritten};

/// Take an object's attributes into the append session, or refuse the reopen.
///
/// Append mode rebuilds every object header it touches out of the attributes
/// read from it, so what this returns is what the object will still have when
/// the session finalizes. An attribute set that could not be read whole —
/// `ObjectAttributes::into_complete` refuses it — would come back as the part
/// that did read, silently deleting the rest.
///
/// Left to surface at `finalize`, that failure would land after this session's
/// chunk data and indices had already been written past the allocation point
/// the superblock still records, leaving a file libhdf5 reads as truncated.
/// Refusing the open leaves it untouched.
///
/// Size is no longer a reason to refuse: an attribute too large for a header
/// message goes back out through dense storage, the form libhdf5 read it from.
///
/// The set comes back in creation-index order, which is the order the registry
/// holds attributes in for an object made in this session too. A dense set is
/// read through the name index, so the order it arrives in is the order a hash
/// walk took; sorting here is what makes "the list is in creation order" true
/// of a reopened object as well, without any later stage having to know which
/// storage form the attributes came out of. Attributes of an untracked object
/// carry no index and keep the order they were read in.
fn take_reopened_attributes(
    attrs: crate::io::reader::ObjectAttributes,
    owner: &str,
) -> IoResult<Vec<AttributeEntry>> {
    let mut attrs = attrs.into_complete(owner)?;
    attrs.sort_by_key(|a| a.creation_index());
    Ok(attrs)
}

/// The creation-order policy an on-disk object header declares — the single
/// owner of the recovery rule, used for the root group, every reopened group
/// and (through its attribute half) every reopened dataset.
///
/// The two halves come from two different places, and reading one for both is
/// how a file that sets only one of them came back with both or neither:
///
///   * links — the `Link Info` message's flag bits, which is what
///     `H5Pget_link_creation_order` reads (`H5G__get_create_plist`). A group
///     with no such message (or one this crate cannot decode) tracks nothing;
///     so does every dataset, which has no links to order.
///   * attributes — the object header's own flag bits, which is what
///     `H5Pget_attr_creation_order` reads (`H5Pocpl.c`). The `Attribute Info`
///     message carries the same two bits, but the header is the authority
///     libhdf5 consults, and it is present even when the object has no
///     attributes yet.
fn recover_track_order(
    header: &crate::format::object_header::ObjectHeader,
    ctx: &FormatContext,
) -> TrackOrder {
    let links = header
        .messages
        .iter()
        .find(|m| m.msg_type == crate::format::messages::MSG_LINK_INFO)
        .and_then(|m| LinkInfoMessage::decode(&m.data, ctx).ok())
        .map(|(info, _)| info.creation_order())
        .unwrap_or_default();
    TrackOrder {
        links,
        attrs: header.attribute_creation_order(),
    }
}

/// `H5O_touch_oh` (H5Oint.c:1273): put an object's tracked times where its
/// header version keeps them.
///
/// INVARIANT: every object header this writer builds passes its times through
/// here. The version decides the storage and nothing else does — a caller that
/// set `ObjectHeader::times` itself would hand a version-1 encode a prefix
/// field that version has no room for, and one that added the message itself
/// would put a second copy in a version-2 header.
///
/// `force` is upstream's own parameter, and it is what splits datasets from
/// everything else: it creates the version-1 `H5O_MTIME_NEW` message when the
/// header has none, and only `H5D__update_oh_info` passes it true
/// (H5Dint.c:1022-1026). Every other caller passes false and so creates no
/// message at all, which is why a version-1 group or committed datatype
/// records no time even when it is tracking them. A version-2 header keeps all
/// four times in its prefix whatever `force` says.
fn touch_oh(
    header: &mut ObjectHeader,
    format: ObjectFormat,
    times: Option<ObjectTimes>,
    force: bool,
) {
    let Some(times) = touched_times(times) else {
        return;
    };
    match format {
        ObjectFormat::Modern => header.times = Some(times),
        ObjectFormat::Legacy if force => header.add_message(
            crate::format::messages::MSG_MOD_TIME,
            0x00,
            ModificationTime(times.change).encode(),
        ),
        ObjectFormat::Legacy => {}
    }
}

/// The times a header being (re)written carries, given what the object had.
///
/// Every object header this writer emits is one it is writing *now*, which is
/// what `H5O_touch_oh` is called for: an object that stores times gets its
/// access and change time moved to now, and one that does not store them stays
/// that way — the flag belongs to the object's creation property list, and a
/// rewrite is not a creation.
fn touched_times(times: Option<ObjectTimes>) -> Option<ObjectTimes> {
    times.map(|t| t.touched(now_seconds()))
}

/// Seconds since the epoch, as an object header stores them (`H5_now`).
///
/// Saturates rather than wrapping: the field is a 32-bit count, and a clock
/// past 2106 is better reported as the largest time the format can express
/// than as a time in 1970. A clock before the epoch yields 0, which is what
/// libhdf5 writes for "no time recorded".
fn now_seconds() -> u32 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| u32::try_from(d.as_secs()).unwrap_or(u32::MAX))
}

/// The dense storage an on-disk object header names: the fractal heap and the
/// indices its `Attribute Info` and `Link Info` messages point at.
///
/// A rewrite of that header lays fresh storage out and stops naming this, so
/// what this returns is exactly what the rewrite supersedes and must free.
/// Compact storage names no heap and yields `None` — there is nothing to free
/// and nothing that could be freed twice.
fn superseded_dense(
    header: &crate::format::object_header::ObjectHeader,
    ctx: &FormatContext,
) -> (Option<AttributeInfoMessage>, Option<LinkInfoMessage>) {
    let decode = |msg_type: u8| {
        header
            .messages
            .iter()
            .find(|m| m.msg_type == msg_type)
            .map(|m| m.data.as_slice())
    };
    let attrs = decode(crate::format::messages::MSG_ATTR_INFO)
        .and_then(|d| AttributeInfoMessage::decode(d, ctx).ok())
        .map(|(info, _)| info)
        .filter(|info| info.is_dense());
    let links = decode(crate::format::messages::MSG_LINK_INFO)
        .and_then(|d| LinkInfoMessage::decode(d, ctx).ok())
        .map(|(info, _)| info)
        .filter(|info| info.is_dense());
    (attrs, links)
}

/// One collection block with free space that a later vlen insert may
/// fill — an entry in the writer's CWFS list (libhdf5 `f->shared->cwfs`).
struct CwfsEntry {
    /// Block address of the collection.
    addr: u64,
    /// Declared block size; never changes after allocation.
    size: usize,
    /// Bytes its free-space marker owns, per
    /// [`GlobalHeapCollection::free_space_at`](crate::format::global_heap::GlobalHeapCollection::free_space_at).
    free: usize,
}

/// Maximum CWFS entries tracked — libhdf5's `H5HG_NCWFS` (H5HGpkg.h).
const H5HG_NCWFS: usize = 16;

/// Record a collection with `free` bytes in the CWFS list: update its
/// entry if present, append while the list is short, and otherwise
/// replace the entry with the least free space when this one has more —
/// the retention rule of libhdf5's `H5HG_insert`.
fn cwfs_note(cwfs: &mut Vec<CwfsEntry>, addr: u64, size: usize, free: usize) {
    if let Some(p) = cwfs.iter().position(|e| e.addr == addr) {
        cwfs[p].free = free;
        return;
    }
    if cwfs.len() < H5HG_NCWFS {
        cwfs.insert(0, CwfsEntry { addr, size, free });
        return;
    }
    if let Some(p) = (0..cwfs.len()).min_by_key(|&p| cwfs[p].free) {
        if free > cwfs[p].free {
            cwfs[p] = CwfsEntry { addr, size, free };
        }
    }
}

/// The uniform rejection for `delete_dataset` / `delete_group` while SWMR
/// streaming is active: deleting frees the object's blocks, and a live
/// reader may hold any of their addresses.
fn swmr_delete_error(name: &str) -> crate::io::IoError {
    crate::io::IoError::InvalidState(format!(
        "cannot delete '{name}' during SWMR streaming: a reader may hold the \
         object's header and storage addresses (libhdf5 forbids link deletion \
         during SWMR writes too)"
    ))
}

/// Whether the chunk at grid `coords` lies entirely at or beyond `extent` in
/// some dimension — no element of it would survive a shrink to that extent.
fn chunk_outside_extent(coords: &[u64], chunk_dims: &[u64], extent: &[u64]) -> bool {
    coords
        .iter()
        .zip(chunk_dims)
        .zip(extent)
        .any(|((&c, &cd), &e)| c.saturating_mul(cd) >= e)
}

/// Whether the chunk at grid `coords` keeps elements under `extent` but
/// extends past it in some dimension — a shrink must refill its
/// out-of-extent region with the fill value.
fn chunk_straddles_extent(coords: &[u64], chunk_dims: &[u64], extent: &[u64]) -> bool {
    !chunk_outside_extent(coords, chunk_dims, extent)
        && coords
            .iter()
            .zip(chunk_dims)
            .zip(extent)
            .any(|((&c, &cd), &e)| (c + 1).saturating_mul(cd) > e)
}

/// Overwrite, in `data` (one whole chunk, unfiltered, row-major), every
/// element at or beyond `extent` with the matching bytes of `fill` — a
/// same-sized buffer tiled with the fill value. The caller guarantees the
/// chunk at `coords` straddles `extent`, so every dimension keeps at least
/// one element. Returns the replaced bytes, so a vlen dataset's dead
/// heap references can be released rather than stranded.
fn refill_chunk_beyond_extent(
    data: &mut [u8],
    fill: &[u8],
    coords: &[u64],
    chunk_dims: &[u64],
    extent: &[u64],
    element_size: usize,
) -> Vec<u8> {
    let ndims = chunk_dims.len();
    let keep: Vec<usize> = (0..ndims)
        .map(|d| {
            let origin = coords[d] * chunk_dims[d];
            chunk_dims[d].min(extent[d].saturating_sub(origin)) as usize
        })
        .collect();
    // Row-major walk: for every row (all dimensions but the last),
    // overwrite the whole row when its prefix is outside the keep box,
    // else only the row's out-of-extent tail.
    let row_elems = chunk_dims[ndims - 1] as usize;
    let keep_last = keep[ndims - 1];
    let nrows: u64 = chunk_dims[..ndims - 1].iter().product();
    let mut replaced = Vec::new();
    for r in 0..nrows {
        let mut rem = r;
        let mut in_keep = true;
        for d in (0..ndims - 1).rev() {
            let c = rem % chunk_dims[d];
            rem /= chunk_dims[d];
            if c as usize >= keep[d] {
                in_keep = false;
            }
        }
        let start = if in_keep { keep_last } else { 0 };
        if start == row_elems {
            continue;
        }
        let a = (r as usize * row_elems + start) * element_size;
        let b = (r as usize + 1) * row_elems * element_size;
        replaced.extend_from_slice(&data[a..b]);
        data[a..b].copy_from_slice(&fill[a..b]);
    }
    replaced
}

/// Validate caller-supplied chunk geometry at dataset definition, the rule
/// libhdf5 applies in `H5D__chunk_construct` (H5Dchunk.c): the chunk rank
/// must match the dataspace rank, no chunk dimension may be zero, and a
/// chunk dimension may not exceed a fixed maximum dimension — except in a
/// dimension whose current size is zero, which libhdf5 exempts.
fn validate_chunk_geometry(dims: &[u64], max_dims: &[u64], chunk_dims: &[u64]) -> IoResult<()> {
    let ndims = dims.len();
    if chunk_dims.len() != ndims {
        return Err(crate::io::IoError::InvalidState(format!(
            "chunk shape has {} dimensions but the dataspace has {}",
            chunk_dims.len(),
            ndims
        )));
    }
    if max_dims.len() != ndims {
        return Err(crate::io::IoError::InvalidState(format!(
            "maximum shape has {} dimensions but the dataspace has {}",
            max_dims.len(),
            ndims
        )));
    }
    for d in 0..ndims {
        if chunk_dims[d] == 0 {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk dimension {d} is zero"
            )));
        }
        if dims[d] != 0 && max_dims[d] != u64::MAX && max_dims[d] < chunk_dims[d] {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk dimension {} is {} but the maximum dimension size is {}",
                d, chunk_dims[d], max_dims[d]
            )));
        }
    }
    Ok(())
}

/// An extensible-array index requires at most one unlimited dimension —
/// `H5D__chunk_construct` (H5Dchunk.c) only selects this index for exactly
/// one — at any position: `chunk_grid::linear_index` seeds the unlimited
/// dimension into the slot no down-chunks multiplier touches, the same
/// address libhdf5 reaches by swizzling it to the slowest position
/// (`H5VM_swizzle_coords`, H5Dearray.c). Two or more unlimited dimensions
/// have no finite grid at all; that shape needs a v2 B-tree index instead.
fn ensure_at_most_one_unlimited(max_dims: &[u64]) -> IoResult<()> {
    let unlimited: Vec<usize> = max_dims
        .iter()
        .enumerate()
        .filter(|&(_, &m)| m == u64::MAX)
        .map(|(d, _)| d)
        .collect();
    if unlimited.len() > 1 {
        return Err(crate::io::IoError::InvalidState(format!(
            "an extensible-array index supports at most one unlimited dimension, \
             but dimensions {unlimited:?} are all unlimited; a v2 B-tree index \
             handles two or more"
        )));
    }
    Ok(())
}

/// Reject strings the dataset's declared character set cannot label.
///
/// A Rust `&str` is always UTF-8, so only an ASCII declaration (charset 0)
/// can be violated. libhdf5 stores the bytes unvalidated — its vlen write
/// path has no cset check anywhere — which mislabels them for every reader
/// that trusts the declaration (h5py raises on the same mismatch).
fn ensure_vlen_charset(charset: u8, strings: &[&str]) -> IoResult<()> {
    if charset == 0 {
        if let Some((i, s)) = strings.iter().enumerate().find(|(_, s)| !s.is_ascii()) {
            return Err(crate::io::IoError::InvalidState(format!(
                "string {i} ({s:?}) is not ASCII, but the dataset's character set is"
            )));
        }
    }
    Ok(())
}

/// Runtime metadata for a fixed-array-indexed chunked dataset.
pub struct FixedArrayDatasetInfo {
    /// Chunk dimension sizes.
    pub chunk_dims: Vec<u64>,
    /// File offset of the FA header.
    pub fa_header_addr: u64,
    /// File offset of the FA data block.
    pub fa_dblk_addr: u64,
    /// In-memory copy of the FA header.
    pub fa_header: FixedArrayHeader,
    /// In-memory copy of the FA data block.
    pub fa_dblk: FixedArrayDataBlock,
    /// Number of chunks written so far.
    pub chunks_written: u64,
}

/// Runtime metadata for an implicitly indexed chunked dataset — the index
/// that is no structure at all (`H5Dnone.c`).
///
/// Every chunk of the maximum-extent grid is allocated at create in one
/// contiguous run, in the row-major order [`crate::io::chunk_grid`] defines,
/// so a chunk's address is `data_addr + linear_index * chunk_bytes` and
/// nothing has to be recorded when one is written. libhdf5 picks this index
/// only when that arithmetic is total: no filter (every chunk is exactly
/// `chunk_bytes` long), no unlimited dimension (the run has a finite length),
/// and early allocation (the run exists before any write).
pub struct ImplicitDatasetInfo {
    /// Chunk dimension sizes.
    pub chunk_dims: Vec<u64>,
    /// File offset of the first chunk — the layout message's index address.
    pub data_addr: u64,
    /// Byte length of the whole chunk run: `nchunks * chunk_bytes`.
    pub data_size: u64,
}

/// Runtime metadata for a single-chunk indexed dataset (`H5Dsingle.c`): a
/// fixed dataspace exactly one chunk wide in every dimension
/// (`dims == max_dims == chunk_dims`), so there is exactly one chunk and its
/// address — and, when filtered, its stored size and filter mask — are held
/// directly in the layout message rather than in any index structure.
///
/// libhdf5 selects this index ahead of the implicit and fixed-array indexes
/// whenever the shape qualifies, whether or not the dataset is filtered or
/// early-allocated (`H5D__layout_set_latest_indexing`, H5Dlayout.c).
pub struct SingleChunkDatasetInfo {
    /// Chunk dimension sizes (equal to the dataspace's `dims`).
    pub chunk_dims: Vec<u64>,
    /// File offset of the chunk, [`UNDEF_ADDR`] until the chunk is written
    /// (or immediately, for an unfiltered dataset created with early
    /// allocation).
    pub data_addr: u64,
    /// The chunk's full unfiltered byte length — `chunk_dims.product() *
    /// element_size`, fixed for the dataset's lifetime.
    pub data_size: u64,
    /// Stored (on-disk) byte length: equal to `data_size` when the dataset
    /// carries no filter pipeline; the filtered length once the chunk has
    /// been written, 0 before then.
    pub nbytes: u64,
    /// Filter mask recorded for the stored chunk (bit *i* set means filter
    /// *i* was skipped); meaningful only when the dataset is filtered.
    pub filter_mask: u32,
    /// Chunks written this session (0 or 1) — `storage_dirty`'s signal that
    /// the layout message's address/size/mask fields must be re-flushed.
    pub chunks_written: u64,
    /// Whether this dataset was created with early allocation
    /// (`H5D_ALLOC_TIME_EARLY`) — distinct from `data_addr` being defined,
    /// which also becomes true the moment an incrementally allocated
    /// dataset's one chunk is written; `build_dataset_header` needs this to
    /// tell the two apart when it reports the fill-value message's
    /// allocation time. Only ever set for an unfiltered dataset: a filtered
    /// chunk's stored length is not known until it is compressed, so there
    /// is nothing to allocate ahead of that write regardless of alloc time
    /// (the same gap `create_fixed_array_dataset_with_pipeline` has).
    pub early_alloc: bool,
}

/// One chunk as the version-1 B-tree records it — the key libhdf5 stores
/// (`H5D_btree_key_t`) plus the address it keys.
pub struct BtreeV1ChunkRecord {
    /// Grid position of the chunk. The key's element offsets are derived from
    /// it at encode time (`scaled * chunk_dim`), so this is the one place the
    /// position is stored and the sort order is over these coordinates.
    pub scaled: Vec<u64>,
    /// File offset of the chunk's bytes.
    pub address: u64,
    /// Stored byte length — the filtered length when the dataset is filtered,
    /// the full chunk otherwise. `u32` because the key's field is.
    pub nbytes: u32,
    /// Filter mask: bit `i` set means filter `i` was skipped for this chunk.
    pub filter_mask: u32,
}

/// Runtime metadata for a chunked dataset indexed by a version-1 B-tree —
/// the classic-format chunk index (`H5Dbtree.c`), and the only one a
/// version-0/1 superblock file can carry.
pub struct BtreeV1DatasetInfo {
    /// Chunk dimension sizes.
    pub chunk_dims: Vec<u64>,
    /// Maximum dimensions (u64::MAX = unlimited).
    pub max_dims: Vec<u64>,
    /// The file's v1-B-tree "K" ranks. Every node's width is derived from
    /// them, and they are recorded only in the superblock this file was
    /// opened with — so they are carried rather than re-derived.
    pub config: BTreeV1Config,
    /// The chunks, in key order (`scaled` ascending, lexicographically).
    pub records: Vec<BtreeV1ChunkRecord>,
    /// Pool of node-size blocks holding the tree's nodes, on the same terms
    /// as [`Bt2DatasetInfo::node_addrs`]: a flush re-serializes the whole
    /// bulk-loaded tree over them and allocates only the shortfall, so no
    /// flush can orphan a block it replaced.
    pub node_addrs: Vec<u64>,
    /// Address of the tree's root node — what the version-3 data layout
    /// message carries. `UNDEF_ADDR` until a flush puts a node in the file,
    /// which is the state libhdf5 leaves a chunked dataset in until its first
    /// chunk is written.
    pub root_addr: u64,
    /// Number of chunks written so far.
    pub chunks_written: u64,
}

impl BtreeV1DatasetInfo {
    /// The chunk shape a key's offsets are scaled by: the chunk dimensions
    /// with the element size appended, which is also what the layout message
    /// stores.
    fn key_dims(&self, element_size: u64) -> Vec<u64> {
        let mut dims = self.chunk_dims.clone();
        dims.push(element_size);
        dims
    }

    /// Bulk-load the tree this index's records describe.
    fn build_tree(&self, element_size: u64, sizeof_addr: usize) -> ChunkBTreeV1Tree {
        let dims = self.key_dims(element_size);
        let entries: Vec<(ChunkKey, u64)> = self
            .records
            .iter()
            .map(|r| {
                (
                    ChunkKey::for_chunk(&r.scaled, &dims, r.nbytes, r.filter_mask),
                    r.address,
                )
            })
            .collect();
        // The right boundary closes the tree past its greatest key, which is
        // the last record's — the records are kept in key order.
        let last = self
            .records
            .last()
            .map_or_else(|| vec![0; self.chunk_dims.len()], |r| r.scaled.clone());
        ChunkBTreeV1Tree::build(
            &entries,
            ChunkKey::right_bound(&last, &dims),
            &self.config,
            sizeof_addr,
        )
    }

    /// Where `scaled` sits in [`records`](Self::records): `Ok` at its record,
    /// `Err` at the position one would be inserted at.
    fn position(&self, scaled: &[u64]) -> Result<usize, usize> {
        self.records
            .binary_search_by(|r| r.scaled.as_slice().cmp(scaled))
    }
}

/// Runtime metadata for a B-tree v2 indexed chunked dataset.
pub struct Bt2DatasetInfo {
    /// Chunk dimension sizes.
    pub chunk_dims: Vec<u64>,
    /// File offset of the BT2 header.
    pub bt2_header_addr: u64,
    /// Pool of node-size blocks (the index's
    /// [`node_size`](Bt2ChunkIndex::node_size) bytes each) holding the tree's
    /// nodes, in the order [`Bt2Tree::encode`] emits them.
    ///
    /// The single owner of the tree's node addresses: a flush re-serializes the
    /// whole tree over these blocks and allocates only the shortfall, so no
    /// flush can orphan a block it replaced. Every node is the same size, so a
    /// block stays usable however the tree reshapes.
    ///
    /// The pool holds exactly one block per node after every flush, in both
    /// directions: a taller tree allocates the shortfall, a smaller one frees
    /// the surplus. Nothing here depends on the record count only ever rising,
    /// so a record-removal path can be added to [`Bt2ChunkIndex`] without the
    /// blocks it drops going unreachable.
    pub node_addrs: Vec<u64>,
    /// In-memory chunk index.
    pub index: Bt2ChunkIndex,
    /// Number of chunks written so far.
    pub chunks_written: u64,
}

/// Metadata for a group being written.
pub struct GroupInfo {
    /// Full path of this group (e.g. "/detector" or "/detector/raw").
    pub name: String,
    /// Index of the parent group in the groups vec, or None for root-level groups.
    pub parent: Option<usize>,
    /// Indices of child datasets (into `datasets` vec).
    pub child_datasets: Vec<usize>,
    /// Indices of child groups (into `groups` vec).
    pub child_groups: Vec<usize>,
    /// File offset of this group's object header (set during finalize).
    pub obj_header_addr: u64,
    /// File offset of the on-disk header a reopen found for this group, so
    /// finalize can free the block it supersedes.
    pub obj_header_written_addr: Option<u64>,
    /// Encoded size of that on-disk header (first block).
    /// Every block the object's on-disk header occupies, chunk 0 first, or
    /// empty when it has none yet. A rewrite keeps chunk 0's block — its
    /// address is what every reference to the object holds — and frees the
    /// rest, so a continuation block left behind is space no free-space
    /// manager records.
    pub obj_header_blocks: crate::io::object_header_io::HeaderBlocks,
    /// Soft-deleted: excluded from finalize output.
    pub deleted: bool,
    /// Attributes attached to this group (e.g. NeXus `NX_class`).
    pub attributes: Vec<AttributeEntry>,
    /// When the link naming this group was created, on the writer's single
    /// monotonic sequence. Groups, datasets and hard links share it, so a
    /// parent can order its links the way they were actually made.
    pub creation_seq: u64,
    /// How this group records creation order for its links and, separately,
    /// for its attributes. Creation-order tracking is a property of the
    /// object's creation property list in libhdf5, so it is captured here
    /// when the group is created rather than read from the writer at
    /// finalize: a later change of policy must not rewrite an object already
    /// made.
    pub track_order: TrackOrder,
    /// The times this group tracks, on the same terms as
    /// [`DatasetInfo::times`]. A version-1 group header records none of them:
    /// nothing calls `H5O_touch_oh` with `force` for a group, so the message a
    /// version-1 dataset gets is never created for one.
    pub times: Option<ObjectTimes>,
}

/// One object's creation-order policy, with the two subsystems libhdf5 keeps
/// apart kept apart here too.
///
/// `H5Pset_link_creation_order` and `H5Pset_attr_creation_order` are separate
/// calls reading back out of separate places on disk — the Link Info message
/// and the object header's own flag bits — and a file may set either alone.
/// Carrying them as one flag made a reopen give a one-of-two file both or
/// neither.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct TrackOrder {
    /// Creation order of the links this group holds. Meaningless for a
    /// dataset, which is why `DatasetInfo` keeps only the attribute half.
    pub links: CreationOrder,
    /// Creation order of the attributes attached to this object.
    pub attrs: CreationOrder,
}

impl TrackOrder {
    /// The policy the crate's single `track_order` knob selects: both
    /// subsystems tracked *and* indexed, or neither — the pair h5py's
    /// `File(track_order=True)` writes.
    pub fn uniform(track: bool) -> Self {
        let order = if track {
            CreationOrder::Indexed
        } else {
            CreationOrder::Untracked
        };
        Self {
            links: order,
            attrs: order,
        }
    }
}

/// The object a [`HardLink`] resolves to.
#[derive(Clone, Copy)]
pub enum HardLinkTarget {
    /// Index into the writer's `datasets` vec.
    Dataset(usize),
    /// Index into the writer's `groups` vec.
    Group(usize),
}

/// A user-created hard link: an additional name, in some group, for an
/// object that already exists under its own name.
///
/// The HDF5 file format makes every group entry a `name -> object header
/// address` mapping, so a hard link is just a second such entry pointing at
/// an already-written object. No data is copied.
#[derive(Clone)]
pub struct HardLink {
    /// Parent group index (`None` = the root group).
    pub parent: Option<usize>,
    /// Leaf name of the link within the parent group.
    pub name: String,
    /// Object this link resolves to.
    pub target: HardLinkTarget,
    /// When this link was created; see [`GroupInfo::creation_seq`].
    pub creation_seq: u64,
}

/// A user-created symbolic link: a name in a group whose value is a path
/// rather than an object header address.
///
/// A soft link holds a path within this file; an external link holds a file
/// name and a path within that file. Neither names an object this writer
/// owns, so — unlike [`HardLink`] — nothing about it is resolved: the link is
/// stored as written and answered at traversal time, exactly as `H5Lcreate_soft`
/// and `H5Lcreate_external` store theirs.
#[derive(Clone)]
pub struct SymbolicLink {
    /// Parent group index (`None` = the root group).
    pub parent: Option<usize>,
    /// Leaf name of the link within the parent group.
    pub name: String,
    /// The path (and, for an external link, the file) this link names.
    pub target: LinkTarget,
    /// When this link was created; see [`GroupInfo::creation_seq`].
    pub creation_seq: u64,
}

/// A committed (named) datatype: an object header holding one datatype
/// message and nothing else, reached by a link like any other object.
///
/// `H5Tcommit2` makes the type an object in its own right so several datasets
/// can declare they share it; each of those datasets then stores a pointer to
/// this object header in place of its own datatype message. The object's
/// reference count is therefore the links naming it *plus* the datasets
/// sharing it — `H5O__shared_link_adj` counts a share as a link — and an
/// object no link and no dataset reaches is not written at all.
#[derive(Clone)]
pub struct CommittedDatatype {
    /// Full path with no leading `/`, the form dataset names take.
    pub name: String,
    /// Parent group index (`None` = the root group).
    pub parent: Option<usize>,
    /// The committed type.
    pub datatype: DatatypeMessage,
    /// When the link naming it was created; see [`GroupInfo::creation_seq`].
    pub creation_seq: u64,
    /// The times it tracks, on the same terms as [`DatasetInfo::times`]. A
    /// version-1 committed datatype header records none of them, for the same
    /// reason a version-1 group's does not.
    pub times: Option<ObjectTimes>,
    /// File offset of its object header (set during finalize).
    pub obj_header_addr: u64,
}

/// Where the object header a dataset's shared datatype pointer must name
/// comes from.
///
/// A dataset built on a committed type stores no datatype message: it stores
/// the address of the type's object header. Only the address matters at
/// encode time, but it is knowable at two different moments — a type this
/// session commits has no address until finalize lays the file out, while one
/// a reopen found is already at an address this session will not move. Naming
/// both here keeps [`build_dataset_header`](Hdf5Writer::build_dataset_header)
/// the one place that turns a share into a pointer, whichever way the share
/// arrived.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CommittedTypeRef {
    /// A type committed in this session, by its index in
    /// [`committed_datatypes`](Hdf5Writer::committed_datatypes); its address
    /// is read from that registry once finalize has stamped one.
    Session(usize),
    /// A committed datatype a reopen kept by its bytes, at the object header
    /// address it already occupies.
    Preserved(u64),
}

/// A link a reopened file already held that this writer cannot express.
///
/// Soft, external and user-defined links have no creation, retarget or delete
/// operation here — only hard links do — so a header rewrite that emits what
/// the registry models would erase them. Their encoded `Link` message rides
/// along instead and is written back byte for byte, which preserves every
/// field (name character set, creation order, the link value) without this
/// writer having to model any of them.
///
/// A *hard* link is preserved the same way when the object it names is one
/// the reopen could not model: writing the link back unchanged leaves that
/// object's header exactly where it is, which is the only way the rewrite can
/// keep what it cannot rebuild.
#[derive(Clone)]
pub struct PreservedLink {
    /// Parent group index (`None` = the root group).
    pub parent: Option<usize>,
    /// Leaf name of the link within the parent group.
    pub name: String,
    /// The link's class, decoded once at collection so listings can report
    /// it. Never the source of what gets written — `encoded` is.
    pub class: crate::io::reader::LinkClass,
    /// The encoded `Link` message body, exactly as read from the file.
    pub encoded: Vec<u8>,
    /// Why the object this link names could not be modelled, for the callers
    /// that ask for it by name. `None` when the link's own class — not its
    /// target — is what this writer cannot express.
    pub reason: Option<String>,
    /// What the object this link names is, when the walk could tell. A
    /// listing asks this; `reason` is prose for the caller that asks why.
    pub kind: PreservedKind,
}

/// Every link a reopen walk met, split by what the writer can do with it.
/// A header rewrite emits both halves, so a link in neither half is a link
/// the close would destroy.
#[derive(Default)]
struct CollectedLinks {
    /// Hard links whose target the reopen modelled, with the plan that says
    /// how to rebuild it.
    hard: Vec<(HardEntry, CollectedObject)>,
    /// Links written back unchanged: the class this writer cannot express,
    /// and the hard links whose object it cannot model.
    preserved: Vec<PreservedEntry>,
}

/// One hard link the reopen walk met: what it names, and the exact message
/// that names it.
#[derive(Clone)]
struct HardEntry {
    /// Full link path, in the no-leading-`/` form the registry uses.
    path: String,
    /// Object header address the link names.
    address: u64,
    /// The encoded `Link` message body, exactly as read from the file.
    encoded: Vec<u8>,
}

/// A link the rewrite writes back exactly as it read it.
struct PreservedEntry {
    path: String,
    class: crate::io::reader::LinkClass,
    encoded: Vec<u8>,
    /// Why the object it names could not be modelled; `None` when the link's
    /// own class is what this writer cannot express.
    reason: Option<String>,
    /// What the object is, when the walk could tell.
    kind: PreservedKind,
}

/// What a reopen can do with one object it reached.
///
/// A header rewrite emits a modelled object out of the registry, so the
/// registry may hold an object only when *every* message the model consumes
/// decoded. A partial read is not a smaller object, it is a different one:
/// before this rule a dataset whose datatype message did not decode was
/// registered as a group, and the close rewrote its header as one.
enum ObjectPlan {
    /// A dataset the rewrite can rebuild.
    Dataset(Box<DatasetParts>),
    /// A group the rewrite can rebuild, and the links it holds.
    Group(GroupParts),
    /// An object this writer cannot model, and why. Its header is never
    /// rewritten and never freed; the link naming it is written back byte for
    /// byte, so the object stays exactly as the file already had it — what
    /// libhdf5 does with the parts of a file it does not understand.
    ///
    /// `kind` is what the walk could still tell about the object it is
    /// keeping. Not modelling an object is not the same as not knowing what
    /// it is, and answering the second question with the first is what made
    /// `named_datatype_names` deny, in write mode, a datatype the same file
    /// reports in read mode.
    Preserve { why: String, kind: PreservedKind },
}

/// What a preserved object is, as far as the reopen walk could tell.
///
/// Deliberately not a copy of the reader's `ObjectKind`: that one carries the
/// decoded object, and a preserved object is precisely the one whose contents
/// the writer does not decode. This says only what a listing needs.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum PreservedKind {
    /// The walk did not classify it — or the link's own class, not its
    /// target, is what could not be expressed.
    Unclassified,
    /// A committed (named) datatype, by
    /// [`header_is_committed_datatype`](crate::io::reader::header_is_committed_datatype).
    NamedDatatype,
}

impl ObjectPlan {
    /// An object kept by its bytes, of a kind the walk did not classify.
    ///
    /// Every reason that is a *failure* to read reaches this: a message that
    /// did not decode says nothing about what the object was.
    fn preserve(why: impl Into<String>) -> Self {
        ObjectPlan::Preserve {
            why: why.into(),
            kind: PreservedKind::Unclassified,
        }
    }
}

/// The messages a dataset's rewrite is built from, all decoded.
struct DatasetParts {
    /// Every block the header chain occupies, chunk 0 first. All of them are
    /// superseded: the rewrite re-encodes the whole chain into one fresh
    /// chunk, so a continuation left unfreed is space nothing claims.
    header_blocks: crate::io::object_header_io::HeaderBlocks,
    datatype: DatatypeMessage,
    /// The committed datatype object header `datatype` was read *through*,
    /// when the header stores a pointer instead of a message of its own.
    ///
    /// The literal type is in `datatype` either way, because the read resolves
    /// the pointer before anything decodes it; this is what a rewrite needs to
    /// put the pointer back rather than inline a copy of the named type and
    /// leave `H5Tcommitted` false.
    committed_type: Option<u64>,
    dataspace: crate::format::messages::dataspace::DataspaceMessage,
    /// The object format the reopen found this dataset's messages written in,
    /// read from the dataspace message's own version byte.
    ///
    /// A version-2 superblock does not settle it: `H5F__super_init` raises the
    /// superblock for a shared-message table or non-default file-space
    /// properties without touching `H5F_LOW_BOUND` (H5Fsuper.c:1135, :1144), so
    /// a file created at the earliest bound with either can hold version-1
    /// messages under a version-2 superblock — which is what
    /// `tests/fixtures/sohm_*.h5` are.
    read_format: ObjectFormat,
    layout: crate::format::messages::data_layout::DataLayoutMessage,
    filter_pipeline: Option<FilterPipeline>,
    fill_value: Option<Vec<u8>>,
    /// The fill-value message's write-time byte, preserved across a
    /// rewrite the same way `fill_value` is — an appended-to dataset must
    /// keep the policy libhdf5 (or this writer) declared for it, not fall
    /// back to the `H5D_CRT_FILL_TIME_DEF` a fresh dataset gets.
    fill_write_time: u8,
    attributes: Vec<AttributeEntry>,
    /// The creation-order policy the on-disk header declares; a rewrite that
    /// read it from the writer instead would stamp this session's policy onto
    /// an object libhdf5 created under another.
    track_order: TrackOrder,
    /// The times the on-disk header records, for the same reason: whether an
    /// object tracks them is settled when it is created, not when it is
    /// rewritten. Recovered by [`ObjectHeader::recorded_times`].
    times: Option<ObjectTimes>,
    /// The dense storage the rewrite supersedes and must free.
    dense: DenseCarry,
    /// The External File List the header carries, with each slot's name
    /// already read back out of the local heap the message points at. `None`
    /// for a dataset whose raw data is in this file.
    ///
    /// Carried rather than re-derived because the rewrite has to re-emit the
    /// message: a contiguous layout with an undefined address and no EFL
    /// beside it is a dataset with no data at all, so dropping this on a
    /// header rewrite would silently unlink every external byte.
    external: Option<ExternalStorage>,
}

/// The same for a group, plus the links it holds — decoded once, with the
/// bytes they came from, so the walk and the rewrite agree on its contents.
struct GroupParts {
    header_blocks: crate::io::object_header_io::HeaderBlocks,
    attributes: Vec<AttributeEntry>,
    links: Vec<(crate::format::messages::link::LinkMessage, Vec<u8>)>,
    track_order: TrackOrder,
    times: Option<ObjectTimes>,
    dense: DenseCarry,
    /// The symbol-table storage a classic group's header names — the blocks
    /// the rewrite supersedes. `None` for a link-message group, which has
    /// none. Its links are already in `links`: the walk turns each symbol
    /// table entry into the link message it stands for, so nothing downstream
    /// has to know which of the two forms the group was in.
    stab: Option<StabExtents>,
}

/// The dense storage one reopened object's header names, which the rewrite of
/// that header stops naming and therefore has to free. Both halves are read
/// back before this is built — a heap that could not be read makes the object
/// [`ObjectPlan::Preserve`], so nothing here describes storage whose contents
/// were lost.
#[derive(Default)]
struct DenseCarry {
    attrs: Option<AttributeInfoMessage>,
    links: Option<LinkInfoMessage>,
}

/// A modelled object, as the walk hands it to the registry rebuild. A group's
/// links are not here: the walk followed them, and each child is an entry of
/// its own.
enum CollectedObject {
    Dataset(Box<DatasetParts>),
    Group {
        header_blocks: crate::io::object_header_io::HeaderBlocks,
        attributes: Vec<AttributeEntry>,
        track_order: TrackOrder,
        times: Option<ObjectTimes>,
        dense: DenseCarry,
        stab: Option<StabExtents>,
    },
}

/// The reopen's discovery pass: one walk that classifies every object it
/// reaches and descends into the groups among them.
///
/// Every object the close will touch is decided here and nowhere else, so
/// "modelled or preserved" is a property of the walk rather than of whatever
/// each later stage happened to be able to decode.
struct ReopenWalk<'a> {
    handle: &'a mut FileHandle,
    meta: &'a crate::io::FileMeta,
    out: CollectedLinks,
    /// Object headers already descended into, so hard-link cycles end.
    visited: std::collections::HashSet<u64>,
}

impl<'a> ReopenWalk<'a> {
    fn new(handle: &'a mut FileHandle, meta: &'a crate::io::FileMeta) -> Self {
        Self {
            handle,
            meta,
            out: CollectedLinks::default(),
            visited: std::collections::HashSet::new(),
        }
    }

    /// Everything the walk found.
    fn finish(self) -> CollectedLinks {
        self.out
    }

    /// Decide what the reopen can do with the object at `addr`.
    ///
    /// The single gate: every object the rewrite touches is classified here,
    /// and an object is modelled only when each message the model consumes
    /// decoded. See [`ObjectPlan`] for why anything else must keep its bytes.
    fn plan(&mut self, addr: u64) -> IoResult<ObjectPlan> {
        let (handle, meta) = (&mut *self.handle, self.meta);
        let ctx = &meta.ctx;
        use crate::format::messages::data_layout::DataLayoutMessage;
        use crate::format::messages::dataspace::DataspaceMessage;
        use crate::format::messages::link::{CharacterSet, LinkMessage};
        use crate::format::messages::link_info::LinkInfoMessage;
        use crate::format::messages::shared::MSG_FLAG_SHARED;
        use crate::format::messages::{
            MSG_ATTRIBUTE, MSG_DATASPACE, MSG_DATATYPE, MSG_DATA_LAYOUT, MSG_EXTERNAL_FILE_LIST,
            MSG_FILL_VALUE, MSG_FILTER_PIPELINE, MSG_LINK, MSG_LINK_INFO, MSG_SYMBOL_TABLE,
        };

        // The whole chain, messages and blocks alike: a filter pipeline or an
        // attribute that spilled into a continuation is one the rewrite would
        // otherwise drop, and a continuation block it does not know about is
        // one the rewrite would orphan.
        let (header, header_blocks) =
            match crate::io::object_header_io::read_object_header_with_blocks(handle, meta, addr) {
                Ok(h) => h,
                Err(e) => {
                    return Ok(ObjectPlan::preserve(format!(
                        "its object header chain does not read: {e}"
                    )))
                }
            };

        // The policy, the times and the storage the header declares, read once
        // from the whole chain: all three are properties of the object, not of
        // any one message the loop below happens to reach.
        let track_order = recover_track_order(&header, ctx);
        let times = header.recorded_times();
        let (dense_attrs, dense_links) = superseded_dense(&header, ctx);

        // Attributes come from the reader's collector rather than from the
        // loop below, so compact, dense and shared attributes all reach the
        // rewrite by the one path that knows how to read each of them. An
        // object whose set did not read whole is preserved: a short set here
        // would be a rewrite deleting the attributes it could not read.
        let attributes = match take_reopened_attributes(
            crate::io::reader::collect_object_attributes(handle, ctx, &header),
            &format!("the object at {addr:#x}"),
        ) {
            Ok(a) => a,
            Err(e) => {
                return Ok(ObjectPlan::preserve(format!(
                    "its attributes do not read back whole: {e}"
                )))
            }
        };

        let mut datatype = None;
        let mut dataspace = None;
        let mut layout = None;
        let mut filter_pipeline = None;
        let mut fill_value = None;
        // No fill-value message at all is the library default, the same
        // convention the reader-side decode (`Hdf5Reader::dataset_info`)
        // uses for `fill_defined`.
        let mut fill_write_time: u8 = FILL_TIME_IFSET;
        let mut external = None;
        let mut links = Vec::new();
        let mut stab = None;
        // A datatype, dataspace or layout message says the object is not a
        // group, whether or not the three a dataset needs are all there.
        let mut dataset_shaped = false;

        for msg in &header.messages {
            let consumed = matches!(
                msg.msg_type,
                MSG_DATATYPE
                    | MSG_DATASPACE
                    | MSG_DATA_LAYOUT
                    | MSG_FILTER_PIPELINE
                    | MSG_FILL_VALUE
                    | MSG_EXTERNAL_FILE_LIST
                    | MSG_ATTRIBUTE
                    | MSG_LINK
                    | MSG_LINK_INFO
                    | MSG_SYMBOL_TABLE
            );
            // A shared message holds a reference to where its body lives, not
            // the body. Decoding those bytes as one does not fail loudly — the
            // reference's version byte reads as a version and a class of its
            // own — so the guard is the only thing between a shared datatype
            // and a rewrite that invents a type for it.
            if consumed && msg.flags & MSG_FLAG_SHARED != 0 {
                return Ok(ObjectPlan::preserve(format!(
                    "its message of type {:#04x} is a shared-message reference, which this \
                     writer does not resolve",
                    msg.msg_type
                )));
            }
            macro_rules! consume {
                ($decode:expr, $what:literal) => {
                    match $decode {
                        Ok(v) => v,
                        Err(e) => {
                            return Ok(ObjectPlan::preserve(format!(
                                "its {} message does not decode: {e}",
                                $what
                            )))
                        }
                    }
                };
            }
            match msg.msg_type {
                // The pre-1.6 modification time, a formatted date string
                // (`H5O_MTIME`, type 0x0E). `recorded_times` reads only the
                // modern form, and a rewrite emits only that, so an object
                // carrying this one would come back out with the time it
                // recorded gone. Keeping its bytes is the same answer an
                // undecodable message already gets.
                crate::format::messages::MSG_MOD_TIME_OLD => {
                    return Ok(ObjectPlan::preserve(
                        "it carries a pre-1.6 modification time message, which this writer \
                         reads but does not write",
                    ))
                }
                MSG_DATATYPE => {
                    dataset_shaped = true;
                    let (dt, _) = consume!(DatatypeMessage::decode(&msg.data, ctx), "datatype");
                    datatype = Some(dt);
                }
                MSG_DATASPACE => {
                    dataset_shaped = true;
                    let version = msg.data.first().copied().unwrap_or(1);
                    let (ds, _) = consume!(DataspaceMessage::decode(&msg.data, ctx), "dataspace");
                    dataspace = Some((ds, version));
                }
                MSG_DATA_LAYOUT => {
                    dataset_shaped = true;
                    let (dl, _) =
                        consume!(DataLayoutMessage::decode(&msg.data, ctx), "data layout");
                    layout = Some(dl);
                }
                MSG_FILTER_PIPELINE => {
                    let (p, _) = consume!(FilterPipeline::decode(&msg.data), "filter pipeline");
                    if !p.filters.is_empty() {
                        filter_pipeline = Some(p);
                    }
                }
                MSG_FILL_VALUE => {
                    let (fv, _) = consume!(FillValueMessage::decode(&msg.data), "fill value");
                    if fv.fill_defined == 2 {
                        fill_value = fv.fill_value;
                    }
                    fill_write_time = fv.fill_write_time;
                }
                MSG_EXTERNAL_FILE_LIST => {
                    dataset_shaped = true;
                    let (efl, _) = consume!(
                        ExternalFileListMessage::decode(&msg.data, ctx),
                        "external file list"
                    );
                    // The names live in a local heap of their own, so the
                    // rewrite cannot re-emit the message from its bytes alone
                    // — it has to be able to point at the same strings. A heap
                    // that does not read back leaves the object preserved,
                    // which is what keeps its data reachable.
                    let resolved = match crate::io::reader::Hdf5Reader::resolve_external_file_slots(
                        handle, ctx, &efl,
                    ) {
                        Ok(r) => r,
                        Err(e) => {
                            return Ok(ObjectPlan::preserve(format!(
                                "its external file list names do not read back: {e}"
                            )))
                        }
                    };
                    external = Some(ExternalStorage {
                        heap_addr: efl.heap_addr,
                        // `H5Fopen` opens no dataset, so nothing has read a
                        // dapl for this one yet; the first handle it hands
                        // out settles the prefix.
                        prefix: EfilePrefix::default(),
                        files: efl
                            .slots
                            .iter()
                            .zip(resolved)
                            .map(|(slot, seg)| ExternalFile {
                                name: seg.name,
                                name_offset: slot.name_offset,
                                offset: slot.offset,
                                size: slot.size,
                            })
                            .collect(),
                    });
                }
                MSG_LINK => {
                    let (l, _) = consume!(LinkMessage::decode(&msg.data, ctx), "link");
                    links.push((l, msg.data.clone()));
                }
                MSG_LINK_INFO => {
                    let (li, _) = consume!(LinkInfoMessage::decode(&msg.data, ctx), "link info");
                    // Once a group holds enough links libhdf5 moves them into
                    // the fractal heap this message names and writes no `Link`
                    // messages at all. Reading them back is what makes the
                    // rewrite emit the group with its children; a rewrite from
                    // the header messages alone emitted it empty, orphaning
                    // every object below it.
                    if li.fractal_heap_address != UNDEF_ADDR {
                        let dense = match crate::io::reader::Hdf5Reader::read_dense_links(
                            handle,
                            ctx,
                            li.fractal_heap_address,
                        ) {
                            Ok(l) => l,
                            Err(e) => {
                                return Ok(ObjectPlan::preserve(format!(
                                    "its dense link storage does not read: {e}"
                                )))
                            }
                        };
                        // Re-encoded rather than carried as bytes: a heap
                        // object is not a header message, so there are no
                        // message bytes to carry. The encoding round-trips
                        // through the same decoder that just read it.
                        links.extend(dense.into_iter().map(|l| {
                            let bytes = l.encode(ctx);
                            (l, bytes)
                        }));
                    }
                }
                MSG_SYMBOL_TABLE => {
                    // A classic group keeps no link message at all: its links
                    // are symbol table entries in the B-tree this message
                    // names. Turning each into the link message it stands for
                    // is what lets the rest of the reopen — the walk, the
                    // registry, the preserve path — work on one link model
                    // whichever form the group is in.
                    let Some(s) = Stab::decode(&msg.data, ctx) else {
                        return Ok(ObjectPlan::preserve(
                            "its symbol table message is shorter than the two addresses it \
                             must carry",
                        ));
                    };
                    let contents = match crate::io::symbol_table_io::read_stab(handle, meta, s) {
                        Ok(c) => c,
                        Err(e) => {
                            return Ok(ObjectPlan::preserve(format!(
                                "its symbol table does not read: {e}"
                            )))
                        }
                    };
                    stab = Some(contents.extents);
                    links.extend(contents.links.into_iter().map(|l| {
                        let msg = match l.target {
                            StabTarget::Hard { addr, .. } => LinkMessage::hard(&l.name, addr),
                            StabTarget::Soft { value } => LinkMessage::soft(&l.name, &value),
                        };
                        // An entry carries no character set field, so the link
                        // it stands for has the file default whatever its name
                        // looks like (`H5G__ent_to_link`, H5Gent.c:372).
                        // Deriving one from the name would take a group
                        // libhdf5 wrote with a high-byte ASCII name out of its
                        // symbol table on the rewrite.
                        let msg = msg.with_cset(CharacterSet::Ascii);
                        let bytes = msg.encode(ctx);
                        (msg, bytes)
                    }));
                }
                _ => {}
            }
        }

        // libhdf5 refuses a layout that disagrees with its sibling dataspace
        // and datatype as the dataset opens (`H5O__layout_decode` for the
        // chunk rank, `H5D__compact_init` for the compact size); modelled
        // anyway, the disagreement would be read at the wrong rank or past
        // the compact payload, so the dataset keeps its bytes, exactly as
        // unreadable as the file already had it.
        if let (Some((ds, _)), Some(dt), Some(dl)) = (&dataspace, &datatype, &layout) {
            if let Err(e) = dl.check_against_dataset(ds, dt, ctx) {
                return Ok(ObjectPlan::preserve(format!(
                    "its layout doesn't fit its dataspace and datatype: {e}"
                )));
            }
        }

        match (datatype, dataspace, layout) {
            // A layout `rebuild_dataset` has no arm for leaves the registry
            // entry with an undefined data address, and the close then rewrites
            // the header as a contiguous, unallocated dataset — every element
            // gone, silently. Only the layouts that rebuild are modelled; the
            // rest keep their bytes, as an undecodable message already does.
            // The virtual layout is this.
            (Some(_), Some(_), Some(layout)) if !layout_rebuilds(&layout) => {
                Ok(ObjectPlan::preserve(format!(
                    "its data layout is {}, which this writer reads but does not build",
                    layout.describe()
                )))
            }
            (Some(datatype), Some((dataspace, dataspace_version)), Some(layout)) => {
                // Asked of the raw chain, not of `header`: the read above has
                // already put the named type's message in place of the pointer.
                let committed_type = match crate::io::object_header_io::committed_datatype_address(
                    handle, meta, addr,
                ) {
                    Ok(c) => c,
                    Err(e) => {
                        return Ok(ObjectPlan::preserve(format!(
                            "its shared datatype pointer does not decode: {e}"
                        )))
                    }
                };
                Ok(ObjectPlan::Dataset(Box::new(DatasetParts {
                    header_blocks,
                    datatype,
                    committed_type,
                    dataspace,
                    read_format: if dataspace_version <= 1 {
                        ObjectFormat::Legacy
                    } else {
                        ObjectFormat::Modern
                    },
                    layout,
                    filter_pipeline,
                    fill_value,
                    fill_write_time,
                    attributes,
                    track_order,
                    times,
                    dense: DenseCarry {
                        attrs: dense_attrs,
                        links: dense_links,
                    },
                    external,
                })))
            }
            // A committed (named) datatype has a datatype message and neither
            // of the other two; so does a dataset whose header this crate only
            // half understands. Neither is a group, and modelling either as
            // one is what rewrote them into empty groups. They part company
            // here and nowhere else: the datatype is kept by its bytes like
            // the other, but a listing can still name it.
            _ if crate::io::reader::header_is_committed_datatype(&header) => {
                Ok(ObjectPlan::Preserve {
                    why: "it is a committed (named) datatype, which this writer carries by \
                          its bytes rather than re-encoding"
                        .into(),
                    kind: PreservedKind::NamedDatatype,
                })
            }
            _ if dataset_shaped => Ok(ObjectPlan::preserve(
                "it carries a datatype, dataspace or layout message but not the three a \
                 dataset is built from; this writer models only groups and datasets",
            )),
            _ => Ok(ObjectPlan::Group(GroupParts {
                header_blocks,
                attributes,
                links,
                track_order,
                times,
                dense: DenseCarry {
                    attrs: dense_attrs,
                    links: dense_links,
                },
                stab,
            })),
        }
    }

    /// Walk `links` (one group's, already decoded), classifying every object
    /// they name and descending into the groups among them.
    fn group(
        &mut self,
        links: &[(crate::format::messages::link::LinkMessage, Vec<u8>)],
        prefix: &str,
        depth: usize,
    ) -> IoResult<()> {
        // Bound nesting depth so a pathologically deep group chain cannot
        // overflow the stack (the `visited` set bounds total work but not
        // recursion depth).
        if depth > 256 {
            return Ok(());
        }
        use crate::format::messages::link::LinkTarget;
        for (link, encoded) in links {
            let full_name = if prefix.is_empty() {
                link.name.clone()
            } else {
                format!("{}/{}", prefix, link.name)
            };

            // Only a hard link names an object this writer can rebuild. Every
            // other class is kept by its bytes, because a close that emitted
            // only what the registry models would drop it from the file.
            let LinkTarget::Hard { address } = &link.target else {
                self.out.preserved.push(PreservedEntry {
                    path: full_name,
                    class: crate::io::reader::LinkClass::from_target(&link.target),
                    encoded: encoded.clone(),
                    reason: None,
                    kind: PreservedKind::Unclassified,
                });
                continue;
            };
            let entry = HardEntry {
                path: full_name.clone(),
                address: *address,
                encoded: encoded.clone(),
            };

            match self.plan(*address)? {
                // Kept by its bytes, exactly as a link class this writer
                // cannot express is: writing the link back unchanged is what
                // leaves the object's header where the file already has it.
                ObjectPlan::Preserve { why, kind } => self.out.preserved.push(PreservedEntry {
                    path: full_name,
                    class: crate::io::reader::LinkClass::Hard,
                    encoded: entry.encoded,
                    reason: Some(why),
                    kind,
                }),
                ObjectPlan::Dataset(parts) => {
                    self.out.hard.push((entry, CollectedObject::Dataset(parts)));
                }
                ObjectPlan::Group(parts) => {
                    self.out.hard.push((
                        entry,
                        CollectedObject::Group {
                            header_blocks: parts.header_blocks,
                            attributes: parts.attributes,
                            track_order: parts.track_order,
                            times: parts.times,
                            dense: parts.dense,
                            stab: parts.stab,
                        },
                    ));
                    // Recurse only into a group's header we have not entered
                    // before — breaks hard-link cycles.
                    if self.visited.insert(*address) {
                        self.group(&parts.links, &full_name, depth + 1)?;
                    }
                }
            }
        }
        Ok(())
    }
}

/// Rebuild one reopened dataset's in-memory registry entry, storage and
/// all, from the header messages the walk decoded.
///
/// Fails when the chunk index the file names does not read back. The
/// caller answers that by preserving the object rather than registering
/// a dataset whose index has forgotten where its chunks are: the close
/// rewrites what the registry holds, so an index rebuilt from the part of
/// it that decoded would strand every chunk it could not read.
fn rebuild_dataset(
    handle: &mut FileHandle,
    meta: &FileMeta,
    file_size: u64,
    name: String,
    obj_addr: u64,
    parts: DatasetParts,
) -> IoResult<DatasetInfo> {
    let ctx = &meta.ctx;
    let DatasetParts {
        header_blocks,
        datatype: dt,
        committed_type,
        dataspace: ds,
        read_format,
        layout: dl,
        filter_pipeline: fp,
        fill_value,
        fill_write_time,
        attributes: attrs,
        track_order,
        times,
        dense: _,
        external,
    } = parts;

    let mut info = DatasetInfo {
        name,
        datatype: dt,
        // The named type's own object is preserved by its bytes, so the
        // address the walk read the pointer from is the address it will still
        // be at when this header is written back.
        committed_type: committed_type.map(CommittedTypeRef::Preserved),
        read_format: Some(read_format),
        external,
        virtual_storage: None,
        dataspace: ds,
        obj_header_addr: obj_addr,
        data_addr: UNDEF_ADDR,
        data_size: 0,
        compact: None,
        chunked: None,
        fixed_array: None,
        implicit: None,
        single_chunk: None,
        btree_v1: None,
        btree_v2: None,
        append: None,
        attributes: attrs,
        obj_header_written_addr: Some(obj_addr),
        obj_header_blocks: header_blocks,
        filter_pipeline: fp,
        deleted: false,
        extent_dirty: false,
        header_dirty: false,
        // Stamped by the caller once the whole link graph is registered: it
        // is the count of links reaching this object, which one dataset's
        // parts cannot see.
        nlink_written: 1,
        // Stamped by the caller, which knows the order the walk met each
        // object; the rebuild sees one dataset at a time.
        creation_seq: 0,
        track_attr_order: track_order.attrs,
        fill_value,
        fill_time: fill_write_time,
        // Preserve the on-disk layout version so finalize re-encodes
        // what it read: a v5 file reopened and appended to must not be
        // silently downgraded to v4 (the filtered indexes keep their
        // 8-byte size fields, which v4 readers would mis-derive).
        layout_version: match &dl {
            DataLayoutMessage::ChunkedV4 { version, .. } => *version,
            // The classic index has no version above its own: a version-3
            // message is the whole of `H5D__chunk_set_info`'s MAX below the
            // version-4 gate, and re-encoding it any higher would name an
            // index the message cannot carry.
            DataLayoutMessage::ChunkedV3 { .. } => LAYOUT_VERSION_DEFAULT,
            _ => 4,
        },
        times,
    };

    // Reconstruct storage-specific metadata
    debug_assert!(
        layout_rebuilds(&dl),
        "ReopenWalk::plan must preserve a layout this has no arm for"
    );
    match &dl {
        DataLayoutMessage::Contiguous { address, size } => {
            info.data_addr = *address;
            info.data_size = *size;
        }
        // The image is the layout message, so the rebuild carries it out of
        // the header it came from: anything that makes this dataset's header
        // stale rewrites the layout message from `compact`, and a rebuild
        // that left it empty would rewrite the dataset as an unallocated
        // contiguous one — dropping every byte.
        DataLayoutMessage::Compact { data } => {
            info.compact = Some(data.clone());
        }
        // The classic chunk index, reconstructed into the same
        // `BtreeV1DatasetInfo` a chunked dataset *created* in this format
        // gets, so the one set of machinery — `build_tree`, the flush's block
        // pool, `write_chunk`, `extend_dataset`, the prune a delete runs —
        // drives a reopened dataset and a fresh one alike. `root_addr` is what
        // the layout message carries and stays undefined for a dataset whose
        // chunks were never written, exactly as libhdf5 leaves it.
        DataLayoutMessage::ChunkedV3 {
            chunk_dims,
            b_tree_address,
        } => {
            let real_chunk_dims: Vec<u64> = chunk_dims[..chunk_dims.len() - 1].to_vec();
            let mut walk = BtreeV1Walk::new(handle, ctx, &meta.btree, &real_chunk_dims, file_size);
            walk.descend(*b_tree_address, 0)?;
            let BtreeV1Walk {
                records,
                node_addrs,
                ..
            } = walk;
            let max_dims = info
                .dataspace
                .max_dims
                .clone()
                .unwrap_or_else(|| info.dataspace.dims.clone());
            info.btree_v1 = Some(BtreeV1DatasetInfo {
                chunk_dims: real_chunk_dims,
                max_dims,
                // The file's own "K" ranks, not this session's defaults: they
                // set every node's width, so a tree bulk-loaded under the
                // wrong ones would re-serialize over blocks of the wrong size.
                config: meta.btree,
                records,
                node_addrs,
                root_addr: *b_tree_address,
                chunks_written: 0,
            });
        }
        DataLayoutMessage::ChunkedV4 {
            chunk_dims,
            index_address,
            index_type,
            earray_params,
            single_chunk_filter,
            ..
        } => {
            let real_chunk_dims: Vec<u64> = chunk_dims[..chunk_dims.len() - 1].to_vec();

            if *index_type == crate::format::messages::data_layout::ChunkIndexType::ExtensibleArray
            {
                if let Some(params) = earray_params {
                    let ep = EarrayParams {
                        max_nelmts_bits: params.max_nelmts_bits,
                        idx_blk_elmts: params.idx_blk_elmts,
                        sup_blk_min_data_ptrs: params.sup_blk_min_data_ptrs,
                        data_blk_min_elmts: params.data_blk_min_elmts,
                        max_dblk_page_nelmts_bits: params.max_dblk_page_nelmts_bits,
                    };
                    let ndblk_addrs = compute_ndblk_addrs(ep.sup_blk_min_data_ptrs)?;
                    let nsblk_addrs = compute_nsblk_addrs(
                        ep.idx_blk_elmts,
                        ep.data_blk_min_elmts,
                        ep.sup_blk_min_data_ptrs,
                        ep.max_nelmts_bits,
                    )?;

                    // Read EA header
                    let hdr_buf = handle.read_at_most(*index_address, 256)?;
                    let ea_header = ExtensibleArrayHeader::decode(&hdr_buf, ctx)?;

                    let is_filtered = ea_header.class_id
                        == crate::format::chunk_index::extensible_array::EA_CLS_FILT_CHUNK;
                    let chunk_size_len = if is_filtered {
                        ea_header.raw_elmt_size - ctx.sizeof_addr - 4
                    } else {
                        0
                    };

                    // Read the EA index block. Filtered datasets
                    // store a `FilteredIndexBlock`; unfiltered ones a
                    // plain `ExtensibleArrayIndexBlock`. Both must be
                    // reconstructed so a reopened dataset can append
                    // (write_chunk consults whichever applies).
                    let ea_iblk_addr = ea_header.idx_blk_addr;
                    let (ea_iblk, filt_iblk) = if is_filtered {
                        let placeholder = ExtensibleArrayIndexBlock::new(
                            *index_address,
                            ep.idx_blk_elmts,
                            ndblk_addrs,
                            nsblk_addrs,
                        );
                        let fib = if ea_iblk_addr != UNDEF_ADDR {
                            let iblk_buf = handle.read_at_most(ea_iblk_addr, 65536)?;
                            FilteredIndexBlock::decode(
                                &iblk_buf,
                                ctx,
                                ep.idx_blk_elmts as usize,
                                ndblk_addrs,
                                nsblk_addrs,
                                chunk_size_len,
                            )?
                        } else {
                            FilteredIndexBlock::new(
                                *index_address,
                                ep.idx_blk_elmts,
                                ndblk_addrs,
                                nsblk_addrs,
                            )
                        };
                        (placeholder, Some(fib))
                    } else {
                        let eib = if ea_iblk_addr != UNDEF_ADDR {
                            let iblk_buf = handle.read_at_most(ea_iblk_addr, 65536)?;
                            ExtensibleArrayIndexBlock::decode(
                                &iblk_buf,
                                ctx,
                                ep.idx_blk_elmts as usize,
                                ndblk_addrs,
                                nsblk_addrs,
                            )?
                        } else {
                            ExtensibleArrayIndexBlock::new(
                                *index_address,
                                ep.idx_blk_elmts,
                                ndblk_addrs,
                                nsblk_addrs,
                            )
                        };
                        (eib, None)
                    };

                    info.chunked = Some(ChunkedDatasetInfo {
                        chunk_dims: real_chunk_dims,
                        earray_params: ep,
                        ea_header_addr: *index_address,
                        ea_iblk_addr,
                        ea_header,
                        ea_iblk,
                        chunks_written: 0,
                        filt_iblk,
                        chunk_size_len,
                    });
                }
            } else if *index_type
                == crate::format::messages::data_layout::ChunkIndexType::FixedArray
            {
                // Read the FA header and data block back so a
                // reopened dataset is writable and deletable, not
                // re-link only — a placeholder made a delete free
                // just the header and leak every chunk plus the
                // index. Paged data blocks (any FA with more than
                // dblk_page_nelmts chunks, libhdf5 default 1024)
                // reconstruct through the same decode owner; only
                // pages the bitmap marks initialized are decoded.
                let hdr_buf = handle.read_at_most(*index_address, 256)?;
                let fa_header = FixedArrayHeader::decode(&hdr_buf, ctx)?;
                let is_filtered = fa_header.client_id == FA_CLIENT_FILT_CHUNK;
                let chunk_size_len = if is_filtered {
                    (fa_header.element_size as usize)
                        .checked_sub(ctx.sizeof_addr as usize + 4)
                        .ok_or_else(|| {
                            crate::io::IoError::InvalidState(
                                "fixed array filtered element_size too small".into(),
                            )
                        })?
                } else {
                    0
                };
                if fa_header.data_blk_addr != UNDEF_ADDR && chunk_size_len <= 8 {
                    let dblk_size = fixed_array_dblk_disk_size(ctx, &fa_header) as usize;
                    let dblk_buf = handle.read_at_most(fa_header.data_blk_addr, dblk_size)?;
                    let fa_dblk =
                        decode_fixed_array_dblk(ctx, &fa_header, &dblk_buf, chunk_size_len)?;
                    info.fixed_array = Some(FixedArrayDatasetInfo {
                        chunk_dims: real_chunk_dims,
                        fa_header_addr: *index_address,
                        fa_dblk_addr: fa_header.data_blk_addr,
                        fa_header,
                        fa_dblk,
                        // Chunks written this session, matching the
                        // EA reconstruction above.
                        chunks_written: 0,
                    });
                }
            } else if *index_type == crate::format::messages::data_layout::ChunkIndexType::BTreeV2 {
                use crate::format::chunk_index::btree_v2::{
                    Bt2Geometry, Bt2Header, BT2_TYPE_CHUNK_FILT, BT2_TYPE_CHUNK_UNFILT,
                };

                // Walk the tree back into the in-memory index and
                // adopt its node blocks as the flush pool. The pool
                // re-serializes at the header's node_size, whatever
                // it is — libhdf5 sizes every node from
                // hdr->node_size (H5B2leaf.c, H5B2internal.c) — so
                // a foreign size reopens too. Only a record type
                // that is not a chunk record, or a node size below
                // the bulk loader's few-records-per-node floor
                // (the same bound creation enforces), stays
                // re-link only.
                let hdr_buf = handle.read_at_most(*index_address, 256)?;
                let bt2_hdr = Bt2Header::decode(&hdr_buf, ctx)?;
                let ndims = real_chunk_dims.len();
                let is_filt = match bt2_hdr.record_type {
                    BT2_TYPE_CHUNK_UNFILT => Some(false),
                    BT2_TYPE_CHUNK_FILT => Some(true),
                    _ => None,
                };
                if let (Some(is_filt), true) = (
                    is_filt,
                    bt2_hdr.node_size as usize >= 10 + 3 * bt2_hdr.record_size as usize,
                ) {
                    let mut index = if is_filt {
                        let csl = (bt2_hdr.record_size as usize)
                            .checked_sub(ctx.sizeof_addr as usize + 4 + ndims * 8)
                            .filter(|&c| c <= 8)
                            .ok_or_else(|| {
                                crate::io::IoError::InvalidState(
                                    "v2 B-tree filtered record size does not fit \
                                     its rank and address width"
                                        .into(),
                                )
                            })?;
                        Bt2ChunkIndex::new_filtered(ndims, csl as u8)
                    } else {
                        Bt2ChunkIndex::new_unfiltered(ndims)
                    };
                    // Re-serialize with the creator's parameters:
                    // node blocks keep their size and the rewritten
                    // header keeps its declared split/merge.
                    index.node_size = bt2_hdr.node_size;
                    index.split_percent = bt2_hdr.split_percent;
                    index.merge_percent = bt2_hdr.merge_percent;
                    let mut node_addrs = Vec::new();
                    if bt2_hdr.root_node_addr != UNDEF_ADDR && bt2_hdr.total_num_records > 0 {
                        let geo = Bt2Geometry::new(
                            bt2_hdr.node_size,
                            bt2_hdr.record_size,
                            bt2_hdr.depth,
                            ctx.sizeof_addr,
                        );
                        let mut walk =
                            Bt2Walk::new(handle, ctx, bt2_hdr.record_size, bt2_hdr.node_size, &geo);
                        walk.descend(
                            bt2_hdr.root_node_addr,
                            bt2_hdr.depth,
                            bt2_hdr.num_records_in_root,
                        )?;
                        node_addrs = walk.node_addrs;
                        let record_bytes = walk.records;
                        let total = if bt2_hdr.record_size > 0 {
                            record_bytes.len() / bt2_hdr.record_size as usize
                        } else {
                            0
                        };
                        if is_filt {
                            for r in Bt2ChunkIndex::decode_filtered_records(
                                &record_bytes,
                                total,
                                ndims,
                                bt2_hdr.record_size,
                                ctx,
                            )? {
                                index.insert_filtered(
                                    r.scaled_offsets,
                                    r.chunk_address,
                                    r.chunk_size,
                                    r.filter_mask,
                                );
                            }
                        } else {
                            for r in Bt2ChunkIndex::decode_unfiltered_records(
                                &record_bytes,
                                total,
                                ndims,
                                ctx,
                            )? {
                                index.insert(r.scaled_offsets, r.chunk_address);
                            }
                        }
                    }
                    info.btree_v2 = Some(Bt2DatasetInfo {
                        chunk_dims: real_chunk_dims,
                        bt2_header_addr: *index_address,
                        node_addrs,
                        index,
                        chunks_written: 0,
                    });
                }
            } else if *index_type == crate::format::messages::data_layout::ChunkIndexType::Implicit
            {
                // Nothing to read back: the index *is* the run of chunk space
                // at `index_address`, and its length is the chunk grid times
                // the chunk size. Reconstructing that length is what lets a
                // delete free the storage and a write address it — a rebuild
                // that left this empty would rewrite the dataset as an
                // unallocated contiguous one, dropping every byte.
                let mut nchunks: u64 = 1;
                for g in crate::io::chunk_grid::index_grid(
                    &info.dataspace.dims,
                    info.dataspace.max_dims.as_deref(),
                    &real_chunk_dims,
                )? {
                    nchunks = nchunks.checked_mul(g).ok_or_else(|| {
                        crate::io::IoError::InvalidState("chunk count overflows u64".into())
                    })?;
                }
                let data_size = nchunks
                    .checked_mul(chunk_dims.iter().product::<u64>())
                    .ok_or_else(|| {
                        crate::io::IoError::InvalidState(
                            "implicit chunk storage overflows u64".into(),
                        )
                    })?;
                info.implicit = Some(ImplicitDatasetInfo {
                    chunk_dims: real_chunk_dims,
                    data_addr: *index_address,
                    data_size,
                });
            } else if *index_type
                == crate::format::messages::data_layout::ChunkIndexType::SingleChunk
            {
                // No index structure to read back either: the one chunk's
                // address, and its stored size and filter mask if the
                // layout's filtered flag is set, are the whole of the
                // layout message. `chunk_dims` already includes the
                // trailing element-size dimension, so its product is the
                // chunk's unfiltered byte length directly (see `data_size`
                // in the Implicit arm above).
                let data_size = chunk_dims.iter().product::<u64>();
                let (nbytes, filter_mask) = match single_chunk_filter {
                    Some(scf) => (scf.nbytes, scf.filter_mask),
                    None => (data_size, 0),
                };
                info.single_chunk = Some(SingleChunkDatasetInfo {
                    chunk_dims: real_chunk_dims,
                    data_addr: *index_address,
                    data_size,
                    nbytes,
                    filter_mask,
                    chunks_written: 0,
                    // Whether this was created with early allocation isn't
                    // recoverable here: `fill_value` above is only the
                    // decoded fill bytes, not the fill-value message's
                    // `alloc_time` byte the layout was chosen under. A
                    // reopened dataset that later gets a header rewrite
                    // therefore reports incremental allocation regardless
                    // of how it was actually created — the same
                    // imprecision a reopened `fixed_array`/`btree_v2`
                    // dataset already has, for the same reason.
                    early_alloc: false,
                });
            }
        }
        // Unreachable by `layout_rebuilds`, which is the gate
        // `ReopenWalk::plan` consults before it ever calls this.
        _ => {}
    }

    Ok(info)
}

/// Write `data` at *dataset-relative* byte offset `skip` into an external file
/// list, walking slots by cumulative declared size exactly like
/// `H5D__efl_write` (H5Defl.c).
///
/// Each slot's file is opened create-if-missing and never truncated, so a
/// write touches only the byte range that slot owns. A write past the *total*
/// declared size of the list is an error, matching upstream's "write past
/// logical end of file" check.
fn write_external_file_bytes(
    files: &[ExternalFile],
    extfile_prefix: Option<&Path>,
    mut skip: u64,
    data: &[u8],
) -> IoResult<()> {
    // `H5D__efl_write`'s slot walk: an `H5O_EFL_UNLIMITED` slot matches every
    // remaining offset (`skip >= u64::MAX` is never true), so the search stops
    // there and the write below takes the whole rest of the data.
    let mut slot_idx = 0usize;
    while slot_idx < files.len() && skip >= files[slot_idx].size {
        skip -= files[slot_idx].size;
        slot_idx += 1;
    }

    let mut written = 0usize;
    while written < data.len() {
        let Some(slot) = files.get(slot_idx) else {
            return Err(crate::io::IoError::InvalidState(
                "write past the logical end of the external file list".into(),
            ));
        };
        let full_path = crate::io::reader::combine_prefixed_path(extfile_prefix, &slot.name);
        let ext_handle = FileHandle::open_or_create_readwrite_with_locking(
            &full_path,
            crate::io::locking::FileLocking::Disabled,
        )
        .map_err(|e| {
            crate::io::IoError::InvalidState(format!(
                "unable to open external raw data file {} for writing: {e}",
                full_path.display()
            ))
        })?;
        let this_write = (slot.size - skip).min((data.len() - written) as u64) as usize;
        let at = slot.offset.checked_add(skip).ok_or_else(|| {
            crate::io::IoError::InvalidState(format!(
                "external file '{}' slot offset {} overflows {skip} bytes into the slot",
                slot.name, slot.offset
            ))
        })?;
        ext_handle.write_at(at, &data[written..written + this_write])?;
        // This handle is dropped at the end of the iteration, and `Drop` can
        // only print a flush failure. Empty the accumulator here instead, so a
        // full disk on an external raw-data file reaches the caller.
        ext_handle.flush()?;

        written += this_write;
        skip = 0;
        slot_idx += 1;
    }
    Ok(())
}

/// The directory the HDF5 file at `path` sits in — libhdf5's `H5F_t::extpath`,
/// which `H5D__build_file_prefix` expands `${ORIGIN}` to.
///
/// Canonicalized, so the value survives the process changing directory and so
/// a writer and a reader of the same file agree on it. Called once per open,
/// never per I/O, for exactly that reason.
fn source_dir_of(path: &Path) -> IoResult<PathBuf> {
    let canonical = std::fs::canonicalize(path)?;
    Ok(canonical
        .parent()
        .map(Path::to_path_buf)
        .unwrap_or_default())
}

/// Whether [`rebuild_dataset`] has an arm that reconstructs this layout.
///
/// The single list: `ReopenWalk::plan` preserves an object whose layout this
/// says no to, so a layout added to one side and not the other cannot happen.
/// Keeping two lists is what would rewrite a modelled dataset as unallocated
/// contiguous storage, or preserve one the writer can now build.
fn layout_rebuilds(layout: &DataLayoutMessage) -> bool {
    matches!(
        layout,
        DataLayoutMessage::Contiguous { .. }
            | DataLayoutMessage::Compact { .. }
            | DataLayoutMessage::ChunkedV3 { .. }
            | DataLayoutMessage::ChunkedV4 { .. }
    )
}

/// Encode an Object Reference Count message (type 0x16) body: a version
/// byte (`H5O_REFCOUNT_VERSION` = 0) followed by the little-endian u32
/// count. Emitted on objects reached by more than one hard link.
fn encode_refcount(refcount: u32) -> Vec<u8> {
    let mut v = Vec::with_capacity(5);
    v.push(0u8);
    v.extend_from_slice(&refcount.to_le_bytes());
    v
}

/// The symbol-table storage of every group that has one, and the single owner
/// of which groups those are.
///
/// A group stores its links in a symbol table because the file was *made* that
/// way — `H5F_LIBVER_EARLIEST` is the one bound `H5G__obj_create_real`
/// (H5Gobj.c:179) writes them at — or because it already had one when the file
/// was reopened. The second is not the first: `H5G_obj_insert` inserts into
/// whatever storage the group is in and converts only when a link will not fit
/// an entry (H5Gobj.c:512), so a symbol table survives a reopen at any bound.
/// A file with shared messages is where the two come apart, because its
/// superblock extension forces a version-2 superblock over symbol-table groups
/// (H5Fsuper.c:1135) — a group the session adds is made as the session's
/// bound says while the groups already there stay symbol tables.
struct SymbolTables {
    /// The scopes the reopen found a Symbol Table message on. Fixed for the
    /// session: a group already in that storage stays in it, whatever bound
    /// the objects added beside it are written at.
    found: HashSet<LinkScope>,
    /// The symbol-table storage each group's header already names, by the
    /// scope whose rewrite supersedes it.
    ///
    /// INVARIANT: every entry is freed exactly once, by
    /// [`Hdf5Writer::prepare_symbol_tables`], which removes it as it frees.
    superseded: Slot<HashMap<LinkScope, StabExtents>>,
    /// The storage that same pass laid out, read by the header builders.
    ///
    /// INVARIANT: an entry exists here only after every block of that group's
    /// heap and B-tree is on disk. `build_group_header` reads it and never
    /// builds — a header is sized and then written by two separate calls, so a
    /// build that allocated would allocate twice.
    written: Slot<HashMap<LinkScope, Stab>>,
}

impl SymbolTables {
    /// What a file being created starts from: no group found in a symbol table
    /// because none was read, and nothing on disk to free.
    fn none_found() -> Self {
        Self {
            found: HashSet::new(),
            superseded: Slot::new(HashMap::new()),
            written: Slot::new(HashMap::new()),
        }
    }
}

/// Everything a version-0/1 (symbol-table) file carries that a version-2/3 one
/// does not.
///
/// Its presence *is* the generation switch — [`Hdf5Writer::message_format`]
/// reads nothing else: libhdf5 at `H5F_LIBVER_EARLIEST` writes a version-0/1
/// superblock over version-1 object headers over symbol-table groups. Which
/// groups are symbol tables is the separate question [`SymbolTables`] answers,
/// because a reopen at a newer bound keeps the ones it finds.
///
/// Two things put one here, and only two: reopening a file that already is in
/// that format, and creating one at that bound
/// ([`LegacyFile::created`]). Neither is distinguished afterwards — a file is
/// classic or it is not, and every encoder asks only that.
struct LegacyFile {
    /// The superblock as it was read, or as [`LegacyFile::created`] built it.
    /// The close re-emits it with only the end of file and the root symbol
    /// table entry recomputed: the "K" ranks in particular are recorded
    /// nowhere else, and every node width in the file is derived from them.
    superblock: SuperblockV0V1,
}

impl LegacyFile {
    /// The classic-format state a file created at `H5F_LIBVER_EARLIEST`
    /// starts from.
    ///
    /// A new file has no symbol table on disk to free and none laid out, so
    /// its [`SymbolTables`] starts empty and every group it makes takes that
    /// storage from the bound rather than from what was found.
    ///
    /// The superblock is the one `H5F__super_init` writes at that bound: the
    /// library-default "K" ranks (`H5F_CRT_SYM_LEAF_DEF`,
    /// `HDF5_BTREE_SNODE_IK_DEF`), no free-space info and no driver info. The
    /// root entry's object header address and cached symbol table are stamped
    /// in by [`Hdf5Writer::write_superblock`] once the root group has one;
    /// its name offset is the empty string at the front of every local heap.
    ///
    /// Version 0, not 1: a version-1 superblock exists only to carry a
    /// non-default chunked-storage "K" value (H5Fsuper.c:1150), and this
    /// writer has no property to set one.
    fn created(ctx: FormatContext, base_address: u64) -> Self {
        let btree = BTreeV1Config::default();
        Self {
            superblock: SuperblockV0V1 {
                version: SUPERBLOCK_V0,
                sizeof_offsets: ctx.sizeof_addr,
                sizeof_lengths: ctx.sizeof_size,
                file_consistency_flags: 0,
                sym_leaf_k: btree.sym_leaf_k,
                btree_internal_k: btree.snode_internal_k,
                indexed_storage_k: None,
                base_address,
                superblock_extension_address: UNDEF_ADDR,
                end_of_file_address: 0,
                driver_info_address: UNDEF_ADDR,
                root_symbol_table_entry: SymbolTableEntry {
                    name_offset: 0,
                    obj_header_addr: UNDEF_ADDR,
                    cache: SymbolTableCache::Nothing,
                },
            },
        }
    }
}

/// The superblock extension a reopen found, and the single owner of the one
/// this file's close writes back.
///
/// The extension is external truth: it is where a file records the things its
/// superblock has no field for — non-default v1 B-tree "K" ranks, a driver's
/// settings, the file space strategy and its persisted free-space managers,
/// and the shared object header message table. `H5F__super_ext_write_msg`
/// modifies one message of it and leaves the rest alone, so a close that lays
/// a fresh extension out from what *this writer* models drops everything it
/// does not — and the K ranks are not decoration: a chunked dataset's version-1
/// B-tree nodes are sized from `chunk_internal_k`, so a reader that has lost
/// the message reads the tree at the default rank and fails outright.
///
/// INVARIANT: every message of the extension read is re-emitted by
/// [`Hdf5Writer::write_superblock_extension`], byte for byte, except the
/// shared-message table — the one message naming storage this session lays out
/// afresh, which [`SohmState`] recomputes. Nothing else here is interpreted,
/// so a message this crate does not model survives exactly as a modelled one
/// does.
struct CarriedExtension {
    /// Every block the extension header occupied — chunk 0 and each
    /// continuation it named — freed once the replacement is laid out. Empty
    /// for a file with no extension, and for one whose extension this session
    /// is the first to write. A rewrite re-encodes the whole chain into one
    /// chunk, so freeing only the first would leave the rest as space no
    /// free-space manager records and no object claims.
    superseded: crate::io::object_header_io::HeaderBlocks,
    /// Every message that header held — the shared-message table,
    /// continuations and null padding excepted. The first two are structure
    /// rather than content; the third is free space.
    carried: Vec<crate::io::object_header_io::ExtensionMessage>,
    /// Where [`Hdf5Writer::write_superblock_extension`] put the replacement,
    /// and the only value the superblock's extension address is read from.
    /// `None` until that pass runs, and for a file that needs no extension.
    addr: Slot<Option<u64>>,
}

/// What a reopen learns from a file's free-space managers, split by who owns
/// it: the sections go to the allocator and the rest stays with the writer.
struct ReopenedFreeSpace {
    /// `None` for a file this writer records no free space for.
    state: Option<Box<FileSpaceState>>,
    /// Every section the managers held, each tagged with the manager it came
    /// out of and merged only within it, address-ordered. Empty whenever
    /// `state` is `None`.
    sections: Vec<FreeBlock>,
}

/// The file-space info message this session is responsible for, and the
/// manager blocks it supersedes.
///
/// A file whose message says `persist` records the space its own edits
/// released in one free-space manager per allocation type: a header block
/// (`FSHD`) naming a sections block (`FSSE`) that lists every free region.
/// Nothing else in the file says those regions are free, so a session that
/// rewrites the file without reading them either leaks the space it frees or
/// hands out space a manager still claims.
///
/// Present for a file this writer *created* with non-default file-space
/// properties as well, where there is nothing to read and the message is this
/// session's to write. `None` — the field, not this struct — is the third
/// case: a reopened file whose message this session must not touch, which the
/// carried extension re-emits byte for byte.
///
/// INVARIANT: the sections read are handed to [`FileAllocator`] and tracked
/// there alone, so there is one account of the file's free space and not two.
/// What stays here is only what the allocator has no place for: the message to
/// write, and the managers' own blocks, which are not free space until the
/// close that replaces them frees them.
struct FileSpaceState {
    /// The message, as read or as the creation options declared it. It is the
    /// only place the manager addresses are recorded, so the close that moves
    /// them rewrites this message.
    info: FileSpaceInfoMessage,
    /// The manager blocks themselves — one header, and one sections block per
    /// manager that had any sections. Freed by the close that lays their
    /// replacements out, the rule every other superseded structure follows.
    /// Empty for a created file, which supersedes nothing.
    superseded: Vec<(u64, u64)>,
}

impl FileSpaceState {
    /// Whether this file keeps free-space managers on disk. Both strategies
    /// that have managers do — paged aggregation has the same managers plus a
    /// large one — while the two aggregator-only strategies and
    /// `persist: false` still carry the message with nothing to write into it.
    fn records_free_space(&self) -> bool {
        self.info.persist
            && matches!(
                self.info.strategy,
                FileSpaceStrategy::FsmAggr | FileSpaceStrategy::Page
            )
    }
}

/// One free-space manager that has been given its own two blocks, and the
/// sections it will write into them.
///
/// Produced by
/// [`settle_free_space_managers`](Hdf5Writer::settle_free_space_managers).
/// Both blocks are ordinary allocations out of the same [`FileAllocator`] the
/// rest of the file uses, because upstream's are too:
/// `H5FS_vfd_alloc_hdr_and_section_info_if_needed` calls `H5MF_alloc`
/// (H5FSsection.c:2352, 2406).
struct PlacedManager {
    /// Which of the file's managers this is; its message slot names it in the
    /// file-space info message.
    manager: FreeSpaceManager,
    /// Header block address.
    hdr_addr: u64,
    /// Sections block address.
    sect_addr: u64,
    /// Bytes the sections block occupies. What the header records as both
    /// `sect_size` and `alloc_sect_size`, so an image shorter than the block
    /// is padded rather than reported short.
    sect_size: u64,
    /// The sections this manager records, in serialization order. Filled on
    /// the settling round, once no allocation can change them.
    sections: Vec<FreeSection>,
}

/// The manager header for `sections`, before its own blocks have addresses.
///
/// Every width the section encoding uses comes from here, and the only one
/// that varies with the content is `serial_sections` — it decides how many
/// bytes a per-size run count takes — so sizing a layout and encoding it must
/// go through this one function or the two disagree.
fn manager_header(sections: &[FreeSection]) -> FreeSpaceHeader {
    FreeSpaceHeader {
        client: free_space::CLIENT_FILE,
        total_space: sections.iter().map(|s| s.len).sum(),
        total_sections: sections.len() as u64,
        // Every class the file client registers is serializable; only a
        // fractal heap's manager has ghost sections.
        serial_sections: sections.len() as u64,
        ghost_sections: 0,
        nclasses: free_space::FILE_SECT_CLASSES,
        shrink_percent: free_space::SHRINK_PERCENT,
        expand_percent: free_space::EXPAND_PERCENT,
        max_sect_addr: free_space::SEC2_MAX_SECT_ADDR,
        max_sect_size: free_space::SEC2_MAXADDR,
        sect_addr: UNDEF_ADDR,
        sect_size: 0,
        alloc_sect_size: 0,
    }
}

impl Default for CarriedExtension {
    /// What a file with no extension carries: nothing to free, nothing to
    /// re-emit, and no address until a shared-message table gives it one.
    fn default() -> Self {
        Self {
            superseded: Vec::new(),
            carried: Vec::new(),
            addr: Slot::new(None),
        }
    }
}

/// Where a file's superblock version comes from — the two cases libhdf5 keeps
/// strictly apart, and this writer's single source for the version it writes
/// back.
///
/// INVARIANT: reopening a file never changes its superblock version.
///
/// libhdf5 splits the same way. `H5F__super_init` is the only place a version
/// is *decided* — content first, then `MAX(super_vers,
/// HDF5_superblock_ver_bounds[low_bound])` (H5Fsuper.c:1128-1154).
/// `H5F__super_read` never recomputes one. Nor does it bound anything by it:
/// the structures a session appends are written at the bound the caller
/// named, or the writer's default, whatever version the superblock has.
/// libhdf5 1.14 raised a reopened file's low bound to the row its superblock
/// version belongs to; libhdf5 2.0 dropped that (HDFGroup/hdf5#4939), and the
/// one raise left is SWMR write access, to `H5F_LIBVER_V110`
/// (H5Fsuper.c:453), which [`reject_swmr`](Hdf5Writer::reject_swmr) asks of
/// the caller instead. One direction only: the bound decides a created
/// file's version, the version never decides the bound.
///
/// Two variants rather than one number with a rule attached, because the
/// number means different things on the two paths — a floor to raise on the
/// create path, a fixed value on the reopen path — and a single field would
/// have every reader re-derive which.
#[derive(Debug, Clone, Copy)]
enum SuperblockVersion {
    /// A file this writer created. The version its creation options start
    /// from, which [`superblock_version_for`](Hdf5Writer::superblock_version_for)
    /// raises to what the content and the named bound need.
    Chosen(u8),
    /// A file this writer reopened: the version already in the file, written
    /// back unchanged. `Existing(0..=1)` and `Hdf5Writer::legacy` say the same
    /// thing from two directions and cannot disagree: `open_append_with_locking`
    /// builds the `LegacyFile` from exactly those versions.
    Existing(u8),
}

/// A registry entry that has held some name.
///
/// Datasets, groups and committed datatypes keep stable indices — their
/// registries only grow, deletion being a flag — so the index can name the
/// exact entry. The link registries shrink as links are unlinked, and a
/// link's path is derived from its parent group's current name, so for those
/// the index records only that the kind once claimed the name and the (short)
/// list itself answers.
#[derive(Clone, Copy, PartialEq, Eq)]
enum NameHit {
    Dataset(usize),
    Group(usize),
    Datatype(usize),
    HardLink,
    SymbolicLink,
    PreservedLink,
}

/// Which names the file model already holds, so creating an object does not
/// have to walk every registry to find out.
///
/// INVARIANT: while `map` is `Some`, every name a registry entry currently
/// holds has an entry in `map` covering that entry. The converse is not
/// required: a hit whose object was since deleted, or whose name has since
/// changed, stays in the map and is filtered out by
/// [`Hdf5Writer::name_holder`], which re-runs the very predicates the linear
/// scan used. The index may therefore answer "maybe", never "free" for a name
/// that is taken.
///
/// MUST NOT: no code may give a registry entry a name, or move the path a
/// link is emitted under, without either registering the new name through
/// [`Hdf5Writer::register_name`] or dropping the index through
/// [`Hdf5Writer::forget_name_index`]. State a constructor puts straight into
/// the registries needs neither — `map` starts `None`, and the first query
/// builds it from the registries as they then stand.
struct NameIndex {
    map: Option<HashMap<String, Vec<NameHit>>>,
    /// Bumped whenever the registries move under a build in flight, so that
    /// build's result is discarded instead of being installed stale.
    epoch: u64,
}

impl NameIndex {
    fn new() -> Self {
        NameIndex {
            map: None,
            epoch: 0,
        }
    }

    /// Record that `hit` holds `name`. With no map built there is nothing to
    /// record, but the registries have moved, so any build in flight is
    /// invalidated rather than trusted.
    fn insert(&mut self, name: &str, hit: NameHit) {
        match self.map.as_mut() {
            None => self.epoch += 1,
            Some(map) => {
                let hits = map.entry(name.to_string()).or_default();
                if !hits.contains(&hit) {
                    hits.push(hit);
                }
            }
        }
    }

    /// Throw the index away: the next query rebuilds it from the registries.
    fn forget(&mut self) {
        self.map = None;
        self.epoch += 1;
    }
}

/// HDF5 file writer.
///
/// Usage:
/// 1. `Hdf5Writer::create(path)` to create a new file.
/// 2. `create_dataset(name, datatype, dims)` to define datasets.
/// 3. `write_dataset_raw(index, data)` to write raw data.
/// 4. `close()` to finalize the file (writes superblock, headers, etc.).
pub struct Hdf5Writer {
    handle: FileHandle,
    allocator: FileAllocator,
    ctx: FormatContext,
    /// Dataset registry. The outer [`Slot`] guards the spine (push on create,
    /// index/clone on access) and is held only briefly; each [`DatasetRef`]
    /// carries one dataset's metadata behind its own lock. A writer clones
    /// the `DatasetRef` out (releasing this lock) before doing the long
    /// per-dataset work, so a create never blocks an in-flight write.
    pub(crate) datasets: Slot<Vec<DatasetRef>>,
    /// Group registry, same shape as [`Self::datasets`].
    pub(crate) groups: Slot<Vec<GroupRef>>,
    /// User-created hard links (additional names for existing objects),
    /// resolved and emitted during finalize.
    pub(crate) hard_links: Slot<Vec<HardLink>>,
    /// User-created soft and external links. Held apart from
    /// [`Self::hard_links`] because they name a path rather than an object:
    /// nothing resolves them, and no object's reference count counts them.
    pub(crate) symbolic_links: Slot<Vec<SymbolicLink>>,
    /// Datatypes committed this session, each an object of its own; see
    /// [`CommittedDatatype`].
    pub(crate) committed_datatypes: Slot<Vec<CommittedDatatype>>,
    /// Links a reopened file held that this writer cannot express, carried
    /// through every header rewrite by their encoded bytes. Always empty for
    /// a freshly created file; see [`PreservedLink`].
    pub(crate) preserved_links: Slot<Vec<PreservedLink>>,
    /// Which names the registries above already hold; see [`NameIndex`].
    /// Boxed so this side table costs the writer one pointer: inline, its
    /// map shifted every field after it and cost the attribute path ~5%.
    name_index: Slot<Box<NameIndex>>,
    /// Attributes attached to the root group (file-level attributes).
    pub(crate) root_attributes: Slot<Vec<crate::format::messages::attribute::AttributeEntry>>,
    /// Serializes object creation so name-uniqueness check and registry insert
    /// happen atomically.
    ///
    /// INVARIANT: no two emitted links share a full-path name. Under
    /// `threadsafe`, create methods run on the shared read guard, so without
    /// this gate two threads could both pass the duplicate-name check (which
    /// snapshots a registry and drops its lock) and both push, writing an
    /// invalid HDF5 file with two same-named links. A create holds this lock
    /// across its check *and* its push; the streaming write path never takes
    /// it, so writes to existing datasets stay fully concurrent. It is the
    /// outermost lock a create acquires (create_lock → spine → slot), and no
    /// write path takes it, so it cannot deadlock with the registry locks.
    pub(crate) create_lock: Slot<()>,
    /// The low `H5Pset_libver_bounds` bound the *caller named*, or `None`
    /// when none was: the oldest libhdf5 the objects this writer creates must
    /// stay readable by. It is the one switch the version-bearing messages
    /// read — the datatype message version (`H5O_dtype_ver_bounds`), the data
    /// layout message version (`H5O_layout_ver_bounds`) and with it the chunk
    /// index, and the superblock floor (`HDF5_superblock_ver_bounds`) of a
    /// file this writer creates.
    ///
    /// `None` is not `Some(Earliest)`. No single libhdf5 bound describes this
    /// crate's default file: it takes the earliest row of the datatype and
    /// superblock tables (version-1 datatypes, a version-2 superblock raised
    /// to 3 only by what the content needs) over the v1.10 chunk indexes,
    /// which is the `H5F_LIBVER_V110` row of the layout table. Naming a bound
    /// asks for one whole libhdf5 generation instead, so the two cannot share
    /// a field.
    ///
    /// Nothing reads this directly:
    /// [`session_libver`](Hdf5Writer::session_libver) is the only reader, and
    /// it is where `None` becomes the default of the family asking, the same
    /// on a created file and a reopened one: the superblock a reopened file
    /// already has says nothing about the bound (see [`SuperblockVersion`]).
    libver: Option<LibverBound>,
    closed: bool,
    /// Set once `finalize_for_swmr` has published a readable file.
    ///
    /// A SWMR reader may hold a chunk index that still points at a block this
    /// writer has since replaced, so from that point on a relocated chunk's
    /// old block is kept rather than released for reuse — the same rule as
    /// libhdf5's `H5D__chunk_file_alloc`, which skips `H5MF_xfree` under
    /// `H5F_ACC_SWMR_WRITE`.
    swmr_active: bool,
    /// Collections with free space — libhdf5's `f->shared->cwfs` list. A
    /// vlen insert fills these partially-filled collection blocks before
    /// creating a new one, so many small writes share 4096-byte blocks
    /// instead of each taking their own. Entries hold `(addr, block size,
    /// free bytes)` hints; the block on disk stays the single truth for
    /// contents, and only the two functions that rewrite collection blocks
    /// ([`insert_vlen_objects`](Self::insert_vlen_objects) and
    /// [`release_vlen_references`](Self::release_vlen_references)) may
    /// update this list. In-memory only, like the allocator's free list:
    /// a reopened file's free space is rediscovered as releases touch its
    /// collections. Capped at [`H5HG_NCWFS`] entries.
    cwfs: Slot<Vec<CwfsEntry>>,
    /// Address of the root group object header (set after first finalize).
    root_group_addr: Option<u64>,
    /// Size of the encoded root group object header (for in-place rewrites).
    /// The on-disk root header block a reopen found, `(addr, len)`, so
    /// finalize can free the block its rewrite supersedes.
    superseded_root_header: crate::io::object_header_io::HeaderBlocks,
    /// Where this file's superblock version comes from. The single owner of
    /// the reopen invariant — see [`SuperblockVersion`] and
    /// [`superblock_version_for`](Self::superblock_version_for).
    superblock_version: SuperblockVersion,
    /// Objects whose attributes this finalize spilled to dense storage, and
    /// the `Attribute Info` message naming what was written for each.
    ///
    /// INVARIANT: an entry exists here only after every block of that
    /// object's heap and name index is on disk, and only
    /// [`prepare_dense_attributes`](Self::prepare_dense_attributes) may add
    /// one. `emit_attributes` reads it and never builds — a header is sized
    /// and then written by two separate `build_*_header` calls, so a build
    /// that allocated would allocate twice and leave the sized-for blocks
    /// stranded.
    dense_attributes: Slot<HashMap<AttrScope, AttributeInfoMessage>>,
    /// Groups whose links this finalize spilled to dense storage, and the
    /// `Link Info` message naming what was written for each.
    ///
    /// INVARIANT: an entry exists here only after every block of that group's
    /// heap and name index is on disk, and only
    /// [`prepare_dense_links`](Self::prepare_dense_links) may add one.
    dense_links: Slot<HashMap<LinkScope, LinkInfoMessage>>,
    /// The dense storage the reopened object headers already name — the heaps
    /// and indices this session's rewrites and deletes supersede.
    ///
    /// `None` for a file this session created: every block such a file will
    /// hold was allocated here, so there is nothing on disk to supersede and
    /// nothing to allocate for the bookkeeping either.
    ///
    /// INVARIANT: every entry is freed exactly once, by
    /// [`release_superseded_dense_attrs`](Self::release_superseded_dense_attrs)
    /// or [`release_superseded_dense_links`](Self::release_superseded_dense_links),
    /// which remove it as they free. Nothing else may remove one: an entry
    /// that leaves without reaching the allocator is a leaked heap, and one
    /// that reaches it twice hands the same blocks to two objects.
    superseded_dense: Slot<Option<Box<SupersededDense>>>,
    /// The creation-order policy in force: whether an object created from
    /// now on records creation order for its links and its attributes. The
    /// h5py `track_order` analogue; see
    /// [`set_track_order`](Self::set_track_order). Each object captures this
    /// at creation, so changing it never rewrites an object already made.
    track_order: TrackOrder,
    /// Whether an object created from now on records the times its header can
    /// hold — `H5Pset_obj_track_times`, whose default is on
    /// (`H5O_CRT_OHDR_FLAGS_DEF` is `H5O_HDR_STORE_TIMES`, H5Opkg.h:74).
    /// Captured by each object at creation for the same reason
    /// [`track_order`](Self::track_order) is: it belongs to the creation
    /// property list, so a later change must not rewrite an object already
    /// made.
    track_times: bool,
    /// The root group's own captured policy. The root is created with the
    /// file, so its value comes from
    /// [`create_with_options`](Self::create_with_options) — or, on reopen,
    /// from the header already on disk.
    root_track_order: TrackOrder,
    /// The root group's stored times, on the same terms as
    /// [`GroupInfo::times`]: whatever a reopened file's root header had, and
    /// `None` for a file this writer created.
    root_times: Option<ObjectTimes>,
    /// Hands out the creation sequence numbers that order a group's links.
    next_creation_seq: Slot<u64>,
    /// Object-reference elements waiting for their target's object header
    /// address, which only exists once finalize has placed every header.
    pending_object_references: Slot<Vec<PendingObjectReference>>,
    /// Heap-backed reference objects waiting for the same address — the
    /// pre-1.12 region form and every 1.12 form whose element is a blob id.
    pending_heap_references: Slot<Vec<PendingHeapReference>>,
    /// What each object-reference attribute's value *means*, so
    /// [`object_attributes`](Hdf5Writer::object_attributes) can say it in
    /// addresses every time an object header is built.
    attribute_references: Slot<Vec<AttributeReferenceValue>>,
    /// Set when this file is in the classic (version-0/1 superblock) format,
    /// whether it was reopened in it or created at `H5F_LIBVER_EARLIEST`.
    /// See [`LegacyFile`]; [`is_legacy`](Self::is_legacy) is the only reader
    /// of whether it is there.
    legacy: Option<Box<LegacyFile>>,
    /// Which groups keep their links in a symbol table, and the storage each
    /// of them has. Empty for a file whose groups all store links in messages;
    /// see [`SymbolTables`], which owns the question.
    symbol_tables: SymbolTables,
    /// The v1 B-tree "K" ranks every node width in this file is derived from,
    /// after the superblock extension has had its say. A property of the file
    /// rather than of its generation: a version-2 superblock records no ranks
    /// of its own but its extension may, and a rewrite that used the library
    /// defaults there would write nodes of the wrong width.
    /// [`btree_v1_config`](Hdf5Writer::btree_v1_config) is the only reader.
    btree: BTreeV1Config,
    /// The superblock extension this file carries, and where the replacement
    /// went; see [`CarriedExtension`].
    extension: Box<CarriedExtension>,
    /// The free-space managers a reopened `persist: true` file carries; see
    /// [`FileSpaceState`]. `None` for every other file — one with no
    /// file-space info message, one that does not persist, one under paged
    /// aggregation, and every file this session created — and those files get
    /// no free-space manager written either.
    free_space: Option<Box<FileSpaceState>>,
    /// The file's shared-message indexes, when it was created with any.
    /// `None` — the default — is a file with no shared-message table, where
    /// [`share_message`](Self::share_message) is the identity.
    sohm: Option<Box<SohmState>>,
    /// The directory holding this HDF5 file, resolved once when it was opened
    /// — libhdf5's `H5F_t::extpath`, and the same value the read side keeps.
    /// External raw-data file names are joined against it when
    /// `HDF5_EXTFILE_PREFIX` names `${ORIGIN}`, so a write and a later read of
    /// the same dataset must resolve a relative name identically; capturing it
    /// at open time rather than reading the process's current directory per
    /// write is what makes that hold.
    source_dir: PathBuf,
}

/// A file's shared object header messages, from creation to the table on disk.
///
/// INVARIANT: a message body reaches the file either literally or as a pointer
/// to exactly one heap object, never both, and the reference count of that
/// object is the number of headers that hold the pointer.
/// [`share_message`](Hdf5Writer::share_message) is the only place a body is
/// offered to an index, and
/// [`prepare_shared_messages`](Hdf5Writer::prepare_shared_messages) is the
/// only place the phase changes — so counting and substituting are two passes
/// over the same call site rather than two pieces of logic that must agree.
struct SohmState {
    /// The indexes the file was created with, in table order.
    indexes: Vec<SohmIndexSpec>,
    /// What `share_message` does to an eligible message right now.
    phase: Slot<SohmPhase>,
    /// Address of the master table this session laid out, once it has one.
    /// Also the once-only latch on the layout: a second finalize keeps the
    /// table the first one published, and
    /// [`Hdf5Writer::write_superblock_extension`] reads it to name that table
    /// in the extension.
    table_addr: Slot<Option<u64>>,
    /// The blocks the table a reopen found occupies — the master table and
    /// each index's heap and index structure — taken by the finalize that
    /// replaces them. Empty for a file this session created.
    ///
    /// The table is laid out whole from the whole message set, so a reopen
    /// replaces it rather than inserting into it, and every header holding a
    /// pointer into the old one is rewritten in the same finalize
    /// ([`Hdf5Writer::rebuilds_shared_messages`]).
    superseded: Slot<Vec<(u64, u64)>>,
}

/// The passes `share_message` runs in, and the state between them.
enum SohmPhase {
    /// Outside a finalize: every message stays literal.
    Idle,
    /// Measuring headers, before the bodies they will hold are final. A
    /// shareable message answers at the width of a heap pointer over a heap
    /// object that does not exist yet, which is the width the one it ends up
    /// pointing at has: a `H5O_shared_t` in heap form is the same size
    /// whatever it names. Nothing this pass produces is written — it exists so
    /// [`allocate_object_headers`](Hdf5Writer::allocate_object_headers) can
    /// reserve a block for a header whose messages are shared before the
    /// content phase has decided which heap object each one shares.
    ///
    /// The set is [`FirstCopies`], and it is why this pass has state at all:
    /// a message left literal is *wider* than a pointer, so a header can only
    /// be measured by making the same first-copy decision the substituting
    /// pass will make.
    Predict(FirstCopies),
    /// Counting the bodies the file will share. Messages still go in
    /// literally, so nothing this pass builds is written.
    Collect(SohmCollector),
    /// Substituting. A body the collect pass never saw stays literal, which
    /// is a valid file: the record it would have shared simply keeps a
    /// reference count one higher than the pointers that reach it.
    Resolve {
        /// Heap ID per body, from the table this finalize laid out.
        ids: HashMap<(u8, Vec<u8>), [u8; SOHM_HEAP_ID_LEN]>,
        /// The first copies this pass has already handed out; see
        /// [`FirstCopies`].
        first: FirstCopies,
    },
}

/// The bodies a pass has already left literal in the header that offered them
/// first (`H5SM_IN_OH`, H5SM.c:1400-1417).
///
/// INVARIANT: the three passes walk the same object headers in the same order
/// — [`allocate_object_headers`](Hdf5Writer::allocate_object_headers),
/// [`prepare_shared_messages`](Hdf5Writer::prepare_shared_messages) and
/// [`write_object_headers`](Hdf5Writer::write_object_headers) each build every
/// dataset in `datasets` order, then every group, then the root — so "the
/// header that offered this body first" is the same header in all three. Each
/// pass keeps its own set rather than sharing one, so a pass that does not run
/// cannot leave a stale decision behind for the next one. A divergence would
/// make a header wider than the block reserved for it, which
/// [`check_header_size`] refuses rather than writing.
type FirstCopies = std::collections::HashSet<(u8, Vec<u8>)>;

/// The object header a message is being written into — `H5SM_try_share`'s
/// `open_oh` argument, which is what decides whether a first copy has a header
/// to stay literal in at all.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ShareOwner {
    /// `H5SM_try_share(f, NULL, ...)`: the message belongs to no object header
    /// of its own. An attribute's datatype and dataspace are offered this way
    /// (H5Aint.c:375-377) — they live inside the attribute's body, so there is
    /// no header message for a record to name and the body goes to the heap on
    /// first use however shareable its class is.
    Detached,
    /// `H5SM_try_share(f, oh, ...)`: the message is a message of the object
    /// header at this address (`H5O__msg_alloc`, H5Omessage.c:1735).
    Header(u64),
}

impl SohmState {
    /// A file's indexes, plus the blocks of the table they were read out of
    /// when the file was reopened (empty when it was created this session).
    fn new(indexes: Vec<SohmIndexSpec>, superseded: Vec<(u64, u64)>) -> Self {
        Self {
            indexes,
            phase: Slot::new(SohmPhase::Idle),
            table_addr: Slot::new(None),
            superseded: Slot::new(superseded),
        }
    }

    /// The index that would take a `msg_type` message of `body_len` bytes,
    /// as `H5SM_try_share` resolves one: the first index whose type mask
    /// covers the class, and then only if the message reaches that index's
    /// minimum. A message too small for its index is not offered to another —
    /// `H5SM__get_index` picks by type alone and the size check comes after.
    fn index_for(&self, msg_type: u8, body_len: usize) -> Option<usize> {
        let flag = type_flag(msg_type)?;
        let (at, spec) = self
            .indexes
            .iter()
            .enumerate()
            .find(|(_, spec)| spec.mesg_types & flag != 0)?;
        (body_len as u64 >= u64::from(spec.min_mesg_size)).then_some(at)
    }

    /// Whether any index takes attribute messages, which is what makes the
    /// file record message creation indices — `H5SM_init` sets
    /// `store_msg_crt_idx` on exactly this condition (H5SM.c:220).
    fn shares_attributes(&self) -> bool {
        let Some(flag) = type_flag(MSG_ATTRIBUTE) else {
            return false;
        };
        self.indexes.iter().any(|spec| spec.mesg_types & flag != 0)
    }
}

/// What decides whether two offers are the same shared message: the class,
/// the bytes, and the messages the bytes will end up pointing at.
type CollectedKey = (u8, Vec<u8>, Vec<NestedShare>);

/// The shareable message bodies of one collect pass, in first-seen order.
struct SohmCollector {
    /// Per index, its bodies with the number of headers holding each.
    messages: Vec<Vec<SharedMessage>>,
    /// Where a body sits: `(index, position in that index's messages)`, keyed
    /// by everything that decides what will be stored — the class, the bytes,
    /// and the messages the bytes will end up pointing at.
    seen: HashMap<CollectedKey, (usize, usize)>,
}

impl SohmCollector {
    fn new(nindexes: usize) -> Self {
        Self {
            messages: vec![Vec::new(); nindexes],
            seen: HashMap::new(),
        }
    }

    /// Count one message against `index`, adding the body the first time it
    /// is seen, and say whether that body is new.
    ///
    /// `ohdr` is the header this offer would leave the body literal in when it
    /// is the first — `None` when the class cannot be shared in an object
    /// header or the offer names none. It is recorded only for a first copy:
    /// once a body is in the heap, later offers of it are pointers whatever
    /// header they come from.
    ///
    /// Two bodies are the same message only if their nesting agrees as well:
    /// the heap IDs a nesting body will hold are still zero here, so two
    /// attributes that differ only in their datatype are the same bytes at
    /// this point and different bytes on disk.
    fn record(
        &mut self,
        index: usize,
        msg_type: u8,
        body: &[u8],
        nested: &[NestedShare],
        ohdr: Option<u64>,
    ) -> bool {
        let key = (msg_type, body.to_vec(), nested.to_vec());
        match self.seen.get(&key) {
            Some(&(at, pos)) => {
                self.messages[at][pos].ref_count += 1;
                false
            }
            None => {
                let pos = self.messages[index].len();
                self.messages[index].push(SharedMessage {
                    msg_type,
                    body: body.to_vec(),
                    nested: nested.to_vec(),
                    ref_count: 1,
                    ohdr_addr: ohdr,
                });
                self.seen.insert(key, (index, pos));
                true
            }
        }
    }

    /// Give back the reference [`record`](Self::record) took for a body whose
    /// container turned out to be a copy of one already here.
    ///
    /// A body reached only through a shared container is referenced once per
    /// container *record*, not once per object that has one: the pointer to
    /// it lives in the container's heap object, which exists once however
    /// many headers name it. `H5O__attr_create` reaches the same count from
    /// the other side, by building each attribute's components shared and
    /// then calling `H5O__attr_delete` — which decrements exactly the
    /// datatype and dataspace (H5Oattr.c:568-585) — whenever the attribute it
    /// built was not the first copy (H5Oattribute.c:331-366).
    fn release(&mut self, msg_type: u8, body: &[u8]) {
        if let Some(&(at, pos)) = self.seen.get(&(msg_type, body.to_vec(), Vec::new())) {
            let count = &mut self.messages[at][pos].ref_count;
            *count = count.saturating_sub(1);
        }
    }
}

/// The file-creation properties a brand-new file is made with.
///
/// libhdf5 splits these across the file creation and file access property
/// lists (`H5Pset_userblock`, `H5Pset_link_creation_order`,
/// `H5Pset_libver_bounds`, the locking property); what they have in common is
/// that they are read once, when the file is created, and cannot be changed
/// afterwards without rewriting it. Options that *can* change mid-session —
/// the bound for objects created later, the creation-order policy for later
/// objects — have their own setters.
#[derive(Debug, Clone, Copy, Default)]
pub struct FileCreateOptions {
    /// OS-level locking policy for the new file.
    pub locking: crate::io::locking::FileLocking,
    /// Creation-order policy for the root group, and the default for every
    /// object created afterwards; see [`Hdf5Writer::set_track_order`].
    pub track_order: bool,
    /// Time-tracking policy for the root group, and the default for every
    /// object created afterwards; see [`Hdf5Writer::set_track_times`].
    pub track_times: bool,
    /// The file's low library-version bound (`H5Pset_libver_bounds`'s `low`),
    /// or `None` when the caller named none.
    ///
    /// The distinction is not decoration. `Some(LibverBound::Earliest)` is a
    /// request for the format libhdf5 writes at `H5F_LIBVER_EARLIEST` — a
    /// version-0 superblock over symbol-table groups and version-1 object
    /// headers, which is what [`ObjectFormat::Legacy`] encodes. `None` keeps
    /// what this crate has always written for a file whose creator said
    /// nothing: the version-2 superblock and link-message groups of the v1.8
    /// format, with the earliest bound's message versions where they can
    /// express the content. That combination is this crate's own, not one
    /// libhdf5 writes, so it cannot be spelled as a bound.
    pub libver: Option<LibverBound>,
    /// Bytes reserved in front of the superblock for the application's own
    /// use (`H5Pset_userblock`). Zero, the default, places the superblock at
    /// offset 0; otherwise a power of two of at least
    /// [`MIN_USERBLOCK`] bytes, since a reader finds the
    /// superblock by doubling its search offset from there.
    pub userblock: u64,
    /// Shared object header message indexes; see [`SharedMessageConfig`].
    pub shared_messages: SharedMessageConfig,
    /// How the file manages its own space; see [`FileSpaceConfig`].
    pub file_space: FileSpaceConfig,
}

/// The file-space handling properties a new file is created with — the three
/// arguments of `H5Pset_file_space_strategy` and the one of
/// `H5Pset_file_space_page_size`.
///
/// The four together are what `H5F__super_init` compares against the library
/// defaults to decide whether the file needs a file-space info message at all
/// (H5Fsuper.c:1092-1097), which is why the page size belongs here even though
/// only paged aggregation allocates by it: a file that names a page size and
/// nothing else still carries the message.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileSpaceConfig {
    /// `H5F_fspace_strategy_t`.
    pub strategy: FileSpaceStrategy,
    /// Whether the free-space managers are written to the file on close.
    pub persist: bool,
    /// The smallest section a manager records; a block freed below it is
    /// space the file leaks rather than tracks.
    pub threshold: u64,
    /// `H5Pset_file_space_page_size`: the file-space page every allocation of
    /// a paged file is shaped by, and the value the message carries whatever
    /// the strategy.
    pub page_size: u64,
}

impl Default for FileSpaceConfig {
    /// `H5F_FILE_SPACE_STRATEGY_DEF`, `H5F_FREE_SPACE_PERSIST_DEF`,
    /// `H5F_FREE_SPACE_THRESHOLD_DEF` and `H5F_FILE_SPACE_PAGE_SIZE_DEF`
    /// (H5Fprivate.h:326-336).
    fn default() -> Self {
        Self {
            strategy: FileSpaceStrategy::FsmAggr,
            persist: false,
            threshold: 1,
            page_size: DEFAULT_FILE_SPACE_PAGE_SIZE,
        }
    }
}

impl FileSpaceConfig {
    /// The properties as `H5P__set_file_space_strategy` (H5Pfcpl.c:1176)
    /// stores them: `persist` and `threshold` are set only for the two
    /// strategies that have free-space managers to persist, and keep their
    /// defaults for the two that do not.
    pub fn new(strategy: FileSpaceStrategy, persist: bool, threshold: u64) -> Self {
        let uses_managers = matches!(
            strategy,
            FileSpaceStrategy::FsmAggr | FileSpaceStrategy::Page
        );
        Self {
            strategy,
            persist: uses_managers && persist,
            threshold: if uses_managers {
                threshold
            } else {
                Self::default().threshold
            },
            ..Self::default()
        }
    }

    /// `H5Pset_file_space_page_size`, the fourth file-space property and the
    /// one libhdf5 sets on its own call.
    ///
    /// Independent of the strategy, as upstream is: the value reaches the
    /// file-space info message whatever the strategy is, and only paged
    /// aggregation allocates by it. Out-of-range sizes are refused where the
    /// file is created ([`validate`](Self::validate)) rather than here, so a
    /// builder chain stays a builder chain.
    pub fn with_page_size(mut self, page_size: u64) -> Self {
        self.page_size = page_size;
        self
    }

    /// Whether the file has to say any of this on disk. `H5F__super_init`
    /// writes the file-space info message only for a file that differs from
    /// the library defaults in one of the four properties (H5Fsuper.c:1092),
    /// and raises such a file's superblock to version 2 so it has an
    /// extension to write it into (H5Fsuper.c:1144).
    pub fn is_default(&self) -> bool {
        *self == Self::default()
    }

    /// Refuse what this writer cannot make. `H5Pset_file_space_strategy`
    /// itself only refuses a strategy outside the enum (H5Pfcpl.c:1223), and
    /// `H5Pset_file_space_page_size` a page size outside `[512, 1 GiB]`
    /// (H5Pfcpl.c:1389-1393) — no power of two required, only the bounds.
    fn validate(&self) -> IoResult<()> {
        if !(PAGE_SIZE_MIN..=PAGE_SIZE_MAX).contains(&self.page_size) {
            return Err(crate::io::IoError::InvalidState(format!(
                "a file-space page size is between {PAGE_SIZE_MIN} bytes and \
                 {PAGE_SIZE_MAX}, not {}",
                self.page_size
            )));
        }
        match self.strategy {
            FileSpaceStrategy::FsmAggr
            | FileSpaceStrategy::Aggr
            | FileSpaceStrategy::None
            | FileSpaceStrategy::Page => Ok(()),
            FileSpaceStrategy::Unknown(b) => Err(crate::io::IoError::InvalidState(format!(
                "invalid file-space strategy {b}"
            ))),
        }
    }

    /// The message a created file carries, before anything is allocated:
    /// every manager address undefined and no end-of-allocation recorded,
    /// which is what `H5F__super_init` writes (H5Fsuper.c:1369-1382).
    fn message(&self) -> FileSpaceInfoMessage {
        FileSpaceInfoMessage {
            // `H5O_fsinfo_set_version` starts at version 1 and only ever
            // raises it, so a created file never carries the version-0 form
            // however low its version bounds are.
            version: 1,
            strategy: self.strategy,
            persist: self.persist,
            threshold: self.threshold,
            page_size: self.page_size,
            pgend_meta_thres: 0,
            eoa_pre_fsm_fsalloc: UNDEF_ADDR,
            fs_addr: vec![UNDEF_ADDR; FS_ADDR_COUNT_V1],
        }
    }
}

/// The shared object header message indexes a new file is created with.
///
/// libhdf5 sets these with three calls on the file creation property list:
/// `H5Pset_shared_mesg_nindexes` fixes how many indexes there are,
/// `H5Pset_shared_mesg_index` gives each one the message types it covers and
/// the smallest message it will take, and `H5Pset_shared_mesg_phase_change`
/// sets the list/B-tree thresholds for all of them at once. The default —
/// no indexes — is a file with no shared-message table, which is what every
/// file this crate wrote before the option existed.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SharedMessageConfig {
    /// Indexes in table order; only the first `count` are in use.
    indexes: [SohmIndexSpec; MAX_SOHM_INDEXES],
    /// How many indexes the caller asked for. Kept even when it is more than
    /// the array holds, so file creation can refuse the count the way
    /// `H5Pset_shared_mesg_nindexes` does rather than silently drop indexes.
    count: usize,
}

impl Default for SharedMessageConfig {
    fn default() -> Self {
        Self {
            indexes: [SohmIndexSpec {
                mesg_types: 0,
                min_mesg_size: 0,
                list_max: DEFAULT_SOHM_LIST_MAX,
                btree_min: DEFAULT_SOHM_BTREE_MIN,
            }; MAX_SOHM_INDEXES],
            count: 0,
        }
    }
}

impl SharedMessageConfig {
    /// One index per `(mesg_types, min_mesg_size)` pair — the arguments
    /// `H5Pset_shared_mesg_index` takes, where `mesg_types` is the bit mask
    /// [`type_flag`](crate::format::sohm::type_flag) builds — with the
    /// file-wide phase change `H5Pset_shared_mesg_phase_change` sets: above
    /// `list_max` an index is a v2 B-tree, below `btree_min` it is a list
    /// again, and `list_max == 0` makes it a B-tree from its first message.
    ///
    /// Nothing is validated here; [`Hdf5Writer::create_with_options`] refuses
    /// a configuration libhdf5 would refuse, so an invalid one is reported
    /// where the file is made rather than where the value is typed.
    pub fn new(indexes: &[(u16, u32)], list_max: u16, btree_min: u16) -> Self {
        let mut config = Self {
            count: indexes.len(),
            ..Self::default()
        };
        for (slot, &(mesg_types, min_mesg_size)) in config.indexes.iter_mut().zip(indexes) {
            *slot = SohmIndexSpec {
                mesg_types,
                min_mesg_size,
                list_max,
                btree_min,
            };
        }
        config
    }

    /// The indexes in use, in table order.
    pub(crate) fn specs(&self) -> &[SohmIndexSpec] {
        &self.indexes[..self.count.min(MAX_SOHM_INDEXES)]
    }

    /// Refuse a configuration `H5Pset_shared_mesg_nindexes` or
    /// `H5Pset_shared_mesg_phase_change` would refuse.
    fn validate(&self) -> IoResult<()> {
        if self.count > MAX_SOHM_INDEXES {
            return Err(crate::io::IoError::InvalidState(format!(
                "a file may declare at most {MAX_SOHM_INDEXES} shared-message \
                 indexes, not {}",
                self.count
            )));
        }
        for spec in self.specs() {
            // The two thresholds must not overlap, or an index would convert
            // back and forth on every insert.
            if u32::from(spec.btree_min) > u32::from(spec.list_max) + 1 {
                return Err(crate::io::IoError::InvalidState(format!(
                    "shared-message phase change needs btree_min ({}) at most one \
                     past list_max ({}), or an index converts on every insert",
                    spec.btree_min, spec.list_max
                )));
            }
            if spec.mesg_types == 0 {
                return Err(crate::io::IoError::InvalidState(
                    "a shared-message index covering no message type would never \
                     be used; give it a type mask or drop it"
                        .into(),
                ));
            }
        }
        Ok(())
    }
}

/// One object-reference element written before its value could be known.
///
/// An `H5R_OBJECT1` element is the target's object header address, and
/// addresses are assigned during finalize, so a write records the target by
/// path here and [`Hdf5Writer::write_object_reference_values`] puts the address
/// down once every header has one.
pub(crate) struct PendingObjectReference {
    /// Dataset holding the element.
    dataset: usize,
    /// Element index within that dataset.
    element: u64,
    /// Path of the object the element names; `/` is the root group.
    target: String,
}

/// One heap-backed reference object written before its target's address could
/// be known.
///
/// The *element* of a `H5R_DATASET_REGION1`, and of every 1.12 reference whose
/// encoding does not fit inline, is final at write time — it is the global-heap
/// id of the object the write inserted. What waits is the `sizeof_addr` bytes
/// of that heap object holding the target's object header address, which
/// [`Hdf5Writer::write_heap_reference_values`] stamps in.
pub(crate) struct PendingHeapReference {
    /// Address of the global-heap collection holding the object.
    collection: u64,
    /// The object's index within that collection.
    index: u16,
    /// Where the target's token sits inside that object. The pre-1.12 region
    /// form leads with it (`H5R__encode_token_region_compat`); every 1.12 form
    /// puts the token's length byte first (`H5R__encode_obj_token`).
    token_offset: usize,
    /// What the reference names, and how strictly its path must resolve.
    target: PendingHeapTarget,
}

/// What the path of a heap-backed reference must resolve to.
///
/// The two rules `H5R` applies: a region reference names a *dataset*, since
/// `H5Rcreate_region` takes one dataset's dataspace and every reader
/// dereferences it as one, while an attribute reference names the attribute's
/// owner, which `H5Rcreate_attr` lets be any object.
#[derive(Debug, Clone)]
pub(crate) enum PendingHeapTarget {
    Dataset(String),
    Object(String),
}

/// The value of an attribute whose elements are object references, kept as
/// what it means rather than as what it encodes to.
///
/// An attribute's value is part of its object header message, so it cannot be
/// stamped after the fact the way a dataset element can — the header is one
/// block, written once. What is stored instead is the paths, and
/// [`Hdf5Writer::object_attributes`] turns them into addresses every time the
/// attribute set is built: the measuring pass reads the zeros of objects that
/// have no address yet, the content pass reads the addresses the file will
/// have, and the two agree in length because an address is a fixed-width
/// field. The entry in the object's attribute list carries an image with
/// zeros where the addresses go and is never itself written.
///
/// The address of `targets[i]` lands at byte `i * stride` of that image: the
/// whole element when the attribute is an array of references, the leading
/// member when each element is a compound that carries other fields beside
/// the reference (`REFERENCE_LIST`'s `dimension`), which the stored image
/// already holds.
pub(crate) struct AttributeReferenceValue {
    /// The object the attribute hangs on.
    scope: AttrScope,
    /// The attribute's name within that object.
    name: String,
    /// Paths of the objects the elements name, in element order; `/` is the
    /// root group.
    targets: Vec<String>,
    /// Bytes from one element's address to the next: the element size.
    stride: usize,
}

/// The attribute naming the scales attached to each axis of a dataset.
pub(crate) const DIMENSION_LIST: &str = "DIMENSION_LIST";
/// The attribute naming every (dataset, axis) a dimension scale is attached to.
pub(crate) const REFERENCE_LIST: &str = "REFERENCE_LIST";
/// The `CLASS` a dimension scale carries.
const DIMENSION_SCALE_CLASS: &str = "DIMENSION_SCALE";

/// A dataset's `CLASS` attribute as `H5DS` reads it.
enum ClassAttr {
    /// A fixed-length string, with what `H5DSis_scale` checks beside the text.
    Fixed {
        size: u32,
        null_terminated: bool,
        text: String,
    },
    /// A variable-length string.
    VarLen(String),
    /// Not a string at all.
    NotString,
}

/// `bytes` read as a C string: everything before the first NUL.
fn c_string(bytes: &[u8]) -> String {
    let end = bytes.iter().position(|&b| b == 0).unwrap_or(bytes.len());
    String::from_utf8_lossy(&bytes[..end]).into_owned()
}

/// Refuse an object header body that is not the length its block was reserved
/// at.
///
/// The one check standing behind
/// [`HeaderLayout`]'s premise that measuring a header before its content is
/// final gives the same length as encoding it after. `what` names the object
/// only when the check fails, so the caller pays for the lookup only then.
fn check_header_size(
    encoded: &[u8],
    reserved: usize,
    what: impl FnOnce() -> String,
) -> IoResult<()> {
    if encoded.len() == reserved {
        return Ok(());
    }
    Err(crate::io::IoError::InvalidState(format!(
        "the object header of {} encodes to {} bytes but was measured at {}; \
         a message in it changed length once the addresses it names were known",
        what(),
        encoded.len(),
        reserved
    )))
}

/// Where one object header goes: chunk 0's block and, when the header does
/// not fit it, a continuation block of its own.
///
/// Produced by [`Hdf5Writer::place_header`] and consumed by
/// [`Hdf5Writer::encode_header_in`]; between the two, everything the header
/// names is built against the address it records. The sizes travel with the
/// addresses because they are what the blocks were reserved at: the writing
/// pass checks each image against them rather than trusting that the two
/// passes agreed.
#[derive(Debug, Clone, Copy)]
struct HeaderPlacement {
    /// Chunk 0's address.
    addr: u64,
    /// Bytes reserved at `addr`. For a fresh header that is the whole image,
    /// a continuation chunk included, since one is laid directly behind
    /// chunk 0 in the same block.
    size: usize,
    /// Whether the block is one the object's existing header already
    /// occupied, which chunk 0 is then held to the size of; a fresh block is
    /// an exact fit.
    kept: bool,
    /// A continuation block of its own, `(address, size)`: what a kept block
    /// too small for every message spills into.
    continuation: Option<(u64, usize)>,
}

impl HeaderPlacement {
    /// A block of `size` bytes at `addr` holding the whole header.
    fn fresh(addr: u64, size: usize) -> Self {
        Self {
            addr,
            size,
            kept: false,
            continuation: None,
        }
    }

    /// The placement as the registry records a written header: chunk 0's
    /// block, then the continuation block when there is one.
    fn blocks(&self) -> crate::io::object_header_io::HeaderBlocks {
        std::iter::once((self.addr, self.size as u64))
            .chain(self.continuation.map(|(a, s)| (a, s as u64)))
            .collect()
    }

    /// The placement a written header's recorded blocks describe, to write
    /// it back over: chunk 0 held to its block, and the continuation chunk,
    /// if it has one, to its own.
    fn over(blocks: &[(u64, u64)]) -> Option<Self> {
        match blocks {
            [(addr, size)] => Some(Self {
                addr: *addr,
                size: *size as usize,
                kept: true,
                continuation: None,
            }),
            [(addr, size), (cont, cont_size)] => Some(Self {
                addr: *addr,
                size: *size as usize,
                kept: true,
                continuation: Some((*cont, *cont_size as usize)),
            }),
            _ => None,
        }
    }
}

/// Where every object header this finalize writes goes.
///
/// Produced by [`Hdf5Writer::allocate_object_headers`] and consumed by
/// [`Hdf5Writer::write_object_headers`].
struct HeaderLayout {
    /// `(dataset index, placement)`, in write order.
    datasets: Vec<(usize, HeaderPlacement)>,
    /// `(group index, placement)`, in write order.
    groups: Vec<(usize, HeaderPlacement)>,
    /// The root group's placement.
    root: HeaderPlacement,
}

/// The chunk-0 blocks existing object headers keep across a rewrite, by
/// object: `(address, length)` of each, as the open-time walk read it.
///
/// Filled by [`Hdf5Writer::supersede_headers`] from the registry's
/// `obj_header_blocks` and consumed by
/// [`Hdf5Writer::allocate_object_headers`].
#[derive(Default)]
struct KeptChunks {
    datasets: std::collections::HashMap<usize, (u64, u64)>,
    groups: std::collections::HashMap<usize, (u64, u64)>,
    root: Option<(u64, u64)>,
}

/// Refuse a region-reference selection the target dataset's extent does not
/// admit — libhdf5's `H5S_select_valid`, which `H5Rcreate` applies before it
/// serializes anything.
///
/// The rank check comes from [`Selection::to_boxes`], which also refuses a
/// regular hyperslab with an unlimited count or block; a region reference has
/// no growable extent to resolve one against.
fn validate_region_selection(selection: &Selection, dims: &[u64], path: &str) -> IoResult<()> {
    let boxes = selection.to_boxes(dims).map_err(|e| {
        crate::io::IoError::InvalidState(format!("region reference over '{path}': {e}"))
    })?;
    for (start, count) in boxes {
        for (d, (&s, &c)) in start.iter().zip(&count).enumerate() {
            if s.checked_add(c).is_none_or(|end| end > dims[d]) {
                return Err(crate::io::IoError::InvalidState(format!(
                    "region reference over '{path}' selects {s}..{} in dimension {d}, \
                     outside the dataset's extent of {}",
                    s.saturating_add(c),
                    dims[d]
                )));
            }
        }
    }
    Ok(())
}

/// What a reopen found already on disk in dense form, by the scope whose
/// header names it.
///
/// Both halves together because they are found together — one walk of the
/// reopened headers fills both — and released together only in the delete
/// path; a finalize supersedes attribute storage before it lays object
/// headers out and link storage after, so each half has its own owner.
#[derive(Debug, Default)]
struct SupersededDense {
    attrs: HashMap<AttrScope, AttributeInfoMessage>,
    links: HashMap<LinkScope, LinkInfoMessage>,
}

/// Which object's attribute list a prepared dense layout belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum AttrScope {
    Root,
    Group(usize),
    Dataset(usize),
}

/// Which group's link list a prepared dense layout belongs to.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) enum LinkScope {
    Root,
    Group(usize),
}

/// Attributes an object header keeps before libhdf5 spills the whole set to
/// dense storage (`H5O_CRT_ATTR_MAX_COMPACT_DEF`).
const MAX_COMPACT_ATTRS: usize = 8;

/// Links `H5G__obj_create_real` sizes a new group's object header for
/// (`H5G_CRT_GINFO_EST_NUM_ENTRIES`), and the name length it assumes for each
/// (`H5G_CRT_GINFO_EST_NAME_LEN`). Together with the link info and group info
/// messages they are the whole of chunk 0 — see
/// [`chunk0_capacity`](Hdf5Writer::chunk0_capacity).
const EST_LINK_COUNT: usize = 4;
/// See [`EST_LINK_COUNT`].
const EST_LINK_NAME_LEN: usize = 8;

/// Messages a shared-message index keeps in list form before it becomes a v2
/// B-tree (`H5F_CRT_SHMSG_LIST_MAX_DEF`).
const DEFAULT_SOHM_LIST_MAX: u16 = 50;

/// Messages a shared-message B-tree index drops to before it reverts to a
/// list (`H5F_CRT_SHMSG_BTREE_MIN_DEF`).
const DEFAULT_SOHM_BTREE_MIN: u16 = 40;

/// Links a group header keeps before libhdf5 spills the whole set to dense
/// storage (`H5G_CRT_GINFO_MAX_COMPACT`). This writer emits no phase-change
/// values in the Group Info message, so the default is what applies.
const MAX_COMPACT_LINKS: usize = 8;

/// Bytes a compact dataset's raw image may occupy.
///
/// `H5D__compact_construct` bounds it by `H5O_MESG_MAX_SIZE` less the layout
/// message's own four bytes (version, class, and the 2-byte data length).
/// The constant it subtracts from is 65536, one past what the object header's
/// 2-byte message size field can express, so the ceiling here is taken from
/// [`MAX_MESSAGE_SIZE`] — the largest message that actually encodes — and is
/// one byte below libhdf5's.
pub const MAX_COMPACT_DATA: usize = MAX_MESSAGE_SIZE - 4;

/// Smallest userblock a file can be created with, and the granularity of
/// every larger one: `H5Pset_userblock` takes 0 or a power of two from here
/// up, because `H5FD_locate_signature` looks for the superblock at 0 and then
/// at this offset doubled repeatedly.
pub const MIN_USERBLOCK: u64 = 512;

impl Hdf5Writer {
    /// Create a new HDF5 file at `path` using the env-var-derived locking
    /// policy (controlled by `HDF5_USE_FILE_LOCKING`).
    ///
    /// The superblock (48 bytes for v3 with 8-byte offsets) is reserved at
    /// offset 0 and written during `close()`.
    pub fn create(path: &Path) -> IoResult<Self> {
        Self::create_with_locking(
            path,
            crate::io::locking::FileLocking::from_env_or(Default::default()),
        )
    }

    /// Create a new HDF5 file at `path` with an explicit locking policy.
    pub fn create_with_locking(
        path: &Path,
        locking: crate::io::locking::FileLocking,
    ) -> IoResult<Self> {
        Self::create_with_options(
            path,
            FileCreateOptions {
                locking,
                ..Default::default()
            },
        )
    }

    /// Create a new HDF5 file at `path` with explicit file-creation options.
    pub fn create_with_options(path: &Path, options: FileCreateOptions) -> IoResult<Self> {
        let FileCreateOptions {
            locking,
            track_order,
            track_times,
            libver,
            userblock,
            shared_messages,
            file_space,
        } = options;
        shared_messages.validate()?;
        file_space.validate()?;
        if userblock != 0 && (userblock < MIN_USERBLOCK || !userblock.is_power_of_two()) {
            return Err(crate::io::IoError::InvalidState(format!(
                "a userblock is {MIN_USERBLOCK} bytes or a power of two above it, \
                 not {userblock}: a reader locates the superblock by doubling its \
                 search offset from {MIN_USERBLOCK}, so no other size can hold one"
            )));
        }
        let policy = free_space::SpacePolicy::for_message(&file_space.message());
        // `H5F__super_init` (H5Fsuper.c:1182-1192) refuses a userblock that is
        // not a whole number of allocation units, which for a paged file is
        // the file-space page: everything after the userblock is addressed
        // from its end, so a userblock that is not a page multiple would put
        // every page boundary off the file's own grid.
        if let Some(page) = policy.page() {
            if userblock != 0 && userblock % page != 0 {
                return Err(crate::io::IoError::InvalidState(format!(
                    "a paged file's userblock is a multiple of its {page}-byte \
                     file-space page, not {userblock}"
                )));
            }
        }
        let mut handle = FileHandle::create_with_locking(path, locking)?;
        if userblock != 0 {
            // Written while the handle is still unbased, so offset 0 is the
            // start of the file: the block belongs to the application, not to
            // the HDF5 address space that begins where it ends. libhdf5 zeroes
            // it the same way (`H5F__super_init`), leaving a file whose first
            // `userblock` bytes are the application's to overwrite.
            handle.write_at(0, &vec![0u8; userblock as usize])?;
            handle.set_base(userblock);
        }
        let ctx = FormatContext::default_v3();

        // `H5F_LIBVER_EARLIEST` is the one bound under which libhdf5 writes
        // the classic generation — the version-1 rows of every
        // message-version table, the symbol-table group form
        // (`H5G__obj_create_real`, H5Gobj.c:179) and the version-0 superblock
        // row of `HDF5_superblock_ver_bounds`.
        //
        // Shared object header messages move the last of those three and
        // nothing else. Their master table lives in a superblock extension,
        // which only a version-2 superblock has, so `H5F__super_init` raises
        // the superblock to version 2 whatever the low bound says
        // (H5Fsuper.c:1135) — but it does not touch `H5F_LOW_BOUND`, which is
        // what every other rule reads. So such a file is a version-2
        // superblock over symbol-table groups and version-1 messages, which
        // is what the `tests/fixtures/sohm_*.h5` files libhdf5 itself wrote
        // are.
        let classic = libver == Some(LibverBound::Earliest);
        let legacy = classic.then(|| Box::new(LegacyFile::created(ctx, userblock)));
        // Non-default file-space properties raise the superblock the same way
        // a shared-message table does, and for the same reason: the message
        // that declares them lives in an extension, and only a version-2
        // superblock has one (H5Fsuper.c:1144).
        let superblock_version = SuperblockVersion::Chosen(
            if classic && shared_messages.specs().is_empty() && file_space.is_default() {
                SUPERBLOCK_V0
            } else {
                SUPERBLOCK_V2
            },
        );

        // Reserve the superblock at offset 0. Which version it gets is only
        // known once the file's content is (see `superblock_version_for`),
        // but the two a version-2 file can reach — 2 and 3 — encode to the
        // same size, so the reservation follows the base version alone.
        let superblock_size = match legacy.as_deref() {
            Some(l) if matches!(superblock_version, SuperblockVersion::Chosen(v) if v < SUPERBLOCK_V2) => {
                l.superblock.encoded_size()
            }
            _ => SuperblockV2V3::size_for(ctx.sizeof_addr),
        };
        // The superblock is an ordinary allocation, not a reservation: under
        // paged aggregation it takes the whole of page zero and leaves the
        // rest of that page as a section of the metadata manager, which is
        // what `H5F__super_init` gets from `H5MF_alloc(f, H5FD_MEM_SUPER, ...)`
        // going through `H5MF__alloc_pagefs`. Unpaged it returns offset zero
        // and moves the end of the file to `superblock_size`, which is what
        // reserving it did.
        let allocator = FileAllocator::with_policy(0, policy);
        allocator.allocate(superblock_size as u64, FreeSpaceClass::Metadata);

        Ok(Self {
            handle,
            allocator,
            ctx,
            datasets: Slot::new(Vec::new()),
            groups: Slot::new(Vec::new()),
            hard_links: Slot::new(Vec::new()),
            symbolic_links: Slot::new(Vec::new()),
            committed_datatypes: Slot::new(Vec::new()),
            preserved_links: Slot::new(Vec::new()),
            name_index: Slot::new(Box::new(NameIndex::new())),
            root_attributes: Slot::new(Vec::new()),
            create_lock: Slot::new(()),
            libver,
            closed: false,
            swmr_active: false,
            cwfs: Slot::new(Vec::new()),
            root_group_addr: None,
            superseded_root_header: Vec::new(),
            // A new file starts at the oldest superblock the generation it was
            // created in allows, and finalize raises it if the content needs a
            // newer one.
            superblock_version,
            dense_attributes: Slot::new(HashMap::new()),
            dense_links: Slot::new(HashMap::new()),
            superseded_dense: Slot::new(None),
            track_order: TrackOrder::uniform(track_order),
            track_times,
            root_track_order: TrackOrder::uniform(track_order),
            // The root group is created with the file, so it captures the
            // policy the same instant every other field of it is settled.
            root_times: track_times.then(|| ObjectTimes::created_at(now_seconds())),
            next_creation_seq: Slot::new(0),
            pending_object_references: Slot::new(Vec::new()),
            pending_heap_references: Slot::new(Vec::new()),
            attribute_references: Slot::new(Vec::new()),
            legacy,
            symbol_tables: SymbolTables::none_found(),
            // A created file has no extension to carry and no ranks but the
            // library defaults: `H5Pset_sym_k`/`H5Pset_istore_k` have no
            // equivalent on this writer's creation path.
            btree: BTreeV1Config::default(),
            extension: Box::default(),
            // A file created at the library defaults declares no file-space
            // strategy, so it has no message to write and no manager to keep;
            // one created with any other properties owns both.
            free_space: (!file_space.is_default()).then(|| {
                Box::new(FileSpaceState {
                    info: file_space.message(),
                    superseded: Vec::new(),
                })
            }),
            sohm: (!shared_messages.specs().is_empty())
                .then(|| Box::new(SohmState::new(shared_messages.specs().to_vec(), Vec::new()))),
            source_dir: source_dir_of(path)?,
        })
    }

    /// Target the libhdf5 2.0 file format for datasets created after this
    /// call: filtered chunked datasets get layout message version 5, whose
    /// chunk indexes store chunk sizes in a fixed `sizeof_size`-byte field
    /// with no overflow limit (see [`Self::chunk_layout_version`]). Off by
    /// default, because readers older than libhdf5 2.0 — including the
    /// 1.14-based h5py wheels — reject version 5.
    ///
    /// `false` names `H5F_LIBVER_EARLIEST`, the far end of the same table,
    /// rather than un-naming the bound: it is `set_libver_bound`'s contract
    /// that applies, chunk index included.
    pub fn set_libver_latest(&mut self, latest: bool) -> IoResult<()> {
        self.set_libver_bound(if latest {
            LibverBound::V200
        } else {
            LibverBound::Earliest
        })
    }

    /// Bytes this file reserves in front of its superblock
    /// (`H5Pget_userblock`).
    ///
    /// The same value for a file created with one and for a file reopened
    /// through [`open_append_with_locking`](Self::open_append_with_locking),
    /// which takes it from where the signature turned up: it is the base of
    /// the handle's address space either way.
    pub fn userblock_size(&self) -> u64 {
        self.handle.base()
    }

    /// Set the file's low libver bound, the equivalent of
    /// `H5Pset_libver_bounds`'s `low` argument. Objects created after this
    /// call encode their messages at the versions that bound calls for.
    ///
    /// On a reopened file the bound is taken as named, above or below the row
    /// the file's superblock version belongs to, as `H5Fopen` takes a fapl's
    /// since libhdf5 2.0 (HDFGroup/hdf5#4939): a version-3 superblock opened
    /// at `Earliest` gets version-1 B-tree chunk indexes appended, as h5py on
    /// libhdf5 2.x appends them. Only a bound the file's format cannot express
    /// at all is refused.
    pub fn set_libver_bound(&mut self, libver: LibverBound) -> IoResult<()> {
        // A classic file cannot honour a newer bound: every encoder in it
        // reads `H5F_LOW_BOUND`, and raising that is what makes libhdf5 write
        // the version-2/3 superblock this file does not have. Refused rather
        // than pinned silently, so the caller learns the bound did not take.
        if libver != LibverBound::Earliest && self.is_legacy() {
            return Err(crate::io::IoError::Unsupported(format!(
                "cannot set the library-version bound to {libver:?} on this file: it is                  in the classic (version-0/1 superblock) format, which libhdf5 writes                  only at H5F_LIBVER_EARLIEST"
            )));
        }
        self.libver = Some(libver);
        Ok(())
    }

    /// The generation the *message* encoders follow — dataspace, datatype,
    /// fill value, attribute.
    ///
    /// A property of the file, not of the object: `H5S__set_version`,
    /// `H5O__fill_set_version`, `H5A__set_version` and `H5T_set_version` all
    /// read `H5F_LOW_BOUND(f)` and nothing about the object they are encoding
    /// for. So a creation-order-tracking group in a classic file still gets
    /// version-1 dataspaces and version-1 attribute messages, even though its
    /// own header is version 2.
    fn message_format(&self) -> ObjectFormat {
        match self.legacy {
            Some(_) => ObjectFormat::Legacy,
            None => ObjectFormat::Modern,
        }
    }

    /// The object header version an object with this creation-order policy
    /// gets — `H5O__set_version` (H5Oint.c:251).
    ///
    /// Version 1 is the floor a classic file's low bound sets, but tracking
    /// creation order of *either* kind raises the object past it: the link
    /// creation index lives in the message envelope and the attribute tracking
    /// flags live in the header prefix, and version 1 has neither. This is a
    /// per-object question in a classic file, which is why the format is not
    /// one switch for the whole file — libhdf5 writes version-2 headers inside
    /// a version-0 superblock whenever the creation property list asks for
    /// creation order.
    fn header_format(&self, track: TrackOrder) -> ObjectFormat {
        let attrs = self.header_attr_order(track.attrs);
        if self.legacy.is_some() && !track.links.is_tracked() && !attrs.is_tracked() {
            ObjectFormat::Legacy
        } else {
            ObjectFormat::Modern
        }
    }

    /// The attribute creation-order policy an object header records, given
    /// what the object's creation property list asked for.
    ///
    /// A file whose shared-message configuration covers attributes records a
    /// creation index on every object header message: a shared attribute is
    /// found again through it, so `H5SM_init` sets `store_msg_crt_idx`
    /// (H5SM.c:220) and `H5O__create_ohdr` then raises every header it creates
    /// to version 2 and ORs `H5O_HDR_ATTR_CRT_ORDER_TRACKED` into its flags
    /// (H5Oint.c:364, H5Oint.c:442) whatever the property list says. So on
    /// such a file the floor is `Tracked` — this is the only place that floor
    /// is applied, and both the header version and the header flags come
    /// through here.
    fn header_attr_order(&self, requested: CreationOrder) -> CreationOrder {
        if requested.is_tracked() || !self.tracks_message_creation_index() {
            return requested;
        }
        CreationOrder::Tracked
    }

    /// Whether this finalize replaces the file's shared-message table.
    ///
    /// It does whenever the file has indexes and no table has been published
    /// this session — every finalize of a file created with them, and the
    /// first finalize after a reopen. `build_shared_messages` lays a table out
    /// whole from the whole message set rather than inserting into an existing
    /// one, so a reopen's table is a *replacement*: every heap ID in the file
    /// is reassigned, which makes every object header that holds one stale
    /// however little else about it changed. A second finalize (a SWMR close)
    /// keeps the table the first published and answers `false`.
    fn rebuilds_shared_messages(&self) -> bool {
        self.sohm
            .as_deref()
            .is_some_and(|s| s.table_addr.lock().is_none())
    }

    /// Whether every object header this writer emits records message creation
    /// indices.
    fn tracks_message_creation_index(&self) -> bool {
        self.sohm
            .as_deref()
            .is_some_and(SohmState::shares_attributes)
    }

    /// Whether the group at `scope` stores its links in a symbol table —
    /// `H5G__obj_create_real` (H5Gobj.c:129) and the conversion
    /// `H5G_obj_insert` performs (H5Gobj.c:512).
    ///
    /// The new group format is used unconditionally from `H5F_LIBVER_V18` up
    /// *for a group being created*, and below it only when the group tracks
    /// link creation order: a symbol table entry has no room for a creation
    /// index. The two axes are independent — a group that tracks only
    /// *attribute* creation order gets a version-2 header over a symbol table,
    /// which is what libhdf5 writes for it.
    ///
    /// A group the reopen found in a symbol table is not being created, and
    /// `H5G_obj_insert` never moves an existing group to the new format for
    /// the bound's sake. So [`SymbolTables::found`] answers for it whatever
    /// generation the rest of this session writes at.
    ///
    /// The content of the group is the third axis. A symbol table entry has
    /// three cache types and no room for a fourth, so an external or
    /// user-defined link cannot go in one; libhdf5 answers by converting that
    /// one group to link messages the moment such a link is inserted, leaving
    /// the superblock version, the object header version and every other group
    /// in the file alone. This writer builds each group's storage once at
    /// finalize rather than link by link, so the same rule reads as a question
    /// about the finished set.
    fn uses_symbol_table(&self, scope: LinkScope, links: CreationOrder) -> bool {
        (self.legacy.is_some() || self.symbol_tables.found.contains(&scope))
            && !links.is_tracked()
            && self.links_fit_symbol_table(scope, links)
    }

    /// Whether every link `scope` holds is one a symbol table entry can
    /// express — `H5G_obj_insert`'s `obj_lnk->cset != H5T_CSET_ASCII ||
    /// obj_lnk->type > H5L_TYPE_BUILTIN_MAX` test (H5Gobj.c:514), asked of the
    /// whole set.
    ///
    /// A link a reopen carried through verbatim counts too, and one this
    /// writer cannot even decode counts as not fitting: the entry would have
    /// to be built from the decoded form, while a link message is re-emitted
    /// byte for byte.
    fn links_fit_symbol_table(&self, scope: LinkScope, order: CreationOrder) -> bool {
        self.group_links(scope, order)
            .iter()
            .all(LinkMessage::fits_symbol_table)
            && self.preserved_links_for(scope).iter().all(|encoded| {
                LinkMessage::decode(encoded, &self.ctx)
                    .is_ok_and(|(link, _)| link.fits_symbol_table())
            })
    }

    /// The header format of the registered dataset at `index`.
    ///
    /// A dataset has no links, so only the attribute half of the policy can
    /// raise it past version 1.
    fn dataset_header_format(&self, index: usize) -> ObjectFormat {
        let ds = self.ds(index);
        let attrs = ds.lock().track_attr_order;
        self.header_format(TrackOrder {
            links: CreationOrder::default(),
            attrs,
        })
    }

    /// The header format of the registered group at `index`.
    fn group_header_format(&self, index: usize) -> ObjectFormat {
        let grp = self.grp(index);
        let track = grp.lock().track_order;
        self.header_format(track)
    }

    /// The bound one family of encoders is written at, and the single reader
    /// of the `libver` field.
    ///
    /// A bound the caller named is the fapl's `low`, as it was named: since
    /// libhdf5 2.0 `H5F__super_read` raises nothing but an SWMR-write open
    /// (HDFGroup/hdf5#4939), so a reopened file's superblock version says
    /// which generation the file *is* and nothing about the generation this
    /// session appends in. With no bound named the answer is `create_default`,
    /// which differs per family because this crate's default file is two rows
    /// rather than one bound (see the `libver` field, [`encoding_libver`] and
    /// [`layout_version_bound`]) — the same default on a created file, whose
    /// superblock is then written to match, and on a reopened one, whose
    /// superblock is written back as found.
    ///
    /// [`encoding_libver`]: Self::encoding_libver
    /// [`layout_version_bound`]: Self::layout_version_bound
    fn session_libver(&self, create_default: LibverBound) -> LibverBound {
        let bound = self.libver.unwrap_or(create_default);
        match self.message_format() {
            // `H5F_LIBVER_EARLIEST` is the only low bound under which libhdf5
            // writes a version-0/1 superblock at all, so a newer structure
            // inside one is a combination no libhdf5 produces. Refused where
            // the caller asks for it (`set_libver_bound`) rather than silently
            // dropped; capping here is what keeps the encoders honest if a
            // path ever misses that gate.
            ObjectFormat::Legacy => bound.min(LibverBound::Earliest),
            ObjectFormat::Modern => bound,
        }
    }

    /// The bound the message encoders see — dataspace, datatype, fill value,
    /// attribute.
    fn encoding_libver(&self) -> LibverBound {
        self.session_libver(LibverBound::Earliest)
    }

    /// The data layout message version this file's bound calls for —
    /// `H5O_layout_ver_bounds[H5F_LOW_BOUND(f)]` (H5Dlayout.c:44), the term
    /// `H5D__chunk_set_info` weighs against the version a chunk *requires*
    /// (H5Dchunk.c:936, :1046).
    ///
    /// With no bound named the row is `H5F_LIBVER_V110`'s: this crate's
    /// default file uses the v1.10 chunk indexes, which is exactly what that
    /// row says and what no other row does (see the `libver` field for why the
    /// default is not `Earliest` here even though the datatype and superblock
    /// tables read it that way). A reopened file takes the same default: a
    /// version-2 superblock gets v1.10 indexes appended unless a bound below
    /// `V110` is named, whose layout version of 3 has no index-type field at
    /// all and puts the appended chunks on the version-1 B-tree.
    fn layout_version_bound(&self) -> u8 {
        self.session_libver(LibverBound::V110).layout_version()
    }

    /// The data layout version a chunk of `chunk_bytes` *requires* whatever
    /// the bound says — `version_req` in `H5D__chunk_set_info` (H5Dchunk.c:909).
    ///
    /// Only one thing raises it: a chunk over 4 GiB does not fit the version-4
    /// message's 32-bit stored-size field. The floor is the default the
    /// creation property list carries, `H5O_LAYOUT_VERSION_DEFAULT`
    /// (H5Oprivate.h:451), which is why a classic file's chunked dataset is a
    /// version-3 message rather than the version-1 its bound's row names.
    fn required_chunk_layout_version(chunk_bytes: u64) -> u8 {
        if chunk_bytes > u32::MAX as u64 {
            5
        } else {
            LAYOUT_VERSION_DEFAULT
        }
    }

    /// Whether a new chunked dataset of this chunk size is indexed by one of
    /// the v1.10 indexes — extensible array, fixed array, v2 B-tree, single
    /// chunk or implicit — rather than by the version-1 B-tree.
    ///
    /// The gate `H5D__chunk_set_info` puts in front of the whole
    /// index-selection block (H5Dchunk.c:936): the bound's layout version
    /// reaches 4, or the chunk requires a version that does. Only inside it
    /// does the dataspace get to pick between the five; below it the layout
    /// message has no index-type field and the chunks go on the version-1
    /// B-tree. So the format decides before the shape does — a fixed shape
    /// covered by exactly one chunk takes the single-chunk index only on the
    /// near side of this gate.
    pub(crate) fn uses_v110_chunk_indexing(&self, chunk_bytes: u64) -> bool {
        self.layout_version_bound() >= 4 || Self::required_chunk_layout_version(chunk_bytes) >= 4
    }

    /// Refuse an SWMR session this file's format cannot record.
    ///
    /// The two checks `H5F__start_swmr_write` opens with: the superblock must
    /// be at least version 3 (H5Fint.c:3814, hdf5_1.14.6 H5Fint.c:3751) — the
    /// only version with the status-flags field that says a writer is attached
    /// — and the low bound must be at least `H5F_LIBVER_V110` (H5Fint.c:3818),
    /// the oldest bound whose `HDF5_superblock_ver_bounds` row reaches version
    /// 3.
    ///
    /// The first is the [`SuperblockVersion`] question: a reopened file
    /// already has its version and reopening never rewrites one, so it is
    /// checked as found; a file this writer created has no version on disk
    /// yet, and nothing in it says the superblock may not be version 3 — SWMR
    /// is what makes it one. The second is asked of the bound the caller
    /// named on either path, since a reopened file's superblock no longer
    /// raises it (HDFGroup/hdf5#4939); a caller who named none passes,
    /// because the default's layout row is `V110`'s, the row the v1.10 chunk
    /// indexes an SWMR reader follows belong to.
    ///
    /// Named, not silently upgraded. libhdf5 upgrades in the one case where
    /// SWMR is asked for at *create* time (`H5F_ACC_SWMR_WRITE` raises the
    /// bound to V110 in `H5F__super_init`, H5Fsuper.c:1131); on the reopen
    /// path it refuses instead, and so does this.
    fn reject_swmr(&self) -> IoResult<()> {
        let below_v110 = self.libver.is_some_and(|b| b < LibverBound::V110);
        let why = match self.superblock_version {
            SuperblockVersion::Existing(version) if version < SUPERBLOCK_V3 => format!(
                "its superblock is version {version}, and reopening a file never \
                 rewrites that"
            ),
            SuperblockVersion::Chosen(_) if self.is_legacy() => {
                "it is in the classic (version-0/1 superblock) format that \
                 H5F_LIBVER_EARLIEST selects"
                    .to_string()
            }
            _ if below_v110 => "it was asked for at a library-version bound below \
                 H5F_LIBVER_V110, whose superblock row is version 2"
                .to_string(),
            _ => return Ok(()),
        };
        Err(crate::io::IoError::Unsupported(format!(
            "cannot start an SWMR session on this file: {why}, and SWMR needs a \
             version-3 superblock to record that a writer is attached; create the \
             file at H5F_LIBVER_V110 or newer"
        )))
    }

    /// Whether this file is in the classic (version-0/1 superblock) format,
    /// whose groups store their links in symbol tables — either because it
    /// was reopened in it or because it was created at
    /// `H5F_LIBVER_EARLIEST`.
    pub(crate) fn is_legacy(&self) -> bool {
        self.legacy.is_some()
    }

    /// The v1-B-tree "K" ranks in force for this file, from which every v1
    /// node's width is derived.
    ///
    /// A version-0/1 superblock records them in a field of its own and a
    /// version-2/3 one in a B-tree-K message in its superblock extension, so
    /// the file's generation says nothing about whether they are the defaults
    /// — `H5F__super_read` reads both into the same `H5F_shared_t`, and so
    /// does the reopen, into `btree`.
    fn btree_v1_config(&self) -> BTreeV1Config {
        self.btree
    }

    /// Track and index creation order for the links and the attributes of
    /// every object created after this call — the equivalent of setting
    /// `H5Pset_link_creation_order` and `H5Pset_attr_creation_order` to
    /// `H5P_CRT_ORDER_TRACKED | H5P_CRT_ORDER_INDEXED` on the creation
    /// property lists those objects are made with.
    ///
    /// Objects already created keep the policy they were made under, exactly
    /// as libhdf5 keeps what their creation property list said. The root
    /// group is created with the file, so its policy comes from
    /// [`create_with_options`](Self::create_with_options) instead.
    pub fn set_track_order(&mut self, track: bool) {
        self.track_order = TrackOrder::uniform(track);
    }

    /// Record the times of every object created after this call —
    /// `H5Pset_obj_track_times` on the creation property lists those objects
    /// are made with.
    ///
    /// Off by default, which is h5py's default and not libhdf5's: h5py's
    /// high-level API sets `track_times=False` on every object it makes
    /// (`_hl/files.py:189`, `_hl/dataset.py:39`, `_hl/group.py:42`), while a
    /// bare creation property list leaves it on (`H5O_CRT_OHDR_FLAGS_DEF` is
    /// `H5O_HDR_STORE_TIMES`, H5Opkg.h:74). A caller after libhdf5's own
    /// bytes turns it on here.
    ///
    /// Objects already created keep the policy they were made under, and the
    /// root group takes its own from
    /// [`create_with_options`](Self::create_with_options) — the same split
    /// [`set_track_order`](Self::set_track_order) has, and for the same
    /// reason: this is a creation property, not a file-wide setting.
    pub fn set_track_times(&mut self, track: bool) {
        self.track_times = track;
    }

    /// The times an object created right now records — all four set to the
    /// current time, as `H5O_apply_ohdr` initialises them (H5Oint.c:411-414),
    /// or `None` when this session is not tracking times.
    ///
    /// INVARIANT: every object this writer registers takes its `times` from
    /// here. The policy belongs to the creation property list, so reading
    /// [`track_times`](Self::track_times) at any later moment — a finalize, a
    /// header rewrite — would stamp a policy the object was not made under.
    fn created_object_times(&self) -> Option<ObjectTimes> {
        self.track_times
            .then(|| ObjectTimes::created_at(now_seconds()))
    }

    /// Layout message version for a new chunked dataset on one of the v1.10
    /// indexes — `H5D__chunk_set_info`'s closing
    /// `MAX3(layout->version, version_req, MIN(bound, version_perf))`
    /// (H5Dchunk.c:1046).
    ///
    /// Version 5 is *required* for a chunk over 4 GiB (pre-2.0 readers cannot
    /// handle one even though the v4 wire format could express it) and
    /// *preferred* for filtered chunks, which is why it takes the file's
    /// bound to get there: the preference is capped by the bound's own row,
    /// so only the 2.0 format lets it through. Everything else stays at
    /// version 4, which every 1.10+ reader accepts.
    fn chunk_layout_version(&self, filtered: bool, chunk_bytes: u64) -> u8 {
        // `version_perf`: 4 for the v1.10 indexes as such, 5 when a filter
        // can make a chunk expand past what version 4 can record.
        let preferred = if filtered { 5 } else { 4 };
        Self::required_chunk_layout_version(chunk_bytes)
            .max(self.layout_version_bound().min(preferred))
            .max(LAYOUT_VERSION_DEFAULT)
    }

    /// Width of the stored-chunk-size field in a filtered chunk index:
    /// version 5 uses the fixed `sizeof_size`; version 4 derives it from the
    /// uncompressed chunk byte count (one spare byte included), the
    /// `H5D_*_COMPUTE_CHUNK_SIZE_LEN` rule shared by the extensible-array,
    /// fixed-array and v2-B-tree indexes.
    fn chunk_size_len_for(&self, layout_version: u8, chunk_bytes: u64) -> u8 {
        if layout_version >= 5 {
            self.ctx.sizeof_size
        } else {
            compute_chunk_size_len(chunk_bytes)
        }
    }

    /// Provide public access to the format context.
    pub fn ctx(&self) -> &FormatContext {
        &self.ctx
    }

    /// Number of dataset slots in the registry (including soft-deleted ones).
    pub(crate) fn dataset_count(&self) -> usize {
        self.datasets.lock().len()
    }

    /// Clone out the [`DatasetRef`] for `index`, releasing the registry lock
    /// immediately. Lock the returned ref to read or mutate that one dataset.
    ///
    /// Panics on an out-of-range index, exactly like the `Vec` indexing it
    /// replaces; bounds-checking callers consult [`Self::dataset_count`] first.
    ///
    /// MUST NOT be called while the registry [`Slot`] is already locked (it
    /// would deadlock the `threadsafe` mutex / panic the single-thread
    /// `RefCell`): collect the refs you need, drop the registry guard, then work.
    pub(crate) fn ds(&self, index: usize) -> DatasetRef {
        Shared::clone(&self.datasets.lock()[index])
    }

    /// Number of group slots in the registry (including soft-deleted ones).
    pub(crate) fn group_count(&self) -> usize {
        self.groups.lock().len()
    }

    /// Clone out the [`GroupRef`] for `index`. Same contract as [`Self::ds`].
    pub(crate) fn grp(&self, index: usize) -> GroupRef {
        Shared::clone(&self.groups.lock()[index])
    }

    /// Enter the create gate: take `create_lock` and check that `name` is not
    /// already taken. The returned witness is what [`Self::push_dataset`]
    /// requires, so the uniqueness check and the registry push are atomic
    /// (see `create_lock`) at every creator by construction.
    pub(crate) fn begin_create(&self, name: &str) -> IoResult<CreateGuard<'_>> {
        let gate = self.create_lock.lock();
        // A creation path through hard links lands in the link's target
        // group, as HDF5 traversal does. Canonicalizing here — the one
        // entry every creator passes — keeps alias forms out of the
        // registry.
        let name = self.canonical_dataset_path(name);
        // A path that leaves this file, or that runs into an object the
        // reopen kept verbatim, is refused here rather than at each creator:
        // this is the one gate every creation passes, so a creator added
        // later cannot forget the check. Both run before the parent lookup,
        // which would otherwise report the group such a path names as absent
        // instead of naming what stops the path. Uniqueness comes first among
        // them: a name already in the file is taken whatever holds it.
        self.reject_external_traversal(&name)?;
        self.ensure_name_free(&name)?;
        self.reject_preserved_object(&name)?;
        let (parent, _leaf) = self.split_parent(&name)?;
        Ok(CreateGuard {
            _gate: gate,
            name,
            parent,
        })
    }

    /// Split an object path into the group that will hold its link and the
    /// leaf link name, resolving every component through the group registry.
    ///
    /// `path` is the registry form — no leading `/`, e.g. `"grp/sub/late"`.
    /// This is what keeps a `/` out of a link name: HDF5 link names are
    /// single path components (`H5G_traverse` splits on `/` before it ever
    /// reaches `H5L_link`), so a name that carries a path must name a group
    /// that exists, or be refused.
    ///
    /// A missing component is an error rather than an implicit group: the
    /// default link creation property list has `H5Pset_create_intermediate_group`
    /// off, and this writer exposes no property list to turn it on with.
    fn split_parent(&self, path: &str) -> IoResult<(Option<usize>, String)> {
        let (parent_path, leaf) = path.rsplit_once('/').unwrap_or(("", path));
        if leaf.is_empty() {
            return Err(crate::io::IoError::InvalidState(format!(
                "'{path}' does not end in a link name"
            )));
        }
        if parent_path.is_empty() {
            return Ok((None, leaf.to_string()));
        }
        let abs = format!("/{parent_path}");
        let groups = self.group_refs();
        let idx = groups
            .iter()
            .position(|g| {
                let gg = g.lock();
                gg.name == abs && !gg.deleted
            })
            .ok_or_else(|| {
                crate::io::IoError::NotFound(format!(
                    "cannot create '{path}': group '{abs}' does not exist"
                ))
            })?;
        Ok((Some(idx), leaf.to_string()))
    }

    /// Push a freshly-built dataset into the registry and return its index.
    /// Takes the registry lock only for the push, so it does not block an
    /// in-flight write that already cloned its own [`DatasetRef`] out.
    /// The [`CreateGuard`] proves the caller entered through
    /// [`Self::begin_create`] and still holds the gate.
    pub(crate) fn push_dataset(&self, create: &CreateGuard<'_>, info: DatasetInfo) -> usize {
        let name = info.name.clone();
        let idx = {
            let mut reg = self.datasets.lock();
            let idx = reg.len();
            reg.push(Shared::new(DatasetCell::new(info)));
            idx
        };
        self.register_name(&name, NameHit::Dataset(idx));
        // The spine guard is dropped before the group slot is taken: the lock
        // order is spine -> slot and never the reverse.
        if let Some(pidx) = create.parent {
            self.grp(pidx).lock().child_datasets.push(idx);
        }
        idx
    }

    /// Push a freshly-built group into the registry and return its index.
    pub(crate) fn push_group(&self, info: GroupInfo) -> usize {
        let name = info.name.trim_start_matches('/').to_string();
        let idx = {
            let mut reg = self.groups.lock();
            let idx = reg.len();
            reg.push(Shared::new(Slot::new(info)));
            idx
        };
        self.register_name(&name, NameHit::Group(idx));
        idx
    }

    /// Snapshot every [`DatasetRef`] (spine lock held only for the clone).
    /// Iterate the snapshot to lock each dataset one at a time — this keeps
    /// the lock order *spine → slot* and never reacquires the spine while a
    /// slot is held, which is what makes the registry deadlock-free.
    pub(crate) fn dataset_refs(&self) -> Vec<DatasetRef> {
        self.datasets.lock().iter().map(Shared::clone).collect()
    }

    /// Snapshot every [`GroupRef`]; see [`Self::dataset_refs`].
    pub(crate) fn group_refs(&self) -> Vec<GroupRef> {
        self.groups.lock().iter().map(Shared::clone).collect()
    }

    /// Snapshot the hard-link list (the lock is held only for the clone), so
    /// callers can resolve each link's target/parent — which locks dataset and
    /// group slots — without holding the hard-link lock.
    /// The next creation sequence number.
    ///
    /// One monotonic counter for datasets, groups and hard links alike: a
    /// group orders its links by it, so an interleaved run of `create_group`
    /// and `create_dataset` comes back out in the order it was made rather
    /// than grouped by kind.
    fn take_creation_seq(&self) -> u64 {
        let mut next = self.next_creation_seq.lock();
        let seq = *next;
        *next += 1;
        seq
    }

    pub(crate) fn hard_links_vec(&self) -> Vec<HardLink> {
        self.hard_links.lock().clone()
    }

    /// Snapshot the symbolic-link list; see [`Self::hard_links_vec`].
    pub(crate) fn symbolic_links_vec(&self) -> Vec<SymbolicLink> {
        self.symbolic_links.lock().clone()
    }

    /// Open an existing HDF5 file for appending new datasets, using the
    /// env-var-derived locking policy.
    ///
    /// Reads existing dataset object headers fully, reconstructing metadata
    /// for chunked datasets so that `write_chunk` and `extend_dataset` work
    /// on reopened datasets.
    pub fn open_append(path: &Path) -> IoResult<Self> {
        Self::open_append_with_locking(
            path,
            crate::io::locking::FileLocking::from_env_or(Default::default()),
        )
    }

    /// Carry a reopened file's shared-message table into the writer's model:
    /// the index specifications the file was created with, and every block the
    /// table occupies so the finalize that replaces it can give them back.
    ///
    /// `H5SM_init` fixes the index count, each index's type mask, its minimum
    /// message size and the file-wide phase-change pair when the file is
    /// created, and nothing afterwards changes any of them — they are file
    /// creation properties. So the master table on disk *is* the
    /// [`SharedMessageConfig`] the file was made with, read back.
    ///
    /// Returns `None` for a file with no shared-message table, which is every
    /// file libhdf5 writes without `H5Pset_shared_mesg_nindexes`.
    /// Read the free-space managers a reopened file persists, if it does.
    ///
    /// `H5F__super_read` copies the file-space info message's addresses into
    /// `f->shared->fs_addr[]` and the library opens each manager lazily; this
    /// reads them all at once, because the writer needs the whole section set
    /// before it allocates anything.
    ///
    /// Returns `None` — nothing read, nothing to write back — for a file with
    /// no file-space info message, one that does not persist, and one whose
    /// strategy keeps no managers at all.
    fn reopen_free_space(
        handle: &mut FileHandle,
        meta: &crate::io::FileMeta,
        ext: &crate::io::reader::SuperblockExtension,
    ) -> IoResult<ReopenedFreeSpace> {
        let none = || ReopenedFreeSpace {
            state: None,
            sections: Vec::new(),
        };
        let Some(info) = ext.file_space_info.as_ref().filter(|i| i.persist) else {
            return Ok(none());
        };
        if !matches!(
            info.strategy,
            FileSpaceStrategy::FsmAggr | FileSpaceStrategy::Page
        ) {
            return Ok(none());
        }
        let found = crate::io::free_space_io::read_managers(handle, &meta.ctx, info)?;
        Ok(ReopenedFreeSpace {
            state: Some(Box::new(FileSpaceState {
                info: info.clone(),
                superseded: found.blocks,
            })),
            sections: found.sections,
        })
    }

    fn reopen_shared_messages(
        handle: &mut FileHandle,
        meta: &crate::io::FileMeta,
        ext: &crate::io::reader::SuperblockExtension,
    ) -> IoResult<Option<Box<SohmState>>> {
        use crate::format::chunk_index::btree_v2::collect_btree_v2_extents;
        use crate::format::fractal_heap::collect_heap_extents;
        use crate::format::sohm::{list_size, SohmMasterTable, SOHM_INDEX_LIST};

        let (Some(table), Some(smt)) = (
            meta.sohm.as_ref().filter(|t| !t.indexes.is_empty()),
            ext.shared_message_table.as_ref(),
        ) else {
            return Ok(None);
        };
        let ctx = &meta.ctx;

        // The extension header itself is superseded by `CarriedExtension`,
        // which owns it whether or not the file has shared messages; what is
        // superseded here is only the storage the table message names.
        let mut superseded = Vec::new();
        superseded.push((
            smt.table_address,
            SohmMasterTable::encoded_size(ctx, smt.nindexes) as u64,
        ));

        let mut specs = Vec::with_capacity(table.indexes.len());
        for index in &table.indexes {
            specs.push(SohmIndexSpec {
                mesg_types: index.mesg_types,
                min_mesg_size: index.min_mesg_size,
                list_max: index.list_max,
                btree_min: index.btree_min,
            });
            let mut reader = crate::io::reader::HandleBlockReader { handle };
            if index.heap_addr != UNDEF_ADDR {
                superseded.extend(collect_heap_extents(index.heap_addr, ctx, &mut reader)?);
            }
            if index.index_addr != UNDEF_ADDR {
                if index.index_type == SOHM_INDEX_LIST {
                    // `H5SM_LIST_SIZE`: the block is sized for `list_max`
                    // records however few are in it.
                    superseded.push((index.index_addr, list_size(ctx, index.list_max) as u64));
                } else {
                    superseded.extend(collect_btree_v2_extents(
                        index.index_addr,
                        ctx,
                        &mut reader,
                    )?);
                }
            }
        }
        Ok(Some(Box::new(SohmState::new(specs, superseded))))
    }

    /// Open an existing HDF5 file for appending with an explicit locking
    /// policy.
    pub fn open_append_with_locking(
        path: &Path,
        locking: crate::io::locking::FileLocking,
    ) -> IoResult<Self> {
        let mut handle = FileHandle::open_readwrite_with_locking(path, locking)?;
        // The same `H5FD_locate_signature` search the read path makes, through
        // the same handle mechanism: the offset it finds is the file's base
        // address, so the allocator's end-of-file, every write and the
        // superblock rewrite all work in the HDF5 address space, and the
        // userblock in `[0, base)` is not addressable from this writer at all.
        let super_addr = handle
            .locate_signature()?
            .ok_or(crate::format::FormatError::InvalidSignature)?;
        handle.set_base(super_addr);
        let file_size = handle.file_size()?;

        let sb_buf = handle.read_at_most(0, 256)?;
        // Which generation the file is decides everything the close then
        // writes back: version-1 object headers and symbol-table groups over a
        // version-0/1 superblock, or version-2 headers and link-message groups
        // over a version-2/3 one. libhdf5 writes those two combinations and no
        // mixture of them, so the branch is taken once, here, and carried as
        // `legacy`.
        let version = crate::format::superblock::detect_superblock_version(&sb_buf)?;
        let (ctx, sb_btree, root_addr, ext_addr, legacy) = if version <= 1 {
            let sb = SuperblockV0V1::decode(&sb_buf)?;
            let ctx = FormatContext {
                sizeof_addr: sb.sizeof_offsets,
                sizeof_size: sb.sizeof_lengths,
            };
            // Unlike a v2/v3 superblock, a classic one carries the "K" ranks
            // itself; every v1-B-tree and symbol-table node width in the file
            // comes from them.
            let btree = crate::format::btree_v1::BTreeV1Config {
                sym_leaf_k: sb.sym_leaf_k,
                snode_internal_k: sb.btree_internal_k,
                chunk_internal_k: sb.indexed_storage_k.unwrap_or(32),
            };
            let root = sb.root_symbol_table_entry.obj_header_addr;
            let ext = sb.superblock_extension_address;
            (ctx, btree, root, ext, Some(sb))
        } else {
            let sb = SuperblockV2V3::decode(&sb_buf)?;
            let ctx = FormatContext {
                sizeof_addr: sb.sizeof_offsets,
                sizeof_size: sb.sizeof_lengths,
            };
            (
                ctx,
                crate::format::btree_v1::BTreeV1Config::default(),
                sb.root_group_object_header_address,
                sb.superblock_extension_address,
                None,
            )
        };

        // The reopen reads object headers exactly as the reader does, so it
        // needs the same file-level parameters: a v2/v3 superblock carries no
        // B-tree K values, and only the extension can override the defaults.
        let (meta, ext) = crate::io::reader::Hdf5Reader::read_extension_and_meta(
            &mut handle,
            ctx,
            sb_btree,
            ext_addr,
        )?;

        // A file with shared object header messages keeps datatypes,
        // dataspaces and attributes in a fractal heap per index, and each
        // object header holds a heap ID pointing at one. The table is laid out
        // whole from the whole message set (`build_shared_messages`), never
        // grown insert by insert, so a reopen carries the indexes and the
        // bodies forward and the next finalize lays a new table out over the
        // old one's blocks — which is sound exactly while no header keeping
        // its bytes still points into the old heap. The walk below is what
        // settles that.
        let sohm = Self::reopen_shared_messages(&mut handle, &meta, &ext)?;

        // The extension is external truth this close rewrites, so what it held
        // is captured whole here — before anything else reads the file — and
        // re-emitted by `write_superblock_extension`. Read from the raw chain
        // rather than from `ext`, which keeps only the messages this crate
        // models.
        let extension = if ext_addr == UNDEF_ADDR || ext_addr == 0 {
            Box::<CarriedExtension>::default()
        } else {
            let (carried, blocks) = crate::io::object_header_io::superblock_extension_messages(
                &mut handle,
                &meta,
                ext_addr,
            )?;
            Box::new(CarriedExtension {
                superseded: blocks,
                carried,
                addr: Slot::new(None),
            })
        };

        // The managers that extension's file-space info message names, read
        // before anything allocates: the sections they hold are file space
        // this session may hand out, and the close rewrites them.
        let reopened_free_space = Self::reopen_free_space(&mut handle, &meta, &ext)?;

        // Discover links from root group (and subgroups recursively). Every
        // object is classified before it is registered, and the root is the
        // one object with no alternative: its header must be rewritten to
        // hold anything new, so an unmodellable root is refused here rather
        // than rewritten into whatever this writer could read of it.
        let mut walk = ReopenWalk::new(&mut handle, &meta);
        let root = match walk.plan(root_addr)? {
            ObjectPlan::Group(parts) => parts,
            ObjectPlan::Dataset(_) => {
                return Err(crate::io::IoError::InvalidState(
                    "cannot open this file for appending: its root object is a dataset, \
                     not a group"
                        .into(),
                ))
            }
            ObjectPlan::Preserve { why, .. } => {
                return Err(crate::io::IoError::Unsupported(format!(
                    "cannot open this file for appending: {why}. Every append rewrites the \
                     root group's header, and this writer will not rewrite it from the part \
                     of it that it can read"
                )));
            }
        };
        let root_header_blocks = root.header_blocks;
        let root_attributes = root.attributes;
        let root_track_order = root.track_order;
        let root_times = root.times;
        let root_dense = root.dense;
        let root_stab = root.stab;

        walk.group(&root.links, "", 0)?;
        let collected = walk.finish();
        let mut link_entries = collected.hard;
        let mut preserved = collected.preserved;
        // Objects the loop below could not rebuild, by header address, so the
        // other links to one are preserved with it rather than left pointing
        // at a registry entry that is no longer there.
        let mut unrebuilt: std::collections::HashMap<u64, String> = Default::default();

        // Two link entries can share one object header — hard links. Only
        // the first-walked path becomes the object; the rest are rebuilt
        // as hard-link registry entries further down. Without this split
        // every alias came back as its own DatasetInfo carrying the same
        // storage addresses, so deleting (or finalizing) one freed blocks
        // the others still referenced.
        let mut seen_header_addrs = std::collections::HashSet::new();
        let mut alias_entries: Vec<HardEntry> = Vec::new();
        link_entries.retain(|(entry, _)| {
            if seen_header_addrs.insert(entry.address) {
                true
            } else {
                alias_entries.push(entry.clone());
                false
            }
        });

        // The order the walk met each object, kept before the loop below
        // consumes the entries: `ensure_groups_for` needs parents to precede
        // children.
        let walk_order: Vec<String> = link_entries.iter().map(|(e, _)| e.path.clone()).collect();

        let mut existing_datasets = Vec::new();
        // Non-dataset link targets (groups): the header's chunk-0 address and
        // every block its chain occupies, by link path — so finalize can free
        // the blocks its rewrite supersedes — plus the attributes the header
        // carries, which the group registry below must keep or finalize
        // rewrites the group without them.
        type GroupHeaderInfo = (
            u64,
            crate::io::object_header_io::HeaderBlocks,
            Vec<AttributeEntry>,
            TrackOrder,
            Option<ObjectTimes>,
        );
        let mut group_headers: std::collections::HashMap<String, GroupHeaderInfo> =
            Default::default();
        // The dense storage each rebuilt dataset's header named, by registry
        // index, so finalize frees exactly what its rewrite supersedes. Keyed
        // after the rebuild succeeded: a preserved dataset keeps its header,
        // and freeing the heap that header still names would strand it.
        let mut dataset_dense: Vec<(usize, AttributeInfoMessage)> = Vec::new();
        let mut group_dense: Vec<(String, DenseCarry)> = Vec::new();
        // The same, for the symbol-table storage a classic group's header
        // names: keyed by path here, by registry index once every group has
        // one.
        let mut group_stabs: Vec<(String, StabExtents)> = Vec::new();
        for (entry, object) in link_entries {
            let HardEntry {
                path: name,
                address: obj_addr,
                encoded,
            } = entry;
            let parts = match object {
                CollectedObject::Group {
                    header_blocks,
                    attributes,
                    track_order,
                    times,
                    dense,
                    stab,
                } => {
                    group_dense.push((name.clone(), dense));
                    if let Some(stab) = stab {
                        group_stabs.push((name.clone(), stab));
                    }
                    group_headers.insert(
                        name,
                        (obj_addr, header_blocks, attributes, track_order, times),
                    );
                    continue;
                }
                CollectedObject::Dataset(parts) => *parts,
            };
            let dense_attrs = parts.dense.attrs.clone();
            match rebuild_dataset(&mut handle, &meta, file_size, name.clone(), obj_addr, parts) {
                Ok(info) => {
                    if let Some(ainfo) = dense_attrs {
                        dataset_dense.push((existing_datasets.len(), ainfo));
                    }
                    existing_datasets.push(info);
                }
                // Kept by its bytes for the same reason a header this walk
                // could not decode is: the rewrite would otherwise emit an
                // object whose chunk index no longer names its chunks.
                Err(e) => {
                    let why = format!("this writer could not rebuild its chunk index: {e}");
                    unrebuilt.insert(obj_addr, why.clone());
                    preserved.push(PreservedEntry {
                        path: name,
                        class: crate::io::reader::LinkClass::Hard,
                        encoded,
                        reason: Some(why),
                        // A dataset whose chunk index would not rebuild: the
                        // walk classified it, and it is not a datatype.
                        kind: PreservedKind::Unclassified,
                    });
                }
            }
        }

        // Reconstruct the group registry. Every group is a link entry of its
        // own, whether or not a dataset lives under it, so the registry is
        // built from the discovered links — rebuilding it from dataset paths
        // alone made attribute-only and empty groups vanish at close, and
        // dropped the attributes of the groups that survived.
        let mut groups: Vec<GroupInfo> = Vec::new();
        let mut group_index_map: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();

        // Register the chain of groups "/a", "/a/b", … for the link-style
        // path `link_path` ("a/b"), taking each one's on-disk header block
        // and attributes out of `group_headers` when the link walk saw it.
        fn ensure_groups_for(
            link_path: &str,
            groups: &mut Vec<GroupInfo>,
            group_index_map: &mut std::collections::HashMap<String, usize>,
            group_headers: &mut std::collections::HashMap<String, GroupHeaderInfo>,
        ) {
            let mut path = String::new();
            for part in link_path.split('/') {
                let parent_path = if path.is_empty() {
                    "/".to_string()
                } else {
                    path.clone()
                };
                if path.is_empty() {
                    path = format!("/{}", part);
                } else {
                    path = format!("{}/{}", path, part);
                }
                if group_index_map.contains_key(&path) {
                    continue;
                }
                let parent = if parent_path == "/" {
                    None
                } else {
                    group_index_map.get(&parent_path).copied()
                };
                let gidx = groups.len();
                let (obj_header_written_addr, obj_header_blocks, attributes, track_order, times) =
                    group_headers.remove(path.trim_start_matches('/')).map_or(
                        (None, Vec::new(), Vec::new(), TrackOrder::default(), None),
                        |(addr, blocks, attrs, track, times)| {
                            (Some(addr), blocks, attrs, track, times)
                        },
                    );
                groups.push(GroupInfo {
                    name: path.clone(),
                    parent,
                    creation_seq: 0,
                    track_order,
                    times,
                    child_datasets: Vec::new(),
                    child_groups: Vec::new(),
                    obj_header_addr: 0,
                    obj_header_written_addr,
                    obj_header_blocks,
                    deleted: false,
                    attributes,
                });
                if let Some(pidx) = parent {
                    groups[pidx].child_groups.push(gidx);
                }
                group_index_map.insert(path.clone(), gidx);
            }
        }

        // Every linked group, in link-walk order (parents precede children).
        for name in &walk_order {
            if group_headers.contains_key(name.as_str()) {
                ensure_groups_for(name, &mut groups, &mut group_index_map, &mut group_headers);
            }
        }

        // Assign each dataset to its immediate parent group, creating any
        // group the link walk could not decode (its chain stays placeholder).
        for (di, ds) in existing_datasets.iter().enumerate() {
            let parts: Vec<&str> = ds.name.split('/').collect();
            if parts.len() <= 1 {
                continue; // root-level dataset, no group
            }
            let parent_link_path = parts[..parts.len() - 1].join("/");
            ensure_groups_for(
                &parent_link_path,
                &mut groups,
                &mut group_index_map,
                &mut group_headers,
            );
            let gidx = group_index_map[&format!("/{}", parent_link_path)];
            groups[gidx].child_datasets.push(di);
        }

        // An object the rebuild above gave up on is preserved by its bytes,
        // so the other links to it are preserved too: there is no registry
        // entry for them to name.
        alias_entries.retain(|entry| match unrebuilt.get(&entry.address) {
            None => true,
            Some(why) => {
                preserved.push(PreservedEntry {
                    path: entry.path.clone(),
                    class: crate::io::reader::LinkClass::Hard,
                    encoded: entry.encoded.clone(),
                    reason: Some(why.clone()),
                    kind: PreservedKind::Unclassified,
                });
                false
            }
        });

        // The one thing a rebuilt shared-message table can break: an object
        // kept by its bytes keeps the heap IDs its header holds, and the
        // finalize gives the heap those IDs name back to the allocator. Every
        // object the registry holds is rewritten instead
        // ([`rebuilds_shared_messages`](Self::rebuilds_shared_messages)), so
        // this asks only the preserved ones, and names the object rather than
        // the feature — the file is appendable the moment nothing preserved
        // holds a heap ID or hides a subtree that might.
        if sohm.is_some() {
            for entry in &preserved {
                if !matches!(entry.class, crate::io::reader::LinkClass::Hard) {
                    continue;
                }
                let Ok((link, _)) = LinkMessage::decode(&entry.encoded, &meta.ctx) else {
                    continue;
                };
                let LinkTarget::Hard { address } = link.target else {
                    continue;
                };
                if let Some(blocks) = crate::io::object_header_io::blocks_shared_message_rebuild(
                    &mut handle,
                    &meta,
                    address,
                )? {
                    let why = entry
                        .reason
                        .as_deref()
                        .unwrap_or("this writer cannot model it");
                    return Err(crate::io::IoError::Unsupported(format!(
                        "cannot open this file for appending: '{}' {blocks}, but {why}, so \
                         its header keeps the bytes it has while the append lays the \
                         shared-message table out afresh",
                        entry.path
                    )));
                }
            }
        }

        // Rebuild the hard-link registry from the alias entries set aside
        // above, so the H5Ldelete semantics survive a reopen. An alias whose
        // target the walk could not model is not here at all: it was
        // preserved by its own bytes, exactly as the first link to that
        // object was.
        let mut hard_links: Vec<HardLink> = Vec::new();
        for HardEntry {
            path,
            address: addr,
            ..
        } in alias_entries
        {
            let target = if let Some(di) = existing_datasets
                .iter()
                .position(|d| d.obj_header_addr == addr)
            {
                HardLinkTarget::Dataset(di)
            } else if let Some(gi) = groups
                .iter()
                .position(|g| g.obj_header_written_addr == Some(addr))
            {
                HardLinkTarget::Group(gi)
            } else {
                continue;
            };
            let (parent, link_name) = match path.rsplit_once('/') {
                None => (None, path),
                Some((dir, leaf)) => {
                    ensure_groups_for(dir, &mut groups, &mut group_index_map, &mut group_headers);
                    (
                        group_index_map.get(&format!("/{dir}")).copied(),
                        leaf.to_string(),
                    )
                }
            };
            hard_links.push(HardLink {
                parent,
                name: link_name,
                target,
                creation_seq: 0,
            });
        }

        // Attach every link the writer cannot express to the group that
        // holds it, so the rewrite of that group's header emits it again.
        // `ensure_groups_for` registers the parent chain, which matters for
        // a group whose only content is such a link: nothing else would put
        // it in the registry, and the close would drop group and link alike.
        let mut preserved_links: Vec<PreservedLink> = Vec::new();
        for PreservedEntry {
            path,
            class,
            encoded,
            reason,
            kind,
        } in preserved
        {
            let (parent, link_name) = match path.rsplit_once('/') {
                None => (None, path),
                Some((dir, leaf)) => {
                    ensure_groups_for(dir, &mut groups, &mut group_index_map, &mut group_headers);
                    (
                        group_index_map.get(&format!("/{dir}")).copied(),
                        leaf.to_string(),
                    )
                }
            };
            preserved_links.push(PreservedLink {
                parent,
                name: link_name,
                class,
                encoded,
                reason,
                kind,
            });
        }

        // Stamp the creation sequence a reopened file cannot supply. Nothing
        // on disk says which link was made first unless the group tracked
        // creation order, and this reader does not carry that back out, so
        // discovery order is what there is: datasets, then groups, then the
        // hard links found beside them — the order the writer emitted links
        // in before it ordered them at all.
        let mut creation_seq = 0u64;
        for d in &mut existing_datasets {
            d.creation_seq = creation_seq;
            creation_seq += 1;
        }
        for g in &mut groups {
            g.creation_seq = creation_seq;
            creation_seq += 1;
        }
        for l in &mut hard_links {
            l.creation_seq = creation_seq;
            creation_seq += 1;
        }

        // The strategy is the file's, not this session's: a paged file
        // allocates on its own page grid however it was opened, `persist`
        // deciding only whether the managers survive the close.
        let allocator = FileAllocator::with_policy(
            file_size,
            ext.file_space_info
                .as_ref()
                .map_or(free_space::SpacePolicy::Aggr, |info| {
                    free_space::SpacePolicy::for_message(info)
                }),
        );
        // The sections the file's own managers recorded are free space, so
        // they are what this session allocates from first — `H5MF_alloc` asks
        // the free-space manager before it bumps the end of the file, and a
        // reopen that skipped this would grow a file that had room.
        allocator.reset_free_list(&reopened_free_space.sections);

        // Now that every object has its registry index, key the dense storage
        // found on disk by the scope that will supersede it. A group the link
        // walk saw but never registered is not rewritten either, so leaving it
        // out is what keeps its storage referenced.
        let mut superseded = SupersededDense {
            attrs: dataset_dense
                .into_iter()
                .map(|(di, ainfo)| (AttrScope::Dataset(di), ainfo))
                .collect(),
            links: HashMap::new(),
        };
        superseded
            .attrs
            .extend(root_dense.attrs.map(|a| (AttrScope::Root, a)));
        superseded
            .links
            .extend(root_dense.links.map(|l| (LinkScope::Root, l)));
        for (name, dense) in group_dense {
            let Some(&gidx) = group_index_map.get(&format!("/{name}")) else {
                continue;
            };
            superseded
                .attrs
                .extend(dense.attrs.map(|a| (AttrScope::Group(gidx), a)));
            superseded
                .links
                .extend(dense.links.map(|l| (LinkScope::Group(gidx), l)));
        }
        let superseded = (!superseded.attrs.is_empty() || !superseded.links.is_empty())
            .then(|| Box::new(superseded));

        // The same keying for the symbol-table storage. Built from the headers
        // alone, not from the superblock version: a group whose header carried
        // no Symbol Table message contributes nothing — what happens to a group
        // libhdf5 wrote at a newer bound inside an otherwise classic file — and
        // one that carried it keeps its storage even where the superblock is
        // version 2, which is what a file with shared messages is.
        let mut stabs: HashMap<LinkScope, StabExtents> = HashMap::new();
        stabs.extend(root_stab.map(|s| (LinkScope::Root, s)));
        for (name, extents) in group_stabs {
            if let Some(&gidx) = group_index_map.get(&format!("/{name}")) {
                stabs.insert(LinkScope::Group(gidx), extents);
            }
        }
        let symbol_tables = SymbolTables {
            found: stabs.keys().copied().collect(),
            superseded: Slot::new(stabs),
            written: Slot::new(HashMap::new()),
        };

        // The superblock the close re-emits, and the generation every message
        // this session encodes belongs to.
        let legacy = legacy.map(|superblock| Box::new(LegacyFile { superblock }));

        // Wrap the reconstructed plain vecs into the per-slot registry. The
        // reconstruction logic above runs single-threaded on local `Vec`s;
        // only the final hand-off needs the `Shared<Slot<_>>` shape.
        let datasets = existing_datasets
            .into_iter()
            .map(|i| Shared::new(DatasetCell::new(i)))
            .collect();
        let groups = groups
            .into_iter()
            .map(|g| Shared::new(Slot::new(g)))
            .collect();

        let writer = Self {
            handle,
            allocator,
            ctx,
            datasets: Slot::new(datasets),
            groups: Slot::new(groups),
            hard_links: Slot::new(hard_links),
            // A reopen carries the soft and external links it found as
            // `preserved_links`, byte for byte; this list holds only the ones
            // created in this session.
            symbolic_links: Slot::new(Vec::new()),
            committed_datatypes: Slot::new(Vec::new()),
            preserved_links: Slot::new(preserved_links),
            name_index: Slot::new(Box::new(NameIndex::new())),
            root_attributes: Slot::new(root_attributes),
            create_lock: Slot::new(()),
            // A reopen names no bound, so the session appends at the same
            // default a create uses. `set_libver_bound` is where a caller
            // names one, exactly as `H5Fopen` takes a fapl.
            libver: None,
            closed: false,
            swmr_active: false,
            cwfs: Slot::new(Vec::new()),
            root_group_addr: None,
            superseded_root_header: root_header_blocks,
            // The version the file already has, written back unchanged.
            superblock_version: SuperblockVersion::Existing(version),
            // The reopened file's own policy, so objects added in this
            // session are made the way the file already declares.
            root_track_order,
            root_times,
            dense_attributes: Slot::new(HashMap::new()),
            dense_links: Slot::new(HashMap::new()),
            superseded_dense: Slot::new(superseded),
            track_order: root_track_order,
            // Not recovered from the file the way the creation-order policy
            // is: a version-1 header leaves no trace of whether the object was
            // tracking times, so there is nothing on disk to read the policy
            // back from. An object added to a reopened file gets this writer's
            // own default, the same one a created file starts at.
            track_times: false,
            next_creation_seq: Slot::new(creation_seq),
            pending_object_references: Slot::new(Vec::new()),
            pending_heap_references: Slot::new(Vec::new()),
            attribute_references: Slot::new(Vec::new()),
            legacy,
            symbol_tables,
            // The ranks the superblock or its extension declared, which every
            // v1-B-tree and symbol-table node this session writes is sized by.
            btree: meta.btree,
            extension,
            free_space: reopened_free_space.state,
            // The indexes the file was created with, and the blocks its
            // current table occupies; the next finalize lays a new table out
            // over them from the whole message set.
            sohm,
            source_dir: source_dir_of(path)?,
        };
        // The link graph is complete only now, so this is the first point the
        // count each on-disk header was written with can be read off it: in a
        // well-formed file the links the walk found reaching an object *are*
        // that count, so nothing has to be decoded out of the headers.
        for i in 0..writer.dataset_count() {
            let nlink = writer.object_link_count(HardLinkTarget::Dataset(i));
            writer.ds(i).lock().nlink_written = nlink;
        }
        Ok(writer)
    }

    /// Return the names of all datasets created so far.
    pub fn dataset_names(&self) -> Vec<String> {
        self.dataset_refs()
            .iter()
            .filter_map(|d| {
                let g = d.lock();
                (!g.deleted).then(|| g.name.clone())
            })
            .collect()
    }

    /// Find a dataset index by name. Like `H5Dopen`, the name may be any
    /// link path to the dataset: a user hard link's path — or a path
    /// whose group components pass through such links — resolves to its
    /// target.
    pub fn dataset_index(&self, name: &str) -> Option<usize> {
        let name = self.canonical_dataset_path(name);
        self.dataset_refs()
            .iter()
            .position(|d| {
                let g = d.lock();
                g.name == name && !g.deleted
            })
            .or_else(|| {
                self.hard_links_vec().iter().find_map(|l| match l.target {
                    HardLinkTarget::Dataset(i)
                        if self.hard_link_emitted(l) && self.hard_link_full_path(l) == name =>
                    {
                        Some(i)
                    }
                    _ => None,
                })
            })
    }

    /// Reconstruct the fields a writer-mode `H5Dataset` handle needs for the
    /// dataset at `index`, and open it under `access`. Single owner of this
    /// mapping so `H5File::dataset_writer`, `H5Group::dataset_writer`, and
    /// the vlen-string helpers all agree — including on
    /// [`bind_efile_prefix`](Self::bind_efile_prefix), which no handle site
    /// can then forget to run.
    pub(crate) fn dataset_handle_parts(
        &self,
        index: usize,
        access: &DatasetAccess,
    ) -> IoResult<DatasetHandleParts> {
        let open = self.bind_efile_prefix(index, access)?;
        let ds = self.ds(index);
        let g = ds.lock();
        Ok(DatasetHandleParts {
            shape: g.dataspace.dims.iter().map(|&d| d as usize).collect(),
            element_size: g.datatype.element_size() as usize,
            chunk_index: g.chunk_index_kind(),
            open,
        })
    }

    /// Put `access`'s external file prefix in force for the dataset at
    /// `index`, or join the open that already settled one.
    ///
    /// INVARIANT: every write of an externally stored dataset's raw bytes
    /// joins its slot names against the prefix an *open* settled, and this is
    /// the only place that settles one. `write_contiguous_bytes` reads it and
    /// nothing else writes it, so a write cannot resolve a prefix of its own
    /// and land bytes where a read under the same properties would not look
    /// for them.
    ///
    /// First open wins, and a joining open may not disagree: `H5D__open_name`
    /// compares its own expanded prefix against the open dataset's and fails
    /// when they differ (H5Dint.c:1533-1545). Measured under libhdf5 1.14.6
    /// and 2.0.0, with a dataset created through a dapl naming a directory
    /// and its handle still alive: a second open naming another directory is
    /// refused, one naming the same directory joins, one naming none is
    /// refused too, and with `HDF5_EXTFILE_PREFIX` set — which shadows every
    /// property, so all three expand alike — none of them is. Dropping every
    /// handle releases the answer and the next open settles it afresh, which
    /// the same measurement confirms.
    ///
    /// Returns the token that keeps the open alive, `None` for a dataset
    /// whose raw data is in this file and which therefore has no prefix to
    /// agree about.
    pub(crate) fn bind_efile_prefix(
        &self,
        index: usize,
        access: &DatasetAccess,
    ) -> IoResult<Option<crate::io::reader::DatasetOpenToken>> {
        let ds = self.ds(index);
        let mut g = ds.lock();
        let source_dir = &self.source_dir;
        let Some(ext) = g.external.as_mut() else {
            return Ok(None);
        };
        let want =
            crate::io::reader::resolve_extfile_prefix(access.efile_prefix_value(), source_dir);
        if let Some(open) = ext.prefix.open.upgrade() {
            if ext.prefix.expanded != want {
                let name = g.name.clone();
                return Err(crate::io::IoError::InvalidState(format!(
                    "dataset {name:?} is already open under a different external file                      prefix, and libhdf5 refuses to join an open that disagrees about one"
                )));
            }
            return Ok(Some(open));
        }
        let token: crate::io::reader::DatasetOpenToken = std::sync::Arc::new(());
        ext.prefix = EfilePrefix {
            expanded: want,
            open: std::sync::Arc::downgrade(&token),
        };
        Ok(Some(token))
    }

    /// Reject a name some other link in the file already occupies.
    ///
    /// `name` is the registry's full-path form, with no leading `/`. HDF5
    /// requires link names to be unique within their group, and every kind of
    /// link this writer can emit competes for the same name: a dataset's own
    /// link, a group's, a user hard link, a soft or external link, and a link
    /// a reopen is carrying through verbatim. This is the one place that list
    /// is written down, so a creator cannot be blind to a kind it does not
    /// itself make — nor a kind added after it.
    fn ensure_name_free(&self, name: &str) -> IoResult<()> {
        let holder = self.name_holder(name);
        // The index is a filter over the registries, not a second copy of
        // them, so a debug build re-derives the answer on every create: a
        // name it failed to record surfaces as a failing assertion in the
        // suite rather than as two links of one name in somebody's file.
        #[cfg(debug_assertions)]
        assert_eq!(
            holder,
            self.scan_name_holder(name),
            "the name index disagrees with the registries for '{name}'"
        );
        match holder {
            None => Ok(()),
            Some(kind) => Err(crate::io::IoError::InvalidState(format!(
                "a {kind} named '{name}' already exists"
            ))),
        }
    }

    /// What already holds `name`, or `None` if it is free.
    ///
    /// The kinds answer in a fixed order — dataset, group, committed
    /// datatype, hard link, symbolic link, preserved link — because the
    /// refusal names the first one that holds it. [`NameIndex`] narrows each
    /// kind to the entries that ever took this name; every candidate is then
    /// put through the same predicate the full scan used, so a hit left
    /// behind by a delete or a rename answers exactly as an absent one does.
    fn name_holder(&self, name: &str) -> Option<&'static str> {
        self.build_name_index();
        let hits: Vec<NameHit> = {
            let index = self.name_index.lock();
            index.map.as_ref().and_then(|m| m.get(name))?.clone()
        };
        for hit in &hits {
            if let NameHit::Dataset(i) = *hit {
                let ds = self.ds(i);
                let d = ds.lock();
                if !d.deleted && d.name == name {
                    return Some("dataset");
                }
            }
        }
        for hit in &hits {
            if let NameHit::Group(i) = *hit {
                let grp = self.grp(i);
                let g = grp.lock();
                if !g.deleted && g.name.trim_start_matches('/') == name {
                    return Some("group");
                }
            }
        }
        for hit in &hits {
            if let NameHit::Datatype(i) = *hit {
                // The registry lock goes before `parent_alive` takes a group
                // slot, never across it.
                let (parent, held) = {
                    let reg = self.committed_datatypes.lock();
                    (reg[i].parent, reg[i].name == name)
                };
                if held && self.parent_alive(parent) {
                    return Some("committed datatype");
                }
            }
        }
        if hits.contains(&NameHit::HardLink)
            && self
                .hard_links_vec()
                .iter()
                .any(|l| self.hard_link_emitted(l) && self.hard_link_full_path(l) == name)
        {
            return Some("hard link");
        }
        if hits.contains(&NameHit::SymbolicLink)
            && self
                .symbolic_links_vec()
                .iter()
                .any(|l| self.symbolic_link_emitted(l) && self.symbolic_link_full_path(l) == name)
        {
            return Some("link");
        }
        // A preserved link occupies its name in the group just as a modelled
        // one does; both are emitted, and two link messages of one name in a
        // group is an invalid file.
        if hits.contains(&NameHit::PreservedLink)
            && self.preserved_link_paths().iter().any(|(p, _)| *p == name)
        {
            return Some("link");
        }
        None
    }

    /// The same answer read straight off the registries, which is what the
    /// index is checked against in a debug build.
    #[cfg(debug_assertions)]
    fn scan_name_holder(&self, name: &str) -> Option<&'static str> {
        if self.dataset_refs().iter().any(|d| {
            let g = d.lock();
            !g.deleted && g.name == name
        }) {
            return Some("dataset");
        }
        if self.group_refs().iter().any(|g| {
            let gg = g.lock();
            !gg.deleted && gg.name.trim_start_matches('/') == name
        }) {
            return Some("group");
        }
        if self
            .committed_datatypes_vec()
            .iter()
            .any(|c| self.parent_alive(c.parent) && c.name == name)
        {
            return Some("committed datatype");
        }
        if self
            .hard_links_vec()
            .iter()
            .any(|l| self.hard_link_emitted(l) && self.hard_link_full_path(l) == name)
        {
            return Some("hard link");
        }
        if self
            .symbolic_links_vec()
            .iter()
            .any(|l| self.symbolic_link_emitted(l) && self.symbolic_link_full_path(l) == name)
        {
            return Some("link");
        }
        if self.preserved_link_paths().iter().any(|(p, _)| *p == name) {
            return Some("link");
        }
        None
    }

    /// Build the name index unless it is already built.
    ///
    /// The walk takes the registry spines and their slots, so it runs with no
    /// index lock held — the writer never holds one lock across another — and
    /// the result is kept only if nothing renamed, created or unlinked
    /// anything while it ran.
    fn build_name_index(&self) {
        let epoch = {
            let index = self.name_index.lock();
            if index.map.is_some() {
                return;
            }
            index.epoch
        };
        let mut map: HashMap<String, Vec<NameHit>> = HashMap::new();
        for (i, ds) in self.dataset_refs().iter().enumerate() {
            let d = ds.lock();
            if !d.deleted {
                map.entry(d.name.clone())
                    .or_default()
                    .push(NameHit::Dataset(i));
            }
        }
        for (i, grp) in self.group_refs().iter().enumerate() {
            let g = grp.lock();
            if !g.deleted {
                map.entry(g.name.trim_start_matches('/').to_string())
                    .or_default()
                    .push(NameHit::Group(i));
            }
        }
        for (i, c) in self.committed_datatypes_vec().iter().enumerate() {
            map.entry(c.name.clone())
                .or_default()
                .push(NameHit::Datatype(i));
        }
        for l in self.hard_links_vec().iter() {
            map.entry(self.hard_link_full_path(l))
                .or_default()
                .push(NameHit::HardLink);
        }
        for l in self.symbolic_links_vec().iter() {
            map.entry(self.symbolic_link_full_path(l))
                .or_default()
                .push(NameHit::SymbolicLink);
        }
        for (path, _) in self.preserved_link_paths() {
            map.entry(path).or_default().push(NameHit::PreservedLink);
        }
        let mut index = self.name_index.lock();
        if index.map.is_none() && index.epoch == epoch {
            index.map = Some(map);
        }
    }

    /// Record that `hit` now holds `name` — the one way a new name enters the
    /// index, called from every push that gives a registry entry a name.
    fn register_name(&self, name: &str, hit: NameHit) {
        self.name_index.lock().insert(name, hit);
    }

    /// Drop the index because something moved names wholesale (a group
    /// rename carries its subtree and every link path under it).
    fn forget_name_index(&self) {
        self.name_index.lock().forget();
    }

    /// Delete a dataset name, with libhdf5's `H5Ldelete` semantics: a name
    /// is only a link. If `name` is a user hard link's path, just that
    /// link is removed and the object is untouched. If it is the tree name
    /// and a user hard link still names the object, the object survives
    /// under it — the link becomes the primary name and nothing is freed.
    /// Only deleting the *last* name soft-deletes the object and frees the
    /// file space it owned: its chunk blocks and chunk-index structures
    /// (or contiguous data block), the global-heap objects of its
    /// variable-length data and attributes, and — on a reopened file — the
    /// on-disk object header block. The freed space is reused by later
    /// allocations in this session; the file does not shrink.
    ///
    /// Refused while SWMR streaming is active: a live reader may hold any
    /// of those addresses (libhdf5 forbids link deletion during SWMR
    /// writes too).
    pub fn delete_dataset(&self, name: &str) -> IoResult<()> {
        if self.swmr_active {
            return Err(swmr_delete_error(name));
        }
        self.reject_external_traversal(name)?;
        // The gate keeps the link list and child lists still while this
        // delete reads and rewrites them (create_lock → op → slot order,
        // the same as every creator).
        let _create = self.create_lock.lock();
        // `H5Ldelete` resolves the path through links only *up to* the
        // leaf — the leaf is what gets deleted, so a leaf naming a user
        // link must stay literal and be unlinked, not its target.
        let name = match name.rsplit_once('/') {
            None => name.to_string(),
            Some((dir, leaf)) => format!(
                "{}/{leaf}",
                self.canonical_group_path(&format!("/{dir}"))
                    .trim_start_matches('/')
            ),
        };
        let refs = self.dataset_refs();
        let idx = match refs.iter().position(|d| {
            let g = d.lock();
            g.name == name && !g.deleted
        }) {
            Some(i) => i,
            None => {
                // Not a tree name — the path may name a user hard link,
                // and deleting a link path unlinks just that link (the
                // creation collision checks keep the two namespaces
                // disjoint, so the order of the lookups cannot matter).
                let link = self.hard_links_vec().iter().position(|l| {
                    self.hard_link_emitted(l)
                        && matches!(l.target, HardLinkTarget::Dataset(_))
                        && self.hard_link_full_path(l) == name
                });
                let Some(pos) = link else {
                    return Err(crate::io::IoError::NotFound(name));
                };
                self.hard_links.lock().remove(pos);
                return Ok(());
            }
        };
        // A surviving hard link keeps the object: promote the first one to
        // the primary name and delete nothing.
        let promote = self.hard_links_vec().iter().position(|l| {
            self.hard_link_emitted(l) && matches!(l.target, HardLinkTarget::Dataset(i) if i == idx)
        });
        if let Some(pos) = promote {
            self.promote_dataset_to_link(idx, pos);
            return Ok(());
        }
        refs[idx].lock().deleted = true;
        // Remove from parent group's child_datasets
        for grp in self.group_refs() {
            grp.lock().child_datasets.retain(|&di| di != idx);
        }
        self.purge_dead_links();
        let ds = self.ds(idx);
        let _op = ds.op.lock();
        self.release_dataset_storage(idx)
    }

    /// Soft-delete a group and all its child datasets and sub-groups,
    /// freeing every deleted object's file space the way
    /// [`delete_dataset`](Self::delete_dataset) does — with the same
    /// `H5Ldelete` semantics: a `name` that is a user hard link's path
    /// unlinks just that link, and hard links from *outside* the subtree
    /// keep their targets. A dataset or group such a link names survives,
    /// re-homed under the link (a group brings its whole subtree with
    /// it); a link naming the deleted group itself turns the call into a
    /// pure rename and nothing is freed. Refused while SWMR streaming is
    /// active, same rule as `delete_dataset`.
    pub fn delete_group(&self, name: &str) -> IoResult<()> {
        if self.swmr_active {
            return Err(swmr_delete_error(name));
        }
        self.reject_external_traversal(name)?;
        // Same gate as `delete_dataset`: the pre-scan below and the
        // promotions must see a still link list and child lists.
        let _create = self.create_lock.lock();
        let name = if name.starts_with('/') {
            name.to_string()
        } else {
            format!("/{}", name)
        };
        // Leaf stays literal, directory resolves through links — the
        // same `H5Ldelete` rule as `delete_dataset`.
        let name = match name.rsplit_once('/') {
            Some((dir, leaf)) if !dir.is_empty() => {
                format!("{}/{leaf}", self.canonical_group_path(dir))
            }
            _ => name,
        };
        let groups = self.group_refs();
        let gidx = match groups.iter().position(|g| {
            let gg = g.lock();
            gg.name == name && !gg.deleted
        }) {
            Some(i) => i,
            None => {
                // Same `H5Ldelete` rule as `delete_dataset`: a path naming
                // a user hard link to a group unlinks just that link.
                let trimmed = name.trim_start_matches('/');
                let link = self.hard_links_vec().iter().position(|l| {
                    self.hard_link_emitted(l)
                        && matches!(l.target, HardLinkTarget::Group(_))
                        && self.hard_link_full_path(l) == trimmed
                });
                let Some(pos) = link else {
                    return Err(crate::io::IoError::NotFound(name.clone()));
                };
                self.hard_links.lock().remove(pos);
                return Ok(());
            }
        };

        // A link is "outside" when its parent group does not die with the
        // subtree; only outside links can keep their targets alive.
        fn outside(parent: Option<usize>, doomed_gs: &[usize]) -> bool {
            match parent {
                None => true,
                Some(pi) => !doomed_gs.contains(&pi),
            }
        }
        // A group an outside link names survives, re-homed with its whole
        // subtree under the link. Each promotion moves that subtree out of
        // the doomed set — and can turn a link inside it into an outside
        // one — so rescan from scratch until no promotable group is left.
        // Promoting `gidx` itself makes the delete a pure rename: return.
        let mut doomed_ds = Vec::new();
        let mut doomed_gs = Vec::new();
        loop {
            doomed_ds.clear();
            doomed_gs.clear();
            self.collect_live_subtree(gidx, &mut doomed_ds, &mut doomed_gs);
            let promote = self
                .hard_links_vec()
                .iter()
                .enumerate()
                .find_map(|(pos, l)| match l.target {
                    HardLinkTarget::Group(gi)
                        if self.hard_link_emitted(l)
                            && outside(l.parent, &doomed_gs)
                            && doomed_gs.contains(&gi) =>
                    {
                        Some((pos, gi))
                    }
                    _ => None,
                });
            let Some((pos, gi)) = promote else { break };
            self.promote_group_to_link(gi, pos);
            if gi == gidx {
                return Ok(());
            }
        }
        // A dataset an outside link names survives its container: re-home
        // it under the link now, so the marking pass below never sees it.
        for di in doomed_ds {
            let promote = self.hard_links_vec().iter().position(|l| {
                self.hard_link_emitted(l)
                    && outside(l.parent, &doomed_gs)
                    && matches!(l.target, HardLinkTarget::Dataset(i) if i == di)
            });
            if let Some(pos) = promote {
                self.promote_dataset_to_link(di, pos);
            }
        }

        let mut ds_deleted = Vec::new();
        let mut gs_deleted = Vec::new();
        self.delete_group_recursive(gidx, &mut ds_deleted, &mut gs_deleted);
        // Remove from parent's child_groups
        let parent = groups[gidx].lock().parent;
        if let Some(pidx) = parent {
            groups[pidx].lock().child_groups.retain(|&gi| gi != gidx);
        }
        self.purge_dead_links();
        // Free storage only after the whole subtree is marked: the lists
        // hold each object exactly once (the marking pass skips anything
        // already deleted), so nothing is freed twice.
        for di in ds_deleted {
            let ds = self.ds(di);
            let _op = ds.op.lock();
            self.release_dataset_storage(di)?;
        }
        for gi in gs_deleted {
            self.release_group_storage(gi)?;
        }
        Ok(())
    }

    /// Collect the live (not soft-deleted) members of `gidx`'s subtree,
    /// each exactly once, without changing anything — the read-only twin
    /// of [`delete_group_recursive`](Self::delete_group_recursive), for
    /// the pre-scan that must run before any marking.
    fn collect_live_subtree(&self, gidx: usize, ds_out: &mut Vec<usize>, gs_out: &mut Vec<usize>) {
        if gs_out.contains(&gidx) {
            return;
        }
        let (child_ds, child_gs) = {
            let grp = self.grp(gidx);
            let g = grp.lock();
            if g.deleted {
                return;
            }
            (g.child_datasets.clone(), g.child_groups.clone())
        };
        gs_out.push(gidx);
        for di in child_ds {
            if !self.ds(di).lock().deleted && !ds_out.contains(&di) {
                ds_out.push(di);
            }
        }
        for gi in child_gs {
            self.collect_live_subtree(gi, ds_out, gs_out);
        }
    }

    /// Re-home dataset `idx` under the hard link at `pos` in the link
    /// list — the surviving half of `H5Ldelete`: the link leaves the user
    /// list and becomes the dataset's primary (tree) name, in the link's
    /// parent group. Storage is untouched; any further links to the
    /// dataset stay in the list and keep resolving.
    fn promote_dataset_to_link(&self, idx: usize, pos: usize) {
        let link = self.hard_links.lock().remove(pos);
        let new_name = self.hard_link_full_path(&link);
        for grp in self.group_refs() {
            grp.lock().child_datasets.retain(|&di| di != idx);
        }
        if let Some(pi) = link.parent {
            self.grp(pi).lock().child_datasets.push(idx);
        }
        self.ds(idx).lock().name = new_name.clone();
        self.register_name(&new_name, NameHit::Dataset(idx));
    }

    /// The group counterpart of
    /// [`promote_dataset_to_link`](Self::promote_dataset_to_link): re-home
    /// group `gidx` under the hard link at `pos`, bringing its whole
    /// subtree with it. Names are stored as full paths, so every live
    /// descendant is renamed by prefix.
    fn promote_group_to_link(&self, gidx: usize, pos: usize) {
        let link = self.hard_links.lock().remove(pos);
        let new_name = format!("/{}", self.hard_link_full_path(&link));
        let old_name = self.grp(gidx).lock().name.clone();
        for grp in self.group_refs() {
            grp.lock().child_groups.retain(|&g| g != gidx);
        }
        {
            let grp = self.grp(gidx);
            let mut g = grp.lock();
            g.parent = link.parent;
            g.name = new_name.clone();
        }
        if let Some(pi) = link.parent {
            self.grp(pi).lock().child_groups.push(gidx);
        }

        let mut ds_in = Vec::new();
        let mut gs_in = Vec::new();
        self.collect_live_subtree(gidx, &mut ds_in, &mut gs_in);
        // Group names carry a leading '/' ("/a/b"), dataset names none
        // ("a/b/ds") — two prefix forms of the same rename.
        let old_grp_prefix = format!("{old_name}/");
        let new_grp_prefix = format!("{new_name}/");
        let old_ds_prefix = old_grp_prefix.trim_start_matches('/').to_string();
        let new_ds_prefix = new_grp_prefix.trim_start_matches('/').to_string();
        for gi in gs_in {
            if gi == gidx {
                continue;
            }
            let grp = self.grp(gi);
            let mut g = grp.lock();
            let renamed = g
                .name
                .strip_prefix(&old_grp_prefix)
                .map(|rest| format!("{new_grp_prefix}{rest}"));
            if let Some(n) = renamed {
                g.name = n;
            }
        }
        for di in ds_in {
            let ds = self.ds(di);
            let mut d = ds.lock();
            let renamed = d
                .name
                .strip_prefix(&old_ds_prefix)
                .map(|rest| format!("{new_ds_prefix}{rest}"));
            if let Some(n) = renamed {
                d.name = n;
            }
        }
        // A group carries its subtree and every link path under it, so far
        // more names moved than this function can enumerate: start over.
        self.forget_name_index();
    }

    /// Drop link entries that can no longer be emitted — their parent group
    /// or, for a hard link, their target object was just deleted — so the
    /// lists mirror what the file will hold instead of carrying suppressed
    /// zombies. Both kinds are purged here so a delete cannot clear one list
    /// and leave the other holding a name in a group that is gone.
    fn purge_dead_links(&self) {
        let dead: Vec<usize> = self
            .hard_links_vec()
            .iter()
            .enumerate()
            .filter(|(_, l)| !self.hard_link_emitted(l))
            .map(|(p, _)| p)
            .collect();
        let mut links = self.hard_links.lock();
        for p in dead.into_iter().rev() {
            links.remove(p);
        }
        drop(links);

        let dead: Vec<usize> = self
            .symbolic_links_vec()
            .iter()
            .enumerate()
            .filter(|(_, l)| !self.symbolic_link_emitted(l))
            .map(|(p, _)| p)
            .collect();
        let mut links = self.symbolic_links.lock();
        for p in dead.into_iter().rev() {
            links.remove(p);
        }
    }

    /// Mark `gidx` and its subtree deleted, appending each newly-deleted
    /// object's index to `ds_out` / `gs_out` exactly once — the caller
    /// frees their storage, and an object reachable twice (or a subtree
    /// already deleted) must not be freed twice.
    fn delete_group_recursive(
        &self,
        gidx: usize,
        ds_out: &mut Vec<usize>,
        gs_out: &mut Vec<usize>,
    ) {
        // Mark deleted and snapshot the child lists, releasing the group lock
        // before locking any dataset/child-group slot (spine → slot order).
        let (child_ds, child_gs) = {
            let grp = self.grp(gidx);
            let mut g = grp.lock();
            if g.deleted {
                return;
            }
            g.deleted = true;
            (g.child_datasets.clone(), g.child_groups.clone())
        };
        gs_out.push(gidx);
        for di in child_ds {
            let ds = self.ds(di);
            let mut d = ds.lock();
            if !d.deleted {
                d.deleted = true;
                ds_out.push(di);
            }
        }
        for gi in child_gs {
            self.delete_group_recursive(gi, ds_out, gs_out);
        }
    }

    /// Free everything a soft-deleted dataset owned. The single owner of
    /// delete-time reclamation, called only from the two delete paths with
    /// the dataset already marked deleted and its op lock held.
    ///
    /// A deleted dataset contributes nothing to finalize (the header,
    /// index-flush and append-flush loops all skip it), so nothing in the
    /// finalized file can reference the blocks freed here. Never runs under
    /// SWMR — the delete entry points refuse first.
    fn release_dataset_storage(&self, index: usize) -> IoResult<()> {
        use crate::format::messages::datatype::DatatypeMessage;
        let (indexed, ndims, contiguous, is_vlen, attrs, header_blocks, mapping_list) = {
            let ds = self.ds(index);
            let mut m = ds.lock();
            // Buffered rows were never written to a chunk; they die with
            // the dataset instead of being flushed at close.
            m.append = None;
            let indexed = m.is_chunked();
            let contiguous = (!indexed && m.data_addr != UNDEF_ADDR && m.data_size > 0)
                .then_some((m.data_addr, m.data_size));
            m.data_addr = UNDEF_ADDR;
            m.data_size = 0;
            // The external files themselves are the application's, not this
            // file's, and neither is the name heap freed: `H5O_MSG_EFL`
            // installs no file-delete method, so libhdf5 leaves the heap block
            // behind too. Dropping the list is what stops a deleted dataset
            // still claiming storage.
            m.external = None;
            // The mapping list is this file's own metadata, so unlike the
            // external files above it *is* freed — `H5D__virtual_delete`
            // removes the heap object. The source datasets it named are
            // another file's and are left alone.
            let mapping_list = m
                .virtual_storage
                .take()
                .and_then(|v| u16::try_from(v.heap_index).ok().map(|i| (v.heap_addr, i)));
            let is_vlen = matches!(
                m.datatype,
                DatatypeMessage::VarLenString { .. } | DatatypeMessage::VarLenSequence { .. }
            );
            let attrs = std::mem::take(&mut m.attributes);
            m.obj_header_written_addr = None;
            let header_blocks = std::mem::take(&mut m.obj_header_blocks);
            (
                indexed,
                m.dataspace.dims.len(),
                contiguous,
                is_vlen,
                attrs,
                header_blocks,
                mapping_list,
            )
        };
        if let Some((addr, idx)) = mapping_list {
            self.remove_heap_objects([(addr, vec![idx])].into_iter().collect())?;
        }
        if indexed {
            // Prune to a zero extent: every stored chunk is entirely beyond
            // it, so the walk frees each chunk block and collects the vlen
            // references its bytes held (released inside).
            self.prune_chunks_beyond(index, &vec![0; ndims])?;
            self.free_chunk_index(index)?;
        } else if let Some((addr, size)) = contiguous {
            if is_vlen {
                let data = self.handle.read_at(addr, size as usize)?;
                self.release_vlen_references(&data)?;
            }
            self.allocator.free(addr, size, FreeSpaceClass::RawData);
        }
        for attr in &attrs {
            self.release_attr_vlen(attr)?;
        }
        self.release_superseded_dense_attrs(AttrScope::Dataset(index))?;
        for (addr, size) in header_blocks {
            self.allocator.free(addr, size, FreeSpaceClass::Metadata);
        }
        Ok(())
    }

    /// Free a deleted group's file space: its attributes' global-heap
    /// objects and, on a reopened file, the on-disk header block. The
    /// group counterpart of
    /// [`release_dataset_storage`](Self::release_dataset_storage).
    fn release_group_storage(&self, gidx: usize) -> IoResult<()> {
        let (attrs, header_blocks) = {
            let grp = self.grp(gidx);
            let mut g = grp.lock();
            let attrs = std::mem::take(&mut g.attributes);
            g.obj_header_written_addr = None;
            (attrs, std::mem::take(&mut g.obj_header_blocks))
        };
        for attr in &attrs {
            self.release_attr_vlen(attr)?;
        }
        self.release_superseded_dense_attrs(AttrScope::Group(gidx))?;
        self.release_superseded_dense_links(LinkScope::Group(gidx))?;
        for (addr, size) in header_blocks {
            self.allocator.free(addr, size, FreeSpaceClass::Metadata);
        }
        Ok(())
    }

    /// Free the dense attribute storage a reopened header names, once, when
    /// this session stops naming it — because the header is being rewritten
    /// around fresh storage, or because the object was deleted.
    ///
    /// The single owner of that transition: nothing else removes an attribute
    /// entry from [`superseded_dense`](Self::superseded_dense), and this
    /// removes it as it frees, so no heap is freed twice or left half freed.
    /// An object whose storage was compact, or whose header this session
    /// keeps, has no entry and nothing happens.
    ///
    /// Never under SWMR: a live reader may still be walking the storage the
    /// published headers name, the same rule the superseded-header and
    /// relocated-chunk paths follow. The entry stays in place, unfreed.
    fn release_superseded_dense_attrs(&self, scope: AttrScope) -> IoResult<()> {
        if self.swmr_active {
            return Ok(());
        }
        let taken = self
            .superseded_dense
            .lock()
            .as_mut()
            .and_then(|s| s.attrs.remove(&scope));
        let Some(ainfo) = taken else {
            return Ok(());
        };
        self.release_dense_storage(
            ainfo.fractal_heap_address,
            ainfo.name_btree_address,
            ainfo.creation_order_btree_address,
        )
    }

    /// The link counterpart of
    /// [`release_superseded_dense_attrs`](Self::release_superseded_dense_attrs),
    /// under the same invariant and the same SWMR rule. Split from it because
    /// the two are superseded at different points of a finalize: attribute
    /// storage before the object headers are laid out, link storage after
    /// every one of them has an address.
    fn release_superseded_dense_links(&self, scope: LinkScope) -> IoResult<()> {
        if self.swmr_active {
            return Ok(());
        }
        let taken = self
            .superseded_dense
            .lock()
            .as_mut()
            .and_then(|s| s.links.remove(&scope));
        let Some(linfo) = taken else {
            return Ok(());
        };
        self.release_dense_storage(
            linfo.fractal_heap_address,
            linfo.name_btree_address,
            linfo.creation_order_btree_address,
        )
    }

    /// Return one dense storage's file space to the allocator: the fractal
    /// heap in full, its name index, and the creation-order index when the
    /// object had one.
    ///
    /// The extents come from walking the structures themselves rather than
    /// from re-deriving what a writer would have allocated, so storage
    /// libhdf5 laid out is freed as accurately as storage this crate wrote.
    /// Every walk here already ran once this session — the reopen read every
    /// attribute out of this heap through the same index — so a failure means
    /// the file changed underneath us, and surfacing it beats freeing a
    /// partial extent list.
    fn release_dense_storage(
        &self,
        heap_addr: u64,
        name_bt2_addr: u64,
        corder_bt2_addr: Option<u64>,
    ) -> IoResult<()> {
        use crate::format::chunk_index::btree_v2::collect_btree_v2_extents;
        use crate::format::fractal_heap::collect_heap_extents;

        let mut reader = crate::io::reader::HandleBlockReader {
            handle: &self.handle,
        };
        let mut extents = Vec::new();
        if heap_addr != UNDEF_ADDR {
            extents.extend(collect_heap_extents(heap_addr, &self.ctx, &mut reader)?);
        }
        for addr in [Some(name_bt2_addr), corder_bt2_addr]
            .into_iter()
            .flatten()
            .filter(|&a| a != UNDEF_ADDR)
        {
            extents.extend(collect_btree_v2_extents(addr, &self.ctx, &mut reader)?);
        }
        for (addr, len) in extents {
            self.allocator.free(addr, len, FreeSpaceClass::Metadata);
        }
        Ok(())
    }

    /// Free a deleted dataset's chunk-index structures, after the chunks
    /// themselves were freed by a zero-extent prune. Takes the index info
    /// out of the slot, so the dataset no longer claims chunked storage.
    ///
    /// Every block's size is recovered the way its allocation computed it:
    /// re-encoding the in-memory copy (EA header and index block, FA
    /// header and data block, BT2 header) or sizing a same-shape dummy
    /// from the array geometry (EA data blocks, whose element counts come
    /// from [`EaGeometry`]; BT2 nodes are all `node_size`).
    fn free_chunk_index(&self, index: usize) -> IoResult<()> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        if let Some(c) = m.chunked.take() {
            let p = &c.earray_params;
            let bits = p.max_nelmts_bits;
            let csl = c.chunk_size_len;
            let geo = EaGeometry::new(
                p.idx_blk_elmts,
                p.data_blk_min_elmts,
                p.sup_blk_min_data_ptrs,
                bits,
                p.max_dblk_page_nelmts_bits,
            )?;
            let dblk_size = |nelmts: u64| -> u64 {
                if is_filtered {
                    FilteredDataBlock::new(c.ea_header_addr, 0, nelmts as usize)
                        .encode(&self.ctx, bits, csl)
                        .len() as u64
                } else {
                    ExtensibleArrayDataBlock::new(c.ea_header_addr, 0, nelmts as usize)
                        .encoded_size(&self.ctx, bits) as u64
                }
            };
            let (dblk_addrs, sblk_addrs, iblk_size) = if is_filtered {
                let f = c.filt_iblk.as_ref().unwrap();
                (
                    f.dblk_addrs.clone(),
                    f.sblk_addrs.clone(),
                    f.encode(&self.ctx, csl).len() as u64,
                )
            } else {
                (
                    c.ea_iblk.dblk_addrs.clone(),
                    c.ea_iblk.sblk_addrs.clone(),
                    c.ea_iblk.encoded_size(&self.ctx) as u64,
                )
            };
            // Data blocks addressed from the index block belong to the
            // first `iblock_nsblks` super blocks; each of those defines the
            // element count (and so the disk size) of its data blocks.
            let mut g = 0usize;
            'direct: for s in geo.sblk.iter().take(geo.iblock_nsblks) {
                for _ in 0..s.ndblks {
                    let Some(&a) = dblk_addrs.get(g) else {
                        break 'direct;
                    };
                    g += 1;
                    if a == UNDEF_ADDR {
                        continue;
                    }
                    if s.dblk_nelmts > geo.dblk_page_nelmts {
                        return Err(crate::io::IoError::InvalidState(
                            "cannot free a paged extensible-array data block, \
                             which is not yet supported"
                                .into(),
                        ));
                    }
                    self.allocator
                        .free(a, dblk_size(s.dblk_nelmts), FreeSpaceClass::Metadata);
                }
            }
            for (off, &sa) in sblk_addrs.iter().enumerate() {
                if sa == UNDEF_ADDR {
                    continue;
                }
                let s = geo.sblk[geo.iblock_nsblks + off];
                if s.dblk_nelmts > geo.dblk_page_nelmts {
                    return Err(crate::io::IoError::InvalidState(
                        "cannot free a paged extensible-array data block, \
                         which is not yet supported"
                            .into(),
                    ));
                }
                let buf = self.handle.read_at_most(sa, 65536)?;
                let sb =
                    ExtensibleArraySuperBlock::decode(&buf, &self.ctx, bits, s.ndblks as usize, 0)?;
                for &da in &sb.dblk_addrs {
                    if da != UNDEF_ADDR {
                        self.allocator
                            .free(da, dblk_size(s.dblk_nelmts), FreeSpaceClass::Metadata);
                    }
                }
                self.allocator.free(
                    sa,
                    sb.encode(&self.ctx, bits).len() as u64,
                    FreeSpaceClass::Metadata,
                );
            }
            self.allocator
                .free(c.ea_iblk_addr, iblk_size, FreeSpaceClass::Metadata);
            self.allocator.free(
                c.ea_header_addr,
                c.ea_header.encoded_size(&self.ctx) as u64,
                FreeSpaceClass::Metadata,
            );
            return Ok(());
        }
        if let Some(fa) = m.fixed_array.take() {
            self.allocator.free(
                fa.fa_dblk_addr,
                fixed_array_dblk_disk_size(&self.ctx, &fa.fa_header),
                FreeSpaceClass::Metadata,
            );
            self.allocator.free(
                fa.fa_header_addr,
                fa.fa_header.encode(&self.ctx).len() as u64,
                FreeSpaceClass::Metadata,
            );
            return Ok(());
        }
        // The implicit index has no structure to free, only the one run of
        // chunk space it was given at create — which is the whole of its
        // storage, so nothing else can be leaked or double-freed here.
        if let Some(imp) = m.implicit.take() {
            self.allocator
                .free(imp.data_addr, imp.data_size, FreeSpaceClass::RawData);
            return Ok(());
        }
        // The single-chunk index has no structure of its own either: its one
        // chunk is the whole of its storage, addressed directly from the
        // layout message rather than any index this function's doc comment's
        // "chunks already freed by a zero-extent prune" applies to — so
        // freeing it here, if it was ever allocated, is the only place it
        // happens.
        if let Some(sc) = m.single_chunk.take() {
            if sc.data_addr != UNDEF_ADDR {
                let len = if is_filtered { sc.nbytes } else { sc.data_size };
                self.allocator
                    .free(sc.data_addr, len, FreeSpaceClass::RawData);
            }
            return Ok(());
        }
        // The version-1 B-tree owns nothing but its node blocks: the header
        // every other index has is, here, the root pointer inside the layout
        // message.
        if let Some(bt1) = m.btree_v1.take() {
            let element_size = m.datatype.element_size() as u64;
            let node_size = bt1
                .build_tree(element_size, self.ctx.sizeof_addr as usize)
                .node_size();
            for &a in &bt1.node_addrs {
                self.allocator
                    .free(a, node_size as u64, FreeSpaceClass::Metadata);
            }
            return Ok(());
        }
        if let Some(bt2) = m.btree_v2.take() {
            let tree = bt2.index.build_tree(&self.ctx);
            for &a in &bt2.node_addrs {
                self.allocator
                    .free(a, tree.node_size as u64, FreeSpaceClass::Metadata);
            }
            self.allocator.free(
                bt2.bt2_header_addr,
                tree.header(UNDEF_ADDR).encode(&self.ctx).len() as u64,
                FreeSpaceClass::Metadata,
            );
        }
        Ok(())
    }

    /// Return the chunk dimensions for a dataset, if chunked.
    ///
    /// Returns an owned `Vec` because the chunk geometry now lives behind the
    /// per-dataset [`Slot`]; it cannot be borrowed past the guard.
    pub fn dataset_chunk_dims(&self, index: usize) -> Option<Vec<u64>> {
        let ds = self.ds(index);
        let m = ds.lock();
        m.chunk_index_kind().map(|kind| match kind {
            ChunkIndexKind::ExtensibleArray => m.chunked.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::FixedArray => m.fixed_array.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::BtreeV2 => m.btree_v2.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::Implicit => m.implicit.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::SingleChunk => m.single_chunk.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::BtreeV1 => m.btree_v1.as_ref().unwrap().chunk_dims.clone(),
        })
    }

    /// Return the current dimensions of a dataset.
    ///
    /// Returns an owned `Vec` because the dataspace now lives behind the
    /// per-dataset [`Slot`]; it cannot be borrowed past the guard.
    pub fn dataset_dims(&self, index: usize) -> Vec<u64> {
        self.ds(index).lock().dataspace.dims.clone()
    }

    /// Return the maximum extent a dataset declares, per dimension.
    ///
    /// An absent maximum shape means the shape is fixed at its current extent
    /// (libhdf5 defaults maxdims to dims at creation), so the current
    /// dimensions are returned; `H5S_UNLIMITED` is `u64::MAX`.
    pub fn dataset_max_dims(&self, index: usize) -> Vec<u64> {
        let ds = self.ds(index);
        let m = ds.lock();
        m.dataspace
            .max_dims
            .clone()
            .unwrap_or_else(|| m.dataspace.dims.clone())
    }

    /// Whether a dataset stores its raw data through a filter pipeline.
    ///
    /// The write paths ask before choosing how to hand a chunk over: an
    /// unfiltered chunk's bytes go to the file exactly as the caller holds
    /// them, while a filtered one has to be compressed first.
    pub(crate) fn dataset_is_filtered(&self, index: usize) -> bool {
        self.ds(index).lock().filter_pipeline.is_some()
    }

    /// Return the datatype a dataset declares on disk.
    ///
    /// The typed write paths need it to store bytes in the declared byte
    /// order; a reopened dataset handle has no copy of its own, and a cached
    /// one could disagree with what the header will say.
    pub fn dataset_datatype(&self, index: usize) -> DatatypeMessage {
        self.ds(index).lock().datatype.clone()
    }

    /// Create a group in the file hierarchy.
    ///
    /// `parent_path` is the full path of the parent group (e.g., "/" for root).
    /// `name` is the name of the new group (e.g., "detector").
    ///
    /// Returns the group index in the writer's group list.
    pub fn create_group(&self, parent_path: &str, name: &str) -> IoResult<usize> {
        // Hold the create gate across the uniqueness check and the registry
        // push so the two are atomic (see `create_lock`).
        let _create = self.create_lock.lock();
        // A parent path through hard links creates in the link's target,
        // as HDF5 traversal does.
        let parent_path = self.canonical_group_path(parent_path);
        let parent_path = parent_path.as_str();
        let full_name = if parent_path == "/" {
            format!("/{}", name)
        } else {
            format!("{}/{}", parent_path, name)
        };
        // Same rule as dataset creation: a path through a carried external
        // link names a group in the other file, which this writer cannot make.
        self.reject_external_traversal(&full_name)?;
        // `name` may itself carry path components; resolving the whole thing
        // is what keeps a '/' out of the link this group will be reached by.
        let (parent_idx, _leaf) = self.split_parent(full_name.trim_start_matches('/'))?;

        self.ensure_name_free(full_name.trim_start_matches('/'))?;

        let group_idx = self.push_group(GroupInfo {
            name: full_name,
            parent: parent_idx,
            creation_seq: self.take_creation_seq(),
            track_order: self.track_order,
            times: self.created_object_times(),
            child_datasets: Vec::new(),
            child_groups: Vec::new(),
            obj_header_addr: 0,
            obj_header_written_addr: None,
            obj_header_blocks: Vec::new(),
            deleted: false,
            attributes: Vec::new(),
        });

        // Register this group as a child of its parent
        if let Some(pidx) = parent_idx {
            self.grp(pidx).lock().child_groups.push(group_idx);
        }

        Ok(group_idx)
    }

    /// Register a dataset as belonging to a group.
    ///
    /// `group_path` is the full path of the group (e.g., "/detector").
    /// `ds_index` is the dataset index returned by `create_dataset`.
    pub fn assign_dataset_to_group(&self, group_path: &str, ds_index: usize) -> IoResult<()> {
        let group_path = self.canonical_group_path(group_path);
        let group_path = group_path.as_str();
        let groups = self.group_refs();
        let group_idx = groups
            .iter()
            .position(|g| {
                let gg = g.lock();
                gg.name == group_path && !gg.deleted
            })
            .ok_or_else(|| {
                crate::io::IoError::NotFound(format!("group '{}' not found", group_path))
            })?;
        // A move, not an addition: the create gate has already placed every
        // dataset from the path components of its name, so appending here
        // would leave one dataset linked from two groups at once.
        for g in &groups {
            g.lock().child_datasets.retain(|&d| d != ds_index);
        }
        groups[group_idx].lock().child_datasets.push(ds_index);
        Ok(())
    }

    /// Create a hard link: an additional name for an object that already
    /// exists in the file.
    ///
    /// No data is copied — the link and its target share one object header,
    /// exactly as `h5py` / libhdf5 hard links do.
    ///
    /// * `parent_group_path` — full path of the group that will hold the
    ///   link (`"/"` for the root group).
    /// * `link_name` — leaf name of the new link within that group.
    /// * `target_path` — full path of an existing dataset or group, with or
    ///   without a leading `/`.
    pub fn create_hard_link(
        &self,
        parent_group_path: &str,
        link_name: &str,
        target_path: &str,
    ) -> IoResult<()> {
        if link_name.is_empty() || link_name.contains('/') {
            return Err(crate::io::IoError::InvalidState(format!(
                "hard link name '{link_name}' must be a non-empty leaf name"
            )));
        }

        // Neither end may sit across a carried external link: the target
        // would be an object in the other file, and the link itself would be
        // a name in a group this writer does not own.
        self.reject_external_traversal(target_path)?;
        self.reject_external_traversal(&format!(
            "{}/{link_name}",
            parent_group_path.trim_end_matches('/')
        ))?;

        // Hold the create gate across the collision check and the hard-link
        // push so the two are atomic (see `create_lock`).
        let _create = self.create_lock.lock();
        // Both paths resolve through hard links, as HDF5 traversal does.
        let parent_group_path = self.canonical_group_path(parent_group_path);
        let parent_group_path = parent_group_path.as_str();

        // Resolve the parent group (None == root).
        let parent = if parent_group_path == "/" {
            None
        } else {
            Some(
                self.group_refs()
                    .iter()
                    .position(|g| {
                        let gg = g.lock();
                        gg.name == parent_group_path && !gg.deleted
                    })
                    .ok_or_else(|| {
                        crate::io::IoError::NotFound(format!(
                            "parent group '{parent_group_path}' not found"
                        ))
                    })?,
            )
        };

        // Resolve the target. Dataset names are stored without a leading
        // '/', group names with one — compare on the trimmed form. A
        // trailing '/' is tolerated too.
        let target_rel = self.canonical_dataset_path(target_path.trim_matches('/'));
        let target_rel = target_rel.as_str();
        if target_rel.is_empty() {
            return Err(crate::io::IoError::InvalidState(
                "cannot hard-link the root group".into(),
            ));
        }
        let target = self.resolve_object(target_rel).ok_or_else(|| {
            crate::io::IoError::NotFound(format!("hard link target '{target_path}' not found"))
        })?;

        // Reject a name already taken in the parent group.
        self.ensure_name_free(&self.link_full_path(parent, link_name))?;

        self.hard_links.lock().push(HardLink {
            parent,
            name: link_name.to_string(),
            target,
            creation_seq: self.take_creation_seq(),
        });
        self.register_name(&self.link_full_path(parent, link_name), NameHit::HardLink);
        Ok(())
    }

    /// Whether a hard link will actually be emitted: both its parent group
    /// and its target object must still be present (not soft-deleted).
    fn hard_link_emitted(&self, link: &HardLink) -> bool {
        let parent_ok = self.parent_alive(link.parent);
        let target_ok = match link.target {
            HardLinkTarget::Dataset(i) => !self.ds(i).lock().deleted,
            HardLinkTarget::Group(i) => !self.grp(i).lock().deleted,
        };
        parent_ok && target_ok
    }

    /// The full path a link occupies, with no leading `/` — the same form
    /// dataset names are stored in. The one place a parent index and a leaf
    /// name become a path, so every link kind answers the collision check in
    /// the same spelling.
    fn link_full_path(&self, parent: Option<usize>, name: &str) -> String {
        match parent {
            None => name.to_string(),
            Some(pi) => format!(
                "{}/{name}",
                self.grp(pi).lock().name.trim_start_matches('/')
            ),
        }
    }

    /// The full path a hard link occupies; see [`Self::link_full_path`].
    fn hard_link_full_path(&self, link: &HardLink) -> String {
        self.link_full_path(link.parent, &link.name)
    }

    /// Whether a symbolic link will actually be emitted: its parent group
    /// must still be present. There is no target to check — a soft or
    /// external link is allowed to dangle, and `H5Lcreate_soft` does not look
    /// at the path it stores.
    fn symbolic_link_emitted(&self, link: &SymbolicLink) -> bool {
        self.parent_alive(link.parent)
    }

    /// Whether the group that would hold a link still exists; `None` is the
    /// root group, which cannot be deleted.
    ///
    /// A deleted group's header is never written, so nothing it would have
    /// held is in the file — and the name is free again. Every registry
    /// decides that the same way, through here.
    fn parent_alive(&self, parent: Option<usize>) -> bool {
        match parent {
            None => true,
            Some(pi) => !self.grp(pi).lock().deleted,
        }
    }

    /// The full path a symbolic link occupies; see [`Self::link_full_path`].
    fn symbolic_link_full_path(&self, link: &SymbolicLink) -> String {
        self.link_full_path(link.parent, &link.name)
    }

    /// Create a soft or external link: a name in a group whose value is a
    /// path rather than an object.
    ///
    /// The single owner of symbolic-link creation — `H5Lcreate_soft` and
    /// `H5Lcreate_external` differ only in the value they store, and the
    /// name, parent and collision rules they share are all here.
    ///
    /// * `parent_group_path` — full path of the group that will hold the
    ///   link (`"/"` for the root group).
    /// * `link_name` — leaf name of the new link within that group.
    /// * `target` — the path this link names, and for an external link the
    ///   file holding it. Neither is resolved or required to exist: HDF5
    ///   answers a symbolic link at traversal time, so a dangling one is a
    ///   legal file.
    pub fn create_symbolic_link(
        &self,
        parent_group_path: &str,
        link_name: &str,
        target: LinkTarget,
    ) -> IoResult<()> {
        if link_name.is_empty() || link_name.contains('/') {
            return Err(crate::io::IoError::InvalidState(format!(
                "link name '{link_name}' must be a non-empty leaf name"
            )));
        }
        // `H5Lcreate_external` refuses an empty file or object name, and
        // stores the object path normalized; a link written here and one
        // libhdf5 writes from the same arguments then hold the same bytes.
        let target = match target {
            LinkTarget::External { file, path } => {
                if file.is_empty() || path.is_empty() {
                    return Err(crate::io::IoError::InvalidState(
                        "an external link needs both a file name and an object path".into(),
                    ));
                }
                LinkTarget::External {
                    file,
                    path: crate::format::messages::link::normalize_object_path(&path),
                }
            }
            other => other,
        };
        // The link itself would be a name in a group that lives in another
        // file; its *value* may name anything, including a path this writer
        // cannot follow, because nothing follows it here.
        self.reject_external_traversal(&format!(
            "{}/{link_name}",
            parent_group_path.trim_end_matches('/')
        ))?;

        let _create = self.create_lock.lock();
        let parent_group_path = self.canonical_group_path(parent_group_path);
        let parent_group_path = parent_group_path.as_str();
        let parent = if parent_group_path == "/" {
            None
        } else {
            Some(
                self.group_refs()
                    .iter()
                    .position(|g| {
                        let gg = g.lock();
                        gg.name == parent_group_path && !gg.deleted
                    })
                    .ok_or_else(|| {
                        crate::io::IoError::NotFound(format!(
                            "parent group '{parent_group_path}' not found"
                        ))
                    })?,
            )
        };

        self.ensure_name_free(&self.link_full_path(parent, link_name))?;
        self.symbolic_links.lock().push(SymbolicLink {
            parent,
            name: link_name.to_string(),
            target,
            creation_seq: self.take_creation_seq(),
        });
        self.register_name(
            &self.link_full_path(parent, link_name),
            NameHit::SymbolicLink,
        );
        Ok(())
    }

    // ---------------------------------------------------------- committed types

    /// Snapshot the committed-datatype list; see [`Self::hard_links_vec`].
    pub(crate) fn committed_datatypes_vec(&self) -> Vec<CommittedDatatype> {
        self.committed_datatypes.lock().clone()
    }

    /// The paths of every committed datatype a name still reaches, in
    /// creation order. One inside a deleted group is not among them: no link
    /// to it is emitted, so the file will not hold that name.
    ///
    /// Both halves of the file answer. A datatype an earlier session
    /// committed is carried by its bytes, not re-encoded, so it lives in the
    /// preserved-link list rather than the registry — and listing only the
    /// registry is what made this answer `[]` for a file whose every named
    /// type was committed before it was opened, while a reader of the same
    /// file named them all.
    pub(crate) fn committed_datatype_names(&self) -> Vec<String> {
        let mut out: Vec<String> = self
            .committed_datatypes_vec()
            .iter()
            .filter(|c| self.parent_alive(c.parent))
            .map(|c| c.name.clone())
            .collect();
        out.extend(
            self.preserved_links
                .lock()
                .iter()
                .filter(|l| l.kind == PreservedKind::NamedDatatype)
                .map(|l| self.preserved_link_full_path(l)),
        );
        out
    }

    /// Commit `datatype` as an object of its own under `name` —
    /// `H5Tcommit2`. Returns its index in the committed-datatype registry.
    ///
    /// The object holds one datatype message and nothing else. It goes
    /// through [`begin_create`](Self::begin_create) like a dataset, so its
    /// name is resolved to a real parent group, refused if taken, and refused
    /// if it would cross a carried external link.
    pub fn commit_datatype(&self, name: &str, datatype: DatatypeMessage) -> IoResult<usize> {
        let create = self.begin_create(name.trim_start_matches('/'))?;
        let entry = CommittedDatatype {
            name: create.name.clone(),
            parent: create.parent,
            datatype,
            creation_seq: self.take_creation_seq(),
            times: self.created_object_times(),
            obj_header_addr: 0,
        };
        let name = entry.name.clone();
        let idx = {
            let mut reg = self.committed_datatypes.lock();
            let idx = reg.len();
            reg.push(entry);
            idx
        };
        self.register_name(&name, NameHit::Datatype(idx));
        Ok(idx)
    }

    /// Resolve a committed datatype's path to its registry index and the type
    /// it holds — the pair a dataset needs to be built on it.
    ///
    /// Returned together so the caller cannot pair one committed type's index
    /// with another's datatype: the dataset's element width, dataspace and
    /// payload checks all come from the type, and its header names the index.
    pub(crate) fn committed_datatype_for_share(
        &self,
        name: &str,
    ) -> IoResult<(usize, DatatypeMessage)> {
        let name = self.canonical_dataset_path(name.trim_start_matches('/'));
        let all = self.committed_datatypes_vec();
        all.iter()
            .position(|c| self.parent_alive(c.parent) && c.name == name)
            .map(|i| (i, all[i].datatype.clone()))
            .ok_or_else(|| {
                crate::io::IoError::NotFound(format!("no committed datatype named '{name}'"))
            })
    }

    /// Record that dataset `dataset` stores its datatype as a pointer to the
    /// committed datatype `committed`.
    ///
    /// Takes an index [`committed_datatype_for_share`](Self::committed_datatype_for_share)
    /// produced, alongside the datatype from the same call, so the two cannot
    /// disagree and there is nothing here that can fail after the dataset
    /// exists.
    pub(crate) fn share_committed_type(&self, dataset: usize, committed: usize) {
        debug_assert!(committed < self.committed_datatypes.lock().len());
        self.ds(dataset).lock().committed_type = Some(CommittedTypeRef::Session(committed));
    }

    /// How many names reach the committed datatype `index`: the link that
    /// gave it its name, plus every live dataset that shares it.
    ///
    /// `H5O__shared_link_adj` counts a share as a link, which is why a type
    /// h5py commits and then builds one dataset on reports `rc == 2`. Zero
    /// means nothing reaches it at all — the group holding its name was
    /// deleted and no dataset shares it — and then it is not written.
    fn committed_datatype_refcount(&self, index: usize) -> u32 {
        let linked = {
            let parent = self.committed_datatypes.lock()[index].parent;
            u32::from(self.parent_alive(parent))
        };
        let shares = self
            .dataset_refs()
            .iter()
            .filter(|d| {
                let m = d.lock();
                !m.deleted && m.committed_type == Some(CommittedTypeRef::Session(index))
            })
            .count() as u32;
        linked + shares
    }

    /// Append the link naming each committed datatype whose parent group is
    /// `parent`. A committed datatype is reached by an ordinary hard link —
    /// what makes it a datatype rather than a group or a dataset is the one
    /// message in the header it points at.
    ///
    /// Only a live group's links are collected, and a live parent is itself a
    /// reference, so every address named here belongs to a header
    /// `write_committed_datatype_headers` wrote.
    fn push_committed_datatypes(&self, links: &mut Vec<(u64, LinkMessage)>, parent: Option<usize>) {
        for cd in self.committed_datatypes_vec() {
            if cd.parent != parent {
                continue;
            }
            let leaf = cd.name.rsplit('/').next().unwrap_or(&cd.name);
            links.push((cd.creation_seq, LinkMessage::hard(leaf, cd.obj_header_addr)));
        }
    }

    /// Rewrite a group path that passes through hard links into the tree
    /// path of the group it reaches — HDF5 traversal, where any link in a
    /// path component resolves to its target. Group-name form (leading
    /// `/`). Repeats because a substituted target's subtree can hold
    /// further links; bounded like libhdf5's link-traversal limit, so a
    /// link cycle cannot loop forever. A path with no link components
    /// (including one naming nothing at all) comes back unchanged.
    pub(crate) fn canonical_group_path(&self, path: &str) -> String {
        let mut path = path.to_string();
        for _ in 0..64 {
            // The longest emitted group-link path that is the whole of
            // `path` or a '/'-boundary prefix of it.
            let mut best: Option<(usize, usize)> = None; // (prefix len, target)
            for l in self.hard_links_vec() {
                let HardLinkTarget::Group(gi) = l.target else {
                    continue;
                };
                if !self.hard_link_emitted(&l) {
                    continue;
                }
                let lp = format!("/{}", self.hard_link_full_path(&l));
                let covers = path == lp || path.starts_with(&format!("{lp}/"));
                if covers && best.is_none_or(|(len, _)| lp.len() > len) {
                    best = Some((lp.len(), gi));
                }
            }
            let Some((len, gi)) = best else { break };
            let target_name = self.grp(gi).lock().name.clone();
            path = format!("{}{}", target_name, &path[len..]);
        }
        path
    }

    /// [`canonical_group_path`](Self::canonical_group_path) in the
    /// dataset-name form (no leading `/`): the leaf is a dataset, so only
    /// group links can appear as components and the whole path can go
    /// through the group rewrite unchanged.
    fn canonical_dataset_path(&self, name: &str) -> String {
        self.canonical_group_path(&format!("/{name}"))
            .trim_start_matches('/')
            .to_string()
    }

    /// Total number of hard links resolving to an object: its own tree link
    /// plus every emitted user-created hard link pointing at it.
    fn object_link_count(&self, target: HardLinkTarget) -> u32 {
        let same = |a: HardLinkTarget, b: HardLinkTarget| -> bool {
            matches!(
                (a, b),
                (HardLinkTarget::Dataset(x), HardLinkTarget::Dataset(y))
                    | (HardLinkTarget::Group(x), HardLinkTarget::Group(y))
                if x == y
            )
        };
        1 + self
            .hard_links_vec()
            .iter()
            .filter(|l| self.hard_link_emitted(l) && same(l.target, target))
            .count() as u32
    }

    /// The object a path names, or `None` when nothing in the file does.
    ///
    /// `path` is the trimmed, hard-link-canonical form (no leading or
    /// trailing `/`) that dataset and group names compare against. The single
    /// owner of path→object resolution on the write side: hard links and
    /// object references must agree on what a path means, including that a
    /// path may itself be a user hard link — links have no chain (each points
    /// straight at the object header, as in libhdf5), so the existing link's
    /// target is the answer.
    pub(crate) fn resolve_object(&self, path: &str) -> Option<HardLinkTarget> {
        if let Some(idx) = self.dataset_refs().iter().position(|d| {
            let g = d.lock();
            !g.deleted && g.name.trim_start_matches('/') == path
        }) {
            return Some(HardLinkTarget::Dataset(idx));
        }
        if let Some(idx) = self.group_refs().iter().position(|g| {
            let gg = g.lock();
            !gg.deleted && gg.name.trim_start_matches('/') == path
        }) {
            return Some(HardLinkTarget::Group(idx));
        }
        self.hard_links_vec().iter().find_map(|l| {
            (self.hard_link_emitted(l) && self.hard_link_full_path(l) == path).then_some(l.target)
        })
    }

    /// The address of dataset `index`'s own contiguous block, for the two
    /// writers that stamp single elements into it by file offset — object and
    /// region references, whose values are only known once finalize has placed
    /// every object header.
    ///
    /// Refuses, rather than handing back an address that is not one, every
    /// dataset that has no such block: chunked, compact, unallocated, or with
    /// its raw data in files outside this one.
    fn local_element_block(&self, index: usize, what: &str) -> IoResult<u64> {
        let ds = self.ds(index);
        let m = ds.lock();
        match m.contiguous_target() {
            Some(ContiguousTarget::Local(addr)) => Ok(addr),
            Some(ContiguousTarget::External { .. }) => {
                Err(crate::io::IoError::InvalidState(format!(
                    "{what} are stamped into the dataset's own contiguous block, and \
                 dataset '{}' has none: its raw data lives in external files",
                    m.name
                )))
            }
            Some(ContiguousTarget::Virtual) => Err(crate::io::IoError::InvalidState(format!(
                "{what} are stamped into the dataset's own contiguous block, and \
                 dataset '{}' has none: it is virtual, and its elements come from \
                 the source datasets its mappings name",
                m.name
            ))),
            None => Err(crate::io::IoError::InvalidState(format!(
                "{what} are stamped into contiguous storage; create the dataset \
                 without chunking"
            ))),
        }
    }

    /// Store object references naming `paths` into the elements of dataset
    /// `index` starting at `start`.
    ///
    /// The value of an `H5R_OBJECT1` element is its target's object header
    /// address, which finalize assigns, so what lands here is the target path;
    /// [`Self::write_object_reference_values`] writes the addresses. Elements
    /// never written keep the zero image libhdf5 reads back as a null
    /// reference.
    pub fn write_object_references(
        &self,
        index: usize,
        start: u64,
        paths: &[&str],
    ) -> IoResult<()> {
        let elements = {
            let ds = self.ds(index);
            let m = ds.lock();
            match &m.datatype {
                // Both generations of object reference: `H5T_STD_REF_OBJ` and
                // the 1.12 `H5T_STD_REF`. They differ only in the element
                // image, which `encode_reference_element` owns.
                DatatypeMessage::Reference {
                    kind: ReferenceKind::Object1 | ReferenceKind::Object2,
                    ..
                } => {}
                other => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset '{}' has datatype {other}, not an object reference",
                        m.name
                    )))
                }
            }
            m.dataspace
                .dims
                .iter()
                .fold(1u64, |a, &d| a.saturating_mul(d))
        };
        // Refused here as well as at fixup time, so a dataset whose storage
        // cannot hold stamped elements is reported at the call that chose it.
        self.local_element_block(index, "object references")?;
        let end = start.saturating_add(paths.len() as u64);
        if end > elements {
            return Err(crate::io::IoError::InvalidState(format!(
                "elements {start}..{end} are outside the dataset's {elements}"
            )));
        }
        // Resolve now as well as at fixup time, so a path that names nothing
        // is reported at the call that got it wrong.
        for path in paths {
            self.object_reference_target(path)?;
        }
        let mut pending = self.pending_object_references.lock();
        for (i, path) in paths.iter().enumerate() {
            pending.push(PendingObjectReference {
                dataset: index,
                element: start + i as u64,
                target: (*path).to_string(),
            });
        }
        Ok(())
    }

    /// Record a hard link count of `rc` in `header`, if this file's format
    /// needs a message to carry it.
    ///
    /// A version-2 header carries the count in an Object Reference Count
    /// message, and only when more than one link reaches the object. A
    /// version-1 header carries it in its prefix and gets no message at all —
    /// `H5O_link_oh` gates every refcount-message operation on
    /// `oh->version > H5O_VERSION_1` (H5Oint.c:851), so a version-1 header
    /// holding one is a shape libhdf5 never writes.
    ///
    /// The message carries `H5O_MSG_FLAG_DONTSHARE`, which both refcount
    /// operations pass (H5Oint.c:874 append, H5Oint.c:864 write): the count is
    /// a property of this one object header, so a shared-message index that
    /// pointed several headers at one copy would make every object with the
    /// same link count share a single number.
    fn emit_refcount(&self, header: &mut ObjectHeader, rc: u32, format: ObjectFormat) {
        if rc > 1 && format == ObjectFormat::Modern {
            header.add_message(MSG_OBJ_REF_COUNT, MSG_FLAG_DONTSHARE, encode_refcount(rc));
        }
    }

    /// Encode `header` as `placement` lays it out, at the version this file's
    /// format calls for and with `rc` as the object's hard link count: every
    /// `(address, image)` pair to write, chunk 0 first.
    ///
    /// The count is passed rather than read off the header because the two
    /// versions carry it in different places — the version-1 prefix's `nlink`
    /// field, the version-2 Reference Count message
    /// [`emit_refcount`](Self::emit_refcount) already added — and only the
    /// caller knows it.
    ///
    /// INVARIANT: an object header's chunk 0 never moves once something in the
    /// file has named its address. A written header is rewritten over the
    /// chunk-0 block it already has, padded when the messages shrank and
    /// spilling into a continuation block of its own when they grew — the way
    /// `H5O__alloc_new_chunk` (H5Oalloc.c) grows a header libhdf5 cannot
    /// extend in place. That is what keeps every object reference already in
    /// the file — in a reference dataset, an attribute, a `REFERENCE_LIST`,
    /// whoever wrote them — resolving after this session. The one exception is
    /// a block too small to hold even the message naming a continuation, which
    /// [`place_header`](Self::place_header) gives up and replaces.
    ///
    /// A fresh header lives in the one block its address and encoded size
    /// describe: one whose messages overflow chunk 0 gets its continuation
    /// chunk immediately behind it in that same block, so the address is
    /// enough to free or supersede the whole header. libhdf5 would have grown
    /// chunk 0 into space that free rather than chaining onto it, but it reads
    /// a continuation chunk by the address and length its message states and
    /// cares nothing for where that lands.
    fn encode_header_in(
        &self,
        header: &ObjectHeader,
        rc: u32,
        format: ObjectFormat,
        placement: &HeaderPlacement,
    ) -> IoResult<Vec<(u64, Vec<u8>)>> {
        let plan = if placement.kept {
            header.plan_chunks_in(format, placement.size, &self.ctx)?
        } else {
            header.plan_chunks(format, self.chunk0_capacity(header, format), &self.ctx)?
        };
        let continuation_addr = match placement.continuation {
            Some((addr, _)) => addr,
            None => placement.addr + plan.chunk0_size as u64,
        };
        let (mut chunk0, continuation) =
            header.encode_chunked(&plan, format, &self.ctx, continuation_addr, rc)?;
        match (placement.continuation, continuation) {
            (Some((addr, _)), Some(image)) => Ok(vec![(placement.addr, chunk0), (addr, image)]),
            (None, Some(image)) => {
                chunk0.extend_from_slice(&image);
                Ok(vec![(placement.addr, chunk0)])
            }
            (None, None) => Ok(vec![(placement.addr, chunk0)]),
            (Some((addr, size)), None) => Err(crate::io::IoError::InvalidState(format!(
                "an object header was placed with a {size}-byte continuation block at \
                 {addr:#x} that it no longer needs; a message in it changed length \
                 once the addresses it names were known"
            ))),
        }
    }

    /// Reserve the blocks `header` will be written over, keeping `kept` — the
    /// chunk-0 block the object's existing header occupies — when there is
    /// one it can be written over.
    ///
    /// A header's layout does not depend on the addresses it carries, which is
    /// what lets the group pass hand every group header an address before it
    /// writes any of their content: every address is a fixed-width field.
    ///
    /// A kept block is given up only when it cannot describe the header at
    /// all: too narrow for the message naming a continuation chunk, or not a
    /// shape the header's version can pad (see `ObjectHeader::plan_chunks_in`).
    /// Then it is freed and the header gets a fresh block, exactly as a new
    /// object does — and the references naming it are the caller's to
    /// restamp, which the writer does for every one it registered.
    fn place_header(
        &mut self,
        header: &ObjectHeader,
        format: ObjectFormat,
        kept: Option<(u64, u64)>,
    ) -> IoResult<HeaderPlacement> {
        if let Some((addr, len)) = kept {
            let plan = usize::try_from(len)
                .ok()
                .and_then(|len| header.plan_chunks_in(format, len, &self.ctx).ok());
            match plan {
                Some(plan) => {
                    let continuation = (plan.continuation_size > 0).then(|| {
                        let size = plan.continuation_size;
                        let addr = self
                            .allocator
                            .allocate(size as u64, FreeSpaceClass::Metadata);
                        (addr, size)
                    });
                    return Ok(HeaderPlacement {
                        addr,
                        size: len as usize,
                        kept: true,
                        continuation,
                    });
                }
                // A block a SWMR reader may be walking stays allocated, as
                // everywhere else under `swmr_active`.
                None if !self.swmr_active => {
                    self.allocator.free(addr, len, FreeSpaceClass::Metadata);
                }
                None => {}
            }
        }
        let plan = header.plan_chunks(format, self.chunk0_capacity(header, format), &self.ctx)?;
        let size = plan.chunk0_size + plan.continuation_size;
        let addr = self
            .allocator
            .allocate(size as u64, FreeSpaceClass::Metadata);
        Ok(HeaderPlacement::fresh(addr, size))
    }

    /// How many bytes of messages `header`'s chunk 0 holds before the rest
    /// spill into a continuation chunk.
    ///
    /// libhdf5 sizes chunk 0 once, when the object header is created, and can
    /// only grow it while the space behind it is still free — so an object
    /// whose creation-time estimate covered every message it would ever hold
    /// keeps one chunk, and one whose estimate was a guess does not. A dataset
    /// or a committed datatype is created from messages already in hand
    /// (`H5D__update_oh_info`, `H5T__commit`), so its estimate is exact and
    /// this writer's exact fit is the same answer.
    ///
    /// A group is the exception: `H5G__obj_create_real` (H5Gobj.c:219) sizes
    /// its header for the link info and group info messages plus
    /// `H5G_CRT_GINFO_EST_NUM_ENTRIES` links of `H5G_CRT_GINFO_EST_NAME_LEN`
    /// characters, and nothing else — attributes above all — is in that
    /// estimate. The Link Info message is what identifies one: it is the
    /// message that makes an object a new-format group, and
    /// `H5G__obj_get_linfo` uses it for exactly this question.
    ///
    /// A version-1 header is written as one chunk whatever it holds: its
    /// groups keep their links in a symbol table, not in the header, so the
    /// estimate that makes a version-2 group spill never applies to one.
    fn chunk0_capacity(&self, header: &ObjectHeader, format: ObjectFormat) -> usize {
        if format == ObjectFormat::Legacy {
            return usize::MAX;
        }
        let envelope = header.message_envelope_size();
        let sized = |msg_type: u8| {
            header
                .messages
                .iter()
                .find(|m| m.msg_type == msg_type)
                .map(|m| envelope + m.data.len())
        };
        let Some(link_info) = sized(MSG_LINK_INFO) else {
            return usize::MAX;
        };
        // One estimated hard link: version, flags, a one-byte name length for
        // a name this short, the name, and the object header address.
        let link = envelope + 1 + 1 + 1 + EST_LINK_NAME_LEN + self.ctx.sizeof_addr as usize;
        link_info + sized(MSG_GROUP_INFO).unwrap_or(0) + EST_LINK_COUNT * link
    }

    /// The object an object reference's path names, as a hard-link target;
    /// `None` for the root group, which has no registry slot.
    fn object_reference_target(&self, path: &str) -> IoResult<Option<HardLinkTarget>> {
        let rel = self.canonical_dataset_path(path.trim_matches('/'));
        if rel.is_empty() {
            return Ok(None);
        }
        self.resolve_object(&rel)
            .map(Some)
            .ok_or_else(|| crate::io::IoError::NotFound(format!("reference target '{path}'")))
    }

    /// The object header address an object reference's `path` names, or zero
    /// when that object has not been given one yet.
    ///
    /// Zero is where the superblock sits, so it is never an object header's
    /// address. It is what every object reads as before
    /// [`allocate_object_headers`](Self::allocate_object_headers) runs, which
    /// is what lets the pass that measures a header stand in for the pass that
    /// writes it: an address is a fixed-width field, so the placeholder is the
    /// same size as the answer.
    fn object_reference_address(&self, path: &str) -> IoResult<u64> {
        Ok(match self.object_reference_target(path)? {
            Some(HardLinkTarget::Dataset(i)) => self.ds(i).lock().obj_header_addr,
            Some(HardLinkTarget::Group(i)) => self.grp(i).lock().obj_header_addr,
            None => self.root_group_addr.unwrap_or(0),
        })
    }

    /// `scope`'s attributes as this finalize will write them: the stored set,
    /// with every object-reference attribute's value said in the object header
    /// addresses assigned so far.
    ///
    /// The single owner of a reference attribute's value, and the only source
    /// an object header build may take an attribute set from. Nothing stored
    /// is mutated, so the pass that measures a header and the pass that writes
    /// it cannot disagree about anything but the addresses — which they cannot
    /// disagree about in length.
    ///
    /// INVARIANT: the stored attribute list is what says which attributes
    /// exist; a recorded reference value can only give a value to one already
    /// in it. So a value left behind by an object whose list was emptied — a
    /// deleted group or dataset — cannot put the attribute back, and a value
    /// whose attribute was replaced by one of another type is dropped at the
    /// replacement instead of reaching it (see
    /// [`forget_attribute_reference`](Self::forget_attribute_reference)).
    fn object_attributes(&self, scope: AttrScope) -> IoResult<Vec<AttributeEntry>> {
        let mut attrs = match scope {
            AttrScope::Root => self.root_attributes.lock().clone(),
            AttrScope::Group(gi) => self.grp(gi).lock().attributes.clone(),
            AttrScope::Dataset(i) => self.ds(i).lock().attributes.clone(),
        };
        // Snapshot first: resolving a path locks group and dataset slots.
        let values: Vec<(String, Vec<String>, usize)> = self
            .attribute_references
            .lock()
            .iter()
            .filter(|r| r.scope == scope)
            .map(|r| (r.name.clone(), r.targets.clone(), r.stride))
            .collect();
        let width = self.ctx.sizeof_addr as usize;
        for (name, targets, stride) in values {
            let Some(pos) = attrs.iter().position(|a| a.name() == name) else {
                continue;
            };
            let Some(msg) = attrs[pos].readable() else {
                continue;
            };
            let mut msg = msg.clone();
            for (i, target) in targets.iter().enumerate() {
                let at = i * stride;
                let held = msg.data.len();
                let slot = msg.data.get_mut(at..at + width).ok_or_else(|| {
                    crate::io::IoError::InvalidState(format!(
                        "attribute '{name}' holds {held} bytes, too few for reference {i} at {at}"
                    ))
                })?;
                slot.copy_from_slice(
                    &self.object_reference_address(target)?.to_le_bytes()[..width],
                );
            }
            attrs[pos] = AttributeEntry::from(msg).with_creation_index(attrs[pos].creation_index());
        }
        Ok(attrs)
    }

    /// The registry scope `target` names — the same object
    /// [`with_attr_list`](Self::with_attr_list) reaches, as the key the
    /// reference-value registry is indexed by. Refuses what that accessor
    /// refuses, and for the same reasons.
    fn attr_scope(&self, target: AttrTarget<'_>) -> IoResult<AttrScope> {
        match target {
            AttrTarget::Root => Ok(AttrScope::Root),
            AttrTarget::Group(path) => {
                let path = self.canonical_group_path(path);
                self.group_refs()
                    .iter()
                    .position(|g| {
                        let gg = g.lock();
                        gg.name == path && !gg.deleted
                    })
                    .map(AttrScope::Group)
                    .ok_or_else(|| {
                        crate::io::IoError::NotFound(format!("group '{path}' not found"))
                    })
            }
            AttrTarget::Dataset(index) => {
                let count = self.dataset_count();
                if index >= count {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset index {index} out of range (have {count})"
                    )));
                }
                Ok(AttrScope::Dataset(index))
            }
        }
    }

    /// Drop the reference value recorded for `scope`'s attribute `name`.
    ///
    /// Called by both owners of attribute-list mutation —
    /// [`insert_attribute`](Self::insert_attribute) and
    /// [`evict_attr`](Self::evict_attr) — so an attribute that is replaced or
    /// removed cannot leave its value behind for whatever takes its name next.
    /// A string attribute written over a reference attribute is the case that
    /// needs it: without this the string's bytes would be overwritten with
    /// addresses at finalize.
    fn forget_attribute_reference(&self, scope: AttrScope, name: &str) {
        self.attribute_references
            .lock()
            .retain(|r| !(r.scope == scope && r.name == name));
    }

    /// Write every pending object reference element as its target's object
    /// header address.
    ///
    /// INVARIANT: a reference element on disk holds its target's header
    /// address. Reached through [`write_reference_values`](Self::write_reference_values),
    /// which places it after every header has an address; a target that no
    /// longer resolves fails the finalize rather than leaving a placeholder
    /// behind.
    fn write_object_reference_values(&mut self) -> IoResult<()> {
        // Snapshot rather than drain: a SWMR session finalizes twice, and the
        // close-time finalize rebuilds every header at a fresh address, so the
        // elements must be stamped again with the addresses that survive.
        let pending: Vec<(usize, u64, String)> = self
            .pending_object_references
            .lock()
            .iter()
            .map(|p| (p.dataset, p.element, p.target.clone()))
            .collect();
        for (dataset, element, target) in &pending {
            let addr = match self.object_reference_target(target)? {
                Some(HardLinkTarget::Dataset(i)) => self.ds(i).lock().obj_header_addr,
                Some(HardLinkTarget::Group(i)) => self.grp(i).lock().obj_header_addr,
                None => self.root_group_addr.ok_or_else(|| {
                    crate::io::IoError::InvalidState(
                        "root group header address is not assigned yet".into(),
                    )
                })?,
            };
            // The element image is the dataset's own datatype's business: the
            // pre-1.12 and 1.12 forms differ in width and in layout, and the
            // dataset says which it holds.
            let (kind, width) = {
                let ds = self.ds(*dataset);
                let m = ds.lock();
                let DatatypeMessage::Reference { kind, size } = &m.datatype else {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset '{}' is no longer a reference dataset",
                        m.name
                    )));
                };
                (*kind, *size as usize)
            };
            let image = match kind {
                ReferenceKind::Object1 => ReferenceElementImage::Legacy(addr),
                ReferenceKind::Object2 => ReferenceElementImage::Inline(addr),
                other => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset {dataset} now holds {other:?} elements, not object references"
                    )))
                }
            };
            let image = encode_reference_element(&image, width, &self.ctx)?;
            let data_addr = self.local_element_block(*dataset, "object references")?;
            let at = data_addr + element * width as u64;
            self.handle.write_at(at, &image)?;
        }
        Ok(())
    }

    /// Store region references over `targets` into the elements of dataset
    /// `index` starting at `start`.
    ///
    /// Each target is the path of a dataset and a selection over it. What the
    /// element holds is a global-heap id — collection address then object index
    /// (`H5R__encode_heap`) — and the heap object it names is the target's
    /// object header address followed by the serialized selection
    /// (`H5R__encode_token_region_compat`). Both the object and the element are
    /// written here; only the address inside the object waits for
    /// [`Self::write_heap_reference_values`]. Elements never written keep the
    /// zero image libhdf5 reads back as a null reference.
    pub fn write_region_references(
        &self,
        index: usize,
        start: u64,
        targets: &[(&str, Selection)],
    ) -> IoResult<()> {
        let elements = {
            let ds = self.ds(index);
            let m = ds.lock();
            match &m.datatype {
                DatatypeMessage::Reference {
                    kind: ReferenceKind::DatasetRegion1,
                    ..
                } => {}
                other => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset '{}' has datatype {other}, not a region reference",
                        m.name
                    )))
                }
            }
            m.dataspace
                .dims
                .iter()
                .fold(1u64, |a, &d| a.saturating_mul(d))
        };
        let data_addr = self.local_element_block(index, "region references")?;
        let end = start.saturating_add(targets.len() as u64);
        if end > elements {
            return Err(crate::io::IoError::InvalidState(format!(
                "elements {start}..{end} are outside the dataset's {elements}"
            )));
        }

        // Build every heap object before inserting any: a path that names no
        // dataset, or a selection its extent does not admit, is reported at the
        // call that got it wrong rather than after half the batch is on disk.
        let sa = self.ctx.sizeof_addr as usize;
        let mut blobs = Vec::with_capacity(targets.len());
        for (path, selection) in targets {
            let target = self.region_reference_target(path)?;
            let dims = self.ds(target).lock().dataspace.dims.clone();
            validate_region_selection(selection, &dims, path)?;
            let mut blob = vec![0u8; sa];
            blob.extend_from_slice(&selection.encode()?);
            blobs.push(blob);
        }
        let items: Vec<&[u8]> = blobs.iter().map(Vec::as_slice).collect();
        let placements = self.insert_vlen_objects(&items)?;

        let width = (sa + 4) as u64;
        let mut pending = self.pending_heap_references.lock();
        for (i, &(collection, obj_index)) in placements.iter().enumerate() {
            let mut elem = Vec::with_capacity(width as usize);
            elem.extend_from_slice(&collection.to_le_bytes()[..sa]);
            elem.extend_from_slice(&u32::from(obj_index).to_le_bytes());
            self.handle
                .write_at(data_addr + (start + i as u64) * width, &elem)?;
            pending.push(PendingHeapReference {
                collection,
                index: obj_index,
                token_offset: 0,
                target: PendingHeapTarget::Dataset(targets[i].0.to_string()),
            });
        }
        Ok(())
    }

    /// Store 1.12 references over `targets` into the elements of dataset
    /// `index` starting at `start` — the `H5T_STD_REF` trio.
    ///
    /// One datatype holds all three kinds, because a 1.12 element leads with
    /// the kind it holds; which is why this takes a [`ReferenceTarget`] per
    /// element rather than a fixed kind. `H5R_OBJECT2` needs nothing but the
    /// target's address, so its element is written inline by the same finalize
    /// pass every object reference goes through. The other two encode a
    /// selection or an attribute name alongside the token, which does not fit
    /// an element, so what is stored is a global-heap blob and the element is
    /// its id (`H5T__ref_disk_write`). Elements never written keep the zero
    /// image `H5T__ref_disk_isnull` reads back as a null reference.
    pub fn write_revised_references(
        &self,
        index: usize,
        start: u64,
        targets: &[(&str, ReferenceTarget)],
    ) -> IoResult<()> {
        let (width, elements) = {
            let ds = self.ds(index);
            let m = ds.lock();
            match &m.datatype {
                DatatypeMessage::Reference {
                    kind: ReferenceKind::Object2,
                    size,
                } => (
                    *size as u64,
                    m.dataspace
                        .dims
                        .iter()
                        .fold(1u64, |a, &d| a.saturating_mul(d)),
                ),
                other => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset '{}' has datatype {other}, not the 1.12 H5T_STD_REF",
                        m.name
                    )))
                }
            }
        };
        let data_addr = self.local_element_block(index, "references")?;
        let end = start.saturating_add(targets.len() as u64);
        if end > elements {
            return Err(crate::io::IoError::InvalidState(format!(
                "elements {start}..{end} are outside the dataset's {elements}"
            )));
        }

        // Build every blob before inserting any, so a path that names nothing,
        // a selection an extent does not admit or an attribute that does not
        // exist is reported at the call that got it wrong rather than after
        // half the batch is on disk.
        let mut blobs: Vec<(u64, ReferenceKind, PendingHeapTarget, Vec<u8>)> = Vec::new();
        let mut inline: Vec<(u64, String)> = Vec::new();
        for (i, (path, target)) in targets.iter().enumerate() {
            let element = start + i as u64;
            // The rank of the extent the selection is over, which only a region
            // reference encodes and takes from the target's dataspace.
            let mut extent_rank = 0;
            let (kind, pending) = match target {
                ReferenceTarget::Object => {
                    self.object_reference_target(path)?;
                    inline.push((element, (*path).to_string()));
                    continue;
                }
                ReferenceTarget::Region(selection) => {
                    let ds = self.region_reference_target(path)?;
                    let dims = self.ds(ds).lock().dataspace.dims.clone();
                    validate_region_selection(selection, &dims, path)?;
                    extent_rank = dims.len();
                    (
                        ReferenceKind::DatasetRegion2,
                        PendingHeapTarget::Dataset((*path).to_string()),
                    )
                }
                ReferenceTarget::Attribute(name) => {
                    let scope = match self.object_reference_target(path)? {
                        Some(HardLinkTarget::Dataset(i)) => AttrScope::Dataset(i),
                        Some(HardLinkTarget::Group(i)) => AttrScope::Group(i),
                        None => AttrScope::Root,
                    };
                    if !self
                        .object_attributes(scope)?
                        .iter()
                        .any(|a| a.name() == name)
                    {
                        return Err(crate::io::IoError::NotFound(format!(
                            "attribute '{name}' of reference target '{path}'"
                        )));
                    }
                    (
                        ReferenceKind::Attr,
                        PendingHeapTarget::Object((*path).to_string()),
                    )
                }
            };
            blobs.push((
                element,
                kind,
                pending,
                encode_revised_blob(0, target, extent_rank, &self.ctx)?,
            ));
        }

        let items: Vec<&[u8]> = blobs.iter().map(|(_, _, _, b)| b.as_slice()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        let mut pending = self.pending_heap_references.lock();
        for ((element, kind, target, blob), &(collection, obj_index)) in
            blobs.iter().zip(&placements)
        {
            // The size the element declares is the heap object's own byte
            // count: `H5VL__native_blob_get` refuses to read one whose size
            // does not match what the element says.
            let image = encode_reference_element(
                &ReferenceElementImage::Blob {
                    kind: *kind,
                    size: blob.len() as u32,
                    collection,
                    index: u32::from(obj_index),
                },
                width as usize,
                &self.ctx,
            )?;
            self.handle.write_at(data_addr + element * width, &image)?;
            pending.push(PendingHeapReference {
                collection,
                index: obj_index,
                token_offset: REVISED_BLOB_TOKEN_OFFSET,
                target: target.clone(),
            });
        }
        drop(pending);

        let mut pending = self.pending_object_references.lock();
        for (element, path) in inline {
            pending.push(PendingObjectReference {
                dataset: index,
                element,
                target: path,
            });
        }
        Ok(())
    }

    /// The dataset a region reference's path names.
    ///
    /// A region reference names a *dataset*: `H5Rcreate` with
    /// `H5R_DATASET_REGION` takes the dataspace of one, and every reader
    /// dereferences it as one. A path that resolves to a group — or to the root
    /// group, which has no registry slot — is refused here rather than stored
    /// as a reference nothing can dereference.
    fn region_reference_target(&self, path: &str) -> IoResult<usize> {
        match self.object_reference_target(path)? {
            Some(HardLinkTarget::Dataset(i)) => Ok(i),
            _ => Err(crate::io::IoError::InvalidState(format!(
                "region reference target '{path}' is not a dataset"
            ))),
        }
    }

    /// Stamp every pending heap-backed reference's object with its target's
    /// object header address.
    ///
    /// The references that are still stamped rather than written once: the
    /// *element* is a global-heap id, so the heap object has to exist at the
    /// call that stores the reference, long before any address does. The object
    /// was inserted with its token zeroed, so its size does not change here:
    /// each collection is read once, patched, and rewritten at its own declared
    /// size, which leaves every element's heap id valid — and leaves the
    /// object's byte count equal to the size the 1.12 element declares, which
    /// `H5VL__native_blob_get` refuses to read past.
    fn write_heap_reference_values(&mut self) -> IoResult<()> {
        use crate::format::global_heap::GlobalHeapCollection;

        // Snapshot rather than drain, for the same reason the object-reference
        // pass does: a SWMR session finalizes twice and the close-time finalize
        // rebuilds every header at a fresh address.
        let pending: Vec<(u64, u16, usize, PendingHeapTarget)> = self
            .pending_heap_references
            .lock()
            .iter()
            .map(|p| (p.collection, p.index, p.token_offset, p.target.clone()))
            .collect();
        if pending.is_empty() {
            return Ok(());
        }
        let sa = self.ctx.sizeof_addr as usize;
        // Group by collection so one holding several references is read and
        // rewritten once.
        let mut per_collection: std::collections::BTreeMap<u64, Vec<(u16, usize, u64)>> =
            Default::default();
        for (collection, index, token_offset, target) in &pending {
            let addr = match target {
                PendingHeapTarget::Dataset(path) => {
                    let ds = self.region_reference_target(path)?;
                    self.ds(ds).lock().obj_header_addr
                }
                PendingHeapTarget::Object(path) => self.object_reference_address(path)?,
            };
            per_collection
                .entry(*collection)
                .or_default()
                .push((*index, *token_offset, addr));
        }
        for (collection, patches) in per_collection {
            // A collection is at least 4096 bytes (H5HG_MINALLOC) and most are
            // exactly that, so one read usually covers the whole image.
            let mut image = self.handle.read_at_most(collection, 4096)?;
            let declared = GlobalHeapCollection::decode_size(&image, &self.ctx)?;
            if declared > image.len() {
                image = self.handle.read_at(collection, declared)?;
            }
            let (mut gcol, _) = GlobalHeapCollection::decode(&image[..declared], &self.ctx)?;
            for (index, token_offset, addr) in patches {
                let token = gcol
                    .objects
                    .iter_mut()
                    .find(|o| o.index == index)
                    .and_then(|o| o.data.get_mut(token_offset..token_offset + sa))
                    .ok_or_else(|| {
                        crate::io::IoError::InvalidState(format!(
                            "object {index} of global heap collection {collection:#x} is no \
                             longer the reference written into it"
                        ))
                    })?;
                token.copy_from_slice(&addr.to_le_bytes()[..sa]);
            }
            let rewritten = gcol.encode_at_size(&self.ctx, declared)?;
            self.handle.write_at(collection, &rewritten)?;
        }
        Ok(())
    }

    /// Give every reference written this session its target's object header
    /// address.
    ///
    /// INVARIANT: no file is closed holding a reference whose target address is
    /// still the placeholder its write left. Both finalize paths call this in
    /// the content phase — after
    /// [`allocate_object_headers`](Self::allocate_object_headers), so every
    /// address exists, and before any object header is written — and this is
    /// the only caller of the per-kind passes, so a reference kind added later
    /// is written at both finalize sites or at neither. A target that no longer
    /// resolves fails the finalize rather than leaving a placeholder behind.
    ///
    /// This covers the two reference kinds whose value lives outside an object
    /// header. An attribute's value lives *inside* one, so it has no pass here:
    /// [`object_attributes`](Self::object_attributes) says it in addresses as
    /// the header is built.
    fn write_reference_values(&mut self) -> IoResult<()> {
        self.write_object_reference_values()?;
        self.write_heap_reference_values()
    }

    /// Append every user-created hard link whose parent group is `parent`
    /// (`None` == the root group). Called while collecting a group's links,
    /// once every object's header address has been assigned.
    fn push_hard_links(&self, links: &mut Vec<(u64, LinkMessage)>, parent: Option<usize>) {
        for link in self.hard_links_vec() {
            if link.parent != parent || !self.hard_link_emitted(&link) {
                continue;
            }
            let addr = match link.target {
                HardLinkTarget::Dataset(i) => self.ds(i).lock().obj_header_addr,
                HardLinkTarget::Group(i) => self.grp(i).lock().obj_header_addr,
            };
            links.push((link.creation_seq, LinkMessage::hard(&link.name, addr)));
        }
    }

    /// Append every user-created symbolic link whose parent group is `parent`
    /// (`None` == the root group).
    ///
    /// Nothing here waits on the layout pass — the link's value is a path, not
    /// an address — but it is collected with the rest so it takes its place in
    /// creation order and counts toward the phase change.
    fn push_symbolic_links(&self, links: &mut Vec<(u64, LinkMessage)>, parent: Option<usize>) {
        for link in self.symbolic_links_vec() {
            if link.parent != parent || !self.symbolic_link_emitted(&link) {
                continue;
            }
            links.push((
                link.creation_seq,
                LinkMessage {
                    name: link.name.clone(),
                    target: link.target.clone(),
                    creation_order: None,
                    cset: CharacterSet::for_name(&link.name),
                },
            ));
        }
    }

    /// Refuse a caller path that would have to leave this file through one of
    /// the external links a reopened file brought in.
    ///
    /// The reader follows such a path into the file the link names; the writer
    /// cannot, because it models one file and would have to write into
    /// another. Saying which link stops the path — rather than reporting the
    /// name as absent, or worse, creating a second link of that name beside
    /// it — is the whole of what write mode does here.
    pub(crate) fn reject_external_traversal(&self, path: &str) -> IoResult<()> {
        let path = path.trim_start_matches('/');
        let crossing = self.preserved_link_paths().into_iter().find(|(p, class)| {
            matches!(class, crate::io::reader::LinkClass::External { .. })
                && (path == p || path.starts_with(&format!("{p}/")))
        });
        match crossing {
            None => Ok(()),
            Some((link, crate::io::reader::LinkClass::External { file, path: target })) => {
                Err(crate::io::IoError::Unsupported(format!(
                    "'{path}' resolves through the external link '{link}' to '{target}' in \
                     '{file}'; this writer carries external links through a rewrite but does \
                     not open the file they name"
                )))
            }
            // `find` matched on the External arm, so no other class reaches here.
            Some(_) => Ok(()),
        }
    }

    /// Resolve `name` to a live dataset index, reporting *why* it does not
    /// resolve rather than collapsing every cause into absence.
    ///
    /// The write-mode counterpart of [`Hdf5Reader::open_dataset`]: the single
    /// gate every by-name dataset lookup in write mode goes through.
    ///
    /// [`Hdf5Reader::open_dataset`]: crate::io::reader::Hdf5Reader::open_dataset
    pub(crate) fn open_dataset_index(&self, name: &str) -> IoResult<usize> {
        self.reject_external_traversal(name)?;
        self.reject_preserved_object(name)?;
        self.dataset_index(name)
            .ok_or_else(|| crate::io::IoError::NotFound(name.to_string()))
    }

    /// Refuse a caller path that names an object the reopen kept by its bytes
    /// rather than modelling.
    ///
    /// Such an object is in the file and stays in it, but this writer holds
    /// none of what it would need to read or rewrite it. Saying so — with the
    /// reason the classification recorded — is the difference between an
    /// object the writer will not touch and a name the file does not have.
    pub(crate) fn reject_preserved_object(&self, path: &str) -> IoResult<()> {
        let path = path.trim_start_matches('/');
        let objects: Vec<(String, String)> = {
            let preserved = self.preserved_links.lock();
            preserved
                .iter()
                .filter_map(|l| {
                    l.reason
                        .as_ref()
                        .map(|why| (self.preserved_link_full_path(l), why.clone()))
                })
                .collect()
        };
        match objects
            .into_iter()
            .find(|(full, _)| path == full || path.starts_with(&format!("{full}/")))
        {
            None => Ok(()),
            Some((link, why)) => Err(crate::io::IoError::Unsupported(format!(
                "'{path}' is, or is inside, the object '{link}', which this file's reopen \
                 kept exactly as it found it because {why}"
            ))),
        }
    }

    /// Every link this writer will emit that names a *path* rather than an
    /// object, with the class a listing reports for it: the soft and external
    /// links created this session, and the ones a reopen is carrying through.
    ///
    /// The object listings answer for hard links, so a write-mode link
    /// listing is this plus those; keeping both sources in one place is what
    /// stops a listing from seeing a kind the class lookup does not, or the
    /// reverse.
    pub(crate) fn path_link_classes(&self) -> Vec<(String, crate::io::reader::LinkClass)> {
        let mut out: Vec<(String, crate::io::reader::LinkClass)> = self
            .symbolic_links_vec()
            .iter()
            .filter(|l| self.symbolic_link_emitted(l))
            .map(|l| {
                (
                    self.symbolic_link_full_path(l),
                    crate::io::reader::LinkClass::from_target(&l.target),
                )
            })
            .collect();
        out.extend(self.preserved_link_paths());
        out
    }

    /// Every link this writer is carrying but cannot express, by full path.
    pub(crate) fn preserved_link_paths(&self) -> Vec<(String, crate::io::reader::LinkClass)> {
        self.preserved_links
            .lock()
            .iter()
            .map(|l| (self.preserved_link_full_path(l), l.class.clone()))
            .collect()
    }

    /// The full path of a preserved link: its parent group's path plus its
    /// leaf name, in the no-leading-`/` form the registry uses.
    fn preserved_link_full_path(&self, link: &PreservedLink) -> String {
        match link.parent {
            None => link.name.clone(),
            Some(gi) => {
                let group = self.grp(gi).lock().name.clone();
                format!("{}/{}", group.trim_start_matches('/'), link.name)
            }
        }
    }

    /// The single owner of "which links does this group hold", in the order
    /// they were created and, when the file tracks creation order, stamped
    /// with it.
    ///
    /// Both the compact form (one `MSG_LINK` per link) and the dense form (the
    /// same messages inside a fractal heap) are built from this one list, so
    /// the phase-change decision, the storage it selects and the creation
    /// order recorded in either can never disagree about what the group
    /// contains.
    fn group_links(&self, scope: LinkScope, order: CreationOrder) -> Vec<LinkMessage> {
        let mut links: Vec<(u64, LinkMessage)> = Vec::new();
        match scope {
            LinkScope::Root => {
                // Datasets that belong to a subgroup are that group's links,
                // not the root's. Each group slot is locked one at a time.
                let mut datasets_in_subgroups: std::collections::HashSet<usize> =
                    std::collections::HashSet::new();
                for grp in self.group_refs() {
                    let g = grp.lock();
                    if g.deleted {
                        continue;
                    }
                    datasets_in_subgroups.extend(g.child_datasets.iter().copied());
                }
                // `dataset_refs` preserves registry order, so `enumerate`
                // yields each dataset's true index.
                for (i, ds) in self.dataset_refs().into_iter().enumerate() {
                    let m = ds.lock();
                    if m.deleted || datasets_in_subgroups.contains(&i) {
                        continue;
                    }
                    // The leaf, never the registry path: a link name is one
                    // path component, and `H5G_traverse` would split a '/'
                    // in it before `H5L_link` ever saw the name.
                    let leaf_name = m.name.rsplit('/').next().unwrap_or(&m.name);
                    links.push((
                        m.creation_seq,
                        LinkMessage::hard(leaf_name, m.obj_header_addr),
                    ));
                }
                for grp in self.group_refs() {
                    let g = grp.lock();
                    if g.deleted || g.parent.is_some() {
                        continue;
                    }
                    let leaf_name = g.name.rsplit('/').next().unwrap_or(&g.name);
                    links.push((
                        g.creation_seq,
                        LinkMessage::hard(leaf_name, g.obj_header_addr),
                    ));
                }
                self.push_hard_links(&mut links, None);
                self.push_symbolic_links(&mut links, None);
                self.push_committed_datatypes(&mut links, None);
            }
            LinkScope::Group(group_idx) => {
                // Snapshot the child lists, then drop the slot guard: the
                // per-child reads below re-lock dataset and group slots
                // (including this one).
                let (child_datasets, child_groups) = {
                    let grp = self.grp(group_idx);
                    let g = grp.lock();
                    (g.child_datasets.clone(), g.child_groups.clone())
                };
                for ds_idx in child_datasets {
                    let ds = self.ds(ds_idx);
                    let m = ds.lock();
                    if m.deleted {
                        continue;
                    }
                    let leaf_name = m.name.rsplit('/').next().unwrap_or(&m.name);
                    links.push((
                        m.creation_seq,
                        LinkMessage::hard(leaf_name, m.obj_header_addr),
                    ));
                }
                for child_idx in child_groups {
                    let child_grp = self.grp(child_idx);
                    let g = child_grp.lock();
                    if g.deleted {
                        continue;
                    }
                    let leaf_name = g.name.rsplit('/').next().unwrap_or(&g.name);
                    links.push((
                        g.creation_seq,
                        LinkMessage::hard(leaf_name, g.obj_header_addr),
                    ));
                }
                self.push_hard_links(&mut links, Some(group_idx));
                self.push_symbolic_links(&mut links, Some(group_idx));
                self.push_committed_datatypes(&mut links, Some(group_idx));
            }
        }
        // Creation order, not order by kind: a run of create_group and
        // create_dataset draws from one counter, so this is the order the
        // caller made them in. `H5G_obj_insert` numbers from zero within the
        // group, so the rank here is the link's creation order.
        links.sort_by_key(|(seq, _)| *seq);
        links
            .into_iter()
            .enumerate()
            .map(|(rank, (_, link))| {
                if order.is_tracked() {
                    link.with_creation_order(rank as i64)
                } else {
                    link
                }
            })
            .collect()
    }

    /// Whether `links` must live in dense storage rather than in the group's
    /// object header — the `H5G_obj_insert` phase-change rule, applied to the
    /// whole set at once because this writer builds each header from scratch
    /// rather than inserting one link at a time.
    ///
    /// libhdf5 converts when the count *reaches* `max_compact` and another
    /// link arrives, so a set of exactly `max_compact` is still compact; and
    /// separately when one message would not fit the 16-bit size field an
    /// object header message has.
    ///
    /// The answer depends only on the link names and kinds, never on the
    /// addresses they point at, which is what lets a group header be sized
    /// before [`prepare_dense_links`](Self::prepare_dense_links) has run.
    fn links_need_dense(&self, links: &[LinkMessage]) -> bool {
        links.len() > MAX_COMPACT_LINKS
            || links
                .iter()
                .any(|l| l.encode(&self.ctx).len() > MAX_MESSAGE_SIZE)
    }

    /// The single owner of link emission into a group object header: the Link
    /// Info and Group Info messages, and then either one `MSG_LINK` per link
    /// or nothing at all when the set has spilled to dense storage.
    ///
    /// The two storage forms are exclusive (`H5G_obj_insert` moves the whole
    /// set at once), and a header carrying both would report every link twice.
    ///
    /// A group whose links are dense but not yet laid out gets a compact Link
    /// Info message here. That is deliberate: the message encodes to the same
    /// length either way — two addresses, defined or not — so the sizing pass
    /// that runs before `prepare_dense_links` still reserves the right number
    /// of bytes, and the write pass that runs after it emits the real heap and
    /// index addresses. It is the same two-pass rule the child link addresses
    /// already follow.
    fn emit_links(
        &self,
        header: &mut ObjectHeader,
        scope: LinkScope,
        links: &[LinkMessage],
        order: CreationOrder,
    ) {
        // A symbol-table group holds no link messages at all: its links are the
        // entries of the symbol table `prepare_symbol_tables` laid out, and
        // the header carries only the two addresses naming it. Link Info and
        // Group Info are version-1.8 messages and have no business in a
        // version-1 header — `H5G__stab_valid` reads the Symbol Table message
        // and nothing else.
        if self.uses_symbol_table(scope, order) {
            // Sizing runs before the tables are laid out; the message is the
            // same two addresses wide either way, so the placeholder reserves
            // exactly what the real one needs. Same two-pass rule the child
            // link addresses already follow.
            let stab = self
                .symbol_tables
                .written
                .lock()
                .get(&scope)
                .copied()
                .unwrap_or(Stab {
                    btree_addr: UNDEF_ADDR,
                    heap_addr: UNDEF_ADDR,
                });
            header.add_message(MSG_SYMBOL_TABLE, 0x00, stab.encode(&self.ctx));
            return;
        }
        // Links a reopen carried through verbatim because this writer cannot
        // express them. They are emitted here rather than by a second caller
        // so that no header-rewrite path can drop them, and their presence
        // pins the group to compact storage: dense storage would have to
        // re-encode each link into the heap, which is exactly the byte
        // fidelity preserving them is for.
        let preserved = self.preserved_links_for(scope);
        let dense = preserved.is_empty() && self.links_need_dense(links);
        let link_info = self.dense_links.lock().get(&scope).cloned();
        let link_info = link_info.unwrap_or_else(|| {
            let mut info = LinkInfoMessage::compact();
            if order.is_tracked() {
                // `H5G__obj_insert` post-increments `max_corder`, so a group
                // holding n links reports n.
                info.max_creation_order = Some(links.len() as u64);
            }
            if order.is_indexed() {
                // The index address stays undefined while the links live in
                // the header, but the message must still carry the field:
                // `H5Pget_link_creation_order` reads INDEXED off this flag,
                // not off the address.
                info.creation_order_btree_address = Some(UNDEF_ADDR);
            }
            info
        });
        header.add_message(MSG_LINK_INFO, 0x00, link_info.encode(&self.ctx));
        // The link info message takes no flags and the group info message
        // takes `H5O_MSG_FLAG_CONSTANT`, exactly as `H5G__obj_create_real`
        // creates the pair (H5Gobj.c:255, :259) and as
        // `H5G__obj_insert`'s phase change re-creates it (H5Gobj.c:526). The
        // asymmetry is real: the link info message records the group's
        // storage and its creation-order counter, both of which change as
        // links come and go, while the group info message holds the phase
        // change and estimated-name-length constants of the creation property
        // list, which nothing after creation rewrites.
        header.add_message(
            MSG_GROUP_INFO,
            MSG_FLAG_CONSTANT,
            GroupInfoMessage::default().encode(),
        );
        if dense {
            return;
        }
        for link in links {
            header.add_message(MSG_LINK, 0x00, link.encode(&self.ctx));
        }
        for encoded in preserved {
            header.add_message(MSG_LINK, 0x00, encoded);
        }
    }

    /// The verbatim link bodies a reopen carried into `scope`.
    fn preserved_links_for(&self, scope: LinkScope) -> Vec<Vec<u8>> {
        let parent = match scope {
            LinkScope::Root => None,
            LinkScope::Group(i) => Some(i),
        };
        self.preserved_links
            .lock()
            .iter()
            .filter(|l| l.parent == parent)
            .map(|l| l.encoded.clone())
            .collect()
    }

    /// Lay out and write dense link storage for every group that needs it,
    /// recording the resulting `Link Info` message per group.
    ///
    /// The sole owner of that transition. It must run after every object
    /// header address is assigned — the heap holds encoded link messages, and
    /// those name their targets — and before any group header is written.
    ///
    /// Every group whose header this finalize rewrites passes through here,
    /// dense or not: the storage a reopened header named is superseded by the
    /// rewrite whichever form the new link set takes, and freeing it first is
    /// what lets the replacement reuse those blocks.
    fn prepare_dense_links(&self) -> IoResult<()> {
        let mut scopes: Vec<(LinkScope, Vec<LinkMessage>, CreationOrder)> = Vec::new();
        for gi in 0..self.group_count() {
            let (deleted, order) = {
                let grp = self.grp(gi);
                let g = grp.lock();
                (g.deleted, g.track_order.links)
            };
            // A symbol-table group is `prepare_symbol_tables`' business; it
            // has no Link Info message to hold a fractal heap address, and it
            // never had dense storage to release.
            if deleted || self.uses_symbol_table(LinkScope::Group(gi), order) {
                continue;
            }
            self.release_superseded_dense_links(LinkScope::Group(gi))?;
            let links = self.group_links(LinkScope::Group(gi), order);
            if self.links_need_dense(&links) {
                scopes.push((LinkScope::Group(gi), links, order));
            }
        }
        let root_order = self.root_track_order.links;
        if !self.uses_symbol_table(LinkScope::Root, root_order) {
            self.release_superseded_dense_links(LinkScope::Root)?;
            let root_links = self.group_links(LinkScope::Root, root_order);
            if self.links_need_dense(&root_links) {
                scopes.push((LinkScope::Root, root_links, root_order));
            }
        }

        for (scope, links, order) in scopes {
            // `close` after `start_swmr` finalizes a second time over the same
            // groups, so rebuilding here would allocate a whole second heap
            // and strand the one the published headers already name.
            if self.dense_links.lock().contains_key(&scope) {
                continue;
            }
            let dense = build_dense_links(&links, &self.ctx, order, &mut |len| {
                self.allocator.allocate(len, FreeSpaceClass::Metadata)
            })?;
            for block in &dense.blocks {
                self.handle.write_at(block.addr, &block.image)?;
            }
            self.dense_links.lock().insert(scope, dense.linfo);
        }
        Ok(())
    }

    /// Lay out whichever of the two forms of link storage this file uses,
    /// before any group header is written.
    ///
    /// The two are exclusive because the formats are: a classic group has no
    /// Link Info message to put a fractal heap address in, and a link-message
    /// group has no symbol table.
    fn prepare_link_storage(&self) -> IoResult<()> {
        self.prepare_dense_links()?;
        self.prepare_symbol_tables()
    }

    /// Lay out and write the symbol table of every classic group, and free the
    /// storage each rewrite supersedes. A no-op on a link-message file.
    ///
    /// The classic counterpart of [`prepare_dense_links`](Self::prepare_dense_links),
    /// and the sole owner of that transition. The same two placement rules
    /// apply for the same two reasons: it runs after every object header has
    /// an address, because a symbol table entry names its target's header, and
    /// before any group header is written, because the header carries the
    /// Symbol Table message naming what this laid out.
    ///
    /// Deepest group first, root last. A hard link to a group caches that
    /// group's own B-tree and heap in the entry's scratch pad
    /// (`H5G__link_to_ent`), so the child's table must exist before the
    /// parent's is built; `H5G__stab_valid` checks the root entry's cache
    /// against the root header's Symbol Table message, so a stale pair there
    /// is not a slow lookup but a file `H5Fopen` rejects.
    ///
    /// Every classic group is rebuilt on every pass — there is no "already
    /// done" short-circuit like the dense one, because the only way this runs
    /// twice is a `Drop` retry after a failed `close`, and the entries of the
    /// first pass name header addresses the second pass has moved. (A SWMR
    /// session, the other double-finalize, cannot reach here: SWMR needs a
    /// version-3 superblock, so `start_swmr` refuses a classic file.)
    fn prepare_symbol_tables(&self) -> IoResult<()> {
        // Depth by parent chain, not by counting separators in the registry
        // path: the chain is what actually says which table has to exist first.
        let mut scopes: Vec<(usize, LinkScope, CreationOrder)> = Vec::new();
        for gi in 0..self.group_count() {
            let (deleted, order, mut parent) = {
                let grp = self.grp(gi);
                let g = grp.lock();
                (g.deleted, g.track_order.links, g.parent)
            };
            if deleted || !self.uses_symbol_table(LinkScope::Group(gi), order) {
                continue;
            }
            let mut depth = 1usize;
            while let Some(p) = parent {
                depth += 1;
                parent = self.grp(p).lock().parent;
            }
            scopes.push((depth, LinkScope::Group(gi), order));
        }
        scopes.sort_by_key(|&(depth, ..)| std::cmp::Reverse(depth));
        let root_order = self.root_track_order.links;
        if self.uses_symbol_table(LinkScope::Root, root_order) {
            scopes.push((0, LinkScope::Root, root_order));
        }

        let meta = self.stab_meta();
        for (_, scope, order) in scopes {
            // Freed before the replacement is laid out, so a rewrite reuses
            // the same blocks instead of growing the file on every open/close
            // cycle — the rule `prepare_dense_links` and the header rewrite
            // already follow. Removed as it is freed, so no second pass can
            // free it twice.
            let superseded = self.symbol_tables.superseded.lock().remove(&scope);
            if let Some(extents) = superseded {
                free_stab(&self.allocator, &extents);
            }
            let links = self.stab_links_for(scope, order)?;
            let stab = write_stab(&self.handle, &self.allocator, &meta, &links)?;
            self.symbol_tables.written.lock().insert(scope, stab);
        }
        Ok(())
    }

    /// The file-level parameters every symbol-table node width is derived from
    /// — the address/length widths and the B-tree "K" ranks. Only a version-0/1
    /// superblock records ranks of its own; [`btree_v1_config`] is the one
    /// place that decides whether this file has any.
    ///
    /// [`btree_v1_config`]: Self::btree_v1_config
    fn stab_meta(&self) -> FileMeta {
        FileMeta {
            ctx: self.ctx,
            btree: self.btree_v1_config(),
            sohm: None,
        }
    }

    /// `scope`'s links as symbol table entries.
    ///
    /// A link a reopen carried through verbatim is decoded back out of its
    /// encoded Link message here, because a classic group has no link message
    /// to preserve it into. Nothing is lost in the round trip: the walk built
    /// that message from a symbol table entry in the first place, and the two
    /// forms carry the same three facts.
    fn stab_links_for(&self, scope: LinkScope, order: CreationOrder) -> IoResult<Vec<StabLink>> {
        let groups = self.group_header_scopes();
        let mut out = Vec::new();
        for link in self.group_links(scope, order) {
            out.push(self.stab_link(&link, &groups)?);
        }
        for encoded in self.preserved_links_for(scope) {
            let (link, _) = LinkMessage::decode(&encoded, &self.ctx)?;
            out.push(self.stab_link(&link, &groups)?);
        }
        Ok(out)
    }

    /// Where each group's object header now sits, so a hard link that lands on
    /// one can cache that group's symbol table in its scratch pad.
    fn group_header_scopes(&self) -> HashMap<u64, LinkScope> {
        let mut map = HashMap::new();
        for gi in 0..self.group_count() {
            let grp = self.grp(gi);
            let g = grp.lock();
            if !g.deleted {
                map.insert(g.obj_header_addr, LinkScope::Group(gi));
            }
        }
        map
    }

    /// One link as a symbol table entry.
    ///
    /// The scratch pad caches the target group's B-tree and heap when the
    /// target is a group this pass has already laid out — what
    /// `H5G__link_to_ent` does, and what lets `H5G__stab_lookup` walk a path
    /// without opening each header on the way. For anything else the pad stays
    /// `H5G_NOTHING_CACHED`, the value libhdf5 itself writes whenever the
    /// target has no Symbol Table message to read.
    fn stab_link(
        &self,
        link: &LinkMessage,
        groups: &HashMap<u64, LinkScope>,
    ) -> IoResult<StabLink> {
        let target = match &link.target {
            LinkTarget::Hard { address } => {
                let cached = groups
                    .get(address)
                    .and_then(|scope| self.symbol_tables.written.lock().get(scope).copied());
                StabTarget::Hard {
                    addr: *address,
                    cached,
                }
            }
            LinkTarget::Soft { target } => StabTarget::Soft {
                value: target.clone(),
            },
            // Unreachable by construction: a group holding one of these is
            // not a symbol-table group at all
            // ([`LinkMessage::fits_symbol_table`] is what
            // [`Hdf5Writer::uses_symbol_table`] asks), so this pass never
            // visits it. Reported rather than panicked so a future caller
            // that skips that gate learns which link it lost.
            LinkTarget::External { .. } | LinkTarget::UserDefined { .. } => {
                return Err(crate::io::IoError::InvalidState(format!(
                    "cannot store the link {:?} in a symbol table: it holds only \
                     hard and soft links, and this group was not converted to link \
                     messages the way `H5G_obj_insert` converts it",
                    link.name
                )))
            }
        };
        Ok(StabLink {
            name: link.name.clone(),
            target,
        })
    }

    /// The single owner of attribute emission into an object header: appends
    /// the Attribute Info message and then one `MSG_ATTRIBUTE` per attribute.
    ///
    /// On a version-2 object header the two are inseparable.
    /// `H5O__attr_count_real` derives `H5Oget_info().num_attrs` from the
    /// Attribute Info message alone — with no such message the count reads as
    /// zero however many attribute messages follow, which is what made every
    /// rust-written file report `num_attrs == 0` to libhdf5 while
    /// `H5Aiterate2` still yielded the attributes. The message carries no
    /// count of its own: `H5A__get_ainfo` fills `nattrs` from the attribute
    /// messages the header loader actually saw, so compact storage needs
    /// nothing but the message's presence.
    ///
    /// When [`prepare_dense_attributes`](Self::prepare_dense_attributes) has
    /// spilled `scope`'s attributes to a fractal heap, the same message names
    /// that heap instead and *no* attribute message follows: the two storage
    /// forms are exclusive (`H5O__attr_create` moves the whole set at once),
    /// and a header carrying both would report every attribute twice.
    fn emit_attributes(
        &self,
        header: &mut ObjectHeader,
        scope: AttrScope,
        attributes: &[AttributeEntry],
        order: CreationOrder,
        format: ObjectFormat,
        owner: ShareOwner,
    ) {
        // `H5Pget_attr_creation_order` reads the object header's own flags,
        // not the Attribute Info message, so this is what makes the object
        // report creation-ordered attributes — and tracking widens every
        // message envelope by the creation index below.
        let order = self.header_attr_order(order);
        header.set_attribute_creation_order(order);
        if attributes.is_empty() {
            return;
        }
        // A version-1 object header gets the attribute messages alone.
        // `H5O__attr_create` gates every mention of the Attribute Info message
        // on `oh->version > H5O_VERSION_1` (H5Oattribute.c:218), and so does
        // `H5O__attr_count_real`, which is why the count still reads correctly
        // without it: on a version-1 header libhdf5 counts the messages.
        if format == ObjectFormat::Legacy {
            for attr in attributes {
                header.add_message(MSG_ATTRIBUTE, 0x00, self.encode_attribute(attr));
            }
            return;
        }
        // Whether the set spills is a property of the set alone, so it is the
        // same answer in the pass that measures this header and in the pass
        // that writes it — even though the storage itself is laid out between
        // the two, because it can only be laid out once every object header
        // has an address. Sizing therefore falls back to a placeholder message
        // of the same width: only the creation-order flags change the
        // Attribute Info message's length, so the header measured here holds
        // the header written against the storage that replaces it. Same
        // two-pass rule `emit_links` follows for dense links and symbol
        // tables.
        let dense = self.attributes_need_dense(attributes, format);
        let stored = self.dense_attributes.lock().get(&scope).cloned();
        let ainfo = stored.unwrap_or_else(|| {
            let mut ainfo = AttributeInfoMessage::compact();
            if order.is_tracked() {
                ainfo.max_creation_index = Some(next_creation_index(attributes));
            }
            if order.is_indexed() {
                // Compact storage has no index B-tree, but the message still
                // announces one so that its flags match the header's
                // (`H5O__attr_create` asserts they agree).
                ainfo.creation_order_btree_address = Some(UNDEF_ADDR);
            }
            ainfo
        });
        header.add_message(MSG_ATTR_INFO, MSG_FLAG_DONTSHARE, ainfo.encode(&self.ctx));
        if dense {
            return;
        }
        // Each attribute states its own creation index — the one it was
        // created with here, or the one the file it was read from records. An
        // attribute with none belongs to an object that tracks no order, where
        // the field is not encoded at all.
        for attr in attributes {
            let (flags, body) = self.share_attribute(attr, format, owner);
            header.add_message_indexed(
                MSG_ATTRIBUTE,
                flags,
                body,
                attr.creation_index().unwrap_or(0),
            );
        }
    }

    /// One attribute message body, at the version this file's low library
    /// bound calls for (`H5A__set_version`, which reads the bound and nothing
    /// about the object the attribute hangs on).
    fn encode_attribute(&self, attr: &AttributeEntry) -> Vec<u8> {
        attr.encode_for(&self.ctx, self.encoding_libver(), self.message_format())
    }

    /// What a header stores for one attribute: the message flags and the body,
    /// with the attribute's own datatype and dataspace shared wherever an
    /// index covers them.
    ///
    /// `H5A__create` offers both to `H5SM_try_share` (H5Aint.c:375-377) before
    /// `H5O__attr_create` offers the attribute itself (H5Oattribute.c:726), so
    /// the attribute body that reaches the heap already holds their pointers
    /// and says which fields they are in its own flags byte
    /// (`H5O_ATTR_FLAG_TYPE_SHARED` / `H5O_ATTR_FLAG_SPACE_SHARED`,
    /// H5Oattr.c:358-359). Both offers go through
    /// [`share_message`](Self::share_message) like any other, so the pass that
    /// counts references and the pass that substitutes see the same three
    /// messages.
    fn share_attribute(
        &self,
        attr: &AttributeEntry,
        format: ObjectFormat,
        owner: ShareOwner,
    ) -> (u8, Vec<u8>) {
        let libver = self.encoding_libver();
        // Only a readable attribute has pieces to offer: an unreadable one is
        // the bytes it was read from, put back as they were. Version 1 has no
        // flags byte to record a shared field in — `H5O__attr_encode` writes a
        // reserved zero there — so a classic file shares the attribute whole
        // or not at all.
        let Some(message) = attr.readable().filter(|_| format.attribute_version() >= 2) else {
            return self.share_message(
                owner,
                MSG_ATTRIBUTE,
                0x00,
                attr.encode_for(&self.ctx, libver, format),
            );
        };

        let datatype = message.datatype.encode_at(&self.ctx, libver);
        let dataspace = message.dataspace.encode_for(&self.ctx, format);
        // `H5A__create` passes no open header for either (H5Aint.c:375-377):
        // both live inside the attribute's body, so neither has a header
        // message a `H5SM_IN_OH` record could name and both reach the heap on
        // first use.
        let (dt_flags, dt_field) =
            self.share_message(ShareOwner::Detached, MSG_DATATYPE, 0x00, datatype.clone());
        let (ds_flags, ds_field) =
            self.share_message(ShareOwner::Detached, MSG_DATASPACE, 0x00, dataspace.clone());

        let mut attr_flags = 0u8;
        if dt_flags & MSG_FLAG_SHARED != 0 {
            attr_flags |= ATTR_FLAG_TYPE_SHARED;
        }
        if ds_flags & MSG_FLAG_SHARED != 0 {
            attr_flags |= ATTR_FLAG_SPACE_SHARED;
        }
        let encoded = message.encode_with_fields(attr_flags, &dt_field, &ds_field);

        // Each shared field's heap ID sits two bytes into the pointer that
        // replaced it; the body offered below carries whatever
        // `share_message` just produced, which is a zeroed ID in the pass that
        // counts and the real one in the pass that substitutes.
        let mut nested = Vec::new();
        if attr_flags & ATTR_FLAG_TYPE_SHARED != 0 {
            nested.push(NestedShare {
                heap_id_at: encoded.datatype_at + SOHM_POINTER_HEAP_ID_AT,
                target: (MSG_DATATYPE, datatype),
            });
        }
        if attr_flags & ATTR_FLAG_SPACE_SHARED != 0 {
            nested.push(NestedShare {
                heap_id_at: encoded.dataspace_at + SOHM_POINTER_HEAP_ID_AT,
                target: (MSG_DATASPACE, dataspace),
            });
        }
        self.share_nesting_message(owner, MSG_ATTRIBUTE, 0x00, encoded.body, nested)
    }

    /// Whether `attributes` must live in dense storage rather than in the
    /// object header — the `H5O__attr_create` phase-change rule, applied to
    /// the whole set at once because this writer builds each header from
    /// scratch rather than inserting one attribute at a time.
    ///
    /// libhdf5 converts when the count *reaches* `max_compact` and another
    /// attribute arrives, so a set of exactly `max_compact` is still compact;
    /// and separately when one message would not fit the 16-bit size field an
    /// object header message has.
    ///
    /// Never in a classic file. Dense attribute storage is a fractal heap
    /// reached through an Attribute Info message, both introduced in the 1.8
    /// format; at `H5F_LIBVER_EARLIEST` libhdf5 keeps every attribute in the
    /// header however many there are (`H5O__attr_create` reaches the phase
    /// change only when the object header version allows it). An attribute
    /// too large for the 16-bit size field is then an error, which
    /// `ObjectHeader::encode_v1` raises, rather than a reason to spill.
    fn attributes_need_dense(&self, attributes: &[AttributeEntry], format: ObjectFormat) -> bool {
        if format == ObjectFormat::Legacy {
            return false;
        }
        attributes.len() > MAX_COMPACT_ATTRS
            || attributes
                .iter()
                .any(|a| self.encode_attribute(a).len() > MAX_MESSAGE_SIZE)
    }

    /// Every object whose attributes this finalize re-lays-out, with the
    /// creation-order policy each one's storage must follow.
    ///
    /// `datasets` lists the datasets whose headers this finalize will
    /// actually write. A reopened dataset that took no writes keeps its
    /// original header — and with it whatever storage that header already
    /// names — so touching its attribute storage would strand every block of
    /// it.
    ///
    /// The policy is the one the *header* records, not the one the object's
    /// creation property list asked for: those differ on a file whose
    /// shared-message configuration covers attributes, where
    /// [`header_attr_order`](Self::header_attr_order) raises every object to
    /// tracked. Storage laid out against the property list would then omit the
    /// creation indices the header says are there — and, since the Attribute
    /// Info message carries a maximum creation index only when tracked, would
    /// be two bytes shorter than the message the sizing pass measured.
    fn attribute_scopes(&self, datasets: &[usize]) -> Vec<(AttrScope, CreationOrder)> {
        let order_of = |requested| self.header_attr_order(requested);
        let mut scopes = vec![(AttrScope::Root, order_of(self.root_track_order.attrs))];
        for gi in 0..self.group_count() {
            if self.grp(gi).lock().deleted {
                continue;
            }
            let order = self.grp(gi).lock().track_order.attrs;
            scopes.push((AttrScope::Group(gi), order_of(order)));
        }
        for &i in datasets {
            let order = self.ds(i).lock().track_attr_order;
            scopes.push((AttrScope::Dataset(i), order_of(order)));
        }
        scopes
    }

    /// Lay out and write dense attribute storage for every object that needs
    /// it, recording the resulting `Attribute Info` message per object.
    ///
    /// The sole owner of that transition. It runs after every object header
    /// has an address — an attribute may hold an object reference, and the
    /// heap holds the encoded attribute messages — and before any object
    /// header is written, because the header carries the Attribute Info
    /// message naming what this laid out. Every block is on disk before the
    /// map naming it is populated, so a header written from that map can only
    /// point at bytes that exist. The same placement rule, for the same two
    /// reasons, as [`prepare_dense_links`](Self::prepare_dense_links).
    ///
    /// Which objects spill is not decided here: `emit_attributes` asks
    /// [`attributes_need_dense`](Self::attributes_need_dense) itself, so the
    /// header measured before this ran and the header written after it agree
    /// without either consulting the other.
    fn prepare_dense_attributes(&self, datasets: &[usize]) -> IoResult<()> {
        for (scope, order) in self.attribute_scopes(datasets) {
            // Every scope here has its header rewritten, so the storage a
            // reopen found on it is superseded whether or not the new set is
            // dense again — a free driven by "the new set needs a heap" would
            // never reach an object that dropped back to compact. Freed
            // immediately before its replacement is laid out, so the rewrite
            // lands in the blocks it just gave back instead of growing the
            // file on every open/close cycle.
            self.release_superseded_dense_attrs(scope)?;
            // `close` after `start_swmr` finalizes a second time over the same
            // attribute sets — SWMR refuses every attribute mutation — so
            // rebuilding here would allocate a whole second heap and strand
            // the one the published headers already name.
            if self.dense_attributes.lock().contains_key(&scope) {
                continue;
            }
            let attributes = self.object_attributes(scope)?;
            if !self.attributes_need_dense(&attributes, self.attr_scope_format(scope)) {
                continue;
            }
            let dense = build_dense_attributes(&attributes, &self.ctx, order, &mut |len| {
                self.allocator.allocate(len, FreeSpaceClass::Metadata)
            })?;
            for block in &dense.blocks {
                self.handle.write_at(block.addr, &block.image)?;
            }
            self.dense_attributes.lock().insert(scope, dense.ainfo);
        }
        Ok(())
    }

    /// The object header format `scope`'s owner is written at, which is what
    /// decides whether its attributes may spill at all.
    fn attr_scope_format(&self, scope: AttrScope) -> ObjectFormat {
        match scope {
            AttrScope::Root => self.header_format(self.root_track_order),
            AttrScope::Group(gi) => self.group_header_format(gi),
            AttrScope::Dataset(i) => self.dataset_header_format(i),
        }
    }

    /// Whether a dataset's datatype message may be offered to a
    /// shared-message index at all.
    ///
    /// The datatype is the one message class carrying a `can_share` callback
    /// (`H5O__dtype_can_share`, H5Odtype.c:99), and `H5SM__can_share_common`
    /// asks it before any index is consulted (H5SM.c:895-899). It refuses an
    /// immutable type and a committed one (H5Odtype.c:1893-1901); the
    /// committed half is already answered by address at the call site.
    ///
    /// A dataset's type reaches that predicate still immutable only when
    /// `H5D__init_type` kept the caller's own `H5T_t` rather than copying it,
    /// which it does exactly when the type is immutable, is not relocatable,
    /// and the low bound this dataset's messages are written at is below
    /// `H5F_LIBVER_V18` (H5Dint.c:569-572) — the bound the dataset was
    /// *created* under, which for a dataset a reopen found is not this
    /// session's.
    /// Any of the three failing produces an `H5T_COPY_ALL` copy, which is
    /// `H5T_STATE_RDONLY` rather than immutable (H5T.c:4461-4462) and so is
    /// shareable — which is why `H5Tcopy(H5T_STD_I32LE)` shares where
    /// `H5T_STD_I32LE` itself does not (tests/fixtures/gen_sohm.c).
    ///
    /// An attribute has no such branch: `H5A__create` copies unconditionally
    /// (H5Aint.c:341), so its datatype is always eligible and
    /// [`share_attribute`](Self::share_attribute) offers it without asking.
    fn dataset_datatype_shareable(&self, datatype: &DatatypeMessage, libver: LibverBound) -> bool {
        !datatype.is_predefined() || datatype.is_relocatable() || libver >= LibverBound::V18
    }

    /// Whether the first copy of a `msg_type` message may stay literal in the
    /// object header that writes it.
    ///
    /// `H5O_msg_can_share_in_ohdr` reads the class's `H5O_SHARE_IN_OHDR` flag
    /// (H5Omessage.c:1426); the five classes that carry it are datatype
    /// (H5Odtype.c:89), dataspace (H5Osdspace.c:61), both fill value messages
    /// (H5Ofill.c:106 and :130) and the filter pipeline (H5Opline.c:65). The
    /// attribute class does not, which is why an attribute reaches the heap on
    /// its first use.
    const fn shares_in_ohdr(msg_type: u8) -> bool {
        matches!(
            msg_type,
            MSG_DATASPACE
                | MSG_DATATYPE
                | MSG_FILL_VALUE
                | MSG_FILL_VALUE_OLD
                | MSG_FILTER_PIPELINE
        )
    }

    /// What a header stores for a message a shared-message index may cover:
    /// the body itself, or a pointer into the shared-message heap.
    ///
    /// The single point at which a message is offered to an index. Every
    /// header builder routes its shareable messages through here, so the pass
    /// that counts references and the pass that substitutes pointers walk
    /// exactly the same set — the counting and the substituting cannot drift
    /// apart, because they are one call site in two phases.
    ///
    /// `owner` is `H5SM_try_share`'s `open_oh`: the header this message
    /// belongs to, or [`ShareOwner::Detached`] for a body that is part of
    /// another message rather than a message of a header.
    ///
    /// Outside a finalize, and in any file created without indexes, this is
    /// the identity.
    fn share_message(
        &self,
        owner: ShareOwner,
        msg_type: u8,
        flags: u8,
        body: Vec<u8>,
    ) -> (u8, Vec<u8>) {
        self.share_nesting_message(owner, msg_type, flags, body, Vec::new())
    }

    /// [`share_message`](Self::share_message) for a body that itself holds
    /// shared-message pointers.
    ///
    /// `nested` names each heap ID inside `body`, which is zero until the
    /// table is laid out. Two bodies that differ only in what they point at
    /// are the same bytes here and different bytes on disk, so the count and
    /// the substitute are keyed on the pair.
    fn share_nesting_message(
        &self,
        owner: ShareOwner,
        msg_type: u8,
        flags: u8,
        body: Vec<u8>,
        nested: Vec<NestedShare>,
    ) -> (u8, Vec<u8>) {
        let Some(sohm) = self.sohm.as_deref() else {
            return (flags, body);
        };
        // A message already carrying a pointer — a committed datatype — is
        // shared by address and must not be shared again, and the message
        // classes libhdf5 marks `H5O_MSG_FLAG_DONTSHARE` never reach an index.
        if flags & (MSG_FLAG_SHARED | MSG_FLAG_DONTSHARE) != 0 {
            return (flags, body);
        }
        let Some(index) = sohm.index_for(msg_type, body.len()) else {
            return (flags, body);
        };
        // `share_in_ohdr && open_oh` (H5SM.c:1400): the first copy of one of
        // these classes stays where it was written, marked shareable, and only
        // a second use moves the body to the heap.
        let ohdr = match owner {
            ShareOwner::Header(addr) if Self::shares_in_ohdr(msg_type) => Some(addr),
            _ => None,
        };
        // What a pointer to this body looks like: a zeroed heap ID until the
        // table exists, which is the width the real one has.
        let pointer = |id| {
            (
                flags | MSG_FLAG_SHARED,
                SharedMessagePointer::encode_sohm(id),
            )
        };
        match &mut *sohm.phase.lock() {
            SohmPhase::Idle => (flags, body),
            SohmPhase::Predict(first) => {
                if ohdr.is_some() && first.insert((msg_type, body.clone())) {
                    return (flags | MSG_FLAG_SHAREABLE, body);
                }
                pointer([0u8; SOHM_HEAP_ID_LEN])
            }
            // The same substitution `Predict` makes, so that what the collect
            // pass builds around a shared message is the width the resolve
            // pass will build — which is what lets an attribute body assembled
            // in this pass be the body assembled in that one, bar the heap IDs
            // it is here recording a need for.
            SohmPhase::Collect(collector) => {
                let first = collector.record(index, msg_type, &body, &nested, ohdr);
                if !first && !nested.is_empty() {
                    // This body is already here, so the pointers it holds
                    // already exist in the heap and the offers that built
                    // this copy of it must not count a second time.
                    for share in &nested {
                        collector.release(share.target.0, &share.target.1);
                    }
                }
                if ohdr.is_some() && first {
                    return (flags | MSG_FLAG_SHAREABLE, body);
                }
                pointer([0u8; SOHM_HEAP_ID_LEN])
            }
            SohmPhase::Resolve { ids, first } => {
                if ohdr.is_some() && first.insert((msg_type, body.clone())) {
                    return (flags | MSG_FLAG_SHAREABLE, body);
                }
                let key = (msg_type, body);
                match ids.get(&key) {
                    Some(&id) => pointer(id),
                    // The collect pass never saw this body — a dataspace a
                    // SWMR extend changed after the table was laid out, say.
                    // Left literal, which leaves a heap object counted for one
                    // reference more than reaches it and nothing else.
                    None => (flags, key.1),
                }
            }
        }
    }

    /// Answer every shareable message at a heap pointer's width for the rest
    /// of this finalize's allocation phase.
    ///
    /// Half of the bracket [`prepare_shared_messages`](Self::prepare_shared_messages)
    /// closes, and the reason the two can sit on opposite sides of the
    /// allocation: a header cannot be measured until it is known which of its
    /// messages are pointers, and a body cannot be counted until every address
    /// it names exists. Only the width is knowable in the first phase, and the
    /// width is all the measurement needs.
    ///
    /// A finalize that will not lay a table out — a `finalize_for_swmr`, a
    /// second finalize over a table already published — leaves the phase where
    /// it found it, so what that pass measures is what it writes.
    fn begin_shared_message_layout(&self) {
        let Some(sohm) = self.sohm.as_deref() else {
            return;
        };
        let mut phase = sohm.phase.lock();
        if matches!(*phase, SohmPhase::Idle) && sohm.table_addr.lock().is_none() {
            *phase = SohmPhase::Predict(FirstCopies::default());
        }
    }

    /// Lay out the file's shared-message table: count the bodies every header
    /// this finalize writes would share, put them in their index's heap, and
    /// arm the substitution the header builders then apply.
    ///
    /// The sole owner of the transition to `Resolve`. It runs last in the
    /// content phase, after
    /// [`prepare_dense_attributes`](Self::prepare_dense_attributes),
    /// [`prepare_link_storage`](Self::prepare_link_storage) and
    /// [`write_reference_values`](Self::write_reference_values), because a
    /// body is only counted once it is the body the file will hold: an
    /// attribute that spilled into dense storage is not in a header to be
    /// shared at all, and one holding an object reference says an object
    /// header address that exists only after the allocation phase. Counting
    /// either of them earlier would count a body no header ends up carrying,
    /// and leave the header that carries the real one literal — which
    /// [`check_header_size`] would then refuse, the block having been
    /// reserved at a pointer's width.
    ///
    /// Once per file: a second finalize (a SWMR session's close) keeps the
    /// table the first one published rather than allocating a second one and
    /// stranding the first.
    fn prepare_shared_messages(&self, datasets: &[usize]) -> IoResult<()> {
        let Some(sohm) = self.sohm.as_deref() else {
            return Ok(());
        };
        if sohm.table_addr.lock().is_some() {
            return Ok(());
        }

        // Collect: build every header this finalize will write and throw it
        // away, keeping only what its shareable messages were.
        *sohm.phase.lock() = SohmPhase::Collect(SohmCollector::new(sohm.indexes.len()));
        for &i in datasets {
            self.build_dataset_header(i)?;
        }
        for gi in 0..self.group_count() {
            if self.grp(gi).lock().deleted {
                continue;
            }
            self.build_group_header(gi)?;
        }
        self.build_root_group_header()?;
        let SohmPhase::Collect(collector) =
            std::mem::replace(&mut *sohm.phase.lock(), SohmPhase::Idle)
        else {
            return Err(crate::io::IoError::InvalidState(
                "the shared-message collect pass did not finish in the collect phase".into(),
            ));
        };

        let indexes: Vec<SohmIndexContent> = sohm
            .indexes
            .iter()
            .zip(collector.messages)
            .map(|(&spec, messages)| SohmIndexContent { spec, messages })
            .collect();
        // The table a reopen found is superseded whole by the one below, and
        // every header that pointed into it is in this finalize's rewrite set
        // — so its blocks go back immediately before the replacement is laid
        // out, and the new table lands in them instead of growing the file on
        // every open/close cycle. Taken, not read: a second finalize must not
        // free the same blocks twice.
        for (addr, len) in std::mem::take(&mut *sohm.superseded.lock()) {
            self.allocator.free(addr, len, FreeSpaceClass::Metadata);
        }
        let built = build_shared_messages(&indexes, &self.ctx, &mut |len| {
            self.allocator.allocate(len, FreeSpaceClass::Metadata)
        })?;
        for block in &built.blocks {
            self.handle.write_at(block.addr, &block.image)?;
        }

        // Only now, with every block on disk: from here the header builders
        // substitute pointers, and `write_superblock_extension` names the
        // table this laid out.
        *sohm.phase.lock() = SohmPhase::Resolve {
            ids: built.heap_ids,
            first: FirstCopies::default(),
        };
        *sohm.table_addr.lock() = Some(built.table_addr);
        Ok(())
    }

    /// Write the file's free-space managers over the space this close leaves
    /// free, and return the file-space info message body naming them.
    ///
    /// Called from [`write_superblock_extension`](Self::write_superblock_extension)
    /// once every other block of the file has an address, which is what makes
    /// the allocator's free list the file's *final* free space: a block
    /// allocated after this point would land in space a manager still claims.
    ///
    /// INVARIANT: from the moment this returns, every byte the allocator holds
    /// free is a byte some sections block records, and the two blocks each
    /// manager itself occupies are held by neither. Nothing may allocate
    /// between here and the superblock write; `write_object_headers` writes
    /// over blocks reserved in an earlier phase and is the only thing that
    /// runs in between.
    ///
    /// Returns `None` for a file with no message of its own to write — a
    /// reopen whose carried message this session must not touch, and a file
    /// created at the library defaults — which leaves both byte-identical to
    /// what the same close wrote before free space was recorded at all. A file
    /// that carries the message but keeps no managers (either non-manager
    /// strategy, or `persist: false`) gets the message back with every address
    /// undefined, which is what `H5F__super_init` writes for it.
    fn write_free_space_managers(&self) -> IoResult<Option<Vec<u8>>> {
        let Some(fs) = self.free_space.as_deref() else {
            return Ok(None);
        };
        if !fs.records_free_space() {
            return Ok(Some(fs.info.encode(&self.ctx)?));
        }
        // The managers a reopen found are superseded whole by the ones below,
        // so their blocks go back before anything is laid out: the space the
        // old manager occupied is free space the new one records, and the new
        // one may be laid out in it.
        for &(addr, len) in &fs.superseded {
            self.allocator.free(addr, len, FreeSpaceClass::Metadata);
        }

        let hdr_size = FreeSpaceHeader::encoded_size(&self.ctx) as u64;
        let settled = self.settle_free_space_managers(hdr_size, fs.info.threshold)?;

        let mut info = fs.info.clone();
        info.fs_addr = vec![UNDEF_ADDR; info.fs_addr.len()];
        for placed in &settled {
            let mut header = manager_header(&placed.sections);
            // The settle loop sized the block; that the encode agrees is the
            // invariant that makes `sect_size` a length a reader can trust.
            let needed = free_space::sinfo_encoded_size(&header, &placed.sections, &self.ctx);
            if needed > placed.sect_size {
                return Err(crate::io::IoError::InvalidState(format!(
                    "the free-space sections need {needed} bytes, not the {} laid out",
                    placed.sect_size
                )));
            }
            header.sect_addr = placed.sect_addr;
            header.sect_size = placed.sect_size;
            header.alloc_sect_size = placed.sect_size;
            self.handle.write_at(
                placed.sect_addr,
                &free_space::encode_sections(
                    &header,
                    placed.hdr_addr,
                    &placed.sections,
                    placed.sect_size as usize,
                    &self.ctx,
                ),
            )?;
            self.handle
                .write_at(placed.hdr_addr, &header.encode(&self.ctx))?;
            // `H5MF__close_delete_fstype` leaves a manager with no sections
            // without an address, so only the ones written name themselves.
            info.fs_addr[placed.manager.message_slot()] = placed.hdr_addr;
        }
        // The end of the file *after* the settle above, not before it, which
        // the field's name denies: it is 1.10 vintage, where two EOAs were
        // kept — one taken before the self-referential managers were placed
        // and one after (H5MF.c:3305 and 3382 in 1.10.11) — and the message
        // carried the first (1.10.11 H5MF.c:1833, 1999). 1.14 keeps one,
        // `f->shared->eoa_fsm_fsalloc`, read once the allocation loop has run
        // (H5MF.c:3234-3240) and encoded into this field by both close paths
        // (H5MF.c:1759, 1923); H5Fsuper.c:826 names it "the final eoa". A
        // 1.10 reader wants that value and not the older one: equal EOAs are
        // the case `H5MF_tidy_self_referential_fsm_hack` returns on
        // (1.10.11 H5MF.c:3620-3622), which is what leaves the managers this
        // close wrote in place.
        info.eoa_pre_fsm_fsalloc = self.allocator.eof();
        Ok(Some(info.encode(&self.ctx)?))
    }

    /// The file's free space as each manager will record it: address-ordered
    /// per manager, tagged with the section class that manager writes, and
    /// with everything below `threshold` left out.
    ///
    /// The allocator is the single owner of merging — `H5FS__sect_merge`'s
    /// rules, per manager and, on a paged file, per page — so nothing merges
    /// here; overlap is checked because two overlapping sections would be a
    /// manager claiming space another structure holds.
    fn free_sections(&self, threshold: u64) -> IoResult<Vec<(FreeSpaceManager, Vec<FreeSection>)>> {
        let policy = self.allocator.policy();
        let extents = self.allocator.free_extents();
        let mut sets = Vec::new();
        for manager in FreeSpaceManager::ALL {
            let mut sections: Vec<FreeSection> = extents
                .iter()
                .filter(|b| b.manager == manager)
                // `H5FS_sect_add` refuses a section below the file's
                // threshold, so a block smaller than it is space the file
                // leaks rather than records — the same trade the threshold is
                // there to make.
                .filter(|b| b.len >= threshold)
                .map(|b| FreeSection {
                    addr: b.addr,
                    len: b.len,
                    class: policy.section_class(manager),
                })
                .collect();
            sections.sort_unstable_by_key(|s| s.addr);
            if let Some(bad) = sections
                .windows(2)
                .find(|w| w[0].addr + w[0].len > w[1].addr)
            {
                return Err(crate::io::IoError::InvalidState(format!(
                    "this session freed overlapping blocks: {:#x}+{} overlaps {:#x}",
                    bad[0].addr, bad[0].len, bad[1].addr
                )));
            }
            sets.push((manager, sections));
        }
        Ok(sets)
    }

    /// Give every manager that records anything its own header and sections
    /// blocks, and return what each will write.
    ///
    /// Self-referential, which is the whole difficulty: a manager's two blocks
    /// come out of the free space the managers record, and taking them changes
    /// that space, which changes how many bytes the sections block needs.
    /// Upstream reruns the allocation pass until no manager allocates anything
    /// further — the `do { ... } while (continue_alloc_fsm)` loop in
    /// `H5MF_settle_meta_data_fsm` (H5MF.c:3213-3247) around
    /// `H5FS_vfd_alloc_hdr_and_section_info_if_needed`, which allocates
    /// through `H5MF_alloc` like everything else. So does this: the blocks
    /// come out of the same [`FileAllocator`], under the same strategy, so a
    /// paged file's manager blocks land in pages and their page remainders are
    /// recorded like any others.
    ///
    /// Two rules make it terminate. A manager, once placed, stays placed: were
    /// its blocks released because its sections had been consumed, freeing
    /// them would put those sections back and the next round would place it
    /// again. And a sections block only ever grows: upstream frees a block
    /// that turned out too small and reallocates it next round
    /// (H5FSsection.c:2418-2423), and a size that only rises reaches its
    /// bound.
    fn settle_free_space_managers(
        &self,
        hdr_size: u64,
        threshold: u64,
    ) -> IoResult<Vec<PlacedManager>> {
        /// Rounds before the layout is called divergent. A round either places
        /// a manager or grows one sections block, and there are three
        /// managers, so a file that needs more than this is not converging.
        const ROUNDS: usize = 16;

        // Raw data first and metadata last, in `H5MF_settle_raw_data_fsm`'s
        // order (H5C.c:689-696): every manager's own blocks are metadata
        // allocations, so the metadata manager funds all of them and is the
        // one whose section set the others change.
        const ORDER: [FreeSpaceManager; 3] = [
            FreeSpaceManager::RawData,
            FreeSpaceManager::Large,
            FreeSpaceManager::Metadata,
        ];

        let size_of = |sections: &[FreeSection]| {
            let ordered = free_space::serialization_order(sections);
            free_space::sinfo_encoded_size(&manager_header(&ordered), &ordered, &self.ctx)
        };
        let mut placed: Vec<PlacedManager> = Vec::new();
        for _ in 0..ROUNDS {
            let sets = self.free_sections(threshold)?;
            let sections_of = |manager: FreeSpaceManager| {
                sets.iter()
                    .find(|(m, _)| *m == manager)
                    .map(|(_, s)| s.as_slice())
                    .unwrap_or_default()
            };

            let mut changed = false;
            for manager in ORDER {
                let sections = sections_of(manager);
                if sections.is_empty() || placed.iter().any(|p| p.manager == manager) {
                    continue;
                }
                let sect_size = size_of(sections);
                let hdr_addr = self.allocator.allocate(hdr_size, FreeSpaceClass::Metadata);
                let sect_addr = self.allocator.allocate(sect_size, FreeSpaceClass::Metadata);
                placed.push(PlacedManager {
                    manager,
                    hdr_addr,
                    sect_addr,
                    sect_size,
                    sections: Vec::new(),
                });
                changed = true;
            }
            if !changed {
                for p in &mut placed {
                    let needed = size_of(sections_of(p.manager));
                    if needed > p.sect_size {
                        self.allocator
                            .free(p.sect_addr, p.sect_size, FreeSpaceClass::Metadata);
                        p.sect_size = needed;
                        p.sect_addr = self.allocator.allocate(needed, FreeSpaceClass::Metadata);
                        changed = true;
                    }
                }
            }
            if !changed {
                for p in &mut placed {
                    p.sections = free_space::serialization_order(sections_of(p.manager));
                }
                return Ok(placed);
            }
        }
        Err(crate::io::IoError::InvalidState(format!(
            "the free-space managers did not settle in {ROUNDS} rounds"
        )))
    }

    /// Write the file's superblock extension, and the sole owner of that
    /// object header.
    ///
    /// Runs after [`prepare_shared_messages`](Self::prepare_shared_messages),
    /// whose table it names, and before the superblock that names it. What it
    /// writes is [`CarriedExtension`] — every message the reopened file's
    /// extension held — plus the shared-message table message, which is the
    /// one message whose content this session owns: the table moved, so the
    /// message read is stale and the message written names the new address.
    ///
    /// A file with neither carried messages nor shared messages gets no
    /// extension, which is what libhdf5 writes for it: `H5F__super_ext_create`
    /// is called only when there is a message to put in one.
    ///
    /// Version 1, holding its messages in one chunk: the extension is created
    /// before anything raises the file's object header version
    /// (`H5F__super_ext_create` passes `H5O_HDR_STORE_TIMES` off and takes the
    /// version-1 path), so an extension of any generation of file looks the
    /// same.
    fn write_superblock_extension(&self) -> IoResult<()> {
        if self.extension.addr.lock().is_some() {
            return Ok(());
        }
        let table = self.sohm.as_deref().and_then(|sohm| {
            sohm.table_addr
                .lock()
                .map(|addr| (sohm.indexes.len(), addr))
        });
        // A file with file-space properties of its own needs an extension
        // too: the message that declares them is the only place they are
        // recorded, and a file created with them carries nothing else.
        if self.extension.carried.is_empty() && table.is_none() && self.free_space.is_none() {
            return Ok(());
        }

        let mut messages: Vec<crate::io::object_header_io::ExtensionMessage> =
            self.extension.carried.clone();
        if let Some(fs) = self.free_space.as_deref() {
            // The declared message, at exactly the length the one written
            // below will have — every field of it is fixed-width, and only
            // `persist` and the message version change the count of
            // addresses, neither of which the close alters. The image is sized
            // and its block allocated before the managers can be laid out, so
            // the message has to reach its final *length* here even though its
            // content is settled later.
            let declared = fs.info.encode(&self.ctx)?;
            match messages
                .iter_mut()
                .find(|m| m.msg_type == MSG_FILE_SPACE_INFO)
            {
                Some(msg) => msg.body = declared,
                None => messages.push(crate::io::object_header_io::ExtensionMessage {
                    msg_type: MSG_FILE_SPACE_INFO,
                    flags: MSG_FLAG_DONTSHARE | MSG_FLAG_MARK_IF_UNKNOWN,
                    body: declared,
                }),
            }
        }
        if let Some((nindexes, table_addr)) = table {
            let nindexes = u8::try_from(nindexes).map_err(|_| {
                crate::io::IoError::InvalidState(format!("{nindexes} shared-message indexes"))
            })?;
            messages.push(crate::io::object_header_io::ExtensionMessage {
                msg_type: MSG_SHARED_MESSAGE_TABLE,
                flags: MSG_FLAG_CONSTANT | MSG_FLAG_DONTSHARE,
                body: SharedMessageTableMessage {
                    version: 0,
                    table_address: table_addr,
                    nindexes,
                }
                .encode(&self.ctx),
            });
        }
        let encode = |messages: &[crate::io::object_header_io::ExtensionMessage]| {
            let mut extension = ObjectHeader::new();
            for msg in messages {
                extension.add_message(msg.msg_type, msg.flags, msg.body.clone());
            }
            extension.encode_v1(1)
        };
        let image = encode(&messages)?;
        // Freed before the replacement is placed, so a reopen reuses the block
        // instead of stranding one per open/close cycle — the rule every other
        // superseded structure follows.
        for &(addr, len) in &self.extension.superseded {
            self.allocator.free(addr, len, FreeSpaceClass::Metadata);
        }
        let addr = self
            .allocator
            .allocate(image.len() as u64, FreeSpaceClass::Metadata);

        // Every block of this file now has an address, so the allocator holds
        // exactly the file's free space: settle the free-space managers over
        // it and say in this extension where they went.
        let image = match self.write_free_space_managers()? {
            None => image,
            Some(body) => {
                let msg = messages
                    .iter_mut()
                    .find(|m| m.msg_type == MSG_FILE_SPACE_INFO)
                    .ok_or_else(|| {
                        crate::io::IoError::InvalidState(
                            "a persisting file lost its file-space info message".into(),
                        )
                    })?;
                // Same length as the declared body put in above, so the
                // image measured before the block was allocated still fits.
                if body.len() != msg.body.len() {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "the file-space info message was laid out at {} bytes and \
                         written back at {}",
                        msg.body.len(),
                        body.len()
                    )));
                }
                msg.body = body;
                encode(&messages)?
            }
        };
        self.handle.write_at(addr, &image)?;
        *self.extension.addr.lock() = Some(addr);
        Ok(())
    }

    /// Define a new contiguous dataset. Returns the dataset index (used with
    /// `write_dataset_raw`).
    ///
    /// The raw-data region is allocated immediately so that
    /// `write_dataset_raw` can be called at any time before `close()`.
    pub fn create_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let total_elements: u64 = if dims.is_empty() {
            1
        } else {
            dims.iter().product()
        };
        let element_size = datatype.element_size() as u64;
        let data_size = total_elements * element_size;

        // Allocate space for the raw data.
        let data_addr = if data_size > 0 {
            self.allocator.allocate(data_size, FreeSpaceClass::RawData)
        } else {
            UNDEF_ADDR
        };

        let dataspace = if dims.is_empty() {
            DataspaceMessage::scalar()
        } else {
            DataspaceMessage::simple(dims)
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0, // set during finalize
                data_addr,
                data_size,
                compact: None,
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                append: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
            },
        );

        Ok(idx)
    }

    /// Define a new dataset whose raw data lives in files outside this one —
    /// `H5Pset_external`, h5py's `external=[(name, offset, size)]`.
    ///
    /// Each entry names a file, the byte offset in it where that entry's
    /// region starts, and how many bytes of the dataset the region holds; the
    /// entries concatenate, in order, into the dataset's logical byte range,
    /// and together must cover it. Nothing is allocated in this file: the data
    /// layout message says contiguous storage at an undefined address, and it
    /// is the External File List beside it that says where the bytes are
    /// (`H5D__layout_oh_create`).
    ///
    /// A named file is created on first write and never truncated, so several
    /// slots — or several datasets — may own disjoint ranges of one file, the
    /// way `H5D__efl_write` opens them.
    ///
    /// The last slot may take the unlimited size `H5O_EFL_UNLIMITED`, which
    /// makes it absorb however many bytes the dataset comes to hold; a
    /// dataset whose dataspace is unlimited must have one, since nothing
    /// finite could cover it (`H5D__efl_construct`: "unlimited dataspace but
    /// finite storage"). Only the first dimension may be extendible, which is
    /// the same function's other rule.
    pub fn create_external_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: Option<&[u64]>,
        files: &[(&str, u64, u64)],
    ) -> IoResult<usize> {
        if files.is_empty() {
            return Err(crate::io::IoError::InvalidState(format!(
                "external dataset '{name}' names no files; external storage is defined by \
                 the files it lives in, so at least one is required"
            )));
        }
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let total_elements: u64 = if dims.is_empty() {
            1
        } else {
            dims.iter().product()
        };
        let data_size = total_elements * datatype.element_size() as u64;

        let mut heap = LocalHeapImage::with_empty_string();
        let mut entries = Vec::with_capacity(files.len());
        for (i, &(file_name, offset, size)) in files.iter().enumerate() {
            if file_name.is_empty() {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' has a slot with an empty file name"
                )));
            }
            // `H5Pset_external` refuses to add a slot behind an unlimited one
            // ("previous file size is unlimited"): the unlimited slot already
            // owns every byte from its own start onwards, so nothing after it
            // could ever be reached.
            if size == UNLIMITED && i + 1 != files.len() {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' gives slot {i} ('{file_name}') the unlimited \
                     size H5O_EFL_UNLIMITED with {} slot(s) behind it; an unlimited slot \
                     absorbs the rest of the dataset, so it can only be the last",
                    files.len() - i - 1
                )));
            }
            if offset.checked_add(size).is_none() {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' slot '{file_name}' spans offset {offset} \
                     plus {size} bytes, past the end of the 64-bit address space"
                )));
            }
            entries.push(ExternalFile {
                name: file_name.to_string(),
                name_offset: heap.insert_str(file_name),
                offset,
                size,
            });
        }
        let external = ExternalStorage {
            // Filled in below, once the heap the names went into has an
            // address; the names' offsets within it are already final.
            heap_addr: UNDEF_ADDR,
            files: entries,
            // Settled by the open this create hands a handle out for, which
            // is `H5D__create` reading the dapl at H5Dint.c:1318.
            prefix: EfilePrefix::default(),
        };
        // `H5D__efl_construct`, over the dataset's *maximum* extent: the
        // slots must reserve at least every byte the dataset could come to
        // hold, and an unlimited extent can only be covered by an unlimited
        // last slot ("unlimited dataspace but finite storage").
        let max_dims = max_dims.unwrap_or(dims);
        if max_dims.len() != dims.len() {
            return Err(crate::io::IoError::InvalidState(format!(
                "external dataset '{name}' has {} dimensions but {} maximum ones",
                dims.len(),
                max_dims.len()
            )));
        }
        for (d, (&max, &cur)) in max_dims.iter().zip(dims).enumerate().skip(1) {
            if max > cur {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' makes dimension {d} extendible ({cur} of \
                     {max}); only the first dimension can be extendible for external storage"
                )));
            }
        }
        let reserved = external.total_size();
        if max_dims.contains(&u64::MAX) {
            if reserved != UNLIMITED {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' has an unlimited dataspace but its files \
                     reserve only {reserved} bytes; the last slot must take the unlimited \
                     size H5O_EFL_UNLIMITED"
                )));
            }
        } else {
            let max_bytes = max_dims
                .iter()
                .try_fold(datatype.element_size() as u64, |acc, &d| acc.checked_mul(d))
                .ok_or_else(|| {
                    crate::io::IoError::InvalidState(format!(
                        "external dataset '{name}' maximum extent times its element size \
                         overflows 64 bits"
                    ))
                })?;
            if reserved < max_bytes {
                return Err(crate::io::IoError::InvalidState(format!(
                    "external dataset '{name}' needs {max_bytes} bytes but its files reserve \
                     only {reserved}"
                )));
            }
        }

        // The names' heap, written now: it is ordinary metadata of this file,
        // and the message the header carries is only an address into it.
        let sa = self.ctx.sizeof_addr as usize;
        let ss = self.ctx.sizeof_size as usize;
        let heap_bytes = heap.as_bytes().to_vec();
        let heap_addr = self.allocator.allocate(
            local_heap_header_size(sa, ss) as u64,
            FreeSpaceClass::Metadata,
        );
        let heap_data_addr = self
            .allocator
            .allocate(heap_bytes.len() as u64, FreeSpaceClass::Metadata);
        let heap_hdr = LocalHeapHeader {
            data_size: heap_bytes.len() as u64,
            // Sized to hold exactly these names, so no block of it is free.
            free_list_offset: LOCAL_HEAP_FREE_NULL,
            data_addr: heap_data_addr,
        };
        self.handle.write_at(heap_addr, &heap_hdr.encode(sa, ss))?;
        self.handle.write_at(heap_data_addr, &heap_bytes)?;
        let external = ExternalStorage {
            heap_addr,
            ..external
        };

        let dataspace = if dims.is_empty() {
            DataspaceMessage::scalar()
        } else {
            let mut ds = DataspaceMessage::simple(dims);
            if max_dims != dims {
                ds.max_dims = Some(max_dims.to_vec());
            }
            ds
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: Some(external),
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0, // set during finalize
                // No block of this file's own: the layout message declares
                // contiguous storage at an undefined address, which is what
                // sends a reader to the external file list instead.
                data_addr: UNDEF_ADDR,
                data_size,
                compact: None,
                chunked: None,
                fixed_array: None,
                btree_v2: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                append: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
            },
        );

        Ok(idx)
    }

    /// Define a new virtual dataset — `H5Pset_virtual`, h5py's
    /// `create_virtual_dataset(name, VirtualLayout)`.
    ///
    /// Each mapping says which elements of this dataset (`virtual_selection`)
    /// are read from which elements (`source_selection`) of a dataset in
    /// another file; the sources are never opened here, and a mapping naming
    /// one that does not exist yet is perfectly legal — libhdf5 resolves each
    /// at read time, filling from the fill value where nothing maps.
    ///
    /// The mappings do not live in the object header: they are serialized
    /// into one global heap object and the layout message carries only its
    /// address and index (`H5D__virtual_store_layout`), which is why this
    /// allocates a heap object and nothing else.
    ///
    /// An unlimited (`H5S_UNLIMITED`) selection is written as one: the
    /// mapping grows with its source, and the virtual dataset's extent in
    /// that dimension is whatever the sources reachable at read time supply
    /// (`H5D__virtual_set_extent_unlim`). A `printf`-style source name is
    /// written as one too: `%b` substitutes the block index, so one mapping
    /// stands for the family of source datasets that fill the successive
    /// blocks of an unlimited virtual selection.
    pub fn create_virtual_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: Option<&[u64]>,
        mappings: &[VirtualMapping],
    ) -> IoResult<usize> {
        if mappings.is_empty() {
            return Err(crate::io::IoError::InvalidState(format!(
                "virtual dataset '{name}' names no mappings; a virtual dataset is defined \
                 by the source datasets it maps, so at least one is required"
            )));
        }
        for m in mappings {
            check_virtual_mapping(name, m)?;
        }

        let create = self.begin_create(name)?;
        let name = create.name.as_str();

        // The mapping list is ordinary file metadata, written now: the header
        // built at finalize carries only the heap address and object index it
        // lands at.
        let block = VirtualMappingList {
            mappings: mappings.to_vec(),
        }
        .encode(&self.ctx)?;
        let (heap_addr, heap_index) = self.insert_vlen_objects(&[&block])?[0];

        let dataspace = if dims.is_empty() {
            DataspaceMessage::scalar()
        } else {
            let mut ds = DataspaceMessage::simple(dims);
            // A caller that named no maximum gets the current dimensions, the
            // maximum `simple` already filled in: `H5Screate_simple(rank,
            // dims, NULL)` reaches the encoder with `extent.max` set
            // (H5S.c:1293-1299), so leaving it absent here would write a
            // message no upstream API call can produce.
            if let Some(max) = max_dims {
                ds.max_dims = Some(max.to_vec());
            }
            ds
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: Some(VirtualStorage {
                    heap_addr,
                    heap_index: heap_index as u32,
                    mappings: mappings.to_vec(),
                }),
                dataspace,
                read_format: None,
                obj_header_addr: 0, // set during finalize
                // Not a block of this file at all: every element is read out
                // of a source dataset, so there is nothing here to allocate
                // and nothing to free when the dataset is deleted.
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                chunked: None,
                fixed_array: None,
                btree_v2: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                append: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
            },
        );

        Ok(idx)
    }

    /// Define a new compact dataset — `H5Pset_layout(dcpl, H5D_COMPACT)`.
    ///
    /// The raw data lives inside the data layout message in the dataset's own
    /// object header, so it costs no block of its own and no extra seek to
    /// read; the price is the ceiling, and that the whole image is rewritten
    /// whenever the header is. The buffer is created at its final length and
    /// zero-filled, which is what `H5D__compact_fill` does at create time, so
    /// a dataset never written still reads back as its fill value.
    ///
    /// Errors when the image exceeds [`MAX_COMPACT_DATA`].
    pub fn create_compact_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
    ) -> IoResult<usize> {
        let total_elements: u64 = if dims.is_empty() {
            1
        } else {
            dims.iter().product()
        };
        let data_size = total_elements * datatype.element_size() as u64;
        if data_size > MAX_COMPACT_DATA as u64 {
            return Err(crate::io::IoError::InvalidState(format!(
                "compact dataset '{name}' needs {data_size} bytes, above the \
                 {MAX_COMPACT_DATA}-byte ceiling a data layout message can hold; \
                 use contiguous or chunked storage"
            )));
        }

        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let dataspace = if dims.is_empty() {
            DataspaceMessage::scalar()
        } else {
            DataspaceMessage::simple(dims)
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0, // set during finalize
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: Some(vec![0u8; data_size as usize]),
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                append: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
            },
        );

        Ok(idx)
    }

    /// Define a new dataset with the NULL dataspace: no elements at all.
    ///
    /// Distinct from a scalar dataset (`create_dataset` with `dims == []`),
    /// which holds exactly one element — a NULL dataspace holds zero, so
    /// there is no raw image to allocate: `data_addr` stays `UNDEF_ADDR` and
    /// `data_size` stays 0 permanently, the same terminal state
    /// `create_dataset` already reaches for a zero-length dimension.
    pub fn create_null_dataset(&self, name: &str, datatype: DatatypeMessage) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace: DataspaceMessage::null(),
                read_format: None,
                obj_header_addr: 0, // set during finalize
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                append: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
            },
        );

        Ok(idx)
    }

    /// Define a new chunked dataset with an extensible array index.
    ///
    /// Returns the dataset index. The dataset starts empty (dims[0] = 0 if
    /// the first dimension is unlimited). Use `write_chunk` and
    /// `extend_dataset` to add data.
    pub fn create_chunked_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, max_dims, chunk_dims)?;
        ensure_at_most_one_unlimited(max_dims)?;
        let chunk_bytes = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let layout_version = self.chunk_layout_version(false, chunk_bytes);
        let earray_params = EarrayParams::default_params();
        let ndblk_addrs = compute_ndblk_addrs(earray_params.sup_blk_min_data_ptrs)?;
        let nsblk_addrs = compute_nsblk_addrs(
            earray_params.idx_blk_elmts,
            earray_params.data_blk_min_elmts,
            earray_params.sup_blk_min_data_ptrs,
            earray_params.max_nelmts_bits,
        )?;

        // Create EA header
        let mut ea_header = ExtensibleArrayHeader::new_for_chunks(&self.ctx);
        ea_header.max_nelmts_bits = earray_params.max_nelmts_bits;
        ea_header.idx_blk_elmts = earray_params.idx_blk_elmts;
        ea_header.data_blk_min_elmts = earray_params.data_blk_min_elmts;
        ea_header.sup_blk_min_data_ptrs = earray_params.sup_blk_min_data_ptrs;
        ea_header.max_dblk_page_nelmts_bits = earray_params.max_dblk_page_nelmts_bits;

        // Allocate and write EA header (placeholder, will be updated)
        let hdr_encoded = ea_header.encode(&self.ctx);
        let ea_header_addr = self
            .allocator
            .allocate(hdr_encoded.len() as u64, FreeSpaceClass::Metadata);

        // Create EA index block with pre-allocated super block address slots
        let ea_iblk = ExtensibleArrayIndexBlock::new(
            ea_header_addr,
            earray_params.idx_blk_elmts,
            ndblk_addrs,
            nsblk_addrs,
        );

        // Allocate and write EA index block
        let iblk_encoded = ea_iblk.encode(&self.ctx);
        let ea_iblk_addr = self
            .allocator
            .allocate(iblk_encoded.len() as u64, FreeSpaceClass::Metadata);

        // Update header with index block address
        ea_header.idx_blk_addr = ea_iblk_addr;

        // Write both to disk
        let hdr_encoded = ea_header.encode(&self.ctx);
        self.handle.write_at(ea_header_addr, &hdr_encoded)?;
        self.handle.write_at(ea_iblk_addr, &iblk_encoded)?;

        // Build dataspace with max dims
        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                chunked: Some(ChunkedDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    earray_params,
                    ea_header_addr,
                    ea_iblk_addr,
                    ea_header,
                    ea_iblk,
                    chunks_written: 0,
                    filt_iblk: None,
                    chunk_size_len: 0,
                }),
                append: None,
            },
        );

        Ok(idx)
    }

    /// Write `data` into a contiguous dataset's raw storage at *dataset-
    /// relative* byte offset `off`.
    ///
    /// The single owner of a contiguous raw-data write. Which storage that is
    /// — a block of this file, or the files an External File List names — is
    /// decided once, by [`DatasetInfo::contiguous_target`], and never at a
    /// call site.
    fn write_contiguous_bytes(
        &self,
        target: &ContiguousTarget,
        off: u64,
        data: &[u8],
    ) -> IoResult<()> {
        match target {
            ContiguousTarget::Local(addr) => Ok(self.handle.write_at(addr + off, data)?),
            ContiguousTarget::External { files, prefix } => {
                // The prefix the open settled, not one resolved here:
                // `H5D__efl_write` joins against `dset->shared->extfile_prefix`
                // (H5Defl.c:429-431), the same field `H5D__efl_read` joins
                // against, so a relative name lands where a later read looks.
                write_external_file_bytes(files, prefix.as_deref(), off, data)
            }
            ContiguousTarget::Virtual => Err(virtual_write_refused()),
        }
    }

    /// Write raw bytes to a contiguous dataset identified by `index`.
    ///
    /// The caller is responsible for providing data in the correct byte order
    /// and layout. The length must match the total data size declared at
    /// creation time.
    pub fn write_dataset_raw(&self, index: usize, data: &[u8]) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        let target = {
            let mut g = ds.lock();
            if g.is_chunked() {
                return Err(crate::io::IoError::InvalidState(
                    "use write_chunk for chunked datasets".into(),
                ));
            }
            // A compact dataset's raw image is its layout message, so the
            // write lands in the buffer the header is built from rather than
            // at a file offset, and the header it is built into is now stale.
            if let Some(image) = g.compact.as_mut() {
                if data.len() != image.len() {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "data size mismatch: expected {} bytes, got {}",
                        image.len(),
                        data.len()
                    )));
                }
                image.copy_from_slice(data);
                g.header_dirty = true;
                return Ok(());
            }
            let Some(target) = g.contiguous_target() else {
                return Err(crate::io::IoError::InvalidState(
                    "dataset has no data allocated".into(),
                ));
            };
            // A dataset that stores nothing of its own has no byte count to
            // check a write against — `write_contiguous_bytes` refuses it by
            // name below, which is the answer the caller needs.
            if target.is_storage() && data.len() as u64 != g.data_size {
                return Err(crate::io::IoError::InvalidState(format!(
                    "data size mismatch: expected {} bytes, got {}",
                    g.data_size,
                    data.len()
                )));
            }
            target
        };
        self.write_contiguous_bytes(&target, 0, data)
    }

    /// Write a chunk of data to a chunked dataset.
    ///
    /// `chunk_offset` is the chunk coordinates (e.g., [frame_idx] for a 1D-chunked
    /// streaming dataset where chunk_dims = [1, H, W]).
    /// Only the first (unlimited) dimension index is used for EA indexing.
    ///
    /// `data` must be exactly chunk_size bytes (product of chunk_dims * element_size).
    pub fn write_chunk(&self, index: usize, chunk_idx: u64, data: &[u8]) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.write_chunk_inner(index, chunk_idx, data)
    }

    /// [`Self::write_chunk`] body; the caller holds the dataset's op lock or
    /// the writer exclusively.
    pub(crate) fn write_chunk_inner(
        &self,
        index: usize,
        chunk_idx: u64,
        data: &[u8],
    ) -> IoResult<()> {
        let ds = self.ds(index);
        // Read the chunk geometry and filter pipeline under one brief lock,
        // then drop it: compression runs *outside* the lock, and
        // `record_ea_chunk` re-locks the same slot, so the guard must not be
        // held across either.
        let (chunk_bytes, pipeline) = {
            let g = ds.lock();
            let element_size = g.datatype.element_size() as u64;
            let chunked = g
                .chunked
                .as_ref()
                .ok_or_else(|| crate::io::IoError::InvalidState("not a chunked dataset".into()))?;
            (
                chunked.chunk_dims.iter().product::<u64>() * element_size,
                g.filter_pipeline.clone(),
            )
        };

        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }

        // Apply compression if filter pipeline is set
        let compressed;
        let write_data = if let Some(ref pipeline) = pipeline {
            compressed = filter::apply_filters(pipeline, data)?;
            &compressed
        } else {
            data
        };
        // filter_mask = 0: this path runs the whole pipeline, so no filter is
        // skipped for the chunk.
        self.record_ea_chunk(index, chunk_idx, write_data, 0)
    }

    /// Decide where a chunk's bytes belong and put them there, returning the
    /// address to record in the index.
    ///
    /// `old` is the chunk's current `(address, stored length)` if the index
    /// already holds an entry for it. This is the single owner of the
    /// rewrite-placement rule, mirroring libhdf5's `H5D__chunk_file_alloc`
    /// (`H5Dchunk.c`): a chunk whose stored size is unchanged is overwritten
    /// where it already lives, and only a chunk that no longer fits moves,
    /// releasing its old block. Without this every rewrite would abandon the
    /// old block and grow the file.
    fn place_chunk(&self, old: Option<(u64, u64)>, new_len: u64) -> u64 {
        match old {
            // Same stored size: overwrite in place. This is every unfiltered
            // rewrite (the stored size is fixed by the chunk shape) and every
            // filtered rewrite that compressed to the same length.
            Some((addr, len)) if addr != UNDEF_ADDR && len == new_len => addr,
            Some((addr, len)) if addr != UNDEF_ADDR => {
                // The chunk has to move. Under SWMR a reader may still hold an
                // index that points at the old block, so libhdf5 keeps it
                // (H5D__chunk_file_alloc skips H5MF_xfree when the file is
                // open for SWMR writing); do the same.
                if !self.swmr_active {
                    self.allocator.free(addr, len, FreeSpaceClass::RawData);
                }
                self.allocator.allocate(new_len, FreeSpaceClass::RawData)
            }
            _ => self.allocator.allocate(new_len, FreeSpaceClass::RawData),
        }
    }

    /// Place a chunk's already-final bytes (filtered if the dataset is
    /// filtered) in the file and record them in the extensible-array index —
    /// in the index block, a data block, or a super block per the EA geometry.
    /// Shared by write_chunk and write_compressed_chunk.
    ///
    /// The index lookup happens *before* the bytes are placed, because the
    /// entry it finds is what tells [`place_chunk`](Self::place_chunk) whether
    /// this is a rewrite that can stay put.
    fn record_ea_chunk(
        &self,
        index: usize,
        chunk_idx: u64,
        final_bytes: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        let compressed_size = final_bytes.len() as u64;
        let ds = self.ds(index);
        // Hold one slot guard for the whole method: every dataset-state access
        // below goes through `m`, while `self.handle`/`self.allocator`/`self.ctx`
        // are disjoint fields safe to touch with the guard held.
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        // For a filtered dataset the chunk's stored size is encoded in the
        // `chunk_size_len`-byte field of each filtered EA entry
        // (`FilteredChunkEntry::encode` writes `nbytes[..chunk_size_len]`,
        // which truncates silently). Reject a size that would not fit, the way
        // libhdf5's H5D_CHUNK_ENCODE_SIZE_CHECK does, instead of corrupting the
        // index. The compress path never exceeds this (chunk_size_len holds the
        // uncompressed chunk size); a direct/raw write with caller-supplied
        // bytes can.
        if is_filtered {
            let chunk_size_len = m.chunked.as_ref().unwrap().chunk_size_len as usize;
            if chunk_size_len < 8 && compressed_size >= (1u64 << (chunk_size_len * 8)) {
                return Err(crate::io::IoError::InvalidState(format!(
                    "filtered chunk size {compressed_size} does not fit in the \
                     {chunk_size_len}-byte extensible-array chunk-size field"
                )));
            }
        }
        let idx_blk_elmts = {
            let c = m.chunked.as_ref().unwrap();
            c.earray_params.idx_blk_elmts as u64
        };

        if chunk_idx < idx_blk_elmts {
            let chunked = m.chunked.as_mut().unwrap();
            if is_filtered {
                if let Some(ref mut fiblk) = chunked.filt_iblk {
                    let old = fiblk.elements[chunk_idx as usize];
                    let chunk_addr =
                        self.place_chunk(Some((old.addr, old.nbytes)), compressed_size);
                    self.handle.write_at(chunk_addr, final_bytes)?;
                    fiblk.elements[chunk_idx as usize] = FilteredChunkEntry {
                        addr: chunk_addr,
                        nbytes: compressed_size,
                        filter_mask,
                    };
                }
            } else {
                // An unfiltered chunk's stored size is fixed by the chunk
                // shape, so a rewrite always fits where it already is.
                let old = chunked.ea_iblk.elements[chunk_idx as usize];
                let chunk_addr = self.place_chunk(Some((old, compressed_size)), compressed_size);
                self.handle.write_at(chunk_addr, final_bytes)?;
                chunked.ea_iblk.elements[chunk_idx as usize] = chunk_addr;
            }
            chunked.chunks_written += 1;
            if chunk_idx + 1 > chunked.ea_header.max_idx_set {
                chunked.ea_header.max_idx_set = chunk_idx + 1;
            }
            if chunked.ea_header.num_elmts_realized < idx_blk_elmts {
                chunked.ea_header.num_elmts_realized = idx_blk_elmts;
            }
        } else {
            // chunk_idx >= idx_blk_elmts: place the chunk through the EA
            // data-block / super-block hierarchy (libhdf5-compatible geometry).
            let (geo, max_nelmts_bits, chunk_size_len, ea_header_addr) = {
                let c = m.chunked.as_ref().unwrap();
                let p = &c.earray_params;
                (
                    EaGeometry::new(
                        p.idx_blk_elmts,
                        p.data_blk_min_elmts,
                        p.sup_blk_min_data_ptrs,
                        p.max_nelmts_bits,
                        p.max_dblk_page_nelmts_bits,
                    )?,
                    p.max_nelmts_bits,
                    c.chunk_size_len,
                    c.ea_header_addr,
                )
            };
            let loc = match geo.locate(chunk_idx)? {
                EaLoc::Dblk(l) => l,
                EaLoc::Index { .. } => unreachable!("chunk_idx >= idx_blk_elmts"),
            };
            if loc.paged {
                return Err(crate::io::IoError::InvalidState(format!(
                    "chunk index {} needs a paged extensible-array data block, \
                     which is not yet supported",
                    chunk_idx
                )));
            }
            let class_id = if is_filtered {
                EA_CLS_FILT_CHUNK
            } else {
                EA_CLS_CHUNK
            };
            let dblk_nelmts = loc.dblk_nelmts as usize;

            // Resolve the data block's current address and its parent slot,
            // creating the owning super block on demand.
            let parent: DblkParent;
            let mut dblk_addr: u64;
            match loc.path {
                EaDblkPath::Direct { idx: di } => {
                    let c = m.chunked.as_ref().unwrap();
                    dblk_addr = if is_filtered {
                        c.filt_iblk.as_ref().unwrap().dblk_addrs[di]
                    } else {
                        c.ea_iblk.dblk_addrs[di]
                    };
                    parent = DblkParent::IndexBlock(di);
                }
                EaDblkPath::ViaSblk {
                    sblk_off,
                    local_dblk,
                    ndblks_in_sblk,
                    sblk_block_offset,
                } => {
                    let mut sblk_addr = {
                        let c = m.chunked.as_ref().unwrap();
                        if is_filtered {
                            c.filt_iblk.as_ref().unwrap().sblk_addrs[sblk_off]
                        } else {
                            c.ea_iblk.sblk_addrs[sblk_off]
                        }
                    };
                    if sblk_addr == UNDEF_ADDR {
                        let sb = ExtensibleArraySuperBlock::new(
                            class_id,
                            ea_header_addr,
                            sblk_block_offset,
                            ndblks_in_sblk,
                        );
                        let enc = sb.encode(&self.ctx, max_nelmts_bits);
                        sblk_addr = self
                            .allocator
                            .allocate(enc.len() as u64, FreeSpaceClass::Metadata);
                        self.handle.write_at(sblk_addr, &enc)?;
                        let c = m.chunked.as_mut().unwrap();
                        if is_filtered {
                            c.filt_iblk.as_mut().unwrap().sblk_addrs[sblk_off] = sblk_addr;
                        } else {
                            c.ea_iblk.sblk_addrs[sblk_off] = sblk_addr;
                        }
                        c.ea_header.num_sblks_created += 1;
                        c.ea_header.size_sblks_created += enc.len() as u64;
                    }
                    let sb_buf = self.handle.read_at_most(sblk_addr, 65536)?;
                    // The writer never creates paged super blocks (it errors
                    // before the paging threshold), so page_init_total is 0.
                    let sb = ExtensibleArraySuperBlock::decode(
                        &sb_buf,
                        &self.ctx,
                        max_nelmts_bits,
                        ndblks_in_sblk,
                        0,
                    )?;
                    dblk_addr = sb.dblk_addrs[local_dblk];
                    parent = DblkParent::SuperBlock {
                        sblk_addr,
                        ndblks_in_sblk,
                        local_dblk,
                    };
                }
            }

            // Create or update the data block holding this chunk's entry.
            let created = dblk_addr == UNDEF_ADDR;
            if is_filtered {
                let mut dblk = if created {
                    FilteredDataBlock::new(ea_header_addr, loc.dblk_block_offset, dblk_nelmts)
                } else {
                    let buf = self.handle.read_at_most(dblk_addr, 65536)?;
                    FilteredDataBlock::decode(
                        &buf,
                        &self.ctx,
                        max_nelmts_bits,
                        dblk_nelmts,
                        chunk_size_len,
                    )?
                };
                // A freshly created data block holds only undefined addresses,
                // so this reads as "no previous chunk" without a special case.
                let old = dblk.elements[loc.offset_in_dblk as usize];
                let chunk_addr = self.place_chunk(Some((old.addr, old.nbytes)), compressed_size);
                self.handle.write_at(chunk_addr, final_bytes)?;
                let entry = FilteredChunkEntry {
                    addr: chunk_addr,
                    nbytes: compressed_size,
                    filter_mask,
                };
                dblk.elements[loc.offset_in_dblk as usize] = entry;
                let enc = dblk.encode(&self.ctx, max_nelmts_bits, chunk_size_len);
                if created {
                    dblk_addr = self
                        .allocator
                        .allocate(enc.len() as u64, FreeSpaceClass::Metadata);
                }
                self.handle.write_at(dblk_addr, &enc)?;
                if created {
                    let c = m.chunked.as_mut().unwrap();
                    c.ea_header.num_dblks_created += 1;
                    c.ea_header.size_dblks_created += enc.len() as u64;
                }
            } else {
                let mut dblk = if created {
                    ExtensibleArrayDataBlock::new(
                        ea_header_addr,
                        loc.dblk_block_offset,
                        dblk_nelmts,
                    )
                } else {
                    let buf = self.handle.read_at_most(dblk_addr, 65536)?;
                    ExtensibleArrayDataBlock::decode(&buf, &self.ctx, max_nelmts_bits, dblk_nelmts)?
                };
                // Unfiltered: the stored size is fixed by the chunk shape, so
                // a rewrite always fits its old block. A freshly created data
                // block holds undefined addresses and falls through to a new
                // allocation.
                let old = dblk.elements[loc.offset_in_dblk as usize];
                let chunk_addr = self.place_chunk(Some((old, compressed_size)), compressed_size);
                self.handle.write_at(chunk_addr, final_bytes)?;
                dblk.elements[loc.offset_in_dblk as usize] = chunk_addr;
                let enc = dblk.encode(&self.ctx, max_nelmts_bits);
                if created {
                    dblk_addr = self
                        .allocator
                        .allocate(enc.len() as u64, FreeSpaceClass::Metadata);
                }
                self.handle.write_at(dblk_addr, &enc)?;
                if created {
                    let c = m.chunked.as_mut().unwrap();
                    c.ea_header.num_dblks_created += 1;
                    c.ea_header.size_dblks_created += enc.len() as u64;
                }
            }

            // Record a newly-created data block's address in its parent.
            if created {
                match parent {
                    DblkParent::IndexBlock(di) => {
                        let c = m.chunked.as_mut().unwrap();
                        if is_filtered {
                            c.filt_iblk.as_mut().unwrap().dblk_addrs[di] = dblk_addr;
                        } else {
                            c.ea_iblk.dblk_addrs[di] = dblk_addr;
                        }
                    }
                    DblkParent::SuperBlock {
                        sblk_addr,
                        ndblks_in_sblk,
                        local_dblk,
                    } => {
                        let buf = self.handle.read_at_most(sblk_addr, 65536)?;
                        let mut sb = ExtensibleArraySuperBlock::decode(
                            &buf,
                            &self.ctx,
                            max_nelmts_bits,
                            ndblks_in_sblk,
                            0,
                        )?;
                        sb.dblk_addrs[local_dblk] = dblk_addr;
                        let enc = sb.encode(&self.ctx, max_nelmts_bits);
                        self.handle.write_at(sblk_addr, &enc)?;
                    }
                }
            }

            // Statistics.
            let c = m.chunked.as_mut().unwrap();
            c.chunks_written += 1;
            if chunk_idx + 1 > c.ea_header.max_idx_set {
                c.ea_header.max_idx_set = chunk_idx + 1;
            }
            if created {
                c.ea_header.num_elmts_realized += loc.dblk_nelmts;
            }
        }
        Ok(())
    }

    /// Write a slice (hyperslab) of data to a dataset, contiguous or chunked.
    ///
    /// `starts` and `counts` define the N-dimensional selection.
    /// `data` must be exactly `product(counts) * element_size` bytes.
    ///
    /// The selection is validated once here and then handed to the layout's
    /// own writer, so a caller never has to know which storage the dataset
    /// uses.
    pub fn write_slice(
        &self,
        index: usize,
        starts: &[u64],
        counts: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.write_slice_inner(index, starts, counts, data)
    }

    /// [`Self::write_slice`] body; the caller holds the dataset's op lock or
    /// the writer exclusively.
    pub(crate) fn write_slice_inner(
        &self,
        index: usize,
        starts: &[u64],
        counts: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let ds_ref = self.ds(index);
        let ds = ds_ref.lock();
        let is_chunked = ds.is_chunked();

        let dims = &ds.dataspace.dims;
        let element_size = ds.datatype.element_size() as u64;
        let ndims = dims.len();

        // Every hyperslab edge must stay inside the dataset; without this an
        // out-of-bounds selection writes raw bytes over neighbouring data.
        check_hyperslab(dims, starts, counts)?;
        if ndims == 0 {
            return Err(crate::io::IoError::InvalidState(
                "write_slice does not support scalar datasets; use write_dataset_raw".into(),
            ));
        }

        let out_elems: u64 = counts.iter().product();
        if data.len() as u64 != out_elems * element_size {
            return Err(crate::io::IoError::InvalidState(format!(
                "data size mismatch: expected {} bytes, got {}",
                out_elems * element_size,
                data.len()
            )));
        }

        // `dims` borrows the dataset slot; collect what the writers below need
        // so the guard can be dropped before they re-lock it.
        let dims = dims.clone();
        let target = ds.contiguous_target();
        drop(ds);

        if is_chunked {
            // Rows the append buffer holds are not in the chunks yet; writing
            // them there anyway would be undone when the buffer flushes at
            // close. Hand them to the chunks first.
            self.flush_append_buffer_if_intersecting(index, starts[0], starts[0] + counts[0])?;
            return self.write_slice_chunked(index, starts, counts, data);
        }
        let Some(target) = target else {
            return Err(crate::io::IoError::InvalidState(
                "dataset has no data allocated".into(),
            ));
        };

        // Write each maximal contiguous run in one write. Trailing
        // full-selected dimensions coalesce, mirroring the read path: a slice
        // with a full last axis becomes one write per outer index instead of
        // one write per last-axis row.
        for_each_contiguous_run(
            &dims,
            starts,
            counts,
            element_size,
            |dst_off, src_off, len| {
                self.write_contiguous_bytes(&target, dst_off, &data[src_off..src_off + len])
            },
        )?;

        Ok(())
    }

    /// Write a hyperslab into a chunked dataset, one chunk at a time.
    ///
    /// The selection is already validated by [`write_slice`](Self::write_slice).
    /// For each chunk the selection touches, the chunk's share of `data` is
    /// scattered into a whole-chunk buffer and the chunk is rewritten:
    ///
    /// - a chunk the selection covers completely is built from `data` alone —
    ///   nothing needs reading back (libhdf5 takes the same shortcut with the
    ///   `relax` flag of `H5D__chunk_lock`);
    /// - a chunk covered only in part starts from what is already stored, or
    ///   from a fill-value buffer when the chunk has never been written, so
    ///   neighbouring elements survive and untouched ones read as fill.
    ///
    /// An edge chunk that hangs past the dataset extent is always the partial
    /// case, so the region beyond the extent keeps its fill value.
    fn write_slice_chunked(
        &self,
        index: usize,
        starts: &[u64],
        counts: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        if counts.contains(&0) {
            return Ok(());
        }
        let geo = self.chunk_geometry(index)?;
        let ndims = geo.dims.len();
        if geo.chunk_dims.len() != ndims {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset chunk shape has {} dimensions but the dataspace has {}",
                geo.chunk_dims.len(),
                ndims
            )));
        }
        if geo.chunk_dims.contains(&0) {
            return Err(crate::io::IoError::InvalidState(
                "chunk shape has a zero-length dimension".into(),
            ));
        }
        let chunk_bytes = geo.chunk_bytes() as usize;

        // Grid range the selection touches, inclusive on both ends.
        let first: Vec<u64> = (0..ndims).map(|d| starts[d] / geo.chunk_dims[d]).collect();
        let last: Vec<u64> = (0..ndims)
            .map(|d| (starts[d] + counts[d] - 1) / geo.chunk_dims[d])
            .collect();

        let mut coords = first.clone();
        loop {
            // Intersect the selection with this chunk. `in_chunk` is the
            // region's origin inside the chunk, `in_data` its origin inside
            // the caller's counts-shaped buffer, `extent` its size.
            let mut in_chunk = vec![0u64; ndims];
            let mut in_data = vec![0u64; ndims];
            let mut extent = vec![0u64; ndims];
            let mut covers_whole_chunk = true;
            for d in 0..ndims {
                let chunk_origin = coords[d] * geo.chunk_dims[d];
                let lo = starts[d].max(chunk_origin);
                let hi = (starts[d] + counts[d]).min(chunk_origin + geo.chunk_dims[d]);
                in_chunk[d] = lo - chunk_origin;
                in_data[d] = lo - starts[d];
                extent[d] = hi - lo;
                if in_chunk[d] != 0 || extent[d] != geo.chunk_dims[d] {
                    covers_whole_chunk = false;
                }
            }

            let mut buf = if covers_whole_chunk {
                // Every byte is overwritten below.
                vec![0u8; chunk_bytes]
            } else {
                match self.read_chunk_at_coords(index, &coords)? {
                    Some(existing) => {
                        if existing.len() != chunk_bytes {
                            return Err(crate::io::IoError::InvalidState(format!(
                                "stored chunk at {coords:?} is {} bytes but the chunk shape \
                                 needs {chunk_bytes}",
                                existing.len()
                            )));
                        }
                        existing
                    }
                    None => self.new_write_chunk_buffer(index, chunk_bytes),
                }
            };

            for_each_dual_run(
                &geo.chunk_dims,
                &in_chunk,
                counts,
                &in_data,
                &extent,
                geo.element_size,
                |dst_off, src_off, len| {
                    let dst = dst_off as usize;
                    let src = src_off as usize;
                    buf[dst..dst + len].copy_from_slice(&data[src..src + len]);
                    Ok(())
                },
            )?;
            self.write_chunk_at_coords(index, &coords, &buf)?;

            // Odometer over the touched grid range.
            let mut d = ndims;
            loop {
                if d == 0 {
                    return Ok(());
                }
                d -= 1;
                if coords[d] < last[d] {
                    coords[d] += 1;
                    break;
                }
                coords[d] = first[d];
            }
        }
    }

    /// Add an attribute to the root group (file-level attribute), replacing
    /// a same-name attribute. See [`set_attribute`](Self::set_attribute).
    pub fn add_root_attribute(&self, attr: AttributeMessage) -> IoResult<()> {
        self.set_attribute(AttrTarget::Root, attr)
    }

    /// Insert `attr` into the attribute list `target` names, replacing a
    /// same-name attribute.
    ///
    /// The single owner of attribute-list mutation: an `AttributeMessage`
    /// that leaves a list here has its vlen global-heap objects released, so
    /// no replacement — vlen over vlen, numeric over vlen — can strand heap
    /// space (the attribute counterpart of issue #10's dataset fix).
    ///
    /// Under SWMR every attribute mutation is refused, matching libhdf5's
    /// rule for SWMR writes. Object headers are frozen once streaming
    /// starts — a change was committed at close only when the header
    /// happened to be rebuilt (group attrs always, dataset attrs only if
    /// the dataset also got chunk writes) and silently dropped otherwise —
    /// and a replacement's superseded vlen value could never be reclaimed,
    /// since a streaming reader may hold its heap references.
    pub fn set_attribute(&self, target: AttrTarget<'_>, attr: AttributeMessage) -> IoResult<()> {
        self.insert_attribute(target, attr, Created)
    }

    /// The body of [`set_attribute`](Self::set_attribute), told whether the
    /// attribute it is inserting is genuinely new — see [`AttrOrigin`].
    fn insert_attribute(
        &self,
        target: AttrTarget<'_>,
        attr: AttributeMessage,
        origin: AttrOrigin,
    ) -> IoResult<()> {
        if self.swmr_active {
            return Err(swmr_attr_error(&attr.name));
        }
        // Whatever this name meant before, it means the incoming message now.
        self.forget_attribute_reference(self.attr_scope(target)?, &attr.name);
        // No size gate: an attribute whose message is too large for the
        // 16-bit size field an object header message has spills the object's
        // whole attribute set to dense storage at finalize, exactly as
        // `H5O__attr_create` does. See `attributes_need_dense`.
        let mut entry = AttributeEntry::from(attr);
        let old = self.with_attr_list(target, |attrs| {
            if let Some(pos) = attrs.iter().position(|a| a.name() == entry.name()) {
                // `H5O__attr_write` replaces an existing attribute's value and
                // leaves its `crt_idx` alone: the attribute was not created
                // again, so its creation index does not move.
                entry.set_creation_index(attrs[pos].creation_index());
                Some(std::mem::replace(&mut attrs[pos], entry))
            } else {
                // `H5O__attr_create` stamps the set's running maximum onto the
                // new attribute and post-increments it — but only a create
                // reaches for it.
                entry.set_creation_index(match origin {
                    Created => Some(next_creation_index(attrs)),
                    Rewritten(kept) => kept,
                });
                attrs.push(entry);
                None
            }
        })?;
        match old {
            Some(old) => self.release_attr_vlen(&old),
            None => Ok(()),
        }
    }

    /// Set a variable-length string attribute on `target`, replacing any
    /// same-name attribute.
    ///
    /// Owns the whole replacement sequence: the superseded attribute is
    /// removed and its heap objects released *before* the new value's
    /// collection is allocated — the free-before-alloc order (issue #10)
    /// that lets a reopen-replace loop land in the block it just freed
    /// instead of growing the file every session. The cost, as on the
    /// dataset path: a failure between the eviction and the insert below
    /// loses the attribute rather than leaking its heap space.
    pub fn set_vlen_string_attribute(
        &self,
        target: AttrTarget<'_>,
        name: &str,
        value: &str,
    ) -> IoResult<()> {
        let origin = self.evict_attr(target, name)?;
        let attr = self.vlen_string_attribute(name, value)?;
        self.insert_attribute(target, attr, origin)
    }

    /// The array counterpart of
    /// [`set_vlen_string_attribute`](Self::set_vlen_string_attribute).
    pub fn set_vlen_string_array_attribute(
        &self,
        target: AttrTarget<'_>,
        name: &str,
        values: &[&str],
        dims: &[u64],
    ) -> IoResult<()> {
        let origin = self.evict_attr(target, name)?;
        let attr = self.vlen_string_array_attribute(name, values, dims)?;
        self.insert_attribute(target, attr, origin)
    }

    /// Set an attribute on `target` whose value is the object references
    /// naming `paths` — h5py's `obj.attrs['ref'] = f['/target'].ref`.
    ///
    /// `dims` is the attribute's dataspace: empty for the scalar shape a
    /// single reference takes, `&[n]` for an array of them. Each path names a
    /// dataset or a group (`/` is the root group) and must already exist. What
    /// reaches the file is each target's object header address, which finalize
    /// assigns — so the paths are what is stored, and the attribute's message
    /// is built from them every time an object header is
    /// ([`object_attributes`](Self::object_attributes)). The message carries a
    /// zero image of the final width until then.
    pub fn set_object_reference_attribute(
        &self,
        target: AttrTarget<'_>,
        name: &str,
        paths: &[&str],
        dims: &[u64],
    ) -> IoResult<()> {
        let scope = self.attr_scope(target)?;
        // An empty `dims` is the scalar shape, whose one element the empty
        // product already reports.
        let elements: u64 = dims.iter().product();
        if elements != paths.len() as u64 {
            return Err(crate::io::IoError::InvalidState(format!(
                "attribute '{name}' shape {dims:?} needs {elements} references, got {}",
                paths.len()
            )));
        }
        // Resolve now as well as at finalize, so a path that names nothing is
        // reported at the call that got it wrong.
        for path in paths {
            self.object_reference_target(path)?;
        }
        let datatype = DatatypeMessage::object_reference(&self.ctx);
        let image = vec![0u8; paths.len() * datatype.element_size() as usize];
        let attr = if dims.is_empty() {
            AttributeMessage::scalar_numeric(name, datatype, image)
        } else {
            AttributeMessage::array_numeric(name, datatype, dims, image)
        };
        // Through the same owner as every other attribute, which is also what
        // drops any value this name carried before.
        self.set_attribute(target, attr)?;
        self.attribute_references
            .lock()
            .push(AttributeReferenceValue {
                scope,
                name: name.to_string(),
                targets: paths.iter().map(|p| (*p).to_string()).collect(),
                stride: self.ctx.sizeof_addr as usize,
            });
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Dimension scales — the H5DS high-level API (hl/src/H5DS.c)
    // -----------------------------------------------------------------------

    /// Mark dataset `dsid` as a dimension scale — `H5DSset_scale`.
    ///
    /// Writes `CLASS` as the fixed-length null-terminated ASCII string
    /// `DIMENSION_SCALE` and, when `name` is given, `NAME` the same way: the
    /// `H5LT_set_attribute_string` form, one byte longer than the text so the
    /// terminator is stored, which is what `H5DSis_scale` requires of a scale
    /// (a 16-byte null-terminated `CLASS`). Either attribute already there is
    /// deleted and created anew, as `H5LT_set_attribute_string` does, so it
    /// takes a fresh creation index. A dataset with scales of its own
    /// (`DIMENSION_LIST`) is refused, as upstream refuses it.
    pub fn set_dimension_scale(&self, dsid: usize, name: Option<&str>) -> IoResult<()> {
        let scale_path = self.dataset_name(dsid)?;
        if self.dataset_attribute(dsid, DIMENSION_LIST)?.is_some() {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset '{scale_path}' has dimension scales attached and cannot become one"
            )));
        }
        self.set_fixed_string_attribute(dsid, "CLASS", DIMENSION_SCALE_CLASS)?;
        if let Some(name) = name {
            self.set_fixed_string_attribute(dsid, "NAME", name)?;
        }
        Ok(())
    }

    /// Attach dataset `dsid` as a dimension scale of axis `idx` of dataset
    /// `did` — `H5DSattach_scale`.
    ///
    /// Two attributes record the attachment: `DIMENSION_LIST` on `did`, one
    /// variable-length sequence of object references per axis (a scalar
    /// dataset counts as rank 1), and `REFERENCE_LIST` on `dsid`, an array
    /// of `{dataset: H5T_STD_REF_OBJ, dimension: uint}` compounds naming
    /// every (dataset, axis) the scale is attached to. `dsid` is then made a
    /// scale if it is not one already ([`set_dimension_scale`] with no name).
    /// Both lists are rewritten whole; what an existing list holds is read
    /// back as paths (registered this session, or resolved from the file's
    /// addresses), so an attach in an append session keeps earlier
    /// attachments and every reference is stamped with the address its
    /// target ends up at.
    ///
    /// Refused, as upstream refuses them: `did == dsid`; a `did` that is a
    /// scale or carries a reserved `CLASS` (`IMAGE`, `PALETTE`, `TABLE`); a
    /// `dsid` that has scales of its own; an axis beyond `did`'s rank.
    ///
    /// Attaching a scale already attached to that axis changes nothing. This
    /// is stricter than upstream, which leaves `DIMENSION_LIST` as it is but
    /// still appends a duplicate `REFERENCE_LIST` entry; a second entry for
    /// the same (dataset, axis) tells `H5DSis_attached` nothing the first
    /// does not.
    ///
    /// [`set_dimension_scale`]: Self::set_dimension_scale
    pub fn attach_dimension_scale(&self, did: usize, dsid: usize, idx: usize) -> IoResult<()> {
        let data_path = self.dataset_name(did)?;
        let scale_path = self.dataset_name(dsid)?;
        if did == dsid {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset '{data_path}' cannot be its own dimension scale"
            )));
        }
        if self.is_dimension_scale(did)? {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset '{data_path}' is a dimension scale and cannot have scales attached"
            )));
        }
        if self.dataset_attribute(dsid, DIMENSION_LIST)?.is_some() {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset '{scale_path}' has dimension scales attached and cannot be one"
            )));
        }
        if self.has_reserved_class(did)? {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset '{data_path}' holds an image, palette or table and cannot have \
                 dimension scales"
            )));
        }
        let rank = self.ds(did).lock().dataspace.dims.len().max(1);
        if idx >= rank {
            return Err(crate::io::IoError::InvalidState(format!(
                "axis {idx} is out of range for the rank-{rank} dataset '{data_path}'"
            )));
        }

        let mut lists = match self.dimension_list(did)? {
            Some(lists) => lists,
            None => vec![Vec::new(); rank],
        };
        if lists.len() != rank {
            return Err(crate::io::IoError::InvalidState(format!(
                "DIMENSION_LIST of '{data_path}' has {} entries for a rank-{rank} dataset",
                lists.len()
            )));
        }
        if lists[idx].contains(&scale_path) {
            return Ok(());
        }
        lists[idx].push(scale_path);
        self.write_dimension_list(did, &lists)?;

        let mut entries = self.reference_list(dsid)?;
        entries.push((data_path, idx as u32));
        self.write_reference_list(dsid, &entries)?;

        if !self.is_dimension_scale(dsid)? {
            self.set_dimension_scale(dsid, None)?;
        }
        Ok(())
    }

    /// The registry name of live dataset `index`, or why there is none.
    fn dataset_name(&self, index: usize) -> IoResult<String> {
        let count = self.dataset_count();
        if index >= count {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset index {index} out of range (have {count})"
            )));
        }
        let ds = self.ds(index);
        let m = ds.lock();
        if m.deleted {
            return Err(crate::io::IoError::NotFound(format!(
                "dataset '{}' has been deleted",
                m.name
            )));
        }
        Ok(m.name.clone())
    }

    /// The stored attribute `name` of dataset `index`, without marking the
    /// header dirty the way [`with_attr_list`](Self::with_attr_list) must.
    fn dataset_attribute(&self, index: usize, name: &str) -> IoResult<Option<AttributeEntry>> {
        self.dataset_name(index)?;
        Ok(self
            .ds(index)
            .lock()
            .attributes
            .iter()
            .find(|a| a.name() == name)
            .cloned())
    }

    /// Write the scalar fixed-length string attribute `name` = `value` on
    /// dataset `index` — `H5LT_set_attribute_string`: the string is stored
    /// null-terminated in `strlen + 1` bytes, and an attribute of that name
    /// is deleted first rather than written over.
    fn set_fixed_string_attribute(&self, index: usize, name: &str, value: &str) -> IoResult<()> {
        if value.as_bytes().contains(&0) {
            return Err(crate::io::IoError::InvalidState(format!(
                "attribute '{name}' value holds an interior NUL"
            )));
        }
        let size = u32::try_from(value.len() + 1).map_err(|_| {
            crate::io::IoError::InvalidState(format!(
                "attribute '{name}' value of {} bytes exceeds the fixed-string width field",
                value.len()
            ))
        })?;
        let mut data = value.as_bytes().to_vec();
        data.push(0);
        let attr =
            AttributeMessage::scalar_numeric(name, DatatypeMessage::fixed_string(size), data);
        let target = AttrTarget::Dataset(index);
        self.evict_attr(target, name)?;
        self.insert_attribute(target, attr, Created)
    }

    /// The `CLASS` attribute of dataset `index`, read the way `H5DS` reads
    /// it: as a C string, up to the first NUL.
    fn class_attribute(&self, index: usize) -> IoResult<Option<ClassAttr>> {
        use crate::format::global_heap::decode_vlen_reference;

        let Some(entry) = self.dataset_attribute(index, "CLASS")? else {
            return Ok(None);
        };
        let msg = entry.decoded().map_err(|reason| {
            crate::io::IoError::InvalidState(format!(
                "CLASS attribute of '{}' cannot be decoded: {reason}",
                self.ds(index).lock().name
            ))
        })?;
        Ok(Some(match &msg.datatype {
            DatatypeMessage::FixedString { size, padding, .. } => {
                let avail = (*size as usize).min(msg.data.len());
                ClassAttr::Fixed {
                    size: *size,
                    null_terminated: *padding == 0,
                    text: c_string(&msg.data[..avail]),
                }
            }
            DatatypeMessage::VarLenString { .. } => {
                let (_, addr, obj_idx) = decode_vlen_reference(&msg.data, &self.ctx)?;
                let bytes = if addr == 0 || addr == UNDEF_ADDR {
                    Vec::new()
                } else {
                    let obj_idx = u16::try_from(obj_idx).map_err(|_| {
                        crate::io::IoError::InvalidState(format!(
                            "global heap object index {obj_idx} does not fit the 16-bit on-disk \
                             field"
                        ))
                    })?;
                    self.read_heap_object(addr, obj_idx)?
                };
                ClassAttr::VarLen(c_string(&bytes))
            }
            _ => ClassAttr::NotString,
        }))
    }

    /// `H5DSis_scale`: a `CLASS` that is a string saying `DIMENSION_SCALE` —
    /// and, for a fixed-length string, null-terminated and exactly 16 bytes
    /// wide, the width the spec gives the attribute.
    fn is_dimension_scale(&self, index: usize) -> IoResult<bool> {
        Ok(match self.class_attribute(index)? {
            None | Some(ClassAttr::NotString) => false,
            Some(ClassAttr::Fixed {
                size,
                null_terminated,
                text,
            }) => null_terminated && size == 16 && text == DIMENSION_SCALE_CLASS,
            Some(ClassAttr::VarLen(text)) => text == DIMENSION_SCALE_CLASS,
        })
    }

    /// `H5DS_is_reserved`: a `CLASS` naming an image, palette or table — the
    /// datasets the other high-level APIs own. A `CLASS` that is not a string
    /// is an error here, where [`is_dimension_scale`](Self::is_dimension_scale)
    /// reads it as "not a scale", because that is how upstream splits them.
    fn has_reserved_class(&self, index: usize) -> IoResult<bool> {
        Ok(match self.class_attribute(index)? {
            None => false,
            Some(ClassAttr::NotString) => {
                return Err(crate::io::IoError::InvalidState(format!(
                    "CLASS attribute of '{}' is not a string",
                    self.ds(index).lock().name
                )))
            }
            Some(ClassAttr::Fixed { text, .. }) | Some(ClassAttr::VarLen(text)) => {
                matches!(text.as_str(), "IMAGE" | "PALETTE" | "TABLE")
            }
        })
    }

    /// The bytes of object `index` in the global heap collection at
    /// `collection` — `H5HG_read`.
    fn read_heap_object(&self, collection: u64, index: u16) -> IoResult<Vec<u8>> {
        use crate::format::global_heap::GlobalHeapCollection;

        let mut image = self.handle.read_at_most(collection, 4096)?;
        let declared = GlobalHeapCollection::decode_size(&image, &self.ctx)?;
        if declared > image.len() {
            image = self.handle.read_at(collection, declared)?;
        }
        let (gcol, _) = GlobalHeapCollection::decode(&image[..declared], &self.ctx)?;
        gcol.get_object(index).map(<[u8]>::to_vec).ok_or_else(|| {
            crate::io::IoError::InvalidState(format!(
                "global heap collection {collection:#x} has no object {index}"
            ))
        })
    }

    /// The path of the object whose header is at `addr` in the file as it
    /// was opened — what an object reference read back from an append
    /// session's existing attributes names.
    fn path_of_header_address(&self, addr: u64) -> IoResult<String> {
        for ds in self.dataset_refs() {
            let m = ds.lock();
            if !m.deleted && m.obj_header_written_addr == Some(addr) {
                return Ok(m.name.clone());
            }
        }
        for grp in self.group_refs() {
            let g = grp.lock();
            if !g.deleted && g.obj_header_written_addr == Some(addr) {
                return Ok(g.name.clone());
            }
        }
        Err(crate::io::IoError::InvalidState(format!(
            "object reference to header {addr:#x} names no dataset or group of this file"
        )))
    }

    /// The path a reference slot inside a global heap object names: the one
    /// registered for stamping when this session wrote the slot, else the
    /// one the address on disk resolves to.
    fn heap_reference_path(
        &self,
        collection: u64,
        index: u16,
        token_offset: usize,
        on_disk: &[u8],
    ) -> IoResult<String> {
        let registered = self
            .pending_heap_references
            .lock()
            .iter()
            .find(|p| {
                p.collection == collection && p.index == index && p.token_offset == token_offset
            })
            .map(|p| match &p.target {
                PendingHeapTarget::Dataset(path) | PendingHeapTarget::Object(path) => path.clone(),
            });
        if let Some(path) = registered {
            return Ok(path);
        }
        let mut raw = [0u8; 8];
        raw[..on_disk.len()].copy_from_slice(on_disk);
        self.path_of_header_address(u64::from_le_bytes(raw))
    }

    /// Dataset `did`'s `DIMENSION_LIST` as the paths of the scales on each
    /// axis, or `None` when it has no such attribute.
    fn dimension_list(&self, did: usize) -> IoResult<Option<Vec<Vec<String>>>> {
        use crate::format::global_heap::{decode_vlen_reference, vlen_reference_size};

        let Some(entry) = self.dataset_attribute(did, DIMENSION_LIST)? else {
            return Ok(None);
        };
        let name = || self.ds(did).lock().name.clone();
        let msg = entry.decoded().map_err(|reason| {
            crate::io::IoError::InvalidState(format!(
                "DIMENSION_LIST of '{}' cannot be decoded: {reason}",
                name()
            ))
        })?;
        match &msg.datatype {
            DatatypeMessage::VarLenSequence { base }
                if matches!(
                    **base,
                    DatatypeMessage::Reference {
                        kind: ReferenceKind::Object1,
                        ..
                    }
                ) => {}
            other => {
                return Err(crate::io::IoError::InvalidState(format!(
                    "DIMENSION_LIST of '{}' is {other}; only a sequence of H5T_STD_REF_OBJ \
                     references is supported",
                    name()
                )))
            }
        }
        let sa = self.ctx.sizeof_addr as usize;
        let ref_size = vlen_reference_size(&self.ctx);
        let mut lists = Vec::new();
        for elem in msg.data.chunks_exact(ref_size) {
            let (seq_len, addr, obj_idx) = decode_vlen_reference(elem, &self.ctx)?;
            let seq_len = seq_len as usize;
            let mut paths = Vec::with_capacity(seq_len);
            if seq_len > 0 {
                let index = u16::try_from(obj_idx).map_err(|_| {
                    crate::io::IoError::InvalidState(format!(
                        "global heap object index {obj_idx} does not fit the 16-bit on-disk field"
                    ))
                })?;
                let bytes = self.read_heap_object(addr, index)?;
                if bytes.len() < seq_len * sa {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "DIMENSION_LIST of '{}' names {seq_len} scales in a {}-byte heap object",
                        name(),
                        bytes.len()
                    )));
                }
                for k in 0..seq_len {
                    paths.push(self.heap_reference_path(
                        addr,
                        index,
                        k * sa,
                        &bytes[k * sa..(k + 1) * sa],
                    )?);
                }
            }
            lists.push(paths);
        }
        Ok(Some(lists))
    }

    /// Store `lists` — the scales attached to each axis — as dataset `did`'s
    /// `DIMENSION_LIST`, replacing the one it has.
    ///
    /// Each axis is one global heap object of `sizeof_addr` bytes per scale,
    /// zero until finalize stamps the scale's header address in through
    /// [`write_heap_reference_values`](Self::write_heap_reference_values);
    /// an axis with no scale is an empty heap object, as libhdf5's
    /// `H5VL__native_blob_put` stores an empty sequence. The attribute's
    /// value is the vlen reference to each object, final at write time.
    fn write_dimension_list(&self, did: usize, lists: &[Vec<String>]) -> IoResult<()> {
        use crate::format::global_heap::{
            encode_vlen_reference, vlen_reference_size, vlen_seq_len,
        };

        let target = AttrTarget::Dataset(did);
        let origin = self.evict_attr(target, DIMENSION_LIST)?;
        let sa = self.ctx.sizeof_addr as usize;
        let blobs: Vec<Vec<u8>> = lists.iter().map(|l| vec![0u8; l.len() * sa]).collect();
        let items: Vec<&[u8]> = blobs.iter().map(Vec::as_slice).collect();
        let placements = self.insert_vlen_objects(&items)?;

        let mut data = Vec::with_capacity(lists.len() * vlen_reference_size(&self.ctx));
        let mut pending = self.pending_heap_references.lock();
        for (axis, &(collection, index)) in placements.iter().enumerate() {
            for (k, path) in lists[axis].iter().enumerate() {
                pending.push(PendingHeapReference {
                    collection,
                    index,
                    token_offset: k * sa,
                    target: PendingHeapTarget::Object(path.clone()),
                });
            }
            data.extend_from_slice(&encode_vlen_reference(
                vlen_seq_len(lists[axis].len())?,
                collection,
                u32::from(index),
                &self.ctx,
            ));
        }
        drop(pending);

        let attr = AttributeMessage {
            name: DIMENSION_LIST.to_string(),
            datatype: DatatypeMessage::VarLenSequence {
                base: Box::new(DatatypeMessage::object_reference(&self.ctx)),
            },
            dataspace: DataspaceMessage::simple(&[lists.len() as u64]),
            data,
        };
        self.insert_attribute(target, attr, origin)
    }

    /// Scale `dsid`'s `REFERENCE_LIST` as (dataset path, axis) pairs; empty
    /// when it has no such attribute.
    fn reference_list(&self, dsid: usize) -> IoResult<Vec<(String, u32)>> {
        let Some(entry) = self.dataset_attribute(dsid, REFERENCE_LIST)? else {
            return Ok(Vec::new());
        };
        let name = || self.ds(dsid).lock().name.clone();
        let msg = entry.decoded().map_err(|reason| {
            crate::io::IoError::InvalidState(format!(
                "REFERENCE_LIST of '{}' cannot be decoded: {reason}",
                name()
            ))
        })?;
        let unsupported = |why: String| {
            crate::io::IoError::InvalidState(format!(
                "REFERENCE_LIST of '{}' is {}; {why}",
                name(),
                msg.datatype
            ))
        };
        let DatatypeMessage::Compound { size, members } = &msg.datatype else {
            return Err(unsupported("a compound is required".into()));
        };
        let member = |m: &str| {
            members
                .iter()
                .find(|c| c.name == m)
                .ok_or_else(|| unsupported(format!("member '{m}' is missing")))
        };
        let dataset = member("dataset")?;
        let dimension = member("dimension")?;
        let sa = self.ctx.sizeof_addr as usize;
        if !matches!(
            dataset.datatype,
            DatatypeMessage::Reference {
                kind: ReferenceKind::Object1,
                ..
            }
        ) {
            return Err(unsupported(
                "only an H5T_STD_REF_OBJ 'dataset' member is supported".into(),
            ));
        }
        let DatatypeMessage::FixedPoint {
            size: 4,
            byte_order,
            ..
        } = dimension.datatype
        else {
            return Err(unsupported(
                "a 4-byte integer 'dimension' member is required".into(),
            ));
        };
        let stride = *size as usize;
        let registered: Option<Vec<String>> = self
            .attribute_references
            .lock()
            .iter()
            .find(|r| r.scope == AttrScope::Dataset(dsid) && r.name == REFERENCE_LIST)
            .map(|r| r.targets.clone());
        let mut entries = Vec::with_capacity(msg.data.len() / stride);
        for (i, elem) in msg.data.chunks_exact(stride).enumerate() {
            let at = |offset: u32, len: usize| {
                elem.get(offset as usize..offset as usize + len)
                    .ok_or_else(|| unsupported(format!("element {i} is too short for its members")))
            };
            let path = match &registered {
                Some(targets) => targets.get(i).cloned().ok_or_else(|| {
                    crate::io::IoError::InvalidState(format!(
                        "REFERENCE_LIST of '{}' entry {i} has no registered target",
                        name()
                    ))
                })?,
                None => {
                    let mut raw = [0u8; 8];
                    raw[..sa].copy_from_slice(at(dataset.offset, sa)?);
                    self.path_of_header_address(u64::from_le_bytes(raw))?
                }
            };
            let dim: [u8; 4] = at(dimension.offset, 4)?.try_into().expect("4 bytes");
            let dim = match byte_order {
                ByteOrder::LittleEndian => u32::from_le_bytes(dim),
                ByteOrder::BigEndian => u32::from_be_bytes(dim),
            };
            entries.push((path, dim));
        }
        Ok(entries)
    }

    /// Store `entries` as scale `dsid`'s `REFERENCE_LIST`, replacing the one
    /// it has — deleted and created anew, as upstream does, so it takes a
    /// fresh creation index.
    ///
    /// The element is libhdf5's `ds_list_t` as it lands on disk: the
    /// reference at offset 0, `dimension` right after it, and the struct's
    /// trailing padding — 16 bytes over 8-byte addresses. The addresses are
    /// stamped at finalize through [`object_attributes`](Self::object_attributes)
    /// like any reference attribute's; the `dimension` fields are final here.
    fn write_reference_list(&self, dsid: usize, entries: &[(String, u32)]) -> IoResult<()> {
        use crate::format::messages::datatype::CompoundMember;

        let target = AttrTarget::Dataset(dsid);
        self.evict_attr(target, REFERENCE_LIST)?;
        let sa = self.ctx.sizeof_addr as usize;
        let stride = sa + 8;
        let datatype = DatatypeMessage::compound(
            stride as u32,
            vec![
                CompoundMember {
                    name: "dataset".to_string(),
                    offset: 0,
                    datatype: DatatypeMessage::object_reference(&self.ctx),
                },
                CompoundMember {
                    name: "dimension".to_string(),
                    offset: sa as u32,
                    datatype: DatatypeMessage::u32_type(),
                },
            ],
        );
        let mut data = vec![0u8; entries.len() * stride];
        for (i, (_, dim)) in entries.iter().enumerate() {
            data[i * stride + sa..i * stride + sa + 4].copy_from_slice(&dim.to_le_bytes());
        }
        let attr = AttributeMessage::array_numeric(
            REFERENCE_LIST,
            datatype,
            &[entries.len() as u64],
            data,
        );
        self.insert_attribute(target, attr, Created)?;
        self.attribute_references
            .lock()
            .push(AttributeReferenceValue {
                scope: AttrScope::Dataset(dsid),
                name: REFERENCE_LIST.to_string(),
                targets: entries.iter().map(|(p, _)| p.clone()).collect(),
                stride,
            });
        Ok(())
    }

    /// Take the attribute `name` off `target`'s list, releasing its heap
    /// objects. No-op when absent. Refused under SWMR — see
    /// [`set_attribute`](Self::set_attribute).
    ///
    /// What it answers is what the insert that follows it must be told: an
    /// attribute that was there is being rewritten and keeps its creation
    /// index, and one that was not is created.
    fn evict_attr(&self, target: AttrTarget<'_>, name: &str) -> IoResult<AttrOrigin> {
        if self.swmr_active {
            return Err(swmr_attr_error(name));
        }
        self.forget_attribute_reference(self.attr_scope(target)?, name);
        let old = self.with_attr_list(target, |attrs| {
            attrs
                .iter()
                .position(|a| a.name() == name)
                .map(|pos| attrs.remove(pos))
        })?;
        match old {
            Some(old) => {
                let origin = Rewritten(old.creation_index());
                self.release_attr_vlen(&old)?;
                Ok(origin)
            }
            None => Ok(Created),
        }
    }

    /// Release the global-heap objects a superseded attribute owned.
    /// Recognizes top-level vlen datatypes only: a *compound* attribute
    /// with vlen members — which this crate cannot write, only a foreign
    /// file can carry — keeps its members' heap objects when replaced or
    /// deleted, the storage cost the foreign writer accepted. Every other
    /// class stores its value inline in the message. Per-object removal
    /// keeps collections shared with other refs (libhdf5-written files)
    /// intact.
    fn release_attr_vlen(&self, old: &AttributeEntry) -> IoResult<()> {
        use crate::format::messages::datatype::DatatypeMessage;
        // An attribute whose message this crate could not decode keeps
        // whatever heap space it references: releasing objects named by bytes
        // we cannot interpret would free storage that is still live.
        let Some(old) = old.readable() else {
            return Ok(());
        };
        if matches!(
            old.datatype,
            DatatypeMessage::VarLenString { .. } | DatatypeMessage::VarLenSequence { .. }
        ) {
            self.release_vlen_references(&old.data)?;
        }
        Ok(())
    }

    /// Run `f` on the attribute list `target` names — the accessor every
    /// attribute mutation shares.
    fn with_attr_list<R>(
        &self,
        target: AttrTarget<'_>,
        f: impl FnOnce(&mut Vec<AttributeEntry>) -> R,
    ) -> IoResult<R> {
        match target {
            AttrTarget::Root => Ok(f(&mut self.root_attributes.lock())),
            AttrTarget::Group(path) => {
                let path = self.canonical_group_path(path);
                for grp in self.group_refs() {
                    let mut g = grp.lock();
                    if g.name == path && !g.deleted {
                        return Ok(f(&mut g.attributes));
                    }
                }
                Err(crate::io::IoError::NotFound(format!(
                    "group '{path}' not found"
                )))
            }
            AttrTarget::Dataset(index) => {
                let count = self.dataset_count();
                if index >= count {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "dataset index {index} out of range (have {count})"
                    )));
                }
                let ds = self.ds(index);
                let mut m = ds.lock();
                // Every caller of this mutates the list, and a reopened
                // dataset's header is rewritten only when it is marked stale.
                m.header_dirty = true;
                Ok(f(&mut m.attributes))
            }
        }
    }

    /// Store each of `items` as a global heap object and return its
    /// placement `(collection address, object index)`, in input order —
    /// the writer side of libhdf5's `H5HG_insert`.
    ///
    /// Placement follows libhdf5: a collection from the CWFS list takes an
    /// item when its free space holds the object *and* a residual
    /// free-space marker header (`encode_at_size` always emits the
    /// marker); what no listed collection can take goes into a fresh
    /// collection, spilling into another at the 65535-object index cap.
    /// One batch may therefore span several collections — invisible to
    /// readers, which resolve each reference's own collection address. An
    /// empty batch allocates nothing: an empty collection still encodes
    /// to the 4096-byte `H5HG_MINALLOC` minimum, a block nothing would
    /// reference. libhdf5 additionally tries to extend a nearly-full
    /// collection's block in place (`H5MF_try_extend`); this writer does
    /// not — an oversized item always starts a fresh collection.
    ///
    /// The `cwfs` lock is held across every read-modify-rewrite of a
    /// listed collection block: it serializes concurrent inserts (two
    /// datasets' writers can pack the same block) and inserts against
    /// [`release_vlen_references`](Self::release_vlen_references), which
    /// rewrites the same blocks when objects are freed.
    ///
    /// Under SWMR the CWFS list is neither consulted nor updated and every
    /// batch gets fresh collections: packing rewrites a block a streaming
    /// reader may be mid-walk on — the same reason `place_chunk` keeps a
    /// relocated chunk's old block.
    fn insert_vlen_objects(&self, items: &[&[u8]]) -> IoResult<Vec<(u64, u16)>> {
        use crate::format::global_heap::{GlobalHeapCollection, GlobalHeapObject};

        if items.is_empty() {
            return Ok(Vec::new());
        }
        let objhdr = GlobalHeapCollection::object_disk_size(&self.ctx, 0);
        let mut placements = Vec::with_capacity(items.len());
        let mut i = 0;

        // Pack into listed collections while one can take the next item.
        if !self.swmr_active {
            let mut cwfs = self.cwfs.lock();
            while i < items.len() {
                let need = GlobalHeapCollection::object_disk_size(&self.ctx, items[i].len());
                let Some(pos) = cwfs.iter().position(|e| e.free >= need + objhdr) else {
                    // Second pass of libhdf5's H5F_cwfs_find_free_heap: no
                    // listed collection has room, so try to grow one in
                    // place before falling back to a fresh collection.
                    if self.extend_listed_collection(&mut cwfs, need + objhdr)? {
                        continue;
                    }
                    break;
                };
                let (addr, size) = (cwfs[pos].addr, cwfs[pos].size);
                let image = self.handle.read_at(addr, size)?;
                let (mut gcol, _) = GlobalHeapCollection::decode(&image[..size], &self.ctx)?;
                // The disk is the truth for free space; the entry is a hint.
                let Some(mut free) = gcol.free_space_at(&self.ctx, size) else {
                    cwfs.remove(pos);
                    continue;
                };
                let mut next_idx = gcol.max_index();
                let mut took = false;
                while i < items.len() && next_idx < u16::MAX {
                    let need = GlobalHeapCollection::object_disk_size(&self.ctx, items[i].len());
                    if free < need + objhdr {
                        break;
                    }
                    next_idx += 1;
                    gcol.objects.push(GlobalHeapObject {
                        index: next_idx,
                        ref_count: 0,
                        data: items[i].to_vec(),
                    });
                    placements.push((addr, next_idx));
                    free -= need;
                    took = true;
                    i += 1;
                }
                if took {
                    let rewritten = gcol.encode_at_size(&self.ctx, size)?;
                    self.handle.write_at(addr, &rewritten)?;
                    // Correct the entry to the measured free space and move
                    // it to the front — libhdf5 keeps `cwfs` in
                    // most-recently-used order.
                    let mut e = cwfs.remove(pos);
                    e.free = free;
                    cwfs.insert(0, e);
                } else if next_idx == u16::MAX {
                    // At the index cap nothing can be inserted no matter the
                    // free space; drop the entry or the scan re-picks it
                    // forever. (A removal can lower the top index again, and
                    // the release side re-lists the collection then.)
                    cwfs.remove(pos);
                } else {
                    // The hint overstated the block's free space — shrink it
                    // to the measured value so the scan moves on.
                    cwfs[pos].free = free;
                }
            }
        }

        // What remains goes into fresh collections.
        while i < items.len() {
            let mut gcol = GlobalHeapCollection::new();
            // Objects are pushed with a running index: `add_object` rescans
            // for the max index per call, O(n²) across a spill-sized batch.
            let mut next_idx: u16 = 0;
            while i < items.len() && next_idx < u16::MAX {
                next_idx += 1;
                gcol.objects.push(GlobalHeapObject {
                    index: next_idx,
                    ref_count: 0,
                    data: items[i].to_vec(),
                });
                i += 1;
            }
            let encoded = gcol.encode(&self.ctx);
            let addr = self
                .allocator
                .allocate(encoded.len() as u64, FreeSpaceClass::RawData);
            self.handle.write_at(addr, &encoded)?;
            for idx in 1..=next_idx {
                placements.push((addr, idx));
            }
            // List the block's leftover free space for later inserts — the
            // minimum-size padding of a small batch is most of 4096 bytes.
            // Below two object headers not even an empty object fits.
            if !self.swmr_active {
                if let Some(free) = gcol.free_space_at(&self.ctx, encoded.len()) {
                    if free >= 2 * objhdr {
                        cwfs_note(&mut self.cwfs.lock(), addr, encoded.len(), free);
                    }
                }
            }
        }
        Ok(placements)
    }

    /// Try to extend one listed collection in place so it can take an
    /// object needing `want` bytes of free space — the second pass of
    /// libhdf5's `H5F_cwfs_find_free_heap`: grow the file allocation
    /// ([`FileAllocator::try_extend`], mirroring `H5MF_try_extend`) and then
    /// the collection itself (`H5HG_extend`: a larger declared size and a
    /// free-space marker covering the new tail — here by re-encoding at the
    /// grown size, which writes exactly those two things).
    ///
    /// Extension size is `max(collection_size, shortfall)` — at least a
    /// doubling — capped so the result stays within [`GCOL_MAX_SIZE`], both
    /// as upstream computes them. On success the grown entry moves to the
    /// front of the list and the caller's scan re-picks it; the free-space
    /// measurement is taken from the block on disk, not the list's hint, so
    /// the rewrite and the entry agree.
    ///
    /// Caller holds the `cwfs` lock (it passes the guarded list), which is
    /// what serializes this read-modify-rewrite against concurrent inserts
    /// and releases.
    fn extend_listed_collection(&self, cwfs: &mut Vec<CwfsEntry>, want: usize) -> IoResult<bool> {
        use crate::format::global_heap::{GlobalHeapCollection, GCOL_MAX_SIZE};

        let mut pos = 0;
        while pos < cwfs.len() {
            let (addr, size) = (cwfs[pos].addr, cwfs[pos].size);
            let image = self.handle.read_at(addr, size)?;
            let (gcol, _) = GlobalHeapCollection::decode(&image[..size], &self.ctx)?;
            // The disk is the truth for free space; the entry is a hint.
            let Some(free) = gcol.free_space_at(&self.ctx, size) else {
                cwfs.remove(pos);
                continue;
            };
            // A hint can understate the block (upstream's FREE_SIZE is its
            // in-memory truth and cannot): if the block already has room,
            // correct the hint instead of doubling the collection.
            if free >= want {
                cwfs[pos].free = free;
                return Ok(true);
            }
            let new_need = size.max(want.saturating_sub(free));
            if size + new_need > GCOL_MAX_SIZE
                || !self.allocator.try_extend(
                    addr,
                    size as u64,
                    new_need as u64,
                    FreeSpaceClass::RawData,
                )
            {
                pos += 1;
                continue;
            }
            let new_size = size + new_need;
            let rewritten = gcol.encode_at_size(&self.ctx, new_size)?;
            self.handle.write_at(addr, &rewritten)?;
            let mut e = cwfs.remove(pos);
            e.size = new_size;
            e.free = free + new_need;
            cwfs.insert(0, e);
            return Ok(true);
        }
        Ok(false)
    }

    /// Create a variable-length string dataset and write string data.
    ///
    /// Stores strings in the global heap. The dataset raw data consists of
    /// vlen references (collection_addr + object_index pairs).
    ///
    /// `charset` is the datatype's declared character set (0 = ASCII,
    /// 1 = UTF-8); the strings are checked against it before anything is
    /// written, so the type never misdescribes the bytes under it.
    pub fn create_vlen_string_dataset(
        &self,
        name: &str,
        strings: &[&str],
        charset: u8,
    ) -> IoResult<usize> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::datatype::DatatypeMessage;

        ensure_vlen_charset(charset, strings)?;

        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let num_strings = strings.len() as u64;

        // Store the strings as heap objects; a batch that fits an earlier
        // collection's free space shares its block.
        let items: Vec<&[u8]> = strings.iter().map(|s| s.as_bytes()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        // Build raw data: vlen references
        let ref_size = crate::format::global_heap::vlen_reference_size(&self.ctx);
        let data_size = (num_strings as usize) * ref_size;
        let mut raw_data = Vec::with_capacity(data_size);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            let seq_len = crate::format::global_heap::vlen_seq_len(strings[i].len())?;
            raw_data.extend_from_slice(&encode_vlen_reference(
                seq_len,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }

        // Allocate and write raw data
        let data_addr = self
            .allocator
            .allocate(data_size as u64, FreeSpaceClass::RawData);
        self.handle.write_at(data_addr, &raw_data)?;

        // Create the dataset with vlen string datatype
        let datatype = DatatypeMessage::VarLenString {
            padding: 0,
            charset,
        };
        let dataspace =
            crate::format::messages::dataspace::DataspaceMessage::simple(&[num_strings]);

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr,
                data_size: data_size as u64,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                append: None,
            },
        );

        Ok(idx)
    }

    /// Create a 1-D variable-length byte-array dataset.
    ///
    /// The `u8` case of [`create_vlen_sequence_dataset`], where an item's
    /// byte image and its element count are the same number.
    ///
    /// [`create_vlen_sequence_dataset`]: Self::create_vlen_sequence_dataset
    ///
    /// Superseded in production by [`write_vlen_numeric`](crate::H5File::write_vlen_numeric)
    /// (`H5Group::write_vlen_bytes` routes through it, not through here);
    /// kept as a direct entry point for this crate's own white-box tests.
    #[cfg(test)]
    pub fn create_vlen_bytes_dataset(&self, name: &str, items: &[&[u8]]) -> IoResult<usize> {
        use crate::format::messages::datatype::DatatypeMessage;

        self.create_vlen_sequence_dataset(name, DatatypeMessage::u8_type(), items)
    }

    /// Create a 1-D variable-length sequence dataset over `base`.
    ///
    /// Each item is the encoded image of one sequence — `n * base.element_size()`
    /// bytes in the base type's own byte order — and is stored as a global-heap
    /// object; the dataset holds one vlen reference per item, the same on-disk
    /// shape a vlen string dataset has. The `H5T_VLEN` length field counts base
    /// elements rather than bytes, so an image whose length is not a whole
    /// number of elements is refused here rather than stored under a length
    /// that misreads it.
    pub fn create_vlen_sequence_dataset(
        &self,
        name: &str,
        base: DatatypeMessage,
        items: &[&[u8]],
    ) -> IoResult<usize> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::datatype::DatatypeMessage;

        let elem_size = base.element_size() as usize;
        if elem_size == 0 {
            return Err(crate::io::IoError::InvalidState(format!(
                "vlen base datatype {base} has no element size"
            )));
        }
        for (i, item) in items.iter().enumerate() {
            if !item.len().is_multiple_of(elem_size) {
                return Err(crate::io::IoError::InvalidState(format!(
                    "sequence {i} is {} bytes, not a whole number of {elem_size}-byte elements",
                    item.len()
                )));
            }
        }

        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let num_items = items.len() as u64;

        // Store the sequence images as heap objects, sharing collection
        // blocks as `create_vlen_string_dataset` does.
        let placements = self.insert_vlen_objects(items)?;

        // Build raw data: one vlen reference per item.
        let ref_size = crate::format::global_heap::vlen_reference_size(&self.ctx);
        let data_size = (num_items as usize) * ref_size;
        let mut raw_data = Vec::with_capacity(data_size);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            let seq_len = crate::format::global_heap::vlen_seq_len(items[i].len() / elem_size)?;
            raw_data.extend_from_slice(&encode_vlen_reference(
                seq_len,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }

        // Allocate and write raw data.
        let data_addr = self
            .allocator
            .allocate(data_size as u64, FreeSpaceClass::RawData);
        self.handle.write_at(data_addr, &raw_data)?;

        let datatype = DatatypeMessage::VarLenSequence {
            base: Box::new(base),
        };
        let dataspace = crate::format::messages::dataspace::DataspaceMessage::simple(&[num_items]);

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr,
                data_size: data_size as u64,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version: 4,
                times: self.created_object_times(),
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                append: None,
            },
        );

        Ok(idx)
    }

    /// Create a chunked, compressed variable-length string dataset.
    ///
    /// Strings are stored in the global heap (same as `create_vlen_string_dataset`),
    /// but the vlen references are stored in chunked layout with the given filter
    /// pipeline (e.g., deflate, zstd). `chunk_size` is the number of strings per chunk.
    pub fn create_vlen_string_dataset_compressed(
        &self,
        name: &str,
        strings: &[&str],
        chunk_size: usize,
        pipeline: FilterPipeline,
    ) -> IoResult<usize> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::datatype::DatatypeMessage;

        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        let num_strings = strings.len() as u64;
        validate_chunk_geometry(&[num_strings], &[num_strings], &[chunk_size as u64])?;

        // Store the strings as heap objects; the geometry validation above
        // must precede this so a refused call allocates nothing.
        let items: Vec<&[u8]> = strings.iter().map(|s| s.as_bytes()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        // Build raw data: vlen references
        let ref_size = crate::format::global_heap::vlen_reference_size(&self.ctx);
        let data_size = (num_strings as usize) * ref_size;
        let mut raw_data = Vec::with_capacity(data_size);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            let seq_len = crate::format::global_heap::vlen_seq_len(strings[i].len())?;
            raw_data.extend_from_slice(&encode_vlen_reference(
                seq_len,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }

        // Set up chunked compressed layout
        let datatype = DatatypeMessage::vlen_string_utf8();
        let element_size = datatype.element_size_ctx(&self.ctx) as u64;
        let chunk_dims: Vec<u64> = vec![chunk_size as u64];
        let dims: Vec<u64> = vec![num_strings];
        let max_dims: Vec<u64> = vec![num_strings];
        let chunk_bytes = chunk_size as u64 * element_size;
        let layout_version = self.chunk_layout_version(true, chunk_bytes);
        let chunk_size_len = self.chunk_size_len_for(layout_version, chunk_bytes);

        let earray_params = EarrayParams::default_params();
        let ndblk_addrs = compute_ndblk_addrs(earray_params.sup_blk_min_data_ptrs)?;
        let nsblk_addrs = compute_nsblk_addrs(
            earray_params.idx_blk_elmts,
            earray_params.data_blk_min_elmts,
            earray_params.sup_blk_min_data_ptrs,
            earray_params.max_nelmts_bits,
        )?;

        // Create filtered EA header
        let mut ea_header =
            ExtensibleArrayHeader::new_for_filtered_chunks(&self.ctx, chunk_size_len);
        ea_header.max_nelmts_bits = earray_params.max_nelmts_bits;
        ea_header.idx_blk_elmts = earray_params.idx_blk_elmts;
        ea_header.data_blk_min_elmts = earray_params.data_blk_min_elmts;
        ea_header.sup_blk_min_data_ptrs = earray_params.sup_blk_min_data_ptrs;
        ea_header.max_dblk_page_nelmts_bits = earray_params.max_dblk_page_nelmts_bits;

        let hdr_encoded = ea_header.encode(&self.ctx);
        let ea_header_addr = self
            .allocator
            .allocate(hdr_encoded.len() as u64, FreeSpaceClass::Metadata);

        // Create filtered index block
        let filt_iblk = FilteredIndexBlock::new(
            ea_header_addr,
            earray_params.idx_blk_elmts,
            ndblk_addrs,
            nsblk_addrs,
        );
        let iblk_encoded = filt_iblk.encode(&self.ctx, chunk_size_len);
        let ea_iblk_addr = self
            .allocator
            .allocate(iblk_encoded.len() as u64, FreeSpaceClass::Metadata);

        ea_header.idx_blk_addr = ea_iblk_addr;

        let hdr_encoded = ea_header.encode(&self.ctx);
        self.handle.write_at(ea_header_addr, &hdr_encoded)?;
        self.handle.write_at(ea_iblk_addr, &iblk_encoded)?;

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };

        let ea_iblk = ExtensibleArrayIndexBlock::new(
            ea_header_addr,
            earray_params.idx_blk_elmts,
            ndblk_addrs,
            nsblk_addrs,
        );

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: Some(pipeline),
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                chunked: Some(ChunkedDatasetInfo {
                    chunk_dims: chunk_dims.clone(),
                    earray_params,
                    ea_header_addr,
                    ea_iblk_addr,
                    ea_header,
                    ea_iblk,
                    chunks_written: 0,
                    filt_iblk: Some(filt_iblk),
                    chunk_size_len,
                }),
                append: None,
            },
        );

        // Write chunks of vlen references with compression
        let chunk_byte_size = chunk_bytes as usize;
        let num_chunks = raw_data.len().div_ceil(chunk_byte_size);
        for chunk_i in 0..num_chunks {
            let start = chunk_i * chunk_byte_size;
            let end = (start + chunk_byte_size).min(raw_data.len());
            let chunk_data = if end - start < chunk_byte_size {
                // Pad last chunk to full size (vlen datasets carry no user
                // fill value, so this resolves to zero = null vlen reference).
                let mut padded = self.new_chunk_buffer(idx, chunk_byte_size);
                padded[..end - start].copy_from_slice(&raw_data[start..end]);
                padded
            } else {
                raw_data[start..end].to_vec()
            };
            self.write_chunk(idx, chunk_i as u64, &chunk_data)?;
        }

        Ok(idx)
    }

    /// Create an empty chunked vlen string dataset ready for incremental appends.
    ///
    /// The dataset starts with `dims = [0]` and `max_dims = [unlimited]`.
    /// Use `append_vlen_strings` to add data.
    pub fn create_appendable_vlen_string_dataset(
        &self,
        name: &str,
        chunk_size: usize,
        pipeline: Option<FilterPipeline>,
    ) -> IoResult<usize> {
        let datatype = DatatypeMessage::vlen_string_utf8();
        let chunk_dims: Vec<u64> = vec![chunk_size as u64];
        let dims: Vec<u64> = vec![0];
        let max_dims: Vec<u64> = vec![u64::MAX];

        if let Some(ref pl) = pipeline {
            self.create_chunked_dataset_with_pipeline(
                name,
                datatype,
                &dims,
                &max_dims,
                &chunk_dims,
                pl.clone(),
            )
        } else {
            self.create_chunked_dataset(name, datatype, &dims, &max_dims, &chunk_dims)
        }
    }

    /// Append variable-length strings to an existing chunked vlen string dataset.
    ///
    /// Creates a new global heap collection for the strings, builds vlen
    /// references, and appends them as new chunks to the dataset.
    pub fn append_vlen_strings(&self, ds_index: usize, strings: &[&str]) -> IoResult<()> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::datatype::DatatypeMessage;

        if strings.is_empty() {
            return Ok(());
        }

        // Whole-operation guard: buffer take, frame writes, re-buffer and
        // extend below are separate slot acquisitions that a concurrent
        // same-dataset append must not interleave with.
        let cell = self.ds(ds_index);
        let _op = cell.op.lock();

        // The elements about to be written are vlen references; any other
        // element type would be overwritten with them as raw bytes.
        let charset = {
            let ds = self.ds(ds_index);
            let m = ds.lock();
            match m.datatype {
                DatatypeMessage::VarLenString { charset, .. } => charset,
                _ => {
                    return Err(crate::io::IoError::InvalidState(
                        "append_vlen_strings is only for variable-length string datasets".into(),
                    ))
                }
            }
        };
        ensure_vlen_charset(charset, strings)?;

        // Every deterministic rejection must precede the heap write below:
        // a collection written for a batch the append then refuses (a
        // contiguous dataset, or a reopened dataset whose chunk index was
        // not reconstructed) is a 4096-byte orphan nothing references.
        let chunk_dims = self
            .dataset_chunk_dims(ds_index)
            .ok_or_else(|| crate::io::IoError::InvalidState("not a chunked dataset".into()))?
            .to_vec();
        let dims = self.dataset_dims(ds_index).to_vec();

        // Store the batch's strings as heap objects; a batch that fits an
        // earlier collection's free space shares its block.
        let items: Vec<&[u8]> = strings.iter().map(|s| s.as_bytes()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        // Build raw vlen reference bytes
        let ref_size = crate::format::global_heap::vlen_reference_size(&self.ctx);
        let mut raw = Vec::with_capacity(strings.len() * ref_size);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            let seq_len = crate::format::global_heap::vlen_seq_len(strings[i].len())?;
            raw.extend_from_slice(&encode_vlen_reference(
                seq_len,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }

        let n_new_frames = strings.len();
        let current_dim0 = dims[0] as usize;
        let chunk_dim0 = chunk_dims[0] as usize;
        let frame_bytes = ref_size;

        // Merge the buffer with the new frames when it is the dataset's tail;
        // a buffer left mid-extent (the extent moved past it) keeps its
        // recorded place — flush it and start fresh at the current end.
        let taken = { self.ds(ds_index).lock().append.take() };
        let (base_dim0, buffered_frames, mut combined) = match taken {
            Some(b) if b.base + b.frames == current_dim0 as u64 => {
                (b.base as usize, b.frames as usize, b.bytes)
            }
            Some(b) => {
                self.write_append_frames(ds_index, b.base, b.frames, &b.bytes)?;
                (current_dim0, 0, Vec::new())
            }
            None => (current_dim0, 0, Vec::new()),
        };
        combined.extend_from_slice(&raw);

        let total_frames = buffered_frames + n_new_frames;

        // Rows up to the last chunk boundary are written now; the tail that
        // does not complete a chunk goes back in the buffer for the next
        // append (or the flush at close). The boundary can precede
        // `base_dim0` — a reopened file's flushed partial chunk leaves the
        // base mid-chunk — in which case everything is tail.
        let last_boundary = ((base_dim0 + total_frames) / chunk_dim0) * chunk_dim0;
        let write_frames = last_boundary.saturating_sub(base_dim0);
        let tail_frames = total_frames - write_frames;
        if write_frames > 0 {
            self.write_append_frames(
                ds_index,
                base_dim0 as u64,
                write_frames as u64,
                &combined[..write_frames * frame_bytes],
            )?;
        }
        if tail_frames > 0 {
            let ds = self.ds(ds_index);
            let mut m = ds.lock();
            m.append = Some(AppendBuffer {
                base: (base_dim0 + write_frames) as u64,
                frames: tail_frames as u64,
                bytes: combined[write_frames * frame_bytes..].to_vec(),
            });
        }

        // Extend dims
        let logical_dim0 = base_dim0 + total_frames;
        let mut new_dims = dims;
        new_dims[0] = logical_dim0 as u64;
        self.extend_dataset_inner(ds_index, &new_dims)?;

        Ok(())
    }

    /// Replace elements `start .. start + strings.len()` of a 1-D
    /// variable-length string dataset, leaving its extent and every other
    /// element alone.
    ///
    /// The replacements go into the global heap and only the vlen
    /// references of the named elements are rewritten, so the cost is the
    /// new strings plus the chunks those references live in — not the column.
    /// The objects the old references pointed at are freed *before* the
    /// replacement is allocated, so repeated updates reuse space instead of
    /// growing the file — including across close/reopen cycles, where the
    /// in-memory free list starts empty and only this free-first order lets
    /// the session reuse the block it just released. This is what libhdf5
    /// does: `H5T__vlen_disk_write` deletes the reference it read into the
    /// conversion background buffer before storing the new one.
    ///
    /// Elements the append buffer still holds are flushed to their chunks
    /// first, so the whole range is on disk and one write path covers it.
    pub fn write_vlen_strings_slice(
        &self,
        ds_index: usize,
        start: u64,
        strings: &[&str],
    ) -> IoResult<()> {
        use crate::format::global_heap::{encode_vlen_reference, vlen_reference_size};
        use crate::format::messages::datatype::DatatypeMessage;

        // An empty batch is a no-op: nothing to replace, nothing to free.
        if strings.is_empty() {
            return Ok(());
        }

        // Whole-operation guard: the flush, the old-reference reads and the
        // slice write below must not interleave with a concurrent
        // same-dataset operation.
        let cell = self.ds(ds_index);
        let _op = cell.op.lock();

        // Snapshot what the write needs, then drop the guard: `write_slice`
        // below re-locks the same slot.
        let (charset, dims, writable) = {
            let ds = self.ds(ds_index);
            let m = ds.lock();
            let charset = match m.datatype {
                DatatypeMessage::VarLenString { charset, .. } => charset,
                _ => {
                    return Err(crate::io::IoError::InvalidState(
                        "write_vlen_strings_slice is only for variable-length string datasets"
                            .into(),
                    ))
                }
            };
            let writable = if m.is_chunked() {
                Ok(())
            } else {
                match m.contiguous_target() {
                    Some(ContiguousTarget::Virtual) => Err(virtual_write_refused()),
                    Some(_) => Ok(()),
                    None => Err(crate::io::IoError::InvalidState(
                        "dataset has no data allocated".into(),
                    )),
                }
            };
            (charset, m.dataspace.dims.clone(), writable)
        };

        // `write_slice_inner` rejects a dataset with neither chunk machinery
        // nor allocated data (a reopened dataset whose index was not
        // reconstructed), and refuses a virtual one outright — those
        // rejections must come before the heap write below, or every failed
        // call orphans a 4096-byte collection.
        writable?;

        if dims.len() != 1 {
            return Err(crate::io::IoError::InvalidState(format!(
                "write_vlen_strings_slice is only for 1-dimension datasets, this one has {}",
                dims.len()
            )));
        }
        let end = start + strings.len() as u64;
        if end > dims[0] {
            return Err(crate::io::IoError::InvalidState(format!(
                "elements {start}..{end} are outside the dataset's {} elements",
                dims[0]
            )));
        }
        ensure_vlen_charset(charset, strings)?;

        let ref_size = vlen_reference_size(&self.ctx);

        // Elements the append buffer holds are not in the chunks yet: hand
        // them to the chunks first so the whole range is on disk and the one
        // write path below covers it.
        self.flush_append_buffer_if_intersecting(ds_index, start, end)?;

        // The on-disk references about to be overwritten, read before anything
        // moves. libhdf5 reads the same bytes into the conversion background
        // buffer (`H5D__scatgath_write` gathers the file's current elements
        // when `need_bkg` is set) and hands them to `H5T__vlen_disk_write`,
        // which deletes them before storing the new reference.
        let superseded = self.current_element_bytes(ds_index, start, end - start, ref_size)?;

        // Free the superseded objects *before* allocating the replacement,
        // the order `H5T__vlen_disk_write` uses. The freed block satisfies
        // the allocation below within this same session, so a reopen-and-
        // replace loop keeps the file flat — no persisted free-space
        // information exists to carry it across sessions (issue #10). The
        // cost, shared with libhdf5: a failure between here and the ref
        // write below leaves the dataset's old references dangling.
        self.release_vlen_references(&superseded)?;

        // The insert comes after the release above so the space the release
        // recovered — a freed block, or in-collection bytes the release just
        // listed in `cwfs` — can satisfy this batch.
        let items: Vec<&[u8]> = strings.iter().map(|s| s.as_bytes()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        let mut refs = Vec::with_capacity(strings.len() * ref_size);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            refs.extend_from_slice(&encode_vlen_reference(
                crate::format::global_heap::vlen_seq_len(strings[i].len())?,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }

        self.write_slice_inner(ds_index, &[start], &[strings.len() as u64], &refs)?;

        Ok(())
    }

    /// The bytes elements `start .. start + count` of a 1-D dataset currently
    /// hold, whichever layout stores them.
    ///
    /// Elements no write has reached yet read as zeros — for a vlen dataset
    /// that is the nil reference, which names no heap object.
    fn current_element_bytes(
        &self,
        ds_index: usize,
        start: u64,
        count: u64,
        element_size: usize,
    ) -> IoResult<Vec<u8>> {
        let mut out = vec![0u8; count as usize * element_size];
        if count == 0 {
            return Ok(out);
        }

        let (is_chunked, data_addr) = {
            let ds = self.ds(ds_index);
            let m = ds.lock();
            (m.is_chunked(), m.data_addr)
        };

        if !is_chunked {
            if data_addr != UNDEF_ADDR {
                // `read_at_most`, not `read_at`: a contiguous dataset's block is
                // reserved when it is created, so the file can still be shorter
                // than the block until something writes it. What is missing has
                // never been written, which is the zeros above.
                let at = data_addr + start * element_size as u64;
                let got = self.handle.read_at_most(at, out.len())?;
                out[..got.len()].copy_from_slice(&got);
            }
            return Ok(out);
        }

        let geo = self.chunk_geometry(ds_index)?;
        let per_chunk = geo.chunk_dims[0];
        // Only a corrupt or crafted file declares a zero-length chunk
        // dimension; the divisions below must reject it the way
        // `write_slice` does, not panic.
        if per_chunk == 0 {
            return Err(crate::io::IoError::InvalidState(
                "chunk shape has a zero-length dimension".into(),
            ));
        }
        let end = start + count;
        for c in (start / per_chunk)..=((end - 1) / per_chunk) {
            let origin = c * per_chunk;
            let lo = start.max(origin);
            let hi = end.min(origin + per_chunk);
            // A chunk with no block yet leaves this span as the zeros above.
            let Some(chunk) = self.read_chunk_at_coords(ds_index, &[c])? else {
                continue;
            };
            let src = ((lo - origin) as usize) * element_size;
            let dst = ((lo - start) as usize) * element_size;
            let len = ((hi - lo) as usize) * element_size;
            if src + len > chunk.len() {
                return Err(crate::io::IoError::InvalidState(format!(
                    "chunk {c} is {} bytes, too short for elements {lo}..{hi}",
                    chunk.len()
                )));
            }
            out[dst..dst + len].copy_from_slice(&chunk[src..src + len]);
        }
        Ok(out)
    }

    /// Free the global heap objects `refs` names, so replacing a vlen element
    /// does not strand what it used to point at.
    ///
    /// Callers pass refs only for *top-level* vlen datatypes (the
    /// `collect_refs` / `is_vlen` decisions at the prune, delete and
    /// attribute-release sites all match `VarLenString`/`VarLenSequence`).
    /// A compound datatype with vlen members — writable only by a foreign
    /// library, never by this crate — keeps its members' heap objects when
    /// its storage is pruned, deleted or replaced.
    ///
    /// This is libhdf5's `H5HG_remove` reached through `H5T__vlen_disk_delete`:
    /// the object leaves its collection, the collection is rewritten at its
    /// existing size with the recovered bytes given to the free-space marker,
    /// and a collection that ends up empty returns its block to the allocator.
    /// A rewritten collection's recovered space is listed in `cwfs` for
    /// [`insert_vlen_objects`](Self::insert_vlen_objects) to pack into; a
    /// freed block leaves the list.
    /// A nil reference (address 0 or `UNDEF_ADDR`) names no object. The
    /// address decides, not the sequence length: this crate's writers store
    /// even the empty string as a real heap object, so a zero-length reference
    /// with a defined address still holds one that must be released. libhdf5
    /// diverges here against itself — `H5T__vlen_disk_delete` returns before
    /// `H5HG_remove` when the sequence length is zero, yet its write path
    /// (`H5VL__native_blob_put`) inserts a heap object even for an empty
    /// sequence, stranding it forever. The address rule frees those objects.
    ///
    /// Heap objects carry no reference count on this path, matching libhdf5:
    /// its vlen code never calls `H5HG_link` (only the virtual-dataset layer
    /// does). Releasing the same reference twice is absorbed by the
    /// missing-index check below, but a crafted file in which two elements
    /// share one heap object would lose it for the survivor when either is
    /// replaced — the same exposure the file has under libhdf5. This crate's
    /// writers never share: each element write inserts its own object.
    ///
    /// Under SWMR nothing is freed and no collection is rewritten: a reader may
    /// be following those references, the same reason `place_chunk` keeps a
    /// relocated chunk's old block.
    fn release_vlen_references(&self, refs: &[u8]) -> IoResult<()> {
        use crate::format::global_heap::{decode_vlen_reference, vlen_reference_size};

        let ref_size = vlen_reference_size(&self.ctx);
        if ref_size == 0 || refs.len() < ref_size {
            return Ok(());
        }

        // Group by collection so one holding several replaced objects is read,
        // rewritten and judged empty exactly once.
        let mut per_collection: std::collections::BTreeMap<u64, Vec<u16>> = Default::default();
        for r in refs.chunks_exact(ref_size) {
            let (_seq_len, addr, obj_idx) = decode_vlen_reference(r, &self.ctx)?;
            if addr == 0 || addr == UNDEF_ADDR {
                continue;
            }
            let Ok(idx) = u16::try_from(obj_idx) else {
                return Err(crate::io::IoError::InvalidState(format!(
                    "global heap object index {obj_idx} does not fit the 16-bit on-disk field"
                )));
            };
            per_collection.entry(addr).or_default().push(idx);
        }
        self.remove_heap_objects(per_collection)
    }

    /// Remove global heap objects — `H5HG_remove` — given the object indices
    /// grouped by the collection they live in.
    ///
    /// The single owner of heap-object removal: the vlen release path above
    /// reaches it with the objects a replaced element used to name, and
    /// [`release_dataset_storage`](Self::release_dataset_storage) with the
    /// one mapping-list object a deleted virtual dataset owned, which is what
    /// `H5D__virtual_delete` frees the same way.
    fn remove_heap_objects(
        &self,
        per_collection: std::collections::BTreeMap<u64, Vec<u16>>,
    ) -> IoResult<()> {
        use crate::format::global_heap::GlobalHeapCollection;

        if self.swmr_active {
            return Ok(());
        }

        // An object on its way out can hold no stamp: a reference this
        // session wrote into it would otherwise be stamped into whatever a
        // later insert puts at the same index. Pruned here, by the one owner
        // of removal, so no release path — attribute replacement, element
        // rewrite, dataset deletion — can leave one behind.
        self.pending_heap_references.lock().retain(|p| {
            !per_collection
                .get(&p.collection)
                .is_some_and(|indices| indices.contains(&p.index))
        });

        // The `cwfs` lock is held across the sweep: it serializes these
        // collection-block rewrites (and frees) against
        // `insert_vlen_objects`, which may be packing new objects into the
        // same blocks.
        let objhdr = GlobalHeapCollection::object_disk_size(&self.ctx, 0);
        let mut cwfs = self.cwfs.lock();
        for (addr, indices) in per_collection {
            // A collection is at least 4096 bytes (H5HG_MINALLOC) and most are
            // exactly that, so one read usually covers the whole image; only
            // an oversized collection needs a second read at its declared size.
            let mut image = self.handle.read_at_most(addr, 4096)?;
            let declared = GlobalHeapCollection::decode_size(&image, &self.ctx)?;
            if declared > image.len() {
                image = self.handle.read_at(addr, declared)?;
            }
            let (mut gcol, _) = GlobalHeapCollection::decode(&image[..declared], &self.ctx)?;
            let mut removed_any = false;
            for idx in indices {
                removed_any |= gcol.remove_object(idx);
            }
            // Every index already gone (a stale or duplicate reference):
            // leave the image alone. Rewriting is not just wasted I/O — a
            // 100%-full collection written by libhdf5 has no free-space
            // marker, so re-encoding it at its declared size cannot fit one
            // and the whole element update would fail.
            if !removed_any {
                continue;
            }
            if gcol.is_empty() {
                self.allocator
                    .free(addr, declared as u64, FreeSpaceClass::RawData);
                // The block is gone; a lingering entry would let an insert
                // pack into space the allocator can hand to anything.
                cwfs.retain(|e| e.addr != addr);
            } else {
                let rewritten = gcol.encode_at_size(&self.ctx, declared)?;
                self.handle.write_at(addr, &rewritten)?;
                // The recovered bytes are packable now — list them, the way
                // libhdf5's `H5HG_remove` adds the heap to `cwfs`.
                if let Some(free) = gcol.free_space_at(&self.ctx, declared) {
                    if free >= 2 * objhdr {
                        cwfs_note(&mut cwfs, addr, declared, free);
                    }
                }
            }
        }
        Ok(())
    }

    /// Add an attribute to a dataset.
    ///
    /// The attribute will be written as a message in the dataset's object
    /// header when the file is finalized.
    pub fn add_dataset_attribute(&self, ds_index: usize, attr: AttributeMessage) -> IoResult<()> {
        self.set_attribute(AttrTarget::Dataset(ds_index), attr)
    }

    /// Build a variable-length UTF-8 string attribute message.
    ///
    /// The string is stored as one object in a global heap collection and the
    /// returned [`AttributeMessage`] carries the vlen reference as its data,
    /// with a vlen-string datatype and scalar dataspace. h5py reads the value
    /// back as a Python `str` (not `bytes`).
    ///
    /// This is the single owner of vlen-string-attribute construction: every
    /// public string-attribute setter (dataset, group, root, and the SWMR
    /// equivalents) routes through it, so a `VarLenUnicode` /
    /// `set_attr_string` value is always stored as a true variable-length
    /// string rather than the fixed-length string it used to be.
    ///
    /// The string's heap object is placed by
    /// [`insert_vlen_objects`](Self::insert_vlen_objects), so consecutive
    /// attributes pack into a shared collection instead of each paying the
    /// 4096-byte `H5HG_MINALLOC` minimum for a block that holds one string.
    fn vlen_string_attribute(&self, name: &str, value: &str) -> IoResult<AttributeMessage> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::dataspace::DataspaceMessage;
        use crate::format::messages::datatype::DatatypeMessage;

        let (gcol_addr, obj_idx) = self.insert_vlen_objects(&[value.as_bytes()])?[0];
        let seq_len = crate::format::global_heap::vlen_seq_len(value.len())?;
        let data = encode_vlen_reference(seq_len, gcol_addr, obj_idx as u32, &self.ctx);
        Ok(AttributeMessage {
            name: name.to_string(),
            datatype: DatatypeMessage::vlen_string_utf8(),
            dataspace: DataspaceMessage::scalar(),
            data,
        })
    }

    /// Build a variable-length UTF-8 string **array** attribute message.
    ///
    /// The N-dimensional counterpart of
    /// [`vlen_string_attribute`](Self::vlen_string_attribute): every element
    /// string is stored as one object in a single global heap collection, and
    /// the attribute data is the row-major concatenation of one vlen reference
    /// per element. The datatype is the same vlen-string datatype; the dataspace
    /// is the simple dataspace described by `shape` (an empty `shape` is a
    /// scalar). h5py reads the value back as a numpy array of Python `str` with
    /// that shape.
    ///
    /// The caller owns the invariant that `values.len()` equals the product of
    /// `shape` (the public setters validate it before calling). The element
    /// objects are placed by
    /// [`insert_vlen_objects`](Self::insert_vlen_objects) — a zero-element
    /// array allocates nothing, and each reference carries its element's
    /// own collection address.
    fn vlen_string_array_attribute(
        &self,
        name: &str,
        values: &[&str],
        shape: &[u64],
    ) -> IoResult<AttributeMessage> {
        use crate::format::global_heap::encode_vlen_reference;
        use crate::format::messages::dataspace::DataspaceMessage;
        use crate::format::messages::datatype::DatatypeMessage;

        debug_assert_eq!(
            values.len() as u64,
            shape.iter().product::<u64>(),
            "vlen_string_array_attribute values.len() must equal product(shape)"
        );

        let items: Vec<&[u8]> = values.iter().map(|v| v.as_bytes()).collect();
        let placements = self.insert_vlen_objects(&items)?;

        let mut data = Vec::with_capacity(values.len() * 16);
        for (i, &(gcol_addr, obj_idx)) in placements.iter().enumerate() {
            data.extend_from_slice(&encode_vlen_reference(
                crate::format::global_heap::vlen_seq_len(values[i].len())?,
                gcol_addr,
                obj_idx as u32,
                &self.ctx,
            ));
        }
        Ok(AttributeMessage {
            name: name.to_string(),
            datatype: DatatypeMessage::vlen_string_utf8(),
            dataspace: DataspaceMessage::simple(shape),
            data,
        })
    }

    /// Set a user-defined fill value for a dataset.
    ///
    /// `bytes` must be exactly one element wide (matching the dataset's
    /// datatype). The value is emitted as a `fill_defined = 2` fill-value
    /// message in the dataset object header when the file is finalized.
    ///
    /// IMPORTANT: for a *contiguous* dataset this also immediately writes
    /// the tiled fill value across the whole data block, so it must be
    /// called BEFORE any `write_dataset_raw` / `write_slice` — otherwise the
    /// fill write clobbers data already written. (The high-level builder
    /// always calls this right after creating the dataset.)
    pub fn set_dataset_fill_value(&self, ds_index: usize, bytes: Vec<u8>) -> IoResult<()> {
        let count = self.dataset_count();
        if ds_index >= count {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset index {} out of range",
                ds_index
            )));
        }
        let ds_ref = self.ds(ds_index);
        let mut ds = ds_ref.lock();
        let es = ds.datatype.element_size() as usize;
        if bytes.len() != es {
            return Err(crate::io::IoError::InvalidState(format!(
                "fill value is {} bytes but dataset element size is {}",
                bytes.len(),
                es
            )));
        }
        // For a dataset with no per-chunk fill path the fill-value message
        // only declares fill-on-allocation — tile the fill value across the
        // storage itself now, so unwritten elements read back as the fill
        // value. Which storage that is depends on the layout: a compact
        // dataset's is the image inside its layout message, a contiguous
        // one's is its data block. (The high-level builder calls this
        // immediately after create, before any data is written; a subsequent
        // write_raw/write_slice overwrites its region.)
        // An implicitly indexed dataset is filled here too, and for the same
        // reason: that index has no per-chunk fill path because it has no
        // per-chunk anything — its whole chunk grid is one run of space,
        // allocated and filled at create like a contiguous block. So the test
        // is not "is it chunked" but "does something else fill its chunks".
        let fills_per_chunk = ds
            .chunk_index_kind()
            .is_some_and(|k| k != ChunkIndexKind::Implicit);
        // `H5D_FILL_TIME_NEVER` means exactly this: the library never writes
        // the fill value into allocated storage. Call `set_dataset_fill_time`
        // before this method to have it observed here — the storage this
        // would otherwise tile keeps whatever zero bytes its allocation
        // already gave it.
        if !fills_per_chunk && ds.fill_time != FILL_TIME_NEVER {
            if let Some(len) = ds.compact.as_ref().map(Vec::len) {
                ds.compact = Some(crate::format::messages::fill_value::tiled_fill(
                    len,
                    Some(&bytes),
                ));
            } else {
                // An implicit index's chunk grid is filled as one run, the
                // same way a contiguous block is, and storage this file did
                // not allocate is not filled at all; `allocated_storage_run`
                // is where both of those are decided.
                let run = ds.allocated_storage_run();
                if let Some((target, data_size)) = run.filter(|&(_, size)| size > 0) {
                    let filled = crate::format::messages::fill_value::tiled_fill(
                        data_size as usize,
                        Some(&bytes),
                    );
                    self.write_contiguous_bytes(&target, 0, &filled)?;
                }
            }
        }

        ds.fill_value = Some(bytes);
        ds.header_dirty = true;
        Ok(())
    }

    /// Set when the fill value is written into allocated storage —
    /// `H5Pset_fill_time`. `time` is one of [`FILL_TIME_ALLOC`],
    /// [`FILL_TIME_NEVER`], [`FILL_TIME_IFSET`]; anything else is rejected
    /// the way `H5Pset_fill_time` rejects an out-of-range `H5D_fill_time_t`.
    ///
    /// Call this before [`set_dataset_fill_value`](Self::set_dataset_fill_value)
    /// so that a `FILL_TIME_NEVER` policy is in place before that call
    /// decides whether to eager-tile the value into storage. (The
    /// high-level builder always calls it first.)
    pub fn set_dataset_fill_time(&self, ds_index: usize, time: u8) -> IoResult<()> {
        if !matches!(time, FILL_TIME_ALLOC | FILL_TIME_NEVER | FILL_TIME_IFSET) {
            return Err(crate::io::IoError::InvalidState(format!(
                "invalid fill time {time}; must be {FILL_TIME_ALLOC} (alloc), \
                 {FILL_TIME_NEVER} (never) or {FILL_TIME_IFSET} (if-set)"
            )));
        }
        let count = self.dataset_count();
        if ds_index >= count {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset index {} out of range",
                ds_index
            )));
        }
        let ds_ref = self.ds(ds_index);
        let mut ds = ds_ref.lock();
        ds.fill_time = time;
        ds.header_dirty = true;
        Ok(())
    }

    /// Allocate a `chunk_bytes`-sized buffer pre-filled with dataset
    /// `ds_index`'s fill value (tiled one element wide), or zeros when no
    /// user-defined fill value exists.
    ///
    /// Every partial chunk the writer emits must be built on top of a
    /// buffer from this method, so that the unwritten element region of an
    /// allocated chunk reads back as the fill value rather than zero.
    ///
    /// Unconditional: a shrink's straddler refill
    /// (`refill_chunk_beyond_extent`) calls this to repair data about to
    /// become reachable again, which libhdf5's `H5D__chunk_prune_fill` does
    /// regardless of the fill-time policy. [`new_write_chunk_buffer`](Self::new_write_chunk_buffer)
    /// is the gated counterpart for a chunk touched for the first time
    /// during a write, where the policy does apply.
    pub(crate) fn new_chunk_buffer(&self, ds_index: usize, chunk_bytes: usize) -> Vec<u8> {
        let ds = self.ds(ds_index);
        let m = ds.lock();
        let fv = m.fill_value.as_deref();
        crate::format::messages::fill_value::tiled_fill(chunk_bytes, fv)
    }

    /// The buffer a chunk gets the first time a write touches it — this
    /// dataset's allocation-time fill gate. `H5D__chunk_lock`'s cache-miss
    /// path (H5Dchunk.c:4894) fills such a buffer only for `ALLOC`, or for
    /// `IFSET` with a fill value defined; `NEVER` leaves it as the zeros a
    /// fresh buffer already has. Everything else about the buffer is
    /// [`new_chunk_buffer`](Self::new_chunk_buffer)'s.
    fn new_write_chunk_buffer(&self, ds_index: usize, chunk_bytes: usize) -> Vec<u8> {
        let never = {
            let ds = self.ds(ds_index);
            let m = ds.lock();
            m.fill_time == FILL_TIME_NEVER
        };
        if never {
            vec![0u8; chunk_bytes]
        } else {
            self.new_chunk_buffer(ds_index, chunk_bytes)
        }
    }

    /// Write `n_frames` whole frames whose first row is `base_frame`, for
    /// whichever chunk index the dataset uses and whatever its chunk shape.
    ///
    /// The single owner of an append's chunk writes. The frames are one
    /// hyperslab — rows `base_frame .. base_frame + n_frames` over the full
    /// row shape — so the write goes through
    /// [`write_slice_chunked`](Self::write_slice_chunked), the same engine
    /// `write_slice` uses: a chunk the span covers completely is written
    /// straight through, a partial one is read-modify-write on top of what
    /// is stored (or the fill value), and a chunk row narrower or wider
    /// than the frame row is scattered at the chunk stride. The previous
    /// owner required the extensible-array index and packed rows at the
    /// frame stride, so appends to a fixed-array or v2 B-tree dataset
    /// failed at close and lost the buffered rows.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_append_frames(
        &self,
        ds_index: usize,
        base_frame: u64,
        n_frames: u64,
        frames: &[u8],
    ) -> IoResult<()> {
        if n_frames == 0 {
            return Ok(());
        }
        let geo = self.chunk_geometry(ds_index)?;
        let mut starts = vec![0u64; geo.dims.len()];
        starts[0] = base_frame;
        let mut counts = geo.dims.clone();
        counts[0] = n_frames;
        let expected = counts.iter().product::<u64>() * geo.element_size;
        if frames.len() as u64 != expected {
            return Err(crate::io::IoError::InvalidState(format!(
                "{n_frames} frames at rows {base_frame}.. need {expected} bytes, got {}",
                frames.len()
            )));
        }
        self.write_slice_chunked(ds_index, &starts, &counts, frames)
    }

    /// Write the dataset's append buffer (if any) into its chunks and clear
    /// it. The single owner of the buffer-to-chunks transition: the flush at
    /// close, an append meeting a non-contiguous buffer, and any operation
    /// about to write rows the buffer holds all come through here.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively —
    /// the take and the frame writes are separate acquisitions.
    pub(crate) fn flush_append_buffer(&self, ds_index: usize) -> IoResult<()> {
        let taken = { self.ds(ds_index).lock().append.take() };
        match taken {
            Some(b) => self.write_append_frames(ds_index, b.base, b.frames, &b.bytes),
            None => Ok(()),
        }
    }

    /// Flush the append buffer when rows `start_row .. end_row` intersect
    /// the buffered range — those rows' current content is the buffer, and
    /// writing them on disk while the buffer still holds them would be
    /// undone by the flush at close.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn flush_append_buffer_if_intersecting(
        &self,
        ds_index: usize,
        start_row: u64,
        end_row: u64,
    ) -> IoResult<()> {
        let intersects = {
            let ds = self.ds(ds_index);
            let m = ds.lock();
            m.append
                .as_ref()
                .is_some_and(|b| start_row < b.base + b.frames && end_row > b.base)
        };
        if intersects {
            self.flush_append_buffer(ds_index)
        } else {
            Ok(())
        }
    }

    /// Read an already-written chunk's *decompressed* bytes when the chunk
    /// is allocated and resolvable from the in-memory extensible-array
    /// index. Handles index-block and data-block chunks, filtered and
    /// unfiltered.
    ///
    /// Returns `Ok(None)` only when the chunk has never been written
    /// (address `UNDEF`) or the index genuinely does not reach it, which for
    /// a read-modify-write means the chunk's content is the fill value.
    pub(crate) fn read_chunk_if_present(
        &self,
        ds_index: usize,
        chunk_idx: u64,
    ) -> IoResult<Option<Vec<u8>>> {
        // Phase 1: resolve the chunk's location from the in-memory index.
        // Hold the slot guard through Phase 1: `chunked` borrows it, while the
        // `self.handle`/`self.ctx` reads below touch disjoint fields.
        let ds = self.ds(ds_index);
        let m = ds.lock();
        let element_size = m.datatype.element_size() as u64;
        let pipeline = m.filter_pipeline.clone();
        let Some(chunked) = m.chunked.as_ref() else {
            return Ok(None);
        };
        let chunk_bytes = chunked.chunk_dims.iter().product::<u64>() * element_size;
        let max_nelmts_bits = chunked.earray_params.max_nelmts_bits;
        let chunk_size_len = chunked.chunk_size_len;
        let is_filtered = chunked.filt_iblk.is_some();

        // The chunk entry is either read straight from an index block, or
        // located via a data block that must itself be read from disk.
        enum Loc {
            Direct(u64, u64, u32),
            DataBlock {
                dblk_addr: u64,
                offset: usize,
                nelmts: usize,
            },
        }

        // Resolve the chunk's location with the libhdf5-compatible EA
        // geometry (super-block-grouped data blocks), matching `record_ea_chunk`.
        let ea_loc = {
            let p = &chunked.earray_params;
            EaGeometry::new(
                p.idx_blk_elmts,
                p.data_blk_min_elmts,
                p.sup_blk_min_data_ptrs,
                p.max_nelmts_bits,
                p.max_dblk_page_nelmts_bits,
            )?
            .locate(chunk_idx)?
        };
        let loc = match ea_loc {
            EaLoc::Index { elem } => {
                if is_filtered {
                    let e = &chunked.filt_iblk.as_ref().unwrap().elements[elem];
                    Loc::Direct(e.addr, e.nbytes, e.filter_mask)
                } else {
                    Loc::Direct(chunked.ea_iblk.elements[elem], chunk_bytes, 0)
                }
            }
            EaLoc::Dblk(l) => {
                if l.paged {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "chunk index {} lives in a paged extensible-array data \
                         block, which is not yet supported for read-modify-write",
                        chunk_idx
                    )));
                }
                let dblk_addr = match l.path {
                    EaDblkPath::Direct { idx } => {
                        if is_filtered {
                            chunked.filt_iblk.as_ref().unwrap().dblk_addrs[idx]
                        } else {
                            chunked.ea_iblk.dblk_addrs[idx]
                        }
                    }
                    EaDblkPath::ViaSblk {
                        sblk_off,
                        local_dblk,
                        ndblks_in_sblk,
                        ..
                    } => {
                        let sblk_addr = if is_filtered {
                            chunked.filt_iblk.as_ref().unwrap().sblk_addrs[sblk_off]
                        } else {
                            chunked.ea_iblk.sblk_addrs[sblk_off]
                        };
                        if sblk_addr == UNDEF_ADDR {
                            return Ok(None);
                        }
                        let sb_buf = self.handle.read_at_most(sblk_addr, 65536)?;
                        let sb = ExtensibleArraySuperBlock::decode(
                            &sb_buf,
                            &self.ctx,
                            max_nelmts_bits,
                            ndblks_in_sblk,
                            0,
                        )?;
                        sb.dblk_addrs[local_dblk]
                    }
                };
                if dblk_addr == UNDEF_ADDR {
                    return Ok(None);
                }
                Loc::DataBlock {
                    dblk_addr,
                    offset: l.offset_in_dblk as usize,
                    nelmts: l.dblk_nelmts as usize,
                }
            }
        };

        // Phase 2: resolve through the data block (if needed) and read. The
        // mask is the chunk's filter mask (0 for unfiltered), so a chunk
        // written via a direct chunk write with a skipped filter is reversed
        // correctly during read-modify-write.
        let (addr, nbytes, mask) = match loc {
            Loc::Direct(a, n, m) => (a, n, m),
            Loc::DataBlock {
                dblk_addr,
                offset,
                nelmts,
            } => {
                let buf = self.handle.read_at_most(dblk_addr, 65536)?;
                if is_filtered {
                    let dblk = FilteredDataBlock::decode(
                        &buf,
                        &self.ctx,
                        max_nelmts_bits,
                        nelmts,
                        chunk_size_len,
                    )?;
                    let e = &dblk.elements[offset];
                    (e.addr, e.nbytes, e.filter_mask)
                } else {
                    let dblk =
                        ExtensibleArrayDataBlock::decode(&buf, &self.ctx, max_nelmts_bits, nelmts)?;
                    (dblk.elements[offset], chunk_bytes, 0)
                }
            }
        };
        self.read_chunk_block(pipeline.as_ref(), addr, nbytes, mask)
    }

    /// Read one stored chunk block and undo its filters.
    ///
    /// `nbytes` is the *stored* length and `mask` the chunk's filter mask, so
    /// a chunk written by a direct chunk write with a skipped filter is
    /// reversed correctly. `Ok(None)` means the chunk has no block yet — the
    /// single place that judgement is made, shared by every chunk index.
    fn read_chunk_block(
        &self,
        pipeline: Option<&FilterPipeline>,
        addr: u64,
        nbytes: u64,
        mask: u32,
    ) -> IoResult<Option<Vec<u8>>> {
        if addr == UNDEF_ADDR || nbytes == 0 {
            return Ok(None);
        }
        let raw = self.handle.read_at(addr, nbytes as usize)?;
        match pipeline {
            Some(pl) => Ok(Some(filter::reverse_filters_masked(pl, &raw, mask)?)),
            None => Ok(Some(raw)),
        }
    }

    /// Read the *decompressed* bytes of the chunk at `chunk_coords`, whichever
    /// chunk index the dataset uses, or `Ok(None)` when that chunk has never
    /// been written.
    ///
    /// This is the read half of a partial-chunk read-modify-write: a hyperslab
    /// write that covers only part of a chunk must start from what is already
    /// there. Keeping one entry point for all three index types is what lets
    /// [`write_slice`](Self::write_slice) stay index-agnostic.
    pub(crate) fn read_chunk_at_coords(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
    ) -> IoResult<Option<Vec<u8>>> {
        let geo = self.chunk_geometry(ds_index)?;
        // Only the linearly-addressed indexes compute a slot; a v2 B-tree is
        // keyed by the coordinates themselves (and may hold unlimited inner
        // dimensions, which have no linear slot).
        match geo.kind {
            ChunkIndexKind::ExtensibleArray => {
                let linear = geo.linear_index(chunk_coords)?;
                self.read_chunk_if_present(ds_index, linear)
            }
            ChunkIndexKind::FixedArray => {
                let linear = geo.linear_index(chunk_coords)?;
                let ds = self.ds(ds_index);
                let m = ds.lock();
                let pipeline = m.filter_pipeline.clone();
                let fa = m.fixed_array.as_ref().unwrap();
                let lidx = linear as usize;
                let (addr, nbytes, mask) = if pipeline.is_some() {
                    match fa.fa_dblk.filtered_elements.get(lidx) {
                        Some(e) => (e.address, e.chunk_size, e.filter_mask),
                        None => return Ok(None),
                    }
                } else {
                    match fa.fa_dblk.elements.get(lidx) {
                        Some(&a) => (a, geo.chunk_bytes(), 0),
                        None => return Ok(None),
                    }
                };
                drop(m);
                self.read_chunk_block(pipeline.as_ref(), addr, nbytes, mask)
            }
            ChunkIndexKind::BtreeV2 => {
                let ds = self.ds(ds_index);
                let m = ds.lock();
                let pipeline = m.filter_pipeline.clone();
                let bt2 = m.btree_v2.as_ref().unwrap();
                // A filtered index records the stored size and mask per chunk;
                // an unfiltered one stores whole chunks, so their size is the
                // chunk shape and no filter ran.
                let found = if bt2.index.filtered {
                    bt2.index
                        .lookup_filtered(chunk_coords)
                        .map(|r| (r.chunk_address, r.chunk_size, r.filter_mask))
                } else {
                    bt2.index
                        .lookup(chunk_coords)
                        .map(|r| (r.chunk_address, geo.chunk_bytes(), 0))
                };
                drop(m);
                match found {
                    Some((addr, nbytes, mask)) => {
                        self.read_chunk_block(pipeline.as_ref(), addr, nbytes, mask)
                    }
                    None => Ok(None),
                }
            }
            // Every chunk of an implicitly indexed dataset exists from the
            // moment the dataset does, so there is no "never written" answer
            // to give: an untouched chunk reads back as the fill value the
            // create wrote there.
            ChunkIndexKind::Implicit => {
                let (grid, offset) = self.implicit_chunk_slot(ds_index, &geo, chunk_coords)?;
                self.read_chunk_block(None, grid + offset, geo.chunk_bytes(), 0)
            }
            // A single-chunk dataset's one chunk is never written until its
            // first write (unless the dataset was early-allocated and
            // unfiltered, in which case create already gave it an address) —
            // unlike Implicit, `UNDEF_ADDR` here is a real "never written".
            ChunkIndexKind::SingleChunk => {
                let ds = self.ds(ds_index);
                let m = ds.lock();
                let pipeline = m.filter_pipeline.clone();
                let sc = m.single_chunk.as_ref().unwrap();
                if sc.data_addr == UNDEF_ADDR {
                    return Ok(None);
                }
                let (addr, nbytes, mask) = if pipeline.is_some() {
                    (sc.data_addr, sc.nbytes, sc.filter_mask)
                } else {
                    (sc.data_addr, geo.chunk_bytes(), 0)
                };
                drop(m);
                self.read_chunk_block(pipeline.as_ref(), addr, nbytes, mask)
            }
            ChunkIndexKind::BtreeV1 => {
                let ds = self.ds(ds_index);
                let m = ds.lock();
                let pipeline = m.filter_pipeline.clone();
                let bt1 = m.btree_v1.as_ref().unwrap();
                let found = bt1
                    .position(chunk_coords)
                    .ok()
                    .map(|i| &bt1.records[i])
                    .map(|r| (r.address, r.nbytes as u64, r.filter_mask));
                drop(m);
                match found {
                    Some((addr, nbytes, mask)) => {
                        self.read_chunk_block(pipeline.as_ref(), addr, nbytes, mask)
                    }
                    None => Ok(None),
                }
            }
        }
    }

    /// The slot one chunk of an implicitly indexed dataset occupies: the
    /// address its whole chunk grid starts at, and the chunk's offset within
    /// that grid. `data_addr + linear_index * chunk_bytes` is the whole of
    /// that index (`H5D__none_idx_get_addr`, H5Dnone.c).
    ///
    /// The one place a chunk of such a dataset is placed — read and write both
    /// come through here, so the bounds check below covers both. The grid it
    /// names is [`DatasetInfo::implicit_grid`], which is why the write side
    /// can hand [`ContiguousTarget::Local`] to
    /// [`write_contiguous_bytes`](Self::write_contiguous_bytes) without asking
    /// anything: the external and virtual destinations that owner also knows
    /// about are unreachable from a chunked dataset.
    fn implicit_chunk_slot(
        &self,
        ds_index: usize,
        geo: &ChunkGeometry,
        chunk_coords: &[u64],
    ) -> IoResult<(u64, u64)> {
        let linear = geo.linear_index(chunk_coords)?;
        let ds = self.ds(ds_index);
        let m = ds.lock();
        let (grid, grid_size) = m.implicit_grid().ok_or_else(|| {
            crate::io::IoError::InvalidState("no implicitly indexed chunk grid".into())
        })?;
        let offset = linear.checked_mul(geo.chunk_bytes()).ok_or_else(|| {
            crate::io::IoError::InvalidState("implicit chunk offset overflows u64".into())
        })?;
        if offset + geo.chunk_bytes() > grid_size {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk {chunk_coords:?} lies outside the {grid_size} bytes of chunk space \
                 this implicitly indexed dataset was created with"
            )));
        }
        Ok((grid, offset))
    }

    /// Write one whole chunk addressed by its grid coordinates, whichever
    /// chunk index the dataset uses. `data` is the chunk's unfiltered bytes;
    /// the dataset's filter pipeline (if any) runs here.
    ///
    /// The write half of the pair with
    /// [`read_chunk_at_coords`](Self::read_chunk_at_coords). Unlike the
    /// dataset-level `write_chunk_at`, this never grows the dataspace — a
    /// hyperslab write is bounded by the current extent by definition.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_chunk_at_coords(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let geo = self.chunk_geometry(ds_index)?;
        match geo.kind {
            ChunkIndexKind::ExtensibleArray => {
                let linear = geo.linear_index(chunk_coords)?;
                self.write_chunk_inner(ds_index, linear, data)
            }
            ChunkIndexKind::FixedArray => {
                self.write_chunk_fixed_array_inner(ds_index, chunk_coords, data)
            }
            ChunkIndexKind::BtreeV2 => {
                self.write_chunk_btree_v2_inner(ds_index, chunk_coords, data)
            }
            ChunkIndexKind::Implicit => {
                self.write_chunk_implicit_inner(ds_index, chunk_coords, data)
            }
            ChunkIndexKind::SingleChunk => {
                self.write_chunk_single_chunk_inner(ds_index, chunk_coords, data)
            }
            ChunkIndexKind::BtreeV1 => {
                self.write_chunk_btree_v1_inner(ds_index, chunk_coords, data)
            }
        }
    }

    /// Write one whole chunk to a dataset indexed by a version-1 B-tree.
    ///
    /// `chunk_coords` is the chunk's grid position. `data` is the chunk's
    /// unfiltered bytes; the dataset's filter pipeline runs here if it has
    /// one, and the key records the stored size and mask the way libhdf5's
    /// does (`H5D__btree_new_node`).
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_chunk_btree_v1_inner(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        // Read what the write needs under a brief guard, then filter OUTSIDE
        // the lock, as every other index's write path does.
        let ds = self.ds(ds_index);
        let (chunk_bytes, pipeline) = {
            let m = ds.lock();
            let element_size = m.datatype.element_size() as u64;
            let bt1 = m.btree_v1.as_ref().ok_or_else(|| {
                crate::io::IoError::InvalidState("not a version-1 B-tree dataset".into())
            })?;
            (
                bt1.chunk_dims.iter().product::<u64>() * element_size,
                m.filter_pipeline.clone(),
            )
        };
        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }

        let filtered;
        let stored = match pipeline {
            Some(ref pl) => {
                filtered = filter::apply_filters(pl, data)?;
                &filtered[..]
            }
            None => data,
        };
        self.record_btree_v1_chunk(ds_index, chunk_coords, stored, 0)
    }

    /// Write a pre-filtered chunk verbatim to a version-1 B-tree dataset,
    /// recording the caller-supplied `filter_mask` — the classic-index half
    /// of the HDF5 "direct chunk write" (`H5Dwrite_chunk`).
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_compressed_chunk_btree_v1_inner(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
        data: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        if self.ds(ds_index).lock().filter_pipeline.is_none() {
            return Err(crate::io::IoError::InvalidState(
                "write_chunk_raw requires a filtered dataset (an unfiltered chunk \
                 is stored at its full size, so there is nothing for a stored size \
                 or a filter mask to say)"
                    .into(),
            ));
        }
        self.record_btree_v1_chunk(ds_index, chunk_coords, data, filter_mask)
    }

    /// Place a chunk's already-final bytes in the file and record them in the
    /// version-1 B-tree under the caller-supplied `filter_mask`.
    ///
    /// Shared by the two writes above, so both reach the index through one
    /// placement rule. The records are kept in key order here — the bulk load
    /// at flush walks them in that order and a lookup bisects them.
    fn record_btree_v1_chunk(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
        final_bytes: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        let stored_len = final_bytes.len() as u64;
        // The key's size field is 32 bits wide (`H5D_btree_key_t::nbytes`),
        // which is also libhdf5's limit on a chunk in this index.
        let Ok(nbytes) = u32::try_from(stored_len) else {
            return Err(crate::io::IoError::InvalidState(format!(
                "stored chunk size {stored_len} does not fit in the 32-bit size \
                 field of a version-1 B-tree chunk key"
            )));
        };
        let ds = self.ds(ds_index);
        let mut m = ds.lock();
        let bt1 = m.btree_v1.as_ref().ok_or_else(|| {
            crate::io::IoError::InvalidState("not a version-1 B-tree dataset".into())
        })?;
        if chunk_coords.len() != bt1.chunk_dims.len() {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk_coords has {} entries but the dataset has {} dimensions",
                chunk_coords.len(),
                bt1.chunk_dims.len()
            )));
        }
        // A coordinate past the maximum extent has no chunk to be: unlike the
        // array indexes there is no slot to run out of, so the bound is
        // checked here or not at all. An unlimited dimension has none.
        for (d, ((&c, &cd), &max)) in chunk_coords
            .iter()
            .zip(&bt1.chunk_dims)
            .zip(&bt1.max_dims)
            .enumerate()
        {
            if max != u64::MAX && c.saturating_mul(cd) >= max {
                return Err(crate::io::IoError::InvalidState(format!(
                    "chunk coordinate {c} in dimension {d} is outside the maximum \
                     extent {max}"
                )));
            }
        }
        let slot = bt1.position(chunk_coords);
        let old = slot.ok().map(|i| {
            let r = &bt1.records[i];
            (r.address, r.nbytes as u64)
        });
        // A rewrite whose stored size is unchanged stays where it is (always
        // so when unfiltered), one that no longer fits moves. See `place_chunk`.
        let address = self.place_chunk(old, stored_len);
        self.handle.write_at(address, final_bytes)?;

        let bt1 = m.btree_v1.as_mut().unwrap();
        let record = BtreeV1ChunkRecord {
            scaled: chunk_coords.to_vec(),
            address,
            nbytes,
            filter_mask,
        };
        match slot {
            Ok(i) => bt1.records[i] = record,
            Err(i) => bt1.records.insert(i, record),
        }
        bt1.chunks_written += 1;
        Ok(())
    }

    /// Write one whole chunk of an implicitly indexed dataset into the slot
    /// its coordinates name. There is no index to record anything in — the
    /// slot is where it always was — so this is the write in full.
    ///
    /// The bytes go through [`write_contiguous_bytes`](Self::write_contiguous_bytes),
    /// the one owner of a raw-byte write, against the grid
    /// [`implicit_chunk_slot`](Self::implicit_chunk_slot) names.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_chunk_implicit_inner(
        &self,
        ds_index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let geo = self.chunk_geometry(ds_index)?;
        let chunk_bytes = geo.chunk_bytes();
        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }
        let (grid, offset) = self.implicit_chunk_slot(ds_index, &geo, chunk_coords)?;
        self.write_contiguous_bytes(&ContiguousTarget::Local(grid), offset, data)
    }

    /// Snapshot the geometry needed to address a chunked dataset's grid.
    ///
    /// Taken under one brief slot guard so the callers below — which re-lock
    /// the slot through `write_chunk`/`read_chunk_*` — never hold it across
    /// compression or I/O.
    fn chunk_geometry(&self, ds_index: usize) -> IoResult<ChunkGeometry> {
        let ds = self.ds(ds_index);
        let m = ds.lock();
        let Some(kind) = m.chunk_index_kind() else {
            return Err(crate::io::IoError::InvalidState(
                "not a chunked dataset".into(),
            ));
        };
        let chunk_dims = match kind {
            ChunkIndexKind::ExtensibleArray => m.chunked.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::FixedArray => m.fixed_array.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::BtreeV2 => m.btree_v2.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::Implicit => m.implicit.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::SingleChunk => m.single_chunk.as_ref().unwrap().chunk_dims.clone(),
            ChunkIndexKind::BtreeV1 => m.btree_v1.as_ref().unwrap().chunk_dims.clone(),
        };
        Ok(ChunkGeometry {
            kind,
            dims: m.dataspace.dims.clone(),
            max_dims: m.dataspace.max_dims.clone(),
            chunk_dims,
            element_size: m.datatype.element_size() as u64,
        })
    }

    /// Index-grid slot of the chunk at grid `coords` (see
    /// [`crate::io::chunk_grid`]).
    pub(crate) fn chunk_slot(&self, ds_index: usize, coords: &[u64]) -> IoResult<u64> {
        self.chunk_geometry(ds_index)?.linear_index(coords)
    }

    /// Grid coordinates of the chunk recorded under index-grid slot `linear`
    /// — the inverse of [`Self::chunk_slot`].
    pub(crate) fn chunk_coords_from_slot(
        &self,
        ds_index: usize,
        linear: u64,
    ) -> IoResult<Vec<u64>> {
        let geo = self.chunk_geometry(ds_index)?;
        crate::io::chunk_grid::coords_of(
            &geo.dims,
            geo.max_dims.as_deref(),
            &geo.chunk_dims,
            linear,
        )
    }

    /// Define a chunked dataset indexed by a fixed array, fixed at its
    /// current shape (`max_dims == dims`). `chunk_dims` defines the chunk
    /// shape. Returns the dataset index.
    pub fn create_fixed_array_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        chunk_dims: &[u64],
    ) -> IoResult<usize> {
        self.create_fixed_array_dataset_with_max(name, datatype, dims, dims, chunk_dims, None)
    }

    /// Define a fixed-shape compressed chunked dataset indexed by a
    /// *filtered* Fixed Array (`max_dims == dims`).
    ///
    /// Like `create_fixed_array_dataset`, but the FA header carries the filtered
    /// client id and a `chunk_size_len`-wide compressed-size field per chunk
    /// (`FixedArrayFilteredChunkElement`), and the dataset gets a filter
    /// pipeline. Chunks written via `write_chunk_fixed_array` are compressed and
    /// their compressed size + filter mask are recorded in the data block.
    ///
    /// A convenience over [`create_fixed_array_dataset_with_max`]'s own
    /// pipeline argument; production dataset creation calls that directly,
    /// so this is kept as a direct entry point for this crate's own
    /// white-box tests.
    ///
    /// [`create_fixed_array_dataset_with_max`]: Self::create_fixed_array_dataset_with_max
    #[cfg(all(test, feature = "deflate"))]
    pub fn create_fixed_array_dataset_with_pipeline(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        chunk_dims: &[u64],
        pipeline: FilterPipeline,
    ) -> IoResult<usize> {
        self.create_fixed_array_dataset_with_max(
            name,
            datatype,
            dims,
            dims,
            chunk_dims,
            Some(pipeline),
        )
    }

    /// Define a chunked dataset indexed by a fixed array, growable up to
    /// `max_dims` (every maximum finite — libhdf5 picks this index exactly
    /// when no dimension is unlimited).
    ///
    /// The array is sized for the chunk grid of the *maximum* extent, the
    /// libhdf5 rule (`H5D__farray_idx_create` uses `max_nchunks`), so the
    /// dataset can be extended to `max_dims` without re-indexing chunks.
    pub fn create_fixed_array_dataset_with_max(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
        pipeline: Option<FilterPipeline>,
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, max_dims, chunk_dims)?;
        if max_dims.contains(&u64::MAX) {
            return Err(crate::io::IoError::InvalidState(
                "a fixed-array index requires a fixed maximum shape (no unlimited dimension)"
                    .into(),
            ));
        }
        let mut num_chunks: u64 = 1;
        for g in crate::io::chunk_grid::index_grid(dims, Some(max_dims), chunk_dims)? {
            num_chunks = num_chunks.checked_mul(g).ok_or_else(|| {
                crate::io::IoError::InvalidState("chunk count overflows u64".into())
            })?;
        }

        let chunk_bytes: u64 = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let layout_version = self.chunk_layout_version(pipeline.is_some(), chunk_bytes);

        // Create the FA header. For a filtered FA, chunk_size_len is sized
        // the same way the filtered Extensible Array path computes it:
        // derived from the uncompressed chunk byte count under layout v4,
        // the fixed `sizeof_size` under layout v5.
        let mut fa_header = if pipeline.is_some() {
            let chunk_size_len = self.chunk_size_len_for(layout_version, chunk_bytes);
            FixedArrayHeader::new_for_filtered_chunks(&self.ctx, num_chunks, chunk_size_len)
        } else {
            FixedArrayHeader::new_for_chunks(&self.ctx, num_chunks)
        };
        let hdr_encoded = fa_header.encode(&self.ctx);
        let fa_header_addr = self
            .allocator
            .allocate(hdr_encoded.len() as u64, FreeSpaceClass::Metadata);

        // Create the FA data block. libhdf5 switches to a paged layout once
        // num_elmts exceeds dblk_page_nelmts; both layouts allocate space
        // for `num_chunks` entries up front, but the paged layout also
        // reserves the page-init bitmap and a per-page checksum.
        let fa_dblk = if pipeline.is_some() {
            FixedArrayDataBlock::new_filtered(fa_header_addr, num_chunks as usize)
        } else {
            FixedArrayDataBlock::new_unfiltered(fa_header_addr, num_chunks as usize)
        };
        let dblk_size = fixed_array_dblk_disk_size(&self.ctx, &fa_header);
        let fa_dblk_addr = self.allocator.allocate(dblk_size, FreeSpaceClass::Metadata);

        // Update header with data block address
        fa_header.data_blk_addr = fa_dblk_addr;

        // Write both. The data block content is finalized in `flush_dataset`
        // once all chunk addresses are known; here we just reserve space and
        // write the header so the file is structurally consistent.
        let hdr_encoded = fa_header.encode(&self.ctx);
        self.handle.write_at(fa_header_addr, &hdr_encoded)?;
        let dblk_encoded = encode_fixed_array_dblk(&self.ctx, &fa_header, &fa_dblk);
        debug_assert_eq!(dblk_encoded.len() as u64, dblk_size);
        self.handle.write_at(fa_dblk_addr, &dblk_encoded)?;

        // The maximum is stored even when it equals the dims: it is what
        // `extend_dataset` checks growth against, and the FA capacity above
        // is exactly its chunk grid.
        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: pipeline,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                chunked: None,
                btree_v2: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                fixed_array: Some(FixedArrayDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    fa_header_addr,
                    fa_dblk_addr,
                    fa_header,
                    fa_dblk,
                    chunks_written: 0,
                }),
                append: None,
            },
        );

        Ok(idx)
    }

    /// Define a chunked dataset with the *implicit* index: no index structure
    /// at all, every chunk of the grid allocated at create in one contiguous
    /// run, addressed by arithmetic (`H5Dnone.c`).
    ///
    /// libhdf5 picks this index only where that arithmetic is total, and this
    /// enforces the same three conditions
    /// (`H5D__layout_set_latest_indexing`, H5Dlayout.c): no filter — a
    /// filtered chunk is not `chunk_bytes` long, so the run would not be a
    /// grid; no unlimited dimension — the run has to have a length; and early
    /// allocation, which is what this creator *does* rather than something it
    /// checks. The dataset's fill-value message says so
    /// (`build_dataset_header`), because a file claiming incremental
    /// allocation is one libhdf5 would never have chosen this index for.
    pub fn create_implicit_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        chunk_dims: &[u64],
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, dims, chunk_dims)?;
        let mut num_chunks: u64 = 1;
        for g in crate::io::chunk_grid::index_grid(dims, None, chunk_dims)? {
            num_chunks = num_chunks.checked_mul(g).ok_or_else(|| {
                crate::io::IoError::InvalidState("chunk count overflows u64".into())
            })?;
        }
        let chunk_bytes: u64 = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let data_size = num_chunks.checked_mul(chunk_bytes).ok_or_else(|| {
            crate::io::IoError::InvalidState("implicit chunk storage overflows u64".into())
        })?;
        let layout_version = self.chunk_layout_version(false, chunk_bytes);

        // Early allocation is the whole of this index: the run exists, and
        // holds the fill value, before any chunk is written. It is written
        // out rather than merely reserved because the file's end-of-file
        // address is what libhdf5 checks a file's completeness against — a
        // reserved-but-absent tail is a truncated file to it.
        let data_addr = self.allocator.allocate(data_size, FreeSpaceClass::RawData);
        self.handle.write_at(
            data_addr,
            &crate::format::messages::fill_value::tiled_fill(data_size as usize, None),
        )?;

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                chunked: None,
                btree_v2: None,
                fixed_array: None,
                implicit: Some(ImplicitDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    data_addr,
                    data_size,
                }),
                single_chunk: None,
                btree_v1: None,
                append: None,
            },
        );

        Ok(idx)
    }

    /// Define a chunked dataset indexed by the single-chunk index: a fixed
    /// shape covered by exactly one whole chunk (`chunk_dims == dims`), its
    /// address — and, once written, size and filter mask if filtered — held
    /// directly in the layout message instead of any index structure
    /// (`H5Dsingle.c`). libhdf5 selects this index ahead of both Implicit and
    /// Fixed Array whenever the shape qualifies, filtered or not, early
    /// allocation or not (`H5D__layout_set_latest_indexing`).
    ///
    /// `early_alloc` mirrors [`create_implicit_dataset`](Self::create_implicit_dataset):
    /// when true, the chunk's storage is allocated and filled with the fill
    /// value immediately, matching an early-allocated unfiltered dataset
    /// whose one chunk covers the whole shape. When false, the chunk has no
    /// address until its first write, the same as an unfiltered Fixed Array
    /// element.
    pub fn create_single_chunk_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        chunk_dims: &[u64],
        early_alloc: bool,
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, dims, chunk_dims)?;
        let data_size = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let layout_version = self.chunk_layout_version(false, data_size);

        let data_addr = if early_alloc {
            // Same reasoning as `create_implicit_dataset`: the fill-value
            // bytes are written now, not merely reserved, because the
            // file's end-of-file address is what libhdf5 checks a file's
            // completeness against.
            let addr = self.allocator.allocate(data_size, FreeSpaceClass::RawData);
            self.handle.write_at(
                addr,
                &crate::format::messages::fill_value::tiled_fill(data_size as usize, None),
            )?;
            addr
        } else {
            UNDEF_ADDR
        };

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: None,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                chunked: None,
                btree_v2: None,
                fixed_array: None,
                implicit: None,
                single_chunk: Some(SingleChunkDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    data_addr,
                    data_size,
                    nbytes: if early_alloc { data_size } else { 0 },
                    filter_mask: 0,
                    chunks_written: 0,
                    early_alloc,
                }),
                btree_v1: None,
                append: None,
            },
        );

        Ok(idx)
    }

    /// Define a fixed-shape compressed chunked dataset — of exactly one
    /// whole chunk — indexed by a *filtered* single-chunk index
    /// (`H5O_LAYOUT_CHUNK_SINGLE_INDEX_WITH_FILTER`, H5Dsingle.c). The
    /// chunk's stored size and filter mask are recorded inline in the
    /// layout message once the chunk is written.
    ///
    /// Like [`create_fixed_array_dataset_with_pipeline`](Self::create_fixed_array_dataset_with_pipeline),
    /// there is nothing to allocate ahead of that first write — a filtered
    /// chunk's stored length isn't known until it is compressed — so this
    /// dataset is always incrementally allocated regardless of the caller's
    /// requested allocation time.
    pub fn create_single_chunk_dataset_with_pipeline(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        chunk_dims: &[u64],
        pipeline: FilterPipeline,
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, dims, chunk_dims)?;
        let data_size = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let layout_version = self.chunk_layout_version(true, data_size);

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: Some(pipeline),
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                chunked: None,
                btree_v2: None,
                fixed_array: None,
                implicit: None,
                single_chunk: Some(SingleChunkDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    data_addr: UNDEF_ADDR,
                    data_size,
                    nbytes: 0,
                    filter_mask: 0,
                    chunks_written: 0,
                    early_alloc: false,
                }),
                btree_v1: None,
                append: None,
            },
        );

        Ok(idx)
    }

    /// Define a chunked dataset indexed by a version-1 B-tree — the classic
    /// chunk index, and the only one a version-0/1 superblock file can carry.
    ///
    /// The tree itself is not created here: libhdf5 leaves the layout
    /// message's address undefined until the first chunk is inserted
    /// (`H5D__btree_idx_create` runs on that insert), and so does this — the
    /// flush that bulk-loads the records is what puts a node in the file.
    ///
    /// Unlike the array indexes this one has no grid to size, so it takes any
    /// number of unlimited dimensions: a key *is* the chunk's position, and
    /// the tree is ordered by it.
    pub fn create_btree_v1_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
        pipeline: Option<FilterPipeline>,
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, max_dims, chunk_dims)?;
        let chunk_bytes: u64 = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        if chunk_bytes > u32::MAX as u64 {
            return Err(crate::io::IoError::InvalidState(format!(
                "a {chunk_bytes}-byte chunk does not fit the 32-bit size field of a \
                 version-1 B-tree chunk key"
            )));
        }

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: pipeline,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                // The version-3 data layout message this index encodes as:
                // `H5O_LAYOUT_VERSION_DEFAULT`, which is the floor of
                // `H5D__chunk_set_info`'s final MAX and the whole of it below
                // the version-4 gate — a bound whose row is lower does not
                // push the message down, it only keeps the v1.10 indexes out.
                layout_version: LAYOUT_VERSION_DEFAULT,
                times: self.created_object_times(),
                chunked: None,
                fixed_array: None,
                btree_v2: None,
                implicit: None,
                single_chunk: None,
                btree_v1: Some(BtreeV1DatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    max_dims: max_dims.to_vec(),
                    config: self.btree_v1_config(),
                    records: Vec::new(),
                    node_addrs: Vec::new(),
                    root_addr: UNDEF_ADDR,
                    chunks_written: 0,
                }),
                append: None,
            },
        );

        Ok(idx)
    }

    /// Define a chunked dataset indexed by a B-tree v2 (multiple unlimited dimensions).
    ///
    /// Returns the dataset index.
    pub fn create_btree_v2_dataset(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
    ) -> IoResult<usize> {
        self.create_btree_v2_dataset_inner(name, datatype, dims, max_dims, chunk_dims, None)
    }

    /// Define a *filtered* chunked dataset indexed by a B-tree v2.
    ///
    /// The v2 B-tree counterpart of
    /// [`create_chunked_dataset_with_pipeline`](Self::create_chunked_dataset_with_pipeline):
    /// chunks are compressed on write and the index records each chunk's
    /// stored size and filter mask (record type 11), the same shape libhdf5
    /// builds when a multi-unlimited-dimension dataset has a filter pipeline
    /// (`H5Dbtree2.c`, `H5D_BT2_FILT`).
    pub fn create_btree_v2_dataset_with_pipeline(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
        pipeline: FilterPipeline,
    ) -> IoResult<usize> {
        self.create_btree_v2_dataset_inner(
            name,
            datatype,
            dims,
            max_dims,
            chunk_dims,
            Some(pipeline),
        )
    }

    fn create_btree_v2_dataset_inner(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
        pipeline: Option<FilterPipeline>,
    ) -> IoResult<usize> {
        use crate::format::chunk_index::btree_v2::Bt2Header;

        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, max_dims, chunk_dims)?;
        let ndims = dims.len();
        let chunk_bytes: u64 = chunk_dims.iter().product::<u64>() * datatype.element_size() as u64;
        let layout_version = self.chunk_layout_version(pipeline.is_some(), chunk_bytes);

        // The filtered record's size field is as wide as libhdf5 will
        // recompute it — from the uncompressed chunk size under layout v4,
        // the fixed `sizeof_size` under layout v5 — exactly as the
        // extensible- and fixed-array filtered paths size theirs.
        let bt2_index = match pipeline {
            Some(_) => {
                let len = self.chunk_size_len_for(layout_version, chunk_bytes);
                Bt2ChunkIndex::new_filtered(ndims, len)
            }
            None => Bt2ChunkIndex::new_unfiltered(ndims),
        };

        // The bulk loader spreads a level's records evenly over its nodes, one
        // separator between adjacent siblings, which needs room for a few
        // records per node. HDF5's rank limit of 32 leaves room for seven; a
        // wider rank than that has no valid geometry, so reject it here rather
        // than emit a tree no reader can walk.
        let record_size = bt2_index.record_size(&self.ctx) as usize;
        let node_size = bt2_index.node_size as usize;
        if node_size < 10 + 3 * record_size {
            return Err(crate::io::IoError::InvalidState(format!(
                "a {ndims}-dimension v2 B-tree record is {record_size} bytes, too wide \
                 for a {node_size}-byte node"
            )));
        }

        // Only the header gets a home now: it names an empty tree, whose root
        // is undefined until the first flush bulk-loads the index into nodes.
        let hdr = if bt2_index.filtered {
            Bt2Header::new_for_filtered_chunks(&self.ctx, ndims, bt2_index.chunk_size_len)
        } else {
            Bt2Header::new_for_chunks(&self.ctx, ndims)
        };
        let hdr_encoded = hdr.encode(&self.ctx);
        let bt2_header_addr = self
            .allocator
            .allocate(hdr_encoded.len() as u64, FreeSpaceClass::Metadata);
        self.handle.write_at(bt2_header_addr, &hdr_encoded)?;

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: pipeline,
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                chunked: None,
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: Some(Bt2DatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    bt2_header_addr,
                    node_addrs: Vec::new(),
                    index: bt2_index,
                    chunks_written: 0,
                }),
                append: None,
            },
        );

        Ok(idx)
    }

    /// Create a chunked dataset with a custom filter pipeline.
    pub fn create_chunked_dataset_with_pipeline(
        &self,
        name: &str,
        datatype: DatatypeMessage,
        dims: &[u64],
        max_dims: &[u64],
        chunk_dims: &[u64],
        pipeline: FilterPipeline,
    ) -> IoResult<usize> {
        let create = self.begin_create(name)?;
        let name = create.name.as_str();
        validate_chunk_geometry(dims, max_dims, chunk_dims)?;
        ensure_at_most_one_unlimited(max_dims)?;
        let element_size = datatype.element_size() as u64;
        let chunk_bytes: u64 = chunk_dims.iter().product::<u64>() * element_size;
        let layout_version = self.chunk_layout_version(true, chunk_bytes);
        let chunk_size_len = self.chunk_size_len_for(layout_version, chunk_bytes);

        let earray_params = EarrayParams::default_params();
        let ndblk_addrs = compute_ndblk_addrs(earray_params.sup_blk_min_data_ptrs)?;
        let nsblk_addrs = compute_nsblk_addrs(
            earray_params.idx_blk_elmts,
            earray_params.data_blk_min_elmts,
            earray_params.sup_blk_min_data_ptrs,
            earray_params.max_nelmts_bits,
        )?;

        let mut ea_header =
            ExtensibleArrayHeader::new_for_filtered_chunks(&self.ctx, chunk_size_len);
        ea_header.max_nelmts_bits = earray_params.max_nelmts_bits;
        ea_header.idx_blk_elmts = earray_params.idx_blk_elmts;
        ea_header.data_blk_min_elmts = earray_params.data_blk_min_elmts;
        ea_header.sup_blk_min_data_ptrs = earray_params.sup_blk_min_data_ptrs;
        ea_header.max_dblk_page_nelmts_bits = earray_params.max_dblk_page_nelmts_bits;

        let hdr_encoded = ea_header.encode(&self.ctx);
        let ea_header_addr = self
            .allocator
            .allocate(hdr_encoded.len() as u64, FreeSpaceClass::Metadata);

        let filt_iblk = FilteredIndexBlock::new(
            ea_header_addr,
            earray_params.idx_blk_elmts,
            ndblk_addrs,
            nsblk_addrs,
        );
        let iblk_encoded = filt_iblk.encode(&self.ctx, chunk_size_len);
        let ea_iblk_addr = self
            .allocator
            .allocate(iblk_encoded.len() as u64, FreeSpaceClass::Metadata);

        ea_header.idx_blk_addr = ea_iblk_addr;
        let hdr_encoded = ea_header.encode(&self.ctx);
        self.handle.write_at(ea_header_addr, &hdr_encoded)?;
        self.handle.write_at(ea_iblk_addr, &iblk_encoded)?;

        let dataspace = DataspaceMessage {
            // Chunked storage always requires at least one dimension, so
            // this is never Scalar or Null.
            class: DataspaceClass::Simple,
            dims: dims.to_vec(),
            max_dims: Some(max_dims.to_vec()),
        };
        let ea_iblk = ExtensibleArrayIndexBlock::new(
            ea_header_addr,
            earray_params.idx_blk_elmts,
            ndblk_addrs,
            nsblk_addrs,
        );

        let idx = self.push_dataset(
            &create,
            DatasetInfo {
                name: name.to_string(),
                datatype,
                committed_type: None,
                external: None,
                virtual_storage: None,
                dataspace,
                read_format: None,
                obj_header_addr: 0,
                data_addr: UNDEF_ADDR,
                data_size: 0,
                compact: None,
                attributes: Vec::new(),
                obj_header_written_addr: None,
                obj_header_blocks: Vec::new(),
                filter_pipeline: Some(pipeline),
                deleted: false,
                extent_dirty: false,
                header_dirty: false,
                nlink_written: 1,
                creation_seq: self.take_creation_seq(),
                track_attr_order: self.track_order.attrs,
                fill_value: None,
                fill_time: FILL_TIME_IFSET,
                layout_version,
                times: self.created_object_times(),
                fixed_array: None,
                implicit: None,
                single_chunk: None,
                btree_v1: None,
                btree_v2: None,
                chunked: Some(ChunkedDatasetInfo {
                    chunk_dims: chunk_dims.to_vec(),
                    earray_params,
                    ea_header_addr,
                    ea_iblk_addr,
                    ea_header,
                    ea_iblk,
                    chunks_written: 0,
                    filt_iblk: Some(filt_iblk),
                    chunk_size_len,
                }),
                append: None,
            },
        );
        Ok(idx)
    }

    /// Write a chunk to a fixed-array-indexed dataset.
    ///
    /// `chunk_coords` is the multidimensional chunk index (e.g., [row_chunk, col_chunk]).
    /// The uncompressed `data` must be exactly one chunk wide; the filter
    /// pipeline (if any) runs here before the bytes reach the index.
    pub fn write_chunk_fixed_array(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.write_chunk_fixed_array_inner(index, chunk_coords, data)
    }

    /// [`Self::write_chunk_fixed_array`] body; the caller holds the dataset's
    /// op lock or the writer exclusively.
    pub(crate) fn write_chunk_fixed_array_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        // Read what we need under one brief slot guard, then compress
        // OUTSIDE the lock: `record_fixed_array_chunk` re-locks the same slot,
        // so the guard must be dropped before it (and before apply_filters).
        let ds = self.ds(index);
        let (chunk_bytes, pipeline) = {
            let m = ds.lock();
            let element_size = m.datatype.element_size() as u64;
            let fa = m.fixed_array.as_ref().ok_or_else(|| {
                crate::io::IoError::InvalidState("not a fixed-array dataset".into())
            })?;
            (
                fa.chunk_dims.iter().product::<u64>() * element_size,
                m.filter_pipeline.clone(),
            )
        };

        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }
        let write_data;
        let data_to_write = if let Some(ref pipeline) = pipeline {
            write_data = filter::apply_filters(pipeline, data)?;
            &write_data[..]
        } else {
            data
        };
        // filter_mask = 0: the whole pipeline ran (or the dataset is
        // unfiltered), so no filter is skipped for this chunk.
        self.record_fixed_array_chunk(index, chunk_coords, data_to_write, 0)
    }

    /// Write a pre-filtered chunk verbatim to a fixed-array dataset, recording
    /// the caller-supplied `filter_mask`.
    ///
    /// The bytes are stored exactly as given (no filter pipeline is run); this
    /// is the fixed-array half of the HDF5 "direct chunk write"
    /// (`H5Dwrite_chunk`) operation. `filter_mask` is a bitfield: bit *i* set
    /// means filter *i* of the pipeline was **not** applied to this chunk and
    /// must be skipped on read; pass 0 when the full pipeline was applied
    /// upstream.
    ///
    /// Requires a filtered dataset — only the filtered FA element carries the
    /// size+mask slot.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_compressed_chunk_fixed_array_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        if self.ds(index).lock().filter_pipeline.is_none() {
            return Err(crate::io::IoError::InvalidState(
                "write_compressed_chunk_fixed_array requires a filtered dataset \
                 (no slot for a compressed size or filter mask on an unfiltered \
                 chunk index)"
                    .into(),
            ));
        }
        self.record_fixed_array_chunk(index, chunk_coords, data, filter_mask)
    }

    /// Place an already-final chunk (`final_bytes` is whatever goes to disk —
    /// filtered if the dataset is filtered, raw otherwise) into a fixed-array
    /// dataset's data block, recording the caller-supplied `filter_mask`.
    /// Shared by [`write_chunk_fixed_array`](Self::write_chunk_fixed_array)
    /// and [`write_compressed_chunk_fixed_array`](Self::write_compressed_chunk_fixed_array).
    fn record_fixed_array_chunk(
        &self,
        index: usize,
        chunk_coords: &[u64],
        final_bytes: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        // Hold one slot guard for the whole method; `self.allocator`/`self.handle`/
        // `self.ctx` below touch disjoint fields safe to use with the guard held.
        let ds = self.ds(index);
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        let fa = m
            .fixed_array
            .as_ref()
            .ok_or_else(|| crate::io::IoError::InvalidState("not a fixed-array dataset".into()))?;

        // Linear chunk index in the maximum-extent grid — the slot the fixed
        // array (sized from that grid at create) records the chunk under.
        let linear_idx = crate::io::chunk_grid::linear_index(
            &m.dataspace.dims,
            m.dataspace.max_dims.as_deref(),
            &fa.chunk_dims,
            chunk_coords,
        )?;

        // Update the fixed array data block. The slot is read before the bytes
        // are placed so a rewrite can stay where it is (see `place_chunk`).
        let fa = m.fixed_array.as_mut().unwrap();
        let lidx = linear_idx as usize;
        if is_filtered {
            // Filtered FA: store address + stored size + filter mask. A
            // non-zero mask bit means "filter i was skipped for this chunk".
            let stored_size = final_bytes.len();
            // The stored size is encoded in the FA header's `chunk_size_len`-byte
            // field; libhdf5 errors if it does not fit (H5D_CHUNK_ENCODE_SIZE_CHECK)
            // rather than truncating silently. element_size = sizeof_addr +
            // chunk_size_len + 4 by construction.
            let chunk_size_len = (fa.fa_header.element_size as usize)
                .checked_sub(self.ctx.sizeof_addr as usize + 4)
                .ok_or_else(|| {
                    crate::io::IoError::InvalidState(
                        "filtered fixed-array element size is too small".into(),
                    )
                })?;
            if chunk_size_len < 8 && stored_size >= (1usize << (chunk_size_len * 8)) {
                return Err(crate::io::IoError::InvalidState(format!(
                    "compressed chunk size {stored_size} does not fit in the \
                     {chunk_size_len}-byte fixed-array chunk-size field"
                )));
            }
            if lidx < fa.fa_dblk.filtered_elements.len() {
                let old = &fa.fa_dblk.filtered_elements[lidx];
                let chunk_addr =
                    self.place_chunk(Some((old.address, old.chunk_size)), stored_size as u64);
                self.handle.write_at(chunk_addr, final_bytes)?;
                fa.fa_dblk.filtered_elements[lidx] = FixedArrayFilteredChunkElement {
                    address: chunk_addr,
                    chunk_size: stored_size as u64,
                    filter_mask,
                };
                fa.chunks_written += 1;
            } else {
                return Err(crate::io::IoError::InvalidState(format!(
                    "chunk index {} out of range (max {})",
                    linear_idx,
                    fa.fa_dblk.filtered_elements.len()
                )));
            }
        } else {
            // An unfiltered fixed array stores only addresses — there is no
            // slot for a filter mask, so a non-zero mask cannot be honored.
            if filter_mask != 0 {
                return Err(crate::io::IoError::InvalidState(
                    "filter_mask is non-zero but the dataset is unfiltered".into(),
                ));
            }
            if lidx < fa.fa_dblk.elements.len() {
                // Unfiltered: the stored size is fixed by the chunk shape, so
                // a rewrite always fits its old block.
                let old = fa.fa_dblk.elements[lidx];
                let len = final_bytes.len() as u64;
                let chunk_addr = self.place_chunk(Some((old, len)), len);
                self.handle.write_at(chunk_addr, final_bytes)?;
                fa.fa_dblk.elements[lidx] = chunk_addr;
                fa.chunks_written += 1;
            } else {
                return Err(crate::io::IoError::InvalidState(format!(
                    "chunk index {} out of range (max {})",
                    linear_idx,
                    fa.fa_dblk.elements.len()
                )));
            }
        }

        Ok(())
    }

    /// Write the one chunk of a single-chunk indexed dataset.
    ///
    /// `chunk_coords` is validated against the grid the same way every other
    /// coordinate-addressed index does (`ChunkGeometry::linear_index`), even
    /// though the grid holds exactly one slot — this is what rejects an
    /// out-of-range coordinate instead of silently writing to that slot.
    /// `data` is the chunk's unfiltered bytes; the dataset's filter pipeline
    /// runs here if it has one.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_chunk_single_chunk_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let geo = self.chunk_geometry(index)?;
        geo.linear_index(chunk_coords)?;
        let chunk_bytes = geo.chunk_bytes();
        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }
        let pipeline = self.ds(index).lock().filter_pipeline.clone();
        let write_data;
        let data_to_write = if let Some(ref pipeline) = pipeline {
            write_data = filter::apply_filters(pipeline, data)?;
            &write_data[..]
        } else {
            data
        };
        // filter_mask = 0: the whole pipeline ran (or the dataset is
        // unfiltered), so no filter is skipped for this chunk.
        self.record_single_chunk(index, data_to_write, 0)
    }

    /// Write a pre-filtered chunk verbatim to a single-chunk dataset,
    /// recording the caller-supplied `filter_mask`.
    ///
    /// The bytes are stored exactly as given (no filter pipeline is run); this
    /// is the single-chunk half of the HDF5 "direct chunk write"
    /// (`H5Dwrite_chunk`) operation. `filter_mask` is a bitfield: bit *i* set
    /// means filter *i* of the pipeline was **not** applied to this chunk and
    /// must be skipped on read; pass 0 when the full pipeline was applied
    /// upstream.
    ///
    /// Requires a filtered dataset — only the filtered single-chunk layout
    /// carries a size+mask slot.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_compressed_chunk_single_chunk_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        if self.ds(index).lock().filter_pipeline.is_none() {
            return Err(crate::io::IoError::InvalidState(
                "write_compressed_chunk_single_chunk requires a filtered dataset \
                 (no slot for a compressed size or filter mask on an unfiltered \
                 chunk index)"
                    .into(),
            ));
        }
        let geo = self.chunk_geometry(index)?;
        geo.linear_index(chunk_coords)?;
        self.record_single_chunk(index, data, filter_mask)
    }

    /// Place an already-final chunk (`final_bytes` is whatever goes to disk —
    /// filtered if the dataset is filtered, raw otherwise) into a single-chunk
    /// dataset's layout message fields, recording the caller-supplied
    /// `filter_mask`. Shared by
    /// [`write_chunk_single_chunk_inner`](Self::write_chunk_single_chunk_inner)
    /// and
    /// [`write_compressed_chunk_single_chunk_inner`](Self::write_compressed_chunk_single_chunk_inner).
    ///
    /// Unlike the array indexes there is no per-chunk slot to look up — the
    /// dataset has exactly one chunk, and its address/size/mask live directly
    /// in the layout message (`H5Dsingle.c`) — so this only ever rewrites the
    /// one chunk in place, via [`place_chunk`](Self::place_chunk) the same as
    /// every other index's rewrite path.
    fn record_single_chunk(
        &self,
        index: usize,
        final_bytes: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        if !is_filtered && filter_mask != 0 {
            return Err(crate::io::IoError::InvalidState(
                "filter_mask is non-zero but the dataset is unfiltered".into(),
            ));
        }
        let sc = m
            .single_chunk
            .as_ref()
            .ok_or_else(|| crate::io::IoError::InvalidState("not a single-chunk dataset".into()))?;

        // A rewrite whose stored size is unchanged stays where it is (always
        // so when unfiltered), one that no longer fits moves. See `place_chunk`.
        let old = if sc.data_addr == UNDEF_ADDR {
            None
        } else {
            Some((
                sc.data_addr,
                if is_filtered { sc.nbytes } else { sc.data_size },
            ))
        };
        let stored_size = final_bytes.len() as u64;
        let addr = self.place_chunk(old, stored_size);
        self.handle.write_at(addr, final_bytes)?;

        let sc = m.single_chunk.as_mut().unwrap();
        sc.data_addr = addr;
        sc.nbytes = stored_size;
        sc.filter_mask = filter_mask;
        sc.chunks_written = 1;
        Ok(())
    }

    /// Write a chunk to a B-tree v2 indexed dataset.
    ///
    /// `chunk_coords` is the scaled chunk coordinates (one per dimension).
    /// `data` is the chunk's unfiltered bytes; if the dataset has a filter
    /// pipeline it runs here and the index records the stored size and mask.
    ///
    /// Production writes call [`write_chunk_btree_v2_inner`](Self::write_chunk_btree_v2_inner)
    /// directly (they already hold the dataset's op lock); this self-locking
    /// form is kept as a direct entry point for this crate's own white-box
    /// tests.
    #[cfg(test)]
    pub fn write_chunk_btree_v2(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.write_chunk_btree_v2_inner(index, chunk_coords, data)
    }

    /// [`Self::write_chunk_btree_v2`] body; the caller holds the dataset's op
    /// lock or the writer exclusively.
    pub(crate) fn write_chunk_btree_v2_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
    ) -> IoResult<()> {
        // Read what the write needs under a brief guard, then compress OUTSIDE
        // the lock — filtering a chunk must not hold the dataset slot.
        let ds = self.ds(index);
        let (chunk_bytes, pipeline) = {
            let m = ds.lock();
            let element_size = m.datatype.element_size() as u64;
            let bt2 = m.btree_v2.as_ref().ok_or_else(|| {
                crate::io::IoError::InvalidState("not a B-tree v2 dataset".into())
            })?;
            (
                bt2.chunk_dims.iter().product::<u64>() * element_size,
                m.filter_pipeline.clone(),
            )
        };

        if data.len() as u64 != chunk_bytes {
            return Err(crate::io::IoError::InvalidState(format!(
                "chunk data size mismatch: expected {} bytes, got {}",
                chunk_bytes,
                data.len()
            )));
        }

        let filtered;
        let stored = match pipeline {
            Some(ref pl) => {
                filtered = filter::apply_filters(pl, data)?;
                &filtered[..]
            }
            None => data,
        };

        // filter_mask = 0: the whole pipeline ran (or the dataset is
        // unfiltered), so no filter is skipped.
        self.record_btree_v2_chunk(index, chunk_coords, stored, 0)
    }

    /// Write a pre-filtered chunk verbatim to a BT2-indexed dataset, recording
    /// the caller-supplied `filter_mask`.
    ///
    /// The v2-B-tree half of the HDF5 "direct chunk write" (`H5Dwrite_chunk`).
    /// The bytes are stored exactly as given; `filter_mask` bit *i* set means
    /// filter *i* of the pipeline was **not** applied and must be skipped on
    /// read. Requires a filtered dataset — only a type-11 record has a slot for
    /// a stored size and mask.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_compressed_chunk_btree_v2_inner(
        &self,
        index: usize,
        chunk_coords: &[u64],
        data: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        if self.ds(index).lock().filter_pipeline.is_none() {
            return Err(crate::io::IoError::InvalidState(
                "write_compressed_chunk_btree_v2 requires a filtered dataset (no \
                 slot for a compressed size or filter mask on an unfiltered chunk \
                 index)"
                    .into(),
            ));
        }
        self.record_btree_v2_chunk(index, chunk_coords, data, filter_mask)
    }

    /// Place a chunk's already-final bytes (filtered if the dataset is
    /// filtered, raw otherwise) in the file and record them in the v2 B-tree,
    /// under the caller-supplied `filter_mask`.
    ///
    /// Shared by [`write_chunk_btree_v2`](Self::write_chunk_btree_v2) and
    /// [`write_compressed_chunk_btree_v2`](Self::write_compressed_chunk_btree_v2),
    /// so both reach the index through one placement rule.
    fn record_btree_v2_chunk(
        &self,
        index: usize,
        chunk_coords: &[u64],
        final_bytes: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        let stored_len = final_bytes.len() as u64;
        let ds = self.ds(index);
        let mut m = ds.lock();
        let element_size = m.datatype.element_size() as u64;
        let bt2 = m
            .btree_v2
            .as_ref()
            .ok_or_else(|| crate::io::IoError::InvalidState("not a B-tree v2 dataset".into()))?;
        let chunk_bytes = bt2.chunk_dims.iter().product::<u64>() * element_size;
        // A filtered record encodes the stored size in a `chunk_size_len`-byte
        // field that truncates silently. Reject a size that would not fit, as
        // the extensible-array path does — the compress path never exceeds it,
        // but a direct write with caller-supplied bytes can.
        if bt2.index.filtered {
            let chunk_size_len = bt2.index.chunk_size_len as usize;
            if chunk_size_len < 8 && stored_len >= (1u64 << (chunk_size_len * 8)) {
                return Err(crate::io::IoError::InvalidState(format!(
                    "filtered chunk size {stored_len} does not fit in the \
                     {chunk_size_len}-byte v2 B-tree chunk-size field"
                )));
            }
        }
        // Place the bytes: a rewrite whose stored size is unchanged stays
        // where it is (always so when unfiltered — the size is fixed by the
        // chunk shape), and one that no longer fits moves, releasing its old
        // block. See `place_chunk`.
        let old = if bt2.index.filtered {
            bt2.index
                .lookup_filtered(chunk_coords)
                .map(|r| (r.chunk_address, r.chunk_size))
        } else {
            bt2.index
                .lookup(chunk_coords)
                .map(|r| (r.chunk_address, chunk_bytes))
        };
        let chunk_addr = self.place_chunk(old, stored_len);
        self.handle.write_at(chunk_addr, final_bytes)?;

        let bt2 = m.btree_v2.as_mut().unwrap();
        if bt2.index.filtered {
            bt2.index
                .insert_filtered(chunk_coords.to_vec(), chunk_addr, stored_len, filter_mask);
        } else {
            bt2.index.insert(chunk_coords.to_vec(), chunk_addr);
        }
        bt2.chunks_written += 1;

        Ok(())
    }

    /// Write multiple chunks in a batch, optionally compressing in parallel.
    ///
    /// `chunks` is a list of (chunk_idx, data) pairs for an EA-indexed dataset.
    pub fn write_chunks_batch(&self, ds_index: usize, chunks: &[(u64, &[u8])]) -> IoResult<()> {
        let ds = self.ds(ds_index);
        let _op = ds.op.lock();
        self.write_chunks_batch_inner(ds_index, chunks)
    }

    /// [`Self::write_chunks_batch`] body; the caller holds the dataset's op
    /// lock or the writer exclusively.
    pub(crate) fn write_chunks_batch_inner(
        &self,
        ds_index: usize,
        chunks: &[(u64, &[u8])],
    ) -> IoResult<()> {
        #[cfg(feature = "parallel")]
        {
            // If filter pipeline is set, compress all chunks in parallel.
            // Clone the pipeline out under a brief slot guard so the parallel
            // compression below runs off the lock.
            let pipeline = self.ds(ds_index).lock().filter_pipeline.clone();
            if let Some(ref pipeline) = pipeline {
                let chunk_data: Vec<&[u8]> = chunks.iter().map(|&(_, d)| d).collect();
                // Propagate a filter error rather than storing raw bytes under a
                // filter_mask that claims the pipeline ran (see
                // apply_filters_parallel). Ok reaching here means every chunk
                // compressed fully, so filter_mask = 0 is truthful.
                let compressed = filter::apply_filters_parallel(pipeline, &chunk_data)?;
                for ((idx, _), compressed_data) in chunks.iter().zip(compressed.iter()) {
                    self.write_compressed_chunk_inner(ds_index, *idx, compressed_data, 0)?;
                }
                return Ok(());
            }
        }
        // Fallback: sequential
        for (idx, data) in chunks {
            self.write_chunk_inner(ds_index, *idx, data)?;
        }
        Ok(())
    }

    /// Write multiple fixed-array chunks in a batch, compressing them in
    /// parallel when a filter pipeline is set and the `parallel` feature is on.
    ///
    /// The fixed-array analogue of [`write_chunks_batch`](Self::write_chunks_batch):
    /// chunks are addressed by grid coordinates rather than a linear index.
    /// `record_fixed_array_chunk` writes already-compressed bytes verbatim, so
    /// the parallel compressor is the only place a filter runs. Falls back to
    /// per-chunk [`write_chunk_fixed_array`](Self::write_chunk_fixed_array) when
    /// unfiltered or when `parallel` is off.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_chunks_fixed_array_batch_inner(
        &self,
        ds_index: usize,
        chunks: &[(&[u64], &[u8])],
    ) -> IoResult<()> {
        #[cfg(feature = "parallel")]
        {
            // Clone the pipeline out under a brief slot guard so the parallel
            // compression below runs off the lock.
            let pipeline = self.ds(ds_index).lock().filter_pipeline.clone();
            if let Some(ref pipeline) = pipeline {
                let chunk_data: Vec<&[u8]> = chunks.iter().map(|&(_, d)| d).collect();
                // Same single owner as the EA batch: apply_filters_parallel
                // propagates a filter error instead of storing raw bytes under a
                // filter_mask that claims the pipeline ran. Ok here means every
                // chunk compressed fully, so filter_mask = 0 is truthful.
                let compressed = filter::apply_filters_parallel(pipeline, &chunk_data)?;
                for ((coords, _), compressed_data) in chunks.iter().zip(compressed.iter()) {
                    self.record_fixed_array_chunk(ds_index, coords, compressed_data, 0)?;
                }
                return Ok(());
            }
        }
        // Fallback: sequential (write_chunk_fixed_array_inner compresses per
        // chunk).
        for (coords, data) in chunks {
            self.write_chunk_fixed_array_inner(ds_index, coords, data)?;
        }
        Ok(())
    }

    /// Write a pre-filtered chunk verbatim to an EA-indexed dataset, recording
    /// the caller-supplied `filter_mask`.
    ///
    /// The bytes are stored exactly as given (no filter pipeline is run); this
    /// is the extensible-array half of the HDF5 "direct chunk write"
    /// (`H5Dwrite_chunk`) operation. `filter_mask` is a bitfield: bit *i* set
    /// means filter *i* of the pipeline was **not** applied to this chunk and
    /// must be skipped on read; pass 0 when the full pipeline was applied
    /// upstream.
    ///
    /// Requires a filtered dataset — only the filtered EA entry carries the
    /// size+mask slot. An unfiltered dataset has nowhere to record either.
    ///
    /// The caller holds the dataset's op lock or the writer exclusively.
    pub(crate) fn write_compressed_chunk_inner(
        &self,
        index: usize,
        chunk_idx: u64,
        compressed_data: &[u8],
        filter_mask: u32,
    ) -> IoResult<()> {
        if self.ds(index).lock().filter_pipeline.is_none() {
            return Err(crate::io::IoError::InvalidState(
                "write_compressed_chunk requires a filtered dataset (no slot for \
                 a compressed size or filter mask on an unfiltered chunk index)"
                    .into(),
            ));
        }
        self.record_ea_chunk(index, chunk_idx, compressed_data, filter_mask)
    }

    /// Extend the dimensions of a chunked dataset.
    pub fn extend_dataset(&self, index: usize, new_dims: &[u64]) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.extend_dataset_inner(index, new_dims)
    }

    /// [`Self::extend_dataset`] body; the caller holds the dataset's op lock
    /// or the writer exclusively.
    pub(crate) fn extend_dataset_inner(&self, index: usize, new_dims: &[u64]) -> IoResult<()> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        if !m.is_chunked() {
            return Err(crate::io::IoError::InvalidState(
                "can only extend chunked datasets".into(),
            ));
        }
        if new_dims.len() != m.dataspace.dims.len() {
            return Err(crate::io::IoError::InvalidState(format!(
                "extend_dataset rank mismatch: dataset has {} dimensions, got {}",
                m.dataspace.dims.len(),
                new_dims.len()
            )));
        }
        // The chunk index and append buffers assume the logical size only
        // grows; shrinking below already-written data desynchronizes them.
        for (d, (&new, &cur)) in new_dims.iter().zip(&m.dataspace.dims).enumerate() {
            if new < cur {
                return Err(crate::io::IoError::InvalidState(format!(
                    "extend_dataset cannot shrink dimension {d} from {cur} to {new}"
                )));
            }
            // An absent maximum shape means the shape is fixed (libhdf5
            // defaults maxdims to dims at creation), so any growth exceeds it.
            match m.dataspace.max_dims {
                Some(ref max) if new > max[d] => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "extend_dataset dimension {d} ({new}) exceeds the maximum {}",
                        max[d]
                    )));
                }
                None if new > cur => {
                    return Err(crate::io::IoError::InvalidState(format!(
                        "extend_dataset dimension {d} ({new}) exceeds the maximum {cur}: \
                         a dataset without a stored maximum shape is fixed at its extent"
                    )));
                }
                _ => {}
            }
        }
        if m.dataspace.dims != new_dims {
            m.dataspace.dims = new_dims.to_vec();
            m.extent_dirty = true;
        }
        Ok(())
    }

    /// Set the logical extent of a chunked dataset, growing **or shrinking**
    /// any dimension (unlike [`extend_dataset`](Self::extend_dataset), which
    /// only grows).
    ///
    /// A shrink prunes the stored chunks the way libhdf5's
    /// `H5D__chunk_prune_by_extent` (H5Dchunk.c) does: a chunk entirely
    /// beyond the new extent leaves the chunk index and its block is freed
    /// for reuse (kept under SWMR, where a live reader may still hold its
    /// address — the rule `H5Dearray.c` applies in `idx_remove`), and a
    /// chunk the new extent cuts through has its out-of-extent region
    /// overwritten with the fill value, so growing the extent back exposes
    /// fill values rather than the stale data.
    pub fn set_dataset_extent(&self, index: usize, new_dims: &[u64]) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        let old_dims = {
            let m = ds.lock();
            if !m.is_chunked() {
                return Err(crate::io::IoError::InvalidState(
                    "can only set the extent of chunked datasets".into(),
                ));
            }
            if new_dims.len() != m.dataspace.dims.len() {
                return Err(crate::io::IoError::InvalidState(format!(
                    "set_extent rank mismatch: dataset has {} dimensions, got {}",
                    m.dataspace.dims.len(),
                    new_dims.len()
                )));
            }
            // A shrink can cut into buffered rows, whose recorded base would
            // then point past the extent; refuse rather than reconcile.
            if m.append.is_some() {
                return Err(crate::io::IoError::InvalidState(
                    "set_extent cannot run while the dataset has buffered appends; \
                     flush them first"
                        .into(),
                ));
            }
            // An absent maximum shape means the shape is fixed (libhdf5
            // defaults maxdims to dims at creation), so growth is bounded by
            // the extent.
            match m.dataspace.max_dims {
                Some(ref max) => {
                    for (d, (&new, &mx)) in new_dims.iter().zip(max).enumerate() {
                        if new > mx {
                            return Err(crate::io::IoError::InvalidState(format!(
                                "set_extent dimension {d} ({new}) exceeds the maximum {mx}"
                            )));
                        }
                    }
                }
                None => {
                    for (d, (&new, &cur)) in new_dims.iter().zip(&m.dataspace.dims).enumerate() {
                        if new > cur {
                            return Err(crate::io::IoError::InvalidState(format!(
                                "set_extent dimension {d} ({new}) exceeds the maximum {cur}: \
                                 a dataset without a stored maximum shape is fixed at its extent"
                            )));
                        }
                    }
                }
            }
            m.dataspace.dims.clone()
        };
        // A shrink strands chunks; prune them (and refill the straddlers)
        // *before* the dims update — chunk addressing uses the
        // maximum-extent grid, which the update does not change, and the
        // helpers re-lock the slot themselves.
        if new_dims.iter().zip(&old_dims).any(|(&n, &o)| n < o) {
            self.prune_chunks_beyond(index, new_dims)?;
        }
        let mut m = ds.lock();
        if m.dataspace.dims != new_dims {
            m.dataspace.dims = new_dims.to_vec();
            m.extent_dirty = true;
        }
        Ok(())
    }

    /// Remove and refill the chunks a shrink to `new_dims` strands — the
    /// libhdf5 `H5D__chunk_prune_by_extent` behavior. A chunk entirely
    /// beyond the new extent leaves the index and its block is freed (kept
    /// under SWMR, where a live reader may still hold its address); a chunk
    /// the extent cuts through gets its out-of-extent region refilled with
    /// the fill value, so a later regrow reads fill, not stale elements.
    ///
    /// Runs *before* the dims update: the index grid chunks are addressed in
    /// comes from the maximum extent, which a shrink never changes, so every
    /// stored entry still resolves. The caller holds the dataset's op lock.
    fn prune_chunks_beyond(&self, index: usize, new_dims: &[u64]) -> IoResult<()> {
        let geo = self.chunk_geometry(index)?;
        // A vlen dataset's elements are global-heap IDs: the pruned chunks
        // still reference live heap objects, so the walkers read each dead
        // chunk's bytes before freeing its block and the heap objects are
        // released here — otherwise every shrink strands its strings in the
        // file. `release_vlen_references` is a SWMR no-op, so the reads are
        // skipped under SWMR too.
        let collect_refs = !self.swmr_active && {
            let ds = self.ds(index);
            let m = ds.lock();
            matches!(
                m.datatype,
                DatatypeMessage::VarLenString { .. } | DatatypeMessage::VarLenSequence { .. }
            )
        };
        let (straddlers, dead_refs) = match geo.kind {
            ChunkIndexKind::ExtensibleArray => {
                self.prune_ea_chunks(index, &geo, new_dims, collect_refs)?
            }
            ChunkIndexKind::FixedArray => {
                self.prune_fa_chunks(index, &geo, new_dims, collect_refs)?
            }
            ChunkIndexKind::BtreeV2 => {
                self.prune_bt2_chunks(index, &geo, new_dims, collect_refs)?
            }
            // Removing a chunk from the implicit index is
            // `H5D__none_idx_remove`: a no-op, because the chunk's space is
            // the dataset's space and stays allocated either way. Only the
            // straddlers matter, and they are refilled by the caller.
            ChunkIndexKind::Implicit => (self.implicit_straddlers(&geo, new_dims)?, Vec::new()),
            // A single-chunk index has no per-chunk remove either — its one
            // chunk's address lives in the layout message, not an index
            // structure, and stays exactly where it is; a shrink only ever
            // straddles that one chunk (`H5D__single_idx_remove` is likewise
            // a no-op).
            ChunkIndexKind::SingleChunk => (self.implicit_straddlers(&geo, new_dims)?, Vec::new()),
            ChunkIndexKind::BtreeV1 => {
                self.prune_btree_v1_chunks(index, &geo, new_dims, collect_refs)?
            }
        };
        if !dead_refs.is_empty() {
            self.release_vlen_references(&dead_refs)?;
        }
        // Whole-chunk read-modify-write per straddler: an unfiltered chunk
        // rewrites in place, a filtered one re-places through `place_chunk`.
        let chunk_bytes = geo.chunk_bytes() as usize;
        for coords in straddlers {
            let Some(mut data) = self.read_chunk_at_coords(index, &coords)? else {
                continue;
            };
            let fill = self.new_chunk_buffer(index, chunk_bytes);
            let replaced = refill_chunk_beyond_extent(
                &mut data,
                &fill,
                &coords,
                &geo.chunk_dims,
                new_dims,
                geo.element_size as usize,
            );
            // Release before the write-back: a filtered straddler re-places
            // its block, and freed heap space must be visible to that
            // allocation (free-before-alloc, as everywhere else).
            if collect_refs && !replaced.is_empty() {
                self.release_vlen_references(&replaced)?;
            }
            self.write_chunk_at_coords(index, &coords, &data)?;
        }
        Ok(())
    }

    /// Extensible-array half of [`prune_chunks_beyond`](Self::prune_chunks_beyond):
    /// walk every slot the array has ever set, free and clear the entries of
    /// chunks entirely beyond `new_dims`, and return the grid coordinates of
    /// the chunks that straddle it, plus — when `collect_refs` — the dead
    /// chunks' element bytes so the caller can release their heap objects.
    fn prune_ea_chunks(
        &self,
        index: usize,
        geo: &ChunkGeometry,
        new_dims: &[u64],
        collect_refs: bool,
    ) -> IoResult<(Vec<Vec<u64>>, Vec<u8>)> {
        let ds = self.ds(index);
        // One slot guard for the whole walk, the `record_ea_chunk` pattern:
        // `self.handle`/`self.allocator`/`self.ctx` are disjoint fields.
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        let pipeline = m.filter_pipeline.clone();
        let chunk_bytes = geo.chunk_bytes();
        let (ea_geo, max_nelmts_bits, chunk_size_len, max_idx) = {
            let c = m.chunked.as_ref().unwrap();
            let p = &c.earray_params;
            (
                EaGeometry::new(
                    p.idx_blk_elmts,
                    p.data_blk_min_elmts,
                    p.sup_blk_min_data_ptrs,
                    p.max_nelmts_bits,
                    p.max_dblk_page_nelmts_bits,
                )?,
                p.max_nelmts_bits,
                c.chunk_size_len,
                c.ea_header.max_idx_set,
            )
        };

        let mut straddlers = Vec::new();
        let mut dead_refs = Vec::new();

        // The decoded data block the walk is currently inside, written back
        // when the walk leaves it (or ends) having cleared an entry.
        enum Dblk {
            Unfiltered(ExtensibleArrayDataBlock),
            Filtered(FilteredDataBlock),
        }
        let mut cache: Option<(u64, Dblk, bool)> = None;
        let flush = |cache: &mut Option<(u64, Dblk, bool)>| -> IoResult<()> {
            if let Some((addr, blk, dirty)) = cache.take() {
                if dirty {
                    let enc = match &blk {
                        Dblk::Unfiltered(d) => d.encode(&self.ctx, max_nelmts_bits),
                        Dblk::Filtered(d) => d.encode(&self.ctx, max_nelmts_bits, chunk_size_len),
                    };
                    self.handle.write_at(addr, &enc)?;
                }
            }
            Ok(())
        };
        // Consecutive slots resolve through the same super block, so keep
        // the last decode. Super blocks are only read here — clearing a
        // data-block element never moves the block — so it never dirties.
        let mut sblk_cache: Option<(usize, ExtensibleArraySuperBlock)> = None;

        let mut slot = 0u64;
        while slot < max_idx {
            let coords = crate::io::chunk_grid::coords_of(
                &geo.dims,
                geo.max_dims.as_deref(),
                &geo.chunk_dims,
                slot,
            )?;
            if !chunk_outside_extent(&coords, &geo.chunk_dims, new_dims) {
                if chunk_straddles_extent(&coords, &geo.chunk_dims, new_dims) {
                    straddlers.push(coords);
                }
                slot += 1;
                continue;
            }
            match ea_geo.locate(slot)? {
                EaLoc::Index { elem } => {
                    let c = m.chunked.as_mut().unwrap();
                    if is_filtered {
                        let fiblk = c.filt_iblk.as_mut().unwrap();
                        let e = fiblk.elements[elem];
                        if e.addr != UNDEF_ADDR {
                            if collect_refs {
                                if let Some(bytes) = self.read_chunk_block(
                                    pipeline.as_ref(),
                                    e.addr,
                                    e.nbytes,
                                    e.filter_mask,
                                )? {
                                    dead_refs.extend_from_slice(&bytes);
                                }
                            }
                            if !self.swmr_active {
                                self.allocator
                                    .free(e.addr, e.nbytes, FreeSpaceClass::RawData);
                            }
                            fiblk.elements[elem] = FilteredChunkEntry {
                                addr: UNDEF_ADDR,
                                nbytes: 0,
                                filter_mask: 0,
                            };
                        }
                    } else {
                        let a = c.ea_iblk.elements[elem];
                        if a != UNDEF_ADDR {
                            if collect_refs {
                                if let Some(bytes) =
                                    self.read_chunk_block(pipeline.as_ref(), a, chunk_bytes, 0)?
                                {
                                    dead_refs.extend_from_slice(&bytes);
                                }
                            }
                            if !self.swmr_active {
                                self.allocator.free(a, chunk_bytes, FreeSpaceClass::RawData);
                            }
                            c.ea_iblk.elements[elem] = UNDEF_ADDR;
                        }
                    }
                    slot += 1;
                }
                EaLoc::Dblk(l) => {
                    if l.paged {
                        return Err(crate::io::IoError::InvalidState(format!(
                            "chunk index {slot} lives in a paged extensible-array \
                             data block, which is not yet supported"
                        )));
                    }
                    let dblk_start = slot - l.offset_in_dblk;
                    let dblk_end = dblk_start + l.dblk_nelmts;
                    // Resolve the data block's address; an undefined super or
                    // data block means nothing in its whole element range was
                    // ever written, so the walk skips the range.
                    let dblk_addr = {
                        let c = m.chunked.as_ref().unwrap();
                        match l.path {
                            EaDblkPath::Direct { idx } => {
                                if is_filtered {
                                    c.filt_iblk.as_ref().unwrap().dblk_addrs[idx]
                                } else {
                                    c.ea_iblk.dblk_addrs[idx]
                                }
                            }
                            EaDblkPath::ViaSblk {
                                sblk_off,
                                local_dblk,
                                ndblks_in_sblk,
                                ..
                            } => {
                                let sblk_addr = if is_filtered {
                                    c.filt_iblk.as_ref().unwrap().sblk_addrs[sblk_off]
                                } else {
                                    c.ea_iblk.sblk_addrs[sblk_off]
                                };
                                if sblk_addr == UNDEF_ADDR {
                                    UNDEF_ADDR
                                } else {
                                    if sblk_cache.as_ref().map(|&(o, _)| o) != Some(sblk_off) {
                                        let buf = self.handle.read_at_most(sblk_addr, 65536)?;
                                        let sb = ExtensibleArraySuperBlock::decode(
                                            &buf,
                                            &self.ctx,
                                            max_nelmts_bits,
                                            ndblks_in_sblk,
                                            0,
                                        )?;
                                        sblk_cache = Some((sblk_off, sb));
                                    }
                                    sblk_cache.as_ref().unwrap().1.dblk_addrs[local_dblk]
                                }
                            }
                        }
                    };
                    if dblk_addr == UNDEF_ADDR {
                        slot = dblk_end;
                        continue;
                    }
                    if cache.as_ref().map(|&(a, _, _)| a) != Some(dblk_addr) {
                        flush(&mut cache)?;
                        let buf = self.handle.read_at_most(dblk_addr, 65536)?;
                        let blk = if is_filtered {
                            Dblk::Filtered(FilteredDataBlock::decode(
                                &buf,
                                &self.ctx,
                                max_nelmts_bits,
                                l.dblk_nelmts as usize,
                                chunk_size_len,
                            )?)
                        } else {
                            Dblk::Unfiltered(ExtensibleArrayDataBlock::decode(
                                &buf,
                                &self.ctx,
                                max_nelmts_bits,
                                l.dblk_nelmts as usize,
                            )?)
                        };
                        cache = Some((dblk_addr, blk, false));
                    }
                    let (_, blk, dirty) = cache.as_mut().unwrap();
                    match blk {
                        Dblk::Filtered(d) => {
                            let e = d.elements[l.offset_in_dblk as usize];
                            if e.addr != UNDEF_ADDR {
                                if collect_refs {
                                    if let Some(bytes) = self.read_chunk_block(
                                        pipeline.as_ref(),
                                        e.addr,
                                        e.nbytes,
                                        e.filter_mask,
                                    )? {
                                        dead_refs.extend_from_slice(&bytes);
                                    }
                                }
                                if !self.swmr_active {
                                    self.allocator
                                        .free(e.addr, e.nbytes, FreeSpaceClass::RawData);
                                }
                                d.elements[l.offset_in_dblk as usize] = FilteredChunkEntry {
                                    addr: UNDEF_ADDR,
                                    nbytes: 0,
                                    filter_mask: 0,
                                };
                                *dirty = true;
                            }
                        }
                        Dblk::Unfiltered(d) => {
                            let a = d.elements[l.offset_in_dblk as usize];
                            if a != UNDEF_ADDR {
                                if collect_refs {
                                    if let Some(bytes) =
                                        self.read_chunk_block(pipeline.as_ref(), a, chunk_bytes, 0)?
                                    {
                                        dead_refs.extend_from_slice(&bytes);
                                    }
                                }
                                if !self.swmr_active {
                                    self.allocator.free(a, chunk_bytes, FreeSpaceClass::RawData);
                                }
                                d.elements[l.offset_in_dblk as usize] = UNDEF_ADDR;
                                *dirty = true;
                            }
                        }
                    }
                    slot += 1;
                }
            }
        }
        flush(&mut cache)?;
        Ok((straddlers, dead_refs))
    }

    /// Fixed-array half of [`prune_chunks_beyond`](Self::prune_chunks_beyond):
    /// the whole element array is in memory and flushed at close, so
    /// clearing an entry is pure bookkeeping.
    fn prune_fa_chunks(
        &self,
        index: usize,
        geo: &ChunkGeometry,
        new_dims: &[u64],
        collect_refs: bool,
    ) -> IoResult<(Vec<Vec<u64>>, Vec<u8>)> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        let is_filtered = m.filter_pipeline.is_some();
        let pipeline = m.filter_pipeline.clone();
        let chunk_bytes = geo.chunk_bytes();
        let mut straddlers = Vec::new();
        let mut dead_refs = Vec::new();
        let fa = m.fixed_array.as_mut().unwrap();
        let nslots = if is_filtered {
            fa.fa_dblk.filtered_elements.len()
        } else {
            fa.fa_dblk.elements.len()
        };
        for lidx in 0..nslots {
            let (addr, stored, mask) = if is_filtered {
                let e = &fa.fa_dblk.filtered_elements[lidx];
                (e.address, e.chunk_size, e.filter_mask)
            } else {
                (fa.fa_dblk.elements[lidx], chunk_bytes, 0)
            };
            if addr == UNDEF_ADDR {
                continue;
            }
            let coords = crate::io::chunk_grid::coords_of(
                &geo.dims,
                geo.max_dims.as_deref(),
                &geo.chunk_dims,
                lidx as u64,
            )?;
            if chunk_outside_extent(&coords, &geo.chunk_dims, new_dims) {
                if collect_refs {
                    if let Some(bytes) =
                        self.read_chunk_block(pipeline.as_ref(), addr, stored, mask)?
                    {
                        dead_refs.extend_from_slice(&bytes);
                    }
                }
                if !self.swmr_active {
                    self.allocator.free(addr, stored, FreeSpaceClass::RawData);
                }
                if is_filtered {
                    fa.fa_dblk.filtered_elements[lidx] = FixedArrayFilteredChunkElement {
                        address: UNDEF_ADDR,
                        chunk_size: 0,
                        filter_mask: 0,
                    };
                } else {
                    fa.fa_dblk.elements[lidx] = UNDEF_ADDR;
                }
            } else if chunk_straddles_extent(&coords, &geo.chunk_dims, new_dims) {
                straddlers.push(coords);
            }
        }
        Ok((straddlers, dead_refs))
    }

    /// Implicit half of [`prune_chunks_beyond`](Self::prune_chunks_beyond):
    /// the grid coordinates of the chunks a shrink to `new_dims` cuts
    /// through. Nothing is freed or cleared — this index has no per-chunk
    /// state to clear and no per-chunk block to free — so the chunks wholly
    /// beyond the extent keep their bytes, exactly as `H5D__none_idx_remove`
    /// leaves them. That also means their elements stay reachable, so a
    /// variable-length dataset's heap objects must *not* be released here.
    fn implicit_straddlers(
        &self,
        geo: &ChunkGeometry,
        new_dims: &[u64],
    ) -> IoResult<Vec<Vec<u64>>> {
        let mut nchunks: u64 = 1;
        for g in
            crate::io::chunk_grid::index_grid(&geo.dims, geo.max_dims.as_deref(), &geo.chunk_dims)?
        {
            nchunks = nchunks.checked_mul(g).ok_or_else(|| {
                crate::io::IoError::InvalidState("chunk count overflows u64".into())
            })?;
        }
        let mut straddlers = Vec::new();
        for lidx in 0..nchunks {
            let coords = crate::io::chunk_grid::coords_of(
                &geo.dims,
                geo.max_dims.as_deref(),
                &geo.chunk_dims,
                lidx,
            )?;
            if chunk_straddles_extent(&coords, &geo.chunk_dims, new_dims) {
                straddlers.push(coords);
            }
        }
        Ok(straddlers)
    }

    /// V2-B-tree half of [`prune_chunks_beyond`](Self::prune_chunks_beyond):
    /// drop the records of chunks beyond the extent — the next flush
    /// re-serializes the smaller tree over the node pool and releases the
    /// surplus node blocks.
    fn prune_bt2_chunks(
        &self,
        index: usize,
        geo: &ChunkGeometry,
        new_dims: &[u64],
        collect_refs: bool,
    ) -> IoResult<(Vec<Vec<u64>>, Vec<u8>)> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        let pipeline = m.filter_pipeline.clone();
        let chunk_bytes = geo.chunk_bytes();
        let swmr = self.swmr_active;
        let mut straddlers = Vec::new();
        let mut dead_refs = Vec::new();
        let bt2 = m.btree_v2.as_mut().unwrap();
        if bt2.index.filtered {
            let records = std::mem::take(&mut bt2.index.filtered_records);
            let mut kept = Vec::with_capacity(records.len());
            for r in records {
                if chunk_outside_extent(&r.scaled_offsets, &geo.chunk_dims, new_dims) {
                    if collect_refs {
                        if let Some(bytes) = self.read_chunk_block(
                            pipeline.as_ref(),
                            r.chunk_address,
                            r.chunk_size,
                            r.filter_mask,
                        )? {
                            dead_refs.extend_from_slice(&bytes);
                        }
                    }
                    if !swmr {
                        self.allocator
                            .free(r.chunk_address, r.chunk_size, FreeSpaceClass::RawData);
                    }
                } else {
                    if chunk_straddles_extent(&r.scaled_offsets, &geo.chunk_dims, new_dims) {
                        straddlers.push(r.scaled_offsets.clone());
                    }
                    kept.push(r);
                }
            }
            bt2.index.filtered_records = kept;
        } else {
            let records = std::mem::take(&mut bt2.index.records);
            let mut kept = Vec::with_capacity(records.len());
            for r in records {
                if chunk_outside_extent(&r.scaled_offsets, &geo.chunk_dims, new_dims) {
                    if collect_refs {
                        if let Some(bytes) = self.read_chunk_block(
                            pipeline.as_ref(),
                            r.chunk_address,
                            chunk_bytes,
                            0,
                        )? {
                            dead_refs.extend_from_slice(&bytes);
                        }
                    }
                    if !swmr {
                        self.allocator
                            .free(r.chunk_address, chunk_bytes, FreeSpaceClass::RawData);
                    }
                } else {
                    if chunk_straddles_extent(&r.scaled_offsets, &geo.chunk_dims, new_dims) {
                        straddlers.push(r.scaled_offsets.clone());
                    }
                    kept.push(r);
                }
            }
            bt2.index.records = kept;
        }
        Ok((straddlers, dead_refs))
    }

    /// Version-1-B-tree half of [`prune_chunks_beyond`](Self::prune_chunks_beyond):
    /// drop the records of chunks beyond the extent — the next flush
    /// re-serializes the smaller tree over the node pool and releases the
    /// surplus node blocks.
    fn prune_btree_v1_chunks(
        &self,
        index: usize,
        geo: &ChunkGeometry,
        new_dims: &[u64],
        collect_refs: bool,
    ) -> IoResult<(Vec<Vec<u64>>, Vec<u8>)> {
        let ds = self.ds(index);
        let mut m = ds.lock();
        let pipeline = m.filter_pipeline.clone();
        let swmr = self.swmr_active;
        let mut straddlers = Vec::new();
        let mut dead_refs = Vec::new();
        let bt1 = m.btree_v1.as_mut().unwrap();
        let records = std::mem::take(&mut bt1.records);
        let mut kept = Vec::with_capacity(records.len());
        for r in records {
            if chunk_outside_extent(&r.scaled, &geo.chunk_dims, new_dims) {
                if collect_refs {
                    if let Some(bytes) = self.read_chunk_block(
                        pipeline.as_ref(),
                        r.address,
                        r.nbytes as u64,
                        r.filter_mask,
                    )? {
                        dead_refs.extend_from_slice(&bytes);
                    }
                }
                if !swmr {
                    self.allocator
                        .free(r.address, r.nbytes as u64, FreeSpaceClass::RawData);
                }
            } else {
                if chunk_straddles_extent(&r.scaled, &geo.chunk_dims, new_dims) {
                    straddlers.push(r.scaled.clone());
                }
                kept.push(r);
            }
        }
        m.btree_v1.as_mut().unwrap().records = kept;
        Ok((straddlers, dead_refs))
    }

    /// Flush a chunked dataset's index structures to disk (durable).
    ///
    /// Writes the index blocks and issues an `fdatasync` so the data is
    /// durable — the guarantee SWMR readers and standalone callers rely on.
    pub fn flush_dataset(&self, index: usize) -> IoResult<()> {
        let ds = self.ds(index);
        let _op = ds.op.lock();
        self.flush_dataset_synced(index, true)
    }

    /// Flush a chunked dataset's index structures, syncing only if `sync`.
    ///
    /// `finalize` threads its own durability choice here so that a
    /// [`close_no_sync`](Self::close_no_sync) skips this per-dataset
    /// `sync_data` too — otherwise gating only the final `sync_all` would
    /// leave one `fdatasync` per indexed dataset and defeat the fast close.
    fn flush_dataset_synced(&self, index: usize, sync: bool) -> IoResult<()> {
        // Hold one slot guard for the whole method; `self.handle`/`self.ctx`/
        // `self.allocator` below touch disjoint fields.
        let ds = self.ds(index);
        let mut m = ds.lock();

        // EA-indexed dataset
        if let Some(ref chunked) = m.chunked {
            if let Some(ref fiblk) = chunked.filt_iblk {
                // Filtered EA
                let iblk_encoded = fiblk.encode(&self.ctx, chunked.chunk_size_len);
                self.handle.write_at(chunked.ea_iblk_addr, &iblk_encoded)?;
            } else {
                // Unfiltered EA
                let iblk_encoded = chunked.ea_iblk.encode(&self.ctx);
                self.handle.write_at(chunked.ea_iblk_addr, &iblk_encoded)?;
            }
            let hdr_encoded = chunked.ea_header.encode(&self.ctx);
            self.handle.write_at(chunked.ea_header_addr, &hdr_encoded)?;
            if sync {
                self.handle.sync_data()?;
            }
            return Ok(());
        }

        // Fixed-array-indexed dataset
        if let Some(ref fa) = m.fixed_array {
            let dblk_encoded = encode_fixed_array_dblk(&self.ctx, &fa.fa_header, &fa.fa_dblk);
            self.handle.write_at(fa.fa_dblk_addr, &dblk_encoded)?;
            let hdr_encoded = fa.fa_header.encode(&self.ctx);
            self.handle.write_at(fa.fa_header_addr, &hdr_encoded)?;
            if sync {
                self.handle.sync_data()?;
            }
            return Ok(());
        }

        // BT2-indexed dataset
        if let Some(ref bt2) = m.btree_v2 {
            // Bulk-load the index into fixed-size nodes and lay them over the
            // dataset's block pool. Because every node is the same size, the
            // blocks already on disk are reused in place and only the shortfall
            // is allocated — the pool is the single owner of these addresses,
            // so no flush leaves a block behind. The addresses a reader already
            // holds stay valid, which is also what SWMR needs.
            let tree = bt2.index.build_tree(&self.ctx);
            let mut node_addrs = bt2.node_addrs.clone();
            while node_addrs.len() < tree.nodes.len() {
                node_addrs.push(
                    self.allocator
                        .allocate(tree.node_size as u64, FreeSpaceClass::Metadata),
                );
            }
            // A tree with fewer nodes than last flush releases the surplus
            // rather than leaving it recorded and unreachable, so the pool is
            // exactly one block per node whichever way the count moved. Under
            // SWMR a reader may still hold a header naming those blocks, so
            // keep them out of the free list — the same rule `place_chunk`
            // applies to a relocated chunk.
            for addr in node_addrs.split_off(tree.nodes.len()) {
                if !self.swmr_active {
                    self.allocator
                        .free(addr, tree.node_size as u64, FreeSpaceClass::Metadata);
                }
            }

            for (image, &addr) in tree.encode(&self.ctx, &node_addrs).iter().zip(&node_addrs) {
                self.handle.write_at(addr, image)?;
            }

            // The root is the last node the bulk load emits.
            let root_addr = match tree.nodes.len() {
                0 => UNDEF_ADDR,
                n => node_addrs[n - 1],
            };
            let hdr_encoded = tree.header(root_addr).encode(&self.ctx);
            self.handle.write_at(bt2.bt2_header_addr, &hdr_encoded)?;

            m.btree_v2.as_mut().unwrap().node_addrs = node_addrs;

            if sync {
                self.handle.sync_data()?;
            }
            return Ok(());
        }

        // Version-1-B-tree-indexed dataset
        if let Some(ref bt1) = m.btree_v1 {
            // Bulk-loaded over the same block pool the v2 B-tree above uses,
            // and for the same reason: every node of a v1 tree is the width
            // its "K" value gives, so a block stays usable however the tree
            // reshapes, and only the shortfall is ever allocated.
            let element_size = m.datatype.element_size() as u64;
            let tree = bt1.build_tree(element_size, self.ctx.sizeof_addr as usize);
            let node_size = tree.node_size() as u64;
            let mut node_addrs = bt1.node_addrs.clone();
            while node_addrs.len() < tree.node_count() {
                node_addrs.push(self.allocator.allocate(node_size, FreeSpaceClass::Metadata));
            }
            // A tree with fewer nodes than last flush releases the surplus
            // straight away, where the v2 B-tree has to keep it out of the
            // free list for a live SWMR reader: this index lives only in a
            // classic file, which `start_swmr` refuses outright (and upstream
            // says the same in `H5D_COPS_BTREE`).
            for addr in node_addrs.split_off(tree.node_count()) {
                self.allocator
                    .free(addr, node_size, FreeSpaceClass::Metadata);
            }
            for (image, &addr) in tree.encode(&node_addrs)?.iter().zip(&node_addrs) {
                self.handle.write_at(addr, image)?;
            }
            // The root is the last node the bulk load emits, and is undefined
            // while the dataset has no chunks — what the version-3 data
            // layout message then carries, exactly as libhdf5 leaves it.
            let root_addr = tree.root_address(&node_addrs);
            let bt1 = m.btree_v1.as_mut().unwrap();
            bt1.node_addrs = node_addrs;
            bt1.root_addr = root_addr;

            if sync {
                self.handle.sync_data()?;
            }
            return Ok(());
        }

        Ok(())
    }

    /// Finalize and close the file.
    ///
    /// Writes the dataset object headers, root group object header, and
    /// superblock. After this call the file is a valid HDF5 file.
    pub fn close(mut self) -> IoResult<()> {
        self.close_in_place()
    }

    /// [`close`](Self::close) for a holder that cannot give the writer up by
    /// value because it has a `Drop` of its own ([`SwmrWriter`]): the same
    /// one-shot commit, after which this writer's `Drop` is a no-op.
    ///
    /// [`SwmrWriter`]: crate::io::swmr::SwmrWriter
    pub(crate) fn close_in_place(&mut self) -> IoResult<()> {
        // Mark closed BEFORE finalizing: finalize writes external truth
        // (object headers + superblock) and must run exactly once. If we
        // finalized first and it failed, the `?` would return with `closed`
        // still false, and dropping `self` would re-run `finalize` a second
        // time over a half-written file (and print the "call close()" notice
        // the caller already heeded). Committing to the close path first makes
        // `Drop` (the only other finalize site) a no-op regardless of outcome,
        // so the error is reported exactly once via this `Result`.
        self.closed = true;
        self.finalize(true)
    }

    /// Finalize and close the file without a final `fsync`.
    ///
    /// Identical to [`close`](Self::close) — the same object headers and
    /// superblock are written, so on return the file is a complete, valid HDF5
    /// file readable by any process — except that the trailing `sync_all`
    /// (fsync) is skipped. The bytes are handed to the OS but are not
    /// guaranteed durable against power loss or an OS crash until the OS
    /// flushes its page cache; a normal process exit or a same-machine reader
    /// sees the full file regardless.
    ///
    /// This trades durability for speed: `sync_all` typically dominates close
    /// latency, so bulk writers that do not need crash durability (the file can
    /// be regenerated) can use this to avoid that cost. Use [`close`](Self::close)
    /// when durability matters. `Drop` always finalizes durably, so a writer
    /// finalized this way must reach `close_no_sync` explicitly.
    pub fn close_no_sync(mut self) -> IoResult<()> {
        // Same close-once discipline as `close`: commit to the close path
        // before finalizing so `Drop` cannot re-run `finalize` on failure.
        self.closed = true;
        self.finalize(false)
    }

    /// Provide mutable access to the underlying file handle.
    pub fn handle(&mut self) -> &mut FileHandle {
        &mut self.handle
    }

    /// The superblock version this file will be written with.
    ///
    /// `H5F__super_init` takes the oldest version that can describe the file
    /// and raises it to the one the file's library-version low bound implies:
    /// `super_vers = MAX(super_vers, HDF5_superblock_ver_bounds[low_bound])`,
    /// with the bounds table reading 0, 2, 3, 3, 3, 3, 3 for EARLIEST, V18,
    /// V110, V112, V114, V200, LATEST (H5Fsuper.c:68, :1128-1154). A file
    /// created at `H5F_LIBVER_EARLIEST` takes that bound's entry directly
    /// ([`SuperblockVersion::Chosen`], and the classic branch below) — version
    /// 0, or version 2 when the file carries shared messages, whose master
    /// table needs the superblock extension only a version-2 superblock has
    /// (H5Fsuper.c:1135). For every other file the bound is read back from
    /// what this crate writes:
    ///
    /// * The floor is `H5F_LIBVER_V18`, hence version 2. Every group such a
    ///   file holds is a link-message group, which libhdf5 only writes at a
    ///   low bound of V18 or newer (`use_at_least_v18`, H5Gobj.c:179), and
    ///   every object header in it is version 2, which `H5O_obj_ver_bounds`
    ///   likewise puts at V18 (H5Oint.c:125). A version-0 superblock over
    ///   this content would claim a file libhdf5 1.6 can read, and no libhdf5
    ///   writes that combination.
    /// * A chunked dataset — extensible array, fixed array or version-2
    ///   B-tree, all reached through a version-4 or -5 data layout message —
    ///   reads back as V110 (`H5O_layout_ver_bounds`, H5Dlayout.c:44), hence
    ///   version 3.
    /// * SWMR writes version 3 outright (H5Fsuper.c:1129).
    ///
    /// A file whose caller *named* a bound skips the read-back and takes that
    /// bound's row directly, so `V18` stays at version 2 however its chunked
    /// datasets are indexed — which is what libhdf5 does, the layout version
    /// being no input to `H5F__super_init` at all.
    ///
    /// None of that applies to a reopened file. `H5F__super_read` validates
    /// the version it finds and never recomputes one, so the version written
    /// back is the version read, whatever this session appends — see
    /// [`SuperblockVersion`].
    fn superblock_version_for(&self, flags: u8) -> u8 {
        let chosen = match self.superblock_version {
            SuperblockVersion::Existing(version) => return version,
            SuperblockVersion::Chosen(version) => version,
        };
        if self.is_legacy() {
            // A classic file keeps the version it was created at — 0, or 2
            // when its shared messages needed the extension. Nothing a session
            // can add reaches past that: its objects get symbol-table links,
            // its chunked datasets the version-1 B-tree behind a version-3
            // layout message, and the two features that would raise the bound
            // — SWMR and the 2.0 format — are refused where the caller asks
            // for them.
            return chosen;
        }
        let mut version = chosen
            .max(SUPERBLOCK_V2)
            .max(self.effective_libver().superblock_version());
        if self.swmr_active || flags & FLAG_SWMR_WRITE != 0 {
            version = version.max(SUPERBLOCK_V3);
        }
        version
    }

    /// The low bound a modern file this writer *created* is effectively
    /// written at: the one the caller named, or — with none named — the one
    /// its content reads back as. A reopened file never reaches here; its
    /// superblock version is not derived from its content at all.
    ///
    /// The read-back is what `superblock_version_for` needs and the field
    /// alone cannot give: this crate's default file names no bound, and the
    /// generation it writes is not one bound but two rows (see the `libver`
    /// field). The floor is `V18`, the oldest bound under which libhdf5 writes
    /// link-message groups (`use_at_least_v18`, H5Gobj.c:179) and version-2
    /// object headers (`H5O_obj_ver_bounds`, H5Oint.c:125), which is all such
    /// a file holds; a v1.10 chunk index in it raises that to `V110`, the
    /// oldest bound whose `H5O_layout_ver_bounds` row reaches the version-4
    /// layout message that index is written behind.
    fn effective_libver(&self) -> LibverBound {
        self.libver.unwrap_or_else(|| {
            if self.has_v110_chunk_index() {
                LibverBound::V110
            } else {
                LibverBound::V18
            }
        })
    }

    /// Whether any dataset still in the file is indexed by a v1.10 chunk
    /// index — the markers `build_dataset_header` turns into a version-4/5
    /// data layout message, and nothing else it can emit reaches that
    /// version.
    ///
    /// Not "is any dataset chunked": the version-1 B-tree is a chunk index
    /// that encodes as a *version-3* layout message, the version
    /// `H5O_layout_ver_bounds` gives the earliest bound, so a dataset using
    /// it asks nothing of the superblock.
    fn has_v110_chunk_index(&self) -> bool {
        self.dataset_refs().iter().any(|d| {
            let m = d.lock();
            !m.deleted
                && m.chunk_index_kind()
                    .is_some_and(|k| k != ChunkIndexKind::BtreeV1)
        })
    }

    /// Write the superblock at offset 0 with the given flags.
    ///
    /// Requires that the root group has already been written (via `finalize`
    /// or `finalize_for_swmr`).
    pub fn write_superblock(&mut self, flags: u8) -> IoResult<()> {
        let root_addr = self
            .root_group_addr
            .ok_or_else(|| crate::io::IoError::InvalidState("root group not yet written".into()))?;
        // The userblock this file was opened with. `H5F__super_read` prefers
        // the located address over this field, but `H5Pget_userblock` reports
        // it, so a rewrite that zeroed it would hide the block from every
        // reader that asks for its size.
        let base = self.handle.base();
        // The end of file is the one address in the superblock measured from
        // the start of the *file* rather than from the base: `H5F__super_read`
        // sets the EOA to `stored_eof - base_addr` (H5Fsuper.c:635) and calls
        // the file truncated when `eof + base_addr < stored_eof` (:573). The
        // allocator counts in the based space, so the userblock is added back.
        let eof = self.allocator.eof() + base;
        let version = self.superblock_version_for(flags);
        // Which of the two images is written follows the version, not the
        // generation: a classic file carrying shared messages is a version-2
        // superblock over version-1 messages and symbol-table groups
        // (H5Fsuper.c:1135), and only the version-2/3 image has the extension
        // address that table is reached through. Below version 2 the file is
        // always a classic one — the other branch floors at 2.
        if let Some(legacy) = self.legacy.as_deref().filter(|_| version < SUPERBLOCK_V2) {
            // Re-emitted, not rebuilt: the "K" ranks, the userblock size and
            // the driver info address are recorded nowhere else in the file,
            // and every node width in it is derived from the ranks. Only the
            // three things this session can have changed are recomputed.
            let root_stab = self
                .symbol_tables
                .written
                .lock()
                .get(&LinkScope::Root)
                .copied();
            let mut sb = legacy.superblock.clone();
            sb.version = version;
            sb.file_consistency_flags = flags as u32;
            sb.end_of_file_address = eof;
            sb.root_symbol_table_entry.obj_header_addr = root_addr;
            // `H5G__stab_valid` (H5Groot.c) reads this pair back and compares
            // it against the root header's Symbol Table message, repairing the
            // superblock when they disagree. Writing the pair that message now
            // names is what keeps the file from needing that repair. A root
            // that keeps its links in messages has no such pair and no entry
            // in `written`, and gets `H5G_NOTHING_CACHED` — what libhdf5
            // writes for the same root.
            sb.root_symbol_table_entry.cache = match root_stab {
                Some(s) => SymbolTableCache::SymbolTable {
                    btree_addr: s.btree_addr,
                    heap_addr: s.heap_addr,
                },
                None => SymbolTableCache::Nothing,
            };
            self.handle.write_at(0, &sb.encode())?;
            return Ok(());
        }
        let sb = SuperblockV2V3 {
            version,
            sizeof_offsets: self.ctx.sizeof_addr,
            sizeof_lengths: self.ctx.sizeof_size,
            file_consistency_flags: flags,
            base_address: base,
            // Whatever `write_superblock_extension` put there, which is the
            // only place an extension is written.
            superblock_extension_address: self.extension.addr.lock().unwrap_or(UNDEF_ADDR),
            end_of_file_address: eof,
            root_group_object_header_address: root_addr,
        };
        self.handle.write_at(0, &sb.encode())?;
        Ok(())
    }

    /// Re-write a dataset's object header in place (SWMR update).
    ///
    /// The header must have been written by `finalize_for_swmr`, and goes
    /// back over the same blocks: chunk 0 held to its block and the
    /// continuation chunk, when it has one, to its own. Only the dataspace
    /// dimensions are meant to change; a header that no longer fits is
    /// refused rather than moved, since a reader holds its address.
    pub fn write_dataset_header_inplace(&mut self, index: usize) -> IoResult<()> {
        // Scope the slot guard: `build_dataset_header` re-locks the same slot.
        let placement = {
            let ds = self.ds(index);
            let m = ds.lock();
            HeaderPlacement::over(&m.obj_header_blocks).ok_or_else(|| {
                crate::io::IoError::InvalidState("dataset header not yet written".into())
            })?
        };

        let header = self.build_dataset_header(index)?;
        let nlink = self.object_link_count(HardLinkTarget::Dataset(index));
        let format = self.dataset_header_format(index);
        let images = self.encode_header_in(&header, nlink, format, &placement)?;
        let reserved = placement.blocks();
        let fits = images.len() == reserved.len()
            && images
                .iter()
                .zip(&reserved)
                .all(|((_, image), &(_, size))| image.len() as u64 == size);
        if !fits {
            return Err(crate::io::IoError::InvalidState(format!(
                "dataset header grew from {} to {} bytes; cannot rewrite in place",
                reserved.iter().map(|&(_, size)| size).sum::<u64>(),
                images.iter().map(|(_, image)| image.len()).sum::<usize>()
            )));
        }
        for (addr, image) in &images {
            self.handle.write_at(*addr, image)?;
        }
        // Only after the bytes are down: a failed write leaves the registry
        // describing the header the file still holds.
        self.ds(index).lock().header_written(nlink);
        Ok(())
    }

    /// Perform a full finalize for SWMR mode.
    ///
    /// This writes all dataset object headers, the root group header, and the
    /// superblock with SWMR flags. After this call, the file is valid for
    /// SWMR readers. Subsequent writes use in-place updates.
    pub fn finalize_for_swmr(&mut self) -> IoResult<()> {
        self.reject_swmr()?;
        // 0. Flush all chunked dataset index structures.
        for i in 0..self.dataset_count() {
            let is_indexed = {
                let ds = self.ds(i);
                let m = ds.lock();
                !m.deleted && m.is_chunked()
            };
            if is_indexed {
                self.flush_dataset(i)?;
            }
        }

        // 1. Allocate every object header (none for a dataset deleted before
        // start_swmr — its storage was freed at delete time). Same three
        // phases as the full finalize, and for the same reason: nothing a
        // header names can be laid out until every object has an address.
        let live: Vec<usize> = (0..self.dataset_count())
            .filter(|&i| !self.ds(i).lock().deleted)
            .collect();
        let kept = self.supersede_headers(&live);
        // Before any dataset header: a sharing dataset's header names the
        // committed type's address.
        self.write_committed_datatype_headers()?;
        let layout = self.allocate_object_headers(&live, &kept)?;

        // 2. Build content against those addresses.
        self.prepare_dense_attributes(&live)?;
        self.prepare_link_storage()?;
        self.write_reference_values()?;

        // 3. Write every object header.
        self.write_object_headers(&layout)?;
        // Where each header is published and how much room it has: what
        // `write_dataset_header_inplace` rewrites within, and what the
        // closing finalize writes over, since a reader may by then hold any
        // of these addresses.
        for &(i, placement) in &layout.datasets {
            let ds = self.ds(i);
            let mut m = ds.lock();
            m.obj_header_written_addr = Some(placement.addr);
            m.obj_header_blocks = placement.blocks();
        }
        for &(gi, placement) in &layout.groups {
            let grp = self.grp(gi);
            let mut g = grp.lock();
            g.obj_header_written_addr = Some(placement.addr);
            g.obj_header_blocks = placement.blocks();
        }
        self.superseded_root_header = layout.root.blocks();

        // 4. Write superblock with SWMR flags.
        self.write_superblock(FLAG_WRITE_ACCESS | FLAG_SWMR_WRITE)?;
        self.handle.set_eof(self.allocator.eof())?;

        self.handle.sync_all()?;
        // Readers can now be following this file, so a chunk that moves must
        // leave its old block intact for whoever is still holding the previous
        // index (see `swmr_active`).
        self.swmr_active = true;
        Ok(())
    }

    // ------------------------------------------------------------------
    // Internal helpers
    // ------------------------------------------------------------------

    /// Flush every dataset's append buffer into the chunks it belongs to,
    /// through [`flush_append_buffer`](Self::flush_append_buffer): frames
    /// already in the chunk survive, and the rest of it reads back as the
    /// dataset's fill value (zeros when none is defined).
    fn flush_append_buffers(&mut self) -> IoResult<()> {
        for i in 0..self.dataset_count() {
            if self.ds(i).lock().deleted {
                continue;
            }
            self.flush_append_buffer(i)?;
        }
        Ok(())
    }

    /// Write all object headers and the superblock, producing a complete,
    /// valid HDF5 file.
    ///
    /// `sync == true` issues a final `sync_all` (fsync) so the bytes are
    /// durable against power loss / OS crash before returning. `sync == false`
    /// skips that fsync: the file is still fully written to the OS and readable
    /// by any process, but durability is left to the OS page-cache flush. This
    /// is the only difference between [`close`](Self::close) (durable) and
    /// [`close_no_sync`](Self::close_no_sync) (fast).
    fn finalize(&mut self, sync: bool) -> IoResult<()> {
        // Flush any partial append buffers before finalizing
        self.flush_append_buffers()?;

        // A SWMR session (`finalize_for_swmr` already ran, so
        // `root_group_addr` is `Some`) is closed by the same full finalize as
        // a fresh write: every object header is rebuilt over its chunk 0 and
        // the superblock is written with clean-close flags. A full rebuild —
        // rather than the in-place header rewrite used by the live
        // `SwmrWriter::flush` path — is required so any structural change made
        // after `start_swmr` is committed to the final file. A hard link, in
        // particular, both grows its target's header with an object
        // reference-count message and adds a `MSG_LINK` record to a group
        // header; an in-place rewrite cannot accommodate the grown header and
        // never re-emits group/root headers. The fall-through below already
        // handles datasets whose header was written by `finalize_for_swmr`
        // (`obj_header_written_addr.is_some()`).

        // 0. Flush chunked dataset index structures (only modified datasets).
        for i in 0..self.dataset_count() {
            let ds = self.ds(i);
            {
                let m = ds.lock();
                if m.deleted {
                    continue;
                }
                if m.obj_header_written_addr.is_some() && !m.storage_dirty() {
                    continue;
                }
                let is_indexed = m.is_chunked();
                if !is_indexed {
                    continue;
                }
            }
            self.flush_dataset_synced(i, sync)?;
        }

        // 1. Plan. Which datasets get a header (deleted datasets get none —
        // their storage was already freed at delete time) is settled first,
        // because everything the next phases lay out is laid out only for the
        // headers this finalize actually rewrites; and every header those
        // phases supersede is taken here, before the first allocation, so
        // the rewrite lands over it.
        let mut rewritten: Vec<usize> = Vec::new();
        // A finalize that lays the shared-message table out afresh reassigns
        // every heap ID in the file, so no existing header can keep its bytes:
        // the pointers in them name heap objects the new table does not have.
        let table_replaced = self.rebuilds_shared_messages();
        for i in 0..self.dataset_count() {
            // Before the slot guard: `object_link_count` re-locks every
            // dataset and group slot, this one included.
            let nlink = self.object_link_count(HardLinkTarget::Dataset(i));
            let ds = self.ds(i);
            let mut m = ds.lock();
            if m.deleted {
                continue;
            }
            // An existing dataset from append mode keeps its header — and
            // everything that header names — unless this session changed
            // what the header says.
            if let Some(written) = m.obj_header_written_addr {
                if !table_replaced && !m.header_stale_with(nlink) {
                    // Keep the original object header address for the root group link.
                    m.obj_header_addr = written;
                    continue;
                }
            }
            rewritten.push(i);
        }
        let kept = self.supersede_headers(&rewritten);

        // 2. Allocate. Committed datatype headers go down whole: a header of
        // theirs holds a datatype and a reference count, so it waits on
        // nothing, while a dataset sharing the type and the group naming it
        // both store its address. They are written before the shared-message
        // phase opens, so a committed type reaches the file as itself.
        self.write_committed_datatype_headers()?;
        self.begin_shared_message_layout();
        let layout = self.allocate_object_headers(&rewritten, &kept)?;

        // 3. Build content, with every object header's address known. Dense
        // attribute storage holds the attribute messages themselves — an
        // object reference among them is a header address; dense links and
        // symbol tables name header addresses; a reference dataset's elements
        // are header addresses. Nothing here is a fixup: each is written once,
        // with the value the file keeps. The shared-message table comes last:
        // it counts the bodies the headers will hold, and the three above are
        // what settle them.
        self.prepare_dense_attributes(&rewritten)?;
        self.prepare_link_storage()?;
        self.write_reference_values()?;
        self.prepare_shared_messages(&rewritten)?;
        self.write_superblock_extension()?;

        // 4. Write every object header over the block phase 2 reserved for it.
        self.write_object_headers(&layout)?;

        // 5. Write superblock at offset 0.
        self.write_superblock(0)?;

        // 6. End the file where its address space ends (`H5FD_truncate`, which
        // `H5F__dest` calls on every close). Allocated-but-unwritten space at
        // the end would otherwise leave the file shorter than the end-of-file
        // address the superblock just recorded, which libhdf5 reads as a
        // truncated file.
        self.handle.set_eof(self.allocator.eof())?;

        // Durability is opt-in per call: `close` passes `true`, `close_no_sync`
        // passes `false`, and `Drop` passes `true` so an un-`close`d writer is
        // still finalized durably by default.
        if sync {
            self.handle.sync_all()?;
        }
        Ok(())
    }

    /// Take the on-disk header of every object this finalize rewrites — the
    /// datasets in `datasets`, every group, and the root — and hand each
    /// chunk-0 block to [`allocate_object_headers`](Self::allocate_object_headers)
    /// to be written over.
    ///
    /// Chunk 0 stays where it is: its address is what every reference in the
    /// file holds. The continuation blocks behind it go back to the free
    /// list, so the rewrite reuses them instead of growing the file on every
    /// open/close cycle — nothing names one but its own header. Hard links
    /// can alias one header under several names; the set keeps an aliased
    /// chain from being taken twice. The registry forgets each header here,
    /// so a finalize that fails later describes none the file no longer holds.
    fn supersede_headers(&mut self, datasets: &[usize]) -> KeptChunks {
        let mut kept = KeptChunks::default();
        let mut taken = std::collections::HashSet::new();
        for &i in datasets {
            let ds = self.ds(i);
            let mut m = ds.lock();
            let Some(old) = m.obj_header_written_addr.take() else {
                continue;
            };
            let blocks = std::mem::take(&mut m.obj_header_blocks);
            if !blocks.is_empty() && taken.insert(old) {
                kept.datasets.insert(i, self.keep_chunk0(blocks));
            }
        }
        for gi in 0..self.group_count() {
            let grp = self.grp(gi);
            let mut g = grp.lock();
            let Some(old) = g.obj_header_written_addr.take() else {
                continue;
            };
            let blocks = std::mem::take(&mut g.obj_header_blocks);
            if !blocks.is_empty() && taken.insert(old) {
                kept.groups.insert(gi, self.keep_chunk0(blocks));
            }
        }
        let root_blocks = std::mem::take(&mut self.superseded_root_header);
        if root_blocks
            .first()
            .is_some_and(|&(addr, _)| taken.insert(addr))
        {
            kept.root = Some(self.keep_chunk0(root_blocks));
        }
        kept
    }

    /// Keep `blocks`' chunk 0 for a rewrite and free the continuation blocks
    /// behind it — never under SWMR, where a live reader may be walking them,
    /// the same rule `release_vlen_references` and `place_chunk` follow.
    fn keep_chunk0(&self, blocks: crate::io::object_header_io::HeaderBlocks) -> (u64, u64) {
        let mut blocks = blocks.into_iter();
        let chunk0 = blocks.next().expect("a written header has a chunk 0");
        if !self.swmr_active {
            for (addr, len) in blocks {
                self.allocator.free(addr, len, FreeSpaceClass::Metadata);
            }
        }
        chunk0
    }

    /// Give every object header this finalize writes an address, before
    /// anything that names one is built.
    ///
    /// INVARIANT: from the moment this returns until the file is closed, every
    /// object in it has the object header address it will be found at. That is
    /// what lets the phase after this one say an address wherever the format
    /// wants one — in a link message, in a symbol table entry, in a reference
    /// dataset's elements, and in an attribute's value, which is the one of the
    /// four that cannot be revisited after its header is written.
    ///
    /// An object in `kept` is placed over the chunk-0 block it already has, so
    /// its address is the one every reference in the file already holds. A
    /// header is measured before its content is final, which is sound because
    /// no address changes its length: every address is a fixed-width field,
    /// and an object that has none yet reads as zero, which is the same width.
    /// The storage a header names is laid out between the two passes for the
    /// same reason and answers the same way — `emit_attributes` and
    /// `emit_links` each fall back to a size-equal placeholder message. It is
    /// [`write_object_headers`](Self::write_object_headers) that checks this
    /// held, rather than either pass assuming it.
    fn allocate_object_headers(
        &mut self,
        datasets: &[usize],
        kept: &KeptChunks,
    ) -> IoResult<HeaderLayout> {
        let mut layout = HeaderLayout {
            datasets: Vec::with_capacity(datasets.len()),
            groups: Vec::new(),
            root: HeaderPlacement::fresh(0, 0),
        };
        for &i in datasets {
            let header = self.build_dataset_header(i)?;
            let format = self.dataset_header_format(i);
            let placement = self.place_header(&header, format, kept.datasets.get(&i).copied())?;
            self.ds(i).lock().obj_header_addr = placement.addr;
            layout.datasets.push((i, placement));
        }
        for gi in 0..self.group_count() {
            if self.grp(gi).lock().deleted {
                continue;
            }
            let header = self.build_group_header(gi)?;
            let format = self.group_header_format(gi);
            let placement = self.place_header(&header, format, kept.groups.get(&gi).copied())?;
            self.grp(gi).lock().obj_header_addr = placement.addr;
            layout.groups.push((gi, placement));
        }
        let header = self.build_root_group_header()?;
        let format = self.header_format(self.root_track_order);
        let placement = self.place_header(&header, format, kept.root)?;
        self.root_group_addr = Some(placement.addr);
        layout.root = placement;
        Ok(layout)
    }

    /// Write every object header over the blocks
    /// [`allocate_object_headers`](Self::allocate_object_headers) reserved for
    /// it.
    ///
    /// The single owner of object header writing in both finalize paths, and
    /// the only place a header's body meets its block: a body that does not
    /// fill its measurement exactly fails the finalize here rather than
    /// overrunning the next object or leaving a tail of the previous one, which
    /// is how a message whose length turns out to depend on an address would
    /// show up.
    fn write_object_headers(&mut self, layout: &HeaderLayout) -> IoResult<()> {
        for &(i, placement) in &layout.datasets {
            let rc = self.object_link_count(HardLinkTarget::Dataset(i));
            let header = self.build_dataset_header(i)?;
            let format = self.dataset_header_format(i);
            let what = format!("dataset '{}'", self.ds(i).lock().name);
            self.write_header_in(&header, rc, format, &placement, &what)?;
            // Only after the bytes are down: a failed write leaves the registry
            // describing the header the file still holds.
            self.ds(i).lock().header_written(rc);
        }
        for &(gi, placement) in &layout.groups {
            let rc = self.object_link_count(HardLinkTarget::Group(gi));
            let header = self.build_group_header(gi)?;
            let format = self.group_header_format(gi);
            let what = format!("group '{}'", self.grp(gi).lock().name);
            self.write_header_in(&header, rc, format, &placement, &what)?;
        }
        let header = self.build_root_group_header()?;
        let format = self.header_format(self.root_track_order);
        self.write_header_in(&header, 1, format, &layout.root, "the root group")
    }

    /// Encode `header` into `placement` and write it, after checking each
    /// image against the block reserved for it.
    fn write_header_in(
        &mut self,
        header: &ObjectHeader,
        rc: u32,
        format: ObjectFormat,
        placement: &HeaderPlacement,
        what: &str,
    ) -> IoResult<()> {
        let images = self.encode_header_in(header, rc, format, placement)?;
        let reserved =
            std::iter::once(placement.size).chain(placement.continuation.map(|(_, s)| s));
        for ((addr, image), size) in images.iter().zip(reserved) {
            check_header_size(image, size, || what.to_string())?;
            self.handle.write_at(*addr, image)?;
        }
        Ok(())
    }

    fn build_dataset_header(&self, index: usize) -> IoResult<ObjectHeader> {
        // Compute the link count first: object_link_count re-locks dataset and
        // group slots (including this one), so it must run before we take this
        // dataset's slot guard — otherwise it would deadlock on the same slot.
        let rc = self.object_link_count(HardLinkTarget::Dataset(index));
        // Same reason: reading the committed type's address locks the
        // committed-datatype registry, which the slot guard below must not be
        // held across.
        let committed = self.ds(index).lock().committed_type;
        let committed_addr = committed.map(|r| match r {
            CommittedTypeRef::Session(ci) => self.committed_datatypes.lock()[ci].obj_header_addr,
            CommittedTypeRef::Preserved(addr) => addr,
        });
        // And again: an attribute holding an object reference is said in the
        // target's header address, which is read off that object's slot.
        let attributes = self.object_attributes(AttrScope::Dataset(index))?;

        // Hold one slot guard for the whole header build.
        let ds = self.ds(index);
        let m = ds.lock();
        let mut header = ObjectHeader::new();

        // Every message below is written in the format this dataset already
        // has, not the one this session would pick. libhdf5 grows a header in
        // place and never re-encodes a message it did not touch, so a reopen
        // at a newer bound leaves the version-1 dataspaces an EARLIEST-bound
        // creating session wrote exactly as they are. This writer has to lay
        // the whole header out again whenever the shared-message heap moves,
        // so preserving the encoding is the only way to land on the same
        // bytes.
        let format = m.read_format.unwrap_or_else(|| self.message_format());
        let libver = match format {
            ObjectFormat::Legacy => LibverBound::Earliest,
            ObjectFormat::Modern => self.encoding_libver(),
        };

        // Dataspace message (type 0x01)
        let ds_msg = m.dataspace.encode_for(&self.ctx, format);
        let owner = ShareOwner::Header(m.obj_header_addr);
        let (flags, ds_msg) = self.share_message(owner, MSG_DATASPACE, 0x00, ds_msg);
        header.add_message(MSG_DATASPACE, flags, ds_msg);

        // Datatype message (type 0x03). A dataset built on a committed type
        // stores a pointer to that object header in place of the message, and
        // the shared flag is what says the body is a pointer — the two are one
        // statement, so they are written together.
        match committed_addr {
            Some(addr) => header.add_message(
                MSG_DATATYPE,
                MSG_FLAG_CONSTANT | MSG_FLAG_SHARED,
                SharedMessagePointer::encode_committed(addr, &self.ctx),
            ),
            None => {
                let body = m.datatype.encode_at(&self.ctx, libver);
                let (flags, body) = if self.dataset_datatype_shareable(&m.datatype, libver) {
                    self.share_message(owner, MSG_DATATYPE, MSG_FLAG_CONSTANT, body)
                } else {
                    (MSG_FLAG_CONSTANT, body)
                };
                header.add_message(MSG_DATATYPE, flags, body)
            }
        }

        // Fill Value message (type 0x05)
        let is_chunked = m.is_chunked();
        // `H5P__init_def_layout` gives each storage class its own default
        // allocation time: incremental for chunked and for virtual (whose
        // source datasets are allocated as they are written), early for
        // compact (the space is the header, so it exists as soon as the
        // dataset does), late for contiguous. An implicitly indexed dataset is
        // the one chunked exception, and not by default but by definition:
        // early allocation is a *condition* of that index
        // (`H5D__layout_set_latest_indexing`), so a header claiming
        // incremental would describe a file libhdf5 would never have chosen
        // this index for. A single-chunk dataset can go either way — unlike
        // Implicit, early allocation is not one of its selection conditions
        // — so its `early_alloc` flag (set only for an unfiltered dataset
        // created that way) is what this checks instead.
        let alloc_time = if m.compact.is_some()
            || m.implicit.is_some()
            || m.single_chunk.as_ref().is_some_and(|s| s.early_alloc)
        {
            1 // early
        } else if is_chunked || m.virtual_storage.is_some() {
            3 // incremental
        } else {
            2 // late
        };
        // `H5D__update_oh_info` (H5Dint.c:927-943): a variable-length
        // datatype with no explicit fill value forces ALLOC regardless of
        // the declared policy — its heap-reference encoding has no safe
        // all-zero "no fill" representation, so libhdf5 always writes the
        // (empty) fill value at allocation for such a dataset. `IFSET` is
        // the only declared policy this touches: an explicit `ALLOC` is
        // already what it forces, and upstream rejects `NEVER` for a
        // VL-typed dataset at `H5Dcreate` outright — this crate's
        // VL-typed datasets have no builder path to declare `NEVER` in the
        // first place, so that branch cannot be reached here.
        let is_vlen = matches!(
            m.datatype,
            DatatypeMessage::VarLenString { .. } | DatatypeMessage::VarLenSequence { .. }
        );
        let fill_write_time = if is_vlen && m.fill_value.is_none() && m.fill_time == FILL_TIME_IFSET
        {
            FILL_TIME_ALLOC
        } else {
            m.fill_time
        };
        let fv = if let Some(ref bytes) = m.fill_value {
            // User-defined fill value (fill_defined = 2).
            FillValueMessage {
                alloc_time,
                fill_write_time,
                fill_defined: 2,
                fill_value: Some(bytes.clone()),
            }
        } else {
            // No fill value of the dataset's own (fill_defined = 1, the
            // implicit default zero fill) — `alloc_time` above already
            // carries the per-layout-class default (`H5P__set_layout`,
            // H5Pdcpl.c:1864-1877), so this branch must use it too instead
            // of `FillValueMessage::default()`'s hardcoded LATE: that was
            // wrong for a compact (EARLY) or virtual (INCR) dataset with no
            // fill value, only coincidentally right for contiguous.
            FillValueMessage {
                alloc_time,
                fill_write_time,
                fill_defined: 1, // default value (zeros)
                fill_value: None,
            }
        };
        // `H5O_MSG_FLAG_CONSTANT`, as `H5D__update_oh_info` appends it
        // (H5Dint.c:965) — the same flag the datatype message beside it
        // carries (H5Dint.c:961) and the old fill value below (H5Dint.c:981).
        // A dataset's fill value is fixed at creation: `H5Pset_fill_value` is
        // a creation property, so nothing can rewrite the message in place and
        // libhdf5 tells the header so.
        let fv_msg = fv.encode_for(format);
        let (flags, fv_msg) = self.share_message(owner, MSG_FILL_VALUE, MSG_FLAG_CONSTANT, fv_msg);
        header.add_message(MSG_FILL_VALUE, flags, fv_msg);

        // The "fill value (old)" message (type 0x04) beside the new one, for a
        // user-defined fill value below the v1.8 bound. `H5D__update_oh_info`
        // (H5Dint.c:1024-1035) appends `H5O_FILL_ID` whenever `fill_prop->buf`
        // is set and `use_at_least_v18` — `H5F_LOW_BOUND(file) >= V18`, which
        // here is exactly a non-`Legacy` message format — is false, so that a
        // reader that predates the new message still finds the value. The body
        // is the size and the bytes and nothing else: no allocation time, no
        // write time, no defined flag (`H5O__fill_old_encode`, H5Ofill.c:512).
        if matches!(format, ObjectFormat::Legacy) {
            if let Some(ref bytes) = m.fill_value {
                let mut old = Vec::with_capacity(4 + bytes.len());
                old.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
                old.extend_from_slice(bytes);
                let (flags, old) =
                    self.share_message(owner, MSG_FILL_VALUE_OLD, MSG_FLAG_CONSTANT, old);
                header.add_message(MSG_FILL_VALUE_OLD, flags, old);
            }
        }

        // External Data Files message (type 0x07), before the layout message
        // and marked constant, exactly where `H5D__layout_oh_create` puts it.
        // It is what makes a reader route the dataset's I/O through the files
        // it names rather than through the undefined address the layout
        // message below still declares.
        if let Some(ref ext) = m.external {
            header.add_message(
                MSG_EXTERNAL_FILE_LIST,
                MSG_FLAG_CONSTANT,
                ext.message().encode(&self.ctx),
            );
        }

        // Data Layout message (type 0x08)
        let layout = if let Some(ref chunked) = m.chunked {
            let mut layout_dims = chunked.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            DataLayoutMessage::chunked_v4_earray(
                m.layout_version,
                layout_dims,
                chunked.earray_params.clone(),
                chunked.ea_header_addr,
            )
        } else if let Some(ref fa) = m.fixed_array {
            let mut layout_dims = fa.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            DataLayoutMessage::chunked_v4_farray(
                m.layout_version,
                layout_dims,
                FixedArrayParams::default_params(),
                fa.fa_header_addr,
            )
        } else if let Some(ref bt2) = m.btree_v2 {
            let mut layout_dims = bt2.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            DataLayoutMessage::chunked_v4_btree_v2(
                m.layout_version,
                layout_dims,
                crate::format::messages::data_layout::Bt2Params {
                    node_size: bt2.index.node_size,
                    split_percent: bt2.index.split_percent,
                    merge_percent: bt2.index.merge_percent,
                },
                bt2.bt2_header_addr,
            )
        } else if let Some(ref imp) = m.implicit {
            let mut layout_dims = imp.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            DataLayoutMessage::chunked_v4_implicit(m.layout_version, layout_dims, imp.data_addr)
        } else if let Some(ref sc) = m.single_chunk {
            let mut layout_dims = sc.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            if m.filter_pipeline.is_some() {
                DataLayoutMessage::chunked_v4_single_filtered(
                    layout_dims,
                    sc.data_addr,
                    sc.nbytes,
                    sc.filter_mask,
                )
            } else {
                DataLayoutMessage::chunked_v4_single(layout_dims, sc.data_addr)
            }
        } else if let Some(ref bt1) = m.btree_v1 {
            // The classic index: a version-3 layout message carrying the
            // address of the tree's root node, which is undefined until a
            // chunk is written.
            let mut layout_dims = bt1.chunk_dims.clone();
            layout_dims.push(m.datatype.element_size() as u64);
            DataLayoutMessage::chunked_v3_btree_v1(layout_dims, bt1.root_addr)
        } else if let Some(ref image) = m.compact {
            DataLayoutMessage::compact(image.clone())
        } else if let Some(ref virt) = m.virtual_storage {
            // Version 4 always: the virtual layout class did not exist before
            // it, so the default virtual layout is created at version 4 and
            // `H5Pset_virtual` raises any lower one to it (H5Pdcpl.c),
            // whatever the file's library-version bounds say — which is why a
            // v0-superblock file can still hold one.
            DataLayoutMessage::virtual_layout(4, virt.heap_addr, virt.heap_index)
        } else {
            DataLayoutMessage::contiguous(m.data_addr, m.data_size)
        };
        // `H5D__layout_oh_create` (H5Dlayout.c:530-536) marks the layout
        // message constant only where the storage it names is certain to be
        // there already: allocation time is early, the class is not compact,
        // no filter can change a chunk's size, and the dataspace holds at
        // least one element. Anything else leaves the address undefined at
        // creation and rewrites the message when the space is allocated, so
        // the flag would be a lie. `H5S_GET_EXTENT_NPOINTS` is zero for a
        // NULL dataspace and for any extent with a zero-length dimension.
        let npoints: u64 = if m.dataspace.is_null() {
            0
        } else {
            m.dataspace.dims.iter().product()
        };
        let filtered = m
            .filter_pipeline
            .as_ref()
            .is_some_and(|p| !p.filters.is_empty());
        let layout_flags = if alloc_time == 1 && m.compact.is_none() && !filtered && npoints != 0 {
            MSG_FLAG_CONSTANT
        } else {
            0x00
        };
        let layout_msg = layout.encode(&self.ctx);
        header.add_message(MSG_DATA_LAYOUT, layout_flags, layout_msg);

        // Filter Pipeline message (type 0x0B) -- only if filters are
        // configured. `H5D__layout_oh_create` appends it with
        // `H5O_MSG_FLAG_CONSTANT` (H5Dlayout.c:462), as does the group
        // pipeline for dense links (H5Gobj.c:264): the pipeline is a creation
        // property, and every chunk already written was filtered through it,
        // so it can never be rewritten in place.
        if let Some(ref pipeline) = m.filter_pipeline {
            if !pipeline.filters.is_empty() {
                let (flags, filter_msg) = self.share_message(
                    owner,
                    MSG_FILTER_PIPELINE,
                    MSG_FLAG_CONSTANT,
                    pipeline.encode_for(format),
                );
                header.add_message(MSG_FILTER_PIPELINE, flags, filter_msg);
            }
        }

        // A dataset has no links, so only attribute creation order can raise
        // its header past version 1 (`H5O__set_version`).
        let format = self.header_format(TrackOrder {
            links: CreationOrder::default(),
            attrs: m.track_attr_order,
        });

        // Modification time, here and not earlier: `H5D__update_oh_info` makes
        // this the last message it writes (H5Dint.c:1022-1026), and the
        // attributes below it are added by `H5A` calls that come after the
        // dataset exists.
        touch_oh(&mut header, format, m.times, true);

        // Attribute Info (type 0x15) + attribute messages (type 0x0C).
        self.emit_attributes(
            &mut header,
            AttrScope::Dataset(index),
            &attributes,
            m.track_attr_order,
            format,
            owner,
        );

        self.emit_refcount(&mut header, rc, format);

        Ok(header)
    }

    /// Write the object header of every committed datatype something still
    /// reaches, recording the address each one landed at.
    ///
    /// Runs before the dataset and group headers because both name these
    /// addresses — a sharing dataset in its datatype message, the parent
    /// group in the link. One pass is enough: the header holds a datatype
    /// message and at most a reference count, neither of which depends on an
    /// address.
    fn write_committed_datatype_headers(&mut self) -> IoResult<()> {
        // The count is bound first: a lock guard in the `for` iterator
        // expression would live for the whole loop body, which locks the same
        // registry again.
        let count = self.committed_datatypes.lock().len();
        for i in 0..count {
            let rc = self.committed_datatype_refcount(i);
            if rc == 0 {
                // Its name's group was deleted and no dataset shares it, so
                // nothing in the file could reach the header.
                continue;
            }
            let format = self.committed_datatype_header_format();
            let encoded = self
                .build_committed_datatype_header(i, rc, format)
                .encode_for(format, rc)?;
            let addr = self
                .allocator
                .allocate(encoded.len() as u64, FreeSpaceClass::Metadata);
            self.handle.write_at(addr, &encoded)?;
            self.committed_datatypes.lock()[i].obj_header_addr = addr;
        }
        Ok(())
    }

    /// The header format a committed datatype gets.
    ///
    /// `H5T__commit` creates the header from the datatype creation property
    /// list (H5Tcommit.c:468), which carries no link order and, by default, no
    /// attribute order — so the version is the file's floor exactly as
    /// `H5O__set_version` computes it, and a committed datatype in a classic
    /// file is a version-1 header like every other object in it.
    fn committed_datatype_header_format(&self) -> ObjectFormat {
        self.header_format(TrackOrder::default())
    }

    /// Build the object header for a committed datatype: the type, and the
    /// reference count when more than one name reaches it.
    fn build_committed_datatype_header(
        &self,
        index: usize,
        rc: u32,
        format: ObjectFormat,
    ) -> ObjectHeader {
        let (datatype, times) = {
            let reg = self.committed_datatypes.lock();
            (reg[index].datatype.clone(), reg[index].times)
        };
        let mut header = ObjectHeader::new();
        // No attributes to emit, so nothing else would apply the file-wide
        // floor to this header. `store_msg_crt_idx` is a property of the file,
        // not of the object: every header created under it records creation
        // indices, a committed datatype's included.
        header.set_attribute_creation_order(self.header_attr_order(CreationOrder::default()));
        // `H5T__commit` marks the message constant and unshareable: this
        // header is where shared datatype bodies are read *from*, so its own
        // message must never become a pointer into the shared-message heap.
        header.add_message(
            MSG_DATATYPE,
            MSG_FLAG_CONSTANT | MSG_FLAG_DONTSHARE,
            datatype.encode_at(&self.ctx, self.encoding_libver()),
        );
        touch_oh(&mut header, format, times, false);
        // Through the same owner as every other object's count: a dataset
        // sharing this type raises it (`H5O__shared_link_adj`, H5Oshared.c:249)
        // just as a second name does, and where that count is recorded is the
        // header version's business, not the caller's.
        self.emit_refcount(&mut header, rc, format);
        header
    }

    /// Build the object header for a subgroup.
    fn build_group_header(&self, group_idx: usize) -> IoResult<ObjectHeader> {
        let mut header = ObjectHeader::new();

        // Link Info (type 0x02) + Group Info (type 0x0A) + the links
        // themselves, compact or dense.
        // Snapshot what the header needs, then drop the slot guard: the calls
        // below re-lock group slots (including this one).
        let (track_order, times, owner) = {
            let grp = self.grp(group_idx);
            let g = grp.lock();
            (
                g.track_order,
                g.times,
                ShareOwner::Header(g.obj_header_addr),
            )
        };
        let attributes = self.object_attributes(AttrScope::Group(group_idx))?;
        touch_oh(&mut header, self.header_format(track_order), times, false);

        let links = self.group_links(LinkScope::Group(group_idx), track_order.links);
        self.emit_links(
            &mut header,
            LinkScope::Group(group_idx),
            &links,
            track_order.links,
        );

        // Attribute Info (type 0x15) + attributes (type 0x0C) -- e.g. NeXus
        // `NX_class`.
        let format = self.header_format(track_order);
        self.emit_attributes(
            &mut header,
            AttrScope::Group(group_idx),
            &attributes,
            track_order.attrs,
            format,
            owner,
        );

        self.emit_refcount(
            &mut header,
            self.object_link_count(HardLinkTarget::Group(group_idx)),
            format,
        );

        Ok(header)
    }

    fn build_root_group_header(&self) -> IoResult<ObjectHeader> {
        let mut header = ObjectHeader::new();
        touch_oh(
            &mut header,
            self.header_format(self.root_track_order),
            self.root_times,
            false,
        );

        // Link Info (type 0x02) + Group Info (type 0x0A) + the links
        // themselves, compact or dense.
        let links = self.group_links(LinkScope::Root, self.root_track_order.links);
        self.emit_links(
            &mut header,
            LinkScope::Root,
            &links,
            self.root_track_order.links,
        );

        // Root-level attributes
        let root_attributes = self.object_attributes(AttrScope::Root)?;
        self.emit_attributes(
            &mut header,
            AttrScope::Root,
            &root_attributes,
            self.root_track_order.attrs,
            self.header_format(self.root_track_order),
            ShareOwner::Header(self.root_group_addr.unwrap_or(0)),
        );

        Ok(header)
    }
}

impl Drop for Hdf5Writer {
    fn drop(&mut self) {
        if !self.closed {
            // Best-effort finalize on drop. Drop cannot return a Result, so a
            // failure here is otherwise invisible: it would leave a truncated
            // or unflushed file on disk while the caller believes the write
            // succeeded. Surface it on stderr instead of swallowing it.
            // Callers that need to handle the error must call
            // `H5File::close()` explicitly, which returns the Result.
            if let Err(e) = self.finalize(true) {
                eprintln!(
                    "rust-hdf5: failed to finalize HDF5 file on drop: {e}. \
                     The file may be incomplete or corrupt; call \
                     H5File::close() to handle this error explicitly."
                );
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::format::messages::datatype::DatatypeMessage;
    use crate::io::reader::Hdf5Reader;

    fn fixture(name: &str) -> std::path::PathBuf {
        std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
            .join("tests/fixtures")
            .join(name)
    }

    /// Copy a fixture so a test that appends does not edit the checked-in file.
    fn fixture_copy(name: &str, tag: &str) -> std::path::PathBuf {
        let path = temp_path(tag);
        std::fs::copy(fixture(name), &path).unwrap();
        path
    }

    fn temp_path(tag: &str) -> std::path::PathBuf {
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        std::env::temp_dir().join(format!(
            "rust_hdf5_w_{}_{}_{}.h5",
            std::process::id(),
            tag,
            n
        ))
    }

    /// A group past the link phase change keeps its links in a fractal heap
    /// with a v2 B-tree name index. The reopen that rewrites that group's
    /// header lays a fresh pair out, so both blocks the old header named must
    /// come back to the allocator — every block of the heap, and the index
    /// header with its nodes.
    ///
    /// Asserted on the free list rather than on the file size: a reopen does
    /// not yet carry dense links forward, so the rewritten group's links (and
    /// the datasets they name) are dropped, and the file size that follows
    /// says more about that than about this.
    #[test]
    fn a_reopen_frees_the_dense_link_storage_its_rewrite_supersedes() {
        let path = temp_path("dense_link_reclaim");

        let writer = Hdf5Writer::create(&path).unwrap();
        writer.create_group("/", "run").unwrap();
        for i in 0..12 {
            writer
                .create_dataset(&format!("run/d{i:02}"), DatatypeMessage::i32_type(), &[2])
                .unwrap();
        }
        writer.close().unwrap();

        let writer = Hdf5Writer::open_append(&path).unwrap();
        let gidx = (0..writer.group_count())
            .find(|&g| writer.grp(g).lock().name == "/run")
            .expect("the reopen registered the group");
        let linfo = writer
            .superseded_dense
            .lock()
            .as_ref()
            .and_then(|s| s.links.get(&LinkScope::Group(gidx)).cloned())
            .expect("the reopen recorded the group's dense link storage");
        assert_ne!(linfo.fractal_heap_address, UNDEF_ADDR);
        assert_ne!(linfo.name_btree_address, UNDEF_ADDR);

        writer
            .release_superseded_dense_links(LinkScope::Group(gidx))
            .unwrap();
        let freed = writer.allocator.free_blocks();
        let covers = |addr: u64| {
            freed
                .iter()
                .any(|&(a, len)| addr >= a && addr < a.saturating_add(len))
        };
        assert!(covers(linfo.fractal_heap_address), "heap header: {freed:?}");
        assert!(covers(linfo.name_btree_address), "name index: {freed:?}");

        // And exactly once: the entry is gone, so the finalize that follows
        // cannot hand the same blocks back a second time.
        assert!(writer
            .superseded_dense
            .lock()
            .as_ref()
            .is_none_or(|s| s.links.is_empty()));
        writer
            .release_superseded_dense_links(LinkScope::Group(gidx))
            .unwrap();
        assert_eq!(writer.allocator.free_blocks(), freed);

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// The rewrite frees what it supersedes even when the replacement is not
    /// dense at all. An attribute set that drops back under `max_compact`
    /// goes into the object header, so nothing names the old heap any more —
    /// and a free driven by "the new set needs dense storage" would never
    /// reach this one.
    #[test]
    fn a_rewrite_that_drops_out_of_dense_storage_still_frees_it() {
        let path = temp_path("dense_attr_to_compact");
        let numeric = |name: &str| {
            AttributeMessage::scalar_numeric(
                name,
                DatatypeMessage::i32_type(),
                7i32.to_le_bytes().to_vec(),
            )
        };

        let writer = Hdf5Writer::create(&path).unwrap();
        for i in 0..12 {
            writer
                .add_root_attribute(numeric(&format!("a{i:02}")))
                .unwrap();
        }
        writer.close().unwrap();

        let writer = Hdf5Writer::open_append(&path).unwrap();
        let ainfo = writer
            .superseded_dense
            .lock()
            .as_ref()
            .and_then(|s| s.attrs.get(&AttrScope::Root).cloned())
            .expect("the reopen recorded the root's dense attribute storage");
        for i in 0..10 {
            writer
                .evict_attr(AttrTarget::Root, &format!("a{i:02}"))
                .unwrap();
        }
        assert!(!writer.attributes_need_dense(&writer.root_attributes.lock(), ObjectFormat::Modern));

        writer.prepare_dense_attributes(&[]).unwrap();
        let freed = writer.allocator.free_blocks();
        let covers = |addr: u64| {
            freed
                .iter()
                .any(|&(a, len)| addr >= a && addr < a.saturating_add(len))
        };
        assert!(covers(ainfo.fractal_heap_address), "heap header: {freed:?}");
        assert!(covers(ainfo.name_btree_address), "name index: {freed:?}");
        assert!(writer
            .superseded_dense
            .lock()
            .as_ref()
            .is_none_or(|s| s.attrs.is_empty()));

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// Deleting a reopened object supersedes its dense storage as surely as
    /// rewriting one does: nothing in the finalized file names the heap, so
    /// the delete owner frees it through the same entry.
    #[test]
    fn deleting_a_reopened_group_frees_its_dense_attribute_storage() {
        let path = temp_path("dense_attr_delete");
        let numeric = |name: &str| {
            AttributeMessage::scalar_numeric(
                name,
                DatatypeMessage::i32_type(),
                7i32.to_le_bytes().to_vec(),
            )
        };

        let writer = Hdf5Writer::create(&path).unwrap();
        writer.create_group("/", "run").unwrap();
        for i in 0..12 {
            writer
                .set_attribute(AttrTarget::Group("/run"), numeric(&format!("a{i:02}")))
                .unwrap();
        }
        writer.close().unwrap();

        let writer = Hdf5Writer::open_append(&path).unwrap();
        let gidx = (0..writer.group_count())
            .find(|&g| writer.grp(g).lock().name == "/run")
            .expect("the reopen registered the group");
        let ainfo = writer
            .superseded_dense
            .lock()
            .as_ref()
            .and_then(|s| s.attrs.get(&AttrScope::Group(gidx)).cloned())
            .expect("the reopen recorded the group's dense attribute storage");

        writer.delete_group("/run").unwrap();
        let freed = writer.allocator.free_blocks();
        let covers = |addr: u64| {
            freed
                .iter()
                .any(|&(a, len)| addr >= a && addr < a.saturating_add(len))
        };
        assert!(covers(ainfo.fractal_heap_address), "heap header: {freed:?}");
        assert!(covers(ainfo.name_btree_address), "name index: {freed:?}");
        assert!(writer
            .superseded_dense
            .lock()
            .as_ref()
            .is_none_or(|s| s.attrs.is_empty()));

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// The charset rule is one owner shared by every vlen string writer:
    /// appends into an ASCII-declared dataset reject non-ASCII strings the
    /// same way the slice writer does, and a dataset whose elements are not
    /// vlen references at all is refused instead of overwritten with them.
    #[test]
    fn append_vlen_strings_checks_the_datatype_and_charset() {
        let path = temp_path("append_vlen_charset");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_appendable_vlen_string_dataset("d", 4, None)
            .unwrap();
        writer.ds(idx).lock().datatype = DatatypeMessage::vlen_string_ascii();
        let err = writer
            .append_vlen_strings(idx, &["ok", "안녕"])
            .unwrap_err();
        assert!(
            err.to_string().contains("is not ASCII"),
            "unexpected error: {err}"
        );
        writer.append_vlen_strings(idx, &["ok", "fine"]).unwrap();

        let nums = writer
            .create_chunked_dataset("n", DatatypeMessage::i32_type(), &[0], &[u64::MAX], &[4])
            .unwrap();
        let err = writer.append_vlen_strings(nums, &["x"]).unwrap_err();
        assert!(
            err.to_string()
                .contains("only for variable-length string datasets"),
            "unexpected error: {err}"
        );

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// `create_chunked_dataset` builds an extensible-array index unconditionally
    /// (the caller — the high-level dataset API — is the one that decides when
    /// two-or-more unlimited dimensions should go to a v2 B-tree instead), so
    /// its own guard is the last line of defense against a shape that index
    /// can't represent at all.
    #[test]
    fn create_chunked_dataset_rejects_two_unlimited_dimensions() {
        let path = temp_path("earray_two_unlimited");
        let writer = Hdf5Writer::create(&path).unwrap();
        let err = writer
            .create_chunked_dataset(
                "d",
                DatatypeMessage::i32_type(),
                &[4, 4],
                &[u64::MAX, u64::MAX],
                &[2, 2],
            )
            .unwrap_err();
        assert!(err.to_string().contains("at most one unlimited"), "{err}");
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// Every creator must enter through `begin_create`; the four that used
    /// to bypass it could push a second dataset under an existing name and
    /// emit an invalid file with two same-named links.
    #[test]
    fn every_creator_rejects_an_existing_dataset_name() {
        let path = temp_path("create_gate");

        let writer = Hdf5Writer::create(&path).unwrap();
        writer
            .create_dataset("d", DatatypeMessage::i32_type(), &[2])
            .unwrap();

        let attempts: [(&str, IoResult<usize>); 4] = [
            (
                "vlen_string",
                writer.create_vlen_string_dataset("d", &["x"], 1),
            ),
            ("vlen_bytes", writer.create_vlen_bytes_dataset("d", &[b"x"])),
            (
                "vlen_string_compressed",
                writer.create_vlen_string_dataset_compressed(
                    "d",
                    &["x"],
                    1,
                    FilterPipeline::deflate(6),
                ),
            ),
            (
                "chunked_with_pipeline",
                writer.create_chunked_dataset_with_pipeline(
                    "d",
                    DatatypeMessage::i32_type(),
                    &[0],
                    &[u64::MAX],
                    &[4],
                    FilterPipeline::deflate(6),
                ),
            ),
        ];
        for (which, res) in attempts {
            match res {
                Ok(_) => panic!("{which} accepted a duplicate name"),
                Err(e) => assert!(
                    e.to_string().contains("already exists"),
                    "{which}: unexpected error: {e}"
                ),
            }
        }

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// Every creator and every kind of name meet at `ensure_name_free`.
    ///
    /// The gate's whole value is that it is one list: a creator must be
    /// blind neither to a name kind it does not itself make nor to one added
    /// after it. This crosses the two — six names, one of each kind the
    /// writer can put in a group, against every creator — so a creator that
    /// grows its own check, or a name kind that stops being on the list,
    /// fails here rather than in a file holding two links of one name.
    #[test]
    fn every_creator_refuses_every_kind_of_taken_name() {
        let path = temp_path("create_gate_matrix");
        let writer = Hdf5Writer::create(&path).unwrap();

        let i32t = || DatatypeMessage::i32_type();
        writer.create_dataset("d", i32t(), &[2]).unwrap();
        writer.create_compact_dataset("c", i32t(), &[2]).unwrap();
        writer.create_group("/", "g").unwrap();
        writer.commit_datatype("t", i32t()).unwrap();
        writer.create_hard_link("/", "h", "d").unwrap();
        writer
            .create_symbolic_link(
                "/",
                "s",
                LinkTarget::Soft {
                    target: "/d".into(),
                },
            )
            .unwrap();
        writer
            .create_symbolic_link(
                "/",
                "e",
                LinkTarget::External {
                    file: "other.h5".into(),
                    path: "/x".into(),
                },
            )
            .unwrap();

        for taken in ["d", "c", "g", "t", "h", "s", "e"] {
            let attempts: [(&str, IoResult<()>); 8] = [
                (
                    "dataset",
                    writer.create_dataset(taken, i32t(), &[2]).map(|_| ()),
                ),
                (
                    "compact",
                    writer
                        .create_compact_dataset(taken, i32t(), &[2])
                        .map(|_| ()),
                ),
                (
                    "chunked",
                    writer
                        .create_chunked_dataset(taken, i32t(), &[0], &[u64::MAX], &[4])
                        .map(|_| ()),
                ),
                (
                    "vlen_string",
                    writer
                        .create_vlen_string_dataset(taken, &["x"], 1)
                        .map(|_| ()),
                ),
                (
                    "committed datatype",
                    writer.commit_datatype(taken, i32t()).map(|_| ()),
                ),
                ("group", writer.create_group("/", taken).map(|_| ())),
                ("hard link", writer.create_hard_link("/", taken, "d")),
                (
                    "soft link",
                    writer.create_symbolic_link(
                        "/",
                        taken,
                        LinkTarget::Soft {
                            target: "/d".into(),
                        },
                    ),
                ),
            ];
            for (which, res) in attempts {
                match res {
                    Ok(()) => panic!("{which} accepted the taken name '{taken}'"),
                    Err(e) => assert!(
                        e.to_string().contains("already exists"),
                        "{which} on '{taken}': unexpected error: {e}"
                    ),
                }
            }
        }

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// The `H5T_VLEN` length field counts base elements, so an image that is
    /// not a whole number of them has no length that reads back as what was
    /// handed over; it is refused at the call rather than stored truncated.
    #[test]
    fn vlen_sequence_refuses_a_partial_element() {
        let path = temp_path("vlen_partial_element");

        let writer = Hdf5Writer::create(&path).unwrap();
        let err = writer
            .create_vlen_sequence_dataset("d", DatatypeMessage::i32_type(), &[&[1u8, 2, 3, 4, 5]])
            .unwrap_err()
            .to_string();
        assert!(err.contains("5 bytes"), "unexpected error: {err}");
        assert!(err.contains("4-byte elements"), "unexpected error: {err}");

        // The refusal is the length rule alone: the same base takes a whole
        // number of elements, and an empty sequence is a legal one.
        writer
            .create_vlen_sequence_dataset(
                "d",
                DatatypeMessage::i32_type(),
                &[&[1u8, 2, 3, 4], &[][..]],
            )
            .unwrap();

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// A corrupt file can declare a zero-length chunk dimension; the
    /// superseded-reference read must reject it the way `write_slice` does,
    /// not divide by it.
    #[test]
    fn vlen_slice_rejects_a_zero_chunk_dimension() {
        let path = temp_path("vlen_slice_zero_chunk");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_appendable_vlen_string_dataset("d", 2, None)
            .unwrap();
        writer.append_vlen_strings(idx, &["a", "b"]).unwrap();
        writer.ds(idx).lock().chunked.as_mut().unwrap().chunk_dims[0] = 0;
        let err = writer.write_vlen_strings_slice(idx, 0, &["x"]).unwrap_err();
        assert!(
            err.to_string().contains("zero-length dimension"),
            "unexpected error: {err}"
        );

        writer.ds(idx).lock().chunked.as_mut().unwrap().chunk_dims[0] = 2;
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// A libhdf5-written collection can be 100% full — no free-space marker,
    /// content exactly the declared size. When a stale reference names an
    /// index that is not there, nothing is removed, and the collection must
    /// be left alone: re-encoding it at its declared size cannot fit the
    /// free-space marker and would fail the whole update.
    #[test]
    fn release_leaves_a_full_collection_it_removed_nothing_from() {
        use crate::format::global_heap::encode_vlen_reference;

        let path = temp_path("release_full_gcol");
        let writer = Hdf5Writer::create(&path).unwrap();

        // Hand-built full collection: 16-byte header + one 16+8-byte object,
        // declared size exactly 40, no free-space marker.
        let mut img = Vec::new();
        img.extend_from_slice(b"GCOL");
        img.push(1);
        img.extend_from_slice(&[0u8; 3]);
        img.extend_from_slice(&40u64.to_le_bytes());
        img.extend_from_slice(&1u16.to_le_bytes()); // object index 1
        img.extend_from_slice(&1u16.to_le_bytes()); // ref_count
        img.extend_from_slice(&0u32.to_le_bytes()); // reserved
        img.extend_from_slice(&8u64.to_le_bytes()); // data size
        img.extend_from_slice(b"deadbeef");
        assert_eq!(img.len(), 40);
        let addr = writer
            .allocator
            .allocate(img.len() as u64, FreeSpaceClass::RawData);
        writer.handle.write_at(addr, &img).unwrap();

        // The superseded reference names index 2, which the collection does
        // not hold — a no-op removal.
        let refs = encode_vlen_reference(3, addr, 2, &writer.ctx);
        writer.release_vlen_references(&refs).unwrap();
        assert_eq!(writer.handle.read_at(addr, 40).unwrap(), img);

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// The CWFS second pass (`H5F_cwfs_find_free_heap`): an object too big
    /// for the listed collection's remaining free space extends the
    /// collection in place — the file allocation grows off the end of the
    /// file (`H5MF_try_extend`) and the collection's declared size and
    /// free-space marker grow with it (`H5HG_extend`) — instead of opening
    /// a second collection.
    #[test]
    fn an_oversized_vlen_insert_extends_the_listed_collection() {
        use crate::format::global_heap::GlobalHeapCollection;

        let path = temp_path("cwfs_extend_tail");
        let writer = Hdf5Writer::create(&path).unwrap();
        // A small object opens a minimum-size (4096) listed collection —
        // the file's last allocation, so the extension grows the file end.
        let p1 = writer.insert_vlen_objects(&[b"hello".as_slice()]).unwrap();
        let big = vec![0x41u8; 5000]; // more than the ~4 KiB remaining
        let p2 = writer.insert_vlen_objects(&[big.as_slice()]).unwrap();
        assert_eq!(
            p2[0].0, p1[0].0,
            "the big object opened a second collection"
        );

        // The block on disk is one grown collection holding both objects.
        let img = writer.handle.read_at_most(p1[0].0, 65536).unwrap();
        let (gcol, csize) = GlobalHeapCollection::decode(&img, &writer.ctx).unwrap();
        assert!(csize > 4096, "declared size did not grow: {csize}");
        assert_eq!(gcol.objects.len(), 2);
        assert_eq!(gcol.objects[1].data, big);

        writer.close().unwrap();
        let bytes = std::fs::read(&path).unwrap();
        assert_eq!(
            bytes.windows(4).filter(|w| *w == b"GCOL").count(),
            1,
            "a second collection signature is in the file"
        );
        std::fs::remove_file(&path).ok();
    }

    /// The non-tail counterpart: the collection is pinned away from the end
    /// of the file, but a released block starts right after it, so the
    /// extension consumes the front of that block (`H5MF_try_extend`'s
    /// free-section path) and the remainder stays reusable.
    #[test]
    fn extension_consumes_a_freed_block_after_the_collection() {
        use crate::format::global_heap::GlobalHeapCollection;

        let path = temp_path("cwfs_extend_freed");
        let writer = Hdf5Writer::create(&path).unwrap();
        let p1 = writer.insert_vlen_objects(&[b"hello".as_slice()]).unwrap();
        let addr = p1[0].0;
        // Land a block right after the collection, pin the file end past
        // it, then release it: extension must use the released space.
        let spacer = writer.allocator.allocate(8192, FreeSpaceClass::RawData);
        assert_eq!(spacer, addr + 4096, "spacer not adjacent; layout changed");
        writer.allocator.allocate(8, FreeSpaceClass::RawData);
        writer.allocator.free(spacer, 8192, FreeSpaceClass::RawData);

        let big = vec![0x42u8; 5000];
        let p2 = writer.insert_vlen_objects(&[big.as_slice()]).unwrap();
        assert_eq!(p2[0].0, addr, "the big object opened a second collection");

        let img = writer.handle.read_at_most(addr, 65536).unwrap();
        let (gcol, csize) = GlobalHeapCollection::decode(&img, &writer.ctx).unwrap();
        assert_eq!(csize, 8192, "grew by max(size, shortfall) = 4096");
        assert_eq!(gcol.objects.len(), 2);

        // The remainder of the released block is still allocatable.
        assert_eq!(
            writer.allocator.allocate(4096, FreeSpaceClass::RawData),
            addr + 8192,
            "the freed block's tail was lost"
        );
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// Issue #10: a reopen-and-replace loop on a vlen string must not grow
    /// the file. The superseded heap objects are freed *before* the
    /// replacement is allocated, so each session reuses the block it just
    /// released even though the free list starts empty on reopen. The old
    /// free-after-alloc order failed this by one collection per session.
    #[test]
    fn vlen_replace_across_reopen_keeps_the_file_flat() {
        let path = temp_path("vlen_reopen_flat");
        let payload_a = "a".repeat(64 * 1024);
        let payload_b = "b".repeat(64 * 1024);

        let writer = Hdf5Writer::create(&path).unwrap();
        writer
            .create_vlen_string_dataset("notes", &["initial"], 1)
            .unwrap();
        writer.close().unwrap();

        let mut sizes = Vec::new();
        for i in 0..8 {
            let writer = Hdf5Writer::open_append(&path).unwrap();
            let payload = if i % 2 == 0 { &payload_a } else { &payload_b };
            writer
                .write_vlen_strings_slice(0, 0, &[payload.as_str()])
                .unwrap();
            writer.close().unwrap();
            sizes.push(std::fs::metadata(&path).unwrap().len());
        }
        // The first replacement grows the file once (the initial collection
        // cannot hold 64 KiB); every later equal-size replacement must land
        // in the block its own session just freed.
        assert_eq!(&sizes[1..], &vec![sizes[0]; 7][..], "sizes: {sizes:?}");

        // The reused blocks still form a valid file holding the last value.
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(
            reader.read_vlen_strings("notes").unwrap(),
            vec![payload_b.clone()]
        );

        std::fs::remove_file(&path).ok();
    }

    /// Replacing a vlen string attribute must release the superseded
    /// global-heap collection *before* the replacement's collection is
    /// allocated, so a reopen-replace loop lands each new value in the block
    /// it just freed instead of growing the file by one collection per
    /// session — the attribute counterpart of
    /// [`vlen_replace_across_reopen_keeps_the_file_flat`].
    #[test]
    fn vlen_attr_replace_across_reopen_keeps_the_file_flat() {
        let path = temp_path("vlen_attr_reopen_flat");
        let payload_a = "a".repeat(8 * 1024);
        let payload_b = "b".repeat(8 * 1024);

        let writer = Hdf5Writer::create(&path).unwrap();
        writer
            .set_vlen_string_attribute(AttrTarget::Root, "note", &payload_a)
            .unwrap();
        writer.close().unwrap();

        let mut sizes = Vec::new();
        for i in 0..8 {
            let writer = Hdf5Writer::open_append(&path).unwrap();
            let payload = if i % 2 == 0 { &payload_b } else { &payload_a };
            writer
                .set_vlen_string_attribute(AttrTarget::Root, "note", payload)
                .unwrap();
            writer.close().unwrap();
            sizes.push(std::fs::metadata(&path).unwrap().len());
        }
        assert_eq!(&sizes[1..], &vec![sizes[0]; 7][..], "sizes: {sizes:?}");

        // The reused blocks still hold the last value.
        let reader = Hdf5Reader::open(&path).unwrap();
        let attr = reader.root_attr("note").unwrap().clone();
        let mut reader = reader;
        assert_eq!(reader.attr_string_value(&attr).unwrap(), payload_a);

        std::fs::remove_file(&path).ok();
    }

    /// A numeric attribute replacing a vlen one goes through the same list
    /// owner, so the superseded collection is released even though the new
    /// value holds no heap reference: a later same-size vlen attribute must
    /// land in the freed block, making the file exactly as large as one that
    /// never stored the replaced value.
    #[test]
    fn numeric_replacing_a_vlen_attr_releases_its_collection() {
        let payload = "x".repeat(8 * 1024);
        let numeric = || {
            AttributeMessage::scalar_numeric(
                "x",
                DatatypeMessage::i32_type(),
                7i32.to_le_bytes().to_vec(),
            )
        };

        let path_a = temp_path("vlen_attr_cross_a");
        let writer = Hdf5Writer::create(&path_a).unwrap();
        writer
            .set_vlen_string_attribute(AttrTarget::Root, "x", &payload)
            .unwrap();
        writer.add_root_attribute(numeric()).unwrap();
        writer
            .set_vlen_string_attribute(AttrTarget::Root, "y", &payload)
            .unwrap();
        writer.close().unwrap();

        // The same end state written without the replaced vlen value.
        let path_b = temp_path("vlen_attr_cross_b");
        let writer = Hdf5Writer::create(&path_b).unwrap();
        writer.add_root_attribute(numeric()).unwrap();
        writer
            .set_vlen_string_attribute(AttrTarget::Root, "y", &payload)
            .unwrap();
        writer.close().unwrap();

        assert_eq!(
            std::fs::metadata(&path_a).unwrap().len(),
            std::fs::metadata(&path_b).unwrap().len()
        );

        let reader = Hdf5Reader::open(&path_a).unwrap();
        let y = reader.root_attr("y").unwrap().clone();
        let mut reader = reader;
        assert_eq!(reader.attr_string_value(&y).unwrap(), payload);

        std::fs::remove_file(&path_a).ok();
        std::fs::remove_file(&path_b).ok();
    }

    /// Reopen/write/close cycles must not leak the object-header blocks
    /// finalize rewrites: the reopened root header, the reopened group
    /// header, and the modified chunked dataset's header are each freed
    /// before their replacements are allocated. The chunk rewrite itself is
    /// in place (unfiltered chunks never move), so a leak of any header
    /// block shows up as monotonic growth here.
    #[test]
    fn reopen_cycles_reuse_superseded_header_blocks() {
        let path = temp_path("header_reuse");
        {
            let writer = Hdf5Writer::create(&path).unwrap();
            writer.create_group("/", "g").unwrap();
            let idx = writer
                .create_chunked_dataset(
                    "g/data",
                    DatatypeMessage::i32_type(),
                    &[4],
                    &[u64::MAX],
                    &[4],
                )
                .unwrap();
            let seed: Vec<u8> = [1i32, 2, 3, 4]
                .iter()
                .flat_map(|v| v.to_le_bytes())
                .collect();
            writer.write_chunk(idx, 0, &seed).unwrap();
            writer.close().unwrap();
        }

        let mut sizes = Vec::new();
        for i in 0..6i32 {
            let writer = Hdf5Writer::open_append(&path).unwrap();
            let data: Vec<u8> = [i; 4].iter().flat_map(|v| v.to_le_bytes()).collect();
            writer.write_chunk(0, 0, &data).unwrap();
            writer.close().unwrap();
            sizes.push(std::fs::metadata(&path).unwrap().len());
        }
        assert_eq!(&sizes[1..], &vec![sizes[0]; 5][..], "sizes: {sizes:?}");

        // The reused header blocks still form a valid file.
        let mut reader = Hdf5Reader::open(&path).unwrap();
        let raw = reader.read_dataset_raw("g/data").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values, vec![5, 5, 5, 5]);

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn create_empty_file() {
        let path = temp_path("empty");

        let writer = Hdf5Writer::create(&path).unwrap();
        writer.close().unwrap();

        // Verify we can read it back
        let reader = Hdf5Reader::open(&path).unwrap();
        assert!(reader.dataset_names().is_empty());

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn create_single_dataset() {
        let path = temp_path("single");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_dataset("data", DatatypeMessage::f64_type(), &[4])
            .unwrap();
        let values: Vec<f64> = vec![1.0, 2.0, 3.0, 4.0];
        let raw: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();
        writer.write_dataset_raw(idx, &raw).unwrap();
        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_names(), vec!["data"]);
        assert_eq!(reader.dataset_shape("data").unwrap(), vec![4]);
        let readback = reader.read_dataset_raw("data").unwrap();
        assert_eq!(readback, raw);

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn create_multiple_datasets() {
        let path = temp_path("multi");

        let writer = Hdf5Writer::create(&path).unwrap();

        let idx0 = writer
            .create_dataset("ints", DatatypeMessage::i32_type(), &[3])
            .unwrap();
        let i_data: Vec<u8> = [10i32, 20, 30]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_dataset_raw(idx0, &i_data).unwrap();

        let idx1 = writer
            .create_dataset("floats", DatatypeMessage::f32_type(), &[2, 2])
            .unwrap();
        let f_data: Vec<u8> = [1.0f32, 2.0, 3.0, 4.0]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_dataset_raw(idx1, &f_data).unwrap();

        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        let names = reader.dataset_names();
        assert!(names.contains(&"ints"));
        assert!(names.contains(&"floats"));
        assert_eq!(reader.dataset_shape("ints").unwrap(), vec![3]);
        assert_eq!(reader.dataset_shape("floats").unwrap(), vec![2, 2]);
        assert_eq!(reader.read_dataset_raw("ints").unwrap(), i_data);
        assert_eq!(reader.read_dataset_raw("floats").unwrap(), f_data);

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn data_size_mismatch() {
        let path = temp_path("mismatch");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_dataset("x", DatatypeMessage::u8_type(), &[4])
            .unwrap();
        let err = writer.write_dataset_raw(idx, &[1, 2, 3]); // 3 bytes instead of 4
        assert!(err.is_err());

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn create_chunked_dataset_simple() {
        let path = temp_path("chunked_simple");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_chunked_dataset(
                "data",
                DatatypeMessage::f64_type(),
                &[0, 4],        // start empty
                &[u64::MAX, 4], // unlimited first dim
                &[1, 4],        // chunk = [1, 4]
            )
            .unwrap();

        // Write 3 frames (chunks)
        for frame in 0..3u64 {
            let values: Vec<f64> = (0..4).map(|i| (frame * 4 + i) as f64).collect();
            let raw: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();
            writer.write_chunk(idx, frame, &raw).unwrap();
        }

        // Extend dimensions
        writer.extend_dataset(idx, &[3, 4]).unwrap();

        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_names(), vec!["data"]);
        assert_eq!(reader.dataset_shape("data").unwrap(), vec![3, 4]);

        let raw = reader.read_dataset_raw("data").unwrap();
        let values: Vec<f64> = raw
            .chunks(8)
            .map(|chunk| f64::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 12);
        for (i, val) in values.iter().enumerate() {
            assert_eq!(*val, i as f64);
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn chunked_dataset_many_frames() {
        let path = temp_path("chunked_many");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_chunked_dataset(
                "frames",
                DatatypeMessage::i32_type(),
                &[0, 2],
                &[u64::MAX, 2],
                &[1, 2],
            )
            .unwrap();

        let n_frames = 10u64;
        for frame in 0..n_frames {
            let values = [(frame * 2) as i32, (frame * 2 + 1) as i32];
            let raw: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();
            writer.write_chunk(idx, frame, &raw).unwrap();
        }

        writer.extend_dataset(idx, &[n_frames, 2]).unwrap();
        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("frames").unwrap(), vec![10, 2]);

        let raw = reader.read_dataset_raw("frames").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|chunk| i32::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 20);
        for (i, val) in values.iter().enumerate() {
            assert_eq!(*val, i as i32);
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn create_fixed_array_dataset_roundtrip() {
        let path = temp_path("fixed_array");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_fixed_array_dataset(
                "grid",
                DatatypeMessage::i32_type(),
                &[4, 6], // 4x6 grid
                &[2, 3], // chunk = 2x3
            )
            .unwrap();

        // Write all chunks: 2x2 = 4 chunks
        // chunk (0,0): rows 0-1, cols 0-2
        let c00: Vec<u8> = [0i32, 1, 2, 6, 7, 8]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[0, 0], &c00).unwrap();

        // chunk (0,1): rows 0-1, cols 3-5
        let c01: Vec<u8> = [3i32, 4, 5, 9, 10, 11]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[0, 1], &c01).unwrap();

        // chunk (1,0): rows 2-3, cols 0-2
        let c10: Vec<u8> = [12i32, 13, 14, 18, 19, 20]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[1, 0], &c10).unwrap();

        // chunk (1,1): rows 2-3, cols 3-5
        let c11: Vec<u8> = [15i32, 16, 17, 21, 22, 23]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[1, 1], &c11).unwrap();

        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_names(), vec!["grid"]);
        assert_eq!(reader.dataset_shape("grid").unwrap(), vec![4, 6]);

        let raw = reader.read_dataset_raw("grid").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|chunk| i32::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 24);
        for (i, val) in values.iter().enumerate() {
            assert_eq!(*val, i as i32);
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn fixed_array_paged_dblk_disk_size() {
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        // 1024 elements per page (bits=10). 3000 chunks => 3 pages.
        let hdr = FixedArrayHeader::new_for_chunks(&ctx, 3000);
        assert!(hdr.is_paged());
        assert_eq!(hdr.npages(), 3);
        // prefix: 4+1+1+8 + bitmap(1) + cksum(4) = 19
        // elements: 3000 * 8 = 24000 ; per-page cksum: 3 * 4 = 12
        assert_eq!(fixed_array_dblk_disk_size(&ctx, &hdr), 19 + 24000 + 12);

        // Non-paged: 1000 elements. prefix(14) + 1000*8 + cksum(4).
        let small = FixedArrayHeader::new_for_chunks(&ctx, 1000);
        assert!(!small.is_paged());
        assert_eq!(fixed_array_dblk_disk_size(&ctx, &small), 14 + 8000 + 4);
    }

    #[test]
    fn fixed_array_paged_encode_matches_reader_layout() {
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        let mut hdr = FixedArrayHeader::new_for_chunks(&ctx, 2500);
        hdr.data_blk_addr = 0x9000;
        let npages = hdr.npages() as usize; // ceil(2500/1024) = 3

        let mut dblk = FixedArrayDataBlock::new_unfiltered(0x1000, 2500);
        for (i, e) in dblk.elements.iter_mut().enumerate() {
            *e = 0x10000 + (i as u64) * 0x100;
        }

        let encoded = encode_fixed_array_dblk(&ctx, &hdr, &dblk);
        assert_eq!(encoded.len() as u64, fixed_array_dblk_disk_size(&ctx, &hdr));

        // Decode the prefix and pages exactly as the reader does.
        let prefix = FixedArrayPagedPrefix::decode(&encoded, &ctx, npages as u64).unwrap();
        assert_eq!(prefix.header_addr, 0x1000);
        for p in 0..npages {
            assert!(prefix.page_initialized(p), "page {p} should be initialized");
        }

        let dblk_page_nelmts = hdr.dblk_page_nelmts() as usize;
        let page_stride = dblk_page_nelmts * 8 + 4;
        let mut recovered = Vec::new();
        for p in 0..npages {
            let page_nelmts = if p + 1 == npages {
                2500 - p * dblk_page_nelmts
            } else {
                dblk_page_nelmts
            };
            let off = prefix.prefix_size + p * page_stride;
            let page_buf = &encoded[off..];
            let addrs = crate::format::chunk_index::fixed_array::decode_unfiltered_page(
                page_buf,
                &ctx,
                page_nelmts,
            )
            .unwrap();
            recovered.extend(addrs);
        }
        assert_eq!(recovered, dblk.elements);
    }

    #[test]
    fn fixed_array_paged_decode_roundtrip_with_uninitialized_page() {
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        let hdr = FixedArrayHeader::new_for_chunks(&ctx, 2500);
        let npages = hdr.npages() as usize; // 3
        let page = hdr.dblk_page_nelmts() as usize; // 1024

        // Populate pages 0 and 2; leave page 1 entirely undefined so its
        // bitmap bit stays clear on encode.
        let mut dblk = FixedArrayDataBlock::new_unfiltered(0x1000, 2500);
        for i in (0..page).chain(2 * page..2500) {
            dblk.elements[i] = 0x10000 + (i as u64) * 0x100;
        }

        let mut encoded = encode_fixed_array_dblk(&ctx, &hdr, &dblk);
        let prefix = FixedArrayPagedPrefix::decode(&encoded, &ctx, npages as u64).unwrap();
        assert!(prefix.page_initialized(0));
        assert!(!prefix.page_initialized(1));
        assert!(prefix.page_initialized(2));

        // Corrupt the uninitialized page's bytes the way libhdf5 leaves
        // them: arbitrary, no valid checksum. Decode must not look at it.
        let page_stride = page * 8 + 4;
        let p1 = prefix.prefix_size + page_stride;
        for b in &mut encoded[p1..p1 + page_stride] {
            *b = 0x5A;
        }

        let decoded = decode_fixed_array_dblk(&ctx, &hdr, &encoded, 0).unwrap();
        assert_eq!(decoded.elements, dblk.elements);
        assert_eq!(decoded.header_addr, 0x1000);
    }

    #[test]
    fn fixed_array_paged_decode_filtered_roundtrip() {
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        let chunk_size_len = 4usize;
        let hdr = FixedArrayHeader::new_for_filtered_chunks(&ctx, 1500, chunk_size_len as u8);
        assert!(hdr.is_paged());

        let mut dblk = FixedArrayDataBlock::new_filtered(0x2000, 1500);
        for (i, e) in dblk.filtered_elements.iter_mut().enumerate() {
            e.address = 0x8000 + (i as u64) * 0x40;
            e.chunk_size = 100 + i as u64;
            e.filter_mask = (i % 3) as u32;
        }

        let encoded = encode_fixed_array_dblk(&ctx, &hdr, &dblk);
        assert_eq!(encoded.len() as u64, fixed_array_dblk_disk_size(&ctx, &hdr));
        let decoded = decode_fixed_array_dblk(&ctx, &hdr, &encoded, chunk_size_len).unwrap();
        assert_eq!(decoded.filtered_elements, dblk.filtered_elements);
        assert_eq!(decoded.client_id, FA_CLIENT_FILT_CHUNK);
    }

    #[test]
    fn create_fixed_array_paged_dataset_roundtrip() {
        let path = temp_path("fixed_array_paged");

        // 1D dataset of 3000 elements, chunk size 1 => 3000 chunks.
        // 3000 > 1024 (one page) => the FA data block must be paged.
        let n: usize = 3000;
        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_fixed_array_dataset("paged", DatatypeMessage::i32_type(), &[n as u64], &[1])
            .unwrap();

        for i in 0..n {
            let v = (i as i32).to_le_bytes();
            writer
                .write_chunk_fixed_array(idx, &[i as u64], &v)
                .unwrap();
        }
        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("paged").unwrap(), vec![n as u64]);
        let raw = reader.read_dataset_raw("paged").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), n);
        for (i, v) in values.iter().enumerate() {
            assert_eq!(*v, i as i32, "element {i}");
        }

        std::fs::remove_file(&path).ok();
    }

    #[cfg(feature = "deflate")]
    #[test]
    fn create_filtered_fixed_array_dataset_roundtrip() {
        // Small compressed fixed-shape chunked dataset: flat filtered FA.
        let path = temp_path("fixed_array_filt");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_fixed_array_dataset_with_pipeline(
                "grid",
                DatatypeMessage::i32_type(),
                &[4, 6], // 4x6 grid
                &[2, 3], // chunk = 2x3 => 2x2 = 4 chunks
                FilterPipeline::deflate(6),
            )
            .unwrap();

        let c00: Vec<u8> = [0i32, 1, 2, 6, 7, 8]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[0, 0], &c00).unwrap();
        let c01: Vec<u8> = [3i32, 4, 5, 9, 10, 11]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[0, 1], &c01).unwrap();
        let c10: Vec<u8> = [12i32, 13, 14, 18, 19, 20]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[1, 0], &c10).unwrap();
        let c11: Vec<u8> = [15i32, 16, 17, 21, 22, 23]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_fixed_array(idx, &[1, 1], &c11).unwrap();

        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("grid").unwrap(), vec![4, 6]);
        let raw = reader.read_dataset_raw("grid").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 24);
        for (i, v) in values.iter().enumerate() {
            assert_eq!(*v, i as i32, "element {i}");
        }

        std::fs::remove_file(&path).ok();
    }

    #[cfg(feature = "deflate")]
    #[test]
    fn create_filtered_fixed_array_paged_dataset_roundtrip() {
        // Large compressed fixed-shape chunked dataset (>1024 chunks): the
        // filtered FA data block must be paged.
        let path = temp_path("fixed_array_filt_paged");

        let n: usize = 3000;
        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_fixed_array_dataset_with_pipeline(
                "paged",
                DatatypeMessage::i32_type(),
                &[n as u64],
                &[1],
                FilterPipeline::deflate(6),
            )
            .unwrap();

        for i in 0..n {
            let v = (i as i32).to_le_bytes();
            writer
                .write_chunk_fixed_array(idx, &[i as u64], &v)
                .unwrap();
        }
        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("paged").unwrap(), vec![n as u64]);
        let raw = reader.read_dataset_raw("paged").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), n);
        for (i, v) in values.iter().enumerate() {
            assert_eq!(*v, i as i32, "element {i}");
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn filtered_fixed_array_dblk_disk_size_and_encode() {
        // Cross-check filtered FA data-block sizing against the encoded length,
        // for both flat and paged layouts.
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        let csl = 3u8; // chunk_size_len
        let elem_size = 8 + csl as usize + 4; // addr + size + filter_mask

        // Flat: 100 chunks. prefix(14) + 100*elem_size + cksum(4).
        let mut flat = FixedArrayHeader::new_for_filtered_chunks(&ctx, 100, csl);
        flat.data_blk_addr = 0x4000;
        assert!(!flat.is_paged());
        assert_eq!(
            fixed_array_dblk_disk_size(&ctx, &flat),
            (14 + 100 * elem_size + 4) as u64
        );
        let flat_dblk = FixedArrayDataBlock::new_filtered(0x1000, 100);
        assert_eq!(
            encode_fixed_array_dblk(&ctx, &flat, &flat_dblk).len() as u64,
            fixed_array_dblk_disk_size(&ctx, &flat)
        );

        // Paged: 2500 chunks => 3 pages. prefix(4+1+1+8+1+4=19)
        // + 2500*elem_size + 3*cksum(4).
        let mut paged = FixedArrayHeader::new_for_filtered_chunks(&ctx, 2500, csl);
        paged.data_blk_addr = 0x9000;
        assert!(paged.is_paged());
        assert_eq!(paged.npages(), 3);
        assert_eq!(
            fixed_array_dblk_disk_size(&ctx, &paged),
            (19 + 2500 * elem_size + 12) as u64
        );
        let mut paged_dblk = FixedArrayDataBlock::new_filtered(0x1000, 2500);
        for (i, e) in paged_dblk.filtered_elements.iter_mut().enumerate() {
            e.address = 0x10000 + (i as u64) * 0x100;
            e.chunk_size = (i % 200) as u64;
        }
        let encoded = encode_fixed_array_dblk(&ctx, &paged, &paged_dblk);
        assert_eq!(
            encoded.len() as u64,
            fixed_array_dblk_disk_size(&ctx, &paged)
        );

        // Decode the paged prefix + pages as the reader does.
        let npages = paged.npages() as usize;
        let prefix = FixedArrayPagedPrefix::decode(&encoded, &ctx, npages as u64).unwrap();
        for p in 0..npages {
            assert!(prefix.page_initialized(p), "page {p}");
        }
        let dblk_page_nelmts = paged.dblk_page_nelmts() as usize;
        let page_stride = dblk_page_nelmts * elem_size + 4;
        let mut recovered = Vec::new();
        for p in 0..npages {
            let page_nelmts = if p + 1 == npages {
                2500 - p * dblk_page_nelmts
            } else {
                dblk_page_nelmts
            };
            let off = prefix.prefix_size + p * page_stride;
            let elems = crate::format::chunk_index::fixed_array::decode_filtered_page(
                &encoded[off..],
                &ctx,
                page_nelmts,
                csl as usize,
            )
            .unwrap();
            recovered.extend(elems);
        }
        assert_eq!(recovered, paged_dblk.filtered_elements);
    }

    #[test]
    fn create_btree_v2_dataset_roundtrip() {
        let path = temp_path("btree_v2");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_btree_v2_dataset(
                "data",
                DatatypeMessage::f64_type(),
                &[0, 0],               // start empty
                &[u64::MAX, u64::MAX], // both dims unlimited
                &[2, 3],               // chunk = 2x3
            )
            .unwrap();

        // Write chunks for a 4x6 dataset
        // chunk (0,0)
        let c00: Vec<u8> = [0.0f64, 1.0, 2.0, 6.0, 7.0, 8.0]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_btree_v2(idx, &[0, 0], &c00).unwrap();

        // chunk (0,1)
        let c01: Vec<u8> = [3.0f64, 4.0, 5.0, 9.0, 10.0, 11.0]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_btree_v2(idx, &[0, 1], &c01).unwrap();

        // chunk (1,0)
        let c10: Vec<u8> = [12.0f64, 13.0, 14.0, 18.0, 19.0, 20.0]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_btree_v2(idx, &[1, 0], &c10).unwrap();

        // chunk (1,1)
        let c11: Vec<u8> = [15.0f64, 16.0, 17.0, 21.0, 22.0, 23.0]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_chunk_btree_v2(idx, &[1, 1], &c11).unwrap();

        writer.extend_dataset(idx, &[4, 6]).unwrap();
        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_names(), vec!["data"]);
        assert_eq!(reader.dataset_shape("data").unwrap(), vec![4, 6]);

        let raw = reader.read_dataset_raw("data").unwrap();
        let values: Vec<f64> = raw
            .chunks(8)
            .map(|chunk| f64::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 24);
        for (i, val) in values.iter().enumerate() {
            assert_eq!(*val, i as f64);
        }

        std::fs::remove_file(&path).ok();
    }

    /// Bytes one chunk of [`btree_v2_flush_probe`]'s dataset occupies — an
    /// f64 element, so the allocator's alignment neither pads nor merges it and
    /// the file's growth is exactly the bytes asked for.
    const BT2_PROBE_CHUNK: u64 = 8;

    /// Write chunks of a 1x1-chunked 2-D BT2 dataset, flushing at each batch
    /// boundary, and report `(node addresses, file length)` after every flush.
    /// Chunks are addressed down column 0 so the record count — and hence the
    /// tree's shape — grows one record at a time.
    fn btree_v2_flush_probe(path: &std::path::Path, batches: &[u64]) -> Vec<(Vec<u64>, u64)> {
        let writer = Hdf5Writer::create(path).unwrap();
        let idx = writer
            .create_btree_v2_dataset(
                "data",
                DatatypeMessage::f64_type(),
                &[0, 0],
                &[u64::MAX, u64::MAX],
                &[1, 1],
            )
            .unwrap();
        let mut written = 0u64;
        let mut out = Vec::new();
        for &upto in batches {
            while written < upto {
                writer
                    .write_chunk_btree_v2(idx, &[written, 0], &(written as f64).to_le_bytes())
                    .unwrap();
                written += 1;
            }
            writer.flush_dataset(idx).unwrap();
            let addrs = writer
                .ds(idx)
                .lock()
                .btree_v2
                .as_ref()
                .unwrap()
                .node_addrs
                .clone();
            out.push((addrs, std::fs::metadata(path).unwrap().len()));
        }
        writer.extend_dataset(idx, &[written.max(1), 1]).unwrap();
        writer.close().unwrap();
        out
    }

    /// The node pool tracks the tree in both directions. Dropping records is
    /// what a removal path would do — [`Bt2ChunkIndex`] has none today, so the
    /// test drops them itself — and the flush that follows must hand the blocks
    /// its smaller tree no longer needs back to the allocator instead of
    /// leaving them recorded and unreachable.
    #[test]
    fn a_btree_v2_flush_frees_the_node_blocks_its_tree_gave_up() {
        use crate::format::chunk_index::btree_v2::BT2_NODE_SIZE;

        let path = temp_path("bt2_node_shrink");
        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_btree_v2_dataset(
                "data",
                DatatypeMessage::f64_type(),
                &[0, 0],
                &[u64::MAX, u64::MAX],
                &[1, 1],
            )
            .unwrap();
        // 85 records is one past a leaf, so the tree is two leaves and a root.
        for i in 0..85u64 {
            writer
                .write_chunk_btree_v2(idx, &[i, 0], &(i as f64).to_le_bytes())
                .unwrap();
        }
        writer.flush_dataset(idx).unwrap();
        let grown = writer
            .ds(idx)
            .lock()
            .btree_v2
            .as_ref()
            .unwrap()
            .node_addrs
            .clone();
        assert_eq!(grown.len(), 3, "expected two leaves and a root");

        // Back to 84 records: one leaf, so two of the three blocks are surplus.
        writer
            .ds(idx)
            .lock()
            .btree_v2
            .as_mut()
            .unwrap()
            .index
            .records
            .truncate(84);
        writer.flush_dataset(idx).unwrap();
        let shrunk = writer
            .ds(idx)
            .lock()
            .btree_v2
            .as_ref()
            .unwrap()
            .node_addrs
            .clone();
        assert_eq!(
            shrunk,
            grown[..1],
            "the pool still records the surplus blocks"
        );

        // The surplus went back to the allocator, not on the floor: the next
        // node-sized allocation lands inside the region the two blocks covered.
        let reused = writer
            .allocator
            .allocate(BT2_NODE_SIZE as u64, FreeSpaceClass::Metadata);
        assert!(
            (grown[1]..grown[1] + 2 * BT2_NODE_SIZE as u64).contains(&reused),
            "a node block allocated at {reused:#x}, outside the freed \
             [{:#x}, {:#x}) the flush gave up",
            grown[1],
            grown[1] + 2 * BT2_NODE_SIZE as u64
        );

        writer.extend_dataset(idx, &[85, 1]).unwrap();
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// A v2 B-tree whose header declares a non-default node size — libhdf5
    /// built with a different `H5D_BT2_NODE_SIZE`, or any other writer —
    /// reopens for append: the reconstruction adopts the header's node_size,
    /// split and merge instead of refusing everything but 2048, and the next
    /// flush re-serializes at that size (upstream allocates every node at
    /// `hdr->node_size`, H5B2leaf.c / H5B2internal.c).
    #[test]
    fn a_btree_v2_with_a_foreign_node_size_reopens_and_grows() {
        let path = temp_path("bt2_foreign_node_size");
        {
            let writer = Hdf5Writer::create(&path).unwrap();
            let idx = writer
                .create_btree_v2_dataset(
                    "data",
                    DatatypeMessage::f64_type(),
                    &[0, 0],
                    &[u64::MAX, u64::MAX],
                    &[1, 1],
                )
                .unwrap();
            // Act as a foreign writer: 512-byte nodes, non-default tuning.
            // record_size 24 => a 512-byte leaf holds 20 records, so 85
            // records make a depth-1 tree of 512-byte blocks.
            {
                let ds = writer.ds(idx);
                let mut m = ds.lock();
                let index = &mut m.btree_v2.as_mut().unwrap().index;
                index.node_size = 512;
                index.split_percent = 90;
                index.merge_percent = 30;
            }
            for i in 0..85u64 {
                writer
                    .write_chunk_btree_v2(idx, &[i, 0], &(i as f64).to_le_bytes())
                    .unwrap();
            }
            writer.extend_dataset(idx, &[85, 1]).unwrap();
            writer.close().unwrap();
        }
        {
            let writer = Hdf5Writer::open_append(&path).unwrap();
            let idx = writer.dataset_index("data").unwrap();
            {
                let ds = writer.ds(idx);
                let m = ds.lock();
                let index = &m.btree_v2.as_ref().unwrap().index;
                assert_eq!(index.node_size, 512, "header node_size not adopted");
                assert_eq!(index.split_percent, 90);
                assert_eq!(index.merge_percent, 30);
                assert_eq!(index.records.len(), 85, "records not walked back");
            }
            for i in 85..115u64 {
                writer
                    .write_chunk_btree_v2(idx, &[i, 0], &(i as f64).to_le_bytes())
                    .unwrap();
            }
            writer.extend_dataset(idx, &[115, 1]).unwrap();
            writer.close().unwrap();
        }

        let mut reader = Hdf5Reader::open(&path).unwrap();
        let raw = reader.read_dataset_raw("data").unwrap();
        let values: Vec<f64> = raw
            .chunks(8)
            .map(|c| f64::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 115);
        for (i, v) in values.iter().enumerate() {
            assert_eq!(*v, i as f64, "element {i}");
        }
        std::fs::remove_file(&path).ok();
    }

    /// A node's record count falls as well as rises: the tree's first leaf goes
    /// from a full 84 records to 42 when 85 records force it to split. The node
    /// image is padded to the whole block so re-serializing overwrites the
    /// block, not a prefix of it — otherwise that leaf keeps the tail of its
    /// 84-record self, stale records sitting in a live node block.
    #[test]
    fn a_shrinking_btree_v2_node_leaves_no_stale_records_behind() {
        use crate::format::chunk_index::btree_v2::{Bt2ChunkIndex, BT2_NODE_SIZE};

        let path = temp_path("bt2_node_blocks");
        let probe = btree_v2_flush_probe(&path, &[84, 85]);
        let node0 = probe.last().unwrap().0[0];

        // What the first leaf holds once the tree has split.
        let ctx = FormatContext {
            sizeof_addr: 8,
            sizeof_size: 8,
        };
        let mut index = Bt2ChunkIndex::new_unfiltered(2);
        for i in 0..85u64 {
            index.insert(vec![i, 0], 0);
        }
        let tree = index.build_tree(&ctx);
        assert!(
            tree.nodes[0].num_records < 84,
            "this test needs the first leaf to shrink, got {}",
            tree.nodes[0].num_records
        );
        // signature(4) + version(1) + type(1) + records + checksum(4)
        let used = 10 + tree.nodes[0].num_records as usize * tree.record_size as usize;

        let bytes = std::fs::read(&path).unwrap();
        let block = &bytes[node0 as usize..node0 as usize + BT2_NODE_SIZE as usize];
        assert!(
            block[used..].iter().all(|&b| b == 0),
            "leaf block at {node0:#x} still holds {} bytes of its previous, larger image",
            block[used..].iter().rposition(|&b| b != 0).unwrap_or(0) + 1
        );
        std::fs::remove_file(&path).ok();
    }

    /// The node pool is the single owner of the tree's block addresses: a flush
    /// reuses every block already in it and allocates only the shortfall. So
    /// re-flushing an unchanged index must cost nothing, and a flush that grows
    /// the tree must cost exactly the blocks it added — anything more means a
    /// block was stranded.
    #[test]
    fn a_btree_v2_flush_allocates_only_the_node_blocks_it_adds() {
        use crate::format::chunk_index::btree_v2::BT2_NODE_SIZE;

        let path = temp_path("bt2_pool_growth");
        // Re-flush at 84 (still one leaf), then cross into a three-node depth-1
        // tree, then keep growing.
        let batches = [84u64, 84, 85, 200, 200];
        let probe = btree_v2_flush_probe(&path, &batches);
        for i in 1..probe.len() {
            let (prev_addrs, prev_len) = &probe[i - 1];
            let (addrs, len) = &probe[i];
            assert!(
                addrs.starts_with(prev_addrs),
                "flush {i} moved a node block instead of reusing it"
            );
            let new_blocks = (addrs.len() - prev_addrs.len()) as u64 * BT2_NODE_SIZE as u64;
            let new_chunks = (batches[i] - batches[i - 1]) * BT2_PROBE_CHUNK;
            assert_eq!(
                len - prev_len,
                new_blocks + new_chunks,
                "flush {i} grew the file by more than the blocks it added"
            );
        }
        // The unchanged re-flushes must be free.
        assert_eq!(probe[1].1, probe[0].1);
        assert_eq!(probe[4].1, probe[3].1);
        std::fs::remove_file(&path).ok();
    }

    #[cfg(feature = "parallel")]
    #[test]
    fn parallel_batch_write_roundtrip() {
        let path = temp_path("parallel_batch");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_chunked_dataset(
                "data",
                DatatypeMessage::i32_type(),
                &[0, 4],
                &[u64::MAX, 4],
                &[1, 4],
            )
            .unwrap();

        // Prepare chunks
        let chunks_data: Vec<(u64, Vec<u8>)> = (0..8u64)
            .map(|frame| {
                let values: Vec<i32> = (0..4).map(|i| (frame * 4 + i) as i32).collect();
                let raw: Vec<u8> = values.iter().flat_map(|v| v.to_le_bytes()).collect();
                (frame, raw)
            })
            .collect();

        let batch: Vec<(u64, &[u8])> = chunks_data
            .iter()
            .map(|(idx, data)| (*idx, data.as_slice()))
            .collect();

        writer.write_chunks_batch(idx, &batch).unwrap();
        writer.extend_dataset(idx, &[8, 4]).unwrap();
        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("data").unwrap(), vec![8, 4]);
        let raw = reader.read_dataset_raw("data").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|chunk| i32::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 32);
        for (i, val) in values.iter().enumerate() {
            assert_eq!(*val, i as i32);
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn swmr_writer_append_frames() {
        use crate::io::swmr::SwmrWriter;

        // Per-call unique path so concurrent cargo invocations and
        // kernel-side flock release races cannot collide.
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_append_{}_{}.h5",
            std::process::id(),
            n
        ));

        let mut swmr = SwmrWriter::create(&path).unwrap();
        let idx = swmr
            .create_streaming_dataset("detector", DatatypeMessage::u16_type(), &[4, 4])
            .unwrap();

        swmr.start_swmr().unwrap();

        // Append 5 frames
        for frame in 0..5u16 {
            let data: Vec<u16> = (0..16).map(|i| frame * 16 + i).collect();
            let raw: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
            swmr.append_frame(idx, &raw).unwrap();
        }

        swmr.flush().unwrap();
        swmr.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("detector").unwrap(), vec![5, 4, 4]);

        let raw = reader.read_dataset_raw("detector").unwrap();
        let values: Vec<u16> = raw
            .chunks(2)
            .map(|chunk| u16::from_le_bytes(chunk.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 80); // 5 * 4 * 4
                                      // Verify first frame
        for (i, val) in values.iter().enumerate().take(16) {
            assert_eq!(*val, i as u16);
        }
        // Verify last frame
        for (i, val) in values[64..80].iter().enumerate() {
            assert_eq!(*val, 4 * 16 + i as u16);
        }

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn swmr_writer_tiled_frames() {
        use crate::io::swmr::SwmrWriter;
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_tiled_{}_{}.h5",
            std::process::id(),
            n
        ));

        let mut swmr = SwmrWriter::create(&path).unwrap();
        // 4x4 frames, tiled into 2x2 chunks -> 4 chunks per frame.
        let idx = swmr
            .create_streaming_dataset_tiled("det", DatatypeMessage::u16_type(), &[4, 4], &[2, 2])
            .unwrap();
        swmr.start_swmr().unwrap();

        for frame in 0..3u16 {
            let data: Vec<u16> = (0..16).map(|i| frame * 100 + i).collect();
            let raw: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
            swmr.append_frame(idx, &raw).unwrap();
        }
        swmr.flush().unwrap();
        swmr.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("det").unwrap(), vec![3, 4, 4]);
        let raw = reader.read_dataset_raw("det").unwrap();
        let values: Vec<u16> = raw
            .chunks(2)
            .map(|c| u16::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 48);
        // Every element must survive the frame -> tile split and the
        // tile -> frame reassembly on read.
        for frame in 0..3u16 {
            for i in 0..16usize {
                assert_eq!(values[frame as usize * 16 + i], frame * 100 + i as u16);
            }
        }
        std::fs::remove_file(&path).ok();
    }

    /// A chunk tile larger than the frame is geometry libhdf5 refuses to
    /// create (`H5D__chunk_construct`: chunk must not exceed a fixed maximum
    /// dimension), so no libhdf5-based writer — including the NDFileHDF5
    /// tiling controls this API mirrors — can produce such a file. Until
    /// 0.4.1 we accepted it and zero-padded the frame up to the tile; now
    /// the create is rejected like every other creator's.
    #[test]
    fn swmr_writer_tiled_chunk_larger_than_frame_is_rejected() {
        use crate::io::swmr::SwmrWriter;
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_bigchunk_{}_{}.h5",
            std::process::id(),
            n
        ));

        let mut swmr = SwmrWriter::create(&path).unwrap();
        let err = swmr
            .create_streaming_dataset_tiled("det", DatatypeMessage::u16_type(), &[3, 3], &[8, 8])
            .unwrap_err();
        assert!(
            err.to_string().contains("maximum dimension size"),
            "unexpected error: {err}"
        );
        swmr.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn swmr_writer_multi_frame_chunks() {
        use crate::io::swmr::SwmrWriter;
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_mfc_{}_{}.h5",
            std::process::id(),
            n
        ));

        // 3x3 frames, chunk = 4 frames x full frame. 10 frames -> 3 bands
        // of 4, 4, 2 (the last band partial).
        let mut swmr = SwmrWriter::create(&path).unwrap();
        let idx = swmr
            .create_streaming_dataset_chunked(
                "det",
                DatatypeMessage::u16_type(),
                &[3, 3],
                &[4, 3, 3],
            )
            .unwrap();
        swmr.start_swmr().unwrap();
        for frame in 0..10u16 {
            let data: Vec<u16> = (0..9).map(|i| frame * 100 + i).collect();
            let raw: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
            swmr.append_frame(idx, &raw).unwrap();
        }
        swmr.flush().unwrap();
        swmr.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        // The partial last band must not over-extend the frame count.
        assert_eq!(reader.dataset_shape("det").unwrap(), vec![10, 3, 3]);
        let raw = reader.read_dataset_raw("det").unwrap();
        let values: Vec<u16> = raw
            .chunks(2)
            .map(|c| u16::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 90);
        for frame in 0..10u16 {
            for i in 0..9usize {
                assert_eq!(values[frame as usize * 9 + i], frame * 100 + i as u16);
            }
        }
        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn swmr_writer_multi_frame_tiled_chunks() {
        use crate::io::swmr::SwmrWriter;
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_mftc_{}_{}.h5",
            std::process::id(),
            n
        ));

        // 4x4 frames, chunk = 2 frames x 2x2 tiles. 5 frames -> bands of
        // 2, 2, 1; every frame is also split into a 2x2 tile grid.
        let mut swmr = SwmrWriter::create(&path).unwrap();
        let idx = swmr
            .create_streaming_dataset_chunked(
                "det",
                DatatypeMessage::u16_type(),
                &[4, 4],
                &[2, 2, 2],
            )
            .unwrap();
        swmr.start_swmr().unwrap();
        for frame in 0..5u16 {
            let data: Vec<u16> = (0..16).map(|i| frame * 100 + i).collect();
            let raw: Vec<u8> = data.iter().flat_map(|v| v.to_le_bytes()).collect();
            swmr.append_frame(idx, &raw).unwrap();
        }
        swmr.flush().unwrap();
        swmr.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("det").unwrap(), vec![5, 4, 4]);
        let raw = reader.read_dataset_raw("det").unwrap();
        let values: Vec<u16> = raw
            .chunks(2)
            .map(|c| u16::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values.len(), 80);
        for frame in 0..5u16 {
            for i in 0..16usize {
                assert_eq!(values[frame as usize * 16 + i], frame * 100 + i as u16);
            }
        }
        std::fs::remove_file(&path).ok();
    }

    #[cfg(feature = "deflate")]
    #[test]
    fn swmr_writer_compressed_frames() {
        use crate::io::swmr::SwmrWriter;
        use std::sync::atomic::{AtomicU64, Ordering};
        static COUNTER: AtomicU64 = AtomicU64::new(0);
        let n = COUNTER.fetch_add(1, Ordering::Relaxed);
        let path = std::env::temp_dir().join(format!(
            "rust_hdf5_swmr_comp_{}_{}.h5",
            std::process::id(),
            n
        ));

        let mut swmr = SwmrWriter::create(&path).unwrap();
        let pipeline = crate::format::messages::filter::FilterPipeline::deflate(4);
        let idx = swmr
            .create_streaming_dataset_compressed(
                "detector",
                DatatypeMessage::i32_type(),
                &[8],
                pipeline,
            )
            .unwrap();
        swmr.start_swmr().unwrap();

        for frame in 0..40i32 {
            let raw: Vec<u8> = (0..8).flat_map(|i| (frame * 8 + i).to_le_bytes()).collect();
            swmr.append_frame(idx, &raw).unwrap();
            if frame % 7 == 0 {
                swmr.flush().unwrap();
            }
        }
        swmr.flush().unwrap();
        swmr.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("detector").unwrap(), vec![40, 8]);
        let raw = reader.read_dataset_raw("detector").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values, (0..320).collect::<Vec<i32>>());

        std::fs::remove_file(&path).ok();
    }

    #[test]
    fn group_hierarchy_writer_reader() {
        let path = temp_path("group_hierarchy");

        let writer = Hdf5Writer::create(&path).unwrap();

        // Create groups
        let g0 = writer.create_group("/", "group1").unwrap();
        let g1 = writer.create_group("/group1", "sub").unwrap();
        assert_eq!(g0, 0);
        assert_eq!(g1, 1);

        // Create datasets
        let ds_root = writer
            .create_dataset("root_data", DatatypeMessage::f64_type(), &[2])
            .unwrap();
        let raw_root: Vec<u8> = [1.0f64, 2.0].iter().flat_map(|v| v.to_le_bytes()).collect();
        writer.write_dataset_raw(ds_root, &raw_root).unwrap();

        let ds_g0 = writer
            .create_dataset("group1/data", DatatypeMessage::i32_type(), &[3])
            .unwrap();
        let raw_g0: Vec<u8> = [10i32, 20, 30]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_dataset_raw(ds_g0, &raw_g0).unwrap();

        let ds_g1 = writer
            .create_dataset("group1/sub/values", DatatypeMessage::u8_type(), &[4])
            .unwrap();
        writer.write_dataset_raw(ds_g1, &[1u8, 2, 3, 4]).unwrap();

        writer.close().unwrap();

        // Read back
        let mut reader = Hdf5Reader::open(&path).unwrap();
        let names = reader.dataset_names();
        assert!(names.contains(&"root_data"), "names: {:?}", names);
        assert!(names.contains(&"group1/data"), "names: {:?}", names);
        assert!(names.contains(&"group1/sub/values"), "names: {:?}", names);

        let raw = reader.read_dataset_raw("root_data").unwrap();
        let vals: Vec<f64> = raw
            .chunks(8)
            .map(|c| f64::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(vals, vec![1.0, 2.0]);

        let raw = reader.read_dataset_raw("group1/data").unwrap();
        let vals: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(vals, vec![10, 20, 30]);

        let raw = reader.read_dataset_raw("group1/sub/values").unwrap();
        assert_eq!(raw, vec![1, 2, 3, 4]);

        std::fs::remove_file(&path).ok();
    }

    /// libhdf5 (`H5D__chunk_construct`) rejects a chunk dimension that
    /// exceeds a fixed maximum dimension. Before this check, such a dataset
    /// was created and appends landed rows at the chunk stride instead of
    /// the row stride, reading back [1, 2, 0, 0] for [1, 2, 3, 4].
    #[test]
    fn create_rejects_a_chunk_wider_than_a_fixed_max_dimension() {
        let path = temp_path("chunk_wider_than_max");

        let writer = Hdf5Writer::create(&path).unwrap();
        let err = writer
            .create_chunked_dataset(
                "data",
                DatatypeMessage::f64_type(),
                &[0, 2],
                &[u64::MAX, 2],
                &[2, 4],
            )
            .unwrap_err();
        assert!(
            err.to_string().contains("maximum dimension size"),
            "unexpected error: {err}"
        );

        // The fixed-array creators derive the maximum from the fixed dims.
        let err = writer
            .create_fixed_array_dataset("fa", DatatypeMessage::f64_type(), &[3], &[5])
            .unwrap_err();
        assert!(
            err.to_string().contains("maximum dimension size"),
            "unexpected error: {err}"
        );

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// libhdf5 exempts a dimension whose *current* size is zero from the
    /// chunk-vs-maximum check (`curr_dims[u] &&` in `H5D__chunk_construct`),
    /// and rejects a zero chunk dimension on every path.
    #[test]
    fn create_mirrors_the_libhdf5_chunk_geometry_exemptions() {
        let path = temp_path("chunk_geometry_exemptions");

        let writer = Hdf5Writer::create(&path).unwrap();
        // dims[1] == 0: chunk 4 > max 2 is allowed, as libhdf5 allows it.
        writer
            .create_chunked_dataset(
                "exempt",
                DatatypeMessage::f64_type(),
                &[0, 0],
                &[u64::MAX, 2],
                &[2, 4],
            )
            .unwrap();

        let err = writer
            .create_chunked_dataset("zero", DatatypeMessage::f64_type(), &[0], &[u64::MAX], &[0])
            .unwrap_err();
        assert!(
            err.to_string().contains("chunk dimension 0 is zero"),
            "unexpected error: {err}"
        );

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// A file written by 0.4.0 can carry a chunk row wider than the frame
    /// row — create now rejects that geometry, but reopened files keep it.
    /// Appends must scatter frames at the chunk stride, not pack them at
    /// the frame stride (which read back `[1, 2, 0, 0]` for `[1, 2, 3, 4]`).
    /// The wide shape is simulated by widening the registered chunk dims
    /// after create, which also lands in the layout message at close.
    #[test]
    fn append_scatters_into_a_legacy_wider_than_row_chunk() {
        let path = temp_path("legacy_wide_chunk_append");

        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_chunked_dataset(
                "data",
                DatatypeMessage::i32_type(),
                &[0, 2],
                &[u64::MAX, 2],
                &[2, 2],
            )
            .unwrap();
        writer.ds(idx).lock().chunked.as_mut().unwrap().chunk_dims = vec![2, 4];

        let frames: Vec<u8> = [1i32, 2, 3, 4]
            .iter()
            .flat_map(|v| v.to_le_bytes())
            .collect();
        writer.write_append_frames(idx, 0, 2, &frames).unwrap();
        writer.extend_dataset(idx, &[2, 2]).unwrap();
        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        assert_eq!(reader.dataset_shape("data").unwrap(), vec![2, 2]);
        let raw = reader.read_dataset_raw("data").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values, vec![1, 2, 3, 4]);
        std::fs::remove_file(&path).ok();
    }

    /// The compressed vlen creator sizes its chunked layout from a
    /// caller-supplied chunk size; it goes through the same geometry
    /// validation as every other creator (empty inputs are exempt because
    /// their current size is zero).
    #[test]
    #[cfg(feature = "deflate")]
    fn compressed_vlen_create_validates_its_chunk_size() {
        use crate::format::messages::filter::FilterPipeline;
        let path = temp_path("vlen_compressed_chunk");

        let writer = Hdf5Writer::create(&path).unwrap();
        let err = writer
            .create_vlen_string_dataset_compressed(
                "texts",
                &["a", "b", "c"],
                100,
                FilterPipeline::deflate(6),
            )
            .unwrap_err();
        assert!(
            err.to_string().contains("maximum dimension size"),
            "unexpected error: {err}"
        );

        writer
            .create_vlen_string_dataset_compressed("empty", &[], 16, FilterPipeline::deflate(6))
            .unwrap();

        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// `set_libver_latest` moves *filtered* chunked datasets to layout v5 with
    /// fixed 8-byte chunk-size fields; unfiltered chunked and pre-opt-in
    /// datasets keep v4 with the derived width, matching libhdf5's
    /// `version_perf` rule (only the filtered index arms bump to 5).
    #[cfg(feature = "deflate")]
    #[test]
    fn libver_latest_selects_v5_for_filtered_chunks_only() {
        let path = temp_path("libver_v5_select");

        let mut writer = Hdf5Writer::create(&path).unwrap();
        let before = writer
            .create_chunked_dataset_with_pipeline(
                "d4",
                DatatypeMessage::i32_type(),
                &[0],
                &[u64::MAX],
                &[16],
                FilterPipeline::deflate(4),
            )
            .unwrap();
        writer.set_libver_latest(true).unwrap();
        let ea5 = writer
            .create_chunked_dataset_with_pipeline(
                "ea5",
                DatatypeMessage::i32_type(),
                &[0],
                &[u64::MAX],
                &[16],
                FilterPipeline::deflate(4),
            )
            .unwrap();
        let plain = writer
            .create_chunked_dataset(
                "plain",
                DatatypeMessage::i32_type(),
                &[0],
                &[u64::MAX],
                &[16],
            )
            .unwrap();
        let fa5 = writer
            .create_fixed_array_dataset_with_pipeline(
                "fa5",
                DatatypeMessage::i32_type(),
                &[4, 6],
                &[2, 3],
                FilterPipeline::deflate(6),
            )
            .unwrap();
        let bt5 = writer
            .create_btree_v2_dataset_with_pipeline(
                "bt5",
                DatatypeMessage::i32_type(),
                &[0, 0],
                &[u64::MAX, u64::MAX],
                &[2, 3],
                FilterPipeline::deflate(6),
            )
            .unwrap();

        {
            let d4 = writer.ds(before);
            let d4 = d4.lock();
            assert_eq!(d4.layout_version, 4);
            assert_eq!(
                d4.chunked.as_ref().unwrap().chunk_size_len,
                compute_chunk_size_len(16 * 4)
            );
            let e5 = writer.ds(ea5);
            let e5 = e5.lock();
            assert_eq!(e5.layout_version, 5);
            assert_eq!(e5.chunked.as_ref().unwrap().chunk_size_len, 8);
            assert_eq!(writer.ds(plain).lock().layout_version, 4);
            assert_eq!(writer.ds(fa5).lock().layout_version, 5);
            assert_eq!(writer.ds(bt5).lock().layout_version, 5);
        }

        // Write through the FA and BT2 v5 indexes so their 8-byte chunk-size
        // fields are exercised end to end, not just selected.
        for (coords, vals) in [
            ([0u64, 0], [0i32, 1, 2, 6, 7, 8]),
            ([0, 1], [3, 4, 5, 9, 10, 11]),
            ([1, 0], [12, 13, 14, 18, 19, 20]),
            ([1, 1], [15, 16, 17, 21, 22, 23]),
        ] {
            let bytes: Vec<u8> = vals.iter().flat_map(|v| v.to_le_bytes()).collect();
            writer
                .write_chunk_fixed_array(fa5, &coords, &bytes)
                .unwrap();
            writer.write_chunk_btree_v2(bt5, &coords, &bytes).unwrap();
        }
        writer.extend_dataset(bt5, &[4, 6]).unwrap();
        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        for name in ["fa5", "bt5"] {
            let raw = reader.read_dataset_raw(name).unwrap();
            let values: Vec<i32> = raw
                .chunks(4)
                .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
                .collect();
            assert_eq!(values, (0..24).collect::<Vec<i32>>(), "dataset {name}");
        }

        std::fs::remove_file(&path).ok();
    }

    /// A v5 file reopened for append must stay v5: the decode → `DatasetInfo`
    /// → finalize path carries the version through, so the re-encoded layout
    /// message matches the 8-byte size fields the filtered index was built
    /// with. A silent v4 downgrade here would make libhdf5 derive a narrower
    /// field width than the index uses.
    #[cfg(feature = "deflate")]
    #[test]
    fn v5_layout_survives_reopen_and_append() {
        let path = temp_path("libver_v5_reopen");
        let chunk: usize = 8;

        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer.set_libver_latest(true).unwrap();
        let idx = writer
            .create_chunked_dataset_with_pipeline(
                "d",
                DatatypeMessage::i32_type(),
                &[0],
                &[u64::MAX],
                &[chunk as u64],
                FilterPipeline::deflate(4),
            )
            .unwrap();
        for c in 0..2u64 {
            let data: Vec<u8> = (0..chunk as i32)
                .flat_map(|i| (c as i32 * chunk as i32 + i).to_le_bytes())
                .collect();
            writer.write_chunk(idx, c, &data).unwrap();
        }
        writer.extend_dataset(idx, &[2 * chunk as u64]).unwrap();
        writer.close().unwrap();

        // Reopen: the decoded layout version must be preserved, and appends
        // must keep working against the 8-byte-size-field index.
        let writer = Hdf5Writer::open_append(&path).unwrap();
        assert_eq!(writer.ds(0).lock().layout_version, 5);
        for c in 2..4u64 {
            let data: Vec<u8> = (0..chunk as i32)
                .flat_map(|i| (c as i32 * chunk as i32 + i).to_le_bytes())
                .collect();
            writer.write_chunk(0, c, &data).unwrap();
        }
        writer.extend_dataset(0, &[4 * chunk as u64]).unwrap();
        writer.close().unwrap();

        // Still v5 after the second finalize, and fully readable.
        let writer = Hdf5Writer::open_append(&path).unwrap();
        assert_eq!(writer.ds(0).lock().layout_version, 5);
        writer.close().unwrap();

        let mut reader = Hdf5Reader::open(&path).unwrap();
        let raw = reader.read_dataset_raw("d").unwrap();
        let values: Vec<i32> = raw
            .chunks(4)
            .map(|c| i32::from_le_bytes(c.try_into().unwrap()))
            .collect();
        assert_eq!(values, (0..4 * chunk as i32).collect::<Vec<i32>>());

        std::fs::remove_file(&path).ok();
    }

    /// A chunk strictly larger than `u32::MAX` bytes forces layout v5 with no
    /// opt-in — v4's size field cannot represent it — while a chunk of exactly
    /// `u32::MAX` bytes stays v4, matching libhdf5's `version_req` boundary
    /// (`> 0xffffffff`, filtered or not).
    #[test]
    fn oversized_chunk_forces_v5_without_opt_in() {
        let path = temp_path("libver_4gib_force");

        let writer = Hdf5Writer::create(&path).unwrap();
        let at_limit = writer
            .create_chunked_dataset_with_pipeline(
                "at_limit",
                DatatypeMessage::u8_type(),
                &[0],
                &[u64::MAX],
                &[u32::MAX as u64],
                FilterPipeline::deflate(4),
            )
            .unwrap();
        let over = writer
            .create_chunked_dataset_with_pipeline(
                "over",
                DatatypeMessage::u8_type(),
                &[0],
                &[u64::MAX],
                &[u32::MAX as u64 + 1],
                FilterPipeline::deflate(4),
            )
            .unwrap();
        let over_unfiltered = writer
            .create_chunked_dataset(
                "over_plain",
                DatatypeMessage::u8_type(),
                &[0],
                &[u64::MAX],
                &[u32::MAX as u64 + 1],
            )
            .unwrap();

        assert_eq!(writer.ds(at_limit).lock().layout_version, 4);
        {
            let ds = writer.ds(over);
            let ds = ds.lock();
            assert_eq!(ds.layout_version, 5);
            assert_eq!(ds.chunked.as_ref().unwrap().chunk_size_len, 8);
        }
        assert_eq!(writer.ds(over_unfiltered).lock().layout_version, 5);
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// SWMR reaches version 3 on its own, without a chunked dataset to raise
    /// the bound — through the flags `finalize_for_swmr` passes, and then
    /// through `swmr_active` for every superblock written after it. Only a
    /// file with nothing else newer in it can tell the two arms apart, and
    /// the public SWMR API always creates a chunked streaming dataset.
    #[test]
    fn swmr_reaches_version_3_with_no_chunked_dataset_in_the_file() {
        let path = temp_path("swmr_superblock");

        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer
            .create_dataset("d", DatatypeMessage::i32_type(), &[2])
            .unwrap();
        assert_eq!(writer.superblock_version_for(0), SUPERBLOCK_V2);

        writer.finalize_for_swmr().unwrap();
        // What `start_swmr` does after finalizing, and what lets a second
        // handle read the file while this writer lives — the writer's
        // exclusive lock is mandatory on Windows.
        writer.handle().release_lock().unwrap();
        assert_eq!(std::fs::read(&path).unwrap()[8], SUPERBLOCK_V3);

        // The close-time finalize carries no SWMR flag; the file is still an
        // SWMR file and must not be handed back a version older than the one
        // its readers attached to.
        writer.close().unwrap();
        assert_eq!(std::fs::read(&path).unwrap()[8], SUPERBLOCK_V3);
        std::fs::remove_file(&path).ok();
    }

    /// A named bound below `H5F_LIBVER_V110` refuses the session instead —
    /// the two checks `H5F__start_swmr_write` opens with, a version-3
    /// superblock (H5Fint.c:3814) and a low bound of at least V110
    /// (H5Fint.c:3818). Naming no bound at all is what the test above does,
    /// and that file is free to become version 3.
    #[test]
    fn a_named_bound_below_v110_refuses_an_swmr_session() {
        for bound in [LibverBound::Earliest, LibverBound::V18] {
            let path = temp_path(&format!("swmr_refused_{bound:?}"));
            let mut writer = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    libver: Some(bound),
                    ..Default::default()
                },
            )
            .unwrap();
            writer
                .create_dataset("d", DatatypeMessage::i32_type(), &[2])
                .unwrap();

            let err = writer.finalize_for_swmr().unwrap_err().to_string();
            assert!(err.contains("SWMR"), "{bound:?}: {err}");
            assert!(err.contains("H5F_LIBVER_V110"), "{bound:?}: {err}");

            // Refused, not half-done: nothing was published, and the close
            // writes the file the bound asked for.
            writer.close().unwrap();
            let version = std::fs::read(&path).unwrap()[8];
            assert_eq!(version, bound.superblock_version(), "{bound:?}");
            std::fs::remove_file(&path).ok();
        }
    }

    /// A dataset header the SWMR publish could not fit into the chunk 0 it
    /// already had chains into a continuation block, and the in-place rewrite
    /// goes back over both: chunk 0 stays at the address the file's readers
    /// hold, and the continuation chunk at the one chunk 0 names.
    #[test]
    fn inplace_rewrite_goes_over_a_chained_header() {
        let path = temp_path("inplace_rewrite_chained");
        let writer = Hdf5Writer::create_with_options(
            &path,
            FileCreateOptions {
                libver: Some(LibverBound::V110),
                ..Default::default()
            },
        )
        .unwrap();
        writer
            .create_chunked_dataset("d", DatatypeMessage::i32_type(), &[0], &[u64::MAX], &[4])
            .unwrap();
        writer.close().unwrap();

        let mut writer = Hdf5Writer::open_append(&path).unwrap();
        let idx = 0;
        let published = writer.ds(idx).lock().obj_header_written_addr.unwrap();
        for i in 0..4 {
            writer
                .add_dataset_attribute(
                    idx,
                    AttributeMessage::array_numeric(
                        &format!("wide{i}"),
                        DatatypeMessage::f64_type(),
                        &[32],
                        vec![0u8; 256],
                    ),
                )
                .unwrap();
        }
        writer.finalize_for_swmr().unwrap();
        let blocks = writer.ds(idx).lock().obj_header_blocks.clone();
        assert_eq!(blocks.len(), 2, "chunk 0 and a continuation: {blocks:?}");
        assert_eq!(blocks[0].0, published, "chunk 0 stayed where it was");

        writer.write_dataset_header_inplace(idx).unwrap();
        assert_eq!(writer.ds(idx).lock().obj_header_blocks, blocks);
        writer.close().unwrap();

        // The closing finalize wrote over the same chunk 0, and the chained
        // header reads back whole.
        let writer = Hdf5Writer::open_append(&path).unwrap();
        assert_eq!(writer.ds(0).lock().obj_header_written_addr, Some(published));
        assert_eq!(writer.ds(0).lock().attributes.len(), 4);
        std::fs::remove_file(&path).ok();
    }

    /// `H5F__start_swmr_write` refuses a low bound below `H5F_LIBVER_V110`
    /// (H5Fint.c:3818) on a reopened file as on a created one, now that the
    /// superblock no longer raises it: an SWMR reader follows the v1.10 chunk
    /// indexes, which a lower bound's layout version cannot name. No bound
    /// named passes, the default's layout row being `V110`'s.
    #[test]
    fn swmr_on_a_reopened_file_refuses_a_named_bound_below_v110() {
        let path = temp_path("swmr_reopen_bound");
        let writer = Hdf5Writer::create_with_options(
            &path,
            FileCreateOptions {
                libver: Some(LibverBound::V110),
                ..Default::default()
            },
        )
        .unwrap();
        writer
            .create_dataset("d", DatatypeMessage::i32_type(), &[2])
            .unwrap();
        writer.close().unwrap();
        assert_eq!(std::fs::read(&path).unwrap()[8], SUPERBLOCK_V3);

        for bound in [LibverBound::Earliest, LibverBound::V18] {
            let mut writer = Hdf5Writer::open_append(&path).unwrap();
            writer.set_libver_bound(bound).unwrap();
            let err = writer.finalize_for_swmr().unwrap_err().to_string();
            assert!(err.contains("H5F_LIBVER_V110"), "{bound:?}: {err}");
            writer.close().unwrap();
        }
        let mut writer = Hdf5Writer::open_append(&path).unwrap();
        writer.finalize_for_swmr().unwrap();
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// After every writer of a dataset object header, `nlink_written` is the
    /// count that writer encoded.
    ///
    /// `header_stale_with` is the one authority for "does the on-disk header
    /// still describe this dataset?", and it reads `nlink_written`; the three
    /// writers — `finalize`, `finalize_for_swmr` and
    /// `write_dataset_header_inplace` — therefore all record through
    /// `DatasetInfo::header_written`. This walks the SWMR sequence, where the
    /// in-place writer is the one that could drift, and pins why it does not:
    /// a name added after the publish grows the header past the block it was
    /// published into, so the rewrite is refused rather than half-applied and
    /// the count on disk stays the one the registry names.
    #[test]
    fn every_dataset_header_write_records_its_link_count() {
        let path = temp_path("header_write_records_nlink");
        let writer = Hdf5Writer::create(&path).unwrap();
        let idx = writer
            .create_chunked_dataset("d", DatatypeMessage::i32_type(), &[0], &[u64::MAX], &[4])
            .unwrap();
        let mut writer = writer;
        writer.finalize_for_swmr().unwrap();
        assert_eq!(
            writer.ds(idx).lock().nlink_written,
            1,
            "the SWMR publish put one name in the header"
        );
        writer.write_dataset_header_inplace(idx).unwrap();
        assert_eq!(writer.ds(idx).lock().nlink_written, 1);

        // A second name after the publish: the reference-count message it
        // adds does not fit the published block.
        writer.create_hard_link("/", "alias", "d").unwrap();
        assert_eq!(writer.object_link_count(HardLinkTarget::Dataset(idx)), 2);
        let grew = writer
            .write_dataset_header_inplace(idx)
            .unwrap_err()
            .to_string();
        assert!(
            grew.contains("cannot rewrite in place"),
            "a header that outgrew its block must be refused: {grew}"
        );
        assert_eq!(
            writer.ds(idx).lock().nlink_written,
            1,
            "a refused rewrite leaves the registry describing the header the file holds"
        );

        // The close-time finalize is the writer that commits the second name,
        // and a reopen reads the same count back off the link graph.
        writer.close().unwrap();
        let writer = Hdf5Writer::open_append(&path).unwrap();
        assert_eq!(
            writer.ds(0).lock().nlink_written,
            2,
            "finalize wrote two names and the reopen reads two"
        );
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// `H5D__chunk_set_info`'s `version_req` (H5Dchunk.c:909, :936): version 5
    /// is required for a chunk over 4 GiB — the version-4 layout message's
    /// stored-size field is 32 bits and cannot record one — and
    /// `LAYOUT_VERSION_DEFAULT` (3, `H5O_LAYOUT_VERSION_DEFAULT`) is the floor
    /// for everything at or under that limit. Pure arithmetic on the byte
    /// count: no chunk is ever allocated.
    #[test]
    fn required_chunk_layout_version_pins_5_past_4_gib() {
        assert_eq!(
            Hdf5Writer::required_chunk_layout_version(u32::MAX as u64),
            LAYOUT_VERSION_DEFAULT
        );
        assert_eq!(
            Hdf5Writer::required_chunk_layout_version(u32::MAX as u64 + 1),
            5
        );
    }

    /// `H5D__chunk_set_info`'s index-selection gate (H5Dchunk.c:936): a chunk
    /// over 4 GiB reaches the v1.10 chunk indexes even under a bound whose
    /// `H5O_layout_ver_bounds` row (`LibverBound::layout_version`) is below
    /// 4 — `V18` (row 3) and `Earliest` (row 1) both normally keep an
    /// ordinary chunk on the version-1 B-tree, but
    /// `required_chunk_layout_version`'s own escape to 5 overrides that row
    /// for this one chunk. The default bound (`V110`, row 4) already crosses
    /// the threshold on its own, so it is asserted only as the baseline, not
    /// as a distinguishing case for the escape.
    #[test]
    fn uses_v110_chunk_indexing_escapes_past_4_gib_at_every_bound() {
        let over_4gib = u32::MAX as u64 + 1;
        let small = 1024u64;

        let path = temp_path("uses_v110_default");
        let writer = Hdf5Writer::create(&path).unwrap();
        assert!(writer.uses_v110_chunk_indexing(small));
        assert!(writer.uses_v110_chunk_indexing(over_4gib));
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();

        let path = temp_path("uses_v110_v18");
        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer.set_libver_bound(LibverBound::V18).unwrap();
        assert!(
            !writer.uses_v110_chunk_indexing(small),
            "V18's layout row (3) stays below the v1.10 gate for an ordinary chunk"
        );
        assert!(
            writer.uses_v110_chunk_indexing(over_4gib),
            "the >4 GiB escape reaches v1.10 indexing despite V18's row"
        );
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();

        let path = temp_path("uses_v110_earliest");
        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer.set_libver_bound(LibverBound::Earliest).unwrap();
        assert!(
            !writer.uses_v110_chunk_indexing(small),
            "Earliest's layout row (1) stays below the v1.10 gate for an ordinary chunk"
        );
        assert!(
            writer.uses_v110_chunk_indexing(over_4gib),
            "the >4 GiB escape reaches v1.10 indexing despite Earliest's row"
        );
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }

    /// `H5D__chunk_set_info`'s closing `MAX3` (H5Dchunk.c:1046): the same
    /// escape pins the layout message itself at version 5 for a chunk over
    /// 4 GiB regardless of bound — `required_chunk_layout_version` dominates
    /// the max chain ahead of both the bound-derived preference and
    /// `LAYOUT_VERSION_DEFAULT`.
    #[test]
    fn chunk_layout_version_pins_5_past_4_gib_at_every_bound() {
        let over_4gib = u32::MAX as u64 + 1;
        let small = 1024u64;

        let path = temp_path("chunk_ver_default");
        let writer = Hdf5Writer::create(&path).unwrap();
        assert_eq!(writer.chunk_layout_version(false, small), 4);
        assert_eq!(writer.chunk_layout_version(false, over_4gib), 5);
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();

        let path = temp_path("chunk_ver_v18");
        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer.set_libver_bound(LibverBound::V18).unwrap();
        assert_eq!(writer.chunk_layout_version(false, small), 3);
        assert_eq!(writer.chunk_layout_version(false, over_4gib), 5);
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();

        let path = temp_path("chunk_ver_earliest");
        let mut writer = Hdf5Writer::create(&path).unwrap();
        writer.set_libver_bound(LibverBound::Earliest).unwrap();
        assert_eq!(
            writer.chunk_layout_version(false, small),
            LAYOUT_VERSION_DEFAULT
        );
        assert_eq!(writer.chunk_layout_version(false, over_4gib), 5);
        writer.close().unwrap();
        std::fs::remove_file(&path).ok();
    }
    /// `fsm_persist.h5` persists two managers — metadata and raw data. The
    /// reopen reads both, hands their merged sections to the allocator, and
    /// claims the four blocks the managers themselves occupy.
    #[test]
    fn a_persisting_file_reopens_with_its_free_sections() {
        let path = fixture_copy("fsm_persist.h5", "fsm_read");
        let writer = Hdf5Writer::open_append(&path).unwrap();
        let fs = writer.free_space.as_deref().expect("managers were read");

        assert!(fs.info.persist);
        assert_eq!(fs.info.strategy, FileSpaceStrategy::FsmAggr);
        assert_eq!(fs.info.threshold, 1);

        let sections = writer.allocator.free_blocks();
        // h5stat -S reports 1910 bytes of tracked free space for this file.
        assert_eq!(sections.iter().map(|s| s.1).sum::<u64>(), 1910);
        // Address-ordered, and no two sections touch: what the two managers
        // held separately came out coalesced.
        for w in sections.windows(2) {
            assert!(w[0].0 + w[0].1 < w[1].0, "{sections:?}");
        }
        // Two headers plus the two sections blocks they name.
        assert_eq!(fs.superseded.len(), 4);
        for &(addr, len) in &fs.superseded {
            assert!(len > 0);
            assert!(
                !sections
                    .iter()
                    .any(|&(a, l)| addr < a + l && a < addr + len),
                "manager block {addr:#x}+{len} sits in a free section"
            );
        }
        drop(writer);
        let _ = std::fs::remove_file(&path);
    }

    /// A file created with non-default file-space properties carries the
    /// message that declares them, and one created to persist gets real
    /// managers as soon as anything is freed.
    #[test]
    fn a_created_file_declares_the_strategy_it_was_made_with() {
        let path = temp_path("fsm_create");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::FsmAggr, true, 1),
                    ..Default::default()
                },
            )
            .unwrap();
            let i = w
                .create_dataset("keep", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.write_dataset_raw(i, &[0u8; 32]).unwrap();
            w.close().unwrap();
        }

        let info = read_only_append(&path)
            .free_space
            .as_deref()
            .expect("the created file declares a strategy")
            .info
            .clone();
        assert_eq!(info.strategy, FileSpaceStrategy::FsmAggr);
        assert!(info.persist);
        assert_eq!(info.threshold, 1);
        assert_eq!(info.page_size, 4096);
        // The alignment fragments the creation left behind are the file's
        // first free space, so the metadata manager already has an address
        // and the raw-data one, which nothing freed into, does not.
        assert_ne!(info.fs_addr[0], UNDEF_ADDR);
        assert!(info.fs_addr.iter().skip(1).all(|&a| a == UNDEF_ADDR));

        // An append supersedes the root header and the extension, and that
        // freed space is what the managers now record.
        append_one(&path, "added", false);
        assert!(
            tracked_free_space(&path) > 0,
            "the append recorded no free space"
        );
        let _ = std::fs::remove_file(&path);
    }

    /// The two strategies without managers, and the default. All three are
    /// `H5Pset_file_space_strategy` settings; only the default leaves the file
    /// without the message.
    #[test]
    fn a_strategy_without_managers_still_declares_itself() {
        for (strategy, persist) in [
            (FileSpaceStrategy::Aggr, true),
            (FileSpaceStrategy::None, false),
        ] {
            let path = temp_path("fsm_nomgr");
            {
                let w = Hdf5Writer::create_with_options(
                    &path,
                    FileCreateOptions {
                        file_space: FileSpaceConfig::new(strategy, persist, 7),
                        ..Default::default()
                    },
                )
                .unwrap();
                w.create_dataset("d", DatatypeMessage::f64_type(), &[4])
                    .unwrap();
                w.close().unwrap();
            }
            // Read through the reader, not the writer: a reopen only builds
            // free-space state for a file it will rewrite managers for, and
            // these two have none.
            let info = declared_file_space(&path).expect("the strategy is declared");
            assert_eq!(info.strategy, strategy);
            // `H5P__set_file_space_strategy` stores neither for a strategy
            // that has no managers, so both keep the library defaults.
            assert!(!info.persist);
            assert_eq!(info.threshold, 1);
            let _ = std::fs::remove_file(&path);
        }
    }

    /// The library defaults are what a file says by saying nothing.
    #[test]
    fn the_default_strategy_writes_no_message() {
        let path = temp_path("fsm_default");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::FsmAggr, false, 1),
                    ..Default::default()
                },
            )
            .unwrap();
            w.create_dataset("d", DatatypeMessage::f64_type(), &[4])
                .unwrap();
            w.close().unwrap();
        }
        assert!(declared_file_space(&path).is_none());
        let _ = std::fs::remove_file(&path);
    }

    /// The file-space info message a file carries, read back the way any
    /// reader sees it.
    fn declared_file_space(path: &std::path::Path) -> Option<FileSpaceInfoMessage> {
        crate::io::reader::Hdf5Reader::open(path)
            .unwrap()
            .superblock_extension()
            .file_space_info
            .clone()
    }

    /// A created paged file is laid out on its page grid: the superblock takes
    /// the whole of page zero and the rest of that page is the metadata
    /// manager's first section, which is what `H5MF__alloc_pagefs` gives
    /// `H5F__super_init`'s `H5MF_alloc(f, H5FD_MEM_SUPER, ...)`.
    #[test]
    fn a_created_paged_file_lays_its_pages_out() {
        let path = temp_path("fsm_paged_created");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::Page, true, 1),
                    ..Default::default()
                },
            )
            .unwrap();
            let i = w
                .create_dataset("keep", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.write_dataset_raw(i, &[0u8; 32]).unwrap();
            w.close().unwrap();
        }
        let info = read_only_append(&path)
            .free_space
            .as_deref()
            .expect("the created file declares a strategy")
            .info
            .clone();
        assert_eq!(info.strategy, FileSpaceStrategy::Page);
        assert!(info.persist);
        assert_eq!(info.page_size, 4096);
        assert_eq!(
            std::fs::metadata(&path).unwrap().len() % info.page_size,
            0,
            "a paged file ends on a page boundary"
        );
        let _ = std::fs::remove_file(&path);
    }

    /// A userblock has to be a whole number of pages, or every page boundary
    /// after it is off the file's own grid — `H5F__super_init` refuses one
    /// that is not (H5Fsuper.c:1182-1192).
    #[test]
    fn a_paged_file_refuses_a_userblock_smaller_than_its_page() {
        let path = temp_path("fsm_paged_userblock");
        let Err(err) = Hdf5Writer::create_with_options(
            &path,
            FileCreateOptions {
                file_space: FileSpaceConfig::new(FileSpaceStrategy::Page, true, 1),
                userblock: 512,
                ..Default::default()
            },
        ) else {
            panic!("a 512-byte userblock was accepted on a 4096-byte page");
        };
        assert!(
            format!("{err}").contains("multiple of its 4096-byte"),
            "{err}"
        );
        let _ = std::fs::remove_file(&path);
    }

    /// A page size the builder names is the page the file is actually laid
    /// out in, not just a number the message repeats: every allocation is
    /// shaped by it and the file ends on one of its boundaries.
    #[test]
    fn a_file_created_at_a_non_default_page_size_allocates_by_it() {
        let path = temp_path("fsm_page_size_8k");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::Page, true, 1)
                        .with_page_size(8192),
                    ..Default::default()
                },
            )
            .unwrap();
            let i = w
                .create_dataset("keep", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.write_dataset_raw(i, &[0u8; 32]).unwrap();
            w.close().unwrap();
        }
        let info = read_only_append(&path)
            .free_space
            .as_deref()
            .expect("the created file declares a strategy")
            .info
            .clone();
        assert_eq!(info.page_size, 8192);
        assert_eq!(
            std::fs::metadata(&path).unwrap().len() % 8192,
            0,
            "the file ends on one of the pages it was created with"
        );
        let _ = std::fs::remove_file(&path);
    }

    /// The page size is the fourth of the four properties `H5F__super_init`
    /// compares against the library defaults (H5Fsuper.c:1092-1097), so
    /// naming it is on its own enough to give a file the message — under the
    /// default strategy, which allocates without it.
    #[test]
    fn a_non_default_page_size_alone_gives_the_file_a_message() {
        let path = temp_path("fsm_page_size_only");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::default().with_page_size(1024),
                    ..Default::default()
                },
            )
            .unwrap();
            w.close().unwrap();
        }
        let info = declared_file_space(&path)
            .expect("a file naming only a page size still carries the message");
        assert_eq!(info.strategy, FileSpaceStrategy::FsmAggr);
        assert!(!info.persist);
        assert_eq!(info.page_size, 1024);
        let _ = std::fs::remove_file(&path);
    }

    /// `H5Pset_file_space_page_size` refuses anything below 512 or above
    /// 1 GiB (H5Pfcpl.c:1389-1393), and nothing between: no power of two is
    /// required, so a size the bounds admit is one the file may carry.
    #[test]
    fn a_page_size_outside_the_library_bounds_is_refused() {
        for size in [0, 1, 511, PAGE_SIZE_MAX + 1] {
            let path = temp_path(&format!("fsm_page_size_bad_{size}"));
            let Err(err) = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::Page, true, 1)
                        .with_page_size(size),
                    ..Default::default()
                },
            ) else {
                panic!("a {size}-byte file-space page was accepted");
            };
            assert!(
                format!("{err}").contains("between 512 bytes and 1073741824"),
                "{err}"
            );
            let _ = std::fs::remove_file(&path);
        }
        let path = temp_path("fsm_page_size_odd");
        let w = Hdf5Writer::create_with_options(
            &path,
            FileCreateOptions {
                file_space: FileSpaceConfig::new(FileSpaceStrategy::Page, true, 1)
                    .with_page_size(513),
                ..Default::default()
            },
        )
        .expect("513 is inside the bounds, and no power of two is required");
        w.close().unwrap();
        let _ = std::fs::remove_file(&path);
    }

    /// A paged file's managers are read on reopen, the same as any other
    /// file's: paged aggregation changes which manager a request maps to, not
    /// whether the file has managers to rewrite.
    #[test]
    fn a_paged_file_reports_the_managers_it_persists() {
        let path = fixture_copy("fsm_persist_page.h5", "fsm_read_paged");
        let writer = Hdf5Writer::open_append(&path).unwrap();
        let fs = writer.free_space.as_deref().expect("no managers read");
        assert_eq!(fs.info.strategy, FileSpaceStrategy::Page);
        assert!(
            !writer.allocator.free_extents().is_empty(),
            "the sections the file records were not put back in circulation"
        );
        drop(writer);
        let _ = std::fs::remove_file(&path);
    }

    /// A file with no file-space info message at all — every file this crate
    /// creates — has nothing to read and nothing to write back.
    #[test]
    fn a_file_without_a_strategy_has_no_managers() {
        let path = temp_path("fsm_none");
        {
            let w = Hdf5Writer::create(&path).unwrap();
            w.create_dataset("d", DatatypeMessage::f64_type(), &[4])
                .unwrap();
            w.close().unwrap();
        }
        let writer = Hdf5Writer::open_append(&path).unwrap();
        assert!(writer.free_space.is_none());
        drop(writer);
        let _ = std::fs::remove_file(&path);
    }
    /// Sum of the sections the managers a file names actually hold — what
    /// `h5stat -S` prints as "Amount of tracked free space", read back through
    /// this crate's own decoder so a test can assert on it. A reopen seeds the
    /// allocator with exactly those sections, so its free list is the number.
    fn tracked_free_space(path: &std::path::Path) -> u64 {
        read_only_append(path)
            .allocator
            .free_blocks()
            .iter()
            .map(|b| b.1)
            .sum()
    }

    /// Open for append and mark the writer closed, so dropping it releases the
    /// file lock instead of finalizing and rewriting what is being inspected.
    fn read_only_append(path: &std::path::Path) -> Hdf5Writer {
        let mut w = Hdf5Writer::open_append(path).unwrap();
        w.closed = true;
        w
    }

    /// Add one small dataset, the smallest append that still rewrites the root
    /// header, the superblock extension and — on a persisting file — the
    /// free-space manager.
    fn append_one(path: &std::path::Path, name: &str, disable_managers: bool) {
        let mut w = Hdf5Writer::open_append(path).unwrap();
        if disable_managers {
            // Both halves of the change, so the control is the file as this
            // crate wrote it before: the session neither allocates from the
            // recorded sections nor writes any back.
            w.free_space = None;
            w.allocator.reset_free_list(&[]);
        }
        let i = w
            .create_dataset(name, DatatypeMessage::i32_type(), &[8])
            .unwrap();
        w.write_dataset_raw(
            i,
            &(0..8i32).flat_map(|v| v.to_le_bytes()).collect::<Vec<u8>>(),
        )
        .unwrap();
        w.close().unwrap();
    }

    /// The block list a reopen carries for the superblock extension covers
    /// every chunk of the header, not just the first. The fixture's extension
    /// is a two-chunk header — libhdf5 put the file-space info message in a
    /// continuation — and freeing chunk zero alone left the continuation
    /// allocated with nothing naming it.
    #[test]
    fn a_reopen_carries_every_chunk_of_the_superblock_extension() {
        let path = fixture_copy("fsm_persist.h5", "fsm_ext_chunks");
        let blocks = read_only_append(&path).extension.superseded.clone();
        assert!(
            blocks.len() > 1,
            "the fixture's extension is one chunk, so this proves nothing: {blocks:?}"
        );
        let _ = std::fs::remove_file(&path);
    }

    /// An append on a persisting file both spends and records the space its
    /// managers track: the new dataset comes out of the sections the file
    /// already had, and what the rewrite frees goes back into them.
    #[test]
    fn an_append_reuses_and_records_the_space_the_managers_track() {
        let path = fixture_copy("fsm_persist.h5", "fsm_write");
        let original = std::fs::metadata(&path).unwrap().len();
        let before = tracked_free_space(&path);
        assert_eq!(before, 1910, "the fixture's own managers");

        append_one(&path, "added", false);
        let size = std::fs::metadata(&path).unwrap().len();
        let tracked = tracked_free_space(&path);

        // Negative control: the same append with both halves of this off — no
        // allocating out of the recorded sections and no writing any back —
        // which is what this crate did before it read free space at all.
        let control = fixture_copy("fsm_persist.h5", "fsm_write_control");
        append_one(&control, "added", true);
        let control_size = std::fs::metadata(&control).unwrap().len();
        assert_eq!(
            tracked_free_space(&control),
            before,
            "with the manager rewrite disabled the number must not move"
        );

        // The new dataset's raw data comes out of the raw-data sections the
        // file already recorded, so the append grows the file by less than the
        // same append with the reuse off. It does not stop the growth:
        // `H5MF_alloc` asks one manager and no other, and of this fixture's
        // 1910 free bytes 1848 are raw-data ones, so the metadata the append
        // writes still comes from the end of the file.
        assert!(
            size < control_size,
            "the append took nothing from the {before} bytes free: \
             {original} grew to {size}, the control to {control_size}"
        );
        assert!(
            control_size > original,
            "the control has to grow or it proves nothing"
        );
        // Space no manager and no object claims — `h5stat -S`'s "unaccounted
        // space" — is what the leak was, and it is smaller now.
        assert!(
            size - tracked < control_size - before,
            "unaccounted space went from {} to {}",
            control_size - before,
            size - tracked
        );

        for p in [&path, &control] {
            let _ = std::fs::remove_file(p);
        }
    }

    /// The set the writer holds free when it finishes is exactly the set the
    /// manager it just wrote records — the invariant that makes the on-disk
    /// managers a faithful account of the file's free space.
    #[test]
    fn the_manager_records_the_free_list_the_close_ends_with() {
        let path = fixture_copy("fsm_persist.h5", "fsm_roundtrip");
        let internal = {
            let mut w = Hdf5Writer::open_append(&path).unwrap();
            let i = w
                .create_dataset("added", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.write_dataset_raw(i, &[0u8; 32]).unwrap();
            w.finalize(true).unwrap();
            let blocks = w.allocator.free_extents();
            w.closed = true;
            blocks
        };
        assert!(!internal.is_empty(), "the append freed nothing");

        // Classes included: a section read back out of the wrong manager is a
        // section libhdf5 would offer to the wrong kind of allocation.
        let reread = {
            let w = read_only_append(&path);
            assert!(w.free_space.is_some(), "managers were written");
            w.allocator.free_extents()
        };
        assert_eq!(internal, reread);
        let _ = std::fs::remove_file(&path);
    }

    /// The paged half of
    /// [`the_manager_records_the_free_list_the_close_ends_with`]: a paged
    /// file's sections carry a page and a class as well as an address, and a
    /// section written into the wrong manager or split across a page boundary
    /// would come back different.
    #[test]
    fn the_manager_records_the_free_list_a_paged_close_ends_with() {
        let path = fixture_copy("fsm_persist_page.h5", "fsm_paged_roundtrip");
        let internal = {
            let mut w = Hdf5Writer::open_append(&path).unwrap();
            let i = w
                .create_dataset("added", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.write_dataset_raw(i, &[0u8; 32]).unwrap();
            w.finalize(true).unwrap();
            let blocks = w.allocator.free_extents();
            w.closed = true;
            blocks
        };
        assert!(!internal.is_empty(), "the append freed nothing");

        let reread = {
            let w = read_only_append(&path);
            assert!(w.free_space.is_some(), "managers were written");
            w.allocator.free_extents()
        };
        assert_eq!(internal, reread);
        let _ = std::fs::remove_file(&path);
    }

    /// Negative control for the paged managers: with the read and the rewrite
    /// both off — the file as this crate handled a paged file before — the
    /// space the append frees is recorded nowhere, and the number this crate
    /// reads back is the fixture's own.
    #[test]
    fn a_paged_append_records_nothing_without_the_manager_rewrite() {
        let path = fixture_copy("fsm_persist_page.h5", "fsm_paged_measured");
        let control = fixture_copy("fsm_persist_page.h5", "fsm_paged_control");
        let before = tracked_free_space(&path);
        let original = std::fs::metadata(&path).unwrap().len();

        append_one(&path, "added", false);
        append_one(&control, "added", true);

        assert_eq!(
            tracked_free_space(&control),
            before,
            "the control moved the number it is there to hold still"
        );
        assert_eq!(
            std::fs::metadata(&path).unwrap().len(),
            original,
            "the append grew a paged file with {before} bytes recorded free"
        );
        assert!(
            std::fs::metadata(&control).unwrap().len() > original,
            "the control has to grow or it proves nothing"
        );
        assert_ne!(
            tracked_free_space(&path),
            before,
            "the managers came back holding what the fixture wrote"
        );
        for p in [&path, &control] {
            let _ = std::fs::remove_file(p);
        }
    }

    /// A block released from a dataset's raw data is recorded by the manager
    /// `H5MF_ALLOC_TO_FS_AGGR_TYPE` maps `H5FD_MEM_DRAW` to, and nothing else
    /// is: the dichotomy the sec2 driver installs is what decides, and the two
    /// managers it collapses to are the file-space info message's slots 0 and
    /// 2.
    #[test]
    fn a_released_raw_block_lands_in_the_raw_data_manager() {
        let path = temp_path("fsm_dichotomy");
        {
            let w = Hdf5Writer::create_with_options(
                &path,
                FileCreateOptions {
                    file_space: FileSpaceConfig::new(FileSpaceStrategy::FsmAggr, true, 1),
                    ..Default::default()
                },
            )
            .unwrap();
            let i = w
                .create_dataset("bulk", DatatypeMessage::i32_type(), &[256])
                .unwrap();
            w.write_dataset_raw(i, &vec![0u8; 1024]).unwrap();
            w.create_dataset("keep", DatatypeMessage::i32_type(), &[8])
                .unwrap();
            w.close().unwrap();
        }
        let (raw_addr, raw_len) = {
            let w = read_only_append(&path);
            let i = w.dataset_index("bulk").unwrap();
            let ds = w.ds(i);
            let m = ds.lock();
            (m.data_addr, m.data_size)
        };
        assert!(raw_len >= 1024, "the raw block is {raw_len} bytes");
        {
            let w = Hdf5Writer::open_append(&path).unwrap();
            w.delete_dataset("bulk").unwrap();
            w.close().unwrap();
        }

        let mut w = read_only_append(&path);
        let info = w
            .free_space
            .as_deref()
            .expect("the file persists managers")
            .info
            .clone();
        assert_ne!(info.fs_addr[0], UNDEF_ADDR, "no metadata manager");
        assert_ne!(info.fs_addr[2], UNDEF_ADDR, "no raw-data manager");
        for (slot, &addr) in info.fs_addr.iter().enumerate() {
            if slot != 0 && slot != 2 {
                assert_eq!(addr, UNDEF_ADDR, "slot {slot} names a manager");
            }
        }

        let found = crate::io::free_space_io::read_managers(&mut w.handle, &w.ctx, &info).unwrap();
        let inside = |b: &FreeBlock| b.addr >= raw_addr && b.addr + b.len <= raw_addr + raw_len;
        let raw: Vec<&FreeBlock> = found
            .sections
            .iter()
            .filter(|b| b.manager == FreeSpaceManager::RawData)
            .collect();
        assert!(
            !raw.is_empty(),
            "the deleted dataset's bytes were not recorded"
        );
        assert!(
            raw.iter().all(|b| inside(b)),
            "a raw-data section is outside the deleted dataset's block: {raw:?}"
        );
        assert!(
            found
                .sections
                .iter()
                .filter(|b| b.manager == FreeSpaceManager::Metadata)
                .all(|b| !inside(b)),
            "raw-data bytes were recorded by the metadata manager"
        );
        drop(w);
        let _ = std::fs::remove_file(&path);
    }

    /// A reopened paged file's managers are this writer's to rewrite, and the
    /// three the sec2 driver can reach are the only ones it names.
    ///
    /// `H5MF__alloc_to_fs_type` (H5MF.c:265) sends a request of at least one
    /// page to `H5F_MEM_PAGE_GENERIC` unless the driver declares
    /// `H5FD_FEAT_PAGED_AGGR`, which only the multi and split drivers do, so a
    /// sec2 file has the dichotomy's two small managers and that one large
    /// one: message slots 0, 2 and 6.
    #[test]
    fn a_paged_file_names_only_the_managers_sec2_can_reach() {
        let path = fixture_copy("fsm_persist_page.h5", "fsm_write_paged");
        assert!(
            read_only_append(&path).free_space.is_some(),
            "the paged fixture's managers were not read"
        );
        append_one(&path, "added", false);

        let mut w = read_only_append(&path);
        let info = w
            .free_space
            .as_deref()
            .expect("the file persists managers")
            .info
            .clone();
        assert_eq!(info.strategy, FileSpaceStrategy::Page);
        for (slot, &addr) in info.fs_addr.iter().enumerate() {
            if !matches!(slot, 0 | 2 | 6) {
                assert_eq!(addr, UNDEF_ADDR, "slot {slot} names a manager");
            }
        }
        assert!(
            info.fs_addr.iter().any(|&a| a != UNDEF_ADDR),
            "the rewritten file records nothing free"
        );
        crate::io::free_space_io::read_managers(&mut w.handle, &w.ctx, &info).unwrap();
        drop(w);
        let _ = std::fs::remove_file(&path);
    }

    /// Every section a paged file records sits inside one page, and the pages
    /// its small managers use are pages of their own kind — the invariant
    /// `H5MF__alloc_pagefs` maintains by giving each small request a whole
    /// page of its class and recording the rest of it in that class's manager.
    #[test]
    fn a_paged_files_small_sections_stay_inside_one_page_of_one_kind() {
        let path = fixture_copy("fsm_persist_page.h5", "fsm_paged_pages");
        append_one(&path, "added", false);

        let mut w = read_only_append(&path);
        let info = w
            .free_space
            .as_deref()
            .expect("the file persists managers")
            .info
            .clone();
        let page = info.page_size;
        let found = crate::io::free_space_io::read_managers(&mut w.handle, &w.ctx, &info).unwrap();
        let mut kind_of_page: std::collections::HashMap<u64, FreeSpaceManager> =
            std::collections::HashMap::new();
        for section in &found.sections {
            if section.manager == FreeSpaceManager::Large {
                continue;
            }
            assert_eq!(
                section.addr / page,
                (section.addr + section.len - 1) / page,
                "the section at {:#x} crosses a page boundary",
                section.addr
            );
            let owner = kind_of_page
                .entry(section.addr / page)
                .or_insert(section.manager);
            assert_eq!(
                *owner,
                section.manager,
                "page {} holds sections of two kinds",
                section.addr / page
            );
        }
        drop(w);
        let _ = std::fs::remove_file(&path);
    }
}