summaryrefslogtreecommitdiffstats
path: root/extras
diff options
context:
space:
mode:
authorMilind Changire <mchangir@redhat.com>2019-02-15 14:20:07 +0530
committerAmar Tumballi <amarts@redhat.com>2019-02-18 02:45:40 +0000
commit776ba851c6ee6c265253d44cf1d6e4e3d4a21772 (patch)
tree045cc2bd91b72048bc8a54eac67b3e605c85e3ae /extras
parentfc133767e5ed705f246547cdfa5b7a2db5dcd53f (diff)
socket: socket event handlers now return void
Problem: Returning any value from socket event handlers to the event sub-system doesn't make sense since event sub-system cannot handle socket sub-system errors. Solution: Change return type of all socket event handlers to 'void' Change-Id: I70dc2c57f12b7ea2fae41120f71aa0d7fe0b2b6f Fixes: bz#1651246 Signed-off-by: Milind Changire <mchangir@redhat.com>
Diffstat (limited to 'extras')
0 files changed, 0 insertions, 0 deletions
'#n85'>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
/*
  Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

  GlusterFS is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3 of the License,
  or (at your option) any later version.

  GlusterFS is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see
  <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include <inttypes.h>

#include "xlator.h"
#include "nfs3.h"
#include "nfs3-fh.h"
#include "msg-nfs3.h"
#include "rbthash.h"
#include "nfs-fops.h"
#include "nfs-inodes.h"
#include "nfs-generics.h"
#include "nfs3-helpers.h"
#include "nfs-mem-types.h"
#include "iatt.h"
#include <string.h>

#define nfs3_call_resume(cst)                                   \
        do {                                                    \
                if (((cst)) && (cst)->resume_fn)                \
                        (cst)->resume_fn (cst);                 \
        } while (0)                                             \

#define nfs3_call_resume_estale(csta)                           \
        do {                                                    \
                (csta)->resolve_ret = -1;                       \
                (csta)->resolve_errno = ESTALE;                 \
                nfs3_call_resume (csta);                        \
        } while (0)                                             \

struct nfs3stat_strerror {
        nfsstat3        stat;
        char            strerror[100];
};


struct nfs3stat_strerror nfs3stat_strerror_table[] = {
        { NFS3_OK,              "Call completed successfully."          },
        { NFS3ERR_PERM,         "Not owner"                             },
        { NFS3ERR_NOENT,        "No such file or directory"             },
        { NFS3ERR_IO,           "I/O error"                             },
        { NFS3ERR_NXIO,         "I/O error"                             },
        { NFS3ERR_ACCES,        "Permission denied"                     },
        { NFS3ERR_EXIST,        "File exists"                           },
        { NFS3ERR_XDEV,         "Attempt to do a cross-device hard link"},
        { NFS3ERR_NODEV,        "No such device"                        },
        { NFS3ERR_NOTDIR,       "Not a directory"                       },
        { NFS3ERR_ISDIR,        "Is a directory"                        },
        { NFS3ERR_INVAL,        "Invalid argument for operation"        },
        { NFS3ERR_FBIG,         "File too large"                        },
        { NFS3ERR_NOSPC,        "No space left on device"               },
        { NFS3ERR_ROFS,         "Read-only file system"                 },
        { NFS3ERR_MLINK,        "Too many hard links"                   },
        { NFS3ERR_NAMETOOLONG,  "Filename in operation was too long"    },
        { NFS3ERR_NOTEMPTY,     "Directory not empty"                   },
        { NFS3ERR_DQUOT,        "Resource (quota) hard limit exceeded"  },
        { NFS3ERR_STALE,        "Invalid file handle"                   },
        { NFS3ERR_REMOTE,       "Too many levels of remote in path"     },
        { NFS3ERR_BADHANDLE,    "Illegal NFS file handle"               },
        { NFS3ERR_NOT_SYNC,     "Update synchronization mismatch detected" },
        { NFS3ERR_BAD_COOKIE,   "READDIR or READDIRPLUS cookie is stale"},
        { NFS3ERR_NOTSUPP,      "Operation is not supported"            },
        { NFS3ERR_TOOSMALL,     "Buffer or request is too small"        },
        { NFS3ERR_SERVERFAULT,  "Error occurred on the server or IO Error" },
        { NFS3ERR_BADTYPE,      "Type not supported by the server"      },
        { NFS3ERR_JUKEBOX,      "Cannot complete server initiated request" },
        { -1,                   "IO Error"                              },

};


void
nfs3_map_xlid_to_statdev (struct iatt *ia, uint16_t xlid)
{
        if (!ia)
                return;

        ia->ia_dev = xlid;
}


struct nfs3_fh
nfs3_extract_nfs3_fh (nfs_fh3 fh)
{
        struct nfs3_fh          gfh;

        memcpy (&gfh, fh.data.data_val, fh.data.data_len);
        return gfh;
}


struct nfs3_fh
nfs3_extract_lookup_fh (lookup3args *args)
{
        return nfs3_extract_nfs3_fh(args->what.dir);
}


char *
nfs3_extract_lookup_name (lookup3args *args)
{
        return args->what.name;
}


nfsstat3
nfs3_errno_to_nfsstat3 (int errnum)
{
        nfsstat3        stat = NFS3_OK;

        switch (errnum) {

        case 0:
                stat = NFS3_OK;
                break;

        case EPERM:
                stat = NFS3ERR_PERM;
                break;

        case ENOENT:
                stat = NFS3ERR_NOENT;
                break;

        case EACCES:
                stat = NFS3ERR_ACCES;
                break;

        case EEXIST:
                stat = NFS3ERR_EXIST;
                break;

        case EXDEV:
                stat = NFS3ERR_XDEV;
                break;

        case ENODEV:
                stat = NFS3ERR_NODEV;
                break;

        case EIO:
                stat = NFS3ERR_IO;
                break;

        case ENXIO:
                stat = NFS3ERR_NXIO;
                break;

        case ENOTDIR:
                stat = NFS3ERR_NOTDIR;
                break;

        case EISDIR:
                stat = NFS3ERR_ISDIR;
                break;

        case EINVAL:
                stat = NFS3ERR_INVAL;
                break;

        case ENOSPC:
                stat = NFS3ERR_NOSPC;
                break;

        case EROFS:
                stat = NFS3ERR_ROFS;
                break;

        case EFBIG:
                stat = NFS3ERR_FBIG;
                break;

        case EMLINK:
                stat = NFS3ERR_MLINK;
                break;

        case ENAMETOOLONG:
                stat = NFS3ERR_NAMETOOLONG;
                break;

        case ENOTEMPTY:
                stat = NFS3ERR_NOTEMPTY;
                break;

        case EFAULT:
                stat = NFS3ERR_SERVERFAULT;
                break;

        case ENOSYS:
                stat = NFS3ERR_NOTSUPP;
                break;

        case EBADF:
                stat = NFS3ERR_BADTYPE;
                break;

        case ESTALE:
                stat = NFS3ERR_STALE;
                break;

        default:
                stat = NFS3ERR_SERVERFAULT;
                break;
        }

        return stat;
}


void
nfs3_fill_lookup3res_error (lookup3res *res, nfsstat3 stat,
                            struct iatt *dirstat)
{

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (!dirstat) {
                res->lookup3res_u.resfail.dir_attributes.attributes_follow = FALSE;
        }

}

fattr3
nfs3_stat_to_fattr3 (struct iatt *buf)
{
        fattr3          fa = {0, };

        if (IA_ISDIR (buf->ia_type))
                fa.type = NF3DIR;
        else if (IA_ISREG (buf->ia_type))
                fa.type = NF3REG;
        else if (IA_ISCHR (buf->ia_type))
                fa.type = NF3CHR;
        else if (IA_ISBLK (buf->ia_type))
                fa.type = NF3BLK;
        else if (IA_ISFIFO (buf->ia_type))
                fa.type = NF3FIFO;
        else if (IA_ISLNK (buf->ia_type))
                fa.type = NF3LNK;
        else if (IA_ISSOCK (buf->ia_type))
                fa.type = NF3SOCK;

        if (IA_PROT_RUSR (buf->ia_prot))
                fa.mode |= NFS3MODE_ROWNER;
        if (IA_PROT_WUSR (buf->ia_prot))
                fa.mode |= NFS3MODE_WOWNER;
        if (IA_PROT_XUSR (buf->ia_prot))
                fa.mode |= NFS3MODE_XOWNER;

        if (IA_PROT_RGRP (buf->ia_prot))
                fa.mode |= NFS3MODE_RGROUP;
        if (IA_PROT_WGRP (buf->ia_prot))
                fa.mode |= NFS3MODE_WGROUP;
        if (IA_PROT_XGRP (buf->ia_prot))
                fa.mode |= NFS3MODE_XGROUP;

        if (IA_PROT_ROTH (buf->ia_prot))
                fa.mode |= NFS3MODE_ROTHER;
        if (IA_PROT_WOTH (buf->ia_prot))
                fa.mode |= NFS3MODE_WOTHER;
        if (IA_PROT_XOTH (buf->ia_prot))
                fa.mode |= NFS3MODE_XOTHER;

        if (IA_PROT_SUID (buf->ia_prot))
                fa.mode |= NFS3MODE_SETXUID;
        if (IA_PROT_SGID (buf->ia_prot))
                fa.mode |= NFS3MODE_SETXGID;
        if (IA_PROT_STCKY (buf->ia_prot))
                fa.mode |= NFS3MODE_SAVESWAPTXT;

        fa.nlink = buf->ia_nlink;
        fa.uid = buf->ia_uid;
        fa.gid = buf->ia_gid;
        fa.size = buf->ia_size;
        fa.used = (buf->ia_blocks * 512);

        if ((IA_ISCHR (buf->ia_type) || IA_ISBLK (buf->ia_type))) {
                fa.rdev.specdata1 = ia_major (buf->ia_rdev);
                fa.rdev.specdata2 = ia_minor (buf->ia_rdev);
        } else {
                fa.rdev.specdata1 = 0;
                fa.rdev.specdata2 = 0;
        }

        fa.fsid = buf->ia_dev;
        fa.fileid = buf->ia_ino;
        /* FIXME: Handle time resolutions for sub-second granularity */
        if (buf->ia_atime == 9669) {
                fa.mtime.seconds = 0;
                fa.mtime.nseconds = 0;
                fa.atime.seconds = 0;
                fa.atime.nseconds = 0;
        } else {
                fa.mtime.seconds = buf->ia_mtime;
                fa.mtime.nseconds = 0;
                fa.atime.seconds = buf->ia_atime;
                fa.atime.seconds = 0;
                fa.atime.nseconds = 0;
        }

        fa.atime.seconds = buf->ia_atime;
        fa.atime.nseconds = 0;

        fa.ctime.seconds = buf->ia_ctime;
        fa.ctime.nseconds = 0;

        return fa;
}


post_op_attr
nfs3_stat_to_post_op_attr (struct iatt *buf)
{
        post_op_attr    attr = {0, };
        if (!buf)
                return attr;

        /* Some performance translators return zero-filled stats when they
         * do not have up-to-date attributes. Need to handle this by not
         * returning these zeroed out attrs.
         */
        attr.attributes_follow = FALSE;
        if (nfs_zero_filled_stat (buf))
                goto out;

        attr.post_op_attr_u.attributes = nfs3_stat_to_fattr3 (buf);
        attr.attributes_follow = TRUE;

out:
        return attr;
}


pre_op_attr
nfs3_stat_to_pre_op_attr (struct iatt *pre)
{
        pre_op_attr     poa = {0, };

        /* Some performance translators return zero-filled stats when they
         * do not have up-to-date attributes. Need to handle this by not
         * returning these zeroed out attrs.
         */
        poa.attributes_follow = FALSE;
        if (nfs_zero_filled_stat (pre))
                goto out;

        poa.attributes_follow = TRUE;
        poa.pre_op_attr_u.attributes.size = pre->ia_size;
        if (pre->ia_atime == 9669)
                poa.pre_op_attr_u.attributes.mtime.seconds = 0;
        else
                poa.pre_op_attr_u.attributes.mtime.seconds = pre->ia_mtime;
        poa.pre_op_attr_u.attributes.ctime.seconds = pre->ia_ctime;

out:
        return poa;
}

void
nfs3_fill_lookup3res_success (lookup3res *res, nfsstat3 stat,
                              struct nfs3_fh *fh, struct iatt *buf,
                              struct iatt *postparent)
{
        post_op_attr    obj, dir;
        uint32_t        fhlen = 0;

        res->status = stat;
        if (fh) {
                res->lookup3res_u.resok.object.data.data_val = (void *)fh;
                fhlen = nfs3_fh_compute_size (fh);
                res->lookup3res_u.resok.object.data.data_len = fhlen;
        }

        obj.attributes_follow = FALSE;
        dir.attributes_follow = FALSE;
        if (buf && fh) {
                nfs3_map_xlid_to_statdev (buf, fh->xlatorid);
                obj = nfs3_stat_to_post_op_attr (buf);
        }

        if (postparent && fh) {
                nfs3_map_xlid_to_statdev (postparent, fh->xlatorid);
                dir = nfs3_stat_to_post_op_attr (postparent);
        }

        res->lookup3res_u.resok.obj_attributes = obj;
        res->lookup3res_u.resok.dir_attributes = dir;
}


void
nfs3_fill_lookup3res (lookup3res *res, nfsstat3 stat, struct nfs3_fh *newfh,
                      struct iatt *buf, struct iatt *postparent)
{

        memset (res, 0, sizeof (*res));
        if (stat != NFS3_OK)
                nfs3_fill_lookup3res_error (res, stat, postparent);
        else
                nfs3_fill_lookup3res_success (res, stat, newfh, buf,
                                              postparent);
}

struct nfs3_fh
nfs3_extract_getattr_fh (getattr3args *args)
{
        return nfs3_extract_nfs3_fh(args->object);
}


void
nfs3_fill_getattr3res (getattr3res *res, nfsstat3 stat, struct iatt *buf,
                       uint16_t xlid)
{

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (buf, xlid);
        res->getattr3res_u.resok.obj_attributes = nfs3_stat_to_fattr3 (buf);

}


struct nfs3_fh
nfs3_extract_fsinfo_fh (fsinfo3args *args)
{
        return nfs3_extract_nfs3_fh (args->fsroot);
}


void
nfs3_fill_fsinfo3res (struct nfs3_state *nfs3, fsinfo3res *res,
                      nfsstat3 status, struct iatt *fsroot, uint16_t xlid)
{
        fsinfo3resok    resok = {{0}, };
        nfstime3        tdelta = GF_NFS3_TIMEDELTA_SECS;

        memset (res, 0, sizeof (*res));
        res->status = status;
        if (status != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (fsroot, xlid);
        resok.obj_attributes = nfs3_stat_to_post_op_attr (fsroot);
        resok.rtmax = nfs3->readsize;
        resok.rtpref = nfs3->readsize;
        resok.rtmult = GF_NFS3_RTMULT;
        resok.wtmax = nfs3->writesize;
        resok.wtpref = nfs3->writesize;
        resok.wtmult = GF_NFS3_WTMULT;
        resok.dtpref = nfs3->readdirsize;
        resok.maxfilesize = GF_NFS3_MAXFILE;
        resok.time_delta = tdelta;
        resok.properties = GF_NFS3_FS_PROP;

        res->fsinfo3res_u.resok = resok;

}


void
nfs3_prep_lookup3args (lookup3args *args, struct nfs3_fh *fh, char *name)
{
        memset (args, 0, sizeof (*args));
        args->what.dir.data.data_val = (void *)fh;
        args->what.name = name;
}


void
nfs3_prep_getattr3args (getattr3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->object.data.data_val = (void *)fh;
}


void
nfs3_prep_fsinfo3args (fsinfo3args *args, struct nfs3_fh *root)
{
        memset (args, 0, sizeof (*args));
        args->fsroot.data.data_val = (void *)root;
}


char *
nfsstat3_strerror(int stat)
{
        int i;
        for(i = 0; nfs3stat_strerror_table[i].stat != -1; i++) {
                if (nfs3stat_strerror_table[i].stat == stat)
                        return nfs3stat_strerror_table[i].strerror;
        }

        return nfs3stat_strerror_table[i].strerror;
}



void
nfs3_prep_access3args (access3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->object.data.data_val = (void *)fh;
}


uint32_t
nfs3_owner_accessbits (ia_prot_t prot, ia_type_t type, uint32_t request)
{
        uint32_t accresult = 0;

        if (IA_PROT_RUSR (prot) && (request & ACCESS3_READ))
                accresult |= ACCESS3_READ;

        if (request & ACCESS3_LOOKUP)
                if ((IA_ISDIR (type)) && (IA_PROT_XUSR (prot)))
                        accresult |= ACCESS3_LOOKUP;

        if ((IA_PROT_WUSR (prot) && (request & ACCESS3_MODIFY)))
                accresult |= ACCESS3_MODIFY;

        if ((IA_PROT_WUSR (prot) && (request & ACCESS3_EXTEND)))
                accresult |= ACCESS3_EXTEND;

        /* ACCESS3_DELETE is ignored for now since that requires
         * knowing the permissions on the parent directory.
         */

        if (request & ACCESS3_EXECUTE)
                if (IA_PROT_XUSR (prot) && (!IA_ISDIR (type)))
                        accresult |= ACCESS3_EXECUTE;

        return accresult;
}


uint32_t
nfs3_group_accessbits (ia_prot_t prot, ia_type_t type, uint32_t request)
{
        uint32_t accresult = 0;

        if (IA_PROT_RGRP (prot) && (request & ACCESS3_READ))
                accresult |= ACCESS3_READ;

        if (request & ACCESS3_LOOKUP)
                if ((IA_ISDIR (type)) && IA_PROT_RGRP (prot))
                        accresult |= ACCESS3_LOOKUP;

        if (IA_PROT_WGRP (prot) && (request & ACCESS3_MODIFY))
                accresult |= ACCESS3_MODIFY;

        if (IA_PROT_WGRP (prot) && (request & ACCESS3_EXTEND))
                accresult |= ACCESS3_EXTEND;

        /* ACCESS3_DELETE is ignored for now since that requires
         * knowing the permissions on the parent directory.
         */

        if (request & ACCESS3_EXECUTE)
                if (IA_PROT_XGRP (prot) && (!IA_ISDIR (type)))
                        accresult |= ACCESS3_EXECUTE;

        return accresult;
}


uint32_t
nfs3_other_accessbits (ia_prot_t prot, ia_type_t type, uint32_t request)
{
        uint32_t accresult = 0;

        if (IA_PROT_ROTH (prot) && (request & ACCESS3_READ))
                accresult |= ACCESS3_READ;

        if (request & ACCESS3_LOOKUP)
                if (IA_ISDIR (type) && IA_PROT_ROTH (prot))
                        accresult |= ACCESS3_LOOKUP;

        if (IA_PROT_WOTH (prot) && (request & ACCESS3_MODIFY))
                accresult |= ACCESS3_MODIFY;

        if (IA_PROT_WOTH (prot) && (request & ACCESS3_EXTEND))
                accresult |= ACCESS3_EXTEND;

        /* ACCESS3_DELETE is ignored for now since that requires
         * knowing the permissions on the parent directory.
         */

        if (request & ACCESS3_EXECUTE)
                if (IA_PROT_XOTH (prot) && (!IA_ISDIR (type)))
                        accresult |= ACCESS3_EXECUTE;

        return accresult;
}


uint32_t
nfs3_superuser_accessbits (ia_prot_t prot, ia_type_t type, uint32_t request)
{
        uint32_t accresult = 0;

        if (request & ACCESS3_READ)
                accresult |= ACCESS3_READ;

        if (request & ACCESS3_LOOKUP)
                if (IA_ISDIR (type))
                        accresult |= ACCESS3_LOOKUP;

        if (request & ACCESS3_MODIFY)
                accresult |= ACCESS3_MODIFY;

        if (request & ACCESS3_EXTEND)
                accresult |= ACCESS3_EXTEND;

        /* ACCESS3_DELETE is ignored for now since that requires
         * knowing the permissions on the parent directory.
         */

        if (request & ACCESS3_EXECUTE)
                if ((IA_PROT_XOTH (prot) || IA_PROT_XUSR (prot) ||
                     IA_PROT_XGRP (prot)) && (!IA_ISDIR (type)))
                        accresult |= ACCESS3_EXECUTE;

        return accresult;
}


uint32_t
nfs3_stat_to_accessbits (struct iatt *buf, uint32_t request, uid_t uid,
                         gid_t gid)
{
        uint32_t        accresult = 0;
        ia_prot_t       prot = {0, };
        ia_type_t       type = 0;

        prot = buf->ia_prot;
        type = buf->ia_type;

        if (uid == 0)
                accresult = nfs3_superuser_accessbits (prot, type, request);
        else if (buf->ia_uid == uid)
                accresult = nfs3_owner_accessbits (prot, type, request);
        else if (buf->ia_gid == gid)
                accresult = nfs3_group_accessbits (prot, type, request);
        else
                accresult = nfs3_other_accessbits (prot, type, request);

        return accresult;
}


void
nfs3_fill_access3res (access3res *res, nfsstat3 status, struct iatt *buf,
                      uint32_t accbits, uid_t uid, gid_t gid, uint16_t xlid)
{
        post_op_attr    objattr;
        uint32_t        accres = 0;

        memset (res, 0, sizeof (*res));
        res->status = status;
        if (status != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (buf, xlid);
        objattr = nfs3_stat_to_post_op_attr (buf);
        accres = nfs3_stat_to_accessbits (buf, accbits, uid, gid);

        res->access3res_u.resok.obj_attributes = objattr;
        res->access3res_u.resok.access = accres;
}

void
nfs3_prep_readdir3args (readdir3args *ra, struct nfs3_fh *fh)
{
        memset (ra, 0, sizeof (*ra));
        ra->dir.data.data_val = (void *)fh;
}


int
nfs3_is_dot_entry (char *entry)
{
        int     ret = 0;

        if (!entry)
                return 0;

        if (strcmp (entry, ".") == 0)
                ret = 1;

        return ret;
}


int
nfs3_is_parentdir_entry (char *entry)
{
        int     ret = 0;

        if (!entry)
                return 0;

        if (strcmp (entry, "..") == 0)
                ret = 1;

        return ret;
}


void
nfs3_funge_root_dotdot_dirent (gf_dirent_t *ent, struct nfs3_fh *dfh)
{
        if ((!ent) || (!dfh))
                return;

        if (nfs3_fh_is_root_fh (dfh) &&
            nfs3_is_parentdir_entry (ent->d_name)) {
                ent->d_ino = 1;
                ent->d_stat.ia_ino = 1;
                ent->d_stat.ia_gen = 0;
        }

        if (nfs3_fh_is_root_fh (dfh) &&
            nfs3_is_dot_entry (ent->d_name)) {
                ent->d_ino = 1;
                ent->d_stat.ia_ino = 1;
                ent->d_stat.ia_gen = 0;
        }

}


entry3 *
nfs3_fill_entry3 (gf_dirent_t *entry, struct nfs3_fh *dfh)
{
        entry3          *ent = NULL;
        if ((!entry) || (!dfh))
                return NULL;

        ent = GF_CALLOC (1, sizeof (*ent), gf_nfs_mt_entry3);
        if (!ent)
                return NULL;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Entry: %s", entry->d_name);

        /* If the entry is . or .., we need to replace the physical ino and gen
         * with 1 and 0 respectively if the directory is root. This funging is
         * needed because there is no parent directory of the root. In that
         * sense the behavious we provide is similar to the output of the
         * command: "stat /.."
         */
        nfs3_funge_root_dotdot_dirent (entry, dfh);
        ent->fileid = entry->d_ino;
        ent->cookie = entry->d_off;
        ent->name = GF_CALLOC ((strlen (entry->d_name) + 1), sizeof (char),
                               gf_nfs_mt_char);
        if (!ent->name) {
                GF_FREE (ent);
                ent = NULL;
                goto err;
        }
        strcpy (ent->name, entry->d_name);

err:
        return ent;
}


void
nfs3_fill_post_op_fh3 (struct nfs3_fh *fh, post_op_fh3 *pfh)
{
        uint32_t        fhlen = 0;

        if ((!fh) || (!pfh))
                return;

        pfh->handle_follows = 1;
        fhlen = nfs3_fh_compute_size (fh);
        pfh->post_op_fh3_u.handle.data.data_val = (void *)fh;
        pfh->post_op_fh3_u.handle.data.data_len = fhlen;
}


post_op_fh3
nfs3_fh_to_post_op_fh3 (struct nfs3_fh *fh)
{
        post_op_fh3     pfh = {0, };
        char            *fhp = NULL;

        if (!fh)
                return pfh;

        pfh.handle_follows = 1;

        fhp = GF_CALLOC (1, sizeof (*fh), gf_nfs_mt_char);
        if (!fhp)
                return pfh;

        memcpy (fhp, fh, sizeof (*fh));
        nfs3_fill_post_op_fh3 ((struct nfs3_fh *)fhp, &pfh);
        return pfh;
}


entryp3 *
nfs3_fill_entryp3 (gf_dirent_t *entry, struct nfs3_fh *dirfh)
{
        entryp3         *ent = NULL;
        struct nfs3_fh  newfh = {{0}, };

        if ((!entry) || (!dirfh))
                return NULL;

        /* If the entry is . or .., we need to replace the physical ino and gen
         * with 1 and 0 respectively if the directory is root. This funging is
         * needed because there is no parent directory of the root. In that
         * sense the behavious we provide is similar to the output of the
         * command: "stat /.."
         */
        nfs3_funge_root_dotdot_dirent (entry, dirfh);
        gf_log (GF_NFS3, GF_LOG_TRACE, "Entry: %s, ino: %"PRIu64,
                entry->d_name, entry->d_ino);
        ent = GF_CALLOC (1, sizeof (*ent), gf_nfs_mt_entryp3);
        if (!ent)
                return NULL;

        ent->fileid = entry->d_ino;
        ent->cookie = entry->d_off;
        ent->name = GF_CALLOC ((strlen (entry->d_name) + 1), sizeof (char),
                               gf_nfs_mt_char);
        if (!ent->name) {
                GF_FREE (ent);
                ent = NULL;
                goto err;
        }
        strcpy (ent->name, entry->d_name);

        nfs3_fh_build_child_fh (dirfh, &entry->d_stat, &newfh);
        nfs3_map_xlid_to_statdev (&entry->d_stat, dirfh->xlatorid);
        ent->name_attributes = nfs3_stat_to_post_op_attr (&entry->d_stat);
        ent->name_handle = nfs3_fh_to_post_op_fh3 (&newfh);
err:
        return ent;
}


void
nfs3_fill_readdir3res (readdir3res *res, nfsstat3 stat, struct nfs3_fh *dirfh,
                       uint64_t cverf, struct iatt *dirstat,
                       gf_dirent_t *entries, count3 count, int is_eof)
{
        post_op_attr    dirattr;
        entry3          *ent = NULL;
        entry3          *headentry = NULL;
        entry3          *preventry = NULL;
        count3          filled = 0;
        gf_dirent_t     *listhead = NULL;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (dirstat, dirfh->xlatorid);
        dirattr = nfs3_stat_to_post_op_attr (dirstat);
        res->readdir3res_u.resok.dir_attributes = dirattr;
        res->readdir3res_u.resok.reply.eof = (bool_t)is_eof;
        memcpy (res->readdir3res_u.resok.cookieverf, &cverf, sizeof (cverf));

        filled = NFS3_READDIR_RESOK_SIZE;
        /* First entry is just the list head */
        listhead = entries;
        entries = entries->next;
        while (((entries) && (entries != listhead)) && (filled < count)) {
                /*
                if ((strcmp (entries->d_name, ".") == 0) ||
                    (strcmp (entries->d_name, "..") == 0))
                        goto nextentry;
                        */
                ent = nfs3_fill_entry3 (entries, dirfh);
                if (!ent)
                        break;

                if (!headentry)
                        headentry = ent;

                if (preventry) {
                        preventry->nextentry = ent;
                        preventry = ent;
                } else
                        preventry = ent;

                filled += NFS3_ENTRY3_FIXED_SIZE + strlen (ent->name);
//nextentry:
                entries = entries->next;
        }

        res->readdir3res_u.resok.reply.entries = headentry;

        return;
}


void
nfs3_fill_readdirp3res (readdirp3res *res, nfsstat3 stat, struct nfs3_fh *dirfh,
                        uint64_t cverf, struct iatt *dirstat,
                        gf_dirent_t *entries, count3 dircount, count3 maxcount,
                        int is_eof)
{
        post_op_attr    dirattr;
        entryp3         *ent = NULL;
        entryp3         *headentry = NULL;
        entryp3         *preventry = NULL;
        count3          filled = 0;
        gf_dirent_t     *listhead = NULL;
        int             fhlen = 0;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (dirstat, dirfh->xlatorid);
        dirattr = nfs3_stat_to_post_op_attr (dirstat);
        res->readdirp3res_u.resok.dir_attributes = dirattr;
        res->readdirp3res_u.resok.reply.eof = (bool_t)is_eof;
        memcpy (res->readdirp3res_u.resok.cookieverf, &cverf, sizeof (cverf));

        filled = NFS3_READDIR_RESOK_SIZE;
        /* First entry is just the list head */
        listhead = entries;
        entries = entries->next;
        while (((entries) && (entries != listhead)) && (filled < maxcount)) {
                /* Linux does not display . and .. entries unless we provide
                 * these entries here.
                 */
/*                if ((strcmp (entries->d_name, ".") == 0) ||
                    (strcmp (entries->d_name, "..") == 0))
                        goto nextentry;
                        */
                ent = nfs3_fill_entryp3 (entries, dirfh);
                if (!ent)
                        break;

                if (!headentry)
                        headentry = ent;

                if (preventry) {
                        preventry->nextentry = ent;
                        preventry = ent;
                } else
                        preventry = ent;

                fhlen = ent->name_handle.post_op_fh3_u.handle.data.data_len;
                filled += NFS3_ENTRYP3_FIXED_SIZE + fhlen + strlen (ent->name);
//nextentry:
                entries = entries->next;
        }

        res->readdirp3res_u.resok.reply.entries = headentry;

        return;
}


void
nfs3_prep_readdirp3args (readdirp3args *ra, struct nfs3_fh *fh)
{
        memset (ra, 0, sizeof (*ra));
        ra->dir.data.data_val = (void *)fh;
}

void
nfs3_free_readdirp3res (readdirp3res *res)
{
        entryp3 *ent = NULL;
        entryp3 *next = NULL;

        if (!res)
                return;

        ent = res->readdirp3res_u.resok.reply.entries;
        while (ent) {

                next = ent->nextentry;
                GF_FREE (ent->name);
                GF_FREE (ent->name_handle.post_op_fh3_u.handle.data.data_val);
                GF_FREE (ent);
                ent = next;
        }

        return;
}


void
nfs3_free_readdir3res (readdir3res *res)
{
        entry3 *ent = NULL;
        entry3 *next = NULL;

        if (!res)
                return;

        ent = res->readdir3res_u.resok.reply.entries;
        while (ent) {

                next = ent->nextentry;
                GF_FREE (ent->name);
                GF_FREE (ent);
                ent = next;
        }

        return;
}

void
nfs3_prep_fsstat3args (fsstat3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->fsroot.data.data_val = (char *)fh;
}


void
nfs3_fill_fsstat3res (fsstat3res *res, nfsstat3 stat, struct statvfs *fsbuf,
                      struct iatt *postbuf, uint16_t xlid)
{
        post_op_attr    poa;
        fsstat3resok    resok;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (postbuf, xlid);
        poa = nfs3_stat_to_post_op_attr (postbuf);
        resok.tbytes = (size3)(fsbuf->f_frsize * fsbuf->f_blocks);
        resok.fbytes = (size3)(fsbuf->f_bsize * fsbuf->f_bfree);
        resok.abytes = (size3)(fsbuf->f_bsize * fsbuf->f_bavail);
        resok.tfiles = (size3)(fsbuf->f_files);
        resok.ffiles = (size3)(fsbuf->f_ffree);
        resok.afiles = (size3)(fsbuf->f_favail);
        resok.invarsec = 0;

        resok.obj_attributes = poa;
        res->fsstat3res_u.resok = resok;
}


int32_t
nfs3_sattr3_to_setattr_valid (sattr3 *sattr, struct iatt *buf, mode_t *omode)
{
        int32_t         valid = 0;
        ia_prot_t       prot = {0, };
        mode_t          mode = 0;

        if (!sattr)
                return 0;

        if (sattr->mode.set_it) {
                valid |= GF_SET_ATTR_MODE;

                if (sattr->mode.set_mode3_u.mode & NFS3MODE_ROWNER) {
                        mode |= S_IRUSR;
                        prot.owner.read = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_WOWNER) {
                        mode |= S_IWUSR;
                        prot.owner.write = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_XOWNER) {
                        mode |= S_IXUSR;
                        prot.owner.exec = 1;
                }

                if (sattr->mode.set_mode3_u.mode & NFS3MODE_RGROUP) {
                        mode |= S_IRGRP;
                        prot.group.read = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_WGROUP) {
                        mode |= S_IWGRP;
                        prot.group.write = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_XGROUP) {
                        mode |= S_IXGRP;
                        prot.group.exec = 1;
                }

                if (sattr->mode.set_mode3_u.mode & NFS3MODE_ROTHER) {
                        mode |= S_IROTH;
                        prot.other.read = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_WOTHER) {
                        mode |= S_IWOTH;
                        prot.other.write = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_XOTHER) {
                        mode |= S_IXOTH;
                        prot.other.exec = 1;
                }

                if (sattr->mode.set_mode3_u.mode & NFS3MODE_SETXUID) {
                        mode |= S_ISUID;
                        prot.suid = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_SETXGID) {
                        mode |= S_ISGID;
                        prot.sgid = 1;
                }
                if (sattr->mode.set_mode3_u.mode & NFS3MODE_SAVESWAPTXT) {
                        mode |= S_ISVTX;
                        prot.sticky = 1;
                }

                if (buf)
                        buf->ia_prot = prot;
                /* Create fop still requires the old mode_t style argument. */
                if (omode)
                        *omode = mode;
        }

        if (sattr->uid.set_it) {
                valid |= GF_SET_ATTR_UID;
                if (buf)
                        buf->ia_uid = sattr->uid.set_uid3_u.uid;
        }

        if (sattr->gid.set_it) {
                valid |= GF_SET_ATTR_GID;
                if (buf)
                        buf->ia_gid = sattr->gid.set_gid3_u.gid;
        }

        if (sattr->size.set_it) {
                valid |= GF_SET_ATTR_SIZE;
                if (buf)
                        buf->ia_size = sattr->size.set_size3_u.size;
        }

        if (sattr->atime.set_it == SET_TO_CLIENT_TIME) {
                valid |= GF_SET_ATTR_ATIME;
                if (buf)
                        buf->ia_atime = sattr->atime.set_atime_u.atime.seconds;
        }

        if (sattr->atime.set_it == SET_TO_SERVER_TIME) {
                valid |= GF_SET_ATTR_ATIME;
                if (buf)
                        buf->ia_atime = time (NULL);
        }

        if (sattr->mtime.set_it == SET_TO_CLIENT_TIME) {
                valid |= GF_SET_ATTR_MTIME;
                if (buf)
                        buf->ia_mtime = sattr->mtime.set_mtime_u.mtime.seconds;
        }

        if (sattr->mtime.set_it == SET_TO_SERVER_TIME) {
                valid |= GF_SET_ATTR_MTIME;
                if (buf)
                        buf->ia_mtime = time (NULL);
        }

        return valid;
}


wcc_data
nfs3_stat_to_wcc_data (struct iatt *pre, struct iatt *post)
{
        wcc_data        wd = {{0}, };

        if (post)
                wd.after = nfs3_stat_to_post_op_attr (post);
        if (pre)
                wd.before = nfs3_stat_to_pre_op_attr (pre);

        return wd;
}

void
nfs3_fill_create3res (create3res *res, nfsstat3 stat, struct nfs3_fh *newfh,
                      struct iatt *newbuf, struct iatt *preparent,
                      struct iatt *postparent)
{
        post_op_attr    poa = {0, };
        wcc_data        dirwcc = {{0}, };

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_fill_post_op_fh3 (newfh, &res->create3res_u.resok.obj);
        nfs3_map_xlid_to_statdev (newbuf, newfh->xlatorid);
        poa = nfs3_stat_to_post_op_attr (newbuf);
        res->create3res_u.resok.obj_attributes = poa;
        if (preparent) {
                nfs3_map_xlid_to_statdev (preparent, newfh->xlatorid);
                nfs3_map_xlid_to_statdev (postparent, newfh->xlatorid);
                dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        }

        res->create3res_u.resok.dir_wcc = dirwcc;
}

void
nfs3_prep_create3args (create3args *args, struct nfs3_fh *fh, char *name)
{

        memset (args, 0, sizeof (*args));
        args->where.dir.data.data_val = (void *)fh;
        args->where.name = name;
}

void
nfs3_prep_setattr3args (setattr3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->object.data.data_val = (void *)fh;
}


void
nfs3_fill_setattr3res (setattr3res *res, nfsstat3 stat, struct iatt *preop,
                       struct iatt *postop, uint16_t xlid)
{
        wcc_data        wcc;
        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (preop, xlid);
        nfs3_map_xlid_to_statdev (postop, xlid);
        wcc = nfs3_stat_to_wcc_data (preop, postop);
        res->setattr3res_u.resok.obj_wcc = wcc;
}


void
nfs3_prep_mkdir3args (mkdir3args *args, struct nfs3_fh *dirfh, char *name)
{

        memset (args, 0, sizeof (*args));
        args->where.dir.data.data_val = (void *)dirfh;
        args->where.name = name;
}


void
nfs3_fill_mkdir3res (mkdir3res *res, nfsstat3 stat, struct nfs3_fh *fh,
                     struct iatt *buf, struct iatt *preparent,
                     struct iatt *postparent)
{
        wcc_data        dirwcc;
        post_op_attr    poa;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_fill_post_op_fh3 (fh, &res->mkdir3res_u.resok.obj);
        nfs3_map_xlid_to_statdev (buf, fh->xlatorid);
        poa = nfs3_stat_to_post_op_attr (buf);
        nfs3_map_xlid_to_statdev (preparent, fh->xlatorid);
        nfs3_map_xlid_to_statdev (postparent, fh->xlatorid);
        dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        res->mkdir3res_u.resok.obj_attributes = poa;
        res->mkdir3res_u.resok.dir_wcc = dirwcc;

}


void
nfs3_prep_symlink3args (symlink3args *args, struct nfs3_fh *dirfh, char *name,
                        char *target)
{
        memset (args, 0, sizeof (*args));
        args->where.dir.data.data_val = (void *)dirfh;
        args->where.name = name;
        args->symlink.symlink_data = target;
}


void
nfs3_fill_symlink3res (symlink3res *res, nfsstat3 stat, struct nfs3_fh *fh,
                       struct iatt *buf, struct iatt *preparent,
                       struct iatt *postparent)
{
        wcc_data        dirwcc;
        post_op_attr    poa;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_fill_post_op_fh3 (fh, &res->symlink3res_u.resok.obj);
        nfs3_map_xlid_to_statdev (buf, fh->xlatorid);
        poa = nfs3_stat_to_post_op_attr (buf);
        nfs3_map_xlid_to_statdev (postparent, fh->xlatorid);
        nfs3_map_xlid_to_statdev (preparent, fh->xlatorid);
        dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        res->symlink3res_u.resok.obj_attributes = poa;
        res->symlink3res_u.resok.dir_wcc = dirwcc;

}


void
nfs3_prep_readlink3args (readlink3args *args, struct nfs3_fh *fh)
{

        memset (args, 0, sizeof (*args));
        args->symlink.data.data_val = (void *)fh;
}


void
nfs3_fill_readlink3res (readlink3res *res, nfsstat3 stat, char *path,
                        struct iatt *buf, uint16_t xlid)
{
        post_op_attr    poa;

        memset (res, 0, sizeof (*res));
        res->status = stat;

        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (buf, xlid);
        poa = nfs3_stat_to_post_op_attr (buf);
        res->readlink3res_u.resok.data = (void *)path;
        res->readlink3res_u.resok.symlink_attributes = poa;
}


void
nfs3_prep_mknod3args (mknod3args *args, struct nfs3_fh *fh, char *name)
{
        memset (args, 0, sizeof (*args));
        args->where.dir.data.data_val = (void *)fh;
        args->where.name = name;

}

void
nfs3_fill_mknod3res (mknod3res *res, nfsstat3 stat, struct nfs3_fh *fh,
                     struct iatt *buf, struct iatt *preparent,
                     struct iatt *postparent)
{
        post_op_attr    poa;
        wcc_data        wccdir;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_fill_post_op_fh3 (fh, &res->mknod3res_u.resok.obj);
        nfs3_map_xlid_to_statdev (buf, fh->xlatorid);
        poa = nfs3_stat_to_post_op_attr (buf);
        nfs3_map_xlid_to_statdev (preparent, fh->xlatorid);
        nfs3_map_xlid_to_statdev (postparent, fh->xlatorid);
        wccdir = nfs3_stat_to_wcc_data (preparent, postparent);
        res->mknod3res_u.resok.obj_attributes = poa;
        res->mknod3res_u.resok.dir_wcc = wccdir;

}


void
nfs3_fill_remove3res (remove3res *res, nfsstat3 stat, struct iatt *preparent,
                      struct iatt *postparent, uint16_t xlid)
{
        wcc_data        dirwcc;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (preparent, xlid);
        nfs3_map_xlid_to_statdev (postparent, xlid);
        dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        res->remove3res_u.resok.dir_wcc = dirwcc;
}


void
nfs3_prep_remove3args (remove3args *args, struct nfs3_fh *fh, char *name)
{
        memset (args, 0, sizeof (*args));
        args->object.dir.data.data_val = (void *)fh;
        args->object.name = name;
}


void
nfs3_prep_rmdir3args (rmdir3args *args, struct nfs3_fh *fh, char *name)
{
        memset (args, 0, sizeof (*args));
        args->object.dir.data.data_val = (void *)fh;
        args->object.name = name;
}


void
nfs3_fill_rmdir3res (rmdir3res *res, nfsstat3 stat, struct iatt *preparent,
                     struct iatt *postparent, uint16_t xlid)
{
        wcc_data        dirwcc;
        memset (res, 0, sizeof (*res));
        res->status = stat;

        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (postparent, xlid);
        nfs3_map_xlid_to_statdev (preparent, xlid);
        dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        res->rmdir3res_u.resok.dir_wcc = dirwcc;
}


void
nfs3_prep_link3args (link3args *args, struct nfs3_fh *target,
                     struct nfs3_fh * dirfh, char *name)
{
        memset (args, 0, sizeof (*args));
        args->file.data.data_val = (void *)target;
        args->link.dir.data.data_val = (void *)dirfh;
        args->link.name = name;
}


void
nfs3_fill_link3res (link3res *res, nfsstat3 stat, struct iatt *buf,
                    struct iatt *preparent, struct iatt *postparent,
                    uint16_t xlid)
{
        post_op_attr    poa;
        wcc_data        dirwcc;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (preparent, xlid);
        nfs3_map_xlid_to_statdev (postparent, xlid);
        nfs3_map_xlid_to_statdev (buf, xlid);
        poa = nfs3_stat_to_post_op_attr (buf);
        dirwcc = nfs3_stat_to_wcc_data (preparent, postparent);
        res->link3res_u.resok.file_attributes = poa;
        res->link3res_u.resok.linkdir_wcc = dirwcc;
}



void
nfs3_prep_rename3args (rename3args *args, struct nfs3_fh *olddirfh,
                       char *oldname, struct nfs3_fh *newdirfh, char *newname)
{
        memset (args, 0, sizeof (*args));

        args->from.name = oldname;
        args->from.dir.data.data_val = (void *)olddirfh;
        args->to.name = newname;
        args->to.dir.data.data_val = (void *)newdirfh;

}


void
nfs3_fill_rename3res (rename3res *res, nfsstat3 stat, struct iatt *buf,
                      struct iatt *preoldparent, struct iatt *postoldparent,
                      struct iatt *prenewparent, struct iatt *postnewparent,
                      uint16_t xlid)

{
        wcc_data        dirwcc;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (preoldparent, xlid);
        nfs3_map_xlid_to_statdev (postoldparent, xlid);
        nfs3_map_xlid_to_statdev (prenewparent, xlid);
        nfs3_map_xlid_to_statdev (postnewparent, xlid);
        nfs3_map_xlid_to_statdev (buf, xlid);
        dirwcc = nfs3_stat_to_wcc_data (preoldparent, postoldparent);
        res->rename3res_u.resok.fromdir_wcc = dirwcc;
        dirwcc = nfs3_stat_to_wcc_data (prenewparent, postnewparent);
        res->rename3res_u.resok.todir_wcc = dirwcc;
}


void
nfs3_prep_write3args (write3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->file.data.data_val = (void *)fh;
}


void
nfs3_fill_write3res (write3res *res, nfsstat3 stat, count3 count,
                     stable_how stable, uint64_t wverf, struct iatt *prestat,
                     struct iatt *poststat, uint16_t xlid)
{
        write3resok     resok;
        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (prestat, xlid);
        nfs3_map_xlid_to_statdev (poststat, xlid);
        resok.file_wcc = nfs3_stat_to_wcc_data (prestat, poststat);
        resok.count = count;
        resok.committed = stable;
        memcpy (resok.verf, &wverf, sizeof (wverf));

        res->write3res_u.resok = resok;
}


void
nfs3_prep_commit3args (commit3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->file.data.data_val = (void *)fh;
}


void
nfs3_fill_commit3res (commit3res *res, nfsstat3 stat, uint64_t wverf,
                      struct iatt *prestat, struct iatt *poststat,uint16_t xlid)
{
        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (poststat, xlid);
        nfs3_map_xlid_to_statdev (prestat, xlid);
        res->commit3res_u.resok.file_wcc = nfs3_stat_to_wcc_data (prestat,
                                                                  poststat);
        memcpy (res->commit3res_u.resok.verf, &wverf, sizeof (wverf));
}

void
nfs3_fill_read3res (read3res *res, nfsstat3 stat, count3 count,
                    struct iatt *poststat, int is_eof, uint16_t xlid)
{
        post_op_attr    poa;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (poststat, xlid);
        poa = nfs3_stat_to_post_op_attr (poststat);
        res->read3res_u.resok.file_attributes = poa;
        res->read3res_u.resok.count = count;
        res->read3res_u.resok.eof = is_eof;
        res->read3res_u.resok.data.data_len = count;
}


void
nfs3_prep_read3args (read3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->file.data.data_val = (void *)fh;
}


void
nfs3_fill_pathconf3res (pathconf3res *res, nfsstat3 stat, struct iatt *buf,
                        uint16_t xlid)
{
        pathconf3resok  resok;

        memset (res, 0, sizeof (*res));
        res->status = stat;
        if (stat != NFS3_OK)
                return;

        nfs3_map_xlid_to_statdev (buf, xlid);
        resok.obj_attributes = nfs3_stat_to_post_op_attr (buf);
        resok.linkmax = 256;
        resok.name_max = NFS_NAME_MAX;
        resok.no_trunc = TRUE;
        resok.chown_restricted = FALSE;
        resok.case_insensitive = FALSE;
        resok.case_preserving = TRUE;

        res->pathconf3res_u.resok = resok;
}


void
nfs3_prep_pathconf3args (pathconf3args *args, struct nfs3_fh *fh)
{
        memset (args, 0, sizeof (*args));
        args->object.data.data_val = (void *)fh;
}


int
nfs3_verify_dircookie (struct nfs3_state *nfs3, fd_t *dirfd, cookie3 cookie,
                       uint64_t cverf, nfsstat3 *stat)
{
        int             ret = -1;

        if ((!nfs3) || (!dirfd))
                return -1;

        /* Can assume that this is first read on the dir, so cookie check
         * is successful by default.
         */
        if (cookie == 0)
                return 0;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Verifying cookie: cverf: %"PRIu64
                ", cookie: %"PRIu64, cverf, cookie);
        /* The cookie bad, no way cverf will be zero with a non-zero cookie. */
        if ((cverf == 0) && (cookie != 0)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Bad cookie requested");
                if (stat)
                        *stat = NFS3ERR_BAD_COOKIE;
                goto err;
        }

        /* Yes, its true, our cookie is simply the fd_t address.
         * NOTE: We used have the check for cookieverf but VMWare client sends
         * a readdirp requests even after we've told it that EOF has been
         * reached on the directory. This causes a problem because we close a
         * dir fd_t after reaching EOF. The next readdirp sent by VMWare
         * contains the address of the closed fd_t as cookieverf. Since we
         * closed that fd_t, this readdirp results in a new opendir which will
         * give an fd_t that will fail this check below.
         */
/*        if ((cverf != (uint64_t)dirfd)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Cookieverf does not match");
                if (stat)
                        *stat = NFS3ERR_BAD_COOKIE;
                goto err;
        }
*/
        gf_log (GF_NFS3, GF_LOG_TRACE, "Cookie verified");
        if (stat)
                *stat = NFS3_OK;
        ret = 0;
err:
        return ret;
}


/* When remove a file, we need to unref the cached fd for an inode but this
 * needs to happen only when the file was in fact opened. However, it is
 * possible that fd_lookup on a file returns an fd because the file was in
 * process of being created(which also returns an fd) but since this fd was not
 * opened through this path, in the NFS3 remove path, we'll end up removing the
 * reference that belongs to someone else. That means, nfs3 remove path should
 * not unref unless it is sure that the file was cached open also. If it was,
 * only then perform the fd_unref, else not.
 *
 * We determine that using a flag in the inode context.
 */
int
nfs3_set_inode_opened (xlator_t *nfsxl, inode_t *inode)
{
        if ((!nfsxl) || (!inode))
                return -1;

        inode_ctx_put (inode, nfsxl, GF_NFS3_FD_CACHED);

        return 0;
}


/* Returns 1 if inode was cached open, otherwise 0 */
int
nfs3_cached_inode_opened (xlator_t *nfsxl, inode_t *inode)
{
        int             ret = -1;
        uint64_t        cflag = 0;

        if ((!nfsxl) || (!inode))
                return -1;

        ret = inode_ctx_get (inode, nfsxl, &cflag);
        if (ret == -1)
                ret = 0;
        else if (cflag == GF_NFS3_FD_CACHED)
                ret = 1;

        return ret;
}


int32_t
nfs3_dir_open_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, fd_t *fd)
{
        nfs3_call_state_t       *cs = NULL;

        cs = frame->local;
        if (op_ret == -1) {
                cs->resolve_ret = -1;
                cs->resolve_errno = op_errno;
                nfs3_call_resume (cs);
                goto err;
        }

        cs->fd = fd_ref (fd);
        nfs3_set_inode_opened (cs->nfsx, cs->resolvedloc.inode);
        gf_log (GF_NFS3, GF_LOG_TRACE, "FD_REF: %d", fd->refcount);
        nfs3_call_resume (cs);
err:
        return 0;
}


int
__nfs3_dir_open_and_resume (nfs3_call_state_t *cs)
{
        nfs_user_t      nfu = {0, };
        int             ret = -EFAULT;

        if (!cs)
                return ret;

        nfs_user_root_create (&nfu);
        ret = nfs_opendir (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                           nfs3_dir_open_cbk, cs);
        return ret;
}


int
nfs3_dir_open_and_resume (nfs3_call_state_t *cs, nfs3_resume_fn_t resume)
{
        fd_t    *fd = NULL;
        int     ret = -EFAULT;

        if ((!cs))
                return ret;

        cs->resume_fn = resume;
        gf_log (GF_NFS3, GF_LOG_TRACE, "Opening: %s", cs->resolvedloc.path);
        fd = fd_lookup (cs->resolvedloc.inode, 0);
        if (fd) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "fd found in state: ref: %d", fd->refcount);
                cs->fd = fd;    /* Gets unrefd when the call state is wiped. */
                cs->resolve_ret = 0;
                nfs3_call_resume (cs);
                ret = 0;
                goto err;
        }

        ret = __nfs3_dir_open_and_resume (cs);

err:
        return ret;
}


int
nfs3_flush_call_state (nfs3_call_state_t *cs, fd_t *openedfd)
{
        if ((!cs) || (!openedfd))
                return -1;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Calling resume");
        cs->resolve_ret = 0;
        gf_log (GF_NFS3, GF_LOG_TRACE, "Opening uncached fd done: %d",
                openedfd->refcount);
        cs->fd = fd_ref (openedfd);
        list_del (&cs->openwait_q);
        nfs3_call_resume (cs);

        return 0;
}


int
nfs3_flush_inode_queue (struct inode_op_queue *inode_q, fd_t *openedfd)
{
        nfs3_call_state_t       *cstmp = NULL;
        nfs3_call_state_t       *cs = NULL;

        if ((!openedfd) || (!inode_q))
                return -1;

        list_for_each_entry_safe (cs, cstmp, &inode_q->opq, openwait_q)
                nfs3_flush_call_state (cs, openedfd);

        return 0;
}


int
nfs3_flush_open_wait_call_states (nfs3_call_state_t *cs, fd_t *openedfd)
{
        struct inode_op_queue   *inode_q = NULL;
        uint64_t                ctxaddr = 0;
        int                     ret = 0;

        if (!cs)
                return -1;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Flushing call state");
        ret = inode_ctx_get (cs->resolvedloc.inode, cs->nfsx, &ctxaddr);
        if (ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "No inode queue present");
                goto out;
        }

        inode_q = (struct inode_op_queue *)(long)ctxaddr;
        if (!inode_q)
                goto out;

        pthread_mutex_lock (&inode_q->qlock);
        {
                nfs3_flush_inode_queue (inode_q, openedfd);
        }
        pthread_mutex_unlock (&inode_q->qlock);

out:
        return 0;
}


int
__nfs3_fdcache_update_entry (struct nfs3_state *nfs3, fd_t *fd)
{
        uint64_t                ctxaddr = 0;
        struct nfs3_fd_entry    *fde = NULL;

        if ((!nfs3) || (!fd))
                return -1;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Updating fd: 0x%lx", (long int)fd);
        fd_ctx_get (fd, nfs3->nfsx, &ctxaddr);
        fde = (struct nfs3_fd_entry *)(long)ctxaddr;
        if (fde) {
                list_del (&fde->list);
                list_add_tail (&fde->list, &nfs3->fdlru);
        }

        return 0;
}


int
nfs3_fdcache_update (struct nfs3_state *nfs3, fd_t *fd)
{
        if ((!nfs3) || (!fd))
                return -1;

        LOCK (&nfs3->fdlrulock);
        {
                __nfs3_fdcache_update_entry (nfs3, fd);
        }
        UNLOCK (&nfs3->fdlrulock);

        return 0;
}


int
__nfs3_fdcache_remove_entry (struct nfs3_state *nfs3, struct nfs3_fd_entry *fde)
{
        if ((!fde) || (!nfs3))
                return 0;

        gf_log (GF_NFS3, GF_LOG_TRACE, "Removing fd: 0x%lx: %d",
                (long int)fde->cachedfd, fde->cachedfd->refcount);
        list_del (&fde->list);
        fd_ctx_del (fde->cachedfd, nfs3->nfsx, NULL);
        fd_unref (fde->cachedfd);
        GF_FREE (fde);
        --nfs3->fdcount;

        return 0;
}


int
nfs3_fdcache_remove (struct nfs3_state *nfs3, fd_t *fd)
{
        struct nfs3_fd_entry    *fde = NULL;
        uint64_t                ctxaddr = 0;

        if ((!nfs3) || (!fd))
                return -1;

        LOCK (&nfs3->fdlrulock);
        {
                fd_ctx_get (fd, nfs3->nfsx, &ctxaddr);
                fde = (struct nfs3_fd_entry *)(long)ctxaddr;
                __nfs3_fdcache_remove_entry (nfs3, fde);
        }
        UNLOCK (&nfs3->fdlrulock);

        return 0;
}


int
__nfs3_fdcache_replace (struct nfs3_state *nfs3)
{
        struct nfs3_fd_entry    *fde = NULL;
        struct nfs3_fd_entry    *tmp = NULL;

        if (!nfs3)
                return -1;

        if (nfs3->fdcount <= GF_NFS3_FDCACHE_SIZE)
                return 0;

        list_for_each_entry_safe (fde, tmp, &nfs3->fdlru, list)
                break;

        __nfs3_fdcache_remove_entry (nfs3, fde);

        return 0;
}



int
nfs3_fdcache_add (struct nfs3_state *nfs3, fd_t *fd)
{
        struct nfs3_fd_entry    *fde = NULL;
        int                     ret = -1;

        if ((!nfs3) || (!fd))
                return -1;

        fde = GF_CALLOC (1, sizeof (*fd), gf_nfs_mt_nfs3_fd_entry);
        if (!fde) {
                gf_log (GF_NFS3, GF_LOG_ERROR, "fd entry allocation failed");
                goto out;
        }

        /* Already refd by caller. */
        fde->cachedfd = fd;
        INIT_LIST_HEAD (&fde->list);

        LOCK (&nfs3->fdlrulock);
        {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Adding fd: 0x%lx",
                        (long int) fd);
                fd_ctx_set (fd, nfs3->nfsx, (uintptr_t)fde);
                fd_bind (fd);
                list_add_tail (&fde->list, &nfs3->fdlru);
                ++nfs3->fdcount;
                __nfs3_fdcache_replace (nfs3);
        }
        UNLOCK (&nfs3->fdlrulock);

out:
        return ret;
}


int32_t
nfs3_file_open_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                    int32_t op_ret, int32_t op_errno, fd_t *fd)
{
        nfs3_call_state_t       *cs = NULL;
        struct nfs3_state       *nfs3 = NULL;

        cs = frame->local;
        if (op_ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Opening uncached fd failed");
                cs->resolve_ret = -1;
                cs->resolve_errno = op_errno;
                fd = NULL;
        } else {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Opening uncached fd done: %d",
                        fd->refcount);
        }

        nfs3 = nfs_rpcsvc_request_program_private (cs->req);
        nfs3_flush_open_wait_call_states (cs, fd);
        nfs3_fdcache_add (nfs3, fd);
        return 0;
}


struct inode_op_queue *
__nfs3_get_inode_queue (nfs3_call_state_t *cs)
{
        struct inode_op_queue   *inode_q = NULL;
        int                     ret = -1;
        uint64_t                ctxaddr = 0;

        ret = __inode_ctx_get (cs->resolvedloc.inode, cs->nfsx, &ctxaddr);
        if (ret == 0) {
                inode_q = (struct inode_op_queue *)(long)ctxaddr;
                gf_log (GF_NFS3, GF_LOG_TRACE, "Inode queue already inited");
                goto err;
        }

        inode_q = GF_CALLOC (1, sizeof (*inode_q), gf_nfs_mt_inode_q);
        if (!inode_q) {
                gf_log (GF_NFS3, GF_LOG_ERROR, "Memory allocation failed");
                goto err;
        }

        gf_log (GF_NFS3, GF_LOG_TRACE, "Initing inode queue");
        INIT_LIST_HEAD (&inode_q->opq);
        pthread_mutex_init (&inode_q->qlock, NULL);
        __inode_ctx_put (cs->resolvedloc.inode, cs->nfsx, (uintptr_t)inode_q);

err:
        return inode_q;
}


struct inode_op_queue *
nfs3_get_inode_queue (nfs3_call_state_t *cs)
{
        struct inode_op_queue   *inode_q = NULL;

        LOCK (&cs->resolvedloc.inode->lock);
        {
                inode_q = __nfs3_get_inode_queue (cs);
        }
        UNLOCK (&cs->resolvedloc.inode->lock);

        return inode_q;
}


#define GF_NFS3_FD_OPEN_INPROGRESS      1
#define GF_NFS3_FD_NEEDS_OPEN           0


int
__nfs3_queue_call_state (struct inode_op_queue *inode_q, nfs3_call_state_t *cs)
{
        int     ret = -1;

        if (!inode_q)
                goto err;

        pthread_mutex_lock (&inode_q->qlock);
        {
                if (list_empty (&inode_q->opq)) {
                        gf_log (GF_NFS3, GF_LOG_TRACE, "First call in queue");
                        ret = GF_NFS3_FD_NEEDS_OPEN;
                } else
                        ret = GF_NFS3_FD_OPEN_INPROGRESS;

                gf_log (GF_NFS3, GF_LOG_TRACE, "Queueing call state");
                list_add_tail (&cs->openwait_q, &inode_q->opq);
        }
        pthread_mutex_unlock (&inode_q->qlock);

err:
        return ret;
}


/* Returns GF_NFS3_FD_NEEDS_OPEN if the current call is the first one to be
 * queued. If so, the caller will need to send the open fop. If this is a
 * non-first call to be queued, it means the fd opening is in progress and
 * GF_NFS3_FD_OPEN_INPROGRESS is returned.
 *
 * Returns -1 on error.
 */
int
nfs3_queue_call_state (nfs3_call_state_t *cs)
{
        struct inode_op_queue   *inode_q = NULL;
        int                     ret = -1;

        inode_q = nfs3_get_inode_queue (cs);
        if (!inode_q) {
                gf_log (GF_NFS3, GF_LOG_ERROR, "Failed to get inode op queue");
                goto err;
        }

        ret = __nfs3_queue_call_state (inode_q, cs);

err:
        return ret;
}


int
__nfs3_file_open_and_resume (nfs3_call_state_t *cs)
{
        nfs_user_t      nfu = {0, };
        int             ret = -EFAULT;

        if (!cs)
                return ret;

        ret = nfs3_queue_call_state (cs);
        if (ret == -1) {
                gf_log (GF_NFS3, GF_LOG_ERROR, "Error queueing call state");
                ret = -EFAULT;
                goto out;
        } else if (ret == GF_NFS3_FD_OPEN_INPROGRESS) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Open in progress. Will wait.");
                ret = 0;
                goto out;
        }

        nfs_user_root_create (&nfu);
        gf_log (GF_NFS3, GF_LOG_TRACE, "Opening uncached fd");
        ret = nfs_open (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc, O_RDWR,
                        nfs3_file_open_cbk, cs);
out:
        return ret;
}


fd_t *
nfs3_fdcache_getfd (struct nfs3_state *nfs3, inode_t *inode)
{
        fd_t    *fd = NULL;

        if ((!nfs3) || (!inode))
                return NULL;

        fd = fd_lookup (inode, 0);
        if (fd) {
                /* Already refd by fd_lookup, so no need to ref again. */
                gf_log (GF_NFS3, GF_LOG_TRACE, "fd found in state: %d",
                        fd->refcount);
                nfs3_fdcache_update (nfs3, fd);
        } else
                gf_log (GF_NFS3, GF_LOG_TRACE, "fd not found in state");

        return fd;
}



int
nfs3_file_open_and_resume (nfs3_call_state_t *cs, nfs3_resume_fn_t resume)
{
        fd_t    *fd = NULL;
        int     ret = -EFAULT;

        if (!cs)
                return ret;

        cs->resume_fn = resume;
        gf_log (GF_NFS3, GF_LOG_TRACE, "Opening: %s", cs->resolvedloc.path);
        fd = nfs3_fdcache_getfd (cs->nfs3state, cs->resolvedloc.inode);
        if (fd) {
                cs->fd = fd;    /* Gets unrefd when the call state is wiped. */
                cs->resolve_ret = 0;
                nfs3_call_resume (cs);
                ret = 0;
                goto err;
        }

        ret = __nfs3_file_open_and_resume (cs);

err:
        return ret;
}


void
nfs3_stat_to_errstr (uint32_t xid, char *op, nfsstat3 stat, int pstat,
                     char *errstr)
{
        if ((!op) || (!errstr))
                return;

        sprintf (errstr, "XID: %x, %s: NFS: %d(%s), POSIX: %d(%s)", xid, op,
                 stat, nfsstat3_strerror (stat), pstat, strerror (pstat));
}

void
nfs3_log_common_call (uint32_t xid, char *op, struct nfs3_fh *fh)
{
        char    fhstr[1024];

        nfs3_fh_to_str (fh, fhstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, %s: args: %s", xid, op,
                fhstr);
}


void
nfs3_log_fh_entry_call (uint32_t xid, char *op, struct nfs3_fh *fh,
                        char *name)
{
        char    fhstr[1024];

        nfs3_fh_to_str (fh, fhstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, %s: args: %s, name: %s", xid,
                op, fhstr, name);
}


void
nfs3_log_rename_call (uint32_t xid, struct nfs3_fh *src, char *sname,
                      struct nfs3_fh *dst, char *dname)
{
        char    sfhstr[1024];
        char    dfhstr[1024];

        nfs3_fh_to_str (src, sfhstr);
        nfs3_fh_to_str (dst, dfhstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, RENAME: args: Src: %s, "
                "name: %s, Dst: %s, name: %s", xid, sfhstr, sname, dfhstr,
                dname);
}



void
nfs3_log_create_call (uint32_t xid, struct nfs3_fh *fh, char *name,
                      createmode3 mode)
{
        char    fhstr[1024];
        char    *modestr = NULL;
        char    exclmode[] = "EXCLUSIVE";
        char    unchkd[] = "UNCHECKED";
        char    guarded[] = "GUARDED";

        nfs3_fh_to_str (fh, fhstr);
        if (mode == EXCLUSIVE)
                modestr = exclmode;
        else if (mode == GUARDED)
                modestr = guarded;
        else
                modestr = unchkd;

        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, CREATE: args: %s, name: %s,"
                " mode: %s", xid, fhstr, name, modestr);
}


void
nfs3_log_mknod_call (uint32_t xid, struct nfs3_fh *fh, char *name, int type)
{
        char    fhstr[1024];
        char    *modestr = NULL;
        char    chr[] = "CHAR";
        char    blk[] = "BLK";
        char    sock[] = "SOCK";
        char    fifo[] = "FIFO";

        nfs3_fh_to_str (fh, fhstr);
        if (type == NF3CHR)
                modestr = chr;
        else if (type == NF3BLK)
                modestr = blk;
        else if (type == NF3SOCK)
                modestr = sock;
        else
                modestr = fifo;

        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, MKNOD: args: %s, name: %s,"
                " type: %s", xid, fhstr, name, modestr);
}



void
nfs3_log_symlink_call (uint32_t xid, struct nfs3_fh *fh, char *name, char *tgt)
{
        char    fhstr[1024];

        nfs3_fh_to_str (fh, fhstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, SYMLINK: args: %s, name: %s,"
                " target: %s", xid, fhstr, name, tgt);
}


void
nfs3_log_link_call (uint32_t xid, struct nfs3_fh *fh, char *name,
                    struct nfs3_fh *tgt)
{
        char    dfhstr[1024];
        char    tfhstr[1024];

        nfs3_fh_to_str (fh, dfhstr);
        nfs3_fh_to_str (tgt, tfhstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, LINK: args: %s, name: %s,"
                " target: %s", xid, dfhstr, name, tfhstr);
}


void
nfs3_log_rw_call (uint32_t xid, char *op, struct nfs3_fh *fh, offset3 offt,
                  count3 count, int stablewrite)
{
        char    fhstr[1024];

        nfs3_fh_to_str (fh, fhstr);
        if (stablewrite == -1)
                gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, %s: args: %s, offset:"
                        " %"PRIu64",  count: %"PRIu32, xid, op, fhstr, offt,
                        count);
        else
                gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, %s: args: %s, offset:"
                        " %"PRIu64",  count: %"PRIu32", %s", xid, op, fhstr,
                        offt, count,
                        (stablewrite == UNSTABLE)?"UNSTABLE":"STABLE");

}


void
nfs3_log_common_res (uint32_t xid, char *op, nfsstat3 stat, int pstat)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, op, stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s", errstr);
}

void
nfs3_log_readlink_res (uint32_t xid, nfsstat3 stat, int pstat, char *linkpath)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "READLINK", stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, target: %s", errstr, linkpath);

}

void
nfs3_log_read_res (uint32_t xid, nfsstat3 stat, int pstat, count3 count,
                   int is_eof, struct iovec *vec, int32_t veccount)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "READ", stat, pstat, errstr);
        if (vec)
                gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, count: %"PRIu32", is_eof:"
                        " %d, vector: count: %d, len: %zd", errstr, count,
                        is_eof, veccount, vec->iov_len);
        else
                gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, count: %"PRIu32", is_eof:"
                        " %d", errstr, count, is_eof);
}


void
nfs3_log_write_res (uint32_t xid, nfsstat3 stat, int pstat, count3 count,
                    int stable, uint64_t wverf)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "WRITE", stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, count: %"PRIu32", %s,wverf: %"PRIu64
                , errstr, count, (stable == UNSTABLE)?"UNSTABLE":"STABLE",
                wverf);
}


void
nfs3_log_newfh_res (uint32_t xid, char *op, nfsstat3 stat, int pstat,
                    struct nfs3_fh *newfh)
{
        char    errstr[1024];
        char    fhstr[1024];

        nfs3_stat_to_errstr (xid, op, stat, pstat, errstr);
        nfs3_fh_to_str (newfh, fhstr);

        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, %s", errstr, fhstr);
}


void
nfs3_log_readdir_res (uint32_t xid, nfsstat3 stat, int pstat, uint64_t cverf,
                      count3 count, int is_eof)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "READDIR", stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, count: %"PRIu32", cverf: %"PRIu64
                ", is_eof: %d", errstr, count, cverf, is_eof);
}


void
nfs3_log_readdirp_res (uint32_t xid, nfsstat3 stat, int pstat, uint64_t cverf,
                       count3 dircount, count3 maxcount, int is_eof)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "READDIRPLUS", stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, dircount: %"PRIu32", maxcount: %"
                PRIu32", cverf: %"PRIu64", is_eof: %d", errstr, dircount,
                maxcount, cverf, is_eof);
}


void
nfs3_log_commit_res (uint32_t xid, nfsstat3 stat, int pstat, uint64_t wverf)
{
        char    errstr[1024];

        nfs3_stat_to_errstr (xid, "COMMIT", stat, pstat, errstr);
        gf_log (GF_NFS3, GF_LOG_DEBUG, "%s, wverf: %"PRIu64, errstr, wverf);
}


void
nfs3_log_readdir_call (uint32_t xid, struct nfs3_fh *fh, count3 dircount,
                       count3 maxcount)
{
        char    fhstr[1024];

        nfs3_fh_to_str (fh, fhstr);

        if (maxcount == 0)
                gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, READDIR: args: %s,"
                        " count: %d", xid, fhstr, (uint32_t)dircount);
        else
                gf_log (GF_NFS3, GF_LOG_DEBUG, "XID: %x, READDIRPLUS: args: %s,"
                        " dircount: %d, maxcount: %d", xid, fhstr,
                        (uint32_t)dircount, (uint32_t)maxcount);
}


int
nfs3_fh_resolve_inode_done (nfs3_call_state_t *cs, inode_t *inode)
{
        int             ret = -EFAULT;

        if ((!cs) || (!inode))
                return ret;

        gf_log (GF_NFS3, GF_LOG_TRACE, "FH inode resolved");
        ret = nfs_inode_loc_fill (inode, &cs->resolvedloc);
        if (ret < 0)
                goto err;

        nfs3_call_resume (cs);

err:
        return ret;
}

#define GF_NFS3_FHRESOLVE_FOUND         1
#define GF_NFS3_FHRESOLVE_NOTFOUND      2
#define GF_NFS3_FHRESOLVE_DIRFOUND      3

int
nfs3_fh_resolve_check_entry (struct nfs3_fh *fh, gf_dirent_t *candidate,
                             int hashidx)
{
        struct iatt             *ia = NULL;
        int                     ret = GF_NFS3_FHRESOLVE_NOTFOUND;
        nfs3_hash_entry_t       entryhash = 0;

        if ((!fh) || (!candidate))
                return ret;

        if ((strcmp (candidate->d_name, ".") == 0) ||
            (strcmp (candidate->d_name, "..") == 0))
                goto found_entry;

        ia = &candidate->d_stat;
        if ((ia->ia_gen == fh->gen) && (ia->ia_ino == fh->ino)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Found entry: gen: %"PRId64
                        " ino: %"PRId64", name: %s", ia->ia_gen, ia->ia_ino,
                        candidate->d_name);
                ret = GF_NFS3_FHRESOLVE_FOUND;
                goto found_entry;
        }

        /* This condition ensures that we never have to be afraid of having
         * a directory hash conflict with a file hash. The consequence of
         * this condition is that we can now have unlimited files in a directory
         * and upto 65536 sub-directories in a directory.
         */
        if (!IA_ISDIR (candidate->d_stat.ia_type))
                goto found_entry;
        entryhash = fh->entryhash[hashidx];
        if (entryhash == nfs3_fh_hash_entry (ia->ia_ino, ia->ia_gen)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Found hash match: %s: %d",
                        candidate->d_name, entryhash);
                ret = GF_NFS3_FHRESOLVE_DIRFOUND;
                goto found_entry;
        }

found_entry:

        return ret;
}


int32_t
nfs3_fh_resolve_entry_lookup_cbk (call_frame_t *frame, void *cookie,
                                  xlator_t *this, int32_t op_ret,
                                  int32_t op_errno, inode_t *inode,
                                  struct iatt *buf, dict_t *xattr,
                                  struct iatt *postparent)
{
        nfs3_call_state_t       *cs = NULL;

        cs = frame->local;
        cs->resolve_ret = op_ret;
        cs->resolve_errno = op_errno;

        if (op_ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Lookup failed: %s: %s",
                        cs->resolvedloc.path, strerror (op_errno));
                goto err;
        } else
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry looked up: %s",
                        cs->resolvedloc.path);

        inode_link (inode, cs->resolvedloc.parent, cs->resolvedloc.name, buf);
err:
        nfs3_call_resume (cs);
        return 0;
}



int32_t
nfs3_fh_resolve_readdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                             int32_t op_ret, int32_t op_errno,
                             gf_dirent_t *entries);

int
nfs3_fh_resolve_found_entry (nfs3_call_state_t *cs, gf_dirent_t *candidate)
{
        uint64_t        dirino = 0;
        uint64_t        dirgen = 0;
        int             ret = 0;
        nfs_user_t      nfu = {0, };

        if ((!cs) || (!candidate))
                return -EFAULT;

        dirino = cs->resolvedloc.inode->ino;
        dirgen = cs->resolvedloc.inode->generation;

        nfs_loc_wipe (&cs->resolvedloc);
        ret = nfs_entry_loc_fill (cs->vol->itable, dirino, dirgen,
                                  candidate->d_name, &cs->resolvedloc,
                                  NFS_RESOLVE_CREATE);
        if (ret == -ENOENT) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry not in itable, needs"
                        " lookup");
                nfs_user_root_create (&nfu);
                ret = nfs_lookup (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                  nfs3_fh_resolve_entry_lookup_cbk,
                                  cs);
        } else {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry got from itable");
                nfs3_call_resume (cs);
        }

        return ret;
}


int32_t
nfs3_fh_resolve_parent_lookup_cbk (call_frame_t *frame, void *cookie,
                                  xlator_t *this, int32_t op_ret,
                                  int32_t op_errno, inode_t *inode,
                                  struct iatt *buf, dict_t *xattr,
                                  struct iatt *postparent)
{
        nfs3_call_state_t       *cs = NULL;

        cs = frame->local;
        cs->resolve_ret = op_ret;
        cs->resolve_errno = op_errno;

        if (op_ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Lookup failed: %s: %s",
                        cs->resolvedloc.path, strerror (op_errno));
                nfs3_call_resume (cs);
                goto err;
        } else
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry looked up: %s",
                        cs->resolvedloc.path);

        inode_link (inode, cs->resolvedloc.parent, cs->resolvedloc.name, buf);
        nfs3_fh_resolve_entry_hard (cs);

err:
        return 0;
}


int
nfs3_fh_resolve_found_parent (nfs3_call_state_t *cs, gf_dirent_t *candidate)
{
        uint64_t        dirino = 0;
        uint64_t        dirgen = 0;
        int             ret = 0;
        nfs_user_t      nfu = {0, };

        if ((!cs) || (!candidate))
                return -EFAULT;

        dirino = cs->resolvedloc.inode->ino;
        dirgen = cs->resolvedloc.inode->generation;

        nfs_loc_wipe (&cs->resolvedloc);
        ret = nfs_entry_loc_fill (cs->vol->itable, dirino, dirgen,
                                  candidate->d_name, &cs->resolvedloc,
                                  NFS_RESOLVE_CREATE);
        if (ret == -ENOENT) {
                nfs_user_root_create (&nfu);
                ret = nfs_lookup (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                  nfs3_fh_resolve_parent_lookup_cbk,
                                  cs);
        } else
                nfs3_fh_resolve_entry_hard (cs);

        return ret;
}


int
nfs3_fh_resolve_found (nfs3_call_state_t *cs, gf_dirent_t *candidate)
{
        int             ret = 0;

        if ((!cs) || (!candidate))
                return -EFAULT;

        if (!cs->resolventry) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Candidate entry was found");
                ret = nfs3_fh_resolve_found_entry (cs, candidate);
        } else {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry's parent was found");
                ret = nfs3_fh_resolve_found_parent (cs, candidate);
        }

        return ret;
}


int32_t
nfs3_fh_resolve_opendir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                             int32_t op_ret, int32_t op_errno, fd_t *fd)
{
        nfs3_call_state_t       *cs = NULL;
        int                     ret = -EFAULT;
        nfs_user_t              nfu = {0, };

        cs = frame->local;
        cs->resolve_ret = op_ret;
        cs->resolve_errno = op_errno;

        if (op_ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir open failed: %s: %s",
                        cs->resolvedloc.path, strerror (op_errno));
                nfs3_call_resume (cs);
                goto err;
        } else
                gf_log (GF_NFS3, GF_LOG_TRACE, "Reading directory: %s",
                        cs->resolvedloc.path);

        nfs_user_root_create (&nfu);
        /* Keep this directory fd_t around till we have either:
         * a. found the entry,
         * b. exhausted all the entries,
         * c. decide to step into a child directory.
         *
         * This decision is made in nfs3_fh_resolve_check_response.
         */
        cs->resolve_dir_fd = fd;
        gf_log (GF_NFS3, GF_LOG_TRACE, "resolve new fd refed: 0x%lx, ref: %d",
                (long)cs->resolve_dir_fd, cs->resolve_dir_fd->refcount);
        ret = nfs_readdirp (cs->nfsx, cs->vol, &nfu, fd, GF_NFS3_DTPREF, 0,
                            nfs3_fh_resolve_readdir_cbk, cs);

err:
        return ret;
}

int32_t
nfs3_fh_resolve_dir_lookup_cbk (call_frame_t *frame, void *cookie,
                                xlator_t *this, int32_t op_ret,int32_t op_errno,
                                inode_t *inode, struct iatt *buf, dict_t *xattr,
                                struct iatt *postparent)
{
        nfs3_call_state_t       *cs = NULL;
        nfs_user_t              nfu = {0, };

        cs = frame->local;
        cs->resolve_ret = op_ret;
        cs->resolve_errno = op_errno;

        if (op_ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Lookup failed: %s: %s",
                        cs->resolvedloc.path, strerror (op_errno));
                nfs3_call_resume (cs);
                goto err;
        } else
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir will be opened: %s",
                        cs->resolvedloc.path);

        nfs_user_root_create (&nfu);
        inode_link (inode, cs->resolvedloc.parent, cs->resolvedloc.name, buf);
        nfs_opendir (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                     nfs3_fh_resolve_opendir_cbk, cs);

err:
        return 0;
}



int
nfs3_fh_resolve_dir_hard (nfs3_call_state_t *cs, uint64_t ino, uint64_t gen,
                          char *entry)
{
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };

        if (!cs)
                return ret;

        cs->hashidx++;
        nfs_loc_wipe (&cs->resolvedloc);
        if (nfs3_fh_hash_index_is_beyond (&cs->resolvefh, cs->hashidx)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Hash index is beyond: idx %d, "
                        " fh idx: %d", cs->hashidx, cs->resolvefh.hashcount);
                nfs3_call_resume_estale (cs);
                ret = 0;
                goto out;
        }

        nfs_user_root_create (&nfu);
        gf_log (GF_NFS3, GF_LOG_TRACE, "FH hard dir resolution: ino:"
                " %"PRIu64", gen: %"PRIu64", entry: %s, hashidx: %d",
                ino, gen, entry, cs->hashidx);
        ret = nfs_entry_loc_fill (cs->vol->itable, ino, gen, entry,
                                  &cs->resolvedloc, NFS_RESOLVE_CREATE);

        if (ret == 0) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir will be opened: %s",
                        cs->resolvedloc.path);
                ret = nfs_opendir (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                   nfs3_fh_resolve_opendir_cbk, cs);
        } else if (ret == -ENOENT) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir needs lookup: %s",
                        cs->resolvedloc.path);
                ret = nfs_lookup (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                  nfs3_fh_resolve_dir_lookup_cbk, cs);
        }
out:
        return ret;
}


int
nfs3_fh_resolve_check_response (nfs3_call_state_t *cs, gf_dirent_t *candidate,
                                int response, off_t last_offt)
{
        uint64_t        dirino = 0;
        uint64_t        dirgen = 0;
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };

        if (!cs)
                return ret;

        dirino = cs->resolvedloc.inode->ino;
        dirgen = cs->resolvedloc.inode->generation;

        if (response == GF_NFS3_FHRESOLVE_DIRFOUND)
                nfs3_fh_resolve_dir_hard (cs, dirino, dirgen,
                                          candidate->d_name);
        else if (response == GF_NFS3_FHRESOLVE_FOUND)
                nfs3_fh_resolve_found (cs, candidate);
        else if (response == GF_NFS3_FHRESOLVE_NOTFOUND) {
                nfs_user_root_create (&nfu);
                nfs_readdirp (cs->nfsx, cs->vol, &nfu, cs->resolve_dir_fd,
                              GF_NFS3_DTPREF, last_offt,
                              nfs3_fh_resolve_readdir_cbk, cs);
        }

        return 0;
}

int
nfs3_fh_resolve_search_dir (nfs3_call_state_t *cs, gf_dirent_t *entries)
{
        gf_dirent_t     *candidate = NULL;
        int             ret = GF_NFS3_FHRESOLVE_NOTFOUND;
        off_t           lastoff = 0;

        if ((!cs) || (!entries))
                return -EFAULT;

        if (list_empty (&entries->list))
                goto not_found;

        list_for_each_entry (candidate, &entries->list, list) {
                lastoff = candidate->d_off;
                gf_log (GF_NFS3, GF_LOG_TRACE, "Candidate: %s, ino: %"PRIu64
                        ", gen: %"PRIu64, candidate->d_name, candidate->d_ino,
                        candidate->d_stat.ia_gen);
                ret = nfs3_fh_resolve_check_entry (&cs->resolvefh, candidate,
                                                   cs->hashidx);
                if (ret != GF_NFS3_FHRESOLVE_NOTFOUND)
                        break;
        }

not_found:
        nfs3_fh_resolve_check_response (cs, candidate, ret, lastoff);
        return ret;
}


int32_t
nfs3_fh_resolve_readdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                             int32_t op_ret, int32_t op_errno,
                             gf_dirent_t *entries)
{
        nfs3_call_state_t       *cs = NULL;

        cs = frame->local;
        if (op_ret <= 0) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Directory read done: %s: %s",
                        cs->resolvedloc.path, strerror (op_ret));
                cs->resolve_ret = -1;
                cs->resolve_errno = ESTALE;
                nfs3_call_resume (cs);
                goto err;
        }

        nfs3_fh_resolve_search_dir (cs, entries);

err:
        return 0;
}

/* Needs no extra argument since it knows that the fh to be resolved is in
 * resolvefh and that it needs to start looking from the root.
 */
int
nfs3_fh_resolve_inode_hard (nfs3_call_state_t *cs)
{
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };

        if (!cs)
                return ret;

        cs->hashidx++;
        nfs_loc_wipe (&cs->resolvedloc);
        if (nfs3_fh_hash_index_is_beyond (&cs->resolvefh, cs->hashidx)) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Hash index is beyond: idx %d, "
                        " fh idx: %d", cs->hashidx, cs->resolvefh.hashcount);
                nfs3_call_resume_estale (cs);
                ret = 0;
                goto out;
        }

        nfs_user_root_create (&nfu);
        gf_log (GF_NFS3, GF_LOG_TRACE, "FH hard resolution: ino:"
                " %"PRIu64", gen: %"PRIu64", hashidx: %d", cs->resolvefh.ino,
                cs->resolvefh.gen, cs->hashidx);
        ret = nfs_ino_loc_fill (cs->vol->itable, 1, 0, &cs->resolvedloc);

        if (ret == 0) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir will be opened: %s",
                        cs->resolvedloc.path);
                ret = nfs_opendir (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                   nfs3_fh_resolve_opendir_cbk, cs);
        } else if (ret == -ENOENT) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Dir needs lookup: %s",
                        cs->resolvedloc.path);
                ret = nfs_lookup (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                                  nfs3_fh_resolve_dir_lookup_cbk, cs);
        }

out:
        return ret;
}


int
nfs3_fh_resolve_entry_hard (nfs3_call_state_t *cs)
{
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };

        if (!cs)
                return ret;

        nfs_loc_wipe (&cs->resolvedloc);
        nfs_user_root_create (&nfu);
        gf_log (GF_NFS3, GF_LOG_TRACE, "FH hard resolution: ino:"
                " %"PRIu64", gen: %"PRIu64", entry: %s, hashidx: %d",
                cs->resolvefh.ino, cs->resolvefh.gen, cs->resolventry,
                cs->hashidx);

        ret = nfs_entry_loc_fill (cs->vol->itable, cs->resolvefh.ino,
                                  cs->resolvefh.gen, cs->resolventry,
                                  &cs->resolvedloc, NFS_RESOLVE_CREATE);

        if (ret == -2) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry needs lookup: %s",
                        cs->resolvedloc.path);
                nfs_lookup (cs->nfsx, cs->vol, &nfu, &cs->resolvedloc,
                            nfs3_fh_resolve_entry_lookup_cbk, cs);
                ret = 0;
        } else if (ret == -1) {
                gf_log (GF_NFS3, GF_LOG_TRACE, "Entry needs parent lookup: %s",
                        cs->resolvedloc.path);
                ret = nfs3_fh_resolve_inode_hard (cs);
        } else if (ret == 0) {
                cs->resolve_ret = 0;
                nfs3_call_resume (cs);
        }

        return ret;
}

int
nfs3_fh_resolve_inode (nfs3_call_state_t *cs)
{
        inode_t         *inode = NULL;
        int             ret = -EFAULT;

        if (!cs)
                return ret;

        gf_log (GF_NFS3, GF_LOG_TRACE, "FH needs inode resolution");
        inode = inode_get (cs->vol->itable, cs->resolvefh.ino,
                           cs->resolvefh.gen);
        if (!inode)
                ret = nfs3_fh_resolve_inode_hard (cs);
        else
                ret = nfs3_fh_resolve_inode_done (cs, inode);

        if (inode)
                inode_unref (inode);

        return ret;
}

int
nfs3_fh_resolve_entry (nfs3_call_state_t *cs)
{
        int     ret = -EFAULT;

        if (!cs)
                return ret;

        ret = nfs3_fh_resolve_entry_hard (cs);
        if (ret < 0)
		nfs3_call_resume_estale (cs);

        return 0;
}

int
nfs3_fh_resolve_and_resume (nfs3_call_state_t *cs, struct nfs3_fh *fh,
                            char *entry, nfs3_resume_fn_t resum_fn)
{
        int     ret = -EFAULT;

        if ((!cs) || (!fh))
                return ret;

        cs->resume_fn = resum_fn;
        cs->resolvefh = *fh;
        cs->hashidx = 0;

        if (!entry)
                ret = nfs3_fh_resolve_inode (cs);
        else {
                cs->resolventry = gf_strdup (entry);
                if (!cs->resolventry)
                        goto err;

                ret = nfs3_fh_resolve_entry (cs);
        }

err:
        return ret;
}