ferrosys 0.8.0

Pure-Rust filesystem formatter and reader: ext4, ext3, ext2, FAT32, FAT16, FAT12, exFAT, btrfs, and APFS — rootless, in userspace, byte-reproducible
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
//! The materializer: turn a plan, an allocator, and an inode model into image
//! bytes.
//!
//! This is the layer that does I/O in the sense of producing the final byte image;
//! everything it decides was decided by the pure layers it consumes. It places each
//! inode's data through the allocator, writes the inode table, block and inode
//! bitmaps, group descriptors, and the primary and backup superblocks, and threads
//! the reserved descriptor blocks through the resize inode's double-indirect map so
//! the image can grow safely.
//!
//! Bytes go to any seekable writer. [`format()`] collects them into an in-memory
//! [`Image`]; [`format_to()`] streams them straight out, touching only the blocks it
//! writes, so a filesystem far larger than memory can be created into a file that
//! stays sparse. Nothing is ever read back from the destination.
//!
//! Every checksum field is written through the [`Checksummer`] seam: with
//! `metadata_csum` set the seam is a real crc32c and each metadata object — the
//! superblock and its backups, the group descriptors, inodes, block and inode
//! bitmaps, directory blocks, and attribute blocks — is checksummed as it is laid
//! down; without it the seam zeroes those fields. Which seam is active is the only
//! thing that changes between the two, not this code.

use std::borrow::Cow;
use std::collections::BTreeMap;
use std::io::{Seek, Write};

use crate::alloc::{AllocError, Allocator};
use crate::crc32c::crc32c;
use crate::csum as recipe;
use crate::csum::{Checksummer, Crc32c, CsumScheme, NullCsum};
use crate::dir::{DirBlock, DirBlockKind, DirError, DirLayout, HtreeDir, LinearDir};
use crate::escape::hex;
use crate::extent::{
    ExtentError, build_leaves, build_tree, node_capacity, plan_tree, tail_offset, write_node,
};
use crate::feature::{FeatureSet, LARGE_FILE_MIN_SIZE, Profile, resize_inode_size};
use crate::geometry::{
    BlockRange, GeometryError, GroupLayout, GrowReservation, InodeCount, Layout, PlanRequest,
    ReservedRatio, plan_layout,
};
use crate::hash::{HashSignedness, HashVersion};
use crate::io::ByteSink;
use crate::journal::{self, JournalSize};
use crate::model::{
    Content, FIRST_USER_INO, FsModel, LOST_FOUND_INO, ModelConfig, ModelError, ModelInode,
    build_model,
};
use crate::ondisk::{
    BG_BLOCK_UNINIT, BG_INODE_UNINIT, BG_INODE_ZEROED, DIR_TAIL_LEN, DX_ENTRY_LEN, DX_TAIL_LEN,
    DirEntry, GroupDescriptor, Inode, InodeFlags, ParseError, SuperBlock, XATTR_CHECKSUM_OFFSET,
    encode_block, encode_device, encode_inline, extra_isize_for, get_u16, get_u32,
    orphan_entries_len, orphan_tail_bytes, put_u16, put_u32, split_for_storage,
    superblock_checksum, write_dir_tail, write_dx_tail,
};
use crate::sizing::Slack;
use crate::source::Source;
use crate::time::Timestamp;
use crate::xattr::Xattr;

/// The reserved inode mapping the reserved group-descriptor-table blocks.
const RESIZE_INO: u32 = 7;

/// Relabel an out-of-space failure as the journal's own.
///
/// The journal is the largest structure a filesystem places for itself, so on a small
/// filesystem it is the allocation that runs out of room — and a bare "out of space" for it
/// reads as though the caller's contents did not fit, on a filesystem that may hold none.
/// Any other failure passes through unchanged.
fn journal_space(e: impl Into<FormatError>) -> FormatError {
    match e.into() {
        FormatError::Alloc(AllocError::OutOfSpace {
            requested,
            available,
        }) => FormatError::JournalDoesNotFit {
            requested,
            available,
        },
        other => other,
    }
}

/// The reserved inode holding the journal (`s_journal_inum`).
const JOURNAL_INO: u32 = 8;

/// The inode holding the orphan file (`s_orphan_file_inum`). It is not one of the
/// reserved inodes: the orphan file is allocated at format time, so it takes the first
/// inode past `/lost+found` — which is why a source's entries begin one higher when
/// `orphan_file` is on.
const ORPHAN_INO: u32 = FIRST_USER_INO;

/// The inode number a source's entries start at: the first past `/lost+found`, moved up
/// by the inodes the feature set claims for itself at format time.
fn first_user_inode(feature: &FeatureSet) -> u32 {
    if feature.has_orphan_file() {
        ORPHAN_INO + 1
    } else {
        FIRST_USER_INO
    }
}

/// The most 512-byte sectors an inode is charged without `huge_file`.
///
/// `i_blocks_lo` is 32 bits wide, and without the feature the two bytes above it are ext2's
/// `l_i_frag` and `l_i_fsize` rather than a high half — so this is the whole field. Two
/// tebibytes, less one sector.
const MAX_SECTORS_WITHOUT_HUGE_FILE: u64 = u32::MAX as u64;

/// The orphan file's size in blocks: one block per 4096 filesystem blocks, held between
/// 32 and 512 blocks. The floor keeps concurrent deletions on a small filesystem from
/// contending for the few entries a strict ratio would give them, and the ceiling stops
/// a large one from reserving space for far more orphans than can ever exist at once.
///
/// It is a count of blocks, not of bytes, so a filesystem of a given block count is
/// given the same orphan file whatever its block size.
fn orphan_file_blocks(total_blocks: u64) -> u32 {
    const RATIO: u64 = 4096;
    const MIN: u64 = 32;
    const MAX: u64 = 512;
    (total_blocks / RATIO).clamp(MIN, MAX) as u32
}

/// What the kernel does when it detects an error on the mounted filesystem, recorded in
/// the superblock's `s_errors` field (offset `0x3c`) and consulted by the kernel at mount
/// time.
///
/// This is a runtime policy the image carries, not a property of the layout: every value
/// produces a filesystem this crate considers equally correct, differing only in that one
/// superblock field and the checksum computed over it.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum ErrorBehavior {
    /// Note the error and carry on (`s_errors = 1`) — the kernel's own default, and this
    /// crate's.
    #[default]
    Continue,
    /// Remount the filesystem read-only (`s_errors = 2`), so a detected inconsistency
    /// cannot spread through further writes.
    RemountReadOnly,
    /// Panic the kernel (`s_errors = 3`), halting the machine on any filesystem error.
    Panic,
}

// The names `mke2fs -e` takes, in the order it documents them. A report that *describes*
// an image spells these differently on purpose — `dumpe2fs` writes `Remount read-only`, and
// a description of an image reads as that tool's output rather than as an argument — so the
// spelling here is the one that can be typed back into a format.
crate::naming::named_choice!(ErrorBehavior {
    ErrorBehavior::Continue => "continue",
    ErrorBehavior::RemountReadOnly => "remount-ro",
    ErrorBehavior::Panic => "panic",
});

impl ErrorBehavior {
    /// The on-disk `s_errors` value this policy is stored as.
    const fn to_s_errors(self) -> u16 {
        match self {
            Self::Continue => 1,
            Self::RemountReadOnly => 2,
            Self::Panic => 3,
        }
    }
}

/// Options controlling a format that do not come from the source or the size.
///
/// Build one with [`new`](Self::new), which takes the three identity inputs every image
/// needs and defaults the rest, then set the fields a format departs from the default on.
#[derive(Clone, Copy, Debug)]
#[non_exhaustive]
pub struct FormatOptions {
    /// The 16-byte filesystem UUID (`s_uuid`), supplied by the caller.
    pub uuid: [u8; 16],
    /// The filesystem creation and write time.
    pub time: Timestamp,
    /// The 16-byte directory-hash seed (`s_hash_seed`).
    pub hash_seed: [u8; 16],
    /// The algorithm a hash-indexed directory orders its names by
    /// (`s_def_hash_version`). Defaults to [`HashVersion::HalfMd4`].
    pub hash_version: HashVersion,
    /// Whether a name's bytes are read as signed or unsigned when a hash-indexed directory
    /// hashes it (`s_flags`). Defaults to [`HashSignedness::Unsigned`], which makes a name's
    /// hash — and so the image's bytes — independent of the machine that wrote it. An
    /// extended attribute's hash reads its name's bytes as unsigned either way, as `mke2fs`
    /// writes it.
    ///
    /// Set it to [`HashSignedness::Signed`] to match an image built by a tool that
    /// takes its signedness from the host's `char`, which is signed on x86 and
    /// unsigned on arm64. An image records the choice, so either is read correctly.
    pub hash_signedness: HashSignedness,
    /// How much reserved descriptor headroom to build in, sizing the reserved GDT
    /// blocks that make the image resize-safe. Defaults to [`GrowReservation::Max`].
    pub grow: GrowReservation,
    /// How many inodes to provide. Defaults to [`InodeCount::Auto`], the size-driven
    /// count; an override sets the density or the count directly.
    pub inodes: InodeCount,
    /// The share of blocks held back for the super-user (`s_r_blocks_count`). Defaults to
    /// [`ReservedRatio::DEFAULT`], 5%.
    pub reserved: ReservedRatio,
    /// The volume label (`s_volume_name`), NUL-padded to sixteen bytes; all zero for an
    /// unlabelled filesystem, which is the default.
    pub volume_name: [u8; 16],
    /// The feature profile to emit.
    pub feature: FeatureSet,
    /// What the kernel does on a detected filesystem error (`s_errors`). Defaults to
    /// [`ErrorBehavior::Continue`], the kernel's own default.
    pub errors: ErrorBehavior,
    /// How large the journal is, when the feature set carries a journal. Ignored when
    /// `has_journal` is off. Defaults to [`JournalSize::Auto`].
    pub journal: JournalSize,
    /// When set, every inode's access, change, modification, and creation time is
    /// forced to this value — the source-derived inodes and the reserved structural
    /// ones (resize, journal, orphan) alike — overriding the per-entry times a source
    /// supplies. This makes output byte-reproducible regardless of the source's
    /// timestamps: the clamp a build takes when reproducibility outranks timestamp
    /// fidelity. Unset, each source-derived inode keeps its own times and the
    /// structural inodes take the format [`time`](Self::time).
    pub fixed_time: Option<Timestamp>,
}

impl FormatOptions {
    /// Reject a format-time clock the superblock's 32-bit time fields cannot hold, so a
    /// far-future creation time is refused rather than silently truncated to 1970-plus.
    fn validate_format_time(&self) -> Result<(), FormatError> {
        let secs = self.time.secs;
        if secs < 0 || secs > u64::from(u32::MAX) as i64 {
            return Err(FormatError::FormatTimeOutOfRange { secs });
        }
        Ok(())
    }

    /// Options for the default feature profile ([`FeatureSet::DEFAULT`]) with the
    /// given identity inputs. The grow reservation defaults to [`GrowReservation::Max`]
    /// — the fail-safe choice that reserves the most online-grow headroom the format
    /// allows; set [`grow`](Self::grow) to [`GrowReservation::UpTo`] for a known
    /// deployment target or [`GrowReservation::None`] to reserve nothing.
    #[must_use]
    pub fn new(uuid: [u8; 16], time: Timestamp, hash_seed: [u8; 16]) -> Self {
        Self {
            uuid,
            time,
            hash_seed,
            hash_version: HashVersion::default(),
            hash_signedness: HashSignedness::default(),
            grow: GrowReservation::default(),
            inodes: InodeCount::default(),
            reserved: ReservedRatio::default(),
            volume_name: [0; 16],
            feature: FeatureSet::default(),
            errors: ErrorBehavior::default(),
            journal: JournalSize::Auto,
            fixed_time: None,
        }
    }

    /// The geometry planner's inputs for a filesystem of `size_bytes` under these
    /// options: the feature set and the three sizing knobs, gathered into the request
    /// [`plan_layout`] takes.
    ///
    /// This is the one place the format options become a plan, so a geometry knob added
    /// here reaches the planner once rather than at every entry point that formats.
    #[must_use]
    pub const fn plan_request(&self, size_bytes: u64) -> PlanRequest {
        PlanRequest::new(size_bytes, self.feature)
            .grow(self.grow)
            .inodes(self.inodes)
            .reserved(self.reserved)
    }

    /// Seed the feature set from an ext filesystem profile, replacing
    /// [`feature`](Self::feature) with the baseline words `mke2fs -t` writes for that
    /// family. Sugar for `options.feature = profile.feature_set()`, chainable from
    /// [`new`](Self::new); set individual features on [`feature`](Self::feature) afterward
    /// to depart from the baseline.
    #[must_use]
    pub fn profile(mut self, profile: Profile) -> Self {
        self.feature = profile.feature_set();
        self
    }
}

/// A failure formatting an image.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FormatError {
    /// Writing to the destination failed.
    #[error(transparent)]
    Io(#[from] std::io::Error),
    /// Planning the geometry failed.
    #[error(transparent)]
    Geometry(#[from] GeometryError),
    /// Building the inode model failed.
    #[error(transparent)]
    Model(#[from] ModelError),
    /// Packing a directory failed.
    #[error(transparent)]
    Dir(#[from] DirError),
    /// Building an extent tree failed.
    #[error(transparent)]
    Extent(#[from] ExtentError),
    /// Allocating blocks failed.
    #[error(transparent)]
    Alloc(#[from] AllocError),
    /// Serializing an on-disk structure failed.
    #[error(transparent)]
    Parse(#[from] ParseError),
    /// The source needs more inodes than the geometry provides.
    #[error("source needs {needed} inodes but the filesystem has {available}")]
    #[non_exhaustive]
    TooManyInodes {
        /// Inodes the source needs.
        needed: u32,
        /// Inodes the geometry provides.
        available: u32,
    },
    /// An explicit [`JournalSize::Blocks`] is below the minimum jbd2 accepts.
    #[error("journal of {requested} blocks is below the minimum of {minimum}")]
    #[non_exhaustive]
    JournalTooSmall {
        /// Journal blocks requested.
        requested: u32,
        /// The minimum journal size in blocks.
        minimum: u32,
    },
    /// The journal does not fit in the blocks left free after the filesystem's fixed
    /// metadata.
    ///
    /// The bare allocator failure ([`Alloc`](Self::Alloc)) is what a *source's* files
    /// running out of room looks like; the journal earns its own variant because it is the
    /// filesystem's own structure and the largest one a small filesystem places, so "out of
    /// space" for it would otherwise read as though the caller's contents did not fit.
    #[error(
        "the journal needs {requested} blocks but only {available} are free: build a \
         smaller journal, none at all, or a larger filesystem"
    )]
    #[non_exhaustive]
    JournalDoesNotFit {
        /// Blocks the journal needs.
        requested: u64,
        /// Blocks left free for it.
        available: u64,
    },
    /// The feature set carries a journal, but the filesystem is too small to hold the
    /// smallest one jbd2 accepts.
    ///
    /// This is a different fault from [`JournalTooSmall`](Self::JournalTooSmall), which is
    /// a size the caller named: here no size would do, so the ways out are a filesystem
    /// large enough for a journal or a feature set without one. A journal is not silently
    /// dropped to make the format succeed — a feature word is a promise about what the
    /// filesystem carries, and clearing one the caller asked for would break it quietly.
    #[error(
        "a filesystem of {blocks} blocks has no room for a journal, which needs at least \
         {minimum} blocks: build it without the has_journal feature, or make it larger"
    )]
    #[non_exhaustive]
    FilesystemTooSmallForJournal {
        /// Blocks the filesystem has.
        blocks: u64,
        /// The smallest journal jbd2 accepts, in blocks.
        minimum: u32,
    },
    /// A regular file the filesystem provides for itself reaches
    /// [`LARGE_FILE_MIN_SIZE`] on a feature set without `large_file`, which is the
    /// feature that describes such a file. Only a journal sized past the bound by an
    /// explicit [`JournalSize::Blocks`] reaches this: the resize inode's pairing is
    /// settled at plan time by [`FeatureSet::validate`], the orphan file is bounded far
    /// below it, and a source entry is refused by the model, which can name its path.
    #[error("the {what} is {size} bytes, a large file on a filesystem without large_file")]
    #[non_exhaustive]
    LargeFileWithoutFeature {
        /// The structure whose size crosses the bound.
        what: &'static str,
        /// The size it declares.
        size: u64,
    },
    /// An inode is charged more 512-byte sectors than a feature set without `huge_file`
    /// records. Without the feature only `i_blocks_lo` exists — the two bytes beside it are
    /// ext2's `l_i_frag` and `l_i_fsize` — so the count is 32 bits wide and stops at two
    /// tebibytes.
    ///
    /// A classic block map reaches 4.004 TiB at a 4096-byte block, and
    /// [`FeatureSet::validate`] refuses `huge_file` on a non-extent set — so on ext2 and
    /// ext3 the map outruns the field, and a file in that range is refused here rather than
    /// serialized with a wrapped low half and a high half the feature words say is not
    /// there.
    #[error(
        "the {what} is charged {sectors} sectors, past the {limit} an inode records \
         without the huge_file feature"
    )]
    #[non_exhaustive]
    BlockCountWithoutHugeFile {
        /// The structure whose block count crosses the bound.
        what: &'static str,
        /// The 512-byte sectors it is charged.
        sectors: u64,
        /// The most an inode records without the feature.
        limit: u64,
    },
    /// A file needs more blocks than a classic (ext2/ext3) block map reaches: twelve direct
    /// pointers and three levels of indirect blocks, `12 + p + p² + p³` blocks for
    /// `p = block_size / 4`. That is 16.06 GiB at a 1024-byte block and 4.004 TiB at the
    /// 4096-byte default.
    ///
    /// This is the block-mapped twin of [`ExtentError::FileTooLarge`]. A file past the reach
    /// would otherwise be mapped as far as the words go and written no further, while its
    /// size claimed the whole length — so it is refused instead. Only a feature set without
    /// `extent` reaches it: an extent-mapped file has the extent tree's own bound.
    #[error(
        "a file of {blocks} blocks exceeds the {limit} a classic block map reaches at a \
         {block_size}-byte block: build it on a feature set with extents, or at a larger \
         block size"
    )]
    #[non_exhaustive]
    FileTooLargeForBlockMap {
        /// Logical blocks the file needs.
        blocks: u64,
        /// Logical blocks the map reaches.
        limit: u64,
        /// The block size whose pointer count fixes that reach.
        block_size: u32,
    },
    /// A block the resize inode must map lies past what its block map addresses. The
    /// map is a classic 32-bit block map, and the geometry reserves descriptor blocks
    /// only on a filesystem a 32-bit block number spans, so a block past that reaching
    /// the map means the two disagree. It is refused rather than truncated into a
    /// pointer at the wrong block.
    #[error("the resize inode's 32-bit block map cannot name block {block}")]
    #[non_exhaustive]
    ResizeMapNeeds32BitBlocks {
        /// The block the map would have had to name.
        block: u64,
    },
    /// The inode the orphan file takes is already held by an entry, so writing the file
    /// there would displace it — leaving a directory entry that names the orphan file
    /// rather than the entry it was written for.
    ///
    /// Formatting derives the first entry's inode number from the feature set, so the
    /// two cannot disagree. The check stands guard over that derivation: a feature added
    /// later that claims an inode of its own must move the entries up, and until it does,
    /// this refuses the image rather than writing a directory that points at the wrong
    /// file.
    #[error("the orphan file's inode {inode} is already held by an entry")]
    #[non_exhaustive]
    OrphanInodeInUse {
        /// The contested inode number.
        inode: u32,
    },
    /// The image is larger than this machine can hold in memory: past what a `usize`
    /// addresses, which a 32-bit target reaches at 4 GiB, or more than the allocator will
    /// provide. [`format_to`] streams an image of any size to a seekable destination and is
    /// the path for one this large.
    #[error(
        "an image of {bytes} bytes cannot be held in memory on this machine; \
         stream it with format_to"
    )]
    #[non_exhaustive]
    ImageTooLargeInMemory {
        /// The image's size in bytes.
        bytes: u64,
    },
    /// No filesystem this format describes holds the source with the room
    /// [`FormatPlan::fit`] was asked to leave free.
    ///
    /// The search tries sizes up to a ceiling — the largest filesystem the feature set
    /// addresses, or the grow target when [`GrowReservation::UpTo`] names one — and this is
    /// what it says when the largest of them was planned and placed successfully and still
    /// had less room left than the slack asks for. A size that *failed* is reported by its
    /// own failure instead, so this names the case where nothing was wrong except that the
    /// filesystem could not be made big enough.
    #[error(
        "no filesystem of up to {ceiling} blocks holds the source with the requested room \
         to spare"
    )]
    #[non_exhaustive]
    DoesNotFit {
        /// The largest block count the search was allowed to try.
        ceiling: u64,
    },
    /// A [`Slack::Share`] asks for a larger share of the filesystem than a fit search will
    /// look for.
    ///
    /// Past the limit the filesystem is more than ten times the source it holds, and a size
    /// that far from what the contents need is better named outright than searched for.
    #[error("slack share of {hundredths} hundredths of a percent is past the {limit} limit")]
    #[non_exhaustive]
    SlackShareTooLarge {
        /// The share asked for, in hundredths of one percent.
        hundredths: u16,
        /// The largest share a fit search accepts.
        limit: u16,
    },
    /// The format-time clock ([`FormatOptions::time`]) lies outside the range the
    /// superblock's 32-bit time fields hold: seconds in `[0, 2^32)`, from 1970 to 2106.
    /// It is refused rather than truncated to a different instant. Per-file timestamps
    /// span a wider range; this bound is on the filesystem's own creation clock.
    #[error("format time of {secs}s is outside the superblock range [0, 2^32) seconds")]
    #[non_exhaustive]
    FormatTimeOutOfRange {
        /// The out-of-range seconds value.
        secs: i64,
    },
}

/// A block number as the resize inode's block map stores it.
///
/// The map is 32 bits wide, so every block it names must be one a 32-bit block number
/// addresses. Converting through this function refuses a block past that rather than
/// silently truncating it into a pointer at the wrong block.
fn map_block(block: u64) -> Result<u32, FormatError> {
    u32::try_from(block).map_err(|_| FormatError::ResizeMapNeeds32BitBlocks { block })
}

/// The classic (ext2/ext3) block map's twelve direct pointers, filling the first twelve
/// of the inode's fifteen block-area words. The remaining three hold the single-,
/// double-, and triple-indirect roots.
const DIRECT_BLOCKS: usize = 12;

/// Levels of indirection above the direct pointers: single, double, and triple.
const INDIRECT_LEVELS: usize = 3;

/// The most logical blocks a classic block map reaches at `block_size`: the twelve direct
/// pointers, plus one, two, and three levels of indirect blocks each holding
/// `block_size / 4` pointers.
///
/// A 1024-byte block reaches 16,843,020 blocks — 16.06 GiB — and a 4096-byte block reaches
/// 1,074,791,436, which is 4.004 TiB. Past that the map runs out of words, and a file that
/// needs more of them is refused rather than mapped short.
fn classic_map_reach(block_size: usize) -> u64 {
    let ppb = (block_size / 4) as u64;
    DIRECT_BLOCKS as u64 + ppb + ppb * ppb + ppb * ppb * ppb
}

/// A finished filesystem image: the bytes, and the geometry that produced them.
pub struct Image {
    /// The complete image bytes, `total_blocks * block_size` long.
    bytes: Vec<u8>,
    /// The layout the image was written against.
    layout: Layout,
}

impl Image {
    /// The image bytes.
    #[must_use]
    pub fn as_bytes(&self) -> &[u8] {
        &self.bytes
    }

    /// Consume the image, returning its bytes.
    #[must_use]
    pub fn into_bytes(self) -> Vec<u8> {
        self.bytes
    }

    /// The layout the image was written against.
    #[must_use]
    pub fn layout(&self) -> &Layout {
        &self.layout
    }

    /// Write the image to `w`.
    ///
    /// # Errors
    ///
    /// Any I/O error from `w`.
    pub fn write_to(&self, mut w: impl std::io::Write) -> std::io::Result<()> {
        w.write_all(&self.bytes)
    }
}

/// Format a filesystem of `size_bytes` populated from `source`, assembling the whole
/// image in memory.
///
/// The image is held as one buffer of its full size, so this needs as much memory as the
/// filesystem is large. [`format_to`] writes the same bytes to a seekable destination
/// without ever holding them all.
///
/// # Errors
///
/// A [`FormatError`] if the geometry, model, allocation, or serialization cannot be
/// realized, or [`FormatError::ImageTooLargeInMemory`] if the image is larger than this
/// machine can hold in memory.
pub fn format(
    source: impl Source,
    size_bytes: u64,
    options: FormatOptions,
) -> Result<Image, FormatError> {
    let plan = FormatPlan::new(source, size_bytes, options)?;

    // The whole image is one buffer, so its size must be one this platform can address:
    // a 32-bit target holds no 4 GiB image, and a cast would silently size the buffer to
    // the low bits of the count and write a filesystem into the wrong number of bytes.
    let image_bytes = plan.layout.total_blocks * u64::from(plan.layout.block_size);
    let buffer = crate::io::image_buffer(image_bytes)
        .ok_or(FormatError::ImageTooLargeInMemory { bytes: image_bytes })?;
    let mut sink = std::io::Cursor::new(buffer);
    let layout = plan.write_to(&mut sink)?;
    Ok(Image {
        bytes: sink.into_inner(),
        layout,
    })
}

/// Format a filesystem of `size_bytes` populated from `source`, streaming its bytes
/// into `sink` and returning the layout they realize.
///
/// Only the blocks the filesystem actually uses are written, and nothing is read
/// back, so a file destination stays sparse and the whole image never exists in
/// memory. The sink is extended to the filesystem's full size, and every byte it
/// holds that is not written must read back as zero — a freshly created file, or one
/// truncated to zero length, satisfies that.
///
/// # Memory
///
/// Three things are held while the image streams out, and none of them is the image:
///
/// - **The entry list.** Every [`SourceEntry`](crate::source::SourceEntry) the source yields — its path, metadata,
///   and extended attributes — is materialized before the first block is written, and the
///   inode model built from it is held until the last one is. This grows with the number
///   of entries, not with their size.
/// - **One window of a file's contents, while it is placed.** How long a file's bytes are
///   held depends on what the source supplies. A
///   [`FileContent::Owned`](crate::source::FileContent::Owned) entry holds its bytes from the
///   moment the source is built, so a list of them costs the sum of every file. A
///   [`FileContent::Range`](crate::source::FileContent::Range) is read at placement a
///   mebibyte at a time, so a list of them costs one mebibyte however large the files are.
///   `ArchiveSource::from_path` is the difference for a tar source.
/// - **The allocator's used-block bitmap**, for the whole run, at one bit per filesystem
///   block: `total_blocks / 8` bytes, 128 MiB for a 4 TiB image at a 4 KiB block.
///
/// So peak memory grows with the entry count and the filesystem's block count — never with
/// the image's size in bytes.
///
/// # Errors
///
/// A [`FormatError`] if the geometry, model, allocation, or serialization cannot be
/// realized, or if writing to `sink` fails.
pub fn format_to(
    source: impl Source,
    size_bytes: u64,
    options: FormatOptions,
    mut sink: impl Write + Seek,
) -> Result<Layout, FormatError> {
    FormatPlan::new(source, size_bytes, options)?.write_to(&mut sink)
}

/// Everything a format decides before a byte is written: the geometry the planner produces
/// and the inode model the source builds, checked against each other.
///
/// This is the whole fallible half of a format, and holding it as a value is what lets a
/// caller find out whether a format will work **before** touching the destination. That
/// matters because a format's destination must read as zero where the filesystem does not
/// write — so creating or truncating it is part of formatting — and a destination truncated
/// for a format that then failed on its source would be a file destroyed by a run that
/// wrote no filesystem. [`write_to`](Self::write_to) is the half that can only fail on I/O.
///
/// It is also what a caller reports from without writing anything: [`layout`](Self::layout)
/// is the geometry the bytes would realize, exact rather than estimated, because it is the
/// same value the write uses.
///
/// [`plan_layout`] plans the *geometry* alone, from a size and a feature set. A plan here is
/// that plus the model built from a source, and the check that the one fits the other.
///
/// Both entry points build one, so a knob added to [`FormatOptions`] reaches them together.
/// Two paths that derived this separately would be two paths that could disagree, and a
/// disagreement between them is not a compile error — it is one entry point formatting
/// differently from the other.
///
/// ```no_run
/// # use ferrosys::ext::{FormatOptions, FormatPlan, TreeBuilder};
/// # use ferrosys::ext::Timestamp;
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let options = FormatOptions::new([0x11; 16], Timestamp::from_secs(1_700_000_000), [0; 16]);
/// let plan = FormatPlan::new(TreeBuilder::new(), 64 << 20, options)?;
/// println!("{} blocks, {} reserved to grow into",
///     plan.layout().total_blocks, plan.layout().reserved_gdt_blocks);
///
/// // Nothing has been written yet, and the destination is untouched until here.
/// let out = std::fs::File::create("rootfs.img")?;
/// let layout = plan.write_to(out)?;
/// # let _ = layout;
/// # Ok(())
/// # }
/// ```
pub struct FormatPlan {
    layout: Layout,
    model: FsModel,
    options: FormatOptions,
    /// The journal's size in blocks, or `None` under a feature set without one.
    journal_blocks: Option<u32>,
}

impl FormatPlan {
    /// Plan a format of `size_bytes` from `source` under `options`, deciding everything
    /// but the bytes.
    ///
    /// # Errors
    ///
    /// A [`FormatError`] if the geometry cannot be realized, the source cannot be modelled,
    /// the model needs more inodes than the geometry provides, or the model does not fit
    /// the blocks the geometry leaves. Every failure a format has that is not an I/O failure
    /// of the destination is here.
    ///
    /// The model is placed into the geometry here, through the same placement a format
    /// performs, writing nothing: that is what finds a source too large for the size before
    /// a destination is opened, so a plan that is returned is one that writes. It costs the
    /// allocator's bitmap, one bit per block, and reads no file's bytes.
    pub fn new(
        source: impl Source,
        size_bytes: u64,
        options: FormatOptions,
    ) -> Result<Self, FormatError> {
        options.validate_format_time()?;
        let model = model_of(source, &options)?;
        let (layout, journal_blocks) = plan_geometry(&model, &options, size_bytes)?;
        free_after_placing(&layout, &options, journal_blocks, &model)?;
        Ok(Self {
            layout,
            model,
            options,
            journal_blocks,
        })
    }

    /// Plan the smallest filesystem that holds `source` with `slack` left free, deciding
    /// the size as well as everything else.
    ///
    /// [`new`](Self::new) is given a size; this one finds it. The floor is not a formula —
    /// how much room a filesystem has depends on how many groups it has, how large its
    /// inode tables are, and how large a journal its size earns, all of which follow from
    /// the size itself — so it is found by planning candidate sizes and placing the source
    /// into each, which is the same placement a format performs. A candidate is judged by
    /// what the placement leaves free, not by an estimate beside it, so a size this returns
    /// is a size that formats.
    ///
    /// **The size returned formats, and one block less does not.** The search closes a
    /// bracket whose ends are both established by placing: it ends holding a size that was
    /// placed successfully and the size one block below it that was not. Fit is not
    /// monotone in size — a filesystem one block larger can need another group, and so have
    /// less room than the one below it — so that is a smallest size rather than provably
    /// *the* smallest, and it is the guarantee worth having either way.
    ///
    /// The source is consumed once and the model built from it is kept, so the finished
    /// plan is ready to [`write_to`](Self::write_to) with no second walk of the source.
    /// [`size_bytes`](Self::size_bytes) reports what was decided.
    ///
    /// # What the search costs
    ///
    /// A handful of placements rather than one: a bracket found by doubling from what the
    /// contents occupy, then a bisection within it. No file's bytes are read and no block is
    /// written at any of them — a probe places, and the destination it places into keeps
    /// nothing.
    ///
    /// A probe's memory is a format's at that size, because it is a format's own placement:
    /// the allocator's bitmap, at one bit per block. So the search costs what formatting the
    /// sizes it tries would cost, and it tries sizes near the answer.
    ///
    /// ```no_run
    /// # use ferrosys::Slack;
    /// # use ferrosys::ext::{FormatOptions, FormatPlan, Timestamp, TreeBuilder};
    /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
    /// # let source = TreeBuilder::new();
    /// let options = FormatOptions::new([0x11; 16], Timestamp::from_secs(1_700_000_000), [0; 16]);
    /// // The smallest filesystem holding the source, with a fifth of it still free.
    /// let plan = FormatPlan::fit(source, options, Slack::Share(2000))?;
    /// println!("{} bytes", plan.size_bytes());
    /// plan.write_to(std::fs::File::create("rootfs.img")?)?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// [`FormatError::SlackShareTooLarge`] if the slack asks for a share past the limit,
    /// [`FormatError::DoesNotFit`] if no filesystem the format describes holds the source
    /// with that room, and otherwise whatever the largest size tried failed with — which
    /// for a source too large for its feature set is the failure that size met, not a bare
    /// statement that nothing worked.
    pub fn fit(
        source: impl Source,
        options: FormatOptions,
        slack: Slack,
    ) -> Result<Self, FormatError> {
        options.validate_format_time()?;
        // A feature set that cannot be realized fails at every size, so it is refused once
        // here rather than found again at each candidate and reported as a sizing failure.
        options.feature.validate().map_err(GeometryError::from)?;
        let model = model_of(source, &options)?;
        let fitted = crate::fit::search(&model, &options, slack)?;
        Ok(Self {
            layout: fitted.layout,
            model,
            options,
            journal_blocks: fitted.journal_blocks,
        })
    }

    /// The geometry the bytes will realize.
    #[must_use]
    pub fn layout(&self) -> &Layout {
        &self.layout
    }

    /// The filesystem's size in bytes: its block count times its block size.
    ///
    /// This is what [`fit`](Self::fit) decided, and with [`Slack::None`] it is the smallest
    /// filesystem that holds the source. For a plan from [`new`](Self::new) it is the size
    /// that was asked for, rounded down to a whole number of blocks.
    #[must_use]
    pub fn size_bytes(&self) -> u64 {
        self.layout.total_blocks * u64::from(self.layout.block_size)
    }

    /// How many inodes the source occupies, of the [`Layout::total_inodes`] the geometry
    /// provides. The reserved inodes every filesystem carries are part of the count.
    #[must_use]
    pub fn used_inodes(&self) -> u32 {
        self.model.used_inode_count()
    }

    /// The geometry this plan realized, as one canonical document.
    ///
    /// This is what the *size* decided: block and inode counts, the group table, the
    /// descriptor headroom the grow reservation sized, and the journal's realized length.
    /// It moves with the filesystem size, so two images built to the same contract at
    /// different sizes have different geometry pins and that is correct rather than drift.
    ///
    /// ```text
    /// ferrosys-geometry-pin 1
    /// block_size 4096
    /// total_blocks 16384
    /// blocks_per_group 32768
    /// first_data_block 0
    /// group_count 1
    /// inodes_per_group 16384
    /// inode_table_blocks 1024
    /// total_inodes 16384
    /// gdt_blocks 1
    /// reserved_gdt_blocks 256
    /// flex_bg_size 16
    /// max_grow_blocks 538968064
    /// reserved_blocks 819
    /// groups 1 crc32c 0x8a137015
    /// journal_blocks 1024
    /// ```
    ///
    /// **Pin this at a size you choose, not at the size you happen to be building.** A
    /// [`policy_pin`](FormatOptions::policy_pin) records the options by name, so it moves
    /// when an option is renamed or re-defaulted — and not when the formula behind one
    /// changes underneath an unchanged name. `grow max` reads the same before and after a
    /// change to what `Max` reserves; `reserved_gdt_blocks` does not. Planning at one
    /// reference size and pinning the result turns such a change into a diff wherever it
    /// moves what that size resolves to, and the reference size makes the pin independent
    /// of what any particular build is sized to.
    ///
    /// The per-group placements are one line rather than one per group — the count, and a
    /// `crc32c` over every field of every group in order. A filesystem has as many groups
    /// as it has room for and a large one has millions, so the document stays a fixed size
    /// while a placement that moves still changes it. The projection is an exhaustive
    /// destructure, so a field added to [`Layout`] or [`GroupLayout`] is a compile error
    /// rather than a silent omission from every recorded pin.
    #[must_use]
    pub fn geometry_pin(&self) -> String {
        let mut out = String::from("ferrosys-geometry-pin 1\n");
        push_layout_pin(&mut out, &self.layout);
        out.push_str("journal_blocks ");
        match self.journal_blocks {
            Some(blocks) => out.push_str(&blocks.to_string()),
            None => out.push_str("none"),
        }
        out.push('\n');
        out
    }

    /// Write the filesystem into `sink`, returning the geometry it realized.
    ///
    /// Only the blocks the filesystem uses are written, and nothing is read back, so a file
    /// destination stays sparse. The sink is extended to the filesystem's full size, and
    /// every byte it holds that is not written must read back as zero — a freshly created
    /// file, or one truncated to zero length, satisfies that.
    ///
    /// # Errors
    ///
    /// A [`FormatError`] if the allocation or serialization cannot be realized, or
    /// [`FormatError::Io`] if the sink cannot be written or sought.
    pub fn write_to(self, mut sink: impl Write + Seek) -> Result<Layout, FormatError> {
        let Self {
            layout,
            model,
            options,
            journal_blocks,
        } = self;
        let feature = options.feature;
        let bytes = Bytes(ByteSink::new(&mut sink));
        let mut writer = Writer::new(&layout, &feature, options, journal_blocks, bytes)?;
        writer.materialize(&model)?;
        writer.extend_to_full_size()?;
        Ok(layout)
    }
}

/// Build the inode model one source implies under these options.
///
/// The model depends on the feature set and the timestamps and not at all on the
/// filesystem's size, which is what lets a fit search build it once and try it against
/// many sizes.
fn model_of(source: impl Source, options: &FormatOptions) -> Result<FsModel, FormatError> {
    let feature = options.feature;
    let mut config = ModelConfig::new(feature, first_user_inode(&feature), options.time);
    config.fixed_time = options.fixed_time;
    Ok(build_model(source, config)?)
}

/// The geometry a size implies, checked against the model it has to hold.
///
/// Everything a size decides before a block is placed: the layout, whether it provides
/// inodes enough for the model, and how large a journal it earns. One function so that
/// planning a named size and probing a candidate one settle these the same way.
pub(crate) fn plan_geometry(
    model: &FsModel,
    options: &FormatOptions,
    size_bytes: u64,
) -> Result<(Layout, Option<u32>), FormatError> {
    let layout = plan_layout(&options.plan_request(size_bytes))?;
    if model.used_inode_count() > layout.total_inodes {
        return Err(FormatError::TooManyInodes {
            needed: model.used_inode_count(),
            available: layout.total_inodes,
        });
    }
    let journal_blocks = journal_size(&layout, options)?;
    Ok((layout, journal_blocks))
}

/// Place the model into this geometry and report the blocks left free, writing nothing.
///
/// This is the format's own placement pass over a sink that keeps nothing: the same
/// allocator, the same calls, in the same order — so what it says about a size is what a
/// format would find, rather than an estimate that could drift from it. The count it
/// returns is the one the superblock's `s_free_blocks_count` would carry.
///
/// # Errors
///
/// Whatever placing the model into this geometry fails with: out of space, a journal with
/// no room, a directory that cannot be packed.
pub(crate) fn free_after_placing(
    layout: &Layout,
    options: &FormatOptions,
    journal_blocks: Option<u32>,
    model: &FsModel,
) -> Result<u64, FormatError> {
    let feature = options.feature;
    let mut writer = Writer::new(layout, &feature, *options, journal_blocks, Discard)?;
    writer.place(model)?;
    Ok(writer.alloc.free_count())
}

/// A timestamp as `seconds.nanoseconds`, so the sub-second half a source can carry is part
/// of the contract rather than rounded out of it.
fn pin_time(t: Timestamp) -> String {
    format!("{}.{}", t.secs, t.nanos)
}

impl FormatOptions {
    /// The contract these options state, as one canonical document: the feature set, and
    /// every option that is a property of *how* images are built rather than of which image
    /// this is.
    ///
    /// The document is the contract. A caller records what this emits — verbatim, as a blob
    /// — and compares it string-for-string on a later build; a difference is drift in what
    /// the build promises, surfaced as a diff a person reads rather than as changed image
    /// bytes nobody notices. Comparison is exactly string equality: the rendering is
    /// deterministic, ordered, and stable across releases of this crate under the version
    /// on its first line.
    ///
    /// ```text
    /// ferrosys-policy-pin 1
    /// compat 0x0000103c has_journal ext_attr resize_inode dir_index orphan_file
    /// incompat 0x000022c2 filetype extent 64bit flex_bg metadata_csum_seed
    /// ro_compat 0x0000046b sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
    /// block_size 4096
    /// inode_size 256
    /// grow max
    /// inodes auto
    /// reserved 500
    /// errors continue
    /// journal auto
    /// hash_version half_md4
    /// hash_signedness unsigned
    /// timestamp_clamp none
    /// ```
    ///
    /// **Nothing here varies with the image.** No UUID, no timestamp, no label, no block
    /// count — those are [`identity_pin`](Self::identity_pin) and
    /// [`FormatPlan::geometry_pin`], and each moves for reasons that are not drift. A
    /// builder that writes many images from one set of constants gets one policy pin for
    /// all of them, so an empty diff between two images' recorded pins means they were
    /// built to the same contract, and a non-empty one always means something changed.
    ///
    /// **This is why the pin is the whole policy and not
    /// [`FeatureSet::pin`](crate::feature::FeatureSet::pin).** The feature set decides five
    /// of the values above. The grow reservation, inode count, reserved share, error
    /// behavior, journal size, and the two hash choices each move bytes too, and a contract
    /// recorded from the feature set alone shows no difference across a change to any of
    /// them — `errors` least visibly of all, since it reaches neither the feature words nor
    /// the geometry.
    ///
    /// What it does *not* catch is a change to the formula behind an option whose name is
    /// unchanged: `grow max` reads the same before and after a change to what `Max`
    /// reserves. [`FormatPlan::geometry_pin`] at a fixed reference size is what covers that.
    ///
    /// Every option is routed to one of the two documents by a single exhaustive
    /// destructure, so a field added to [`FormatOptions`] is a compile error until it is
    /// given a home rather than a silent omission from both.
    #[must_use]
    pub fn policy_pin(&self) -> String {
        let mut policy = String::from("ferrosys-policy-pin 1\n");
        let mut identity = String::new();
        self.push_pins(&mut policy, &mut identity);
        policy
    }

    /// Which image this is, as one canonical document: the identity inputs the superblock
    /// carries verbatim.
    ///
    /// ```text
    /// ferrosys-identity-pin 1
    /// uuid 11111111111111111111111111111111
    /// time 1700000000.0
    /// hash_seed 00000000000000000000000000000000
    /// volume_name 00000000000000000000000000000000
    /// fixed_time none
    /// ```
    ///
    /// This is a separate document because these fields move for a reason that is not
    /// drift. An image is meant to have its own UUID and its own timestamps, so a builder
    /// producing one image per board, or one per run, gets a different identity pin every
    /// time and nothing is wrong. Recording it alongside a [`policy_pin`](Self::policy_pin)
    /// would make every such diff non-empty and the comparison worthless.
    ///
    /// Every field here is recoverable from the image itself — `s_uuid`, `s_mkfs_time`,
    /// `s_hash_seed`, and `s_volume_name` are superblock fields a reader reports — so a
    /// caller that can open the image it built need not record this at all.
    #[must_use]
    pub fn identity_pin(&self) -> String {
        let mut policy = String::new();
        let mut identity = String::from("ferrosys-identity-pin 1\n");
        self.push_pins(&mut policy, &mut identity);
        identity
    }

    /// Route every option into the document it belongs in.
    ///
    /// Both documents are rendered together and the caller keeps the one it asked for. One
    /// function rather than two is what makes the routing total: the destructure below is
    /// exhaustive, so a field added to [`FormatOptions`] cannot be left out of both
    /// documents without failing to compile — which two independent projections, each
    /// ignoring what the other emits, would allow.
    fn push_pins(&self, policy: &mut String, identity: &mut String) {
        // Exhaustive on purpose: see the note above. Do not replace with field accesses.
        let Self {
            uuid,
            time,
            hash_seed,
            hash_version,
            hash_signedness,
            grow,
            inodes,
            reserved,
            volume_name,
            feature,
            errors,
            journal,
            fixed_time,
        } = self;

        // ── Policy: the contract, identical across every image built from these options ──
        //
        // The feature set is emitted in the form `FeatureSet::pin` gives it, so the two
        // documents agree line for line about the fields they share.
        feature.push_pin_body(policy);
        let line = |out: &mut String, key: &str, value: String| {
            out.push_str(key);
            out.push(' ');
            out.push_str(&value);
            out.push('\n');
        };
        line(
            policy,
            "grow",
            match grow {
                GrowReservation::None => "none".to_string(),
                GrowReservation::Max => "max".to_string(),
                GrowReservation::UpTo(bytes) => format!("upto {bytes}"),
            },
        );
        line(
            policy,
            "inodes",
            match inodes {
                InodeCount::Auto => "auto".to_string(),
                InodeCount::BytesPerInode(n) => format!("bytes_per_inode {n}"),
                InodeCount::Count(n) => format!("count {n}"),
            },
        );
        // The exact stored fixed-point share, not a percentage rendered back out of it.
        line(
            policy,
            "reserved",
            reserved.hundredths_of_percent().to_string(),
        );
        line(
            policy,
            "errors",
            match errors {
                ErrorBehavior::Continue => "continue".to_string(),
                ErrorBehavior::RemountReadOnly => "remount_read_only".to_string(),
                ErrorBehavior::Panic => "panic".to_string(),
            },
        );
        line(
            policy,
            "journal",
            match journal {
                JournalSize::Auto => "auto".to_string(),
                JournalSize::Blocks(n) => format!("blocks {n}"),
            },
        );
        line(
            policy,
            "hash_version",
            match hash_version {
                HashVersion::Legacy => "legacy".to_string(),
                HashVersion::HalfMd4 => "half_md4".to_string(),
                HashVersion::Tea => "tea".to_string(),
            },
        );
        line(
            policy,
            "hash_signedness",
            match hash_signedness {
                HashSignedness::Unsigned => "unsigned".to_string(),
                HashSignedness::Signed => "signed".to_string(),
            },
        );
        // Whether the build clamps every inode to one time is a property of the build; the
        // time it clamps to is a property of the image, so the two are recorded apart.
        line(
            policy,
            "timestamp_clamp",
            match fixed_time {
                None => "none".to_string(),
                Some(_) => "fixed".to_string(),
            },
        );

        // ── Identity: which image this is ──
        line(identity, "uuid", hex(uuid));
        line(identity, "time", pin_time(*time));
        line(identity, "hash_seed", hex(hash_seed));
        line(identity, "volume_name", hex(volume_name));
        line(
            identity,
            "fixed_time",
            match fixed_time {
                None => "none".to_string(),
                Some(t) => pin_time(*t),
            },
        );
    }
}

/// The realized geometry as the pin document's lines. See [`FormatPlan::geometry_pin`].
fn push_layout_pin(out: &mut String, layout: &Layout) {
    // Exhaustive on purpose, as above.
    let Layout {
        // The feature set this layout was planned for is the options' own, and states the
        // contract rather than the geometry: it belongs to the policy pin, which emits it.
        feature: _,
        block_size,
        total_blocks,
        blocks_per_group,
        first_data_block,
        group_count,
        inodes_per_group,
        inode_table_blocks,
        total_inodes,
        gdt_blocks,
        reserved_gdt_blocks,
        flex_bg_size,
        max_grow_blocks,
        reserved_blocks,
        groups,
    } = layout;

    for (key, value) in [
        // The block size is geometry as much as it is contract: every count below is in
        // these units, so a document without it states numbers with no scale.
        ("block_size", u64::from(*block_size)),
        ("total_blocks", *total_blocks),
        ("blocks_per_group", u64::from(*blocks_per_group)),
        ("first_data_block", u64::from(*first_data_block)),
        ("group_count", u64::from(*group_count)),
        ("inodes_per_group", u64::from(*inodes_per_group)),
        ("inode_table_blocks", u64::from(*inode_table_blocks)),
        ("total_inodes", u64::from(*total_inodes)),
        ("gdt_blocks", u64::from(*gdt_blocks)),
        ("reserved_gdt_blocks", u64::from(*reserved_gdt_blocks)),
        ("flex_bg_size", u64::from(*flex_bg_size)),
        ("max_grow_blocks", *max_grow_blocks),
        ("reserved_blocks", *reserved_blocks),
    ] {
        out.push_str(key);
        out.push(' ');
        out.push_str(&value.to_string());
        out.push('\n');
    }

    // One line for a table with as many rows as the filesystem has groups: the count, and
    // a checksum over every field of every row. A placement that moves changes the digest.
    let digest = groups_digest(groups);
    out.push_str(&format!("groups {group_count} crc32c 0x{digest:08x}\n"));
}

/// A `crc32c` over the per-group placements, field by field in group order.
///
/// Each field is fed in its widest form and in a fixed order, so the digest depends on the
/// placements and on nothing about how they happen to be stored.
fn groups_digest(groups: &[GroupLayout]) -> u32 {
    let mut crc = !0u32;
    for group in groups {
        // Exhaustive on purpose, as above: a placement field added here must change the
        // digest, so leaving it out has to be a compile error.
        let GroupLayout {
            index,
            start_block,
            block_count,
            has_super,
            block_bitmap,
            inode_bitmap,
            inode_table,
        } = group;
        crc = crc32c(crc, &u64::from(*index).to_le_bytes());
        crc = crc32c(crc, &start_block.to_le_bytes());
        crc = crc32c(crc, &u64::from(*block_count).to_le_bytes());
        crc = crc32c(crc, &[u8::from(*has_super)]);
        crc = crc32c(crc, &block_bitmap.to_le_bytes());
        crc = crc32c(crc, &inode_bitmap.to_le_bytes());
        crc = crc32c(crc, &inode_table.to_le_bytes());
    }
    !crc
}

/// The journal's size in blocks for a planned geometry, or `None` under a feature set that
/// carries no journal.
///
/// This is decided at plan time because it is decided by the geometry and the options alone,
/// and because both of its failures are failures of the *request* rather than of the
/// writing: a filesystem with no room for the smallest journal jbd2 accepts, and an explicit
/// size below that minimum or past what the feature set can describe. Discovering either
/// after the destination was opened would mean a truncated file and no filesystem.
///
/// # Errors
///
/// [`FormatError::FilesystemTooSmallForJournal`] if no journal fits;
/// [`FormatError::JournalTooSmall`] if an explicit size is below the jbd2 minimum;
/// [`FormatError::LargeFileWithoutFeature`] if an explicit size makes the log a large file
/// on a filesystem without `large_file`.
fn journal_size(layout: &Layout, options: &FormatOptions) -> Result<Option<u32>, FormatError> {
    if !options.feature.has_journal() {
        return Ok(None);
    }
    let minimum = journal::MIN_JOURNAL_BLOCKS;
    let blocks = match options.journal {
        JournalSize::Auto => journal::default_journal_blocks(layout.total_blocks).ok_or(
            FormatError::FilesystemTooSmallForJournal {
                blocks: layout.total_blocks,
                minimum,
            },
        )?,
        JournalSize::Blocks(n) if n >= minimum => n,
        JournalSize::Blocks(n) => {
            return Err(FormatError::JournalTooSmall {
                requested: n,
                minimum,
            });
        }
    };
    // The journal is a regular file, so a log an explicit size pushed to 2 GiB needs
    // `large_file` like any other. The heuristic never reaches that far; an explicit block
    // count can, and the conflict is stated rather than written to disk.
    let size = u64::from(blocks) * u64::from(options.feature.block_size);
    if size >= LARGE_FILE_MIN_SIZE && !options.feature.has_large_file() {
        return Err(FormatError::LargeFileWithoutFeature {
            what: "journal",
            size,
        });
    }
    Ok(Some(blocks))
}

/// Where a format's bytes go.
///
/// There are two, and the second is what a fit search runs on: [`Bytes`] writes to a
/// seekable destination, [`Discard`] keeps nothing at all. Which one is in place changes
/// no placement decision — the same allocator calls happen in the same order either way —
/// so a search that only places settles exactly what a write would find out about a size,
/// without writing a block or reading a file.
trait Sink {
    /// Take `bytes` at absolute byte offset `offset`.
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), FormatError>;

    /// Whether the bytes are kept. A sink that discards them lets a file's contents go
    /// unread, which is the difference between a fit search costing a placement and
    /// costing a whole format.
    fn keeps_bytes(&self) -> bool;

    /// Make the destination as long as the filesystem, for the case where its final
    /// blocks hold nothing and so were never written.
    fn extend_to(&mut self, size: u64) -> Result<(), FormatError>;
}

/// The sink that writes: bytes go to a seekable destination at absolute offsets, and
/// nothing is ever read back. The destination itself is [`ByteSink`], which every family's
/// materializer writes through; what this adds is the fit search's question of whether the
/// bytes are being kept.
struct Bytes<W>(ByteSink<W>);

impl<W: Write + Seek> Sink for Bytes<W> {
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), FormatError> {
        Ok(self.0.write_at(offset, bytes)?)
    }

    fn keeps_bytes(&self) -> bool {
        true
    }

    fn extend_to(&mut self, size: u64) -> Result<(), FormatError> {
        Ok(self.0.extend_to(size)?)
    }
}

/// The sink that keeps nothing, so what a format decides can be found out without a
/// destination to decide it into.
struct Discard;

impl Sink for Discard {
    fn write_at(&mut self, _offset: u64, _bytes: &[u8]) -> Result<(), FormatError> {
        Ok(())
    }

    fn keeps_bytes(&self) -> bool {
        false
    }

    fn extend_to(&mut self, _size: u64) -> Result<(), FormatError> {
        Ok(())
    }
}

/// The mutable state of one format: the destination, the allocator, and the inodes
/// as they are built.
struct Writer<'a, S> {
    layout: &'a Layout,
    feature: &'a FeatureSet,
    options: FormatOptions,
    alloc: Allocator,
    /// Where the bytes go.
    sink: S,
    block_size: usize,
    /// The metadata-checksum seam: [`Crc32c`] when `metadata_csum` is on, otherwise
    /// [`NullCsum`], which zeroes every checksum. Held dynamically so the per-object
    /// checksum construction sites read the same seam regardless of which is active.
    csum: Box<dyn Checksummer>,
    /// The directory-layout seam: [`HtreeDir`] when `dir_index` is on, otherwise
    /// [`LinearDir`]. A directory small enough to need no index is packed linearly
    /// by either.
    dir: Box<dyn DirLayout>,
    /// The serialized inodes, by number.
    inodes: BTreeMap<u32, Inode>,
    /// The primary descriptor table, retained to copy into the backup groups.
    gdt_bytes: Vec<u8>,
    /// The journal inode's block map and size, backed up into the superblock's
    /// `s_jnl_blocks`. `None` until the journal is materialized, and when the feature
    /// set carries no journal.
    journal_backup: Option<[u32; 17]>,
    /// How many blocks the journal takes, decided at plan time, or `None` when the feature
    /// set carries no journal. The size is a pure function of the geometry and the options,
    /// so it is settled before the destination is opened — a journal a filesystem has no
    /// room for must not be discovered half way through writing one.
    journal_blocks: Option<u32>,
}

impl<'a, S: Sink> Writer<'a, S> {
    fn new(
        layout: &'a Layout,
        feature: &'a FeatureSet,
        options: FormatOptions,
        journal_blocks: Option<u32>,
        sink: S,
    ) -> Result<Self, FormatError> {
        let block_size = layout.block_size as usize;
        let csum: Box<dyn Checksummer> = if feature.has_metadata_csum() {
            Box::new(Crc32c::new(&options.uuid))
        } else {
            Box::new(NullCsum)
        };
        let dir: Box<dyn DirLayout> = if feature.has_dir_index() {
            Box::new(HtreeDir {
                seed: options.hash_seed,
                version: options.hash_version,
                signedness: options.hash_signedness,
                checksums: feature.has_metadata_csum(),
            })
        } else {
            Box::new(LinearDir {
                tail_len: if feature.has_metadata_csum() {
                    DIR_TAIL_LEN
                } else {
                    0
                },
            })
        };
        Ok(Self {
            layout,
            feature,
            options,
            alloc: Allocator::new(layout)?,
            sink,
            block_size,
            csum,
            dir,
            inodes: BTreeMap::new(),
            gdt_bytes: Vec::new(),
            journal_backup: None,
            journal_blocks,
        })
    }

    /// Place every block the filesystem occupies: the source's inodes, then the ones the
    /// filesystem provides for itself.
    ///
    /// This is the whole of what consumes free space, so it is what decides whether a size
    /// works — and it is all a fit search runs. Once it returns, the allocator holds the
    /// filesystem's settled free state and every remaining pass only reads it.
    fn place(&mut self, model: &FsModel) -> Result<(), FormatError> {
        for minode in model.inodes.values() {
            let inode = self.materialize_inode(minode)?;
            self.inodes.insert(minode.number, inode);
        }
        self.materialize_reserved_inodes()
    }

    fn materialize(&mut self, model: &FsModel) -> Result<(), FormatError> {
        // Data first: allocating file, directory, symlink, and resize-map blocks
        // settles the allocator before free counts and bitmaps are read back.
        self.place(model)?;

        // Then the fixed structures, in any order — they read the settled state.
        self.write_inode_tables()?;
        self.write_bitmaps_and_descriptors(model)?;
        self.write_superblocks(model)?;
        Ok(())
    }

    /// Write `bytes` at absolute byte offset `offset`.
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), FormatError> {
        self.sink.write_at(offset, bytes)
    }

    /// Make the destination as long as the filesystem, for the case where its final
    /// blocks hold nothing and so were never written.
    fn extend_to_full_size(&mut self) -> Result<(), FormatError> {
        let size = self.layout.total_blocks * u64::from(self.layout.block_size);
        self.sink.extend_to(size)
    }

    /// A blank inode carrying the extra area this filesystem's inode size affords.
    ///
    /// Every inode this writer builds starts here, so the extra area is decided in one
    /// place from `s_inode_size` rather than assumed. At 128 bytes there is no extra
    /// area, and the inode carries no creation time, no sub-second timestamps, no
    /// inline attributes, and no `i_checksum_hi`.
    fn new_inode(&self) -> Inode {
        Inode::empty(self.feature.inode_size)
    }

    /// Stamp a reserved structural inode's four timestamps (resize, journal, orphan).
    /// The fixed-time clamp forces the value when set, otherwise the format time stands
    /// — the same resolution the model gives a source-derived inode, so the clamp
    /// reaches every inode and leaves no source-independent time unforced.
    fn stamp_structural_times(&self, inode: &mut Inode) {
        let t = self.options.fixed_time.unwrap_or(self.options.time);
        inode.atime = t;
        inode.ctime = t;
        inode.mtime = t;
        inode.crtime = t;
    }

    /// Build and write one model inode, returning its on-disk form.
    fn materialize_inode(&mut self, minode: &ModelInode) -> Result<Inode, FormatError> {
        let mut inode = self.new_inode();
        inode.mode = minode.mode;
        inode.uid = minode.uid;
        inode.gid = minode.gid;
        inode.links_count = minode.links_count;
        inode.atime = minode.atime;
        inode.ctime = minode.ctime;
        inode.mtime = minode.mtime;
        inode.crtime = minode.crtime;

        match &minode.content {
            Content::Directory(children) => {
                let entries: Vec<DirEntry> = children
                    .iter()
                    .map(|c| DirEntry {
                        inode: c.inode,
                        file_type: c.file_type,
                        name: c.name.clone(),
                    })
                    .collect();
                let mut blocks = self.dir.build_blocks(&entries, self.block_size)?;
                // /lost+found is preallocated to 16 KiB so a repair has room to
                // reconnect orphaned inodes. Its entries fit in one block, so it is
                // never indexed and the padding is plain entry blocks.
                if minode.number == LOST_FOUND_INO {
                    let min_blocks = (16 * 1024usize).div_ceil(self.block_size);
                    while blocks.len() < min_blocks {
                        blocks.push(DirBlock {
                            bytes: self.empty_dir_block(),
                            kind: DirBlockKind::Entries,
                        });
                    }
                }
                let indexed = blocks
                    .iter()
                    .any(|b| matches!(b.kind, DirBlockKind::Index { .. }));
                self.write_dir_checksums(minode.number, &mut blocks);
                let bytes: Vec<Vec<u8>> = blocks.into_iter().map(|b| b.bytes).collect();
                self.place_blocks(minode.number, &mut inode, bytes.iter())?;
                if indexed {
                    inode.flags |= InodeFlags::INDEX;
                }
            }
            Content::File(content) => {
                // The block count comes from the length the content declares, which is
                // known without reading it. That is what lets a fit search place a file
                // without opening it — and it is the same count the bytes below chunk
                // into, because a content that read short is a file that changed since the
                // source named it, which `FileContent::for_each_window` refuses rather than
                // reports.
                let count = content.len().div_ceil(self.block_size as u64);
                let physical = self.map_data_blocks(minode.number, &mut inode, count, "file")?;
                if self.sink.keeps_bytes() {
                    // The bytes are read here, at placement, rather than held from the
                    // moment the source was built, and a window at a time: a file costs one
                    // window of memory and the runs its blocks form, however large it is. A
                    // source that supplied them owned pays nothing extra — each window is
                    // lent from what it holds. The windows come in order and every one but
                    // the last is a whole number of blocks, so one walk of the blocks serves
                    // them all and only the file's final block is ever padded.
                    let block_size = self.block_size;
                    let mut blocks = physical.blocks();
                    content.for_each_window(|_, window| {
                        for (data, phys) in block_chunks(window, block_size).zip(&mut blocks) {
                            self.write_block(phys, data.as_ref())?;
                        }
                        Ok::<(), FormatError>(())
                    })?;
                }
                inode.size = content.len();
            }
            Content::SlowSymlink(target) => {
                let block_size = self.block_size;
                self.place_blocks(minode.number, &mut inode, block_chunks(target, block_size))?;
                inode.size = target.len() as u64;
            }
            Content::FastSymlink(target) => {
                // The target lives inline in the block area; no data block, no
                // extents flag.
                inode.block[..target.len()].copy_from_slice(target);
                inode.size = target.len() as u64;
            }
            Content::Device { major, minor } => {
                // A device node stores its number in the block area and maps no
                // blocks; the file type (char vs block) is already in the mode.
                let (b0, b1) = encode_device(*major, *minor);
                put_u32(&mut inode.block, 0, b0);
                put_u32(&mut inode.block, 4, b1);
            }
            Content::Special => {
                // A FIFO or socket has no data and no block map.
            }
        }
        self.encode_xattrs(&mut inode, &minode.xattrs)?;
        Ok(inode)
    }

    /// Attach an inode's extended attributes: as many as fit go inline in the inode,
    /// and the rest spill to a freshly allocated external block charged to the inode.
    /// The split is [`split_for_storage`]'s, the same one the model validated against,
    /// so a set that reaches here always fits.
    ///
    /// The model also settled that this filesystem holds attributes at all: a non-empty
    /// set on a feature word without `ext_attr` is refused there, naming the entry, so
    /// what reaches here is a set the emitted feature words describe.
    fn encode_xattrs(&mut self, inode: &mut Inode, xattrs: &[Xattr]) -> Result<(), FormatError> {
        if xattrs.is_empty() {
            return Ok(());
        }
        // What is left of the inode after the classic fields and the extra area. A
        // 128-byte inode has none, so its attributes all spill.
        let region_len = inode.inline_xattr_capacity(self.feature.inode_size);
        let (inline, spilled) = split_for_storage(xattrs, region_len);
        if !inline.is_empty() {
            inode.inline_xattr = encode_inline(&inline, region_len)
                .expect("the inline side of the split fits the region by construction");
        }
        if !spilled.is_empty() {
            let mut block = encode_block(&spilled, self.block_size);
            let phys = self.alloc.allocate_one()?;
            if self.csum.scheme().writes_object_checksums() {
                // `h_checksum` is zero in the encoded block, which is how it participates
                // in its own checksum.
                let c = recipe::xattr_block(&*self.csum, phys, &block);
                put_u32(&mut block, XATTR_CHECKSUM_OFFSET, c);
            }
            self.write_block(phys, &block)?;
            inode.file_acl = phys;
            // Through the same gate every other charge takes. This is the one charge
            // added after the contents were totalled, and adding it unchecked is how a
            // count within one block of the cap would serialize a wrapped low half
            // beside a high half the feature words deny.
            self.charge_sectors(
                inode,
                inode.blocks + self.sectors_per_block(),
                "extended attributes",
            )?;
        }
        Ok(())
    }

    /// Allocate `count` data blocks, map them at the inode, and return the physical block
    /// each logical block `0..count` landed on, in order.
    ///
    /// The extent family roots an extent tree; the block-mapped family fills the classic
    /// direct-and-indirect map. Either way the map's own blocks — extent nodes, indirect
    /// blocks — are allocated here too and counted into `i_blocks`, so this is every block
    /// an inode's contents cost. A count of zero still gets an empty extent header (extent
    /// family) or an empty map (block-mapped family), so the inode is valid.
    ///
    /// The map is placed and the data written separately, which is what lets a file whose
    /// blocks are mostly zero (the journal) write only the blocks it must — and what lets a
    /// fit search take this path and stop, since nothing below it moves a block.
    fn map_data_blocks(
        &mut self,
        ino: u32,
        inode: &mut Inode,
        count: u64,
        what: &'static str,
    ) -> Result<BlockRuns, FormatError> {
        // Refused from the count, before a block is allocated, so the bound fires without a
        // filesystem large enough to hold the file existing. The exact charge is checked
        // again once the mapping's own blocks are known, since those only add to it.
        self.check_sectors(count * self.sectors_per_block(), what)?;
        if self.feature.has_extents() {
            inode.flags = InodeFlags::EXTENTS;
            let ranges = self.alloc.allocate(count)?;
            let meta = self.root_extent_tree(ino, inode, &ranges)?;
            self.charge_sectors(inode, (count + meta) * self.sectors_per_block(), what)?;
            Ok(BlockRuns::from_ranges(ranges))
        } else {
            self.build_classic_map(inode, count, what)
        }
    }

    /// Allocate blocks for `blocks`, write their contents, and map them at the inode.
    ///
    /// This is [`map_data_blocks`](Self::map_data_blocks) for content already in memory: a
    /// directory's packed blocks, or a symlink target too long to live in the inode. A
    /// regular file's contents take the mapping call directly, so its bytes are read only
    /// when the sink keeps them.
    fn place_blocks<B: AsRef<[u8]>>(
        &mut self,
        ino: u32,
        inode: &mut Inode,
        blocks: impl ExactSizeIterator<Item = B>,
    ) -> Result<(), FormatError> {
        // The blocks are consumed once, as they are written, so a caller may hand over
        // chunks that borrow their source rather than a materialized copy of it.
        let count = blocks.len();
        let physical = self.map_data_blocks(ino, inode, count as u64, "file")?;
        for (data, phys) in blocks.zip(physical.blocks()) {
            self.write_block(phys, data.as_ref())?;
        }
        if inode.size == 0 {
            inode.size = (count * self.block_size) as u64;
        }
        Ok(())
    }

    /// Lay out a classic (ext2/ext3) block map for `n` logical data blocks: allocate the
    /// data and the indirect blocks that map them, fill the inode's fifteen-word block
    /// area, and write every indirect block. Returns the physical blocks of logical blocks
    /// `0..n`, in order, for the caller to write the data into — the map's
    /// structure and its data are placed in one pass but written separately, so a file
    /// whose blocks are mostly zero (the journal) writes only the blocks it must.
    ///
    /// Indirect blocks are allocated just before the first data block each maps: the map
    /// is a pre-order walk of the block tree with every node allocated the moment it is
    /// first entered, which is the order `mke2fs` writes and the interleaving that fixes
    /// each block's number. Sets `inode.flags` and `inode.blocks`; leaves `inode.size` to
    /// the caller.
    fn build_classic_map(
        &mut self,
        inode: &mut Inode,
        n: u64,
        what: &'static str,
    ) -> Result<BlockRuns, FormatError> {
        // Reached directly by the journal as well as through `map_data_blocks`, so the
        // charge bound is checked here too, before a block is allocated.
        self.check_sectors(n * self.sectors_per_block(), what)?;
        // The map's words run out at three levels of indirection, and the loop below simply
        // stops when they do — so a file past the reach would be mapped short while its size
        // claimed the whole length, which is a file whose tail is neither mapped nor written.
        // Refused here instead, as the extent path refuses a file past what a 32-bit logical
        // block number addresses.
        let reach = classic_map_reach(self.block_size);
        if n > reach {
            return Err(FormatError::FileTooLargeForBlockMap {
                blocks: n,
                limit: reach,
                block_size: self.block_size as u32,
            });
        }
        inode.flags = InodeFlags::NONE;
        let mut physical = BlockRuns::default();
        let mut meta = 0u64;

        // Twelve direct pointers: logical blocks 0..11 in words 0..11.
        for slot in 0..n.min(DIRECT_BLOCKS as u64) {
            let phys = self.alloc.allocate_one()?;
            physical.push(phys);
            put_u32(&mut inode.block, slot as usize * 4, map_block(phys)?);
        }

        // Single-, double-, and triple-indirect trees hang off words 12, 13, 14. Each is
        // built only when the data reaches it, and allocated at the moment it is entered.
        for level in 1..=INDIRECT_LEVELS {
            if physical.len() >= n {
                break;
            }
            let root = self.build_indirect(level as u32, n, &mut physical, &mut meta)?;
            put_u32(
                &mut inode.block,
                (DIRECT_BLOCKS + level - 1) * 4,
                map_block(root)?,
            );
        }

        self.charge_sectors(inode, (n + meta) * self.sectors_per_block(), what)?;
        Ok(physical)
    }

    /// Build one indirect block `level` levels above the data — `1` single, `2` double,
    /// `3` triple — mapping data blocks from where `physical` left off up to `n` total.
    /// The indirect block itself is allocated first, before any child, so the allocation
    /// order is the pre-order walk `mke2fs` writes; its slots then fill left to right with
    /// data blocks (level 1) or deeper indirect blocks (levels 2 and 3) until this subtree
    /// is full or the data runs out. Appends each data block's physical number to
    /// `physical` and counts every indirect block it roots into `meta`. Returns the
    /// indirect block's own physical number.
    fn build_indirect(
        &mut self,
        level: u32,
        n: u64,
        physical: &mut BlockRuns,
        meta: &mut u64,
    ) -> Result<u64, FormatError> {
        let ind_block = self.alloc.allocate_one()?;
        *meta += 1;
        let ppb = self.block_size / 4;
        let mut ptrs = vec![0u8; self.block_size];
        for slot in 0..ppb {
            if physical.len() >= n {
                break;
            }
            let child = if level == 1 {
                let phys = self.alloc.allocate_one()?;
                physical.push(phys);
                phys
            } else {
                self.build_indirect(level - 1, n, physical, meta)?
            };
            put_u32(&mut ptrs, slot * 4, map_block(child)?);
        }
        self.write_block(ind_block, &ptrs)?;
        Ok(ind_block)
    }

    /// Map `ranges` with an extent tree rooted in `inode`'s inline block area,
    /// spilling into external node blocks when the leaves outgrow it. Returns the
    /// node blocks the tree consumed — metadata blocks `i_blocks` counts alongside
    /// the file's data.
    fn root_extent_tree(
        &mut self,
        ino: u32,
        inode: &mut Inode,
        ranges: &[BlockRange],
    ) -> Result<u64, FormatError> {
        let inline_capacity = node_capacity(Inode::BLOCK_BYTES);
        let node_capacity = node_capacity(self.block_size);
        let leaves = build_leaves(ranges)?;

        let shape = plan_tree(leaves.len(), inline_capacity, node_capacity)?;
        let mut node_blocks = Vec::with_capacity(shape.node_blocks);
        for _ in 0..shape.node_blocks {
            node_blocks.push(self.alloc.allocate_one()?);
        }
        let tree = build_tree(&leaves, inline_capacity, node_capacity, &node_blocks)?;

        for node in &tree.nodes {
            let mut buf = vec![0u8; self.block_size];
            write_node(&node.node, node_capacity, &mut buf)?;
            self.write_extent_node_checksum(ino, &mut buf);
            self.write_block(node.block, &buf)?;
        }
        write_node(&tree.root, inline_capacity, &mut inode.block)?;
        Ok(shape.node_blocks as u64)
    }

    /// Fill an external extent node's reserved checksum tail. Under `metadata_csum`
    /// the tail's crc32c covers the node's header and its whole entry area, seeded
    /// from the filesystem seed, the owning inode's number, and its generation (zero
    /// here). With checksums off the tail stays zero, as the node reserved it.
    fn write_extent_node_checksum(&self, ino: u32, buf: &mut [u8]) {
        if !self.csum.scheme().writes_object_checksums() {
            return;
        }
        let tail = tail_offset(self.block_size);
        // Generation zero: this crate writes no inode with a generation, so the identity a
        // node is bound to is its owner's number alone.
        let c = self
            .csum
            .crc32c(recipe::inode_seed(&*self.csum, ino, 0), &buf[..tail]);
        put_u32(buf, tail, c);
    }

    /// A directory block holding no entries: one empty slot spanning the block up to
    /// the reserved checksum tail. Used to pad a preallocated directory.
    fn empty_dir_block(&self) -> Vec<u8> {
        // The block reserves a checksum tail only under `metadata_csum`; without it the
        // empty slot spans the whole block, as mke2fs writes a non-checksummed directory.
        let tail_len = if self.csum.scheme().writes_object_checksums() {
            DIR_TAIL_LEN
        } else {
            0
        };
        let usable = self.block_size - tail_len;
        let mut block = vec![0u8; self.block_size];
        put_u32(&mut block, 0, 0); // inode 0 — an unused slot
        put_u16(&mut block, 4, usable as u16); // rec_len spans to the tail (or block end)
        if tail_len != 0 {
            // The tail slice is exactly DIR_TAIL_LEN, so this cannot fail.
            write_dir_tail(&mut block[usable..usable + DIR_TAIL_LEN], 0)
                .expect("tail slice is exactly DIR_TAIL_LEN");
        }
        block
    }

    /// Fill each directory block's reserved tail checksum. Under `metadata_csum` the
    /// tail's crc32c is seeded from the filesystem seed, the owning directory's inode
    /// number, and its generation (zero here). With checksums off the tails stay zero,
    /// exactly as they were reserved.
    ///
    /// A block of entries is covered up to its twelve-byte tail. An index block is
    /// covered only through the entries it actually holds, followed by its own
    /// eight-byte tail with the checksum field zeroed — the two tails differ in size,
    /// in position, and in what they cover.
    fn write_dir_checksums(&self, dir_ino: u32, blocks: &mut [DirBlock]) {
        if !self.csum.scheme().writes_object_checksums() {
            return;
        }
        // Generation zero, as for an extent node above.
        let seed = recipe::inode_seed(&*self.csum, dir_ino, 0);
        for block in blocks {
            let mut c = seed;
            match block.kind {
                DirBlockKind::Entries => {
                    let covered = self.block_size - DIR_TAIL_LEN;
                    c = self.csum.crc32c(c, &block.bytes[..covered]);
                    put_u32(&mut block.bytes, self.block_size - 4, c);
                }
                // Not one of `csum`'s shared recipes, and deliberately: what is folded here
                // is a tail this writer is about to write, whose reserved word it knows to be
                // zero, where the reader folds the tail it *found* so that an index block a
                // foreign tool wrote still verifies. One function could not be both.
                DirBlockKind::Index {
                    count_offset,
                    limit,
                } => {
                    let count = get_u16(&block.bytes, count_offset + 2) as usize;
                    let covered = count_offset + DX_ENTRY_LEN * count;
                    c = self.csum.crc32c(c, &block.bytes[..covered]);
                    // The tail folds in as `dt_reserved` followed by four zero bytes
                    // standing in for `dt_checksum`. Both words are zero here — the
                    // reserved one because `write_dx_tail` writes it so, and the
                    // checksum because it is the field being computed — so the whole
                    // tail folds as zeros, and it is folded before it is written.
                    c = self.csum.crc32c(c, &[0u8; DX_TAIL_LEN]);
                    write_dx_tail(&mut block.bytes, count_offset, limit, c)
                        .expect("the index block reserved its tail");
                }
            }
        }
    }

    /// Write the inodes the filesystem provides for itself: the empty bad-blocks and
    /// unused reserved inodes, the resize inode with its reserved-descriptor map, the
    /// journal, and the orphan file.
    fn materialize_reserved_inodes(&mut self) -> Result<(), FormatError> {
        // Reserved inodes with no content: bad-blocks (1) and the unused range (3-6,
        // 9-10). The journal (8) is empty only when `has_journal` is off.
        for n in [1u32, 3, 4, 5, 6, 9, 10] {
            self.inodes.insert(n, self.new_inode());
        }
        let resize = if self.feature.has_resize_inode() {
            self.materialize_resize_inode()?
        } else {
            self.new_inode()
        };
        self.inodes.insert(RESIZE_INO, resize);
        let journal = match self.journal_blocks {
            Some(blocks) => self.materialize_journal_inode(blocks)?,
            None => self.new_inode(),
        };
        self.inodes.insert(JOURNAL_INO, journal);
        // The orphan file is not a reserved inode: it exists only with the feature, and
        // when it does not, inode 12 is the source's first entry instead. The model was
        // built to leave it free, so finding an entry there means the two disagree about
        // which inodes the feature set claims — the image is refused rather than written
        // with the entry displaced.
        if self.feature.has_orphan_file() {
            if self.inodes.contains_key(&ORPHAN_INO) {
                return Err(FormatError::OrphanInodeInUse { inode: ORPHAN_INO });
            }
            let orphan = self.materialize_orphan_inode()?;
            self.inodes.insert(ORPHAN_INO, orphan);
        }
        Ok(())
    }

    /// Build the orphan file (inode 12): a regular extent-mapped file holding the
    /// inodes awaiting deletion, of which a fresh filesystem has none — so every entry
    /// is zero and each block carries only its magic-and-checksum tail.
    fn materialize_orphan_inode(&mut self) -> Result<Inode, FormatError> {
        let blocks = orphan_file_blocks(self.layout.total_blocks);
        // One contiguous run keeps the file's extents inline in the inode, as its size
        // always fits a single extent.
        let ranges = match self.alloc.allocate_contiguous(u64::from(blocks)) {
            Some(range) => vec![range],
            None => self.alloc.allocate(u64::from(blocks))?,
        };

        let mut inode = self.new_inode();
        inode.mode = 0o100600;
        inode.links_count = 1;
        self.stamp_structural_times(&mut inode);
        inode.flags = InodeFlags::EXTENTS;
        let meta = self.root_extent_tree(ORPHAN_INO, &mut inode, &ranges)?;
        inode.size = u64::from(blocks) * u64::from(self.feature.block_size);
        self.charge_sectors(
            &mut inode,
            (u64::from(blocks) + meta) * self.sectors_per_block(),
            "orphan file",
        )?;

        // The image starts zeroed and every entry is zero, so each block needs only its
        // tail written. The checksum covers the entry array — the block but for that
        // tail — behind the file's identity and the block's own number, so it differs
        // from block to block even though their contents are identical.
        let entries_len = orphan_entries_len(self.block_size);
        let entries = vec![0u8; entries_len];
        for block in ranges.iter().flat_map(|r| r.start..r.start + r.len) {
            let c =
                recipe::orphan_block(&*self.csum, ORPHAN_INO, inode.generation, block, &entries);
            let offset = block * u64::from(self.layout.block_size) + entries_len as u64;
            self.write_at(offset, &orphan_tail_bytes(c))?;
        }
        Ok(inode)
    }

    /// Build the journal inode (inode 8): a regular extent-mapped file whose first
    /// block is the jbd2 superblock and whose remaining blocks are the empty log. The
    /// journal maps as one extent whenever a contiguous run is free, falling back to a
    /// fragmented allocation otherwise.
    fn materialize_journal_inode(&mut self, blocks: u32) -> Result<Inode, FormatError> {
        let size = u64::from(blocks) * u64::from(self.feature.block_size);
        // The image starts zeroed, so only the first block — the jbd2 superblock — needs
        // writing; the rest of the log is already the zero blocks it must be.
        let sb = journal::build_superblock(&journal::JournalParams::new(
            self.feature.block_size,
            blocks,
            self.options.uuid,
        ));

        let mut inode = self.new_inode();
        inode.mode = 0o100600;
        inode.links_count = 1;
        self.stamp_structural_times(&mut inode);

        if self.feature.has_extents() {
            // Prefer one contiguous run so the journal maps inline; the sparse_super
            // backup gaps leave runs long enough for any journal size the heuristic picks.
            let ranges = match self.alloc.allocate_contiguous(u64::from(blocks)) {
                Some(range) => vec![range],
                None => self
                    .alloc
                    .allocate(u64::from(blocks))
                    .map_err(journal_space)?,
            };
            // The log's first block, which is the jbd2 superblock. Indexing the first range
            // cannot be out of bounds: `blocks` is at least `MIN_JOURNAL_BLOCKS` — the size
            // is checked against that floor before this is called, whether it came from the
            // heuristic or from an explicit `JournalSize::Blocks` — so the allocation is of
            // a nonzero count, and an allocation of a nonzero count either yields at least
            // one range or fails.
            self.write_block(ranges[0].start, &sb)?;
            inode.flags = InodeFlags::EXTENTS;
            let meta = self.root_extent_tree(JOURNAL_INO, &mut inode, &ranges)?;
            self.charge_sectors(
                &mut inode,
                (u64::from(blocks) + meta) * self.sectors_per_block(),
                "journal",
            )?;
        } else {
            // ext3: the journal maps through the classic block map, its indirect blocks
            // interleaved with the log the same way `mke2fs` writes them. Only the first
            // block, the jbd2 superblock, is written; the rest stays zeroed.
            let physical = self
                .build_classic_map(&mut inode, u64::from(blocks), "journal")
                .map_err(journal_space)?;
            // As above: `blocks` is at least `MIN_JOURNAL_BLOCKS`, and the map holds every
            // logical block, so there is a first one.
            let first = physical
                .blocks()
                .next()
                .expect("a journal of at least the minimum length maps a first block");
            self.write_block(first, &sb)?;
        }
        inode.size = size;

        // Back the block map up into the superblock: the 15 i_block words, then the
        // high and low halves of the size.
        let mut backup = [0u32; 17];
        for (i, word) in inode.block.as_chunks::<4>().0.iter().enumerate() {
            backup[i] = get_u32(word, 0);
        }
        backup[15] = (inode.size >> 32) as u32;
        backup[16] = inode.size as u32;
        self.journal_backup = Some(backup);
        Ok(inode)
    }

    /// Build the resize inode (inode 7): a classic double-indirect map whose indirect
    /// blocks are the primary reserved descriptor blocks, each listing the matching
    /// reserved descriptor block in every backup group.
    ///
    /// The double-indirect block exists whenever the `resize_inode` feature does, even
    /// with nothing reserved: an image carrying the feature with an empty resize inode
    /// is not a valid ext4 filesystem. A zero reservation simply leaves the map blank.
    fn materialize_resize_inode(&mut self) -> Result<Inode, FormatError> {
        let mut inode = self.new_inode();
        inode.mode = 0o100600;
        inode.links_count = 1;
        self.stamp_structural_times(&mut inode);
        // No extents: the resize inode uses the classic block map.
        inode.flags = InodeFlags::NONE;

        let reserved = self.layout.reserved_gdt_blocks;
        let bs = self.block_size;
        let fdb = u64::from(self.layout.first_data_block);
        let gdt = u64::from(self.layout.gdt_blocks);
        let backups = self.layout.backup_groups();

        // A freshly allocated double-indirect block indexes the primary reserved
        // descriptor blocks. A descriptor block's slot is its absolute table slot taken
        // modulo the slots one block holds, so a reservation may fill the map: with
        // `gdt` at 1 and `block_size / 4` blocks reserved, the last slot written wraps
        // back to 0, which the primary descriptor table's own slot never claims. The
        // reservation is therefore bounded by the map's `block_size / 4` slots alone,
        // not by the table's slots plus the reservation's.
        let slots = u64::from(self.layout.block_size) / 4;
        let dind_block = self.alloc.allocate_one()?;
        let mut dind = vec![0u8; bs];
        for r in 0..u64::from(reserved) {
            let primary = fdb + 1 + gdt + r; // the r-th primary reserved GDT block
            let slot = (gdt + r) % slots;
            put_u32(&mut dind, (slot * 4) as usize, map_block(primary)?);
        }
        self.write_block(dind_block, &dind)?;

        // Each primary reserved descriptor block, used as an indirect block, lists
        // the matching reserved descriptor block in every backup group.
        for r in 0..u64::from(reserved) {
            let ind_block = fdb + 1 + gdt + r;
            let mut ind = vec![0u8; bs];
            for (j, &bg) in backups.iter().enumerate() {
                let start = fdb + u64::from(bg) * u64::from(self.layout.blocks_per_group);
                let backup_resv = start + 1 + gdt + r;
                put_u32(&mut ind, j * 4, map_block(backup_resv)?);
            }
            self.write_block(ind_block, &ind)?;
        }

        // The map hangs entirely off the double-indirect slot (index 13). The size records
        // the classic map's reach rather than the blocks reserved, which is what makes the
        // resize inode a large file at a 4096-byte block — the pairing `validate` enforces.
        inode.size = resize_inode_size(self.layout.block_size);
        let data_blocks = 1 + u64::from(reserved) + u64::from(reserved) * backups.len() as u64;
        self.charge_sectors(
            &mut inode,
            data_blocks * self.sectors_per_block(),
            "resize inode",
        )?;
        put_u32(&mut inode.block, 13 * 4, map_block(dind_block)?);
        Ok(inode)
    }

    /// Write every built inode into its group's inode table.
    fn write_inode_tables(&mut self) -> Result<(), FormatError> {
        let ipg = self.layout.inodes_per_group;
        let inode_size = self.feature.inode_size;
        let isize = inode_size as usize;
        let writes_checksums = self.csum.scheme().writes_object_checksums();
        // Held aside so each write may borrow the sink mutably; the inodes go back
        // afterwards, because the superblock's accounting still reads them.
        let inodes = std::mem::take(&mut self.inodes);
        let mut bytes = vec![0u8; isize];
        for (&number, inode) in &inodes {
            let group = (number - 1) / ipg;
            let index = ((number - 1) % ipg) as usize;
            let table = self.layout.groups[group as usize].inode_table;
            let offset = table * u64::from(self.layout.block_size) + (index * isize) as u64;
            // On the block-mapped family an unused inode — no mode, no links —
            // serializes as all zeros, the way mke2fs leaves the reserved inodes it does
            // not fill: no `i_extra_isize`, and (there being no checksums) nothing else.
            // Under `metadata_csum` an unused inode still carries the `i_extra_isize` and
            // the crc32c the checksum pass writes below, so the zeroing is confined to the
            // family that has no checksums to write.
            if !writes_checksums && inode.mode == 0 && inode.links_count == 0 {
                bytes.iter_mut().for_each(|b| *b = 0);
                self.write_at(offset, &bytes)?;
                continue;
            }
            inode.write_to(&mut bytes, inode_size)?;
            if writes_checksums {
                // The inode checksum covers the whole inode with its checksum fields
                // taken as zero — they are zero in `bytes` here, because the field this
                // is about to fill in serialized from a zero `Inode::checksum`.
                //
                // The high half only exists when the inode is large enough to hold it.
                // Writing it regardless would land two bytes of checksum in whatever
                // occupies 0x82 on an inode that has no extended area — for a 128-byte
                // inode, past its end and into the next one.
                let has_hi = Inode::has_checksum_hi(inode_size, inode.extra_isize);
                let c = recipe::inode(&*self.csum, number, inode.generation, &bytes, has_hi);
                put_u16(&mut bytes, Inode::CHECKSUM_LO_OFFSET, (c & 0xffff) as u16);
                if has_hi {
                    put_u16(&mut bytes, Inode::CHECKSUM_HI_OFFSET, (c >> 16) as u16);
                }
            }
            self.write_at(offset, &bytes)?;
        }
        self.inodes = inodes;
        Ok(())
    }

    /// Write each group's block bitmap and inode bitmap, and build and write the
    /// group descriptor table.
    fn write_bitmaps_and_descriptors(&mut self, model: &FsModel) -> Result<(), FormatError> {
        let ipg = self.layout.inodes_per_group;
        let desc_size = self.feature.desc_size() as usize;
        let mut gdt_bytes = vec![0u8; self.layout.gdt_blocks as usize * self.block_size];

        // Directory counts per group, for `bg_used_dirs_count`.
        let mut dir_counts = vec![0u32; self.layout.group_count as usize];
        for minode in model.inodes.values() {
            if minode.is_dir() {
                dir_counts[((minode.number - 1) / ipg) as usize] += 1;
            }
        }

        let uninit_bg = self.csum.scheme().uninit_bg_semantics();
        let last_group = self.layout.group_count - 1;
        // The bytes of each bitmap the checksum covers: the whole block bitmap, and the
        // inode bitmap rounded up to whole bytes (`recipe::block_bitmap_len` and
        // `recipe::inode_bitmap_len` say why each is that long).
        let bb_len = recipe::block_bitmap_len(self.layout.blocks_per_group);
        let ib_len = recipe::inode_bitmap_len(ipg);

        for g in 0..self.layout.group_count {
            let gl = &self.layout.groups[g as usize];
            let used_inodes = used_inodes_in_group(g, ipg, model.first_free_inode);

            // Under metadata_csum a group with no in-use inodes leaves its inode
            // bitmap uninitialized, and one with no data blocks leaves its block
            // bitmap uninitialized — except a flex-group head, which physically holds
            // the packed tables, and the final group. An uninitialized bitmap is
            // written zero and its checksum field left zero; the group is derived from
            // its layout instead.
            let inode_uninit = uninit_bg && used_inodes == 0;
            let block_uninit = uninit_bg
                && !self.layout.is_flex_head(g)
                && g != last_group
                && self.group_is_dataless(g);

            let bbmp = if block_uninit {
                vec![0u8; self.block_size]
            } else {
                // The allocator is built over this layout, so every group the layout
                // has, it has a bitmap for: `g` is never past the last.
                self.alloc
                    .group_bitmap(g)
                    .expect("a group of this layout has a bitmap")
                    .to_vec()
            };
            let ibmp = if inode_uninit {
                vec![0u8; self.block_size]
            } else {
                self.inode_bitmap(g, model.first_free_inode)
            };

            let block_bitmap_csum = if block_uninit {
                0
            } else {
                recipe::bitmap(&*self.csum, &bbmp[..bb_len])
            };
            let inode_bitmap_csum = if inode_uninit {
                0
            } else {
                recipe::bitmap(&*self.csum, &ibmp[..ib_len])
            };

            // `bg_flags` is meaningful only under metadata_csum / uninit_bg: `BG_INODE_ZEROED`
            // marks an initialized inode table, the two UNINIT bits an uninitialized bitmap.
            // Without the feature the whole field is zero, as `mke2fs` writes it, rather than
            // a flag no feature backs.
            let mut flags = if uninit_bg { BG_INODE_ZEROED } else { 0 };
            if inode_uninit {
                flags |= BG_INODE_UNINIT;
            }
            if block_uninit {
                flags |= BG_BLOCK_UNINIT;
            }

            let mut desc = GroupDescriptor {
                block_bitmap: gl.block_bitmap,
                inode_bitmap: gl.inode_bitmap,
                inode_table: gl.inode_table,
                free_blocks_count: self.alloc.group_free_count(g),
                free_inodes_count: ipg - used_inodes,
                used_dirs_count: dir_counts[g as usize],
                flags,
                // The count of never-used inodes at the tail of the table lets a
                // checker skip scanning them; meaningful only under metadata_csum.
                itable_unused: if uninit_bg { ipg - used_inodes } else { 0 },
                checksum: 0,
                block_bitmap_csum,
                inode_bitmap_csum,
            };
            // The descriptor checksum covers the two bitmap checksums, so it is
            // computed last, over the otherwise-complete descriptor.
            desc.checksum = self.group_desc_csum(g, &desc, desc_size);

            let off = g as usize * desc_size;
            desc.write_to(&mut gdt_bytes[off..off + desc_size], desc_size)
                .expect("descriptor buffer is sized to the whole table");
            self.write_block(gl.block_bitmap, &bbmp)?;
            self.write_block(gl.inode_bitmap, &ibmp)?;
        }

        // Write the primary group-descriptor table right after the primary superblock.
        let gdt_start =
            (u64::from(self.layout.first_data_block) + 1) * u64::from(self.layout.block_size);
        self.write_at(gdt_start, &gdt_bytes)?;
        // Keep the table for the backup copies.
        self.gdt_bytes = gdt_bytes;
        Ok(())
    }

    /// Whether group `g` holds no data blocks — only its fixed metadata — and so is
    /// eligible for `BLOCK_UNINIT`. A non-flex-head group's only local metadata is its
    /// superblock-and-descriptor backup, if it carries one; anything used beyond that
    /// is data. Called only for non-flex-head groups (a flex head holds the packed
    /// tables and is never treated as dataless).
    fn group_is_dataless(&self, g: u32) -> bool {
        let block_count = u64::from(self.layout.groups[g as usize].block_count);
        let free = u64::from(self.alloc.group_free_count(g));
        let meta = self.layout.super_overhead_region(g).map_or(0, |r| r.len);
        block_count - free == meta
    }

    /// The group descriptor checksum (`bg_checksum`): the low 16 bits of a crc32c
    /// over the filesystem seed, the group number, and the descriptor bytes with the
    /// checksum field taken as zero. Zero when checksums are off.
    ///
    /// This is the one field whose *algorithm* the checksum scheme names rather than
    /// merely switching on and off, so it reads the scheme itself: a scheme added to
    /// [`CsumScheme`] is a compile error here, which is where the choice belongs.
    fn group_desc_csum(&self, group: u32, desc: &GroupDescriptor, desc_size: usize) -> u16 {
        match self.csum.scheme() {
            CsumScheme::None => return 0,
            CsumScheme::Crc32c => {}
        }
        let mut buf = [0u8; GroupDescriptor::SIZE_64];
        desc.write_to(&mut buf[..desc_size], desc_size)
            .expect("descriptor buffer holds a full descriptor");
        // `desc.checksum` is zero here, so the two bg_checksum bytes in `buf` already
        // hold the zeros ext4 folds in place of the field.
        recipe::group_descriptor(&*self.csum, group, &buf[..desc_size])
    }

    /// Build one group's inode bitmap: a used bit per in-use inode, and set padding
    /// past the group's inode count.
    fn inode_bitmap(&self, group: u32, first_free_inode: u32) -> Vec<u8> {
        let ipg = self.layout.inodes_per_group;
        let mut bmp = vec![0u8; self.block_size];
        let base = u64::from(group) * u64::from(ipg);
        for i in 0..ipg {
            let inode = base + u64::from(i) + 1;
            if inode < u64::from(first_free_inode) {
                bmp[(i / 8) as usize] |= 1 << (i % 8);
            }
        }
        // Padding past the group's inodes is marked used.
        for i in ipg..(self.block_size as u32 * 8) {
            bmp[(i / 8) as usize] |= 1 << (i % 8);
        }
        bmp
    }

    /// Write the primary superblock and, in every backup group, a superblock copy
    /// (with its own group number) and a group-descriptor-table copy.
    fn write_superblocks(&mut self, model: &FsModel) -> Result<(), FormatError> {
        let sb = self.build_superblock(model);

        // Primary: 1024 bytes into the image, inside block 0 for a 4 KiB block.
        let mut primary = sb.to_bytes();
        self.finalize_sb_csum(&mut primary);
        self.write_at(1024, &primary)?;

        let gdt = std::mem::take(&mut self.gdt_bytes);
        for g in self.layout.backup_groups() {
            let start = (u64::from(self.layout.first_data_block)
                + u64::from(g) * u64::from(self.layout.blocks_per_group))
                * u64::from(self.layout.block_size);
            // The backup superblock sits at the group's first block, and records its
            // own group number — the one field that differs from the primary, so its
            // checksum is computed over the backup's own bytes.
            //
            // `s_block_group_nr` is sixteen bits. A backup in a group past 65535 records
            // 65535, as the pinned `mke2fs` does (observed at groups 78,125 and 117,649).
            let mut backup = sb.clone();
            backup.block_group_nr = u16::try_from(g).unwrap_or(u16::MAX);
            let mut bytes = backup.to_bytes();
            self.finalize_sb_csum(&mut bytes);
            self.write_at(start, &bytes)?;
            // The descriptor-table copy follows in the next block.
            self.write_at(start + u64::from(self.layout.block_size), &gdt)?;
        }
        Ok(())
    }

    /// Write the superblock crc32c (`s_checksum`) into a serialized superblock.
    ///
    /// The recipe is [`superblock_checksum`], which the reader and a re-identification also
    /// call — the one place the seed and the covered span are stated. What is decided here is
    /// only whether the field is written at all: with checksums off it stays the zero the
    /// serialization left, which is what a checker expects when the feature bit is clear.
    fn finalize_sb_csum(&self, bytes: &mut [u8; SuperBlock::SIZE]) {
        if !self.csum.scheme().writes_object_checksums() {
            return;
        }
        let c = superblock_checksum(bytes);
        put_u32(bytes, SuperBlock::CHECKSUM_OFFSET, c);
    }

    /// Assemble the superblock from the layout, feature set, and settled counts.
    fn build_superblock(&self, model: &FsModel) -> SuperBlock {
        let mut sb = SuperBlock::new();
        let l = self.layout;
        sb.inodes_count = l.total_inodes;
        sb.blocks_count = l.total_blocks;
        sb.r_blocks_count = l.reserved_blocks;
        sb.free_blocks_count = self.alloc.free_count();
        sb.free_inodes_count = l.total_inodes - model.used_inode_count();
        sb.first_data_block = l.first_data_block;
        sb.log_block_size = l.block_size.trailing_zeros() - 10;
        sb.log_cluster_size = sb.log_block_size;
        sb.blocks_per_group = l.blocks_per_group;
        sb.clusters_per_group = l.blocks_per_group;
        sb.inodes_per_group = l.inodes_per_group;
        sb.wtime = self.options.time.secs as u32;
        sb.max_mnt_count = 0xffff;
        sb.state = crate::ondisk::STATE_CLEAN;
        sb.errors = self.options.errors.to_s_errors();
        sb.lastcheck = self.options.time.secs as u32;
        sb.rev_level = 1; // dynamic
        sb.first_ino = 11;
        sb.inode_size = self.feature.inode_size;
        sb.feature_compat = self.feature.compat.bits();
        sb.feature_incompat = self.feature.incompat.bits();
        sb.feature_ro_compat = self.feature.ro_compat.bits();
        sb.uuid = self.options.uuid;
        sb.volume_name = self.options.volume_name;
        if let Some(backup) = self.journal_backup {
            sb.journal_inum = JOURNAL_INO;
            sb.jnl_backup_type = 1; // the block map is backed up in s_jnl_blocks
            sb.jnl_blocks = backup;
        }
        if self.feature.has_orphan_file() {
            sb.orphan_file_inum = ORPHAN_INO;
        }
        if self.feature.has_csum_seed() {
            // The stored seed is the one the UUID yields, so it changes no checksum;
            // recording it is what lets the UUID change later without rewriting them.
            sb.checksum_seed = self.csum.base_seed();
        }
        sb.reserved_gdt_blocks = l.reserved_gdt_blocks as u16;
        sb.hash_seed = self.options.hash_seed;
        sb.def_hash_version = self.options.hash_version.to_u8();
        sb.flags = self.options.hash_signedness.to_flag();
        // The superblock's `s_desc_size` records the 64-byte descriptor width only under
        // `64bit`; a 32-byte-descriptor filesystem leaves it zero, which every tool reads
        // as the classic 32. The descriptors are still serialized at their real width
        // (`feature.desc_size()`) — this is the advertised field, not the layout.
        sb.desc_size = if self.feature.is_64bit() {
            self.feature.desc_size()
        } else {
            0
        };
        sb.default_mount_opts = 0x0c; // user_xattr | acl
        sb.mkfs_time = self.options.time.secs as u32;
        // The extra area every inode declares. A 128-byte inode has none, and
        // advertising one the inodes do not carry would misdescribe the filesystem.
        sb.min_extra_isize = extra_isize_for(self.feature.inode_size);
        sb.want_extra_isize = sb.min_extra_isize;
        // `s_log_groups_per_flex` describes the flex packing; a non-flex filesystem
        // leaves it zero even though the layout still groups by `flex_bg_size` internally.
        sb.log_groups_per_flex = if self.feature.has_flex_bg() {
            l.flex_bg_size.trailing_zeros() as u8
        } else {
            0
        };
        // `s_checksum_type` names the algorithm: 1 is crc32c, and a filesystem with no
        // object checksums leaves it zero.
        sb.checksum_type = match self.csum.scheme() {
            CsumScheme::None => 0,
            CsumScheme::Crc32c => 1,
        };
        // The filesystem's own bookkeeping, as the pinned `mke2fs` records it at every profile
        // and block size (the overhead gate): the per-group metadata plus the internal
        // journal's log blocks, with the orphan file counted as ordinary file data. The log is counted and not
        // the blocks that map it: observed, the pinned `mke2fs` counts a 4096-block
        // journal as 4096 on ext3, whose 1 KiB block map adds seventeen indirect blocks. A
        // recomputable hint (e2fsck ignores it), so saturate rather than wrap at the
        // exabyte scale where the count exceeds a u32.
        let journal_blocks = if self.feature.has_journal() {
            self.journal_blocks.map_or(0, u64::from)
        } else {
            0
        };
        // The blocks before the first group count as well: block 0 at a one-kibibyte block,
        // where the superblock is block 1. Observed: the pinned `mke2fs` counts it.
        sb.overhead_clusters =
            u32::try_from(l.overhead_blocks() + u64::from(l.first_data_block) + journal_blocks)
                .unwrap_or(u32::MAX);
        sb
    }

    /// Blocks-to-512-byte-sectors conversion for `i_blocks`.
    fn sectors_per_block(&self) -> u64 {
        u64::from(self.layout.block_size) / 512
    }

    /// Record the sectors an inode is charged, refusing a count its feature set cannot hold.
    ///
    /// Every `i_blocks` this writer sets goes through here, because the field is only 32
    /// bits wide without `huge_file` and the bytes above it are another field entirely. A
    /// count past that would serialize as a wrapped low half beside a high half the feature
    /// words deny — no panic, no error, and an image every checker faults.
    fn charge_sectors(
        &self,
        inode: &mut Inode,
        sectors: u64,
        what: &'static str,
    ) -> Result<(), FormatError> {
        self.check_sectors(sectors, what)?;
        inode.blocks = sectors;
        Ok(())
    }

    /// The bound alone, for a caller that knows the count before it has an inode to put it
    /// on — which is how a request too large to record is refused before a block of it is
    /// allocated.
    fn check_sectors(&self, sectors: u64, what: &'static str) -> Result<(), FormatError> {
        if !self.feature.has_huge_file() && sectors > MAX_SECTORS_WITHOUT_HUGE_FILE {
            return Err(FormatError::BlockCountWithoutHugeFile {
                what,
                sectors,
                limit: MAX_SECTORS_WITHOUT_HUGE_FILE,
            });
        }
        Ok(())
    }

    fn write_block(&mut self, block: u64, data: &[u8]) -> Result<(), FormatError> {
        self.write_at(block * u64::from(self.layout.block_size), data)
    }
}

/// Split content into block-sized chunks for placement, zero-padding the final chunk.
///
/// The chunks borrow the source. Only a final short chunk is copied, into a padded block
/// of its own, so placing a window of a file costs one block beyond the window rather than
/// a second copy of it.
fn block_chunks(bytes: &[u8], block_size: usize) -> impl ExactSizeIterator<Item = Cow<'_, [u8]>> {
    bytes.chunks(block_size).map(move |chunk| {
        if chunk.len() == block_size {
            Cow::Borrowed(chunk)
        } else {
            let mut block = vec![0u8; block_size];
            block[..chunk.len()].copy_from_slice(chunk);
            Cow::Owned(block)
        }
    })
}

/// The physical blocks of an inode's data, in logical order, held as the runs they form.
///
/// A file's blocks are a few long runs, broken only where the allocator moved on or a block
/// map's indirect block sits between two of them. So what this costs grows with how the
/// mapping is broken up rather than with the file's length, and a file placed a window at a
/// time holds a window and its runs rather than a word for every block.
#[derive(Default)]
struct BlockRuns {
    runs: Vec<BlockRange>,
    /// Blocks across every run.
    count: u64,
}

impl BlockRuns {
    /// The runs an allocation returned, in the order it returned them.
    fn from_ranges(runs: Vec<BlockRange>) -> Self {
        let count = runs.iter().map(|run| run.len).sum();
        Self { runs, count }
    }

    /// Append `block` as the next logical block, extending the last run where it follows it.
    fn push(&mut self, block: u64) {
        match self.runs.last_mut() {
            Some(run) if run.end() == block => run.len += 1,
            _ => self.runs.push(BlockRange {
                start: block,
                len: 1,
            }),
        }
        self.count += 1;
    }

    /// How many blocks are held.
    const fn len(&self) -> u64 {
        self.count
    }

    /// Every block, in logical order.
    fn blocks(&self) -> impl Iterator<Item = u64> + '_ {
        self.runs.iter().flat_map(|run| run.start..run.end())
    }
}

/// Inodes in use within group `group`, given the first free inode number.
fn used_inodes_in_group(group: u32, ipg: u32, first_free_inode: u32) -> u32 {
    let base = group * ipg; // one below this group's first inode number
    (first_free_inode - 1).saturating_sub(base).min(ipg)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::read::Reader;
    use crate::source::TreeBuilder;

    const MIB: u64 = 1024 * 1024;

    fn opts() -> FormatOptions {
        let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16]);
        o.grow = GrowReservation::UpTo(32 * 1024 * MIB);
        o
    }

    /// The plan the pin tests read: the default profile at a size small enough to state
    /// every geometry number in the golden document.
    fn pinned_options() -> FormatOptions {
        let mut o = FormatOptions::new([0x11; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16]);
        o.grow = GrowReservation::Max;
        o
    }

    fn pinned_plan() -> FormatPlan {
        FormatPlan::new(TreeBuilder::new(), 64 * MIB, pinned_options()).expect("plan")
    }

    #[test]
    fn block_runs_hold_a_mapping_as_its_runs_and_hand_back_every_block_in_order() {
        // A block map's shape: twelve direct blocks, an indirect block between them and the
        // next data, then data again. Three runs, however many blocks.
        let mut runs = BlockRuns::default();
        let order: Vec<u64> = (100..112).chain(113..1137).chain([2000, 2001]).collect();
        for &block in &order {
            runs.push(block);
        }
        assert_eq!(runs.len(), order.len() as u64);
        assert_eq!(runs.runs.len(), 3);
        assert!(runs.blocks().eq(order.iter().copied()));

        // An allocation's ranges are taken as they are.
        let ranges = vec![
            BlockRange { start: 7, len: 3 },
            BlockRange { start: 40, len: 2 },
        ];
        let runs = BlockRuns::from_ranges(ranges);
        assert_eq!(runs.len(), 5);
        assert!(runs.blocks().eq([7, 8, 9, 40, 41]));
    }

    #[test]
    fn the_policy_pin_is_the_document_it_documents() {
        // The golden document, and what `policy_pin`'s rustdoc shows, so the example a
        // reader copies is the one the code emits. Every value here is a decision that
        // moves bytes and none of them varies with the image.
        let expected = "\
ferrosys-policy-pin 1
compat 0x0000103c has_journal ext_attr resize_inode dir_index orphan_file
incompat 0x000022c2 filetype extent 64bit flex_bg metadata_csum_seed
ro_compat 0x0000046b sparse_super large_file huge_file dir_nlink extra_isize metadata_csum
block_size 4096
inode_size 256
grow max
inodes auto
reserved 500
errors continue
journal auto
hash_version half_md4
hash_signedness unsigned
timestamp_clamp none
";
        assert_eq!(pinned_options().policy_pin(), expected);
    }

    #[test]
    fn the_identity_pin_is_the_document_it_documents() {
        let expected = "\
ferrosys-identity-pin 1
uuid 11111111111111111111111111111111
time 1700000000.0
hash_seed 00000000000000000000000000000000
volume_name 00000000000000000000000000000000
fixed_time none
";
        assert_eq!(pinned_options().identity_pin(), expected);
    }

    #[test]
    fn the_geometry_pin_is_the_document_it_documents() {
        let expected = "\
ferrosys-geometry-pin 1
block_size 4096
total_blocks 16384
blocks_per_group 32768
first_data_block 0
group_count 1
inodes_per_group 16384
inode_table_blocks 1024
total_inodes 16384
gdt_blocks 1
reserved_gdt_blocks 256
flex_bg_size 16
max_grow_blocks 538968064
reserved_blocks 819
groups 1 crc32c 0x8a137015
journal_blocks 1024
";
        assert_eq!(pinned_plan().geometry_pin(), expected);
    }

    #[test]
    fn the_policy_pin_holds_still_while_the_image_varies() {
        // The property the split exists for. A builder writing one image per board changes
        // the identity and the size every time; if either reached the policy pin, an
        // empty-diff comparison of two boards' contracts would be impossible.
        let base = pinned_options().policy_pin();
        for (what, mut options) in [
            ("uuid", pinned_options()),
            ("time", pinned_options()),
            ("hash_seed", pinned_options()),
            ("volume_name", pinned_options()),
        ] {
            match what {
                "uuid" => options.uuid = [0x99; 16],
                "time" => options.time = Timestamp::from_secs(1_800_000_123),
                "hash_seed" => options.hash_seed = [0x44; 16],
                _ => options.volume_name = *b"board-two\0\0\0\0\0\0\0",
            }
            assert_eq!(base, options.policy_pin(), "{what} reached the policy pin");
            assert_ne!(
                pinned_options().identity_pin(),
                options.identity_pin(),
                "{what} did not reach the identity pin"
            );
        }
        // And the size, which is not an option at all: two filesystems built to one
        // contract at different sizes pin the same policy and different geometry.
        let small = FormatPlan::new(TreeBuilder::new(), 64 * MIB, pinned_options()).expect("plan");
        let large = FormatPlan::new(TreeBuilder::new(), 512 * MIB, pinned_options()).expect("plan");
        assert_eq!(base, pinned_options().policy_pin());
        assert_ne!(
            small.geometry_pin(),
            large.geometry_pin(),
            "two sizes pin the same geometry"
        );
    }

    #[test]
    fn a_source_too_large_for_the_size_is_refused_by_the_plan() {
        // The plan places the model, so a format that cannot succeed fails before a
        // destination is opened: a dry run reports it, and a caller writing over an existing
        // file keeps the file. Thirty-two mebibytes of content do not fit in sixteen.
        let time = Timestamp::from_secs(1_700_000_000);
        let src = TreeBuilder::new().file(
            b"/big".to_vec(),
            vec![0xAB; 32 * MIB as usize],
            crate::Metadata::new(0o644, time),
        );
        let planned = FormatPlan::new(src, 16 * MIB, pinned_options());
        assert!(
            matches!(
                planned,
                Err(FormatError::Alloc(AllocError::OutOfSpace { .. }))
            ),
            "{:?}",
            planned.err()
        );
    }

    #[test]
    fn every_option_that_moves_bytes_moves_one_of_the_two_pins() {
        // Each case changes exactly one input. A knob absent from *both* documents would
        // produce identical pins for a different image, which is the failure the pins exist
        // to prevent -- and `errors` is the field that reaches neither the feature words
        // nor the geometry, so nothing else would record it.
        /// One knob's name, whether it belongs to the policy, and the change that moves it.
        type Case = (&'static str, bool, Box<dyn Fn(&mut FormatOptions)>);
        let cases: Vec<Case> = vec![
            (
                "uuid",
                false,
                Box::new(|o: &mut FormatOptions| o.uuid = [0x22; 16]),
            ),
            (
                "time",
                false,
                Box::new(|o: &mut FormatOptions| o.time = Timestamp::from_secs(1_700_000_001)),
            ),
            (
                "hash_seed",
                false,
                Box::new(|o: &mut FormatOptions| o.hash_seed = [0x33; 16]),
            ),
            (
                "volume_name",
                false,
                Box::new(|o: &mut FormatOptions| o.volume_name = *b"rootfs\0\0\0\0\0\0\0\0\0\0"),
            ),
            (
                "fixed_time",
                // Both: whether a build clamps is policy, the time it clamps to is identity.
                true,
                Box::new(|o: &mut FormatOptions| {
                    o.fixed_time = Some(Timestamp::from_secs(1_600_000_000));
                }),
            ),
            (
                "hash_version",
                true,
                Box::new(|o: &mut FormatOptions| o.hash_version = HashVersion::Tea),
            ),
            (
                "hash_signedness",
                true,
                Box::new(|o: &mut FormatOptions| o.hash_signedness = HashSignedness::Signed),
            ),
            (
                "grow",
                true,
                Box::new(|o: &mut FormatOptions| o.grow = GrowReservation::None),
            ),
            (
                "inodes",
                true,
                Box::new(|o: &mut FormatOptions| o.inodes = InodeCount::Count(2048)),
            ),
            (
                "reserved",
                true,
                Box::new(|o: &mut FormatOptions| {
                    o.reserved = ReservedRatio::from_hundredths_of_percent(1000).expect("10%");
                }),
            ),
            (
                "feature",
                true,
                Box::new(|o: &mut FormatOptions| o.feature = FeatureSet::EXT2),
            ),
            (
                "errors",
                true,
                Box::new(|o: &mut FormatOptions| o.errors = ErrorBehavior::Panic),
            ),
            (
                "journal",
                true,
                Box::new(|o: &mut FormatOptions| o.journal = JournalSize::Blocks(2048)),
            ),
        ];

        let base_policy = pinned_options().policy_pin();
        let base_identity = pinned_options().identity_pin();
        for (name, is_policy, change) in cases {
            let mut o = pinned_options();
            change(&mut o);
            let moved = if is_policy {
                base_policy != o.policy_pin()
            } else {
                base_identity != o.identity_pin()
            };
            let which = if is_policy { "policy" } else { "identity" };
            assert!(moved, "changing {name} left the {which} pin unchanged");
        }
    }

    #[test]
    fn a_moved_group_placement_moves_the_digest() {
        // The group table is a digest rather than a list, so this is what proves the
        // digest is load-bearing: two layouts whose scalar geometry agrees but whose
        // placements differ must not pin alike.
        let mut layout = pinned_plan().layout().clone();
        let before = groups_digest(&layout.groups);
        layout.groups[0].inode_table += 1;
        assert_ne!(
            before,
            groups_digest(&layout.groups),
            "a moved inode table changed no digest"
        );
    }

    #[test]
    fn the_profile_setter_seeds_the_matching_feature_set() {
        let base = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16]);
        assert_eq!(base.profile(Profile::Ext2).feature, FeatureSet::EXT2);
        assert_eq!(base.profile(Profile::Ext3).feature, FeatureSet::EXT3);
        assert_eq!(base.profile(Profile::Ext4).feature, FeatureSet::DEFAULT);
        // Sugar over the feature field only: no other option is disturbed.
        let seeded = base.profile(Profile::Ext3);
        assert_eq!(seeded.uuid, base.uuid);
        assert_eq!(seeded.grow, base.grow);
        assert_eq!(seeded.inodes, base.inodes);
    }

    #[test]
    fn a_volume_label_is_written_into_the_superblock_and_reads_back() {
        let mut o = opts();
        o.volume_name = *b"rootfs\0\0\0\0\0\0\0\0\0\0";
        let image = format(TreeBuilder::new(), 64 * MIB, o).expect("format");
        // `s_volume_name` sits at offset 0x78 in the 1024-byte superblock, which begins
        // 1024 bytes into the image.
        assert_eq!(
            &image.as_bytes()[1024 + 0x78..1024 + 0x78 + 16],
            b"rootfs\0\0\0\0\0\0\0\0\0\0"
        );
        // The reader hands the same label back, NUL-padding and all.
        let reader = Reader::open(std::io::Cursor::new(image.as_bytes())).expect("open");
        assert_eq!(
            reader.superblock().volume_name,
            *b"rootfs\0\0\0\0\0\0\0\0\0\0"
        );

        // An unlabelled format leaves the field all zero, matching mke2fs's default.
        let image = format(TreeBuilder::new(), 64 * MIB, opts()).expect("format");
        assert_eq!(image.as_bytes()[1024 + 0x78..1024 + 0x78 + 16], [0u8; 16]);
    }

    #[test]
    fn the_error_behavior_reaches_the_superblock_and_reads_back() {
        // `s_errors` sits at offset 0x3c in the 1024-byte superblock. Each policy maps to
        // the kernel's value (1 continue, 2 remount-ro, 3 panic); the default is continue.
        let read_errors = |image: &Image| {
            u16::from_le_bytes(
                image.as_bytes()[1024 + 0x3c..1024 + 0x3c + 2]
                    .try_into()
                    .expect("two bytes"),
            )
        };
        assert_eq!(
            read_errors(&format(TreeBuilder::new(), 64 * MIB, opts()).expect("format")),
            1,
            "the default continues, matching the kernel"
        );
        for (policy, expected) in [
            (ErrorBehavior::Continue, 1u16),
            (ErrorBehavior::RemountReadOnly, 2),
            (ErrorBehavior::Panic, 3),
        ] {
            let mut o = opts();
            o.errors = policy;
            let image = format(TreeBuilder::new(), 64 * MIB, o).expect("format");
            assert_eq!(
                read_errors(&image),
                expected,
                "{policy:?} in the raw superblock"
            );
            // The reader hands the same value back, and it survives the superblock
            // checksum the metadata_csum profile computes over the field.
            let reader = Reader::open(std::io::Cursor::new(image.as_bytes())).expect("open");
            assert_eq!(reader.superblock().errors, expected, "{policy:?} read back");
        }
    }

    #[test]
    fn the_orphan_file_size_follows_the_block_count_between_its_floor_and_ceiling() {
        // The curve mke2fs 1.47 realizes, in filesystem blocks: one orphan block per 4096
        // of them, no fewer than 32 and no more than 512. It counts blocks, not bytes, so
        // these hold at any block size.
        assert_eq!(orphan_file_blocks(2048), 32, "below the floor");
        assert_eq!(orphan_file_blocks(131_072), 32, "exactly at the floor");
        assert_eq!(orphan_file_blocks(262_144), 64);
        assert_eq!(orphan_file_blocks(524_288), 128);
        assert_eq!(orphan_file_blocks(2_097_152), 512, "exactly at the ceiling");
        assert_eq!(orphan_file_blocks(67_108_864), 512, "above the ceiling");
    }

    #[test]
    fn an_entry_on_the_orphan_files_inode_is_refused() {
        // `format` derives the first entry's inode from the feature set, so this cannot
        // arise through the public API — it is reached here by building the model with a
        // first user inode that ignores the orphan file, which is what a feature added
        // later that claims an inode and forgets to move the entries up would do. The
        // image must be refused: writing it would leave `/f`'s directory entry naming the
        // orphan file.
        let options = opts();
        let feature = options.feature;
        assert!(feature.has_orphan_file());
        let layout = plan_layout(&options.plan_request(64 * MIB)).expect("layout");
        let model = build_model(
            TreeBuilder::new().file(
                b"/f".to_vec(),
                b"x".to_vec(),
                crate::source::Metadata::new(0o644, options.time),
            ),
            // The collision a stale derivation produces: entries starting at the inode
            // the orphan file takes.
            ModelConfig::new(feature, ORPHAN_INO, options.time),
        )
        .expect("model");
        assert!(model.inodes.contains_key(&ORPHAN_INO), "the entry took it");

        let journal = journal_size(&layout, &options).expect("the journal size is realizable");
        // The refusal happens while placing, so no destination is needed to provoke it.
        let mut writer = Writer::new(&layout, &feature, options, journal, Discard)
            .expect("an allocator for a layout this size");
        let err = writer
            .materialize(&model)
            .expect_err("the collision is refused");
        assert!(
            matches!(err, FormatError::OrphanInodeInUse { inode } if inode == ORPHAN_INO),
            "expected OrphanInodeInUse, got {err:?}"
        );
    }

    #[test]
    fn the_orphan_file_takes_the_inode_below_the_first_entry() {
        // With the orphan file the source's entries start at 13; without it, at 12. The
        // number is not a detail: it is what a foreign tool reads out of
        // `s_orphan_file_inum`, and what the first user inode has to make room for.
        let with = FeatureSet::default();
        assert!(with.has_orphan_file());
        assert_eq!(first_user_inode(&with), ORPHAN_INO + 1);

        let mut without = FeatureSet::default();
        without.compat = crate::feature::Compat::from_bits(
            without.compat.bits()
                & !(crate::feature::Compat::HAS_JOURNAL.bits()
                    | crate::feature::Compat::ORPHAN_FILE.bits()),
        );
        assert_eq!(first_user_inode(&without), FIRST_USER_INO);
    }

    #[test]
    fn the_block_mapped_family_round_trips_direct_and_indirect_files() {
        // The ext2/ext3 classic block map — twelve direct pointers, then single- and
        // double-indirect trees — must read back exactly what it wrote. Files sized to
        // land in each region prove the writer and the reader agree on the map's shape and
        // that its just-in-time metadata blocks are placed where the reader looks for them.
        use crate::source::Metadata;
        let time = Timestamp::from_secs(1_700_000_000);
        let bs = 4096usize;
        // Direct only (a partial final block); across the single indirect boundary; and
        // across the single into the double indirect (12 + 1024 + 4 blocks).
        let direct = vec![0xa1u8; 5 * bs - 17];
        let single = vec![0xb2u8; 20 * bs];
        let mut double = vec![0u8; 1040 * bs];
        for (i, byte) in double.iter_mut().enumerate() {
            *byte = (i % 251) as u8;
        }
        for profile in [Profile::Ext2, Profile::Ext3] {
            let src = TreeBuilder::new()
                .file(
                    b"/direct".to_vec(),
                    direct.clone(),
                    Metadata::new(0o644, time),
                )
                .file(
                    b"/single".to_vec(),
                    single.clone(),
                    Metadata::new(0o644, time),
                )
                .file(
                    b"/double".to_vec(),
                    double.clone(),
                    Metadata::new(0o644, time),
                );
            let mut o = FormatOptions::new([7u8; 16], time, [0u8; 16]).profile(profile);
            o.grow = GrowReservation::UpTo(32 * 1024 * MIB);
            let image = format(src, 64 * MIB, o).expect("format block-mapped");
            let mut r = Reader::open(std::io::Cursor::new(image.into_bytes())).expect("open");
            assert_eq!(
                r.profile(),
                profile,
                "reads back as the family it was written for"
            );
            for (name, want) in [
                (b"/direct".as_slice(), &direct),
                (b"/single".as_slice(), &single),
                (b"/double".as_slice(), &double),
            ] {
                let (_, inode) = r.lookup(name).expect("lookup");
                assert!(
                    !inode.flags.contains(crate::ondisk::InodeFlags::EXTENTS),
                    "{profile} {}: block-mapped inode carries no EXTENTS flag",
                    String::from_utf8_lossy(name)
                );
                let got = r.read_data(&inode).expect("read");
                assert_eq!(got, *want, "{profile} {}", String::from_utf8_lossy(name));
            }
        }
    }

    #[test]
    fn a_checksum_off_profile_writes_zero_group_flags() {
        // bg_flags is meaningful only under metadata_csum / uninit_bg. With the checksum
        // off, every descriptor's flags are zero, as mke2fs writes them — not
        // BG_INODE_ZEROED carried with no feature to back it.
        let mut feature = FeatureSet::default();
        feature.ro_compat = crate::feature::RoCompat::from_bits(
            feature.ro_compat.bits() & !crate::feature::RoCompat::METADATA_CSUM.bits(),
        );
        // metadata_csum_seed cannot stand without metadata_csum, so it is cleared too.
        feature.incompat = crate::feature::Incompat::from_bits(
            feature.incompat.bits() & !crate::feature::Incompat::CSUM_SEED.bits(),
        );
        assert!(!feature.has_metadata_csum());
        feature.validate().expect("a checksum-off profile is valid");

        let mut o = opts();
        o.feature = feature;
        let image = format(TreeBuilder::new(), 64 * MIB, o).expect("format");

        let group_count = image.layout().group_count;
        let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).expect("open");
        for g in 0..group_count {
            let desc = r.group_descriptor(g).expect("group descriptor");
            assert_eq!(
                desc.flags, 0,
                "group {g}: bg_flags must be zero without metadata_csum"
            );
        }
    }

    #[test]
    fn the_resize_map_refuses_a_block_past_thirty_two_bits() {
        // The map stores 32-bit block numbers. Converting a block past that must be a
        // typed error, not a truncated pointer at the wrong block — the geometry never
        // reserves such a block, so this guards a future planner change, not today's.
        assert_eq!(map_block(0).unwrap(), 0);
        assert_eq!(map_block(u64::from(u32::MAX)).unwrap(), u32::MAX);
        let err = map_block(u64::from(u32::MAX) + 1).unwrap_err();
        assert!(matches!(
            err,
            FormatError::ResizeMapNeeds32BitBlocks { block } if block == u64::from(u32::MAX) + 1
        ));
    }

    #[test]
    fn a_format_time_past_the_superblock_range_is_rejected() {
        // The superblock's time fields are 32 bits; a format clock past 2106 would
        // truncate to a different instant, so it is refused rather than written wrong.
        let mut o = opts();
        o.time = Timestamp::from_secs(i64::from(u32::MAX) + 1);
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected FormatTimeOutOfRange");
        };
        assert!(
            matches!(err, FormatError::FormatTimeOutOfRange { secs } if secs == i64::from(u32::MAX) + 1),
            "expected FormatTimeOutOfRange, got {err:?}"
        );
    }

    #[test]
    fn the_default_max_reservation_formats_and_reads_back() {
        // Every other test overrides `grow`, so the shipped default — Max — is
        // exercised only here: a plain `FormatOptions::new` must format, fill the
        // resize inode's map without running off its block, and read back clean.
        let plain = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16]);
        assert_eq!(plain.grow, GrowReservation::Max);
        let image = format(TreeBuilder::new(), 64 * MIB, plain).expect("default format");
        // 64 MiB is below the knee at which the whole map is affordable, so the
        // reservation is the share of the filesystem `Max` will spend: 16384 blocks / 64.
        assert_eq!(
            image.layout().reserved_gdt_blocks,
            256,
            "Max reserves the share of a 64 MiB filesystem it can spare"
        );
        let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).expect("open");
        // The resize inode (7) maps its reservation through the classic block map, so
        // it carries no extents flag and a nonzero double-indirect pointer.
        let resize = r.inode(7).expect("resize inode");
        let dind = u32::from_le_bytes([
            resize.block[52],
            resize.block[53],
            resize.block[54],
            resize.block[55],
        ]);
        assert_ne!(
            dind, 0,
            "the reservation hangs off the double-indirect slot"
        );
    }

    #[test]
    fn streaming_a_format_writes_the_same_bytes_as_collecting_one() {
        // The two entry points share every decision; only the destination differs, so
        // a streamed image must be byte-identical to the collected one.
        use crate::source::Metadata;

        let time = Timestamp::from_secs(1_700_000_000);
        let source = || {
            TreeBuilder::new()
                .directory(b"/etc".to_vec(), Metadata::new(0o755, time))
                .file(
                    b"/etc/hostname".to_vec(),
                    b"ferrosys\n".to_vec(),
                    Metadata::new(0o644, time),
                )
                .symlink(
                    b"/etc/mtab".to_vec(),
                    b"/proc/self/mounts".to_vec(),
                    Metadata::new(0o777, time),
                )
        };

        let collected = format(source(), 64 * MIB, opts()).expect("format");

        let mut streamed = std::io::Cursor::new(Vec::new());
        let layout = format_to(source(), 64 * MIB, opts(), &mut streamed).expect("format_to");
        let streamed = streamed.into_inner();

        assert_eq!(layout, *collected.layout());
        assert_eq!(
            streamed.len(),
            collected.as_bytes().len(),
            "a streamed image is extended to the filesystem's full size"
        );
        assert_eq!(streamed, collected.as_bytes(), "the images differ");
    }

    #[test]
    fn a_streamed_image_leaves_the_blocks_it_never_writes_alone() {
        // Streaming touches only the blocks the filesystem uses, which is what keeps
        // a file destination sparse. The rest read back as the zeros they were.
        let mut sink = std::io::Cursor::new(Vec::new());
        let layout = format_to(TreeBuilder::new(), 64 * MIB, opts(), &mut sink).expect("format_to");
        let bytes = sink.into_inner();
        assert_eq!(
            bytes.len() as u64,
            layout.total_blocks * u64::from(layout.block_size)
        );
        // The final block of a fresh 64 MiB filesystem holds nothing.
        let last = bytes.len() - layout.block_size as usize;
        assert!(bytes[last..].iter().all(|&b| b == 0));
    }

    #[test]
    fn explicit_journal_size_is_honored() {
        let mut o = opts();
        o.journal = JournalSize::Blocks(2048);
        let image = format(TreeBuilder::new(), 512 * MIB, o).expect("format");
        let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
        let jsb = r.journal_superblock().unwrap().expect("journal");
        assert_eq!(jsb.max_len, 2048);
        assert_eq!(r.inode(8).unwrap().size, 2048 * 4096);
    }

    #[test]
    fn the_classic_block_map_reaches_what_its_pointer_words_hold() {
        // Twelve direct pointers plus one, two, and three levels of `block_size / 4`
        // pointers each. These are the whole reach of an ext2/ext3 file at each block size
        // the format allows, and the bound a file past them is refused against.
        assert_eq!(classic_map_reach(1024), 16_843_020); // 16.06 GiB
        assert_eq!(classic_map_reach(2048), 134_480_396); // 256.5 GiB
        assert_eq!(classic_map_reach(4096), 1_074_791_436); // 4.004 TiB
    }

    #[test]
    fn a_block_count_the_feature_words_cannot_record_is_refused_rather_than_wrapped() {
        // Without `huge_file` an inode's block count is `i_blocks_lo` alone — the two bytes
        // above it are ext2's `l_i_frag` and `l_i_fsize`, not a high half. So the field
        // stops at two tebibytes, while a classic map at a 4096-byte block reaches 4.004 —
        // and `FeatureSet::validate` refuses adding `huge_file` to a non-extent set, so on
        // ext2 and ext3 the map genuinely outruns the field.
        //
        // Written anyway, it serializes as a wrapped low half beside a high half the feature
        // words deny: no panic, no error, and an image every checker faults. The journal is
        // the structure an explicit size pushes there without a filesystem large enough to
        // hold it existing.
        let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16])
            .profile(Profile::Ext3);
        o.grow = GrowReservation::None;
        assert!(
            !o.feature.has_huge_file(),
            "the block-mapped profiles cannot carry huge_file"
        );
        // Two tebibytes at a 4096-byte block: 536,870,912 blocks of eight sectors each is
        // exactly `u32::MAX + 1`, so this is the first count the field cannot hold.
        o.journal = JournalSize::Blocks(536_870_912);
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected BlockCountWithoutHugeFile");
        };
        assert!(
            matches!(
                err,
                FormatError::BlockCountWithoutHugeFile {
                    what: "journal",
                    limit,
                    ..
                } if limit == u64::from(u32::MAX)
            ),
            "expected BlockCountWithoutHugeFile, got {err:?}"
        );

        // And the bound is the field's, not a refusal of every large log: one block below it
        // gets as far as running out of filesystem.
        o.journal = JournalSize::Blocks(536_870_911);
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected the log not to fit in 64 MiB");
        };
        assert!(
            !matches!(err, FormatError::BlockCountWithoutHugeFile { .. }),
            "the count one below the field's reach is a count it records: {err:?}"
        );
    }

    #[test]
    fn a_file_past_the_classic_block_maps_reach_is_refused_rather_than_mapped_short() {
        // The block-mapped twin of the extent tree's own bound. A map that ran out of words
        // would leave the tail of a file neither mapped nor written while its size claimed
        // the whole length, so the count is checked before a block is allocated — which is
        // what lets this fire without a filesystem large enough to hold the file existing.
        //
        // The journal is the structure an explicit size can push past the bound: a source
        // entry that large would have to be read, and the resize inode is bounded far below
        // it. A 1024-byte block puts the reach at 16,843,020 blocks.
        let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16])
            .profile(Profile::Ext3);
        o.feature.block_size = 1024;
        o.grow = GrowReservation::None;
        o.journal = JournalSize::Blocks(16_843_021);
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected FileTooLargeForBlockMap");
        };
        assert!(
            matches!(
                err,
                FormatError::FileTooLargeForBlockMap {
                    blocks: 16_843_021,
                    limit: 16_843_020,
                    block_size: 1024,
                }
            ),
            "expected FileTooLargeForBlockMap, got {err:?}"
        );

        // One block below the reach is a map the words hold, so what refuses the log above
        // is the bound and not the size alone: it gets as far as running out of filesystem.
        o.journal = JournalSize::Blocks(16_843_020);
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected the log not to fit in 64 MiB");
        };
        assert!(
            matches!(err, FormatError::JournalDoesNotFit { .. }),
            "expected JournalDoesNotFit, got {err:?}"
        );
    }

    #[test]
    fn a_journal_past_the_large_file_bound_is_rejected_without_the_feature() {
        // The journal is a regular file, so a log an explicit size pushed to 2 GiB needs
        // `large_file` like any other. A 1024-byte block keeps the resize inode under the
        // bound, so this profile is otherwise valid without the feature — which is what
        // leaves the journal as the one structure that reaches it. The check runs before
        // any journal block is allocated, so no filesystem large enough to hold the log
        // has to exist for it to fire.
        let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16])
            .profile(Profile::Ext3);
        o.feature.block_size = 1024;
        o.feature = o
            .feature
            .with_feature("large_file", false)
            .expect("a known name");
        o.grow = GrowReservation::None;
        o.journal = JournalSize::Blocks(2 * 1024 * 1024); // 2 GiB at a 1024-byte block
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("expected LargeFileWithoutFeature");
        };
        assert!(
            matches!(
                err,
                FormatError::LargeFileWithoutFeature {
                    what: "journal",
                    size
                } if size == 2 * 1024 * MIB
            ),
            "expected LargeFileWithoutFeature for the journal, got {err:?}"
        );

        // The same journal is written where the feature permits it — the rule is about
        // the pairing, not about the size alone.
        let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16])
            .profile(Profile::Ext3);
        o.feature.block_size = 1024;
        o.grow = GrowReservation::None;
        o.journal = JournalSize::Blocks(2 * 1024 * 1024);
        assert!(o.feature.has_large_file());
        let Err(err) = format(TreeBuilder::new(), 64 * MIB, o) else {
            panic!("a 2 GiB log does not fit a 64 MiB image");
        };
        assert!(
            matches!(err, FormatError::JournalDoesNotFit { .. }),
            "the feature check must give way to the space the journal needs, got {err:?}"
        );
    }

    #[test]
    fn journal_below_the_minimum_is_rejected() {
        let mut o = opts();
        o.journal = JournalSize::Blocks(512);
        assert!(matches!(
            format(TreeBuilder::new(), 512 * MIB, o),
            Err(FormatError::JournalTooSmall {
                requested: 512,
                minimum: 1024
            })
        ));
    }

    #[test]
    fn auto_journal_maps_as_a_single_extent() {
        // A contiguous run holds any heuristic journal size, so the map stays inline.
        // Streamed to a file rather than held as bytes: the two gigabytes that put
        // the heuristic in its top tier are almost entirely unwritten, and an
        // in-memory image would cost that much resident memory for zeros.
        let file = tempfile::tempfile().expect("temp file");
        format_to(TreeBuilder::new(), 2048 * MIB, opts(), &file).expect("format");
        let mut r = Reader::open(file).unwrap();
        let inode = r.inode(8).unwrap();
        // A single leaf: the inline extent header reports one entry.
        let entries = u16::from_le_bytes([inode.block[2], inode.block[3]]);
        assert_eq!(entries, 1, "journal should map as one contiguous extent");
    }
}