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

#include <vm/pvn.h>
#include <vm/seg_map.h>
#include <vm/seg_vn.h>
#include <vm/seg_kpm.h>

#include "fuse_queue.h"
#include "fuse.h"

/* Vnode operations */
static int fuse_open(struct vnode **vpp, int flag, struct cred *cred,
    caller_context_t *ct);
static int fuse_close(struct vnode *vp, int flag, int count,
    offset_t offset, struct cred *cred, caller_context_t *ct);
static int fuse_read(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *cred, struct caller_context *ct);
static int fuse_write(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *cred, struct caller_context *ct);
static int fuse_getattr(struct vnode *vp, struct vattr *vap, int flags,
    struct cred *cr, caller_context_t *ct);
static int fuse_setattr(struct vnode *vp, struct vattr *vap,
    int flags, struct cred *cr, caller_context_t *ct);
static int fuse_access(struct vnode *vp, int mode, int flags,
    struct cred *cr, caller_context_t *ct);
static int fuse_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
    struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
    caller_context_t *ct, int *direntflags, pathname_t *realpnp);
static int fuse_create(struct vnode *dvp, char *nm, struct vattr *vap,
    vcexcl_t excl, int mode, struct vnode **vpp, struct cred *cred,
    int flag, caller_context_t *ct, vsecattr_t *vsecp);
static int fuse_readdir(struct vnode *dvp, struct uio *uiop,
    struct cred *cred, int *eofp, caller_context_t *ct, int flags);
static int fuse_fsync(struct vnode *vp, int syncflag, struct cred *cred,
    caller_context_t *ct);
static void fuse_inactive(struct vnode *vp, struct cred *cred,
    caller_context_t *ct);
static int fuse_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
    caller_context_t *ct);
static int fuse_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap,
    vnode_t **vpp, cred_t *cr, caller_context_t *ct, int flags,
    vsecattr_t *vsecp);
static int fuse_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr,
    caller_context_t *ct, int flags);
static int fuse_link(struct vnode *dvp, struct vnode *srcvp, char *tnm,
    struct cred *cred, caller_context_t *ct, int flags);
static int fuse_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm,
    cred_t *cr, caller_context_t *ct, int flags);
static int fuse_remove(vnode_t *dvp, char *name, cred_t *cr,
    caller_context_t *ct, int flags);
static int fuse_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link,
    cred_t *cr, caller_context_t *ct, int flags);
static int fuse_readlink(vnode_t *vp, uio_t *uio, cred_t *cr,
    caller_context_t *ct);
static int fuse_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
    offset_t off, cred_t *cr, caller_context_t *ct);
static int fuse_getpage(struct vnode *vp, offset_t off, size_t len,
    uint_t *protp, page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
    enum seg_rw rw, struct cred *credp, caller_context_t *ct);
static int fuse_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
	struct cred *credp, caller_context_t *ct);
static int fuse_map(vnode_t *, offset_t, struct as *, caddr_t *, size_t,
    uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
static int fuse_addmap(vnode_t *, offset_t, struct as *, caddr_t,  size_t,
    uchar_t, uchar_t, uint_t, cred_t *, caller_context_t *);
static int fuse_delmap(vnode_t *, offset_t, struct as *, caddr_t,  size_t,
    uint_t, uint_t, uint_t, cred_t *, caller_context_t *);

/* Other supporting routines */
static int rdfuse(struct vnode *vp, struct uio *uiop, struct cred *credp);
static int fuse_send_release(fuse_session_t *sep, struct fuse_file_handle *fh,
    enum fuse_opcode op, struct vnode *vp, int flags);
static void fuse_vnode_cache_remove(struct vnode *vp, fuse_session_t *sep);
static void fuse_vnode_cleanup(struct vnode *vp, struct cred *credp,
    fuse_session_t *sep);
static void
fuse_vnode_destroy(struct vnode *vp, struct cred *credp, fuse_session_t *sep);
static void fuse_set_getattr(struct vnode *vp, struct vattr *vap,
    struct fuse_attr *attr);
static int
fuse_add_entry(struct vnode **vpp, struct vnode *dvp, fuse_msg_node_t *msgp,
    fuse_session_t *sep, char *name, int namelen, cred_t *credp, vtype_t vtype);
static void
    fuse_send_forget(uint64_t nodeid, fuse_session_t *sep, uint64_t nlookup);
static int
fuse_getvnode(uint64_t nodeid, struct vnode **vpp, v_getmode vmode,
    uint32_t mode, fuse_session_t *sep, vfs_t *vfsp, int namelen,
    char *name, uint64_t parent_nid, struct cred *credp);
static int
fuse_lookup_i(struct vnode *dvp, char *nm, struct vnode **vpp, cred_t *credp);
static int
fuse_getfilesize(struct vnode *vp, u_offset_t *fsize, struct cred *credp);
static inline void
fsize_change_notify(struct vnode *vp, size_t fsize, int flag);
static int
fuse_access_i(void *vp, int mode, struct cred *credp);
static int
fuse_access_inkernelcheck(void *vvp, int mode, struct cred *credp);
static int
fuse_fsync_fh(struct fuse_file_handle *fhp, struct fuse_fh_param *param);
static void
fuse_page_mapin(struct vnode *vp, struct buf **bp, struct page *pp, size_t len,
    int flag, struct fuse_iov *iovp);
static inline void fuse_vnode_free(struct vnode *vp, fuse_session_t *sep);

const fs_operation_def_t fuse_vnodeops_template[] = {
	VOPNAME_OPEN,		{ .vop_open = fuse_open },
	VOPNAME_CLOSE,		{ .vop_close = fuse_close },
	VOPNAME_READ,		{ .vop_read = fuse_read },
	VOPNAME_WRITE,		{ .vop_write = fuse_write },
	VOPNAME_GETATTR,	{ .vop_getattr = fuse_getattr },
	VOPNAME_SETATTR,	{ .vop_setattr = fuse_setattr },
	VOPNAME_ACCESS,		{ .vop_access = fuse_access },
	VOPNAME_LOOKUP,		{ .vop_lookup = fuse_lookup },
	VOPNAME_LINK,		{ .vop_link = fuse_link },
	VOPNAME_CREATE,		{ .vop_create = fuse_create },
	VOPNAME_READDIR,	{ .vop_readdir = fuse_readdir },
	VOPNAME_FSYNC,		{ .vop_fsync = fuse_fsync },
	VOPNAME_INACTIVE,	{ .vop_inactive = fuse_inactive },
	VOPNAME_DISPOSE,	{ .vop_dispose = fs_dispose },
	VOPNAME_MKDIR,		{ .vop_mkdir = fuse_mkdir },
	VOPNAME_RMDIR,		{ .vop_rmdir = fuse_rmdir },
	VOPNAME_RENAME,		{ .vop_rename = fuse_rename },
	VOPNAME_REMOVE,		{ .vop_remove = fuse_remove },
	VOPNAME_SYMLINK,	{ .vop_symlink = fuse_symlink },
	VOPNAME_READLINK,	{ .vop_readlink = fuse_readlink },
	VOPNAME_SPACE,		{ .vop_space = fuse_space },
	VOPNAME_GETPAGE,	{ .vop_getpage = fuse_getpage },
	VOPNAME_PUTPAGE,	{ .vop_putpage = fuse_putpage },
	VOPNAME_SEEK,		{ .vop_seek = fuse_seek },
	VOPNAME_MAP,		{ .vop_map = fuse_map },
	VOPNAME_ADDMAP,		{ .vop_addmap = fuse_addmap },
	VOPNAME_DELMAP,		{ .vop_delmap = fuse_delmap },
	NULL, NULL
};


const fs_operation_def_t temp_vnodeops_template[] = {
	VOPNAME_OPEN, fuse_open,
	NULL, 		NULL
};

#define	cache_attrs(vp, fuse_out) do {					\
	timestruc_t ts;							\
									\
	VTOFD(vp)->cached_attrs_bound.tv_sec = (fuse_out)->attr_valid;	\
	VTOFD(vp)->cached_attrs_bound.tv_nsec = (fuse_out)->attr_valid_nsec; \
	gethrestime(&ts);						\
	timespecadd(&VTOFD(vp)->cached_attrs_bound, &ts);		\
	fuse_set_getattr(vp, &(VTOFD(vp)->cached_attrs), &(fuse_out)->attr); \
_NOTE(CONSTCOND) } while (0)

#define	ATTR_CACHE_VALID(curtime, attrtime)				\
	(curtime.tv_sec == attrtime.tv_sec) ?				\
	    (curtime.tv_nsec <= attrtime.tv_nsec) :			\
	    (curtime.tv_sec <= attrtime.tv_sec)

#define	invalidate_cached_attrs(vp)					\
	if (VTOFD(vp))							\
	    bzero(&(VTOFD(vp)->cached_attrs_bound), sizeof (timestruc_t));

/* Function which allocates the requested amount of size for message passing */
void
fuse_buf_alloc(struct fuse_iov *iov, size_t len)
{
	iov->base = kmem_zalloc(FUSE_BUF_ADJUST_SIZE(len), KM_SLEEP);
	iov->len = len;
	iov->memsize = FUSE_BUF_ADJUST_SIZE(len);
	iov->memflag = MEM_TYPE_KMEM;
}

/* Function which initializes the message header structure used by fuse lib */
static void
fuse_setup_inheader(struct fuse_in_header *finh, size_t arglen, int op,
    uint64_t unique, uint64_t nodeid, cred_t *cred)
{
	finh->len	= sizeof (struct fuse_in_header) + arglen;
	finh->opcode	= op;
	finh->unique	= unique;
	finh->nodeid	= nodeid;
	finh->uid	= crgetuid(cred);
	finh->gid	= crgetgid(cred);
	finh->pid	= ddi_get_pid();
}

/* Function which creates a message with ony header and no arguments */
static void
setup_msg_onlyheader(fuse_msg_node_t *msgp, enum fuse_opcode op,
    uint64_t unique, uint64_t nodeid, struct cred *credp)
{
	msgp->fmn_unique = unique;
	msgp->ipdata.iovs_used = 1;
	fuse_buf_alloc(&(msgp->ipdata.iovbuf[0]),
	    (sizeof (struct fuse_in_header)));
	msgp->ipdata.finh = msgp->ipdata.iovbuf[0].base;

	fuse_setup_inheader(msgp->ipdata.finh, 0, op, unique, nodeid, credp);
}

/* Function assigns various parameters to a message node */
static void
fuse_setup_msgparams(fuse_msg_node_t *msgp, size_t argsize,
    enum fuse_opcode op, uint64_t nodeid, cred_t *credp, uint64_t unique)
{
	msgp->ipdata.finh	= msgp->ipdata.iovbuf[0].base;
	msgp->ipdata.indata	= ((char *)msgp->ipdata.iovbuf[0].base +
	    sizeof (struct fuse_in_header));
	msgp->fmn_unique	= unique;

	fuse_setup_inheader(msgp->ipdata.finh, argsize, op,
	    msgp->fmn_unique, nodeid, credp);
	msgp->ipdata.iosize = argsize;
}

static void
fuse_msg_refresh(fuse_msg_node_t *msgp, size_t size, int forcefree)
{
	if (msgp->ipdata.iovbuf[0].memsize == size ||
	    (msgp->ipdata.iovbuf[0].memsize > size && !forcefree))
		return;
	else {
		kmem_free(msgp->ipdata.iovbuf[0].base,
		    msgp->ipdata.iovbuf[0].memsize);
		fuse_buf_alloc(&msgp->ipdata.iovbuf[0], size);
	}
}

/* Frees the memory allocated for data passed from the daemon */
static void
fuse_free_opdata(fuse_msg_node_t *msgp)
{
	if (msgp->opdata.iovbuf.base &&
	    msgp->opdata.iovbuf.memflag == MEM_TYPE_KMEM) {
		kmem_free(msgp->opdata.iovbuf.base,
		    msgp->opdata.iovbuf.memsize);
		if (msgp->opdata.fouth) {
			kmem_free(msgp->opdata.fouth,
			    sizeof (*(msgp->opdata.fouth)));
		}
		bzero(&(msgp->opdata), sizeof (msgp->opdata));
	}
}

/* Function which refreshes a previously used message with new values */
static void
fuse_msg_recreate(fuse_msg_node_t *msgp, size_t argsize, enum fuse_opcode op,
    uint64_t nodeid, cred_t *credp, uint64_t unique, int forcefree)
{
	/* Free the buffer used to receive data from fuse lib */
	fuse_free_opdata(msgp);

	fuse_msg_refresh(msgp, argsize, forcefree);

	fuse_setup_msgparams(msgp, argsize, op, nodeid, credp, unique);
}

/* Function which creates a message with header and arguments */
fuse_msg_node_t *
fuse_setup_message(size_t argsize, enum fuse_opcode op,
    uint64_t nodeid, cred_t *credp, uint64_t unique)
{
	fuse_msg_node_t *msgp = fuse_alloc_msg();

	msgp->ipdata.iovs_used = 1;
	fuse_buf_alloc(&(msgp->ipdata.iovbuf[0]),
	    (sizeof (struct fuse_in_header) + argsize));
	fuse_setup_msgparams(msgp, argsize, op, nodeid, credp, unique);

	return (msgp);
}

/* This function is used from FreeBSD Fuse */
static int
checkentry(struct fuse_entry_out *feo, enum vtype vtyp)
{
	if (vtyp != IFTOVT(feo->attr.mode) ||
	    feo->nodeid == FUSE_NULL_ID ||
	    feo->nodeid == FUSE_ROOT_ID) {
		DTRACE_PROBE2(checkentry_err_nodeid,
		    char *, "Invalid node entry, dropped",
		    struct fuse_entry_out *, feo);
		return (EINVAL);
	}

	return (0);
}

static void
release_create_data(struct fuse_create_data *fcd)
{
	if (fcd->name)
		kmem_free(fcd->name, fcd->namelen);
	kmem_free(fcd, sizeof (*fcd));
}

static int
create_filehandle(struct vnode *vp, int flag, cred_t *credp,
    fuse_msg_node_t **msgpp)
{
	struct fuse_vnode_data	*fvdata = VTOFD(vp);
	struct fuse_open_in	*foi;
	struct fuse_entry_out	*feo;
	struct fuse_mknod_in	*fmni;
	int			err = 0;
	fuse_msg_node_t		*msgp;
	fuse_session_t		*sep;
	fuse_avl_cache_node_t	*cache_nodep;
	int			sent_mknod = 0;
	fuse_avl_cache_node_t 	tofind, *foundp;
	avl_index_t		where;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(create_filehandle_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	msgp = fuse_setup_message((sizeof (*foi) + fvdata->fcd->namelen + 1),
	    FUSE_CREATE, fvdata->par_nid, credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	foi = (struct fuse_open_in *)msgp->ipdata.indata;

	foi->flags = F_TO_O_FLAGS(flag & ~O_NOCTTY);
	foi->mode = fvdata->fcd->mode;
	(void *) strlcpy(((char *)msgp->ipdata.indata + sizeof (*foi)),
	    fvdata->fcd->name, fvdata->fcd->namelen);

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	if (msgp->opdata.fouth->error == ENOSYS) {
		DTRACE_PROBE2(create_filehandle_info_enosys,
		    char *, "FUSE_CREATE not supported by filesys",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto sendmknod;
	}

	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(create_filehandle_err_create_req,
		    char *, "FUSE_CREATE request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

	/* Obtained response from fuse library so interpret it */
	goto resp_intrprt;

sendmknod:
	sent_mknod = 1;
	/*
	 * Try avoiding freeing and allocating memory. Do it only if
	 * inevitable
	 */
	fuse_msg_recreate(msgp, (sizeof (*fmni) + fvdata->fcd->namelen + 1),
	    FUSE_MKNOD, fvdata->par_nid, fvdata->fcd->credp,
	    FUSE_GET_UNIQUE(sep), 0);

	/* Set up arguments to the fuse library */
	fmni = (struct fuse_mknod_in *)msgp->ipdata.indata;

	fmni->mode = fvdata->fcd->mode;
	fmni->rdev = 0;
	(void *) strlcpy((char *)msgp->ipdata.indata + sizeof (*foi),
	    fvdata->fcd->name, fvdata->fcd->namelen);

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(create_filehandle_err_mknod_req,
		    char *, "FUSE_MKNOD request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

resp_intrprt:
	feo = (struct fuse_entry_out *)msgp->opdata.outdata;

	if ((err = checkentry(feo, VREG))) {
		goto cleanup;
	}

	/* Check if there will be a collision? */
	tofind.facn_nodeid = feo->nodeid;
	if ((foundp = avl_find(&(sep->avl_cache), &tofind,
	    &where)) != NULL) {
		DTRACE_PROBE2(create_filehandle_err_collision,
		    char *, "Node already in cache",
		    struct fuse_entry_out *, feo);
		/*
		 * We have the unhappy situation of forcing a purge on the
		 * existing vnode
		 */
		fuse_vnode_destroy(foundp->facn_vnode_p, credp, sep);
	}
	/* Add the vnode to the avl tree */
	cache_nodep = fuse_avl_cache_node_create(vp, feo->nodeid,
	    fvdata->fcd->par_nodeid, fvdata->fcd->namelen, fvdata->fcd->name);
	avl_add(&(sep->avl_cache), cache_nodep);

	release_create_data(fvdata->fcd);
	fvdata->fcd = NULL;
	bzero(&fvdata->fh, sizeof (fvdata->fh));

	/* Reinitialize vnode related fields */
	fvdata->nodeid = feo->nodeid;
	fvdata->nlookup++;
	vn_setops(vp, dv_vnodeops);
#ifndef DONT_CACHE_ATTRIBUTES
	cache_attrs(vp, feo);
#endif

	/*
	 * if we sent FUSE_CREATE request to library, then we don't need to
	 * do FUSE_OPEN, but if we got a response for FUSE_MKNOD, then we
	 * need to send FUSE_OPEN which is taken care by the caller of this
	 * function
	 */
	if (!sent_mknod)
		msgp->opdata.outdata = feo + 1;
	else
		msgp->opdata.outdata = NULL;

	*msgpp = msgp;
	return (err);
cleanup:
	if (fvdata->fcd)
		release_create_data(fvdata->fcd);
	fvdata->fcd = NULL;
	fuse_free_msg(msgp);
	*msgpp = NULL;
	return (err);
}

typedef int fuse_file_check_t(struct fuse_file_handle *fhp,
    struct fuse_fh_param *fh_param);

/* Function which releases a cached file handle */
static int
fuse_release_fh(struct fuse_file_handle *fhp, struct fuse_fh_param *param)
{
	struct vnode *vp = param->vp;
	fuse_vnode_data_t *fvdatap = VTOFD(vp);
	enum fuse_opcode op = (vp->v_type == VDIR) ?
	    FUSE_RELEASEDIR : FUSE_RELEASE;
	fuse_session_t *sep;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_release_fh_info_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
	}

	if ((param->flag & FUSE_FORCE_FH_RELEASE) || fhp->ref == 0) {
		DTRACE_PROBE2(fuse_release_fh_info,
		    char *, "file handle released.",
		    struct fuse_file_handle *, fhp);
		list_remove(&fvdatap->fh_list, fhp);
		if (sep != NULL) {
			(void) fuse_send_release(sep, fhp, op, vp, fhp->mode);
		}
		kmem_free(fhp, sizeof (*fhp));
	}
	/*
	 * This function is always called from iterate_filehandle, and if
	 * we return anything other than zero, iterate_filhandle will not scan
	 * the complete list, so we always return zero from this function.
	 */
	return (0);
}

/*
 * Function does a check if the filehandle passed as parameter is the one needed
 * by the current request
 */
static int
fuse_std_filecheck(struct fuse_file_handle *fhp, struct fuse_fh_param *param)
{
	if (((param->rw_mode & (FREAD | FWRITE | FAPPEND)) &
	    (fhp->mode & (FREAD | FWRITE | FAPPEND))) &&
	    crgetuid(param->credp) == crgetuid(fhp->credp) &&
	    crgetgid(param->credp) == crgetgid(fhp->credp) &&
	    curproc->p_pidp->pid_id == fhp->process_id) {
		fhp->ref++;
		DTRACE_PROBE2(fuse_std_filecheck_info_found,
		    char *, "Found file handle in cache list.",
		    struct fuse_file_handle *, fhp);
		return (1);
	}
	return (0);
}

/*
 * Iterates through the list of file handles and invokes the passed function
 * pointer
 */
static int
iterate_filehandle(struct vnode *vp, fuse_file_check_t file_check_fp,
    void *param, struct fuse_file_handle **fufhpp)
{
	int ret = 0;
	struct fuse_file_handle *fh, *next_fh;
	struct fuse_vnode_data *fvdatap = VTOFD(vp);

	mutex_enter(&fvdatap->fh_list_lock);
	for (fh = list_head(&fvdatap->fh_list); fh; fh = next_fh) {
		next_fh = list_next(&fvdatap->fh_list, fh);
		if ((ret = file_check_fp(fh, param))) {
			*fufhpp = fh;
			break;
		}
	}
	mutex_exit(&fvdatap->fh_list_lock);
	return (ret);
}

static int
get_filehandle(struct vnode *vp, int flag, struct cred *credp,
    struct fuse_file_handle **fufhpp, int check_cache)
{
	struct fuse_open_in	*foin;
	struct fuse_open_out	*foout;
	struct fuse_file_handle *fhp;
	int 			err = 0;
	fuse_msg_node_t		*msgp = NULL;
	fuse_session_t		*sep;
	struct fuse_fh_param	fh_param;
	fuse_vnode_data_t 	*fvdatap;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(get_filehandle_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	/* Check if it is due to create + open */
	if (vn_matchops(vp, temp_vnodeops)) {
		if ((err = create_filehandle(vp, flag, credp, &msgp))) {
			DTRACE_PROBE3(get_filehandle_err_create,
			    char *, "create_filehandle request failed",
			    int, err, struct vnode *, vp);
			goto out;
		} else if (msgp && msgp->opdata.outdata) {
			DTRACE_PROBE2(get_filehandle_info_create_ok,
			    char *, "create_filehandle request successful",
			    struct vnode *, vp);
			goto resp_intrprt;
		}
	}
	if ((check_cache & CACHE_LIST_CHECK)) {
		fh_param.credp = credp;
		fh_param.rw_mode = flag;
		fh_param.fufh = NULL;
		/*
		 * Check if we already have retrieved the file handle
		 * from user space before
		 */
		if (iterate_filehandle(vp, fuse_std_filecheck, &fh_param,
		    fufhpp))
			goto out;
	}
	/*
	 * Allocate a new message node only if create_filehandle wasn't able
	 * to do it for us, if not reuse memory
	 */

	if (!msgp)
		msgp = fuse_setup_message(sizeof (*foin),
		    (vp->v_type == VDIR? FUSE_OPENDIR: FUSE_OPEN),
		    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep));
	else
		fuse_msg_recreate(msgp, sizeof (*foin),
		    (vp->v_type == VDIR? FUSE_OPENDIR: FUSE_OPEN),
		    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep), 0);


	/* Set up arguments to the fuse library */
	foin = (struct fuse_open_in *)msgp->ipdata.indata;
	foin->flags = F_TO_O_FLAGS(flag & ~(O_CREAT | O_EXCL | O_NOCTTY |
	    O_TRUNC));

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* We got woken up, so fuse library has replied to our OPEN request */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(get_filehandle_err_open_req,
		    char *, "FUSE_OPEN request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

	if (msgp->opdata.outsize != sizeof (struct fuse_open_out)) {
		DTRACE_PROBE2(get_filehandle_err_open_inval_resp,
		    char *, "FUSE_OPEN response invalid size",
		    size_t, msgp->opdata.outsize);
		err = EINVAL;
		goto cleanup;
	}
resp_intrprt:
	foout = (struct fuse_open_out *)msgp->opdata.outdata;
	fvdatap = VTOFD(vp);
	fhp = kmem_alloc(sizeof (struct fuse_file_handle), KM_SLEEP);

	fhp->fh_id = foout->fh;
	fhp->flags = foout->open_flags;

	fhp->mode = flag & ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
	crhold(credp);
	fhp->credp = credp;
	fhp->ref = 1;
	fhp->process_id = curproc->p_pidp->pid_id;

	if (fufhpp)
		*fufhpp = fhp;

	/* Add it to the list */
	DTRACE_PROBE2(get_filehandle_info_insert,
	    char *, "file handle added to list",
	    struct fuse_file_handle *, fhp);
	mutex_enter(&fvdatap->fh_list_lock);
	list_insert_head(&fvdatap->fh_list, fhp);
	mutex_exit(&fvdatap->fh_list_lock);

cleanup:
	fuse_free_msg(msgp);
out:
	return (err);
}

/* ARGSUSED */
static int fuse_open(struct vnode **vpp, int flag, struct cred *cred_p,
    caller_context_t *ct)
{
	/*
	 * Send the fuse_OPEN req to lib and get the fh from there.
	 * alloc filehandle and associate with vnode private operation.
	 */
	struct vnode *vp = *vpp;
	int err = 0;

	if ((err = get_filehandle(vp, flag, cred_p, NULL,
	    CACHE_LIST_NO_CHECK))) {
		DTRACE_PROBE2(fuse_open_err_filehandle,
		    char *, "get_filehandle failed",
		    struct vnode *, vp);
		goto out;
	}

	if ((flag & (FWRITE | FTRUNC | FAPPEND)) == (FWRITE | FTRUNC)) {
		vattr_t va;
		int err;

		va.va_mask = AT_SIZE;
		va.va_size = 0;
		err = VOP_SETATTR(vp, &va, 0, cred_p, ct);
	}

	/*
	 * TBD: Check can we do any optimization here? FreeBSD Fuse invokes
	 * vnode_create_vobject to avoid as it says needless getattr...
	 * Anything that can be done for Solaris?
	 */
out:
	return (err);
}

static int
fuse_send_release(fuse_session_t *sep, struct fuse_file_handle *fh,
    enum fuse_opcode op, struct vnode *vp, int flags)
{
	struct fuse_release_in *fri;
	fuse_msg_node_t *msgp;
	int err = DDI_SUCCESS;

	msgp = fuse_setup_message(sizeof (*fri), op, VNODE_TO_NODEID(vp),
	    sep->usercred, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	fri = (struct fuse_release_in *)msgp->ipdata.indata;
	fri->fh	= fh->fh_id;
	fri->flags = F_TO_O_FLAGS(flags & ~O_EXCL);

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* We got woken up, so fuse lib has replied to our release request */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_send_release_err_release_req,
		    char *, "FUSE_RELEASE request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

cleanup:
	fuse_free_msg(msgp);
	return (err);
}

/* ARGSUSED */
static int fuse_close(struct vnode *vp, int flags, int count,
    offset_t offset, struct cred *credp, caller_context_t *ct)
{
	int err = DDI_SUCCESS;
	fuse_vnode_data_t *fvdatap = VTOFD(vp);
	struct fuse_file_handle *fhp = NULL;
	fuse_session_t *sep;
	enum fuse_opcode op = (vp->v_type == VDIR) ?
	    FUSE_RELEASEDIR : FUSE_RELEASE;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_close_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	if ((err = get_filehandle(vp, flags, credp, &fhp, CACHE_LIST_CHECK))) {
		DTRACE_PROBE2(fuse_close_err_filehandle,
		    char *, "get_filehandle failed",
		    struct vnode *, vp);
		goto cleanup;
	}

	fhp->ref--;
	if (fhp->ref == 1) {
		/* Remove it from the list */
		DTRACE_PROBE2(fuse_close_info_release,
		    char *, "releasing file handle",
		    struct fuse_file_handle *, fhp);

		mutex_enter(&fvdatap->fh_list_lock);
		list_remove(&fvdatap->fh_list, fhp);
		mutex_exit(&fvdatap->fh_list_lock);

		err = fuse_send_release(sep, fhp, op, vp, flags);
		kmem_free(fhp, sizeof (*fhp));
	}
cleanup:
	return (err);
}

/* ARGSUSED */
static int
fuse_getapage(struct vnode *vp, u_offset_t off, size_t len, uint_t *protp,
    page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
    enum seg_rw rw, struct cred *credp)
{
	struct page *pp;
	fuse_msg_node_t *msgp = NULL;
	int err = 0;
	struct fuse_read_in *fri;
	fuse_session_t *sep;
	struct fuse_file_handle *fhp = NULL;
	int mode = FREAD;
	struct buf *bp = NULL;

	if (protp != NULL)
		*protp = PROT_ALL;

	if (pl == NULL)
		return (0);

	pl[0] = NULL;

again:
	if (pp = page_lookup(vp, off, rw == S_CREATE ? SE_EXCL : SE_SHARED)) {
		DTRACE_PROBE2(fuse_getapage_info_found,
		    char *, "page found",
		    struct page *, pp);
		pl[0] = pp;
		pl[1] = NULL;
		return (err);
	} else {
		pp = page_create_va(vp, off, PAGESIZE, PG_WAIT,
		    seg, addr);
		/*
		 * Someone raced in and created the page after we did the
		 * lookup but before we did the create, so go back and
		 * try to look it up again.
		 */
		if (pp == NULL) {
			DTRACE_PROBE1(fuse_getapage_err_create_failed,
			    char *, "page_create_va failed, try again");
			goto again;
		}

		DTRACE_PROBE2(fuse_getapage_info_create,
		    char *, "page_create_va successful",
		    struct page *, pp);

		sep = fuse_minor_get_session(getminor(vp->v_rdev));
		if (sep == NULL) {
			DTRACE_PROBE2(fuse_getapage_err_session,
			    char *, "failed to find session",
			    struct vnode *, vp);
			err = ENODEV;
			goto cleanup;
		}

		msgp = fuse_setup_message(sizeof (*fri),
		    (vp->v_type == VDIR) ? FUSE_READDIR : FUSE_READ,
		    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep));

		fri = (struct fuse_read_in *)msgp->ipdata.indata;

		/*
		 * Obtain the file handle associated with this request
		 * if it has already been retrieved from userspace then we
		 * should find it in our file handle list
		 */
		if ((err = get_filehandle(vp, mode, credp, &fhp,
		    CACHE_LIST_CHECK))) {
			DTRACE_PROBE2(fuse_getapage_err_filehandle,
			    char *, "get_filehandle failed",
			    struct vnode *, vp);
			goto cleanup;
		}

		fri->fh = fhp->fh_id;
		fri->offset = off;
		fri->size = (uint32_t)PAGESIZE;
		/* Map the page so that uio_move can be done on the page */
		fuse_page_mapin(vp, &bp, pp, len, B_READ,
		    &(msgp->opdata.iovbuf));

		/*
		 * queue the message for sending to userland filesystem
		 * framework
		 */
		if ((err = fuse_queue_request_wait(sep, msgp))) {
			goto cleanup;
		}
		/*
		 * If all has gone well we should have got the data in the page
		 * by this time already
		 */
		if ((err = msgp->opdata.fouth->error) != 0) {
			DTRACE_PROBE2(fuse_getapage_err_read_req,
			    char *, "FUSE_READ request failed",
			    struct fuse_out_header *, msgp->opdata.fouth);
			goto cleanup;
		}

		/*
		 * XXX: Is this required as this might have already been
		 * done earlier?
		 */
		/* If the returned data is less than what was asked for */
		if (msgp->opdata.outsize < PAGESIZE) {
			/* Zero out the rest of the page */
			bzero((void *)((char *)msgp->opdata.iovbuf.base +
			    msgp->opdata.outsize),
			    (PAGESIZE - msgp->opdata.outsize));
		}

	cleanup:
		if (bp != NULL) {
			bp_mapout(bp);
			pageio_done(bp);
		}

		if (err && pp) {
			pvn_read_done(pp, B_ERROR);
		}

		if (err == 0) {
			pvn_plist_init(pp, pl, plsz, off, PAGESIZE, rw);
		}

		if (fhp)
			fhp->ref--;

		if (msgp)
			fuse_free_msg(msgp);

	}

	return (err);
}


/* ARGSUSED */
static int
fuse_getpage(struct vnode *vp, offset_t off, size_t len, uint_t *protp,
    page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr,
    enum seg_rw rw, struct cred *credp, caller_context_t *ct)
{

	int err;

	if (len > PAGESIZE) {
		err = pvn_getpages(fuse_getapage, vp, off, len, protp, pl, plsz,
		    seg, addr, rw, credp);
	} else {
		err = fuse_getapage(vp, off, len, protp, pl, plsz, seg, addr,
		    rw, credp);
	}

	return (err);
}


/*
 * Read of devices (leaf nodes) is handled by specfs.
 * Read of directories is not supported.
 */
/* ARGSUSED */
static int
fuse_read(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *credp, struct caller_context *ct)
{
	return (rdfuse(vp, uiop, credp));

}

/*
 * This routine performs the actual read operation from the vnode. As a
 * side effect of the call made here, getpage routine gets
 * invoked which obtains the required data from the daemon.
 */
static int
rdfuse(struct vnode *vp, struct uio *uiop, struct cred *credp)
{
	int err;
	ulong_t req_len = uiop->uio_resid;
	ulong_t pageoffset;
	ulong_t segmap_offset;
	offset_t off;
	ssize_t len;
	caddr_t base;
	offset_t diff;
	u_offset_t fsize;

	/* Do consistency checks */
	if (uiop->uio_loffset < 0) {
		DTRACE_PROBE2(rdfuse_err_offset,
		    char *, "uio_loffset < 0",
		    struct uio *, uiop);
		return (EINVAL);
	}

	if (uiop->uio_resid == 0 ||
	    uiop->uio_loffset > MAXOFF_T) {
		DTRACE_PROBE2(rdfuse_info_offset,
		    char *, "uio_resid == 0 || uio_loffset > MAXOFF_T",
		    struct uio *, uiop);
		return (0);
	}

	if ((err = fuse_getfilesize(vp, &fsize, credp))) {
		DTRACE_PROBE3(rdfuse_err_filesize,
		    char *, "fuse_getfilesize failed",
		    int, err, struct vnode *, vp);
		return (err);
	}
	do {
		/* Calculate offset within a page that must be read */
		off = uiop->uio_loffset;
		pageoffset = off & PAGEOFFSET;
		len = MIN(PAGESIZE - pageoffset, uiop->uio_resid);

		/* Check if we are crossing end of file */
		diff = fsize - off;

		if (diff <= 0) {
			err = 0;
			break;
		}

		/* Adjust the length to be read if necessary */
		if (diff < len)
			len = (ssize_t)diff;

		if (vpm_enable) {
			err = vpm_data_copy(vp, off, len, uiop, 1,
			    NULL, 0, S_READ);
		} else {
			segmap_offset = (off & PAGEMASK) & MAXBOFFSET;
			base = segmap_getmapflt(segkmap, vp, off & MAXBMASK,
			    len, 1, S_READ);

			err = uiomove(base + segmap_offset + pageoffset,
			    (long)len, UIO_READ, uiop);
		}

		if (vpm_enable)
			(void) vpm_sync_pages(vp, off, len, 0);
		else
			(void) segmap_release(segkmap, base, 0);
	} while (err == 0 && uiop->uio_resid > 0);

	if (uiop->uio_resid != req_len)
		err = 0;

	return (err);
}

static inline void
fsize_change_notify(struct vnode *vp, size_t fsize, int flag)
{
	VTOFD(vp)->fsize = fsize;
	VTOFD(vp)->file_size_status = flag;
}

/* Main routine which does the actual write operation */
static int
wrfuse(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *credp, caller_context_t *ct)
{
	u_offset_t uoff;
	long	pageoff;	/* offset within a page */
	caddr_t base;		/* base of segmap */
	ssize_t bytes;
	int err;
	int pagecreate, newpage;
	ssize_t premove_resid;
	uint_t flags;
	ulong_t segmap_offset;
	rlim64_t limit = uiop->uio_llimit;
	int file_size_change = 0;
	long start_resid = uiop->uio_resid;	/* save starting resid */
	u_offset_t start_off = uiop->uio_offset; /* save starting offset */
	u_offset_t fsize;

	if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T)
		limit = MAXOFFSET_T;

	if (uiop->uio_loffset >= limit) {
		proc_t *p = ttoproc(curthread);

		mutex_enter(&p->p_lock);
		(void) rctl_action(rctlproc_legacy[RLIMIT_FSIZE], p->p_rctls,
		    p, RCA_UNSAFE_SIGINFO);
		mutex_exit(&p->p_lock);
		return (EFBIG);
	}
	if ((err = fuse_getfilesize(vp, &fsize, credp))) {
		DTRACE_PROBE3(wrfuse_err_filesize,
		    char *, "fuse_getfilesize failed",
		    int, err, struct vnode *, vp);
		return (err);
	}

	if (((ioflag & FAPPEND) != 0) && (vp->v_type == VREG)) {
		uiop->uio_loffset = fsize;
	}

	/*
	 * XXX If we later decide to support DIRECT_IO, its logic should
	 * go here
	 */

	do {
		uoff = uiop->uio_offset;
		pageoff = uoff & (u_offset_t)PAGEOFFSET;
		bytes = MIN(PAGESIZE - pageoff, uiop->uio_resid);

		if (uoff + bytes >= limit) {
			if (uoff >= limit) {
				err = EFBIG;
				goto out;
			}
			bytes = limit - uoff;
		}
		/*
		 * When is it not necessary to read in old page?
		 * Under following cases:
		 * 1. When we are writing one full page with page offset zero.
		 * 2. When the file is yet to be written to or of zero size.
		 * 3. When we are extending the file from a new page
		 */
		pagecreate = (bytes == PAGESIZE) | (fsize == 0) |
		    ((pageoff == 0) && (fsize <= uiop->uio_offset));

		if (uoff + bytes > fsize) {
			file_size_change = 1;
		}

		newpage = 0;
		premove_resid = uiop->uio_resid;

		if (vpm_enable) {
			err = vpm_data_copy(vp, uoff, bytes, uiop, !pagecreate,
			    &newpage, 0, S_WRITE);
		} else {
			segmap_offset = (uoff & PAGEMASK) & MAXBOFFSET;
			base = segmap_getmapflt(segkmap, vp,
			    uoff & MAXBMASK, PAGESIZE, !pagecreate, S_WRITE);
			/*
			 * segmap_pagecreate() returns 1 if it calls
			 * page_create_va() to allocate any pages.
			 */
			if (pagecreate) {
				newpage = segmap_pagecreate(segkmap,
				    base + segmap_offset, (size_t)PAGESIZE, 0);
				/*
				 * clear from the beginning of the page to the
				 * start offset of the data.
				 */
				if (pageoff != 0)
					(void) kzero(base + segmap_offset,
					    (size_t)pageoff);
			}

			err = uiomove(base + segmap_offset + pageoff,
			    (long)bytes, UIO_WRITE, uiop);
			/*
			 * If a new page was created, then we need to make sure
			 * that the page is properly initialized. Take care to
			 * zero out that part of the page where uio_move din't
			 * write valid data.
			 */
			if (pagecreate &&
			    uiop->uio_offset < P2ROUNDUP(uoff + bytes,
			    PAGESIZE)) {
				long zoffset;
				long nmoved;

				nmoved = uiop->uio_offset - uoff;
				ASSERT((nmoved + pageoff) <= PAGESIZE);

				if ((zoffset = pageoff + nmoved) < PAGESIZE)
					(void) kzero(
					    base + segmap_offset + zoffset,
					    (size_t)PAGESIZE - zoffset);
			}
			/*
			 * Unlock the page which have been allocated by
			 * page_create_va() in segmap_pagecreate()
			 */
			if (newpage) {
				segmap_pageunlock(segkmap, base + segmap_offset,
				    (size_t)PAGESIZE, S_WRITE);
			}
		}
		/*
		 * If the file size indeed changed after the
		 * uiomove, record that fact which will be
		 * used in fuse_putpage
		 */
		if (file_size_change) {
			/*
			 * if there was an error than first determine whether
			 * we did extend the file
			 */
			if (err) {
				if ((uoff + (premove_resid - uiop->uio_resid))
				    > fsize) {
					fsize = fsize + (premove_resid -
					    uiop->uio_resid);
					(void) fsize_change_notify(vp, fsize,
					    FSIZE_UPDATED);
				}
			} else {
				fsize = uoff + bytes;
				(void) fsize_change_notify(vp, fsize,
				    FSIZE_UPDATED);
			}
		}

		if (err) {
			DTRACE_PROBE2(wrfuse_err_write_fail,
			    char *, "write error, invalidate pages",
			    struct uio *, uiop);

			/*
			 * If we did not allocate a new page and if the file
			 * size remained unchanged, then we need to destroy
			 * the page where write failed so that we don't end up
			 * flushing invalid data
			 */
			if (file_size_change == 0 && newpage == 0) {
				/*
				 * Similar to UFS, we unwind what uiomove
				 * last did
				 */
				uiop->uio_resid = premove_resid;
				flags = SM_DESTROY;
			} else {
				/*
				 * Remove the page from the cache, if the page
				 * is dirty it would be written back
				 */
				flags = SM_INVAL;
			}
			/*
			 * If we failed on a write, we must
			 * be sure to invalidate any pages that may have
			 * been allocated.
			 */
			if (vpm_enable) {
				(void) vpm_sync_pages(vp, uoff, PAGESIZE,
				    flags);
			} else {
				(void) segmap_release(segkmap, base, flags);
			}
		} else {
			if (vpm_enable) {
				err = vpm_sync_pages(vp, uoff, PAGESIZE, 0);
			} else {
				err = segmap_release(segkmap, base, 0);
			}
		}
	} while (uiop->uio_resid > 0 && err == 0 && bytes != 0);
out:
	/* if we did write any data, then try flushing it out to daemon */
	if (start_resid != uiop->uio_resid) {
		err = VOP_PUTPAGE(vp, (offset_t)(start_off & PAGEMASK),
		    (size_t)0, 0, credp, ct);
	}

	return (err);
}

/*
 * Write of devices (leaf nodes) is handled by specfs.
 * Write of directories is not supported.
 */
/* ARGSUSED */
static int
fuse_write(struct vnode *vp, struct uio *uiop, int ioflag,
    struct cred *credp, struct caller_context *ct)
{
	/* Check for valid file types */
	if (vp->v_type != VREG && vp->v_type != VLNK)
		return (EIO);

	if (uiop->uio_loffset < (offset_t)0)
		return (EINVAL);

	if (uiop->uio_resid == 0)
		return (0);

	return (wrfuse(vp, uiop, ioflag, credp, ct));
}

static void fuse_set_getattr(struct vnode *vp, struct vattr *vap,
    struct fuse_attr *attr)
{
	vap->va_mode = attr->mode & MODEMASK;
	vap->va_size = attr->size;
	vap->va_atime.tv_sec = attr->atime;
	vap->va_atime.tv_nsec = attr->atimensec;
	vap->va_mtime.tv_sec = attr->mtime;
	vap->va_mtime.tv_nsec = attr->mtimensec;

	vap->va_uid = attr->uid;
	vap->va_gid = attr->gid;
	vap->va_nlink = attr->nlink;
	vap->va_ctime.tv_sec = attr->ctime;
	vap->va_ctime.tv_nsec = attr->ctimensec;

	vap->va_mask = AT_ALL;
	vap->va_type = vp->v_type;
	vap->va_nodeid = VNODE_TO_NODEID(vp);

	vap->va_rdev = vp->v_rdev;
	vap->va_blksize = vp->v_vfsp->vfs_bsize;

	vap->va_nblocks = attr->blocks;
	/* TBD: What value should we set here ? */
	vap->va_seq = 0;
	vap->va_fsid = vp->v_vfsp->vfs_dev;
}

static int
fuse_getattr_from_daemon(struct vnode *vp, struct vattr *vap,
    cred_t *credp, fuse_msg_node_t **msgpp)
{
	int err;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_getattr_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	msgp = fuse_alloc_msg();

	/*
	 * setup the message with only header and no arguments to be sent to
	 * fuse library
	 */
	setup_msg_onlyheader(msgp, FUSE_GETATTR, FUSE_GET_UNIQUE(sep),
	    VNODE_TO_NODEID(vp), credp);


	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto out;
	}

	/* We got woken up, so fuse library has replied to our request */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_getattr_err_getattr_req,
		    char *, "FUSE_GETATTR request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto out;
	}

	if (msgp->opdata.outsize != sizeof (struct fuse_attr_out)) {
		DTRACE_PROBE2(fuse_getattr_err_inval_resp,
		    char *, "FUSE_GETATTR response invalid size",
		    size_t, msgp->opdata.outsize);
		err = EINVAL;
		goto out;
	}
	fuse_set_getattr(vp, vap,
	    &((struct fuse_attr_out *)msgp->opdata.outdata)->attr);

	if (msgpp) {
		/* It's caller's responsibility to free the msg node */
		*msgpp = msgp;
		return (err);
	}

out:
	fuse_free_msg(msgp);
	return (err);
}

/*
 * The flags parameter is generally passed as zero from most syscalls to
 * VOP_GETATTR
 */
/* ARGSUSED */
static int
fuse_getattr(struct vnode *vp, struct vattr *vap, int flags, cred_t *credp,
    caller_context_t *ct)
{
	int err = DDI_SUCCESS;
	fuse_msg_node_t *msgp = NULL;
#ifndef DONT_CACHE_ATTRIBUTES
	timestruc_t ts;
#endif

#ifndef DONT_CACHE_ATTRIBUTES
	gethrestime(&ts);
	if (ATTR_CACHE_VALID(ts, VTOFD(vp)->cached_attrs_bound)) {
		(void) memcpy(vap, &(VTOFD(vp)->cached_attrs), sizeof (*vap));
		DTRACE_PROBE2(fuse_getattr_info_cached,
		    char *, "Using cached attributes", struct vnode *, vp);
		return (0);
	}
#endif
	err = fuse_getattr_from_daemon(vp, vap, credp, &msgp);

	if (err)
		return (err);

#ifndef DONT_CACHE_ATTRIBUTES
	cache_attrs(vp, (struct fuse_attr_out *)(msgp->opdata.outdata));
#endif

out:
	fuse_free_msg(msgp);
	return (err);
}


/*
 * The flags parameter is generally passed as zero from most syscalls to
 * VOP_SETATTR.
 */
/* ARGSUSED */
static int
fuse_setattr(
    struct vnode *vp,
    struct vattr *vap,
    int flags,
    struct cred *credp,
    caller_context_t *ct)
{
	int err = 0;
	fuse_msg_node_t *msgp;
	struct fuse_setattr_in *fsai;
	struct vattr va;
	enum vtype vtyp;
	long int mask = vap->va_mask;
	fuse_session_t *sep;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_setattr_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	/* Check for unsettable attributes */
	if (mask & AT_NOSET)
		return (EINVAL);

	mutex_enter(&VTOFD(vp)->f_lock);

	if (err = VOP_GETATTR(vp, &va, flags, credp, ct))
		goto cleanup2;

	/* Let kernel do the appropriate access checks */
	err = secpolicy_vnode_setattr(credp, vp, vap, &va, flags,
	    fuse_access_i, vp);

	if (err)
		goto cleanup2;

	mask = vap->va_mask;

	msgp = fuse_setup_message(sizeof (*fsai), FUSE_SETATTR,
	    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep));

	fsai = msgp->ipdata.indata;

/*
 * In case we plan to support lower versions of fuse library this macro
 * can be changed
 */
#define	FUSEATTR(x) x

	if (mask & AT_UID) {
		fsai->FUSEATTR(uid) = vap->va_uid;
		fsai->valid |= FATTR_UID;
	}

	if (mask & AT_GID) {
		fsai->FUSEATTR(gid) = vap->va_gid;
		fsai->valid |= FATTR_GID;
	}

	if (mask & AT_MODE) {
		va.va_mode &= S_IFMT;
		va.va_mode |= vap->va_mode & ~S_IFMT;
		fsai->FUSEATTR(mode) = va.va_mode;
		fsai->valid |= FATTR_MODE;
	}

	if (mask & AT_ATIME) {
		fsai->FUSEATTR(atime) = vap->va_atime.tv_sec;
		fsai->FUSEATTR(atimensec) = vap->va_atime.tv_nsec;
		fsai->valid |= FATTR_ATIME;
	}

	if (mask & AT_MTIME) {
		fsai->FUSEATTR(mtime) = vap->va_mtime.tv_sec;
		fsai->FUSEATTR(mtimensec) = vap->va_mtime.tv_nsec;
		fsai->valid |= FATTR_MTIME;
	}

	if (mask & AT_SIZE) {
		fsai->FUSEATTR(size) = vap->va_size;
		fsai->valid |= FATTR_SIZE;
	}
	/* if we received a signal or daemon replied with an error */
	if ((err = fuse_queue_request_wait(sep, msgp)) != 0) {
		goto cleanup;
	}
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_setattr_err_setattr_req,
		    char *, "FUSE_SETATTR request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
#ifndef DONT_CACHE_ATTRIBUTES
		invalidate_cached_attrs(vp);
#endif
		goto cleanup;
	}
	vtyp =
	    IFTOVT(((struct fuse_attr_out *)msgp->opdata.outdata)->attr.mode);

	if (vp->v_type != vtyp) {
		if (vp->v_type == VNON) {
			vp->v_type = vtyp;
		} else {
			VOP_INACTIVE(vp, credp, ct);
			err = ENOTCONN;
			goto cleanup;
		}
	}
#ifndef DONT_CACHE_ATTRIBUTES
	cache_attrs(vp, (struct fuse_attr_out *)msgp->opdata.outdata);
#endif

cleanup:
	fuse_free_msg(msgp);
cleanup2:
	mutex_exit(&VTOFD(vp)->f_lock);
	return (err);
}
/*
 * vnode operation to support POSIX link()
 * The first param is the vnode of the dir containing the file, second param is
 * the vnode of the file to hard link, the third param is the new name to be
 * given.
 */
/* ARGSUSED */
static int
fuse_link(struct vnode *dvp, struct vnode *srcvp, char *tnm,
    struct cred *credp, caller_context_t *ct, int flags)
{
	struct fuse_link_in *fli;
	struct fuse_entry_out *feo;
	fuse_msg_node_t *msgp;
	char *name;
	fuse_session_t *sep;
	int err = DDI_SUCCESS;
	int nmlen = strlen(tnm) + 1;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_link_err_session,
		    char *, "failed to find session",
		    struct vnode *, dvp);
		return (ENODEV);
	}

	msgp = fuse_setup_message(sizeof (*fli) + nmlen, FUSE_LINK,
	    VNODE_TO_NODEID(dvp), credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	fli = (struct fuse_link_in *)msgp->ipdata.indata;
	fli->oldnodeid = VNODE_TO_NODEID(srcvp);
	name = (char *)msgp->ipdata.indata + sizeof (*fli);
	(void *) strlcpy(name, tnm, nmlen);
	name[nmlen - 1] = '\0';

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* We got woken up, so fuse lib has replied to our release request */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_link_err_link_req,
		    char *, "FUSE_LINK request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

	feo = (struct fuse_entry_out *)msgp->opdata.outdata;

	if ((err = checkentry(feo, srcvp->v_type))) {
		goto cleanup;
	}
#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(dvp);
	/*
	 * To be on the safer side we disregard the cached attribute for the
	 * original so that fs gets a chance to pass on updated attributes next
	 * time we need
	 */
	/* See the comments at the same place in FreeBSD Fuse kernel module */
	invalidate_cached_attrs(srcvp);
#endif
	VTOFD(srcvp)->nlookup++;

cleanup:
	fuse_free_msg(msgp);
	return (err);
}

/* unistd.h */
/* Symbolic constants for the "access" routine: */
#define	R_OK	4	/* Test for Read permission */
#define	W_OK	2	/* Test for Write permission */
#define	X_OK	1	/* Test for eXecute permission */
#define	F_OK	0	/* Test for existence of File */

/* ARGSUSED */
static int
fuse_access(struct vnode *vp, int mode, int flags, struct cred *credp,
    caller_context_t *ct)
{
	return (fuse_access_i((void *)vp, mode, credp));
}

static int
fuse_access_i(void *vvp, int mode, struct cred *credp)
{
	struct vnode *vp = vvp;
	struct fuse_access_in *fai;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;
	int err = 0;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_access_i_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	/*
	 * TBD: Check if it is mounted readonly and return error on
	 * Write access
	 */

	/*
	 * According to FreeBSD Fuse, if default permission option is
	 * set while mounting or if a regular file is checked for executing
	 * we have to do a in-kernel check. Since we don't have mount options
	 * support yet, we just check for execute mode
	 */
	if (vp->v_type == VREG && mode == VEXEC) {
		return (fuse_access_inkernelcheck(vp, mode, credp));
	} else {
		msgp = fuse_setup_message(sizeof (*fai), FUSE_ACCESS,
		    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep));

		fai = (struct fuse_access_in *)msgp->ipdata.indata;

		fai->mask = F_OK;
		if (mode & VREAD)
			fai->mask |= R_OK;
		if (mode & VWRITE)
			fai->mask |= W_OK;
		if (mode & VEXEC)
			fai->mask |= X_OK;

		/*
		 * queue the message for sending to userland filesystem
		 * lib framework
		 */
		err = fuse_queue_request_wait(sep, msgp);
		if (!(err)) {
			/*
			 * We got woken up, so fuse library has replied to
			 * our request
			 */
			err = msgp->opdata.fouth->error;
			if (err == ENOSYS) {
				DTRACE_PROBE2(fuse_access_i_info_enosys,
				    char *,
				    "FUSE_ACCESS not supported by filesys",
				    struct fuse_out_header *,
				    msgp->opdata.fouth);
				/*
				 * If the userspace filesystem has not provided
				 * an implementation we just return SUCCESS to
				 * the kernel
				 */
				err = 0;
			} else {
				DTRACE_PROBE2(fuse_access_i_err_access_req,
				    char *, "FUSE_ACCESS request failed",
				    struct fuse_out_header *,
				    msgp->opdata.fouth);
			}
		}
		fuse_free_msg(msgp);
		return (err);
	}
}

/* Performs the basic access check w.r.t. owner, group and public permissions */
#define	MODESHIFT	3
static int
fuse_access_inkernelcheck(void *vvp, int mode, struct cred *credp)
{
	int shift = 0;
	struct vnode *vp = (struct vnode *)vvp;
	int err = 0;
	struct vattr va;

	if (err = VOP_GETATTR(vp, &va, 0, credp, NULL))
		return (err);

	/* Check access based on owner, group and public permissions */
	if (crgetuid(credp) != va.va_uid) {
		shift += MODESHIFT;
		if (groupmember(va.va_gid, credp))
			shift += MODESHIFT;
	}
	/* Compute missing mode bits */
	mode &= ~(va.va_mode << shift);

	if (mode == 0)
		return (0);

	return (secpolicy_vnode_access(credp, vp, va.va_uid, mode));
}

/*
 * Logic is from UFS.
 * Decide whether it is okay to remove within a sticky directory.
 * Two conditions need to be met:  write access to the directory
 * is needed.  In sticky directories, write access is not sufficient;
 * you can remove entries from a directory only if you own the directory,
 * if you are privileged, if you own the entry or if the entry is
 * a plain file and you have write access to that file.
 * Function returns 0 if remove access is granted.
 */
static int
fuse_sticky_remove_access(struct vnode *dvp, struct vnode *vp, struct cred *cr,
    caller_context_t *ct)
{
	struct vattr dva;
	struct vattr va;
	uid_t uid;
	int err;

	/*
	 * Ah! We will have to request daemon for both source and its parent
	 * directory vnode attributes. We try getting attributes for source
	 * before its par directory since there is a better chance that we
	 * might be able to sneak in and get the attributes from the cache.
	 */
	if (err = VOP_GETATTR(vp, &va, 0, cr, ct))
		return (err);

	if (err = VOP_GETATTR(dvp, &dva, 0, cr, ct))
		return (err);

	/* Perform the check */
	if ((dva.va_mode & VSVTX) &&
	    (uid = crgetuid(cr)) != dva.va_uid &&
	    uid != va.va_uid &&
	    (va.va_mode != VREG ||
	    fuse_access_i((void *)vp, VWRITE, cr) != 0))
		return (secpolicy_vnode_remove(cr));

	return (0);
}

/*
 * Lookup
 *
 * Given the directory vnode and the name of the component, return
 * the corresponding held vnode for that component.
 */
/* ARGSUSED */

static int fuse_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
    struct pathname *pnp, int flags, struct vnode *rdir, struct cred *credp,
    caller_context_t *ct, int *direntflags, pathname_t *realpnp)
{
	int err = 0;
	fuse_session_t *sep;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_lookup_err_session,
		    char *, "failed to find session",
		    struct vnode *, dvp);
		return (ENODEV);
	}

	if (dvp->v_type != VDIR) {
		err = ENOTDIR;
		goto out;
	}

	/*
	 * if this is the root of our filesystem, we will check
	 * for valid access permissions
	 */
	if ((VTOFD(dvp))->nodeid == FUSE_ROOT_ID) {
		if (err = fuse_access(dvp, VEXEC, flags, credp, ct)) {
			DTRACE_PROBE2(fuse_lookup_err_access,
			    char *, "fuse_access failed",
			    struct vnode *, dvp);
			goto out;
		}
	}

	/* Check for NULL name and "." */
	if (nm[0] == '\0' || ((nm[0] == '.') && (nm[1] == '\0'))) {
		VN_HOLD(dvp);
		*vpp = dvp;
		goto out;
	}

	/* Check if lookup needs to traverse beyond our filesystem */
	if (nm[0] == '.' && nm[1] == '.' && nm[2] == '\0') {
		if (VTOFD(dvp)->nodeid == FUSE_ROOT_ID) {
			if (dvp->v_vfsp->vfs_vnodecovered) {
				err = VOP_LOOKUP(dvp->v_vfsp->vfs_vnodecovered,
				    nm, vpp, pnp, flags, rdir, credp, ct,
				    direntflags, realpnp);
			} else {
				DTRACE_PROBE2(fuse_lookup_err_vnodecovered,
				    char *, "vnode covered is NULL",
				    struct vnode *, dvp);
				err = EIO;
			}
		} else if (VTOFD(dvp)->par_nid == FUSE_ROOT_ID) {
			err = VFS_ROOT(dvp->v_vfsp, vpp);
		} else if (VTOFD(dvp)->par_nid != FUSE_NULL_ID) {
			err = fuse_getvnode(VTOFD(dvp)->par_nid, vpp,
			    VNODE_CACHED, VDIR, sep, dvp->v_vfsp, 0, NULL,
			    FUSE_NULL_ID, credp);
			/*
			 * We don't seem to have the parent vnode cached, so
			 * we need to request daemon then
			 */
			if (err == ENOENT) {
				goto request_daemon;
			}
		} else {
			/*
			 * If we don't have a valid parent nodeid then there
			 * isn't much we can do, so we return error to the user
			 */
			DTRACE_PROBE2(fuse_lookup_err_parent,
			    char *, "No valid parent nodeid",
			    struct vnode *, dvp);
			err = EIO;
		}
		goto out;
	}

request_daemon:
	err = fuse_lookup_i(dvp, nm, vpp, credp);
out:
	return (err);
}


/*
 * This will free the private data allocated for a vnode. The caller should
 * ensure all the file handles associated with the passed vnode has been
 * released appropriately before invoking this function
 */
static void
fuse_free_vdata(struct vnode *vp)
{
	fuse_vnode_data_t *fvdatap = VTOFD(vp);
	list_destroy(&fvdatap->fh_list);
	mutex_destroy(&fvdatap->fh_list_lock);
	mutex_destroy(&fvdatap->f_lock);
	kmem_free(fvdatap, sizeof (*fvdatap));
}

/*
 * This routine frees a vnode and ensures that appropriate cleanups is taken
 * care of before returning it to the kernel
 */
static void
fuse_vnode_destroy(struct vnode *vp, struct cred *credp, fuse_session_t *sep)
{
	fuse_vnode_cleanup(vp, credp, sep);

	if (VTOFD(vp))
		fuse_free_vdata(vp);
	vp->v_data = NULL;

	vn_free(vp);
}

static int
fuse_getvnode(uint64_t nodeid, struct vnode **vpp, v_getmode vmode,
    uint32_t mode, fuse_session_t *sep, vfs_t *vfsp, int namelen,
    char *name, uint64_t parent_nid, struct cred *credp)
{
	fuse_avl_cache_node_t tofind, *foundp;
	fuse_avl_cache_node_t *avl_nodep;
	int create_new = 0;

	tofind.facn_nodeid = nodeid;
	if ((foundp = avl_find(&(sep->avl_cache), &tofind,
	    NULL)) != NULL) {
		*vpp = foundp->facn_vnode_p;
		ASSERT(*vpp);
		/*
		 * If we need a new vnode, then try to destroy the previously
		 * cached vnode
		 */
		if (vmode == VNODE_NEW) {
			(void) fuse_vnode_destroy(*vpp, credp, sep);
			create_new = 1;
		} else {
			VN_HOLD(*vpp);
		}
	} else {
		/*
		 * If the caller was looking for the previously cached vnode
		 * then return an error since we din't find it in the AVL tree
		 */
		if (vmode == VNODE_CACHED) {
			return (ENOENT);
		}
		/*
		 * Create and add the vnode in AVL cache
		 */
		create_new = 1;
	}
	if (create_new) {
		*vpp = fuse_create_vnode(vfsp, nodeid, parent_nid,
		    mode, OTHER_OP);

		avl_nodep = fuse_avl_cache_node_create(
		    *vpp, nodeid, parent_nid, namelen, name);
		avl_add(&(sep->avl_cache), avl_nodep);
	}
	VTOFD(*vpp)->nlookup++;
	return (0);
}

static int
fuse_lookup_i(struct vnode *dvp, char *nm, struct vnode **vpp, cred_t *credp)
{
	int err = 0;
	int nmlen = 0;
	uint64_t nodeid;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;
	struct fuse_entry_out *feo;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_lookup_i_err_session,
		    char *, "failed to find session",
		    struct vnode *, dvp);
		return (ENODEV);
	}

	nmlen = strlen(nm) + 1;

	/* Be cautions against a possible attack */
	if (nmlen > MAXNAMELEN) {
		DTRACE_PROBE2(fuse_lookup_i_err_nametoolong,
		    char *, "name too long", char *, nm);
		return (ENAMETOOLONG);
	}

	/* Need to send a FUSE_LOOKUP message to the fuse library */
	msgp = fuse_setup_message(nmlen, FUSE_LOOKUP, VNODE_TO_NODEID(dvp),
	    credp, FUSE_GET_UNIQUE(sep));

	(void *) strlcpy(msgp->ipdata.indata, nm, nmlen);

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		fuse_free_msg(msgp);
		return (err);
	}

	/* We got woken up, so fuse library has replied to our Lookup request */

	/* Check for any error from fuse library */
	if ((err = msgp->opdata.fouth->error) == 0) {
		/*
		 * If there was no error from the fuse library, then verify
		 * obtained nodeid
		 */
		feo = (struct fuse_entry_out *)msgp->opdata.outdata;
		nodeid = feo->nodeid;
		if (!nodeid) {
			err = ENOENT;
		} else if (nodeid == FUSE_ROOT_ID) {
			err = EIO;
		} else { /* Valid nodeid perhaps */
			err = fuse_getvnode(nodeid, vpp, VNODE_ANY,
			    IFTOVT(feo->attr.mode), sep, dvp->v_vfsp, nmlen, nm,
			    VNODE_TO_NODEID(dvp), credp);

			if (err) {
				fuse_send_forget(nodeid, sep, 1);
			} else {
#ifndef DONT_CACHE_ATTRIBUTES
				cache_attrs((*vpp), feo);
#endif

				/*
				 * Convert device special files
		 		 */
				if (IS_DEVVP(*vpp)) {
					vnode_t	*svp;

					svp = specvp(*vpp, (*vpp)->v_rdev,
					    (*vpp)->v_type, credp);
					VN_RELE(*vpp);
					if (svp == NULL)
						err = ENOSYS;
					else
						*vpp = svp;
				}
			}
		}
	} else {
		DTRACE_PROBE2(fuse_lookup_i_err_lookup_req,
		    char *, "FUSE_LOOKUP request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
	}

	fuse_free_msg(msgp);
	return (err);
}

/*
 * The following comments are from (OpenSolaris) ZFS :
 * Remove a directory subdir entry. If the current working
 * directory is the same as the subdir to be removed, the
 * remove will fail. [BUT fuse has no way to know if cwd is
 *	 subdir of dvp as we do not maintain the dir tree in kernel]
 *
 *	IN:	dvp	- vnode of directory to remove from.
 *		name	- name of directory to be removed.
 *		cwd	- vnode of current working directory.
 *		cr	- credentials of caller.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 */
/* ARGSUSED */
static int
fuse_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *credp,
    caller_context_t *ct, int flags)
{
	int err = 0;
	fuse_session_t *sep;
	fuse_msg_node_t *msgp;
	struct vnode *vp = NULL;
	int namelen = strlen(name) + 1;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_rmdir_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	if (dvp->v_type != VDIR)
		return (ENOTDIR);

	/* . and .. cannot be removed */
	if (name[0] == '.') {
		if (name[1] == '\0') {
			return (EINVAL);
		} else if (name[1] == '.' && name[2] == '\0') {
			return (EEXIST);
		}
	}

	err = fuse_getvnode(FUSE_NULL_ID, &vp, VNODE_CACHED,
	    0, sep, dvp->v_vfsp, namelen, name, VNODE_TO_NODEID(dvp), credp);

	if (err) {
		/*
		 * This means, we have never seen this directory before, so
		 * we don't have its vnode, just send the message to daemon
		 */
		if (err == ENOENT) {
			goto sendmsg;
		} else {
			/* This shouldn't occur */
			DTRACE_PROBE3(fuse_rmdir_err_getvnode,
			    char *, "getvnode returned error",
			    int, err, uint64_t, FUSE_NULL_ID);
			return (err);
		}
	}
	ASSERT(vp != NULL);

	/*
	 * XXX: We have no way to know if cwd is subdir of dvp as we do not
	 * maintain the dir tree in kernel
	 */

	if (vp == cwd || vp == dvp) {
		err = EINVAL;
		goto out;
	}

	if (vp->v_type != VDIR) {
		err = ENOTDIR;
		goto out;
	}

	if (vn_vfswlock(vp)) {
		err = EBUSY;
		goto out;
	}

	if (vn_mountedvfs(vp)) {
		vn_vfsunlock(vp);
		err = EBUSY;
		goto out;
	}

sendmsg:
	msgp = fuse_setup_message(namelen, FUSE_RMDIR, VNODE_TO_NODEID(dvp),
	    credp, FUSE_GET_UNIQUE(sep));

	(void *) strlcpy(msgp->ipdata.indata, name, namelen);

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* We got woken up, so fuse library has replied to our Lookup request */
	/* Check for any error from fuse library */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_rmdir_err_rmdir_req,
		    char *, "FUSE_RMDIR request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}
	if (vp) {
		/*
		 * If all went well, we don't need this vnode any more so
		 * we will get rid of it
		 */
		/*
		 * XXX: This call releases vnode with vn_free, can this
		 * be an issue? Or does VN_RELE do the required job for us?
		 */
		fuse_vnode_free(vp, sep);
		vp = NULL;
	}

#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(dvp);
#endif

cleanup:
	fuse_free_msg(msgp);
out:	if (vp)
		VN_RELE(vp);
	return (err);
}

/*
 * the (OpenSolaris)devfs prototype was used in this func.
 *
 * - any create fails if the node doesn't exist - EROFS.
 * - creating an existing directory read-only succeeds, otherwise EISDIR.
 * - exclusive creates fail if the node already exists - EEXIST.
 * - failure to create the snode for an existing device - ENOSYS.
 */
/*
 * Attempt to create a new entry in a directory. If the entry
 * already exists, truncate the file if permissible, else return
 * an error. Return the vp of the created or trunc'd file.
 *
 *	IN:	dvp	- vnode of directory to put new file entry in.
 *		name	- name of new file entry.
 *		vap	- attributes of new file.
 *		excl	- flag indicating exclusive or non-exclusive mode.
 *		mode	- mode to open file with.
 *		cr	- credentials of caller.
 *		flag	- creat() param is passed here [filemode &
 *                        ~(FTRUNC|FEXCL)]
 *
 *	OUT:	vpp	- vnode of created or trunc'd entry.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 * Timestamps:
 *	dvp - ctime|mtime updated if new entry created
 *	 vp - ctime|mtime always, atime if new
 */
/* ARGSUSED */
static int
fuse_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
    int mode, struct vnode **vpp, struct cred *cred_p, int flag,
    caller_context_t *ct, vsecattr_t *va)
{
	struct fuse_vnode_data *fvdata;
	uint32_t stringlen;

	if (!nm || *nm == '\0') {
		DTRACE_PROBE2(fuse_create_err_invalid_name,
		    char *, "Invalid filename", char *, nm);
		return (EEXIST);
	}

	if (vap->va_type == VDIR) {
		return (VOP_MKDIR(dvp, nm, vap, vpp, cred_p, ct, 0, va));
	}

	if (vap->va_type != VREG) {
		DTRACE_PROBE2(fuse_create_err_invalid_type,
		    char *, "Invalid file type",
		    struct vattr *, vap);
		return (EINVAL);
	}

	int lku = fuse_lookup_i(dvp, nm, vpp, cred_p);
	if (*vpp)
		VN_RELE(*vpp);
	if (!lku) {
		return (0);
	}

	/*
	 * We follow the approach as in FreeBSD Fuse and provide atomic
	 * create + open. So this create method for a regular file doesn't send
	 * a request to user space fuse lib, rather its done in the open method
	 */
	*vpp = fuse_create_vnode(dvp->v_vfsp, FUSE_NULL_ID,
	    VNODE_TO_NODEID(dvp), vap->va_type, FILE_CREATE_OP);
	fvdata = (struct fuse_vnode_data *)(*vpp)->v_data;

	/* Copy the needed parameters, to be used in open() */
	crhold(cred_p);
	fvdata->fcd->credp	= cred_p;
	fvdata->fcd->flag	= flag;
	fvdata->fcd->mode	= MAKEIMODE(vap->va_type, vap->va_mode);
	fvdata->fcd->par_nodeid	= VNODE_TO_NODEID(dvp);
	stringlen		= strlen(nm) + 1;

	/* Be cautious against any possible attacks */
	if (stringlen > MAXNAMELEN) {
		stringlen = MAXNAMELEN;
	}

	fvdata->fcd->namelen	= stringlen;
	fvdata->fcd->name	= kmem_alloc(stringlen, KM_SLEEP);
	(void *) strlcpy(fvdata->fcd->name, nm, stringlen);
#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(dvp);
#endif

	return (0);
}

/*
 * This function consumes directory entries sent by Fuse library in the form
 * of fuse_direct structures. It creates equivalent dirent64 entries which is
 * the form expected by Solaris Kernel. The logic of this function is similar
 * to FreeBSD Fuse function fuse_dir_buffeater()
 */
/* ARGSUSED */
static int
fuse_cons_dir(struct uio *uiop, size_t reqsize, void *buf, size_t bufsize,
    void *arg)
{
	struct dirent64 *de;
	struct fuse_dirent *fudge;
	int err = 0;
	size_t freclen;
	int cou = 0;
	unsigned short bytesavail;
	struct fuse_iov iov, *iovp = &iov;

	ASSERT(bufsize <= reqsize);

	/*
	 * Sanity check: if this fails, we would overrun the allocated space
	 * upon entering the loop below, so we'd better leave right now.
	 * If so, we return -1 to terminate reading.
	 */
	if (bufsize < FUSE_NAME_OFFSET)
		return (-1);

	/*
	 * We preallocate a buffer of maximum dirent size so that we don't have
	 * to frequently free and alloc again.
	 */
	fuse_buf_alloc(iovp, MAX_DENTRY64_SIZE);

	de = (struct dirent64 *)iovp->base;
	/*
	 * The following piece of code is from FreeBSD Fuse with slight
	 * modification
	 */
	/*
	 * Can we avoid infite loops? An infinite loop could occur only if we
	 * leave this function with 0 return value, because otherwise we wont't
	 * be called again. But both 0 exit points imply that some data has
	 * been consumed... because
	 *   1) if a turn is not aborted, it consumes positive amount of data
	 *   2) the 0 jump-out from within the loop can't occur in the first
	 *	  turn
	 *   3) if we exit 0 after the loop is over, then at least one turn
	 *	  was completed, otherwise we hed exited above with -1.
	 */

	for (;;) {
		if (bufsize < FUSE_NAME_OFFSET) {
			err = -1;
			break;
		}

		cou++;
		fudge = (struct fuse_dirent *)buf;
		freclen = FUSE_DIRENT_SIZE(fudge);

		/*
		 * Here is an exit condition: we terminate the whole reading
		 * process if a fresh chunk of buffer is already too short to
		 * cut out an entry.
		 * (It it was not the first turn in the loop, nevermind,
		 * return with asking for more)
		 */
		if (bufsize < freclen) {
			err = ((cou == 1) ? -1 : 0);
			break;
		}

		/* Sanity checks */
		if (!fudge->namelen || fudge->namelen > MAXNAMELEN) {
			DTRACE_PROBE2(fuse_cons_dir_err_invalid_name,
			    char *, "Invalid name length",
			    struct fuse_dirent *, fudge);
			err = EIO;
			break;
		}

		bytesavail = DIRENT64_RECLEN(fudge->namelen);

		/*
		 * Exit condition 2: if the pretended amount of input is more
		 * than that the userspace wants, then it's time to stop
		 * reading.
		 */
		if (bytesavail > uiop->uio_resid) {
			err = -1;
			break;
		}

		/*
		 * Verify if size allocated is sufficient for this record, if
		 * not allocate as much as we need
		 */
		if (bytesavail > iovp->memsize) {
			kmem_free(iovp->base, iovp->memsize);
			fuse_buf_alloc(iovp, bytesavail);
		}

		de = (struct dirent64 *)iovp->base;

		de->d_ino = fudge->ino;
		de->d_reclen = bytesavail;
		(void *) memcpy((char *)iovp->base + DENTRY64_NAME_OFFSET,
		    (char *)buf + FUSE_NAME_OFFSET, fudge->namelen);

		((char *)iovp->base)[DENTRY64_NAME_OFFSET + fudge->namelen] =
		    '\0';

		err = uiomove(iovp->base, bytesavail, UIO_READ, uiop);

		if (err)
			break;

		buf = (char *)buf + freclen;
		bufsize -= freclen;

		uiop->uio_loffset = fudge->off;
	}

	return (err);
}

/*
 * This function performs a generic read by sending repeated requests to the
 * fuse library and waiting for a response until the desired amount of data
 * is not obtained. It invokes the call back function initialized by the caller
 * after every response from the fuse library, which in turn consumes the data
 * The logic of this function is similar to FreeBSD Fuse function:
 * fuse_direct_backend()
 */
static int
fuse_perform_read(struct fuse_io_data *fiodata)
{
	struct vnode		*vp = fiodata->vp;
	struct fuse_file_handle *fh = fiodata->fh;
	struct uio 		*uiop = fiodata->uiop;
	cred_t 			*credp = fiodata->credp;
	enum fuse_opcode 	op = fiodata->op;
	fuse_consfunc_t		*consfunc = fiodata->consfunc;
	void			*arg = fiodata->arg;
	fuse_msg_node_t		*msgp;
	fuse_session_t		*sep;
	struct fuse_read_in	*fri;
	int			err = 0;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_perform_read_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	if (uiop->uio_resid == 0)
		return (0);

	msgp = fuse_setup_message(sizeof (*fri), op,
	    VNODE_TO_NODEID(vp), credp, FUSE_GET_UNIQUE(sep));

	while (uiop->uio_resid > 0) {

		/* Set up arguments to the fuse library */
		fri = (struct fuse_read_in *)msgp->ipdata.indata;
		fri->fh = fh->fh_id;
		fri->offset = uiop->uio_offset;
		fri->size = MIN(uiop->uio_resid,
		    PAGESIZE * FUSE_MAX_PAGES_PER_REQ);

		if ((err = fuse_queue_request_wait(sep, msgp))) {
			goto cleanup;
		}

		/* We got woken up, so fuse lib has replied to our request */
		if ((err = msgp->opdata.fouth->error) != 0) {
			DTRACE_PROBE2(fuse_perform_read_err_read_req,
			    char *, "FUSE_READ request failed",
			    struct fuse_out_header *, msgp->opdata.fouth);
			goto cleanup;
		}

		if ((err = consfunc(uiop, fri->size, msgp->opdata.iovbuf.base,
		    msgp->opdata.outsize, arg)))
			break;

		/* Refresh the message node if necessary */
		if (uiop->uio_resid)
			fuse_msg_recreate(msgp, sizeof (*fri), op,
			    VNODE_TO_NODEID(vp), credp,
			    FUSE_GET_UNIQUE(sep), 0);
	}

cleanup:
	fuse_free_msg(msgp);
	return ((err == -1) ? 0 : err);
}

/* ARGSUSED */
static int
fuse_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred_p,
    int *eof_p, caller_context_t *ct, int flags)
{
	int err = 0;
	struct fuse_file_handle *fh = NULL;
	struct fuse_io_data fiodata;

	if (uiop->uio_loffset < 0 || uiop->uio_resid <= 0)
		return (ENOENT);

	if (uiop->uio_iovcnt != 1)
		return (EINVAL);

	if (dvp->v_type != VDIR)
		return (ENOTDIR);
	/*
	 * the readdir() libc is called in a loop, the syscall readdir calls
	 * getdent64 [usr/src/uts/common/syscall/getdents.c#190] getdents64()
	 * takes first param as fd, from which it gets the File pointer and
	 * getsvnode from it. This vnode's call back is mapped to this
	 * filesystem, thus this function gets called. The function getdents64()
	 * passes the last offset in uio_loffset, which we must check to figure
	 * out if EOF has been reached. It ignores the last eof pointer and
	 * instead figures out that EOF has reached if uio_resid has not been
	 * changed.
	 */
#if 0 /* TBD: Verify if this is needed */
	if ((uiop->uio_loffset != 0) && (uiop->uio_loffset >=
	    ((fuse_vnode_data_t *)dvp->v_data)->fileh.fh_offset)) {
		if (eof_p)
			*eof_p = 1;
		return (0);
	}
#endif

	/* Get File handle by checking through the cached file handle list */
	if ((err = get_filehandle(dvp, FREAD, cred_p, &fh, CACHE_LIST_CHECK))) {
		DTRACE_PROBE2(fuse_readdir_err_filehandle,
		    char *, "get_filehandle failed",
		    struct vnode *, dvp);
		goto out;
	}

	if (!fh) {
		DTRACE_PROBE2(fuse_readdir_err_filehandle,
		    char *, "get_filehandle returned NULL handle",
		    struct vnode *, dvp);
		return (EIO);
	}

	fh->ref++;

	/* setup fuse i/o data structure */
	fiodata.uiop = uiop;
	fiodata.credp = cred_p;
	fiodata.vp = dvp;
	fiodata.fh = fh;
	fiodata.op = FUSE_READDIR;
	fiodata.consfunc = fuse_cons_dir;

	err = fuse_perform_read(&fiodata);
	fh->ref--;
#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(dvp);
#endif
out:
	return (err);
}

/*
 * Check if the source vnode is in the path to the target directory
 */
static int
fuse_path_check(struct vnode *svp, struct vnode *tdvp, struct cred *credp)
{
	struct vnode *vp = tdvp;
	struct vnode *par_vp = NULL;
	fuse_session_t *sep;
	int err = 0;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_path_check_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	for (;;) {
		/*
		 * if the current vnode under investigation is same as the
		 * source vnode then return error since source is above the
		 * target in the path
		 */
		if (VNODE_TO_NODEID(vp) == VNODE_TO_NODEID(svp)) {
			err = EINVAL;
			break;
		}
		if (VNODE_TO_NODEID(vp) == FUSE_ROOT_ID) {
			break;
		}
		if ((VTOFD(vp)->par_nid != FUSE_NULL_ID)) {
			/*
			 * Try finding the parent vnode, this call
			 * also sets the hold on the vnode
			 */
			err = fuse_getvnode(VTOFD(vp)->par_nid, &par_vp,
			    VNODE_CACHED, VDIR, sep, vp->v_vfsp, 0, NULL,
			    FUSE_NULL_ID, credp);
			/*
			 * We don't seem to have the parent vnode cached, since
			 * we can't know who the parent is, even the daemon
			 * can't help us, so return error.
			 */
			if (err == ENOENT) {
				DTRACE_PROBE2(fuse_path_check_err_parent,
				    char *, "Parent vnode not found",
				    struct vnode *, vp);
				vp = NULL;
				/*
				 * XXX: Is this the right error no to return?
				 */
				err = EINVAL;
				break;
			} else if (err) {
				DTRACE_PROBE3(fuse_path_check_err_getvnode,
				    char *, "fuse_getvnode returned error",
				    int, err, uint64_t, VTOFD(vp)->par_nid);
				vp = NULL;
				break;
			}
			/* Release our hold on the currently looked up dir */
			if (vp != tdvp) {
				VN_RELE(vp);
			}
			/* Start checking from this directory onwards */
			vp = par_vp;
		}
	}
	/* Release hold if any */
	if (vp && vp != tdvp)
		VN_RELE(vp);

	return (err);
}

/* This function interacts with the daemon for rename operation */
static int
fuse_rename_i(struct vnode *sdvp, char *oldname, struct vnode *tdvp,
    char *newname, struct cred *credp)
{
	struct fuse_rename_in *fri;
	fuse_session_t *sep;
	fuse_msg_node_t *msgp;
	char *strptr;
	int err = 0;
	int old_namelen = strlen(oldname) + 1;
	int new_namelen = strlen(newname) + 1;

	if (old_namelen > MAXNAMELEN || new_namelen > MAXNAMELEN)
		return (ENAMETOOLONG);

	sep = fuse_minor_get_session(getminor(sdvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_rename_i_err_session,
		    char *, "failed to find session",
		    struct vnode *, sdvp);
		return (ENODEV);
	}

	msgp = fuse_setup_message((sizeof (*fri) + old_namelen + new_namelen),
	    FUSE_RENAME, VNODE_TO_NODEID(sdvp), credp, FUSE_GET_UNIQUE(sep));

	/* Set up the arguments required by the daemon */
	fri = (struct fuse_rename_in *)msgp->ipdata.indata;
	fri->newdir = VNODE_TO_NODEID(tdvp);
	/* Copy the old and new name */
	strptr = (char *)fri + sizeof (*fri);
	(void) memcpy(strptr, oldname, old_namelen);
	strptr += old_namelen;
	(void) memcpy(strptr, newname, new_namelen);

	err = fuse_queue_request_wait(sep, msgp);
	fuse_free_msg(msgp);
	return (err);
}

/*
 * Move an entry from the provided source directory to the target
 * directory. (comment & func. prtotype copies from zfs)
 *
 *	IN:	sdvp	- Source directory containing the "old entry".
 *		oldname	- Old entry name.
 *		tdvp	- Target directory to contain the "new entry".
 *		newname	- New entry name.
 *		credp	- credentials of caller.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 * Timestamps:
 *	sdvp, tdvp - ctime|mtime updated
 */
/* ARGSUSED */
static int
fuse_rename(vnode_t *sdvp, char *oldname, vnode_t *tdvp, char *newname,
    cred_t *credp,  caller_context_t *ct, int flags)
{
	int err = EIO;
	int samedir;
	struct vnode *svp = NULL;
	struct vnode *tvp = NULL;


	/* Try obtaining the source vnode by doing a lookup */
	err = fuse_lookup_i(sdvp, oldname, &svp, credp);

	if (err)
		return (err);

	/*
	 * Make sure we can delete the old (source) entry.  This
	 * requires write permission on the containing directory.  If
	 * that directory is "sticky" it requires further checks.
	 */
	if (((err = fuse_access_i((void *)sdvp, VWRITE, credp)) != 0) ||
	    (err = fuse_sticky_remove_access(sdvp, svp, credp, ct)) != 0) {
		goto errout;
	}

	/*
	 * Check for renaming to or from '.' or '..'
	 */
	if ((oldname[0] == '.' &&
	    (oldname[1] == '\0' || (oldname[1] == '.' &&
	    oldname[2] == '\0'))) ||
	    (newname[0] == '.' &&
	    (newname[1] == '\0' || (newname[1] == '.' &&
	    newname[2] == '\0'))) ||
	    (sdvp == svp)) {
		err = EINVAL;
		goto errout;
	}

	samedir = (sdvp == tdvp);

	/*
	 * Make sure we can search and rename into the new
	 * (destination) directory.
	 */
	if (!samedir) {
		err = fuse_access_i((void *)tdvp, VEXEC|VWRITE, credp);
		if (err)
			goto errout;
	}
	/*
	 * If we are trying to move a directory, then make sure there is
	 * no pathname conflicts, i.e. make sure the source is not above the
	 * target in the path
	 */
	if (svp->v_type == VDIR && !samedir) {
		if ((err = fuse_path_check(svp, tdvp, credp))) {
			goto errout;
		}
	}

	/* Try doing a lookup for the target name */
	if ((err = fuse_lookup_i(tdvp, newname, &tvp, credp))) {
		if (err != ENOENT) {
			goto errout;
		} else {
			err = 0;
		}
	}
	/*
	 * if the target exists, then validate if src and target are
	 * of compatible type so that target directory can be rewritten to
	 * point to src
	 */
	if (tvp) {
		if ((tvp->v_type == VDIR && svp->v_type != VDIR) ||
		    (tvp->v_type != VDIR && svp->v_type == VDIR)) {
			err = ENOTDIR;
			goto errout;
		}
	}
	/* Inform daemon to handle the rename operation */
	if ((err = fuse_rename_i(sdvp, oldname, tdvp, newname, credp)))
		goto errout;

errout:
	if (svp)
		VN_RELE(svp);
	if (tvp)
		VN_RELE(tvp);
	return (err);
}

/* ARGSUSED */
static int
fuse_fsync(struct vnode *vp, int syncflag, struct cred *credp,
    caller_context_t *ct)
{
	int err;
	struct fuse_fh_param fh_param;

	/* XXX: Do locking related logic here */

	/* Flush out any dirty pages associated with this vnode */
	if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) &&
	    (vp->v_type != VCHR)) {
		err = VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, 0, credp, ct);
		if (err)
			return (err);
	}
	/*
	 * We will iterate through all the file handles associated with this
	 * vnode and send FUSE_FSYNC/FUSE_FSYNCDIR. See comment in FreeBSD
	 * Fuse fsync implementation for more information
	 */
	fh_param.vp = vp;
	fh_param.credp = credp;
	fh_param.rw_mode = FWRITE;
	fh_param.fufh = NULL;
	(void) iterate_filehandle(vp, fuse_fsync_fh, &fh_param, NULL);

	return (0);
}

/*
 * This function sends FUSE_FSYNC/FUSE_FSYNCDIR on the passed
 * file handle associated with the concerned vnode
 */
static int
fuse_fsync_fh(struct fuse_file_handle *fhp, struct fuse_fh_param *param)
{
	struct fuse_fsync_in *ffsi;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;
	enum fuse_opcode op = (param->vp->v_type == VDIR) ? FUSE_FSYNCDIR :
	    FUSE_FSYNC;

	sep = fuse_minor_get_session(getminor(param->vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_fsync_fh_err_session,
		    char *, "failed to find session",
		    struct vnode *, param->vp);
		return (ENODEV);
	}

	msgp = fuse_setup_message(sizeof (*ffsi), op,
	    VNODE_TO_NODEID(param->vp), param->credp, FUSE_GET_UNIQUE(sep));
	ffsi = (struct fuse_fsync_in *)msgp->ipdata.indata;
	ffsi->fh = fhp->fh_id;
	/* We want to sync file data and not just metadata */
	ffsi->fsync_flags = 1;

	(void) fuse_queue_request_wait(sep, msgp);

	fuse_free_msg(msgp);
	/*
	 * Since this is called from iterate_filehandle() we will not return
	 * obtained err if any. This is to ensure that all the file handles
	 * in the list are serviced.
	 */
	return (0);
}

/* ARGSUSED */
static int
fuse_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
    caller_context_t *ct)
{
	return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0);
}

/*
 * Remove an entry from a directory.
 *
 *	IN:	dvp	- vnode of directory to remove entry from.
 *		name	- name of entry to remove.
 *		credp	- credentials of caller.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 * Timestamps:
 *	dvp - ctime|mtime
 *	 vp - ctime (if nlink > 0)
 */
/* ARGSUSED */
static int
fuse_remove(vnode_t *dvp, char *name, cred_t *credp, caller_context_t *ct,
    int flags)
{
	int err;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;
	int namelen = strlen(name) + 1;
	struct vnode *vp = NULL;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_remove_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	msgp = fuse_setup_message(namelen, FUSE_UNLINK,
	    VNODE_TO_NODEID(dvp), credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	(void) memcpy(msgp->ipdata.indata, name, namelen - 1);
	((char *)msgp->ipdata.indata)[namelen - 1] = '\0';

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* Check for any error from fuse library */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_unlink_err_unlink_req,
			char *, "FUSE_UNLINK request failed",
			struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}

	/* Check if we have seen and cached the associated vnode */
	err = fuse_getvnode(FUSE_NULL_ID, &vp, VNODE_CACHED,
	    0, sep, dvp->v_vfsp, namelen, name, VNODE_TO_NODEID(dvp), credp);

	if (err) {
		if (err == ENOENT) {
			/*
			 * This means, we don't have this vnode, so
			 * there is nothing else to do, just reset the error
			 * and return.
			 */
			err = 0;
		}
		goto cleanup;
	}
	ASSERT(vp != NULL);
	/*
	 * Destroy the vnode, but we can't go through the sane way of
	 * destroying the vnode which involves sending FUSE_FORGET message
	 * to the daemon, flushing dirty data and so on. All this is a bit
	 * too heavy weight when we don't have to care about this vnode any
	 * more. So we just free all associated memory with this vnode and
	 * return it to kernel.
	 */
	/*
	 * XXX: This call releases vnode with vn_free, can this
	 * be an issue? Or does VN_RELE do the required job for us?
	 */
	fuse_vnode_free(vp, sep);
cleanup:
	fuse_free_msg(msgp);
	return (err);
}

/*
 * This function does the release of all the associated
 * memory with the passed vnode
 */
static inline void
fuse_vnode_free(struct vnode *vp, fuse_session_t *sep)
{
	struct fuse_file_handle *fhp;
	struct fuse_vnode_data *fvdatap = VTOFD(vp);

	if (VTOFD(vp)) {
		mutex_enter(&fvdatap->fh_list_lock);
		for (fhp = list_head(&fvdatap->fh_list); fhp;
		    fhp = list_next(&fvdatap->fh_list, fhp)) {
			list_remove(&fvdatap->fh_list, fhp);
			kmem_free(fhp, sizeof (*fhp));
		}
		mutex_exit(&fvdatap->fh_list_lock);
		fuse_vnode_cache_remove(vp, sep);
		fuse_free_vdata(vp);
	}
	vp->v_data = NULL;

	vn_free(vp);
}
/*
 * Insert the indicated symbolic reference entry into the directory.
 *
 *	IN:	dvp	- Directory to contain new symbolic link.
 *		name	- Name for new symlink entry.
 *		vap	- Attributes of new entry.
 *		target	- Target path of new symlink.
 *		cr	- credentials of caller.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 * Timestamps:
 *	dvp - ctime|mtime updated
 */
/* ARGSUSED */
static int
fuse_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *target,
    cred_t *credp,  caller_context_t *ct, int flags)
{
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;
	int err = DDI_SUCCESS;
	int namelen = strlen(name) + 1;
	int targlen = strlen(target) + 1;
	struct vnode *vp;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_symlink_err_session,
		    char *, "failed to find session",
		    struct vnode *, dvp);
		return (ENODEV);
	}

	msgp = fuse_setup_message(namelen + targlen, FUSE_SYMLINK,
	    VNODE_TO_NODEID(dvp), credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	(void) memcpy(msgp->ipdata.indata, name, namelen - 1);
	((char *)msgp->ipdata.indata)[namelen - 1] = '\0';
	(void) memcpy((char *)msgp->ipdata.indata + namelen, target,
	    targlen - 1);
	((char *)msgp->ipdata.indata)[namelen + targlen - 1] = '\0';

	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto cleanup;
	}

	/* We got woken up, so fuse lib has replied to our release request */
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_symlink_err_symlink_req,
		    char *, "FUSE_SYMLINK request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}


	err = fuse_add_entry(&vp, dvp, msgp, sep, name, namelen, credp, VLNK);

cleanup:
	fuse_free_msg(msgp);
	return (err);
}
/*
 * Return, in the buffer contained in the provided uio structure,
 * the symbolic path referred to by vp.
 *
 *	IN:	vp	- vnode of symbolic link.
 *		uoip	- structure to contain the link path.
 *		cr	- credentials of caller.
 *
 *	OUT:	uio	- structure to contain the link path.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 * Timestamps:
 *	vp - atime updated
 */
/* ARGSUSED */
static int
fuse_readlink(vnode_t *vp, uio_t *uiop, cred_t *cred_p, caller_context_t *ct)
{
	int err = DDI_SUCCESS;
	fuse_msg_node_t *msgp;
	fuse_session_t *sep;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_readlink_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	msgp = fuse_alloc_msg();

	/*
	 * setup the message with only header and no arguments to be sent to
	 * fuse library
	 */
	setup_msg_onlyheader(msgp, FUSE_READLINK, FUSE_GET_UNIQUE(sep),
	    VNODE_TO_NODEID(vp), cred_p);


	if ((err = fuse_queue_request_wait(sep, msgp))) {
		goto out;
	}

	err = msgp->opdata.fouth->error;

	if (!err) {
		err = uiomove(msgp->opdata.outdata, msgp->opdata.outsize,
		    UIO_READ, uiop);
	} else {
		DTRACE_PROBE2(fuse_readlink_err_readlink_req,
		    char *, "FUSE_READLINK request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
	}

#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(vp);
#endif

out:
	fuse_free_msg(msgp);
	return (err);
}

/*
 * Sends the FUSE_FORGET message to daemon which performs the necessary
 * cleanup
 */
static void
fuse_send_forget(uint64_t nodeid, fuse_session_t *sep, uint64_t nlookup)
{
	fuse_msg_node_t *msgp;
	struct fuse_forget_in *ffi;

	msgp = fuse_setup_message(sizeof (*ffi), FUSE_FORGET,
	    nodeid, sep->usercred, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	ffi = (struct fuse_forget_in *)msgp->ipdata.indata;
	ffi->nlookup = nlookup;

	/* Send the request to the fuse daemon and return */
	msgp->fmn_noreply = 1;
	fuse_queue_request_nowait(sep, msgp);
}

/*
 * Returns the file size associated with the passed vnode, if needed contacts
 * the daemon
 */
static int
fuse_getfilesize(struct vnode *vp, u_offset_t *fsize, struct cred *credp)
{
	int err;
	struct vattr va;
	fuse_msg_node_t *msgp;

	/* Provide, if the updated file size is available with us */
	if (VTOFD(vp)->file_size_status & FSIZE_UPDATED) {
		*fsize = VTOFD(vp)->fsize;
		DTRACE_PROBE2(fuse_getfilesize_info_cache,
		    char *, "returning file size from cache",
		    u_offset_t, *fsize);
		return (0);
	}
#ifndef DONT_CACHE_ATTRIBUTES
	/*
	 * Next step is to check if the attribute cache is valid, if so
	 * no need to ask the daemon for the file size
	 */
	timestruc_t ts;
	gethrestime(&ts);
	if (ATTR_CACHE_VALID(ts, VTOFD(vp)->cached_attrs_bound)) {
		*fsize = VTOFD(vp)->cached_attrs.va_size;
		DTRACE_PROBE2(fuse_getfilesize_info_attr_cache,
		    char *, "returning file size from attr cache",
		    u_offset_t, *fsize);
		return (0);
	}
#endif
	/* We are not lucky, have to get file size from daemon */
	err = fuse_getattr_from_daemon(vp, &va, credp, &msgp);

	if (err)
		return (err);

#ifndef DONT_CACHE_ATTRIBUTES
	cache_attrs(vp, (struct fuse_attr_out *)(msgp->opdata.outdata));
#endif
	*fsize = va.va_size;
	DTRACE_PROBE2(fuse_getfilesize_info_daemon,
	    char *, "returning file size from daemon",
	    u_offset_t, *fsize);

	fuse_free_msg(msgp);
	return (err);
}

/* Performs the mapping of a page to kernel virtual address space */
static void
fuse_page_mapin(struct vnode *vp, struct buf **bp, struct page *pp, size_t len,
    int flag, struct fuse_iov *iovp)
{

	/* Map the page into kernel virtual memory */
	*bp = pageio_setup(pp, len, vp, flag);
	ASSERT((*bp) != NULL);

	(*bp)->b_edev = 0;
	(*bp)->b_dev = 0;
	(*bp)->b_lblkno = 0;
	(*bp)->b_file = vp;
	(*bp)->b_offset = 0;

	bp_mapin((*bp));

	iovp->memsize = len;
	iovp->len = 0;
	iovp->memflag = MEM_TYPE_PAGE;
	iovp->base = (*bp)->b_un.b_addr;

}

/*
 * Free storage space associated with the specified vnode. The portion
 * to be freed is specified by bfp->l_start and bfp->l_len (already
 * normalized to a "whence" of 0).
 *
 * This is an experimental facility whose continued existence is not
 * guaranteed.  Currently, we only support the special case
 * of l_len == 0, meaning free to end of file.
 */
/* ARGSUSED */
static int
fuse_space(vnode_t *vp, int cmd, struct flock64 *bfp, int flag,
    offset_t offset, cred_t *cr, caller_context_t *ct)
{
	int error;

	if (cmd != F_FREESP)
		return (EINVAL);

	ASSERT(vp->v_type == VREG);

	error = convoff(vp, bfp, 0, offset);
	if (error == 0) {
		ASSERT(bfp->l_start >= 0);

		if (bfp->l_len == 0) {
			vattr_t va;

			va.va_mask = AT_SIZE;
			error = VOP_GETATTR(vp, &va, 0, cr, ct);
			if (error || va.va_size == bfp->l_start) {
				/*
				 * do not update ctime/mtime if truncate
				 * to previous size, just exit
				 */
				return (error);
			}
			va.va_mask = AT_SIZE;
			va.va_size = bfp->l_start;
			error = VOP_SETATTR(vp, &va, 0, cr, ct);
		}
		else
			error = EINVAL;
	}

	return (error);
}

/* ARGSUSED */
static int
fuse_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp,
    size_t *lenp, int flags, cred_t *credp)
{
	fuse_session_t *sep;
	u_offset_t fsize;
	int err, diff;
	struct fuse_write_in *fwi;
	struct fuse_write_out *fwo;
	struct fuse_file_handle *fhp = NULL;
	fuse_msg_node_t *msgp = NULL;
	struct buf *bp = NULL;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_putapage_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return (ENODEV);
	}

	if ((err = get_filehandle(vp, FWRITE, credp, &fhp, CACHE_LIST_CHECK))) {
		DTRACE_PROBE2(fuse_putapage_err_filehandle,
		    char *, "get_filehandle failed",
		    struct vnode *, vp);
		goto cleanup;
	}
	if ((err = fuse_getfilesize(vp, &fsize, credp))) {
		DTRACE_PROBE3(fuse_putapage_err_file_size,
		    char *, "fuse_getfilesize failed",
		    int, err, struct vnode *, vp);
		goto cleanup;
	}
	msgp = fuse_setup_message(sizeof (*fwi),
	    FUSE_WRITE, VTOFD(vp)->nodeid, credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	fwi = (struct fuse_write_in *)msgp->ipdata.indata;

	fwi->fh = fhp->fh_id;
	fwi->offset = pp->p_offset;
	fwi->size = (uint32_t)MIN(PAGESIZE, fsize - pp->p_offset);
	/*
	 * XXX:There is a lot of room for optimization here, we can do
	 * page klustering and then create a scatter gather list
	 * of pages and their addresses to be used by device read
	 */

	/* Map this page in to kernel virtual address space */
	fuse_page_mapin(vp, &bp, pp, PAGESIZE, B_WRITE,
	    &(msgp->ipdata.iovbuf[1]));
	msgp->ipdata.iovbuf[1].len = fwi->size;
	msgp->ipdata.iovs_used = 2;

	if ((err = fuse_queue_request_wait(sep, msgp)) != 0) {
		goto cleanup;
	}

	if ((err = msgp->opdata.fouth->error) !=  0) {
		DTRACE_PROBE2(fuse_putapage_err_write_req,
		    char *, "FUSE_WRITE request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}
	fwo = (struct fuse_write_out *)msgp->opdata.outdata;
	diff = fwi->size - fwo->size;
	if (diff < 0) {
		err = EINVAL;
		goto cleanup;
	}
	if (diff > 0) {
		/*
		 * This means daemon could do only a partial write, we
		 * currently don't manage the hassles of handling partial
		 * write with possibly partial dirty pages, so we treat
		 * as an error
		 */
		DTRACE_PROBE2(fuse_putapage_err_short_write,
		    char *, "daemon executed partial write ",
		    struct fuse_write_out *, fwo);
		err = EIO;
		goto cleanup;
	}
cleanup:
	if (fhp)
		fhp->ref--;
	if (err) {
		flags |= B_ERROR;
	} else {
		if (lenp)
			*lenp = fwi->size;
	}

	if (bp != NULL) {
		bp_mapout(bp);
		pageio_done(bp);
	}
	if (msgp != NULL)
		fuse_free_msg(msgp);

	(void) pvn_write_done(pp, B_WRITE | flags);

	return (err);
}

/*
 * If len == 0, do from off to EOF.
 *
 * The normal cases should be len == 0 & off == 0 (entire vp list),
 * len == MAXBSIZE (from segmap_release actions), and len == PAGESIZE
 * (from pageout).
 */
/* ARGSUSED */
static int
fuse_putpage(struct vnode *vp, offset_t off, size_t len, int flags,
	struct cred *credp, caller_context_t *ct)
{
	register page_t *pp;
	int err = 0;
	size_t io_len = 0;
	u_offset_t fsize = 0;
	u_offset_t io_off;
	u_offset_t eoff;

	if (vp->v_count == 0) {
		DTRACE_PROBE2(fuse_putpage_err_vcount,
		    char *, "vnode v_count == 0",
		    struct vnode *, vp);
		return (EAGAIN);
	}

	if (vp->v_flag & VNOMAP)
		return (ENOSYS);

	if (!vn_has_cached_data(vp))
		return (0);

	if (len == 0) {
		err = pvn_vplist_dirty(vp, (u_offset_t)off, fuse_putapage,
		    flags, credp);
	} else {
		if ((err = fuse_getfilesize(vp, &fsize, credp))) {
			DTRACE_PROBE3(fuse_putpage_err_filesize,
			    char *, "fuse_getfilesize failed",
			    int, err, struct vnode *, vp);
			return (err);
		}

		eoff = MIN(off + len, fsize);

		for (io_off = off; io_off < eoff; io_off += io_len) {
			/*
			 * If we are not invalidating, synchronously
			 * freeing or writing pages use the routine
			 * page_lookup_nowait() to prevent reclaiming
			 * them from the free list.
			 */
			if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) {
				pp = page_lookup(vp, io_off,
				    (flags & (B_INVAL | B_FREE)) ?
				    SE_EXCL : SE_SHARED);
			} else {
				pp = page_lookup_nowait(vp, io_off,
				    (flags & B_FREE) ? SE_EXCL : SE_SHARED);
			}

			if (pp == NULL || pvn_getdirty(pp, flags) == 0) {
				io_len = PAGESIZE;
			} else {
				err = fuse_putapage(vp, pp, &io_off, &io_len,
				    flags, credp);
				if (err != 0)
					break;
			}
		}
	}
	/* If invalidating, verify all pages on vnode list are gone. */
	if (err == 0 && off == 0 && len == 0 &&
	    (flags & B_INVAL) && vn_has_cached_data(vp)) {
		panic("fuse_putpage: B_INVAL, pages not gone");
	}
	/*
	 * If all the dirty pages was written back, remove the file size changed
	 * status flag
	 */
	if (len == 0 || eoff == fsize) {
		(void) fsize_change_notify(vp, 0, FSIZE_NOT_RELIABLE);
	}
	return (err);
}

/* ARGSUSED */
static int
fuse_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)
{
	struct segvn_crargs vn_a;
	int error;

	if (vp->v_flag & VNOMAP)
		return (ENOSYS);

	if (off < (offset_t)0 || (offset_t)(off + len) < (offset_t)0)
		return (ENXIO);

	if (vp->v_type != VREG)
		return (ENODEV);

	/*
	 * XXX: If file is being locked, disallow mapping.
	 */

	as_rangelock(as);
	error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags);
	if (error != 0) {
		as_rangeunlock(as);
		goto out;
	}

	vn_a.vp = vp;
	vn_a.offset = off;
	vn_a.type = (flags & MAP_TYPE);
	vn_a.prot = prot;
	vn_a.maxprot = maxprot;
	vn_a.cred = cr;
	vn_a.amp = NULL;
	vn_a.flags = (flags & ~MAP_TYPE);
	vn_a.szc = 0;
	vn_a.lgrp_mem_policy_flags = 0;

	error = as_map(as, *addrp, len, segvn_create, &vn_a);
	as_rangeunlock(as);

out:
	return (error);
}

/* ARGSUSED */
static int
fuse_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)
{
	if (vp->v_flag & VNOMAP)
		return (ENOSYS);

	mutex_enter(&VTOFD(vp)->f_lock);
	VTOFD(vp)->f_mapcnt += btopr(len);
	mutex_exit(&VTOFD(vp)->f_lock);

	return (0);
}

/* ARGSUSED */
static int
fuse_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)
{
	if (vp->v_flag & VNOMAP)
		return (ENOSYS);

	mutex_enter(&VTOFD(vp)->f_lock);
	VTOFD(vp)->f_mapcnt -= btopr(len);
	mutex_exit(&VTOFD(vp)->f_lock);

	if ((flags & MAP_SHARED) && (prot & PROT_WRITE) &&
	    vn_has_cached_data(vp))
		(void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct);

	return (0);
}

static void
fuse_vnode_cache_remove(struct vnode *vp, fuse_session_t *sep)
{
	avl_index_t where;
	fuse_avl_cache_node_t discard, *removep;

	discard.facn_nodeid = VNODE_TO_NODEID(vp);
	if ((removep = avl_find(& (sep->avl_cache), &discard, &where))
	    != NULL) {
		avl_remove(&(sep->avl_cache), removep);
		fuse_avl_cache_node_destroy(removep);
	}
}

/* Does all the necessary cleanup w.r.t a vnode */
static void
fuse_vnode_cleanup(struct vnode *vp, struct cred *credp, fuse_session_t *sep)
{
	struct fuse_fh_param fh_param;
	/* Invalidate pages while flushing out any dirty data */
	if (vn_has_cached_data(vp)) {
		(void) pvn_vplist_dirty(vp, 0, fuse_putapage, B_FREE, credp);
	}
	/* Release if the vnode has any file handles with it */
	if (VTOFD(vp)) {
		fh_param.vp = vp;
		fh_param.flag = FUSE_FORCE_FH_RELEASE;
		(void) iterate_filehandle(vp, fuse_release_fh, &fh_param, NULL);
	}
	/* Let daemon know that we are getting rid of this vnode */
	fuse_send_forget(VNODE_TO_NODEID(vp), sep, (VTOFD(vp))->nlookup);

	/* Finally its time to remove it from our cache */
	fuse_vnode_cache_remove(vp, sep);
}

/* ARGSUSED */
static void
fuse_inactive(struct vnode *vp, struct cred *credp, caller_context_t *ct)
{
	struct fuse_fh_param fh_param;
	fuse_session_t *sep;

	fh_param.vp = vp;
	fh_param.flag = 0;

	sep = fuse_minor_get_session(getminor(vp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_inactive_err_session,
		    char *, "failed to find session",
		    struct vnode *, vp);
		return;
	}

	(void) iterate_filehandle(vp, fuse_release_fh, &fh_param, NULL);

	mutex_enter(&vp->v_lock);
	if (vp->v_count > 1) {
		vp->v_count--;
		mutex_exit(&vp->v_lock);
		return;
	}
	mutex_exit(&vp->v_lock);
#ifndef FLUSH_DATA_ON_VNODE_RELEASE

	/*
	 * We don't free any pages associated with this vnode, nor do we
	 * remove it from the AVL tree
	 */
	return;
#else
	if (!(vp->v_flag & VROOT)) {
		fuse_vnode_destroy(vp, credp, sep);
	}
#endif
}

static int
fuse_add_entry(struct vnode **vpp, struct vnode *dvp, fuse_msg_node_t *msgp,
    fuse_session_t *sep, char *name, int namelen, cred_t *credp, vtype_t vtype)
{
	int err = DDI_SUCCESS;
	struct fuse_entry_out *feo =
	    (struct fuse_entry_out *)msgp->opdata.outdata;

	if ((err = checkentry(feo, vtype))) {
		return (err);
	}

	err = fuse_getvnode(feo->nodeid, vpp, VNODE_NEW,
	    IFTOVT(feo->attr.mode), sep, dvp->v_vfsp, namelen, name,
	    VNODE_TO_NODEID(dvp), credp);

	if (err) {
		DTRACE_PROBE3(fuse_add_entry_err_getvnode,
		    char *, "fuse_getvnode returned error",
		    int, err, uint64_t, feo->nodeid);
		fuse_send_forget(feo->nodeid, sep, 1);
		return (err);
	}
#ifndef DONT_CACHE_ATTRIBUTES
	invalidate_cached_attrs(dvp);
	cache_attrs((*vpp), feo);
#endif
	return (err);
}

/*
 * Create a new directory and insert it into dvp using the name
 * provided. Return a pointer to the inserted directory.
 *
 *	IN:	dvp	- vnode of directory to add subdir to.
 *		dirname	- name of new directory.
 *		vap	- attributes of new directory.
 *		cr	- credentials of caller.
 *
 *	OUT:	vpp	- vnode of created directory.
 *
 *	RETURN:	0 if success
 *		error code if failure
 *
 */
/* ARGSUSED */
static int
fuse_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp,
    cred_t *credp, caller_context_t *ct, int flags, vsecattr_t *vsecp)
{
	struct fuse_mkdir_in *fmkdi;
	fuse_msg_node_t *msgp;
	int err = 0;
	fuse_session_t *sep;
	int namelen;

	sep = fuse_minor_get_session(getminor(dvp->v_rdev));
	if (sep == NULL) {
		DTRACE_PROBE2(fuse_mkdir_err_session,
		    char *, "failed to find session",
		    struct vnode *, dvp);
		return (ENODEV);
	}

	namelen = strlen(dirname) + 1;
	if (namelen > MAXNAMELEN) {
		return (ENAMETOOLONG);
	}

	msgp = fuse_setup_message((sizeof (*fmkdi) + namelen),
	    FUSE_MKDIR, VTOFD(dvp)->nodeid, credp, FUSE_GET_UNIQUE(sep));

	/* Set up arguments to the fuse library */
	fmkdi = (struct fuse_mkdir_in *)msgp->ipdata.indata;

	fmkdi->mode = MAKEIMODE(vap->va_type, vap->va_mode);
	(void *) strlcpy(((char *)msgp->ipdata.indata + sizeof (*fmkdi)),
	    dirname, namelen);

	if ((err = fuse_queue_request_wait(sep, msgp)) != 0) {
		goto cleanup;
	}
	if ((err = msgp->opdata.fouth->error) != 0) {
		DTRACE_PROBE2(fuse_mkdir_err_mkdir_req,
		    char *, "FUSE_MKDIR request failed",
		    struct fuse_out_header *, msgp->opdata.fouth);
		goto cleanup;
	}
	err = fuse_add_entry(vpp, dvp, msgp, sep, dirname, namelen, credp,
	    VDIR);
cleanup:
	fuse_free_msg(msgp);
	return (err);
}