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

static int nfscl_delegcnt = 0;
static int nfscl_getopen(struct nfsclownerhead *, u_int8_t *, int, u_int8_t *,
    NFSPROC_T *, u_int32_t, struct nfsclowner **, 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(u_int32_t);
static struct nfscldeleg *nfscl_finddeleg(struct nfsclclient *, u_int8_t *,
    int);
static int nfscl_checkconflict(struct nfscllockownerhead *, struct nfscllock *,
    u_int8_t *, struct nfscllock **);
static void nfscl_freelockowner(struct nfscllockowner *, int);
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, 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 *);
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 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.
	 */
	MALLOC(nowp, struct nfsclowner *, sizeof (struct nfsclowner),
	    M_NFSCLOWNER, M_WAITOK);
	if (nfhp != NULL)
	    MALLOC(nop, struct nfsclopen *, sizeof (struct nfsclopen) +
		fhlen - 1, M_NFSCLOPEN, M_WAITOK);
	ret = nfscl_getcl(vp, cred, p, &clp);
	if (ret != 0) {
		FREE((caddr_t)nowp, M_NFSCLOWNER);
		if (nop != NULL)
			FREE((caddr_t)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".
	 */
	nfscl_filllockowner(p->td_proc, own, F_POSIX);
	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)
		ohp = &dp->nfsdl_owner;
	else
		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,
	    newonep);

	/*
	 * Serialize modifications to the open owner for multiple threads
	 * within the same process using a read/write sleep lock.
	 */
	if (lockit)
		nfscl_lockexcl(&owp->nfsow_rwlock, NFSCLSTATEMUTEXPTR);
	NFSUNLOCKCLSTATE();
	if (nowp != NULL)
		FREE((caddr_t)nowp, M_NFSCLOWNER);
	if (nop != NULL)
		FREE((caddr_t)nop, M_NFSCLOPEN);
	if (owpp != NULL)
		*owpp = owp;
	if (opp != NULL)
		*opp = op;
	if (retp != NULL) {
		if (nfhp != NULL && dp != NULL && nop == NULL)
			/* new local open on delegation */
			*retp = NFSCLOPEN_SETCRED;
		else
			*retp = NFSCLOPEN_OK;
	}

	/*
	 * Now, check the mode on the open and return the appropriate
	 * value.
	 */
	if (op != NULL && (amode & ~(op->nfso_mode))) {
		op->nfso_mode |= amode;
		if (retp != NULL && dp == NULL)
			*retp = NFSCLOPEN_DOOPEN;
	}
	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,
    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) {
			newnfsstats.cllocalopenowners++;
			LIST_INSERT_HEAD(&dp->nfsdl_owner, nowp, nfsow_list);
		} else {
			newnfsstats.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;
			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;
				newnfsstats.cllocalopens++;
			} else {
				newnfsstats.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((caddr_t)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;
		newnfsstats.cldelegates++;
		nfscl_delegcnt++;
	} else {
		/*
		 * Delegation already exists, what do we do if a new one??
		 */
		if (dp != NULL) {
			printf("Deleg already exists!\n");
			FREE((caddr_t)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,
    struct ucred *cred, NFSPROC_T *p, nfsv4stateid_t *stateidp,
    void **lckpp)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;
	struct nfsclopen *op = NULL;
	struct nfscllockowner *lp;
	struct nfscldeleg *dp;
	struct nfsnode *np;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];
	int error, done;

	*lckpp = NULL;
	/*
	 * Initially, just set the special stateid of all zeros.
	 */
	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);
	NFSLOCKCLSTATE();
	clp = nfscl_findcl(VFSTONFS(vnode_mount(vp)));
	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.
		 */
		nfscl_filllockowner(p->td_proc, own, F_POSIX);
		error = nfscl_getopen(&clp->nfsc_owner, nfhp, fhlen, NULL, p,
		    mode, NULL, &op);
		if (error == 0) {
			/* now look for a lockowner */
			LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
				if (!NFSBCMP(lp->nfsl_owner, own,
				    NFSV4CL_LOCKNAMELEN)) {
					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. */
		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) &&
				    (mode & op->nfso_mode) == mode) {
					done = 1;
					break;
				}
			}
			if (!done)
				owp = LIST_NEXT(owp, nfsow_list);
		}
		if (!done) {
			NFSUNLOCKCLSTATE();
			return (ENOENT);
		}
		/* for read aheads or write behinds, use the open cred */
		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);
}

/*
 * Get an existing open. Search up the parentage tree for a match and
 * return with the first one found.
 */
static int
nfscl_getopen(struct nfsclownerhead *ohp, u_int8_t *nfhp, int fhlen,
    u_int8_t *rown, NFSPROC_T *p, u_int32_t mode, struct nfsclowner **owpp,
    struct nfsclopen **opp)
{
	struct nfsclowner *owp = NULL;
	struct nfsclopen *op;
	NFSPROC_T *nproc;
	u_int8_t own[NFSV4CL_LOCKNAMELEN], *ownp;

	nproc = p;
	op = NULL;
	while (op == NULL && (nproc != NULL || rown != NULL)) {
		if (nproc != NULL) {
			nfscl_filllockowner(nproc->td_proc, own, F_POSIX);
			ownp = own;
		} else {
			ownp = rown;
		}
		/* Search the client list */
		LIST_FOREACH(owp, ohp, nfsow_list) {
			if (!NFSBCMP(owp->nfsow_owner, ownp,
			    NFSV4CL_LOCKNAMELEN))
				break;
		}
		if (owp != NULL) {
			/* and look for the correct open */
			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
				if (op->nfso_fhlen == fhlen &&
				    !NFSBCMP(op->nfso_fh, nfhp, fhlen)
				    && (op->nfso_mode & mode) == mode) {
					break;
				}
			}
		}
		if (rown != NULL)
			break;
		if (op == NULL)
			nproc = nfscl_getparent(nproc);
	}
	if (op == NULL) {
		return (EBADF);
	}
	if (owpp)
		*owpp = owp;
	*opp = op;
	return (0);
}

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

	if (owp == NULL)
		return;
	NFSLOCKCLSTATE();
	if (!unlocked)
		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 nfsclopen *op, int error, int candelete)
{
	struct nfsclclient *clp;
	struct nfsclowner *owp;

	if (op == NULL)
		return;
	NFSLOCKCLSTATE();
	owp = op->nfso_own;
	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 refernce
 * 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.
 * It always clpp with a reference count on it, unless returning an error.
 */
APPLESTATIC int
nfscl_getcl(vnode_t vp, struct ucred *cred, NFSPROC_T *p,
    struct nfsclclient **clpp)
{
	struct nfsclclient *clp;
	struct nfsclclient *newclp = NULL;
	struct nfscllockowner *lp, *nlp;
	struct mount *mp;
	struct nfsmount *nmp;
	char uuid[HOSTUUIDLEN];
	int igotlock = 0, error, trystalecnt, clidinusedelay, i;
	u_int16_t idlen = 0;

	mp = vnode_mount(vp);
	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 */
		MALLOC(newclp, struct nfsclclient *,
		    sizeof (struct nfsclclient) + idlen - 1, M_NFSCLCLIENT,
		    M_WAITOK);
	}
	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 ((mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
		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;
		NFSBZERO((caddr_t)clp, sizeof(struct nfsclclient) + idlen - 1);
		clp->nfsc_idlen = idlen;
		LIST_INIT(&clp->nfsc_owner);
		TAILQ_INIT(&clp->nfsc_deleg);
		for (i = 0; i < NFSCLDELEGHASHSIZE; i++)
			LIST_INIT(&clp->nfsc_deleghash[i]);
		LIST_INIT(&clp->nfsc_defunctlockowner);
		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();
		nfscl_start_renewthread(clp);
	} else {
		NFSUNLOCKCLSTATE();
		if (newclp != NULL)
			FREE((caddr_t)newclp, M_NFSCLCLIENT);
	}
	NFSLOCKCLSTATE();
	while ((clp->nfsc_flags & NFSCLFLAGS_HASCLIENTID) == 0 && !igotlock)
		igotlock = nfsv4_lock(&clp->nfsc_lock, 1, NULL,
		    NFSCLSTATEMUTEXPTR, mp);
	if (!igotlock)
		nfsv4_getref(&clp->nfsc_lock, NULL, NFSCLSTATEMUTEXPTR, mp);
	if (igotlock == 0 && (mp->mnt_kern_flag & MNTK_UNMOUNTF) != 0) {
		/*
		 * Both nfsv4_lock() and nfsv4_getref() know to check
		 * for MNTK_UNMOUNTF 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);
		}
		/* get rid of defunct lockowners */
		LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list,
		    nlp) {
			nfscl_freelockowner(lp, 0);
		}
		/*
		 * 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(VFSTONFS(vnode_mount(vp)),
			    clp, cred, p);
			if (error == NFSERR_STALECLIENTID ||
			    error == NFSERR_STALEDONTRECOVER ||
			    error == NFSERR_CLIDINUSE) {
				(void) nfs_catnap(PZERO, error, "nfs_setcl");
			}
		} while (((error == NFSERR_STALECLIENTID ||
		     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;
	int error = 0, ret, donelocally = 0;
	u_int32_t mode;

	if (type == F_WRLCK)
		mode = NFSV4OPEN_ACCESSWRITE;
	else
		mode = NFSV4OPEN_ACCESSREAD;
	np = VTONFS(vp);
	*lpp = NULL;
	*newonep = 0;
	*donelocallyp = 0;

	/*
	 * Might need these, so MALLOC them now, to
	 * avoid a tsleep() in MALLOC later.
	 */
	MALLOC(nlp, struct nfscllockowner *,
	    sizeof (struct nfscllockowner), M_NFSCLLOCKOWNER, M_WAITOK);
	MALLOC(otherlop, struct nfscllock *,
	    sizeof (struct nfscllock), M_NFSCLLOCK, M_WAITOK);
	MALLOC(nlop, struct nfscllock *,
	    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(vp, cred, p, &clp);
	}
	if (error) {
		FREE((caddr_t)nlp, M_NFSCLLOCKOWNER);
		FREE((caddr_t)otherlop, M_NFSCLLOCK);
		FREE((caddr_t)nlop, M_NFSCLLOCK);
		return (error);
	}

	op = NULL;
	if (recovery) {
		ownp = rownp;
	} else {
		nfscl_filllockowner(id, own, flags);
		ownp = own;
	}
	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 the associated open to get the correct openowner */
		ret = nfscl_getopen(&dp->nfsdl_owner, np->n_fhp->nfh_fh,
		    np->n_fhp->nfh_len, NULL, p, mode, NULL, &op);
		if (ret)
			ret = nfscl_getopen(&clp->nfsc_owner,
			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, NULL, p,
			    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.
		 */
		if (recovery)
			error = nfscl_getopen(&clp->nfsc_owner,
			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, ropenownp,
			    NULL, mode, NULL, &op);
		else
			error = nfscl_getopen(&clp->nfsc_owner,
			    np->n_fhp->nfh_fh, np->n_fhp->nfh_len, NULL, p,
			    mode, NULL, &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((caddr_t)nlp, M_NFSCLLOCKOWNER);
		FREE((caddr_t)otherlop, M_NFSCLLOCK);
		FREE((caddr_t)nlop, M_NFSCLLOCK);
		return (error);
	}

	/*
	 * Ok, see if a lockowner exists and create one, as required.
	 */
	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_defunct = 0;
		nlp->nfsl_inprog = NULL;
		nfscl_lockinit(&nlp->nfsl_rwlock);
		LIST_INIT(&nlp->nfsl_lock);
		if (donelocally) {
			nlp->nfsl_open = NULL;
			newnfsstats.cllocallockowners++;
		} else {
			nlp->nfsl_open = op;
			newnfsstats.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((caddr_t)nlp, M_NFSCLLOCKOWNER);
	if (nlop)
		FREE((caddr_t)nlop, M_NFSCLLOCK);
	if (otherlop)
		FREE((caddr_t)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.
	 */
	MALLOC(nlop, struct nfscllock *,
	    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((caddr_t)nlop, M_NFSCLLOCK);
			return (NFSERR_INVAL);
		}
	}
	if (callcnt == 0) {
		MALLOC(other_lop, struct nfscllock *,
		    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((caddr_t)nlop, M_NFSCLLOCK);
	if (other_lop)
		FREE((caddr_t)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(vp, cred, p, &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((caddr_t)op, M_NFSCLOPEN);
	if (local)
		newnfsstats.cllocalopens--;
	else
		newnfsstats.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((caddr_t)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((caddr_t)owp, M_NFSCLOWNER);
	if (local)
		newnfsstats.cllocalopenowners--;
	else
		newnfsstats.clopenowners--;
}

/*
 * Free up a byte range lock owner structure.
 */
static 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((caddr_t)lp, M_NFSCLLOCKOWNER);
	if (local)
		newnfsstats.cllocallockowners--;
	else
		newnfsstats.cllockowners--;
}

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

	LIST_REMOVE(lop, nfslo_list);
	FREE((caddr_t)lop, M_NFSCLLOCK);
	if (local)
		newnfsstats.cllocallocks--;
	else
		newnfsstats.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((caddr_t)dp, M_NFSCLDELEG);
	newnfsstats.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 nfscllockowner *lp, *nlp;


	/* get rid of defunct lockowners */
	LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list, nlp) {
		nfscl_freelockowner(lp, 0);
	}

	/* 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);
			    newnfsstats.cllocalopens--;
			    newnfsstats.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);
			newnfsstats.cllocalopenowners--;
			newnfsstats.clopenowners++;
			newnfsstats.cllocalopens--;
			newnfsstats.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);
	}
}

#ifndef	__FreeBSD__
/*
 * Called from exit() upon process termination.
 */
APPLESTATIC void
nfscl_cleanup(NFSPROC_T *p)
{
	struct nfsclclient *clp;
	u_int8_t own[NFSV4CL_LOCKNAMELEN];

	if (!nfscl_inited)
		return;
	nfscl_filllockowner(p->td_proc, own, F_POSIX);

	NFSLOCKCLSTATE();
	/*
	 * Loop through all the clientids, looking for the OpenOwners.
	 */
	LIST_FOREACH(clp, &nfsclhead, nfsc_list)
		nfscl_cleanup_common(clp, own);
	NFSUNLOCKCLSTATE();
}
#endif	/* !__FreeBSD__ */

/*
 * Common code used by nfscl_cleanup() and nfscl_cleanupkext().
 * Must be called with CLSTATE lock held.
 */
static void
nfscl_cleanup_common(struct nfsclclient *clp, u_int8_t *own)
{
	struct nfsclowner *owp, *nowp;
	struct nfsclopen *op;
	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;
		} else {
			/* look for lockowners on other opens */
			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
				LIST_FOREACH(lp, &op->nfso_lock, nfsl_list) {
					if (!NFSBCMP(lp->nfsl_owner, own,
					    NFSV4CL_LOCKNAMELEN))
						lp->nfsl_defunct = 1;
				}
			}
		}
		owp = nowp;
	}

	/* and check the defunct list */
	LIST_FOREACH(lp, &clp->nfsc_defunctlockowner, nfsl_list) {
		if (!NFSBCMP(lp->nfsl_owner, own, NFSV4CL_LOCKNAMELEN))
		    lp->nfsl_defunct = 1;
	}
}

#if defined(APPLEKEXT) || defined(__FreeBSD__)
/*
 * Simulate the call nfscl_cleanup() by looking for open owners associated
 * with processes that no longer exist, since a call to nfscl_cleanup()
 * can't be patched into exit().
 */
static void
nfscl_cleanupkext(struct nfsclclient *clp)
{
	struct nfsclowner *owp, *nowp;
	struct nfscllockowner *lp;

	NFSPROCLISTLOCK();
	NFSLOCKCLSTATE();
	LIST_FOREACH_SAFE(owp, &clp->nfsc_owner, nfsow_list, nowp) {
		if (nfscl_procdoesntexist(owp->nfsow_owner))
			nfscl_cleanup_common(clp, owp->nfsow_owner);
	}

	/* and check the defunct list */
	LIST_FOREACH(lp, &clp->nfsc_defunctlockowner, nfsl_list) {
		if (nfscl_procdoesntexist(lp->nfsl_owner))
			lp->nfsl_defunct = 1;
	}
	NFSUNLOCKCLSTATE();
	NFSPROCLISTUNLOCK();
}
#endif	/* APPLEKEXT || __FreeBSD__ */

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:
	 * http://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();
		(void) nfsrpc_setclient(nmp, clp, cred, p);
		nfscl_cleanclient(clp);
		nmp->nm_clp = NULL;
		NFSFREECRED(cred);
		FREE((caddr_t)clp, M_NFSCLCLIENT);
	} else
		NFSUNLOCKCLSTATE();
}

/*
 * This function is called when a server replies with NFSERR_STALECLIENTID
 * or NFSERR_STALESTATEID. 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 igotlock = 0, error, trycnt, firstlock, s;

	/*
	 * 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");
	trycnt = 5;
	do {
		error = nfsrpc_setclient(nmp, clp, cred, p);
	} while ((error == NFSERR_STALECLIENTID ||
	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
	if (error) {
		nfscl_cleanclient(clp);
		NFSLOCKCLSTATE();
		clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
		    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 or
	 * NFSERR_STALECLIENTID reply from the server. This will be
	 * translated to NFSERR_STALEDONTRECOVER when R_DONTRECOVER is set.
	 */
	s = splsoftclock();
	NFSLOCKREQ();
	TAILQ_FOREACH(rep, &nfsd_reqq, r_chain) {
		if (rep->r_nmp == nmp)
			rep->r_flags |= R_DONTRECOVER;
	}
	NFSUNLOCKREQ();
	splx(s);

	/* get rid of defunct lockowners */
	LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list, nlp) {
		nfscl_freelockowner(lp, 0);
	}

	/*
	 * 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) {
		    /* 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((caddr_t)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;
				if (error != NFSERR_NOGRACE)
				    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;
			}
		    } else {
			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) {
		    MALLOC(nowp, struct nfsclowner *,
			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) {
		    MALLOC(nop, struct nfsclopen *, 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((caddr_t)tdp, M_NFSCLDELEG);
			} else {
			    TAILQ_INSERT_HEAD(&extra_deleg, tdp, nfsdl_list);
			}
		    }
		}
		if (error) {
		    if (nop != NULL)
			FREE((caddr_t)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((caddr_t)op, M_NFSCLOPEN);
	}
	if (nowp != NULL)
		FREE((caddr_t)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((caddr_t)dp, M_NFSCLDELEG);
	}

	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 nfscllockowner *lp, *nlp;
	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, cred, p);
	} while ((error == NFSERR_STALECLIENTID ||
	     error == NFSERR_STALEDONTRECOVER) && --trycnt > 0);
	if (error) {
		/*
		 * Clear out any state.
		 */
		nfscl_cleanclient(clp);
		NFSLOCKCLSTATE();
		clp->nfsc_flags &= ~(NFSCLFLAGS_HASCLIENTID |
		    NFSCLFLAGS_RECOVER);
	} else {
		/* get rid of defunct lockowners */
		LIST_FOREACH_SAFE(lp, &clp->nfsc_defunctlockowner, nfsl_list,
		    nlp) {
			nfscl_freelockowner(lp, 0);
		}

		/*
		 * 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)
		newnfsstats.cllocallocks++;
	else
		newnfsstats.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, *olp;
	struct nfscldeleghead dh;
	struct nfscllockownerhead lh;
	struct nfscldeleg *dp, *ndp;
	struct ucred *cred;
	u_int32_t clidrev;
	int error, cbpathdown, islept, igotlock, ret, clearok;
	uint32_t recover_done_time = 0;

	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_recover(clp, cred, p);
			} else {
				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, cred, p);
			if (error == NFSERR_CBPATHDOWN)
			    cbpathdown = 1;
			else if (error == NFSERR_STALECLIENTID) {
			    NFSLOCKCLSTATE();
			    clp->nfsc_flags |= NFSCLFLAGS_RECOVER;
			    NFSUNLOCKCLSTATE();
			} else if (error == NFSERR_EXPIRED)
			    (void) nfscl_hasexpired(clp, clidrev, p);
		}

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

		/*
		 * Now, handle defunct owners.
		 */
		owp = LIST_FIRST(&clp->nfsc_owner);
		while (owp != NULL) {
		    nowp = LIST_NEXT(owp, nfsow_list);
		    if (LIST_EMPTY(&owp->nfsow_open)) {
			if (owp->nfsow_defunct)
			    nfscl_freeopenowner(owp, 0);
		    } else {
			LIST_FOREACH(op, &owp->nfsow_open, nfso_list) {
			    lp = LIST_FIRST(&op->nfso_lock);
			    while (lp != NULL) {
				nlp = LIST_NEXT(lp, nfsl_list);
				if (lp->nfsl_defunct &&
				    LIST_EMPTY(&lp->nfsl_lock)) {
				    LIST_FOREACH(olp, &lh, nfsl_list) {
					if (!NFSBCMP(olp->nfsl_owner,
					    lp->nfsl_owner,NFSV4CL_LOCKNAMELEN))
					    break;
				    }
				    if (olp == NULL) {
					LIST_REMOVE(lp, nfsl_list);
					LIST_INSERT_HEAD(&lh, lp, nfsl_list);
				    } else {
					nfscl_freelockowner(lp, 0);
				    }
				}
				lp = nlp;
			    }
			}
		    }
		    owp = nowp;
		}

		/* also search the defunct list */
		lp = LIST_FIRST(&clp->nfsc_defunctlockowner);
		while (lp != NULL) {
		    nlp = LIST_NEXT(lp, nfsl_list);
		    if (lp->nfsl_defunct) {
			LIST_FOREACH(olp, &lh, nfsl_list) {
			    if (!NFSBCMP(olp->nfsl_owner, lp->nfsl_owner,
				NFSV4CL_LOCKNAMELEN))
				break;
			}
			if (olp == NULL) {
			    LIST_REMOVE(lp, nfsl_list);
			    LIST_INSERT_HEAD(&lh, lp, nfsl_list);
			} else {
			    nfscl_freelockowner(lp, 0);
			}
		    }
		    lp = nlp;
		}
		/* and release defunct lock owners */
		LIST_FOREACH_SAFE(lp, &lh, nfsl_list, nlp) {
		    nfscl_freelockowner(lp, 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--;
				    newnfsstats.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--;
			    newnfsstats.cldelegates--;
			}
		    }
		    dp = ndp;
		}
		if (igotlock)
			nfsv4_unlock(&clp->nfsc_lock, 0);
		NFSUNLOCKCLSTATE();

		/*
		 * 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((caddr_t)dp, M_NFSCLDELEG);
		}

#if defined(APPLEKEXT) || defined(__FreeBSD__)
		/*
		 * Simulate the calls to nfscl_cleanup() when a process
		 * exits, since the call can't be patched into exit().
		 */
		{
			struct timespec mytime;
			static time_t prevsec = 0;

			NFSGETNANOTIME(&mytime);
			if (prevsec != mytime.tv_sec) {
				prevsec = mytime.tv_sec;
				nfscl_cleanupkext(clp);
			}
		}
#endif	/* APPLEKEXT || __FreeBSD__ */

		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 or
 * NFSERR_STALESTATEID 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(vp, NULL, NULL, &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;
	int error;

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

	nfhp = VTONFS(vp)->n_fhp;
	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);
		}
	}

	/* 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();
	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 i, op;
	u_int32_t *tl;
	struct nfsclclient *clp;
	struct nfscldeleg *dp = NULL;
	int numops, taglen = -1, error = 0, trunc, ret = 0;
	u_int32_t minorvers, 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;

	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)
		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) {
		    nd->nd_repstat = NFSERR_OPILLEGAL;
		    *repp = nfscl_errmap(nd);
		    retops++;
		    break;
		}
		nd->nd_procnum = op;
		newnfsstats.cbrpccnt[nd->nd_procnum]++;
		switch (op) {
		case NFSV4OP_CBGETATTR:
			clp = NULL;
			error = nfsm_getfh(nd, &nfhp);
			if (!error)
				error = nfsrv_getattrbits(nd, &attrbits,
				    NULL, NULL);
			if (!error) {
				mp = nfscl_getmnt(cbident);
				if (mp == NULL)
					error = NFSERR_SERVERFAULT;
			}
			if (!error) {
				dp = NULL;
				NFSLOCKCLSTATE();
				clp = nfscl_findcl(VFSTONFS(mp));
				if (clp != NULL)
					dp = nfscl_finddeleg(clp, nfhp->nfh_fh,
					    nfhp->nfh_len);
				NFSUNLOCKCLSTATE();
				if (dp == NULL)
					error = NFSERR_SERVERFAULT;
			}
			if (!error) {
				ret = nfscl_ngetreopen(mp, nfhp->nfh_fh,
				    nfhp->nfh_len, p, &np);
				if (!ret)
					vp = NFSTOV(np);
			}
			if (nfhp != NULL)
				FREE((caddr_t)nfhp, M_NFSFH);
			if (!error) {
				NFSZERO_ATTRBIT(&rattrbits);
				if (NFSISSET_ATTRBIT(&attrbits,
				    NFSATTRBIT_SIZE)) {
					if (!ret)
						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 (ret || (np->n_flag & NDELEGMOD))
						va.va_filerev++;
					NFSSETBIT_ATTRBIT(&rattrbits,
					    NFSATTRBIT_CHANGE);
				}
				(void) nfsv4_fillattr(nd, NULL, NULL, NULL, &va,
				    NULL, 0, &rattrbits, NULL, NULL, 0, 0, 0, 0,
				    (uint64_t)0);
				if (!ret)
					vrele(vp);
			}
			break;
		case NFSV4OP_CBRECALL:
			clp = NULL;
			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) {
				mp = nfscl_getmnt(cbident);
				if (mp == NULL)
					error = NFSERR_SERVERFAULT;
			}
			if (!error) {
				NFSLOCKCLSTATE();
				clp = nfscl_findcl(VFSTONFS(mp));
				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((caddr_t)nfhp, M_NFSFH);
			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);
			break;
		} else
			*repp = 0;	/* NFS4_OK */
	}
nfsmout:
	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);
}

/*
 * 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.
 */
static mount_t
nfscl_getmnt(u_int32_t cbident)
{
	struct nfsclclient *clp;
	struct nfsmount *nmp;

	NFSLOCKCLSTATE();
	LIST_FOREACH(clp, &nfsclhead, nfsc_list) {
		if (clp->nfsc_cbident == cbident)
			break;
	}
	if (clp == NULL) {
		NFSUNLOCKCLSTATE();
		return (NULL);
	}
	nmp = clp->nfsc_nmp;
	NFSUNLOCKCLSTATE();
	return (nmp->nm_mountp);
}

/*
 * 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, cred, 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) {
						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) {
				MALLOC(nowp, struct nfsclowner *,
				    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);
				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) {
						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) {
				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);
	MALLOC(nop, struct nfsclopen *, 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, &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 {
		if (newone)
			newnfs_copyincred(cred, &op->nfso_cred);
		op->nfso_mode |= lop->nfso_mode;
		op->nfso_opencnt += lop->nfso_opencnt;
		nfscl_freeopen(lop, 1);
	}
	if (nop != NULL)
		FREE((caddr_t)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((caddr_t)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;
	u_int32_t clidrev = 0;
	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, 0, lp->nfsl_owner, lp->nfsl_openowner, &nlp, &newone,
	    &donelocally);
	if (error || donelocally)
		return (error);
	if (nmp->nm_clp != NULL)
		clidrev = nmp->nm_clp->nfsc_clientidrev;
	else
		clidrev = 0;
	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)) {
		NFSGETNANOTIME(&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)
{
	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));
	errp = defaulterrp = nfscl_cberrmap[nd->nd_procnum];
	while (*++errp)
		if (*errp == (short)nd->nd_repstat)
			return (txdr_unsigned(nd->nd_repstat));
	return (txdr_unsigned(*defaulterrp));
}

OpenPOWER on IntegriCloud