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

  This file is licensed to you under your choice of the GNU Lesser
  General Public License, version 3 or any later version (LGPLv3 or
  later), or the GNU General Public License, version 2 (GPLv2), in all
  cases as published by the Free Software Foundation.
*/

#include "rpcsvc.h"
#include "dict.h"
#include "xlator.h"
#include "mount3.h"
#include "xdr-nfs3.h"
#include "msg-nfs3.h"
#include "iobuf.h"
#include "nfs-common.h"
#include "nfs3-fh.h"
#include "nfs-fops.h"
#include "nfs-inodes.h"
#include "nfs-generics.h"
#include "locking.h"
#include "iatt.h"
#include "nfs-mem-types.h"
#include "nfs.h"
#include "common-utils.h"
#include "store.h"
#include "glfs-internal.h"
#include "glfs.h"
#include "mount3-auth.h"
#include "hashfn.h"
#include "nfs-messages.h"

#include <errno.h>
#include <sys/socket.h>
#include <sys/uio.h>


/* This macro will assist in freeing up entire link list
 * of host_auth_spec structure.
 */
#define FREE_HOSTSPEC(exp) do {                                 \
                struct host_auth_spec *host= exp->hostspec;     \
                while (NULL != host){                           \
                        struct host_auth_spec* temp = host;     \
                        host = host->next;                      \
                        if (NULL != temp->host_addr) {          \
                                GF_FREE (temp->host_addr);      \
                        }                                       \
                        GF_FREE (temp);                         \
                }                                               \
                exp->hostspec = NULL;                           \
        } while (0)

/* Paths for export and netgroup files */
const char *exports_file_path   = GLUSTERD_DEFAULT_WORKDIR "/nfs/exports";
const char *netgroups_file_path = GLUSTERD_DEFAULT_WORKDIR "/nfs/netgroups";

typedef ssize_t (*mnt3_serializer) (struct iovec outmsg, void *args);

extern void *
mount3udp_thread (void *argv);

static void
mnt3_export_free (struct mnt3_export *exp)
{
        if (!exp)
                return;

        if (exp->exptype == MNT3_EXPTYPE_DIR)
                FREE_HOSTSPEC (exp);
        GF_FREE (exp->expname);
        GF_FREE (exp->fullpath);
        GF_FREE (exp);
}

/* Generic reply function for MOUNTv3 specific replies. */
int
mnt3svc_submit_reply (rpcsvc_request_t *req, void *arg, mnt3_serializer sfunc)
{
        struct iovec            outmsg  = {0, };
        struct iobuf            *iob    = NULL;
        struct mount3_state     *ms     = NULL;
        int                     ret     = -1;
        ssize_t                 msglen  = 0;
        struct iobref           *iobref = NULL;

        if (!req)
                return -1;

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND, "mount state not found");
                goto ret;
        }

        /* First, get the io buffer into which the reply in arg will
         * be serialized.
         */
        /* TODO: use 'xdrproc_t' instead of 'sfunc' to get the xdr-size */
        iob = iobuf_get (ms->iobpool);
        if (!iob) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to get iobuf");
                goto ret;
        }

        iobuf_to_iovec (iob, &outmsg);
        /* Use the given serializer to translate the give C structure in arg
         * to XDR format which will be written into the buffer in outmsg.
         */
        msglen = sfunc (outmsg, arg);
        if (msglen < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_ENCODE_MSG_FAIL,
                        "Failed to encode message");
                goto ret;
        }
        outmsg.iov_len = msglen;

        iobref = iobref_new ();
        if (iobref == NULL) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to get iobref");
                goto ret;
        }

        ret = iobref_add (iobref, iob);
        if (ret) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to add iob to iobref");
                goto ret;
        }

        /* Then, submit the message for transmission. */
        ret = rpcsvc_submit_message (req, &outmsg, 1, NULL, 0, iobref);
        if (ret == -1) {
                gf_msg (GF_MNT, GF_LOG_ERROR, errno, NFS_MSG_REP_SUBMIT_FAIL,
                        "Reply submission failed");
                goto ret;
        }

        ret = 0;
ret:
        if (NULL != iob)
                iobuf_unref (iob);
        if (NULL != iobref)
                iobref_unref (iobref);

        return ret;
}
/**
 * __mountdict_insert -- Insert a mount entry into the mount state
 *
 * @ms: The mount state holding the entries
 * @me: The mount entry to insert
 *
 * Not for external use.
 */
void
__mountdict_insert (struct mount3_state *ms, struct mountentry *me)
{
        char   *exname = NULL;
        char   *fpath  = NULL;
        data_t *medata = NULL;

        GF_VALIDATE_OR_GOTO (GF_MNT, ms, out);
        GF_VALIDATE_OR_GOTO (GF_MNT, me, out);

        /* We don't want export names with leading slashes */
        exname = me->exname;
        while (exname[0] == '/')
                exname++;

        /* Get the fullpath for the export */
        fpath = me->fullpath;
        if (me->has_full_path) {
                while (fpath[0] == '/')
                        fpath++;

                /* Export names can either be just volumes or paths inside that
                 * volume. */
                exname = fpath;
        }

        snprintf (me->hashkey, sizeof (me->hashkey), "%s:%s", exname,
                  me->hostname);

        medata = bin_to_data (me, sizeof (*me));
        dict_set (ms->mountdict, me->hashkey, medata);
        gf_msg_trace (GF_MNT, 0, "Inserted into mountdict: %s", me->hashkey);
out:
        return;
}

/**
 * __mountdict_remove -- Remove a mount entry from the mountstate.
 *
 * @ms: The mount state holding the entries
 * @me: The mount entry to remove
 *
 * Not for external use.
 */
void
__mountdict_remove (struct mount3_state *ms, struct mountentry *me)
{
        dict_del (ms->mountdict, me->hashkey);
}

/* Generic error reply function, just pass the err status
 * and it will do the rest, including transmission.
 */
int
mnt3svc_mnt_error_reply (rpcsvc_request_t *req, int mntstat)
{
        mountres3       res;

        if (!req)
                return -1;

        res.fhs_status = mntstat;
        mnt3svc_submit_reply (req, (void *)&res,
                              (mnt3_serializer)xdr_serialize_mountres3);

        return 0;
}


mountstat3
mnt3svc_errno_to_mnterr (int32_t errnum)
{
        mountstat3      stat;

        switch (errnum) {

                case 0:
                        stat = MNT3_OK;
                        break;
                case ENOENT:
                        stat = MNT3ERR_NOENT;
                        break;
                case EPERM:
                        stat = MNT3ERR_PERM;
                        break;
                case EIO:
                        stat = MNT3ERR_IO;
                        break;
                case EACCES:
                        stat = MNT3ERR_ACCES;
                        break;
                case ENOTDIR:
                        stat = MNT3ERR_NOTDIR;
                        break;
                case EINVAL:
                        stat = MNT3ERR_INVAL;
                        break;
                case ENOSYS:
                        stat = MNT3ERR_NOTSUPP;
                        break;
                case ENOMEM:
                        stat = MNT3ERR_SERVERFAULT;
                        break;
                default:
                        stat = MNT3ERR_SERVERFAULT;
                        break;
        }

        return stat;
}


mountres3
mnt3svc_set_mountres3 (mountstat3 stat, struct nfs3_fh *fh, int *authflavor,
                       u_int aflen)
{
        mountres3       res = {0, };
        uint32_t        fhlen = 0;

        res.fhs_status = stat;

        if (fh)
                fhlen = nfs3_fh_compute_size ();

        res.mountres3_u.mountinfo.fhandle.fhandle3_len = fhlen;
        res.mountres3_u.mountinfo.fhandle.fhandle3_val = (char *)fh;
        res.mountres3_u.mountinfo.auth_flavors.auth_flavors_val = authflavor;
        res.mountres3_u.mountinfo.auth_flavors.auth_flavors_len = aflen;

        return res;
}

/* Read the rmtab from the store_handle and append (or not) the entries to the
 * mountlist.
 *
 * Requires the store_handle to be locked.
 */
static int
__mount_read_rmtab (gf_store_handle_t *sh, struct list_head *mountlist,
                    gf_boolean_t append)
{
        int                     ret = 0;
        unsigned int            idx = 0;
        struct mountentry       *me = NULL, *tmp = NULL;
                                /* me->hostname is a char[MNTPATHLEN] */
        char                    key[MNTPATHLEN + 11];

        GF_ASSERT (sh && mountlist);

        if (!gf_store_locked_local (sh)) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_READ_LOCKED,
                        "Not reading unlocked %s", sh->path);
                return -1;
        }

        if (!append) {
                list_for_each_entry_safe (me, tmp, mountlist, mlist) {
                        list_del (&me->mlist);
                        GF_FREE (me);
                }
                me = NULL;
        }

        for (;;) {
                char *value = NULL;

                if (me && append) {
                        /* do not add duplicates */
                        list_for_each_entry (tmp, mountlist, mlist) {
                                if (!strcmp(tmp->hostname, me->hostname) &&
                                    !strcmp(tmp->exname, me->exname)) {
                                        GF_FREE (me);
                                        goto dont_add;
                                }
                        }
                        list_add_tail (&me->mlist, mountlist);
                } else if (me) {
                        list_add_tail (&me->mlist, mountlist);
                }

dont_add:
                me = GF_CALLOC (1, sizeof (*me), gf_nfs_mt_mountentry);
                if (!me) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Out of memory");
                        ret = -1;
                        goto out;
                }

                INIT_LIST_HEAD (&me->mlist);

                snprintf (key, 9 + MNTPATHLEN, "hostname-%d", idx);
                ret = gf_store_retrieve_value (sh, key, &value);
                if (ret)
                        break;
                strncpy (me->hostname, value, MNTPATHLEN);
                GF_FREE (value);

                snprintf (key, 11 + MNTPATHLEN, "mountpoint-%d", idx);
                ret = gf_store_retrieve_value (sh, key, &value);
                if (ret)
                        break;
                strncpy (me->exname, value, MNTPATHLEN);
                GF_FREE (value);

                idx++;
                gf_msg_trace (GF_MNT, 0, "Read entries %s:%s",
                              me->hostname, me->exname);
        }
        gf_msg_debug (GF_MNT, 0, "Read %d entries from '%s'", idx, sh->path);
        GF_FREE (me);
out:
        return ret;
}

/* Overwrite the contents of the rwtab with te in-memory client list.
 * Fail gracefully if the stora_handle is not locked.
 */
static void
__mount_rewrite_rmtab(struct mount3_state *ms, gf_store_handle_t *sh)
{
        struct mountentry       *me = NULL;
        char                    key[16];
        int                     fd, ret;
        unsigned int            idx = 0;

        if (!gf_store_locked_local (sh)) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_MODIFY_LOCKED,
                        "Not modifying unlocked %s", sh->path);
                return;
        }

        fd = gf_store_mkstemp (sh);
        if (fd == -1) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
                        "Failed to open %s", sh->path);
                return;
        }

        list_for_each_entry (me, &ms->mountlist, mlist) {
                snprintf (key, 16, "hostname-%d", idx);
                ret = gf_store_save_value (fd, key, me->hostname);
                if (ret)
                        goto fail;

                snprintf (key, 16, "mountpoint-%d", idx);
                ret = gf_store_save_value (fd, key, me->exname);
                if (ret)
                        goto fail;

                idx++;
        }

        gf_msg_debug (GF_MNT, 0, "Updated rmtab with %d entries", idx);

        if (gf_store_rename_tmppath (sh))
                gf_msg (GF_MNT, GF_LOG_ERROR, errno,
                        NFS_MSG_RWTAB_OVERWRITE_FAIL,
                        "Failed to overwrite rwtab %s", sh->path);

        return;

fail:
        gf_msg (GF_MNT, GF_LOG_ERROR, errno, NFS_MSG_UPDATE_FAIL,
                "Failed to update %s", sh->path);
        gf_store_unlink_tmppath (sh);
}

static gf_boolean_t
mount_open_rmtab (const char *rmtab, gf_store_handle_t **sh)
{
        int ret = -1;

        /* updating the rmtab is disabled, use in-memory only */
        if (!rmtab || rmtab[0] == '\0')
                return _gf_false;

        ret = gf_store_handle_new (rmtab, sh);
        if (ret) {
                gf_log (GF_MNT, GF_LOG_WARNING, "Failed to open '%s'", rmtab);
                return _gf_false;
        }

        return _gf_true;
}


/* Read the rmtab into a clean ms->mountlist.
 */
static void
mount_read_rmtab (struct mount3_state *ms)
{
        gf_store_handle_t       *sh = NULL;
        struct nfs_state        *nfs = NULL;
        gf_boolean_t            read_rmtab = _gf_false;

        nfs = (struct nfs_state *)ms->nfsx->private;

        read_rmtab = mount_open_rmtab (nfs->rmtab, &sh);
        if (!read_rmtab)
                return;

        if (gf_store_lock (sh)) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_LOCK_FAIL,
                        "Failed to lock '%s'", nfs->rmtab);
                goto out;
        }

        __mount_read_rmtab (sh, &ms->mountlist, _gf_false);
        gf_store_unlock (sh);

out:
        gf_store_handle_destroy (sh);
}

/* Write the ms->mountlist to the rmtab.
 *
 * The rmtab could be empty, or it can exists and have been updated by a
 * different storage server without our knowing.
 *
 * 0. if opening the nfs->rmtab fails, return gracefully
 * 1. takes the store_handle lock on the current rmtab
 *    - blocks if an other storage server rewrites the rmtab at the same time
 * 2. [if new_rmtab] takes the store_handle lock on the new rmtab
 * 3. reads/merges the entries from the current rmtab
 * 4. [if new_rmtab] reads/merges the entries from the new rmtab
 * 5. [if new_rmtab] writes the new rmtab
 * 6. [if not new_rmtab] writes the current rmtab
 * 7  [if new_rmtab] replaces nfs->rmtab to point to the new location
 * 8. [if new_rmtab] releases the store_handle lock of the new rmtab
 * 9. releases the store_handle lock of the old rmtab
 */
void
mount_rewrite_rmtab (struct mount3_state *ms, char *new_rmtab)
{
        gf_store_handle_t       *sh = NULL, *nsh = NULL;
        struct nfs_state        *nfs = NULL;
        int                     ret;
        char                    *rmtab = NULL;
        gf_boolean_t            got_old_rmtab = _gf_false;

        nfs = (struct nfs_state *)ms->nfsx->private;

        got_old_rmtab = mount_open_rmtab (nfs->rmtab, &sh);
        if (!got_old_rmtab && !new_rmtab)
                return;

        if (got_old_rmtab && gf_store_lock (sh)) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_REWRITE_ERROR,
                        "Not rewriting '%s'", nfs->rmtab);
                goto free_sh;
        }

        if (new_rmtab) {
                ret = gf_store_handle_new (new_rmtab, &nsh);
                if (ret) {
                        gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_OPEN_FAIL,
                                "Failed to open '%s'", new_rmtab);
                        goto unlock_sh;
                }

                if (gf_store_lock (nsh)) {
                        gf_msg (GF_MNT, GF_LOG_WARNING, 0,
                                NFS_MSG_REWRITE_ERROR,
                                "Not rewriting '%s'", new_rmtab);
                        goto free_nsh;
                }
        }

        /* always read the currently used rmtab */
        if (got_old_rmtab)
                __mount_read_rmtab (sh, &ms->mountlist, _gf_true);

        if (new_rmtab) {
                /* read the new rmtab and write changes to the new location */
                __mount_read_rmtab (nsh, &ms->mountlist, _gf_true);
                __mount_rewrite_rmtab (ms, nsh);

                /* replace the nfs->rmtab reference to the new rmtab */
                rmtab = gf_strdup(new_rmtab);
                if (rmtab == NULL) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, errno, NFS_MSG_NO_MEMORY,
                                "Out of memory, keeping %s as rmtab",
                                nfs->rmtab);
                } else {
                        GF_FREE (nfs->rmtab);
                        nfs->rmtab = rmtab;
                }

                gf_store_unlock (nsh);
        } else {
                /* rewrite the current (unchanged location) rmtab */
                __mount_rewrite_rmtab (ms, sh);
        }

free_nsh:
        if (new_rmtab)
                gf_store_handle_destroy (nsh);
unlock_sh:
        if (got_old_rmtab)
                gf_store_unlock (sh);
free_sh:
        if (got_old_rmtab)
                gf_store_handle_destroy (sh);
}

/* Add a new NFS-client to the ms->mountlist and update the rmtab if we can.
 *
 * A NFS-client will only be removed from the ms->mountlist in case the
 * NFS-client sends a unmount request. It is possible that a NFS-client
 * crashed/rebooted had network loss or something else prevented the NFS-client
 * to unmount cleanly. In this case, a duplicate entry would be added to the
 * ms->mountlist, which is wrong and we should prevent.
 *
 * It is fully acceptable that the ms->mountlist is not 100% correct, this is a
 * common issue for all(?) NFS-servers.
 */
int
mnt3svc_update_mountlist (struct mount3_state *ms, rpcsvc_request_t *req,
                          const char *expname, const char *fullpath)
{
        struct mountentry       *me = NULL;
        struct mountentry       *cur = NULL;
        int                     ret = -1;
        char                    *colon = NULL;
        struct nfs_state        *nfs = NULL;
        gf_store_handle_t       *sh = NULL;
        gf_boolean_t            update_rmtab = _gf_false;

        if ((!ms) || (!req) || (!expname))
                return -1;

        me = (struct mountentry *)GF_CALLOC (1, sizeof (*me),
                                             gf_nfs_mt_mountentry);
        if (!me)
                return -1;

        nfs = (struct nfs_state *)ms->nfsx->private;

        update_rmtab = mount_open_rmtab (nfs->rmtab, &sh);

        strncpy (me->exname, expname, MNTPATHLEN);
        /* Sometimes we don't care about the full path
         * so a NULL value for fullpath is valid.
         */
        if (fullpath) {
                if (strlen (fullpath) < MNTPATHLEN) {
                        strcpy (me->fullpath, fullpath);
                        me->has_full_path = _gf_true;
                }
        }


        INIT_LIST_HEAD (&me->mlist);
        /* Must get the IP or hostname of the client so we
         * can map it into the mount entry.
         */
        ret = rpcsvc_transport_peername (req->trans, me->hostname, MNTPATHLEN);
        if (ret == -1)
                goto free_err;

        colon = strrchr (me->hostname, ':');
        if (colon) {
                *colon = '\0';
        }
        LOCK (&ms->mountlock);
        {
                /* in case locking fails, we just don't write the rmtab */
                if (update_rmtab && gf_store_lock (sh)) {
                        gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_LOCK_FAIL,
                                "Failed to lock '%s', changes will not be "
                                "written", nfs->rmtab);
                } else if (update_rmtab) {
                        __mount_read_rmtab (sh, &ms->mountlist, _gf_false);
                }

                /* do not add duplicates */
                list_for_each_entry (cur, &ms->mountlist, mlist) {
                        if (!strcmp(cur->hostname, me->hostname) &&
                            !strcmp(cur->exname, me->exname)) {
                                GF_FREE (me);
                                goto dont_add;
                        }
                }
                list_add_tail (&me->mlist, &ms->mountlist);
                __mountdict_insert (ms, me);

                /* only write the rmtab in case it was locked */
                if (update_rmtab && gf_store_locked_local (sh))
                        __mount_rewrite_rmtab (ms, sh);
        }
dont_add:
        if (update_rmtab && gf_store_locked_local (sh))
                gf_store_unlock (sh);

        UNLOCK (&ms->mountlock);

free_err:
        if (update_rmtab)
                gf_store_handle_destroy (sh);

        if (ret == -1)
                GF_FREE (me);

        return ret;
}


int
__mnt3_get_volume_id (struct mount3_state *ms, xlator_t *mntxl,
                      uuid_t volumeid)
{
        int                     ret = -1;
        struct mnt3_export      *exp = NULL;

        if ((!ms) || (!mntxl))
                return ret;

        LOCK (&ms->mountlock);
        list_for_each_entry (exp, &ms->exportlist, explist) {
                if (exp->vol == mntxl) {
                        gf_uuid_copy (volumeid, exp->volumeid);
                        ret = 0;
                        goto out;
                }
        }

out:
        UNLOCK (&ms->mountlock);
        return ret;
}

int
__mnt3_build_mountid_from_path (const char *path, uuid_t mountid)
{
        uint32_t hashed_path = 0;
        int      ret = -1;

        if (!path)
                goto out;

        while (strlen (path) > 0 && path[0] == '/')
                path++;

        /* Clear the mountid */
        gf_uuid_clear (mountid);

        hashed_path = SuperFastHash (path,  strlen (path));
        if (hashed_path == 1) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_HASH_PATH_FAIL,
                        "failed to hash path: %s", path);
                goto out;
        }

        memcpy (mountid, &hashed_path, sizeof (hashed_path));
        ret = 0;
out:
        return ret;
}

int
__mnt3_get_mount_id (xlator_t *mntxl, uuid_t mountid)
{
        int ret = -1;
        uint32_t hashed_path = 0;


        /* first clear the mountid */
        gf_uuid_clear (mountid);

        hashed_path = SuperFastHash (mntxl->name, strlen (mntxl->name));
        if (hashed_path == 1) {
                gf_msg (GF_MNT, GF_LOG_WARNING, 0, NFS_MSG_HASH_XLATOR_FAIL,
                        "failed to hash xlator name: %s", mntxl->name);
                goto out;
        }

        memcpy (mountid, &hashed_path, sizeof (hashed_path));
        ret = 0;
out:
        return ret;
}


int32_t
mnt3svc_lookup_mount_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)
{
        mountres3               res = {0, };
        rpcsvc_request_t        *req = NULL;
        struct nfs3_fh          fh = {{0}, };
        struct mount3_state     *ms = NULL;
        mountstat3              status = 0;
        int                     autharr[10];
        int                     autharrlen = 0;
        rpcsvc_t                *svc = NULL;
        xlator_t                *mntxl = NULL;
        uuid_t                  volumeid = {0, };
        char                    *path = NULL;
        uuid_t                  mountid = {1, };
        char                    fhstr[1536];

        req = (rpcsvc_request_t *)frame->local;

        if (!req)
                return -1;

        mntxl = (xlator_t *)cookie;
        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND,
                        "mount state not found");
                op_ret = -1;
                op_errno = EINVAL;
        }

        if (op_ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, op_errno,
                        NFS_MSG_LOOKUP_MNT_ERROR, "error=%s",
                        strerror (op_errno));
                status = mnt3svc_errno_to_mnterr (op_errno);
        }
        if (status != MNT3_OK)
                goto xmit_res;

        path = GF_CALLOC (PATH_MAX, sizeof (char), gf_nfs_mt_char);
        if (!path) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Out of memory");
                goto xmit_res;
        }

        snprintf (path, PATH_MAX, "/%s", mntxl->name);
        mnt3svc_update_mountlist (ms, req, path, NULL);
        GF_FREE (path);
        if (gf_nfs_dvm_off (nfs_state (ms->nfsx))) {
                fh = nfs3_fh_build_indexed_root_fh (ms->nfsx->children, mntxl);
                goto xmit_res;
        }

        __mnt3_get_mount_id (mntxl, mountid);
        __mnt3_get_volume_id (ms, mntxl, volumeid);
        fh = nfs3_fh_build_uuid_root_fh (volumeid, mountid);

xmit_res:
        nfs3_fh_to_str (&fh, fhstr, sizeof (fhstr));
        gf_msg_debug (GF_MNT, 0, "MNT reply: fh %s, status: %d", fhstr,
                      status);
        if (op_ret == 0) {
                svc = rpcsvc_request_service (req);
                autharrlen = rpcsvc_auth_array (svc, mntxl->name, autharr,
                                                10);
        }

        res = mnt3svc_set_mountres3 (status, &fh, autharr, autharrlen);
        mnt3svc_submit_reply (req, (void *)&res,
                              (mnt3_serializer)xdr_serialize_mountres3);

        return 0;
}


int
mnt3_match_dirpath_export (const char *expname, const char *dirpath,
                           gf_boolean_t export_parsing_match)
{
        int     ret = 0;
        size_t  dlen;
        char    *fullpath = NULL;
        char    *second_slash = NULL;
        char    *dirdup = NULL;

        if ((!expname) || (!dirpath))
                return 0;

        dirdup = strdupa (dirpath);

        /* Some clients send a dirpath for mount that includes the slash at the
         * end. String compare for searching the export will fail because our
         * exports list does not include that slash. Remove the slash to
         * compare.
         */
        dlen = strlen (dirdup);
        if (dlen && dirdup[dlen - 1] == '/')
                dirdup[dlen - 1] = '\0';

        /* Here we try to match fullpaths with export names */
        fullpath = dirdup;

        if (export_parsing_match) {
                if (dirdup[0] == '/')
                        fullpath = dirdup + 1;

                second_slash = strchr (fullpath, '/');
                if (second_slash)
                        *second_slash = '\0';
        }

        /* The export name begins with a slash so move it forward by one
         * to ignore the slash when we want to compare the fullpath and
         * export.
         */
        if (fullpath[0] != '/')
                expname++;

        if (strcmp (expname, fullpath) == 0)
                ret = 1;

        return ret;
}


int
mnt3svc_mount_inode (rpcsvc_request_t *req, struct mount3_state *ms,
                     xlator_t * xl, inode_t *exportinode)
{
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };
        loc_t           exportloc = {0, };

        if ((!req) || (!xl) || (!ms) || (!exportinode))
                return ret;

        ret = nfs_inode_loc_fill (exportinode, &exportloc, NFS_RESOLVE_EXIST);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_INODE_LOC_FILL_ERROR,
                        "Loc fill failed for export inode"
                        ": gfid %s, volume: %s",
                        uuid_utoa (exportinode->gfid), xl->name);
                goto err;
        }

        /* To service the mount request, all we need to do
         * is to send a lookup fop that returns the stat
         * for the root of the child volume. This is
         * used to build the root fh sent to the client.
         */
        nfs_request_user_init (&nfu, req);
        ret = nfs_lookup (ms->nfsx, xl, &nfu, &exportloc,
                          mnt3svc_lookup_mount_cbk, (void *)req);

        nfs_loc_wipe (&exportloc);
err:
        return ret;
}


/* For a volume mount request, we just have to create loc on the root inode,
 * and send a lookup. In the lookup callback the mount reply is send along with
 * the file handle.
 */
int
mnt3svc_volume_mount (rpcsvc_request_t *req, struct mount3_state *ms,
                      struct mnt3_export *exp)
{
        inode_t         *exportinode = NULL;
        int             ret = -EFAULT;
        uuid_t          rootgfid = {0, };

        if ((!req) || (!exp) || (!ms))
                return ret;

        rootgfid[15] = 1;
        exportinode = inode_find (exp->vol->itable, rootgfid);
        if (!exportinode) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOENT,
                        NFS_MSG_GET_ROOT_INODE_FAIL,
                        "Failed to get root inode");
                ret = -ENOENT;
                goto err;
        }

        ret = mnt3svc_mount_inode (req, ms, exp->vol, exportinode);
        inode_unref (exportinode);

err:
        return ret;
}


/* The catch with directory exports is that the first component of the export
 * name will be the name of the volume.
 * Any lookup that needs to be performed to build the directory's file handle
 * needs to start from the directory path from the root of the volume. For that
 * we need to strip out the volume name first.
 */
char *
mnt3_get_volume_subdir (char *dirpath, char **volname)
{
        /* subdir points to the first / after the volume name while dirpath
         * points to the first char of the volume name.
         */
        char          *subdir        = NULL;
        int            volname_len   = 0;
        static char   *root          = "/";

        /* all callers are expected to pass a valid *dirpath */
        GF_ASSERT (dirpath);

        if (dirpath[0] == '/')
                dirpath++;

        subdir = index (dirpath, (int)'/');
        if (!subdir) {
                subdir = root;
                volname_len = strlen (dirpath);
        } else {
                volname_len = subdir - dirpath;
        }

        if (!volname)
                goto out;

        if (!*volname)
                goto out;

        strncpy (*volname, dirpath, volname_len);
        *(*volname + volname_len) = '\0';
out:
        return subdir;
}


void
mnt3_resolve_state_wipe (mnt3_resolve_t *mres)
{
        if (!mres)
                return;

        nfs_loc_wipe (&mres->resolveloc);
        GF_FREE (mres);

}


/* Sets up the component argument to contain the next component in the path and
 * sets up path as an absolute path starting from the next component.
 */
static char *
setup_next_component (char *path, size_t plen, char *component, size_t clen)
{
        char    *comp = NULL;
        char    *nextcomp = NULL;

        if ((!path) || (!component))
                return NULL;

        strncpy (component, path, clen);
        comp = index (component, (int)'/');
        if (!comp)
                goto err;

        comp++;
        nextcomp = index (comp, (int)'/');
        if (nextcomp) {
                strncpy (path, nextcomp, plen);
                *nextcomp = '\0';
        } else
                path[0] = '\0';

err:
        return comp;
}

int32_t
mnt3_resolve_subdir_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);

int32_t
mnt3_readlink_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, const char *path,
                   struct iatt *buf, dict_t *xdata);

/* There are multiple components in the directory export path and each one
 * needs to be looked up one after the other.
 */
int
__mnt3_resolve_export_subdir_comp (mnt3_resolve_t *mres)
{
        char            dupsubdir[MNTPATHLEN];
        char            *nextcomp = NULL;
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };
        uuid_t          gfid = {0, };

        if (!mres)
                return ret;

        nextcomp = setup_next_component (mres->remainingdir,
                                         sizeof (mres->remainingdir),
                                         dupsubdir, sizeof (dupsubdir));
        if (!nextcomp)
                goto err;

        /* Wipe the contents of the previous component */
        gf_uuid_copy (gfid, mres->resolveloc.inode->gfid);
        nfs_loc_wipe (&mres->resolveloc);
        ret = nfs_entry_loc_fill (mres->mstate->nfsx, mres->exp->vol->itable,
                                  gfid, nextcomp, &mres->resolveloc,
                                  NFS_RESOLVE_CREATE, NULL);
        if ((ret < 0) && (ret != -2)) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EFAULT,
                        NFS_MSG_RESOLVE_INODE_FAIL, "Failed to resolve and "
                        "create inode: parent gfid %s, entry %s",
                        uuid_utoa (gfid), nextcomp);
                ret = -EFAULT;
                goto err;
        }

        nfs_request_user_init (&nfu, mres->req);
        if (IA_ISLNK (mres->resolveloc.inode->ia_type)) {
                ret = nfs_readlink (mres->mstate->nfsx, mres->exp->vol, &nfu,
                                    &mres->resolveloc, mnt3_readlink_cbk, mres);
                gf_msg_debug (GF_MNT, 0, "Symlink found , need to resolve"
                              " into directory handle");
                goto err;
        }
        ret = nfs_lookup (mres->mstate->nfsx, mres->exp->vol, &nfu,
                          &mres->resolveloc, mnt3_resolve_subdir_cbk, mres);

err:
        return ret;
}

int __mnt3_resolve_subdir (mnt3_resolve_t *mres);

/*
 * Per the AFR2 comments, this function performs the "fresh" lookup
 * by deleting the inode from cache and calling __mnt3_resolve_subdir
 * again.
 */
int __mnt3_fresh_lookup (mnt3_resolve_t *mres) {
        inode_unlink (mres->resolveloc.inode,
                mres->resolveloc.parent, mres->resolveloc.name);
        strncpy (mres->remainingdir, mres->resolveloc.path,
                strlen(mres->resolveloc.path));
        nfs_loc_wipe (&mres->resolveloc);
        return __mnt3_resolve_subdir (mres);
}

int32_t
mnt3_resolve_subdir_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)
{
        mnt3_resolve_t          *mres = NULL;
        mountstat3              mntstat = MNT3ERR_SERVERFAULT;
        struct nfs3_fh          fh = {{0}, };
        int                     autharr[10];
        int                     autharrlen = 0;
        rpcsvc_t                *svc = NULL;
        mountres3               res = {0, };
        xlator_t                *mntxl = NULL;
        char                    *path = NULL;
        struct mount3_state     *ms = NULL;
        int                     authcode = 0;
        char                    *authorized_host = NULL;
        char                    *authorized_path = NULL;
        inode_t                 *linked_inode = NULL;

        mres = frame->local;
        ms = mres->mstate;
        mntxl = (xlator_t *)cookie;
        if (op_ret == -1 && op_errno == ESTALE) {
                /* Nuke inode from cache and try the LOOKUP
                 * request again. */
                return __mnt3_fresh_lookup (mres);
        } else if (op_ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, op_errno,
                        NFS_MSG_RESOLVE_SUBDIR_FAIL, "path=%s (%s)",
                        mres->resolveloc.path, strerror (op_errno));
                mntstat = mnt3svc_errno_to_mnterr (op_errno);
                goto err;
        }

        linked_inode = inode_link (mres->resolveloc.inode,
                                   mres->resolveloc.parent,
                                   mres->resolveloc.name, buf);

        if (linked_inode)
                nfs_fix_generation (this, linked_inode);

        nfs3_fh_build_child_fh (&mres->parentfh, buf, &fh);
        if (strlen (mres->remainingdir) <= 0) {
                size_t alloclen;
                op_ret = -1;
                mntstat = MNT3_OK;

                /* Construct the full path */
                alloclen = strlen (mres->exp->expname) +
                                   strlen (mres->resolveloc.path) + 1;
                mres->exp->fullpath = GF_CALLOC (alloclen, sizeof (char),
                                                gf_nfs_mt_char);
                if (!mres->exp->fullpath) {
                        gf_msg (GF_MNT, GF_LOG_CRITICAL, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Allocation failed.");
                        goto err;
                }
                snprintf (mres->exp->fullpath, alloclen, "%s%s",
                                mres->exp->expname, mres->resolveloc.path);

                /* Check if this path is authorized to be mounted */
                authcode = mnt3_authenticate_request (ms, mres->req, NULL, NULL,
                                                      mres->exp->fullpath,
                                                      &authorized_path,
                                                      &authorized_host,
                                                      FALSE);
                if (authcode != 0) {
                        mntstat = MNT3ERR_ACCES;
                        gf_msg_debug (GF_MNT, 0, "Client mount not allowed");
                        op_ret = -1;
                        goto err;
                }

                path = GF_CALLOC (PATH_MAX, sizeof (char), gf_nfs_mt_char);
                if (!path) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY,
                                "Memory allocation failed");
                        goto err;
                }
                /* Build mountid from the authorized path and stick it in the
                 * filehandle that will get passed back to the client
                 */
                __mnt3_build_mountid_from_path (authorized_path, fh.mountid);

                snprintf (path, PATH_MAX, "/%s%s", mres->exp->vol->name,
                         mres->resolveloc.path);

                mnt3svc_update_mountlist (mres->mstate, mres->req,
                                          path, mres->exp->fullpath);
                GF_FREE (path);
        } else {
                mres->parentfh = fh;
                op_ret = __mnt3_resolve_export_subdir_comp (mres);
                if (op_ret < 0)
                        mntstat = mnt3svc_errno_to_mnterr (-op_ret);
        }
err:
        if (op_ret == -1) {
                gf_msg_debug (GF_MNT, 0, "Mount reply status: %d", mntstat);
                svc = rpcsvc_request_service (mres->req);
                autharrlen = rpcsvc_auth_array (svc, mntxl->name, autharr,
                                                10);

                res = mnt3svc_set_mountres3 (mntstat, &fh, autharr, autharrlen);
                mnt3svc_submit_reply (mres->req, (void *)&res,
                                      (mnt3_serializer)xdr_serialize_mountres3);
                mnt3_resolve_state_wipe (mres);
        }

        GF_FREE (authorized_path);
        GF_FREE (authorized_host);

        return 0;
}

/* This function resolves symbolic link into directory path from
 * the mount and restart the parsing process from the beginning
 *
 * Note : Path specified in the symlink should be relative to the
 *        symlink, because that is the one which is consistent through
 *        out the file system.
 *        If the symlink resolves into another symlink ,then same process
 *        will be repeated.
 *        If symbolic links points outside the file system are not considered
 *        here.
 *
 * TODO : 1.) This function cannot handle symlinks points to path which
 *            goes out of the filesystem and comes backs again to same.
 *            For example, consider vol is exported volume.It contains
 *            dir,
 *            symlink1 which points to ../vol/dir,
 *            symlink2 which points to ../mnt/../vol/dir,
 *            symlink1 and symlink2 are not handled right now.
 *
 *        2.) udp mount routine is much simpler from tcp routine and resolves
 *            symlink directly.May be ,its better we change this routine
 *            similar to udp
 */
int32_t
mnt3_readlink_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                   int32_t op_ret, int32_t op_errno, const char *path,
                   struct iatt *buf, dict_t *xdata)
{
        mnt3_resolve_t           *mres            = NULL;
        int                      ret              = -EFAULT;
        char                     *real_loc        = NULL;
        size_t                   path_len         = 0;
        size_t                   parent_path_len  = 0;
        char                     *parent_path     = NULL;
        char                     *absolute_path   = NULL;
        char                     *relative_path   = NULL;
        int                      mntstat          = 0;

        GF_ASSERT (frame);

        mres = frame->local;
        if (!mres || !path || (path[0] == '/') || (op_ret < 0))
                goto mnterr;

        /* Finding current location of symlink */
        parent_path_len = strlen (mres->resolveloc.path) - strlen (mres->resolveloc.name);
        parent_path = gf_strndup (mres->resolveloc.path, parent_path_len);
        if (!parent_path) {
                ret = -ENOMEM;
                goto mnterr;
        }

        relative_path = gf_strdup (path);
        if (!relative_path) {
                ret = -ENOMEM;
                goto mnterr;
        }
        /* Resolving into absolute path */
        ret = gf_build_absolute_path (parent_path, relative_path, &absolute_path);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                        NFS_MSG_RESOLVE_SYMLINK_ERROR,
                        "Cannot resolve symlink, path is out of boundary "
                        "from current location %s and with relative path "
                        "%s pointed by symlink", parent_path, relative_path);

                goto mnterr;
        }

        /* Building the actual mount path to be mounted */
        path_len = strlen (mres->exp->vol->name) +  strlen (absolute_path)
                   + strlen (mres->remainingdir) + 1;
        real_loc = GF_CALLOC (1, path_len, gf_nfs_mt_char);
        if (!real_loc) {
                ret = -ENOMEM;
                goto mnterr;
        }
        sprintf (real_loc , "%s%s", mres->exp->vol->name, absolute_path);
        gf_path_strip_trailing_slashes (real_loc);

        /* There may entries after symlink in the mount path,
         * we should include remaining entries too */
        if (strlen (mres->remainingdir) > 0)
                strcat (real_loc, mres->remainingdir);

        gf_msg_debug (GF_MNT, 0, "Resolved path is : %s%s "
                      "and actual mount path is %s",
                      absolute_path, mres->remainingdir, real_loc);

        /* After the resolving the symlink , parsing should be done
         * for the populated mount path
         */
        ret = mnt3_parse_dir_exports (mres->req, mres->mstate, real_loc,
                                      _gf_true);

        if (ret) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_RESOLVE_ERROR,
                        "Resolved into an unknown path %s%s "
                        "from the current location of symlink %s",
                         absolute_path, mres->remainingdir, parent_path);
        }

        GF_FREE (real_loc);
        GF_FREE (absolute_path);
        GF_FREE (parent_path);
        GF_FREE (relative_path);

        return ret;

mnterr:
        if (mres) {
                mntstat = mnt3svc_errno_to_mnterr (-ret);
                mnt3svc_mnt_error_reply (mres->req, mntstat);
        } else
                gf_msg (GF_MNT, GF_LOG_CRITICAL, EINVAL, NFS_MSG_INVALID_ENTRY,
                        "mres == NULL, this should *never* happen");
        if (absolute_path)
                GF_FREE (absolute_path);
        if (parent_path)
                GF_FREE (parent_path);
        if (relative_path)
                GF_FREE (relative_path);
        return ret;
}

/* We will always have to perform a hard lookup on all the components of a
 * directory export for a mount request because in the mount reply we need the
 * file handle of the directory. Our file handle creation code is designed with
 * the assumption that to build a child file/dir fh, we'll always have the
 * parent dir's fh available so that we may copy the hash array of the previous
 * dir levels.
 *
 * Since we do not store the file handles anywhere, for every mount request we
 * must resolve the file handles of every component so that the parent dir file
 * of the exported directory can be built.
 */
int
__mnt3_resolve_subdir (mnt3_resolve_t *mres)
{
        char            dupsubdir[MNTPATHLEN];
        char            *firstcomp = NULL;
        int             ret = -EFAULT;
        nfs_user_t      nfu = {0, };
        uuid_t          rootgfid = {0, };

        if (!mres)
                return ret;

        firstcomp = setup_next_component (mres->remainingdir,
                                          sizeof (mres->remainingdir),
                                          dupsubdir, sizeof (dupsubdir));
        if (!firstcomp)
                goto err;

        rootgfid[15] = 1;
        ret = nfs_entry_loc_fill (mres->mstate->nfsx, mres->exp->vol->itable,
                                  rootgfid, firstcomp, &mres->resolveloc,
                                  NFS_RESOLVE_CREATE, NULL);
        if ((ret < 0) && (ret != -2)) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EFAULT,
                        NFS_MSG_RESOLVE_INODE_FAIL, "Failed to resolve and "
                        "create inode for volume root: %s",
                        mres->exp->vol->name);
                ret = -EFAULT;
                goto err;
        }

        nfs_request_user_init (&nfu, mres->req);
        if (IA_ISLNK (mres->resolveloc.inode->ia_type)) {
                ret = nfs_readlink (mres->mstate->nfsx, mres->exp->vol, &nfu,
                                    &mres->resolveloc, mnt3_readlink_cbk, mres);
                gf_msg_debug (GF_MNT, 0, "Symlink found , need to resolve "
                              "into directory handle");
                goto err;
        }
        ret = nfs_lookup (mres->mstate->nfsx, mres->exp->vol, &nfu,
                          &mres->resolveloc, mnt3_resolve_subdir_cbk, mres);

err:
        return ret;
}


static gf_boolean_t
mnt3_match_subnet_v4 (struct addrinfo *ai, uint32_t saddr, uint32_t mask)
{
        for (; ai; ai = ai->ai_next) {
                struct sockaddr_in *sin = (struct sockaddr_in *)ai->ai_addr;

                if (sin->sin_family != AF_INET)
                        continue;

                if (mask_match (saddr, sin->sin_addr.s_addr, mask))
                        return _gf_true;
        }

        return _gf_false;
}


/**
 * This function will verify if the client is allowed to mount
 * the directory or not. Client's IP address will be compared with
 * allowed IP list or range present in mnt3_export structure.
 *
 * @param client_addr - This structure contains client's IP address.
 * @param export - mnt3_export structure. Contains allowed IP list/range.
 *
 * @return 0 - on Success and -EACCES on failure.
 *
 * TODO: Support IPv6 subnetwork
 */
int
mnt3_verify_auth (struct sockaddr_in *client_addr, struct mnt3_export *export)
{
        int                     retvalue = -EACCES;
        int                     ret = 0;
        struct host_auth_spec   *host = NULL;
        struct sockaddr_in      *allowed_addr = NULL;
        struct addrinfo         *allowed_addrinfo = NULL;

        struct addrinfo         hint = {
                .ai_family      = AF_INET,
                .ai_protocol    = (int)IPPROTO_TCP,
                .ai_flags       = AI_CANONNAME,
        };

        /* Sanity check */
        if ((NULL == client_addr) ||
            (NULL == export) ||
            (NULL == export->hostspec)) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
                        "Invalid argument");
                return retvalue;
        }

        host = export->hostspec;

        /*
         * Currently IPv4 subnetwork is supported i.e. AF_INET.
         * TODO: IPv6 subnetwork i.e. AF_INET6.
         */
        if (client_addr->sin_family != AF_INET) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EAFNOSUPPORT,
                        NFS_MSG_UNSUPPORTED_VERSION,
                        "Only IPv4 is supported for subdir-auth");
                return retvalue;
        }

        /* Try to see if the client IP matches the allowed IP list.*/
        while (NULL != host){
                GF_ASSERT (host->host_addr);

                if (NULL != allowed_addrinfo) {
                        freeaddrinfo (allowed_addrinfo);
                        allowed_addrinfo = NULL;
                }

                /* Get the addrinfo for the allowed host (host_addr). */
                ret = getaddrinfo (host->host_addr, NULL,
                                   &hint, &allowed_addrinfo);
                if (0 != ret){
                        /*
                         * getaddrinfo() FAILED for the host IP addr. Continue
                         * to search other allowed hosts in the  hostspec list.
                         */
                        gf_msg_debug (GF_MNT, 0, "getaddrinfo: %s\n",
                                      gai_strerror (ret));
                        host = host->next;
                        continue;
                }

                allowed_addr = (struct sockaddr_in *)(allowed_addrinfo->ai_addr);
                if (NULL == allowed_addr) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                                NFS_MSG_INVALID_ENTRY, "Invalid structure");
                        break;
                }

                /* Check if the network addr of both IPv4 socket match */
                if (mnt3_match_subnet_v4 (allowed_addrinfo,
                                          client_addr->sin_addr.s_addr,
                                          host->netmask)) {
                        retvalue = 0;
                        break;
                }

                /* No match yet, continue the search */
               host = host->next;
        }

        /* FREE the dynamic memory allocated by getaddrinfo() */
        if (NULL != allowed_addrinfo) {
               freeaddrinfo (allowed_addrinfo);
        }

        return retvalue;
}

int
mnt3_resolve_subdir (rpcsvc_request_t *req, struct mount3_state *ms,
                     struct mnt3_export *exp, char *subdir,
                     gf_boolean_t send_reply)
{
        mnt3_resolve_t        *mres = NULL;
        int                   ret = -EFAULT;
        struct nfs3_fh        pfh = GF_NFS3FH_STATIC_INITIALIZER;
        struct sockaddr_in    *sin = NULL;

        if ((!req) || (!ms) || (!exp) || (!subdir))
                return ret;

        sin = (struct sockaddr_in *)(&(req->trans->peerinfo.sockaddr));

        /* Need to check AUTH */
        if (NULL != exp->hostspec) {
                ret = mnt3_verify_auth (sin, exp);
                if (0 != ret) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, EACCES,
                                NFS_MSG_AUTH_VERIFY_FAILED,
                                "AUTH verification failed");
                        return ret;
                }
        }

        /* no reply is needed (WebNFS permissions checking), just return */
        if (!send_reply)
                return 0; /* no error, mnt3_verify_auth() allowed it */

        mres = GF_CALLOC (1, sizeof (mnt3_resolve_t), gf_nfs_mt_mnt3_resolve);
        if (!mres) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                goto err;
        }

        mres->exp = exp;
        mres->mstate = ms;
        mres->req = req;

        strncpy (mres->remainingdir, subdir, MNTPATHLEN);
        gf_path_strip_trailing_slashes (mres->remainingdir);

        if (gf_nfs_dvm_off (nfs_state (ms->nfsx)))
                pfh = nfs3_fh_build_indexed_root_fh (
                                            mres->mstate->nfsx->children,
                                            mres->exp->vol);
        else
                pfh = nfs3_fh_build_uuid_root_fh (exp->volumeid, exp->mountid);

        mres->parentfh = pfh;
        ret = __mnt3_resolve_subdir (mres);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_RESOLVE_SUBDIR_FAIL,
                        "Failed to resolve export dir: %s", mres->exp->expname);
                GF_FREE (mres);
        }

err:
        return ret;
}


int
mnt3_resolve_export_subdir (rpcsvc_request_t *req, struct mount3_state *ms,
                            struct mnt3_export *exp)
{
        char            *volume_subdir = NULL;
        int             ret = -EFAULT;

        if ((!req) || (!ms) || (!exp))
                return ret;

        volume_subdir = mnt3_get_volume_subdir (exp->expname, NULL);

        ret = mnt3_resolve_subdir (req, ms, exp, volume_subdir, _gf_true);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_RESOLVE_SUBDIR_FAIL,
                        "Failed to resolve export dir: %s", exp->expname);
                goto err;
        }

err:
        return ret;
}


int
mnt3svc_mount (rpcsvc_request_t *req, struct mount3_state *ms,
               struct mnt3_export *exp)
{
        int     ret = -EFAULT;

        if ((!req) || (!ms) || (!exp))
                return ret;

        if (exp->exptype == MNT3_EXPTYPE_VOLUME)
                ret = mnt3svc_volume_mount (req, ms, exp);
        else if (exp->exptype == MNT3_EXPTYPE_DIR)
                ret = mnt3_resolve_export_subdir (req, ms, exp);

        return ret;
}


/* mnt3_mntpath_to_xlator sets this to 1 if the mount is for a full
* volume or 2 for a subdir in the volume.
*
* The parameter 'export_parsing_match' indicates whether this function
* is being called by an exports parser or whether it is being called
* during mount. The behavior is different since we don't have to resolve
* the path when doing the parse.
*/
struct mnt3_export *
mnt3_mntpath_to_export (struct mount3_state *ms, const char *dirpath,
                        gf_boolean_t export_parsing_match)
{
        struct mnt3_export      *exp = NULL;
        struct mnt3_export      *found = NULL;

        if ((!ms) || (!dirpath))
                return NULL;

        LOCK (&ms->mountlock);
        list_for_each_entry (exp, &ms->exportlist, explist) {

                /* Search for the an exact match with the volume */
                if (mnt3_match_dirpath_export (exp->expname, dirpath,
                                               export_parsing_match)) {
                        found = exp;
                        gf_msg_debug (GF_MNT, 0, "Found export volume: "
                                      "%s", exp->vol->name);
                        goto foundexp;
                }
        }

        gf_msg_debug (GF_MNT, 0, "Export not found");
foundexp:
        UNLOCK (&ms->mountlock);
        return found;
}


static int
mnt3_check_client_net_check (rpcsvc_t *svc, char *expvol,
                             char *ipaddr, uint16_t port)
{
        int ret = RPCSVC_AUTH_REJECT;

        if ((!svc) || (!expvol) || (!ipaddr))
                goto err;

        ret = rpcsvc_auth_check (svc, expvol, ipaddr);
        if (ret == RPCSVC_AUTH_REJECT) {
                gf_msg (GF_MNT, GF_LOG_INFO, 0, NFS_MSG_PEER_NOT_ALLOWED,
                        "Peer %s  not allowed", ipaddr);
                goto err;
        }

        ret = rpcsvc_transport_privport_check (svc, expvol, port);
        if (ret == RPCSVC_AUTH_REJECT) {
                gf_msg (GF_MNT, GF_LOG_INFO, errno, NFS_MSG_PEER_NOT_ALLOWED,
                        "Peer %s rejected. Unprivileged "
                        "port %d not allowed", ipaddr, port);
                goto err;
        }

        ret = RPCSVC_AUTH_ACCEPT;
err:
        return ret;
}

int
mnt3_check_client_net_tcp (rpcsvc_request_t *req, char *volname)
{
        rpcsvc_t                *svc = NULL;
        rpc_transport_t         *trans = NULL;
        union gf_sock_union     sock_union;
        socklen_t               socksize = sizeof (struct sockaddr_in);
        char                    peer[RPCSVC_PEER_STRLEN] = {0,};
        char                    *ipaddr = NULL;
        uint16_t                port = 0;
        int                     ret = RPCSVC_AUTH_REJECT;

        if ((!req) || (!volname))
                goto err;

        svc = rpcsvc_request_service (req);
        trans = rpcsvc_request_transport (req);
        if ((!svc) || (!trans))
                goto err;

        ret = rpcsvc_transport_peeraddr (trans, peer, RPCSVC_PEER_STRLEN,
                                         &sock_union.storage, socksize);
        if (ret != 0) {
                gf_msg (GF_MNT, GF_LOG_WARNING, ENOENT,
                        NFS_MSG_GET_PEER_ADDR_FAIL, "Failed to get peer "
                        "addr: %s", gai_strerror (ret));
                ret = RPCSVC_AUTH_REJECT;
                goto err;
        }

        /* peer[] gets IP:PORT formar, slash the port out */
        if (!get_host_name ((char *)peer, &ipaddr))
                ipaddr = peer;

        port = ntohs (sock_union.sin.sin_port);

        ret = mnt3_check_client_net_check (svc, volname, ipaddr, port);
err:
        return ret;
}

static int
mnt3_check_client_net_udp (struct svc_req *req, char *volname, xlator_t *nfsx)
{
        rpcsvc_t                *svc = NULL;
        struct sockaddr_in      *sin = NULL;
        char                    ipaddr[INET_ADDRSTRLEN + 1] = {0, };
        uint16_t                port = 0;
        int                     ret = RPCSVC_AUTH_REJECT;
        struct nfs_state        *nfs = NULL;

        if ((!req) || (!volname) || (!nfsx))
                goto err;

#if !defined(_TIRPC_SVC_H)
        sin = svc_getcaller (req->rq_xprt);
#else
        sin = (struct sockaddr_in *)svc_getcaller (req->rq_xprt);
        /* TIRPC's svc_getcaller() returns a pointer to a sockaddr_in6, even
         * though it might actually be an IPv4 address. It ought return a
         * struct sockaddr and make the caller upcast it to the proper
         * address family. Sigh.
         */
#endif
        /* And let's make sure that it's actually an IPv4 address. */
        GF_ASSERT (sin->sin_family == AF_INET);

        if (!sin)
                goto err;

        (void) inet_ntop (AF_INET, &sin->sin_addr, ipaddr, INET_ADDRSTRLEN);

        port = ntohs (sin->sin_port);

        nfs = (struct nfs_state *)nfsx->private;
        if (nfs != NULL)
                svc = nfs->rpcsvc;

        ret = mnt3_check_client_net_check (svc, volname, ipaddr, port);
err:
        return ret;
}


int
mnt3_parse_dir_exports (rpcsvc_request_t *req, struct mount3_state *ms,
                        char *path, gf_boolean_t send_reply)
{
        char                    volname[1024] = {0, };
        struct mnt3_export      *exp = NULL;
        char                    *volname_ptr = NULL;
        char                    *subdir = NULL;
        int                     ret = -ENOENT;
        struct nfs_state        *nfs = NULL;

        if ((!ms) || (!path))
                return -1;

        volname_ptr = volname;
        subdir = mnt3_get_volume_subdir (path, &volname_ptr);

        /* first try to match the full export/subdir */
        exp = mnt3_mntpath_to_export (ms, path, _gf_false);
        if (!exp) {
                gf_msg_trace (GF_MNT, 0, "Could not find exact matching export "
                              "for path=%s", path);
                /* if no exact match is found, look for a fallback */
                exp = mnt3_mntpath_to_export (ms, volname, _gf_true);
                if (!exp) {
                        gf_msg_trace (GF_MNT, 0, "Could not find export for "
                                      "volume %s", volname);
                        goto err;
                }
        }
        gf_msg_trace (GF_MNT, 0, "volume %s and export %s will be used for "
                      "path %s", exp->vol->name, exp->expname, path);

        nfs = (struct nfs_state *)ms->nfsx->private;
        if (!nfs)
                goto err;

        if (!nfs_subvolume_started (nfs, exp->vol)) {
                gf_msg_debug (GF_MNT, 0, "Volume %s not started",
                              exp->vol->name);
                goto err;
        }

        ret = mnt3_check_client_net_tcp (req, exp->vol->name);
        if (ret == RPCSVC_AUTH_REJECT) {
                gf_msg_debug (GF_MNT, 0, "Client mount not allowed");
                ret = -EACCES;
                goto err;
        }

        ret = mnt3_resolve_subdir (req, ms, exp, subdir, send_reply);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_RESOLVE_SUBDIR_FAIL,
                        "Failed to resolve export dir: %s", subdir);
                goto err;
        }

err:
        return ret;
}


int
mnt3_find_export (rpcsvc_request_t *req, char *path, struct mnt3_export **e)
{
        int                     ret = -EFAULT;
        struct mount3_state     *ms = NULL;
        struct mnt3_export      *exp = NULL;

        if ((!req) || (!path) || (!e))
                return -1;

        ms = (struct mount3_state *) rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND, "Mount state not present");
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                goto err;
        }

        gf_msg_debug (GF_MNT, 0, "dirpath: %s", path);
        exp = mnt3_mntpath_to_export (ms, path, _gf_false);
        if (exp) {
                ret = 0;
                *e = exp;
                goto err;
        }

        if (!gf_mnt3_export_dirs(ms)) {
                ret = -1;
                goto err;
        }

        ret = mnt3_parse_dir_exports (req, ms, path, _gf_true);

err:
        return ret;
}

/**
 * _mnt3_get_peer_addr -- Take an rpc request object and return an allocated
 *                        peer address. A peer address is host:port.
 *
 * @req: An rpc svc request object to extract the peer address from
 *
 * @return: success: Pointer to an allocated string containing the peer address
 *          failure: NULL
 */
char *
_mnt3_get_peer_addr (const rpcsvc_request_t *req)
{
        rpc_transport_t         *trans = NULL;
        struct sockaddr_storage sastorage = {0, };
        char                    peer[RPCSVC_PEER_STRLEN] = {0, };
        char                    *peerdup = NULL;
        int                     ret = 0;

        GF_VALIDATE_OR_GOTO (GF_NFS, req, out);

        trans = rpcsvc_request_transport (req);
        ret = rpcsvc_transport_peeraddr (trans, peer, RPCSVC_PEER_STRLEN,
                                         &sastorage, sizeof (sastorage));
        if (ret != 0)
                goto out;

        peerdup = gf_strdup (peer);
out:
        return peerdup;
}

/**
 * _mnt3_get_host_from_peer -- Take a peer address and get an allocated
 *                             hostname. The hostname is the string on the
 *                             left side of the colon.
 *
 * @peer_addr: The peer address to get a hostname from
 *
 * @return: success: Allocated string containing the hostname
 *          failure: NULL
 *
 */
char *
_mnt3_get_host_from_peer (const char *peer_addr)
{
        char   *part       = NULL;
        size_t host_len    = 0;
        char   *colon      = NULL;

        colon = strrchr (peer_addr, ':');
        if (!colon) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_BAD_PEER,
                        "Bad peer %s", peer_addr);
                goto out;
        }

        host_len = colon - peer_addr;
        if (host_len < RPCSVC_PEER_STRLEN)
                part = gf_strndup (peer_addr, host_len);
        else
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_PEER_TOO_LONG,
                        "Peer too long %s", peer_addr);
out:
        return part;
}

/**
 * mnt3_check_cached_fh -- Check if FH is cached.
 *
 * Calls auxiliary functions based on whether we are checking
 * a write operation.
 *
 */
int
mnt3_check_cached_fh (struct mount3_state *ms, struct nfs3_fh *fh,
                      const char *host_addr, gf_boolean_t is_write_op)
{
        if (!is_write_op)
                return is_nfs_fh_cached (ms->authcache, fh, host_addr);

        return is_nfs_fh_cached_and_writeable (ms->authcache, fh, host_addr);
}

/**
 * _mnt3_authenticate_req -- Given an RPC request and a path OR a filehandle
 *                           check if the host is authorized to make the
 *                           request. Uses exports/netgroups auth model to
 *                           do this check.
 *
 * @ms  : The mount state
 * @req : The RPC request
 * @fh  : The NFS FH to authenticate (set when authenticating an FOP)
 * @path: The path to authenticate (set when authenticating a mount req)
 * @authorized_export: Allocate and fill this value when an export is authorized
 * @authorized_host: Allocate and fill this value when a host is authorized
 * @is_write_op: Is this a write op that we are authenticating?
 *
 * @return: 0 if authorized
 *          -EACCES for completely unauthorized fop
 *          -EROFS  for unauthorized write operations (rm, mkdir, write)
 */
int
_mnt3_authenticate_req (struct mount3_state *ms, rpcsvc_request_t *req,
                        struct nfs3_fh *fh, const char *path,
                        char **authorized_export, char **authorized_host,
                        gf_boolean_t is_write_op)
{
        char                    *peer_addr       = NULL;
        char                    *host_addr_ip    = NULL;
        char                    *host_addr_fqdn  = NULL;
        int                     auth_status_code = -EACCES;
        char                    *pathdup         = NULL;
        size_t                  dlen             = 0;
        char                    *auth_host       = NULL;
        gf_boolean_t            fh_cached        = _gf_false;
        struct export_item      *expitem         = NULL;

        GF_VALIDATE_OR_GOTO (GF_MNT, ms, out);
        GF_VALIDATE_OR_GOTO (GF_MNT, req, out);

        peer_addr    = _mnt3_get_peer_addr (req);
        host_addr_ip = _mnt3_get_host_from_peer (peer_addr);

        if (!host_addr_ip || !peer_addr)
                goto free_and_out;

        if (path) {
                /* Need to strip out trailing '/' */
                pathdup = strdupa (path);
                dlen = strlen (pathdup);
                if (dlen > 0 && pathdup[dlen-1] == '/')
                        pathdup[dlen-1] = '\0';
        }

        /* Check if the filehandle is cached */
        fh_cached = mnt3_check_cached_fh (ms, fh, host_addr_ip, is_write_op);
        if (fh_cached) {
                gf_msg_trace (GF_MNT, 0, "Found cached FH for %s",
                              host_addr_ip);
                auth_status_code = 0;
                goto free_and_out;
        }

        /* Check if the IP is authorized */
        auth_status_code = mnt3_auth_host (ms->auth_params, host_addr_ip,
                                           fh, pathdup, is_write_op, &expitem);

        gf_msg_debug (GF_MNT, 0, "access from IP %s is %s", host_addr_ip,
                      auth_status_code ? "denied" : "allowed");

        if (auth_status_code != 0) {
                /* If not, check if the FQDN is authorized */
                host_addr_fqdn = gf_rev_dns_lookup (host_addr_ip);
                auth_status_code = mnt3_auth_host (ms->auth_params,
                                                   host_addr_fqdn,
                                                   fh, pathdup, is_write_op,
                                                   &expitem);

                gf_msg_debug (GF_MNT, 0, "access from FQDN %s is %s",
                              host_addr_fqdn, auth_status_code ? "denied" :
                                                                 "allowed");

                if (auth_status_code == 0)
                        auth_host = host_addr_fqdn;
        } else
                auth_host = host_addr_ip;

        /* Skip the lines that set authorized export &
         * host if they are null.
         */
        if (!authorized_export || !authorized_host) {
                /* Cache the file handle if it was authorized */
                if (fh && auth_status_code == 0)
                        cache_nfs_fh (ms->authcache, fh, host_addr_ip, expitem);

                goto free_and_out;
        }

        if (!fh && auth_status_code == 0) {
                *authorized_export = gf_strdup (pathdup);
                if (!*authorized_export)
                        gf_msg (GF_MNT, GF_LOG_CRITICAL, ENOMEM,
                                NFS_MSG_NO_MEMORY,
                                "Allocation error when copying "
                                "authorized path");

                *authorized_host = gf_strdup (auth_host);
                if (!*authorized_host)
                        gf_msg (GF_MNT, GF_LOG_CRITICAL, ENOMEM,
                                NFS_MSG_NO_MEMORY,
                                "Allocation error when copying "
                                "authorized host");
        }

free_and_out:
        /* Free allocated strings after doing the auth */
        GF_FREE (peer_addr);
        GF_FREE (host_addr_fqdn);
        GF_FREE (host_addr_ip);
out:
        return auth_status_code;
}

/**
 * mnt3_authenticate_request -- Given an RPC request and a path, check if the
 *                              host is authorized to make the request. This
 *                              function calls _mnt3_authenticate_req_path ()
 *                              in a loop for the parent of each path while
 *                              the authentication check for that path is
 *                              failing.
 *
 * E.g. If the requested path is /patchy/L1, and /patchy is authorized, but
 * /patchy/L1 is not, it follows this code path :
 *
 * _mnt3_authenticate_req ("/patchy/L1") -> F
 * _mnt3_authenticate_req ("/patchy");   -> T
 * return T;
 *
 * @ms  : The mount state
 * @req : The RPC request
 * @path: The requested path
 * @authorized_path: This gets allocated and populated with the authorized path
 * @authorized_host: This gets allocated and populated with the authorized host
 * @return: 0 if authorized
 *          -EACCES for completely unauthorized fop
 *          -EROFS  for unauthorized write operations (rm, mkdir, write)
 */
int
mnt3_authenticate_request (struct mount3_state *ms, rpcsvc_request_t *req,
                           struct nfs3_fh *fh, const char *volname,
                           const char *path, char **authorized_path,
                           char **authorized_host, gf_boolean_t is_write_op)
{
        int          auth_status_code       = -EACCES;
        char         *parent_path = NULL;
        const char   *parent_old  = NULL;

        GF_VALIDATE_OR_GOTO (GF_MNT, ms, out);
        GF_VALIDATE_OR_GOTO (GF_MNT, req, out);

        /* If this option is not set, just allow it through */
        if (!ms->nfs->exports_auth) {
                /* This function is called in a variety of use-cases (mount
                 * + each fop) so path/authorized_path are not always present.
                 * For the cases which it _is_ present we need to populate the
                 * authorized_path. */
                if (path && authorized_path)
                        *authorized_path = gf_strdup (path);

                auth_status_code = 0;
                goto out;
        }

        /* First check if the path is allowed */
        auth_status_code = _mnt3_authenticate_req (ms, req, fh, path,
                                                   authorized_path,
                                                   authorized_host,
                                                   is_write_op);

        /* If the filehandle is set, just exit since we have to make only
         * one call to the function above
         */
        if (fh)
                goto out;

        parent_old = path;
        while (auth_status_code != 0) {
                /* Get the path's parent */
                parent_path = gf_resolve_path_parent (parent_old);
                if (!parent_path) /* Nothing left in the path to resolve */
                        goto out;

                /* Authenticate it */
                auth_status_code = _mnt3_authenticate_req (ms, req, fh,
                                                           parent_path,
                                                           authorized_path,
                                                           authorized_host,
                                                           is_write_op);

                parent_old = strdupa (parent_path); /* Copy the parent onto the
                                                     * stack.
                                                     */

                GF_FREE (parent_path); /* Free the allocated parent string */
        }

out:
        return auth_status_code;
}

int
mnt3svc_mnt (rpcsvc_request_t *req)
{
        struct iovec            pvec = {0, };
        char                    path[MNTPATHLEN];
        int                     ret = -1;
        struct mount3_state     *ms = NULL;
        mountstat3              mntstat = MNT3ERR_SERVERFAULT;
        struct mnt3_export      *exp = NULL;
        struct nfs_state        *nfs = NULL;
        int                     authcode = 0;

        if (!req)
                return -1;

        pvec.iov_base = path;
        pvec.iov_len = MNTPATHLEN;
        ret = xdr_to_mountpath (pvec, req->msg[0]);
        if (ret == -1) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_ARGS_DECODE_ERROR,
                        "Failed to decode args");
                rpcsvc_request_seterr (req, GARBAGE_ARGS);
                goto rpcerr;
        }

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND,
                        "Mount state not present");
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                ret = -1;
                goto rpcerr;
        }

        nfs = (struct nfs_state *)ms->nfsx->private;
        gf_msg_debug (GF_MNT, 0, "dirpath: %s", path);
        ret = mnt3_find_export (req, path, &exp);
        if (ret < 0) {
                mntstat = mnt3svc_errno_to_mnterr (-ret);
                goto mnterr;
        } else if (!exp) {
                /*
                 * SPECIAL CASE: exp is NULL if "path" is subdir in
                 * call to mnt3_find_export().
                 *
                 * This is subdir mount, we are already DONE!
                 * nfs_subvolume_started() and mnt3_check_client_net_tcp()
                 * validation are done in mnt3_parse_dir_exports()
                 * which is invoked through mnt3_find_export().
                 *
                 * TODO: All mount should happen thorugh mnt3svc_mount()
                 *       It needs more clean up.
                 */
                return (0);
        }

        if (!nfs_subvolume_started (nfs, exp->vol)) {
                gf_msg_debug (GF_MNT, 0, "Volume %s not started",
                              exp->vol->name);
                ret = -1;
                mntstat = MNT3ERR_NOENT;
                goto mnterr;
        }

        ret = mnt3_check_client_net_tcp (req, exp->vol->name);
        if (ret == RPCSVC_AUTH_REJECT) {
                mntstat = MNT3ERR_ACCES;
                gf_msg_debug (GF_MNT, 0, "Client mount not allowed");
                ret = -1;
                goto mnterr;
        }

        /* The second authentication check is the exports/netgroups
         * check.
         */
        authcode = mnt3_authenticate_request (ms, req, NULL, NULL, path, NULL,
                                              NULL, _gf_false);
        if (authcode != 0) {
                mntstat = MNT3ERR_ACCES;
                gf_msg_debug (GF_MNT, 0, "Client mount not allowed");
                ret = -1;
                goto mnterr;
        }

        ret = mnt3svc_mount (req, ms, exp);

        if (ret < 0)
                mntstat = mnt3svc_errno_to_mnterr (-ret);
mnterr:
        if (ret < 0) {
                mnt3svc_mnt_error_reply (req, mntstat);
                ret = 0;
        }

rpcerr:
        return ret;
}


int
mnt3svc_null (rpcsvc_request_t *req)
{
        struct iovec    dummyvec = {0, };

        if (!req) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
                        "Got NULL request!");
                return 0;
        }
        rpcsvc_submit_generic (req, &dummyvec, 1,  NULL, 0, NULL);
        return 0;
}


mountlist
__build_mountlist (struct mount3_state *ms, int *count)
{
        struct mountbody        *mlist = NULL;
        struct mountbody        *prev = NULL;
        struct mountbody        *first = NULL;
        size_t                  namelen = 0;
        int                     ret = -1;
        struct mountentry       *me = NULL;

        if ((!ms) || (!count))
                return NULL;

        /* read rmtab, other peers might have updated it */
        mount_read_rmtab(ms);

        *count = 0;
        gf_msg_debug (GF_MNT, 0, "Building mount list:");
        list_for_each_entry (me, &ms->mountlist, mlist) {
                namelen = strlen (me->exname);
                mlist = GF_CALLOC (1, sizeof (*mlist), gf_nfs_mt_mountbody);
                if (!mlist) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Memory allocation failed");
                        goto free_list;
                }
                if (!first)
                        first = mlist;

                mlist->ml_directory = GF_CALLOC (namelen + 2, sizeof (char),
                                                 gf_nfs_mt_char);
                if (!mlist->ml_directory) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Memory allocation failed");
                        goto free_list;
                }

                strcpy (mlist->ml_directory, me->exname);

                namelen = strlen (me->hostname);
                mlist->ml_hostname = GF_CALLOC (namelen + 2, sizeof (char),
                                                gf_nfs_mt_char);
                if (!mlist->ml_hostname) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Memory allocation failed");
                        goto free_list;
                }

                strcat (mlist->ml_hostname, me->hostname);

                gf_msg_debug (GF_MNT, 0, "mount entry: dir: %s, host: %s",
                              mlist->ml_directory, mlist->ml_hostname);
                if (prev) {
                        prev->ml_next = mlist;
                        prev = mlist;
                } else
                        prev = mlist;

                (*count)++;
        }

        ret = 0;

free_list:
        if (ret == -1) {
                xdr_free_mountlist (first);
                first = NULL;
        }

        return first;
}


mountlist
mnt3svc_build_mountlist (struct mount3_state *ms, int *count)
{
        struct mountbody        *first = NULL;

        LOCK (&ms->mountlock);
        {
                first = __build_mountlist (ms, count);
        }
        UNLOCK (&ms->mountlock);

        return first;
}


int
mnt3svc_dump (rpcsvc_request_t *req)
{
        int                     ret = -1;
        struct mount3_state     *ms = NULL;
        mountlist               mlist;
        mountstat3              mstat = 0;
        mnt3_serializer         sfunc = NULL;
        void                    *arg = NULL;


        if (!req)
                return -1;

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                goto rpcerr;
        }

        sfunc = (mnt3_serializer)xdr_serialize_mountlist;
        mlist = mnt3svc_build_mountlist (ms, &ret);
        arg = &mlist;

        if (!mlist) {
                if (ret != 0) {
                        rpcsvc_request_seterr (req, SYSTEM_ERR);
                        ret = -1;
                        goto rpcerr;
                } else {
                        arg = &mstat;
                        sfunc = (mnt3_serializer)xdr_serialize_mountstat3;
                }
        }

        mnt3svc_submit_reply (req, arg, sfunc);

        xdr_free_mountlist (mlist);
        ret = 0;

rpcerr:
        return ret;
}


int
mnt3svc_umount (struct mount3_state *ms, char *dirpath, char *hostname)
{
        struct mountentry       *me = NULL;
        int                     ret = -1;
        gf_store_handle_t       *sh = NULL;
        struct nfs_state        *nfs = NULL;
        gf_boolean_t            update_rmtab = _gf_false;

        if ((!ms) || (!dirpath) || (!hostname))
                return -1;

        nfs = (struct nfs_state *)ms->nfsx->private;

        update_rmtab = mount_open_rmtab (nfs->rmtab, &sh);
        if (update_rmtab) {
                ret = gf_store_lock (sh);
                if (ret)
                        goto out_free;
        }

        LOCK (&ms->mountlock);
        {
                if (update_rmtab)
                        __mount_read_rmtab (sh, &ms->mountlist, _gf_false);

                if (list_empty (&ms->mountlist)) {
                        ret = 0;
                        goto out_unlock;
                }

                ret = -1;
                list_for_each_entry (me, &ms->mountlist, mlist) {
                       if ((strcmp (me->exname, dirpath) == 0) &&
                            (strcmp (me->hostname, hostname) == 0)) {
                               ret = 0;
                               break;
                       }
                }

                /* Need this check here because at the end of the search me
                 * might still be pointing to the last entry, which may not be
                 * the one we're looking for.
                 */
                if (ret == -1)  {/* Not found in list. */
                        gf_msg_trace (GF_MNT, 0, "Export not found");
                        goto out_unlock;
                }

                if (!me)
                        goto out_unlock;

                gf_msg_debug (GF_MNT, 0, "Unmounting: dir %s, host: %s",
                              me->exname, me->hostname);

                list_del (&me->mlist);
                GF_FREE (me);

                if (update_rmtab)
                        __mount_rewrite_rmtab (ms, sh);
        }
out_unlock:
        UNLOCK (&ms->mountlock);

        if (update_rmtab)
                gf_store_unlock (sh);

out_free:
        if (update_rmtab)
                gf_store_handle_destroy (sh);

        return ret;
}


int
mnt3svc_umnt (rpcsvc_request_t *req)
{
        char                    hostname[MNTPATHLEN];
        char                    dirpath[MNTPATHLEN];
        struct iovec            pvec = {0, };
        int                     ret = -1;
        struct mount3_state     *ms = NULL;
        mountstat3              mstat = MNT3_OK;
        char                    *colon = NULL;

        if (!req)
                return -1;

        /* Remove the mount point from the exports list. */
        pvec.iov_base = dirpath;
        pvec.iov_len = MNTPATHLEN;
        ret = xdr_to_mountpath (pvec, req->msg[0]);
        if (ret == -1) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_ARGS_DECODE_ERROR,
                        "Failed decode args");
                rpcsvc_request_seterr (req, GARBAGE_ARGS);
                goto rpcerr;
        }

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND, "Mount state not present");
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                ret = -1;
                goto rpcerr;
        }

        ret = rpcsvc_transport_peername (req->trans, hostname, MNTPATHLEN);
        if (ret != 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOENT,
                        NFS_MSG_GET_REMOTE_NAME_FAIL,
                        "Failed to get remote name: %s", gai_strerror (ret));
                goto rpcerr;
        }

        colon = strrchr (hostname, ':');
        if (colon) {
                *colon= '\0';
        }
        gf_path_strip_trailing_slashes (dirpath);
        gf_msg_debug (GF_MNT, 0, "dirpath: %s, hostname: %s", dirpath,
                      hostname);
        ret = mnt3svc_umount (ms, dirpath, hostname);

        if (ret == -1) {
                ret = 0;
                mstat = MNT3ERR_NOENT;
        }
        /* FIXME: also take care of the corner case where the
         * client was resolvable at mount but not at the umount - vice-versa.
         */
        mnt3svc_submit_reply (req, &mstat,
                              (mnt3_serializer)xdr_serialize_mountstat3);

rpcerr:
        return ret;
}


int
__mnt3svc_umountall (struct mount3_state *ms)
{
        struct mountentry       *me = NULL;
        struct mountentry       *tmp = NULL;

        if (!ms)
                return -1;

        if (list_empty (&ms->mountlist))
                return 0;

        list_for_each_entry_safe (me, tmp, &ms->mountlist, mlist) {
                list_del (&me->mlist);       /* Remove from the mount list */
                __mountdict_remove (ms, me); /* Remove from the mount dict */
                GF_FREE (me);
        }

        return 0;
}


int
mnt3svc_umountall (struct mount3_state *ms)
{
        int     ret = -1;
        if (!ms)
                return -1;

        LOCK (&ms->mountlock);
        {
               ret = __mnt3svc_umountall (ms);
        }
        UNLOCK (&ms->mountlock);

        return ret;
}


int
mnt3svc_umntall (rpcsvc_request_t *req)
{
        int                     ret = RPCSVC_ACTOR_ERROR;
        struct mount3_state     *ms = NULL;
        mountstat3              mstat = MNT3_OK;

        if (!req)
                return ret;

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND, "Mount state not present");
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                goto rpcerr;
        }

        mnt3svc_umountall (ms);
        mnt3svc_submit_reply (req, &mstat,
                              (mnt3_serializer)xdr_serialize_mountstat3);

        ret = RPCSVC_ACTOR_SUCCESS;
rpcerr:
        return ret;
}


exports
mnt3_xlchildren_to_exports (rpcsvc_t *svc, struct mount3_state *ms)
{
        struct exportnode       *elist = NULL;
        struct exportnode       *prev = NULL;
        struct exportnode       *first = NULL;
        size_t                  namelen = 0;
        int                     ret = -1;
        char                    *addrstr = NULL;
        struct mnt3_export      *ent = NULL;
        struct nfs_state        *nfs = NULL;

        if ((!ms) || (!svc))
                return NULL;

        nfs = (struct nfs_state *)ms->nfsx->private;
        if (!nfs)
                return NULL;

        LOCK (&ms->mountlock);
        list_for_each_entry(ent, &ms->exportlist, explist) {

                /* If volume is not started yet, do not list it for tools like
                 * showmount.
                 */
                if (!nfs_subvolume_started (nfs, ent->vol))
                        continue;

                namelen = strlen (ent->expname) + 1;
                elist = GF_CALLOC (1, sizeof (*elist), gf_nfs_mt_exportnode);
                if (!elist) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Memory allocation failed");
                        goto free_list;
                }
                 if (!first)
                         first = elist;
                elist->ex_dir = GF_CALLOC (namelen + 2, sizeof (char),
                                           gf_nfs_mt_char);
                if (!elist->ex_dir) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY, "Memory allocation failed");
                        goto free_list;
                }
                strcpy (elist->ex_dir, ent->expname);

                addrstr = rpcsvc_volume_allowed (svc->options, ent->vol->name);
                if (addrstr) {
                        /* create a groupnode per allowed client */
                        char             *pos        = NULL;
                        char             *addr       = NULL;
                        char             *addrs      = NULL;
                        struct groupnode *group      = NULL;
                        struct groupnode *prev_group = NULL;

                        /* strtok_r() modifies the string, dup it */
                        addrs = gf_strdup (addrstr);
                        if (!addrs)
                                goto free_list;

                        while (1) {
                                /* only pass addrs on the 1st call */
                                addr = strtok_r (group ? NULL : addrs, ",",
                                                 &pos);
                                if (addr == NULL)
                                        /* no mode clients */
                                        break;

                                group = GF_CALLOC (1, sizeof (struct groupnode),
                                                   gf_nfs_mt_groupnode);
                                if (!group) {
                                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                                NFS_MSG_NO_MEMORY, "Memory "
                                                "allocation failed");
                                        GF_FREE (addrs);
                                        goto free_list;
                                }

                                group->gr_name = gf_strdup (addr);
                                if (!group->gr_name) {
                                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                                NFS_MSG_NO_MEMORY, "Memory "
                                                "allocation failed");
                                        GF_FREE (group);
                                        GF_FREE (addrs);
                                        goto free_list;
                                }

                                /* chain the groups together */
                                if (!elist->ex_groups)
                                        elist->ex_groups = group;
                                else
                                        prev_group->gr_next = group;
                                prev_group = group;
                        }

                        GF_FREE (addrs);
                } else {
                        elist->ex_groups = GF_CALLOC (1,
                                                      sizeof (struct groupnode),
                                                      gf_nfs_mt_groupnode);
                        if (!elist->ex_groups) {
                                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                        NFS_MSG_NO_MEMORY, "Memory allocation "
                                        "failed");
                                goto free_list;
                        }

                        addrstr = gf_strdup ("No Access");
                        if (!addrstr)
                                goto free_list;

                        elist->ex_groups->gr_name = addrstr;
                }

                if (prev) {
                        prev->ex_next = elist;
                        prev = elist;
                } else
                        prev = elist;
        }

        ret = 0;

free_list:
        UNLOCK (&ms->mountlock);
        if (ret == -1) {
                xdr_free_exports_list (first);
                first = NULL;
        }

        return first;
}


int
mnt3svc_export (rpcsvc_request_t *req)
{
        struct mount3_state     *ms = NULL;
        exports                 elist = NULL;
        int                     ret = -1;

        if (!req)
                return -1;

        ms = (struct mount3_state *)rpcsvc_request_program_private (req);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_NOT_FOUND, "mount state not found");
                rpcsvc_request_seterr (req, SYSTEM_ERR);
                goto err;
        }

        /* Using the children translator names, build the export list */
        elist = mnt3_xlchildren_to_exports (rpcsvc_request_service (req),
                                            ms);
        /* Do not return error when exports list is empty. An exports list can
         * be empty when no subvolumes have come up. No point returning error
         * and confusing the user.
        if (!elist) {
                gf_log (GF_MNT, GF_LOG_ERROR, "Failed to build exports list");
                nfs_rpcsvc_request_seterr (req, SYSTEM_ERR);
                goto err;
        }
        */

        /* Note how the serializer is passed to the generic reply function. */
        mnt3svc_submit_reply (req, &elist,
                              (mnt3_serializer)xdr_serialize_exports);

        xdr_free_exports_list (elist);
        ret = 0;
err:
        return ret;
}


/*
 * __mnt3udp_get_mstate() Fetches mount3_state from xlator
 * Linkage: Static
 * Usage: Used only for UDP MOUNT codepath
 */
static struct mount3_state *
__mnt3udp_get_mstate (xlator_t *nfsx)
{
        struct nfs_state        *nfs = NULL;
        struct mount3_state     *ms = NULL;

        if (nfsx == NULL)
                return NULL;

        nfs = (struct nfs_state *)nfsx->private;
        if (nfs == NULL)
                return NULL;

        ms = (struct mount3_state *)nfs->mstate;
        return ms;
}

extern int
glfs_resolve_at (struct glfs *, xlator_t *, inode_t *,
                 const char *, loc_t *, struct iatt *, int, int);

extern struct glfs *
glfs_new_from_ctx (glusterfs_ctx_t *);

extern void
glfs_free_from_ctx (struct glfs *);

static inode_t *
__mnt3udp_get_export_subdir_inode (struct svc_req *req, char *subdir,
                                   char *expname, /* OUT */
                                   struct mnt3_export *exp)
{
        inode_t                 *inode = NULL;
        loc_t                   loc = {0, };
        struct iatt             buf = {0, };
        int                     ret = -1;
        glfs_t                  *fs = NULL;

        if ((!req) || (!subdir) || (!expname) || (!exp))
                return NULL;

        /* AUTH check for subdir i.e. nfs.export-dir */
        if (exp->hostspec) {
                struct sockaddr_in *sin = NULL;

#if !defined(_TIRPC_SVC_H)
                sin = svc_getcaller (req->rq_xprt);
#else
                sin = (struct sockaddr_in *)svc_getcaller (req->rq_xprt);
                /* TIRPC's svc_getcaller() returns a pointer to a
                 * sockaddr_in6, even though it might actually be an
                 * IPv4 address. It ought return a struct sockaddr and
                 * make the caller upcast it to the proper address family.
                 */
#endif
                /* And let's make sure that it's actually an IPv4 address. */
                GF_ASSERT (sin->sin_family == AF_INET);

                ret = mnt3_verify_auth (sin, exp);
                if (ret) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, EACCES,
                                NFS_MSG_AUTH_VERIFY_FAILED,
                                "AUTH(nfs.export-dir) verification failed");
                        errno = EACCES;
                        return NULL;
                }
        }

        /*
         * IMP: glfs_t fs object is not used by glfs_resolve_at (). The main
         * purpose is to not change the ABI of glfs_resolve_at () and not to
         * pass a NULL object.
         *
         * TODO: Instead of linking against libgfapi.so, just for one API
         * i.e. glfs_resolve_at(), It would be cleaner if PATH name to
         * inode resolution code can be moved to libglusterfs.so or so.
         * refer bugzilla for more details :
         * https://bugzilla.redhat.com/show_bug.cgi?id=1161573
         */
        fs = glfs_new_from_ctx (exp->vol->ctx);
        if (!fs)
                return NULL;

        ret = glfs_resolve_at (fs, exp->vol, NULL, subdir,
                               &loc, &buf, 1 /* Follow link */,
                               0 /* Hard lookup */);

        glfs_free_from_ctx (fs);

        if (ret != 0) {
                loc_wipe (&loc);
                return NULL;
        }

        inode = inode_ref (loc.inode);
        snprintf (expname, PATH_MAX, "/%s%s", exp->vol->name, loc.path);

        loc_wipe (&loc);

        return inode;
}

static inode_t *
__mnt3udp_get_export_volume_inode (struct svc_req *req, char *volpath,
                                   char *expname, /* OUT */
                                   struct mnt3_export *exp)
{
        char                    *rpath = NULL;
        inode_t                 *inode = NULL;

        if ((!req) || (!volpath) || (!expname) || (!exp))
                return NULL;

        rpath = strchr (volpath, '/');
        if (rpath == NULL)
                rpath = "/";

        inode = inode_from_path (exp->vol->itable, rpath);
        snprintf (expname, PATH_MAX, "/%s", exp->vol->name);

        return inode;
}

/*
 * nfs3_rootfh() is used for NFS MOUNT over UDP i.e. mountudpproc3_mnt_3_svc().
 * Especially in mount3udp_thread() THREAD. Gluster NFS starts this thread
 * when nfs.mount-udp is ENABLED (set to TRUE/ON).
 */
struct nfs3_fh *
nfs3_rootfh (struct svc_req *req, xlator_t *nfsx,
             char *path, char *expname /* OUT */)
{
        struct nfs3_fh          *fh = NULL;
        inode_t                 *inode = NULL;
        struct mnt3_export      *exp = NULL;
        struct mount3_state     *ms = NULL;
        struct nfs_state        *nfs = NULL;
        int                     mnt3type = MNT3_EXPTYPE_DIR;
        int                     ret = RPCSVC_AUTH_REJECT;

        if ((!req) || (!nfsx) || (!path) || (!expname)) {
                errno = EFAULT;
                return NULL;
        }

        /*
         * 1. First check if the MOUNT is for whole volume.
         *      i.e. __mnt3udp_get_export_volume_inode ()
         * 2. If NOT, then TRY for SUBDIR MOUNT.
         *      i.e. __mnt3udp_get_export_subdir_inode ()
         * 3. If a subdir is exported using nfs.export-dir,
         *      then the mount type would be MNT3_EXPTYPE_DIR,
         *      so make sure to find the proper path to be
         *      resolved using mnt3_get_volume_subdir()
         * 3. Make sure subdir export is allowed.
         */
        ms = __mnt3udp_get_mstate(nfsx);
        if (!ms) {
                errno = EFAULT;
                return NULL;
        }

        exp = mnt3_mntpath_to_export (ms, path , _gf_false);
        if (exp != NULL)
                mnt3type = exp->exptype;

        if (mnt3type == MNT3_EXPTYPE_DIR) {
                char    volname [MNTPATHLEN] = {0, };
                char    *volptr = volname;

                /* Subdir export (nfs3.export-dirs) check */
                if (!gf_mnt3_export_dirs(ms)) {
                        errno = EACCES;
                        return NULL;
                }

                path = mnt3_get_volume_subdir (path, &volptr);
                if (exp == NULL)
                        exp = mnt3_mntpath_to_export (ms, volname , _gf_false);
        }

        if (exp == NULL) {
                errno = ENOENT;
                return NULL;
        }

        nfs = (struct nfs_state *)nfsx->private;
        if (!nfs_subvolume_started (nfs, exp->vol)) {
                errno = ENOENT;
                return NULL;
        }

        /* AUTH check: respect nfs.rpc-auth-allow/reject */
        ret = mnt3_check_client_net_udp (req, exp->vol->name, nfsx);
        if (ret == RPCSVC_AUTH_REJECT) {
                errno = EACCES;
                return NULL;
        }

        switch (mnt3type) {

        case MNT3_EXPTYPE_VOLUME:
                inode = __mnt3udp_get_export_volume_inode (req, path,
                                                           expname, exp);
                break;

        case MNT3_EXPTYPE_DIR:
                inode = __mnt3udp_get_export_subdir_inode (req, path,
                                                           expname, exp);
                break;

        default:
                /* Never reachable */
                gf_msg (GF_MNT, GF_LOG_ERROR, EFAULT, NFS_MSG_UNKNOWN_MNT_TYPE,
                        "Unknown MOUNT3 type");
                errno = EFAULT;
                goto err;
        }

        if (inode == NULL) {
                /* Don't over-write errno */
                if (!errno)
                        errno = ENOENT;
                goto err;
        }

        /* Build the inode from FH */
        fh = GF_CALLOC (1, sizeof(*fh), gf_nfs_mt_nfs3_fh);
        if (fh == NULL) {
                errno = ENOMEM;
                goto err;
        }

        (void) nfs3_build_fh (inode, exp->volumeid, fh);

err:
        if (inode)
                inode_unref (inode);

        return fh;
}

int
mount3udp_add_mountlist (xlator_t *nfsx, char *host, char *export)
{
        struct mountentry       *me = NULL;
        struct mount3_state     *ms = NULL;

        if ((!host) || (!export) || (!nfsx))
                return -1;

        ms = __mnt3udp_get_mstate (nfsx);
        if (!ms)
                return -1;

        me = GF_CALLOC (1, sizeof (*me), gf_nfs_mt_mountentry);
        if (!me)
                return -1;

        strncpy (me->exname, export, MNTPATHLEN);
        strncpy (me->hostname, host, MNTPATHLEN);
        INIT_LIST_HEAD (&me->mlist);
        LOCK (&ms->mountlock);
        {
                list_add_tail (&me->mlist, &ms->mountlist);
                mount_rewrite_rmtab(ms, NULL);
        }
        UNLOCK (&ms->mountlock);
        return 0;
}

int
mount3udp_delete_mountlist (xlator_t *nfsx, char *hostname, char *export)
{
        struct mount3_state     *ms = NULL;

        if ((!hostname) || (!export) || (!nfsx))
                return -1;

        ms = __mnt3udp_get_mstate (nfsx);
        if (!ms)
                return -1;

        mnt3svc_umount (ms, export, hostname);
        return 0;
}

/**
 * This function will parse the hostip (IP address, IP range, or hostname)
 * and fill the host_auth_spec structure.
 *
 * @param hostspec - struct host_auth_spec
 * @param hostip   - IP address, IP range (CIDR format) or hostname
 *
 * @return 0 - on success and -1 on failure
 *
 * NB: This does not support IPv6 currently.
 */
int
mnt3_export_fill_hostspec (struct host_auth_spec* hostspec, const char* hostip)
{
        char     *ipdupstr = NULL;
        char     *savptr = NULL;
        char     *endptr = NULL;
        char     *ip = NULL;
        char     *token = NULL;
        int      ret = -1;
        long     prefixlen = IPv4_ADDR_SIZE; /* default */
        uint32_t shiftbits = 0;
        size_t   length = 0;

        /* Create copy of the string so that the source won't change
         */
        ipdupstr = gf_strdup (hostip);
        if (NULL == ipdupstr) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                goto err;
        }

        ip = strtok_r (ipdupstr, "/", &savptr);
        /* Validate the Hostname or IPv4 address
         * TODO: IPv6 support for subdir auth.
         */
        length = strlen (ip);
        if ((!valid_ipv4_address (ip, (int)length, _gf_false)) &&
            (!valid_host_name (ip, (int)length))) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL, NFS_MSG_INVALID_ENTRY,
                        "Invalid hostname or IPv4 address: %s", ip);
                goto err;
        }

        hostspec->host_addr = gf_strdup (ip);
        if (NULL == hostspec->host_addr) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                goto err;
        }

        /**
         * User provided CIDR address (xx.xx.xx.xx/n format) is split
         * into HOST (IP addr or hostname) and network prefix(n) from
         * which netmask would be calculated. This CIDR address may
         * denote a single, distinct interface address or the beginning
         * address of an entire network.
         *
         * e.g. the IPv4 block 192.168.100.0/24 represents the 256
         * IPv4 addresses from 192.168.100.0 to 192.168.100.255.
         * Therefore to check if an IP matches 192.168.100.0/24
         * we should mask the IP with FFFFFF00 and compare it with
         * host address part of CIDR.
         *
         * Refer: mask_match() in common-utils.c.
         */
        token = strtok_r (NULL, "/", &savptr);
        if (token != NULL) {
              prefixlen = strtol (token, &endptr, 10);
              if ((errno != 0) || (*endptr != '\0') ||
                  (prefixlen < 0) || (prefixlen > IPv4_ADDR_SIZE)) {
                        gf_msg (THIS->name, GF_LOG_WARNING, EINVAL,
                                NFS_MSG_INVALID_ENTRY,
                                "Invalid IPv4 subnetwork mask");
                      goto err;
              }
        }

        /*
         * 1. Calculate the network mask address.
         * 2. Convert it into Big-Endian format.
         * 3. Store it in hostspec netmask.
         */
        shiftbits = IPv4_ADDR_SIZE - prefixlen;
        hostspec->netmask = htonl ((uint32_t)~0 << shiftbits);

        ret = 0; /* SUCCESS */
err:
        if (NULL != ipdupstr) {
                GF_FREE (ipdupstr);
        }
        return ret;
}


/**
 * This function will parse the AUTH parameter passed along with
 * "export-dir" option. If AUTH parameter is present then it will be
 * stripped from exportpath and stored in mnt3_export (exp) structure.
 *
 * @param exp - mnt3_export structure. Holds information needed for mount.
 * @param exportpath - Value of "export-dir" key. Holds both export path
 *                     and AUTH parameter for the path.
 *                     exportpath format: <abspath>[(hostdesc[|hostspec|...])]
 *
 * @return This function will return 0 on success and -1 on failure.
 */
int
mnt3_export_parse_auth_param (struct mnt3_export* exp, char* exportpath)
{
        char *token = NULL;
        char *savPtr = NULL;
        char *hostip = NULL;
        struct host_auth_spec *host = NULL;
        int ret = 0;

        /* Using exportpath directly in strtok_r because we want
         * to strip off AUTH parameter from exportpath. */
        token = strtok_r (exportpath, "(", &savPtr);

        /* Get the next token, which will be the AUTH parameter. */
        token = strtok_r (NULL, ")", &savPtr);

        if (NULL == token) {
                /* If AUTH is not present then we should return success. */
                return 0;
        }

        /* Free any previously allocated hostspec structure. */
        if (NULL != exp->hostspec) {
                GF_FREE (exp->hostspec);
                exp->hostspec = NULL;
        }

        exp->hostspec = GF_CALLOC (1,
                        sizeof (*(exp->hostspec)),
                        gf_nfs_mt_auth_spec);
        if (NULL == exp->hostspec){
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                return -1;
        }

        /* AUTH parameter can have multiple entries. For each entry
         * a host_auth_spec structure is created. */
        host = exp->hostspec;

        hostip = strtok_r (token, "|", &savPtr);

        /* Parse all AUTH parameters separated by '|' */
        while (NULL != hostip){
                ret = mnt3_export_fill_hostspec (host, hostip);
                if (0 != ret) {
                        gf_msg (GF_MNT, GF_LOG_WARNING, 0,
                                NFS_MSG_PARSE_HOSTSPEC_FAIL,
                                "Failed to parse hostspec: %s", hostip);
                        goto err;
                }

                hostip = strtok_r (NULL, "|", &savPtr);
                if (NULL == hostip) {
                        break;
                }

                host->next = GF_CALLOC (1, sizeof (*(host)),
                                gf_nfs_mt_auth_spec);
                if (NULL == host->next){
                        gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM,
                                NFS_MSG_NO_MEMORY,
                                "Memory allocation failed");
                        goto err;
                }
                host = host->next;
        }

        /* In case of success return from here */
        return 0;
err:
        /* In case of failure free up hostspec structure.  */
        FREE_HOSTSPEC (exp);

        return -1;
}

/**
 * exportpath will also have AUTH options (ip address, subnet address or
 * hostname) mentioned.
 * exportpath format: <abspath>[(hostdesc[|hostspec|...])]
 */
struct mnt3_export *
mnt3_init_export_ent (struct mount3_state *ms, xlator_t *xl, char *exportpath,
                      uuid_t volumeid)
{
        struct mnt3_export      *exp = NULL;
        int                     alloclen = 0;
        int                     ret = -1;

        if ((!ms) || (!xl))
                return NULL;

        exp = GF_CALLOC (1, sizeof (*exp), gf_nfs_mt_mnt3_export);
        if (!exp) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                return NULL;
        }

        if (NULL != exportpath) {
                /* If exportpath is not NULL then we should check if AUTH
                 * parameter is present or not. If AUTH parameter is present
                 * then it will be stripped and stored in mnt3_export (exp)
                 * structure.
                 */
                if (0 != mnt3_export_parse_auth_param (exp, exportpath)){
                        gf_msg (GF_MNT, GF_LOG_ERROR, 0,
                                NFS_MSG_PARSE_AUTH_PARAM_FAIL,
                                "Failed to parse auth param");
                        goto err;
                }
        }


        INIT_LIST_HEAD (&exp->explist);
        if (exportpath)
                alloclen = strlen (xl->name) + 2 + strlen (exportpath);
        else
                alloclen = strlen (xl->name) + 2;

        exp->expname = GF_CALLOC (alloclen, sizeof (char), gf_nfs_mt_char);
        if (!exp->expname) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                goto err;
        }

        if (exportpath) {
                gf_msg_trace (GF_MNT, 0, "Initing dir export: %s:%s",
                              xl->name, exportpath);
                exp->exptype = MNT3_EXPTYPE_DIR;
                ret = snprintf (exp->expname, alloclen, "/%s%s", xl->name,
                                exportpath);
        } else {
                gf_msg_trace (GF_MNT, 0, "Initing volume export: %s",
                              xl->name);
                exp->exptype = MNT3_EXPTYPE_VOLUME;
                ret = snprintf (exp->expname, alloclen, "/%s", xl->name);
        }
        if (ret < 0) {
                gf_msg (xl->name, GF_LOG_ERROR, ret, NFS_MSG_SET_EXP_FAIL,
                        "Failed to set the export name");
                goto err;
        }
        /* Just copy without discrimination, we'll determine whether to
         * actually use it when a mount request comes in and a file handle
         * needs to be built.
         */
        gf_uuid_copy (exp->volumeid, volumeid);
        exp->vol = xl;

        /* On success we should return from here*/
        return exp;
err:
        /* On failure free exp and it's members.*/
        if (NULL != exp) {
                mnt3_export_free (exp);
                exp = NULL;
        }

        return exp;
}


int
__mnt3_init_volume_direxports (struct mount3_state *ms, xlator_t *xlator,
                               char *optstr, uuid_t volumeid)
{
        struct mnt3_export      *newexp = NULL;
        int                     ret = -1;
        char                    *savptr = NULL;
        char                    *dupopt = NULL;
        char                    *token = NULL;

        if ((!ms) || (!xlator) || (!optstr))
                return -1;

        dupopt = strdupa (optstr);

        token = strtok_r (dupopt, ",", &savptr);
        while (token) {
                newexp = mnt3_init_export_ent (ms, xlator, token, volumeid);
                if (!newexp) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, 0,
                                NFS_MSG_INIT_DIR_EXP_FAIL, "Failed to init dir "
                                "export: %s", token);
                        ret = -1;
                        goto err;
                }

                list_add_tail (&newexp->explist, &ms->exportlist);
                token = strtok_r (NULL, ",", &savptr);
        }

        ret = 0;
err:
        return ret;
}


int
__mnt3_init_volume (struct mount3_state *ms, dict_t *opts, xlator_t *xlator)
{
        struct mnt3_export      *newexp = NULL;
        int                     ret = -1;
        char                    searchstr[1024];
        char                    *optstr  = NULL;
        uuid_t                  volumeid = {0, };

        if ((!ms) || (!xlator) || (!opts))
                return -1;

        gf_uuid_clear (volumeid);
        if (gf_nfs_dvm_off (nfs_state (ms->nfsx)))
                goto no_dvm;

        ret = snprintf (searchstr, 1024, "nfs3.%s.volume-id", xlator->name);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_SNPRINTF_FAIL,
                        "snprintf failed");
                ret = -1;
                goto err;
        }

        if (dict_get (opts, searchstr)) {
                ret = dict_get_str (opts, searchstr, &optstr);
                if (ret < 0) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                                NFS_MSG_DICT_GET_FAILED, "Failed to read "
                                "option: %s", searchstr);
                        ret = -1;
                        goto err;
                }
        } else {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_VOLID_MISSING,
                        "DVM is on but volume-id not "
                        "given for volume: %s", xlator->name);
                ret = -1;
                goto err;
        }

        if (optstr) {
                ret = gf_uuid_parse (optstr, volumeid);
                if (ret < 0) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                                NFS_MSG_PARSE_VOL_UUID_FAIL, "Failed to parse "
                                "volume UUID");
                        ret = -1;
                        goto err;
                }
        }

no_dvm:
        ret = snprintf (searchstr, 1024, "nfs3.%s.export-dir", xlator->name);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_SNPRINTF_FAIL,
                        "snprintf failed");
                ret = -1;
                goto err;
        }

        if (dict_get (opts, searchstr)) {
                ret = dict_get_str (opts, searchstr, &optstr);
                if (ret < 0) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                                NFS_MSG_DICT_GET_FAILED, "Failed to read "
                                "option: %s", searchstr);
                        ret = -1;
                        goto err;
                }

                ret = __mnt3_init_volume_direxports (ms, xlator, optstr,
                                                     volumeid);
                if (ret == -1) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, 0,
                                NFS_MSG_DIR_EXP_SETUP_FAIL, "Dir export "
                                "setup failed for volume: %s", xlator->name);
                        goto err;
                }
        }

        if (ms->export_volumes) {
                newexp = mnt3_init_export_ent (ms, xlator, NULL, volumeid);
                if (!newexp) {
                        ret = -1;
                        goto err;
                }

                list_add_tail (&newexp->explist, &ms->exportlist);
        }
        ret = 0;


err:
        return ret;
}


int
__mnt3_init_volume_export (struct mount3_state *ms, dict_t *opts)
{
        int                     ret = -1;
        char                    *optstr  = NULL;
        /* On by default. */
        gf_boolean_t            boolt = _gf_true;

        if ((!ms) || (!opts))
                return -1;

        if (!dict_get (opts, "nfs3.export-volumes")) {
                ret = 0;
                goto err;
        }

        ret = dict_get_str (opts, "nfs3.export-volumes", &optstr);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_DICT_GET_FAILED,
                        "Failed to read option: nfs3.export-volumes");
                ret = -1;
                goto err;
        }

        ret = gf_string2boolean (optstr, &boolt);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_STR2BOOL_FAIL,
                        "Failed to convert string to boolean");
        }

err:
        if (boolt == _gf_false) {
                gf_msg_trace (GF_MNT, 0, "Volume exports disabled");
                ms->export_volumes = 0;
        } else {
                gf_msg_trace (GF_MNT, 0, "Volume exports enabled");
                ms->export_volumes = 1;
        }

        return ret;
}


int
__mnt3_init_dir_export (struct mount3_state *ms, dict_t *opts)
{
        int                     ret = -1;
        char                    *optstr  = NULL;
        /* On by default. */
        gf_boolean_t            boolt = _gf_true;

        if ((!ms) || (!opts))
                return -1;

        if (!dict_get (opts, "nfs3.export-dirs")) {
                ret = 0;
                goto err;
        }

        ret = dict_get_str (opts, "nfs3.export-dirs", &optstr);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_DICT_GET_FAILED,
                        "Failed to read option: nfs3.export-dirs");
                ret = -1;
                goto err;
        }

        ret = gf_string2boolean (optstr, &boolt);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_STR2BOOL_FAIL,
                        "Failed to convert string to boolean");
         }

err:
        if (boolt == _gf_false) {
                gf_msg_trace (GF_MNT, 0, "Dir exports disabled");
                ms->export_dirs = 0;
        } else {
                gf_msg_trace (GF_MNT, 0, "Dir exports enabled");
                ms->export_dirs = 1;
        }

        return ret;
}


int
mnt3_init_options (struct mount3_state *ms, dict_t *options)
{
        xlator_list_t   *volentry = NULL;
        int             ret = -1;

        if ((!ms) || (!options))
                return -1;

        __mnt3_init_volume_export (ms, options);
        __mnt3_init_dir_export (ms, options);
        volentry = ms->nfsx->children;
        while (volentry) {
                gf_msg_trace (GF_MNT, 0, "Initing options for: %s",
                              volentry->xlator->name);
                ret = __mnt3_init_volume (ms, options, volentry->xlator);
                if (ret < 0) {
                        gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                                NFS_MSG_VOL_INIT_FAIL,
                                "Volume init failed");
                        goto err;
                }

                volentry = volentry->next;
        }


        ret = 0;
err:
        return ret;
}

struct mount3_state *
mnt3_init_state (xlator_t *nfsx)
{
        struct mount3_state     *ms = NULL;
        int                     ret = -1;

        if (!nfsx)
                return NULL;

        ms = GF_CALLOC (1, sizeof (*ms), gf_nfs_mt_mount3_state);
        if (!ms) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Memory allocation failed");
                return NULL;
        }

        ms->iobpool = nfsx->ctx->iobuf_pool;
        ms->nfsx = nfsx;
        INIT_LIST_HEAD (&ms->exportlist);
        ret = mnt3_init_options (ms, nfsx->options);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_OPT_INIT_FAIL,
                        "Options init failed");
                return NULL;
        }

        INIT_LIST_HEAD (&ms->mountlist);
        LOCK_INIT (&ms->mountlock);

        return ms;
}

int
mount_init_state (xlator_t *nfsx)
{
        int              ret = -1;
        struct nfs_state *nfs = NULL;

        if (!nfsx)
                goto out;

        nfs = (struct nfs_state *)nfs_state (nfsx);
        /*Maintaining global state for MOUNT1 and MOUNT3*/
        nfs->mstate =  mnt3_init_state (nfsx);
        if (!nfs->mstate) {
                gf_msg (GF_NFS, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to allocate mount state");
                goto out;
        }
        ret = 0;
out:
        return ret;
}

rpcsvc_actor_t  mnt3svc_actors[MOUNT3_PROC_COUNT] = {
        {"NULL",    MOUNT3_NULL,    mnt3svc_null,    NULL, 0, DRC_NA},
        {"MNT",     MOUNT3_MNT,     mnt3svc_mnt,     NULL, 0, DRC_NA},
        {"DUMP",    MOUNT3_DUMP,    mnt3svc_dump,    NULL, 0, DRC_NA},
        {"UMNT",    MOUNT3_UMNT,    mnt3svc_umnt,    NULL, 0, DRC_NA},
        {"UMNTALL", MOUNT3_UMNTALL, mnt3svc_umntall, NULL, 0, DRC_NA},
        {"EXPORT",  MOUNT3_EXPORT,  mnt3svc_export,  NULL, 0, DRC_NA}
};



/* Static init parts are assigned here, dynamic ones are done in
 * mnt3svc_init and mnt3_init_state.
 * Making MOUNT3 a synctask so that the blocking DNS calls during rpc auth
 * gets offloaded to syncenv, keeping the main/poll thread unblocked
 */
rpcsvc_program_t        mnt3prog = {
                        .progname       = "MOUNT3",
                        .prognum        = MOUNT_PROGRAM,
                        .progver        = MOUNT_V3,
                        .progport       = GF_MOUNTV3_PORT,
                        .actors         = mnt3svc_actors,
                        .numactors      = MOUNT3_PROC_COUNT,
                        .min_auth       = AUTH_NULL,
                        .synctask       = _gf_true,
};

/**
 * __mnt3_mounted_exports_walk -- Walk through the mounted export directories
 *                                and unmount the directories that are no
 *                                longer authorized to be mounted.
 * @dict: The dict to walk
 * @key : The key we are on
 * @val : The value associated with that key
 * @tmp : Additional params (pointer to an auth params struct passed here)
 *
 */
int
__mnt3_mounted_exports_walk (dict_t *dict, char *key, data_t *val, void *tmp)
{
        char                     *path             = NULL;
        char                     *host_addr_ip     = NULL;
        char                     *host_addr_fqdn   = NULL;
        char                     *keydup           = NULL;
        char                     *colon            = NULL;
        struct mnt3_auth_params  *auth_params      = NULL;
        int                       ret              = 0;
        int                       auth_status_code = 0;

        gf_msg_trace (GF_MNT, 0, "Checking if key %s is authorized.", key);

        auth_params = (struct mnt3_auth_params *)tmp;

        /* Since we haven't obtained a lock around the mount dict
         * here, we want to duplicate the key and then process it.
         * Otherwise we would potentially have a race condition
         * by modifying the key in the dict when other threads
         * are accessing it.
         */
        keydup = strdupa (key);

        colon = strchr (keydup, ':');
        if (!colon)
                return 0;

        *colon = '\0';

        path = alloca (strlen (keydup) + 2);
        snprintf (path, strlen (keydup) + 2, "/%s", keydup);

        /* Host is one character after ':' */
        host_addr_ip = colon + 1;

        /* Check if the IP is authorized */
        auth_status_code = mnt3_auth_host (auth_params, host_addr_ip,
                                           NULL, path, FALSE, NULL);
        if (auth_status_code == 0) {
                goto out;
        }

        ret = gf_get_hostname_from_ip (host_addr_ip, &host_addr_fqdn);
        if (ret != 0) {
                gf_msg (GF_MNT, GF_LOG_DEBUG, 0, NFS_MSG_AUTH_ERROR ,
                        "Authorization failed for IP [%s], but name "
                        "resolution also failed!", host_addr_ip);
                goto unmount;
        }

        /* If not, check if the FQDN is authorized */
        gf_msg (GF_MNT, GF_LOG_DEBUG, 0, NFS_MSG_AUTH_ERROR,
                "Authorization failed for IP [%s], attempting to"
                " auth hostname [%s]...", host_addr_ip, host_addr_fqdn);

        auth_status_code = mnt3_auth_host (auth_params, host_addr_fqdn,
                                           NULL, path, FALSE, NULL);
        if (auth_status_code == 0) {
                gf_msg (GF_MNT, GF_LOG_DEBUG, 0, NFS_MSG_AUTH_ERROR,
                        "Authorization succeeded for "
                        "Client [IP=%s, Hostname=%s].",
                        host_addr_ip, host_addr_fqdn);
                goto out;
        }

unmount:
         gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_AUTH_ERROR,
                 "Client [IP=%s, Hostname=%s] not authorized for this mount. "
                 "Unmounting!", host_addr_ip, host_addr_fqdn);
         mnt3svc_umount (auth_params->ms, path, host_addr_ip);
out:
        GF_FREE (host_addr_fqdn);
        return 0;
}

/**
 * _mnt3_invalidate_old_mounts -- Calls __mnt3_mounted_exports_walk which checks
 *                                checks if hosts are authorized to be mounted
 *                                and umounts them.
 *
 * @ms: The mountstate for this service that holds all the information we need
 *
 */
void
_mnt3_invalidate_old_mounts (struct mount3_state *ms)
{
        gf_msg_debug (GF_MNT, 0, "Invalidating old mounts ...");
        dict_foreach (ms->mountdict, __mnt3_mounted_exports_walk,
                      ms->auth_params);
}


/**
 * _mnt3_has_file_changed -- Checks if a file has changed on disk
 *
 * @path: The path of the file on disk
 * @oldmtime: The previous mtime of the file
 *
 * @return: file changed: TRUE
 *          otherwise   : FALSE
 *
 * Uses get_file_mtime () in common-utils.c
 */
gf_boolean_t
_mnt3_has_file_changed (const char *path, time_t *oldmtime)
{
        gf_boolean_t    changed = _gf_false;
        time_t          mtime   = {0};
        int             ret     = 0;

        GF_VALIDATE_OR_GOTO (GF_MNT, path, out);
        GF_VALIDATE_OR_GOTO (GF_MNT, oldmtime, out);

        ret = get_file_mtime (path, &mtime);
        if (ret < 0)
                goto out;

        if (mtime != *oldmtime) {
                changed = _gf_true;
                *oldmtime = mtime;
        }
out:
        return changed;
}

/**
 * _mnt_auth_param_refresh_thread - Started using pthread_create () in
 *                                  mnt3svc_init (). Reloads exports/netgroups
 *                                  files from disk and sets the auth params
 *                                  structure in the mount state to reflect
 *                                  any changes from disk.
 * @argv: Unused argument
 * @return: Always returns NULL
 */
void *
_mnt3_auth_param_refresh_thread (void *argv)
{
        struct          mount3_state *mstate    = (struct mount3_state *)argv;
        char            *exp_file_path          = NULL;
        char            *ng_file_path           = NULL;
        size_t          nbytes                  = 0;
        time_t          exp_time                = 0;
        time_t          ng_time                 = 0;
        gf_boolean_t    any_file_changed        = _gf_false;
        int             ret                     = 0;

        nbytes = strlen (exports_file_path) + 1;
        exp_file_path = alloca (nbytes);
        snprintf (exp_file_path, nbytes, "%s", exports_file_path);

        nbytes = strlen (netgroups_file_path) + 1;
        ng_file_path = alloca (nbytes);
        snprintf (ng_file_path, nbytes, "%s", netgroups_file_path);

        /* Set the initial timestamps to avoid reloading right after
         * mnt3svc_init () spawns this thread */
        get_file_mtime (exp_file_path, &exp_time);
        get_file_mtime (ng_file_path, &ng_time);

        while (_gf_true) {
                if (mstate->stop_refresh)
                        break;
                any_file_changed = _gf_false;

                /* Sleep before checking the file again */
                sleep (mstate->nfs->auth_refresh_time_secs);

                if (_mnt3_has_file_changed (exp_file_path, &exp_time)) {
                        gf_msg (GF_MNT, GF_LOG_INFO, 0, NFS_MSG_UPDATING_EXP,
                                "File %s changed, updating exports,",
                                exp_file_path);

                        ret = mnt3_auth_set_exports_auth (mstate->auth_params,
                                                          exp_file_path);
                        if (ret)
                                gf_msg (GF_MNT, GF_LOG_ERROR, 0,
                                        NFS_MSG_SET_EXP_AUTH_PARAM_FAIL,
                                        "Failed to set export auth params.");
                        else
                                any_file_changed = _gf_true;
                }

                if (_mnt3_has_file_changed (ng_file_path, &ng_time)) {
                        gf_msg (GF_MNT, GF_LOG_INFO, 0,
                                NFS_MSG_UPDATING_NET_GRP, "File %s changed,"
                                "updating netgroups", ng_file_path);

                        ret = mnt3_auth_set_netgroups_auth (mstate->auth_params,
                                                            ng_file_path);
                        if (ret)
                                gf_msg (GF_MNT, GF_LOG_ERROR, 0,
                                        NFS_MSG_SET_NET_GRP_FAIL,
                                        "Failed to set netgroup auth params.");
                        else
                                any_file_changed = _gf_true;
                }

                /* If no files changed, go back to sleep */
                if (!any_file_changed)
                        continue;

                gf_msg (GF_MNT, GF_LOG_INFO, 0, NFS_MSG_PURGING_AUTH_CACHE,
                        "Purging auth cache.");
                auth_cache_purge (mstate->authcache);

                /* Walk through mounts that are no longer authorized
                 * and unmount them on the server side. This will
                 * cause subsequent file ops to fail with access denied.
                 */
                _mnt3_invalidate_old_mounts (mstate);
        }

        return NULL;
}

/**
 * _mnt3_init_auth_params -- Initialize authentication parameters by allocating
 *                           the struct and setting the exports & netgroups
 *                           files as parameters.
 *
 * @mstate : The mount state we are going to set the auth parameters in it.
 *
 * @return : success: 0 for success
 *           failure: -EINVAL for bad args, -ENOMEM for allocation errors, < 0
 *                    for other errors (parsing the files, etc.) These are
 *                    bubbled up from the functions we call to set the params.
 */
int
_mnt3_init_auth_params (struct mount3_state *mstate)
{
        int      ret            = -EINVAL;
        char     *exp_file_path = NULL;
        char     *ng_file_path  = NULL;
        size_t   nbytes         = 0;

        GF_VALIDATE_OR_GOTO (GF_MNT, mstate, out);

        mstate->auth_params = mnt3_auth_params_init (mstate);
        if (!mstate->auth_params) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to init mount auth params.");
                ret = -ENOMEM;
                goto out;
        }

        nbytes = strlen (exports_file_path) + 1;
        exp_file_path = alloca (nbytes);
        snprintf (exp_file_path, nbytes, "%s", exports_file_path);

        ret = mnt3_auth_set_exports_auth (mstate->auth_params, exp_file_path);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                        NFS_MSG_SET_EXP_AUTH_PARAM_FAIL,
                        "Failed to set export auth params.");
                goto out;
        }

        nbytes = strlen (netgroups_file_path) + 1;
        ng_file_path = alloca (nbytes);
        snprintf (ng_file_path, nbytes, "%s", netgroups_file_path);

        ret = mnt3_auth_set_netgroups_auth (mstate->auth_params, ng_file_path);
        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret,
                        NFS_MSG_SET_EXP_AUTH_PARAM_FAIL,
                        "Failed to set netgroup auth params.");
                goto out;
        }

        ret = 0;
out:
        return ret;
}


/**
 * mnt3svc_deinit -- Function called by the nfs translator to cleanup all state
 *
 * @nfsx : The NFS translator used to perform the cleanup
 *         This structure holds all the pointers to memory that we need to free
 *         as well as the threads that have been started.
 */
void
mnt3svc_deinit (xlator_t *nfsx)
{
        struct mount3_state *mstate = NULL;
        struct nfs_state    *nfs    = NULL;

        if (!nfsx || !nfsx->private)
                return;

        nfs = (struct nfs_state *)nfsx->private;
        mstate = (struct mount3_state *)nfs->mstate;

        if (nfs->refresh_auth) {
                /* Mark as true and wait for thread to exit */
                mstate->stop_refresh = _gf_true;
                pthread_join (mstate->auth_refresh_thread, NULL);
        }

        if (nfs->exports_auth)
                mnt3_auth_params_deinit (mstate->auth_params);

        /* Unmount everything and clear mountdict */
        LOCK (&mstate->mountlock);
        {
                __mnt3svc_umountall (mstate);
                dict_unref (mstate->mountdict);
        }
        UNLOCK (&mstate->mountlock);

}

rpcsvc_program_t *
mnt3svc_init (xlator_t *nfsx)
{
        struct mount3_state     *mstate = NULL;
        struct nfs_state        *nfs = NULL;
        dict_t                  *options = NULL;
        char                    *portstr = NULL;
        int                      ret = -1;
        pthread_t                udp_thread;

        if (!nfsx || !nfsx->private)
                return NULL;

        nfs = (struct nfs_state *)nfsx->private;

        gf_msg_debug (GF_MNT, 0, "Initing Mount v3 state");
        mstate = (struct mount3_state *)nfs->mstate;
        if (!mstate) {
                gf_msg (GF_MNT, GF_LOG_ERROR, 0, NFS_MSG_MNT_STATE_INIT_FAIL,
                        "Mount v3 state init failed");
                goto err;
        }

        mstate->nfs = nfs;

        mstate->mountdict = dict_new ();
        if (!mstate->mountdict) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ENOMEM, NFS_MSG_NO_MEMORY,
                        "Failed to setup mount dict. Allocation error.");
                goto err;
        }

        if (nfs->exports_auth) {
                ret = _mnt3_init_auth_params (mstate);
                if (ret < 0)
                        goto err;

                mstate->authcache = auth_cache_init (nfs->auth_cache_ttl_sec);
                if (!mstate->authcache) {
                        ret = -ENOMEM;
                        goto err;
                }

                mstate->stop_refresh = _gf_false; /* Allow thread to run */
                gf_thread_create (&mstate->auth_refresh_thread, NULL,
                                  _mnt3_auth_param_refresh_thread, mstate,
                                  "nfsauth");
        } else
                gf_msg (GF_MNT, GF_LOG_INFO, 0, NFS_MSG_EXP_AUTH_DISABLED,
                        "Exports auth has been disabled!");

        mnt3prog.private = mstate;
        options = dict_new ();

        ret = gf_asprintf (&portstr, "%d", GF_MOUNTV3_PORT);
        if (ret == -1)
                goto err;

        ret = dict_set_dynstr (options, "transport.socket.listen-port",
                               portstr);
        if (ret == -1)
                goto err;

        ret = dict_set_str (options, "transport-type", "socket");
        if (ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
                        "dict_set_str error");
                goto err;
        }

        if (nfs->allow_insecure) {
                ret = dict_set_str (options, "rpc-auth-allow-insecure", "on");
                if (ret == -1) {
                        gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                                NFS_MSG_DICT_SET_FAILED, "dict_set_str error");
                        goto err;
                }
                ret = dict_set_str (options, "rpc-auth.ports.insecure", "on");
                if (ret == -1) {
                        gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                                NFS_MSG_DICT_SET_FAILED, "dict_set_str error");
                        goto err;
                }
        }

        ret= rpcsvc_create_listeners (nfs->rpcsvc, options, nfsx->name);
        if (ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                        NFS_MSG_LISTENERS_CREATE_FAIL,
                        "Unable to create listeners");
                dict_unref (options);
                goto err;
        }

        if (nfs->mount_udp) {
                gf_thread_create (&udp_thread, NULL, mount3udp_thread, nfsx,
                                  "nfsudp");
        }
        return &mnt3prog;
err:
        return NULL;
}


rpcsvc_actor_t  mnt1svc_actors[MOUNT1_PROC_COUNT] = {
        {"NULL",    MOUNT1_NULL,    mnt3svc_null,   NULL, 0, DRC_NA},
        {"MNT",     MOUNT1_MNT,     NULL,           NULL, 0, DRC_NA },
        {"DUMP",    MOUNT1_DUMP,    mnt3svc_dump,   NULL, 0, DRC_NA},
        {"UMNT",    MOUNT1_UMNT,    mnt3svc_umnt,   NULL, 0, DRC_NA},
        {"UMNTALL", MOUNT1_UMNTALL, NULL,           NULL, 0, DRC_NA},
        {"EXPORT",  MOUNT1_EXPORT,  mnt3svc_export, NULL, 0, DRC_NA}
};

rpcsvc_program_t        mnt1prog = {
                        .progname       = "MOUNT1",
                        .prognum        = MOUNT_PROGRAM,
                        .progver        = MOUNT_V1,
                        .progport       = GF_MOUNTV1_PORT,
                        .actors         = mnt1svc_actors,
                        .numactors      = MOUNT1_PROC_COUNT,
                        .min_auth       = AUTH_NULL,
                        .synctask       = _gf_true,
};


rpcsvc_program_t *
mnt1svc_init (xlator_t *nfsx)
{
        struct mount3_state     *mstate = NULL;
        struct nfs_state        *nfs = NULL;
        dict_t                  *options = NULL;
        char                    *portstr = NULL;
        int                      ret = -1;

        if (!nfsx || !nfsx->private)
                return NULL;

        nfs = (struct nfs_state *)nfsx->private;

        gf_msg_debug (GF_MNT, GF_LOG_DEBUG, "Initing Mount v1 state");
        mstate = (struct mount3_state *)nfs->mstate;
        if (!mstate) {
                gf_msg (GF_MNT, GF_LOG_ERROR, EINVAL,
                        NFS_MSG_MNT_STATE_INIT_FAIL,
                        "Mount v3 state init failed");
                goto err;
        }

        mnt1prog.private = mstate;

        options = dict_new ();

        ret = gf_asprintf (&portstr, "%d", GF_MOUNTV1_PORT);
        if (ret == -1)
                goto err;

        ret = dict_set_dynstr (options, "transport.socket.listen-port", portstr);
        if (ret == -1)
                goto err;
        ret = dict_set_str (options, "transport-type", "socket");
        if (ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, errno, NFS_MSG_DICT_SET_FAILED,
                        "dict_set_str error");
                goto err;
        }

        if (nfs->allow_insecure) {
                ret = dict_set_str (options, "rpc-auth-allow-insecure", "on");
                if (ret == -1) {
                        gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                                NFS_MSG_DICT_SET_FAILED,
                                "dict_set_str error");
                        goto err;
                }
                ret = dict_set_str (options, "rpc-auth.ports.insecure", "on");
                if (ret == -1) {
                        gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                                NFS_MSG_DICT_SET_FAILED,
                                "dict_set_str error");
                        goto err;
                }
        }

#ifdef IPV6_DEFAULT
        ret = dict_set_str (options, "transport.address-family", "inet6");
        if (ret == -1) {
                gf_log (GF_NFS, GF_LOG_ERROR,
                        "dict_set_str error when trying to enable ipv6");
              goto err;
        }
#endif

        ret = rpcsvc_create_listeners (nfs->rpcsvc, options, nfsx->name);
        if (ret == -1) {
                gf_msg (GF_NFS, GF_LOG_ERROR, errno,
                        NFS_MSG_LISTENERS_CREATE_FAIL,
                        "Unable to create listeners");
                dict_unref (options);
                goto err;
        }

        return &mnt1prog;
err:
        return NULL;
}

int
mount_reconfigure_state (xlator_t *nfsx, dict_t *options)
{
        int                    ret      = -1;
        struct nfs_state       *nfs     = NULL;
        struct mount3_state    *ms      = NULL;
        struct mnt3_export     *exp     = NULL;
        struct mnt3_export     *texp  = NULL;

        if ((!nfsx) || (!options))
                return (-1);

        nfs = (struct nfs_state *)nfs_state (nfsx);
        if (!nfs)
                return (-1);

        ms = nfs->mstate;
        if (!ms)
                return (-1);

        /*
         * Free() up the old export list. mnt3_init_options() will
         * rebuild the export list from scratch. Do it with locking
         * to avoid unnecessary race conditions.
         */
        LOCK (&ms->mountlock);
        list_for_each_entry_safe (exp, texp, &ms->exportlist, explist) {
                list_del (&exp->explist);
                mnt3_export_free (exp);
        }
        ret = mnt3_init_options (ms, options);
        UNLOCK (&ms->mountlock);

        if (ret < 0) {
                gf_msg (GF_MNT, GF_LOG_ERROR, ret, NFS_MSG_RECONF_FAIL,
                        "Options reconfigure failed");
                return (-1);
        }

        return (0);
}