summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/fs/nfsclient/nfs_clstate.c
blob: 6a93b18335f1b4fd14baf566c1ab923e09edcbff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
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
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
#include <machine/rtems-bsd-kernel-space.h>

/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2009 Rick Macklem, University of Guelph
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

/*
 * These functions implement the client side state handling for NFSv4.
 * NFSv4 state handling:
 * - A lockowner is used to determine lock contention, so it
 *   corresponds directly to a Posix pid. (1 to 1 mapping)
 * - The correct granularity of an OpenOwner is not nearly so
 *   obvious. An OpenOwner does the following:
 *   - provides a serial sequencing of Open/Close/Lock-with-new-lockowner
 *   - is used to check for Open/Share contention (not applicable to
 *     this client, since all Opens are Deny_None)
 *   As such, I considered both extreme.
 *   1 OpenOwner per ClientID - Simple to manage, but fully serializes
 *   all Open, Close and Lock (with a new lockowner) Ops.
 *   1 OpenOwner for each Open - This one results in an OpenConfirm for
 *   every Open, for most servers.
 *   So, I chose to use the same mapping as I did for LockOwnwers.
 *   The main concern here is that you can end up with multiple Opens
 *   for the same File Handle, but on different OpenOwners (opens
 *   inherited from parents, grandparents...) and you do not know
 *   which of these the vnodeop close applies to. This is handled by
 *   delaying the Close Op(s) until all of the Opens have been closed.
 *   (It is not yet obvious if this is the correct granularity.)
 * - How the code handles serialization:
 *   - For the ClientId, it uses an exclusive lock while getting its
 *     SetClientId and during recovery. Otherwise, it uses a shared
 *     lock via a reference count.
 *   - For the rest of the data structures, it uses an SMP mutex
 *     (once the nfs client is SMP safe) and doesn't sleep while
 *     manipulating the linked lists.
 *   - The serialization of Open/Close/Lock/LockU falls out in the
 *     "wash", since OpenOwners and LockOwners are both mapped from
 *     Posix pid. In other words, there is only one Posix pid using
 *     any given owner, so that owner is serialized. (If you change
 *     the granularity of the OpenOwner, then code must be added to
 *     serialize Ops on the OpenOwner.)
 * - When to get rid of OpenOwners and LockOwners.
 *   - The function nfscl_cleanup_common() is executed after a process exits.
 *     It goes through the client list looking for all Open and Lock Owners.
 *     When one is found, it is marked "defunct" or in the case of
 *     an OpenOwner without any Opens, freed.
 *     The renew thread scans for defunct Owners and gets rid of them,
 *     if it can. The LockOwners will also be deleted when the
 *     associated Open is closed.
 *   - If the LockU or Close Op(s) fail during close in a way
 *     that could be recovered upon retry, they are relinked to the
 *     ClientId's defunct open list and retried by the renew thread
 *     until they succeed or an unmount/recovery occurs.
 *     (Since we are done with them, they do not need to be recovered.)
 */

#ifndef APPLEKEXT
#include <fs/nfs/nfsport.h>

/*
 * Global variables
 */
extern struct nfsstatsv1 nfsstatsv1;
extern struct nfsreqhead nfsd_reqq;
extern u_int32_t newnfs_false, newnfs_true;
extern int nfscl_debuglevel;
extern int nfscl_enablecallb;
extern int nfs_numnfscbd;
NFSREQSPINLOCK;
NFSCLSTATEMUTEX;
int nfscl_inited = 0;
struct nfsclhead nfsclhead;	/* Head of clientid list */
int nfscl_deleghighwater = NFSCLDELEGHIGHWATER;
int nfscl_layouthighwater = NFSCLLAYOUTHIGHWATER;
#endif	/* !APPLEKEXT */

static int nfscl_delegcnt = 0;
static int nfscl_layoutcnt = 0;
static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
    u_int8_t *, u_int32_t, struct nfscllockowner **, struct nfsclopen **);
static void nfscl_clrelease(struct nfsclclient *);
static void nfscl_cleanclient(struct nfsclclient *);
static void nfscl_expireclient(struct nfsclclient *, struct nfsmount *,
    struct ucred *, NFSPROC_T *);
static int nfscl_expireopen(struct nfsclclient *, struct nfsclopen *,
    struct nfsmount *, struct ucred *, NFSPROC_T *);
static void nfscl_recover(struct nfsclclient *, struct ucred *, NFSPROC_T *);
static void nfscl_insertlock(struct nfscllockowner *, struct nfscllock *,
    struct nfscllock *, int);
static int nfscl_updatelock(struct nfscllockowner *, struct nfscllock **,
    struct nfscllock **, int);
static void nfscl_delegreturnall(struct nfsclclient *, NFSPROC_T *);
static u_int32_t nfscl_nextcbident(void);
static mount_t nfscl_getmnt(int, uint8_t *, u_int32_t, struct nfsclclient **);
static struct nfsclclient *nfscl_getclnt(u_int32_t);
static struct nfsclclient *nfscl_getclntsess(uint8_t *);
static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
    int);
static void nfscl_retoncloselayout(vnode_t, struct nfsclclient *, uint8_t *,
    int, struct nfsclrecalllayout **);
static void nfscl_reldevinfo_locked(struct nfscldevinfo *);
static struct nfscllayout *nfscl_findlayout(struct nfsclclient *, u_int8_t *,
    int);
static struct nfscldevinfo *nfscl_finddevinfo(struct nfsclclient *, uint8_t *);
static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
    u_int8_t *, struct nfscllock **);
static void nfscl_freealllocks(struct nfscllockownerhead *, int);
static int nfscl_localconflict(struct nfsclclient *, u_int8_t *, int,
    struct nfscllock *, u_int8_t *, struct nfscldeleg *, struct nfscllock **);
static void nfscl_newopen(struct nfsclclient *, struct nfscldeleg *,
    struct nfsclowner **, struct nfsclowner **, struct nfsclopen **,
    struct nfsclopen **, u_int8_t *, u_int8_t *, int, struct ucred *, int *);
static int nfscl_moveopen(vnode_t , struct nfsclclient *,
    struct nfsmount *, struct nfsclopen *, struct nfsclowner *,
    struct nfscldeleg *, struct ucred *, NFSPROC_T *);
static void nfscl_totalrecall(struct nfsclclient *);
static int nfscl_relock(vnode_t , struct nfsclclient *, struct nfsmount *,
    struct nfscllockowner *, struct nfscllock *, struct ucred *, NFSPROC_T *);
static int nfscl_tryopen(struct nfsmount *, vnode_t , u_int8_t *, int,
    u_int8_t *, int, u_int32_t, struct nfsclopen *, u_int8_t *, int,
    struct nfscldeleg **, int, u_int32_t, struct ucred *, NFSPROC_T *);
static int nfscl_trylock(struct nfsmount *, vnode_t , u_int8_t *,
    int, struct nfscllockowner *, int, int, u_int64_t, u_int64_t, short,
    struct ucred *, NFSPROC_T *);
static int nfsrpc_reopen(struct nfsmount *, u_int8_t *, int, u_int32_t,
    struct nfsclopen *, struct nfscldeleg **, struct ucred *, NFSPROC_T *);
static void nfscl_freedeleg(struct nfscldeleghead *, struct nfscldeleg *);
static int nfscl_errmap(struct nfsrv_descript *, u_int32_t);
static void nfscl_cleanup_common(struct nfsclclient *, u_int8_t *);
static int nfscl_recalldeleg(struct nfsclclient *, struct nfsmount *,
    struct nfscldeleg *, vnode_t, struct ucred *, NFSPROC_T *, int);
static void nfscl_freeopenowner(struct nfsclowner *, int);
static void nfscl_cleandeleg(struct nfscldeleg *);
static int nfscl_trydelegreturn(struct nfscldeleg *, struct ucred *,
    struct nfsmount *, NFSPROC_T *);
static void nfscl_emptylockowner(struct nfscllockowner *,
    struct nfscllockownerfhhead *);
static void nfscl_mergeflayouts(struct nfsclflayouthead *,
    struct nfsclflayouthead *);
static int nfscl_layoutrecall(int, struct nfscllayout *, uint32_t, uint64_t,
    uint64_t, uint32_t, uint32_t, uint32_t, char *, struct nfsclrecalllayout *);
static int nfscl_seq(uint32_t, uint32_t);
static void nfscl_layoutreturn(struct nfsmount *, struct nfscllayout *,
    struct ucred *, NFSPROC_T *);
static void nfscl_dolayoutcommit(struct nfsmount *, struct nfscllayout *,
    struct ucred *, NFSPROC_T *);

static short nfscberr_null[] = {
	0,
	0,
};

static short nfscberr_getattr[] = {
	NFSERR_RESOURCE,
	NFSERR_BADHANDLE,
	NFSERR_BADXDR,
	NFSERR_RESOURCE,
	NFSERR_SERVERFAULT,
	0,
};

static short nfscberr_recall[] = {
	NFSERR_RESOURCE,
	NFSERR_BADHANDLE,
	NFSERR_BADSTATEID,
	NFSERR_BADXDR,
	NFSERR_RESOURCE,
	NFSERR_SERVERFAULT,
	0,
};

static short *nfscl_cberrmap[] = {
	nfscberr_null,
	nfscberr_null,
	nfscberr_null,
	nfscberr_getattr,
	nfscberr_recall
};

#define	NETFAMILY(clp) \
		(((clp)->nfsc_flags & NFSCLFLAGS_AFINET6) ? AF_INET6 : AF_INET)

/*
 * Called for an open operation.
 * If the nfhp argument is NULL, just get an openowner.
 */
APPLESTATIC int
nfscl_open(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t amode, int usedeleg,
    struct ucred *cred, NFSPROC_T *p, struct nfsclowner **owpp,
    struct nfsclopen **opp, int *newonep, int *retp, int lockit)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op = NULL, *nop = NULL;
	struct nfscldeleg *dp;
	struct nfsclownerhead *ohp;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int ret;

	if (newonep != NULL)
		*newonep = 0;
	if (opp != NULL)
		*opp = NULL;
	if (owpp != NULL)
		*owpp = NULL;

	/*
	 * Might need one or both of these, so MALLOC them now, to
	 * avoid a tsleep() in MALLOC later.
	 */
	nowp = malloc(sizeof (struct nfsclowner),
	    M_NFSCLOWNER, M_WAITOK);
	if (nfhp != NULL)
	    nop = malloc(sizeof (struct nfsclopen) +
		fhlen - 1, M_NFSCLOPEN, M_WAITOK);
	ret = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
	if (ret != 0) {
		free(nowp, M_NFSCLOWNER);
		if (nop != NULL)
			free(nop, M_NFSCLOPEN);
		return (ret);
	}

	/*
	 * Get the Open iff it already exists.
	 * If none found, add the new one or return error, depending upon
	 * "create".
	 */
	NFSLOCKCLSTATE();
	dp = NULL;
	/* First check the delegation list */
	if (nfhp != NULL && usedeleg) {
		LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
			if (dp->nfsdl_fhlen == fhlen &&
			    !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
				if (!(amode & NFSV4OPEN_ACCESSWRITE) ||
				    (dp->nfsdl_flags & NFSCLDL_WRITE))
					break;
				dp = NULL;
				break;
			}
		}
	}

	if (dp != NULL) {
		nfscl_filllockowner(p->td_proc, own, F_POSIX);
		ohp = &dp->nfsdl_owner;
	} else {
		/* For NFSv4.1 and this option, use a single open_owner. */
		if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
			nfscl_filllockowner(NULL, own, F_POSIX);
		else
			nfscl_filllockowner(p->td_proc, own, F_POSIX);
		ohp = &clp->nfsc_owner;
	}
	/* Now, search for an openowner */
	LIST_FOREACH(owp, ohp, nfsow_list) {
		if (!NFSBCMP(owp->nfsow_owner, own, NFSV4CL_LOCKNAMELEN))
			break;
	}

	/*
	 * Create a new open, as required.
	 */
	nfscl_newopen(clp, dp, &owp, &nowp, &op, &nop, own, nfhp, fhlen,
	    cred, newonep);

	/*
	 * Now, check the mode on the open and return the appropriate
	 * value.
	 */
	if (retp != NULL) {
		if (nfhp != NULL && dp != NULL && nop == NULL)
			/* new local open on delegation */
			*retp = NFSCLOPEN_SETCRED;
		else
			*retp = NFSCLOPEN_OK;
	}
	if (op != NULL && (amode & ~(op->nfso_mode))) {
		op->nfso_mode |= amode;
		if (retp != NULL && dp == NULL)
			*retp = NFSCLOPEN_DOOPEN;
	}

	/*
	 * Serialize modifications to the open owner for multiple threads
	 * within the same process using a read/write sleep lock.
	 * For NFSv4.1 and a single OpenOwner, allow concurrent open operations
	 * by acquiring a shared lock.  The close operations still use an
	 * exclusive lock for this case.
	 */
	if (lockit != 0) {
		if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp)))) {
			/*
			 * Get a shared lock on the OpenOwner, but first
			 * wait for any pending exclusive lock, so that the
			 * exclusive locker gets priority.
			 */
			nfsv4_lock(&owp->nfsow_rwlock, 0, NULL,
			    NFSCLSTATEMUTEXPTR, NULL);
			nfsv4_getref(&owp->nfsow_rwlock, NULL,
			    NFSCLSTATEMUTEXPTR, NULL);
		} else
			nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
	}
	NFSUNLOCKCLSTATE();
	if (nowp != NULL)
		free(nowp, M_NFSCLOWNER);
	if (nop != NULL)
		free(nop, M_NFSCLOPEN);
	if (owpp != NULL)
		*owpp = owp;
	if (opp != NULL)
		*opp = op;
	return (0);
}

/*
 * Create a new open, as required.
 */
static void
nfscl_newopen(struct nfsclclient *clp, struct nfscldeleg *dp,
    struct nfsclowner **owpp, struct nfsclowner **nowpp, struct nfsclopen **opp,
    struct nfsclopen **nopp, u_int8_t *own, u_int8_t *fhp, int fhlen,
    struct ucred *cred, int *newonep)
{
	struct nfsclowner *owp = *owpp, *nowp;
	struct nfsclopen *op, *nop;

	if (nowpp != NULL)
		nowp = *nowpp;
	else
		nowp = NULL;
	if (nopp != NULL)
		nop = *nopp;
	else
		nop = NULL;
	if (owp == NULL && nowp != NULL) {
		NFSBCOPY(own, nowp->nfsow_owner, NFSV4CL_LOCKNAMELEN);
		LIST_INIT(&nowp->nfsow_open);
		nowp->nfsow_clp = clp;
		nowp->nfsow_seqid = 0;
		nowp->nfsow_defunct = 0;
		nfscl_lockinit(&nowp->nfsow_rwlock);
		if (dp != NULL) {
			nfsstatsv1.cllocalopenowners++;
			LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
		} else {
			nfsstatsv1.clopenowners++;
			LIST_INSERT_HEAD(&clp->nfsc_owner, nowp, nfsow_list);
		}
		owp = *owpp = nowp;
		*nowpp = NULL;
		if (newonep != NULL)
			*newonep = 1;
	}

	 /* If an fhp has been specified, create an Open as well. */
	if (fhp != NULL) {
		/* and look for the correct open, based upon FH */
		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			if (op->nfso_fhlen == fhlen &&
			    !NFSBCMP(op->nfso_fh, fhp, fhlen))
				break;
		}
		if (op == NULL && nop != NULL) {
			nop->nfso_own = owp;
			nop->nfso_mode = 0;
			nop->nfso_opencnt = 0;
			nop->nfso_posixlock = 1;
			nop->nfso_fhlen = fhlen;
			NFSBCOPY(fhp, nop->nfso_fh, fhlen);
			LIST_INIT(&nop->nfso_lock);
			nop->nfso_stateid.seqid = 0;
			nop->nfso_stateid.other[0] = 0;
			nop->nfso_stateid.other[1] = 0;
			nop->nfso_stateid.other[2] = 0;
			KASSERT(cred != NULL, ("%s: cred NULL\n", __func__));
			newnfs_copyincred(cred, &nop->nfso_cred);
			if (dp != NULL) {
				TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
				TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
				    nfsdl_list);
				dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
				nfsstatsv1.cllocalopens++;
			} else {
				nfsstatsv1.clopens++;
			}
			LIST_INSERT_HEAD(&owp->nfsow_open, nop, nfso_list);
			*opp = nop;
			*nopp = NULL;
			if (newonep != NULL)
				*newonep = 1;
		} else {
			*opp = op;
		}
	}
}

/*
 * Called to find/add a delegation to a client.
 */
APPLESTATIC int
nfscl_deleg(mount_t mp, struct nfsclclient *clp, u_int8_t *nfhp,
    int fhlen, struct ucred *cred, NFSPROC_T *p, struct nfscldeleg **dpp)
{
	struct nfscldeleg *dp = *dpp, *tdp;

	/*
	 * First, if we have received a Read delegation for a file on a
	 * read/write file system, just return it, because they aren't
	 * useful, imho.
	 */
	if (mp != NULL && dp != NULL && !NFSMNT_RDONLY(mp) &&
	    (dp->nfsdl_flags & NFSCLDL_READ)) {
		(void) nfscl_trydelegreturn(dp, cred, VFSTONFS(mp), p);
		free(dp, M_NFSCLDELEG);
		*dpp = NULL;
		return (0);
	}

	/* Look for the correct deleg, based upon FH */
	NFSLOCKCLSTATE();
	tdp = nfscl_finddeleg(clp, nfhp, fhlen);
	if (tdp == NULL) {
		if (dp == NULL) {
			NFSUNLOCKCLSTATE();
			return (NFSERR_BADSTATEID);
		}
		*dpp = NULL;
		TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
		LIST_INSERT_HEAD(NFSCLDELEGHASH(clp, nfhp, fhlen), dp,
		    nfsdl_hash);
		dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
		nfsstatsv1.cldelegates++;
		nfscl_delegcnt++;
	} else {
		/*
		 * Delegation already exists, what do we do if a new one??
		 */
		if (dp != NULL) {
			printf("Deleg already exists!\n");
			free(dp, M_NFSCLDELEG);
			*dpp = NULL;
		} else {
			*dpp = tdp;
		}
	}
	NFSUNLOCKCLSTATE();
	return (0);
}

/*
 * Find a delegation for this file handle. Return NULL upon failure.
 */
static struct nfscldeleg *
nfscl_finddeleg(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
{
	struct nfscldeleg *dp;

	LIST_FOREACH(dp, NFSCLDELEGHASH(clp, fhp, fhlen), nfsdl_hash) {
	    if (dp->nfsdl_fhlen == fhlen &&
		!NFSBCMP(dp->nfsdl_fh, fhp, fhlen))
		break;
	}
	return (dp);
}

/*
 * Get a stateid for an I/O operation. First, look for an open and iff
 * found, return either a lockowner stateid or the open stateid.
 * If no Open is found, just return error and the special stateid of all zeros.
 */
APPLESTATIC int
nfscl_getstateid(vnode_t vp, u_int8_t *nfhp, int fhlen, u_int32_t mode,
    int fords, struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
    void **lckpp)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;
	struct nfsclopen *op = NULL, *top;
	struct nfscllockowner *lp;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	struct nfsmount *nmp;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int error, done;

	*lckpp = NULL;
	/*
	 * Initially, just set the special stateid of all zeros.
	 * (Don't do this for a DS, since the special stateid can't be used.)
	 */
	if (fords == 0) {
		stateidp->seqid = 0;
		stateidp->other[0] = 0;
		stateidp->other[1] = 0;
		stateidp->other[2] = 0;
	}
	if (vnode_vtype(vp) != VREG)
		return (EISDIR);
	np = VTONFS(vp);
	nmp = VFSTONFS(vnode_mount(vp));
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (EACCES);
	}

	/*
	 * Wait for recovery to complete.
	 */
	while ((clp->nfsc_flags & NFSCLFLAGS_RECVRINPROG))
		(void) nfsmsleep(&clp->nfsc_flags, NFSCLSTATEMUTEXPTR,
		    PZERO, "nfsrecvr", NULL);

	/*
	 * First, look for a delegation.
	 */
	LIST_FOREACH(dp, NFSCLDELEGHASH(clp, nfhp, fhlen), nfsdl_hash) {
		if (dp->nfsdl_fhlen == fhlen &&
		    !NFSBCMP(nfhp, dp->nfsdl_fh, fhlen)) {
			if (!(mode & NFSV4OPEN_ACCESSWRITE) ||
			    (dp->nfsdl_flags & NFSCLDL_WRITE)) {
				stateidp->seqid = dp->nfsdl_stateid.seqid;
				stateidp->other[0] = dp->nfsdl_stateid.other[0];
				stateidp->other[1] = dp->nfsdl_stateid.other[1];
				stateidp->other[2] = dp->nfsdl_stateid.other[2];
				if (!(np->n_flag & NDELEGRECALL)) {
					TAILQ_REMOVE(&clp->nfsc_deleg, dp,
					    nfsdl_list);
					TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp,
					    nfsdl_list);
					dp->nfsdl_timestamp = NFSD_MONOSEC +
					    120;
					dp->nfsdl_rwlock.nfslock_usecnt++;
					*lckpp = (void *)&dp->nfsdl_rwlock;
				}
				NFSUNLOCKCLSTATE();
				return (0);
			}
			break;
		}
	}

	if (p != NULL) {
		/*
		 * If p != NULL, we want to search the parentage tree
		 * for a matching OpenOwner and use that.
		 */
		if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
			nfscl_filllockowner(NULL, own, F_POSIX);
		else
			nfscl_filllockowner(p->td_proc, own, F_POSIX);
		lp = NULL;
		error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, own, own,
		    mode, &lp, &op);
		if (error == 0 && lp != NULL && fords == 0) {
			/* Don't return a lock stateid for a DS. */
			stateidp->seqid =
			    lp->nfsl_stateid.seqid;
			stateidp->other[0] =
			    lp->nfsl_stateid.other[0];
			stateidp->other[1] =
			    lp->nfsl_stateid.other[1];
			stateidp->other[2] =
			    lp->nfsl_stateid.other[2];
			NFSUNLOCKCLSTATE();
			return (0);
		}
	}
	if (op == NULL) {
		/* If not found, just look for any OpenOwner that will work. */
		top = NULL;
		done = 0;
		owp = LIST_FIRST(&clp->nfsc_owner);
		while (!done && owp != NULL) {
			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
				if (op->nfso_fhlen == fhlen &&
				    !NFSBCMP(op->nfso_fh, nfhp, fhlen)) {
					if (top == NULL && (op->nfso_mode &
					    NFSV4OPEN_ACCESSWRITE) != 0 &&
					    (mode & NFSV4OPEN_ACCESSREAD) != 0)
						top = op;
					if ((mode & op->nfso_mode) == mode) {
						done = 1;
						break;
					}
				}
			}
			if (!done)
				owp = LIST_NEXT(owp, nfsow_list);
		}
		if (!done) {
			NFSCL_DEBUG(2, "openmode top=%p\n", top);
			if (top == NULL || NFSHASOPENMODE(nmp)) {
				NFSUNLOCKCLSTATE();
				return (ENOENT);
			} else
				op = top;
		}
		/*
		 * For read aheads or write behinds, use the open cred.
		 * A read ahead or write behind is indicated by p == NULL.
		 */
		if (p == NULL)
			newnfs_copycred(&op->nfso_cred, cred);
	}

	/*
	 * No lock stateid, so return the open stateid.
	 */
	stateidp->seqid = op->nfso_stateid.seqid;
	stateidp->other[0] = op->nfso_stateid.other[0];
	stateidp->other[1] = op->nfso_stateid.other[1];
	stateidp->other[2] = op->nfso_stateid.other[2];
	NFSUNLOCKCLSTATE();
	return (0);
}

/*
 * Search for a matching file, mode and, optionally, lockowner.
 */
static int
nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
    u_int8_t *openown, u_int8_t *lockown, u_int32_t mode,
    struct nfscllockowner **lpp, struct nfsclopen **opp)
{
	struct nfsclowner *owp;
	struct nfsclopen *op, *rop, *rop2;
	struct nfscllockowner *lp;
	int keep_looping;

	if (lpp != NULL)
		*lpp = NULL;
	/*
	 * rop will be set to the open to be returned. There are three
	 * variants of this, all for an open of the correct file:
	 * 1 - A match of lockown.
	 * 2 - A match of the openown, when no lockown match exists.
	 * 3 - A match for any open, if no openown or lockown match exists.
	 * Looking for #2 over #3 probably isn't necessary, but since
	 * RFC3530 is vague w.r.t. the relationship between openowners and
	 * lockowners, I think this is the safer way to go.
	 */
	rop = NULL;
	rop2 = NULL;
	keep_looping = 1;
	/* Search the client list */
	owp = LIST_FIRST(ohp);
	while (owp != NULL && keep_looping != 0) {
		/* and look for the correct open */
		op = LIST_FIRST(&owp->nfsow_open);
		while (op != NULL && keep_looping != 0) {
			if (op->nfso_fhlen == fhlen &&
			    !NFSBCMP(op->nfso_fh, nfhp, fhlen)
			    && (op->nfso_mode & mode) == mode) {
				if (lpp != NULL) {
					/* Now look for a matching lockowner. */
					LIST_FOREACH(lp, &op->nfso_lock,
					    nfsl_list) {
						if (!NFSBCMP(lp->nfsl_owner,
						    lockown,
						    NFSV4CL_LOCKNAMELEN)) {
							*lpp = lp;
							rop = op;
							keep_looping = 0;
							break;
						}
					}
				}
				if (rop == NULL && !NFSBCMP(owp->nfsow_owner,
				    openown, NFSV4CL_LOCKNAMELEN)) {
					rop = op;
					if (lpp == NULL)
						keep_looping = 0;
				}
				if (rop2 == NULL)
					rop2 = op;
			}
			op = LIST_NEXT(op, nfso_list);
		}
		owp = LIST_NEXT(owp, nfsow_list);
	}
	if (rop == NULL)
		rop = rop2;
	if (rop == NULL)
		return (EBADF);
	*opp = rop;
	return (0);
}

/*
 * Release use of an open owner. Called when open operations are done
 * with the open owner.
 */
APPLESTATIC void
nfscl_ownerrelease(struct nfsmount *nmp, struct nfsclowner *owp,
    __unused int error, __unused int candelete, int unlocked)
{

	if (owp == NULL)
		return;
	NFSLOCKCLSTATE();
	if (unlocked == 0) {
		if (NFSHASONEOPENOWN(nmp))
			nfsv4_relref(&owp->nfsow_rwlock);
		else
			nfscl_lockunlock(&owp->nfsow_rwlock);
	}
	nfscl_clrelease(owp->nfsow_clp);
	NFSUNLOCKCLSTATE();
}

/*
 * Release use of an open structure under an open owner.
 */
APPLESTATIC void
nfscl_openrelease(struct nfsmount *nmp, struct nfsclopen *op, int error,
    int candelete)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;

	if (op == NULL)
		return;
	NFSLOCKCLSTATE();
	owp = op->nfso_own;
	if (NFSHASONEOPENOWN(nmp))
		nfsv4_relref(&owp->nfsow_rwlock);
	else
		nfscl_lockunlock(&owp->nfsow_rwlock);
	clp = owp->nfsow_clp;
	if (error && candelete && op->nfso_opencnt == 0)
		nfscl_freeopen(op, 0);
	nfscl_clrelease(clp);
	NFSUNLOCKCLSTATE();
}

/*
 * Called to get a clientid structure. It will optionally lock the
 * client data structures to do the SetClientId/SetClientId_confirm,
 * but will release that lock and return the clientid with a reference
 * count on it.
 * If the "cred" argument is NULL, a new clientid should not be created.
 * If the "p" argument is NULL, a SetClientID/SetClientIDConfirm cannot
 * be done.
 * The start_renewthread argument tells nfscl_getcl() to start a renew
 * thread if this creates a new clp.
 * It always clpp with a reference count on it, unless returning an error.
 */
APPLESTATIC int
nfscl_getcl(struct mount *mp, struct ucred *cred, NFSPROC_T *p,
    int start_renewthread, struct nfsclclient **clpp)
{
	struct nfsclclient *clp;
	struct nfsclclient *newclp = NULL;
	struct nfsmount *nmp;
	char uuid[HOSTUUIDLEN];
	int igotlock = 0, error, trystalecnt, clidinusedelay, i;
	u_int16_t idlen = 0;

	nmp = VFSTONFS(mp);
	if (cred != NULL) {
		getcredhostuuid(cred, uuid, sizeof uuid);
		idlen = strlen(uuid);
		if (idlen > 0)
			idlen += sizeof (u_int64_t);
		else
			idlen += sizeof (u_int64_t) + 16; /* 16 random bytes */
		newclp = malloc(
		    sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
		    M_WAITOK | M_ZERO);
	}
	NFSLOCKCLSTATE();
	/*
	 * If a forced dismount is already in progress, don't
	 * allocate a new clientid and get out now. For the case where
	 * clp != NULL, this is a harmless optimization.
	 */
	if (NFSCL_FORCEDISM(mp)) {
		NFSUNLOCKCLSTATE();
		if (newclp != NULL)
			free(newclp, M_NFSCLCLIENT);
		return (EBADF);
	}
	clp = nmp->nm_clp;
	if (clp == NULL) {
		if (newclp == NULL) {
			NFSUNLOCKCLSTATE();
			return (EACCES);
		}
		clp = newclp;
		clp->nfsc_idlen = idlen;
		LIST_INIT(&clp->nfsc_owner);
		TAILQ_INIT(&clp->nfsc_deleg);
		TAILQ_INIT(&clp->nfsc_layout);
		LIST_INIT(&clp->nfsc_devinfo);
		for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
			LIST_INIT(&clp->nfsc_deleghash[i]);
		for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
			LIST_INIT(&clp->nfsc_layouthash[i]);
		clp->nfsc_flags = NFSCLFLAGS_INITED;
		clp->nfsc_clientidrev = 1;
		clp->nfsc_cbident = nfscl_nextcbident();
		nfscl_fillclid(nmp->nm_clval, uuid, clp->nfsc_id,
		    clp->nfsc_idlen);
		LIST_INSERT_HEAD(&nfsclhead, clp, nfsc_list);
		nmp->nm_clp = clp;
		clp->nfsc_nmp = nmp;
		NFSUNLOCKCLSTATE();
		if (start_renewthread != 0)
			nfscl_start_renewthread(clp);
	} else {
		NFSUNLOCKCLSTATE();
		if (newclp != NULL)
			free(newclp, M_NFSCLCLIENT);
	}
	NFSLOCKCLSTATE();
	while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock &&
	    !NFSCL_FORCEDISM(mp))
		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
		    NFSCLSTATEMUTEXPTR, mp);
	if (igotlock == 0) {
		/*
		 * Call nfsv4_lock() with "iwantlock == 0" so that it will
		 * wait for a pending exclusive lock request.  This gives the
		 * exclusive lock request priority over this shared lock
		 * request.
		 * An exclusive lock on nfsc_lock is used mainly for server
		 * crash recoveries.
		 */
		nfsv4_lock(&clp->nfsc_lock, 0, NULL, NFSCLSTATEMUTEXPTR, mp);
		nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
	}
	if (igotlock == 0 && NFSCL_FORCEDISM(mp)) {
		/*
		 * Both nfsv4_lock() and nfsv4_getref() know to check
		 * for NFSCL_FORCEDISM() and return without sleeping to
		 * wait for the exclusive lock to be released, since it
		 * might be held by nfscl_umount() and we need to get out
		 * now for that case and not wait until nfscl_umount()
		 * releases it.
		 */
		NFSUNLOCKCLSTATE();
		return (EBADF);
	}
	NFSUNLOCKCLSTATE();

	/*
	 * If it needs a clientid, do the setclientid now.
	 */
	if ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0) {
		if (!igotlock)
			panic("nfscl_clget");
		if (p == NULL || cred == NULL) {
			NFSLOCKCLSTATE();
			nfsv4_unlock(&clp->nfsc_lock, 0);
			NFSUNLOCKCLSTATE();
			return (EACCES);
		}
		/*
		 * If RFC3530 Sec. 14.2.33 is taken literally,
		 * NFSERR_CLIDINUSE will be returned persistently for the
		 * case where a new mount of the same file system is using
		 * a different principal. In practice, NFSERR_CLIDINUSE is
		 * only returned when there is outstanding unexpired state
		 * on the clientid. As such, try for twice the lease
		 * interval, if we know what that is. Otherwise, make a
		 * wild ass guess.
		 * The case of returning NFSERR_STALECLIENTID is far less
		 * likely, but might occur if there is a significant delay
		 * between doing the SetClientID and SetClientIDConfirm Ops,
		 * such that the server throws away the clientid before
		 * receiving the SetClientIDConfirm.
		 */
		if (clp->nfsc_renew > 0)
			clidinusedelay = NFSCL_LEASE(clp->nfsc_renew) * 2;
		else
			clidinusedelay = 120;
		trystalecnt = 3;
		do {
			error = nfsrpc_setclient(nmp, clp, 0, cred, p);
			if (error == NFSERR_STALECLIENTID ||
			    error == NFSERR_STALEDONTRECOVER ||
			    error == NFSERR_BADSESSION ||
			    error == NFSERR_CLIDINUSE) {
				(void) nfs_catnap(PZERO, error, "nfs_setcl");
			}
		} while (((error == NFSERR_STALECLIENTID ||
		     error == NFSERR_BADSESSION ||
		     error == NFSERR_STALEDONTRECOVER) && --trystalecnt > 0) ||
		    (error == NFSERR_CLIDINUSE && --clidinusedelay > 0));
		if (error) {
			NFSLOCKCLSTATE();
			nfsv4_unlock(&clp->nfsc_lock, 0);
			NFSUNLOCKCLSTATE();
			return (error);
		}
		clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
	}
	if (igotlock) {
		NFSLOCKCLSTATE();
		nfsv4_unlock(&clp->nfsc_lock, 1);
		NFSUNLOCKCLSTATE();
	}

	*clpp = clp;
	return (0);
}

/*
 * Get a reference to a clientid and return it, if valid.
 */
APPLESTATIC struct nfsclclient *
nfscl_findcl(struct nfsmount *nmp)
{
	struct nfsclclient *clp;

	clp = nmp->nm_clp;
	if (clp == NULL || !(clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID))
		return (NULL);
	return (clp);
}

/*
 * Release the clientid structure. It may be locked or reference counted.
 */
static void
nfscl_clrelease(struct nfsclclient *clp)
{

	if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
		nfsv4_unlock(&clp->nfsc_lock, 0);
	else
		nfsv4_relref(&clp->nfsc_lock);
}

/*
 * External call for nfscl_clrelease.
 */
APPLESTATIC void
nfscl_clientrelease(struct nfsclclient *clp)
{

	NFSLOCKCLSTATE();
	if (clp->nfsc_lock.nfslock_lock & NFSV4LOCK_LOCK)
		nfsv4_unlock(&clp->nfsc_lock, 0);
	else
		nfsv4_relref(&clp->nfsc_lock);
	NFSUNLOCKCLSTATE();
}

/*
 * Called when wanting to lock a byte region.
 */
APPLESTATIC int
nfscl_getbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
    short type, struct ucred *cred, NFSPROC_T *p, struct nfsclclient *rclp,
    int recovery, void *id, int flags, u_int8_t *rownp, u_int8_t *ropenownp,
    struct nfscllockowner **lpp, int *newonep, int *donelocallyp)
{
	struct nfscllockowner *lp;
	struct nfsclopen *op;
	struct nfsclclient *clp;
	struct nfscllockowner *nlp;
	struct nfscllock *nlop, *otherlop;
	struct nfscldeleg *dp = NULL, *ldp = NULL;
	struct nfscllockownerhead *lhp = NULL;
	struct nfsnode *np;
	u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp, openown[NFSV4CL_LOCKNAMELEN];
	u_int8_t *openownp;
	int error = 0, ret, donelocally = 0;
	u_int32_t mode;

	/* For Lock Ops, the open mode doesn't matter, so use 0 to match any. */
	mode = 0;
	np = VTONFS(vp);
	*lpp = NULL;
	lp = NULL;
	*newonep = 0;
	*donelocallyp = 0;

	/*
	 * Might need these, so MALLOC them now, to
	 * avoid a tsleep() in MALLOC later.
	 */
	nlp = malloc(
	    sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
	otherlop = malloc(
	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
	nlop = malloc(
	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
	nlop->nfslo_type = type;
	nlop->nfslo_first = off;
	if (len == NFS64BITSSET) {
		nlop->nfslo_end = NFS64BITSSET;
	} else {
		nlop->nfslo_end = off + len;
		if (nlop->nfslo_end <= nlop->nfslo_first)
			error = NFSERR_INVAL;
	}

	if (!error) {
		if (recovery)
			clp = rclp;
		else
			error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
	}
	if (error) {
		free(nlp, M_NFSCLLOCKOWNER);
		free(otherlop, M_NFSCLLOCK);
		free(nlop, M_NFSCLLOCK);
		return (error);
	}

	op = NULL;
	if (recovery) {
		ownp = rownp;
		openownp = ropenownp;
	} else {
		nfscl_filllockowner(id, own, flags);
		ownp = own;
		if (NFSHASONEOPENOWN(VFSTONFS(vnode_mount(vp))))
			nfscl_filllockowner(NULL, openown, F_POSIX);
		else
			nfscl_filllockowner(p->td_proc, openown, F_POSIX);
		openownp = openown;
	}
	if (!recovery) {
		NFSLOCKCLSTATE();
		/*
		 * First, search for a delegation. If one exists for this file,
		 * the lock can be done locally against it, so long as there
		 * isn't a local lock conflict.
		 */
		ldp = dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len);
		/* Just sanity check for correct type of delegation */
		if (dp != NULL && ((dp->nfsdl_flags &
		    (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) != 0 ||
		     (type == F_WRLCK &&
		      (dp->nfsdl_flags & NFSCLDL_WRITE) == 0)))
			dp = NULL;
	}
	if (dp != NULL) {
		/* Now, find an open and maybe a lockowner. */
		ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len, openownp, ownp, mode, NULL, &op);
		if (ret)
			ret = nfscl_getopen(&clp->nfsc_owner,
			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
			    ownp, mode, NULL, &op);
		if (!ret) {
			lhp = &dp->nfsdl_lock;
			TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
			TAILQ_INSERT_HEAD(&clp->nfsc_deleg, dp, nfsdl_list);
			dp->nfsdl_timestamp = NFSD_MONOSEC + 120;
			donelocally = 1;
		} else {
			dp = NULL;
		}
	}
	if (!donelocally) {
		/*
		 * Get the related Open and maybe lockowner.
		 */
		error = nfscl_getopen(&clp->nfsc_owner,
		    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, openownp,
		    ownp, mode, &lp, &op);
		if (!error)
			lhp = &op->nfso_lock;
	}
	if (!error && !recovery)
		error = nfscl_localconflict(clp, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len, nlop, ownp, ldp, NULL);
	if (error) {
		if (!recovery) {
			nfscl_clrelease(clp);
			NFSUNLOCKCLSTATE();
		}
		free(nlp, M_NFSCLLOCKOWNER);
		free(otherlop, M_NFSCLLOCK);
		free(nlop, M_NFSCLLOCK);
		return (error);
	}

	/*
	 * Ok, see if a lockowner exists and create one, as required.
	 */
	if (lp == NULL)
		LIST_FOREACH(lp, lhp, nfsl_list) {
			if (!NFSBCMP(lp->nfsl_owner, ownp, NFSV4CL_LOCKNAMELEN))
				break;
		}
	if (lp == NULL) {
		NFSBCOPY(ownp, nlp->nfsl_owner, NFSV4CL_LOCKNAMELEN);
		if (recovery)
			NFSBCOPY(ropenownp, nlp->nfsl_openowner,
			    NFSV4CL_LOCKNAMELEN);
		else
			NFSBCOPY(op->nfso_own->nfsow_owner, nlp->nfsl_openowner,
			    NFSV4CL_LOCKNAMELEN);
		nlp->nfsl_seqid = 0;
		nlp->nfsl_lockflags = flags;
		nlp->nfsl_inprog = NULL;
		nfscl_lockinit(&nlp->nfsl_rwlock);
		LIST_INIT(&nlp->nfsl_lock);
		if (donelocally) {
			nlp->nfsl_open = NULL;
			nfsstatsv1.cllocallockowners++;
		} else {
			nlp->nfsl_open = op;
			nfsstatsv1.cllockowners++;
		}
		LIST_INSERT_HEAD(lhp, nlp, nfsl_list);
		lp = nlp;
		nlp = NULL;
		*newonep = 1;
	}

	/*
	 * Now, update the byte ranges for locks.
	 */
	ret = nfscl_updatelock(lp, &nlop, &otherlop, donelocally);
	if (!ret)
		donelocally = 1;
	if (donelocally) {
		*donelocallyp = 1;
		if (!recovery)
			nfscl_clrelease(clp);
	} else {
		/*
		 * Serial modifications on the lock owner for multiple threads
		 * for the same process using a read/write lock.
		 */
		if (!recovery)
			nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
	}
	if (!recovery)
		NFSUNLOCKCLSTATE();

	if (nlp)
		free(nlp, M_NFSCLLOCKOWNER);
	if (nlop)
		free(nlop, M_NFSCLLOCK);
	if (otherlop)
		free(otherlop, M_NFSCLLOCK);

	*lpp = lp;
	return (0);
}

/*
 * Called to unlock a byte range, for LockU.
 */
APPLESTATIC int
nfscl_relbytelock(vnode_t vp, u_int64_t off, u_int64_t len,
    __unused struct ucred *cred, NFSPROC_T *p, int callcnt,
    struct nfsclclient *clp, void *id, int flags,
    struct nfscllockowner **lpp, int *dorpcp)
{
	struct nfscllockowner *lp;
	struct nfsclowner *owp;
	struct nfsclopen *op;
	struct nfscllock *nlop, *other_lop = NULL;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int ret = 0, fnd;

	np = VTONFS(vp);
	*lpp = NULL;
	*dorpcp = 0;

	/*
	 * Might need these, so MALLOC them now, to
	 * avoid a tsleep() in MALLOC later.
	 */
	nlop = malloc(
	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
	nlop->nfslo_type = F_UNLCK;
	nlop->nfslo_first = off;
	if (len == NFS64BITSSET) {
		nlop->nfslo_end = NFS64BITSSET;
	} else {
		nlop->nfslo_end = off + len;
		if (nlop->nfslo_end <= nlop->nfslo_first) {
			free(nlop, M_NFSCLLOCK);
			return (NFSERR_INVAL);
		}
	}
	if (callcnt == 0) {
		other_lop = malloc(
		    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
		*other_lop = *nlop;
	}
	nfscl_filllockowner(id, own, flags);
	dp = NULL;
	NFSLOCKCLSTATE();
	if (callcnt == 0)
		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len);

	/*
	 * First, unlock any local regions on a delegation.
	 */
	if (dp != NULL) {
		/* Look for this lockowner. */
		LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
			if (!NFSBCMP(lp->nfsl_owner, own,
			    NFSV4CL_LOCKNAMELEN))
				break;
		}
		if (lp != NULL)
			/* Use other_lop, so nlop is still available */
			(void)nfscl_updatelock(lp, &other_lop, NULL, 1);
	}

	/*
	 * Now, find a matching open/lockowner that hasn't already been done,
	 * as marked by nfsl_inprog.
	 */
	lp = NULL;
	fnd = 0;
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
		    LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
			if (lp->nfsl_inprog == NULL &&
			    !NFSBCMP(lp->nfsl_owner, own,
			     NFSV4CL_LOCKNAMELEN)) {
				fnd = 1;
				break;
			}
		    }
		    if (fnd)
			break;
		}
	    }
	    if (fnd)
		break;
	}

	if (lp != NULL) {
		ret = nfscl_updatelock(lp, &nlop, NULL, 0);
		if (ret)
			*dorpcp = 1;
		/*
		 * Serial modifications on the lock owner for multiple
		 * threads for the same process using a read/write lock.
		 */
		lp->nfsl_inprog = p;
		nfscl_lockexcl(&lp->nfsl_rwlock, NFSCLSTATEMUTEXPTR);
		*lpp = lp;
	}
	NFSUNLOCKCLSTATE();
	if (nlop)
		free(nlop, M_NFSCLLOCK);
	if (other_lop)
		free(other_lop, M_NFSCLLOCK);
	return (0);
}

/*
 * Release all lockowners marked in progess for this process and file.
 */
APPLESTATIC void
nfscl_releasealllocks(struct nfsclclient *clp, vnode_t vp, NFSPROC_T *p,
    void *id, int flags)
{
	struct nfsclowner *owp;
	struct nfsclopen *op;
	struct nfscllockowner *lp;
	struct nfsnode *np;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];

	np = VTONFS(vp);
	nfscl_filllockowner(id, own, flags);
	NFSLOCKCLSTATE();
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
		    LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
			if (lp->nfsl_inprog == p &&
			    !NFSBCMP(lp->nfsl_owner, own,
			    NFSV4CL_LOCKNAMELEN)) {
			    lp->nfsl_inprog = NULL;
			    nfscl_lockunlock(&lp->nfsl_rwlock);
			}
		    }
		}
	    }
	}
	nfscl_clrelease(clp);
	NFSUNLOCKCLSTATE();
}

/*
 * Called to find out if any bytes within the byte range specified are
 * write locked by the calling process. Used to determine if flushing
 * is required before a LockU.
 * If in doubt, return 1, so the flush will occur.
 */
APPLESTATIC int
nfscl_checkwritelocked(vnode_t vp, struct flock *fl,
    struct ucred *cred, NFSPROC_T *p, void *id, int flags)
{
	struct nfsclowner *owp;
	struct nfscllockowner *lp;
	struct nfsclopen *op;
	struct nfsclclient *clp;
	struct nfscllock *lop;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	u_int64_t off, end;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int error = 0;

	np = VTONFS(vp);
	switch (fl->l_whence) {
	case SEEK_SET:
	case SEEK_CUR:
		/*
		 * Caller is responsible for adding any necessary offset
		 * when SEEK_CUR is used.
		 */
		off = fl->l_start;
		break;
	case SEEK_END:
		off = np->n_size + fl->l_start;
		break;
	default:
		return (1);
	}
	if (fl->l_len != 0) {
		end = off + fl->l_len;
		if (end < off)
			return (1);
	} else {
		end = NFS64BITSSET;
	}

	error = nfscl_getcl(vnode_mount(vp), cred, p, 1, &clp);
	if (error)
		return (1);
	nfscl_filllockowner(id, own, flags);
	NFSLOCKCLSTATE();

	/*
	 * First check the delegation locks.
	 */
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL) {
		LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
			if (!NFSBCMP(lp->nfsl_owner, own,
			    NFSV4CL_LOCKNAMELEN))
				break;
		}
		if (lp != NULL) {
			LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
				if (lop->nfslo_first >= end)
					break;
				if (lop->nfslo_end <= off)
					continue;
				if (lop->nfslo_type == F_WRLCK) {
					nfscl_clrelease(clp);
					NFSUNLOCKCLSTATE();
					return (1);
				}
			}
		}
	}

	/*
	 * Now, check state against the server.
	 */
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if (op->nfso_fhlen == np->n_fhp->nfh_len &&
		    !NFSBCMP(op->nfso_fh, np->n_fhp->nfh_fh, op->nfso_fhlen)) {
		    LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
			if (!NFSBCMP(lp->nfsl_owner, own,
			    NFSV4CL_LOCKNAMELEN))
			    break;
		    }
		    if (lp != NULL) {
			LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
			    if (lop->nfslo_first >= end)
				break;
			    if (lop->nfslo_end <= off)
				continue;
			    if (lop->nfslo_type == F_WRLCK) {
				nfscl_clrelease(clp);
				NFSUNLOCKCLSTATE();
				return (1);
			    }
			}
		    }
		}
	    }
	}
	nfscl_clrelease(clp);
	NFSUNLOCKCLSTATE();
	return (0);
}

/*
 * Release a byte range lock owner structure.
 */
APPLESTATIC void
nfscl_lockrelease(struct nfscllockowner *lp, int error, int candelete)
{
	struct nfsclclient *clp;

	if (lp == NULL)
		return;
	NFSLOCKCLSTATE();
	clp = lp->nfsl_open->nfso_own->nfsow_clp;
	if (error != 0 && candelete &&
	    (lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED) == 0)
		nfscl_freelockowner(lp, 0);
	else
		nfscl_lockunlock(&lp->nfsl_rwlock);
	nfscl_clrelease(clp);
	NFSUNLOCKCLSTATE();
}

/*
 * Free up an open structure and any associated byte range lock structures.
 */
APPLESTATIC void
nfscl_freeopen(struct nfsclopen *op, int local)
{

	LIST_REMOVE(op, nfso_list);
	nfscl_freealllocks(&op->nfso_lock, local);
	free(op, M_NFSCLOPEN);
	if (local)
		nfsstatsv1.cllocalopens--;
	else
		nfsstatsv1.clopens--;
}

/*
 * Free up all lock owners and associated locks.
 */
static void
nfscl_freealllocks(struct nfscllockownerhead *lhp, int local)
{
	struct nfscllockowner *lp, *nlp;

	LIST_FOREACH_SAFE(lp, lhp, nfsl_list, nlp) {
		if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
			panic("nfscllckw");
		nfscl_freelockowner(lp, local);
	}
}

/*
 * Called for an Open when NFSERR_EXPIRED is received from the server.
 * If there are no byte range locks nor a Share Deny lost, try to do a
 * fresh Open. Otherwise, free the open.
 */
static int
nfscl_expireopen(struct nfsclclient *clp, struct nfsclopen *op,
    struct nfsmount *nmp, struct ucred *cred, NFSPROC_T *p)
{
	struct nfscllockowner *lp;
	struct nfscldeleg *dp;
	int mustdelete = 0, error;

	/*
	 * Look for any byte range lock(s).
	 */
	LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
		if (!LIST_EMPTY(&lp->nfsl_lock)) {
			mustdelete = 1;
			break;
		}
	}

	/*
	 * If no byte range lock(s) nor a Share deny, try to re-open.
	 */
	if (!mustdelete && (op->nfso_mode & NFSLCK_DENYBITS) == 0) {
		newnfs_copycred(&op->nfso_cred, cred);
		dp = NULL;
		error = nfsrpc_reopen(nmp, op->nfso_fh,
		    op->nfso_fhlen, op->nfso_mode, op, &dp, cred, p);
		if (error) {
			mustdelete = 1;
			if (dp != NULL) {
				free(dp, M_NFSCLDELEG);
				dp = NULL;
			}
		}
		if (dp != NULL)
			nfscl_deleg(nmp->nm_mountp, clp, op->nfso_fh,
			    op->nfso_fhlen, cred, p, &dp);
	}

	/*
	 * If a byte range lock or Share deny or couldn't re-open, free it.
	 */
	if (mustdelete)
		nfscl_freeopen(op, 0);
	return (mustdelete);
}

/*
 * Free up an open owner structure.
 */
static void
nfscl_freeopenowner(struct nfsclowner *owp, int local)
{

	LIST_REMOVE(owp, nfsow_list);
	free(owp, M_NFSCLOWNER);
	if (local)
		nfsstatsv1.cllocalopenowners--;
	else
		nfsstatsv1.clopenowners--;
}

/*
 * Free up a byte range lock owner structure.
 */
APPLESTATIC void
nfscl_freelockowner(struct nfscllockowner *lp, int local)
{
	struct nfscllock *lop, *nlop;

	LIST_REMOVE(lp, nfsl_list);
	LIST_FOREACH_SAFE(lop, &lp->nfsl_lock, nfslo_list, nlop) {
		nfscl_freelock(lop, local);
	}
	free(lp, M_NFSCLLOCKOWNER);
	if (local)
		nfsstatsv1.cllocallockowners--;
	else
		nfsstatsv1.cllockowners--;
}

/*
 * Free up a byte range lock structure.
 */
APPLESTATIC void
nfscl_freelock(struct nfscllock *lop, int local)
{

	LIST_REMOVE(lop, nfslo_list);
	free(lop, M_NFSCLLOCK);
	if (local)
		nfsstatsv1.cllocallocks--;
	else
		nfsstatsv1.cllocks--;
}

/*
 * Clean out the state related to a delegation.
 */
static void
nfscl_cleandeleg(struct nfscldeleg *dp)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op;

	LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
		op = LIST_FIRST(&owp->nfsow_open);
		if (op != NULL) {
			if (LIST_NEXT(op, nfso_list) != NULL)
				panic("nfscleandel");
			nfscl_freeopen(op, 1);
		}
		nfscl_freeopenowner(owp, 1);
	}
	nfscl_freealllocks(&dp->nfsdl_lock, 1);
}

/*
 * Free a delegation.
 */
static void
nfscl_freedeleg(struct nfscldeleghead *hdp, struct nfscldeleg *dp)
{

	TAILQ_REMOVE(hdp, dp, nfsdl_list);
	LIST_REMOVE(dp, nfsdl_hash);
	free(dp, M_NFSCLDELEG);
	nfsstatsv1.cldelegates--;
	nfscl_delegcnt--;
}

/*
 * Free up all state related to this client structure.
 */
static void
nfscl_cleanclient(struct nfsclclient *clp)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op, *nop;
	struct nfscllayout *lyp, *nlyp;
	struct nfscldevinfo *dip, *ndip;

	TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
		nfscl_freelayout(lyp);

	LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip)
		nfscl_freedevinfo(dip);

	/* Now, all the OpenOwners, etc. */
	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
		LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
			nfscl_freeopen(op, 0);
		}
		nfscl_freeopenowner(owp, 0);
	}
}

/*
 * Called when an NFSERR_EXPIRED is received from the server.
 */
static void
nfscl_expireclient(struct nfsclclient *clp, struct nfsmount *nmp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclowner *owp, *nowp, *towp;
	struct nfsclopen *op, *nop, *top;
	struct nfscldeleg *dp, *ndp;
	int ret, printed = 0;

	/*
	 * First, merge locally issued Opens into the list for the server.
	 */
	dp = TAILQ_FIRST(&clp->nfsc_deleg);
	while (dp != NULL) {
	    ndp = TAILQ_NEXT(dp, nfsdl_list);
	    owp = LIST_FIRST(&dp->nfsdl_owner);
	    while (owp != NULL) {
		nowp = LIST_NEXT(owp, nfsow_list);
		op = LIST_FIRST(&owp->nfsow_open);
		if (op != NULL) {
		    if (LIST_NEXT(op, nfso_list) != NULL)
			panic("nfsclexp");
		    LIST_FOREACH(towp, &clp->nfsc_owner, nfsow_list) {
			if (!NFSBCMP(towp->nfsow_owner, owp->nfsow_owner,
			    NFSV4CL_LOCKNAMELEN))
			    break;
		    }
		    if (towp != NULL) {
			/* Merge opens in */
			LIST_FOREACH(top, &towp->nfsow_open, nfso_list) {
			    if (top->nfso_fhlen == op->nfso_fhlen &&
				!NFSBCMP(top->nfso_fh, op->nfso_fh,
				 op->nfso_fhlen)) {
				top->nfso_mode |= op->nfso_mode;
				top->nfso_opencnt += op->nfso_opencnt;
				break;
			    }
			}
			if (top == NULL) {
			    /* Just add the open to the owner list */
			    LIST_REMOVE(op, nfso_list);
			    op->nfso_own = towp;
			    LIST_INSERT_HEAD(&towp->nfsow_open, op, nfso_list);
			    nfsstatsv1.cllocalopens--;
			    nfsstatsv1.clopens++;
			}
		    } else {
			/* Just add the openowner to the client list */
			LIST_REMOVE(owp, nfsow_list);
			owp->nfsow_clp = clp;
			LIST_INSERT_HEAD(&clp->nfsc_owner, owp, nfsow_list);
			nfsstatsv1.cllocalopenowners--;
			nfsstatsv1.clopenowners++;
			nfsstatsv1.cllocalopens--;
			nfsstatsv1.clopens++;
		    }
		}
		owp = nowp;
	    }
	    if (!printed && !LIST_EMPTY(&dp->nfsdl_lock)) {
		printed = 1;
		printf("nfsv4 expired locks lost\n");
	    }
	    nfscl_cleandeleg(dp);
	    nfscl_freedeleg(&clp->nfsc_deleg, dp);
	    dp = ndp;
	}
	if (!TAILQ_EMPTY(&clp->nfsc_deleg))
	    panic("nfsclexp");

	/*
	 * Now, try and reopen against the server.
	 */
	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
		owp->nfsow_seqid = 0;
		LIST_FOREACH_SAFE(op, &owp->nfsow_open, nfso_list, nop) {
			ret = nfscl_expireopen(clp, op, nmp, cred, p);
			if (ret && !printed) {
				printed = 1;
				printf("nfsv4 expired locks lost\n");
			}
		}
		if (LIST_EMPTY(&owp->nfsow_open))
			nfscl_freeopenowner(owp, 0);
	}
}

/*
 * This function must be called after the process represented by "own" has
 * exited. Must be called with CLSTATE lock held.
 */
static void
nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
{
	struct nfsclowner *owp, *nowp;
	struct nfscllockowner *lp, *nlp;
	struct nfscldeleg *dp;

	/* First, get rid of local locks on delegations. */
	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
		LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
		    if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
			if ((lp->nfsl_rwlock.nfslock_lock & NFSV4LOCK_WANTED))
			    panic("nfscllckw");
			nfscl_freelockowner(lp, 1);
		    }
		}
	}
	owp = LIST_FIRST(&clp->nfsc_owner);
	while (owp != NULL) {
		nowp = LIST_NEXT(owp, nfsow_list);
		if (!NFSBCMP(owp->nfsow_owner, own,
		    NFSV4CL_LOCKNAMELEN)) {
			/*
			 * If there are children that haven't closed the
			 * file descriptors yet, the opens will still be
			 * here. For that case, let the renew thread clear
			 * out the OpenOwner later.
			 */
			if (LIST_EMPTY(&owp->nfsow_open))
				nfscl_freeopenowner(owp, 0);
			else
				owp->nfsow_defunct = 1;
		}
		owp = nowp;
	}
}

/*
 * Find open/lock owners for processes that have exited.
 */
static void
nfscl_cleanupkext(struct nfsclclient *clp, struct nfscllockownerfhhead *lhp)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op;
	struct nfscllockowner *lp, *nlp;
	struct nfscldeleg *dp;

	NFSPROCLISTLOCK();
	NFSLOCKCLSTATE();
	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			LIST_FOREACH_SAFE(lp, &op->nfso_lock, nfsl_list, nlp) {
				if (LIST_EMPTY(&lp->nfsl_lock))
					nfscl_emptylockowner(lp, lhp);
			}
		}
		if (nfscl_procdoesntexist(owp->nfsow_owner))
			nfscl_cleanup_common(clp, owp->nfsow_owner);
	}

	/*
	 * For the single open_owner case, these lock owners need to be
	 * checked to see if they still exist separately.
	 * This is because nfscl_procdoesntexist() never returns true for
	 * the single open_owner so that the above doesn't ever call
	 * nfscl_cleanup_common().
	 */
	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
		LIST_FOREACH_SAFE(lp, &dp->nfsdl_lock, nfsl_list, nlp) {
			if (nfscl_procdoesntexist(lp->nfsl_owner))
				nfscl_cleanup_common(clp, lp->nfsl_owner);
		}
	}
	NFSUNLOCKCLSTATE();
	NFSPROCLISTUNLOCK();
}

/*
 * Take the empty lock owner and move it to the local lhp list if the
 * associated process no longer exists.
 */
static void
nfscl_emptylockowner(struct nfscllockowner *lp,
    struct nfscllockownerfhhead *lhp)
{
	struct nfscllockownerfh *lfhp, *mylfhp;
	struct nfscllockowner *nlp;
	int fnd_it;

	/* If not a Posix lock owner, just return. */
	if ((lp->nfsl_lockflags & F_POSIX) == 0)
		return;

	fnd_it = 0;
	mylfhp = NULL;
	/*
	 * First, search to see if this lock owner is already in the list.
	 * If it is, then the associated process no longer exists.
	 */
	SLIST_FOREACH(lfhp, lhp, nfslfh_list) {
		if (lfhp->nfslfh_len == lp->nfsl_open->nfso_fhlen &&
		    !NFSBCMP(lfhp->nfslfh_fh, lp->nfsl_open->nfso_fh,
		    lfhp->nfslfh_len))
			mylfhp = lfhp;
		LIST_FOREACH(nlp, &lfhp->nfslfh_lock, nfsl_list)
			if (!NFSBCMP(nlp->nfsl_owner, lp->nfsl_owner,
			    NFSV4CL_LOCKNAMELEN))
				fnd_it = 1;
	}
	/* If not found, check if process still exists. */
	if (fnd_it == 0 && nfscl_procdoesntexist(lp->nfsl_owner) == 0)
		return;

	/* Move the lock owner over to the local list. */
	if (mylfhp == NULL) {
		mylfhp = malloc(sizeof(struct nfscllockownerfh), M_TEMP,
		    M_NOWAIT);
		if (mylfhp == NULL)
			return;
		mylfhp->nfslfh_len = lp->nfsl_open->nfso_fhlen;
		NFSBCOPY(lp->nfsl_open->nfso_fh, mylfhp->nfslfh_fh,
		    mylfhp->nfslfh_len);
		LIST_INIT(&mylfhp->nfslfh_lock);
		SLIST_INSERT_HEAD(lhp, mylfhp, nfslfh_list);
	}
	LIST_REMOVE(lp, nfsl_list);
	LIST_INSERT_HEAD(&mylfhp->nfslfh_lock, lp, nfsl_list);
}

static int	fake_global;	/* Used to force visibility of MNTK_UNMOUNTF */
/*
 * Called from nfs umount to free up the clientid.
 */
APPLESTATIC void
nfscl_umount(struct nfsmount *nmp, NFSPROC_T *p)
{
	struct nfsclclient *clp;
	struct ucred *cred;
	int igotlock;

	/*
	 * For the case that matters, this is the thread that set
	 * MNTK_UNMOUNTF, so it will see it set. The code that follows is
	 * done to ensure that any thread executing nfscl_getcl() after
	 * this time, will see MNTK_UNMOUNTF set. nfscl_getcl() uses the
	 * mutex for NFSLOCKCLSTATE(), so it is "m" for the following
	 * explanation, courtesy of Alan Cox.
	 * What follows is a snippet from Alan Cox's email at:
	 * https://docs.FreeBSD.org/cgi/mid.cgi?BANLkTikR3d65zPHo9==08ZfJ2vmqZucEvw
	 * 
	 * 1. Set MNTK_UNMOUNTF
	 * 2. Acquire a standard FreeBSD mutex "m".
	 * 3. Update some data structures.
	 * 4. Release mutex "m".
	 * 
	 * Then, other threads that acquire "m" after step 4 has occurred will
	 * see MNTK_UNMOUNTF as set.  But, other threads that beat thread X to
	 * step 2 may or may not see MNTK_UNMOUNTF as set.
	 */
	NFSLOCKCLSTATE();
	if ((nmp->nm_mountp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
		fake_global++;
		NFSUNLOCKCLSTATE();
		NFSLOCKCLSTATE();
	}

	clp = nmp->nm_clp;
	if (clp != NULL) {
		if ((clp->nfsc_flags & NFSCLFLAGS_INITED) == 0)
			panic("nfscl umount");
	
		/*
		 * First, handshake with the nfscl renew thread, to terminate
		 * it.
		 */
		clp->nfsc_flags |= NFSCLFLAGS_UMOUNT;
		while (clp->nfsc_flags & NFSCLFLAGS_HASTHREAD)
			(void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT,
			    "nfsclumnt", hz);
	
		/*
		 * Now, get the exclusive lock on the client state, so
		 * that no uses of the state are still in progress.
		 */
		do {
			igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
			    NFSCLSTATEMUTEXPTR, NULL);
		} while (!igotlock);
		NFSUNLOCKCLSTATE();
	
		/*
		 * Free up all the state. It will expire on the server, but
		 * maybe we should do a SetClientId/SetClientIdConfirm so
		 * the server throws it away?
		 */
		LIST_REMOVE(clp, nfsc_list);
		nfscl_delegreturnall(clp, p);
		cred = newnfs_getcred();
		if (NFSHASNFSV4N(nmp)) {
			(void)nfsrpc_destroysession(nmp, clp, cred, p);
			(void)nfsrpc_destroyclient(nmp, clp, cred, p);
		} else
			(void)nfsrpc_setclient(nmp, clp, 0, cred, p);
		nfscl_cleanclient(clp);
		nmp->nm_clp = NULL;
		NFSFREECRED(cred);
		free(clp, M_NFSCLCLIENT);
	} else
		NFSUNLOCKCLSTATE();
}

/*
 * This function is called when a server replies with NFSERR_STALECLIENTID
 * NFSERR_STALESTATEID or NFSERR_BADSESSION. It traverses the clientid lists,
 * doing Opens and Locks with reclaim. If these fail, it deletes the
 * corresponding state.
 */
static void
nfscl_recover(struct nfsclclient *clp, struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op, *nop;
	struct nfscllockowner *lp, *nlp;
	struct nfscllock *lop, *nlop;
	struct nfscldeleg *dp, *ndp, *tdp;
	struct nfsmount *nmp;
	struct ucred *tcred;
	struct nfsclopenhead extra_open;
	struct nfscldeleghead extra_deleg;
	struct nfsreq *rep;
	u_int64_t len;
	u_int32_t delegtype = NFSV4OPEN_DELEGATEWRITE, mode;
	int i, igotlock = 0, error, trycnt, firstlock;
	struct nfscllayout *lyp, *nlyp;

	/*
	 * First, lock the client structure, so everyone else will
	 * block when trying to use state.
	 */
	NFSLOCKCLSTATE();
	clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
	do {
		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
		    NFSCLSTATEMUTEXPTR, NULL);
	} while (!igotlock);
	NFSUNLOCKCLSTATE();

	nmp = clp->nfsc_nmp;
	if (nmp == NULL)
		panic("nfscl recover");

	/*
	 * For now, just get rid of all layouts. There may be a need
	 * to do LayoutCommit Ops with reclaim == true later.
	 */
	TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp)
		nfscl_freelayout(lyp);
	TAILQ_INIT(&clp->nfsc_layout);
	for (i = 0; i < NFSCLLAYOUTHASHSIZE; i++)
		LIST_INIT(&clp->nfsc_layouthash[i]);

	trycnt = 5;
	do {
		error = nfsrpc_setclient(nmp, clp, 1, cred, p);
	} while ((error == NFSERR_STALECLIENTID ||
	     error == NFSERR_BADSESSION ||
	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
	if (error) {
		NFSLOCKCLSTATE();
		clp->nfsc_flags &= ~(NFSCLFLAGS_RECOVER |
		    NFSCLFLAGS_RECVRINPROG);
		wakeup(&clp->nfsc_flags);
		nfsv4_unlock(&clp->nfsc_lock, 0);
		NFSUNLOCKCLSTATE();
		return;
	}
	clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
	clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;

	/*
	 * Mark requests already queued on the server, so that they don't
	 * initiate another recovery cycle. Any requests already in the
	 * queue that handle state information will have the old stale
	 * clientid/stateid and will get a NFSERR_STALESTATEID,
	 * NFSERR_STALECLIENTID or NFSERR_BADSESSION reply from the server.
	 * This will be translated to NFSERR_STALEDONTRECOVER when
	 * R_DONTRECOVER is set.
	 */
	NFSLOCKREQ();
	TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
		if (rep->r_nmp == nmp)
			rep->r_flags |= R_DONTRECOVER;
	}
	NFSUNLOCKREQ();

	/*
	 * Now, mark all delegations "need reclaim".
	 */
	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list)
		dp->nfsdl_flags |= NFSCLDL_NEEDRECLAIM;

	TAILQ_INIT(&extra_deleg);
	LIST_INIT(&extra_open);
	/*
	 * Now traverse the state lists, doing Open and Lock Reclaims.
	 */
	tcred = newnfs_getcred();
	owp = LIST_FIRST(&clp->nfsc_owner);
	while (owp != NULL) {
	    nowp = LIST_NEXT(owp, nfsow_list);
	    owp->nfsow_seqid = 0;
	    op = LIST_FIRST(&owp->nfsow_open);
	    while (op != NULL) {
		nop = LIST_NEXT(op, nfso_list);
		if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
		    /* Search for a delegation to reclaim with the open */
		    TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
			if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
			    continue;
			if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
			    mode = NFSV4OPEN_ACCESSWRITE;
			    delegtype = NFSV4OPEN_DELEGATEWRITE;
			} else {
			    mode = NFSV4OPEN_ACCESSREAD;
			    delegtype = NFSV4OPEN_DELEGATEREAD;
			}
			if ((op->nfso_mode & mode) == mode &&
			    op->nfso_fhlen == dp->nfsdl_fhlen &&
			    !NFSBCMP(op->nfso_fh, dp->nfsdl_fh, op->nfso_fhlen))
			    break;
		    }
		    ndp = dp;
		    if (dp == NULL)
			delegtype = NFSV4OPEN_DELEGATENONE;
		    newnfs_copycred(&op->nfso_cred, tcred);
		    error = nfscl_tryopen(nmp, NULL, op->nfso_fh,
			op->nfso_fhlen, op->nfso_fh, op->nfso_fhlen,
			op->nfso_mode, op, NULL, 0, &ndp, 1, delegtype,
			tcred, p);
		    if (!error) {
			/* Handle any replied delegation */
			if (ndp != NULL && ((ndp->nfsdl_flags & NFSCLDL_WRITE)
			    || NFSMNT_RDONLY(nmp->nm_mountp))) {
			    if ((ndp->nfsdl_flags & NFSCLDL_WRITE))
				mode = NFSV4OPEN_ACCESSWRITE;
			    else
				mode = NFSV4OPEN_ACCESSREAD;
			    TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
				if (!(dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM))
				    continue;
				if ((op->nfso_mode & mode) == mode &&
				    op->nfso_fhlen == dp->nfsdl_fhlen &&
				    !NFSBCMP(op->nfso_fh, dp->nfsdl_fh,
				    op->nfso_fhlen)) {
				    dp->nfsdl_stateid = ndp->nfsdl_stateid;
				    dp->nfsdl_sizelimit = ndp->nfsdl_sizelimit;
				    dp->nfsdl_ace = ndp->nfsdl_ace;
				    dp->nfsdl_change = ndp->nfsdl_change;
				    dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
				    if ((ndp->nfsdl_flags & NFSCLDL_RECALL))
					dp->nfsdl_flags |= NFSCLDL_RECALL;
				    free(ndp, M_NFSCLDELEG);
				    ndp = NULL;
				    break;
				}
			    }
			}
			if (ndp != NULL)
			    TAILQ_INSERT_HEAD(&extra_deleg, ndp, nfsdl_list);

			/* and reclaim all byte range locks */
			lp = LIST_FIRST(&op->nfso_lock);
			while (lp != NULL) {
			    nlp = LIST_NEXT(lp, nfsl_list);
			    lp->nfsl_seqid = 0;
			    firstlock = 1;
			    lop = LIST_FIRST(&lp->nfsl_lock);
			    while (lop != NULL) {
				nlop = LIST_NEXT(lop, nfslo_list);
				if (lop->nfslo_end == NFS64BITSSET)
				    len = NFS64BITSSET;
				else
				    len = lop->nfslo_end - lop->nfslo_first;
				error = nfscl_trylock(nmp, NULL,
				    op->nfso_fh, op->nfso_fhlen, lp,
				    firstlock, 1, lop->nfslo_first, len,
				    lop->nfslo_type, tcred, p);
				if (error != 0)
				    nfscl_freelock(lop, 0);
				else
				    firstlock = 0;
				lop = nlop;
			    }
			    /* If no locks, but a lockowner, just delete it. */
			    if (LIST_EMPTY(&lp->nfsl_lock))
				nfscl_freelockowner(lp, 0);
			    lp = nlp;
			}
		    }
		}
		if (error != 0 && error != NFSERR_BADSESSION)
		    nfscl_freeopen(op, 0);
		op = nop;
	    }
	    owp = nowp;
	}

	/*
	 * Now, try and get any delegations not yet reclaimed by cobbling
	 * to-gether an appropriate open.
	 */
	nowp = NULL;
	dp = TAILQ_FIRST(&clp->nfsc_deleg);
	while (dp != NULL) {
	    ndp = TAILQ_NEXT(dp, nfsdl_list);
	    if ((dp->nfsdl_flags & NFSCLDL_NEEDRECLAIM)) {
		if (nowp == NULL) {
		    nowp = malloc(
			sizeof (struct nfsclowner), M_NFSCLOWNER, M_WAITOK);
		    /*
		     * Name must be as long an largest possible
		     * NFSV4CL_LOCKNAMELEN. 12 for now.
		     */
		    NFSBCOPY("RECLAIMDELEG", nowp->nfsow_owner,
			NFSV4CL_LOCKNAMELEN);
		    LIST_INIT(&nowp->nfsow_open);
		    nowp->nfsow_clp = clp;
		    nowp->nfsow_seqid = 0;
		    nowp->nfsow_defunct = 0;
		    nfscl_lockinit(&nowp->nfsow_rwlock);
		}
		nop = NULL;
		if (error != NFSERR_NOGRACE && error != NFSERR_BADSESSION) {
		    nop = malloc(sizeof (struct nfsclopen) +
			dp->nfsdl_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
		    nop->nfso_own = nowp;
		    if ((dp->nfsdl_flags & NFSCLDL_WRITE)) {
			nop->nfso_mode = NFSV4OPEN_ACCESSWRITE;
			delegtype = NFSV4OPEN_DELEGATEWRITE;
		    } else {
			nop->nfso_mode = NFSV4OPEN_ACCESSREAD;
			delegtype = NFSV4OPEN_DELEGATEREAD;
		    }
		    nop->nfso_opencnt = 0;
		    nop->nfso_posixlock = 1;
		    nop->nfso_fhlen = dp->nfsdl_fhlen;
		    NFSBCOPY(dp->nfsdl_fh, nop->nfso_fh, dp->nfsdl_fhlen);
		    LIST_INIT(&nop->nfso_lock);
		    nop->nfso_stateid.seqid = 0;
		    nop->nfso_stateid.other[0] = 0;
		    nop->nfso_stateid.other[1] = 0;
		    nop->nfso_stateid.other[2] = 0;
		    newnfs_copycred(&dp->nfsdl_cred, tcred);
		    newnfs_copyincred(tcred, &nop->nfso_cred);
		    tdp = NULL;
		    error = nfscl_tryopen(nmp, NULL, nop->nfso_fh,
			nop->nfso_fhlen, nop->nfso_fh, nop->nfso_fhlen,
			nop->nfso_mode, nop, NULL, 0, &tdp, 1,
			delegtype, tcred, p);
		    if (tdp != NULL) {
			if ((tdp->nfsdl_flags & NFSCLDL_WRITE))
			    mode = NFSV4OPEN_ACCESSWRITE;
			else
			    mode = NFSV4OPEN_ACCESSREAD;
			if ((nop->nfso_mode & mode) == mode &&
			    nop->nfso_fhlen == tdp->nfsdl_fhlen &&
			    !NFSBCMP(nop->nfso_fh, tdp->nfsdl_fh,
			    nop->nfso_fhlen)) {
			    dp->nfsdl_stateid = tdp->nfsdl_stateid;
			    dp->nfsdl_sizelimit = tdp->nfsdl_sizelimit;
			    dp->nfsdl_ace = tdp->nfsdl_ace;
			    dp->nfsdl_change = tdp->nfsdl_change;
			    dp->nfsdl_flags &= ~NFSCLDL_NEEDRECLAIM;
			    if ((tdp->nfsdl_flags & NFSCLDL_RECALL))
				dp->nfsdl_flags |= NFSCLDL_RECALL;
			    free(tdp, M_NFSCLDELEG);
			} else {
			    TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
			}
		    }
		}
		if (error) {
		    if (nop != NULL)
			free(nop, M_NFSCLOPEN);
		    /*
		     * Couldn't reclaim it, so throw the state
		     * away. Ouch!!
		     */
		    nfscl_cleandeleg(dp);
		    nfscl_freedeleg(&clp->nfsc_deleg, dp);
		} else {
		    LIST_INSERT_HEAD(&extra_open, nop, nfso_list);
		}
	    }
	    dp = ndp;
	}

	/*
	 * Now, get rid of extra Opens and Delegations.
	 */
	LIST_FOREACH_SAFE(op, &extra_open, nfso_list, nop) {
		do {
			newnfs_copycred(&op->nfso_cred, tcred);
			error = nfscl_tryclose(op, tcred, nmp, p);
			if (error == NFSERR_GRACE)
				(void) nfs_catnap(PZERO, error, "nfsexcls");
		} while (error == NFSERR_GRACE);
		LIST_REMOVE(op, nfso_list);
		free(op, M_NFSCLOPEN);
	}
	if (nowp != NULL)
		free(nowp, M_NFSCLOWNER);

	TAILQ_FOREACH_SAFE(dp, &extra_deleg, nfsdl_list, ndp) {
		do {
			newnfs_copycred(&dp->nfsdl_cred, tcred);
			error = nfscl_trydelegreturn(dp, tcred, nmp, p);
			if (error == NFSERR_GRACE)
				(void) nfs_catnap(PZERO, error, "nfsexdlg");
		} while (error == NFSERR_GRACE);
		TAILQ_REMOVE(&extra_deleg, dp, nfsdl_list);
		free(dp, M_NFSCLDELEG);
	}

	/* For NFSv4.1 or later, do a RECLAIM_COMPLETE. */
	if (NFSHASNFSV4N(nmp))
		(void)nfsrpc_reclaimcomplete(nmp, cred, p);

	NFSLOCKCLSTATE();
	clp->nfsc_flags &= ~NFSCLFLAGS_RECVRINPROG;
	wakeup(&clp->nfsc_flags);
	nfsv4_unlock(&clp->nfsc_lock, 0);
	NFSUNLOCKCLSTATE();
	NFSFREECRED(tcred);
}

/*
 * This function is called when a server replies with NFSERR_EXPIRED.
 * It deletes all state for the client and does a fresh SetClientId/confirm.
 * XXX Someday it should post a signal to the process(es) that hold the
 * state, so they know that lock state has been lost.
 */
APPLESTATIC int
nfscl_hasexpired(struct nfsclclient *clp, u_int32_t clidrev, NFSPROC_T *p)
{
	struct nfsmount *nmp;
	struct ucred *cred;
	int igotlock = 0, error, trycnt;

	/*
	 * If the clientid has gone away or a new SetClientid has already
	 * been done, just return ok.
	 */
	if (clp == NULL || clidrev != clp->nfsc_clientidrev)
		return (0);

	/*
	 * First, lock the client structure, so everyone else will
	 * block when trying to use state. Also, use NFSCLFLAGS_EXPIREIT so
	 * that only one thread does the work.
	 */
	NFSLOCKCLSTATE();
	clp->nfsc_flags |= NFSCLFLAGS_EXPIREIT;
	do {
		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
		    NFSCLSTATEMUTEXPTR, NULL);
	} while (!igotlock && (clp->nfsc_flags & NFSCLFLAGS_EXPIREIT));
	if ((clp->nfsc_flags & NFSCLFLAGS_EXPIREIT) == 0) {
		if (igotlock)
			nfsv4_unlock(&clp->nfsc_lock, 0);
		NFSUNLOCKCLSTATE();
		return (0);
	}
	clp->nfsc_flags |= NFSCLFLAGS_RECVRINPROG;
	NFSUNLOCKCLSTATE();

	nmp = clp->nfsc_nmp;
	if (nmp == NULL)
		panic("nfscl expired");
	cred = newnfs_getcred();
	trycnt = 5;
	do {
		error = nfsrpc_setclient(nmp, clp, 0, cred, p);
	} while ((error == NFSERR_STALECLIENTID ||
	     error == NFSERR_BADSESSION ||
	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
	if (error) {
		NFSLOCKCLSTATE();
		clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
	} else {
		/*
		 * Expire the state for the client.
		 */
		nfscl_expireclient(clp, nmp, cred, p);
		NFSLOCKCLSTATE();
		clp->nfsc_flags |= NFSCLFLAGS_HASCLIENTID;
		clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
	}
	clp->nfsc_flags &= ~(NFSCLFLAGS_EXPIREIT | NFSCLFLAGS_RECVRINPROG);
	wakeup(&clp->nfsc_flags);
	nfsv4_unlock(&clp->nfsc_lock, 0);
	NFSUNLOCKCLSTATE();
	NFSFREECRED(cred);
	return (error);
}

/*
 * This function inserts a lock in the list after insert_lop.
 */
static void
nfscl_insertlock(struct nfscllockowner *lp, struct nfscllock *new_lop,
    struct nfscllock *insert_lop, int local)
{

	if ((struct nfscllockowner *)insert_lop == lp)
		LIST_INSERT_HEAD(&lp->nfsl_lock, new_lop, nfslo_list);
	else
		LIST_INSERT_AFTER(insert_lop, new_lop, nfslo_list);
	if (local)
		nfsstatsv1.cllocallocks++;
	else
		nfsstatsv1.cllocks++;
}

/*
 * This function updates the locking for a lock owner and given file. It
 * maintains a list of lock ranges ordered on increasing file offset that
 * are NFSCLLOCK_READ or NFSCLLOCK_WRITE and non-overlapping (aka POSIX style).
 * It always adds new_lop to the list and sometimes uses the one pointed
 * at by other_lopp.
 * Returns 1 if the locks were modified, 0 otherwise.
 */
static int
nfscl_updatelock(struct nfscllockowner *lp, struct nfscllock **new_lopp,
    struct nfscllock **other_lopp, int local)
{
	struct nfscllock *new_lop = *new_lopp;
	struct nfscllock *lop, *tlop, *ilop;
	struct nfscllock *other_lop;
	int unlock = 0, modified = 0;
	u_int64_t tmp;

	/*
	 * Work down the list until the lock is merged.
	 */
	if (new_lop->nfslo_type == F_UNLCK)
		unlock = 1;
	ilop = (struct nfscllock *)lp;
	lop = LIST_FIRST(&lp->nfsl_lock);
	while (lop != NULL) {
	    /*
	     * Only check locks for this file that aren't before the start of
	     * new lock's range.
	     */
	    if (lop->nfslo_end >= new_lop->nfslo_first) {
		if (new_lop->nfslo_end < lop->nfslo_first) {
		    /*
		     * If the new lock ends before the start of the
		     * current lock's range, no merge, just insert
		     * the new lock.
		     */
		    break;
		}
		if (new_lop->nfslo_type == lop->nfslo_type ||
		    (new_lop->nfslo_first <= lop->nfslo_first &&
		     new_lop->nfslo_end >= lop->nfslo_end)) {
		    /*
		     * This lock can be absorbed by the new lock/unlock.
		     * This happens when it covers the entire range
		     * of the old lock or is contiguous
		     * with the old lock and is of the same type or an
		     * unlock.
		     */
		    if (new_lop->nfslo_type != lop->nfslo_type ||
			new_lop->nfslo_first != lop->nfslo_first ||
			new_lop->nfslo_end != lop->nfslo_end)
			modified = 1;
		    if (lop->nfslo_first < new_lop->nfslo_first)
			new_lop->nfslo_first = lop->nfslo_first;
		    if (lop->nfslo_end > new_lop->nfslo_end)
			new_lop->nfslo_end = lop->nfslo_end;
		    tlop = lop;
		    lop = LIST_NEXT(lop, nfslo_list);
		    nfscl_freelock(tlop, local);
		    continue;
		}

		/*
		 * All these cases are for contiguous locks that are not the
		 * same type, so they can't be merged.
		 */
		if (new_lop->nfslo_first <= lop->nfslo_first) {
		    /*
		     * This case is where the new lock overlaps with the
		     * first part of the old lock. Move the start of the
		     * old lock to just past the end of the new lock. The
		     * new lock will be inserted in front of the old, since
		     * ilop hasn't been updated. (We are done now.)
		     */
		    if (lop->nfslo_first != new_lop->nfslo_end) {
			lop->nfslo_first = new_lop->nfslo_end;
			modified = 1;
		    }
		    break;
		}
		if (new_lop->nfslo_end >= lop->nfslo_end) {
		    /*
		     * This case is where the new lock overlaps with the
		     * end of the old lock's range. Move the old lock's
		     * end to just before the new lock's first and insert
		     * the new lock after the old lock.
		     * Might not be done yet, since the new lock could
		     * overlap further locks with higher ranges.
		     */
		    if (lop->nfslo_end != new_lop->nfslo_first) {
			lop->nfslo_end = new_lop->nfslo_first;
			modified = 1;
		    }
		    ilop = lop;
		    lop = LIST_NEXT(lop, nfslo_list);
		    continue;
		}
		/*
		 * The final case is where the new lock's range is in the
		 * middle of the current lock's and splits the current lock
		 * up. Use *other_lopp to handle the second part of the
		 * split old lock range. (We are done now.)
		 * For unlock, we use new_lop as other_lop and tmp, since
		 * other_lop and new_lop are the same for this case.
		 * We noted the unlock case above, so we don't need
		 * new_lop->nfslo_type any longer.
		 */
		tmp = new_lop->nfslo_first;
		if (unlock) {
		    other_lop = new_lop;
		    *new_lopp = NULL;
		} else {
		    other_lop = *other_lopp;
		    *other_lopp = NULL;
		}
		other_lop->nfslo_first = new_lop->nfslo_end;
		other_lop->nfslo_end = lop->nfslo_end;
		other_lop->nfslo_type = lop->nfslo_type;
		lop->nfslo_end = tmp;
		nfscl_insertlock(lp, other_lop, lop, local);
		ilop = lop;
		modified = 1;
		break;
	    }
	    ilop = lop;
	    lop = LIST_NEXT(lop, nfslo_list);
	    if (lop == NULL)
		break;
	}

	/*
	 * Insert the new lock in the list at the appropriate place.
	 */
	if (!unlock) {
		nfscl_insertlock(lp, new_lop, ilop, local);
		*new_lopp = NULL;
		modified = 1;
	}
	return (modified);
}

/*
 * This function must be run as a kernel thread.
 * It does Renew Ops and recovery, when required.
 */
APPLESTATIC void
nfscl_renewthread(struct nfsclclient *clp, NFSPROC_T *p)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op;
	struct nfscllockowner *lp, *nlp;
	struct nfscldeleghead dh;
	struct nfscldeleg *dp, *ndp;
	struct ucred *cred;
	u_int32_t clidrev;
	int error, cbpathdown, islept, igotlock, ret, clearok;
	uint32_t recover_done_time = 0;
	time_t mytime;
	static time_t prevsec = 0;
	struct nfscllockownerfh *lfhp, *nlfhp;
	struct nfscllockownerfhhead lfh;
	struct nfscllayout *lyp, *nlyp;
	struct nfscldevinfo *dip, *ndip;
	struct nfscllayouthead rlh;
	struct nfsclrecalllayout *recallp;
	struct nfsclds *dsp;

	cred = newnfs_getcred();
	NFSLOCKCLSTATE();
	clp->nfsc_flags |= NFSCLFLAGS_HASTHREAD;
	NFSUNLOCKCLSTATE();
	for(;;) {
		newnfs_setroot(cred);
		cbpathdown = 0;
		if (clp->nfsc_flags & NFSCLFLAGS_RECOVER) {
			/*
			 * Only allow one recover within 1/2 of the lease
			 * duration (nfsc_renew).
			 */
			if (recover_done_time < NFSD_MONOSEC) {
				recover_done_time = NFSD_MONOSEC +
				    clp->nfsc_renew;
				NFSCL_DEBUG(1, "Doing recovery..\n");
				nfscl_recover(clp, cred, p);
			} else {
				NFSCL_DEBUG(1, "Clear Recovery dt=%u ms=%jd\n",
				    recover_done_time, (intmax_t)NFSD_MONOSEC);
				NFSLOCKCLSTATE();
				clp->nfsc_flags &= ~NFSCLFLAGS_RECOVER;
				NFSUNLOCKCLSTATE();
			}
		}
		if (clp->nfsc_expire <= NFSD_MONOSEC &&
		    (clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID)) {
			clp->nfsc_expire = NFSD_MONOSEC + clp->nfsc_renew;
			clidrev = clp->nfsc_clientidrev;
			error = nfsrpc_renew(clp, NULL, cred, p);
			if (error == NFSERR_CBPATHDOWN)
			    cbpathdown = 1;
			else if (error == NFSERR_STALECLIENTID ||
			    error == NFSERR_BADSESSION) {
			    NFSLOCKCLSTATE();
			    clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
			    NFSUNLOCKCLSTATE();
			} else if (error == NFSERR_EXPIRED)
			    (void) nfscl_hasexpired(clp, clidrev, p);
		}

checkdsrenew:
		if (NFSHASNFSV4N(clp->nfsc_nmp)) {
			/* Do renews for any DS sessions. */
			NFSLOCKMNT(clp->nfsc_nmp);
			/* Skip first entry, since the MDS is handled above. */
			dsp = TAILQ_FIRST(&clp->nfsc_nmp->nm_sess);
			if (dsp != NULL)
				dsp = TAILQ_NEXT(dsp, nfsclds_list);
			while (dsp != NULL) {
				if (dsp->nfsclds_expire <= NFSD_MONOSEC &&
				    dsp->nfsclds_sess.nfsess_defunct == 0) {
					dsp->nfsclds_expire = NFSD_MONOSEC +
					    clp->nfsc_renew;
					NFSUNLOCKMNT(clp->nfsc_nmp);
					(void)nfsrpc_renew(clp, dsp, cred, p);
					goto checkdsrenew;
				}
				dsp = TAILQ_NEXT(dsp, nfsclds_list);
			}
			NFSUNLOCKMNT(clp->nfsc_nmp);
		}

		TAILQ_INIT(&dh);
		NFSLOCKCLSTATE();
		if (cbpathdown)
			/* It's a Total Recall! */
			nfscl_totalrecall(clp);

		/*
		 * Now, handle defunct owners.
		 */
		LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
			if (LIST_EMPTY(&owp->nfsow_open)) {
				if (owp->nfsow_defunct != 0)
					nfscl_freeopenowner(owp, 0);
			}
		}

		/*
		 * Do the recall on any delegations. To avoid trouble, always
		 * come back up here after having slept.
		 */
		igotlock = 0;
tryagain:
		dp = TAILQ_FIRST(&clp->nfsc_deleg);
		while (dp != NULL) {
			ndp = TAILQ_NEXT(dp, nfsdl_list);
			if ((dp->nfsdl_flags & NFSCLDL_RECALL)) {
				/*
				 * Wait for outstanding I/O ops to be done.
				 */
				if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
				    if (igotlock) {
					nfsv4_unlock(&clp->nfsc_lock, 0);
					igotlock = 0;
				    }
				    dp->nfsdl_rwlock.nfslock_lock |=
					NFSV4LOCK_WANTED;
				    (void) nfsmsleep(&dp->nfsdl_rwlock,
					NFSCLSTATEMUTEXPTR, PZERO, "nfscld",
					NULL);
				    goto tryagain;
				}
				while (!igotlock) {
				    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
					&islept, NFSCLSTATEMUTEXPTR, NULL);
				    if (islept)
					goto tryagain;
				}
				NFSUNLOCKCLSTATE();
				newnfs_copycred(&dp->nfsdl_cred, cred);
				ret = nfscl_recalldeleg(clp, clp->nfsc_nmp, dp,
				    NULL, cred, p, 1);
				if (!ret) {
				    nfscl_cleandeleg(dp);
				    TAILQ_REMOVE(&clp->nfsc_deleg, dp,
					nfsdl_list);
				    LIST_REMOVE(dp, nfsdl_hash);
				    TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
				    nfscl_delegcnt--;
				    nfsstatsv1.cldelegates--;
				}
				NFSLOCKCLSTATE();
			}
			dp = ndp;
		}

		/*
		 * Clear out old delegations, if we are above the high water
		 * mark. Only clear out ones with no state related to them.
		 * The tailq list is in LRU order.
		 */
		dp = TAILQ_LAST(&clp->nfsc_deleg, nfscldeleghead);
		while (nfscl_delegcnt > nfscl_deleghighwater && dp != NULL) {
		    ndp = TAILQ_PREV(dp, nfscldeleghead, nfsdl_list);
		    if (dp->nfsdl_rwlock.nfslock_usecnt == 0 &&
			dp->nfsdl_rwlock.nfslock_lock == 0 &&
			dp->nfsdl_timestamp < NFSD_MONOSEC &&
			(dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_ZAPPED |
			  NFSCLDL_NEEDRECLAIM | NFSCLDL_DELEGRET)) == 0) {
			clearok = 1;
			LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
			    op = LIST_FIRST(&owp->nfsow_open);
			    if (op != NULL) {
				clearok = 0;
				break;
			    }
			}
			if (clearok) {
			    LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
				if (!LIST_EMPTY(&lp->nfsl_lock)) {
				    clearok = 0;
				    break;
				}
			    }
			}
			if (clearok) {
			    TAILQ_REMOVE(&clp->nfsc_deleg, dp, nfsdl_list);
			    LIST_REMOVE(dp, nfsdl_hash);
			    TAILQ_INSERT_HEAD(&dh, dp, nfsdl_list);
			    nfscl_delegcnt--;
			    nfsstatsv1.cldelegates--;
			}
		    }
		    dp = ndp;
		}
		if (igotlock)
			nfsv4_unlock(&clp->nfsc_lock, 0);

		/*
		 * Do the recall on any layouts. To avoid trouble, always
		 * come back up here after having slept.
		 */
		TAILQ_INIT(&rlh);
tryagain2:
		TAILQ_FOREACH_SAFE(lyp, &clp->nfsc_layout, nfsly_list, nlyp) {
			if ((lyp->nfsly_flags & NFSLY_RECALL) != 0) {
				/*
				 * Wait for outstanding I/O ops to be done.
				 */
				if (lyp->nfsly_lock.nfslock_usecnt > 0 ||
				    (lyp->nfsly_lock.nfslock_lock &
				     NFSV4LOCK_LOCK) != 0) {
					lyp->nfsly_lock.nfslock_lock |=
					    NFSV4LOCK_WANTED;
					nfsmsleep(&lyp->nfsly_lock.nfslock_lock,
					    NFSCLSTATEMUTEXPTR, PZERO, "nfslyp",
					    NULL);
					goto tryagain2;
				}
				/* Move the layout to the recall list. */
				TAILQ_REMOVE(&clp->nfsc_layout, lyp,
				    nfsly_list);
				LIST_REMOVE(lyp, nfsly_hash);
				TAILQ_INSERT_HEAD(&rlh, lyp, nfsly_list);

				/* Handle any layout commits. */
				if (!NFSHASNOLAYOUTCOMMIT(clp->nfsc_nmp) &&
				    (lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
					lyp->nfsly_flags &= ~NFSLY_WRITTEN;
					NFSUNLOCKCLSTATE();
					NFSCL_DEBUG(3, "do layoutcommit\n");
					nfscl_dolayoutcommit(clp->nfsc_nmp, lyp,
					    cred, p);
					NFSLOCKCLSTATE();
					goto tryagain2;
				}
			}
		}

		/* Now, look for stale layouts. */
		lyp = TAILQ_LAST(&clp->nfsc_layout, nfscllayouthead);
		while (lyp != NULL) {
			nlyp = TAILQ_PREV(lyp, nfscllayouthead, nfsly_list);
			if (lyp->nfsly_timestamp < NFSD_MONOSEC &&
			    (lyp->nfsly_flags & NFSLY_RECALL) == 0 &&
			    lyp->nfsly_lock.nfslock_usecnt == 0 &&
			    lyp->nfsly_lock.nfslock_lock == 0) {
				NFSCL_DEBUG(4, "ret stale lay=%d\n",
				    nfscl_layoutcnt);
				recallp = malloc(sizeof(*recallp),
				    M_NFSLAYRECALL, M_NOWAIT);
				if (recallp == NULL)
					break;
				(void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE,
				    lyp, NFSLAYOUTIOMODE_ANY, 0, UINT64_MAX,
				    lyp->nfsly_stateid.seqid, 0, 0, NULL,
				    recallp);
			}
			lyp = nlyp;
		}

		/*
		 * Free up any unreferenced device info structures.
		 */
		LIST_FOREACH_SAFE(dip, &clp->nfsc_devinfo, nfsdi_list, ndip) {
			if (dip->nfsdi_layoutrefs == 0 &&
			    dip->nfsdi_refcnt == 0) {
				NFSCL_DEBUG(4, "freeing devinfo\n");
				LIST_REMOVE(dip, nfsdi_list);
				nfscl_freedevinfo(dip);
			}
		}
		NFSUNLOCKCLSTATE();

		/* Do layout return(s), as required. */
		TAILQ_FOREACH_SAFE(lyp, &rlh, nfsly_list, nlyp) {
			TAILQ_REMOVE(&rlh, lyp, nfsly_list);
			NFSCL_DEBUG(4, "ret layout\n");
			nfscl_layoutreturn(clp->nfsc_nmp, lyp, cred, p);
			nfscl_freelayout(lyp);
		}

		/*
		 * Delegreturn any delegations cleaned out or recalled.
		 */
		TAILQ_FOREACH_SAFE(dp, &dh, nfsdl_list, ndp) {
			newnfs_copycred(&dp->nfsdl_cred, cred);
			(void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
			TAILQ_REMOVE(&dh, dp, nfsdl_list);
			free(dp, M_NFSCLDELEG);
		}

		SLIST_INIT(&lfh);
		/*
		 * Call nfscl_cleanupkext() once per second to check for
		 * open/lock owners where the process has exited.
		 */
		mytime = NFSD_MONOSEC;
		if (prevsec != mytime) {
			prevsec = mytime;
			nfscl_cleanupkext(clp, &lfh);
		}

		/*
		 * Do a ReleaseLockOwner for all lock owners where the
		 * associated process no longer exists, as found by
		 * nfscl_cleanupkext().
		 */
		newnfs_setroot(cred);
		SLIST_FOREACH_SAFE(lfhp, &lfh, nfslfh_list, nlfhp) {
			LIST_FOREACH_SAFE(lp, &lfhp->nfslfh_lock, nfsl_list,
			    nlp) {
				(void)nfsrpc_rellockown(clp->nfsc_nmp, lp,
				    lfhp->nfslfh_fh, lfhp->nfslfh_len, cred,
				    p);
				nfscl_freelockowner(lp, 0);
			}
			free(lfhp, M_TEMP);
		}
		SLIST_INIT(&lfh);

		NFSLOCKCLSTATE();
		if ((clp->nfsc_flags & NFSCLFLAGS_RECOVER) == 0)
			(void)mtx_sleep(clp, NFSCLSTATEMUTEXPTR, PWAIT, "nfscl",
			    hz);
		if (clp->nfsc_flags & NFSCLFLAGS_UMOUNT) {
			clp->nfsc_flags &= ~NFSCLFLAGS_HASTHREAD;
			NFSUNLOCKCLSTATE();
			NFSFREECRED(cred);
			wakeup((caddr_t)clp);
			return;
		}
		NFSUNLOCKCLSTATE();
	}
}

/*
 * Initiate state recovery. Called when NFSERR_STALECLIENTID,
 * NFSERR_STALESTATEID or NFSERR_BADSESSION is received.
 */
APPLESTATIC void
nfscl_initiate_recovery(struct nfsclclient *clp)
{

	if (clp == NULL)
		return;
	NFSLOCKCLSTATE();
	clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
	NFSUNLOCKCLSTATE();
	wakeup((caddr_t)clp);
}

/*
 * Dump out the state stuff for debugging.
 */
APPLESTATIC void
nfscl_dumpstate(struct nfsmount *nmp, int openowner, int opens,
    int lockowner, int locks)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;
	struct nfsclopen *op;
	struct nfscllockowner *lp;
	struct nfscllock *lop;
	struct nfscldeleg *dp;

	clp = nmp->nm_clp;
	if (clp == NULL) {
		printf("nfscl dumpstate NULL clp\n");
		return;
	}
	NFSLOCKCLSTATE();
	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
	  LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
	    if (openowner && !LIST_EMPTY(&owp->nfsow_open))
		printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
		    owp->nfsow_owner[0], owp->nfsow_owner[1],
		    owp->nfsow_owner[2], owp->nfsow_owner[3],
		    owp->nfsow_seqid);
	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if (opens)
		    printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
			op->nfso_stateid.other[0], op->nfso_stateid.other[1],
			op->nfso_stateid.other[2], op->nfso_opencnt,
			op->nfso_fh[12]);
		LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
		    if (lockowner)
			printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
			    lp->nfsl_owner[0], lp->nfsl_owner[1],
			    lp->nfsl_owner[2], lp->nfsl_owner[3],
			    lp->nfsl_seqid,
			    lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
			    lp->nfsl_stateid.other[2]);
		    LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
			if (locks)
#ifdef __FreeBSD__
			    printf("lck typ=%d fst=%ju end=%ju\n",
				lop->nfslo_type, (intmax_t)lop->nfslo_first,
				(intmax_t)lop->nfslo_end);
#else
			    printf("lck typ=%d fst=%qd end=%qd\n",
				lop->nfslo_type, lop->nfslo_first,
				lop->nfslo_end);
#endif
		    }
		}
	    }
	  }
	}
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
	    if (openowner && !LIST_EMPTY(&owp->nfsow_open))
		printf("owner=0x%x 0x%x 0x%x 0x%x seqid=%d\n",
		    owp->nfsow_owner[0], owp->nfsow_owner[1],
		    owp->nfsow_owner[2], owp->nfsow_owner[3],
		    owp->nfsow_seqid);
	    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if (opens)
		    printf("open st=0x%x 0x%x 0x%x cnt=%d fh12=0x%x\n",
			op->nfso_stateid.other[0], op->nfso_stateid.other[1],
			op->nfso_stateid.other[2], op->nfso_opencnt,
			op->nfso_fh[12]);
		LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
		    if (lockowner)
			printf("lckown=0x%x 0x%x 0x%x 0x%x seqid=%d st=0x%x 0x%x 0x%x\n",
			    lp->nfsl_owner[0], lp->nfsl_owner[1],
			    lp->nfsl_owner[2], lp->nfsl_owner[3],
			    lp->nfsl_seqid,
			    lp->nfsl_stateid.other[0], lp->nfsl_stateid.other[1],
			    lp->nfsl_stateid.other[2]);
		    LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
			if (locks)
#ifdef __FreeBSD__
			    printf("lck typ=%d fst=%ju end=%ju\n",
				lop->nfslo_type, (intmax_t)lop->nfslo_first,
				(intmax_t)lop->nfslo_end);
#else
			    printf("lck typ=%d fst=%qd end=%qd\n",
				lop->nfslo_type, lop->nfslo_first,
				lop->nfslo_end);
#endif
		    }
		}
	    }
	}
	NFSUNLOCKCLSTATE();
}

/*
 * Check for duplicate open owners and opens.
 * (Only used as a diagnostic aid.)
 */
APPLESTATIC void
nfscl_dupopen(vnode_t vp, int dupopens)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp, *owp2;
	struct nfsclopen *op, *op2;
	struct nfsfh *nfhp;

	clp = VFSTONFS(vnode_mount(vp))->nm_clp;
	if (clp == NULL) {
		printf("nfscl dupopen NULL clp\n");
		return;
	}
	nfhp = VTONFS(vp)->n_fhp;
	NFSLOCKCLSTATE();

	/*
	 * First, search for duplicate owners.
	 * These should never happen!
	 */
	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
		if (owp != owp2 &&
		    !NFSBCMP(owp->nfsow_owner, owp2->nfsow_owner,
		    NFSV4CL_LOCKNAMELEN)) {
			NFSUNLOCKCLSTATE();
			printf("DUP OWNER\n");
			nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0, 0);
			return;
		}
	    }
	}

	/*
	 * Now, search for duplicate stateids.
	 * These shouldn't happen, either.
	 */
	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
		LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
		    LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			if (op != op2 &&
			    (op->nfso_stateid.other[0] != 0 ||
			     op->nfso_stateid.other[1] != 0 ||
			     op->nfso_stateid.other[2] != 0) &&
			    op->nfso_stateid.other[0] == op2->nfso_stateid.other[0] &&
			    op->nfso_stateid.other[1] == op2->nfso_stateid.other[1] &&
			    op->nfso_stateid.other[2] == op2->nfso_stateid.other[2]) {
			    NFSUNLOCKCLSTATE();
			    printf("DUP STATEID\n");
			    nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1, 0,
				0);
			    return;
			}
		    }
		}
	    }
	}

	/*
	 * Now search for duplicate opens.
	 * Duplicate opens for the same owner
	 * should never occur. Other duplicates are
	 * possible and are checked for if "dupopens"
	 * is true.
	 */
	LIST_FOREACH(owp2, &clp->nfsc_owner, nfsow_list) {
	    LIST_FOREACH(op2, &owp2->nfsow_open, nfso_list) {
		if (nfhp->nfh_len == op2->nfso_fhlen &&
		    !NFSBCMP(nfhp->nfh_fh, op2->nfso_fh, nfhp->nfh_len)) {
		    LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			    if (op != op2 && nfhp->nfh_len == op->nfso_fhlen &&
				!NFSBCMP(nfhp->nfh_fh, op->nfso_fh, nfhp->nfh_len) &&
				(!NFSBCMP(op->nfso_own->nfsow_owner,
				 op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN) ||
				 dupopens)) {
				if (!NFSBCMP(op->nfso_own->nfsow_owner,
				    op2->nfso_own->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
				    NFSUNLOCKCLSTATE();
				    printf("BADDUP OPEN\n");
				} else {
				    NFSUNLOCKCLSTATE();
				    printf("DUP OPEN\n");
				}
				nfscl_dumpstate(VFSTONFS(vnode_mount(vp)), 1, 1,
				    0, 0);
				return;
			    }
			}
		    }
		}
	    }
	}
	NFSUNLOCKCLSTATE();
}

/*
 * During close, find an open that needs to be dereferenced and
 * dereference it. If there are no more opens for this file,
 * log a message to that effect.
 * Opens aren't actually Close'd until VOP_INACTIVE() is performed
 * on the file's vnode.
 * This is the safe way, since it is difficult to identify
 * which open the close is for and I/O can be performed after the
 * close(2) system call when a file is mmap'd.
 * If it returns 0 for success, there will be a referenced
 * clp returned via clpp.
 */
APPLESTATIC int
nfscl_getclose(vnode_t vp, struct nfsclclient **clpp)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;
	struct nfsclopen *op;
	struct nfscldeleg *dp;
	struct nfsfh *nfhp;
	int error, notdecr;

	error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
	if (error)
		return (error);
	*clpp = clp;

	nfhp = VTONFS(vp)->n_fhp;
	notdecr = 1;
	NFSLOCKCLSTATE();
	/*
	 * First, look for one under a delegation that was locally issued
	 * and just decrement the opencnt for it. Since all my Opens against
	 * the server are DENY_NONE, I don't see a problem with hanging
	 * onto them. (It is much easier to use one of the extant Opens
	 * that I already have on the server when a Delegation is recalled
	 * than to do fresh Opens.) Someday, I might need to rethink this, but.
	 */
	dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
	if (dp != NULL) {
		LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
			op = LIST_FIRST(&owp->nfsow_open);
			if (op != NULL) {
				/*
				 * Since a delegation is for a file, there
				 * should never be more than one open for
				 * each openowner.
				 */
				if (LIST_NEXT(op, nfso_list) != NULL)
					panic("nfscdeleg opens");
				if (notdecr && op->nfso_opencnt > 0) {
					notdecr = 0;
					op->nfso_opencnt--;
					break;
				}
			}
		}
	}

	/* Now process the opens against the server. */
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			if (op->nfso_fhlen == nfhp->nfh_len &&
			    !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
			    nfhp->nfh_len)) {
				/* Found an open, decrement cnt if possible */
				if (notdecr && op->nfso_opencnt > 0) {
					notdecr = 0;
					op->nfso_opencnt--;
				}
				/*
				 * There are more opens, so just return.
				 */
				if (op->nfso_opencnt > 0) {
					NFSUNLOCKCLSTATE();
					return (0);
				}
			}
		}
	}
	NFSUNLOCKCLSTATE();
	if (notdecr)
		printf("nfscl: never fnd open\n");
	return (0);
}

APPLESTATIC int
nfscl_doclose(vnode_t vp, struct nfsclclient **clpp, NFSPROC_T *p)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op;
	struct nfscldeleg *dp;
	struct nfsfh *nfhp;
	struct nfsclrecalllayout *recallp;
	int error;

	error = nfscl_getcl(vnode_mount(vp), NULL, NULL, 1, &clp);
	if (error)
		return (error);
	*clpp = clp;

	nfhp = VTONFS(vp)->n_fhp;
	recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
	NFSLOCKCLSTATE();
	/*
	 * First get rid of the local Open structures, which should be no
	 * longer in use.
	 */
	dp = nfscl_finddeleg(clp, nfhp->nfh_fh, nfhp->nfh_len);
	if (dp != NULL) {
		LIST_FOREACH_SAFE(owp, &dp->nfsdl_owner, nfsow_list, nowp) {
			op = LIST_FIRST(&owp->nfsow_open);
			if (op != NULL) {
				KASSERT((op->nfso_opencnt == 0),
				    ("nfscl: bad open cnt on deleg"));
				nfscl_freeopen(op, 1);
			}
			nfscl_freeopenowner(owp, 1);
		}
	}

	/* Return any layouts marked return on close. */
	nfscl_retoncloselayout(vp, clp, nfhp->nfh_fh, nfhp->nfh_len, &recallp);

	/* Now process the opens against the server. */
lookformore:
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
		op = LIST_FIRST(&owp->nfsow_open);
		while (op != NULL) {
			if (op->nfso_fhlen == nfhp->nfh_len &&
			    !NFSBCMP(op->nfso_fh, nfhp->nfh_fh,
			    nfhp->nfh_len)) {
				/* Found an open, close it. */
				KASSERT((op->nfso_opencnt == 0),
				    ("nfscl: bad open cnt on server"));
				NFSUNLOCKCLSTATE();
				nfsrpc_doclose(VFSTONFS(vnode_mount(vp)), op,
				    p);
				NFSLOCKCLSTATE();
				goto lookformore;
			}
			op = LIST_NEXT(op, nfso_list);
		}
	}
	NFSUNLOCKCLSTATE();
	/*
	 * recallp has been set NULL by nfscl_retoncloselayout() if it was
	 * used by the function, but calling free() with a NULL pointer is ok.
	 */
	free(recallp, M_NFSLAYRECALL);
	return (0);
}

/*
 * Return all delegations on this client.
 * (Must be called with client sleep lock.)
 */
static void
nfscl_delegreturnall(struct nfsclclient *clp, NFSPROC_T *p)
{
	struct nfscldeleg *dp, *ndp;
	struct ucred *cred;

	cred = newnfs_getcred();
	TAILQ_FOREACH_SAFE(dp, &clp->nfsc_deleg, nfsdl_list, ndp) {
		nfscl_cleandeleg(dp);
		(void) nfscl_trydelegreturn(dp, cred, clp->nfsc_nmp, p);
		nfscl_freedeleg(&clp->nfsc_deleg, dp);
	}
	NFSFREECRED(cred);
}

/*
 * Do a callback RPC.
 */
APPLESTATIC void
nfscl_docb(struct nfsrv_descript *nd, NFSPROC_T *p)
{
	int clist, gotseq_ok, i, j, k, op, rcalls;
	u_int32_t *tl;
	struct nfsclclient *clp;
	struct nfscldeleg *dp = NULL;
	int numops, taglen = -1, error = 0, trunc __unused;
	u_int32_t minorvers = 0, retops = 0, *retopsp = NULL, *repp, cbident;
	u_char tag[NFSV4_SMALLSTR + 1], *tagstr;
	vnode_t vp = NULL;
	struct nfsnode *np;
	struct vattr va;
	struct nfsfh *nfhp;
	mount_t mp;
	nfsattrbit_t attrbits, rattrbits;
	nfsv4stateid_t stateid;
	uint32_t seqid, slotid = 0, highslot, cachethis __unused;
	uint8_t sessionid[NFSX_V4SESSIONID];
	struct mbuf *rep;
	struct nfscllayout *lyp;
	uint64_t filesid[2], len, off;
	int changed, gotone, laytype, recalltype;
	uint32_t iomode;
	struct nfsclrecalllayout *recallp = NULL;
	struct nfsclsession *tsep;

	gotseq_ok = 0;
	nfsrvd_rephead(nd);
	NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
	taglen = fxdr_unsigned(int, *tl);
	if (taglen < 0) {
		error = EBADRPC;
		goto nfsmout;
	}
	if (taglen <= NFSV4_SMALLSTR)
		tagstr = tag;
	else
		tagstr = malloc(taglen + 1, M_TEMP, M_WAITOK);
	error = nfsrv_mtostr(nd, tagstr, taglen);
	if (error) {
		if (taglen > NFSV4_SMALLSTR)
			free(tagstr, M_TEMP);
		taglen = -1;
		goto nfsmout;
	}
	(void) nfsm_strtom(nd, tag, taglen);
	if (taglen > NFSV4_SMALLSTR) {
		free(tagstr, M_TEMP);
	}
	NFSM_BUILD(retopsp, u_int32_t *, NFSX_UNSIGNED);
	NFSM_DISSECT(tl, u_int32_t *, 3 * NFSX_UNSIGNED);
	minorvers = fxdr_unsigned(u_int32_t, *tl++);
	if (minorvers != NFSV4_MINORVERSION && minorvers != NFSV41_MINORVERSION)
		nd->nd_repstat = NFSERR_MINORVERMISMATCH;
	cbident = fxdr_unsigned(u_int32_t, *tl++);
	if (nd->nd_repstat)
		numops = 0;
	else
		numops = fxdr_unsigned(int, *tl);
	/*
	 * Loop around doing the sub ops.
	 */
	for (i = 0; i < numops; i++) {
		NFSM_DISSECT(tl, u_int32_t *, NFSX_UNSIGNED);
		NFSM_BUILD(repp, u_int32_t *, 2 * NFSX_UNSIGNED);
		*repp++ = *tl;
		op = fxdr_unsigned(int, *tl);
		if (op < NFSV4OP_CBGETATTR ||
		   (op > NFSV4OP_CBRECALL && minorvers == NFSV4_MINORVERSION) ||
		   (op > NFSV4OP_CBNOTIFYDEVID &&
		    minorvers == NFSV41_MINORVERSION)) {
		    nd->nd_repstat = NFSERR_OPILLEGAL;
		    *repp = nfscl_errmap(nd, minorvers);
		    retops++;
		    break;
		}
		nd->nd_procnum = op;
		if (op < NFSV41_CBNOPS)
			nfsstatsv1.cbrpccnt[nd->nd_procnum]++;
		switch (op) {
		case NFSV4OP_CBGETATTR:
			NFSCL_DEBUG(4, "cbgetattr\n");
			mp = NULL;
			vp = NULL;
			error = nfsm_getfh(nd, &nfhp);
			if (!error)
				error = nfsrv_getattrbits(nd, &attrbits,
				    NULL, NULL);
			if (error == 0 && i == 0 &&
			    minorvers != NFSV4_MINORVERSION)
				error = NFSERR_OPNOTINSESS;
			if (!error) {
				mp = nfscl_getmnt(minorvers, sessionid, cbident,
				    &clp);
				if (mp == NULL)
					error = NFSERR_SERVERFAULT;
			}
			if (!error) {
				error = nfscl_ngetreopen(mp, nfhp->nfh_fh,
				    nfhp->nfh_len, p, &np);
				if (!error)
					vp = NFSTOV(np);
			}
			if (!error) {
				NFSZERO_ATTRBIT(&rattrbits);
				NFSLOCKCLSTATE();
				dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
				    nfhp->nfh_len);
				if (dp != NULL) {
					if (NFSISSET_ATTRBIT(&attrbits,
					    NFSATTRBIT_SIZE)) {
						if (vp != NULL)
							va.va_size = np->n_size;
						else
							va.va_size =
							    dp->nfsdl_size;
						NFSSETBIT_ATTRBIT(&rattrbits,
						    NFSATTRBIT_SIZE);
					}
					if (NFSISSET_ATTRBIT(&attrbits,
					    NFSATTRBIT_CHANGE)) {
						va.va_filerev =
						    dp->nfsdl_change;
						if (vp == NULL ||
						    (np->n_flag & NDELEGMOD))
							va.va_filerev++;
						NFSSETBIT_ATTRBIT(&rattrbits,
						    NFSATTRBIT_CHANGE);
					}
				} else
					error = NFSERR_SERVERFAULT;
				NFSUNLOCKCLSTATE();
			}
			if (vp != NULL)
				vrele(vp);
			if (mp != NULL)
				vfs_unbusy(mp);
			if (nfhp != NULL)
				free(nfhp, M_NFSFH);
			if (!error)
				(void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
				    NULL, 0, &rattrbits, NULL, p, 0, 0, 0, 0,
				    (uint64_t)0, NULL);
			break;
		case NFSV4OP_CBRECALL:
			NFSCL_DEBUG(4, "cbrecall\n");
			NFSM_DISSECT(tl, u_int32_t *, NFSX_STATEID +
			    NFSX_UNSIGNED);
			stateid.seqid = *tl++;
			NFSBCOPY((caddr_t)tl, (caddr_t)stateid.other,
			    NFSX_STATEIDOTHER);
			tl += (NFSX_STATEIDOTHER / NFSX_UNSIGNED);
			trunc = fxdr_unsigned(int, *tl);
			error = nfsm_getfh(nd, &nfhp);
			if (error == 0 && i == 0 &&
			    minorvers != NFSV4_MINORVERSION)
				error = NFSERR_OPNOTINSESS;
			if (!error) {
				NFSLOCKCLSTATE();
				if (minorvers == NFSV4_MINORVERSION)
					clp = nfscl_getclnt(cbident);
				else
					clp = nfscl_getclntsess(sessionid);
				if (clp != NULL) {
					dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
					    nfhp->nfh_len);
					if (dp != NULL && (dp->nfsdl_flags &
					    NFSCLDL_DELEGRET) == 0) {
						dp->nfsdl_flags |=
						    NFSCLDL_RECALL;
						wakeup((caddr_t)clp);
					}
				} else {
					error = NFSERR_SERVERFAULT;
				}
				NFSUNLOCKCLSTATE();
			}
			if (nfhp != NULL)
				free(nfhp, M_NFSFH);
			break;
		case NFSV4OP_CBLAYOUTRECALL:
			NFSCL_DEBUG(4, "cblayrec\n");
			nfhp = NULL;
			NFSM_DISSECT(tl, uint32_t *, 4 * NFSX_UNSIGNED);
			laytype = fxdr_unsigned(int, *tl++);
			iomode = fxdr_unsigned(uint32_t, *tl++);
			if (newnfs_true == *tl++)
				changed = 1;
			else
				changed = 0;
			recalltype = fxdr_unsigned(int, *tl);
			NFSCL_DEBUG(4, "layt=%d iom=%d ch=%d rectyp=%d\n",
			    laytype, iomode, changed, recalltype);
			recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL,
			    M_WAITOK);
			if (laytype != NFSLAYOUT_NFSV4_1_FILES &&
			    laytype != NFSLAYOUT_FLEXFILE)
				error = NFSERR_NOMATCHLAYOUT;
			else if (recalltype == NFSLAYOUTRETURN_FILE) {
				error = nfsm_getfh(nd, &nfhp);
				NFSCL_DEBUG(4, "retfile getfh=%d\n", error);
				if (error != 0)
					goto nfsmout;
				NFSM_DISSECT(tl, u_int32_t *, 2 * NFSX_HYPER +
				    NFSX_STATEID);
				off = fxdr_hyper(tl); tl += 2;
				len = fxdr_hyper(tl); tl += 2;
				stateid.seqid = fxdr_unsigned(uint32_t, *tl++);
				NFSBCOPY(tl, stateid.other, NFSX_STATEIDOTHER);
				if (minorvers == NFSV4_MINORVERSION)
					error = NFSERR_NOTSUPP;
				else if (i == 0)
					error = NFSERR_OPNOTINSESS;
				NFSCL_DEBUG(4, "off=%ju len=%ju sq=%u err=%d\n",
				    (uintmax_t)off, (uintmax_t)len,
				    stateid.seqid, error);
				if (error == 0) {
					NFSLOCKCLSTATE();
					clp = nfscl_getclntsess(sessionid);
					NFSCL_DEBUG(4, "cbly clp=%p\n", clp);
					if (clp != NULL) {
						lyp = nfscl_findlayout(clp,
						    nfhp->nfh_fh,
						    nfhp->nfh_len);
						NFSCL_DEBUG(4, "cblyp=%p\n",
						    lyp);
						if (lyp != NULL &&
						    (lyp->nfsly_flags &
						     (NFSLY_FILES |
						      NFSLY_FLEXFILE)) != 0 &&
						    !NFSBCMP(stateid.other,
						    lyp->nfsly_stateid.other,
						    NFSX_STATEIDOTHER)) {
							error =
							    nfscl_layoutrecall(
							    recalltype,
							    lyp, iomode, off,
							    len, stateid.seqid,
							    0, 0, NULL,
							    recallp);
							recallp = NULL;
							wakeup(clp);
							NFSCL_DEBUG(4,
							    "aft layrcal=%d\n",
							    error);
						} else
							error =
							  NFSERR_NOMATCHLAYOUT;
					} else
						error = NFSERR_NOMATCHLAYOUT;
					NFSUNLOCKCLSTATE();
				}
				free(nfhp, M_NFSFH);
			} else if (recalltype == NFSLAYOUTRETURN_FSID) {
				NFSM_DISSECT(tl, uint32_t *, 2 * NFSX_HYPER);
				filesid[0] = fxdr_hyper(tl); tl += 2;
				filesid[1] = fxdr_hyper(tl); tl += 2;
				gotone = 0;
				NFSLOCKCLSTATE();
				clp = nfscl_getclntsess(sessionid);
				if (clp != NULL) {
					TAILQ_FOREACH(lyp, &clp->nfsc_layout,
					    nfsly_list) {
						if (lyp->nfsly_filesid[0] ==
						    filesid[0] &&
						    lyp->nfsly_filesid[1] ==
						    filesid[1]) {
							error =
							    nfscl_layoutrecall(
							    recalltype,
							    lyp, iomode, 0,
							    UINT64_MAX,
							    lyp->nfsly_stateid.seqid,
							    0, 0, NULL,
							    recallp);
							recallp = NULL;
							gotone = 1;
						}
					}
					if (gotone != 0)
						wakeup(clp);
					else
						error = NFSERR_NOMATCHLAYOUT;
				} else
					error = NFSERR_NOMATCHLAYOUT;
				NFSUNLOCKCLSTATE();
			} else if (recalltype == NFSLAYOUTRETURN_ALL) {
				gotone = 0;
				NFSLOCKCLSTATE();
				clp = nfscl_getclntsess(sessionid);
				if (clp != NULL) {
					TAILQ_FOREACH(lyp, &clp->nfsc_layout,
					    nfsly_list) {
						error = nfscl_layoutrecall(
						    recalltype, lyp, iomode, 0,
						    UINT64_MAX,
						    lyp->nfsly_stateid.seqid,
						    0, 0, NULL, recallp);
						recallp = NULL;
						gotone = 1;
					}
					if (gotone != 0)
						wakeup(clp);
					else
						error = NFSERR_NOMATCHLAYOUT;
				} else
					error = NFSERR_NOMATCHLAYOUT;
				NFSUNLOCKCLSTATE();
			} else
				error = NFSERR_NOMATCHLAYOUT;
			if (recallp != NULL) {
				free(recallp, M_NFSLAYRECALL);
				recallp = NULL;
			}
			break;
		case NFSV4OP_CBSEQUENCE:
			NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
			    5 * NFSX_UNSIGNED);
			bcopy(tl, sessionid, NFSX_V4SESSIONID);
			tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
			seqid = fxdr_unsigned(uint32_t, *tl++);
			slotid = fxdr_unsigned(uint32_t, *tl++);
			highslot = fxdr_unsigned(uint32_t, *tl++);
			cachethis = *tl++;
			/* Throw away the referring call stuff. */
			clist = fxdr_unsigned(int, *tl);
			for (j = 0; j < clist; j++) {
				NFSM_DISSECT(tl, uint32_t *, NFSX_V4SESSIONID +
				    NFSX_UNSIGNED);
				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
				rcalls = fxdr_unsigned(int, *tl);
				for (k = 0; k < rcalls; k++) {
					NFSM_DISSECT(tl, uint32_t *,
					    2 * NFSX_UNSIGNED);
				}
			}
			NFSLOCKCLSTATE();
			if (i == 0) {
				clp = nfscl_getclntsess(sessionid);
				if (clp == NULL)
					error = NFSERR_SERVERFAULT;
			} else
				error = NFSERR_SEQUENCEPOS;
			if (error == 0) {
				tsep = nfsmnt_mdssession(clp->nfsc_nmp);
				error = nfsv4_seqsession(seqid, slotid,
				    highslot, tsep->nfsess_cbslots, &rep,
				    tsep->nfsess_backslots);
			}
			NFSUNLOCKCLSTATE();
			if (error == 0 || error == NFSERR_REPLYFROMCACHE) {
				gotseq_ok = 1;
				if (rep != NULL) {
					/*
					 * Handle a reply for a retried
					 * callback.  The reply will be
					 * re-inserted in the session cache
					 * by the nfsv4_seqsess_cacherep() call
					 * after out:
					 */
					KASSERT(error == NFSERR_REPLYFROMCACHE,
					    ("cbsequence: non-NULL rep"));
					NFSCL_DEBUG(4, "Got cbretry\n");
					m_freem(nd->nd_mreq);
					nd->nd_mreq = rep;
					rep = NULL;
					goto out;
				}
				NFSM_BUILD(tl, uint32_t *,
				    NFSX_V4SESSIONID + 4 * NFSX_UNSIGNED);
				bcopy(sessionid, tl, NFSX_V4SESSIONID);
				tl += NFSX_V4SESSIONID / NFSX_UNSIGNED;
				*tl++ = txdr_unsigned(seqid);
				*tl++ = txdr_unsigned(slotid);
				*tl++ = txdr_unsigned(NFSV4_CBSLOTS - 1);
				*tl = txdr_unsigned(NFSV4_CBSLOTS - 1);
			}
			break;
		default:
			if (i == 0 && minorvers == NFSV41_MINORVERSION)
				error = NFSERR_OPNOTINSESS;
			else {
				NFSCL_DEBUG(1, "unsupp callback %d\n", op);
				error = NFSERR_NOTSUPP;
			}
			break;
		}
		if (error) {
			if (error == EBADRPC || error == NFSERR_BADXDR) {
				nd->nd_repstat = NFSERR_BADXDR;
			} else {
				nd->nd_repstat = error;
			}
			error = 0;
		}
		retops++;
		if (nd->nd_repstat) {
			*repp = nfscl_errmap(nd, minorvers);
			break;
		} else
			*repp = 0;	/* NFS4_OK */
	}
nfsmout:
	if (recallp != NULL)
		free(recallp, M_NFSLAYRECALL);
	if (error) {
		if (error == EBADRPC || error == NFSERR_BADXDR)
			nd->nd_repstat = NFSERR_BADXDR;
		else
			printf("nfsv4 comperr1=%d\n", error);
	}
	if (taglen == -1) {
		NFSM_BUILD(tl, u_int32_t *, 2 * NFSX_UNSIGNED);
		*tl++ = 0;
		*tl = 0;
	} else {
		*retopsp = txdr_unsigned(retops);
	}
	*nd->nd_errp = nfscl_errmap(nd, minorvers);
out:
	if (gotseq_ok != 0) {
		rep = m_copym(nd->nd_mreq, 0, M_COPYALL, M_WAITOK);
		NFSLOCKCLSTATE();
		clp = nfscl_getclntsess(sessionid);
		if (clp != NULL) {
			tsep = nfsmnt_mdssession(clp->nfsc_nmp);
			nfsv4_seqsess_cacherep(slotid, tsep->nfsess_cbslots,
			    NFSERR_OK, &rep);
			NFSUNLOCKCLSTATE();
		} else {
			NFSUNLOCKCLSTATE();
			m_freem(rep);
		}
	}
}

/*
 * Generate the next cbident value. Basically just increment a static value
 * and then check that it isn't already in the list, if it has wrapped around.
 */
static u_int32_t
nfscl_nextcbident(void)
{
	struct nfsclclient *clp;
	int matched;
	static u_int32_t nextcbident = 0;
	static int haswrapped = 0;

	nextcbident++;
	if (nextcbident == 0)
		haswrapped = 1;
	if (haswrapped) {
		/*
		 * Search the clientid list for one already using this cbident.
		 */
		do {
			matched = 0;
			NFSLOCKCLSTATE();
			LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
				if (clp->nfsc_cbident == nextcbident) {
					matched = 1;
					break;
				}
			}
			NFSUNLOCKCLSTATE();
			if (matched == 1)
				nextcbident++;
		} while (matched);
	}
	return (nextcbident);
}

/*
 * Get the mount point related to a given cbident or session and busy it.
 */
static mount_t
nfscl_getmnt(int minorvers, uint8_t *sessionid, u_int32_t cbident,
    struct nfsclclient **clpp)
{
	struct nfsclclient *clp;
	mount_t mp;
	int error;
	struct nfsclsession *tsep;

	*clpp = NULL;
	NFSLOCKCLSTATE();
	LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
		tsep = nfsmnt_mdssession(clp->nfsc_nmp);
		if (minorvers == NFSV4_MINORVERSION) {
			if (clp->nfsc_cbident == cbident)
				break;
		} else if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
		    NFSX_V4SESSIONID))
			break;
	}
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (NULL);
	}
	mp = clp->nfsc_nmp->nm_mountp;
	vfs_ref(mp);
	NFSUNLOCKCLSTATE();
	error = vfs_busy(mp, 0);
	vfs_rel(mp);
	if (error != 0)
		return (NULL);
	*clpp = clp;
	return (mp);
}

/*
 * Get the clientid pointer related to a given cbident.
 */
static struct nfsclclient *
nfscl_getclnt(u_int32_t cbident)
{
	struct nfsclclient *clp;

	LIST_FOREACH(clp, &nfsclhead, nfsc_list)
		if (clp->nfsc_cbident == cbident)
			break;
	return (clp);
}

/*
 * Get the clientid pointer related to a given sessionid.
 */
static struct nfsclclient *
nfscl_getclntsess(uint8_t *sessionid)
{
	struct nfsclclient *clp;
	struct nfsclsession *tsep;

	LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
		tsep = nfsmnt_mdssession(clp->nfsc_nmp);
		if (!NFSBCMP(tsep->nfsess_sessionid, sessionid,
		    NFSX_V4SESSIONID))
			break;
	}
	return (clp);
}

/*
 * Search for a lock conflict locally on the client. A conflict occurs if
 * - not same owner and overlapping byte range and at least one of them is
 *   a write lock or this is an unlock.
 */
static int
nfscl_localconflict(struct nfsclclient *clp, u_int8_t *fhp, int fhlen,
    struct nfscllock *nlop, u_int8_t *own, struct nfscldeleg *dp,
    struct nfscllock **lopp)
{
	struct nfsclowner *owp;
	struct nfsclopen *op;
	int ret;

	if (dp != NULL) {
		ret = nfscl_checkconflict(&dp->nfsdl_lock, nlop, own, lopp);
		if (ret)
			return (ret);
	}
	LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
		LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			if (op->nfso_fhlen == fhlen &&
			    !NFSBCMP(op->nfso_fh, fhp, fhlen)) {
				ret = nfscl_checkconflict(&op->nfso_lock, nlop,
				    own, lopp);
				if (ret)
					return (ret);
			}
		}
	}
	return (0);
}

static int
nfscl_checkconflict(struct nfscllockownerhead *lhp, struct nfscllock *nlop,
    u_int8_t *own, struct nfscllock **lopp)
{
	struct nfscllockowner *lp;
	struct nfscllock *lop;

	LIST_FOREACH(lp, lhp, nfsl_list) {
		if (NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN)) {
			LIST_FOREACH(lop, &lp->nfsl_lock, nfslo_list) {
				if (lop->nfslo_first >= nlop->nfslo_end)
					break;
				if (lop->nfslo_end <= nlop->nfslo_first)
					continue;
				if (lop->nfslo_type == F_WRLCK ||
				    nlop->nfslo_type == F_WRLCK ||
				    nlop->nfslo_type == F_UNLCK) {
					if (lopp != NULL)
						*lopp = lop;
					return (NFSERR_DENIED);
				}
			}
		}
	}
	return (0);
}

/*
 * Check for a local conflicting lock.
 */
APPLESTATIC int
nfscl_lockt(vnode_t vp, struct nfsclclient *clp, u_int64_t off,
    u_int64_t len, struct flock *fl, NFSPROC_T *p, void *id, int flags)
{
	struct nfscllock *lop, nlck;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int error;

	nlck.nfslo_type = fl->l_type;
	nlck.nfslo_first = off;
	if (len == NFS64BITSSET) {
		nlck.nfslo_end = NFS64BITSSET;
	} else {
		nlck.nfslo_end = off + len;
		if (nlck.nfslo_end <= nlck.nfslo_first)
			return (NFSERR_INVAL);
	}
	np = VTONFS(vp);
	nfscl_filllockowner(id, own, flags);
	NFSLOCKCLSTATE();
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	error = nfscl_localconflict(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len,
	    &nlck, own, dp, &lop);
	if (error != 0) {
		fl->l_whence = SEEK_SET;
		fl->l_start = lop->nfslo_first;
		if (lop->nfslo_end == NFS64BITSSET)
			fl->l_len = 0;
		else
			fl->l_len = lop->nfslo_end - lop->nfslo_first;
		fl->l_pid = (pid_t)0;
		fl->l_type = lop->nfslo_type;
		error = -1;			/* no RPC required */
	} else if (dp != NULL && ((dp->nfsdl_flags & NFSCLDL_WRITE) ||
	    fl->l_type == F_RDLCK)) {
		/*
		 * The delegation ensures that there isn't a conflicting
		 * lock on the server, so return -1 to indicate an RPC
		 * isn't required.
		 */
		fl->l_type = F_UNLCK;
		error = -1;
	}
	NFSUNLOCKCLSTATE();
	return (error);
}

/*
 * Handle Recall of a delegation.
 * The clp must be exclusive locked when this is called.
 */
static int
nfscl_recalldeleg(struct nfsclclient *clp, struct nfsmount *nmp,
    struct nfscldeleg *dp, vnode_t vp, struct ucred *cred, NFSPROC_T *p,
    int called_from_renewthread)
{
	struct nfsclowner *owp, *lowp, *nowp;
	struct nfsclopen *op, *lop;
	struct nfscllockowner *lp;
	struct nfscllock *lckp;
	struct nfsnode *np;
	int error = 0, ret, gotvp = 0;

	if (vp == NULL) {
		/*
		 * First, get a vnode for the file. This is needed to do RPCs.
		 */
		ret = nfscl_ngetreopen(nmp->nm_mountp, dp->nfsdl_fh,
		    dp->nfsdl_fhlen, p, &np);
		if (ret) {
			/*
			 * File isn't open, so nothing to move over to the
			 * server.
			 */
			return (0);
		}
		vp = NFSTOV(np);
		gotvp = 1;
	} else {
		np = VTONFS(vp);
	}
	dp->nfsdl_flags &= ~NFSCLDL_MODTIMESET;

	/*
	 * Ok, if it's a write delegation, flush data to the server, so
	 * that close/open consistency is retained.
	 */
	ret = 0;
	NFSLOCKNODE(np);
	if ((dp->nfsdl_flags & NFSCLDL_WRITE) && (np->n_flag & NMODIFIED)) {
		np->n_flag |= NDELEGRECALL;
		NFSUNLOCKNODE(np);
		ret = ncl_flush(vp, MNT_WAIT, p, 1, called_from_renewthread);
		NFSLOCKNODE(np);
		np->n_flag &= ~NDELEGRECALL;
	}
	NFSINVALATTRCACHE(np);
	NFSUNLOCKNODE(np);
	if (ret == EIO && called_from_renewthread != 0) {
		/*
		 * If the flush failed with EIO for the renew thread,
		 * return now, so that the dirty buffer will be flushed
		 * later.
		 */
		if (gotvp != 0)
			vrele(vp);
		return (ret);
	}

	/*
	 * Now, for each openowner with opens issued locally, move them
	 * over to state against the server.
	 */
	LIST_FOREACH(lowp, &dp->nfsdl_owner, nfsow_list) {
		lop = LIST_FIRST(&lowp->nfsow_open);
		if (lop != NULL) {
			if (LIST_NEXT(lop, nfso_list) != NULL)
				panic("nfsdlg mult opens");
			/*
			 * Look for the same openowner against the server.
			 */
			LIST_FOREACH(owp, &clp->nfsc_owner, nfsow_list) {
				if (!NFSBCMP(lowp->nfsow_owner,
				    owp->nfsow_owner, NFSV4CL_LOCKNAMELEN)) {
					newnfs_copycred(&dp->nfsdl_cred, cred);
					ret = nfscl_moveopen(vp, clp, nmp, lop,
					    owp, dp, cred, p);
					if (ret == NFSERR_STALECLIENTID ||
					    ret == NFSERR_STALEDONTRECOVER ||
					    ret == NFSERR_BADSESSION) {
						if (gotvp)
							vrele(vp);
						return (ret);
					}
					if (ret) {
						nfscl_freeopen(lop, 1);
						if (!error)
							error = ret;
					}
					break;
				}
			}

			/*
			 * If no openowner found, create one and get an open
			 * for it.
			 */
			if (owp == NULL) {
				nowp = malloc(
				    sizeof (struct nfsclowner), M_NFSCLOWNER,
				    M_WAITOK);
				nfscl_newopen(clp, NULL, &owp, &nowp, &op, 
				    NULL, lowp->nfsow_owner, dp->nfsdl_fh,
				    dp->nfsdl_fhlen, NULL, NULL);
				newnfs_copycred(&dp->nfsdl_cred, cred);
				ret = nfscl_moveopen(vp, clp, nmp, lop,
				    owp, dp, cred, p);
				if (ret) {
					nfscl_freeopenowner(owp, 0);
					if (ret == NFSERR_STALECLIENTID ||
					    ret == NFSERR_STALEDONTRECOVER ||
					    ret == NFSERR_BADSESSION) {
						if (gotvp)
							vrele(vp);
						return (ret);
					}
					if (ret) {
						nfscl_freeopen(lop, 1);
						if (!error)
							error = ret;
					}
				}
			}
		}
	}

	/*
	 * Now, get byte range locks for any locks done locally.
	 */
	LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
		LIST_FOREACH(lckp, &lp->nfsl_lock, nfslo_list) {
			newnfs_copycred(&dp->nfsdl_cred, cred);
			ret = nfscl_relock(vp, clp, nmp, lp, lckp, cred, p);
			if (ret == NFSERR_STALESTATEID ||
			    ret == NFSERR_STALEDONTRECOVER ||
			    ret == NFSERR_STALECLIENTID ||
			    ret == NFSERR_BADSESSION) {
				if (gotvp)
					vrele(vp);
				return (ret);
			}
			if (ret && !error)
				error = ret;
		}
	}
	if (gotvp)
		vrele(vp);
	return (error);
}

/*
 * Move a locally issued open over to an owner on the state list.
 * SIDE EFFECT: If it needs to sleep (do an rpc), it unlocks clstate and
 * returns with it unlocked.
 */
static int
nfscl_moveopen(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
    struct nfsclopen *lop, struct nfsclowner *owp, struct nfscldeleg *dp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclopen *op, *nop;
	struct nfscldeleg *ndp;
	struct nfsnode *np;
	int error = 0, newone;

	/*
	 * First, look for an appropriate open, If found, just increment the
	 * opencnt in it.
	 */
	LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
		if ((op->nfso_mode & lop->nfso_mode) == lop->nfso_mode &&
		    op->nfso_fhlen == lop->nfso_fhlen &&
		    !NFSBCMP(op->nfso_fh, lop->nfso_fh, op->nfso_fhlen)) {
			op->nfso_opencnt += lop->nfso_opencnt;
			nfscl_freeopen(lop, 1);
			return (0);
		}
	}

	/* No appropriate open, so we have to do one against the server. */
	np = VTONFS(vp);
	nop = malloc(sizeof (struct nfsclopen) +
	    lop->nfso_fhlen - 1, M_NFSCLOPEN, M_WAITOK);
	newone = 0;
	nfscl_newopen(clp, NULL, &owp, NULL, &op, &nop, owp->nfsow_owner,
	    lop->nfso_fh, lop->nfso_fhlen, cred, &newone);
	ndp = dp;
	error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data, np->n_v4->n4_fhlen,
	    lop->nfso_fh, lop->nfso_fhlen, lop->nfso_mode, op,
	    NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, &ndp, 0, 0, cred, p);
	if (error) {
		if (newone)
			nfscl_freeopen(op, 0);
	} else {
		op->nfso_mode |= lop->nfso_mode;
		op->nfso_opencnt += lop->nfso_opencnt;
		nfscl_freeopen(lop, 1);
	}
	if (nop != NULL)
		free(nop, M_NFSCLOPEN);
	if (ndp != NULL) {
		/*
		 * What should I do with the returned delegation, since the
		 * delegation is being recalled? For now, just printf and
		 * through it away.
		 */
		printf("Moveopen returned deleg\n");
		free(ndp, M_NFSCLDELEG);
	}
	return (error);
}

/*
 * Recall all delegations on this client.
 */
static void
nfscl_totalrecall(struct nfsclclient *clp)
{
	struct nfscldeleg *dp;

	TAILQ_FOREACH(dp, &clp->nfsc_deleg, nfsdl_list) {
		if ((dp->nfsdl_flags & NFSCLDL_DELEGRET) == 0)
			dp->nfsdl_flags |= NFSCLDL_RECALL;
	}
}

/*
 * Relock byte ranges. Called for delegation recall and state expiry.
 */
static int
nfscl_relock(vnode_t vp, struct nfsclclient *clp, struct nfsmount *nmp,
    struct nfscllockowner *lp, struct nfscllock *lop, struct ucred *cred,
    NFSPROC_T *p)
{
	struct nfscllockowner *nlp;
	struct nfsfh *nfhp;
	u_int64_t off, len;
	int error, newone, donelocally;

	off = lop->nfslo_first;
	len = lop->nfslo_end - lop->nfslo_first;
	error = nfscl_getbytelock(vp, off, len, lop->nfslo_type, cred, p,
	    clp, 1, NULL, lp->nfsl_lockflags, lp->nfsl_owner,
	    lp->nfsl_openowner, &nlp, &newone, &donelocally);
	if (error || donelocally)
		return (error);
	nfhp = VTONFS(vp)->n_fhp;
	error = nfscl_trylock(nmp, vp, nfhp->nfh_fh,
	    nfhp->nfh_len, nlp, newone, 0, off,
	    len, lop->nfslo_type, cred, p);
	if (error)
		nfscl_freelockowner(nlp, 0);
	return (error);
}

/*
 * Called to re-open a file. Basically get a vnode for the file handle
 * and then call nfsrpc_openrpc() to do the rest.
 */
static int
nfsrpc_reopen(struct nfsmount *nmp, u_int8_t *fhp, int fhlen,
    u_int32_t mode, struct nfsclopen *op, struct nfscldeleg **dpp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsnode *np;
	vnode_t vp;
	int error;

	error = nfscl_ngetreopen(nmp->nm_mountp, fhp, fhlen, p, &np);
	if (error)
		return (error);
	vp = NFSTOV(np);
	if (np->n_v4 != NULL) {
		error = nfscl_tryopen(nmp, vp, np->n_v4->n4_data,
		    np->n_v4->n4_fhlen, fhp, fhlen, mode, op,
		    NFS4NODENAME(np->n_v4), np->n_v4->n4_namelen, dpp, 0, 0,
		    cred, p);
	} else {
		error = EINVAL;
	}
	vrele(vp);
	return (error);
}

/*
 * Try an open against the server. Just call nfsrpc_openrpc(), retrying while
 * NFSERR_DELAY. Also, try system credentials, if the passed in credentials
 * fail.
 */
static int
nfscl_tryopen(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
    u_int8_t *newfhp, int newfhlen, u_int32_t mode, struct nfsclopen *op,
    u_int8_t *name, int namelen, struct nfscldeleg **ndpp,
    int reclaim, u_int32_t delegtype, struct ucred *cred, NFSPROC_T *p)
{
	int error;

	do {
		error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp, newfhlen,
		    mode, op, name, namelen, ndpp, reclaim, delegtype, cred, p,
		    0, 0);
		if (error == NFSERR_DELAY)
			(void) nfs_catnap(PZERO, error, "nfstryop");
	} while (error == NFSERR_DELAY);
	if (error == EAUTH || error == EACCES) {
		/* Try again using system credentials */
		newnfs_setroot(cred);
		do {
		    error = nfsrpc_openrpc(nmp, vp, fhp, fhlen, newfhp,
			newfhlen, mode, op, name, namelen, ndpp, reclaim,
			delegtype, cred, p, 1, 0);
		    if (error == NFSERR_DELAY)
			(void) nfs_catnap(PZERO, error, "nfstryop");
		} while (error == NFSERR_DELAY);
	}
	return (error);
}

/*
 * Try a byte range lock. Just loop on nfsrpc_lock() while it returns
 * NFSERR_DELAY. Also, retry with system credentials, if the provided
 * cred don't work.
 */
static int
nfscl_trylock(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp,
    int fhlen, struct nfscllockowner *nlp, int newone, int reclaim,
    u_int64_t off, u_int64_t len, short type, struct ucred *cred, NFSPROC_T *p)
{
	struct nfsrv_descript nfsd, *nd = &nfsd;
	int error;

	do {
		error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp, newone,
		    reclaim, off, len, type, cred, p, 0);
		if (!error && nd->nd_repstat == NFSERR_DELAY)
			(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
			    "nfstrylck");
	} while (!error && nd->nd_repstat == NFSERR_DELAY);
	if (!error)
		error = nd->nd_repstat;
	if (error == EAUTH || error == EACCES) {
		/* Try again using root credentials */
		newnfs_setroot(cred);
		do {
			error = nfsrpc_lock(nd, nmp, vp, fhp, fhlen, nlp,
			    newone, reclaim, off, len, type, cred, p, 1);
			if (!error && nd->nd_repstat == NFSERR_DELAY)
				(void) nfs_catnap(PZERO, (int)nd->nd_repstat,
				    "nfstrylck");
		} while (!error && nd->nd_repstat == NFSERR_DELAY);
		if (!error)
			error = nd->nd_repstat;
	}
	return (error);
}

/*
 * Try a delegreturn against the server. Just call nfsrpc_delegreturn(),
 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
 * credentials fail.
 */
static int
nfscl_trydelegreturn(struct nfscldeleg *dp, struct ucred *cred,
    struct nfsmount *nmp, NFSPROC_T *p)
{
	int error;

	do {
		error = nfsrpc_delegreturn(dp, cred, nmp, p, 0);
		if (error == NFSERR_DELAY)
			(void) nfs_catnap(PZERO, error, "nfstrydp");
	} while (error == NFSERR_DELAY);
	if (error == EAUTH || error == EACCES) {
		/* Try again using system credentials */
		newnfs_setroot(cred);
		do {
			error = nfsrpc_delegreturn(dp, cred, nmp, p, 1);
			if (error == NFSERR_DELAY)
				(void) nfs_catnap(PZERO, error, "nfstrydp");
		} while (error == NFSERR_DELAY);
	}
	return (error);
}

/*
 * Try a close against the server. Just call nfsrpc_closerpc(),
 * retrying while NFSERR_DELAY. Also, try system credentials, if the passed in
 * credentials fail.
 */
APPLESTATIC int
nfscl_tryclose(struct nfsclopen *op, struct ucred *cred,
    struct nfsmount *nmp, NFSPROC_T *p)
{
	struct nfsrv_descript nfsd, *nd = &nfsd;
	int error;

	do {
		error = nfsrpc_closerpc(nd, nmp, op, cred, p, 0);
		if (error == NFSERR_DELAY)
			(void) nfs_catnap(PZERO, error, "nfstrycl");
	} while (error == NFSERR_DELAY);
	if (error == EAUTH || error == EACCES) {
		/* Try again using system credentials */
		newnfs_setroot(cred);
		do {
			error = nfsrpc_closerpc(nd, nmp, op, cred, p, 1);
			if (error == NFSERR_DELAY)
				(void) nfs_catnap(PZERO, error, "nfstrycl");
		} while (error == NFSERR_DELAY);
	}
	return (error);
}

/*
 * Decide if a delegation on a file permits close without flushing writes
 * to the server. This might be a big performance win in some environments.
 * (Not useful until the client does caching on local stable storage.)
 */
APPLESTATIC int
nfscl_mustflush(vnode_t vp)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	struct nfsmount *nmp;

	np = VTONFS(vp);
	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return (1);
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (1);
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL && (dp->nfsdl_flags &
	    (NFSCLDL_WRITE | NFSCLDL_RECALL | NFSCLDL_DELEGRET)) ==
	     NFSCLDL_WRITE &&
	    (dp->nfsdl_sizelimit >= np->n_size ||
	     !NFSHASSTRICT3530(nmp))) {
		NFSUNLOCKCLSTATE();
		return (0);
	}
	NFSUNLOCKCLSTATE();
	return (1);
}

/*
 * See if a (write) delegation exists for this file.
 */
APPLESTATIC int
nfscl_nodeleg(vnode_t vp, int writedeleg)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	struct nfsmount *nmp;

	np = VTONFS(vp);
	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return (1);
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (1);
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL &&
	    (dp->nfsdl_flags & (NFSCLDL_RECALL | NFSCLDL_DELEGRET)) == 0 &&
	    (writedeleg == 0 || (dp->nfsdl_flags & NFSCLDL_WRITE) ==
	     NFSCLDL_WRITE)) {
		NFSUNLOCKCLSTATE();
		return (0);
	}
	NFSUNLOCKCLSTATE();
	return (1);
}

/*
 * Look for an associated delegation that should be DelegReturned.
 */
APPLESTATIC int
nfscl_removedeleg(vnode_t vp, NFSPROC_T *p, nfsv4stateid_t *stp)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsclowner *owp;
	struct nfscllockowner *lp;
	struct nfsmount *nmp;
	struct ucred *cred;
	struct nfsnode *np;
	int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;

	nmp = VFSTONFS(vnode_mount(vp));
	np = VTONFS(vp);
	NFSLOCKCLSTATE();
	/*
	 * Loop around waiting for:
	 * - outstanding I/O operations on delegations to complete
	 * - for a delegation on vp that has state, lock the client and
	 *   do a recall
	 * - return delegation with no state
	 */
	while (1) {
		clp = nfscl_findcl(nmp);
		if (clp == NULL) {
			NFSUNLOCKCLSTATE();
			return (retcnt);
		}
		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len);
		if (dp != NULL) {
		    /*
		     * Wait for outstanding I/O ops to be done.
		     */
		    if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
			if (igotlock) {
			    nfsv4_unlock(&clp->nfsc_lock, 0);
			    igotlock = 0;
			}
			dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
			(void) nfsmsleep(&dp->nfsdl_rwlock,
			    NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
			continue;
		    }
		    needsrecall = 0;
		    LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
			if (!LIST_EMPTY(&owp->nfsow_open)) {
			    needsrecall = 1;
			    break;
			}
		    }
		    if (!needsrecall) {
			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
				needsrecall = 1;
				break;
			    }
			}
		    }
		    if (needsrecall && !triedrecall) {
			dp->nfsdl_flags |= NFSCLDL_DELEGRET;
			islept = 0;
			while (!igotlock) {
			    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
				&islept, NFSCLSTATEMUTEXPTR, NULL);
			    if (islept)
				break;
			}
			if (islept)
			    continue;
			NFSUNLOCKCLSTATE();
			cred = newnfs_getcred();
			newnfs_copycred(&dp->nfsdl_cred, cred);
			(void) nfscl_recalldeleg(clp, nmp, dp, vp, cred, p, 0);
			NFSFREECRED(cred);
			triedrecall = 1;
			NFSLOCKCLSTATE();
			nfsv4_unlock(&clp->nfsc_lock, 0);
			igotlock = 0;
			continue;
		    }
		    *stp = dp->nfsdl_stateid;
		    retcnt = 1;
		    nfscl_cleandeleg(dp);
		    nfscl_freedeleg(&clp->nfsc_deleg, dp);
		}
		if (igotlock)
		    nfsv4_unlock(&clp->nfsc_lock, 0);
		NFSUNLOCKCLSTATE();
		return (retcnt);
	}
}

/*
 * Look for associated delegation(s) that should be DelegReturned.
 */
APPLESTATIC int
nfscl_renamedeleg(vnode_t fvp, nfsv4stateid_t *fstp, int *gotfdp, vnode_t tvp,
    nfsv4stateid_t *tstp, int *gottdp, NFSPROC_T *p)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsclowner *owp;
	struct nfscllockowner *lp;
	struct nfsmount *nmp;
	struct ucred *cred;
	struct nfsnode *np;
	int igotlock = 0, triedrecall = 0, needsrecall, retcnt = 0, islept;

	nmp = VFSTONFS(vnode_mount(fvp));
	*gotfdp = 0;
	*gottdp = 0;
	NFSLOCKCLSTATE();
	/*
	 * Loop around waiting for:
	 * - outstanding I/O operations on delegations to complete
	 * - for a delegation on fvp that has state, lock the client and
	 *   do a recall
	 * - return delegation(s) with no state.
	 */
	while (1) {
		clp = nfscl_findcl(nmp);
		if (clp == NULL) {
			NFSUNLOCKCLSTATE();
			return (retcnt);
		}
		np = VTONFS(fvp);
		dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len);
		if (dp != NULL && *gotfdp == 0) {
		    /*
		     * Wait for outstanding I/O ops to be done.
		     */
		    if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
			if (igotlock) {
			    nfsv4_unlock(&clp->nfsc_lock, 0);
			    igotlock = 0;
			}
			dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
			(void) nfsmsleep(&dp->nfsdl_rwlock,
			    NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
			continue;
		    }
		    needsrecall = 0;
		    LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
			if (!LIST_EMPTY(&owp->nfsow_open)) {
			    needsrecall = 1;
			    break;
			}
		    }
		    if (!needsrecall) {
			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
				needsrecall = 1;
				break;
			    }
			}
		    }
		    if (needsrecall && !triedrecall) {
			dp->nfsdl_flags |= NFSCLDL_DELEGRET;
			islept = 0;
			while (!igotlock) {
			    igotlock = nfsv4_lock(&clp->nfsc_lock, 1,
				&islept, NFSCLSTATEMUTEXPTR, NULL);
			    if (islept)
				break;
			}
			if (islept)
			    continue;
			NFSUNLOCKCLSTATE();
			cred = newnfs_getcred();
			newnfs_copycred(&dp->nfsdl_cred, cred);
			(void) nfscl_recalldeleg(clp, nmp, dp, fvp, cred, p, 0);
			NFSFREECRED(cred);
			triedrecall = 1;
			NFSLOCKCLSTATE();
			nfsv4_unlock(&clp->nfsc_lock, 0);
			igotlock = 0;
			continue;
		    }
		    *fstp = dp->nfsdl_stateid;
		    retcnt++;
		    *gotfdp = 1;
		    nfscl_cleandeleg(dp);
		    nfscl_freedeleg(&clp->nfsc_deleg, dp);
		}
		if (igotlock) {
		    nfsv4_unlock(&clp->nfsc_lock, 0);
		    igotlock = 0;
		}
		if (tvp != NULL) {
		    np = VTONFS(tvp);
		    dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh,
			np->n_fhp->nfh_len);
		    if (dp != NULL && *gottdp == 0) {
			/*
			 * Wait for outstanding I/O ops to be done.
			 */
			if (dp->nfsdl_rwlock.nfslock_usecnt > 0) {
			    dp->nfsdl_rwlock.nfslock_lock |= NFSV4LOCK_WANTED;
			    (void) nfsmsleep(&dp->nfsdl_rwlock,
				NFSCLSTATEMUTEXPTR, PZERO, "nfscld", NULL);
			    continue;
			}
			LIST_FOREACH(owp, &dp->nfsdl_owner, nfsow_list) {
			    if (!LIST_EMPTY(&owp->nfsow_open)) {
				NFSUNLOCKCLSTATE();
				return (retcnt);
			    }
			}
			LIST_FOREACH(lp, &dp->nfsdl_lock, nfsl_list) {
			    if (!LIST_EMPTY(&lp->nfsl_lock)) {
				NFSUNLOCKCLSTATE();
				return (retcnt);
			    }
			}
			*tstp = dp->nfsdl_stateid;
			retcnt++;
			*gottdp = 1;
			nfscl_cleandeleg(dp);
			nfscl_freedeleg(&clp->nfsc_deleg, dp);
		    }
		}
		NFSUNLOCKCLSTATE();
		return (retcnt);
	}
}

/*
 * Get a reference on the clientid associated with the mount point.
 * Return 1 if success, 0 otherwise.
 */
APPLESTATIC int
nfscl_getref(struct nfsmount *nmp)
{
	struct nfsclclient *clp;

	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (0);
	}
	nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, NULL);
	NFSUNLOCKCLSTATE();
	return (1);
}

/*
 * Release a reference on a clientid acquired with the above call.
 */
APPLESTATIC void
nfscl_relref(struct nfsmount *nmp)
{
	struct nfsclclient *clp;

	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return;
	}
	nfsv4_relref(&clp->nfsc_lock);
	NFSUNLOCKCLSTATE();
}

/*
 * Save the size attribute in the delegation, since the nfsnode
 * is going away.
 */
APPLESTATIC void
nfscl_reclaimnode(vnode_t vp)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np = VTONFS(vp);
	struct nfsmount *nmp;

	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return;
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return;
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
		dp->nfsdl_size = np->n_size;
	NFSUNLOCKCLSTATE();
}

/*
 * Get the saved size attribute in the delegation, since it is a
 * newly allocated nfsnode.
 */
APPLESTATIC void
nfscl_newnode(vnode_t vp)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np = VTONFS(vp);
	struct nfsmount *nmp;

	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return;
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return;
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE))
		np->n_size = dp->nfsdl_size;
	NFSUNLOCKCLSTATE();
}

/*
 * If there is a valid write delegation for this file, set the modtime
 * to the local clock time.
 */
APPLESTATIC void
nfscl_delegmodtime(vnode_t vp)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np = VTONFS(vp);
	struct nfsmount *nmp;

	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return;
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return;
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL && (dp->nfsdl_flags & NFSCLDL_WRITE)) {
		nanotime(&dp->nfsdl_modtime);
		dp->nfsdl_flags |= NFSCLDL_MODTIMESET;
	}
	NFSUNLOCKCLSTATE();
}

/*
 * If there is a valid write delegation for this file with a modtime set,
 * put that modtime in mtime.
 */
APPLESTATIC void
nfscl_deleggetmodtime(vnode_t vp, struct timespec *mtime)
{
	struct nfsclclient *clp;
	struct nfscldeleg *dp;
	struct nfsnode *np = VTONFS(vp);
	struct nfsmount *nmp;

	nmp = VFSTONFS(vnode_mount(vp));
	if (!NFSHASNFSV4(nmp))
		return;
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(nmp);
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return;
	}
	dp = nfscl_finddeleg(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (dp != NULL &&
	    (dp->nfsdl_flags & (NFSCLDL_WRITE | NFSCLDL_MODTIMESET)) ==
	    (NFSCLDL_WRITE | NFSCLDL_MODTIMESET))
		*mtime = dp->nfsdl_modtime;
	NFSUNLOCKCLSTATE();
}

static int
nfscl_errmap(struct nfsrv_descript *nd, u_int32_t minorvers)
{
	short *defaulterrp, *errp;

	if (!nd->nd_repstat)
		return (0);
	if (nd->nd_procnum == NFSPROC_NOOP)
		return (txdr_unsigned(nd->nd_repstat & 0xffff));
	if (nd->nd_repstat == EBADRPC)
		return (txdr_unsigned(NFSERR_BADXDR));
	if (nd->nd_repstat == NFSERR_MINORVERMISMATCH ||
	    nd->nd_repstat == NFSERR_OPILLEGAL)
		return (txdr_unsigned(nd->nd_repstat));
	if (nd->nd_repstat >= NFSERR_BADIOMODE && nd->nd_repstat < 20000 &&
	    minorvers > NFSV4_MINORVERSION) {
		/* NFSv4.n error. */
		return (txdr_unsigned(nd->nd_repstat));
	}
	if (nd->nd_procnum < NFSV4OP_CBNOPS)
		errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
	else
		return (txdr_unsigned(nd->nd_repstat));
	while (*++errp)
		if (*errp == (short)nd->nd_repstat)
			return (txdr_unsigned(nd->nd_repstat));
	return (txdr_unsigned(*defaulterrp));
}

/*
 * Called to find/add a layout to a client.
 * This function returns the layout with a refcnt (shared lock) upon
 * success (returns 0) or with no lock/refcnt on the layout when an
 * error is returned.
 * If a layout is passed in via lypp, it is locked (exclusively locked).
 */
APPLESTATIC int
nfscl_layout(struct nfsmount *nmp, vnode_t vp, u_int8_t *fhp, int fhlen,
    nfsv4stateid_t *stateidp, int layouttype, int retonclose,
    struct nfsclflayouthead *fhlp, struct nfscllayout **lypp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclclient *clp;
	struct nfscllayout *lyp, *tlyp;
	struct nfsclflayout *flp;
	struct nfsnode *np = VTONFS(vp);
	mount_t mp;
	int layout_passed_in;

	mp = nmp->nm_mountp;
	layout_passed_in = 1;
	tlyp = NULL;
	lyp = *lypp;
	if (lyp == NULL) {
		layout_passed_in = 0;
		tlyp = malloc(sizeof(*tlyp) + fhlen - 1, M_NFSLAYOUT,
		    M_WAITOK | M_ZERO);
	}

	NFSLOCKCLSTATE();
	clp = nmp->nm_clp;
	if (clp == NULL) {
		if (layout_passed_in != 0)
			nfsv4_unlock(&lyp->nfsly_lock, 0);
		NFSUNLOCKCLSTATE();
		if (tlyp != NULL)
			free(tlyp, M_NFSLAYOUT);
		return (EPERM);
	}
	if (lyp == NULL) {
		/*
		 * Although no lyp was passed in, another thread might have
		 * allocated one. If one is found, just increment it's ref
		 * count and return it.
		 */
		lyp = nfscl_findlayout(clp, fhp, fhlen);
		if (lyp == NULL) {
			lyp = tlyp;
			tlyp = NULL;
			lyp->nfsly_stateid.seqid = stateidp->seqid;
			lyp->nfsly_stateid.other[0] = stateidp->other[0];
			lyp->nfsly_stateid.other[1] = stateidp->other[1];
			lyp->nfsly_stateid.other[2] = stateidp->other[2];
			lyp->nfsly_lastbyte = 0;
			LIST_INIT(&lyp->nfsly_flayread);
			LIST_INIT(&lyp->nfsly_flayrw);
			LIST_INIT(&lyp->nfsly_recall);
			lyp->nfsly_filesid[0] = np->n_vattr.na_filesid[0];
			lyp->nfsly_filesid[1] = np->n_vattr.na_filesid[1];
			lyp->nfsly_clp = clp;
			if (layouttype == NFSLAYOUT_FLEXFILE)
				lyp->nfsly_flags = NFSLY_FLEXFILE;
			else
				lyp->nfsly_flags = NFSLY_FILES;
			if (retonclose != 0)
				lyp->nfsly_flags |= NFSLY_RETONCLOSE;
			lyp->nfsly_fhlen = fhlen;
			NFSBCOPY(fhp, lyp->nfsly_fh, fhlen);
			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
			LIST_INSERT_HEAD(NFSCLLAYOUTHASH(clp, fhp, fhlen), lyp,
			    nfsly_hash);
			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
			nfscl_layoutcnt++;
		} else {
			if (retonclose != 0)
				lyp->nfsly_flags |= NFSLY_RETONCLOSE;
			TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
		}
		nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
		if (NFSCL_FORCEDISM(mp)) {
			NFSUNLOCKCLSTATE();
			if (tlyp != NULL)
				free(tlyp, M_NFSLAYOUT);
			return (EPERM);
		}
		*lypp = lyp;
	} else
		lyp->nfsly_stateid.seqid = stateidp->seqid;

	/* Merge the new list of File Layouts into the list. */
	flp = LIST_FIRST(fhlp);
	if (flp != NULL) {
		if (flp->nfsfl_iomode == NFSLAYOUTIOMODE_READ)
			nfscl_mergeflayouts(&lyp->nfsly_flayread, fhlp);
		else
			nfscl_mergeflayouts(&lyp->nfsly_flayrw, fhlp);
	}
	if (layout_passed_in != 0)
		nfsv4_unlock(&lyp->nfsly_lock, 1);
	NFSUNLOCKCLSTATE();
	if (tlyp != NULL)
		free(tlyp, M_NFSLAYOUT);
	return (0);
}

/*
 * Search for a layout by MDS file handle.
 * If one is found, it is returned with a refcnt (shared lock) iff
 * retflpp returned non-NULL and locked (exclusive locked) iff retflpp is
 * returned NULL.
 */
struct nfscllayout *
nfscl_getlayout(struct nfsclclient *clp, uint8_t *fhp, int fhlen,
    uint64_t off, struct nfsclflayout **retflpp, int *recalledp)
{
	struct nfscllayout *lyp;
	mount_t mp;
	int error, igotlock;

	mp = clp->nfsc_nmp->nm_mountp;
	*recalledp = 0;
	*retflpp = NULL;
	NFSLOCKCLSTATE();
	lyp = nfscl_findlayout(clp, fhp, fhlen);
	if (lyp != NULL) {
		if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
			TAILQ_REMOVE(&clp->nfsc_layout, lyp, nfsly_list);
			TAILQ_INSERT_HEAD(&clp->nfsc_layout, lyp, nfsly_list);
			lyp->nfsly_timestamp = NFSD_MONOSEC + 120;
			error = nfscl_findlayoutforio(lyp, off,
			    NFSV4OPEN_ACCESSREAD, retflpp);
			if (error == 0)
				nfsv4_getref(&lyp->nfsly_lock, NULL,
				    NFSCLSTATEMUTEXPTR, mp);
			else {
				do {
					igotlock = nfsv4_lock(&lyp->nfsly_lock,
					    1, NULL, NFSCLSTATEMUTEXPTR, mp);
				} while (igotlock == 0 && !NFSCL_FORCEDISM(mp));
				*retflpp = NULL;
			}
			if (NFSCL_FORCEDISM(mp)) {
				lyp = NULL;
				*recalledp = 1;
			}
		} else {
			lyp = NULL;
			*recalledp = 1;
		}
	}
	NFSUNLOCKCLSTATE();
	return (lyp);
}

/*
 * Search for a layout by MDS file handle. If one is found, mark in to be
 * recalled, if it already marked "return on close".
 */
static void
nfscl_retoncloselayout(vnode_t vp, struct nfsclclient *clp, uint8_t *fhp,
    int fhlen, struct nfsclrecalllayout **recallpp)
{
	struct nfscllayout *lyp;
	uint32_t iomode;

	if (vp->v_type != VREG || !NFSHASPNFS(VFSTONFS(vnode_mount(vp))) ||
	    nfscl_enablecallb == 0 || nfs_numnfscbd == 0 ||
	    (VTONFS(vp)->n_flag & NNOLAYOUT) != 0)
		return;
	lyp = nfscl_findlayout(clp, fhp, fhlen);
	if (lyp != NULL && (lyp->nfsly_flags & (NFSLY_RETONCLOSE |
	    NFSLY_RECALL)) == NFSLY_RETONCLOSE) {
		iomode = 0;
		if (!LIST_EMPTY(&lyp->nfsly_flayread))
			iomode |= NFSLAYOUTIOMODE_READ;
		if (!LIST_EMPTY(&lyp->nfsly_flayrw))
			iomode |= NFSLAYOUTIOMODE_RW;
		(void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
		    0, UINT64_MAX, lyp->nfsly_stateid.seqid, 0, 0, NULL,
		    *recallpp);
		NFSCL_DEBUG(4, "retoncls recall iomode=%d\n", iomode);
		*recallpp = NULL;
	}
}

/*
 * Mark the layout to be recalled and with an error.
 * Also, disable the dsp from further use.
 */
void
nfscl_dserr(uint32_t op, uint32_t stat, struct nfscldevinfo *dp,
    struct nfscllayout *lyp, struct nfsclds *dsp)
{
	struct nfsclrecalllayout *recallp;
	uint32_t iomode;

	printf("DS being disabled, error=%d\n", stat);
	/* Set up the return of the layout. */
	recallp = malloc(sizeof(*recallp), M_NFSLAYRECALL, M_WAITOK);
	iomode = 0;
	NFSLOCKCLSTATE();
	if ((lyp->nfsly_flags & NFSLY_RECALL) == 0) {
		if (!LIST_EMPTY(&lyp->nfsly_flayread))
			iomode |= NFSLAYOUTIOMODE_READ;
		if (!LIST_EMPTY(&lyp->nfsly_flayrw))
			iomode |= NFSLAYOUTIOMODE_RW;
		(void)nfscl_layoutrecall(NFSLAYOUTRETURN_FILE, lyp, iomode,
		    0, UINT64_MAX, lyp->nfsly_stateid.seqid, stat, op,
		    dp->nfsdi_deviceid, recallp);
		NFSUNLOCKCLSTATE();
		NFSCL_DEBUG(4, "nfscl_dserr recall iomode=%d\n", iomode);
	} else {
		NFSUNLOCKCLSTATE();
		free(recallp, M_NFSLAYRECALL);
	}

	/* And shut the TCP connection down. */
	nfscl_cancelreqs(dsp);
}

/*
 * Cancel all RPCs for this "dsp" by closing the connection.
 * Also, mark the session as defunct.
 * If NFSCLDS_SAMECONN is set, the connection is shared with other DSs and
 * cannot be shut down.
 */
APPLESTATIC void
nfscl_cancelreqs(struct nfsclds *dsp)
{
	struct __rpc_client *cl;
	static int non_event;

	NFSLOCKDS(dsp);
	if ((dsp->nfsclds_flags & (NFSCLDS_CLOSED | NFSCLDS_SAMECONN)) == 0 &&
	    dsp->nfsclds_sockp != NULL &&
	    dsp->nfsclds_sockp->nr_client != NULL) {
		dsp->nfsclds_flags |= NFSCLDS_CLOSED;
		cl = dsp->nfsclds_sockp->nr_client;
		dsp->nfsclds_sess.nfsess_defunct = 1;
		NFSUNLOCKDS(dsp);
		CLNT_CLOSE(cl);
		/*
		 * This 1sec sleep is done to reduce the number of reconnect
		 * attempts made on the DS while it has failed.
		 */
		tsleep(&non_event, PVFS, "ndscls", hz);
		return;
	}
	NFSUNLOCKDS(dsp);
}

/*
 * Dereference a layout.
 */
void
nfscl_rellayout(struct nfscllayout *lyp, int exclocked)
{

	NFSLOCKCLSTATE();
	if (exclocked != 0)
		nfsv4_unlock(&lyp->nfsly_lock, 0);
	else
		nfsv4_relref(&lyp->nfsly_lock);
	NFSUNLOCKCLSTATE();
}

/*
 * Search for a devinfo by deviceid. If one is found, return it after
 * acquiring a reference count on it.
 */
struct nfscldevinfo *
nfscl_getdevinfo(struct nfsclclient *clp, uint8_t *deviceid,
    struct nfscldevinfo *dip)
{

	NFSLOCKCLSTATE();
	if (dip == NULL)
		dip = nfscl_finddevinfo(clp, deviceid);
	if (dip != NULL)
		dip->nfsdi_refcnt++;
	NFSUNLOCKCLSTATE();
	return (dip);
}

/*
 * Dereference a devinfo structure.
 */
static void
nfscl_reldevinfo_locked(struct nfscldevinfo *dip)
{

	dip->nfsdi_refcnt--;
	if (dip->nfsdi_refcnt == 0)
		wakeup(&dip->nfsdi_refcnt);
}

/*
 * Dereference a devinfo structure.
 */
void
nfscl_reldevinfo(struct nfscldevinfo *dip)
{

	NFSLOCKCLSTATE();
	nfscl_reldevinfo_locked(dip);
	NFSUNLOCKCLSTATE();
}

/*
 * Find a layout for this file handle. Return NULL upon failure.
 */
static struct nfscllayout *
nfscl_findlayout(struct nfsclclient *clp, u_int8_t *fhp, int fhlen)
{
	struct nfscllayout *lyp;

	LIST_FOREACH(lyp, NFSCLLAYOUTHASH(clp, fhp, fhlen), nfsly_hash)
		if (lyp->nfsly_fhlen == fhlen &&
		    !NFSBCMP(lyp->nfsly_fh, fhp, fhlen))
			break;
	return (lyp);
}

/*
 * Find a devinfo for this deviceid. Return NULL upon failure.
 */
static struct nfscldevinfo *
nfscl_finddevinfo(struct nfsclclient *clp, uint8_t *deviceid)
{
	struct nfscldevinfo *dip;

	LIST_FOREACH(dip, &clp->nfsc_devinfo, nfsdi_list)
		if (NFSBCMP(dip->nfsdi_deviceid, deviceid, NFSX_V4DEVICEID)
		    == 0)
			break;
	return (dip);
}

/*
 * Merge the new file layout list into the main one, maintaining it in
 * increasing offset order.
 */
static void
nfscl_mergeflayouts(struct nfsclflayouthead *fhlp,
    struct nfsclflayouthead *newfhlp)
{
	struct nfsclflayout *flp, *nflp, *prevflp, *tflp;

	flp = LIST_FIRST(fhlp);
	prevflp = NULL;
	LIST_FOREACH_SAFE(nflp, newfhlp, nfsfl_list, tflp) {
		while (flp != NULL && flp->nfsfl_off < nflp->nfsfl_off) {
			prevflp = flp;
			flp = LIST_NEXT(flp, nfsfl_list);
		}
		if (prevflp == NULL)
			LIST_INSERT_HEAD(fhlp, nflp, nfsfl_list);
		else
			LIST_INSERT_AFTER(prevflp, nflp, nfsfl_list);
		prevflp = nflp;
	}
}

/*
 * Add this nfscldevinfo to the client, if it doesn't already exist.
 * This function consumes the structure pointed at by dip, if not NULL.
 */
APPLESTATIC int
nfscl_adddevinfo(struct nfsmount *nmp, struct nfscldevinfo *dip, int ind,
    struct nfsclflayout *flp)
{
	struct nfsclclient *clp;
	struct nfscldevinfo *tdip;
	uint8_t *dev;

	NFSLOCKCLSTATE();
	clp = nmp->nm_clp;
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		if (dip != NULL)
			free(dip, M_NFSDEVINFO);
		return (ENODEV);
	}
	if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
		dev = flp->nfsfl_dev;
	else
		dev = flp->nfsfl_ffm[ind].dev;
	tdip = nfscl_finddevinfo(clp, dev);
	if (tdip != NULL) {
		tdip->nfsdi_layoutrefs++;
		if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
			flp->nfsfl_devp = tdip;
		else
			flp->nfsfl_ffm[ind].devp = tdip;
		nfscl_reldevinfo_locked(tdip);
		NFSUNLOCKCLSTATE();
		if (dip != NULL)
			free(dip, M_NFSDEVINFO);
		return (0);
	}
	if (dip != NULL) {
		LIST_INSERT_HEAD(&clp->nfsc_devinfo, dip, nfsdi_list);
		dip->nfsdi_layoutrefs = 1;
		if ((flp->nfsfl_flags & NFSFL_FILE) != 0)
			flp->nfsfl_devp = dip;
		else
			flp->nfsfl_ffm[ind].devp = dip;
	}
	NFSUNLOCKCLSTATE();
	if (dip == NULL)
		return (ENODEV);
	return (0);
}

/*
 * Free up a layout structure and associated file layout structure(s).
 */
APPLESTATIC void
nfscl_freelayout(struct nfscllayout *layp)
{
	struct nfsclflayout *flp, *nflp;
	struct nfsclrecalllayout *rp, *nrp;

	LIST_FOREACH_SAFE(flp, &layp->nfsly_flayread, nfsfl_list, nflp) {
		LIST_REMOVE(flp, nfsfl_list);
		nfscl_freeflayout(flp);
	}
	LIST_FOREACH_SAFE(flp, &layp->nfsly_flayrw, nfsfl_list, nflp) {
		LIST_REMOVE(flp, nfsfl_list);
		nfscl_freeflayout(flp);
	}
	LIST_FOREACH_SAFE(rp, &layp->nfsly_recall, nfsrecly_list, nrp) {
		LIST_REMOVE(rp, nfsrecly_list);
		free(rp, M_NFSLAYRECALL);
	}
	nfscl_layoutcnt--;
	free(layp, M_NFSLAYOUT);
}

/*
 * Free up a file layout structure.
 */
APPLESTATIC void
nfscl_freeflayout(struct nfsclflayout *flp)
{
	int i, j;

	if ((flp->nfsfl_flags & NFSFL_FILE) != 0) {
		for (i = 0; i < flp->nfsfl_fhcnt; i++)
			free(flp->nfsfl_fh[i], M_NFSFH);
		if (flp->nfsfl_devp != NULL)
			flp->nfsfl_devp->nfsdi_layoutrefs--;
	}
	if ((flp->nfsfl_flags & NFSFL_FLEXFILE) != 0)
		for (i = 0; i < flp->nfsfl_mirrorcnt; i++) {
			for (j = 0; j < flp->nfsfl_ffm[i].fhcnt; j++)
				free(flp->nfsfl_ffm[i].fh[j], M_NFSFH);
			if (flp->nfsfl_ffm[i].devp != NULL)	
				flp->nfsfl_ffm[i].devp->nfsdi_layoutrefs--;	
		}
	free(flp, M_NFSFLAYOUT);
}

/*
 * Free up a file layout devinfo structure.
 */
APPLESTATIC void
nfscl_freedevinfo(struct nfscldevinfo *dip)
{

	free(dip, M_NFSDEVINFO);
}

/*
 * Mark any layouts that match as recalled.
 */
static int
nfscl_layoutrecall(int recalltype, struct nfscllayout *lyp, uint32_t iomode,
    uint64_t off, uint64_t len, uint32_t stateseqid, uint32_t stat, uint32_t op,
    char *devid, struct nfsclrecalllayout *recallp)
{
	struct nfsclrecalllayout *rp, *orp;

	recallp->nfsrecly_recalltype = recalltype;
	recallp->nfsrecly_iomode = iomode;
	recallp->nfsrecly_stateseqid = stateseqid;
	recallp->nfsrecly_off = off;
	recallp->nfsrecly_len = len;
	recallp->nfsrecly_stat = stat;
	recallp->nfsrecly_op = op;
	if (devid != NULL)
		NFSBCOPY(devid, recallp->nfsrecly_devid, NFSX_V4DEVICEID);
	/*
	 * Order the list as file returns first, followed by fsid and any
	 * returns, both in increasing stateseqid order.
	 * Note that the seqids wrap around, so 1 is after 0xffffffff.
	 * (I'm not sure this is correct because I find RFC5661 confusing
	 *  on this, but hopefully it will work ok.)
	 */
	orp = NULL;
	LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
		orp = rp;
		if ((recalltype == NFSLAYOUTRETURN_FILE &&
		     (rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE ||
		      nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) ||
		    (recalltype != NFSLAYOUTRETURN_FILE &&
		     rp->nfsrecly_recalltype != NFSLAYOUTRETURN_FILE &&
		     nfscl_seq(stateseqid, rp->nfsrecly_stateseqid) != 0)) {
			LIST_INSERT_BEFORE(rp, recallp, nfsrecly_list);
			break;
		}

		/*
		 * Put any error return on all the file returns that will
		 * preceed this one.
		 */
		if (rp->nfsrecly_recalltype == NFSLAYOUTRETURN_FILE &&
		   stat != 0 && rp->nfsrecly_stat == 0) {
			rp->nfsrecly_stat = stat;
			rp->nfsrecly_op = op;
			if (devid != NULL)
				NFSBCOPY(devid, rp->nfsrecly_devid,
				    NFSX_V4DEVICEID);
		}
	}
	if (rp == NULL) {
		if (orp == NULL)
			LIST_INSERT_HEAD(&lyp->nfsly_recall, recallp,
			    nfsrecly_list);
		else
			LIST_INSERT_AFTER(orp, recallp, nfsrecly_list);
	}
	lyp->nfsly_flags |= NFSLY_RECALL;
	wakeup(lyp->nfsly_clp);
	return (0);
}

/*
 * Compare the two seqids for ordering. The trick is that the seqids can
 * wrap around from 0xffffffff->0, so check for the cases where one
 * has wrapped around.
 * Return 1 if seqid1 comes before seqid2, 0 otherwise.
 */
static int
nfscl_seq(uint32_t seqid1, uint32_t seqid2)
{

	if (seqid2 > seqid1 && (seqid2 - seqid1) >= 0x7fffffff)
		/* seqid2 has wrapped around. */
		return (0);
	if (seqid1 > seqid2 && (seqid1 - seqid2) >= 0x7fffffff)
		/* seqid1 has wrapped around. */
		return (1);
	if (seqid1 <= seqid2)
		return (1);
	return (0);
}

/*
 * Do a layout return for each of the recalls.
 */
static void
nfscl_layoutreturn(struct nfsmount *nmp, struct nfscllayout *lyp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclrecalllayout *rp;
	nfsv4stateid_t stateid;
	int layouttype;

	NFSBCOPY(lyp->nfsly_stateid.other, stateid.other, NFSX_STATEIDOTHER);
	stateid.seqid = lyp->nfsly_stateid.seqid;
	if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
		layouttype = NFSLAYOUT_NFSV4_1_FILES;
	else
		layouttype = NFSLAYOUT_FLEXFILE;
	LIST_FOREACH(rp, &lyp->nfsly_recall, nfsrecly_list) {
		(void)nfsrpc_layoutreturn(nmp, lyp->nfsly_fh,
		    lyp->nfsly_fhlen, 0, layouttype,
		    rp->nfsrecly_iomode, rp->nfsrecly_recalltype,
		    rp->nfsrecly_off, rp->nfsrecly_len,
		    &stateid, cred, p, rp->nfsrecly_stat, rp->nfsrecly_op,
		    rp->nfsrecly_devid);
	}
}

/*
 * Do the layout commit for a file layout.
 */
static void
nfscl_dolayoutcommit(struct nfsmount *nmp, struct nfscllayout *lyp,
    struct ucred *cred, NFSPROC_T *p)
{
	struct nfsclflayout *flp;
	uint64_t len;
	int error, layouttype;

	if ((lyp->nfsly_flags & NFSLY_FILES) != 0)
		layouttype = NFSLAYOUT_NFSV4_1_FILES;
	else
		layouttype = NFSLAYOUT_FLEXFILE;
	LIST_FOREACH(flp, &lyp->nfsly_flayrw, nfsfl_list) {
		if (layouttype == NFSLAYOUT_FLEXFILE &&
		    (flp->nfsfl_fflags & NFSFLEXFLAG_NO_LAYOUTCOMMIT) != 0) {
			NFSCL_DEBUG(4, "Flex file: no layoutcommit\n");
			/* If not supported, don't bother doing it. */
			NFSLOCKMNT(nmp);
			nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
			NFSUNLOCKMNT(nmp);
			break;
		} else if (flp->nfsfl_off <= lyp->nfsly_lastbyte) {
			len = flp->nfsfl_end - flp->nfsfl_off;
			error = nfsrpc_layoutcommit(nmp, lyp->nfsly_fh,
			    lyp->nfsly_fhlen, 0, flp->nfsfl_off, len,
			    lyp->nfsly_lastbyte, &lyp->nfsly_stateid,
			    layouttype, cred, p, NULL);
			NFSCL_DEBUG(4, "layoutcommit err=%d\n", error);
			if (error == NFSERR_NOTSUPP) {
				/* If not supported, don't bother doing it. */
				NFSLOCKMNT(nmp);
				nmp->nm_state |= NFSSTA_NOLAYOUTCOMMIT;
				NFSUNLOCKMNT(nmp);
				break;
			}
		}
	}
}

/*
 * Commit all layouts for a file (vnode).
 */
int
nfscl_layoutcommit(vnode_t vp, NFSPROC_T *p)
{
	struct nfsclclient *clp;
	struct nfscllayout *lyp;
	struct nfsnode *np = VTONFS(vp);
	mount_t mp;
	struct nfsmount *nmp;

	mp = vnode_mount(vp);
	nmp = VFSTONFS(mp);
	if (NFSHASNOLAYOUTCOMMIT(nmp))
		return (0);
	NFSLOCKCLSTATE();
	clp = nmp->nm_clp;
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (EPERM);
	}
	lyp = nfscl_findlayout(clp, np->n_fhp->nfh_fh, np->n_fhp->nfh_len);
	if (lyp == NULL) {
		NFSUNLOCKCLSTATE();
		return (EPERM);
	}
	nfsv4_getref(&lyp->nfsly_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
	if (NFSCL_FORCEDISM(mp)) {
		NFSUNLOCKCLSTATE();
		return (EPERM);
	}
tryagain:
	if ((lyp->nfsly_flags & NFSLY_WRITTEN) != 0) {
		lyp->nfsly_flags &= ~NFSLY_WRITTEN;
		NFSUNLOCKCLSTATE();
		NFSCL_DEBUG(4, "do layoutcommit2\n");
		nfscl_dolayoutcommit(clp->nfsc_nmp, lyp, NFSPROCCRED(p), p);
		NFSLOCKCLSTATE();
		goto tryagain;
	}
	nfsv4_relref(&lyp->nfsly_lock);
	NFSUNLOCKCLSTATE();
	return (0);
}