summaryrefslogtreecommitdiff
path: root/9ns/src/nine.zig
blob: 6b48e7a605cf648829e46040e5b100a96fd94cc2 (plain) (blame)
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
//! 9P2000 session over a blocking file descriptor, shared by several threads.
//!
//! A thin RPC layer over `cloud9.Client` (push/take, allocation-free). Many
//! requests may be outstanding at once, each under its own tag, and their
//! replies may come back in any order: a read the server parks (a blocking
//! `log` or `event` file) holds up only the thread that asked for it.
//!
//! The multiplexing follows Plan 9's mount driver (`mountio`, `mountmux` and
//! `mntgate` in kernel/devices/sys/devmnt.c). Each thread that makes calls
//! owns a `Caller`. A call writes its T-message and then waits; the first
//! waiter to find nobody reading the socket takes the reading baton and
//! reads for everyone, handing each R-message to the waiter its tag names;
//! when its own reply has come in it passes the baton on. There is no reader
//! thread and no lock held across a blocking read.
//!
//! Fids are handed out from a free list; fid 0 is reserved for the root.
const std = @import("std");
const cloud9 = @import("cloud9");
const linux = std.os.linux;

pub const Address = union(enum) {
    unix: []const u8,
    tcp: struct { host: []const u8, port: u16 },
    fd: i32,
};

/// A Stat whose every field means "leave unchanged" in a Twstat.
pub const dontcare = cloud9.Stat{
    .type = 0xFFFF,
    .dev = 0xFFFF_FFFF,
    .qid = .{ .type = 0xFF, .version = 0xFFFF_FFFF, .path = 0xFFFF_FFFF_FFFF_FFFF },
    .mode = 0xFFFF_FFFF,
    .atime = 0xFFFF_FFFF,
    .mtime = 0xFFFF_FFFF,
    .length = 0xFFFF_FFFF_FFFF_FFFF,
    .name = "",
    .uid = "",
    .gid = "",
    .muid = "",
};

/// Requests one session keeps in flight at once (one more tag is kept for a
/// Tflush). Every parked read a mount serves holds one tag and pins one
/// bridge worker (`bridge.max_workers` is one fewer), so the window must be
/// wider than any server parks per connection: pardes parks 128
/// (`fs.Options.slot_capacity` in its editor options) and refuses the next
/// with `too many reads waiting: 128`, which frees the worker. With 256 a
/// mount can hold every read such a server parks and still have workers
/// for the rest of its traffic (a `cat layout` beside 128 held event reads).
pub const max_tags: usize = 256;
pub const Client = cloud9.ClientWith(max_tags);

/// The `Io` the locks here (and the bridge's) hand to `std.Io.Mutex` and
/// `std.Io.Condition`. They only use it for futex waits and wakes, and the
/// uncancelable paths used throughout ignore the instance and go straight to
/// the futex syscall, which is thread-safe; no `Io` is threaded through the
/// FUSE loop, so this one stands in.
pub fn lockIo() std.Io {
    return std.Io.Threaded.global_single_threaded.io();
}

/// How the owner of a caller (the FUSE bridge) gets a say while its rpc waits
/// for a reply. `watch` names a descriptor to poll alongside the socket, or
/// -1 to poll nothing extra right now; when it becomes readable `onReadable`
/// consumes whatever is there and returns true if the request in flight should
/// be cancelled with a Tflush. `armed` reports whether such a cancellation was
/// requested earlier for the operation in progress; the chunked read/write
/// loops stop between chunks when it is set (a reply that raced the flush still
/// leaves the caller wanting out).
pub const Interrupt = struct {
    ctx: *anyopaque,
    watch: *const fn (ctx: *anyopaque) i32,
    onReadable: *const fn (ctx: *anyopaque) Session.Error!bool,
    armed: *const fn (ctx: *anyopaque) bool,
};

/// One connection: the socket, the protocol state, the fids, and the table
/// of calls waiting for replies. Calls are made through a `Caller`.
pub const Session = struct {
    pub const Error = error{ Nine, Protocol, Io, Closed, Stopped, Interrupted, TooLarge, OutOfMemory };

    pub const Walk = struct { nwqid: u16, wqid: [cloud9.max_welem]cloud9.Qid };
    pub const Open = struct { qid: cloud9.Qid, iounit: u32 };

    gpa: std.mem.Allocator,
    fd: i32,
    client: Client,
    in_buf: []u8,
    out_buf: []u8,
    /// Negotiated maximum message size.
    msize: u32,
    /// Requests sent at once, at most `max_tags`: a call past it waits for a
    /// tag as one past the client's window does. Narrowed by tests whose
    /// fake server holds fewer.
    window: usize = max_tags,
    next_fid: u32 = 1,
    free_fids: std.ArrayList(u32) = .empty,
    /// Per-fid iounit learned from open/create (0 = none); used to chunk read/write.
    iounits: std.AutoHashMapUnmanaged(u32, u32) = .empty,
    /// Optional descriptor every waiting call watches: when it becomes
    /// readable (the bridge's "child exited" pipe) the call fails with
    /// `error.Stopped` instead of blocking on a server that never answers.
    stop_fd: i32 = -1,
    /// Milliseconds a Tflush may go unanswered before the server is declared
    /// wedged: the call fails with `error.Interrupted` and the session with it.
    /// The protocol says a client waits for the Rflush; a server that has not
    /// managed one in this long is not going to, and the process behind the
    /// interrupt is unkillable until we stop waiting. 0 waits forever.
    flush_grace_ms: i32 = 3000,
    /// The server is gone as far as this session is concerned (see
    /// `Caller.abort_on_cancel`, `flush_grace_ms`); every call answers
    /// `error.Closed`, the ones already waiting included.
    wedged: bool = false,
    /// The connection itself failed (EOF, a socket error, a reply that
    /// breaks the protocol): every call answers this.
    dead: ?Error = null,
    /// A send failed: the server hung up, perhaps just after answering. No
    /// more is sent, but the replies already in the socket are still read,
    /// to EOF or until `drain_until`, before the session dies of this.
    write_dead: ?Error = null,
    drain_until: i64 = 0,

    // -- multiplexing (devmnt's Mnt: `queue`, `rip`) ------------------------------------

    /// Guards the client's tags and input side, the tables below, the fid
    /// tables, `wedged` and `dead`. Never held across a blocking syscall.
    mutex: std.Io.Mutex = .init,
    /// Serializes transmission: the client's output buffer and the write side
    /// of the socket. Taken before `mutex`, and never by the baton holder
    /// (see `Waiter.cancel`), so a write that blocks because the server is
    /// itself blocked writing replies cannot stop those replies being read.
    wlock: std.Io.Mutex = .init,
    /// The call waiting on each tag. A call that sent a Tflush sits under
    /// both tags until one of them answers.
    waiting: [max_tags + 1]?*Waiter = @splat(null),
    /// The Tversion's waiter (it travels under NOTAG).
    versioning: ?*Waiter = null,
    /// Calls that found every tag taken, waiting for a reply to free one.
    starved: ?*Waiter = null,
    /// The waiter holding the reading baton, if any (devmnt's `m->rip`).
    reader: ?*Waiter = null,

    /// Connect to `address`, then negotiate the protocol version.
    /// `msize` is the maximum message size to ask for (0 = the buffers' size).
    pub fn connect(gpa: std.mem.Allocator, address: Address, msize: u32) !Session {
        return connectWatched(gpa, address, msize, -1);
    }

    /// `connect`, with `stop_fd` watched for the whole handshake (the version
    /// rpc included). A server that accepts the connection and then never
    /// answers the Tversion would otherwise pin the caller in a blocking read
    /// with no way out; -1 disables the watch. The field stays set on the
    /// returned session. An `.fd` address is the caller's to close on failure.
    pub fn connectWatched(gpa: std.mem.Allocator, address: Address, msize: u32, stop_fd: i32) !Session {
        var s = try dial(gpa, address, msize, stop_fd);
        negotiate(&s) catch |e| {
            s.freeBuffers();
            if (address != .fd) _ = linux.close(s.fd);
            return e;
        };
        return s;
    }

    fn negotiate(s: *Session) !void {
        var c = try Caller.init(s);
        defer c.deinit();
        try c.version();
    }

    /// The transport and the buffers, no handshake: for a caller that wants
    /// its interrupt source in place before `version()` (9ns's mntgen
    /// worker, so a walk interrupted mid-dial can abandon the dial). Owns
    /// the descriptor from here: `deinit` closes it.
    pub fn dial(gpa: std.mem.Allocator, address: Address, msize: u32, stop_fd: i32) !Session {
        const want: u32 = if (msize == 0) 8192 else @max(msize, 24);
        const fd = try openTransport(address);
        errdefer if (address != .fd) {
            _ = linux.close(fd);
        };
        const in_buf = try gpa.alloc(u8, want);
        errdefer gpa.free(in_buf);
        const out_buf = try gpa.alloc(u8, want);
        errdefer gpa.free(out_buf);
        return .{
            .gpa = gpa,
            .fd = fd,
            .client = .init(.{ .in = in_buf, .out = out_buf }),
            .in_buf = in_buf,
            .out_buf = out_buf,
            .msize = want,
            .stop_fd = stop_fd,
        };
    }

    fn freeBuffers(s: *Session) void {
        s.free_fids.deinit(s.gpa);
        s.iounits.deinit(s.gpa);
        s.gpa.free(s.in_buf);
        s.gpa.free(s.out_buf);
    }

    /// Closes the descriptor and frees the buffers. Fids are not clunked.
    /// Every `Caller` must be done with the session (and is best deinit first).
    pub fn deinit(s: *Session) void {
        _ = linux.close(s.fd);
        s.freeBuffers();
        s.* = undefined;
    }

    /// Fid 0 is never handed out: it belongs to the root attach.
    pub fn allocFid(s: *Session) u32 {
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        if (s.free_fids.pop()) |fid| return fid;
        const fid = s.next_fid;
        s.next_fid += 1;
        return fid;
    }

    /// Fids currently bound (excluding fid 0); a debugging aid for leak hunting.
    pub fn fidsInUse(s: *Session) usize {
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        return (s.next_fid - 1) - s.free_fids.items.len;
    }

    pub fn freeFid(s: *Session, fid: u32) void {
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        _ = s.iounits.remove(fid);
        // If the free list cannot grow the fid is simply leaked; the counter keeps going.
        s.free_fids.append(s.gpa, fid) catch {};
    }

    /// Whether the session was declared gone (`wedged`) or broke (`dead`):
    /// nothing more will come of it.
    pub fn over(s: *Session) bool {
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        return s.wedged or s.dead != null;
    }

    // -- internals: every function below runs with `mutex` held -------------------------

    /// Wakes a caller from its poll: its reply is in, the baton is free, a
    /// tag was freed, or the session is over. Spurious pokes are harmless
    /// (the waiter rechecks under the mutex), so the counter is never trusted.
    fn poke(c: *Caller) void {
        const one: u64 = 1;
        _ = linux.write(c.wake, std.mem.asBytes(&one), 8);
    }

    /// Wakes every waiting call: the baton is free (devmnt's `mntgate`, which
    /// wakes one; here all of them recheck, so a waiter that turns out to be
    /// done or still writing its request cannot swallow the handoff), or the
    /// session is over.
    /// `me` is the caller doing it, which needs no poke (and a stale one
    /// would only cost it a spurious wake-up later).
    fn pokeAll(s: *Session, me: *Caller) void {
        for (s.waiting) |slot| if (slot) |w| if (w.caller != me) poke(w.caller);
        if (s.versioning) |w| if (w.caller != me) poke(w.caller);
        var it = s.starved;
        while (it) |w| : (it = w.next_starved) if (w.caller != me) poke(w.caller);
    }

    fn die(s: *Session, e: Error, me: *Caller) void {
        if (s.dead == null) s.dead = e;
        s.pokeAll(me);
    }

    fn wedge(s: *Session, me: *Caller) void {
        s.wedged = true;
        s.pokeAll(me);
    }

    /// Takes every complete R-message out of the client and hands each to the
    /// call its tag names (devmnt's `mountmux`). The reply is copied into the
    /// caller's own buffer: the client's input buffer belongs to whoever reads
    /// next. A reply nobody waits for any more (its call gave up, or a Rflush
    /// whose request was answered first) is dropped.
    fn deliver(s: *Session, me: *Caller) void {
        var freed = false;
        while (s.client.take()) |d| {
            freed = true;
            const frame = s.client.in[0..s.client.frame];
            if (d.op == .version) {
                const w = s.versioning orelse continue;
                s.versioning = null;
                w.result = copyResult(d.result, frame, w.caller.reply);
                w.done = true;
                if (w.caller != me) poke(w.caller);
                continue;
            }
            const w = s.waiting[d.tag] orelse continue;
            s.waiting[d.tag] = null;
            if (d.op == .flush) {
                // The Rflush came first: the server dropped the request, and
                // the client has released its tag with this reply.
                w.flush_tag = null;
                if (s.waiting[w.tag] == w) s.waiting[w.tag] = null;
                if (!w.done) {
                    w.done = true;
                    w.flushed = true;
                }
            } else {
                w.result = copyResult(d.result, frame, w.caller.reply);
                w.done = true;
            }
            if (w.caller != me) poke(w.caller);
        }
        if (s.client.dead) s.die(error.Protocol, me);
        if (freed) {
            // A reply freed a tag: every starved call tries again.
            while (s.starved) |w| {
                s.starved = w.next_starved;
                w.next_starved = null;
                w.on_starved = false;
                w.tag_freed = true;
                if (w.caller != me) poke(w.caller);
            }
        }
    }

    /// Takes a waiter out of every table, and passes the baton on if it held it.
    fn leave(s: *Session, w: *Waiter) void {
        for (&s.waiting) |*slot| {
            if (slot.* == w) slot.* = null;
        }
        if (s.versioning == w) s.versioning = null;
        if (w.on_starved) {
            var link = &s.starved;
            while (link.*) |it| : (link = &it.next_starved) {
                if (it == w) {
                    link.* = w.next_starved;
                    break;
                }
            }
            w.on_starved = false;
            w.next_starved = null;
        }
        if (s.reader == w) {
            s.reader = null;
            s.pokeAll(w.caller);
        }
    }

    fn starve(s: *Session, w: *Waiter) void {
        if (w.on_starved) return;
        w.tag_freed = false;
        w.on_starved = true;
        w.next_starved = s.starved;
        s.starved = w;
    }

    /// Claims a tag for `req`, registers `w` under it and writes the message.
    /// When every tag is taken the call waits (reading, if it gets the baton)
    /// until a reply frees one; a cancellation meanwhile ends it with
    /// `error.Interrupted`, nothing having been sent.
    fn transmit(s: *Session, w: *Waiter, req: cloud9.Client.Request) Error!void {
        const io = lockIo();
        const c = w.caller;
        while (true) {
            if (s.wedged) return error.Closed;
            if (s.dead) |e| return e;
            if (s.write_dead) |e| return e;
            // Lock order: wlock, then mutex.
            s.mutex.unlock(io);
            s.wlock.lockUncancelable(io);
            s.mutex.lockUncancelable(io);
            const full = s.client.pending() >= s.window;
            const tag = (if (full) error.NoTags else s.client.submit(req)) catch |e| {
                s.wlock.unlock(io);
                switch (e) {
                    error.NoTags => {
                        s.starve(w);
                        try s.await(w, .tag);
                        continue;
                    },
                    error.Handshake, error.Dead => return error.Protocol,
                    error.NoSpace, error.TooLarge => return error.TooLarge,
                    error.BadRequest => {
                        c.setEname("bad request");
                        return error.Nine;
                    },
                }
            };
            w.tag = tag;
            if (req == .version) s.versioning = w else s.waiting[tag] = w;
            try s.send();
            return;
        }
    }

    /// Writes out what `submit` left in the client's output buffer. Entered
    /// with `mutex` and `wlock` held; returns with `mutex` held and `wlock`
    /// released. The socket write happens with `mutex` released, so the
    /// reader keeps reading while it blocks.
    fn send(s: *Session) Error!void {
        const io = lockIo();
        s.mutex.unlock(io);
        const sent = writeOut(s);
        s.wlock.unlock(io);
        s.mutex.lockUncancelable(io);
        sent catch |e| {
            // This call's request did not go out, so it fails; the calls
            // already sent may have their replies waiting to be read (a
            // server that answers and then hangs up), so the session is not
            // dead until the reader reaches the end of what came in.
            if (s.write_dead == null) {
                s.write_dead = e;
                s.drain_until = nowMs() + drain_ms;
            }
            return e;
        };
    }

    const Until = enum { reply, tag };

    // ponytail: a fixed drain window; a server that hangs up closes the
    // socket, so EOF normally ends the drain long before this.
    const drain_ms = 1000;

    /// Waits until `w` is answered (`.reply`: its reply or its Rflush is in)
    /// or a tag was freed (`.tag`). Takes the reading baton whenever nobody
    /// holds it (devmnt's `mountio` gate), reads for everyone while holding
    /// it, and lets go of it on the way out.
    ///
    /// While waiting it polls, besides its own wake-up descriptor, `stop_fd`
    /// (→ `error.Stopped`) and the caller's interrupt source. When the latter
    /// asks for a cancellation a Tflush for the request's tag goes out and the
    /// wait continues until either the original reply arrives (the flush lost
    /// the race: it is honoured, and its result returned, as flush(5) says;
    /// the later Rflush is dropped) or the Rflush does (the call ends in
    /// `error.Interrupted`), or neither within `flush_grace_ms` (→
    /// `error.Interrupted`, and the session is wedged: the server stopped
    /// talking). Under `abort_on_cancel` the cancellation itself wedges the
    /// session, with nothing sent.
    fn await(s: *Session, w: *Waiter, until: Until) Error!void {
        const io = lockIo();
        const c = w.caller;
        defer if (s.reader == w) {
            s.reader = null;
            s.pokeAll(c);
        };
        var cancel = false;
        var flush_deadline: ?i64 = null;
        var tmp: [64 * 1024]u8 = undefined;
        while (true) {
            switch (until) {
                .reply => if (w.done) return,
                .tag => if (w.tag_freed) return,
            }
            if (s.wedged) return error.Closed;
            if (s.dead) |e| return e;
            // (On the starved list the Tflush waits for a reply to free a tag.)
            if (cancel and w.flush_tag == null and !w.on_starved and until == .reply) {
                // devmnt's `mntflushalloc`: the flush is an rpc of its own.
                // The baton goes first, so a write that blocks cannot stop
                // everyone's replies (the Rflush among them) being read.
                if (s.reader == w) {
                    s.reader = null;
                    s.pokeAll(c);
                }
                try s.sendFlush(w);
                continue;
            }
            if (s.reader == null) s.reader = w;
            const reading = s.reader == w;
            // Only the baton holder touches the input side, so `room` holds
            // across the unlocked read. After take() returned null the frame
            // in progress still fits (a frame is at most msize bytes).
            const room = s.client.in.len - s.client.in_len;
            if (reading and room == 0) {
                s.die(error.Protocol, c);
                continue;
            }
            var grace: i32 = if (flush_deadline) |d| @intCast(@max(d - nowMs(), 0)) else -1;
            if (s.write_dead != null) {
                const left: i32 = @intCast(@max(s.drain_until - nowMs(), 0));
                grace = if (grace < 0) left else @min(grace, left);
            }
            s.mutex.unlock(io);
            var got: usize = 0;
            const ready = c.wait(if (reading) s.fd else -1, grace);
            const read_err: ?Error = if (ready) |r| (if (r == .socket) blk: {
                got = readSocket(s.fd, tmp[0..@min(room, tmp.len)]) catch |e| break :blk e;
                break :blk null;
            } else null) else |_| null;
            s.mutex.lockUncancelable(io);
            switch (try ready) {
                .socket => {
                    if (read_err) |e| {
                        s.die(e, c);
                        continue;
                    }
                    if (got == 0) {
                        s.die(error.Closed, c);
                        continue;
                    }
                    if (s.client.push(tmp[0..got]) != got) {
                        s.die(error.Protocol, c);
                        continue;
                    }
                    s.deliver(c);
                },
                .wake => {},
                .cancel => {
                    if (c.abort_on_cancel) {
                        s.wedge(c);
                        return error.Interrupted;
                    }
                    // Nothing on the wire yet: nothing to flush.
                    if (until == .tag) return error.Interrupted;
                    if (!cancel) {
                        cancel = true;
                        if (s.flush_grace_ms != 0) flush_deadline = nowMs() + s.flush_grace_ms;
                    }
                },
                .timeout => {
                    // The drain after a failed send is over: nothing more is coming.
                    if (s.write_dead) |e| if (nowMs() >= s.drain_until) {
                        s.die(e, c);
                        continue;
                    };
                    s.wedge(c);
                    return error.Interrupted;
                },
            }
        }
    }

    /// Sends the Tflush for `w`'s request, unless its reply beat us to it.
    /// With no tag free it waits on the starved list: the next reply frees
    /// one (the client keeps a tag aside for flushes, but several calls may
    /// be flushing at once).
    fn sendFlush(s: *Session, w: *Waiter) Error!void {
        const io = lockIo();
        s.mutex.unlock(io);
        s.wlock.lockUncancelable(io);
        s.mutex.lockUncancelable(io);
        if (w.done) {
            s.wlock.unlock(io);
            return;
        }
        const tag = s.client.submit(.{ .flush = .{ .oldtag = w.tag } }) catch |e| {
            s.wlock.unlock(io);
            if (e != error.NoTags) return error.Protocol;
            s.starve(w);
            return;
        };
        w.flush_tag = tag;
        s.waiting[tag] = w;
        try s.send();
    }
};

/// One call in flight (devmnt's Mntrpc): lives on the calling thread's stack
/// for the length of the rpc.
const Waiter = struct {
    caller: *Caller,
    tag: u16 = cloud9.notag,
    /// The Tflush sent for this call, until its Rflush (or the reply) is in.
    flush_tag: ?u16 = null,
    /// The reply (or, after a Tflush, the Rflush) came in.
    done: bool = false,
    /// It was the Rflush: the server dropped the request.
    flushed: bool = false,
    result: cloud9.Client.Result = undefined,
    /// Starved-list link, and the flag a freed tag sets.
    on_starved: bool = false,
    next_starved: ?*Waiter = null,
    tag_freed: bool = false,
};

/// One thread's side of a session: where its replies land, the descriptor
/// that wakes it, its interrupt source and its last error. Every thread
/// that makes calls on a session has its own; a Caller is used by one
/// thread at a time.
pub const Caller = struct {
    session: *Session,
    /// The reply to the latest call, copied out of the session's input
    /// buffer (msize bytes): result slices borrow it until the next call.
    reply: []u8,
    /// eventfd poked by whoever has something for this caller.
    wake: i32,
    /// After `error.Nine`, the server's Rerror text (copied, bounded).
    ename: [256]u8 = undefined,
    ename_len: usize = 0,
    /// Optional interrupt source consulted while a reply is outstanding;
    /// see `Interrupt`.
    interrupt: ?Interrupt = null,
    /// While set, a cancellation ends the rpc at once with `error.Interrupted`
    /// and wedges the session, with no Tflush: for the handshake (version,
    /// attach, the root stat), where there is no session yet to flush a
    /// request out of, and the honest answer to "stop waiting" is to hang up.
    abort_on_cancel: bool = false,

    pub fn init(s: *Session) !Caller {
        const rc = linux.eventfd(0, linux.EFD.CLOEXEC | linux.EFD.NONBLOCK);
        if (linux.errno(rc) != .SUCCESS) return error.SystemResources;
        const wake: i32 = @intCast(rc);
        errdefer _ = linux.close(wake);
        return .{ .session = s, .reply = try s.gpa.alloc(u8, s.in_buf.len), .wake = wake };
    }

    pub fn deinit(c: *Caller) void {
        c.session.gpa.free(c.reply);
        _ = linux.close(c.wake);
        c.* = undefined;
    }

    /// Generic RPC. Result slices borrow the caller's reply buffer until its
    /// next call. See `Session.await` for how the wait ends.
    pub fn rpc(c: *Caller, req: cloud9.Client.Request) Session.Error!cloud9.Client.Result {
        const s = c.session;
        const io = lockIo();
        c.ename_len = 0;
        var w: Waiter = .{ .caller = c };
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        defer s.leave(&w);
        try s.transmit(&w, req);
        try s.await(&w, .reply);
        if (w.flushed) return error.Interrupted;
        switch (w.result) {
            .fail => |ename| {
                c.setEname(ename);
                return error.Nine;
            },
            else => return w.result,
        }
    }

    /// Negotiates the protocol version with the msize `dial` was given.
    pub fn version(c: *Caller) Session.Error!void {
        const s = c.session;
        const r = try c.rpc(.{ .version = .{ .msize = s.msize } });
        if (!std.mem.eql(u8, r.version.version, "9P2000")) return error.Protocol;
        s.msize = r.version.msize;
    }

    pub fn attach(c: *Caller, fid: u32, uname: []const u8, aname: []const u8) Session.Error!cloud9.Qid {
        const r = try c.rpc(.{ .attach = .{ .fid = fid, .uname = uname, .aname = aname } });
        return r.attach;
    }

    /// True when the interrupt source has asked for the operation in progress to
    /// stop; consulted between the chunks of a read or write.
    pub fn interruptArmed(c: *const Caller) bool {
        const i = c.interrupt orelse return false;
        return i.armed(i.ctx);
    }

    /// Walk `names` from `fid` to `newfid`. A partial walk leaves `newfid` unbound
    /// (9P semantics) and reports `error.Nine` with ename "file does not exist".
    pub fn walk(c: *Caller, fid: u32, newfid: u32, names: []const []const u8) Session.Error!Session.Walk {
        const r = try c.rpc(.{ .walk = .{ .fid = fid, .newfid = newfid, .names = names } });
        if (r.walk.nwqid < names.len) {
            c.setEname("file does not exist");
            return error.Nine;
        }
        return .{ .nwqid = r.walk.nwqid, .wqid = r.walk.wqid };
    }

    /// allocFid + zero-element walk. The fid is released again on failure.
    pub fn clone(c: *Caller, fid: u32) Session.Error!u32 {
        const newfid = c.session.allocFid();
        errdefer c.session.freeFid(newfid);
        _ = try c.walk(fid, newfid, &.{});
        return newfid;
    }

    pub fn open(c: *Caller, fid: u32, mode: u8) Session.Error!Session.Open {
        const r = try c.rpc(.{ .open = .{ .fid = fid, .mode = mode } });
        c.noteIounit(fid, r.open.iounit);
        return .{ .qid = r.open.qid, .iounit = r.open.iounit };
    }

    pub fn create(c: *Caller, fid: u32, name: []const u8, perm: u32, mode: u8) Session.Error!Session.Open {
        const r = try c.rpc(.{ .create = .{ .fid = fid, .name = name, .perm = perm, .mode = mode } });
        c.noteIounit(fid, r.create.iounit);
        return .{ .qid = r.create.qid, .iounit = r.create.iounit };
    }

    /// Reads into `buf`, chunking by min(maxRead, iounit) and stopping at the first
    /// short read. Returns the number of bytes read (0 at end of file).
    pub fn read(c: *Caller, fid: u32, offset: u64, buf: []u8) Session.Error!usize {
        return readWith(c, rpc, fid, offset, buf, c.chunk(fid, .read));
    }

    /// Writes `data`, chunking like `read` and stopping at the first short write.
    pub fn write(c: *Caller, fid: u32, offset: u64, data: []const u8) Session.Error!usize {
        return writeWith(c, rpc, fid, offset, data, c.chunk(fid, .write));
    }

    /// The returned Stat's strings (name/uid/gid/muid) borrow the caller's
    /// reply buffer: they are valid only until its next rpc. Copy what must
    /// outlive it.
    pub fn stat(c: *Caller, fid: u32) Session.Error!cloud9.Stat {
        const r = try c.rpc(.{ .stat = .{ .fid = fid } });
        return r.stat;
    }

    pub fn wstat(c: *Caller, fid: u32, st: cloud9.Stat) Session.Error!void {
        _ = try c.rpc(.{ .wstat = .{ .fid = fid, .stat = st } });
    }

    /// Frees the fid locally even when the server reports an error.
    pub fn clunk(c: *Caller, fid: u32) Session.Error!void {
        defer c.session.freeFid(fid);
        _ = try c.rpc(.{ .clunk = .{ .fid = fid } });
    }

    /// Frees the fid locally even when the server reports an error.
    pub fn remove(c: *Caller, fid: u32) Session.Error!void {
        defer c.session.freeFid(fid);
        _ = try c.rpc(.{ .remove = .{ .fid = fid } });
    }

    /// Maps the last Rerror text to an errno (case-insensitive substring match).
    pub fn errno(c: *const Caller) linux.E {
        return enameToErrno(c.ename[0..c.ename_len]);
    }

    // -- internals --------------------------------------------------------------

    fn setEname(c: *Caller, text: []const u8) void {
        const n = @min(text.len, 255);
        @memcpy(c.ename[0..n], text[0..n]);
        c.ename_len = n;
    }

    fn noteIounit(c: *Caller, fid: u32, iounit: u32) void {
        const s = c.session;
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        if (iounit == 0) {
            _ = s.iounits.remove(fid);
        } else {
            s.iounits.put(s.gpa, fid, iounit) catch {};
        }
    }

    fn chunk(c: *Caller, fid: u32, dir: enum { read, write }) u32 {
        const s = c.session;
        const io = lockIo();
        s.mutex.lockUncancelable(io);
        defer s.mutex.unlock(io);
        const max = switch (dir) {
            .read => s.client.maxRead(),
            .write => s.client.maxWrite(),
        };
        return chunkSize(max, s.iounits.get(fid) orelse 0);
    }

    const Ready = enum { socket, wake, cancel, timeout };

    /// Blocks until the socket is readable (`.socket`; `sock` is -1 when this
    /// caller does not hold the baton), the caller is poked (`.wake`), the
    /// interrupt source wants the request in flight cancelled (`.cancel`),
    /// `timeout_ms` passes with none of that (`.timeout`; -1 waits forever),
    /// or `stop_fd` fires (`error.Stopped`). Anything the interrupt source
    /// consumes without asking for a cancellation simply resumes the wait.
    fn wait(c: *Caller, sock: i32, timeout_ms: i32) Session.Error!Ready {
        const stop_fd = c.session.stop_fd;
        while (true) {
            var pfds: [4]linux.pollfd = undefined;
            var n: usize = 0;
            pfds[n] = .{ .fd = sock, .events = linux.POLL.IN, .revents = 0 }; // -1: ignored by poll
            n += 1;
            pfds[n] = .{ .fd = c.wake, .events = linux.POLL.IN, .revents = 0 };
            n += 1;
            const stop_at: ?usize = if (stop_fd >= 0) n else null;
            if (stop_at != null) {
                pfds[n] = .{ .fd = stop_fd, .events = linux.POLL.IN, .revents = 0 };
                n += 1;
            }
            const ifd: i32 = if (c.interrupt) |i| i.watch(i.ctx) else -1;
            const int_at: ?usize = if (ifd >= 0) n else null;
            if (int_at != null) {
                pfds[n] = .{ .fd = ifd, .events = linux.POLL.IN, .revents = 0 };
                n += 1;
            }
            const prc = linux.poll(&pfds, @intCast(n), timeout_ms);
            switch (linux.errno(prc)) {
                .SUCCESS => {},
                .INTR, .AGAIN => continue,
                else => return error.Io,
            }
            if (prc == 0) return .timeout;
            // A reply that is already there wins over everything else.
            if (pfds[0].revents != 0) return .socket;
            if (pfds[1].revents != 0) {
                var count: u64 = 0;
                _ = linux.read(c.wake, std.mem.asBytes(&count), 8);
                return .wake;
            }
            if (stop_at) |i| {
                if (pfds[i].revents != 0) return error.Stopped;
            }
            if (int_at) |i| {
                if (pfds[i].revents != 0) {
                    const src = c.interrupt.?;
                    if (try src.onReadable(src.ctx)) return .cancel;
                }
            }
        }
    }
};

/// A result whose slices point into `from` (the frame in the client's input
/// buffer), with those slices moved to the same offsets in `to`, where the
/// frame is copied.
fn copyResult(r: cloud9.Client.Result, from: []const u8, to: []u8) cloud9.Client.Result {
    @memcpy(to[0..from.len], from);
    const move = struct {
        fn f(slice: []const u8, src: []const u8, dst: []u8) []const u8 {
            const lo = @intFromPtr(src.ptr);
            const at = @intFromPtr(slice.ptr);
            if (at < lo or at + slice.len > lo + src.len) return slice; // a literal, not the frame
            return dst[at - lo ..][0..slice.len];
        }
    }.f;
    var out = r;
    switch (out) {
        .fail => |*e| e.* = move(e.*, from, to),
        .version => |*v| v.version = move(v.version, from, to),
        .read => |*d| d.* = move(d.*, from, to),
        .stat => |*st| {
            st.name = move(st.name, from, to);
            st.uid = move(st.uid, from, to);
            st.gid = move(st.gid, from, to);
            st.muid = move(st.muid, from, to);
        },
        else => {},
    }
    return out;
}

/// Writes everything in the client's output buffer to the socket (with `wlock` held).
fn writeOut(s: *Session) Session.Error!void {
    while (s.client.output().len != 0) {
        const out = s.client.output();
        const rc = linux.write(s.fd, out.ptr, out.len);
        switch (linux.errno(rc)) {
            .SUCCESS => {
                if (rc == 0) return error.Closed;
                s.client.wrote(rc);
            },
            .INTR, .AGAIN => continue,
            .PIPE, .CONNRESET => return error.Closed,
            else => return error.Io,
        }
    }
}

/// The monotonic clock in milliseconds: deadlines, not timestamps.
fn nowMs() i64 {
    var ts: linux.timespec = undefined;
    _ = linux.clock_gettime(.MONOTONIC, &ts);
    return @as(i64, ts.sec) * 1000 + @divTrunc(@as(i64, ts.nsec), 1_000_000);
}

fn chunkSize(max: u32, iounit: u32) u32 {
    if (iounit != 0 and iounit < max) return iounit;
    return max;
}

/// Chunked read over any rpc-shaped function (injected so the loop is testable).
fn readWith(
    s: anytype,
    comptime rpcFn: anytype,
    fid: u32,
    offset: u64,
    buf: []u8,
    max_chunk: u32,
) Session.Error!usize {
    if (max_chunk == 0) return error.Protocol;
    var done: usize = 0;
    while (done < buf.len) {
        // Like read(2): an interruption after some data arrived is a short read.
        if (done != 0 and s.interruptArmed()) break;
        const want: u32 = @intCast(@min(buf.len - done, max_chunk));
        const r = rpcFn(s, .{ .read = .{ .fid = fid, .offset = offset + done, .count = want } }) catch |e| {
            if (e == error.Interrupted and done != 0) break;
            return e;
        };
        const data = r.read;
        @memcpy(buf[done..][0..data.len], data);
        done += data.len;
        if (data.len < want) break;
    }
    return done;
}

/// Chunked write over any rpc-shaped function.
fn writeWith(
    s: anytype,
    comptime rpcFn: anytype,
    fid: u32,
    offset: u64,
    data: []const u8,
    max_chunk: u32,
) Session.Error!usize {
    if (max_chunk == 0) return error.Protocol;
    var done: usize = 0;
    while (done < data.len) {
        if (done != 0 and s.interruptArmed()) break;
        const want: usize = @min(data.len - done, max_chunk);
        const r = rpcFn(s, .{ .write = .{ .fid = fid, .offset = offset + done, .data = data[done..][0..want] } }) catch |e| {
            if (e == error.Interrupted and done != 0) break;
            return e;
        };
        done += r.write;
        if (r.write < want) break;
    }
    return done;
}

fn readSocket(fd: i32, buf: []u8) Session.Error!usize {
    while (true) {
        const rc = linux.read(fd, buf.ptr, buf.len);
        switch (linux.errno(rc)) {
            .SUCCESS => return rc,
            .INTR, .AGAIN => continue,
            .CONNRESET => return error.Closed,
            else => return error.Io,
        }
    }
}

/// Rerror text → errno, per docs/DESIGN.md (first match wins): cloud9's
/// fs.enameErrno, which a server's own tests can ask too.
pub fn enameToErrno(ename: []const u8) linux.E {
    return @fromBackingInt(@intCast(cloud9.fs.enameErrno(ename)));
}

// -- transport ------------------------------------------------------------------

fn openTransport(address: Address) !i32 {
    switch (address) {
        .fd => |fd| return fd,
        .unix => |path| {
            if (path.len == 0 or path.len >= 108) return error.NameTooLong;
            var sa: linux.sockaddr.un = .{ .path = @splat(0) };
            @memcpy(sa.path[0..path.len], path);
            const fd = try newSocket(linux.AF.UNIX, 0);
            errdefer _ = linux.close(fd);
            try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.un));
            return fd;
        },
        .tcp => |t| {
            const ip = std.Io.net.IpAddress.parse(t.host, t.port) catch return error.InvalidAddress;
            switch (ip) {
                .ip4 => |a| {
                    const sa: linux.sockaddr.in = .{
                        .port = std.mem.nativeToBig(u16, t.port),
                        .addr = std.mem.bytesToValue(u32, &a.bytes),
                    };
                    const fd = try newSocket(linux.AF.INET, linux.IPPROTO.TCP);
                    errdefer _ = linux.close(fd);
                    setNodelay(fd);
                    try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.in));
                    return fd;
                },
                .ip6 => |a| {
                    const sa: linux.sockaddr.in6 = .{
                        .port = std.mem.nativeToBig(u16, t.port),
                        .flowinfo = 0,
                        .addr = a.bytes,
                        .scope_id = 0,
                    };
                    const fd = try newSocket(linux.AF.INET6, linux.IPPROTO.TCP);
                    errdefer _ = linux.close(fd);
                    setNodelay(fd);
                    try doConnect(fd, @ptrCast(&sa), @sizeOf(linux.sockaddr.in6));
                    return fd;
                },
            }
        },
    }
}

fn newSocket(domain: u32, protocol: u32) !i32 {
    const rc = linux.socket(domain, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, protocol);
    switch (linux.errno(rc)) {
        .SUCCESS => return @intCast(rc),
        .MFILE, .NFILE => return error.ProcessFdQuotaExceeded,
        .AFNOSUPPORT, .PROTONOSUPPORT => return error.AddressFamilyNotSupported,
        .ACCES => return error.AccessDenied,
        .NOMEM, .NOBUFS => return error.SystemResources,
        else => return error.Unexpected,
    }
}

fn setNodelay(fd: i32) void {
    const one: u32 = 1;
    _ = linux.setsockopt(fd, linux.IPPROTO.TCP, linux.TCP.NODELAY, @ptrCast(&one), @sizeOf(u32));
}

fn doConnect(fd: i32, addr: *const linux.sockaddr, len: linux.socklen_t) !void {
    while (true) {
        const rc = linux.connect(fd, addr, len);
        switch (linux.errno(rc)) {
            .SUCCESS => return,
            .INTR => continue,
            .CONNREFUSED => return error.ConnectionRefused,
            .NOENT, .NOTDIR => return error.FileNotFound,
            .ACCES, .PERM => return error.AccessDenied,
            .TIMEDOUT => return error.ConnectionTimedOut,
            .NETUNREACH, .HOSTUNREACH => return error.NetworkUnreachable,
            .ADDRNOTAVAIL => return error.AddressNotAvailable,
            .AGAIN, .INPROGRESS => return error.WouldBlock,
            else => return error.Unexpected,
        }
    }
}

// -- tests ----------------------------------------------------------------------

const testing = std.testing;

test {
    testing.refAllDecls(@This());
}

test "acme's address and argument refusals are EINVAL, and remap nothing an older rule answers" {
    for ([_][]const u8{
        "no match for regexp",
        "no previous regular expression",
        "address out of range",
        "address out of range: a column counts from 1",
        "addresses out of order",
        "range past end of body",
        "no address: the last one written to addr failed",
        "pane not locked by this open",
        "size: too small for the panes, each its tag and 2 rows \"160 57\"",
        "size: a frontend is attached and owns the size \"120 40\"",
        "no question asked \"answer\"",
        "answer takes k j or -",
        "answer takes a b or - \"answer\"",
    }) |ename| try testing.expectEqual(linux.E.INVAL, enameToErrno(ename));
    // Earlier rules still win: a fid's own words stay EBADF.
    try testing.expectEqual(linux.E.BADF, enameToErrno("fid unknown or out of range"));
    try testing.expectEqual(linux.E.NOENT, enameToErrno("no such file: answer takes"));
}

test "ename → errno mapping" {
    try testing.expectEqual(linux.E.INTR, enameToErrno("Interrupted system call"));
    try testing.expectEqual(linux.E.INTR, enameToErrno("read interrupted"));
    try testing.expectEqual(linux.E.IO, enameToErrno("window shut down"));
    try testing.expectEqual(linux.E.NOENT, enameToErrno("file does not exist"));
    try testing.expectEqual(linux.E.NOENT, enameToErrno("No Such File"));
    try testing.expectEqual(linux.E.MFILE, enameToErrno("too many open files"));
    try testing.expectEqual(linux.E.NOENT, enameToErrno("directory entry not found"));
    try testing.expectEqual(linux.E.EXIST, enameToErrno("file already exists"));
    try testing.expectEqual(linux.E.NOTEMPTY, enameToErrno("directory not empty"));
    try testing.expectEqual(linux.E.NOTDIR, enameToErrno("not a directory"));
    try testing.expectEqual(linux.E.ISDIR, enameToErrno("is a directory"));
    try testing.expectEqual(linux.E.ACCES, enameToErrno("permission denied"));
    try testing.expectEqual(linux.E.ACCES, enameToErrno("access denied"));
    try testing.expectEqual(linux.E.ROFS, enameToErrno("read-only file system"));
    try testing.expectEqual(linux.E.NOSPC, enameToErrno("no space left"));
    try testing.expectEqual(linux.E.PERM, enameToErrno("operation not permitted"));
    try testing.expectEqual(linux.E.PERM, enameToErrno("cannot remove root"));
    try testing.expectEqual(linux.E.BADF, enameToErrno("unknown fid"));
    try testing.expectEqual(linux.E.BADF, enameToErrno("fid in use")); // "fid" precedes "in use"
    try testing.expectEqual(linux.E.INVAL, enameToErrno("bad offset"));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("invalid argument"));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("bad request"));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("unknown control message \"Bogus 1\""));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("wrong #args in control message \"Newcol 2\""));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("ill-formed control message"));
    try testing.expectEqual(linux.E.INVAL, enameToErrno("unknown control message \"Find permission denied\""));
    try testing.expectEqual(linux.E.BUSY, enameToErrno("device busy"));
    try testing.expectEqual(linux.E.NAMETOOLONG, enameToErrno("name too long"));
    try testing.expectEqual(linux.E.OPNOTSUPP, enameToErrno("operation not supported"));
    try testing.expectEqual(linux.E.IO, enameToErrno("something odd happened"));
    try testing.expectEqual(linux.E.IO, enameToErrno(""));
}

test "fid allocator recycles and never hands out 0" {
    var s: Session = undefined;
    s.gpa = testing.allocator;
    s.mutex = .init;
    s.next_fid = 1;
    s.free_fids = .empty;
    s.iounits = .empty;
    defer s.free_fids.deinit(s.gpa);
    defer s.iounits.deinit(s.gpa);

    const a = s.allocFid();
    const b = s.allocFid();
    const c = s.allocFid();
    try testing.expectEqual(@as(u32, 1), a);
    try testing.expectEqual(@as(u32, 2), b);
    try testing.expectEqual(@as(u32, 3), c);
    s.freeFid(b);
    try testing.expectEqual(b, s.allocFid());
    s.freeFid(a);
    s.freeFid(c);
    const x = s.allocFid();
    const y = s.allocFid();
    try testing.expect((x == a and y == c) or (x == c and y == a));
    try testing.expectEqual(@as(u32, 4), s.allocFid());
    try testing.expect(a != 0 and b != 0 and c != 0);
}

test "chunkSize honours iounit only when smaller" {
    try testing.expectEqual(@as(u32, 100), chunkSize(100, 0));
    try testing.expectEqual(@as(u32, 40), chunkSize(100, 40));
    try testing.expectEqual(@as(u32, 100), chunkSize(100, 400));
}

/// Fake rpc for the chunked read/write loops: a file of `len` bytes where byte i == i & 0xff.
const FakeFile = struct {
    len: usize,
    calls: usize = 0,
    max_count: u32 = 0,
    short_write_at: ?usize = null,
    /// Fail the call with `error.Interrupted` once this many calls were made.
    interrupt_at: ?usize = null,
    /// Report an armed interrupt once this many calls were made.
    armed_at: ?usize = null,
    scratch: [4096]u8 = undefined,

    fn interruptArmed(f: *const FakeFile) bool {
        return if (f.armed_at) |at| f.calls >= at else false;
    }

    fn rpc(f: *FakeFile, req: cloud9.Client.Request) Session.Error!cloud9.Client.Result {
        f.calls += 1;
        if (f.interrupt_at) |at| if (f.calls > at) return error.Interrupted;
        switch (req) {
            .read => |r| {
                f.max_count = @max(f.max_count, r.count);
                if (r.offset >= f.len) return .{ .read = "" };
                const n: usize = @min(@as(usize, r.count), f.len - @as(usize, @intCast(r.offset)));
                for (f.scratch[0..n], 0..) |*b, i| b.* = @truncate(r.offset + i);
                return .{ .read = f.scratch[0..n] };
            },
            .write => |w| {
                f.max_count = @max(f.max_count, @as(u32, @intCast(w.data.len)));
                if (f.short_write_at) |at| {
                    if (w.offset + w.data.len > at) {
                        const n: usize = if (w.offset >= at) 0 else @intCast(at - w.offset);
                        return .{ .write = @intCast(n) };
                    }
                }
                return .{ .write = @intCast(w.data.len) };
            },
            else => unreachable,
        }
    }
};

test "read chunks by max_chunk and stops at a short read" {
    var f: FakeFile = .{ .len = 2500 };
    var buf: [4000]u8 = undefined;
    const n = try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000);
    try testing.expectEqual(@as(usize, 2500), n);
    try testing.expectEqual(@as(usize, 3), f.calls); // 1000, 1000, 500 (short → stop)
    try testing.expectEqual(@as(u32, 1000), f.max_count);
    for (buf[0..n], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b);

    // Reading exactly up to a chunk boundary uses one call per chunk and no more.
    f = .{ .len = 2000 };
    try testing.expectEqual(@as(usize, 2000), try readWith(&f, FakeFile.rpc, 7, 0, buf[0..2000], 1000));
    try testing.expectEqual(@as(usize, 2), f.calls);

    // Offset past EOF → 0.
    f = .{ .len = 10 };
    try testing.expectEqual(@as(usize, 0), try readWith(&f, FakeFile.rpc, 7, 50, &buf, 1000));
}

test "write chunks and stops at a short write" {
    var f: FakeFile = .{ .len = 0 };
    var data: [2500]u8 = undefined;
    for (&data, 0..) |*b, i| b.* = @truncate(i);
    try testing.expectEqual(@as(usize, 2500), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000));
    try testing.expectEqual(@as(usize, 3), f.calls);
    try testing.expectEqual(@as(u32, 1000), f.max_count);

    f = .{ .len = 0, .short_write_at = 1500 };
    try testing.expectEqual(@as(usize, 1500), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000));
    try testing.expectEqual(@as(usize, 2), f.calls);
}

test "interrupted chunk loops: partial count if data moved, Interrupted otherwise" {
    var buf: [4000]u8 = undefined;
    // The second chunk's rpc is interrupted: the first chunk is returned.
    var f: FakeFile = .{ .len = 2500, .interrupt_at = 1 };
    try testing.expectEqual(@as(usize, 1000), try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000));
    // The first chunk's rpc is interrupted: nothing was transferred.
    f = .{ .len = 2500, .interrupt_at = 0 };
    try testing.expectError(error.Interrupted, readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000));
    // A reply that raced the flush arms the interrupt: stop before the next chunk.
    f = .{ .len = 2500, .armed_at = 2 };
    try testing.expectEqual(@as(usize, 2000), try readWith(&f, FakeFile.rpc, 7, 0, &buf, 1000));
    try testing.expectEqual(@as(usize, 2), f.calls);
    // Same for writes.
    var data: [2500]u8 = undefined;
    for (&data, 0..) |*b, i| b.* = @truncate(i);
    f = .{ .len = 0, .interrupt_at = 2 };
    try testing.expectEqual(@as(usize, 2000), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000));
    f = .{ .len = 0, .interrupt_at = 0 };
    try testing.expectError(error.Interrupted, writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000));
    f = .{ .len = 0, .armed_at = 1 };
    try testing.expectEqual(@as(usize, 1000), try writeWith(&f, FakeFile.rpc, 7, 0, &data, 1000));
}

// -- in-process server test ---------------------------------------------------------

test "flush grace: a Tflush the server never answers wedges the session after the grace" {
    var pi = try PipeInterrupt.init(7);
    defer pi.deinit();
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .ignore, .on_hang_inject = &pi };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    s.interrupt = pi.interface();
    s.session.flush_grace_ms = 100;
    // The read at offset 0 hangs; the injected INTERRUPT sends a Tflush; the
    // server ignores it; the grace runs out.
    var buf: [100]u8 = undefined;
    try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expect(s.session.wedged);
    // From here on the session is closed for business, without another
    // byte to the server.
    try testing.expectError(error.Closed, s.read(1, 10, &buf));
    try testing.expectError(error.Closed, s.clunk(1));
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    // The socket is still the session's to close: the server's thread ends
    // when `close` drops it.
}

test "flush grace: zero waits for the Rflush, however late" {
    var pi = try PipeInterrupt.init(7);
    defer pi.deinit();
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi, .read_delay_ns = 0 };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    s.interrupt = pi.interface();
    s.session.flush_grace_ms = 0;
    var buf: [100]u8 = undefined;
    try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
    try testing.expect(!s.session.wedged);
    // The session lives: the Rflush released the tag and reads go on.
    try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
}

test "abort_on_cancel: a cancellation ends the rpc at once, sends no Tflush, wedges the session" {
    var pi = try PipeInterrupt.init(7);
    defer pi.deinit();
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .on_hang_inject = &pi };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    s.interrupt = pi.interface();
    s.abort_on_cancel = true;
    var buf: [100]u8 = undefined;
    try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
    try testing.expect(s.session.wedged);
    try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst));
    try testing.expectError(error.Closed, s.read(1, 10, &buf));
}

/// A tiny 9P2000 backend on a cloud9.Server: answers version/attach/walk/stat/open/
/// read/clunk/remove with canned data. Runs in its own thread over a socketpair.
/// Test support only (bridge.zig's tests use it too).
pub const FakeServer = struct {
    fd: i32,
    msize: u32,
    max_read_count: u32 = 0,
    file_len: usize,
    /// A Tread at this offset is never answered (a blocked stream read); the
    /// server keeps serving whatever else arrives, notably a Tflush.
    hang_offset: ?u64 = null,
    /// What a Tflush gets: the connection dropped (`.hangup`), an Rflush,
    /// first the Rread the flush was aimed at and then the Rflush (the
    /// race), or nothing at all (`.ignore`: counted, never answered, the
    /// read stays hung — a wedged server).
    on_flush: enum { hangup, rflush, reply_then_rflush, ignore } = .hangup,
    /// Observed by the test thread: number of Tflush seen and the last oldtag.
    flushes: std.atomic.Value(u32) = .init(0),
    flush_oldtag: std.atomic.Value(u32) = .init(0xFFFF),
    /// The tag of the hung Tread, for the test to compare with `flush_oldtag`.
    hung_tag: std.atomic.Value(u32) = .init(0xFFFF),
    /// How many Treads are hung right now (several callers can each park one).
    hung_now: std.atomic.Value(u32) = .init(0),
    /// A Tstat first answers every hung Tread (with data), then itself: lets a
    /// test release a crowd of parked readers.
    release_on_stat: bool = false,
    /// The first byte of every Rread is the fid it was read on (low byte):
    /// lets a test tell which fid, i.e. which open, a read went to.
    fid_in_data: bool = false,
    /// Tclunks seen.
    clunks: std.atomic.Value(u32) = .init(0),
    /// When set, the server injects that source's INTERRUPT the moment a read
    /// hangs, so the cancellation provably arrives while the wait is on.
    on_hang_inject: ?*PipeInterrupt = null,
    /// Delay before every Rread, so a test can be sure the client is waiting.
    read_delay_ns: u64 = 0,
    /// A Twrite is answered, and then the server hangs up (pardes answering
    /// a Restore and cutting every connection).
    hangup_after_write: bool = false,
    /// Set once the server has closed its end.
    gone: std.atomic.Value(bool) = .init(false),

    pub const file_qid: cloud9.Qid = .{ .type = 0, .version = 3, .path = 0x1234 };
    pub const dir_qid: cloud9.Qid = .{ .type = cloud9.qtdir, .version = 1, .path = 0x1 };

    pub fn run(fs: *FakeServer) void {
        fs.loop() catch |e| std.debug.print("fake server: {s}\n", .{@errorName(e)});
        _ = linux.close(fs.fd);
        fs.gone.store(true, .release);
    }

    fn loop(fs: *FakeServer) !void {
        const gpa = testing.allocator;
        const in = try gpa.alloc(u8, fs.msize);
        defer gpa.free(in);
        const out = try gpa.alloc(u8, fs.msize * 2);
        defer gpa.free(out);
        var srv: cloud9.Server = .init(.{ .in = in, .out = out });
        var tmp: [4096]u8 = undefined;
        var data: [8192]u8 = undefined;
        const Hung = struct { tag: u16, fid: u32, offset: u64, count: u32 };
        var hung: [max_tags + 1]?Hung = @splat(null);
        while (true) {
            while (try srv.receive()) |req| {
                const tag = req.tag;
                switch (req.msg) {
                    .tversion => |m| try srv.negotiate(m.msize, m.version),
                    .tattach => try srv.reply(tag, .{ .rattach = .{ .qid = dir_qid } }),
                    .twalk => |m| {
                        var wq: [cloud9.max_welem]cloud9.Qid = @splat(dir_qid);
                        var n: u16 = 0;
                        for (m.wname[0..m.nwname]) |name| {
                            if (std.mem.eql(u8, name, "file")) {
                                wq[n] = file_qid;
                            } else if (std.mem.eql(u8, name, "dir")) {
                                wq[n] = dir_qid;
                            } else break;
                            n += 1;
                        }
                        if (n == 0 and m.nwname != 0) {
                            try srv.reply(tag, .{ .rerror = .{ .ename = "file does not exist" } });
                        } else {
                            try srv.reply(tag, .{ .rwalk = .{ .nwqid = n, .wqid = wq } });
                        }
                    },
                    .tstat => {
                        if (fs.release_on_stat) for (&hung) |*slot| if (slot.*) |h| {
                            try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } });
                            slot.* = null;
                            _ = fs.hung_now.fetchSub(1, .seq_cst);
                        };
                        try srv.reply(tag, .{ .rstat = .{ .stat = .{
                            .type = 0,
                            .dev = 0,
                            .qid = file_qid,
                            .mode = 0o644,
                            .atime = 1,
                            .mtime = 2,
                            .length = fs.file_len,
                            .name = "file",
                            .uid = "u",
                            .gid = "g",
                            .muid = "u",
                        } } });
                    },
                    .topen => |m| try srv.reply(tag, .{ .ropen = .{ .qid = file_qid, .iounit = if (m.mode == cloud9.owrite) 700 else 0 } }),
                    .tread => |m| {
                        fs.max_read_count = @max(fs.max_read_count, m.count);
                        if (fs.hang_offset != null and fs.hang_offset.? == m.offset) {
                            for (&hung) |*slot| if (slot.* == null) {
                                slot.* = .{ .tag = tag, .fid = m.fid, .offset = m.offset, .count = m.count };
                                break;
                            };
                            fs.hung_tag.store(tag, .seq_cst);
                            _ = fs.hung_now.fetchAdd(1, .seq_cst);
                            if (fs.on_hang_inject) |p| try p.inject(p.unique);
                        } else {
                            if (fs.read_delay_ns != 0) {
                                const ts: linux.timespec = .{ .sec = @intCast(fs.read_delay_ns / std.time.ns_per_s), .nsec = @intCast(fs.read_delay_ns % std.time.ns_per_s) };
                                _ = linux.nanosleep(&ts, null);
                            }
                            try srv.reply(tag, .{ .rread = .{ .data = fs.readData(&data, m.fid, m.offset, m.count) } });
                        }
                    },
                    .twrite => |m| {
                        try srv.reply(tag, .{ .rwrite = .{ .count = @intCast(m.data.len) } });
                        if (fs.hangup_after_write) {
                            while (srv.output().len != 0) {
                                const o = srv.output();
                                const rc = linux.write(fs.fd, o.ptr, o.len);
                                if (linux.errno(rc) != .SUCCESS) return error.Write;
                                srv.wrote(rc);
                            }
                            return;
                        }
                    },
                    .tclunk => {
                        _ = fs.clunks.fetchAdd(1, .seq_cst);
                        try srv.reply(tag, .rclunk);
                    },
                    .tremove => try srv.reply(tag, .{ .rerror = .{ .ename = "permission denied" } }),
                    .twstat => try srv.reply(tag, .rwstat),
                    .tflush => |m| {
                        fs.flush_oldtag.store(m.oldtag, .seq_cst);
                        _ = fs.flushes.fetchAdd(1, .seq_cst);
                        switch (fs.on_flush) {
                            // The test's "hang up now" signal.
                            .hangup => return,
                            .ignore => {},
                            .rflush => {
                                for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) {
                                    slot.* = null;
                                    _ = fs.hung_now.fetchSub(1, .seq_cst);
                                };
                                try srv.reply(tag, .rflush);
                            },
                            .reply_then_rflush => {
                                for (&hung) |*slot| if (slot.*) |h| if (h.tag == m.oldtag) {
                                    try srv.reply(h.tag, .{ .rread = .{ .data = fs.readData(&data, h.fid, h.offset, h.count) } });
                                    slot.* = null;
                                    _ = fs.hung_now.fetchSub(1, .seq_cst);
                                };
                                try srv.reply(tag, .rflush);
                            },
                        }
                    },
                    else => try srv.reply(tag, .{ .rerror = .{ .ename = "not supported" } }),
                }
                srv.release();
            }
            while (srv.output().len != 0) {
                const o = srv.output();
                const rc = linux.write(fs.fd, o.ptr, o.len);
                if (linux.errno(rc) != .SUCCESS) return error.Write;
                srv.wrote(rc);
            }
            const rc = linux.read(fs.fd, &tmp, tmp.len);
            if (linux.errno(rc) != .SUCCESS) return error.Read;
            if (rc == 0) return;
            if (srv.push(tmp[0..rc]) != rc) return error.Overflow;
        }
    }

    /// What a Tread on `fid` gets: `fill`, stamped with the fid under `fid_in_data`.
    fn readData(fs: *const FakeServer, data: []u8, fid: u32, offset: u64, count: u32) []const u8 {
        const got = fs.fill(data, offset, count);
        if (fs.fid_in_data and got.len != 0) data[0] = @truncate(fid);
        return got;
    }

    /// File contents: byte i == i & 0xff, `file_len` bytes long.
    fn fill(fs: *const FakeServer, data: []u8, offset: u64, count: u32) []const u8 {
        var n: usize = 0;
        if (offset < fs.file_len) n = @min(@as(usize, count), fs.file_len - @as(usize, @intCast(offset)));
        n = @min(n, data.len);
        for (data[0..n], 0..) |*b, i| b.* = @truncate(offset + i);
        return data[0..n];
    }

    /// A connected session (fid 0 attached, fid 1 walked to "file" and opened
    /// for reading) plus the server thread; `close` when done.
    pub const Pair = struct {
        server: *FakeServer,
        session: *Session,
        /// The test thread's caller; `newCaller` makes more.
        caller: *Caller,
        thread: std.Thread,

        pub fn newCaller(p: *Pair) !*Caller {
            const c = try testing.allocator.create(Caller);
            errdefer testing.allocator.destroy(c);
            c.* = try Caller.init(p.session);
            return c;
        }

        pub fn freeCaller(c: *Caller) void {
            c.deinit();
            testing.allocator.destroy(c);
        }

        pub fn close(p: *Pair) void {
            freeCaller(p.caller);
            p.session.deinit();
            testing.allocator.destroy(p.session);
            p.thread.join();
        }
    };

    pub fn start(fs: *FakeServer) !Pair {
        var fds: [2]i32 = undefined;
        if (linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &fds)) != .SUCCESS) return error.Io;
        fs.fd = fds[1];
        const th = try std.Thread.spawn(.{}, FakeServer.run, .{fs});
        const s = try testing.allocator.create(Session);
        errdefer testing.allocator.destroy(s);
        s.* = try Session.connect(testing.allocator, .{ .fd = fds[0] }, fs.msize);
        errdefer s.deinit();
        var pair: Pair = .{ .server = fs, .session = s, .caller = undefined, .thread = th };
        pair.caller = try pair.newCaller();
        errdefer Pair.freeCaller(pair.caller);
        const c = pair.caller;
        _ = try c.attach(0, "me", "");
        const fid = s.allocFid();
        _ = try c.walk(0, fid, &.{"file"});
        _ = try c.open(fid, cloud9.oread);
        return pair;
    }
};

/// A fake interrupt source for the rpc wait loop: a SOCK_SEQPACKET pair stands
/// in for the FUSE descriptor (one datagram per request, like /dev/fuse
/// delivers one request per read). Mirrors the bridge's rules: an INTERRUPT for
/// `unique` arms and cancels, anything else is consumed and ignored.
pub const PipeInterrupt = struct {
    read_end: i32,
    write_end: i32,
    unique: u64,
    armed_flag: bool = false,
    /// Requests consumed that were not the matching INTERRUPT.
    ignored: usize = 0,

    const fuse_interrupt_opcode: u32 = 36;

    pub fn init(unique: u64) !PipeInterrupt {
        var fds: [2]i32 = undefined;
        const flags = linux.SOCK.SEQPACKET | linux.SOCK.CLOEXEC | linux.SOCK.NONBLOCK;
        if (linux.errno(linux.socketpair(linux.AF.UNIX, flags, 0, &fds)) != .SUCCESS) return error.Io;
        return .{ .read_end = fds[0], .write_end = fds[1], .unique = unique };
    }

    pub fn deinit(p: *PipeInterrupt) void {
        _ = linux.close(p.read_end);
        _ = linux.close(p.write_end);
    }

    pub fn interface(p: *PipeInterrupt) Interrupt {
        return .{ .ctx = p, .watch = watch, .onReadable = onReadable, .armed = armed };
    }

    /// Writes a FUSE_INTERRUPT request (InHeader + InterruptIn) naming `target`.
    pub fn inject(p: *PipeInterrupt, target: u64) !void {
        var wire: [48]u8 = undefined;
        std.mem.writeInt(u32, wire[0..4], 48, .little); // len
        std.mem.writeInt(u32, wire[4..8], fuse_interrupt_opcode, .little); // opcode
        std.mem.writeInt(u64, wire[8..16], 0x8000_0000_0000_0001, .little); // the interrupt's own unique
        @memset(wire[16..40], 0); // nodeid, uid, gid, pid, extlen, padding
        std.mem.writeInt(u64, wire[40..48], target, .little); // InterruptIn.unique
        if (linux.write(p.write_end, &wire, wire.len) != wire.len) return error.Io;
    }

    fn watch(ctx: *anyopaque) i32 {
        const p: *PipeInterrupt = @ptrCast(@alignCast(ctx));
        return p.read_end;
    }

    fn onReadable(ctx: *anyopaque) Session.Error!bool {
        const p: *PipeInterrupt = @ptrCast(@alignCast(ctx));
        var buf: [4096]u8 = undefined;
        const rc = linux.read(p.read_end, &buf, buf.len);
        if (linux.errno(rc) != .SUCCESS or rc != 48) return error.Io;
        const opcode = std.mem.readInt(u32, buf[4..8], .little);
        const target = std.mem.readInt(u64, buf[40..48], .little);
        if (opcode == fuse_interrupt_opcode and target == p.unique) {
            p.armed_flag = true;
            return true;
        }
        p.ignored += 1;
        return false;
    }

    fn armed(ctx: *anyopaque) bool {
        const p: *PipeInterrupt = @ptrCast(@alignCast(ctx));
        return p.armed_flag;
    }
};

test "rpc wait loop: INTERRUPT → Tflush → Rflush → error.Interrupted; session still usable" {
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    var pi = try PipeInterrupt.init(77);
    defer pi.deinit();
    s.interrupt = pi.interface();
    defer s.interrupt = null;

    // An INTERRUPT for some other request is consumed and ignored: the wait
    // goes on, and the reply (a read past the hang offset) arrives normally.
    var buf: [100]u8 = undefined;
    try pi.inject(78);
    try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf));
    try testing.expect(!pi.armed_flag);

    // The read at offset 0 hangs; the INTERRUPT for our request cancels it
    // (the stray one above is consumed along the way if the reply beat it).
    try pi.inject(77);
    try testing.expectError(error.Interrupted, s.read(1, 0, &buf));
    try testing.expect(pi.armed_flag);
    try testing.expectEqual(@as(usize, 1), pi.ignored);
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expectEqual(fs.hung_tag.load(.seq_cst), fs.flush_oldtag.load(.seq_cst));

    // Both tags are free again: further rpcs work.
    const st = try s.stat(1);
    try testing.expectEqual(@as(u64, 50), st.length);
    try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf));
    try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}

test "rpc wait loop: the reply beats the Rflush → data returned, stray Rflush swallowed" {
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .reply_then_rflush };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    var pi = try PipeInterrupt.init(5);
    defer pi.deinit();
    s.interrupt = pi.interface();
    defer s.interrupt = null;

    var buf: [40]u8 = undefined;
    try pi.inject(5);
    try testing.expectEqual(@as(usize, 30), try s.read(1, 0, buf[0..30]));
    for (buf[0..30], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b);
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expectEqual(fs.hung_tag.load(.seq_cst), fs.flush_oldtag.load(.seq_cst));

    // The Rflush is still in flight (or already buffered): the next rpcs must
    // step over it, and afterwards nothing is pending in the client.
    pi.armed_flag = false;
    const st = try s.stat(1);
    try testing.expectEqual(@as(u64, 50), st.length);
    try testing.expectEqual(@as(usize, 40), try s.read(1, 10, &buf));
    try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}

test "rpc wait loop: a read interrupted after some data is a short read" {
    // Chunks of maxRead = 1013 (msize 1024); the third chunk (offset 2026) hangs
    // and the server fires the INTERRUPT at that moment.
    var pi = try PipeInterrupt.init(9);
    defer pi.deinit();
    var fs: FakeServer = .{ .fd = -1, .msize = 1024, .file_len = 5000, .hang_offset = 2026, .on_flush = .rflush, .on_hang_inject = &pi };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.caller;
    s.interrupt = pi.interface();
    defer s.interrupt = null;

    var buf: [4000]u8 = undefined;
    try testing.expectEqual(@as(usize, 2026), try s.read(1, 0, &buf));
    for (buf[0..2026], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b);
    try testing.expect(pi.armed_flag);
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expectEqual(@as(usize, 0), s.session.client.pending());

    // An INTERRUPT already waiting when the read starts: the first chunk's
    // reply races the flush and wins, the armed flag then stops the loop.
    // (The delayed reply makes sure the INTERRUPT is seen first: with the
    // reply already readable the wait takes it and the flush falls on the
    // second chunk instead, which on a loaded machine happened.)
    pi.armed_flag = false;
    fs.read_delay_ns = 20 * std.time.ns_per_ms;
    try pi.inject(9);
    try testing.expectEqual(@as(usize, 1013), try s.read(1, 0, &buf));
    // The stray Rflush is consumed by the next call.
    _ = try s.stat(1);
    try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}

test "connectWatched: a silent server cannot pin the handshake past stop_fd" {
    // A server that accepts and then never answers: the version handshake has
    // nothing to read. With a readable stop_fd the connect must come back with
    // error.Stopped instead of blocking in readSocket (the fd is blocking), and
    // the caller's descriptor must survive: `Address.fd` is not ours to close.
    var sv: [2]i32 = undefined;
    try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &sv)));
    defer _ = linux.close(sv[0]);
    defer _ = linux.close(sv[1]);
    var p: [2]i32 = undefined;
    try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.pipe2(&p, .{ .CLOEXEC = true, .NONBLOCK = true })));
    defer _ = linux.close(p[0]);
    defer _ = linux.close(p[1]);
    try testing.expectEqual(@as(usize, 1), linux.write(p[1], "x", 1));

    const Probe = struct {
        const Self = @This();
        done: std.atomic.Value(bool) = .init(false),
        stopped: std.atomic.Value(bool) = .init(false),
        fd_open: std.atomic.Value(bool) = .init(false),

        fn run(w: *Self, client: i32, stop: i32) void {
            if (Session.connectWatched(testing.allocator, .{ .fd = client }, 8192, stop)) |session| {
                var s = session;
                s.deinit();
            } else |e| w.stopped.store(e == error.Stopped, .release);
            w.fd_open.store(linux.errno(linux.fcntl(client, linux.F.GETFD, 0)) == .SUCCESS, .release);
            w.done.store(true, .release);
        }
    };
    var w: Probe = .{};
    const th = try std.Thread.spawn(.{}, Probe.run, .{ &w, sv[0], p[0] });
    var waited_ms: usize = 0;
    while (!w.done.load(.acquire) and waited_ms < 3000) : (waited_ms += 10) {
        const ts: linux.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms };
        _ = linux.nanosleep(&ts, null);
    }
    try testing.expect(w.done.load(.acquire));
    try testing.expect(w.stopped.load(.acquire));
    try testing.expect(w.fd_open.load(.acquire));
    th.join();
}

test "session against an in-process cloud9.Server" {
    var fds: [2]i32 = undefined;
    try testing.expectEqual(linux.E.SUCCESS, linux.errno(linux.socketpair(linux.AF.UNIX, linux.SOCK.STREAM | linux.SOCK.CLOEXEC, 0, &fds)));

    var fs: FakeServer = .{ .fd = fds[1], .msize = 8192, .file_len = 20_000 };
    const th = try std.Thread.spawn(.{}, FakeServer.run, .{&fs});

    var session = try Session.connect(testing.allocator, .{ .fd = fds[0] }, 8192);
    defer {
        session.deinit();
        th.join();
    }
    try testing.expectEqual(@as(u32, 8192), session.msize);
    var caller = try Caller.init(&session);
    defer caller.deinit();
    const s = &caller;

    const root = try s.attach(0, "me", "");
    try testing.expectEqual(FakeServer.dir_qid.path, root.path);

    // Plain rpc + stat borrowing the input buffer.
    const fid = session.allocFid();
    const w = try s.walk(0, fid, &.{"file"});
    try testing.expectEqual(@as(u16, 1), w.nwqid);
    try testing.expectEqual(FakeServer.file_qid.path, w.wqid[0].path);
    const st = try s.stat(fid);
    try testing.expectEqualStrings("file", st.name);
    try testing.expectEqual(@as(u64, 20_000), st.length);

    // Chunked read: 20000 bytes at maxRead = msize - 11 = 8181 per chunk.
    _ = try s.open(fid, cloud9.oread);
    const buf = try testing.allocator.alloc(u8, 30_000);
    defer testing.allocator.free(buf);
    const n = try s.read(fid, 0, buf);
    try testing.expectEqual(@as(usize, 20_000), n);
    for (buf[0..n], 0..) |b, i| try testing.expectEqual(@as(u8, @truncate(i)), b);
    try testing.expectEqual(@as(u32, 8181), fs.max_read_count);
    try testing.expectEqual(@as(usize, 0), try s.read(fid, 20_000, buf));

    // iounit from open bounds the chunk.
    const wfid = try s.clone(fid);
    _ = try s.open(wfid, cloud9.owrite);
    fs.max_read_count = 0;
    _ = try s.read(wfid, 0, buf[0..3000]);
    try testing.expectEqual(@as(u32, 700), fs.max_read_count);
    try testing.expectEqual(@as(usize, 3000), try s.write(wfid, 0, buf[0..3000]));

    // Partial walk → error.Nine with a "not exist" ename → ENOENT.
    const pfid = session.allocFid();
    try testing.expectError(error.Nine, s.walk(0, pfid, &.{ "dir", "nope" }));
    try testing.expectEqual(linux.E.NOENT, s.errno());
    try testing.expectEqualStrings("file does not exist", s.ename[0..s.ename_len]);
    session.freeFid(pfid);

    // Server Rerror → error.Nine, ename copied, fid freed by remove even on error.
    try testing.expectError(error.Nine, s.remove(wfid));
    try testing.expectEqual(linux.E.ACCES, s.errno());
    try testing.expectEqual(wfid, session.allocFid()); // recycled
    session.freeFid(wfid);

    // Unsupported op → "not supported" → ENOTSUP; a plain wstat succeeds.
    try testing.expectError(error.Nine, s.rpc(.{ .auth = .{ .afid = 5, .uname = "me" } }));
    try testing.expectEqual(linux.E.OPNOTSUPP, s.errno());
    try s.wstat(fid, dontcare);
    try s.clunk(fid);
    try testing.expectEqual(fid, session.allocFid());
    session.freeFid(fid);

    // A clone bound to a fid that then fails to walk must release the fid.
    const before = session.next_fid;
    const cfid = session.allocFid();
    session.freeFid(cfid);
    try testing.expectError(error.Nine, s.walk(0, cfid, &.{"nope"}));
    try testing.expectEqual(before, session.next_fid);

    // The server hanging up makes the pending rpc fail with error.Closed.
    try testing.expectError(error.Closed, s.rpc(.{ .flush = .{ .oldtag = 0 } }));
}

// -- multiplexing ----------------------------------------------------------------------

/// Polls `cond` every millisecond for up to three seconds.
fn eventually(cond: anytype) bool {
    for (0..3000) |_| {
        if (cond.check()) return true;
        const ts: linux.timespec = .{ .sec = 0, .nsec = std.time.ns_per_ms };
        _ = linux.nanosleep(&ts, null);
    }
    return cond.check();
}

const HungAtLeast = struct {
    fs: *FakeServer,
    n: u32,
    fn check(h: HungAtLeast) bool {
        return h.fs.hung_now.load(.seq_cst) >= h.n;
    }
};

/// A caller reading `file` at offset 0 on its own thread (the offset the
/// FakeServer parks), recording how the read ended.
const ParkedRead = struct {
    caller: *Caller,
    thread: std.Thread = undefined,
    result: Session.Error!usize = 0,
    done: std.atomic.Value(bool) = .init(false),

    fn start(p: *ParkedRead) !void {
        p.thread = try std.Thread.spawn(.{}, run, .{p});
    }

    fn run(p: *ParkedRead) void {
        var buf: [100]u8 = undefined;
        p.result = p.caller.read(1, 0, &buf);
        p.done.store(true, .release);
    }
};

test "multiplexing: a parked read holds up only its own caller; its interrupt flushes exactly its tag" {
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush };
    var pair = try fs.start();
    defer pair.close();
    var pi = try PipeInterrupt.init(21);
    defer pi.deinit();
    var parked: ParkedRead = .{ .caller = try pair.newCaller() };
    defer FakeServer.Pair.freeCaller(parked.caller);
    parked.caller.interrupt = pi.interface();
    try parked.start();
    try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = 1 }));

    // The other caller's requests go through while the read is parked; which
    // of the two threads holds the reading baton at any moment does not matter.
    const s = pair.caller;
    var buf: [100]u8 = undefined;
    const t0 = nowMs();
    for (0..50) |_| {
        try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
        const st = try s.stat(1);
        try testing.expectEqualStrings("file", st.name);
    }
    try testing.expect(nowMs() - t0 < 2000);
    try testing.expect(!parked.done.load(.acquire));
    try testing.expectEqual(@as(u32, 0), fs.flushes.load(.seq_cst));

    // Interrupting the parked read sends one Tflush, for the parked tag, and
    // the Rflush ends that read (and only it) in error.Interrupted.
    const hung_tag = fs.hung_tag.load(.seq_cst);
    try pi.inject(21);
    parked.thread.join();
    try testing.expectError(error.Interrupted, parked.result);
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expectEqual(hung_tag, fs.flush_oldtag.load(.seq_cst));

    // Both callers carry on; nothing is left pending.
    try testing.expectEqual(@as(usize, 40), try s.read(1, 10, buf[0..40]));
    try testing.expectEqual(@as(usize, 40), try parked.caller.read(1, 10, buf[0..40]));
    try testing.expectEqual(@as(usize, 0), s.session.client.pending());
}

test "a server that answers and then hangs up: the reply is read, though another call's send failed first" {
    // pardes answers a Restore's write and then cuts every connection. The
    // write's Rwrite is in the socket when another caller's request finds
    // the socket closed; that failed send must not lose the reply.
    const ign = linux.Sigaction{ .handler = .{ .handler = linux.SIG.IGN }, .mask = linux.sigemptyset(), .flags = 0 };
    std.posix.sigaction(.PIPE, &ign, null);
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hangup_after_write = true };
    var pair = try fs.start();
    defer pair.close();
    const s = pair.session;
    const io = lockIo();
    const writer = try pair.newCaller();
    defer FakeServer.Pair.freeCaller(writer);
    const wfid = try pair.caller.clone(1);
    _ = try pair.caller.open(wfid, cloud9.owrite);

    // The write goes out, and nobody reads its answer yet.
    var w: Waiter = .{ .caller = writer };
    s.mutex.lockUncancelable(io);
    s.transmit(&w, .{ .write = .{ .fid = wfid, .offset = 0, .data = "Restore x\n" } }) catch |e| {
        s.leave(&w);
        s.mutex.unlock(io);
        return e;
    };
    s.mutex.unlock(io);
    // The server answers and hangs up; then another call's send fails.
    const Gone = struct {
        fs: *FakeServer,
        fn check(g: @This()) bool {
            return g.fs.gone.load(.acquire);
        }
    };
    try testing.expect(eventually(Gone{ .fs = &fs }));
    try testing.expectError(error.Closed, pair.caller.stat(0));

    // The write's reply was already in: it is read, not failed.
    s.mutex.lockUncancelable(io);
    const got = s.await(&w, .reply);
    s.leave(&w);
    s.mutex.unlock(io);
    try got;
    try testing.expectEqual(@as(u32, "Restore x\n".len), w.result.write);
    // And then the session is over.
    try testing.expectError(error.Closed, pair.caller.stat(0));
}

test "multiplexing: with every tag parked, a call waits for a tag instead of failing" {
    // max_tags reads park (the client's whole request window); the next call
    // finds no tag. Interrupting one parked read sends its Tflush on the tag
    // the client keeps for flushes; the Rflush frees a tag, the waiting stat
    // goes out, and (release_on_stat) the server answers the other reads.
    var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hang_offset = 0, .on_flush = .rflush, .release_on_stat = true };
    var pair = try fs.start();
    defer pair.close();
    // The fake's cloud9.Server holds 64 requests; a window of 32 fills the
    // same way the whole one does.
    const window = 32;
    pair.session.window = window;
    var pi = try PipeInterrupt.init(33);
    defer pi.deinit();
    var reads: [window]ParkedRead = undefined;
    for (&reads) |*r| r.* = .{ .caller = try pair.newCaller() };
    defer for (&reads) |*r| FakeServer.Pair.freeCaller(r.caller);
    reads[0].caller.interrupt = pi.interface();
    for (&reads) |*r| try r.start();
    try testing.expect(eventually(HungAtLeast{ .fs = &fs, .n = window }));

    const Stat = struct {
        caller: *Caller,
        result: Session.Error!u64 = 0,
        done: std.atomic.Value(bool) = .init(false),
        fn run(st: *@This()) void {
            st.result = if (st.caller.stat(1)) |x| x.length else |e| e;
            st.done.store(true, .release);
        }
    };
    var stat: Stat = .{ .caller = pair.caller };
    const th = try std.Thread.spawn(.{}, Stat.run, .{&stat});
    // It is waiting for a tag, not failed.
    const ts: linux.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
    _ = linux.nanosleep(&ts, null);
    try testing.expect(!stat.done.load(.acquire));

    try pi.inject(33);
    th.join();
    try testing.expectEqual(@as(u64, 50), try stat.result);
    for (&reads) |*r| r.thread.join();
    try testing.expectError(error.Interrupted, reads[0].result);
    for (reads[1..]) |r| try testing.expectEqual(@as(usize, 50), try r.result);
    try testing.expectEqual(@as(u32, 1), fs.flushes.load(.seq_cst));
    try testing.expectEqual(@as(usize, 0), pair.session.client.pending());
}