summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_cmn_oplock.c
blob: bee55e2e604a6a7e3325eabaafd36ed2614c72cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2020 Nexenta by DDN, Inc.  All rights reserved.
 * Copyright 2022 RackTop Systems, Inc.
 */

/*
 * (SMB1/SMB2) common (FS-level) Oplock support.
 *
 * This is the file-system (FS) level oplock code.  This level
 * knows about the rules by which various kinds of oplocks may
 * coexist and how they interact.  Note that this code should
 * have NO knowledge of specific SMB protocol details.  Those
 * details are handled in smb_srv_oplock.c and related.
 *
 * This file is intentionally written to very closely follow the
 * [MS-FSA] specification sections about oplocks.  Almost every
 * section of code is preceeded by a block of text from that
 * specification describing the logic.  Where the implementation
 * differs from what the spec. describes, there are notes like:
 * Implementation specific: ...
 */

#include <smbsrv/smb_kproto.h>
#include <smbsrv/smb_oplock.h>

/*
 * Several short-hand defines and enums used in this file.
 */

#define	NODE_FLAGS_DELETING	(NODE_FLAGS_DELETE_ON_CLOSE |\
				NODE_FLAGS_DELETE_COMMITTED)

static uint32_t
smb_oplock_req_excl(
    smb_ofile_t *ofile,		/* in: the "Open" */
    uint32_t *rop);		/* in: "RequestedOplock", out:NewOplockLevel */

static uint32_t
smb_oplock_req_shared(
    smb_ofile_t *ofile,		/* the "Open" */
    uint32_t *rop,		/* in: "RequestedOplock", out:NewOplockLevel */
    boolean_t GrantingInAck);

static uint32_t smb_oplock_break_cmn(smb_node_t *node,
    smb_ofile_t *ofile, uint32_t BreakCacheLevel);


/*
 * [MS-FSA] 2.1.4.12.2 Algorithm to Compare Oplock Keys
 *
 * The inputs for this algorithm are:
 *
 *	OperationOpen: The Open used in the request that can
 *	  cause an oplock to break.
 *	OplockOpen: The Open originally used to request the oplock,
 *	  as specified in section 2.1.5.17.
 *	Flags: If unspecified it is considered to contain 0.
 *	  Valid nonzero values are:
 *		PARENT_OBJECT
 *
 * This algorithm returns TRUE if the appropriate oplock key field of
 * OperationOpen equals OplockOpen.TargetOplockKey, and FALSE otherwise.
 *
 * Note: Unlike many comparison functions, ARG ORDER MATTERS.
 */

static boolean_t
CompareOplockKeys(smb_ofile_t *OperOpen, smb_ofile_t *OplockOpen, int flags)
{
	static const uint8_t key0[SMB_LEASE_KEY_SZ] = { 0 };

	/*
	 * When we're called via FEM, (smb_oplock_break_...)
	 * the OperOpen arg is NULL because I/O outside of SMB
	 * doesn't have an "ofile".  That's "not a match".
	 */
	if (OperOpen == NULL)
		return (B_FALSE);
	ASSERT(OplockOpen != NULL);

	/*
	 * If OperationOpen equals OplockOpen:
	 * Return TRUE.
	 */
	if (OperOpen == OplockOpen)
		return (B_TRUE);

	/*
	 * If both OperationOpen.TargetOplockKey and
	 * OperationOpen.ParentOplockKey are empty
	 * or both OplockOpen.TargetOplockKey and
	 * OplockOpen.ParentOplockKey are empty:
	 * Return FALSE.
	 */
	if (bcmp(OperOpen->TargetOplockKey, key0, sizeof (key0)) == 0 &&
	    bcmp(OperOpen->ParentOplockKey, key0, sizeof (key0)) == 0)
		return (B_FALSE);
	if (bcmp(OplockOpen->TargetOplockKey, key0, sizeof (key0)) == 0 &&
	    bcmp(OplockOpen->ParentOplockKey, key0, sizeof (key0)) == 0)
		return (B_FALSE);

	/*
	 * If OplockOpen.TargetOplockKey is empty or...
	 */
	if (bcmp(OplockOpen->TargetOplockKey, key0, sizeof (key0)) == 0)
		return (B_FALSE);

	/*
	 * If Flags contains PARENT_OBJECT:
	 */
	if ((flags & PARENT_OBJECT) != 0) {
		/*
		 * If OperationOpen.ParentOplockKey is empty:
		 * Return FALSE.
		 */
		if (bcmp(OperOpen->ParentOplockKey, key0, sizeof (key0)) == 0)
			return (B_FALSE);

		/*
		 * If OperationOpen.ParentOplockKey equals
		 * OplockOpen.TargetOplockKey:
		 * return TRUE, else FALSE
		 */
		if (bcmp(OperOpen->ParentOplockKey,
		    OplockOpen->TargetOplockKey,
		    SMB_LEASE_KEY_SZ) == 0) {
			return (B_TRUE);
		}
	} else {
		/*
		 * ... from above:
		 * (Flags does not contain PARENT_OBJECT and
		 * OperationOpen.TargetOplockKey is empty):
		 * Return FALSE.
		 */
		if (bcmp(OperOpen->TargetOplockKey, key0, sizeof (key0)) == 0)
			return (B_FALSE);

		/*
		 * If OperationOpen.TargetOplockKey equals
		 * OplockOpen.TargetOplockKey:
		 *  Return TRUE, else FALSE
		 */
		if (bcmp(OperOpen->TargetOplockKey,
		    OplockOpen->TargetOplockKey,
		    SMB_LEASE_KEY_SZ) == 0) {
			return (B_TRUE);
		}
	}

	return (B_FALSE);
}

/*
 * 2.1.4.13 Algorithm to Recompute the State of a Shared Oplock
 *
 * The inputs for this algorithm are:
 *	ThisOplock: The Oplock on whose state is being recomputed.
 */
static void
RecomputeOplockState(smb_node_t *node)
{
	smb_oplock_t *ol = &node->n_oplock;

	ASSERT(RW_READ_HELD(&node->n_ofile_list.ll_lock));
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * If ThisOplock.IIOplocks, ThisOplock.ROplocks, ThisOplock.RHOplocks,
	 * and ThisOplock.RHBreakQueue are all empty:
	 *	Set ThisOplock.State to NO_OPLOCK.
	 */
	if (ol->cnt_II == 0 && ol->cnt_R == 0 &&
	    ol->cnt_RH == 0 && ol->cnt_RHBQ == 0) {
		ol->ol_state = NO_OPLOCK;
		return;
	}

	/*
	 * Else If ThisOplock.ROplocks is not empty and either
	 *    ThisOplock.RHOplocks or ThisOplock.RHBreakQueue are not empty:
	 *	Set ThisOplock.State to
	 *	  (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH).
	 */
	else if (ol->cnt_R != 0 && (ol->cnt_RH != 0 || ol->cnt_RHBQ != 0)) {
		ol->ol_state = (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH);
	}

	/*
	 * Else If ThisOplock.ROplocks is empty and
	 * ThisOplock.RHOplocks is not empty:
	 *	Set ThisOplock.State to (READ_CACHING|HANDLE_CACHING).
	 */
	else if (ol->cnt_R == 0 && ol->cnt_RH != 0) {
		ol->ol_state = (READ_CACHING|HANDLE_CACHING);
	}

	/*
	 * Else If ThisOplock.ROplocks is not empty and
	 * ThisOplock.IIOplocks is not empty:
	 *	Set ThisOplock.State to (READ_CACHING|LEVEL_TWO_OPLOCK).
	 */
	else if (ol->cnt_R != 0 && ol->cnt_II != 0) {
		ol->ol_state = (READ_CACHING|LEVEL_TWO_OPLOCK);
	}

	/*
	 * Else If ThisOplock.ROplocks is not empty and
	 * ThisOplock.IIOplocks is empty:
	 *	Set ThisOplock.State to READ_CACHING.
	 */
	else if (ol->cnt_R != 0 && ol->cnt_II == 0) {
		ol->ol_state = READ_CACHING;
	}

	/*
	 * Else If ThisOplock.ROplocks is empty and
	 * ThisOplock.IIOplocks is not empty:
	 *	Set ThisOplock.State to LEVEL_TWO_OPLOCK.
	 */
	else if (ol->cnt_R == 0 && ol->cnt_II != 0) {
		ol->ol_state = LEVEL_TWO_OPLOCK;
	}

	else {
		smb_ofile_t *o;
		int cntBrkToRead;

		/*
		 * ThisOplock.RHBreakQueue MUST be non-empty by this point.
		 */
		ASSERT(ol->cnt_RHBQ != 0);

		/*
		 * How many on RHBQ have BreakingToRead set?
		 */
		cntBrkToRead = 0;
		FOREACH_NODE_OFILE(node, o) {
			if (o->f_oplock.onlist_RHBQ == 0)
				continue;
			if (o->f_oplock.BreakingToRead)
				cntBrkToRead++;
		}

		/*
		 * If RHOpContext.BreakingToRead is TRUE for
		 *  every RHOpContext on ThisOplock.RHBreakQueue:
		 */
		if (cntBrkToRead == ol->cnt_RHBQ) {
			/*
			 * Set ThisOplock.State to
			 * (READ_CACHING|HANDLE_CACHING|BREAK_TO_READ_CACHING).
			 */
			ol->ol_state = (READ_CACHING|HANDLE_CACHING|
			    BREAK_TO_READ_CACHING);
		}

		/*
		 * Else If RHOpContext.BreakingToRead is FALSE for
		 *  every RHOpContext on ThisOplock.RHBreakQueue:
		 */
		else if (cntBrkToRead == 0) {
			/*
			 * Set ThisOplock.State to
			 *  (READ_CACHING|HANDLE_CACHING|BREAK_TO_NO_CACHING).
			 */
			ol->ol_state = (READ_CACHING|HANDLE_CACHING|
			    BREAK_TO_NO_CACHING);
		} else {
			/*
			 * Set ThisOplock.State to
			 *  (READ_CACHING|HANDLE_CACHING).
			 */
			ol->ol_state = (READ_CACHING|HANDLE_CACHING);
		}
	}
}

/*
 * [MS-FSA] 2.1.5.17 Server Requests an Oplock
 *
 * The server (caller) provides:
 *	Open - The Open on which the oplock is being requested. (ofile)
 *	Type - The type of oplock being requested. Valid values are as follows:
 *		LEVEL_TWO (Corresponds to SMB2_OPLOCK_LEVEL_II)
 *		LEVEL_ONE (Corresponds to SMB2_OPLOCK_LEVEL_EXCLUSIVE)
 *		LEVEL_BATCH (Corresponds to SMB2_OPLOCK_LEVEL_BATCH)
 *		LEVEL_GRANULAR (Corresponds to SMB2_OPLOCK_LEVEL_LEASE)
 *	RequestedOplockLevel - A combination of zero or more of the
 *	  following flags (ignored if Type != LEVEL_GRANULAR)
 *		READ_CACHING
 *		HANDLE_CACHING
 *		WRITE_CACHING
 *
 *	(Type + RequestedOplockLevel come in *statep)
 *
 * Returns:
 *	*statep = NewOplockLevel (possibly less than requested)
 *		  containing: LEVEL_NONE, LEVEL_TWO + cache_flags
 *	NTSTATUS
 */

uint32_t
smb_oplock_request(smb_request_t *sr, smb_ofile_t *ofile, uint32_t *statep)
{
	smb_node_t *node = ofile->f_node;
	uint32_t type = *statep & OPLOCK_LEVEL_TYPE_MASK;
	uint32_t level = *statep & OPLOCK_LEVEL_CACHE_MASK;
	uint32_t status;

	*statep = LEVEL_NONE;

	/*
	 * If Open.Stream.StreamType is DirectoryStream:
	 *	The operation MUST be failed with STATUS_INVALID_PARAMETER
	 *	under either of the following conditions:
	 *	* Type is not LEVEL_GRANULAR.
	 *	* Type is LEVEL_GRANULAR but RequestedOplockLevel is
	 *	  neither READ_CACHING nor (READ_CACHING|HANDLE_CACHING).
	 */
	if (!smb_node_is_file(node)) {
		/* ofile is a directory. */
		if (type != LEVEL_GRANULAR)
			return (NT_STATUS_INVALID_PARAMETER);
		if (level != READ_CACHING &&
		    level != (READ_CACHING|HANDLE_CACHING))
			return (NT_STATUS_INVALID_PARAMETER);
		/*
		 * We're not supporting directory leases yet.
		 * Todo.
		 */
		return (NT_STATUS_OPLOCK_NOT_GRANTED);
	}

	smb_llist_enter(&node->n_ofile_list, RW_READER);
	mutex_enter(&node->n_oplock.ol_mutex);

	/*
	 * If Type is LEVEL_ONE or LEVEL_BATCH:
	 * The operation MUST be failed with STATUS_OPLOCK_NOT_GRANTED
	 * under either of the following conditions:
	 *	Open.File.OpenList contains more than one Open
	 *	  whose Stream is the same as Open.Stream.
	 *	Open.Mode contains either FILE_SYNCHRONOUS_IO_ALERT or
	 *	  FILE_SYNCHRONOUS_IO_NONALERT.
	 * Request an exclusive oplock according to the algorithm in
	 * section 2.1.5.17.1, setting the algorithm's params as follows:
	 *	Pass in the current Open.
	 *	RequestedOplock = Type.
	 * The operation MUST at this point return any status code
	 * returned by the exclusive oplock request algorithm.
	 */
	if (type == LEVEL_ONE || type == LEVEL_BATCH) {
		if (node->n_open_count > 1) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		/* XXX: Should be a flag on the ofile. */
		if (node->flags & NODE_FLAGS_WRITE_THROUGH) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		*statep = type;
		status = smb_oplock_req_excl(ofile, statep);
		goto out;
	}

	/*
	 * Else If Type is LEVEL_TWO:
	 * The operation MUST be failed with STATUS_OPLOCK_NOT_GRANTED under
	 *  either of the following conditions:
	 *	Open.Stream.ByteRangeLockList is not empty.
	 *	Open.Mode contains either FILE_SYNCHRONOUS_IO_ALERT or
	 *	  FILE_SYNCHRONOUS_IO_NONALERT.
	 * Request a shared oplock according to the algorithm in
	 * section 2.1.5.17.2, setting the algorithm's parameters as follows:
	 *	Pass in the current Open.
	 *	RequestedOplock = Type.
	 *	GrantingInAck = FALSE.
	 * The operation MUST at this point return any status code
	 * returned by the shared oplock request algorithm.
	 */
	if (type == LEVEL_TWO) {
		if (smb_lock_range_access(sr, node, 0, ~0, B_FALSE) != 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		/* XXX: Should be a flag on the ofile. */
		if (node->flags & NODE_FLAGS_WRITE_THROUGH) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		*statep = type;
		status = smb_oplock_req_shared(ofile, statep, B_FALSE);
		goto out;
	}

	/*
	 * Else If Type is LEVEL_GRANULAR:
	 *   Sub-cases on RequestedOplockLevel (our "level")
	 *
	 * This is the last Type, so error on !granular and then
	 * deal with the cache levels using one less indent.
	 */
	if (type != LEVEL_GRANULAR) {
		status = NT_STATUS_INVALID_PARAMETER;
		goto out;
	}

	switch (level) {

	/*
	 * If RequestedOplockLevel is READ_CACHING or
	 *   (READ_CACHING|HANDLE_CACHING):
	 *	The operation MUST be failed with STATUS_OPLOCK_NOT_GRANTED
	 *	under either of the following conditions:
	 *		Open.Stream.ByteRangeLockList is not empty.
	 *		Open.Mode contains either FILE_SYNCHRONOUS_IO_ALERT or
	 *		  FILE_SYNCHRONOUS_IO_NONALERT.
	 *	Request a shared oplock according to the algorithm in
	 *	section 2.1.5.17.2, setting the parameters as follows:
	 *		Pass in the current Open.
	 *		RequestedOplock = RequestedOplockLevel.
	 *		GrantingInAck = FALSE.
	 *
	 *	The operation MUST at this point return any status code
	 *	  returned by the shared oplock request algorithm.
	 */
	case READ_CACHING:
	case (READ_CACHING|HANDLE_CACHING):
		if (smb_lock_range_access(sr, node, 0, ~0, B_FALSE) != 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		/* XXX: Should be a flag on the ofile. */
		if (node->flags & NODE_FLAGS_WRITE_THROUGH) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		*statep = level;
		status = smb_oplock_req_shared(ofile, statep, B_FALSE);
		break;

	/*
	 * Else If RequestedOplockLevel is
	 * (READ_CACHING|WRITE_CACHING) or
	 * (READ_CACHING|WRITE_CACHING|HANDLE_CACHING):
	 * If Open.Mode contains either FILE_SYNCHRONOUS_IO_ALERT or
	 * FILE_SYNCHRONOUS_IO_NONALERT, the operation MUST be failed
	 * with STATUS_OPLOCK_NOT_GRANTED.
	 * Request an exclusive oplock according to the algorithm in
	 * section 2.1.5.17.1, setting the parameters as follows:
	 *	Pass in the current Open.
	 *	RequestedOplock = RequestedOplockLevel.
	 * The operation MUST at this point return any status code
	 * returned by the exclusive oplock request algorithm.
	 */
	case (READ_CACHING | WRITE_CACHING):
	case (READ_CACHING | WRITE_CACHING | HANDLE_CACHING):
		/* XXX: Should be a flag on the ofile. */
		if (node->flags & NODE_FLAGS_WRITE_THROUGH) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}
		*statep = level;
		status = smb_oplock_req_excl(ofile, statep);
		break;

	/*
	 * Else if RequestedOplockLevel is 0 (that is, no flags):
	 * The operation MUST return STATUS_SUCCESS at this point.
	 */
	case 0:
		*statep = 0;
		status = NT_STATUS_SUCCESS;
		break;

	/*
	 * Else
	 *  The operation MUST be failed with STATUS_INVALID_PARAMETER.
	 */
	default:
		status = NT_STATUS_INVALID_PARAMETER;
		break;
	}

	/* Give caller back the "Granular" bit. */
	if (status == NT_STATUS_SUCCESS) {
		*statep |= LEVEL_GRANULAR;

		/*
		 * The oplock lease may have moved to this ofile. Update.
		 * Minor violation of layering here (leases vs oplocks)
		 * but we want this update coverd by the oplock mutex.
		 */
#ifndef	TESTJIG
		if (ofile->f_lease != NULL)
			ofile->f_lease->ls_oplock_ofile = ofile;
#endif
	}

out:
	mutex_exit(&node->n_oplock.ol_mutex);
	smb_llist_exit(&node->n_ofile_list);

	return (status);
}

/*
 * 2.1.5.17.1 Algorithm to Request an Exclusive Oplock
 *
 * The inputs for requesting an exclusive oplock are:
 *	Open: The Open on which the oplock is being requested.
 *	RequestedOplock: The oplock type being requested. One of:
 *	  LEVEL_ONE, LEVEL_BATCH, CACHE_RW, CACHE_RWH
 *
 * On completion, the object store MUST return:
 *	Status: An NTSTATUS code that specifies the result.
 *	NewOplockLevel: The type of oplock that the requested oplock has been
 *	  broken (reduced) to.  If a failure status is returned in Status,
 *	  the value of this field is undefined.  Valid values are as follows:
 *		LEVEL_NONE (that is, no oplock)
 *		LEVEL_TWO
 *		A combination of one or more of the following flags:
 *			READ_CACHING
 *			HANDLE_CACHING
 *			WRITE_CACHING
 *	AcknowledgeRequired: A Boolean value: TRUE if the server MUST
 *	acknowledge the oplock break; FALSE if not, as specified in
 *	section 2.1.5.18. If a failure status is returned in Status,
 *	the value of this field is undefined.
 *
 * Note: Stores NewOplockLevel in *rop
 */
static uint32_t
smb_oplock_req_excl(
    smb_ofile_t *ofile,		/* in: the "Open" */
    uint32_t *rop)		/* in: "RequestedOplock", out:NewOplockLevel */
{
	smb_node_t *node = ofile->f_node;
	smb_ofile_t *o;
	boolean_t GrantExcl = B_FALSE;
	uint32_t status = NT_STATUS_OPLOCK_NOT_GRANTED;

	ASSERT(RW_READ_HELD(&node->n_ofile_list.ll_lock));
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * Don't allow grants on closing ofiles.
	 */
	if (ofile->f_oplock_closing)
		return (status);

	/*
	 * If Open.Stream.Oplock is empty:
	 *   Build a new Oplock object with fields initialized as follows:
	 *	Oplock.State set to NO_OPLOCK.
	 *	All other fields set to 0/empty.
	 *   Store the new Oplock object in Open.Stream.Oplock.
	 * EndIf
	 *
	 * Implementation specific:
	 * Open.Stream.Oplock maps to: node->n_oplock
	 */
	if (node->n_oplock.ol_state == 0) {
		node->n_oplock.ol_state = NO_OPLOCK;
	}

	/*
	 * If Open.Stream.Oplock.State contains
	 * LEVEL_TWO_OPLOCK or NO_OPLOCK: ...
	 *
	 * Per ms, this is the "If" matching the unbalalanced
	 * "Else If" below (for which we requested clarification).
	 */
	if ((node->n_oplock.ol_state & (LEVEL_TWO | NO_OPLOCK)) != 0) {

		/*
		 * If Open.Stream.Oplock.State contains LEVEL_TWO_OPLOCK and
		 * RequestedOplock contains one or more of READ_CACHING,
		 * HANDLE_CACHING, or WRITE_CACHING, the operation MUST be
		 * failed with Status set to STATUS_OPLOCK_NOT_GRANTED.
		 */
		if ((node->n_oplock.ol_state & LEVEL_TWO) != 0 &&
		    (*rop & CACHE_RWH) != 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}

		/*
		 * [ from dochelp@ms ]
		 *
		 * By this point if there is a level II oplock present,
		 * the caller can only be requesting an old-style oplock
		 * because we rejected enhanced oplock requests above.
		 * If the caller is requesting an old-style oplock our
		 * caller already verfied that there is only one handle
		 * open to this stream, and we've already verified that
		 * this request is for a legacy oplock, meaning that there
		 * can be at most one level II oplock (and no R oplocks),
		 * and the level II oplock belongs to this handle.  Clear
		 * the level II oplock and grant the exclusive oplock.
		 */

		/*
		 * If Open.Stream.Oplock.State is equal to LEVEL_TWO_OPLOCK:
		 * Remove the first Open ThisOpen from
		 *  Open.Stream.Oplock.IIOplocks (there is supposed to be
		 * exactly one present), and notify the server of an
		 * oplock break according to the algorithm in section
		 *  2.1.5.17.3, setting the algorithm's parameters as follows:
		 *	BreakingOplockOpen = ThisOpen.
		 *	NewOplockLevel = LEVEL_NONE.
		 *	AcknowledgeRequired = FALSE.
		 *	OplockCompletionStatus = STATUS_SUCCESS.
		 * (The operation does not end at this point; this call
		 *  to 2.1.5.17.3 completes some earlier call to 2.1.5.17.2.)
		 *
		 * Implementation specific:
		 *
		 * As explained above, the passed in ofile should be the
		 * only open file on this node.  Out of caution, we'll
		 * walk the ofile list as usual here, making sure there
		 * are no LevelII oplocks remaining, as those may not
		 * coexist with the exclusive oplock were're creating
		 * in this call.  Also, if the passed in ofile has a
		 * LevelII oplock, don't do an "ind break" up call on
		 * this ofile, as that would just cause an immediate
		 * "break to none" of the oplock we'll grant here.
		 * If there were other ofiles with LevelII oplocks,
		 * it would be appropriate to "ind break" those.
		 */
		if ((node->n_oplock.ol_state & LEVEL_TWO) != 0) {
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_II == 0)
					continue;
				o->f_oplock.onlist_II = B_FALSE;
				node->n_oplock.cnt_II--;
				ASSERT(node->n_oplock.cnt_II >= 0);
				if (o == ofile)
					continue;
				DTRACE_PROBE1(unexpected, smb_ofile_t *, o);
				smb_oplock_ind_break(o,
				    LEVEL_NONE, B_FALSE,
				    NT_STATUS_SUCCESS);
			}
		}

		/*
		 * Note the spec. had an extra "EndIf" here.
		 * Confirmed by dochelp@ms
		 */

		/*
		 * If Open.File.OpenList contains more than one Open whose
		 * Stream is the same as Open.Stream, and NO_OPLOCK is present
		 * in Open.Stream.Oplock.State, the operation MUST be failed
		 * with Status set to STATUS_OPLOCK_NOT_GRANTED.
		 *
		 * Implementation specific:
		 * Allow other opens if they have the same lease ours,
		 * so we can upgrade RH to RWH (for example). Therefore
		 * only count opens with a different TargetOplockKey.
		 * Also ignore "attribute-only" opens.
		 */
		if ((node->n_oplock.ol_state & NO_OPLOCK) != 0) {
			FOREACH_NODE_OFILE(node, o) {
				if (!smb_ofile_is_open(o))
					continue;
				if ((o->f_granted_access & FILE_DATA_ALL) == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, 0)) {
					status = NT_STATUS_OPLOCK_NOT_GRANTED;
					goto out;
				}
			}
		}

		/*
		 * If Open.Stream.IsDeleted is TRUE and RequestedOplock
		 * contains HANDLE_CACHING, the operation MUST be failed
		 * with Status set to STATUS_OPLOCK_NOT_GRANTED.
		 */
		if (((node->flags & NODE_FLAGS_DELETING) != 0) &&
		    (*rop & HANDLE_CACHING) != 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}

		/* Set GrantExclusiveOplock to TRUE. */
		GrantExcl = B_TRUE;
	}

	/*
	 * "Else" If (Open.Stream.Oplock.State contains one or more of
	 * READ_CACHING, WRITE_CACHING, or HANDLE_CACHING) and
	 * (Open.Stream.Oplock.State contains none of (BREAK_ANY)) and
	 * (Open.Stream.Oplock.RHBreakQueue is empty):
	 */
	else if ((node->n_oplock.ol_state & CACHE_RWH) != 0 &&
	    (node->n_oplock.ol_state & BREAK_ANY) == 0 &&
	    node->n_oplock.cnt_RHBQ == 0) {

		/*
		 * This is a granular oplock and it is not breaking.
		 */

		/*
		 * If RequestedOplock contains none of READ_CACHING,
		 * WRITE_CACHING, or HANDLE_CACHING, the operation
		 * MUST be failed with Status set to
		 * STATUS_OPLOCK_NOT_GRANTED.
		 */
		if ((*rop & CACHE_RWH) == 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}

		/*
		 * If Open.Stream.IsDeleted (already checked above)
		 */

		/*
		 * Switch (Open.Stream.Oplock.State):
		 */
		switch (node->n_oplock.ol_state) {

		case CACHE_R:
			/*
			 * If RequestedOplock is neither
			 * (READ_CACHING|WRITE_CACHING) nor
			 * (READ_CACHING|WRITE_CACHING|HANDLE_CACHING),
			 * the operation MUST be failed with Status set
			 * to STATUS_OPLOCK_NOT_GRANTED.
			 */
			if (*rop != CACHE_RW && *rop != CACHE_RWH) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}

			/*
			 * For each Open ThisOpen in
			 *  Open.Stream.Oplock.ROplocks:
			 *	If ThisOpen.TargetOplockKey !=
			 *	Open.TargetOplockKey, the operation
			 *	MUST be failed with Status set to
			 *	STATUS_OPLOCK_NOT_GRANTED.
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_R == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, 0)) {
					status = NT_STATUS_OPLOCK_NOT_GRANTED;
					goto out;
				}
			}

			/*
			 * For each Open o in Open.Stream.Oplock.ROplocks:
			 *	Remove o from Open.Stream.Oplock.ROplocks.
			 *	Notify the server of an oplock break
			 *	according to the algorithm in section
			 *	2.1.5.17.3, setting the algorithm's
			 *	parameters as follows:
			 *		BreakingOplockOpen = o.
			 *		NewOplockLevel = RequestedOplock.
			 *		AcknowledgeRequired = FALSE.
			 *		OplockCompletionStatus =
			 *		  STATUS_OPLOCK_SWITCHED_TO_NEW_HANDLE.
			 *	(The operation does not end at this point;
			 *	 this call to 2.1.5.17.3 completes some
			 *	 earlier call to 2.1.5.17.2.)
			 * EndFor
			 *
			 * Note: Upgrade to excl. on same lease.
			 * Won't send a break for this.
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_R == 0)
					continue;
				o->f_oplock.onlist_R = B_FALSE;
				node->n_oplock.cnt_R--;
				ASSERT(node->n_oplock.cnt_R >= 0);

				smb_oplock_ind_break(o, *rop,
				    B_FALSE, STATUS_NEW_HANDLE);
			}
			/*
			 * Set GrantExclusiveOplock to TRUE.
			 * EndCase // _R
			 */
			GrantExcl = B_TRUE;
			break;

		case CACHE_RH:
			/*
			 * If RequestedOplock is not
			 * (READ_CACHING|WRITE_CACHING|HANDLE_CACHING)
			 * or Open.Stream.Oplock.RHBreakQueue is not empty,
			 * the operation MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 * Note: Have RHBreakQueue==0 from above.
			 */
			if (*rop != CACHE_RWH) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}

			/*
			 * For each Open ThisOpen in
			 *  Open.Stream.Oplock.RHOplocks:
			 *	If ThisOpen.TargetOplockKey !=
			 *	Open.TargetOplockKey, the operation
			 *	MUST be failed with Status set to
			 *	STATUS_OPLOCK_NOT_GRANTED.
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RH == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, 0)) {
					status = NT_STATUS_OPLOCK_NOT_GRANTED;
					goto out;
				}
			}

			/*
			 * For each Open o in Open.Stream.Oplock.RHOplocks:
			 *	Remove o from Open.Stream.Oplock.RHOplocks.
			 *	Notify the server of an oplock break
			 *	according to the algorithm in section
			 *	2.1.5.17.3, setting the algorithm's
			 *	parameters as follows:
			 *		BreakingOplockOpen = o.
			 *		NewOplockLevel = RequestedOplock.
			 *		AcknowledgeRequired = FALSE.
			 *		OplockCompletionStatus =
			 *		  STATUS_OPLOCK_SWITCHED_TO_NEW_HANDLE.
			 *	(The operation does not end at this point;
			 *	 this call to 2.1.5.17.3 completes some
			 *	 earlier call to 2.1.5.17.2.)
			 * EndFor
			 *
			 * Note: Upgrade to excl. on same lease.
			 * Won't send a break for this.
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RH == 0)
					continue;
				o->f_oplock.onlist_RH = B_FALSE;
				node->n_oplock.cnt_RH--;
				ASSERT(node->n_oplock.cnt_RH >= 0);

				smb_oplock_ind_break(o, *rop,
				    B_FALSE, STATUS_NEW_HANDLE);
			}
			/*
			 * Set GrantExclusiveOplock to TRUE.
			 * EndCase // _RH
			 */
			GrantExcl = B_TRUE;
			break;

		case (CACHE_RWH | EXCLUSIVE):
			/*
			 * If RequestedOplock is not
			 * (READ_CACHING|WRITE_CACHING|HANDLE_CACHING),
			 * the operation MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 */
			if (*rop != CACHE_RWH) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}
			/* Deliberate FALL-THROUGH to next Case statement. */
			/* FALLTHROUGH */

		case (CACHE_RW | EXCLUSIVE):
			/*
			 * If RequestedOplock is neither
			 * (READ_CACHING|WRITE_CACHING|HANDLE_CACHING) nor
			 * (READ_CACHING|WRITE_CACHING), the operation MUST be
			 * failed with Status set to STATUS_OPLOCK_NOT_GRANTED.
			 */
			if (*rop != CACHE_RWH && *rop != CACHE_RW) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}

			o = node->n_oplock.excl_open;
			if (o == NULL) {
				ASSERT(0);
				GrantExcl = B_TRUE;
				break;
			}

			/*
			 * If Open.TargetOplockKey !=
			 * Open.Stream.Oplock.ExclusiveOpen.TargetOplockKey,
			 * the operation MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 */
			if (!CompareOplockKeys(ofile, o, 0)) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}

			/*
			 * Notify the server of an oplock break according to
			 * the algorithm in section 2.1.5.17.3, setting the
			 * algorithm's parameters as follows:
			 *	BreakingOplockOpen =
			 *	  Open.Stream.Oplock.ExclusiveOpen.
			 *	NewOplockLevel = RequestedOplock.
			 *	AcknowledgeRequired = FALSE.
			 *	OplockCompletionStatus =
			 *	  STATUS_OPLOCK_SWITCHED_TO_NEW_HANDLE.
			 * (The operation does not end at this point;
			 *  this call to 2.1.5.17.3 completes some
			 *  earlier call to 2.1.5.17.1.)
			 *
			 * Set Open.Stream.Oplock.ExclusiveOpen to NULL.
			 * Set GrantExclusiveOplock to TRUE.
			 *
			 * Note: We will keep this exclusive oplock,
			 * but move it to a new handle on this lease.
			 * Won't send a break for this.
			 */
			smb_oplock_ind_break(o, *rop,
			    B_FALSE, STATUS_NEW_HANDLE);
			node->n_oplock.excl_open = o = NULL;
			GrantExcl = B_TRUE;
			break;

		default:
			/*
			 * The operation MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 */
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;

		} /* switch n_oplock.ol_state */
	} /* EndIf CACHE_RWH & !BREAK_ANY... */
	else {
		/*
		 * The operation MUST be failed with...
		 */
		status = NT_STATUS_OPLOCK_NOT_GRANTED;
		goto out;
	}

	/*
	 * If GrantExclusiveOplock is TRUE:
	 *
	 * Set Open.Stream.Oplock.ExclusiveOpen = Open.
	 * Set Open.Stream.Oplock.State =
	 *   (RequestedOplock|EXCLUSIVE).
	 */
	if (GrantExcl) {
		node->n_oplock.excl_open = ofile;
		node->n_oplock.ol_state = *rop | EXCLUSIVE;

		/*
		 * This operation MUST be made cancelable...
		 * This operation waits until the oplock is
		 * broken or canceled, as specified in
		 * section 2.1.5.17.3. Note: This function
		 * does not cause breaks that require a wait,
		 * so never returns ..._BREAK_IN_PROGRESS.
		 *
		 * When the operation specified in section
		 * 2.1.5.17.3 is called, its following input
		 * parameters are transferred to this routine
		 * and then returned by it:
		 *
		 * Status is set to OplockCompletionStatus
		 * NewOplockLevel, AcknowledgeRequired...
		 * from the operation specified in
		 * section 2.1.5.17.3.
		 */
		/* Keep *rop = ... from caller. */
		status = NT_STATUS_SUCCESS;

		/*
		 * First oplock grant installs FEM hooks.
		 */
		if (node->n_oplock.ol_fem == B_FALSE) {
			if (smb_fem_oplock_install(node) != 0) {
				cmn_err(CE_NOTE,
				    "smb_fem_oplock_install failed");
			} else {
				node->n_oplock.ol_fem =	B_TRUE;
			}
		}
	}

out:
	if (status == NT_STATUS_OPLOCK_NOT_GRANTED)
		*rop = LEVEL_NONE;

	return (status);
}

/*
 * 2.1.5.17.2 Algorithm to Request a Shared Oplock
 *
 * The inputs for requesting a shared oplock are:
 *	Open: The Open on which the oplock is being requested.
 *	RequestedOplock: The oplock type being requested.
 *	GrantingInAck: A Boolean value, TRUE if this oplock is being
 *	  requested as part of an oplock break acknowledgement,
 *	  FALSE if not.
 *
 * On completion, the object store MUST return:
 *	Status: An NTSTATUS code that specifies the result.
 *	NewOplockLevel: The type of oplock that the requested oplock has been
 *	  broken (reduced) to.  If a failure status is returned in Status,
 *	  the value of this field is undefined.  Valid values are as follows:
 *		LEVEL_NONE (that is, no oplock)
 *		LEVEL_TWO
 *		A combination of one or more of the following flags:
 *			READ_CACHING
 *			HANDLE_CACHING
 *			WRITE_CACHING
 *	AcknowledgeRequired: A Boolean value: TRUE if the server MUST
 *	acknowledge the oplock break; FALSE if not, as specified in
 *	section 2.1.5.18. If a failure status is returned in Status,
 *	the value of this field is undefined.
 *
 * Note: Stores NewOplockLevel in *rop
 */
static uint32_t
smb_oplock_req_shared(
    smb_ofile_t *ofile,		/* in: the "Open" */
    uint32_t *rop,		/* in: "RequestedOplock", out:NewOplockLevel */
    boolean_t GrantingInAck)
{
	smb_node_t *node = ofile->f_node;
	smb_ofile_t *o;
	boolean_t OplockGranted = B_FALSE;
	uint32_t status = NT_STATUS_OPLOCK_NOT_GRANTED;

	ASSERT(RW_READ_HELD(&node->n_ofile_list.ll_lock));
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * Don't allow grants on closing ofiles.
	 */
	if (ofile->f_oplock_closing)
		return (status);

	/*
	 * If Open.Stream.Oplock is empty:
	 *   Build a new Oplock object with fields initialized as follows:
	 *	Oplock.State set to NO_OPLOCK.
	 *	All other fields set to 0/empty.
	 *   Store the new Oplock object in Open.Stream.Oplock.
	 * EndIf
	 *
	 * Implementation specific:
	 * Open.Stream.Oplock maps to: node->n_oplock
	 */
	if (node->n_oplock.ol_state == 0) {
		node->n_oplock.ol_state = NO_OPLOCK;
	}

	/*
	 * If (GrantingInAck is FALSE) and (Open.Stream.Oplock.State
	 * contains one or more of BREAK_TO_TWO, BREAK_TO_NONE,
	 * BREAK_TO_TWO_TO_NONE, BREAK_TO_READ_CACHING,
	 * BREAK_TO_WRITE_CACHING, BREAK_TO_HANDLE_CACHING,
	 * BREAK_TO_NO_CACHING, or EXCLUSIVE), then:
	 *	The operation MUST be failed with Status set to
	 *	STATUS_OPLOCK_NOT_GRANTED.
	 * EndIf
	 */
	if (GrantingInAck == B_FALSE &&
	    (node->n_oplock.ol_state & (BREAK_ANY | EXCLUSIVE)) != 0) {
		status = NT_STATUS_OPLOCK_NOT_GRANTED;
		goto out;
	}

	/* Switch (RequestedOplock): */
	switch (*rop) {

	case LEVEL_TWO:
		/*
		 * The operation MUST be failed with Status set to
		 * STATUS_OPLOCK_NOT_GRANTED if Open.Stream.Oplock.State
		 * is anything other than the following:
		 *	NO_OPLOCK
		 *	LEVEL_TWO_OPLOCK
		 *	READ_CACHING
		 *	(LEVEL_TWO_OPLOCK|READ_CACHING)
		 */
		switch (node->n_oplock.ol_state) {
		default:
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		case NO_OPLOCK:
		case LEVEL_TWO:
		case READ_CACHING:
		case (LEVEL_TWO | READ_CACHING):
			break;
		}
		/* Deliberate FALL-THROUGH to next Case statement. */
		/* FALLTHROUGH */

	case READ_CACHING:
		/*
		 * The operation MUST be failed with Status set to
		 * STATUS_OPLOCK_NOT_GRANTED if GrantingInAck is FALSE
		 * and Open.Stream.Oplock.State is anything other than...
		 */
		switch (node->n_oplock.ol_state) {
		default:
			if (GrantingInAck == B_FALSE) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}
			break;
		case NO_OPLOCK:
		case LEVEL_TWO:
		case READ_CACHING:
		case (LEVEL_TWO | READ_CACHING):
		case (READ_CACHING | HANDLE_CACHING):
		case (READ_CACHING | HANDLE_CACHING | MIXED_R_AND_RH):
		case (READ_CACHING | HANDLE_CACHING | BREAK_TO_READ_CACHING):
		case (READ_CACHING | HANDLE_CACHING | BREAK_TO_NO_CACHING):
			break;
		}

		if (GrantingInAck == B_FALSE) {
			/*
			 * If there is an Open on
			 * Open.Stream.Oplock.RHOplocks
			 * whose TargetOplockKey is equal to
			 * Open.TargetOplockKey, the operation
			 * MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 *
			 * If there is an Open on
			 * Open.Stream.Oplock.RHBreakQueue
			 * whose TargetOplockKey is equal to
			 * Open.TargetOplockKey, the operation
			 * MUST be failed with Status set to
			 * STATUS_OPLOCK_NOT_GRANTED.
			 *
			 * Implement both in one list walk.
			 */
			FOREACH_NODE_OFILE(node, o) {
				if ((o->f_oplock.onlist_RH ||
				    o->f_oplock.onlist_RHBQ) &&
				    CompareOplockKeys(ofile, o, 0)) {
					status = NT_STATUS_OPLOCK_NOT_GRANTED;
					goto out;
				}
			}

			/*
			 * If there is an Open ThisOpen on
			 * Open.Stream.Oplock.ROplocks whose
			 * TargetOplockKey is equal to Open.TargetOplockKey
			 * (there is supposed to be at most one present):
			 *	* Remove ThisOpen from Open...ROplocks.
			 *	* Notify the server of an oplock break
			 *	  according to the algorithm in section
			 *	  2.1.5.17.3, setting the algorithm's
			 *	  parameters as follows:
			 *		* BreakingOplockOpen = ThisOpen
			 *		* NewOplockLevel = READ_CACHING
			 *		* AcknowledgeRequired = FALSE
			 *		* OplockCompletionStatus =
			 *		  STATUS_..._NEW_HANDLE
			 * (The operation does not end at this point;
			 *  this call to 2.1.5.17.3 completes some
			 *  earlier call to 2.1.5.17.2.)
			 * EndIf
			 *
			 * If this SMB2 lease already has an "R" handle,
			 * we'll update that lease locally to point to
			 * this new handle.
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_R == 0)
					continue;
				if (CompareOplockKeys(ofile, o, 0)) {
					o->f_oplock.onlist_R = B_FALSE;
					node->n_oplock.cnt_R--;
					ASSERT(node->n_oplock.cnt_R >= 0);
					smb_oplock_ind_break(o,
					    CACHE_R, B_FALSE,
					    STATUS_NEW_HANDLE);
				}
			}
		} /* EndIf !GrantingInAck */

		/*
		 * If RequestedOplock equals LEVEL_TWO:
		 *	Add Open to Open.Stream.Oplock.IIOplocks.
		 * Else // RequestedOplock equals READ_CACHING:
		 *	Add Open to Open.Stream.Oplock.ROplocks.
		 * EndIf
		 */
		if (*rop == LEVEL_TWO) {
			ofile->f_oplock.onlist_II = B_TRUE;
			node->n_oplock.cnt_II++;
		} else {
			/* (*rop == READ_CACHING) */
			if (ofile->f_oplock.onlist_R == B_FALSE) {
				ofile->f_oplock.onlist_R = B_TRUE;
				node->n_oplock.cnt_R++;
			}
		}

		/*
		 * Recompute Open.Stream.Oplock.State according to the
		 * algorithm in section 2.1.4.13, passing Open.Stream.Oplock
		 * as the ThisOplock parameter.
		 * Set OplockGranted to TRUE.
		 */
		RecomputeOplockState(node);
		OplockGranted = B_TRUE;
		break;

	case (READ_CACHING|HANDLE_CACHING):
		/*
		 * The operation MUST be failed with Status set to
		 * STATUS_OPLOCK_NOT_GRANTED if GrantingInAck is FALSE
		 * and Open.Stream.Oplock.State is anything other than...
		 */
		switch (node->n_oplock.ol_state) {
		default:
			if (GrantingInAck == B_FALSE) {
				status = NT_STATUS_OPLOCK_NOT_GRANTED;
				goto out;
			}
			break;
		case NO_OPLOCK:
		case READ_CACHING:
		case (READ_CACHING | HANDLE_CACHING):
		case (READ_CACHING | HANDLE_CACHING | MIXED_R_AND_RH):
		case (READ_CACHING | HANDLE_CACHING | BREAK_TO_READ_CACHING):
		case (READ_CACHING | HANDLE_CACHING | BREAK_TO_NO_CACHING):
			break;
		}

		/*
		 * If Open.Stream.IsDeleted is TRUE, the operation MUST be
		 *  failed with Status set to STATUS_OPLOCK_NOT_GRANTED.
		 */
		if ((node->flags & NODE_FLAGS_DELETING) != 0) {
			status = NT_STATUS_OPLOCK_NOT_GRANTED;
			goto out;
		}

		if (GrantingInAck == B_FALSE) {
			/*
			 * If there is an Open ThisOpen on
			 * Open.Stream.Oplock.ROplocks whose
			 * TargetOplockKey is equal to Open.TargetOplockKey
			 * (there is supposed to be at most one present):
			 *	* Remove ThisOpen from Open...ROplocks.
			 *	* Notify the server of an oplock break
			 *	  according to the algorithm in section
			 *	  2.1.5.17.3, setting the algorithm's
			 *	  parameters as follows:
			 *		* BreakingOplockOpen = ThisOpen
			 *		* NewOplockLevel = CACHE_RH
			 *		* AcknowledgeRequired = FALSE
			 *		* OplockCompletionStatus =
			 *		  STATUS_..._NEW_HANDLE
			 * (The operation does not end at this point;
			 *  this call to 2.1.5.17.3 completes some
			 *  earlier call to 2.1.5.17.2.)
			 * EndIf
			 *
			 * If this SMB2 lease already has an "R" handle,
			 * we'll update that lease locally to point to
			 * this new handle (upgrade to "RH").
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_R == 0)
					continue;
				if (CompareOplockKeys(ofile, o, 0)) {
					o->f_oplock.onlist_R = B_FALSE;
					node->n_oplock.cnt_R--;
					ASSERT(node->n_oplock.cnt_R >= 0);
					smb_oplock_ind_break(o,
					    CACHE_RH, B_FALSE,
					    STATUS_NEW_HANDLE);
				}
			}

			/*
			 * If there is an Open ThisOpen on
			 * Open.Stream.Oplock.RHOplocks whose
			 * TargetOplockKey is equal to Open.TargetOplockKey
			 * (there is supposed to be at most one present):
			 *	XXX: Note, the spec. was missing a step:
			 *	XXX: Remove the open from RHOplocks
			 *	XXX: Confirm with MS dochelp
			 *	* Notify the server of an oplock break
			 *	  according to the algorithm in section
			 *	  2.1.5.17.3, setting the algorithm's
			 *	  parameters as follows:
			 *		* BreakingOplockOpen = ThisOpen
			 *		* NewOplockLevel =
			 *		  (READ_CACHING|HANDLE_CACHING)
			 *		* AcknowledgeRequired = FALSE
			 *		* OplockCompletionStatus =
			 *		  STATUS_..._NEW_HANDLE
			 * (The operation does not end at this point;
			 *  this call to 2.1.5.17.3 completes some
			 *  earlier call to 2.1.5.17.2.)
			 * EndIf
			 *
			 * If this SMB2 lease already has an "RH" handle,
			 * we'll update that lease locally to point to
			 * this new handle.
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RH == 0)
					continue;
				if (CompareOplockKeys(ofile, o, 0)) {
					o->f_oplock.onlist_RH = B_FALSE;
					node->n_oplock.cnt_RH--;
					ASSERT(node->n_oplock.cnt_RH >= 0);
					smb_oplock_ind_break(o,
					    CACHE_RH, B_FALSE,
					    STATUS_NEW_HANDLE);
				}
			}
		} /* EndIf !GrantingInAck */

		/*
		 * Add Open to Open.Stream.Oplock.RHOplocks.
		 */
		if (ofile->f_oplock.onlist_RH == B_FALSE) {
			ofile->f_oplock.onlist_RH = B_TRUE;
			node->n_oplock.cnt_RH++;
		}

		/*
		 * Recompute Open.Stream.Oplock.State according to the
		 * algorithm in section 2.1.4.13, passing Open.Stream.Oplock
		 * as the ThisOplock parameter.
		 * Set OplockGranted to TRUE.
		 */
		RecomputeOplockState(node);
		OplockGranted = B_TRUE;
		break;

	default:
		/* No other value of RequestedOplock is possible. */
		ASSERT(0);
		status = NT_STATUS_OPLOCK_NOT_GRANTED;
		goto out;
	}  /* EndSwitch (RequestedOplock) */

	/*
	 * If OplockGranted is TRUE:
	 * This operation MUST be made cancelable by inserting it into
	 *   CancelableOperations.CancelableOperationList.
	 * The operation waits until the oplock is broken or canceled,
	 * as specified in section 2.1.5.17.3.
	 * When the operation specified in section 2.1.5.17.3 is called,
	 * its following input parameters are transferred to this routine
	 * and returned by it:
	 *	Status is set to OplockCompletionStatus from the
	 *	  operation specified in section 2.1.5.17.3.
	 *	NewOplockLevel is set to NewOplockLevel from the
	 *	  operation specified in section 2.1.5.17.3.
	 *	AcknowledgeRequired is set to AcknowledgeRequired from
	 *	  the operation specified in section 2.1.5.17.3.
	 * EndIf
	 */
	if (OplockGranted) {
		/* Note: *rop already set. */
		if ((node->n_oplock.ol_state & BREAK_ANY) != 0) {
			status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
			/* Caller does smb_oplock_wait_break() */
		} else {
			status = NT_STATUS_SUCCESS;
		}

		/*
		 * First oplock grant installs FEM hooks.
		 */
		if (node->n_oplock.ol_fem == B_FALSE) {
			if (smb_fem_oplock_install(node) != 0) {
				cmn_err(CE_NOTE,
				    "smb_fem_oplock_install failed");
			} else {
				node->n_oplock.ol_fem =	B_TRUE;
			}
		}
	}

out:
	if (status == NT_STATUS_OPLOCK_NOT_GRANTED)
		*rop = LEVEL_NONE;

	return (status);
}

/*
 * 2.1.5.17.3 Indicating an Oplock Break to the Server
 * See smb_srv_oplock.c
 */

/*
 * 2.1.5.18 Server Acknowledges an Oplock Break
 *
 * The server provides:
 *	Open - The Open associated with the oplock that has broken.
 *	Type - As part of the acknowledgement, the server indicates a
 *	  new oplock it would like in place of the one that has broken.
 *	  Valid values are as follows:
 *		LEVEL_NONE
 *		LEVEL_TWO
 *		LEVEL_GRANULAR - If this oplock type is specified,
 *		  the server additionally provides:
 *	RequestedOplockLevel - A combination of zero or more of
 *	  the following flags:
 *		READ_CACHING
 *		HANDLE_CACHING
 *		WRITE_CACHING
 *
 * If the server requests a new oplock and it is granted, the request
 * does not complete until the oplock is broken; the operation waits for
 * this to happen. Processing of an oplock break is described in
 * section 2.1.5.17.3.  Whether the new oplock is granted or not, the
 * object store MUST return:
 *
 *	Status - An NTSTATUS code indicating the result of the operation.
 *
 * If the server requests a new oplock and it is granted, then when the
 * oplock breaks and the request finally completes, the object store MUST
 * additionally return:
 *	NewOplockLevel: The type of oplock the requested oplock has
 *	  been broken to. Valid values are as follows:
 *		LEVEL_NONE (that is, no oplock)
 *		LEVEL_TWO
 *		A combination of one or more of the following flags:
 *			READ_CACHING
 *			HANDLE_CACHING
 *			WRITE_CACHING
 *	AcknowledgeRequired: A Boolean value; TRUE if the server MUST
 *	  acknowledge the oplock break, FALSE if not, as specified in
 *	  section 2.1.5.17.2.
 *
 * Note: Stores NewOplockLevel in *rop
 */
uint32_t
smb_oplock_ack_break(
    smb_request_t *sr,
    smb_ofile_t *ofile,
    uint32_t *rop)
{
	smb_node_t *node = ofile->f_node;
	uint32_t type = *rop & OPLOCK_LEVEL_TYPE_MASK;
	uint32_t level = *rop & OPLOCK_LEVEL_CACHE_MASK;
	uint32_t status = NT_STATUS_SUCCESS;
	uint32_t BreakToLevel;
	boolean_t NewOplockGranted = B_FALSE;
	boolean_t ReturnBreakToNone = B_FALSE;
	boolean_t FoundMatchingRHOplock = B_FALSE;
	int other_keys;

	smb_llist_enter(&node->n_ofile_list, RW_READER);
	mutex_enter(&node->n_oplock.ol_mutex);

	/*
	 * If Open.Stream.Oplock is empty, the operation MUST be
	 * failed with Status set to STATUS_INVALID_OPLOCK_PROTOCOL.
	 */
	if (node->n_oplock.ol_state == 0) {
		status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
		goto out;
	}

	if (type == LEVEL_NONE || type == LEVEL_TWO) {
		/*
		 * If Open.Stream.Oplock.ExclusiveOpen is not equal to Open,
		 * the operation MUST be failed with Status set to
		 * STATUS_INVALID_OPLOCK_PROTOCOL.
		 */
		if (node->n_oplock.excl_open != ofile) {
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}

		/*
		 * If Type is LEVEL_TWO and Open.Stream.Oplock.State
		 * contains BREAK_TO_TWO:
		 *	Set Open.Stream.Oplock.State to LEVEL_TWO_OPLOCK.
		 *	Set NewOplockGranted to TRUE.
		 */
		if (type == LEVEL_TWO &&
		    (node->n_oplock.ol_state & BREAK_TO_TWO) != 0) {
			node->n_oplock.ol_state = LEVEL_TWO;
			NewOplockGranted = B_TRUE;
		}

		/*
		 * Else If Open.Stream.Oplock.State contains
		 * BREAK_TO_TWO or BREAK_TO_NONE:
		 *	Set Open.Stream.Oplock.State to NO_OPLOCK.
		 */
		else if ((node->n_oplock.ol_state &
		    (BREAK_TO_TWO | BREAK_TO_NONE)) != 0) {
			node->n_oplock.ol_state = NO_OPLOCK;
		}

		/*
		 * Else If Open.Stream.Oplock.State contains
		 * BREAK_TO_TWO_TO_NONE:
		 *	Set Open.Stream.Oplock.State to NO_OPLOCK.
		 *	Set ReturnBreakToNone to TRUE.
		 */
		else if ((node->n_oplock.ol_state &
		    BREAK_TO_TWO_TO_NONE) != 0) {
			node->n_oplock.ol_state = NO_OPLOCK;
			ReturnBreakToNone = B_TRUE;
		}

		/*
		 * Else
		 *	The operation MUST be failed with Status set to
		 *	STATUS_INVALID_OPLOCK_PROTOCOL.
		 */
		else {
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}

		/*
		 * For each Open WaitingOpen on Open.Stream.Oplock.WaitList:
		 *	Indicate that the operation associated with
		 *	  WaitingOpen can continue according to the
		 *	  algorithm in section 2.1.4.12.1, setting
		 *	  OpenToRelease = WaitingOpen.
		 *	Remove WaitingOpen from Open.Stream.Oplock.WaitList.
		 * EndFor
		 */
		if (node->n_oplock.waiters)
			cv_broadcast(&node->n_oplock.WaitingOpenCV);

		/*
		 * Set Open.Stream.Oplock.ExclusiveOpen to NULL.
		 */
		node->n_oplock.excl_open = NULL;

		if (NewOplockGranted) {
			/*
			 * The operation waits until the newly-granted
			 * Level 2 oplock is broken, as specified in
			 * section 2.1.5.17.3.
			 *
			 * Here we have just Ack'ed a break-to-II
			 * so now get the level II oplock.  We also
			 * checked for break-to-none above, so this
			 * will not need to wait for oplock breaks.
			 */
			status = smb_oplock_req_shared(ofile, rop, B_TRUE);
		}

		else if (ReturnBreakToNone) {
			/*
			 * In this case the server was expecting the oplock
			 * to break to Level 2, but because the oplock is
			 * actually breaking to None (that is, no oplock),
			 * the object store MUST indicate an oplock break
			 * to the server according to the algorithm in
			 * section 2.1.5.17.3, setting the algorithm's
			 * parameters as follows:
			 *	BreakingOplockOpen = Open.
			 *	NewOplockLevel = LEVEL_NONE.
			 *	AcknowledgeRequired = FALSE.
			 *	OplockCompletionStatus = STATUS_SUCCESS.
			 * (Because BreakingOplockOpen is equal to the
			 * passed-in Open, the operation ends at this point.)
			 *
			 * It should be OK to return the reduced oplock
			 * (*rop = LEVEL_NONE) here and avoid the need
			 * to send another oplock break.  This is safe
			 * because we already have an Ack of the break
			 * to Level_II, and the additional break to none
			 * would use AckRequired = FALSE.
			 *
			 * If we followed the spec here, we'd have:
			 * smb_oplock_ind_break(ofile,
			 *    LEVEL_NONE, B_FALSE,
			 *    NT_STATUS_SUCCESS);
			 * (Or smb_oplock_ind_break_in_ack...)
			 */
			*rop = LEVEL_NONE;	/* Reduced from L2 */
		}
		status = NT_STATUS_SUCCESS;
		goto out;
	} /* LEVEL_NONE or LEVEL_TWO */

	if (type != LEVEL_GRANULAR) {
		status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
		goto out;
	}

	/* LEVEL_GRANULAR */

	/*
	 * Let BREAK_LEVEL_MASK = (BREAK_TO_READ_CACHING |
	 *   BREAK_TO_WRITE_CACHING | BREAK_TO_HANDLE_CACHING |
	 *   BREAK_TO_NO_CACHING),
	 * R_AND_RH_GRANTED = (READ_CACHING | HANDLE_CACHING |
	 *   MIXED_R_AND_RH),
	 * RH_GRANTED = (READ_CACHING | HANDLE_CACHING)
	 *
	 * (See BREAK_LEVEL_MASK in smb_oplock.h)
	 */
#define	RH_GRANTED		(READ_CACHING|HANDLE_CACHING)
#define	R_AND_RH_GRANTED	(RH_GRANTED|MIXED_R_AND_RH)

	/*
	 * If there are no BREAK_LEVEL_MASK flags set, this is invalid,
	 * unless the state is R_AND_RH_GRANTED or RH_GRANTED, in which
	 * case we'll need to see if the RHBreakQueue is empty.
	 */

	/*
	 * If (Open.Stream.Oplock.State does not contain any flag in
	 * BREAK_LEVEL_MASK and
	 *  (Open.Stream.Oplock.State != R_AND_RH_GRANTED) and
	 *   (Open.Stream.Oplock.State != RH_GRANTED)) or
	 *   (((Open.Stream.Oplock.State == R_AND_RH_GRANTED) or
	 *  (Open.Stream.Oplock.State == RH_GRANTED)) and
	 *   Open.Stream.Oplock.RHBreakQueue is empty):
	 *	The request MUST be failed with Status set to
	 *	  STATUS_INVALID_OPLOCK_PROTOCOL.
	 * EndIf
	 */
	if ((node->n_oplock.ol_state & BREAK_LEVEL_MASK) == 0) {
		if ((node->n_oplock.ol_state != R_AND_RH_GRANTED) &&
		    (node->n_oplock.ol_state != RH_GRANTED)) {
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}
		/* State is R_AND_RH_GRANTED or RH_GRANTED */
		if (node->n_oplock.cnt_RHBQ == 0) {
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}
	}

	/*
	 * Compute the "Break To" cache level from the
	 * BREAK_TO_... flags
	 */
	switch (node->n_oplock.ol_state & BREAK_LEVEL_MASK) {
	case (BREAK_TO_READ_CACHING | BREAK_TO_WRITE_CACHING |
	    BREAK_TO_HANDLE_CACHING):
		BreakToLevel = CACHE_RWH;
		break;
	case (BREAK_TO_READ_CACHING | BREAK_TO_WRITE_CACHING):
		BreakToLevel = CACHE_RW;
		break;
	case (BREAK_TO_READ_CACHING | BREAK_TO_HANDLE_CACHING):
		BreakToLevel = CACHE_RH;
		break;
	case BREAK_TO_READ_CACHING:
		BreakToLevel = READ_CACHING;
		break;
	case BREAK_TO_NO_CACHING:
	default:
		BreakToLevel = LEVEL_NONE;
		break;
	}

	/* Switch Open.Stream.Oplock.State */
	switch (node->n_oplock.ol_state) {

	case (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH):
	case (READ_CACHING|HANDLE_CACHING):
	case (READ_CACHING|HANDLE_CACHING|BREAK_TO_READ_CACHING):
	case (READ_CACHING|HANDLE_CACHING|BREAK_TO_NO_CACHING):
		/*
		 * For each RHOpContext ThisContext in
		 * Open.Stream.Oplock.RHBreakQueue:
		 *	If ThisContext.Open equals Open:
		 *		(see below)
		 *
		 * Implementation skips the list walk, because
		 * we can get the ofile directly.
		 */
		if (ofile->f_oplock.onlist_RHBQ) {
			smb_ofile_t *o;

			/*
			 * Set FoundMatchingRHOplock to TRUE.
			 * If ThisContext.BreakingToRead is FALSE:
			 *	If RequestedOplockLevel is not 0 and
			 *	Open.Stream.Oplock.WaitList is not empty:
			 *	    The object store MUST indicate an
			 *	    oplock break to the server according to
			 *	    the algorithm in section 2.1.5.17.3,
			 *	    setting the algorithm's params as follows:
			 *		BreakingOplockOpen = Open.
			 *		NewOplockLevel = LEVEL_NONE.
			 *		AcknowledgeRequired = TRUE.
			 *		OplockCompletionStatus =
			 *		  STATUS_CANNOT_GRANT_...
			 *  (Because BreakingOplockOpen is equal to the
			 *   passed Open, the operation ends at this point.)
			 * EndIf
			 */
			FoundMatchingRHOplock = B_TRUE;
			if (ofile->f_oplock.BreakingToRead == B_FALSE) {
				if (level != 0 && node->n_oplock.waiters) {
					/* The ofile stays on RHBQ */
					smb_oplock_ind_break_in_ack(
					    sr, ofile,
					    LEVEL_NONE, B_TRUE);
					status = NT_STATUS_SUCCESS;
					goto out;
				}
			}

			/*
			 * Else // ThisContext.BreakingToRead is TRUE.
			 *    If Open.Stream.Oplock.WaitList is not empty and
			 *    (RequestedOplockLevel is CACHE_RW or CACHE_RWH:
			 *	The object store MUST indicate an oplock
			 *	break to the server according to the
			 *	algorithm in section 2.1.5.17.3, setting
			 *	the algorithm's parameters as follows:
			 *		* BreakingOplockOpen = Open
			 *		* NewOplockLevel = READ_CACHING
			 *		* AcknowledgeRequired = TRUE
			 *		* OplockCompletionStatus =
			 *		  STATUS_CANNOT_GRANT...
			 *	(Because BreakingOplockOpen is equal to the
			 *	 passed-in Open, the operation ends at this
			 *	 point.)
			 *    EndIf
			 * EndIf
			 */
			else { /* BreakingToRead is TRUE */
				if (node->n_oplock.waiters &&
				    (level == CACHE_RW ||
				    level == CACHE_RWH)) {
					/* The ofile stays on RHBQ */
					smb_oplock_ind_break_in_ack(
					    sr, ofile,
					    CACHE_R, B_TRUE);
					status = NT_STATUS_SUCCESS;
					goto out;
				}
			}

			/*
			 * Remove ThisContext from Open...RHBreakQueue.
			 */
			ofile->f_oplock.onlist_RHBQ = B_FALSE;
			node->n_oplock.cnt_RHBQ--;
			ASSERT(node->n_oplock.cnt_RHBQ >= 0);

			/*
			 * The operation waiting for the Read-Handle
			 * oplock to break can continue if there are
			 * no more Read-Handle oplocks outstanding, or
			 * if all the remaining Read-Handle oplocks
			 * have the same oplock key as the waiting
			 * operation.
			 *
			 * For each Open WaitingOpen on Open...WaitList:
			 *
			 *	* If (Open...RHBreakQueue is empty) or
			 *	  (all RHOpContext.Open.TargetOplockKey values
			 *	  on Open.Stream.Oplock.RHBreakQueue are
			 *	  equal to WaitingOpen.TargetOplockKey):
			 *		* Indicate that the operation assoc.
			 *		  with WaitingOpen can continue
			 *		  according to the algorithm in
			 *		  section 2.1.4.12.1, setting
			 *		  OpenToRelease = WaitingOpen.
			 *		* Remove WaitingOpen from
			 *		  Open.Stream.Oplock.WaitList.
			 *	* EndIf
			 * EndFor
			 */
			other_keys = 0;
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RHBQ == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, 0))
					other_keys++;
			}
			if (other_keys == 0)
				cv_broadcast(&node->n_oplock.WaitingOpenCV);

			/*
			 * If RequestedOplockLevel is 0 (that is, no flags):
			 *	* Recompute Open.Stream.Oplock.State
			 *	  according to the algorithm in section
			 *	  2.1.4.13, passing Open.Stream.Oplock as
			 *	  the ThisOplock parameter.
			 *	* The algorithm MUST return Status set to
			 *	  STATUS_SUCCESS at this point.
			 */
			if (level == 0) {
				RecomputeOplockState(node);
				status = NT_STATUS_SUCCESS;
				goto out;
			}

			/*
			 * Else If RequestedOplockLevel does not contain
			 * WRITE_CACHING:
			 *	* The object store MUST request a shared oplock
			 *	  according to the algorithm in section
			 *	  2.1.5.17.2, setting the algorithm's
			 *	  parameters as follows:
			 *		* Open = current Open.
			 *		* RequestedOplock =
			 *		  RequestedOplockLevel.
			 *		* GrantingInAck = TRUE.
			 *	* The operation MUST at this point return any
			 *	  status code returned by the shared oplock
			 *	  request algorithm.
			 */
			else if ((level & WRITE_CACHING) == 0) {
				*rop = level;
				status = smb_oplock_req_shared(
				    ofile, rop, B_TRUE);
				goto out;
			}

			/*
			 * Set Open.Stream.Oplock.ExclusiveOpen to
			 *   ThisContext.Open.
			 * Set Open.Stream.Oplock.State to
			 *   (RequestedOplockLevel|EXCLUSIVE).
			 * This operation MUST be made cancelable by
			 *   inserting it into CancelableOperations...
			 * This operation waits until the oplock is
			 * broken or canceled, as specified in
			 * section 2.1.5.17.3.
			 *
			 * Implementation note:
			 *
			 * Once we assing ol_state below, there
			 * will be no BREAK_TO_... flags set,
			 * so no need to wait for oplock breaks.
			 */
			node->n_oplock.excl_open = ofile;
			node->n_oplock.ol_state = level | EXCLUSIVE;
			status = NT_STATUS_SUCCESS;
		} /* onlist_RHBQ */
		if (FoundMatchingRHOplock == B_FALSE) {
			/* The operation MUST be failed with Status... */
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}
		break;	/* case (READ_CACHING|HANDLE_CACHING...) */

	case (READ_CACHING|WRITE_CACHING|EXCLUSIVE|BREAK_TO_READ_CACHING):
	case (READ_CACHING|WRITE_CACHING|EXCLUSIVE|BREAK_TO_NO_CACHING):
	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING|BREAK_TO_WRITE_CACHING):
	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING|BREAK_TO_HANDLE_CACHING):
	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING):
	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_NO_CACHING):
		/*
		 * If Open.Stream.Oplock.ExclusiveOpen != Open:
		 *	* The operation MUST be failed with Status set to
		 *	  STATUS_INVALID_OPLOCK_PROTOCOL.
		 * EndIf
		 */
		if (node->n_oplock.excl_open != ofile) {
			status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
			goto out;
		}

		/*
		 * If Open.Stream.Oplock.WaitList is not empty and
		 * Open.Stream.Oplock.State does not contain HANDLE_CACHING
		 * and RequestedOplockLevel is CACHE_RWH:
		 *	The object store MUST indicate an oplock break to
		 *	the server according to the algorithm in section
		 *	2.1.5.17.3, setting the algorithm's params as follows:
		 *	* BreakingOplockOpen = Open.
		 *	* NewOplockLevel = BreakToLevel (see above)
		 *	* AcknowledgeRequired = TRUE.
		 *	* OplockCompletionStatus =
		 *	  STATUS_CANNOT_GRANT_REQUESTED_OPLOCK.
		 *   (Because BreakingOplockOpen is equal to the passed-in
		 *    Open, the operation ends at this point.)
		 */
		if (node->n_oplock.waiters &&
		    (node->n_oplock.ol_state & HANDLE_CACHING) == 0 &&
		    level == CACHE_RWH) {
			smb_oplock_ind_break_in_ack(
			    sr, ofile,
			    BreakToLevel, B_TRUE);
			status = NT_STATUS_SUCCESS;
			goto out;
		}

		/*
		 * Else If Open.Stream.IsDeleted is TRUE and
		 * RequestedOplockLevel contains HANDLE_CACHING:
		 */
		else if (((node->flags & NODE_FLAGS_DELETING) != 0) &&
		    (level & HANDLE_CACHING) != 0) {

			/*
			 * The object store MUST indicate an oplock break to
			 * the server according to the algorithm in section
			 * 2.1.5.17.3, setting the algorithm's params as
			 * follows:
			 *	* BreakingOplockOpen = Open.
			 *	* NewOplockLevel = RequestedOplockLevel
			 *	  without HANDLE_CACHING (for example if
			 *	  RequestedOplockLevel is
			 *	  (READ_CACHING|HANDLE_CACHING), then
			 *	   NewOplockLevel would be just READ_CACHING).
			 *	* AcknowledgeRequired = TRUE.
			 *	* OplockCompletionStatus =
			 *	  STATUS_CANNOT_GRANT_REQUESTED_OPLOCK.
			 * (Because BreakingOplockOpen is equal to the
			 *  passed-in Open, the operation ends at this point.)
			 */
			level &= ~HANDLE_CACHING;
			smb_oplock_ind_break_in_ack(
			    sr, ofile,
			    level, B_TRUE);
			status = NT_STATUS_SUCCESS;
			goto out;
		}

		/*
		 * For each Open WaitingOpen on Open.Stream.Oplock.WaitList:
		 *	* Indicate that the operation associated with
		 *	  WaitingOpen can continue according to the algorithm
		 *	  in section 2.1.4.12.1, setting OpenToRelease
		 *	  = WaitingOpen.
		 *	* Remove WaitingOpen from Open.Stream.Oplock.WaitList.
		 * EndFor
		 */
		cv_broadcast(&node->n_oplock.WaitingOpenCV);

		/*
		 * If RequestedOplockLevel does not contain WRITE_CACHING:
		 *	* Set Open.Stream.Oplock.ExclusiveOpen to NULL.
		 * EndIf
		 */
		if ((level & WRITE_CACHING) == 0) {
			node->n_oplock.excl_open = NULL;
		}

		/*
		 * If RequestedOplockLevel is 0 (that is, no flags):
		 *	* Set Open.Stream.Oplock.State to NO_OPLOCK.
		 *	* The operation returns Status set to STATUS_SUCCESS
		 *	  at this point.
		 */
		if (level == 0) {
			node->n_oplock.ol_state = NO_OPLOCK;
			status = NT_STATUS_SUCCESS;
			goto out;
		}

		/*
		 * Deal with possibly still pending breaks.
		 * Two cases: R to none, RH to R or none.
		 *
		 * XXX: These two missing from [MS-FSA]
		 */

		/*
		 * Breaking R to none?  This is like:
		 * "If BreakCacheLevel contains READ_CACHING..."
		 * from smb_oplock_break_cmn.
		 */
		if (level == CACHE_R && BreakToLevel == LEVEL_NONE) {
			smb_oplock_ind_break_in_ack(
			    sr, ofile,
			    LEVEL_NONE, B_FALSE);
			node->n_oplock.ol_state = NO_OPLOCK;
			status = NT_STATUS_SUCCESS;
			goto out;
		}

		/*
		 * Breaking RH to R or RH to none?  This is like:
		 * "If BreakCacheLevel equals HANDLE_CACHING..."
		 * from smb_oplock_break_cmn.
		 */
		if (level == CACHE_RH &&
		    (BreakToLevel == CACHE_R ||
		    BreakToLevel == LEVEL_NONE)) {
			smb_oplock_ind_break_in_ack(
			    sr, ofile,
			    BreakToLevel, B_TRUE);

			ofile->f_oplock.BreakingToRead =
			    (BreakToLevel & READ_CACHING) ? 1: 0;

			ASSERT(!(ofile->f_oplock.onlist_RHBQ));
			ofile->f_oplock.onlist_RHBQ = B_TRUE;
			node->n_oplock.cnt_RHBQ++;

			RecomputeOplockState(node);
			status = NT_STATUS_SUCCESS;
			goto out;
		}

		/*
		 * Else If RequestedOplockLevel does not contain WRITE_CACHING:
		 *	* The object store MUST request a shared oplock
		 *	  according to the algorithm in section 2.1.5.17.2,
		 *	  setting the algorithm's parameters as follows:
		 *		* Pass in the current Open.
		 *		* RequestedOplock = RequestedOplockLevel.
		 *		* GrantingInAck = TRUE.
		 *	* The operation MUST at this point return any status
		 *	  returned by the shared oplock request algorithm.
		 */
		if ((level & WRITE_CACHING) == 0) {
			*rop = level;
			status = smb_oplock_req_shared(ofile, rop, B_TRUE);
			goto out;
		}

		/*
		 * Note that because this oplock is being set up as part of
		 * an acknowledgement of an exclusive oplock break,
		 * Open.Stream.Oplock.ExclusiveOpen was set
		 * at the time of the original oplock request;
		 * it contains Open.
		 *	* Set Open.Stream.Oplock.State to
		 *	  (RequestedOplockLevel|EXCLUSIVE).
		 *	* This operation MUST be made cancelable...
		 *	* This operation waits until the oplock is broken or
		 *	  canceled, as specified in section 2.1.5.17.3.
		 *
		 * Implementation notes:
		 *
		 * This can only be a break from RWH to RW.
		 * The assignment of ol_state below means there will be
		 * no BREAK_TO_... bits set, and therefore no need for
		 * "waits until the oplock is broken" as described in
		 * the spec for this bit of code.  Therefore, this will
		 * return SUCCESS instead of OPLOCK_BREAK_IN_PROGRESS.
		 */
		node->n_oplock.ol_state = level | EXCLUSIVE;
		status = NT_STATUS_SUCCESS;
		break;	/* case (READ_CACHING|WRITE_CACHING|...) */

	default:
		/* The operation MUST be failed with Status */
		status = NT_STATUS_INVALID_OPLOCK_PROTOCOL;
		break;

	} /* Switch (oplock.state) */

out:
	if (status == NT_STATUS_INVALID_OPLOCK_PROTOCOL)
		*rop = LEVEL_NONE;

	if (status == NT_STATUS_SUCCESS &&
	    type == LEVEL_GRANULAR &&
	    *rop != LEVEL_NONE) {
		*rop |= LEVEL_GRANULAR;
		/* As above, leased oplock may have moved. */
#ifndef	TESTJIG
		if (ofile->f_lease != NULL)
			ofile->f_lease->ls_oplock_ofile = ofile;
#endif
	}

	/*
	 * If this node no longer has any oplock grants, let's
	 * go ahead and remove the FEM hooks now. We could leave
	 * that until close, but this lets access outside of SMB
	 * be free of FEM oplock work after a "break to none".
	 */
	if (node->n_oplock.ol_state == NO_OPLOCK &&
	    node->n_oplock.ol_fem == B_TRUE) {
		smb_fem_oplock_uninstall(node);
		node->n_oplock.ol_fem = B_FALSE;
	}

	/*
	 * The spec. describes waiting for a break here,
	 * but we let the caller do that (when needed) if
	 * status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS
	 */
	mutex_exit(&node->n_oplock.ol_mutex);
	smb_llist_exit(&node->n_ofile_list);

	return (status);
}

/*
 * 2.1.4.12 Algorithm to Check for an Oplock Break
 *
 * The inputs for this algorithm are:
 *
 * Open: The Open being used in the request calling this algorithm.
 *
 * Oplock: The Oplock being checked.
 *
 * Operation: A code describing the operation being processed.
 *
 * OpParams: Parameters associated with the Operation code that are
 *   passed in from the calling request. For example, if Operation is
 *   OPEN, as specified in section 2.1.5.1, then OpParams will have the
 *   members DesiredAccess and CreateDisposition. Each of these is a
 *   parameter to the open request as specified in section 2.1.5.1.
 *   This parameter could be empty, depending on the Operation code.
 *
 * Flags: An optional parameter. If unspecified it is considered to
 *   contain 0. Valid nonzero values are:
 *	PARENT_OBJECT
 *
 * The algorithm uses the following local variables:
 *
 * Boolean values (initialized to FALSE):
 *   BreakToTwo, BreakToNone, NeedToWait
 *
 * BreakCacheLevel – MAY contain 0 or a combination of one or more of
 *   READ_CACHING, WRITE_CACHING, or HANDLE_CACHING, as specified in
 *   section 2.1.1.10. Initialized to 0.
 *   Note that there are only four legal nonzero combinations of flags
 *   for BreakCacheLevel:
 *	(READ_CACHING|WRITE_CACHING|HANDLE_CACHING)
 *	(READ_CACHING|WRITE_CACHING)
 *	WRITE_CACHING
 *	HANDLE_CACHING
 *
 * Algorithm: (all)
 * If Oplock is not empty and Oplock.State is not NO_OPLOCK:
 *	If Flags contains PARENT_OBJECT:
 *		If Operation is OPEN, CLOSE, FLUSH_DATA,
 *		  FS_CONTROL(set_encryption) or
 *		  SET_INFORMATION(Basic, Allocation, EoF,
 *		  Rename, Link, Shortname, VDL):
 *			Set BreakCacheLevel to (READ_CACHING|WRITE_CACHING).
 *		EndIf
 *	Else // Normal operation (not PARENT_OBJECT)
 *		Switch (Operation):
 *		Case OPEN, CLOSE, ...
 *		EndSwitch
 *	EndIf // not parent
 *	// Common section for all above
 *	If BreakToTwo is TRUE:
 *		...
 *	Else If BreakToNone
 *		...
 *	EndIf
 *	...
 * EndIf
 *
 * This implementation uses separate functions for each of:
 *	if (flags & PARENT)... else
 *	switch (Operation)...
 */


/*
 * If Flags contains PARENT_OBJECT:
 * ...
 * Note that this function is unusual in that the node arg is
 * the PARENT directory node, and ofile is NOT on the ofile list
 * of that directory but one of the nodes under it.
 *
 * Note that until we implement directory leases, this is a no-op.
 */
uint32_t
smb_oplock_break_PARENT(smb_node_t *node, smb_ofile_t *ofile)
{
	uint32_t BreakCacheLevel;

	/*
	 * If Operation is OPEN, CLOSE, FLUSH_DATA,
	 *  FS_CONTROL(set_encryption) or
	 * SET_INFORMATION(Basic, Allocation, EoF,
	 * Rename, Link, Shortname, VDL):
	 *	 Set BreakCacheLevel to (READ_CACHING|WRITE_CACHING).
	 * EndIf
	 */
	BreakCacheLevel = PARENT_OBJECT |
	    (READ_CACHING|WRITE_CACHING);

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Helper for the cases where section 2.1.5.1 says:
 *
 * If Open.Stream.Oplock is not empty and Open.Stream.Oplock.State
 * contains BATCH_OPLOCK, the object store MUST check for an oplock
 * break according to the algorithm in section 2.1.4.12,
 * with input values as follows:
 *	Open equal to this operation's Open
 *	Oplock equal to Open.Stream.Oplock
 *	Operation equal to "OPEN"
 *	OpParams containing two members:
 *      (DesiredAccess, CreateDisposition)
 *
 * So basically, just call smb_oplock_break_OPEN(), but
 * only if there's a batch oplock.
 */
uint32_t
smb_oplock_break_BATCH(smb_node_t *node, smb_ofile_t *ofile,
    uint32_t DesiredAccess, uint32_t CreateDisposition)
{
	if ((node->n_oplock.ol_state & BATCH_OPLOCK) == 0)
		return (0);

	return (smb_oplock_break_OPEN(node, ofile,
	    DesiredAccess, CreateDisposition));
}

/*
 * Case OPEN, as specified in section 2.1.5.1:
 *
 * Note: smb_ofile_open constructs a partially complete smb_ofile_t
 * for this call, which can be considerd a "proposed open".  This
 * open may or may not turn into a usable open depending on what
 * happens in the remainder of the ofile_open code path.
 */
uint32_t
smb_oplock_break_OPEN(smb_node_t *node, smb_ofile_t *ofile,
    uint32_t DesiredAccess, uint32_t CreateDisposition)
{
	uint32_t BreakCacheLevel = 0;
	/* BreakToTwo, BreakToNone, NeedToWait */

	/*
	 * If OpParams.DesiredAccess contains no flags other than
	 * FILE_READ_ATTRIBUTES, FILE_WRITE_ATTRIBUTES, or SYNCHRONIZE,
	 *   the algorithm returns at this point.
	 * EndIf
	 */
	if ((DesiredAccess & ~(FILE_READ_ATTRIBUTES |
	    FILE_WRITE_ATTRIBUTES | SYNCHRONIZE | READ_CONTROL)) == 0)
		return (0);

	/*
	 * If OpParams.CreateDisposition is FILE_SUPERSEDE,
	 * FILE_OVERWRITE, or FILE_OVERWRITE_IF:
	 *	Set BreakToNone to TRUE, set BreakCacheLevel to
	 *	   (READ_CACHING|WRITE_CACHING).
	 * Else
	 *	Set BreakToTwo to TRUE,
	 *	set BreakCacheLevel to WRITE_CACHING.
	 * EndIf
	 */
	if (CreateDisposition == FILE_SUPERSEDE ||
	    CreateDisposition == FILE_OVERWRITE ||
	    CreateDisposition == FILE_OVERWRITE_IF) {
		BreakCacheLevel = BREAK_TO_NONE |
		    (READ_CACHING|WRITE_CACHING);
	} else {
		/*
		 * CreateDispositons: OPEN, OPEN_IF
		 */
		BreakCacheLevel = BREAK_TO_TWO |
		    WRITE_CACHING;
	}

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Case OPEN_BREAK_H, as specified in section 2.1.5.1:
 *	Set BreakCacheLevel to HANDLE_CACHING.
 * EndCase
 */
uint32_t
smb_oplock_break_HANDLE(smb_node_t *node, smb_ofile_t *ofile)
{
	uint32_t BreakCacheLevel = HANDLE_CACHING;

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Case CLOSE, as specified in section 2.1.5.4:
 *
 * The MS-FSA spec. describes sending oplock break indications
 * (smb_oplock_ind_break ... NT_STATUS_OPLOCK_HANDLE_CLOSED)
 * for several cases where the ofile we're closing has some
 * oplock grants.  We modify these slightly and use them to
 * clear out the SMB-level oplock state.  We could probably
 * just skip most of these, as the caller knows this handle is
 * closing and could just discard the SMB-level oplock state.
 * For now, keeping this close to what the spec says.
 */
void
smb_oplock_break_CLOSE(smb_node_t *node, smb_ofile_t *ofile)
{
	smb_ofile_t *o;

	ASSERT(RW_READ_HELD(&node->n_ofile_list.ll_lock));
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * If Oplock.IIOplocks is not empty:
	 *   For each Open ThisOpen in Oplock.IIOplocks:
	 *	If ThisOpen == Open:
	 *		Remove ThisOpen from Oplock.IIOplocks.
	 *		Notify the server of an oplock break according to
	 *		  the algorithm in section 2.1.5.17.3, setting the
	 *		  algorithm's parameters as follows:
	 *			BreakingOplockOpen = ThisOpen.
	 *			NewOplockLevel = LEVEL_NONE.
	 *			AcknowledgeRequired = FALSE.
	 *			OplockCompletionStatus = STATUS_SUCCESS.
	 *		(The operation does not end at this point;
	 *		 this call to 2.1.5.17.3 completes some
	 *		 earlier call to 2.1.5.17.2.)
	 *	EndIf
	 *   EndFor
	 *   Recompute Oplock.State according to the algorithm in
	 *     section 2.1.4.13, passing Oplock as the ThisOplock parameter.
	 * EndIf
	 */
	if (node->n_oplock.cnt_II > 0) {
		o = ofile; /* No need for list walk */
		if (o->f_oplock.onlist_II) {
			o->f_oplock.onlist_II = B_FALSE;
			node->n_oplock.cnt_II--;
			ASSERT(node->n_oplock.cnt_II >= 0);
			/*
			 * The spec. says to do:
			 * smb_oplock_ind_break(o,
			 *    LEVEL_NONE, B_FALSE,
			 *    NT_STATUS_SUCCESS);
			 *
			 * We'll use STATUS_OPLOCK_HANDLE_CLOSED
			 * like all the other ind_break calls in
			 * this function, so the SMB-level will
			 * just clear out its oplock state.
			 */
			smb_oplock_ind_break(o,
			    LEVEL_NONE, B_FALSE,
			    NT_STATUS_OPLOCK_HANDLE_CLOSED);
		}
		RecomputeOplockState(node);
	}

	/*
	 * If Oplock.ROplocks is not empty:
	 *   For each Open ThisOpen in Oplock.ROplocks:
	 *	If ThisOpen == Open:
	 *		Remove ThisOpen from Oplock.ROplocks.
	 *		Notify the server of an oplock break according to
	 *		  the algorithm in section 2.1.5.17.3, setting the
	 *		  algorithm's parameters as follows:
	 *			BreakingOplockOpen = ThisOpen.
	 *			NewOplockLevel = LEVEL_NONE.
	 *			AcknowledgeRequired = FALSE.
	 *			OplockCompletionStatus =
	 *			  STATUS_OPLOCK_HANDLE_CLOSED.
	 *		(The operation does not end at this point;
	 *		 this call to 2.1.5.17.3 completes some
	 *		 earlier call to 2.1.5.17.2.)
	 *	EndIf
	 *   EndFor
	 *   Recompute Oplock.State according to the algorithm in
	 *     section 2.1.4.13, passing Oplock as the ThisOplock parameter.
	 * EndIf
	 */
	if (node->n_oplock.cnt_R > 0) {
		o = ofile; /* No need for list walk */
		if (o->f_oplock.onlist_R) {
			o->f_oplock.onlist_R = B_FALSE;
			node->n_oplock.cnt_R--;
			ASSERT(node->n_oplock.cnt_R >= 0);

			smb_oplock_ind_break(o,
			    LEVEL_NONE, B_FALSE,
			    NT_STATUS_OPLOCK_HANDLE_CLOSED);
		}
		RecomputeOplockState(node);
	}

	/*
	 * If Oplock.RHOplocks is not empty:
	 *   For each Open ThisOpen in Oplock.RHOplocks:
	 *	If ThisOpen == Open:
	 *		Remove ThisOpen from Oplock.RHOplocks.
	 *		Notify the server of an oplock break according to
	 *		  the algorithm in section 2.1.5.17.3, setting the
	 *		  algorithm's parameters as follows:
	 *			BreakingOplockOpen = ThisOpen.
	 *			NewOplockLevel = LEVEL_NONE.
	 *			AcknowledgeRequired = FALSE.
	 *			OplockCompletionStatus =
	 *			   STATUS_OPLOCK_HANDLE_CLOSED.
	 *		(The operation does not end at this point;
	 *		 this call to 2.1.5.17.3 completes some
	 *		 earlier call to 2.1.5.17.2.)
	 *	EndIf
	 *   EndFor
	 *   Recompute Oplock.State according to the algorithm in
	 *     section 2.1.4.13, passing Oplock as the ThisOplock parameter.
	 * EndIf
	 */
	if (node->n_oplock.cnt_RH > 0) {
		o = ofile; /* No need for list walk */
		if (o->f_oplock.onlist_RH) {
			o->f_oplock.onlist_RH = B_FALSE;
			node->n_oplock.cnt_RH--;
			ASSERT(node->n_oplock.cnt_RH >= 0);

			smb_oplock_ind_break(o,
			    LEVEL_NONE, B_FALSE,
			    NT_STATUS_OPLOCK_HANDLE_CLOSED);
		}
		RecomputeOplockState(node);
	}

	/*
	 * If Oplock.RHBreakQueue is not empty:
	 *	For each RHOpContext ThisContext in Oplock.RHBreakQueue:
	 *		If ThisContext.Open == Open:
	 *			Remove ThisContext from Oplock.RHBreakQueue.
	 *		EndIf
	 *	EndFor
	 *	Recompute Oplock.State according to the algorithm in
	 *	  section 2.1.4.13, passing Oplock as the ThisOplock parameter.
	 *	For each Open WaitingOpen on Oplock.WaitList:
	 *		If Oplock.RHBreakQueue is empty:
	 *		(or) If the value of every
	 *		RHOpContext.Open.TargetOplockKey
	 *		on Oplock.RHBreakQueue is equal to
	 *		WaitingOpen .TargetOplockKey:
	 *			Indicate that the op. assoc. with
	 *			WaitingOpen can continue according to
	 *			the algorithm in section 2.1.4.12.1,
	 *			setting OpenToRelease = WaitingOpen.
	 *			Remove WaitingOpen from Oplock.WaitList.
	 *		EndIf
	 *	EndFor
	 * EndIf
	 */
	if (node->n_oplock.cnt_RHBQ > 0) {
		o = ofile; /* No need for list walk */
		if (o->f_oplock.onlist_RHBQ) {
			o->f_oplock.onlist_RHBQ = B_FALSE;
			node->n_oplock.cnt_RHBQ--;
			ASSERT(node->n_oplock.cnt_RHBQ >= 0);
		}
		RecomputeOplockState(node);
		/*
		 * We don't keep a WaitingOpen list, so just
		 * wake them all and let them look at the
		 * updated Oplock.RHBreakQueue
		 */
		cv_broadcast(&node->n_oplock.WaitingOpenCV);
	}

	/*
	 * If Open equals Open.Oplock.ExclusiveOpen
	 *	If Oplock.State contains none of (BREAK_ANY):
	 *		Notify the server of an oplock break according to
	 *		  the algorithm in section 2.1.5.17.3, setting the
	 *		  algorithm's parameters as follows:
	 *			BreakingOplockOpen = Oplock.ExclusiveOpen.
	 *			NewOplockLevel = LEVEL_NONE.
	 *			AcknowledgeRequired = FALSE.
	 *			OplockCompletionStatus equal to:
	 *				STATUS_OPLOCK_HANDLE_CLOSED if
	 *				  Oplock.State contains any of
	 *				  READ_CACHING, WRITE_CACHING, or
	 *				  HANDLE_CACHING.
	 *				STATUS_SUCCESS otherwise.
	 *		(The operation does not end at this point;
	 *		 this call to 2.1.5.17.3 completes some
	 *		 earlier call to 2.1.5.17.1.)
	 *	EndIf
	 *	Set Oplock.ExclusiveOpen to NULL.
	 *	Set Oplock.State to NO_OPLOCK.
	 *	For each Open WaitingOpen on Oplock.WaitList:
	 *		Indicate that the operation associated with WaitingOpen
	 *		  can continue according to the algorithm in section
	 *		  2.1.4.12.1, setting OpenToRelease = WaitingOpen.
	 *		Remove WaitingOpen from Oplock.WaitList.
	 *	EndFor
	 * EndIf
	 *
	 * Modify this slightly from what the spec. says and only
	 * up-call the break with status STATUS_OPLOCK_HANDLE_CLOSED.
	 * The STATUS_SUCCESS case would do nothing at the SMB level,
	 * so we'll just skip that part.
	 */
	if (ofile == node->n_oplock.excl_open) {
		uint32_t level = node->n_oplock.ol_state & CACHE_RWH;
		if (level != 0 &&
		    (node->n_oplock.ol_state & BREAK_ANY) == 0) {
			smb_oplock_ind_break(ofile,
			    LEVEL_NONE, B_FALSE,
			    NT_STATUS_OPLOCK_HANDLE_CLOSED);
		}
		node->n_oplock.excl_open = NULL;
		node->n_oplock.ol_state = NO_OPLOCK;
		cv_broadcast(&node->n_oplock.WaitingOpenCV);
	}

	/*
	 * The CLOSE sub-case of 2.1.5.4 (separate function here)
	 * happens to always leave BreakCacheLevel=0 (see 2.1.5.4)
	 * so there's never a need to call smb_oplock_break_cmn()
	 * in this function.  If that changed and we were to have
	 * BreakCacheLevel != 0 here, then we'd need to call:
	 * smb_oplock_break_cmn(node, ofile, BreakCacheLevel);
	 */

	if ((node->n_oplock.ol_state & BREAK_ANY) == 0)
		cv_broadcast(&node->n_oplock.WaitingOpenCV);

	/*
	 * If no longer any oplock, remove FEM hooks.
	 */
	if (node->n_oplock.ol_state == NO_OPLOCK &&
	    node->n_oplock.ol_fem == B_TRUE) {
		smb_fem_oplock_uninstall(node);
		node->n_oplock.ol_fem = B_FALSE;
	}
}

/*
 * Case READ, as specified in section 2.1.5.2:
 *	Set BreakToTwo to TRUE
 *	Set BreakCacheLevel to WRITE_CACHING.
 * EndCase
 */
uint32_t
smb_oplock_break_READ(smb_node_t *node, smb_ofile_t *ofile)
{
	uint32_t BreakCacheLevel = BREAK_TO_TWO | WRITE_CACHING;

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Case FLUSH_DATA, as specified in section 2.1.5.6:
 *	Set BreakToTwo to TRUE
 *	Set BreakCacheLevel to WRITE_CACHING.
 * EndCase
 * Callers just use smb_oplock_break_READ() -- same thing.
 */

/*
 * Case LOCK_CONTROL, as specified in section 2.1.5.7:
 * Note: Spec does fall-through to WRITE here.
 *
 * Case WRITE, as specified in section 2.1.5.3:
 *	Set BreakToNone to TRUE
 *	Set BreakCacheLevel to (READ_CACHING|WRITE_CACHING).
 * EndCase
 */
uint32_t
smb_oplock_break_WRITE(smb_node_t *node, smb_ofile_t *ofile)
{
	uint32_t BreakCacheLevel = BREAK_TO_NONE |
	    (READ_CACHING|WRITE_CACHING);

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Case SET_INFORMATION, as specified in section 2.1.5.14:
 * Switch (OpParams.FileInformationClass):
 *	Case FileEndOfFileInformation:
 *	Case FileAllocationInformation:
 *		Set BreakToNone to TRUE
 *		Set BreakCacheLevel to (READ_CACHING|WRITE_CACHING).
 *	EndCase
 *	Case FileRenameInformation:
 *	Case FileLinkInformation:
 *	Case FileShortNameInformation:
 *		Set BreakCacheLevel to HANDLE_CACHING.
 *		If Oplock.State contains BATCH_OPLOCK,
 *		  set BreakToNone to TRUE.
 *	EndCase
 *	Case FileDispositionInformation:
 *		If OpParams.DeleteFile is TRUE,
 *		Set BreakCacheLevel to HANDLE_CACHING.
 *	EndCase
 * EndSwitch
 */
uint32_t
smb_oplock_break_SETINFO(smb_node_t *node, smb_ofile_t *ofile,
    uint32_t InfoClass)
{
	uint32_t BreakCacheLevel = 0;

	switch (InfoClass) {
	case FileEndOfFileInformation:
	case FileAllocationInformation:
		BreakCacheLevel = BREAK_TO_NONE |
		    (READ_CACHING|WRITE_CACHING);
		break;

	case FileRenameInformation:
	case FileLinkInformation:
	case FileShortNameInformation:
		BreakCacheLevel = HANDLE_CACHING;
		if (node->n_oplock.ol_state & BATCH_OPLOCK) {
			BreakCacheLevel |= BREAK_TO_NONE;
		}
		break;
	case FileDispositionInformation:
		/* Only called if (OpParams.DeleteFile is TRUE) */
		BreakCacheLevel = HANDLE_CACHING;
		break;

	}

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * This one is not from the spec.  It appears that Windows will
 * open a handle for an SMB1 delete call (at least internally).
 * We don't open a handle for delete, but do want to break as if
 * we had done, so this breaks like a combination of:
 *	break_BATCH(... DELETE, FILE_OPEN_IF)
 *	break_HANDLE(...)
 */
uint32_t
smb_oplock_break_DELETE(smb_node_t *node, smb_ofile_t *ofile)
{
	uint32_t BreakCacheLevel = HANDLE_CACHING;

	if ((node->n_oplock.ol_state & BATCH_OPLOCK) != 0)
		BreakCacheLevel |= BREAK_TO_TWO;

	return (smb_oplock_break_cmn(node, ofile, BreakCacheLevel));
}

/*
 * Case FS_CONTROL, as specified in section 2.1.5.9:
 *	If OpParams.ControlCode is FSCTL_SET_ZERO_DATA:
 *		Set BreakToNone to TRUE.
 *		Set BreakCacheLevel to (READ_CACHING|WRITE_CACHING).
 *	EndIf
 * EndCase
 * Callers just use smb_oplock_break_WRITE() -- same thing.
 */

/*
 * Common section for all cases above
 * Note: When called via FEM: ofile == NULL
 */
static uint32_t
smb_oplock_break_cmn(smb_node_t *node,
    smb_ofile_t *ofile, uint32_t BreakCacheLevel)
{
	smb_oplock_t *nol = &node->n_oplock;
	uint32_t CmpFlags, status;
	boolean_t BreakToTwo, BreakToNone, NeedToWait;
	smb_ofile_t *o = NULL;

	CmpFlags = (BreakCacheLevel & PARENT_OBJECT);
	BreakToTwo = (BreakCacheLevel & BREAK_TO_TWO) != 0;
	BreakToNone = (BreakCacheLevel & BREAK_TO_NONE) != 0;
	BreakCacheLevel &= (READ_CACHING | WRITE_CACHING | HANDLE_CACHING);
	NeedToWait = B_FALSE;
	status = NT_STATUS_SUCCESS;

	smb_llist_enter(&node->n_ofile_list, RW_READER);
	mutex_enter(&node->n_oplock.ol_mutex);

	if (node->n_oplock.ol_state == 0 ||
	    node->n_oplock.ol_state == NO_OPLOCK)
		goto out;

	if (BreakToTwo) {
		/*
		 * If (Oplock.State != LEVEL_TWO_OPLOCK) and
		 *    ((Oplock.ExclusiveOpen is empty) or
		 *     (Oplock.ExclusiveOpen.TargetOplockKey !=
		 *      Open.TargetOplockKey)):
		 */
		if ((nol->ol_state != LEVEL_TWO_OPLOCK) &&
		    (((o = nol->excl_open) == NULL) ||
		    !CompareOplockKeys(ofile, o, CmpFlags))) {

			/*
			 * If (Oplock.State contains EXCLUSIVE) and
			 *  (Oplock.State contains none of READ_CACHING,
			 *   WRITE_CACHING, or HANDLE_CACHING):
			 */
			if ((nol->ol_state & EXCLUSIVE) != 0 &&
			    (nol->ol_state & CACHE_RWH) == 0) {
				/*
				 * If Oplock.State contains none of:
				 *	BREAK_TO_NONE,
				 *	BREAK_TO_TWO,
				 *	BREAK_TO_TWO_TO_NONE,
				 *	BREAK_TO_READ_CACHING,
				 *	BREAK_TO_WRITE_CACHING,
				 *	BREAK_TO_HANDLE_CACHING,
				 *	BREAK_TO_NO_CACHING:
				 */
				if ((nol->ol_state & BREAK_ANY) == 0) {

					/*
					 * Oplock.State MUST contain either
					 * LEVEL_ONE_OPLOCK or BATCH_OPLOCK.
					 * Set BREAK_TO_TWO in Oplock.State.
					 */
					ASSERT((nol->ol_state &
					    (LEVEL_ONE | LEVEL_BATCH)) != 0);
					nol->ol_state |= BREAK_TO_TWO;

					/*
					 * Notify the server of an oplock break
					 * according to the algorithm in section
					 * 2.1.5.17.3, setting the algorithm's
					 * parameters as follows:
					 *	BreakingOplockOpen =
					 *	  Oplock.ExclusiveOpen.
					 *	NewOplockLevel = LEVEL_TWO.
					 *	AcknowledgeRequired = TRUE.
					 *	Compl_Status = STATUS_SUCCESS.
					 * (The operation does not end at this
					 * point; this call to 2.1.5.17.3
					 * completes some earlier call to
					 * 2.1.5.17.1.)
					 */
					smb_oplock_ind_break(o,
					    LEVEL_TWO, B_TRUE,
					    NT_STATUS_SUCCESS);
				}

				/*
				 * The operation that called this algorithm
				 *  MUST be made cancelable by ...
				 * The operation that called this algorithm
				 *  waits until the oplock break is
				 *  acknowledged, as specified in section
				 *  2.1.5.18, or the operation is canceled.
				 */
				status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
				/* Caller does smb_oplock_wait_break() */
			}
		}
	} else if (BreakToNone) {
		/*
		 * If (Oplock.State == LEVEL_TWO_OPLOCK) or
		 *  (Oplock.ExclusiveOpen is empty) or
		 *  (Oplock.ExclusiveOpen.TargetOplockKey !=
		 *   Open.TargetOplockKey):
		 */
		if (nol->ol_state == LEVEL_TWO_OPLOCK ||
		    (((o = nol->excl_open) == NULL) ||
		    !CompareOplockKeys(ofile, o, CmpFlags))) {

			/*
			 * If (Oplock.State != NO_OPLOCK) and
			 * (Oplock.State contains neither
			 *  WRITE_CACHING nor HANDLE_CACHING):
			 */
			if (nol->ol_state != NO_OPLOCK &&
			    (nol->ol_state &
			    (WRITE_CACHING | HANDLE_CACHING)) == 0) {

				/*
				 * If Oplock.State contains none of:
				 *	LEVEL_TWO_OPLOCK,
				 *	BREAK_TO_NONE,
				 *	BREAK_TO_TWO,
				 *	BREAK_TO_TWO_TO_NONE,
				 *	BREAK_TO_READ_CACHING,
				 *	BREAK_TO_WRITE_CACHING,
				 *	BREAK_TO_HANDLE_CACHING, or
				 *	BREAK_TO_NO_CACHING:
				 */
				if ((nol->ol_state &
				    (LEVEL_TWO_OPLOCK | BREAK_ANY)) == 0) {

					/*
					 * There could be a READ_CACHING-only
					 * oplock here. Those are broken later.
					 *
					 * If Oplock.State contains READ_CACHING
					 *  go to the LeaveBreakToNone label.
					 * Set BREAK_TO_NONE in Oplock.State.
					 */
					if ((nol->ol_state & READ_CACHING) != 0)
						goto LeaveBreakToNone;
					nol->ol_state |= BREAK_TO_NONE;

					/*
					 * Notify the server of an oplock break
					 * according to the algorithm in section
					 * 2.1.5.17.3, setting the algorithm's
					 * parameters as follows:
					 *	BreakingOplockOpen =
					 *	  Oplock.ExclusiveOpen.
					 *	NewOplockLevel = LEVEL_NONE.
					 *	AcknowledgeRequired = TRUE.
					 *	Commpl_Status = STATUS_SUCCESS.
					 * (The operation does not end at this
					 * point; this call to 2.1.5.17.3
					 * completes some earlier call to
					 * 2.1.5.17.1.)
					 */
					smb_oplock_ind_break(o,
					    LEVEL_NONE, B_TRUE,
					    NT_STATUS_SUCCESS);
				}

				/*
				 * Else If Oplock.State equals LEVEL_TWO_OPLOCK
				 *  or (LEVEL_TWO_OPLOCK|READ_CACHING):
				 */
				else if (nol->ol_state == LEVEL_TWO ||
				    nol->ol_state == (LEVEL_TWO|READ_CACHING)) {

					/*
					 * For each Open O in Oplock.IIOplocks:
					 *   Remove O from Oplock.IIOplocks.
					 *   Notify the server of an oplock
					 *    break according to the algorithm
					 *    in section 2.1.5.17.3, setting the
					 *    algorithm's parameters as follows:
					 *	BreakingOplockOpen = ThisOpen.
					 *	NewOplockLevel = LEVEL_NONE.
					 *	AcknowledgeRequired = FALSE.
					 *	Compl_Status = STATUS_SUCCESS.
					 *    (The operation does not end at
					 *    this point; this call to
					 *    2.1.5.17.3 completes some
					 *    earlier call to 2.1.5.17.2.)
					 * EndFor
					 */
					FOREACH_NODE_OFILE(node, o) {
						if (o->f_oplock.onlist_II == 0)
							continue;
						o->f_oplock.onlist_II = B_FALSE;
						nol->cnt_II--;
						ASSERT(nol->cnt_II >= 0);

						smb_oplock_ind_break(o,
						    LEVEL_NONE, B_FALSE,
						    NT_STATUS_SUCCESS);
					}
					/*
					 * If Oplock.State equals
					 *  (LEVEL_TWO_OPLOCK|READ_CACHING):
					 *	Set Oplock.State = READ_CACHING.
					 * Else
					 *	Set Oplock.State = NO_OPLOCK.
					 * EndIf
					 * Go to the LeaveBreakToNone label.
					 */
					if (nol->ol_state ==
					    (LEVEL_TWO_OPLOCK | READ_CACHING)) {
						nol->ol_state = READ_CACHING;
					} else {
						nol->ol_state = NO_OPLOCK;
					}
					goto LeaveBreakToNone;
				}

				/*
				 * Else If Oplock.State contains BREAK_TO_TWO:
				 *	Clear BREAK_TO_TWO from Oplock.State.
				 *	Set BREAK_TO_TWO_TO_NONE in Oplock.State
				 * EndIf
				 */
				else if (nol->ol_state & BREAK_TO_TWO) {
					nol->ol_state &= ~BREAK_TO_TWO;
					nol->ol_state |= BREAK_TO_TWO_TO_NONE;
				}

				/*
				 * If Oplock.ExclusiveOpen is not empty,
				 *  and Oplock.Excl_Open.TargetOplockKey
				 *  equals Open.TargetOplockKey,
				 *	 go to the LeaveBreakToNone label.
				 */
				if (o != NULL &&
				    CompareOplockKeys(ofile, o, CmpFlags))
					goto LeaveBreakToNone;

				/*
				 * The operation that called this algorithm
				 *  MUST be made cancelable by ...
				 * The operation that called this algorithm
				 * waits until the opl. break is acknowledged,
				 * as specified in section 2.1.5.18, or the
				 * operation is canceled.
				 */
				status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
				/* Caller does smb_oplock_wait_break() */
			}
		}
	}

LeaveBreakToNone:

	/*
	 * if (BreakCacheLevel != 0) and		(pp 37)
	 * If Oplock.State contains any flags that are in BreakCacheLevel:
	 * (Body of that "If" was here to just above the out label.)
	 */
	if ((nol->ol_state & BreakCacheLevel) == 0)
		goto out;

	/*
	 * If Oplock.ExclusiveOpen is not empty, call the
	 * algorithm in section 2.1.4.12.2, passing
	 *	Open as the OperationOpen parameter,
	 *	Oplock.ExclusiveOpen as the OplockOpen parameter,
	 *	and Flags as the Flagsparameter.
	 * If the algorithm returns TRUE:
	 *	The algorithm returns at this point.
	 */
	if ((o = nol->excl_open) != NULL &&
	    CompareOplockKeys(ofile, o, CmpFlags) == B_TRUE) {
		status = NT_STATUS_SUCCESS;
		goto out;
	}

	/*
	 * Switch (Oplock.State):
	 */
	switch (nol->ol_state) {

	case (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH):
	case READ_CACHING:
	case (LEVEL_TWO_OPLOCK|READ_CACHING):
		/*
		 * If BreakCacheLevel contains READ_CACHING:
		 */
		if ((BreakCacheLevel & READ_CACHING) != 0) {
			/*
			 * For each Open ThisOpen in Oplock.ROplocks:
			 *   Call the algorithm in section 2.1.4.12.2, pass:
			 *	Open as the OperationOpen parameter,
			 *	ThisOpen as the OplockOpen parameter,
			 *	and Flags as the Flagsparameter.
			 *   If the algorithm returns FALSE:
			 *	Remove ThisOpen from Oplock.ROplocks.
			 *	Notify the server of an oplock break
			 *	  according to the algorithm in
			 *	  section 2.1.5.17.3, setting the
			 *	  algorithm's parameters as follows:
			 *		BreakingOplockOpen = ThisOpen.
			 *		NewOplockLevel = LEVEL_NONE.
			 *		AcknowledgeRequired = FALSE.
			 *		Compl_Status = STATUS_SUCCESS.
			 *	(The operation does not end at this point;
			 *	 this call to 2.1.5.17.3 completes some
			 *	 earlier call to 2.1.5.17.2.)
			 *	EndIf
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_R == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					o->f_oplock.onlist_R = B_FALSE;
					nol->cnt_R--;
					ASSERT(nol->cnt_R >= 0);

					smb_oplock_ind_break(o,
					    LEVEL_NONE, B_FALSE,
					    NT_STATUS_SUCCESS);
				}
			}
		}
		/*
		 * If Oplock.State equals
		 *  (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH):
		 *	// Do nothing; FALL THROUGH to next Case statement.
		 * Else
		 *	Recompute Oplock.State according to the
		 *	algorithm in section 2.1.4.13, passing
		 *	Oplock as the ThisOplock parameter.
		 * EndIf
		 */
		if (nol->ol_state ==
		    (READ_CACHING|HANDLE_CACHING|MIXED_R_AND_RH))
			goto case_cache_rh;

		RecomputeOplockState(node);
		break;
	/* EndCase	XXX Note: spec. swapped this with prev. Endif. */

	case_cache_rh:
	case (READ_CACHING|HANDLE_CACHING):

		/*
		 * If BreakCacheLevel equals HANDLE_CACHING:
		 */
		if (BreakCacheLevel == HANDLE_CACHING) {

			/*
			 * For each Open ThisOpen in Oplock.RHOplocks:
			 *	If ThisOpen.OplockKey != Open.OplockKey:
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RH == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {

					/*
					 * Remove ThisOpen from
					 *  Oplock.RHOplocks.
					 */
					o->f_oplock.onlist_RH = B_FALSE;
					nol->cnt_RH--;
					ASSERT(nol->cnt_RH >= 0);

					/*
					 * Notify the server of an oplock break
					 *   according to the algorithm in
					 *   section 2.1.5.17.3, setting the
					 *   algorithm's parameters as follows:
					 *	BreakingOplockOpen = ThisOpen.
					 *	NewOplockLevel = READ_CACHING.
					 *	AcknowledgeRequired = TRUE.
					 *	Compl_Status = STATUS_SUCCESS.
					 * (The operation does not end at this
					 *  point; this call to 2.1.5.17.3
					 *  completes some earlier call to
					 *  2.1.5.17.2.)
					 */
					smb_oplock_ind_break(o,
					    READ_CACHING, B_TRUE,
					    NT_STATUS_SUCCESS);

					/*
					 * Initialize a new RHOpContext object,
					 *   setting its fields as follows:
					 *	RHOpCtx.Open = ThisOpen.
					 *	RHOpCtx.BreakingToRead = TRUE.
					 * Add the new RHOpContext object to
					 *    Oplock.RHBreakQueue.
					 * Set NeedToWait to TRUE.
					 */
					o->f_oplock.BreakingToRead = B_TRUE;
					ASSERT(!(o->f_oplock.onlist_RHBQ));
					o->f_oplock.onlist_RHBQ = B_TRUE;
					nol->cnt_RHBQ++;

					NeedToWait = B_TRUE;
				}
			}
		}

		/*
		 * Else If BreakCacheLevel contains both
		 *   READ_CACHING and WRITE_CACHING:
		 */
		else if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) ==
		    (READ_CACHING | WRITE_CACHING)) {

			/*
			 * For each RHOpContext ThisContext in
			 * Oplock.RHBreakQueue:
			 *	Call the algorithm in section 2.1.4.12.2,
			 *	  passing Open as the OperationOpen parameter,
			 *	  ThisContext.Open as the OplockOpen parameter,
			 *	  and Flags as the Flags parameter.
			 *	If the algorithm returns FALSE:
			 *		Set ThisContext.BreakingToRead to FALSE.
			 *		If BreakCacheLevel & HANDLE_CACHING:
			 *			Set NeedToWait to TRUE.
			 *		EndIf
			 *	EndIf
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RHBQ == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					o->f_oplock.BreakingToRead = B_FALSE;
					if (BreakCacheLevel & HANDLE_CACHING)
						NeedToWait = B_TRUE;
				}
			}

			/*
			 * For each Open ThisOpen in Oplock.RHOplocks:
			 *	Call the algorithm in section 2.1.4.12.2,
			 *	  passing Open as the OperationOpen parameter,
			 *	  ThisOpen as the OplockOpen parameter, and
			 *	  Flags as the Flagsparameter.
			 *	If the algorithm  returns FALSE:
			 *		Remove ThisOpen from Oplock.RHOplocks.
			 *		Notify the server of an oplock break
			 *		  according to the algorithm in
			 *		  section 2.1.5.17.3, setting the
			 *		  algorithm's parameters as follows:
			 *			BreakingOplockOpen = ThisOpen.
			 *			NewOplockLevel = LEVEL_NONE.
			 *			AcknowledgeRequired = TRUE.
			 *			Compl_Status = STATUS_SUCCESS.
			 *		(The operation does not end at this
			 *		 point; this call to 2.1.5.17.3
			 *		 completes some earlier call to
			 *		 2.1.5.17.2.)
			 *		Initialize a new RHOpContext object,
			 *		  setting its fields as follows:
			 *			RHOpCtx.Open = ThisOpen.
			 *			RHOpCtx.BreakingToRead = FALSE
			 *		Add the new RHOpContext object to
			 *		  Oplock.RHBreakQueue.
			 *		If BreakCacheLevel contains
			 *		  HANDLE_CACHING:
			 *			Set NeedToWait to TRUE.
			 *		EndIf
			 *	EndIf
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RH == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					o->f_oplock.onlist_RH = B_FALSE;
					nol->cnt_RH--;
					ASSERT(nol->cnt_RH >= 0);

					smb_oplock_ind_break(o,
					    LEVEL_NONE, B_TRUE,
					    NT_STATUS_SUCCESS);

					o->f_oplock.BreakingToRead = B_FALSE;
					ASSERT(!(o->f_oplock.onlist_RHBQ));
					o->f_oplock.onlist_RHBQ = B_TRUE;
					nol->cnt_RHBQ++;

					if (BreakCacheLevel & HANDLE_CACHING)
						NeedToWait = B_TRUE;
				}
			}
		}

// If the oplock is explicitly losing HANDLE_CACHING, RHBreakQueue is
// not empty, and the algorithm has not yet decided to wait, this operation
// might have to wait if there is an oplock on RHBreakQueue with a
// non-matching key. This is done because even if this operation didn't
// cause a break of a currently-granted Read-Handle caching oplock, it
// might have done so had a currently-breaking oplock still been granted.

		/*
		 * If (NeedToWait is FALSE) and
		 *   (Oplock.RHBreakQueue is empty) and   (XXX: Not empty)
		 *   (BreakCacheLevel contains HANDLE_CACHING):
		 *	For each RHOpContext ThisContex in Oplock.RHBreakQueue:
		 *		If ThisContext.Open.OplockKey != Open.OplockKey:
		 *			Set NeedToWait to TRUE.
		 *			Break out of the For loop.
		 *		EndIf
		 *	EndFor
		 * EndIf
		 * Recompute Oplock.State according to the algorithm in
		 *   section 2.1.4.13, passing Oplock as ThisOplock.
		 */
		if (NeedToWait == B_FALSE &&
		    (BreakCacheLevel & HANDLE_CACHING) != 0) {
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RHBQ == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					NeedToWait = B_TRUE;
					break;
				}
			}
		}
		RecomputeOplockState(node);
		break;

	case (READ_CACHING|HANDLE_CACHING|BREAK_TO_READ_CACHING):
		/*
		 * If BreakCacheLevel contains READ_CACHING:
		 */
		if ((BreakCacheLevel & READ_CACHING) != 0) {
			/*
			 * For each RHOpContext ThisContext in
			 *  Oplock.RHBreakQueue:
			 *	Call the algorithm in section 2.1.4.12.2,
			 *	  passing Open = OperationOpen parameter,
			 *	  ThisContext.Open = OplockOpen parameter,
			 *	  and Flags as the Flags parameter.
			 *	If the algorithm returns FALSE:
			 *		Set ThisCtx.BreakingToRead = FALSE.
			 *	EndIf
			 *	Recompute Oplock.State according to the
			 *	  algorithm in section 2.1.4.13, passing
			 *	  Oplock as the ThisOplock parameter.
			 * EndFor
			 */
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RHBQ == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					o->f_oplock.BreakingToRead = B_FALSE;
				}
			}
			RecomputeOplockState(node);
		}
		/* FALLTHROUGH */

	case (READ_CACHING|HANDLE_CACHING|BREAK_TO_NO_CACHING):
		/*
		 * If BreakCacheLevel contains HANDLE_CACHING:
		 *	For each RHOpContext ThisContext in Oplock.RHBreakQueue:
		 *		If ThisContext.Open.OplockKey != Open.OplockKey:
		 *			Set NeedToWait to TRUE.
		 *			Break out of the For loop.
		 *		EndIf
		 *	EndFor
		 * EndIf
		 */
		if ((BreakCacheLevel & HANDLE_CACHING) != 0) {
			FOREACH_NODE_OFILE(node, o) {
				if (o->f_oplock.onlist_RHBQ == 0)
					continue;
				if (!CompareOplockKeys(ofile, o, CmpFlags)) {
					NeedToWait = B_TRUE;
					break;
				}
			}
		}
		break;

	case (READ_CACHING|WRITE_CACHING|EXCLUSIVE):
		/*
		 * If BreakCacheLevel contains both
		 *  READ_CACHING and WRITE_CACHING:
		 *	Notify the server of an oplock break according to
		 *	  the algorithm in section 2.1.5.17.3, setting the
		 *	  algorithm's parameters as follows:
		 *		BreakingOplockOpen = Oplock.ExclusiveOpen.
		 *		NewOplockLevel = LEVEL_NONE.
		 *		AcknowledgeRequired = TRUE.
		 *		OplockCompletionStatus = STATUS_SUCCESS.
		 *	(The operation does not end at this point;
		 *	 this call to 2.1.5.17.3 completes some
		 *	 earlier call to 2.1.5.17.1.)
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING| \
		 *			EXCLUSIVE|BREAK_TO_NO_CACHING).
		 *	Set NeedToWait to TRUE.
		 */
		if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) ==
		    (READ_CACHING | WRITE_CACHING)) {
			o = nol->excl_open;
			ASSERT(o != NULL);
			smb_oplock_ind_break(o,
			    LEVEL_NONE, B_TRUE,
			    NT_STATUS_SUCCESS);

			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    EXCLUSIVE|BREAK_TO_NO_CACHING);
			NeedToWait = B_TRUE;
		}

		/*
		 * Else If BreakCacheLevel contains WRITE_CACHING:
		 *	Notify the server of an oplock break according to
		 *	  the algorithm in section 2.1.5.17.3, setting the
		 *	  algorithm's parameters as follows:
		 *		BreakingOplockOpen = Oplock.ExclusiveOpen.
		 *		NewOplockLevel = READ_CACHING.
		 *		AcknowledgeRequired = TRUE.
		 *		OplockCompletionStatus = STATUS_SUCCESS.
		 *	(The operation does not end at this point;
		 *	 this call to 2.1.5.17.3 completes some
		 *	 earlier call to 2.1.5.17.1.)
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			 EXCLUSIVE|BREAK_TO_READ_CACHING).
		 *	Set NeedToWait to TRUE.
		 * EndIf
		 */
		else if ((BreakCacheLevel & WRITE_CACHING) != 0) {
			o = nol->excl_open;
			ASSERT(o != NULL);
			smb_oplock_ind_break(o,
			    READ_CACHING, B_TRUE,
			    NT_STATUS_SUCCESS);

			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    EXCLUSIVE|BREAK_TO_READ_CACHING);
			NeedToWait = B_TRUE;
		}
		break;

	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE):
		/*
		 * If BreakCacheLevel equals WRITE_CACHING:
		 *	Notify the server of an oplock break according to
		 *	  the algorithm in section 2.1.5.17.3, setting the
		 *	  algorithm's parameters as follows:
		 *		BreakingOplockOpen = Oplock.ExclusiveOpen.
		 *		NewOplockLevel = (READ_CACHING|HANDLE_CACHING).
		 *		AcknowledgeRequired = TRUE.
		 *		OplockCompletionStatus = STATUS_SUCCESS.
		 *	(The operation does not end at this point;
		 *	 this call to 2.1.5.17.3 completes some
		 *	 earlier call to 2.1.5.17.1.)
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_READ_CACHING|
		 *			BREAK_TO_HANDLE_CACHING).
		 *	Set NeedToWait to TRUE.
		 */
		if (BreakCacheLevel == WRITE_CACHING) {
			o = nol->excl_open;
			ASSERT(o != NULL);
			smb_oplock_ind_break(o,
			    CACHE_RH, B_TRUE,
			    NT_STATUS_SUCCESS);

			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|
			    EXCLUSIVE|BREAK_TO_READ_CACHING|
			    BREAK_TO_HANDLE_CACHING);
			NeedToWait = B_TRUE;
		}

		/*
		 * Else If BreakCacheLevel equals HANDLE_CACHING:
		 *	Notify the server of an oplock break according to
		 *	  the algorithm in section 2.1.5.17.3, setting the
		 *	  algorithm's parameters as follows:
		 *		BreakingOplockOpen = Oplock.ExclusiveOpen.
		 *		NewOplockLevel = (READ_CACHING|WRITE_CACHING).
		 *		AcknowledgeRequired = TRUE.
		 *		OplockCompletionStatus = STATUS_SUCCESS.
		 *	(The operation does not end at this point;
		 *	 this call to 2.1.5.17.3 completes some
		 *	 earlier call to 2.1.5.17.1.)
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_READ_CACHING|
		 *			BREAK_TO_WRITE_CACHING).
		 *	Set NeedToWait to TRUE.
		 */
		else if (BreakCacheLevel == HANDLE_CACHING) {
			o = nol->excl_open;
			ASSERT(o != NULL);
			smb_oplock_ind_break(o,
			    CACHE_RW, B_TRUE,
			    NT_STATUS_SUCCESS);

			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|
			    EXCLUSIVE|BREAK_TO_READ_CACHING|
			    BREAK_TO_WRITE_CACHING);
			NeedToWait = B_TRUE;
		}

		/*
		 * Else If BreakCacheLevel contains both
		 *  READ_CACHING and WRITE_CACHING:
		 *	Notify the server of an oplock break according to
		 *	  the algorithm in section 2.1.5.17.3, setting the
		 *	  algorithm's parameters as follows:
		 *		BreakingOplockOpen = Oplock.ExclusiveOpen.
		 *		NewOplockLevel = LEVEL_NONE.
		 *		AcknowledgeRequired = TRUE.
		 *		OplockCompletionStatus = STATUS_SUCCESS.
		 *	(The operation does not end at this point;
		 *	 this call to 2.1.5.17.3 completes some
		 *	 earlier call to 2.1.5.17.1.)
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_NO_CACHING).
		 *	Set NeedToWait to TRUE.
		 * EndIf
		 */
		else if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) ==
		    (READ_CACHING | WRITE_CACHING)) {
			o = nol->excl_open;
			ASSERT(o != NULL);
			smb_oplock_ind_break(o,
			    LEVEL_NONE, B_TRUE,
			    NT_STATUS_SUCCESS);

			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|
			    EXCLUSIVE|BREAK_TO_NO_CACHING);
			NeedToWait = B_TRUE;
		}
		break;

	case (READ_CACHING|WRITE_CACHING|EXCLUSIVE|BREAK_TO_READ_CACHING):
		/*
		 * If BreakCacheLevel contains READ_CACHING:
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			EXCLUSIVE|BREAK_TO_NO_CACHING).
		 * EndIf
		 * If BreakCacheLevel contains either
		 *  READ_CACHING or WRITE_CACHING:
		 *	Set NeedToWait to TRUE.
		 * EndIf
		 */
		if ((BreakCacheLevel & READ_CACHING) != 0) {
			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    EXCLUSIVE|BREAK_TO_NO_CACHING);
		}
		if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) != 0) {
			NeedToWait = B_TRUE;
		}
		break;

	case (READ_CACHING|WRITE_CACHING|EXCLUSIVE|BREAK_TO_NO_CACHING):
		/*
		 * If BreakCacheLevel contains either
		 *  READ_CACHING or WRITE_CACHING:
		 *	Set NeedToWait to TRUE.
		 * EndIf
		 */
		if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) != 0) {
			NeedToWait = B_TRUE;
		}
		break;

	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING|BREAK_TO_WRITE_CACHING):
		/*
		 * If BreakCacheLevel == WRITE_CACHING:
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *	    HANDLE_CACHING|EXCLUSIVE|BREAK_TO_READ_CACHING).
		 * Else If BreakCacheLevel contains both
		 *  READ_CACHING and WRITE_CACHING:
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *	    HANDLE_CACHING|EXCLUSIVE|BREAK_TO_NO_CACHING).
		 * EndIf
		 * Set NeedToWait to TRUE.
		 */
		if (BreakCacheLevel == WRITE_CACHING) {
			nol->ol_state = (READ_CACHING|WRITE_CACHING|
			    HANDLE_CACHING|EXCLUSIVE|BREAK_TO_READ_CACHING);
		}
		else if ((BreakCacheLevel & (READ_CACHING | WRITE_CACHING)) ==
		    (READ_CACHING | WRITE_CACHING)) {
			nol->ol_state = (READ_CACHING|WRITE_CACHING|
			    HANDLE_CACHING|EXCLUSIVE|BREAK_TO_NO_CACHING);
		}
		NeedToWait = B_TRUE;
		break;

	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING|BREAK_TO_HANDLE_CACHING):
		/*
		 * If BreakCacheLevel == HANDLE_CACHING:
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_READ_CACHING).
		 * Else If BreakCacheLevel contains READ_CACHING:
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_NO_CACHING).
		 * EndIf
		 * Set NeedToWait to TRUE.
		 */
		if (BreakCacheLevel == HANDLE_CACHING) {
			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    HANDLE_CACHING|EXCLUSIVE|
			    BREAK_TO_READ_CACHING);
		}
		else if ((BreakCacheLevel & READ_CACHING) != 0) {
			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    HANDLE_CACHING|EXCLUSIVE|
			    BREAK_TO_NO_CACHING);
		}
		NeedToWait = B_TRUE;
		break;

	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_READ_CACHING):
		/*
		 * If BreakCacheLevel contains READ_CACHING,
		 *	Set Oplock.State to (READ_CACHING|WRITE_CACHING|
		 *			HANDLE_CACHING|EXCLUSIVE|
		 *			BREAK_TO_NO_CACHING).
		 * EndIf
		 * Set NeedToWait to TRUE.
		 */
		if ((BreakCacheLevel & READ_CACHING) != 0) {
			nol->ol_state =
			    (READ_CACHING|WRITE_CACHING|
			    HANDLE_CACHING|EXCLUSIVE|
			    BREAK_TO_NO_CACHING);
		}
		NeedToWait = B_TRUE;
		break;

	case (READ_CACHING|WRITE_CACHING|HANDLE_CACHING|EXCLUSIVE|
	    BREAK_TO_NO_CACHING):
		NeedToWait = B_TRUE;
		break;

	} /* Switch */

	if (NeedToWait) {
		/*
		 * The operation that called this algorithm MUST be
		 *   made cancelable by inserting it into
		 *   CancelableOperations.CancelableOperationList.
		 * The operation that called this algorithm waits until
		 *   the oplock break is acknowledged, as specified in
		 *   section 2.1.5.18, or the operation is canceled.
		 */
		status = NT_STATUS_OPLOCK_BREAK_IN_PROGRESS;
		/* Caller does smb_oplock_wait_break() */
	}

out:
	mutex_exit(&node->n_oplock.ol_mutex);
	smb_llist_exit(&node->n_ofile_list);

	return (status);
}

/*
 * smb_oplock_move()
 *
 * Helper function for smb2_lease_ofile_close, where we're closing the
 * ofile that has the oplock for a given lease, and need to move that
 * oplock to another handle with the same lease.
 *
 * This is not described in [MS-FSA], so presumably Windows does this
 * by keeping oplock objects separate from the open files (no action
 * needed in the FSA layer).  We keep the oplock state as part of the
 * ofile, so we need to relocate the oplock state in this case.
 *
 * Note that in here, we're moving state for both the FSA level and
 * the SMB level (which is unusual) but this is the easiest way to
 * make sure we move the state without any other effects.
 */
void
smb_oplock_move(smb_node_t *node,
    smb_ofile_t *fr_ofile, smb_ofile_t *to_ofile)
{
	/*
	 * These are the two common states for an ofile with
	 * a lease that's not the one holding the oplock.
	 * Log if it's not either of these.
	 */
	static const smb_oplock_grant_t og0 = { 0 };
	static const smb_oplock_grant_t og8 = {
	    .og_state = OPLOCK_LEVEL_GRANULAR, 0 };
	smb_oplock_grant_t og_tmp;

	ASSERT(fr_ofile->f_node == node);
	ASSERT(to_ofile->f_node == node);
	ASSERT(MUTEX_HELD(&node->n_oplock.ol_mutex));

	/*
	 * The ofile to which we're moving the oplock
	 * should NOT have any oplock state.  However,
	 * as long as we just swap state between the
	 * two oplocks, we won't invalidate any of
	 * the node's "onlist" counts etc.
	 */
	if (bcmp(&to_ofile->f_oplock, &og0, sizeof (og0)) != 0 &&
	    bcmp(&to_ofile->f_oplock, &og8, sizeof (og8)) != 0) {
#ifdef	DEBUG
		cmn_err(CE_NOTE, "smb_oplock_move: not empty?");
#endif
		DTRACE_PROBE2(dst__not__empty,
		    smb_node_t *, node, smb_ofile_t *, to_ofile);
	}

	og_tmp = to_ofile->f_oplock;
	to_ofile->f_oplock = fr_ofile->f_oplock;
	fr_ofile->f_oplock = og_tmp;

	if (node->n_oplock.excl_open == fr_ofile)
		node->n_oplock.excl_open = to_ofile;

}