coordinode-lsm-tree 6.0.0

Embedded LSM-tree storage engine in pure Rust, no C/C++ dependency. MVCC snapshots, BuRR filters, zstd dictionary compression, columnar PAX blocks, AES-256-GCM at rest, self-healing per-block ECC, compaction on a near-full disk, no_std support.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
#![expect(
    clippy::expect_used,
    reason = "test assertions over known-good fixtures; failure surfaces via panic"
)]
#![expect(
    clippy::indexing_slicing,
    reason = "test code indexes fixture buffers with known sizes"
)]

use super::*;
use std::io::{Read, Write};
use std::sync::Arc;
// Shadows #[test] to enable log capture in test output.
use test_log::test;

/// Returns an `IoUringFs`, skipping only if the kernel lacks `io_uring`.
/// Constructor bugs (e.g. broken `RingThread::spawn`) will panic the
/// test instead of silently skipping.
fn try_io_uring() -> Option<IoUringFs> {
    if !is_io_uring_available() {
        eprintln!("skipping: io_uring not supported by kernel");
        return None;
    }
    // Kernel supports io_uring — constructor failures are real bugs.
    Some(IoUringFs::new().expect("io_uring available but IoUringFs::new() failed"))
}

#[test]
fn probe_availability() {
    // Just exercises the probe — result depends on the kernel.
    let available = is_io_uring_available();
    eprintln!("io_uring available: {available}");
}

#[test]
fn create_read_write() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let path = dir.path().join("test.txt");
    let opts = FsOpenOptions::new().write(true).create(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hello world")?;
    file.sync_all()?;
    drop(file);

    let opts = FsOpenOptions::new().read(true);
    let mut file = fs.open(&path, &opts)?;
    let mut buf = String::new();
    file.read_to_string(&mut buf)?;
    assert_eq!(buf, "hello world");

    Ok(())
}

#[test]
fn read_at_pread_semantics() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let path = dir.path().join("pread.bin");
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hello world")?;
    file.sync_data()?;

    let mut buf = [0u8; 5];
    let n = file.read_at(&mut buf, 6)?;
    assert_eq!(n, 5);
    assert_eq!(&buf, b"world");

    let n = file.read_at(&mut buf, 0)?;
    assert_eq!(n, 5);
    assert_eq!(&buf, b"hello");

    Ok(())
}

#[test]
fn directory_operations() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let nested = dir.path().join("a").join("b").join("c");
    fs.create_dir_all(&nested)?;
    assert!(fs.exists(&nested)?);

    let file_path = nested.join("data.bin");
    let opts = FsOpenOptions::new().write(true).create_new(true);
    let mut file = fs.open(&file_path, &opts)?;
    file.write_all(b"data")?;
    drop(file);

    let entries: Vec<_> = fs.read_dir(&nested)?;
    assert_eq!(entries.len(), 1);
    assert_eq!(entries[0].file_name, "data.bin");

    let meta = fs.metadata(&file_path)?;
    assert!(meta.is_file);
    assert_eq!(meta.len, 4);

    fs.remove_file(&file_path)?;
    assert!(!fs.exists(&file_path)?);

    let top = dir.path().join("a");
    fs.remove_dir_all(&top)?;
    assert!(!fs.exists(&top)?);

    Ok(())
}

#[test]
fn rename() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let src = dir.path().join("src.txt");
    let dst = dir.path().join("dst.txt");

    let opts = FsOpenOptions::new().write(true).create(true);
    let mut file = fs.open(&src, &opts)?;
    file.write_all(b"content")?;
    drop(file);

    fs.rename(&src, &dst)?;
    assert!(!fs.exists(&src)?);
    assert!(fs.exists(&dst)?);

    Ok(())
}

#[test]
fn sync_directory() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    fs.sync_directory(dir.path())?;
    Ok(())
}

#[test]
fn file_metadata() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let path = dir.path().join("meta.bin");
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"12345")?;

    let meta = file.metadata()?;
    assert!(meta.is_file);
    assert_eq!(meta.len, 5);

    Ok(())
}

#[test]
fn file_set_len() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let path = dir.path().join("truncate.bin");
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hello world")?;
    file.set_len(5)?;

    let meta = file.metadata()?;
    assert_eq!(meta.len, 5);

    Ok(())
}

#[test]
fn lock_exclusive() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;

    let path = dir.path().join("lockfile");
    let opts = FsOpenOptions::new().write(true).create(true);
    let file = fs.open(&path, &opts)?;
    file.lock_exclusive()?;

    Ok(())
}

#[test]
fn truncate_and_append() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("trunc.txt");

    let opts = FsOpenOptions::new().write(true).create(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hello world")?;
    drop(file);

    let opts = FsOpenOptions::new().write(true).truncate(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hi")?;
    drop(file);

    let meta = fs.metadata(&path)?;
    assert_eq!(meta.len, 2);

    let opts = FsOpenOptions::new().write(true).append(true);
    let mut file = fs.open(&path, &opts)?;
    // Seek to start, then write — append mode must ignore seek and
    // write at EOF regardless of cursor position.
    file.seek(SeekFrom::Start(0))?;
    file.write_all(b"!")?;
    drop(file);

    // Verify append went to EOF (len=3), not to start (which would
    // overwrite "hi" and keep len=2).
    let mut file = fs.open(&path, &FsOpenOptions::new().read(true))?;
    let mut buf = String::new();
    file.read_to_string(&mut buf)?;
    assert_eq!(buf, "hi!");
    assert_eq!(fs.metadata(&path)?.len, 3);

    Ok(())
}

#[test]
fn seek_operations() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("seek.bin");

    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"hello world")?;

    // Seek to start and re-read
    file.seek(SeekFrom::Start(0))?;
    let mut buf = [0u8; 5];
    file.read_exact(&mut buf)?;
    assert_eq!(&buf, b"hello");

    // Seek from current (+1 to skip space)
    file.seek(SeekFrom::Current(1))?;
    file.read_exact(&mut buf)?;
    assert_eq!(&buf, b"world");

    // Seek from end
    let pos = file.seek(SeekFrom::End(-5))?;
    assert_eq!(pos, 6);

    Ok(())
}

#[test]
fn concurrent_read_at() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("concurrent.bin");

    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    // Write 1000 bytes: each byte = (offset % 256)
    #[expect(clippy::cast_possible_truncation, reason = "% 256 guarantees 0..=255")]
    let data: Vec<u8> = (0u32..1000).map(|i| (i % 256) as u8).collect();
    file.write_all(&data)?;
    file.sync_all()?;

    let file = Arc::new(file);
    let mut handles = Vec::new();

    for chunk_start in (0..1000).step_by(100) {
        let file = Arc::clone(&file);
        handles.push(thread::spawn(move || -> io::Result<()> {
            let mut buf = [0u8; 100];
            let n = file.read_at(&mut buf, chunk_start as u64)?;
            assert_eq!(n, 100);
            for (i, &byte) in buf.iter().enumerate() {
                #[expect(clippy::cast_possible_truncation, reason = "% 256 guarantees 0..=255")]
                let expected = ((chunk_start + i) % 256) as u8;
                assert_eq!(byte, expected);
            }
            Ok(())
        }));
    }

    for h in handles {
        match h.join() {
            Ok(result) => result?,
            Err(_) => return Err(io::Error::other("thread panicked")),
        }
    }

    Ok(())
}

#[test]
fn metadata_directory() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let meta = fs.metadata(dir.path())?;
    assert!(meta.is_dir);
    assert!(!meta.is_file);

    Ok(())
}

#[test]
fn object_safety() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let fs: Arc<dyn Fs> = Arc::new(fs);
    let dir = tempfile::tempdir()?;
    let bogus = dir.path().join("nonexistent");
    assert!(!fs.exists(&bogus)?);
    Ok(())
}

#[test]
fn empty_buffer_returns_zero() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("empty_buf.bin");

    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"data")?;

    // read_at with empty buffer
    let n = file.read_at(&mut [], 0)?;
    assert_eq!(n, 0);

    // Read::read with empty buffer
    let n = file.read(&mut [])?;
    assert_eq!(n, 0);

    // Write::write with empty buffer
    let n = file.write(&[])?;
    assert_eq!(n, 0);

    // flush is a no-op
    file.flush()?;

    Ok(())
}

#[test]
fn sync_directory_rejects_file() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("not_a_dir.txt");

    let opts = FsOpenOptions::new().write(true).create(true);
    fs.open(&path, &opts)?;

    match fs.sync_directory(&path) {
        Ok(()) => panic!("sync_directory on a file should fail"),
        // sync_directory is an `Fs` method → returns `crate::io::Result`.
        Err(err) => assert_eq!(err.kind(), crate::io::ErrorKind::InvalidInput),
    }

    Ok(())
}

#[test]
fn seek_overflow_returns_error() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("seek_overflow.bin");

    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"data")?;

    // Seek to near u64::MAX, then seek forward — should overflow.
    file.seek(SeekFrom::Start(u64::MAX - 1))?;
    match file.seek(SeekFrom::Current(2)) {
        Ok(_) => panic!("seek past u64::MAX should fail"),
        Err(err) => assert_eq!(err.kind(), io::ErrorKind::InvalidInput),
    }

    // SeekFrom::Current negative past zero — should underflow.
    file.seek(SeekFrom::Start(0))?;
    match file.seek(SeekFrom::Current(-1)) {
        Ok(_) => panic!("seek before zero should fail"),
        Err(err) => assert_eq!(err.kind(), io::ErrorKind::InvalidInput),
    }

    // SeekFrom::End negative past zero — should underflow.
    match file.seek(SeekFrom::End(-100)) {
        Ok(_) => panic!("seek before zero should fail"),
        Err(err) => assert_eq!(err.kind(), io::ErrorKind::InvalidInput),
    }

    Ok(())
}

#[test]
fn debug_impl() {
    let Some(fs) = try_io_uring() else {
        return;
    };
    let debug = format!("{fs:?}");
    assert!(debug.contains("IoUringFs"));
}

#[test]
fn with_ring_size() -> io::Result<()> {
    if !is_io_uring_available() {
        eprintln!("skipping: io_uring not supported by kernel");
        return Ok(());
    }
    let fs =
        IoUringFs::with_ring_size(64).expect("io_uring available but with_ring_size(64) failed");
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("ring64.bin");
    let opts = FsOpenOptions::new().write(true).create(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"ok")?;
    file.sync_all()?;
    assert_eq!(fs.metadata(&path)?.len, 2);
    Ok(())
}

#[test]
fn seek_negative_from_current() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("seek_neg.bin");

    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(b"abcdefghij")?;

    // Seek to position 8, then back 3
    file.seek(SeekFrom::Start(8))?;
    let pos = file.seek(SeekFrom::Current(-3))?;
    assert_eq!(pos, 5);

    let mut buf = [0u8; 5];
    file.read_exact(&mut buf)?;
    assert_eq!(&buf, b"fghij");

    Ok(())
}

#[test]
fn clone_shares_ring() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let fs2 = fs.clone();
    let dir = tempfile::tempdir()?;

    // Both clones should work with the same ring thread.
    let p1 = dir.path().join("a.txt");
    let p2 = dir.path().join("b.txt");
    let opts = FsOpenOptions::new().write(true).create(true);

    let mut f1 = fs.open(&p1, &opts)?;
    let mut f2 = fs2.open(&p2, &opts)?;
    f1.write_all(b"one")?;
    f2.write_all(b"two")?;
    f1.sync_all()?;
    f2.sync_all()?;

    assert_eq!(fs.metadata(&p1)?.len, 3);
    assert_eq!(fs2.metadata(&p2)?.len, 3);

    Ok(())
}

#[test]
fn available_space_reports_plausible_free_bytes() -> io::Result<()> {
    // The cold-path free-space probe delegates to the shared statvfs helper:
    // the filesystem backing the tempdir must report a plausible, non-zero
    // figure below the unbounded sentinel.
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let free = fs.available_space(dir.path())?;
    assert!(
        free > 0,
        "a writable tempdir filesystem must report free space"
    );
    assert!(
        free < u64::MAX,
        "a real probe must not return the unbounded sentinel"
    );
    Ok(())
}

#[test]
fn read_many_fills_every_region() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("read_many.bin");
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    let data: Vec<u8> = (0..=255u8).collect();
    file.write_all(&data)?;
    file.sync_all()?;

    // Disjoint regions in non-file order plus an EMPTY region (the submit loop
    // skips it) must each be filled by the one batched submission.
    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 8];
    let mut empty: [u8; 0] = [];
    let mut b2 = [0u8; 1];
    let mut regions: Vec<(u64, &mut [u8])> = vec![
        (10, &mut b0[..]),
        (200, &mut b1[..]),
        (50, &mut empty[..]),
        (0, &mut b2[..]),
    ];
    file.read_many(&mut regions)?;
    drop(regions);

    assert_eq!(&b0[..], &data[10..14]);
    assert_eq!(&b1[..], &data[200..208]);
    assert_eq!(b2[0], data[0]);
    Ok(())
}

#[test]
fn read_blocks_batched_across_files_via_ring() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut f0 = fs.open(&dir.path().join("a.bin"), &opts)?;
    f0.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    f0.sync_all()?;
    let mut f1 = fs.open(&dir.path().join("b.bin"), &opts)?;
    let rev: Vec<u8> = (0..=255u8).rev().collect();
    f1.write_all(&rev)?;
    f1.sync_all()?;

    // Both handles back onto the SAME shared ring, so reads from two files
    // submit in one batch (submit_reads_multi, per-request fd).
    assert!(f0.backing_fd().is_some(), "io_uring file exposes its fd");
    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: f0.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: f1.as_ref(),
                offset: 20,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)?;
    }
    assert_eq!(b0, [10, 11, 12, 13]);
    assert_eq!(b1, [rev[20], rev[21], rev[22], rev[23]]);
    Ok(())
}

/// A request whose destination arrives partly filled must resume at
/// `offset + filled` on the ring path too: the SQE's pointer and length
/// already describe the unfilled suffix, so submitting the original offset
/// would duplicate the block's first bytes into that suffix.
#[test]
fn read_blocks_batched_partly_filled_request_resumes_via_ring() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&dir.path().join("resume.bin"), &opts)?;
    file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    file.sync_all()?;
    assert!(file.backing_fd().is_some(), "io_uring file exposes its fd");

    // Block = bytes 10..18; the first 3 are already in the destination, so the
    // ring owes bytes 13..18 into the suffix.
    let mut buf = [0u8; 8];
    {
        let mut dst = crate::fs::BlockBuf::new(&mut buf);
        dst.append(&[10, 11, 12]);
        let mut reqs = vec![crate::fs::BlockRead {
            file: file.as_ref(),
            offset: 10,
            buf: dst,
        }];
        fs.read_blocks_batched(&mut reqs)?;
        assert!(reqs[0].buf.is_full(), "the request is completed, not short");
    }
    assert_eq!(buf, [10, 11, 12, 13, 14, 15, 16, 17]);
    Ok(())
}

#[test]
fn read_many_short_read_at_eof_errors() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let path = dir.path().join("short_many.bin");
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&path, &opts)?;
    file.write_all(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
    file.sync_all()?;

    // First region runs past EOF (a fixed-size short read = UnexpectedEof, not
    // EOF); the second is in range. The first failing must NOT short-circuit the
    // drain: every later op is still recv'd so its in-flight kernel write
    // completes before the buffers are freed. The call surfaces the first error.
    let mut short = [0u8; 32];
    let mut ok = [0xAAu8; 4]; // sentinel: the drained read must overwrite it
    let mut regions: Vec<(u64, &mut [u8])> = vec![(0, &mut short[..]), (4, &mut ok[..])];
    match file.read_many(&mut regions) {
        Ok(()) => panic!("read past EOF must fail, not report a short read as success"),
        Err(err) => assert_eq!(err.kind(), crate::io::ErrorKind::UnexpectedEof),
    }
    // The in-range op was drained to completion despite the earlier short read:
    // its buffer holds the on-disk bytes, not the sentinel.
    assert_eq!(ok, [5, 6, 7, 8]);
    Ok(())
}

#[test]
fn read_blocks_batched_short_read_at_eof_errors() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut file = fs.open(&dir.path().join("short_block.bin"), &opts)?;
    file.write_all(&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10])?;
    file.sync_all()?;

    // First block runs past EOF; the second is in range. The failing block must
    // not short-circuit the drain (every later op is recv'd before return).
    let mut short = [0u8; 64];
    let mut ok = [0xAAu8; 4]; // sentinel: the drained read must overwrite it
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut short),
            },
            crate::fs::BlockRead {
                file: file.as_ref(),
                offset: 4,
                buf: crate::fs::BlockBuf::new(&mut ok),
            },
        ];
        match fs.read_blocks_batched(&mut reqs) {
            Ok(()) => panic!("read past EOF must fail, not report a short read as success"),
            Err(err) => assert_eq!(err.kind(), crate::io::ErrorKind::UnexpectedEof),
        }
    }
    // The in-range op was drained to completion: its buffer holds on-disk bytes.
    assert_eq!(ok, [5, 6, 7, 8]);
    Ok(())
}

#[test]
fn read_blocks_batched_fallback_short_read_errors() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(1..=64u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    // A StdFs handle (no fd for the ring) forces the whole batch onto the serial
    // read_at fallback; its block runs past EOF, so the fallback's fixed-size
    // short read is reported as UnexpectedEof, not a silent partial fill.
    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&[0u8; 10])?;
    std_file.sync_all()?;

    let mut b0 = [0xAAu8; 8]; // sentinel: the in-range fallback read must fill it
    let mut b1 = [0u8; 64]; // past EOF on the std file
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        match fs.read_blocks_batched(&mut reqs) {
            Ok(()) => panic!("a short read in the serial fallback must fail"),
            Err(err) => assert_eq!(err.kind(), crate::io::ErrorKind::UnexpectedEof),
        }
    }
    // The in-range block was read by the fallback before the short one failed.
    assert_eq!(b0, [1, 2, 3, 4, 5, 6, 7, 8]);
    Ok(())
}

#[test]
fn read_blocks_batched_serves_a_mixed_batch_without_degrading_the_ring_group() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;
    // Wrapped so the test can see whether this request was submitted to the
    // ring or read through the serial `read_at` fallback.
    let counted = CountingFile::new(uring_file);

    // A StdFs handle has no fd for the ring (backing_fd None), so it cannot be
    // submitted and is read serially — but only it.
    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    let rev: Vec<u8> = (0..=255u8).rev().collect();
    std_file.write_all(&rev)?;
    std_file.sync_all()?;
    assert_eq!(std_file.backing_fd(), None);

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: &counted,
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 20,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)?;
    }
    assert_eq!(b0, [10, 11, 12, 13]);
    assert_eq!(b1, [rev[20], rev[21], rev[22], rev[23]]);
    assert_eq!(
        counted.read_at_calls(),
        0,
        "the descriptor-bearing request went to the ring; one handle without a \
         descriptor must not drag the rest onto the serial path",
    );
    Ok(())
}

/// The split must not reorder what each request reads: every destination holds
/// the bytes ITS `(file, offset)` names, whichever group served it.
#[test]
fn read_blocks_batched_keeps_each_destination_with_its_own_request() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    let rev: Vec<u8> = (0..=255u8).rev().collect();
    std_file.write_all(&rev)?;
    std_file.sync_all()?;

    // Interleaved, so a split that preserved only group-internal order would
    // still be caught. Offsets stay within the 256-byte fixtures, and `u8`
    // keeps the expected bytes derivable without a narrowing cast.
    let mut bufs = [[0u8; 2]; 6];
    let offsets: [u8; 6] = [0, 10, 20, 30, 40, 50];
    {
        let mut reqs: Vec<crate::fs::BlockRead<'_>> = Vec::new();
        for (i, (buf, offset)) in bufs.iter_mut().zip(offsets).enumerate() {
            reqs.push(crate::fs::BlockRead {
                file: if i % 2 == 0 {
                    uring_file.as_ref()
                } else {
                    std_file.as_ref()
                },
                offset: u64::from(offset),
                buf: crate::fs::BlockBuf::new(&mut buf[..]),
            });
        }
        fs.read_blocks_batched(&mut reqs)?;
    }
    for (i, (buf, offset)) in bufs.iter().zip(offsets).enumerate() {
        let at = usize::from(offset);
        // The uring fixture holds byte value == its offset; the std fixture
        // holds the reverse.
        let expected: [u8; 2] = if i % 2 == 0 {
            [offset, offset + 1]
        } else {
            [rev[at], rev[at + 1]]
        };
        assert_eq!(*buf, expected, "request {i} was served the wrong bytes");
    }
    Ok(())
}

/// A batch where NOTHING can be submitted still reads correctly: the ring group
/// is empty and every request takes the serial path.
#[test]
fn read_blocks_batched_with_no_submittable_request_reads_serially() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 100,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)?;
    }
    assert_eq!(b0, [0, 1, 2, 3]);
    assert_eq!(b1, [100, 101, 102, 103]);
    Ok(())
}

/// A short read in the SERIAL half of a mixed batch is still the documented
/// `UnexpectedEof`, and the ring half is still filled — the split must not turn
/// one group's failure into silence about the other.
#[test]
fn read_blocks_batched_reports_a_short_serial_read_in_a_mixed_batch() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&[1, 2, 3, 4])?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 8]; // past EOF of the 4-byte file
    let err = {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("a short read on a fixed-size block is an error")
    };
    assert_eq!(err.kind(), crate::io::ErrorKind::UnexpectedEof, "{err}");
    assert_eq!(b0, [10, 11, 12, 13], "the ring group was served first");
    Ok(())
}

/// A short read in the RING half of a mixed batch fails the whole call, and
/// the serial half is left unread: the ring group is submitted first and its
/// failure is surfaced before anything else runs.
#[test]
fn read_blocks_batched_reports_a_short_ring_read_in_a_mixed_batch() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&[1, 2, 3, 4])?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 8]; // past EOF of the 4-byte uring file
    let mut b1 = [0u8; 4];
    let err = {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("a short read on a fixed-size block is an error")
    };
    assert_eq!(err.kind(), crate::io::ErrorKind::UnexpectedEof, "{err}");
    Ok(())
}

/// A destination that arrives partly filled owns the block's first bytes
/// already, so the read covers the suffix from `offset + filled` — on both
/// sides of the split.
#[test]
fn read_blocks_batched_resumes_partly_filled_destinations_in_a_mixed_batch() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    let rev: Vec<u8> = (0..=255u8).rev().collect();
    std_file.write_all(&rev)?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    {
        let mut buf0 = crate::fs::BlockBuf::new(&mut b0);
        let mut buf1 = crate::fs::BlockBuf::new(&mut b1);
        // Two bytes of each block are already owned by the caller.
        assert_eq!(buf0.append(&[0xAA, 0xBB]), 2);
        assert_eq!(buf1.append(&[0xCC, 0xDD]), 2);
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: buf0,
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 20,
                buf: buf1,
            },
        ];
        fs.read_blocks_batched(&mut reqs)?;
        assert!(reqs.iter().all(|r| r.buf.is_full()));
    }
    // The suffix starts two bytes into each block, and the prefix is untouched.
    assert_eq!(b0, [0xAA, 0xBB, 12, 13]);
    assert_eq!(b1, [0xCC, 0xDD, rev[22], rev[23]]);
    Ok(())
}

/// An empty request is a no-op wherever it lands, including in the middle of a
/// split batch: the ring skips it and the serial loop reads nothing for it.
#[test]
fn read_blocks_batched_tolerates_an_empty_request_in_a_mixed_batch() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    let rev: Vec<u8> = (0..=255u8).rev().collect();
    std_file.write_all(&rev)?;
    std_file.sync_all()?;

    let mut empty_uring: [u8; 0] = [];
    let mut empty_std: [u8; 0] = [];
    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut empty_uring),
            },
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut empty_std),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 20,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)?;
        assert!(
            reqs.iter().all(|r| r.buf.is_full()),
            "an empty destination is trivially full, and the rest were filled",
        );
    }
    assert_eq!(b0, [10, 11, 12, 13]);
    assert_eq!(b1, [rev[20], rev[21], rev[22], rev[23]]);
    Ok(())
}

/// When BOTH halves of a split batch fail, the error returned is the one whose
/// request came first in the CALLER's order — the contract is the first failing
/// block, and splitting must not re-order which failure wins. The serial
/// request is placed first here, so its error has to beat the ring's.
#[test]
fn read_blocks_batched_reports_the_earliest_failure_when_both_halves_fail() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    // Both files are 4 bytes, so both requests below read past EOF.
    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&[1, 2, 3, 4])?;
    uring_file.sync_all()?;
    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&[1, 2, 3, 4])?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 8];
    let mut b1 = [0u8; 8];
    let err = {
        let mut serial_first = crate::fs::BlockBuf::new(&mut b0);
        // An overflowing resume offset, which is `InvalidInput` — distinct from
        // the ring half's `UnexpectedEof`, so the verdict says which one won.
        assert_eq!(serial_first.append(&[0xAA, 0xBB]), 2);
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: u64::MAX - 1,
                buf: serial_first,
            },
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("both halves fail, so the call fails")
    };
    assert_eq!(
        err.kind(),
        crate::io::ErrorKind::InvalidInput,
        "the request at index 0 is the serial one, so its error wins: {err}",
    );
    Ok(())
}

/// The mirror of the case above: when the RING half holds the earlier request,
/// its error is the one reported.
#[test]
fn read_blocks_batched_prefers_the_ring_failure_when_it_came_first() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&[1, 2, 3, 4])?;
    uring_file.sync_all()?;
    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&[1, 2, 3, 4])?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 8];
    let mut b1 = [0u8; 8];
    let err = {
        let mut serial_second = crate::fs::BlockBuf::new(&mut b1);
        assert_eq!(serial_second.append(&[0xAA, 0xBB]), 2);
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: u64::MAX - 1,
                buf: serial_second,
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("both halves fail, so the call fails")
    };
    assert_eq!(
        err.kind(),
        crate::io::ErrorKind::UnexpectedEof,
        "the request at index 0 is the ring one, so its short read wins: {err}",
    );
    Ok(())
}

/// The resume-offset overflow guard covers the RING half too: a submittable
/// request whose partly filled destination would push its offset past `u64`
/// is refused before anything is submitted, not wrapped around into a read of
/// some other part of the file.
#[test]
fn read_blocks_batched_rejects_an_overflowing_resume_offset_on_the_ring_side() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let mut buf = [0u8; 4];
    let err = {
        let mut dst = crate::fs::BlockBuf::new(&mut buf);
        assert_eq!(dst.append(&[0xAA, 0xBB]), 2);
        let mut reqs = vec![crate::fs::BlockRead {
            file: uring_file.as_ref(),
            offset: u64::MAX - 1,
            buf: dst,
        }];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("an overflowing resume offset must be refused")
    };
    assert_eq!(err.kind(), crate::io::ErrorKind::InvalidInput, "{err}");
    Ok(())
}

/// A serial read that FAILS outright (as opposed to coming up short) is
/// reported as itself: the backend's own error, not a substitute.
#[test]
fn read_blocks_batched_surfaces_a_serial_read_error_verbatim() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    // A descriptor-less handle whose reads are made to fail.
    let faulty = crate::fs::FaultFs::new(crate::fs::StdFs);
    let mut std_file = faulty.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    std_file.sync_all()?;
    assert_eq!(std_file.backing_fd(), None);
    faulty.injector().arm(crate::fs::FaultRule::new(
        crate::fs::FaultOp::ReadAt,
        crate::fs::Fault::Error(crate::io::ErrorKind::PermissionDenied),
    ));

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    let err = {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: 20,
                buf: crate::fs::BlockBuf::new(&mut b1),
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("the serial half's read fails")
    };
    assert_eq!(
        err.kind(),
        crate::io::ErrorKind::PermissionDenied,
        "the backend's own error is what the caller sees: {err}",
    );
    assert_eq!(b0, [10, 11, 12, 13], "the ring half still ran");
    Ok(())
}

/// A ring request that SUCCEEDS must not lend its index to a later one that
/// fails. With a good ring read at 0, a failing serial read at 1 and a failing
/// ring read at 2, the winner is the serial failure at index 1 — attributing
/// the ring's failure to the group's first request would wrongly report index 0
/// and hand back the wrong error.
#[test]
fn read_blocks_batched_blames_the_ring_request_that_failed_not_its_group() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;
    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&[1, 2, 3, 4])?;
    std_file.sync_all()?;

    let mut good = [0u8; 4];
    let mut serial_bad = [0u8; 4];
    let mut ring_bad = [0u8; 8];
    let err = {
        let mut serial_buf = crate::fs::BlockBuf::new(&mut serial_bad);
        // Overflowing resume offset → InvalidInput, at index 1.
        assert_eq!(serial_buf.append(&[0xAA, 0xBB]), 2);
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut good),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: u64::MAX - 1,
                buf: serial_buf,
            },
            crate::fs::BlockRead {
                // Reads past the 256-byte fixture's end → UnexpectedEof, at 2.
                file: uring_file.as_ref(),
                offset: 252,
                buf: crate::fs::BlockBuf::new(&mut ring_bad),
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("two of the three requests fail")
    };
    assert_eq!(
        err.kind(),
        crate::io::ErrorKind::InvalidInput,
        "index 1 fails before index 2, so the serial error wins: {err}",
    );
    Ok(())
}

/// An offset that would overflow while resuming a partly filled destination is
/// rejected, not wrapped into a read somewhere else in the file. The check
/// lives in the serial half of the split, so a mixed batch has to reach it.
#[test]
fn read_blocks_batched_rejects_an_overflowing_resume_offset() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let opts = FsOpenOptions::new().write(true).create(true).read(true);

    let mut uring_file = fs.open(&dir.path().join("u.bin"), &opts)?;
    uring_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    uring_file.sync_all()?;

    let std_fs = crate::fs::StdFs;
    let mut std_file = std_fs.open(&dir.path().join("s.bin"), &opts)?;
    std_file.write_all(&(0..=255u8).collect::<Vec<_>>())?;
    std_file.sync_all()?;

    let mut b0 = [0u8; 4];
    let mut b1 = [0u8; 4];
    let err = {
        let mut buf1 = crate::fs::BlockBuf::new(&mut b1);
        // Two bytes already owned, so the resume offset is `u64::MAX - 1 + 2`.
        assert_eq!(buf1.append(&[0xAA, 0xBB]), 2);
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: uring_file.as_ref(),
                offset: 10,
                buf: crate::fs::BlockBuf::new(&mut b0),
            },
            crate::fs::BlockRead {
                file: std_file.as_ref(),
                offset: u64::MAX - 1,
                buf: buf1,
            },
        ];
        fs.read_blocks_batched(&mut reqs)
            .expect_err("an overflowing resume offset must be refused")
    };
    assert_eq!(err.kind(), crate::io::ErrorKind::InvalidInput, "{err}");
    Ok(())
}

/// Wraps a file handle, passing its descriptor through so the ring still
/// accepts it, and counts the serial `read_at` calls made against it. A count
/// above zero says the request took the fallback path.
struct CountingFile {
    inner: Box<dyn crate::fs::FsFile>,
    read_at_calls: std::sync::atomic::AtomicUsize,
}

impl CountingFile {
    fn new(inner: Box<dyn crate::fs::FsFile>) -> Self {
        Self {
            inner,
            read_at_calls: std::sync::atomic::AtomicUsize::new(0),
        }
    }

    fn read_at_calls(&self) -> usize {
        self.read_at_calls
            .load(std::sync::atomic::Ordering::Relaxed)
    }
}

impl io::Read for CountingFile {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.inner.read(buf)
    }
}

impl io::Write for CountingFile {
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.inner.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl io::Seek for CountingFile {
    fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
        self.inner.seek(pos)
    }
}

impl crate::fs::FsFile for CountingFile {
    fn sync_all(&self) -> crate::io::Result<()> {
        self.inner.sync_all()
    }

    fn sync_data(&self) -> crate::io::Result<()> {
        self.inner.sync_data()
    }

    fn metadata(&self) -> crate::io::Result<crate::fs::FsMetadata> {
        self.inner.metadata()
    }

    fn set_len(&self, size: u64) -> crate::io::Result<()> {
        self.inner.set_len(size)
    }

    fn read_at(&self, buf: &mut [u8], offset: u64) -> crate::io::Result<usize> {
        self.read_at_calls
            .fetch_add(1, std::sync::atomic::Ordering::Relaxed);
        self.inner.read_at(buf, offset)
    }

    fn backing_fd(&self) -> Option<i32> {
        self.inner.backing_fd()
    }

    fn lock_exclusive(&self) -> crate::io::Result<()> {
        self.inner.lock_exclusive()
    }
}

#[test]
fn volume_id_matches_the_kernel_mount() -> io::Result<()> {
    // Free space is a property of the mount, not the I/O submission path, so
    // the uring backend reports the same volume id as `StdFs` for a path —
    // letting the space gate treat a uring data dir and a `StdFs` blob dir on
    // the same mount as one free-space pool.
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    assert_eq!(
        fs.volume_id(dir.path()),
        crate::fs::StdFs.volume_id(dir.path()),
        "uring and std agree on the mount backing a path"
    );
    assert!(fs.volume_id(dir.path()).is_some(), "a real mount has an id");
    Ok(())
}

/// Two files of `len` bytes each, byte `i` of file `f` being `(i + f) % 251`.
fn two_files(fs: &IoUringFs, dir: &Path, len: usize) -> io::Result<Vec<Box<dyn FsFile>>> {
    let opts = FsOpenOptions::new().write(true).create(true).read(true);
    let mut files = Vec::new();
    for f in 0..2usize {
        let mut file = fs.open(&dir.join(format!("f{f}.bin")), &opts)?;
        let bytes: Vec<u8> = (0..len)
            .map(|i| u8::try_from((i + f) % 251).expect("below 251"))
            .collect();
        file.write_all(&bytes)?;
        file.sync_all()?;
        files.push(file);
    }
    Ok(files)
}

/// What a caller spends per batch before the kernel sees it is one completion
/// channel and one acquisition of the submission lock, however many reads the
/// batch holds.
#[test]
fn a_batched_read_takes_one_channel_and_one_lock_per_batch() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files = two_files(&fs, dir.path(), 64 * 16)?;
    let mut buffers = vec![[0u8; 16]; 64];
    let mut reqs: Vec<_> = buffers
        .iter_mut()
        .enumerate()
        .map(|(i, buf)| crate::fs::BlockRead {
            file: files[i % 2].as_ref(),
            offset: (i * 16) as u64,
            buf: crate::fs::BlockBuf::new(buf),
        })
        .collect();

    let counts = &fs.inner.counts;
    let channels = counts.channels.load(core::sync::atomic::Ordering::Relaxed);
    let locks = counts.locks.load(core::sync::atomic::Ordering::Relaxed);
    fs.read_blocks_batched(&mut reqs)?;
    assert_eq!(
        counts.channels.load(core::sync::atomic::Ordering::Relaxed) - channels,
        1
    );
    assert_eq!(
        counts.locks.load(core::sync::atomic::Ordering::Relaxed) - locks,
        1
    );
    drop(reqs);
    for (i, buf) in buffers.iter().enumerate() {
        let expected: Vec<u8> = (0..16)
            .map(|b| u8::try_from((i * 16 + b + i % 2) % 251).expect("below 251"))
            .collect();
        assert_eq!(buf.as_slice(), expected, "block {i}");
    }
    Ok(())
}

/// `drain_batch` over completions fed in the given order: `(position, result)`.
fn drain_in(
    expected: &[usize],
    order: &[(usize, i32)],
) -> (Result<(), (usize, io::Error)>, Vec<usize>) {
    let mut feed = order.iter().copied();
    let mut handed = Vec::new();
    let verdict = drain_batch(expected, || feed.next(), "short", |p| handed.push(p));
    (verdict, handed)
}

/// Which failure a batch reports is a rule over the requests, not a race: the
/// lowest position, whatever order its completions arrive in.
#[test]
fn a_failed_batch_reports_the_lowest_failure_in_any_completion_order() {
    // Position 1 fails with EIO, position 3 reads short, position 4 fails with
    // ENOSPC; 0 and 2 succeed.
    let expected = [8, 8, 8, 8, 8];
    let events = [(0, 8), (1, -5), (2, 8), (3, 2), (4, -28)];
    let forward: Vec<_> = events.to_vec();
    let reverse: Vec<_> = events.iter().rev().copied().collect();
    let shuffled = vec![events[3], events[0], events[4], events[2], events[1]];

    for order in [forward, reverse, shuffled] {
        let (verdict, mut handed) = drain_in(&expected, &order);
        let Err((position, error)) = verdict else {
            panic!("a batch with failures must fail ({order:?})");
        };
        assert_eq!(position, 1, "{order:?}");
        assert_eq!(error.raw_os_error(), Some(5), "{order:?}");
        handed.sort_unstable();
        assert_eq!(
            handed,
            [0, 2],
            "only full reads are handed over ({order:?})"
        );
    }
}

/// A read that never completes, because the ring went away, is a broken pipe
/// at its position; a failure at a lower position still wins over it.
#[test]
fn a_batch_cut_short_reports_the_missing_read() {
    let (verdict, _) = drain_in(&[4, 4, 4], &[(0, 4), (2, 4)]);
    let Err((position, error)) = verdict else {
        panic!("a read that never completed must fail the batch");
    };
    assert_eq!(position, 1);
    assert_eq!(error.kind(), io::ErrorKind::BrokenPipe);

    let (verdict, _) = drain_in(&[4, 4, 4], &[(2, 4), (0, -5)]);
    assert!(matches!(verdict, Err((0, _))));
}

/// A finished read is handed over before later completions are even looked
/// at: the caller works on it while the rest are still in flight.
#[test]
fn a_finished_read_is_handed_over_before_later_completions_arrive() {
    let log = core::cell::RefCell::new(Vec::new());
    let mut feed = [(2, 4), (0, 4), (1, 4)].into_iter();
    let verdict = drain_batch(
        &[4, 4, 4],
        || {
            let next = feed.next();
            if let Some((position, _)) = next {
                log.borrow_mut().push(format!("complete {position}"));
            }
            next
        },
        "short",
        |position| log.borrow_mut().push(format!("hand {position}")),
    );
    assert!(verdict.is_ok());
    assert_eq!(
        log.into_inner(),
        [
            "complete 2",
            "hand 2",
            "complete 0",
            "hand 0",
            "complete 1",
            "hand 1"
        ]
    );
}

/// Handing requests over as they complete returns exactly what the ordered
/// path returns: the same requests, filled with the same bytes.
#[test]
fn read_blocks_batched_each_returns_what_the_ordered_path_returns() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files = two_files(&fs, dir.path(), 32 * 64)?;
    let read = |each: bool| -> io::Result<(Vec<[u8; 64]>, Vec<usize>)> {
        let mut buffers = vec![[0u8; 64]; 32];
        let mut handed = Vec::new();
        {
            let mut reqs: Vec<_> = buffers
                .iter_mut()
                .enumerate()
                .map(|(i, buf)| crate::fs::BlockRead {
                    file: files[i % 2].as_ref(),
                    offset: (i * 64) as u64,
                    buf: crate::fs::BlockBuf::new(buf),
                })
                .collect();
            if each {
                fs.read_blocks_batched_each(&mut reqs, &mut |i, req| {
                    assert!(req.buf.is_full(), "request {i} handed over unfilled");
                    handed.push(i);
                })?;
            } else {
                fs.read_blocks_batched(&mut reqs)?;
            }
        }
        handed.sort_unstable();
        Ok((buffers, handed))
    };
    let (ordered, _) = read(false)?;
    let (each, handed) = read(true)?;
    assert_eq!(ordered, each);
    assert_eq!(handed, (0..32).collect::<Vec<_>>());
    Ok(())
}

/// A batch mixing reads that fail with reads that succeed, run many times:
/// every successful read is filled and handed over, none that failed is, and
/// the failure reported is always the lowest one. The buffers of reads still
/// in flight when an earlier one fails are written before the call returns.
#[test]
fn a_mixed_batch_drains_every_read_and_reports_the_lowest_failure() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files = two_files(&fs, dir.path(), 16 * 16)?;
    for _ in 0..200 {
        // Every third request reads past the end of its file.
        let mut buffers = vec![[0xAAu8; 16]; 24];
        let mut handed = Vec::new();
        let verdict = {
            let mut reqs: Vec<_> = buffers
                .iter_mut()
                .enumerate()
                .map(|(i, buf)| crate::fs::BlockRead {
                    file: files[i % 2].as_ref(),
                    offset: if i % 3 == 1 {
                        1 << 20
                    } else {
                        (i % 16 * 16) as u64
                    },
                    buf: crate::fs::BlockBuf::new(buf),
                })
                .collect();
            fs.read_blocks_batched_each(&mut reqs, &mut |i, _| handed.push(i))
        };
        let Err(error) = verdict else {
            panic!("reads past the end must fail the batch");
        };
        assert_eq!(error.kind(), crate::io::ErrorKind::UnexpectedEof);
        handed.sort_unstable();
        let succeeded: Vec<usize> = (0..24).filter(|i| i % 3 != 1).collect();
        assert_eq!(handed, succeeded);
        for &i in &succeeded {
            assert_ne!(buffers[i], [0xAAu8; 16], "read {i} was drained and written");
        }
    }
    Ok(())
}

/// A callback that panics must not cut the drain short: the reads still in
/// flight write into buffers the unwind would free, so every completion is
/// received before the panic carries on.
#[test]
fn a_panicking_callback_still_drains_every_completion() {
    let received = core::cell::Cell::new(0usize);
    let mut feed = [(0, 4), (1, 4), (2, 4)].into_iter();
    let unwound = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
        drain_batch(
            &[4, 4, 4],
            || {
                let next = feed.next();
                if next.is_some() {
                    received.set(received.get() + 1);
                }
                next
            },
            "short",
            |_| panic!("the caller's work on a finished read failed"),
        )
    }));
    assert!(unwound.is_err(), "the callback's panic carries on");
    assert_eq!(received.get(), 3, "every completion was received first");
}

/// An empty request is complete without a read, so it is handed over like
/// every other finished one, as the default and the serial path do.
#[test]
fn an_empty_request_in_a_batch_is_handed_over() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files = two_files(&fs, dir.path(), 64)?;
    let mut first = [0u8; 16];
    let mut empty = [0u8; 0];
    let mut last = [0u8; 16];
    let mut handed = Vec::new();
    {
        let mut reqs = vec![
            crate::fs::BlockRead {
                file: files[0].as_ref(),
                offset: 0,
                buf: crate::fs::BlockBuf::new(&mut first),
            },
            crate::fs::BlockRead {
                file: files[1].as_ref(),
                offset: 16,
                buf: crate::fs::BlockBuf::new(&mut empty),
            },
            crate::fs::BlockRead {
                file: files[1].as_ref(),
                offset: 32,
                buf: crate::fs::BlockBuf::new(&mut last),
            },
        ];
        fs.read_blocks_batched_each(&mut reqs, &mut |i, _| handed.push(i))?;
    }
    handed.sort_unstable();
    assert_eq!(handed, [0, 1, 2]);
    Ok(())
}

/// The bytes a [`two_files`] file `f` holds at `offset`, `len` of them.
fn expected_bytes(f: usize, offset: usize, len: usize) -> Vec<u8> {
    (offset..offset + len)
        .map(|i| u8::try_from((i + f) % 251).expect("below 251"))
        .collect()
}

/// Reads submitted to the queue across two files come back with their tags
/// and bytes, and reads submitted after a wait, while earlier ones may still
/// be on the ring, are told apart from them.
#[test]
fn a_read_queue_hands_back_reads_of_several_submissions() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 64 * 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = fs.read_queue();
    let mut got: Vec<(usize, Vec<u8>)> = Vec::new();
    for round in 0..4usize {
        for i in 0..16usize {
            let tag = round * 16 + i;
            queue.submit(QueuedRead {
                tag,
                file: Arc::clone(&files[tag % 2]),
                offset: (tag * 64) as u64,
                buf: vec![0; 64],
            });
        }
        // Issues this round's reads and takes whatever has come back.
        queue.wait(0, &mut |done| {
            assert!(done.result.is_ok(), "read {} failed", done.tag);
            got.push((done.tag, done.buf));
        });
    }
    while queue.outstanding() > 0 {
        queue.wait(1, &mut |done| {
            assert!(done.result.is_ok(), "read {} failed", done.tag);
            got.push((done.tag, done.buf));
        });
    }
    got.sort_unstable_by_key(|(tag, _)| *tag);
    let expected: Vec<(usize, Vec<u8>)> = (0..64)
        .map(|tag| (tag, expected_bytes(tag % 2, tag * 64, 64)))
        .collect();
    assert_eq!(got, expected);
    Ok(())
}

/// A read without a descriptor, which only a wait for some reads, is left
/// submitted by a wait for none, so a wrapper takes it back with the reads
/// that wait kept, and reads it at the wait that issues it.
#[test]
fn a_wait_for_none_keeps_a_read_without_a_descriptor_for_take_kept() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let mem = crate::fs::MemFs::new();
    mem.create_dir_all(Path::new("/d"))?;
    mem.open(
        Path::new("/d/f"),
        &FsOpenOptions::new().write(true).create(true),
    )?
    .write_all(&[7; 16])?;
    let file: Arc<dyn FsFile> =
        Arc::from(mem.open(Path::new("/d/f"), &FsOpenOptions::new().read(true))?);
    assert!(file.backing_fd().is_none(), "self-check: no descriptor");

    let mut queue = fs.read_queue();
    queue.submit(QueuedRead {
        tag: 3,
        file,
        offset: 0,
        buf: vec![0; 16],
    });
    let mut done = Vec::new();
    queue.wait(0, &mut |read| done.push(read));
    assert!(done.is_empty(), "a wait for none reads nothing serially");
    let kept = queue.take_kept();
    assert_eq!(
        kept.iter().map(|read| read.tag).collect::<Vec<_>>(),
        vec![3],
        "the read is handed back"
    );
    assert_eq!(queue.outstanding(), 0, "the queue no longer holds it");

    for read in kept {
        queue.submit(read);
    }
    queue.wait(1, &mut |read| done.push(read));
    assert_eq!(done.len(), 1);
    assert_eq!(done[0].buf, vec![7; 16]);
    Ok(())
}

/// Counts the wakes a queue gives it.
struct CountWake(core::sync::atomic::AtomicUsize);

impl crate::fs::ReadWake for CountWake {
    fn wake(&self) {
        self.0.fetch_add(1, core::sync::atomic::Ordering::SeqCst);
    }
}

/// A queue given a wake rings it once per read the ring finished, and only
/// once that read is ready: after `n` wakes, a wait that does not block hands
/// over at least `n` reads in all.
#[test]
fn a_read_queue_wakes_once_a_read_is_ready_to_hand_over() -> io::Result<()> {
    use core::sync::atomic::Ordering::SeqCst;

    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 32 * 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let wake = Arc::new(CountWake(core::sync::atomic::AtomicUsize::new(0)));
    let mut queue = fs.read_queue();
    assert!(queue.set_wake(Arc::clone(&wake) as Arc<dyn crate::fs::ReadWake>));
    for tag in 0..32usize {
        queue.submit(QueuedRead {
            tag,
            file: Arc::clone(&files[tag % 2]),
            offset: (tag * 64) as u64,
            buf: vec![0; 64],
        });
    }
    let mut handed = 0usize;
    while queue.outstanding() > 0 {
        let rung = wake.0.load(SeqCst);
        queue.wait(0, &mut |done| {
            assert!(done.result.is_ok(), "read {} failed", done.tag);
            handed += 1;
        });
        assert!(
            handed >= rung,
            "{rung} wakes rung but {handed} reads handed over"
        );
        std::thread::yield_now();
    }
    assert_eq!(handed, 32);
    assert_eq!(
        wake.0.load(SeqCst),
        32,
        "one wake per read the ring finished"
    );
    Ok(())
}

/// A read of a file the ring cannot take is read serially, which blocks, so
/// a wait for none leaves it held, and a wait for one reads it.
#[test]
fn a_read_without_a_descriptor_waits_for_a_wait_for_one() -> io::Result<()> {
    use crate::fs::MemFs;

    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let mem = MemFs::new();
    let path = Path::new("/m");
    mem.open(path, &FsOpenOptions::new().write(true).create(true))?
        .write_all(b"hello, world")?;
    let file: Arc<dyn FsFile> = Arc::from(mem.open(path, &FsOpenOptions::new().read(true))?);
    assert!(
        file.backing_fd().is_none(),
        "an in-memory file has no descriptor"
    );

    let mut queue = fs.read_queue();
    queue.submit(QueuedRead {
        tag: 1,
        file,
        offset: 7,
        buf: vec![0; 5],
    });
    let mut got = Vec::new();
    queue.wait(0, &mut |done| got.push(done.buf));
    assert!(got.is_empty(), "a wait for none reads nothing");
    assert_eq!((queue.outstanding(), queue.held()), (1, 1));
    queue.wait(1, &mut |done| got.push(done.buf));
    assert_eq!(got, [b"world".to_vec()]);
    assert_eq!((queue.outstanding(), queue.held()), (0, 0));
    Ok(())
}

/// A wait for none never blocks on the ring's submission channel: with the
/// ring thread held on a read that has no data yet and the channel full, it
/// keeps its reads and returns, and a later wait for one sends and reads them.
#[test]
fn a_wait_for_none_does_not_wait_for_room_on_the_ring() -> io::Result<()> {
    // Ring capacity, which bounds the submission channel.
    const ENTRIES: u32 = 2;
    let Some(_) = try_io_uring() else {
        return Ok(());
    };
    let fs = IoUringFs::with_ring_size(ENTRIES)?;
    let dir = tempfile::tempdir()?;
    let fifo = dir.path().join("fifo");
    let made = std::process::Command::new("mkfifo").arg(&fifo).status()?;
    assert!(made.success(), "mkfifo");
    // Read and write, so the open does not wait for a writer.
    let pipe: Arc<dyn FsFile> =
        Arc::from(fs.open(&fifo, &FsOpenOptions::new().read(true).write(true))?);
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let read_of = |file: &Arc<dyn FsFile>, len: usize| QueuedRead {
        tag: 0,
        file: Arc::clone(file),
        offset: 0,
        buf: vec![0; len],
    };

    // The ring thread takes this read and waits on it.
    let mut held = fs.read_queue();
    held.submit(read_of(&pipe, 1));
    held.wait(0, &mut |_| {});
    std::thread::sleep(std::time::Duration::from_millis(200));
    // Nothing drains the channel now: these fill it.
    let mut filling: Vec<_> = (0..ENTRIES)
        .map(|_| {
            let mut queue = fs.read_queue();
            queue.submit(read_of(&files[0], 64));
            queue.wait(0, &mut |_| {});
            queue
        })
        .collect();

    // The queue itself, which a thread can take, rather than the trait
    // object `read_queue` hands out.
    let mut late = UringReadQueue::new(&fs.inner);
    late.submit(read_of(&files[1], 64));
    std::thread::scope(|scope| {
        let (returned, polled) = std::sync::mpsc::channel();
        let poll = scope.spawn(move || {
            late.wait(0, &mut |_| {});
            returned.send(()).ok();
            late
        });
        let prompt = polled
            .recv_timeout(std::time::Duration::from_secs(5))
            .is_ok();
        // Release the ring thread either way, so the test ends.
        std::fs::OpenOptions::new()
            .write(true)
            .open(&fifo)
            .and_then(|mut writer| writer.write_all(b"x"))
            .expect("write to the fifo");
        let mut late = poll.join().expect("the poll thread");
        assert!(prompt, "a wait for none waited for room on the ring");

        assert_eq!(late.outstanding(), 1, "the read is kept, not lost");
        assert_eq!(
            late.held(),
            1,
            "a read no ring has is one only a wait carries out"
        );
        let mut got = Vec::new();
        while late.outstanding() > 0 {
            late.wait(1, &mut |done| got.push(done.result.is_ok()));
        }
        assert_eq!(got, [true], "a later wait sends and reads it");
    });
    for queue in &mut filling {
        while queue.outstanding() > 0 {
            queue.wait(1, &mut |done| assert!(done.result.is_ok()));
        }
    }
    while held.outstanding() > 0 {
        held.wait(1, &mut |done| assert!(done.result.is_ok()));
    }
    Ok(())
}

/// A wait for none does not wait for the submission lock either: while
/// another sender holds it (blocked on a full channel, say), the wait keeps
/// its reads, reports them held, and a later wait sends them.
#[test]
fn a_wait_for_none_does_not_wait_for_the_submission_lock() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = UringReadQueue::new(&fs.inner);
    queue.submit(QueuedRead {
        tag: 0,
        file: Arc::clone(&files[0]),
        offset: 0,
        buf: vec![0; 64],
    });

    let lock = fs
        .inner
        .tx
        .lock()
        .unwrap_or_else(std::sync::PoisonError::into_inner);
    let mut queue = std::thread::scope(|scope| {
        let (returned, polled) = std::sync::mpsc::channel();
        let poll = scope.spawn(move || {
            queue.wait(0, &mut |_| {});
            returned.send(()).ok();
            queue
        });
        let prompt = polled
            .recv_timeout(std::time::Duration::from_secs(5))
            .is_ok();
        drop(lock);
        let queue = poll.join().expect("the poll thread");
        assert!(prompt, "a wait for none waited for the submission lock");
        queue
    });
    assert_eq!((queue.outstanding(), queue.held()), (1, 1));
    let mut got = Vec::new();
    while queue.outstanding() > 0 {
        queue.wait(1, &mut |done| got.push(done.result.is_ok()));
    }
    assert_eq!(got, [true], "a later wait sends and reads it");
    Ok(())
}

/// A wake asked for once reads are on the ring is declined: those reads
/// report to a sink without it, and a caller sleeping on it would not wake.
#[test]
fn a_wake_asked_for_with_reads_in_flight_is_declined() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let wake = Arc::new(CountWake(core::sync::atomic::AtomicUsize::new(0)));
    let mut queue = UringReadQueue::new(&fs.inner);
    queue.submit(QueuedRead {
        tag: 0,
        file: Arc::clone(&files[0]),
        offset: 0,
        buf: vec![0; 64],
    });
    queue.issue(true);
    assert!(!queue.set_wake(Arc::clone(&wake) as Arc<dyn crate::fs::ReadWake>));
    while queue.outstanding() > 0 {
        queue.wait(1, &mut |done| assert!(done.result.is_ok()));
    }
    assert!(
        queue.set_wake(wake as Arc<dyn crate::fs::ReadWake>),
        "nothing in flight"
    );
    Ok(())
}

/// A read past the end of its file comes back failed as a short read, and
/// the reads beside it still come back read.
#[test]
fn a_read_queue_reports_a_short_read_as_failed() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 256)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = fs.read_queue();
    for (tag, offset) in [(0usize, 0u64), (1, 1 << 20), (2, 64)] {
        queue.submit(QueuedRead {
            tag,
            file: Arc::clone(&files[0]),
            offset,
            buf: vec![0; 64],
        });
    }
    let mut got = Vec::new();
    while queue.outstanding() > 0 {
        queue.wait(1, &mut |done| {
            got.push((done.tag, done.result.map_err(|e| e.kind())));
        });
    }
    got.sort_unstable_by_key(|(tag, _)| *tag);
    assert_eq!(
        got,
        [
            (0, Ok(())),
            (1, Err(crate::io::ErrorKind::UnexpectedEof)),
            (2, Ok(()))
        ]
    );
    Ok(())
}

/// A queue reused for many rounds keeps no slot for a read it handed back:
/// its bookkeeping follows the reads in flight, not every read it has sent.
#[test]
fn a_reused_read_queue_keeps_no_slot_for_a_read_handed_back() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 8 * 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = UringReadQueue::new(&fs.inner);
    for round in 0..100usize {
        for i in 0..8usize {
            queue.submit(QueuedRead {
                tag: round * 8 + i,
                file: Arc::clone(&files[i % 2]),
                offset: (i * 64) as u64,
                buf: vec![0; 64],
            });
        }
        while queue.outstanding() > 0 {
            queue.wait(1, &mut |done| {
                assert!(done.result.is_ok(), "read {} failed", done.tag);
            });
        }
        assert!(
            queue.sent.is_empty(),
            "round {round} kept {} slots",
            queue.sent.len()
        );
    }
    Ok(())
}

/// The results of draining `queue` to empty, by tag, with each error's kind.
fn drain_results(queue: &mut dyn ReadQueue) -> Vec<(usize, Result<(), crate::io::ErrorKind>)> {
    let mut got = Vec::new();
    while queue.outstanding() > 0 {
        queue.wait(1, &mut |done| {
            got.push((done.tag, done.result.map_err(|e| e.kind())));
        });
    }
    got.sort_unstable_by_key(|(tag, _)| *tag);
    got
}

/// A read the ring need not take is handed back without it: an empty one at
/// once, and one too long for a submission queue entry as failed, beside a
/// read the ring serves.
#[test]
fn a_read_queue_hands_back_reads_the_ring_cannot_take() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 256)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = fs.read_queue();
    queue.submit(QueuedRead {
        tag: 0,
        file: Arc::clone(&files[0]),
        offset: 0,
        buf: Vec::new(),
    });
    // Zeroed and never touched, so it costs address space, not memory.
    let too_long = usize::try_from(i32::MAX).expect("fits") + 1;
    queue.submit(QueuedRead {
        tag: 1,
        file: Arc::clone(&files[0]),
        offset: 0,
        buf: vec![0; too_long],
    });
    queue.submit(QueuedRead {
        tag: 2,
        file: Arc::clone(&files[0]),
        offset: 64,
        buf: vec![0; 64],
    });
    assert_eq!(
        drain_results(&mut *queue),
        [
            (0, Ok(())),
            (1, Err(crate::io::ErrorKind::InvalidInput)),
            (2, Ok(())),
        ]
    );
    Ok(())
}

/// A file with no descriptor is read serially, and what that read returns is
/// handed back as is: short of its block as failed, a refused read with its
/// error.
#[test]
fn a_serial_read_reports_a_short_read_and_a_refused_one() -> io::Result<()> {
    use crate::fs::{Fault, FaultFs, FaultOp, FaultRule, MemFs};

    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let mem = MemFs::new();
    let path = Path::new("/m");
    mem.open(path, &FsOpenOptions::new().write(true).create(true))?
        .write_all(b"hello, world")?;
    let short: Arc<dyn FsFile> = Arc::from(mem.open(path, &FsOpenOptions::new().read(true))?);
    let faulty = FaultFs::new(mem.clone());
    faulty.injector().arm(FaultRule::new(
        FaultOp::ReadAt,
        Fault::Error(crate::io::ErrorKind::PermissionDenied),
    ));
    let refused: Arc<dyn FsFile> = Arc::from(faulty.open(path, &FsOpenOptions::new().read(true))?);

    let mut queue = fs.read_queue();
    queue.submit(QueuedRead {
        tag: 0,
        file: short,
        offset: 7,
        buf: vec![0; 64],
    });
    queue.submit(QueuedRead {
        tag: 1,
        file: refused,
        offset: 0,
        buf: vec![0; 5],
    });
    assert_eq!(
        drain_results(&mut *queue),
        [
            (0, Err(crate::io::ErrorKind::UnexpectedEof)),
            (1, Err(crate::io::ErrorKind::PermissionDenied)),
        ]
    );
    Ok(())
}

/// Reads sent to a ring thread that has shut down fail, whether the wait
/// would block for room or not, and none is left outstanding.
#[test]
fn a_read_queue_fails_its_reads_once_the_ring_thread_is_gone() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 256)?
        .into_iter()
        .map(Arc::from)
        .collect();
    // What a shutdown leaves: no sender to the ring thread.
    drop(
        fs.inner
            .tx
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
            .take(),
    );
    for min in [0usize, 1] {
        let mut queue = fs.read_queue();
        queue.submit(QueuedRead {
            tag: 0,
            file: Arc::clone(&files[0]),
            offset: 0,
            buf: vec![0; 64],
        });
        let mut got = Vec::new();
        queue.wait(min, &mut |done| {
            got.push((done.tag, done.result.map_err(|e| e.kind())));
        });
        assert_eq!(
            got,
            [(0, Err(crate::io::ErrorKind::BrokenPipe))],
            "a wait for {min}"
        );
        assert_eq!(queue.outstanding(), 0, "a wait for {min}");
    }
    Ok(())
}

/// A submission lock poisoned by a panicking sender still sends: the
/// channel it guards is intact, so a wait for none puts the reads on the ring.
#[test]
fn a_poisoned_submission_lock_still_sends() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 256)?
        .into_iter()
        .map(Arc::from)
        .collect();
    std::thread::scope(|scope| {
        let poisoner = scope.spawn(|| {
            let _held = fs
                .inner
                .tx
                .lock()
                .unwrap_or_else(std::sync::PoisonError::into_inner);
            panic!("a sender panics holding the submission lock");
        });
        assert!(poisoner.join().is_err(), "the sender panicked");
    });
    assert!(fs.inner.tx.is_poisoned(), "the lock is poisoned");

    let mut queue = fs.read_queue();
    queue.submit(QueuedRead {
        tag: 0,
        file: Arc::clone(&files[0]),
        offset: 64,
        buf: vec![0; 64],
    });
    let mut got = Vec::new();
    while queue.outstanding() > 0 {
        queue.wait(0, &mut |done| got.push((done.tag, done.result.is_ok())));
        std::thread::yield_now();
    }
    assert_eq!(got, [(0, true)]);
    Ok(())
}

/// The ring's result for a read is handed back as the read's verdict: a
/// negative result is the read's `errno`. Completions may arrive in any order:
/// the slots of handed-back reads are dropped from either end, and a
/// completion for a read already handed back hands back nothing.
#[test]
fn a_read_queue_hands_back_completions_in_any_order() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 256)?
        .into_iter()
        .map(Arc::from)
        .collect();
    let mut queue = UringReadQueue::new(&fs.inner);
    // Two reads as if on the ring, at positions 0 and 1.
    for tag in 0..2usize {
        queue.sent.push_back(Some(QueuedRead {
            tag,
            file: Arc::clone(&files[0]),
            offset: 0,
            buf: vec![0; 64],
        }));
    }
    queue.on_ring = 2;

    /// `EACCES` on Linux.
    const EACCES: i32 = 13;
    // Every completion is taken before any assertion, so a failing one leaves
    // nothing for the queue's drop to wait for.
    let last = queue.complete(1, -EACCES);
    let slots_after_last = queue.sent.len();
    let again = queue.complete(1, 64);
    let first = queue.complete(0, 64);

    let last = last.expect("the read at 1");
    assert_eq!(
        last.result.map_err(|e| e.kind()),
        Err(crate::io::ErrorKind::PermissionDenied),
        "a negative result is the read's errno"
    );
    assert_eq!(
        slots_after_last, 1,
        "the handed-back slot at the end is dropped"
    );
    assert!(
        again.is_none(),
        "a completion for a read handed back hands back nothing"
    );
    assert!(first.expect("the read at 0").result.is_ok());
    assert!(queue.sent.is_empty(), "no slot outlives its read");
    assert_eq!(queue.on_ring, 0);
    Ok(())
}

/// Dropping a queue with reads in flight waits their completions out
/// before their buffers go, and the backend stays usable after.
#[test]
fn dropping_a_read_queue_with_reads_in_flight_leaves_the_backend_usable() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let files: Vec<Arc<dyn FsFile>> = two_files(&fs, dir.path(), 128 * 64)?
        .into_iter()
        .map(Arc::from)
        .collect();
    for _ in 0..50 {
        let mut queue = fs.read_queue();
        for tag in 0..128usize {
            queue.submit(QueuedRead {
                tag,
                file: Arc::clone(&files[tag % 2]),
                offset: (tag * 64) as u64,
                buf: vec![0; 64],
            });
        }
        // Puts every read on the ring without waiting for any.
        queue.wait(0, &mut |_| {});
        drop(queue);
    }
    let mut buf = [0u8; 64];
    let n = files[1].read_at(&mut buf, 64)?;
    assert_eq!(n, 64);
    assert_eq!(buf.to_vec(), expected_bytes(1, 64, 64));
    Ok(())
}

/// Writes `len` bytes of `byte` to a new file at `path` through `fs`.
fn write_file(fs: &IoUringFs, path: &Path, byte: u8, len: usize) -> io::Result<()> {
    let mut file = fs.open(path, &FsOpenOptions::new().write(true).create(true))?;
    file.write_all(&vec![byte; len])?;
    file.sync_all()?;
    Ok(())
}

/// A directory reports the same capabilities through `IoUringFs` as through
/// `StdFs`: both reach the same kernel files, and every path that adapts to
/// the file system (tight-space reclaim, prefix punches, reflinked
/// checkpoints) asks the capabilities first.
#[test]
fn capabilities_match_the_std_backend() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    assert_eq!(
        fs.capabilities(dir.path()),
        crate::fs::StdFs.capabilities(dir.path())
    );
    Ok(())
}

/// A hole punched through `IoUringFs` frees the range: it reads back as
/// zeros, the file keeps its length, and the range holds a hole.
#[test]
fn a_hole_punched_through_io_uring_lands() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    if !crate::fs::StdFs.capabilities(dir.path()).punch_hole {
        eprintln!("skipping: the temporary directory's file system cannot punch holes");
        return Ok(());
    }
    let path = dir.path().join("punched");
    write_file(&fs, &path, 0xAB, 1 << 20)?;
    fs.punch_hole(&path, 0, 512 * 1024)?;

    assert_eq!(fs.extent_contains_hole(&path, 0, 512 * 1024)?, Some(true));
    assert_eq!(fs.metadata(&path)?.len, 1 << 20, "the length is kept");
    let file = fs.open(&path, &FsOpenOptions::new().read(true))?;
    let mut head = [0xFFu8; 64];
    assert_eq!(file.read_at(&mut head, 0)?, 64);
    assert_eq!(head, [0u8; 64], "the punched range reads as zeros");
    let mut tail = [0u8; 64];
    assert_eq!(file.read_at(&mut tail, 512 * 1024)?, 64);
    assert_eq!(tail, [0xABu8; 64], "the rest is untouched");
    Ok(())
}

/// The link count of a path reads through `IoUringFs`, so a reclaim can tell
/// a file a checkpoint shares from one it may truncate.
#[test]
fn the_link_count_of_a_path_reads_through_io_uring() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let (path, link) = (dir.path().join("table"), dir.path().join("link"));
    write_file(&fs, &path, 1, 4_096)?;
    assert_eq!(Fs::hard_link_count(&fs, &path)?, 1);
    fs.hard_link(&path, &link)?;
    assert_eq!(Fs::hard_link_count(&fs, &path)?, 2);
    Ok(())
}

/// A file of `blocks` 64-byte blocks, byte `i` holding `i % 251`, opened for
/// reading through `fault` over the ring.
fn faulted_file(
    fs: &IoUringFs,
    fault: &crate::fs::FaultFs<IoUringFs>,
    path: &Path,
    blocks: usize,
) -> io::Result<Arc<dyn FsFile>> {
    let bytes: Vec<u8> = (0..blocks * 64)
        .map(|i| u8::try_from(i % 251).expect("below 251"))
        .collect();
    let mut file = fs.open(path, &FsOpenOptions::new().write(true).create(true))?;
    file.write_all(&bytes)?;
    file.sync_all()?;
    Ok(Arc::from(
        fault.open(path, &FsOpenOptions::new().read(true))?,
    ))
}

/// The bytes of block `block` of a [`faulted_file`].
fn faulted_block(block: usize) -> Vec<u8> {
    (block * 64..(block + 1) * 64)
        .map(|i| u8::try_from(i % 251).expect("below 251"))
        .collect()
}

/// Reads through a `FaultFs` over the ring go to the ring's queue together,
/// not one blocking read each, and a read rule still fires on the read it
/// names, the others unharmed.
#[test]
fn a_fault_rule_fires_on_reads_through_the_wrapped_ring_queue() -> io::Result<()> {
    use crate::fs::{Fault, FaultFs, FaultOp, FaultRule};
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let fault = FaultFs::new(fs.clone());
    let dir = tempfile::tempdir()?;
    let file = faulted_file(&fs, &fault, &dir.path().join("queued"), 8)?;
    fault.injector().arm(
        FaultRule::new(FaultOp::ReadAt, Fault::Error(crate::io::ErrorKind::Other))
            .on_path("queued")
            .at_offset(3 * 64)
            .once(),
    );

    let channels = fs
        .inner
        .counts
        .channels
        .load(core::sync::atomic::Ordering::Relaxed);
    let mut done: Vec<(usize, Vec<u8>, Option<crate::io::ErrorKind>)> = Vec::new();
    {
        let mut queue = fault.read_queue();
        for tag in 0..8 {
            queue.submit(QueuedRead {
                tag,
                file: Arc::clone(&file),
                offset: (tag * 64) as u64,
                buf: vec![0; 64],
            });
        }
        queue.wait(8, &mut |read| {
            done.push((read.tag, read.buf, read.result.err().map(|e| e.kind())));
        });
    }
    assert_eq!(
        fs.inner
            .counts
            .channels
            .load(core::sync::atomic::Ordering::Relaxed)
            - channels,
        0,
        "no read took a blocking channel of its own"
    );
    done.sort_by_key(|(tag, ..)| *tag);
    assert_eq!(done.len(), 8);
    for (tag, buf, error) in done {
        if tag == 3 {
            assert_eq!(error, Some(crate::io::ErrorKind::Other), "the rule fired");
        } else {
            assert_eq!(error, None, "read {tag}");
            assert_eq!(buf, faulted_block(tag), "read {tag}");
        }
    }
    assert_eq!(fault.injector().read_count(), 8, "every read was seen");
    Ok(())
}

/// A batched read through a `FaultFs` over the ring is one batch of the
/// ring, and a read rule on one of its blocks fails the batch with that
/// block's error after the blocks before it were read.
#[test]
fn a_fault_rule_fires_on_a_batched_read_through_the_wrapped_ring() -> io::Result<()> {
    use crate::fs::{Fault, FaultFs, FaultOp, FaultRule};
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let fault = FaultFs::new(fs.clone());
    let dir = tempfile::tempdir()?;
    let file = faulted_file(&fs, &fault, &dir.path().join("batched"), 8)?;
    /// A read of each 64-byte block of `file` into its buffer.
    fn block_reads<'a>(
        file: &'a dyn FsFile,
        buffers: &'a mut [[u8; 64]],
    ) -> Vec<crate::fs::BlockRead<'a>> {
        buffers
            .iter_mut()
            .enumerate()
            .map(|(i, buf)| crate::fs::BlockRead {
                file,
                offset: (i * 64) as u64,
                buf: crate::fs::BlockBuf::new(buf),
            })
            .collect()
    }
    let mut buffers = vec![[0u8; 64]; 8];

    let channels = fs
        .inner
        .counts
        .channels
        .load(core::sync::atomic::Ordering::Relaxed);
    fault.read_blocks_batched(&mut block_reads(file.as_ref(), &mut buffers))?;
    assert_eq!(
        fs.inner
            .counts
            .channels
            .load(core::sync::atomic::Ordering::Relaxed)
            - channels,
        1,
        "one batch of the ring"
    );
    for (i, buf) in buffers.iter().enumerate() {
        assert_eq!(buf.to_vec(), faulted_block(i), "block {i}");
    }

    fault.injector().arm(
        FaultRule::new(FaultOp::ReadAt, Fault::Error(crate::io::ErrorKind::Other))
            .on_path("batched")
            .at_offset(5 * 64)
            .once(),
    );
    let mut fresh = vec![[0u8; 64]; 8];
    let mut batch = block_reads(file.as_ref(), &mut fresh);
    let error = fault
        .read_blocks_batched(&mut batch)
        .expect_err("the rule fails the batch");
    assert_eq!(error.kind(), crate::io::ErrorKind::Other);
    assert!(
        batch.iter().take(5).all(|req| req.buf.is_full()),
        "the blocks before the failing one were read"
    );
    Ok(())
}

/// A file cloned through `IoUringFs` holds the source's bytes in an inode of
/// its own, not a second name of the source.
#[test]
fn a_file_cloned_through_io_uring_is_independent() -> io::Result<()> {
    let Some(fs) = try_io_uring() else {
        return Ok(());
    };
    let dir = tempfile::tempdir()?;
    let (source, clone) = (dir.path().join("source"), dir.path().join("clone"));
    write_file(&fs, &source, 7, 64 * 1024)?;
    fs.reflink_file(&source, &clone)?;

    let clone_file = fs.open(&clone, &FsOpenOptions::new().read(true).write(true))?;
    let mut bytes = vec![0u8; 64 * 1024];
    assert_eq!(clone_file.read_at(&mut bytes, 0)?, bytes.len());
    assert!(bytes.iter().all(|&b| b == 7), "the clone holds the source");
    assert_eq!(Fs::hard_link_count(&fs, &clone)?, 1, "an inode of its own");
    Ok(())
}