smb-server 0.9.0

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

use std::collections::HashSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};

use crate::proto::header::{SMB2_FLAGS_REPLAY_OPERATION, Smb2Header};
use crate::proto::messages::change_notify::{
    FILE_ACTION_ADDED, FILE_ACTION_ADDED_STREAM, FILE_ACTION_MODIFIED, FILE_ACTION_MODIFIED_STREAM,
    FILE_ACTION_REMOVED_STREAM, FILE_NOTIFY_CHANGE_ATTRIBUTES, FILE_NOTIFY_CHANGE_DIR_NAME,
    FILE_NOTIFY_CHANGE_EA, FILE_NOTIFY_CHANGE_FILE_NAME, FILE_NOTIFY_CHANGE_STREAM_NAME,
};
use crate::proto::messages::{
    AaplQuery, CreateContext, CreateRequest, CreateResponse, Dialect, DurableReconnect,
    DurableReconnectV2, DurableRequest, DurableRequestV2, DurableResponse, DurableResponseV2,
    FileId, LeaseContext, OplockLevel,
};
use tokio::time::Instant;
use tracing::{debug, warn};
use uuid::Uuid;

use crate::access_mask::{
    DELETE, FILE_APPEND_DATA, FILE_EXECUTE, FILE_READ_DATA, FILE_WRITE_ATTRIBUTES, FILE_WRITE_DATA,
    granted_access, maximal_access,
};
use crate::backend::{
    FILE_ATTRIBUTE_ARCHIVE, FILE_ATTRIBUTE_DIRECTORY, FILE_ATTRIBUTE_HIDDEN,
    FILE_ATTRIBUTE_NOT_CONTENT_INDEXED, FILE_ATTRIBUTE_READONLY, FILE_ATTRIBUTE_SYSTEM, FileInfo,
    FileTimes, Handle, OpenAction, OpenIntent, OpenOptions, VALID_SET_ATTRIBUTES,
};
use crate::conn::pending::{Stop, stopped};
use crate::conn::state::{Connection, Open};
use crate::dispatch::HandlerResponse;
use crate::ea::{self, Ea};
use crate::error::SmbError;
use crate::files::{
    BreakWait, CONTENT_CHANGED, Change, Closing, Durability, FileRef, Granted, LeaseId, LeaseRef,
    LeaseRequest, LeaseState, LeaseVersion, MAX_BREAK_WAIT, OpenRequest, OpenSlot, Refused,
    STREAM_BASE_CHANGED, STREAM_CHANGED, Sharing, Trigger, finish_close, v2_timeout,
};
use crate::handlers::aapl;
use crate::handlers::reconnect::{self, Reconnect};
use crate::handlers::shared::{lookup_session, lookup_session_tree};
use crate::handlers::{Outcome, Pending};
use crate::info_class::VOLUME_SERIAL_NUMBER;
use crate::ntstatus;
use crate::path::SmbPath;
use crate::proto::auth::ntlm::Identity;
use crate::server::{ServerState, ShareBindings};
use crate::stream::{
    StreamHandle, StreamName, StreamPart, data_stream_info, parse_create_name, stream_error,
    unsupported,
};
use crate::utils::utf16le_to_units;

// CreateOptions
const FILE_DIRECTORY_FILE: u32 = 0x0000_0001;
const FILE_WRITE_THROUGH: u32 = 0x0000_0002;
pub(crate) const FILE_NO_INTERMEDIATE_BUFFERING: u32 = 0x0000_0008;
const FILE_SYNCHRONOUS_IO_ALERT: u32 = 0x0000_0010;
const FILE_SYNCHRONOUS_IO_NONALERT: u32 = 0x0000_0020;
const FILE_NON_DIRECTORY_FILE: u32 = 0x0000_0040;
const FILE_COMPLETE_IF_OPLOCKED: u32 = 0x0000_0100;
const FILE_NO_EA_KNOWLEDGE: u32 = 0x0000_0200;
const FILE_OPEN_REMOTE_INSTANCE: u32 = 0x0000_0400;
const FILE_DELETE_ON_CLOSE: u32 = 0x0000_1000;
const FILE_OPEN_BY_FILE_ID: u32 = 0x0000_2000;
const FILE_OPEN_FOR_BACKUP_INTENT: u32 = 0x0000_4000;
const FILE_NO_COMPRESSION: u32 = 0x0000_8000;
const FILE_OPEN_REQUIRING_OPLOCK: u32 = 0x0001_0000;
const FILE_DISALLOW_EXCLUSIVE: u32 = 0x0002_0000;
const FILE_RESERVE_OPFILTER: u32 = 0x0010_0000;
const FILE_OPEN_REPARSE_POINT: u32 = 0x0020_0000;
const FILE_OPEN_FOR_FREE_SPACE_QUERY: u32 = 0x0080_0000;

/// CreateOptions allowed with FILE_DIRECTORY_FILE (MS-SMB2 2.2.13), and the
/// flags the server ignores.
const DIRECTORY_OPTIONS: u32 = FILE_DIRECTORY_FILE
    | FILE_WRITE_THROUGH
    | FILE_OPEN_FOR_BACKUP_INTENT
    | FILE_DELETE_ON_CLOSE
    | FILE_OPEN_REPARSE_POINT
    | FILE_NO_COMPRESSION
    | FILE_SYNCHRONOUS_IO_ALERT
    | FILE_SYNCHRONOUS_IO_NONALERT
    | FILE_COMPLETE_IF_OPLOCKED
    | FILE_OPEN_REMOTE_INSTANCE
    | FILE_OPEN_REQUIRING_OPLOCK
    | FILE_DISALLOW_EXCLUSIVE
    | FILE_OPEN_FOR_FREE_SPACE_QUERY;

/// CreateOptions under which every write through the open reaches storage
/// before it completes.
pub(crate) const WRITE_THROUGH_OPTIONS: u32 = FILE_WRITE_THROUGH | FILE_NO_INTERMEDIATE_BUFFERING;

// CreateDisposition
const FILE_SUPERSEDE: u32 = 0x0000_0000;
const FILE_OPEN: u32 = 0x0000_0001;
const FILE_CREATE: u32 = 0x0000_0002;
const FILE_OPEN_IF: u32 = 0x0000_0003;
const FILE_OVERWRITE: u32 = 0x0000_0004;
const FILE_OVERWRITE_IF: u32 = 0x0000_0005;

// CreateAction in response (MS-SMB2 2.2.14)
const FILE_SUPERSEDED: u32 = 0x0000_0000;
pub(super) const FILE_OPENED: u32 = 0x0000_0001;
const FILE_CREATED: u32 = 0x0000_0002;
const FILE_OVERWRITTEN: u32 = 0x0000_0003;

/// Offset of the response create contexts from the start of the SMB2
/// header: 64 + 88.
const RESPONSE_CONTEXTS_OFFSET: u32 = 0x98;

/// Access that makes a version 2 durable open one a replayed CREATE may
/// name (MS-SMB2 3.3.5.9.10, Open.IsReplayEligible).
const REPLAYABLE_ACCESS: u32 =
    FILE_READ_DATA | FILE_EXECUTE | FILE_WRITE_DATA | FILE_APPEND_DATA | DELETE;

/// Caching the server grants in a lease.
const GRANTABLE_LEASE: LeaseState = LeaseState::RWH;

/// Create contexts of a request that the response answers (MS-SMB2
/// 3.3.5.9.5, 3.3.5.9.9) or that fail it. Other contexts are ignored.
pub(super) struct ContextQueries {
    /// QUERY_MAXIMAL_ACCESS, with its timestamp if it has one.
    maximal_access: Option<Option<u64>>,
    query_on_disk_id: bool,
    /// Data of an EA buffer (MS-SMB2 3.3.5.9.1).
    ea_buffer: Option<Vec<u8>>,
    /// A timewarp token, which needs snapshots (MS-SMB2 3.3.5.9.4).
    timewarp: bool,
    /// Data of a lease request.
    lease: Option<Vec<u8>>,
    durable: DurableAsk,
    /// Data of Apple's `AAPL` context.
    aapl: Option<Vec<u8>>,
}

/// The durable handle a CREATE asks for.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum DurableAsk {
    None,
    /// DHnQ (MS-SMB2 3.3.5.9.6).
    V1,
    /// DH2Q, on SMB 3.x (MS-SMB2 3.3.5.9.10).
    V2(DurableRequestV2),
    /// DHnC: reconnect the open named (MS-SMB2 3.3.5.9.7).
    Reconnect(DurableReconnect),
    /// DH2C, on SMB 3.x (MS-SMB2 3.3.5.9.12).
    ReconnectV2(DurableReconnectV2),
}

/// Reads the durable handle contexts of a CREATE on `dialect`. The version
/// 2 contexts count on SMB 3.x only (MS-SMB2 2.2.13.2.11, 2.2.13.2.12). A
/// reconnect ignores a DHnQ next to it (MS-SMB2 3.3.5.9.7); any other two
/// of them together, or one of the wrong size, fail the CREATE with
/// STATUS_INVALID_PARAMETER (MS-SMB2 3.3.5.9.6, 3.3.5.9.7, 3.3.5.9.10).
pub(crate) fn durable_ask(
    contexts: &[CreateContext],
    dialect: Option<Dialect>,
) -> Result<DurableAsk, u32> {
    let v3 = matches!(
        dialect,
        Some(Dialect::Smb300 | Dialect::Smb302 | Dialect::Smb311)
    );
    let find = |name: &[u8; 4], counts: bool| {
        contexts
            .iter()
            .find(|c| counts && c.name == name)
            .map(|c| c.data.as_slice())
    };
    let invalid = |_| ntstatus::STATUS_INVALID_PARAMETER;
    let dhnq = find(CreateContext::NAME_DHNQ, true)
        .map(DurableRequest::parse)
        .transpose()
        .map_err(invalid)?;
    let dhnc = find(CreateContext::NAME_DHNC, true)
        .map(DurableReconnect::parse)
        .transpose()
        .map_err(invalid)?;
    let dh2q = find(CreateContext::NAME_DH2Q, v3)
        .map(DurableRequestV2::parse)
        .transpose()
        .map_err(invalid)?;
    let dh2c = find(CreateContext::NAME_DH2C, v3)
        .map(DurableReconnectV2::parse)
        .transpose()
        .map_err(invalid)?;
    Ok(match (dhnq, dhnc, dh2q, dh2c) {
        (None, None, None, None) => DurableAsk::None,
        (Some(_), None, None, None) => DurableAsk::V1,
        (_, Some(reconnect), None, None) => DurableAsk::Reconnect(reconnect),
        (None, None, Some(request), None) => DurableAsk::V2(request),
        (None, None, None, Some(reconnect)) => DurableAsk::ReconnectV2(reconnect),
        _ => return Err(ntstatus::STATUS_INVALID_PARAMETER),
    })
}

impl ContextQueries {
    /// `None` if a context is malformed or the durable handle contexts do
    /// not go together.
    fn parse(chain: &[u8], dialect: Option<Dialect>) -> Option<Self> {
        let contexts = CreateContext::parse_chain(chain).ok()?;
        let find = |name: &[u8; 4]| contexts.iter().find(|c| c.name == name);
        // The data is empty or one FILETIME (MS-SMB2 2.2.13.2.5).
        let maximal_access = match find(CreateContext::NAME_MXAC) {
            None => None,
            Some(c) if c.data.is_empty() => Some(None),
            Some(c) => Some(Some(u64::from_le_bytes(c.data.as_slice().try_into().ok()?))),
        };
        // Skipped when the request reconnects a durable open.
        let query_on_disk_id = find(CreateContext::NAME_QFID).is_some()
            && find(CreateContext::NAME_DHNC).is_none()
            && find(CreateContext::NAME_DH2C).is_none();
        Some(Self {
            maximal_access,
            query_on_disk_id,
            ea_buffer: find(CreateContext::NAME_EXTA).map(|c| c.data.clone()),
            timewarp: find(CreateContext::NAME_TWRP).is_some(),
            lease: find(CreateContext::NAME_RQLS).map(|c| c.data.clone()),
            durable: durable_ask(&contexts, dialect).ok()?,
            aapl: find(CreateContext::NAME_AAPL).map(|c| c.data.clone()),
        })
    }

    /// Response contexts for an open of a file with `info` on a tree whose
    /// MaximalAccess is `maximal`, on the volume with serial number
    /// `volume_serial`.
    pub(super) fn answer(
        &self,
        info: &FileInfo,
        maximal: u32,
        volume_serial: u32,
    ) -> Vec<CreateContext> {
        let mut out = Vec::new();
        if let Some(timestamp) = self.maximal_access {
            let (status, access) = if timestamp == Some(info.change_time) {
                (ntstatus::STATUS_NONE_MAPPED, 0)
            } else {
                (ntstatus::STATUS_SUCCESS, maximal)
            };
            let mut data = status.to_le_bytes().to_vec();
            data.extend_from_slice(&access.to_le_bytes());
            out.push(CreateContext {
                name: CreateContext::NAME_MXAC.to_vec(),
                data,
            });
        }
        if self.query_on_disk_id {
            // DiskFileId, VolumeId, 16 reserved bytes.
            let mut data = vec![0; 32];
            data[..8].copy_from_slice(&info.file_index.to_le_bytes());
            data[8..16].copy_from_slice(&u64::from(volume_serial).to_le_bytes());
            out.push(CreateContext {
                name: CreateContext::NAME_QFID.to_vec(),
                data,
            });
        }
        out
    }
}

/// The lease a CREATE asks for (MS-SMB2 3.3.5.9, 3.3.5.9.8, 3.3.5.9.11),
/// on a connection from `client` at `dialect`. Leases need SMB 2.1 or
/// later, and the version 2 context SMB 3.x. A request with an all-zero
/// key gets no lease. A version 2 request whose `file_attributes` name a
/// directory asks for no write caching. A context of the wrong size fails
/// the CREATE with STATUS_INVALID_PARAMETER.
fn lease_request(
    data: Option<&[u8]>,
    dialect: Option<Dialect>,
    client: Uuid,
    file_attributes: u32,
) -> Result<Option<LeaseRequest>, u32> {
    let (Some(data), Some(dialect)) = (data, dialect) else {
        return Ok(None);
    };
    if matches!(dialect, Dialect::Smb202 | Dialect::Smb2Wildcard) {
        return Ok(None);
    }
    let ctx = LeaseContext::parse(data).map_err(|_| ntstatus::STATUS_INVALID_PARAMETER)?;
    if (ctx.v2 && dialect == Dialect::Smb210) || ctx.key == [0; 16] {
        return Ok(None);
    }
    let mut state = LeaseState::requested(ctx.state);
    let (version, parent) = if ctx.v2 {
        let parent =
            (ctx.flags & LeaseContext::FLAG_PARENT_LEASE_KEY_SET != 0).then_some(ctx.parent_key);
        if file_attributes & FILE_ATTRIBUTE_DIRECTORY != 0 {
            state = state.without(LeaseState::W);
        }
        (LeaseVersion::V2, parent)
    } else {
        (LeaseVersion::V1, None)
    };
    Ok(Some(LeaseRequest {
        id: LeaseId {
            client,
            key: ctx.key,
        },
        state,
        version,
        epoch: ctx.epoch,
        parent,
    }))
}

/// The response lease context for `granted`, in the lease's version
/// (MS-SMB2 2.2.14.2.10, 2.2.14.2.11).
pub(super) fn lease_response(granted: &Granted) -> CreateContext {
    let v2 = granted.version == LeaseVersion::V2;
    let mut flags = 0;
    if granted.breaking {
        flags |= LeaseContext::FLAG_BREAK_IN_PROGRESS;
    }
    if v2 && granted.parent.is_some() {
        flags |= LeaseContext::FLAG_PARENT_LEASE_KEY_SET;
    }
    let ctx = LeaseContext {
        key: granted.id.key,
        state: granted.state.bits(),
        flags,
        parent_key: granted.parent.unwrap_or_default(),
        epoch: if v2 { granted.epoch } else { 0 },
        v2,
    };
    CreateContext {
        name: CreateContext::NAME_RQLS.to_vec(),
        data: ctx.encode(),
    }
}

/// A validated CREATE request: what each attempt to open needs.
struct CreateArgs {
    path: SmbPath,
    /// The named stream to open.
    stream: Option<StreamName>,
    intent: OpenIntent,
    options: u32,
    create_disposition: u32,
    open_access: u32,
    maximal: u32,
    delete_on_close: bool,
    /// Attributes for a file this CREATE makes or overwrites.
    new_attributes: u32,
    /// Extended attributes for a file this CREATE makes, overwrites or
    /// supersedes, or the status that refuses them there.
    eas: Result<Vec<Ea>, u32>,
    /// The CREATE opens a named stream and carries an `ExtA` context,
    /// which needs the stream's file to exist.
    stream_exta: bool,
    /// FILE_NO_EA_KNOWLEDGE on a share with extended attributes, on an
    /// open of a file's default stream or of a directory.
    no_ea_knowledge: bool,
    opts: OpenOptions,
    sharing: Sharing,
    queries: ContextQueries,
    lease: Option<LeaseRequest>,
    /// The `AAPL` server query to answer.
    aapl: Option<AaplQuery>,
    /// The configured name of the session's user; `None` for anonymous.
    owner: Option<String>,
}

/// What a CREATE asks for.
enum Request {
    Open(CreateArgs),
    Reconnect(Reconnect),
}

impl CreateArgs {
    /// Checks the request against the tree it targets (MS-SMB2 3.3.5.9).
    async fn parse(
        server: &ServerState,
        conn: &Arc<Connection>,
        hdr: &Smb2Header,
        body: &[u8],
    ) -> Result<Request, u32> {
        let req = CreateRequest::parse(body).map_err(|_| ntstatus::STATUS_INVALID_PARAMETER)?;
        let tree_arc = lookup_session_tree(conn, hdr).await?;
        let tree = tree_arc.read().await;
        let tree_access = tree.granted_access;
        let share = Arc::clone(&tree.share);
        drop(tree);
        let owner = match &lookup_session(conn, hdr.session_id)
            .await?
            .read()
            .await
            .identity
        {
            Identity::User { user, .. } => Some(user.clone()),
            Identity::Anonymous => None,
        };
        let dialect = *conn.dialect.read().await;
        let client = *conn.client_guid.read().await;

        let queries = ContextQueries::parse(&req.create_contexts, dialect)
            .ok_or(ntstatus::STATUS_INVALID_PARAMETER)?;
        // A reconnect skips the checks of the name and of the open itself
        // (MS-SMB2 3.3.5.9.7, 3.3.5.9.12).
        let reconnect = match queries.durable {
            DurableAsk::Reconnect(r) => Some((r.file_id, None)),
            DurableAsk::ReconnectV2(r) => Some((r.file_id, Some(r.create_guid))),
            _ => None,
        };
        if let Some((file_id, create_guid)) = reconnect {
            let lease = lease_request(queries.lease.as_deref(), dialect, client, 0)?;
            let name = utf16le_to_units(&req.name)
                .and_then(|u| parse_create_name(&u).ok())
                .and_then(|(path, stream)| match stream {
                    StreamPart::Named(name) => Some((path, Some(name))),
                    StreamPart::NamedDirectory(_) => None,
                    _ => Some((path, None)),
                });
            return Ok(Request::Reconnect(Reconnect {
                file_id,
                create_guid,
                name,
                owner,
                client,
                lease,
                queries,
            }));
        }

        // Decode path.
        let units = utf16le_to_units(&req.name).ok_or(ntstatus::STATUS_INVALID_PARAMETER)?;
        let (path, stream) =
            parse_create_name(&units).map_err(|_| ntstatus::STATUS_OBJECT_NAME_INVALID)?;
        if let StreamPart::Named(name) | StreamPart::NamedDirectory(name) = &stream
            && !share.named_streams
        {
            debug!(path = %path, stream = ?name.as_str(), "share without named streams");
            // A volume with neither refuses the extended attributes first.
            if queries
                .ea_buffer
                .as_ref()
                .is_some_and(|data| !data.is_empty())
                && !share.extended_attributes
            {
                return Err(ntstatus::STATUS_EAS_NOT_SUPPORTED);
            }
            return Err(ntstatus::STATUS_OBJECT_NAME_INVALID);
        }

        // Translate disposition.
        let mut intent = match req.create_disposition {
            FILE_SUPERSEDE | FILE_OVERWRITE_IF => OpenIntent::OverwriteOrCreate,
            FILE_OPEN => OpenIntent::Open,
            FILE_CREATE => OpenIntent::Create,
            FILE_OPEN_IF => OpenIntent::OpenOrCreate,
            FILE_OVERWRITE => OpenIntent::Truncate,
            _ => return Err(ntstatus::STATUS_INVALID_PARAMETER),
        };

        let options = req.create_options;
        if options & (FILE_OPEN_BY_FILE_ID | FILE_RESERVE_OPFILTER) != 0 {
            let on_stream = matches!(stream, StreamPart::Named(_) | StreamPart::NamedDirectory(_));
            return Err(unsupported(on_stream));
        }
        let mut directory = options & FILE_DIRECTORY_FILE != 0;
        let mut non_directory = options & FILE_NON_DIRECTORY_FILE != 0;
        if directory
            && (options & !DIRECTORY_OPTIONS != 0
                || !matches!(
                    intent,
                    OpenIntent::Create | OpenIntent::OpenOrCreate | OpenIntent::Open
                ))
        {
            return Err(ntstatus::STATUS_INVALID_PARAMETER);
        }
        // MS-FSA 2.1.5.1 phase 7, after the option checks of phase 1, which
        // look at FILE_DIRECTORY_FILE only: `::$DATA` opens the data of a
        // file, `::$INDEX_ALLOCATION` a directory, and a named stream the
        // stream of a file or directory. The backend never gets both
        // `directory` and `non_directory`.
        match &stream {
            StreamPart::Data if directory => return Err(ntstatus::STATUS_NOT_A_DIRECTORY),
            StreamPart::Data => non_directory = true,
            StreamPart::Directory => {
                directory = true;
                non_directory = false;
            }
            StreamPart::NamedDirectory(_) => return Err(ntstatus::STATUS_INVALID_PARAMETER),
            StreamPart::Named(_) if directory => return Err(ntstatus::STATUS_NOT_A_DIRECTORY),
            StreamPart::Named(_) | StreamPart::None => {}
        }
        let named = match &stream {
            StreamPart::Named(name) => Some(name.clone()),
            _ => None,
        };

        if queries.timewarp {
            // There are no snapshots, so none matches the token.
            return Err(ntstatus::STATUS_OBJECT_NAME_NOT_FOUND);
        }
        let no_ea_knowledge =
            options & FILE_NO_EA_KNOWLEDGE != 0 && share.extended_attributes && named.is_none();
        let aapl = match (&queries.aapl, &server.config.apple_model) {
            (Some(data), Some(_)) => {
                Some(AaplQuery::parse(data).map_err(|_| ntstatus::STATUS_INVALID_PARAMETER)?)
            }
            _ => None,
        };
        // Shares that turn leases off grant none. A named stream, of a file
        // or a directory, gets its own lease; a directory gets none, but
        // its request still fails on a key held elsewhere.
        let lease = if req.requested_oplock_level == OplockLevel::Lease as u8 && share.leases {
            lease_request(
                queries.lease.as_deref(),
                dialect,
                client,
                req.file_attributes,
            )?
        } else {
            None
        };

        let maximal = maximal_access(tree_access);
        let delete_on_close = options & FILE_DELETE_ON_CLOSE != 0;
        // MS-SMB2 3.3.5.9: the tree must allow DELETE.
        if delete_on_close && maximal & DELETE == 0 {
            warn!(path = %path, "delete-on-close open on read-only tree");
            return Err(ntstatus::STATUS_ACCESS_DENIED);
        }
        if req.desired_access == 0 {
            return Err(ntstatus::STATUS_ACCESS_DENIED);
        }
        let Some(mut open_access) = granted_access(req.desired_access, maximal) else {
            warn!(path = %path, "requested access exceeds tree access");
            return Err(ntstatus::STATUS_ACCESS_DENIED);
        };
        if options & FILE_NO_INTERMEDIATE_BUFFERING != 0 {
            open_access &= !FILE_APPEND_DATA;
        }
        // Disposition that creates: requires write permission.
        if !tree_access.allows_write()
            && matches!(
                intent,
                OpenIntent::Create
                    | OpenIntent::OpenOrCreate
                    | OpenIntent::OverwriteOrCreate
                    | OpenIntent::Truncate
            )
        {
            return Err(ntstatus::STATUS_ACCESS_DENIED);
        }

        let new_attributes = req.file_attributes & VALID_SET_ATTRIBUTES;
        if delete_on_close {
            // MS-FSA 2.1.5.1: the open itself must request DELETE.
            if open_access & DELETE == 0 {
                return Err(ntstatus::STATUS_INVALID_PARAMETER);
            }
            // MS-FSA 2.1.5.1: a file created read-only cannot be deleted.
            if intent != OpenIntent::Open && new_attributes & FILE_ATTRIBUTE_READONLY != 0 {
                return Err(ntstatus::STATUS_CANNOT_DELETE);
            }
        }

        let (eas, stream_exta) = match &queries.ea_buffer {
            Some(data) => create_eas(&share, data, named.is_some(), options)?,
            None => (Ok(Vec::new()), false),
        };

        if stream == StreamPart::Directory
            && matches!(intent, OpenIntent::OverwriteOrCreate | OpenIntent::Truncate)
        {
            intent = overwritten_directory(&share, &path, intent).await?;
        }

        let write = open_access & (FILE_WRITE_DATA | FILE_APPEND_DATA) != 0;
        let opts = OpenOptions {
            read: open_access & FILE_READ_DATA != 0 || !write,
            write,
            write_attributes: open_access & FILE_WRITE_ATTRIBUTES != 0,
            intent,
            directory,
            non_directory,
            delete_on_close,
        };
        Ok(Request::Open(Self {
            path,
            stream: named,
            intent,
            options,
            create_disposition: req.create_disposition,
            open_access,
            maximal,
            delete_on_close,
            new_attributes,
            eas,
            stream_exta,
            no_ea_knowledge,
            opts,
            sharing: Sharing {
                access: open_access,
                share: req.share_access,
            },
            queries,
            lease,
            aapl,
            owner,
        }))
    }
}

/// Where an attempt to open stands.
enum Stage {
    Start,
    /// A sharing violation broke handle caching. Another violation waits
    /// while that break is in progress and fails the CREATE once it is
    /// over (MS-FSA 2.1.5.1.2).
    AfterHandleBreak,
    /// The open is in the file table, waiting for other clients to give up
    /// write caching (MS-FSA 2.1.4.12, OPEN). Staying in the table keeps
    /// its sharing mode in force and keeps those clients from regaining
    /// write caching meanwhile.
    Reserved(Box<Reserved>),
}

/// What a CREATE waiting in the file table holds.
struct Reserved {
    file: FileRef,
    slot: OpenSlot,
    lease_ref: Option<LeaseRef>,
    /// The backend open, when it already ran. An open that truncates runs
    /// it after the wait, so data the clients write back first is not lost
    /// to the truncation.
    opened: Option<(HeldHandle, OpenAction)>,
    /// The open made the file, which has yet to get the extended
    /// attributes of the CREATE; a CREATE that gives up removes it again.
    made_without_eas: bool,
}

/// A backend handle a waiting CREATE keeps. One dropped without
/// [`HeldHandle::take`], when the CREATE's task is cancelled or aborted, is
/// closed on the current runtime.
struct HeldHandle(Option<Box<dyn Handle>>);

impl HeldHandle {
    fn take(mut self) -> Box<dyn Handle> {
        self.0.take().expect("a held handle is taken once")
    }
}

impl Drop for HeldHandle {
    fn drop(&mut self) {
        if let Some(handle) = self.0.take()
            && let Ok(runtime) = tokio::runtime::Handle::try_current()
        {
            runtime.spawn(async move {
                let _ = handle.close().await;
            });
        }
    }
}

impl Stage {
    fn break_handles(&self) -> bool {
        matches!(self, Stage::Start)
    }

    /// Gives up what a waiting CREATE holds, closing its backend handle. A
    /// file it made that still lacks the CREATE's extended attributes goes
    /// again, as on delete-on-close.
    async fn release(self) {
        if let Stage::Reserved(reserved) = self {
            let Reserved {
                file,
                opened,
                made_without_eas,
                ..
            } = *reserved;
            match opened {
                Some((handle, _)) if made_without_eas => {
                    let is_directory = file.is_directory();
                    finish_close(
                        Some(handle.take()),
                        Some(file),
                        true,
                        is_directory,
                        Closing::ReadOnly,
                        false,
                    )
                    .await;
                }
                Some((handle, _)) => {
                    let _ = handle.take().close().await;
                    drop(file);
                }
                None => drop(file),
            }
        }
    }
}

/// How an attempt ended.
enum Step {
    Done(HandlerResponse),
    /// A lease break has to complete first.
    Wait(BreakWait, Stage),
}

pub async fn handle(
    server: &Arc<ServerState>,
    conn: &Arc<Connection>,
    hdr: &Smb2Header,
    body: &[u8],
    retained: usize,
) -> Outcome {
    let args = match CreateArgs::parse(server, conn, hdr, body).await {
        Ok(Request::Open(args)) => args,
        Ok(Request::Reconnect(r)) => {
            return reconnect::handle(server, conn, hdr, r, retained).await;
        }
        Err(status) => return Outcome::Done(HandlerResponse::err(status)),
    };
    // MS-SMB2 3.3.5.9.10: a version 2 request that repeats the CreateGuid of
    // a live durable open of the same client fails unless it is marked as a
    // replay. A request marked as one is not answered from that open; it
    // opens the file anew.
    if let DurableAsk::V2(request) = args.queries.durable
        && hdr.flags & SMB2_FLAGS_REPLAY_OPERATION == 0
    {
        let client = *conn.client_guid.read().await;
        if server
            .durable
            .create_guid_in_use(client, request.create_guid)
        {
            return Outcome::Done(HandlerResponse::err(ntstatus::STATUS_DUPLICATE_OBJECTID));
        }
    }
    let (wait, stage) = match attempt(server, conn, hdr, &args, Stage::Start).await {
        Step::Done(response) => return Outcome::Done(response),
        Step::Wait(wait, stage) => (wait, stage),
    };
    // MS-SMB2 3.3.4.2: past its limits the connection gets no more
    // asynchronous requests.
    let Some((op, mut stop)) = conn.pending.reserve(hdr, None, retained) else {
        warn!("too many waiting requests on the connection");
        stage.release().await;
        return Outcome::Done(HandlerResponse::err(
            ntstatus::STATUS_INSUFFICIENT_RESOURCES,
        ));
    };
    let (server, conn, hdr) = (Arc::clone(server), Arc::clone(conn), *hdr);
    // The wait ends when the breaks complete, which their acknowledgment
    // timers bound. A wait that runs out with nothing changed on the file,
    // or one that outlasts [`MAX_BREAK_WAIT`], fails the CREATE as a sharing
    // violation.
    let work = async move {
        let give_up = Instant::now() + MAX_BREAK_WAIT;
        let mut wait = wait;
        let mut stage = stage;
        loop {
            let changed = tokio::select! {
                biased;
                why = stopped(&mut stop) => {
                    if why == Stop::Cancelled {
                        stage.release().await;
                        return HandlerResponse::err(ntstatus::STATUS_CANCELLED);
                    }
                    true
                }
                changed = wait.changed() => changed,
                () = tokio::time::sleep_until(give_up) => false,
            };
            match attempt(&server, &conn, &hdr, &args, stage).await {
                Step::Done(response) => return response,
                Step::Wait(_, left) if !changed => {
                    left.release().await;
                    return HandlerResponse::err(ntstatus::STATUS_SHARING_VIOLATION);
                }
                Step::Wait(next, left) => {
                    wait = next;
                    stage = left;
                }
            }
        }
    };
    Outcome::Pending(Pending {
        op,
        work: Box::pin(work),
    })
}

/// One try at opening the file. Runs the session and tree checks again, so
/// a CREATE that waited fails once its session or tree is gone.
async fn attempt(
    server: &Arc<ServerState>,
    conn: &Arc<Connection>,
    hdr: &Smb2Header,
    args: &CreateArgs,
    stage: Stage,
) -> Step {
    let fail = |status| Step::Done(HandlerResponse::err(status));
    let tree_arc = match lookup_session_tree(conn, hdr).await {
        Ok(t) => t,
        Err(s) => {
            stage.release().await;
            return fail(s);
        }
    };
    let share = Arc::clone(&tree_arc.read().await.share);
    let backend = Arc::clone(&share.backend);
    let path = &args.path;
    // No rename runs from the first backend call until the open is in the
    // file table under `path`, so the open, and the change it reports,
    // carry the name the file had.
    let names = share.files.names_lock().await;
    if args.stream_exta
        && matches!(stage, Stage::Start)
        && let Err(status) = refuse_stream_exta(&share, path, args.create_disposition).await
    {
        return fail(status);
    }

    let stream = args.stream.as_ref();
    let open_request = |overwrite, break_handles| OpenRequest {
        sharing: args.sharing,
        stream: args.stream.clone(),
        owner: Arc::downgrade(&conn.outbound),
        lease: args.lease.map(|l| l.id),
        overwrite,
        supersede: args.create_disposition == FILE_SUPERSEDE && stream.is_none(),
        break_handles,
    };
    let break_handles = stage.break_handles();
    let refused = |refused: Refused, stage: Stage| match refused.wait {
        Some(wait) => Step::Wait(wait, stage),
        None => fail(refused.status),
    };

    let open_failed = |e: Failed| {
        debug!(error = ?e, path = %path, "backend open failed");
        let case_sensitive = backend.capabilities().case_sensitive;
        if matches!(e, Failed::Backend(SmbError::Exists))
            && share.files.delete_pending_at(path, case_sensitive)
        {
            return fail(ntstatus::STATUS_DELETE_PENDING);
        }
        fail(e.status())
    };

    let (slot, lease_ref, reserved, opened, registered) = match stage {
        Stage::Reserved(reserved) => {
            match reserved.file.end_wait() {
                // Write caching other clients still hold keeps it waiting.
                Ok(Some(wait)) => return Step::Wait(wait, Stage::Reserved(reserved)),
                Ok(None) => {}
                Err(status) => {
                    Stage::Reserved(reserved).release().await;
                    return fail(status);
                }
            }
            let Reserved {
                file,
                slot,
                lease_ref,
                opened,
                ..
            } = *reserved;
            let opened = opened.map(|(handle, action)| (handle.take(), action));
            let registered = opened.is_some();
            (slot, lease_ref, Some(file), opened, registered)
        }
        Stage::Start | Stage::AfterHandleBreak => {
            let limit = server.max_opens_per_connection.load(Ordering::Relaxed);
            let Some(slot) = OpenSlot::acquire(&conn.open_count, limit) else {
                warn!(limit, "open limit reached");
                return fail(ntstatus::STATUS_INSUFFICIENT_RESOURCES);
            };
            let lease_ref = match &args.lease {
                Some(l) => match server.leases.acquire(l.id, &share, path, stream) {
                    Ok(lease_ref) => Some(lease_ref),
                    Err(status) => return fail(status),
                },
                None => None,
            };
            let truncating = matches!(
                args.intent,
                OpenIntent::Truncate | OpenIntent::OverwriteOrCreate
            );
            // An open that truncates checks the file and takes its place
            // in the table first, so a refused open leaves the file as it
            // was, no other open slips in while the backend truncates, and
            // clients caching writes write back before it does. A file
            // that is missing is created instead; one that appears
            // meanwhile is checked again.
            let mut tries = 0;
            let (reserved, opened) = loop {
                if !truncating {
                    break (None, None);
                }
                let request = open_request(true, break_handles);
                let checks = Checks {
                    delete_on_close: args.opts.delete_on_close,
                    no_ea_knowledge: args.no_ea_knowledge,
                    attributes: stream.is_none().then_some(args.new_attributes),
                };
                match reserve_existing(&share, path, stream, args.opts, checks, request).await {
                    Ok(Probe::Reserved(file, Some(wait))) => {
                        let reserved = Reserved {
                            file,
                            slot,
                            lease_ref,
                            opened: None,
                            made_without_eas: false,
                        };
                        return Step::Wait(wait, Stage::Reserved(Box::new(reserved)));
                    }
                    Ok(Probe::Reserved(file, None)) => break (Some(file), None),
                    Ok(Probe::Missing(e)) if args.intent == OpenIntent::Truncate => {
                        return open_failed(e);
                    }
                    Ok(Probe::Missing(_)) => {
                        if stream.is_none()
                            && let Err(status) = &args.eas
                        {
                            return fail(*status);
                        }
                        let create = OpenOptions {
                            intent: OpenIntent::Create,
                            ..args.opts
                        };
                        match open_reported(&share, path, stream, create, false).await {
                            Ok(opened) => break (None, Some(opened)),
                            Err(Failed::Backend(SmbError::Exists)) if tries < 2 => tries += 1,
                            Err(e) => return open_failed(e),
                        }
                    }
                    Err(r) => return refused(r, Stage::AfterHandleBreak),
                }
            };
            (slot, lease_ref, reserved, opened, false)
        }
    };

    // An overwrite or supersede replaces the file's extended attributes
    // before it truncates the file, so a set that fails leaves the file
    // as it was.
    // A directory, which the truncating open refuses, keeps them.
    let mut replaced = None;
    if let (Some(file), None) = (&reserved, &opened)
        && stream.is_none()
        && share.extended_attributes
        && !file.is_directory()
    {
        match replace_eas_of(&share, path, file, args).await {
            Ok(old) => replaced = old,
            Err(status) => return fail(status),
        }
    }
    // A CREATE that would make a file with extended attributes it refuses
    // fails, unless the file is there and the CREATE opens it.
    if let (None, None, Err(status)) = (&reserved, &opened, &args.eas)
        && stream.is_none()
        && matches!(args.intent, OpenIntent::Create | OpenIntent::OpenOrCreate)
        && !exists(&share, path, args.opts).await
    {
        return fail(*status);
    }

    // The open this attempt made, or the one a waiting CREATE kept.
    let (handle, action) = match opened {
        Some(opened) => opened,
        None => match open_reported(&share, path, stream, args.opts, replaced.is_some()).await {
            Ok(opened) => opened,
            Err(e) => {
                // The file stays as it was, its extended attributes too.
                if let (Some(old), Some(file)) = (&replaced, &reserved)
                    && let Err(status) = restore_eas(&share, path, file, &args.eas, old).await
                {
                    return fail(status);
                }
                return open_failed(e);
            }
        },
    };

    // Stat for the response.
    let mut info = match handle.stat().await {
        Ok(i) => i,
        Err(e) => {
            let _ = handle.close().await;
            return fail(e.to_nt_status());
        }
    };
    // MS-FSA 2.1.5.1.2.1: a read-only file cannot be opened for deletion.
    if args.delete_on_close && info.read_only {
        let _ = handle.close().await;
        return fail(ntstatus::STATUS_CANNOT_DELETE);
    }
    if action == OpenAction::Opened
        && args.no_ea_knowledge
        && let Err(status) = refuse_needed_eas(handle.as_ref()).await
    {
        let _ = handle.close().await;
        return fail(status);
    }
    let mut file = match reserved {
        Some(file) if registered || file.is_for(&info, path) => file,
        // The file was replaced between the two opens, or did not exist.
        reserved => {
            drop(reserved);
            let overwrite = action == OpenAction::Overwritten;
            match FileRef::register(&share, &info, path, open_request(overwrite, break_handles)) {
                Ok((file, None)) => file,
                Ok((file, Some(wait))) => {
                    let reserved = Reserved {
                        file,
                        slot,
                        lease_ref,
                        opened: Some((HeldHandle(Some(handle)), action)),
                        made_without_eas: action == OpenAction::Created
                            && stream.is_none()
                            && args.eas.as_ref().is_ok_and(|eas| !eas.is_empty()),
                    };
                    return Step::Wait(wait, Stage::Reserved(Box::new(reserved)));
                }
                Err(r) => {
                    let _ = handle.close().await;
                    return refused(r, Stage::AfterHandleBreak);
                }
            }
        }
    };
    // The streams are marked before another CREATE can open one, and the
    // ones made after the overwrite are left alone.
    let unopened = if action == OpenAction::Overwritten && stream.is_none() && share.named_streams {
        mark_streams(&file, handle.as_ref()).await
    } else {
        Vec::new()
    };
    drop(names);
    file.remove_streams(unopened).await;
    // A new file whose extended attributes cannot be set goes again, as a
    // delete-on-close open of it would, once no other open is on it.
    let new_eas = match &args.eas {
        Ok(eas) if action == OpenAction::Created && stream.is_none() => Ok(eas.as_slice()),
        Err(status) if action == OpenAction::Created && stream.is_none() => Err(*status),
        _ => Ok(&[][..]),
    };
    let made_with_eas = new_eas.is_ok_and(|eas| !eas.is_empty());
    let set = match new_eas {
        Ok(eas) if !eas.is_empty() => {
            let _setting = file.lock_eas().await;
            handle.set_eas(eas).await.map_err(|e| e.to_nt_status())
        }
        Ok(_) => Ok(()),
        Err(status) => Err(status),
    };
    if let Err(status) = set {
        debug!(status, path = %path, "extended attributes of a new file not set");
        let is_directory = info.is_directory;
        finish_close(
            Some(handle),
            Some(file),
            true,
            is_directory,
            Closing::ReadOnly,
            false,
        )
        .await;
        return fail(status);
    }
    let mut granted = None;
    let mut after_send = None;
    if let (Some(lease), Some(lease_ref)) = (args.lease, lease_ref)
        && (stream.is_some() || !info.is_directory)
    {
        match server
            .leases
            .grant(lease_ref, &mut file, &lease, GRANTABLE_LEASE)
        {
            Ok((lease, guard)) => {
                granted = Some(lease);
                after_send = Some(guard);
            }
            Err(status) => {
                drop(file);
                let _ = handle.close().await;
                return fail(status);
            }
        }
    }
    // MS-SMB2 3.3.5.9.6, 3.3.5.9.10: an open asking for a durable handle
    // gets one when its lease caches handles; any other open gets none and
    // no durable context in the response. Anonymous sessions get none,
    // since they all count as one user, and neither does a client without a
    // ClientGuid. The PERSISTENT flag of a version 2 request is ignored:
    // no share is continuously available.
    let durability = match args.queries.durable {
        DurableAsk::V1 => Some(Durability {
            create_guid: None,
            timeout: server.durable.timeout,
            replay_eligible: false,
        }),
        DurableAsk::V2(request) => Some(Durability {
            create_guid: Some(request.create_guid),
            timeout: v2_timeout(request.timeout_ms, server.durable.timeout),
            replay_eligible: args.open_access & REPLAYABLE_ACCESS != 0,
        }),
        _ => None,
    };
    let durable = match (&granted, &args.owner, durability) {
        (Some(lease), Some(owner), Some(durability))
            if lease.state.contains(LeaseState::H)
                && share.durable_handles
                && !lease.id.client.is_nil() =>
        {
            let lease = (lease.id, lease.version);
            server
                .durable
                .register(owner, &share, lease, durability, conn)
                .map(|reg| (reg, durability))
        }
        _ => None,
    };
    if let Some((reg, _)) = &durable {
        file.mark_durable(&server.durable, reg.id());
    }
    // An overwrite that took the file's place in the table before the
    // backend truncated it broke the leases then; one granted since breaks
    // now, so it does not keep the old contents cached.
    if action == OpenAction::Overwritten {
        file.break_leases(Trigger::Open { overwrite: true });
    }

    // A stream created on a file leaves the file's attributes alone.
    if stream.is_none()
        && let Some(wanted) = new_attributes(args, action, &info)
        && wanted != info.attributes() & VALID_SET_ATTRIBUTES
    {
        apply_attributes(handle.as_ref(), wanted, path).await;
        if let Ok(updated) = handle.stat().await {
            info = updated;
        }
    }

    // The on-disk id carries the serial number FileFsVolumeInformation
    // reports.
    let volume_serial = if args.queries.query_on_disk_id {
        backend
            .volume_info()
            .await
            .map_or(VOLUME_SERIAL_NUMBER, |v| v.serial_number)
    } else {
        VOLUME_SERIAL_NUMBER
    };

    // Allocate FileId, register Open. A tree disconnected or revoked while
    // the backend worked takes no new opens.
    let mut tree = tree_arc.write().await;
    if tree.closed {
        drop(tree);
        // The open leaves the table, ending a lease it alone held, before
        // the guard could send a break held back for that lease.
        drop(file);
        drop(after_send);
        let _ = handle.close().await;
        return fail(ntstatus::STATUS_NETWORK_NAME_DELETED);
    }
    let file_id = match &durable {
        Some((reg, _)) => FileId::new(reg.id(), tree.alloc_volatile()),
        None => tree.alloc_file_id(),
    };
    let (durable, durability) = durable.unzip();
    let open = Open {
        file_id,
        handle: Some(handle),
        granted_access: args.open_access,
        is_directory: info.is_directory && stream.is_none(),
        on_stream: stream.is_some(),
        delete_on_close: args.delete_on_close,
        search_state: None,
        fixed_times: FileTimes::default(),
        create_options: args.options,
        file: Some(file),
        slot: Some(slot),
        durable,
        watch: None,
        wrote: Default::default(),
        created: action != OpenAction::Opened,
        marked_deleted: Default::default(),
        ea_cursor: Default::default(),
        eas_changed: AtomicBool::new(made_with_eas),
        resized: AtomicBool::new(stream.is_some() && action == OpenAction::Overwritten),
        moved_data: false,
    };
    let open_arc = Arc::new(tokio::sync::RwLock::new(open));
    tree.opens.get_mut().insert(file_id, open_arc);
    drop(tree);

    let create_action = match action {
        OpenAction::Opened => FILE_OPENED,
        OpenAction::Created => FILE_CREATED,
        OpenAction::Overwritten if args.create_disposition == FILE_SUPERSEDE => FILE_SUPERSEDED,
        OpenAction::Overwritten => FILE_OVERWRITTEN,
    };
    let mut answers = args.queries.answer(&info, args.maximal, volume_serial);
    if let (Some(query), Some(model)) = (&args.aapl, &server.config.apple_model) {
        answers.insert(0, aapl::reply(conn, &share, query, model));
    }
    if let Some(granted) = &granted {
        answers.push(lease_response(granted));
    }
    if let Some(durability) = durability {
        debug!(
            id = file_id.persistent,
            timeout = ?durability.timeout,
            v2 = durability.create_guid.is_some(),
            "durable open granted"
        );
        answers.push(match durability.create_guid {
            None => CreateContext {
                name: CreateContext::NAME_DHNQ.to_vec(),
                data: DurableResponse.encode(),
            },
            Some(_) => CreateContext {
                name: CreateContext::NAME_DH2Q.to_vec(),
                data: DurableResponseV2 {
                    timeout_ms: u32::try_from(durability.timeout.as_millis()).unwrap_or(u32::MAX),
                    flags: 0,
                }
                .encode(),
            },
        });
    }
    let shown = match stream {
        Some(_) => data_stream_info(info),
        None => info,
    };
    let buf = encode_response(&shown, file_id, granted.is_some(), create_action, &answers);
    let mut response = HandlerResponse::ok(buf);
    if let Some(guard) = after_send {
        response.after_send.push(Box::new(guard));
    }
    Step::Done(response)
}

/// Opens `path`, or its named stream `stream`, on the backend with `opts`
/// and tells the watchers of `share` when that created the file or
/// overwrote it (MS-FSA 2.1.5.1.1, 2.1.5.1.2). The report goes out right
/// after the backend call, so a CREATE that then waits for a lease break,
/// or fails, reports the change it made once. A creation is noted before
/// the call, so the backend's own report of it is left out whenever it
/// arrives. An overwrite of a file whose extended attributes the CREATE
/// replaced (`eas_replaced`) reports that with it.
async fn open_reported(
    share: &ShareBindings,
    path: &SmbPath,
    stream: Option<&StreamName>,
    opts: OpenOptions,
    eas_replaced: bool,
) -> Result<(Box<dyn Handle>, OpenAction), Failed> {
    let names = if opts.directory {
        FILE_NOTIFY_CHANGE_DIR_NAME
    } else {
        FILE_NOTIFY_CHANGE_FILE_NAME
    };
    let added = [Change {
        path,
        stream: None,
        action: FILE_ACTION_ADDED,
        filter: names,
        seen: true,
    }];
    let may_create = !matches!(opts.intent, OpenIntent::Open | OpenIntent::Truncate);
    let expected = may_create.then(|| share.watches.expect(&added, None));
    let (created, opened) = backend_open(share, path, stream, opts).await;
    let action = opened.as_ref().ok().map(|(_, action)| *action);
    let heard = share.named_streams && !share.watches.is_empty();
    // An overwrite of the file reports the removal of each of its named
    // streams first, but for those whose deletion was reported already.
    let removed = match &opened {
        Ok((handle, OpenAction::Overwritten)) if stream.is_none() && heard => {
            let case_sensitive = share.backend.capabilities().case_sensitive;
            match (handle.stat().await, handle.streams().await) {
                (Ok(info), Ok(streams)) => streams
                    .into_iter()
                    .map(|s| s.name)
                    .filter(|name| {
                        !share
                            .files
                            .stream_delete_pending(&info, path, name, case_sensitive)
                    })
                    .collect(),
                (Err(e), _) | (_, Err(e)) => {
                    debug!(error = %stream_error(e), "streams of an overwritten file not listed");
                    Vec::new()
                }
            }
        }
        _ => Vec::new(),
    };
    // The first named stream of a directory changes its attributes.
    let first_of_directory = match &opened {
        Ok((handle, OpenAction::Created)) if stream.is_some() && heard => {
            handle.stat().await.is_ok_and(|info| info.is_directory)
                && handle
                    .streams()
                    .await
                    .is_ok_and(|streams| streams.len() == 1)
        }
        _ => false,
    };
    let mut changes: Vec<Change<'_>> = removed
        .iter()
        .map(|name| Change {
            path,
            stream: Some(name),
            action: FILE_ACTION_REMOVED_STREAM,
            filter: FILE_NOTIFY_CHANGE_STREAM_NAME,
            seen: false,
        })
        .collect();
    let modified = |filter| Change {
        path,
        stream: None,
        action: FILE_ACTION_MODIFIED,
        filter,
        seen: true,
    };
    let of_stream = |action, filter| Change {
        path,
        stream,
        action,
        filter,
        seen: false,
    };
    match (action, stream) {
        (Some(OpenAction::Overwritten), None) => {
            let eas = if eas_replaced {
                FILE_NOTIFY_CHANGE_EA
            } else {
                0
            };
            changes.push(modified(CONTENT_CHANGED | eas));
        }
        (Some(OpenAction::Overwritten), Some(_)) => {
            changes.push(modified(STREAM_BASE_CHANGED));
            changes.push(of_stream(FILE_ACTION_MODIFIED_STREAM, STREAM_CHANGED));
        }
        (Some(OpenAction::Created), Some(_)) => {
            if first_of_directory {
                changes.push(modified(FILE_NOTIFY_CHANGE_ATTRIBUTES));
            }
            changes.push(of_stream(
                FILE_ACTION_ADDED_STREAM,
                FILE_NOTIFY_CHANGE_STREAM_NAME,
            ));
        }
        _ => {}
    }
    match expected {
        Some(expected) if created => {
            let mut all = Vec::from(added);
            all.append(&mut changes);
            expected.report(&share.watches, &all);
        }
        _ if !changes.is_empty() => share.watches.report(&changes),
        _ => {}
    }
    opened
}

/// Why a backend open for a CREATE failed.
#[derive(Debug)]
enum Failed {
    Backend(SmbError),
    /// The status the share's opens refuse it with before the backend
    /// opens it.
    Refused(u32),
}

impl From<SmbError> for Failed {
    fn from(e: SmbError) -> Self {
        Self::Backend(e)
    }
}

impl Failed {
    fn status(&self) -> u32 {
        match self {
            Self::Backend(e) => e.to_nt_status(),
            Self::Refused(status) => *status,
        }
    }

    /// No file or directory is at the path, or on the way to it.
    fn is_missing(&self) -> bool {
        matches!(
            self,
            Self::Backend(SmbError::NotFound | SmbError::PathNotFound)
        )
    }
}

/// Opens `path`, or its named stream `stream`, on the backend of `share`
/// with `opts`, and tells whether that created the file. A stream is opened
/// through its file, as MS-FSA 2.1.5.1 finds the File before the Stream:
/// the file is opened for reading, and created when the disposition may
/// create the stream, then the stream through the file's handle. A stream
/// that comes with a new file counts as created; a file made for a stream
/// that then fails to open is removed again. The caller holds the share's
/// names lock.
async fn backend_open(
    share: &ShareBindings,
    path: &SmbPath,
    stream: Option<&StreamName>,
    opts: OpenOptions,
) -> (bool, Result<(Box<dyn Handle>, OpenAction), Failed>) {
    let backend = share.backend.as_ref();
    let Some(stream) = stream else {
        let opened = backend.open(path, opts).await;
        let created = matches!(opened, Ok((_, OpenAction::Created)));
        return (created, opened.map_err(Failed::from));
    };
    let file_opts = OpenOptions {
        read: true,
        write: false,
        write_attributes: false,
        intent: match opts.intent {
            OpenIntent::Open | OpenIntent::Truncate => OpenIntent::Open,
            _ => OpenIntent::OpenOrCreate,
        },
        directory: false,
        non_directory: false,
        delete_on_close: false,
    };
    let (file, action) = match backend.open(path, file_opts).await {
        Ok(opened) => opened,
        Err(e) => return (false, Err(stream_error(e).into())),
    };
    let created = action == OpenAction::Created;
    let case_sensitive = backend.capabilities().case_sensitive;
    let (info, refused) = match file.stat().await {
        Ok(info) => {
            let refused = stream_refusal(share, &info, path, stream, opts);
            (Some(info), refused)
        }
        Err(e) => (None, Some(Failed::from(stream_error(e)))),
    };
    let opened = match refused {
        Some(refused) => {
            if let Err(e) = file.close().await {
                debug!(error = %e, path = %path, "backend close failed");
            }
            Err(refused)
        }
        None => {
            // On a read-only file a stream is not made, also by FILE_OPEN_IF.
            let read_only = info
                .as_ref()
                .is_some_and(|info| info.read_only && !info.is_directory);
            let intent = match opts.intent {
                OpenIntent::OpenOrCreate if read_only => OpenIntent::Open,
                intent => intent,
            };
            let stream_opts = OpenOptions {
                intent,
                directory: false,
                non_directory: false,
                ..opts
            };
            match backend.open_stream(file, path, stream, stream_opts).await {
                Err(SmbError::NotFound) if intent != opts.intent => {
                    Err(Failed::Refused(ntstatus::STATUS_ACCESS_DENIED))
                }
                opened => opened.map_err(|e| stream_error(e).into()),
            }
        }
    };
    match opened {
        Ok((handle, action)) => {
            let handle: Box<dyn Handle> = Box::new(StreamHandle(handle));
            (
                created,
                Ok((handle, if created { OpenAction::Created } else { action })),
            )
        }
        Err(failed) => {
            // A CREATE of the file itself may have opened it meanwhile; the
            // file then stays.
            let opened_elsewhere = info
                .as_ref()
                .is_none_or(|info| share.files.has_opens(info, path, case_sensitive));
            if created
                && !opened_elsewhere
                && let Err(e) = backend.unlink(path).await
            {
                debug!(error = %e, path = %path, "file made for a stream not removed");
            }
            (false, Err(failed))
        }
    }
}

/// Why the share refuses to open the named stream `stream` of the file
/// `info` describes at `path`, with `opts`, before the backend opens it:
/// the file or the stream is marked for deletion (MS-FSA 2.1.5.1), or the
/// file is read-only, which keeps every stream of it from being written
/// or made (MS-FSA 2.1.5.1.2.1).
fn stream_refusal(
    share: &ShareBindings,
    info: &FileInfo,
    path: &SmbPath,
    stream: &StreamName,
    opts: OpenOptions,
) -> Option<Failed> {
    let case_sensitive = share.backend.capabilities().case_sensitive;
    if share
        .files
        .stream_delete_pending(info, path, stream, case_sensitive)
    {
        return Some(Failed::Refused(ntstatus::STATUS_DELETE_PENDING));
    }
    let writes = opts.write || !matches!(opts.intent, OpenIntent::Open | OpenIntent::OpenOrCreate);
    if info.read_only && !info.is_directory && writes {
        return Some(Failed::Refused(ntstatus::STATUS_ACCESS_DENIED));
    }
    None
}

/// Marks the named streams of the file `handle` just overwrote for
/// deletion, and returns those no open is on, which go at once; the others
/// go when the last open on each closes.
async fn mark_streams(file: &FileRef, handle: &dyn Handle) -> Vec<StreamName> {
    match handle.streams().await {
        Ok(streams) => file.delete_streams(streams.into_iter().map(|s| s.name).collect()),
        Err(e) => {
            debug!(error = %stream_error(e), "streams of an overwritten file not listed");
            Vec::new()
        }
    }
}

/// The CREATE response body for an open of a file with `info`, holding a
/// lease when `leased`, with the response contexts `answers`.
pub(super) fn encode_response(
    info: &FileInfo,
    file_id: FileId,
    leased: bool,
    create_action: u32,
    answers: &[CreateContext],
) -> Vec<u8> {
    let mut contexts = Vec::new();
    CreateContext::encode_chain(answers, &mut contexts).expect("encoding into a Vec cannot fail");
    let resp = CreateResponse {
        structure_size: 89,
        oplock_level: if leased {
            OplockLevel::Lease as u8
        } else {
            OplockLevel::None as u8
        },
        flags: 0,
        create_action,
        creation_time: info.creation_time,
        last_access_time: info.last_access_time,
        last_write_time: info.last_write_time,
        change_time: info.change_time,
        allocation_size: info.sizes().1,
        end_of_file: info.sizes().0,
        file_attributes: info.attributes(),
        reserved2: 0,
        file_id,
        create_contexts_offset: if contexts.is_empty() {
            0
        } else {
            RESPONSE_CONTEXTS_OFFSET
        },
        create_contexts_length: contexts.len() as u32,
        create_contexts: contexts,
    };
    let mut buf = Vec::new();
    resp.write_to(&mut buf).expect("encode");
    buf
}

/// What the check before a truncating CREATE found.
enum Probe {
    /// No file at the path; the error the open reported.
    Missing(Failed),
    /// The open is in the table, with a wait when clients caching writes
    /// have to write back first.
    Reserved(FileRef, Option<BreakWait>),
}

/// Before a CREATE that truncates the file at `path`, or its named stream
/// `stream`, opens the existing file or stream without truncating it,
/// checks it against delete-on-close and the opens already on it, and
/// enters the new open in the share's table. That open asks for reading,
/// and for the access the CREATE was granted when reading is refused. Only
/// a missing file or stream passes without an entry; any other failure
/// fails the CREATE, as the truncating open would.
async fn reserve_existing(
    share: &Arc<ShareBindings>,
    path: &SmbPath,
    stream: Option<&StreamName>,
    opts: OpenOptions,
    checks: Checks,
    req: OpenRequest,
) -> Result<Probe, Refused> {
    let probe = OpenOptions {
        intent: OpenIntent::Open,
        delete_on_close: false,
        ..opts
    };
    let read_only = OpenOptions {
        read: true,
        write: false,
        write_attributes: false,
        ..probe
    };
    let mut opened = backend_open(share, path, stream, read_only).await.1;
    if matches!(opened, Err(ref e) if !e.is_missing()) && probe.write {
        opened = backend_open(share, path, stream, probe).await.1;
    }
    let handle = match opened {
        Ok((handle, _)) => handle,
        Err(e) if e.is_missing() => return Ok(Probe::Missing(e)),
        Err(e) => return Err(e.status().into()),
    };
    let dropped = |info: &FileInfo, asked: u32| {
        info.attributes() & (FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM) & !asked != 0
    };
    let result = match handle.stat().await {
        Ok(info) if checks.attributes.is_some_and(|asked| dropped(&info, asked)) => {
            Err(ntstatus::STATUS_ACCESS_DENIED.into())
        }
        Ok(info) if checks.delete_on_close && info.read_only => {
            Err(ntstatus::STATUS_CANNOT_DELETE.into())
        }
        Ok(info) => {
            let needed = if checks.no_ea_knowledge {
                refuse_needed_eas(handle.as_ref()).await
            } else {
                Ok(())
            };
            needed
                .map_err(Refused::from)
                .and_then(|()| FileRef::register(share, &info, path, req))
                .map(|(file, wait)| Probe::Reserved(file, wait))
        }
        Err(e) => Err(e.to_nt_status().into()),
    };
    let _ = handle.close().await;
    result
}

/// What [`reserve_existing`] checks of the existing file before it takes
/// its place in the table.
#[derive(Clone, Copy)]
struct Checks {
    /// The open asks for delete-on-close, which a read-only file refuses.
    delete_on_close: bool,
    /// The open asks for FILE_NO_EA_KNOWLEDGE.
    no_ea_knowledge: bool,
    /// The attributes of an open that overwrites or supersedes the file
    /// itself, which has to ask for HIDDEN and SYSTEM again where the file
    /// has them (MS-FSA 2.1.5.1.2).
    attributes: Option<u32>,
}

/// STATUS_ACCESS_DENIED for a file that has an extended attribute with
/// FILE_NEED_EA, which an open with FILE_NO_EA_KNOWLEDGE cannot open
/// (MS-FSA 2.1.5.1.2). A file whose attributes cannot be read needs none.
async fn refuse_needed_eas(handle: &dyn Handle) -> Result<(), u32> {
    match handle.eas().await {
        Ok(eas) if eas.iter().any(|ea| ea.needed) => Err(ntstatus::STATUS_ACCESS_DENIED),
        Ok(_) => Ok(()),
        Err(e) => {
            debug!(error = %e, "extended attributes not read for FILE_NO_EA_KNOWLEDGE");
            Ok(())
        }
    }
}

/// The extended attributes of an `ExtA` create context with `data`
/// (MS-SMB2 2.2.13.2.1, 3.3.5.9.1), for a file the CREATE makes,
/// overwrites or supersedes, or the status that refuses them there, and
/// whether the CREATE opens a named stream with them. Empty data carries
/// none. A share without extended attributes fails the CREATE with
/// STATUS_EAS_NOT_SUPPORTED, an open with FILE_NO_EA_KNOWLEDGE with
/// STATUS_ACCESS_DENIED (MS-SMB2 2.2.13), and a malformed list with
/// STATUS_EA_LIST_INCONSISTENT. Names, flags and the size limit count only
/// where the attributes apply. They belong to the file, so an open of one
/// of its named streams leaves them out.
fn create_eas(
    share: &ShareBindings,
    data: &[u8],
    on_stream: bool,
    options: u32,
) -> Result<(Result<Vec<Ea>, u32>, bool), u32> {
    if data.is_empty() {
        return Ok((Ok(Vec::new()), false));
    }
    if !share.extended_attributes {
        return Err(ntstatus::STATUS_EAS_NOT_SUPPORTED);
    }
    if options & FILE_NO_EA_KNOWLEDGE != 0 {
        return Err(ntstatus::STATUS_ACCESS_DENIED);
    }
    ea::check_full_list(data)?;
    if on_stream {
        return Ok((Ok(Vec::new()), true));
    }
    let eas = ea::parse_full_list(data).and_then(|mut eas| {
        ea::dedup(&mut eas);
        eas.retain(|ea| !ea.value.is_empty());
        if ea::stored_length(&eas) > ea::MAX_STORED_LENGTH {
            return Err(ntstatus::STATUS_EA_TOO_LARGE);
        }
        Ok(eas)
    });
    Ok((eas, false))
}

/// Refuses an open of a named stream that carries an `ExtA` context:
/// with STATUS_ACCESS_DENIED when the stream's file is missing, and with
/// STATUS_INVALID_PARAMETER when the open would overwrite the stream.
async fn refuse_stream_exta(
    share: &ShareBindings,
    path: &SmbPath,
    disposition: u32,
) -> Result<(), u32> {
    match share.backend.open(path, OpenOptions::default()).await {
        Ok((file, _)) => {
            let _ = file.close().await;
        }
        Err(SmbError::NotFound) => return Err(ntstatus::STATUS_ACCESS_DENIED),
        Err(e) => return Err(stream_error(e).to_nt_status()),
    }
    if matches!(
        disposition,
        FILE_SUPERSEDE | FILE_OVERWRITE | FILE_OVERWRITE_IF
    ) {
        return Err(ntstatus::STATUS_INVALID_PARAMETER);
    }
    Ok(())
}

/// Whether a file is at `path`, for a CREATE that would make one with
/// extended attributes it refuses: then it fails, and makes nothing.
async fn exists(share: &ShareBindings, path: &SmbPath, opts: OpenOptions) -> bool {
    let probe = OpenOptions {
        read: true,
        write: false,
        write_attributes: false,
        intent: OpenIntent::Open,
        delete_on_close: false,
        ..opts
    };
    match share.backend.open(path, probe).await {
        Ok((file, _)) => {
            let _ = file.close().await;
            true
        }
        Err(e) => !matches!(e, SmbError::NotFound),
    }
}

/// Gives the existing file at `path`, which `file` holds the place of in
/// the table, the extended attributes `args` carries in place of its own,
/// as an overwrite or supersede does. Returns the attributes it had when
/// that changed any.
async fn replace_eas_of(
    share: &ShareBindings,
    path: &SmbPath,
    file: &FileRef,
    args: &CreateArgs,
) -> Result<Option<Vec<Ea>>, u32> {
    let eas = args.eas.as_deref().map_err(|&status| status)?;
    let opts = OpenOptions {
        read: true,
        write: false,
        write_attributes: false,
        intent: OpenIntent::Open,
        delete_on_close: false,
        ..args.opts
    };
    let (handle, _) = share
        .backend
        .open(path, opts)
        .await
        .map_err(|e| e.to_nt_status())?;
    let setting = file.lock_eas().await;
    let replaced = replace_eas(handle.as_ref(), eas).await;
    drop(setting);
    let _ = handle.close().await;
    if matches!(replaced, Ok(Some(_))) {
        file.count_ea_change();
    }
    replaced
}

/// Puts back `old`, the extended attributes the file at `path` had before
/// [`replace_eas_of`] gave it `new`, when the overwrite then failed. A
/// failure to put them back fails the CREATE with an I/O error.
async fn restore_eas(
    share: &ShareBindings,
    path: &SmbPath,
    file: &FileRef,
    new: &Result<Vec<Ea>, u32>,
    old: &[Ea],
) -> Result<(), u32> {
    let new = new.as_deref().unwrap_or_default();
    let restored = async {
        let (handle, _) = share.backend.open(path, OpenOptions::default()).await?;
        let setting = file.lock_eas().await;
        let kept: HashSet<String> = old.iter().map(|ea| ea::name_key(&ea.name)).collect();
        let mut changes: Vec<Ea> = new
            .iter()
            .filter(|ea| !kept.contains(&ea::name_key(&ea.name)))
            .map(|ea| Ea::new(ea.name.as_str(), Vec::new()))
            .collect();
        changes.extend_from_slice(old);
        let result = handle.set_eas(&changes).await;
        drop(setting);
        let _ = handle.close().await;
        result
    };
    match restored.await {
        Ok(()) => {
            file.count_ea_change();
            Ok(())
        }
        Err(e) => {
            warn!(error = %e, path = %path, "extended attributes of a file not put back");
            Err(ntstatus::STATUS_UNEXPECTED_IO_ERROR)
        }
    }
}

/// Replaces the extended attributes of the file `handle` is open on with
/// `eas`. Returns the attributes it had when that changed any.
async fn replace_eas(handle: &dyn Handle, eas: &[Ea]) -> Result<Option<Vec<Ea>>, u32> {
    let new: HashSet<String> = eas.iter().map(|ea| ea::name_key(&ea.name)).collect();
    let old: Vec<Ea> = handle
        .eas()
        .await
        .map_err(|e| e.to_nt_status())?
        .into_iter()
        .filter(|old| ea::valid_name(old.name.as_bytes()))
        .collect();
    let mut changes: Vec<Ea> = old
        .iter()
        .filter(|old| !new.contains(&ea::name_key(&old.name)))
        .map(|old| Ea::new(old.name.as_str(), Vec::new()))
        .collect();
    changes.extend_from_slice(eas);
    if changes.is_empty() {
        return Ok(None);
    }
    handle
        .set_eas(&changes)
        .await
        .map_err(|e| e.to_nt_status())?;
    Ok(Some(old))
}

/// What an overwrite or supersede of `path` as a directory stream
/// (`::$INDEX_ALLOCATION`) does: an existing directory cannot be
/// overwritten, the share root with STATUS_ACCESS_DENIED and any other with
/// STATUS_OBJECT_NAME_COLLISION (MS-FSA 2.1.5.1.2), a file is not a
/// directory (MS-FSA 2.1.5.1 phase 7), and a missing directory is created
/// unless `intent` is [`OpenIntent::Truncate`] (MS-FSA 2.1.5.1 phase 6).
async fn overwritten_directory(
    share: &ShareBindings,
    path: &SmbPath,
    intent: OpenIntent,
) -> Result<OpenIntent, u32> {
    if path.is_root() {
        return Err(ntstatus::STATUS_ACCESS_DENIED);
    }
    let (handle, _) = match share.backend.open(path, OpenOptions::default()).await {
        Ok(opened) => opened,
        Err(SmbError::NotFound) if intent == OpenIntent::OverwriteOrCreate => {
            return Ok(OpenIntent::Create);
        }
        Err(e) => return Err(e.to_nt_status()),
    };
    let info = handle.stat().await;
    let _ = handle.close().await;
    match info {
        Ok(info) if info.is_directory => Err(ntstatus::STATUS_OBJECT_NAME_COLLISION),
        Ok(_) => Err(ntstatus::STATUS_NOT_A_DIRECTORY),
        Err(e) => Err(e.to_nt_status()),
    }
}

/// The attributes the file or directory `info` describes takes from a
/// CREATE that made or overwrote it with `action`: a new file or
/// directory, or a superseded file, has those the CREATE asks
/// for, and an overwritten file adds them to its own. A file also gets
/// ARCHIVE, and NOT_CONTENT_INDEXED in the request is ignored. `None` when
/// nothing is to be set: an open, and a new entry the CREATE asks no
/// attributes for.
fn new_attributes(args: &CreateArgs, action: OpenAction, info: &FileInfo) -> Option<u32> {
    let asked = if info.is_directory {
        args.new_attributes
    } else {
        (args.new_attributes & !FILE_ATTRIBUTE_NOT_CONTENT_INDEXED) | FILE_ATTRIBUTE_ARCHIVE
    };
    match action {
        OpenAction::Opened => None,
        OpenAction::Created if args.new_attributes == 0 => None,
        OpenAction::Overwritten if args.create_disposition != FILE_SUPERSEDE => {
            Some((info.attributes() & VALID_SET_ATTRIBUTES) | asked)
        }
        _ => Some(asked),
    }
}

/// Gives a file this CREATE made or overwrote the requested attributes. A
/// backend that cannot store all of them gets READONLY alone, when asked
/// for; the CREATE succeeds either way.
async fn apply_attributes(handle: &dyn Handle, attributes: u32, path: &SmbPath) {
    let mut result = handle.set_attributes(attributes).await;
    if matches!(result, Err(SmbError::NotSupported))
        && attributes & FILE_ATTRIBUTE_READONLY != 0
        && attributes != FILE_ATTRIBUTE_READONLY
    {
        result = handle.set_attributes(FILE_ATTRIBUTE_READONLY).await;
    }
    if let Err(e) = result {
        debug!(error = %e, path = %path, attributes, "create attributes not applied");
    }
}

#[cfg(test)]
mod tests {
    use async_trait::async_trait;
    use tokio::sync::{Notify, RwLock, Semaphore};

    use super::*;
    use crate::backend::{BackendCapabilities, ShareBackend};
    use crate::builder::Access;
    use crate::conn::state::{Session, TreeConnect};
    use crate::error::SmbResult;
    use crate::proto::auth::ntlm::Identity;
    use crate::proto::header::{Command, HeaderTail};
    use crate::tests::memfs::MemFsBackend;
    use crate::{Share, SmbServer};

    /// Memory backend whose truncating opens tell `waiting` they have
    /// arrived and then wait for a permit from `go`.
    struct HoldTruncate {
        inner: MemFsBackend,
        waiting: Arc<Notify>,
        go: Arc<Semaphore>,
    }

    #[async_trait]
    impl ShareBackend for HoldTruncate {
        async fn open(
            &self,
            path: &SmbPath,
            opts: OpenOptions,
        ) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
            if opts.intent == OpenIntent::Truncate {
                self.waiting.notify_one();
                self.go.acquire().await.expect("gate").forget();
            }
            self.inner.open(path, opts).await
        }

        async fn unlink(&self, path: &SmbPath) -> SmbResult<()> {
            self.inner.unlink(path).await
        }

        async fn rename(&self, from: &SmbPath, to: &SmbPath, replace: bool) -> SmbResult<()> {
            self.inner.rename(from, to, replace).await
        }

        fn capabilities(&self) -> BackendCapabilities {
            self.inner.capabilities()
        }
    }

    fn create_body(name: &str, disposition: u32) -> Vec<u8> {
        let name: Vec<u8> = name.encode_utf16().flat_map(u16::to_le_bytes).collect();
        let req = CreateRequest {
            structure_size: 57,
            security_flags: 0,
            requested_oplock_level: 0,
            impersonation_level: 2,
            smb_create_flags: 0,
            reserved: 0,
            desired_access: FILE_READ_DATA | FILE_WRITE_DATA,
            file_attributes: 0,
            share_access: 7,
            create_disposition: disposition,
            create_options: 0,
            name_offset: 0x78,
            name_length: name.len() as u16,
            create_contexts_offset: 0,
            create_contexts_length: 0,
            name,
            create_contexts: Vec::new(),
        };
        let mut body = Vec::new();
        req.write_to(&mut body)
            .expect("encoding into a Vec cannot fail");
        body
    }

    #[tokio::test]
    async fn an_overwrite_of_a_hidden_or_system_file_has_to_ask_for_those_attributes() {
        use crate::tests::peer::{Peer, status};
        let cases = [
            (FILE_ATTRIBUTE_HIDDEN, 0, ntstatus::STATUS_ACCESS_DENIED),
            (
                FILE_ATTRIBUTE_HIDDEN,
                FILE_ATTRIBUTE_HIDDEN,
                ntstatus::STATUS_SUCCESS,
            ),
            (
                FILE_ATTRIBUTE_SYSTEM,
                FILE_ATTRIBUTE_HIDDEN,
                ntstatus::STATUS_ACCESS_DENIED,
            ),
            (
                FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
                FILE_ATTRIBUTE_SYSTEM,
                ntstatus::STATUS_ACCESS_DENIED,
            ),
            (
                FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
                FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM,
                ntstatus::STATUS_SUCCESS,
            ),
        ];
        for (has, asked, expected) in cases {
            for disposition in [FILE_OVERWRITE, FILE_OVERWRITE_IF, FILE_SUPERSEDE] {
                let files = MemFsBackend::new()
                    .with_file("f.txt", b"precious")
                    .with_attributes("f.txt", has);
                let state = SmbServer::builder()
                    .listen("127.0.0.1:0".parse().unwrap())
                    .share(Share::new("home", files.clone()).public())
                    .build()
                    .unwrap()
                    .state();
                let mut peer = Peer::new(&state, false).await;
                let mut body = create_body("f.txt", disposition);
                body[28..32].copy_from_slice(&asked.to_le_bytes());
                let (_, reply) = peer.request(&state, Command::Create, &body).await;
                let case = format!("{has:#x} asked {asked:#x} disposition {disposition}");
                assert_eq!(status(&reply), expected, "{case}");
                let left = files.contents("f.txt").unwrap();
                let kept = expected != ntstatus::STATUS_SUCCESS;
                assert_eq!(left == b"precious", kept, "{case}");
            }
        }
    }

    #[test]
    fn a_version_2_request_for_a_directory_asks_for_no_write_caching() {
        let context = |v2| {
            LeaseContext {
                key: [1; 16],
                state: 0x7,
                v2,
                ..LeaseContext::default()
            }
            .encode()
        };
        let state = |v2, attributes| {
            lease_request(
                Some(&context(v2)),
                Some(Dialect::Smb311),
                Uuid::nil(),
                attributes,
            )
            .unwrap()
            .unwrap()
            .state
        };
        assert_eq!(state(true, FILE_ATTRIBUTE_DIRECTORY), LeaseState::RH);
        assert_eq!(state(true, 0), LeaseState::RWH);
        assert_eq!(state(false, FILE_ATTRIBUTE_DIRECTORY), LeaseState::RWH);
    }

    fn ctx(name: &[u8; 4], len: usize) -> CreateContext {
        CreateContext {
            name: name.to_vec(),
            data: vec![0; len],
        }
    }

    #[test]
    fn durable_contexts_are_read_by_dialect_and_combination() {
        use DurableAsk as A;
        let q = || ctx(CreateContext::NAME_DHNQ, 16);
        let c = || ctx(CreateContext::NAME_DHNC, 16);
        let q2 = || ctx(CreateContext::NAME_DH2Q, 32);
        let c2 = || ctx(CreateContext::NAME_DH2C, 36);
        let invalid = Err(ntstatus::STATUS_INVALID_PARAMETER);
        let reconnect = Ok(A::Reconnect(DurableReconnect::default()));
        let reconnect_v2 = Ok(A::ReconnectV2(DurableReconnectV2::default()));
        for dialect in [
            Dialect::Smb202,
            Dialect::Smb210,
            Dialect::Smb300,
            Dialect::Smb302,
            Dialect::Smb311,
        ] {
            let v3 = !matches!(dialect, Dialect::Smb202 | Dialect::Smb210);
            let ask = |list: Vec<CreateContext>| durable_ask(&list, Some(dialect));
            assert_eq!(ask(vec![]), Ok(A::None));
            assert_eq!(ask(vec![q()]), Ok(A::V1));
            assert_eq!(ask(vec![c()]), reconnect);
            assert_eq!(ask(vec![c(), q()]), reconnect, "{dialect:?}");
            assert_eq!(ask(vec![ctx(CreateContext::NAME_DHNQ, 15)]), invalid);
            assert_eq!(ask(vec![ctx(CreateContext::NAME_DHNC, 17)]), invalid);
            if v3 {
                assert_eq!(ask(vec![q2()]), Ok(A::V2(DurableRequestV2::default())));
                assert_eq!(ask(vec![c2()]), reconnect_v2);
                for pair in [
                    vec![c(), q2()],
                    vec![c(), c2()],
                    vec![c2(), q()],
                    vec![c2(), q2()],
                    vec![q2(), q()],
                    vec![ctx(CreateContext::NAME_DH2Q, 31)],
                    vec![ctx(CreateContext::NAME_DH2C, 35)],
                ] {
                    assert_eq!(ask(pair), invalid, "{dialect:?}");
                }
            } else {
                // The version 2 contexts do not exist before SMB 3.x.
                assert_eq!(ask(vec![q2()]), Ok(A::None));
                assert_eq!(ask(vec![c2(), q()]), Ok(A::V1));
                assert_eq!(ask(vec![c(), ctx(CreateContext::NAME_DH2Q, 31)]), reconnect);
            }
        }
    }

    #[tokio::test]
    async fn a_truncating_create_holds_the_file_while_the_backend_truncates() {
        let waiting = Arc::new(Notify::new());
        let go = Arc::new(Semaphore::new(0));
        let backend = HoldTruncate {
            inner: MemFsBackend::new().with_file("f.txt", b"precious"),
            waiting: Arc::clone(&waiting),
            go: Arc::clone(&go),
        };
        let server = SmbServer::builder()
            .listen("127.0.0.1:0".parse().unwrap())
            .share(Share::new("s", backend).public())
            .build()
            .unwrap();
        let state = server.state();
        let share = state.find_share("s").await.unwrap();
        let conn = Arc::new(Connection::new(
            state.config.server_guid,
            state.config.max_read_size,
            state.config.max_write_size,
        ));
        let session = Session::new(Identity::Anonymous, [0; 16], [0; 16], false, None);
        let tree = TreeConnect::new(1, Arc::clone(&share), Access::ReadWrite);
        session
            .trees
            .write()
            .await
            .insert(1, Arc::new(RwLock::new(tree)));
        conn.sessions
            .write()
            .await
            .insert(1, Arc::new(RwLock::new(session)));

        let hdr = Smb2Header {
            command: Command::Create,
            tail: HeaderTail::sync(1),
            session_id: 1,
            ..Smb2Header::default()
        };
        let body = create_body("f.txt", FILE_OVERWRITE);
        let truncating = tokio::spawn({
            let state = Arc::clone(&state);
            let conn = Arc::clone(&conn);
            async move {
                match handle(&state, &conn, &hdr, &body, 0).await {
                    Outcome::Done(response) => response.status,
                    Outcome::Pending(_) => panic!("the CREATE waits"),
                }
            }
        });
        waiting.notified().await;

        // Another connection's exclusive open arrives while the backend
        // truncates.
        let path: SmbPath = "f.txt".parse().unwrap();
        let (probe, _) = share
            .backend
            .open(&path, OpenOptions::default())
            .await
            .unwrap();
        let info = probe.stat().await.unwrap();
        let exclusive = FileRef::register(
            &share,
            &info,
            &path,
            Sharing {
                access: FILE_READ_DATA,
                share: 0,
            }
            .into(),
        );
        assert_eq!(
            exclusive.err().map(|r| r.status),
            Some(ntstatus::STATUS_SHARING_VIOLATION)
        );

        go.add_permits(1);
        assert_eq!(truncating.await.unwrap(), ntstatus::STATUS_SUCCESS);
        assert_eq!(probe.stat().await.unwrap().end_of_file, 0);
    }
    /// A memory backend that claims named streams and opens none.
    struct NoStreams(MemFsBackend);

    #[async_trait]
    impl ShareBackend for NoStreams {
        async fn open(
            &self,
            path: &SmbPath,
            opts: OpenOptions,
        ) -> SmbResult<(Box<dyn Handle>, OpenAction)> {
            self.0.open(path, opts).await
        }

        async fn unlink(&self, path: &SmbPath) -> SmbResult<()> {
            self.0.unlink(path).await
        }

        async fn rename(&self, from: &SmbPath, to: &SmbPath, replace: bool) -> SmbResult<()> {
            self.0.rename(from, to, replace).await
        }

        fn capabilities(&self) -> BackendCapabilities {
            self.0.capabilities()
        }
    }

    #[tokio::test]
    async fn a_file_made_for_a_failed_stream_stays_while_it_is_open() {
        let share = crate::server::ShareBindings::new(
            "s".to_string(),
            Arc::new(NoStreams(MemFsBackend::new())),
            crate::server::ShareMode::Public,
            std::collections::HashMap::new(),
            false,
            true,
            true,
        );
        let path: SmbPath = "new".parse().unwrap();
        let stream: StreamName = "s".parse().unwrap();
        let opts = OpenOptions {
            intent: OpenIntent::OpenOrCreate,
            ..OpenOptions::default()
        };
        let exists = || async {
            share
                .backend
                .open(&path, OpenOptions::default())
                .await
                .is_ok()
        };

        let (created, opened) = backend_open(&share, &path, Some(&stream), opts).await;
        assert!(!created);
        assert_eq!(
            opened.err().map(|e| e.status()),
            Some(ntstatus::STATUS_ACCESS_DENIED)
        );
        assert!(!exists().await, "taken back");

        // Another CREATE has the new file open by the time the stream fails.
        let sharing = Sharing {
            access: FILE_READ_DATA,
            share: 0x7,
        };
        let other = FileRef::register(&share, &FileInfo::new("new"), &path, sharing.into())
            .unwrap()
            .0;
        let (created, opened) = backend_open(&share, &path, Some(&stream), opts).await;
        assert!(!created && opened.is_err());
        assert!(exists().await, "left to the other open");
        drop(other);
    }
}