xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision 13080:fcc1e406c13f)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
2212880SNagakiran.Rajashekar@Sun.COM  * Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <stdio.h>
260Sstevel@tonic-gate #include <unistd.h>
270Sstevel@tonic-gate #include <stdlib.h>
280Sstevel@tonic-gate #include <ctype.h>
290Sstevel@tonic-gate #include <syslog.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <deflt.h>
320Sstevel@tonic-gate #include <kstat.h>
330Sstevel@tonic-gate #include <sys/param.h>
340Sstevel@tonic-gate #include <sys/types.h>
350Sstevel@tonic-gate #include <sys/time.h>
360Sstevel@tonic-gate #include <sys/stat.h>
370Sstevel@tonic-gate #include <sys/wait.h>
380Sstevel@tonic-gate #include <sys/socket.h>
390Sstevel@tonic-gate #include <netinet/in.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <sys/signal.h>
420Sstevel@tonic-gate #include <rpc/rpc.h>
430Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
440Sstevel@tonic-gate #include <sys/mount.h>
450Sstevel@tonic-gate #include <sys/mntent.h>
460Sstevel@tonic-gate #include <sys/mnttab.h>
470Sstevel@tonic-gate #include <sys/fstyp.h>
480Sstevel@tonic-gate #include <sys/fsid.h>
490Sstevel@tonic-gate #include <arpa/inet.h>
500Sstevel@tonic-gate #include <netdb.h>
510Sstevel@tonic-gate #include <netconfig.h>
520Sstevel@tonic-gate #include <netdir.h>
530Sstevel@tonic-gate #include <errno.h>
540Sstevel@tonic-gate #define	NFSCLIENT
550Sstevel@tonic-gate #include <nfs/nfs.h>
560Sstevel@tonic-gate #include <nfs/mount.h>
570Sstevel@tonic-gate #include <rpcsvc/mount.h>
580Sstevel@tonic-gate #include <rpc/nettype.h>
590Sstevel@tonic-gate #include <locale.h>
600Sstevel@tonic-gate #include <setjmp.h>
610Sstevel@tonic-gate #include <sys/socket.h>
620Sstevel@tonic-gate #include <thread.h>
630Sstevel@tonic-gate #include <limits.h>
640Sstevel@tonic-gate #include <nss_dbdefs.h>			/* for NSS_BUFLEN_HOSTS */
650Sstevel@tonic-gate #include <nfs/nfs_sec.h>
660Sstevel@tonic-gate #include <sys/sockio.h>
670Sstevel@tonic-gate #include <net/if.h>
680Sstevel@tonic-gate #include <assert.h>
690Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
700Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
710Sstevel@tonic-gate #define	NO_RDDIR_CACHE
720Sstevel@tonic-gate #include "automount.h"
730Sstevel@tonic-gate #include "replica.h"
740Sstevel@tonic-gate #include "nfs_subr.h"
750Sstevel@tonic-gate #include "webnfs.h"
7611291SRobert.Thurlow@Sun.COM #include "nfs_resolve.h"
770Sstevel@tonic-gate #include <sys/sockio.h>
780Sstevel@tonic-gate #include <net/if.h>
790Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
801676Sjpk #include <pwd.h>
811676Sjpk #include <strings.h>
821676Sjpk #include <tsol/label.h>
831676Sjpk #include <zone.h>
84*13080SPavan.Mettu@Oracle.COM #include <limits.h>
85*13080SPavan.Mettu@Oracle.COM #include <libscf.h>
86*13080SPavan.Mettu@Oracle.COM #include <libshare.h>
87*13080SPavan.Mettu@Oracle.COM #include "smfcfg.h"
880Sstevel@tonic-gate 
895302Sth199096 extern void set_nfsv4_ephemeral_mount_to(void);
905302Sth199096 
910Sstevel@tonic-gate extern char *nfs_get_qop_name();
920Sstevel@tonic-gate extern AUTH *nfs_create_ah();
930Sstevel@tonic-gate extern enum snego_stat nfs_sec_nego();
940Sstevel@tonic-gate 
950Sstevel@tonic-gate #define	MAXHOSTS	512
960Sstevel@tonic-gate 
970Sstevel@tonic-gate #define	MNTTYPE_CACHEFS "cachefs"
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * host cache states
1010Sstevel@tonic-gate  */
1020Sstevel@tonic-gate #define	NOHOST		0
1030Sstevel@tonic-gate #define	GOODHOST	1
1040Sstevel@tonic-gate #define	DEADHOST	2
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1070Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1080Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate struct cache_entry {
1110Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1120Sstevel@tonic-gate 	char	*cache_host;
1130Sstevel@tonic-gate 	time_t	cache_time;
1140Sstevel@tonic-gate 	int	cache_state;
1150Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1160Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1170Sstevel@tonic-gate 	char	*cache_proto;
1180Sstevel@tonic-gate };
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate struct mfs_snego_t {
1210Sstevel@tonic-gate 	int sec_opt;
1220Sstevel@tonic-gate 	bool_t snego_done;
1230Sstevel@tonic-gate 	char *nfs_flavor;
1240Sstevel@tonic-gate 	seconfig_t nfs_sec;
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1290Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1300Sstevel@tonic-gate 
1313391Ssemery static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, int, uid_t,
1323391Ssemery 	action_list *);
1330Sstevel@tonic-gate static int is_nfs_port(char *);
1340Sstevel@tonic-gate 
13511291SRobert.Thurlow@Sun.COM static void netbuf_free(struct netbuf *);
1360Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1370Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1380Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1390Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1400Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1410Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1440Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1450Sstevel@tonic-gate 	bool_t, char *);
1460Sstevel@tonic-gate 
1471676Sjpk static int create_homedir(const char *, const char *);
1481676Sjpk 
1490Sstevel@tonic-gate enum type_of_stuff {
1500Sstevel@tonic-gate 	SERVER_ADDR = 0,
1510Sstevel@tonic-gate 	SERVER_PING = 1,
1520Sstevel@tonic-gate 	SERVER_FH = 2
1530Sstevel@tonic-gate };
1540Sstevel@tonic-gate 
15511291SRobert.Thurlow@Sun.COM static void *get_server_netinfo(enum type_of_stuff, char *, rpcprog_t,
1560Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1570Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
15811291SRobert.Thurlow@Sun.COM static void *get_netconfig_info(enum type_of_stuff, char *, rpcprog_t,
15911291SRobert.Thurlow@Sun.COM 	rpcvers_t, struct netconfig *, ushort_t, struct t_info *,
16011291SRobert.Thurlow@Sun.COM 	struct t_bind *, caddr_t *, bool_t, char *, enum clnt_stat *,
16111291SRobert.Thurlow@Sun.COM 	mfs_snego_t *);
16211291SRobert.Thurlow@Sun.COM static void *get_server_addrorping(char *, rpcprog_t, rpcvers_t,
16311291SRobert.Thurlow@Sun.COM 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
16411291SRobert.Thurlow@Sun.COM 	caddr_t *, bool_t, char *, enum clnt_stat *, int);
16511291SRobert.Thurlow@Sun.COM static void *get_server_fh(char *, rpcprog_t, rpcvers_t, mfs_snego_t *,
16611291SRobert.Thurlow@Sun.COM 	struct netconfig *, ushort_t, struct t_info *, struct t_bind *,
1670Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1700Sstevel@tonic-gate void free_mfs(struct mapfs *);
1710Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1720Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1730Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1740Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1750Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
1762170Sevanl void destroy_auth_client_handle(CLIENT *cl);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifdef CACHE_DEBUG
1790Sstevel@tonic-gate static void trace_host_cache();
1800Sstevel@tonic-gate static void trace_portmap_cache();
1810Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate static int rpc_timeout = 20;
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate #ifdef CACHE_DEBUG
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1880Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1890Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate static int host_cache_accesses = 0;
1920Sstevel@tonic-gate static int host_cache_lookups = 0;
1930Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1940Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
1980Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
1990Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
2000Sstevel@tonic-gate  */
2010Sstevel@tonic-gate static int portmap_cache_accesses = 0;
2020Sstevel@tonic-gate static int portmap_cache_lookups = 0;
2030Sstevel@tonic-gate static int portmap_cache_hits = 0;
2040Sstevel@tonic-gate #endif /* CACHE_DEBUG */
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2080Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2090Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2100Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2110Sstevel@tonic-gate  */
2120Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2130Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * list of support services needed
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2190Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate static void read_default_nfs(void);
2220Sstevel@tonic-gate static int is_v4_mount(char *);
2230Sstevel@tonic-gate static void start_nfs4cbd(void);
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate int
mount_nfs(struct mapent * me,char * mntpnt,char * prevhost,int overlay,uid_t uid,action_list ** alpp)2262170Sevanl mount_nfs(
2272170Sevanl 	struct mapent *me,
2282170Sevanl 	char *mntpnt,
2292170Sevanl 	char *prevhost,
2302170Sevanl 	int overlay,
2313391Ssemery 	uid_t uid,
2322170Sevanl 	action_list **alpp)
2330Sstevel@tonic-gate {
2340Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2350Sstevel@tonic-gate 	int err = -1;
2360Sstevel@tonic-gate 	int cached;
2372170Sevanl 	action_list *alp;
23812880SNagakiran.Rajashekar@Sun.COM 	char *dir;
2392170Sevanl 
2402170Sevanl 
2412170Sevanl 	alp = *alpp;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	read_default_nfs();
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2460Sstevel@tonic-gate 	if (mfs == NULL)
2470Sstevel@tonic-gate 		return (ENOENT);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate 	/*
2500Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2510Sstevel@tonic-gate 	 * works, we will fall back to NFS
2520Sstevel@tonic-gate 	 */
2530Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2540Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2550Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2560Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
2574590Srm15945 				    mntpnt, me->map_mntopts, overlay);
2580Sstevel@tonic-gate 				if (err) {
2590Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2600Sstevel@tonic-gate 				} else {
2612170Sevanl 					/*
2622170Sevanl 					 * Free action_list if there
2632170Sevanl 					 * is one as it is not needed.
2642170Sevanl 					 * Make sure to set alpp to null
2652170Sevanl 					 * so caller doesn't try to free it
2662170Sevanl 					 * again.
2672170Sevanl 					 */
2682170Sevanl 					if (*alpp) {
2692170Sevanl 						free(*alpp);
2702170Sevanl 						*alpp = NULL;
2712170Sevanl 					}
2720Sstevel@tonic-gate 					break;
2730Sstevel@tonic-gate 				}
2740Sstevel@tonic-gate 			}
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 	if (err) {
2780Sstevel@tonic-gate 		cached = strcmp(me->map_mounter, MNTTYPE_CACHEFS) == 0;
27912880SNagakiran.Rajashekar@Sun.COM 		dir = strdup(mfs->mfs_dir);
2800Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
2814590Srm15945 		    cached, overlay, uid, alp);
2820Sstevel@tonic-gate 		if (err && trace > 1) {
28312880SNagakiran.Rajashekar@Sun.COM 			trace_prt(1, "  Couldn't mount %s:%s, err=%d\n",
28412880SNagakiran.Rajashekar@Sun.COM 			    mfs->mfs_host ? mfs->mfs_host : "",
28512880SNagakiran.Rajashekar@Sun.COM 			    mfs->mfs_dir ? mfs->mfs_dir : dir, err);
2860Sstevel@tonic-gate 		}
28712880SNagakiran.Rajashekar@Sun.COM 		free(dir);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 	free_mfs(mfs);
2900Sstevel@tonic-gate 	return (err);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2960Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate static struct mapfs *
get_mysubnet_servers(struct mapfs * mfs_in)3000Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
3010Sstevel@tonic-gate {
3020Sstevel@tonic-gate 	int s;
3030Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate 	struct netconfig *nconf;
3060Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
3070Sstevel@tonic-gate 	struct nd_hostserv hs;
3080Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
3090Sstevel@tonic-gate 	struct netbuf *nb;
3100Sstevel@tonic-gate 	struct sioc_addrreq areq;
3110Sstevel@tonic-gate 	int res;
3120Sstevel@tonic-gate 	int af;
3130Sstevel@tonic-gate 	int i;
3140Sstevel@tonic-gate 	int sa_size;
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
3190Sstevel@tonic-gate 		nc = setnetconfig();
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 			/*
3240Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
3250Sstevel@tonic-gate 			 * indicates if we have already covered this
3260Sstevel@tonic-gate 			 * protocol family. If so skip it
3270Sstevel@tonic-gate 			 */
3280Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
3294590Srm15945 			    (strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
3304590Srm15945 			    (nconf->nc_semantics == NC_TPI_CLTS)) {
3310Sstevel@tonic-gate 			} else
3320Sstevel@tonic-gate 				continue;
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3370Sstevel@tonic-gate 				continue;
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 			/*
3400Sstevel@tonic-gate 			 * For each host address see if it's on our
3410Sstevel@tonic-gate 			 * local subnet.
3420Sstevel@tonic-gate 			 */
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3450Sstevel@tonic-gate 				af = AF_INET6;
3460Sstevel@tonic-gate 			else
3470Sstevel@tonic-gate 				af = AF_INET;
3480Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3490Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3500Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3510Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
3524590Srm15945 				    sizeof (areq.sa_addr)));
3530Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3540Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
3554590Srm15945 					    &mfs_head, &mfs_tail);
3560Sstevel@tonic-gate 					if (!p) {
3570Sstevel@tonic-gate 						netdir_free(retaddrs,
3584590Srm15945 						    ND_ADDRLIST);
3590Sstevel@tonic-gate 						endnetconfig(nc);
3600Sstevel@tonic-gate 						return (NULL);
3610Sstevel@tonic-gate 					}
3620Sstevel@tonic-gate 					break;
3630Sstevel@tonic-gate 				}
3640Sstevel@tonic-gate 			}  /* end of every host */
3650Sstevel@tonic-gate 			if (trace > 2) {
3660Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
3674590Srm15945 				    "netid=%s res=%s\n", mfs->mfs_host,
3684590Srm15945 				    nconf->nc_netid, res == 1?"SUC":"FAIL");
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3720Sstevel@tonic-gate 		} /* end of while */
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate 		endnetconfig(nc);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	} /* end of every map */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	return (mfs_head);
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate }
3810Sstevel@tonic-gate 
3820Sstevel@tonic-gate int
subnet_test(int af,struct sioc_addrreq * areq)3830Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	int s;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3880Sstevel@tonic-gate 		return (0);
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	areq->sa_res = -1;
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3940Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3950Sstevel@tonic-gate 		return (0);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 	close(s);
3980Sstevel@tonic-gate 	if (areq->sa_res == 1)
3990Sstevel@tonic-gate 		return (1);
4000Sstevel@tonic-gate 	else
4010Sstevel@tonic-gate 		return (0);
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate /*
4070Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate static struct mapfs *
sort_servers(struct mapfs * mfs_in,int timeout)4100Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
4110Sstevel@tonic-gate {
4120Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
4130Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	if (!mfs_in)
4160Sstevel@tonic-gate 		return (NULL);
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (!m1) {
4210Sstevel@tonic-gate 		char buff[2048] = {'\0'};
4220Sstevel@tonic-gate 
4230Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
4240Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
4250Sstevel@tonic-gate 			if (m1->mfs_next)
4260Sstevel@tonic-gate 				(void) strcat(buff, ",");
4270Sstevel@tonic-gate 		}
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
4304590Srm15945 		    buff, clnt_sperrno(clnt_stat));
4310Sstevel@tonic-gate 	}
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	return (m1);
4340Sstevel@tonic-gate }
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /*
4370Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4380Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4390Sstevel@tonic-gate  * already seen.
4400Sstevel@tonic-gate  */
4410Sstevel@tonic-gate struct mapfs *
add_mfs(struct mapfs * mfs,int distance,struct mapfs ** mfs_head,struct mapfs ** mfs_tail)4420Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4430Sstevel@tonic-gate 	struct mapfs **mfs_tail)
4440Sstevel@tonic-gate {
4450Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4480Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4490Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
4504590Srm15945 		    mfs->mfs_ignore)
4510Sstevel@tonic-gate 			return (*mfs_head);
4520Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4530Sstevel@tonic-gate 	if (!new) {
4540Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4550Sstevel@tonic-gate 		return (NULL);
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4580Sstevel@tonic-gate 	new->mfs_next = NULL;
4590Sstevel@tonic-gate 	if (distance)
4600Sstevel@tonic-gate 		new->mfs_distance = distance;
4610Sstevel@tonic-gate 	if (!*mfs_head)
4620Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4630Sstevel@tonic-gate 	else {
4640Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4650Sstevel@tonic-gate 		*mfs_tail = new;
4660Sstevel@tonic-gate 	}
4670Sstevel@tonic-gate 	return (*mfs_head);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate static void
dump_mfs(struct mapfs * mfs,char * message,int level)4710Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate 	struct mapfs *m1;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	if (trace <= level)
4760Sstevel@tonic-gate 		return;
4770Sstevel@tonic-gate 
4780Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4790Sstevel@tonic-gate 	if (!mfs) {
4800Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4810Sstevel@tonic-gate 		return;
4820Sstevel@tonic-gate 	}
4830Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4840Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4850Sstevel@tonic-gate 	trace_prt(0, "\n");
4860Sstevel@tonic-gate }
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate static char *
dump_distance(struct mapfs * mfs)4890Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4900Sstevel@tonic-gate {
4910Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4920Sstevel@tonic-gate 	case 0:			return ("zero");
4930Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4940Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4950Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4960Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
4970Sstevel@tonic-gate 	default:		return ("other");
4980Sstevel@tonic-gate 	}
4990Sstevel@tonic-gate }
5000Sstevel@tonic-gate 
5010Sstevel@tonic-gate /*
5020Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
5030Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
5040Sstevel@tonic-gate  */
5050Sstevel@tonic-gate static struct mapfs *
filter_mfs(struct mapfs * raw,struct mapfs * filter)5060Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
5090Sstevel@tonic-gate 	int skip;
5100Sstevel@tonic-gate 
5110Sstevel@tonic-gate 	if (!raw)
5120Sstevel@tonic-gate 		return (NULL);
5130Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
5140Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
5150Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
5160Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
5170Sstevel@tonic-gate 				skip = 1;
5180Sstevel@tonic-gate 				break;
5190Sstevel@tonic-gate 			}
5200Sstevel@tonic-gate 		}
5210Sstevel@tonic-gate 		if (skip)
5220Sstevel@tonic-gate 			continue;
5230Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
5240Sstevel@tonic-gate 		if (!p)
5250Sstevel@tonic-gate 			return (NULL);
5260Sstevel@tonic-gate 	}
5270Sstevel@tonic-gate 	return (mfs_head);
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate /*
5310Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5320Sstevel@tonic-gate  */
5330Sstevel@tonic-gate void
free_mfs(struct mapfs * mfs)5340Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5350Sstevel@tonic-gate {
5360Sstevel@tonic-gate 	struct mapfs *tmp;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 	while (mfs) {
5390Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5400Sstevel@tonic-gate 		free(mfs);
5410Sstevel@tonic-gate 		mfs = tmp;
5420Sstevel@tonic-gate 	}
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate 
5450Sstevel@tonic-gate /*
5460Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5470Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5480Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5490Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5500Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5510Sstevel@tonic-gate  * have ENOUGH or more entries.
5520Sstevel@tonic-gate  */
5530Sstevel@tonic-gate static struct mapfs *
enum_servers(struct mapent * me,char * preferred)5540Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5550Sstevel@tonic-gate {
5560Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 	/*
5590Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5600Sstevel@tonic-gate 	 */
5610Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5620Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5630Sstevel@tonic-gate 		if (!p)
5640Sstevel@tonic-gate 			return (NULL);
5650Sstevel@tonic-gate 		return (mfs_head);
5660Sstevel@tonic-gate 	}
5670Sstevel@tonic-gate 
5680Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5690Sstevel@tonic-gate 
5700Sstevel@tonic-gate 	/*
5710Sstevel@tonic-gate 	 * get addresses & see if any are myself
5720Sstevel@tonic-gate 	 * or were mounted from previously in a
5730Sstevel@tonic-gate 	 * hierarchical mount.
5740Sstevel@tonic-gate 	 */
5750Sstevel@tonic-gate 	if (trace > 2)
5760Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5770Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5780Sstevel@tonic-gate 		if (m1->mfs_ignore)
5790Sstevel@tonic-gate 			continue;
5800Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5810Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5820Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5830Sstevel@tonic-gate 			if (!p)
5840Sstevel@tonic-gate 				return (NULL);
5850Sstevel@tonic-gate 		}
5860Sstevel@tonic-gate 	}
5870Sstevel@tonic-gate 	if (trace > 2 && m1)
5880Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
5894590Srm15945 		    m1->mfs_host);
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	/*
5920Sstevel@tonic-gate 	 * look for entries on this subnet
5930Sstevel@tonic-gate 	 */
5940Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5950Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5960Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
5970Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
5980Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
5990Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
6000Sstevel@tonic-gate 		free_mfs(m1);
6010Sstevel@tonic-gate 		m1 = m2;
6020Sstevel@tonic-gate 	}
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6050Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
6060Sstevel@tonic-gate 		if (!p)
6070Sstevel@tonic-gate 			return (NULL);
6080Sstevel@tonic-gate 	}
6090Sstevel@tonic-gate 	if (m1)
6100Sstevel@tonic-gate 		free_mfs(m1);
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * add the rest of the entries at the end
6140Sstevel@tonic-gate 	 */
6150Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
6160Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
6170Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
6180Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
6190Sstevel@tonic-gate 	if (m1)
6200Sstevel@tonic-gate 		free_mfs(m1);
6210Sstevel@tonic-gate 	m1 = m2;
6220Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
6230Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
6240Sstevel@tonic-gate 		if (!p)
6250Sstevel@tonic-gate 			return (NULL);
6260Sstevel@tonic-gate 	}
6270Sstevel@tonic-gate 	if (m1)
6280Sstevel@tonic-gate 		free_mfs(m1);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate done:
6310Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6320Sstevel@tonic-gate 	return (mfs_head);
6330Sstevel@tonic-gate }
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate static enum nfsstat
nfsmount(struct mapfs * mfs_in,char * mntpnt,char * opts,int cached,int overlay,uid_t uid,action_list * alp)6362170Sevanl nfsmount(
6372170Sevanl 	struct mapfs *mfs_in,
6382170Sevanl 	char *mntpnt, char *opts,
6392170Sevanl 	int cached, int overlay,
6403391Ssemery 	uid_t uid,
6412170Sevanl 	action_list *alp)
6420Sstevel@tonic-gate {
6430Sstevel@tonic-gate 	CLIENT *cl;
6440Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6450Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6460Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
6472170Sevanl 	char	*mntopts = NULL;
6480Sstevel@tonic-gate 	int mnttabcnt = 0;
6490Sstevel@tonic-gate 	int loglevel;
6500Sstevel@tonic-gate 	struct mnttab m;
6510Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
6524590Srm15945 	    *prevhead, *prevtail;
6530Sstevel@tonic-gate 	int flags;
6540Sstevel@tonic-gate 	struct fhstatus fhs;
6550Sstevel@tonic-gate 	struct timeval timeout;
6560Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6570Sstevel@tonic-gate 	enum nfsstat status;
6580Sstevel@tonic-gate 	struct stat stbuf;
6590Sstevel@tonic-gate 	struct netconfig *nconf;
6600Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6610Sstevel@tonic-gate 				/* and mount version with mountd */
6620Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6630Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6640Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6670Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6680Sstevel@tonic-gate 	int posix;
6690Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6700Sstevel@tonic-gate 	struct mountres3 res3;
6710Sstevel@tonic-gate 	nfs_fh3 fh3;
6720Sstevel@tonic-gate 	char *fstype;
6730Sstevel@tonic-gate 	int count, i;
6740Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6750Sstevel@tonic-gate 	int *auths;
6760Sstevel@tonic-gate 	int delay;
6770Sstevel@tonic-gate 	int retries;
6780Sstevel@tonic-gate 	char *nfs_proto = NULL;
6790Sstevel@tonic-gate 	uint_t nfs_port = 0;
6803767Sjs195444 	char *p, *host, *rhost, *dir;
6810Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6820Sstevel@tonic-gate 	int error, last_error = 0;
6830Sstevel@tonic-gate 	int replicated;
6840Sstevel@tonic-gate 	int entries = 0;
6850Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6860Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6870Sstevel@tonic-gate 	int skipentry = 0;
6880Sstevel@tonic-gate 	char *nfs_flavor;
6890Sstevel@tonic-gate 	seconfig_t nfs_sec;
6900Sstevel@tonic-gate 	int sec_opt, scerror;
6910Sstevel@tonic-gate 	struct sec_data *secdata;
6920Sstevel@tonic-gate 	int secflags;
6930Sstevel@tonic-gate 	struct netbuf *syncaddr;
6940Sstevel@tonic-gate 	bool_t	use_pubfh;
6950Sstevel@tonic-gate 	ushort_t thisport;
6960Sstevel@tonic-gate 	int got_val;
6970Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
7000Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
7010Sstevel@tonic-gate 	m.mnt_mntopts = opts;
7020Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
7030Sstevel@tonic-gate 		if (verbose)
7040Sstevel@tonic-gate 			syslog(LOG_WARNING,
7050Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
7060Sstevel@tonic-gate 		replicated = 0;
7070Sstevel@tonic-gate 	}
7080Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
7090Sstevel@tonic-gate 		if (verbose)
7100Sstevel@tonic-gate 			syslog(LOG_WARNING,
7110Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
7120Sstevel@tonic-gate 			    mntpnt);
7130Sstevel@tonic-gate 		replicated = 0;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 	if (replicated && cached) {
7160Sstevel@tonic-gate 		if (verbose)
7170Sstevel@tonic-gate 			syslog(LOG_WARNING,
7180Sstevel@tonic-gate 		    "mount on %s is cached and will not be replicated.",
7194590Srm15945 			    mntpnt);
7200Sstevel@tonic-gate 		replicated = 0;
7210Sstevel@tonic-gate 	}
7220Sstevel@tonic-gate 	if (replicated)
7230Sstevel@tonic-gate 		loglevel = LOG_WARNING;
7240Sstevel@tonic-gate 	else
7250Sstevel@tonic-gate 		loglevel = LOG_ERR;
7260Sstevel@tonic-gate 
7270Sstevel@tonic-gate 	if (trace > 1) {
7280Sstevel@tonic-gate 		if (replicated)
7290Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
7304590Srm15945 			    mntpnt, opts);
7310Sstevel@tonic-gate 		else
7320Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
7334590Srm15945 			    mntpnt, opts);
7340Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7350Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
7364590Srm15945 			    mfs->mfs_host, mfs->mfs_dir);
7370Sstevel@tonic-gate 	}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7430Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7440Sstevel@tonic-gate 		return (NFSERR_NOENT);
7450Sstevel@tonic-gate 	}
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7490Sstevel@tonic-gate 	 */
7500Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7510Sstevel@tonic-gate 		return (NFSERR_NOENT);
7520Sstevel@tonic-gate 	}
7530Sstevel@tonic-gate 
7540Sstevel@tonic-gate 	/*
7550Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7560Sstevel@tonic-gate 	 */
7570Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7580Sstevel@tonic-gate 	if (!got_val)
7590Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7600Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7610Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7620Sstevel@tonic-gate 		return (NFSERR_NOENT);
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7670Sstevel@tonic-gate 	 */
7680Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7690Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7700Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7710Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7720Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7730Sstevel@tonic-gate 		/* direct mount point without offsets */
7740Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7790Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7800Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7810Sstevel@tonic-gate 			syslog(loglevel,
7820Sstevel@tonic-gate 			    "conflicting security options");
7830Sstevel@tonic-gate 			return (NFSERR_IO);
7840Sstevel@tonic-gate 		}
7850Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7860Sstevel@tonic-gate 			syslog(loglevel,
7870Sstevel@tonic-gate 			    "error getting dh information from %s",
7880Sstevel@tonic-gate 			    NFSSEC_CONF);
7890Sstevel@tonic-gate 			return (NFSERR_IO);
7900Sstevel@tonic-gate 		}
7910Sstevel@tonic-gate 	}
7920Sstevel@tonic-gate 
7934955Sjasmith 	if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
7940Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
7954590Srm15945 		    &mfssnego_init.nfs_flavor)) == -1) {
7960Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
7970Sstevel@tonic-gate 			return (NFSERR_IO);
7980Sstevel@tonic-gate 		}
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
8020Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
8030Sstevel@tonic-gate 			syslog(loglevel,
8040Sstevel@tonic-gate 			    "conflicting security options");
8050Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8060Sstevel@tonic-gate 			return (NFSERR_IO);
8070Sstevel@tonic-gate 		}
8080Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
8094590Srm15945 		    &mfssnego_init.nfs_sec)) {
8100Sstevel@tonic-gate 			syslog(loglevel,
8110Sstevel@tonic-gate 			    "error getting %s information from %s",
8120Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
8130Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
8140Sstevel@tonic-gate 			return (NFSERR_IO);
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
8170Sstevel@tonic-gate 	}
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate nextentry:
8200Sstevel@tonic-gate 	skipentry = 0;
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
8230Sstevel@tonic-gate 	if (!got_val)
8240Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8250Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8260Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
8274590Srm15945 		    mntpnt);
8280Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8290Sstevel@tonic-gate 		goto ret;
8300Sstevel@tonic-gate 	}
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	if (nfsvers != 0) {
8330Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8340Sstevel@tonic-gate 	} else {
8350Sstevel@tonic-gate 		pubversmax = vers;
8360Sstevel@tonic-gate 		pubversmin = versmin;
8370Sstevel@tonic-gate 	}
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 	/*
8400Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8410Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8420Sstevel@tonic-gate 	 * homogeneous with respect to version.
8430Sstevel@tonic-gate 	 *
8440Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8450Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8460Sstevel@tonic-gate 	 *
8470Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8480Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8490Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8500Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8510Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8520Sstevel@tonic-gate 	 * subnet.
8530Sstevel@tonic-gate 	 */
8540Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8580Sstevel@tonic-gate 			continue;
8590Sstevel@tonic-gate 
8603767Sjs195444 		/*
8613767Sjs195444 		 * If the host is '[a:d:d:r:e:s:s'],
8623767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
8633767Sjs195444 		 */
8643767Sjs195444 		host = strdup(mfs->mfs_host);
8653767Sjs195444 		if (host == NULL) {
8663767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
8673767Sjs195444 			last_error = NFSERR_IO;
8683767Sjs195444 			goto out;
8693767Sjs195444 		}
8703767Sjs195444 		unbracket(&host);
8713767Sjs195444 
8720Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8750Sstevel@tonic-gate 			char *path;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8780Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
8814590Srm15945 				    " not the same as port (%d) in port "
8824590Srm15945 				    "option\n", mfs->mfs_port, nfs_port);
8830Sstevel@tonic-gate 				last_error = NFSERR_IO;
8840Sstevel@tonic-gate 				goto out;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 			} else if (nfs_port != 0)
8870Sstevel@tonic-gate 				thisport = nfs_port;
8880Sstevel@tonic-gate 			else
8890Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8900Sstevel@tonic-gate 
8910Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8940Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8950Sstevel@tonic-gate 				if (path == NULL) {
8960Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
8970Sstevel@tonic-gate 					last_error = NFSERR_IO;
8980Sstevel@tonic-gate 					goto out;
8990Sstevel@tonic-gate 				}
9000Sstevel@tonic-gate 				path[0] = (char)WNL_NATIVEPATH;
9010Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
9020Sstevel@tonic-gate 			} else {
9030Sstevel@tonic-gate 				path = dir;
9040Sstevel@tonic-gate 			}
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate 			argp = (struct nfs_args *)
9074590Srm15945 			    malloc(sizeof (struct nfs_args));
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate 			if (!argp) {
9100Sstevel@tonic-gate 				if (path != dir)
9110Sstevel@tonic-gate 					free(path);
9120Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
9130Sstevel@tonic-gate 				last_error = NFSERR_IO;
9140Sstevel@tonic-gate 				goto out;
9150Sstevel@tonic-gate 			}
9160Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 			/*
9190Sstevel@tonic-gate 			 * RDMA support
9200Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
9210Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
9220Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
9230Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
9240Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
9250Sstevel@tonic-gate 			 */
9260Sstevel@tonic-gate 			if (nfs_proto) {
9270Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
9280Sstevel@tonic-gate 					free(nfs_proto);
9290Sstevel@tonic-gate 					nfs_proto = NULL;
9300Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
9310Sstevel@tonic-gate 				}
9320Sstevel@tonic-gate 			} else
9330Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9360Sstevel@tonic-gate 			    pubvers--) {
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 				nconf = NULL;
9390Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9400Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9410Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9420Sstevel@tonic-gate 
9430Sstevel@tonic-gate 				if (argp->addr != NULL)
9440Sstevel@tonic-gate 					break;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 				if (nconf != NULL)
9470Sstevel@tonic-gate 					freenetconfigent(nconf);
9480Sstevel@tonic-gate 			}
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 			if (path != dir)
9510Sstevel@tonic-gate 				free(path);
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 			if (argp->addr != NULL) {
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 				/*
9560Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9570Sstevel@tonic-gate 				 * mounts is not required since file
9580Sstevel@tonic-gate 				 * locking is included within the protocol
9590Sstevel@tonic-gate 				 */
9600Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9610Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9640Sstevel@tonic-gate 
9654590Srm15945 				vers = pubvers;
9660Sstevel@tonic-gate 				mfs->mfs_args = argp;
9670Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9680Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9690Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 			} else {
9720Sstevel@tonic-gate 				free(argp);
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 				/*
9750Sstevel@tonic-gate 				 * If -public was specified, give up
9760Sstevel@tonic-gate 				 * on this entry now.
9770Sstevel@tonic-gate 				 */
9780Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9790Sstevel@tonic-gate 					syslog(loglevel,
9800Sstevel@tonic-gate 					    "%s: no public file handle support",
9810Sstevel@tonic-gate 					    host);
9820Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9830Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9840Sstevel@tonic-gate 					continue;
9850Sstevel@tonic-gate 				}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 				/*
9880Sstevel@tonic-gate 				 * Back off to a conventional mount.
9890Sstevel@tonic-gate 				 *
9900Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9910Sstevel@tonic-gate 				 * rid of them.
9920Sstevel@tonic-gate 				 */
9930Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9940Sstevel@tonic-gate 
9950Sstevel@tonic-gate 				if (path == NULL) {
9960Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
9970Sstevel@tonic-gate 					last_error = NFSERR_IO;
9980Sstevel@tonic-gate 					goto out;
9990Sstevel@tonic-gate 				}
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 				strcpy(path, dir);
10020Sstevel@tonic-gate 				URLparse(path);
10030Sstevel@tonic-gate 				mfs->mfs_dir = path;
10040Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
10050Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
10060Sstevel@tonic-gate 			}
10070Sstevel@tonic-gate 		}
10080Sstevel@tonic-gate 
10090Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
10100Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
10114590Srm15945 			    0, FALSE, NULL, nfs_proto);
10120Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
10130Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
10140Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
10154590Srm15945 					    "protocol version mismatch",
10164590Srm15945 					    host);
10170Sstevel@tonic-gate 				} else {
10180Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
10194590Srm15945 					    "responding", host);
10200Sstevel@tonic-gate 				}
10210Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10220Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10230Sstevel@tonic-gate 				continue;
10240Sstevel@tonic-gate 			}
10250Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
10260Sstevel@tonic-gate 				if (nfs_proto == NULL)
10270Sstevel@tonic-gate 					syslog(loglevel,
10284590Srm15945 					    "NFS version %d "
10294590Srm15945 					    "not supported by %s",
10304590Srm15945 					    nfsvers, host);
10310Sstevel@tonic-gate 				else
10320Sstevel@tonic-gate 					syslog(loglevel,
10334590Srm15945 					    "NFS version %d "
10344590Srm15945 					    "with proto %s "
10354590Srm15945 					    "not supported by %s",
10364590Srm15945 					    nfsvers, nfs_proto, host);
10370Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10380Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10390Sstevel@tonic-gate 				continue;
10400Sstevel@tonic-gate 			}
10410Sstevel@tonic-gate 		}
10420Sstevel@tonic-gate 
10433767Sjs195444 		free(host);
10443767Sjs195444 
10450Sstevel@tonic-gate 		switch (vers) {
10460Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10470Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10480Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10490Sstevel@tonic-gate 		default: break;
10500Sstevel@tonic-gate 		}
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 		/*
10530Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10540Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10550Sstevel@tonic-gate 		 * can't hurt.
10560Sstevel@tonic-gate 		 */
10570Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10580Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10590Sstevel@tonic-gate 			switch (vers) {
10600Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10610Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10620Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10630Sstevel@tonic-gate 			default: break;
10640Sstevel@tonic-gate 			}
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 		/*
10680Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10690Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10700Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10713767Sjs195444 		 * to consider another entry if we can't get
10720Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10730Sstevel@tonic-gate 		 */
10740Sstevel@tonic-gate 		if (!replicated)
10750Sstevel@tonic-gate 			break;
10763767Sjs195444 
10770Sstevel@tonic-gate 	}
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	if (nfsvers == 0) {
10800Sstevel@tonic-gate 		/*
10810Sstevel@tonic-gate 		 * Choose the NFS version.
10820Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10830Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10840Sstevel@tonic-gate 		 * network interface and avoid a router.
10850Sstevel@tonic-gate 		 */
10860Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10870Sstevel@tonic-gate 			nfsvers = NFS_V4;
10880Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10890Sstevel@tonic-gate 			nfsvers = NFS_V3;
10900Sstevel@tonic-gate 		else
10910Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10920Sstevel@tonic-gate 		if (trace > 2)
10930Sstevel@tonic-gate 			trace_prt(1,
10940Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10950Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10960Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
11010Sstevel@tonic-gate 	 * mounts, set fstype now.
11020Sstevel@tonic-gate 	 * Also take the opportunity to set
11030Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
11040Sstevel@tonic-gate 	 */
11050Sstevel@tonic-gate 	switch (nfsvers) {
11060Sstevel@tonic-gate 	case NFS_V4:
11070Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
11080Sstevel@tonic-gate 		break;
11090Sstevel@tonic-gate 	case NFS_V3:
11100Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
11110Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11120Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
11130Sstevel@tonic-gate 			versmin = MOUNTVERS3;
11140Sstevel@tonic-gate 		}
11150Sstevel@tonic-gate 		break;
11160Sstevel@tonic-gate 	case NFS_VERSION:
11170Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
11180Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
11190Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
11200Sstevel@tonic-gate 			versmin = MOUNTVERS;
11210Sstevel@tonic-gate 		}
11220Sstevel@tonic-gate 		break;
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	/*
11260Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
11270Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
11280Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
11290Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
11300Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
11310Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
11320Sstevel@tonic-gate 	 * where we found them.
11330Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
11340Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
11350Sstevel@tonic-gate 	 */
11360Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 		/*
11390Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11400Sstevel@tonic-gate 		 */
11410Sstevel@tonic-gate 		retries = get_retry(opts);
11420Sstevel@tonic-gate 		delay = INITDELAY;
11430Sstevel@tonic-gate retry:
11440Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11450Sstevel@tonic-gate 			continue;
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 		/*
11480Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11490Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11500Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11510Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11520Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11530Sstevel@tonic-gate 		 * code.
11540Sstevel@tonic-gate 		 */
11550Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11560Sstevel@tonic-gate 		    !replicated && skipentry)
11570Sstevel@tonic-gate 			goto nextentry;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 		vers = mountversmax;
11600Sstevel@tonic-gate 		host = mfs->mfs_host;
11610Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11623767Sjs195444 
11633767Sjs195444 		/*
11643767Sjs195444 		 * Remember the possible '[a:d:d:r:e:s:s]' as the address to be
11653767Sjs195444 		 * later passed to mount(2) and used in the mnttab line, but
11663767Sjs195444 		 * only use 'a:d:d:r:e:s:s' for communication
11673767Sjs195444 		 */
11683767Sjs195444 		rhost = strdup(host);
11693767Sjs195444 		if (rhost == NULL) {
11703767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
11713767Sjs195444 			last_error = NFSERR_IO;
11723767Sjs195444 			goto out;
11733767Sjs195444 		}
11743767Sjs195444 		unbracket(&host);
11753767Sjs195444 
11763767Sjs195444 		(void) sprintf(remname, "%s:%s", rhost, dir);
11770Sstevel@tonic-gate 		if (trace > 4 && replicated)
11780Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 		/*
11810Sstevel@tonic-gate 		 * If it's cached we need to get cachefs to mount it.
11820Sstevel@tonic-gate 		 */
11830Sstevel@tonic-gate 		if (cached) {
11840Sstevel@tonic-gate 			char *copts = opts;
11850Sstevel@tonic-gate 
11860Sstevel@tonic-gate 			/*
11870Sstevel@tonic-gate 			 * If we started with a URL we need to turn on
11880Sstevel@tonic-gate 			 * -o public if not on already
11890Sstevel@tonic-gate 			 */
11900Sstevel@tonic-gate 			if (use_pubfh == FALSE &&
11910Sstevel@tonic-gate 			    (mfs->mfs_flags & MFS_FH_VIA_WEBNFS)) {
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 				copts = malloc(strlen(opts) +
11944590Srm15945 				    strlen(",public")+1);
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 				if (copts == NULL) {
11970Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
11980Sstevel@tonic-gate 					last_error = NFSERR_IO;
11990Sstevel@tonic-gate 					goto out;
12000Sstevel@tonic-gate 				}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 				strcpy(copts, opts);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 				if (strlen(copts) != 0)
12050Sstevel@tonic-gate 					strcat(copts, ",");
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 				strcat(copts, "public");
12080Sstevel@tonic-gate 			}
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 			last_error = mount_generic(remname, MNTTYPE_CACHEFS,
12114590Srm15945 			    copts, mntpnt, overlay);
12120Sstevel@tonic-gate 
12130Sstevel@tonic-gate 			if (copts != opts)
12140Sstevel@tonic-gate 				free(copts);
12150Sstevel@tonic-gate 
12160Sstevel@tonic-gate 			if (last_error) {
12170Sstevel@tonic-gate 				skipentry = 1;
12180Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12190Sstevel@tonic-gate 				continue;
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 			goto out;
12220Sstevel@tonic-gate 		}
12230Sstevel@tonic-gate 
12240Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 			/*
12270Sstevel@tonic-gate 			 * Allocate nfs_args structure
12280Sstevel@tonic-gate 			 */
12290Sstevel@tonic-gate 			argp = (struct nfs_args *)
12304590Srm15945 			    malloc(sizeof (struct nfs_args));
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 			if (!argp) {
12330Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
12340Sstevel@tonic-gate 				last_error = NFSERR_IO;
12350Sstevel@tonic-gate 				goto out;
12360Sstevel@tonic-gate 			}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 			/*
12410Sstevel@tonic-gate 			 * RDMA support
12420Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
12430Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
12440Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
12450Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
12460Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
12470Sstevel@tonic-gate 			 */
12480Sstevel@tonic-gate 			if (nfs_proto) {
12490Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
12500Sstevel@tonic-gate 					free(nfs_proto);
12510Sstevel@tonic-gate 					nfs_proto = NULL;
12520Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
12530Sstevel@tonic-gate 				}
12540Sstevel@tonic-gate 			} else
12550Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
12560Sstevel@tonic-gate 		} else {
12570Sstevel@tonic-gate 			argp = mfs->mfs_args;
12580Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12590Sstevel@tonic-gate 
12600Sstevel@tonic-gate 			/*
12610Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12620Sstevel@tonic-gate 			 * NFS version is wrong.
12630Sstevel@tonic-gate 			 */
12640Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12650Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 				free(argp);
12680Sstevel@tonic-gate 				skipentry = 1;
12690Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12700Sstevel@tonic-gate 				continue;
12710Sstevel@tonic-gate 			}
12720Sstevel@tonic-gate 		}
12730Sstevel@tonic-gate 
12740Sstevel@tonic-gate 		prevhead = head;
12750Sstevel@tonic-gate 		prevtail = tail;
12760Sstevel@tonic-gate 		if (!head)
12770Sstevel@tonic-gate 			head = tail = argp;
12780Sstevel@tonic-gate 		else
12790Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 		/*
12820Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12830Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12840Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12850Sstevel@tonic-gate 		 * used.
12860Sstevel@tonic-gate 		 */
12870Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
12884590Srm15945 		    nfsvers != NFS_V4) {
12894590Srm15945 			timeout.tv_usec = 0;
12904590Srm15945 			timeout.tv_sec = rpc_timeout;
12914590Srm15945 			rpc_stat = RPC_TIMEDOUT;
12924590Srm15945 
12934590Srm15945 			/* Create the client handle. */
12944590Srm15945 
12954590Srm15945 			if (trace > 1) {
12964590Srm15945 				trace_prt(1,
12974590Srm15945 				    "  nfsmount: Get mount version: request "
12984590Srm15945 				    "vers=%d min=%d\n", vers, versmin);
12990Sstevel@tonic-gate 			}
13004590Srm15945 
13014590Srm15945 			while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
13024590Srm15945 			    versmin, vers, "udp")) == NULL) {
13034590Srm15945 				if (trace > 4) {
13044590Srm15945 					trace_prt(1,
13054590Srm15945 					    "  nfsmount: Can't get mount "
13064590Srm15945 					    "version: rpcerr=%d\n",
13074590Srm15945 					    rpc_createerr.cf_stat);
13084590Srm15945 				}
13094590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
13104590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
13114590Srm15945 					break;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 			/*
13140Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
13150Sstevel@tonic-gate 			 * XXX we should be more careful and handle
13160Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
13170Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
13180Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
13190Sstevel@tonic-gate 			 */
13204590Srm15945 				vers--;
13214590Srm15945 				if (vers < versmin)
13224590Srm15945 					break;
13234590Srm15945 				if (trace > 4) {
13244590Srm15945 					trace_prt(1,
13254590Srm15945 					    "  nfsmount: Try version=%d\n",
13264590Srm15945 					    vers);
13274590Srm15945 				}
13280Sstevel@tonic-gate 			}
13290Sstevel@tonic-gate 
13304590Srm15945 			if (cl == NULL) {
13314590Srm15945 				free(argp);
13324590Srm15945 				head = prevhead;
13334590Srm15945 				tail = prevtail;
13344590Srm15945 				if (tail)
13354590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13364590Srm15945 				last_error = NFSERR_NOENT;
13374590Srm15945 
13384590Srm15945 				if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
13394590Srm15945 				    rpc_createerr.cf_stat !=
13404590Srm15945 				    RPC_PROGVERSMISMATCH &&
13414590Srm15945 				    retries-- > 0) {
13424590Srm15945 					DELAY(delay);
13434590Srm15945 					goto retry;
13444590Srm15945 				}
13454590Srm15945 
13464590Srm15945 				syslog(loglevel, "%s %s", host,
13474590Srm15945 				    clnt_spcreateerror(
13484590Srm15945 				    "server not responding"));
13494590Srm15945 				skipentry = 1;
13504590Srm15945 				mfs->mfs_ignore = 1;
13514590Srm15945 				continue;
13524590Srm15945 			}
13534590Srm15945 			if (trace > 1) {
13544590Srm15945 				trace_prt(1,
13554590Srm15945 				    "	nfsmount: mount version=%d\n", outvers);
13564590Srm15945 			}
13570Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13584590Srm15945 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
13594590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13604590Srm15945 			    __FILE__, __LINE__);
13610Sstevel@tonic-gate #endif
13620Sstevel@tonic-gate 
13634590Srm15945 			if (__clnt_bindresvport(cl) < 0) {
13644590Srm15945 				free(argp);
13654590Srm15945 				head = prevhead;
13664590Srm15945 				tail = prevtail;
13674590Srm15945 				if (tail)
13684590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13694590Srm15945 				last_error = NFSERR_NOENT;
13704590Srm15945 
13714590Srm15945 				if (retries-- > 0) {
13724590Srm15945 					destroy_auth_client_handle(cl);
13734590Srm15945 					DELAY(delay);
13744590Srm15945 					goto retry;
13754590Srm15945 				}
13764590Srm15945 
13774590Srm15945 				syslog(loglevel, "mount %s: %s", host,
13784590Srm15945 				    "Couldn't bind to reserved port");
13790Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13804590Srm15945 				skipentry = 1;
13814590Srm15945 				mfs->mfs_ignore = 1;
13824590Srm15945 				continue;
13830Sstevel@tonic-gate 			}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13864590Srm15945 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
13874590Srm15945 			    __FILE__, __LINE__);
13880Sstevel@tonic-gate #endif
13894590Srm15945 			AUTH_DESTROY(cl->cl_auth);
13904590Srm15945 			if ((cl->cl_auth = authsys_create_default()) == NULL) {
13914590Srm15945 				free(argp);
13924590Srm15945 				head = prevhead;
13934590Srm15945 				tail = prevtail;
13944590Srm15945 				if (tail)
13954590Srm15945 					tail->nfs_ext_u.nfs_extB.next = NULL;
13964590Srm15945 				last_error = NFSERR_NOENT;
13974590Srm15945 
13984590Srm15945 				if (retries-- > 0) {
13994590Srm15945 					destroy_auth_client_handle(cl);
14004590Srm15945 					DELAY(delay);
14014590Srm15945 					goto retry;
14024590Srm15945 				}
14034590Srm15945 
14044590Srm15945 				syslog(loglevel, "mount %s: %s", host,
14054590Srm15945 				    "Failed creating default auth handle");
14060Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14074590Srm15945 				skipentry = 1;
14084590Srm15945 				mfs->mfs_ignore = 1;
14094590Srm15945 				continue;
14100Sstevel@tonic-gate 			}
14110Sstevel@tonic-gate #ifdef MALLOC_DEBUG
14124590Srm15945 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
14134590Srm15945 			    __FILE__, __LINE__);
14140Sstevel@tonic-gate #endif
14150Sstevel@tonic-gate 		} else
14164590Srm15945 			cl = NULL;
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 		/*
14190Sstevel@tonic-gate 		 * set security options
14200Sstevel@tonic-gate 		 */
14210Sstevel@tonic-gate 		sec_opt = 0;
14220Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
14230Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
14240Sstevel@tonic-gate 			if (++sec_opt > 1) {
14250Sstevel@tonic-gate 				syslog(loglevel,
14260Sstevel@tonic-gate 				    "conflicting security options for %s",
14270Sstevel@tonic-gate 				    remname);
14280Sstevel@tonic-gate 				free(argp);
14290Sstevel@tonic-gate 				head = prevhead;
14300Sstevel@tonic-gate 				tail = prevtail;
14310Sstevel@tonic-gate 				if (tail)
14320Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14330Sstevel@tonic-gate 				last_error = NFSERR_IO;
14340Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14350Sstevel@tonic-gate 				skipentry = 1;
14360Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14370Sstevel@tonic-gate 				continue;
14380Sstevel@tonic-gate 			}
14390Sstevel@tonic-gate 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
14400Sstevel@tonic-gate 				syslog(loglevel,
14410Sstevel@tonic-gate 				    "error getting dh information from %s",
14420Sstevel@tonic-gate 				    NFSSEC_CONF);
14430Sstevel@tonic-gate 				free(argp);
14440Sstevel@tonic-gate 				head = prevhead;
14450Sstevel@tonic-gate 				tail = prevtail;
14460Sstevel@tonic-gate 				if (tail)
14470Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14480Sstevel@tonic-gate 				last_error = NFSERR_IO;
14490Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14500Sstevel@tonic-gate 				skipentry = 1;
14510Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14520Sstevel@tonic-gate 				continue;
14530Sstevel@tonic-gate 			}
14540Sstevel@tonic-gate 		}
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 		nfs_flavor = NULL;
14574955Sjasmith 		if (hasmntopt(&m, MNTOPT_SEC) != NULL) {
14580Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14590Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14600Sstevel@tonic-gate 				last_error = NFSERR_IO;
14610Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14620Sstevel@tonic-gate 				goto out;
14630Sstevel@tonic-gate 			}
14640Sstevel@tonic-gate 		}
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 		if (nfs_flavor) {
14670Sstevel@tonic-gate 			if (++sec_opt > 1) {
14680Sstevel@tonic-gate 				syslog(loglevel,
14690Sstevel@tonic-gate 				    "conflicting security options for %s",
14700Sstevel@tonic-gate 				    remname);
14710Sstevel@tonic-gate 				free(nfs_flavor);
14720Sstevel@tonic-gate 				free(argp);
14730Sstevel@tonic-gate 				head = prevhead;
14740Sstevel@tonic-gate 				tail = prevtail;
14750Sstevel@tonic-gate 				if (tail)
14760Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14770Sstevel@tonic-gate 				last_error = NFSERR_IO;
14780Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14790Sstevel@tonic-gate 				skipentry = 1;
14800Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14810Sstevel@tonic-gate 				continue;
14820Sstevel@tonic-gate 			}
14830Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14840Sstevel@tonic-gate 				syslog(loglevel,
14850Sstevel@tonic-gate 				    "error getting %s information from %s",
14860Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14870Sstevel@tonic-gate 				free(nfs_flavor);
14880Sstevel@tonic-gate 				free(argp);
14890Sstevel@tonic-gate 				head = prevhead;
14900Sstevel@tonic-gate 				tail = prevtail;
14910Sstevel@tonic-gate 				if (tail)
14920Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14930Sstevel@tonic-gate 				last_error = NFSERR_IO;
14940Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14950Sstevel@tonic-gate 				skipentry = 1;
14960Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14970Sstevel@tonic-gate 				continue;
14980Sstevel@tonic-gate 			}
14990Sstevel@tonic-gate 			free(nfs_flavor);
15000Sstevel@tonic-gate 		}
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
15034590Srm15945 		    hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
15064590Srm15945 		    nfsvers != NFS_V4) {
15074590Srm15945 			bool_t give_up_on_mnt;
15084590Srm15945 			bool_t got_mnt_error;
15090Sstevel@tonic-gate 		/*
15100Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
15110Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
15120Sstevel@tonic-gate 		 * with a prepended "/".
15130Sstevel@tonic-gate 		 */
15144590Srm15945 			if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
15154590Srm15945 				give_up_on_mnt = FALSE;
15164590Srm15945 			else
15174590Srm15945 				give_up_on_mnt = TRUE;
15184590Srm15945 
15194590Srm15945 			got_mnt_error = FALSE;
15200Sstevel@tonic-gate 
15210Sstevel@tonic-gate try_mnt_slash:
15224590Srm15945 			if (got_mnt_error == TRUE) {
15234590Srm15945 				int i, l;
15244590Srm15945 
15254590Srm15945 				give_up_on_mnt = TRUE;
15264590Srm15945 				l = strlen(dir);
15274590Srm15945 
15284590Srm15945 				/*
15294590Srm15945 				 * Insert a "/" to front of mfs_dir.
15304590Srm15945 				 */
15314590Srm15945 				for (i = l; i > 0; i--)
15324590Srm15945 					dir[i] = dir[i-1];
15334590Srm15945 
15344590Srm15945 				dir[0] = '/';
15354590Srm15945 			}
15364590Srm15945 
15374590Srm15945 			/* Get fhandle of remote path from server's mountd */
15384590Srm15945 
15394590Srm15945 			switch (outvers) {
15404590Srm15945 			case MOUNTVERS:
15414590Srm15945 				if (posix) {
15424590Srm15945 					free(argp);
15434590Srm15945 					head = prevhead;
15444590Srm15945 					tail = prevtail;
15454590Srm15945 					if (tail)
15464590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15474590Srm15945 						    NULL;
15484590Srm15945 					last_error = NFSERR_NOENT;
15494590Srm15945 					syslog(loglevel,
15504590Srm15945 					    "can't get posix info for %s",
15514590Srm15945 					    host);
15524590Srm15945 					destroy_auth_client_handle(cl);
15534590Srm15945 					skipentry = 1;
15544590Srm15945 					mfs->mfs_ignore = 1;
15554590Srm15945 					continue;
15564590Srm15945 				}
15574590Srm15945 		    /* FALLTHRU */
15584590Srm15945 			case MOUNTVERS_POSIX:
15594590Srm15945 				if (nfsvers == NFS_V3) {
15604590Srm15945 					free(argp);
15614590Srm15945 					head = prevhead;
15624590Srm15945 					tail = prevtail;
15634590Srm15945 					if (tail)
15644590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15654590Srm15945 						    NULL;
15664590Srm15945 					last_error = NFSERR_NOENT;
15674590Srm15945 					syslog(loglevel,
15684590Srm15945 					    "%s doesn't support NFS Version 3",
15694590Srm15945 					    host);
15704590Srm15945 					destroy_auth_client_handle(cl);
15714590Srm15945 					skipentry = 1;
15724590Srm15945 					mfs->mfs_ignore = 1;
15734590Srm15945 					continue;
15744590Srm15945 				}
15754590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15764590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
15774590Srm15945 				    xdr_fhstatus, (caddr_t)&fhs, timeout);
15784590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
15794590Srm15945 
15804590Srm15945 					if (give_up_on_mnt == FALSE) {
15814590Srm15945 						got_mnt_error = TRUE;
15824590Srm15945 						goto try_mnt_slash;
15834590Srm15945 					}
15844590Srm15945 
15854590Srm15945 				/*
15864590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
15874590Srm15945 				 * immediately following the "not responding"
15884590Srm15945 				 * is correct.
15894590Srm15945 				 */
15904590Srm15945 					free(argp);
15914590Srm15945 					head = prevhead;
15924590Srm15945 					tail = prevtail;
15934590Srm15945 					if (tail)
15944590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
15954590Srm15945 						    NULL;
15964590Srm15945 					last_error = NFSERR_NOENT;
15974590Srm15945 
15984590Srm15945 					if (retries-- > 0) {
15994590Srm15945 						destroy_auth_client_handle(cl);
16004590Srm15945 						DELAY(delay);
16014590Srm15945 						goto retry;
16024590Srm15945 					}
16034590Srm15945 
16044590Srm15945 					if (trace > 3) {
16054590Srm15945 						trace_prt(1,
16064590Srm15945 						    "  nfsmount: mount RPC "
16074590Srm15945 						    "failed for %s\n",
16084590Srm15945 						    host);
16094590Srm15945 					}
16104590Srm15945 					syslog(loglevel,
16114590Srm15945 					    "%s server not responding%s",
16124590Srm15945 					    host, clnt_sperror(cl, ""));
16134590Srm15945 					destroy_auth_client_handle(cl);
16144590Srm15945 					skipentry = 1;
16154590Srm15945 					mfs->mfs_ignore = 1;
16164590Srm15945 					continue;
16174590Srm15945 				}
16184590Srm15945 				if ((errno = fhs.fhs_status) != MNT_OK)  {
16194590Srm15945 
16204590Srm15945 					if (give_up_on_mnt == FALSE) {
16214590Srm15945 						got_mnt_error = TRUE;
16224590Srm15945 						goto try_mnt_slash;
16234590Srm15945 					}
16244590Srm15945 
16254590Srm15945 					free(argp);
16264590Srm15945 					head = prevhead;
16274590Srm15945 					tail = prevtail;
16284590Srm15945 					if (tail)
16294590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
16304590Srm15945 						    NULL;
16314590Srm15945 					if (errno == EACCES) {
16324590Srm15945 						status = NFSERR_ACCES;
16334590Srm15945 					} else {
16344590Srm15945 						syslog(loglevel, "%s: %m",
16354590Srm15945 						    host);
16364590Srm15945 						status = NFSERR_IO;
16374590Srm15945 					}
16384590Srm15945 					if (trace > 3) {
16394590Srm15945 						trace_prt(1,
16404590Srm15945 						    "  nfsmount: mount RPC gave"
16414590Srm15945 						    " %d for %s:%s\n",
16424590Srm15945 						    errno, host, dir);
16434590Srm15945 					}
16444590Srm15945 					last_error = status;
16454590Srm15945 					destroy_auth_client_handle(cl);
16464590Srm15945 					skipentry = 1;
16474590Srm15945 					mfs->mfs_ignore = 1;
16484590Srm15945 					continue;
16494590Srm15945 				}
16504590Srm15945 				argp->fh = malloc((sizeof (fhandle)));
16514590Srm15945 				if (!argp->fh) {
16524590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
16534590Srm15945 					last_error = NFSERR_IO;
16544590Srm15945 					destroy_auth_client_handle(cl);
16554590Srm15945 					goto out;
16564590Srm15945 				}
16574590Srm15945 				(void) memcpy(argp->fh,
16584590Srm15945 				    &fhs.fhstatus_u.fhs_fhandle,
16594590Srm15945 				    sizeof (fhandle));
16604590Srm15945 				break;
16614590Srm15945 			case MOUNTVERS3:
16624590Srm15945 				posix = 0;
16634590Srm15945 				(void) memset((char *)&res3, '\0',
16644590Srm15945 				    sizeof (res3));
16654590Srm15945 				rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
16664590Srm15945 				    xdr_dirpath, (caddr_t)&dir,
16674590Srm15945 				    xdr_mountres3, (caddr_t)&res3, timeout);
16684590Srm15945 				if (rpc_stat != RPC_SUCCESS) {
16694590Srm15945 
16704590Srm15945 					if (give_up_on_mnt == FALSE) {
16714590Srm15945 						got_mnt_error = TRUE;
16724590Srm15945 						goto try_mnt_slash;
16734590Srm15945 					}
16744590Srm15945 
16754590Srm15945 				/*
16764590Srm15945 				 * Given the way "clnt_sperror" works, the "%s"
16774590Srm15945 				 * immediately following the "not responding"
16784590Srm15945 				 * is correct.
16794590Srm15945 				 */
16804590Srm15945 					free(argp);
16814590Srm15945 					head = prevhead;
16824590Srm15945 					tail = prevtail;
16834590Srm15945 					if (tail)
16844590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
16854590Srm15945 						    NULL;
16864590Srm15945 					last_error = NFSERR_NOENT;
16874590Srm15945 
16884590Srm15945 					if (retries-- > 0) {
16894590Srm15945 						destroy_auth_client_handle(cl);
16904590Srm15945 						DELAY(delay);
16914590Srm15945 						goto retry;
16924590Srm15945 					}
16934590Srm15945 
16944590Srm15945 					if (trace > 3) {
16954590Srm15945 						trace_prt(1,
16964590Srm15945 						    "  nfsmount: mount RPC "
16974590Srm15945 						    "failed for %s\n",
16984590Srm15945 						    host);
16994590Srm15945 					}
17004590Srm15945 					syslog(loglevel,
17014590Srm15945 					    "%s server not responding%s",
17024590Srm15945 					    remname, clnt_sperror(cl, ""));
17034590Srm15945 					destroy_auth_client_handle(cl);
17044590Srm15945 					skipentry = 1;
17054590Srm15945 					mfs->mfs_ignore = 1;
17064590Srm15945 					continue;
17074590Srm15945 				}
17084590Srm15945 				if ((errno = res3.fhs_status) != MNT_OK)  {
17094590Srm15945 
17104590Srm15945 					if (give_up_on_mnt == FALSE) {
17114590Srm15945 						got_mnt_error = TRUE;
17124590Srm15945 						goto try_mnt_slash;
17134590Srm15945 					}
17144590Srm15945 
17154590Srm15945 					free(argp);
17164590Srm15945 					head = prevhead;
17174590Srm15945 					tail = prevtail;
17184590Srm15945 					if (tail)
17194590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
17204590Srm15945 						    NULL;
17214590Srm15945 					if (errno == EACCES) {
17224590Srm15945 						status = NFSERR_ACCES;
17234590Srm15945 					} else {
17244590Srm15945 						syslog(loglevel, "%s: %m",
17254590Srm15945 						    remname);
17264590Srm15945 						status = NFSERR_IO;
17274590Srm15945 					}
17284590Srm15945 					if (trace > 3) {
17294590Srm15945 						trace_prt(1,
17304590Srm15945 						    "  nfsmount: mount RPC gave"
17314590Srm15945 						    " %d for %s:%s\n",
17324590Srm15945 						    errno, host, dir);
17334590Srm15945 					}
17344590Srm15945 					last_error = status;
17354590Srm15945 					destroy_auth_client_handle(cl);
17364590Srm15945 					skipentry = 1;
17374590Srm15945 					mfs->mfs_ignore = 1;
17384590Srm15945 					continue;
17394590Srm15945 				}
17400Sstevel@tonic-gate 
17410Sstevel@tonic-gate 			/*
17424590Srm15945 			 *  Negotiate the security flavor for nfs_mount
17430Sstevel@tonic-gate 			 */
17444590Srm15945 				auths = res3.mountres3_u.mountinfo.
17454590Srm15945 				    auth_flavors.auth_flavors_val;
17464590Srm15945 				count = res3.mountres3_u.mountinfo.
17474590Srm15945 				    auth_flavors.auth_flavors_len;
17484590Srm15945 
17494590Srm15945 				if (sec_opt) {
17504590Srm15945 					for (i = 0; i < count; i++)
17514590Srm15945 						if (auths[i] ==
17524590Srm15945 						    nfs_sec.sc_nfsnum) {
17534590Srm15945 							break;
17544590Srm15945 						}
17554590Srm15945 					if (i >= count) {
17564590Srm15945 						syslog(LOG_ERR,
17574590Srm15945 						    "%s: does not support "
17584590Srm15945 						    "security \"%s\"\n",
17594590Srm15945 						    remname, nfs_sec.sc_name);
17604590Srm15945 						clnt_freeres(cl, xdr_mountres3,
17614590Srm15945 						    (caddr_t)&res3);
17624590Srm15945 						free(argp);
17634590Srm15945 						head = prevhead;
17644590Srm15945 						tail = prevtail;
17654590Srm15945 						if (tail)
17664590Srm15945 							tail->nfs_ext_u.
17674590Srm15945 							    nfs_extB.next =
17684590Srm15945 							    NULL;
17694590Srm15945 						last_error = NFSERR_IO;
17704590Srm15945 						destroy_auth_client_handle(cl);
17714590Srm15945 						skipentry = 1;
17724590Srm15945 						mfs->mfs_ignore = 1;
17734590Srm15945 						continue;
17744590Srm15945 					}
17754590Srm15945 				} else if (count > 0) {
17764590Srm15945 					for (i = 0; i < count; i++) {
17774590Srm15945 						if (!(scerror =
17784590Srm15945 						    nfs_getseconfig_bynumber(
17794590Srm15945 						    auths[i], &nfs_sec))) {
17804590Srm15945 							sec_opt++;
17814590Srm15945 							break;
17824590Srm15945 						}
17834590Srm15945 					}
17844590Srm15945 					if (i >= count) {
17854590Srm15945 						if (nfs_syslog_scerr(scerror,
17864590Srm15945 						    scerror_msg)
17874590Srm15945 						    != -1) {
17884590Srm15945 							syslog(LOG_ERR,
17894590Srm15945 							    "%s cannot be "
17904590Srm15945 							    "mounted because it"
17914590Srm15945 							    " is shared with "
17924590Srm15945 							    "security flavor %d"
17934590Srm15945 							    " which %s",
17944590Srm15945 							    remname,
17954590Srm15945 							    auths[i-1],
17964590Srm15945 							    scerror_msg);
17974590Srm15945 						}
17984590Srm15945 						clnt_freeres(cl, xdr_mountres3,
17994590Srm15945 						    (caddr_t)&res3);
18004590Srm15945 						free(argp);
18014590Srm15945 						head = prevhead;
18024590Srm15945 						tail = prevtail;
18034590Srm15945 						if (tail)
18044590Srm15945 							tail->nfs_ext_u.
18054590Srm15945 							    nfs_extB.next =
18064590Srm15945 							    NULL;
18074590Srm15945 						last_error = NFSERR_IO;
18084590Srm15945 						destroy_auth_client_handle(cl);
18094590Srm15945 						skipentry = 1;
18104590Srm15945 						mfs->mfs_ignore = 1;
18114590Srm15945 						continue;
18124590Srm15945 						}
18134590Srm15945 				}
18144590Srm15945 
18154590Srm15945 				fh3.fh3_length =
18164590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
18174590Srm15945 				    fhandle3_len;
18184590Srm15945 				(void) memcpy(fh3.fh3_u.data,
18194590Srm15945 				    res3.mountres3_u.mountinfo.fhandle.
18204590Srm15945 				    fhandle3_val,
18214590Srm15945 				    fh3.fh3_length);
18224590Srm15945 				clnt_freeres(cl, xdr_mountres3,
18234590Srm15945 				    (caddr_t)&res3);
18244590Srm15945 				argp->fh = malloc(sizeof (nfs_fh3));
18254590Srm15945 				if (!argp->fh) {
18264590Srm15945 					syslog(LOG_ERR, "nfsmount: no memory");
18274590Srm15945 					last_error = NFSERR_IO;
18284590Srm15945 					destroy_auth_client_handle(cl);
18294590Srm15945 					goto out;
18304590Srm15945 				}
18314590Srm15945 				(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
18324590Srm15945 				break;
18334590Srm15945 			default:
18340Sstevel@tonic-gate 				free(argp);
18350Sstevel@tonic-gate 				head = prevhead;
18360Sstevel@tonic-gate 				tail = prevtail;
18370Sstevel@tonic-gate 				if (tail)
18380Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18390Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18400Sstevel@tonic-gate 				syslog(loglevel,
18414590Srm15945 				    "unknown MOUNT version %ld on %s",
18424590Srm15945 				    vers, remname);
18430Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18440Sstevel@tonic-gate 				skipentry = 1;
18450Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18460Sstevel@tonic-gate 				continue;
18474590Srm15945 			} /* switch */
18480Sstevel@tonic-gate 		}
18490Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
18500Sstevel@tonic-gate 			argp->fh = strdup(dir);
18510Sstevel@tonic-gate 			if (argp->fh == NULL) {
18520Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
18530Sstevel@tonic-gate 				last_error = NFSERR_IO;
18540Sstevel@tonic-gate 				goto out;
18550Sstevel@tonic-gate 			}
18560Sstevel@tonic-gate 		}
18570Sstevel@tonic-gate 
18580Sstevel@tonic-gate 		if (trace > 4)
18590Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
18600Sstevel@tonic-gate 			    fstype, remname);
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
18630Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
18643767Sjs195444 		argp->flags |= NFSMNT_HOSTNAME;
18652170Sevanl 		argp->hostname = strdup(host);
18663767Sjs195444 		if (argp->hostname == NULL) {
18673767Sjs195444 			syslog(LOG_ERR, "nfsmount: no memory");
18683767Sjs195444 			last_error = NFSERR_IO;
18693767Sjs195444 			goto out;
18703767Sjs195444 		}
18710Sstevel@tonic-gate 
18720Sstevel@tonic-gate 		/*
18730Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
18740Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
18750Sstevel@tonic-gate 		 */
18760Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
18770Sstevel@tonic-gate 			nconf = NULL;
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 			if (nfs_port != 0)
18800Sstevel@tonic-gate 				thisport = nfs_port;
18810Sstevel@tonic-gate 			else
18820Sstevel@tonic-gate 				thisport = mfs->mfs_port;
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 			/*
18850Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
188611291SRobert.Thurlow@Sun.COM 			 * get_server_netinfo() directly to tell it that
18870Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
18880Sstevel@tonic-gate 			 * do what has always been done.
18890Sstevel@tonic-gate 			 */
18900Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18910Sstevel@tonic-gate 				enum clnt_stat cstat;
189211291SRobert.Thurlow@Sun.COM 
189311291SRobert.Thurlow@Sun.COM 				argp->addr = get_server_netinfo(SERVER_ADDR,
18944590Srm15945 				    host, NFS_PROGRAM, nfsvers, NULL,
18954590Srm15945 				    &nconf, nfs_proto, thisport, NULL,
18964590Srm15945 				    NULL, TRUE, NULL, &cstat);
18970Sstevel@tonic-gate 			} else {
18980Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
18994590Srm15945 				    nfsvers, &nconf, nfs_proto,
19004590Srm15945 				    thisport, NULL);
19010Sstevel@tonic-gate 			}
19020Sstevel@tonic-gate 
19030Sstevel@tonic-gate 			if (argp->addr == NULL) {
19042170Sevanl 				if (argp->hostname)
19052170Sevanl 					free(argp->hostname);
19060Sstevel@tonic-gate 				free(argp->fh);
19070Sstevel@tonic-gate 				free(argp);
19080Sstevel@tonic-gate 				head = prevhead;
19090Sstevel@tonic-gate 				tail = prevtail;
19100Sstevel@tonic-gate 				if (tail)
19110Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19120Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 				if (retries-- > 0) {
19150Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
19160Sstevel@tonic-gate 					DELAY(delay);
19170Sstevel@tonic-gate 					goto retry;
19180Sstevel@tonic-gate 				}
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
19210Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19220Sstevel@tonic-gate 				skipentry = 1;
19230Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19240Sstevel@tonic-gate 				continue;
19250Sstevel@tonic-gate 			}
19260Sstevel@tonic-gate 			if (trace > 4)
19270Sstevel@tonic-gate 				trace_prt(1,
19280Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
19290Sstevel@tonic-gate 				    remname);
19300Sstevel@tonic-gate 
19310Sstevel@tonic-gate 		} else {
19320Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
19330Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
19340Sstevel@tonic-gate 		}
19350Sstevel@tonic-gate 
19360Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
19370Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
19380Sstevel@tonic-gate 		if (argp->knconf == NULL) {
19390Sstevel@tonic-gate 			netbuf_free(argp->addr);
19400Sstevel@tonic-gate 			freenetconfigent(nconf);
19412170Sevanl 			if (argp->hostname)
19422170Sevanl 				free(argp->hostname);
19430Sstevel@tonic-gate 			free(argp->fh);
19440Sstevel@tonic-gate 			free(argp);
19450Sstevel@tonic-gate 			head = prevhead;
19460Sstevel@tonic-gate 			tail = prevtail;
19470Sstevel@tonic-gate 			if (tail)
19480Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
19490Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
19500Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
19510Sstevel@tonic-gate 			skipentry = 1;
19520Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
19530Sstevel@tonic-gate 			continue;
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 		if (trace > 4)
19560Sstevel@tonic-gate 			trace_prt(1,
19570Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
19580Sstevel@tonic-gate 			    remname);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
19610Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
19620Sstevel@tonic-gate 		}
19630Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
19640Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
19650Sstevel@tonic-gate 		}
19660Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
19670Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
19680Sstevel@tonic-gate 		}
19690Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
19700Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
19710Sstevel@tonic-gate 		}
19720Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
19730Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
19740Sstevel@tonic-gate 		}
19750Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
19760Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
19770Sstevel@tonic-gate 		}
19780Sstevel@tonic-gate 
19790Sstevel@tonic-gate 		/*
19800Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
19810Sstevel@tonic-gate 		 */
19820Sstevel@tonic-gate 		if (mfssnego.snego_done) {
19830Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
19844590Srm15945 			    sizeof (seconfig_t));
19850Sstevel@tonic-gate 		} else if (!sec_opt) {
19860Sstevel@tonic-gate 			/*
19870Sstevel@tonic-gate 			 * Get default security mode.
19880Sstevel@tonic-gate 			 */
19890Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
19900Sstevel@tonic-gate 				syslog(loglevel,
19910Sstevel@tonic-gate 				    "error getting default security entry\n");
19920Sstevel@tonic-gate 				free_knconf(argp->knconf);
19930Sstevel@tonic-gate 				netbuf_free(argp->addr);
19940Sstevel@tonic-gate 				freenetconfigent(nconf);
19952170Sevanl 				if (argp->hostname)
19962170Sevanl 					free(argp->hostname);
19970Sstevel@tonic-gate 				free(argp->fh);
19980Sstevel@tonic-gate 				free(argp);
19990Sstevel@tonic-gate 				head = prevhead;
20000Sstevel@tonic-gate 				tail = prevtail;
20010Sstevel@tonic-gate 				if (tail)
20020Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
20030Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
20040Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
20050Sstevel@tonic-gate 				skipentry = 1;
20060Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
20070Sstevel@tonic-gate 				continue;
20080Sstevel@tonic-gate 			}
20090Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 		/*
20130Sstevel@tonic-gate 		 * For AUTH_DH
20140Sstevel@tonic-gate 		 * get the network address for the time service on
20150Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
20160Sstevel@tonic-gate 		 * not available then try the IP time service.
20170Sstevel@tonic-gate 		 *
20180Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
20190Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
202011291SRobert.Thurlow@Sun.COM 		 * get_the_addr/get_netconfig_info routine.
20210Sstevel@tonic-gate 		 */
20220Sstevel@tonic-gate 		secflags = 0;
20230Sstevel@tonic-gate 		syncaddr = NULL;
20240Sstevel@tonic-gate 		retaddrs = NULL;
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
20270Sstevel@tonic-gate 		/*
20280Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
20290Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
20300Sstevel@tonic-gate 		 * prevent us from doing that.
20310Sstevel@tonic-gate 		 */
20324590Srm15945 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
20334590Srm15945 			    nfsvers != NFS_V4) {
203411291SRobert.Thurlow@Sun.COM 				enum clnt_stat cstat;
203511291SRobert.Thurlow@Sun.COM 				syncaddr = get_server_netinfo(SERVER_ADDR,
203611291SRobert.Thurlow@Sun.COM 				    host, RPCBPROG, RPCBVERS, NULL, &nconf,
203711291SRobert.Thurlow@Sun.COM 				    NULL, 0, NULL, NULL, FALSE, NULL, &cstat);
20380Sstevel@tonic-gate 			}
20390Sstevel@tonic-gate 
20404590Srm15945 			if (syncaddr != NULL) {
20414590Srm15945 				/* for flags in sec_data */
20424590Srm15945 				secflags |= AUTH_F_RPCTIMESYNC;
20434590Srm15945 			} else {
20444590Srm15945 				struct nd_hostserv hs;
20454590Srm15945 				int error;
20464590Srm15945 
20474590Srm15945 				hs.h_host = host;
20484590Srm15945 				hs.h_serv = "timserver";
20494590Srm15945 				error = netdir_getbyname(nconf, &hs, &retaddrs);
20504590Srm15945 
20514590Srm15945 				if (error != ND_OK &&
20524590Srm15945 				    nfs_sec.sc_rpcnum == AUTH_DH) {
20534590Srm15945 					syslog(loglevel,
20544590Srm15945 					    "%s: secure: no time service\n",
20554590Srm15945 					    host);
20564590Srm15945 					free_knconf(argp->knconf);
20574590Srm15945 					netbuf_free(argp->addr);
20584590Srm15945 					freenetconfigent(nconf);
20594590Srm15945 					if (argp->hostname)
20604590Srm15945 						free(argp->hostname);
20614590Srm15945 					free(argp->fh);
20624590Srm15945 					free(argp);
20634590Srm15945 					head = prevhead;
20644590Srm15945 					tail = prevtail;
20654590Srm15945 					if (tail)
20664590Srm15945 						tail->nfs_ext_u.nfs_extB.next =
20674590Srm15945 						    NULL;
20684590Srm15945 					last_error = NFSERR_IO;
20694590Srm15945 					destroy_auth_client_handle(cl);
20704590Srm15945 					skipentry = 1;
20714590Srm15945 					mfs->mfs_ignore = 1;
20724590Srm15945 					continue;
20734590Srm15945 				}
20744590Srm15945 
20754590Srm15945 				if (error == ND_OK)
20764590Srm15945 					syncaddr = retaddrs->n_addrs;
20770Sstevel@tonic-gate 
20780Sstevel@tonic-gate 			/*
20790Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
20800Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
20810Sstevel@tonic-gate 			 */
20824590Srm15945 				if (nfsvers == NFS_V4 && syncaddr &&
20830Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
20844590Srm15945 					argp->syncaddr =
20854590Srm15945 					    malloc(sizeof (struct netbuf));
20864590Srm15945 					argp->syncaddr->buf =
20874590Srm15945 					    malloc(syncaddr->len);
20884590Srm15945 					(void) memcpy(argp->syncaddr->buf,
20894590Srm15945 					    syncaddr->buf, syncaddr->len);
20904590Srm15945 					argp->syncaddr->len = syncaddr->len;
20914590Srm15945 					argp->syncaddr->maxlen =
20924590Srm15945 					    syncaddr->maxlen;
20934590Srm15945 					argp->netname = strdup(netname);
20944590Srm15945 					argp->flags |= NFSMNT_SECURE;
20954590Srm15945 				}
20964590Srm15945 			} /* syncaddr */
20970Sstevel@tonic-gate 		} /* AUTH_DH */
20980Sstevel@tonic-gate 
20991676Sjpk 		/*
21001676Sjpk 		 * TSOL notes: automountd in tsol extension
21011676Sjpk 		 * has "read down" capability, i.e. we allow
21021676Sjpk 		 * a user to trigger an nfs mount into a lower
21031676Sjpk 		 * labeled zone. We achieve this by always having
21041676Sjpk 		 * root issue the mount request so that the
21051676Sjpk 		 * lookup ops can go past /zone/<zone_name>
21061676Sjpk 		 * on the server side.
21071676Sjpk 		 */
21081676Sjpk 		if (is_system_labeled())
21091676Sjpk 			nfs_sec.sc_uid = (uid_t)0;
21101676Sjpk 		else
21113391Ssemery 			nfs_sec.sc_uid = uid;
21120Sstevel@tonic-gate 		/*
21130Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
21140Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
21150Sstevel@tonic-gate 		 */
21160Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
21174590Srm15945 		    syncaddr, secflags))) {
21180Sstevel@tonic-gate 			syslog(LOG_ERR,
21194590Srm15945 			    "errors constructing security related data\n");
21200Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
21210Sstevel@tonic-gate 				netbuf_free(syncaddr);
21220Sstevel@tonic-gate 			else if (retaddrs)
21230Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
21240Sstevel@tonic-gate 			if (argp->syncaddr)
21250Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
21260Sstevel@tonic-gate 			if (argp->netname)
21270Sstevel@tonic-gate 				free(argp->netname);
21282170Sevanl 			if (argp->hostname)
21292170Sevanl 				free(argp->hostname);
21300Sstevel@tonic-gate 			free_knconf(argp->knconf);
21310Sstevel@tonic-gate 			netbuf_free(argp->addr);
21320Sstevel@tonic-gate 			freenetconfigent(nconf);
21330Sstevel@tonic-gate 			free(argp->fh);
21340Sstevel@tonic-gate 			free(argp);
21350Sstevel@tonic-gate 			head = prevhead;
21360Sstevel@tonic-gate 			tail = prevtail;
21370Sstevel@tonic-gate 			if (tail)
21380Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
21390Sstevel@tonic-gate 			last_error = NFSERR_IO;
21400Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
21410Sstevel@tonic-gate 			skipentry = 1;
21420Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
21430Sstevel@tonic-gate 			continue;
21440Sstevel@tonic-gate 		}
21450Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
21460Sstevel@tonic-gate 		/* end of security stuff */
21470Sstevel@tonic-gate 
21480Sstevel@tonic-gate 		if (trace > 4)
21490Sstevel@tonic-gate 			trace_prt(1,
21500Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
21510Sstevel@tonic-gate 
21520Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
21530Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
21540Sstevel@tonic-gate 		}
21550Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
21560Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
21570Sstevel@tonic-gate 		}
21580Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
21590Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
21600Sstevel@tonic-gate 		}
21610Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
21620Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
21630Sstevel@tonic-gate 		}
21640Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
21650Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
21660Sstevel@tonic-gate 		}
21670Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
21680Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
21690Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
21700Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
21710Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
21720Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
21734590Srm15945 			    = argp->acregmax;
21740Sstevel@tonic-gate 		} else {
21750Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
21760Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
21770Sstevel@tonic-gate 			}
21780Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
21790Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
21800Sstevel@tonic-gate 			}
21810Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
21820Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
21830Sstevel@tonic-gate 			}
21840Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
21850Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
21860Sstevel@tonic-gate 			}
21870Sstevel@tonic-gate 		}
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 		if (posix) {
21900Sstevel@tonic-gate 			argp->pathconf = NULL;
21910Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
21920Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
21930Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
21940Sstevel@tonic-gate 					netbuf_free(syncaddr);
21950Sstevel@tonic-gate 				else if (retaddrs)
21960Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
21970Sstevel@tonic-gate 				free_knconf(argp->knconf);
21980Sstevel@tonic-gate 				netbuf_free(argp->addr);
21990Sstevel@tonic-gate 				freenetconfigent(nconf);
22000Sstevel@tonic-gate 				nfs_free_secdata(
22014590Srm15945 				    argp->nfs_ext_u.nfs_extB.secdata);
22020Sstevel@tonic-gate 				if (argp->syncaddr)
22030Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
22040Sstevel@tonic-gate 				if (argp->netname)
22050Sstevel@tonic-gate 					free(argp->netname);
22062170Sevanl 				if (argp->hostname)
22072170Sevanl 					free(argp->hostname);
22080Sstevel@tonic-gate 				free(argp->fh);
22090Sstevel@tonic-gate 				free(argp);
22100Sstevel@tonic-gate 				head = prevhead;
22110Sstevel@tonic-gate 				tail = prevtail;
22120Sstevel@tonic-gate 				if (tail)
22130Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
22140Sstevel@tonic-gate 				last_error = NFSERR_IO;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
22170Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
22180Sstevel@tonic-gate 					DELAY(delay);
22190Sstevel@tonic-gate 					goto retry;
22200Sstevel@tonic-gate 				}
22210Sstevel@tonic-gate 
22220Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
22230Sstevel@tonic-gate 				skipentry = 1;
22240Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
22250Sstevel@tonic-gate 				continue;
22260Sstevel@tonic-gate 			}
22270Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
22280Sstevel@tonic-gate 			if (trace > 4)
22290Sstevel@tonic-gate 				trace_prt(1,
22300Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
22310Sstevel@tonic-gate 				    remname);
22320Sstevel@tonic-gate 		}
22330Sstevel@tonic-gate 
22340Sstevel@tonic-gate 		/*
22350Sstevel@tonic-gate 		 * free loop-specific data structures
22360Sstevel@tonic-gate 		 */
22370Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
22380Sstevel@tonic-gate 		freenetconfigent(nconf);
22390Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
22400Sstevel@tonic-gate 			netbuf_free(syncaddr);
22410Sstevel@tonic-gate 		else if (retaddrs)
22420Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
22430Sstevel@tonic-gate 
22440Sstevel@tonic-gate 		/*
22450Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
22460Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
22470Sstevel@tonic-gate 		 * LLOCK on.
22480Sstevel@tonic-gate 		 */
22490Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
22500Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22510Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
22524590Srm15945 		    remote_lock(host, argp->fh)) {
22530Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
22540Sstevel@tonic-gate 			"contact admin to install server change", host);
22550Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
22560Sstevel@tonic-gate 		}
22570Sstevel@tonic-gate 
22580Sstevel@tonic-gate 		/*
22590Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
22600Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
22610Sstevel@tonic-gate 		 */
22620Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
22630Sstevel@tonic-gate 			char *tmp;
22640Sstevel@tonic-gate 
22650Sstevel@tonic-gate 			if (mnttabcnt) {
22660Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
22670Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
22680Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
22690Sstevel@tonic-gate 				} else {
22700Sstevel@tonic-gate 					*p = '\0';
22713767Sjs195444 					mnttabcnt += strlen(rhost) + 2;
22720Sstevel@tonic-gate 				}
22730Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
22740Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
22750Sstevel@tonic-gate 					mnttabtext = tmp;
22760Sstevel@tonic-gate 					strcat(mnttabtext, ",");
22770Sstevel@tonic-gate 				} else {
22780Sstevel@tonic-gate 					free(mnttabtext);
22790Sstevel@tonic-gate 					mnttabtext = NULL;
22800Sstevel@tonic-gate 				}
22810Sstevel@tonic-gate 			} else {
22820Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
22830Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22840Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22850Sstevel@tonic-gate 			}
22860Sstevel@tonic-gate 
22870Sstevel@tonic-gate 			if (mnttabtext != NULL)
22880Sstevel@tonic-gate 				strcat(mnttabtext, remname);
22890Sstevel@tonic-gate 
22900Sstevel@tonic-gate 		} else {
22910Sstevel@tonic-gate 			char *tmp;
22920Sstevel@tonic-gate 			int more_cnt = 0;
22930Sstevel@tonic-gate 			char sport[16];
22940Sstevel@tonic-gate 
22950Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
22960Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
22990Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
23000Sstevel@tonic-gate 			} else
23010Sstevel@tonic-gate 				sport[0] = '\0';
23020Sstevel@tonic-gate 
23030Sstevel@tonic-gate 			more_cnt += strlen(sport);
23040Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
23050Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 			if (mnttabcnt) {
23080Sstevel@tonic-gate 				more_cnt += 1; /* "," */
23090Sstevel@tonic-gate 				mnttabcnt += more_cnt;
23100Sstevel@tonic-gate 
23110Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
23120Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
23130Sstevel@tonic-gate 					mnttabtext = tmp;
23140Sstevel@tonic-gate 					strcat(mnttabtext, ",");
23150Sstevel@tonic-gate 				} else {
23160Sstevel@tonic-gate 					free(mnttabtext);
23170Sstevel@tonic-gate 					mnttabtext = NULL;
23180Sstevel@tonic-gate 				}
23190Sstevel@tonic-gate 			} else {
23200Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
23210Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
23220Sstevel@tonic-gate 					mnttabtext[0] = '\0';
23230Sstevel@tonic-gate 			}
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 			if (mnttabtext != NULL) {
23260Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
23270Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
23280Sstevel@tonic-gate 				strcat(mnttabtext, sport);
23290Sstevel@tonic-gate 				strcat(mnttabtext, "/");
23300Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
23310Sstevel@tonic-gate 			}
23320Sstevel@tonic-gate 		}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 		if (!mnttabtext) {
23350Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
23360Sstevel@tonic-gate 			last_error = NFSERR_IO;
23370Sstevel@tonic-gate 			goto out;
23380Sstevel@tonic-gate 		}
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 		/*
23410Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
23420Sstevel@tonic-gate 		 */
23430Sstevel@tonic-gate 		entries++;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		/*
23460Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
23470Sstevel@tonic-gate 		 */
23480Sstevel@tonic-gate 		if (!replicated)
23490Sstevel@tonic-gate 			break;
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 
23530Sstevel@tonic-gate 	/*
23540Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
23550Sstevel@tonic-gate 	 */
23560Sstevel@tonic-gate 	if (!entries)
23570Sstevel@tonic-gate 		goto out;
23580Sstevel@tonic-gate 
23590Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
23600Sstevel@tonic-gate 	strcpy(mopts, opts);
23610Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
23620Sstevel@tonic-gate 		if (strlen(mopts) > 0)
23630Sstevel@tonic-gate 			strcat(mopts, ",");
23640Sstevel@tonic-gate 		strcat(mopts, "xattr");
23650Sstevel@tonic-gate 	}
23660Sstevel@tonic-gate 
23670Sstevel@tonic-gate 	/*
23680Sstevel@tonic-gate 	 * enable services as needed.
23690Sstevel@tonic-gate 	 */
23700Sstevel@tonic-gate 	{
23710Sstevel@tonic-gate 		char **sl;
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
23740Sstevel@tonic-gate 			sl = service_list_v4;
23750Sstevel@tonic-gate 		else
23760Sstevel@tonic-gate 			sl = service_list;
23770Sstevel@tonic-gate 
23781676Sjpk 		(void) _check_services(sl);
23790Sstevel@tonic-gate 	}
23800Sstevel@tonic-gate 
23810Sstevel@tonic-gate 	/*
23820Sstevel@tonic-gate 	 * Whew; do the mount, at last.
23830Sstevel@tonic-gate 	 */
23840Sstevel@tonic-gate 	if (trace > 1) {
23850Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
23860Sstevel@tonic-gate 	}
23870Sstevel@tonic-gate 
23882170Sevanl 	/*
23895302Sth199096 	 * About to do a nfs mount, make sure the mount_to is set for
23905302Sth199096 	 * potential ephemeral mounts with NFSv4.
23915302Sth199096 	 */
23925302Sth199096 	set_nfsv4_ephemeral_mount_to();
23935302Sth199096 
23945302Sth199096 	/*
23952170Sevanl 	 * If no action list pointer then do the mount, otherwise
23962170Sevanl 	 * build the actions list pointer with the mount information.
23972170Sevanl 	 * so the mount can be done in the kernel.
23982170Sevanl 	 */
23992170Sevanl 	if (alp == NULL) {
24002170Sevanl 		if (mount(mnttabtext, mntpnt, flags | MS_DATA, fstype,
24014590Srm15945 		    head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
24022170Sevanl 			if (trace > 1)
24032170Sevanl 				trace_prt(1, "	Mount of %s on %s: %d\n",
24044590Srm15945 				    mnttabtext, mntpnt, errno);
24052170Sevanl 			if (errno != EBUSY || verbose)
24062170Sevanl 				syslog(LOG_ERR,
24070Sstevel@tonic-gate 				"Mount of %s on %s: %m", mnttabtext, mntpnt);
24082170Sevanl 			last_error = NFSERR_IO;
24092170Sevanl 			goto out;
24102170Sevanl 		}
24112170Sevanl 
24122170Sevanl 		last_error = NFS_OK;
24132170Sevanl 		if (stat(mntpnt, &stbuf) == 0) {
24142170Sevanl 			if (trace > 1) {
24152170Sevanl 				trace_prt(1, "	mount %s dev=%x rdev=%x OK\n",
24164590Srm15945 				    mnttabtext, stbuf.st_dev, stbuf.st_rdev);
24172170Sevanl 			}
24182170Sevanl 		} else {
24192170Sevanl 			if (trace > 1) {
24202170Sevanl 				trace_prt(1, "	mount %s OK\n", mnttabtext);
24212170Sevanl 				trace_prt(1, "	stat of %s failed\n", mntpnt);
24222170Sevanl 			}
24232170Sevanl 
24240Sstevel@tonic-gate 		}
24250Sstevel@tonic-gate 	} else {
24262170Sevanl 		alp->action.action = AUTOFS_MOUNT_RQ;
24272170Sevanl 		alp->action.action_list_entry_u.mounta.spec =
24284590Srm15945 		    strdup(mnttabtext);
24292170Sevanl 		alp->action.action_list_entry_u.mounta.dir = strdup(mntpnt);
24302170Sevanl 		alp->action.action_list_entry_u.mounta.flags =
24314590Srm15945 		    flags | MS_DATA;
24322170Sevanl 		alp->action.action_list_entry_u.mounta.fstype =
24334590Srm15945 		    strdup(fstype);
24342170Sevanl 		alp->action.action_list_entry_u.mounta.dataptr = (char *)head;
24352170Sevanl 		alp->action.action_list_entry_u.mounta.datalen =
24364590Srm15945 		    sizeof (*head);
24372170Sevanl 		mntopts = malloc(strlen(mopts) + 1);
24382170Sevanl 		strcpy(mntopts, mopts);
24392170Sevanl 		mntopts[strlen(mopts)] = '\0';
24402170Sevanl 		alp->action.action_list_entry_u.mounta.optptr = mntopts;
24412170Sevanl 		alp->action.action_list_entry_u.mounta.optlen =
24424590Srm15945 		    strlen(mntopts) + 1;
24432170Sevanl 		last_error = NFS_OK;
24442170Sevanl 		goto ret;
24450Sstevel@tonic-gate 	}
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate out:
24480Sstevel@tonic-gate 	argp = head;
24490Sstevel@tonic-gate 	while (argp) {
24500Sstevel@tonic-gate 		if (argp->pathconf)
24510Sstevel@tonic-gate 			free(argp->pathconf);
24520Sstevel@tonic-gate 		free_knconf(argp->knconf);
24530Sstevel@tonic-gate 		netbuf_free(argp->addr);
24540Sstevel@tonic-gate 		if (argp->syncaddr)
24550Sstevel@tonic-gate 			netbuf_free(argp->syncaddr);
24560Sstevel@tonic-gate 		if (argp->netname) {
24570Sstevel@tonic-gate 			free(argp->netname);
24580Sstevel@tonic-gate 		}
24592170Sevanl 		if (argp->hostname)
24602170Sevanl 			free(argp->hostname);
24610Sstevel@tonic-gate 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
24620Sstevel@tonic-gate 		free(argp->fh);
24630Sstevel@tonic-gate 		head = argp;
24640Sstevel@tonic-gate 		argp = argp->nfs_ext_u.nfs_extB.next;
24650Sstevel@tonic-gate 		free(head);
24660Sstevel@tonic-gate 	}
24670Sstevel@tonic-gate ret:
24680Sstevel@tonic-gate 	if (nfs_proto)
24690Sstevel@tonic-gate 		free(nfs_proto);
24700Sstevel@tonic-gate 	if (mnttabtext)
24710Sstevel@tonic-gate 		free(mnttabtext);
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
24740Sstevel@tonic-gate 
24750Sstevel@tonic-gate 		if (mfs->mfs_flags & MFS_ALLOC_DIR) {
24760Sstevel@tonic-gate 			free(mfs->mfs_dir);
24770Sstevel@tonic-gate 			mfs->mfs_dir = NULL;
24780Sstevel@tonic-gate 			mfs->mfs_flags &= ~MFS_ALLOC_DIR;
24790Sstevel@tonic-gate 		}
24800Sstevel@tonic-gate 
24812170Sevanl 		if (mfs->mfs_args != NULL && alp == NULL) {
24820Sstevel@tonic-gate 			free(mfs->mfs_args);
24830Sstevel@tonic-gate 			mfs->mfs_args = NULL;
24840Sstevel@tonic-gate 		}
24850Sstevel@tonic-gate 
24860Sstevel@tonic-gate 		if (mfs->mfs_nconf != NULL) {
24870Sstevel@tonic-gate 			freenetconfigent(mfs->mfs_nconf);
24880Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
24890Sstevel@tonic-gate 		}
24900Sstevel@tonic-gate 	}
24910Sstevel@tonic-gate 
24920Sstevel@tonic-gate 	return (last_error);
24930Sstevel@tonic-gate }
24940Sstevel@tonic-gate 
24950Sstevel@tonic-gate /*
24960Sstevel@tonic-gate  * get_pathconf(cl, path, fsname, pcnf, cretries)
24970Sstevel@tonic-gate  * ugliness that requires that ppathcnf and pathcnf stay consistent
24980Sstevel@tonic-gate  * cretries is a copy of retries used to determine when to syslog
24990Sstevel@tonic-gate  * on retry situations.
25000Sstevel@tonic-gate  */
25010Sstevel@tonic-gate static int
get_pathconf(CLIENT * cl,char * path,char * fsname,struct pathcnf ** pcnf,int cretries)25020Sstevel@tonic-gate get_pathconf(CLIENT *cl, char *path, char *fsname, struct pathcnf **pcnf,
25030Sstevel@tonic-gate 	int cretries)
25040Sstevel@tonic-gate {
25050Sstevel@tonic-gate 	struct ppathcnf *p = NULL;
25060Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
25070Sstevel@tonic-gate 	struct timeval timeout;
25080Sstevel@tonic-gate 
25090Sstevel@tonic-gate 	p = (struct ppathcnf *)malloc(sizeof (struct ppathcnf));
25100Sstevel@tonic-gate 	if (p == NULL) {
25110Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: Out of memory");
25120Sstevel@tonic-gate 		return (RET_ERR);
25130Sstevel@tonic-gate 	}
25140Sstevel@tonic-gate 	memset((caddr_t)p, 0, sizeof (struct ppathcnf));
25150Sstevel@tonic-gate 
25160Sstevel@tonic-gate 	timeout.tv_sec = 10;
25170Sstevel@tonic-gate 	timeout.tv_usec = 0;
25180Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
25190Sstevel@tonic-gate 	    xdr_dirpath, (caddr_t)&path, xdr_ppathcnf, (caddr_t)p, timeout);
25200Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
25210Sstevel@tonic-gate 		if (cretries-- <= 0) {
25220Sstevel@tonic-gate 			syslog(LOG_ERR,
25230Sstevel@tonic-gate 			    "get_pathconf: %s: server not responding: %s",
25240Sstevel@tonic-gate 			    fsname, clnt_sperror(cl, ""));
25250Sstevel@tonic-gate 		}
25260Sstevel@tonic-gate 		free(p);
25270Sstevel@tonic-gate 		return (RET_RETRY);
25280Sstevel@tonic-gate 	}
25290Sstevel@tonic-gate 	if (_PC_ISSET(_PC_ERROR, p->pc_mask)) {
25300Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: no info for %s", fsname);
25310Sstevel@tonic-gate 		free(p);
25320Sstevel@tonic-gate 		return (RET_ERR);
25330Sstevel@tonic-gate 	}
25340Sstevel@tonic-gate 	*pcnf = (struct pathcnf *)p;
25350Sstevel@tonic-gate 	return (RET_OK);
25360Sstevel@tonic-gate }
25370Sstevel@tonic-gate 
25382170Sevanl void
netbuf_free(nb)25390Sstevel@tonic-gate netbuf_free(nb)
25400Sstevel@tonic-gate 	struct netbuf *nb;
25410Sstevel@tonic-gate {
25420Sstevel@tonic-gate 	if (nb == NULL)
25430Sstevel@tonic-gate 		return;
25440Sstevel@tonic-gate 	if (nb->buf)
25450Sstevel@tonic-gate 		free(nb->buf);
25460Sstevel@tonic-gate 	free(nb);
25470Sstevel@tonic-gate }
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate #define	SMALL_HOSTNAME		20
25500Sstevel@tonic-gate #define	SMALL_PROTONAME		10
25510Sstevel@tonic-gate #define	SMALL_PROTOFMLYNAME		10
25520Sstevel@tonic-gate 
25530Sstevel@tonic-gate struct portmap_cache {
25540Sstevel@tonic-gate 	int cache_prog;
25550Sstevel@tonic-gate 	int cache_vers;
25560Sstevel@tonic-gate 	time_t cache_time;
25570Sstevel@tonic-gate 	char cache_small_hosts[SMALL_HOSTNAME + 1];
25580Sstevel@tonic-gate 	char *cache_hostname;
25590Sstevel@tonic-gate 	char *cache_proto;
25600Sstevel@tonic-gate 	char *cache_protofmly;
25610Sstevel@tonic-gate 	char cache_small_protofmly[SMALL_PROTOFMLYNAME + 1];
25620Sstevel@tonic-gate 	char cache_small_proto[SMALL_PROTONAME + 1];
25630Sstevel@tonic-gate 	struct netbuf cache_srv_addr;
25640Sstevel@tonic-gate 	struct portmap_cache *cache_prev, *cache_next;
25650Sstevel@tonic-gate };
25660Sstevel@tonic-gate 
25670Sstevel@tonic-gate rwlock_t portmap_cache_lock;
25680Sstevel@tonic-gate static int portmap_cache_valid_time = 30;
25690Sstevel@tonic-gate struct portmap_cache *portmap_cache_head, *portmap_cache_tail;
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate #ifdef MALLOC_DEBUG
25720Sstevel@tonic-gate void
portmap_cache_flush()25730Sstevel@tonic-gate portmap_cache_flush()
25740Sstevel@tonic-gate {
25750Sstevel@tonic-gate 	struct  portmap_cache *next = NULL, *cp;
25760Sstevel@tonic-gate 
25770Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
25780Sstevel@tonic-gate 	for (cp = portmap_cache_head; cp; cp = cp->cache_next) {
25790Sstevel@tonic-gate 		if (cp->cache_hostname != NULL &&
25800Sstevel@tonic-gate 		    cp->cache_hostname !=
25810Sstevel@tonic-gate 		    cp->cache_small_hosts)
25820Sstevel@tonic-gate 			free(cp->cache_hostname);
25830Sstevel@tonic-gate 		if (cp->cache_proto != NULL &&
25840Sstevel@tonic-gate 		    cp->cache_proto !=
25850Sstevel@tonic-gate 		    cp->cache_small_proto)
25860Sstevel@tonic-gate 			free(cp->cache_proto);
25870Sstevel@tonic-gate 		if (cp->cache_srv_addr.buf != NULL)
25880Sstevel@tonic-gate 			free(cp->cache_srv_addr.buf);
25890Sstevel@tonic-gate 		next = cp->cache_next;
25900Sstevel@tonic-gate 		free(cp);
25910Sstevel@tonic-gate 	}
25920Sstevel@tonic-gate 	portmap_cache_head = NULL;
25930Sstevel@tonic-gate 	portmap_cache_tail = NULL;
25940Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
25950Sstevel@tonic-gate }
25960Sstevel@tonic-gate #endif
25970Sstevel@tonic-gate 
25980Sstevel@tonic-gate /*
25990Sstevel@tonic-gate  * Returns 1 if the entry is found in the cache, 0 otherwise.
26000Sstevel@tonic-gate  */
26010Sstevel@tonic-gate static int
portmap_cache_lookup(hostname,prog,vers,nconf,addrp)26020Sstevel@tonic-gate portmap_cache_lookup(hostname, prog, vers, nconf, addrp)
26030Sstevel@tonic-gate 	char *hostname;
26040Sstevel@tonic-gate 	rpcprog_t prog;
26050Sstevel@tonic-gate 	rpcvers_t vers;
26060Sstevel@tonic-gate 	struct netconfig *nconf;
26070Sstevel@tonic-gate 	struct netbuf *addrp;
26080Sstevel@tonic-gate {
26090Sstevel@tonic-gate 	struct	portmap_cache *cachep, *prev, *next = NULL, *cp;
26100Sstevel@tonic-gate 	int	retval = 0;
26110Sstevel@tonic-gate 
26120Sstevel@tonic-gate 	timenow = time(NULL);
26130Sstevel@tonic-gate 
26140Sstevel@tonic-gate 	(void) rw_rdlock(&portmap_cache_lock);
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	/*
26170Sstevel@tonic-gate 	 * Increment the portmap cache counters for # accesses and lookups
26180Sstevel@tonic-gate 	 * Use a smaller factor (100 vs 1000 for the host cache) since
26190Sstevel@tonic-gate 	 * initial analysis shows this cache is looked up 10% that of the
26200Sstevel@tonic-gate 	 * host cache.
26210Sstevel@tonic-gate 	 */
26220Sstevel@tonic-gate #ifdef CACHE_DEBUG
26230Sstevel@tonic-gate 	portmap_cache_accesses++;
26240Sstevel@tonic-gate 	portmap_cache_lookups++;
26250Sstevel@tonic-gate 	if ((portmap_cache_lookups%100) == 0)
26260Sstevel@tonic-gate 		trace_portmap_cache();
26270Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26280Sstevel@tonic-gate 
26290Sstevel@tonic-gate 	for (cachep = portmap_cache_head; cachep;
26300Sstevel@tonic-gate 		cachep = cachep->cache_next) {
26310Sstevel@tonic-gate 		if (timenow > cachep->cache_time) {
26320Sstevel@tonic-gate 			/*
26330Sstevel@tonic-gate 			 * We stumbled across an entry in the cache which
26340Sstevel@tonic-gate 			 * has timed out. Free up all the entries that
26350Sstevel@tonic-gate 			 * were added before it, which will positionally
26360Sstevel@tonic-gate 			 * be after this entry. And adjust neighboring
26370Sstevel@tonic-gate 			 * pointers.
26380Sstevel@tonic-gate 			 * When we drop the lock and re-acquire it, we
26390Sstevel@tonic-gate 			 * need to start from the beginning.
26400Sstevel@tonic-gate 			 */
26410Sstevel@tonic-gate 			(void) rw_unlock(&portmap_cache_lock);
26420Sstevel@tonic-gate 			(void) rw_wrlock(&portmap_cache_lock);
26430Sstevel@tonic-gate 			for (cp = portmap_cache_head;
26440Sstevel@tonic-gate 				cp && (cp->cache_time >= timenow);
26450Sstevel@tonic-gate 				cp = cp->cache_next)
26460Sstevel@tonic-gate 				;
26470Sstevel@tonic-gate 			if (cp == NULL)
26480Sstevel@tonic-gate 				goto done;
26490Sstevel@tonic-gate 			/*
26500Sstevel@tonic-gate 			 * Adjust the link of the predecessor.
26510Sstevel@tonic-gate 			 * Make the tail point to the new last entry.
26520Sstevel@tonic-gate 			 */
26530Sstevel@tonic-gate 			prev = cp->cache_prev;
26540Sstevel@tonic-gate 			if (prev == NULL) {
26550Sstevel@tonic-gate 				portmap_cache_head = NULL;
26560Sstevel@tonic-gate 				portmap_cache_tail = NULL;
26570Sstevel@tonic-gate 			} else {
26580Sstevel@tonic-gate 				prev->cache_next = NULL;
26590Sstevel@tonic-gate 				portmap_cache_tail = prev;
26600Sstevel@tonic-gate 			}
26610Sstevel@tonic-gate 			for (; cp; cp = next) {
26620Sstevel@tonic-gate 				if (cp->cache_hostname != NULL &&
26630Sstevel@tonic-gate 				    cp->cache_hostname !=
26640Sstevel@tonic-gate 				    cp->cache_small_hosts)
26650Sstevel@tonic-gate 					free(cp->cache_hostname);
26660Sstevel@tonic-gate 				if (cp->cache_proto != NULL &&
26670Sstevel@tonic-gate 				    cp->cache_proto !=
26680Sstevel@tonic-gate 				    cp->cache_small_proto)
26690Sstevel@tonic-gate 					free(cp->cache_proto);
26700Sstevel@tonic-gate 				if (cp->cache_srv_addr.buf != NULL)
26710Sstevel@tonic-gate 					free(cp->cache_srv_addr.buf);
26720Sstevel@tonic-gate 				next = cp->cache_next;
26730Sstevel@tonic-gate 				free(cp);
26740Sstevel@tonic-gate 			}
26750Sstevel@tonic-gate 			goto done;
26760Sstevel@tonic-gate 		}
26770Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL ||
26780Sstevel@tonic-gate 		    prog != cachep->cache_prog || vers != cachep->cache_vers ||
26790Sstevel@tonic-gate 		    strcmp(nconf->nc_proto, cachep->cache_proto) != 0 ||
26800Sstevel@tonic-gate 		    strcmp(nconf->nc_protofmly, cachep->cache_protofmly) != 0 ||
26810Sstevel@tonic-gate 		    strcmp(hostname, cachep->cache_hostname) != 0)
26820Sstevel@tonic-gate 			continue;
26830Sstevel@tonic-gate 		/*
26840Sstevel@tonic-gate 		 * Cache Hit.
26850Sstevel@tonic-gate 		 */
26860Sstevel@tonic-gate #ifdef CACHE_DEBUG
26870Sstevel@tonic-gate 		portmap_cache_hits++;	/* up portmap cache hit counter */
26880Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26890Sstevel@tonic-gate 		addrp->len = cachep->cache_srv_addr.len;
26900Sstevel@tonic-gate 		memcpy(addrp->buf, cachep->cache_srv_addr.buf, addrp->len);
26910Sstevel@tonic-gate 		retval = 1;
26920Sstevel@tonic-gate 		break;
26930Sstevel@tonic-gate 	}
26940Sstevel@tonic-gate done:
26950Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
26960Sstevel@tonic-gate 	return (retval);
26970Sstevel@tonic-gate }
26980Sstevel@tonic-gate 
26990Sstevel@tonic-gate static void
portmap_cache_enter(hostname,prog,vers,nconf,addrp)27000Sstevel@tonic-gate portmap_cache_enter(hostname, prog, vers, nconf, addrp)
27010Sstevel@tonic-gate 	char *hostname;
27020Sstevel@tonic-gate 	rpcprog_t prog;
27030Sstevel@tonic-gate 	rpcvers_t vers;
27040Sstevel@tonic-gate 	struct netconfig *nconf;
27050Sstevel@tonic-gate 	struct netbuf *addrp;
27060Sstevel@tonic-gate {
27070Sstevel@tonic-gate 	struct portmap_cache *cachep;
27080Sstevel@tonic-gate 	int protofmlylen;
27090Sstevel@tonic-gate 	int protolen, hostnamelen;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	timenow = time(NULL);
27120Sstevel@tonic-gate 
27130Sstevel@tonic-gate 	cachep = malloc(sizeof (struct portmap_cache));
27140Sstevel@tonic-gate 	if (cachep == NULL)
27150Sstevel@tonic-gate 		return;
27160Sstevel@tonic-gate 	memset((char *)cachep, 0, sizeof (*cachep));
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	hostnamelen = strlen(hostname);
27190Sstevel@tonic-gate 	if (hostnamelen <= SMALL_HOSTNAME)
27200Sstevel@tonic-gate 		cachep->cache_hostname = cachep->cache_small_hosts;
27210Sstevel@tonic-gate 	else {
27220Sstevel@tonic-gate 		cachep->cache_hostname = malloc(hostnamelen + 1);
27230Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL)
27240Sstevel@tonic-gate 			goto nomem;
27250Sstevel@tonic-gate 	}
27260Sstevel@tonic-gate 	strcpy(cachep->cache_hostname, hostname);
27270Sstevel@tonic-gate 	protolen = strlen(nconf->nc_proto);
27280Sstevel@tonic-gate 	if (protolen <= SMALL_PROTONAME)
27290Sstevel@tonic-gate 		cachep->cache_proto = cachep->cache_small_proto;
27300Sstevel@tonic-gate 	else {
27310Sstevel@tonic-gate 		cachep->cache_proto = malloc(protolen + 1);
27320Sstevel@tonic-gate 		if (cachep->cache_proto == NULL)
27330Sstevel@tonic-gate 			goto nomem;
27340Sstevel@tonic-gate 	}
27350Sstevel@tonic-gate 	protofmlylen = strlen(nconf->nc_protofmly);
27360Sstevel@tonic-gate 	if (protofmlylen <= SMALL_PROTOFMLYNAME)
27370Sstevel@tonic-gate 		cachep->cache_protofmly = cachep->cache_small_protofmly;
27380Sstevel@tonic-gate 	else {
27390Sstevel@tonic-gate 		cachep->cache_protofmly = malloc(protofmlylen + 1);
27400Sstevel@tonic-gate 		if (cachep->cache_protofmly == NULL)
27410Sstevel@tonic-gate 			goto nomem;
27420Sstevel@tonic-gate 	}
27430Sstevel@tonic-gate 
27440Sstevel@tonic-gate 	strcpy(cachep->cache_proto, nconf->nc_proto);
27450Sstevel@tonic-gate 	cachep->cache_prog = prog;
27460Sstevel@tonic-gate 	cachep->cache_vers = vers;
27470Sstevel@tonic-gate 	cachep->cache_time = timenow + portmap_cache_valid_time;
27480Sstevel@tonic-gate 	cachep->cache_srv_addr.len = addrp->len;
27490Sstevel@tonic-gate 	cachep->cache_srv_addr.buf = malloc(addrp->len);
27500Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf == NULL)
27510Sstevel@tonic-gate 		goto nomem;
27520Sstevel@tonic-gate 	memcpy(cachep->cache_srv_addr.buf, addrp->buf, addrp->maxlen);
27530Sstevel@tonic-gate 	cachep->cache_prev = NULL;
27540Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
27550Sstevel@tonic-gate 	/*
27560Sstevel@tonic-gate 	 * There's a window in which we could have multiple threads making
27570Sstevel@tonic-gate 	 * the same cache entry. This can be avoided by walking the cache
27580Sstevel@tonic-gate 	 * once again here to check and see if there are duplicate entries
27590Sstevel@tonic-gate 	 * (after grabbing the write lock). This isn't fatal and I'm not
27600Sstevel@tonic-gate 	 * going to bother with this.
27610Sstevel@tonic-gate 	 */
27620Sstevel@tonic-gate #ifdef CACHE_DEBUG
27630Sstevel@tonic-gate 	portmap_cache_accesses++;	/* up portmap cache access counter */
27640Sstevel@tonic-gate #endif /* CACHE_DEBUG */
27650Sstevel@tonic-gate 	cachep->cache_next = portmap_cache_head;
27660Sstevel@tonic-gate 	if (portmap_cache_head != NULL)
27670Sstevel@tonic-gate 		portmap_cache_head->cache_prev = cachep;
27680Sstevel@tonic-gate 	portmap_cache_head = cachep;
27690Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
27700Sstevel@tonic-gate 	return;
27710Sstevel@tonic-gate 
27720Sstevel@tonic-gate nomem:
27730Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache_enter: Memory allocation failed");
27740Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf)
27750Sstevel@tonic-gate 		free(cachep->cache_srv_addr.buf);
27760Sstevel@tonic-gate 	if (cachep->cache_proto && protolen > SMALL_PROTONAME)
27770Sstevel@tonic-gate 		free(cachep->cache_proto);
27780Sstevel@tonic-gate 	if (cachep->cache_hostname && hostnamelen > SMALL_HOSTNAME)
27790Sstevel@tonic-gate 		free(cachep->cache_hostname);
27800Sstevel@tonic-gate 	if (cachep->cache_protofmly && protofmlylen > SMALL_PROTOFMLYNAME)
27810Sstevel@tonic-gate 		free(cachep->cache_protofmly);
27820Sstevel@tonic-gate 	if (cachep)
27830Sstevel@tonic-gate 		free(cachep);
27840Sstevel@tonic-gate 	cachep = NULL;
27850Sstevel@tonic-gate }
27860Sstevel@tonic-gate 
27870Sstevel@tonic-gate static int
get_cached_srv_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,struct netbuf * addrp)27880Sstevel@tonic-gate get_cached_srv_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
27890Sstevel@tonic-gate 	struct netconfig *nconf, struct netbuf *addrp)
27900Sstevel@tonic-gate {
27910Sstevel@tonic-gate 	if (portmap_cache_lookup(hostname, prog, vers, nconf, addrp))
27920Sstevel@tonic-gate 		return (1);
27930Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, addrp, hostname) == 0)
27940Sstevel@tonic-gate 		return (0);
27950Sstevel@tonic-gate 	portmap_cache_enter(hostname, prog, vers, nconf, addrp);
27960Sstevel@tonic-gate 	return (1);
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate 
27990Sstevel@tonic-gate /*
28000Sstevel@tonic-gate  * Get a network address on "hostname" for program "prog"
28010Sstevel@tonic-gate  * with version "vers".  If the port number is specified (non zero)
28020Sstevel@tonic-gate  * then try for a TCP/UDP transport and set the port number of the
28030Sstevel@tonic-gate  * resulting IP address.
28040Sstevel@tonic-gate  *
28050Sstevel@tonic-gate  * If the address of a netconfig pointer was passed and
28060Sstevel@tonic-gate  * if it's not null, use it as the netconfig otherwise
28070Sstevel@tonic-gate  * assign the address of the netconfig that was used to
28080Sstevel@tonic-gate  * establish contact with the service.
28090Sstevel@tonic-gate  *
28100Sstevel@tonic-gate  * tinfo argument is for matching the get_addr() defined in
28110Sstevel@tonic-gate  * ../nfs/mount/mount.c
28120Sstevel@tonic-gate  */
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate static struct netbuf *
get_addr(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo)28150Sstevel@tonic-gate get_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
28160Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
28170Sstevel@tonic-gate 	struct t_info *tinfo)
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate {
28200Sstevel@tonic-gate 	enum clnt_stat cstat;
28210Sstevel@tonic-gate 
282211291SRobert.Thurlow@Sun.COM 	return (get_server_netinfo(SERVER_ADDR, hostname, prog, vers, NULL,
28230Sstevel@tonic-gate 		nconfp, proto, port, tinfo, NULL, FALSE, NULL, &cstat));
28240Sstevel@tonic-gate }
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate static struct netbuf *
get_pubfh(char * hostname,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t get_pubfh,char * fspath)28270Sstevel@tonic-gate get_pubfh(char *hostname, rpcvers_t vers, mfs_snego_t *mfssnego,
28280Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
28290Sstevel@tonic-gate 	struct t_info *tinfo, caddr_t *fhp, bool_t get_pubfh, char *fspath)
28300Sstevel@tonic-gate {
28310Sstevel@tonic-gate 	enum clnt_stat cstat;
28320Sstevel@tonic-gate 
283311291SRobert.Thurlow@Sun.COM 	return (get_server_netinfo(SERVER_FH, hostname, NFS_PROGRAM, vers,
28344590Srm15945 	    mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
28354590Srm15945 	    &cstat));
28360Sstevel@tonic-gate }
28370Sstevel@tonic-gate 
28380Sstevel@tonic-gate static enum clnt_stat
get_ping(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig ** nconfp,ushort_t port,bool_t direct_to_server)28390Sstevel@tonic-gate get_ping(char *hostname, rpcprog_t prog, rpcvers_t vers,
28400Sstevel@tonic-gate 	struct netconfig **nconfp, ushort_t port, bool_t direct_to_server)
28410Sstevel@tonic-gate {
28420Sstevel@tonic-gate 	enum clnt_stat cstat;
28430Sstevel@tonic-gate 
284411291SRobert.Thurlow@Sun.COM 	(void) get_server_netinfo(SERVER_PING, hostname, prog,
284511291SRobert.Thurlow@Sun.COM 	    vers, NULL, nconfp, NULL, port, NULL, NULL,
284611291SRobert.Thurlow@Sun.COM 	    direct_to_server, NULL, &cstat);
28470Sstevel@tonic-gate 
28480Sstevel@tonic-gate 	return (cstat);
28490Sstevel@tonic-gate }
28500Sstevel@tonic-gate 
28512170Sevanl void *
get_server_netinfo(enum type_of_stuff type_of_stuff,char * hostname,rpcprog_t prog,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig ** nconfp,char * proto,ushort_t port,struct t_info * tinfo,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstatp)285211291SRobert.Thurlow@Sun.COM get_server_netinfo(
28530Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
28540Sstevel@tonic-gate 	char *hostname,
28550Sstevel@tonic-gate 	rpcprog_t prog,
28560Sstevel@tonic-gate 	rpcvers_t vers,
28570Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
28580Sstevel@tonic-gate 	struct netconfig **nconfp,
28590Sstevel@tonic-gate 	char *proto,
28600Sstevel@tonic-gate 	ushort_t port,			/* may be zero */
28610Sstevel@tonic-gate 	struct t_info *tinfo,
28620Sstevel@tonic-gate 	caddr_t *fhp,
28630Sstevel@tonic-gate 	bool_t direct_to_server,
28640Sstevel@tonic-gate 	char *fspath,
28650Sstevel@tonic-gate 	enum clnt_stat *cstatp)
28660Sstevel@tonic-gate {
28670Sstevel@tonic-gate 	struct netbuf *nb = NULL;
28680Sstevel@tonic-gate 	struct netconfig *nconf = NULL;
28690Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
287011291SRobert.Thurlow@Sun.COM 	int error = 0;
287111291SRobert.Thurlow@Sun.COM 	int fd = 0;
287211291SRobert.Thurlow@Sun.COM 	struct t_bind *tbind = NULL;
28730Sstevel@tonic-gate 	int nthtry = FIRST_TRY;
28740Sstevel@tonic-gate 
287511291SRobert.Thurlow@Sun.COM 	if (nconfp && *nconfp) {
287611291SRobert.Thurlow@Sun.COM 		return (get_netconfig_info(type_of_stuff, hostname,
287711291SRobert.Thurlow@Sun.COM 		    prog, vers, nconf, port, tinfo, tbind, fhp,
287811291SRobert.Thurlow@Sun.COM 		    direct_to_server, fspath, cstatp, mfssnego));
287911291SRobert.Thurlow@Sun.COM 	}
28800Sstevel@tonic-gate 
28810Sstevel@tonic-gate 	/*
28820Sstevel@tonic-gate 	 * No nconf passed in.
28830Sstevel@tonic-gate 	 *
28840Sstevel@tonic-gate 	 * Try to get a nconf from /etc/netconfig.
28850Sstevel@tonic-gate 	 * First choice is COTS, second is CLTS unless proto
28860Sstevel@tonic-gate 	 * is specified.  When we retry, we reset the
28870Sstevel@tonic-gate 	 * netconfig list, so that we search the whole list
28880Sstevel@tonic-gate 	 * for the next choice.
28890Sstevel@tonic-gate 	 */
28900Sstevel@tonic-gate 	if ((nc = setnetpath()) == NULL)
28910Sstevel@tonic-gate 		goto done;
28920Sstevel@tonic-gate 
28930Sstevel@tonic-gate 	/*
28940Sstevel@tonic-gate 	 * If proto is specified, then only search for the match,
28950Sstevel@tonic-gate 	 * otherwise try COTS first, if failed, then try CLTS.
28960Sstevel@tonic-gate 	 */
28970Sstevel@tonic-gate 	if (proto) {
289811291SRobert.Thurlow@Sun.COM 		while ((nconf = getnetpath(nc)) != NULL) {
28990Sstevel@tonic-gate 			if (strcmp(nconf->nc_proto, proto))
29000Sstevel@tonic-gate 				continue;
29010Sstevel@tonic-gate 			/*
29020Sstevel@tonic-gate 			 * If the port number is specified then TCP/UDP
29030Sstevel@tonic-gate 			 * is needed. Otherwise any cots/clts will do.
29040Sstevel@tonic-gate 			 */
29050Sstevel@tonic-gate 			if (port)  {
29060Sstevel@tonic-gate 				if ((strcmp(nconf->nc_protofmly, NC_INET) &&
29070Sstevel@tonic-gate 				    strcmp(nconf->nc_protofmly, NC_INET6)) ||
29080Sstevel@tonic-gate 				    (strcmp(nconf->nc_proto, NC_TCP) &&
29090Sstevel@tonic-gate 				    strcmp(nconf->nc_proto, NC_UDP)))
29100Sstevel@tonic-gate 					continue;
29110Sstevel@tonic-gate 			}
291211291SRobert.Thurlow@Sun.COM 			nb = get_netconfig_info(type_of_stuff, hostname,
291311291SRobert.Thurlow@Sun.COM 			    prog, vers, nconf, port, tinfo, tbind, fhp,
291411291SRobert.Thurlow@Sun.COM 			    direct_to_server, fspath, cstatp, mfssnego);
29150Sstevel@tonic-gate 			if (*cstatp == RPC_SUCCESS)
29160Sstevel@tonic-gate 				break;
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 			assert(nb == NULL);
29190Sstevel@tonic-gate 
292011291SRobert.Thurlow@Sun.COM 		}
29210Sstevel@tonic-gate 		if (nconf == NULL)
29220Sstevel@tonic-gate 			goto done;
29230Sstevel@tonic-gate 	} else {
29240Sstevel@tonic-gate retry:
292511291SRobert.Thurlow@Sun.COM 		while ((nconf = getnetpath(nc)) != NULL) {
29260Sstevel@tonic-gate 			if (nconf->nc_flag & NC_VISIBLE) {
29274590Srm15945 				if (nthtry == FIRST_TRY) {
29284590Srm15945 					if ((nconf->nc_semantics ==
29294590Srm15945 					    NC_TPI_COTS_ORD) ||
29304590Srm15945 					    (nconf->nc_semantics ==
29314590Srm15945 					    NC_TPI_COTS)) {
29324590Srm15945 						if (port == 0)
29334590Srm15945 							break;
29344590Srm15945 						if ((strcmp(nconf->nc_protofmly,
29354590Srm15945 						    NC_INET) == 0 ||
29364590Srm15945 						    strcmp(nconf->nc_protofmly,
29374590Srm15945 						    NC_INET6) == 0) &&
29384590Srm15945 						    (strcmp(nconf->nc_proto,
29394590Srm15945 						    NC_TCP) == 0))
29404590Srm15945 							break;
29414590Srm15945 					}
29420Sstevel@tonic-gate 				}
29434590Srm15945 				if (nthtry == SECOND_TRY) {
29444590Srm15945 					if (nconf->nc_semantics ==
29454590Srm15945 					    NC_TPI_CLTS) {
29464590Srm15945 						if (port == 0)
29474590Srm15945 							break;
29484590Srm15945 						if ((strcmp(nconf->nc_protofmly,
29494590Srm15945 						    NC_INET) == 0 ||
29504590Srm15945 						    strcmp(nconf->nc_protofmly,
29514590Srm15945 						    NC_INET6) == 0) &&
29524590Srm15945 						    (strcmp(nconf->nc_proto,
29534590Srm15945 						    NC_UDP) == 0))
29544590Srm15945 							break;
29554590Srm15945 					}
29560Sstevel@tonic-gate 				}
29570Sstevel@tonic-gate 			}
295811291SRobert.Thurlow@Sun.COM 		}
295911291SRobert.Thurlow@Sun.COM 
29604590Srm15945 		if (nconf == NULL) {
29610Sstevel@tonic-gate 			if (++nthtry <= MNT_PREF_LISTLEN) {
29620Sstevel@tonic-gate 				endnetpath(nc);
29630Sstevel@tonic-gate 				if ((nc = setnetpath()) == NULL)
29640Sstevel@tonic-gate 					goto done;
29650Sstevel@tonic-gate 				goto retry;
29660Sstevel@tonic-gate 			} else
29670Sstevel@tonic-gate 				goto done;
29684590Srm15945 		} else {
296911291SRobert.Thurlow@Sun.COM 			nb = get_netconfig_info(type_of_stuff, hostname,
297011291SRobert.Thurlow@Sun.COM 			    prog, vers, nconf, port, tinfo, tbind, fhp,
297111291SRobert.Thurlow@Sun.COM 			    direct_to_server, fspath, cstatp, mfssnego);
29720Sstevel@tonic-gate 			if (*cstatp != RPC_SUCCESS)
29730Sstevel@tonic-gate 				/*
29740Sstevel@tonic-gate 				 * Continue the same search path in the
29750Sstevel@tonic-gate 				 * netconfig db until no more matched nconf
29760Sstevel@tonic-gate 				 * (nconf == NULL).
29770Sstevel@tonic-gate 				 */
29780Sstevel@tonic-gate 				goto retry;
29794590Srm15945 		}
298011291SRobert.Thurlow@Sun.COM 	}
29810Sstevel@tonic-gate 
29820Sstevel@tonic-gate 	/*
29830Sstevel@tonic-gate 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
29840Sstevel@tonic-gate 	 * and return it thru nconfp.
29850Sstevel@tonic-gate 	 */
298611291SRobert.Thurlow@Sun.COM 	if (nconf != NULL) {
298711291SRobert.Thurlow@Sun.COM 		if ((*nconfp = getnetconfigent(nconf->nc_netid)) == NULL) {
298811291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "no memory\n");
298911291SRobert.Thurlow@Sun.COM 			free(nb);
299011291SRobert.Thurlow@Sun.COM 			nb = NULL;
299111291SRobert.Thurlow@Sun.COM 		}
299211291SRobert.Thurlow@Sun.COM 	} else {
299311291SRobert.Thurlow@Sun.COM 		*nconfp = NULL;
29940Sstevel@tonic-gate 	}
29950Sstevel@tonic-gate done:
29960Sstevel@tonic-gate 	if (nc)
29970Sstevel@tonic-gate 		endnetpath(nc);
29980Sstevel@tonic-gate 	return (nb);
29990Sstevel@tonic-gate }
30000Sstevel@tonic-gate 
300111291SRobert.Thurlow@Sun.COM void *
get_server_fh(char * hostname,rpcprog_t prog,rpcvers_t vers,mfs_snego_t * mfssnego,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat)300211291SRobert.Thurlow@Sun.COM get_server_fh(char *hostname, rpcprog_t	prog, rpcvers_t	vers,
300311291SRobert.Thurlow@Sun.COM 	mfs_snego_t *mfssnego, struct netconfig *nconf, ushort_t port,
300411291SRobert.Thurlow@Sun.COM 	struct t_info *tinfo, struct t_bind *tbind, caddr_t *fhp,
300511291SRobert.Thurlow@Sun.COM 	bool_t direct_to_server, char *fspath, enum clnt_stat *cstat)
300611291SRobert.Thurlow@Sun.COM {
300711291SRobert.Thurlow@Sun.COM 	AUTH *ah = NULL;
300811291SRobert.Thurlow@Sun.COM 	AUTH *new_ah = NULL;
300911291SRobert.Thurlow@Sun.COM 	struct snego_t	snego;
301011291SRobert.Thurlow@Sun.COM 	enum clnt_stat cs = RPC_TIMEDOUT;
301111291SRobert.Thurlow@Sun.COM 	struct timeval tv;
301211291SRobert.Thurlow@Sun.COM 	bool_t file_handle = 1;
301311291SRobert.Thurlow@Sun.COM 	enum snego_stat sec;
301411291SRobert.Thurlow@Sun.COM 	CLIENT *cl = NULL;
301511291SRobert.Thurlow@Sun.COM 	int fd = -1;
301611291SRobert.Thurlow@Sun.COM 	struct netbuf *nb = NULL;
301711291SRobert.Thurlow@Sun.COM 
301811291SRobert.Thurlow@Sun.COM 	if (direct_to_server != TRUE)
301911291SRobert.Thurlow@Sun.COM 		return (NULL);
302011291SRobert.Thurlow@Sun.COM 
302111291SRobert.Thurlow@Sun.COM 	if (prog == NFS_PROGRAM && vers == NFS_V4)
302211291SRobert.Thurlow@Sun.COM 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
302311291SRobert.Thurlow@Sun.COM 			goto done;
302411291SRobert.Thurlow@Sun.COM 
302511291SRobert.Thurlow@Sun.COM 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0)
302611291SRobert.Thurlow@Sun.COM 		goto done;
302711291SRobert.Thurlow@Sun.COM 
302811291SRobert.Thurlow@Sun.COM 	/* LINTED pointer alignment */
302911291SRobert.Thurlow@Sun.COM 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR)) == NULL)
303011291SRobert.Thurlow@Sun.COM 		goto done;
303111291SRobert.Thurlow@Sun.COM 
303211291SRobert.Thurlow@Sun.COM 	if (setup_nb_parms(nconf, tbind, tinfo, hostname, fd,
303311291SRobert.Thurlow@Sun.COM 	    direct_to_server, port, prog, vers, file_handle) < 0) {
303411291SRobert.Thurlow@Sun.COM 		goto done;
303511291SRobert.Thurlow@Sun.COM 	}
303611291SRobert.Thurlow@Sun.COM 
303711291SRobert.Thurlow@Sun.COM 	cl = clnt_tli_create(fd, nconf, &tbind->addr, prog, vers, 0, 0);
303811291SRobert.Thurlow@Sun.COM 	if (cl == NULL)
303911291SRobert.Thurlow@Sun.COM 		goto done;
304011291SRobert.Thurlow@Sun.COM 
304111291SRobert.Thurlow@Sun.COM 	ah = authsys_create_default();
304211291SRobert.Thurlow@Sun.COM 	if (ah != NULL) {
304311291SRobert.Thurlow@Sun.COM #ifdef MALLOC_DEBUG
304411291SRobert.Thurlow@Sun.COM 		drop_alloc("AUTH_HANDLE", cl->cl_auth,
304511291SRobert.Thurlow@Sun.COM 		    __FILE__, __LINE__);
304611291SRobert.Thurlow@Sun.COM #endif
304711291SRobert.Thurlow@Sun.COM 		AUTH_DESTROY(cl->cl_auth);
304811291SRobert.Thurlow@Sun.COM 		cl->cl_auth = ah;
304911291SRobert.Thurlow@Sun.COM #ifdef MALLOC_DEBUG
305011291SRobert.Thurlow@Sun.COM 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
305111291SRobert.Thurlow@Sun.COM 		    __FILE__, __LINE__);
305211291SRobert.Thurlow@Sun.COM #endif
305311291SRobert.Thurlow@Sun.COM 	}
305411291SRobert.Thurlow@Sun.COM 
305511291SRobert.Thurlow@Sun.COM 	if (!mfssnego->snego_done && vers != NFS_V4) {
305611291SRobert.Thurlow@Sun.COM 		/*
305711291SRobert.Thurlow@Sun.COM 		 * negotiate sec flavor.
305811291SRobert.Thurlow@Sun.COM 		 */
305911291SRobert.Thurlow@Sun.COM 		snego.cnt = 0;
306011291SRobert.Thurlow@Sun.COM 		if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
306111291SRobert.Thurlow@Sun.COM 		    SNEGO_SUCCESS) {
306211291SRobert.Thurlow@Sun.COM 			int jj;
306311291SRobert.Thurlow@Sun.COM 
306411291SRobert.Thurlow@Sun.COM 			/*
306511291SRobert.Thurlow@Sun.COM 			 * check if server supports the one
306611291SRobert.Thurlow@Sun.COM 			 * specified in the sec= option.
306711291SRobert.Thurlow@Sun.COM 			 */
306811291SRobert.Thurlow@Sun.COM 			if (mfssnego->sec_opt) {
306911291SRobert.Thurlow@Sun.COM 				for (jj = 0; jj < snego.cnt; jj++) {
307011291SRobert.Thurlow@Sun.COM 					if (snego.array[jj] ==
307111291SRobert.Thurlow@Sun.COM 					    mfssnego->nfs_sec.sc_nfsnum) {
307211291SRobert.Thurlow@Sun.COM 						mfssnego->snego_done = TRUE;
307311291SRobert.Thurlow@Sun.COM 						break;
307411291SRobert.Thurlow@Sun.COM 					}
307511291SRobert.Thurlow@Sun.COM 				}
307611291SRobert.Thurlow@Sun.COM 			}
307711291SRobert.Thurlow@Sun.COM 
307811291SRobert.Thurlow@Sun.COM 			/*
307911291SRobert.Thurlow@Sun.COM 			 * find a common sec flavor
308011291SRobert.Thurlow@Sun.COM 			 */
308111291SRobert.Thurlow@Sun.COM 			if (!mfssnego->snego_done) {
308211291SRobert.Thurlow@Sun.COM 				for (jj = 0; jj < snego.cnt; jj++) {
308311291SRobert.Thurlow@Sun.COM 					if (!nfs_getseconfig_bynumber(
308411291SRobert.Thurlow@Sun.COM 					    snego.array[jj],
308511291SRobert.Thurlow@Sun.COM 					    &mfssnego->nfs_sec)) {
308611291SRobert.Thurlow@Sun.COM 						mfssnego->snego_done = TRUE;
308711291SRobert.Thurlow@Sun.COM 						break;
308811291SRobert.Thurlow@Sun.COM 					}
308911291SRobert.Thurlow@Sun.COM 				}
309011291SRobert.Thurlow@Sun.COM 			}
309111291SRobert.Thurlow@Sun.COM 			if (!mfssnego->snego_done)
309211291SRobert.Thurlow@Sun.COM 				goto done;
309311291SRobert.Thurlow@Sun.COM 			/*
309411291SRobert.Thurlow@Sun.COM 			 * Now that the flavor has been
309511291SRobert.Thurlow@Sun.COM 			 * negotiated, get the fh.
309611291SRobert.Thurlow@Sun.COM 			 *
309711291SRobert.Thurlow@Sun.COM 			 * First, create an auth handle using the negotiated
309811291SRobert.Thurlow@Sun.COM 			 * sec flavor in the next lookup to
309911291SRobert.Thurlow@Sun.COM 			 * fetch the filehandle.
310011291SRobert.Thurlow@Sun.COM 			 */
310111291SRobert.Thurlow@Sun.COM 			new_ah = nfs_create_ah(cl, hostname,
310211291SRobert.Thurlow@Sun.COM 			    &mfssnego->nfs_sec);
310311291SRobert.Thurlow@Sun.COM 			if (new_ah  == NULL)
310411291SRobert.Thurlow@Sun.COM 				goto done;
310511291SRobert.Thurlow@Sun.COM #ifdef MALLOC_DEBUG
310611291SRobert.Thurlow@Sun.COM 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
310711291SRobert.Thurlow@Sun.COM 			    __FILE__, __LINE__);
310811291SRobert.Thurlow@Sun.COM #endif
310911291SRobert.Thurlow@Sun.COM 			AUTH_DESTROY(cl->cl_auth);
311011291SRobert.Thurlow@Sun.COM 			cl->cl_auth = new_ah;
311111291SRobert.Thurlow@Sun.COM #ifdef MALLOC_DEBUG
311211291SRobert.Thurlow@Sun.COM 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
311311291SRobert.Thurlow@Sun.COM 			    __FILE__, __LINE__);
311411291SRobert.Thurlow@Sun.COM #endif
311511291SRobert.Thurlow@Sun.COM 		} else if (sec == SNEGO_ARRAY_TOO_SMALL ||
311611291SRobert.Thurlow@Sun.COM 		    sec == SNEGO_FAILURE) {
311711291SRobert.Thurlow@Sun.COM 			goto done;
311811291SRobert.Thurlow@Sun.COM 		}
311911291SRobert.Thurlow@Sun.COM 	}
312011291SRobert.Thurlow@Sun.COM 
312111291SRobert.Thurlow@Sun.COM 	switch (vers) {
312211291SRobert.Thurlow@Sun.COM 	case NFS_VERSION:
312311291SRobert.Thurlow@Sun.COM 		{
312411291SRobert.Thurlow@Sun.COM 		wnl_diropargs arg;
312511291SRobert.Thurlow@Sun.COM 		wnl_diropres res;
312611291SRobert.Thurlow@Sun.COM 
312711291SRobert.Thurlow@Sun.COM 		memset((char *)&arg.dir, 0, sizeof (wnl_fh));
312811291SRobert.Thurlow@Sun.COM 		memset((char *)&res, 0, sizeof (wnl_diropres));
312911291SRobert.Thurlow@Sun.COM 		arg.name = fspath;
313011291SRobert.Thurlow@Sun.COM 		if (wnlproc_lookup_2(&arg, &res, cl) !=
313111291SRobert.Thurlow@Sun.COM 		    RPC_SUCCESS || res.status != NFS_OK)
313211291SRobert.Thurlow@Sun.COM 			goto done;
313311291SRobert.Thurlow@Sun.COM 		*fhp = malloc(sizeof (wnl_fh));
313411291SRobert.Thurlow@Sun.COM 
313511291SRobert.Thurlow@Sun.COM 		if (*fhp == NULL) {
313611291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "no memory\n");
313711291SRobert.Thurlow@Sun.COM 			goto done;
313811291SRobert.Thurlow@Sun.COM 		}
313911291SRobert.Thurlow@Sun.COM 
314011291SRobert.Thurlow@Sun.COM 		memcpy((char *)*fhp,
314111291SRobert.Thurlow@Sun.COM 		    (char *)&res.wnl_diropres_u.wnl_diropres.file,
314211291SRobert.Thurlow@Sun.COM 		    sizeof (wnl_fh));
314311291SRobert.Thurlow@Sun.COM 		cs = RPC_SUCCESS;
314411291SRobert.Thurlow@Sun.COM 		}
314511291SRobert.Thurlow@Sun.COM 		break;
314611291SRobert.Thurlow@Sun.COM 	case NFS_V3:
314711291SRobert.Thurlow@Sun.COM 		{
314811291SRobert.Thurlow@Sun.COM 		WNL_LOOKUP3args arg;
314911291SRobert.Thurlow@Sun.COM 		WNL_LOOKUP3res res;
315011291SRobert.Thurlow@Sun.COM 		nfs_fh3 *fh3p;
315111291SRobert.Thurlow@Sun.COM 
315211291SRobert.Thurlow@Sun.COM 		memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
315311291SRobert.Thurlow@Sun.COM 		memset((char *)&res, 0, sizeof (WNL_LOOKUP3res));
315411291SRobert.Thurlow@Sun.COM 		arg.what.name = fspath;
315511291SRobert.Thurlow@Sun.COM 		if (wnlproc3_lookup_3(&arg, &res, cl) !=
315611291SRobert.Thurlow@Sun.COM 		    RPC_SUCCESS || res.status != NFS3_OK)
315711291SRobert.Thurlow@Sun.COM 			goto done;
315811291SRobert.Thurlow@Sun.COM 
315911291SRobert.Thurlow@Sun.COM 		fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
316011291SRobert.Thurlow@Sun.COM 
316111291SRobert.Thurlow@Sun.COM 		if (fh3p == NULL) {
316211291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "no memory\n");
316311291SRobert.Thurlow@Sun.COM 			goto done;
316411291SRobert.Thurlow@Sun.COM 		}
316511291SRobert.Thurlow@Sun.COM 
316611291SRobert.Thurlow@Sun.COM 		fh3p->fh3_length =
316711291SRobert.Thurlow@Sun.COM 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_len;
316811291SRobert.Thurlow@Sun.COM 		memcpy(fh3p->fh3_u.data,
316911291SRobert.Thurlow@Sun.COM 		    res.WNL_LOOKUP3res_u.res_ok.object.data.data_val,
317011291SRobert.Thurlow@Sun.COM 		    fh3p->fh3_length);
317111291SRobert.Thurlow@Sun.COM 
317211291SRobert.Thurlow@Sun.COM 		*fhp = (caddr_t)fh3p;
317311291SRobert.Thurlow@Sun.COM 
317411291SRobert.Thurlow@Sun.COM 		cs = RPC_SUCCESS;
317511291SRobert.Thurlow@Sun.COM 		}
317611291SRobert.Thurlow@Sun.COM 		break;
317711291SRobert.Thurlow@Sun.COM 	case NFS_V4:
317811291SRobert.Thurlow@Sun.COM 		tv.tv_sec = 10;
317911291SRobert.Thurlow@Sun.COM 		tv.tv_usec = 0;
318011291SRobert.Thurlow@Sun.COM 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
318111291SRobert.Thurlow@Sun.COM 		    xdr_void, 0, tv);
318211291SRobert.Thurlow@Sun.COM 		if (cs != RPC_SUCCESS)
318311291SRobert.Thurlow@Sun.COM 			goto done;
318411291SRobert.Thurlow@Sun.COM 
318511291SRobert.Thurlow@Sun.COM 		*fhp = strdup(fspath);
318611291SRobert.Thurlow@Sun.COM 		if (fhp == NULL) {
318711291SRobert.Thurlow@Sun.COM 			cs = RPC_SYSTEMERROR;
318811291SRobert.Thurlow@Sun.COM 			goto done;
318911291SRobert.Thurlow@Sun.COM 		}
319011291SRobert.Thurlow@Sun.COM 		break;
319111291SRobert.Thurlow@Sun.COM 	}
319211291SRobert.Thurlow@Sun.COM 	nb = (struct netbuf *)malloc(sizeof (struct netbuf));
319311291SRobert.Thurlow@Sun.COM 	if (nb == NULL) {
319411291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "no memory\n");
319511291SRobert.Thurlow@Sun.COM 		cs = RPC_SYSTEMERROR;
319611291SRobert.Thurlow@Sun.COM 		goto done;
319711291SRobert.Thurlow@Sun.COM 	}
319811291SRobert.Thurlow@Sun.COM 	nb->buf = (char *)malloc(tbind->addr.maxlen);
319911291SRobert.Thurlow@Sun.COM 	if (nb->buf == NULL) {
320011291SRobert.Thurlow@Sun.COM 		syslog(LOG_ERR, "no memory\n");
320111291SRobert.Thurlow@Sun.COM 		free(nb);
320211291SRobert.Thurlow@Sun.COM 		nb = NULL;
320311291SRobert.Thurlow@Sun.COM 		cs = RPC_SYSTEMERROR;
320411291SRobert.Thurlow@Sun.COM 		goto done;
320511291SRobert.Thurlow@Sun.COM 	}
320611291SRobert.Thurlow@Sun.COM 	(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
320711291SRobert.Thurlow@Sun.COM 	nb->len = tbind->addr.len;
320811291SRobert.Thurlow@Sun.COM 	nb->maxlen = tbind->addr.maxlen;
320911291SRobert.Thurlow@Sun.COM done:
321011291SRobert.Thurlow@Sun.COM 	if (cstat != NULL)
321111291SRobert.Thurlow@Sun.COM 		*cstat = cs;
321211291SRobert.Thurlow@Sun.COM 	destroy_auth_client_handle(cl);
321311291SRobert.Thurlow@Sun.COM 	cleanup_tli_parms(tbind, fd);
321411291SRobert.Thurlow@Sun.COM 	return (nb);
321511291SRobert.Thurlow@Sun.COM }
32160Sstevel@tonic-gate 
32170Sstevel@tonic-gate /*
32180Sstevel@tonic-gate  * Sends a null call to the remote host's (NFS program, versp). versp
32190Sstevel@tonic-gate  * may be "NULL" in which case the default maximum version is used.
32200Sstevel@tonic-gate  * Upon return, versp contains the maximum version supported iff versp!= NULL.
32210Sstevel@tonic-gate  */
32220Sstevel@tonic-gate enum clnt_stat
pingnfs(char * hostpart,int attempts,rpcvers_t * versp,rpcvers_t versmin,ushort_t port,bool_t usepub,char * path,char * proto)32230Sstevel@tonic-gate pingnfs(
32240Sstevel@tonic-gate 	char *hostpart,
32250Sstevel@tonic-gate 	int attempts,
32260Sstevel@tonic-gate 	rpcvers_t *versp,
32270Sstevel@tonic-gate 	rpcvers_t versmin,
32283767Sjs195444 	ushort_t port,			/* may be zero */
32290Sstevel@tonic-gate 	bool_t usepub,
32300Sstevel@tonic-gate 	char *path,
32310Sstevel@tonic-gate 	char *proto)
32320Sstevel@tonic-gate {
32330Sstevel@tonic-gate 	CLIENT *cl = NULL;
32340Sstevel@tonic-gate 	struct timeval rpc_to_new = {15, 0};
32350Sstevel@tonic-gate 	static struct timeval rpc_rtrans_new = {-1, -1};
32360Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
32370Sstevel@tonic-gate 	int i, j;
32380Sstevel@tonic-gate 	rpcvers_t versmax;	/* maximum version to try against server */
32390Sstevel@tonic-gate 	rpcvers_t outvers;	/* version supported by host on last call */
32400Sstevel@tonic-gate 	rpcvers_t vers_to_try;	/* to try different versions against host */
32413767Sjs195444 	char *hostname;
32420Sstevel@tonic-gate 	struct netconfig *nconf;
32430Sstevel@tonic-gate 
32443767Sjs195444 	hostname = strdup(hostpart);
32453767Sjs195444 	if (hostname == NULL) {
32463767Sjs195444 		return (RPC_SYSTEMERROR);
32473767Sjs195444 	}
32483767Sjs195444 	unbracket(&hostname);
32493767Sjs195444 
32500Sstevel@tonic-gate 	if (path != NULL && strcmp(hostname, "nfs") == 0 &&
32510Sstevel@tonic-gate 	    strncmp(path, "//", 2) == 0) {
32520Sstevel@tonic-gate 		char *sport;
32530Sstevel@tonic-gate 
32540Sstevel@tonic-gate 		hostname = strdup(path+2);
32550Sstevel@tonic-gate 
32560Sstevel@tonic-gate 		if (hostname == NULL)
32570Sstevel@tonic-gate 			return (RPC_SYSTEMERROR);
32580Sstevel@tonic-gate 
32590Sstevel@tonic-gate 		path = strchr(hostname, '/');
32600Sstevel@tonic-gate 
32610Sstevel@tonic-gate 		/*
32620Sstevel@tonic-gate 		 * This cannot happen. If it does, give up
32630Sstevel@tonic-gate 		 * on the ping as this is obviously a corrupt
32640Sstevel@tonic-gate 		 * entry.
32650Sstevel@tonic-gate 		 */
32660Sstevel@tonic-gate 		if (path == NULL) {
32670Sstevel@tonic-gate 			free(hostname);
32680Sstevel@tonic-gate 			return (RPC_SUCCESS);
32690Sstevel@tonic-gate 		}
32700Sstevel@tonic-gate 
32710Sstevel@tonic-gate 		/*
32720Sstevel@tonic-gate 		 * Probable end point of host string.
32730Sstevel@tonic-gate 		 */
32740Sstevel@tonic-gate 		*path = '\0';
32750Sstevel@tonic-gate 
32760Sstevel@tonic-gate 		sport = strchr(hostname, ':');
32770Sstevel@tonic-gate 
32780Sstevel@tonic-gate 		if (sport != NULL && sport < path) {
32790Sstevel@tonic-gate 
32800Sstevel@tonic-gate 			/*
32810Sstevel@tonic-gate 			 * Actual end point of host string.
32820Sstevel@tonic-gate 			 */
32830Sstevel@tonic-gate 			*sport = '\0';
32840Sstevel@tonic-gate 			port = htons((ushort_t)atoi(sport+1));
32850Sstevel@tonic-gate 		}
32860Sstevel@tonic-gate 
32870Sstevel@tonic-gate 		usepub = TRUE;
32880Sstevel@tonic-gate 	}
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	/* Pick up the default versions and then set them appropriately */
32910Sstevel@tonic-gate 	if (versp) {
32920Sstevel@tonic-gate 		versmax = *versp;
32930Sstevel@tonic-gate 		/* use versmin passed in */
32940Sstevel@tonic-gate 	} else {
32950Sstevel@tonic-gate 		read_default_nfs();
32960Sstevel@tonic-gate 		set_versrange(0, &versmax, &versmin);
32970Sstevel@tonic-gate 	}
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 	if (proto &&
33000Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0 &&
33010Sstevel@tonic-gate 	    versmax == NFS_V4) {
33020Sstevel@tonic-gate 		if (versmin == NFS_V4) {
33030Sstevel@tonic-gate 			if (versp) {
33040Sstevel@tonic-gate 				*versp = versmax - 1;
33050Sstevel@tonic-gate 				return (RPC_SUCCESS);
33060Sstevel@tonic-gate 			}
33070Sstevel@tonic-gate 			return (RPC_PROGUNAVAIL);
33080Sstevel@tonic-gate 		} else {
33090Sstevel@tonic-gate 			versmax--;
33100Sstevel@tonic-gate 		}
33110Sstevel@tonic-gate 	}
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	if (versp)
33140Sstevel@tonic-gate 		*versp = versmax;
33150Sstevel@tonic-gate 
33160Sstevel@tonic-gate 	switch (cache_check(hostname, versp, proto)) {
33170Sstevel@tonic-gate 	case GOODHOST:
33180Sstevel@tonic-gate 		if (hostname != hostpart)
33190Sstevel@tonic-gate 			free(hostname);
33200Sstevel@tonic-gate 		return (RPC_SUCCESS);
33210Sstevel@tonic-gate 	case DEADHOST:
33220Sstevel@tonic-gate 		if (hostname != hostpart)
33230Sstevel@tonic-gate 			free(hostname);
33240Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
33250Sstevel@tonic-gate 	case NOHOST:
33260Sstevel@tonic-gate 	default:
33270Sstevel@tonic-gate 		break;
33280Sstevel@tonic-gate 	}
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 	/*
33310Sstevel@tonic-gate 	 * XXX The retransmission time rpcbrmttime is a global defined
33320Sstevel@tonic-gate 	 * in the rpc library (rpcb_clnt.c). We use (and like) the default
33330Sstevel@tonic-gate 	 * value of 15 sec in the rpc library. The code below is to protect
33340Sstevel@tonic-gate 	 * us in case it changes. This need not be done under a lock since
33350Sstevel@tonic-gate 	 * any # of threads entering this function will get the same
33360Sstevel@tonic-gate 	 * retransmission value.
33370Sstevel@tonic-gate 	 */
33380Sstevel@tonic-gate 	if (rpc_rtrans_new.tv_sec == -1 && rpc_rtrans_new.tv_usec == -1) {
33390Sstevel@tonic-gate 		__rpc_control(CLCR_GET_RPCB_RMTTIME, (char *)&rpc_rtrans_new);
33400Sstevel@tonic-gate 		if (rpc_rtrans_new.tv_sec != 15 && rpc_rtrans_new.tv_sec != 0)
33410Sstevel@tonic-gate 			if (trace > 1)
33420Sstevel@tonic-gate 				trace_prt(1, "RPC library rttimer changed\n");
33430Sstevel@tonic-gate 	}
33440Sstevel@tonic-gate 
33450Sstevel@tonic-gate 	/*
33460Sstevel@tonic-gate 	 * XXX Manipulate the total timeout to get the number of
33470Sstevel@tonic-gate 	 * desired retransmissions. This code is heavily dependant on
33480Sstevel@tonic-gate 	 * the RPC backoff mechanism in clnt_dg_call (clnt_dg.c).
33490Sstevel@tonic-gate 	 */
33500Sstevel@tonic-gate 	for (i = 0, j = rpc_rtrans_new.tv_sec; i < attempts-1; i++) {
33510Sstevel@tonic-gate 		if (j < RPC_MAX_BACKOFF)
33520Sstevel@tonic-gate 			j *= 2;
33530Sstevel@tonic-gate 		else
33540Sstevel@tonic-gate 			j = RPC_MAX_BACKOFF;
33550Sstevel@tonic-gate 		rpc_to_new.tv_sec += j;
33560Sstevel@tonic-gate 	}
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 	vers_to_try = versmax;
33590Sstevel@tonic-gate 
33600Sstevel@tonic-gate 	/*
33610Sstevel@tonic-gate 	 * check the host's version within the timeout
33620Sstevel@tonic-gate 	 */
33630Sstevel@tonic-gate 	if (trace > 1)
33640Sstevel@tonic-gate 		trace_prt(1, "	ping: %s timeout=%ld request vers=%d min=%d\n",
33654590Srm15945 		    hostname, rpc_to_new.tv_sec, versmax, versmin);
33660Sstevel@tonic-gate 
33670Sstevel@tonic-gate 	if (usepub == FALSE) {
33684590Srm15945 		do {
33694590Srm15945 			/*
33704590Srm15945 			 * If NFSv4, then we do the same thing as is used
33714590Srm15945 			 * for public filehandles so that we avoid rpcbind
33724590Srm15945 			 */
33734590Srm15945 			if (vers_to_try == NFS_V4) {
33744590Srm15945 				if (trace > 4) {
33750Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
33764590Srm15945 				    "\"circuit_v\"\n");
33770Sstevel@tonic-gate 				}
33780Sstevel@tonic-gate 
33794590Srm15945 				cl = clnt_create_service_timed(hostname, "nfs",
33804590Srm15945 				    NFS_PROGRAM, vers_to_try,
33814590Srm15945 				    port, "circuit_v", &rpc_to_new);
33824590Srm15945 				if (cl != NULL) {
33834590Srm15945 					outvers = vers_to_try;
33844590Srm15945 					break;
33854590Srm15945 				}
33860Sstevel@tonic-gate 				if (trace > 4) {
33874590Srm15945 					trace_prt(1,
33884590Srm15945 					    "  pingnfs: Can't ping via "
33894590Srm15945 					    "\"circuit_v\" %s: RPC error=%d\n",
33904590Srm15945 					    hostname, rpc_createerr.cf_stat);
33910Sstevel@tonic-gate 				}
33924590Srm15945 
33934590Srm15945 			} else {
33944590Srm15945 				cl = clnt_create_vers_timed(hostname,
33954590Srm15945 				    NFS_PROGRAM, &outvers, versmin, vers_to_try,
33964590Srm15945 				    "datagram_v", &rpc_to_new);
33974590Srm15945 				if (cl != NULL)
33980Sstevel@tonic-gate 					break;
33990Sstevel@tonic-gate 				if (trace > 4) {
34004590Srm15945 					trace_prt(1,
34014590Srm15945 					    "  pingnfs: Can't ping via "
34024590Srm15945 					    "\"datagram_v\"%s: RPC error=%d\n",
34034590Srm15945 					    hostname, rpc_createerr.cf_stat);
34044590Srm15945 				}
34054590Srm15945 				if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
34064590Srm15945 				    rpc_createerr.cf_stat == RPC_TIMEDOUT)
34074590Srm15945 					break;
34084590Srm15945 				if (rpc_createerr.cf_stat ==
34094590Srm15945 				    RPC_PROGNOTREGISTERED) {
34104590Srm15945 					if (trace > 4) {
34114590Srm15945 						trace_prt(1,
34124590Srm15945 						    "  pingnfs: Trying ping "
34134590Srm15945 						    "via \"circuit_v\"\n");
34144590Srm15945 					}
34154590Srm15945 					cl = clnt_create_vers_timed(hostname,
34164590Srm15945 					    NFS_PROGRAM, &outvers,
34174590Srm15945 					    versmin, vers_to_try,
34184590Srm15945 					    "circuit_v", &rpc_to_new);
34194590Srm15945 					if (cl != NULL)
34204590Srm15945 						break;
34214590Srm15945 					if (trace > 4) {
34224590Srm15945 						trace_prt(1,
34234590Srm15945 						    "  pingnfs: Can't ping "
34244590Srm15945 						    "via \"circuit_v\" %s: "
34254590Srm15945 						    "RPC error=%d\n",
34264590Srm15945 						    hostname,
34274590Srm15945 						    rpc_createerr.cf_stat);
34284590Srm15945 					}
34290Sstevel@tonic-gate 				}
34300Sstevel@tonic-gate 			}
34310Sstevel@tonic-gate 
34320Sstevel@tonic-gate 		/*
34330Sstevel@tonic-gate 		 * backoff and return lower version to retry the ping.
34340Sstevel@tonic-gate 		 * XXX we should be more careful and handle
34350Sstevel@tonic-gate 		 * RPC_PROGVERSMISMATCH here, because that error is handled
34360Sstevel@tonic-gate 		 * in clnt_create_vers(). It's not done to stay in sync
34370Sstevel@tonic-gate 		 * with the nfs mount command.
34380Sstevel@tonic-gate 		 */
34394590Srm15945 			vers_to_try--;
34404590Srm15945 			if (vers_to_try < versmin)
34414590Srm15945 				break;
34424590Srm15945 			if (versp != NULL) {	/* recheck the cache */
34434590Srm15945 				*versp = vers_to_try;
34444590Srm15945 				if (trace > 4) {
34454590Srm15945 					trace_prt(1,
34464590Srm15945 					    "  pingnfs: check cache: vers=%d\n",
34474590Srm15945 					    *versp);
34484590Srm15945 				}
34494590Srm15945 				switch (cache_check(hostname, versp, proto)) {
34504590Srm15945 				case GOODHOST:
34514590Srm15945 					if (hostname != hostpart)
34524590Srm15945 						free(hostname);
34534590Srm15945 					return (RPC_SUCCESS);
34544590Srm15945 				case DEADHOST:
34554590Srm15945 					if (hostname != hostpart)
34564590Srm15945 						free(hostname);
34574590Srm15945 					return (RPC_TIMEDOUT);
34584590Srm15945 				case NOHOST:
34594590Srm15945 				default:
34604590Srm15945 					break;
34614590Srm15945 				}
34624590Srm15945 			}
34634590Srm15945 			if (trace > 4) {
34644590Srm15945 				trace_prt(1, "  pingnfs: Try version=%d\n",
34654590Srm15945 				    vers_to_try);
34664590Srm15945 			}
34674590Srm15945 		} while (cl == NULL);
34684590Srm15945 
34694590Srm15945 
34704590Srm15945 		if (cl == NULL) {
34714590Srm15945 			if (verbose)
34724590Srm15945 				syslog(LOG_ERR, "pingnfs: %s%s",
34730Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
34744590Srm15945 			clnt_stat = rpc_createerr.cf_stat;
34754590Srm15945 		} else {
34764590Srm15945 			clnt_destroy(cl);
34774590Srm15945 			clnt_stat = RPC_SUCCESS;
34784590Srm15945 		}
34790Sstevel@tonic-gate 
34800Sstevel@tonic-gate 	} else {
34810Sstevel@tonic-gate 		for (vers_to_try = versmax; vers_to_try >= versmin;
34820Sstevel@tonic-gate 		    vers_to_try--) {
34830Sstevel@tonic-gate 
34840Sstevel@tonic-gate 			nconf = NULL;
34850Sstevel@tonic-gate 
34860Sstevel@tonic-gate 			if (trace > 4) {
34870Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d "
34884590Srm15945 				    "using get_ping()\n", vers_to_try);
34890Sstevel@tonic-gate 			}
34900Sstevel@tonic-gate 
34910Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
34924590Srm15945 			    vers_to_try, &nconf, port, TRUE);
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 			if (nconf != NULL)
34950Sstevel@tonic-gate 				freenetconfigent(nconf);
34960Sstevel@tonic-gate 
34970Sstevel@tonic-gate 			if (clnt_stat == RPC_SUCCESS) {
34980Sstevel@tonic-gate 				outvers = vers_to_try;
34990Sstevel@tonic-gate 				break;
35000Sstevel@tonic-gate 			}
35010Sstevel@tonic-gate 		}
35020Sstevel@tonic-gate 	}
35030Sstevel@tonic-gate 
35040Sstevel@tonic-gate 	if (trace > 1)
35050Sstevel@tonic-gate 		clnt_stat == RPC_SUCCESS ?
35064590Srm15945 		    trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
35074590Srm15945 		    trace_prt(1, "	pingnfs FAIL: can't get nfs version\n");
35080Sstevel@tonic-gate 
35090Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
35100Sstevel@tonic-gate 		cache_enter(hostname, versmax, outvers, proto, GOODHOST);
35110Sstevel@tonic-gate 		if (versp != NULL)
35120Sstevel@tonic-gate 			*versp = outvers;
35130Sstevel@tonic-gate 	} else
35140Sstevel@tonic-gate 		cache_enter(hostname, versmax, versmax, proto, DEADHOST);
35150Sstevel@tonic-gate 
35160Sstevel@tonic-gate 	if (hostpart != hostname)
35170Sstevel@tonic-gate 		free(hostname);
35180Sstevel@tonic-gate 
35190Sstevel@tonic-gate 	return (clnt_stat);
35200Sstevel@tonic-gate }
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate #define	MNTTYPE_LOFS	"lofs"
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate int
loopbackmount(fsname,dir,mntopts,overlay)35250Sstevel@tonic-gate loopbackmount(fsname, dir, mntopts, overlay)
35260Sstevel@tonic-gate 	char *fsname;		/* Directory being mounted */
35270Sstevel@tonic-gate 	char *dir;		/* Directory being mounted on */
35280Sstevel@tonic-gate 	char *mntopts;
35290Sstevel@tonic-gate 	int overlay;
35300Sstevel@tonic-gate {
35310Sstevel@tonic-gate 	struct mnttab mnt;
35320Sstevel@tonic-gate 	int flags = 0;
35330Sstevel@tonic-gate 	char fstype[] = MNTTYPE_LOFS;
35340Sstevel@tonic-gate 	int dirlen;
35350Sstevel@tonic-gate 	struct stat st;
35360Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
35370Sstevel@tonic-gate 
35380Sstevel@tonic-gate 	dirlen = strlen(dir);
35390Sstevel@tonic-gate 	if (dir[dirlen-1] == ' ')
35400Sstevel@tonic-gate 		dirlen--;
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate 	if (dirlen == strlen(fsname) &&
35430Sstevel@tonic-gate 		strncmp(fsname, dir, dirlen) == 0) {
35440Sstevel@tonic-gate 		syslog(LOG_ERR,
35450Sstevel@tonic-gate 			"Mount of %s on %s would result in deadlock, aborted\n",
35460Sstevel@tonic-gate 			fsname, dir);
35470Sstevel@tonic-gate 		return (RET_ERR);
35480Sstevel@tonic-gate 	}
35490Sstevel@tonic-gate 	mnt.mnt_mntopts = mntopts;
35500Sstevel@tonic-gate 	if (hasmntopt(&mnt, MNTOPT_RO) != NULL)
35510Sstevel@tonic-gate 		flags |= MS_RDONLY;
35520Sstevel@tonic-gate 
35530Sstevel@tonic-gate 	(void) strlcpy(optbuf, mntopts, sizeof (optbuf));
35540Sstevel@tonic-gate 
35550Sstevel@tonic-gate 	if (overlay)
35560Sstevel@tonic-gate 		flags |= MS_OVERLAY;
35570Sstevel@tonic-gate 
35580Sstevel@tonic-gate 	if (trace > 1)
35590Sstevel@tonic-gate 		trace_prt(1,
35600Sstevel@tonic-gate 			"  loopbackmount: fsname=%s, dir=%s, flags=%d\n",
35610Sstevel@tonic-gate 			fsname, dir, flags);
35620Sstevel@tonic-gate 
35631676Sjpk 	if (is_system_labeled()) {
35641676Sjpk 		if (create_homedir((const char *)fsname,
35651676Sjpk 		    (const char *)dir) == 0) {
35661676Sjpk 			return (NFSERR_NOENT);
35671676Sjpk 		}
35681676Sjpk 	}
35691676Sjpk 
35700Sstevel@tonic-gate 	if (mount(fsname, dir, flags | MS_DATA | MS_OPTIONSTR, fstype,
35710Sstevel@tonic-gate 	    NULL, 0, optbuf, sizeof (optbuf)) < 0) {
35720Sstevel@tonic-gate 		syslog(LOG_ERR, "Mount of %s on %s: %m", fsname, dir);
35730Sstevel@tonic-gate 		return (RET_ERR);
35740Sstevel@tonic-gate 	}
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	if (stat(dir, &st) == 0) {
35770Sstevel@tonic-gate 		if (trace > 1) {
35780Sstevel@tonic-gate 			trace_prt(1,
35790Sstevel@tonic-gate 			    "  loopbackmount of %s on %s dev=%x rdev=%x OK\n",
35800Sstevel@tonic-gate 			    fsname, dir, st.st_dev, st.st_rdev);
35810Sstevel@tonic-gate 		}
35820Sstevel@tonic-gate 	} else {
35830Sstevel@tonic-gate 		if (trace > 1) {
35840Sstevel@tonic-gate 			trace_prt(1,
35850Sstevel@tonic-gate 			    "  loopbackmount of %s on %s OK\n", fsname, dir);
35860Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", dir);
35870Sstevel@tonic-gate 		}
35880Sstevel@tonic-gate 	}
35890Sstevel@tonic-gate 
35900Sstevel@tonic-gate 	return (0);
35910Sstevel@tonic-gate }
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate /*
35940Sstevel@tonic-gate  * Look for the value of a numeric option of the form foo=x.  If found, set
35950Sstevel@tonic-gate  * *valp to the value and return non-zero.  If not found or the option is
35960Sstevel@tonic-gate  * malformed, return zero.
35970Sstevel@tonic-gate  */
35980Sstevel@tonic-gate 
35990Sstevel@tonic-gate int
nopt(mnt,opt,valp)36000Sstevel@tonic-gate nopt(mnt, opt, valp)
36010Sstevel@tonic-gate 	struct mnttab *mnt;
36020Sstevel@tonic-gate 	char *opt;
36030Sstevel@tonic-gate 	int *valp;			/* OUT */
36040Sstevel@tonic-gate {
36050Sstevel@tonic-gate 	char *equal;
36060Sstevel@tonic-gate 	char *str;
36070Sstevel@tonic-gate 
36080Sstevel@tonic-gate 	/*
36090Sstevel@tonic-gate 	 * We should never get a null pointer, but if we do, it's better to
36100Sstevel@tonic-gate 	 * ignore the option than to dump core.
36110Sstevel@tonic-gate 	 */
36120Sstevel@tonic-gate 
36130Sstevel@tonic-gate 	if (valp == NULL) {
36140Sstevel@tonic-gate 		syslog(LOG_DEBUG, "null pointer for %s option", opt);
36150Sstevel@tonic-gate 		return (0);
36160Sstevel@tonic-gate 	}
36170Sstevel@tonic-gate 
36180Sstevel@tonic-gate 	if (str = hasmntopt(mnt, opt)) {
36190Sstevel@tonic-gate 		if (equal = strchr(str, '=')) {
36200Sstevel@tonic-gate 			*valp = atoi(&equal[1]);
36210Sstevel@tonic-gate 			return (1);
36220Sstevel@tonic-gate 		} else {
36230Sstevel@tonic-gate 			syslog(LOG_ERR, "Bad numeric option '%s'", str);
36240Sstevel@tonic-gate 		}
36250Sstevel@tonic-gate 	}
36260Sstevel@tonic-gate 	return (0);
36270Sstevel@tonic-gate }
36280Sstevel@tonic-gate 
3629249Sjwahlig int
nfsunmount(mnt)36300Sstevel@tonic-gate nfsunmount(mnt)
36310Sstevel@tonic-gate 	struct mnttab *mnt;
36320Sstevel@tonic-gate {
36330Sstevel@tonic-gate 	struct timeval timeout;
36340Sstevel@tonic-gate 	CLIENT *cl;
36350Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
36360Sstevel@tonic-gate 	char *host, *path;
36370Sstevel@tonic-gate 	struct replica *list;
36380Sstevel@tonic-gate 	int i, count = 0;
36390Sstevel@tonic-gate 	int isv4mount = is_v4_mount(mnt->mnt_mountp);
36400Sstevel@tonic-gate 
36410Sstevel@tonic-gate 	if (trace > 1)
36420Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s\n", mnt->mnt_mountp);
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	if (umount(mnt->mnt_mountp) < 0) {
36450Sstevel@tonic-gate 		if (trace > 1)
36460Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s FAILED\n",
36470Sstevel@tonic-gate 				mnt->mnt_mountp);
36480Sstevel@tonic-gate 		if (errno)
36490Sstevel@tonic-gate 			return (errno);
36500Sstevel@tonic-gate 	}
36510Sstevel@tonic-gate 
36520Sstevel@tonic-gate 	/*
36530Sstevel@tonic-gate 	 * If this is a NFSv4 mount, the mount protocol was not used
36540Sstevel@tonic-gate 	 * so we just return.
36550Sstevel@tonic-gate 	 */
36560Sstevel@tonic-gate 	if (isv4mount) {
36570Sstevel@tonic-gate 		if (trace > 1)
36580Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s OK\n",
36590Sstevel@tonic-gate 				mnt->mnt_mountp);
36600Sstevel@tonic-gate 		return (0);
36610Sstevel@tonic-gate 	}
36620Sstevel@tonic-gate 
36630Sstevel@tonic-gate 	/*
36640Sstevel@tonic-gate 	 * If mounted with -o public, then no need to contact server
36650Sstevel@tonic-gate 	 * because mount protocol was not used.
36660Sstevel@tonic-gate 	 */
36670Sstevel@tonic-gate 	if (hasmntopt(mnt, MNTOPT_PUBLIC) != NULL) {
36680Sstevel@tonic-gate 		return (0);
36690Sstevel@tonic-gate 	}
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate 	/*
36720Sstevel@tonic-gate 	 * The rest of this code is advisory to the server.
36730Sstevel@tonic-gate 	 * If it fails return success anyway.
36740Sstevel@tonic-gate 	 */
36750Sstevel@tonic-gate 
36760Sstevel@tonic-gate 	list = parse_replica(mnt->mnt_special, &count);
36770Sstevel@tonic-gate 	if (!list) {
36780Sstevel@tonic-gate 		if (count >= 0)
36790Sstevel@tonic-gate 			syslog(LOG_ERR,
36800Sstevel@tonic-gate 			    "Memory allocation failed: %m");
36810Sstevel@tonic-gate 		return (ENOMEM);
36820Sstevel@tonic-gate 	}
36830Sstevel@tonic-gate 
36840Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
36850Sstevel@tonic-gate 
36860Sstevel@tonic-gate 		host = list[i].host;
36870Sstevel@tonic-gate 		path = list[i].path;
36880Sstevel@tonic-gate 
36890Sstevel@tonic-gate 		/*
36900Sstevel@tonic-gate 		 * Skip file systems mounted using WebNFS, because mount
36910Sstevel@tonic-gate 		 * protocol was not used.
36920Sstevel@tonic-gate 		 */
36930Sstevel@tonic-gate 		if (strcmp(host, "nfs") == 0 && strncmp(path, "//", 2) == 0)
36940Sstevel@tonic-gate 			continue;
36950Sstevel@tonic-gate 
36960Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
36970Sstevel@tonic-gate 		if (cl == NULL)
36980Sstevel@tonic-gate 			break;
36990Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37000Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
37010Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
37020Sstevel@tonic-gate 			__FILE__, __LINE__);
37030Sstevel@tonic-gate #endif
37040Sstevel@tonic-gate 		if (__clnt_bindresvport(cl) < 0) {
37050Sstevel@tonic-gate 			if (verbose)
37060Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37070Sstevel@tonic-gate 					host, path,
37080Sstevel@tonic-gate 					"Couldn't bind to reserved port");
37090Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37100Sstevel@tonic-gate 			continue;
37110Sstevel@tonic-gate 		}
37120Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37130Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
37140Sstevel@tonic-gate #endif
37150Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
37160Sstevel@tonic-gate 		if ((cl->cl_auth = authsys_create_default()) == NULL) {
37170Sstevel@tonic-gate 			if (verbose)
37180Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37190Sstevel@tonic-gate 					host, path,
37200Sstevel@tonic-gate 					"Failed creating default auth handle");
37210Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37220Sstevel@tonic-gate 			continue;
37230Sstevel@tonic-gate 		}
37240Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37250Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
37260Sstevel@tonic-gate #endif
37270Sstevel@tonic-gate 		timeout.tv_usec = 0;
37280Sstevel@tonic-gate 		timeout.tv_sec = 5;
37290Sstevel@tonic-gate 		rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
37300Sstevel@tonic-gate 			    (caddr_t)&path, xdr_void, (char *)NULL, timeout);
37310Sstevel@tonic-gate 		if (verbose && rpc_stat != RPC_SUCCESS)
37320Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s",
37330Sstevel@tonic-gate 				host, clnt_sperror(cl, "unmount"));
37340Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
37350Sstevel@tonic-gate 	}
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 	free_replica(list, count);
37380Sstevel@tonic-gate 
37390Sstevel@tonic-gate 	if (trace > 1)
37400Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s OK\n", mnt->mnt_mountp);
37410Sstevel@tonic-gate 
37420Sstevel@tonic-gate done:
37430Sstevel@tonic-gate 	return (0);
37440Sstevel@tonic-gate }
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate /*
37470Sstevel@tonic-gate  * Put a new entry in the cache chain by prepending it to the front.
37480Sstevel@tonic-gate  * If there isn't enough memory then just give up.
37490Sstevel@tonic-gate  */
37500Sstevel@tonic-gate static void
cache_enter(host,reqvers,outvers,proto,state)37510Sstevel@tonic-gate cache_enter(host, reqvers, outvers, proto, state)
37520Sstevel@tonic-gate 	char *host;
37530Sstevel@tonic-gate 	rpcvers_t reqvers;
37540Sstevel@tonic-gate 	rpcvers_t outvers;
37550Sstevel@tonic-gate 	char *proto;
37560Sstevel@tonic-gate 	int state;
37570Sstevel@tonic-gate {
37580Sstevel@tonic-gate 	struct cache_entry *entry;
37590Sstevel@tonic-gate 	int cache_time = 30;	/* sec */
37600Sstevel@tonic-gate 
37610Sstevel@tonic-gate 	timenow = time(NULL);
37620Sstevel@tonic-gate 
37630Sstevel@tonic-gate 	entry = (struct cache_entry *)malloc(sizeof (struct cache_entry));
37640Sstevel@tonic-gate 	if (entry == NULL)
37650Sstevel@tonic-gate 		return;
37660Sstevel@tonic-gate 	(void) memset((caddr_t)entry, 0, sizeof (struct cache_entry));
37670Sstevel@tonic-gate 	entry->cache_host = strdup(host);
37680Sstevel@tonic-gate 	if (entry->cache_host == NULL) {
37690Sstevel@tonic-gate 		cache_free(entry);
37700Sstevel@tonic-gate 		return;
37710Sstevel@tonic-gate 	}
37720Sstevel@tonic-gate 	entry->cache_reqvers = reqvers;
37730Sstevel@tonic-gate 	entry->cache_outvers = outvers;
37740Sstevel@tonic-gate 	entry->cache_proto = (proto == NULL ? NULL : strdup(proto));
37750Sstevel@tonic-gate 	entry->cache_state = state;
37760Sstevel@tonic-gate 	entry->cache_time = timenow + cache_time;
37770Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
37780Sstevel@tonic-gate #ifdef CACHE_DEBUG
37790Sstevel@tonic-gate 	host_cache_accesses++;		/* up host cache access counter */
37800Sstevel@tonic-gate #endif /* CACHE DEBUG */
37810Sstevel@tonic-gate 	entry->cache_next = cache_head;
37820Sstevel@tonic-gate 	cache_head = entry;
37830Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
37840Sstevel@tonic-gate }
37850Sstevel@tonic-gate 
37860Sstevel@tonic-gate static int
cache_check(host,versp,proto)37870Sstevel@tonic-gate cache_check(host, versp, proto)
37880Sstevel@tonic-gate 	char *host;
37890Sstevel@tonic-gate 	rpcvers_t *versp;
37900Sstevel@tonic-gate 	char *proto;
37910Sstevel@tonic-gate {
37920Sstevel@tonic-gate 	int state = NOHOST;
37930Sstevel@tonic-gate 	struct cache_entry *ce, *prev;
37940Sstevel@tonic-gate 
37950Sstevel@tonic-gate 	timenow = time(NULL);
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	(void) rw_rdlock(&cache_lock);
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate #ifdef CACHE_DEBUG
38000Sstevel@tonic-gate 	/* Increment the lookup and access counters for the host cache */
38010Sstevel@tonic-gate 	host_cache_accesses++;
38020Sstevel@tonic-gate 	host_cache_lookups++;
38030Sstevel@tonic-gate 	if ((host_cache_lookups%1000) == 0)
38040Sstevel@tonic-gate 		trace_host_cache();
38050Sstevel@tonic-gate #endif /* CACHE DEBUG */
38060Sstevel@tonic-gate 
38070Sstevel@tonic-gate 	for (ce = cache_head; ce; ce = ce->cache_next) {
38080Sstevel@tonic-gate 		if (timenow > ce->cache_time) {
38090Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38100Sstevel@tonic-gate 			(void) rw_wrlock(&cache_lock);
38110Sstevel@tonic-gate 			for (prev = NULL, ce = cache_head; ce;
38120Sstevel@tonic-gate 				prev = ce, ce = ce->cache_next) {
38130Sstevel@tonic-gate 				if (timenow > ce->cache_time) {
38140Sstevel@tonic-gate 					cache_free(ce);
38150Sstevel@tonic-gate 					if (prev)
38160Sstevel@tonic-gate 						prev->cache_next = NULL;
38170Sstevel@tonic-gate 					else
38180Sstevel@tonic-gate 						cache_head = NULL;
38190Sstevel@tonic-gate 					break;
38200Sstevel@tonic-gate 				}
38210Sstevel@tonic-gate 			}
38220Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38230Sstevel@tonic-gate 			return (state);
38240Sstevel@tonic-gate 		}
38250Sstevel@tonic-gate 		if (strcmp(host, ce->cache_host) != 0)
38260Sstevel@tonic-gate 			continue;
38270Sstevel@tonic-gate 		if ((proto == NULL && ce->cache_proto != NULL) ||
38280Sstevel@tonic-gate 		    (proto != NULL && ce->cache_proto == NULL))
38290Sstevel@tonic-gate 			continue;
38300Sstevel@tonic-gate 		if (proto != NULL &&
38310Sstevel@tonic-gate 		    strcmp(proto, ce->cache_proto) != 0)
38320Sstevel@tonic-gate 			continue;
38330Sstevel@tonic-gate 
38340Sstevel@tonic-gate 		if (versp == NULL ||
38350Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_reqvers) ||
38360Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_outvers)) {
38370Sstevel@tonic-gate 				if (versp != NULL)
38380Sstevel@tonic-gate 					*versp = ce->cache_outvers;
38390Sstevel@tonic-gate 				state = ce->cache_state;
38400Sstevel@tonic-gate 
38410Sstevel@tonic-gate 				/* increment the host cache hit counters */
38420Sstevel@tonic-gate #ifdef CACHE_DEBUG
38430Sstevel@tonic-gate 				if (state == GOODHOST)
38440Sstevel@tonic-gate 					goodhost_cache_hits++;
38450Sstevel@tonic-gate 				if (state == DEADHOST)
38460Sstevel@tonic-gate 					deadhost_cache_hits++;
38470Sstevel@tonic-gate #endif /* CACHE_DEBUG */
38480Sstevel@tonic-gate 				(void) rw_unlock(&cache_lock);
38490Sstevel@tonic-gate 				return (state);
38500Sstevel@tonic-gate 		}
38510Sstevel@tonic-gate 	}
38520Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38530Sstevel@tonic-gate 	return (state);
38540Sstevel@tonic-gate }
38550Sstevel@tonic-gate 
38560Sstevel@tonic-gate /*
38570Sstevel@tonic-gate  * Free a cache entry and all entries
38580Sstevel@tonic-gate  * further down the chain since they
38590Sstevel@tonic-gate  * will also be expired.
38600Sstevel@tonic-gate  */
38610Sstevel@tonic-gate static void
cache_free(entry)38620Sstevel@tonic-gate cache_free(entry)
38630Sstevel@tonic-gate 	struct cache_entry *entry;
38640Sstevel@tonic-gate {
38650Sstevel@tonic-gate 	struct cache_entry *ce, *next = NULL;
38660Sstevel@tonic-gate 
38670Sstevel@tonic-gate 	for (ce = entry; ce; ce = next) {
38680Sstevel@tonic-gate 		if (ce->cache_host)
38690Sstevel@tonic-gate 			free(ce->cache_host);
38700Sstevel@tonic-gate 		if (ce->cache_proto)
38710Sstevel@tonic-gate 			free(ce->cache_proto);
38720Sstevel@tonic-gate 		next = ce->cache_next;
38730Sstevel@tonic-gate 		free(ce);
38740Sstevel@tonic-gate 	}
38750Sstevel@tonic-gate }
38760Sstevel@tonic-gate 
38770Sstevel@tonic-gate #ifdef MALLOC_DEBUG
38780Sstevel@tonic-gate void
cache_flush()38790Sstevel@tonic-gate cache_flush()
38800Sstevel@tonic-gate {
38810Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
38820Sstevel@tonic-gate 	cache_free(cache_head);
38830Sstevel@tonic-gate 	cache_head = NULL;
38840Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate void
flush_caches()38880Sstevel@tonic-gate flush_caches()
38890Sstevel@tonic-gate {
38900Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
38910Sstevel@tonic-gate 	cond_signal(&cleanup_start_cv);
38920Sstevel@tonic-gate 	(void) cond_wait(&cleanup_done_cv, &cleanup_lock);
38930Sstevel@tonic-gate 	mutex_unlock(&cleanup_lock);
38940Sstevel@tonic-gate 	cache_flush();
38950Sstevel@tonic-gate 	portmap_cache_flush();
38960Sstevel@tonic-gate }
38970Sstevel@tonic-gate #endif
38980Sstevel@tonic-gate 
38990Sstevel@tonic-gate /*
39000Sstevel@tonic-gate  * Returns 1, if port option is NFS_PORT or
39010Sstevel@tonic-gate  *	nfsd is running on the port given
39020Sstevel@tonic-gate  * Returns 0, if both port is not NFS_PORT and nfsd is not
39030Sstevel@tonic-gate  *	running on the port.
39040Sstevel@tonic-gate  */
39050Sstevel@tonic-gate 
39060Sstevel@tonic-gate static int
is_nfs_port(char * opts)39070Sstevel@tonic-gate is_nfs_port(char *opts)
39080Sstevel@tonic-gate {
39090Sstevel@tonic-gate 	struct mnttab m;
39100Sstevel@tonic-gate 	uint_t nfs_port = 0;
39110Sstevel@tonic-gate 	struct servent sv;
39120Sstevel@tonic-gate 	char buf[256];
39130Sstevel@tonic-gate 	int got_port;
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 	m.mnt_mntopts = opts;
39160Sstevel@tonic-gate 
39170Sstevel@tonic-gate 	/*
39180Sstevel@tonic-gate 	 * Get port specified in options list, if any.
39190Sstevel@tonic-gate 	 */
39200Sstevel@tonic-gate 	got_port = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
39210Sstevel@tonic-gate 
39220Sstevel@tonic-gate 	/*
39230Sstevel@tonic-gate 	 * if no port specified or it is same as NFS_PORT return nfs
39240Sstevel@tonic-gate 	 * To use any other daemon the port number should be different
39250Sstevel@tonic-gate 	 */
39260Sstevel@tonic-gate 	if (!got_port || nfs_port == NFS_PORT)
39270Sstevel@tonic-gate 		return (1);
39280Sstevel@tonic-gate 	/*
39290Sstevel@tonic-gate 	 * If daemon is nfsd, return nfs
39300Sstevel@tonic-gate 	 */
39310Sstevel@tonic-gate 	if (getservbyport_r(nfs_port, NULL, &sv, buf, 256) == &sv &&
39324590Srm15945 	    strcmp(sv.s_name, "nfsd") == 0)
39330Sstevel@tonic-gate 		return (1);
39340Sstevel@tonic-gate 
39350Sstevel@tonic-gate 	/*
39360Sstevel@tonic-gate 	 * daemon is not nfs
39370Sstevel@tonic-gate 	 */
39380Sstevel@tonic-gate 	return (0);
39390Sstevel@tonic-gate }
39400Sstevel@tonic-gate 
39410Sstevel@tonic-gate 
39420Sstevel@tonic-gate /*
39430Sstevel@tonic-gate  * destroy_auth_client_handle(cl)
39440Sstevel@tonic-gate  * destroys the created client handle
39450Sstevel@tonic-gate  */
39462170Sevanl void
destroy_auth_client_handle(CLIENT * cl)39470Sstevel@tonic-gate destroy_auth_client_handle(CLIENT *cl)
39480Sstevel@tonic-gate {
39490Sstevel@tonic-gate 	if (cl) {
39500Sstevel@tonic-gate 		if (cl->cl_auth) {
39510Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39520Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
39534590Srm15945 			    __FILE__, __LINE__);
39540Sstevel@tonic-gate #endif
39550Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
39560Sstevel@tonic-gate 			cl->cl_auth = NULL;
39570Sstevel@tonic-gate 		}
39580Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39590Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl,
39604590Srm15945 		    __FILE__, __LINE__);
39610Sstevel@tonic-gate #endif
39620Sstevel@tonic-gate 		clnt_destroy(cl);
39630Sstevel@tonic-gate 	}
39640Sstevel@tonic-gate }
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 
39670Sstevel@tonic-gate /*
39680Sstevel@tonic-gate  * Attempt to figure out which version of NFS to use in pingnfs().  If
39690Sstevel@tonic-gate  * the version number was specified (i.e., non-zero), then use it.
39700Sstevel@tonic-gate  * Otherwise, default to the compiled-in default or the default as set
39710Sstevel@tonic-gate  * by the /etc/default/nfs configuration (as read by read_default().
39720Sstevel@tonic-gate  */
39730Sstevel@tonic-gate int
set_versrange(rpcvers_t nfsvers,rpcvers_t * vers,rpcvers_t * versmin)39740Sstevel@tonic-gate set_versrange(rpcvers_t nfsvers, rpcvers_t *vers, rpcvers_t *versmin)
39750Sstevel@tonic-gate {
39760Sstevel@tonic-gate 	switch (nfsvers) {
39770Sstevel@tonic-gate 	case 0:
39780Sstevel@tonic-gate 		*vers = vers_max_default;
39790Sstevel@tonic-gate 		*versmin = vers_min_default;
39800Sstevel@tonic-gate 		break;
39810Sstevel@tonic-gate 	case NFS_V4:
39820Sstevel@tonic-gate 		*vers = NFS_V4;
39830Sstevel@tonic-gate 		*versmin = NFS_V4;
39840Sstevel@tonic-gate 		break;
39850Sstevel@tonic-gate 	case NFS_V3:
39860Sstevel@tonic-gate 		*vers = NFS_V3;
39870Sstevel@tonic-gate 		*versmin = NFS_V3;
39880Sstevel@tonic-gate 		break;
39890Sstevel@tonic-gate 	case NFS_VERSION:
39900Sstevel@tonic-gate 		*vers = NFS_VERSION;		/* version 2 */
39910Sstevel@tonic-gate 		*versmin = NFS_VERSMIN;		/* version 2 */
39920Sstevel@tonic-gate 		break;
39930Sstevel@tonic-gate 	default:
39940Sstevel@tonic-gate 		return (-1);
39950Sstevel@tonic-gate 	}
39960Sstevel@tonic-gate 	return (0);
39970Sstevel@tonic-gate }
39980Sstevel@tonic-gate 
39990Sstevel@tonic-gate #ifdef CACHE_DEBUG
40000Sstevel@tonic-gate /*
40010Sstevel@tonic-gate  * trace_portmap_cache()
40020Sstevel@tonic-gate  * traces the portmap cache values at desired points
40030Sstevel@tonic-gate  */
40040Sstevel@tonic-gate static void
trace_portmap_cache()40050Sstevel@tonic-gate trace_portmap_cache()
40060Sstevel@tonic-gate {
40070Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache: accesses=%d lookups=%d hits=%d\n",
40084590Srm15945 	    portmap_cache_accesses, portmap_cache_lookups,
40094590Srm15945 	    portmap_cache_hits);
40100Sstevel@tonic-gate }
40110Sstevel@tonic-gate 
40120Sstevel@tonic-gate /*
40130Sstevel@tonic-gate  * trace_host_cache()
40140Sstevel@tonic-gate  * traces the host cache values at desired points
40150Sstevel@tonic-gate  */
40160Sstevel@tonic-gate static void
trace_host_cache()40170Sstevel@tonic-gate trace_host_cache()
40180Sstevel@tonic-gate {
40190Sstevel@tonic-gate 	syslog(LOG_ERR,
40204590Srm15945 	    "host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
40214590Srm15945 	    host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
40224590Srm15945 	    goodhost_cache_hits);
40230Sstevel@tonic-gate }
40240Sstevel@tonic-gate #endif /* CACHE_DEBUG */
40250Sstevel@tonic-gate 
40260Sstevel@tonic-gate /*
4027*13080SPavan.Mettu@Oracle.COM  * Read the NFS SMF properties to determine if the
40280Sstevel@tonic-gate  * client has been configured for a new min/max for the NFS version to
40290Sstevel@tonic-gate  * use.
40300Sstevel@tonic-gate  */
40310Sstevel@tonic-gate 
4032*13080SPavan.Mettu@Oracle.COM #define	SVC_NFS_CLIENT	"svc:/network/nfs/client"
40330Sstevel@tonic-gate 
40340Sstevel@tonic-gate static void
read_default_nfs(void)40350Sstevel@tonic-gate read_default_nfs(void)
40360Sstevel@tonic-gate {
40370Sstevel@tonic-gate 	static time_t lastread = 0;
40380Sstevel@tonic-gate 	struct stat buf;
4039*13080SPavan.Mettu@Oracle.COM 	char defval[4];
4040*13080SPavan.Mettu@Oracle.COM 	int errno, bufsz;
4041*13080SPavan.Mettu@Oracle.COM 	int tmp, ret = 0;
4042*13080SPavan.Mettu@Oracle.COM 
4043*13080SPavan.Mettu@Oracle.COM 	bufsz = 4;
4044*13080SPavan.Mettu@Oracle.COM 	ret = nfs_smf_get_prop("client_versmin", defval, DEFAULT_INSTANCE,
4045*13080SPavan.Mettu@Oracle.COM 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
4046*13080SPavan.Mettu@Oracle.COM 	if (ret == SA_OK) {
4047*13080SPavan.Mettu@Oracle.COM 		errno = 0;
4048*13080SPavan.Mettu@Oracle.COM 		tmp = strtol(defval, (char **)NULL, 10);
4049*13080SPavan.Mettu@Oracle.COM 		if (errno == 0) {
4050*13080SPavan.Mettu@Oracle.COM 			vers_min_default = tmp;
4051*13080SPavan.Mettu@Oracle.COM 		}
4052*13080SPavan.Mettu@Oracle.COM 	}
4053*13080SPavan.Mettu@Oracle.COM 
4054*13080SPavan.Mettu@Oracle.COM 	bufsz = 4;
4055*13080SPavan.Mettu@Oracle.COM 	ret = nfs_smf_get_prop("client_versmax", defval, DEFAULT_INSTANCE,
4056*13080SPavan.Mettu@Oracle.COM 	    SCF_TYPE_INTEGER, SVC_NFS_CLIENT, &bufsz);
4057*13080SPavan.Mettu@Oracle.COM 	if (ret == SA_OK) {
4058*13080SPavan.Mettu@Oracle.COM 		errno = 0;
4059*13080SPavan.Mettu@Oracle.COM 		tmp = strtol(defval, (char **)NULL, 10);
4060*13080SPavan.Mettu@Oracle.COM 		if (errno == 0) {
4061*13080SPavan.Mettu@Oracle.COM 			vers_max_default = tmp;
4062*13080SPavan.Mettu@Oracle.COM 		}
4063*13080SPavan.Mettu@Oracle.COM 	}
4064*13080SPavan.Mettu@Oracle.COM 
4065*13080SPavan.Mettu@Oracle.COM 	lastread = buf.st_mtime;
40660Sstevel@tonic-gate 
40670Sstevel@tonic-gate 	/*
4068*13080SPavan.Mettu@Oracle.COM 	 * Quick sanity check on the values picked up from the
4069*13080SPavan.Mettu@Oracle.COM 	 * defaults file.  Make sure that a mistake wasn't
4070*13080SPavan.Mettu@Oracle.COM 	 * made that will confuse things later on.
4071*13080SPavan.Mettu@Oracle.COM 	 * If so, reset to compiled-in defaults
40720Sstevel@tonic-gate 	 */
4073*13080SPavan.Mettu@Oracle.COM 	if (vers_min_default > vers_max_default ||
4074*13080SPavan.Mettu@Oracle.COM 	    vers_min_default < NFS_VERSMIN ||
4075*13080SPavan.Mettu@Oracle.COM 	    vers_max_default > NFS_VERSMAX) {
4076*13080SPavan.Mettu@Oracle.COM 		if (trace > 1) {
4077*13080SPavan.Mettu@Oracle.COM 			trace_prt(1,
4078*13080SPavan.Mettu@Oracle.COM 	"  read_default: version minimum/maximum incorrectly configured\n");
4079*13080SPavan.Mettu@Oracle.COM 			trace_prt(1,
4080*13080SPavan.Mettu@Oracle.COM "  read_default: config is min=%d, max%d. Resetting to min=%d, max%d\n",
4081*13080SPavan.Mettu@Oracle.COM 			    vers_min_default, vers_max_default,
4082*13080SPavan.Mettu@Oracle.COM 			    NFS_VERSMIN_DEFAULT,
4083*13080SPavan.Mettu@Oracle.COM 			    NFS_VERSMAX_DEFAULT);
40840Sstevel@tonic-gate 		}
4085*13080SPavan.Mettu@Oracle.COM 		vers_min_default = NFS_VERSMIN_DEFAULT;
4086*13080SPavan.Mettu@Oracle.COM 		vers_max_default = NFS_VERSMAX_DEFAULT;
40870Sstevel@tonic-gate 	}
40880Sstevel@tonic-gate }
40890Sstevel@tonic-gate 
40900Sstevel@tonic-gate /*
40910Sstevel@tonic-gate  *  Find the mnttab entry that corresponds to "name".
40920Sstevel@tonic-gate  *  We're not sure what the name represents: either
40930Sstevel@tonic-gate  *  a mountpoint name, or a special name (server:/path).
40940Sstevel@tonic-gate  *  Return the last entry in the file that matches.
40950Sstevel@tonic-gate  */
40960Sstevel@tonic-gate static struct extmnttab *
mnttab_find(dirname)40970Sstevel@tonic-gate mnttab_find(dirname)
40980Sstevel@tonic-gate 	char *dirname;
40990Sstevel@tonic-gate {
41000Sstevel@tonic-gate 	FILE *fp;
41010Sstevel@tonic-gate 	struct extmnttab mnt;
41020Sstevel@tonic-gate 	struct extmnttab *res = NULL;
41030Sstevel@tonic-gate 
41040Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
41050Sstevel@tonic-gate 	if (fp == NULL) {
41060Sstevel@tonic-gate 		if (trace > 1)
41070Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to open mnttab\n");
41080Sstevel@tonic-gate 		return (NULL);
41090Sstevel@tonic-gate 	}
41100Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
41110Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
41120Sstevel@tonic-gate 		    strcmp(mnt.mnt_special, dirname) == 0) {
41130Sstevel@tonic-gate 			if (res)
41140Sstevel@tonic-gate 				fsfreemnttab(res);
41150Sstevel@tonic-gate 			res = fsdupmnttab(&mnt);
41160Sstevel@tonic-gate 		}
41170Sstevel@tonic-gate 	}
41180Sstevel@tonic-gate 
41190Sstevel@tonic-gate 	resetmnttab(fp);
41200Sstevel@tonic-gate 	fclose(fp);
41210Sstevel@tonic-gate 	if (res == NULL) {
41220Sstevel@tonic-gate 		if (trace > 1)
41230Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to find %s\n",
41240Sstevel@tonic-gate 				dirname);
41250Sstevel@tonic-gate 	}
41260Sstevel@tonic-gate 	return (res);
41270Sstevel@tonic-gate }
41280Sstevel@tonic-gate 
41290Sstevel@tonic-gate /*
41300Sstevel@tonic-gate  * This function's behavior is taken from nfsstat.
41310Sstevel@tonic-gate  * Trying to determine what NFS version was used for the mount.
41320Sstevel@tonic-gate  */
41330Sstevel@tonic-gate static int
is_v4_mount(char * mntpath)41340Sstevel@tonic-gate is_v4_mount(char *mntpath)
41350Sstevel@tonic-gate {
41360Sstevel@tonic-gate 	kstat_ctl_t *kc = NULL;		/* libkstat cookie */
41370Sstevel@tonic-gate 	kstat_t *ksp;
41380Sstevel@tonic-gate 	ulong_t fsid;
41390Sstevel@tonic-gate 	struct mntinfo_kstat mik;
41400Sstevel@tonic-gate 	struct extmnttab *mntp;
41410Sstevel@tonic-gate 	uint_t mnt_minor;
41420Sstevel@tonic-gate 
41430Sstevel@tonic-gate 	if ((mntp = mnttab_find(mntpath)) == NULL)
41440Sstevel@tonic-gate 		return (FALSE);
41450Sstevel@tonic-gate 
41460Sstevel@tonic-gate 	/* save the minor number and free the struct so we don't forget */
41470Sstevel@tonic-gate 	mnt_minor = mntp->mnt_minor;
41480Sstevel@tonic-gate 	fsfreemnttab(mntp);
41490Sstevel@tonic-gate 
41500Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
41510Sstevel@tonic-gate 		return (FALSE);
41520Sstevel@tonic-gate 
41530Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
41540Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
41550Sstevel@tonic-gate 			continue;
41560Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
41570Sstevel@tonic-gate 			continue;
41580Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
41590Sstevel@tonic-gate 			continue;
41600Sstevel@tonic-gate 		if (mnt_minor != ksp->ks_instance)
41610Sstevel@tonic-gate 			continue;
41620Sstevel@tonic-gate 
41630Sstevel@tonic-gate 		if (kstat_read(kc, ksp, &mik) == -1)
41640Sstevel@tonic-gate 			continue;
41650Sstevel@tonic-gate 
41660Sstevel@tonic-gate 		(void) kstat_close(kc);
41670Sstevel@tonic-gate 		if (mik.mik_vers == 4)
41680Sstevel@tonic-gate 			return (TRUE);
41690Sstevel@tonic-gate 		else
41700Sstevel@tonic-gate 			return (FALSE);
41710Sstevel@tonic-gate 	}
41720Sstevel@tonic-gate 	(void) kstat_close(kc);
41730Sstevel@tonic-gate 
41740Sstevel@tonic-gate 	return (FALSE);
41750Sstevel@tonic-gate }
41761676Sjpk 
41771676Sjpk static int
create_homedir(const char * src,const char * dst)41781676Sjpk create_homedir(const char *src, const char *dst) {
41791676Sjpk 
41801676Sjpk 	struct stat stbuf;
41811676Sjpk 	char *dst_username;
41821676Sjpk 	struct passwd *pwd, pwds;
41831676Sjpk 	char buf_pwd[NSS_BUFLEN_PASSWD];
41841676Sjpk 	int homedir_len;
41851676Sjpk 	int dst_dir_len;
41861676Sjpk 	int src_dir_len;
41871676Sjpk 
41881676Sjpk 	if (trace > 1)
41891676Sjpk 		trace_prt(1, "entered create_homedir\n");
41901676Sjpk 
41911676Sjpk 	if (stat(src, &stbuf) == 0) {
41921676Sjpk 		if (trace > 1)
41931676Sjpk 			trace_prt(1, "src exists\n");
41941676Sjpk 		return (1);
41951676Sjpk 	}
41961676Sjpk 
41971676Sjpk 	dst_username = strrchr(dst, '/');
41981676Sjpk 	if (dst_username) {
41991676Sjpk 		dst_username++; /* Skip over slash */
42001676Sjpk 		pwd = getpwnam_r(dst_username, &pwds, buf_pwd,
42011676Sjpk 		    sizeof (buf_pwd));
42021676Sjpk 		if (pwd == NULL) {
42031676Sjpk 			return (0);
42041676Sjpk 		}
42051676Sjpk 	} else {
42061676Sjpk 		return (0);
42071676Sjpk 	}
42081676Sjpk 
42091676Sjpk 	homedir_len = strlen(pwd->pw_dir);
42101676Sjpk 	dst_dir_len = strlen(dst) - homedir_len;
42111676Sjpk 	src_dir_len = strlen(src) - homedir_len;
42121676Sjpk 
42131676Sjpk 	/* Check that the paths are in the same zone */
42141676Sjpk 	if (src_dir_len < dst_dir_len ||
42151676Sjpk 	    (strncmp(dst, src, dst_dir_len) != 0)) {
42161676Sjpk 		if (trace > 1)
42171676Sjpk 			trace_prt(1, "	paths don't match\n");
42181676Sjpk 		return (0);
42191676Sjpk 	}
42201676Sjpk 	/* Check that mountpoint is an auto_home entry */
42211676Sjpk 	if (dst_dir_len < 0 ||
42221676Sjpk 	    (strcmp(pwd->pw_dir, dst + dst_dir_len) != 0)) {
42231676Sjpk 		return (0);
42241676Sjpk 	}
42251676Sjpk 
42261676Sjpk 	/* Check that source is an home directory entry */
42271676Sjpk 	if (src_dir_len < 0 ||
42281676Sjpk 	    (strcmp(pwd->pw_dir, src + src_dir_len) != 0)) {
42291676Sjpk 		if (trace > 1)
42301676Sjpk 			trace_prt(1, "	homedir (2) doesn't match %s\n",
42311676Sjpk 		src+src_dir_len);
42321676Sjpk 		return (0);
42331676Sjpk 	}
42341676Sjpk 
42351676Sjpk 	if (mkdir(src,
42361676Sjpk 	    S_IRUSR | S_IWUSR | S_IXUSR | S_IXGRP | S_IXOTH) == -1) {
42371676Sjpk 		if (trace > 1) {
42381676Sjpk 			trace_prt(1, "	Couldn't mkdir %s\n", src);
42391676Sjpk 		}
42401676Sjpk 		return (0);
42411676Sjpk 	}
42421676Sjpk 
42431676Sjpk 	if (chown(src, pwd->pw_uid, pwd->pw_gid) == -1) {
42441676Sjpk 		unlink(src);
42451676Sjpk 		return (0);
42461676Sjpk 	}
42471676Sjpk 
42481676Sjpk 	/* Created new home directory for the user */
42491676Sjpk 	return (1);
42501676Sjpk }
42512170Sevanl 
42522170Sevanl void
free_nfs_args(struct nfs_args * argp)42532170Sevanl free_nfs_args(struct nfs_args *argp)
42542170Sevanl {
42552170Sevanl 	struct nfs_args *oldp;
42562170Sevanl 	while (argp) {
42572170Sevanl 		if (argp->pathconf)
42582170Sevanl 			free(argp->pathconf);
42592170Sevanl 		if (argp->knconf)
42602170Sevanl 			free_knconf(argp->knconf);
42612170Sevanl 		if (argp->addr)
42622170Sevanl 			netbuf_free(argp->addr);
42632170Sevanl 		if (argp->syncaddr)
42642170Sevanl 			netbuf_free(argp->syncaddr);
42652170Sevanl 		if (argp->netname)
42662170Sevanl 			free(argp->netname);
42672170Sevanl 		if (argp->hostname)
42682170Sevanl 			free(argp->hostname);
42692170Sevanl 		if (argp->nfs_ext_u.nfs_extB.secdata)
42702170Sevanl 			nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
42712170Sevanl 		if (argp->fh)
42722170Sevanl 			free(argp->fh);
42732170Sevanl 		if (argp->nfs_ext_u.nfs_extA.secdata) {
42742170Sevanl 			sec_data_t	*sd;
42752170Sevanl 			sd = argp->nfs_ext_u.nfs_extA.secdata;
42762170Sevanl 			if (sd == NULL)
42772170Sevanl 				break;
42782170Sevanl 			switch (sd->rpcflavor) {
42792170Sevanl 			case AUTH_NONE:
42802170Sevanl 			case AUTH_UNIX:
42812170Sevanl 			case AUTH_LOOPBACK:
42822170Sevanl 				break;
42832170Sevanl 			case AUTH_DES:
42842170Sevanl 			{
42852170Sevanl 				dh_k4_clntdata_t	*dhk4;
42862170Sevanl 				dhk4 = (dh_k4_clntdata_t *)sd->data;
42872170Sevanl 				if (dhk4 == NULL)
42882170Sevanl 					break;
42892170Sevanl 				if (dhk4->syncaddr.buf)
42902170Sevanl 					free(dhk4->syncaddr.buf);
42912170Sevanl 				if (dhk4->knconf->knc_protofmly)
42922170Sevanl 					free(dhk4->knconf->knc_protofmly);
42932170Sevanl 				if (dhk4->knconf->knc_proto)
42942170Sevanl 					free(dhk4->knconf->knc_proto);
42952170Sevanl 				if (dhk4->knconf)
42962170Sevanl 					free(dhk4->knconf);
42972170Sevanl 				if (dhk4->netname)
42982170Sevanl 					free(dhk4->netname);
42992170Sevanl 				free(dhk4);
43002170Sevanl 				break;
43012170Sevanl 			}
43022170Sevanl 			case RPCSEC_GSS:
43032170Sevanl 			{
43042170Sevanl 				gss_clntdata_t	*gss;
43052170Sevanl 				gss = (gss_clntdata_t *)sd->data;
43062170Sevanl 				if (gss == NULL)
43072170Sevanl 					break;
43082170Sevanl 				if (gss->mechanism.elements)
43092170Sevanl 					free(gss->mechanism.elements);
43102170Sevanl 				free(gss);
43112170Sevanl 				break;
43122170Sevanl 			}
43132170Sevanl 			}
43142170Sevanl 		}
43152170Sevanl 		oldp = argp;
43162170Sevanl 		if (argp->nfs_args_ext == NFS_ARGS_EXTB)
43172170Sevanl 			argp = argp->nfs_ext_u.nfs_extB.next;
43182170Sevanl 		else
43192170Sevanl 			argp = NULL;
43202170Sevanl 		free(oldp);
43212170Sevanl 	}
43222170Sevanl }
432311291SRobert.Thurlow@Sun.COM 
432411291SRobert.Thurlow@Sun.COM void *
get_netconfig_info(enum type_of_stuff type_of_stuff,char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat,mfs_snego_t * mfssnego)432511291SRobert.Thurlow@Sun.COM get_netconfig_info(enum type_of_stuff type_of_stuff, char *hostname,
432611291SRobert.Thurlow@Sun.COM 	rpcprog_t prog, rpcvers_t vers, struct netconfig *nconf,
432711291SRobert.Thurlow@Sun.COM 	ushort_t port, struct t_info *tinfo, struct t_bind *tbind,
432811291SRobert.Thurlow@Sun.COM 	caddr_t *fhp, bool_t direct_to_server, char *fspath,
432911291SRobert.Thurlow@Sun.COM 	enum clnt_stat *cstat, mfs_snego_t *mfssnego)
433011291SRobert.Thurlow@Sun.COM {
433111291SRobert.Thurlow@Sun.COM 	struct netconfig *nb = NULL;
433211291SRobert.Thurlow@Sun.COM 	int ping_server = 0;
433311291SRobert.Thurlow@Sun.COM 
433411291SRobert.Thurlow@Sun.COM 
433511291SRobert.Thurlow@Sun.COM 	if (nconf == NULL)
433611291SRobert.Thurlow@Sun.COM 		return (NULL);
433711291SRobert.Thurlow@Sun.COM 
433811291SRobert.Thurlow@Sun.COM 	switch (type_of_stuff) {
433911291SRobert.Thurlow@Sun.COM 	case SERVER_FH:
434011291SRobert.Thurlow@Sun.COM 		nb = get_server_fh(hostname, prog, vers, mfssnego,
434111291SRobert.Thurlow@Sun.COM 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
434211291SRobert.Thurlow@Sun.COM 		    fspath, cstat);
434311291SRobert.Thurlow@Sun.COM 		break;
434411291SRobert.Thurlow@Sun.COM 	case SERVER_PING:
434511291SRobert.Thurlow@Sun.COM 		ping_server = 1;
434611291SRobert.Thurlow@Sun.COM 	case SERVER_ADDR:
434711291SRobert.Thurlow@Sun.COM 		nb = get_server_addrorping(hostname, prog, vers,
434811291SRobert.Thurlow@Sun.COM 		    nconf, port, tinfo, tbind, fhp, direct_to_server,
434911291SRobert.Thurlow@Sun.COM 		    fspath, cstat, ping_server);
435011291SRobert.Thurlow@Sun.COM 		break;
435111291SRobert.Thurlow@Sun.COM 	default:
435211291SRobert.Thurlow@Sun.COM 		assert(nb != NULL);
435311291SRobert.Thurlow@Sun.COM 	}
435411291SRobert.Thurlow@Sun.COM 	return (nb);
435511291SRobert.Thurlow@Sun.COM }
435611291SRobert.Thurlow@Sun.COM 
435711291SRobert.Thurlow@Sun.COM /*
435811291SRobert.Thurlow@Sun.COM  * Get the server address or can we ping it or not.
435911291SRobert.Thurlow@Sun.COM  * Check the portmap cache first for server address.
436011291SRobert.Thurlow@Sun.COM  * If no entries there, ping the server with a NULLPROC rpc.
436111291SRobert.Thurlow@Sun.COM  */
436211291SRobert.Thurlow@Sun.COM void *
get_server_addrorping(char * hostname,rpcprog_t prog,rpcvers_t vers,struct netconfig * nconf,ushort_t port,struct t_info * tinfo,struct t_bind * tbind,caddr_t * fhp,bool_t direct_to_server,char * fspath,enum clnt_stat * cstat,int ping_server)436311291SRobert.Thurlow@Sun.COM get_server_addrorping(char *hostname, rpcprog_t prog, rpcvers_t vers,
436411291SRobert.Thurlow@Sun.COM 	struct netconfig *nconf, ushort_t port, struct t_info *tinfo,
436511291SRobert.Thurlow@Sun.COM 	struct t_bind *tbind, caddr_t *fhp, bool_t direct_to_server,
436611291SRobert.Thurlow@Sun.COM 	char *fspath, enum clnt_stat *cstat, int ping_server)
436711291SRobert.Thurlow@Sun.COM {
436811291SRobert.Thurlow@Sun.COM 	struct timeval tv;
436911291SRobert.Thurlow@Sun.COM 	enum clnt_stat cs = RPC_TIMEDOUT;
437011291SRobert.Thurlow@Sun.COM 	struct netbuf *nb = NULL;
437111291SRobert.Thurlow@Sun.COM 	CLIENT *cl = NULL;
437211291SRobert.Thurlow@Sun.COM 	int fd = -1;
437311291SRobert.Thurlow@Sun.COM 
437411291SRobert.Thurlow@Sun.COM 	if (prog == NFS_PROGRAM && vers == NFS_V4)
437511291SRobert.Thurlow@Sun.COM 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
437611291SRobert.Thurlow@Sun.COM 			goto done;
437711291SRobert.Thurlow@Sun.COM 
437811291SRobert.Thurlow@Sun.COM 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0) {
437911291SRobert.Thurlow@Sun.COM 		goto done;
438011291SRobert.Thurlow@Sun.COM 	}
438111291SRobert.Thurlow@Sun.COM 
438211291SRobert.Thurlow@Sun.COM 	/* LINTED pointer alignment */
438311291SRobert.Thurlow@Sun.COM 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
438411291SRobert.Thurlow@Sun.COM 	    == NULL) {
438511291SRobert.Thurlow@Sun.COM 		goto done;
438611291SRobert.Thurlow@Sun.COM 	}
438711291SRobert.Thurlow@Sun.COM 
438811291SRobert.Thurlow@Sun.COM 	if (direct_to_server != TRUE) {
438911291SRobert.Thurlow@Sun.COM 		if (!ping_server) {
439011291SRobert.Thurlow@Sun.COM 			if (get_cached_srv_addr(hostname, prog, vers,
439111291SRobert.Thurlow@Sun.COM 			    nconf, &tbind->addr) == 0)
439211291SRobert.Thurlow@Sun.COM 				goto done;
439311291SRobert.Thurlow@Sun.COM 		} else {
439411291SRobert.Thurlow@Sun.COM 			if (port == 0)
439511291SRobert.Thurlow@Sun.COM 				goto done;
439611291SRobert.Thurlow@Sun.COM 		}
439711291SRobert.Thurlow@Sun.COM 	}
439811291SRobert.Thurlow@Sun.COM 	if (setup_nb_parms(nconf, tbind, tinfo, hostname,
439911291SRobert.Thurlow@Sun.COM 	    fd, direct_to_server, port, prog, vers, 0) < 0)
440011291SRobert.Thurlow@Sun.COM 		goto done;
440111291SRobert.Thurlow@Sun.COM 
440211291SRobert.Thurlow@Sun.COM 	if (port || (direct_to_server == TRUE)) {
440311291SRobert.Thurlow@Sun.COM 		tv.tv_sec = 10;
440411291SRobert.Thurlow@Sun.COM 		tv.tv_usec = 0;
440511291SRobert.Thurlow@Sun.COM 		cl = clnt_tli_create(fd, nconf, &tbind->addr,
440611291SRobert.Thurlow@Sun.COM 		    prog, vers, 0, 0);
440711291SRobert.Thurlow@Sun.COM 		if (cl == NULL)
440811291SRobert.Thurlow@Sun.COM 			goto done;
440911291SRobert.Thurlow@Sun.COM 
441011291SRobert.Thurlow@Sun.COM 		cs = clnt_call(cl, NULLPROC, xdr_void, 0,
441111291SRobert.Thurlow@Sun.COM 		    xdr_void, 0, tv);
441211291SRobert.Thurlow@Sun.COM 		if (cs != RPC_SUCCESS) {
441311291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "error is %d", cs);
441411291SRobert.Thurlow@Sun.COM 			goto done;
441511291SRobert.Thurlow@Sun.COM 		}
441611291SRobert.Thurlow@Sun.COM 	}
441711291SRobert.Thurlow@Sun.COM 	if (!ping_server) {
441811291SRobert.Thurlow@Sun.COM 		nb = (struct netbuf *)malloc(sizeof (struct netbuf));
441911291SRobert.Thurlow@Sun.COM 		if (nb == NULL) {
442011291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "no memory\n");
442111291SRobert.Thurlow@Sun.COM 			goto done;
442211291SRobert.Thurlow@Sun.COM 		}
442311291SRobert.Thurlow@Sun.COM 		nb->buf = (char *)malloc(tbind->addr.maxlen);
442411291SRobert.Thurlow@Sun.COM 		if (nb->buf == NULL) {
442511291SRobert.Thurlow@Sun.COM 			syslog(LOG_ERR, "no memory\n");
442611291SRobert.Thurlow@Sun.COM 			free(nb);
442711291SRobert.Thurlow@Sun.COM 			nb = NULL;
442811291SRobert.Thurlow@Sun.COM 			goto done;
442911291SRobert.Thurlow@Sun.COM 		}
443011291SRobert.Thurlow@Sun.COM 		(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
443111291SRobert.Thurlow@Sun.COM 		nb->len = tbind->addr.len;
443211291SRobert.Thurlow@Sun.COM 		nb->maxlen = tbind->addr.maxlen;
443311291SRobert.Thurlow@Sun.COM 		cs = RPC_SUCCESS;
443411291SRobert.Thurlow@Sun.COM 	}
443511291SRobert.Thurlow@Sun.COM done:
443611291SRobert.Thurlow@Sun.COM 	destroy_auth_client_handle(cl);
443711291SRobert.Thurlow@Sun.COM 	cleanup_tli_parms(tbind, fd);
443811291SRobert.Thurlow@Sun.COM 	*cstat = cs;
443911291SRobert.Thurlow@Sun.COM 	return (nb);
444011291SRobert.Thurlow@Sun.COM }
4441