xref: /onnv-gate/usr/src/cmd/fs.d/autofs/autod_nfs.c (revision 249:6233bc9dd306)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <stdlib.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <syslog.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <deflt.h>
360Sstevel@tonic-gate #include <kstat.h>
370Sstevel@tonic-gate #include <sys/param.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/time.h>
400Sstevel@tonic-gate #include <sys/stat.h>
410Sstevel@tonic-gate #include <sys/wait.h>
420Sstevel@tonic-gate #include <sys/socket.h>
430Sstevel@tonic-gate #include <netinet/in.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <sys/signal.h>
460Sstevel@tonic-gate #include <rpc/rpc.h>
470Sstevel@tonic-gate #include <rpc/pmap_clnt.h>
480Sstevel@tonic-gate #include <sys/mount.h>
490Sstevel@tonic-gate #include <sys/mntent.h>
500Sstevel@tonic-gate #include <sys/mnttab.h>
510Sstevel@tonic-gate #include <sys/fstyp.h>
520Sstevel@tonic-gate #include <sys/fsid.h>
530Sstevel@tonic-gate #include <arpa/inet.h>
540Sstevel@tonic-gate #include <netdb.h>
550Sstevel@tonic-gate #include <netconfig.h>
560Sstevel@tonic-gate #include <netdir.h>
570Sstevel@tonic-gate #include <errno.h>
580Sstevel@tonic-gate #define	NFSCLIENT
590Sstevel@tonic-gate #include <nfs/nfs.h>
600Sstevel@tonic-gate #include <nfs/mount.h>
610Sstevel@tonic-gate #include <rpcsvc/mount.h>
620Sstevel@tonic-gate #include <rpc/nettype.h>
630Sstevel@tonic-gate #include <locale.h>
640Sstevel@tonic-gate #include <setjmp.h>
650Sstevel@tonic-gate #include <sys/socket.h>
660Sstevel@tonic-gate #include <thread.h>
670Sstevel@tonic-gate #include <limits.h>
680Sstevel@tonic-gate #include <nss_dbdefs.h>			/* for NSS_BUFLEN_HOSTS */
690Sstevel@tonic-gate #include <nfs/nfs_sec.h>
700Sstevel@tonic-gate #include <sys/sockio.h>
710Sstevel@tonic-gate #include <net/if.h>
720Sstevel@tonic-gate #include <assert.h>
730Sstevel@tonic-gate #include <nfs/nfs_clnt.h>
740Sstevel@tonic-gate #include <rpcsvc/nfs4_prot.h>
750Sstevel@tonic-gate #define	NO_RDDIR_CACHE
760Sstevel@tonic-gate #include "automount.h"
770Sstevel@tonic-gate #include "replica.h"
780Sstevel@tonic-gate #include "nfs_subr.h"
790Sstevel@tonic-gate #include "webnfs.h"
800Sstevel@tonic-gate #include <sys/sockio.h>
810Sstevel@tonic-gate #include <net/if.h>
820Sstevel@tonic-gate #include <assert.h>
830Sstevel@tonic-gate #include <rpcsvc/daemon_utils.h>
840Sstevel@tonic-gate 
850Sstevel@tonic-gate extern char *nfs_get_qop_name();
860Sstevel@tonic-gate extern AUTH *nfs_create_ah();
870Sstevel@tonic-gate extern enum snego_stat nfs_sec_nego();
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #define	MAXHOSTS	512
900Sstevel@tonic-gate 
910Sstevel@tonic-gate /* number of transports to try */
920Sstevel@tonic-gate #define	MNT_PREF_LISTLEN	2
930Sstevel@tonic-gate #define	FIRST_TRY		1
940Sstevel@tonic-gate #define	SECOND_TRY		2
950Sstevel@tonic-gate 
960Sstevel@tonic-gate #define	MNTTYPE_CACHEFS "cachefs"
970Sstevel@tonic-gate 
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate  * host cache states
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate #define	NOHOST		0
1020Sstevel@tonic-gate #define	GOODHOST	1
1030Sstevel@tonic-gate #define	DEADHOST	2
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate #define	NFS_ARGS_EXTB_secdata(args, secdata) \
1060Sstevel@tonic-gate 	{ (args).nfs_args_ext = NFS_ARGS_EXTB, \
1070Sstevel@tonic-gate 	(args).nfs_ext_u.nfs_extB.secdata = secdata; }
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate struct cache_entry {
1100Sstevel@tonic-gate 	struct	cache_entry *cache_next;
1110Sstevel@tonic-gate 	char	*cache_host;
1120Sstevel@tonic-gate 	time_t	cache_time;
1130Sstevel@tonic-gate 	int	cache_state;
1140Sstevel@tonic-gate 	rpcvers_t cache_reqvers;
1150Sstevel@tonic-gate 	rpcvers_t cache_outvers;
1160Sstevel@tonic-gate 	char	*cache_proto;
1170Sstevel@tonic-gate };
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate struct mfs_snego_t {
1200Sstevel@tonic-gate 	int sec_opt;
1210Sstevel@tonic-gate 	bool_t snego_done;
1220Sstevel@tonic-gate 	char *nfs_flavor;
1230Sstevel@tonic-gate 	seconfig_t nfs_sec;
1240Sstevel@tonic-gate };
1250Sstevel@tonic-gate typedef struct mfs_snego_t mfs_snego_t;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate static struct cache_entry *cache_head = NULL;
1280Sstevel@tonic-gate rwlock_t cache_lock;	/* protect the cache chain */
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate static enum nfsstat nfsmount(struct mapfs *, char *, char *, int, int,
1310Sstevel@tonic-gate 	struct authunix_parms *);
1320Sstevel@tonic-gate static int is_nfs_port(char *);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate static void netbuf_free(struct netbuf *);
1350Sstevel@tonic-gate static struct knetconfig *get_knconf(struct netconfig *);
1360Sstevel@tonic-gate static void free_knconf(struct knetconfig *);
1370Sstevel@tonic-gate static int get_pathconf(CLIENT *, char *, char *, struct pathcnf **, int);
1380Sstevel@tonic-gate static struct mapfs *enum_servers(struct mapent *, char *);
1390Sstevel@tonic-gate static struct mapfs *get_mysubnet_servers(struct mapfs *);
1400Sstevel@tonic-gate static int subnet_test(int af, struct sioc_addrreq *);
1410Sstevel@tonic-gate static	struct	netbuf *get_addr(char *, rpcprog_t, rpcvers_t,
1420Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate static	struct	netbuf *get_pubfh(char *, rpcvers_t, mfs_snego_t *,
1450Sstevel@tonic-gate 	struct netconfig **, char *, ushort_t, struct t_info *, caddr_t *,
1460Sstevel@tonic-gate 	bool_t, char *);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate enum type_of_stuff {
1490Sstevel@tonic-gate 	SERVER_ADDR = 0,
1500Sstevel@tonic-gate 	SERVER_PING = 1,
1510Sstevel@tonic-gate 	SERVER_FH = 2
1520Sstevel@tonic-gate };
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate static	void *get_server_stuff(enum type_of_stuff, char *, rpcprog_t,
1550Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig **, char *, ushort_t,
1560Sstevel@tonic-gate 	struct t_info *, caddr_t *, bool_t, char *, enum clnt_stat *);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate static	void *get_the_stuff(enum type_of_stuff, char *, rpcprog_t,
1590Sstevel@tonic-gate 	rpcvers_t, mfs_snego_t *, struct netconfig *, ushort_t, struct t_info *,
1600Sstevel@tonic-gate 	caddr_t *, bool_t, char *, enum clnt_stat *);
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate struct mapfs *add_mfs(struct mapfs *, int, struct mapfs **, struct mapfs **);
1630Sstevel@tonic-gate void free_mfs(struct mapfs *);
1640Sstevel@tonic-gate static void dump_mfs(struct mapfs *, char *, int);
1650Sstevel@tonic-gate static char *dump_distance(struct mapfs *);
1660Sstevel@tonic-gate static void cache_free(struct cache_entry *);
1670Sstevel@tonic-gate static int cache_check(char *, rpcvers_t *, char *);
1680Sstevel@tonic-gate static void cache_enter(char *, rpcvers_t, rpcvers_t, char *, int);
1690Sstevel@tonic-gate static void destroy_auth_client_handle(CLIENT *cl);
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate #ifdef CACHE_DEBUG
1720Sstevel@tonic-gate static void trace_host_cache();
1730Sstevel@tonic-gate static void trace_portmap_cache();
1740Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate static int rpc_timeout = 20;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate #ifdef CACHE_DEBUG
1790Sstevel@tonic-gate /*
1800Sstevel@tonic-gate  * host cache counters. These variables do not need to be protected
1810Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the
1820Sstevel@tonic-gate  * goodhost/deadhost cache in the lazy hierarchical mounting scheme.
1830Sstevel@tonic-gate  */
1840Sstevel@tonic-gate static int host_cache_accesses = 0;
1850Sstevel@tonic-gate static int host_cache_lookups = 0;
1860Sstevel@tonic-gate static int deadhost_cache_hits = 0;
1870Sstevel@tonic-gate static int goodhost_cache_hits = 0;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /*
1900Sstevel@tonic-gate  * portmap cache counters. These variables do not need to be protected
1910Sstevel@tonic-gate  * by mutex's. They have been added to measure the utility of the portmap
1920Sstevel@tonic-gate  * cache in the lazy hierarchical mounting scheme.
1930Sstevel@tonic-gate  */
1940Sstevel@tonic-gate static int portmap_cache_accesses = 0;
1950Sstevel@tonic-gate static int portmap_cache_lookups = 0;
1960Sstevel@tonic-gate static int portmap_cache_hits = 0;
1970Sstevel@tonic-gate #endif /* CACHE_DEBUG */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate /*
2000Sstevel@tonic-gate  * There are the defaults (range) for the client when determining
2010Sstevel@tonic-gate  * which NFS version to use when probing the server (see above).
2020Sstevel@tonic-gate  * These will only be used when the vers mount option is not used and
2030Sstevel@tonic-gate  * these may be reset if /etc/default/nfs is configured to do so.
2040Sstevel@tonic-gate  */
2050Sstevel@tonic-gate static rpcvers_t vers_max_default = NFS_VERSMAX_DEFAULT;
2060Sstevel@tonic-gate static rpcvers_t vers_min_default = NFS_VERSMIN_DEFAULT;
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate /*
2090Sstevel@tonic-gate  * list of support services needed
2100Sstevel@tonic-gate  */
2110Sstevel@tonic-gate static char	*service_list[] = { STATD, LOCKD, NULL };
2120Sstevel@tonic-gate static char	*service_list_v4[] = { STATD, LOCKD, NFS4CBD, NFSMAPID, NULL };
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate static void read_default_nfs(void);
2150Sstevel@tonic-gate static int is_v4_mount(char *);
2160Sstevel@tonic-gate static void start_nfs4cbd(void);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate int
2190Sstevel@tonic-gate mount_nfs(me, mntpnt, prevhost, overlay, cred)
2200Sstevel@tonic-gate 	struct mapent *me;
2210Sstevel@tonic-gate 	char *mntpnt;
2220Sstevel@tonic-gate 	char *prevhost;
2230Sstevel@tonic-gate 	int overlay;
2240Sstevel@tonic-gate 	struct authunix_parms *cred;
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate 	struct mapfs *mfs, *mp;
2270Sstevel@tonic-gate 	int err = -1;
2280Sstevel@tonic-gate 	int cached;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	read_default_nfs();
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	mfs = enum_servers(me, prevhost);
2330Sstevel@tonic-gate 	if (mfs == NULL)
2340Sstevel@tonic-gate 		return (ENOENT);
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 	/*
2370Sstevel@tonic-gate 	 * Try loopback if we have something on localhost; if nothing
2380Sstevel@tonic-gate 	 * works, we will fall back to NFS
2390Sstevel@tonic-gate 	 */
2400Sstevel@tonic-gate 	if (is_nfs_port(me->map_mntopts)) {
2410Sstevel@tonic-gate 		for (mp = mfs; mp; mp = mp->mfs_next) {
2420Sstevel@tonic-gate 			if (self_check(mp->mfs_host)) {
2430Sstevel@tonic-gate 				err = loopbackmount(mp->mfs_dir,
2440Sstevel@tonic-gate 					mntpnt, me->map_mntopts, overlay);
2450Sstevel@tonic-gate 				if (err) {
2460Sstevel@tonic-gate 					mp->mfs_ignore = 1;
2470Sstevel@tonic-gate 				} else {
2480Sstevel@tonic-gate 					break;
2490Sstevel@tonic-gate 				}
2500Sstevel@tonic-gate 			}
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 	if (err) {
2540Sstevel@tonic-gate 		cached = strcmp(me->map_mounter, MNTTYPE_CACHEFS) == 0;
2550Sstevel@tonic-gate 		err = nfsmount(mfs, mntpnt, me->map_mntopts,
2560Sstevel@tonic-gate 				cached, overlay, cred);
2570Sstevel@tonic-gate 		if (err && trace > 1) {
2580Sstevel@tonic-gate 			trace_prt(1, "	Couldn't mount %s:%s, err=%d\n",
2590Sstevel@tonic-gate 				mfs->mfs_host, mfs->mfs_dir, err);
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	}
2620Sstevel@tonic-gate 	free_mfs(mfs);
2630Sstevel@tonic-gate 	return (err);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 
2670Sstevel@tonic-gate /*
2680Sstevel@tonic-gate  * Using the new ioctl SIOCTONLINK to determine if a host is on the same
2690Sstevel@tonic-gate  * subnet. Remove the old network, subnet check.
2700Sstevel@tonic-gate  */
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate static struct mapfs *
2730Sstevel@tonic-gate get_mysubnet_servers(struct mapfs *mfs_in)
2740Sstevel@tonic-gate {
2750Sstevel@tonic-gate 	int s;
2760Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	struct netconfig *nconf;
2790Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
2800Sstevel@tonic-gate 	struct nd_hostserv hs;
2810Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
2820Sstevel@tonic-gate 	struct netbuf *nb;
2830Sstevel@tonic-gate 	struct sioc_addrreq areq;
2840Sstevel@tonic-gate 	int res;
2850Sstevel@tonic-gate 	int af;
2860Sstevel@tonic-gate 	int i;
2870Sstevel@tonic-gate 	int sa_size;
2880Sstevel@tonic-gate 
2890Sstevel@tonic-gate 	hs.h_serv = "rpcbind";
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
2920Sstevel@tonic-gate 		nc = setnetconfig();
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 		while (nconf = getnetconfig(nc)) {
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 			/*
2970Sstevel@tonic-gate 			 * Care about INET family only. proto_done flag
2980Sstevel@tonic-gate 			 * indicates if we have already covered this
2990Sstevel@tonic-gate 			 * protocol family. If so skip it
3000Sstevel@tonic-gate 			 */
3010Sstevel@tonic-gate 			if (((strcmp(nconf->nc_protofmly, NC_INET6) == 0) ||
3020Sstevel@tonic-gate 				(strcmp(nconf->nc_protofmly, NC_INET) == 0)) &&
3030Sstevel@tonic-gate 				(nconf->nc_semantics == NC_TPI_CLTS)) {
3040Sstevel@tonic-gate 			} else
3050Sstevel@tonic-gate 				continue;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 			hs.h_host = mfs->mfs_host;
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 			if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK)
3100Sstevel@tonic-gate 				continue;
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 			/*
3130Sstevel@tonic-gate 			 * For each host address see if it's on our
3140Sstevel@tonic-gate 			 * local subnet.
3150Sstevel@tonic-gate 			 */
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET6) == 0)
3180Sstevel@tonic-gate 				af = AF_INET6;
3190Sstevel@tonic-gate 			else
3200Sstevel@tonic-gate 				af = AF_INET;
3210Sstevel@tonic-gate 			nb = retaddrs->n_addrs;
3220Sstevel@tonic-gate 			for (i = 0; i < retaddrs->n_cnt; i++, nb++) {
3230Sstevel@tonic-gate 				memset(&areq.sa_addr, 0, sizeof (areq.sa_addr));
3240Sstevel@tonic-gate 				memcpy(&areq.sa_addr, nb->buf, MIN(nb->len,
3250Sstevel@tonic-gate 					sizeof (areq.sa_addr)));
3260Sstevel@tonic-gate 				if (res = subnet_test(af, &areq)) {
3270Sstevel@tonic-gate 					p = add_mfs(mfs, DIST_MYNET,
3280Sstevel@tonic-gate 						&mfs_head, &mfs_tail);
3290Sstevel@tonic-gate 					if (!p) {
3300Sstevel@tonic-gate 						netdir_free(retaddrs,
3310Sstevel@tonic-gate 							ND_ADDRLIST);
3320Sstevel@tonic-gate 						endnetconfig(nc);
3330Sstevel@tonic-gate 						return (NULL);
3340Sstevel@tonic-gate 					}
3350Sstevel@tonic-gate 					break;
3360Sstevel@tonic-gate 				}
3370Sstevel@tonic-gate 			}  /* end of every host */
3380Sstevel@tonic-gate 			if (trace > 2) {
3390Sstevel@tonic-gate 				trace_prt(1, "get_mysubnet_servers: host=%s "
3400Sstevel@tonic-gate 					"netid=%s res=%s\n", mfs->mfs_host,
3410Sstevel@tonic-gate 					nconf->nc_netid, res == 1?"SUC":"FAIL");
3420Sstevel@tonic-gate 			}
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
3450Sstevel@tonic-gate 		} /* end of while */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		endnetconfig(nc);
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	} /* end of every map */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 	return (mfs_head);
3520Sstevel@tonic-gate 
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate int
3560Sstevel@tonic-gate subnet_test(int af, struct sioc_addrreq *areq)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	int s;
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if ((s = socket(af, SOCK_DGRAM, 0)) < 0) {
3610Sstevel@tonic-gate 		return (0);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	areq->sa_res = -1;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	if (ioctl(s, SIOCTONLINK, (caddr_t)areq) < 0) {
3670Sstevel@tonic-gate 		syslog(LOG_ERR, "subnet_test:SIOCTONLINK failed");
3680Sstevel@tonic-gate 		return (0);
3690Sstevel@tonic-gate 	}
3700Sstevel@tonic-gate 	close(s);
3710Sstevel@tonic-gate 	if (areq->sa_res == 1)
3720Sstevel@tonic-gate 		return (1);
3730Sstevel@tonic-gate 	else
3740Sstevel@tonic-gate 		return (0);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /*
3800Sstevel@tonic-gate  * ping a bunch of hosts at once and sort by who responds first
3810Sstevel@tonic-gate  */
3820Sstevel@tonic-gate static struct mapfs *
3830Sstevel@tonic-gate sort_servers(struct mapfs *mfs_in, int timeout)
3840Sstevel@tonic-gate {
3850Sstevel@tonic-gate 	struct mapfs *m1 = NULL;
3860Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	if (!mfs_in)
3890Sstevel@tonic-gate 		return (NULL);
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	clnt_stat = nfs_cast(mfs_in, &m1, timeout);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	if (!m1) {
3940Sstevel@tonic-gate 		char buff[2048] = {'\0'};
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 		for (m1 = mfs_in; m1; m1 = m1->mfs_next) {
3970Sstevel@tonic-gate 			(void) strcat(buff, m1->mfs_host);
3980Sstevel@tonic-gate 			if (m1->mfs_next)
3990Sstevel@tonic-gate 				(void) strcat(buff, ",");
4000Sstevel@tonic-gate 		}
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate 		syslog(LOG_ERR, "servers %s not responding: %s",
4030Sstevel@tonic-gate 			buff, clnt_sperrno(clnt_stat));
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate 
4060Sstevel@tonic-gate 	return (m1);
4070Sstevel@tonic-gate }
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /*
4100Sstevel@tonic-gate  * Add a mapfs entry to the list described by *mfs_head and *mfs_tail,
4110Sstevel@tonic-gate  * provided it is not marked "ignored" and isn't a dupe of ones we've
4120Sstevel@tonic-gate  * already seen.
4130Sstevel@tonic-gate  */
4140Sstevel@tonic-gate struct mapfs *
4150Sstevel@tonic-gate add_mfs(struct mapfs *mfs, int distance, struct mapfs **mfs_head,
4160Sstevel@tonic-gate 	struct mapfs **mfs_tail)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate 	struct mapfs *tmp, *new;
4190Sstevel@tonic-gate 	void bcopy();
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	for (tmp = *mfs_head; tmp; tmp = tmp->mfs_next)
4220Sstevel@tonic-gate 		if ((strcmp(tmp->mfs_host, mfs->mfs_host) == 0 &&
4230Sstevel@tonic-gate 		    strcmp(tmp->mfs_dir, mfs->mfs_dir) == 0) ||
4240Sstevel@tonic-gate 			mfs->mfs_ignore)
4250Sstevel@tonic-gate 			return (*mfs_head);
4260Sstevel@tonic-gate 	new = (struct mapfs *)malloc(sizeof (struct mapfs));
4270Sstevel@tonic-gate 	if (!new) {
4280Sstevel@tonic-gate 		syslog(LOG_ERR, "Memory allocation failed: %m");
4290Sstevel@tonic-gate 		return (NULL);
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 	bcopy(mfs, new, sizeof (struct mapfs));
4320Sstevel@tonic-gate 	new->mfs_next = NULL;
4330Sstevel@tonic-gate 	if (distance)
4340Sstevel@tonic-gate 		new->mfs_distance = distance;
4350Sstevel@tonic-gate 	if (!*mfs_head)
4360Sstevel@tonic-gate 		*mfs_tail = *mfs_head = new;
4370Sstevel@tonic-gate 	else {
4380Sstevel@tonic-gate 		(*mfs_tail)->mfs_next = new;
4390Sstevel@tonic-gate 		*mfs_tail = new;
4400Sstevel@tonic-gate 	}
4410Sstevel@tonic-gate 	return (*mfs_head);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate static void
4450Sstevel@tonic-gate dump_mfs(struct mapfs *mfs, char *message, int level)
4460Sstevel@tonic-gate {
4470Sstevel@tonic-gate 	struct mapfs *m1;
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	if (trace <= level)
4500Sstevel@tonic-gate 		return;
4510Sstevel@tonic-gate 
4520Sstevel@tonic-gate 	trace_prt(1, "%s", message);
4530Sstevel@tonic-gate 	if (!mfs) {
4540Sstevel@tonic-gate 		trace_prt(0, "mfs is null\n");
4550Sstevel@tonic-gate 		return;
4560Sstevel@tonic-gate 	}
4570Sstevel@tonic-gate 	for (m1 = mfs; m1; m1 = m1->mfs_next)
4580Sstevel@tonic-gate 		trace_prt(0, "%s[%s] ", m1->mfs_host, dump_distance(m1));
4590Sstevel@tonic-gate 	trace_prt(0, "\n");
4600Sstevel@tonic-gate }
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate static char *
4630Sstevel@tonic-gate dump_distance(struct mapfs *mfs)
4640Sstevel@tonic-gate {
4650Sstevel@tonic-gate 	switch (mfs->mfs_distance) {
4660Sstevel@tonic-gate 	case 0:			return ("zero");
4670Sstevel@tonic-gate 	case DIST_SELF:		return ("self");
4680Sstevel@tonic-gate 	case DIST_MYSUB:	return ("mysub");
4690Sstevel@tonic-gate 	case DIST_MYNET:	return ("mynet");
4700Sstevel@tonic-gate 	case DIST_OTHER:	return ("other");
4710Sstevel@tonic-gate 	default:		return ("other");
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate }
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate /*
4760Sstevel@tonic-gate  * Walk linked list "raw", building a new list consisting of members
4770Sstevel@tonic-gate  * NOT found in list "filter", returning the result.
4780Sstevel@tonic-gate  */
4790Sstevel@tonic-gate static struct mapfs *
4800Sstevel@tonic-gate filter_mfs(struct mapfs *raw, struct mapfs *filter)
4810Sstevel@tonic-gate {
4820Sstevel@tonic-gate 	struct mapfs *mfs, *p, *mfs_head = NULL, *mfs_tail = NULL;
4830Sstevel@tonic-gate 	int skip;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	if (!raw)
4860Sstevel@tonic-gate 		return (NULL);
4870Sstevel@tonic-gate 	for (mfs = raw; mfs; mfs = mfs->mfs_next) {
4880Sstevel@tonic-gate 		for (skip = 0, p = filter; p; p = p->mfs_next) {
4890Sstevel@tonic-gate 			if (strcmp(p->mfs_host, mfs->mfs_host) == 0 &&
4900Sstevel@tonic-gate 			    strcmp(p->mfs_dir, mfs->mfs_dir) == 0) {
4910Sstevel@tonic-gate 				skip = 1;
4920Sstevel@tonic-gate 				break;
4930Sstevel@tonic-gate 			}
4940Sstevel@tonic-gate 		}
4950Sstevel@tonic-gate 		if (skip)
4960Sstevel@tonic-gate 			continue;
4970Sstevel@tonic-gate 		p = add_mfs(mfs, 0, &mfs_head, &mfs_tail);
4980Sstevel@tonic-gate 		if (!p)
4990Sstevel@tonic-gate 			return (NULL);
5000Sstevel@tonic-gate 	}
5010Sstevel@tonic-gate 	return (mfs_head);
5020Sstevel@tonic-gate }
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate /*
5050Sstevel@tonic-gate  * Walk a linked list of mapfs structs, freeing each member.
5060Sstevel@tonic-gate  */
5070Sstevel@tonic-gate void
5080Sstevel@tonic-gate free_mfs(struct mapfs *mfs)
5090Sstevel@tonic-gate {
5100Sstevel@tonic-gate 	struct mapfs *tmp;
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	while (mfs) {
5130Sstevel@tonic-gate 		tmp = mfs->mfs_next;
5140Sstevel@tonic-gate 		free(mfs);
5150Sstevel@tonic-gate 		mfs = tmp;
5160Sstevel@tonic-gate 	}
5170Sstevel@tonic-gate }
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate /*
5200Sstevel@tonic-gate  * New code for NFS client failover: we need to carry and sort
5210Sstevel@tonic-gate  * lists of server possibilities rather than return a single
5220Sstevel@tonic-gate  * entry.  It preserves previous behaviour of sorting first by
5230Sstevel@tonic-gate  * locality (loopback-or-preferred/subnet/net/other) and then
5240Sstevel@tonic-gate  * by ping times.  We'll short-circuit this process when we
5250Sstevel@tonic-gate  * have ENOUGH or more entries.
5260Sstevel@tonic-gate  */
5270Sstevel@tonic-gate static struct mapfs *
5280Sstevel@tonic-gate enum_servers(struct mapent *me, char *preferred)
5290Sstevel@tonic-gate {
5300Sstevel@tonic-gate 	struct mapfs *p, *m1, *m2, *mfs_head = NULL, *mfs_tail = NULL;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	/*
5330Sstevel@tonic-gate 	 * Short-circuit for simple cases.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	if (!me->map_fs->mfs_next) {
5360Sstevel@tonic-gate 		p = add_mfs(me->map_fs, DIST_OTHER, &mfs_head, &mfs_tail);
5370Sstevel@tonic-gate 		if (!p)
5380Sstevel@tonic-gate 			return (NULL);
5390Sstevel@tonic-gate 		return (mfs_head);
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate 	dump_mfs(me->map_fs, "	enum_servers: mapent: ", 2);
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 	/*
5450Sstevel@tonic-gate 	 * get addresses & see if any are myself
5460Sstevel@tonic-gate 	 * or were mounted from previously in a
5470Sstevel@tonic-gate 	 * hierarchical mount.
5480Sstevel@tonic-gate 	 */
5490Sstevel@tonic-gate 	if (trace > 2)
5500Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: looking for pref/self\n");
5510Sstevel@tonic-gate 	for (m1 = me->map_fs; m1; m1 = m1->mfs_next) {
5520Sstevel@tonic-gate 		if (m1->mfs_ignore)
5530Sstevel@tonic-gate 			continue;
5540Sstevel@tonic-gate 		if (self_check(m1->mfs_host) ||
5550Sstevel@tonic-gate 		    strcmp(m1->mfs_host, preferred) == 0) {
5560Sstevel@tonic-gate 			p = add_mfs(m1, DIST_SELF, &mfs_head, &mfs_tail);
5570Sstevel@tonic-gate 			if (!p)
5580Sstevel@tonic-gate 				return (NULL);
5590Sstevel@tonic-gate 		}
5600Sstevel@tonic-gate 	}
5610Sstevel@tonic-gate 	if (trace > 2 && m1)
5620Sstevel@tonic-gate 		trace_prt(1, "	enum_servers: pref/self found, %s\n",
5630Sstevel@tonic-gate 			m1->mfs_host);
5640Sstevel@tonic-gate 
5650Sstevel@tonic-gate 	/*
5660Sstevel@tonic-gate 	 * look for entries on this subnet
5670Sstevel@tonic-gate 	 */
5680Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: input of get_mysubnet_servers: ", 2);
5690Sstevel@tonic-gate 	m1 = get_mysubnet_servers(me->map_fs);
5700Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: output of get_mysubnet_servers: ", 3);
5710Sstevel@tonic-gate 	if (m1 && m1->mfs_next) {
5720Sstevel@tonic-gate 		m2 = sort_servers(m1, rpc_timeout / 2);
5730Sstevel@tonic-gate 		dump_mfs(m2, "	enum_servers: output of sort_servers: ", 3);
5740Sstevel@tonic-gate 		free_mfs(m1);
5750Sstevel@tonic-gate 		m1 = m2;
5760Sstevel@tonic-gate 	}
5770Sstevel@tonic-gate 
5780Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
5790Sstevel@tonic-gate 		p = add_mfs(m2, 0, &mfs_head, &mfs_tail);
5800Sstevel@tonic-gate 		if (!p)
5810Sstevel@tonic-gate 			return (NULL);
5820Sstevel@tonic-gate 	}
5830Sstevel@tonic-gate 	if (m1)
5840Sstevel@tonic-gate 		free_mfs(m1);
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate 	/*
5870Sstevel@tonic-gate 	 * add the rest of the entries at the end
5880Sstevel@tonic-gate 	 */
5890Sstevel@tonic-gate 	m1 = filter_mfs(me->map_fs, mfs_head);
5900Sstevel@tonic-gate 	dump_mfs(m1, "	enum_servers: etc: output of filter_mfs: ", 3);
5910Sstevel@tonic-gate 	m2 = sort_servers(m1, rpc_timeout / 2);
5920Sstevel@tonic-gate 	dump_mfs(m2, "	enum_servers: etc: output of sort_servers: ", 3);
5930Sstevel@tonic-gate 	if (m1)
5940Sstevel@tonic-gate 		free_mfs(m1);
5950Sstevel@tonic-gate 	m1 = m2;
5960Sstevel@tonic-gate 	for (m2 = m1; m2; m2 = m2->mfs_next) {
5970Sstevel@tonic-gate 		p = add_mfs(m2, DIST_OTHER, &mfs_head, &mfs_tail);
5980Sstevel@tonic-gate 		if (!p)
5990Sstevel@tonic-gate 			return (NULL);
6000Sstevel@tonic-gate 	}
6010Sstevel@tonic-gate 	if (m1)
6020Sstevel@tonic-gate 		free_mfs(m1);
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate done:
6050Sstevel@tonic-gate 	dump_mfs(mfs_head, "  enum_servers: output: ", 1);
6060Sstevel@tonic-gate 	return (mfs_head);
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate 
6090Sstevel@tonic-gate static enum nfsstat
6100Sstevel@tonic-gate nfsmount(mfs_in, mntpnt, opts, cached, overlay, cred)
6110Sstevel@tonic-gate 	struct mapfs *mfs_in;
6120Sstevel@tonic-gate 	char *mntpnt, *opts;
6130Sstevel@tonic-gate 	int cached, overlay;
6140Sstevel@tonic-gate 	struct authunix_parms *cred;
6150Sstevel@tonic-gate {
6160Sstevel@tonic-gate 	CLIENT *cl;
6170Sstevel@tonic-gate 	char remname[MAXPATHLEN], *mnttabtext = NULL;
6180Sstevel@tonic-gate 	char mopts[MAX_MNTOPT_STR];
6190Sstevel@tonic-gate 	char netname[MAXNETNAMELEN+1];
6200Sstevel@tonic-gate 	int mnttabcnt = 0;
6210Sstevel@tonic-gate 	int loglevel;
6220Sstevel@tonic-gate 	struct mnttab m;
6230Sstevel@tonic-gate 	struct nfs_args *argp = NULL, *head = NULL, *tail = NULL,
6240Sstevel@tonic-gate 		*prevhead, *prevtail;
6250Sstevel@tonic-gate 	int flags;
6260Sstevel@tonic-gate 	struct fhstatus fhs;
6270Sstevel@tonic-gate 	struct timeval timeout;
6280Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
6290Sstevel@tonic-gate 	enum nfsstat status;
6300Sstevel@tonic-gate 	struct stat stbuf;
6310Sstevel@tonic-gate 	struct netconfig *nconf;
6320Sstevel@tonic-gate 	rpcvers_t vers, versmin; /* used to negotiate nfs version in pingnfs */
6330Sstevel@tonic-gate 				/* and mount version with mountd */
6340Sstevel@tonic-gate 	rpcvers_t outvers;	/* final version to be used during mount() */
6350Sstevel@tonic-gate 	rpcvers_t nfsvers;	/* version in map options, 0 if not there */
6360Sstevel@tonic-gate 	rpcvers_t mountversmax;	/* tracks the max mountvers during retries */
6370Sstevel@tonic-gate 
6380Sstevel@tonic-gate 	/* used to negotiate nfs version using webnfs */
6390Sstevel@tonic-gate 	rpcvers_t pubvers, pubversmin, pubversmax;
6400Sstevel@tonic-gate 	int posix;
6410Sstevel@tonic-gate 	struct nd_addrlist *retaddrs;
6420Sstevel@tonic-gate 	struct mountres3 res3;
6430Sstevel@tonic-gate 	nfs_fh3 fh3;
6440Sstevel@tonic-gate 	char *fstype;
6450Sstevel@tonic-gate 	int count, i;
6460Sstevel@tonic-gate 	char scerror_msg[MAXMSGLEN];
6470Sstevel@tonic-gate 	int *auths;
6480Sstevel@tonic-gate 	int delay;
6490Sstevel@tonic-gate 	int retries;
6500Sstevel@tonic-gate 	char *nfs_proto = NULL;
6510Sstevel@tonic-gate 	uint_t nfs_port = 0;
6520Sstevel@tonic-gate 	char *p, *host, *dir;
6530Sstevel@tonic-gate 	struct mapfs *mfs = NULL;
6540Sstevel@tonic-gate 	int error, last_error = 0;
6550Sstevel@tonic-gate 	int replicated;
6560Sstevel@tonic-gate 	int entries = 0;
6570Sstevel@tonic-gate 	int v2cnt = 0, v3cnt = 0, v4cnt = 0;
6580Sstevel@tonic-gate 	int v2near = 0, v3near = 0, v4near = 0;
6590Sstevel@tonic-gate 	int skipentry = 0;
6600Sstevel@tonic-gate 	char *nfs_flavor;
6610Sstevel@tonic-gate 	seconfig_t nfs_sec;
6620Sstevel@tonic-gate 	int sec_opt, scerror;
6630Sstevel@tonic-gate 	struct sec_data *secdata;
6640Sstevel@tonic-gate 	int secflags;
6650Sstevel@tonic-gate 	struct netbuf *syncaddr;
6660Sstevel@tonic-gate 	bool_t	use_pubfh;
6670Sstevel@tonic-gate 	ushort_t thisport;
6680Sstevel@tonic-gate 	int got_val;
6690Sstevel@tonic-gate 	mfs_snego_t mfssnego_init, mfssnego;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	dump_mfs(mfs_in, "  nfsmount: input: ", 2);
6720Sstevel@tonic-gate 	replicated = (mfs_in->mfs_next != NULL);
6730Sstevel@tonic-gate 	m.mnt_mntopts = opts;
6740Sstevel@tonic-gate 	if (replicated && hasmntopt(&m, MNTOPT_SOFT)) {
6750Sstevel@tonic-gate 		if (verbose)
6760Sstevel@tonic-gate 			syslog(LOG_WARNING,
6770Sstevel@tonic-gate 		    "mount on %s is soft and will not be replicated.", mntpnt);
6780Sstevel@tonic-gate 		replicated = 0;
6790Sstevel@tonic-gate 	}
6800Sstevel@tonic-gate 	if (replicated && !hasmntopt(&m, MNTOPT_RO)) {
6810Sstevel@tonic-gate 		if (verbose)
6820Sstevel@tonic-gate 			syslog(LOG_WARNING,
6830Sstevel@tonic-gate 		    "mount on %s is not read-only and will not be replicated.",
6840Sstevel@tonic-gate 			    mntpnt);
6850Sstevel@tonic-gate 		replicated = 0;
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	if (replicated && cached) {
6880Sstevel@tonic-gate 		if (verbose)
6890Sstevel@tonic-gate 			syslog(LOG_WARNING,
6900Sstevel@tonic-gate 		    "mount on %s is cached and will not be replicated.",
6910Sstevel@tonic-gate 			mntpnt);
6920Sstevel@tonic-gate 		replicated = 0;
6930Sstevel@tonic-gate 	}
6940Sstevel@tonic-gate 	if (replicated)
6950Sstevel@tonic-gate 		loglevel = LOG_WARNING;
6960Sstevel@tonic-gate 	else
6970Sstevel@tonic-gate 		loglevel = LOG_ERR;
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	if (trace > 1) {
7000Sstevel@tonic-gate 		if (replicated)
7010Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: replicated mount on %s %s:\n",
7020Sstevel@tonic-gate 				mntpnt, opts);
7030Sstevel@tonic-gate 		else
7040Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: standard mount on %s %s:\n",
7050Sstevel@tonic-gate 				mntpnt, opts);
7060Sstevel@tonic-gate 		for (mfs = mfs_in; mfs; mfs = mfs->mfs_next)
7070Sstevel@tonic-gate 			trace_prt(1, "	  %s:%s\n",
7080Sstevel@tonic-gate 				mfs->mfs_host, mfs->mfs_dir);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	/*
7120Sstevel@tonic-gate 	 * Make sure mountpoint is safe to mount on
7130Sstevel@tonic-gate 	 */
7140Sstevel@tonic-gate 	if (lstat(mntpnt, &stbuf) < 0) {
7150Sstevel@tonic-gate 		syslog(LOG_ERR, "Couldn't stat %s: %m", mntpnt);
7160Sstevel@tonic-gate 		return (NFSERR_NOENT);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	/*
7200Sstevel@tonic-gate 	 * Get protocol specified in options list, if any.
7210Sstevel@tonic-gate 	 */
7220Sstevel@tonic-gate 	if ((str_opt(&m, "proto", &nfs_proto)) == -1) {
7230Sstevel@tonic-gate 		return (NFSERR_NOENT);
7240Sstevel@tonic-gate 	}
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	/*
7270Sstevel@tonic-gate 	 * Get port specified in options list, if any.
7280Sstevel@tonic-gate 	 */
7290Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
7300Sstevel@tonic-gate 	if (!got_val)
7310Sstevel@tonic-gate 		nfs_port = 0;	/* "unspecified" */
7320Sstevel@tonic-gate 	if (nfs_port > USHRT_MAX) {
7330Sstevel@tonic-gate 		syslog(LOG_ERR, "%s: invalid port number %d", mntpnt, nfs_port);
7340Sstevel@tonic-gate 		return (NFSERR_NOENT);
7350Sstevel@tonic-gate 	}
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	/*
7380Sstevel@tonic-gate 	 * Set mount(2) flags here, outside of the loop.
7390Sstevel@tonic-gate 	 */
7400Sstevel@tonic-gate 	flags = MS_OPTIONSTR;
7410Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_RO) == NULL) ? 0 : MS_RDONLY;
7420Sstevel@tonic-gate 	flags |= (hasmntopt(&m, MNTOPT_NOSUID) == NULL) ? 0 : MS_NOSUID;
7430Sstevel@tonic-gate 	flags |= overlay ? MS_OVERLAY : 0;
7440Sstevel@tonic-gate 	if (mntpnt[strlen(mntpnt) - 1] != ' ')
7450Sstevel@tonic-gate 		/* direct mount point without offsets */
7460Sstevel@tonic-gate 		flags |= MS_OVERLAY;
7470Sstevel@tonic-gate 
7480Sstevel@tonic-gate 	use_pubfh = (hasmntopt(&m, MNTOPT_PUBLIC) == NULL) ? FALSE : TRUE;
7490Sstevel@tonic-gate 
7500Sstevel@tonic-gate 	(void) memset(&mfssnego_init, 0, sizeof (mfs_snego_t));
7510Sstevel@tonic-gate 	if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
7520Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7530Sstevel@tonic-gate 			syslog(loglevel,
7540Sstevel@tonic-gate 			    "conflicting security options");
7550Sstevel@tonic-gate 			return (NFSERR_IO);
7560Sstevel@tonic-gate 		}
7570Sstevel@tonic-gate 		if (nfs_getseconfig_byname("dh", &mfssnego_init.nfs_sec)) {
7580Sstevel@tonic-gate 			syslog(loglevel,
7590Sstevel@tonic-gate 			    "error getting dh information from %s",
7600Sstevel@tonic-gate 			    NFSSEC_CONF);
7610Sstevel@tonic-gate 			return (NFSERR_IO);
7620Sstevel@tonic-gate 		}
7630Sstevel@tonic-gate 	}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * Have to workaround the fact that hasmntopt() returns true
7670Sstevel@tonic-gate 	 * when comparing "secure" (in &m) with "sec".
7680Sstevel@tonic-gate 	 */
7690Sstevel@tonic-gate 	if (hasmntopt(&m, "sec=") != NULL) {
7700Sstevel@tonic-gate 		if ((str_opt(&m, MNTOPT_SEC,
7710Sstevel@tonic-gate 			&mfssnego_init.nfs_flavor)) == -1) {
7720Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
7730Sstevel@tonic-gate 			return (NFSERR_IO);
7740Sstevel@tonic-gate 		}
7750Sstevel@tonic-gate 	}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	if (mfssnego_init.nfs_flavor) {
7780Sstevel@tonic-gate 		if (++mfssnego_init.sec_opt > 1) {
7790Sstevel@tonic-gate 			syslog(loglevel,
7800Sstevel@tonic-gate 			    "conflicting security options");
7810Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
7820Sstevel@tonic-gate 			return (NFSERR_IO);
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 		if (nfs_getseconfig_byname(mfssnego_init.nfs_flavor,
7850Sstevel@tonic-gate 			&mfssnego_init.nfs_sec)) {
7860Sstevel@tonic-gate 			syslog(loglevel,
7870Sstevel@tonic-gate 			    "error getting %s information from %s",
7880Sstevel@tonic-gate 			    mfssnego_init.nfs_flavor, NFSSEC_CONF);
7890Sstevel@tonic-gate 			free(mfssnego_init.nfs_flavor);
7900Sstevel@tonic-gate 			return (NFSERR_IO);
7910Sstevel@tonic-gate 		}
7920Sstevel@tonic-gate 		free(mfssnego_init.nfs_flavor);
7930Sstevel@tonic-gate 	}
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate nextentry:
7960Sstevel@tonic-gate 	skipentry = 0;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	got_val = nopt(&m, MNTOPT_VERS, (int *)&nfsvers);
7990Sstevel@tonic-gate 	if (!got_val)
8000Sstevel@tonic-gate 		nfsvers = 0;	/* "unspecified" */
8010Sstevel@tonic-gate 	if (set_versrange(nfsvers, &vers, &versmin) != 0) {
8020Sstevel@tonic-gate 		syslog(LOG_ERR, "Incorrect NFS version specified for %s",
8030Sstevel@tonic-gate 			mntpnt);
8040Sstevel@tonic-gate 		last_error = NFSERR_NOENT;
8050Sstevel@tonic-gate 		goto ret;
8060Sstevel@tonic-gate 	}
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	if (nfsvers != 0) {
8090Sstevel@tonic-gate 		pubversmax = pubversmin = nfsvers;
8100Sstevel@tonic-gate 	} else {
8110Sstevel@tonic-gate 		pubversmax = vers;
8120Sstevel@tonic-gate 		pubversmin = versmin;
8130Sstevel@tonic-gate 	}
8140Sstevel@tonic-gate 
8150Sstevel@tonic-gate 	/*
8160Sstevel@tonic-gate 	 * Walk the whole list, pinging and collecting version
8170Sstevel@tonic-gate 	 * info so that we can make sure the mount will be
8180Sstevel@tonic-gate 	 * homogeneous with respect to version.
8190Sstevel@tonic-gate 	 *
8200Sstevel@tonic-gate 	 * If we have a version preference, this is easy; we'll
8210Sstevel@tonic-gate 	 * just reject anything that doesn't match.
8220Sstevel@tonic-gate 	 *
8230Sstevel@tonic-gate 	 * If not, we want to try to provide the best compromise
8240Sstevel@tonic-gate 	 * that considers proximity, preference for a higher version,
8250Sstevel@tonic-gate 	 * sorted order, and number of replicas.  We will count
8260Sstevel@tonic-gate 	 * the number of V2 and V3 replicas and also the number
8270Sstevel@tonic-gate 	 * which are "near", i.e. the localhost or on the same
8280Sstevel@tonic-gate 	 * subnet.
8290Sstevel@tonic-gate 	 */
8300Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate 		if (mfs->mfs_ignore)
8340Sstevel@tonic-gate 			continue;
8350Sstevel@tonic-gate 
8360Sstevel@tonic-gate 		host = mfs->mfs_host;
8370Sstevel@tonic-gate 		(void) memcpy(&mfssnego, &mfssnego_init, sizeof (mfs_snego_t));
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		if (use_pubfh == TRUE || mfs->mfs_flags & MFS_URL) {
8400Sstevel@tonic-gate 			char *path;
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 			if (nfs_port != 0 && mfs->mfs_port != 0 &&
8430Sstevel@tonic-gate 			    nfs_port != mfs->mfs_port) {
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: port (%u) in nfs URL"
8460Sstevel@tonic-gate 					" not the same as port (%d) in port "
8470Sstevel@tonic-gate 					"option\n", mfs->mfs_port, nfs_port);
8480Sstevel@tonic-gate 				last_error = NFSERR_IO;
8490Sstevel@tonic-gate 				goto out;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 			} else if (nfs_port != 0)
8520Sstevel@tonic-gate 				thisport = nfs_port;
8530Sstevel@tonic-gate 			else
8540Sstevel@tonic-gate 				thisport = mfs->mfs_port;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 			dir = mfs->mfs_dir;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_URL) == 0) {
8590Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
8600Sstevel@tonic-gate 				if (path == NULL) {
8610Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
8620Sstevel@tonic-gate 					last_error = NFSERR_IO;
8630Sstevel@tonic-gate 					goto out;
8640Sstevel@tonic-gate 				}
8650Sstevel@tonic-gate 				path[0] = (char)WNL_NATIVEPATH;
8660Sstevel@tonic-gate 				(void) strcpy(&path[1], dir);
8670Sstevel@tonic-gate 			} else {
8680Sstevel@tonic-gate 				path = dir;
8690Sstevel@tonic-gate 			}
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 			argp = (struct nfs_args *)
8720Sstevel@tonic-gate 				malloc(sizeof (struct nfs_args));
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 			if (!argp) {
8750Sstevel@tonic-gate 				if (path != dir)
8760Sstevel@tonic-gate 					free(path);
8770Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
8780Sstevel@tonic-gate 				last_error = NFSERR_IO;
8790Sstevel@tonic-gate 				goto out;
8800Sstevel@tonic-gate 			}
8810Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 			/*
8840Sstevel@tonic-gate 			 * RDMA support
8850Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
8860Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
8870Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
8880Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
8890Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
8900Sstevel@tonic-gate 			 */
8910Sstevel@tonic-gate 			if (nfs_proto) {
8920Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
8930Sstevel@tonic-gate 					free(nfs_proto);
8940Sstevel@tonic-gate 					nfs_proto = NULL;
8950Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
8960Sstevel@tonic-gate 				}
8970Sstevel@tonic-gate 			} else
8980Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 			for (pubvers = pubversmax; pubvers >= pubversmin;
9010Sstevel@tonic-gate 			    pubvers--) {
9020Sstevel@tonic-gate 
9030Sstevel@tonic-gate 				nconf = NULL;
9040Sstevel@tonic-gate 				argp->addr = get_pubfh(host, pubvers, &mfssnego,
9050Sstevel@tonic-gate 				    &nconf, nfs_proto, thisport, NULL,
9060Sstevel@tonic-gate 				    &argp->fh, TRUE, path);
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate 				if (argp->addr != NULL)
9090Sstevel@tonic-gate 					break;
9100Sstevel@tonic-gate 
9110Sstevel@tonic-gate 				if (nconf != NULL)
9120Sstevel@tonic-gate 					freenetconfigent(nconf);
9130Sstevel@tonic-gate 			}
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate 			if (path != dir)
9160Sstevel@tonic-gate 				free(path);
9170Sstevel@tonic-gate 
9180Sstevel@tonic-gate 			if (argp->addr != NULL) {
9190Sstevel@tonic-gate 
9200Sstevel@tonic-gate 				/*
9210Sstevel@tonic-gate 				 * The use of llock option for NFSv4
9220Sstevel@tonic-gate 				 * mounts is not required since file
9230Sstevel@tonic-gate 				 * locking is included within the protocol
9240Sstevel@tonic-gate 				 */
9250Sstevel@tonic-gate 				if (pubvers != NFS_V4)
9260Sstevel@tonic-gate 					argp->flags |= NFSMNT_LLOCK;
9270Sstevel@tonic-gate 
9280Sstevel@tonic-gate 				argp->flags |= NFSMNT_PUBLIC;
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 				mfs->mfs_args = argp;
9310Sstevel@tonic-gate 				mfs->mfs_version = pubvers;
9320Sstevel@tonic-gate 				mfs->mfs_nconf = nconf;
9330Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_FH_VIA_WEBNFS;
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate 			} else {
9360Sstevel@tonic-gate 				free(argp);
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 				/*
9390Sstevel@tonic-gate 				 * If -public was specified, give up
9400Sstevel@tonic-gate 				 * on this entry now.
9410Sstevel@tonic-gate 				 */
9420Sstevel@tonic-gate 				if (use_pubfh == TRUE) {
9430Sstevel@tonic-gate 					syslog(loglevel,
9440Sstevel@tonic-gate 					    "%s: no public file handle support",
9450Sstevel@tonic-gate 					    host);
9460Sstevel@tonic-gate 					last_error = NFSERR_NOENT;
9470Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
9480Sstevel@tonic-gate 					continue;
9490Sstevel@tonic-gate 				}
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 				/*
9520Sstevel@tonic-gate 				 * Back off to a conventional mount.
9530Sstevel@tonic-gate 				 *
9540Sstevel@tonic-gate 				 * URL's can contain escape characters. Get
9550Sstevel@tonic-gate 				 * rid of them.
9560Sstevel@tonic-gate 				 */
9570Sstevel@tonic-gate 				path = malloc(strlen(dir) + 2);
9580Sstevel@tonic-gate 
9590Sstevel@tonic-gate 				if (path == NULL) {
9600Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
9610Sstevel@tonic-gate 					last_error = NFSERR_IO;
9620Sstevel@tonic-gate 					goto out;
9630Sstevel@tonic-gate 				}
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate 				strcpy(path, dir);
9660Sstevel@tonic-gate 				URLparse(path);
9670Sstevel@tonic-gate 				mfs->mfs_dir = path;
9680Sstevel@tonic-gate 				mfs->mfs_flags |= MFS_ALLOC_DIR;
9690Sstevel@tonic-gate 				mfs->mfs_flags &= ~MFS_URL;
9700Sstevel@tonic-gate 			}
9710Sstevel@tonic-gate 		}
9720Sstevel@tonic-gate 
9730Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) ==  0) {
9740Sstevel@tonic-gate 			i = pingnfs(host, get_retry(opts) + 1, &vers, versmin,
9750Sstevel@tonic-gate 				0, FALSE, NULL, nfs_proto);
9760Sstevel@tonic-gate 			if (i != RPC_SUCCESS) {
9770Sstevel@tonic-gate 				if (i == RPC_PROGVERSMISMATCH) {
9780Sstevel@tonic-gate 					syslog(loglevel, "server %s: NFS "
9790Sstevel@tonic-gate 						"protocol version mismatch",
9800Sstevel@tonic-gate 						host);
9810Sstevel@tonic-gate 				} else {
9820Sstevel@tonic-gate 					syslog(loglevel, "server %s not "
9830Sstevel@tonic-gate 						"responding", host);
9840Sstevel@tonic-gate 				}
9850Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
9860Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
9870Sstevel@tonic-gate 				continue;
9880Sstevel@tonic-gate 			}
9890Sstevel@tonic-gate 			if (nfsvers != 0 && nfsvers != vers) {
9900Sstevel@tonic-gate 				if (nfs_proto == NULL)
9910Sstevel@tonic-gate 					syslog(loglevel,
9920Sstevel@tonic-gate 						"NFS version %d "
9930Sstevel@tonic-gate 						"not supported by %s",
9940Sstevel@tonic-gate 						nfsvers, host);
9950Sstevel@tonic-gate 				else
9960Sstevel@tonic-gate 					syslog(loglevel,
9970Sstevel@tonic-gate 						"NFS version %d "
9980Sstevel@tonic-gate 						"with proto %s "
9990Sstevel@tonic-gate 						"not supported by %s",
10000Sstevel@tonic-gate 						nfsvers, nfs_proto, host);
10010Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
10020Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
10030Sstevel@tonic-gate 				continue;
10040Sstevel@tonic-gate 			}
10050Sstevel@tonic-gate 		}
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 		switch (vers) {
10080Sstevel@tonic-gate 		case NFS_V4: v4cnt++; break;
10090Sstevel@tonic-gate 		case NFS_V3: v3cnt++; break;
10100Sstevel@tonic-gate 		case NFS_VERSION: v2cnt++; break;
10110Sstevel@tonic-gate 		default: break;
10120Sstevel@tonic-gate 		}
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 		/*
10150Sstevel@tonic-gate 		 * It's not clear how useful this stuff is if
10160Sstevel@tonic-gate 		 * we are using webnfs across the internet, but it
10170Sstevel@tonic-gate 		 * can't hurt.
10180Sstevel@tonic-gate 		 */
10190Sstevel@tonic-gate 		if (mfs->mfs_distance &&
10200Sstevel@tonic-gate 		    mfs->mfs_distance <= DIST_MYSUB) {
10210Sstevel@tonic-gate 			switch (vers) {
10220Sstevel@tonic-gate 			case NFS_V4: v4near++; break;
10230Sstevel@tonic-gate 			case NFS_V3: v3near++; break;
10240Sstevel@tonic-gate 			case NFS_VERSION: v2near++; break;
10250Sstevel@tonic-gate 			default: break;
10260Sstevel@tonic-gate 			}
10270Sstevel@tonic-gate 		}
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 		/*
10300Sstevel@tonic-gate 		 * If the mount is not replicated, we don't want to
10310Sstevel@tonic-gate 		 * ping every entry, so we'll stop here.  This means
10320Sstevel@tonic-gate 		 * that we may have to go back to "nextentry" above
10330Sstevel@tonic-gate 		 * to consider another entry if there we can't get
10340Sstevel@tonic-gate 		 * all the way to mount(2) with this one.
10350Sstevel@tonic-gate 		 */
10360Sstevel@tonic-gate 		if (!replicated)
10370Sstevel@tonic-gate 			break;
10380Sstevel@tonic-gate 	}
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if (nfsvers == 0) {
10410Sstevel@tonic-gate 		/*
10420Sstevel@tonic-gate 		 * Choose the NFS version.
10430Sstevel@tonic-gate 		 * We prefer higher versions, but will choose a one-
10440Sstevel@tonic-gate 		 * version downgrade in service if we can use a local
10450Sstevel@tonic-gate 		 * network interface and avoid a router.
10460Sstevel@tonic-gate 		 */
10470Sstevel@tonic-gate 		if (v4cnt && v4cnt >= v3cnt && (v4near || !v3near))
10480Sstevel@tonic-gate 			nfsvers = NFS_V4;
10490Sstevel@tonic-gate 		else if (v3cnt && v3cnt >= v2cnt && (v3near || !v2near))
10500Sstevel@tonic-gate 			nfsvers = NFS_V3;
10510Sstevel@tonic-gate 		else
10520Sstevel@tonic-gate 			nfsvers = NFS_VERSION;
10530Sstevel@tonic-gate 		if (trace > 2)
10540Sstevel@tonic-gate 			trace_prt(1,
10550Sstevel@tonic-gate 		    "  nfsmount: v4=%d[%d]v3=%d[%d],v2=%d[%d] => v%d.\n",
10560Sstevel@tonic-gate 			    v4cnt, v4near, v3cnt, v3near,
10570Sstevel@tonic-gate 			    v2cnt, v2near, nfsvers);
10580Sstevel@tonic-gate 	}
10590Sstevel@tonic-gate 
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * Since we don't support different NFS versions in replicated
10620Sstevel@tonic-gate 	 * mounts, set fstype now.
10630Sstevel@tonic-gate 	 * Also take the opportunity to set
10640Sstevel@tonic-gate 	 * the mount protocol version as appropriate.
10650Sstevel@tonic-gate 	 */
10660Sstevel@tonic-gate 	switch (nfsvers) {
10670Sstevel@tonic-gate 	case NFS_V4:
10680Sstevel@tonic-gate 		fstype = MNTTYPE_NFS4;
10690Sstevel@tonic-gate 		break;
10700Sstevel@tonic-gate 	case NFS_V3:
10710Sstevel@tonic-gate 		fstype = MNTTYPE_NFS3;
10720Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
10730Sstevel@tonic-gate 			mountversmax = MOUNTVERS3;
10740Sstevel@tonic-gate 			versmin = MOUNTVERS3;
10750Sstevel@tonic-gate 		}
10760Sstevel@tonic-gate 		break;
10770Sstevel@tonic-gate 	case NFS_VERSION:
10780Sstevel@tonic-gate 		fstype = MNTTYPE_NFS;
10790Sstevel@tonic-gate 		if (use_pubfh == FALSE) {
10800Sstevel@tonic-gate 			mountversmax = MOUNTVERS_POSIX;
10810Sstevel@tonic-gate 			versmin = MOUNTVERS;
10820Sstevel@tonic-gate 		}
10830Sstevel@tonic-gate 		break;
10840Sstevel@tonic-gate 	}
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 	/*
10870Sstevel@tonic-gate 	 * Our goal here is to evaluate each of several possible
10880Sstevel@tonic-gate 	 * replicas and try to come up with a list we can hand
10890Sstevel@tonic-gate 	 * to mount(2).  If we don't have a valid "head" at the
10900Sstevel@tonic-gate 	 * end of this process, it means we have rejected all
10910Sstevel@tonic-gate 	 * potential server:/path tuples.  We will fail quietly
10920Sstevel@tonic-gate 	 * in front of mount(2), and will have printed errors
10930Sstevel@tonic-gate 	 * where we found them.
10940Sstevel@tonic-gate 	 * XXX - do option work outside loop w careful design
10950Sstevel@tonic-gate 	 * XXX - use macro for error condition free handling
10960Sstevel@tonic-gate 	 */
10970Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 		/*
11000Sstevel@tonic-gate 		 * Initialize retry and delay values on a per-server basis.
11010Sstevel@tonic-gate 		 */
11020Sstevel@tonic-gate 		retries = get_retry(opts);
11030Sstevel@tonic-gate 		delay = INITDELAY;
11040Sstevel@tonic-gate retry:
11050Sstevel@tonic-gate 		if (mfs->mfs_ignore)
11060Sstevel@tonic-gate 			continue;
11070Sstevel@tonic-gate 
11080Sstevel@tonic-gate 		/*
11090Sstevel@tonic-gate 		 * If we don't have a fh yet, and if this is not a replicated
11100Sstevel@tonic-gate 		 * mount, we haven't done a pingnfs() on the next entry,
11110Sstevel@tonic-gate 		 * so we don't know if the next entry is up or if it
11120Sstevel@tonic-gate 		 * supports an NFS version we like.  So if we had a problem
11130Sstevel@tonic-gate 		 * with an entry, we need to go back and run through some new
11140Sstevel@tonic-gate 		 * code.
11150Sstevel@tonic-gate 		 */
11160Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
11170Sstevel@tonic-gate 		    !replicated && skipentry)
11180Sstevel@tonic-gate 			goto nextentry;
11190Sstevel@tonic-gate 
11200Sstevel@tonic-gate 		vers = mountversmax;
11210Sstevel@tonic-gate 		host = mfs->mfs_host;
11220Sstevel@tonic-gate 		dir = mfs->mfs_dir;
11230Sstevel@tonic-gate 		(void) sprintf(remname, "%s:%s", host, dir);
11240Sstevel@tonic-gate 		if (trace > 4 && replicated)
11250Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: examining %s\n", remname);
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate 		/*
11280Sstevel@tonic-gate 		 * If it's cached we need to get cachefs to mount it.
11290Sstevel@tonic-gate 		 */
11300Sstevel@tonic-gate 		if (cached) {
11310Sstevel@tonic-gate 			char *copts = opts;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 			/*
11340Sstevel@tonic-gate 			 * If we started with a URL we need to turn on
11350Sstevel@tonic-gate 			 * -o public if not on already
11360Sstevel@tonic-gate 			 */
11370Sstevel@tonic-gate 			if (use_pubfh == FALSE &&
11380Sstevel@tonic-gate 			    (mfs->mfs_flags & MFS_FH_VIA_WEBNFS)) {
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 				copts = malloc(strlen(opts) +
11410Sstevel@tonic-gate 					strlen(",public")+1);
11420Sstevel@tonic-gate 
11430Sstevel@tonic-gate 				if (copts == NULL) {
11440Sstevel@tonic-gate 					syslog(LOG_ERR, "nfsmount: no memory");
11450Sstevel@tonic-gate 					last_error = NFSERR_IO;
11460Sstevel@tonic-gate 					goto out;
11470Sstevel@tonic-gate 				}
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate 				strcpy(copts, opts);
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 				if (strlen(copts) != 0)
11520Sstevel@tonic-gate 					strcat(copts, ",");
11530Sstevel@tonic-gate 
11540Sstevel@tonic-gate 				strcat(copts, "public");
11550Sstevel@tonic-gate 			}
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 			last_error = mount_generic(remname, MNTTYPE_CACHEFS,
11580Sstevel@tonic-gate 				copts, mntpnt, overlay);
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 			if (copts != opts)
11610Sstevel@tonic-gate 				free(copts);
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 			if (last_error) {
11640Sstevel@tonic-gate 				skipentry = 1;
11650Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
11660Sstevel@tonic-gate 				continue;
11670Sstevel@tonic-gate 			}
11680Sstevel@tonic-gate 			goto out;
11690Sstevel@tonic-gate 		}
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 		if (mfs->mfs_args == NULL) {
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 			/*
11740Sstevel@tonic-gate 			 * Allocate nfs_args structure
11750Sstevel@tonic-gate 			 */
11760Sstevel@tonic-gate 			argp = (struct nfs_args *)
11770Sstevel@tonic-gate 				malloc(sizeof (struct nfs_args));
11780Sstevel@tonic-gate 
11790Sstevel@tonic-gate 			if (!argp) {
11800Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
11810Sstevel@tonic-gate 				last_error = NFSERR_IO;
11820Sstevel@tonic-gate 				goto out;
11830Sstevel@tonic-gate 			}
11840Sstevel@tonic-gate 
11850Sstevel@tonic-gate 			(void) memset(argp, 0, sizeof (*argp));
11860Sstevel@tonic-gate 
11870Sstevel@tonic-gate 			/*
11880Sstevel@tonic-gate 			 * RDMA support
11890Sstevel@tonic-gate 			 * By now Mount argument struct has been allocated,
11900Sstevel@tonic-gate 			 * either a pub_fh path will be taken or the regular
11910Sstevel@tonic-gate 			 * one. So here if a protocol was specified and it
11920Sstevel@tonic-gate 			 * was not rdma we let it be, else we set DO_RDMA.
11930Sstevel@tonic-gate 			 * If no proto was there we advise on trying RDMA.
11940Sstevel@tonic-gate 			 */
11950Sstevel@tonic-gate 			if (nfs_proto) {
11960Sstevel@tonic-gate 				if (strcmp(nfs_proto, "rdma") == 0) {
11970Sstevel@tonic-gate 					free(nfs_proto);
11980Sstevel@tonic-gate 					nfs_proto = NULL;
11990Sstevel@tonic-gate 					argp->flags |= NFSMNT_DORDMA;
12000Sstevel@tonic-gate 				}
12010Sstevel@tonic-gate 			} else
12020Sstevel@tonic-gate 				argp->flags |= NFSMNT_TRYRDMA;
12030Sstevel@tonic-gate 		} else {
12040Sstevel@tonic-gate 			argp = mfs->mfs_args;
12050Sstevel@tonic-gate 			mfs->mfs_args = NULL;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 			/*
12080Sstevel@tonic-gate 			 * Skip entry if we already have file handle but the
12090Sstevel@tonic-gate 			 * NFS version is wrong.
12100Sstevel@tonic-gate 			 */
12110Sstevel@tonic-gate 			if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) &&
12120Sstevel@tonic-gate 			    mfs->mfs_version != nfsvers) {
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 				free(argp);
12150Sstevel@tonic-gate 				skipentry = 1;
12160Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
12170Sstevel@tonic-gate 				continue;
12180Sstevel@tonic-gate 			}
12190Sstevel@tonic-gate 		}
12200Sstevel@tonic-gate 
12210Sstevel@tonic-gate 		prevhead = head;
12220Sstevel@tonic-gate 		prevtail = tail;
12230Sstevel@tonic-gate 		if (!head)
12240Sstevel@tonic-gate 			head = tail = argp;
12250Sstevel@tonic-gate 		else
12260Sstevel@tonic-gate 			tail = tail->nfs_ext_u.nfs_extB.next = argp;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 		/*
12290Sstevel@tonic-gate 		 * WebNFS and NFSv4 behave similarly in that they
12300Sstevel@tonic-gate 		 * don't use the mount protocol.  Therefore, avoid
12310Sstevel@tonic-gate 		 * mount protocol like things when version 4 is being
12320Sstevel@tonic-gate 		 * used.
12330Sstevel@tonic-gate 		 */
12340Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
12350Sstevel@tonic-gate 			nfsvers != NFS_V4) {
12360Sstevel@tonic-gate 		    timeout.tv_usec = 0;
12370Sstevel@tonic-gate 		    timeout.tv_sec = rpc_timeout;
12380Sstevel@tonic-gate 		    rpc_stat = RPC_TIMEDOUT;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 		    /* Create the client handle. */
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 		    if (trace > 1) {
12430Sstevel@tonic-gate 			trace_prt(1, "  nfsmount: Get mount version: request "
12440Sstevel@tonic-gate 			    "vers=%d min=%d\n", vers, versmin);
12450Sstevel@tonic-gate 		    }
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 		    while ((cl = clnt_create_vers(host, MOUNTPROG, &outvers,
12480Sstevel@tonic-gate 			versmin, vers, "udp")) == NULL) {
12490Sstevel@tonic-gate 			if (trace > 4) {
12500Sstevel@tonic-gate 			    trace_prt(1,
12510Sstevel@tonic-gate 			    "  nfsmount: Can't get mount version: rpcerr=%d\n",
12520Sstevel@tonic-gate 			    rpc_createerr.cf_stat);
12530Sstevel@tonic-gate 			}
12540Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
12550Sstevel@tonic-gate 			    rpc_createerr.cf_stat == RPC_TIMEDOUT)
12560Sstevel@tonic-gate 				break;
12570Sstevel@tonic-gate 
12580Sstevel@tonic-gate 			/*
12590Sstevel@tonic-gate 			 * backoff and return lower version to retry the ping.
12600Sstevel@tonic-gate 			 * XXX we should be more careful and handle
12610Sstevel@tonic-gate 			 * RPC_PROGVERSMISMATCH here, because that error
12620Sstevel@tonic-gate 			 * is handled in clnt_create_vers(). It's not done to
12630Sstevel@tonic-gate 			 * stay in sync with the nfs mount command.
12640Sstevel@tonic-gate 			 */
12650Sstevel@tonic-gate 			vers--;
12660Sstevel@tonic-gate 			if (vers < versmin)
12670Sstevel@tonic-gate 				break;
12680Sstevel@tonic-gate 			if (trace > 4) {
12690Sstevel@tonic-gate 			    trace_prt(1, "  nfsmount: Try version=%d\n", vers);
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 		    }
12720Sstevel@tonic-gate 
12730Sstevel@tonic-gate 		    if (cl == NULL) {
12740Sstevel@tonic-gate 			free(argp);
12750Sstevel@tonic-gate 			head = prevhead;
12760Sstevel@tonic-gate 			tail = prevtail;
12770Sstevel@tonic-gate 			if (tail)
12780Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
12790Sstevel@tonic-gate 			last_error = NFSERR_NOENT;
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 			if (rpc_createerr.cf_stat != RPC_UNKNOWNHOST &&
12820Sstevel@tonic-gate 			    rpc_createerr.cf_stat != RPC_PROGVERSMISMATCH &&
12830Sstevel@tonic-gate 			    retries-- > 0) {
12840Sstevel@tonic-gate 				DELAY(delay)
12850Sstevel@tonic-gate 				goto retry;
12860Sstevel@tonic-gate 			}
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 			syslog(loglevel, "%s %s", host,
12890Sstevel@tonic-gate 				clnt_spcreateerror("server not responding"));
12900Sstevel@tonic-gate 			skipentry = 1;
12910Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
12920Sstevel@tonic-gate 			continue;
12930Sstevel@tonic-gate 		    }
12940Sstevel@tonic-gate 		    if (trace > 1) {
12950Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: mount version=%d\n", outvers);
12960Sstevel@tonic-gate 		    }
12970Sstevel@tonic-gate #ifdef MALLOC_DEBUG
12980Sstevel@tonic-gate 		    add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
12990Sstevel@tonic-gate 		    add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13000Sstevel@tonic-gate 			__FILE__, __LINE__);
13010Sstevel@tonic-gate #endif
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 		    if (__clnt_bindresvport(cl) < 0) {
13040Sstevel@tonic-gate 			free(argp);
13050Sstevel@tonic-gate 			head = prevhead;
13060Sstevel@tonic-gate 			tail = prevtail;
13070Sstevel@tonic-gate 			if (tail)
13080Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
13090Sstevel@tonic-gate 			last_error = NFSERR_NOENT;
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 			if (retries-- > 0) {
13120Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13130Sstevel@tonic-gate 				DELAY(delay);
13140Sstevel@tonic-gate 				goto retry;
13150Sstevel@tonic-gate 			}
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 			syslog(loglevel, "mount %s: %s", host,
13180Sstevel@tonic-gate 				"Couldn't bind to reserved port");
13190Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
13200Sstevel@tonic-gate 			skipentry = 1;
13210Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
13220Sstevel@tonic-gate 			continue;
13230Sstevel@tonic-gate 		    }
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13260Sstevel@tonic-gate 		    drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
13270Sstevel@tonic-gate #endif
13280Sstevel@tonic-gate 		    AUTH_DESTROY(cl->cl_auth);
13290Sstevel@tonic-gate 		    if ((cl->cl_auth = authsys_create_default()) == NULL) {
13300Sstevel@tonic-gate 			free(argp);
13310Sstevel@tonic-gate 			head = prevhead;
13320Sstevel@tonic-gate 			tail = prevtail;
13330Sstevel@tonic-gate 			if (tail)
13340Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
13350Sstevel@tonic-gate 			last_error = NFSERR_NOENT;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 			if (retries-- > 0) {
13380Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13390Sstevel@tonic-gate 				DELAY(delay);
13400Sstevel@tonic-gate 				goto retry;
13410Sstevel@tonic-gate 			}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 			syslog(loglevel, "mount %s: %s", host,
13440Sstevel@tonic-gate 				"Failed creating default auth handle");
13450Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
13460Sstevel@tonic-gate 			skipentry = 1;
13470Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
13480Sstevel@tonic-gate 			continue;
13490Sstevel@tonic-gate 		    }
13500Sstevel@tonic-gate #ifdef MALLOC_DEBUG
13510Sstevel@tonic-gate 		    add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
13520Sstevel@tonic-gate 			__FILE__, __LINE__);
13530Sstevel@tonic-gate #endif
13540Sstevel@tonic-gate 		} else
13550Sstevel@tonic-gate 		    cl = NULL;
13560Sstevel@tonic-gate 
13570Sstevel@tonic-gate 		/*
13580Sstevel@tonic-gate 		 * set security options
13590Sstevel@tonic-gate 		 */
13600Sstevel@tonic-gate 		sec_opt = 0;
13610Sstevel@tonic-gate 		(void) memset(&nfs_sec, 0, sizeof (nfs_sec));
13620Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SECURE) != NULL) {
13630Sstevel@tonic-gate 			if (++sec_opt > 1) {
13640Sstevel@tonic-gate 				syslog(loglevel,
13650Sstevel@tonic-gate 				    "conflicting security options for %s",
13660Sstevel@tonic-gate 				    remname);
13670Sstevel@tonic-gate 				free(argp);
13680Sstevel@tonic-gate 				head = prevhead;
13690Sstevel@tonic-gate 				tail = prevtail;
13700Sstevel@tonic-gate 				if (tail)
13710Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13720Sstevel@tonic-gate 				last_error = NFSERR_IO;
13730Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13740Sstevel@tonic-gate 				skipentry = 1;
13750Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13760Sstevel@tonic-gate 				continue;
13770Sstevel@tonic-gate 			}
13780Sstevel@tonic-gate 			if (nfs_getseconfig_byname("dh", &nfs_sec)) {
13790Sstevel@tonic-gate 				syslog(loglevel,
13800Sstevel@tonic-gate 				    "error getting dh information from %s",
13810Sstevel@tonic-gate 				    NFSSEC_CONF);
13820Sstevel@tonic-gate 				free(argp);
13830Sstevel@tonic-gate 				head = prevhead;
13840Sstevel@tonic-gate 				tail = prevtail;
13850Sstevel@tonic-gate 				if (tail)
13860Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
13870Sstevel@tonic-gate 				last_error = NFSERR_IO;
13880Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
13890Sstevel@tonic-gate 				skipentry = 1;
13900Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
13910Sstevel@tonic-gate 				continue;
13920Sstevel@tonic-gate 			}
13930Sstevel@tonic-gate 		}
13940Sstevel@tonic-gate 
13950Sstevel@tonic-gate 		nfs_flavor = NULL;
13960Sstevel@tonic-gate 		/*
13970Sstevel@tonic-gate 		 * Have to workaround the fact that hasmntopt() returns true
13980Sstevel@tonic-gate 		 * when comparing "secure" (in &m) with "sec".
13990Sstevel@tonic-gate 		 */
14000Sstevel@tonic-gate 		if (hasmntopt(&m, "sec=") != NULL) {
14010Sstevel@tonic-gate 			if ((str_opt(&m, MNTOPT_SEC, &nfs_flavor)) == -1) {
14020Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
14030Sstevel@tonic-gate 				last_error = NFSERR_IO;
14040Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14050Sstevel@tonic-gate 				goto out;
14060Sstevel@tonic-gate 			}
14070Sstevel@tonic-gate 		}
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 		if (nfs_flavor) {
14100Sstevel@tonic-gate 			if (++sec_opt > 1) {
14110Sstevel@tonic-gate 				syslog(loglevel,
14120Sstevel@tonic-gate 				    "conflicting security options for %s",
14130Sstevel@tonic-gate 				    remname);
14140Sstevel@tonic-gate 				free(nfs_flavor);
14150Sstevel@tonic-gate 				free(argp);
14160Sstevel@tonic-gate 				head = prevhead;
14170Sstevel@tonic-gate 				tail = prevtail;
14180Sstevel@tonic-gate 				if (tail)
14190Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14200Sstevel@tonic-gate 				last_error = NFSERR_IO;
14210Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14220Sstevel@tonic-gate 				skipentry = 1;
14230Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14240Sstevel@tonic-gate 				continue;
14250Sstevel@tonic-gate 			}
14260Sstevel@tonic-gate 			if (nfs_getseconfig_byname(nfs_flavor, &nfs_sec)) {
14270Sstevel@tonic-gate 				syslog(loglevel,
14280Sstevel@tonic-gate 				    "error getting %s information from %s",
14290Sstevel@tonic-gate 				    nfs_flavor, NFSSEC_CONF);
14300Sstevel@tonic-gate 				free(nfs_flavor);
14310Sstevel@tonic-gate 				free(argp);
14320Sstevel@tonic-gate 				head = prevhead;
14330Sstevel@tonic-gate 				tail = prevtail;
14340Sstevel@tonic-gate 				if (tail)
14350Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14360Sstevel@tonic-gate 				last_error = NFSERR_IO;
14370Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14380Sstevel@tonic-gate 				skipentry = 1;
14390Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14400Sstevel@tonic-gate 				continue;
14410Sstevel@tonic-gate 			}
14420Sstevel@tonic-gate 			free(nfs_flavor);
14430Sstevel@tonic-gate 		}
14440Sstevel@tonic-gate 
14450Sstevel@tonic-gate 		posix = (nfsvers != NFS_V4 &&
14460Sstevel@tonic-gate 				hasmntopt(&m, MNTOPT_POSIX) != NULL) ? 1 : 0;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
14490Sstevel@tonic-gate 			nfsvers != NFS_V4) {
14500Sstevel@tonic-gate 		    bool_t give_up_on_mnt;
14510Sstevel@tonic-gate 		    bool_t got_mnt_error;
14520Sstevel@tonic-gate 		/*
14530Sstevel@tonic-gate 		 * If we started with a URL, if first byte of path is not "/",
14540Sstevel@tonic-gate 		 * then the mount will likely fail, so we should try again
14550Sstevel@tonic-gate 		 * with a prepended "/".
14560Sstevel@tonic-gate 		 */
14570Sstevel@tonic-gate 		    if (mfs->mfs_flags & MFS_ALLOC_DIR && *dir != '/')
14580Sstevel@tonic-gate 			give_up_on_mnt = FALSE;
14590Sstevel@tonic-gate 		    else
14600Sstevel@tonic-gate 			give_up_on_mnt = TRUE;
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 		    got_mnt_error = FALSE;
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate try_mnt_slash:
14650Sstevel@tonic-gate 		    if (got_mnt_error == TRUE) {
14660Sstevel@tonic-gate 			int i, l;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 			give_up_on_mnt = TRUE;
14690Sstevel@tonic-gate 			l = strlen(dir);
14700Sstevel@tonic-gate 
14710Sstevel@tonic-gate 			/*
14720Sstevel@tonic-gate 			 * Insert a "/" to front of mfs_dir.
14730Sstevel@tonic-gate 			 */
14740Sstevel@tonic-gate 			for (i = l; i > 0; i--)
14750Sstevel@tonic-gate 				dir[i] = dir[i-1];
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate 			dir[0] = '/';
14780Sstevel@tonic-gate 		    }
14790Sstevel@tonic-gate 
14800Sstevel@tonic-gate 		    /* Get fhandle of remote path from server's mountd */
14810Sstevel@tonic-gate 
14820Sstevel@tonic-gate 		    switch (outvers) {
14830Sstevel@tonic-gate 		    case MOUNTVERS:
14840Sstevel@tonic-gate 			if (posix) {
14850Sstevel@tonic-gate 				free(argp);
14860Sstevel@tonic-gate 				head = prevhead;
14870Sstevel@tonic-gate 				tail = prevtail;
14880Sstevel@tonic-gate 				if (tail)
14890Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
14900Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
14910Sstevel@tonic-gate 				syslog(loglevel, "can't get posix info for %s",
14920Sstevel@tonic-gate 					host);
14930Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
14940Sstevel@tonic-gate 				skipentry = 1;
14950Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
14960Sstevel@tonic-gate 				continue;
14970Sstevel@tonic-gate 			}
14980Sstevel@tonic-gate 		    /* FALLTHRU */
14990Sstevel@tonic-gate 		    case MOUNTVERS_POSIX:
15000Sstevel@tonic-gate 			if (nfsvers == NFS_V3) {
15010Sstevel@tonic-gate 				free(argp);
15020Sstevel@tonic-gate 				head = prevhead;
15030Sstevel@tonic-gate 				tail = prevtail;
15040Sstevel@tonic-gate 				if (tail)
15050Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
15060Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
15070Sstevel@tonic-gate 				syslog(loglevel,
15080Sstevel@tonic-gate 					"%s doesn't support NFS Version 3",
15090Sstevel@tonic-gate 					host);
15100Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
15110Sstevel@tonic-gate 				skipentry = 1;
15120Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
15130Sstevel@tonic-gate 				continue;
15140Sstevel@tonic-gate 			}
15150Sstevel@tonic-gate 			rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15160Sstevel@tonic-gate 				xdr_dirpath, (caddr_t)&dir,
15170Sstevel@tonic-gate 				xdr_fhstatus, (caddr_t)&fhs, timeout);
15180Sstevel@tonic-gate 			if (rpc_stat != RPC_SUCCESS) {
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 				if (give_up_on_mnt == FALSE) {
15210Sstevel@tonic-gate 					got_mnt_error = TRUE;
15220Sstevel@tonic-gate 					goto try_mnt_slash;
15230Sstevel@tonic-gate 				}
15240Sstevel@tonic-gate 
15250Sstevel@tonic-gate 				/*
15260Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
15270Sstevel@tonic-gate 				 * immediately following the "not responding"
15280Sstevel@tonic-gate 				 * is correct.
15290Sstevel@tonic-gate 				 */
15300Sstevel@tonic-gate 				free(argp);
15310Sstevel@tonic-gate 				head = prevhead;
15320Sstevel@tonic-gate 				tail = prevtail;
15330Sstevel@tonic-gate 				if (tail)
15340Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
15350Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
15360Sstevel@tonic-gate 
15370Sstevel@tonic-gate 				if (retries-- > 0) {
15380Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
15390Sstevel@tonic-gate 					DELAY(delay);
15400Sstevel@tonic-gate 					goto retry;
15410Sstevel@tonic-gate 				}
15420Sstevel@tonic-gate 
15430Sstevel@tonic-gate 				if (trace > 3) {
15440Sstevel@tonic-gate 				    trace_prt(1,
15450Sstevel@tonic-gate 					"  nfsmount: mount RPC failed for %s\n",
15460Sstevel@tonic-gate 					host);
15470Sstevel@tonic-gate 				}
15480Sstevel@tonic-gate 				syslog(loglevel, "%s server not responding%s",
15490Sstevel@tonic-gate 				    host, clnt_sperror(cl, ""));
15500Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
15510Sstevel@tonic-gate 				skipentry = 1;
15520Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
15530Sstevel@tonic-gate 				continue;
15540Sstevel@tonic-gate 			}
15550Sstevel@tonic-gate 			if ((errno = fhs.fhs_status) != MNT_OK)  {
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 				if (give_up_on_mnt == FALSE) {
15580Sstevel@tonic-gate 					got_mnt_error = TRUE;
15590Sstevel@tonic-gate 					goto try_mnt_slash;
15600Sstevel@tonic-gate 				}
15610Sstevel@tonic-gate 
15620Sstevel@tonic-gate 				free(argp);
15630Sstevel@tonic-gate 				head = prevhead;
15640Sstevel@tonic-gate 				tail = prevtail;
15650Sstevel@tonic-gate 				if (tail)
15660Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
15670Sstevel@tonic-gate 				if (errno == EACCES) {
15680Sstevel@tonic-gate 					status = NFSERR_ACCES;
15690Sstevel@tonic-gate 				} else {
15700Sstevel@tonic-gate 					syslog(loglevel, "%s: %m", host);
15710Sstevel@tonic-gate 					status = NFSERR_IO;
15720Sstevel@tonic-gate 				}
15730Sstevel@tonic-gate 				if (trace > 3) {
15740Sstevel@tonic-gate 				    trace_prt(1, "  nfsmount: mount RPC gave"
15750Sstevel@tonic-gate 					" %d for %s:%s\n",
15760Sstevel@tonic-gate 					errno, host, dir);
15770Sstevel@tonic-gate 				}
15780Sstevel@tonic-gate 				last_error = status;
15790Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
15800Sstevel@tonic-gate 				skipentry = 1;
15810Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
15820Sstevel@tonic-gate 				continue;
15830Sstevel@tonic-gate 			}
15840Sstevel@tonic-gate 			argp->fh = malloc((sizeof (fhandle)));
15850Sstevel@tonic-gate 			if (!argp->fh) {
15860Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
15870Sstevel@tonic-gate 				last_error = NFSERR_IO;
15880Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
15890Sstevel@tonic-gate 				goto out;
15900Sstevel@tonic-gate 			}
15910Sstevel@tonic-gate 			(void) memcpy(argp->fh, &fhs.fhstatus_u.fhs_fhandle,
15920Sstevel@tonic-gate 				sizeof (fhandle));
15930Sstevel@tonic-gate 			break;
15940Sstevel@tonic-gate 		    case MOUNTVERS3:
15950Sstevel@tonic-gate 			posix = 0;
15960Sstevel@tonic-gate 			(void) memset((char *)&res3, '\0', sizeof (res3));
15970Sstevel@tonic-gate 			rpc_stat = clnt_call(cl, MOUNTPROC_MNT,
15980Sstevel@tonic-gate 				xdr_dirpath, (caddr_t)&dir,
15990Sstevel@tonic-gate 				xdr_mountres3, (caddr_t)&res3, timeout);
16000Sstevel@tonic-gate 			if (rpc_stat != RPC_SUCCESS) {
16010Sstevel@tonic-gate 
16020Sstevel@tonic-gate 				if (give_up_on_mnt == FALSE) {
16030Sstevel@tonic-gate 					got_mnt_error = TRUE;
16040Sstevel@tonic-gate 					goto try_mnt_slash;
16050Sstevel@tonic-gate 				}
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 				/*
16080Sstevel@tonic-gate 				 * Given the way "clnt_sperror" works, the "%s"
16090Sstevel@tonic-gate 				 * immediately following the "not responding"
16100Sstevel@tonic-gate 				 * is correct.
16110Sstevel@tonic-gate 				 */
16120Sstevel@tonic-gate 				free(argp);
16130Sstevel@tonic-gate 				head = prevhead;
16140Sstevel@tonic-gate 				tail = prevtail;
16150Sstevel@tonic-gate 				if (tail)
16160Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
16170Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 				if (retries-- > 0) {
16200Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16210Sstevel@tonic-gate 					DELAY(delay);
16220Sstevel@tonic-gate 					goto retry;
16230Sstevel@tonic-gate 				}
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 				if (trace > 3) {
16260Sstevel@tonic-gate 				    trace_prt(1,
16270Sstevel@tonic-gate 					"  nfsmount: mount RPC failed for %s\n",
16280Sstevel@tonic-gate 					host);
16290Sstevel@tonic-gate 				}
16300Sstevel@tonic-gate 				syslog(loglevel, "%s server not responding%s",
16310Sstevel@tonic-gate 				    remname, clnt_sperror(cl, ""));
16320Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
16330Sstevel@tonic-gate 				skipentry = 1;
16340Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
16350Sstevel@tonic-gate 				continue;
16360Sstevel@tonic-gate 			}
16370Sstevel@tonic-gate 			if ((errno = res3.fhs_status) != MNT_OK)  {
16380Sstevel@tonic-gate 
16390Sstevel@tonic-gate 				if (give_up_on_mnt == FALSE) {
16400Sstevel@tonic-gate 					got_mnt_error = TRUE;
16410Sstevel@tonic-gate 					goto try_mnt_slash;
16420Sstevel@tonic-gate 				}
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 				free(argp);
16450Sstevel@tonic-gate 				head = prevhead;
16460Sstevel@tonic-gate 				tail = prevtail;
16470Sstevel@tonic-gate 				if (tail)
16480Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
16490Sstevel@tonic-gate 				if (errno == EACCES) {
16500Sstevel@tonic-gate 					status = NFSERR_ACCES;
16510Sstevel@tonic-gate 				} else {
16520Sstevel@tonic-gate 					syslog(loglevel, "%s: %m", remname);
16530Sstevel@tonic-gate 					status = NFSERR_IO;
16540Sstevel@tonic-gate 				}
16550Sstevel@tonic-gate 				if (trace > 3) {
16560Sstevel@tonic-gate 				    trace_prt(1, "  nfsmount: mount RPC gave"
16570Sstevel@tonic-gate 					" %d for %s:%s\n",
16580Sstevel@tonic-gate 					errno, host, dir);
16590Sstevel@tonic-gate 				}
16600Sstevel@tonic-gate 				last_error = status;
16610Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
16620Sstevel@tonic-gate 				skipentry = 1;
16630Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
16640Sstevel@tonic-gate 				continue;
16650Sstevel@tonic-gate 			}
16660Sstevel@tonic-gate 
16670Sstevel@tonic-gate 			/*
16680Sstevel@tonic-gate 			 *  Negotiate the security flavor for nfs_mount
16690Sstevel@tonic-gate 			 */
16700Sstevel@tonic-gate 			auths =
16710Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_val;
16720Sstevel@tonic-gate 			count =
16730Sstevel@tonic-gate 		    res3.mountres3_u.mountinfo.auth_flavors.auth_flavors_len;
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 			if (sec_opt) {
16760Sstevel@tonic-gate 				for (i = 0; i < count; i++)
16770Sstevel@tonic-gate 					if (auths[i] == nfs_sec.sc_nfsnum) {
16780Sstevel@tonic-gate 						break;
16790Sstevel@tonic-gate 					}
16800Sstevel@tonic-gate 				if (i >= count) {
16810Sstevel@tonic-gate 					syslog(LOG_ERR,
16820Sstevel@tonic-gate 				    "%s: does not support security \"%s\"\n",
16830Sstevel@tonic-gate 					    remname, nfs_sec.sc_name);
16840Sstevel@tonic-gate 					clnt_freeres(cl, xdr_mountres3,
16850Sstevel@tonic-gate 					    (caddr_t)&res3);
16860Sstevel@tonic-gate 					free(argp);
16870Sstevel@tonic-gate 					head = prevhead;
16880Sstevel@tonic-gate 					tail = prevtail;
16890Sstevel@tonic-gate 					if (tail)
16900Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
16910Sstevel@tonic-gate 					last_error = NFSERR_IO;
16920Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
16930Sstevel@tonic-gate 					skipentry = 1;
16940Sstevel@tonic-gate 					mfs->mfs_ignore = 1;
16950Sstevel@tonic-gate 					continue;
16960Sstevel@tonic-gate 				}
16970Sstevel@tonic-gate 			} else {
16980Sstevel@tonic-gate 				if (count > 0) {
16990Sstevel@tonic-gate 					for (i = 0; i < count; i++) {
17000Sstevel@tonic-gate 					    if (!(scerror =
17010Sstevel@tonic-gate 				nfs_getseconfig_bynumber(auths[i], &nfs_sec))) {
17020Sstevel@tonic-gate 						sec_opt++;
17030Sstevel@tonic-gate 						break;
17040Sstevel@tonic-gate 					    }
17050Sstevel@tonic-gate 					}
17060Sstevel@tonic-gate 					if (i >= count) {
17070Sstevel@tonic-gate 						if (nfs_syslog_scerr(scerror,
17080Sstevel@tonic-gate 								scerror_msg)
17090Sstevel@tonic-gate 							!= -1) {
17100Sstevel@tonic-gate 							syslog(LOG_ERR,
17110Sstevel@tonic-gate 			"%s cannot be mounted because it is shared with "
17120Sstevel@tonic-gate 			"security flavor %d which %s",
17130Sstevel@tonic-gate 							remname,
17140Sstevel@tonic-gate 							auths[i-1],
17150Sstevel@tonic-gate 							scerror_msg);
17160Sstevel@tonic-gate 						}
17170Sstevel@tonic-gate 						clnt_freeres(cl, xdr_mountres3,
17180Sstevel@tonic-gate 						    (caddr_t)&res3);
17190Sstevel@tonic-gate 						free(argp);
17200Sstevel@tonic-gate 						head = prevhead;
17210Sstevel@tonic-gate 						tail = prevtail;
17220Sstevel@tonic-gate 						if (tail)
17230Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
17240Sstevel@tonic-gate 						last_error = NFSERR_IO;
17250Sstevel@tonic-gate 						destroy_auth_client_handle(cl);
17260Sstevel@tonic-gate 						skipentry = 1;
17270Sstevel@tonic-gate 						mfs->mfs_ignore = 1;
17280Sstevel@tonic-gate 						continue;
17290Sstevel@tonic-gate 					}
17300Sstevel@tonic-gate 				}
17310Sstevel@tonic-gate 			}
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate 			fh3.fh3_length =
17340Sstevel@tonic-gate 			    res3.mountres3_u.mountinfo.fhandle.fhandle3_len;
17350Sstevel@tonic-gate 			(void) memcpy(fh3.fh3_u.data,
17360Sstevel@tonic-gate 			    res3.mountres3_u.mountinfo.fhandle.fhandle3_val,
17370Sstevel@tonic-gate 			    fh3.fh3_length);
17380Sstevel@tonic-gate 			clnt_freeres(cl, xdr_mountres3,
17390Sstevel@tonic-gate 			    (caddr_t)&res3);
17400Sstevel@tonic-gate 			argp->fh = malloc(sizeof (nfs_fh3));
17410Sstevel@tonic-gate 			if (!argp->fh) {
17420Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
17430Sstevel@tonic-gate 				last_error = NFSERR_IO;
17440Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
17450Sstevel@tonic-gate 				goto out;
17460Sstevel@tonic-gate 			}
17470Sstevel@tonic-gate 			(void) memcpy(argp->fh, &fh3, sizeof (nfs_fh3));
17480Sstevel@tonic-gate 			break;
17490Sstevel@tonic-gate 		    default:
17500Sstevel@tonic-gate 			free(argp);
17510Sstevel@tonic-gate 			head = prevhead;
17520Sstevel@tonic-gate 			tail = prevtail;
17530Sstevel@tonic-gate 			if (tail)
17540Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
17550Sstevel@tonic-gate 			last_error = NFSERR_NOENT;
17560Sstevel@tonic-gate 			syslog(loglevel, "unknown MOUNT version %ld on %s",
17570Sstevel@tonic-gate 			    vers, remname);
17580Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
17590Sstevel@tonic-gate 			skipentry = 1;
17600Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
17610Sstevel@tonic-gate 			continue;
17620Sstevel@tonic-gate 		    } /* switch */
17630Sstevel@tonic-gate 		}
17640Sstevel@tonic-gate 		if (nfsvers == NFS_V4) {
17650Sstevel@tonic-gate 			argp->fh = strdup(dir);
17660Sstevel@tonic-gate 			if (argp->fh == NULL) {
17670Sstevel@tonic-gate 				syslog(LOG_ERR, "nfsmount: no memory");
17680Sstevel@tonic-gate 				last_error = NFSERR_IO;
17690Sstevel@tonic-gate 				goto out;
17700Sstevel@tonic-gate 			}
17710Sstevel@tonic-gate 		}
17720Sstevel@tonic-gate 
17730Sstevel@tonic-gate 		if (trace > 4)
17740Sstevel@tonic-gate 			trace_prt(1, "	nfsmount: have %s filehandle for %s\n",
17750Sstevel@tonic-gate 			    fstype, remname);
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 		argp->flags |= NFSMNT_NEWARGS;
17780Sstevel@tonic-gate 		argp->flags |= NFSMNT_INT;	/* default is "intr" */
17790Sstevel@tonic-gate 		argp->hostname = host;
17800Sstevel@tonic-gate 		argp->flags |= NFSMNT_HOSTNAME;
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 		/*
17830Sstevel@tonic-gate 		 * In this case, we want NFSv4 to behave like
17840Sstevel@tonic-gate 		 * non-WebNFS so that we get the server address.
17850Sstevel@tonic-gate 		 */
17860Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0) {
17870Sstevel@tonic-gate 			nconf = NULL;
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate 			if (nfs_port != 0)
17900Sstevel@tonic-gate 				thisport = nfs_port;
17910Sstevel@tonic-gate 			else
17920Sstevel@tonic-gate 				thisport = mfs->mfs_port;
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate 			/*
17950Sstevel@tonic-gate 			 * For NFSv4, we want to avoid rpcbind, so call
17960Sstevel@tonic-gate 			 * get_server_stuff() directly to tell it that
17970Sstevel@tonic-gate 			 * we want to go "direct_to_server".  Otherwise,
17980Sstevel@tonic-gate 			 * do what has always been done.
17990Sstevel@tonic-gate 			 */
18000Sstevel@tonic-gate 			if (nfsvers == NFS_V4) {
18010Sstevel@tonic-gate 				enum clnt_stat cstat;
18020Sstevel@tonic-gate 				argp->addr = get_server_stuff(SERVER_ADDR,
18030Sstevel@tonic-gate 					host, NFS_PROGRAM, nfsvers, NULL,
18040Sstevel@tonic-gate 					&nconf, nfs_proto, thisport, NULL,
18050Sstevel@tonic-gate 					NULL, TRUE, NULL, &cstat);
18060Sstevel@tonic-gate 			} else {
18070Sstevel@tonic-gate 				argp->addr = get_addr(host, NFS_PROGRAM,
18080Sstevel@tonic-gate 					nfsvers, &nconf, nfs_proto,
18090Sstevel@tonic-gate 					thisport, NULL);
18100Sstevel@tonic-gate 			}
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 			if (argp->addr == NULL) {
18130Sstevel@tonic-gate 				free(argp->fh);
18140Sstevel@tonic-gate 				free(argp);
18150Sstevel@tonic-gate 				head = prevhead;
18160Sstevel@tonic-gate 				tail = prevtail;
18170Sstevel@tonic-gate 				if (tail)
18180Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
18190Sstevel@tonic-gate 				last_error = NFSERR_NOENT;
18200Sstevel@tonic-gate 
18210Sstevel@tonic-gate 				if (retries-- > 0) {
18220Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
18230Sstevel@tonic-gate 					DELAY(delay);
18240Sstevel@tonic-gate 					goto retry;
18250Sstevel@tonic-gate 				}
18260Sstevel@tonic-gate 
18270Sstevel@tonic-gate 				syslog(loglevel, "%s: no NFS service", host);
18280Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
18290Sstevel@tonic-gate 				skipentry = 1;
18300Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
18310Sstevel@tonic-gate 				continue;
18320Sstevel@tonic-gate 			}
18330Sstevel@tonic-gate 			if (trace > 4)
18340Sstevel@tonic-gate 				trace_prt(1,
18350Sstevel@tonic-gate 				    "\tnfsmount: have net address for %s\n",
18360Sstevel@tonic-gate 				    remname);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 		} else {
18390Sstevel@tonic-gate 			nconf = mfs->mfs_nconf;
18400Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
18410Sstevel@tonic-gate 		}
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 		argp->flags |= NFSMNT_KNCONF;
18440Sstevel@tonic-gate 		argp->knconf = get_knconf(nconf);
18450Sstevel@tonic-gate 		if (argp->knconf == NULL) {
18460Sstevel@tonic-gate 			netbuf_free(argp->addr);
18470Sstevel@tonic-gate 			freenetconfigent(nconf);
18480Sstevel@tonic-gate 			free(argp->fh);
18490Sstevel@tonic-gate 			free(argp);
18500Sstevel@tonic-gate 			head = prevhead;
18510Sstevel@tonic-gate 			tail = prevtail;
18520Sstevel@tonic-gate 			if (tail)
18530Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
18540Sstevel@tonic-gate 			last_error = NFSERR_NOSPC;
18550Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
18560Sstevel@tonic-gate 			skipentry = 1;
18570Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
18580Sstevel@tonic-gate 			continue;
18590Sstevel@tonic-gate 		}
18600Sstevel@tonic-gate 		if (trace > 4)
18610Sstevel@tonic-gate 			trace_prt(1,
18620Sstevel@tonic-gate 			    "\tnfsmount: have net config for %s\n",
18630Sstevel@tonic-gate 			    remname);
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_SOFT) != NULL) {
18660Sstevel@tonic-gate 			argp->flags |= NFSMNT_SOFT;
18670Sstevel@tonic-gate 		}
18680Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOINTR) != NULL) {
18690Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_INT);
18700Sstevel@tonic-gate 		}
18710Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOAC) != NULL) {
18720Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOAC;
18730Sstevel@tonic-gate 		}
18740Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOCTO) != NULL) {
18750Sstevel@tonic-gate 			argp->flags |= NFSMNT_NOCTO;
18760Sstevel@tonic-gate 		}
18770Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_FORCEDIRECTIO) != NULL) {
18780Sstevel@tonic-gate 			argp->flags |= NFSMNT_DIRECTIO;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_NOFORCEDIRECTIO) != NULL) {
18810Sstevel@tonic-gate 			argp->flags &= ~(NFSMNT_DIRECTIO);
18820Sstevel@tonic-gate 		}
18830Sstevel@tonic-gate 
18840Sstevel@tonic-gate 		/*
18850Sstevel@tonic-gate 		 * Set up security data for argp->nfs_ext_u.nfs_extB.secdata.
18860Sstevel@tonic-gate 		 */
18870Sstevel@tonic-gate 		if (mfssnego.snego_done) {
18880Sstevel@tonic-gate 			memcpy(&nfs_sec, &mfssnego.nfs_sec,
18890Sstevel@tonic-gate 				sizeof (seconfig_t));
18900Sstevel@tonic-gate 		} else if (!sec_opt) {
18910Sstevel@tonic-gate 			/*
18920Sstevel@tonic-gate 			 * Get default security mode.
18930Sstevel@tonic-gate 			 */
18940Sstevel@tonic-gate 			if (nfs_getseconfig_default(&nfs_sec)) {
18950Sstevel@tonic-gate 				syslog(loglevel,
18960Sstevel@tonic-gate 				    "error getting default security entry\n");
18970Sstevel@tonic-gate 				free_knconf(argp->knconf);
18980Sstevel@tonic-gate 				netbuf_free(argp->addr);
18990Sstevel@tonic-gate 				freenetconfigent(nconf);
19000Sstevel@tonic-gate 				free(argp->fh);
19010Sstevel@tonic-gate 				free(argp);
19020Sstevel@tonic-gate 				head = prevhead;
19030Sstevel@tonic-gate 				tail = prevtail;
19040Sstevel@tonic-gate 				if (tail)
19050Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19060Sstevel@tonic-gate 				last_error = NFSERR_NOSPC;
19070Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19080Sstevel@tonic-gate 				skipentry = 1;
19090Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19100Sstevel@tonic-gate 				continue;
19110Sstevel@tonic-gate 			}
19120Sstevel@tonic-gate 			argp->flags |= NFSMNT_SECDEFAULT;
19130Sstevel@tonic-gate 		}
19140Sstevel@tonic-gate 
19150Sstevel@tonic-gate 		/*
19160Sstevel@tonic-gate 		 * For AUTH_DH
19170Sstevel@tonic-gate 		 * get the network address for the time service on
19180Sstevel@tonic-gate 		 * the server.	If an RPC based time service is
19190Sstevel@tonic-gate 		 * not available then try the IP time service.
19200Sstevel@tonic-gate 		 *
19210Sstevel@tonic-gate 		 * Eventurally, we want to move this code to nfs_clnt_secdata()
19220Sstevel@tonic-gate 		 * when autod_nfs.c and mount.c can share the same
19230Sstevel@tonic-gate 		 * get_the_addr/get_the_stuff routine.
19240Sstevel@tonic-gate 		 */
19250Sstevel@tonic-gate 		secflags = 0;
19260Sstevel@tonic-gate 		syncaddr = NULL;
19270Sstevel@tonic-gate 		retaddrs = NULL;
19280Sstevel@tonic-gate 
19290Sstevel@tonic-gate 		if (nfs_sec.sc_rpcnum == AUTH_DH || nfsvers == NFS_V4) {
19300Sstevel@tonic-gate 		/*
19310Sstevel@tonic-gate 		 * If not using the public fh and not NFS_V4, we can try
19320Sstevel@tonic-gate 		 * talking RPCBIND. Otherwise, assume that firewalls
19330Sstevel@tonic-gate 		 * prevent us from doing that.
19340Sstevel@tonic-gate 		 */
19350Sstevel@tonic-gate 		    if ((mfs->mfs_flags & MFS_FH_VIA_WEBNFS) == 0 &&
19360Sstevel@tonic-gate 				nfsvers != NFS_V4) {
19370Sstevel@tonic-gate 			syncaddr = get_the_stuff(SERVER_ADDR, host, RPCBPROG,
19380Sstevel@tonic-gate 				RPCBVERS, NULL, nconf, 0, NULL, NULL, FALSE,
19390Sstevel@tonic-gate 				NULL, NULL);
19400Sstevel@tonic-gate 		    }
19410Sstevel@tonic-gate 
19420Sstevel@tonic-gate 		    if (syncaddr != NULL) {
19430Sstevel@tonic-gate 			/* for flags in sec_data */
19440Sstevel@tonic-gate 			secflags |= AUTH_F_RPCTIMESYNC;
19450Sstevel@tonic-gate 		    } else {
19460Sstevel@tonic-gate 			struct nd_hostserv hs;
19470Sstevel@tonic-gate 			int error;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 			hs.h_host = host;
19500Sstevel@tonic-gate 			hs.h_serv = "timserver";
19510Sstevel@tonic-gate 			error = netdir_getbyname(nconf, &hs, &retaddrs);
19520Sstevel@tonic-gate 
19530Sstevel@tonic-gate 			if (error != ND_OK && nfs_sec.sc_rpcnum == AUTH_DH) {
19540Sstevel@tonic-gate 				syslog(loglevel,
19550Sstevel@tonic-gate 				"%s: secure: no time service\n", host);
19560Sstevel@tonic-gate 				free_knconf(argp->knconf);
19570Sstevel@tonic-gate 				netbuf_free(argp->addr);
19580Sstevel@tonic-gate 				freenetconfigent(nconf);
19590Sstevel@tonic-gate 				free(argp->fh);
19600Sstevel@tonic-gate 				free(argp);
19610Sstevel@tonic-gate 				head = prevhead;
19620Sstevel@tonic-gate 				tail = prevtail;
19630Sstevel@tonic-gate 				if (tail)
19640Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
19650Sstevel@tonic-gate 				last_error = NFSERR_IO;
19660Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
19670Sstevel@tonic-gate 				skipentry = 1;
19680Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
19690Sstevel@tonic-gate 				continue;
19700Sstevel@tonic-gate 			}
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate 			if (error == ND_OK)
19730Sstevel@tonic-gate 			    syncaddr = retaddrs->n_addrs;
19740Sstevel@tonic-gate 
19750Sstevel@tonic-gate 			/*
19760Sstevel@tonic-gate 			 * For potential usage by NFS V4 when AUTH_DH
19770Sstevel@tonic-gate 			 * is negotiated via SECINFO in the kernel.
19780Sstevel@tonic-gate 			 */
19790Sstevel@tonic-gate 			if (nfsvers == NFS_V4 && syncaddr &&
19800Sstevel@tonic-gate 				    host2netname(netname, host, NULL)) {
19810Sstevel@tonic-gate 			    argp->syncaddr = malloc(sizeof (struct netbuf));
19820Sstevel@tonic-gate 			    argp->syncaddr->buf = malloc(syncaddr->len);
19830Sstevel@tonic-gate 			    (void) memcpy(argp->syncaddr->buf,
19840Sstevel@tonic-gate 					syncaddr->buf, syncaddr->len);
19850Sstevel@tonic-gate 			    argp->syncaddr->len = syncaddr->len;
19860Sstevel@tonic-gate 			    argp->syncaddr->maxlen = syncaddr->maxlen;
19870Sstevel@tonic-gate 			    argp->netname = strdup(netname);
19880Sstevel@tonic-gate 			    argp->flags |= NFSMNT_SECURE;
19890Sstevel@tonic-gate 			}
19900Sstevel@tonic-gate 		    } /* syncaddr */
19910Sstevel@tonic-gate 		} /* AUTH_DH */
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate 		nfs_sec.sc_uid = cred->aup_uid;
19940Sstevel@tonic-gate 		/*
19950Sstevel@tonic-gate 		 * If AUTH_DH is a chosen flavor now, its data will be stored
19960Sstevel@tonic-gate 		 * in the sec_data structure via nfs_clnt_secdata().
19970Sstevel@tonic-gate 		 */
19980Sstevel@tonic-gate 		if (!(secdata = nfs_clnt_secdata(&nfs_sec, host, argp->knconf,
19990Sstevel@tonic-gate 					syncaddr, secflags))) {
20000Sstevel@tonic-gate 			syslog(LOG_ERR,
20010Sstevel@tonic-gate 				"errors constructing security related data\n");
20020Sstevel@tonic-gate 			if (secflags & AUTH_F_RPCTIMESYNC)
20030Sstevel@tonic-gate 				netbuf_free(syncaddr);
20040Sstevel@tonic-gate 			else if (retaddrs)
20050Sstevel@tonic-gate 				netdir_free(retaddrs, ND_ADDRLIST);
20060Sstevel@tonic-gate 			if (argp->syncaddr)
20070Sstevel@tonic-gate 				netbuf_free(argp->syncaddr);
20080Sstevel@tonic-gate 			if (argp->netname)
20090Sstevel@tonic-gate 				free(argp->netname);
20100Sstevel@tonic-gate 			free_knconf(argp->knconf);
20110Sstevel@tonic-gate 			netbuf_free(argp->addr);
20120Sstevel@tonic-gate 			freenetconfigent(nconf);
20130Sstevel@tonic-gate 			free(argp->fh);
20140Sstevel@tonic-gate 			free(argp);
20150Sstevel@tonic-gate 			head = prevhead;
20160Sstevel@tonic-gate 			tail = prevtail;
20170Sstevel@tonic-gate 			if (tail)
20180Sstevel@tonic-gate 				tail->nfs_ext_u.nfs_extB.next = NULL;
20190Sstevel@tonic-gate 			last_error = NFSERR_IO;
20200Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
20210Sstevel@tonic-gate 			skipentry = 1;
20220Sstevel@tonic-gate 			mfs->mfs_ignore = 1;
20230Sstevel@tonic-gate 			continue;
20240Sstevel@tonic-gate 		}
20250Sstevel@tonic-gate 		NFS_ARGS_EXTB_secdata(*argp, secdata);
20260Sstevel@tonic-gate 		/* end of security stuff */
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 		if (trace > 4)
20290Sstevel@tonic-gate 			trace_prt(1,
20300Sstevel@tonic-gate 			    "  nfsmount: have secure info for %s\n", remname);
20310Sstevel@tonic-gate 
20320Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_GRPID) != NULL) {
20330Sstevel@tonic-gate 			argp->flags |= NFSMNT_GRPID;
20340Sstevel@tonic-gate 		}
20350Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RSIZE, &argp->rsize)) {
20360Sstevel@tonic-gate 			argp->flags |= NFSMNT_RSIZE;
20370Sstevel@tonic-gate 		}
20380Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_WSIZE, &argp->wsize)) {
20390Sstevel@tonic-gate 			argp->flags |= NFSMNT_WSIZE;
20400Sstevel@tonic-gate 		}
20410Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_TIMEO, &argp->timeo)) {
20420Sstevel@tonic-gate 			argp->flags |= NFSMNT_TIMEO;
20430Sstevel@tonic-gate 		}
20440Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_RETRANS, &argp->retrans)) {
20450Sstevel@tonic-gate 			argp->flags |= NFSMNT_RETRANS;
20460Sstevel@tonic-gate 		}
20470Sstevel@tonic-gate 		if (nopt(&m, MNTOPT_ACTIMEO, &argp->acregmax)) {
20480Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMAX;
20490Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMAX;
20500Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACDIRMIN;
20510Sstevel@tonic-gate 			argp->flags |= NFSMNT_ACREGMIN;
20520Sstevel@tonic-gate 			argp->acdirmin = argp->acregmin = argp->acdirmax
20530Sstevel@tonic-gate 				= argp->acregmax;
20540Sstevel@tonic-gate 		} else {
20550Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMIN, &argp->acregmin)) {
20560Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMIN;
20570Sstevel@tonic-gate 			}
20580Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACREGMAX, &argp->acregmax)) {
20590Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACREGMAX;
20600Sstevel@tonic-gate 			}
20610Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMIN, &argp->acdirmin)) {
20620Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMIN;
20630Sstevel@tonic-gate 			}
20640Sstevel@tonic-gate 			if (nopt(&m, MNTOPT_ACDIRMAX, &argp->acdirmax)) {
20650Sstevel@tonic-gate 				argp->flags |= NFSMNT_ACDIRMAX;
20660Sstevel@tonic-gate 			}
20670Sstevel@tonic-gate 		}
20680Sstevel@tonic-gate 
20690Sstevel@tonic-gate 		if (posix) {
20700Sstevel@tonic-gate 			argp->pathconf = NULL;
20710Sstevel@tonic-gate 			if (error = get_pathconf(cl, dir, remname,
20720Sstevel@tonic-gate 			    &argp->pathconf, retries)) {
20730Sstevel@tonic-gate 				if (secflags & AUTH_F_RPCTIMESYNC)
20740Sstevel@tonic-gate 					netbuf_free(syncaddr);
20750Sstevel@tonic-gate 				else if (retaddrs)
20760Sstevel@tonic-gate 					netdir_free(retaddrs, ND_ADDRLIST);
20770Sstevel@tonic-gate 				free_knconf(argp->knconf);
20780Sstevel@tonic-gate 				netbuf_free(argp->addr);
20790Sstevel@tonic-gate 				freenetconfigent(nconf);
20800Sstevel@tonic-gate 				nfs_free_secdata(
20810Sstevel@tonic-gate 					argp->nfs_ext_u.nfs_extB.secdata);
20820Sstevel@tonic-gate 				if (argp->syncaddr)
20830Sstevel@tonic-gate 					netbuf_free(argp->syncaddr);
20840Sstevel@tonic-gate 				if (argp->netname)
20850Sstevel@tonic-gate 					free(argp->netname);
20860Sstevel@tonic-gate 				free(argp->fh);
20870Sstevel@tonic-gate 				free(argp);
20880Sstevel@tonic-gate 				head = prevhead;
20890Sstevel@tonic-gate 				tail = prevtail;
20900Sstevel@tonic-gate 				if (tail)
20910Sstevel@tonic-gate 					tail->nfs_ext_u.nfs_extB.next = NULL;
20920Sstevel@tonic-gate 				last_error = NFSERR_IO;
20930Sstevel@tonic-gate 
20940Sstevel@tonic-gate 				if (error == RET_RETRY && retries-- > 0) {
20950Sstevel@tonic-gate 					destroy_auth_client_handle(cl);
20960Sstevel@tonic-gate 					DELAY(delay);
20970Sstevel@tonic-gate 					goto retry;
20980Sstevel@tonic-gate 				}
20990Sstevel@tonic-gate 
21000Sstevel@tonic-gate 				destroy_auth_client_handle(cl);
21010Sstevel@tonic-gate 				skipentry = 1;
21020Sstevel@tonic-gate 				mfs->mfs_ignore = 1;
21030Sstevel@tonic-gate 				continue;
21040Sstevel@tonic-gate 			}
21050Sstevel@tonic-gate 			argp->flags |= NFSMNT_POSIX;
21060Sstevel@tonic-gate 			if (trace > 4)
21070Sstevel@tonic-gate 				trace_prt(1,
21080Sstevel@tonic-gate 				    "  nfsmount: have pathconf for %s\n",
21090Sstevel@tonic-gate 				    remname);
21100Sstevel@tonic-gate 		}
21110Sstevel@tonic-gate 
21120Sstevel@tonic-gate 		/*
21130Sstevel@tonic-gate 		 * free loop-specific data structures
21140Sstevel@tonic-gate 		 */
21150Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
21160Sstevel@tonic-gate 		freenetconfigent(nconf);
21170Sstevel@tonic-gate 		if (secflags & AUTH_F_RPCTIMESYNC)
21180Sstevel@tonic-gate 			netbuf_free(syncaddr);
21190Sstevel@tonic-gate 		else if (retaddrs)
21200Sstevel@tonic-gate 			netdir_free(retaddrs, ND_ADDRLIST);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 		/*
21230Sstevel@tonic-gate 		 * Decide whether to use remote host's lockd or local locking.
21240Sstevel@tonic-gate 		 * If we are using the public fh, we've already turned
21250Sstevel@tonic-gate 		 * LLOCK on.
21260Sstevel@tonic-gate 		 */
21270Sstevel@tonic-gate 		if (hasmntopt(&m, MNTOPT_LLOCK))
21280Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
21290Sstevel@tonic-gate 		if (!(argp->flags & NFSMNT_LLOCK) && nfsvers == NFS_VERSION &&
21300Sstevel@tonic-gate 			remote_lock(host, argp->fh)) {
21310Sstevel@tonic-gate 			syslog(loglevel, "No network locking on %s : "
21320Sstevel@tonic-gate 			"contact admin to install server change", host);
21330Sstevel@tonic-gate 			argp->flags |= NFSMNT_LLOCK;
21340Sstevel@tonic-gate 		}
21350Sstevel@tonic-gate 
21360Sstevel@tonic-gate 		/*
21370Sstevel@tonic-gate 		 * Build a string for /etc/mnttab.
21380Sstevel@tonic-gate 		 * If possible, coalesce strings with same 'dir' info.
21390Sstevel@tonic-gate 		 */
21400Sstevel@tonic-gate 		if ((mfs->mfs_flags & MFS_URL) == 0) {
21410Sstevel@tonic-gate 			char *tmp;
21420Sstevel@tonic-gate 
21430Sstevel@tonic-gate 			if (mnttabcnt) {
21440Sstevel@tonic-gate 				p = strrchr(mnttabtext, (int)':');
21450Sstevel@tonic-gate 				if (!p || strcmp(p+1, dir) != 0) {
21460Sstevel@tonic-gate 					mnttabcnt += strlen(remname) + 2;
21470Sstevel@tonic-gate 				} else {
21480Sstevel@tonic-gate 					*p = '\0';
21490Sstevel@tonic-gate 					mnttabcnt += strlen(host) + 2;
21500Sstevel@tonic-gate 				}
21510Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
21520Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
21530Sstevel@tonic-gate 					mnttabtext = tmp;
21540Sstevel@tonic-gate 					strcat(mnttabtext, ",");
21550Sstevel@tonic-gate 				} else {
21560Sstevel@tonic-gate 					free(mnttabtext);
21570Sstevel@tonic-gate 					mnttabtext = NULL;
21580Sstevel@tonic-gate 				}
21590Sstevel@tonic-gate 			} else {
21600Sstevel@tonic-gate 				mnttabcnt = strlen(remname) + 1;
21610Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
21620Sstevel@tonic-gate 					mnttabtext[0] = '\0';
21630Sstevel@tonic-gate 			}
21640Sstevel@tonic-gate 
21650Sstevel@tonic-gate 			if (mnttabtext != NULL)
21660Sstevel@tonic-gate 				strcat(mnttabtext, remname);
21670Sstevel@tonic-gate 
21680Sstevel@tonic-gate 		} else {
21690Sstevel@tonic-gate 			char *tmp;
21700Sstevel@tonic-gate 			int more_cnt = 0;
21710Sstevel@tonic-gate 			char sport[16];
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 			more_cnt += strlen("nfs://");
21740Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_host);
21750Sstevel@tonic-gate 
21760Sstevel@tonic-gate 			if (mfs->mfs_port != 0) {
21770Sstevel@tonic-gate 				(void) sprintf(sport, ":%u", mfs->mfs_port);
21780Sstevel@tonic-gate 			} else
21790Sstevel@tonic-gate 				sport[0] = '\0';
21800Sstevel@tonic-gate 
21810Sstevel@tonic-gate 			more_cnt += strlen(sport);
21820Sstevel@tonic-gate 			more_cnt += 1; /* "/" */
21830Sstevel@tonic-gate 			more_cnt += strlen(mfs->mfs_dir);
21840Sstevel@tonic-gate 
21850Sstevel@tonic-gate 			if (mnttabcnt) {
21860Sstevel@tonic-gate 				more_cnt += 1; /* "," */
21870Sstevel@tonic-gate 				mnttabcnt += more_cnt;
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate 				if ((tmp = realloc(mnttabtext,
21900Sstevel@tonic-gate 				    mnttabcnt)) != NULL) {
21910Sstevel@tonic-gate 					mnttabtext = tmp;
21920Sstevel@tonic-gate 					strcat(mnttabtext, ",");
21930Sstevel@tonic-gate 				} else {
21940Sstevel@tonic-gate 					free(mnttabtext);
21950Sstevel@tonic-gate 					mnttabtext = NULL;
21960Sstevel@tonic-gate 				}
21970Sstevel@tonic-gate 			} else {
21980Sstevel@tonic-gate 				mnttabcnt = more_cnt + 1;
21990Sstevel@tonic-gate 				if ((mnttabtext = malloc(mnttabcnt)) != NULL)
22000Sstevel@tonic-gate 					mnttabtext[0] = '\0';
22010Sstevel@tonic-gate 			}
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate 			if (mnttabtext != NULL) {
22040Sstevel@tonic-gate 				strcat(mnttabtext, "nfs://");
22050Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_host);
22060Sstevel@tonic-gate 				strcat(mnttabtext, sport);
22070Sstevel@tonic-gate 				strcat(mnttabtext, "/");
22080Sstevel@tonic-gate 				strcat(mnttabtext, mfs->mfs_dir);
22090Sstevel@tonic-gate 			}
22100Sstevel@tonic-gate 		}
22110Sstevel@tonic-gate 
22120Sstevel@tonic-gate 		if (!mnttabtext) {
22130Sstevel@tonic-gate 			syslog(LOG_ERR, "nfsmount: no memory");
22140Sstevel@tonic-gate 			last_error = NFSERR_IO;
22150Sstevel@tonic-gate 			goto out;
22160Sstevel@tonic-gate 		}
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate 		/*
22190Sstevel@tonic-gate 		 * At least one entry, can call mount(2).
22200Sstevel@tonic-gate 		 */
22210Sstevel@tonic-gate 		entries++;
22220Sstevel@tonic-gate 
22230Sstevel@tonic-gate 		/*
22240Sstevel@tonic-gate 		 * If replication was defeated, don't do more work
22250Sstevel@tonic-gate 		 */
22260Sstevel@tonic-gate 		if (!replicated)
22270Sstevel@tonic-gate 			break;
22280Sstevel@tonic-gate 	}
22290Sstevel@tonic-gate 
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	/*
22320Sstevel@tonic-gate 	 * Did we get through all possibilities without success?
22330Sstevel@tonic-gate 	 */
22340Sstevel@tonic-gate 	if (!entries)
22350Sstevel@tonic-gate 		goto out;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/* Make "xattr" the default if "noxattr" is not specified. */
22380Sstevel@tonic-gate 	strcpy(mopts, opts);
22390Sstevel@tonic-gate 	if (!hasmntopt(&m, MNTOPT_NOXATTR) && !hasmntopt(&m, MNTOPT_XATTR)) {
22400Sstevel@tonic-gate 		if (strlen(mopts) > 0)
22410Sstevel@tonic-gate 			strcat(mopts, ",");
22420Sstevel@tonic-gate 		strcat(mopts, "xattr");
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/*
22460Sstevel@tonic-gate 	 * enable services as needed.
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	{
22490Sstevel@tonic-gate 		char **sl;
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		if (strcmp(fstype, MNTTYPE_NFS4) == 0)
22520Sstevel@tonic-gate 			sl = service_list_v4;
22530Sstevel@tonic-gate 		else
22540Sstevel@tonic-gate 			sl = service_list;
22550Sstevel@tonic-gate 
22560Sstevel@tonic-gate 		_check_services(sl);
22570Sstevel@tonic-gate 	}
22580Sstevel@tonic-gate 
22590Sstevel@tonic-gate 	/*
22600Sstevel@tonic-gate 	 * Whew; do the mount, at last.
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	if (trace > 1) {
22630Sstevel@tonic-gate 		trace_prt(1, "	mount %s %s (%s)\n", mnttabtext, mntpnt, mopts);
22640Sstevel@tonic-gate 	}
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 	if (mount(mnttabtext, mntpnt, flags | MS_DATA, fstype,
22670Sstevel@tonic-gate 			head, sizeof (*head), mopts, MAX_MNTOPT_STR) < 0) {
22680Sstevel@tonic-gate 		if (trace > 1)
22690Sstevel@tonic-gate 			trace_prt(1, "	Mount of %s on %s: %d\n",
22700Sstevel@tonic-gate 			    mnttabtext, mntpnt, errno);
22710Sstevel@tonic-gate 		if (errno != EBUSY || verbose)
22720Sstevel@tonic-gate 			syslog(LOG_ERR,
22730Sstevel@tonic-gate 				"Mount of %s on %s: %m", mnttabtext, mntpnt);
22740Sstevel@tonic-gate 		last_error = NFSERR_IO;
22750Sstevel@tonic-gate 		goto out;
22760Sstevel@tonic-gate 	}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	last_error = NFS_OK;
22790Sstevel@tonic-gate 	if (stat(mntpnt, &stbuf) == 0) {
22800Sstevel@tonic-gate 		if (trace > 1) {
22810Sstevel@tonic-gate 			trace_prt(1, "	mount %s dev=%x rdev=%x OK\n",
22820Sstevel@tonic-gate 				mnttabtext, stbuf.st_dev, stbuf.st_rdev);
22830Sstevel@tonic-gate 		}
22840Sstevel@tonic-gate 	} else {
22850Sstevel@tonic-gate 		if (trace > 1) {
22860Sstevel@tonic-gate 			trace_prt(1, "	mount %s OK\n", mnttabtext);
22870Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", mntpnt);
22880Sstevel@tonic-gate 		}
22890Sstevel@tonic-gate 	}
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate out:
22920Sstevel@tonic-gate 	argp = head;
22930Sstevel@tonic-gate 	while (argp) {
22940Sstevel@tonic-gate 		if (argp->pathconf)
22950Sstevel@tonic-gate 			free(argp->pathconf);
22960Sstevel@tonic-gate 		free_knconf(argp->knconf);
22970Sstevel@tonic-gate 		netbuf_free(argp->addr);
22980Sstevel@tonic-gate 		if (argp->syncaddr)
22990Sstevel@tonic-gate 			netbuf_free(argp->syncaddr);
23000Sstevel@tonic-gate 		if (argp->netname) {
23010Sstevel@tonic-gate 			free(argp->netname);
23020Sstevel@tonic-gate 		}
23030Sstevel@tonic-gate 		nfs_free_secdata(argp->nfs_ext_u.nfs_extB.secdata);
23040Sstevel@tonic-gate 		free(argp->fh);
23050Sstevel@tonic-gate 		head = argp;
23060Sstevel@tonic-gate 		argp = argp->nfs_ext_u.nfs_extB.next;
23070Sstevel@tonic-gate 		free(head);
23080Sstevel@tonic-gate 	}
23090Sstevel@tonic-gate ret:
23100Sstevel@tonic-gate 	if (nfs_proto)
23110Sstevel@tonic-gate 		free(nfs_proto);
23120Sstevel@tonic-gate 	if (mnttabtext)
23130Sstevel@tonic-gate 		free(mnttabtext);
23140Sstevel@tonic-gate 
23150Sstevel@tonic-gate 	for (mfs = mfs_in; mfs; mfs = mfs->mfs_next) {
23160Sstevel@tonic-gate 
23170Sstevel@tonic-gate 		if (mfs->mfs_flags & MFS_ALLOC_DIR) {
23180Sstevel@tonic-gate 			free(mfs->mfs_dir);
23190Sstevel@tonic-gate 			mfs->mfs_dir = NULL;
23200Sstevel@tonic-gate 			mfs->mfs_flags &= ~MFS_ALLOC_DIR;
23210Sstevel@tonic-gate 		}
23220Sstevel@tonic-gate 
23230Sstevel@tonic-gate 		if (mfs->mfs_args != NULL) {
23240Sstevel@tonic-gate 			free(mfs->mfs_args);
23250Sstevel@tonic-gate 			mfs->mfs_args = NULL;
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		if (mfs->mfs_nconf != NULL) {
23290Sstevel@tonic-gate 			freenetconfigent(mfs->mfs_nconf);
23300Sstevel@tonic-gate 			mfs->mfs_nconf = NULL;
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 	}
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	return (last_error);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate /*
23380Sstevel@tonic-gate  * get_pathconf(cl, path, fsname, pcnf, cretries)
23390Sstevel@tonic-gate  * ugliness that requires that ppathcnf and pathcnf stay consistent
23400Sstevel@tonic-gate  * cretries is a copy of retries used to determine when to syslog
23410Sstevel@tonic-gate  * on retry situations.
23420Sstevel@tonic-gate  */
23430Sstevel@tonic-gate static int
23440Sstevel@tonic-gate get_pathconf(CLIENT *cl, char *path, char *fsname, struct pathcnf **pcnf,
23450Sstevel@tonic-gate 	int cretries)
23460Sstevel@tonic-gate {
23470Sstevel@tonic-gate 	struct ppathcnf *p = NULL;
23480Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
23490Sstevel@tonic-gate 	struct timeval timeout;
23500Sstevel@tonic-gate 
23510Sstevel@tonic-gate 	p = (struct ppathcnf *)malloc(sizeof (struct ppathcnf));
23520Sstevel@tonic-gate 	if (p == NULL) {
23530Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: Out of memory");
23540Sstevel@tonic-gate 		return (RET_ERR);
23550Sstevel@tonic-gate 	}
23560Sstevel@tonic-gate 	memset((caddr_t)p, 0, sizeof (struct ppathcnf));
23570Sstevel@tonic-gate 
23580Sstevel@tonic-gate 	timeout.tv_sec = 10;
23590Sstevel@tonic-gate 	timeout.tv_usec = 0;
23600Sstevel@tonic-gate 	rpc_stat = clnt_call(cl, MOUNTPROC_PATHCONF,
23610Sstevel@tonic-gate 	    xdr_dirpath, (caddr_t)&path, xdr_ppathcnf, (caddr_t)p, timeout);
23620Sstevel@tonic-gate 	if (rpc_stat != RPC_SUCCESS) {
23630Sstevel@tonic-gate 		if (cretries-- <= 0) {
23640Sstevel@tonic-gate 			syslog(LOG_ERR,
23650Sstevel@tonic-gate 			    "get_pathconf: %s: server not responding: %s",
23660Sstevel@tonic-gate 			    fsname, clnt_sperror(cl, ""));
23670Sstevel@tonic-gate 		}
23680Sstevel@tonic-gate 		free(p);
23690Sstevel@tonic-gate 		return (RET_RETRY);
23700Sstevel@tonic-gate 	}
23710Sstevel@tonic-gate 	if (_PC_ISSET(_PC_ERROR, p->pc_mask)) {
23720Sstevel@tonic-gate 		syslog(LOG_ERR, "get_pathconf: no info for %s", fsname);
23730Sstevel@tonic-gate 		free(p);
23740Sstevel@tonic-gate 		return (RET_ERR);
23750Sstevel@tonic-gate 	}
23760Sstevel@tonic-gate 	*pcnf = (struct pathcnf *)p;
23770Sstevel@tonic-gate 	return (RET_OK);
23780Sstevel@tonic-gate }
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate static struct knetconfig *
23810Sstevel@tonic-gate get_knconf(nconf)
23820Sstevel@tonic-gate 	struct netconfig *nconf;
23830Sstevel@tonic-gate {
23840Sstevel@tonic-gate 	struct stat stbuf;
23850Sstevel@tonic-gate 	struct knetconfig *k;
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 	if (stat(nconf->nc_device, &stbuf) < 0) {
23880Sstevel@tonic-gate 		syslog(LOG_ERR, "get_knconf: stat %s: %m", nconf->nc_device);
23890Sstevel@tonic-gate 		return (NULL);
23900Sstevel@tonic-gate 	}
23910Sstevel@tonic-gate 	k = (struct knetconfig *)malloc(sizeof (*k));
23920Sstevel@tonic-gate 	if (k == NULL)
23930Sstevel@tonic-gate 		goto nomem;
23940Sstevel@tonic-gate 	k->knc_semantics = nconf->nc_semantics;
23950Sstevel@tonic-gate 	k->knc_protofmly = strdup(nconf->nc_protofmly);
23960Sstevel@tonic-gate 	if (k->knc_protofmly == NULL)
23970Sstevel@tonic-gate 		goto nomem;
23980Sstevel@tonic-gate 	k->knc_proto = strdup(nconf->nc_proto);
23990Sstevel@tonic-gate 	if (k->knc_proto == NULL)
24000Sstevel@tonic-gate 		goto nomem;
24010Sstevel@tonic-gate 	k->knc_rdev = stbuf.st_rdev;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 	return (k);
24040Sstevel@tonic-gate 
24050Sstevel@tonic-gate nomem:
24060Sstevel@tonic-gate 	syslog(LOG_ERR, "get_knconf: no memory");
24070Sstevel@tonic-gate 	free_knconf(k);
24080Sstevel@tonic-gate 	return (NULL);
24090Sstevel@tonic-gate }
24100Sstevel@tonic-gate 
24110Sstevel@tonic-gate static void
24120Sstevel@tonic-gate free_knconf(k)
24130Sstevel@tonic-gate 	struct knetconfig *k;
24140Sstevel@tonic-gate {
24150Sstevel@tonic-gate 	if (k == NULL)
24160Sstevel@tonic-gate 		return;
24170Sstevel@tonic-gate 	if (k->knc_protofmly)
24180Sstevel@tonic-gate 		free(k->knc_protofmly);
24190Sstevel@tonic-gate 	if (k->knc_proto)
24200Sstevel@tonic-gate 		free(k->knc_proto);
24210Sstevel@tonic-gate 	free(k);
24220Sstevel@tonic-gate }
24230Sstevel@tonic-gate 
24240Sstevel@tonic-gate static void
24250Sstevel@tonic-gate netbuf_free(nb)
24260Sstevel@tonic-gate 	struct netbuf *nb;
24270Sstevel@tonic-gate {
24280Sstevel@tonic-gate 	if (nb == NULL)
24290Sstevel@tonic-gate 		return;
24300Sstevel@tonic-gate 	if (nb->buf)
24310Sstevel@tonic-gate 		free(nb->buf);
24320Sstevel@tonic-gate 	free(nb);
24330Sstevel@tonic-gate }
24340Sstevel@tonic-gate 
24350Sstevel@tonic-gate #define	SMALL_HOSTNAME		20
24360Sstevel@tonic-gate #define	SMALL_PROTONAME		10
24370Sstevel@tonic-gate #define	SMALL_PROTOFMLYNAME		10
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate struct portmap_cache {
24400Sstevel@tonic-gate 	int cache_prog;
24410Sstevel@tonic-gate 	int cache_vers;
24420Sstevel@tonic-gate 	time_t cache_time;
24430Sstevel@tonic-gate 	char cache_small_hosts[SMALL_HOSTNAME + 1];
24440Sstevel@tonic-gate 	char *cache_hostname;
24450Sstevel@tonic-gate 	char *cache_proto;
24460Sstevel@tonic-gate 	char *cache_protofmly;
24470Sstevel@tonic-gate 	char cache_small_protofmly[SMALL_PROTOFMLYNAME + 1];
24480Sstevel@tonic-gate 	char cache_small_proto[SMALL_PROTONAME + 1];
24490Sstevel@tonic-gate 	struct netbuf cache_srv_addr;
24500Sstevel@tonic-gate 	struct portmap_cache *cache_prev, *cache_next;
24510Sstevel@tonic-gate };
24520Sstevel@tonic-gate 
24530Sstevel@tonic-gate rwlock_t portmap_cache_lock;
24540Sstevel@tonic-gate static int portmap_cache_valid_time = 30;
24550Sstevel@tonic-gate struct portmap_cache *portmap_cache_head, *portmap_cache_tail;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate #ifdef MALLOC_DEBUG
24580Sstevel@tonic-gate void
24590Sstevel@tonic-gate portmap_cache_flush()
24600Sstevel@tonic-gate {
24610Sstevel@tonic-gate 	struct  portmap_cache *next = NULL, *cp;
24620Sstevel@tonic-gate 
24630Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
24640Sstevel@tonic-gate 	for (cp = portmap_cache_head; cp; cp = cp->cache_next) {
24650Sstevel@tonic-gate 		if (cp->cache_hostname != NULL &&
24660Sstevel@tonic-gate 		    cp->cache_hostname !=
24670Sstevel@tonic-gate 		    cp->cache_small_hosts)
24680Sstevel@tonic-gate 			free(cp->cache_hostname);
24690Sstevel@tonic-gate 		if (cp->cache_proto != NULL &&
24700Sstevel@tonic-gate 		    cp->cache_proto !=
24710Sstevel@tonic-gate 		    cp->cache_small_proto)
24720Sstevel@tonic-gate 			free(cp->cache_proto);
24730Sstevel@tonic-gate 		if (cp->cache_srv_addr.buf != NULL)
24740Sstevel@tonic-gate 			free(cp->cache_srv_addr.buf);
24750Sstevel@tonic-gate 		next = cp->cache_next;
24760Sstevel@tonic-gate 		free(cp);
24770Sstevel@tonic-gate 	}
24780Sstevel@tonic-gate 	portmap_cache_head = NULL;
24790Sstevel@tonic-gate 	portmap_cache_tail = NULL;
24800Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
24810Sstevel@tonic-gate }
24820Sstevel@tonic-gate #endif
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate /*
24850Sstevel@tonic-gate  * Returns 1 if the entry is found in the cache, 0 otherwise.
24860Sstevel@tonic-gate  */
24870Sstevel@tonic-gate static int
24880Sstevel@tonic-gate portmap_cache_lookup(hostname, prog, vers, nconf, addrp)
24890Sstevel@tonic-gate 	char *hostname;
24900Sstevel@tonic-gate 	rpcprog_t prog;
24910Sstevel@tonic-gate 	rpcvers_t vers;
24920Sstevel@tonic-gate 	struct netconfig *nconf;
24930Sstevel@tonic-gate 	struct netbuf *addrp;
24940Sstevel@tonic-gate {
24950Sstevel@tonic-gate 	struct	portmap_cache *cachep, *prev, *next = NULL, *cp;
24960Sstevel@tonic-gate 	int	retval = 0;
24970Sstevel@tonic-gate 
24980Sstevel@tonic-gate 	timenow = time(NULL);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 	(void) rw_rdlock(&portmap_cache_lock);
25010Sstevel@tonic-gate 
25020Sstevel@tonic-gate 	/*
25030Sstevel@tonic-gate 	 * Increment the portmap cache counters for # accesses and lookups
25040Sstevel@tonic-gate 	 * Use a smaller factor (100 vs 1000 for the host cache) since
25050Sstevel@tonic-gate 	 * initial analysis shows this cache is looked up 10% that of the
25060Sstevel@tonic-gate 	 * host cache.
25070Sstevel@tonic-gate 	 */
25080Sstevel@tonic-gate #ifdef CACHE_DEBUG
25090Sstevel@tonic-gate 	portmap_cache_accesses++;
25100Sstevel@tonic-gate 	portmap_cache_lookups++;
25110Sstevel@tonic-gate 	if ((portmap_cache_lookups%100) == 0)
25120Sstevel@tonic-gate 		trace_portmap_cache();
25130Sstevel@tonic-gate #endif /* CACHE_DEBUG */
25140Sstevel@tonic-gate 
25150Sstevel@tonic-gate 	for (cachep = portmap_cache_head; cachep;
25160Sstevel@tonic-gate 		cachep = cachep->cache_next) {
25170Sstevel@tonic-gate 		if (timenow > cachep->cache_time) {
25180Sstevel@tonic-gate 			/*
25190Sstevel@tonic-gate 			 * We stumbled across an entry in the cache which
25200Sstevel@tonic-gate 			 * has timed out. Free up all the entries that
25210Sstevel@tonic-gate 			 * were added before it, which will positionally
25220Sstevel@tonic-gate 			 * be after this entry. And adjust neighboring
25230Sstevel@tonic-gate 			 * pointers.
25240Sstevel@tonic-gate 			 * When we drop the lock and re-acquire it, we
25250Sstevel@tonic-gate 			 * need to start from the beginning.
25260Sstevel@tonic-gate 			 */
25270Sstevel@tonic-gate 			(void) rw_unlock(&portmap_cache_lock);
25280Sstevel@tonic-gate 			(void) rw_wrlock(&portmap_cache_lock);
25290Sstevel@tonic-gate 			for (cp = portmap_cache_head;
25300Sstevel@tonic-gate 				cp && (cp->cache_time >= timenow);
25310Sstevel@tonic-gate 				cp = cp->cache_next)
25320Sstevel@tonic-gate 				;
25330Sstevel@tonic-gate 			if (cp == NULL)
25340Sstevel@tonic-gate 				goto done;
25350Sstevel@tonic-gate 			/*
25360Sstevel@tonic-gate 			 * Adjust the link of the predecessor.
25370Sstevel@tonic-gate 			 * Make the tail point to the new last entry.
25380Sstevel@tonic-gate 			 */
25390Sstevel@tonic-gate 			prev = cp->cache_prev;
25400Sstevel@tonic-gate 			if (prev == NULL) {
25410Sstevel@tonic-gate 				portmap_cache_head = NULL;
25420Sstevel@tonic-gate 				portmap_cache_tail = NULL;
25430Sstevel@tonic-gate 			} else {
25440Sstevel@tonic-gate 				prev->cache_next = NULL;
25450Sstevel@tonic-gate 				portmap_cache_tail = prev;
25460Sstevel@tonic-gate 			}
25470Sstevel@tonic-gate 			for (; cp; cp = next) {
25480Sstevel@tonic-gate 				if (cp->cache_hostname != NULL &&
25490Sstevel@tonic-gate 				    cp->cache_hostname !=
25500Sstevel@tonic-gate 				    cp->cache_small_hosts)
25510Sstevel@tonic-gate 					free(cp->cache_hostname);
25520Sstevel@tonic-gate 				if (cp->cache_proto != NULL &&
25530Sstevel@tonic-gate 				    cp->cache_proto !=
25540Sstevel@tonic-gate 				    cp->cache_small_proto)
25550Sstevel@tonic-gate 					free(cp->cache_proto);
25560Sstevel@tonic-gate 				if (cp->cache_srv_addr.buf != NULL)
25570Sstevel@tonic-gate 					free(cp->cache_srv_addr.buf);
25580Sstevel@tonic-gate 				next = cp->cache_next;
25590Sstevel@tonic-gate 				free(cp);
25600Sstevel@tonic-gate 			}
25610Sstevel@tonic-gate 			goto done;
25620Sstevel@tonic-gate 		}
25630Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL ||
25640Sstevel@tonic-gate 		    prog != cachep->cache_prog || vers != cachep->cache_vers ||
25650Sstevel@tonic-gate 		    strcmp(nconf->nc_proto, cachep->cache_proto) != 0 ||
25660Sstevel@tonic-gate 		    strcmp(nconf->nc_protofmly, cachep->cache_protofmly) != 0 ||
25670Sstevel@tonic-gate 		    strcmp(hostname, cachep->cache_hostname) != 0)
25680Sstevel@tonic-gate 			continue;
25690Sstevel@tonic-gate 		/*
25700Sstevel@tonic-gate 		 * Cache Hit.
25710Sstevel@tonic-gate 		 */
25720Sstevel@tonic-gate #ifdef CACHE_DEBUG
25730Sstevel@tonic-gate 		portmap_cache_hits++;	/* up portmap cache hit counter */
25740Sstevel@tonic-gate #endif /* CACHE_DEBUG */
25750Sstevel@tonic-gate 		addrp->len = cachep->cache_srv_addr.len;
25760Sstevel@tonic-gate 		memcpy(addrp->buf, cachep->cache_srv_addr.buf, addrp->len);
25770Sstevel@tonic-gate 		retval = 1;
25780Sstevel@tonic-gate 		break;
25790Sstevel@tonic-gate 	}
25800Sstevel@tonic-gate done:
25810Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
25820Sstevel@tonic-gate 	return (retval);
25830Sstevel@tonic-gate }
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate static void
25860Sstevel@tonic-gate portmap_cache_enter(hostname, prog, vers, nconf, addrp)
25870Sstevel@tonic-gate 	char *hostname;
25880Sstevel@tonic-gate 	rpcprog_t prog;
25890Sstevel@tonic-gate 	rpcvers_t vers;
25900Sstevel@tonic-gate 	struct netconfig *nconf;
25910Sstevel@tonic-gate 	struct netbuf *addrp;
25920Sstevel@tonic-gate {
25930Sstevel@tonic-gate 	struct portmap_cache *cachep;
25940Sstevel@tonic-gate 	int protofmlylen;
25950Sstevel@tonic-gate 	int protolen, hostnamelen;
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	timenow = time(NULL);
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 	cachep = malloc(sizeof (struct portmap_cache));
26000Sstevel@tonic-gate 	if (cachep == NULL)
26010Sstevel@tonic-gate 		return;
26020Sstevel@tonic-gate 	memset((char *)cachep, 0, sizeof (*cachep));
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	hostnamelen = strlen(hostname);
26050Sstevel@tonic-gate 	if (hostnamelen <= SMALL_HOSTNAME)
26060Sstevel@tonic-gate 		cachep->cache_hostname = cachep->cache_small_hosts;
26070Sstevel@tonic-gate 	else {
26080Sstevel@tonic-gate 		cachep->cache_hostname = malloc(hostnamelen + 1);
26090Sstevel@tonic-gate 		if (cachep->cache_hostname == NULL)
26100Sstevel@tonic-gate 			goto nomem;
26110Sstevel@tonic-gate 	}
26120Sstevel@tonic-gate 	strcpy(cachep->cache_hostname, hostname);
26130Sstevel@tonic-gate 	protolen = strlen(nconf->nc_proto);
26140Sstevel@tonic-gate 	if (protolen <= SMALL_PROTONAME)
26150Sstevel@tonic-gate 		cachep->cache_proto = cachep->cache_small_proto;
26160Sstevel@tonic-gate 	else {
26170Sstevel@tonic-gate 		cachep->cache_proto = malloc(protolen + 1);
26180Sstevel@tonic-gate 		if (cachep->cache_proto == NULL)
26190Sstevel@tonic-gate 			goto nomem;
26200Sstevel@tonic-gate 	}
26210Sstevel@tonic-gate 	protofmlylen = strlen(nconf->nc_protofmly);
26220Sstevel@tonic-gate 	if (protofmlylen <= SMALL_PROTOFMLYNAME)
26230Sstevel@tonic-gate 		cachep->cache_protofmly = cachep->cache_small_protofmly;
26240Sstevel@tonic-gate 	else {
26250Sstevel@tonic-gate 		cachep->cache_protofmly = malloc(protofmlylen + 1);
26260Sstevel@tonic-gate 		if (cachep->cache_protofmly == NULL)
26270Sstevel@tonic-gate 			goto nomem;
26280Sstevel@tonic-gate 	}
26290Sstevel@tonic-gate 
26300Sstevel@tonic-gate 	strcpy(cachep->cache_proto, nconf->nc_proto);
26310Sstevel@tonic-gate 	cachep->cache_prog = prog;
26320Sstevel@tonic-gate 	cachep->cache_vers = vers;
26330Sstevel@tonic-gate 	cachep->cache_time = timenow + portmap_cache_valid_time;
26340Sstevel@tonic-gate 	cachep->cache_srv_addr.len = addrp->len;
26350Sstevel@tonic-gate 	cachep->cache_srv_addr.buf = malloc(addrp->len);
26360Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf == NULL)
26370Sstevel@tonic-gate 		goto nomem;
26380Sstevel@tonic-gate 	memcpy(cachep->cache_srv_addr.buf, addrp->buf, addrp->maxlen);
26390Sstevel@tonic-gate 	cachep->cache_prev = NULL;
26400Sstevel@tonic-gate 	(void) rw_wrlock(&portmap_cache_lock);
26410Sstevel@tonic-gate 	/*
26420Sstevel@tonic-gate 	 * There's a window in which we could have multiple threads making
26430Sstevel@tonic-gate 	 * the same cache entry. This can be avoided by walking the cache
26440Sstevel@tonic-gate 	 * once again here to check and see if there are duplicate entries
26450Sstevel@tonic-gate 	 * (after grabbing the write lock). This isn't fatal and I'm not
26460Sstevel@tonic-gate 	 * going to bother with this.
26470Sstevel@tonic-gate 	 */
26480Sstevel@tonic-gate #ifdef CACHE_DEBUG
26490Sstevel@tonic-gate 	portmap_cache_accesses++;	/* up portmap cache access counter */
26500Sstevel@tonic-gate #endif /* CACHE_DEBUG */
26510Sstevel@tonic-gate 	cachep->cache_next = portmap_cache_head;
26520Sstevel@tonic-gate 	if (portmap_cache_head != NULL)
26530Sstevel@tonic-gate 		portmap_cache_head->cache_prev = cachep;
26540Sstevel@tonic-gate 	portmap_cache_head = cachep;
26550Sstevel@tonic-gate 	(void) rw_unlock(&portmap_cache_lock);
26560Sstevel@tonic-gate 	return;
26570Sstevel@tonic-gate 
26580Sstevel@tonic-gate nomem:
26590Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache_enter: Memory allocation failed");
26600Sstevel@tonic-gate 	if (cachep->cache_srv_addr.buf)
26610Sstevel@tonic-gate 		free(cachep->cache_srv_addr.buf);
26620Sstevel@tonic-gate 	if (cachep->cache_proto && protolen > SMALL_PROTONAME)
26630Sstevel@tonic-gate 		free(cachep->cache_proto);
26640Sstevel@tonic-gate 	if (cachep->cache_hostname && hostnamelen > SMALL_HOSTNAME)
26650Sstevel@tonic-gate 		free(cachep->cache_hostname);
26660Sstevel@tonic-gate 	if (cachep->cache_protofmly && protofmlylen > SMALL_PROTOFMLYNAME)
26670Sstevel@tonic-gate 		free(cachep->cache_protofmly);
26680Sstevel@tonic-gate 	if (cachep)
26690Sstevel@tonic-gate 		free(cachep);
26700Sstevel@tonic-gate 	cachep = NULL;
26710Sstevel@tonic-gate }
26720Sstevel@tonic-gate 
26730Sstevel@tonic-gate static int
26740Sstevel@tonic-gate get_cached_srv_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
26750Sstevel@tonic-gate 	struct netconfig *nconf, struct netbuf *addrp)
26760Sstevel@tonic-gate {
26770Sstevel@tonic-gate 	if (portmap_cache_lookup(hostname, prog, vers, nconf, addrp))
26780Sstevel@tonic-gate 		return (1);
26790Sstevel@tonic-gate 	if (rpcb_getaddr(prog, vers, nconf, addrp, hostname) == 0)
26800Sstevel@tonic-gate 		return (0);
26810Sstevel@tonic-gate 	portmap_cache_enter(hostname, prog, vers, nconf, addrp);
26820Sstevel@tonic-gate 	return (1);
26830Sstevel@tonic-gate }
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate /*
26860Sstevel@tonic-gate  * Get the network address on "hostname" for program "prog"
26870Sstevel@tonic-gate  * with version "vers" by using the nconf configuration data
26880Sstevel@tonic-gate  * passed in.
26890Sstevel@tonic-gate  *
26900Sstevel@tonic-gate  * If the address of a netconfig pointer is null then
26910Sstevel@tonic-gate  * information is not sufficient and no netbuf will be returned.
26920Sstevel@tonic-gate  *
26930Sstevel@tonic-gate  * tinfo argument is for matching the get_the_addr() defined in
26940Sstevel@tonic-gate  * ../nfs/mount/mount.c
26950Sstevel@tonic-gate  */
26960Sstevel@tonic-gate static void *
26970Sstevel@tonic-gate get_the_stuff(
26980Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
26990Sstevel@tonic-gate 	char *hostname,
27000Sstevel@tonic-gate 	rpcprog_t prog,
27010Sstevel@tonic-gate 	rpcprog_t vers,
27020Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
27030Sstevel@tonic-gate 	struct netconfig *nconf,
27040Sstevel@tonic-gate 	ushort_t port,
27050Sstevel@tonic-gate 	struct t_info *tinfo,
27060Sstevel@tonic-gate 	caddr_t *fhp,
27070Sstevel@tonic-gate 	bool_t direct_to_server,
27080Sstevel@tonic-gate 	char *fspath,
27090Sstevel@tonic-gate 	enum clnt_stat *cstat)
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate {
27120Sstevel@tonic-gate 	struct netbuf *nb = NULL;
27130Sstevel@tonic-gate 	struct t_bind *tbind = NULL;
27140Sstevel@tonic-gate 	int fd = -1;
27150Sstevel@tonic-gate 	enum clnt_stat cs = RPC_TIMEDOUT;
27160Sstevel@tonic-gate 	CLIENT *cl = NULL;
27170Sstevel@tonic-gate 	struct timeval tv;
27180Sstevel@tonic-gate 	AUTH *ah = NULL;
27190Sstevel@tonic-gate 	AUTH *new_ah = NULL;
27200Sstevel@tonic-gate 	struct snego_t snego;
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	if (nconf == NULL) {
27230Sstevel@tonic-gate 		goto done;
27240Sstevel@tonic-gate 	}
27250Sstevel@tonic-gate 
27260Sstevel@tonic-gate 	if (prog == NFS_PROGRAM && vers == NFS_V4)
27270Sstevel@tonic-gate 		if (strncasecmp(nconf->nc_proto, NC_UDP, strlen(NC_UDP)) == 0)
27280Sstevel@tonic-gate 			goto done;
27290Sstevel@tonic-gate 
27300Sstevel@tonic-gate 	if ((fd = t_open(nconf->nc_device, O_RDWR, tinfo)) < 0) {
27310Sstevel@tonic-gate 		goto done;
27320Sstevel@tonic-gate 	}
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 	/* LINTED pointer alignment */
27350Sstevel@tonic-gate 	if ((tbind = (struct t_bind *)t_alloc(fd, T_BIND, T_ADDR))
27360Sstevel@tonic-gate 		    == NULL) {
27370Sstevel@tonic-gate 			goto done;
27380Sstevel@tonic-gate 	}
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 	if (direct_to_server == TRUE) {
27410Sstevel@tonic-gate 		struct nd_hostserv hs;
27420Sstevel@tonic-gate 		struct nd_addrlist *retaddrs;
27430Sstevel@tonic-gate 		hs.h_host = hostname;
27440Sstevel@tonic-gate 
27450Sstevel@tonic-gate 		if (trace > 1)
27460Sstevel@tonic-gate 			trace_prt(1, "	get_the_stuff: %s call "
27470Sstevel@tonic-gate 				"direct to server %s\n",
27480Sstevel@tonic-gate 				type_of_stuff == SERVER_FH ? "pub fh" :
27490Sstevel@tonic-gate 				type_of_stuff == SERVER_ADDR ? "get address" :
27500Sstevel@tonic-gate 				type_of_stuff == SERVER_PING ? "ping" :
27510Sstevel@tonic-gate 				"unknown", hostname);
27520Sstevel@tonic-gate 		if (port == 0)
27530Sstevel@tonic-gate 			hs.h_serv = "nfs";
27540Sstevel@tonic-gate 		else
27550Sstevel@tonic-gate 			hs.h_serv = NULL;
27560Sstevel@tonic-gate 
27570Sstevel@tonic-gate 		if (netdir_getbyname(nconf, &hs, &retaddrs) != ND_OK) {
27580Sstevel@tonic-gate 			goto done;
27590Sstevel@tonic-gate 		}
27600Sstevel@tonic-gate 		memcpy(tbind->addr.buf, retaddrs->n_addrs->buf,
27610Sstevel@tonic-gate 			retaddrs->n_addrs->len);
27620Sstevel@tonic-gate 		tbind->addr.len = retaddrs->n_addrs->len;
27630Sstevel@tonic-gate 		netdir_free((void *)retaddrs, ND_ADDRLIST);
27640Sstevel@tonic-gate 		if (port) {
27650Sstevel@tonic-gate 			/* LINTED pointer alignment */
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
27680Sstevel@tonic-gate 				((struct sockaddr_in *)
27690Sstevel@tonic-gate 				tbind->addr.buf)->sin_port =
27700Sstevel@tonic-gate 					htons((ushort_t)port);
27710Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
27720Sstevel@tonic-gate 				((struct sockaddr_in6 *)
27730Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port =
27740Sstevel@tonic-gate 					htons((ushort_t)port);
27750Sstevel@tonic-gate 		}
27760Sstevel@tonic-gate 
27770Sstevel@tonic-gate 		if (type_of_stuff == SERVER_FH) {
27780Sstevel@tonic-gate 			if (netdir_options(nconf, ND_SET_RESERVEDPORT, fd,
27790Sstevel@tonic-gate 				NULL) == -1)
27800Sstevel@tonic-gate 				if (trace > 1)
27810Sstevel@tonic-gate 					trace_prt(1, "\tget_the_stuff: "
27820Sstevel@tonic-gate 						"ND_SET_RESERVEDPORT(%s) "
27830Sstevel@tonic-gate 						"failed\n", hostname);
27840Sstevel@tonic-gate 		}
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 		cl = clnt_tli_create(fd, nconf, &tbind->addr, prog,
27870Sstevel@tonic-gate 			vers, 0, 0);
27880Sstevel@tonic-gate 
27890Sstevel@tonic-gate 		if (trace > 1)
27900Sstevel@tonic-gate 			trace_prt(1, "	get_the_stuff: clnt_tli_create(%s) "
27910Sstevel@tonic-gate 				"returned %p\n", hostname, cl);
27920Sstevel@tonic-gate 		if (cl == NULL)
27930Sstevel@tonic-gate 			goto done;
27940Sstevel@tonic-gate #ifdef MALLOC_DEBUG
27950Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
27960Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
27970Sstevel@tonic-gate 			__FILE__, __LINE__);
27980Sstevel@tonic-gate #endif
27990Sstevel@tonic-gate 
28000Sstevel@tonic-gate 		switch (type_of_stuff) {
28010Sstevel@tonic-gate 		case SERVER_FH:
28020Sstevel@tonic-gate 		    {
28030Sstevel@tonic-gate 		    enum snego_stat sec;
28040Sstevel@tonic-gate 
28050Sstevel@tonic-gate 		    ah = authsys_create_default();
28060Sstevel@tonic-gate 		    if (ah != NULL) {
28070Sstevel@tonic-gate #ifdef MALLOC_DEBUG
28080Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
28090Sstevel@tonic-gate 				__FILE__, __LINE__);
28100Sstevel@tonic-gate #endif
28110Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
28120Sstevel@tonic-gate 			cl->cl_auth = ah;
28130Sstevel@tonic-gate #ifdef MALLOC_DEBUG
28140Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
28150Sstevel@tonic-gate 				__FILE__, __LINE__);
28160Sstevel@tonic-gate #endif
28170Sstevel@tonic-gate 		    }
28180Sstevel@tonic-gate 
28190Sstevel@tonic-gate 		    if (!mfssnego->snego_done && vers != NFS_V4) {
28200Sstevel@tonic-gate 			/*
28210Sstevel@tonic-gate 			 * negotiate sec flavor.
28220Sstevel@tonic-gate 			 */
28230Sstevel@tonic-gate 			snego.cnt = 0;
28240Sstevel@tonic-gate 			if ((sec = nfs_sec_nego(vers, cl, fspath, &snego)) ==
28250Sstevel@tonic-gate 				SNEGO_SUCCESS) {
28260Sstevel@tonic-gate 			    int jj;
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 			/*
28290Sstevel@tonic-gate 			 * check if server supports the one
28300Sstevel@tonic-gate 			 * specified in the sec= option.
28310Sstevel@tonic-gate 			 */
28320Sstevel@tonic-gate 			    if (mfssnego->sec_opt) {
28330Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
28340Sstevel@tonic-gate 				    if (snego.array[jj] ==
28350Sstevel@tonic-gate 					mfssnego->nfs_sec.sc_nfsnum) {
28360Sstevel@tonic-gate 					mfssnego->snego_done = TRUE;
28370Sstevel@tonic-gate 					break;
28380Sstevel@tonic-gate 				    }
28390Sstevel@tonic-gate 				}
28400Sstevel@tonic-gate 			    }
28410Sstevel@tonic-gate 
28420Sstevel@tonic-gate 			/*
28430Sstevel@tonic-gate 			 * find a common sec flavor
28440Sstevel@tonic-gate 			 */
28450Sstevel@tonic-gate 			    if (!mfssnego->snego_done) {
28460Sstevel@tonic-gate 				for (jj = 0; jj < snego.cnt; jj++) {
28470Sstevel@tonic-gate 				    if (!nfs_getseconfig_bynumber(
28480Sstevel@tonic-gate 					snego.array[jj], &mfssnego->nfs_sec)) {
28490Sstevel@tonic-gate 					mfssnego->snego_done = TRUE;
28500Sstevel@tonic-gate 					break;
28510Sstevel@tonic-gate 				    }
28520Sstevel@tonic-gate 				}
28530Sstevel@tonic-gate 			    }
28540Sstevel@tonic-gate 			    if (!mfssnego->snego_done)
28550Sstevel@tonic-gate 				return (NULL);
28560Sstevel@tonic-gate 
28570Sstevel@tonic-gate 			/*
28580Sstevel@tonic-gate 			 * Now that the flavor has been
28590Sstevel@tonic-gate 			 * negotiated, get the fh.
28600Sstevel@tonic-gate 			 *
28610Sstevel@tonic-gate 			 * First, create an auth handle using the negotiated
28620Sstevel@tonic-gate 			 * sec flavor in the next lookup to
28630Sstevel@tonic-gate 			 * fetch the filehandle.
28640Sstevel@tonic-gate 			 */
28650Sstevel@tonic-gate 			    new_ah = nfs_create_ah(cl, hostname,
28660Sstevel@tonic-gate 					&mfssnego->nfs_sec);
28670Sstevel@tonic-gate 			    if (new_ah == NULL)
28680Sstevel@tonic-gate 				goto done;
28690Sstevel@tonic-gate #ifdef MALLOC_DEBUG
28700Sstevel@tonic-gate 			    drop_alloc("AUTH_HANDLE", cl->cl_auth,
28710Sstevel@tonic-gate 				__FILE__, __LINE__);
28720Sstevel@tonic-gate #endif
28730Sstevel@tonic-gate 			    AUTH_DESTROY(cl->cl_auth);
28740Sstevel@tonic-gate 			    cl->cl_auth = new_ah;
28750Sstevel@tonic-gate #ifdef MALLOC_DEBUG
28760Sstevel@tonic-gate 			    add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
28770Sstevel@tonic-gate 				__FILE__, __LINE__);
28780Sstevel@tonic-gate #endif
28790Sstevel@tonic-gate 			} else if (sec == SNEGO_ARRAY_TOO_SMALL ||
28800Sstevel@tonic-gate 			    sec == SNEGO_FAILURE) {
28810Sstevel@tonic-gate 			    goto done;
28820Sstevel@tonic-gate 			}
28830Sstevel@tonic-gate 			/*
28840Sstevel@tonic-gate 			 * Note that if sec == SNEGO_DEF_VALID
28850Sstevel@tonic-gate 			 * the default sec flavor is acceptable.
28860Sstevel@tonic-gate 			 * Use it to get the filehandle.
28870Sstevel@tonic-gate 			 */
28880Sstevel@tonic-gate 		    }
28890Sstevel@tonic-gate 		    }
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate 		    switch (vers) {
28920Sstevel@tonic-gate 		    case NFS_VERSION:
28930Sstevel@tonic-gate 			    {
28940Sstevel@tonic-gate 			    wnl_diropargs arg;
28950Sstevel@tonic-gate 			    wnl_diropres *res;
28960Sstevel@tonic-gate 
28970Sstevel@tonic-gate 			    memset((char *)&arg.dir, 0, sizeof (wnl_fh));
28980Sstevel@tonic-gate 			    arg.name = fspath;
28990Sstevel@tonic-gate 			    res = wnlproc_lookup_2(&arg, cl);
29000Sstevel@tonic-gate 
29010Sstevel@tonic-gate 			    if (res == NULL || res->status != NFS_OK)
29020Sstevel@tonic-gate 				    goto done;
29030Sstevel@tonic-gate 			    *fhp = malloc(sizeof (wnl_fh));
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 			    if (*fhp == NULL) {
29060Sstevel@tonic-gate 				    syslog(LOG_ERR, "no memory\n");
29070Sstevel@tonic-gate 				    goto done;
29080Sstevel@tonic-gate 			    }
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 			    memcpy((char *)*fhp,
29110Sstevel@tonic-gate 			    (char *)&res->wnl_diropres_u.wnl_diropres.file,
29120Sstevel@tonic-gate 				sizeof (wnl_fh));
29130Sstevel@tonic-gate 			    cs = RPC_SUCCESS;
29140Sstevel@tonic-gate 			    }
29150Sstevel@tonic-gate 			    break;
29160Sstevel@tonic-gate 		    case NFS_V3:
29170Sstevel@tonic-gate 			    {
29180Sstevel@tonic-gate 			    WNL_LOOKUP3args arg;
29190Sstevel@tonic-gate 			    WNL_LOOKUP3res *res;
29200Sstevel@tonic-gate 			    nfs_fh3 *fh3p;
29210Sstevel@tonic-gate 
29220Sstevel@tonic-gate 			    memset((char *)&arg.what.dir, 0, sizeof (wnl_fh3));
29230Sstevel@tonic-gate 			    arg.what.name = fspath;
29240Sstevel@tonic-gate 			    res = wnlproc3_lookup_3(&arg, cl);
29250Sstevel@tonic-gate 
29260Sstevel@tonic-gate 			    if (res == NULL || res->status != NFS3_OK)
29270Sstevel@tonic-gate 				    goto done;
29280Sstevel@tonic-gate 
29290Sstevel@tonic-gate 			    fh3p = (nfs_fh3 *)malloc(sizeof (*fh3p));
29300Sstevel@tonic-gate 
29310Sstevel@tonic-gate 			    if (fh3p == NULL) {
29320Sstevel@tonic-gate 				    syslog(LOG_ERR, "no memory\n");
29330Sstevel@tonic-gate 				    CLNT_FREERES(cl, xdr_WNL_LOOKUP3res,
29340Sstevel@tonic-gate 					(char *)res);
29350Sstevel@tonic-gate 				    goto done;
29360Sstevel@tonic-gate 			    }
29370Sstevel@tonic-gate 
29380Sstevel@tonic-gate 			    fh3p->fh3_length = res->
29390Sstevel@tonic-gate 				WNL_LOOKUP3res_u.res_ok.object.data.data_len;
29400Sstevel@tonic-gate 			    memcpy(fh3p->fh3_u.data, res->
29410Sstevel@tonic-gate 				WNL_LOOKUP3res_u.res_ok.object.data.data_val,
29420Sstevel@tonic-gate 				fh3p->fh3_length);
29430Sstevel@tonic-gate 
29440Sstevel@tonic-gate 			    *fhp = (caddr_t)fh3p;
29450Sstevel@tonic-gate 
29460Sstevel@tonic-gate 			    CLNT_FREERES(cl, xdr_WNL_LOOKUP3res, (char *)res);
29470Sstevel@tonic-gate 			    cs = RPC_SUCCESS;
29480Sstevel@tonic-gate 			    }
29490Sstevel@tonic-gate 			    break;
29500Sstevel@tonic-gate 		    case NFS_V4:
29510Sstevel@tonic-gate 			    *fhp = strdup(fspath);
29520Sstevel@tonic-gate 			    cs = RPC_SUCCESS;
29530Sstevel@tonic-gate 			    break;
29540Sstevel@tonic-gate 		    }
29550Sstevel@tonic-gate 		    break;
29560Sstevel@tonic-gate 		case SERVER_ADDR:
29570Sstevel@tonic-gate 		case SERVER_PING:
29580Sstevel@tonic-gate 			tv.tv_sec = 10;
29590Sstevel@tonic-gate 			tv.tv_usec = 0;
29600Sstevel@tonic-gate 			cs = clnt_call(cl, NULLPROC, xdr_void, 0,
29610Sstevel@tonic-gate 				xdr_void, 0, tv);
29620Sstevel@tonic-gate 			if (trace > 1)
29630Sstevel@tonic-gate 				trace_prt(1,
29640Sstevel@tonic-gate 					"get_the_stuff: clnt_call(%s) "
29650Sstevel@tonic-gate 					"returned %s\n",
29660Sstevel@tonic-gate 				hostname,
29670Sstevel@tonic-gate 					cs == RPC_SUCCESS ? "success" :
29680Sstevel@tonic-gate 					"failure");
29690Sstevel@tonic-gate 
29700Sstevel@tonic-gate 			if (cs != RPC_SUCCESS)
29710Sstevel@tonic-gate 				goto done;
29720Sstevel@tonic-gate 			break;
29730Sstevel@tonic-gate 		}
29740Sstevel@tonic-gate 
29750Sstevel@tonic-gate 	} else if (type_of_stuff != SERVER_FH) {
29760Sstevel@tonic-gate 
29770Sstevel@tonic-gate 		if (type_of_stuff == SERVER_ADDR) {
29780Sstevel@tonic-gate 			if (get_cached_srv_addr(hostname, prog, vers, nconf,
29790Sstevel@tonic-gate 			    &tbind->addr) == 0)
29800Sstevel@tonic-gate 				goto done;
29810Sstevel@tonic-gate 		}
29820Sstevel@tonic-gate 
29830Sstevel@tonic-gate 		if (port) {
29840Sstevel@tonic-gate 			/* LINTED pointer alignment */
29850Sstevel@tonic-gate 			if (strcmp(nconf->nc_protofmly, NC_INET) == NULL)
29860Sstevel@tonic-gate 				((struct sockaddr_in *)
29870Sstevel@tonic-gate 				tbind->addr.buf)->sin_port =
29880Sstevel@tonic-gate 					htons((ushort_t)port);
29890Sstevel@tonic-gate 			else if (strcmp(nconf->nc_protofmly, NC_INET6) == NULL)
29900Sstevel@tonic-gate 				((struct sockaddr_in6 *)
29910Sstevel@tonic-gate 				tbind->addr.buf)->sin6_port =
29920Sstevel@tonic-gate 					htons((ushort_t)port);
29930Sstevel@tonic-gate 			cl = clnt_tli_create(fd, nconf, &tbind->addr,
29940Sstevel@tonic-gate 				prog, vers, 0, 0);
29950Sstevel@tonic-gate 			if (cl == NULL)
29960Sstevel@tonic-gate 				goto done;
29970Sstevel@tonic-gate #ifdef MALLOC_DEBUG
29980Sstevel@tonic-gate 			add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
29990Sstevel@tonic-gate 			add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
30000Sstevel@tonic-gate 				__FILE__, __LINE__);
30010Sstevel@tonic-gate #endif
30020Sstevel@tonic-gate 			tv.tv_sec = 10;
30030Sstevel@tonic-gate 			tv.tv_usec = 0;
30040Sstevel@tonic-gate 			cs = clnt_call(cl, NULLPROC, xdr_void, 0, xdr_void,
30050Sstevel@tonic-gate 				0, tv);
30060Sstevel@tonic-gate 			if (cs != RPC_SUCCESS)
30070Sstevel@tonic-gate 				goto done;
30080Sstevel@tonic-gate 		}
30090Sstevel@tonic-gate 
30100Sstevel@tonic-gate 	} else {
30110Sstevel@tonic-gate 		/* can't happen */
30120Sstevel@tonic-gate 		goto done;
30130Sstevel@tonic-gate 	}
30140Sstevel@tonic-gate 
30150Sstevel@tonic-gate 	if (type_of_stuff != SERVER_PING) {
30160Sstevel@tonic-gate 
30170Sstevel@tonic-gate 		cs = RPC_SYSTEMERROR;
30180Sstevel@tonic-gate 
30190Sstevel@tonic-gate 		/*
30200Sstevel@tonic-gate 		 * Make a copy of the netbuf to return
30210Sstevel@tonic-gate 		 */
30220Sstevel@tonic-gate 		nb = (struct netbuf *)malloc(sizeof (struct netbuf));
30230Sstevel@tonic-gate 		if (nb == NULL) {
30240Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
30250Sstevel@tonic-gate 			goto done;
30260Sstevel@tonic-gate 		}
30270Sstevel@tonic-gate 		*nb = tbind->addr;
30280Sstevel@tonic-gate 		nb->buf = (char *)malloc(nb->maxlen);
30290Sstevel@tonic-gate 		if (nb->buf == NULL) {
30300Sstevel@tonic-gate 			syslog(LOG_ERR, "no memory\n");
30310Sstevel@tonic-gate 			free(nb);
30320Sstevel@tonic-gate 			nb = NULL;
30330Sstevel@tonic-gate 			goto done;
30340Sstevel@tonic-gate 		}
30350Sstevel@tonic-gate 		(void) memcpy(nb->buf, tbind->addr.buf, tbind->addr.len);
30360Sstevel@tonic-gate 
30370Sstevel@tonic-gate 		cs = RPC_SUCCESS;
30380Sstevel@tonic-gate 	}
30390Sstevel@tonic-gate 
30400Sstevel@tonic-gate done:
30410Sstevel@tonic-gate 	if (cl != NULL) {
30420Sstevel@tonic-gate 		if (ah != NULL) {
30430Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30440Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
30450Sstevel@tonic-gate 				__FILE__, __LINE__);
30460Sstevel@tonic-gate #endif
30470Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
30480Sstevel@tonic-gate 			cl->cl_auth = NULL;
30490Sstevel@tonic-gate 		}
30500Sstevel@tonic-gate #ifdef MALLOC_DEBUG
30510Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl, __FILE__, __LINE__);
30520Sstevel@tonic-gate #endif
30530Sstevel@tonic-gate 		clnt_destroy(cl);
30540Sstevel@tonic-gate 	}
30550Sstevel@tonic-gate 
30560Sstevel@tonic-gate 	if (tbind) {
30570Sstevel@tonic-gate 		t_free((char *)tbind, T_BIND);
30580Sstevel@tonic-gate 		tbind = NULL;
30590Sstevel@tonic-gate 	}
30600Sstevel@tonic-gate 
30610Sstevel@tonic-gate 	if (fd >= 0)
30620Sstevel@tonic-gate 		(void) t_close(fd);
30630Sstevel@tonic-gate 
30640Sstevel@tonic-gate 	if (cstat != NULL)
30650Sstevel@tonic-gate 		*cstat = cs;
30660Sstevel@tonic-gate 
30670Sstevel@tonic-gate 	return (nb);
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate 
30700Sstevel@tonic-gate /*
30710Sstevel@tonic-gate  * Get a network address on "hostname" for program "prog"
30720Sstevel@tonic-gate  * with version "vers".  If the port number is specified (non zero)
30730Sstevel@tonic-gate  * then try for a TCP/UDP transport and set the port number of the
30740Sstevel@tonic-gate  * resulting IP address.
30750Sstevel@tonic-gate  *
30760Sstevel@tonic-gate  * If the address of a netconfig pointer was passed and
30770Sstevel@tonic-gate  * if it's not null, use it as the netconfig otherwise
30780Sstevel@tonic-gate  * assign the address of the netconfig that was used to
30790Sstevel@tonic-gate  * establish contact with the service.
30800Sstevel@tonic-gate  *
30810Sstevel@tonic-gate  * tinfo argument is for matching the get_addr() defined in
30820Sstevel@tonic-gate  * ../nfs/mount/mount.c
30830Sstevel@tonic-gate  */
30840Sstevel@tonic-gate 
30850Sstevel@tonic-gate static struct netbuf *
30860Sstevel@tonic-gate get_addr(char *hostname, rpcprog_t prog, rpcvers_t vers,
30870Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
30880Sstevel@tonic-gate 	struct t_info *tinfo)
30890Sstevel@tonic-gate 
30900Sstevel@tonic-gate {
30910Sstevel@tonic-gate 	enum clnt_stat cstat;
30920Sstevel@tonic-gate 
30930Sstevel@tonic-gate 	return (get_server_stuff(SERVER_ADDR, hostname, prog, vers, NULL,
30940Sstevel@tonic-gate 		nconfp, proto, port, tinfo, NULL, FALSE, NULL, &cstat));
30950Sstevel@tonic-gate }
30960Sstevel@tonic-gate 
30970Sstevel@tonic-gate static struct netbuf *
30980Sstevel@tonic-gate get_pubfh(char *hostname, rpcvers_t vers, mfs_snego_t *mfssnego,
30990Sstevel@tonic-gate 	struct netconfig **nconfp, char *proto, ushort_t port,
31000Sstevel@tonic-gate 	struct t_info *tinfo, caddr_t *fhp, bool_t get_pubfh, char *fspath)
31010Sstevel@tonic-gate {
31020Sstevel@tonic-gate 	enum clnt_stat cstat;
31030Sstevel@tonic-gate 
31040Sstevel@tonic-gate 	return (get_server_stuff(SERVER_FH, hostname, NFS_PROGRAM, vers,
31050Sstevel@tonic-gate 		mfssnego, nconfp, proto, port, tinfo, fhp, get_pubfh, fspath,
31060Sstevel@tonic-gate 		&cstat));
31070Sstevel@tonic-gate }
31080Sstevel@tonic-gate 
31090Sstevel@tonic-gate static enum clnt_stat
31100Sstevel@tonic-gate get_ping(char *hostname, rpcprog_t prog, rpcvers_t vers,
31110Sstevel@tonic-gate 	struct netconfig **nconfp, ushort_t port, bool_t direct_to_server)
31120Sstevel@tonic-gate {
31130Sstevel@tonic-gate 	enum clnt_stat cstat;
31140Sstevel@tonic-gate 
31150Sstevel@tonic-gate 	(void) get_server_stuff(SERVER_PING, hostname, prog, vers, NULL, nconfp,
31160Sstevel@tonic-gate 		NULL, port, NULL, NULL, direct_to_server, NULL, &cstat);
31170Sstevel@tonic-gate 
31180Sstevel@tonic-gate 	return (cstat);
31190Sstevel@tonic-gate }
31200Sstevel@tonic-gate 
31210Sstevel@tonic-gate static void *
31220Sstevel@tonic-gate get_server_stuff(
31230Sstevel@tonic-gate 	enum type_of_stuff type_of_stuff,
31240Sstevel@tonic-gate 	char *hostname,
31250Sstevel@tonic-gate 	rpcprog_t prog,
31260Sstevel@tonic-gate 	rpcvers_t vers,
31270Sstevel@tonic-gate 	mfs_snego_t *mfssnego,
31280Sstevel@tonic-gate 	struct netconfig **nconfp,
31290Sstevel@tonic-gate 	char *proto,
31300Sstevel@tonic-gate 	ushort_t port,			/* may be zero */
31310Sstevel@tonic-gate 	struct t_info *tinfo,
31320Sstevel@tonic-gate 	caddr_t *fhp,
31330Sstevel@tonic-gate 	bool_t direct_to_server,
31340Sstevel@tonic-gate 	char *fspath,
31350Sstevel@tonic-gate 	enum clnt_stat *cstatp)
31360Sstevel@tonic-gate {
31370Sstevel@tonic-gate 	struct netbuf *nb = NULL;
31380Sstevel@tonic-gate 	struct netconfig *nconf = NULL;
31390Sstevel@tonic-gate 	NCONF_HANDLE *nc = NULL;
31400Sstevel@tonic-gate 	int nthtry = FIRST_TRY;
31410Sstevel@tonic-gate 
31420Sstevel@tonic-gate 	if (nconfp && *nconfp)
31430Sstevel@tonic-gate 		return (get_the_stuff(type_of_stuff, hostname, prog, vers,
31440Sstevel@tonic-gate 			mfssnego, *nconfp, port, tinfo, fhp, direct_to_server,
31450Sstevel@tonic-gate 			fspath, cstatp));
31460Sstevel@tonic-gate 
31470Sstevel@tonic-gate 
31480Sstevel@tonic-gate 	/*
31490Sstevel@tonic-gate 	 * No nconf passed in.
31500Sstevel@tonic-gate 	 *
31510Sstevel@tonic-gate 	 * Try to get a nconf from /etc/netconfig.
31520Sstevel@tonic-gate 	 * First choice is COTS, second is CLTS unless proto
31530Sstevel@tonic-gate 	 * is specified.  When we retry, we reset the
31540Sstevel@tonic-gate 	 * netconfig list, so that we search the whole list
31550Sstevel@tonic-gate 	 * for the next choice.
31560Sstevel@tonic-gate 	 */
31570Sstevel@tonic-gate 	if ((nc = setnetpath()) == NULL)
31580Sstevel@tonic-gate 		goto done;
31590Sstevel@tonic-gate 
31600Sstevel@tonic-gate 	/*
31610Sstevel@tonic-gate 	 * If proto is specified, then only search for the match,
31620Sstevel@tonic-gate 	 * otherwise try COTS first, if failed, then try CLTS.
31630Sstevel@tonic-gate 	 */
31640Sstevel@tonic-gate 	if (proto) {
31650Sstevel@tonic-gate 
31660Sstevel@tonic-gate 		while (nconf = getnetpath(nc)) {
31670Sstevel@tonic-gate 			if (strcmp(nconf->nc_proto, proto))
31680Sstevel@tonic-gate 				continue;
31690Sstevel@tonic-gate 			/*
31700Sstevel@tonic-gate 			 * If the port number is specified then TCP/UDP
31710Sstevel@tonic-gate 			 * is needed. Otherwise any cots/clts will do.
31720Sstevel@tonic-gate 			 */
31730Sstevel@tonic-gate 			if (port)  {
31740Sstevel@tonic-gate 				if ((strcmp(nconf->nc_protofmly, NC_INET) &&
31750Sstevel@tonic-gate 				    strcmp(nconf->nc_protofmly, NC_INET6)) ||
31760Sstevel@tonic-gate 				    (strcmp(nconf->nc_proto, NC_TCP) &&
31770Sstevel@tonic-gate 				    strcmp(nconf->nc_proto, NC_UDP)))
31780Sstevel@tonic-gate 					continue;
31790Sstevel@tonic-gate 			}
31800Sstevel@tonic-gate 
31810Sstevel@tonic-gate 			nb = get_the_stuff(type_of_stuff, hostname, prog, vers,
31820Sstevel@tonic-gate 				mfssnego, nconf, port, tinfo, fhp,
31830Sstevel@tonic-gate 				direct_to_server, fspath, cstatp);
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 			if (*cstatp == RPC_SUCCESS)
31860Sstevel@tonic-gate 				break;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 			assert(nb == NULL);
31890Sstevel@tonic-gate 
31900Sstevel@tonic-gate 		} /* end of while */
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 		if (nconf == NULL)
31930Sstevel@tonic-gate 			goto done;
31940Sstevel@tonic-gate 
31950Sstevel@tonic-gate 	} else {
31960Sstevel@tonic-gate retry:
31970Sstevel@tonic-gate 		while (nconf = getnetpath(nc)) {
31980Sstevel@tonic-gate 			if (nconf->nc_flag & NC_VISIBLE) {
31990Sstevel@tonic-gate 			    if (nthtry == FIRST_TRY) {
32000Sstevel@tonic-gate 				if ((nconf->nc_semantics == NC_TPI_COTS_ORD) ||
32010Sstevel@tonic-gate 					(nconf->nc_semantics == NC_TPI_COTS)) {
32020Sstevel@tonic-gate 				    if (port == 0)
32030Sstevel@tonic-gate 					break;
32040Sstevel@tonic-gate 				    if ((strcmp(nconf->nc_protofmly,
32050Sstevel@tonic-gate 					NC_INET) == 0 ||
32060Sstevel@tonic-gate 					strcmp(nconf->nc_protofmly,
32070Sstevel@tonic-gate 					NC_INET6) == 0) &&
32080Sstevel@tonic-gate 					(strcmp(nconf->nc_proto, NC_TCP) == 0))
32090Sstevel@tonic-gate 					break;
32100Sstevel@tonic-gate 				}
32110Sstevel@tonic-gate 			    }
32120Sstevel@tonic-gate 			    if (nthtry == SECOND_TRY) {
32130Sstevel@tonic-gate 				if (nconf->nc_semantics == NC_TPI_CLTS) {
32140Sstevel@tonic-gate 				    if (port == 0)
32150Sstevel@tonic-gate 					break;
32160Sstevel@tonic-gate 				    if ((strcmp(nconf->nc_protofmly,
32170Sstevel@tonic-gate 					NC_INET) == 0 ||
32180Sstevel@tonic-gate 					strcmp(nconf->nc_protofmly,
32190Sstevel@tonic-gate 					NC_INET6) == 0) &&
32200Sstevel@tonic-gate 					(strcmp(nconf->nc_proto, NC_UDP) == 0))
32210Sstevel@tonic-gate 					break;
32220Sstevel@tonic-gate 				}
32230Sstevel@tonic-gate 			    }
32240Sstevel@tonic-gate 			}
32250Sstevel@tonic-gate 		    } /* while */
32260Sstevel@tonic-gate 		    if (nconf == NULL) {
32270Sstevel@tonic-gate 			if (++nthtry <= MNT_PREF_LISTLEN) {
32280Sstevel@tonic-gate 				endnetpath(nc);
32290Sstevel@tonic-gate 				if ((nc = setnetpath()) == NULL)
32300Sstevel@tonic-gate 					goto done;
32310Sstevel@tonic-gate 				goto retry;
32320Sstevel@tonic-gate 			} else
32330Sstevel@tonic-gate 				goto done;
32340Sstevel@tonic-gate 		    } else {
32350Sstevel@tonic-gate 			nb = get_the_stuff(type_of_stuff, hostname, prog, vers,
32360Sstevel@tonic-gate 			    mfssnego, nconf, port, tinfo, fhp, direct_to_server,
32370Sstevel@tonic-gate 			    fspath, cstatp);
32380Sstevel@tonic-gate 			if (*cstatp != RPC_SUCCESS)
32390Sstevel@tonic-gate 				/*
32400Sstevel@tonic-gate 				 * Continue the same search path in the
32410Sstevel@tonic-gate 				 * netconfig db until no more matched nconf
32420Sstevel@tonic-gate 				 * (nconf == NULL).
32430Sstevel@tonic-gate 				 */
32440Sstevel@tonic-gate 				goto retry;
32450Sstevel@tonic-gate 		    }
32460Sstevel@tonic-gate 	} /* if !proto */
32470Sstevel@tonic-gate 
32480Sstevel@tonic-gate 	/*
32490Sstevel@tonic-gate 	 * Got nconf and nb.  Now dup the netconfig structure (nconf)
32500Sstevel@tonic-gate 	 * and return it thru nconfp.
32510Sstevel@tonic-gate 	 */
32520Sstevel@tonic-gate 	*nconfp = getnetconfigent(nconf->nc_netid);
32530Sstevel@tonic-gate 	if (*nconfp == NULL) {
32540Sstevel@tonic-gate 		syslog(LOG_ERR, "no memory\n");
32550Sstevel@tonic-gate 		free(nb);
32560Sstevel@tonic-gate 		nb = NULL;
32570Sstevel@tonic-gate 	}
32580Sstevel@tonic-gate done:
32590Sstevel@tonic-gate 	if (nc)
32600Sstevel@tonic-gate 		endnetpath(nc);
32610Sstevel@tonic-gate 	return (nb);
32620Sstevel@tonic-gate }
32630Sstevel@tonic-gate 
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate /*
32660Sstevel@tonic-gate  * Sends a null call to the remote host's (NFS program, versp). versp
32670Sstevel@tonic-gate  * may be "NULL" in which case the default maximum version is used.
32680Sstevel@tonic-gate  * Upon return, versp contains the maximum version supported iff versp!= NULL.
32690Sstevel@tonic-gate  */
32700Sstevel@tonic-gate enum clnt_stat
32710Sstevel@tonic-gate pingnfs(
32720Sstevel@tonic-gate 	char *hostpart,
32730Sstevel@tonic-gate 	int attempts,
32740Sstevel@tonic-gate 	rpcvers_t *versp,
32750Sstevel@tonic-gate 	rpcvers_t versmin,
32760Sstevel@tonic-gate 	ushort_t port,			/* may be zeor */
32770Sstevel@tonic-gate 	bool_t usepub,
32780Sstevel@tonic-gate 	char *path,
32790Sstevel@tonic-gate 	char *proto)
32800Sstevel@tonic-gate {
32810Sstevel@tonic-gate 	CLIENT *cl = NULL;
32820Sstevel@tonic-gate 	struct timeval rpc_to_new = {15, 0};
32830Sstevel@tonic-gate 	static struct timeval rpc_rtrans_new = {-1, -1};
32840Sstevel@tonic-gate 	enum clnt_stat clnt_stat;
32850Sstevel@tonic-gate 	int i, j;
32860Sstevel@tonic-gate 	rpcvers_t versmax;	/* maximum version to try against server */
32870Sstevel@tonic-gate 	rpcvers_t outvers;	/* version supported by host on last call */
32880Sstevel@tonic-gate 	rpcvers_t vers_to_try;	/* to try different versions against host */
32890Sstevel@tonic-gate 	char *hostname = hostpart;
32900Sstevel@tonic-gate 	struct netconfig *nconf;
32910Sstevel@tonic-gate 
32920Sstevel@tonic-gate 	if (path != NULL && strcmp(hostname, "nfs") == 0 &&
32930Sstevel@tonic-gate 	    strncmp(path, "//", 2) == 0) {
32940Sstevel@tonic-gate 		char *sport;
32950Sstevel@tonic-gate 
32960Sstevel@tonic-gate 		hostname = strdup(path+2);
32970Sstevel@tonic-gate 
32980Sstevel@tonic-gate 		if (hostname == NULL)
32990Sstevel@tonic-gate 			return (RPC_SYSTEMERROR);
33000Sstevel@tonic-gate 
33010Sstevel@tonic-gate 		path = strchr(hostname, '/');
33020Sstevel@tonic-gate 
33030Sstevel@tonic-gate 		/*
33040Sstevel@tonic-gate 		 * This cannot happen. If it does, give up
33050Sstevel@tonic-gate 		 * on the ping as this is obviously a corrupt
33060Sstevel@tonic-gate 		 * entry.
33070Sstevel@tonic-gate 		 */
33080Sstevel@tonic-gate 		if (path == NULL) {
33090Sstevel@tonic-gate 			free(hostname);
33100Sstevel@tonic-gate 			return (RPC_SUCCESS);
33110Sstevel@tonic-gate 		}
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 		/*
33140Sstevel@tonic-gate 		 * Probable end point of host string.
33150Sstevel@tonic-gate 		 */
33160Sstevel@tonic-gate 		*path = '\0';
33170Sstevel@tonic-gate 
33180Sstevel@tonic-gate 		sport = strchr(hostname, ':');
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 		if (sport != NULL && sport < path) {
33210Sstevel@tonic-gate 
33220Sstevel@tonic-gate 			/*
33230Sstevel@tonic-gate 			 * Actual end point of host string.
33240Sstevel@tonic-gate 			 */
33250Sstevel@tonic-gate 			*sport = '\0';
33260Sstevel@tonic-gate 			port = htons((ushort_t)atoi(sport+1));
33270Sstevel@tonic-gate 		}
33280Sstevel@tonic-gate 
33290Sstevel@tonic-gate 		usepub = TRUE;
33300Sstevel@tonic-gate 	}
33310Sstevel@tonic-gate 
33320Sstevel@tonic-gate 	/* Pick up the default versions and then set them appropriately */
33330Sstevel@tonic-gate 	if (versp) {
33340Sstevel@tonic-gate 		versmax = *versp;
33350Sstevel@tonic-gate 		/* use versmin passed in */
33360Sstevel@tonic-gate 	} else {
33370Sstevel@tonic-gate 		read_default_nfs();
33380Sstevel@tonic-gate 		set_versrange(0, &versmax, &versmin);
33390Sstevel@tonic-gate 	}
33400Sstevel@tonic-gate 
33410Sstevel@tonic-gate 	if (proto &&
33420Sstevel@tonic-gate 	    strncasecmp(proto, NC_UDP, strlen(NC_UDP)) == 0 &&
33430Sstevel@tonic-gate 	    versmax == NFS_V4) {
33440Sstevel@tonic-gate 		if (versmin == NFS_V4) {
33450Sstevel@tonic-gate 			if (versp) {
33460Sstevel@tonic-gate 				*versp = versmax - 1;
33470Sstevel@tonic-gate 				return (RPC_SUCCESS);
33480Sstevel@tonic-gate 			}
33490Sstevel@tonic-gate 			return (RPC_PROGUNAVAIL);
33500Sstevel@tonic-gate 		} else {
33510Sstevel@tonic-gate 			versmax--;
33520Sstevel@tonic-gate 		}
33530Sstevel@tonic-gate 	}
33540Sstevel@tonic-gate 
33550Sstevel@tonic-gate 	if (versp)
33560Sstevel@tonic-gate 		*versp = versmax;
33570Sstevel@tonic-gate 
33580Sstevel@tonic-gate 	switch (cache_check(hostname, versp, proto)) {
33590Sstevel@tonic-gate 	case GOODHOST:
33600Sstevel@tonic-gate 		if (hostname != hostpart)
33610Sstevel@tonic-gate 			free(hostname);
33620Sstevel@tonic-gate 		return (RPC_SUCCESS);
33630Sstevel@tonic-gate 	case DEADHOST:
33640Sstevel@tonic-gate 		if (hostname != hostpart)
33650Sstevel@tonic-gate 			free(hostname);
33660Sstevel@tonic-gate 		return (RPC_TIMEDOUT);
33670Sstevel@tonic-gate 	case NOHOST:
33680Sstevel@tonic-gate 	default:
33690Sstevel@tonic-gate 		break;
33700Sstevel@tonic-gate 	}
33710Sstevel@tonic-gate 
33720Sstevel@tonic-gate 	/*
33730Sstevel@tonic-gate 	 * XXX The retransmission time rpcbrmttime is a global defined
33740Sstevel@tonic-gate 	 * in the rpc library (rpcb_clnt.c). We use (and like) the default
33750Sstevel@tonic-gate 	 * value of 15 sec in the rpc library. The code below is to protect
33760Sstevel@tonic-gate 	 * us in case it changes. This need not be done under a lock since
33770Sstevel@tonic-gate 	 * any # of threads entering this function will get the same
33780Sstevel@tonic-gate 	 * retransmission value.
33790Sstevel@tonic-gate 	 */
33800Sstevel@tonic-gate 	if (rpc_rtrans_new.tv_sec == -1 && rpc_rtrans_new.tv_usec == -1) {
33810Sstevel@tonic-gate 		__rpc_control(CLCR_GET_RPCB_RMTTIME, (char *)&rpc_rtrans_new);
33820Sstevel@tonic-gate 		if (rpc_rtrans_new.tv_sec != 15 && rpc_rtrans_new.tv_sec != 0)
33830Sstevel@tonic-gate 			if (trace > 1)
33840Sstevel@tonic-gate 				trace_prt(1, "RPC library rttimer changed\n");
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate 
33870Sstevel@tonic-gate 	/*
33880Sstevel@tonic-gate 	 * XXX Manipulate the total timeout to get the number of
33890Sstevel@tonic-gate 	 * desired retransmissions. This code is heavily dependant on
33900Sstevel@tonic-gate 	 * the RPC backoff mechanism in clnt_dg_call (clnt_dg.c).
33910Sstevel@tonic-gate 	 */
33920Sstevel@tonic-gate 	for (i = 0, j = rpc_rtrans_new.tv_sec; i < attempts-1; i++) {
33930Sstevel@tonic-gate 		if (j < RPC_MAX_BACKOFF)
33940Sstevel@tonic-gate 			j *= 2;
33950Sstevel@tonic-gate 		else
33960Sstevel@tonic-gate 			j = RPC_MAX_BACKOFF;
33970Sstevel@tonic-gate 		rpc_to_new.tv_sec += j;
33980Sstevel@tonic-gate 	}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 	vers_to_try = versmax;
34010Sstevel@tonic-gate 
34020Sstevel@tonic-gate 	/*
34030Sstevel@tonic-gate 	 * check the host's version within the timeout
34040Sstevel@tonic-gate 	 */
34050Sstevel@tonic-gate 	if (trace > 1)
34060Sstevel@tonic-gate 		trace_prt(1, "	ping: %s timeout=%ld request vers=%d min=%d\n",
34070Sstevel@tonic-gate 				hostname, rpc_to_new.tv_sec, versmax, versmin);
34080Sstevel@tonic-gate 
34090Sstevel@tonic-gate 	if (usepub == FALSE) {
34100Sstevel@tonic-gate 	    do {
34110Sstevel@tonic-gate 		/*
34120Sstevel@tonic-gate 		 * If NFSv4, then we do the same thing as is used
34130Sstevel@tonic-gate 		 * for public filehandles so that we avoid rpcbind
34140Sstevel@tonic-gate 		 */
34150Sstevel@tonic-gate 		if (vers_to_try == NFS_V4) {
34160Sstevel@tonic-gate 			if (trace > 4) {
34170Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Trying ping via "
34180Sstevel@tonic-gate 					"\"circuit_v\"\n");
34190Sstevel@tonic-gate 				}
34200Sstevel@tonic-gate 
34210Sstevel@tonic-gate 			if ((cl = clnt_create_service_timed(hostname, "nfs",
34220Sstevel@tonic-gate 							    NFS_PROGRAM,
34230Sstevel@tonic-gate 							    vers_to_try,
34240Sstevel@tonic-gate 							    port, "circuit_v",
34250Sstevel@tonic-gate 							    &rpc_to_new))
34260Sstevel@tonic-gate 			    != NULL) {
34270Sstevel@tonic-gate 				outvers = vers_to_try;
34280Sstevel@tonic-gate 				break;
34290Sstevel@tonic-gate 			}
34300Sstevel@tonic-gate 			if (trace > 4) {
34310Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Can't ping via "
34320Sstevel@tonic-gate 					"\"circuit_v\" %s: RPC error=%d\n",
34330Sstevel@tonic-gate 					hostname, rpc_createerr.cf_stat);
34340Sstevel@tonic-gate 			}
34350Sstevel@tonic-gate 
34360Sstevel@tonic-gate 		} else {
34370Sstevel@tonic-gate 			if ((cl = clnt_create_vers_timed(hostname, NFS_PROGRAM,
34380Sstevel@tonic-gate 				&outvers, versmin, vers_to_try,
34390Sstevel@tonic-gate 				"datagram_v", &rpc_to_new))
34400Sstevel@tonic-gate 				!= NULL)
34410Sstevel@tonic-gate 				break;
34420Sstevel@tonic-gate 			if (trace > 4) {
34430Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Can't ping via "
34440Sstevel@tonic-gate 					"\"datagram_v\"%s: RPC error=%d\n",
34450Sstevel@tonic-gate 					hostname, rpc_createerr.cf_stat);
34460Sstevel@tonic-gate 			}
34470Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST ||
34480Sstevel@tonic-gate 				rpc_createerr.cf_stat == RPC_TIMEDOUT)
34490Sstevel@tonic-gate 				break;
34500Sstevel@tonic-gate 			if (rpc_createerr.cf_stat == RPC_PROGNOTREGISTERED) {
34510Sstevel@tonic-gate 				if (trace > 4) {
34520Sstevel@tonic-gate 					trace_prt(1, "  pingnfs: Trying ping "
34530Sstevel@tonic-gate 						"via \"circuit_v\"\n");
34540Sstevel@tonic-gate 				}
34550Sstevel@tonic-gate 				if ((cl = clnt_create_vers_timed(hostname,
34560Sstevel@tonic-gate 					NFS_PROGRAM, &outvers,
34570Sstevel@tonic-gate 					versmin, vers_to_try,
34580Sstevel@tonic-gate 					"circuit_v", &rpc_to_new)) != NULL)
34590Sstevel@tonic-gate 					break;
34600Sstevel@tonic-gate 				if (trace > 4) {
34610Sstevel@tonic-gate 					trace_prt(1, "  pingnfs: Can't ping "
34620Sstevel@tonic-gate 						"via \"circuit_v\" %s: "
34630Sstevel@tonic-gate 						"RPC error=%d\n",
34640Sstevel@tonic-gate 						hostname,
34650Sstevel@tonic-gate 						rpc_createerr.cf_stat);
34660Sstevel@tonic-gate 				}
34670Sstevel@tonic-gate 			}
34680Sstevel@tonic-gate 		}
34690Sstevel@tonic-gate 
34700Sstevel@tonic-gate 		/*
34710Sstevel@tonic-gate 		 * backoff and return lower version to retry the ping.
34720Sstevel@tonic-gate 		 * XXX we should be more careful and handle
34730Sstevel@tonic-gate 		 * RPC_PROGVERSMISMATCH here, because that error is handled
34740Sstevel@tonic-gate 		 * in clnt_create_vers(). It's not done to stay in sync
34750Sstevel@tonic-gate 		 * with the nfs mount command.
34760Sstevel@tonic-gate 		 */
34770Sstevel@tonic-gate 		    vers_to_try--;
34780Sstevel@tonic-gate 		    if (vers_to_try < versmin)
34790Sstevel@tonic-gate 			    break;
34800Sstevel@tonic-gate 		    if (versp != NULL) {	/* recheck the cache */
34810Sstevel@tonic-gate 			    *versp = vers_to_try;
34820Sstevel@tonic-gate 			    if (trace > 4) {
34830Sstevel@tonic-gate 				trace_prt(1,
34840Sstevel@tonic-gate 				    "  pingnfs: check cache: vers=%d\n",
34850Sstevel@tonic-gate 				    *versp);
34860Sstevel@tonic-gate 			    }
34870Sstevel@tonic-gate 			    switch (cache_check(hostname, versp, proto)) {
34880Sstevel@tonic-gate 			    case GOODHOST:
34890Sstevel@tonic-gate 				    if (hostname != hostpart)
34900Sstevel@tonic-gate 					    free(hostname);
34910Sstevel@tonic-gate 				    return (RPC_SUCCESS);
34920Sstevel@tonic-gate 			    case DEADHOST:
34930Sstevel@tonic-gate 				    if (hostname != hostpart)
34940Sstevel@tonic-gate 					    free(hostname);
34950Sstevel@tonic-gate 				    return (RPC_TIMEDOUT);
34960Sstevel@tonic-gate 			    case NOHOST:
34970Sstevel@tonic-gate 			    default:
34980Sstevel@tonic-gate 				    break;
34990Sstevel@tonic-gate 			    }
35000Sstevel@tonic-gate 		    }
35010Sstevel@tonic-gate 		    if (trace > 4) {
35020Sstevel@tonic-gate 			trace_prt(1, "  pingnfs: Try version=%d\n",
35030Sstevel@tonic-gate 				vers_to_try);
35040Sstevel@tonic-gate 		    }
35050Sstevel@tonic-gate 	    } while (cl == NULL);
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 
35080Sstevel@tonic-gate 	    if (cl == NULL) {
35090Sstevel@tonic-gate 		    if (verbose)
35100Sstevel@tonic-gate 			    syslog(LOG_ERR, "pingnfs: %s%s",
35110Sstevel@tonic-gate 				    hostname, clnt_spcreateerror(""));
35120Sstevel@tonic-gate 		    clnt_stat = rpc_createerr.cf_stat;
35130Sstevel@tonic-gate 	    } else {
35140Sstevel@tonic-gate 		    clnt_destroy(cl);
35150Sstevel@tonic-gate 		    clnt_stat = RPC_SUCCESS;
35160Sstevel@tonic-gate 	    }
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	} else {
35190Sstevel@tonic-gate 		for (vers_to_try = versmax; vers_to_try >= versmin;
35200Sstevel@tonic-gate 		    vers_to_try--) {
35210Sstevel@tonic-gate 
35220Sstevel@tonic-gate 			nconf = NULL;
35230Sstevel@tonic-gate 
35240Sstevel@tonic-gate 			if (trace > 4) {
35250Sstevel@tonic-gate 				trace_prt(1, "  pingnfs: Try version=%d "
35260Sstevel@tonic-gate 					"using get_ping()\n", vers_to_try);
35270Sstevel@tonic-gate 			}
35280Sstevel@tonic-gate 
35290Sstevel@tonic-gate 			clnt_stat = get_ping(hostname, NFS_PROGRAM,
35300Sstevel@tonic-gate 				vers_to_try, &nconf, port, TRUE);
35310Sstevel@tonic-gate 
35320Sstevel@tonic-gate 			if (nconf != NULL)
35330Sstevel@tonic-gate 				freenetconfigent(nconf);
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 			if (clnt_stat == RPC_SUCCESS) {
35360Sstevel@tonic-gate 				outvers = vers_to_try;
35370Sstevel@tonic-gate 				break;
35380Sstevel@tonic-gate 			}
35390Sstevel@tonic-gate 		}
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 
35420Sstevel@tonic-gate 	if (trace > 1)
35430Sstevel@tonic-gate 		clnt_stat == RPC_SUCCESS ?
35440Sstevel@tonic-gate 			trace_prt(1, "	pingnfs OK: nfs version=%d\n", outvers):
35450Sstevel@tonic-gate 			trace_prt(1, "	pingnfs FAIL: can't get nfs version\n");
35460Sstevel@tonic-gate 
35470Sstevel@tonic-gate 	if (clnt_stat == RPC_SUCCESS) {
35480Sstevel@tonic-gate 		cache_enter(hostname, versmax, outvers, proto, GOODHOST);
35490Sstevel@tonic-gate 		if (versp != NULL)
35500Sstevel@tonic-gate 			*versp = outvers;
35510Sstevel@tonic-gate 	} else
35520Sstevel@tonic-gate 		cache_enter(hostname, versmax, versmax, proto, DEADHOST);
35530Sstevel@tonic-gate 
35540Sstevel@tonic-gate 	if (hostpart != hostname)
35550Sstevel@tonic-gate 		free(hostname);
35560Sstevel@tonic-gate 
35570Sstevel@tonic-gate 	return (clnt_stat);
35580Sstevel@tonic-gate }
35590Sstevel@tonic-gate 
35600Sstevel@tonic-gate #define	MNTTYPE_LOFS	"lofs"
35610Sstevel@tonic-gate 
35620Sstevel@tonic-gate int
35630Sstevel@tonic-gate loopbackmount(fsname, dir, mntopts, overlay)
35640Sstevel@tonic-gate 	char *fsname;		/* Directory being mounted */
35650Sstevel@tonic-gate 	char *dir;		/* Directory being mounted on */
35660Sstevel@tonic-gate 	char *mntopts;
35670Sstevel@tonic-gate 	int overlay;
35680Sstevel@tonic-gate {
35690Sstevel@tonic-gate 	struct mnttab mnt;
35700Sstevel@tonic-gate 	int flags = 0;
35710Sstevel@tonic-gate 	char fstype[] = MNTTYPE_LOFS;
35720Sstevel@tonic-gate 	int dirlen;
35730Sstevel@tonic-gate 	struct stat st;
35740Sstevel@tonic-gate 	char optbuf[MAX_MNTOPT_STR];
35750Sstevel@tonic-gate 
35760Sstevel@tonic-gate 	dirlen = strlen(dir);
35770Sstevel@tonic-gate 	if (dir[dirlen-1] == ' ')
35780Sstevel@tonic-gate 		dirlen--;
35790Sstevel@tonic-gate 
35800Sstevel@tonic-gate 	if (dirlen == strlen(fsname) &&
35810Sstevel@tonic-gate 		strncmp(fsname, dir, dirlen) == 0) {
35820Sstevel@tonic-gate 		syslog(LOG_ERR,
35830Sstevel@tonic-gate 			"Mount of %s on %s would result in deadlock, aborted\n",
35840Sstevel@tonic-gate 			fsname, dir);
35850Sstevel@tonic-gate 		return (RET_ERR);
35860Sstevel@tonic-gate 	}
35870Sstevel@tonic-gate 	mnt.mnt_mntopts = mntopts;
35880Sstevel@tonic-gate 	if (hasmntopt(&mnt, MNTOPT_RO) != NULL)
35890Sstevel@tonic-gate 		flags |= MS_RDONLY;
35900Sstevel@tonic-gate 
35910Sstevel@tonic-gate 	(void) strlcpy(optbuf, mntopts, sizeof (optbuf));
35920Sstevel@tonic-gate 
35930Sstevel@tonic-gate 	if (overlay)
35940Sstevel@tonic-gate 		flags |= MS_OVERLAY;
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	if (trace > 1)
35970Sstevel@tonic-gate 		trace_prt(1,
35980Sstevel@tonic-gate 			"  loopbackmount: fsname=%s, dir=%s, flags=%d\n",
35990Sstevel@tonic-gate 			fsname, dir, flags);
36000Sstevel@tonic-gate 
36010Sstevel@tonic-gate 	if (mount(fsname, dir, flags | MS_DATA | MS_OPTIONSTR, fstype,
36020Sstevel@tonic-gate 	    NULL, 0, optbuf, sizeof (optbuf)) < 0) {
36030Sstevel@tonic-gate 		syslog(LOG_ERR, "Mount of %s on %s: %m", fsname, dir);
36040Sstevel@tonic-gate 		return (RET_ERR);
36050Sstevel@tonic-gate 	}
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 	if (stat(dir, &st) == 0) {
36080Sstevel@tonic-gate 		if (trace > 1) {
36090Sstevel@tonic-gate 			trace_prt(1,
36100Sstevel@tonic-gate 			    "  loopbackmount of %s on %s dev=%x rdev=%x OK\n",
36110Sstevel@tonic-gate 			    fsname, dir, st.st_dev, st.st_rdev);
36120Sstevel@tonic-gate 		}
36130Sstevel@tonic-gate 	} else {
36140Sstevel@tonic-gate 		if (trace > 1) {
36150Sstevel@tonic-gate 			trace_prt(1,
36160Sstevel@tonic-gate 			    "  loopbackmount of %s on %s OK\n", fsname, dir);
36170Sstevel@tonic-gate 			trace_prt(1, "	stat of %s failed\n", dir);
36180Sstevel@tonic-gate 		}
36190Sstevel@tonic-gate 	}
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 	return (0);
36220Sstevel@tonic-gate }
36230Sstevel@tonic-gate 
36240Sstevel@tonic-gate /*
36250Sstevel@tonic-gate  * Look for the value of a numeric option of the form foo=x.  If found, set
36260Sstevel@tonic-gate  * *valp to the value and return non-zero.  If not found or the option is
36270Sstevel@tonic-gate  * malformed, return zero.
36280Sstevel@tonic-gate  */
36290Sstevel@tonic-gate 
36300Sstevel@tonic-gate int
36310Sstevel@tonic-gate nopt(mnt, opt, valp)
36320Sstevel@tonic-gate 	struct mnttab *mnt;
36330Sstevel@tonic-gate 	char *opt;
36340Sstevel@tonic-gate 	int *valp;			/* OUT */
36350Sstevel@tonic-gate {
36360Sstevel@tonic-gate 	char *equal;
36370Sstevel@tonic-gate 	char *str;
36380Sstevel@tonic-gate 
36390Sstevel@tonic-gate 	/*
36400Sstevel@tonic-gate 	 * We should never get a null pointer, but if we do, it's better to
36410Sstevel@tonic-gate 	 * ignore the option than to dump core.
36420Sstevel@tonic-gate 	 */
36430Sstevel@tonic-gate 
36440Sstevel@tonic-gate 	if (valp == NULL) {
36450Sstevel@tonic-gate 		syslog(LOG_DEBUG, "null pointer for %s option", opt);
36460Sstevel@tonic-gate 		return (0);
36470Sstevel@tonic-gate 	}
36480Sstevel@tonic-gate 
36490Sstevel@tonic-gate 	if (str = hasmntopt(mnt, opt)) {
36500Sstevel@tonic-gate 		if (equal = strchr(str, '=')) {
36510Sstevel@tonic-gate 			*valp = atoi(&equal[1]);
36520Sstevel@tonic-gate 			return (1);
36530Sstevel@tonic-gate 		} else {
36540Sstevel@tonic-gate 			syslog(LOG_ERR, "Bad numeric option '%s'", str);
36550Sstevel@tonic-gate 		}
36560Sstevel@tonic-gate 	}
36570Sstevel@tonic-gate 	return (0);
36580Sstevel@tonic-gate }
36590Sstevel@tonic-gate 
3660*249Sjwahlig int
36610Sstevel@tonic-gate nfsunmount(mnt)
36620Sstevel@tonic-gate 	struct mnttab *mnt;
36630Sstevel@tonic-gate {
36640Sstevel@tonic-gate 	struct timeval timeout;
36650Sstevel@tonic-gate 	CLIENT *cl;
36660Sstevel@tonic-gate 	enum clnt_stat rpc_stat;
36670Sstevel@tonic-gate 	char *host, *path;
36680Sstevel@tonic-gate 	struct replica *list;
36690Sstevel@tonic-gate 	int i, count = 0;
36700Sstevel@tonic-gate 	int isv4mount = is_v4_mount(mnt->mnt_mountp);
36710Sstevel@tonic-gate 
36720Sstevel@tonic-gate 	if (trace > 1)
36730Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s\n", mnt->mnt_mountp);
36740Sstevel@tonic-gate 
36750Sstevel@tonic-gate 	if (umount(mnt->mnt_mountp) < 0) {
36760Sstevel@tonic-gate 		if (trace > 1)
36770Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s FAILED\n",
36780Sstevel@tonic-gate 				mnt->mnt_mountp);
36790Sstevel@tonic-gate 		if (errno)
36800Sstevel@tonic-gate 			return (errno);
36810Sstevel@tonic-gate 	}
36820Sstevel@tonic-gate 
36830Sstevel@tonic-gate 	/*
36840Sstevel@tonic-gate 	 * If this is a NFSv4 mount, the mount protocol was not used
36850Sstevel@tonic-gate 	 * so we just return.
36860Sstevel@tonic-gate 	 */
36870Sstevel@tonic-gate 	if (isv4mount) {
36880Sstevel@tonic-gate 		if (trace > 1)
36890Sstevel@tonic-gate 			trace_prt(1, "	nfsunmount: umount %s OK\n",
36900Sstevel@tonic-gate 				mnt->mnt_mountp);
36910Sstevel@tonic-gate 		return (0);
36920Sstevel@tonic-gate 	}
36930Sstevel@tonic-gate 
36940Sstevel@tonic-gate 	/*
36950Sstevel@tonic-gate 	 * If mounted with -o public, then no need to contact server
36960Sstevel@tonic-gate 	 * because mount protocol was not used.
36970Sstevel@tonic-gate 	 */
36980Sstevel@tonic-gate 	if (hasmntopt(mnt, MNTOPT_PUBLIC) != NULL) {
36990Sstevel@tonic-gate 		return (0);
37000Sstevel@tonic-gate 	}
37010Sstevel@tonic-gate 
37020Sstevel@tonic-gate 	/*
37030Sstevel@tonic-gate 	 * The rest of this code is advisory to the server.
37040Sstevel@tonic-gate 	 * If it fails return success anyway.
37050Sstevel@tonic-gate 	 */
37060Sstevel@tonic-gate 
37070Sstevel@tonic-gate 	list = parse_replica(mnt->mnt_special, &count);
37080Sstevel@tonic-gate 	if (!list) {
37090Sstevel@tonic-gate 		if (count >= 0)
37100Sstevel@tonic-gate 			syslog(LOG_ERR,
37110Sstevel@tonic-gate 			    "Memory allocation failed: %m");
37120Sstevel@tonic-gate 		return (ENOMEM);
37130Sstevel@tonic-gate 	}
37140Sstevel@tonic-gate 
37150Sstevel@tonic-gate 	for (i = 0; i < count; i++) {
37160Sstevel@tonic-gate 
37170Sstevel@tonic-gate 		host = list[i].host;
37180Sstevel@tonic-gate 		path = list[i].path;
37190Sstevel@tonic-gate 
37200Sstevel@tonic-gate 		/*
37210Sstevel@tonic-gate 		 * Skip file systems mounted using WebNFS, because mount
37220Sstevel@tonic-gate 		 * protocol was not used.
37230Sstevel@tonic-gate 		 */
37240Sstevel@tonic-gate 		if (strcmp(host, "nfs") == 0 && strncmp(path, "//", 2) == 0)
37250Sstevel@tonic-gate 			continue;
37260Sstevel@tonic-gate 
37270Sstevel@tonic-gate 		cl = clnt_create(host, MOUNTPROG, MOUNTVERS, "datagram_v");
37280Sstevel@tonic-gate 		if (cl == NULL)
37290Sstevel@tonic-gate 			break;
37300Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37310Sstevel@tonic-gate 		add_alloc("CLNT_HANDLE", cl, 0, __FILE__, __LINE__);
37320Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0,
37330Sstevel@tonic-gate 			__FILE__, __LINE__);
37340Sstevel@tonic-gate #endif
37350Sstevel@tonic-gate 		if (__clnt_bindresvport(cl) < 0) {
37360Sstevel@tonic-gate 			if (verbose)
37370Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37380Sstevel@tonic-gate 					host, path,
37390Sstevel@tonic-gate 					"Couldn't bind to reserved port");
37400Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37410Sstevel@tonic-gate 			continue;
37420Sstevel@tonic-gate 		}
37430Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37440Sstevel@tonic-gate 		drop_alloc("AUTH_HANDLE", cl->cl_auth, __FILE__, __LINE__);
37450Sstevel@tonic-gate #endif
37460Sstevel@tonic-gate 		AUTH_DESTROY(cl->cl_auth);
37470Sstevel@tonic-gate 		if ((cl->cl_auth = authsys_create_default()) == NULL) {
37480Sstevel@tonic-gate 			if (verbose)
37490Sstevel@tonic-gate 				syslog(LOG_ERR, "umount %s:%s: %s",
37500Sstevel@tonic-gate 					host, path,
37510Sstevel@tonic-gate 					"Failed creating default auth handle");
37520Sstevel@tonic-gate 			destroy_auth_client_handle(cl);
37530Sstevel@tonic-gate 			continue;
37540Sstevel@tonic-gate 		}
37550Sstevel@tonic-gate #ifdef MALLOC_DEBUG
37560Sstevel@tonic-gate 		add_alloc("AUTH_HANDLE", cl->cl_auth, 0, __FILE__, __LINE__);
37570Sstevel@tonic-gate #endif
37580Sstevel@tonic-gate 		timeout.tv_usec = 0;
37590Sstevel@tonic-gate 		timeout.tv_sec = 5;
37600Sstevel@tonic-gate 		rpc_stat = clnt_call(cl, MOUNTPROC_UMNT, xdr_dirpath,
37610Sstevel@tonic-gate 			    (caddr_t)&path, xdr_void, (char *)NULL, timeout);
37620Sstevel@tonic-gate 		if (verbose && rpc_stat != RPC_SUCCESS)
37630Sstevel@tonic-gate 			syslog(LOG_ERR, "%s: %s",
37640Sstevel@tonic-gate 				host, clnt_sperror(cl, "unmount"));
37650Sstevel@tonic-gate 		destroy_auth_client_handle(cl);
37660Sstevel@tonic-gate 	}
37670Sstevel@tonic-gate 
37680Sstevel@tonic-gate 	free_replica(list, count);
37690Sstevel@tonic-gate 
37700Sstevel@tonic-gate 	if (trace > 1)
37710Sstevel@tonic-gate 		trace_prt(1, "	nfsunmount: umount %s OK\n", mnt->mnt_mountp);
37720Sstevel@tonic-gate 
37730Sstevel@tonic-gate done:
37740Sstevel@tonic-gate 	return (0);
37750Sstevel@tonic-gate }
37760Sstevel@tonic-gate 
37770Sstevel@tonic-gate /*
37780Sstevel@tonic-gate  * Put a new entry in the cache chain by prepending it to the front.
37790Sstevel@tonic-gate  * If there isn't enough memory then just give up.
37800Sstevel@tonic-gate  */
37810Sstevel@tonic-gate static void
37820Sstevel@tonic-gate cache_enter(host, reqvers, outvers, proto, state)
37830Sstevel@tonic-gate 	char *host;
37840Sstevel@tonic-gate 	rpcvers_t reqvers;
37850Sstevel@tonic-gate 	rpcvers_t outvers;
37860Sstevel@tonic-gate 	char *proto;
37870Sstevel@tonic-gate 	int state;
37880Sstevel@tonic-gate {
37890Sstevel@tonic-gate 	struct cache_entry *entry;
37900Sstevel@tonic-gate 	int cache_time = 30;	/* sec */
37910Sstevel@tonic-gate 
37920Sstevel@tonic-gate 	timenow = time(NULL);
37930Sstevel@tonic-gate 
37940Sstevel@tonic-gate 	entry = (struct cache_entry *)malloc(sizeof (struct cache_entry));
37950Sstevel@tonic-gate 	if (entry == NULL)
37960Sstevel@tonic-gate 		return;
37970Sstevel@tonic-gate 	(void) memset((caddr_t)entry, 0, sizeof (struct cache_entry));
37980Sstevel@tonic-gate 	entry->cache_host = strdup(host);
37990Sstevel@tonic-gate 	if (entry->cache_host == NULL) {
38000Sstevel@tonic-gate 		cache_free(entry);
38010Sstevel@tonic-gate 		return;
38020Sstevel@tonic-gate 	}
38030Sstevel@tonic-gate 	entry->cache_reqvers = reqvers;
38040Sstevel@tonic-gate 	entry->cache_outvers = outvers;
38050Sstevel@tonic-gate 	entry->cache_proto = (proto == NULL ? NULL : strdup(proto));
38060Sstevel@tonic-gate 	entry->cache_state = state;
38070Sstevel@tonic-gate 	entry->cache_time = timenow + cache_time;
38080Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
38090Sstevel@tonic-gate #ifdef CACHE_DEBUG
38100Sstevel@tonic-gate 	host_cache_accesses++;		/* up host cache access counter */
38110Sstevel@tonic-gate #endif /* CACHE DEBUG */
38120Sstevel@tonic-gate 	entry->cache_next = cache_head;
38130Sstevel@tonic-gate 	cache_head = entry;
38140Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38150Sstevel@tonic-gate }
38160Sstevel@tonic-gate 
38170Sstevel@tonic-gate static int
38180Sstevel@tonic-gate cache_check(host, versp, proto)
38190Sstevel@tonic-gate 	char *host;
38200Sstevel@tonic-gate 	rpcvers_t *versp;
38210Sstevel@tonic-gate 	char *proto;
38220Sstevel@tonic-gate {
38230Sstevel@tonic-gate 	int state = NOHOST;
38240Sstevel@tonic-gate 	struct cache_entry *ce, *prev;
38250Sstevel@tonic-gate 
38260Sstevel@tonic-gate 	timenow = time(NULL);
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	(void) rw_rdlock(&cache_lock);
38290Sstevel@tonic-gate 
38300Sstevel@tonic-gate #ifdef CACHE_DEBUG
38310Sstevel@tonic-gate 	/* Increment the lookup and access counters for the host cache */
38320Sstevel@tonic-gate 	host_cache_accesses++;
38330Sstevel@tonic-gate 	host_cache_lookups++;
38340Sstevel@tonic-gate 	if ((host_cache_lookups%1000) == 0)
38350Sstevel@tonic-gate 		trace_host_cache();
38360Sstevel@tonic-gate #endif /* CACHE DEBUG */
38370Sstevel@tonic-gate 
38380Sstevel@tonic-gate 	for (ce = cache_head; ce; ce = ce->cache_next) {
38390Sstevel@tonic-gate 		if (timenow > ce->cache_time) {
38400Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38410Sstevel@tonic-gate 			(void) rw_wrlock(&cache_lock);
38420Sstevel@tonic-gate 			for (prev = NULL, ce = cache_head; ce;
38430Sstevel@tonic-gate 				prev = ce, ce = ce->cache_next) {
38440Sstevel@tonic-gate 				if (timenow > ce->cache_time) {
38450Sstevel@tonic-gate 					cache_free(ce);
38460Sstevel@tonic-gate 					if (prev)
38470Sstevel@tonic-gate 						prev->cache_next = NULL;
38480Sstevel@tonic-gate 					else
38490Sstevel@tonic-gate 						cache_head = NULL;
38500Sstevel@tonic-gate 					break;
38510Sstevel@tonic-gate 				}
38520Sstevel@tonic-gate 			}
38530Sstevel@tonic-gate 			(void) rw_unlock(&cache_lock);
38540Sstevel@tonic-gate 			return (state);
38550Sstevel@tonic-gate 		}
38560Sstevel@tonic-gate 		if (strcmp(host, ce->cache_host) != 0)
38570Sstevel@tonic-gate 			continue;
38580Sstevel@tonic-gate 		if ((proto == NULL && ce->cache_proto != NULL) ||
38590Sstevel@tonic-gate 		    (proto != NULL && ce->cache_proto == NULL))
38600Sstevel@tonic-gate 			continue;
38610Sstevel@tonic-gate 		if (proto != NULL &&
38620Sstevel@tonic-gate 		    strcmp(proto, ce->cache_proto) != 0)
38630Sstevel@tonic-gate 			continue;
38640Sstevel@tonic-gate 
38650Sstevel@tonic-gate 		if (versp == NULL ||
38660Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_reqvers) ||
38670Sstevel@tonic-gate 			(versp != NULL && *versp == ce->cache_outvers)) {
38680Sstevel@tonic-gate 				if (versp != NULL)
38690Sstevel@tonic-gate 					*versp = ce->cache_outvers;
38700Sstevel@tonic-gate 				state = ce->cache_state;
38710Sstevel@tonic-gate 
38720Sstevel@tonic-gate 				/* increment the host cache hit counters */
38730Sstevel@tonic-gate #ifdef CACHE_DEBUG
38740Sstevel@tonic-gate 				if (state == GOODHOST)
38750Sstevel@tonic-gate 					goodhost_cache_hits++;
38760Sstevel@tonic-gate 				if (state == DEADHOST)
38770Sstevel@tonic-gate 					deadhost_cache_hits++;
38780Sstevel@tonic-gate #endif /* CACHE_DEBUG */
38790Sstevel@tonic-gate 				(void) rw_unlock(&cache_lock);
38800Sstevel@tonic-gate 				return (state);
38810Sstevel@tonic-gate 		}
38820Sstevel@tonic-gate 	}
38830Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
38840Sstevel@tonic-gate 	return (state);
38850Sstevel@tonic-gate }
38860Sstevel@tonic-gate 
38870Sstevel@tonic-gate /*
38880Sstevel@tonic-gate  * Free a cache entry and all entries
38890Sstevel@tonic-gate  * further down the chain since they
38900Sstevel@tonic-gate  * will also be expired.
38910Sstevel@tonic-gate  */
38920Sstevel@tonic-gate static void
38930Sstevel@tonic-gate cache_free(entry)
38940Sstevel@tonic-gate 	struct cache_entry *entry;
38950Sstevel@tonic-gate {
38960Sstevel@tonic-gate 	struct cache_entry *ce, *next = NULL;
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 	for (ce = entry; ce; ce = next) {
38990Sstevel@tonic-gate 		if (ce->cache_host)
39000Sstevel@tonic-gate 			free(ce->cache_host);
39010Sstevel@tonic-gate 		if (ce->cache_proto)
39020Sstevel@tonic-gate 			free(ce->cache_proto);
39030Sstevel@tonic-gate 		next = ce->cache_next;
39040Sstevel@tonic-gate 		free(ce);
39050Sstevel@tonic-gate 	}
39060Sstevel@tonic-gate }
39070Sstevel@tonic-gate 
39080Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39090Sstevel@tonic-gate void
39100Sstevel@tonic-gate cache_flush()
39110Sstevel@tonic-gate {
39120Sstevel@tonic-gate 	(void) rw_wrlock(&cache_lock);
39130Sstevel@tonic-gate 	cache_free(cache_head);
39140Sstevel@tonic-gate 	cache_head = NULL;
39150Sstevel@tonic-gate 	(void) rw_unlock(&cache_lock);
39160Sstevel@tonic-gate }
39170Sstevel@tonic-gate 
39180Sstevel@tonic-gate void
39190Sstevel@tonic-gate flush_caches()
39200Sstevel@tonic-gate {
39210Sstevel@tonic-gate 	mutex_lock(&cleanup_lock);
39220Sstevel@tonic-gate 	cond_signal(&cleanup_start_cv);
39230Sstevel@tonic-gate 	(void) cond_wait(&cleanup_done_cv, &cleanup_lock);
39240Sstevel@tonic-gate 	mutex_unlock(&cleanup_lock);
39250Sstevel@tonic-gate 	cache_flush();
39260Sstevel@tonic-gate 	portmap_cache_flush();
39270Sstevel@tonic-gate }
39280Sstevel@tonic-gate #endif
39290Sstevel@tonic-gate 
39300Sstevel@tonic-gate /*
39310Sstevel@tonic-gate  * Returns 1, if port option is NFS_PORT or
39320Sstevel@tonic-gate  *	nfsd is running on the port given
39330Sstevel@tonic-gate  * Returns 0, if both port is not NFS_PORT and nfsd is not
39340Sstevel@tonic-gate  *	running on the port.
39350Sstevel@tonic-gate  */
39360Sstevel@tonic-gate 
39370Sstevel@tonic-gate static int
39380Sstevel@tonic-gate is_nfs_port(char *opts)
39390Sstevel@tonic-gate {
39400Sstevel@tonic-gate 	struct mnttab m;
39410Sstevel@tonic-gate 	uint_t nfs_port = 0;
39420Sstevel@tonic-gate 	struct servent sv;
39430Sstevel@tonic-gate 	char buf[256];
39440Sstevel@tonic-gate 	int got_port;
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate 	m.mnt_mntopts = opts;
39470Sstevel@tonic-gate 
39480Sstevel@tonic-gate 	/*
39490Sstevel@tonic-gate 	 * Get port specified in options list, if any.
39500Sstevel@tonic-gate 	 */
39510Sstevel@tonic-gate 	got_port = nopt(&m, MNTOPT_PORT, (int *)&nfs_port);
39520Sstevel@tonic-gate 
39530Sstevel@tonic-gate 	/*
39540Sstevel@tonic-gate 	 * if no port specified or it is same as NFS_PORT return nfs
39550Sstevel@tonic-gate 	 * To use any other daemon the port number should be different
39560Sstevel@tonic-gate 	 */
39570Sstevel@tonic-gate 	if (!got_port || nfs_port == NFS_PORT)
39580Sstevel@tonic-gate 		return (1);
39590Sstevel@tonic-gate 	/*
39600Sstevel@tonic-gate 	 * If daemon is nfsd, return nfs
39610Sstevel@tonic-gate 	 */
39620Sstevel@tonic-gate 	if (getservbyport_r(nfs_port, NULL, &sv, buf, 256) == &sv &&
39630Sstevel@tonic-gate 		strcmp(sv.s_name, "nfsd") == 0)
39640Sstevel@tonic-gate 		return (1);
39650Sstevel@tonic-gate 
39660Sstevel@tonic-gate 	/*
39670Sstevel@tonic-gate 	 * daemon is not nfs
39680Sstevel@tonic-gate 	 */
39690Sstevel@tonic-gate 	return (0);
39700Sstevel@tonic-gate }
39710Sstevel@tonic-gate 
39720Sstevel@tonic-gate 
39730Sstevel@tonic-gate /*
39740Sstevel@tonic-gate  * destroy_auth_client_handle(cl)
39750Sstevel@tonic-gate  * destroys the created client handle
39760Sstevel@tonic-gate  */
39770Sstevel@tonic-gate static void
39780Sstevel@tonic-gate destroy_auth_client_handle(CLIENT *cl)
39790Sstevel@tonic-gate {
39800Sstevel@tonic-gate 	if (cl) {
39810Sstevel@tonic-gate 		if (cl->cl_auth) {
39820Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39830Sstevel@tonic-gate 			drop_alloc("AUTH_HANDLE", cl->cl_auth,
39840Sstevel@tonic-gate 				__FILE__, __LINE__);
39850Sstevel@tonic-gate #endif
39860Sstevel@tonic-gate 			AUTH_DESTROY(cl->cl_auth);
39870Sstevel@tonic-gate 			cl->cl_auth = NULL;
39880Sstevel@tonic-gate 		}
39890Sstevel@tonic-gate #ifdef MALLOC_DEBUG
39900Sstevel@tonic-gate 		drop_alloc("CLNT_HANDLE", cl,
39910Sstevel@tonic-gate 			__FILE__, __LINE__);
39920Sstevel@tonic-gate #endif
39930Sstevel@tonic-gate 		clnt_destroy(cl);
39940Sstevel@tonic-gate 	}
39950Sstevel@tonic-gate }
39960Sstevel@tonic-gate 
39970Sstevel@tonic-gate 
39980Sstevel@tonic-gate /*
39990Sstevel@tonic-gate  * Attempt to figure out which version of NFS to use in pingnfs().  If
40000Sstevel@tonic-gate  * the version number was specified (i.e., non-zero), then use it.
40010Sstevel@tonic-gate  * Otherwise, default to the compiled-in default or the default as set
40020Sstevel@tonic-gate  * by the /etc/default/nfs configuration (as read by read_default().
40030Sstevel@tonic-gate  */
40040Sstevel@tonic-gate int
40050Sstevel@tonic-gate set_versrange(rpcvers_t nfsvers, rpcvers_t *vers, rpcvers_t *versmin)
40060Sstevel@tonic-gate {
40070Sstevel@tonic-gate 	switch (nfsvers) {
40080Sstevel@tonic-gate 	case 0:
40090Sstevel@tonic-gate 		*vers = vers_max_default;
40100Sstevel@tonic-gate 		*versmin = vers_min_default;
40110Sstevel@tonic-gate 		break;
40120Sstevel@tonic-gate 	case NFS_V4:
40130Sstevel@tonic-gate 		*vers = NFS_V4;
40140Sstevel@tonic-gate 		*versmin = NFS_V4;
40150Sstevel@tonic-gate 		break;
40160Sstevel@tonic-gate 	case NFS_V3:
40170Sstevel@tonic-gate 		*vers = NFS_V3;
40180Sstevel@tonic-gate 		*versmin = NFS_V3;
40190Sstevel@tonic-gate 		break;
40200Sstevel@tonic-gate 	case NFS_VERSION:
40210Sstevel@tonic-gate 		*vers = NFS_VERSION;		/* version 2 */
40220Sstevel@tonic-gate 		*versmin = NFS_VERSMIN;		/* version 2 */
40230Sstevel@tonic-gate 		break;
40240Sstevel@tonic-gate 	default:
40250Sstevel@tonic-gate 		return (-1);
40260Sstevel@tonic-gate 	}
40270Sstevel@tonic-gate 	return (0);
40280Sstevel@tonic-gate }
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate #ifdef CACHE_DEBUG
40310Sstevel@tonic-gate /*
40320Sstevel@tonic-gate  * trace_portmap_cache()
40330Sstevel@tonic-gate  * traces the portmap cache values at desired points
40340Sstevel@tonic-gate  */
40350Sstevel@tonic-gate static void
40360Sstevel@tonic-gate trace_portmap_cache()
40370Sstevel@tonic-gate {
40380Sstevel@tonic-gate 	syslog(LOG_ERR, "portmap_cache: accesses=%d lookups=%d hits=%d\n",
40390Sstevel@tonic-gate 		portmap_cache_accesses, portmap_cache_lookups,
40400Sstevel@tonic-gate 		portmap_cache_hits);
40410Sstevel@tonic-gate }
40420Sstevel@tonic-gate 
40430Sstevel@tonic-gate /*
40440Sstevel@tonic-gate  * trace_host_cache()
40450Sstevel@tonic-gate  * traces the host cache values at desired points
40460Sstevel@tonic-gate  */
40470Sstevel@tonic-gate static void
40480Sstevel@tonic-gate trace_host_cache()
40490Sstevel@tonic-gate {
40500Sstevel@tonic-gate 	syslog(LOG_ERR,
40510Sstevel@tonic-gate 		"host_cache: accesses=%d lookups=%d deadhits=%d goodhits=%d\n",
40520Sstevel@tonic-gate 		host_cache_accesses, host_cache_lookups, deadhost_cache_hits,
40530Sstevel@tonic-gate 		goodhost_cache_hits);
40540Sstevel@tonic-gate }
40550Sstevel@tonic-gate #endif /* CACHE_DEBUG */
40560Sstevel@tonic-gate 
40570Sstevel@tonic-gate /*
40580Sstevel@tonic-gate  * Read the /etc/default/nfs configuration file to determine if the
40590Sstevel@tonic-gate  * client has been configured for a new min/max for the NFS version to
40600Sstevel@tonic-gate  * use.
40610Sstevel@tonic-gate  */
40620Sstevel@tonic-gate 
40630Sstevel@tonic-gate #define	NFS_DEFAULT_CHECK 60  /* Seconds to check for nfs default changes */
40640Sstevel@tonic-gate 
40650Sstevel@tonic-gate static void
40660Sstevel@tonic-gate read_default_nfs(void)
40670Sstevel@tonic-gate {
40680Sstevel@tonic-gate 	static time_t lastread = 0;
40690Sstevel@tonic-gate 	struct stat buf;
40700Sstevel@tonic-gate 	char *defval;
40710Sstevel@tonic-gate 	int errno;
40720Sstevel@tonic-gate 	int tmp;
40730Sstevel@tonic-gate 
40740Sstevel@tonic-gate 	/*
40750Sstevel@tonic-gate 	 * Fail silently if we can't stat the default nfs config file
40760Sstevel@tonic-gate 	 */
40770Sstevel@tonic-gate 	if (stat(NFSADMIN, &buf))
40780Sstevel@tonic-gate 		return;
40790Sstevel@tonic-gate 
40800Sstevel@tonic-gate 	if (buf.st_mtime == lastread)
40810Sstevel@tonic-gate 		return;
40820Sstevel@tonic-gate 
40830Sstevel@tonic-gate 	/*
40840Sstevel@tonic-gate 	 * Fail silently if error in opening the default nfs config file
40850Sstevel@tonic-gate 	 * We'll check back in NFS_DEFAULT_CHECK seconds
40860Sstevel@tonic-gate 	 */
40870Sstevel@tonic-gate 	if ((defopen(NFSADMIN)) == 0) {
40880Sstevel@tonic-gate 		if ((defval = defread("NFS_CLIENT_VERSMIN=")) != NULL) {
40890Sstevel@tonic-gate 			errno = 0;
40900Sstevel@tonic-gate 			tmp = strtol(defval, (char **)NULL, 10);
40910Sstevel@tonic-gate 			if (errno == 0) {
40920Sstevel@tonic-gate 				vers_min_default = tmp;
40930Sstevel@tonic-gate 			}
40940Sstevel@tonic-gate 		}
40950Sstevel@tonic-gate 		if ((defval = defread("NFS_CLIENT_VERSMAX=")) != NULL) {
40960Sstevel@tonic-gate 			errno = 0;
40970Sstevel@tonic-gate 			tmp = strtol(defval, (char **)NULL, 10);
40980Sstevel@tonic-gate 			if (errno == 0) {
40990Sstevel@tonic-gate 				vers_max_default = tmp;
41000Sstevel@tonic-gate 			}
41010Sstevel@tonic-gate 		}
41020Sstevel@tonic-gate 		/* close defaults file */
41030Sstevel@tonic-gate 		defopen(NULL);
41040Sstevel@tonic-gate 
41050Sstevel@tonic-gate 		lastread = buf.st_mtime;
41060Sstevel@tonic-gate 
41070Sstevel@tonic-gate 		/*
41080Sstevel@tonic-gate 		 * Quick sanity check on the values picked up from the
41090Sstevel@tonic-gate 		 * defaults file.  Make sure that a mistake wasn't
41100Sstevel@tonic-gate 		 * made that will confuse things later on.
41110Sstevel@tonic-gate 		 * If so, reset to compiled-in defaults
41120Sstevel@tonic-gate 		 */
41130Sstevel@tonic-gate 		if (vers_min_default > vers_max_default ||
41140Sstevel@tonic-gate 			vers_min_default < NFS_VERSMIN ||
41150Sstevel@tonic-gate 			vers_max_default > NFS_VERSMAX) {
41160Sstevel@tonic-gate 			if (trace > 1) {
41170Sstevel@tonic-gate 				trace_prt(1,
41180Sstevel@tonic-gate 	"  read_default: version minimum/maximum incorrectly configured\n");
41190Sstevel@tonic-gate 				trace_prt(1,
41200Sstevel@tonic-gate "  read_default: config is min=%d, max%d. Resetting to min=%d, max%d\n",
41210Sstevel@tonic-gate 					vers_min_default, vers_max_default,
41220Sstevel@tonic-gate 					NFS_VERSMIN_DEFAULT,
41230Sstevel@tonic-gate 					NFS_VERSMAX_DEFAULT);
41240Sstevel@tonic-gate 			}
41250Sstevel@tonic-gate 			vers_min_default = NFS_VERSMIN_DEFAULT;
41260Sstevel@tonic-gate 			vers_max_default = NFS_VERSMAX_DEFAULT;
41270Sstevel@tonic-gate 		}
41280Sstevel@tonic-gate 	}
41290Sstevel@tonic-gate }
41300Sstevel@tonic-gate 
41310Sstevel@tonic-gate /*
41320Sstevel@tonic-gate  *  Find the mnttab entry that corresponds to "name".
41330Sstevel@tonic-gate  *  We're not sure what the name represents: either
41340Sstevel@tonic-gate  *  a mountpoint name, or a special name (server:/path).
41350Sstevel@tonic-gate  *  Return the last entry in the file that matches.
41360Sstevel@tonic-gate  */
41370Sstevel@tonic-gate static struct extmnttab *
41380Sstevel@tonic-gate mnttab_find(dirname)
41390Sstevel@tonic-gate 	char *dirname;
41400Sstevel@tonic-gate {
41410Sstevel@tonic-gate 	FILE *fp;
41420Sstevel@tonic-gate 	struct extmnttab mnt;
41430Sstevel@tonic-gate 	struct extmnttab *res = NULL;
41440Sstevel@tonic-gate 
41450Sstevel@tonic-gate 	fp = fopen(MNTTAB, "r");
41460Sstevel@tonic-gate 	if (fp == NULL) {
41470Sstevel@tonic-gate 		if (trace > 1)
41480Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to open mnttab\n");
41490Sstevel@tonic-gate 		return (NULL);
41500Sstevel@tonic-gate 	}
41510Sstevel@tonic-gate 	while (getextmntent(fp, &mnt, sizeof (struct extmnttab)) == 0) {
41520Sstevel@tonic-gate 		if (strcmp(mnt.mnt_mountp, dirname) == 0 ||
41530Sstevel@tonic-gate 		    strcmp(mnt.mnt_special, dirname) == 0) {
41540Sstevel@tonic-gate 			if (res)
41550Sstevel@tonic-gate 				fsfreemnttab(res);
41560Sstevel@tonic-gate 			res = fsdupmnttab(&mnt);
41570Sstevel@tonic-gate 		}
41580Sstevel@tonic-gate 	}
41590Sstevel@tonic-gate 
41600Sstevel@tonic-gate 	resetmnttab(fp);
41610Sstevel@tonic-gate 	fclose(fp);
41620Sstevel@tonic-gate 	if (res == NULL) {
41630Sstevel@tonic-gate 		if (trace > 1)
41640Sstevel@tonic-gate 			trace_prt(1, "	mnttab_find: unable to find %s\n",
41650Sstevel@tonic-gate 				dirname);
41660Sstevel@tonic-gate 	}
41670Sstevel@tonic-gate 	return (res);
41680Sstevel@tonic-gate }
41690Sstevel@tonic-gate 
41700Sstevel@tonic-gate /*
41710Sstevel@tonic-gate  * This function's behavior is taken from nfsstat.
41720Sstevel@tonic-gate  * Trying to determine what NFS version was used for the mount.
41730Sstevel@tonic-gate  */
41740Sstevel@tonic-gate static int
41750Sstevel@tonic-gate is_v4_mount(char *mntpath)
41760Sstevel@tonic-gate {
41770Sstevel@tonic-gate 	kstat_ctl_t *kc = NULL;		/* libkstat cookie */
41780Sstevel@tonic-gate 	kstat_t *ksp;
41790Sstevel@tonic-gate 	ulong_t fsid;
41800Sstevel@tonic-gate 	struct mntinfo_kstat mik;
41810Sstevel@tonic-gate 	struct extmnttab *mntp;
41820Sstevel@tonic-gate 	uint_t mnt_minor;
41830Sstevel@tonic-gate 
41840Sstevel@tonic-gate 	if ((mntp = mnttab_find(mntpath)) == NULL)
41850Sstevel@tonic-gate 		return (FALSE);
41860Sstevel@tonic-gate 
41870Sstevel@tonic-gate 	/* save the minor number and free the struct so we don't forget */
41880Sstevel@tonic-gate 	mnt_minor = mntp->mnt_minor;
41890Sstevel@tonic-gate 	fsfreemnttab(mntp);
41900Sstevel@tonic-gate 
41910Sstevel@tonic-gate 	if ((kc = kstat_open()) == NULL)
41920Sstevel@tonic-gate 		return (FALSE);
41930Sstevel@tonic-gate 
41940Sstevel@tonic-gate 	for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) {
41950Sstevel@tonic-gate 		if (ksp->ks_type != KSTAT_TYPE_RAW)
41960Sstevel@tonic-gate 			continue;
41970Sstevel@tonic-gate 		if (strcmp(ksp->ks_module, "nfs") != 0)
41980Sstevel@tonic-gate 			continue;
41990Sstevel@tonic-gate 		if (strcmp(ksp->ks_name, "mntinfo") != 0)
42000Sstevel@tonic-gate 			continue;
42010Sstevel@tonic-gate 		if (mnt_minor != ksp->ks_instance)
42020Sstevel@tonic-gate 			continue;
42030Sstevel@tonic-gate 
42040Sstevel@tonic-gate 		if (kstat_read(kc, ksp, &mik) == -1)
42050Sstevel@tonic-gate 			continue;
42060Sstevel@tonic-gate 
42070Sstevel@tonic-gate 		(void) kstat_close(kc);
42080Sstevel@tonic-gate 		if (mik.mik_vers == 4)
42090Sstevel@tonic-gate 			return (TRUE);
42100Sstevel@tonic-gate 		else
42110Sstevel@tonic-gate 			return (FALSE);
42120Sstevel@tonic-gate 	}
42130Sstevel@tonic-gate 	(void) kstat_close(kc);
42140Sstevel@tonic-gate 
42150Sstevel@tonic-gate 	return (FALSE);
42160Sstevel@tonic-gate }
4217