aboutsummaryrefslogtreecommitdiffstats
path: root/uts/common/fs/vnode.c
blob: 382369c7fc7239d42dc1eecf45ef80c2392d9de7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
 */

/*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/

/*
 * University Copyright- Copyright (c) 1982, 1986, 1988
 * The Regents of the University of California
 * All Rights Reserved
 *
 * University Acknowledgment- Portions of this document are derived from
 * software developed by the University of California, Berkeley, and its
 * contributors.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/t_lock.h>
#include <sys/errno.h>
#include <sys/cred.h>
#include <sys/user.h>
#include <sys/uio.h>
#include <sys/file.h>
#include <sys/pathname.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/rwstlock.h>
#include <sys/fem.h>
#include <sys/stat.h>
#include <sys/mode.h>
#include <sys/conf.h>
#include <sys/sysmacros.h>
#include <sys/cmn_err.h>
#include <sys/systm.h>
#include <sys/kmem.h>
#include <sys/debug.h>
#include <c2/audit.h>
#include <sys/acl.h>
#include <sys/nbmlock.h>
#include <sys/fcntl.h>
#include <fs/fs_subr.h>
#include <sys/taskq.h>
#include <fs/fs_reparse.h>

/* Determine if this vnode is a file that is read-only */
#define	ISROFILE(vp)	\
	((vp)->v_type != VCHR && (vp)->v_type != VBLK && \
	    (vp)->v_type != VFIFO && vn_is_readonly(vp))

/* Tunable via /etc/system; used only by admin/install */
int nfs_global_client_only;

/*
 * Array of vopstats_t for per-FS-type vopstats.  This array has the same
 * number of entries as and parallel to the vfssw table.  (Arguably, it could
 * be part of the vfssw table.)  Once it's initialized, it's accessed using
 * the same fstype index that is used to index into the vfssw table.
 */
vopstats_t **vopstats_fstype;

/* vopstats initialization template used for fast initialization via bcopy() */
static vopstats_t *vs_templatep;

/* Kmem cache handle for vsk_anchor_t allocations */
kmem_cache_t *vsk_anchor_cache;

/* file events cleanup routine */
extern void free_fopdata(vnode_t *);

/*
 * Root of AVL tree for the kstats associated with vopstats.  Lock protects
 * updates to vsktat_tree.
 */
avl_tree_t	vskstat_tree;
kmutex_t	vskstat_tree_lock;

/* Global variable which enables/disables the vopstats collection */
int vopstats_enabled = 1;

/*
 * forward declarations for internal vnode specific data (vsd)
 */
static void *vsd_realloc(void *, size_t, size_t);

/*
 * forward declarations for reparse point functions
 */
static int fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr);

/*
 * VSD -- VNODE SPECIFIC DATA
 * The v_data pointer is typically used by a file system to store a
 * pointer to the file system's private node (e.g. ufs inode, nfs rnode).
 * However, there are times when additional project private data needs
 * to be stored separately from the data (node) pointed to by v_data.
 * This additional data could be stored by the file system itself or
 * by a completely different kernel entity.  VSD provides a way for
 * callers to obtain a key and store a pointer to private data associated
 * with a vnode.
 *
 * Callers are responsible for protecting the vsd by holding v_vsd_lock
 * for calls to vsd_set() and vsd_get().
 */

/*
 * vsd_lock protects:
 *   vsd_nkeys - creation and deletion of vsd keys
 *   vsd_list - insertion and deletion of vsd_node in the vsd_list
 *   vsd_destructor - adding and removing destructors to the list
 */
static kmutex_t		vsd_lock;
static uint_t		vsd_nkeys;	 /* size of destructor array */
/* list of vsd_node's */
static list_t *vsd_list = NULL;
/* per-key destructor funcs */
static void 		(**vsd_destructor)(void *);

/*
 * The following is the common set of actions needed to update the
 * vopstats structure from a vnode op.  Both VOPSTATS_UPDATE() and
 * VOPSTATS_UPDATE_IO() do almost the same thing, except for the
 * recording of the bytes transferred.  Since the code is similar
 * but small, it is nearly a duplicate.  Consequently any changes
 * to one may need to be reflected in the other.
 * Rundown of the variables:
 * vp - Pointer to the vnode
 * counter - Partial name structure member to update in vopstats for counts
 * bytecounter - Partial name structure member to update in vopstats for bytes
 * bytesval - Value to update in vopstats for bytes
 * fstype - Index into vsanchor_fstype[], same as index into vfssw[]
 * vsp - Pointer to vopstats structure (either in vfs or vsanchor_fstype[i])
 */

#define	VOPSTATS_UPDATE(vp, counter) {					\
	vfs_t *vfsp = (vp)->v_vfsp;					\
	if (vfsp && vfsp->vfs_implp &&					\
	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
		    size_t, uint64_t *);				\
		__dtrace_probe___fsinfo_##counter(vp, 0, stataddr);	\
		(*stataddr)++;						\
		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
			vsp->n##counter.value.ui64++;			\
		}							\
	}								\
}

#define	VOPSTATS_UPDATE_IO(vp, counter, bytecounter, bytesval) {	\
	vfs_t *vfsp = (vp)->v_vfsp;					\
	if (vfsp && vfsp->vfs_implp &&					\
	    (vfsp->vfs_flag & VFS_STATS) && (vp)->v_type != VBAD) {	\
		vopstats_t *vsp = &vfsp->vfs_vopstats;			\
		uint64_t *stataddr = &(vsp->n##counter.value.ui64);	\
		extern void __dtrace_probe___fsinfo_##counter(vnode_t *, \
		    size_t, uint64_t *);				\
		__dtrace_probe___fsinfo_##counter(vp, bytesval, stataddr); \
		(*stataddr)++;						\
		vsp->bytecounter.value.ui64 += bytesval;		\
		if ((vsp = vfsp->vfs_fstypevsp) != NULL) {		\
			vsp->n##counter.value.ui64++;			\
			vsp->bytecounter.value.ui64 += bytesval;	\
		}							\
	}								\
}

/*
 * If the filesystem does not support XIDs map credential
 * If the vfsp is NULL, perhaps we should also map?
 */
#define	VOPXID_MAP_CR(vp, cr)	{					\
	vfs_t *vfsp = (vp)->v_vfsp;					\
	if (vfsp != NULL && (vfsp->vfs_flag & VFS_XID) == 0)		\
		cr = crgetmapped(cr);					\
	}

/*
 * Convert stat(2) formats to vnode types and vice versa.  (Knows about
 * numerical order of S_IFMT and vnode types.)
 */
enum vtype iftovt_tab[] = {
	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VNON
};

ushort_t vttoif_tab[] = {
	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, S_IFIFO,
	S_IFDOOR, 0, S_IFSOCK, S_IFPORT, 0
};

/*
 * The system vnode cache.
 */

kmem_cache_t *vn_cache;


/*
 * Vnode operations vector.
 */

static const fs_operation_trans_def_t vn_ops_table[] = {
	VOPNAME_OPEN, offsetof(struct vnodeops, vop_open),
	    fs_nosys, fs_nosys,

	VOPNAME_CLOSE, offsetof(struct vnodeops, vop_close),
	    fs_nosys, fs_nosys,

	VOPNAME_READ, offsetof(struct vnodeops, vop_read),
	    fs_nosys, fs_nosys,

	VOPNAME_WRITE, offsetof(struct vnodeops, vop_write),
	    fs_nosys, fs_nosys,

	VOPNAME_IOCTL, offsetof(struct vnodeops, vop_ioctl),
	    fs_nosys, fs_nosys,

	VOPNAME_SETFL, offsetof(struct vnodeops, vop_setfl),
	    fs_setfl, fs_nosys,

	VOPNAME_GETATTR, offsetof(struct vnodeops, vop_getattr),
	    fs_nosys, fs_nosys,

	VOPNAME_SETATTR, offsetof(struct vnodeops, vop_setattr),
	    fs_nosys, fs_nosys,

	VOPNAME_ACCESS, offsetof(struct vnodeops, vop_access),
	    fs_nosys, fs_nosys,

	VOPNAME_LOOKUP, offsetof(struct vnodeops, vop_lookup),
	    fs_nosys, fs_nosys,

	VOPNAME_CREATE, offsetof(struct vnodeops, vop_create),
	    fs_nosys, fs_nosys,

	VOPNAME_REMOVE, offsetof(struct vnodeops, vop_remove),
	    fs_nosys, fs_nosys,

	VOPNAME_LINK, offsetof(struct vnodeops, vop_link),
	    fs_nosys, fs_nosys,

	VOPNAME_RENAME, offsetof(struct vnodeops, vop_rename),
	    fs_nosys, fs_nosys,

	VOPNAME_MKDIR, offsetof(struct vnodeops, vop_mkdir),
	    fs_nosys, fs_nosys,

	VOPNAME_RMDIR, offsetof(struct vnodeops, vop_rmdir),
	    fs_nosys, fs_nosys,

	VOPNAME_READDIR, offsetof(struct vnodeops, vop_readdir),
	    fs_nosys, fs_nosys,

	VOPNAME_SYMLINK, offsetof(struct vnodeops, vop_symlink),
	    fs_nosys, fs_nosys,

	VOPNAME_READLINK, offsetof(struct vnodeops, vop_readlink),
	    fs_nosys, fs_nosys,

	VOPNAME_FSYNC, offsetof(struct vnodeops, vop_fsync),
	    fs_nosys, fs_nosys,

	VOPNAME_INACTIVE, offsetof(struct vnodeops, vop_inactive),
	    fs_nosys, fs_nosys,

	VOPNAME_FID, offsetof(struct vnodeops, vop_fid),
	    fs_nosys, fs_nosys,

	VOPNAME_RWLOCK, offsetof(struct vnodeops, vop_rwlock),
	    fs_rwlock, fs_rwlock,

	VOPNAME_RWUNLOCK, offsetof(struct vnodeops, vop_rwunlock),
	    (fs_generic_func_p) fs_rwunlock,
	    (fs_generic_func_p) fs_rwunlock,	/* no errors allowed */

	VOPNAME_SEEK, offsetof(struct vnodeops, vop_seek),
	    fs_nosys, fs_nosys,

	VOPNAME_CMP, offsetof(struct vnodeops, vop_cmp),
	    fs_cmp, fs_cmp,		/* no errors allowed */

	VOPNAME_FRLOCK, offsetof(struct vnodeops, vop_frlock),
	    fs_frlock, fs_nosys,

	VOPNAME_SPACE, offsetof(struct vnodeops, vop_space),
	    fs_nosys, fs_nosys,

	VOPNAME_REALVP, offsetof(struct vnodeops, vop_realvp),
	    fs_nosys, fs_nosys,

	VOPNAME_GETPAGE, offsetof(struct vnodeops, vop_getpage),
	    fs_nosys, fs_nosys,

	VOPNAME_PUTPAGE, offsetof(struct vnodeops, vop_putpage),
	    fs_nosys, fs_nosys,

	VOPNAME_MAP, offsetof(struct vnodeops, vop_map),
	    (fs_generic_func_p) fs_nosys_map,
	    (fs_generic_func_p) fs_nosys_map,

	VOPNAME_ADDMAP, offsetof(struct vnodeops, vop_addmap),
	    (fs_generic_func_p) fs_nosys_addmap,
	    (fs_generic_func_p) fs_nosys_addmap,

	VOPNAME_DELMAP, offsetof(struct vnodeops, vop_delmap),
	    fs_nosys, fs_nosys,

	VOPNAME_POLL, offsetof(struct vnodeops, vop_poll),
	    (fs_generic_func_p) fs_poll, (fs_generic_func_p) fs_nosys_poll,

	VOPNAME_DUMP, offsetof(struct vnodeops, vop_dump),
	    fs_nosys, fs_nosys,

	VOPNAME_PATHCONF, offsetof(struct vnodeops, vop_pathconf),
	    fs_pathconf, fs_nosys,

	VOPNAME_PAGEIO, offsetof(struct vnodeops, vop_pageio),
	    fs_nosys, fs_nosys,

	VOPNAME_DUMPCTL, offsetof(struct vnodeops, vop_dumpctl),
	    fs_nosys, fs_nosys,

	VOPNAME_DISPOSE, offsetof(struct vnodeops, vop_dispose),
	    (fs_generic_func_p) fs_dispose,
	    (fs_generic_func_p) fs_nodispose,

	VOPNAME_SETSECATTR, offsetof(struct vnodeops, vop_setsecattr),
	    fs_nosys, fs_nosys,

	VOPNAME_GETSECATTR, offsetof(struct vnodeops, vop_getsecattr),
	    fs_fab_acl, fs_nosys,

	VOPNAME_SHRLOCK, offsetof(struct vnodeops, vop_shrlock),
	    fs_shrlock, fs_nosys,

	VOPNAME_VNEVENT, offsetof(struct vnodeops, vop_vnevent),
	    (fs_generic_func_p) fs_vnevent_nosupport,
	    (fs_generic_func_p) fs_vnevent_nosupport,

	VOPNAME_REQZCBUF, offsetof(struct vnodeops, vop_reqzcbuf),
	    fs_nosys, fs_nosys,

	VOPNAME_RETZCBUF, offsetof(struct vnodeops, vop_retzcbuf),
	    fs_nosys, fs_nosys,

	NULL, 0, NULL, NULL
};

/* Extensible attribute (xva) routines. */

/*
 * Zero out the structure, set the size of the requested/returned bitmaps,
 * set AT_XVATTR in the embedded vattr_t's va_mask, and set up the pointer
 * to the returned attributes array.
 */
void
xva_init(xvattr_t *xvap)
{
	bzero(xvap, sizeof (xvattr_t));
	xvap->xva_mapsize = XVA_MAPSIZE;
	xvap->xva_magic = XVA_MAGIC;
	xvap->xva_vattr.va_mask = AT_XVATTR;
	xvap->xva_rtnattrmapp = &(xvap->xva_rtnattrmap)[0];
}

/*
 * If AT_XVATTR is set, returns a pointer to the embedded xoptattr_t
 * structure.  Otherwise, returns NULL.
 */
xoptattr_t *
xva_getxoptattr(xvattr_t *xvap)
{
	xoptattr_t *xoap = NULL;
	if (xvap->xva_vattr.va_mask & AT_XVATTR)
		xoap = &xvap->xva_xoptattrs;
	return (xoap);
}

/*
 * Used by the AVL routines to compare two vsk_anchor_t structures in the tree.
 * We use the f_fsid reported by VFS_STATVFS() since we use that for the
 * kstat name.
 */
static int
vska_compar(const void *n1, const void *n2)
{
	int ret;
	ulong_t p1 = ((vsk_anchor_t *)n1)->vsk_fsid;
	ulong_t p2 = ((vsk_anchor_t *)n2)->vsk_fsid;

	if (p1 < p2) {
		ret = -1;
	} else if (p1 > p2) {
		ret = 1;
	} else {
		ret = 0;
	}

	return (ret);
}

/*
 * Used to create a single template which will be bcopy()ed to a newly
 * allocated vsanchor_combo_t structure in new_vsanchor(), below.
 */
static vopstats_t *
create_vopstats_template()
{
	vopstats_t		*vsp;

	vsp = kmem_alloc(sizeof (vopstats_t), KM_SLEEP);
	bzero(vsp, sizeof (*vsp));	/* Start fresh */

	/* VOP_OPEN */
	kstat_named_init(&vsp->nopen, "nopen", KSTAT_DATA_UINT64);
	/* VOP_CLOSE */
	kstat_named_init(&vsp->nclose, "nclose", KSTAT_DATA_UINT64);
	/* VOP_READ I/O */
	kstat_named_init(&vsp->nread, "nread", KSTAT_DATA_UINT64);
	kstat_named_init(&vsp->read_bytes, "read_bytes", KSTAT_DATA_UINT64);
	/* VOP_WRITE I/O */
	kstat_named_init(&vsp->nwrite, "nwrite", KSTAT_DATA_UINT64);
	kstat_named_init(&vsp->write_bytes, "write_bytes", KSTAT_DATA_UINT64);
	/* VOP_IOCTL */
	kstat_named_init(&vsp->nioctl, "nioctl", KSTAT_DATA_UINT64);
	/* VOP_SETFL */
	kstat_named_init(&vsp->nsetfl, "nsetfl", KSTAT_DATA_UINT64);
	/* VOP_GETATTR */
	kstat_named_init(&vsp->ngetattr, "ngetattr", KSTAT_DATA_UINT64);
	/* VOP_SETATTR */
	kstat_named_init(&vsp->nsetattr, "nsetattr", KSTAT_DATA_UINT64);
	/* VOP_ACCESS */
	kstat_named_init(&vsp->naccess, "naccess", KSTAT_DATA_UINT64);
	/* VOP_LOOKUP */
	kstat_named_init(&vsp->nlookup, "nlookup", KSTAT_DATA_UINT64);
	/* VOP_CREATE */
	kstat_named_init(&vsp->ncreate, "ncreate", KSTAT_DATA_UINT64);
	/* VOP_REMOVE */
	kstat_named_init(&vsp->nremove, "nremove", KSTAT_DATA_UINT64);
	/* VOP_LINK */
	kstat_named_init(&vsp->nlink, "nlink", KSTAT_DATA_UINT64);
	/* VOP_RENAME */
	kstat_named_init(&vsp->nrename, "nrename", KSTAT_DATA_UINT64);
	/* VOP_MKDIR */
	kstat_named_init(&vsp->nmkdir, "nmkdir", KSTAT_DATA_UINT64);
	/* VOP_RMDIR */
	kstat_named_init(&vsp->nrmdir, "nrmdir", KSTAT_DATA_UINT64);
	/* VOP_READDIR I/O */
	kstat_named_init(&vsp->nreaddir, "nreaddir", KSTAT_DATA_UINT64);
	kstat_named_init(&vsp->readdir_bytes, "readdir_bytes",
	    KSTAT_DATA_UINT64);
	/* VOP_SYMLINK */
	kstat_named_init(&vsp->nsymlink, "nsymlink", KSTAT_DATA_UINT64);
	/* VOP_READLINK */
	kstat_named_init(&vsp->nreadlink, "nreadlink", KSTAT_DATA_UINT64);
	/* VOP_FSYNC */
	kstat_named_init(&vsp->nfsync, "nfsync", KSTAT_DATA_UINT64);
	/* VOP_INACTIVE */
	kstat_named_init(&vsp->ninactive, "ninactive", KSTAT_DATA_UINT64);
	/* VOP_FID */
	kstat_named_init(&vsp->nfid, "nfid", KSTAT_DATA_UINT64);
	/* VOP_RWLOCK */
	kstat_named_init(&vsp->nrwlock, "nrwlock", KSTAT_DATA_UINT64);
	/* VOP_RWUNLOCK */
	kstat_named_init(&vsp->nrwunlock, "nrwunlock", KSTAT_DATA_UINT64);
	/* VOP_SEEK */
	kstat_named_init(&vsp->nseek, "nseek", KSTAT_DATA_UINT64);
	/* VOP_CMP */
	kstat_named_init(&vsp->ncmp, "ncmp", KSTAT_DATA_UINT64);
	/* VOP_FRLOCK */
	kstat_named_init(&vsp->nfrlock, "nfrlock", KSTAT_DATA_UINT64);
	/* VOP_SPACE */
	kstat_named_init(&vsp->nspace, "nspace", KSTAT_DATA_UINT64);
	/* VOP_REALVP */
	kstat_named_init(&vsp->nrealvp, "nrealvp", KSTAT_DATA_UINT64);
	/* VOP_GETPAGE */
	kstat_named_init(&vsp->ngetpage, "ngetpage", KSTAT_DATA_UINT64);
	/* VOP_PUTPAGE */
	kstat_named_init(&vsp->nputpage, "nputpage", KSTAT_DATA_UINT64);
	/* VOP_MAP */
	kstat_named_init(&vsp->nmap, "nmap", KSTAT_DATA_UINT64);
	/* VOP_ADDMAP */
	kstat_named_init(&vsp->naddmap, "naddmap", KSTAT_DATA_UINT64);
	/* VOP_DELMAP */
	kstat_named_init(&vsp->ndelmap, "ndelmap", KSTAT_DATA_UINT64);
	/* VOP_POLL */
	kstat_named_init(&vsp->npoll, "npoll", KSTAT_DATA_UINT64);
	/* VOP_DUMP */
	kstat_named_init(&vsp->ndump, "ndump", KSTAT_DATA_UINT64);
	/* VOP_PATHCONF */
	kstat_named_init(&vsp->npathconf, "npathconf", KSTAT_DATA_UINT64);
	/* VOP_PAGEIO */
	kstat_named_init(&vsp->npageio, "npageio", KSTAT_DATA_UINT64);
	/* VOP_DUMPCTL */
	kstat_named_init(&vsp->ndumpctl, "ndumpctl", KSTAT_DATA_UINT64);
	/* VOP_DISPOSE */
	kstat_named_init(&vsp->ndispose, "ndispose", KSTAT_DATA_UINT64);
	/* VOP_SETSECATTR */
	kstat_named_init(&vsp->nsetsecattr, "nsetsecattr", KSTAT_DATA_UINT64);
	/* VOP_GETSECATTR */
	kstat_named_init(&vsp->ngetsecattr, "ngetsecattr", KSTAT_DATA_UINT64);
	/* VOP_SHRLOCK */
	kstat_named_init(&vsp->nshrlock, "nshrlock", KSTAT_DATA_UINT64);
	/* VOP_VNEVENT */
	kstat_named_init(&vsp->nvnevent, "nvnevent", KSTAT_DATA_UINT64);
	/* VOP_REQZCBUF */
	kstat_named_init(&vsp->nreqzcbuf, "nreqzcbuf", KSTAT_DATA_UINT64);
	/* VOP_RETZCBUF */
	kstat_named_init(&vsp->nretzcbuf, "nretzcbuf", KSTAT_DATA_UINT64);

	return (vsp);
}

/*
 * Creates a kstat structure associated with a vopstats structure.
 */
kstat_t *
new_vskstat(char *ksname, vopstats_t *vsp)
{
	kstat_t		*ksp;

	if (!vopstats_enabled) {
		return (NULL);
	}

	ksp = kstat_create("unix", 0, ksname, "misc", KSTAT_TYPE_NAMED,
	    sizeof (vopstats_t)/sizeof (kstat_named_t),
	    KSTAT_FLAG_VIRTUAL|KSTAT_FLAG_WRITABLE);
	if (ksp) {
		ksp->ks_data = vsp;
		kstat_install(ksp);
	}

	return (ksp);
}

/*
 * Called from vfsinit() to initialize the support mechanisms for vopstats
 */
void
vopstats_startup()
{
	if (!vopstats_enabled)
		return;

	/*
	 * Creates the AVL tree which holds per-vfs vopstat anchors.  This
	 * is necessary since we need to check if a kstat exists before we
	 * attempt to create it.  Also, initialize its lock.
	 */
	avl_create(&vskstat_tree, vska_compar, sizeof (vsk_anchor_t),
	    offsetof(vsk_anchor_t, vsk_node));
	mutex_init(&vskstat_tree_lock, NULL, MUTEX_DEFAULT, NULL);

	vsk_anchor_cache = kmem_cache_create("vsk_anchor_cache",
	    sizeof (vsk_anchor_t), sizeof (uintptr_t), NULL, NULL, NULL,
	    NULL, NULL, 0);

	/*
	 * Set up the array of pointers for the vopstats-by-FS-type.
	 * The entries will be allocated/initialized as each file system
	 * goes through modload/mod_installfs.
	 */
	vopstats_fstype = (vopstats_t **)kmem_zalloc(
	    (sizeof (vopstats_t *) * nfstype), KM_SLEEP);

	/* Set up the global vopstats initialization template */
	vs_templatep = create_vopstats_template();
}

/*
 * We need to have the all of the counters zeroed.
 * The initialization of the vopstats_t includes on the order of
 * 50 calls to kstat_named_init().  Rather that do that on every call,
 * we do it once in a template (vs_templatep) then bcopy it over.
 */
void
initialize_vopstats(vopstats_t *vsp)
{
	if (vsp == NULL)
		return;

	bcopy(vs_templatep, vsp, sizeof (vopstats_t));
}

/*
 * If possible, determine which vopstats by fstype to use and
 * return a pointer to the caller.
 */
vopstats_t *
get_fstype_vopstats(vfs_t *vfsp, struct vfssw *vswp)
{
	int		fstype = 0;	/* Index into vfssw[] */
	vopstats_t	*vsp = NULL;

	if (vfsp == NULL || (vfsp->vfs_flag & VFS_STATS) == 0 ||
	    !vopstats_enabled)
		return (NULL);
	/*
	 * Set up the fstype.  We go to so much trouble because all versions
	 * of NFS use the same fstype in their vfs even though they have
	 * distinct entries in the vfssw[] table.
	 * NOTE: A special vfs (e.g., EIO_vfs) may not have an entry.
	 */
	if (vswp) {
		fstype = vswp - vfssw;	/* Gets us the index */
	} else {
		fstype = vfsp->vfs_fstype;
	}

	/*
	 * Point to the per-fstype vopstats. The only valid values are
	 * non-zero positive values less than the number of vfssw[] table
	 * entries.
	 */
	if (fstype > 0 && fstype < nfstype) {
		vsp = vopstats_fstype[fstype];
	}

	return (vsp);
}

/*
 * Generate a kstat name, create the kstat structure, and allocate a
 * vsk_anchor_t to hold it together.  Return the pointer to the vsk_anchor_t
 * to the caller.  This must only be called from a mount.
 */
vsk_anchor_t *
get_vskstat_anchor(vfs_t *vfsp)
{
	char		kstatstr[KSTAT_STRLEN]; /* kstat name for vopstats */
	statvfs64_t	statvfsbuf;		/* Needed to find f_fsid */
	vsk_anchor_t	*vskp = NULL;		/* vfs <--> kstat anchor */
	kstat_t		*ksp;			/* Ptr to new kstat */
	avl_index_t	where;			/* Location in the AVL tree */

	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
		return (NULL);

	/* Need to get the fsid to build a kstat name */
	if (VFS_STATVFS(vfsp, &statvfsbuf) == 0) {
		/* Create a name for our kstats based on fsid */
		(void) snprintf(kstatstr, KSTAT_STRLEN, "%s%lx",
		    VOPSTATS_STR, statvfsbuf.f_fsid);

		/* Allocate and initialize the vsk_anchor_t */
		vskp = kmem_cache_alloc(vsk_anchor_cache, KM_SLEEP);
		bzero(vskp, sizeof (*vskp));
		vskp->vsk_fsid = statvfsbuf.f_fsid;

		mutex_enter(&vskstat_tree_lock);
		if (avl_find(&vskstat_tree, vskp, &where) == NULL) {
			avl_insert(&vskstat_tree, vskp, where);
			mutex_exit(&vskstat_tree_lock);

			/*
			 * Now that we've got the anchor in the AVL
			 * tree, we can create the kstat.
			 */
			ksp = new_vskstat(kstatstr, &vfsp->vfs_vopstats);
			if (ksp) {
				vskp->vsk_ksp = ksp;
			}
		} else {
			/* Oops, found one! Release memory and lock. */
			mutex_exit(&vskstat_tree_lock);
			kmem_cache_free(vsk_anchor_cache, vskp);
			vskp = NULL;
		}
	}
	return (vskp);
}

/*
 * We're in the process of tearing down the vfs and need to cleanup
 * the data structures associated with the vopstats. Must only be called
 * from dounmount().
 */
void
teardown_vopstats(vfs_t *vfsp)
{
	vsk_anchor_t	*vskap;
	avl_index_t	where;

	if (vfsp == NULL || vfsp->vfs_implp == NULL ||
	    (vfsp->vfs_flag & VFS_STATS) == 0 || !vopstats_enabled)
		return;

	/* This is a safe check since VFS_STATS must be set (see above) */
	if ((vskap = vfsp->vfs_vskap) == NULL)
		return;

	/* Whack the pointer right away */
	vfsp->vfs_vskap = NULL;

	/* Lock the tree, remove the node, and delete the kstat */
	mutex_enter(&vskstat_tree_lock);
	if (avl_find(&vskstat_tree, vskap, &where)) {
		avl_remove(&vskstat_tree, vskap);
	}

	if (vskap->vsk_ksp) {
		kstat_delete(vskap->vsk_ksp);
	}
	mutex_exit(&vskstat_tree_lock);

	kmem_cache_free(vsk_anchor_cache, vskap);
}

/*
 * Read or write a vnode.  Called from kernel code.
 */
int
vn_rdwr(
	enum uio_rw rw,
	struct vnode *vp,
	caddr_t base,
	ssize_t len,
	offset_t offset,
	enum uio_seg seg,
	int ioflag,
	rlim64_t ulimit,	/* meaningful only if rw is UIO_WRITE */
	cred_t *cr,
	ssize_t *residp)
{
	struct uio uio;
	struct iovec iov;
	int error;
	int in_crit = 0;

	if (rw == UIO_WRITE && ISROFILE(vp))
		return (EROFS);

	if (len < 0)
		return (EIO);

	VOPXID_MAP_CR(vp, cr);

	iov.iov_base = base;
	iov.iov_len = len;
	uio.uio_iov = &iov;
	uio.uio_iovcnt = 1;
	uio.uio_loffset = offset;
	uio.uio_segflg = (short)seg;
	uio.uio_resid = len;
	uio.uio_llimit = ulimit;

	/*
	 * We have to enter the critical region before calling VOP_RWLOCK
	 * to avoid a deadlock with ufs.
	 */
	if (nbl_need_check(vp)) {
		int svmand;

		nbl_start_crit(vp, RW_READER);
		in_crit = 1;
		error = nbl_svmand(vp, cr, &svmand);
		if (error != 0)
			goto done;
		if (nbl_conflict(vp, rw == UIO_WRITE ? NBL_WRITE : NBL_READ,
		    uio.uio_offset, uio.uio_resid, svmand, NULL)) {
			error = EACCES;
			goto done;
		}
	}

	(void) VOP_RWLOCK(vp,
	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
	if (rw == UIO_WRITE) {
		uio.uio_fmode = FWRITE;
		uio.uio_extflg = UIO_COPY_DEFAULT;
		error = VOP_WRITE(vp, &uio, ioflag, cr, NULL);
	} else {
		uio.uio_fmode = FREAD;
		uio.uio_extflg = UIO_COPY_CACHED;
		error = VOP_READ(vp, &uio, ioflag, cr, NULL);
	}
	VOP_RWUNLOCK(vp,
	    rw == UIO_WRITE ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE, NULL);
	if (residp)
		*residp = uio.uio_resid;
	else if (uio.uio_resid)
		error = EIO;

done:
	if (in_crit)
		nbl_end_crit(vp);
	return (error);
}

/*
 * Release a vnode.  Call VOP_INACTIVE on last reference or
 * decrement reference count.
 *
 * To avoid race conditions, the v_count is left at 1 for
 * the call to VOP_INACTIVE. This prevents another thread
 * from reclaiming and releasing the vnode *before* the
 * VOP_INACTIVE routine has a chance to destroy the vnode.
 * We can't have more than 1 thread calling VOP_INACTIVE
 * on a vnode.
 */
void
vn_rele(vnode_t *vp)
{
	VERIFY(vp->v_count > 0);
	mutex_enter(&vp->v_lock);
	if (vp->v_count == 1) {
		mutex_exit(&vp->v_lock);
		VOP_INACTIVE(vp, CRED(), NULL);
		return;
	}
	vp->v_count--;
	mutex_exit(&vp->v_lock);
}

/*
 * Release a vnode referenced by the DNLC. Multiple DNLC references are treated
 * as a single reference, so v_count is not decremented until the last DNLC hold
 * is released. This makes it possible to distinguish vnodes that are referenced
 * only by the DNLC.
 */
void
vn_rele_dnlc(vnode_t *vp)
{
	VERIFY((vp->v_count > 0) && (vp->v_count_dnlc > 0));
	mutex_enter(&vp->v_lock);
	if (--vp->v_count_dnlc == 0) {
		if (vp->v_count == 1) {
			mutex_exit(&vp->v_lock);
			VOP_INACTIVE(vp, CRED(), NULL);
			return;
		}
		vp->v_count--;
	}
	mutex_exit(&vp->v_lock);
}

/*
 * Like vn_rele() except that it clears v_stream under v_lock.
 * This is used by sockfs when it dismantels the association between
 * the sockfs node and the vnode in the underlaying file system.
 * v_lock has to be held to prevent a thread coming through the lookupname
 * path from accessing a stream head that is going away.
 */
void
vn_rele_stream(vnode_t *vp)
{
	VERIFY(vp->v_count > 0);
	mutex_enter(&vp->v_lock);
	vp->v_stream = NULL;
	if (vp->v_count == 1) {
		mutex_exit(&vp->v_lock);
		VOP_INACTIVE(vp, CRED(), NULL);
		return;
	}
	vp->v_count--;
	mutex_exit(&vp->v_lock);
}

static void
vn_rele_inactive(vnode_t *vp)
{
	VOP_INACTIVE(vp, CRED(), NULL);
}

/*
 * Like vn_rele() except if we are going to call VOP_INACTIVE() then do it
 * asynchronously using a taskq. This can avoid deadlocks caused by re-entering
 * the file system as a result of releasing the vnode. Note, file systems
 * already have to handle the race where the vnode is incremented before the
 * inactive routine is called and does its locking.
 *
 * Warning: Excessive use of this routine can lead to performance problems.
 * This is because taskqs throttle back allocation if too many are created.
 */
void
vn_rele_async(vnode_t *vp, taskq_t *taskq)
{
	VERIFY(vp->v_count > 0);
	mutex_enter(&vp->v_lock);
	if (vp->v_count == 1) {
		mutex_exit(&vp->v_lock);
		VERIFY(taskq_dispatch(taskq, (task_func_t *)vn_rele_inactive,
		    vp, TQ_SLEEP) != NULL);
		return;
	}
	vp->v_count--;
	mutex_exit(&vp->v_lock);
}

int
vn_open(
	char *pnamep,
	enum uio_seg seg,
	int filemode,
	int createmode,
	struct vnode **vpp,
	enum create crwhy,
	mode_t umask)
{
	return (vn_openat(pnamep, seg, filemode, createmode, vpp, crwhy,
	    umask, NULL, -1));
}


/*
 * Open/create a vnode.
 * This may be callable by the kernel, the only known use
 * of user context being that the current user credentials
 * are used for permissions.  crwhy is defined iff filemode & FCREAT.
 */
int
vn_openat(
	char *pnamep,
	enum uio_seg seg,
	int filemode,
	int createmode,
	struct vnode **vpp,
	enum create crwhy,
	mode_t umask,
	struct vnode *startvp,
	int fd)
{
	struct vnode *vp;
	int mode;
	int accessflags;
	int error;
	int in_crit = 0;
	int open_done = 0;
	int shrlock_done = 0;
	struct vattr vattr;
	enum symfollow follow;
	int estale_retry = 0;
	struct shrlock shr;
	struct shr_locowner shr_own;

	mode = 0;
	accessflags = 0;
	if (filemode & FREAD)
		mode |= VREAD;
	if (filemode & (FWRITE|FTRUNC))
		mode |= VWRITE;
	if (filemode & (FSEARCH|FEXEC|FXATTRDIROPEN))
		mode |= VEXEC;

	/* symlink interpretation */
	if (filemode & FNOFOLLOW)
		follow = NO_FOLLOW;
	else
		follow = FOLLOW;

	if (filemode & FAPPEND)
		accessflags |= V_APPEND;

top:
	if (filemode & FCREAT) {
		enum vcexcl excl;

		/*
		 * Wish to create a file.
		 */
		vattr.va_type = VREG;
		vattr.va_mode = createmode;
		vattr.va_mask = AT_TYPE|AT_MODE;
		if (filemode & FTRUNC) {
			vattr.va_size = 0;
			vattr.va_mask |= AT_SIZE;
		}
		if (filemode & FEXCL)
			excl = EXCL;
		else
			excl = NONEXCL;

		if (error =
		    vn_createat(pnamep, seg, &vattr, excl, mode, &vp, crwhy,
		    (filemode & ~(FTRUNC|FEXCL)), umask, startvp))
			return (error);
	} else {
		/*
		 * Wish to open a file.  Just look it up.
		 */
		if (error = lookupnameat(pnamep, seg, follow,
		    NULLVPP, &vp, startvp)) {
			if ((error == ESTALE) &&
			    fs_need_estale_retry(estale_retry++))
				goto top;
			return (error);
		}

		/*
		 * Get the attributes to check whether file is large.
		 * We do this only if the FOFFMAX flag is not set and
		 * only for regular files.
		 */

		if (!(filemode & FOFFMAX) && (vp->v_type == VREG)) {
			vattr.va_mask = AT_SIZE;
			if ((error = VOP_GETATTR(vp, &vattr, 0,
			    CRED(), NULL))) {
				goto out;
			}
			if (vattr.va_size > (u_offset_t)MAXOFF32_T) {
				/*
				 * Large File API - regular open fails
				 * if FOFFMAX flag is set in file mode
				 */
				error = EOVERFLOW;
				goto out;
			}
		}
		/*
		 * Can't write directories, active texts, or
		 * read-only filesystems.  Can't truncate files
		 * on which mandatory locking is in effect.
		 */
		if (filemode & (FWRITE|FTRUNC)) {
			/*
			 * Allow writable directory if VDIROPEN flag is set.
			 */
			if (vp->v_type == VDIR && !(vp->v_flag & VDIROPEN)) {
				error = EISDIR;
				goto out;
			}
			if (ISROFILE(vp)) {
				error = EROFS;
				goto out;
			}
			/*
			 * Can't truncate files on which
			 * sysv mandatory locking is in effect.
			 */
			if (filemode & FTRUNC) {
				vnode_t *rvp;

				if (VOP_REALVP(vp, &rvp, NULL) != 0)
					rvp = vp;
				if (rvp->v_filocks != NULL) {
					vattr.va_mask = AT_MODE;
					if ((error = VOP_GETATTR(vp,
					    &vattr, 0, CRED(), NULL)) == 0 &&
					    MANDLOCK(vp, vattr.va_mode))
						error = EAGAIN;
				}
			}
			if (error)
				goto out;
		}
		/*
		 * Check permissions.
		 */
		if (error = VOP_ACCESS(vp, mode, accessflags, CRED(), NULL))
			goto out;
		/*
		 * Require FSEARCH to return a directory.
		 * Require FEXEC to return a regular file.
		 */
		if ((filemode & FSEARCH) && vp->v_type != VDIR) {
			error = ENOTDIR;
			goto out;
		}
		if ((filemode & FEXEC) && vp->v_type != VREG) {
			error = ENOEXEC;	/* XXX: error code? */
			goto out;
		}
	}

	/*
	 * Do remaining checks for FNOFOLLOW and FNOLINKS.
	 */
	if ((filemode & FNOFOLLOW) && vp->v_type == VLNK) {
		error = ELOOP;
		goto out;
	}
	if (filemode & FNOLINKS) {
		vattr.va_mask = AT_NLINK;
		if ((error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))) {
			goto out;
		}
		if (vattr.va_nlink != 1) {
			error = EMLINK;
			goto out;
		}
	}

	/*
	 * Opening a socket corresponding to the AF_UNIX pathname
	 * in the filesystem name space is not supported.
	 * However, VSOCK nodes in namefs are supported in order
	 * to make fattach work for sockets.
	 *
	 * XXX This uses VOP_REALVP to distinguish between
	 * an unopened namefs node (where VOP_REALVP returns a
	 * different VSOCK vnode) and a VSOCK created by vn_create
	 * in some file system (where VOP_REALVP would never return
	 * a different vnode).
	 */
	if (vp->v_type == VSOCK) {
		struct vnode *nvp;

		error = VOP_REALVP(vp, &nvp, NULL);
		if (error != 0 || nvp == NULL || nvp == vp ||
		    nvp->v_type != VSOCK) {
			error = EOPNOTSUPP;
			goto out;
		}
	}

	if ((vp->v_type == VREG) && nbl_need_check(vp)) {
		/* get share reservation */
		shr.s_access = 0;
		if (filemode & FWRITE)
			shr.s_access |= F_WRACC;
		if (filemode & FREAD)
			shr.s_access |= F_RDACC;
		shr.s_deny = 0;
		shr.s_sysid = 0;
		shr.s_pid = ttoproc(curthread)->p_pid;
		shr_own.sl_pid = shr.s_pid;
		shr_own.sl_id = fd;
		shr.s_own_len = sizeof (shr_own);
		shr.s_owner = (caddr_t)&shr_own;
		error = VOP_SHRLOCK(vp, F_SHARE_NBMAND, &shr, filemode, CRED(),
		    NULL);
		if (error)
			goto out;
		shrlock_done = 1;

		/* nbmand conflict check if truncating file */
		if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
			nbl_start_crit(vp, RW_READER);
			in_crit = 1;

			vattr.va_mask = AT_SIZE;
			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL))
				goto out;
			if (nbl_conflict(vp, NBL_WRITE, 0, vattr.va_size, 0,
			    NULL)) {
				error = EACCES;
				goto out;
			}
		}
	}

	/*
	 * Do opening protocol.
	 */
	error = VOP_OPEN(&vp, filemode, CRED(), NULL);
	if (error)
		goto out;
	open_done = 1;

	/*
	 * Truncate if required.
	 */
	if ((filemode & FTRUNC) && !(filemode & FCREAT)) {
		vattr.va_size = 0;
		vattr.va_mask = AT_SIZE;
		if ((error = VOP_SETATTR(vp, &vattr, 0, CRED(), NULL)) != 0)
			goto out;
	}
out:
	ASSERT(vp->v_count > 0);

	if (in_crit) {
		nbl_end_crit(vp);
		in_crit = 0;
	}
	if (error) {
		if (open_done) {
			(void) VOP_CLOSE(vp, filemode, 1, (offset_t)0, CRED(),
			    NULL);
			open_done = 0;
			shrlock_done = 0;
		}
		if (shrlock_done) {
			(void) VOP_SHRLOCK(vp, F_UNSHARE, &shr, 0, CRED(),
			    NULL);
			shrlock_done = 0;
		}

		/*
		 * The following clause was added to handle a problem
		 * with NFS consistency.  It is possible that a lookup
		 * of the file to be opened succeeded, but the file
		 * itself doesn't actually exist on the server.  This
		 * is chiefly due to the DNLC containing an entry for
		 * the file which has been removed on the server.  In
		 * this case, we just start over.  If there was some
		 * other cause for the ESTALE error, then the lookup
		 * of the file will fail and the error will be returned
		 * above instead of looping around from here.
		 */
		VN_RELE(vp);
		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
			goto top;
	} else
		*vpp = vp;
	return (error);
}

/*
 * The following two accessor functions are for the NFSv4 server.  Since there
 * is no VOP_OPEN_UP/DOWNGRADE we need a way for the NFS server to keep the
 * vnode open counts correct when a client "upgrades" an open or does an
 * open_downgrade.  In NFS, an upgrade or downgrade can not only change the
 * open mode (add or subtract read or write), but also change the share/deny
 * modes.  However, share reservations are not integrated with OPEN, yet, so
 * we need to handle each separately.  These functions are cleaner than having
 * the NFS server manipulate the counts directly, however, nobody else should
 * use these functions.
 */
void
vn_open_upgrade(
	vnode_t *vp,
	int filemode)
{
	ASSERT(vp->v_type == VREG);

	if (filemode & FREAD)
		atomic_add_32(&(vp->v_rdcnt), 1);
	if (filemode & FWRITE)
		atomic_add_32(&(vp->v_wrcnt), 1);

}

void
vn_open_downgrade(
	vnode_t *vp,
	int filemode)
{
	ASSERT(vp->v_type == VREG);

	if (filemode & FREAD) {
		ASSERT(vp->v_rdcnt > 0);
		atomic_add_32(&(vp->v_rdcnt), -1);
	}
	if (filemode & FWRITE) {
		ASSERT(vp->v_wrcnt > 0);
		atomic_add_32(&(vp->v_wrcnt), -1);
	}

}

int
vn_create(
	char *pnamep,
	enum uio_seg seg,
	struct vattr *vap,
	enum vcexcl excl,
	int mode,
	struct vnode **vpp,
	enum create why,
	int flag,
	mode_t umask)
{
	return (vn_createat(pnamep, seg, vap, excl, mode, vpp, why, flag,
	    umask, NULL));
}

/*
 * Create a vnode (makenode).
 */
int
vn_createat(
	char *pnamep,
	enum uio_seg seg,
	struct vattr *vap,
	enum vcexcl excl,
	int mode,
	struct vnode **vpp,
	enum create why,
	int flag,
	mode_t umask,
	struct vnode *startvp)
{
	struct vnode *dvp;	/* ptr to parent dir vnode */
	struct vnode *vp = NULL;
	struct pathname pn;
	int error;
	int in_crit = 0;
	struct vattr vattr;
	enum symfollow follow;
	int estale_retry = 0;
	uint32_t auditing = AU_AUDITING();

	ASSERT((vap->va_mask & (AT_TYPE|AT_MODE)) == (AT_TYPE|AT_MODE));

	/* symlink interpretation */
	if ((flag & FNOFOLLOW) || excl == EXCL)
		follow = NO_FOLLOW;
	else
		follow = FOLLOW;
	flag &= ~(FNOFOLLOW|FNOLINKS);

top:
	/*
	 * Lookup directory.
	 * If new object is a file, call lower level to create it.
	 * Note that it is up to the lower level to enforce exclusive
	 * creation, if the file is already there.
	 * This allows the lower level to do whatever
	 * locking or protocol that is needed to prevent races.
	 * If the new object is directory call lower level to make
	 * the new directory, with "." and "..".
	 */
	if (error = pn_get(pnamep, seg, &pn))
		return (error);
	if (auditing)
		audit_vncreate_start();
	dvp = NULL;
	*vpp = NULL;
	/*
	 * lookup will find the parent directory for the vnode.
	 * When it is done the pn holds the name of the entry
	 * in the directory.
	 * If this is a non-exclusive create we also find the node itself.
	 */
	error = lookuppnat(&pn, NULL, follow, &dvp,
	    (excl == EXCL) ? NULLVPP : vpp, startvp);
	if (error) {
		pn_free(&pn);
		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
			goto top;
		if (why == CRMKDIR && error == EINVAL)
			error = EEXIST;		/* SVID */
		return (error);
	}

	if (why != CRMKNOD)
		vap->va_mode &= ~VSVTX;

	/*
	 * If default ACLs are defined for the directory don't apply the
	 * umask if umask is passed.
	 */

	if (umask) {

		vsecattr_t vsec;

		vsec.vsa_aclcnt = 0;
		vsec.vsa_aclentp = NULL;
		vsec.vsa_dfaclcnt = 0;
		vsec.vsa_dfaclentp = NULL;
		vsec.vsa_mask = VSA_DFACLCNT;
		error = VOP_GETSECATTR(dvp, &vsec, 0, CRED(), NULL);
		/*
		 * If error is ENOSYS then treat it as no error
		 * Don't want to force all file systems to support
		 * aclent_t style of ACL's.
		 */
		if (error == ENOSYS)
			error = 0;
		if (error) {
			if (*vpp != NULL)
				VN_RELE(*vpp);
			goto out;
		} else {
			/*
			 * Apply the umask if no default ACLs.
			 */
			if (vsec.vsa_dfaclcnt == 0)
				vap->va_mode &= ~umask;

			/*
			 * VOP_GETSECATTR() may have allocated memory for
			 * ACLs we didn't request, so double-check and
			 * free it if necessary.
			 */
			if (vsec.vsa_aclcnt && vsec.vsa_aclentp != NULL)
				kmem_free((caddr_t)vsec.vsa_aclentp,
				    vsec.vsa_aclcnt * sizeof (aclent_t));
			if (vsec.vsa_dfaclcnt && vsec.vsa_dfaclentp != NULL)
				kmem_free((caddr_t)vsec.vsa_dfaclentp,
				    vsec.vsa_dfaclcnt * sizeof (aclent_t));
		}
	}

	/*
	 * In general we want to generate EROFS if the file system is
	 * readonly.  However, POSIX (IEEE Std. 1003.1) section 5.3.1
	 * documents the open system call, and it says that O_CREAT has no
	 * effect if the file already exists.  Bug 1119649 states
	 * that open(path, O_CREAT, ...) fails when attempting to open an
	 * existing file on a read only file system.  Thus, the first part
	 * of the following if statement has 3 checks:
	 *	if the file exists &&
	 *		it is being open with write access &&
	 *		the file system is read only
	 *	then generate EROFS
	 */
	if ((*vpp != NULL && (mode & VWRITE) && ISROFILE(*vpp)) ||
	    (*vpp == NULL && dvp->v_vfsp->vfs_flag & VFS_RDONLY)) {
		if (*vpp)
			VN_RELE(*vpp);
		error = EROFS;
	} else if (excl == NONEXCL && *vpp != NULL) {
		vnode_t *rvp;

		/*
		 * File already exists.  If a mandatory lock has been
		 * applied, return error.
		 */
		vp = *vpp;
		if (VOP_REALVP(vp, &rvp, NULL) != 0)
			rvp = vp;
		if ((vap->va_mask & AT_SIZE) && nbl_need_check(vp)) {
			nbl_start_crit(vp, RW_READER);
			in_crit = 1;
		}
		if (rvp->v_filocks != NULL || rvp->v_shrlocks != NULL) {
			vattr.va_mask = AT_MODE|AT_SIZE;
			if (error = VOP_GETATTR(vp, &vattr, 0, CRED(), NULL)) {
				goto out;
			}
			if (MANDLOCK(vp, vattr.va_mode)) {
				error = EAGAIN;
				goto out;
			}
			/*
			 * File cannot be truncated if non-blocking mandatory
			 * locks are currently on the file.
			 */
			if ((vap->va_mask & AT_SIZE) && in_crit) {
				u_offset_t offset;
				ssize_t length;

				offset = vap->va_size > vattr.va_size ?
				    vattr.va_size : vap->va_size;
				length = vap->va_size > vattr.va_size ?
				    vap->va_size - vattr.va_size :
				    vattr.va_size - vap->va_size;
				if (nbl_conflict(vp, NBL_WRITE, offset,
				    length, 0, NULL)) {
					error = EACCES;
					goto out;
				}
			}
		}

		/*
		 * If the file is the root of a VFS, we've crossed a
		 * mount point and the "containing" directory that we
		 * acquired above (dvp) is irrelevant because it's in
		 * a different file system.  We apply VOP_CREATE to the
		 * target itself instead of to the containing directory
		 * and supply a null path name to indicate (conventionally)
		 * the node itself as the "component" of interest.
		 *
		 * The intercession of the file system is necessary to
		 * ensure that the appropriate permission checks are
		 * done.
		 */
		if (vp->v_flag & VROOT) {
			ASSERT(why != CRMKDIR);
			error = VOP_CREATE(vp, "", vap, excl, mode, vpp,
			    CRED(), flag, NULL, NULL);
			/*
			 * If the create succeeded, it will have created
			 * a new reference to the vnode.  Give up the
			 * original reference.  The assertion should not
			 * get triggered because NBMAND locks only apply to
			 * VREG files.  And if in_crit is non-zero for some
			 * reason, detect that here, rather than when we
			 * deference a null vp.
			 */
			ASSERT(in_crit == 0);
			VN_RELE(vp);
			vp = NULL;
			goto out;
		}

		/*
		 * Large File API - non-large open (FOFFMAX flag not set)
		 * of regular file fails if the file size exceeds MAXOFF32_T.
		 */
		if (why != CRMKDIR &&
		    !(flag & FOFFMAX) &&
		    (vp->v_type == VREG)) {
			vattr.va_mask = AT_SIZE;
			if ((error = VOP_GETATTR(vp, &vattr, 0,
			    CRED(), NULL))) {
				goto out;
			}
			if ((vattr.va_size > (u_offset_t)MAXOFF32_T)) {
				error = EOVERFLOW;
				goto out;
			}
		}
	}

	if (error == 0) {
		/*
		 * Call mkdir() if specified, otherwise create().
		 */
		int must_be_dir = pn_fixslash(&pn);	/* trailing '/'? */

		if (why == CRMKDIR)
			/*
			 * N.B., if vn_createat() ever requests
			 * case-insensitive behavior then it will need
			 * to be passed to VOP_MKDIR().  VOP_CREATE()
			 * will already get it via "flag"
			 */
			error = VOP_MKDIR(dvp, pn.pn_path, vap, vpp, CRED(),
			    NULL, 0, NULL);
		else if (!must_be_dir)
			error = VOP_CREATE(dvp, pn.pn_path, vap,
			    excl, mode, vpp, CRED(), flag, NULL, NULL);
		else
			error = ENOTDIR;
	}

out:

	if (auditing)
		audit_vncreate_finish(*vpp, error);
	if (in_crit) {
		nbl_end_crit(vp);
		in_crit = 0;
	}
	if (vp != NULL) {
		VN_RELE(vp);
		vp = NULL;
	}
	pn_free(&pn);
	VN_RELE(dvp);
	/*
	 * The following clause was added to handle a problem
	 * with NFS consistency.  It is possible that a lookup
	 * of the file to be created succeeded, but the file
	 * itself doesn't actually exist on the server.  This
	 * is chiefly due to the DNLC containing an entry for
	 * the file which has been removed on the server.  In
	 * this case, we just start over.  If there was some
	 * other cause for the ESTALE error, then the lookup
	 * of the file will fail and the error will be returned
	 * above instead of looping around from here.
	 */
	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
		goto top;
	return (error);
}

int
vn_link(char *from, char *to, enum uio_seg seg)
{
	return (vn_linkat(NULL, from, NO_FOLLOW, NULL, to, seg));
}

int
vn_linkat(vnode_t *fstartvp, char *from, enum symfollow follow,
    vnode_t *tstartvp, char *to, enum uio_seg seg)
{
	struct vnode *fvp;		/* from vnode ptr */
	struct vnode *tdvp;		/* to directory vnode ptr */
	struct pathname pn;
	int error;
	struct vattr vattr;
	dev_t fsid;
	int estale_retry = 0;
	uint32_t auditing = AU_AUDITING();

top:
	fvp = tdvp = NULL;
	if (error = pn_get(to, seg, &pn))
		return (error);
	if (auditing && fstartvp != NULL)
		audit_setfsat_path(1);
	if (error = lookupnameat(from, seg, follow, NULLVPP, &fvp, fstartvp))
		goto out;
	if (auditing && tstartvp != NULL)
		audit_setfsat_path(3);
	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &tdvp, NULLVPP, tstartvp))
		goto out;
	/*
	 * Make sure both source vnode and target directory vnode are
	 * in the same vfs and that it is writeable.
	 */
	vattr.va_mask = AT_FSID;
	if (error = VOP_GETATTR(fvp, &vattr, 0, CRED(), NULL))
		goto out;
	fsid = vattr.va_fsid;
	vattr.va_mask = AT_FSID;
	if (error = VOP_GETATTR(tdvp, &vattr, 0, CRED(), NULL))
		goto out;
	if (fsid != vattr.va_fsid) {
		error = EXDEV;
		goto out;
	}
	if (tdvp->v_vfsp->vfs_flag & VFS_RDONLY) {
		error = EROFS;
		goto out;
	}
	/*
	 * Do the link.
	 */
	(void) pn_fixslash(&pn);
	error = VOP_LINK(tdvp, fvp, pn.pn_path, CRED(), NULL, 0);
out:
	pn_free(&pn);
	if (fvp)
		VN_RELE(fvp);
	if (tdvp)
		VN_RELE(tdvp);
	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
		goto top;
	return (error);
}

int
vn_rename(char *from, char *to, enum uio_seg seg)
{
	return (vn_renameat(NULL, from, NULL, to, seg));
}

int
vn_renameat(vnode_t *fdvp, char *fname, vnode_t *tdvp,
		char *tname, enum uio_seg seg)
{
	int error;
	struct vattr vattr;
	struct pathname fpn;		/* from pathname */
	struct pathname tpn;		/* to pathname */
	dev_t fsid;
	int in_crit_src, in_crit_targ;
	vnode_t *fromvp, *fvp;
	vnode_t *tovp, *targvp;
	int estale_retry = 0;
	uint32_t auditing = AU_AUDITING();

top:
	fvp = fromvp = tovp = targvp = NULL;
	in_crit_src = in_crit_targ = 0;
	/*
	 * Get to and from pathnames.
	 */
	if (error = pn_get(fname, seg, &fpn))
		return (error);
	if (error = pn_get(tname, seg, &tpn)) {
		pn_free(&fpn);
		return (error);
	}

	/*
	 * First we need to resolve the correct directories
	 * The passed in directories may only be a starting point,
	 * but we need the real directories the file(s) live in.
	 * For example the fname may be something like usr/lib/sparc
	 * and we were passed in the / directory, but we need to
	 * use the lib directory for the rename.
	 */

	if (auditing && fdvp != NULL)
		audit_setfsat_path(1);
	/*
	 * Lookup to and from directories.
	 */
	if (error = lookuppnat(&fpn, NULL, NO_FOLLOW, &fromvp, &fvp, fdvp)) {
		goto out;
	}

	/*
	 * Make sure there is an entry.
	 */
	if (fvp == NULL) {
		error = ENOENT;
		goto out;
	}

	if (auditing && tdvp != NULL)
		audit_setfsat_path(3);
	if (error = lookuppnat(&tpn, NULL, NO_FOLLOW, &tovp, &targvp, tdvp)) {
		goto out;
	}

	/*
	 * Make sure both the from vnode directory and the to directory
	 * are in the same vfs and the to directory is writable.
	 * We check fsid's, not vfs pointers, so loopback fs works.
	 */
	if (fromvp != tovp) {
		vattr.va_mask = AT_FSID;
		if (error = VOP_GETATTR(fromvp, &vattr, 0, CRED(), NULL))
			goto out;
		fsid = vattr.va_fsid;
		vattr.va_mask = AT_FSID;
		if (error = VOP_GETATTR(tovp, &vattr, 0, CRED(), NULL))
			goto out;
		if (fsid != vattr.va_fsid) {
			error = EXDEV;
			goto out;
		}
	}

	if (tovp->v_vfsp->vfs_flag & VFS_RDONLY) {
		error = EROFS;
		goto out;
	}

	if (targvp && (fvp != targvp)) {
		nbl_start_crit(targvp, RW_READER);
		in_crit_targ = 1;
		if (nbl_conflict(targvp, NBL_REMOVE, 0, 0, 0, NULL)) {
			error = EACCES;
			goto out;
		}
	}

	if (nbl_need_check(fvp)) {
		nbl_start_crit(fvp, RW_READER);
		in_crit_src = 1;
		if (nbl_conflict(fvp, NBL_RENAME, 0, 0, 0, NULL)) {
			error = EACCES;
			goto out;
		}
	}

	/*
	 * Do the rename.
	 */
	(void) pn_fixslash(&tpn);
	error = VOP_RENAME(fromvp, fpn.pn_path, tovp, tpn.pn_path, CRED(),
	    NULL, 0);

out:
	pn_free(&fpn);
	pn_free(&tpn);
	if (in_crit_src)
		nbl_end_crit(fvp);
	if (in_crit_targ)
		nbl_end_crit(targvp);
	if (fromvp)
		VN_RELE(fromvp);
	if (tovp)
		VN_RELE(tovp);
	if (targvp)
		VN_RELE(targvp);
	if (fvp)
		VN_RELE(fvp);
	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
		goto top;
	return (error);
}

/*
 * Remove a file or directory.
 */
int
vn_remove(char *fnamep, enum uio_seg seg, enum rm dirflag)
{
	return (vn_removeat(NULL, fnamep, seg, dirflag));
}

int
vn_removeat(vnode_t *startvp, char *fnamep, enum uio_seg seg, enum rm dirflag)
{
	struct vnode *vp;		/* entry vnode */
	struct vnode *dvp;		/* ptr to parent dir vnode */
	struct vnode *coveredvp;
	struct pathname pn;		/* name of entry */
	enum vtype vtype;
	int error;
	struct vfs *vfsp;
	struct vfs *dvfsp;	/* ptr to parent dir vfs */
	int in_crit = 0;
	int estale_retry = 0;

top:
	if (error = pn_get(fnamep, seg, &pn))
		return (error);
	dvp = vp = NULL;
	if (error = lookuppnat(&pn, NULL, NO_FOLLOW, &dvp, &vp, startvp)) {
		pn_free(&pn);
		if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
			goto top;
		return (error);
	}

	/*
	 * Make sure there is an entry.
	 */
	if (vp == NULL) {
		error = ENOENT;
		goto out;
	}

	vfsp = vp->v_vfsp;
	dvfsp = dvp->v_vfsp;

	/*
	 * If the named file is the root of a mounted filesystem, fail,
	 * unless it's marked unlinkable.  In that case, unmount the
	 * filesystem and proceed to unlink the covered vnode.  (If the
	 * covered vnode is a directory, use rmdir instead of unlink,
	 * to avoid file system corruption.)
	 */
	if (vp->v_flag & VROOT) {
		if ((vfsp->vfs_flag & VFS_UNLINKABLE) == 0) {
			error = EBUSY;
			goto out;
		}

		/*
		 * Namefs specific code starts here.
		 */

		if (dirflag == RMDIRECTORY) {
			/*
			 * User called rmdir(2) on a file that has
			 * been namefs mounted on top of.  Since
			 * namefs doesn't allow directories to
			 * be mounted on other files we know
			 * vp is not of type VDIR so fail to operation.
			 */
			error = ENOTDIR;
			goto out;
		}

		/*
		 * If VROOT is still set after grabbing vp->v_lock,
		 * noone has finished nm_unmount so far and coveredvp
		 * is valid.
		 * If we manage to grab vn_vfswlock(coveredvp) before releasing
		 * vp->v_lock, any race window is eliminated.
		 */

		mutex_enter(&vp->v_lock);
		if ((vp->v_flag & VROOT) == 0) {
			/* Someone beat us to the unmount */
			mutex_exit(&vp->v_lock);
			error = EBUSY;
			goto out;
		}
		vfsp = vp->v_vfsp;
		coveredvp = vfsp->vfs_vnodecovered;
		ASSERT(coveredvp);
		/*
		 * Note: Implementation of vn_vfswlock shows that ordering of
		 * v_lock / vn_vfswlock is not an issue here.
		 */
		error = vn_vfswlock(coveredvp);
		mutex_exit(&vp->v_lock);

		if (error)
			goto out;

		VN_HOLD(coveredvp);
		VN_RELE(vp);
		error = dounmount(vfsp, 0, CRED());

		/*
		 * Unmounted the namefs file system; now get
		 * the object it was mounted over.
		 */
		vp = coveredvp;
		/*
		 * If namefs was mounted over a directory, then
		 * we want to use rmdir() instead of unlink().
		 */
		if (vp->v_type == VDIR)
			dirflag = RMDIRECTORY;

		if (error)
			goto out;
	}

	/*
	 * Make sure filesystem is writeable.
	 * We check the parent directory's vfs in case this is an lofs vnode.
	 */
	if (dvfsp && dvfsp->vfs_flag & VFS_RDONLY) {
		error = EROFS;
		goto out;
	}

	vtype = vp->v_type;

	/*
	 * If there is the possibility of an nbmand share reservation, make
	 * sure it's okay to remove the file.  Keep a reference to the
	 * vnode, so that we can exit the nbl critical region after
	 * calling VOP_REMOVE.
	 * If there is no possibility of an nbmand share reservation,
	 * release the vnode reference now.  Filesystems like NFS may
	 * behave differently if there is an extra reference, so get rid of
	 * this one.  Fortunately, we can't have nbmand mounts on NFS
	 * filesystems.
	 */
	if (nbl_need_check(vp)) {
		nbl_start_crit(vp, RW_READER);
		in_crit = 1;
		if (nbl_conflict(vp, NBL_REMOVE, 0, 0, 0, NULL)) {
			error = EACCES;
			goto out;
		}
	} else {
		VN_RELE(vp);
		vp = NULL;
	}

	if (dirflag == RMDIRECTORY) {
		/*
		 * Caller is using rmdir(2), which can only be applied to
		 * directories.
		 */
		if (vtype != VDIR) {
			error = ENOTDIR;
		} else {
			vnode_t *cwd;
			proc_t *pp = curproc;

			mutex_enter(&pp->p_lock);
			cwd = PTOU(pp)->u_cdir;
			VN_HOLD(cwd);
			mutex_exit(&pp->p_lock);
			error = VOP_RMDIR(dvp, pn.pn_path, cwd, CRED(),
			    NULL, 0);
			VN_RELE(cwd);
		}
	} else {
		/*
		 * Unlink(2) can be applied to anything.
		 */
		error = VOP_REMOVE(dvp, pn.pn_path, CRED(), NULL, 0);
	}

out:
	pn_free(&pn);
	if (in_crit) {
		nbl_end_crit(vp);
		in_crit = 0;
	}
	if (vp != NULL)
		VN_RELE(vp);
	if (dvp != NULL)
		VN_RELE(dvp);
	if ((error == ESTALE) && fs_need_estale_retry(estale_retry++))
		goto top;
	return (error);
}

/*
 * Utility function to compare equality of vnodes.
 * Compare the underlying real vnodes, if there are underlying vnodes.
 * This is a more thorough comparison than the VN_CMP() macro provides.
 */
int
vn_compare(vnode_t *vp1, vnode_t *vp2)
{
	vnode_t *realvp;

	if (vp1 != NULL && VOP_REALVP(vp1, &realvp, NULL) == 0)
		vp1 = realvp;
	if (vp2 != NULL && VOP_REALVP(vp2, &realvp, NULL) == 0)
		vp2 = realvp;
	return (VN_CMP(vp1, vp2));
}

/*
 * The number of locks to hash into.  This value must be a power
 * of 2 minus 1 and should probably also be prime.
 */
#define	NUM_BUCKETS	1023

struct  vn_vfslocks_bucket {
	kmutex_t vb_lock;
	vn_vfslocks_entry_t *vb_list;
	char pad[64 - sizeof (kmutex_t) - sizeof (void *)];
};

/*
 * Total number of buckets will be NUM_BUCKETS + 1 .
 */

#pragma	align	64(vn_vfslocks_buckets)
static	struct vn_vfslocks_bucket	vn_vfslocks_buckets[NUM_BUCKETS + 1];

#define	VN_VFSLOCKS_SHIFT	9

#define	VN_VFSLOCKS_HASH(vfsvpptr)	\
	((((intptr_t)(vfsvpptr)) >> VN_VFSLOCKS_SHIFT) & NUM_BUCKETS)

/*
 * vn_vfslocks_getlock() uses an HASH scheme to generate
 * rwstlock using vfs/vnode pointer passed to it.
 *
 * vn_vfslocks_rele() releases a reference in the
 * HASH table which allows the entry allocated by
 * vn_vfslocks_getlock() to be freed at a later
 * stage when the refcount drops to zero.
 */

vn_vfslocks_entry_t *
vn_vfslocks_getlock(void *vfsvpptr)
{
	struct vn_vfslocks_bucket *bp;
	vn_vfslocks_entry_t *vep;
	vn_vfslocks_entry_t *tvep;

	ASSERT(vfsvpptr != NULL);
	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vfsvpptr)];

	mutex_enter(&bp->vb_lock);
	for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
		if (vep->ve_vpvfs == vfsvpptr) {
			vep->ve_refcnt++;
			mutex_exit(&bp->vb_lock);
			return (vep);
		}
	}
	mutex_exit(&bp->vb_lock);
	vep = kmem_alloc(sizeof (*vep), KM_SLEEP);
	rwst_init(&vep->ve_lock, NULL, RW_DEFAULT, NULL);
	vep->ve_vpvfs = (char *)vfsvpptr;
	vep->ve_refcnt = 1;
	mutex_enter(&bp->vb_lock);
	for (tvep = bp->vb_list; tvep != NULL; tvep = tvep->ve_next) {
		if (tvep->ve_vpvfs == vfsvpptr) {
			tvep->ve_refcnt++;
			mutex_exit(&bp->vb_lock);

			/*
			 * There is already an entry in the hash
			 * destroy what we just allocated.
			 */
			rwst_destroy(&vep->ve_lock);
			kmem_free(vep, sizeof (*vep));
			return (tvep);
		}
	}
	vep->ve_next = bp->vb_list;
	bp->vb_list = vep;
	mutex_exit(&bp->vb_lock);
	return (vep);
}

void
vn_vfslocks_rele(vn_vfslocks_entry_t *vepent)
{
	struct vn_vfslocks_bucket *bp;
	vn_vfslocks_entry_t *vep;
	vn_vfslocks_entry_t *pvep;

	ASSERT(vepent != NULL);
	ASSERT(vepent->ve_vpvfs != NULL);

	bp = &vn_vfslocks_buckets[VN_VFSLOCKS_HASH(vepent->ve_vpvfs)];

	mutex_enter(&bp->vb_lock);
	vepent->ve_refcnt--;

	if ((int32_t)vepent->ve_refcnt < 0)
		cmn_err(CE_PANIC, "vn_vfslocks_rele: refcount negative");

	if (vepent->ve_refcnt == 0) {
		for (vep = bp->vb_list; vep != NULL; vep = vep->ve_next) {
			if (vep->ve_vpvfs == vepent->ve_vpvfs) {
				if (bp->vb_list == vep)
					bp->vb_list = vep->ve_next;
				else {
					/* LINTED */
					pvep->ve_next = vep->ve_next;
				}
				mutex_exit(&bp->vb_lock);
				rwst_destroy(&vep->ve_lock);
				kmem_free(vep, sizeof (*vep));
				return;
			}
			pvep = vep;
		}
		cmn_err(CE_PANIC, "vn_vfslocks_rele: vp/vfs not found");
	}
	mutex_exit(&bp->vb_lock);
}

/*
 * vn_vfswlock_wait is used to implement a lock which is logically a writers
 * lock protecting the v_vfsmountedhere field.
 * vn_vfswlock_wait has been modified to be similar to vn_vfswlock,
 * except that it blocks to acquire the lock VVFSLOCK.
 *
 * traverse() and routines re-implementing part of traverse (e.g. autofs)
 * need to hold this lock. mount(), vn_rename(), vn_remove() and so on
 * need the non-blocking version of the writers lock i.e. vn_vfswlock
 */
int
vn_vfswlock_wait(vnode_t *vp)
{
	int retval;
	vn_vfslocks_entry_t *vpvfsentry;
	ASSERT(vp != NULL);

	vpvfsentry = vn_vfslocks_getlock(vp);
	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_WRITER);

	if (retval == EINTR) {
		vn_vfslocks_rele(vpvfsentry);
		return (EINTR);
	}
	return (retval);
}

int
vn_vfsrlock_wait(vnode_t *vp)
{
	int retval;
	vn_vfslocks_entry_t *vpvfsentry;
	ASSERT(vp != NULL);

	vpvfsentry = vn_vfslocks_getlock(vp);
	retval = rwst_enter_sig(&vpvfsentry->ve_lock, RW_READER);

	if (retval == EINTR) {
		vn_vfslocks_rele(vpvfsentry);
		return (EINTR);
	}

	return (retval);
}


/*
 * vn_vfswlock is used to implement a lock which is logically a writers lock
 * protecting the v_vfsmountedhere field.
 */
int
vn_vfswlock(vnode_t *vp)
{
	vn_vfslocks_entry_t *vpvfsentry;

	/*
	 * If vp is NULL then somebody is trying to lock the covered vnode
	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
	 * only happen when unmounting /.  Since that operation will fail
	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
	 */
	if (vp == NULL)
		return (EBUSY);

	vpvfsentry = vn_vfslocks_getlock(vp);

	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_WRITER))
		return (0);

	vn_vfslocks_rele(vpvfsentry);
	return (EBUSY);
}

int
vn_vfsrlock(vnode_t *vp)
{
	vn_vfslocks_entry_t *vpvfsentry;

	/*
	 * If vp is NULL then somebody is trying to lock the covered vnode
	 * of /.  (vfs_vnodecovered is NULL for /).  This situation will
	 * only happen when unmounting /.  Since that operation will fail
	 * anyway, return EBUSY here instead of in VFS_UNMOUNT.
	 */
	if (vp == NULL)
		return (EBUSY);

	vpvfsentry = vn_vfslocks_getlock(vp);

	if (rwst_tryenter(&vpvfsentry->ve_lock, RW_READER))
		return (0);

	vn_vfslocks_rele(vpvfsentry);
	return (EBUSY);
}

void
vn_vfsunlock(vnode_t *vp)
{
	vn_vfslocks_entry_t *vpvfsentry;

	/*
	 * ve_refcnt needs to be decremented twice.
	 * 1. To release refernce after a call to vn_vfslocks_getlock()
	 * 2. To release the reference from the locking routines like
	 *    vn_vfsrlock/vn_vfswlock etc,.
	 */
	vpvfsentry = vn_vfslocks_getlock(vp);
	vn_vfslocks_rele(vpvfsentry);

	rwst_exit(&vpvfsentry->ve_lock);
	vn_vfslocks_rele(vpvfsentry);
}

int
vn_vfswlock_held(vnode_t *vp)
{
	int held;
	vn_vfslocks_entry_t *vpvfsentry;

	ASSERT(vp != NULL);

	vpvfsentry = vn_vfslocks_getlock(vp);
	held = rwst_lock_held(&vpvfsentry->ve_lock, RW_WRITER);

	vn_vfslocks_rele(vpvfsentry);
	return (held);
}


int
vn_make_ops(
	const char *name,			/* Name of file system */
	const fs_operation_def_t *templ,	/* Operation specification */
	vnodeops_t **actual)			/* Return the vnodeops */
{
	int unused_ops;
	int error;

	*actual = (vnodeops_t *)kmem_alloc(sizeof (vnodeops_t), KM_SLEEP);

	(*actual)->vnop_name = name;

	error = fs_build_vector(*actual, &unused_ops, vn_ops_table, templ);
	if (error) {
		kmem_free(*actual, sizeof (vnodeops_t));
	}

#if DEBUG
	if (unused_ops != 0)
		cmn_err(CE_WARN, "vn_make_ops: %s: %d operations supplied "
		    "but not used", name, unused_ops);
#endif

	return (error);
}

/*
 * Free the vnodeops created as a result of vn_make_ops()
 */
void
vn_freevnodeops(vnodeops_t *vnops)
{
	kmem_free(vnops, sizeof (vnodeops_t));
}

/*
 * Vnode cache.
 */

/* ARGSUSED */
static int
vn_cache_constructor(void *buf, void *cdrarg, int kmflags)
{
	struct vnode *vp;

	vp = buf;

	mutex_init(&vp->v_lock, NULL, MUTEX_DEFAULT, NULL);
	mutex_init(&vp->v_vsd_lock, NULL, MUTEX_DEFAULT, NULL);
	cv_init(&vp->v_cv, NULL, CV_DEFAULT, NULL);
	rw_init(&vp->v_nbllock, NULL, RW_DEFAULT, NULL);
	vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
	vp->v_path = NULL;
	vp->v_mpssdata = NULL;
	vp->v_vsd = NULL;
	vp->v_fopdata = NULL;

	return (0);
}

/* ARGSUSED */
static void
vn_cache_destructor(void *buf, void *cdrarg)
{
	struct vnode *vp;

	vp = buf;

	rw_destroy(&vp->v_nbllock);
	cv_destroy(&vp->v_cv);
	mutex_destroy(&vp->v_vsd_lock);
	mutex_destroy(&vp->v_lock);
}

void
vn_create_cache(void)
{
	/* LINTED */
	ASSERT((1 << VNODE_ALIGN_LOG2) ==
	    P2ROUNDUP(sizeof (struct vnode), VNODE_ALIGN));
	vn_cache = kmem_cache_create("vn_cache", sizeof (struct vnode),
	    VNODE_ALIGN, vn_cache_constructor, vn_cache_destructor, NULL, NULL,
	    NULL, 0);
}

void
vn_destroy_cache(void)
{
	kmem_cache_destroy(vn_cache);
}

/*
 * Used by file systems when fs-specific nodes (e.g., ufs inodes) are
 * cached by the file system and vnodes remain associated.
 */
void
vn_recycle(vnode_t *vp)
{
	ASSERT(vp->v_pages == NULL);

	/*
	 * XXX - This really belongs in vn_reinit(), but we have some issues
	 * with the counts.  Best to have it here for clean initialization.
	 */
	vp->v_rdcnt = 0;
	vp->v_wrcnt = 0;
	vp->v_mmap_read = 0;
	vp->v_mmap_write = 0;

	/*
	 * If FEM was in use, make sure everything gets cleaned up
	 * NOTE: vp->v_femhead is initialized to NULL in the vnode
	 * constructor.
	 */
	if (vp->v_femhead) {
		/* XXX - There should be a free_femhead() that does all this */
		ASSERT(vp->v_femhead->femh_list == NULL);
		mutex_destroy(&vp->v_femhead->femh_lock);
		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
		vp->v_femhead = NULL;
	}
	if (vp->v_path) {
		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
		vp->v_path = NULL;
	}

	if (vp->v_fopdata != NULL) {
		free_fopdata(vp);
	}
	vp->v_mpssdata = NULL;
	vsd_free(vp);
}

/*
 * Used to reset the vnode fields including those that are directly accessible
 * as well as those which require an accessor function.
 *
 * Does not initialize:
 *	synchronization objects: v_lock, v_vsd_lock, v_nbllock, v_cv
 *	v_data (since FS-nodes and vnodes point to each other and should
 *		be updated simultaneously)
 *	v_op (in case someone needs to make a VOP call on this object)
 */
void
vn_reinit(vnode_t *vp)
{
	vp->v_count = 1;
	vp->v_count_dnlc = 0;
	vp->v_vfsp = NULL;
	vp->v_stream = NULL;
	vp->v_vfsmountedhere = NULL;
	vp->v_flag = 0;
	vp->v_type = VNON;
	vp->v_rdev = NODEV;

	vp->v_filocks = NULL;
	vp->v_shrlocks = NULL;
	vp->v_pages = NULL;

	vp->v_locality = NULL;
	vp->v_xattrdir = NULL;

	/* Handles v_femhead, v_path, and the r/w/map counts */
	vn_recycle(vp);
}

vnode_t *
vn_alloc(int kmflag)
{
	vnode_t *vp;

	vp = kmem_cache_alloc(vn_cache, kmflag);

	if (vp != NULL) {
		vp->v_femhead = NULL;	/* Must be done before vn_reinit() */
		vp->v_fopdata = NULL;
		vn_reinit(vp);
	}

	return (vp);
}

void
vn_free(vnode_t *vp)
{
	ASSERT(vp->v_shrlocks == NULL);
	ASSERT(vp->v_filocks == NULL);

	/*
	 * Some file systems call vn_free() with v_count of zero,
	 * some with v_count of 1.  In any case, the value should
	 * never be anything else.
	 */
	ASSERT((vp->v_count == 0) || (vp->v_count == 1));
	ASSERT(vp->v_count_dnlc == 0);
	if (vp->v_path != NULL) {
		kmem_free(vp->v_path, strlen(vp->v_path) + 1);
		vp->v_path = NULL;
	}

	/* If FEM was in use, make sure everything gets cleaned up */
	if (vp->v_femhead) {
		/* XXX - There should be a free_femhead() that does all this */
		ASSERT(vp->v_femhead->femh_list == NULL);
		mutex_destroy(&vp->v_femhead->femh_lock);
		kmem_free(vp->v_femhead, sizeof (*(vp->v_femhead)));
		vp->v_femhead = NULL;
	}

	if (vp->v_fopdata != NULL) {
		free_fopdata(vp);
	}
	vp->v_mpssdata = NULL;
	vsd_free(vp);
	kmem_cache_free(vn_cache, vp);
}

/*
 * vnode status changes, should define better states than 1, 0.
 */
void
vn_reclaim(vnode_t *vp)
{
	vfs_t   *vfsp = vp->v_vfsp;

	if (vfsp == NULL ||
	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
		return;
	}
	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_RECLAIMED);
}

void
vn_idle(vnode_t *vp)
{
	vfs_t   *vfsp = vp->v_vfsp;

	if (vfsp == NULL ||
	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
		return;
	}
	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_IDLED);
}
void
vn_exists(vnode_t *vp)
{
	vfs_t   *vfsp = vp->v_vfsp;

	if (vfsp == NULL ||
	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
		return;
	}
	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_EXISTS);
}

void
vn_invalid(vnode_t *vp)
{
	vfs_t   *vfsp = vp->v_vfsp;

	if (vfsp == NULL ||
	    vfsp->vfs_implp == NULL || vfsp->vfs_femhead == NULL) {
		return;
	}
	(void) VFS_VNSTATE(vfsp, vp, VNTRANS_DESTROYED);
}

/* Vnode event notification */

int
vnevent_support(vnode_t *vp, caller_context_t *ct)
{
	if (vp == NULL)
		return (EINVAL);

	return (VOP_VNEVENT(vp, VE_SUPPORT, NULL, NULL, ct));
}

void
vnevent_rename_src(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_RENAME_SRC, dvp, name, ct);
}

void
vnevent_rename_dest(vnode_t *vp, vnode_t *dvp, char *name,
    caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_RENAME_DEST, dvp, name, ct);
}

void
vnevent_rename_dest_dir(vnode_t *vp, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_RENAME_DEST_DIR, NULL, NULL, ct);
}

void
vnevent_remove(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_REMOVE, dvp, name, ct);
}

void
vnevent_rmdir(vnode_t *vp, vnode_t *dvp, char *name, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_RMDIR, dvp, name, ct);
}

void
vnevent_create(vnode_t *vp, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_CREATE, NULL, NULL, ct);
}

void
vnevent_link(vnode_t *vp, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_LINK, NULL, NULL, ct);
}

void
vnevent_mountedover(vnode_t *vp, caller_context_t *ct)
{
	if (vp == NULL || vp->v_femhead == NULL) {
		return;
	}
	(void) VOP_VNEVENT(vp, VE_MOUNTEDOVER, NULL, NULL, ct);
}

/*
 * Vnode accessors.
 */

int
vn_is_readonly(vnode_t *vp)
{
	return (vp->v_vfsp->vfs_flag & VFS_RDONLY);
}

int
vn_has_flocks(vnode_t *vp)
{
	return (vp->v_filocks != NULL);
}

int
vn_has_mandatory_locks(vnode_t *vp, int mode)
{
	return ((vp->v_filocks != NULL) && (MANDLOCK(vp, mode)));
}

int
vn_has_cached_data(vnode_t *vp)
{
	return (vp->v_pages != NULL);
}

/*
 * Return 0 if the vnode in question shouldn't be permitted into a zone via
 * zone_enter(2).
 */
int
vn_can_change_zones(vnode_t *vp)
{
	struct vfssw *vswp;
	int allow = 1;
	vnode_t *rvp;

	if (nfs_global_client_only != 0)
		return (1);

	/*
	 * We always want to look at the underlying vnode if there is one.
	 */
	if (VOP_REALVP(vp, &rvp, NULL) != 0)
		rvp = vp;
	/*
	 * Some pseudo filesystems (including doorfs) don't actually register
	 * their vfsops_t, so the following may return NULL; we happily let
	 * such vnodes switch zones.
	 */
	vswp = vfs_getvfsswbyvfsops(vfs_getops(rvp->v_vfsp));
	if (vswp != NULL) {
		if (vswp->vsw_flag & VSW_NOTZONESAFE)
			allow = 0;
		vfs_unrefvfssw(vswp);
	}
	return (allow);
}

/*
 * Return nonzero if the vnode is a mount point, zero if not.
 */
int
vn_ismntpt(vnode_t *vp)
{
	return (vp->v_vfsmountedhere != NULL);
}

/* Retrieve the vfs (if any) mounted on this vnode */
vfs_t *
vn_mountedvfs(vnode_t *vp)
{
	return (vp->v_vfsmountedhere);
}

/*
 * Return nonzero if the vnode is referenced by the dnlc, zero if not.
 */
int
vn_in_dnlc(vnode_t *vp)
{
	return (vp->v_count_dnlc > 0);
}

/*
 * vn_has_other_opens() checks whether a particular file is opened by more than
 * just the caller and whether the open is for read and/or write.
 * This routine is for calling after the caller has already called VOP_OPEN()
 * and the caller wishes to know if they are the only one with it open for
 * the mode(s) specified.
 *
 * Vnode counts are only kept on regular files (v_type=VREG).
 */
int
vn_has_other_opens(
	vnode_t *vp,
	v_mode_t mode)
{

	ASSERT(vp != NULL);

	switch (mode) {
	case V_WRITE:
		if (vp->v_wrcnt > 1)
			return (V_TRUE);
		break;
	case V_RDORWR:
		if ((vp->v_rdcnt > 1) || (vp->v_wrcnt > 1))
			return (V_TRUE);
		break;
	case V_RDANDWR:
		if ((vp->v_rdcnt > 1) && (vp->v_wrcnt > 1))
			return (V_TRUE);
		break;
	case V_READ:
		if (vp->v_rdcnt > 1)
			return (V_TRUE);
		break;
	}

	return (V_FALSE);
}

/*
 * vn_is_opened() checks whether a particular file is opened and
 * whether the open is for read and/or write.
 *
 * Vnode counts are only kept on regular files (v_type=VREG).
 */
int
vn_is_opened(
	vnode_t *vp,
	v_mode_t mode)
{

	ASSERT(vp != NULL);

	switch (mode) {
	case V_WRITE:
		if (vp->v_wrcnt)
			return (V_TRUE);
		break;
	case V_RDANDWR:
		if (vp->v_rdcnt && vp->v_wrcnt)
			return (V_TRUE);
		break;
	case V_RDORWR:
		if (vp->v_rdcnt || vp->v_wrcnt)
			return (V_TRUE);
		break;
	case V_READ:
		if (vp->v_rdcnt)
			return (V_TRUE);
		break;
	}

	return (V_FALSE);
}

/*
 * vn_is_mapped() checks whether a particular file is mapped and whether
 * the file is mapped read and/or write.
 */
int
vn_is_mapped(
	vnode_t *vp,
	v_mode_t mode)
{

	ASSERT(vp != NULL);

#if !defined(_LP64)
	switch (mode) {
	/*
	 * The atomic_add_64_nv functions force atomicity in the
	 * case of 32 bit architectures. Otherwise the 64 bit values
	 * require two fetches. The value of the fields may be
	 * (potentially) changed between the first fetch and the
	 * second
	 */
	case V_WRITE:
		if (atomic_add_64_nv((&(vp->v_mmap_write)), 0))
			return (V_TRUE);
		break;
	case V_RDANDWR:
		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) &&
		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
			return (V_TRUE);
		break;
	case V_RDORWR:
		if ((atomic_add_64_nv((&(vp->v_mmap_read)), 0)) ||
		    (atomic_add_64_nv((&(vp->v_mmap_write)), 0)))
			return (V_TRUE);
		break;
	case V_READ:
		if (atomic_add_64_nv((&(vp->v_mmap_read)), 0))
			return (V_TRUE);
		break;
	}
#else
	switch (mode) {
	case V_WRITE:
		if (vp->v_mmap_write)
			return (V_TRUE);
		break;
	case V_RDANDWR:
		if (vp->v_mmap_read && vp->v_mmap_write)
			return (V_TRUE);
		break;
	case V_RDORWR:
		if (vp->v_mmap_read || vp->v_mmap_write)
			return (V_TRUE);
		break;
	case V_READ:
		if (vp->v_mmap_read)
			return (V_TRUE);
		break;
	}
#endif

	return (V_FALSE);
}

/*
 * Set the operations vector for a vnode.
 *
 * FEM ensures that the v_femhead pointer is filled in before the
 * v_op pointer is changed.  This means that if the v_femhead pointer
 * is NULL, and the v_op field hasn't changed since before which checked
 * the v_femhead pointer; then our update is ok - we are not racing with
 * FEM.
 */
void
vn_setops(vnode_t *vp, vnodeops_t *vnodeops)
{
	vnodeops_t	*op;

	ASSERT(vp != NULL);
	ASSERT(vnodeops != NULL);

	op = vp->v_op;
	membar_consumer();
	/*
	 * If vp->v_femhead == NULL, then we'll call casptr() to do the
	 * compare-and-swap on vp->v_op.  If either fails, then FEM is
	 * in effect on the vnode and we need to have FEM deal with it.
	 */
	if (vp->v_femhead != NULL || casptr(&vp->v_op, op, vnodeops) != op) {
		fem_setvnops(vp, vnodeops);
	}
}

/*
 * Retrieve the operations vector for a vnode
 * As with vn_setops(above); make sure we aren't racing with FEM.
 * FEM sets the v_op to a special, internal, vnodeops that wouldn't
 * make sense to the callers of this routine.
 */
vnodeops_t *
vn_getops(vnode_t *vp)
{
	vnodeops_t	*op;

	ASSERT(vp != NULL);

	op = vp->v_op;
	membar_consumer();
	if (vp->v_femhead == NULL && op == vp->v_op) {
		return (op);
	} else {
		return (fem_getvnops(vp));
	}
}

/*
 * Returns non-zero (1) if the vnodeops matches that of the vnode.
 * Returns zero (0) if not.
 */
int
vn_matchops(vnode_t *vp, vnodeops_t *vnodeops)
{
	return (vn_getops(vp) == vnodeops);
}

/*
 * Returns non-zero (1) if the specified operation matches the
 * corresponding operation for that the vnode.
 * Returns zero (0) if not.
 */

#define	MATCHNAME(n1, n2) (((n1)[0] == (n2)[0]) && (strcmp((n1), (n2)) == 0))

int
vn_matchopval(vnode_t *vp, char *vopname, fs_generic_func_p funcp)
{
	const fs_operation_trans_def_t *otdp;
	fs_generic_func_p *loc = NULL;
	vnodeops_t	*vop = vn_getops(vp);

	ASSERT(vopname != NULL);

	for (otdp = vn_ops_table; otdp->name != NULL; otdp++) {
		if (MATCHNAME(otdp->name, vopname)) {
			loc = (fs_generic_func_p *)
			    ((char *)(vop) + otdp->offset);
			break;
		}
	}

	return ((loc != NULL) && (*loc == funcp));
}

/*
 * fs_new_caller_id() needs to return a unique ID on a given local system.
 * The IDs do not need to survive across reboots.  These are primarily
 * used so that (FEM) monitors can detect particular callers (such as
 * the NFS server) to a given vnode/vfs operation.
 */
u_longlong_t
fs_new_caller_id()
{
	static uint64_t next_caller_id = 0LL; /* First call returns 1 */

	return ((u_longlong_t)atomic_add_64_nv(&next_caller_id, 1));
}

/*
 * Given a starting vnode and a path, updates the path in the target vnode in
 * a safe manner.  If the vnode already has path information embedded, then the
 * cached path is left untouched.
 */

size_t max_vnode_path = 4 * MAXPATHLEN;

void
vn_setpath(vnode_t *rootvp, struct vnode *startvp, struct vnode *vp,
    const char *path, size_t plen)
{
	char	*rpath;
	vnode_t	*base;
	size_t	rpathlen, rpathalloc;
	int	doslash = 1;

	if (*path == '/') {
		base = rootvp;
		path++;
		plen--;
	} else {
		base = startvp;
	}

	/*
	 * We cannot grab base->v_lock while we hold vp->v_lock because of
	 * the potential for deadlock.
	 */
	mutex_enter(&base->v_lock);
	if (base->v_path == NULL) {
		mutex_exit(&base->v_lock);
		return;
	}

	rpathlen = strlen(base->v_path);
	rpathalloc = rpathlen + plen + 1;
	/* Avoid adding a slash if there's already one there */
	if (base->v_path[rpathlen-1] == '/')
		doslash = 0;
	else
		rpathalloc++;

	/*
	 * We don't want to call kmem_alloc(KM_SLEEP) with kernel locks held,
	 * so we must do this dance.  If, by chance, something changes the path,
	 * just give up since there is no real harm.
	 */
	mutex_exit(&base->v_lock);

	/* Paths should stay within reason */
	if (rpathalloc > max_vnode_path)
		return;

	rpath = kmem_alloc(rpathalloc, KM_SLEEP);

	mutex_enter(&base->v_lock);
	if (base->v_path == NULL || strlen(base->v_path) != rpathlen) {
		mutex_exit(&base->v_lock);
		kmem_free(rpath, rpathalloc);
		return;
	}
	bcopy(base->v_path, rpath, rpathlen);
	mutex_exit(&base->v_lock);

	if (doslash)
		rpath[rpathlen++] = '/';
	bcopy(path, rpath + rpathlen, plen);
	rpath[rpathlen + plen] = '\0';

	mutex_enter(&vp->v_lock);
	if (vp->v_path != NULL) {
		mutex_exit(&vp->v_lock);
		kmem_free(rpath, rpathalloc);
	} else {
		vp->v_path = rpath;
		mutex_exit(&vp->v_lock);
	}
}

/*
 * Sets the path to the vnode to be the given string, regardless of current
 * context.  The string must be a complete path from rootdir.  This is only used
 * by fsop_root() for setting the path based on the mountpoint.
 */
void
vn_setpath_str(struct vnode *vp, const char *str, size_t len)
{
	char *buf = kmem_alloc(len + 1, KM_SLEEP);

	mutex_enter(&vp->v_lock);
	if (vp->v_path != NULL) {
		mutex_exit(&vp->v_lock);
		kmem_free(buf, len + 1);
		return;
	}

	vp->v_path = buf;
	bcopy(str, vp->v_path, len);
	vp->v_path[len] = '\0';

	mutex_exit(&vp->v_lock);
}

/*
 * Called from within filesystem's vop_rename() to handle renames once the
 * target vnode is available.
 */
void
vn_renamepath(vnode_t *dvp, vnode_t *vp, const char *nm, size_t len)
{
	char *tmp;

	mutex_enter(&vp->v_lock);
	tmp = vp->v_path;
	vp->v_path = NULL;
	mutex_exit(&vp->v_lock);
	vn_setpath(rootdir, dvp, vp, nm, len);
	if (tmp != NULL)
		kmem_free(tmp, strlen(tmp) + 1);
}

/*
 * Similar to vn_setpath_str(), this function sets the path of the destination
 * vnode to the be the same as the source vnode.
 */
void
vn_copypath(struct vnode *src, struct vnode *dst)
{
	char *buf;
	int alloc;

	mutex_enter(&src->v_lock);
	if (src->v_path == NULL) {
		mutex_exit(&src->v_lock);
		return;
	}
	alloc = strlen(src->v_path) + 1;

	/* avoid kmem_alloc() with lock held */
	mutex_exit(&src->v_lock);
	buf = kmem_alloc(alloc, KM_SLEEP);
	mutex_enter(&src->v_lock);
	if (src->v_path == NULL || strlen(src->v_path) + 1 != alloc) {
		mutex_exit(&src->v_lock);
		kmem_free(buf, alloc);
		return;
	}
	bcopy(src->v_path, buf, alloc);
	mutex_exit(&src->v_lock);

	mutex_enter(&dst->v_lock);
	if (dst->v_path != NULL) {
		mutex_exit(&dst->v_lock);
		kmem_free(buf, alloc);
		return;
	}
	dst->v_path = buf;
	mutex_exit(&dst->v_lock);
}

/*
 * XXX Private interface for segvn routines that handle vnode
 * large page segments.
 *
 * return 1 if vp's file system VOP_PAGEIO() implementation
 * can be safely used instead of VOP_GETPAGE() for handling
 * pagefaults against regular non swap files. VOP_PAGEIO()
 * interface is considered safe here if its implementation
 * is very close to VOP_GETPAGE() implementation.
 * e.g. It zero's out the part of the page beyond EOF. Doesn't
 * panic if there're file holes but instead returns an error.
 * Doesn't assume file won't be changed by user writes, etc.
 *
 * return 0 otherwise.
 *
 * For now allow segvn to only use VOP_PAGEIO() with ufs and nfs.
 */
int
vn_vmpss_usepageio(vnode_t *vp)
{
	vfs_t   *vfsp = vp->v_vfsp;
	char *fsname = vfssw[vfsp->vfs_fstype].vsw_name;
	char *pageio_ok_fss[] = {"ufs", "nfs", NULL};
	char **fsok = pageio_ok_fss;

	if (fsname == NULL) {
		return (0);
	}

	for (; *fsok; fsok++) {
		if (strcmp(*fsok, fsname) == 0) {
			return (1);
		}
	}
	return (0);
}

/* VOP_XXX() macros call the corresponding fop_xxx() function */

int
fop_open(
	vnode_t **vpp,
	int mode,
	cred_t *cr,
	caller_context_t *ct)
{
	int ret;
	vnode_t *vp = *vpp;

	VN_HOLD(vp);
	/*
	 * Adding to the vnode counts before calling open
	 * avoids the need for a mutex. It circumvents a race
	 * condition where a query made on the vnode counts results in a
	 * false negative. The inquirer goes away believing the file is
	 * not open when there is an open on the file already under way.
	 *
	 * The counts are meant to prevent NFS from granting a delegation
	 * when it would be dangerous to do so.
	 *
	 * The vnode counts are only kept on regular files
	 */
	if ((*vpp)->v_type == VREG) {
		if (mode & FREAD)
			atomic_add_32(&((*vpp)->v_rdcnt), 1);
		if (mode & FWRITE)
			atomic_add_32(&((*vpp)->v_wrcnt), 1);
	}

	VOPXID_MAP_CR(vp, cr);

	ret = (*(*(vpp))->v_op->vop_open)(vpp, mode, cr, ct);

	if (ret) {
		/*
		 * Use the saved vp just in case the vnode ptr got trashed
		 * by the error.
		 */
		VOPSTATS_UPDATE(vp, open);
		if ((vp->v_type == VREG) && (mode & FREAD))
			atomic_add_32(&(vp->v_rdcnt), -1);
		if ((vp->v_type == VREG) && (mode & FWRITE))
			atomic_add_32(&(vp->v_wrcnt), -1);
	} else {
		/*
		 * Some filesystems will return a different vnode,
		 * but the same path was still used to open it.
		 * So if we do change the vnode and need to
		 * copy over the path, do so here, rather than special
		 * casing each filesystem. Adjust the vnode counts to
		 * reflect the vnode switch.
		 */
		VOPSTATS_UPDATE(*vpp, open);
		if (*vpp != vp && *vpp != NULL) {
			vn_copypath(vp, *vpp);
			if (((*vpp)->v_type == VREG) && (mode & FREAD))
				atomic_add_32(&((*vpp)->v_rdcnt), 1);
			if ((vp->v_type == VREG) && (mode & FREAD))
				atomic_add_32(&(vp->v_rdcnt), -1);
			if (((*vpp)->v_type == VREG) && (mode & FWRITE))
				atomic_add_32(&((*vpp)->v_wrcnt), 1);
			if ((vp->v_type == VREG) && (mode & FWRITE))
				atomic_add_32(&(vp->v_wrcnt), -1);
		}
	}
	VN_RELE(vp);
	return (ret);
}

int
fop_close(
	vnode_t *vp,
	int flag,
	int count,
	offset_t offset,
	cred_t *cr,
	caller_context_t *ct)
{
	int err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_close)(vp, flag, count, offset, cr, ct);
	VOPSTATS_UPDATE(vp, close);
	/*
	 * Check passed in count to handle possible dups. Vnode counts are only
	 * kept on regular files
	 */
	if ((vp->v_type == VREG) && (count == 1))  {
		if (flag & FREAD) {
			ASSERT(vp->v_rdcnt > 0);
			atomic_add_32(&(vp->v_rdcnt), -1);
		}
		if (flag & FWRITE) {
			ASSERT(vp->v_wrcnt > 0);
			atomic_add_32(&(vp->v_wrcnt), -1);
		}
	}
	return (err);
}

int
fop_read(
	vnode_t *vp,
	uio_t *uiop,
	int ioflag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;
	ssize_t	resid_start = uiop->uio_resid;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_read)(vp, uiop, ioflag, cr, ct);
	VOPSTATS_UPDATE_IO(vp, read,
	    read_bytes, (resid_start - uiop->uio_resid));
	return (err);
}

int
fop_write(
	vnode_t *vp,
	uio_t *uiop,
	int ioflag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;
	ssize_t	resid_start = uiop->uio_resid;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_write)(vp, uiop, ioflag, cr, ct);
	VOPSTATS_UPDATE_IO(vp, write,
	    write_bytes, (resid_start - uiop->uio_resid));
	return (err);
}

int
fop_ioctl(
	vnode_t *vp,
	int cmd,
	intptr_t arg,
	int flag,
	cred_t *cr,
	int *rvalp,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_ioctl)(vp, cmd, arg, flag, cr, rvalp, ct);
	VOPSTATS_UPDATE(vp, ioctl);
	return (err);
}

int
fop_setfl(
	vnode_t *vp,
	int oflags,
	int nflags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_setfl)(vp, oflags, nflags, cr, ct);
	VOPSTATS_UPDATE(vp, setfl);
	return (err);
}

int
fop_getattr(
	vnode_t *vp,
	vattr_t *vap,
	int flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	/*
	 * If this file system doesn't understand the xvattr extensions
	 * then turn off the xvattr bit.
	 */
	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
		vap->va_mask &= ~AT_XVATTR;
	}

	/*
	 * We're only allowed to skip the ACL check iff we used a 32 bit
	 * ACE mask with VOP_ACCESS() to determine permissions.
	 */
	if ((flags & ATTR_NOACLCHECK) &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
		return (EINVAL);
	}
	err = (*(vp)->v_op->vop_getattr)(vp, vap, flags, cr, ct);
	VOPSTATS_UPDATE(vp, getattr);
	return (err);
}

int
fop_setattr(
	vnode_t *vp,
	vattr_t *vap,
	int flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	/*
	 * If this file system doesn't understand the xvattr extensions
	 * then turn off the xvattr bit.
	 */
	if (vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) == 0) {
		vap->va_mask &= ~AT_XVATTR;
	}

	/*
	 * We're only allowed to skip the ACL check iff we used a 32 bit
	 * ACE mask with VOP_ACCESS() to determine permissions.
	 */
	if ((flags & ATTR_NOACLCHECK) &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
		return (EINVAL);
	}
	err = (*(vp)->v_op->vop_setattr)(vp, vap, flags, cr, ct);
	VOPSTATS_UPDATE(vp, setattr);
	return (err);
}

int
fop_access(
	vnode_t *vp,
	int mode,
	int flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	if ((flags & V_ACE_MASK) &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
		return (EINVAL);
	}

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_access)(vp, mode, flags, cr, ct);
	VOPSTATS_UPDATE(vp, access);
	return (err);
}

int
fop_lookup(
	vnode_t *dvp,
	char *nm,
	vnode_t **vpp,
	pathname_t *pnp,
	int flags,
	vnode_t *rdir,
	cred_t *cr,
	caller_context_t *ct,
	int *deflags,		/* Returned per-dirent flags */
	pathname_t *ppnp)	/* Returned case-preserved name in directory */
{
	int ret;

	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.  It is required
	 * that if the vfs supports case-insensitive lookup, it also
	 * supports extended dirent flags.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	if ((flags & LOOKUP_XATTR) && (flags & LOOKUP_HAVE_SYSATTR_DIR) == 0) {
		ret = xattr_dir_lookup(dvp, vpp, flags, cr);
	} else {
		ret = (*(dvp)->v_op->vop_lookup)
		    (dvp, nm, vpp, pnp, flags, rdir, cr, ct, deflags, ppnp);
	}
	if (ret == 0 && *vpp) {
		VOPSTATS_UPDATE(*vpp, lookup);
		if ((*vpp)->v_path == NULL) {
			vn_setpath(rootdir, dvp, *vpp, nm, strlen(nm));
		}
	}

	return (ret);
}

int
fop_create(
	vnode_t *dvp,
	char *name,
	vattr_t *vap,
	vcexcl_t excl,
	int mode,
	vnode_t **vpp,
	cred_t *cr,
	int flags,
	caller_context_t *ct,
	vsecattr_t *vsecp)	/* ACL to set during create */
{
	int ret;

	if (vsecp != NULL &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
		return (EINVAL);
	}
	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	ret = (*(dvp)->v_op->vop_create)
	    (dvp, name, vap, excl, mode, vpp, cr, flags, ct, vsecp);
	if (ret == 0 && *vpp) {
		VOPSTATS_UPDATE(*vpp, create);
		if ((*vpp)->v_path == NULL) {
			vn_setpath(rootdir, dvp, *vpp, name, strlen(name));
		}
	}

	return (ret);
}

int
fop_remove(
	vnode_t *dvp,
	char *nm,
	cred_t *cr,
	caller_context_t *ct,
	int flags)
{
	int	err;

	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	err = (*(dvp)->v_op->vop_remove)(dvp, nm, cr, ct, flags);
	VOPSTATS_UPDATE(dvp, remove);
	return (err);
}

int
fop_link(
	vnode_t *tdvp,
	vnode_t *svp,
	char *tnm,
	cred_t *cr,
	caller_context_t *ct,
	int flags)
{
	int	err;

	/*
	 * If the target file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(tdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(tdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(tdvp, cr);

	err = (*(tdvp)->v_op->vop_link)(tdvp, svp, tnm, cr, ct, flags);
	VOPSTATS_UPDATE(tdvp, link);
	return (err);
}

int
fop_rename(
	vnode_t *sdvp,
	char *snm,
	vnode_t *tdvp,
	char *tnm,
	cred_t *cr,
	caller_context_t *ct,
	int flags)
{
	int	err;

	/*
	 * If the file system involved does not support
	 * case-insensitive access and said access is requested, fail
	 * quickly.
	 */
	if (flags & FIGNORECASE &&
	    ((vfs_has_feature(sdvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(sdvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0)))
		return (EINVAL);

	VOPXID_MAP_CR(tdvp, cr);

	err = (*(sdvp)->v_op->vop_rename)(sdvp, snm, tdvp, tnm, cr, ct, flags);
	VOPSTATS_UPDATE(sdvp, rename);
	return (err);
}

int
fop_mkdir(
	vnode_t *dvp,
	char *dirname,
	vattr_t *vap,
	vnode_t **vpp,
	cred_t *cr,
	caller_context_t *ct,
	int flags,
	vsecattr_t *vsecp)	/* ACL to set during create */
{
	int ret;

	if (vsecp != NULL &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_ACLONCREATE) == 0) {
		return (EINVAL);
	}
	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	ret = (*(dvp)->v_op->vop_mkdir)
	    (dvp, dirname, vap, vpp, cr, ct, flags, vsecp);
	if (ret == 0 && *vpp) {
		VOPSTATS_UPDATE(*vpp, mkdir);
		if ((*vpp)->v_path == NULL) {
			vn_setpath(rootdir, dvp, *vpp, dirname,
			    strlen(dirname));
		}
	}

	return (ret);
}

int
fop_rmdir(
	vnode_t *dvp,
	char *nm,
	vnode_t *cdir,
	cred_t *cr,
	caller_context_t *ct,
	int flags)
{
	int	err;

	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	err = (*(dvp)->v_op->vop_rmdir)(dvp, nm, cdir, cr, ct, flags);
	VOPSTATS_UPDATE(dvp, rmdir);
	return (err);
}

int
fop_readdir(
	vnode_t *vp,
	uio_t *uiop,
	cred_t *cr,
	int *eofp,
	caller_context_t *ct,
	int flags)
{
	int	err;
	ssize_t	resid_start = uiop->uio_resid;

	/*
	 * If this file system doesn't support retrieving directory
	 * entry flags and said access is requested, fail quickly.
	 */
	if (flags & V_RDDIR_ENTFLAGS &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_DIRENTFLAGS) == 0)
		return (EINVAL);

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_readdir)(vp, uiop, cr, eofp, ct, flags);
	VOPSTATS_UPDATE_IO(vp, readdir,
	    readdir_bytes, (resid_start - uiop->uio_resid));
	return (err);
}

int
fop_symlink(
	vnode_t *dvp,
	char *linkname,
	vattr_t *vap,
	char *target,
	cred_t *cr,
	caller_context_t *ct,
	int flags)
{
	int	err;
	xvattr_t xvattr;

	/*
	 * If this file system doesn't support case-insensitive access
	 * and said access is requested, fail quickly.
	 */
	if (flags & FIGNORECASE &&
	    (vfs_has_feature(dvp->v_vfsp, VFSFT_CASEINSENSITIVE) == 0 &&
	    vfs_has_feature(dvp->v_vfsp, VFSFT_NOCASESENSITIVE) == 0))
		return (EINVAL);

	VOPXID_MAP_CR(dvp, cr);

	/* check for reparse point */
	if ((vfs_has_feature(dvp->v_vfsp, VFSFT_REPARSE)) &&
	    (strncmp(target, FS_REPARSE_TAG_STR,
	    strlen(FS_REPARSE_TAG_STR)) == 0)) {
		if (!fs_reparse_mark(target, vap, &xvattr))
			vap = (vattr_t *)&xvattr;
	}

	err = (*(dvp)->v_op->vop_symlink)
	    (dvp, linkname, vap, target, cr, ct, flags);
	VOPSTATS_UPDATE(dvp, symlink);
	return (err);
}

int
fop_readlink(
	vnode_t *vp,
	uio_t *uiop,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_readlink)(vp, uiop, cr, ct);
	VOPSTATS_UPDATE(vp, readlink);
	return (err);
}

int
fop_fsync(
	vnode_t *vp,
	int syncflag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_fsync)(vp, syncflag, cr, ct);
	VOPSTATS_UPDATE(vp, fsync);
	return (err);
}

void
fop_inactive(
	vnode_t *vp,
	cred_t *cr,
	caller_context_t *ct)
{
	/* Need to update stats before vop call since we may lose the vnode */
	VOPSTATS_UPDATE(vp, inactive);

	VOPXID_MAP_CR(vp, cr);

	(*(vp)->v_op->vop_inactive)(vp, cr, ct);
}

int
fop_fid(
	vnode_t *vp,
	fid_t *fidp,
	caller_context_t *ct)
{
	int	err;

	err = (*(vp)->v_op->vop_fid)(vp, fidp, ct);
	VOPSTATS_UPDATE(vp, fid);
	return (err);
}

int
fop_rwlock(
	vnode_t *vp,
	int write_lock,
	caller_context_t *ct)
{
	int	ret;

	ret = ((*(vp)->v_op->vop_rwlock)(vp, write_lock, ct));
	VOPSTATS_UPDATE(vp, rwlock);
	return (ret);
}

void
fop_rwunlock(
	vnode_t *vp,
	int write_lock,
	caller_context_t *ct)
{
	(*(vp)->v_op->vop_rwunlock)(vp, write_lock, ct);
	VOPSTATS_UPDATE(vp, rwunlock);
}

int
fop_seek(
	vnode_t *vp,
	offset_t ooff,
	offset_t *noffp,
	caller_context_t *ct)
{
	int	err;

	err = (*(vp)->v_op->vop_seek)(vp, ooff, noffp, ct);
	VOPSTATS_UPDATE(vp, seek);
	return (err);
}

int
fop_cmp(
	vnode_t *vp1,
	vnode_t *vp2,
	caller_context_t *ct)
{
	int	err;

	err = (*(vp1)->v_op->vop_cmp)(vp1, vp2, ct);
	VOPSTATS_UPDATE(vp1, cmp);
	return (err);
}

int
fop_frlock(
	vnode_t *vp,
	int cmd,
	flock64_t *bfp,
	int flag,
	offset_t offset,
	struct flk_callback *flk_cbp,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_frlock)
	    (vp, cmd, bfp, flag, offset, flk_cbp, cr, ct);
	VOPSTATS_UPDATE(vp, frlock);
	return (err);
}

int
fop_space(
	vnode_t *vp,
	int cmd,
	flock64_t *bfp,
	int flag,
	offset_t offset,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_space)(vp, cmd, bfp, flag, offset, cr, ct);
	VOPSTATS_UPDATE(vp, space);
	return (err);
}

int
fop_realvp(
	vnode_t *vp,
	vnode_t **vpp,
	caller_context_t *ct)
{
	int	err;

	err = (*(vp)->v_op->vop_realvp)(vp, vpp, ct);
	VOPSTATS_UPDATE(vp, realvp);
	return (err);
}

int
fop_getpage(
	vnode_t *vp,
	offset_t off,
	size_t len,
	uint_t *protp,
	page_t **plarr,
	size_t plsz,
	struct seg *seg,
	caddr_t addr,
	enum seg_rw rw,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_getpage)
	    (vp, off, len, protp, plarr, plsz, seg, addr, rw, cr, ct);
	VOPSTATS_UPDATE(vp, getpage);
	return (err);
}

int
fop_putpage(
	vnode_t *vp,
	offset_t off,
	size_t len,
	int flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_putpage)(vp, off, len, flags, cr, ct);
	VOPSTATS_UPDATE(vp, putpage);
	return (err);
}

int
fop_map(
	vnode_t *vp,
	offset_t off,
	struct as *as,
	caddr_t *addrp,
	size_t len,
	uchar_t prot,
	uchar_t maxprot,
	uint_t flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_map)
	    (vp, off, as, addrp, len, prot, maxprot, flags, cr, ct);
	VOPSTATS_UPDATE(vp, map);
	return (err);
}

int
fop_addmap(
	vnode_t *vp,
	offset_t off,
	struct as *as,
	caddr_t addr,
	size_t len,
	uchar_t prot,
	uchar_t maxprot,
	uint_t flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int error;
	u_longlong_t delta;

	VOPXID_MAP_CR(vp, cr);

	error = (*(vp)->v_op->vop_addmap)
	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);

	if ((!error) && (vp->v_type == VREG)) {
		delta = (u_longlong_t)btopr(len);
		/*
		 * If file is declared MAP_PRIVATE, it can't be written back
		 * even if open for write. Handle as read.
		 */
		if (flags & MAP_PRIVATE) {
			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
			    (int64_t)delta);
		} else {
			/*
			 * atomic_add_64 forces the fetch of a 64 bit value to
			 * be atomic on 32 bit machines
			 */
			if (maxprot & PROT_WRITE)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
				    (int64_t)delta);
			if (maxprot & PROT_READ)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
				    (int64_t)delta);
			if (maxprot & PROT_EXEC)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
				    (int64_t)delta);
		}
	}
	VOPSTATS_UPDATE(vp, addmap);
	return (error);
}

int
fop_delmap(
	vnode_t *vp,
	offset_t off,
	struct as *as,
	caddr_t addr,
	size_t len,
	uint_t prot,
	uint_t maxprot,
	uint_t flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int error;
	u_longlong_t delta;

	VOPXID_MAP_CR(vp, cr);

	error = (*(vp)->v_op->vop_delmap)
	    (vp, off, as, addr, len, prot, maxprot, flags, cr, ct);

	/*
	 * NFS calls into delmap twice, the first time
	 * it simply establishes a callback mechanism and returns EAGAIN
	 * while the real work is being done upon the second invocation.
	 * We have to detect this here and only decrement the counts upon
	 * the second delmap request.
	 */
	if ((error != EAGAIN) && (vp->v_type == VREG)) {

		delta = (u_longlong_t)btopr(len);

		if (flags & MAP_PRIVATE) {
			atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
			    (int64_t)(-delta));
		} else {
			/*
			 * atomic_add_64 forces the fetch of a 64 bit value
			 * to be atomic on 32 bit machines
			 */
			if (maxprot & PROT_WRITE)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_write)),
				    (int64_t)(-delta));
			if (maxprot & PROT_READ)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
				    (int64_t)(-delta));
			if (maxprot & PROT_EXEC)
				atomic_add_64((uint64_t *)(&(vp->v_mmap_read)),
				    (int64_t)(-delta));
		}
	}
	VOPSTATS_UPDATE(vp, delmap);
	return (error);
}


int
fop_poll(
	vnode_t *vp,
	short events,
	int anyyet,
	short *reventsp,
	struct pollhead **phpp,
	caller_context_t *ct)
{
	int	err;

	err = (*(vp)->v_op->vop_poll)(vp, events, anyyet, reventsp, phpp, ct);
	VOPSTATS_UPDATE(vp, poll);
	return (err);
}

int
fop_dump(
	vnode_t *vp,
	caddr_t addr,
	offset_t lbdn,
	offset_t dblks,
	caller_context_t *ct)
{
	int	err;

	/* ensure lbdn and dblks can be passed safely to bdev_dump */
	if ((lbdn != (daddr_t)lbdn) || (dblks != (int)dblks))
		return (EIO);

	err = (*(vp)->v_op->vop_dump)(vp, addr, lbdn, dblks, ct);
	VOPSTATS_UPDATE(vp, dump);
	return (err);
}

int
fop_pathconf(
	vnode_t *vp,
	int cmd,
	ulong_t *valp,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_pathconf)(vp, cmd, valp, cr, ct);
	VOPSTATS_UPDATE(vp, pathconf);
	return (err);
}

int
fop_pageio(
	vnode_t *vp,
	struct page *pp,
	u_offset_t io_off,
	size_t io_len,
	int flags,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_pageio)(vp, pp, io_off, io_len, flags, cr, ct);
	VOPSTATS_UPDATE(vp, pageio);
	return (err);
}

int
fop_dumpctl(
	vnode_t *vp,
	int action,
	offset_t *blkp,
	caller_context_t *ct)
{
	int	err;
	err = (*(vp)->v_op->vop_dumpctl)(vp, action, blkp, ct);
	VOPSTATS_UPDATE(vp, dumpctl);
	return (err);
}

void
fop_dispose(
	vnode_t *vp,
	page_t *pp,
	int flag,
	int dn,
	cred_t *cr,
	caller_context_t *ct)
{
	/* Must do stats first since it's possible to lose the vnode */
	VOPSTATS_UPDATE(vp, dispose);

	VOPXID_MAP_CR(vp, cr);

	(*(vp)->v_op->vop_dispose)(vp, pp, flag, dn, cr, ct);
}

int
fop_setsecattr(
	vnode_t *vp,
	vsecattr_t *vsap,
	int flag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	/*
	 * We're only allowed to skip the ACL check iff we used a 32 bit
	 * ACE mask with VOP_ACCESS() to determine permissions.
	 */
	if ((flag & ATTR_NOACLCHECK) &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
		return (EINVAL);
	}
	err = (*(vp)->v_op->vop_setsecattr) (vp, vsap, flag, cr, ct);
	VOPSTATS_UPDATE(vp, setsecattr);
	return (err);
}

int
fop_getsecattr(
	vnode_t *vp,
	vsecattr_t *vsap,
	int flag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	/*
	 * We're only allowed to skip the ACL check iff we used a 32 bit
	 * ACE mask with VOP_ACCESS() to determine permissions.
	 */
	if ((flag & ATTR_NOACLCHECK) &&
	    vfs_has_feature(vp->v_vfsp, VFSFT_ACEMASKONACCESS) == 0) {
		return (EINVAL);
	}

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_getsecattr) (vp, vsap, flag, cr, ct);
	VOPSTATS_UPDATE(vp, getsecattr);
	return (err);
}

int
fop_shrlock(
	vnode_t *vp,
	int cmd,
	struct shrlock *shr,
	int flag,
	cred_t *cr,
	caller_context_t *ct)
{
	int	err;

	VOPXID_MAP_CR(vp, cr);

	err = (*(vp)->v_op->vop_shrlock)(vp, cmd, shr, flag, cr, ct);
	VOPSTATS_UPDATE(vp, shrlock);
	return (err);
}

int
fop_vnevent(vnode_t *vp, vnevent_t vnevent, vnode_t *dvp, char *fnm,
    caller_context_t *ct)
{
	int	err;

	err = (*(vp)->v_op->vop_vnevent)(vp, vnevent, dvp, fnm, ct);
	VOPSTATS_UPDATE(vp, vnevent);
	return (err);
}

int
fop_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *uiop, cred_t *cr,
    caller_context_t *ct)
{
	int err;

	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
		return (ENOTSUP);
	err = (*(vp)->v_op->vop_reqzcbuf)(vp, ioflag, uiop, cr, ct);
	VOPSTATS_UPDATE(vp, reqzcbuf);
	return (err);
}

int
fop_retzcbuf(vnode_t *vp, xuio_t *uiop, cred_t *cr, caller_context_t *ct)
{
	int err;

	if (vfs_has_feature(vp->v_vfsp, VFSFT_ZEROCOPY_SUPPORTED) == 0)
		return (ENOTSUP);
	err = (*(vp)->v_op->vop_retzcbuf)(vp, uiop, cr, ct);
	VOPSTATS_UPDATE(vp, retzcbuf);
	return (err);
}

/*
 * Default destructor
 *	Needed because NULL destructor means that the key is unused
 */
/* ARGSUSED */
void
vsd_defaultdestructor(void *value)
{}

/*
 * Create a key (index into per vnode array)
 *	Locks out vsd_create, vsd_destroy, and vsd_free
 *	May allocate memory with lock held
 */
void
vsd_create(uint_t *keyp, void (*destructor)(void *))
{
	int	i;
	uint_t	nkeys;

	/*
	 * if key is allocated, do nothing
	 */
	mutex_enter(&vsd_lock);
	if (*keyp) {
		mutex_exit(&vsd_lock);
		return;
	}
	/*
	 * find an unused key
	 */
	if (destructor == NULL)
		destructor = vsd_defaultdestructor;

	for (i = 0; i < vsd_nkeys; ++i)
		if (vsd_destructor[i] == NULL)
			break;

	/*
	 * if no unused keys, increase the size of the destructor array
	 */
	if (i == vsd_nkeys) {
		if ((nkeys = (vsd_nkeys << 1)) == 0)
			nkeys = 1;
		vsd_destructor =
		    (void (**)(void *))vsd_realloc((void *)vsd_destructor,
		    (size_t)(vsd_nkeys * sizeof (void (*)(void *))),
		    (size_t)(nkeys * sizeof (void (*)(void *))));
		vsd_nkeys = nkeys;
	}

	/*
	 * allocate the next available unused key
	 */
	vsd_destructor[i] = destructor;
	*keyp = i + 1;

	/* create vsd_list, if it doesn't exist */
	if (vsd_list == NULL) {
		vsd_list = kmem_alloc(sizeof (list_t), KM_SLEEP);
		list_create(vsd_list, sizeof (struct vsd_node),
		    offsetof(struct vsd_node, vs_nodes));
	}

	mutex_exit(&vsd_lock);
}

/*
 * Destroy a key
 *
 * Assumes that the caller is preventing vsd_set and vsd_get
 * Locks out vsd_create, vsd_destroy, and vsd_free
 * May free memory with lock held
 */
void
vsd_destroy(uint_t *keyp)
{
	uint_t key;
	struct vsd_node *vsd;

	/*
	 * protect the key namespace and our destructor lists
	 */
	mutex_enter(&vsd_lock);
	key = *keyp;
	*keyp = 0;

	ASSERT(key <= vsd_nkeys);

	/*
	 * if the key is valid
	 */
	if (key != 0) {
		uint_t k = key - 1;
		/*
		 * for every vnode with VSD, call key's destructor
		 */
		for (vsd = list_head(vsd_list); vsd != NULL;
		    vsd = list_next(vsd_list, vsd)) {
			/*
			 * no VSD for key in this vnode
			 */
			if (key > vsd->vs_nkeys)
				continue;
			/*
			 * call destructor for key
			 */
			if (vsd->vs_value[k] && vsd_destructor[k])
				(*vsd_destructor[k])(vsd->vs_value[k]);
			/*
			 * reset value for key
			 */
			vsd->vs_value[k] = NULL;
		}
		/*
		 * actually free the key (NULL destructor == unused)
		 */
		vsd_destructor[k] = NULL;
	}

	mutex_exit(&vsd_lock);
}

/*
 * Quickly return the per vnode value that was stored with the specified key
 * Assumes the caller is protecting key from vsd_create and vsd_destroy
 * Assumes the caller is holding v_vsd_lock to protect the vsd.
 */
void *
vsd_get(vnode_t *vp, uint_t key)
{
	struct vsd_node *vsd;

	ASSERT(vp != NULL);
	ASSERT(mutex_owned(&vp->v_vsd_lock));

	vsd = vp->v_vsd;

	if (key && vsd != NULL && key <= vsd->vs_nkeys)
		return (vsd->vs_value[key - 1]);
	return (NULL);
}

/*
 * Set a per vnode value indexed with the specified key
 * Assumes the caller is holding v_vsd_lock to protect the vsd.
 */
int
vsd_set(vnode_t *vp, uint_t key, void *value)
{
	struct vsd_node *vsd;

	ASSERT(vp != NULL);
	ASSERT(mutex_owned(&vp->v_vsd_lock));

	if (key == 0)
		return (EINVAL);

	vsd = vp->v_vsd;
	if (vsd == NULL)
		vsd = vp->v_vsd = kmem_zalloc(sizeof (*vsd), KM_SLEEP);

	/*
	 * If the vsd was just allocated, vs_nkeys will be 0, so the following
	 * code won't happen and we will continue down and allocate space for
	 * the vs_value array.
	 * If the caller is replacing one value with another, then it is up
	 * to the caller to free/rele/destroy the previous value (if needed).
	 */
	if (key <= vsd->vs_nkeys) {
		vsd->vs_value[key - 1] = value;
		return (0);
	}

	ASSERT(key <= vsd_nkeys);

	if (vsd->vs_nkeys == 0) {
		mutex_enter(&vsd_lock);	/* lock out vsd_destroy() */
		/*
		 * Link onto list of all VSD nodes.
		 */
		list_insert_head(vsd_list, vsd);
		mutex_exit(&vsd_lock);
	}

	/*
	 * Allocate vnode local storage and set the value for key
	 */
	vsd->vs_value = vsd_realloc(vsd->vs_value,
	    vsd->vs_nkeys * sizeof (void *),
	    key * sizeof (void *));
	vsd->vs_nkeys = key;
	vsd->vs_value[key - 1] = value;

	return (0);
}

/*
 * Called from vn_free() to run the destructor function for each vsd
 *	Locks out vsd_create and vsd_destroy
 *	Assumes that the destructor *DOES NOT* use vsd
 */
void
vsd_free(vnode_t *vp)
{
	int i;
	struct vsd_node *vsd = vp->v_vsd;

	if (vsd == NULL)
		return;

	if (vsd->vs_nkeys == 0) {
		kmem_free(vsd, sizeof (*vsd));
		vp->v_vsd = NULL;
		return;
	}

	/*
	 * lock out vsd_create and vsd_destroy, call
	 * the destructor, and mark the value as destroyed.
	 */
	mutex_enter(&vsd_lock);

	for (i = 0; i < vsd->vs_nkeys; i++) {
		if (vsd->vs_value[i] && vsd_destructor[i])
			(*vsd_destructor[i])(vsd->vs_value[i]);
		vsd->vs_value[i] = NULL;
	}

	/*
	 * remove from linked list of VSD nodes
	 */
	list_remove(vsd_list, vsd);

	mutex_exit(&vsd_lock);

	/*
	 * free up the VSD
	 */
	kmem_free(vsd->vs_value, vsd->vs_nkeys * sizeof (void *));
	kmem_free(vsd, sizeof (struct vsd_node));
	vp->v_vsd = NULL;
}

/*
 * realloc
 */
static void *
vsd_realloc(void *old, size_t osize, size_t nsize)
{
	void *new;

	new = kmem_zalloc(nsize, KM_SLEEP);
	if (old) {
		bcopy(old, new, osize);
		kmem_free(old, osize);
	}
	return (new);
}

/*
 * Setup the extensible system attribute for creating a reparse point.
 * The symlink data 'target' is validated for proper format of a reparse
 * string and a check also made to make sure the symlink data does not
 * point to an existing file.
 *
 * return 0 if ok else -1.
 */
static int
fs_reparse_mark(char *target, vattr_t *vap, xvattr_t *xvattr)
{
	xoptattr_t *xoap;

	if ((!target) || (!vap) || (!xvattr))
		return (-1);

	/* validate reparse string */
	if (reparse_validate((const char *)target))
		return (-1);

	xva_init(xvattr);
	xvattr->xva_vattr = *vap;
	xvattr->xva_vattr.va_mask |= AT_XVATTR;
	xoap = xva_getxoptattr(xvattr);
	ASSERT(xoap);
	XVA_SET_REQ(xvattr, XAT_REPARSE);
	xoap->xoa_reparse = 1;

	return (0);
}

/*
 * Function to check whether a symlink is a reparse point.
 * Return B_TRUE if it is a reparse point, else return B_FALSE
 */
boolean_t
vn_is_reparse(vnode_t *vp, cred_t *cr, caller_context_t *ct)
{
	xvattr_t xvattr;
	xoptattr_t *xoap;

	if ((vp->v_type != VLNK) ||
	    !(vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR)))
		return (B_FALSE);

	xva_init(&xvattr);
	xoap = xva_getxoptattr(&xvattr);
	ASSERT(xoap);
	XVA_SET_REQ(&xvattr, XAT_REPARSE);

	if (VOP_GETATTR(vp, &xvattr.xva_vattr, 0, cr, ct))
		return (B_FALSE);

	if ((!(xvattr.xva_vattr.va_mask & AT_XVATTR)) ||
	    (!(XVA_ISSET_RTN(&xvattr, XAT_REPARSE))))
		return (B_FALSE);

	return (xoap->xoa_reparse ? B_TRUE : B_FALSE);
}