summaryrefslogtreecommitdiff
path: root/usr/src/cmd/fs.d/nfs/mount/mount.c
blob: dc0a8526cfa5a597d74c5ec7b10af1a89d1fd447 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

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

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

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

/*
 * nfs mount
 */

#define	NFSCLIENT
#include <locale.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include <stdarg.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/param.h>
#include <rpc/rpc.h>
#include <errno.h>
#include <sys/stat.h>
#include <netdb.h>
#include <sys/mount.h>
#include <sys/mntent.h>
#include <sys/mnttab.h>
#include <nfs/nfs.h>
#include <nfs/mount.h>
#include <rpcsvc/mount.h>
#include <sys/pathconf.h>
#include <netdir.h>
#include <netconfig.h>
#include <sys/sockio.h>
#include <net/if.h>
#include <syslog.h>
#include <fslib.h>
#include <deflt.h>
#include <sys/wait.h>
#include "replica.h"
#include <netinet/in.h>
#include <nfs/nfs_sec.h>
#include <rpcsvc/daemon_utils.h>
#include <priv.h>
#include <tsol/label.h>
#include "nfs_subr.h"
#include "webnfs.h"
#include <rpcsvc/nfs4_prot.h>
#include <limits.h>

#include <nfs/nfssys.h>
extern int _nfssys(enum nfssys_op, void *);

#ifndef	NFS_VERSMAX
#define	NFS_VERSMAX	4
#endif
#ifndef	NFS_VERSMIN
#define	NFS_VERSMIN	2
#endif

#define	RET_OK		0
#define	RET_RETRY	32
#define	RET_ERR		33
#define	RET_MNTERR	1000
#define	ERR_PROTO_NONE		0
#define	ERR_PROTO_INVALID	901
#define	ERR_PROTO_UNSUPP	902
#define	ERR_NETPATH		903
#define	ERR_NOHOST		904
#define	ERR_RPCERROR		905

typedef struct err_ret {
	int error_type;
	int error_value;
} err_ret_t;

#define	SET_ERR_RET(errst, etype, eval) \
	if (errst) { \
		(errst)->error_type = etype; \
		(errst)->error_value = eval; \
	}

/* number of transports to try */
#define	MNT_PREF_LISTLEN	2
#define	FIRST_TRY		1
#define	SECOND_TRY		2

#define	BIGRETRY	10000

/* maximum length of RPC header for NFS messages */
#define	NFS_RPC_HDR	432

#define	NFS_ARGS_EXTB_secdata(args, secdata) \
	{ (args)->nfs_args_ext = NFS_ARGS_EXTB, \
	(args)->nfs_ext_u.nfs_extB.secdata = secdata; }

extern int __clnt_bindresvport(CLIENT *);
extern char *nfs_get_qop_name();
extern AUTH * nfs_create_ah();
extern enum snego_stat nfs_sec_nego();

static void usage(void);
static int retry(struct mnttab *, int);
static int set_args(int *, struct nfs_args *, char *, struct mnttab *);
static int get_fh_via_pub(struct nfs_args *, char *, char *, bool_t, bool_t,
	int *, struct netconfig **, ushort_t);
static int get_fh(struct nfs_args *, char *, char *, int *, bool_t,
	struct netconfig **, ushort_t);
static int make_secure(struct nfs_args *, char *, struct netconfig *,
	bool_t, rpcvers_t);
static int mount_nfs(struct mnttab *, int, err_ret_t *);
static int getaddr_nfs(struct nfs_args *, char *, struct netconfig **,
		    bool_t, char *, ushort_t, err_ret_t *, bool_t);
static void pr_err(const char *fmt, ...);
static void usage(void);
static struct netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
	struct netconfig **, char *, ushort_t, struct t_info *,
	caddr_t *, bool_t, char *, err_ret_t *);

static struct netbuf *get_the_addr(char *, rpcprog_t, rpcvers_t,
	struct netconfig *, ushort_t, struct t_info *, caddr_t *,
	bool_t, char *, err_ret_t *);

extern int self_check(char *);

static void read_default(void);

static char typename[64];

static int bg = 0;
static int backgrounded = 0;
static int posix = 0;
static int retries = BIGRETRY;
static ushort_t nfs_port = 0;
static char *nfs_proto = NULL;

static int mflg = 0;
static int Oflg = 0;	/* Overlay mounts */
static int qflg = 0;	/* quiet - don't print warnings on bad options */

static char *fstype = MNTTYPE_NFS;

static seconfig_t nfs_sec;
static int sec_opt = 0;	/* any security option ? */
static bool_t snego_done;
static void sigusr1(int);

extern void set_nfsv4_ephemeral_mount_to(void);

/*
 * list of support services needed
 */
static char	*service_list[] = { STATD, LOCKD, NULL };
static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };

/*
 * These two variables control the NFS version number to be used.
 *
 * nfsvers defaults to 0 which means to use the highest number that
 * both the client and the server support.  It can also be set to
 * a particular value, either 2, 3, or 4 to indicate the version
 * number of choice.  If the server (or the client) do not support
 * the version indicated, then the mount attempt will be failed.
 *
 * nfsvers_to_use is the actual version number found to use.  It
 * is determined in get_fh by pinging the various versions of the
 * NFS service on the server to see which responds positively.
 *
 * nfsretry_vers is the version number set when we retry the mount
 * command with the version decremented from nfsvers_to_use.
 * nfsretry_vers is set from nfsvers_to_use when we retry the mount
 * for errors other than RPC errors; it helps un know why we are
 * retrying. It is an indication that the retry is due to
 * non-RPC errors.
 */
static rpcvers_t nfsvers = 0;
static rpcvers_t nfsvers_to_use = 0;
static rpcvers_t nfsretry_vers = 0;

/*
 * There are the defaults (range) for the client when determining
 * which NFS version to use when probing the server (see above).
 * These will only be used when the vers mount option is not used and
 * these may be reset if /etc/default/nfs is configured to do so.
 */
static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;

/*
 * This variable controls whether to try the public file handle.
 */
static bool_t public_opt;

int
main(int argc, char *argv[])
{
	struct mnttab mnt;
	extern char *optarg;
	extern int optind;
	char optbuf[MAX_MNTOPT_STR];
	int ro = 0;
	int r;
	int c;
	char *myname;
	err_ret_t retry_error;

	(void) setlocale(LC_ALL, "");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	myname = strrchr(argv[0], '/');
	myname = myname ? myname + 1 : argv[0];
	(void) snprintf(typename, sizeof (typename), "%s %s",
	    MNTTYPE_NFS, myname);
	argv[0] = typename;

	mnt.mnt_mntopts = optbuf;
	(void) strcpy(optbuf, "rw");

	/*
	 * Set options
	 */
	while ((c = getopt(argc, argv, "ro:mOq")) != EOF) {
		switch (c) {
		case 'r':
			ro++;
			break;
		case 'o':
			if (strlen(optarg) >= MAX_MNTOPT_STR) {
				pr_err(gettext("option string too long"));
				return (RET_ERR);
			}
			(void) strcpy(mnt.mnt_mntopts, optarg);
#ifdef LATER					/* XXX */
			if (strstr(optarg, MNTOPT_REMOUNT)) {
				/*
				 * If remount is specified, only rw is allowed.
				 */
				if ((strcmp(optarg, MNTOPT_REMOUNT) != 0) &&
				    (strcmp(optarg, "remount,rw") != 0) &&
				    (strcmp(optarg, "rw,remount") != 0)) {
					pr_err(gettext("Invalid options\n"));
					exit(RET_ERR);
				}
			}
#endif /* LATER */				/* XXX */
			break;
		case 'm':
			mflg++;
			break;
		case 'O':
			Oflg++;
			break;
		case 'q':
			qflg++;
			break;
		default:
			usage();
			exit(RET_ERR);
		}
	}
	if (argc - optind != 2) {
		usage();
		exit(RET_ERR);
	}

	mnt.mnt_special = argv[optind];
	mnt.mnt_mountp = argv[optind+1];

	if (!priv_ineffect(PRIV_SYS_MOUNT) ||
	    !priv_ineffect(PRIV_NET_PRIVADDR)) {
		pr_err(gettext("insufficient privileges\n"));
		exit(RET_ERR);
	}

	/*
	 * On a labeled system, allow read-down nfs mounts if privileged
	 * (PRIV_NET_MAC_AWARE) to do so.  Otherwise, ignore the error
	 * and "mount equal label only" behavior will result.
	 */
	if (is_system_labeled())
		(void) setpflags(NET_MAC_AWARE, 1);

	/*
	 * Read the defaults file to see if the min/max versions have
	 * been set and therefore would override the encoded defaults.
	 * Then check to make sure that if they were set that the
	 * values are reasonable.
	 */
	read_default();
	if (vers_min_default > vers_max_default ||
	    vers_min_default < NFS_VERSMIN ||
	    vers_max_default > NFS_VERSMAX) {
		pr_err("%s %s\n%s %s\n",
		    gettext("Incorrect configuration of client\'s"),
		    NFSADMIN,
		    gettext("NFS_CLIENT_VERSMIN or NFS_CLIENT_VERSMAX"),
		    gettext("is either out of range or overlaps."));
	}

	SET_ERR_RET(&retry_error, ERR_PROTO_NONE, 0);
	r = mount_nfs(&mnt, ro, &retry_error);
	if (r == RET_RETRY && retries) {
		/*
		 * Check the error code from the last mount attempt if it was
		 * an RPC error, then retry as is. Otherwise we retry with the
		 * nfsretry_vers set. It is set by decrementing nfsvers_to_use.
		 * If we are retrying with nfsretry_vers then we don't print any
		 * retry messages, since we are not retrying due to an RPC
		 * error.
		 */
		if (retry_error.error_type) {
			if (retry_error.error_type != ERR_RPCERROR) {
				nfsretry_vers = nfsvers_to_use =
				    nfsvers_to_use - 1;
				if (nfsretry_vers < NFS_VERSMIN)
					return (r);
			}
		}

		r = retry(&mnt, ro);
	}
	/*
	 * exit(r);
	 */
	return (r);
}

static void
pr_err(const char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	if (backgrounded != 0) {
		(void) vsyslog(LOG_ERR, fmt, ap);
	} else {
		(void) fprintf(stderr, "%s: ", typename);
		(void) vfprintf(stderr, fmt, ap);
		(void) fflush(stderr);
	}
	va_end(ap);
}

static void
usage()
{
	(void) fprintf(stderr,
	    gettext("Usage: nfs mount [-r] [-o opts] [server:]path dir\n"));
	exit(RET_ERR);
}

static int
mount_nfs(struct mnttab *mntp, int ro, err_ret_t *retry_error)
{
	struct nfs_args *args = NULL, *argp = NULL, *prev_argp = NULL;
	struct netconfig *nconf = NULL;
	struct replica *list = NULL;
	int mntflags = 0;
	int i, r, n;
	int oldvers = 0, vers = 0;
	int last_error = RET_OK;
	int replicated = 0;
	char *p;
	bool_t url;
	bool_t use_pubfh;
	char *special = NULL;
	char *oldpath = NULL;
	char *newpath = NULL;
	char *service;
	pid_t pi;
	struct flock f;
	char *saveopts = NULL;
	char **sl = NULL;

	mntp->mnt_fstype = MNTTYPE_NFS;

	if (ro) {
		mntflags |= MS_RDONLY;
		/* convert "rw"->"ro" */
		if (p = strstr(mntp->mnt_mntopts, "rw")) {
			if (*(p+2) == ',' || *(p+2) == '\0')
				*(p+1) = 'o';
		}
	}

	if (Oflg)
		mntflags |= MS_OVERLAY;

	list = parse_replica(mntp->mnt_special, &n);
	if (list == NULL) {
		if (n < 0)
			pr_err(gettext("nfs file system; use [host:]path\n"));
		else
			pr_err(gettext("no memory\n"));
		return (RET_ERR);
	}

	replicated = (n > 1);

	/*
	 * There are some free() calls at the bottom of this loop, so be
	 * careful about adding continue statements.
	 */
	for (i = 0; i < n; i++) {
		char *path;
		char *host;
		ushort_t port;

		argp = (struct nfs_args *)malloc(sizeof (*argp));
		if (argp == NULL) {
			pr_err(gettext("no memory\n"));
			last_error = RET_ERR;
			goto out;
		}
		memset(argp, 0, sizeof (*argp));

		memset(&nfs_sec, 0, sizeof (nfs_sec));
		sec_opt = 0;
		use_pubfh = FALSE;
		url = FALSE;
		port = 0;
		snego_done = FALSE;

		/*
		 * Looking for resources of the form
		 *	nfs://server_host[:port_number]/path_name
		 */
		if (strcmp(list[i].host, "nfs") == 0 && strncmp(list[i].path,
		    "//", 2) == 0) {
			char *sport, *cb;
			url = TRUE;
			oldpath = strdup(list[i].path);
			if (oldpath == NULL) {
				pr_err(gettext("memory allocation failure\n"));
				last_error = RET_ERR;
				goto out;
			}
			host = list[i].path+2;
			path = strchr(host, '/');

			if (path == NULL) {
				pr_err(gettext(
				    "illegal nfs url syntax\n"));
				last_error = RET_ERR;
				goto out;
			}

			*path = '\0';
			if (*host == '[') {
				cb = strchr(host, ']');
				if (cb == NULL) {
					pr_err(gettext(
					    "illegal nfs url syntax\n"));
					last_error = RET_ERR;
					goto out;
				} else {
					*cb = '\0';
					host++;
					cb++;
					if (*cb == ':')
						port = htons((ushort_t)
						    atoi(cb+1));
				}
			} else {
				sport = strchr(host, ':');

				if (sport != NULL && sport < path) {
					*sport = '\0';
					port = htons((ushort_t)atoi(sport+1));
				}
			}

			path++;
			if (*path == '\0')
				path = ".";

		} else {
			host = list[i].host;
			path = list[i].path;
		}

		if (r = set_args(&mntflags, argp, host, mntp)) {
			last_error = r;
			goto out;
		}

		if (public_opt == TRUE)
			use_pubfh = TRUE;

		if (port == 0) {
			port = nfs_port;
		} else if (nfs_port != 0 && nfs_port != port) {
			pr_err(gettext(
			    "port (%u) in nfs URL not the same"
			    " as port (%u) in port option\n"),
			    (unsigned int)ntohs(port),
			    (unsigned int)ntohs(nfs_port));
			last_error = RET_ERR;
			goto out;
		}


		if (replicated && !(mntflags & MS_RDONLY)) {
			pr_err(gettext(
			    "replicated mounts must be read-only\n"));
			last_error = RET_ERR;
			goto out;
		}

		if (replicated && (argp->flags & NFSMNT_SOFT)) {
			pr_err(gettext(
			    "replicated mounts must not be soft\n"));
			last_error = RET_ERR;
			goto out;
		}

		oldvers = vers;
		nconf = NULL;

		r = RET_ERR;

		/*
		 * If -o public was specified, and/or a URL was specified,
		 * then try the public file handle method.
		 */
		if ((use_pubfh == TRUE) || (url == TRUE)) {
			r = get_fh_via_pub(argp, host, path, url, use_pubfh,
			    &vers, &nconf, port);

			if (r != RET_OK) {
				/*
				 * If -o public was specified, then return the
				 * error now.
				 */
				if (use_pubfh == TRUE) {
					last_error = r;
					goto out;
				}
			} else
				use_pubfh = TRUE;
			argp->flags |= NFSMNT_PUBLIC;
		}

		if ((r != RET_OK) || (vers == NFS_V4)) {
			bool_t loud_on_mnt_err;

			/*
			 * This can happen if -o public is not specified,
			 * special is a URL, and server doesn't support
			 * public file handle.
			 */
			if (url) {
				URLparse(path);
			}

			/*
			 * If the path portion of the URL didn't have
			 * a leading / then there is good possibility
			 * that a mount without a leading slash will
			 * fail.
			 */
			if (url == TRUE && *path != '/')
				loud_on_mnt_err = FALSE;
			else
				loud_on_mnt_err = TRUE;

			r = get_fh(argp, host, path, &vers,
			    loud_on_mnt_err, &nconf, port);

			if (r != RET_OK) {

				/*
				 * If there was no leading / and the path was
				 * derived from a URL, then try again
				 * with a leading /.
				 */
				if ((r == RET_MNTERR) &&
				    (loud_on_mnt_err == FALSE)) {

					newpath = malloc(strlen(path)+2);

					if (newpath == NULL) {
						pr_err(gettext("memory "
						    "allocation failure\n"));
						last_error = RET_ERR;
						goto out;
					}

					strcpy(newpath, "/");
					strcat(newpath, path);

					r = get_fh(argp, host, newpath, &vers,
					    TRUE, &nconf, port);

					if (r == RET_OK)
						path = newpath;
				}

				/*
				 * map exit code back to RET_ERR.
				 */
				if (r == RET_MNTERR)
					r = RET_ERR;

				if (r != RET_OK) {

					if (replicated) {
						if (argp->fh)
							free(argp->fh);
						if (argp->pathconf)
							free(argp->pathconf);
						free(argp);
						goto cont;
					}

					last_error = r;
					goto out;
				}
			}
		}

		if (oldvers && vers != oldvers) {
			pr_err(
			    gettext("replicas must have the same version\n"));
			last_error = RET_ERR;
			goto out;
		}

		/*
		 * decide whether to use remote host's
		 * lockd or do local locking
		 */
		if (!(argp->flags & NFSMNT_LLOCK) && vers == NFS_VERSION &&
		    remote_lock(host, argp->fh)) {
			(void) fprintf(stderr, gettext(
			    "WARNING: No network locking on %s:%s:"),
			    host, path);
			(void) fprintf(stderr, gettext(
			    " contact admin to install server change\n"));
			argp->flags |= NFSMNT_LLOCK;
		}

		if (self_check(host))
			argp->flags |= NFSMNT_LOOPBACK;

		if (use_pubfh == FALSE) {
			/*
			 * Call to get_fh() above may have obtained the
			 * netconfig info and NULL proc'd the server.
			 * This would be the case with v4
			 */
			if (!(argp->flags & NFSMNT_KNCONF)) {
				nconf = NULL;
				if (r = getaddr_nfs(argp, host, &nconf,
				    FALSE, path, port, retry_error,
				    TRUE)) {
						last_error = r;
						goto out;
				}
			}
		}

		if (make_secure(argp, host, nconf, use_pubfh, vers) < 0) {
			last_error = RET_ERR;
			goto out;
		}

		if ((url == TRUE) && (use_pubfh == FALSE)) {
			/*
			 * Convert the special from
			 *	nfs://host/path
			 * to
			 *	host:path
			 */
			if (convert_special(&special, host, oldpath, path,
			    mntp->mnt_special) == -1) {
				(void) fprintf(stderr, gettext(
				    "could not convert URL nfs:%s to %s:%s\n"),
				    oldpath, host, path);
				last_error = RET_ERR;
				goto out;
			} else {
				mntp->mnt_special = special;
			}
		}

		if (prev_argp == NULL)
			args = argp;
		else
			prev_argp->nfs_ext_u.nfs_extB.next = argp;
		prev_argp = argp;

cont:
		if (oldpath != NULL) {
			free(oldpath);
			oldpath = NULL;
		}

		if (newpath != NULL) {
			free(newpath);
			newpath = NULL;
		}
	}

	argp = NULL;

	if (args == NULL) {
		last_error = RET_RETRY;
		goto out;
	}

	/* Determine which services are appropriate for the NFS version */
	if (strcmp(fstype, MNTTYPE_NFS4) == 0)
		sl = service_list_v4;
	else
		sl = service_list;

	/*
	 * enable services as needed.
	 */
	_check_services(sl);

	mntflags |= MS_DATA | MS_OPTIONSTR;

	if (mflg)
		mntflags |= MS_NOMNTTAB;

	if (!qflg)
		saveopts = strdup(mntp->mnt_mntopts);

	/*
	 * And make sure that we have the ephemeral mount_to
	 * set for this zone.
	 */
	set_nfsv4_ephemeral_mount_to();

	if (mount(mntp->mnt_special, mntp->mnt_mountp, mntflags, fstype, args,
	    sizeof (*args), mntp->mnt_mntopts, MAX_MNTOPT_STR) < 0) {
		if (errno != ENOENT) {
			pr_err(gettext("mount: %s: %s\n"),
			    mntp->mnt_mountp, strerror(errno));
		} else {
			struct stat sb;
			if (stat(mntp->mnt_mountp, &sb) < 0 && errno == ENOENT)
				pr_err(gettext("mount: %s: %s\n"),
				    mntp->mnt_mountp, strerror(ENOENT));
			else
				pr_err("%s: %s\n", mntp->mnt_special,
				    strerror(ENOENT));
		}

		last_error = RET_ERR;
		goto out;
	}

	if (!qflg && saveopts != NULL) {
		cmp_requested_to_actual_options(saveopts, mntp->mnt_mntopts,
		    mntp->mnt_special, mntp->mnt_mountp);
	}

out:
	if (saveopts != NULL)
		free(saveopts);
	if (special != NULL)
		free(special);
	if (oldpath != NULL)
		free(oldpath);
	if (newpath != NULL)
		free(newpath);

	free_replica(list, n);

	if (argp != NULL) {
		/*
		 * If we had a new entry which was not added to the
		 * list yet, then add it now that it can be freed.
		 */
		if (prev_argp == NULL)
			args = argp;
		else
			prev_argp->nfs_ext_u.nfs_extB.next = argp;
	}
	argp = args;
	while (argp != NULL) {
		if (argp->fh)
			free(argp->fh);
		if (argp->pathconf)
			free(argp->pathconf);
		if (argp->knconf)
			free(argp->knconf);
		if (argp->addr) {
			free(argp->addr->buf);
			free(argp->addr);
		}
		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
		if (argp->syncaddr) {
			free(argp->syncaddr->buf);
			free(argp->syncaddr);
		}
		if (argp->netname)
			free(argp->netname);
		prev_argp = argp;
		argp = argp->nfs_ext_u.nfs_extB.next;
		free(prev_argp);
	}

	return (last_error);
}

/*
 * These options are duplicated in uts/common/fs/nfs/nfs_dlinet.c
 * Changes must be made to both lists.
 */
static char *optlist[] = {
#define	OPT_RO		0
	MNTOPT_RO,
#define	OPT_RW		1
	MNTOPT_RW,
#define	OPT_QUOTA	2
	MNTOPT_QUOTA,
#define	OPT_NOQUOTA	3
	MNTOPT_NOQUOTA,
#define	OPT_SOFT	4
	MNTOPT_SOFT,
#define	OPT_HARD	5
	MNTOPT_HARD,
#define	OPT_SUID	6
	MNTOPT_SUID,
#define	OPT_NOSUID	7
	MNTOPT_NOSUID,
#define	OPT_GRPID	8
	MNTOPT_GRPID,
#define	OPT_REMOUNT	9
	MNTOPT_REMOUNT,
#define	OPT_NOSUB	10
	MNTOPT_NOSUB,
#define	OPT_INTR	11
	MNTOPT_INTR,
#define	OPT_NOINTR	12
	MNTOPT_NOINTR,
#define	OPT_PORT	13
	MNTOPT_PORT,
#define	OPT_SECURE	14
	MNTOPT_SECURE,
#define	OPT_RSIZE	15
	MNTOPT_RSIZE,
#define	OPT_WSIZE	16
	MNTOPT_WSIZE,
#define	OPT_TIMEO	17
	MNTOPT_TIMEO,
#define	OPT_RETRANS	18
	MNTOPT_RETRANS,
#define	OPT_ACTIMEO	19
	MNTOPT_ACTIMEO,
#define	OPT_ACREGMIN	20
	MNTOPT_ACREGMIN,
#define	OPT_ACREGMAX	21
	MNTOPT_ACREGMAX,
#define	OPT_ACDIRMIN	22
	MNTOPT_ACDIRMIN,
#define	OPT_ACDIRMAX	23
	MNTOPT_ACDIRMAX,
#define	OPT_BG		24
	MNTOPT_BG,
#define	OPT_FG		25
	MNTOPT_FG,
#define	OPT_RETRY	26
	MNTOPT_RETRY,
#define	OPT_NOAC	27
	MNTOPT_NOAC,
#define	OPT_NOCTO	28
	MNTOPT_NOCTO,
#define	OPT_LLOCK	29
	MNTOPT_LLOCK,
#define	OPT_POSIX	30
	MNTOPT_POSIX,
#define	OPT_VERS	31
	MNTOPT_VERS,
#define	OPT_PROTO	32
	MNTOPT_PROTO,
#define	OPT_SEMISOFT	33
	MNTOPT_SEMISOFT,
#define	OPT_NOPRINT	34
	MNTOPT_NOPRINT,
#define	OPT_SEC		35
	MNTOPT_SEC,
#define	OPT_LARGEFILES	36
	MNTOPT_LARGEFILES,
#define	OPT_NOLARGEFILES 37
	MNTOPT_NOLARGEFILES,
#define	OPT_PUBLIC	38
	MNTOPT_PUBLIC,
#define	OPT_DIRECTIO	39
	MNTOPT_FORCEDIRECTIO,
#define	OPT_NODIRECTIO	40
	MNTOPT_NOFORCEDIRECTIO,
#define	OPT_XATTR	41
	MNTOPT_XATTR,
#define	OPT_NOXATTR	42
	MNTOPT_NOXATTR,
#define	OPT_DEVICES	43
	MNTOPT_DEVICES,
#define	OPT_NODEVICES	44
	MNTOPT_NODEVICES,
#define	OPT_SETUID	45
	MNTOPT_SETUID,
#define	OPT_NOSETUID	46
	MNTOPT_NOSETUID,
#define	OPT_EXEC	47
	MNTOPT_EXEC,
#define	OPT_NOEXEC	48
	MNTOPT_NOEXEC,
	NULL
};

static int
convert_int(int *val, char *str)
{
	long lval;

	if (str == NULL || !isdigit(*str))
		return (-1);

	lval = strtol(str, &str, 10);
	if (*str != '\0' || lval > INT_MAX)
		return (-2);

	*val = (int)lval;
	return (0);
}

static int
set_args(int *mntflags, struct nfs_args *args, char *fshost, struct mnttab *mnt)
{
	char *saveopt, *optstr, *opts, *newopts, *val;
	int num;
	int largefiles = 0;
	int invalid = 0;
	int attrpref = 0;
	int optlen;

	args->flags = NFSMNT_INT;	/* default is "intr" */
	args->flags |= NFSMNT_HOSTNAME;
	args->flags |= NFSMNT_NEWARGS;	/* using extented nfs_args structure */
	args->hostname = fshost;

	optstr = opts = strdup(mnt->mnt_mntopts);
	/* sizeof (MNTOPT_XXX) includes one extra byte we may need for "," */
	optlen = strlen(mnt->mnt_mntopts) + sizeof (MNTOPT_XATTR) + 1;
	if (optlen > MAX_MNTOPT_STR) {
		pr_err(gettext("option string too long"));
		return (RET_ERR);
	}
	newopts = malloc(optlen);
	if (opts == NULL || newopts == NULL) {
		pr_err(gettext("no memory"));
		if (opts)
			free(opts);
		if (newopts)
			free(newopts);
		return (RET_ERR);
	}
	newopts[0] = '\0';

	while (*opts) {
		invalid = 0;
		saveopt = opts;
		switch (getsubopt(&opts, optlist, &val)) {
		case OPT_RO:
			*mntflags |= MS_RDONLY;
			break;
		case OPT_RW:
			*mntflags &= ~(MS_RDONLY);
			break;
		case OPT_QUOTA:
		case OPT_NOQUOTA:
			break;
		case OPT_SOFT:
			args->flags |= NFSMNT_SOFT;
			args->flags &= ~(NFSMNT_SEMISOFT);
			break;
		case OPT_SEMISOFT:
			args->flags |= NFSMNT_SOFT;
			args->flags |= NFSMNT_SEMISOFT;
			break;
		case OPT_HARD:
			args->flags &= ~(NFSMNT_SOFT);
			args->flags &= ~(NFSMNT_SEMISOFT);
			break;
		case OPT_SUID:
			*mntflags &= ~(MS_NOSUID);
			break;
		case OPT_NOSUID:
			*mntflags |= MS_NOSUID;
			break;
		case OPT_GRPID:
			args->flags |= NFSMNT_GRPID;
			break;
		case OPT_REMOUNT:
			*mntflags |= MS_REMOUNT;
			break;
		case OPT_INTR:
			args->flags |= NFSMNT_INT;
			break;
		case OPT_NOINTR:
			args->flags &= ~(NFSMNT_INT);
			break;
		case OPT_NOAC:
			args->flags |= NFSMNT_NOAC;
			break;
		case OPT_PORT:
			if (convert_int(&num, val) != 0)
				goto badopt;
			nfs_port = htons((ushort_t)num);
			break;

		case OPT_SECURE:
			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
				pr_err(gettext("can not get \"dh\" from %s\n"),
				    NFSSEC_CONF);
				goto badopt;
			}
			sec_opt++;
			break;

		case OPT_NOCTO:
			args->flags |= NFSMNT_NOCTO;
			break;

		case OPT_RSIZE:
			if (convert_int(&args->rsize, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_RSIZE;
			break;
		case OPT_WSIZE:
			if (convert_int(&args->wsize, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_WSIZE;
			break;
		case OPT_TIMEO:
			if (convert_int(&args->timeo, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_TIMEO;
			break;
		case OPT_RETRANS:
			if (convert_int(&args->retrans, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_RETRANS;
			break;
		case OPT_ACTIMEO:
			if (convert_int(&args->acregmax, val) != 0)
				goto badopt;
			args->acdirmin = args->acregmin = args->acdirmax
			    = args->acregmax;
			args->flags |= NFSMNT_ACDIRMAX;
			args->flags |= NFSMNT_ACREGMAX;
			args->flags |= NFSMNT_ACDIRMIN;
			args->flags |= NFSMNT_ACREGMIN;
			break;
		case OPT_ACREGMIN:
			if (convert_int(&args->acregmin, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_ACREGMIN;
			break;
		case OPT_ACREGMAX:
			if (convert_int(&args->acregmax, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_ACREGMAX;
			break;
		case OPT_ACDIRMIN:
			if (convert_int(&args->acdirmin, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_ACDIRMIN;
			break;
		case OPT_ACDIRMAX:
			if (convert_int(&args->acdirmax, val) != 0)
				goto badopt;
			args->flags |= NFSMNT_ACDIRMAX;
			break;
		case OPT_BG:
			bg++;
			break;
		case OPT_FG:
			bg = 0;
			break;
		case OPT_RETRY:
			if (convert_int(&retries, val) != 0)
				goto badopt;
			break;
		case OPT_LLOCK:
			args->flags |= NFSMNT_LLOCK;
			break;
		case OPT_POSIX:
			posix = 1;
			break;
		case OPT_VERS:
			if (convert_int(&num, val) != 0)
				goto badopt;
			nfsvers = (rpcvers_t)num;
			break;
		case OPT_PROTO:
			if (val == NULL)
				goto badopt;

			nfs_proto = (char *)malloc(strlen(val)+1);
			if (!nfs_proto) {
				pr_err(gettext("no memory"));
				return (RET_ERR);
			}

			(void) strncpy(nfs_proto, val, strlen(val)+1);
			break;

		case OPT_NOPRINT:
			args->flags |= NFSMNT_NOPRINT;
			break;

		case OPT_LARGEFILES:
			largefiles = 1;
			break;

		case OPT_NOLARGEFILES:
			pr_err(gettext("NFS can't support \"nolargefiles\"\n"));
			free(optstr);
			return (RET_ERR);

		case OPT_SEC:
			if (val == NULL) {
				pr_err(gettext(
				    "\"sec\" option requires argument\n"));
				return (RET_ERR);
			}
			if (nfs_getseconfig_byname(val, &nfs_sec)) {
				pr_err(gettext("can not get \"%s\" from %s\n"),
				    val, NFSSEC_CONF);
				return (RET_ERR);
			}
			sec_opt++;
			break;

		case OPT_PUBLIC:
			public_opt = TRUE;
			break;

		case OPT_DIRECTIO:
			args->flags |= NFSMNT_DIRECTIO;
			break;

		case OPT_NODIRECTIO:
			args->flags &= ~(NFSMNT_DIRECTIO);
			break;

		case OPT_XATTR:
		case OPT_NOXATTR:
			/*
			 * VFS options; just need to get them into the
			 * new mount option string and note we've seen them
			 */
			attrpref = 1;
			break;
		default:
			/*
			 * Note that this could be a valid OPT_* option so
			 * we can't use "val" but need to use "saveopt".
			 */
			if (fsisstdopt(saveopt))
				break;
			invalid = 1;
			if (!qflg)
				(void) fprintf(stderr, gettext(
				    "mount: %s on %s - WARNING unknown option"
				    " \"%s\"\n"), mnt->mnt_special,
				    mnt->mnt_mountp, saveopt);
			break;
		}
		if (!invalid) {
			if (newopts[0])
				strcat(newopts, ",");
			strcat(newopts, saveopt);
		}
	}
	/* Default is to turn extended attrs on */
	if (!attrpref) {
		if (newopts[0])
			strcat(newopts, ",");
		strcat(newopts, MNTOPT_XATTR);
	}
	strcpy(mnt->mnt_mntopts, newopts);
	free(newopts);
	free(optstr);

	/* ensure that only one secure mode is requested */
	if (sec_opt > 1) {
		pr_err(gettext("Security options conflict\n"));
		return (RET_ERR);
	}

	/* ensure that the user isn't trying to get large files over V2 */
	if (nfsvers == NFS_VERSION && largefiles) {
		pr_err(gettext("NFS V2 can't support \"largefiles\"\n"));
		return (RET_ERR);
	}

	if (nfsvers == NFS_V4 &&
	    nfs_proto != NULL &&
	    strncasecmp(nfs_proto, NC_UDP, strlen(NC_UDP)) == 0) {
		pr_err(gettext("NFS V4 does not support %s\n"), nfs_proto);
		return (RET_ERR);
	}

	return (RET_OK);

badopt:
	pr_err(gettext("invalid option: \"%s\"\n"), saveopt);
	free(optstr);
	return (RET_ERR);
}

static int
make_secure(struct nfs_args *args, char *hostname, struct netconfig *nconf,
	bool_t use_pubfh, rpcvers_t vers)
{
	sec_data_t *secdata;
	int flags;
	struct netbuf *syncaddr = NULL;
	struct nd_addrlist *retaddrs = NULL;
	char netname[MAXNETNAMELEN+1];

	/*
	 * check to see if any secure mode is requested.
	 * if not, use default security mode.
	 */
	if (!snego_done && !sec_opt) {
		/*
		 * Get default security mode.
		 * AUTH_UNIX has been the default choice for a long time.
		 * The better NFS security service becomes, the better chance
		 * we will set stronger security service as the default NFS
		 * security mode.
		 */
		if (nfs_getseconfig_default(&nfs_sec)) {
			pr_err(gettext("error getting default"
			    " security entry\n"));
			return (-1);
		}
		args->flags |= NFSMNT_SECDEFAULT;
	}

	/*
	 * Get the network address for the time service on the server.
	 * If an RPC based time service is not available then try the
	 * IP time service.
	 *
	 * This is for AUTH_DH processing. We will also pass down syncaddr
	 * and netname for NFS V4 even if AUTH_DH is not requested right now.
	 * NFS V4 does security negotiation in the kernel via SECINFO.
	 * These information might be needed later in the kernel.
	 *
	 * Eventurally, we want to move this code to nfs_clnt_secdata()
	 * when autod_nfs.c and mount.c can share the same get_the_addr()
	 * routine.
	 */
	flags = 0;
	syncaddr = NULL;

	if (nfs_sec.sc_rpcnum == AUTH_DH || vers == NFS_V4) {
		/*
		 * If using the public fh or nfsv4, we will not contact the
		 * remote RPCBINDer, since it is possibly behind a firewall.
		 */
		if (use_pubfh == FALSE && vers != NFS_V4)
			syncaddr = get_the_addr(hostname, RPCBPROG, RPCBVERS,
			    nconf, 0, NULL, NULL, FALSE, NULL, NULL);

		if (syncaddr != NULL) {
			/* for flags in sec_data */
			flags |= AUTH_F_RPCTIMESYNC;
		} else {
			struct nd_hostserv hs;
			int error;

			hs.h_host = hostname;
			hs.h_serv = "timserver";

			error = netdir_getbyname(nconf, &hs, &retaddrs);

			if (error != ND_OK && (nfs_sec.sc_rpcnum == AUTH_DH)) {
				pr_err(gettext("%s: secure: no time service\n"),
				    hostname);
				return (-1);
			}

			if (error == ND_OK)
				syncaddr = retaddrs->n_addrs;

			/*
			 * For NFS_V4 if AUTH_DH is negotiated later in the
			 * kernel thru SECINFO, it will need syncaddr
			 * and netname data.
			 */
			if (vers == NFS_V4 && syncaddr &&
			    host2netname(netname, hostname, NULL)) {
				args->syncaddr = malloc(sizeof (struct netbuf));
				args->syncaddr->buf = malloc(syncaddr->len);
				(void) memcpy(args->syncaddr->buf,
				    syncaddr->buf, syncaddr->len);
				args->syncaddr->len = syncaddr->len;
				args->syncaddr->maxlen = syncaddr->maxlen;
				args->netname = strdup(netname);
				args->flags |= NFSMNT_SECURE;
			}
		}
	}

	/*
	 * For the initial chosen flavor (any flavor defined in nfssec.conf),
	 * the data will be stored in the sec_data structure via
	 * nfs_clnt_secdata() and be passed to the kernel via nfs_args_*
	 * extended data structure.
	 */
	if (!(secdata = nfs_clnt_secdata(&nfs_sec, hostname, args->knconf,
	    syncaddr, flags))) {
		pr_err(gettext("errors constructing security related data\n"));
		if (flags & AUTH_F_RPCTIMESYNC) {
			free(syncaddr->buf);
			free(syncaddr);
		} else if (retaddrs)
			netdir_free((void *)retaddrs, ND_ADDRLIST);
		return (-1);
	}

	NFS_ARGS_EXTB_secdata(args, secdata);
	if (flags & AUTH_F_RPCTIMESYNC) {
		free(syncaddr->buf);
		free(syncaddr);
	} else if (retaddrs)
		netdir_free((void *)retaddrs, ND_ADDRLIST);
	return (0);
}

/*
 * Get the network address on "hostname" for program "prog"
 * with version "vers" by using the nconf configuration data
 * passed in.
 *
 * If the address of a netconfig pointer is null then
 * information is not sufficient and no netbuf will be returned.
 *
 * Finally, ping the null procedure of that service.
 *
 * A similar routine is also defined in ../../autofs/autod_nfs.c.
 * This is a potential routine to move to ../lib for common usage.
 */
static struct netbuf *
get_the_addr(char *hostname, ulong_t prog, ulong_t vers,
	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
	caddr_t *fhp, bool_t get_pubfh, char *fspath, err_ret_t *error)
{
	struct netbuf *nb = NULL;
	struct t_bind *tbind = NULL;
	CLIENT *cl = NULL;
	struct timeval tv;
	int fd = -1;
	AUTH *ah = NULL;
	AUTH *new_ah = NULL;
	struct snego_t snego;

	if (nconf == NULL)
		return (NULL);

	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) == -1)
		goto done;

	/* LINTED pointer alignment */
	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
	    == NULL)
		goto done;

	/*
	 * In the case of public filehandle usage or NFSv4 we want to
	 * avoid use of the rpcbind/portmap protocol
	 */
	if ((get_pubfh == TRUE) || (vers == NFS_V4)) {
		struct nd_hostserv hs;
		struct nd_addrlist *retaddrs;
		int retval;
		hs.h_host = hostname;

		/* NFS where vers==4 does not support UDP */
		if (vers == NFS_V4 &&
		    strncasecmp(nconf->nc_proto, NC_UDP,
		    strlen(NC_UDP)) == 0) {
			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);
			goto done;
		}

		if (port == 0)
			hs.h_serv = "nfs";
		else
			hs.h_serv = NULL;

		if ((retval = netdir_getbyname(nconf, &hs, &retaddrs))
		    != ND_OK) {
			/*
			 * Carefully set the error value here. Want to signify
			 * that the error was an unknown host.
			 */
			if (retval == ND_NOHOST) {
				SET_ERR_RET(error, ERR_NOHOST, retval);
			}

			goto done;
		}
		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
		    retaddrs->n_addrs->len);
		tbind->addr.len = retaddrs->n_addrs->len;
		netdir_free((void *)retaddrs, ND_ADDRLIST);
		(void) netdir_options(nconf, ND_SET_RESERVEDPORT, fd, NULL);

	} else {
		if (rpcb_getaddr(prog, vers, nconf, &tbind->addr,
		    hostname) == FALSE) {
			goto done;
		}
	}

	if (port) {
		/* LINTED pointer alignment */
		if (strcmp(nconf->nc_protofmly, NC_INET) == 0)
			((struct sockaddr_in *)tbind->addr.buf)->sin_port
			    = port;
		else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
			((struct sockaddr_in6 *)tbind->addr.buf)->sin6_port
			    = port;

	}

	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
	if (cl == NULL) {
		/*
		 * clnt_tli_create() returns either RPC_SYSTEMERROR,
		 * RPC_UNKNOWNPROTO or RPC_TLIERROR. The RPC_TLIERROR translates
		 * to "Misc. TLI error". This is not too helpful. Most likely
		 * the connection to the remote server timed out, so this
		 * error is at least less perplexing.
		 * See: usr/src/cmd/rpcinfo/rpcinfo.c
		 */
		if (rpc_createerr.cf_stat == RPC_TLIERROR) {
			SET_ERR_RET(error, ERR_RPCERROR, RPC_PMAPFAILURE);
		} else {
			SET_ERR_RET(error, ERR_RPCERROR, rpc_createerr.cf_stat);
		}
		goto done;
	}

	ah = authsys_create_default();
	if (ah != NULL)
		cl->cl_auth = ah;

	tv.tv_sec = 5;
	tv.tv_usec = 0;

	(void) clnt_control(cl, CLSET_TIMEOUT, (char *)&tv);

	if ((get_pubfh == TRUE) && (vers != NFS_V4)) {
		enum snego_stat sec;

		if (!snego_done) {
			/*
			 * negotiate sec flavor.
			 */
			snego.cnt = 0;
			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
			    SNEGO_SUCCESS) {
				int jj;

				/*
				 * check if server supports the one
				 * specified in the sec= option.
				 */
				if (sec_opt) {
					for (jj = 0; jj < snego.cnt; jj++) {
						if (snego.array[jj] ==
						    nfs_sec.sc_nfsnum) {
							snego_done = TRUE;
							break;
						}
					}
				}

				/*
				 * find a common sec flavor
				 */
				if (!snego_done) {
					if (sec_opt) {
						pr_err(gettext(
						    "Server does not support"
						    " the security flavor"
						    " specified.\n"));
					}

					for (jj = 0; jj < snego.cnt; jj++) {
						if (!nfs_getseconfig_bynumber(
						    snego.array[jj],
						    &nfs_sec)) {
							snego_done = TRUE;
#define	EMSG80SUX "Security flavor %d was negotiated and will be used.\n"
							if (sec_opt)
								pr_err(gettext(
								    EMSG80SUX),
								    nfs_sec.
								    sc_nfsnum);
							break;
						}
					}
				}

				if (!snego_done)
					return (NULL);

				/*
				 * Now that the flavor has been
				 * negotiated, get the fh.
				 *
				 * First, create an auth handle using the
				 * negotiated sec flavor in the next lookup to
				 * fetch the filehandle.
				 */
				new_ah = nfs_create_ah(cl, hostname, &nfs_sec);
				if (new_ah == NULL)
					goto done;
				cl->cl_auth = new_ah;
			} else if (sec == SNEGO_ARRAY_TOO_SMALL || sec ==
			    SNEGO_FAILURE) {
				goto done;
			}

			/*
			 * Note that if sec == SNEGO_DEF_VALID
			 * default sec flavor is acceptable.
			 * Use it to get the filehandle.
			 */
		}

		if (vers == NFS_VERSION) {
			wnl_diropargs arg;
			wnl_diropres res;

			memset((char *)&arg.dir, 0, sizeof (wnl_fh));
			arg.name = fspath;
			memset((char *)&res, 0, sizeof (wnl_diropres));
			if (wnlproc_lookup_2(&arg, &res, cl) !=
			    RPC_SUCCESS || res.status != NFS_OK)
				goto done;

			*fhp = malloc(sizeof (wnl_fh));

			if (*fhp == NULL) {
				pr_err(gettext("no memory\n"));
				goto done;
			}

			memcpy((char *)*fhp,
			    (char *)&res.wnl_diropres_u.wnl_diropres.file,
			    sizeof (wnl_fh));
		} else {
			WNL_LOOKUP3args arg;
			WNL_LOOKUP3res res;
			nfs_fh3 *fh3p;

			memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
			arg.what.name = fspath;
			memset((char *)&res, 0, sizeof (WNL_LOOKUP3res));
			if (wnlproc3_lookup_3(&arg, &res, cl) !=
			    RPC_SUCCESS || res.status != NFS3_OK)
				goto done;

			fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));

			if (fh3p == NULL) {
				pr_err(gettext("no memory\n"));
				goto done;
			}

			fh3p->fh3_length =
			    res.WNL_LOOKUP3res_u.res_ok.object.data.data_len;
			memcpy(fh3p->fh3_u.data,
			    res.WNL_LOOKUP3res_u.res_ok.object.data.data_val,
			    fh3p->fh3_length);

			*fhp = (caddr_t)fh3p;
		}
	} else {
		struct rpc_err r_err;
		enum clnt_stat rc;

		/*
		 * NULL procedures need not have an argument or
		 * result param.
		 */
		if (vers == NFS_VERSION)
			rc = wnlproc_null_2(NULL, NULL, cl);
		else if (vers == NFS_V3)
			rc = wnlproc3_null_3(NULL, NULL, cl);
		else
			rc = wnlproc4_null_4(NULL, NULL, cl);

		if (rc != RPC_SUCCESS) {
			clnt_geterr(cl, &r_err);
			if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
				switch (r_err.re_status) {
				case RPC_TLIERROR:
				case RPC_CANTRECV:
				case RPC_CANTSEND:
					r_err.re_status = RPC_PROGVERSMISMATCH;
				}
			}
			SET_ERR_RET(error, ERR_RPCERROR, r_err.re_status);
			goto done;
		}
	}

	/*
	 * Make a copy of the netbuf to return
	 */
	nb = (struct netbuf *)malloc(sizeof (*nb));
	if (nb == NULL) {
		pr_err(gettext("no memory\n"));
		goto done;
	}
	*nb = tbind->addr;
	nb->buf = (char *)malloc(nb->maxlen);
	if (nb->buf == NULL) {
		pr_err(gettext("no memory\n"));
		free(nb);
		nb = NULL;
		goto done;
	}
	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);

done:
	if (cl) {
		if (ah != NULL) {
			if (new_ah != NULL)
				AUTH_DESTROY(ah);
			AUTH_DESTROY(cl->cl_auth);
			cl->cl_auth = NULL;
		}
		clnt_destroy(cl);
		cl = NULL;
	}
	if (tbind) {
		t_free((char *)tbind, T_BIND);
		tbind = NULL;
	}
	if (fd >= 0)
		(void) t_close(fd);
	return (nb);
}

static int
check_nconf(struct netconfig *nconf, int nthtry, int *valid_proto)
{
	int	try_test = 0;
	int	valid_family;
	char	*proto = NULL;


	if (nthtry == FIRST_TRY) {
		try_test = ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
		    (nconf->nc_semantics == NC_TPI_COTS));
		proto = NC_TCP;
	} else if (nthtry == SECOND_TRY) {
		try_test = (nconf->nc_semantics == NC_TPI_CLTS);
		proto = NC_UDP;
	}

	if (proto &&
	    (strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
	    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
	    (strcmp(nconf->nc_proto, proto) == 0))
		*valid_proto = TRUE;
	else
		*valid_proto = FALSE;

	return (try_test);
}

/*
 * Get a network address on "hostname" for program "prog"
 * with version "vers".  If the port number is specified (non zero)
 * then try for a TCP/UDP transport and set the port number of the
 * resulting IP address.
 *
 * If the address of a netconfig pointer was passed and
 * if it's not null, use it as the netconfig otherwise
 * assign the address of the netconfig that was used to
 * establish contact with the service.
 *
 * A similar routine is also defined in ../../autofs/autod_nfs.c.
 * This is a potential routine to move to ../lib for common usage.
 *
 * "error" refers to a more descriptive term when get_addr fails
 * and returns NULL: ERR_PROTO_NONE if no error introduced by
 * -o proto option, ERR_NETPATH if error found in NETPATH
 * environment variable, ERR_PROTO_INVALID if an unrecognized
 * protocol is specified by user, and ERR_PROTO_UNSUPP for a
 * recognized but invalid protocol (eg. ticlts, ticots, etc.).
 * "error" is ignored if get_addr returns non-NULL result.
 *
 */
static struct netbuf *
get_addr(char *hostname, ulong_t prog, ulong_t vers, struct netconfig **nconfp,
	char *proto, ushort_t port, struct t_info *tinfo, caddr_t *fhp,
	bool_t get_pubfh, char *fspath, err_ret_t *error)
{
	struct netbuf *nb = NULL;
	struct netconfig *nconf = NULL;
	NCONF_HANDLE *nc = NULL;
	int nthtry = FIRST_TRY;
	err_ret_t errsave_nohost, errsave_rpcerr;

	SET_ERR_RET(&errsave_nohost, ERR_PROTO_NONE, 0);
	SET_ERR_RET(&errsave_rpcerr, ERR_PROTO_NONE, 0);

	SET_ERR_RET(error, ERR_PROTO_NONE, 0);

	if (nconfp && *nconfp)
		return (get_the_addr(hostname, prog, vers, *nconfp, port,
		    tinfo, fhp, get_pubfh, fspath, error));
	/*
	 * No nconf passed in.
	 *
	 * Try to get a nconf from /etc/netconfig filtered by
	 * the NETPATH environment variable.
	 * First search for COTS, second for CLTS unless proto
	 * is specified.  When we retry, we reset the
	 * netconfig list so that we would search the whole list
	 * all over again.
	 */

	if ((nc = setnetpath()) == NULL) {
		/* should only return an error if problems with NETPATH */
		/* In which case you are hosed */
		SET_ERR_RET(error, ERR_NETPATH, 0);
		goto done;
	}

	/*
	 * If proto is specified, then only search for the match,
	 * otherwise try COTS first, if failed, try CLTS.
	 */
	if (proto) {
		/* no matching proto name */
		SET_ERR_RET(error, ERR_PROTO_INVALID, 0);

		while (nconf = getnetpath(nc)) {
			if (strcmp(nconf->nc_netid, proto))
				continue;

			/* may be unsupported */
			SET_ERR_RET(error, ERR_PROTO_UNSUPP, 0);

			if ((port != 0) &&
			    ((strcmp(nconf->nc_protofmly, NC_INET) == 0 ||
			    strcmp(nconf->nc_protofmly, NC_INET6) == 0) &&
			    (strcmp(nconf->nc_proto, NC_TCP) != 0 &&
			    strcmp(nconf->nc_proto, NC_UDP) != 0))) {
				continue;
			} else {
				nb = get_the_addr(hostname, prog,
				    vers, nconf, port, tinfo,
				    fhp, get_pubfh, fspath, error);

				if (nb != NULL)
					break;

				/* nb is NULL - deal with errors */
				if (error) {
					if (error->error_type == ERR_NOHOST)
						SET_ERR_RET(&errsave_nohost,
						    error->error_type,
						    error->error_value);
					if (error->error_type == ERR_RPCERROR)
						SET_ERR_RET(&errsave_rpcerr,
						    error->error_type,
						    error->error_value);
				}
				/*
				 * continue with same protocol
				 * selection
				 */
				continue;
			}
		} /* end of while */

		if (nconf == NULL)
			goto done;

		if ((nb = get_the_addr(hostname, prog, vers, nconf, port,
		    tinfo, fhp, get_pubfh, fspath, error)) == NULL)
			goto done;
	} else {
retry:
		SET_ERR_RET(error, ERR_NETPATH, 0);
		while (nconf = getnetpath(nc)) {
			SET_ERR_RET(error, ERR_PROTO_NONE, 0);

			if (nconf->nc_flag & NC_VISIBLE) {
				int	valid_proto;

				if (check_nconf(nconf,
				    nthtry, &valid_proto)) {
					if (port == 0)
						break;

					if (valid_proto == TRUE)
						break;
				}
			}
		} /* while */
		if (nconf == NULL) {
			if (++nthtry <= MNT_PREF_LISTLEN) {
				endnetpath(nc);
				if ((nc = setnetpath()) == NULL)
					goto done;
				goto retry;
			} else
				goto done;
		} else {
			if ((nb = get_the_addr(hostname, prog, vers, nconf,
			    port, tinfo, fhp, get_pubfh, fspath, error))
			    == NULL) {
				/* nb is NULL - deal with errors */
				if (error) {
					if (error->error_type == ERR_NOHOST)
						SET_ERR_RET(&errsave_nohost,
						    error->error_type,
						    error->error_value);
					if (error->error_type == ERR_RPCERROR)
						SET_ERR_RET(&errsave_rpcerr,
						    error->error_type,
						    error->error_value);
				}
				/*
				 * Continue the same search path in the
				 * netconfig db until no more matched
				 * nconf (nconf == NULL).
				 */
				goto retry;
			}
		}
	}
	SET_ERR_RET(error, ERR_PROTO_NONE, 0);

	/*
	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
	 * and return it thru nconfp.
	 */
	*nconfp = getnetconfigent(nconf->nc_netid);
	if (*nconfp == NULL) {
		syslog(LOG_ERR, "no memory\n");
		free(nb);
		nb = NULL;
	}
done:
	if (nc)
		endnetpath(nc);

	if (nb == NULL) {
		/*
		 * Check the saved errors. The RPC error has *
		 * precedence over the no host error.
		 */
		if (errsave_nohost.error_type != ERR_PROTO_NONE)
			SET_ERR_RET(error, errsave_nohost.error_type,
			    errsave_nohost.error_value);

		if (errsave_rpcerr.error_type != ERR_PROTO_NONE)
			SET_ERR_RET(error, errsave_rpcerr.error_type,
			    errsave_rpcerr.error_value);
	}

	return (nb);
}

/*
 * Get a file handle usinging multi-component lookup with the public
 * file handle.
 */
static int
get_fh_via_pub(struct nfs_args *args, char *fshost, char *fspath, bool_t url,
	bool_t loud, int *versp, struct netconfig **nconfp, ushort_t port)
{
	uint_t vers_min;
	uint_t vers_max;
	int r;
	char *path;

	if (nfsvers != 0) {
		vers_max = vers_min = nfsvers;
	} else {
		vers_max = vers_max_default;
		vers_min = vers_min_default;
	}

	if (url == FALSE) {
		path = malloc(strlen(fspath) + 2);
		if (path == NULL) {
			if (loud == TRUE)
				pr_err(gettext("no memory\n"));
			return (RET_ERR);
		}

		path[0] = (char)WNL_NATIVEPATH;
		(void) strcpy(&path[1], fspath);

	} else  {
		path = fspath;
	}

	for (nfsvers_to_use = vers_max; nfsvers_to_use >= vers_min;
	    nfsvers_to_use--) {
		/*
		 * getaddr_nfs will also fill in the fh for us.
		 */
		r = getaddr_nfs(args, fshost, nconfp,
		    TRUE, path, port, NULL, FALSE);

		if (r == RET_OK) {
			/*
			 * Since we are using the public fh, and NLM is
			 * not firewall friendly, use local locking.
			 * Not the case for v4.
			 */
			*versp = nfsvers_to_use;
			switch (nfsvers_to_use) {
			case NFS_V4:
				fstype = MNTTYPE_NFS4;
				break;
			case NFS_V3:
				fstype = MNTTYPE_NFS3;
				/* fall through to pick up llock option */
			default:
				args->flags |= NFSMNT_LLOCK;
				break;
			}
			if (fspath != path)
				free(path);

			return (r);
		}
	}

	if (fspath != path)
		free(path);

	if (loud == TRUE) {
		pr_err(gettext("Could not use public filehandle in request to"
		    " server %s\n"), fshost);
	}

	return (r);
}

/*
 * get fhandle of remote path from server's mountd
 */
static int
get_fh(struct nfs_args *args, char *fshost, char *fspath, int *versp,
	bool_t loud_on_mnt_err, struct netconfig **nconfp, ushort_t port)
{
	static struct fhstatus fhs;
	static struct mountres3 mountres3;
	static struct pathcnf p;
	nfs_fh3 *fh3p;
	struct timeval timeout = { 25, 0};
	CLIENT *cl;
	enum clnt_stat rpc_stat;
	rpcvers_t outvers = 0;
	rpcvers_t vers_to_try;
	rpcvers_t vers_min;
	static int printed = 0;
	int count, i, *auths;
	char *msg;

	switch (nfsvers) {
	case 2: /* version 2 specified try that only */
		vers_to_try = MOUNTVERS_POSIX;
		vers_min = MOUNTVERS;
		break;
	case 3: /* version 3 specified try that only */
		vers_to_try = MOUNTVERS3;
		vers_min = MOUNTVERS3;
		break;
	case 4: /* version 4 specified try that only */
		/*
		 * This assignment is in the wrong version sequence.
		 * The above are MOUNT program and this is NFS
		 * program.  However, it happens to work out since the
		 * two don't collide for NFSv4.
		 */
		vers_to_try = NFS_V4;
		vers_min = NFS_V4;
		break;
	default: /* no version specified, start with default */
		/*
		 * If the retry version is set, use that. This will
		 * be set if the last mount attempt returned any other
		 * besides an RPC error.
		 */
		if (nfsretry_vers)
			vers_to_try = nfsretry_vers;
		else {
			vers_to_try = vers_max_default;
			vers_min = vers_min_default;
		}

		break;
	}

	/*
	 * In the case of version 4, just NULL proc the server since
	 * there is no MOUNT program.  If this fails, then decrease
	 * vers_to_try and continue on with regular MOUNT program
	 * processing.
	 */
	if (vers_to_try == NFS_V4) {
		int savevers = nfsvers_to_use;
		err_ret_t error;
		int retval;
		SET_ERR_RET(&error, ERR_PROTO_NONE, 0);

		/* Let's hope for the best */
		nfsvers_to_use = NFS_V4;
		retval = getaddr_nfs(args, fshost, nconfp, FALSE,
		    fspath, port, &error, vers_min == NFS_V4);

		if (retval == RET_OK) {
			*versp = nfsvers_to_use = NFS_V4;
			fstype = MNTTYPE_NFS4;
			args->fh = strdup(fspath);
			if (args->fh == NULL) {
				pr_err(gettext("no memory\n"));
				*versp = nfsvers_to_use = savevers;
				return (RET_ERR);
			}
			return (RET_OK);
		}
		nfsvers_to_use = savevers;

		vers_to_try--;
		/* If no more versions to try, let the user know. */
		if (vers_to_try < vers_min)
			return (retval);

		/*
		 * If we are here, there are more versions to try but
		 * there has been an error of some sort.  If it is not
		 * an RPC error (e.g. host unknown), we just stop and
		 * return the error since the other versions would see
		 * the same error as well.
		 */
		if (retval == RET_ERR && error.error_type != ERR_RPCERROR)
			return (retval);
	}

	while ((cl = clnt_create_vers(fshost, MOUNTPROG, &outvers,
	    vers_min, vers_to_try, "datagram_v")) == NULL) {
		if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST) {
			pr_err(gettext("%s: %s\n"), fshost,
			    clnt_spcreateerror(""));
			return (RET_ERR);
		}

		/*
		 * We don't want to downgrade version on lost packets
		 */
		if ((rpc_createerr.cf_stat == RPC_TIMEDOUT) ||
		    (rpc_createerr.cf_stat == RPC_PMAPFAILURE)) {
			pr_err(gettext("%s: %s\n"), fshost,
			    clnt_spcreateerror(""));
			return (RET_RETRY);
		}

		/*
		 * back off and try the previous version - patch to the
		 * problem of version numbers not being contigous and
		 * clnt_create_vers failing (SunOS4.1 clients & SGI servers)
		 * The problem happens with most non-Sun servers who
		 * don't support mountd protocol #2. So, in case the
		 * call fails, we re-try the call anyway.
		 */
		vers_to_try--;
		if (vers_to_try < vers_min) {
			if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH) {
				if (nfsvers == 0) {
					pr_err(gettext(
			"%s:%s: no applicable versions of NFS supported\n"),
					    fshost, fspath);
				} else {
					pr_err(gettext(
			"%s:%s: NFS Version %d not supported\n"),
					    fshost, fspath, nfsvers);
				}
				return (RET_ERR);
			}
			if (!printed) {
				pr_err(gettext("%s: %s\n"), fshost,
				    clnt_spcreateerror(""));
				printed = 1;
			}
			return (RET_RETRY);
		}
	}
	if (posix && outvers < MOUNTVERS_POSIX) {
		pr_err(gettext("%s: %s: no pathconf info\n"),
		    fshost, clnt_sperror(cl, ""));
		clnt_destroy(cl);
		return (RET_ERR);
	}

	if (__clnt_bindresvport(cl) < 0) {
		pr_err(gettext("Couldn't bind to reserved port\n"));
		clnt_destroy(cl);
		return (RET_RETRY);
	}

	if ((cl->cl_auth = authsys_create_default()) == NULL) {
		pr_err(
		    gettext("Couldn't create default authentication handle\n"));
		clnt_destroy(cl);
		return (RET_RETRY);
	}

	switch (outvers) {
	case MOUNTVERS:
	case MOUNTVERS_POSIX:
		*versp = nfsvers_to_use = NFS_VERSION;
		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
		    (caddr_t)&fspath, xdr_fhstatus, (caddr_t)&fhs, timeout);
		if (rpc_stat != RPC_SUCCESS) {
			pr_err(gettext("%s:%s: server not responding %s\n"),
			    fshost, fspath, clnt_sperror(cl, ""));
			clnt_destroy(cl);
			return (RET_RETRY);
		}

		if ((errno = fhs.fhs_status) != MNT_OK) {
			if (loud_on_mnt_err) {
				if (errno == EACCES) {
					pr_err(gettext(
					    "%s:%s: access denied\n"),
					    fshost, fspath);
				} else {
					pr_err(gettext("%s:%s: %s\n"), fshost,
					    fspath, errno >= 0 ?
					    strerror(errno) : "invalid error "
					    "returned by server");
				}
			}
			clnt_destroy(cl);
			return (RET_MNTERR);
		}
		args->fh = malloc(sizeof (fhs.fhstatus_u.fhs_fhandle));
		if (args->fh == NULL) {
			pr_err(gettext("no memory\n"));
			return (RET_ERR);
		}
		memcpy((caddr_t)args->fh, (caddr_t)&fhs.fhstatus_u.fhs_fhandle,
		    sizeof (fhs.fhstatus_u.fhs_fhandle));
		if (!errno && posix) {
			rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
			    xdr_dirpath, (caddr_t)&fspath, xdr_ppathcnf,
			    (caddr_t)&p, timeout);
			if (rpc_stat != RPC_SUCCESS) {
				pr_err(gettext(
				    "%s:%s: server not responding %s\n"),
				    fshost, fspath, clnt_sperror(cl, ""));
				free(args->fh);
				clnt_destroy(cl);
				return (RET_RETRY);
			}
			if (_PC_ISSET(_PC_ERROR, p.pc_mask)) {
				pr_err(gettext(
				    "%s:%s: no pathconf info\n"),
				    fshost, fspath);
				free(args->fh);
				clnt_destroy(cl);
				return (RET_ERR);
			}
			args->flags |= NFSMNT_POSIX;
			args->pathconf = malloc(sizeof (p));
			if (args->pathconf == NULL) {
				pr_err(gettext("no memory\n"));
				free(args->fh);
				clnt_destroy(cl);
				return (RET_ERR);
			}
			memcpy((caddr_t)args->pathconf, (caddr_t)&p,
			    sizeof (p));
		}
		break;

	case MOUNTVERS3:
		*versp = nfsvers_to_use = NFS_V3;
		rpc_stat = clnt_call(cl, MOUNTPROC_MNT, xdr_dirpath,
		    (caddr_t)&fspath, xdr_mountres3, (caddr_t)&mountres3,
		    timeout);
		if (rpc_stat != RPC_SUCCESS) {
			pr_err(gettext("%s:%s: server not responding %s\n"),
			    fshost, fspath, clnt_sperror(cl, ""));
			clnt_destroy(cl);
			return (RET_RETRY);
		}

		/*
		 * Assume here that most of the MNT3ERR_*
		 * codes map into E* errors.
		 */
		if ((errno = mountres3.fhs_status) != MNT_OK) {
			if (loud_on_mnt_err) {
				switch (errno) {
				case MNT3ERR_NAMETOOLONG:
					msg = "path name is too long";
					break;
				case MNT3ERR_NOTSUPP:
					msg = "operation not supported";
					break;
				case MNT3ERR_SERVERFAULT:
					msg = "server fault";
					break;
				default:
					if (errno >= 0)
						msg = strerror(errno);
					else
						msg = "invalid error returned "
						    "by server";
					break;
				}
				pr_err(gettext("%s:%s: %s\n"), fshost,
				    fspath, msg);
			}
			clnt_destroy(cl);
			return (RET_MNTERR);
		}

		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
		if (fh3p == NULL) {
			pr_err(gettext("no memory\n"));
			return (RET_ERR);
		}
		fh3p->fh3_length =
		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_len;
		(void) memcpy(fh3p->fh3_u.data,
		    mountres3.mountres3_u.mountinfo.fhandle.fhandle3_val,
		    fh3p->fh3_length);
		args->fh = (caddr_t)fh3p;
		fstype = MNTTYPE_NFS3;

		/*
		 * Check the security flavor to be used.
		 *
		 * If "secure" or "sec=flavor" is a mount
		 * option, check if the server supports the "flavor".
		 * If the server does not support the flavor, return
		 * error.
		 *
		 * If no mount option is given then look for default auth
		 * (default auth entry in /etc/nfssec.conf) in the auth list
		 * returned from server. If default auth not found, then use
		 * the first supported security flavor (by the client) in the
		 * auth list returned from the server.
		 *
		 */
		auths =
		    mountres3.mountres3_u.mountinfo.auth_flavors
		    .auth_flavors_val;
		count =
		    mountres3.mountres3_u.mountinfo.auth_flavors
		    .auth_flavors_len;

		if (count <= 0) {
			pr_err(gettext(
			    "server %s did not return any security mode\n"),
			    fshost);
			clnt_destroy(cl);
			return (RET_ERR);
		}

		if (sec_opt) {
			for (i = 0; i < count; i++) {
				if (auths[i] == nfs_sec.sc_nfsnum)
					break;
			}
			if (i == count)
				goto autherr;
		} else {
			seconfig_t default_sec;

			/*
			 * Get client configured default auth.
			 */
			nfs_sec.sc_nfsnum = -1;
			default_sec.sc_nfsnum = -1;
			(void) nfs_getseconfig_default(&default_sec);

			/*
			 * Look for clients default auth in servers list.
			 */
			if (default_sec.sc_nfsnum != -1) {
				for (i = 0; i < count; i++) {
					if (auths[i] == default_sec.sc_nfsnum) {
						sec_opt++;
						nfs_sec = default_sec;
						break;
					}
				}
			}

			/*
			 * Could not find clients default auth in servers list.
			 * Pick the first auth from servers list that is
			 * also supported on the client.
			 */
			if (nfs_sec.sc_nfsnum == -1) {
				for (i = 0; i < count; i++) {
					if (!nfs_getseconfig_bynumber(auths[i],
					    &nfs_sec)) {
						sec_opt++;
						break;

					}
				}
			}

			if (i == count)
				goto autherr;
		}
		break;
	default:
		pr_err(gettext("%s:%s: Unknown MOUNT version %d\n"),
		    fshost, fspath, outvers);
		clnt_destroy(cl);
		return (RET_ERR);
	}

	clnt_destroy(cl);
	return (RET_OK);

autherr:
	pr_err(gettext(
	    "security mode does not match the server exporting %s:%s\n"),
	    fshost, fspath);
	clnt_destroy(cl);
	return (RET_ERR);
}

/*
 * Fill in the address for the server's NFS service and
 * fill in a knetconfig structure for the transport that
 * the service is available on.
 */
static int
getaddr_nfs(struct nfs_args *args, char *fshost, struct netconfig **nconfp,
	    bool_t get_pubfh, char *fspath, ushort_t port, err_ret_t *error,
	    bool_t print_rpcerror)
{
	struct stat sb;
	struct netconfig *nconf;
	struct knetconfig *knconfp;
	static int printed = 0;
	struct t_info tinfo;
	err_ret_t addr_error;

	SET_ERR_RET(error, ERR_PROTO_NONE, 0);
	SET_ERR_RET(&addr_error, ERR_PROTO_NONE, 0);

	if (nfs_proto) {
		/*
		 * If a proto is specified and its rdma try this. The kernel
		 * will later do the reachablity test and fail form there
		 * if rdma transport is not available to kernel rpc
		 */
		if (strcmp(nfs_proto, "rdma") == 0) {
			args->addr = get_addr(fshost, NFS_PROGRAM,
			    nfsvers_to_use, nconfp, NULL, port, &tinfo,
			    &args->fh, get_pubfh, fspath, &addr_error);

			args->flags |= NFSMNT_DORDMA;
		} else {
			args->addr = get_addr(fshost, NFS_PROGRAM,
			    nfsvers_to_use, nconfp, nfs_proto, port, &tinfo,
			    &args->fh, get_pubfh, fspath, &addr_error);
		}
	} else {
		args->addr = get_addr(fshost, NFS_PROGRAM, nfsvers_to_use,
		    nconfp, nfs_proto, port, &tinfo, &args->fh, get_pubfh,
		    fspath, &addr_error);
		/*
		 * If no proto is specified set this flag.
		 * Kernel mount code will try to use RDMA if its on the
		 * system, otherwise it will keep on using the protocol
		 * selected here, through the above get_addr call.
		 */
		if (nfs_proto == NULL)
			args->flags |= NFSMNT_TRYRDMA;
	}

	if (args->addr == NULL) {
		/*
		 * We could have failed because the server had no public
		 * file handle support. So don't print a message and don't
		 * retry.
		 */
		if (get_pubfh == TRUE)
			return (RET_ERR);

		if (!printed) {
			switch (addr_error.error_type) {
			case 0:
				printed = 1;
				break;
			case ERR_RPCERROR:
				if (!print_rpcerror)
					/* no error print at this time */
					break;
				pr_err(gettext("%s NFS service not"
				    " available %s\n"), fshost,
				    clnt_sperrno(addr_error.error_value));
				printed = 1;
				break;
			case ERR_NETPATH:
				pr_err(gettext("%s: Error in NETPATH.\n"),
				    fshost);
				printed = 1;
				break;
			case ERR_PROTO_INVALID:
				pr_err(gettext("%s: NFS service does not"
				    " recognize protocol: %s.\n"), fshost,
				    nfs_proto);
				printed = 1;
				break;
			case ERR_PROTO_UNSUPP:
				if (nfsvers || nfsvers_to_use == NFS_VERSMIN) {
					/*
					 * Don't set "printed" here. Since we
					 * have to keep checking here till we
					 * exhaust transport errors on all vers.
					 *
					 * Print this message if:
					 * 1. After we have tried all versions
					 *    of NFS and none support the asked
					 *    transport.
					 *
					 * 2. If a version is specified and it
					 *    does'nt support the asked
					 *    transport.
					 *
					 * Otherwise we decrement the version
					 * and retry below.
					 */
					pr_err(gettext("%s: NFS service does"
					    " not support protocol: %s.\n"),
					    fshost, nfs_proto);
				}
				break;
			case ERR_NOHOST:
				pr_err("%s: %s\n", fshost, "Unknown host");
				printed = 1;
				break;
			default:
				/* case ERR_PROTO_NONE falls through */
				pr_err(gettext("%s: NFS service not responding"
				    "\n"), fshost);
				printed = 1;
				break;
			}
		}
		SET_ERR_RET(error,
		    addr_error.error_type, addr_error.error_value);
		if (addr_error.error_type == ERR_PROTO_NONE)
			return (RET_RETRY);
		else if (addr_error.error_type == ERR_RPCERROR &&
		    !IS_UNRECOVERABLE_RPC(addr_error.error_value)) {
			return (RET_RETRY);
		} else if (nfsvers == 0 && addr_error.error_type ==
		    ERR_PROTO_UNSUPP && nfsvers_to_use != NFS_VERSMIN) {
			/*
			 * If no version is specified, and the error is due
			 * to an unsupported transport, then decrement the
			 * version and retry.
			 */
			return (RET_RETRY);
		} else
			return (RET_ERR);
	}
	nconf = *nconfp;

	if (stat(nconf->nc_device, &sb) < 0) {
		pr_err(gettext("getaddr_nfs: couldn't stat: %s: %s\n"),
		    nconf->nc_device, strerror(errno));
		return (RET_ERR);
	}

	knconfp = (struct knetconfig *)malloc(sizeof (*knconfp));
	if (!knconfp) {
		pr_err(gettext("no memory\n"));
		return (RET_ERR);
	}
	knconfp->knc_semantics = nconf->nc_semantics;
	knconfp->knc_protofmly = nconf->nc_protofmly;
	knconfp->knc_proto = nconf->nc_proto;
	knconfp->knc_rdev = sb.st_rdev;

	/* make sure we don't overload the transport */
	if (tinfo.tsdu > 0 && tinfo.tsdu < NFS_MAXDATA + NFS_RPC_HDR) {
		args->flags |= (NFSMNT_RSIZE | NFSMNT_WSIZE);
		if (args->rsize == 0 || args->rsize > tinfo.tsdu - NFS_RPC_HDR)
			args->rsize = tinfo.tsdu - NFS_RPC_HDR;
		if (args->wsize == 0 || args->wsize > tinfo.tsdu - NFS_RPC_HDR)
			args->wsize = tinfo.tsdu - NFS_RPC_HDR;
	}

	args->flags |= NFSMNT_KNCONF;
	args->knconf = knconfp;
	return (RET_OK);
}

static int
retry(struct mnttab *mntp, int ro)
{
	int delay = 5;
	int count = retries;
	int r;

	/*
	 * Please see comments on nfsretry_vers in the beginning of this file
	 * and in main() routine.
	 */

	if (bg) {
		if (fork() > 0)
			return (RET_OK);
		backgrounded = 1;
		pr_err(gettext("backgrounding: %s\n"), mntp->mnt_mountp);
	} else {
		if (!nfsretry_vers)
			pr_err(gettext("retrying: %s\n"), mntp->mnt_mountp);
	}

	while (count--) {
		if ((r = mount_nfs(mntp, ro, NULL)) == RET_OK) {
			pr_err(gettext("%s: mounted OK\n"), mntp->mnt_mountp);
			return (RET_OK);
		}
		if (r != RET_RETRY)
			break;

		if (count > 0) {
			(void) sleep(delay);
			delay *= 2;
			if (delay > 120)
				delay = 120;
		}
	}

	if (!nfsretry_vers)
		pr_err(gettext("giving up on: %s\n"), mntp->mnt_mountp);

	return (RET_ERR);
}

/*
 * Read the /etc/default/nfs configuration file to determine if the
 * client has been configured for a new min/max for the NFS version to
 * use.
 */
static void
read_default(void)
{
	char *defval;
	int errno;
	int tmp;

	/* Fail silently if error in opening the default nfs config file */
	if ((defopen(NFSADMIN)) == 0) {
		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
			errno = 0;
			tmp = strtol(defval, (char **)NULL, 10);
			if (errno == 0) {
				vers_min_default = tmp;
			}
		}
		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
			errno = 0;
			tmp = strtol(defval, (char **)NULL, 10);
			if (errno == 0) {
				vers_max_default = tmp;
			}
		}
		/* close defaults file */
		defopen(NULL);
	}
}

static void
sigusr1(int s)
{
}